From 82d0817d6c3d6550a8777e9f95d0662e2e31687b Mon Sep 17 00:00:00 2001 From: Alex Leung Date: Fri, 9 Feb 2018 11:02:03 -0800 Subject: [PATCH] Add callback when new audio data is ready Bug: webrtc:8864 Change-Id: I476e9430da281f6815eb1af8ffd98afd9b664a63 Reviewed-on: https://webrtc-review.googlesource.com/49981 Commit-Queue: Alex Leung Reviewed-by: Henrik Andreassson Reviewed-by: Ivo Creusen Reviewed-by: Alex Glaznev Cr-Commit-Position: refs/heads/master@{#21976} --- .../webrtc/voiceengine/WebRtcAudioRecord.java | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/modules/audio_device/android/java/src/org/webrtc/voiceengine/WebRtcAudioRecord.java b/modules/audio_device/android/java/src/org/webrtc/voiceengine/WebRtcAudioRecord.java index 2b4ef1a003..8a6cb7150a 100644 --- a/modules/audio_device/android/java/src/org/webrtc/voiceengine/WebRtcAudioRecord.java +++ b/modules/audio_device/android/java/src/org/webrtc/voiceengine/WebRtcAudioRecord.java @@ -17,6 +17,7 @@ import android.media.MediaRecorder.AudioSource; import android.os.Process; import java.lang.System; import java.nio.ByteBuffer; +import java.util.Arrays; import java.util.concurrent.TimeUnit; import org.webrtc.Logging; import org.webrtc.ThreadUtils; @@ -79,6 +80,55 @@ public class WebRtcAudioRecord { WebRtcAudioRecord.errorCallback = errorCallback; } + /** + * Contains audio sample information. Object is passed using {@link + * WebRtcAudioRecord.WebRtcAudioRecordSamplesReadyCallback} + */ + public static class AudioSamples { + /** See {@link AudioRecord#getAudioFormat()} */ + private final int audioFormat; + /** See {@link AudioRecord#getChannelCount()} */ + private final int channelCount; + /** See {@link AudioRecord#getSampleRate()} */ + private final int sampleRate; + + private final byte[] data; + + private AudioSamples(AudioRecord audioRecord, byte[] data) { + this.audioFormat = audioRecord.getAudioFormat(); + this.channelCount = audioRecord.getChannelCount(); + this.sampleRate = audioRecord.getSampleRate(); + this.data = data; + } + + public int getAudioFormat() { + return audioFormat; + } + + public int getChannelCount() { + return channelCount; + } + + public int getSampleRate() { + return sampleRate; + } + + public byte[] getData() { + return data; + } + } + + /** Called when new audio samples are ready. This should only be set for debug purposes */ + public static interface WebRtcAudioRecordSamplesReadyCallback { + void onWebRtcAudioRecordSamplesReady(AudioSamples samples); + } + + private static WebRtcAudioRecordSamplesReadyCallback audioSamplesReadyCallback = null; + + public static void setOnAudioSamplesReady(WebRtcAudioRecordSamplesReadyCallback callback) { + audioSamplesReadyCallback = callback; + } + /** * Audio thread which keeps calling ByteBuffer.read() waiting for audio * to be recorded. Feeds recorded data to the native counterpart as a @@ -112,6 +162,13 @@ public class WebRtcAudioRecord { if (keepAlive) { nativeDataIsRecorded(bytesRead, nativeAudioRecord); } + if (audioSamplesReadyCallback != null) { + // Copy the entire byte buffer array. Assume that the start of the byteBuffer is + // at index 0. + byte[] data = Arrays.copyOf(byteBuffer.array(), byteBuffer.capacity()); + audioSamplesReadyCallback.onWebRtcAudioRecordSamplesReady( + new AudioSamples(audioRecord, data)); + } } else { String errorMessage = "AudioRecord.read failed: " + bytesRead; Logging.e(TAG, errorMessage);