1 use std::time::Duration;
2 use std::thread::JoinHandle;
3 use std::sync::{Arc, Mutex};
4 use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
5 use std::sync::mpsc::{Receiver, SyncSender, TrySendError};
8 use sdl2::AudioSubsystem;
9 use sdl2::audio::{AudioDevice, AudioCallback, AudioSpecDesired};
11 use nihav_core::formats::*;
12 use nihav_core::codecs::*;
13 use nihav_core::soundcvt::*;
15 use super::{DecoderStuff, PktSendEvent};
17 static SKIP_ADECODING: AtomicBool = AtomicBool::new(false);
18 static AUDIO_VOLUME: AtomicUsize = AtomicUsize::new(100);
19 static AUDIO_END: AtomicBool = AtomicBool::new(false);
20 static CUR_QUEUE_FILL: AtomicUsize = AtomicUsize::new(0);
21 static SAMPLE_RATE: AtomicUsize = AtomicUsize::new(0);
22 static CHANNELS: AtomicUsize = AtomicUsize::new(0);
24 static CURRENT_TIME: AtomicUsize = AtomicUsize::new(0);
25 static CURRENT_TIME_SET: AtomicBool = AtomicBool::new(false);
27 const QUEUE_INITIAL_SIZE: usize = 16384;
28 const QUEUE_REFILL_LIMIT: usize = 262144;
40 fn new(srate: usize, chans: usize) -> Self {
41 SAMPLE_RATE.store(srate, Ordering::Relaxed);
42 CHANNELS.store(chans, Ordering::Relaxed);
43 CUR_QUEUE_FILL.store(0, Ordering::Relaxed);
45 queue: Vec::with_capacity(QUEUE_INITIAL_SIZE),
53 if self.start == 0 { return; }
54 let fill = self.fill();
56 if fill < self.start {
57 let (dst, src) = self.queue.split_at_mut(self.start);
58 dst[..fill].copy_from_slice(&src[..fill]);
61 self.queue[i] = self.queue[self.start + i];
68 fn set_time(&mut self) {
69 let fill = self.fill();
70 let samp_time = self.spos.saturating_sub(fill / self.chans);
71 CURRENT_TIME.store(samp_time * 1000 / self.srate, Ordering::Relaxed);
72 CURRENT_TIME_SET.store(true, Ordering::Relaxed);
73 CUR_QUEUE_FILL.store(self.fill(), Ordering::Relaxed);
75 fn add(&mut self, src: &[i16], samplepos: usize) {
76 if self.end + src.len() > self.queue.len() {
79 if self.end + src.len() > self.queue.len() {
80 self.queue.resize(self.end + src.len(), 0);
82 self.queue[self.end..][..src.len()].copy_from_slice(&src);
83 self.end += src.len();
84 self.spos = samplepos;
87 fn add_bytes(&mut self, src: &[u8], samplepos: usize) {
88 let srclen = src.len() / 2;
89 if self.end + srclen > self.queue.len() {
92 if self.end + srclen > self.queue.len() {
93 self.queue.resize(self.end + srclen, 0);
95 for (dst, src) in self.queue[self.end..][..srclen].iter_mut().zip(src.chunks_exact(2)) {
96 *dst = (u16::from(src[0]) + u16::from(src[1]) * 256) as i16;
99 self.spos = samplepos;
102 fn drain(&mut self, size: usize) {
103 let fill = self.fill();
111 fn fill(&self) -> usize { self.end - self.start }
112 fn flush(&mut self) {
118 pub struct AudioOutput {
119 queue: Arc<Mutex<AudioQueue>>,
122 impl AudioCallback for AudioOutput {
125 fn callback(&mut self, out: &mut [Self::Channel]) {
126 let mut queue = self.queue.lock().unwrap();
127 let dstlen = out.len();
128 let copylen = queue.fill().min(dstlen);
129 let volume = AUDIO_VOLUME.load(Ordering::Relaxed) as i32;
131 out[..copylen].copy_from_slice(&queue.queue[queue.start..][..copylen]);
133 for (dst, &src) in out[..copylen].iter_mut().zip(queue.queue[queue.start..].iter()) {
134 *dst = (i32::from(src) * volume / 100).max(-32768).min(32767) as i16;
137 queue.drain(copylen);
138 for el in out[copylen..].iter_mut() { *el = 0; }
142 fn dummy_audio_thread(aprecv: Receiver<PktSendEvent>) -> JoinHandle<()> {
143 std::thread::Builder::new().name("acontrol-dummy".to_string()).spawn(move ||{
145 match aprecv.recv() {
146 Ok(PktSendEvent::End) => break,
147 Ok(PktSendEvent::ImmediateEnd) => break,
157 type AudioPlaybackType = Option<AudioDevice<AudioOutput>>;
159 fn start_audio_decoding(asystem: &AudioSubsystem, ainfo: NAAudioInfo, mut audio_dec: DecoderStuff, aprecv: Receiver<PktSendEvent>) -> (AudioPlaybackType, JoinHandle<()>) {
160 let ch = ainfo.channels.max(2);
161 let desired_spec = AudioSpecDesired {
162 freq: Some(ainfo.sample_rate as i32),
166 let dst_info = NAAudioInfo {
167 sample_rate: ainfo.sample_rate,
169 format: SND_S16_FORMAT,
172 let queue = Arc::new(Mutex::new(AudioQueue::new(ainfo.sample_rate as usize, ch as usize)));
173 let qclone = queue.clone();
174 let ret = asystem.open_playback(None, &desired_spec, |_spec| {
180 return (None, dummy_audio_thread(aprecv))
182 let adevice = ret.unwrap();
183 (Some(adevice), std::thread::Builder::new().name("acontrol".to_string()).spawn(move ||{
184 let mut samplepos = 0usize;
185 let dst_chmap = if dst_info.channels == 2 {
186 NAChannelMap::from_str("L,R").unwrap()
188 NAChannelMap::from_str("C").unwrap()
190 SKIP_ADECODING.store(false, Ordering::Relaxed);
192 match aprecv.recv() {
193 Ok(PktSendEvent::Packet(pkt)) => {
195 if CUR_QUEUE_FILL.load(Ordering::Relaxed)
196 < QUEUE_REFILL_LIMIT || SKIP_ADECODING.load(Ordering::Relaxed) {
199 std::thread::sleep(Duration::from_millis(100));
201 if !SKIP_ADECODING.load(Ordering::Relaxed) {
202 if let Ok(frm) = audio_dec.dec.decode(&mut audio_dec.dsupp, &pkt) {
203 let buf = frm.get_buffer();
204 if let Some(pts) = frm.get_pts() {
205 samplepos = NATimeInfo::ts_to_time(pts, u64::from(dst_info.sample_rate), frm.ts.tb_num, frm.ts.tb_den) as usize;
207 samplepos += buf.get_audio_length();
208 if let Ok(out_buf) = convert_audio_frame(&buf, &dst_info, &dst_chmap) {
210 NABufferType::AudioI16(abuf) => {
211 let mut qdata = queue.lock().unwrap();
212 qdata.add(abuf.get_data(), samplepos);
215 NABufferType::AudioPacked(abuf) => {
216 let mut qdata = queue.lock().unwrap();
217 qdata.add_bytes(abuf.get_data(), samplepos);
226 Ok(PktSendEvent::Flush) => {
227 audio_dec.dec.flush();
228 let mut qdata = queue.lock().unwrap();
230 SKIP_ADECODING.store(false, Ordering::Relaxed);
232 Ok(PktSendEvent::End) => break,
233 Ok(PktSendEvent::ImmediateEnd) => {
234 let mut qdata = queue.lock().unwrap();
238 Ok(PktSendEvent::HurryUp) => {},
245 let qdata = queue.lock().unwrap();
246 if qdata.fill() == 0 || SKIP_ADECODING.load(Ordering::Relaxed) {
250 AUDIO_END.store(true, Ordering::Relaxed);
254 pub struct AudioControl {
255 aqueue: Vec<PktSendEvent>,
256 apsend: SyncSender<PktSendEvent>,
257 adevice: AudioPlaybackType,
258 athread: JoinHandle<()>,
262 pub fn new(audio_dec: Option<DecoderStuff>, ainfo: Option<NAAudioInfo>, asystem: &AudioSubsystem) -> Self {
263 let (apsend, aprecv) = std::sync::mpsc::sync_channel::<PktSendEvent>(20);
264 let (adevice, athread) = if let Some(audio_dec) = audio_dec {
265 start_audio_decoding(asystem, ainfo.unwrap(), audio_dec, aprecv)
267 (None, dummy_audio_thread(aprecv))
269 AUDIO_END.store(false, Ordering::Relaxed);
278 pub fn has_audio(&self) -> bool { self.adevice.is_some() }
279 pub fn pause(&mut self) {
280 if let Some(ref device) = self.adevice {
284 pub fn resume(&mut self) {
285 if let Some(ref device) = self.adevice {
289 pub fn set_volume(&mut self, volume: usize) {
290 AUDIO_VOLUME.store(volume, Ordering::Relaxed);
292 pub fn get_volume(&self) -> usize {
293 AUDIO_VOLUME.load(Ordering::Relaxed)
295 pub fn is_audio_end(&self) -> bool {
296 AUDIO_END.load(Ordering::Relaxed)
298 pub fn get_fill(&self) -> usize { CUR_QUEUE_FILL.load(Ordering::Relaxed) }
299 pub fn get_time(&self) -> Option<u64> {
300 if CURRENT_TIME_SET.load(Ordering::Relaxed) {
301 Some(CURRENT_TIME.load(Ordering::Relaxed) as u64)
306 pub fn get_time_left(&self) -> u64 {
307 let srate = SAMPLE_RATE.load(Ordering::Relaxed);
308 let chans = CHANNELS.load(Ordering::Relaxed);
309 if srate != 0 && chans != 0{
310 let fill = self.get_fill();
311 (fill * 1000 / srate / chans) as u64
317 pub fn get_queue_size(&self) -> usize { self.aqueue.len() }
318 pub fn try_send_audio(&mut self, evt: PktSendEvent) -> bool {
319 if self.aqueue.len() > 0 {
320 self.aqueue.push(evt);
323 self.try_send_event(evt)
326 fn try_send_event(&mut self, evt: PktSendEvent) -> bool {
327 if let Err(TrySendError::Full(evt)) = self.apsend.try_send(evt) {
328 self.aqueue.insert(0, evt);
334 pub fn try_send_queued(&mut self) -> bool {
335 while !self.aqueue.is_empty() {
336 let pkt = self.aqueue.remove(0);
337 if !self.try_send_event(pkt) {
344 pub fn flush(&mut self) {
346 self.aqueue.truncate(0);
347 SKIP_ADECODING.store(true, Ordering::Release);
348 CURRENT_TIME_SET.store(false, Ordering::Release);
349 let _ = self.apsend.send(PktSendEvent::Flush);
351 pub fn finish(self) {
352 SKIP_ADECODING.store(true, Ordering::Release);
353 let _ = self.apsend.send(PktSendEvent::ImmediateEnd);
354 self.athread.join().unwrap();