From 0a3ff7976bcb0c063e3c364a12f03f95fdfb7175 Mon Sep 17 00:00:00 2001 From: "henrika@webrtc.org" Date: Wed, 25 Feb 2015 09:27:51 +0000 Subject: [PATCH] New AudioTrack implementation now works on pre-Lollipop devices. The previous version used an AudioTrack.write() implementation that required API Level 21. This is now fixed. BUG=4339 R=magjed@webrtc.org, perkj@webrtc.org Review URL: https://webrtc-codereview.appspot.com/42459004 Cr-Commit-Position: refs/heads/master@{#8494} git-svn-id: http://webrtc.googlecode.com/svn/trunk@8494 4adac7df-926f-26a2-2b94-8c16560cd09d --- .../org/webrtc/voiceengine/WebRtcAudioTrack.java | 14 ++++++++++---- .../org/webrtc/voiceengine/WebRtcAudioUtils.java | 4 ++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/webrtc/modules/audio_device/android/java/src/org/webrtc/voiceengine/WebRtcAudioTrack.java b/webrtc/modules/audio_device/android/java/src/org/webrtc/voiceengine/WebRtcAudioTrack.java index 0a0678e75..d5a90ffd9 100644 --- a/webrtc/modules/audio_device/android/java/src/org/webrtc/voiceengine/WebRtcAudioTrack.java +++ b/webrtc/modules/audio_device/android/java/src/org/webrtc/voiceengine/WebRtcAudioTrack.java @@ -96,9 +96,16 @@ class WebRtcAudioTrack { // Upon return, the buffer position will have been advanced to reflect // the amount of data that was successfully written to the AudioTrack. assertTrue(sizeInBytes <= byteBuffer.remaining()); - int bytesWritten = audioTrack.write(byteBuffer, - sizeInBytes, - AudioTrack.WRITE_BLOCKING); + int bytesWritten = 0; + if (WebRtcAudioUtils.runningOnLollipopOrHigher()) { + bytesWritten = audioTrack.write(byteBuffer, + sizeInBytes, + AudioTrack.WRITE_BLOCKING); + } else { + bytesWritten = audioTrack.write(byteBuffer.array(), + 0, + sizeInBytes); + } if (bytesWritten != sizeInBytes) { Loge("AudioTrack.write failed: " + bytesWritten); if (bytesWritten == AudioTrack.ERROR_INVALID_OPERATION) { @@ -146,7 +153,6 @@ class WebRtcAudioTrack { byteBuffer = byteBuffer.allocateDirect( BYTES_PER_FRAME * (sampleRate / BUFFERS_PER_SECOND)); Logd("byteBuffer.capacity: " + byteBuffer.capacity()); - // Rather than passing the ByteBuffer with every callback (requiring // the potentially expensive GetDirectBufferAddress) we simply have the // the native class cache the address to the memory once. diff --git a/webrtc/modules/audio_device/android/java/src/org/webrtc/voiceengine/WebRtcAudioUtils.java b/webrtc/modules/audio_device/android/java/src/org/webrtc/voiceengine/WebRtcAudioUtils.java index 6821726ee..6b73c288f 100644 --- a/webrtc/modules/audio_device/android/java/src/org/webrtc/voiceengine/WebRtcAudioUtils.java +++ b/webrtc/modules/audio_device/android/java/src/org/webrtc/voiceengine/WebRtcAudioUtils.java @@ -28,6 +28,10 @@ public final class WebRtcAudioUtils { return Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1; } + public static boolean runningOnLollipopOrHigher() { + return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP; + } + /** Helper method for building a string of thread information.*/ public static String getThreadInfo() { return "@[name=" + Thread.currentThread().getName()