Skip to content

Commit 6a8f3af

Browse files
committed
refactor: simplify catchup logic
1 parent 18ff2a9 commit 6a8f3af

3 files changed

Lines changed: 16 additions & 69 deletions

File tree

‎player/src/audio.rs‎

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,29 @@ pub struct Player {
2020
decoder: Decoder,
2121
buffer: Arc<Mutex<Ringbuf<f32>>>,
2222
stream: Option<Stream>,
23-
frames_received: usize,
2423
volume: Arc<Mutex<f32>>,
24+
last_frame: Option<u32>,
2525
}
2626
impl Player {
2727
pub fn new() -> Self {
2828
Self {
2929
decoder: Decoder::new(48000, opus::Channels::Stereo).unwrap(),
3030
buffer: Arc::new(Mutex::new(Ringbuf::<f32>::new(PLAYER_BUFFER_SIZE))),
3131
stream: None,
32-
frames_received: 0,
32+
last_frame: None,
3333
volume: Arc::new(Mutex::new(1.0)),
3434
}
3535
}
3636

3737
// decode opus data when received and buffer the samples
38-
pub fn receive(&mut self, frame: Vec<u8>) {
38+
pub fn receive(&mut self, frame_data: Vec<u8>, frame_idx: u32) {
39+
self.last_frame = Some(frame_idx);
40+
3941
// allocate and decode into here
4042
let mut pcm = [0.0; 960];
41-
self.decoder.decode_float(&frame, &mut pcm, false).unwrap();
42-
self.frames_received += 1;
43+
self.decoder
44+
.decode_float(&frame_data, &mut pcm, false)
45+
.unwrap();
4346

4447
let mut buffer = self.buffer.lock().unwrap();
4548
if buffer.len() < buffer.free_len() {
@@ -106,7 +109,7 @@ impl Player {
106109

107110
pub fn clear(&mut self) {
108111
self.buffer.lock().unwrap().clear();
109-
self.frames_received = 0;
112+
self.last_frame = None;
110113
self.pause();
111114
}
112115

@@ -132,17 +135,14 @@ impl Player {
132135
let buffer = self.buffer.lock().unwrap();
133136
buffer.len() > 48000 * 2 * SECONDS // 1s of stereo 48k
134137
}
135-
pub fn get_seconds_elapsed(&self) -> usize {
136-
const SECONDS: usize = 1;
138+
pub fn get_seconds_elapsed(&self) -> u32 {
139+
const SECONDS: u32 = 1;
137140

138141
// 960 = 2 channels * 480 samples per frame
139-
let samples_received = self.frames_received * 480;
142+
let samples_received = self.last_frame.unwrap_or_default() * 480;
140143
let current_sample = samples_received.saturating_sub(48000 * SECONDS);
141144
current_sample.max(0) / 48000
142145
}
143-
pub fn fake_frames_received(&mut self, frames: usize) {
144-
self.frames_received = frames;
145-
}
146146
pub fn get_visualizer_buffer(&mut self) -> Option<[f32; 4096]> {
147147
let buf = self.buffer.lock().unwrap();
148148

@@ -209,7 +209,6 @@ fn write_audio<T: Sample>(
209209
#[derive(Debug, Clone)]
210210
pub enum AudioCommand {
211211
AudioData(AudioData),
212-
StartLate(usize),
213212
Clear,
214213
Volume(f32),
215214
Shutdown,
@@ -278,7 +277,7 @@ impl AudioThread {
278277
let _ = self.tx.send(AudioStatus::Buffer(self.p.buffer_status()));
279278
}
280279

281-
self.p.receive(frame.data);
280+
self.p.receive(frame.data, frame.frame);
282281

283282
if let Some(samples) = self.p.get_visualizer_buffer() {
284283
let bars = calculate_visualizer(&samples);
@@ -287,7 +286,6 @@ impl AudioThread {
287286
}
288287
AudioData::Start => {
289288
self.wants_play = true;
290-
self.p.fake_frames_received(0);
291289

292290
// if we don't have enough buffer yet, wait
293291
// otherwise we had extra so we can just keep playing i guess?
@@ -302,18 +300,14 @@ impl AudioThread {
302300
}
303301
}
304302
AudioData::Finish => {
303+
// TODO: we probably shouldnt do this
305304
while !self.p.finish() {
306305
std::thread::sleep(std::time::Duration::from_millis(20));
307306
}
308307
self.p.pause();
309308
let _ = self.tx.send(AudioStatus::Finished);
310309
}
311310
},
312-
AudioCommand::StartLate(frame_id) => {
313-
self.wants_play = true;
314-
self.p.fake_frames_received(frame_id);
315-
}
316-
317311
AudioCommand::Clear => {
318312
let _ = self.tx.send(AudioStatus::Elapsed(0));
319313
self.p.clear();

‎player/src/connection.rs‎

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -356,48 +356,7 @@ impl ConnectionActor {
356356
},
357357

358358
Message::AudioData(data) => {
359-
match data {
360-
protocol::AudioData::Frame(f) => {
361-
let state = self.state.read().await;
362-
let room = state.connection.as_ref().unwrap().room.as_ref().unwrap();
363-
364-
if !room.is_synced {
365-
// lol
366-
drop(state);
367-
368-
// we're late.... try and catch up will you
369-
let mut state_write = self.state.write().await;
370-
let room_write = state_write
371-
.connection
372-
.as_mut()
373-
.unwrap()
374-
.room
375-
.as_mut()
376-
.unwrap();
377-
room_write.is_synced = true;
378-
379-
self.audio
380-
.send(AudioCommand::StartLate(f.frame as usize))
381-
.unwrap();
382-
}
383-
384-
self.audio
385-
.send(AudioCommand::AudioData(AudioData::Frame(f)))
386-
.unwrap();
387-
}
388-
protocol::AudioData::Start => {
389-
{
390-
let mut state = self.state.write().await;
391-
let room = state.connection.as_mut().unwrap().room.as_mut().unwrap();
392-
room.is_synced = true;
393-
}
394-
self.audio.send(AudioCommand::AudioData(data)).unwrap();
395-
}
396-
_ => {
397-
// forward to audio thread
398-
self.audio.send(AudioCommand::AudioData(data)).unwrap();
399-
}
400-
}
359+
self.audio.send(AudioCommand::AudioData(data)).unwrap();
401360
}
402361

403362
// should not be receiving these
@@ -512,7 +471,6 @@ impl ConnectionActor {
512471
current_track: 0,
513472
playback_state: PlaybackState::Stopped,
514473
connected_users: HashMap::new(),
515-
is_synced: false,
516474
buffering: false,
517475
transmit_thread: None,
518476
})

‎player/src/main.rs‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,6 @@ pub struct RoomState {
7777
id: u32,
7878
name: String,
7979

80-
// we only need to use this once, when we first connect
81-
// if we see the start message -> we are in sync
82-
// if we see a frame without start message -> we need to catch up first
83-
is_synced: bool,
84-
8580
buffering: bool,
8681

8782
queue: VecDeque<Track>,
@@ -393,7 +388,7 @@ impl MainThread {
393388

394389
#[derive(Debug, Clone)]
395390
pub enum AudioStatus {
396-
Elapsed(usize),
391+
Elapsed(u32),
397392
Buffering(bool),
398393
Finished,
399394
Visualizer([u8; 14]),

0 commit comments

Comments
 (0)