protected virtual IProgramManager CreateProgramManager(ICollection <Uri> source, ContentType contentType)
        {
            if (ContentTypes.M3U != contentType && ContentTypes.M3U8 != contentType)
            {
                throw new NotSupportedException(string.Format("Content type {0} not supported by this program manager", (ContentType)null == contentType ? (object)"<unknown>" : (object)contentType.ToString()));
            }
            HlsProgramManager hlsProgramManager = this._programManagerFactory();

            hlsProgramManager.Playlists = source;
            return((IProgramManager)hlsProgramManager);
        }
Example #2
0
        private static void AddMedia(Uri playlist, M3U8TagInstance gt, Dictionary <string, HlsProgramManager.MediaGroup> audioStreams)
        {
            string key       = M3U8TagInstanceExtensions.Attribute <string>(gt, ExtMediaSupport.AttrGroupId).Value;
            string uriString = M3U8TagInstanceExtensions.AttributeObject <string>(gt, ExtMediaSupport.AttrUri);
            Uri    uri       = (Uri)null;

            if (null != uriString)
            {
                uri = new Uri(playlist, new Uri(uriString, UriKind.RelativeOrAbsolute));
            }
            string            str = M3U8TagInstanceExtensions.AttributeObject <string>(gt, ExtMediaSupport.AttrLanguage);
            PlaylistSubStream playlistSubStream1 = new PlaylistSubStream();

            playlistSubStream1.Type         = M3U8TagInstanceExtensions.AttributeObject <string>(gt, ExtMediaSupport.AttrType);
            playlistSubStream1.Name         = key;
            playlistSubStream1.Playlist     = uri;
            playlistSubStream1.IsAutoselect = HlsProgramManager.IsYesNo(gt, ExtMediaSupport.AttrAutoselect, false);
            playlistSubStream1.Language     = str == null ? (string)null : str.Trim().ToLower();
            PlaylistSubStream playlistSubStream2 = playlistSubStream1;

            HlsProgramManager.MediaGroup mediaGroup;
            if (!audioStreams.TryGetValue(key, out mediaGroup))
            {
                mediaGroup = new HlsProgramManager.MediaGroup()
                {
                    Default = (SubStream)playlistSubStream2
                };
                audioStreams[key] = mediaGroup;
            }
            if (HlsProgramManager.IsYesNo(gt, ExtMediaSupport.AttrDefault, false))
            {
                mediaGroup.Default = (SubStream)playlistSubStream2;
            }
            string index = M3U8TagInstanceExtensions.Attribute <string>(gt, ExtMediaSupport.AttrName).Value;

            mediaGroup.Streams[index] = (SubStream)playlistSubStream2;
        }
Example #3
0
        private async Task <IDictionary <long, Program> > LoadAsync(IWebReader webReader, M3U8Parser parser, CancellationToken cancellationToken)
        {
            Dictionary <string, HlsProgramManager.MediaGroup> audioStreams = new Dictionary <string, HlsProgramManager.MediaGroup>();

            foreach (M3U8TagInstance m3U8TagInstance in parser.GlobalTags)
            {
                if (M3U8Tags.ExtXMedia == m3U8TagInstance.Tag)
                {
                    try
                    {
                        if (null != M3U8TagInstanceExtensions.Attribute <string>(m3U8TagInstance, ExtMediaSupport.AttrType, "AUDIO"))
                        {
                            HlsProgramManager.AddMedia(parser.BaseUrl, m3U8TagInstance, audioStreams);
                        }
                    }
                    catch (NullReferenceException ex)
                    {
                    }
                }
            }
            Dictionary <long, Program> programs = new Dictionary <long, Program>();
            bool hasSegments = false;

            foreach (M3U8Parser.M3U8Uri m3U8Uri in parser.Playlist)
            {
                if (m3U8Uri.Tags == null || m3U8Uri.Tags.Length < 1)
                {
                    hasSegments = true;
                }
                else
                {
                    ExtStreamInfTagInstance streamInfTagInstance = M3U8Tags.ExtXStreamInf.Find((IEnumerable <M3U8TagInstance>)m3U8Uri.Tags);
                    long programId = long.MinValue;
                    HlsProgramManager.MediaGroup mediaGroup = (HlsProgramManager.MediaGroup)null;
                    if (null != streamInfTagInstance)
                    {
                        M3U8AttributeValueInstance <long> attributeValueInstance1 = M3U8TagInstanceExtensions.Attribute <long>((M3U8TagInstance)streamInfTagInstance, ExtStreamInfSupport.AttrProgramId);
                        if (null != attributeValueInstance1)
                        {
                            programId = attributeValueInstance1.Value;
                        }
                        string key = M3U8TagInstanceExtensions.AttributeObject <string>((M3U8TagInstance)streamInfTagInstance, ExtStreamInfSupport.AttrAudio);
                        if (null != key)
                        {
                            audioStreams.TryGetValue(key, out mediaGroup);
                        }
                        Uri uri = M3U8ParserExtensions.ResolveUrl(parser, m3U8Uri.Uri);
                        M3U8AttributeValueInstance <long> attributeValueInstance2 = M3U8TagInstanceExtensions.Attribute <long>((M3U8TagInstance)streamInfTagInstance, ExtStreamInfSupport.AttrBandwidth);
                        ResolutionAttributeInstance       attributeInstance       = M3U8TagInstanceExtensions.AttributeInstance <ResolutionAttributeInstance>((M3U8TagInstance)streamInfTagInstance, ExtStreamInfSupport.AttrResolution);
                        Uri               baseUrl          = parser.BaseUrl;
                        Program           program          = HlsProgramManager.GetProgram((IDictionary <long, Program>)programs, programId, baseUrl);
                        IHlsProgramStream hlsProgramStream = this._programStreamFactory.Create((ICollection <Uri>) new Uri[1]
                        {
                            uri
                        }, webReader);
                        PlaylistSubProgram playlistSubProgram1 = new PlaylistSubProgram((IProgram)program, (IProgramStream)hlsProgramStream);
                        playlistSubProgram1.Bandwidth  = attributeValueInstance2 == null ? 0L : attributeValueInstance2.Value;
                        playlistSubProgram1.Playlist   = uri;
                        playlistSubProgram1.AudioGroup = mediaGroup;
                        PlaylistSubProgram playlistSubProgram2 = playlistSubProgram1;
                        if (null != attributeInstance)
                        {
                            playlistSubProgram2.Width  = new int?(attributeInstance.X);
                            playlistSubProgram2.Height = new int?(attributeInstance.Y);
                        }
                        program.SubPrograms.Add((ISubProgram)playlistSubProgram2);
                    }
                    else
                    {
                        hasSegments = true;
                    }
                }
            }
            if (hasSegments)
            {
                Program           program          = HlsProgramManager.GetProgram((IDictionary <long, Program>)programs, long.MinValue, parser.BaseUrl);
                IHlsProgramStream hlsProgramStream = this._programStreamFactory.Create((ICollection <Uri>) new Uri[1]
                {
                    webReader.RequestUri
                }, webReader);
                await hlsProgramStream.SetParserAsync(parser, cancellationToken).ConfigureAwait(false);

                PlaylistSubProgram subProgram = new PlaylistSubProgram((IProgram)program, (IProgramStream)hlsProgramStream);
                program.SubPrograms.Add((ISubProgram)subProgram);
            }
            return((IDictionary <long, Program>)programs);
        }