Adjust "process_commands" and "Command"

This commit is contained in:
Viktor Lazarev 2019-08-28 19:14:36 +02:00 committed by mitchmindtree
parent 28cf3fee43
commit dd418c08ae
1 changed files with 23 additions and 62 deletions

View File

@ -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);
run_context.handles.push(event);
},
Command::DestroyStream(stream_id) => {
match run_context.stream.iter().position(|s| s.id == stream_id) {
None => continue,
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 { let hresult = unsafe {
(*run_context.stream[p].audio_client).Start() (*run_context.stream.audio_client).Start()
}; };
match stream_error_from_hresult(hresult) {
Ok(()) => { if let Err(err) = stream_error_from_hresult(hresult) {
run_context.stream[p].playing = true; return Err(err);
}
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,
Some(p) => {
if run_context.stream[p].playing {
let hresult = unsafe { let hresult = unsafe {
(*run_context.stream[p].audio_client).Stop() (*run_context.stream.audio_client).Stop()
}; };
match stream_error_from_hresult(hresult) { if let Err(err) = stream_error_from_hresult(hresult) {
Ok(()) => { return Err(err);
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);
}
} }
run_context.stream.playing = false;
} }
}, },
} }
},
}
}
} }
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