Skip to content

Make accidentally dropping OutputStream impossible #746

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 83 additions & 80 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ tracing = { version = "0.1.40", optional = true }

atomic_float = { version = "1.1.0", optional = true }
num-rational = "0.4.2"
drop_bomb = "0.1.5"

[features]
default = ["playback", "flac", "vorbis", "wav", "mp3"]
Expand Down
1 change: 1 addition & 0 deletions examples/automatic_gain_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,6 @@ fn main() -> Result<(), Box<dyn Error>> {

// Keep the program running until the playback is complete.
sink.sleep_until_end();
stream_handle.close();
Ok(())
}
1 change: 1 addition & 0 deletions examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,6 @@ fn main() -> Result<(), Box<dyn Error>> {

thread::sleep(Duration::from_millis(1500));

stream_handle.close();
Ok(())
}
1 change: 1 addition & 0 deletions examples/callback_on_end.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ fn main() -> Result<(), Box<dyn Error>> {
playlist_pos.load(Ordering::Relaxed)
);

stream_handle.close();
Ok(())
}
1 change: 1 addition & 0 deletions examples/custom_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ fn main() -> Result<(), Box<dyn Error>> {
println!("Beep...");
thread::sleep(Duration::from_millis(1500));

stream_handle.close();
Ok(())
}
1 change: 1 addition & 0 deletions examples/distortion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ fn main() -> Result<(), Box<dyn Error>> {
thread::sleep(Duration::from_secs(3));
println!("Done.");

stream_handle.close();
Ok(())
}
1 change: 1 addition & 0 deletions examples/distortion_mp3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ fn main() -> Result<(), Box<dyn Error>> {

sink.sleep_until_end();

stream_handle.close();
Ok(())
}
1 change: 1 addition & 0 deletions examples/distortion_wav.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ fn main() -> Result<(), Box<dyn Error>> {

sink.sleep_until_end();

stream_handle.close();
Ok(())
}
1 change: 1 addition & 0 deletions examples/distortion_wav_alternate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,6 @@ fn main() -> Result<(), Box<dyn Error>> {
// Wait for playback to finish
sink.sleep_until_end();

stream_handle.close();
Ok(())
}
1 change: 1 addition & 0 deletions examples/error_callback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ fn main() -> Result<(), Box<dyn Error>> {
eprintln!("Error with stream {}", err);
}

stream_handle.close();
Ok(())
}
1 change: 1 addition & 0 deletions examples/low_pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ fn main() -> Result<(), Box<dyn Error>> {

sink.sleep_until_end();

stream_handle.close();
Ok(())
}
1 change: 1 addition & 0 deletions examples/mix_multiple_sources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ fn main() -> Result<(), Box<dyn Error>> {
// Sleep the thread until sink is empty.
sink.sleep_until_end();

stream_handle.close();
Ok(())
}
1 change: 1 addition & 0 deletions examples/music_flac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ fn main() -> Result<(), Box<dyn Error>> {

sink.sleep_until_end();

stream_handle.close();
Ok(())
}
1 change: 1 addition & 0 deletions examples/music_m4a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ fn main() -> Result<(), Box<dyn Error>> {

sink.sleep_until_end();

stream_handle.close();
Ok(())
}
1 change: 1 addition & 0 deletions examples/music_mp3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ fn main() -> Result<(), Box<dyn Error>> {

sink.sleep_until_end();

stream_handle.close();
Ok(())
}
1 change: 1 addition & 0 deletions examples/music_ogg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ fn main() -> Result<(), Box<dyn Error>> {

sink.sleep_until_end();

stream_handle.close();
Ok(())
}
1 change: 1 addition & 0 deletions examples/music_wav.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ fn main() -> Result<(), Box<dyn Error>> {

sink.sleep_until_end();

stream_handle.close();
Ok(())
}
1 change: 1 addition & 0 deletions examples/noise_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ fn main() -> Result<(), Box<dyn Error>> {

thread::sleep(interval_duration);

stream_handle.close();
Ok(())
}
1 change: 1 addition & 0 deletions examples/reverb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ fn main() -> Result<(), Box<dyn Error>> {

sink.sleep_until_end();

stream_handle.close();
Ok(())
}
1 change: 1 addition & 0 deletions examples/seek_mp3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ fn main() -> Result<(), Box<dyn Error>> {
sink.try_seek(Duration::from_secs(5))?;
println!("seek example ended");

stream_handle.close();
Ok(())
}
1 change: 1 addition & 0 deletions examples/signal_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,6 @@ fn main() -> Result<(), Box<dyn Error>> {

thread::sleep(interval_duration);

stream_handle.close();
Ok(())
}
1 change: 1 addition & 0 deletions examples/spatial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,6 @@ fn main() -> Result<(), Box<dyn Error>> {
}
sink.sleep_until_end();

stream_handle.close();
Ok(())
}
1 change: 1 addition & 0 deletions examples/stereo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ fn main() -> Result<(), Box<dyn Error>> {

sink.sleep_until_end();

stream_handle.close();
Ok(())
}
22 changes: 19 additions & 3 deletions src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,33 @@ use crate::mixer::{mixer, Mixer, MixerSource};
use crate::sink::Sink;
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
use cpal::{BufferSize, Sample, SampleFormat, StreamConfig};
use drop_bomb::DropBomb;
use std::io::{Read, Seek};
use std::marker::Sync;
use std::{error, fmt};

const HZ_44100: SampleRate = 44_100;

/// `cpal::Stream` container.
/// Use `mixer()` method to control output.
/// If this is dropped, playback will end, and the associated output stream will be disposed.
/// `cpal::Stream` container. Use `mixer()` method to control output. This
/// type **can not be dropped**. It will panic and crash your program if
/// dropped normally. Instead you must call [`OutputStream::close`].
///
/// # Panics
/// This will panic if dropped. Instead call [`OutputStream::close`]
pub struct OutputStream {
config: OutputStreamConfig,
mixer: Mixer,
_stream: cpal::Stream,
bomb: DropBomb,
}

impl OutputStream {
/// Closes the outputstream and drops it whithout panicking. This will
/// stop all audio playing through this `OutputStream`.
pub fn close(mut self) {
self.bomb.defuse();
}

/// Access the output stream's mixer.
pub fn mixer(&self) -> &Mixer {
&self.mixer
Expand Down Expand Up @@ -449,6 +460,11 @@ impl OutputStream {
_stream: stream,
mixer: controller,
config: *config,
bomb: DropBomb::new(
"You can not drop an `OutputStream` you \
must call `close` on it instead. \
See the docs for `OutputStream` for more",
),
})
})
}
Expand Down