Skip to content

Commit

Permalink
Tidy color info checking tests
Browse files Browse the repository at this point in the history
ExoPlayer extractors (backing `MetadataRetriever`) now parse the color format
from the bitstream so using `MetadataRetriever` should be an equivalent but
more lightweight way to verify the color info.

Also remove try/catch blocks in test code calling into these methods, and add
skipping based on decoder capabilities in the cases where it was missing.

PiperOrigin-RevId: 537789483
  • Loading branch information
andrewlewis authored and tof-tof committed Jun 5, 2023
1 parent 15b509b commit 88db011
Show file tree
Hide file tree
Showing 5 changed files with 282 additions and 164 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,70 +16,57 @@

package com.google.android.exoplayer2.transformer.mh;

import static com.google.android.exoplayer2.util.Assertions.checkNotNull;
import static com.google.common.truth.Truth.assertThat;

import android.media.MediaFormat;
import android.content.Context;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.testutil.DecodeOneFrameUtil;
import com.google.android.exoplayer2.util.MediaFormatUtil;
import com.google.android.exoplayer2.util.Util;
import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.MetadataRetriever;
import com.google.android.exoplayer2.source.TrackGroup;
import com.google.android.exoplayer2.source.TrackGroupArray;
import com.google.android.exoplayer2.video.ColorInfo;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

/** 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 24, or if decoding this file is not supported on this
* device.
* Asserts that the file has a certain color transfer.
*
* @param context The current context.
* @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 < 24) {
// MediaFormat#KEY_COLOR_TRANSFER unsupported before API 24.
return;
public static void assertFileHasColorTransfer(
Context context, @Nullable String filePath, @C.ColorTransfer int expectedColorTransfer) {
TrackGroupArray trackGroupArray;
try {
trackGroupArray =
MetadataRetriever.retrieveMetadata(context, MediaItem.fromUri("file://" + filePath))
.get();
} catch (ExecutionException | InterruptedException e) {
throw new IllegalStateException(e);
}
DecodeOneFrameUtil.Listener listener =
new DecodeOneFrameUtil.Listener() {
@Override
public void onContainerExtracted(MediaFormat mediaFormat) {
@Nullable ColorInfo extractedColorInfo = MediaFormatUtil.getColorInfo(mediaFormat);
assertColorInfoHasTransfer(extractedColorInfo, expectedColorTransfer);
}

@Override
public void onFrameDecoded(MediaFormat mediaFormat) {
@Nullable ColorInfo decodedColorInfo = MediaFormatUtil.getColorInfo(mediaFormat);
assertColorInfoHasTransfer(decodedColorInfo, expectedColorTransfer);
}
};

try {
DecodeOneFrameUtil.decodeOneCacheFileFrame(
checkNotNull(filePath), listener, /* surface= */ null);
} catch (UnsupportedOperationException e) {
if (e.getMessage() != null
&& e.getMessage().equals(DecodeOneFrameUtil.NO_DECODER_SUPPORT_ERROR_STRING)) {
int trackGroupCount = trackGroupArray.length;
assertThat(trackGroupCount).isEqualTo(2);
for (int i = 0; i < trackGroupCount; i++) {
TrackGroup trackGroup = trackGroupArray.get(i);
if (trackGroup.type == C.TRACK_TYPE_VIDEO) {
assertThat(trackGroup.length).isEqualTo(1);
@Nullable ColorInfo colorInfo = trackGroup.getFormat(0).colorInfo;
@C.ColorTransfer
int actualColorTransfer =
colorInfo == null || colorInfo.colorTransfer == Format.NO_VALUE
? C.COLOR_TRANSFER_SDR
: colorInfo.colorTransfer;
assertThat(actualColorTransfer).isEqualTo(expectedColorTransfer);
return;
} else {
throw e;
}
}
}

private static void assertColorInfoHasTransfer(
@Nullable ColorInfo colorInfo, @C.ColorTransfer int expectedColorTransfer) {
@C.ColorTransfer
int actualColorTransfer = colorInfo == null ? C.COLOR_TRANSFER_SDR : colorInfo.colorTransfer;
assertThat(actualColorTransfer).isEqualTo(expectedColorTransfer);
throw new IllegalStateException("Couldn't find video track");
}

private FileUtil() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import static com.google.android.exoplayer2.transformer.AndroidTestUtil.MP4_ASSET_720P_4_SECOND_HDR10_FORMAT;
import static com.google.android.exoplayer2.transformer.AndroidTestUtil.recordTestSkipped;
import static com.google.android.exoplayer2.transformer.AndroidTestUtil.skipAndLogIfFormatsUnsupported;
import static com.google.android.exoplayer2.transformer.mh.FileUtil.maybeAssertFileHasColorTransfer;
import static com.google.android.exoplayer2.transformer.mh.FileUtil.assertFileHasColorTransfer;
import static com.google.android.exoplayer2.util.Util.SDK_INT;

import android.content.Context;
Expand All @@ -46,7 +46,6 @@
*/
@RunWith(AndroidJUnit4.class)
public class ForceInterpretHdrVideoAsSdrTest {
public static final String TAG = "ForceInterpretHdrVideoAsSdrTest";

@Test
public void forceInterpretHdrVideoAsSdrTest_hdr10File_transformsOrThrows() throws Exception {
Expand Down Expand Up @@ -85,7 +84,7 @@ public void forceInterpretHdrVideoAsSdrTest_hdr10File_transformsOrThrows() throw
.build()
.run(testId, mediaItem);

maybeAssertFileHasColorTransfer(exportTestResult.filePath, C.COLOR_TRANSFER_SDR);
assertFileHasColorTransfer(context, exportTestResult.filePath, C.COLOR_TRANSFER_SDR);
}

@Test
Expand Down Expand Up @@ -125,6 +124,6 @@ public void forceInterpretHdrVideoAsSdrTest_hlg10File_transformsOrThrows() throw
.build()
.run(testId, mediaItem);

maybeAssertFileHasColorTransfer(exportTestResult.filePath, C.COLOR_TRANSFER_SDR);
assertFileHasColorTransfer(context, exportTestResult.filePath, C.COLOR_TRANSFER_SDR);
}
}
Loading

0 comments on commit 88db011

Please sign in to comment.