Example #1
0
        public HlsDownloader(HlsMediaPlaylist mediaPlaylist)
        {
            _mediaPlaylist = mediaPlaylist;

            _keyCache    = new Dictionary <Uri, byte[]>();
            _nextSegment = 0;
        }
Example #2
0
        private void HandleMediaPlaylistLine(string line)
        {
            if (_isMasterPlaylist.HasValue && _isMasterPlaylist.Value)
            {
                throw new HlsException("Unexpected tag in media playlist.");
            }

            if (!_isMasterPlaylist.HasValue)
            {
                _isMasterPlaylist = false;
                _mediaPlaylist    = new HlsMediaPlaylist {
                    Version = _version, IsIndependentSegments = _isIndependentSegments, Start = _start
                };
            }

            if (line[0] != '#')
            {
                // A URI line for a media segment should be the only case when this happens.
                _mediaSegment.Uri = new Uri(_baseUri, line);
                _mediaSegment     = null;
                return;
            }

            int    firstColonIndex = line.IndexOf(':');
            string tag             = (firstColonIndex > -1 ? line.Substring(1, firstColonIndex - 1) : line.Substring(1));
            string value           = (firstColonIndex > -1 ? line.Substring(firstColonIndex + 1, line.Length - firstColonIndex - 1) : null);

            switch (tag)
            {
            // Media segment tags.
            case "EXTINF":
                int commaIndex = value.IndexOf(',');
                _mediaSegment = new HlsMediaSegment
                {
                    SequenceNumber = _mediaPlaylist.MediaSequence + _mediaPlaylist._mediaSegments.Count,
                    Duration       = Convert.ToSingle((commaIndex != -1 ? value.Substring(0, commaIndex) : value)),
                    Title          = (commaIndex != -1 ? value.Substring(commaIndex + 1) : null),
                    Key            = _mediaSegmentKey
                };
                _mediaPlaylist._mediaSegments.Add(_mediaSegment);
                break;

            case "EXT-X-BYTERANGE":
                _mediaSegment.ByteRange = value;
                break;

            case "EXT-X-DISCONTINUITY":
                _mediaSegment.HasDiscontinuityAfter = true;
                break;

            case "EXT-X-KEY":
                IReadOnlyDictionary <string, string> keyAttributes = AttributeList.Parse(value);
                _mediaSegmentKey = new HlsMediaSegmentKey
                {
                    Method    = AttributeValueUtils.ParseEnum <HlsKeyMethod>(keyAttributes["METHOD"]),
                    Uri       = AttributeValueUtils.GetUriOrNull(keyAttributes, _baseUri, "URI"),
                    KeyFormat = (AttributeValueUtils.GetStringOrNull(keyAttributes, "KEYFORMAT") ?? "identity"),
                };
                break;

            case "EXT-X-MAP":
                break;

            case "EXT-X-PROGRAM-DATE-TIME":
                _mediaSegment.ProgramDateTime = value;
                break;

            case "EXT-X-ALLOW-CACHE":
                break;

            case "EXT-X-DATERANGE":
                break;

            // Media playlist tags.
            case "EXT-X-TARGETDURATION":
                _mediaPlaylist.TargetDuration = Convert.ToInt32(value);
                break;

            case "EXT-X-MEDIA-SEQUENCE":
                _mediaPlaylist.MediaSequence = Convert.ToInt32(value);
                break;

            case "EXT-X-DISCONTINUITY-SEQUENCE":
                _mediaPlaylist.DiscontinuitySequence = Convert.ToInt32(value);
                break;

            case "EXT-X-ENDLIST":
                _mediaSegment = null;
                HasEndlist    = true;
                break;

            case "EXT-X-PLAYLIST-TYPE":
                _mediaPlaylist.PlaylistType = value;
                break;

            case "EXT-X-I-FRAMES-ONLY":
                _mediaPlaylist.IsIFramesOnly = true;
                break;
            }
        }