mozilla / cubeb

Cross platform audio library
ISC License
439 stars 124 forks source link

macOS: cubeb_enumerate_devices seems to crash when airpods are "disconnected" #719

Open Megamouse opened 2 years ago

Megamouse commented 2 years ago

A user reported that the application crashes when they takes off their airpods while the device is selected as default device. I tracked this down to cubeb_enumerate_devices.

In particular, we try to reconnect a new default device in the device_collection_changed_cb. EDIT: the default device is connected and a stream is open before the airpods are taken off

So here's a snippet:

void MyClass::init()
{
    if (int err = cubeb_init(&m_context, "MyContext", nullptr))
    {
        printf("Error in cubeb_init\n");
        return;
    }

    if (int err = cubeb_register_device_collection_changed(m_context, CUBEB_DEVICE_TYPE_OUTPUT, device_collection_changed_cb, this))
    {
        printf("Error in cubeb_register_device_collection_changed\n");
        return;
    }
}

void MyClass::device_collection_changed_cb(cubeb* context, void* user_ptr)
{
    if (context == nullptr) return;

    cubeb_device_collection collection{};

    printf("Begin enumeration\n"); // THIS WAS THE LAST LOG MESSAGE

    int err = cubeb_enumerate_devices(context, CUBEB_DEVICE_TYPE_OUTPUT, &collection);

    // THIS IS NEVER REACHED

    if (err != CUBEB_OK)
    {
        printf("Error in cubeb_enumerate_devices\n");
        return;
    }

    // Do other cool stuff
}
padenot commented 2 years ago

I can't reproduce this. Here's what I tried:

This works as it should here on macOS 12.5. Maybe it would be possible to get stacks so we can figure out what's going on?

mygizli04 commented 2 years ago

Hi, I'm the user which has the issue in the original post. I tested again on macOS 12.5.1 and the issue only occurs on macOS 13.0 beta. I'm not sure how to provide stacks, should I reproduce the crash on macOS 13 and post the "error report" here?

padenot commented 2 years ago

If you can attach a debugger, and reproduce the crash, it would be amazing, as I don't have macOS 13 beta yet on a machine. It would go like this:

kinetiknz commented 1 year ago

Invoking the cubeb APIs from within a cubeb callback is not safe in general, unfortunately. Some combinations of calls/backends may happen to work, but I'd recommend using the device_collection_changed_cb to signal some other thread to re-enumerate and handle any other stream configuration you need to do.