npv: inverted resync logic and rate limiter
[nyanmp.git] / npv / pipeline / public / code.frag.c
blobbc341e72a896b3862b672b9c7d31988409b7ddf7
1 STATIC void limits_reset(void)
3 limits_p.pkts.audio_bytes_n = 0;
4 limits_p.pkts.video_bytes_n = 0;
5 /* hardcoded for now: arbitrary rough estimates */
6 /* ~8s of 400kb audio */
7 limits_p.pkts.limit.audio_bytes_n = 400000 / 8 * 8;
8 /* ~8s of 10Mb video */
9 limits_p.pkts.limit.video_bytes_n = 10000000 / 8 * 8;
11 STATIC void prefill_reset(u8 percent)
13 if (percent > 100)
14 fatal("invalid prefill of %u%% for the buffer of packet queues\n", percent);
15 if (percent != 0) {
16 limits_p.pkts.prefill.audio_bytes_rem =
17 limits_p.pkts.limit.audio_bytes_n * percent / 100;
18 pout("prefill size for the audio packet queue buffer is %u%%/%"PRId64" bytes\n", percent, limits_p.pkts.prefill.audio_bytes_rem);
19 limits_p.pkts.prefill.video_bytes_rem =
20 limits_p.pkts.limit.video_bytes_n * percent / 100;
21 pout("prefill size for the video packet queue buffer is %u%%/%"PRId64" bytes\n", percent, limits_p.pkts.prefill.video_bytes_rem);
22 } else {
23 limits_p.pkts.prefill.audio_bytes_rem = 0;
24 limits_p.pkts.prefill.video_bytes_rem = 0;
25 pout("prefill for the packet queue buffers is disabled\n");
28 STATIC void init_once(void)
30 int r;
32 eof_pkt_l = avcodec_pkt_ref_alloc();
33 if (eof_pkt_l == 0)
34 fatal("ffmpeg:unable to allocate a null/eof reference on a packet\n");
35 eof_pkt_l->data = 0;
36 eof_pkt_l->sz = 0;
38 r = pthread_mutex_init(&limits_p.mutex, 0);
39 if (r != 0)
40 fatal("unable to initialize the mutex to guard the accounting of limits\n");
41 memset(&rate_limiter_previous_tp, 0, sizeof(rate_limiter_previous_tp));
43 STATIC void limits_lock(void)
45 int r;
47 r = pthread_mutex_lock(&limits_p.mutex);
48 if (r != 0)
49 fatal("unable to lock the limits\n");
51 STATIC void limits_unlock(void)
53 int r;
55 r = pthread_mutex_unlock(&limits_p.mutex);
56 if (r != 0)
57 fatal("unable to unlock the limits\n");
59 STATIC void read_thd_start(void)
61 int r;
62 pthread_t id;
63 pthread_attr_t attr;
65 r = pthread_attr_init(&attr);
66 if (r != 0)
67 fatal("unable to initialize read thread attribute\n");
68 r = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
69 if (r != 0)
70 fatal("unable to set the read thread attribute to detach mode\n");
71 r = pthread_create(&id, &attr, &read_thd_entry, 0);
72 if (r != 0)
73 fatal("unable to create the read thread\n");
74 pout("read thread %lu\n", (unsigned long)id);
75 pthread_attr_destroy(&attr);
77 STATIC void audio_thd_start(void)
79 int r;
80 pthread_t id;
81 pthread_attr_t attr;
83 r = pthread_attr_init(&attr);
84 if (r != 0)
85 fatal("unable to initialize audio thread attribute\n");
86 r = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
87 if (r != 0)
88 fatal("unable to set the audio thread attribute to detach mode\n");
89 r = pthread_create(&id, &attr, &audio_thd_entry, 0);
90 if (r != 0)
91 fatal("unable to create the audio thread\n");
92 pout("audio thread %lu\n", (unsigned long)id);
93 pthread_attr_destroy(&attr);
95 STATIC void video_thd_start(void)
97 int r;
98 pthread_t id;
99 pthread_attr_t attr;
101 r = pthread_attr_init(&attr);
102 if (r != 0)
103 fatal("unable to initialize video thread attribute\n");
104 r = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
105 if (r != 0)
106 fatal("unable to set the video thread attribute to detach mode\n");
107 r = pthread_create(&id, &attr, &video_thd_entry, 0);
108 if (r != 0)
109 fatal("unable to create the video thread\n");
110 pout("video thread %lu\n", (unsigned long)id);
111 pthread_attr_destroy(&attr);