Search in sources :

Example 21 with Transformer

use of com.google.android.exoplayer2.transformer.Transformer in project ExoPlayer by google.

the class AndroidTestUtil method runTransformer.

/**
 * Transforms the {@code uriString} with the {@link Transformer}.
 *
 * @param context The {@link Context}.
 * @param testId An identifier for the test.
 * @param transformer The {@link Transformer} that performs the transformation.
 * @param uriString The uri (as a {@link String}) that will be transformed.
 * @param timeoutSeconds The transformer timeout. An assertion confirms this is not exceeded.
 * @return The {@link TransformationResult}.
 * @throws Exception The cause of the transformation not completing.
 */
public static TransformationResult runTransformer(Context context, String testId, Transformer transformer, String uriString, int timeoutSeconds) throws Exception {
    AtomicReference<@NullableType Exception> exceptionReference = new AtomicReference<>();
    CountDownLatch countDownLatch = new CountDownLatch(1);
    Transformer testTransformer = transformer.buildUpon().addListener(new Transformer.Listener() {

        @Override
        public void onTransformationCompleted(MediaItem inputMediaItem) {
            countDownLatch.countDown();
        }

        @Override
        public void onTransformationError(MediaItem inputMediaItem, TransformationException exception) {
            exceptionReference.set(exception);
            countDownLatch.countDown();
        }
    }).build();
    Uri uri = Uri.parse(uriString);
    File outputVideoFile = createExternalCacheFile(context, /* fileName= */
    testId + "-output.mp4");
    InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> {
        try {
            testTransformer.startTransformation(MediaItem.fromUri(uri), outputVideoFile.getAbsolutePath());
        } catch (IOException e) {
            exceptionReference.set(e);
        }
    });
    assertWithMessage("Transformer timed out after " + timeoutSeconds + " seconds.").that(countDownLatch.await(timeoutSeconds, SECONDS)).isTrue();
    @Nullable Exception exception = exceptionReference.get();
    if (exception != null) {
        throw exception;
    }
    TransformationResult result = new TransformationResult.Builder(testId).setFileSizeBytes(outputVideoFile.length()).build();
    writeTransformationResultToFile(context, result);
    return result;
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) Uri(android.net.Uri) IOException(java.io.IOException) MediaItem(com.google.android.exoplayer2.MediaItem) File(java.io.File) Nullable(androidx.annotation.Nullable)

Example 22 with Transformer

use of com.google.android.exoplayer2.transformer.Transformer in project ExoPlayer by google.

the class RemoveVideoTransformationTest method removeVideoTransform.

@Test
public void removeVideoTransform() throws Exception {
    Context context = ApplicationProvider.getApplicationContext();
    Transformer transformer = new Transformer.Builder(context).setRemoveVideo(true).build();
    runTransformer(context, /* testId= */
    "removeVideoTransform", transformer, MP4_ASSET_URI_STRING, /* timeoutSeconds= */
    120);
}
Also used : Context(android.content.Context) AndroidTestUtil.runTransformer(com.google.android.exoplayer2.transformer.AndroidTestUtil.runTransformer) Transformer(com.google.android.exoplayer2.transformer.Transformer) Test(org.junit.Test)

Example 23 with Transformer

use of com.google.android.exoplayer2.transformer.Transformer in project ExoPlayer by google.

the class TransformerEndToEndTest method videoEditing_completesWithConsistentFrameCount.

@Test
public void videoEditing_completesWithConsistentFrameCount() throws Exception {
    Context context = ApplicationProvider.getApplicationContext();
    Matrix transformationMatrix = new Matrix();
    transformationMatrix.postTranslate(/* dx= */
    .2f, /* dy= */
    .1f);
    FrameCountingMuxer.Factory muxerFactory = new FrameCountingMuxer.Factory(new FrameworkMuxer.Factory());
    Transformer transformer = new Transformer.Builder(context).setTransformationRequest(new TransformationRequest.Builder().setTransformationMatrix(transformationMatrix).build()).setMuxerFactory(muxerFactory).build();
    // Result of the following command:
    // ffprobe -count_frames -select_streams v:0 -show_entries stream=nb_read_frames sample.mp4
    int expectedFrameCount = 30;
    runTransformer(context, /* testId= */
    "videoEditing_completesWithConsistentFrameCount", transformer, AVC_VIDEO_URI_STRING, /* timeoutSeconds= */
    120);
    FrameCountingMuxer frameCountingMuxer = checkNotNull(muxerFactory.getLastFrameCountingMuxerCreated());
    assertThat(frameCountingMuxer.getFrameCount()).isEqualTo(expectedFrameCount);
}
Also used : Context(android.content.Context) Matrix(android.graphics.Matrix) AndroidTestUtil.runTransformer(com.google.android.exoplayer2.transformer.AndroidTestUtil.runTransformer) Test(org.junit.Test)

