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.
20 #include "../include/asoundlib.h"
22 static char *pcm_name = "hw:0";
23 snd_output_t *output = NULL;
25 static void usage(char *command)
27 printf("Usage: %s [OPTION]... \n"
30 "-c, --capture capture tstamps \n"
31 "-d, --delay add delay \n"
32 "-D, --device=NAME select PCM by name \n"
33 "-p, --playback playback tstamps \n"
34 "-t, --ts_type=TYPE Compat(0),default(1),link(2),link_absolute(3),link_estimated(4),link_synchronized(5) \n"
35 "-r, --report show audio timestamp and accuracy validity\n"
40 long long timestamp2ns(snd_htimestamp_t t)
44 nsec = t.tv_sec * 1000000000ULL;
50 long long timediff(snd_htimestamp_t t1, snd_htimestamp_t t2)
52 long long nsec1, nsec2;
54 nsec1 = timestamp2ns(t1);
55 nsec2 = timestamp2ns(t2);
60 void _gettimestamp(snd_pcm_t *handle, snd_htimestamp_t *timestamp,
61 snd_htimestamp_t *trigger_timestamp,
62 snd_htimestamp_t *audio_timestamp,
63 snd_pcm_audio_tstamp_config_t *audio_tstamp_config,
64 snd_pcm_audio_tstamp_report_t *audio_tstamp_report,
65 snd_pcm_uframes_t *avail, snd_pcm_sframes_t *delay)
68 snd_pcm_status_t *status;
70 snd_pcm_status_alloca(&status);
72 snd_pcm_status_set_audio_htstamp_config(status, audio_tstamp_config);
74 if ((err = snd_pcm_status(handle, status)) < 0) {
75 printf("Stream status error: %s\n", snd_strerror(err));
78 snd_pcm_status_get_trigger_htstamp(status, trigger_timestamp);
79 snd_pcm_status_get_htstamp(status, timestamp);
80 snd_pcm_status_get_audio_htstamp(status, audio_timestamp);
81 snd_pcm_status_get_audio_htstamp_report(status, audio_tstamp_report);
82 *avail = snd_pcm_status_get_avail(status);
83 *delay = snd_pcm_status_get_delay(status);
86 #define TIMESTAMP_FREQ 8 /* Hz */
87 #define SAMPLE_FREQ 48000
88 #define PERIOD (SAMPLE_FREQ/TIMESTAMP_FREQ)
89 #define PCM_LINK /* sync start for playback and capture */
90 #define TRACK_CAPTURE /* dump capture timing info */
91 #define TRACK_PLAYBACK /* dump playback timing info */
92 /*#define TRACK_SAMPLE_COUNTS */ /* show difference between sample counters and audiotimestamps returned by driver */
93 #define PLAYBACK_BUFFERS 4
94 #define TSTAMP_TYPE SND_PCM_TSTAMP_TYPE_MONOTONIC_RAW
97 int main(int argc, char *argv[])
102 snd_pcm_t *handle_p = NULL;
103 snd_pcm_t *handle_c = NULL;
104 snd_pcm_sframes_t frames;
105 snd_htimestamp_t tstamp_c, tstamp_p;
106 snd_htimestamp_t trigger_tstamp_c, trigger_tstamp_p;
107 snd_htimestamp_t audio_tstamp_c, audio_tstamp_p;
108 unsigned char buffer_p[PERIOD*4*4];
109 unsigned char buffer_c[PERIOD*4*4];
111 snd_pcm_hw_params_t *hwparams_p;
112 snd_pcm_hw_params_t *hwparams_c;
114 snd_pcm_sw_params_t *swparams_p;
115 snd_pcm_sw_params_t *swparams_c;
117 snd_pcm_uframes_t frame_count_c = 0;
118 snd_pcm_uframes_t frame_count_p = 0;
120 snd_pcm_sframes_t delay_p, delay_c;
121 snd_pcm_uframes_t avail_p, avail_c;
123 snd_pcm_audio_tstamp_config_t audio_tstamp_config_p;
124 snd_pcm_audio_tstamp_config_t audio_tstamp_config_c;
125 snd_pcm_audio_tstamp_report_t audio_tstamp_report_p;
126 snd_pcm_audio_tstamp_report_t audio_tstamp_report_c;
129 static const char short_options[] = "hcpdrD:t:";
131 static const struct option long_options[] = {
132 {"capture", 0, 0, 'c'},
133 {"delay", 0, 0, 'd'},
134 {"device", required_argument, 0, 'D'},
135 {"help", no_argument, 0, 'h'},
136 {"playback", 0, 0, 'p'},
137 {"ts_type", required_argument, 0, 't'},
138 {"report", 0, 0, 'r'},
148 while ((c = getopt_long(argc, argv, short_options, long_options, &option_index)) != -1) {
173 memset(&audio_tstamp_config_p, 0, sizeof(snd_pcm_audio_tstamp_config_t));
174 memset(&audio_tstamp_config_c, 0, sizeof(snd_pcm_audio_tstamp_config_t));
175 memset(&audio_tstamp_report_p, 0, sizeof(snd_pcm_audio_tstamp_report_t));
176 memset(&audio_tstamp_report_c, 0, sizeof(snd_pcm_audio_tstamp_report_t));
179 if ((err = snd_pcm_open(&handle_p, pcm_name, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
180 printf("Playback open error: %s\n", snd_strerror(err));
184 if ((err = snd_pcm_set_params(handle_p,
186 SND_PCM_ACCESS_RW_INTERLEAVED,
190 4*1000000/TIMESTAMP_FREQ)) < 0) {
191 printf("Playback open error: %s\n", snd_strerror(err));
195 snd_pcm_hw_params_alloca(&hwparams_p);
196 /* get the current hwparams */
197 err = snd_pcm_hw_params_current(handle_p, hwparams_p);
199 printf("Unable to determine current hwparams_p: %s\n", snd_strerror(err));
203 if (snd_pcm_hw_params_supports_audio_ts_type(hwparams_p, SND_PCM_AUDIO_TSTAMP_TYPE_COMPAT))
204 printf("Playback supports audio compat timestamps\n");
205 if (snd_pcm_hw_params_supports_audio_ts_type(hwparams_p, SND_PCM_AUDIO_TSTAMP_TYPE_DEFAULT))
206 printf("Playback supports audio default timestamps\n");
207 if (snd_pcm_hw_params_supports_audio_ts_type(hwparams_p, SND_PCM_AUDIO_TSTAMP_TYPE_LINK))
208 printf("Playback supports audio link timestamps\n");
209 if (snd_pcm_hw_params_supports_audio_ts_type(hwparams_p, SND_PCM_AUDIO_TSTAMP_TYPE_LINK_ABSOLUTE))
210 printf("Playback supports audio link absolute timestamps\n");
211 if (snd_pcm_hw_params_supports_audio_ts_type(hwparams_p, SND_PCM_AUDIO_TSTAMP_TYPE_LINK_ESTIMATED))
212 printf("Playback supports audio link estimated timestamps\n");
213 if (snd_pcm_hw_params_supports_audio_ts_type(hwparams_p, SND_PCM_AUDIO_TSTAMP_TYPE_LINK_SYNCHRONIZED))
214 printf("Playback supports audio link synchronized timestamps\n");
216 snd_pcm_sw_params_alloca(&swparams_p);
217 /* get the current swparams */
218 err = snd_pcm_sw_params_current(handle_p, swparams_p);
220 printf("Unable to determine current swparams_p: %s\n", snd_strerror(err));
225 err = snd_pcm_sw_params_set_tstamp_mode(handle_p, swparams_p, SND_PCM_TSTAMP_ENABLE);
227 printf("Unable to set tstamp mode : %s\n", snd_strerror(err));
231 err = snd_pcm_sw_params_set_tstamp_type(handle_p, swparams_p, TSTAMP_TYPE);
233 printf("Unable to set tstamp type : %s\n", snd_strerror(err));
237 /* write the sw parameters */
238 err = snd_pcm_sw_params(handle_p, swparams_p);
240 printf("Unable to set swparams_p : %s\n", snd_strerror(err));
248 if ((err = snd_pcm_open(&handle_c, pcm_name, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK)) < 0) {
249 printf("Capture open error: %s\n", snd_strerror(err));
252 if ((err = snd_pcm_set_params(handle_c,
254 SND_PCM_ACCESS_RW_INTERLEAVED,
258 4*1000000/TIMESTAMP_FREQ)) < 0) {
259 printf("Capture open error: %s\n", snd_strerror(err));
263 snd_pcm_hw_params_alloca(&hwparams_c);
264 /* get the current hwparams */
265 err = snd_pcm_hw_params_current(handle_c, hwparams_c);
267 printf("Unable to determine current hwparams_c: %s\n", snd_strerror(err));
271 if (snd_pcm_hw_params_supports_audio_ts_type(hwparams_c, SND_PCM_AUDIO_TSTAMP_TYPE_COMPAT))
272 printf("Capture supports audio compat timestamps\n");
273 if (snd_pcm_hw_params_supports_audio_ts_type(hwparams_c, SND_PCM_AUDIO_TSTAMP_TYPE_DEFAULT))
274 printf("Capture supports audio default timestamps\n");
275 if (snd_pcm_hw_params_supports_audio_ts_type(hwparams_c, SND_PCM_AUDIO_TSTAMP_TYPE_LINK))
276 printf("Capture supports audio link timestamps\n");
277 if (snd_pcm_hw_params_supports_audio_ts_type(hwparams_c, SND_PCM_AUDIO_TSTAMP_TYPE_LINK_ABSOLUTE))
278 printf("Capture supports audio link absolute timestamps\n");
279 if (snd_pcm_hw_params_supports_audio_ts_type(hwparams_c, SND_PCM_AUDIO_TSTAMP_TYPE_LINK_ESTIMATED))
280 printf("Capture supports audio link estimated timestamps\n");
281 if (snd_pcm_hw_params_supports_audio_ts_type(hwparams_c, SND_PCM_AUDIO_TSTAMP_TYPE_LINK_SYNCHRONIZED))
282 printf("Capture supports audio link synchronized timestamps\n");
284 snd_pcm_sw_params_alloca(&swparams_c);
285 /* get the current swparams */
286 err = snd_pcm_sw_params_current(handle_c, swparams_c);
288 printf("Unable to determine current swparams_c: %s\n", snd_strerror(err));
293 err = snd_pcm_sw_params_set_tstamp_mode(handle_c, swparams_c, SND_PCM_TSTAMP_ENABLE);
295 printf("Unable to set tstamp mode : %s\n", snd_strerror(err));
299 err = snd_pcm_sw_params_set_tstamp_type(handle_c, swparams_c, TSTAMP_TYPE);
301 printf("Unable to set tstamp type : %s\n", snd_strerror(err));
305 /* write the sw parameters */
306 err = snd_pcm_sw_params(handle_c, swparams_c);
308 printf("Unable to set swparams_c : %s\n", snd_strerror(err));
313 if (do_playback && do_capture) {
315 if ((err = snd_pcm_link(handle_c, handle_p)) < 0) {
316 printf("Streams link error: %s\n", snd_strerror(err));
323 i = PLAYBACK_BUFFERS;
325 frames = snd_pcm_writei(handle_p, buffer_p, PERIOD);
327 printf("snd_pcm_writei failed: %s\n", snd_strerror(frames));
330 frame_count_p += frames;
333 if (PLAYBACK_BUFFERS != 4)
334 snd_pcm_start(handle_p);
339 /* need to start capture explicitly */
340 snd_pcm_start(handle_c);
343 /* need to start capture explicitly */
344 snd_pcm_start(handle_c);
352 frames = snd_pcm_wait(handle_c, -1);
354 printf("snd_pcm_wait failed: %s\n", snd_strerror(frames));
358 frames = snd_pcm_readi(handle_c, buffer_c, PERIOD);
360 printf("snd_pcm_readi failed: %s\n", snd_strerror(frames));
363 frame_count_c += frames;
365 #if defined(TRACK_CAPTURE)
366 audio_tstamp_config_c.type_requested = type;
367 audio_tstamp_config_c.report_delay = do_delay;
368 _gettimestamp(handle_c, &tstamp_c, &trigger_tstamp_c,
369 &audio_tstamp_c, &audio_tstamp_config_c, &audio_tstamp_report_c,
371 #if defined(TRACK_SAMPLE_COUNTS)
372 curr_count_c = frame_count_c + delay_c; /* read plus queued */
375 printf("capture: curr_count %lli driver count %lli, delta %lli\n",
376 (long long)curr_count_c * 1000000000LL / SAMPLE_FREQ ,
377 timestamp2ns(audio_tstamp_c),
378 (long long)curr_count_c * 1000000000LL / SAMPLE_FREQ - timestamp2ns(audio_tstamp_c)
382 if (audio_tstamp_report_c.valid == 0)
383 printf("Audio capture timestamp report invalid - ");
384 if (audio_tstamp_report_c.accuracy_report == 0)
385 printf("Audio capture timestamp accuracy report invalid");
390 printf("\t capture: systime: %lli nsec, audio time %lli nsec, \tsystime delta %lli \t resolution %d ns \n",
391 timediff(tstamp_c, trigger_tstamp_c),
392 timestamp2ns(audio_tstamp_c),
393 timediff(tstamp_c, trigger_tstamp_c) - timestamp2ns(audio_tstamp_c), audio_tstamp_report_c.accuracy
399 frames = snd_pcm_writei(handle_p, buffer_p, PERIOD);
401 printf("snd_pcm_writei failed: %s\n", snd_strerror(frames));
405 frame_count_p += frames;
407 #if defined(TRACK_PLAYBACK)
409 audio_tstamp_config_p.type_requested = type;
410 audio_tstamp_config_p.report_delay = do_delay;
411 _gettimestamp(handle_p, &tstamp_p, &trigger_tstamp_p,
412 &audio_tstamp_p, &audio_tstamp_config_p, &audio_tstamp_report_p,
415 #if defined(TRACK_SAMPLE_COUNTS)
416 curr_count_p = frame_count_p - delay_p; /* written minus queued */
418 printf("playback: curr_count %lli driver count %lli, delta %lli\n",
419 (long long)curr_count_p * 1000000000LL / SAMPLE_FREQ ,
420 timestamp2ns(audio_tstamp_p),
421 (long long)curr_count_p * 1000000000LL / SAMPLE_FREQ - timestamp2ns(audio_tstamp_p)
425 if (audio_tstamp_report_p.valid == 0)
426 printf("Audio playback timestamp report invalid - ");
427 if (audio_tstamp_report_p.accuracy_report == 0)
428 printf("Audio playback timestamp accuracy report invalid");
432 printf("playback: systime: %lli nsec, audio time %lli nsec, \tsystime delta %lli resolution %d ns\n",
433 timediff(tstamp_p, trigger_tstamp_p),
434 timestamp2ns(audio_tstamp_p),
435 timediff(tstamp_p, trigger_tstamp_p) - timestamp2ns(audio_tstamp_p), audio_tstamp_report_p.accuracy
445 snd_pcm_close(handle_p);
447 snd_pcm_close(handle_c);