mirror of
https://github.com/samsonjs/media.git
synced 2026-03-25 09:25:53 +00:00
Clean up SimpleExoPlayer.
- Simplify setSurface by always blocking when the surface is being cleared. - Add analogous setVolume API. - Support setting of playback params for extension audio decoders. We can probably use this to add a setPlaybackRate API to SimpleExoPlayer for API level 23 and above only, but we'd probably want to make sure it works properly when there's no audio track too, so some thought will be required. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=123204581
This commit is contained in:
parent
7d229003ad
commit
35d7dad047
5 changed files with 52 additions and 52 deletions
|
|
@ -553,7 +553,7 @@ public class PlayerActivity extends Activity implements SurfaceHolder.Callback,
|
|||
@Override
|
||||
public void surfaceDestroyed(SurfaceHolder holder) {
|
||||
if (player != null) {
|
||||
player.blockingClearSurface();
|
||||
player.setSurface(null);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -230,10 +230,26 @@ public interface C {
|
|||
UUID PLAYREADY_UUID = new UUID(0x9A04F07998404286L, 0xAB92E65BE0885F95L);
|
||||
|
||||
/**
|
||||
* The type of a message that can be passed to an video {@link TrackRenderer} via
|
||||
* The type of a message that can be passed to a video {@link TrackRenderer} via
|
||||
* {@link ExoPlayer#sendMessage} or {@link ExoPlayer#blockingSendMessage}. The message object
|
||||
* should be the target {@link Surface}, or null.
|
||||
*/
|
||||
int MSG_SET_SURFACE = 1;
|
||||
|
||||
/**
|
||||
* The type of a message that can be passed to an audio {@link TrackRenderer} via
|
||||
* {@link ExoPlayer#sendMessage} or {@link ExoPlayer#blockingSendMessage}. The message object
|
||||
* should be a {@link Float} with 0 being silence and 1 being unity gain.
|
||||
*/
|
||||
int MSG_SET_VOLUME = 1;
|
||||
|
||||
/**
|
||||
* The type of a message that can be passed to an audio {@link TrackRenderer} via
|
||||
* {@link ExoPlayer#sendMessage} or {@link ExoPlayer#blockingSendMessage}. The message object
|
||||
* should be a {@link android.media.PlaybackParams}, which will be used to configure the
|
||||
* underlying {@link android.media.AudioTrack}. The message object should not be modified by the
|
||||
* caller after it has been passed
|
||||
*/
|
||||
int MSG_SET_PLAYBACK_PARAMS = 2;
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -41,22 +41,6 @@ import java.nio.ByteBuffer;
|
|||
@TargetApi(16)
|
||||
public class MediaCodecAudioTrackRenderer extends MediaCodecTrackRenderer implements MediaClock {
|
||||
|
||||
/**
|
||||
* The type of a message that can be passed to an instance of this class via
|
||||
* {@link ExoPlayer#sendMessage} or {@link ExoPlayer#blockingSendMessage}. The message object
|
||||
* should be a {@link Float} with 0 being silence and 1 being unity gain.
|
||||
*/
|
||||
public static final int MSG_SET_VOLUME = 1;
|
||||
|
||||
/**
|
||||
* The type of a message that can be passed to an instance of this class via
|
||||
* {@link ExoPlayer#sendMessage} or {@link ExoPlayer#blockingSendMessage}. The message object
|
||||
* should be a {@link android.media.PlaybackParams}, which will be used to configure the
|
||||
* underlying {@link android.media.AudioTrack}. The message object should not be modified by the
|
||||
* caller after it has been passed
|
||||
*/
|
||||
public static final int MSG_SET_PLAYBACK_PARAMS = 2;
|
||||
|
||||
private final EventDispatcher eventDispatcher;
|
||||
private final AudioTrack audioTrack;
|
||||
|
||||
|
|
@ -411,10 +395,10 @@ public class MediaCodecAudioTrackRenderer extends MediaCodecTrackRenderer implem
|
|||
@Override
|
||||
public void handleMessage(int messageType, Object message) throws ExoPlaybackException {
|
||||
switch (messageType) {
|
||||
case MSG_SET_VOLUME:
|
||||
case C.MSG_SET_VOLUME:
|
||||
audioTrack.setVolume((Float) message);
|
||||
break;
|
||||
case MSG_SET_PLAYBACK_PARAMS:
|
||||
case C.MSG_SET_PLAYBACK_PARAMS:
|
||||
audioTrack.setPlaybackParams((PlaybackParams) message);
|
||||
break;
|
||||
default:
|
||||
|
|
|
|||
|
|
@ -27,6 +27,7 @@ import com.google.android.exoplayer.text.TextTrackRenderer;
|
|||
import com.google.android.exoplayer.upstream.BandwidthMeter;
|
||||
import com.google.android.exoplayer.upstream.DefaultBandwidthMeter;
|
||||
|
||||
import android.annotation.TargetApi;
|
||||
import android.content.Context;
|
||||
import android.media.AudioManager;
|
||||
import android.media.MediaCodec;
|
||||
|
|
@ -43,6 +44,7 @@ import java.util.List;
|
|||
* <p>
|
||||
* Instances of this class can be obtained from {@link ExoPlayerFactory}.
|
||||
*/
|
||||
@TargetApi(16)
|
||||
public final class SimpleExoPlayer implements ExoPlayer {
|
||||
|
||||
/**
|
||||
|
|
@ -88,7 +90,6 @@ public final class SimpleExoPlayer implements ExoPlayer {
|
|||
private final ComponentListener componentListener;
|
||||
private final Handler mainHandler;
|
||||
|
||||
private Surface surface;
|
||||
private Format videoFormat;
|
||||
private Format audioFormat;
|
||||
|
||||
|
|
@ -144,16 +145,29 @@ public final class SimpleExoPlayer implements ExoPlayer {
|
|||
* @param surface The {@link Surface}.
|
||||
*/
|
||||
public void setSurface(Surface surface) {
|
||||
this.surface = surface;
|
||||
pushSurface(false);
|
||||
for (TrackRenderer renderer : renderers) {
|
||||
if (renderer.getTrackType() == C.TRACK_TYPE_VIDEO) {
|
||||
if (surface == null) {
|
||||
// Block to ensure that the surface is not accessed after the method returns.
|
||||
player.blockingSendMessage(renderer, C.MSG_SET_SURFACE, null);
|
||||
} else {
|
||||
player.sendMessage(renderer, C.MSG_SET_SURFACE, surface);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears the {@link Surface} onto which video will be rendered.
|
||||
* Sets the audio volume, with 0 being silence and 1 being unity gain.
|
||||
*
|
||||
* @param volume The volume.
|
||||
*/
|
||||
public void blockingClearSurface() {
|
||||
surface = null;
|
||||
pushSurface(true);
|
||||
public void setVolume(float volume) {
|
||||
for (TrackRenderer renderer : renderers) {
|
||||
if (renderer.getTrackType() == C.TRACK_TYPE_AUDIO) {
|
||||
player.sendMessage(renderer, C.MSG_SET_VOLUME, volume);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -278,7 +292,6 @@ public final class SimpleExoPlayer implements ExoPlayer {
|
|||
|
||||
@Override
|
||||
public void release() {
|
||||
surface = null;
|
||||
player.release();
|
||||
}
|
||||
|
||||
|
|
@ -314,18 +327,6 @@ public final class SimpleExoPlayer implements ExoPlayer {
|
|||
|
||||
// Internal methods.
|
||||
|
||||
private void pushSurface(boolean blockForSurfacePush) {
|
||||
for (TrackRenderer renderer : renderers) {
|
||||
if (renderer.getTrackType() == C.TRACK_TYPE_VIDEO) {
|
||||
if (blockForSurfacePush) {
|
||||
player.blockingSendMessage(renderer, C.MSG_SET_SURFACE, surface);
|
||||
} else {
|
||||
player.sendMessage(renderer, C.MSG_SET_SURFACE, surface);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void buildRenderers(Context context, DrmSessionManager drmSessionManager,
|
||||
ArrayList<TrackRenderer> renderersList) {
|
||||
MediaCodecVideoTrackRenderer videoRenderer = new MediaCodecVideoTrackRenderer(context,
|
||||
|
|
|
|||
|
|
@ -21,7 +21,6 @@ import com.google.android.exoplayer.C;
|
|||
import com.google.android.exoplayer.CodecCounters;
|
||||
import com.google.android.exoplayer.DecoderInputBuffer;
|
||||
import com.google.android.exoplayer.ExoPlaybackException;
|
||||
import com.google.android.exoplayer.ExoPlayer;
|
||||
import com.google.android.exoplayer.Format;
|
||||
import com.google.android.exoplayer.FormatHolder;
|
||||
import com.google.android.exoplayer.MediaClock;
|
||||
|
|
@ -30,6 +29,7 @@ import com.google.android.exoplayer.TrackStream;
|
|||
import com.google.android.exoplayer.audio.AudioTrack;
|
||||
import com.google.android.exoplayer.util.MimeTypes;
|
||||
|
||||
import android.media.PlaybackParams;
|
||||
import android.os.Handler;
|
||||
import android.os.SystemClock;
|
||||
|
||||
|
|
@ -38,13 +38,6 @@ import android.os.SystemClock;
|
|||
*/
|
||||
public abstract class AudioDecoderTrackRenderer extends TrackRenderer implements MediaClock {
|
||||
|
||||
/**
|
||||
* The type of a message that can be passed to an instance of this class via
|
||||
* {@link ExoPlayer#sendMessage} or {@link ExoPlayer#blockingSendMessage}. The message object
|
||||
* should be a {@link Float} with 0 being silence and 1 being unity gain.
|
||||
*/
|
||||
public static final int MSG_SET_VOLUME = 1;
|
||||
|
||||
public final CodecCounters codecCounters = new CodecCounters();
|
||||
|
||||
private final EventDispatcher eventDispatcher;
|
||||
|
|
@ -355,10 +348,16 @@ public abstract class AudioDecoderTrackRenderer extends TrackRenderer implements
|
|||
|
||||
@Override
|
||||
public void handleMessage(int messageType, Object message) throws ExoPlaybackException {
|
||||
if (messageType == MSG_SET_VOLUME) {
|
||||
audioTrack.setVolume((Float) message);
|
||||
} else {
|
||||
super.handleMessage(messageType, message);
|
||||
switch (messageType) {
|
||||
case C.MSG_SET_VOLUME:
|
||||
audioTrack.setVolume((Float) message);
|
||||
break;
|
||||
case C.MSG_SET_PLAYBACK_PARAMS:
|
||||
audioTrack.setPlaybackParams((PlaybackParams) message);
|
||||
break;
|
||||
default:
|
||||
super.handleMessage(messageType, message);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue