Skip to content

Commit e2f07c0

Browse files
authored
Merge pull request #2 from paulgrammer/tags
implemented code for http_file flags
2 parents 16a2f2a + 85e76f7 commit e2f07c0

File tree

4 files changed

+232
-2
lines changed

4 files changed

+232
-2
lines changed

examples/http_upload/main.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Refer to the Shaka Packager HTTP upload tutorial for more details:
2+
// https://shaka-project.github.io/shaka-packager/html/tutorials/http_upload.html
3+
// This implementation follows the HTTP upload process, ensuring media segments
4+
// and MPD files are uploaded correctly to the specified server.
5+
package main
6+
7+
import (
8+
"fmt"
9+
"log"
10+
"path"
11+
12+
packlit "github.com/m4urici0gm/packlit/pkg"
13+
)
14+
15+
func buildFlags(uploadURL string) *packlit.ShakaFlags {
16+
return packlit.NewFlags(
17+
packlit.WithUserAgentFlag("Packlit-client/1.0"),
18+
packlit.WithCaFileFlag("/etc/ssl/certs/ca-certificates.crt"),
19+
packlit.WithClientCertFileFlag("/etc/ssl/certs/client-cert.pem"),
20+
packlit.WithClientCertPrivateKeyFileFlag("/etc/ssl/private/client-key.pem"),
21+
packlit.WithClientCertPrivateKeyPasswordFlag("supersecurepassword"),
22+
packlit.WithDisablePeerVerificationFlag(),
23+
packlit.WithIgnoreHttpOutputFailuresFlag(),
24+
packlit.WithStaticLiveMpd(),
25+
packlit.WithSegmentDuration("4"),
26+
packlit.WithMpdOutput(path.Join(uploadURL, "h264.mpd")),
27+
)
28+
}
29+
30+
type stream struct {
31+
streamType packlit.StreamType
32+
input string
33+
}
34+
35+
func main() {
36+
uploadURL := "http://localhost:6767/vod"
37+
38+
streams := []stream{
39+
{streamType: "audio", input: "audio.mp4"},
40+
{streamType: "video", input: "video.mp4"},
41+
}
42+
43+
builder := packlit.NewBuilder()
44+
flags := buildFlags(uploadURL)
45+
46+
for _, stream := range streams {
47+
initSegment := path.Join(uploadURL, fmt.Sprintf("%v-init.mp4", stream.streamType))
48+
segmentTemplate := path.Join(uploadURL, fmt.Sprintf("%v-$Time$.m4s", stream.streamType))
49+
50+
builder.WithStream(
51+
packlit.NewStreamBuilder().
52+
WithInput(stream.input).
53+
WithStream(stream.streamType).
54+
AddOption(packlit.WithInitSegment(initSegment)).
55+
AddOption(packlit.WithSegmentTemplate(segmentTemplate)).
56+
Build(),
57+
)
58+
}
59+
60+
packager := builder.WithFlag(flags).Build()
61+
62+
cmd, err := packager.PreviewCommand()
63+
if err != nil {
64+
log.Fatalf("error: when previewing command %v", err)
65+
}
66+
67+
// Print command
68+
log.Println(cmd)
69+
70+
packagerExecutor := packlit.NewExecutor(packager)
71+
if err := packagerExecutor.Run(); err != nil {
72+
log.Fatalf("error when running shaka-packager: %v", err)
73+
}
74+
}

pkg/flags.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ var (
1515
_ = (*GenerateStaticLiveMpd)(nil)
1616
_ = (*DumpStreamInfo)(nil)
1717
_ = (*BaseUrls)(nil)
18+
_ = (*UserAgentFlag)(nil)
19+
_ = (*CaFileFlag)(nil)
20+
_ = (*ClientCertFileFlag)(nil)
21+
_ = (*ClientCertPrivateKeyFileFlag)(nil)
22+
_ = (*ClientCertPrivateKeyPasswordFlag)(nil)
23+
_ = (*DisablePeerVerificationFlag)(nil)
24+
_ = (*IgnoreHttpOutputFailuresFlag)(nil)
25+
_ = (*IoCacheSizeFlag)(nil)
1826
)
1927