Example 24 with Transformer

use of com.google.android.exoplayer2.transformer.Transformer in project ExoPlayer by google.

the class RemoveAudioTransformationTest method removeAudioTransform.

@Test
public void removeAudioTransform() throws Exception {
    Context context = ApplicationProvider.getApplicationContext();
    Transformer transformer = new Transformer.Builder(context).setRemoveAudio(true).build();
    runTransformer(context, /* testId= */
    "removeAudioTransform", transformer, MP4_ASSET_URI_STRING, /* timeoutSeconds= */
    120);
}
Also used : Context(android.content.Context) AndroidTestUtil.runTransformer(com.google.android.exoplayer2.transformer.AndroidTestUtil.runTransformer) Transformer(com.google.android.exoplayer2.transformer.Transformer) Test(org.junit.Test)

Example 25 with Transformer

use of com.google.android.exoplayer2.transformer.Transformer in project ExoPlayer by google.

the class RepeatedTranscodeTransformationTest method repeatedTranscodeNoVideo_givesConsistentLengthOutput.

@Test
public void repeatedTranscodeNoVideo_givesConsistentLengthOutput() throws Exception {
    Context context = ApplicationProvider.getApplicationContext();
    Transformer transformer = new Transformer.Builder(context).setRemoveVideo(true).setTransformationRequest(new TransformationRequest.Builder().setAudioMimeType(MimeTypes.AUDIO_AMR_NB).build()).build();
    Set<Long> differentOutputSizesBytes = new HashSet<>();
    for (int i = 0; i < TRANSCODE_COUNT; i++) {
        // Use a long video in case an error occurs a while after the start of the video.
        AndroidTestUtil.TransformationResult result = runTransformer(context, /* testId= */
        "repeatedTranscodeNoVideo_givesConsistentLengthOutput_" + i, transformer, AndroidTestUtil.REMOTE_MP4_10_SECONDS_URI_STRING, /* timeoutSeconds= */
        120);
        differentOutputSizesBytes.add(Assertions.checkNotNull(result.fileSizeBytes));
    }
    assertWithMessage("Different transcoding output sizes detected. Sizes: " + differentOutputSizesBytes).that(differentOutputSizesBytes.size()).isEqualTo(1);
}
Also used : Context(android.content.Context) AndroidTestUtil(com.google.android.exoplayer2.transformer.AndroidTestUtil) Transformer(com.google.android.exoplayer2.transformer.Transformer) AndroidTestUtil.runTransformer(com.google.android.exoplayer2.transformer.AndroidTestUtil.runTransformer) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)37 MediaItem (com.google.android.exoplayer2.MediaItem)29 Context (android.content.Context)10 AndroidTestUtil.runTransformer (com.google.android.exoplayer2.transformer.AndroidTestUtil.runTransformer)10 Transformer (com.google.android.exoplayer2.transformer.Transformer)10 Handler (android.os.Handler)6 Matrix (android.graphics.Matrix)5 IOException (java.io.IOException)4 Message (android.os.Message)3 Nullable (androidx.annotation.Nullable)3 AndroidTestUtil (com.google.android.exoplayer2.transformer.AndroidTestUtil)3 HashSet (java.util.HashSet)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 Uri (android.net.Uri)2 HandlerThread (android.os.HandlerThread)2 TransformationRequest (com.google.android.exoplayer2.transformer.TransformationRequest)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 RequiresNonNull (org.checkerframework.checker.nullness.qual.RequiresNonNull)2