Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for playlist version check #32

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module github.com/Eyevinn/hls-m3u8

go 1.16
go 1.22

require github.com/matryer/is v1.4.1
28 changes: 28 additions & 0 deletions m3u8/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,7 @@ func decodeLineOfMediaPlaylist(p *MediaPlaylist, state *decodingState, line stri
if state.duration, err = strconv.ParseFloat(duration, 64); strict && err != nil {
return fmt.Errorf("duration parsing error: %w", err)
}
state.versionCheckRule = FloatPointDuration{p.ver, duration}
}
if len(line) > sepIndex {
state.title = line[sepIndex+1:]
Expand Down Expand Up @@ -915,6 +916,7 @@ func decodeLineOfMediaPlaylist(p *MediaPlaylist, state *decodingState, line stri
state.listType = MEDIA
state.xkey = parseKeyParams(line[11:])
state.tagKey = true
state.versionCheckRule = ValidIVInEXTXKey{p.ver, state.xkey.IV}
case strings.HasPrefix(line, "#EXT-X-MAP:"):
state.listType = MEDIA
xMap, err := parseExtXMapParameters(line[11:])
Expand Down Expand Up @@ -947,6 +949,7 @@ func decodeLineOfMediaPlaylist(p *MediaPlaylist, state *decodingState, line stri
return fmt.Errorf("byterange sub-range offset value parsing error: %w ", err)
}
}
state.versionCheckRule = ContainsByteRangeOrIFrameOnly{p.ver}
case !state.tagSCTE35 && strings.HasPrefix(line, "#EXT-SCTE35:"):
state.tagSCTE35 = true
state.listType = MEDIA
Expand Down Expand Up @@ -1021,10 +1024,21 @@ func decodeLineOfMediaPlaylist(p *MediaPlaylist, state *decodingState, line stri
case strings.HasPrefix(line, "#EXT-X-I-FRAMES-ONLY"):
state.listType = MEDIA
p.Iframe = true
state.versionCheckRule = ContainsByteRangeOrIFrameOnly{p.ver}
case strings.HasPrefix(line, "#EXT-X-ALLOW-CACHE:"):
val := strings.TrimPrefix(line, "#EXT-X-ALLOW-CACHE:") == "YES"
p.AllowCache = &val
}

// Check version rules
if valid, mismatch := state.versionCheckRule.Validate(); !valid {
minimumVersion := mismatch.ExpectedVersion
if strict && p.ver < minimumVersion {
return fmt.Errorf("minimum version required: %d", minimumVersion)
}
fmt.Printf("minimum version suggested: %d\n", minimumVersion)
}

return err
}

Expand Down Expand Up @@ -1084,3 +1098,17 @@ func trimLineEnd(line string) string {
}
return line
}

func isStringFloatNum(s string) bool {
if !strings.Contains(s, ".") {
return false
}

_, err := strconv.ParseFloat(s, 64)
return err == nil
}

func isStringInteger(s string) bool {
_, err := strconv.Atoi(s)
return err == nil
}
44 changes: 44 additions & 0 deletions m3u8/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,50 @@ func TestDecodeMediaPlaylistWithDefines(t *testing.T) {
is.Equal(p.Defines[2].Value, "")
}

func TestIsStringFloatNum(t *testing.T) {
tests := []struct {
input string
expected bool
}{
{"123", false},
{"123.456", true},
{"-123.456", true},
{"1e10", false},
{"abc", false},
{"123abc", false},
{"", false},
}

for _, test := range tests {
result := isStringFloatNum(test.input)
if result != test.expected {
t.Errorf("isStringFloatNum(%q) = %v; want %v", test.input, result, test.expected)
}
}
}

func TestIsStringInteger(t *testing.T) {
tests := []struct {
input string
expected bool
}{
{"123", true},
{"-123", true},
{"0", true},
{"123.456", false},
{"abc", false},
{"123abc", false},
{"", false},
}

for _, test := range tests {
result := isStringInteger(test.input)
if result != test.expected {
t.Errorf("isStringInteger(%q) = %v; want %v", test.input, result, test.expected)
}
}
}

/***************************
* Code parsing examples *
***************************/
Expand Down
1 change: 1 addition & 0 deletions m3u8/structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ type decodingState struct {
scte *SCTE
scte35DateRanges []*DateRange
custom CustomMap
versionCheckRule VersionMatchingRule
}

// DateRange corresponds to EXT-X-DATERANGE tag.
Expand Down
74 changes: 74 additions & 0 deletions m3u8/version_check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package m3u8

import "fmt"

type VersionMismatch struct {
ActualVersion uint8
ExpectedVersion uint8
Description string
}

