[RELAND] Add support of AudioRecord.Builder in the ADM for Android

Now fixed issue which caused http://b/140707892

First version was reverted in https://webrtc-review.googlesource.com/c/src/+/152526.
The mistake I had done in the original version was that I missed that the new
builder could throw a different type of exception and it was never caught.

TBR: glaznev@webrtc.org
Bug: webrtc:10942
Change-Id: I0e11511936d2d25681a1ffae3bbd367095fee7a1
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/152664
Reviewed-by: Henrik Andreassson <henrika@webrtc.org>
Commit-Queue: Henrik Andreassson <henrika@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#29164}
This commit is contained in:
henrika
2019-09-12 13:07:59 +02:00
committed by Commit Bot
parent dc7d2c6fd7
commit 69f8c42d2c

View File

@ -10,6 +10,7 @@
package org.webrtc.audio;
import android.annotation.TargetApi;
import android.content.Context;
import android.media.AudioFormat;
import android.media.AudioManager;
@ -251,15 +252,25 @@ class WebRtcAudioRecord {
int bufferSizeInBytes = Math.max(BUFFER_SIZE_FACTOR * minBufferSize, byteBuffer.capacity());
Logging.d(TAG, "bufferSizeInBytes: " + bufferSizeInBytes);
try {
audioRecord =
new AudioRecord(audioSource, sampleRate, channelConfig, audioFormat, bufferSizeInBytes);
} catch (IllegalArgumentException e) {
reportWebRtcAudioRecordInitError("AudioRecord ctor error: " + e.getMessage());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// Use the AudioRecord.Builder class on Android M (23) and above.
// Throws IllegalArgumentException.
audioRecord = createAudioRecordOnMOrHigher(
audioSource, sampleRate, channelConfig, audioFormat, bufferSizeInBytes);
} else {
// Use the old AudioRecord constructor for API levels below 23.
// Throws UnsupportedOperationException.
audioRecord = createAudioRecordOnLowerThanM(
audioSource, sampleRate, channelConfig, audioFormat, bufferSizeInBytes);
}
} catch (IllegalArgumentException | UnsupportedOperationException e) {
// Report of exception message is sufficient. Example: "Cannot create AudioRecord".
reportWebRtcAudioRecordInitError(e.getMessage());
releaseAudioResources();
return -1;
}
if (audioRecord == null || audioRecord.getState() != AudioRecord.STATE_INITIALIZED) {
reportWebRtcAudioRecordInitError("Failed to create a new AudioRecord instance");
reportWebRtcAudioRecordInitError("Creation or initialization of audio recorder failed.");
releaseAudioResources();
return -1;
}
@ -283,7 +294,7 @@ class WebRtcAudioRecord {
}
if (audioRecord.getRecordingState() != AudioRecord.RECORDSTATE_RECORDING) {
reportWebRtcAudioRecordStartError(AudioRecordStartErrorCode.AUDIO_RECORD_START_STATE_MISMATCH,
"AudioRecord.startRecording failed - incorrect state :"
"AudioRecord.startRecording failed - incorrect state: "
+ audioRecord.getRecordingState());
return false;
}
@ -307,6 +318,27 @@ class WebRtcAudioRecord {
return true;
}
@TargetApi(Build.VERSION_CODES.M)
private static AudioRecord createAudioRecordOnMOrHigher(
int audioSource, int sampleRate, int channelConfig, int audioFormat, int bufferSizeInBytes) {
Logging.d(TAG, "createAudioRecordOnMOrHigher");
return new AudioRecord.Builder()
.setAudioSource(audioSource)
.setAudioFormat(new AudioFormat.Builder()
.setEncoding(audioFormat)
.setSampleRate(sampleRate)
.setChannelMask(channelConfig)
.build())
.setBufferSizeInBytes(bufferSizeInBytes)
.build();
}
private static AudioRecord createAudioRecordOnLowerThanM(
int audioSource, int sampleRate, int channelConfig, int audioFormat, int bufferSizeInBytes) {
Logging.d(TAG, "createAudioRecordOnLowerThanM");
return new AudioRecord(audioSource, sampleRate, channelConfig, audioFormat, bufferSizeInBytes);
}
private void logMainParameters() {
Logging.d(TAG,
"AudioRecord: "