-
Notifications
You must be signed in to change notification settings - Fork 469
Avoid calling alsa::PCM::new() multiple times. #506
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Ensures that alsa::PCM::new() is called only once for a given Device so that we are not rapidly opening and closing ALSA devices.
mitchmindtree
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot for the PR!
This looks fine to me, but I should cc @diwic as they're more intimate with the alsa API than I.
If we don't hear back I'm happy to merge though :)
|
Hi and thanks for the PR!
I would argue that this would be a bug in the driver if this is the case. But anyhow...
...this sounds like a good idea. Opening and closing devices can cause clicks sometimes, and can also take some milliseconds to complete, and if we can minimize that we should definitely do it.
Is there any reason That a Mutex is needed at all is a bit unfortunate (Rust can hand over ownership just fine) but we have just a
|
I agree, but there's not much cpal can do about that.
Just for clarity, this use case is supported by the patch. When the first stream is opened it takes the
This behavior is different, but don't think it's likely to cause a problem. The only way to get a non-default |
|
@alexmoon Thanks for the answers! So I have not been involved in cpal's design, I just know ALSA from having worked with it previously. So if other maintainers prefer parking_lot's mutex over std's I don't mind either.
Ah, now I see it. And something similar goes for supported_configs. From a code readability perspective I wonder if it would be good to refactor the creation of PCM's into a separate function, perhaps if the new |
Yes, that's a good improvement. I've updated the PR. |
diwic
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the update! I've done more suggestions; they are just my opinions though so if you disagree then I'm fine with this PR merged as it is.
|
I think this looks good now, so I merged it. Thanks for the work! |
Remove handle caching introduced in #506. The caching held devices open during enumeration, which prevented querying both input and output configs on duplex devices (EBUSY errors) and blocked other applications from accessing the devices. For the rare hardware where rapid open/close is problematic (like some NVIDIA HDMI cards), applications can now implement retry logic using the new DeviceBusy error variant, which separates retriable errors (EBUSY, EAGAIN) from permanent failures (ENOENT, EPERM, etc). Fixes: - #615 - #634
Remove handle caching introduced in #506. The caching held devices open during enumeration, which prevented querying both input and output configs on duplex devices (EBUSY errors) and blocked other applications from accessing the devices. For the rare hardware where rapid open/close is problematic (like some NVIDIA HDMI cards), applications can now implement retry logic using the new DeviceBusy error variant, which separates retriable errors (EBUSY, EAGAIN) from permanent failures (ENOENT, EPERM, etc). Fixes: - #615 - #634
Remove handle caching introduced in #506. The caching held devices open during enumeration, which prevented querying both input and output configs on duplex devices (EBUSY errors) and blocked other applications from accessing the devices. For the rare hardware where rapid open/close is problematic (like some NVIDIA HDMI cards), applications can now implement retry logic using the new DeviceBusy error variant, which separates retriable errors (EBUSY, EAGAIN) from permanent failures (ENOENT, EPERM, etc). Fixes: - #615 - #634
* chore(alsa): bump alsa-rs to 0.11 * feat(alsa): add Debug derives * feat(alsa): improve error handling * fix(alsa): suppress raw ALSA errors during enumeration on Linux * fix(alsa): remove device handle caching to fix duplex config queries Remove handle caching introduced in #506. The caching held devices open during enumeration, which prevented querying both input and output configs on duplex devices (EBUSY errors) and blocked other applications from accessing the devices. For the rare hardware where rapid open/close is problematic (like some NVIDIA HDMI cards), applications can now implement retry logic using the new DeviceBusy error variant, which separates retriable errors (EBUSY, EAGAIN) from permanent failures (ENOENT, EPERM, etc). * feat(alsa): properly check and free ALSA global config When the last Host is dropped, free the global ALSA config cache via alsa::config::update_free_global. This reduces Valgrind errors. * chore(alsa): raise MSRV to 1.82 * chore: bump overall MSRV to 1.78 and update CI Fixes: - #384 - #615 - #634
Some low-level ALSA devices seem to not react well to having
snd_pcm_open()called immediately aftersnd_pcm_close()on another handle for the same device. See for example this StackOverflow answer.Currently enumerating ALSA devices will cause each device to be opened and immediately closed, as will calling
supported_configs(). Finally, the device is only opened for good whenbuild_*_stream_[raw]()is called.This patch ensures that
alsa::PCM::new()is called only once for a givenDeviceby saving eachPCMobject in theDeviceinstance the first time it is opened and then handing ownership off to theStreamwhen it is created. Aparking_lot::Mutexis used to provide interior mutability andSyncfor theDevice.This also ensures that a
Devicethat isn't shared can't be "stolen" by another process between when it is enumerated and when the stream is opened.