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
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 0a0678e..d5a90ff 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 @@
         // 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 @@
     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 6821726..6b73c28 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 @@
     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()