AsynchronousMediaCodecAdapter cleanup

After refactoring MediaCodecAdapter.Factory to create configured and
started MediaCodecAdapters in a single operation, the
AsynchronousMediaCodecAdapter does not need to have separate methods to
configure and start, so they are merged. The CONFIGURED state is
removed.

PiperOrigin-RevId: 377519117
This commit is contained in:
christosts 2021-06-04 16:42:43 +01:00 committed by bachinger
parent 29eeff9ff7
commit fd4cfcdd67
2 changed files with 15 additions and 21 deletions

View file

@ -114,16 +114,11 @@ import java.nio.ByteBuffer;
forceQueueingSynchronizationWorkaround,
synchronizeCodecInteractionsWithQueueing);
TraceUtil.endSection();
TraceUtil.beginSection("configureCodec");
codecAdapter.configure(
codecAdapter.initialize(
configuration.mediaFormat,
configuration.surface,
configuration.crypto,
configuration.flags);
TraceUtil.endSection();
TraceUtil.beginSection("startCodec");
codecAdapter.start();
TraceUtil.endSection();
return codecAdapter;
} catch (Exception e) {
if (codecAdapter != null) {
@ -138,13 +133,12 @@ import java.nio.ByteBuffer;
@Documented
@Retention(RetentionPolicy.SOURCE)
@IntDef({STATE_CREATED, STATE_CONFIGURED, STATE_STARTED, STATE_SHUT_DOWN})
@IntDef({STATE_CREATED, STATE_INITIALIZED, STATE_SHUT_DOWN})
private @interface State {}
private static final int STATE_CREATED = 0;
private static final int STATE_CONFIGURED = 1;
private static final int STATE_STARTED = 2;
private static final int STATE_SHUT_DOWN = 3;
private static final int STATE_INITIALIZED = 1;
private static final int STATE_SHUT_DOWN = 2;
private final MediaCodec codec;
private final AsynchronousMediaCodecCallback asynchronousMediaCodecCallback;
@ -168,20 +162,20 @@ import java.nio.ByteBuffer;
this.state = STATE_CREATED;
}
private void configure(
private void initialize(
@Nullable MediaFormat mediaFormat,
@Nullable Surface surface,
@Nullable MediaCrypto crypto,
int flags) {
asynchronousMediaCodecCallback.initialize(codec);
TraceUtil.beginSection("configureCodec");
codec.configure(mediaFormat, surface, crypto, flags);
state = STATE_CONFIGURED;
}
private void start() {
TraceUtil.endSection();
bufferEnqueuer.start();
TraceUtil.beginSection("startCodec");
codec.start();
state = STATE_STARTED;
TraceUtil.endSection();
state = STATE_INITIALIZED;
}
@Override
@ -253,10 +247,8 @@ import java.nio.ByteBuffer;
@Override
public void release() {
try {
if (state == STATE_STARTED) {
if (state == STATE_INITIALIZED) {
bufferEnqueuer.shutdown();
}
if (state == STATE_CONFIGURED || state == STATE_STARTED) {
asynchronousMediaCodecCallback.shutdown();
}
state = STATE_SHUT_DOWN;

View file

@ -30,9 +30,11 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.internal.DoNotInstrument;
/** Unit tests for {@link AsynchronousMediaCodecAdapter}. */
@RunWith(AndroidJUnit4.class)
@DoNotInstrument
public class AsynchronousMediaCodecAdapterTest {
private AsynchronousMediaCodecAdapter adapter;
private HandlerThread callbackThread;
@ -60,8 +62,8 @@ public class AsynchronousMediaCodecAdapterTest {
/* synchronizeCodecInteractionsWithQueueing= */ false)
.createAdapter(configuration);
bufferInfo = new MediaCodec.BufferInfo();
// After start(), the ShadowMediaCodec offers input buffer 0. We advance the looper to make sure
// and messages have been propagated to the adapter.
// After starting the MediaCodec, the ShadowMediaCodec offers input buffer 0. We advance the
// looper to make sure any messages have been propagated to the adapter.
shadowOf(callbackThread.getLooper()).idle();
}