Commit fd4cfcdd by christosts Committed by bachinger

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
parent 29eeff9f
...@@ -114,16 +114,11 @@ import java.nio.ByteBuffer; ...@@ -114,16 +114,11 @@ import java.nio.ByteBuffer;
forceQueueingSynchronizationWorkaround, forceQueueingSynchronizationWorkaround,
synchronizeCodecInteractionsWithQueueing); synchronizeCodecInteractionsWithQueueing);
TraceUtil.endSection(); TraceUtil.endSection();
TraceUtil.beginSection("configureCodec"); codecAdapter.initialize(
codecAdapter.configure(
configuration.mediaFormat, configuration.mediaFormat,
configuration.surface, configuration.surface,
configuration.crypto, configuration.crypto,
configuration.flags); configuration.flags);
TraceUtil.endSection();
TraceUtil.beginSection("startCodec");
codecAdapter.start();
TraceUtil.endSection();
return codecAdapter; return codecAdapter;
} catch (Exception e) { } catch (Exception e) {
if (codecAdapter != null) { if (codecAdapter != null) {
...@@ -138,13 +133,12 @@ import java.nio.ByteBuffer; ...@@ -138,13 +133,12 @@ import java.nio.ByteBuffer;
@Documented @Documented
@Retention(RetentionPolicy.SOURCE) @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 @interface State {}
private static final int STATE_CREATED = 0; private static final int STATE_CREATED = 0;
private static final int STATE_CONFIGURED = 1; private static final int STATE_INITIALIZED = 1;
private static final int STATE_STARTED = 2; private static final int STATE_SHUT_DOWN = 2;
private static final int STATE_SHUT_DOWN = 3;
private final MediaCodec codec; private final MediaCodec codec;
private final AsynchronousMediaCodecCallback asynchronousMediaCodecCallback; private final AsynchronousMediaCodecCallback asynchronousMediaCodecCallback;
...@@ -168,20 +162,20 @@ import java.nio.ByteBuffer; ...@@ -168,20 +162,20 @@ import java.nio.ByteBuffer;
this.state = STATE_CREATED; this.state = STATE_CREATED;
} }
private void configure( private void initialize(
@Nullable MediaFormat mediaFormat, @Nullable MediaFormat mediaFormat,
@Nullable Surface surface, @Nullable Surface surface,
@Nullable MediaCrypto crypto, @Nullable MediaCrypto crypto,
int flags) { int flags) {
asynchronousMediaCodecCallback.initialize(codec); asynchronousMediaCodecCallback.initialize(codec);
TraceUtil.beginSection("configureCodec");
codec.configure(mediaFormat, surface, crypto, flags); codec.configure(mediaFormat, surface, crypto, flags);
state = STATE_CONFIGURED; TraceUtil.endSection();
}
private void start() {
bufferEnqueuer.start(); bufferEnqueuer.start();
TraceUtil.beginSection("startCodec");
codec.start(); codec.start();
state = STATE_STARTED; TraceUtil.endSection();
state = STATE_INITIALIZED;
} }
@Override @Override
...@@ -253,10 +247,8 @@ import java.nio.ByteBuffer; ...@@ -253,10 +247,8 @@ import java.nio.ByteBuffer;
@Override @Override
public void release() { public void release() {
try { try {
if (state == STATE_STARTED) { if (state == STATE_INITIALIZED) {
bufferEnqueuer.shutdown(); bufferEnqueuer.shutdown();
}
if (state == STATE_CONFIGURED || state == STATE_STARTED) {
asynchronousMediaCodecCallback.shutdown(); asynchronousMediaCodecCallback.shutdown();
} }
state = STATE_SHUT_DOWN; state = STATE_SHUT_DOWN;
......
...@@ -30,9 +30,11 @@ import org.junit.After; ...@@ -30,9 +30,11 @@ 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;
import org.robolectric.annotation.internal.DoNotInstrument;
/** Unit tests for {@link AsynchronousMediaCodecAdapter}. */ /** Unit tests for {@link AsynchronousMediaCodecAdapter}. */
@RunWith(AndroidJUnit4.class) @RunWith(AndroidJUnit4.class)
@DoNotInstrument
public class AsynchronousMediaCodecAdapterTest { public class AsynchronousMediaCodecAdapterTest {
private AsynchronousMediaCodecAdapter adapter; private AsynchronousMediaCodecAdapter adapter;
private HandlerThread callbackThread; private HandlerThread callbackThread;
...@@ -60,8 +62,8 @@ public class AsynchronousMediaCodecAdapterTest { ...@@ -60,8 +62,8 @@ public class AsynchronousMediaCodecAdapterTest {
/* synchronizeCodecInteractionsWithQueueing= */ false) /* synchronizeCodecInteractionsWithQueueing= */ false)
.createAdapter(configuration); .createAdapter(configuration);
bufferInfo = new MediaCodec.BufferInfo(); bufferInfo = new MediaCodec.BufferInfo();
// After start(), the ShadowMediaCodec offers input buffer 0. We advance the looper to make sure // After starting the MediaCodec, the ShadowMediaCodec offers input buffer 0. We advance the
// and messages have been propagated to the adapter. // looper to make sure any messages have been propagated to the adapter.
shadowOf(callbackThread.getLooper()).idle(); shadowOf(callbackThread.getLooper()).idle();
} }
......
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