2 extern crate nihav_core;
3 extern crate nihav_registry;
4 extern crate nihav_allstuff;
10 use std::time::{Duration, Instant};
13 use sdl2::event::{Event, WindowEvent};
14 use sdl2::keyboard::Keycode;
15 use sdl2::mouse::MouseButton;
16 use sdl2::render::{Canvas, Texture, TextureCreator};
17 use sdl2::pixels::PixelFormatEnum;
18 use sdl2::video::{Window, WindowContext};
20 use nihav_registry::detect;
21 use nihav_core::frame::*;
22 use nihav_core::io::byteio::{FileReader, ByteReader};
23 use nihav_core::reorder::*;
24 use nihav_core::codecs::*;
25 use nihav_core::demuxers::*;
26 use nihav_registry::register::*;
27 use nihav_allstuff::*;
36 #[cfg(feature="debug")]
37 macro_rules! debug_log {
38 ($log: expr; $blk: block) => {
39 $log.logfile.write($blk.as_bytes()).unwrap();
40 $log.logfile.write(b"\n").unwrap();
43 #[cfg(not(feature="debug"))]
44 macro_rules! debug_log {
45 ($log: expr; $blk: block) => {};
48 pub enum PktSendEvent {
57 pub enum DecoderType {
58 Audio(Box<dyn NADecoder + Send>),
59 Video(Box<dyn NADecoder + Send>, Box<dyn FrameReorderer + Send>),
60 VideoMT(Box<dyn NADecoderMT + Send>, MTFrameReorderer),
63 pub struct DecoderStuff {
64 pub dsupp: Box<NADecoderSupport>,
68 fn format_time(ms: u64) -> String {
70 let ds = (ms % 1000) / 100;
71 let (min, s) = (s / 60, s % 60);
72 let (h, min) = (min / 60, min % 60);
75 format!("{}.{}", s, ds)
77 format!("{}:{:02}.{}", min, s, ds)
80 format!("{}:{:02}:{:02}.{}", h, min, s, ds)
84 const FRAME_QUEUE_LEN: usize = 25;
85 const MAX_VOLUME: usize = 200;
87 pub type FrameRecord = (NABufferType, u64);
97 ref_time: Instant::now(),
101 pub fn get_cur_time(&self) -> u64 {
102 let add = self.ref_time.elapsed().as_millis() as u64;
105 fn reset_ts(&mut self) {
108 fn reset_all(&mut self, ts: u64) {
109 self.ref_time = Instant::now();
112 fn set_ts(&mut self) {
113 self.ref_ts = self.get_cur_time();
115 fn set_time(&mut self) {
116 self.ref_time = Instant::now();
120 pub struct DispFrame<'a> {
124 pub rgb_tex: Texture<'a>,
125 pub yuv_tex: Texture<'a>,
128 pub struct DispQueue<'a> {
129 pub pool: Vec<DispFrame<'a>>,
139 impl<'a> DispQueue<'a> {
140 fn new(texture_creator: &'a TextureCreator<WindowContext>, width: usize, height: usize, len: usize) -> Self {
141 let mut pool = Vec::with_capacity(len);
142 for _ in 0..len + 1 {
143 let rgb_tex = texture_creator.create_texture_streaming(PixelFormatEnum::RGB24, width as u32, height as u32).unwrap();
144 let yuv_tex = texture_creator.create_texture_streaming(PixelFormatEnum::IYUV, ((width + 1) & !1) as u32, ((height + 1) & !1) as u32).unwrap();
145 pool.push(DispFrame{ ts: 0, is_yuv: false, valid: false, rgb_tex, yuv_tex });
147 pool[len].is_yuv = false;
148 pool[len].rgb_tex.with_lock(None, |buffer: &mut [u8], _pitch: usize| {
149 for el in buffer.iter_mut() { *el = 0; }
152 Self { pool, first_ts: 0, last_ts: 0, start: 0, end: 0, len, width, height }
155 fn flush(&mut self) {
160 for frm in self.pool.iter_mut() {
165 fn get_last_texture(&mut self, osd: &OSD) -> &Texture<'a> {
166 if self.pool[self.len].is_yuv {
168 self.pool[self.len].yuv_tex.with_lock(None, |buffer: &mut [u8], pitch: usize| {
169 osd.draw_yuv(buffer, pitch);
172 &self.pool[self.len].yuv_tex
175 self.pool[self.len].rgb_tex.with_lock(None, |buffer: &mut [u8], pitch: usize| {
176 osd.draw_rgb(buffer, pitch);
179 &self.pool[self.len].rgb_tex
182 pub fn is_empty(&self) -> bool { self.start == self.end }
183 pub fn is_full(&self) -> bool { self.len == 0 || self.start == (self.end + 1) % self.len }
184 pub fn move_end(&mut self) {
186 if self.end >= self.len {
187 self.end -= self.len;
190 pub fn move_start(&mut self) {
191 self.pool.swap(self.start, self.len);
193 if self.start >= self.len {
194 self.start -= self.len;
196 if !self.is_empty() {
197 self.first_ts = self.pool[self.start].ts;
202 fn try_display(disp_queue: &mut DispQueue, canvas: &mut Canvas<Window>, osd: &mut OSD, ctime: &TimeKeep) -> Option<u64> {
203 while !disp_queue.is_empty() {
204 let disp_time = disp_queue.first_ts;
205 let ctime = ctime.get_cur_time();
206 if disp_time > ctime + 10 {
207 return Some(disp_time - ctime);
208 } else if disp_time + 10 < ctime {
209 disp_queue.move_start();
214 let frm = &mut disp_queue.pool[disp_queue.start];
215 let texture = if frm.is_yuv {
217 frm.yuv_tex.with_lock(None, |buffer: &mut [u8], pitch: usize| {
218 osd.draw_yuv(buffer, pitch);
224 frm.rgb_tex.with_lock(None, |buffer: &mut [u8], pitch: usize| {
225 osd.draw_rgb(buffer, pitch);
231 canvas.copy(texture, None, None).unwrap();
234 disp_queue.move_start();
235 if !disp_queue.is_empty() {
236 return Some((disp_queue.first_ts - ctime).saturating_sub(2));
246 sdl_context: sdl2::Sdl,
247 vsystem: sdl2::VideoSubsystem,
248 asystem: sdl2::AudioSubsystem,
252 acontrol: AudioControl,
253 vcontrol: VideoControl,
275 #[cfg(feature="debug")]
281 let sdl_context = sdl2::init().unwrap();
282 let vsystem = sdl_context.video().unwrap();
283 let asystem = sdl_context.audio().unwrap();
284 vsystem.disable_screen_saver();
285 let acontrol = AudioControl::new(None, None, &asystem);
286 let vcontrol = VideoControl::new(None, 0, 0, 0, 0);
288 sdl_context, asystem, vsystem,
309 tkeep: TimeKeep::new(),
314 #[cfg(feature="debug")]
315 logfile: File::create("debug.log").unwrap(),
318 fn seek(&mut self, off: u64, fwd: bool, dmx: &mut Demuxer, disp_queue: &mut DispQueue) {
319 let cur_time = self.tkeep.get_cur_time();
320 let seektime = if fwd { cur_time + off * 1000 } else {
321 cur_time.saturating_sub(off * 1000) };
322 debug_log!(self; {format!(" seek to {}", seektime)});
324 let ret = dmx.seek(NATimePoint::Milliseconds(seektime));
326 println!(" seek error");
330 self.acontrol.flush();
331 self.vcontrol.flush();
334 self.tkeep.reset_ts();
335 self.prefill(dmx, disp_queue);
336 if !disp_queue.is_empty() {
337 self.tkeep.reset_all(disp_queue.first_ts);
339 let mut iterations = 0;
340 let mut time = self.acontrol.get_time();
341 while time.is_none() {
343 std::thread::yield_now();
344 if iterations > 1000000 { println!(" still no time set?!"); break; }
345 time = self.acontrol.get_time();
347 if let Some(time) = time {
348 self.tkeep.reset_all(time);
352 self.acontrol.resume();
355 fn prefill(&mut self, dmx: &mut Demuxer, disp_queue: &mut DispQueue) {
356 debug_log!(self; {" prefilling"});
357 while self.vcontrol.get_queue_size() < FRAME_QUEUE_LEN {
358 let mut try_send = self.acontrol.get_queue_size() < FRAME_QUEUE_LEN && (!self.has_video || (!self.vcontrol.is_filled(FRAME_QUEUE_LEN) && !disp_queue.is_full()));
360 if !self.vcontrol.try_send_queued() && self.vcontrol.get_queue_size() > FRAME_QUEUE_LEN / 2 {
363 if !self.acontrol.try_send_queued() && self.acontrol.get_queue_size() > FRAME_QUEUE_LEN / 2 {
367 match dmx.get_frame() {
368 Err(DemuxerError::EOF) => break,
371 let streamno = pkt.get_stream().get_id();
372 if self.has_video && streamno == self.video_str {
373 self.vcontrol.try_send_video(PktSendEvent::Packet(pkt));
374 } else if self.has_audio && streamno == self.audio_str {
375 self.acontrol.try_send_audio(PktSendEvent::Packet(pkt));
380 self.vcontrol.fill(disp_queue);
387 while self.vcontrol.get_queue_size() > 0 && !disp_queue.is_full() {
388 self.vcontrol.try_send_queued();
389 self.vcontrol.fill(disp_queue);
390 std::thread::sleep(Duration::from_millis(10));
392 self.vcontrol.wait_for_frames();
393 self.vcontrol.fill(disp_queue);
395 debug_log!(self; {format!(" prefilling done, frames {}-{} audio {}", disp_queue.start, disp_queue.end, self.acontrol.get_fill())});
397 fn toggle_pause(&mut self) {
398 self.paused = !self.paused;
400 self.vsystem.enable_screen_saver();
403 self.vsystem.disable_screen_saver();
404 self.tkeep.set_time();
407 self.acontrol.pause();
409 self.acontrol.resume();
412 fn handle_events(&mut self, event_pump: &mut sdl2::EventPump, canvas: &mut Canvas<Window>, dmx: &mut Demuxer, disp_queue: &mut DispQueue) -> bool {
413 for event in event_pump.poll_iter() {
414 if let Event::Quit {..} = event {
419 if let Event::Window {win_event: WindowEvent::Exposed, ..} = event {
421 canvas.copy(disp_queue.get_last_texture(&self.osd), None, None).unwrap();
424 if let Event::MouseButtonDown {mouse_btn: MouseButton::Right, ..} = event {
427 if let Event::KeyDown {keycode: Some(keycode), ..} = event {
429 Keycode::Escape | Keycode::Q => {
434 Keycode::Return => return true,
435 Keycode::Right => { self.seek(10, true, dmx, disp_queue); },
436 Keycode::Left => { self.seek(10, false, dmx, disp_queue); },
437 Keycode::Up => { self.seek(60, true, dmx, disp_queue); },
438 Keycode::Down => { self.seek(60, false, dmx, disp_queue); },
439 Keycode::PageUp => { self.seek(600, true, dmx, disp_queue); },
440 Keycode::PageDown => { self.seek(600, false, dmx, disp_queue); },
441 Keycode::Space => { self.toggle_pause(); },
442 Keycode::Plus | Keycode::KpPlus => {
443 self.volume = (self.volume + 10).min(MAX_VOLUME);
445 self.acontrol.set_volume(self.volume);
448 Keycode::Minus | Keycode::KpMinus => {
449 self.volume = self.volume.saturating_sub(10);
451 self.acontrol.set_volume(self.volume);
455 self.debug = !self.debug;
458 self.mute = !self.mute;
460 self.acontrol.set_volume(0);
462 self.acontrol.set_volume(self.volume);
466 self.vcontrol.try_send_video(PktSendEvent::HurryUp);
474 print!("{:60}\r", ' ');
475 std::io::stdout().flush().unwrap();
481 fn play(&mut self, name: &str, start_time: NATimePoint) {
482 debug_log!(self; {format!("Playing {}", name)});
484 // prepare data source
485 let path = Path::new(name);
486 let mut file = File::open(path).unwrap();
488 let mut fr = FileReader::new_read(&mut file);
489 let mut br = ByteReader::new(&mut fr);
490 let res = detect::detect_format(name, &mut br);
492 println!("cannot detect format for {}", name);
495 let (dmx_name, _score) = res.unwrap();
496 debug_log!(self; {format!(" found demuxer {} with score {:?}", dmx_name, _score)});
497 println!("trying demuxer {} on {}", dmx_name, name);
499 let mut dmx_reg = RegisteredDemuxers::new();
500 nihav_register_all_demuxers(&mut dmx_reg);
501 let mut dec_reg = RegisteredDecoders::new();
502 nihav_register_all_decoders(&mut dec_reg);
503 let mut mtdec_reg = RegisteredMTDecoders::new();
505 nihav_register_all_mt_decoders(&mut mtdec_reg);
508 let ret = dmx_reg.find_demuxer(dmx_name);
510 println!("error finding {} demuxer", dmx_name);
513 dmx_fact = ret.unwrap();
514 br.seek(SeekFrom::Start(0)).unwrap();
515 let ret = create_demuxer(dmx_fact, &mut br);
517 println!("error creating demuxer");
520 let mut dmx = ret.unwrap();
521 if start_time != NATimePoint::None {
522 debug_log!(self; {format!(" start seek to {}", start_time)});
523 if dmx.seek(start_time).is_err() {
524 println!("initial seek failed");
529 let mut height = 480;
532 let mut ainfo: Option<NAAudioInfo> = None;
534 let mut video_dec: Option<DecoderStuff> = None;
535 let mut audio_dec: Option<DecoderStuff> = None;
537 let duration = dmx.get_duration();
539 println!(" total duration {}", format_time(duration));
541 self.has_video = false;
542 self.has_audio = false;
544 self.osd.set_duration(duration);
545 for i in 0..dmx.get_num_streams() {
546 let s = dmx.get_stream(i).unwrap();
547 let info = s.get_info();
548 let decfunc = dec_reg.find_decoder(info.get_name());
549 let decfunc_mt = mtdec_reg.find_decoder(info.get_name());
550 println!("stream {} - {} {}", i, s, info.get_name());
551 debug_log!(self; {format!(" stream {} - {} {}", i, s, info.get_name())});
552 let str_id = s.get_id();
554 if video_dec.is_none() && self.play_video {
555 if let Some(decfunc) = decfunc_mt {
556 let mut dec = (decfunc)();
557 let mut dsupp = Box::new(NADecoderSupport::new());
558 let props = info.get_properties().get_video_info().unwrap();
559 if props.get_width() != 0 {
560 width = props.get_width();
561 height = props.get_height();
563 if dec.init(&mut dsupp, info.clone(), self.vthreads).is_ok() {
564 video_dec = Some(DecoderStuff{ dsupp, dec: DecoderType::VideoMT(dec, MTFrameReorderer::new()) });
565 self.video_str = str_id;
566 let (tbn, tbd) = s.get_timebase();
569 self.has_video = true;
572 println!("failed to create multi-threaded decoder, falling back");
575 if let Some(decfunc) = decfunc {
576 let mut dec = (decfunc)();
577 let mut dsupp = Box::new(NADecoderSupport::new());
578 let props = info.get_properties().get_video_info().unwrap();
579 if props.get_width() != 0 {
580 width = props.get_width();
581 height = props.get_height();
583 let desc = get_codec_description(info.get_name());
584 let (reorder_depth, reord) = if desc.is_none() || (desc.unwrap().caps & CODEC_CAP_COMPLEX_REORDER) == 0 {
585 let reord: Box<dyn FrameReorderer + Send> = Box::new(IPBReorderer::new());
588 let reord: Box<dyn FrameReorderer + Send> = Box::new(ComplexReorderer::new());
591 dsupp.pool_u8 = NAVideoBufferPool::new(reorder_depth);
592 dsupp.pool_u16 = NAVideoBufferPool::new(reorder_depth);
593 dsupp.pool_u32 = NAVideoBufferPool::new(reorder_depth);
594 dec.init(&mut dsupp, info).unwrap();
595 video_dec = Some(DecoderStuff{ dsupp, dec: DecoderType::Video(dec, reord) });
596 self.video_str = str_id;
597 let (tbn, tbd) = s.get_timebase();
600 self.has_video = true;
602 println!("no video decoder for {} found!", info.get_name());
605 } else if info.is_audio() {
606 if audio_dec.is_none() && self.play_audio {
607 if let Some(decfunc) = decfunc {
608 let mut dec = (decfunc)();
609 let mut dsupp = Box::new(NADecoderSupport::new());
610 ainfo = info.get_properties().get_audio_info();
611 dec.init(&mut dsupp, info).unwrap();
612 audio_dec = Some(DecoderStuff{ dsupp, dec: DecoderType::Audio(dec) });
613 self.audio_str = str_id;
614 self.has_audio = true;
616 println!("no audio decoder for {} found!", info.get_name());
620 println!("decoder {} not found", info.get_name());
623 if !self.has_video && !self.has_audio {
624 println!("No playable streams found.");
628 while (width <= 384) && (height <= 288) {
633 // prepare playback structure
634 let mut new_vcontrol = VideoControl::new(video_dec, width, height, tb_num, tb_den);
635 std::mem::swap(&mut self.vcontrol, &mut new_vcontrol);
637 let mut new_acontrol = AudioControl::new(audio_dec, ainfo, &self.asystem);
638 std::mem::swap(&mut self.acontrol, &mut new_acontrol);
641 self.acontrol.set_volume(0);
643 self.acontrol.set_volume(self.volume);
646 let fname = path.file_name();
647 let wname = if let Some(fname) = fname {
648 "NihAV player - ".to_owned() + fname.to_str().unwrap()
650 "NihAV player".to_owned()
652 let mut builder = self.vsystem.window(&wname, width as u32, height as u32);
653 let window = if let (Some(xpos), Some(ypos)) = (self.xpos, self.ypos) {
654 builder.position(xpos, ypos).build().unwrap()
656 builder.position_centered().build().unwrap()
658 let mut canvas = window.into_canvas().build().unwrap();
659 let texture_creator = canvas.texture_creator();
660 let mut disp_q = DispQueue::new(&texture_creator, width, height, if self.has_video { FRAME_QUEUE_LEN } else { 0 });
663 canvas.copy(disp_q.get_last_texture(&self.osd), None, None).unwrap();
667 self.has_audio = self.acontrol.has_audio();
668 if !self.has_video && !self.has_audio {
669 println!("No playable streams.");
674 self.prefill(&mut dmx, &mut disp_q);
675 self.tkeep.reset_all(if !disp_q.is_empty() { disp_q.first_ts } else { 0 });
677 self.acontrol.resume();
679 let mut event_pump = self.sdl_context.event_pump().unwrap();
680 let mut last_disp = Instant::now();
681 let mut has_data = true;
683 if self.handle_events(&mut event_pump, &mut canvas, &mut dmx, &mut disp_q) {
688 let mut try_send = self.acontrol.get_queue_size() < FRAME_QUEUE_LEN && self.vcontrol.get_queue_size() < FRAME_QUEUE_LEN;
689 if !self.vcontrol.try_send_queued() && self.vcontrol.is_filled(FRAME_QUEUE_LEN) {
692 if !self.acontrol.try_send_queued() {
695 while has_data && try_send {
696 match dmx.get_frame() {
697 Err(DemuxerError::EOF) => {
698 self.vcontrol.try_send_video(PktSendEvent::End);
699 self.acontrol.try_send_audio(PktSendEvent::End);
703 println!("demuxer error {:?}", err);
704 if err == DemuxerError::IOError {
705 self.vcontrol.try_send_video(PktSendEvent::End);
706 self.acontrol.try_send_audio(PktSendEvent::End);
711 let streamno = pkt.get_stream().get_id();
712 if self.has_video && streamno == self.video_str {
713 debug_log!(self; {" sending video packet"});
714 self.vcontrol.try_send_video(PktSendEvent::Packet(pkt));
715 if self.vcontrol.is_filled(FRAME_QUEUE_LEN) {
718 } else if self.has_audio && streamno == self.audio_str {
719 debug_log!(self; {" sending audio packet"});
720 self.acontrol.try_send_audio(PktSendEvent::Packet(pkt));
721 if self.acontrol.get_queue_size() >= FRAME_QUEUE_LEN {
728 self.vcontrol.fill(&mut disp_q);
729 let mut sleep_time = 25;
730 debug_log!(self; {format!(" time {}", self.tkeep.get_cur_time())});
732 debug_log!(self; {format!(" disp queue {}-{}, {}-{} vqueue fill {}", disp_q.first_ts, disp_q.last_ts, disp_q.start, disp_q.end, self.vcontrol.get_queue_size())});
733 let ret = try_display(&mut disp_q, &mut canvas, &mut self.osd, &self.tkeep);
734 if let Some(next_time) = ret {
735 sleep_time = sleep_time.min(next_time);
739 let time_left = self.acontrol.get_time_left();
740 debug_log!(self; {format!(" audio left {}", time_left)});
741 sleep_time = sleep_time.min(time_left);
743 debug_log!(self; {format!(" sleep {}ms", sleep_time)});
744 if last_disp.elapsed().as_millis() >= 10 {
745 let c_time = self.tkeep.get_cur_time();
748 print!(" {} {}% \r", format_time(c_time), self.acontrol.get_volume());
750 print!(" {} {} {}% {:3} {:6}\r", format_time(c_time), if self.vcontrol.is_yuv() { 'Y' } else { 'R' }, self.acontrol.get_volume(), (disp_q.end + disp_q.len - disp_q.start) % disp_q.len, self.acontrol.get_fill());
752 std::io::stdout().flush().unwrap();
753 last_disp = Instant::now();
756 if self.has_video && !self.vcontrol.is_video_end() {
759 if self.has_audio && !self.acontrol.is_audio_end() {
765 thread::sleep(Duration::from_millis(sleep_time));
767 thread::sleep(Duration::from_millis(20));
770 let (xpos, ypos) = canvas.into_window().position();
771 self.xpos = Some(xpos);
772 self.ypos = Some(ypos);
774 std::mem::swap(&mut self.vcontrol, &mut new_vcontrol);
775 new_vcontrol.finish();
776 std::mem::swap(&mut self.acontrol, &mut new_acontrol);
777 new_acontrol.finish();
782 let args: Vec<String> = env::args().collect();
785 println!("usage: nihav-player file1 file2 ...");
789 let mut player = Player::new();
791 let mut aiter = args.iter().skip(1);
792 let mut seek_time = NATimePoint::None;
793 while let Some(arg) = aiter.next() {
795 "-an" => { player.play_audio = false; },
796 "-ae" => { player.play_audio = true; },
797 "-vn" => { player.play_video = false; },
798 "-ve" => { player.play_video = true; },
800 if let Some(arg) = aiter.next() {
801 if let Ok(time) = arg.parse::<NATimePoint>() {
804 println!("wrong seek time");
805 seek_time = NATimePoint::None;
810 if let Some(arg) = aiter.next() {
811 if let Ok(vol) = arg.parse::<usize>() {
812 player.volume = vol.min(MAX_VOLUME);
814 println!("wrong volume");
822 player.debug = false;
825 player.use_mt = true;
828 player.use_mt = false;
831 if let Some(arg) = aiter.next() {
832 if let Ok(val) = arg.parse::<usize>() {
833 player.vthreads = val.max(1);
835 println!("wrong number of threads");
840 player.play(arg, seek_time);
841 if player.end { break; }
842 seek_time = NATimePoint::None;