@@ -26,8 +26,8 @@ var reKeyValue = regexp.MustCompile(`([a-zA-Z0-9_-]+)=("[^"]+"|[^",]+)`)
2626
2727// TimeParse allows globally apply and/or override Time Parser function.
2828// Available variants:
29- // * FullTimeParse - implements full featured ISO/IEC 8601:2004
30- // * StrictTimeParse - implements only RFC3339 Nanoseconds format
29+ // - FullTimeParse - implements full featured ISO/IEC 8601:2004
30+ // - StrictTimeParse - implements only RFC3339 Nanoseconds format
3131var TimeParse func (value string ) (time.Time , error ) = FullTimeParse
3232
3333// Decode parses a master playlist passed from the buffer. If `strict`
@@ -78,12 +78,37 @@ func (p *MasterPlaylist) decode(buf *bytes.Buffer, strict bool) error {
7878 return err
7979 }
8080 }
81+
82+ p .attachRenditionsToVariants (state .alternatives )
83+
8184 if strict && ! state .m3u {
8285 return errors .New ("#EXTM3U absent" )
8386 }
8487 return nil
8588}
8689
90+ func (p * MasterPlaylist ) attachRenditionsToVariants (alternatives []* Alternative ) {
91+ for _ , variant := range p .Variants {
92+ for _ , alt := range alternatives {
93+ if alt == nil {
94+ continue
95+ }
96+ if variant .Video != "" && alt .Type == "VIDEO" && variant .Video == alt .GroupId {
97+ variant .Alternatives = append (variant .Alternatives , alt )
98+ }
99+ if variant .Audio != "" && alt .Type == "AUDIO" && variant .Audio == alt .GroupId {
100+ variant .Alternatives = append (variant .Alternatives , alt )
101+ }
102+ if variant .Captions != "" && alt .Type == "CLOSED-CAPTIONS" && variant .Captions == alt .GroupId {
103+ variant .Alternatives = append (variant .Alternatives , alt )
104+ }
105+ if variant .Subtitles != "" && alt .Type == "SUBTITLES" && variant .Subtitles == alt .GroupId {
106+ variant .Alternatives = append (variant .Alternatives , alt )
107+ }
108+ }
109+ }
110+ }
111+
87112// Decode parses a media playlist passed from the buffer. If `strict`
88113// parameter is true then return first syntax error.
89114func (p * MediaPlaylist ) Decode (data bytes.Buffer , strict bool ) error {
@@ -220,6 +245,7 @@ func decode(buf *bytes.Buffer, strict bool, customDecoders []CustomDecoder) (Pla
220245 }
221246
222247 err = decodeLineOfMasterPlaylist (master , state , line , strict )
248+ master .attachRenditionsToVariants (state .alternatives )
223249 if strict && err != nil {
224250 return master , state .listType , err
225251 }
@@ -336,10 +362,6 @@ func decodeLineOfMasterPlaylist(p *MasterPlaylist, state *decodingState, line st
336362 state .tagStreamInf = true
337363 state .listType = MASTER
338364 state .variant = new (Variant )
339- if len (state .alternatives ) > 0 {
340- state .variant .Alternatives = state .alternatives
341- state .alternatives = nil
342- }
343365 p .Variants = append (p .Variants , state .variant )
344366 for k , v := range decodeParamsLine (line [18 :]) {
345367 switch k {
0 commit comments