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
9 STATIC
void sigs_init_once(void)
14 r
= sigfillset(&sset
);
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
);
21 fatal("unable to remove SIGQUIT from our signal mask\n");
23 r
= pthread_sigmask(SIG_SETMASK
, &sset
, 0);
25 fatal("unable to \"block\" \"all\" signals\n");
27 /* from here, we "steal" signals with signalfd */
29 r
= sigemptyset(&sset
);
31 fatal("unable to get an empty signal mask\n");
32 /* we are asked nicely to terminate */
33 r
= sigaddset(&sset
, SIGTERM
);
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
);
39 fatal("unable to add SIGINT to our signal mask\n");
41 r
= signalfd(-1, &sset
, SFD_NONBLOCK
);
43 fatal("unable to get a signalfd file descriptor\n");
46 /* we need a timer for the pre[fill|decode] state */
47 STATIC
void pre_x_init_once(void)
50 pre_x_timer_fd_p
= timerfd_create(CLOCK_MONOTONIC
, TFD_NONBLOCK
);
51 if (pre_x_timer_fd_p
== -1)
52 fatal("unable to get a timer file descriptor for the pre[fill|decode] states:%s\n", strerror(errno
));
54 STATIC
void evt_init_once(void)
56 ep_fd_p
= epoll_create1(0);
58 fatal("unable to create the epoll file descriptor\n");
60 STATIC
void evt_add_all_fds(void)
64 struct epoll_event evt
;
65 /*--------------------------------------------------------------------*/
68 evt
.data
.fd
= sig_fd_l
;
69 r
= epoll_ctl(ep_fd_p
, EPOLL_CTL_ADD
, sig_fd_l
, &evt
);
71 fatal("unable to add the signalfd file descriptior to the epoll file descriptor\n");
72 /*--------------------------------------------------------------------*/
73 /* pre[fill|decode] timer */
75 evt
.data
.fd
= pre_x_timer_fd_p
;
76 r
= epoll_ctl(ep_fd_p
, EPOLL_CTL_ADD
, pre_x_timer_fd_p
, &evt
);
78 fatal("unable to add the pre[fill|decode] timer file descriptor\n");
79 /*--------------------------------------------------------------------*/
82 evt
.data
.fd
= npv_video_timer_fd_p
;
83 r
= epoll_ctl(ep_fd_p
, EPOLL_CTL_ADD
, npv_video_timer_fd_p
, &evt
);
85 fatal("unable to add the video timer file descriptor\n");
86 /*--------------------------------------------------------------------*/
87 /* the x11 xcb file descriptor */
89 evt
.data
.fd
= npv_xcb_p
.fd
;
90 r
= epoll_ctl(ep_fd_p
, EPOLL_CTL_ADD
, npv_xcb_p
.fd
, &evt
);
92 fatal("unable to add the x11 xcb file descriptor\n");
93 /*--------------------------------------------------------------------*/
94 /* alsa pcm poll file descriptors */
97 if (i
== npv_audio_pcm_pollfds_n_p
)
99 evt
.events
= npv_audio_pcm_pollfds_p
[i
].events
;
100 evt
.data
.fd
= npv_audio_pcm_pollfds_p
[i
].fd
;
101 r
= epoll_ctl(ep_fd_p
, EPOLL_CTL_ADD
,
102 npv_audio_pcm_pollfds_p
[i
].fd
, &evt
);
104 fatal("unable to add alsa poll file descriptor index %d to epoll file descriptor\n", i
);
107 /*--------------------------------------------------------------------*/
108 /* the draining timer */
109 evt
.events
= EPOLLIN
;
110 evt
.data
.fd
= npv_audio_draining_timer_fd_p
;
111 r
= epoll_ctl(ep_fd_p
, EPOLL_CTL_ADD
, npv_audio_draining_timer_fd_p
,
114 fatal("unable to add the draining timer file descriptor\n");
115 /*--------------------------------------------------------------------*/
116 /* the xcb screensaver heartbeat timer */
117 evt
.events
= EPOLLIN
;
118 evt
.data
.fd
= npv_xcb_p
.screensaver_heartbeat_timer_fd
;
119 r
= epoll_ctl(ep_fd_p
, EPOLL_CTL_ADD
,
120 npv_xcb_p
.screensaver_heartbeat_timer_fd
, &evt
);
122 fatal("unable to add the xcb screensaver heartbeat timer file descriptor\n");
123 /*--------------------------------------------------------------------*/
124 /* the xcb mouse visibility timer */
125 evt
.events
= EPOLLIN
;
126 evt
.data
.fd
= npv_xcb_p
.mouse_visibility_timer_fd
;
127 r
= epoll_ctl(ep_fd_p
, EPOLL_CTL_ADD
,
128 npv_xcb_p
.mouse_visibility_timer_fd
, &evt
);
130 fatal("unable to add the xcb screensaver heartbeat timer file descriptor\n");
132 STATIC
void evt_sigs(void)
135 struct signalfd_siginfo siginfo
;
138 r
= read(sig_fd_l
, &siginfo
, sizeof(siginfo
));
139 if (r
!= sizeof(siginfo
))
140 fatal("unable to read signal information\n");
142 switch (siginfo
.ssi_signo
) {
144 exit_ok("received SIGTERM\n");
146 exit_ok("received SIGINT\n");
148 warning("signal handle:unwanted signal %d received, discarding\n", siginfo
.ssi_signo
);
151 STATIC
void evt_accumulate(struct epoll_event
*evt
, bool *have_evt_sigs
,
152 bool *have_evt_pcm
, bool *have_evt_video
, bool *have_evt_pcm_draining
,
153 bool *have_evt_x11
, bool *have_evt_xcb_screensaver_heartbeat
,
154 bool *have_evt_xcb_mouse_visibility
, bool *have_evt_pre_x
)
158 if (evt
->data
.fd
== sig_fd_l
) {
159 if ((evt
->events
& EPOLLIN
) != 0) {
160 *have_evt_sigs
= true;
163 fatal("event loop wait:signal:unexpected event\n");
165 /*-------------------------------------------------------------------*/
166 /* only update alsa fds */
169 if (i
== npv_audio_pcm_pollfds_n_p
)
171 if (evt
->data
.fd
== npv_audio_pcm_pollfds_p
[i
].fd
) {
172 npv_audio_pcm_pollfds_p
[i
].revents
= evt
->events
;
173 *have_evt_pcm
= true;
178 /*-------------------------------------------------------------------*/
179 if (evt
->data
.fd
== npv_xcb_p
.fd
) {
180 if ((evt
->events
& EPOLLIN
) != 0) {
181 *have_evt_x11
= true;
185 /*-------------------------------------------------------------------*/
186 if (evt
->data
.fd
== npv_video_timer_fd_p
) {
187 if ((evt
->events
& EPOLLIN
) != 0) {
188 *have_evt_video
= true;
191 fatal("event loop wait:video:unexpected event\n");
193 /*-------------------------------------------------------------------*/
194 if (evt
->data
.fd
== npv_audio_draining_timer_fd_p
) {
195 if ((evt
->events
& EPOLLIN
) != 0) {
196 *have_evt_pcm_draining
= true;
199 fatal("event loop wait:audio draining timer:unexpected event\n");
201 /*-------------------------------------------------------------------*/
202 if (evt
->data
.fd
== npv_xcb_p
.screensaver_heartbeat_timer_fd
) {
203 if ((evt
->events
& EPOLLIN
) != 0) {
204 *have_evt_xcb_screensaver_heartbeat
= true;
207 fatal("event loop wait:xcb screensaver heartbeat timer:unexpected event\n");
209 /*-------------------------------------------------------------------*/
210 if (evt
->data
.fd
== npv_xcb_p
.mouse_visibility_timer_fd
) {
211 if ((evt
->events
& EPOLLIN
) != 0) {
212 *have_evt_xcb_mouse_visibility
= true;
215 fatal("event loop wait:xcb mouse visibility timer:unexpected event\n");
217 /*-------------------------------------------------------------------*/
218 if (evt
->data
.fd
== pre_x_timer_fd_p
) {
219 if ((evt
->events
& EPOLLIN
) != 0) {
220 *have_evt_pre_x
= true;
223 fatal("event loop wait:pre[fill|decode] timer:unexpected event\n");
227 * do _not_ block decoding based on a hard limit of video/audio data
228 * availability, use a soft timer
230 STATIC
void predecode_wait_start(void)
235 /* we target ~double audio buf, namely (0.25s * 2 ~ 500ms) */
236 memset(&t
, 0, sizeof(t
));
237 t
.it_value
.tv_nsec
= 500000000; /* in ns */
238 r
= timerfd_settime(pre_x_timer_fd_p
, 0, &t
, 0);
240 fatal("unable to arm the predecode timer to 500 ms\n");
243 STATIC
void prefill_chk(void)
249 npv_pipeline_limits_lock();
250 prefill
= npv_pipeline_limits_p
.pkts
.prefill
.bytes_rem
;
251 npv_pipeline_limits_unlock();
253 /* prefill is done, switch to predecode */
254 atomic_store(&pre_x_p
, PREDECODE
);
255 predecode_wait_start();
258 /* arm the timer for 100 ms */
259 memset(&t
, 0, sizeof(t
));
260 t
.it_value
.tv_nsec
= 100000000; /* in ns */
261 r
= timerfd_settime(pre_x_timer_fd_p
, 0, &t
, 0);
263 fatal("unable to arm the prefill timer to 100 ms\n");
269 STATIC
void pre_x_evt(void)
277 r
= read(pre_x_timer_fd_p
, &exps_n
, sizeof(exps_n
));
279 warning("unable to read the number of the pre[fill|decode] timer expirations\n");
281 pre_x
= atomic_load(&pre_x_p
);
282 if (pre_x
== PREFILL
) {
283 prefill_chk_do_p
= true;
285 } else if (pre_x
== PREDECODE
) {
286 /* predecode is done, switch to running/paused, namel no pre_x */
287 atomic_store(&pre_x_p
, NONE
);
290 warning("got an spurious pre[fill|decode] timer event\n");
296 * XXX: remember that all heavy lifting should be done in other threads.
297 * this thread should not "block" or perform "expensive" work.
298 * "blocking", "expensive" work should be offloaded to other threads.
300 #define EPOLL_EVTS_N 32 /* why not */
304 STATIC
void evts_loop(void) { loop
309 struct epoll_event evts
[EPOLL_EVTS_N
];
313 bool have_evt_pcm_draining
;
315 bool have_evt_xcb_screensaver_heartbeat
;
316 bool have_evt_xcb_mouse_visibility
;
321 pre_x
= atomic_load(&pre_x_p
);
322 if (pre_x
== PREFILL
&& prefill_chk_do_p
) {
324 prefill_chk_do_p
= false;
327 memset(evts
, 0, sizeof(evts
));
328 fds_n
= epoll_wait(ep_fd_p
, evts
, EPOLL_EVTS_N
, -1);
330 if (errno
== EINTR
) {
331 warning("event loop wait:was interrupted by a signal\n");
334 fatal("event loop wait:an error occured\n");
337 have_evt_sigs
= false;
338 have_evt_pcm
= false;
339 have_evt_video
= false;
340 have_evt_pcm_draining
= false;
341 have_evt_x11
= false;
342 have_evt_xcb_screensaver_heartbeat
= false;
343 have_evt_xcb_mouse_visibility
= false;
344 have_evt_pre_x
= false;
350 evt_accumulate(&evts
[fd_idx
], &have_evt_sigs
, &have_evt_pcm
,
351 &have_evt_video
, &have_evt_pcm_draining
,
352 &have_evt_x11
, &have_evt_xcb_screensaver_heartbeat
,
353 &have_evt_xcb_mouse_visibility
, &have_evt_pre_x
);
357 /* once we have our evts, we use a sort of priority order */
359 /* process any q-ed and we-handle sigs before anything */
363 * XXX: it may be more appropriate to break this in 2 steps: key inputs
364 * (light processing), wins resizing (heavy processing)
366 /* key input and win resizing */
369 /* pre[fill|decode] timer evt */
372 /* XXX: once in audio draining mode, this should not really happen */
373 /* we are more sensitive to audio issues than video issues */
376 * since alsa could use several file descriptors, only once the
377 * pollfds were properly updated we can actually know we got
378 * something from alsa
380 r
= snd_pcm_poll_descriptors_revents(npv_audio_pcm_p
,
381 npv_audio_pcm_pollfds_p
, npv_audio_pcm_pollfds_n_p
,
384 fatal("alsa:error processing the poll file descriptors\n");
385 /* XXX: you can get POLLERR if in XRUN, will be handled later */
386 if ((pcm_evts
& POLLOUT
) != 0)
387 npv_audio_evt_pcm_write();
390 npv_video_timer_evt();
391 /* while audio is draining, video fr may need to be displayed */
392 if (have_evt_pcm_draining
)
393 npv_audio_draining_state_evt();
394 if (have_evt_xcb_screensaver_heartbeat
)
395 npv_xcb_screensaver_heartbeat_timer_evt();
396 if (have_evt_xcb_mouse_visibility
)
397 npv_xcb_mouse_visibility_timer_evt();
403 STATIC
void ff_log_stdout(void *a
, int b
, const char *fmt
, va_list ap
)
407 struct ff_supported_audio_fr_fmt_t
{
409 enum avutil_audio_fr_fmt_t audio_fr_fmt
;
411 /* this is the intersection of ff audio fr fmt and alsa pcm fmt */
412 STATIC
struct ff_supported_audio_fr_fmt_t ff_supported_audio_fr_fmts
[] = {
413 {"u8", AVUTIL_AUDIO_FR_FMT_U8
},
414 {"u8planar", AVUTIL_AUDIO_FR_FMT_U8P
},
415 {"s16", AVUTIL_AUDIO_FR_FMT_S16
},
416 {"s16planar", AVUTIL_AUDIO_FR_FMT_S16P
},
417 {"s32", AVUTIL_AUDIO_FR_FMT_S32
},
418 {"s32planar", AVUTIL_AUDIO_FR_FMT_S32P
},
419 {"float32", AVUTIL_AUDIO_FR_FMT_FLT
},
420 {"float32planar", AVUTIL_AUDIO_FR_FMT_FLTP
},
421 {"float64", AVUTIL_AUDIO_FR_FMT_DBL
},
422 {"float64planar", AVUTIL_AUDIO_FR_FMT_DBLP
},
425 STATIC
void usage(void)
427 struct ff_supported_audio_fr_fmt_t
*fmt
;
430 npv [-f send a fullscreen message to the window manager at start]\n\
431 [-l live mode (seek/pause are disabled)]\n\
433 [-fc override initial ffmpeg count of channels used to approximate the alsa\n\
434 pcm configuration]\n\
435 [-fr override initial ffmpeg rate(hz) used to approximate the alsa pcm\n\
437 [-ff override initial ffmpeg audio frame format used to approximate the alsa\n\
438 pcm configuration, see below for a list]\n\
439 [-v volume(0..100)]\n\
440 [-h window height in pixels]\n\
441 [-w window width in pixels]\n\
442 [-b packet buffer prefill wait(0..100)]\n\
446 the ffmpeg audio frame formats which intersect alsa pcm audio formats are:\n"
448 fmt
= ff_supported_audio_fr_fmts
;
452 pout("\t%s\n", fmt
->str
);
456 STATIC
void opts_parse(int argc
, u8
**args
, u8
**url
, bool *live
,
457 bool* start_fullscreen
, u16
*w
, u16
*h
, u8
**pcm_str
,
458 int *override_initial_ff_chans_n
, int *override_initial_ff_rate
,
459 enum avutil_audio_fr_fmt_t
*override_initial_ff_audio_fr_fmt
,
460 double *vol
, u8
*pkts_prefill_percent
)
470 if (strcmp("-f", args
[i
]) == 0) {
471 *start_fullscreen
= true;
473 } else if (strcmp("-l", args
[i
]) == 0) {
476 } else if (strcmp("-v", args
[i
]) == 0) {
477 unsigned long vol_ul
;
479 if ((i
+ 1) == argc
) {
480 perr("-v:initial volume is missing\n");
484 vol_ul
= strtoul(args
[i
+ 1], 0, 10);
485 if (vol_ul
< 0 || 100 < vol_ul
)
486 fatal("-v:invalid volume value %lu (0..100)\n", vol_ul
);
487 *vol
= (double)vol_ul
/ 100.;
488 pout("-v:using initial volume %f\n", *vol
);
490 } else if (strcmp("-h", args
[i
]) == 0) {
493 if ((i
+ 1) == argc
) {
494 perr("-h:initial window pixel height is missing\n");
498 h_ul
= strtoul(args
[i
+ 1], 0, 10);
499 if (h_ul
== 0 || h_ul
> U16_MAX
)
500 fatal("-h:invalid window pixel height %lu (1..%lu)\n", h_ul
, U16_MAX
);
502 pout("-h:using initial window height %lu pixels\n", h_ul
);
504 } else if (strcmp("-w", args
[i
]) == 0) {
507 if ((i
+ 1) == argc
) {
508 perr("-h:initial window pixel width is missing\n");
512 w_ul
= strtoul(args
[i
+ 1], 0, 10);
513 if (w_ul
== 0 || w_ul
> U16_MAX
)
514 fatal("-w:invalid window pixel width %lu (1..%lu)\n", w_ul
, U16_MAX
);
516 pout("-w:using initial window width %lu pixels\n", w_ul
);
518 } else if (strcmp("-b", args
[i
]) == 0) {
519 if ((i
+ 1) == argc
) {
520 perr("-b:percent value for prefill of buffer of packet queues is missing\n");
524 *pkts_prefill_percent
= (u8
)strtoul(args
[i
+ 1], 0, 10);
525 pout("-v:using a %u prefilled buffer of packet queues\n", *pkts_prefill_percent
);
527 } else if (strcmp("-p", args
[i
]) == 0) {
528 *pcm_str
= args
[i
+ 1];
529 pout("-p:alsa pcm \"%s\"\n", *pcm_str
);
531 /*------------------------------------------------------------*/
532 /* ff initial override for alsa pcm cfg -- start */
533 } else if (strcmp("-fr", args
[i
]) == 0) {
534 if ((i
+ 1) == argc
) {
535 perr("-fr:override initial ffmpeg rate(hz) is missing\n");
539 *override_initial_ff_rate
= (int)strtol(args
[i
+ 1], 0,
541 pout("-fr:override initial ffmpeg audio rate to %dHz used for alsa pcm configuration\n", *override_initial_ff_rate
);
543 } else if (strcmp("-fc", args
[i
]) == 0) {
544 if ((i
+ 1) == argc
) {
545 perr("-fc:override initial ffmpeg channel count is missing\n");
549 *override_initial_ff_chans_n
= (int)strtol(args
[i
+ 1],
551 pout("-fc:override initial ffmpeg count of channels to %d used for alsa pcm configuration\n", *override_initial_ff_chans_n
);
553 } else if (strcmp("-ff", args
[i
]) == 0) {
554 struct ff_supported_audio_fr_fmt_t
*fmt
;
556 if ((i
+ 1) == argc
) {
557 perr("-fc:override initial ffmpeg audio frame format is missing\n");
561 fmt
= ff_supported_audio_fr_fmts
;
564 perr("-ff:unknown ffmpeg audio frame format\n");
568 if (strcmp(fmt
->str
, args
[i
+ 1]) == 0) {
569 *override_initial_ff_audio_fr_fmt
=
575 pout("-ff:override initial ffmpeg audio frame format is %s\n", avutil_get_audio_fr_fmt_name(fmt
->audio_fr_fmt
));
577 /* ff initial override for alsa pcm cfg -- end */
578 /*------------------------------------------------------------*/
579 } else if (strcmp("-help", args
[i
]) == 0) {
588 perr("missing url\n");
592 *url
= args
[url_idx
];
593 pout("url-->####%s####\n", *url
);
596 STATIC
void states_init_once(void)
599 * this state is driving several threads, we start in PREFILL asking for
600 * a prefill chk (it is possible to be in PREFILL state without asking
601 * for a prefill chk).
603 atomic_store(&pre_x_p
, PREFILL
);
604 prefill_chk_do_p
= true;
605 /* the following is accessed only from the main thread */
609 #define WIDTH_NOT_DEFINED 0
610 #define HEIGHT_NOT_DEFINED 0
611 STATIC
void init_once(u8
*url
, bool live
, bool start_fullscreen
,
612 double initial_vol
, u16 win_width
, u16 win_height
, u8
*pcm_str
,
613 avcodec_params_t
**audio_codec_params
,
614 avcodec_params_t
**video_codec_params
,
617 avutil_rational_t
*audio_st_tb
;
618 avutil_rational_t
*video_st_tb
;
625 npv_vk_init_once(); /* generic plumbing */
626 npv_audio_filt_init_once(initial_vol
);
627 /* before audio_st_idx_p is actually used */
628 npv_audio_init_once(pcm_str
);
629 npv_video_init_once(); /* before video_st_idx_p is actually used */
630 npv_video_osd_init_once(faces
);
632 npv_pipeline_init_once();
634 npv_fmt_init_once(url
);
635 /* we need something to start with */
636 npv_fmt_probe_best_sts(
637 &npv_video_st_p
.idx
, &npv_video_st_p
.id
, &video_st_tb
,
638 &npv_video_st_p
.start_time
, video_codec_params
,
639 &npv_audio_st_p
.idx
, &npv_audio_st_p
.id
, &audio_st_tb
,
640 &npv_audio_st_p
.start_time
, audio_codec_params
);
641 memcpy(&npv_audio_st_p
.tb
, audio_st_tb
, sizeof(*audio_st_tb
));
642 memcpy(&npv_video_st_p
.tb
, video_st_tb
, sizeof(*video_st_tb
));
643 if (win_width
== WIDTH_NOT_DEFINED
)
644 win_width
= (*video_codec_params
)->width
;
645 if (win_height
== HEIGHT_NOT_DEFINED
)
646 win_height
= (*video_codec_params
)->height
;
647 npv_xcb_init_once(win_width
, win_height
, start_fullscreen
);
648 npv_vk_surf_init_once(npv_xcb_p
.c
, npv_xcb_p
.win_id
);
650 #undef WIDTH_NOT_DEFINED
651 #undef HEIGHT_NOT_DEFINED
652 STATIC
void prepare(double initial_vol
, int override_initial_ff_chans_n
,
653 int override_initial_ff_rate
,
654 enum avutil_audio_fr_fmt_t override_initial_ff_fmt
,
655 u8 pkts_prefill_percent
, avcodec_params_t
*audio_codec_params
,
656 avcodec_params_t
*video_codec_params
)
658 npv_pipeline_limits_reset();
659 npv_pipeline_prefill_reset(pkts_prefill_percent
);
660 npv_audio_dec_ctx_cfg(audio_codec_params
);
661 npv_video_dec_ctx_cfg(video_codec_params
);
662 npv_audio_prepare(override_initial_ff_chans_n
, override_initial_ff_rate
,
663 override_initial_ff_fmt
);
666 STATIC
void npv_cmd_quit(void)
668 exit_ok("quit command received\n");
670 STATIC
void seek_lock(void)
672 /* see lock_hierarchy file */
673 npv_pkt_q_lock(npv_video_pkt_q_p
);
674 npv_video_dec_ctx_lock();
675 npv_video_dec_frs_lock();
676 npv_pkt_q_lock(npv_audio_pkt_q_p
);
677 npv_audio_dec_ctx_lock();
678 npv_audio_dec_sets_lock();
679 npv_pipeline_limits_lock();
682 STATIC
void seek_unlock(void)
684 /* see lock_hierarchy file */
685 npv_fmt_ctx_unlock();
686 npv_pipeline_limits_unlock();
687 npv_audio_dec_sets_unlock();
688 npv_audio_dec_ctx_unlock();
689 npv_pkt_q_unlock(npv_audio_pkt_q_p
);
690 npv_video_dec_frs_unlock();
691 npv_video_dec_ctx_unlock();
692 npv_pkt_q_unlock(npv_video_pkt_q_p
);
694 #define TS_FROM_CLK_OK 0
696 STATIC
void seek_x(s64 delta
)
700 s64 now_audio_filt_ts
;
701 avutil_rational_t audio_filt_tb
;
702 s64 new_audio_filt_ts
;
706 if (npv_audio_draining_p
) {
707 warning("seek:audio is draining, seeking disable\n");
710 if (paused_p
) {/* we don't seek in pause */
711 warning("seek:disabled while paused\n");
714 if (live_p
) {/* we don't seek in live mode */
715 warning("seek:disabled in live mode\n");
719 npv_thdsws_wait_for_idle(npv_video_scaler_p
.ctx
);
723 r
= npv_clk_get_audio_filt_ts(&now_audio_filt_ts
);
724 if (r
!= TS_FROM_CLK_OK
) {
725 warning("seek:audio:clock timestamp unavailable, ignoring command\n");
729 (void)snd_pcm_drop(npv_audio_pcm_p
);
730 /* XXX: a set of sts can share the same id for seeking */
731 /*--------------------------------------------------------------------*/
732 audio_filt_tb
= avfilter_bufsink_tb_get(npv_audio_filt_p
.abufsink_ctx
);
733 new_audio_filt_ts
= now_audio_filt_ts
+ delta
* audio_filt_tb
.den
735 new_audio_st_ts
= avutil_rescale_q(new_audio_filt_ts
, audio_filt_tb
,
737 pout("trying to seek to %"PRId64
" audio filter time base units/%"PRId64
" audio stream time base units\n", new_audio_filt_ts
, new_audio_st_ts
);
738 a
= avformat_seek_pkt(npv_fmt_ctx_p
, npv_audio_st_p
.id
, new_audio_st_ts
,
741 pout("unable to seek to %"PRId64
" audio filter time base units/%"PRId64
" audio stream time base units\n", new_audio_filt_ts
, new_audio_st_ts
);
742 goto try_restore_audio
;
744 pout("global seek using audio seek to %"PRId64
" audio filter time base units/%"PRId64
" audio stream time base units\n", new_audio_filt_ts
, new_audio_st_ts
);
746 npv_video_dec_flush();
747 npv_audio_dec_flush();
748 npv_audio_filt_flush();
750 npv_clk_invalidate();
751 npv_pipeline_limits_reset();
752 npv_pipeline_prefill_reset(npv_pipeline_limits_p
.pkts
.prefill
.percent
);
755 atomic_store(&pre_x_p
, PREFILL
);
756 prefill_chk_do_p
= true;
757 (void)snd_pcm_prepare(npv_audio_pcm_p
);
761 now_audio_st_ts
= avutil_rescale_q(now_audio_filt_ts
, audio_filt_tb
,
763 a
= avformat_seek_pkt(npv_fmt_ctx_p
, npv_audio_st_p
.id
, now_audio_st_ts
,
765 if (a
< 0) /* we don't send an application error */
766 exit_ok("unable to restore audio to %"PRId64
" audio filter time base units/%"PRId64
" audio stream time base units\n", now_audio_filt_ts
, now_audio_st_ts
);
769 #undef TS_FROM_CLK_OK
771 STATIC
void npv_cmd_rewind(void)
773 pout("COMMAND:rewind\n");
777 STATIC
void npv_cmd_rewind_big(void)
779 pout("COMMAND:rewind big\n");
780 seek_x(-SEEK_DELTA_BIG
);
783 STATIC
void npv_cmd_fastforward(void)
785 pout("COMMAND:fastforward\n");
789 STATIC
void npv_cmd_fastforward_big(void)
791 pout("COMMAND:fastforward big\n");
792 seek_x(SEEK_DELTA_BIG
);
795 STATIC
void npv_cmd_pause(void)
797 if (npv_audio_draining_p
) {
798 warning("pause:audio is draining, toggling pause is disable\n");
802 warning("pause:disabled in live mode\n");
808 pout("COMMAND:unpause\n");
811 avformat_read_play(npv_fmt_ctx_p
);
812 npv_fmt_ctx_unlock();
814 npv_video_timer_start();
818 pout("COMMAND:pause\n");
821 avformat_read_pause(npv_fmt_ctx_p
);
822 npv_fmt_ctx_unlock();
824 npv_video_timer_slow_start();
827 STATIC
void npv_cmd_vol_up(void)
829 npv_audio_filt_cmd_vol_up();
831 STATIC
void npv_cmd_vol_down(void)
833 npv_audio_filt_cmd_vol_down();
835 STATIC
void npv_cmd_vol_up_small(void)
837 npv_audio_filt_cmd_vol_up_small();
839 STATIC
void npv_cmd_vol_down_small(void)
841 npv_audio_filt_cmd_vol_down_small();
843 STATIC
void npv_cmd_mute(void)
845 npv_audio_filt_cmd_mute();
847 #define WIDTH_NOT_DEFINED 0
848 #define HEIGHT_NOT_DEFINED 0
849 #define CHANS_N_NOT_OVERRIDDEN 0
850 #define RATE_NOT_OVERRIDDEN 0
851 #define AUDIO_FR_FMT_NOT_OVERRIDDEN AVUTIL_AUDIO_FR_FMT_NONE
852 int main(int argc
, u8
**args
)
855 bool start_fullscreen
;
859 /* audio override -- start */
861 * we could have got direct parameters for alsa, but doing only an
862 * override triggers an autoconfiguration of any missing parameters
863 * based on initial codec params. In other words, the user is not
864 * required to provide all audio params, the code will try to fit the
865 * missing ones with the initial codec params, which is the default
868 int override_initial_ff_chans_n
;
869 int override_initial_ff_rate
;
870 enum avutil_audio_fr_fmt_t override_initial_ff_audio_fr_fmt
;
871 /* audio override -- end */
874 avcodec_params_t
*audio_codec_params
;
875 avcodec_params_t
*video_codec_params
;
876 /* "turn on utf8" processing in used libs if any *AND* locale system */
877 setlocale(LC_ALL
, "");
878 /* av_log_set_level(AV_LOG_VERBOSE); */
879 /* av_log_set_level(AV_LOG_DEBUG); */
884 start_fullscreen
= false;
885 win_width
= WIDTH_NOT_DEFINED
;
886 win_height
= HEIGHT_NOT_DEFINED
;
889 override_initial_ff_chans_n
= CHANS_N_NOT_OVERRIDDEN
;
890 override_initial_ff_rate
= RATE_NOT_OVERRIDDEN
;
891 override_initial_ff_audio_fr_fmt
= AUDIO_FR_FMT_NOT_OVERRIDDEN
;
894 npv_pipeline_limits_p
.pkts
.prefill
.percent
= 0;
895 opts_parse(argc
, args
, &url
, &live
, &start_fullscreen
,
896 &win_width
, &win_height
,
897 &pcm_str
, &override_initial_ff_chans_n
,
898 &override_initial_ff_rate
,
899 &override_initial_ff_audio_fr_fmt
,
901 &npv_pipeline_limits_p
.pkts
.prefill
.percent
);
902 init_once(url
, live
, start_fullscreen
, initial_vol
, win_width
,
903 win_height
, pcm_str
, &audio_codec_params
, &video_codec_params
,
905 prepare(initial_vol
, override_initial_ff_chans_n
,
906 override_initial_ff_rate
, override_initial_ff_audio_fr_fmt
,
907 npv_pipeline_limits_p
.pkts
.prefill
.percent
, audio_codec_params
,
910 /* switch the ffmpeg log to stdout for metadata/etc dump */
911 avutil_log_set_callback(ff_log_stdout
);
912 avformat_dump_fmt(npv_fmt_ctx_p
, 0, url
, 0);
913 avutil_log_set_callback(avutil_log_default_callback
);
914 /* we are in prefill state */
915 npv_pipeline_read_thd_start();
916 npv_pipeline_audio_thd_start();
917 npv_pipeline_video_thd_start();
918 npv_video_timer_start();
919 npv_xcb_screensaver_heartbeat_timer_start();
924 #undef WIDTH_NOT_DEFINED
925 #undef HEIGHT_NOT_DEFINED
926 #undef CHANS_N_NOT_OVERRIDDEN
927 #undef RATE_NOT_OVERRIDDEN
928 #undef AUDIO_FR_FMT_NOT_OVERRIDDEN