Fix up WASAPI host
This commit is contained in:
parent
d4965d3673
commit
49a38c2641
|
@ -113,7 +113,7 @@ impl DeviceTrait for Device {
|
||||||
E: FnMut(StreamError) + Send + 'static,
|
E: FnMut(StreamError) + Send + 'static,
|
||||||
{
|
{
|
||||||
Ok(Stream::new(
|
Ok(Stream::new(
|
||||||
Arc::new(self.build_input_stream_inner(format)?),
|
self.build_input_stream_inner(format)?,
|
||||||
data_callback,
|
data_callback,
|
||||||
error_callback,
|
error_callback,
|
||||||
))
|
))
|
||||||
|
@ -130,7 +130,7 @@ impl DeviceTrait for Device {
|
||||||
E: FnMut(StreamError) + Send + 'static,
|
E: FnMut(StreamError) + Send + 'static,
|
||||||
{
|
{
|
||||||
Ok(Stream::new(
|
Ok(Stream::new(
|
||||||
Arc::new(self.build_output_stream_inner(format)?),
|
self.build_output_stream_inner(format)?,
|
||||||
data_callback,
|
data_callback,
|
||||||
error_callback,
|
error_callback,
|
||||||
))
|
))
|
||||||
|
|
|
@ -13,10 +13,7 @@ use std::slice;
|
||||||
use std::sync::mpsc::{channel, Receiver, Sender};
|
use std::sync::mpsc::{channel, Receiver, Sender};
|
||||||
|
|
||||||
use crate::traits::StreamTrait;
|
use crate::traits::StreamTrait;
|
||||||
use std::{
|
use std::thread::{self, JoinHandle};
|
||||||
sync::Arc,
|
|
||||||
thread::{self, JoinHandle},
|
|
||||||
};
|
|
||||||
|
|
||||||
use BackendSpecificError;
|
use BackendSpecificError;
|
||||||
use PauseStreamError;
|
use PauseStreamError;
|
||||||
|
@ -44,7 +41,7 @@ pub struct Stream {
|
||||||
|
|
||||||
struct RunContext {
|
struct RunContext {
|
||||||
// Streams that have been created in this event loop.
|
// Streams that have been created in this event loop.
|
||||||
stream: Arc<StreamInner>,
|
stream: StreamInner,
|
||||||
|
|
||||||
// Handles corresponding to the `event` field of each element of `voices`. Must always be in
|
// Handles corresponding to the `event` field of each element of `voices`. Must always be in
|
||||||
// sync with `voices`, except that the first element is always `pending_scheduled_event`.
|
// sync with `voices`, except that the first element is always `pending_scheduled_event`.
|
||||||
|
@ -53,12 +50,15 @@ struct RunContext {
|
||||||
commands: Receiver<Command>,
|
commands: Receiver<Command>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) enum Command {
|
// Once we start running the eventloop, the RunContext will not be moved.
|
||||||
|
unsafe impl Send for RunContext {}
|
||||||
|
|
||||||
|
pub enum Command {
|
||||||
PlayStream,
|
PlayStream,
|
||||||
PauseStream,
|
PauseStream,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) enum AudioClientFlow {
|
pub enum AudioClientFlow {
|
||||||
Render {
|
Render {
|
||||||
render_client: *mut audioclient::IAudioRenderClient,
|
render_client: *mut audioclient::IAudioRenderClient,
|
||||||
},
|
},
|
||||||
|
@ -67,24 +67,24 @@ pub(crate) enum AudioClientFlow {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) struct StreamInner {
|
pub struct StreamInner {
|
||||||
audio_client: *mut audioclient::IAudioClient,
|
pub audio_client: *mut audioclient::IAudioClient,
|
||||||
client_flow: AudioClientFlow,
|
pub client_flow: AudioClientFlow,
|
||||||
// Event that is signalled by WASAPI whenever audio data must be written.
|
// Event that is signalled by WASAPI whenever audio data must be written.
|
||||||
event: winnt::HANDLE,
|
pub event: winnt::HANDLE,
|
||||||
// True if the stream is currently playing. False if paused.
|
// True if the stream is currently playing. False if paused.
|
||||||
playing: bool,
|
pub playing: bool,
|
||||||
// Number of frames of audio data in the underlying buffer allocated by WASAPI.
|
// Number of frames of audio data in the underlying buffer allocated by WASAPI.
|
||||||
max_frames_in_buffer: UINT32,
|
pub max_frames_in_buffer: UINT32,
|
||||||
// Number of bytes that each frame occupies.
|
// Number of bytes that each frame occupies.
|
||||||
bytes_per_frame: WORD,
|
pub bytes_per_frame: WORD,
|
||||||
// The sample format with which the stream was created.
|
// The sample format with which the stream was created.
|
||||||
sample_format: SampleFormat,
|
pub sample_format: SampleFormat,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Stream {
|
impl Stream {
|
||||||
pub(crate) fn new<D, E>(
|
pub(crate) fn new<D, E>(
|
||||||
stream_inner: Arc<StreamInner>,
|
stream_inner: StreamInner,
|
||||||
mut data_callback: D,
|
mut data_callback: D,
|
||||||
mut error_callback: E,
|
mut error_callback: E,
|
||||||
) -> Stream
|
) -> Stream
|
||||||
|
@ -253,7 +253,7 @@ fn stream_error_from_hresult(hresult: winnt::HRESULT) -> Result<(), StreamError>
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run_inner(
|
fn run_inner(
|
||||||
run_context: RunContext,
|
mut run_context: RunContext,
|
||||||
data_callback: &mut dyn FnMut(StreamData),
|
data_callback: &mut dyn FnMut(StreamData),
|
||||||
error_callback: &mut dyn FnMut(StreamError),
|
error_callback: &mut dyn FnMut(StreamError),
|
||||||
) {
|
) {
|
||||||
|
@ -283,7 +283,7 @@ fn run_inner(
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let stream = run_context.stream;
|
let stream = &mut run_context.stream;
|
||||||
let sample_size = stream.sample_format.sample_size();
|
let sample_size = stream.sample_format.sample_size();
|
||||||
|
|
||||||
// Obtaining a pointer to the buffer.
|
// Obtaining a pointer to the buffer.
|
||||||
|
|
Loading…
Reference in New Issue