]> git.nihav.org Git - nihav-player.git/blob - videoplayer/src/main.rs
6becffd09bd7e897b3b40de86a10895a4d3797b8
[nihav-player.git] / videoplayer / src / main.rs
1 extern crate sdl2;
2 extern crate nihav_core;
3 extern crate nihav_registry;
4 extern crate nihav_allstuff;
5
6 use std::env;
7 use std::fs::File;
8 use std::io::Write;
9 use std::path::Path;
10 use std::time::{Duration, Instant};
11 use std::thread;
12 use std::sync::atomic::{AtomicU8, Ordering};
13
14 use sdl2::event::{Event, WindowEvent};
15 use sdl2::keyboard::{Keycode, Mod};
16 use sdl2::mouse::MouseButton;
17 use sdl2::render::{Canvas, Texture, TextureCreator};
18 use sdl2::pixels::PixelFormatEnum;
19 use sdl2::video::{Window, WindowContext};
20
21 use nihav_registry::detect;
22 use nihav_core::frame::*;
23 use nihav_core::io::byteio::{FileReader, ByteReader};
24 use nihav_core::reorder::*;
25 use nihav_core::codecs::*;
26 use nihav_core::demuxers::*;
27 use nihav_registry::register::*;
28 use nihav_allstuff::*;
29
30 mod audiodec;
31 use audiodec::*;
32 mod videodec;
33 use videodec::*;
34 mod osd;
35 use osd::*;
36
37 #[repr(u8)]
38 #[derive(Clone,Copy,Debug,PartialEq)]
39 enum DecodingState {
40 Normal,
41 Waiting,
42 Flush,
43 Prefetch,
44 Error,
45 End,
46 }
47
48 impl Default for DecodingState {
49 fn default() -> Self { DecodingState::Normal }
50 }
51
52 impl From<u8> for DecodingState {
53 fn from(val: u8) -> Self {
54 match val {
55 0 => DecodingState::Normal,
56 1 => DecodingState::Waiting,
57 2 => DecodingState::Flush,
58 3 => DecodingState::Prefetch,
59 4 => DecodingState::End,
60 _ => DecodingState::Error,
61 }
62 }
63 }
64
65 struct DecoderState {
66 state: AtomicU8
67 }
68
69 impl DecoderState {
70 const fn new() -> Self {
71 Self {
72 state: AtomicU8::new(DecodingState::Normal as u8)
73 }
74 }
75 fn set_state(&self, state: DecodingState) {
76 self.state.store(state as u8, Ordering::Release);
77 }
78 fn get_state(&self) -> DecodingState {
79 self.state.load(Ordering::Acquire).into()
80 }
81 fn is_flushing(&self) -> bool {
82 matches!(self.get_state(), DecodingState::Flush | DecodingState::Error)
83 }
84 }
85
86 #[cfg(feature="debug")]
87 macro_rules! debug_log {
88 ($log: expr; $blk: block) => {
89 $log.logfile.write($blk.as_bytes()).unwrap();
90 $log.logfile.write(b"\n").unwrap();
91 };
92 }
93 #[cfg(not(feature="debug"))]
94 macro_rules! debug_log {
95 ($log: expr; $blk: block) => {};
96 }
97
98 pub enum PktSendEvent {
99 Packet(NAPacket),
100 GetFrames,
101 Flush,
102 End,
103 ImmediateEnd,
104 HurryUp,
105 }
106
107 pub enum DecoderType {
108 Audio(Box<dyn NADecoder + Send>),
109 Video(Box<dyn NADecoder + Send>, Box<dyn FrameReorderer + Send>),
110 VideoMT(Box<dyn NADecoderMT + Send>, MTFrameReorderer),
111 }
112
113 pub struct DecoderStuff {
114 pub dsupp: Box<NADecoderSupport>,
115 pub dec: DecoderType,
116 }
117
118 fn format_time(ms: u64) -> String {
119 let s = ms / 1000;
120 let ds = (ms % 1000) / 100;
121 let (min, s) = (s / 60, s % 60);
122 let (h, min) = (min / 60, min % 60);
123 if h == 0 {
124 if min == 0 {
125 format!("{}.{}", s, ds)
126 } else {
127 format!("{}:{:02}.{}", min, s, ds)
128 }
129 } else {
130 format!("{}:{:02}:{:02}.{}", h, min, s, ds)
131 }
132 }
133
134 const FRAME_QUEUE_LEN: usize = 25;
135 const MAX_VOLUME: usize = 200;
136
137 pub type FrameRecord = (NABufferType, u64);
138
139 pub struct TimeKeep {
140 ref_time: Instant,
141 ref_ts: u64,
142 }
143
144 impl TimeKeep {
145 fn new() -> Self {
146 Self {
147 ref_time: Instant::now(),
148 ref_ts: 0,
149 }
150 }
151 pub fn get_cur_time(&self) -> u64 {
152 let add = self.ref_time.elapsed().as_millis() as u64;
153 self.ref_ts + add
154 }
155 fn reset_ts(&mut self) {
156 self.ref_ts = 0;
157 }
158 fn reset_all(&mut self, ts: u64) {
159 self.ref_time = Instant::now();
160 self.ref_ts = ts;
161 }
162 fn set_ts(&mut self) {
163 self.ref_ts = self.get_cur_time();
164 }
165 fn set_time(&mut self) {
166 self.ref_time = Instant::now();
167 }
168 }
169
170 pub struct DispFrame<'a> {
171 pub ts: u64,
172 pub is_yuv: bool,
173 pub valid: bool,
174 pub rgb_tex: Texture<'a>,
175 pub yuv_tex: Texture<'a>,
176 }
177
178 pub struct DispQueue<'a> {
179 pub pool: Vec<DispFrame<'a>>,
180 pub first_ts: u64,
181 pub last_ts: u64,
182 pub start: usize,
183 pub end: usize,
184 pub len: usize,
185 pub width: usize,
186 pub height: usize,
187 }
188
189 impl<'a> DispQueue<'a> {
190 fn new(texture_creator: &'a TextureCreator<WindowContext>, width: usize, height: usize, len: usize) -> Self {
191 let mut pool = Vec::with_capacity(len);
192 for _ in 0..len + 1 {
193 let rgb_tex = texture_creator.create_texture_streaming(PixelFormatEnum::RGB24, width as u32, height as u32).expect("failed to create RGB texture");
194 let yuv_tex = texture_creator.create_texture_streaming(PixelFormatEnum::IYUV, ((width + 1) & !1) as u32, ((height + 1) & !1) as u32).expect("failed to create YUV texture");
195 pool.push(DispFrame{ ts: 0, is_yuv: false, valid: false, rgb_tex, yuv_tex });
196 }
197 pool[len].is_yuv = false;
198 pool[len].rgb_tex.with_lock(None, |buffer: &mut [u8], _pitch: usize| {
199 for el in buffer.iter_mut() { *el = 0; }
200 }).expect("RGB texture could not be locked");
201
202 Self { pool, first_ts: 0, last_ts: 0, start: 0, end: 0, len, width, height }
203 }
204
205 fn flush(&mut self) {
206 self.start = 0;
207 self.end = 0;
208 self.first_ts = 0;
209 self.last_ts = 0;
210 for frm in self.pool.iter_mut() {
211 frm.valid = false;
212 }
213 }
214
215 fn get_last_texture(&mut self, osd: &OSD) -> &Texture<'a> {
216 if self.pool[self.len].is_yuv {
217 if osd.is_active() {
218 self.pool[self.len].yuv_tex.with_lock(None, |buffer: &mut [u8], pitch: usize| {
219 osd.draw_yuv(buffer, pitch);
220 }).expect("YUV texture locking failure");
221 }
222 &self.pool[self.len].yuv_tex
223 } else {
224 if osd.is_active() {
225 self.pool[self.len].rgb_tex.with_lock(None, |buffer: &mut [u8], pitch: usize| {
226 osd.draw_rgb(buffer, pitch);
227 }).expect("RGB texture locking failure");
228 }
229 &self.pool[self.len].rgb_tex
230 }
231 }
232 pub fn is_empty(&self) -> bool { self.start == self.end }
233 pub fn is_full(&self) -> bool { self.len == 0 || self.start == (self.end + 1) % self.len }
234 pub fn move_end(&mut self) {
235 self.end += 1;
236 if self.end >= self.len {
237 self.end -= self.len;
238 }
239 }
240 pub fn move_start(&mut self) {
241 self.pool.swap(self.start, self.len);
242 self.start += 1;
243 if self.start >= self.len {
244 self.start -= self.len;
245 }
246 if !self.is_empty() {
247 self.first_ts = self.pool[self.start].ts;
248 }
249 }
250 }
251
252 fn try_display(disp_queue: &mut DispQueue, canvas: &mut Canvas<Window>, osd: &mut OSD, ctime: &TimeKeep) -> Option<u64> {
253 while !disp_queue.is_empty() {
254 let disp_time = disp_queue.first_ts;
255 let ctime = ctime.get_cur_time();
256 if disp_time > ctime + 10 {
257 return Some(disp_time - ctime);
258 } else if disp_time + 10 < ctime {
259 disp_queue.move_start();
260 } else {
261 if osd.is_active() {
262 osd.prepare(ctime);
263 }
264 let frm = &mut disp_queue.pool[disp_queue.start];
265 let texture = if frm.is_yuv {
266 if osd.is_active() {
267 frm.yuv_tex.with_lock(None, |buffer: &mut [u8], pitch: usize| {
268 osd.draw_yuv(buffer, pitch);
269 }).expect("YUV texture locking failure");
270 }
271 &frm.yuv_tex
272 } else {
273 if osd.is_active() {
274 frm.rgb_tex.with_lock(None, |buffer: &mut [u8], pitch: usize| {
275 osd.draw_rgb(buffer, pitch);
276 }).expect("RGB texture locking failure");
277 }
278 &frm.rgb_tex
279 };
280 canvas.clear();
281 canvas.copy(texture, None, None).expect("canvas blit failure");
282 canvas.present();
283
284 disp_queue.move_start();
285 if !disp_queue.is_empty() {
286 return Some((disp_queue.first_ts - ctime).saturating_sub(2));
287 } else {
288 return None;
289 }
290 }
291 }
292 None
293 }
294
295 struct Player {
296 sdl_context: sdl2::Sdl,
297 vsystem: sdl2::VideoSubsystem,
298 asystem: sdl2::AudioSubsystem,
299 xpos: Option<i32>,
300 ypos: Option<i32>,
301
302 acontrol: AudioControl,
303 vcontrol: VideoControl,
304
305 play_video: bool,
306 play_audio: bool,
307 has_video: bool,
308 has_audio: bool,
309 video_str: u32,
310 audio_str: u32,
311
312 vthreads: usize,
313 use_mt: bool,
314
315 paused: bool,
316 mute: bool,
317 volume: usize,
318 end: bool,
319
320 tkeep: TimeKeep,
321
322 debug: bool,
323 osd: OSD,
324
325 #[cfg(feature="debug")]
326 logfile: File,
327 }
328
329 impl Player {
330 fn new() -> Self {
331 let sdl_context = sdl2::init().expect("SDL2 init failure");
332 let vsystem = sdl_context.video().expect("video subsystem init failure");
333 let asystem = sdl_context.audio().expect("audio subsystem init failure");
334 vsystem.disable_screen_saver();
335 let acontrol = AudioControl::new(None, None, &asystem);
336 let vcontrol = VideoControl::new(None, 0, 0, 0, 0);
337 Self {
338 sdl_context, asystem, vsystem,
339 xpos: None,
340 ypos: None,
341
342 acontrol, vcontrol,
343
344 play_video: true,
345 play_audio: true,
346 has_video: false,
347 has_audio: false,
348 video_str: 0,
349 audio_str: 0,
350
351 vthreads: 3,
352 use_mt: true,
353
354 paused: false,
355 mute: false,
356 volume: 100,
357 end: false,
358
359 tkeep: TimeKeep::new(),
360
361 debug: false,
362 osd: OSD::new(),
363
364 #[cfg(feature="debug")]
365 logfile: File::create("debug.log").expect("'debug.log' should be available for writing"),
366 }
367 }
368 fn seek(&mut self, off: u64, fwd: bool, dmx: &mut Demuxer, disp_queue: &mut DispQueue) -> Result<(), ()> {
369 let cur_time = self.tkeep.get_cur_time();
370 let seektime = if fwd { cur_time + off * 1000 } else {
371 cur_time.saturating_sub(off * 1000) };
372 debug_log!(self; {format!(" seek to {}", seektime)});
373
374 let ret = dmx.seek(NATimePoint::Milliseconds(seektime));
375 if ret.is_err() {
376 println!(" seek error");
377 return Ok(()); //TODO: not ignore some of seek errors?
378 }
379
380 self.acontrol.flush();
381 self.vcontrol.flush();
382 disp_queue.flush();
383
384 self.tkeep.reset_ts();
385 self.prefill(dmx, disp_queue)?;
386 if !disp_queue.is_empty() {
387 self.tkeep.reset_all(disp_queue.first_ts);
388 } else {
389 let mut iterations = 0;
390 let mut time = self.acontrol.get_time();
391 while time.is_none() {
392 iterations += 1;
393 std::thread::yield_now();
394 if iterations > 1000000 { println!(" still no time set?!"); break; }
395 time = self.acontrol.get_time();
396 }
397 if let Some(time) = time {
398 self.tkeep.reset_all(time);
399 }
400 }
401 if !self.paused {
402 self.acontrol.resume();
403 }
404 Ok(())
405 }
406 fn prefill(&mut self, dmx: &mut Demuxer, disp_queue: &mut DispQueue) -> Result<(), ()> {
407 debug_log!(self; {" prefilling"});
408 while self.vcontrol.get_queue_size() < FRAME_QUEUE_LEN {
409 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()));
410
411 if !self.vcontrol.try_send_queued() && self.vcontrol.get_queue_size() > FRAME_QUEUE_LEN / 2 {
412 try_send = false;
413 }
414 if !self.acontrol.try_send_queued() && self.acontrol.get_queue_size() > FRAME_QUEUE_LEN / 2 {
415 try_send = false;
416 }
417 if try_send {
418 match dmx.get_frame() {
419 Err(DemuxerError::EOF) => break,
420 Err(_) => break,
421 Ok(pkt) => {
422 let streamno = pkt.get_stream().get_id();
423 if self.has_video && streamno == self.video_str {
424 self.vcontrol.try_send_video(PktSendEvent::Packet(pkt));
425 } else if self.has_audio && streamno == self.audio_str {
426 self.acontrol.try_send_audio(PktSendEvent::Packet(pkt));
427 }
428 }
429 };
430 }
431 self.vcontrol.fill(disp_queue);
432
433 if !try_send {
434 break;
435 }
436 }
437 if self.has_video {
438 while self.vcontrol.get_queue_size() > 0 && !disp_queue.is_full() {
439 self.vcontrol.try_send_queued();
440 self.vcontrol.fill(disp_queue);
441 std::thread::sleep(Duration::from_millis(10));
442 }
443 self.vcontrol.wait_for_frames()?;
444 self.vcontrol.fill(disp_queue);
445 }
446 debug_log!(self; {format!(" prefilling done, frames {}-{} audio {}", disp_queue.start, disp_queue.end, self.acontrol.get_fill())});
447 Ok(())
448 }
449 fn toggle_pause(&mut self) {
450 self.paused = !self.paused;
451 if self.paused {
452 self.vsystem.enable_screen_saver();
453 self.tkeep.set_ts();
454 } else {
455 self.vsystem.disable_screen_saver();
456 self.tkeep.set_time();
457 }
458 if self.paused {
459 self.acontrol.pause();
460 } else {
461 self.acontrol.resume();
462 }
463 }
464 fn handle_events(&mut self, event_pump: &mut sdl2::EventPump, canvas: &mut Canvas<Window>, dmx: &mut Demuxer, disp_queue: &mut DispQueue) -> Result<bool, ()> {
465 for event in event_pump.poll_iter() {
466 if let Event::Quit {..} = event {
467 self.end = true;
468 println!();
469 return Ok(true);
470 }
471 if let Event::Window {win_event: WindowEvent::Exposed, ..} = event {
472 canvas.clear();
473 canvas.copy(disp_queue.get_last_texture(&self.osd), None, None).expect("blitting failure");
474 canvas.present();
475 }
476 if let Event::MouseButtonDown {mouse_btn: MouseButton::Right, ..} = event {
477 self.toggle_pause();
478 }
479 if let Event::KeyDown {keycode: Some(keycode), keymod, ..} = event {
480 match keycode {
481 Keycode::Escape => {
482 self.end = true;
483 println!();
484 return Ok(true);
485 },
486 Keycode::Q if matches!(keymod, Mod::RSHIFTMOD | Mod::LSHIFTMOD) => {
487 self.end = true;
488 println!();
489 return Ok(true);
490 },
491 Keycode::Return => return Ok(true),
492 Keycode::Right => { self.seek(10, true, dmx, disp_queue)?; },
493 Keycode::Left => { self.seek(10, false, dmx, disp_queue)?; },
494 Keycode::Up => { self.seek(60, true, dmx, disp_queue)?; },
495 Keycode::Down => { self.seek(60, false, dmx, disp_queue)?; },
496 Keycode::PageUp => { self.seek(600, true, dmx, disp_queue)?; },
497 Keycode::PageDown => { self.seek(600, false, dmx, disp_queue)?; },
498 Keycode::Space => { self.toggle_pause(); },
499 Keycode::Plus | Keycode::KpPlus => {
500 self.volume = (self.volume + 10).min(MAX_VOLUME);
501 if !self.mute {
502 self.acontrol.set_volume(self.volume);
503 }
504 },
505 Keycode::Minus | Keycode::KpMinus => {
506 self.volume = self.volume.saturating_sub(10);
507 if !self.mute {
508 self.acontrol.set_volume(self.volume);
509 }
510 },
511 Keycode::D => {
512 self.debug = !self.debug;
513 },
514 Keycode::M => {
515 self.mute = !self.mute;
516 if self.mute {
517 self.acontrol.set_volume(0);
518 } else {
519 self.acontrol.set_volume(self.volume);
520 }
521 },
522 Keycode::H => {
523 self.vcontrol.try_send_video(PktSendEvent::HurryUp);
524 },
525 Keycode::O => {
526 self.osd.toggle();
527 },
528 _ => {},
529 };
530 if !self.paused {
531 print!("{:60}\r", ' ');
532 std::io::stdout().flush().unwrap();
533 }
534 }
535 }
536 Ok(false)
537 }
538 fn play(&mut self, name: &str, start_time: NATimePoint) {
539 debug_log!(self; {format!("Playing {}", name)});
540
541 // prepare data source
542 let path = Path::new(name);
543 let mut file = if let Ok(handle) = File::open(path) {
544 handle
545 } else {
546 println!("failed to open {}", name);
547 return;
548 };
549 let dmx_fact;
550 let mut fr = FileReader::new_read(&mut file);
551 let mut br = ByteReader::new(&mut fr);
552 let res = detect::detect_format(name, &mut br);
553 if res.is_none() {
554 println!("cannot detect format for {}", name);
555 return;
556 }
557 let (dmx_name, _score) = res.unwrap();
558 debug_log!(self; {format!(" found demuxer {} with score {:?}", dmx_name, _score)});
559 println!("trying demuxer {} on {}", dmx_name, name);
560
561 let mut dmx_reg = RegisteredDemuxers::new();
562 nihav_register_all_demuxers(&mut dmx_reg);
563 let mut dec_reg = RegisteredDecoders::new();
564 nihav_register_all_decoders(&mut dec_reg);
565 let mut mtdec_reg = RegisteredMTDecoders::new();
566 if self.use_mt {
567 nihav_register_all_mt_decoders(&mut mtdec_reg);
568 }
569
570 let ret = dmx_reg.find_demuxer(dmx_name);
571 if ret.is_none() {
572 println!("error finding {} demuxer", dmx_name);
573 return;
574 }
575 dmx_fact = ret.unwrap();
576 br.seek(SeekFrom::Start(0)).expect("should be able to seek to the start");
577 let ret = create_demuxer(dmx_fact, &mut br);
578 if ret.is_err() {
579 println!("error creating demuxer");
580 return;
581 }
582 let mut dmx = ret.unwrap();
583 if start_time != NATimePoint::None {
584 debug_log!(self; {format!(" start seek to {}", start_time)});
585 if dmx.seek(start_time).is_err() {
586 println!("initial seek failed");
587 }
588 }
589
590 let mut width = 640;
591 let mut height = 480;
592 let mut tb_num = 0;
593 let mut tb_den = 0;
594 let mut ainfo: Option<NAAudioInfo> = None;
595
596 let mut video_dec: Option<DecoderStuff> = None;
597 let mut audio_dec: Option<DecoderStuff> = None;
598
599 let duration = dmx.get_duration();
600 if duration != 0 {
601 println!(" total duration {}", format_time(duration));
602 }
603 self.has_video = false;
604 self.has_audio = false;
605 self.osd.reset();
606 self.osd.set_duration(duration);
607 for i in 0..dmx.get_num_streams() {
608 let s = dmx.get_stream(i).unwrap();
609 let info = s.get_info();
610 let decfunc = dec_reg.find_decoder(info.get_name());
611 let decfunc_mt = mtdec_reg.find_decoder(info.get_name());
612 println!("stream {} - {} {}", i, s, info.get_name());
613 debug_log!(self; {format!(" stream {} - {} {}", i, s, info.get_name())});
614 let str_id = s.get_id();
615 if info.is_video() {
616 if video_dec.is_none() && self.play_video {
617 if let Some(decfunc) = decfunc_mt {
618 let mut dec = (decfunc)();
619 let mut dsupp = Box::new(NADecoderSupport::new());
620 let props = info.get_properties().get_video_info().unwrap();
621 if props.get_width() != 0 {
622 width = props.get_width();
623 height = props.get_height();
624 }
625 if dec.init(&mut dsupp, info.clone(), self.vthreads).is_ok() {
626 video_dec = Some(DecoderStuff{ dsupp, dec: DecoderType::VideoMT(dec, MTFrameReorderer::new()) });
627 self.video_str = str_id;
628 let (tbn, tbd) = s.get_timebase();
629 tb_num = tbn;
630 tb_den = tbd;
631 self.has_video = true;
632 continue;
633 } else {
634 println!("failed to create multi-threaded decoder, falling back");
635 }
636 }
637 if let Some(decfunc) = decfunc {
638 let mut dec = (decfunc)();
639 let mut dsupp = Box::new(NADecoderSupport::new());
640 let props = info.get_properties().get_video_info().unwrap();
641 if props.get_width() != 0 {
642 width = props.get_width();
643 height = props.get_height();
644 }
645 let desc = get_codec_description(info.get_name());
646 let (reorder_depth, reord) = if desc.is_none() || (desc.unwrap().caps & CODEC_CAP_COMPLEX_REORDER) == 0 {
647 let reord: Box<dyn FrameReorderer + Send> = Box::new(IPBReorderer::new());
648 (3, reord)
649 } else {
650 let reord: Box<dyn FrameReorderer + Send> = Box::new(ComplexReorderer::new());
651 (16, reord)
652 };
653 dsupp.pool_u8 = NAVideoBufferPool::new(reorder_depth);
654 dsupp.pool_u16 = NAVideoBufferPool::new(reorder_depth);
655 dsupp.pool_u32 = NAVideoBufferPool::new(reorder_depth);
656 if dec.init(&mut dsupp, info).is_err() {
657 println!("failed to initialise video decoder");
658 return;
659 }
660 video_dec = Some(DecoderStuff{ dsupp, dec: DecoderType::Video(dec, reord) });
661 self.video_str = str_id;
662 let (tbn, tbd) = s.get_timebase();
663 tb_num = tbn;
664 tb_den = tbd;
665 self.has_video = true;
666 } else {
667 println!("no video decoder for {} found!", info.get_name());
668 }
669 }
670 } else if info.is_audio() {
671 if audio_dec.is_none() && self.play_audio {
672 if let Some(decfunc) = decfunc {
673 let mut dec = (decfunc)();
674 let mut dsupp = Box::new(NADecoderSupport::new());
675 ainfo = info.get_properties().get_audio_info();
676 if dec.init(&mut dsupp, info).is_err() {
677 println!("failed to initialise audio decoder");
678 return;
679 }
680 audio_dec = Some(DecoderStuff{ dsupp, dec: DecoderType::Audio(dec) });
681 self.audio_str = str_id;
682 self.has_audio = true;
683 } else {
684 println!("no audio decoder for {} found!", info.get_name());
685 }
686 }
687 } else {
688 println!("decoder {} not found", info.get_name());
689 }
690 }
691 if !self.has_video && !self.has_audio {
692 println!("No playable streams found.");
693 return;
694 }
695
696 while (width <= 384) && (height <= 288) {
697 width <<= 1;
698 height <<= 1;
699 }
700
701 // prepare playback structure
702 let mut new_vcontrol = VideoControl::new(video_dec, width, height, tb_num, tb_den);
703 std::mem::swap(&mut self.vcontrol, &mut new_vcontrol);
704
705 let mut new_acontrol = AudioControl::new(audio_dec, ainfo, &self.asystem);
706 std::mem::swap(&mut self.acontrol, &mut new_acontrol);
707
708 if self.mute {
709 self.acontrol.set_volume(0);
710 } else {
711 self.acontrol.set_volume(self.volume);
712 }
713
714 let fname = path.file_name();
715 let wname = if let Some(fname) = fname {
716 "NihAV player - ".to_owned() + fname.to_str().expect("should be able to set window title")
717 } else {
718 "NihAV player".to_owned()
719 };
720 let mut builder = self.vsystem.window(&wname, width as u32, height as u32);
721 let window = if let (Some(xpos), Some(ypos)) = (self.xpos, self.ypos) {
722 builder.position(xpos, ypos).build().expect("should be able to set window position")
723 } else {
724 builder.position_centered().build().expect("should be able to centre window")
725 };
726 let mut canvas = window.into_canvas().build().expect("should be able to build canvas");
727 let texture_creator = canvas.texture_creator();
728 let mut disp_q = DispQueue::new(&texture_creator, width, height, if self.has_video { FRAME_QUEUE_LEN } else { 0 });
729 if !self.has_video {
730 canvas.clear();
731 canvas.copy(disp_q.get_last_texture(&self.osd), None, None).expect("blit failure");
732 canvas.present();
733 }
734
735 self.has_audio = self.acontrol.has_audio();
736 if !self.has_video && !self.has_audio {
737 println!("No playable streams.");
738 return;
739 }
740
741 // play
742 if self.prefill(&mut dmx, &mut disp_q).is_err() {
743 std::mem::swap(&mut self.vcontrol, &mut new_vcontrol);
744 new_vcontrol.finish();
745 std::mem::swap(&mut self.acontrol, &mut new_acontrol);
746 new_acontrol.finish();
747 return;
748 }
749 self.tkeep.reset_all(if !disp_q.is_empty() { disp_q.first_ts } else { 0 });
750 if !self.paused {
751 self.acontrol.resume();
752 }
753 let mut event_pump = self.sdl_context.event_pump().expect("should be able to create event pump");
754 let mut last_disp = Instant::now();
755 let mut has_data = true;
756 'main: loop {
757 let ret = self.handle_events(&mut event_pump, &mut canvas, &mut dmx, &mut disp_q);
758 if matches!(ret, Ok(true) | Err(_)) {
759 println!();
760 break 'main;
761 }
762 if !self.paused {
763 let mut try_send = self.acontrol.get_queue_size() < FRAME_QUEUE_LEN && self.vcontrol.get_queue_size() < FRAME_QUEUE_LEN;
764 if !self.vcontrol.try_send_queued() && self.vcontrol.is_filled(FRAME_QUEUE_LEN) {
765 try_send = false;
766 }
767 if !self.acontrol.try_send_queued() {
768 try_send = false;
769 }
770 while has_data && try_send {
771 match dmx.get_frame() {
772 Err(DemuxerError::EOF) => {
773 self.vcontrol.try_send_video(PktSendEvent::End);
774 self.acontrol.try_send_audio(PktSendEvent::End);
775 has_data = false;
776 },
777 Err(err) => {
778 println!("demuxer error {:?}", err);
779 if err == DemuxerError::IOError {
780 self.vcontrol.try_send_video(PktSendEvent::End);
781 self.acontrol.try_send_audio(PktSendEvent::End);
782 has_data = false;
783 }
784 },
785 Ok(pkt) => {
786 let streamno = pkt.get_stream().get_id();
787 if self.has_video && streamno == self.video_str {
788 debug_log!(self; {" sending video packet"});
789 self.vcontrol.try_send_video(PktSendEvent::Packet(pkt));
790 if self.vcontrol.is_filled(FRAME_QUEUE_LEN) {
791 try_send = false;
792 }
793 } else if self.has_audio && streamno == self.audio_str {
794 debug_log!(self; {" sending audio packet"});
795 self.acontrol.try_send_audio(PktSendEvent::Packet(pkt));
796 if self.acontrol.get_queue_size() >= FRAME_QUEUE_LEN {
797 try_send = false;
798 }
799 }
800 }
801 };
802 }
803 self.vcontrol.fill(&mut disp_q);
804 let mut sleep_time = 25;
805 debug_log!(self; {format!(" time {}", self.tkeep.get_cur_time())});
806 if self.has_video {
807 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())});
808 let ret = try_display(&mut disp_q, &mut canvas, &mut self.osd, &self.tkeep);
809 if let Some(next_time) = ret {
810 sleep_time = sleep_time.min(next_time);
811 }
812 }
813 if self.has_audio {
814 let time_left = self.acontrol.get_time_left();
815 debug_log!(self; {format!(" audio left {}", time_left)});
816 sleep_time = sleep_time.min(time_left);
817 }
818 debug_log!(self; {format!(" sleep {}ms", sleep_time)});
819 if last_disp.elapsed().as_millis() >= 10 {
820 let c_time = self.tkeep.get_cur_time();
821
822 if !self.debug {
823 print!(" {} {}% \r", format_time(c_time), self.acontrol.get_volume());
824 } else {
825 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());
826 }
827 std::io::stdout().flush().unwrap();
828 last_disp = Instant::now();
829 }
830 let mut end = true;
831 if self.has_video && !self.vcontrol.is_video_end() {
832 end = false;
833 }
834 if self.has_audio && !self.acontrol.is_audio_end() {
835 end = false;
836 }
837 if end {
838 break;
839 }
840 thread::sleep(Duration::from_millis(sleep_time));
841 } else {
842 thread::sleep(Duration::from_millis(20));
843 }
844 }
845 let (xpos, ypos) = canvas.into_window().position();
846 self.xpos = Some(xpos);
847 self.ypos = Some(ypos);
848 println!();
849 std::mem::swap(&mut self.vcontrol, &mut new_vcontrol);
850 new_vcontrol.finish();
851 std::mem::swap(&mut self.acontrol, &mut new_acontrol);
852 new_acontrol.finish();
853 }
854 }
855
856 fn main() {
857 let args: Vec<String> = env::args().collect();
858
859 if args.len() == 1 {
860 println!("usage: nihav-player file1 file2 ...");
861 return;
862 }
863
864 let mut player = Player::new();
865
866 let mut aiter = args.iter().skip(1);
867 let mut seek_time = NATimePoint::None;
868 while let Some(arg) = aiter.next() {
869 match arg.as_str() {
870 "-an" => { player.play_audio = false; },
871 "-ae" => { player.play_audio = true; },
872 "-vn" => { player.play_video = false; },
873 "-ve" => { player.play_video = true; },
874 "-seek" => {
875 if let Some(arg) = aiter.next() {
876 if let Ok(time) = arg.parse::<NATimePoint>() {
877 seek_time = time;
878 } else {
879 println!("wrong seek time");
880 seek_time = NATimePoint::None;
881 }
882 }
883 },
884 "-vol" => {
885 if let Some(arg) = aiter.next() {
886 if let Ok(vol) = arg.parse::<usize>() {
887 player.volume = vol.min(MAX_VOLUME);
888 } else {
889 println!("wrong volume");
890 }
891 }
892 },
893 "-debug" => {
894 player.debug = true;
895 },
896 "-nodebug" => {
897 player.debug = false;
898 },
899 "-mt" => {
900 player.use_mt = true;
901 },
902 "-nomt" => {
903 player.use_mt = false;
904 },
905 "-threads" => {
906 if let Some(arg) = aiter.next() {
907 if let Ok(val) = arg.parse::<usize>() {
908 player.vthreads = val.max(1);
909 } else {
910 println!("wrong number of threads");
911 }
912 }
913 },
914 _ => {
915 player.play(arg, seek_time);
916 if player.end { break; }
917 seek_time = NATimePoint::None;
918 },
919 };
920 }
921 }