From 24bebb86bd81878e12829436fe13717bc66157e9 Mon Sep 17 00:00:00 2001 From: Paulina Hensman Date: Tue, 8 May 2018 11:13:46 +0200 Subject: [PATCH] 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 Reviewed-by: Magnus Jedvert Commit-Queue: Paulina Hensman Cr-Commit-Position: refs/heads/master@{#23172} --- .../src/java/org/webrtc/audio/WebRtcAudioRecord.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/sdk/android/src/java/org/webrtc/audio/WebRtcAudioRecord.java b/sdk/android/src/java/org/webrtc/audio/WebRtcAudioRecord.java index 3bf51df310..5ae94d6991 100644 --- a/sdk/android/src/java/org/webrtc/audio/WebRtcAudioRecord.java +++ b/sdk/android/src/java/org/webrtc/audio/WebRtcAudioRecord.java @@ -88,8 +88,6 @@ class WebRtcAudioRecord { super(name); } - // TODO(titovartem) make correct fix during webrtc:9175 - @SuppressWarnings("ByteBufferBackingArray") @Override public void run() { Process.setThreadPriority(Process.THREAD_PRIORITY_URGENT_AUDIO); @@ -111,9 +109,10 @@ class WebRtcAudioRecord { nativeDataIsRecorded(nativeAudioRecord, bytesRead); } 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. - byte[] data = Arrays.copyOf(byteBuffer.array(), byteBuffer.capacity()); + byte[] data = Arrays.copyOfRange(byteBuffer.array(), byteBuffer.arrayOffset(), + byteBuffer.capacity() + byteBuffer.arrayOffset()); audioSamplesReadyCallback.onWebRtcAudioRecordSamplesReady( new JavaAudioDeviceModule.AudioSamples(audioRecord.getAudioFormat(), audioRecord.getChannelCount(), audioRecord.getSampleRate(), data)); @@ -208,6 +207,10 @@ class WebRtcAudioRecord { final int bytesPerFrame = channels * (BITS_PER_SAMPLE / 8); final int framesPerBuffer = sampleRate / BUFFERS_PER_SECOND; byteBuffer = ByteBuffer.allocateDirect(bytesPerFrame * framesPerBuffer); + if (!(byteBuffer.hasArray())) { + reportWebRtcAudioRecordInitError("ByteBuffer does not have backing array."); + return -1; + } Logging.d(TAG, "byteBuffer.capacity: " + byteBuffer.capacity()); emptyBytes = new byte[byteBuffer.capacity()]; // Rather than passing the ByteBuffer with every callback (requiring