cpal icon indicating copy to clipboard operation
cpal copied to clipboard

dropped frames - set scheduling priority?

Open majg0 opened this issue 6 years ago • 10 comments

On my old Windows surface, I'm hearing dropped frames even for the simple beep.rs compiled in release mode. CPU usage is kept really low as expected, so I'm thinking maybe the memory latency is the problem and the buffer size too low, but I'm a newb web guy. I'd love some help in nailing the cause down. I have not dug very much yet. As a first step I guess I should find out what backend is being used. I gather it could be either WASAPI or ALSA? I've found the buffer size to default to 441, whereas it grows to a multiple of that on frame drop. I have yet to try just copying in data instead of computing it, but tbh it's silly - it should be able to handle much more processing per frame imo. I could be conceptually off here as well though; any help is highly appreciated. My main goal atm is to just understand why this is happening and where the bottleneck is. I don't want a hacky workaround, much rather fixing the libs.

majg0 avatar May 17 '19 05:05 majg0

Two causes I can think of are:

  1. cpal uses Mutex during audio processing, which is obviously bad for realtime workload.
  2. We don't run the callback with elevated scheduling priority yet (planned for implementation).

Please try raising the process priority from task manager - if it makes a difference, I guess we would prioritize implementing number 2.

ishitatsuyuki avatar Jun 13 '19 04:06 ishitatsuyuki

FYI: I got rid of the Mutex in master.

ishitatsuyuki avatar Jun 16 '19 09:06 ishitatsuyuki

TYVM @ishitatsuyuki!

I'm firmly intent on testing and getting back with results, please bear with me while I long for a moment to do so!

majg0 avatar Jun 18 '19 12:06 majg0

So I think yeah, hiccups are actually a thing. The current WASAPI implementation uses the smallest buffer size the audio server supports, which is 10ms. Without priority scheduling at this buffer size it will soon start to glitch when CPU resources are contested.

ishitatsuyuki avatar Aug 13 '19 03:08 ishitatsuyuki

:+1: I agree. Found some links for the windows side of things

majg0 avatar Aug 13 '19 06:08 majg0

The priority thing I'm referring to is MMCSS, which doesn't require elevation and has its own set of APIs. I plan to integrate this at some point.

ishitatsuyuki avatar Aug 13 '19 06:08 ishitatsuyuki

There is a nice crate audio_thread_priority, used in firefox, implementing all the work for setting the priority.

It uses the MMCSS on windows, RealtimeKit daemon on linux and smth else on mac.

DCNick3 avatar Jan 07 '23 22:01 DCNick3

The priority thing I'm referring to is MMCSS, which doesn't require elevation and has its own set of APIs. I plan to integrate this at some point.

Any updates on this? I noticed that when you're on battery, frames start to drop because the callback takes too long with minimum processing (this doesn't happen when plugged in)

yxor avatar Mar 16 '23 14:03 yxor

Has anyone integrated the audio_thread_priority crate within a fork?

jacksongoode avatar Jul 10 '24 21:07 jacksongoode

I'm using audio_thread_priority for WASAPI in https://github.com/jangler/cpal. For me it's a big improvement over THREAD_PRIORITY_TIME_CRITICAL (which is what cpal uses now for WASAPI). I haven't integrated it with other hosts since I haven't had audio thread priority issues with ALSA, JACK, or CoreAudio.

jangler avatar Feb 21 '25 13:02 jangler