Commit 3e139c3d authored by Erik Wolsheimer's avatar Erik Wolsheimer Committed by Commit Bot

Make AudioSinkAudioTrackImpl compatible to pre-M devices

Change-Id: I2d6d9628f752860afcc8bf13caecc36e2c10fc5b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1640788Reviewed-by: default avatarSimeon Anfinrud <sanfin@chromium.org>
Commit-Queue: Erik Wolsheimer <ewol@google.com>
Cr-Commit-Position: refs/heads/master@{#665955}
parent 8593e41c
...@@ -76,6 +76,16 @@ class AudioSinkAudioTrackImpl { ...@@ -76,6 +76,16 @@ class AudioSinkAudioTrackImpl {
} }
}; };
/** See VolumeControl for this mapping. */
private static final SparseIntArray CAST_TYPE_TO_ANDROID_STREAM_TYPE = new SparseIntArray(4) {
{
append(AudioContentType.MEDIA, AudioManager.STREAM_MUSIC);
append(AudioContentType.ALARM, AudioManager.STREAM_ALARM);
append(AudioContentType.COMMUNICATION, AudioManager.STREAM_SYSTEM);
append(AudioContentType.OTHER, AudioManager.STREAM_VOICE_CALL);
}
};
// Hardcoded AudioTrack config parameters. // Hardcoded AudioTrack config parameters.
private static final int CHANNEL_CONFIG = AudioFormat.CHANNEL_OUT_STEREO; private static final int CHANNEL_CONFIG = AudioFormat.CHANNEL_OUT_STEREO;
private static final int AUDIO_FORMAT = AudioFormat.ENCODING_PCM_FLOAT; private static final int AUDIO_FORMAT = AudioFormat.ENCODING_PCM_FLOAT;
...@@ -331,6 +341,7 @@ class AudioSinkAudioTrackImpl { ...@@ -331,6 +341,7 @@ class AudioSinkAudioTrackImpl {
+ "ms) usageType=" + usageType + " contentType=" + contentType + "ms) usageType=" + usageType + " contentType=" + contentType
+ " with session-id=" + sessionId); + " with session-id=" + sessionId);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
AudioTrack.Builder builder = new AudioTrack.Builder(); AudioTrack.Builder builder = new AudioTrack.Builder();
builder.setBufferSizeInBytes(bufferSizeInBytes) builder.setBufferSizeInBytes(bufferSizeInBytes)
.setTransferMode(AUDIO_MODE) .setTransferMode(AUDIO_MODE)
...@@ -344,8 +355,19 @@ class AudioSinkAudioTrackImpl { ...@@ -344,8 +355,19 @@ class AudioSinkAudioTrackImpl {
.setChannelMask(CHANNEL_CONFIG) .setChannelMask(CHANNEL_CONFIG)
.build()); .build());
if (sessionId != AudioManager.ERROR) builder.setSessionId(sessionId); if (sessionId != AudioManager.ERROR) builder.setSessionId(sessionId);
mAudioTrack = builder.build(); mAudioTrack = builder.build();
} else {
// Using pre-M API.
if (sessionId == AudioManager.ERROR) {
mAudioTrack = new AudioTrack(CAST_TYPE_TO_ANDROID_STREAM_TYPE.get(castContentType),
mSampleRateInHz, CHANNEL_CONFIG, AUDIO_FORMAT, bufferSizeInBytes,
AudioTrack.MODE_STREAM);
} else {
mAudioTrack = new AudioTrack(CAST_TYPE_TO_ANDROID_STREAM_TYPE.get(castContentType),
mSampleRateInHz, CHANNEL_CONFIG, AudioFormat.AUDIO_FORMAT,
bufferSizeInBytes, AudioTrack.MODE_STREAM, sessionId);
}
}
// Allocated shared buffers. // Allocated shared buffers.
mPcmBuffer = ByteBuffer.allocateDirect(bytesPerBuffer); mPcmBuffer = ByteBuffer.allocateDirect(bytesPerBuffer);
...@@ -414,9 +436,14 @@ class AudioSinkAudioTrackImpl { ...@@ -414,9 +436,14 @@ class AudioSinkAudioTrackImpl {
playtimeLeftNsecs = lastPlayoutTimeNsecs - now; playtimeLeftNsecs = lastPlayoutTimeNsecs - now;
} else { } else {
// We have no timestamp to estimate how much is left to play, so assume the worst case. // We have no timestamp to estimate how much is left to play, so assume the worst case.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
long most_frames_left = long most_frames_left =
Math.min(mTotalFramesWritten, mAudioTrack.getBufferSizeInFrames()); Math.min(mTotalFramesWritten, mAudioTrack.getBufferSizeInFrames());
playtimeLeftNsecs = convertFramesToNanoTime(most_frames_left); playtimeLeftNsecs = convertFramesToNanoTime(most_frames_left);
} else {
// Using pre-M API. Don't know how many frames there are, so assume the worst case.
playtimeLeftNsecs = 0;
}
} }
return (playtimeLeftNsecs < 0) ? 0 : playtimeLeftNsecs / 1000; // return usecs return (playtimeLeftNsecs < 0) ? 0 : playtimeLeftNsecs / 1000; // return usecs
} }
...@@ -449,8 +476,12 @@ class AudioSinkAudioTrackImpl { ...@@ -449,8 +476,12 @@ class AudioSinkAudioTrackImpl {
} }
int getUnderrunCount() { int getUnderrunCount() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return mAudioTrack.getUnderrunCount(); return mAudioTrack.getUnderrunCount();
} }
// Using pre-M API.
return 0;
}
/** Writes the PCM data of the given size into the AudioTrack object. The /** Writes the PCM data of the given size into the AudioTrack object. The
* PCM data is provided through the memory-mapped ByteBuffer. * PCM data is provided through the memory-mapped ByteBuffer.
......
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 to comment