Skip to content

Commit

Permalink
Make SimpleDecoderXRenderers work with any Decoder implementation
Browse files Browse the repository at this point in the history
The restriction that these classes only work with SimpleDecoders
is unnecessary. An FfmpegVideoRenderer will not be able to use a
SimpleDecoder, because the SimpleDecoder assumption that each input
buffer can be decoded immediately into a corresponding output is
not true for all video codecs that Ffmpeg supports (e.g., H264 does
not have this property). Generalizing SimpleDecoderVideoRenderer to
DecoderVideoRenderer will allow FfmpegVideoRenderer to still use
the base class, without having to use a SimpleDecoder.

This is a preliminary change toward being able to merge a version
of #7079.

Issue: #2159
PiperOrigin-RevId: 301412344
  • Loading branch information
ojw28 committed Mar 19, 2020
1 parent a3a3b5b commit cbe99ec
Show file tree
Hide file tree
Showing 23 changed files with 79 additions and 80 deletions.
3 changes: 3 additions & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
* Add option to `MergingMediaSource` to adjust the time offsets between
the merged sources
([#6103](https://github.com/google/ExoPlayer/issues/6103)).
* `SimpleDecoderVideoRenderer` and `SimpleDecoderAudioRenderer` renamed to
`DecoderVideoRenderer` and `DecoderAudioRenderer` respectively, and
generalized to work with `Decoder` rather than `SimpleDecoder`.
* Text:
* Parse `<ruby>` and `<rt>` tags in WebVTT subtitles (rendering is coming
later).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import com.google.android.exoplayer2.util.MimeTypes;
import com.google.android.exoplayer2.util.TraceUtil;
import com.google.android.exoplayer2.util.Util;
import com.google.android.exoplayer2.video.SimpleDecoderVideoRenderer;
import com.google.android.exoplayer2.video.DecoderVideoRenderer;
import com.google.android.exoplayer2.video.VideoDecoderException;
import com.google.android.exoplayer2.video.VideoDecoderInputBuffer;
import com.google.android.exoplayer2.video.VideoDecoderOutputBuffer;
Expand All @@ -52,7 +52,7 @@
* VideoDecoderOutputBufferRenderer}, or null.
* </ul>
*/
public class Libgav1VideoRenderer extends SimpleDecoderVideoRenderer {
public class Libgav1VideoRenderer extends DecoderVideoRenderer {

private static final int DEFAULT_NUM_OF_INPUT_BUFFERS = 4;
private static final int DEFAULT_NUM_OF_OUTPUT_BUFFERS = 4;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,15 @@
import com.google.android.exoplayer2.audio.AudioProcessor;
import com.google.android.exoplayer2.audio.AudioRendererEventListener;
import com.google.android.exoplayer2.audio.AudioSink;
import com.google.android.exoplayer2.audio.DecoderAudioRenderer;
import com.google.android.exoplayer2.audio.DefaultAudioSink;
import com.google.android.exoplayer2.audio.SimpleDecoderAudioRenderer;
import com.google.android.exoplayer2.drm.ExoMediaCrypto;
import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.MimeTypes;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;

/**
* Decodes and renders audio using FFmpeg.
*/
public final class FfmpegAudioRenderer extends SimpleDecoderAudioRenderer {
/** Decodes and renders audio using FFmpeg. */
public final class FfmpegAudioRenderer extends DecoderAudioRenderer {

/** The number of input and output buffers. */
private static final int NUM_BUFFERS = 16;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ protected DecoderInputBuffer createInputBuffer() {

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ protected DecoderInputBuffer createInputBuffer() {

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import com.google.android.exoplayer2.audio.AudioProcessor;
import com.google.android.exoplayer2.audio.AudioRendererEventListener;
import com.google.android.exoplayer2.audio.AudioSink;
import com.google.android.exoplayer2.audio.SimpleDecoderAudioRenderer;
import com.google.android.exoplayer2.audio.DecoderAudioRenderer;
import com.google.android.exoplayer2.drm.ExoMediaCrypto;
import com.google.android.exoplayer2.extractor.FlacStreamMetadata;
import com.google.android.exoplayer2.util.Assertions;
Expand All @@ -32,7 +32,7 @@
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;

/** Decodes and renders audio using the native Flac decoder. */
public final class LibflacAudioRenderer extends SimpleDecoderAudioRenderer {
public final class LibflacAudioRenderer extends DecoderAudioRenderer {

private static final int NUM_BUFFERS = 16;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.audio.AudioProcessor;
import com.google.android.exoplayer2.audio.AudioRendererEventListener;
import com.google.android.exoplayer2.audio.SimpleDecoderAudioRenderer;
import com.google.android.exoplayer2.audio.DecoderAudioRenderer;
import com.google.android.exoplayer2.drm.ExoMediaCrypto;
import com.google.android.exoplayer2.util.MimeTypes;

/** Decodes and renders audio using the native Opus decoder. */
public class LibopusAudioRenderer extends SimpleDecoderAudioRenderer {
public class LibopusAudioRenderer extends DecoderAudioRenderer {

/** The number of input and output buffers. */
private static final int NUM_BUFFERS = 16;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ protected DecoderInputBuffer createInputBuffer() {

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import com.google.android.exoplayer2.drm.ExoMediaCrypto;
import com.google.android.exoplayer2.util.MimeTypes;
import com.google.android.exoplayer2.util.TraceUtil;
import com.google.android.exoplayer2.video.SimpleDecoderVideoRenderer;
import com.google.android.exoplayer2.video.DecoderVideoRenderer;
import com.google.android.exoplayer2.video.VideoDecoderException;
import com.google.android.exoplayer2.video.VideoDecoderInputBuffer;
import com.google.android.exoplayer2.video.VideoDecoderOutputBuffer;
Expand All @@ -52,7 +52,7 @@
* VideoDecoderOutputBufferRenderer}, or null.
* </ul>
*/
public class LibvpxVideoRenderer extends SimpleDecoderVideoRenderer {
public class LibvpxVideoRenderer extends DecoderVideoRenderer {

/** The number of input buffers. */
private final int numInputBuffers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public final class VpxOutputBuffer extends VideoDecoderOutputBuffer {
*
* @param owner Buffer owner.
*/
public VpxOutputBuffer(VideoDecoderOutputBuffer.Owner owner) {
public VpxOutputBuffer(Owner<VideoDecoderOutputBuffer> owner) {
super(owner);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import com.google.android.exoplayer2.source.SampleStream;
import com.google.android.exoplayer2.util.MediaClock;
import com.google.android.exoplayer2.util.Util;
import com.google.android.exoplayer2.video.SimpleDecoderVideoRenderer;
import com.google.android.exoplayer2.video.DecoderVideoRenderer;
import com.google.android.exoplayer2.video.VideoDecoderOutputBufferRenderer;
import com.google.android.exoplayer2.video.VideoFrameMetadataListener;
import com.google.android.exoplayer2.video.spherical.CameraMotionListener;
Expand Down Expand Up @@ -115,7 +115,7 @@ public interface Renderer extends PlayerMessage.Target {
@SuppressWarnings("deprecation")
int MSG_SET_CAMERA_MOTION_LISTENER = C.MSG_SET_CAMERA_MOTION_LISTENER;
/**
* The type of a message that can be passed to a {@link SimpleDecoderVideoRenderer} via {@link
* The type of a message that can be passed to a {@link DecoderVideoRenderer} via {@link
* ExoPlayer#createMessage(Target)}. The message payload should be the target {@link
* VideoDecoderOutputBufferRenderer}, or null.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
import com.google.android.exoplayer2.PlayerMessage.Target;
import com.google.android.exoplayer2.RendererCapabilities;
import com.google.android.exoplayer2.audio.AudioRendererEventListener.EventDispatcher;
import com.google.android.exoplayer2.decoder.Decoder;
import com.google.android.exoplayer2.decoder.DecoderCounters;
import com.google.android.exoplayer2.decoder.DecoderInputBuffer;
import com.google.android.exoplayer2.decoder.SimpleDecoder;
import com.google.android.exoplayer2.decoder.SimpleOutputBuffer;
import com.google.android.exoplayer2.drm.DrmSession;
import com.google.android.exoplayer2.drm.DrmSession.DrmSessionException;
Expand All @@ -47,7 +47,7 @@
import java.lang.annotation.RetentionPolicy;

/**
* Decodes and renders audio using a {@link SimpleDecoder}.
* Decodes and renders audio using a {@link Decoder}.
*
* <p>This renderer accepts the following messages sent via {@link ExoPlayer#createMessage(Target)}
* on the playback thread:
Expand All @@ -68,7 +68,7 @@
* underlying audio track.
* </ul>
*/
public abstract class SimpleDecoderAudioRenderer extends BaseRenderer implements MediaClock {
public abstract class DecoderAudioRenderer extends BaseRenderer implements MediaClock {

@Documented
@Retention(RetentionPolicy.SOURCE)
Expand Down Expand Up @@ -105,8 +105,7 @@ public abstract class SimpleDecoderAudioRenderer extends BaseRenderer implements
private int encoderPadding;

@Nullable
private SimpleDecoder<
DecoderInputBuffer, ? extends SimpleOutputBuffer, ? extends AudioDecoderException>
private Decoder<DecoderInputBuffer, ? extends SimpleOutputBuffer, ? extends AudioDecoderException>
decoder;

@Nullable private DecoderInputBuffer inputBuffer;
Expand All @@ -125,7 +124,7 @@ public abstract class SimpleDecoderAudioRenderer extends BaseRenderer implements
private boolean outputStreamEnded;
private boolean waitingForKeys;

public SimpleDecoderAudioRenderer() {
public DecoderAudioRenderer() {
this(/* eventHandler= */ null, /* eventListener= */ null);
}

Expand All @@ -135,7 +134,7 @@ public SimpleDecoderAudioRenderer() {
* @param eventListener A listener of events. May be null if delivery of events is not required.
* @param audioProcessors Optional {@link AudioProcessor}s that will process audio before output.
*/
public SimpleDecoderAudioRenderer(
public DecoderAudioRenderer(
@Nullable Handler eventHandler,
@Nullable AudioRendererEventListener eventListener,
AudioProcessor... audioProcessors) {
Expand All @@ -154,7 +153,7 @@ public SimpleDecoderAudioRenderer(
* default capabilities (no encoded audio passthrough support) should be assumed.
* @param audioProcessors Optional {@link AudioProcessor}s that will process audio before output.
*/
public SimpleDecoderAudioRenderer(
public DecoderAudioRenderer(
@Nullable Handler eventHandler,
@Nullable AudioRendererEventListener eventListener,
@Nullable AudioCapabilities audioCapabilities,
Expand All @@ -168,7 +167,7 @@ public SimpleDecoderAudioRenderer(
* @param eventListener A listener of events. May be null if delivery of events is not required.
* @param audioSink The sink to which audio will be output.
*/
public SimpleDecoderAudioRenderer(
public DecoderAudioRenderer(
@Nullable Handler eventHandler,
@Nullable AudioRendererEventListener eventListener,
AudioSink audioSink) {
Expand Down Expand Up @@ -306,7 +305,7 @@ protected void onAudioTrackSkipSilenceEnabledChanged(boolean skipSilenceEnabled)
* @return The decoder.
* @throws AudioDecoderException If an error occurred creating a suitable decoder.
*/
protected abstract SimpleDecoder<
protected abstract Decoder<
DecoderInputBuffer, ? extends SimpleOutputBuffer, ? extends AudioDecoderException>
createDecoder(Format format, @Nullable ExoMediaCrypto mediaCrypto)
throws AudioDecoderException;
Expand Down Expand Up @@ -689,14 +688,14 @@ private final class AudioSinkListener implements AudioSink.Listener {
@Override
public void onAudioSessionId(int audioSessionId) {
eventDispatcher.audioSessionId(audioSessionId);
SimpleDecoderAudioRenderer.this.onAudioSessionId(audioSessionId);
DecoderAudioRenderer.this.onAudioSessionId(audioSessionId);
}

@Override
public void onPositionDiscontinuity() {
onAudioTrackPositionDiscontinuity();
// We are out of sync so allow currentPositionUs to jump backwards.
SimpleDecoderAudioRenderer.this.allowPositionDiscontinuity = true;
DecoderAudioRenderer.this.allowPositionDiscontinuity = true;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@
*/
public abstract class OutputBuffer extends Buffer {

/** Buffer owner. */
public interface Owner<S extends OutputBuffer> {

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

/**
* The presentation timestamp for the buffer, in microseconds.
*/
Expand All @@ -34,5 +45,4 @@ public abstract class OutputBuffer extends Buffer {
* Releases the output buffer for reuse. Must be called when the buffer is no longer needed.
*/
public abstract void release();

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
import com.google.android.exoplayer2.util.Assertions;
import java.util.ArrayDeque;

/** Base class for {@link Decoder}s that use their own decode thread. */
/**
* Base class for {@link Decoder}s that use their own decode thread and decode each input buffer
* immediately into a corresponding output buffer.
*/
@SuppressWarnings("UngroupedOverloads")
public abstract class SimpleDecoder<
I extends DecoderInputBuffer, O extends OutputBuffer, E extends Exception>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
*/
public class SimpleOutputBuffer extends OutputBuffer {

private final SimpleDecoder<?, SimpleOutputBuffer, ?> owner;
private final Owner<SimpleOutputBuffer> owner;

@Nullable public ByteBuffer data;

public SimpleOutputBuffer(SimpleDecoder<?, SimpleOutputBuffer, ?> owner) {
public SimpleOutputBuffer(Owner<SimpleOutputBuffer> owner) {
this.owner = owner;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,14 @@ protected final SubtitleInputBuffer createInputBuffer() {

@Override
protected final SubtitleOutputBuffer createOutputBuffer() {
return new SimpleSubtitleOutputBuffer(this);
return new SimpleSubtitleOutputBuffer(this::releaseOutputBuffer);
}

@Override
protected final SubtitleDecoderException createUnexpectedDecodeException(Throwable error) {
return new SubtitleDecoderException("Unexpected decode error", error);
}

@Override
protected final void releaseOutputBuffer(SubtitleOutputBuffer buffer) {
super.releaseOutputBuffer(buffer);
}

@SuppressWarnings("ByteBufferBackingArray")
@Override
@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@
*/
/* package */ final class SimpleSubtitleOutputBuffer extends SubtitleOutputBuffer {

private final SimpleSubtitleDecoder owner;
private final Owner<SubtitleOutputBuffer> owner;

/**
* @param owner The decoder that owns this buffer.
*/
public SimpleSubtitleOutputBuffer(SimpleSubtitleDecoder owner) {
/** @param owner The decoder that owns this buffer. */
public SimpleSubtitleOutputBuffer(Owner<SubtitleOutputBuffer> owner) {
super();
this.owner = owner;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@ public List<Cue> getCues(long timeUs) {
return Assertions.checkNotNull(subtitle).getCues(timeUs - subsampleOffsetUs);
}

@Override
public abstract void release();

@Override
public void clear() {
super.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,15 @@
private long playbackPositionUs;
private long queuedInputBufferCount;

@SuppressWarnings("nullness:methodref.receiver.bound.invalid")
public CeaDecoder() {
availableInputBuffers = new ArrayDeque<>();
for (int i = 0; i < NUM_INPUT_BUFFERS; i++) {
availableInputBuffers.add(new CeaInputBuffer());
}
availableOutputBuffers = new ArrayDeque<>();
for (int i = 0; i < NUM_OUTPUT_BUFFERS; i++) {
availableOutputBuffers.add(new CeaOutputBuffer());
availableOutputBuffers.add(new CeaOutputBuffer(this::releaseOutputBuffer));
}
queuedInputBuffers = new PriorityQueue<>();
}
Expand Down Expand Up @@ -199,11 +200,17 @@ public int compareTo(CeaInputBuffer other) {
}
}

private final class CeaOutputBuffer extends SubtitleOutputBuffer {
private static final class CeaOutputBuffer extends SubtitleOutputBuffer {

private Owner<CeaOutputBuffer> owner;

public CeaOutputBuffer(Owner<CeaOutputBuffer> owner) {
this.owner = owner;
}

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

0 comments on commit cbe99ec

Please sign in to comment.