Commit 421aa9df by claincly Committed by Ian Baker

Added a test-only method to clear the clearDecoderInfosCache.

The cache, being static, is updated every time a new MimeType is encountered.
The static cache needs to be cleared between tests that register codecs through
ShadowMediaCodec, or the subsequent tests could possibly pick up a wrong codec.

PiperOrigin-RevId: 351576018
parent bdc2a4e3
...@@ -24,6 +24,7 @@ import android.media.MediaCodecList; ...@@ -24,6 +24,7 @@ import android.media.MediaCodecList;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Pair; import android.util.Pair;
import androidx.annotation.CheckResult; import androidx.annotation.CheckResult;
import androidx.annotation.GuardedBy;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi; import androidx.annotation.RequiresApi;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
...@@ -63,6 +64,7 @@ public final class MediaCodecUtil { ...@@ -63,6 +64,7 @@ public final class MediaCodecUtil {
private static final String TAG = "MediaCodecUtil"; private static final String TAG = "MediaCodecUtil";
private static final Pattern PROFILE_PATTERN = Pattern.compile("^\\D?(\\d+)$"); private static final Pattern PROFILE_PATTERN = Pattern.compile("^\\D?(\\d+)$");
@GuardedBy("MediaCodecUtil.class")
private static final HashMap<CodecKey, List<MediaCodecInfo>> decoderInfosCache = new HashMap<>(); private static final HashMap<CodecKey, List<MediaCodecInfo>> decoderInfosCache = new HashMap<>();
// Codecs to constant mappings. // Codecs to constant mappings.
...@@ -106,6 +108,15 @@ public final class MediaCodecUtil { ...@@ -106,6 +108,15 @@ public final class MediaCodecUtil {
} }
/** /**
* Clears the codec cache.
*
* <p>This method should only be called in tests.
*/
public static synchronized void clearDecoderInfoCache() {
decoderInfosCache.clear();
}
/**
* Returns information about a decoder that will only decrypt data, without decoding it. * Returns information about a decoder that will only decrypt data, without decoding it.
* *
* @return A {@link MediaCodecInfo} describing the decoder, or null if no suitable decoder exists. * @return A {@link MediaCodecInfo} describing the decoder, or null if no suitable decoder exists.
...@@ -134,7 +145,7 @@ public final class MediaCodecUtil { ...@@ -134,7 +145,7 @@ public final class MediaCodecUtil {
return decoderInfos.isEmpty() ? null : decoderInfos.get(0); return decoderInfos.isEmpty() ? null : decoderInfos.get(0);
} }
/** /*
* Returns all {@link MediaCodecInfo}s for the given mime type, in the order given by {@link * Returns all {@link MediaCodecInfo}s for the given mime type, in the order given by {@link
* MediaCodecList}. * MediaCodecList}.
* *
......
...@@ -26,6 +26,7 @@ import com.google.android.exoplayer2.Format; ...@@ -26,6 +26,7 @@ import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.MediaItem; import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.Player; import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.SimpleExoPlayer; import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.mediacodec.MediaCodecUtil;
import com.google.android.exoplayer2.robolectric.RandomizedMp3Decoder; import com.google.android.exoplayer2.robolectric.RandomizedMp3Decoder;
import com.google.android.exoplayer2.robolectric.TestPlayerRunHelper; import com.google.android.exoplayer2.robolectric.TestPlayerRunHelper;
import com.google.android.exoplayer2.testutil.AutoAdvancingFakeClock; import com.google.android.exoplayer2.testutil.AutoAdvancingFakeClock;
...@@ -34,6 +35,7 @@ import com.google.common.collect.ImmutableList; ...@@ -34,6 +35,7 @@ import com.google.common.collect.ImmutableList;
import com.google.common.primitives.Bytes; import com.google.common.primitives.Bytes;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.util.Arrays; import java.util.Arrays;
import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
...@@ -77,6 +79,13 @@ public class EndToEndGaplessTest { ...@@ -77,6 +79,13 @@ public class EndToEndGaplessTest {
.build()); .build());
} }
@After
public void cleanUp() {
MediaCodecUtil.clearDecoderInfoCache();
ShadowMediaCodecList.reset();
ShadowMediaCodec.clearCodecs();
}
@Test @Test
public void testPlayback_twoIdenticalMp3Files() throws Exception { public void testPlayback_twoIdenticalMp3Files() throws Exception {
SimpleExoPlayer player = SimpleExoPlayer player =
......
...@@ -17,6 +17,7 @@ package com.google.android.exoplayer2.robolectric; ...@@ -17,6 +17,7 @@ package com.google.android.exoplayer2.robolectric;
import android.media.MediaCodecInfo; import android.media.MediaCodecInfo;
import android.media.MediaFormat; import android.media.MediaFormat;
import com.google.android.exoplayer2.mediacodec.MediaCodecUtil;
import com.google.android.exoplayer2.util.MimeTypes; import com.google.android.exoplayer2.util.MimeTypes;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.primitives.Ints; import com.google.common.primitives.Ints;
...@@ -81,6 +82,7 @@ public final class ShadowMediaCodecConfig extends ExternalResource { ...@@ -81,6 +82,7 @@ public final class ShadowMediaCodecConfig extends ExternalResource {
@Override @Override
protected void after() { protected void after() {
MediaCodecUtil.clearDecoderInfoCache();
ShadowMediaCodecList.reset(); ShadowMediaCodecList.reset();
ShadowMediaCodec.clearCodecs(); ShadowMediaCodec.clearCodecs();
} }
......
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