Add checks and offset when using byteBuffer in WebRtcAudioRecord.

See bug for more info.

In this case, the offset of the byteBuffer was observed to be 4 bytes
when testing, meaning that the first 4 bytes sent to the AudioSamples
callback were empty, and the last 4 bytes that should have been sent
were not sent.

This CL adjusts the range copied from the backing array to match the
offset.

Bug: webrtc:9175
Change-Id: I40ac6e10c6d7058ead7eff1c9fa2f342920cf2a4
Reviewed-on: https://webrtc-review.googlesource.com/75123
Reviewed-by: Henrik Andreassson <henrika@webrtc.org>
Reviewed-by: Magnus Jedvert <magjed@webrtc.org>
Commit-Queue: Paulina Hensman <phensman@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#23172}
This commit is contained in:
Paulina Hensman
2018-05-08 11:13:46 +02:00
committed by Commit Bot
parent 0520b0eb7b
commit 24bebb86bd

View File

@ -88,8 +88,6 @@ class WebRtcAudioRecord {
super(name); super(name);
} }
// TODO(titovartem) make correct fix during webrtc:9175
@SuppressWarnings("ByteBufferBackingArray")
@Override @Override
public void run() { public void run() {
Process.setThreadPriority(Process.THREAD_PRIORITY_URGENT_AUDIO); Process.setThreadPriority(Process.THREAD_PRIORITY_URGENT_AUDIO);
@ -111,9 +109,10 @@ class WebRtcAudioRecord {
nativeDataIsRecorded(nativeAudioRecord, bytesRead); nativeDataIsRecorded(nativeAudioRecord, bytesRead);
} }
if (audioSamplesReadyCallback != null) { if (audioSamplesReadyCallback != null) {
// Copy the entire byte buffer array. Assume that the start of the byteBuffer is // Copy the entire byte buffer array. The start of the byteBuffer is not necessarily
// at index 0. // at index 0.
byte[] data = Arrays.copyOf(byteBuffer.array(), byteBuffer.capacity()); byte[] data = Arrays.copyOfRange(byteBuffer.array(), byteBuffer.arrayOffset(),
byteBuffer.capacity() + byteBuffer.arrayOffset());
audioSamplesReadyCallback.onWebRtcAudioRecordSamplesReady( audioSamplesReadyCallback.onWebRtcAudioRecordSamplesReady(
new JavaAudioDeviceModule.AudioSamples(audioRecord.getAudioFormat(), new JavaAudioDeviceModule.AudioSamples(audioRecord.getAudioFormat(),
audioRecord.getChannelCount(), audioRecord.getSampleRate(), data)); audioRecord.getChannelCount(), audioRecord.getSampleRate(), data));
@ -208,6 +207,10 @@ class WebRtcAudioRecord {
final int bytesPerFrame = channels * (BITS_PER_SAMPLE / 8); final int bytesPerFrame = channels * (BITS_PER_SAMPLE / 8);
final int framesPerBuffer = sampleRate / BUFFERS_PER_SECOND; final int framesPerBuffer = sampleRate / BUFFERS_PER_SECOND;
byteBuffer = ByteBuffer.allocateDirect(bytesPerFrame * framesPerBuffer); byteBuffer = ByteBuffer.allocateDirect(bytesPerFrame * framesPerBuffer);
if (!(byteBuffer.hasArray())) {
reportWebRtcAudioRecordInitError("ByteBuffer does not have backing array.");
return -1;
}
Logging.d(TAG, "byteBuffer.capacity: " + byteBuffer.capacity()); Logging.d(TAG, "byteBuffer.capacity: " + byteBuffer.capacity());
emptyBytes = new byte[byteBuffer.capacity()]; emptyBytes = new byte[byteBuffer.capacity()];
// Rather than passing the ByteBuffer with every callback (requiring // Rather than passing the ByteBuffer with every callback (requiring