2028
func buildFlags(flags *ShakaFlags) ([]string, error) {
@@ -85,9 +93,9 @@ func (g DumpStreamInfo) Validate() error {
8593
type BaseUrls []string
8694

8795
func (b BaseUrls) Parse() string {
88-
return fmt.Sprintf("--base_urls=%s", strings.Join(b, ","))
96+
return fmt.Sprintf("--base_urls=%s", strings.Join(b, ","))
8997
}
9098

9199
func (b BaseUrls) Validate() error {
92-
return nil
100+
return nil
93101
}

pkg/http_file_flags.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package packlit
2+
3+
import "fmt"
4+
5+
// UserAgentFlag represents the flag for setting a custom User-Agent string for HTTP requests.
6+
type UserAgentFlag string
7+
8+
func (u UserAgentFlag) Parse() string {
9+
return fmt.Sprintf("--user_agent=%s", u)
10+
}
11+
12+
func (u UserAgentFlag) Validate() error {
13+
return nil
14+
}
15+
16+
// CaFileFlag represents the flag for setting the Certificate Authority file for the server cert.
17+
type CaFileFlag string
18+
19+
func (c CaFileFlag) Parse() string {
20+
return fmt.Sprintf("--ca_file=%s", c)
21+
}
22+
23+
func (c CaFileFlag) Validate() error {
24+
return nil
25+
}
26+
27+
// ClientCertFileFlag represents the flag for setting the client certificate file path.
28+
type ClientCertFileFlag string
29+
30+
func (c ClientCertFileFlag) Parse() string {
31+
return fmt.Sprintf("--client_cert_file=%s", c)
32+
}
33+
34+
func (c ClientCertFileFlag) Validate() error {
35+
return nil
36+
}
37+
38+
// ClientCertPrivateKeyFileFlag represents the flag for setting the Private Key file path for the client cert.
39+
type ClientCertPrivateKeyFileFlag string
40+
41+
func (c ClientCertPrivateKeyFileFlag) Parse() string {
42+
return fmt.Sprintf("--client_cert_private_key_file=%s", c)
43+
}
44+
45+
func (c ClientCertPrivateKeyFileFlag) Validate() error {
46+
return nil
47+
}
48+
49+
// ClientCertPrivateKeyPasswordFlag represents the flag for setting the password to the private key file.
50+
type ClientCertPrivateKeyPasswordFlag string
51+
52+
func (c ClientCertPrivateKeyPasswordFlag) Parse() string {
53+
return fmt.Sprintf("--client_cert_private_key_password=%s", c)
54+
}
55+
56+
func (c ClientCertPrivateKeyPasswordFlag) Validate() error {
57+
return nil
58+
}
59+
60+
// DisablePeerVerificationFlag represents the flag for disabling peer verification for HTTP connections.
61+
type DisablePeerVerificationFlag struct{}
62+
63+
func (d DisablePeerVerificationFlag) Parse() string {
64+
return "--disable_peer_verification"
65+
}
66+
67+
func (d DisablePeerVerificationFlag) Validate() error {
68+
return nil
69+
}
70+
71+
// IgnoreHttpOutputFailuresFlag represents the flag for ignoring HTTP output failures.
72+
type IgnoreHttpOutputFailuresFlag struct{}
73+
74+
func (i IgnoreHttpOutputFailuresFlag) Parse() string {
75+
return "--ignore_http_output_failures"
76+
}
77+
78+
func (i IgnoreHttpOutputFailuresFlag) Validate() error {
79+
return nil
80+
}
81+
82+
// IoCacheSizeFlag represents the flag for setting the IO cache size.
83+
type IoCacheSizeFlag uint
84+
85+
func (i IoCacheSizeFlag) Parse() string {
86+
return fmt.Sprintf("--io_cache_size=%d", i)
87+
}
88+
89+
func (i IoCacheSizeFlag) Validate() error {
90+
return nil
91+
}

pkg/http_file_flags_helpers.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package packlit
2+
3+
// WithUserAgentFlag Adds flag '--user_agent <value>'
4+
func WithUserAgentFlag(val string) ShakaFlagFn {
5+
return func(so *ShakaFlags) {
6+
so.Add(UserAgentFlag(val))
7+
}
8+
}
9+
10+
// WithCaFileFlag Adds flag '--ca_file <value>'
11+
func WithCaFileFlag(val string) ShakaFlagFn {
12+
return func(so *ShakaFlags) {
13+
so.Add(CaFileFlag(val))
14+
}
15+
}
16+
17+
// WithClientCertFileFlag Adds flag '--client_cert_file <value>'
18+
func WithClientCertFileFlag(val string) ShakaFlagFn {
19+
return func(so *ShakaFlags) {
20+
so.Add(ClientCertFileFlag(val))
21+
}
22+
}
23+
24+
// WithClientCertPrivateKeyFileFlag Adds flag '--client_cert_private_key_file <value>'
25+
func WithClientCertPrivateKeyFileFlag(val string) ShakaFlagFn {
26+
return func(so *ShakaFlags) {
27+
so.Add(ClientCertPrivateKeyFileFlag(val))
28+
}
29+
}
30+
31+
// WithClientCertPrivateKeyPasswordFlag Adds flag '--client_cert_private_key_password <value>'
32+
func WithClientCertPrivateKeyPasswordFlag(val string) ShakaFlagFn {
33+
return func(so *ShakaFlags) {
34+
so.Add(ClientCertPrivateKeyPasswordFlag(val))
35+
}
36+
}
37+
38+
// WithDisablePeerVerificationFlag Adds flag '--disable_peer_verification'
39+
func WithDisablePeerVerificationFlag() ShakaFlagFn {
40+
return func(so *ShakaFlags) {
41+
so.Add(DisablePeerVerificationFlag{})
42+
}
43+
}
44+
45+
// WithIgnoreHttpOutputFailuresFlag Adds flag '--ignore_http_output_failures'
46+
func WithIgnoreHttpOutputFailuresFlag() ShakaFlagFn {
47+
return func(so *ShakaFlags) {
48+
so.Add(IgnoreHttpOutputFailuresFlag{})
49+
}
50+
}
51+
52+
// WithIoCacheSizeFlag Adds flag '--io_cache_size <value>'
53+
func WithIoCacheSizeFlag(val uint) ShakaFlagFn {
54+
return func(so *ShakaFlags) {
55+
so.Add(IoCacheSizeFlag(val))
56+
}
57+
}

0 commit comments

Comments
 (0)