Fixed wasapi thread panics involving the main(or other) thread(when Stream drops). (#756)
when main(or other) thread(s) try to drop the Stream object, they would panic at host/wasapi/stream.rs Stream::drop() if the wasapi thread is dead. this occurs when the user wants to do something after all the cpal objects have been dropped.
This commit is contained in:
parent
72d5731256
commit
6ed426614a
1 changed files with 13 additions and 10 deletions
|
|
@ -6,7 +6,7 @@ use crate::{
|
||||||
};
|
};
|
||||||
use std::mem;
|
use std::mem;
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
use std::sync::mpsc::{channel, Receiver, Sender};
|
use std::sync::mpsc::{channel, Receiver, SendError, Sender};
|
||||||
use std::thread::{self, JoinHandle};
|
use std::thread::{self, JoinHandle};
|
||||||
use windows::Win32::Foundation;
|
use windows::Win32::Foundation;
|
||||||
use windows::Win32::Foundation::WAIT_OBJECT_0;
|
use windows::Win32::Foundation::WAIT_OBJECT_0;
|
||||||
|
|
@ -147,34 +147,37 @@ impl Stream {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn push_command(&self, command: Command) {
|
fn push_command(&self, command: Command) -> Result<(), SendError<Command>> {
|
||||||
// Sender generally outlives receiver, unless the device gets unplugged.
|
self.commands.send(command)?;
|
||||||
let _ = self.commands.send(command);
|
|
||||||
unsafe {
|
unsafe {
|
||||||
let result = Threading::SetEvent(self.pending_scheduled_event);
|
let result = Threading::SetEvent(self.pending_scheduled_event);
|
||||||
assert_ne!(result, false);
|
assert_ne!(result, false);
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Drop for Stream {
|
impl Drop for Stream {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
self.push_command(Command::Terminate);
|
if let Ok(_) = self.push_command(Command::Terminate) {
|
||||||
self.thread.take().unwrap().join().unwrap();
|
self.thread.take().unwrap().join().unwrap();
|
||||||
unsafe {
|
unsafe {
|
||||||
Foundation::CloseHandle(self.pending_scheduled_event);
|
Foundation::CloseHandle(self.pending_scheduled_event);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl StreamTrait for Stream {
|
impl StreamTrait for Stream {
|
||||||
fn play(&self) -> Result<(), PlayStreamError> {
|
fn play(&self) -> Result<(), PlayStreamError> {
|
||||||
self.push_command(Command::PlayStream);
|
self.push_command(Command::PlayStream)
|
||||||
|
.map_err(|_| crate::error::PlayStreamError::DeviceNotAvailable)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
fn pause(&self) -> Result<(), PauseStreamError> {
|
fn pause(&self) -> Result<(), PauseStreamError> {
|
||||||
self.push_command(Command::PauseStream);
|
self.push_command(Command::PauseStream)
|
||||||
|
.map_err(|_| crate::error::PauseStreamError::DeviceNotAvailable)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue