diff --git a/crates/kira/src/sound/streaming/data.rs b/crates/kira/src/sound/streaming/data.rs index b37ba94e..9b7dea22 100644 --- a/crates/kira/src/sound/streaming/data.rs +++ b/crates/kira/src/sound/streaming/data.rs @@ -392,6 +392,7 @@ impl StreamingSoundData { shared, command_writers, error_consumer: Mutex::new(error_consumer), + thread: None, }; Ok((sound, handle, scheduler)) } @@ -404,8 +405,8 @@ impl SoundData for StreamingSoundData { #[allow(clippy::type_complexity)] fn into_sound(self) -> Result<(Box, Self::Handle), Self::Error> { - let (sound, handle, scheduler) = self.split()?; - scheduler.start(); + let (sound, mut handle, scheduler) = self.split()?; + scheduler.start(&mut handle); Ok((Box::new(sound), handle)) } } diff --git a/crates/kira/src/sound/streaming/handle.rs b/crates/kira/src/sound/streaming/handle.rs index 97bb4b82..e5eb9629 100644 --- a/crates/kira/src/sound/streaming/handle.rs +++ b/crates/kira/src/sound/streaming/handle.rs @@ -1,6 +1,7 @@ use std::{ fmt::{Debug, Formatter}, sync::{Arc, Mutex}, + thread::JoinHandle, }; use crate::{ @@ -17,6 +18,16 @@ pub struct StreamingSoundHandle { pub(super) shared: Arc, pub(super) command_writers: CommandWriters, pub(super) error_consumer: Mutex>, + pub(super) thread: Option>, +} + +impl Drop for StreamingSoundHandle { + fn drop(&mut self) { + self.shared.set_state(PlaybackState::Stopped); + if let Some(thread) = self.thread.take() { + let _ = thread.join(); + } + } } impl StreamingSoundHandle { diff --git a/crates/kira/src/sound/streaming/sound/decode_scheduler.rs b/crates/kira/src/sound/streaming/sound/decode_scheduler.rs index d3428412..835e236e 100644 --- a/crates/kira/src/sound/streaming/sound/decode_scheduler.rs +++ b/crates/kira/src/sound/streaming/sound/decode_scheduler.rs @@ -7,7 +7,10 @@ use crate::{ frame::Frame, sound::{ PlaybackState, - streaming::{DecodeSchedulerCommandReaders, StreamingSoundSettings, decoder::Decoder}, + streaming::{ + DecodeSchedulerCommandReaders, StreamingSoundHandle, StreamingSoundSettings, + decoder::Decoder, + }, transport::Transport, }, }; @@ -91,8 +94,8 @@ impl DecodeScheduler { self.transport.position } - pub fn start(mut self) { - std::thread::spawn(move || { + pub fn start(mut self, handle: &mut StreamingSoundHandle) { + handle.thread = Some(std::thread::spawn(move || { loop { match self.run() { Ok(result) => match result { @@ -106,7 +109,7 @@ impl DecodeScheduler { } } } - }); + })); } pub fn run(&mut self) -> Result { diff --git a/justfile b/justfile new file mode 100644 index 00000000..697bfebf --- /dev/null +++ b/justfile @@ -0,0 +1,17 @@ +set windows-shell := ["powershell.exe", "-NoLogo", "-Command"] +set dotenv-load + +check: + cargo clippy -p kira --no-default-features + cargo clippy -p kira --no-default-features --features=mp3 --target=wasm32-unknown-unknown + +test: + cargo test -p kira --all-features + cargo test -p kira --no-default-features --lib + cargo test -p kira --no-default-features --features=cpal --lib + cargo test -p kira --no-default-features --features=mp3 --lib + +lint: + cargo clippy --fix --allow-dirty + cargo fix --allow-dirty + cargo fmt \ No newline at end of file