diff --git a/writer.go b/writer.go index dec3690c..6109cd27 100644 --- a/writer.go +++ b/writer.go @@ -315,7 +315,7 @@ func (p *MasterPlaylist) String() string { // NewMediaPlaylist creates a new media playlist structure. Winsize // defines how much items will displayed on playlist generation. // Capacity is total size of a playlist. -func NewMediaPlaylist(winsize uint, capacity uint) (*MediaPlaylist, error) { +func NewMediaPlaylist(winsize, capacity uint) (*MediaPlaylist, error) { p := new(MediaPlaylist) p.ver = minver p.capacity = capacity @@ -373,7 +373,7 @@ func (p *MediaPlaylist) AppendSegment(seg *MediaSegment) error { p.tail = (p.tail + 1) % p.capacity p.count++ if p.TargetDuration < seg.Duration { - p.TargetDuration = math.Ceil(seg.Duration) + p.TargetDuration = math.Round(seg.Duration) } p.buf.Reset() return nil @@ -614,32 +614,32 @@ func (p *MediaPlaylist) Encode() *bytes.Buffer { } } } - // check for key change - if seg.Key != nil && p.Key != seg.Key { - p.buf.WriteString("#EXT-X-KEY:") - p.buf.WriteString("METHOD=") - p.buf.WriteString(seg.Key.Method) - if seg.Key.Method != "NONE" { - p.buf.WriteString(",URI=\"") - p.buf.WriteString(seg.Key.URI) - p.buf.WriteRune('"') - if seg.Key.IV != "" { - p.buf.WriteString(",IV=") - p.buf.WriteString(seg.Key.IV) - } - if seg.Key.Keyformat != "" { - p.buf.WriteString(",KEYFORMAT=\"") - p.buf.WriteString(seg.Key.Keyformat) - p.buf.WriteRune('"') - } - if seg.Key.Keyformatversions != "" { - p.buf.WriteString(",KEYFORMATVERSIONS=\"") - p.buf.WriteString(seg.Key.Keyformatversions) - p.buf.WriteRune('"') - } - } - p.buf.WriteRune('\n') - } + // check for key seg change then change global key as well, but we no need it so comment first + // if seg.Key != nil && p.Key != nil && *p.Key != *seg.Key { + // p.buf.WriteString("#EXT-X-KEY:") + // p.buf.WriteString("METHOD=") + // p.buf.WriteString(seg.Key.Method) + // if seg.Key.Method != "NONE" { + // p.buf.WriteString(",URI=\"") + // p.buf.WriteString(seg.Key.URI) + // p.buf.WriteRune('"') + // if seg.Key.IV != "" { + // p.buf.WriteString(",IV=") + // p.buf.WriteString(seg.Key.IV) + // } + // if seg.Key.Keyformat != "" { + // p.buf.WriteString(",KEYFORMAT=\"") + // p.buf.WriteString(seg.Key.Keyformat) + // p.buf.WriteRune('"') + // } + // if seg.Key.Keyformatversions != "" { + // p.buf.WriteString(",KEYFORMATVERSIONS=\"") + // p.buf.WriteString(seg.Key.Keyformatversions) + // p.buf.WriteRune('"') + // } + // } + // p.buf.WriteRune('\n') + // } if seg.Discontinuity { p.buf.WriteString("#EXT-X-DISCONTINUITY\n") } @@ -717,12 +717,12 @@ func (p *MediaPlaylist) String() string { } // DurationAsInt represents the duration as the integer in encoded playlist. -func (p *MediaPlaylist) DurationAsInt(yes bool) { - if yes { +func (p *MediaPlaylist) DurationAsInt(isDurationasInt bool) { + if isDurationasInt { // duration must be integers if protocol version is less than 3 version(&p.ver, 3) } - p.durationAsInt = yes + p.durationAsInt = isDurationasInt } // Count tells us the number of items that are currently in the media @@ -813,7 +813,7 @@ func (p *MediaPlaylist) SetRange(limit, offset int64) error { // SetSCTE sets the SCTE cue format for the current media segment. // // Deprecated: Use SetSCTE35 instead. -func (p *MediaPlaylist) SetSCTE(cue string, id string, time float64) error { +func (p *MediaPlaylist) SetSCTE(cue, id string, time float64) error { return p.SetSCTE35(&SCTE{Syntax: SCTE35_67_2014, Cue: cue, ID: id, Time: time}) } diff --git a/writer_test.go b/writer_test.go index b4281331..e2d59a30 100644 --- a/writer_test.go +++ b/writer_test.go @@ -498,18 +498,17 @@ func TestEncryptionKeysInMediaPlaylist(t *testing.T) { } } -func TestEncryptionKeyMethodNoneInMediaPlaylist(t *testing.T) { +func TestEncryptionKeyMethodNoneInHeaderOfMediaPlaylist(t *testing.T) { p, e := NewMediaPlaylist(5, 5) + p.SetDefaultKey("AES-128", "key-uri", "iv", "identity", "1") if e != nil { t.Fatalf("Create media playlist failed: %s", e) } p.Append("segment-1.ts", 4, "") - p.SetKey("AES-128", "key-uri", "iv", "identity", "1") p.Append("segment-2.ts", 4, "") - p.SetKey("NONE", "", "", "", "") - expected := `#EXT-X-KEY:METHOD=NONE -#EXTINF:4.000, -segment-2.ts` + p.SetDefaultKey("NONE", "key-uri", "iv", "identity", "1") + expected := `#EXT-X-VERSION:5 +#EXT-X-KEY:METHOD=NONE` if !strings.Contains(p.String(), expected) { t.Errorf("Manifest %+v did not contain expected %+v", p, expected) } @@ -817,7 +816,7 @@ func TestNewMasterPlaylistWithClosedCaptionEqNone(t *testing.T) { if err != nil { t.Fatalf("Create media playlist failed: %s", err) } - m.Append(fmt.Sprintf("eng_rendition_rendition.m3u8"), p, *vp) + m.Append("eng_rendition_rendition.m3u8", p, *vp) expected := "CLOSED-CAPTIONS=NONE" if !strings.Contains(m.String(), expected) { @@ -826,7 +825,7 @@ func TestNewMasterPlaylistWithClosedCaptionEqNone(t *testing.T) { // quotes need to be include if not eq NONE vp.Captions = "CC1" m2 := NewMasterPlaylist() - m2.Append(fmt.Sprintf("eng_rendition_rendition.m3u8"), p, *vp) + m2.Append("eng_rendition_rendition.m3u8", p, *vp) expected = `CLOSED-CAPTIONS="CC1"` if !strings.Contains(m2.String(), expected) { t.Fatalf("Master playlist did not contain: %s\nMaster Playlist:\n%v", expected, m2.String()) @@ -971,7 +970,7 @@ func ExampleMediaPlaylist_String() { // Create new media playlist // Add two segments to media playlist // Print it -func ExampleMediaPlaylist_String_Winsize0() { +func ExampleMediaPlaylist_winsize0() { p, _ := NewMediaPlaylist(0, 2) p.Append("test01.ts", 5.0, "") p.Append("test02.ts", 6.0, "") @@ -990,7 +989,7 @@ func ExampleMediaPlaylist_String_Winsize0() { // Create new media playlist // Add two segments to media playlist // Print it -func ExampleMediaPlaylist_String_Winsize0_VOD() { +func ExampleMediaPlaylist_winsize0_vod() { p, _ := NewMediaPlaylist(0, 2) p.Append("test01.ts", 5.0, "") p.Append("test02.ts", 6.0, "")