Move retrieveTrackFormat() method to TestUtil

TestUtil class is more appropriate for the given method.
In the next CL, the only method in FileUtil.java will be moved back
into transformer library and the FileUtil class will be removed.

PiperOrigin-RevId: 605648034
This commit is contained in:
sheenachhabra 2024-02-09 09:14:46 -08:00 committed by Copybara-Service
parent 8a758c2ed7
commit 69c555be0a
4 changed files with 38 additions and 39 deletions

View File

@ -16,19 +16,14 @@
package androidx.media3.test.utils;
import static androidx.media3.common.util.Assertions.checkState;
import static androidx.media3.test.utils.TestUtil.retrieveTrackFormat;
import android.content.Context;
import androidx.annotation.Nullable;
import androidx.media3.common.C;
import androidx.media3.common.ColorInfo;
import androidx.media3.common.Format;
import androidx.media3.common.MediaItem;
import androidx.media3.common.TrackGroup;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.exoplayer.MetadataRetriever;
import androidx.media3.exoplayer.source.TrackGroupArray;
import java.util.concurrent.ExecutionException;
/** Utilities for accessing details of media files. */
@UnstableApi
@ -38,8 +33,7 @@ public final class FileUtil {
* Returns {@link C.ColorTransfer} information from the media file, or {@link
* C#COLOR_TRANSFER_SDR} if the information can not be found.
*/
public static @C.ColorTransfer int retrieveColorTransfer(
Context context, @Nullable String filePath) {
public static @C.ColorTransfer int retrieveColorTransfer(Context context, String filePath) {
Format videoTrackFormat = retrieveTrackFormat(context, filePath, C.TRACK_TYPE_VIDEO);
@Nullable ColorInfo colorInfo = videoTrackFormat.colorInfo;
return colorInfo == null || colorInfo.colorTransfer == Format.NO_VALUE
@ -47,30 +41,5 @@ public final class FileUtil {
: colorInfo.colorTransfer;
}
/** Returns {@linkplain Format track format} from the media file. */
public static Format retrieveTrackFormat(
Context context, @Nullable String filePath, @C.TrackType int trackType) {
TrackGroupArray trackGroupArray;
try {
trackGroupArray =
MetadataRetriever.retrieveMetadata(context, MediaItem.fromUri("file://" + filePath))
.get();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IllegalStateException(e);
} catch (ExecutionException e) {
throw new IllegalStateException(e);
}
for (int i = 0; i < trackGroupArray.length; i++) {
TrackGroup trackGroup = trackGroupArray.get(i);
if (trackGroup.type == trackType) {
checkState(trackGroup.length == 1);
return trackGroup.getFormat(0);
}
}
throw new IllegalStateException("Couldn't find track");
}
private FileUtil() {}
}

View File

@ -16,6 +16,7 @@
package androidx.media3.test.utils;
import static androidx.media3.common.util.Assertions.checkNotNull;
import static androidx.media3.common.util.Assertions.checkState;
import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
@ -30,10 +31,12 @@ import android.os.Bundle;
import android.os.Parcel;
import androidx.annotation.Nullable;
import androidx.media3.common.C;
import androidx.media3.common.Format;
import androidx.media3.common.MediaItem;
import androidx.media3.common.MediaMetadata;
import androidx.media3.common.StreamKey;
import androidx.media3.common.Timeline;
import androidx.media3.common.TrackGroup;
import androidx.media3.common.util.Assertions;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.common.util.Util;
@ -42,6 +45,8 @@ import androidx.media3.database.DefaultDatabaseProvider;
import androidx.media3.datasource.DataSource;
import androidx.media3.datasource.DataSourceUtil;
import androidx.media3.datasource.DataSpec;
import androidx.media3.exoplayer.MetadataRetriever;
import androidx.media3.exoplayer.source.TrackGroupArray;
import androidx.media3.extractor.DefaultExtractorInput;
import androidx.media3.extractor.Extractor;
import androidx.media3.extractor.ExtractorInput;
@ -70,6 +75,7 @@ import java.util.Queue;
import java.util.Random;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
/** Utility methods for tests. */
@UnstableApi
@ -356,6 +362,31 @@ public class TestUtil {
return Uri.parse("asset:///" + assetPath);
}
/** Returns the {@linkplain Format track format} from the media file. */
public static Format retrieveTrackFormat(
Context context, String filePath, @C.TrackType int trackType) {
TrackGroupArray trackGroupArray;
try {
trackGroupArray =
MetadataRetriever.retrieveMetadata(context, MediaItem.fromUri("file://" + filePath))
.get();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IllegalStateException(e);
} catch (ExecutionException e) {
throw new IllegalStateException(e);
}
for (int i = 0; i < trackGroupArray.length; i++) {
TrackGroup trackGroup = trackGroupArray.get(i);
if (trackGroup.type == trackType) {
checkState(trackGroup.length == 1);
return trackGroup.getFormat(0);
}
}
throw new IllegalStateException("Couldn't find track");
}
/**
* Reads from the given input using the given {@link Extractor}, until it can produce the {@link
* SeekMap} and all of the track formats have been identified, or until the extractor encounters

View File

@ -17,7 +17,7 @@ package androidx.media3.transformer;
import static androidx.media3.common.util.Assertions.checkNotNull;
import static androidx.media3.common.util.Util.isRunningOnEmulator;
import static androidx.media3.test.utils.FileUtil.retrieveTrackFormat;
import static androidx.media3.test.utils.TestUtil.retrieveTrackFormat;
import static androidx.media3.transformer.AndroidTestUtil.JPG_ASSET_URI_STRING;
import static androidx.media3.transformer.AndroidTestUtil.MP3_ASSET_URI_STRING;
import static androidx.media3.transformer.AndroidTestUtil.MP4_ASSET_FORMAT;
@ -75,7 +75,6 @@ import androidx.media3.effect.RgbFilter;
import androidx.media3.effect.ScaleAndRotateTransformation;
import androidx.media3.effect.TimestampWrapper;
import androidx.media3.exoplayer.audio.TeeAudioProcessor;
import androidx.media3.test.utils.FileUtil;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.common.collect.ImmutableList;
@ -494,7 +493,7 @@ public class TransformerEndToEndTest {
.run(testId, editedMediaItem);
assertThat(result.exportResult.durationMs).isAtMost(clippingEndMs - clippingStartMs);
Format format = FileUtil.retrieveTrackFormat(context, result.filePath, C.TRACK_TYPE_VIDEO);
Format format = retrieveTrackFormat(context, result.filePath, C.TRACK_TYPE_VIDEO);
// The output video is portrait, but Transformer's default setup encodes videos landscape.
assertThat(format.rotationDegrees).isEqualTo(90);
}
@ -582,7 +581,7 @@ public class TransformerEndToEndTest {
assertThat(result.exportResult.optimizationResult)
.isEqualTo(OPTIMIZATION_FAILED_FORMAT_MISMATCH);
assertThat(result.exportResult.durationMs).isAtMost(clippingEndMs - clippingStartMs);
Format format = FileUtil.retrieveTrackFormat(context, result.filePath, C.TRACK_TYPE_VIDEO);
Format format = retrieveTrackFormat(context, result.filePath, C.TRACK_TYPE_VIDEO);
// The video is transcoded, so the rotation is performed in the VideoFrameProcessor.
// The output video is portrait, but Transformer's default setup encodes videos landscape.
assertThat(format.rotationDegrees).isEqualTo(0);
@ -743,7 +742,7 @@ public class TransformerEndToEndTest {
.isEqualTo(CONVERSION_PROCESS_TRANSMUXED_AND_TRANSCODED);
assertThat(result.exportResult.audioConversionProcess).isEqualTo(CONVERSION_PROCESS_NA);
Format format = FileUtil.retrieveTrackFormat(context, result.filePath, C.TRACK_TYPE_VIDEO);
Format format = retrieveTrackFormat(context, result.filePath, C.TRACK_TYPE_VIDEO);
// The video is trim-optimized, so the rotation is performed in MuxerWrapper.
// The MuxerWrapper rotation is clockwise while the ScaleAndRotateTransformation rotation
// is counterclockwise.

View File

@ -16,7 +16,7 @@
package androidx.media3.transformer;
import static androidx.media3.common.util.Assertions.checkState;
import static androidx.media3.test.utils.FileUtil.retrieveTrackFormat;
import static androidx.media3.test.utils.TestUtil.retrieveTrackFormat;
import static com.google.common.truth.Truth.assertThat;
import android.content.Context;