npv: tidy mutex locking
[nyanmp.git] / npv / local / code.frag.c
blobe9e3f8cc7474f4f95faf6cf262fa120ec0e8e359
1 /*
2 * block as much as possible.
3 * handle only async "usual" sigs, with sync signalfd.
4 * allow some signals to go thru though. many sync signal can only
5 * be handles with an async handler.
6 * always presume the process "controlling terminal" is different than the
7 * terminal connected on standard input and standard output
8 */
9 STATIC void sigs_init_once(void)
11 int r;
12 sigset_t sset;
14 r = sigfillset(&sset);
15 if (r == -1)
16 fatal("unable to get a full signal mask\n");
18 /* the "controlling terminal" line asks for a core dump, leave it be */
19 r = sigdelset(&sset, SIGQUIT);
20 if (r == -1)
21 fatal("unable to remove SIGQUIT from our signal mask\n");
23 r = pthread_sigmask(SIG_SETMASK, &sset, 0);
24 if (r != 0)
25 fatal("unable to \"block\" \"all\" signals\n");
27 /* from here, we "steal" signals with signalfd */
29 r = sigemptyset(&sset);
30 if (r == -1)
31 fatal("unable to get an empty signal mask\n");
32 /* we are asked nicely to terminate */
33 r = sigaddset(&sset, SIGTERM);
34 if (r == -1)
35 fatal("unable to add SIGTERM to our signal mask\n");
36 /* the "controlling terminal" line (^c) asks nicely to terminate */
37 r = sigaddset(&sset, SIGINT);
38 if (r == -1)
39 fatal("unable to add SIGINT to our signal mask\n");
41 r = signalfd(-1, &sset, SFD_NONBLOCK);
42 if (r == -1)
43 fatal("unable to get a signalfd file descriptor\n");
44 sig_fd_l = r;
46 STATIC void prefill_wait(void) { loop
48 struct timespec wanted;
49 struct timespec rem;
50 s64 prefill_audio;
51 s64 prefill_video;
53 npv_pipeline_limits_lock();
54 prefill_audio = npv_pipeline_limits_p.pkts.prefill.audio_bytes_rem;
55 prefill_video = npv_pipeline_limits_p.pkts.prefill.video_bytes_rem;
56 npv_pipeline_limits_unlock();
57 if (prefill_audio <= 0 && prefill_video <= 0)
58 break;
59 memset(&wanted, 0, sizeof(wanted));
60 memset(&rem, 0, sizeof(rem));
61 wanted.tv_nsec = 100000000; /* 100 ms */
62 loop {
63 int r;
65 /* linux bug: cannot specify CLOCK_MONOTONIC_RAW */
66 r = clock_nanosleep(CLOCK_MONOTONIC, 0, &wanted, &rem);
67 if (r == 0)
68 break;
69 if (r != EINTR)
70 fatal("prefill wait timer failed:%d\n", r);
71 /* r == EINTR */
72 memcpy(&wanted, &rem, sizeof(wanted));
73 memset(&rem, 0, sizeof(rem));
76 STATIC void predecode_wait(void)
78 struct timespec wanted;
79 struct timespec rem;
81 memset(&wanted, 0, sizeof(wanted));
82 memset(&rem, 0, sizeof(rem));
83 /* we target ~double audio buf, namely (0.25s * 2 ~ 500ms) */
84 wanted.tv_nsec = 500000000;
85 loop {
86 int r;
88 /* linux bug: cannot specify CLOCK_MONOTONIC_RAW */
89 r = clock_nanosleep(CLOCK_MONOTONIC, 0, &wanted, &rem);
90 if (r == 0)
91 break;
92 if (r != EINTR)
93 fatal("predecoding wait timer failed:%d\n", r);
94 /* r == EINTR */
95 memcpy(&wanted, &rem, sizeof(wanted));
96 memset(&rem, 0, sizeof(rem));
99 STATIC void evt_init_once(void)
101 ep_fd_p = epoll_create1(0);
102 if (ep_fd_p == -1)
103 fatal("unable to create the epoll file descriptor\n");
105 STATIC void evt_add_all_fds(void)
107 int r;
108 u8 i;
109 struct epoll_event evt;
111 /* signals */
112 evt.events = EPOLLIN;
113 evt.data.fd = sig_fd_l;
114 r = epoll_ctl(ep_fd_p, EPOLL_CTL_ADD, sig_fd_l, &evt);
115 if (r == -1)
116 fatal("unable to add the signalfd file descriptior to the epoll file descriptor\n");
117 /*--------------------------------------------------------------------*/
118 /* the video timer */
119 evt.events = EPOLLIN;
120 evt.data.fd = npv_video_timer_fd_p;
121 r = epoll_ctl(ep_fd_p, EPOLL_CTL_ADD, npv_video_timer_fd_p, &evt);
122 if (r == -1)
123 fatal("unable to add the video timer file descriptor\n");
124 /*--------------------------------------------------------------------*/
125 /* the x11 xcb file descriptor */
126 evt.events = EPOLLIN;
127 evt.data.fd = npv_xcb_p.fd;
128 r = epoll_ctl(ep_fd_p, EPOLL_CTL_ADD, npv_xcb_p.fd, &evt);
129 if (r == -1)
130 fatal("unable to add the x11 xcb file descriptor\n");
131 /*--------------------------------------------------------------------*/
132 /* alsa pcm poll file descriptors */
133 i = 0;
134 loop {
135 if (i == npv_audio_pcm_pollfds_n_p)
136 break;
137 evt.events = npv_audio_pcm_pollfds_p[i].events;
138 evt.data.fd = npv_audio_pcm_pollfds_p[i].fd;
139 r = epoll_ctl(ep_fd_p, EPOLL_CTL_ADD,
140 npv_audio_pcm_pollfds_p[i].fd, &evt);
141 if (r == -1)
142 fatal("unable to add alsa poll file descriptor index %d to epoll file descriptor\n", i);
143 ++i;
145 /*--------------------------------------------------------------------*/
146 /* the draining timer */
147 evt.events = EPOLLIN;
148 evt.data.fd = npv_audio_draining_timer_fd_p;
149 r = epoll_ctl(ep_fd_p, EPOLL_CTL_ADD, npv_audio_draining_timer_fd_p,
150 &evt);
151 if (r == -1)
152 fatal("unable to add the draining timer file descriptor\n");
153 /*--------------------------------------------------------------------*/
154 /* the xcb screensaver heartbeat timer */
155 evt.events = EPOLLIN;
156 evt.data.fd = npv_xcb_p.screensaver_heartbeat_timer_fd;
157 r = epoll_ctl(ep_fd_p, EPOLL_CTL_ADD,
158 npv_xcb_p.screensaver_heartbeat_timer_fd, &evt);
159 if (r == -1)
160 fatal("unable to add the xcb screensaver heartbeat timer file descriptor\n");
161 /*--------------------------------------------------------------------*/
162 /* the xcb mouse visibility timer */
163 evt.events = EPOLLIN;
164 evt.data.fd = npv_xcb_p.mouse_visibility_timer_fd;
165 r = epoll_ctl(ep_fd_p, EPOLL_CTL_ADD,
166 npv_xcb_p.mouse_visibility_timer_fd, &evt);
167 if (r == -1)
168 fatal("unable to add the xcb screensaver heartbeat timer file descriptor\n");
170 STATIC void evt_sigs(void)
172 int r;
173 struct signalfd_siginfo siginfo;
175 /* no short reads */
176 r = read(sig_fd_l, &siginfo, sizeof(siginfo));
177 if (r != sizeof(siginfo))
178 fatal("unable to read signal information\n");
180 switch (siginfo.ssi_signo) {
181 case SIGTERM:
182 exit_ok("received SIGTERM\n");
183 case SIGINT:
184 exit_ok("received SIGINT\n");
185 default:
186 warning("signal handle:unwanted signal %d received, discarding\n", siginfo.ssi_signo);
189 STATIC void evt_accumulate(struct epoll_event *evt, bool *have_evt_sigs,
190 bool *have_evt_pcm, bool *have_evt_video, bool *have_evt_pcm_draining,
191 bool *have_evt_x11, bool *have_evt_xcb_screensaver_heartbeat,
192 bool *have_evt_xcb_mouse_visibility)
194 u8 i;
196 if (evt->data.fd == sig_fd_l) {
197 if ((evt->events & EPOLLIN) != 0) {
198 *have_evt_sigs = true;
199 return;
201 fatal("event loop wait:signal:unexpected event\n");
203 /*-------------------------------------------------------------------*/
204 /* only update alsa fds */
205 i = 0;
206 loop {
207 if (i == npv_audio_pcm_pollfds_n_p)
208 break;
209 if (evt->data.fd == npv_audio_pcm_pollfds_p[i].fd) {
210 npv_audio_pcm_pollfds_p[i].revents = evt->events;
211 *have_evt_pcm = true;
212 return;
214 ++i;
216 /*-------------------------------------------------------------------*/
217 if (evt->data.fd == npv_xcb_p.fd) {
218 if ((evt->events & EPOLLIN) != 0)
219 *have_evt_x11 = true;
220 return;
222 /*-------------------------------------------------------------------*/
223 if (evt->data.fd == npv_video_timer_fd_p) {
224 if ((evt->events & EPOLLIN) != 0) {
225 *have_evt_video = true;
226 return;
228 fatal("event loop wait:video:unexpected event\n");
230 /*-------------------------------------------------------------------*/
231 if (evt->data.fd == npv_audio_draining_timer_fd_p) {
232 if ((evt->events & EPOLLIN) != 0) {
233 *have_evt_pcm_draining = true;
234 return;
236 fatal("event loop wait:audio draining timer:unexpected event\n");
238 /*-------------------------------------------------------------------*/
239 if (evt->data.fd == npv_xcb_p.screensaver_heartbeat_timer_fd) {
240 if ((evt->events & EPOLLIN) != 0) {
241 *have_evt_xcb_screensaver_heartbeat = true;
242 return;
244 fatal("event loop wait:xcb screensaver heartbeat timer:unexpected event\n");
246 /*-------------------------------------------------------------------*/
247 if (evt->data.fd == npv_xcb_p.mouse_visibility_timer_fd) {
248 if ((evt->events & EPOLLIN) != 0) {
249 *have_evt_xcb_mouse_visibility = true;
250 return;
252 fatal("event loop wait:xcb mouse visibility timer:unexpected event\n");
256 * XXX: remember that all heavy lifting should be done in other threads.
257 * this thread should not "block" or perform "expensive" work.
258 * "blocking", "expensive" work should be offloaded to other threads.
260 #define EPOLL_EVTS_N 32 /* why not */
261 STATIC void evts_loop(void)
263 int fds_n;
264 int fd_idx;
265 struct epoll_event evts[EPOLL_EVTS_N];
266 bool have_evt_sigs;
267 bool have_evt_pcm;
268 bool have_evt_video;
269 bool have_evt_pcm_draining;
270 bool have_evt_x11;
271 bool have_evt_xcb_screensaver_heartbeat;
272 bool have_evt_xcb_mouse_visibility;
273 int r;
274 short pcm_evts;
276 errno = 0;
277 memset(evts, 0, sizeof(evts));
278 fds_n = epoll_wait(ep_fd_p, evts, EPOLL_EVTS_N, -1);
279 if (fds_n == -1) {
280 if (errno == EINTR) {
281 warning("event loop wait:was interrupted by a signal\n");
282 return;
284 fatal("event loop wait:an error occured\n");
287 have_evt_sigs = false;
288 have_evt_pcm = false;
289 have_evt_video = false;
290 have_evt_pcm_draining = false;
291 have_evt_x11 = false;
292 have_evt_xcb_screensaver_heartbeat = false;
293 have_evt_xcb_mouse_visibility= false;
295 fd_idx = 0;
296 loop {
297 if (fd_idx == fds_n)
298 break;
299 evt_accumulate(&evts[fd_idx], &have_evt_sigs, &have_evt_pcm,
300 &have_evt_video, &have_evt_pcm_draining,
301 &have_evt_x11, &have_evt_xcb_screensaver_heartbeat,
302 &have_evt_xcb_mouse_visibility);
303 ++fd_idx;
306 /* once we have our evts, we use a sort of priority order */
308 /* process any q-ed and we-handle sigs before anything */
309 if (have_evt_sigs)
310 evt_sigs();
312 * XXX: it may be more appropriate to break this in 2 steps: key inputs
313 * (light processing), wins resizing (heavy processing)
315 /* key input and win resizing */
316 if (have_evt_x11)
317 npv_xcb_evt();
318 /* XXX: once in audio draining mode, this should not really happen */
319 /* we are more sensitive to audio issues than video issues */
320 if (have_evt_pcm) {
322 * since alsa could use several file descriptors, only once the
323 * pollfds were properly updated we can actually know we got
324 * something from alsa
326 r = snd_pcm_poll_descriptors_revents(npv_audio_pcm_p,
327 npv_audio_pcm_pollfds_p, npv_audio_pcm_pollfds_n_p,
328 &pcm_evts);
329 if (r != 0)
330 fatal("alsa:error processing the poll file descriptors\n");
331 if ((pcm_evts & ~POLLOUT) != 0)
332 fatal("alsa:unexpected events\n");
333 if ((pcm_evts & POLLOUT) != 0)
334 npv_audio_evt_pcm_write();
336 if (have_evt_video)
337 npv_video_timer_evt();
338 /* while audio is draining, video fr may need to be displayed */
339 if (have_evt_pcm_draining)
340 npv_audio_draining_state_evt();
341 if (have_evt_xcb_screensaver_heartbeat)
342 npv_xcb_screensaver_heartbeat_timer_evt();
343 if (have_evt_xcb_mouse_visibility)
344 npv_xcb_mouse_visibility_timer_evt();
346 #undef EPOLL_EVTS_N
347 STATIC void ff_log_stdout(void *a, int b, const char *fmt, va_list ap)
349 vprintf(fmt, ap);
351 STATIC void usage(void)
353 pout("\
354 npv [-f send a fullscreen message to the window manager at start] [-p alsa pcm]\n\
355 [-v volume(0..100)] [-h window height in pixels] [-w window width in pixels]\n\
356 [-b packet buffer prefill wait(0..100)] [-help] url\n");
358 STATIC void opts_parse(int argc, u8 **args, u8 **url, bool* start_fullscreen,
359 u16 *w, u16 *h, u8 **pcm_str, double *vol, u8 *pkts_prefill_percent)
361 int i;
362 int url_idx;
364 i = 1;
365 url_idx = -1;
366 loop {
367 if (i == argc)
368 break;
369 if (strcmp("-f", args[i]) == 0) {
370 *start_fullscreen = true;
371 ++i;
372 } else if (strcmp("-p", args[i]) == 0) {
373 *pcm_str = args[i + 1];
374 pout("-p:alsa pcm \"%s\"\n", *pcm_str);
375 i += 2;
376 } else if (strcmp("-v", args[i]) == 0) {
377 unsigned long vol_ul;
379 if ((i + 1) == argc)
380 fatal("-v:initial volume is missing\n");
381 vol_ul = strtoul(args[i + 1], 0, 10);
382 if (vol_ul < 0 || 100 < vol_ul)
383 fatal("-v:invalid volume value %lu (0..100)\n", vol_ul);
384 *vol = (double)vol_ul / 100.;
385 pout("-v:using initial volume %f\n", *vol);
386 i += 2;
387 } else if (strcmp("-h", args[i]) == 0) {
388 unsigned long h_ul;
390 if ((i + 1) == argc)
391 fatal("-h:initial window pixel height is missing\n");
392 h_ul = strtoul(args[i + 1], 0, 10);
393 if (h_ul == 0 || h_ul > U16_MAX)
394 fatal("-h:invalid window pixel height %lu (1..%lu)\n", h_ul, U16_MAX);
395 *h = (u16)h_ul;
396 pout("-h:using initial window height %lu pixels\n", h_ul);
397 i += 2;
398 } else if (strcmp("-w", args[i]) == 0) {
399 unsigned long w_ul;
401 if ((i + 1) == argc)
402 fatal("-h:initial window pixel width is missing\n");
403 w_ul = strtoul(args[i + 1], 0, 10);
404 if (w_ul == 0 || w_ul > U16_MAX)
405 fatal("-w:invalid window pixel width %lu (1..%lu)\n", w_ul, U16_MAX);
406 *w = (u16)w_ul;
407 pout("-w:using initial window width %lu pixels\n", w_ul);
408 i += 2;
409 } else if (strcmp("-b", args[i]) == 0) {
410 if ((i + 1) == argc)
411 fatal("-b:percent value for prefill of buffer of packet queues is missing\n");
412 *pkts_prefill_percent = (u8)strtoul(args[i + 1], 0, 10);
413 pout("-v:using a %u prefilled buffer of packet queues\n", *pkts_prefill_percent);
414 i += 2;
415 } else if (strcmp("-help", args[i]) == 0) {
416 usage();
417 exit(0);
418 } else {
419 url_idx = i;
420 ++i;
423 if (url_idx == -1)
424 fatal("missing url\n");
425 *url = args[url_idx];
426 pout("url-->####%s####\n", *url);
428 #define WIDTH_NOT_DEFINED 0
429 #define HEIGHT_NOT_DEFINED 0
430 STATIC void init_once(u8 *url, bool start_fullscreen, double initial_vol,
431 u16 win_width, u16 win_height, u8 *pcm_str,
432 avcodec_params_t **audio_codec_params,
433 avcodec_params_t **video_codec_params,
434 u8 **faces)
436 avutil_rational_t *audio_st_tb;
437 avutil_rational_t *video_st_tb;
439 evt_init_once();
440 sigs_init_once();
441 npv_vk_init_once(); /* generic plumbing */
442 npv_audio_filt_init_once(initial_vol);
443 /* before audio_st_idx_p is actually used */
444 npv_audio_init_once(pcm_str);
445 npv_video_init_once(); /* before video_st_idx_p is actually used */
446 npv_video_osd_init_once(faces);
447 npv_clk_init_once();
448 npv_pipeline_init_once();
450 npv_fmt_init_once(url);
451 /* we need something to start with */
452 npv_fmt_probe_best_sts(
453 &npv_video_st_p.idx, &npv_video_st_p.id, &video_st_tb,
454 &npv_video_st_p.start_time, video_codec_params,
455 &npv_audio_st_p.idx, &npv_audio_st_p.id, &audio_st_tb,
456 &npv_audio_st_p.start_time, audio_codec_params);
457 memcpy(&npv_audio_st_p.tb, audio_st_tb, sizeof(*audio_st_tb));
458 memcpy(&npv_video_st_p.tb, video_st_tb, sizeof(*video_st_tb));
459 if (win_width == WIDTH_NOT_DEFINED)
460 win_width = (*video_codec_params)->width;
461 if (win_height == HEIGHT_NOT_DEFINED)
462 win_height = (*video_codec_params)->height;
463 npv_xcb_init_once(win_width, win_height, start_fullscreen);
464 npv_vk_surf_init_once(npv_xcb_p.c, npv_xcb_p.win_id);
466 #undef WIDTH_NOT_DEFINED
467 #undef HEIGHT_NOT_DEFINED
468 #define PRINT_INFO true
469 STATIC void prepare(double initial_vol, u8 pkts_prefill_percent,
470 avcodec_params_t *audio_codec_params,
471 avcodec_params_t *video_codec_params)
473 enum avutil_audio_fr_fmt_t dst_fmt;
474 int dst_rate;
475 int dst_chans_n;
476 uint64_t dst_chans_layout;
478 npv_pipeline_limits_reset();
479 npv_pipeline_prefill_reset(pkts_prefill_percent);
481 npv_audio_dec_ctx_cfg(audio_codec_params);
482 npv_video_dec_ctx_cfg(video_codec_params);
484 * do our best to match the pcm cfg to audio ff dec output, BUT we don't
485 * expect to match it "exactly": see right below why
487 npv_audio_pcm_cfg(npv_audio_pcm_p, npv_audio_dec_ctx_p->chans_n,
488 npv_audio_dec_ctx_p->fr_rate, npv_audio_dec_ctx_p->fr_fmt);
489 /* use a ff filt to fill in the gap between the pcm and audio ff dec */
490 npv_audio_pcm2ff(npv_audio_pcm_p, &dst_fmt, &dst_rate, &dst_chans_n,
491 &dst_chans_layout, PRINT_INFO);
492 npv_audio_filt_cfg(
493 npv_audio_dec_ctx_p->fr_fmt, npv_audio_dec_ctx_p->fr_rate,
494 npv_audio_dec_ctx_p->chans_n, npv_audio_dec_ctx_p->chans_layout,
495 false, initial_vol,
496 dst_fmt, dst_rate, dst_chans_n, dst_chans_layout,
497 PRINT_INFO);
498 npv_audio_pcm_silence_bufs_cfg(npv_audio_pcm_p, PRINT_INFO);
500 evt_add_all_fds();
502 STATIC void npv_cmd_quit(void)
504 exit_ok("quit command received\n");
506 STATIC void seek_lock(void)
508 /* see lock_hierarchy file */
509 npv_pkt_q_lock(npv_video_pkt_q_p);
510 npv_video_dec_ctx_lock();
511 npv_video_dec_frs_lock();
512 npv_pkt_q_lock(npv_audio_pkt_q_p);
513 npv_audio_dec_ctx_lock();
514 npv_audio_dec_sets_lock();
515 npv_pipeline_limits_lock();
516 npv_fmt_ctx_lock();
518 STATIC void seek_unlock(void)
520 /* see lock_hierarchy file */
521 npv_fmt_ctx_unlock();
522 npv_pipeline_limits_unlock();
523 npv_audio_dec_sets_unlock();
524 npv_audio_dec_ctx_unlock();
525 npv_pkt_q_unlock(npv_audio_pkt_q_p);
526 npv_video_dec_frs_unlock();
527 npv_video_dec_ctx_unlock();
528 npv_pkt_q_unlock(npv_video_pkt_q_p);
530 #define TS_FROM_CLK_OK 0
531 STATIC void seek_x(s64 delta)
533 int a;
534 u8 r;
535 s64 new_audio_ts;
536 s64 new_video_ts;
537 s64 audio_now;
539 if (npv_audio_draining_p) {
540 warning("seek:audio is draining, seeking disable\n");
541 return;
543 if (paused_p) /* we don't seek in pause */
544 return;
546 npv_thdsws_wait_for_idle(npv_video_scaler_p.ctx);
548 seek_lock();
550 r = npv_clk_get_audio_st_ts(&audio_now);
551 if (r != TS_FROM_CLK_OK) {
552 warning("seek:audio:clock timestamp unavailable, ignoring command\n");
553 seek_unlock();
554 return;
556 (void)snd_pcm_drop(npv_audio_pcm_p);
558 * XXX: a set of sts can share the same id for seeking. if they share
559 * the same id then the tbs should be the same.
561 /*--------------------------------------------------------------------*/
562 new_audio_ts = audio_now + delta * npv_audio_st_p.tb.den
563 / npv_audio_st_p.tb.num;
564 /* rewind capping if possible */
565 if (npv_audio_st_p.start_time != AV_NOPTS_VALUE)
566 if (new_audio_ts < npv_audio_st_p.start_time)
567 new_audio_ts = npv_audio_st_p.start_time;
568 pout("trying to seek to %"PRId64" audio stream time base units\n", new_audio_ts);
569 a = avformat_seek_pkt(npv_fmt_ctx_p, npv_audio_st_p.id, new_audio_ts,
571 if (a < 0) {
572 pout("unable to seek to %"PRId64" audio stream time base units\n", new_audio_ts);
573 goto try_restore_audio;
575 pout("global seek using audio seek to %"PRId64" audio stream time base units\n", new_audio_ts);
576 flush:
577 npv_video_dec_flush();
578 npv_audio_dec_flush();
579 npv_audio_filt_flush();
580 npv_fmt_flush();
581 npv_clk_invalidate();
582 npv_pipeline_limits_reset();
583 npv_pipeline_prefill_reset(npv_pipeline_limits_p.pkts.prefill.percent);
584 seek_unlock();
586 pout("prefilling audio and video buffers...");
587 prefill_wait(); /* use the lock on the pipeline limits */
588 pout("done\n");
589 pout("predecoding audio and video...");
590 predecode_wait(); /* use the lock on the pipeline limits */
591 pout("done\n");
592 (void)snd_pcm_prepare(npv_audio_pcm_p);
593 return;
595 try_restore_audio:
596 a = avformat_seek_pkt(npv_fmt_ctx_p, npv_audio_st_p.id, audio_now, 0);
597 if (a < 0) /* we don't send an application error */
598 exit_ok("unable to restore audio to %"PRId64" audio stream time base units\n", audio_now);
599 goto flush;
601 #undef TS_FROM_CLK_OK
602 STATIC void npv_cmd_rewind(void)
604 pout("COMMAND:rewind\n");
605 seek_x(-SEEK_DELTA);
608 STATIC void npv_cmd_rewind_big(void)
610 pout("COMMAND:rewind big\n");
611 seek_x(-SEEK_DELTA_BIG);
614 STATIC void npv_cmd_fastforward(void)
616 pout("COMMAND:fastforward\n");
617 seek_x(SEEK_DELTA);
620 STATIC void npv_cmd_fastforward_big(void)
622 pout("COMMAND:fastforward big\n");
623 seek_x(SEEK_DELTA_BIG);
626 STATIC void npv_cmd_pause(void)
628 if (npv_audio_draining_p) {
629 warning("pause:audio is draining, toggling pause is disable\n");
630 return;
632 if (paused_p) {
633 int r;
635 pout("COMMAND:unpause\n");
636 paused_p = false;
637 npv_fmt_ctx_lock();
638 avformat_read_play(npv_fmt_ctx_p);
639 npv_fmt_ctx_unlock();
640 npv_clk_unpause();
641 npv_video_timer_start();
642 } else {
643 int r;
645 pout("COMMAND:pause\n");
646 paused_p = true;
647 npv_fmt_ctx_lock();
648 avformat_read_pause(npv_fmt_ctx_p);
649 npv_fmt_ctx_unlock();
650 npv_clk_pause();
651 npv_video_timer_slow_start();
654 STATIC void npv_cmd_vol_up(void)
656 npv_audio_filt_cmd_vol_up();
658 STATIC void npv_cmd_vol_down(void)
660 npv_audio_filt_cmd_vol_down();
662 STATIC void npv_cmd_mute(void)
664 npv_audio_filt_cmd_mute();
666 #define WIDTH_NOT_DEFINED 0
667 #define HEIGHT_NOT_DEFINED 0
668 int main(int argc, u8 **args)
670 bool start_fullscreen;
671 u16 win_width;
672 u16 win_height;
673 u8 *pcm_str;
674 u8 *url;
675 double initial_vol;
676 avcodec_params_t *audio_codec_params;
677 avcodec_params_t *video_codec_params;
678 /* "turn on utf8" processing in used libs if any *AND* locale system */
679 setlocale(LC_ALL, "");
680 /* av_log_set_level(AV_LOG_VERBOSE); */
681 /* av_log_set_level(AV_LOG_DEBUG); */
682 start_fullscreen = false;
683 win_width = WIDTH_NOT_DEFINED;
684 win_height = HEIGHT_NOT_DEFINED;
685 url = 0;
686 pcm_str = "default";
687 url = 0;
688 initial_vol = 1.;
689 npv_pipeline_limits_p.pkts.prefill.percent = 0;
690 opts_parse(argc, args, &url, &start_fullscreen, &win_width, &win_height,
691 &pcm_str, &initial_vol,
692 &npv_pipeline_limits_p.pkts.prefill.percent);
693 init_once(url, start_fullscreen, initial_vol, win_width, win_height,
694 pcm_str, &audio_codec_params, &video_codec_params, npv_faces);
695 prepare(initial_vol, npv_pipeline_limits_p.pkts.prefill.percent,
696 audio_codec_params, video_codec_params);
698 /* switch the ffmpeg log to stdout for metadata/etc dump */
699 avutil_log_set_callback(ff_log_stdout);
700 avformat_dump_fmt(npv_fmt_ctx_p, 0, url, 0);
701 avutil_log_set_callback(avutil_log_default_callback);
703 npv_pipeline_read_thd_start();
704 pout("prefilling audio and video buffers...");
705 prefill_wait();
706 pout("done\n");
707 npv_pipeline_audio_thd_start();
708 npv_pipeline_video_thd_start();
709 pout("predecoding audio and video...");
710 predecode_wait();
711 pout("done\n");
712 npv_video_timer_start();
713 npv_xcb_screensaver_heartbeat_timer_start();
715 loop evts_loop();
716 /* unreachable */
718 #undef WIDTH_NOT_DEFINED
719 #undef HEIGHT_NOT_DEFINED