OSDN Git Service

03817c7c673bf9e61bb89ae4533787c96404726a
[android-x86/external-alsa-lib.git] / test / audio_time.c
1 /*
2  * This program only tracks the difference between system time
3  * and audio time, as reported in snd_pcm_status(). It should be
4  * helpful to verify the information reported by drivers.
5  */
6
7 #include "../include/asoundlib.h"
8 #include <math.h>
9
10 static char *device = "hw:0,0";
11
12 snd_output_t *output = NULL;
13
14 long long timestamp2ns(snd_htimestamp_t t)
15 {
16         long long nsec;
17
18         nsec = t.tv_sec * 1000000000;
19         nsec += t.tv_nsec;
20
21         return nsec;
22 }
23
24 long long timediff(snd_htimestamp_t t1, snd_htimestamp_t t2)
25 {
26         long long nsec1, nsec2;
27
28         nsec1 = timestamp2ns(t1);
29         nsec2 = timestamp2ns(t2);
30
31         return nsec1 - nsec2;
32 }
33
34 void gettimestamp(snd_pcm_t *handle, snd_htimestamp_t *timestamp,
35                   snd_htimestamp_t *trigger_timestamp,
36                   snd_htimestamp_t *audio_timestamp,
37                   snd_pcm_uframes_t *avail, snd_pcm_sframes_t *delay)
38 {
39         int err;
40         snd_pcm_status_t *status;
41
42         snd_pcm_status_alloca(&status);
43         if ((err = snd_pcm_status(handle, status)) < 0) {
44                 printf("Stream status error: %s\n", snd_strerror(err));
45                 exit(0);
46         }
47         snd_pcm_status_get_trigger_htstamp(status, trigger_timestamp);
48         snd_pcm_status_get_htstamp(status, timestamp);
49         snd_pcm_status_get_audio_htstamp(status, audio_timestamp);
50         *avail = snd_pcm_status_get_avail(status);
51         *delay = snd_pcm_status_get_delay(status);
52 }
53
54 #define PERIOD 6000
55 #define PCM_LINK        /* sync start for playback and capture */
56 #define TRACK_CAPTURE   /* dump capture timing info  */
57 #define TRACK_PLAYBACK  /* dump playback timing info */
58 #define TRACK_SAMPLE_COUNTS /* show difference between sample counters and audiotimestamps returned by driver */
59 #define PLAYBACK_BUFFERS 4
60
61
62 int main(void)
63 {
64         int err;
65         unsigned int i;
66         snd_pcm_t *handle_p = NULL;
67         snd_pcm_t *handle_c = NULL;
68         snd_pcm_sframes_t frames;
69         snd_htimestamp_t tstamp_c, tstamp_p;
70         snd_htimestamp_t trigger_tstamp_c, trigger_tstamp_p;
71         snd_htimestamp_t audio_tstamp_c, audio_tstamp_p;
72         unsigned char buffer_p[PERIOD*4*4];
73         unsigned char buffer_c[PERIOD*4*4];
74
75         snd_pcm_hw_params_t *hwparams_p;
76         snd_pcm_hw_params_t *hwparams_c;
77
78         snd_pcm_sw_params_t *swparams_p;
79         snd_pcm_sw_params_t *swparams_c;
80
81         snd_pcm_uframes_t curr_count_c;
82         snd_pcm_uframes_t frame_count_c = 0;
83         snd_pcm_uframes_t curr_count_p;
84         snd_pcm_uframes_t frame_count_p = 0;
85
86         snd_pcm_sframes_t delay_p, delay_c;
87         snd_pcm_uframes_t avail_p, avail_c;
88
89         if ((err = snd_pcm_open(&handle_p, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
90                 printf("Playback open error: %s\n", snd_strerror(err));
91                 goto _exit;
92         }
93         if ((err = snd_pcm_set_params(handle_p,
94                                       SND_PCM_FORMAT_S16,
95                                       SND_PCM_ACCESS_RW_INTERLEAVED,
96                                       2,
97                                       48000,
98                                       0,
99                                       500000)) < 0) {   /* 0.5sec */
100                 printf("Playback open error: %s\n", snd_strerror(err));
101                 goto _exit;
102         }
103
104         snd_pcm_hw_params_alloca(&hwparams_p);
105         /* get the current hwparams */
106         err = snd_pcm_hw_params_current(handle_p, hwparams_p);
107         if (err < 0) {
108                 printf("Unable to determine current hwparams_p: %s\n", snd_strerror(err));
109                 goto _exit;
110         }
111         if (snd_pcm_hw_params_supports_audio_wallclock_ts(hwparams_p))
112                 printf("Playback relies on audio wallclock timestamps\n");
113         else
114                 printf("Playback relies on audio sample counter timestamps\n");
115
116         snd_pcm_sw_params_alloca(&swparams_p);
117         /* get the current swparams */
118         err = snd_pcm_sw_params_current(handle_p, swparams_p);
119         if (err < 0) {
120                 printf("Unable to determine current swparams_p: %s\n", snd_strerror(err));
121                 goto _exit;
122         }
123
124         /* enable tstamp */
125         err = snd_pcm_sw_params_set_tstamp_mode(handle_p, swparams_p, SND_PCM_TSTAMP_ENABLE);
126         if (err < 0) {
127                 printf("Unable to set tstamp mode : %s\n", snd_strerror(err));
128                 goto _exit;
129         }
130
131         /* write the sw parameters */
132         err = snd_pcm_sw_params(handle_p, swparams_p);
133         if (err < 0) {
134                 printf("Unable to set swparams_p : %s\n", snd_strerror(err));
135                 goto _exit;
136         }
137
138         if ((err = snd_pcm_open(&handle_c, device, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK)) < 0) {
139                 printf("Capture open error: %s\n", snd_strerror(err));
140                 goto _exit;
141         }
142         if ((err = snd_pcm_set_params(handle_c,
143                                       SND_PCM_FORMAT_S16,
144                                       SND_PCM_ACCESS_RW_INTERLEAVED,
145                                       2,
146                                       48000,
147                                       0,
148                                       500000)) < 0) {   /* 0.5sec */
149                 printf("Capture open error: %s\n", snd_strerror(err));
150                 goto _exit;
151         }
152
153         snd_pcm_hw_params_alloca(&hwparams_c);
154         /* get the current hwparams */
155         err = snd_pcm_hw_params_current(handle_c, hwparams_c);
156         if (err < 0) {
157                 printf("Unable to determine current hwparams_c: %s\n", snd_strerror(err));
158                 goto _exit;
159         }
160         if (snd_pcm_hw_params_supports_audio_wallclock_ts(hwparams_c))
161                 printf("Capture relies on audio wallclock timestamps\n");
162         else
163                 printf("Capture relies on audio sample counter timestamps\n");
164
165         snd_pcm_sw_params_alloca(&swparams_c);
166         /* get the current swparams */
167         err = snd_pcm_sw_params_current(handle_c, swparams_c);
168         if (err < 0) {
169                 printf("Unable to determine current swparams_c: %s\n", snd_strerror(err));
170                 goto _exit;
171         }
172
173         /* enable tstamp */
174         err = snd_pcm_sw_params_set_tstamp_mode(handle_c, swparams_c, SND_PCM_TSTAMP_ENABLE);
175         if (err < 0) {
176                 printf("Unable to set tstamp mode : %s\n", snd_strerror(err));
177                 goto _exit;
178         }
179
180         /* write the sw parameters */
181         err = snd_pcm_sw_params(handle_c, swparams_c);
182         if (err < 0) {
183                 printf("Unable to set swparams_c : %s\n", snd_strerror(err));
184                 goto _exit;
185         }
186
187 #ifdef PCM_LINK
188         if ((err = snd_pcm_link(handle_c, handle_p)) < 0) {
189                 printf("Streams link error: %s\n", snd_strerror(err));
190                 exit(0);
191         }
192 #endif
193
194         i = PLAYBACK_BUFFERS;
195         while (i--) {
196                 frames = snd_pcm_writei(handle_p, buffer_p, PERIOD);
197                 if (frames < 0) {
198                         printf("snd_pcm_writei failed: %s\n", snd_strerror(frames));
199                         goto _exit;
200                 }
201                 frame_count_p += frames;
202         }
203
204         if (PLAYBACK_BUFFERS != 4)
205                 snd_pcm_start(handle_p);
206
207 #ifndef PCM_LINK
208         /* need to start capture explicitly */
209         snd_pcm_start(handle_c);
210 #endif
211
212         while (1) {
213
214                 frames = snd_pcm_wait(handle_c, -1);
215                 if (frames < 0) {
216                         printf("snd_pcm_wait failed: %s\n", snd_strerror(frames));
217                         goto _exit;
218                 }
219
220                 frames = snd_pcm_readi(handle_c, buffer_c, PERIOD);
221                 if (frames < 0) {
222                         printf("snd_pcm_readi failed: %s\n", snd_strerror(frames));
223                         goto _exit;
224                 }
225                 frame_count_c += frames;
226
227                 frames = snd_pcm_writei(handle_p, buffer_p, PERIOD);
228                 if (frames < 0) {
229                         printf("snd_pcm_writei failed: %s\n", snd_strerror(frames));
230                         goto _exit;
231                 }
232
233                 frame_count_p += frames;
234
235 #if defined(TRACK_PLAYBACK)
236                 gettimestamp(handle_p, &tstamp_p, &trigger_tstamp_p, &audio_tstamp_p, &avail_p, &delay_p);
237
238 #if defined(TRACK_SAMPLE_COUNTS)
239                 curr_count_p = frame_count_p - delay_p; /* written minus queued */
240
241                 printf("playback: curr_count %lli driver count %lli, delta %lli\n",
242                        (long long)curr_count_p * 1000000000LL / 48000 ,
243                        timestamp2ns(audio_tstamp_p),
244                        (long long)curr_count_p * 1000000000LL / 48000 - timestamp2ns(audio_tstamp_p)
245                        );
246 #endif
247
248                 printf("playback: systime: %lli nsec, audio time %lli nsec, \tsystime delta %lli\n",
249                        timediff(tstamp_p, trigger_tstamp_p),
250                        timestamp2ns(audio_tstamp_p),
251                        timediff(tstamp_p, trigger_tstamp_p) - timestamp2ns(audio_tstamp_p)
252                        );
253 #endif
254
255 #if defined(TRACK_CAPTURE)
256                 gettimestamp(handle_c, &tstamp_c, &trigger_tstamp_c, &audio_tstamp_c, &avail_c, &delay_c);
257
258 #if defined(TRACK_SAMPLE_COUNTS)
259                 curr_count_c = frame_count_c + delay_c; /* read plus queued */
260
261
262                 printf("capture: curr_count %lli driver count %lli, delta %lli\n",
263                        (long long)curr_count_c * 1000000000LL / 48000 ,
264                        timestamp2ns(audio_tstamp_c),
265                        (long long)curr_count_c * 1000000000LL / 48000 - timestamp2ns(audio_tstamp_c)
266                        );
267 #endif
268
269                 printf("\t capture: systime: %lli nsec, audio time %lli nsec, \tsystime delta %lli\n",
270                        timediff(tstamp_c, trigger_tstamp_c),
271                        timestamp2ns(audio_tstamp_c),
272                        timediff(tstamp_c, trigger_tstamp_c) - timestamp2ns(audio_tstamp_c)
273                        );
274 #endif
275
276         }
277
278 _exit:
279         if (handle_p)
280                 snd_pcm_close(handle_p);
281         if (handle_c)
282                 snd_pcm_close(handle_c);
283
284         return 0;
285 }