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,21 +341,33 @@ class AudioSinkAudioTrackImpl { ...@@ -331,21 +341,33 @@ class AudioSinkAudioTrackImpl {
+ "ms) usageType=" + usageType + " contentType=" + contentType + "ms) usageType=" + usageType + " contentType=" + contentType
+ " with session-id=" + sessionId); + " with session-id=" + sessionId);
AudioTrack.Builder builder = new AudioTrack.Builder(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
builder.setBufferSizeInBytes(bufferSizeInBytes) AudioTrack.Builder builder = new AudioTrack.Builder();
.setTransferMode(AUDIO_MODE) builder.setBufferSizeInBytes(bufferSizeInBytes)
.setAudioAttributes(new AudioAttributes.Builder() .setTransferMode(AUDIO_MODE)
.setUsage(usageType) .setAudioAttributes(new AudioAttributes.Builder()
.setContentType(contentType) .setUsage(usageType)
.build()) .setContentType(contentType)
.setAudioFormat(new AudioFormat.Builder() .build())
.setEncoding(AUDIO_FORMAT) .setAudioFormat(new AudioFormat.Builder()
.setSampleRate(mSampleRateInHz) .setEncoding(AUDIO_FORMAT)
.setChannelMask(CHANNEL_CONFIG) .setSampleRate(mSampleRateInHz)
.build()); .setChannelMask(CHANNEL_CONFIG)
if (sessionId != AudioManager.ERROR) builder.setSessionId(sessionId); .build());
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.
long most_frames_left = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Math.min(mTotalFramesWritten, mAudioTrack.getBufferSizeInFrames()); long most_frames_left =
playtimeLeftNsecs = convertFramesToNanoTime(most_frames_left); Math.min(mTotalFramesWritten, mAudioTrack.getBufferSizeInFrames());
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,7 +476,11 @@ class AudioSinkAudioTrackImpl { ...@@ -449,7 +476,11 @@ class AudioSinkAudioTrackImpl {
} }
int getUnderrunCount() { int getUnderrunCount() {
return mAudioTrack.getUnderrunCount(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
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
......
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