Make Renderer an interface

In V1 we received complaints that Renderer was too locked down.
In particular, it wasn't possible to implement a Renderer that
wrapped another Renderer, since most of the methods were package
private and final (e.g. enable).

This change defines Renderer as a proper interface, removing
this limitation. A method-reordering-only change will follow
this one, to get things into a nice consistent/sane order in
each of the Renderer classes.
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=127527995
This commit is contained in:
olly 2016-07-15 04:16:23 -07:00 committed by Oliver Woodman
parent 7c551081f5
commit dcc1ac56df
13 changed files with 322 additions and 253 deletions

View File

@ -18,7 +18,7 @@ package com.google.android.exoplayer2.demo;
import com.google.android.exoplayer2.ExoPlaybackException; import com.google.android.exoplayer2.ExoPlaybackException;
import com.google.android.exoplayer2.ExoPlayer; import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.Format; import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.Renderer; import com.google.android.exoplayer2.RendererCapabilities;
import com.google.android.exoplayer2.SimpleExoPlayer; import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.decoder.DecoderCounters; import com.google.android.exoplayer2.decoder.DecoderCounters;
import com.google.android.exoplayer2.drm.StreamingDrmSessionManager; import com.google.android.exoplayer2.drm.StreamingDrmSessionManager;
@ -130,7 +130,8 @@ public class EventLogger implements ExoPlayer.EventListener, SimpleExoPlayer.Deb
TrackGroup trackGroup = trackGroups.get(groupIndex); TrackGroup trackGroup = trackGroups.get(groupIndex);
for (int trackIndex = 0; trackIndex < trackGroup.length; trackIndex++) { for (int trackIndex = 0; trackIndex < trackGroup.length; trackIndex++) {
String status = getTrackStatusString(false); String status = getTrackStatusString(false);
String formatSupport = getFormatSupportString(Renderer.FORMAT_UNSUPPORTED_TYPE); String formatSupport = getFormatSupportString(
RendererCapabilities.FORMAT_UNSUPPORTED_TYPE);
Log.d(TAG, " " + status + " Track:" + trackIndex + ", " Log.d(TAG, " " + status + " Track:" + trackIndex + ", "
+ getFormatString(trackGroup.getFormat(trackIndex)) + getFormatString(trackGroup.getFormat(trackIndex))
+ ", supported=" + formatSupport); + ", supported=" + formatSupport);
@ -296,13 +297,13 @@ public class EventLogger implements ExoPlayer.EventListener, SimpleExoPlayer.Deb
private static String getFormatSupportString(int formatSupport) { private static String getFormatSupportString(int formatSupport) {
switch (formatSupport) { switch (formatSupport) {
case Renderer.FORMAT_HANDLED: case RendererCapabilities.FORMAT_HANDLED:
return "YES"; return "YES";
case Renderer.FORMAT_EXCEEDS_CAPABILITIES: case RendererCapabilities.FORMAT_EXCEEDS_CAPABILITIES:
return "NO_EXCEEDS_CAPABILITIES"; return "NO_EXCEEDS_CAPABILITIES";
case Renderer.FORMAT_UNSUPPORTED_SUBTYPE: case RendererCapabilities.FORMAT_UNSUPPORTED_SUBTYPE:
return "NO_UNSUPPORTED_TYPE"; return "NO_UNSUPPORTED_TYPE";
case Renderer.FORMAT_UNSUPPORTED_TYPE: case RendererCapabilities.FORMAT_UNSUPPORTED_TYPE:
return "NO"; return "NO";
default: default:
return "?"; return "?";
@ -314,11 +315,11 @@ public class EventLogger implements ExoPlayer.EventListener, SimpleExoPlayer.Deb
return "N/A"; return "N/A";
} }
switch (adaptiveSupport) { switch (adaptiveSupport) {
case Renderer.ADAPTIVE_SEAMLESS: case RendererCapabilities.ADAPTIVE_SEAMLESS:
return "YES"; return "YES";
case Renderer.ADAPTIVE_NOT_SEAMLESS: case RendererCapabilities.ADAPTIVE_NOT_SEAMLESS:
return "YES_NOT_SEAMLESS"; return "YES_NOT_SEAMLESS";
case Renderer.ADAPTIVE_NOT_SUPPORTED: case RendererCapabilities.ADAPTIVE_NOT_SUPPORTED:
return "NO"; return "NO";
default: default:
return "?"; return "?";

View File

@ -16,7 +16,7 @@
package com.google.android.exoplayer2.demo; package com.google.android.exoplayer2.demo;
import com.google.android.exoplayer2.Format; import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.Renderer; import com.google.android.exoplayer2.RendererCapabilities;
import com.google.android.exoplayer2.source.TrackGroup; import com.google.android.exoplayer2.source.TrackGroup;
import com.google.android.exoplayer2.source.TrackGroupArray; import com.google.android.exoplayer2.source.TrackGroupArray;
import com.google.android.exoplayer2.trackselection.MappingTrackSelector; import com.google.android.exoplayer2.trackselection.MappingTrackSelector;
@ -81,7 +81,7 @@ import java.util.Locale;
trackGroupsAdaptive = new boolean[trackGroups.length]; trackGroupsAdaptive = new boolean[trackGroups.length];
for (int i = 0; i < trackGroups.length; i++) { for (int i = 0; i < trackGroups.length; i++) {
trackGroupsAdaptive[i] = trackInfo.getAdaptiveSupport(rendererIndex, i, false) trackGroupsAdaptive[i] = trackInfo.getAdaptiveSupport(rendererIndex, i, false)
!= Renderer.ADAPTIVE_NOT_SUPPORTED; != RendererCapabilities.ADAPTIVE_NOT_SUPPORTED;
} }
isDisabled = selector.getRendererDisabled(rendererIndex); isDisabled = selector.getRendererDisabled(rendererIndex);
override = selector.hasSelectionOverride(rendererIndex, trackGroups) override = selector.hasSelectionOverride(rendererIndex, trackGroups)
@ -133,7 +133,7 @@ import java.util.Locale;
trackViewLayoutId, root, false); trackViewLayoutId, root, false);
trackView.setText(buildTrackName(group.getFormat(trackIndex))); trackView.setText(buildTrackName(group.getFormat(trackIndex)));
if (trackInfo.getTrackFormatSupport(rendererIndex, groupIndex, trackIndex) if (trackInfo.getTrackFormatSupport(rendererIndex, groupIndex, trackIndex)
== Renderer.FORMAT_HANDLED) { == RendererCapabilities.FORMAT_HANDLED) {
haveSupportedTracks = true; haveSupportedTracks = true;
trackView.setTag(Pair.create(groupIndex, trackIndex)); trackView.setTag(Pair.create(groupIndex, trackIndex));
trackView.setOnClickListener(this); trackView.setOnClickListener(this);

View File

@ -15,12 +15,12 @@
*/ */
package com.google.android.exoplayer2.ext.vp9; package com.google.android.exoplayer2.ext.vp9;
import com.google.android.exoplayer2.BaseRenderer;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.ExoPlaybackException; import com.google.android.exoplayer2.ExoPlaybackException;
import com.google.android.exoplayer2.ExoPlayer; import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.Format; import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.FormatHolder; import com.google.android.exoplayer2.FormatHolder;
import com.google.android.exoplayer2.Renderer;
import com.google.android.exoplayer2.decoder.DecoderCounters; import com.google.android.exoplayer2.decoder.DecoderCounters;
import com.google.android.exoplayer2.decoder.DecoderInputBuffer; import com.google.android.exoplayer2.decoder.DecoderInputBuffer;
import com.google.android.exoplayer2.util.MimeTypes; import com.google.android.exoplayer2.util.MimeTypes;
@ -37,7 +37,7 @@ import android.view.Surface;
/** /**
* Decodes and renders video using the native VP9 decoder. * Decodes and renders video using the native VP9 decoder.
*/ */
public final class LibvpxVideoRenderer extends Renderer { public final class LibvpxVideoRenderer extends BaseRenderer {
/** /**
* The type of a message that can be passed to an instance of this class via * The type of a message that can be passed to an instance of this class via
@ -150,7 +150,7 @@ public final class LibvpxVideoRenderer extends Renderer {
} }
@Override @Override
protected void render(long positionUs, long elapsedRealtimeUs) throws ExoPlaybackException { public void render(long positionUs, long elapsedRealtimeUs) throws ExoPlaybackException {
if (outputStreamEnded) { if (outputStreamEnded) {
return; return;
} }
@ -241,8 +241,7 @@ public final class LibvpxVideoRenderer extends Renderer {
return false; return false;
} }
if (getState() == Renderer.STATE_STARTED if (getState() == STATE_STARTED && outputBuffer.timestampUs <= positionUs + 30000) {
&& outputBuffer.timestampUs <= positionUs + 30000) {
renderBuffer(); renderBuffer();
} }
return false; return false;
@ -330,12 +329,12 @@ public final class LibvpxVideoRenderer extends Renderer {
} }
@Override @Override
protected boolean isEnded() { public boolean isEnded() {
return outputStreamEnded; return outputStreamEnded;
} }
@Override @Override
protected boolean isReady() { public boolean isReady() {
if (format != null && (isSourceReady() || outputBuffer != null) && renderedFirstFrame) { if (format != null && (isSourceReady() || outputBuffer != null) && renderedFirstFrame) {
// Ready. If we were joining then we've now joined, so clear the joining deadline. // Ready. If we were joining then we've now joined, so clear the joining deadline.
joiningDeadlineMs = -1; joiningDeadlineMs = -1;

View File

@ -0,0 +1,236 @@
/*
* Copyright (C) 2016 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;
import com.google.android.exoplayer2.decoder.DecoderInputBuffer;
import com.google.android.exoplayer2.source.SampleStream;
import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.MediaClock;
import java.io.IOException;
/**
* An abstract base class suitable for most {@link Renderer} implementations.
*/
public abstract class BaseRenderer implements Renderer {
private int index;
private int state;
private SampleStream stream;
private long streamOffsetUs;
private boolean readEndOfStream;
private boolean streamIsFinal;
public BaseRenderer() {
readEndOfStream = true;
}
@Override
public final void setIndex(int index) {
this.index = index;
}
@Override
public final int getIndex() {
return index;
}
@Override
public MediaClock getMediaClock() {
return null;
}
@Override
public final int getState() {
return state;
}
@Override
public final void enable(Format[] formats, SampleStream stream, long positionUs,
boolean joining, long offsetUs) throws ExoPlaybackException {
Assertions.checkState(state == STATE_DISABLED);
state = STATE_ENABLED;
onEnabled(joining);
replaceStream(formats, stream, offsetUs);
onReset(positionUs, joining);
}
/**
* Called when the renderer is enabled.
* <p>
* The default implementation is a no-op.
*
* @param joining Whether this renderer is being enabled to join an ongoing playback.
* @throws ExoPlaybackException If an error occurs.
*/
protected void onEnabled(boolean joining) throws ExoPlaybackException {
// Do nothing.
}
@Override
public final void replaceStream(Format[] formats, SampleStream stream, long offsetUs)
throws ExoPlaybackException {
Assertions.checkState(!streamIsFinal);
this.stream = stream;
readEndOfStream = false;
streamOffsetUs = offsetUs;
onStreamChanged(formats);
}
/**
* Called when the renderer's stream has changed.
* <p>
* The default implementation is a no-op.
*
* @param formats The enabled formats.
* @throws ExoPlaybackException Thrown if an error occurs.
*/
protected void onStreamChanged(Format[] formats) throws ExoPlaybackException {
// Do nothing.
}
@Override
public final void reset(long positionUs) throws ExoPlaybackException {
streamIsFinal = false;
onReset(positionUs, false);
}
/**
* Invoked when a reset is encountered, and also when the renderer is enabled.
* <p>
* This method may be called when the renderer is in the following states:
* {@link #STATE_ENABLED}, {@link #STATE_STARTED}.
*
* @param positionUs The playback position in microseconds.
* @param joining Whether this renderer is being enabled to join an ongoing playback.
* @throws ExoPlaybackException If an error occurs handling the reset.
*/
protected void onReset(long positionUs, boolean joining) throws ExoPlaybackException {
// Do nothing.
}
@Override
public final boolean hasReadStreamToEnd() {
return readEndOfStream;
}
@Override
public final void setCurrentStreamIsFinal() {
streamIsFinal = true;
}
@Override
public final void start() throws ExoPlaybackException {
Assertions.checkState(state == STATE_ENABLED);
state = STATE_STARTED;
onStarted();
}
/**
* Called when the renderer is started.
* <p>
* The default implementation is a no-op.
*
* @throws ExoPlaybackException If an error occurs.
*/
protected void onStarted() throws ExoPlaybackException {
// Do nothing.
}
@Override
public final void stop() throws ExoPlaybackException {
Assertions.checkState(state == STATE_STARTED);
state = STATE_ENABLED;
onStopped();
}
/**
* Called when the renderer is stopped.
* <p>
* The default implementation is a no-op.
*
* @throws ExoPlaybackException If an error occurs.
*/
protected void onStopped() throws ExoPlaybackException {
// Do nothing.
}
@Override
public final void disable() {
Assertions.checkState(state == STATE_ENABLED);
state = STATE_DISABLED;
onDisabled();
stream = null;
streamIsFinal = false;
}
/**
* Called when the renderer is disabled.
* <p>
* The default implementation is a no-op.
*/
protected void onDisabled() {
// Do nothing.
}
@Override
public final void maybeThrowStreamError() throws IOException {
stream.maybeThrowError();
}
// RendererCapabilities implementation.
@Override
public int supportsMixedMimeTypeAdaptation() throws ExoPlaybackException {
return ADAPTIVE_NOT_SUPPORTED;
}
// ExoPlayerComponent implementation.
@Override
public void handleMessage(int what, Object object) throws ExoPlaybackException {
// Do nothing.
}
// Methods to be called by subclasses.
/**
* Reads from the enabled upstream source.
*
* @see SampleStream#readData(FormatHolder, DecoderInputBuffer)
*/
protected final int readSource(FormatHolder formatHolder, DecoderInputBuffer buffer) {
int result = stream.readData(formatHolder, buffer);
if (result == C.RESULT_BUFFER_READ) {
if (buffer.isEndOfStream()) {
readEndOfStream = true;
return streamIsFinal ? C.RESULT_BUFFER_READ : C.RESULT_NOTHING_READ;
}
buffer.timeUs += streamOffsetUs;
}
return result;
}
/**
* Returns whether the upstream source is ready.
*
* @return True if the source is ready. False otherwise.
*/
protected final boolean isSourceReady() {
return readEndOfStream ? streamIsFinal : stream.isReady();
}
}

View File

@ -717,12 +717,12 @@ import java.util.ArrayList;
for (int j = 0; j < formats.length; j++) { for (int j = 0; j < formats.length; j++) {
formats[j] = groups.get(newSelection.group).getFormat(newSelection.getTrack(j)); formats[j] = groups.get(newSelection.group).getFormat(newSelection.getTrack(j));
} }
renderer.replaceSampleStream(formats, readingPeriod.sampleStreams[i], renderer.replaceStream(formats, readingPeriod.sampleStreams[i],
readingPeriod.offsetUs); readingPeriod.offsetUs);
} else { } else {
// The renderer will be disabled when transitioning to playing the next period. Mark // The renderer will be disabled when transitioning to playing the next period. Mark
// the SampleStream as final to play out any remaining data. // the SampleStream as final to play out any remaining data.
renderer.setCurrentSampleStreamIsFinal(); renderer.setCurrentStreamIsFinal();
} }
} }
} }
@ -731,7 +731,7 @@ import java.util.ArrayList;
readingPeriod = null; readingPeriod = null;
// This is the last period, so signal the renderers to read the end of the stream. // This is the last period, so signal the renderers to read the end of the stream.
for (Renderer renderer : enabledRenderers) { for (Renderer renderer : enabledRenderers) {
renderer.setCurrentSampleStreamIsFinal(); renderer.setCurrentStreamIsFinal();
} }
} }
} }

