Вопрос

I am developing an application that receives a stream of audio samples from a remote server via custom UDP protocol. The stream consists of small sequences of samples, every sequence may have different number of channels and rate.

As I understand, the sample_spec of the stream can only be set during the stream's creation (pa_simple_new, pa_stream_new, etc) and there is no way to switch it afterwards.

My question is: what would be the most efficient approach to these samples' playback in PulseAudio? I see three options:

  1. Keep a single PulseAudio stream, manually resample the sample sequences and mix channels when needed (in order to feed PulseAudio with samples of the same sample_spec)
  2. Keep a single PulseAudio stream, close and reopen it with new sample_spec when specs change
  3. Keep a number of streams, one for every kind of samples I have to work with

Option 1 seems like a lot of manual work, and pretty much resource-consuming, too. Option 2 seems to be terribly inefficient. Option 3 looks like probably the best one, but I am concerned about resource consumption. Is there something I may be missing?

Это было полезно?

Решение

Resampling yourself would be a wasted effort. You do have the option of updating the sample rate dynamically. You need to set the PA_STREAM_VARIABLE_RATE and use pa_stream_update_sample_rate(). This still doesn't handle sample spec changes, though.

2 and 3 are both valid, and clearly there's a tradeoff between allocating resources up front (option 3) and having a minor performance penalty (option 2). To be quite honest, stream creation isn't that expensive, and holding a stream shouldn't be high overhead either (unless you're in a severely resource-constrained environment).

So if you've got a fixed number of spec/rate combinations, just create the streams upfront. If the change in spec/rate is rare, just tear down and recreate the stream again.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top