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 Task<Uri> GetTrackAsync(PlsParser pls, ContentType contentType, CancellationToken cancellationToken)
        {
            var tracks = pls.Tracks;

            var track = SelectTrack(tracks);

            if (null == track)
                return Task.FromResult<Uri>(null);

            if (tracks.Count > 1)
                Debug.WriteLine("PlsSegmentManagerPolicy.GetTrackAsync() multiple tracks are not supported");

            if (null == track.File)
                Debug.WriteLine("PlsSegmentManagerPolicy.GetTrackAsync() track does not have a file");

            Uri trackUrl;
            if (!Uri.TryCreate(pls.BaseUrl, track.File, out trackUrl))
            {
                Debug.WriteLine("PlsSegmentManagerPolicy.GetTrackAsync() invalid track file: " + track.File);

                return Task.FromResult<Uri>(null);
            }

            return Task.FromResult(trackUrl);
        }
Example #3
0
        public Task <Uri> GetTrackAsync(PlsParser pls, ContentType contentType, CancellationToken cancellationToken)
        {
            ICollection <PlsTrack> tracks = pls.Tracks;
            PlsTrack plsTrack             = PlsSegmentManagerPolicy.SelectTrack(tracks);

            if (null == plsTrack)
            {
                return(TaskEx.FromResult <Uri>((Uri)null));
            }
            if (tracks.Count > 1)
            {
                Debug.WriteLine("PlsSegmentManagerPolicy.GetTrackAsync() multiple tracks are not supported");
            }
            if (null == plsTrack.File)
            {
                Debug.WriteLine("PlsSegmentManagerPolicy.GetTrackAsync() track does not have a file");
            }
            Uri result;

            if (Uri.TryCreate(pls.BaseUrl, plsTrack.File, out result))
            {
                return(TaskEx.FromResult <Uri>(result));
            }
            Debug.WriteLine("PlsSegmentManagerPolicy.GetTrackAsync() invalid track file: " + plsTrack.File);
            return(TaskEx.FromResult <Uri>((Uri)null));
        }
