Skip to content

Commit

Permalink
Remove usages of deprecated startTransformation
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 503138745
  • Loading branch information
kim-vde authored and christosts committed Jan 23, 2023
1 parent f15b752 commit 2cb4e3d
Show file tree
Hide file tree
Showing 8 changed files with 315 additions and 203 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@
import androidx.media3.exoplayer.util.DebugTextViewHelper;
import androidx.media3.transformer.DefaultEncoderFactory;
import androidx.media3.transformer.DefaultMuxer;
import androidx.media3.transformer.EditedMediaItem;
import androidx.media3.transformer.Effects;
import androidx.media3.transformer.ProgressHolder;
import androidx.media3.transformer.TransformationException;
import androidx.media3.transformer.TransformationRequest;
Expand Down Expand Up @@ -207,8 +209,9 @@ private void startTransformation() {
MediaItem mediaItem = createMediaItem(bundle, uri);
try {
Transformer transformer = createTransformer(bundle, filePath);
EditedMediaItem editedMediaItem = createEditedMediaItem(mediaItem, bundle);
transformationStopwatch.start();
transformer.startTransformation(mediaItem, filePath);
transformer.startTransformation(editedMediaItem, filePath);
this.transformer = transformer;
} catch (PackageManager.NameNotFoundException e) {
throw new IllegalStateException(e);
Expand Down Expand Up @@ -265,8 +268,7 @@ private MediaItem createMediaItem(@Nullable Bundle bundle, Uri uri) {
"progressViewGroup",
"debugFrame",
})
private Transformer createTransformer(@Nullable Bundle bundle, String filePath)
throws PackageManager.NameNotFoundException {
private Transformer createTransformer(@Nullable Bundle bundle, String filePath) {
Transformer.Builder transformerBuilder = new Transformer.Builder(/* context= */ this);
if (bundle != null) {
TransformationRequest.Builder requestBuilder = new TransformationRequest.Builder();
Expand All @@ -283,29 +285,6 @@ private Transformer createTransformer(@Nullable Bundle bundle, String filePath)
requestBuilder.setHdrMode(bundle.getInt(ConfigurationActivity.HDR_MODE));
transformerBuilder.setTransformationRequest(requestBuilder.build());

transformerBuilder.setAudioProcessors(createAudioProcessorsFromBundle(bundle));

ImmutableList.Builder<Effect> effectsListBuilder =
new ImmutableList.Builder<Effect>().addAll(createVideoEffectsFromBundle(bundle));
float scaleX = bundle.getFloat(ConfigurationActivity.SCALE_X, /* defaultValue= */ 1);
float scaleY = bundle.getFloat(ConfigurationActivity.SCALE_Y, /* defaultValue= */ 1);
float rotateDegrees =
bundle.getFloat(ConfigurationActivity.ROTATE_DEGREES, /* defaultValue= */ 0);
if (scaleX != 1f || scaleY != 1f || rotateDegrees != 0f) {
effectsListBuilder.add(
new ScaleToFitTransformation.Builder()
.setScale(scaleX, scaleY)
.setRotationDegrees(rotateDegrees)
.build());
}
int resolutionHeight =
bundle.getInt(
ConfigurationActivity.RESOLUTION_HEIGHT, /* defaultValue= */ C.LENGTH_UNSET);
if (resolutionHeight != C.LENGTH_UNSET) {
effectsListBuilder.add(Presentation.createForHeight(resolutionHeight));
}
transformerBuilder.setVideoEffects(effectsListBuilder.build());

transformerBuilder
.setRemoveAudio(bundle.getBoolean(ConfigurationActivity.SHOULD_REMOVE_AUDIO))
.setRemoveVideo(bundle.getBoolean(ConfigurationActivity.SHOULD_REMOVE_VIDEO))
Expand Down Expand Up @@ -358,6 +337,24 @@ private File createExternalCacheFile(String fileName) throws IOException {
return file;
}

@RequiresNonNull({
"inputCardView",
"outputPlayerView",
"transformationStopwatch",
"progressViewGroup",
})
private EditedMediaItem createEditedMediaItem(MediaItem mediaItem, @Nullable Bundle bundle)
throws PackageManager.NameNotFoundException {
if (bundle == null) {
return new EditedMediaItem(mediaItem);
}

ImmutableList<AudioProcessor> audioProcessors = createAudioProcessorsFromBundle(bundle);
ImmutableList<Effect> videoEffects = createVideoEffectsFromBundle(bundle);
Effects effects = new Effects(audioProcessors, videoEffects);
return new EditedMediaItem(mediaItem, effects);
}

private ImmutableList<AudioProcessor> createAudioProcessorsFromBundle(Bundle bundle) {
@Nullable
boolean[] selectedAudioEffects =
Expand Down Expand Up @@ -518,6 +515,24 @@ private ImmutableList<Effect> createVideoEffectsFromBundle(Bundle bundle)
effects.add(overlayEffect);
}

float scaleX = bundle.getFloat(ConfigurationActivity.SCALE_X, /* defaultValue= */ 1);
float scaleY = bundle.getFloat(ConfigurationActivity.SCALE_Y, /* defaultValue= */ 1);
float rotateDegrees =
bundle.getFloat(ConfigurationActivity.ROTATE_DEGREES, /* defaultValue= */ 0);
if (scaleX != 1f || scaleY != 1f || rotateDegrees != 0f) {
effects.add(
new ScaleToFitTransformation.Builder()
.setScale(scaleX, scaleY)
.setRotationDegrees(rotateDegrees)
.build());
}

int resolutionHeight =
bundle.getInt(ConfigurationActivity.RESOLUTION_HEIGHT, /* defaultValue= */ C.LENGTH_UNSET);
if (resolutionHeight != C.LENGTH_UNSET) {
effects.add(Presentation.createForHeight(resolutionHeight));
}

return effects.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,26 @@ private TransformerAndroidTestRunner(
* @throws Exception The cause of the transformation not completing.
*/
public TransformationTestResult run(String testId, MediaItem mediaItem) throws Exception {
return run(testId, new EditedMediaItem(mediaItem));
}

/**
* Transforms the {@link EditedMediaItem}, saving a summary of the transformation to the
* application cache.
*
* @param testId A unique identifier for the transformer test run.
* @param editedMediaItem The {@link EditedMediaItem} to transform.
* @return The {@link TransformationTestResult}.
* @throws Exception The cause of the transformation not completing.
*/
public TransformationTestResult run(String testId, EditedMediaItem editedMediaItem)
throws Exception {
JSONObject resultJson = new JSONObject();
if (inputValues != null) {
resultJson.put("inputValues", JSONObject.wrap(inputValues));
}
try {
TransformationTestResult transformationTestResult = runInternal(testId, mediaItem);
TransformationTestResult transformationTestResult = runInternal(testId, editedMediaItem);
resultJson.put("transformationResult", transformationTestResult.asJsonObject());
if (transformationTestResult.testException != null) {
throw transformationTestResult.testException;
Expand All @@ -208,17 +222,18 @@ public TransformationTestResult run(String testId, MediaItem mediaItem) throws E
}

/**
* Transforms the {@link MediaItem}.
* Transforms the {@link EditedMediaItem}.
*
* @param testId An identifier for the test.
* @param mediaItem The {@link MediaItem} to transform.
* @param editedMediaItem The {@link EditedMediaItem} to transform.
* @return The {@link TransformationTestResult}.
* @throws InterruptedException If the thread is interrupted whilst waiting for transformer to
* complete.
* @throws IOException If an error occurs opening the output file for writing.
*/
private TransformationTestResult runInternal(String testId, MediaItem mediaItem)
private TransformationTestResult runInternal(String testId, EditedMediaItem editedMediaItem)
throws InterruptedException, IOException {
MediaItem mediaItem = editedMediaItem.mediaItem;
if (!mediaItem.clippingConfiguration.equals(MediaItem.ClippingConfiguration.UNSET)
&& requestCalculateSsim) {
throw new UnsupportedOperationException(
Expand Down Expand Up @@ -293,7 +308,8 @@ public void onFallbackApplied(
.runOnMainSync(
() -> {
try {
testTransformer.startTransformation(mediaItem, outputVideoFile.getAbsolutePath());
testTransformer.startTransformation(
editedMediaItem, outputVideoFile.getAbsolutePath());
// Catch all exceptions to report. Exceptions thrown here and not caught will NOT
// propagate.
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import android.content.Context;
import android.net.Uri;
import androidx.media3.common.Effect;
import androidx.media3.common.Format;
import androidx.media3.common.MediaItem;
import androidx.media3.effect.Presentation;
Expand All @@ -44,20 +45,21 @@ public class TransformerEndToEndTest {
public void videoEditing_completesWithConsistentFrameCount() throws Exception {
Transformer transformer =
new Transformer.Builder(context)
.setVideoEffects(ImmutableList.of(Presentation.createForHeight(480)))
.setEncoderFactory(
new DefaultEncoderFactory.Builder(context).setEnableFallback(false).build())
.build();
MediaItem mediaItem = MediaItem.fromUri(Uri.parse(MP4_ASSET_URI_STRING));
ImmutableList<Effect> videoEffects = ImmutableList.of(Presentation.createForHeight(480));
Effects effects = new Effects(/* audioProcessors= */ ImmutableList.of(), videoEffects);
EditedMediaItem editedMediaItem = new EditedMediaItem(mediaItem, effects);
// Result of the following command:
// ffprobe -count_frames -select_streams v:0 -show_entries stream=nb_read_frames sample.mp4
int expectedFrameCount = 30;

TransformationTestResult result =
new TransformerAndroidTestRunner.Builder(context, transformer)
.build()
.run(
/* testId= */ "videoEditing_completesWithConsistentFrameCount",
MediaItem.fromUri(Uri.parse(MP4_ASSET_URI_STRING)));
.run(/* testId= */ "videoEditing_completesWithConsistentFrameCount", editedMediaItem);

assertThat(result.transformationResult.videoFrameCount).isEqualTo(expectedFrameCount);
}
Expand All @@ -67,18 +69,19 @@ public void videoOnly_completesWithConsistentDuration() throws Exception {
Transformer transformer =
new Transformer.Builder(context)
.setRemoveAudio(true)
.setVideoEffects(ImmutableList.of(Presentation.createForHeight(480)))
.setEncoderFactory(
new DefaultEncoderFactory.Builder(context).setEnableFallback(false).build())
.build();
MediaItem mediaItem = MediaItem.fromUri(Uri.parse(MP4_ASSET_URI_STRING));
ImmutableList<Effect> videoEffects = ImmutableList.of(Presentation.createForHeight(480));
Effects effects = new Effects(/* audioProcessors= */ ImmutableList.of(), videoEffects);
EditedMediaItem editedMediaItem = new EditedMediaItem(mediaItem, effects);
long expectedDurationMs = 967;

TransformationTestResult result =
new TransformerAndroidTestRunner.Builder(context, transformer)
.build()
.run(
/* testId= */ "videoOnly_completesWithConsistentDuration",
MediaItem.fromUri(Uri.parse(MP4_ASSET_URI_STRING)));
.run(/* testId= */ "videoOnly_completesWithConsistentDuration", editedMediaItem);

assertThat(result.transformationResult.durationMs).isEqualTo(expectedDurationMs);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@
import android.net.Uri;
import androidx.media3.common.C;
import androidx.media3.common.ColorInfo;
import androidx.media3.common.Effect;
import androidx.media3.common.MediaItem;
import androidx.media3.common.util.Log;
import androidx.media3.common.util.Util;
import androidx.media3.effect.ScaleToFitTransformation;
import androidx.media3.transformer.EditedMediaItem;
import androidx.media3.transformer.Effects;
import androidx.media3.transformer.EncoderUtil;
import androidx.media3.transformer.TransformationException;
import androidx.media3.transformer.TransformationRequest;
Expand Down Expand Up @@ -79,7 +82,6 @@ public void transform_noRequestedTranscode_hdr10File_transformsOrThrows() throws
.run(testId, MediaItem.fromUri(Uri.parse(MP4_ASSET_1080P_4_SECOND_HDR10)));
Log.i(TAG, "Transformed.");
assertFileHasColorTransfer(transformationTestResult.filePath, C.COLOR_TRANSFER_ST2084);
return;
} catch (TransformationException exception) {
Log.i(TAG, checkNotNull(exception.getCause()).toString());
assertThat(exception).hasCauseThat().isInstanceOf(IllegalArgumentException.class);
Expand All @@ -102,7 +104,6 @@ public void transform_noRequestedTranscode_hlg10File_transformsOrThrows() throws
.run(testId, MediaItem.fromUri(Uri.parse(MP4_ASSET_1080P_5_SECOND_HLG10)));
Log.i(TAG, "Transformed.");
assertFileHasColorTransfer(transformationTestResult.filePath, C.COLOR_TRANSFER_HLG);
return;
} catch (TransformationException exception) {
Log.i(TAG, checkNotNull(exception.getCause()).toString());
assertThat(exception).hasCauseThat().isInstanceOf(IllegalArgumentException.class);
Expand All @@ -121,17 +122,17 @@ public void transformAndTranscode_hdr10File_whenHdrEditingIsSupported_transforms
return;
}

Transformer transformer =
new Transformer.Builder(context)
.setVideoEffects(
ImmutableList.of(
new ScaleToFitTransformation.Builder().setRotationDegrees(180).build()))
.build();
Transformer transformer = new Transformer.Builder(context).build();
MediaItem mediaItem = MediaItem.fromUri(Uri.parse(MP4_ASSET_1080P_4_SECOND_HDR10));
ImmutableList<Effect> videoEffects =
ImmutableList.of(new ScaleToFitTransformation.Builder().setRotationDegrees(180).build());
Effects effects = new Effects(/* audioProcessors= */ ImmutableList.of(), videoEffects);
EditedMediaItem editedMediaItem = new EditedMediaItem(mediaItem, effects);

TransformationTestResult transformationTestResult =
new TransformerAndroidTestRunner.Builder(context, transformer)
.build()
.run(testId, MediaItem.fromUri(Uri.parse(MP4_ASSET_1080P_4_SECOND_HDR10)));
.run(testId, editedMediaItem);
assertFileHasColorTransfer(transformationTestResult.filePath, C.COLOR_TRANSFER_ST2084);
}

Expand All @@ -145,17 +146,17 @@ public void transformAndTranscode_hlg10File_whenHdrEditingIsSupported_transforms
return;
}

Transformer transformer =
new Transformer.Builder(context)
.setVideoEffects(
ImmutableList.of(
new ScaleToFitTransformation.Builder().setRotationDegrees(180).build()))
.build();
Transformer transformer = new Transformer.Builder(context).build();
MediaItem mediaItem = MediaItem.fromUri(Uri.parse(MP4_ASSET_1080P_5_SECOND_HLG10));
ImmutableList<Effect> videoEffects =
ImmutableList.of(new ScaleToFitTransformation.Builder().setRotationDegrees(180).build());
Effects effects = new Effects(/* audioProcessors= */ ImmutableList.of(), videoEffects);
EditedMediaItem editedMediaItem = new EditedMediaItem(mediaItem, effects);

TransformationTestResult transformationTestResult =
new TransformerAndroidTestRunner.Builder(context, transformer)
.build()
.run(testId, MediaItem.fromUri(Uri.parse(MP4_ASSET_1080P_5_SECOND_HLG10)));
.run(testId, editedMediaItem);
assertFileHasColorTransfer(transformationTestResult.filePath, C.COLOR_TRANSFER_HLG);
}

Expand All @@ -173,9 +174,6 @@ public void transformAndTranscode_hdr10File_whenHdrEditingUnsupported_toneMapsOr
AtomicBoolean isToneMappingFallbackApplied = new AtomicBoolean();
Transformer transformer =
new Transformer.Builder(context)
.setVideoEffects(
ImmutableList.of(
new ScaleToFitTransformation.Builder().setRotationDegrees(180).build()))
.addListener(
new Transformer.Listener() {
@Override
Expand All @@ -192,12 +190,17 @@ public void onFallbackApplied(
}
})
.build();
MediaItem mediaItem = MediaItem.fromUri(Uri.parse(MP4_ASSET_1080P_4_SECOND_HDR10));
ImmutableList<Effect> videoEffects =
ImmutableList.of(new ScaleToFitTransformation.Builder().setRotationDegrees(180).build());
Effects effects = new Effects(/* audioProcessors= */ ImmutableList.of(), videoEffects);
EditedMediaItem editedMediaItem = new EditedMediaItem(mediaItem, effects);

try {
TransformationTestResult transformationTestResult =
new TransformerAndroidTestRunner.Builder(context, transformer)
.build()
.run(testId, MediaItem.fromUri(Uri.parse(MP4_ASSET_1080P_4_SECOND_HDR10)));
.run(testId, editedMediaItem);
Log.i(TAG, "Tone mapped.");
assertThat(isToneMappingFallbackApplied.get()).isTrue();
assertFileHasColorTransfer(transformationTestResult.filePath, C.COLOR_TRANSFER_SDR);
Expand All @@ -209,7 +212,6 @@ public void onFallbackApplied(
TransformationException.ERROR_CODE_HDR_ENCODING_UNSUPPORTED,
TransformationException.ERROR_CODE_DECODING_FORMAT_UNSUPPORTED);
assertThat(isFallbackListenerInvoked.get()).isFalse();
return;
}
}

Expand All @@ -227,9 +229,6 @@ public void transformAndTranscode_hlg10File_whenHdrEditingUnsupported_toneMapsOr
AtomicBoolean isToneMappingFallbackApplied = new AtomicBoolean();
Transformer transformer =
new Transformer.Builder(context)
.setVideoEffects(
ImmutableList.of(
new ScaleToFitTransformation.Builder().setRotationDegrees(180).build()))
.addListener(
new Transformer.Listener() {
@Override
Expand All @@ -246,12 +245,17 @@ public void onFallbackApplied(
}
})
.build();
MediaItem mediaItem = MediaItem.fromUri(Uri.parse(MP4_ASSET_1080P_5_SECOND_HLG10));
ImmutableList<Effect> videoEffects =
ImmutableList.of(new ScaleToFitTransformation.Builder().setRotationDegrees(180).build());
Effects effects = new Effects(/* audioProcessors= */ ImmutableList.of(), videoEffects);
EditedMediaItem editedMediaItem = new EditedMediaItem(mediaItem, effects);

try {
TransformationTestResult transformationTestResult =
new TransformerAndroidTestRunner.Builder(context, transformer)
.build()
.run(testId, MediaItem.fromUri(Uri.parse(MP4_ASSET_1080P_5_SECOND_HLG10)));
.run(testId, editedMediaItem);
Log.i(TAG, "Tone mapped.");
assertThat(isToneMappingFallbackApplied.get()).isTrue();
assertFileHasColorTransfer(transformationTestResult.filePath, C.COLOR_TRANSFER_SDR);
Expand Down
Loading

0 comments on commit 2cb4e3d

Please sign in to comment.