View File

@ -16,9 +16,7 @@
package com.google.android.exoplayer2; package com.google.android.exoplayer2;
import com.google.android.exoplayer2.ExoPlayer.ExoPlayerComponent; import com.google.android.exoplayer2.ExoPlayer.ExoPlayerComponent;
import com.google.android.exoplayer2.decoder.DecoderInputBuffer;
import com.google.android.exoplayer2.source.SampleStream; import com.google.android.exoplayer2.source.SampleStream;
import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.MediaClock; import com.google.android.exoplayer2.util.MediaClock;
import java.io.IOException; import java.io.IOException;
@ -34,50 +32,35 @@ import java.io.IOException;
* alt="Renderer state transitions" * alt="Renderer state transitions"
* border="0"/></p> * border="0"/></p>
*/ */
public abstract class Renderer implements ExoPlayerComponent, RendererCapabilities { public interface Renderer extends ExoPlayerComponent, RendererCapabilities {
/** /**
* The renderer is disabled. * The renderer is disabled.
*/ */
protected static final int STATE_DISABLED = 0; int STATE_DISABLED = 0;
/** /**
* The renderer is enabled but not started. A renderer in this state will typically hold any * The renderer is enabled but not started. A renderer in this state will typically hold any
* resources that it requires for rendering (e.g. media decoders). * resources that it requires for rendering (e.g. media decoders).
*/ */
protected static final int STATE_ENABLED = 1; int STATE_ENABLED = 1;
/** /**
* The renderer is started. Calls to {@link #render(long, long)} will cause media to be rendered. * The renderer is started. Calls to {@link #render(long, long)} will cause media to be rendered.
*/ */
protected static final int STATE_STARTED = 2; int STATE_STARTED = 2;
private int index;
private int state;
private SampleStream stream;
private long streamOffsetUs;
private boolean readEndOfStream;
private boolean streamIsFinal;
public Renderer() {
readEndOfStream = true;
}
/** /**
* Sets the index of this renderer within the player. * Sets the index of this renderer within the player.
* *
* @param index The renderer index. * @param index The renderer index.
*/ */
/* package */ final void setIndex(int index) { void setIndex(int index);
this.index = index;
}
/** /**
* Returns the index of the renderer within the player. * Returns the index of the renderer within the player.
* *
* @return The index of the renderer within the player. * @return The index of the renderer within the player.
*/ */
protected final int getIndex() { int getIndex();
return index;
}
/** /**
* If the renderer advances its own playback position then this method returns a corresponding * If the renderer advances its own playback position then this method returns a corresponding
@ -87,18 +70,14 @@ public abstract class Renderer implements ExoPlayerComponent, RendererCapabiliti
* *
* @return The {@link MediaClock} tracking the playback position of the renderer, or null. * @return The {@link MediaClock} tracking the playback position of the renderer, or null.
*/ */
protected MediaClock getMediaClock() { MediaClock getMediaClock();
return null;
}
/** /**
* Returns the current state of the renderer. * Returns the current state of the renderer.
* *
* @return The current state (one of the {@code STATE_*} constants). * @return The current state (one of the {@code STATE_*} constants).
*/ */
protected final int getState() { int getState();
return state;
}
/** /**
* Enable the renderer to consume from the specified {@link SampleStream}. * Enable the renderer to consume from the specified {@link SampleStream}.
@ -111,26 +90,8 @@ public abstract class Renderer implements ExoPlayerComponent, RendererCapabiliti
* before they are rendered. * before they are rendered.
* @throws ExoPlaybackException If an error occurs. * @throws ExoPlaybackException If an error occurs.
*/ */
/* package */ final void enable(Format[] formats, SampleStream stream, long positionUs, void enable(Format[] formats, SampleStream stream, long positionUs, boolean joining,
boolean joining, long offsetUs) throws ExoPlaybackException { long offsetUs) throws ExoPlaybackException;
Assertions.checkState(state == STATE_DISABLED);
state = STATE_ENABLED;
onEnabled(joining);
replaceSampleStream(formats, stream, offsetUs);
onReset(positionUs, joining);
}
/**
* Called when the renderer is enabled.
* <p>
* The default implementation is a no-op.
*
* @param joining Whether this renderer is being enabled to join an ongoing playback.
* @throws ExoPlaybackException If an error occurs.
*/
protected void onEnabled(boolean joining) throws ExoPlaybackException {
// Do nothing.
}
/** /**
* Sets the {@link SampleStream} from which samples will be consumed. * Sets the {@link SampleStream} from which samples will be consumed.
@ -141,26 +102,8 @@ public abstract class Renderer implements ExoPlayerComponent, RendererCapabiliti
* they are rendered. * they are rendered.
* @throws ExoPlaybackException If an error occurs. * @throws ExoPlaybackException If an error occurs.
*/ */
/* package */ final void replaceSampleStream(Format[] formats, SampleStream stream, long offsetUs) void replaceStream(Format[] formats, SampleStream stream, long offsetUs)
throws ExoPlaybackException { throws ExoPlaybackException;
Assertions.checkState(!streamIsFinal);
this.stream = stream;
readEndOfStream = false;
streamOffsetUs = offsetUs;
onStreamChanged(formats);
}
/**
* Called when the renderer's stream has changed.
* <p>
* The default implementation is a no-op.
*
* @param formats The enabled formats.
* @throws ExoPlaybackException Thrown if an error occurs.
*/
protected void onStreamChanged(Format[] formats) throws ExoPlaybackException {
// Do nothing.
}
/** /**
* Called when a reset is encountered. * Called when a reset is encountered.
@ -168,39 +111,18 @@ public abstract class Renderer implements ExoPlayerComponent, RendererCapabiliti
* @param positionUs The playback position in microseconds. * @param positionUs The playback position in microseconds.
* @throws ExoPlaybackException If an error occurs handling the reset. * @throws ExoPlaybackException If an error occurs handling the reset.
*/ */
/* package */ final void reset(long positionUs) throws ExoPlaybackException { void reset(long positionUs) throws ExoPlaybackException;
streamIsFinal = false;
onReset(positionUs, false);
}
/**
* Invoked when a reset is encountered, and also when the renderer is enabled.
* <p>
* This method may be called when the renderer is in the following states:
* {@link #STATE_ENABLED}, {@link #STATE_STARTED}.
*
* @param positionUs The playback position in microseconds.
* @param joining Whether this renderer is being enabled to join an ongoing playback.
* @throws ExoPlaybackException If an error occurs handling the reset.
*/
protected void onReset(long positionUs, boolean joining) throws ExoPlaybackException {
// Do nothing.
}
/** /**
* Returns whether the renderer has read the current {@link SampleStream} to the end. * Returns whether the renderer has read the current {@link SampleStream} to the end.
*/ */
/* package */ final boolean hasReadStreamToEnd() { boolean hasReadStreamToEnd();
return readEndOfStream;
}
/** /**
* Signals to the renderer that the current {@link SampleStream} will be the final one supplied * Signals to the renderer that the current {@link SampleStream} will be the final one supplied
* before it is next disabled or reset. * before it is next disabled or reset.
*/ */
/* package */ final void setCurrentSampleStreamIsFinal() { void setCurrentStreamIsFinal();
streamIsFinal = true;
}
/** /**
* Starts the renderer, meaning that calls to {@link #render(long, long)} will cause media to be * Starts the renderer, meaning that calls to {@link #render(long, long)} will cause media to be
@ -208,66 +130,19 @@ public abstract class Renderer implements ExoPlayerComponent, RendererCapabiliti
* *
* @throws ExoPlaybackException If an error occurs. * @throws ExoPlaybackException If an error occurs.
*/ */
/* package */ final void start() throws ExoPlaybackException { void start() throws ExoPlaybackException;
Assertions.checkState(state == STATE_ENABLED);
state = STATE_STARTED;
onStarted();
}
/**
* Called when the renderer is started.
* <p>
* The default implementation is a no-op.
*
* @throws ExoPlaybackException If an error occurs.
*/
protected void onStarted() throws ExoPlaybackException {
// Do nothing.
}
/** /**
* Stops the renderer. * Stops the renderer.
* *
* @throws ExoPlaybackException If an error occurs. * @throws ExoPlaybackException If an error occurs.
*/ */
/* package */ final void stop() throws ExoPlaybackException { void stop() throws ExoPlaybackException;
Assertions.checkState(state == STATE_STARTED);
state = STATE_ENABLED;
onStopped();
}
/**
* Called when the renderer is stopped.
* <p>
* The default implementation is a no-op.
*
* @throws ExoPlaybackException If an error occurs.
*/
protected void onStopped() throws ExoPlaybackException {
// Do nothing.
}
/** /**
* Disable the renderer. * Disable the renderer.
*/ */
/* package */ final void disable() { void disable();
Assertions.checkState(state == STATE_ENABLED);
state = STATE_DISABLED;
onDisabled();
stream = null;
streamIsFinal = false;
}
/**
* Called when the renderer is disabled.
* <p>
* The default implementation is a no-op.
*/
protected void onDisabled() {
// Do nothing.
}
// Methods to be called by subclasses.
/** /**
* Throws an error that's preventing the renderer from reading from its {@link SampleStream}. Does * Throws an error that's preventing the renderer from reading from its {@link SampleStream}. Does
@ -279,37 +154,7 @@ public abstract class Renderer implements ExoPlayerComponent, RendererCapabiliti
* @throws IOException An error that's preventing the renderer from making progress or buffering * @throws IOException An error that's preventing the renderer from making progress or buffering
* more data. * more data.
*/ */
protected final void maybeThrowStreamError() throws IOException { void maybeThrowStreamError() throws IOException;
stream.maybeThrowError();
}
/**
* Reads from the enabled upstream source.
*
* @see SampleStream#readData(FormatHolder, DecoderInputBuffer)
*/
protected final int readSource(FormatHolder formatHolder, DecoderInputBuffer buffer) {
int result = stream.readData(formatHolder, buffer);
if (result == C.RESULT_BUFFER_READ) {
if (buffer.isEndOfStream()) {
readEndOfStream = true;
return streamIsFinal ? C.RESULT_BUFFER_READ : C.RESULT_NOTHING_READ;
}
buffer.timeUs += streamOffsetUs;
}
return result;
}
/**
* Returns whether the upstream source is ready.
*
* @return True if the source is ready. False otherwise.
*/
protected final boolean isSourceReady() {
return readEndOfStream ? streamIsFinal : stream.isReady();
}
// Abstract methods.
/** /**
* Incrementally renders the {@link SampleStream}. * Incrementally renders the {@link SampleStream}.
@ -326,8 +171,7 @@ public abstract class Renderer implements ExoPlayerComponent, RendererCapabiliti
* measured at the start of the current iteration of the rendering loop. * measured at the start of the current iteration of the rendering loop.
* @throws ExoPlaybackException If an error occurs. * @throws ExoPlaybackException If an error occurs.
*/ */
protected abstract void render(long positionUs, long elapsedRealtimeUs) void render(long positionUs, long elapsedRealtimeUs) throws ExoPlaybackException;
throws ExoPlaybackException;
/** /**
* Whether the renderer is able to immediately render media from the current position. * Whether the renderer is able to immediately render media from the current position.
@ -344,7 +188,7 @@ public abstract class Renderer implements ExoPlayerComponent, RendererCapabiliti
* *
* @return True if the renderer is ready to render media. False otherwise. * @return True if the renderer is ready to render media. False otherwise.
*/ */
protected abstract boolean isReady(); boolean isReady();
/** /**
* Whether the renderer is ready for the {@link ExoPlayer} instance to transition to * Whether the renderer is ready for the {@link ExoPlayer} instance to transition to
@ -356,20 +200,6 @@ public abstract class Renderer implements ExoPlayerComponent, RendererCapabiliti
* *
* @return Whether the renderer is ready for the player to transition to the ended state. * @return Whether the renderer is ready for the player to transition to the ended state.
*/ */
protected abstract boolean isEnded(); boolean isEnded();
// RendererCapabilities implementation
@Override
public int supportsMixedMimeTypeAdaptation() throws ExoPlaybackException {
return ADAPTIVE_NOT_SUPPORTED;
}
// ExoPlayerComponent implementation.
@Override
public void handleMessage(int what, Object object) throws ExoPlaybackException {
// Do nothing.
}
} }