Example #4
0
        static async Task PlayAsync(SingleStreamSimulator simulator, CancellationToken cancellationToken)
        {
            var source = new Uri(Sources[0]);

            if (source.HasExtension(".pls"))
            {
                using (var client = new HttpClient())
                using (var response = await client.GetAsync(source, HttpCompletionOption.ResponseContentRead, cancellationToken).ConfigureAwait(false))
                {
                    response.EnsureSuccessStatusCode();

                    using (var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
                    using (var reader = new StreamReader(stream))
                    {
                        var pls = new PlsParser(response.RequestMessage.RequestUri);

                        if (!await pls.ParseAsync(reader).ConfigureAwait(false))
                            throw new FileNotFoundException("Unable to parse PLS playlist");

                        var track = pls.Tracks.FirstOrDefault();

                        if (null == track)
                            throw new FileNotFoundException("Empty PLS playlist");

                        Uri trackUrl;
                        if (!Uri.TryCreate(pls.BaseUrl, track.File, out trackUrl))
                            throw new FileNotFoundException("Invalid URL in PLS playlist");

                        source = trackUrl;
                    }
                }
            }

            await simulator.StartAsync(source, cancellationToken).ConfigureAwait(false);
        }
        protected virtual async Task <ISegmentManager> ReadPlaylistAsync(IWebReader webReader, Uri url, Stream stream, CancellationToken cancellationToken)
        {
            PlsParser       pls = new PlsParser(url);
            ISegmentManager segmentManager;

            using (StreamReader streamReader = new StreamReader(stream))
            {
                bool ret = await pls.ParseAsync((TextReader)streamReader).ConfigureAwait(false);

                if (!ret)
                {
                    segmentManager = (ISegmentManager)null;
                    goto label_8;
                }
            }
            segmentManager = await this.CreateManagerAsync(pls, webReader, cancellationToken).ConfigureAwait(false);

label_8:
            return(segmentManager);
        }
        protected virtual async Task<ISegmentManager> CreateManagerAsync(PlsParser pls, IWebReader webReader, ContentType contentType, ContentType streamContentType, CancellationToken cancellationToken)
        {
            var trackUrl = await _plsSegmentManagerPolicy.GetTrackAsync(pls, webReader.ContentType, cancellationToken);

            if (null == trackUrl)
                return null;

            if (null == streamContentType)
                streamContentType = await webReader.DetectContentTypeAsync(trackUrl, ContentKind.AnyMedia, cancellationToken).ConfigureAwait(false);

            //DumpIcy(headers.ResponseHeaders);

            if (null == streamContentType)
            {
                Debug.WriteLine("PlsSegmentManagerFactory.CreateManagerAsync() unable to detect type for " + trackUrl);
                return null;
            }

            return new SimpleSegmentManager(webReader, new[] { trackUrl }, contentType, streamContentType);
        }
Example #7
0
        async Task<Uri> GetUrlFromPlsPlaylistAsync(Uri url)
        {
            using (var client = new HttpClient())
            using (var response = await client.GetAsync(url, HttpCompletionOption.ResponseContentRead).AsTask(_cancellationToken).ConfigureAwait(false))
            {
                response.EnsureSuccessStatusCode();

                var buffer = await response.Content.ReadAsBufferAsync().AsTask(_cancellationToken).ConfigureAwait(false);

                var pls = new PlsParser(response.RequestMessage.RequestUri);

                using (var stream = new MemoryStream(buffer.ToArray(), false))
                using (var reader = new StreamReader(stream))
                {
                    if (!await pls.ParseAsync(reader).ConfigureAwait(false))
                        throw new FileNotFoundException("Unable to parse PLS playlist");
                }

                var track = pls.Tracks.FirstOrDefault();

                if (null == track)
                    throw new FileNotFoundException("Empty PLS playlist");

                Uri trackUrl;
                if (!Uri.TryCreate(pls.BaseUrl, track.File, out trackUrl))
                    throw new FileNotFoundException("Invalid URL in PLS playlist");

                url = trackUrl;
            }
            return url;
        }
Example #8
0
        private void HandleTrack(string line, string key, string value)
        {
            int startOfTrackNumber = PlsParser.FindStartOfTrackNumber(key);

            if (startOfTrackNumber < 0)
            {
                Debug.WriteLine("PlsParser.HandleTrack() unable to find track number: " + line);
            }
            else
            {
                string b = key.Substring(0, startOfTrackNumber + 1).Trim();
                int    result1;
                if (!int.TryParse(key.Substring(startOfTrackNumber + 1, key.Length - (startOfTrackNumber + 1)), NumberStyles.Integer, (IFormatProvider)CultureInfo.InvariantCulture, out result1))
                {
                    Debug.WriteLine("PlsParser.HandleTrack() invalid track number: " + line);
                }
                else
                {
                    PlsTrack plsTrack;
                    if (!this._tracks.TryGetValue(result1, out plsTrack))
                    {
                        plsTrack = new PlsTrack();
                        this._tracks[result1] = plsTrack;
                    }
                    if (string.Equals("File", b))
                    {
                        if (null != plsTrack.File)
                        {
                            Debug.WriteLine("PlsParser.Parser() duplicate file property for entry {0}: {1}", (object)result1, (object)line);
                        }
                        else
                        {
                            plsTrack.File = value;
                        }
                    }
                    else if (string.Equals("Title", b))
                    {
                        if (null != plsTrack.Title)
                        {
                            Debug.WriteLine("PlsParser.Parser() duplicate title property for entry {0}: {1}", (object)result1, (object)line);
                        }
                        else
                        {
                            plsTrack.Title = value;
                        }
                    }
                    else if (string.Equals("Length", b))
                    {
                        if (plsTrack.Length.HasValue)
                        {
                            Debug.WriteLine("PlsParser.Parser() duplicate length property for entry {0}: {1}", (object)result1, (object)line);
                        }
                        else
                        {
                            Decimal result2;
                            if (!Decimal.TryParse(value, NumberStyles.Number, (IFormatProvider)CultureInfo.InvariantCulture, out result2))
                            {
                                Debug.WriteLine("PlsParser.Parser() invalid length property for entry {0}: {1}", (object)result1, (object)line);
                            }
                            else
                            {
                                try
                                {
                                    plsTrack.Length = new TimeSpan?(TimeSpan.FromSeconds((double)result2));
                                }
                                catch (InvalidCastException ex)
                                {
                                    Debug.WriteLine("PlsParser.Parser() invalid numeric length property for entry {0}: {1}", (object)result1, (object)line);
                                }
                            }
                        }
                    }
                    else
                    {
                        Debug.WriteLine("PlsParser.Parse() unknown property: " + line);
                    }
                }
            }
        }
        protected virtual async Task<ISegmentManager> ReadPlaylistAsync(IWebReader webReader, Uri url, Stream stream, ContentType contentType, ContentType streamContentType, CancellationToken cancellationToken)
        {
            var pls = new PlsParser(url);

            using (var tr = new StreamReader(stream))
            {
                var ret = await pls.ParseAsync(tr).ConfigureAwait(false);

                if (!ret)
                    return null;
            }

            return await CreateManagerAsync(pls, webReader, contentType, streamContentType, cancellationToken).ConfigureAwait(false);
        }