Search in sources :

Example 1 with Transformer

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

the class TransformerActivity method startTransformation.

@RequiresNonNull({ "playerView", "debugTextView", "informationTextView", "progressIndicator", "transformationStopwatch", "progressViewGroup", "debugFrame" })
private void startTransformation() {
    requestTransformerPermission();
    Intent intent = getIntent();
    Uri uri = checkNotNull(intent.getData());
    try {
        externalCacheFile = createExternalCacheFile("transformer-output.mp4");
        String filePath = externalCacheFile.getAbsolutePath();
        @Nullable Bundle bundle = intent.getExtras();
        Transformer transformer = createTransformer(bundle, filePath);
        transformationStopwatch.start();
        transformer.startTransformation(MediaItem.fromUri(uri), filePath);
        this.transformer = transformer;
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
    informationTextView.setText(R.string.transformation_started);
    playerView.setVisibility(View.GONE);
    Handler mainHandler = new Handler(getMainLooper());
    ProgressHolder progressHolder = new ProgressHolder();
    mainHandler.post(new Runnable() {

        @Override
        public void run() {
            if (transformer != null && transformer.getProgress(progressHolder) != Transformer.PROGRESS_STATE_NO_TRANSFORMATION) {
                progressIndicator.setProgress(progressHolder.progress);
                informationTextView.setText(getString(R.string.transformation_timer, transformationStopwatch.elapsed(TimeUnit.SECONDS)));
                mainHandler.postDelayed(/* r= */
                this, /* delayMillis= */
                500);
            }
        }
    });
}
Also used : Transformer(com.google.android.exoplayer2.transformer.Transformer) Bundle(android.os.Bundle) Handler(android.os.Handler) Intent(android.content.Intent) IOException(java.io.IOException) Uri(android.net.Uri) Nullable(androidx.annotation.Nullable) ProgressHolder(com.google.android.exoplayer2.transformer.ProgressHolder) RequiresNonNull(org.checkerframework.checker.nullness.qual.RequiresNonNull)

Example 2 with Transformer

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

the class TransformerEndToEndTest method startTransformation_audioOnlyTranscoding_completesSuccessfully.

@Test
public void startTransformation_audioOnlyTranscoding_completesSuccessfully() throws Exception {
    Transformer transformer = createTransformerBuilder().setTransformationRequest(new TransformationRequest.Builder().setAudioMimeType(// supported by encoder and muxer
    MimeTypes.AUDIO_AAC).build()).build();
    MediaItem mediaItem = MediaItem.fromUri(URI_PREFIX + FILE_AUDIO_UNSUPPORTED_BY_ENCODER);
    transformer.startTransformation(mediaItem, outputPath);
    TransformerTestRunner.runUntilCompleted(transformer);
    DumpFileAsserts.assertOutput(context, testMuxer, getDumpFileName(FILE_AUDIO_UNSUPPORTED_BY_ENCODER + ".aac"));
}
Also used : MediaItem(com.google.android.exoplayer2.MediaItem) Test(org.junit.Test)

Example 3 with Transformer

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

the class TransformerEndToEndTest method startTransformation_fromWrongThread_throwsError.

@Test
public void startTransformation_fromWrongThread_throwsError() throws Exception {
    Transformer transformer = createTransformerBuilder().build();
    MediaItem mediaItem = MediaItem.fromUri(URI_PREFIX + FILE_AUDIO_VIDEO);
    HandlerThread anotherThread = new HandlerThread("AnotherThread");
    AtomicReference<IllegalStateException> illegalStateException = new AtomicReference<>();
    CountDownLatch countDownLatch = new CountDownLatch(1);
    anotherThread.start();
    new Handler(anotherThread.getLooper()).post(() -> {
        try {
            transformer.startTransformation(mediaItem, outputPath);
        } catch (IOException e) {
        // Do nothing.
        } catch (IllegalStateException e) {
            illegalStateException.set(e);
        } finally {
            countDownLatch.countDown();
        }
    });
    countDownLatch.await();
    assertThat(illegalStateException.get()).isNotNull();
}
Also used : HandlerThread(android.os.HandlerThread) MediaItem(com.google.android.exoplayer2.MediaItem) Handler(android.os.Handler) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 4 with Transformer

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

the class TransformerEndToEndTest method startTransformation_concurrentTransformations_throwsError.

@Test
public void startTransformation_concurrentTransformations_throwsError() throws Exception {
    Transformer transformer = createTransformerBuilder().build();
    MediaItem mediaItem = MediaItem.fromUri(URI_PREFIX + FILE_VIDEO_ONLY);
    transformer.startTransformation(mediaItem, outputPath);
    assertThrows(IllegalStateException.class, () -> transformer.startTransformation(mediaItem, outputPath));
}
Also used : MediaItem(com.google.android.exoplayer2.MediaItem) Test(org.junit.Test)

Example 5 with Transformer

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

the class TransformerEndToEndTest method getProgress_knownDuration_givesIncreasingPercentages.

@Test
public void getProgress_knownDuration_givesIncreasingPercentages() throws Exception {
    Transformer transformer = createTransformerBuilder().build();
    MediaItem mediaItem = MediaItem.fromUri(URI_PREFIX + FILE_VIDEO_ONLY);
    List<Integer> progresses = new ArrayList<>();
    Handler progressHandler = new Handler(Looper.myLooper()) {

        @Override
        public void handleMessage(Message msg) {
            @Transformer.ProgressState int progressState = transformer.getProgress(progressHolder);
            if (progressState == PROGRESS_STATE_NO_TRANSFORMATION) {
                return;
            }
            if (progressState != PROGRESS_STATE_WAITING_FOR_AVAILABILITY && (progresses.isEmpty() || Iterables.getLast(progresses) != progressHolder.progress)) {
                progresses.add(progressHolder.progress);
            }
            sendEmptyMessage(0);
        }
    };
    transformer.startTransformation(mediaItem, outputPath);
    progressHandler.sendEmptyMessage(0);
    TransformerTestRunner.runUntilCompleted(transformer);
    assertThat(progresses).isInOrder();
    if (!progresses.isEmpty()) {
        // The progress list could be empty if the transformation ends before any progress can be
        // retrieved.
        assertThat(progresses.get(0)).isAtLeast(0);
        assertThat(Iterables.getLast(progresses)).isLessThan(100);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Message(android.os.Message) MediaItem(com.google.android.exoplayer2.MediaItem) ArrayList(java.util.ArrayList) Handler(android.os.Handler) 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