1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
// ABOUTME: Synced audio player with drift correction
// ABOUTME: Uses DAC callback timestamps to drop/insert frames for alignment
use crate::audio::gain::{GainControl, GainRamp};
use crate::audio::sync_correction::{CorrectionPlanner, CorrectionSchedule};
use crate::audio::{AudioBuffer, AudioFormat, Sample};
use crate::error::Error;
use crate::sync::ClockSync;
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
use cpal::{Device, Stream, StreamConfig};
use parking_lot::Mutex;
use std::collections::VecDeque;
use std::sync::Arc;
use std::time::{Duration, Instant};
/// Callback for post-processing audio samples before output.
///
/// Receives `&mut [f32]` (interleaved, after gain is applied).
///
/// The callback is invoked on **every** audio callback, including during
/// pre-start silence when the buffer is all zeros. This allows consumers
/// (e.g. VU meters) to observe the silence rather than missing callbacks.
///
/// # Thread Safety
///
/// This closure runs on the **audio callback thread**. It must:
/// - Not block (no locks, I/O, or sleeping)
/// - Not allocate (no `Vec::push`, `Box::new`, etc.)
/// - Not panic (would abort the audio thread)
///
/// # Why `Box<dyn>`?
///
/// Using dynamic dispatch (`Box<dyn FnMut>`) keeps `SyncedPlayer` a concrete,
/// non-generic type. This simplifies storage, trait object compatibility, and
/// downstream usage at the cost of one vtable indirect call per audio callback
/// (~1 ns vs the ~200 us callback budget).
pub type ProcessCallback = Box<dyn FnMut(&mut [f32]) + Send + 'static>;
struct PlaybackQueue {
queue: VecDeque<AudioBuffer>,
current: Option<AudioBuffer>,
index: usize,
/// Current playback position in **server-time microseconds**. Periodically
/// reanchored to the server's clock during clock-sync correction, so this
/// represents "what server timestamp is playing right now", not how much
/// audio content has been consumed.
cursor_us: i64,
cursor_remainder: i64,
initialized: bool,
generation: u64,
force_reanchor: bool,
}
impl PlaybackQueue {
fn new() -> Self {
Self {
queue: VecDeque::new(),
current: None,
index: 0,
cursor_us: 0,
cursor_remainder: 0,
initialized: false,
generation: 0,
force_reanchor: true,
}
}
fn clear(&mut self) {
self.queue.clear();
self.current = None;
self.index = 0;
self.cursor_us = 0;
self.cursor_remainder = 0;
self.initialized = false;
self.generation = self.generation.wrapping_add(1);
self.force_reanchor = true;
}
fn push(&mut self, buffer: AudioBuffer) {
// Initialize the cursor from the first enqueued buffer so the audio
// callback can see a valid cursor_us before it starts reading. Without
// this, the callback's pre-start gate can't evaluate timestamps and
// outputs silence indefinitely. Use the minimum timestamp seen so far
// since buffers may arrive out of order.
if !self.initialized {
self.cursor_us = buffer.timestamp;
self.cursor_remainder = 0;
self.initialized = true;
}
// When the server rebases its timeline backward (e.g. after event loop
// starvation), new chunks arrive with timestamps that overlap chunks
// already in the queue. Remove all overlapping buffers to prevent
// duplicate audio that causes audible stuttering (~500ms of audio
// played twice). The server will send fresh buffers for any gaps.
//
// Two buffers overlap when their time ranges intersect:
// start_a < end_b && start_b < end_a
// This works for any chunk size (the sendspin spec allows arbitrarily
// small chunks), unlike the previous fixed-threshold approach.
let new_end = buffer.timestamp + buffer.duration_us();
self.queue.retain(|b| {
let existing_end = b.timestamp + b.duration_us();
!(buffer.timestamp < existing_end && b.timestamp < new_end)
});
let pos = self
.queue
.iter()
.position(|b| b.timestamp > buffer.timestamp);
if let Some(pos) = pos {
self.queue.insert(pos, buffer);
} else {
self.queue.push_back(buffer);
}
}
fn next_frame(&mut self, channels: usize, sample_rate: u32) -> Option<&[Sample]> {
let needs_buffer = match self.current {
None => true,
Some(ref c) => self.index + channels > c.samples.len(),
};
if needs_buffer {
// Drop stale buffers that are entirely before the cursor.
if self.initialized {
while let Some(front) = self.queue.front() {
if front.timestamp + front.duration_us() < self.cursor_us {
let _ = self.queue.pop_front();
continue;
}
break;
}
}
// Pop buffers until we find one with remaining samples past the
// cursor, or the queue is empty.
loop {
self.current = self.queue.pop_front();
self.index = 0;
// Skip past samples that are behind the cursor. This handles
// buffers that partially overlap with the current playback
// position, e.g. from backward timestamp jumps during server
// timeline rebases. Without this, playing from the start of
// such a buffer repeats audio the cursor has already passed,
// causing an audible stutter.
if self.initialized {
if let Some(ref current) = self.current {
if current.timestamp < self.cursor_us {
let skip_us = self.cursor_us - current.timestamp;
let skip_frames =
(skip_us.saturating_mul(sample_rate as i64) / 1_000_000) as usize;
if skip_frames > 0 {
self.index = skip_frames
.saturating_mul(channels)
.min(current.samples.len());
}
}
}
}
// If the skip consumed the entire buffer (or left fewer
// samples than one frame), discard it and try the next one.
match self.current {
Some(ref c) if self.index + channels > c.samples.len() => {
self.current = None;
if self.queue.is_empty() {
break;
}
continue;
}
_ => break,
}
}
}
if !self.initialized {
if let Some(current) = self.current.as_ref() {
self.cursor_us = current.timestamp;
self.cursor_remainder = 0;
self.initialized = true;
}
}
// Bail before advancing cursor/index when the queue is empty.
// Without this the cursor races ahead during underruns, causing
// the stale-buffer-dropping logic to discard valid buffers when
// audio resumes.
self.current.as_ref()?;
let start = self.index;
let end = self.index + channels;
self.index = end;
self.advance_cursor(sample_rate);
Some(&self.current.as_ref()?.samples[start..end])
}
fn advance_cursor(&mut self, sample_rate: u32) {
self.cursor_remainder += 1_000_000;
let advance = self.cursor_remainder / sample_rate as i64;
self.cursor_remainder %= sample_rate as i64;
self.cursor_us += advance;
}
}
/// Bundles gain and post-processing parameters for the data callback.
struct CallbackConfig {
gain_control: GainControl,
process_callback: Option<ProcessCallback>,
}
/// Synced audio output with drift correction.
pub struct SyncedPlayer {
format: AudioFormat,
_stream: Stream,
queue: Arc<Mutex<PlaybackQueue>>,
/// Last error from the audio stream callback, if any.
last_error: Arc<Mutex<Option<String>>>,
gain: GainControl,
}
impl SyncedPlayer {
/// Create a new synced player using the provided clock sync and optional device.
///
/// The player starts at `volume` (0-100) and `muted` state. These are
/// applied immediately — the first audio callback uses the correct gain
/// with no ramp from a default value.
pub fn new(
format: AudioFormat,
clock_sync: Arc<Mutex<ClockSync>>,
device: Option<Device>,
volume: u8,
muted: bool,
) -> Result<Self, Error> {
Self::build(format, clock_sync, device, None, volume, muted)
}
/// Create a player with a process callback for post-gain audio processing.
///
/// The callback receives samples **after** gain/mute processing has been
/// applied. See [`ProcessCallback`] for thread-safety requirements.
///
/// # Example (requires physical audio hardware to run)
/// ```no_run
/// # use std::sync::Arc;
/// # use parking_lot::Mutex;
/// # use sendspin::audio::{AudioFormat, Codec, SyncedPlayer};
/// # use sendspin::sync::ClockSync;
/// # use sendspin::DefaultClock;
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let format = AudioFormat {
/// codec: Codec::Pcm,
/// sample_rate: 48_000,
/// channels: 2,
/// bit_depth: 24,
/// codec_header: None,
/// };
/// let clock_sync = Arc::new(Mutex::new(ClockSync::new(Arc::new(DefaultClock::new()))));
/// let player = SyncedPlayer::with_process_callback(
/// format, clock_sync, None,
/// 100, false,
/// Box::new(|data| { /* e.g. feed a VU meter or visualizer */ }),
/// )?;
/// # Ok(())
/// # }
/// ```
pub fn with_process_callback(
format: AudioFormat,
clock_sync: Arc<Mutex<ClockSync>>,
device: Option<Device>,
volume: u8,
muted: bool,
callback: ProcessCallback,
) -> Result<Self, Error> {
Self::build(format, clock_sync, device, Some(callback), volume, muted)
}
fn build(
format: AudioFormat,
clock_sync: Arc<Mutex<ClockSync>>,
device: Option<Device>,
process_callback: Option<ProcessCallback>,
volume: u8,
muted: bool,
) -> Result<Self, Error> {
if format.channels == 0 {
return Err(Error::Output("channels must be > 0".to_string()));
}
let host = cpal::default_host();
let device = match device {
Some(device) => device,
None => host
.default_output_device()
.ok_or_else(|| Error::Output("No output device available".to_string()))?,
};
let config = StreamConfig {
channels: format.channels as u16,
sample_rate: cpal::SampleRate::from(format.sample_rate),
buffer_size: cpal::BufferSize::Default,
};
let queue = Arc::new(Mutex::new(PlaybackQueue::new()));
let queue_clone = Arc::clone(&queue);
let format_clone = format.clone();
let last_error = Arc::new(Mutex::new(None));
let gain = GainControl::new(volume, muted);
let cb_config = CallbackConfig {
gain_control: gain.clone(),
process_callback,
};
let error_clone = Arc::clone(&last_error);
let stream = Self::build_stream(
&device,
&config,
queue_clone,
clock_sync,
format_clone,
cb_config,
error_clone,
)?;
stream.play().map_err(|e| Error::Output(e.to_string()))?;
Ok(Self {
format,
_stream: stream,
queue,
last_error,
gain,
})
}
/// Enqueue a decoded buffer for playback.
///
/// Scheduling uses `buffer.timestamp` (server time in microseconds) for
/// drift-corrected playback. The `play_at` field is ignored.
pub fn enqueue(&self, buffer: AudioBuffer) {
self.queue.lock().push(buffer);
}
/// Clear queued audio and reset playback state.
pub fn clear(&self) {
self.queue.lock().clear();
}
/// Return the configured audio format.
pub fn format(&self) -> &AudioFormat {
&self.format
}
/// Check if the audio stream has encountered an error.
///
/// Returns the error message if one occurred, clearing it in the process.
pub fn take_error(&self) -> Option<String> {
self.last_error.lock().take()
}
/// Check if the audio stream has an error without clearing it.
pub fn has_error(&self) -> bool {
self.last_error.lock().is_some()
}
/// Get a reference to the volume/mute control.
///
/// Call `.clone()` if you need an owned handle to share across threads
/// (cloning is cheap — single `Arc` increment, no data copy).
pub fn gain_control(&self) -> &GainControl {
&self.gain
}
// -- Volume/mute convenience methods --
//
// These promote the most common operations for ergonomics in simple
// use-cases. For full control, use `gain_control()` directly.
/// Current volume as 0-100.
pub fn volume(&self) -> u8 {
self.gain.volume()
}
/// Whether playback is currently muted.
pub fn is_muted(&self) -> bool {
self.gain.is_muted()
}
/// Set playback volume (0-100).
pub fn set_volume(&self, volume: u8) {
self.gain.set_volume(volume);
}
/// Set mute state.
pub fn set_mute(&self, muted: bool) {
self.gain.set_mute(muted);
}
fn build_stream(
device: &Device,
config: &StreamConfig,
queue: Arc<Mutex<PlaybackQueue>>,
clock_sync: Arc<Mutex<ClockSync>>,
format: AudioFormat,
mut cb_config: CallbackConfig,
error_sink: Arc<Mutex<Option<String>>>,
) -> Result<Stream, Error> {
let channels = format.channels as usize;
let sample_rate = format.sample_rate;
let planner = CorrectionPlanner::new();
let mut last_frame = vec![Sample::ZERO; channels];
let mut schedule = CorrectionSchedule::default();
let mut insert_counter = 0u32;
let mut drop_counter = 0u32;
let mut started = false;
let mut last_generation = 0u64;
let initial_gain = cb_config.gain_control.gain();
let mut gain_ramp = GainRamp::new(sample_rate, initial_gain);
let stream = device
.build_output_stream(
config,
move |data: &mut [f32], info: &cpal::OutputCallbackInfo| {
// Read all queue state in a single lock to avoid
// a TOCTOU window between generation and cursor reads.
// After clear(), force_reanchor is true but cursor_us
// is None (initialized=false), so the reanchor block
// is normally skipped and the flag persists. If clear()
// races with an in-flight reanchor, the flag can be
// cleared early; next_frame() re-anchors from the
// first buffer's timestamp in that case.
let (generation, cursor_us, force_reanchor) = {
let queue = queue.lock();
let cursor = if queue.initialized {
Some(queue.cursor_us)
} else {
None
};
(queue.generation, cursor, queue.force_reanchor)
};
if generation != last_generation {
last_generation = generation;
started = false;
schedule = CorrectionSchedule::default();
insert_counter = 0;
drop_counter = 0;
for sample in last_frame.iter_mut() {
*sample = Sample::ZERO;
}
}
let callback_instant = Instant::now();
let ts = info.timestamp();
let playback_delta = ts
.playback
.duration_since(&ts.callback)
.unwrap_or(Duration::ZERO);
let playback_instant = callback_instant + playback_delta;
// try_lock: skip sync if contended rather than blocking
// the audio thread. force_reanchor is sticky in the
// queue, so it will be retried on the next callback.
if let (Some(cursor_us), Some(sync)) = (cursor_us, clock_sync.try_lock()) {
if let Some(expected_instant) = sync.server_to_local_instant(cursor_us) {
let early_window = Duration::from_millis(1);
if !started && playback_instant + early_window < expected_instant {
for sample in data.iter_mut() {
*sample = 0.0;
}
let target = cb_config.gain_control.gain();
let frames = data.len() / channels;
gain_ramp.advance(frames, target);
if let Some(ref mut cb) = cb_config.process_callback {
cb(data);
}
return;
}
started = true;
let error_us = if playback_instant >= expected_instant {
playback_instant
.duration_since(expected_instant)
.as_micros() as i64
} else {
-(expected_instant
.duration_since(playback_instant)
.as_micros() as i64)
};
let new_schedule =
planner.plan(error_us, sample_rate, schedule.is_correcting());
if new_schedule != schedule {
if new_schedule.is_correcting() != schedule.is_correcting() {
if new_schedule.is_correcting() {
log::debug!(
"Sync correction engaged: \
error={:.1}ms, insert_every={}, drop_every={}",
error_us as f64 / 1000.0,
new_schedule.insert_every_n_frames,
new_schedule.drop_every_n_frames,
);
} else {
log::debug!(
"Sync correction disengaged: \
error={:.1}ms",
error_us as f64 / 1000.0,
);
}
}
schedule = new_schedule;
insert_counter = schedule.insert_every_n_frames;
drop_counter = schedule.drop_every_n_frames;
}
if schedule.reanchor || force_reanchor {
let client_micros = sync.instant_to_client_micros(playback_instant);
if let Some(server_time) =
sync.client_to_server_micros(client_micros)
{
let mut queue = queue.lock();
queue.cursor_us = server_time;
queue.cursor_remainder = 0;
queue.force_reanchor = false;
}
schedule = CorrectionSchedule::default();
insert_counter = 0;
drop_counter = 0;
}
}
}
// If playback hasn't started yet (clock sync not converged,
// lock contention, or pre-start gate active), output silence.
// Audio data stays in the ring buffer for when sync converges
// and reanchor positions the cursor correctly.
if !started {
for sample in data.iter_mut() {
*sample = 0.0;
}
let target = cb_config.gain_control.gain();
let frames = data.len() / channels;
gain_ramp.advance(frames, target);
if let Some(ref mut cb) = cb_config.process_callback {
cb(data);
}
return;
}
{
let mut queue = queue.lock();
let frames = data.len() / channels;
let mut out_index = 0;
for _ in 0..frames {
if schedule.drop_every_n_frames > 0 {
drop_counter = drop_counter.saturating_sub(1);
if drop_counter == 0 {
// Discard one frame to catch up
let _ = queue.next_frame(channels, sample_rate);
drop_counter = schedule.drop_every_n_frames;
// Get and output the next frame (don't repeat last_frame)
if let Some(frame) = queue.next_frame(channels, sample_rate) {
last_frame.copy_from_slice(frame);
for sample in frame {
data[out_index] = sample.to_f32();
out_index += 1;
}
} else {
for sample in &last_frame {
data[out_index] = sample.to_f32();
out_index += 1;
}
}
continue;
}
}
if schedule.insert_every_n_frames > 0 {
insert_counter = insert_counter.saturating_sub(1);
if insert_counter == 0 {
insert_counter = schedule.insert_every_n_frames;
for sample in &last_frame {
data[out_index] = sample.to_f32();
out_index += 1;
}
continue;
}
}
if let Some(frame) = queue.next_frame(channels, sample_rate) {
last_frame.copy_from_slice(frame);
for sample in frame {
data[out_index] = sample.to_f32();
out_index += 1;
}
} else {
for _ in 0..channels {
data[out_index] = 0.0;
out_index += 1;
}
}
}
} // queue lock dropped before user callback
// Apply gain with per-frame ramping
let target = cb_config.gain_control.gain();
gain_ramp.apply(data, channels, target);
if let Some(ref mut cb) = cb_config.process_callback {
cb(data);
}
},
move |err| {
eprintln!("Audio stream error: {}", err);
*error_sink.lock() = Some(err.to_string());
},
None,
)
.map_err(|e| Error::Output(e.to_string()))?;
Ok(stream)
}
}
#[cfg(test)]
mod tests {
// Note: SyncedPlayer's convenience methods (volume, is_muted, set_volume,
// set_mute, gain_control) delegate to GainControl which is thoroughly tested
// in gain.rs. Wiring tests require a real audio device (cpal Stream) and
// cannot run in CI.
use super::PlaybackQueue;
use crate::audio::{AudioBuffer, AudioFormat, Codec, Sample};
use std::sync::Arc;
use std::time::Instant;
/// Standard test format: 48kHz stereo 24-bit PCM.
fn test_format() -> AudioFormat {
AudioFormat {
codec: Codec::Pcm,
sample_rate: 48_000,
channels: 2,
bit_depth: 24,
codec_header: None,
}
}
/// Mono variant of [`test_format`].
fn test_format_mono() -> AudioFormat {
AudioFormat {
channels: 1,
..test_format()
}
}
#[test]
fn test_queue_clear_bumps_generation() {
let mut queue = PlaybackQueue::new();
let format = test_format();
let samples = vec![Sample::ZERO; 96];
queue.push(AudioBuffer {
timestamp: 1234,
play_at: Instant::now(),
samples: Arc::from(samples.into_boxed_slice()),
format,
});
let before = queue.generation;
queue.clear();
assert_ne!(queue.generation, before);
assert!(queue.queue.is_empty());
assert!(!queue.initialized);
}
#[test]
fn test_queue_drops_stale_buffers() {
let mut queue = PlaybackQueue::new();
let format = test_format();
// Use distinct sample values so we can verify which buffer was returned.
// 4800 stereo frames at 48kHz = 100ms per buffer.
// With cursor at 150ms, the first buffer (ts=0, ends at 100ms) is stale.
let stale_samples: Vec<Sample> = (0..4800 * 2).map(|_| Sample(111)).collect();
let fresh_samples: Vec<Sample> = (0..4800 * 2).map(|_| Sample(222)).collect();
queue.push(AudioBuffer {
timestamp: 0,
play_at: Instant::now(),
samples: Arc::from(stale_samples.into_boxed_slice()),
format: format.clone(),
});
queue.push(AudioBuffer {
timestamp: 200_000,
play_at: Instant::now(),
samples: Arc::from(fresh_samples.into_boxed_slice()),
format,
});
queue.cursor_us = 150_000;
queue.initialized = true;
// Copy the frame data so we can release the mutable borrow on queue.
let frame_data: Vec<Sample> = queue
.next_frame(2, 48_000)
.expect("expected a frame")
.to_vec();
assert_eq!(queue.current.as_ref().unwrap().timestamp, 200_000);
// Verify we got the fresh buffer's data, not the stale one
assert_eq!(frame_data[0], Sample(222));
assert_eq!(frame_data[1], Sample(222));
}
#[test]
fn test_queue_push_sorts_by_timestamp() {
let mut queue = PlaybackQueue::new();
let format = test_format_mono();
// Push out of order: 300, 100, 200
for ts in [300_000i64, 100_000, 200_000] {
let samples = vec![Sample(ts as i32); 48]; // 1ms of mono
queue.push(AudioBuffer {
timestamp: ts,
play_at: Instant::now(),
samples: Arc::from(samples.into_boxed_slice()),
format: format.clone(),
});
}
// Reset cursor so stale-buffer-dropping doesn't interfere with
// the sort-order verification (in real usage, sync reanchor sets
// cursor before playback begins).
queue.cursor_us = 0;
// Drain and verify sorted order
let _ = queue.next_frame(1, 48_000);
assert_eq!(queue.current.as_ref().unwrap().timestamp, 100_000);
// Exhaust the first buffer (48 frames)
for _ in 1..48 {
queue.next_frame(1, 48_000);
}
// Next frame should come from the second buffer
let _ = queue.next_frame(1, 48_000);
assert_eq!(queue.current.as_ref().unwrap().timestamp, 200_000);
// Exhaust the second buffer
for _ in 1..48 {
queue.next_frame(1, 48_000);
}
// Next frame should come from the third buffer
let _ = queue.next_frame(1, 48_000);
assert_eq!(queue.current.as_ref().unwrap().timestamp, 300_000);
}
#[test]
fn test_queue_cursor_advances_correctly() {
let mut queue = PlaybackQueue::new();
let format = test_format();
// 480 stereo frames = 10ms at 48kHz
let num_frames = 480;
let samples = vec![Sample::ZERO; num_frames * 2];
let start_ts = 1_000_000i64; // 1 second
queue.push(AudioBuffer {
timestamp: start_ts,
play_at: Instant::now(),
samples: Arc::from(samples.into_boxed_slice()),
format,
});
// Consume all frames
for _ in 0..num_frames {
let _ = queue.next_frame(2, 48_000);
}
// 480 frames at 48kHz = 10,000us = 10ms
let expected_end = start_ts + 10_000;
assert_eq!(
queue.cursor_us,
expected_end,
"cursor should advance by exactly 10ms (10000us), got delta={}",
queue.cursor_us - start_ts
);
}
#[test]
fn test_cursor_does_not_advance_during_underrun() {
let mut queue = PlaybackQueue::new();
let format = test_format();
// Push one buffer to initialize the cursor
let samples = vec![Sample::ZERO; 480 * 2]; // 10ms stereo
let start_ts = 1_000_000i64;
queue.push(AudioBuffer {
timestamp: start_ts,
play_at: Instant::now(),
samples: Arc::from(samples.into_boxed_slice()),
format: format.clone(),
});
// Consume all frames
for _ in 0..480 {
assert!(queue.next_frame(2, 48_000).is_some());
}
let cursor_after_drain = queue.cursor_us;
// Queue is now empty. Calling next_frame should return None
// and NOT advance the cursor.
for _ in 0..1000 {
assert!(queue.next_frame(2, 48_000).is_none());
}
assert_eq!(
queue.cursor_us,
cursor_after_drain,
"cursor must not advance during underrun; advanced by {}us",
queue.cursor_us - cursor_after_drain
);
// Push a new buffer after the underrun. It should NOT be
// dropped as stale — the cursor hasn't raced ahead.
let fresh_samples: Vec<Sample> = (0..480 * 2).map(|_| Sample(999)).collect();
queue.push(AudioBuffer {
timestamp: cursor_after_drain, // starts right where we left off
play_at: Instant::now(),
samples: Arc::from(fresh_samples.into_boxed_slice()),
format,
});
let frame = queue
.next_frame(2, 48_000)
.expect("buffer should not be dropped as stale");
assert_eq!(
frame[0],
Sample(999),
"should get the fresh buffer, not stale data"
);
}
#[test]
fn test_push_initializes_cursor_from_first_buffer() {
let mut queue = PlaybackQueue::new();
let format = test_format();
assert!(!queue.initialized);
assert_eq!(queue.cursor_us, 0);
let samples = vec![Sample::ZERO; 96];
queue.push(AudioBuffer {
timestamp: 500_000,
play_at: Instant::now(),
samples: Arc::from(samples.into_boxed_slice()),
format,
});
assert!(queue.initialized);
assert_eq!(queue.cursor_us, 500_000);
assert_eq!(queue.cursor_remainder, 0);
}
#[test]
fn test_push_does_not_regress_cursor_after_init() {
let mut queue = PlaybackQueue::new();
let format = test_format();
let samples = vec![Sample::ZERO; 96];
// First buffer at 500ms — initializes cursor
queue.push(AudioBuffer {
timestamp: 500_000,
play_at: Instant::now(),
samples: Arc::from(samples.clone().into_boxed_slice()),
format: format.clone(),
});
assert_eq!(queue.cursor_us, 500_000);
// Consume a frame so cursor advances past init
let _ = queue.next_frame(2, 48_000);
let cursor_after_consume = queue.cursor_us;
assert!(cursor_after_consume > 500_000);
// Push an earlier buffer — cursor must NOT regress
queue.push(AudioBuffer {
timestamp: 200_000,
play_at: Instant::now(),
samples: Arc::from(samples.into_boxed_slice()),
format,
});
assert_eq!(
queue.cursor_us, cursor_after_consume,
"cursor must not regress after playback has started"
);
}
#[test]
fn test_next_frame_skips_into_overlapping_buffer() {
// Simulates a backward timestamp jump from a server timeline rebase.
// Buffer A is 50ms (2400 frames stereo at 48kHz). After consuming A
// the cursor is at 50ms. Buffer B arrives at 25ms — the skip logic
// should jump 25ms (1200 frames) into B.
//
// B is pushed AFTER A is consumed so dedup doesn't apply (A is no
// longer in the queue).
let mut queue = PlaybackQueue::new();
let format = test_format();
let buf_a: Vec<Sample> = (0..2400 * 2).map(|_| Sample(111)).collect();
let buf_b: Vec<Sample> = (0..2400 * 2)
.map(|i| {
// First half (1200 frames) = 222, second half = 333
if i < 2400 {
Sample(222)
} else {
Sample(333)
}
})
.collect();
queue.push(AudioBuffer {
timestamp: 0,
play_at: Instant::now(),
samples: Arc::from(buf_a.into_boxed_slice()),
format: format.clone(),
});
// Consume all of buffer A (2400 frames). Cursor advances to 50000µs.
for _ in 0..2400 {
assert!(queue.next_frame(2, 48_000).is_some());
}
assert_eq!(queue.cursor_us, 50_000);
// Push B after A is consumed — no dedup, tests skip logic only.
// push() no longer regresses cursor_us after init, so cursor stays
// at 50000 and the skip logic activates naturally.
queue.push(AudioBuffer {
timestamp: 25_000,
play_at: Instant::now(),
samples: Arc::from(buf_b.into_boxed_slice()),
format,
});
// Buffer B starts at 25ms but cursor is at 50ms, so 25ms (1200 frames)
// should be skipped. First returned frame should be Sample(333).
let frame = queue
.next_frame(2, 48_000)
.expect("should get a frame from buffer B");
assert_eq!(
frame[0],
Sample(333),
"expected skip into second half of buffer B (past the overlap), \
got first half — backward-timestamped audio was replayed"
);
}
#[test]
fn test_next_frame_no_skip_when_buffer_starts_at_or_after_cursor() {
// Verify that the skip logic doesn't activate for normal (non-overlapping)
// buffers — only for buffers that start before the cursor.
let mut queue = PlaybackQueue::new();
let format = test_format();
// 2400 frames = 50ms per buffer. Adjacent, non-overlapping.
let samples_a: Vec<Sample> = (0..2400 * 2).map(|_| Sample(111)).collect();
let samples_b: Vec<Sample> = (0..2400 * 2).map(|_| Sample(222)).collect();
// Two consecutive, non-overlapping buffers.
queue.push(AudioBuffer {
timestamp: 0,
play_at: Instant::now(),
samples: Arc::from(samples_a.into_boxed_slice()),
format: format.clone(),
});
queue.push(AudioBuffer {
timestamp: 50_000, // starts exactly where A ends
play_at: Instant::now(),
samples: Arc::from(samples_b.into_boxed_slice()),
format,
});
// Consume all of buffer A.
for _ in 0..2400 {
assert!(queue.next_frame(2, 48_000).is_some());
}
// Buffer B starts at cursor (50000µs) — no skip should occur.
let frame = queue
.next_frame(2, 48_000)
.expect("should get first frame of buffer B");
assert_eq!(
frame[0],
Sample(222),
"buffer B should play from the start (no skip needed)"
);
}
#[test]
fn test_push_dedup_replaces_overlapping_buffer() {
let mut queue = PlaybackQueue::new();
let format = test_format();
// Buffer A: 10ms at ts=0 (480 stereo frames)
let samples_a: Vec<Sample> = (0..480 * 2).map(|_| Sample(111)).collect();
queue.push(AudioBuffer {
timestamp: 0,
play_at: Instant::now(),
samples: Arc::from(samples_a.into_boxed_slice()),
format: format.clone(),
});
assert_eq!(queue.queue.len(), 1);
// Buffer B: 10ms at ts=5000 (5ms) — overlaps A's range [0, 10000)
let samples_b: Vec<Sample> = (0..480 * 2).map(|_| Sample(222)).collect();
queue.push(AudioBuffer {
timestamp: 5_000,
play_at: Instant::now(),
samples: Arc::from(samples_b.into_boxed_slice()),
format,
});
// Should replace A, not add a second entry
assert_eq!(queue.queue.len(), 1);
assert_eq!(queue.queue[0].samples[0], Sample(222));
}
#[test]
fn test_push_dedup_no_false_positive_small_chunks() {
let mut queue = PlaybackQueue::new();
let format = test_format();
// Two adjacent 5ms chunks (240 stereo frames each).
// Chunk A: [0, 5000), Chunk B: [5000, 10000) — no overlap.
let samples_a: Vec<Sample> = (0..240 * 2).map(|_| Sample(111)).collect();
let samples_b: Vec<Sample> = (0..240 * 2).map(|_| Sample(222)).collect();
queue.push(AudioBuffer {
timestamp: 0,
play_at: Instant::now(),
samples: Arc::from(samples_a.into_boxed_slice()),
format: format.clone(),
});
queue.push(AudioBuffer {
timestamp: 5_000,
play_at: Instant::now(),
samples: Arc::from(samples_b.into_boxed_slice()),
format,
});
// Both should be kept — they're adjacent, not overlapping
assert_eq!(queue.queue.len(), 2);
assert_eq!(queue.queue[0].timestamp, 0);
assert_eq!(queue.queue[1].timestamp, 5_000);
}
#[test]
fn test_push_dedup_removes_all_overlapping() {
let mut queue = PlaybackQueue::new();
let format = test_format();
// Buffer A: 10ms at ts=0 — range [0, 10000)
let samples_a: Vec<Sample> = (0..480 * 2).map(|_| Sample(111)).collect();
// Buffer B: 10ms at ts=12000 — range [12000, 22000). No overlap with A.
let samples_b: Vec<Sample> = (0..480 * 2).map(|_| Sample(222)).collect();
queue.push(AudioBuffer {
timestamp: 0,
play_at: Instant::now(),
samples: Arc::from(samples_a.into_boxed_slice()),
format: format.clone(),
});
queue.push(AudioBuffer {
timestamp: 12_000,
play_at: Instant::now(),
samples: Arc::from(samples_b.into_boxed_slice()),
format: format.clone(),
});
assert_eq!(queue.queue.len(), 2);
// Buffer C: 20ms at ts=9000 — range [9000, 29000).
// Overlaps both A (9000 < 10000 && 0 < 29000) and B (9000 < 22000 && 12000 < 29000).
// Both stale buffers should be removed — the server will send fresh
// data for any gaps. Keeping either would cause duplicate audio.
let samples_c: Vec<Sample> = (0..960 * 2).map(|_| Sample(333)).collect();
queue.push(AudioBuffer {
timestamp: 9_000,
play_at: Instant::now(),
samples: Arc::from(samples_c.into_boxed_slice()),
format,
});
assert_eq!(queue.queue.len(), 1);
assert_eq!(queue.queue[0].timestamp, 9_000);
assert_eq!(queue.queue[0].samples[0], Sample(333));
}
#[test]
fn test_skip_past_entire_buffer_does_not_panic() {
// When the cursor is far ahead of a buffer, the skip logic can set
// self.index past the buffer's sample count. next_frame must not
// panic; it should discard the buffer and return the next one.
let mut queue = PlaybackQueue::new();
let format = test_format();
// 1ms buffer (48 stereo frames) at ts=49000. Duration = 1000µs,
// so it ends at 50000 which is NOT < cursor (50000), surviving
// the stale-drop. But the skip logic sees ts=49000 < cursor=50000
// and tries to skip 1ms (48 frames) — exactly the buffer length.
let short_samples: Vec<Sample> = (0..48 * 2).map(|_| Sample(111)).collect();
// Buffer that starts at cursor: 10ms at ts=50000
let ahead_samples: Vec<Sample> = (0..480 * 2).map(|_| Sample(222)).collect();
queue.initialized = true;
queue.cursor_us = 50_000;
queue.queue.push_back(AudioBuffer {
timestamp: 49_000,
play_at: Instant::now(),
samples: Arc::from(short_samples.into_boxed_slice()),
format: format.clone(),
});
queue.queue.push_back(AudioBuffer {
timestamp: 50_000,
play_at: Instant::now(),
samples: Arc::from(ahead_samples.into_boxed_slice()),
format,
});
// The skip tries to skip 1ms (48 frames) into a 48-frame buffer —
// index lands at the end. Must not panic; should discard the short
// buffer and return from the next one (ts=50000).
let frame = queue
.next_frame(2, 48_000)
.expect("should return a frame from the next buffer, not panic");
assert_eq!(
frame[0],
Sample(222),
"expected frame from the ahead buffer"
);
}
}