Пример #1
0
        private async Task ReadResponseAsync(IMediaParser mediaParser, IWebStreamResponse webStreamResponse, WebResponse webResponse, SingleStreamMediaManager.QueueThrottle throttle, CancellationToken cancellationToken)
        {
            byte[] buffer           = new byte[16384];
            Task   cancellationTask = CancellationTokenExtensions.AsTask(cancellationToken);

            try
            {
                ISegmentMetadata segmentMetadata = this._webMetadataFactory.CreateSegmentMetadata(webResponse, (ContentType)null);
                mediaParser.StartSegment(segmentMetadata);
                using (Stream stream = await webStreamResponse.GetStreamAsync(cancellationToken).ConfigureAwait(false))
                {
                    while (true)
                    {
                        Task waitTask = throttle.WaitAsync();
                        if (!waitTask.IsCompleted)
                        {
                            Task task = await TaskEx.WhenAny(waitTask, cancellationTask).ConfigureAwait(false);

                            cancellationToken.ThrowIfCancellationRequested();
                        }
                        int length = await stream.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false);

                        if (length > 0)
                        {
                            mediaParser.ProcessData(buffer, 0, length);
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }
            finally
            {
                mediaParser.ProcessEndOfData();
            }
        }
Пример #2
0
        private async Task SimplePlayAsync(ContentType contentType, IWebReader webReader, IWebStreamResponse webStreamResponse, WebResponse webResponse, TaskCompletionSource <bool> configurationTaskCompletionSource, CancellationToken cancellationToken)
        {
            try
            {
                this._mediaStreamConfigurator.Initialize();
                this._mediaStreamConfigurator.MediaManager = (IMediaManager)this;
                IMediaParser mediaParser = await this._mediaParserFactory.CreateAsync((IMediaParserParameters) new MediaParserParameters(), contentType, cancellationToken).ConfigureAwait(false);

                if (null == mediaParser)
                {
                    throw new NotSupportedException("Unsupported content type: " + (object)contentType);
                }
                this.State = MediaManagerState.Opening;
                EventHandler configurationComplete = (EventHandler)null;
                configurationComplete = (EventHandler)((sender, args) =>
                {
                    mediaParser.ConfigurationComplete -= configurationComplete;
                    configurationTaskCompletionSource.TrySetResult(true);
                });
                mediaParser.ConfigurationComplete += configurationComplete;
                using (IBufferingManager bufferingManager = this._bufferingManagerFactory())
                {
                    SingleStreamMediaManager.QueueThrottle throttle = new SingleStreamMediaManager.QueueThrottle();
                    bufferingManager.Initialize((IQueueThrottling)throttle, new Action(this._mediaStreamConfigurator.CheckForSamples));
                    mediaParser.Initialize(bufferingManager, (Action <IProgramStreams>)null);
                    IStreamMetadata streamMetadata = this._webMetadataFactory.CreateStreamMetadata(webResponse, (ContentType)null);
                    mediaParser.InitializeStream(streamMetadata);
                    Task reader = (Task)null;
                    try
                    {
                        using (webReader)
                        {
                            try
                            {
                                if (null == webStreamResponse)
                                {
                                    webStreamResponse = await webReader.GetWebStreamAsync((Uri)null, false, cancellationToken, (Uri)null, new long?(), new long?(), webResponse).ConfigureAwait(false);
                                }
                                reader = this.ReadResponseAsync(mediaParser, webStreamResponse, webResponse, throttle, cancellationToken);
                                Task task = await TaskEx.WhenAny((Task)configurationTaskCompletionSource.Task, CancellationTokenExtensions.AsTask(cancellationToken)).ConfigureAwait(false);

                                cancellationToken.ThrowIfCancellationRequested();
                                await MediaStreamSourceExtensions.PlayAsync(this._mediaStreamConfigurator, (IEnumerable <IMediaParserMediaStream>) mediaParser.MediaStreams, new TimeSpan?(), cancellationToken).ConfigureAwait(false);

                                this.State = MediaManagerState.Playing;
                                await reader.ConfigureAwait(false);

                                reader = (Task)null;
                            }
                            finally
                            {
                                if (null != webStreamResponse)
                                {
                                    webStreamResponse.Dispose();
                                }
                            }
                        }
                    }
                    catch (OperationCanceledException ex)
                    {
                    }
                    catch (Exception ex)
                    {
                        string message = ExceptionExtensions.ExtendedMessage(ex);
                        Debug.WriteLine("SingleStreamMediaManager.SimplePlayAsync() failed: " + message);
                        this.SetMediaState(MediaManagerState.Error, message);
                    }
                    this.State = MediaManagerState.Closing;
                    if (null != reader)
                    {
                        try
                        {
                            await reader.ConfigureAwait(false);
                        }
                        catch (OperationCanceledException ex)
                        {
                        }
                        catch (Exception ex)
                        {
                            Debug.WriteLine("SingleStreamMediaManager.SimplePlayAsync() reader failed: " + ExceptionExtensions.ExtendedMessage(ex));
                        }
                    }
                    mediaParser.ConfigurationComplete -= configurationComplete;
                    mediaParser.EnableProcessing       = false;
                    mediaParser.FlushBuffers();
                    bufferingManager.Flush();
                    bufferingManager.Shutdown((IQueueThrottling)throttle);
                    await this._mediaStreamConfigurator.CloseAsync().ConfigureAwait(false);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("SingleStreamMediaManager.SimplePlayAsync() cleanup failed: " + ExceptionExtensions.ExtendedMessage(ex));
            }
            this._mediaStreamConfigurator.MediaManager = (IMediaManager)null;
            if (!configurationTaskCompletionSource.Task.IsCompleted)
            {
                configurationTaskCompletionSource.TrySetCanceled();
            }
            this.State = MediaManagerState.Closed;
            await this._reportStateTask.WaitAsync().ConfigureAwait(false);
        }