Adjust "process_commands" and "Command"
This commit is contained in:
parent
28cf3fee43
commit
dd418c08ae
|
@ -67,10 +67,8 @@ struct RunContext {
|
||||||
}
|
}
|
||||||
|
|
||||||
enum Command {
|
enum Command {
|
||||||
NewStream(StreamInner),
|
PlayStream,
|
||||||
DestroyStream(StreamId),
|
PauseStream,
|
||||||
PlayStream(StreamId),
|
|
||||||
PauseStream(StreamId),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
enum AudioClientFlow {
|
enum AudioClientFlow {
|
||||||
|
@ -575,75 +573,38 @@ fn format_to_waveformatextensible(format: &Format) -> Option<mmreg::WAVEFORMATEX
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process any pending commands that are queued within the `RunContext`.
|
// Process any pending commands that are queued within the `RunContext`.
|
||||||
fn process_commands(
|
fn process_commands(run_context: &mut RunContext) -> Result<(), StreamError> {
|
||||||
run_context: &mut RunContext,
|
|
||||||
callback: &mut dyn FnMut(StreamId, StreamData),
|
|
||||||
) {
|
|
||||||
// Process the pending commands.
|
// Process the pending commands.
|
||||||
for command in run_context.commands.try_iter() {
|
for command in run_context.commands.try_iter() {
|
||||||
match command {
|
match command {
|
||||||
Command::NewStream(stream_inner) => {
|
Command::PlayStream => {
|
||||||
let event = stream_inner.event;
|
if !run_context.stream.playing {
|
||||||
run_context.stream.push(stream_inner);
|
let hresult = unsafe {
|
||||||
run_context.handles.push(event);
|
(*run_context.stream.audio_client).Start()
|
||||||
},
|
};
|
||||||
Command::DestroyStream(stream_id) => {
|
|
||||||
match run_context.stream.iter().position(|s| s.id == stream_id) {
|
if let Err(err) = stream_error_from_hresult(hresult) {
|
||||||
None => continue,
|
return Err(err);
|
||||||
Some(p) => {
|
|
||||||
run_context.handles.remove(p + 1);
|
|
||||||
run_context.stream.remove(p);
|
|
||||||
},
|
|
||||||
}
|
|
||||||
},
|
|
||||||
Command::PlayStream(stream_id) => {
|
|
||||||
match run_context.stream.iter().position(|s| s.id == stream_id) {
|
|
||||||
None => continue,
|
|
||||||
Some(p) => {
|
|
||||||
if !run_context.stream[p].playing {
|
|
||||||
let hresult = unsafe {
|
|
||||||
(*run_context.stream[p].audio_client).Start()
|
|
||||||
};
|
|
||||||
match stream_error_from_hresult(hresult) {
|
|
||||||
Ok(()) => {
|
|
||||||
run_context.stream[p].playing = true;
|
|
||||||
}
|
|
||||||
Err(err) => {
|
|
||||||
callback(stream_id, Err(err.into()));
|
|
||||||
run_context.handles.remove(p + 1);
|
|
||||||
run_context.stream.remove(p);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
run_context.stream.playing = true;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Command::PauseStream(stream_id) => {
|
Command::PauseStream => {
|
||||||
match run_context.stream.iter().position(|s| s.id == stream_id) {
|
if run_context.stream.playing {
|
||||||
None => continue,
|
let hresult = unsafe {
|
||||||
Some(p) => {
|
(*run_context.stream.audio_client).Stop()
|
||||||
if run_context.stream[p].playing {
|
};
|
||||||
let hresult = unsafe {
|
if let Err(err) = stream_error_from_hresult(hresult) {
|
||||||
(*run_context.stream[p].audio_client).Stop()
|
return Err(err);
|
||||||
};
|
}
|
||||||
match stream_error_from_hresult(hresult) {
|
run_context.stream.playing = false;
|
||||||
Ok(()) => {
|
|
||||||
run_context.stream[p].playing = false;
|
|
||||||
}
|
|
||||||
Err(err) => {
|
|
||||||
callback(stream_id, Err(err.into()));
|
|
||||||
run_context.handles.remove(p + 1);
|
|
||||||
run_context.stream.remove(p);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
// Wait for any of the given handles to be signalled.
|
// Wait for any of the given handles to be signalled.
|
||||||
//
|
//
|
||||||
// Returns the index of the `handle` that was signalled, or an `Err` if
|
// Returns the index of the `handle` that was signalled, or an `Err` if
|
||||||
|
|
Loading…
Reference in New Issue