]> code.delx.au - pulseaudio/blob - src/tests/alsa-time-test.c
Merge remote-tracking branch 'mkbosmans/merge/build-sys'
[pulseaudio] / src / tests / alsa-time-test.c
1 #include <assert.h>
2 #include <inttypes.h>
3 #include <time.h>
4
5 #include <alsa/asoundlib.h>
6
7 static uint64_t timespec_us(const struct timespec *ts) {
8 return
9 ts->tv_sec * 1000000LLU +
10 ts->tv_nsec / 1000LLU;
11 }
12
13 int main(int argc, char *argv[]) {
14 const char *dev;
15 int r, cap;
16 snd_pcm_hw_params_t *hwparams;
17 snd_pcm_sw_params_t *swparams;
18 snd_pcm_status_t *status;
19 snd_pcm_t *pcm;
20 unsigned rate = 44100;
21 unsigned periods = 2;
22 snd_pcm_uframes_t boundary, buffer_size = 44100/10; /* 100s */
23 int dir = 1;
24 struct timespec start, last_timestamp = { 0, 0 };
25 uint64_t start_us;
26 snd_pcm_sframes_t last_avail = 0, last_delay = 0;
27 struct pollfd *pollfds;
28 int n_pollfd;
29 int64_t sample_count = 0;
30
31 snd_pcm_hw_params_alloca(&hwparams);
32 snd_pcm_sw_params_alloca(&swparams);
33 snd_pcm_status_alloca(&status);
34
35 r = clock_gettime(CLOCK_MONOTONIC, &start);
36 assert(r == 0);
37
38 start_us = timespec_us(&start);
39
40 dev = argc > 1 ? argv[1] : "front:AudioPCI";
41 cap = argc > 2 ? atoi(argv[2]) : 0;
42
43 if (cap == 0)
44 r = snd_pcm_open(&pcm, dev, SND_PCM_STREAM_PLAYBACK, 0);
45 else
46 r = snd_pcm_open(&pcm, dev, SND_PCM_STREAM_CAPTURE, 0);
47 assert(r == 0);
48
49 r = snd_pcm_hw_params_any(pcm, hwparams);
50 assert(r == 0);
51
52 r = snd_pcm_hw_params_set_rate_resample(pcm, hwparams, 0);
53 assert(r == 0);
54
55 r = snd_pcm_hw_params_set_access(pcm, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED);
56 assert(r == 0);
57
58 r = snd_pcm_hw_params_set_format(pcm, hwparams, SND_PCM_FORMAT_S16_LE);
59 assert(r == 0);
60
61 r = snd_pcm_hw_params_set_rate_near(pcm, hwparams, &rate, NULL);
62 assert(r == 0);
63
64 r = snd_pcm_hw_params_set_channels(pcm, hwparams, 2);
65 assert(r == 0);
66
67 r = snd_pcm_hw_params_set_periods_integer(pcm, hwparams);
68 assert(r == 0);
69
70 r = snd_pcm_hw_params_set_periods_near(pcm, hwparams, &periods, &dir);
71 assert(r == 0);
72
73 r = snd_pcm_hw_params_set_buffer_size_near(pcm, hwparams, &buffer_size);
74 assert(r == 0);
75
76 r = snd_pcm_hw_params(pcm, hwparams);
77 assert(r == 0);
78
79 r = snd_pcm_hw_params_current(pcm, hwparams);
80 assert(r == 0);
81
82 r = snd_pcm_sw_params_current(pcm, swparams);
83 assert(r == 0);
84
85 if (cap == 0)
86 r = snd_pcm_sw_params_set_avail_min(pcm, swparams, 1);
87 else
88 r = snd_pcm_sw_params_set_avail_min(pcm, swparams, 0);
89 assert(r == 0);
90
91 r = snd_pcm_sw_params_set_period_event(pcm, swparams, 0);
92 assert(r == 0);
93
94 r = snd_pcm_hw_params_get_buffer_size(hwparams, &buffer_size);
95 assert(r == 0);
96 r = snd_pcm_sw_params_set_start_threshold(pcm, swparams, buffer_size);
97 assert(r == 0);
98
99 r = snd_pcm_sw_params_get_boundary(swparams, &boundary);
100 assert(r == 0);
101 r = snd_pcm_sw_params_set_stop_threshold(pcm, swparams, boundary);
102 assert(r == 0);
103
104 r = snd_pcm_sw_params_set_tstamp_mode(pcm, swparams, SND_PCM_TSTAMP_ENABLE);
105 assert(r == 0);
106
107 r = snd_pcm_sw_params(pcm, swparams);
108 assert(r == 0);
109
110 r = snd_pcm_prepare(pcm);
111 assert(r == 0);
112
113 r = snd_pcm_sw_params_current(pcm, swparams);
114 assert(r == 0);
115
116 /* assert(snd_pcm_hw_params_is_monotonic(hwparams) > 0); */
117
118 n_pollfd = snd_pcm_poll_descriptors_count(pcm);
119 assert(n_pollfd > 0);
120
121 pollfds = malloc(sizeof(struct pollfd) * n_pollfd);
122 assert(pollfds);
123
124 r = snd_pcm_poll_descriptors(pcm, pollfds, n_pollfd);
125 assert(r == n_pollfd);
126
127 if (cap) {
128 r = snd_pcm_start(pcm);
129 assert(r == 0);
130 }
131
132 for (;;) {
133 snd_pcm_sframes_t avail, delay;
134 struct timespec now, timestamp;
135 unsigned short revents;
136 int handled = 0;
137 uint64_t now_us, timestamp_us;
138 snd_pcm_state_t state;
139 unsigned long long pos;
140
141 r = poll(pollfds, n_pollfd, 0);
142 assert(r >= 0);
143
144 r = snd_pcm_poll_descriptors_revents(pcm, pollfds, n_pollfd, &revents);
145 assert(r == 0);
146
147 if (cap == 0)
148 assert((revents & ~POLLOUT) == 0);
149 else
150 assert((revents & ~POLLIN) == 0);
151
152 avail = snd_pcm_avail(pcm);
153 assert(avail >= 0);
154
155 r = snd_pcm_status(pcm, status);
156 assert(r == 0);
157
158 /* This assertion fails from time to time. ALSA seems to be broken */
159 /* assert(avail == (snd_pcm_sframes_t) snd_pcm_status_get_avail(status)); */
160 /* printf("%lu %lu\n", (unsigned long) avail, (unsigned long) snd_pcm_status_get_avail(status)); */
161
162 snd_pcm_status_get_htstamp(status, &timestamp);
163 delay = snd_pcm_status_get_delay(status);
164 state = snd_pcm_status_get_state(status);
165
166 r = clock_gettime(CLOCK_MONOTONIC, &now);
167 assert(r == 0);
168
169 assert(!revents || avail > 0);
170
171 if ((!cap && avail) || (cap && (unsigned)avail >= buffer_size)) {
172 snd_pcm_sframes_t sframes;
173 static const uint16_t psamples[2] = { 0, 0 };
174 uint16_t csamples[2];
175
176 if (cap == 0)
177 sframes = snd_pcm_writei(pcm, psamples, 1);
178 else
179 sframes = snd_pcm_readi(pcm, csamples, 1);
180 assert(sframes == 1);
181
182 handled = 1;
183 sample_count++;
184 }
185
186 if (!handled &&
187 memcmp(&timestamp, &last_timestamp, sizeof(timestamp)) == 0 &&
188 avail == last_avail &&
189 delay == last_delay) {
190 /* This is boring */
191 continue;
192 }
193
194 now_us = timespec_us(&now);
195 timestamp_us = timespec_us(&timestamp);
196
197 if (cap == 0)
198 pos = (unsigned long long) ((sample_count - handled - delay) * 1000000LU / 44100);
199 else
200 pos = (unsigned long long) ((sample_count - handled + delay) * 1000000LU / 44100);
201
202 printf("%llu\t%llu\t%llu\t%llu\t%li\t%li\t%i\t%i\t%i\n",
203 (unsigned long long) (now_us - start_us),
204 (unsigned long long) (timestamp_us ? timestamp_us - start_us : 0),
205 pos,
206 (unsigned long long) sample_count,
207 (signed long) avail,
208 (signed long) delay,
209 revents,
210 handled,
211 state);
212
213 if (cap == 0)
214 /** When this assert is hit, most likely something bad
215 * happened, i.e. the avail jumped suddenly. */
216 assert((unsigned) avail <= buffer_size);
217
218 last_avail = avail;
219 last_delay = delay;
220 last_timestamp = timestamp;
221 }
222
223 return 0;
224 }