Merge "Camera2: Automated tests for testing HAL2.0 interface"
diff --git a/modules/audio_remote_submix/audio_hw.cpp b/modules/audio_remote_submix/audio_hw.cpp
index b24608f..e9759e5 100755
--- a/modules/audio_remote_submix/audio_hw.cpp
+++ b/modules/audio_remote_submix/audio_hw.cpp
@@ -233,12 +233,22 @@
     ssize_t written_frames = 0;
     struct submix_stream_out *out = reinterpret_cast<struct submix_stream_out *>(stream);
 
+    const size_t frame_size = audio_stream_frame_size(&stream->common);
+    const size_t frames = bytes / frame_size;
+
     pthread_mutex_lock(&out->dev->lock);
 
     out->dev->output_standby = false;
 
     MonoPipe* sink = out->dev->rsxSink.get();
     if (sink != NULL) {
+        if (sink->isShutdown()) {
+            pthread_mutex_unlock(&out->dev->lock);
+            // the pipe has already been shutdown, this buffer will be lost but we must
+            //   simulate timing so we don't drain the output faster than realtime
+            usleep(frames * 1000000 / out_get_sample_rate(&stream->common));
+            return bytes;
+        }
         sink->incStrong(buffer);
     } else {
         pthread_mutex_unlock(&out->dev->lock);
@@ -249,8 +259,6 @@
 
     pthread_mutex_unlock(&out->dev->lock);
 
-    const size_t frame_size = audio_stream_frame_size(&stream->common);
-    const size_t frames = bytes / frame_size;
     written_frames = sink->write(buffer, frames);
     if (written_frames < 0) {
         if (written_frames == (ssize_t)NEGOTIATE) {
@@ -563,8 +571,7 @@
     // initialize pipe
     {
         ALOGV("  initializing pipe");
-        const NBAIO_Format format =
-                config->sample_rate == 48000 ? Format_SR48_C2_I16 : Format_SR44_1_C2_I16;
+        const NBAIO_Format format = Format_from_SR_C(config->sample_rate, 2);
         const NBAIO_Format offers[1] = {format};
         size_t numCounterOffers = 0;
         // creating a MonoPipe with optional blocking set to true.
@@ -741,6 +748,12 @@
 
     pthread_mutex_lock(&rsxadev->lock);
 
+    MonoPipe* sink = rsxadev->rsxSink.get();
+    if (sink != NULL) {
+        ALOGI("shutdown");
+        sink->shutdown(true);
+    }
+
     free(stream);
 
     pthread_mutex_unlock(&rsxadev->lock);