Skip to content
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
5 changes: 3 additions & 2 deletions crates/kira/src/sound/streaming/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ impl<Error: Send + 'static> StreamingSoundData<Error> {
shared,
command_writers,
error_consumer: Mutex::new(error_consumer),
thread: None,
};
Ok((sound, handle, scheduler))
}
Expand All @@ -404,8 +405,8 @@ impl<Error: Send + 'static> SoundData for StreamingSoundData<Error> {

#[allow(clippy::type_complexity)]
fn into_sound(self) -> Result<(Box<dyn crate::sound::Sound>, 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))
}
}
11 changes: 11 additions & 0 deletions crates/kira/src/sound/streaming/handle.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{
fmt::{Debug, Formatter},
sync::{Arc, Mutex},
thread::JoinHandle,
};

use crate::{
Expand All @@ -17,6 +18,16 @@ pub struct StreamingSoundHandle<Error> {
pub(super) shared: Arc<Shared>,
pub(super) command_writers: CommandWriters,
pub(super) error_consumer: Mutex<Consumer<Error>>,
pub(super) thread: Option<JoinHandle<()>>,
}

impl<E> Drop for StreamingSoundHandle<E> {
fn drop(&mut self) {
self.shared.set_state(PlaybackState::Stopped);
if let Some(thread) = self.thread.take() {
let _ = thread.join();
}
}
}

impl<Error> StreamingSoundHandle<Error> {
Expand Down
11 changes: 7 additions & 4 deletions crates/kira/src/sound/streaming/sound/decode_scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ use crate::{
frame::Frame,
sound::{
PlaybackState,
streaming::{DecodeSchedulerCommandReaders, StreamingSoundSettings, decoder::Decoder},
streaming::{
DecodeSchedulerCommandReaders, StreamingSoundHandle, StreamingSoundSettings,
decoder::Decoder,
},
transport::Transport,
},
};
Expand Down Expand Up @@ -91,8 +94,8 @@ impl<Error: Send + 'static> DecodeScheduler<Error> {
self.transport.position
}

pub fn start(mut self) {
std::thread::spawn(move || {
pub fn start<E: Send + 'static>(mut self, handle: &mut StreamingSoundHandle<E>) {
handle.thread = Some(std::thread::spawn(move || {
loop {
match self.run() {
Ok(result) => match result {
Expand All @@ -106,7 +109,7 @@ impl<Error: Send + 'static> DecodeScheduler<Error> {
}
}
}
});
}));
}

pub fn run(&mut self) -> Result<NextStep, Error> {
Expand Down
17 changes: 17 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
@@ -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
Loading