Merge "Put usb stream into standby on disconnection if offloading" into oc-dr1-dev
diff --git a/hal/audio_hw.c b/hal/audio_hw.c
index 0ff581d..7f91a24 100644
--- a/hal/audio_hw.c
+++ b/hal/audio_hw.c
@@ -1801,6 +1801,15 @@
     /* Must be called after removing the usecase from list */
     if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
         check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
+    else if (out->devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
+        struct listnode *node;
+        struct audio_usecase *usecase;
+        list_for_each(node, &adev->usecase_list) {
+            usecase = node_to_item(node, struct audio_usecase, list);
+            if (usecase->devices & AUDIO_DEVICE_OUT_SPEAKER)
+                select_devices(adev, usecase->id);
+        }
+    }
 
     ALOGV("%s: exit: status(%d)", __func__, ret);
     return ret;
@@ -2193,6 +2202,15 @@
     return out == adev->primary_output || out == adev->voice_tx_output;
 }
 
+static int get_alive_usb_card(struct str_parms* parms) {
+    int card;
+    if ((str_parms_get_int(parms, "card", &card) >= 0) &&
+        !audio_extn_usb_alive(card)) {
+        return card;
+    }
+    return -ENODEV;
+}
+
 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
 {
     struct stream_out *out = (struct stream_out *)stream;
@@ -2242,9 +2260,11 @@
 
         // Workaround: If routing to an non existing usb device, fail gracefully
         // The routing request will otherwise block during 10 second
-        if (audio_is_usb_out_device(new_dev) && !audio_extn_usb_alive(adev->snd_card)) {
-            ALOGW("out_set_parameters() ignoring rerouting to non existing USB card %d",
-                  adev->snd_card);
+        int card;
+        if (audio_is_usb_out_device(new_dev) &&
+            (card = get_alive_usb_card(parms)) >= 0) {
+
+            ALOGW("out_set_parameters() ignoring rerouting to non existing USB card %d", card);
             pthread_mutex_unlock(&adev->lock);
             pthread_mutex_unlock(&out->lock);
             status = -ENOSYS;
@@ -3162,9 +3182,11 @@
 
             // Workaround: If routing to an non existing usb device, fail gracefully
             // The routing request will otherwise block during 10 second
-            if (audio_is_usb_in_device(val) && !audio_extn_usb_alive(adev->snd_card)) {
-                ALOGW("in_set_parameters() ignoring rerouting to non existing USB card %d",
-                      adev->snd_card);
+            int card;
+            if (audio_is_usb_in_device(val) &&
+                (card = get_alive_usb_card(parms)) >= 0) {
+
+                ALOGW("in_set_parameters() ignoring rerouting to non existing USB card %d", card);
                 status = -ENOSYS;
             } else {