Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit fce1900c authored by Kevin Rocard's avatar Kevin Rocard Committed by Zhou Song
Browse files

Workaround: Do not send command to a disconnected usb subsystem



-After usb headset disconnection,
the audio policy receives two separate messages warning it of a
usb recoding device and usb playback device disconnection.

If the recording message is handled before the playback one,
the policy transiently switches from headset to headphone then speaker.
Thus temporally rerouting existing streams to a device that no longer exists.

This rerouting causes all usb commands and audio writes to freeze (for 2sec).
Delaying the subsequent routing to speaker by ~8sec.

If the unnecessary routing was to be removed this workaround might not
be necessary.

Taking the problem the other way around, an other fix would
be for the usb drivers to fail immediately if no
usb is present. This fix is targeted for the next release.

In the meantime, this commit patches the primary hal to intercept
routing requests towards a disconnected usb device
and immediately return an error.

Test: disconnect usb headset while playing video in Photo
Bug: 62815577
Signed-off-by: default avatarKevin Rocard <krocard@google.com>
(cherry picked from commit f9f241e4)

-Routing to usb was rejected due to incorrect card number

Fixes regression introduced by: f9f241e4

The card number is updated by adev_set_parameters,
that is called after out_set_parameters.
As a result, out_set_parameters was using the wrong card number to check
for the card presence (in an attempt to ignore rerouting to an unplug
card).
Thus all routing to USB we denied.

Bug: 64532887
Test: Plug and unplug usb headset (skylab) during Photo playback,
      Youtube, Play music and voice call.
Signed-off-by: default avatarKevin Rocard <krocard@google.com>
(cherry picked from commit 1e02c88b)

Change-Id: I148a0b35067703e6ba3ca3414d3afe9cf6718dc0
parent 939c563a
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -221,6 +221,7 @@ int32_t audio_extn_get_afe_proxy_channel_count();
#define audio_extn_usb_get_max_bit_width(p)                            (0)
#define audio_extn_usb_get_sup_sample_rates(t, s, l)                   (0)
#define audio_extn_usb_is_tunnel_supported()                           (0)
#define audio_extn_usb_alive(adev)                                     (false)
#else
void audio_extn_usb_init(void *adev);
void audio_extn_usb_deinit();
@@ -238,6 +239,7 @@ int audio_extn_usb_get_max_channels(bool playback);
int audio_extn_usb_get_max_bit_width(bool playback);
int audio_extn_usb_get_sup_sample_rates(int type, uint32_t *sr, uint32_t l);
bool audio_extn_usb_is_tunnel_supported();
bool audio_extn_usb_alive(int card);
#endif

#ifndef SPLIT_A2DP_ENABLED
+7 −0
Original line number Diff line number Diff line
@@ -1133,6 +1133,13 @@ exit:
    return;
}

bool audio_extn_usb_alive(int card) {
    char path[PATH_MAX] = {0};
    // snprintf should never fail
    (void) snprintf(path, sizeof(path), "/proc/asound/card%u/stream0", card);
    return access(path, F_OK) == 0;
}

void audio_extn_usb_init(void *adev)
{
    if (usbmod == NULL) {
+47 −7
Original line number Diff line number Diff line
@@ -3573,6 +3573,15 @@ static void out_snd_mon_cb(void * stream, struct str_parms * parms)
    return;
}

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;
@@ -3640,6 +3649,22 @@ static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
                }
            }
        }

        audio_devices_t new_dev = val;

        // Workaround: If routing to an non existing usb device, fail gracefully
        // The routing request will otherwise block during 10 second
        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);
            ret = -ENOSYS;
            goto routing_fail;
        }

        /*
         * select_devices() call below switches all the usecases on the same
         * backend to the new device. Refer to check_usecases_codec_backend() in
@@ -3714,6 +3739,7 @@ static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
        pthread_mutex_unlock(&adev->lock);
        pthread_mutex_unlock(&out->lock);
    }
    routing_fail:

    if (out == adev->primary_output) {
        pthread_mutex_lock(&adev->lock);
@@ -5125,18 +5151,32 @@ static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
    err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
    if (err >= 0) {
        val = atoi(value);
        if (((int)in->device != val) && (val != 0)) {
        if (((int)in->device != val) && (val != 0) && audio_is_input_device(val) ) {

            // Workaround: If routing to an non existing usb device, fail gracefully
            // The routing request will otherwise block during 10 second
            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);
                ret = -ENOSYS;
            } else {

                in->device = val;
                /* If recording is in progress, change the tx device to new device */
                if (!in->standby && !in->is_st_session) {
                    ALOGV("update input routing change");
                if (adev->adm_on_routing_change)
                    // inform adm before actual routing to prevent glitches.
                    if (adev->adm_on_routing_change) {
                        adev->adm_on_routing_change(adev->adm_data,
                                                    in->capture_handle);
                        ret = select_devices(adev, in->usecase);
                    }
                }
            }
        }
    }

    err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_PROFILE, value, sizeof(value));
    if (err >= 0) {