hal: force stop after drain.

Force playback state to IDLE and send compress_stop()
when drain completes to force reset driver and DSP pointers.
This ensures that even if last write was partial, next write
will be on a 32 byte boundary.

Also do not wait for write completion if compress_write()
returns an error.

Bug 8174410.

Change-Id: If144981c6396b24515d45b32a75ab61872a35ea2
diff --git a/hal/audio_hw.c b/hal/audio_hw.c
index 73d9a3b..852ddf6 100644
--- a/hal/audio_hw.c
+++ b/hal/audio_hw.c
@@ -740,8 +740,11 @@
         pthread_mutex_lock(&out->lock);
         out->offload_thread_blocked = false;
         pthread_cond_signal(&out->cond);
-        if (send_callback)
+        if (send_callback) {
+            if (event == STREAM_CBK_EVENT_DRAIN_READY)
+                stop_compressed_output_l(out);
             out->offload_callback(event, NULL, out->offload_cookie);
+        }
         free(cmd);
     }
 
@@ -1268,7 +1271,7 @@
 {
     struct stream_out *out = (struct stream_out *)stream;
     struct audio_device *adev = out->dev;
-    size_t ret = 0;
+    ssize_t ret = 0;
 
     pthread_mutex_lock(&out->lock);
     if (out->standby) {
@@ -1285,7 +1288,8 @@
 
     if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
         ret = compress_write(out->compr, buffer, bytes);
-        if (ret < bytes) {
+        ALOGVV("%s: writing buffer (%d bytes) to pcm device returned %d", __func__, bytes, ret);
+        if (ret >= 0 && ret < (ssize_t)bytes) {
             send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
         }
         if (!out->playback_started) {