Commit 19bf020d by kimvde Committed by microkatz

Build TransformationResult only when transformation succeeded

PiperOrigin-RevId: 489442518
parent d9d71686
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
package com.google.android.exoplayer2.transformer; package com.google.android.exoplayer2.transformer;
import static com.google.android.exoplayer2.transformer.TransformerInternal.END_TRANSFORMATION_REASON_CANCELLED;
import static com.google.android.exoplayer2.util.Assertions.checkState; import static com.google.android.exoplayer2.util.Assertions.checkState;
import static java.lang.annotation.ElementType.TYPE_USE; import static java.lang.annotation.ElementType.TYPE_USE;
...@@ -777,7 +778,7 @@ public final class Transformer { ...@@ -777,7 +778,7 @@ public final class Transformer {
return; return;
} }
try { try {
transformerInternal.release(/* forCancellation= */ true); transformerInternal.release(END_TRANSFORMATION_REASON_CANCELLED);
} catch (TransformationException impossible) { } catch (TransformationException impossible) {
throw new IllegalStateException(impossible); throw new IllegalStateException(impossible);
} }
......
...@@ -22,10 +22,12 @@ import static com.google.android.exoplayer2.transformer.Transformer.PROGRESS_STA ...@@ -22,10 +22,12 @@ import static com.google.android.exoplayer2.transformer.Transformer.PROGRESS_STA
import static com.google.android.exoplayer2.transformer.Transformer.PROGRESS_STATE_WAITING_FOR_AVAILABILITY; import static com.google.android.exoplayer2.transformer.Transformer.PROGRESS_STATE_WAITING_FOR_AVAILABILITY;
import static com.google.android.exoplayer2.util.Assertions.checkNotNull; import static com.google.android.exoplayer2.util.Assertions.checkNotNull;
import static java.lang.Math.min; import static java.lang.Math.min;
import static java.lang.annotation.ElementType.TYPE_USE;
import android.content.Context; import android.content.Context;
import android.os.Handler; import android.os.Handler;
import android.os.ParcelFileDescriptor; import android.os.ParcelFileDescriptor;
import androidx.annotation.IntDef;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.Format; import com.google.android.exoplayer2.Format;
...@@ -44,6 +46,10 @@ import com.google.android.exoplayer2.util.HandlerWrapper; ...@@ -44,6 +46,10 @@ import com.google.android.exoplayer2.util.HandlerWrapper;
import com.google.android.exoplayer2.util.MimeTypes; import com.google.android.exoplayer2.util.MimeTypes;
import com.google.android.exoplayer2.util.Util; import com.google.android.exoplayer2.util.Util;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull; import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
...@@ -57,6 +63,28 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; ...@@ -57,6 +63,28 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
void onTransformationError(TransformationException exception); void onTransformationError(TransformationException exception);
} }
/**
* Represents a reason for ending a transformation. May be one of {@link
* #END_TRANSFORMATION_REASON_COMPLETED}, {@link #END_TRANSFORMATION_REASON_CANCELLED} or {@link
* #END_TRANSFORMATION_REASON_ERROR}.
*/
@Documented
@Retention(RetentionPolicy.SOURCE)
@Target(TYPE_USE)
@IntDef({
END_TRANSFORMATION_REASON_COMPLETED,
END_TRANSFORMATION_REASON_CANCELLED,
END_TRANSFORMATION_REASON_ERROR
})
public @interface EndTransformationReason {}
/** The transformation completed successfully. */
public static final int END_TRANSFORMATION_REASON_COMPLETED = 0;
/** The transformation was cancelled. */
public static final int END_TRANSFORMATION_REASON_CANCELLED = 1;
/** An error occurred during the transformation. */
public static final int END_TRANSFORMATION_REASON_ERROR = 2;
private final Context context; private final Context context;
private final TransformationRequest transformationRequest; private final TransformationRequest transformationRequest;
private final ImmutableList<AudioProcessor> audioProcessors; private final ImmutableList<AudioProcessor> audioProcessors;
...@@ -145,12 +173,13 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; ...@@ -145,12 +173,13 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
/** /**
* Releases the resources. * Releases the resources.
* *
* @param forCancellation Whether the reason for releasing the resources is the transformation * @param endTransformationReason The {@linkplain EndTransformationReason reason} for ending the
* cancellation. * transformation.
* @throws TransformationException If the muxer is in the wrong state and {@code forCancellation} * @throws TransformationException If the muxer is in the wrong state and {@code
* is false. * endTransformationReason} is not {@link #END_TRANSFORMATION_REASON_CANCELLED}.
*/ */
public void release(boolean forCancellation) throws TransformationException { public void release(@EndTransformationReason int endTransformationReason)
throws TransformationException {
if (released) { if (released) {
return; return;
} }
...@@ -161,16 +190,20 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; ...@@ -161,16 +190,20 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
clock.createHandler(exoPlayerAssetLoader.getPlaybackLooper(), /* callback= */ null); clock.createHandler(exoPlayerAssetLoader.getPlaybackLooper(), /* callback= */ null);
playbackHandler.post( playbackHandler.post(
() -> { () -> {
transformationResult = if (endTransformationReason == END_TRANSFORMATION_REASON_COMPLETED) {
new TransformationResult.Builder() transformationResult =
.setDurationMs(checkNotNull(muxerWrapper).getDurationMs()) new TransformationResult.Builder()
.setAverageAudioBitrate(muxerWrapper.getTrackAverageBitrate(C.TRACK_TYPE_AUDIO)) .setDurationMs(checkNotNull(muxerWrapper).getDurationMs())
.setAverageVideoBitrate(muxerWrapper.getTrackAverageBitrate(C.TRACK_TYPE_VIDEO)) .setAverageAudioBitrate(muxerWrapper.getTrackAverageBitrate(C.TRACK_TYPE_AUDIO))
.setVideoFrameCount(muxerWrapper.getTrackSampleCount(C.TRACK_TYPE_VIDEO)) .setAverageVideoBitrate(muxerWrapper.getTrackAverageBitrate(C.TRACK_TYPE_VIDEO))
.setFileSizeBytes(muxerWrapper.getCurrentOutputSizeBytes()) .setVideoFrameCount(muxerWrapper.getTrackSampleCount(C.TRACK_TYPE_VIDEO))
.build(); .setFileSizeBytes(muxerWrapper.getCurrentOutputSizeBytes())
.build();
}
try { try {
muxerWrapper.release(forCancellation); muxerWrapper.release(
/* forCancellation= */ endTransformationReason
== END_TRANSFORMATION_REASON_CANCELLED);
} catch (Muxer.MuxerException e) { } catch (Muxer.MuxerException e) {
releaseMuxerException = releaseMuxerException =
TransformationException.createForMuxer( TransformationException.createForMuxer(
...@@ -392,7 +425,10 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; ...@@ -392,7 +425,10 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
() -> { () -> {
@Nullable TransformationException releaseException = null; @Nullable TransformationException releaseException = null;
try { try {
release(/* forCancellation= */ false); release(
transformationException == null
? END_TRANSFORMATION_REASON_COMPLETED
: END_TRANSFORMATION_REASON_ERROR);
} catch (TransformationException e) { } catch (TransformationException e) {
releaseException = e; releaseException = e;
} catch (RuntimeException e) { } catch (RuntimeException e) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment