Commit c9591d76 by hoangtc Committed by Oliver Woodman

Added support for No-Sample Renderer.

Currently our Renderer is always associated with and consume data from
some SampleStreams, which were constructed from the provided MediaSource.
There are use-cases, in which the users want to have simple Renderer
implementation that does not consume data from SampleStream at all, but
render using their custom logic at each rendering position - they mostly just
need ExoPlayer to keep track of the playback position and enable/disable the
renderer.
This CL adds support for such Renderer by adding a TRACK_TYPE_NONE.
Renderer of such type will be:
- Associated with null TrackSelection as the result of track-selection
operation.
- Associated with EmptySampleStream.

GitHub: #3212

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=168545749
parent 5019da3e
...@@ -520,6 +520,10 @@ public final class C { ...@@ -520,6 +520,10 @@ public final class C {
*/ */
public static final int TRACK_TYPE_METADATA = 4; public static final int TRACK_TYPE_METADATA = 4;
/** /**
* A type constant for a dummy or empty track.
*/
public static final int TRACK_TYPE_NONE = 5;
/**
* Applications or extensions may define custom {@code TRACK_TYPE_*} constants greater than or * Applications or extensions may define custom {@code TRACK_TYPE_*} constants greater than or
* equal to this value. * equal to this value.
*/ */
......
/*
* 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.source.SampleStream;
import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.MediaClock;
import java.io.IOException;
/**
* A {@link Renderer} implementation whose track type is {@link C#TRACK_TYPE_NONE} and does not
* consume data from its {@link SampleStream}.
*/
public abstract class NoSampleRenderer implements Renderer, RendererCapabilities {
private RendererConfiguration configuration;
private int index;
private int state;
private SampleStream stream;
private boolean streamIsFinal;
@Override
public final int getTrackType() {
return C.TRACK_TYPE_NONE;
}
@Override
public final RendererCapabilities getCapabilities() {
return this;
}
@Override
public final void setIndex(int index) {
this.index = index;
}
@Override
public MediaClock getMediaClock() {
return null;
}
@Override
public final int getState() {
return state;
}
/**
* Replaces the {@link SampleStream} that will be associated with this renderer.
* <p>
* This method may be called when the renderer is in the following states:
* {@link #STATE_DISABLED}.
*
* @param configuration The renderer configuration.
* @param formats The enabled formats. Should be empty.
* @param stream The {@link SampleStream} from which the renderer should consume.
* @param positionUs The player's current position.
* @param joining Whether this renderer is being enabled to join an ongoing playback.
* @param offsetUs The offset that should be subtracted from {@code positionUs}
* to get the playback position with respect to the media.
* @throws ExoPlaybackException If an error occurs.
*/
@Override
public final void enable(RendererConfiguration configuration, Format[] formats,
SampleStream stream, long positionUs, boolean joining, long offsetUs)
throws ExoPlaybackException {
Assertions.checkState(state == STATE_DISABLED);
this.configuration = configuration;
state = STATE_ENABLED;
onEnabled(joining);
replaceStream(formats, stream, offsetUs);
onPositionReset(positionUs, joining);
}
@Override
public final void start() throws ExoPlaybackException {
Assertions.checkState(state == STATE_ENABLED);
state = STATE_STARTED;
onStarted();
}
/**
* Replaces the {@link SampleStream} that will be associated with this renderer.
* <p>
* This method may be called when the renderer is in the following states:
* {@link #STATE_ENABLED}, {@link #STATE_STARTED}.
*
* @param formats The enabled formats. Should be empty.
* @param stream The {@link SampleStream} to be associated with this renderer.
* @param offsetUs The offset that should be subtracted from {@code positionUs} in
* {@link #render(long, long)} to get the playback position with respect to the media.
* @throws ExoPlaybackException If an error occurs.
*/
@Override
public final void replaceStream(Format[] formats, SampleStream stream, long offsetUs)
throws ExoPlaybackException {
Assertions.checkState(!streamIsFinal);
this.stream = stream;
onRendererOffsetChanged(offsetUs);
}
@Override
public final SampleStream getStream() {
return stream;
}
@Override
public final boolean hasReadStreamToEnd() {
return true;
}
@Override
public final void setCurrentStreamFinal() {
streamIsFinal = true;
}
@Override
public final boolean isCurrentStreamFinal() {
return streamIsFinal;
}
@Override
public final void maybeThrowStreamError() throws IOException {
}
@Override
public final void resetPosition(long positionUs) throws ExoPlaybackException {
streamIsFinal = false;
onPositionReset(positionUs, false);
}
@Override
public final void stop() throws ExoPlaybackException {
Assertions.checkState(state == STATE_STARTED);
state = STATE_ENABLED;
onStopped();
}
@Override
public final void disable() {
Assertions.checkState(state == STATE_ENABLED);
state = STATE_DISABLED;
stream = null;
streamIsFinal = false;
onDisabled();
}
@Override
public boolean isReady() {
return true;
}
@Override
public boolean isEnded() {
return true;
}
// RendererCapabilities implementation.
@Override
public int supportsFormat(Format format) throws ExoPlaybackException {
return FORMAT_UNSUPPORTED_TYPE;
}
@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 overridden by subclasses.
/**
* 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.
}
/**
* Called when the renderer's offset has been changed.
* <p>
* The default implementation is a no-op.
*
* @param offsetUs The offset that should be subtracted from {@code positionUs} in
* {@link #render(long, long)} to get the playback position with respect to the media.
* @throws ExoPlaybackException If an error occurs.
*/
protected void onRendererOffsetChanged(long offsetUs) throws ExoPlaybackException {
// Do nothing.
}
/**
* Called when the position is reset. This occurs when the renderer is enabled after
* {@link #onRendererOffsetChanged(long)} has been called, and also when a position
* discontinuity is encountered.
* <p>
* The default implementation is a no-op.
*
* @param positionUs The new playback position in microseconds.
* @param joining Whether this renderer is being enabled to join an ongoing playback.
* @throws ExoPlaybackException If an error occurs.
*/
protected void onPositionReset(long positionUs, boolean joining) throws ExoPlaybackException {
// Do nothing.
}
/**
* 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.
}
/**
* 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.
}
/**
* 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.
/**
* Returns the configuration set when the renderer was most recently enabled.
*/
protected final RendererConfiguration getConfiguration() {
return configuration;
}
/**
* Returns the index of the renderer within the player.
*/
protected final int getIndex() {
return index;
}
}
...@@ -573,6 +573,8 @@ public abstract class MappingTrackSelector extends TrackSelector { ...@@ -573,6 +573,8 @@ public abstract class MappingTrackSelector extends TrackSelector {
} }
} }
boolean[] rendererEnabled = determineEnabledRenderers(rendererCapabilities, trackSelections);
// Package up the track information and selections. // Package up the track information and selections.
MappedTrackInfo mappedTrackInfo = new MappedTrackInfo(rendererTrackTypes, MappedTrackInfo mappedTrackInfo = new MappedTrackInfo(rendererTrackTypes,
rendererTrackGroupArrays, mixedMimeTypeAdaptationSupport, rendererFormatSupports, rendererTrackGroupArrays, mixedMimeTypeAdaptationSupport, rendererFormatSupports,
...@@ -583,14 +585,26 @@ public abstract class MappingTrackSelector extends TrackSelector { ...@@ -583,14 +585,26 @@ public abstract class MappingTrackSelector extends TrackSelector {
RendererConfiguration[] rendererConfigurations = RendererConfiguration[] rendererConfigurations =
new RendererConfiguration[rendererCapabilities.length]; new RendererConfiguration[rendererCapabilities.length];
for (int i = 0; i < rendererCapabilities.length; i++) { for (int i = 0; i < rendererCapabilities.length; i++) {
rendererConfigurations[i] = trackSelections[i] != null ? RendererConfiguration.DEFAULT : null; rendererConfigurations[i] = rendererEnabled[i] ? RendererConfiguration.DEFAULT : null;
} }
// Configure audio and video renderers to use tunneling if appropriate. // Configure audio and video renderers to use tunneling if appropriate.
maybeConfigureRenderersForTunneling(rendererCapabilities, rendererTrackGroupArrays, maybeConfigureRenderersForTunneling(rendererCapabilities, rendererTrackGroupArrays,
rendererFormatSupports, rendererConfigurations, trackSelections, tunnelingAudioSessionId); rendererFormatSupports, rendererConfigurations, trackSelections, tunnelingAudioSessionId);
return new TrackSelectorResult(trackGroups, new TrackSelectionArray(trackSelections), return new TrackSelectorResult(trackGroups, rendererEnabled,
mappedTrackInfo, rendererConfigurations); new TrackSelectionArray(trackSelections), mappedTrackInfo, rendererConfigurations);
}
private boolean[] determineEnabledRenderers(RendererCapabilities[] rendererCapabilities,
TrackSelection[] trackSelections) {
boolean[] rendererEnabled = new boolean[trackSelections.length];
for (int i = 0; i < rendererEnabled.length; i++) {
boolean forceRendererDisabled = rendererDisabledFlags.get(i);
rendererEnabled[i] = !forceRendererDisabled
&& (rendererCapabilities[i].getTrackType() == C.TRACK_TYPE_NONE
|| trackSelections[i] != null);
}
return rendererEnabled;
} }
@Override @Override
......
...@@ -29,6 +29,10 @@ public final class TrackSelectorResult { ...@@ -29,6 +29,10 @@ public final class TrackSelectorResult {
*/ */
public final TrackGroupArray groups; public final TrackGroupArray groups;
/** /**
* An array containing whether each renderer is enabled after the track selection operation.
*/
public final boolean[] renderersEnabled;
/**
* A {@link TrackSelectionArray} containing the track selection for each renderer. * A {@link TrackSelectionArray} containing the track selection for each renderer.
*/ */
public final TrackSelectionArray selections; public final TrackSelectionArray selections;
...@@ -38,21 +42,25 @@ public final class TrackSelectorResult { ...@@ -38,21 +42,25 @@ public final class TrackSelectorResult {
*/ */
public final Object info; public final Object info;
/** /**
* A {@link RendererConfiguration} for each renderer, to be used with the selections. * A {@link RendererConfiguration} for each enabled renderer, to be used with the selections.
*/ */
public final RendererConfiguration[] rendererConfigurations; public final RendererConfiguration[] rendererConfigurations;
/** /**
* @param groups The track groups provided to the {@link TrackSelector}. * @param groups The track groups provided to the {@link TrackSelector}.
* @param renderersEnabled An array containing whether each renderer is enabled after the track
* selection operation.
* @param selections A {@link TrackSelectionArray} containing the selection for each renderer. * @param selections A {@link TrackSelectionArray} containing the selection for each renderer.
* @param info An opaque object that will be returned to * @param info An opaque object that will be returned to
* {@link TrackSelector#onSelectionActivated(Object)} should the selection be activated. * {@link TrackSelector#onSelectionActivated(Object)} should the selection be activated.
* @param rendererConfigurations A {@link RendererConfiguration} for each renderer, to be used * @param rendererConfigurations A {@link RendererConfiguration} for each enabled renderer,
* with the selections. * to be used with the selections.
*/ */
public TrackSelectorResult(TrackGroupArray groups, TrackSelectionArray selections, Object info, public TrackSelectorResult(TrackGroupArray groups, boolean[] renderersEnabled,
TrackSelectionArray selections, Object info,
RendererConfiguration[] rendererConfigurations) { RendererConfiguration[] rendererConfigurations) {
this.groups = groups; this.groups = groups;
this.renderersEnabled = renderersEnabled;
this.selections = selections; this.selections = selections;
this.info = info; this.info = info;
this.rendererConfigurations = rendererConfigurations; this.rendererConfigurations = rendererConfigurations;
...@@ -79,8 +87,8 @@ public final class TrackSelectorResult { ...@@ -79,8 +87,8 @@ public final class TrackSelectorResult {
/** /**
* Returns whether this result is equivalent to {@code other} for the renderer at the given index. * Returns whether this result is equivalent to {@code other} for the renderer at the given index.
* The results are equivalent if they have equal track selections and configurations for the * The results are equivalent if they have equal renderersEnabled array, track selections, and
* renderer. * configurations for the renderer.
* *
* @param other The other {@link TrackSelectorResult}. May be null, in which case {@code false} * @param other The other {@link TrackSelectorResult}. May be null, in which case {@code false}
* will be returned. * will be returned.
...@@ -92,7 +100,8 @@ public final class TrackSelectorResult { ...@@ -92,7 +100,8 @@ public final class TrackSelectorResult {
if (other == null) { if (other == null) {
return false; return false;
} }
return Util.areEqual(selections.get(index), other.selections.get(index)) return renderersEnabled[index] == other.renderersEnabled[index]
&& Util.areEqual(selections.get(index), other.selections.get(index))
&& Util.areEqual(rendererConfigurations[index], other.rendererConfigurations[index]); && Util.areEqual(rendererConfigurations[index], other.rendererConfigurations[index]);
} }
......
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