diff --git a/libraries/effect/src/androidTest/java/androidx/media3/effect/SpeedChangeEffectTest.java b/libraries/effect/src/androidTest/java/androidx/media3/effect/SpeedChangeEffectTest.java new file mode 100644 index 0000000000..770c77ff05 --- /dev/null +++ b/libraries/effect/src/androidTest/java/androidx/media3/effect/SpeedChangeEffectTest.java @@ -0,0 +1,69 @@ +/* + * Copyright 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package androidx.media3.effect; + +import static androidx.media3.test.utils.BitmapPixelTestUtil.readBitmap; +import static com.google.common.truth.Truth.assertThat; + +import androidx.media3.common.C; +import androidx.media3.common.Effect; +import androidx.media3.common.VideoFrameProcessor; +import androidx.media3.test.utils.VideoFrameProcessorTestRunner; +import androidx.test.ext.junit.runners.AndroidJUnit4; +import com.google.common.collect.ImmutableList; +import java.util.ArrayList; +import java.util.List; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TestName; +import org.junit.runner.RunWith; + +/** Tests for {@link SpeedChangeEffect}. */ +@RunWith(AndroidJUnit4.class) +public class SpeedChangeEffectTest { + + @Rule public final TestName testName = new TestName(); + + private static final String IMAGE_PATH = + "media/bitmap/sample_mp4_first_frame/electrical_colors/original.png"; + + @Test + public void changeSpeed_outputsFramesAtTheCorrectPresentationTimesUs() throws Exception { + String testId = testName.getMethodName(); + VideoFrameProcessor.Factory videoFrameProcessorFactory = + new DefaultVideoFrameProcessor.Factory.Builder().build(); + ImmutableList effects = ImmutableList.of(new SpeedChangeEffect(2f)); + List outputPresentationTimesUs = new ArrayList<>(); + VideoFrameProcessorTestRunner.OnOutputFrameAvailableForRenderingListener + onOutputFrameAvailableForRenderingListener = outputPresentationTimesUs::add; + VideoFrameProcessorTestRunner videoFrameProcessorTestRunner = + new VideoFrameProcessorTestRunner.Builder() + .setTestId(testId) + .setVideoFrameProcessorFactory(videoFrameProcessorFactory) + .setEffects(effects) + .setOnOutputFrameAvailableForRenderingListener( + onOutputFrameAvailableForRenderingListener) + .build(); + + videoFrameProcessorTestRunner.queueInputBitmap( + readBitmap(IMAGE_PATH), C.MICROS_PER_SECOND, /* offsetToAddUs= */ 0L, /* frameRate= */ 5); + videoFrameProcessorTestRunner.endFrameProcessing(); + + assertThat(outputPresentationTimesUs) + .containsExactly(0L, 100_000L, 200_000L, 300_000L, 400_000L) + .inOrder(); + } +} diff --git a/libraries/effect/src/main/java/androidx/media3/effect/SpeedChangeEffect.java b/libraries/effect/src/main/java/androidx/media3/effect/SpeedChangeEffect.java new file mode 100644 index 0000000000..cb59418b7b --- /dev/null +++ b/libraries/effect/src/main/java/androidx/media3/effect/SpeedChangeEffect.java @@ -0,0 +1,51 @@ +/* + * Copyright 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package androidx.media3.effect; + +import static androidx.media3.common.util.Assertions.checkArgument; + +import android.content.Context; +import androidx.annotation.FloatRange; +import androidx.media3.common.VideoFrameProcessingException; +import androidx.media3.common.util.UnstableApi; + +/** + * Applies a speed change by updating the frame timestamps. + * + *

This effect doesn't drop any frames. + */ +@UnstableApi +public final class SpeedChangeEffect implements GlEffect { + + private final float speed; + + /** Creates an instance. */ + public SpeedChangeEffect(@FloatRange(from = 0, fromInclusive = false) float speed) { + checkArgument(speed > 0f); + this.speed = speed; + } + + @Override + public GlShaderProgram toGlShaderProgram(Context context, boolean useHdr) + throws VideoFrameProcessingException { + return new SpeedChangeShaderProgram(context, speed, useHdr); + } + + @Override + public boolean isNoOp(int inputWidth, int inputHeight) { + return speed == 1f; + } +} diff --git a/libraries/effect/src/main/java/androidx/media3/effect/SpeedChangeShaderProgram.java b/libraries/effect/src/main/java/androidx/media3/effect/SpeedChangeShaderProgram.java new file mode 100644 index 0000000000..99189bb3ce --- /dev/null +++ b/libraries/effect/src/main/java/androidx/media3/effect/SpeedChangeShaderProgram.java @@ -0,0 +1,41 @@ +/* + * Copyright 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package androidx.media3.effect; + +import android.content.Context; +import androidx.media3.common.GlObjectsProvider; +import androidx.media3.common.GlTextureInfo; +import androidx.media3.common.VideoFrameProcessingException; +import androidx.media3.common.util.UnstableApi; + +/** Applies a speed change by updating the frame timestamps. */ +@UnstableApi +/* package */ final class SpeedChangeShaderProgram extends FrameCacheGlShaderProgram { + + private final float speed; + + public SpeedChangeShaderProgram(Context context, float speed, boolean useHdr) + throws VideoFrameProcessingException { + super(context, /* capacity= */ 1, useHdr); + this.speed = speed; + } + + @Override + public void queueInputFrame( + GlObjectsProvider glObjectsProvider, GlTextureInfo inputTexture, long presentationTimeUs) { + super.queueInputFrame(glObjectsProvider, inputTexture, (long) (presentationTimeUs / speed)); + } +} diff --git a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/ExoPlayer.java b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/ExoPlayer.java index 523d8af15e..e109982ec2 100644 --- a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/ExoPlayer.java +++ b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/ExoPlayer.java @@ -1604,6 +1604,7 @@ public interface ExoPlayer extends Player { *