-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add EXT-X-KEY tag support to playlist parser (#201)
* add EXT-X-KEY tag support to playlist parser * add test cases for EXT-X-KEY * add additional test cases --------- Co-authored-by: aler9 <[email protected]>
- Loading branch information
1 parent
f35e511
commit 030bcdd
Showing
7 changed files
with
292 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package playlist | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/bluenviron/gohlslib/v2/pkg/playlist/primitives" | ||
) | ||
|
||
// MediaKeyMethod is the encryption method used for the media segments. | ||
type MediaKeyMethod string | ||
|
||
// standard encryption methods | ||
const ( | ||
MediaKeyMethodNone = "NONE" | ||
MediaKeyMethodAES128 = "AES-128" | ||
MediaKeyMethodSampleAES = "SAMPLE-AES" | ||
) | ||
|
||
// MediaKey is a EXT-X-KEY tag. | ||
type MediaKey struct { | ||
// METHOD | ||
// required | ||
Method MediaKeyMethod | ||
|
||
// URI is required unless METHOD is NONE | ||
URI string | ||
|
||
// IV | ||
IV string | ||
|
||
// KEYFORMAT | ||
KeyFormat string | ||
|
||
// KEYFORMATVERSIONS | ||
KeyFormatVersions string | ||
} | ||
|
||
func (t *MediaKey) unmarshal(v string) error { | ||
attrs, err := primitives.AttributesUnmarshal(v) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for key, val := range attrs { | ||
switch key { | ||
case "METHOD": | ||
km := MediaKeyMethod(val) | ||
if km != MediaKeyMethodNone && | ||
km != MediaKeyMethodAES128 && | ||
km != MediaKeyMethodSampleAES { | ||
return fmt.Errorf("invalid method: %s", val) | ||
} | ||
t.Method = km | ||
|
||
case "URI": | ||
t.URI = val | ||
|
||
case "IV": | ||
t.IV = val | ||
|
||
case "KEYFORMAT": | ||
t.KeyFormat = val | ||
|
||
case "KEYFORMATVERSIONS": | ||
t.KeyFormatVersions = val | ||
} | ||
} | ||
|
||
switch t.Method { | ||
case MediaKeyMethodAES128, MediaKeyMethodSampleAES: | ||
if t.URI == "" { | ||
return fmt.Errorf("URI is required for method %s", t.Method) | ||
} | ||
default: | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (t MediaKey) marshal() string { | ||
ret := "#EXT-X-KEY:METHOD=" + string(t.Method) | ||
|
||
// If the encryption method is NONE, other attributes MUST NOT be present. | ||
if t.Method != MediaKeyMethodNone { | ||
ret += ",URI=\"" + t.URI + "\"" | ||
|
||
if t.IV != "" { | ||
ret += ",IV=" + t.IV | ||
} | ||
|
||
if t.KeyFormat != "" { | ||
ret += ",KEYFORMAT=\"" + t.KeyFormat + "\"" | ||
} | ||
|
||
if t.KeyFormatVersions != "" { | ||
ret += ",KEYFORMATVERSIONS=\"" + t.KeyFormatVersions + "\"" | ||
} | ||
} | ||
|
||
ret += "\n" | ||
|
||
return ret | ||
} | ||
|
||
// Equal checks if two MediaKey objects are equal. | ||
func (t *MediaKey) Equal(key *MediaKey) bool { | ||
if t == key { | ||
return true | ||
} | ||
|
||
if key == nil { | ||
return false | ||
} | ||
|
||
return t.Method == key.Method && | ||
t.URI == key.URI && | ||
t.IV == key.IV && | ||
t.KeyFormat == key.KeyFormat && | ||
t.KeyFormatVersions == key.KeyFormatVersions | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
pkg/playlist/testdata/fuzz/FuzzMediaUnmarshal/32c9b70f1dcd40a8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
go test fuzz v1 | ||
string("#EXTM3U\n#EXT-X-KEY:0") |
2 changes: 2 additions & 0 deletions
2
pkg/playlist/testdata/fuzz/FuzzMediaUnmarshal/bdfd84d77054b242
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
go test fuzz v1 | ||
string("#EXTM3U\n#EXT-X-KEY:METHOD=AES-128") |
2 changes: 2 additions & 0 deletions
2
pkg/playlist/testdata/fuzz/FuzzMediaUnmarshal/eba654116c9d19f6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
go test fuzz v1 | ||
string("#EXTM3U\n#EXT-X-KEY:METHOD=") |