Skip to content

Commit

Permalink
Extract vpx code used for GL rendering to common classes
Browse files Browse the repository at this point in the history
This will be used by both vp9 and av1 Exoplayer extensions.

PiperOrigin-RevId: 271568429
  • Loading branch information
sofijajvc authored and ojw28 committed Oct 2, 2019
1 parent d632cb8 commit 22c3be7
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.google.android.exoplayer2.source.ProgressiveMediaSource;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.util.Log;
import com.google.android.exoplayer2.video.VideoDecoderSurfaceView;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -122,7 +123,7 @@ public void run() {
player
.createMessage(videoRenderer)
.setType(LibvpxVideoRenderer.MSG_SET_OUTPUT_BUFFER_RENDERER)
.setPayload(new VpxVideoSurfaceView(context))
.setPayload(new VideoDecoderSurfaceView(context))
.send();
player.prepare(mediaSource);
player.setPlayWhenReady(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.google.android.exoplayer2.video.VideoDecoderException;
import com.google.android.exoplayer2.video.VideoDecoderInputBuffer;
import com.google.android.exoplayer2.video.VideoDecoderOutputBuffer;
import com.google.android.exoplayer2.video.VideoDecoderOutputBufferRenderer;
import com.google.android.exoplayer2.video.VideoFrameMetadataListener;
import com.google.android.exoplayer2.video.VideoRendererEventListener;

Expand All @@ -48,16 +49,16 @@
* <li>Message with type {@link C#MSG_SET_SURFACE} to set the output surface. The message payload
* should be the target {@link Surface}, or null.
* <li>Message with type {@link #MSG_SET_OUTPUT_BUFFER_RENDERER} to set the output buffer
* renderer. The message payload should be the target {@link VpxOutputBufferRenderer}, or
* null.
* renderer. The message payload should be the target {@link
* VideoDecoderOutputBufferRenderer}, or null.
* </ul>
*/
public class LibvpxVideoRenderer extends SimpleDecoderVideoRenderer {

/**
* The type of a message that can be passed to an instance of this class via {@link
* ExoPlayer#createMessage(Target)}. The message payload should be the target {@link
* VpxOutputBufferRenderer}, or null.
* VideoDecoderOutputBufferRenderer}, or null.
*/
public static final int MSG_SET_OUTPUT_BUFFER_RENDERER = C.MSG_CUSTOM_BASE;

Expand All @@ -79,11 +80,11 @@ public class LibvpxVideoRenderer extends SimpleDecoderVideoRenderer {
private final int threads;

private Surface surface;
private VpxOutputBufferRenderer outputBufferRenderer;
private VideoDecoderOutputBufferRenderer outputBufferRenderer;
@C.VideoOutputMode private int outputMode;

private VpxDecoder decoder;
private VpxOutputBuffer outputBuffer;
private VideoDecoderOutputBuffer outputBuffer;

private VideoFrameMetadataListener frameMetadataListener;

Expand Down Expand Up @@ -298,7 +299,7 @@ public void handleMessage(int messageType, @Nullable Object message) throws ExoP
if (messageType == C.MSG_SET_SURFACE) {
setOutput((Surface) message, null);
} else if (messageType == MSG_SET_OUTPUT_BUFFER_RENDERER) {
setOutput(null, (VpxOutputBufferRenderer) message);
setOutput(null, (VideoDecoderOutputBufferRenderer) message);
} else if (messageType == C.MSG_SET_VIDEO_FRAME_METADATA_LISTENER) {
frameMetadataListener = (VideoFrameMetadataListener) message;
} else {
Expand All @@ -309,7 +310,7 @@ public void handleMessage(int messageType, @Nullable Object message) throws ExoP
// Internal methods.

private void setOutput(
@Nullable Surface surface, @Nullable VpxOutputBufferRenderer outputBufferRenderer) {
@Nullable Surface surface, @Nullable VideoDecoderOutputBufferRenderer outputBufferRenderer) {
// At most one output may be non-null. Both may be null if the output is being cleared.
Assertions.checkState(surface == null || outputBufferRenderer == null);
if (this.surface != surface || this.outputBufferRenderer != outputBufferRenderer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@
import com.google.android.exoplayer2.drm.ExoMediaCrypto;
import com.google.android.exoplayer2.util.Util;
import com.google.android.exoplayer2.video.VideoDecoderInputBuffer;
import com.google.android.exoplayer2.video.VideoDecoderOutputBuffer;
import java.nio.ByteBuffer;

/** Vpx decoder. */
/* package */ final class VpxDecoder
extends SimpleDecoder<VideoDecoderInputBuffer, VpxOutputBuffer, VpxDecoderException> {
extends SimpleDecoder<VideoDecoderInputBuffer, VideoDecoderOutputBuffer, VpxDecoderException> {

// These constants should match the codes returned from vpxDecode and vpxSecureDecode functions in
// https://github.com/google/ExoPlayer/blob/release-v2/extensions/vp9/src/main/jni/vpx_jni.cc.
Expand Down Expand Up @@ -63,7 +64,9 @@ public VpxDecoder(
boolean enableRowMultiThreadMode,
int threads)
throws VpxDecoderException {
super(new VideoDecoderInputBuffer[numInputBuffers], new VpxOutputBuffer[numOutputBuffers]);
super(
new VideoDecoderInputBuffer[numInputBuffers],
new VideoDecoderOutputBuffer[numOutputBuffers]);
if (!VpxLibrary.isAvailable()) {
throw new VpxDecoderException("Failed to load decoder native libraries.");
}
Expand Down Expand Up @@ -98,12 +101,12 @@ protected VideoDecoderInputBuffer createInputBuffer() {
}

@Override
protected VpxOutputBuffer createOutputBuffer() {
return new VpxOutputBuffer(this);
protected VideoDecoderOutputBuffer createOutputBuffer() {
return new VideoDecoderOutputBuffer(this::releaseOutputBuffer);
}

@Override
protected void releaseOutputBuffer(VpxOutputBuffer buffer) {
protected void releaseOutputBuffer(VideoDecoderOutputBuffer buffer) {
// Decode only frames do not acquire a reference on the internal decoder buffer and thus do not
// require a call to vpxReleaseFrame.
if (outputMode == C.VIDEO_OUTPUT_MODE_SURFACE_YUV && !buffer.isDecodeOnly()) {
Expand All @@ -120,7 +123,7 @@ protected VpxDecoderException createUnexpectedDecodeException(Throwable error) {
@Override
@Nullable
protected VpxDecoderException decode(
VideoDecoderInputBuffer inputBuffer, VpxOutputBuffer outputBuffer, boolean reset) {
VideoDecoderInputBuffer inputBuffer, VideoDecoderOutputBuffer outputBuffer, boolean reset) {
ByteBuffer inputData = Util.castNonNull(inputBuffer.data);
int inputSize = inputData.limit();
CryptoInfo cryptoInfo = inputBuffer.cryptoInfo;
Expand Down Expand Up @@ -163,7 +166,7 @@ public void release() {
}

/** Renders the outputBuffer to the surface. Used with OUTPUT_MODE_SURFACE_YUV only. */
public void renderToSurface(VpxOutputBuffer outputBuffer, Surface surface)
public void renderToSurface(VideoDecoderOutputBuffer outputBuffer, Surface surface)
throws VpxDecoderException {
int getFrameResult = vpxRenderFrame(vpxDecContext, surface, outputBuffer);
if (getFrameResult == -1) {
Expand All @@ -189,19 +192,20 @@ private native long vpxSecureDecode(
int[] numBytesOfClearData,
int[] numBytesOfEncryptedData);

private native int vpxGetFrame(long context, VpxOutputBuffer outputBuffer);
private native int vpxGetFrame(long context, VideoDecoderOutputBuffer outputBuffer);

/**
* Renders the frame to the surface. Used with OUTPUT_MODE_SURFACE_YUV only. Must only be called
* if {@link #vpxInit} was called with {@code enableBufferManager = true}.
*/
private native int vpxRenderFrame(long context, Surface surface, VpxOutputBuffer outputBuffer);
private native int vpxRenderFrame(
long context, Surface surface, VideoDecoderOutputBuffer outputBuffer);

/**
* Releases the frame. Used with OUTPUT_MODE_SURFACE_YUV only. Must only be called if {@link
* #vpxInit} was called with {@code enableBufferManager = true}.
*/
private native int vpxReleaseFrame(long context, VpxOutputBuffer outputBuffer);
private native int vpxReleaseFrame(long context, VideoDecoderOutputBuffer outputBuffer);

private native int vpxGetErrorCode(long context);
private native String vpxGetErrorMessage(long context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,22 @@

import com.google.android.exoplayer2.video.VideoDecoderOutputBuffer;

/** Video output buffer, populated by {@link VpxDecoder}. */
// TODO(b/139174707): Delete this class once binaries in WVVp9OpusPlaybackTest are updated to depend
// on VideoDecoderOutputBuffer. Also mark VideoDecoderOutputBuffer as final.
/**
* Video output buffer, populated by {@link VpxDecoder}.
*
* @deprecated Use {@link VideoDecoderOutputBuffer} instead.
*/
@Deprecated
public final class VpxOutputBuffer extends VideoDecoderOutputBuffer {

private final VpxDecoder owner;

public VpxOutputBuffer(VpxDecoder owner) {
this.owner = owner;
/**
* Creates VpxOutputBuffer.
*
* @param owner Buffer owner.
*/
public VpxOutputBuffer(VideoDecoderOutputBuffer.Owner owner) {
super(owner);
}

@Override
public void release() {
owner.releaseOutputBuffer(this);
}

}
44 changes: 22 additions & 22 deletions extensions/vp9/src/main/jni/vpx_jni.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,27 @@
#define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, \
__VA_ARGS__))

#define DECODER_FUNC(RETURN_TYPE, NAME, ...) \
extern "C" { \
JNIEXPORT RETURN_TYPE \
Java_com_google_android_exoplayer2_ext_vp9_VpxDecoder_ ## NAME \
(JNIEnv* env, jobject thiz, ##__VA_ARGS__);\
} \
JNIEXPORT RETURN_TYPE \
Java_com_google_android_exoplayer2_ext_vp9_VpxDecoder_ ## NAME \
(JNIEnv* env, jobject thiz, ##__VA_ARGS__)\

#define LIBRARY_FUNC(RETURN_TYPE, NAME, ...) \
extern "C" { \
JNIEXPORT RETURN_TYPE \
Java_com_google_android_exoplayer2_ext_vp9_VpxLibrary_ ## NAME \
(JNIEnv* env, jobject thiz, ##__VA_ARGS__);\
} \
JNIEXPORT RETURN_TYPE \
Java_com_google_android_exoplayer2_ext_vp9_VpxLibrary_ ## NAME \
(JNIEnv* env, jobject thiz, ##__VA_ARGS__)\

// JNI references for VpxOutputBuffer class.
#define DECODER_FUNC(RETURN_TYPE, NAME, ...) \
extern "C" { \
JNIEXPORT RETURN_TYPE \
Java_com_google_android_exoplayer2_ext_vp9_VpxDecoder_##NAME( \
JNIEnv* env, jobject thiz, ##__VA_ARGS__); \
} \
JNIEXPORT RETURN_TYPE \
Java_com_google_android_exoplayer2_ext_vp9_VpxDecoder_##NAME( \
JNIEnv* env, jobject thiz, ##__VA_ARGS__)

#define LIBRARY_FUNC(RETURN_TYPE, NAME, ...) \
extern "C" { \
JNIEXPORT RETURN_TYPE \
Java_com_google_android_exoplayer2_ext_vp9_VpxLibrary_##NAME( \
JNIEnv* env, jobject thiz, ##__VA_ARGS__); \
} \
JNIEXPORT RETURN_TYPE \
Java_com_google_android_exoplayer2_ext_vp9_VpxLibrary_##NAME( \
JNIEnv* env, jobject thiz, ##__VA_ARGS__)

// JNI references for VideoDecoderOutputBuffer class.
static jmethodID initForYuvFrame;
static jmethodID initForPrivateFrame;
static jfieldID dataField;
Expand Down Expand Up @@ -477,7 +477,7 @@ DECODER_FUNC(jlong, vpxInit, jboolean disableLoopFilter,

// Populate JNI References.
const jclass outputBufferClass = env->FindClass(
"com/google/android/exoplayer2/ext/vp9/VpxOutputBuffer");
"com/google/android/exoplayer2/video/VideoDecoderOutputBuffer");
initForYuvFrame = env->GetMethodID(outputBufferClass, "initForYuvFrame",
"(IIIII)Z");
initForPrivateFrame =
Expand Down
5 changes: 5 additions & 0 deletions library/core/proguard-rules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,8 @@
# Don't warn about checkerframework and Kotlin annotations
-dontwarn org.checkerframework.**
-dontwarn kotlin.annotations.jvm.**

# Some members of this class are being accessed from native methods. Keep them unobfuscated.
-keep class com.google.android.exoplayer2.ext.video.VideoDecoderOutputBuffer {
*;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,18 @@
import java.nio.ByteBuffer;

/** Video decoder output buffer containing video frame data. */
public abstract class VideoDecoderOutputBuffer extends OutputBuffer {
public class VideoDecoderOutputBuffer extends OutputBuffer {

/** Buffer owner. */
public interface Owner {

/**
* Releases the buffer.
*
* @param outputBuffer Output buffer.
*/
public void releaseOutputBuffer(VideoDecoderOutputBuffer outputBuffer);
}

// LINT.IfChange
public static final int COLORSPACE_UNKNOWN = 0;
Expand Down Expand Up @@ -54,6 +65,22 @@ public abstract class VideoDecoderOutputBuffer extends OutputBuffer {
*/
@Nullable public ByteBuffer supplementalData;

private final Owner owner;

/**
* Creates VideoDecoderOutputBuffer.
*
* @param owner Buffer owner.
*/
public VideoDecoderOutputBuffer(Owner owner) {
this.owner = owner;
}

@Override
public void release() {
owner.releaseOutputBuffer(this);
}

/**
* Initializes the buffer.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.exoplayer2.ext.vp9;
package com.google.android.exoplayer2.video;

/**
* Renders the {@link VpxOutputBuffer}.
*/
public interface VpxOutputBufferRenderer {
/** Renders the {@link VideoDecoderOutputBuffer}. */
public interface VideoDecoderOutputBufferRenderer {

/**
* Sets the output buffer to be rendered. The renderer is responsible for releasing the buffer.
*
* @param outputBuffer The output buffer to be rendered.
*/
void setOutputBuffer(VpxOutputBuffer outputBuffer);

void setOutputBuffer(VideoDecoderOutputBuffer outputBuffer);
}
Loading

0 comments on commit 22c3be7

Please sign in to comment.