again with timeout
This commit is contained in:
parent
085445b35d
commit
08e69a3062
|
@ -1,6 +1,7 @@
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
use sdl2::Sdl;
|
use sdl2::Sdl;
|
||||||
use sdl2::audio::{AudioCallback, AudioDevice, AudioSpecDesired};
|
use sdl2::audio::{AudioCallback, AudioDevice, AudioSpecDesired};
|
||||||
|
@ -16,6 +17,7 @@ pub struct SimpleSdl2AudioThreadComponent {
|
||||||
struct MySdlAudio {
|
struct MySdlAudio {
|
||||||
must_resample: bool,
|
must_resample: bool,
|
||||||
reader: Consumer<i16>,
|
reader: Consumer<i16>,
|
||||||
|
timing: SystemTiming,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AudioCallback for MySdlAudio {
|
impl AudioCallback for MySdlAudio {
|
||||||
|
@ -27,8 +29,17 @@ impl AudioCallback for MySdlAudio {
|
||||||
}
|
}
|
||||||
let buf_len = data.len();
|
let buf_len = data.len();
|
||||||
let mut remaining = buf_len;
|
let mut remaining = buf_len;
|
||||||
|
let mut dead_frames = 0;
|
||||||
while remaining > 0 {
|
while remaining > 0 {
|
||||||
remaining -= self.reader.pop_slice(&mut data[buf_len - remaining..]);
|
let read = self.reader.pop_slice(&mut data[buf_len - remaining..]);
|
||||||
|
remaining -= read;
|
||||||
|
if read == 0 {
|
||||||
|
std::thread::sleep(Duration::from_secs_f64(1.0 / self.timing.fps));
|
||||||
|
dead_frames += 1;
|
||||||
|
if dead_frames > self.timing.fps as usize / 2 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -88,6 +99,7 @@ impl SimpleSdl2AudioThreadComponent {
|
||||||
MySdlAudio {
|
MySdlAudio {
|
||||||
must_resample, // todo: include freqs from & to
|
must_resample, // todo: include freqs from & to
|
||||||
reader,
|
reader,
|
||||||
|
timing: timing.clone(),
|
||||||
}
|
}
|
||||||
})?;
|
})?;
|
||||||
device.resume();
|
device.resume();
|
||||||
|
|
Loading…
Reference in New Issue