Merge "work-around AudioTrack bug" into jb-dev
diff --git a/suite/audio_quality/client/src/com/android/cts/audiotest/AudioProtocol.java b/suite/audio_quality/client/src/com/android/cts/audiotest/AudioProtocol.java
index 32cc3ed..c5bc671 100644
--- a/suite/audio_quality/client/src/com/android/cts/audiotest/AudioProtocol.java
+++ b/suite/audio_quality/client/src/com/android/cts/audiotest/AudioProtocol.java
@@ -110,17 +110,16 @@
     @Override
     public void onMarkerReached(AudioTrack track) {
         Log.d(TAG, "playback completed");
+        track.stop();
+        track.flush();
+        track.release();
+        mPlaybackThread.quitLoop();
+        mPlaybackThread = null;
         try {
             sendSimpleReplyHeader(CMD_START_PLAYBACK, PROTOCOL_OK);
         } catch (IOException e) {
             // maybe socket already closed. don't do anything
             Log.e(TAG, "ignore exception", e);
-        } finally {
-            track.stop();
-            track.flush();
-            track.release();
-            mPlaybackThread.quitLoop(false);
-            mPlaybackThread = null;
         }
     }
 
@@ -199,7 +198,7 @@
             mRecord = null;
         }
         if (mRecordThread != null) {
-            mRecordThread.quitLoop(true);
+            mRecordThread.quitLoop();
             mRecordThread = null;
         }
         if (mPlayback != null) {
@@ -211,7 +210,7 @@
             mPlayback = null;
         }
         if (mPlaybackThread != null) {
-            mPlaybackThread.quitLoop(true);
+            mPlaybackThread.quitLoop();
             mPlaybackThread = null;
         }
         mDataMap.clear();
@@ -246,8 +245,6 @@
             if (samplingRate != 44100) {
                 throw new ProtocolError("wrong rate");
             }
-            //FIXME cannot start playback again
-            //TODO repeat
             //FIXME in MODE_STATIC, setNotificationMarkerPosition does not work with full length
             mPlaybackThread = new LoopThread(new Runnable() {
 
@@ -259,9 +256,16 @@
                     }
                     int type = (mode == 0) ? AudioManager.STREAM_VOICE_CALL :
                         AudioManager.STREAM_MUSIC;
+                    int bufferSize = AudioTrack.getMinBufferSize(samplingRate,
+                            stereo ? AudioFormat.CHANNEL_OUT_STEREO : AudioFormat.CHANNEL_OUT_MONO,
+                            AudioFormat.ENCODING_PCM_16BIT);
+                    bufferSize = bufferSize * 4;
+                    if (bufferSize < 256 * 1024) {
+                        bufferSize = 256 * 1024;
+                    }
                     mPlayback = new AudioTrack(type, samplingRate,
                             stereo ? AudioFormat.CHANNEL_OUT_STEREO : AudioFormat.CHANNEL_OUT_MONO,
-                            AudioFormat.ENCODING_PCM_16BIT, data.capacity(),
+                            AudioFormat.ENCODING_PCM_16BIT, bufferSize,
                             AudioTrack.MODE_STREAM);
                     float minVolume = mPlayback.getMinVolume();
                     float maxVolume = mPlayback.getMaxVolume();
@@ -269,7 +273,10 @@
                     mPlayback.setStereoVolume(newVolume, newVolume);
                     Log.d(TAG, "setting volume " + newVolume + " max " + maxVolume +
                             " min " + minVolume + " received " + volume);
-                    mPlayback.write(data.array(), 0, data.capacity());
+                    int dataWritten = 0;
+                    int dataToWrite = (bufferSize < data.capacity())? bufferSize : data.capacity();
+                    mPlayback.write(data.array(), 0, dataToWrite);
+                    dataWritten = dataToWrite;
                     mPlayback.setPlaybackPositionUpdateListener(AudioProtocol.this);
 
                     int endMarker = data.capacity()/(stereo ? 4 : 2);
@@ -278,6 +285,12 @@
                             " set.. res " + res + " stereo? " + stereo + " mode " + mode +
                             " end " + endMarker);
                     mPlayback.play();
+                    while (dataWritten < data.capacity()) {
+                        int dataLeft = data.capacity() - dataWritten;
+                        dataToWrite = (bufferSize < dataLeft)? bufferSize : dataLeft;
+                        mPlayback.write(data.array(), dataWritten, dataToWrite);
+                        dataWritten += dataToWrite;
+                    }
                 }
             });
             mPlaybackThread.start();
@@ -299,7 +312,7 @@
             mPlayback = null;
         }
         if (mPlaybackThread != null) {
-            mPlaybackThread.quitLoop(true);
+            mPlaybackThread.quitLoop();
             mPlaybackThread = null;
         }
         sendSimpleReplyHeader(CMD_STOP_PLAYBACK, PROTOCOL_OK);
@@ -393,7 +406,7 @@
             mRecord = null;
         }
         if (mRecordThread != null) {
-            mRecordThread.quitLoop(true);
+            mRecordThread.quitLoop();
             mRecordThread = null;
         }
         sendSimpleReplyHeader(CMD_STOP_RECORDING, PROTOCOL_OK);
@@ -437,10 +450,10 @@
             Looper.loop();
         }
         // should be called outside this thread
-        public void quitLoop(boolean wait) {
+        public void quitLoop() {
             mLooper.quit();
             try {
-                if (wait) {
+                if (Thread.currentThread() != this) {
                     join();
                 }
             } catch (InterruptedException e) {
diff --git a/suite/audio_quality/test_description/dut_speaker_calibration.xml b/suite/audio_quality/test_description/dut_speaker_calibration.xml
index 9cc655b..f0ddf17 100644
--- a/suite/audio_quality/test_description/dut_speaker_calibration.xml
+++ b/suite/audio_quality/test_description/dut_speaker_calibration.xml
@@ -22,7 +22,7 @@
 	</setup>
 
 	<action> <!-- 1 action -->
-		<!--  equivalent of for loop. all children will be completed before moving to the next 
+		<!--  equivalent of for loop. all children will be completed before moving to the next
 		      stage.repeat up to 100 times unless stopped by some condition -->
 		<sequential repeat="20" index="i">
 			<!--  sync start : execute only sync complete : execute + complete
diff --git a/suite/audio_quality/test_description/test/dut_speaker_play.xml b/suite/audio_quality/test_description/test/dut_speaker_play.xml
new file mode 100644
index 0000000..d2e35e8
--- /dev/null
+++ b/suite/audio_quality/test_description/test/dut_speaker_play.xml
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<!-- Copyright (C) 2012 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+
+<case name="dut_speaker_play" version="1.0" description="Play DUT speaker for some time">
+	<setup> <!-- 1 setup -->
+		<!-- prepare sound source id: to be used in output, sine 1000Hz, 20000ms long -->
+		<sound id="sound1" type="sin:32000:100:5000" preload="1" />
+	</setup>
+
+
+	<action> <!-- 1 action -->
+		<!--  equivalent of for loop. all children will be completed before moving to the next 
+		      stage.repeat up to 100 times unless stopped by some condition -->
+		<sequential repeat="20" index="i">
+			<!--  sync start : execute only sync complete : execute + complete
+			      For sync start, complete will be called when the parent completes -->
+			<output device="DUT" id="sound1" gain="100" sync="start" waitforcompletion="1" />
+		</sequential>
+	</action>
+</case>