Skip to content

Commit

Permalink
Add support for resetting the AudioTrack stream type.
Browse files Browse the repository at this point in the history
This is only exposed on the MediaCodecAudioTrackRenderer in v1.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=139891484
  • Loading branch information
andrewlewis authored and ojw28 committed Nov 25, 2016
1 parent 22b25c0 commit 836a573
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,33 @@ public interface EventListener extends MediaCodecTrackRenderer.EventListener {
}

/**
* The type of a message that can be passed to an instance of this class via
* A 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
* A 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;

/**
* A 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 an integer stream type accepted by {@link android.media.AudioTrack}'s constructor
* (see {@link android.media.AudioTrack#AudioTrack(int, int, int, int, int, int)}).
* <p>
* Note that when the stream type changes, the AudioTrack must be reinitialized, which can
* introduce a brief gap in audio output. Note also that tracks in the same audio session must
* share the same routing, so a new audio session id will be generated.
*/
public static final int MSG_SET_STREAM_TYPE = 3;

private final EventListener eventListener;
private final AudioTrack audioTrack;

Expand Down Expand Up @@ -441,6 +453,12 @@ public void handleMessage(int messageType, Object message) throws ExoPlaybackExc
case MSG_SET_PLAYBACK_PARAMS:
audioTrack.setPlaybackParams((PlaybackParams) message);
break;
case MSG_SET_STREAM_TYPE:
int streamType = (Integer) message;
if (audioTrack.setStreamType(streamType)) {
audioSessionId = AudioTrack.SESSION_ID_NOT_SET;
}
break;
default:
super.handleMessage(messageType, message);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ public InvalidAudioTrackTimestampException(String message) {
public static boolean failOnSpuriousAudioTimestamp = false;

private final AudioCapabilities audioCapabilities;
private final int streamType;
private final ConditionVariable releasingConditionVariable;
private final long[] playheadOffsets;
private final AudioTrackUtil audioTrackUtil;
Expand All @@ -197,6 +196,7 @@ public InvalidAudioTrackTimestampException(String message) {
private android.media.AudioTrack audioTrack;
private int sampleRate;
private int channelConfig;
private int streamType;
private int sourceEncoding;
private int targetEncoding;
private boolean passthrough;
Expand Down Expand Up @@ -243,7 +243,6 @@ public AudioTrack() {
*/
public AudioTrack(AudioCapabilities audioCapabilities, int streamType) {
this.audioCapabilities = audioCapabilities;
this.streamType = streamType;
releasingConditionVariable = new ConditionVariable(true);
if (Util.SDK_INT >= 18) {
try {
Expand All @@ -261,6 +260,7 @@ public AudioTrack(AudioCapabilities audioCapabilities, int streamType) {
audioTrackUtil = new AudioTrackUtil();
}
playheadOffsets = new long[MAX_PLAYHEAD_OFFSET_COUNT];
this.streamType = streamType;
volume = 1.0f;
startMediaTimeState = START_NOT_SET;
}
Expand Down Expand Up @@ -702,6 +702,24 @@ public void setPlaybackParams(PlaybackParams playbackParams) {
audioTrackUtil.setPlaybackParameters(playbackParams);
}

/**
* Sets the stream type for audio track. If the stream type has changed, {@link #isInitialized()}
* will return {@code false} and the caller must re-{@link #initialize(int)} the audio track
* before writing more data. The caller must not reuse the audio session identifier when
* re-initializing with a new stream type.
*
* @param streamType The stream type to use for audio output.
* @return Whether the stream type changed.
*/
public boolean setStreamType(int streamType) {
if (this.streamType == streamType) {
return false;
}
this.streamType = streamType;
reset();
return true;
}

/**
* Sets the playback volume.
*/
Expand Down

0 comments on commit 836a573

Please sign in to comment.