Skip to content

Commit 26f7e99

Browse files
committed
Remove all Pause, Play and Close events
A follow up to [this comment](RustAudio#288 (comment)).
1 parent b1539c5 commit 26f7e99

File tree

5 files changed

+12
-121
lines changed

5 files changed

+12
-121
lines changed

src/alsa/mod.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use PlayStreamError;
1414
use SupportedFormatsError;
1515
use SampleFormat;
1616
use SampleRate;
17-
use StreamCloseCause;
1817
use StreamData;
1918
use StreamError;
2019
use StreamEvent;
@@ -473,7 +472,7 @@ impl EventLoop {
473472
let run_context = &mut *run_context;
474473

475474
'stream_loop: loop {
476-
process_commands(run_context, callback);
475+
process_commands(run_context);
477476

478477
reset_descriptors_with_pending_command_trigger(
479478
&mut run_context.descriptors,
@@ -827,22 +826,16 @@ impl EventLoop {
827826
}
828827

829828
// Process any pending `Command`s within the `RunContext`'s queue.
830-
fn process_commands(
831-
run_context: &mut RunContext,
832-
callback: &mut dyn FnMut(StreamId, StreamEvent),
833-
) {
829+
fn process_commands(run_context: &mut RunContext) {
834830
for command in run_context.commands.try_iter() {
835831
match command {
836832
Command::DestroyStream(stream_id) => {
837833
run_context.streams.retain(|s| s.id != stream_id);
838-
let event = StreamCloseCause::UserDestroyed.into();
839-
callback(stream_id, event);
840834
},
841835
Command::PlayStream(stream_id) => {
842836
if let Some(stream) = run_context.streams.iter_mut()
843837
.find(|stream| stream.can_pause && stream.id == stream_id)
844838
{
845-
callback(stream_id, StreamEvent::Play);
846839
unsafe {
847840
alsa::snd_pcm_pause(stream.channel, 0);
848841
}
@@ -853,7 +846,6 @@ fn process_commands(
853846
if let Some(stream) = run_context.streams.iter_mut()
854847
.find(|stream| stream.can_pause && stream.id == stream_id)
855848
{
856-
callback(stream_id, StreamEvent::Pause);
857849
unsafe {
858850
alsa::snd_pcm_pause(stream.channel, 1);
859851
}

src/coreaudio/mod.rs

Lines changed: 8 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use PlayStreamError;
1212
use SupportedFormatsError;
1313
use SampleFormat;
1414
use SampleRate;
15-
use StreamCloseCause;
1615
use StreamData;
1716
use StreamEvent;
1817
use SupportedFormat;
@@ -336,9 +335,7 @@ enum UserCallback {
336335
Active(&'static mut (FnMut(StreamId, StreamEvent) + Send)),
337336
// A queue of events that have occurred but that have not yet been emitted to the user as we
338337
// don't yet have a callback to do so.
339-
Inactive {
340-
pending_events: Vec<(StreamId, StreamEvent<'static>)>
341-
},
338+
Inactive,
342339
}
343340

344341
struct StreamInner {
@@ -440,7 +437,7 @@ impl EventLoop {
440437
#[inline]
441438
pub fn new() -> EventLoop {
442439
EventLoop {
443-
user_callback: Arc::new(Mutex::new(UserCallback::Inactive { pending_events: vec![] })),
440+
user_callback: Arc::new(Mutex::new(UserCallback::Inactive)),
444441
streams: Mutex::new(Vec::new()),
445442
}
446443
}
@@ -451,20 +448,10 @@ impl EventLoop {
451448
{
452449
{
453450
let mut guard = self.user_callback.lock().unwrap();
454-
let pending_events = match *guard {
455-
UserCallback::Inactive { ref mut pending_events } => {
456-
mem::replace(pending_events, vec![])
457-
}
458-
UserCallback::Active(_) => {
459-
panic!("`EventLoop::run` was called when the event loop was already running");
460-
}
461-
};
462-
463-
let callback: &mut (FnMut(StreamId, StreamEvent) + Send) = &mut callback;
464-
for (stream_id, event) in pending_events {
465-
callback(stream_id, event);
451+
if let UserCallback::Active(_) = *guard {
452+
panic!("`EventLoop::run` was called when the event loop was already running");
466453
}
467-
454+
let callback: &mut (FnMut(StreamId, StreamEvent) + Send) = &mut callback;
468455
*guard = UserCallback::Active(unsafe { mem::transmute(callback) });
469456
}
470457

@@ -474,7 +461,7 @@ impl EventLoop {
474461
}
475462

476463
// It is critical that we remove the callback before returning (currently not possible).
477-
// *self.user_callback.lock().unwrap() = UserCallback::Inactive { pending_events: vec![] };
464+
// *self.user_callback.lock().unwrap() = UserCallback::Inactive;
478465
}
479466

480467
fn next_stream_id(&self) -> usize {
@@ -698,7 +685,7 @@ impl EventLoop {
698685
let data_slice = slice::from_raw_parts(data as *const $SampleType, data_len);
699686
let callback = match *user_callback {
700687
UserCallback::Active(ref mut cb) => cb,
701-
UserCallback::Inactive { .. } => return Ok(()),
688+
UserCallback::Inactive => return Ok(()),
702689
};
703690
let unknown_type_buffer = UnknownTypeInputBuffer::$SampleFormat(::InputBuffer { buffer: data_slice });
704691
let stream_data = StreamData::Input { buffer: unknown_type_buffer };
@@ -770,7 +757,7 @@ impl EventLoop {
770757
let data_slice = slice::from_raw_parts_mut(data as *mut $SampleType, data_len);
771758
let callback = match *user_callback {
772759
UserCallback::Active(ref mut cb) => cb,
773-
UserCallback::Inactive { .. } => {
760+
UserCallback::Inactive => {
774761
for sample in data_slice.iter_mut() {
775762
*sample = $equilibrium;
776763
}
@@ -802,33 +789,18 @@ impl EventLoop {
802789
Ok(StreamId(stream_id))
803790
}
804791

805-
fn emit_or_enqueue_event(&self, id: StreamId, event: StreamEvent<'static>) {
806-
let mut guard = self.user_callback.lock().unwrap();
807-
match *guard {
808-
UserCallback::Active(ref mut callback) => callback(id, event),
809-
UserCallback::Inactive { ref mut pending_events } => pending_events.push((id, event)),
810-
}
811-
}
812-
813792
pub fn destroy_stream(&self, stream_id: StreamId) {
814793
{
815794
let mut streams = self.streams.lock().unwrap();
816795
streams[stream_id.0] = None;
817796
}
818-
// Emit the `Close` event to the user.
819-
let event = StreamEvent::Close(StreamCloseCause::UserDestroyed);
820-
self.emit_or_enqueue_event(stream_id, event);
821797
}
822798

823799
pub fn play_stream(&self, stream_id: StreamId) -> Result<(), PlayStreamError> {
824800
let mut streams = self.streams.lock().unwrap();
825801
let stream = streams[stream_id.0].as_mut().unwrap();
826802

827803
if !stream.playing {
828-
// Emit the `Play` event to the user. This should not block, as the stream should not
829-
// yet be playing if this is being called.
830-
self.emit_or_enqueue_event(stream_id, StreamEvent::Play);
831-
832804
if let Err(e) = stream.audio_unit.start() {
833805
let description = format!("{}", std::error::Error::description(&e));
834806
let err = BackendSpecificError { description };
@@ -850,9 +822,6 @@ impl EventLoop {
850822
return Err(err.into());
851823
}
852824

853-
// Emit the `Pause` event to the user.
854-
self.emit_or_enqueue_event(stream_id, StreamEvent::Pause);
855-
856825
stream.playing = false;
857826
}
858827
Ok(())

src/emscripten/mod.rs

Lines changed: 2 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::mem;
22
use std::os::raw::c_void;
33
use std::slice::from_raw_parts;
4-
use std::sync::{Arc, Mutex};
4+
use std::sync::Mutex;
55
use stdweb;
66
use stdweb::Reference;
77
use stdweb::unstable::TryInto;
@@ -16,7 +16,6 @@ use Format;
1616
use PauseStreamError;
1717
use PlayStreamError;
1818
use SupportedFormatsError;
19-
use StreamCloseCause;
2019
use StreamData;
2120
use StreamEvent;
2221
use SupportedFormat;
@@ -33,22 +32,6 @@ use UnknownTypeOutputBuffer;
3332

3433
pub struct EventLoop {
3534
streams: Mutex<Vec<Option<Reference>>>,
36-
// The `EventLoop` requires a handle to the callbacks in order to be able to emit necessary
37-
// events for `Play`, `Pause` and `Close`.
38-
user_callback: Arc<Mutex<UserCallback>>
39-
}
40-
41-
enum UserCallback {
42-
// When `run` is called with a callback, that callback will be stored here.
43-
//
44-
// It is essential for the safety of the program that this callback is removed before `run`
45-
// returns (not possible with the current CPAL API).
46-
Active(&'static mut (dyn FnMut(StreamId, StreamEvent) + Send)),
47-
// A queue of events that have occurred but that have not yet been emitted to the user as we
48-
// don't yet have a callback to do so.
49-
Inactive {
50-
pending_events: Vec<(StreamId, StreamEvent<'static>)>
51-
},
5235
}
5336

5437
impl EventLoop {
@@ -57,38 +40,13 @@ impl EventLoop {
5740
stdweb::initialize();
5841
EventLoop {
5942
streams: Mutex::new(Vec::new()),
60-
user_callback: Arc::new(Mutex::new(UserCallback::Inactive { pending_events: vec![] })),
6143
}
6244
}
6345

6446
#[inline]
65-
pub fn run<F>(&self, mut callback: F) -> !
47+
pub fn run<F>(&self, callback: F) -> !
6648
where F: FnMut(StreamId, StreamEvent) + Send,
6749
{
68-
// Retrieve and process any pending events.
69-
//
70-
// Then, set the callback ready to be shared between audio processing and the event loop
71-
// handle.
72-
{
73-
let mut guard = self.user_callback.lock().unwrap();
74-
let pending_events = match *guard {
75-
UserCallback::Inactive { ref mut pending_events } => {
76-
mem::replace(pending_events, vec![])
77-
}
78-
UserCallback::Active(_) => {
79-
panic!("`EventLoop::run` was called when the event loop was already running");
80-
}
81-
};
82-
83-
let callback: &mut (dyn FnMut(StreamId, StreamEvent) + Send) = &mut callback;
84-
for (stream_id, event) in pending_events {
85-
callback(stream_id, event);
86-
}
87-
88-
*guard = UserCallback::Active(unsafe { mem::transmute(callback) });
89-
}
90-
91-
9250
// The `run` function uses `set_timeout` to invoke a Rust callback repeatidely. The job
9351
// of this callback is to fill the content of the audio buffers.
9452

@@ -164,9 +122,6 @@ impl EventLoop {
164122
set_timeout(|| callback_fn::<F>(user_data_ptr as *mut _), 10);
165123

166124
stdweb::event_loop();
167-
168-
// It is critical that we remove the callback before returning (currently not possible).
169-
// *self.user_callback.lock().unwrap() = UserCallback::Inactive { pending_events: vec![] };
170125
}
171126

172127
#[inline]
@@ -191,19 +146,9 @@ impl EventLoop {
191146
Ok(StreamId(stream_id))
192147
}
193148

194-
fn emit_or_enqueue_event(&self, id: StreamId, event: StreamEvent<'static>) {
195-
let mut guard = self.user_callback.lock().unwrap();
196-
match *guard {
197-
UserCallback::Active(ref mut callback) => callback(id, event),
198-
UserCallback::Inactive { ref mut pending_events } => pending_events.push((id, event)),
199-
}
200-
}
201-
202149
#[inline]
203150
pub fn destroy_stream(&self, stream_id: StreamId) {
204151
self.streams.lock().unwrap()[stream_id.0] = None;
205-
let event = StreamEvent::Close(StreamCloseCause::UserDestroyed);
206-
self.emit_or_enqueue_event(stream_id, event);
207152
}
208153

209154
#[inline]
@@ -213,7 +158,6 @@ impl EventLoop {
213158
.get(stream_id.0)
214159
.and_then(|v| v.as_ref())
215160
.expect("invalid stream ID");
216-
self.emit_or_enqueue_event(stream_id, StreamEvent::Play);
217161
js!(@{stream}.resume());
218162
Ok(())
219163
}
@@ -226,7 +170,6 @@ impl EventLoop {
226170
.and_then(|v| v.as_ref())
227171
.expect("invalid stream ID");
228172
js!(@{stream}.suspend());
229-
self.emit_or_enqueue_event(stream_id, StreamEvent::Pause);
230173
Ok(())
231174
}
232175
}

src/lib.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,6 @@ pub enum StreamData<'a> {
219219
pub enum StreamEvent<'a> {
220220
/// Some data is ready to be processed.
221221
Data(StreamData<'a>),
222-
/// The stream has received a **Play** command.
223-
Play,
224-
/// The stream has received a **Pause** command.
225-
///
226-
/// No **Data** events should occur until a subsequent **Play** command is received.
227-
Pause,
228222
/// The stream was closed, either because the user destroyed it or because of an error.
229223
///
230224
/// The stream event callback will not be called again after this event occurs.

src/wasapi/stream.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ use Format;
2626
use PauseStreamError;
2727
use PlayStreamError;
2828
use SampleFormat;
29-
use StreamCloseCause;
3029
use StreamData;
3130
use StreamError;
3231
use StreamEvent;
@@ -758,8 +757,6 @@ fn process_commands(
758757
run_context.streams.remove(p);
759758
},
760759
}
761-
let event = StreamEvent::Close(StreamCloseCause::UserDestroyed);
762-
callback(stream_id, event);
763760
},
764761
Command::PlayStream(stream_id) => {
765762
match run_context.streams.iter().position(|s| s.id == stream_id) {
@@ -772,8 +769,6 @@ fn process_commands(
772769
match stream_error_from_hresult(hresult) {
773770
Ok(()) => {
774771
run_context.streams[p].playing = true;
775-
let event = StreamEvent::Play;
776-
callback(stream_id, event);
777772
}
778773
Err(err) => {
779774
let event = StreamEvent::Close(err.into());
@@ -797,8 +792,6 @@ fn process_commands(
797792
match stream_error_from_hresult(hresult) {
798793
Ok(()) => {
799794
run_context.streams[p].playing = false;
800-
let event = StreamEvent::Pause;
801-
callback(stream_id, event);
802795
}
803796
Err(err) => {
804797
let event = StreamEvent::Close(err.into());

0 commit comments

Comments
 (0)