Skip to content

Commit 084e0c7

Browse files
committed
Validate output config before opening stream
1 parent de4d06d commit 084e0c7

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/// Stream sample rate (samples per second per channel).
1+
/// Stream sample rate (a frame rate or samples per second per channel).
22
pub type SampleRate = u32;
33

44
/// Number of channels in a stream.

src/stream.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,10 +317,19 @@ impl error::Error for StreamError {
317317
}
318318

319319
impl OutputStream {
320+
fn validate_config(config: &OutputStreamConfig) {
321+
if let BufferSize::Fixed(sz) = config.buffer_size {
322+
assert!(sz > 0, "fixed buffer size is greater than zero");
323+
}
324+
assert!(config.sample_rate > 0, "sample rate is greater than zero");
325+
assert!(config.channel_count > 0, "channel number is greater than zero");
326+
}
327+
320328
fn open(
321329
device: &cpal::Device,
322330
config: &OutputStreamConfig,
323331
) -> Result<OutputStream, StreamError> {
332+
Self::validate_config(config);
324333
let (controller, source) = mixer(config.channel_count, config.sample_rate);
325334
Self::init_stream(device, config, source)
326335
.map_err(StreamError::BuildStreamError)

0 commit comments

Comments
 (0)