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.
7 #include "../include/asoundlib.h"
10 static char *device = "hw:0,0";
12 snd_output_t *output = NULL;
14 long long timestamp2ns(snd_htimestamp_t t)
18 nsec = t.tv_sec * 1000000000;
24 long long timediff(snd_htimestamp_t t1, snd_htimestamp_t t2)
26 long long nsec1, nsec2;
28 nsec1 = timestamp2ns(t1);
29 nsec2 = timestamp2ns(t2);
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)
40 snd_pcm_status_t *status;
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));
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);
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
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];
75 snd_pcm_hw_params_t *hwparams_p;
76 snd_pcm_hw_params_t *hwparams_c;
78 snd_pcm_sw_params_t *swparams_p;
79 snd_pcm_sw_params_t *swparams_c;
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;
86 snd_pcm_sframes_t delay_p, delay_c;
87 snd_pcm_uframes_t avail_p, avail_c;
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));
93 if ((err = snd_pcm_set_params(handle_p,
95 SND_PCM_ACCESS_RW_INTERLEAVED,
99 500000)) < 0) { /* 0.5sec */
100 printf("Playback open error: %s\n", snd_strerror(err));
104 snd_pcm_hw_params_alloca(&hwparams_p);
105 /* get the current hwparams */
106 err = snd_pcm_hw_params_current(handle_p, hwparams_p);
108 printf("Unable to determine current hwparams_p: %s\n", snd_strerror(err));
111 if (snd_pcm_hw_params_supports_audio_wallclock_ts(hwparams_p))
112 printf("Playback relies on audio wallclock timestamps\n");
114 printf("Playback relies on audio sample counter timestamps\n");
116 snd_pcm_sw_params_alloca(&swparams_p);
117 /* get the current swparams */
118 err = snd_pcm_sw_params_current(handle_p, swparams_p);
120 printf("Unable to determine current swparams_p: %s\n", snd_strerror(err));
125 err = snd_pcm_sw_params_set_tstamp_mode(handle_p, swparams_p, SND_PCM_TSTAMP_ENABLE);
127 printf("Unable to set tstamp mode : %s\n", snd_strerror(err));
131 /* write the sw parameters */
132 err = snd_pcm_sw_params(handle_p, swparams_p);
134 printf("Unable to set swparams_p : %s\n", snd_strerror(err));
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));
142 if ((err = snd_pcm_set_params(handle_c,
144 SND_PCM_ACCESS_RW_INTERLEAVED,
148 500000)) < 0) { /* 0.5sec */
149 printf("Capture open error: %s\n", snd_strerror(err));
153 snd_pcm_hw_params_alloca(&hwparams_c);
154 /* get the current hwparams */
155 err = snd_pcm_hw_params_current(handle_c, hwparams_c);
157 printf("Unable to determine current hwparams_c: %s\n", snd_strerror(err));
160 if (snd_pcm_hw_params_supports_audio_wallclock_ts(hwparams_c))
161 printf("Capture relies on audio wallclock timestamps\n");
163 printf("Capture relies on audio sample counter timestamps\n");
165 snd_pcm_sw_params_alloca(&swparams_c);
166 /* get the current swparams */
167 err = snd_pcm_sw_params_current(handle_c, swparams_c);
169 printf("Unable to determine current swparams_c: %s\n", snd_strerror(err));
174 err = snd_pcm_sw_params_set_tstamp_mode(handle_c, swparams_c, SND_PCM_TSTAMP_ENABLE);
176 printf("Unable to set tstamp mode : %s\n", snd_strerror(err));
180 /* write the sw parameters */
181 err = snd_pcm_sw_params(handle_c, swparams_c);
183 printf("Unable to set swparams_c : %s\n", snd_strerror(err));
188 if ((err = snd_pcm_link(handle_c, handle_p)) < 0) {
189 printf("Streams link error: %s\n", snd_strerror(err));
194 i = PLAYBACK_BUFFERS;
196 frames = snd_pcm_writei(handle_p, buffer_p, PERIOD);
198 printf("snd_pcm_writei failed: %s\n", snd_strerror(frames));
201 frame_count_p += frames;
204 if (PLAYBACK_BUFFERS != 4)
205 snd_pcm_start(handle_p);
208 /* need to start capture explicitly */
209 snd_pcm_start(handle_c);
214 frames = snd_pcm_wait(handle_c, -1);
216 printf("snd_pcm_wait failed: %s\n", snd_strerror(frames));
220 frames = snd_pcm_readi(handle_c, buffer_c, PERIOD);
222 printf("snd_pcm_readi failed: %s\n", snd_strerror(frames));
225 frame_count_c += frames;
227 frames = snd_pcm_writei(handle_p, buffer_p, PERIOD);
229 printf("snd_pcm_writei failed: %s\n", snd_strerror(frames));
233 frame_count_p += frames;
235 #if defined(TRACK_PLAYBACK)
236 gettimestamp(handle_p, &tstamp_p, &trigger_tstamp_p, &audio_tstamp_p, &avail_p, &delay_p);
238 #if defined(TRACK_SAMPLE_COUNTS)
239 curr_count_p = frame_count_p - delay_p; /* written minus queued */
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)
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)
255 #if defined(TRACK_CAPTURE)
256 gettimestamp(handle_c, &tstamp_c, &trigger_tstamp_c, &audio_tstamp_c, &avail_c, &delay_c);
258 #if defined(TRACK_SAMPLE_COUNTS)
259 curr_count_c = frame_count_c + delay_c; /* read plus queued */
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)
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)
280 snd_pcm_close(handle_p);
282 snd_pcm_close(handle_c);