Commit 2edf9857 by christosts Committed by Oliver Woodman

Add DedicatedThreadAsyncMediaCodecAdapter

The DedicatedThreadAsyncMediaCodecAdapter is an
asynchronous MediaCodecAdapter that routes callback
to a separate Thread.

PiperOrigin-RevId: 285397368
parent 00eab444
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.exoplayer2.mediacodec;
import android.media.MediaCodec;
import android.media.MediaFormat;
import android.os.Handler;
import android.os.HandlerThread;
import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.annotation.VisibleForTesting;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.Util;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
/**
* A {@link MediaCodecAdapter} that operates the underlying {@link MediaCodec} in asynchronous mode
* and routes {@link MediaCodec.Callback} callbacks on a dedicated Thread that is managed
* internally.
*
* <p>After creating an instance, you need to call {@link #start()} to start the internal Thread.
*/
@RequiresApi(23)
/* package */ final class DedicatedThreadAsyncMediaCodecAdapter extends MediaCodec.Callback
implements MediaCodecAdapter {
@IntDef({State.CREATED, State.STARTED, State.SHUT_DOWN})
private @interface State {
int CREATED = 0;
int STARTED = 1;
int SHUT_DOWN = 2;
}
private final MediaCodecAsyncCallback mediaCodecAsyncCallback;
private final MediaCodec codec;
private final HandlerThread handlerThread;
@MonotonicNonNull private Handler handler;
private long pendingFlushCount;
private @State int state;
private Runnable onCodecStart;
@Nullable private IllegalStateException internalException;
/**
* Creates an instance that wraps the specified {@link MediaCodec}.
*
* @param codec The {@link MediaCodec} to wrap.
* @param trackType One of {@link C#TRACK_TYPE_AUDIO} or {@link C#TRACK_TYPE_VIDEO}. Used for
* labelling the internal Thread accordingly.
* @throws IllegalArgumentException If {@code trackType} is not one of {@link C#TRACK_TYPE_AUDIO}
* or {@link C#TRACK_TYPE_VIDEO}.
*/
/* package */ DedicatedThreadAsyncMediaCodecAdapter(MediaCodec codec, int trackType) {
this(codec, new HandlerThread(createThreadLabel(trackType)));
}
@VisibleForTesting
/* package */ DedicatedThreadAsyncMediaCodecAdapter(
MediaCodec codec, HandlerThread handlerThread) {
mediaCodecAsyncCallback = new MediaCodecAsyncCallback();
this.codec = codec;
this.handlerThread = handlerThread;
state = State.CREATED;
onCodecStart = codec::start;
}
/**
* Starts the operation of the instance.
*
* <p>After a call to this method, make sure to call {@link #shutdown()} to terminate the internal
* Thread. You can only call this method once during the lifetime of this instance; calling this
* method again will throw an {@link IllegalStateException}.
*
* @throws IllegalStateException If this method has been called already.
*/
public synchronized void start() {
Assertions.checkState(state == State.CREATED);
handlerThread.start();
handler = new Handler(handlerThread.getLooper());
codec.setCallback(this, handler);
state = State.STARTED;
}
@Override
public synchronized int dequeueInputBufferIndex() {
Assertions.checkState(state == State.STARTED);
if (isFlushing()) {
return MediaCodec.INFO_TRY_AGAIN_LATER;
} else {
maybeThrowException();
return mediaCodecAsyncCallback.dequeueInputBufferIndex();
}
}
@Override
public synchronized int dequeueOutputBufferIndex(MediaCodec.BufferInfo bufferInfo) {
Assertions.checkState(state == State.STARTED);
if (isFlushing()) {
return MediaCodec.INFO_TRY_AGAIN_LATER;
} else {
maybeThrowException();
return mediaCodecAsyncCallback.dequeueOutputBufferIndex(bufferInfo);
}
}
@Override
public synchronized MediaFormat getOutputFormat() {
Assertions.checkState(state == State.STARTED);
return mediaCodecAsyncCallback.getOutputFormat();
}
@Override
public synchronized void flush() {
Assertions.checkState(state == State.STARTED);
codec.flush();
++pendingFlushCount;
Util.castNonNull(handler).post(this::onFlushCompleted);
}
@Override
public synchronized void shutdown() {
if (state == State.STARTED) {
handlerThread.quit();
mediaCodecAsyncCallback.flush();
}
state = State.SHUT_DOWN;
}
@Override
public synchronized void onInputBufferAvailable(@NonNull MediaCodec codec, int index) {
mediaCodecAsyncCallback.onInputBufferAvailable(codec, index);
}
@Override
public synchronized void onOutputBufferAvailable(
@NonNull MediaCodec codec, int index, @NonNull MediaCodec.BufferInfo info) {
mediaCodecAsyncCallback.onOutputBufferAvailable(codec, index, info);
}
@Override
public synchronized void onError(
@NonNull MediaCodec codec, @NonNull MediaCodec.CodecException e) {
mediaCodecAsyncCallback.onError(codec, e);
}
@Override
public synchronized void onOutputFormatChanged(
@NonNull MediaCodec codec, @NonNull MediaFormat format) {
mediaCodecAsyncCallback.onOutputFormatChanged(codec, format);
}
@VisibleForTesting
/* package */ void onMediaCodecError(IllegalStateException e) {
mediaCodecAsyncCallback.onMediaCodecError(e);
}
@VisibleForTesting
/* package */ void setOnCodecStart(Runnable onCodecStart) {
this.onCodecStart = onCodecStart;
}
private synchronized void onFlushCompleted() {
if (state != State.STARTED) {
// The adapter has been shutdown.
return;
}
--pendingFlushCount;
if (pendingFlushCount > 0) {
// Another flush() has been called.
return;
} else if (pendingFlushCount < 0) {
// This should never happen.
internalException = new IllegalStateException();
return;
}
mediaCodecAsyncCallback.flush();
try {
onCodecStart.run();
} catch (IllegalStateException e) {
internalException = e;
} catch (Exception e) {
internalException = new IllegalStateException(e);
}
}
private synchronized boolean isFlushing() {
return pendingFlushCount > 0;
}
private synchronized void maybeThrowException() {
maybeThrowInternalException();
mediaCodecAsyncCallback.maybeThrowMediaCodecException();
}
private synchronized void maybeThrowInternalException() {
if (internalException != null) {
IllegalStateException e = internalException;
internalException = null;
throw e;
}
}
private static String createThreadLabel(int trackType) {
StringBuilder labelBuilder = new StringBuilder("MediaCodecAsyncAdapter:");
if (trackType == C.TRACK_TYPE_AUDIO) {
labelBuilder.append("Audio");
} else if (trackType == C.TRACK_TYPE_VIDEO) {
labelBuilder.append("Video");
} else {
labelBuilder.append("Unknown(").append(trackType).append(")");
}
return labelBuilder.toString();
}
}
......@@ -195,7 +195,8 @@ public abstract class MediaCodecRenderer extends BaseRenderer {
@Retention(RetentionPolicy.SOURCE)
@IntDef({
MediaCodecOperationMode.SYNCHRONOUS,
MediaCodecOperationMode.ASYNCHRONOUS_PLAYBACK_THREAD
MediaCodecOperationMode.ASYNCHRONOUS_PLAYBACK_THREAD,
MediaCodecOperationMode.ASYNCHRONOUS_DEDICATED_THREAD
})
public @interface MediaCodecOperationMode {
......@@ -206,6 +207,11 @@ public abstract class MediaCodecRenderer extends BaseRenderer {
* callbacks to the playback Thread.
*/
int ASYNCHRONOUS_PLAYBACK_THREAD = 1;
/**
* Operates the {@link MediaCodec} in asynchronous mode and routes {@link MediaCodec.Callback}
* callbacks to a dedicated Thread.
*/
int ASYNCHRONOUS_DEDICATED_THREAD = 2;
}
/** Indicates no codec operating rate should be set. */
......@@ -472,6 +478,11 @@ public abstract class MediaCodecRenderer extends BaseRenderer {
* routed to the Playback Thread. This mode requires API level &ge; 21; if the API level
* is &le; 20, the operation mode will be set to {@link
* MediaCodecOperationMode#SYNCHRONOUS}.
* <li>{@link MediaCodecOperationMode#ASYNCHRONOUS_DEDICATED_THREAD}: The {@link MediaCodec}
* will operate in asynchronous mode and {@link MediaCodec.Callback} callbacks will be
* routed to a dedicated Thread. This mode requires API level &ge; 23; if the API level
* is &le; 22, the operation mode will be set to {@link
* MediaCodecOperationMode#SYNCHRONOUS}.
* </ul>
* By default, the operation mode is set to {@link MediaCodecOperationMode#SYNCHRONOUS}.
*/
......@@ -943,6 +954,10 @@ public abstract class MediaCodecRenderer extends BaseRenderer {
if (mediaCodecOperationMode == MediaCodecOperationMode.ASYNCHRONOUS_PLAYBACK_THREAD
&& Util.SDK_INT >= 21) {
codecAdapter = new AsynchronousMediaCodecAdapter(codec);
} else if (mediaCodecOperationMode == MediaCodecOperationMode.ASYNCHRONOUS_DEDICATED_THREAD
&& Util.SDK_INT >= 23) {
codecAdapter = new DedicatedThreadAsyncMediaCodecAdapter(codec, getTrackType());
((DedicatedThreadAsyncMediaCodecAdapter) codecAdapter).start();
} else {
codecAdapter = new SynchronousMediaCodecAdapter(codec, getDequeueOutputBufferTimeoutUs());
}
......
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