View File

@ -18,7 +18,6 @@ package com.google.android.exoplayer2.audio;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.ExoPlaybackException; import com.google.android.exoplayer2.ExoPlaybackException;
import com.google.android.exoplayer2.Format; import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.Renderer;
import com.google.android.exoplayer2.audio.AudioRendererEventListener.EventDispatcher; import com.google.android.exoplayer2.audio.AudioRendererEventListener.EventDispatcher;
import com.google.android.exoplayer2.drm.DrmSessionManager; import com.google.android.exoplayer2.drm.DrmSessionManager;
import com.google.android.exoplayer2.mediacodec.MediaCodecInfo; import com.google.android.exoplayer2.mediacodec.MediaCodecInfo;
@ -210,7 +209,7 @@ public class MediaCodecAudioRenderer extends MediaCodecRenderer implements Media
} }
@Override @Override
protected MediaClock getMediaClock() { public MediaClock getMediaClock() {
return this; return this;
} }
@ -300,12 +299,12 @@ public class MediaCodecAudioRenderer extends MediaCodecRenderer implements Media
} }
@Override @Override
protected boolean isEnded() { public boolean isEnded() {
return super.isEnded() && !audioTrack.hasPendingData(); return super.isEnded() && !audioTrack.hasPendingData();
} }
@Override @Override
protected boolean isReady() { public boolean isReady() {
return audioTrack.hasPendingData() || super.isReady(); return audioTrack.hasPendingData() || super.isReady();
} }
@ -351,14 +350,14 @@ public class MediaCodecAudioRenderer extends MediaCodecRenderer implements Media
} catch (AudioTrack.InitializationException e) { } catch (AudioTrack.InitializationException e) {
throw ExoPlaybackException.createForRenderer(e, getIndex()); throw ExoPlaybackException.createForRenderer(e, getIndex());
} }
if (getState() == Renderer.STATE_STARTED) { if (getState() == STATE_STARTED) {
audioTrack.play(); audioTrack.play();
} }
} else { } else {
// Check for AudioTrack underrun. // Check for AudioTrack underrun.
boolean audioTrackHadData = audioTrackHasData; boolean audioTrackHadData = audioTrackHasData;
audioTrackHasData = audioTrack.hasPendingData(); audioTrackHasData = audioTrack.hasPendingData();
if (audioTrackHadData && !audioTrackHasData && getState() == Renderer.STATE_STARTED) { if (audioTrackHadData && !audioTrackHasData && getState() == STATE_STARTED) {
long elapsedSinceLastFeedMs = SystemClock.elapsedRealtime() - lastFeedElapsedRealtimeMs; long elapsedSinceLastFeedMs = SystemClock.elapsedRealtime() - lastFeedElapsedRealtimeMs;
long bufferSizeUs = audioTrack.getBufferSizeUs(); long bufferSizeUs = audioTrack.getBufferSizeUs();
long bufferSizeMs = bufferSizeUs == C.UNSET_TIME_US ? -1 : bufferSizeUs / 1000; long bufferSizeMs = bufferSizeUs == C.UNSET_TIME_US ? -1 : bufferSizeUs / 1000;

View File

@ -15,11 +15,11 @@
*/ */
package com.google.android.exoplayer2.audio; package com.google.android.exoplayer2.audio;
import com.google.android.exoplayer2.BaseRenderer;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.ExoPlaybackException; import com.google.android.exoplayer2.ExoPlaybackException;
import com.google.android.exoplayer2.Format; import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.FormatHolder; import com.google.android.exoplayer2.FormatHolder;
import com.google.android.exoplayer2.Renderer;
import com.google.android.exoplayer2.audio.AudioRendererEventListener.EventDispatcher; import com.google.android.exoplayer2.audio.AudioRendererEventListener.EventDispatcher;
import com.google.android.exoplayer2.decoder.DecoderCounters; import com.google.android.exoplayer2.decoder.DecoderCounters;
import com.google.android.exoplayer2.decoder.DecoderInputBuffer; import com.google.android.exoplayer2.decoder.DecoderInputBuffer;
@ -37,7 +37,7 @@ import android.os.SystemClock;
/** /**
* Decodes and renders audio using a {@link SimpleDecoder}. * Decodes and renders audio using a {@link SimpleDecoder}.
*/ */
public abstract class SimpleDecoderAudioRenderer extends Renderer implements MediaClock { public abstract class SimpleDecoderAudioRenderer extends BaseRenderer implements MediaClock {
private final EventDispatcher eventDispatcher; private final EventDispatcher eventDispatcher;
private final FormatHolder formatHolder; private final FormatHolder formatHolder;
@ -92,12 +92,12 @@ public abstract class SimpleDecoderAudioRenderer extends Renderer implements Med
} }
@Override @Override
protected MediaClock getMediaClock() { public MediaClock getMediaClock() {
return this; return this;
} }
@Override @Override
protected void render(long positionUs, long elapsedRealtimeUs) throws ExoPlaybackException { public void render(long positionUs, long elapsedRealtimeUs) throws ExoPlaybackException {
if (outputStreamEnded) { if (outputStreamEnded) {
return; return;
} }
@ -192,14 +192,14 @@ public abstract class SimpleDecoderAudioRenderer extends Renderer implements Med
onAudioSessionId(audioSessionId); onAudioSessionId(audioSessionId);
} }
audioTrackHasData = false; audioTrackHasData = false;
if (getState() == Renderer.STATE_STARTED) { if (getState() == STATE_STARTED) {
audioTrack.play(); audioTrack.play();
} }
} else { } else {
// Check for AudioTrack underrun. // Check for AudioTrack underrun.
boolean audioTrackHadData = audioTrackHasData; boolean audioTrackHadData = audioTrackHasData;
audioTrackHasData = audioTrack.hasPendingData(); audioTrackHasData = audioTrack.hasPendingData();
if (audioTrackHadData && !audioTrackHasData && getState() == Renderer.STATE_STARTED) { if (audioTrackHadData && !audioTrackHasData && getState() == STATE_STARTED) {
long elapsedSinceLastFeedMs = SystemClock.elapsedRealtime() - lastFeedElapsedRealtimeMs; long elapsedSinceLastFeedMs = SystemClock.elapsedRealtime() - lastFeedElapsedRealtimeMs;
long bufferSizeUs = audioTrack.getBufferSizeUs(); long bufferSizeUs = audioTrack.getBufferSizeUs();
long bufferSizeMs = bufferSizeUs == C.UNSET_TIME_US ? -1 : bufferSizeUs / 1000; long bufferSizeMs = bufferSizeUs == C.UNSET_TIME_US ? -1 : bufferSizeUs / 1000;
@ -270,12 +270,12 @@ public abstract class SimpleDecoderAudioRenderer extends Renderer implements Med
} }
@Override @Override
protected boolean isEnded() { public boolean isEnded() {
return outputStreamEnded && !audioTrack.hasPendingData(); return outputStreamEnded && !audioTrack.hasPendingData();
} }
@Override @Override
protected boolean isReady() { public boolean isReady() {
return audioTrack.hasPendingData() return audioTrack.hasPendingData()
|| (inputFormat != null && (isSourceReady() || outputBuffer != null)); || (inputFormat != null && (isSourceReady() || outputBuffer != null));
} }

View File

@ -15,11 +15,11 @@
*/ */
package com.google.android.exoplayer2.mediacodec; package com.google.android.exoplayer2.mediacodec;
import com.google.android.exoplayer2.BaseRenderer;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.ExoPlaybackException; import com.google.android.exoplayer2.ExoPlaybackException;
import com.google.android.exoplayer2.Format; import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.FormatHolder; import com.google.android.exoplayer2.FormatHolder;
import com.google.android.exoplayer2.Renderer;
import com.google.android.exoplayer2.decoder.DecoderCounters; import com.google.android.exoplayer2.decoder.DecoderCounters;
import com.google.android.exoplayer2.decoder.DecoderInputBuffer; import com.google.android.exoplayer2.decoder.DecoderInputBuffer;
import com.google.android.exoplayer2.drm.DrmSession; import com.google.android.exoplayer2.drm.DrmSession;
@ -46,10 +46,10 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
/** /**
* An abstract {@link Renderer} that uses {@link MediaCodec} to decode samples for rendering. * An abstract renderer that uses {@link MediaCodec} to decode samples for rendering.
*/ */
@TargetApi(16) @TargetApi(16)
public abstract class MediaCodecRenderer extends Renderer { public abstract class MediaCodecRenderer extends BaseRenderer {
/** /**
* Thrown when a failure occurs instantiating a decoder. * Thrown when a failure occurs instantiating a decoder.
@ -361,7 +361,7 @@ public abstract class MediaCodecRenderer extends Renderer {
throwDecoderInitError(new DecoderInitializationException(format, e, throwDecoderInitError(new DecoderInitializationException(format, e,
drmSessionRequiresSecureDecoder, codecName)); drmSessionRequiresSecureDecoder, codecName));
} }
codecHotswapDeadlineMs = getState() == Renderer.STATE_STARTED codecHotswapDeadlineMs = getState() == STATE_STARTED
? (SystemClock.elapsedRealtime() + MAX_CODEC_HOTSWAP_TIME_MS) : -1; ? (SystemClock.elapsedRealtime() + MAX_CODEC_HOTSWAP_TIME_MS) : -1;
inputIndex = -1; inputIndex = -1;
outputIndex = -1; outputIndex = -1;
@ -452,7 +452,7 @@ public abstract class MediaCodecRenderer extends Renderer {
} }
@Override @Override
protected void render(long positionUs, long elapsedRealtimeUs) throws ExoPlaybackException { public void render(long positionUs, long elapsedRealtimeUs) throws ExoPlaybackException {
if (format == null) { if (format == null) {
readFormat(); readFormat();
} }
@ -780,12 +780,12 @@ public abstract class MediaCodecRenderer extends Renderer {
} }
@Override @Override
protected boolean isEnded() { public boolean isEnded() {
return outputStreamEnded; return outputStreamEnded;
} }
@Override @Override
protected boolean isReady() { public boolean isReady() {
return format != null && !waitingForKeys && (isSourceReady() || outputIndex >= 0 return format != null && !waitingForKeys && (isSourceReady() || outputIndex >= 0
|| (SystemClock.elapsedRealtime() < codecHotswapDeadlineMs)); || (SystemClock.elapsedRealtime() < codecHotswapDeadlineMs));
} }

View File

@ -20,6 +20,7 @@ import com.google.android.exoplayer2.ExoPlaybackException;
import com.google.android.exoplayer2.Format; import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.FormatHolder; import com.google.android.exoplayer2.FormatHolder;
import com.google.android.exoplayer2.Renderer; import com.google.android.exoplayer2.Renderer;
import com.google.android.exoplayer2.BaseRenderer;
import com.google.android.exoplayer2.decoder.DecoderInputBuffer; import com.google.android.exoplayer2.decoder.DecoderInputBuffer;
import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.Assertions;
@ -35,7 +36,7 @@ import java.nio.ByteBuffer;
* *
* @param <T> The type of the metadata. * @param <T> The type of the metadata.
*/ */
public final class MetadataRenderer<T> extends Renderer implements Callback { public final class MetadataRenderer<T> extends BaseRenderer implements Callback {
/** /**
* An output for the renderer. * An output for the renderer.
@ -90,8 +91,8 @@ public final class MetadataRenderer<T> extends Renderer implements Callback {
@Override @Override
public int supportsFormat(Format format) { public int supportsFormat(Format format) {
return metadataDecoder.canDecode(format.sampleMimeType) ? Renderer.FORMAT_HANDLED return metadataDecoder.canDecode(format.sampleMimeType) ? FORMAT_HANDLED
: Renderer.FORMAT_UNSUPPORTED_TYPE; : FORMAT_UNSUPPORTED_TYPE;
} }
@Override @Override
@ -101,7 +102,7 @@ public final class MetadataRenderer<T> extends Renderer implements Callback {
} }
@Override @Override
protected void render(long positionUs, long elapsedRealtimeUs) throws ExoPlaybackException { public void render(long positionUs, long elapsedRealtimeUs) throws ExoPlaybackException {
if (!inputStreamEnded && pendingMetadata == null) { if (!inputStreamEnded && pendingMetadata == null) {
buffer.clear(); buffer.clear();
int result = readSource(formatHolder, buffer); int result = readSource(formatHolder, buffer);
@ -134,12 +135,12 @@ public final class MetadataRenderer<T> extends Renderer implements Callback {
} }
@Override @Override
protected boolean isEnded() { public boolean isEnded() {
return inputStreamEnded; return inputStreamEnded;
} }
@Override @Override
protected boolean isReady() { public boolean isReady() {
return true; return true;
} }

View File

@ -15,11 +15,11 @@
*/ */
package com.google.android.exoplayer2.text; package com.google.android.exoplayer2.text;
import com.google.android.exoplayer2.BaseRenderer;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.ExoPlaybackException; import com.google.android.exoplayer2.ExoPlaybackException;
import com.google.android.exoplayer2.Format; import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.FormatHolder; import com.google.android.exoplayer2.FormatHolder;
import com.google.android.exoplayer2.Renderer;
import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.MimeTypes; import com.google.android.exoplayer2.util.MimeTypes;
@ -33,14 +33,14 @@ import java.util.Collections;
import java.util.List; import java.util.List;
/** /**
* A {@link Renderer} for subtitles. * A renderer for subtitles.
* <p> * <p>
* Text is parsed from sample data using {@link SubtitleDecoder} instances obtained from a * Text is parsed from sample data using {@link SubtitleDecoder} instances obtained from a
* {@link SubtitleDecoderFactory}. The actual rendering of each line of text is delegated to a * {@link SubtitleDecoderFactory}. The actual rendering of each line of text is delegated to a
* {@link Output}. * {@link Output}.
*/ */
@TargetApi(16) @TargetApi(16)
public final class TextRenderer extends Renderer implements Callback { public final class TextRenderer extends BaseRenderer implements Callback {
/** /**
* An output for the renderer. * An output for the renderer.
@ -106,7 +106,7 @@ public final class TextRenderer extends Renderer implements Callback {
@Override @Override
public int supportsFormat(Format format) { public int supportsFormat(Format format) {
return decoderFactory.supportsFormat(format) ? Renderer.FORMAT_HANDLED return decoderFactory.supportsFormat(format) ? FORMAT_HANDLED
: (MimeTypes.isText(format.sampleMimeType) ? FORMAT_UNSUPPORTED_SUBTYPE : (MimeTypes.isText(format.sampleMimeType) ? FORMAT_UNSUPPORTED_SUBTYPE
: FORMAT_UNSUPPORTED_TYPE); : FORMAT_UNSUPPORTED_TYPE);
} }
@ -137,7 +137,7 @@ public final class TextRenderer extends Renderer implements Callback {
} }
@Override @Override
protected void render(long positionUs, long elapsedRealtimeUs) throws ExoPlaybackException { public void render(long positionUs, long elapsedRealtimeUs) throws ExoPlaybackException {
if (outputStreamEnded) { if (outputStreamEnded) {
return; return;
} }
@ -151,7 +151,7 @@ public final class TextRenderer extends Renderer implements Callback {
} }
} }
if (getState() != Renderer.STATE_STARTED) { if (getState() != STATE_STARTED) {
return; return;
} }
@ -237,12 +237,12 @@ public final class TextRenderer extends Renderer implements Callback {
} }
@Override @Override
protected boolean isEnded() { public boolean isEnded() {
return outputStreamEnded; return outputStreamEnded;
} }
@Override @Override
protected boolean isReady() { public boolean isReady() {
// Don't block playback whilst subtitles are loading. // Don't block playback whilst subtitles are loading.
// Note: To change this behavior, it will be necessary to consider [Internal: b/12949941]. // Note: To change this behavior, it will be necessary to consider [Internal: b/12949941].
return true; return true;

View File

@ -244,7 +244,7 @@ public class MediaCodecVideoRenderer extends MediaCodecRenderer {
} }
@Override @Override
protected boolean isReady() { public boolean isReady() {
if (renderedFirstFrame && super.isReady()) { if (renderedFirstFrame && super.isReady()) {
// Ready. If we were joining then we've now joined, so clear the joining deadline. // Ready. If we were joining then we've now joined, so clear the joining deadline.
joiningDeadlineMs = -1; joiningDeadlineMs = -1;

View File

@ -18,7 +18,6 @@ package com.google.android.exoplayer2.playbacktests.gts;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.ExoPlaybackException; import com.google.android.exoplayer2.ExoPlaybackException;
import com.google.android.exoplayer2.ExoPlayer; import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.Renderer;
import com.google.android.exoplayer2.RendererCapabilities; import com.google.android.exoplayer2.RendererCapabilities;
import com.google.android.exoplayer2.decoder.DecoderCounters; import com.google.android.exoplayer2.decoder.DecoderCounters;
import com.google.android.exoplayer2.mediacodec.MediaCodecInfo; import com.google.android.exoplayer2.mediacodec.MediaCodecInfo;
@ -535,8 +534,7 @@ public final class DashTest extends ActivityInstrumentationTestCase2<HostActivit
// Select additional video representations, if supported by the device. // Select additional video representations, if supported by the device.
if (canIncludeAdditionalFormats) { if (canIncludeAdditionalFormats) {
for (int i = 0; i < trackGroup.length; i++) { for (int i = 0; i < trackGroup.length; i++) {
if (!trackIndices.contains(i) && (formatSupport[i] & Renderer.FORMAT_SUPPORT_MASK) if (!trackIndices.contains(i) && isFormatHandled(formatSupport[i])) {
== Renderer.FORMAT_HANDLED) {
Log.d(TAG, "Adding video format: " + trackGroup.getFormat(i).id); Log.d(TAG, "Adding video format: " + trackGroup.getFormat(i).id);
trackIndices.add(i); trackIndices.add(i);
} }
@ -548,6 +546,11 @@ public final class DashTest extends ActivityInstrumentationTestCase2<HostActivit
return trackIndicesArray; return trackIndicesArray;
} }
private static final boolean isFormatHandled(int formatSupport) {
return (formatSupport & RendererCapabilities.FORMAT_SUPPORT_MASK)
== RendererCapabilities.FORMAT_HANDLED;
}
} }
} }