Skip to content

Commit 3de4fa6

Browse files
authored
Support Parsing Shaka Packager Commands from JSON (#4)
* fix: flag typos and spellings * feat(json-parser): added json structures * feat(json-parser): added flag processors * feat(json-parser): added json packager parser * feat(json-parser): added stream builder options * refactor(json-parser): refacted encryption grouping and added tests
1 parent 1834b72 commit 3de4fa6

12 files changed

+1050
-27
lines changed

examples/json_server/main.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"log"
6+
"net/http"
7+
8+
packlit "github.com/m4urici0gm/packlit/pkg"
9+
)
10+
11+
func main() {
12+
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
13+
if r.Method != http.MethodPost {
14+
w.WriteHeader(http.StatusMethodNotAllowed)
15+
return
16+
}
17+
18+
var req packlit.PackagerOptions
19+
decoder := json.NewDecoder(r.Body)
20+
decoder.DisallowUnknownFields()
21+
if err := decoder.Decode(&req); err != nil {
22+
http.Error(w, err.Error(), http.StatusBadRequest)
23+
return
24+
}
25+
26+
sp, err := packlit.BuildFromJSON(req)
27+
if err != nil {
28+
http.Error(w, err.Error(), http.StatusBadRequest)
29+
return
30+
}
31+
32+
command, err := sp.PreviewCommand()
33+
if err != nil {
34+
http.Error(w, err.Error(), http.StatusInternalServerError)
35+
return
36+
}
37+
38+
w.Header().Set("Content-Type", "application/json")
39+
_ = json.NewEncoder(w).Encode(map[string]interface{}{
40+
"command_preview": command,
41+
})
42+
})
43+
44+
log.Println("Starting server on :8080. POST /")
45+
log.Fatal(http.ListenAndServe(":8080", nil))
46+
}

pkg/crypto_flags.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ func (p CryptByteBlockFlag) Parse() string {
2727
// Apply to video streams with 'cbcs' and 'cens' protection schemes only;
2828
// ignored otherwise.
2929
func (p CryptByteBlockFlag) Validate() error {
30+
if p < 0 {
31+
return fmt.Errorf("crypt_byte_block cannot be negative: %d", p)
32+
}
33+
3034
return nil
3135
}
3236

@@ -57,6 +61,10 @@ func (s SkipByteBlockFlag) Parse() string {
5761
// Apply to video streams with 'cbcs' and 'cens' protection schemes only;
5862
// ignored otherwise.
5963
func (s SkipByteBlockFlag) Validate() error {
64+
if s < 0 {
65+
return fmt.Errorf("skip_byte_block cannot be negative: %d", s)
66+
}
67+
6068
return nil
6169
}
6270

pkg/generic_flags.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package packlit
2+
3+
import "fmt"
4+
5+
// Generic flag types for extensibility
6+
type GenericKeyValueOption struct {
7+
Key string
8+
Value string
9+
}
10+
11+
func (g GenericKeyValueOption) Validate() error { return nil }
12+
func (g GenericKeyValueOption) Parse() string { return fmt.Sprintf("%s=%s", g.Key, g.Value) }
13+
14+
type GenericRawFlag string
15+
16+
func (g GenericRawFlag) Validate() error { return nil }
17+
func (g GenericRawFlag) Parse() string { return string(g) }
18+
19+
type GenericKeyValueFlag struct {
20+
Key string
21+
Value string
22+
}
23+
24+
func (g GenericKeyValueFlag) Validate() error {
25+
if g.Key == "" {
26+
return fmt.Errorf("flag key cannot be empty")
27+
}
28+
return nil
29+
}
30+
31+
func (g GenericKeyValueFlag) Parse() string { return fmt.Sprintf("--%s=%s", g.Key, g.Value) }
32+
33+
type GenericBoolFlag string
34+
35+
func (g GenericBoolFlag) Validate() error {
36+
if string(g) == "" {
37+
return fmt.Errorf("flag name cannot be empty")
38+
}
39+
return nil
40+
}
41+
42+
func (g GenericBoolFlag) Parse() string { return "--" + string(g) }

pkg/hls_flags.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ func (h HLSMediaSequenceNumberFlag) Parse() string {
8888

8989
// Validate checks if the value of HLSMediaSequenceNumber is valid.
9090
func (h HLSMediaSequenceNumberFlag) Validate() error {
91+
if h < 0 {
92+
return fmt.Errorf("hls_media_sequence_number cannot be negative: %d", h)
93+
}
94+
9195
return nil
9296
}
9397

pkg/hls_flags_helpers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ func WithHLSKeyURIFlag(val string) ShakaFlagFn {
2222
}
2323

2424
// WithHLSPlaylistTypeFlag Adds flag '--hls_playlist_type <VOD|EVENT|LIVE>'
25-
func WithHLSPlaylistTypeFlag(val HLSPlaylistTypeFlag) ShakaFlagFn {
25+
func WithHLSPlaylistTypeFlag(val string) ShakaFlagFn {
2626
return func(so *ShakaFlags) {
27-
so.Add(val)
27+
so.Add(HLSPlaylistTypeFlag(val))
2828
}
2929
}
3030

0 commit comments

Comments
 (0)