protected virtual async Task <ISegmentManager> CreateManagerAsync(PlsParser pls, IWebReader webReader, CancellationToken cancellationToken)
        {
            Uri trackUrl = await this._plsSegmentManagerPolicy.GetTrackAsync(pls, webReader.ContentType, cancellationToken);

            ISegmentManager segmentManager;

            if ((Uri)null == trackUrl)
            {
                segmentManager = (ISegmentManager)null;
            }
            else
            {
                ContentType contentType = await WebReaderExtensions.DetectContentTypeAsync(webReader, trackUrl, ContentKind.AnyMedia, cancellationToken).ConfigureAwait(false);

                if ((ContentType)null == contentType)
                {
                    Debug.WriteLine("PlsSegmentManagerFactory.CreateSegmentManager() unable to detect type for " + (object)trackUrl);
                    segmentManager = (ISegmentManager)null;
                }
                else
                {
                    segmentManager = (ISegmentManager) new SimpleSegmentManager(webReader, (IEnumerable <Uri>) new Uri[1]
                    {
                        trackUrl
                    }, contentType);
                }
            }
            return(segmentManager);
        }
        public static Task <Uri> ParseAsync(this M3U8Parser parser, IWebReader webReader, IRetryManager retryManager, Uri playlist, CancellationToken cancellationToken)
        {
            IRetry retry = RetryManagerExtensions.CreateWebRetry(retryManager, 2, 250);

            return(retry.CallAsync <Uri>((Func <Task <Uri> >)(() => WebReaderExtensions.ReadStreamAsync <Uri>(webReader, playlist, retry, (Func <Uri, Stream, Uri>)((actualPlaylist, stream) =>
            {
                M3U8ParserExtensions.Parse(parser, actualPlaylist, stream, (Encoding)null);
                return actualPlaylist;
            }), cancellationToken)), cancellationToken));
        }
 private void UpdateSubPlaylistCache(Uri playlist)
 {
     if (this._subPlaylistCache != null && !(this._subPlaylistCache.WebReader.BaseAddress != playlist))
     {
         return;
     }
     if (null != this._subPlaylistCache)
     {
         this._subPlaylistCache.WebReader.Dispose();
     }
     this._subPlaylistCache = WebReaderExtensions.CreateWebCache(this._webReader, playlist, ContentKind.Playlist, (ContentType)null);
 }
示例#4
0
        public async Task StartAsync()
        {
            this.ThrowIfDisposed();
            this._cancellationToken.ThrowIfCancellationRequested();
            SignalTask oldReadTask = (SignalTask)null;
            CancellationTokenSource cancellationTokenSource = (CancellationTokenSource)null;
            bool   lockTaken = false;
            object obj;

            try
            {
                Monitor.Enter(obj = this._segmentLock, ref lockTaken);
                if (!this._isRunning)
                {
                    if (this._abortTokenSource.IsCancellationRequested)
                    {
                        cancellationTokenSource = this._abortTokenSource;
                        this._abortTokenSource  = CancellationTokenSource.CreateLinkedTokenSource(this._cancellationToken);
                        oldReadTask             = this._readTask;
                        this._readTask          = new SignalTask(new Func <Task>(this.ReadSubList), this._abortTokenSource.Token);
                    }
                    this._isRunning = true;
                }
                else
                {
                    goto label_15;
                }
            }
            finally
            {
                if (lockTaken)
                {
                    Monitor.Exit(obj);
                }
            }
            await HlsPlaylistSegmentManager.CleanupReader(oldReadTask, cancellationTokenSource).ConfigureAwait(false);

            if (this._programStream.Segments != null && this._programStream.Segments.Count > 0)
            {
                this.UpdatePlaylist();
            }
            ISegment segment = await AsyncEnumerableExtensions.FirstOrDefaultAsync <ISegment>(this.Playlist).ConfigureAwait(false);

            if (null == segment)
            {
                Debug.WriteLine("HlsPlaylistSegmentManager.StartAsync() no segments found");
                throw new FileNotFoundException("Unable to find the first segment");
            }
            this.ContentType = await this._programStream.GetContentTypeAsync(this._cancellationToken).ConfigureAwait(false);

            this.WebReader = WebReaderExtensions.CreateChild(this._programStream.WebReader, (Uri)null, ContentKind.AnyMedia, this.ContentType);
            label_15 :;
        }
        public async Task <ContentType> GetContentTypeAsync(CancellationToken cancellationToken)
        {
            ContentType contentType;

            if (null == this._segments)
            {
                contentType = (ContentType)null;
            }
            else
            {
                ISegment segment0 = Enumerable.FirstOrDefault <ISegment>((IEnumerable <ISegment>) this._segments);
                if (segment0 == null || (Uri)null == segment0.Url)
                {
                    contentType = (ContentType)null;
                }
                else
                {
                    ((HlsProgramStream)this)._contentType = await WebReaderExtensions.DetectContentTypeAsync(this._subPlaylistCache.WebReader, segment0.Url, ContentKind.AnyMedia, cancellationToken).ConfigureAwait(false);

                    contentType = this._contentType;
                }
            }
            return(contentType);
        }
        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);
        }