// Error implements error.
func (m VersionMismatch) Error() string {
return fmt.Sprintf("Playlist version mismatch %s. Actual version is %d, expected version is %d", m.Description, m.ActualVersion, m.ExpectedVersion)

Check failure on line 13 in m3u8/version_check.go

View workflow job for this annotation

GitHub Actions / lint

The line is 151 characters long, which exceeds the maximum of 120 characters. (lll)
}

type VersionMatchingRule interface {
Validate() (bool, VersionMismatch)
}

type DefaultMatchingRule struct{}

func (d *DefaultMatchingRule) Validate() (bool, VersionMismatch) {
return true, VersionMismatch{}
}

type ValidIVInEXTXKey struct {
ActualVersion uint8
IV string
}

func (v ValidIVInEXTXKey) Validate() (bool, VersionMismatch) {
if v.IV == "" || v.ActualVersion >= 2 {
return true, VersionMismatch{}
}

return false, VersionMismatch{
ActualVersion: v.ActualVersion,
ExpectedVersion: 2,
Description: "Protocol version needs to be at least 2 if you have IV in EXT-X-KEY.",
}
}

type FloatPointDuration struct {
ActualVersion uint8
duration string
}

func (f FloatPointDuration) Validate() (bool, VersionMismatch) {
if isStringInteger(f.duration) || f.ActualVersion >= 3 {
return true, VersionMismatch{}
}

return false, VersionMismatch{
ActualVersion: f.ActualVersion,
ExpectedVersion: 3,
Description: "Protocol version needs to be at least 3 if you have floating point duration.",
}
}

type ContainsByteRangeOrIFrameOnly struct {
ActualVersion uint8
}

func (c ContainsByteRangeOrIFrameOnly) Validate() (bool, VersionMismatch) {
if c.ActualVersion >= 4 {
return true, VersionMismatch{}
}

return false, VersionMismatch{
ActualVersion: c.ActualVersion,
ExpectedVersion: 4,
Description: "Protocol version needs to be at least 4 if you have EXT-X-BYTERANGE or EXT-X-I-FRAMES-ONLY tag",

Check failure on line 72 in m3u8/version_check.go

View workflow job for this annotation

GitHub Actions / lint

The line is 122 characters long, which exceeds the maximum of 120 characters. (lll)
}
}
119 changes: 119 additions & 0 deletions m3u8/version_check_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package m3u8

import (
"testing"
)

func TestDefaultMatchingRule_Validate(t *testing.T) {
rule := DefaultMatchingRule{}
valid, err := rule.Validate()
if !valid {
t.Errorf("expected true, got false")
}
if err != (VersionMismatch{}) {
t.Errorf("expected empty error, got %v", err)
}
}

func TestValidIVInEXTXKEY(t *testing.T) {
tests := []struct {
name string
actualVersion uint8
iv string
expectedValid bool
expectedError VersionMismatch
}{
{
name: "Valid case with empty IV",
actualVersion: 1,
iv: "",
expectedValid: true,
expectedError: VersionMismatch{},
},
{
name: "Invalid case with non-empty IV",
actualVersion: 1,
iv: "someIV",
expectedValid: false,
expectedError: VersionMismatch{
ActualVersion: 1,
ExpectedVersion: 2,
Description: "Protocol version needs to be at least 2 if you have IV in EXT-X-KEY.",
},
},
{
name: "Valid case with non-empty IV",
actualVersion: 2,
iv: "someIV",
expectedValid: true,
expectedError: VersionMismatch{},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rule := ValidIVInEXTXKey{
ActualVersion: tt.actualVersion,
IV: tt.iv,
}
valid, err := rule.Validate()
if valid != tt.expectedValid {
t.Errorf("expected %v, got %v", tt.expectedValid, valid)
}
if err != tt.expectedError {
t.Errorf("expected %v, got %v", tt.expectedError, err)
}
})
}
}
func TestFloatPointDuration(t *testing.T) {
tests := []struct {
name string
actualVersion uint8
duration string
expectedValid bool
expectedError VersionMismatch
}{
{
name: "Valid case with integer duration",
actualVersion: 1,
duration: "10",
expectedValid: true,
expectedError: VersionMismatch{},
},
{
name: "Invalid case with floating point duration and version less than 3",
actualVersion: 2,
duration: "10.5",
expectedValid: false,
expectedError: VersionMismatch{
ActualVersion: 2,
ExpectedVersion: 3,
Description: "Protocol version needs to be at least 3 if you have floating point duration.",
},
},
{
name: "Valid case with floating point duration and version 3 or higher",
actualVersion: 3,
duration: "10.5",
expectedValid: true,
expectedError: VersionMismatch{},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rule := FloatPointDuration{
ActualVersion: tt.actualVersion,
duration: tt.duration,
}
valid, err := rule.Validate()
if valid != tt.expectedValid {
t.Errorf("expected %v, got %v", tt.expectedValid, valid)
}
if err != tt.expectedError {
t.Errorf("expected %v, got %v", tt.expectedError, err)
}
})
}
}
Loading