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

Pass segment duration to transcoder instead of overall duration #105

Merged
merged 1 commit into from
Oct 31, 2022
Merged
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 handlers/misttriggers/recording_end.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (d *MistCallbackHandlersCollection) triggerRecordingEndSegmenting(w http.Re
}

go func() {
err := transcode.RunTranscodeProcess(transcodeRequest, p.StreamName, p.StreamMediaDurationMillis)
err := transcode.RunTranscodeProcess(transcodeRequest, p.StreamName)
if err != nil {
_ = config.Logger.Log(
"msg", "RunTranscodeProcess returned an error",
Expand Down
2 changes: 1 addition & 1 deletion handlers/transcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (d *CatalystAPIHandlersCollection) TranscodeSegment() httprouter.Handle {

// TODO: Do this asynchronously and pass valid stream-name and input file duration
// when the transcode api endpoint is accessed (only used for testing for now)
err = transcode.RunTranscodeProcess(transcodeRequest, "", 0)
err = transcode.RunTranscodeProcess(transcodeRequest, "")
if err != nil {
errors.WriteHTTPInternalServerError(w, "Error running Transcode process", err)
}
Expand Down
17 changes: 14 additions & 3 deletions transcode/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,14 @@ func DownloadRenditionManifest(sourceManifestOSURL string) (m3u8.MediaPlaylist,
return *mediaPlaylist, nil
}

type SourceSegment struct {
URL string
DurationMillis int64
}

// Loop over each segment and convert it from a relative to a full ObjectStore-compatible URL
func GetSourceSegmentURLs(sourceManifestURL string, manifest m3u8.MediaPlaylist) ([]string, error) {
var urls []string
func GetSourceSegmentURLs(sourceManifestURL string, manifest m3u8.MediaPlaylist) ([]SourceSegment, error) {
var urls []SourceSegment
for _, segment := range manifest.Segments {
// The segments list is a ring buffer - see https://github.com/grafov/m3u8/issues/140
// and so we only know we've hit the end of the list when we find a nil element
Expand All @@ -50,7 +55,13 @@ func GetSourceSegmentURLs(sourceManifestURL string, manifest m3u8.MediaPlaylist)
if err != nil {
return nil, err
}
urls = append(urls, u)
urls = append(
urls,
SourceSegment{
URL: u,
DurationMillis: int64(segment.Duration * 1000),
},
)
}

return urls, nil
Expand Down
17 changes: 17 additions & 0 deletions transcode/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,23 @@ func TestItCanConvertRelativeURLsToOSURLs(t *testing.T) {
require.Equal(t, "s3+https://REDACTED:[email protected]/something/001.ts", u)
}

func TestItParsesManifestAndConvertsRelativeURLs(t *testing.T) {
sourceManifest, _, err := m3u8.DecodeFrom(strings.NewReader(validMediaManifest), true)
require.NoError(t, err)

sourceMediaManifest, ok := sourceManifest.(*m3u8.MediaPlaylist)
require.True(t, ok)

us, err := GetSourceSegmentURLs("s3+https://REDACTED:[email protected]/something/output.m3u8", *sourceMediaManifest)
require.NoError(t, err)

require.Equal(t, 2, len(us))
require.Equal(t, "s3+https://REDACTED:[email protected]/something/0.ts", us[0].URL)
require.Equal(t, int64(10416), us[0].DurationMillis)
require.Equal(t, "s3+https://REDACTED:[email protected]/something/5000.ts", us[1].URL)
require.Equal(t, int64(5334), us[1].DurationMillis)
}

func TestItCanGenerateAndWriteManifests(t *testing.T) {
// Set up the parameters we pass in
sourceManifest, _, err := m3u8.DecodeFrom(strings.NewReader(validMediaManifest), true)
Expand Down
8 changes: 4 additions & 4 deletions transcode/transcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func init() {
localBroadcasterClient = b
}

func RunTranscodeProcess(transcodeRequest TranscodeSegmentRequest, streamName string, durationMillis int64) error {
func RunTranscodeProcess(transcodeRequest TranscodeSegmentRequest, streamName string) error {
_ = config.Logger.Log("msg", "RunTranscodeProcess (v2) Beginning", "source", transcodeRequest.SourceFile, "target", transcodeRequest.UploadURL)

// Create a separate subdirectory for the transcoded renditions
Expand Down Expand Up @@ -94,7 +94,7 @@ func RunTranscodeProcess(transcodeRequest TranscodeSegmentRequest, streamName st
// Iterate through the segment URLs and transcode them
// TODO: Some level of parallelisation once we're happy this works well
for segmentIndex, u := range sourceSegmentURLs {
rc, err := clients.DownloadOSURL(u)
rc, err := clients.DownloadOSURL(u.URL)
if err != nil {
return fmt.Errorf("failed to download source segment %q: %s", u, err)
}
Expand All @@ -108,14 +108,14 @@ func RunTranscodeProcess(transcodeRequest TranscodeSegmentRequest, streamName st
}
broadcasterClient, _ := clients.NewRemoteBroadcasterClient(creds)

tr, err := broadcasterClient.TranscodeSegmentWithRemoteBroadcaster(rc, int64(segmentIndex), transcodeProfiles, streamName, durationMillis)
tr, err := broadcasterClient.TranscodeSegmentWithRemoteBroadcaster(rc, int64(segmentIndex), transcodeProfiles, streamName, u.DurationMillis)
if err != nil {
return fmt.Errorf("failed to run TranscodeSegmentWithRemoteBroadcaster: %s", err)
}
fmt.Println("transcodeResult", tr) //remove this
// TODO: Upload the output segments
} else {
tr, err := localBroadcasterClient.TranscodeSegment(rc, int64(segmentIndex), transcodeProfiles, durationMillis)
tr, err := localBroadcasterClient.TranscodeSegment(rc, int64(segmentIndex), transcodeProfiles, u.DurationMillis)
if err != nil {
return fmt.Errorf("failed to run TranscodeSegment: %s", err)
}
Expand Down
1 change: 0 additions & 1 deletion transcode/transcode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ func TestItCanTranscode(t *testing.T) {
UploadURL: manifestFile.Name(),
},
"streamName",
123,
)
require.NoError(t, err)

Expand Down