FEAT: Return Playback URL in Get Videos
This commit is contained in:
+52
-9
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -24,11 +25,35 @@ import (
|
||||
// client does still goes over plain HTTP.
|
||||
|
||||
const (
|
||||
defaultEndpointURL = "http://localhost.localstack.cloud:4566"
|
||||
defaultTopicARN = "arn:aws:sns:us-east-1:000000000000:mediaconvert-job-events"
|
||||
defaultRegion = "us-east-1"
|
||||
defaultEndpointURL = "http://localhost.localstack.cloud:4566"
|
||||
defaultTopicARN = "arn:aws:sns:us-east-1:000000000000:mediaconvert-job-events"
|
||||
defaultRegion = "us-east-1"
|
||||
defaultOutputBucket = "encoded-bucket"
|
||||
defaultPlaybackBaseURL = "http://localhost.localstack.cloud:4566/encoded-bucket"
|
||||
)
|
||||
|
||||
// playbackBaseURL is the public host the encoded output is served from — the
|
||||
// CloudFront distribution in AWS, LocalStack's own S3 endpoint here. It has to
|
||||
// match what cms is configured with, since the whole point of the rewrite is
|
||||
// that the two agree.
|
||||
func playbackBaseURL() string {
|
||||
return strings.TrimRight(envOr("PLAYBACK_BASE_URL", defaultPlaybackBaseURL), "/")
|
||||
}
|
||||
|
||||
// playlistPathFor is where the HLS output group writes the master playlist for
|
||||
// a given source key: one folder per video, named after the source object.
|
||||
// QueueEncodingJob derives the job's destination the same way.
|
||||
func playlistPathFor(storageKey string) string {
|
||||
base := strings.TrimSuffix(storageKey, path.Ext(storageKey))
|
||||
return fmt.Sprintf("s3://%s/%s/index.m3u8", envOr("MEDIACONVERT_OUTPUT_BUCKET", defaultOutputBucket), base)
|
||||
}
|
||||
|
||||
// playbackURLFor is what cms should end up storing for that same key.
|
||||
func playbackURLFor(storageKey string) string {
|
||||
base := strings.TrimSuffix(storageKey, path.Ext(storageKey))
|
||||
return fmt.Sprintf("%s/%s/index.m3u8", playbackBaseURL(), base)
|
||||
}
|
||||
|
||||
func envOr(name, fallback string) string {
|
||||
if v := strings.TrimSpace(os.Getenv(name)); v != "" {
|
||||
return v
|
||||
@@ -51,11 +76,19 @@ type jobStateChange struct {
|
||||
}
|
||||
|
||||
type jobStateDetail struct {
|
||||
Timestamp int64 `json:"timestamp"`
|
||||
AccountID string `json:"accountId"`
|
||||
Queue string `json:"queue"`
|
||||
JobID string `json:"jobId"`
|
||||
Status string `json:"status"`
|
||||
Timestamp int64 `json:"timestamp"`
|
||||
AccountID string `json:"accountId"`
|
||||
Queue string `json:"queue"`
|
||||
JobID string `json:"jobId"`
|
||||
Status string `json:"status"`
|
||||
OutputGroupDetails []outputGroup `json:"outputGroupDetails,omitempty"`
|
||||
}
|
||||
|
||||
// outputGroup is one output group's result. An HLS group reports the manifests
|
||||
// it wrote under playlistFilePaths; the segments themselves are not listed.
|
||||
type outputGroup struct {
|
||||
Type string `json:"type"`
|
||||
PlaylistFilePaths []string `json:"playlistFilePaths,omitempty"`
|
||||
}
|
||||
|
||||
// eventPublisher puts job state changes on the topic cms's queue subscribes to.
|
||||
@@ -85,7 +118,10 @@ func newEventPublisher(ctx context.Context) (*eventPublisher, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (p *eventPublisher) publishJobState(ctx context.Context, jobID, state string) error {
|
||||
// publishJobState sends the event MediaConvert would send. playlistPath is the
|
||||
// master playlist an HLS output group reports on COMPLETE; pass "" to send a
|
||||
// COMPLETE that names no playlist, which is what a job with no HLS group does.
|
||||
func (p *eventPublisher) publishJobState(ctx context.Context, jobID, state, playlistPath string) error {
|
||||
event := jobStateChange{
|
||||
Version: "0",
|
||||
ID: fmt.Sprintf("test-%d", time.Now().UnixNano()),
|
||||
@@ -103,6 +139,13 @@ func (p *eventPublisher) publishJobState(ctx context.Context, jobID, state strin
|
||||
},
|
||||
}
|
||||
|
||||
if playlistPath != "" {
|
||||
event.Detail.OutputGroupDetails = []outputGroup{{
|
||||
Type: "HLS_GROUP",
|
||||
PlaylistFilePaths: []string{playlistPath},
|
||||
}}
|
||||
}
|
||||
|
||||
body, err := json.Marshal(event)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user