Skip to content

Commit 023ee21

Browse files
authored
Fix Sink skip apis (#494)
* Call `Sink::clear`/`Sink::skip_one` on an empty `Sink` will permanently skip any `Source` append to it * Call `Sink::clear` on an non-empty `Sink` can't reset it's length().
1 parent 65d4dd3 commit 023ee21

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

src/sink.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ struct Controls {
2626
volume: Mutex<f32>,
2727
stopped: AtomicBool,
2828
speed: Mutex<f32>,
29-
do_skip: AtomicBool,
3029
to_clear: Mutex<u32>,
3130
}
3231

@@ -52,7 +51,6 @@ impl Sink {
5251
volume: Mutex::new(1.0),
5352
stopped: AtomicBool::new(false),
5453
speed: Mutex::new(1.0),
55-
do_skip: AtomicBool::new(false),
5654
to_clear: Mutex::new(0),
5755
}),
5856
sound_count: Arc::new(AtomicUsize::new(0)),
@@ -79,6 +77,8 @@ impl Sink {
7977

8078
let controls = self.controls.clone();
8179

80+
let start_played = AtomicBool::new(false);
81+
8282
let source = source
8383
.speed(1.0)
8484
.pausable(false)
@@ -89,13 +89,10 @@ impl Sink {
8989
if controls.stopped.load(Ordering::SeqCst) {
9090
src.stop();
9191
}
92-
if controls.do_skip.load(Ordering::SeqCst) {
93-
let _ = src.inner_mut().skip();
92+
{
9493
let mut to_clear = controls.to_clear.lock().unwrap();
95-
if *to_clear == 1 {
96-
controls.do_skip.store(false, Ordering::SeqCst);
97-
*to_clear = 0;
98-
} else if *to_clear > 0 {
94+
if *to_clear > 0 {
95+
let _ = src.inner_mut().skip();
9996
*to_clear -= 1;
10097
}
10198
}
@@ -106,6 +103,7 @@ impl Sink {
106103
amp.inner_mut()
107104
.inner_mut()
108105
.set_factor(*controls.speed.lock().unwrap());
106+
start_played.store(true, Ordering::SeqCst);
109107
})
110108
.convert_samples();
111109
self.sound_count.fetch_add(1, Ordering::Relaxed);
@@ -180,7 +178,7 @@ impl Sink {
180178
pub fn clear(&self) {
181179
let len = self.sound_count.load(Ordering::SeqCst) as u32;
182180
*self.controls.to_clear.lock().unwrap() = len;
183-
self.skip_one();
181+
self.sleep_until_end();
184182
self.pause();
185183
}
186184

@@ -190,7 +188,11 @@ impl Sink {
190188
/// it will play the next one. Otherwise, the `Sink` will finish as if
191189
/// it had finished playing a `Source` all the way through.
192190
pub fn skip_one(&self) {
193-
self.controls.do_skip.store(true, Ordering::SeqCst);
191+
let len = self.sound_count.load(Ordering::SeqCst) as u32;
192+
let mut to_clear = self.controls.to_clear.lock().unwrap();
193+
if len > *to_clear {
194+
*to_clear += 1;
195+
}
194196
}
195197

196198
/// Stops the sink by emptying the queue.

0 commit comments

Comments
 (0)