GNOME Bugzilla – Bug 559111
ALSA sink hangs on USB audio device unplug while playing
Last modified: 2008-11-03 17:09:53 UTC
The ALSA sink uses blocking writes to write to the soundcard. If the audio device is unplugged this write will block forever. It should use non-blocking writes instead and handle the case of an unplugged device.
Created attachment 121868 [details] [review] Patch to make writes non-blocking
I changed the patch a little so that it leaves the device open in non-blocking mode. I rearranged the code so that it starts with a wait operation before attempting the write. Please test it this also solves your problems. Based on patch by: Matthias Kretz <kretz at kde dot org> * ext/alsa/gstalsasink.c: (gst_alsasink_open), (gst_alsasink_prepare), (gst_alsasink_unprepare), (gst_alsasink_write): Make all access non-blocking so that we can better handle unplugging of usb devices. Fixes #559111
It's pretty lame that userspace has to care about this - ALSA should be failing the read if it can't complete it afaics.
Careful with fully switching to non-blocking unconditionally. I've seen libxine failing to play the last audio frames because of a non-blocking snd_pcm_drain and snd_pcm_close. AFAICS you're not using snd_pcm_drain, so gstreamer might not need blocking mode at all, but as I don't know how the end of a stream and closing of the output is handled I thought I point this out so that you can check it doesn't introduce regressions... I also just tested your patch and so far it seems to work alright.