Commit 80851807 by hschlueter Committed by Ian Baker

Use specific error code for exceptions during encoding/decoding.

After this change exceptions throw by MediaCodec during
encoding/decoding will result in TransformationExceptions with
ERROR_CODE_ENCODING_FAILED/ERROR_CODE_DECODING_FAILED.
Before this change ERROR_CODE_FAILED_RUNTIME_CHECK was used.

PiperOrigin-RevId: 421560396
parent 05924eaa
...@@ -113,17 +113,17 @@ import java.nio.ByteBuffer; ...@@ -113,17 +113,17 @@ import java.nio.ByteBuffer;
@Override @Override
@Nullable @Nullable
public DecoderInputBuffer dequeueInputBuffer() { public DecoderInputBuffer dequeueInputBuffer() throws TransformationException {
return decoder.maybeDequeueInputBuffer(decoderInputBuffer) ? decoderInputBuffer : null; return decoder.maybeDequeueInputBuffer(decoderInputBuffer) ? decoderInputBuffer : null;
} }
@Override @Override
public void queueInputBuffer() { public void queueInputBuffer() throws TransformationException {
decoder.queueInputBuffer(decoderInputBuffer); decoder.queueInputBuffer(decoderInputBuffer);
} }
@Override @Override
public boolean processData() { public boolean processData() throws TransformationException {
if (sonicAudioProcessor.isActive()) { if (sonicAudioProcessor.isActive()) {
return feedEncoderFromSonic() || feedSonicFromDecoder(); return feedEncoderFromSonic() || feedSonicFromDecoder();
} else { } else {
...@@ -133,13 +133,13 @@ import java.nio.ByteBuffer; ...@@ -133,13 +133,13 @@ import java.nio.ByteBuffer;
@Override @Override
@Nullable @Nullable
public Format getOutputFormat() { public Format getOutputFormat() throws TransformationException {
return encoder != null ? encoder.getOutputFormat() : null; return encoder != null ? encoder.getOutputFormat() : null;
} }
@Override @Override
@Nullable @Nullable
public DecoderInputBuffer getOutputBuffer() { public DecoderInputBuffer getOutputBuffer() throws TransformationException {
if (encoder != null) { if (encoder != null) {
encoderOutputBuffer.data = encoder.getOutputBuffer(); encoderOutputBuffer.data = encoder.getOutputBuffer();
if (encoderOutputBuffer.data != null) { if (encoderOutputBuffer.data != null) {
...@@ -152,7 +152,7 @@ import java.nio.ByteBuffer; ...@@ -152,7 +152,7 @@ import java.nio.ByteBuffer;
} }
@Override @Override
public void releaseOutputBuffer() { public void releaseOutputBuffer() throws TransformationException {
checkStateNotNull(encoder).releaseOutputBuffer(); checkStateNotNull(encoder).releaseOutputBuffer();
} }
...@@ -174,7 +174,7 @@ import java.nio.ByteBuffer; ...@@ -174,7 +174,7 @@ import java.nio.ByteBuffer;
* Attempts to pass decoder output data to the encoder, and returns whether it may be possible to * Attempts to pass decoder output data to the encoder, and returns whether it may be possible to
* pass more data immediately by calling this method again. * pass more data immediately by calling this method again.
*/ */
private boolean feedEncoderFromDecoder() { private boolean feedEncoderFromDecoder() throws TransformationException {
if (!encoder.maybeDequeueInputBuffer(encoderInputBuffer)) { if (!encoder.maybeDequeueInputBuffer(encoderInputBuffer)) {
return false; return false;
} }
...@@ -203,7 +203,7 @@ import java.nio.ByteBuffer; ...@@ -203,7 +203,7 @@ import java.nio.ByteBuffer;
* Attempts to pass audio processor output data to the encoder, and returns whether it may be * Attempts to pass audio processor output data to the encoder, and returns whether it may be
* possible to pass more data immediately by calling this method again. * possible to pass more data immediately by calling this method again.
*/ */
private boolean feedEncoderFromSonic() { private boolean feedEncoderFromSonic() throws TransformationException {
if (!encoder.maybeDequeueInputBuffer(encoderInputBuffer)) { if (!encoder.maybeDequeueInputBuffer(encoderInputBuffer)) {
return false; return false;
} }
...@@ -226,7 +226,7 @@ import java.nio.ByteBuffer; ...@@ -226,7 +226,7 @@ import java.nio.ByteBuffer;
* Attempts to process decoder output data, and returns whether it may be possible to process more * Attempts to process decoder output data, and returns whether it may be possible to process more
* data immediately by calling this method again. * data immediately by calling this method again.
*/ */
private boolean feedSonicFromDecoder() { private boolean feedSonicFromDecoder() throws TransformationException {
if (drainingSonicForSpeedChange) { if (drainingSonicForSpeedChange) {
if (sonicAudioProcessor.isEnded() && !sonicOutputBuffer.hasRemaining()) { if (sonicAudioProcessor.isEnded() && !sonicOutputBuffer.hasRemaining()) {
flushSonicAndSetSpeed(currentSpeed); flushSonicAndSetSpeed(currentSpeed);
...@@ -267,7 +267,7 @@ import java.nio.ByteBuffer; ...@@ -267,7 +267,7 @@ import java.nio.ByteBuffer;
* Feeds as much data as possible between the current position and limit of the specified {@link * Feeds as much data as possible between the current position and limit of the specified {@link
* ByteBuffer} to the encoder, and advances its position by the number of bytes fed. * ByteBuffer} to the encoder, and advances its position by the number of bytes fed.
*/ */
private void feedEncoder(ByteBuffer inputBuffer) { private void feedEncoder(ByteBuffer inputBuffer) throws TransformationException {
ByteBuffer encoderInputBufferData = checkNotNull(encoderInputBuffer.data); ByteBuffer encoderInputBufferData = checkNotNull(encoderInputBuffer.data);
int bufferLimit = inputBuffer.limit(); int bufferLimit = inputBuffer.limit();
inputBuffer.limit(min(bufferLimit, inputBuffer.position() + encoderInputBufferData.capacity())); inputBuffer.limit(min(bufferLimit, inputBuffer.position() + encoderInputBufferData.capacity()));
...@@ -283,7 +283,7 @@ import java.nio.ByteBuffer; ...@@ -283,7 +283,7 @@ import java.nio.ByteBuffer;
encoder.queueInputBuffer(encoderInputBuffer); encoder.queueInputBuffer(encoderInputBuffer);
} }
private void queueEndOfStreamToEncoder() { private void queueEndOfStreamToEncoder() throws TransformationException {
checkState(checkNotNull(encoderInputBuffer.data).position() == 0); checkState(checkNotNull(encoderInputBuffer.data).position() == 0);
encoderInputBuffer.timeUs = nextEncoderInputBufferTimeUs; encoderInputBuffer.timeUs = nextEncoderInputBufferTimeUs;
encoderInputBuffer.addFlag(C.BUFFER_FLAG_END_OF_STREAM); encoderInputBuffer.addFlag(C.BUFFER_FLAG_END_OF_STREAM);
......
...@@ -138,12 +138,14 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull; ...@@ -138,12 +138,14 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull;
if (inputSurface != null) { if (inputSurface != null) {
inputSurface.release(); inputSurface.release();
} }
@Nullable String mediaCodecName = null;
if (mediaCodec != null) { if (mediaCodec != null) {
mediaCodecName = mediaCodec.getName();
mediaCodec.release(); mediaCodec.release();
} }
throw createTransformationException(e, format, isVideo, isDecoder); throw createTransformationException(e, format, isVideo, isDecoder, mediaCodecName);
} }
return new Codec(mediaCodec, inputSurface); return new Codec(mediaCodec, format, inputSurface);
} }
private static void configureCodec( private static void configureCodec(
...@@ -167,13 +169,18 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull; ...@@ -167,13 +169,18 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull;
} }
private static TransformationException createTransformationException( private static TransformationException createTransformationException(
Exception cause, Format format, boolean isVideo, boolean isDecoder) { Exception cause,
Format format,
boolean isVideo,
boolean isDecoder,
@Nullable String mediaCodecName) {
String componentName = (isVideo ? "Video" : "Audio") + (isDecoder ? "Decoder" : "Encoder"); String componentName = (isVideo ? "Video" : "Audio") + (isDecoder ? "Decoder" : "Encoder");
if (cause instanceof IOException || cause instanceof MediaCodec.CodecException) { if (cause instanceof IOException || cause instanceof MediaCodec.CodecException) {
return TransformationException.createForCodec( return TransformationException.createForCodec(
cause, cause,
componentName, componentName,
format, format,
mediaCodecName,
isDecoder isDecoder
? TransformationException.ERROR_CODE_DECODER_INIT_FAILED ? TransformationException.ERROR_CODE_DECODER_INIT_FAILED
: TransformationException.ERROR_CODE_ENCODER_INIT_FAILED); : TransformationException.ERROR_CODE_ENCODER_INIT_FAILED);
...@@ -183,6 +190,7 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull; ...@@ -183,6 +190,7 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull;
cause, cause,
componentName, componentName,
format, format,
mediaCodecName,
isDecoder isDecoder
? TransformationException.ERROR_CODE_DECODING_FORMAT_UNSUPPORTED ? TransformationException.ERROR_CODE_DECODING_FORMAT_UNSUPPORTED
: TransformationException.ERROR_CODE_ENCODING_FORMAT_UNSUPPORTED); : TransformationException.ERROR_CODE_ENCODING_FORMAT_UNSUPPORTED);
......
...@@ -29,7 +29,7 @@ import com.google.android.exoplayer2.decoder.DecoderInputBuffer; ...@@ -29,7 +29,7 @@ import com.google.android.exoplayer2.decoder.DecoderInputBuffer;
/** Returns a buffer if the pipeline is ready to accept input, and {@code null} otherwise. */ /** Returns a buffer if the pipeline is ready to accept input, and {@code null} otherwise. */
@Nullable @Nullable
DecoderInputBuffer dequeueInputBuffer(); DecoderInputBuffer dequeueInputBuffer() throws TransformationException;
/** /**
* Informs the pipeline that its input buffer contains new input. * Informs the pipeline that its input buffer contains new input.
...@@ -37,7 +37,7 @@ import com.google.android.exoplayer2.decoder.DecoderInputBuffer; ...@@ -37,7 +37,7 @@ import com.google.android.exoplayer2.decoder.DecoderInputBuffer;
* <p>Should be called after filling the input buffer from {@link #dequeueInputBuffer()} with new * <p>Should be called after filling the input buffer from {@link #dequeueInputBuffer()} with new
* input. * input.
*/ */
void queueInputBuffer(); void queueInputBuffer() throws TransformationException;
/** /**
* Processes the input data and returns whether more data can be processed by calling this method * Processes the input data and returns whether more data can be processed by calling this method
...@@ -47,18 +47,18 @@ import com.google.android.exoplayer2.decoder.DecoderInputBuffer; ...@@ -47,18 +47,18 @@ import com.google.android.exoplayer2.decoder.DecoderInputBuffer;
/** Returns the output format of the pipeline if available, and {@code null} otherwise. */ /** Returns the output format of the pipeline if available, and {@code null} otherwise. */
@Nullable @Nullable
Format getOutputFormat(); Format getOutputFormat() throws TransformationException;
/** Returns an output buffer if the pipeline has produced output, and {@code null} otherwise */ /** Returns an output buffer if the pipeline has produced output, and {@code null} otherwise */
@Nullable @Nullable
DecoderInputBuffer getOutputBuffer(); DecoderInputBuffer getOutputBuffer() throws TransformationException;
/** /**
* Releases the pipeline's output buffer. * Releases the pipeline's output buffer.
* *
* <p>Should be called when the output buffer from {@link #getOutputBuffer()} is no longer needed. * <p>Should be called when the output buffer from {@link #getOutputBuffer()} is no longer needed.
*/ */
void releaseOutputBuffer(); void releaseOutputBuffer() throws TransformationException;
/** Returns whether the pipeline has ended. */ /** Returns whether the pipeline has ended. */
boolean isEnded(); boolean isEnded();
......
...@@ -21,6 +21,7 @@ import static java.lang.annotation.ElementType.METHOD; ...@@ -21,6 +21,7 @@ import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER; import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE_USE; import static java.lang.annotation.ElementType.TYPE_USE;
import android.media.MediaCodec;
import android.os.SystemClock; import android.os.SystemClock;
import androidx.annotation.IntDef; import androidx.annotation.IntDef;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
...@@ -216,14 +217,25 @@ public final class TransformationException extends Exception { ...@@ -216,14 +217,25 @@ public final class TransformationException extends Exception {
* *
* @param cause The cause of the failure. * @param cause The cause of the failure.
* @param componentName The name of the component used, e.g. 'VideoEncoder'. * @param componentName The name of the component used, e.g. 'VideoEncoder'.
* @param format The {@link Format} used for the decoder/encoder. * @param configurationFormat The {@link Format} used for configuring the decoder/encoder.
* @param mediaCodecName The name of the {@link MediaCodec} used, if known.
* @param errorCode See {@link #errorCode}. * @param errorCode See {@link #errorCode}.
* @return The created instance. * @return The created instance.
*/ */
public static TransformationException createForCodec( public static TransformationException createForCodec(
Throwable cause, String componentName, Format format, int errorCode) { Throwable cause,
String componentName,
Format configurationFormat,
@Nullable String mediaCodecName,
int errorCode) {
return new TransformationException( return new TransformationException(
componentName + " error, format = " + format, cause, errorCode); componentName
+ " error, format = "
+ configurationFormat
+ ", mediaCodecName="
+ mediaCodecName,
cause,
errorCode);
} }
/** /**
......
...@@ -139,7 +139,8 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull; ...@@ -139,7 +139,8 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull;
protected abstract boolean ensureConfigured() throws TransformationException; protected abstract boolean ensureConfigured() throws TransformationException;
@RequiresNonNull({"samplePipeline", "#1.data"}) @RequiresNonNull({"samplePipeline", "#1.data"})
protected void maybeQueueSampleToPipeline(DecoderInputBuffer inputBuffer) { protected void maybeQueueSampleToPipeline(DecoderInputBuffer inputBuffer)
throws TransformationException {
samplePipeline.queueInputBuffer(); samplePipeline.queueInputBuffer();
} }
...@@ -147,9 +148,11 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull; ...@@ -147,9 +148,11 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull;
* Attempts to write sample pipeline output data to the muxer. * Attempts to write sample pipeline output data to the muxer.
* *
* @return Whether it may be possible to write more data immediately by calling this method again. * @return Whether it may be possible to write more data immediately by calling this method again.
* @throws Muxer.MuxerException If a muxing problem occurs.
* @throws TransformationException If a {@link SamplePipeline} problem occurs.
*/ */
@RequiresNonNull("samplePipeline") @RequiresNonNull("samplePipeline")
private boolean feedMuxerFromPipeline() throws Muxer.MuxerException { private boolean feedMuxerFromPipeline() throws Muxer.MuxerException, TransformationException {
if (!muxerWrapperTrackAdded) { if (!muxerWrapperTrackAdded) {
@Nullable Format samplePipelineOutputFormat = samplePipeline.getOutputFormat(); @Nullable Format samplePipelineOutputFormat = samplePipeline.getOutputFormat();
if (samplePipelineOutputFormat == null) { if (samplePipelineOutputFormat == null) {
...@@ -185,9 +188,10 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull; ...@@ -185,9 +188,10 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull;
* Attempts to read input data and pass the input data to the sample pipeline. * Attempts to read input data and pass the input data to the sample pipeline.
* *
* @return Whether it may be possible to read more data immediately by calling this method again. * @return Whether it may be possible to read more data immediately by calling this method again.
* @throws TransformationException If a {@link SamplePipeline} problem occurs.
*/ */
@RequiresNonNull("samplePipeline") @RequiresNonNull("samplePipeline")
private boolean feedPipelineFromInput() { private boolean feedPipelineFromInput() throws TransformationException {
@Nullable DecoderInputBuffer samplePipelineInputBuffer = samplePipeline.dequeueInputBuffer(); @Nullable DecoderInputBuffer samplePipelineInputBuffer = samplePipeline.dequeueInputBuffer();
if (samplePipelineInputBuffer == null) { if (samplePipelineInputBuffer == null) {
return false; return false;
......
...@@ -122,10 +122,14 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull; ...@@ -122,10 +122,14 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull;
/** /**
* Queues the input buffer to the sample pipeline unless it should be dropped because of slow * Queues the input buffer to the sample pipeline unless it should be dropped because of slow
* motion flattening. * motion flattening.
*
* @param inputBuffer The {@link DecoderInputBuffer}.
* @throws TransformationException If a {@link SamplePipeline} problem occurs.
*/ */
@Override @Override
@RequiresNonNull({"samplePipeline", "#1.data"}) @RequiresNonNull({"samplePipeline", "#1.data"})
protected void maybeQueueSampleToPipeline(DecoderInputBuffer inputBuffer) { protected void maybeQueueSampleToPipeline(DecoderInputBuffer inputBuffer)
throws TransformationException {
ByteBuffer data = inputBuffer.data; ByteBuffer data = inputBuffer.data;
boolean shouldDropSample = boolean shouldDropSample =
sefSlowMotionFlattener != null && sefSlowMotionFlattener.dropOrTransformSample(inputBuffer); sefSlowMotionFlattener != null && sefSlowMotionFlattener.dropOrTransformSample(inputBuffer);
......
...@@ -131,12 +131,12 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; ...@@ -131,12 +131,12 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
@Override @Override
@Nullable @Nullable
public DecoderInputBuffer dequeueInputBuffer() { public DecoderInputBuffer dequeueInputBuffer() throws TransformationException {
return decoder.maybeDequeueInputBuffer(decoderInputBuffer) ? decoderInputBuffer : null; return decoder.maybeDequeueInputBuffer(decoderInputBuffer) ? decoderInputBuffer : null;
} }
@Override @Override
public void queueInputBuffer() { public void queueInputBuffer() throws TransformationException {
decoder.queueInputBuffer(decoderInputBuffer); decoder.queueInputBuffer(decoderInputBuffer);
} }
...@@ -217,7 +217,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; ...@@ -217,7 +217,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
@Override @Override
@Nullable @Nullable
public Format getOutputFormat() { public Format getOutputFormat() throws TransformationException {
Format format = encoder.getOutputFormat(); Format format = encoder.getOutputFormat();
return format == null return format == null
? null ? null
...@@ -226,7 +226,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; ...@@ -226,7 +226,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
@Override @Override
@Nullable @Nullable
public DecoderInputBuffer getOutputBuffer() { public DecoderInputBuffer getOutputBuffer() throws TransformationException {
encoderOutputBuffer.data = encoder.getOutputBuffer(); encoderOutputBuffer.data = encoder.getOutputBuffer();
if (encoderOutputBuffer.data == null) { if (encoderOutputBuffer.data == null) {
return null; return null;
...@@ -238,7 +238,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; ...@@ -238,7 +238,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
} }
@Override @Override
public void releaseOutputBuffer() { public void releaseOutputBuffer() throws TransformationException {
encoder.releaseOutputBuffer(); encoder.releaseOutputBuffer();
} }
......
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