1 | //loop.c: |
---|
2 | |
---|
3 | #include "roard.h" |
---|
4 | |
---|
5 | int main_loop (int driver, DRIVER_USERDATA_T driver_inst, struct roar_audio_info * sa) { |
---|
6 | void ** streams_input = NULL; |
---|
7 | uint32_t pos = 0; |
---|
8 | #ifdef MONITOR_LATENCY |
---|
9 | struct timeval try, ans; |
---|
10 | long int ans_1last = 0, ans_2last = 0, ans_3last = 0; |
---|
11 | long int loopc = 0; |
---|
12 | |
---|
13 | printf("\n\e[s"); |
---|
14 | fflush(stdout); |
---|
15 | #endif |
---|
16 | |
---|
17 | ROAR_DBG("main_loop(*) = ?"); |
---|
18 | alive = 1; |
---|
19 | |
---|
20 | while (alive) { |
---|
21 | #ifdef MONITOR_LATENCY |
---|
22 | gettimeofday(&try, NULL); |
---|
23 | #endif |
---|
24 | |
---|
25 | ROAR_DBG("main_loop(*): looping..."); |
---|
26 | |
---|
27 | if ( g_listen_socket != -1 ) { |
---|
28 | ROAR_DBG("main_loop(*): check for new clients..."); |
---|
29 | net_check_listen(); |
---|
30 | } |
---|
31 | |
---|
32 | ROAR_DBG("main_loop(*): check for new data..."); |
---|
33 | if ( clients_check_all() == 0 && g_terminate && g_listen_socket == -1 ) |
---|
34 | alive = 0; |
---|
35 | |
---|
36 | ROAR_DBG("main_loop(*): mixing clients..."); |
---|
37 | if ( g_standby ) { |
---|
38 | // while in standby we still neet to get the buffers to free input buffer space. |
---|
39 | streams_get_mixbuffers(&streams_input, sa, pos); |
---|
40 | } else { |
---|
41 | if ( streams_get_mixbuffers(&streams_input, sa, pos) == 0 ) { |
---|
42 | mix_clients(g_output_buffer, sa->bits, streams_input, ROAR_OUTPUT_BUFFER_SAMPLES * sa->channels); |
---|
43 | } |
---|
44 | } |
---|
45 | |
---|
46 | /* |
---|
47 | // while in standby we still need to write out our buffer to not run in an endless loop without |
---|
48 | // a break |
---|
49 | */ |
---|
50 | |
---|
51 | if ( g_standby ) { |
---|
52 | usleep((1000000 * ROAR_OUTPUT_BUFFER_SAMPLES) / sa->rate); |
---|
53 | printf("usleep(%u) = ?\n", (1000000 * ROAR_OUTPUT_BUFFER_SAMPLES) / sa->rate); |
---|
54 | } else { |
---|
55 | clients_send_filter(sa, pos); |
---|
56 | output_buffer_flush(driver_inst, driver); |
---|
57 | clients_send_mon(sa, pos); |
---|
58 | } |
---|
59 | // output_buffer_reinit(); |
---|
60 | |
---|
61 | pos = ROAR_MATH_OVERFLOW_ADD(pos, ROAR_OUTPUT_BUFFER_SAMPLES); |
---|
62 | ROAR_DBG("main_loop(*): current pos: %u", pos); |
---|
63 | #ifdef MONITOR_LATENCY |
---|
64 | gettimeofday(&ans, NULL); |
---|
65 | |
---|
66 | while (ans.tv_sec > try.tv_sec) { |
---|
67 | ans.tv_sec--; |
---|
68 | ans.tv_usec += 1000000; |
---|
69 | } |
---|
70 | ans.tv_usec -= try.tv_usec; |
---|
71 | |
---|
72 | if ( loopc % 128 ) { |
---|
73 | printf("\e[ucurrent latency: %.3fms average: %.3fms ", ans.tv_usec / (double)1000, |
---|
74 | (ans.tv_usec+ans_3last+ans_2last+ans_1last)/ (double)4000); |
---|
75 | fflush(stdout); |
---|
76 | } |
---|
77 | |
---|
78 | ans_3last = ans_2last; |
---|
79 | ans_2last = ans_1last; |
---|
80 | ans_1last = ans.tv_usec; |
---|
81 | loopc++; |
---|
82 | #endif |
---|
83 | } |
---|
84 | |
---|
85 | return -1; |
---|
86 | } |
---|
87 | |
---|
88 | //ll |
---|