HDR: Catch test util decoder support error.

Otherwise, a lack of HDR decoding support will result in the tests checking output files for HDR output, like HdrEditingTest.transform_noRequestedTranscode_hdr10File_transformsOrThrows, failing.

PiperOrigin-RevId: 510213020
This commit is contained in:
huangdarwin 2023-02-16 20:25:03 +00:00 committed by christosts
parent e3484d632f
commit da8b4db278
6 changed files with 56 additions and 28 deletions

View file

@ -35,6 +35,7 @@ import androidx.media3.common.MimeTypes;
import androidx.media3.common.util.MediaFormatUtil;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.common.util.Util;
import java.io.IOException;
import java.nio.ByteBuffer;
import org.checkerframework.checker.nullness.qual.Nullable;
@ -67,9 +68,10 @@ public final class DecodeOneFrameUtil {
* @param listener A {@link Listener} implementation.
* @param surface The {@link Surface} to render the decoded frame to, {@code null} if the decoded
* frame is not needed.
* @throws IOException If extractor or codec creation fails.
*/
public static void decodeOneCacheFileFrame(
String cacheFilePath, Listener listener, @Nullable Surface surface) throws Exception {
String cacheFilePath, Listener listener, @Nullable Surface surface) throws IOException {
MediaExtractor mediaExtractor = new MediaExtractor();
try {
mediaExtractor.setDataSource(cacheFilePath);
@ -109,10 +111,12 @@ public final class DecodeOneFrameUtil {
* @param listener A {@link Listener} implementation.
* @param surface The {@link Surface} to render the decoded frame to, {@code null} if the decoded
* frame is not needed.
* @throws IOException If codec creation fails.
* @throws UnsupportedOperationException If no decoder supports this file's MediaFormat.
*/
private static void decodeOneFrame(
MediaExtractor mediaExtractor, Listener listener, @Nullable Surface surface)
throws Exception {
throws IOException {
// Set up the extractor to read the first video frame and get its format.
if (surface == null) {
// Creates a placeholder surface.

View file

@ -26,18 +26,29 @@ import androidx.media3.common.ColorInfo;
import androidx.media3.common.util.MediaFormatUtil;
import androidx.media3.common.util.Util;
import androidx.media3.test.utils.DecodeOneFrameUtil;
import java.io.IOException;
/** Utilities for reading color info from a file. */
public class FileUtil {
public static void assertFileHasColorTransfer(
@Nullable String filePath, @C.ColorTransfer int expectedColorTransfer) throws Exception {
/** Utilities for accessing details of media files. */
/* package */ class FileUtil {
/**
* Assert that the file has a certain color transfer, if supported on this device.
*
* <p>This will silently pass if under API 29, or if decoding this file is not supported on this
* device.
*
* @param filePath The path of the input file.
* @param expectedColorTransfer The expected {@link C.ColorTransfer} for the input file.
* @throws IOException If extractor or codec creation fails.
*/
public static void maybeAssertFileHasColorTransfer(
@Nullable String filePath, @C.ColorTransfer int expectedColorTransfer) throws IOException {
if (Util.SDK_INT < 29) {
// Skipping on this API version due to lack of support for MediaFormat#getInteger, which is
// required for MediaFormatUtil#getColorInfo.
return;
}
DecodeOneFrameUtil.decodeOneCacheFileFrame(
checkNotNull(filePath),
DecodeOneFrameUtil.Listener listener =
new DecodeOneFrameUtil.Listener() {
@Override
public void onContainerExtracted(MediaFormat mediaFormat) {
@ -50,8 +61,19 @@ public class FileUtil {
@Nullable ColorInfo decodedColorInfo = MediaFormatUtil.getColorInfo(mediaFormat);
assertColorInfoHasTransfer(decodedColorInfo, expectedColorTransfer);
}
},
/* surface= */ null);
};
try {
DecodeOneFrameUtil.decodeOneCacheFileFrame(
checkNotNull(filePath), listener, /* surface= */ null);
} catch (UnsupportedOperationException e) {
if (e.getMessage() != null
&& e.getMessage().equals(DecodeOneFrameUtil.NO_DECODER_SUPPORT_ERROR_STRING)) {
return;
} else {
throw e;
}
}
}
private static void assertColorInfoHasTransfer(

View file

@ -19,7 +19,7 @@ import static androidx.media3.transformer.AndroidTestUtil.MP4_ASSET_1080P_4_SECO
import static androidx.media3.transformer.AndroidTestUtil.MP4_ASSET_1080P_4_SECOND_HDR10_FORMAT;
import static androidx.media3.transformer.AndroidTestUtil.MP4_ASSET_1080P_5_SECOND_HLG10;
import static androidx.media3.transformer.AndroidTestUtil.MP4_ASSET_1080P_5_SECOND_HLG10_FORMAT;
import static androidx.media3.transformer.mh.FileUtil.assertFileHasColorTransfer;
import static androidx.media3.transformer.mh.FileUtil.maybeAssertFileHasColorTransfer;
import android.content.Context;
import android.net.Uri;
@ -73,7 +73,7 @@ public class ForceInterpretHdrVideoAsSdrTest {
new TransformerAndroidTestRunner.Builder(context, transformer)
.build()
.run(testId, mediaItem);
assertFileHasColorTransfer(exportTestResult.filePath, C.COLOR_TRANSFER_SDR);
maybeAssertFileHasColorTransfer(exportTestResult.filePath, C.COLOR_TRANSFER_SDR);
Log.i(TAG, "Transformed.");
} catch (ExportException exception) {
if (exception.errorCode != ExportException.ERROR_CODE_DECODING_FORMAT_UNSUPPORTED) {
@ -109,7 +109,7 @@ public class ForceInterpretHdrVideoAsSdrTest {
new TransformerAndroidTestRunner.Builder(context, transformer)
.build()
.run(testId, mediaItem);
assertFileHasColorTransfer(exportTestResult.filePath, C.COLOR_TRANSFER_SDR);
maybeAssertFileHasColorTransfer(exportTestResult.filePath, C.COLOR_TRANSFER_SDR);
Log.i(TAG, "Transformed.");
} catch (ExportException exception) {
if (exception.errorCode != ExportException.ERROR_CODE_DECODING_FORMAT_UNSUPPORTED) {

View file

@ -21,7 +21,7 @@ import static androidx.media3.transformer.AndroidTestUtil.MP4_ASSET_1080P_1_SECO
import static androidx.media3.transformer.AndroidTestUtil.MP4_ASSET_1080P_4_SECOND_HDR10;
import static androidx.media3.transformer.AndroidTestUtil.MP4_ASSET_1080P_5_SECOND_HLG10;
import static androidx.media3.transformer.AndroidTestUtil.recordTestSkipped;
import static androidx.media3.transformer.mh.FileUtil.assertFileHasColorTransfer;
import static androidx.media3.transformer.mh.FileUtil.maybeAssertFileHasColorTransfer;
import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
@ -82,7 +82,7 @@ public class HdrEditingTest {
.build()
.run(testId, mediaItem);
Log.i(TAG, "Exported.");
assertFileHasColorTransfer(exportTestResult.filePath, C.COLOR_TRANSFER_ST2084);
maybeAssertFileHasColorTransfer(exportTestResult.filePath, C.COLOR_TRANSFER_ST2084);
} catch (ExportException exception) {
Log.i(TAG, checkNotNull(exception.getCause()).toString());
assertThat(exception).hasCauseThat().isInstanceOf(IllegalArgumentException.class);
@ -107,7 +107,7 @@ public class HdrEditingTest {
.build()
.run(testId, mediaItem);
Log.i(TAG, "Exported.");
assertFileHasColorTransfer(exportTestResult.filePath, C.COLOR_TRANSFER_HLG);
maybeAssertFileHasColorTransfer(exportTestResult.filePath, C.COLOR_TRANSFER_HLG);
} catch (ExportException exception) {
Log.i(TAG, checkNotNull(exception.getCause()).toString());
assertThat(exception).hasCauseThat().isInstanceOf(IllegalArgumentException.class);
@ -139,7 +139,7 @@ public class HdrEditingTest {
new TransformerAndroidTestRunner.Builder(context, transformer)
.build()
.run(testId, editedMediaItem);
assertFileHasColorTransfer(exportTestResult.filePath, C.COLOR_TRANSFER_ST2084);
maybeAssertFileHasColorTransfer(exportTestResult.filePath, C.COLOR_TRANSFER_ST2084);
}
@Test
@ -163,7 +163,7 @@ public class HdrEditingTest {
new TransformerAndroidTestRunner.Builder(context, transformer)
.build()
.run(testId, editedMediaItem);
assertFileHasColorTransfer(exportTestResult.filePath, C.COLOR_TRANSFER_HLG);
maybeAssertFileHasColorTransfer(exportTestResult.filePath, C.COLOR_TRANSFER_HLG);
}
@Test
@ -210,7 +210,7 @@ public class HdrEditingTest {
.run(testId, editedMediaItem);
Log.i(TAG, "Tone mapped.");
assertThat(isToneMappingFallbackApplied.get()).isTrue();
assertFileHasColorTransfer(exportTestResult.filePath, C.COLOR_TRANSFER_SDR);
maybeAssertFileHasColorTransfer(exportTestResult.filePath, C.COLOR_TRANSFER_SDR);
} catch (ExportException exception) {
Log.i(TAG, checkNotNull(exception.getCause()).toString());
assertThat(exception).hasCauseThat().isInstanceOf(IllegalArgumentException.class);
@ -264,7 +264,7 @@ public class HdrEditingTest {
.run(testId, editedMediaItem);
Log.i(TAG, "Tone mapped.");
assertThat(isToneMappingFallbackApplied.get()).isTrue();
assertFileHasColorTransfer(exportTestResult.filePath, C.COLOR_TRANSFER_SDR);
maybeAssertFileHasColorTransfer(exportTestResult.filePath, C.COLOR_TRANSFER_SDR);
} catch (ExportException exception) {
Log.i(TAG, checkNotNull(exception.getCause()).toString());
assertThat(exception).hasCauseThat().isInstanceOf(IllegalArgumentException.class);

View file

@ -18,7 +18,7 @@ package androidx.media3.transformer.mh;
import static androidx.media3.common.util.Assertions.checkNotNull;
import static androidx.media3.transformer.AndroidTestUtil.MP4_ASSET_1080P_4_SECOND_HDR10;
import static androidx.media3.transformer.AndroidTestUtil.MP4_ASSET_1080P_5_SECOND_HLG10;
import static androidx.media3.transformer.mh.FileUtil.assertFileHasColorTransfer;
import static androidx.media3.transformer.mh.FileUtil.maybeAssertFileHasColorTransfer;
import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
@ -83,7 +83,7 @@ public class ToneMapHdrToSdrUsingMediaCodecTest {
.build()
.run(testId, mediaItem);
Log.i(TAG, "Tone mapped.");
assertFileHasColorTransfer(exportTestResult.filePath, C.COLOR_TRANSFER_SDR);
maybeAssertFileHasColorTransfer(exportTestResult.filePath, C.COLOR_TRANSFER_SDR);
} catch (ExportException exception) {
Log.i(TAG, checkNotNull(exception.getCause()).toString());
assertThat(exception).hasCauseThat().isInstanceOf(IllegalArgumentException.class);
@ -125,7 +125,7 @@ public class ToneMapHdrToSdrUsingMediaCodecTest {
.build()
.run(testId, mediaItem);
Log.i(TAG, "Tone mapped.");
assertFileHasColorTransfer(exportTestResult.filePath, C.COLOR_TRANSFER_SDR);
maybeAssertFileHasColorTransfer(exportTestResult.filePath, C.COLOR_TRANSFER_SDR);
} catch (ExportException exception) {
Log.i(TAG, checkNotNull(exception.getCause()).toString());
assertThat(exception).hasCauseThat().isInstanceOf(IllegalArgumentException.class);
@ -172,7 +172,7 @@ public class ToneMapHdrToSdrUsingMediaCodecTest {
.build()
.run(testId, editedMediaItem);
Log.i(TAG, "Tone mapped.");
assertFileHasColorTransfer(exportTestResult.filePath, C.COLOR_TRANSFER_SDR);
maybeAssertFileHasColorTransfer(exportTestResult.filePath, C.COLOR_TRANSFER_SDR);
} catch (ExportException exception) {
Log.i(TAG, checkNotNull(exception.getCause()).toString());
assertThat(exception).hasCauseThat().isInstanceOf(IllegalArgumentException.class);
@ -219,7 +219,7 @@ public class ToneMapHdrToSdrUsingMediaCodecTest {
.build()
.run(testId, editedMediaItem);
Log.i(TAG, "Tone mapped.");
assertFileHasColorTransfer(exportTestResult.filePath, C.COLOR_TRANSFER_SDR);
maybeAssertFileHasColorTransfer(exportTestResult.filePath, C.COLOR_TRANSFER_SDR);
} catch (ExportException exception) {
Log.i(TAG, checkNotNull(exception.getCause()).toString());
assertThat(exception).hasCauseThat().isInstanceOf(IllegalArgumentException.class);

View file

@ -20,7 +20,7 @@ import static androidx.media3.transformer.AndroidTestUtil.MP4_ASSET_1080P_4_SECO
import static androidx.media3.transformer.AndroidTestUtil.MP4_ASSET_1080P_5_SECOND_HLG10;
import static androidx.media3.transformer.AndroidTestUtil.MP4_ASSET_1080P_5_SECOND_HLG10_FORMAT;
import static androidx.media3.transformer.AndroidTestUtil.recordTestSkipped;
import static androidx.media3.transformer.mh.FileUtil.assertFileHasColorTransfer;
import static androidx.media3.transformer.mh.FileUtil.maybeAssertFileHasColorTransfer;
import static androidx.test.core.app.ApplicationProvider.getApplicationContext;
import android.content.Context;
@ -90,7 +90,8 @@ public class ToneMapHdrToSdrUsingOpenGlTest {
new TransformerAndroidTestRunner.Builder(context, transformer)
.build()
.run(testId, mediaItem);
assertFileHasColorTransfer(exportTestResult.filePath, C.COLOR_TRANSFER_SDR);
Log.i(TAG, "Tone mapped.");
maybeAssertFileHasColorTransfer(exportTestResult.filePath, C.COLOR_TRANSFER_SDR);
} catch (ExportException exception) {
Log.e(TAG, "Error during export.", exception);
if (exception.errorCode != ExportException.ERROR_CODE_DECODING_FORMAT_UNSUPPORTED) {
@ -140,7 +141,8 @@ public class ToneMapHdrToSdrUsingOpenGlTest {
new TransformerAndroidTestRunner.Builder(context, transformer)
.build()
.run(testId, mediaItem);
assertFileHasColorTransfer(exportTestResult.filePath, C.COLOR_TRANSFER_SDR);
Log.i(TAG, "Tone mapped.");
maybeAssertFileHasColorTransfer(exportTestResult.filePath, C.COLOR_TRANSFER_SDR);
} catch (ExportException exception) {
Log.e(TAG, "Error during export.", exception);
if (exception.errorCode != ExportException.ERROR_CODE_DECODING_FORMAT_UNSUPPORTED) {