Search in sources :

Example 1 with ProgressHolder

use of com.google.android.exoplayer2.transformer.ProgressHolder 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 ProgressHolder

use of com.google.android.exoplayer2.transformer.ProgressHolder 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)

Example 3 with ProgressHolder

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

the class TransformerEndToEndTest method getProgress_noCurrentTransformation_returnsNoTransformation.

@Test
public void getProgress_noCurrentTransformation_returnsNoTransformation() throws Exception {
    Transformer transformer = createTransformerBuilder().build();
    MediaItem mediaItem = MediaItem.fromUri(URI_PREFIX + FILE_VIDEO_ONLY);
    @Transformer.ProgressState int stateBeforeTransform = transformer.getProgress(progressHolder);
    transformer.startTransformation(mediaItem, outputPath);
    TransformerTestRunner.runUntilCompleted(transformer);
    @Transformer.ProgressState int stateAfterTransform = transformer.getProgress(progressHolder);
    assertThat(stateBeforeTransform).isEqualTo(Transformer.PROGRESS_STATE_NO_TRANSFORMATION);
    assertThat(stateAfterTransform).isEqualTo(Transformer.PROGRESS_STATE_NO_TRANSFORMATION);
}
Also used : MediaItem(com.google.android.exoplayer2.MediaItem) Test(org.junit.Test)

Example 4 with ProgressHolder

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

the class TransformerEndToEndTest method getProgress_knownDuration_returnsConsistentStates.

@Test
public void getProgress_knownDuration_returnsConsistentStates() throws Exception {
    Transformer transformer = createTransformerBuilder().build();
    MediaItem mediaItem = MediaItem.fromUri(URI_PREFIX + FILE_VIDEO_ONLY);
    AtomicInteger previousProgressState = new AtomicInteger(PROGRESS_STATE_WAITING_FOR_AVAILABILITY);
    AtomicBoolean foundInconsistentState = new AtomicBoolean();
    Handler progressHandler = new Handler(Looper.myLooper()) {

        @Override
        public void handleMessage(Message msg) {
            @Transformer.ProgressState int progressState = transformer.getProgress(progressHolder);
            if (progressState == PROGRESS_STATE_UNAVAILABLE) {
                foundInconsistentState.set(true);
                return;
            }
            switch(previousProgressState.get()) {
                case PROGRESS_STATE_WAITING_FOR_AVAILABILITY:
                    break;
                case PROGRESS_STATE_AVAILABLE:
                    if (progressState == PROGRESS_STATE_WAITING_FOR_AVAILABILITY) {
                        foundInconsistentState.set(true);
                        return;
                    }
                    break;
                case PROGRESS_STATE_NO_TRANSFORMATION:
                    if (progressState != PROGRESS_STATE_NO_TRANSFORMATION) {
                        foundInconsistentState.set(true);
                        return;
                    }
                    break;
                default:
                    throw new IllegalStateException();
            }
            previousProgressState.set(progressState);
            sendEmptyMessage(0);
        }
    };
    transformer.startTransformation(mediaItem, outputPath);
    progressHandler.sendEmptyMessage(0);
    TransformerTestRunner.runUntilCompleted(transformer);
    assertThat(foundInconsistentState.get()).isFalse();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Message(android.os.Message) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MediaItem(com.google.android.exoplayer2.MediaItem) Handler(android.os.Handler) Test(org.junit.Test)

Example 5 with ProgressHolder

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

the class Transformer method getProgress.

/**
 * Returns the current {@link ProgressState} and updates {@code progressHolder} with the current
 * progress if it is {@link #PROGRESS_STATE_AVAILABLE available}.
 *
 * <p>After a transformation {@link Listener#onTransformationCompleted(MediaItem) completes}, this
 * method returns {@link #PROGRESS_STATE_NO_TRANSFORMATION}.
 *
 * @param progressHolder A {@link ProgressHolder}, updated to hold the percentage progress if
 *     {@link #PROGRESS_STATE_AVAILABLE available}.
 * @return The {@link ProgressState}.
 * @throws IllegalStateException If this method is called from the wrong thread.
 */
@ProgressState
public int getProgress(ProgressHolder progressHolder) {
    verifyApplicationThread();
    if (progressState == PROGRESS_STATE_AVAILABLE) {
        Player player = checkNotNull(this.player);
        long durationMs = player.getDuration();
        long positionMs = player.getCurrentPosition();
        progressHolder.progress = min((int) (positionMs * 100 / durationMs), 99);
    }
    return progressState;
}
Also used : Player(com.google.android.exoplayer2.Player) ExoPlayer(com.google.android.exoplayer2.ExoPlayer)

Aggregations

Handler (android.os.Handler)4 MediaItem (com.google.android.exoplayer2.MediaItem)4 Test (org.junit.Test)4 Message (android.os.Message)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Intent (android.content.Intent)1 Uri (android.net.Uri)1 Bundle (android.os.Bundle)1 Nullable (androidx.annotation.Nullable)1 ExoPlayer (com.google.android.exoplayer2.ExoPlayer)1 Player (com.google.android.exoplayer2.Player)1 FakeClock (com.google.android.exoplayer2.testutil.FakeClock)1 ProgressHolder (com.google.android.exoplayer2.transformer.ProgressHolder)1 Transformer (com.google.android.exoplayer2.transformer.Transformer)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 RequiresNonNull (org.checkerframework.checker.nullness.qual.RequiresNonNull)1 Before (org.junit.Before)1