]> code.delx.au - pulseaudio/blob - src/tests/alsa-time-test.c
Updated linguas file for it lang
[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;
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 = 0;
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, last_delay;
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
42 r = snd_pcm_open(&pcm, dev, SND_PCM_STREAM_PLAYBACK, 0);
43 assert(r == 0);
44
45 r = snd_pcm_hw_params_any(pcm, hwparams);
46 assert(r == 0);
47
48 r = snd_pcm_hw_params_set_rate_resample(pcm, hwparams, 0);
49 assert(r == 0);
50
51 r = snd_pcm_hw_params_set_access(pcm, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED);
52 assert(r == 0);
53
54 r = snd_pcm_hw_params_set_format(pcm, hwparams, SND_PCM_FORMAT_S16_LE);
55 assert(r == 0);
56
57 r = snd_pcm_hw_params_set_rate_near(pcm, hwparams, &rate, NULL);
58 assert(r == 0);
59
60 r = snd_pcm_hw_params_set_channels(pcm, hwparams, 2);
61 assert(r == 0);
62
63 r = snd_pcm_hw_params_set_periods_integer(pcm, hwparams);
64 assert(r == 0);
65
66 r = snd_pcm_hw_params_set_periods_near(pcm, hwparams, &periods, &dir);
67 assert(r == 0);
68
69 r = snd_pcm_hw_params_set_buffer_size_near(pcm, hwparams, &buffer_size);
70 assert(r == 0);
71
72 r = snd_pcm_hw_params(pcm, hwparams);
73 assert(r == 0);
74
75 r = snd_pcm_hw_params_current(pcm, hwparams);
76 assert(r == 0);
77
78 r = snd_pcm_sw_params_set_avail_min(pcm, swparams, 1);
79 assert(r == 0);
80
81 r = snd_pcm_sw_params_set_period_event(pcm, swparams, 1);
82 assert(r == 0);
83
84 r = snd_pcm_hw_params_get_buffer_size(hwparams, &buffer_size);
85 assert(r == 0);
86 r = snd_pcm_sw_params_set_start_threshold(pcm, swparams, buffer_size);
87 assert(r == 0);
88
89 r = snd_pcm_sw_params_get_boundary(swparams, &boundary);
90 assert(r == 0);
91 r = snd_pcm_sw_params_set_stop_threshold(pcm, swparams, boundary);
92 assert(r == 0);
93
94 r = snd_pcm_sw_params_set_tstamp_mode(pcm, swparams, SND_PCM_TSTAMP_ENABLE);
95 assert(r == 0);
96
97 r = snd_pcm_sw_params(pcm, swparams);
98 assert(r == 0);
99
100 r = snd_pcm_prepare(pcm);
101 assert(r == 0);
102
103 r = snd_pcm_sw_params_current(pcm, swparams);
104 assert(r == 0);
105
106 /* assert(snd_pcm_hw_params_is_monotonic(hwparams) > 0); */
107
108 n_pollfd = snd_pcm_poll_descriptors_count(pcm);
109 assert(n_pollfd > 0);
110
111 pollfds = malloc(sizeof(struct pollfd) * n_pollfd);
112 assert(pollfds);
113
114 r = snd_pcm_poll_descriptors(pcm, pollfds, n_pollfd);
115 assert(r == n_pollfd);
116
117 for (;;) {
118 snd_pcm_sframes_t avail, delay;
119 /* snd_pcm_uframes_t avail2; */
120 struct timespec now, timestamp;
121 unsigned short revents;
122 int written = 0;
123 uint64_t now_us, timestamp_us;
124 snd_pcm_state_t state;
125
126 r = poll(pollfds, n_pollfd, 0);
127 assert(r >= 0);
128
129 r = snd_pcm_poll_descriptors_revents(pcm, pollfds, n_pollfd, &revents);
130 assert(r == 0);
131
132 assert((revents & ~POLLOUT) == 0);
133
134 /* state = snd_pcm_get_state(pcm); */
135
136 avail = snd_pcm_avail(pcm);
137 assert(avail >= 0);
138
139 r = snd_pcm_status(pcm, status);
140 assert(r == 0);
141
142 printf("%lu %lu\n", (unsigned long) avail, (unsigned long) snd_pcm_status_get_avail(status));
143
144 assert(avail == (snd_pcm_sframes_t) snd_pcm_status_get_avail(status));
145 snd_pcm_status_get_htstamp(status, &timestamp);
146 delay = snd_pcm_status_get_delay(status);
147 state = snd_pcm_status_get_state(status);
148
149 /* r = snd_pcm_avail_delay(pcm, &avail, &delay); */
150 /* assert(r == 0); */
151
152 /* r = snd_pcm_htimestamp(pcm, &avail2, &timestamp); */
153 /* assert(r == 0); */
154
155 /* assert(avail == (snd_pcm_sframes_t) avail2); */
156
157 r = clock_gettime(CLOCK_MONOTONIC, &now);
158 assert(r == 0);
159
160 assert(!revents || avail > 0);
161
162 if (avail) {
163 snd_pcm_sframes_t sframes;
164 static const uint16_t samples[2] = { 0, 0 };
165
166 sframes = snd_pcm_writei(pcm, samples, 1);
167 assert(sframes == 1);
168
169 written = 1;
170 sample_count++;
171 }
172
173 if (!written &&
174 memcmp(&timestamp, &last_timestamp, sizeof(timestamp)) == 0 &&
175 avail == last_avail &&
176 delay == last_delay) {
177 /* This is boring */
178 continue;
179 }
180
181 now_us = timespec_us(&now);
182 timestamp_us = timespec_us(&timestamp);
183
184 printf("%llu\t%llu\t%llu\t%li\t%li\t%i\t%i\t%i\n",
185 (unsigned long long) (now_us - start_us),
186 (unsigned long long) (timestamp_us ? timestamp_us - start_us : 0),
187 (unsigned long long) ((sample_count - 1 - delay) * 1000000LU / 44100),
188 (signed long) avail,
189 (signed long) delay,
190 revents,
191 written,
192 state);
193
194 last_avail = avail;
195 last_delay = delay;
196 last_timestamp = timestamp;
197 }
198
199 return 0;
200 }