public async Task <IMediaStreamConfigurator> OpenMediaAsync(ICollection <Uri> source, CancellationToken cancellationToken)
        {
            this.State = MediaManagerState.OpenMedia;
            WebResponse response = new WebResponse();
            IMediaStreamConfigurator streamConfigurator;

            using (IWebReader rootReader = WebReaderManagerExtensions.CreateRootReader(this._webReaderManager, (ContentType)null))
            {
                using (IEnumerator <Uri> enumerator = source.GetEnumerator())
                {
label_25:
                    if (enumerator.MoveNext())
                    {
                        Uri                     url       = enumerator.Current;
                        IWebReader              webReader = (IWebReader)null;
                        IWebStreamResponse      webStream = (IWebStreamResponse)null;
                        CancellationTokenSource playCancellationTokenSource = (CancellationTokenSource)null;
                        Task                    playTask = (Task)null;
                        try
                        {
                            webReader = WebReaderExtensions.CreateChild(rootReader, url, ContentKind.Unknown, this.ContentType);
                            webStream = await webReader.GetWebStreamAsync((Uri)null, false, cancellationToken, (Uri)null, new long?(), new long?(), response).ConfigureAwait(false);

                            if (webStream.IsSuccessStatusCode)
                            {
                                ContentType contentType = response.ContentType;
                                if (!((ContentType)null == contentType))
                                {
                                    if ((ContentType)null == contentType || ContentKind.Playlist == contentType.Kind)
                                    {
                                        throw new FileNotFoundException("Content not supported with this media manager");
                                    }
                                    TaskCompletionSource <bool> configurationTaskCompletionSource = new TaskCompletionSource <bool>();
                                    playCancellationTokenSource = new CancellationTokenSource();
                                    CancellationTokenSource localPlayCancellationTokenSource = playCancellationTokenSource;
                                    Task cancelPlayTask = configurationTaskCompletionSource.Task.ContinueWith((Action <Task <bool> >)(t =>
                                    {
                                        if (!t.IsFaulted && !t.IsCanceled)
                                        {
                                            return;
                                        }
                                        localPlayCancellationTokenSource.Cancel();
                                    }));
                                    TaskCollector.Default.Add(cancelPlayTask, "SingleStreamMediaManager play cancellation");
                                    IWebReader         localWebReader        = webReader;
                                    IWebStreamResponse localWebStream        = webStream;
                                    CancellationToken  playCancellationToken = playCancellationTokenSource.Token;
                                    playTask = TaskEx.Run((Func <Task>)(() => this.SimplePlayAsync(contentType, localWebReader, localWebStream, response, configurationTaskCompletionSource, playCancellationToken)), playCancellationToken);
                                    bool   lockTaken = false;
                                    object obj;
                                    try
                                    {
                                        Monitor.Enter(obj = this._lock, ref lockTaken);
                                        this._playCancellationTokenSource = playCancellationTokenSource;
                                        playCancellationTokenSource       = (CancellationTokenSource)null;
                                        this._playTask = playTask;
                                        playTask       = (Task)null;
                                    }
                                    finally
                                    {
                                        if (lockTaken)
                                        {
                                            Monitor.Exit(obj);
                                        }
                                    }
                                    bool isConfigured = await configurationTaskCompletionSource.Task.ConfigureAwait(false);

                                    if (isConfigured)
                                    {
                                        webReader          = (IWebReader)null;
                                        webStream          = (IWebStreamResponse)null;
                                        streamConfigurator = this._mediaStreamConfigurator;
                                        goto label_33;
                                    }
                                    else
                                    {
                                        goto label_25;
                                    }
                                }
                                else
                                {
                                    goto label_25;
                                }
                            }
                            else
                            {
                                goto label_25;
                            }
                        }
                        finally
                        {
                            if (null != webStream)
                            {
                                webStream.Dispose();
                            }
                            if (null != webReader)
                            {
                                webReader.Dispose();
                            }
                            if (null != playCancellationTokenSource)
                            {
                                playCancellationTokenSource.Cancel();
                            }
                            if (null != playTask)
                            {
                                TaskCollector.Default.Add(playTask, "SingleStreamMediaManager play task");
                            }
                        }
                    }
                }
            }
            throw new FileNotFoundException();
label_33:
            return(streamConfigurator);
        }
예제 #2
0
 public Task <ISegmentManager> CreateAsync(ISegmentManagerParameters parameters, ContentType contentType, CancellationToken cancellationToken)
 {
     return(TaskEx.FromResult <ISegmentManager>((ISegmentManager) new SimpleSegmentManager(parameters.WebReader ?? WebReaderManagerExtensions.CreateRootReader(this._webReaderManager, ContentKind.AnyMedia, contentType), (IEnumerable <Uri>)parameters.Source, contentType)));
 }