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 <alexleung@google.com> Reviewed-by: Henrik Andreassson <henrika@webrtc.org> Reviewed-by: Ivo Creusen <ivoc@webrtc.org> Reviewed-by: Alex Glaznev <glaznev@webrtc.org> Cr-Commit-Position: refs/heads/master@{#21976}
This commit is contained in:
@ -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);
|
||||
|
||||
Reference in New Issue
Block a user