Exemple #1
0
        public async Task InitializeAsync(ISegmentManagerReaders segmentManagerReaders, Action checkConfiguration, Action checkForSamples, CancellationToken cancellationToken, Action <IProgramStreams> programStreamsHandler)
        {
            this._checkConfiguration = checkConfiguration;
            Task        startReaderTask          = this._segmentReaders.Manager.StartAsync();
            MediaReader localReader              = this;
            QueueWorker <WorkBuffer> queueWorker = new QueueWorker <WorkBuffer>((Action <WorkBuffer>)(wi =>
            {
                IMediaParser mediaParser = localReader._mediaParser;
                if (null == wi)
                {
                    mediaParser.ProcessEndOfData();
                }
                else
                {
                    if (null != wi.Metadata)
                    {
                        mediaParser.StartSegment(wi.Metadata);
                        wi.Metadata = (ISegmentMetadata)null;
                    }
                    mediaParser.ProcessData(wi.Buffer, 0, wi.Length);
                }
            }), (Action <WorkBuffer>)(buffer => this._blockingPool.Free(buffer)));

            this._queueWorker    = queueWorker;
            this._callbackReader = new CallbackReader(segmentManagerReaders.Readers, new Action <WorkBuffer>(queueWorker.Enqueue), this._blockingPool);
            this._bufferingManager.Initialize((IQueueThrottling)queueWorker, checkForSamples);
            try
            {
                await startReaderTask.ConfigureAwait(false);

                ContentType contentType = this._segmentReaders.Manager.ContentType;
                if ((ContentType)null == contentType)
                {
                    Debug.WriteLine("MediaReader.CreateReaderPipeline() unable to determine content type, defaulting to transport stream");
                    contentType = ContentTypes.TransportStream;
                }
                else if (ContentTypes.Binary == contentType)
                {
                    Debug.WriteLine("MediaReader.CreateReaderPipeline() detected binary content, defaulting to transport stream");
                    contentType = ContentTypes.TransportStream;
                }
                MediaParserParameters mediaParserParameters = new MediaParserParameters();
                this._mediaParser = await this._mediaParserFactory.CreateAsync((IMediaParserParameters)mediaParserParameters, contentType, cancellationToken).ConfigureAwait(false);

                if (null == this._mediaParser)
                {
                    throw new NotSupportedException("Unsupported content type: " + (object)contentType);
                }
                this._mediaParser.ConfigurationComplete += new EventHandler(this.ConfigurationComplete);
                this._mediaParser.Initialize(this._bufferingManager, programStreamsHandler);
                this._mediaParser.InitializeStream(this._segmentReaders.Manager.StreamMetadata);
            }
            catch (Exception ex)
            {
                this._bufferingManager.Shutdown((IQueueThrottling)queueWorker);
                throw;
            }
        }
Exemple #2
0
        private async Task StopReadingAsync()
        {
            CallbackReader callbackReader = this._callbackReader;

            if (null != callbackReader)
            {
                try
                {
                    await callbackReader.StopAsync().ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("MediaReader.StopReadingAsync(): callback reader stop failed: " + ex.Message);
                }
            }
        }
Exemple #3
0
 public void Dispose()
 {
     if (0 != Interlocked.Exchange(ref this._isDisposed, 1))
     {
         return;
     }
     using (this._callbackReader)
         ;
     using (this._queueWorker)
         ;
     using (this._blockingPool)
         ;
     using (this._mediaParser)
         ;
     this._callbackReader   = (CallbackReader)null;
     this._queueWorker      = (QueueWorker <WorkBuffer>)null;
     this._blockingPool     = (IBlockingPool <WorkBuffer>)null;
     this._mediaParser      = (IMediaParser)null;
     this._bufferingManager = (IBufferingManager)null;
     this._segmentReaders   = (ISegmentManagerReaders)null;
 }
Exemple #4
0
        public void Dispose()
        {
            if (0 != Interlocked.Exchange(ref _isDisposed, 1))
                return;

            using (_callbackReader)
            { }

            using (_queueWorker)
            { }

            using (_blockingPool)
            { }

            using (_mediaParser)
            { }

            _callbackReader = null;
            _queueWorker = null;
            _blockingPool = null;
            _mediaParser = null;
            _bufferingManager = null;
            _segmentReaders = null;
        }
Exemple #5
0
        public async Task InitializeAsync(ISegmentManagerReaders segmentManagerReaders, Action checkConfiguration,
            Action checkForSamples, CancellationToken cancellationToken, Action<IProgramStreams> programStreamsHandler)
        {
            _checkConfiguration = checkConfiguration;

            var startReaderTask = _segmentReaders.Manager.StartAsync();

            var localReader = this;

            var queueWorker = new QueueWorker<WorkBuffer>(
                wi =>
                {
                    //Debug.WriteLine("MediaReader dequeued " + wi);

                    var mediaParser = localReader._mediaParser;

                    if (null == wi)
                        mediaParser.ProcessEndOfData();
                    else
                    {
                        if (null != wi.Metadata)
                        {
                            mediaParser.StartSegment(wi.Metadata);
                            wi.Metadata = null;
                        }

                        mediaParser.ProcessData(wi.Buffer, 0, wi.Length);
                    }
                },
                buffer => _blockingPool.Free(buffer));

            _queueWorker = queueWorker;

            _callbackReader = new CallbackReader(segmentManagerReaders.Readers, queueWorker.Enqueue, _blockingPool);

            _bufferingManager.Initialize(queueWorker, checkForSamples);

            try
            {
                await startReaderTask.ConfigureAwait(false);

                var streamContentType = _segmentReaders.Manager.StreamContentType;

                if (null == streamContentType)
                {
                    Debug.WriteLine("MediaReader.CreateReaderPipeline() unable to determine content type, defaulting to transport stream");

                    streamContentType = ContentTypes.TransportStream;
                }
                else if (ContentTypes.Binary == streamContentType)
                {
                    Debug.WriteLine("MediaReader.CreateReaderPipeline() detected binary content, defaulting to transport stream");

                    streamContentType = ContentTypes.TransportStream;
                }

                var mediaParserParameters = new MediaParserParameters();

                _mediaParser = await _mediaParserFactory.CreateAsync(mediaParserParameters, streamContentType, cancellationToken).ConfigureAwait(false);

                if (null == _mediaParser)
                    throw new NotSupportedException("Unsupported stream content type: " + streamContentType);

                _mediaParser.ConfigurationComplete += ConfigurationComplete;

                _mediaParser.Initialize(_bufferingManager, programStreamsHandler);

                _mediaParser.InitializeStream(_segmentReaders.Manager.StreamMetadata);
            }
            catch (Exception)
            {
                _bufferingManager.Shutdown(queueWorker);

                throw;
            }
        }