Skip to content

Commit

Permalink
Add missing duration target track metadata when adding track to muxer (
Browse files Browse the repository at this point in the history
  • Loading branch information
izzytwosheds authored Jan 23, 2023
1 parent bfa4512 commit c08fcbb
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,8 @@ private int writeEncodedOutputFrame() throws TrackTranscoderException {
// TODO for now, we assume that we only get one media format as a first buffer
MediaFormat outputMediaFormat = encoder.getOutputFormat();
if (!targetTrackAdded) {
targetFormat = outputMediaFormat;
targetTrack = mediaMuxer.addTrack(outputMediaFormat, targetTrack);
targetFormat = addMissingMetadata(sourceAudioFormat, outputMediaFormat);
targetTrack = mediaMuxer.addTrack(targetFormat, targetTrack);
targetTrackAdded = true;
renderer.onMediaFormatChanged(sourceAudioFormat, targetFormat);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

import android.media.MediaCodec;
import android.media.MediaFormat;
import android.os.Build;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
Expand Down Expand Up @@ -131,4 +133,17 @@ protected int advanceToNextTrack() {
return RESULT_END_OF_RANGE_REACHED;
}

protected MediaFormat addMissingMetadata(@NonNull MediaFormat sourceMediaFormat, @NonNull MediaFormat targetMediaFormat) {
if (!targetMediaFormat.containsKey(MediaFormat.KEY_DURATION)
&& sourceMediaFormat.containsKey(MediaFormat.KEY_DURATION)) {
targetMediaFormat.setLong(MediaFormat.KEY_DURATION, sourceMediaFormat.getLong(MediaFormat.KEY_DURATION));
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
&& !targetMediaFormat.containsKey(MediaFormat.KEY_LANGUAGE)
&& sourceMediaFormat.containsKey(MediaFormat.KEY_LANGUAGE)) {
targetMediaFormat.setString(MediaFormat.KEY_LANGUAGE, sourceMediaFormat.getString(MediaFormat.KEY_LANGUAGE));
}
return targetMediaFormat;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,8 @@ private int writeEncodedOutputFrame() throws TrackTranscoderException {
// TODO for now, we assume that we only get one media format as a first buffer
MediaFormat outputMediaFormat = encoder.getOutputFormat();
if (!targetTrackAdded) {
targetVideoFormat = targetFormat = outputMediaFormat;
targetTrack = mediaMuxer.addTrack(outputMediaFormat, targetTrack);
targetVideoFormat = targetFormat = addMissingMetadata(sourceVideoFormat, outputMediaFormat);
targetTrack = mediaMuxer.addTrack(targetFormat, targetTrack);
targetTrackAdded = true;
renderer.onMediaFormatChanged(sourceVideoFormat, targetVideoFormat);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ public class AudioTrackTranscoderShould {
public void setup() throws Exception {
MockitoAnnotations.initMocks(this);

when(sourceMediaFormat.containsKey(MediaFormat.KEY_DURATION)).thenReturn(true);
when(sourceMediaFormat.getLong(MediaFormat.KEY_DURATION)).thenReturn(DURATION);

sampleFrame = new Frame(BUFFER_INDEX, ByteBuffer.allocate(BUFFER_SIZE), bufferInfo);
fullMediaRange = new MediaRange(0, Long.MAX_VALUE);
trimmedMediaRange = new MediaRange(SELECTION_START, SELECTION_END);
Expand Down Expand Up @@ -414,7 +417,9 @@ public void addTrackWhenEncoderMediaFormatReceived() throws Exception {
audioTrackTranscoder.lastDecodeFrameResult = TrackTranscoder.RESULT_EOS_REACHED;
audioTrackTranscoder.lastEncodeFrameResult = TrackTranscoder.RESULT_FRAME_PROCESSED;

MediaFormat encoderMediaFormat = new MediaFormat();
MediaFormat encoderMediaFormat = mock(MediaFormat.class);
when(encoderMediaFormat.containsKey(MediaFormat.KEY_DURATION)).thenReturn(false);

doReturn(MediaCodec.INFO_OUTPUT_FORMAT_CHANGED).when(encoder).dequeueOutputFrame(anyLong());
doReturn(encoderMediaFormat).when(encoder).getOutputFormat();
doReturn(AUDIO_TRACK).when(mediaTarget).addTrack(any(MediaFormat.class), anyInt());
Expand All @@ -424,6 +429,7 @@ public void addTrackWhenEncoderMediaFormatReceived() throws Exception {
ArgumentCaptor<MediaFormat> mediaFormatArgumentCaptor = ArgumentCaptor.forClass(MediaFormat.class);
verify(mediaTarget).addTrack(mediaFormatArgumentCaptor.capture(), eq(AUDIO_TRACK));
assertThat(mediaFormatArgumentCaptor.getValue(), is(encoderMediaFormat));
verify(encoderMediaFormat).setLong(MediaFormat.KEY_DURATION, DURATION);

assertThat(audioTrackTranscoder.targetTrack, is(AUDIO_TRACK));
assertThat(result, is(TrackTranscoder.RESULT_OUTPUT_MEDIA_FORMAT_CHANGED));
Expand Down Expand Up @@ -506,8 +512,6 @@ public void failWhenSelectionEndIsBeforeStart() throws Exception {

@Test
public void adjustDurationToMediaSelection() throws Exception {
when(sourceMediaFormat.containsKey(MediaFormat.KEY_DURATION)).thenReturn(true);
when(sourceMediaFormat.getLong(MediaFormat.KEY_DURATION)).thenReturn(DURATION);
when(mediaSource.getSelection()).thenReturn(trimmedMediaRange);

AudioTrackTranscoder audioTrackTranscoder = new AudioTrackTranscoder(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ public void setup() throws Exception {
fullMediaRange = new MediaRange(0, Long.MAX_VALUE);
trimmedMediaRange = new MediaRange(SELECTION_START, SELECTION_END);

when(sourceMediaFormat.containsKey(MediaFormat.KEY_DURATION)).thenReturn(true);
when(sourceMediaFormat.getLong(MediaFormat.KEY_DURATION)).thenReturn(DURATION);

doReturn(sourceMediaFormat).when(mediaSource).getTrackFormat(anyInt());
doReturn(surface).when(encoder).createInputSurface();
doReturn(surface).when(renderer).getInputSurface();
Expand Down Expand Up @@ -428,7 +431,9 @@ public void addTrackWhenEncoderMediaFormatReceived() throws Exception {
videoTrackTranscoder.lastDecodeFrameResult = TrackTranscoder.RESULT_EOS_REACHED;
videoTrackTranscoder.lastEncodeFrameResult = TrackTranscoder.RESULT_FRAME_PROCESSED;

MediaFormat encoderMediaFormat = new MediaFormat();
MediaFormat encoderMediaFormat = mock(MediaFormat.class);
when(encoderMediaFormat.containsKey(MediaFormat.KEY_DURATION)).thenReturn(false);

doReturn(MediaCodec.INFO_OUTPUT_FORMAT_CHANGED).when(encoder).dequeueOutputFrame(anyLong());
doReturn(encoderMediaFormat).when(encoder).getOutputFormat();
doReturn(VIDEO_TRACK).when(mediaTarget).addTrack(any(MediaFormat.class), anyInt());
Expand All @@ -438,6 +443,7 @@ public void addTrackWhenEncoderMediaFormatReceived() throws Exception {
ArgumentCaptor<MediaFormat> mediaFormatArgumentCaptor = ArgumentCaptor.forClass(MediaFormat.class);
verify(mediaTarget).addTrack(mediaFormatArgumentCaptor.capture(), eq(VIDEO_TRACK));
assertThat(mediaFormatArgumentCaptor.getValue(), is(encoderMediaFormat));
verify(encoderMediaFormat).setLong(MediaFormat.KEY_DURATION, DURATION);

assertThat(videoTrackTranscoder.targetTrack, is(VIDEO_TRACK));
assertThat(result, is(TrackTranscoder.RESULT_OUTPUT_MEDIA_FORMAT_CHANGED));
Expand Down Expand Up @@ -521,8 +527,6 @@ public void failWhenSelectionEndIsBeforeStart() throws Exception {

@Test
public void adjustDurationToMediaSelection() throws Exception {
when(sourceMediaFormat.containsKey(MediaFormat.KEY_DURATION)).thenReturn(true);
when(sourceMediaFormat.getLong(MediaFormat.KEY_DURATION)).thenReturn(DURATION);
when(mediaSource.getSelection()).thenReturn(trimmedMediaRange);

VideoTrackTranscoder videoTrackTranscoder = new VideoTrackTranscoder(
Expand Down

0 comments on commit c08fcbb

Please sign in to comment.