Receive: Config of decode sample rate/channels (#265)

* Receive: Config of decode sample rate/channels

This PR allows for dynamic configuration of the output sample rate and
channel count of received Opus audio. Users who rely on supported
formats should no longer need to manually resample & downmix audio
decoded from SSRCs in a call.

Opus exposes tuples of (Mono, Stereo) x (8, 12, 16, 24, 48)kHz.
Changing this at runtime (mid-call) may cause some audio glitches, as
decoder state must be reconstructed from scratch for all affected SSRCs.

* Fix doc typo, consistent naming with MixMode.
This commit is contained in:
Kyle Simpson
2024-11-16 11:57:52 +00:00
committed by GitHub
parent 312799d231
commit 91bf1538fc
6 changed files with 134 additions and 11 deletions

View File

@@ -1,5 +1,5 @@
#[cfg(feature = "receive")]
use crate::driver::DecodeMode;
#[cfg(all(feature = "driver", feature = "receive"))]
use crate::driver::{Channels, DecodeMode, SampleRate};
#[cfg(feature = "driver")]
use crate::{
driver::{
@@ -61,6 +61,18 @@ pub struct Config {
/// [User speaking state]: crate::events::CoreEvent::VoiceTick
pub decode_mode: DecodeMode,
#[cfg(all(feature = "driver", feature = "receive"))]
/// Configures the channel layout for output audio when using [`DecodeMode::Decode`].
///
/// Defaults to [`Channels::Stereo`].
pub decode_channels: Channels,
#[cfg(all(feature = "driver", feature = "receive"))]
/// Configures the sample rate for output audio when using [`DecodeMode::Decode`].
///
/// Defaults to [`SampleRate::Hz48000`].
pub decode_sample_rate: SampleRate,
#[cfg(all(feature = "driver", feature = "receive"))]
/// Configures the amount of time after a user/SSRC is inactive before their decoder state
/// should be removed.
@@ -215,6 +227,10 @@ impl Default for Config {
#[cfg(all(feature = "driver", feature = "receive"))]
decode_mode: DecodeMode::Decrypt,
#[cfg(all(feature = "driver", feature = "receive"))]
decode_channels: Channels::Stereo,
#[cfg(all(feature = "driver", feature = "receive"))]
decode_sample_rate: SampleRate::Hz48000,
#[cfg(all(feature = "driver", feature = "receive"))]
decode_state_timeout: Duration::from_secs(60),
#[cfg(all(feature = "driver", feature = "receive"))]
playout_buffer_length: NonZeroUsize::new(5).unwrap(),
@@ -267,6 +283,22 @@ impl Config {
self
}
#[cfg(feature = "receive")]
/// Sets this `Config`'s channel layout for output audio when using [`DecodeMode::Decode`]
#[must_use]
pub fn decode_channels(mut self, decode_channels: Channels) -> Self {
self.decode_channels = decode_channels;
self
}
#[cfg(feature = "receive")]
/// Sets this `Config`'s sample rate for output audio when using [`DecodeMode::Decode`]
#[must_use]
pub fn decode_sample_rate(mut self, decode_sample_rate: SampleRate) -> Self {
self.decode_sample_rate = decode_sample_rate;
self
}
#[cfg(feature = "receive")]
/// Sets this `Config`'s received packet decoder cleanup timer.
#[must_use]