Exemplo n.º 1
0
        private void SubscribeFile(CancellationToken cancellation, WebCastData info, IConnectableObservable <byte[]> observable)
        {
            IObservable <byte[]> audioStream = info.Mime != "audio/mpeg"
                ? observable.Transcode(new PcmStream(info.Mime), FlacStream.Default)
                : observable;

            FileStream outStream = null;

            audioStream
            .SubscribeOn(TaskPoolScheduler.Default)
            .Subscribe(next =>
            {
                string ext = info.Mime == "audio/mpeg" ? "mp3" : "wav";

                if (outStream == null)
                {
                    outStream = File.OpenWrite($"websocket-test-{Guid.NewGuid():N}.{ext}");
                }

                outStream.Write(next, 0, next.Length);
            }, error =>
            {
                outStream?.Flush();
                outStream?.Dispose();
            }, () =>
            {
                outStream?.Flush();
                outStream?.Dispose();
            }, cancellation);
        }
Exemplo n.º 2
0
        private void SubscribeIcecast(CancellationToken cancellation, WebCastData info, IConnectableObservable <byte[]> observable)
        {
            Stream icecastStream             = null;
            IObservable <byte[]> audioStream = info.Mime != "audio/mpeg"
                ? observable.Transcode(new PcmStream(info.Mime), Mp3Stream.Default)
                : observable;

            audioStream
            .SubscribeOn(TaskPoolScheduler.Default)
            .Subscribe(next =>
            {
                try
                {
                    if (icecastStream == null)
                    {
                        icecastStream = _iceCaster.StartStreaming(_ekklesiaConfiguration.IceCastServer,
                                                                  _ekklesiaConfiguration.IceCastPassword,
                                                                  info.MountPoint,
                                                                  "audio/mpeg", false, "Test", "Test stream", "http://0.0.0.0",
                                                                  "Undefined");
                    }

                    icecastStream.Write(next, 0, next.Length);
                }
                catch (Exception e)
                {
                    _logger.Warning(e, "Error streaming");
                    try
                    {
                        icecastStream?.Flush();
                        icecastStream?.Dispose();
                        icecastStream = null;
                    }
                    catch (Exception exception)
                    {
                        _logger.Warning(exception, "Error cleaning up");
                    }
                }
            }, error =>
            {
                icecastStream?.Flush();
                icecastStream?.Dispose();
            },
                       () =>
            {
                icecastStream?.Flush();
                icecastStream?.Dispose();
            }, cancellation);
        }
Exemplo n.º 3
0
        private async Task HandleConnection(WebSocket webSocket, CancellationToken cancellation)
        {
            try
            {
                WebCastData streamInfo = await GetStreamInfo(webSocket, cancellation);

                IConnectableObservable <byte[]> observable = CreateObserver(webSocket);

                SubscribeFile(cancellation, streamInfo, observable);
                SubscribeIcecast(cancellation, streamInfo, observable);

                observable.Connect();
            }
            catch (Exception e)
            {
                _logger.Error(e, "Error handling web socket connection");
            }
        }