Blame test/audio_time.c

Packit Service db8eaa
/*
Packit Service db8eaa
 * This program only tracks the difference between system time
Packit Service db8eaa
 * and audio time, as reported in snd_pcm_status(). It should be
Packit Service db8eaa
 * helpful to verify the information reported by drivers.
Packit Service db8eaa
 */
Packit Service db8eaa
Packit Service db8eaa
#include <stdio.h>
Packit Service db8eaa
#include <malloc.h>
Packit Service db8eaa
#include <unistd.h>
Packit Service db8eaa
#include <stdlib.h>
Packit Service db8eaa
#include <string.h>
Packit Service db8eaa
#include <getopt.h>
Packit Service db8eaa
#include <fcntl.h>
Packit Service db8eaa
#include <ctype.h>
Packit Service db8eaa
#include <errno.h>
Packit Service db8eaa
#include <limits.h>
Packit Service db8eaa
#include <time.h>
Packit Service db8eaa
#include <locale.h>
Packit Service db8eaa
#include <math.h>
Packit Service db8eaa
#include "../include/asoundlib.h"
Packit Service db8eaa
Packit Service db8eaa
static char *command;
Packit Service db8eaa
static char *pcm_name = "hw:0";
Packit Service db8eaa
snd_output_t *output = NULL;
Packit Service db8eaa
Packit Service db8eaa
static void usage(char *command)
Packit Service db8eaa
{
Packit Service db8eaa
	printf("Usage: %s [OPTION]... \n"
Packit Service db8eaa
		"\n"
Packit Service db8eaa
		"-h, --help              help\n"
Packit Service db8eaa
		"-c, --capture           capture tstamps \n"
Packit Service db8eaa
		"-d, --delay             add delay \n"
Packit Service db8eaa
		"-D, --device=NAME       select PCM by name \n"
Packit Service db8eaa
		"-p, --playback          playback tstamps \n"
Packit Service 01c0b7
		"-t, --ts_type=TYPE      Compat(0),default(1),link(2),link_absolute(3),link_estimated(4),link_synchronized(5) \n"
Packit Service db8eaa
		"-r, --report            show audio timestamp and accuracy validity\n"
Packit Service db8eaa
		, command);
Packit Service db8eaa
}
Packit Service db8eaa
Packit Service db8eaa
Packit Service db8eaa
long long timestamp2ns(snd_htimestamp_t t)
Packit Service db8eaa
{
Packit Service db8eaa
	long long nsec;
Packit Service db8eaa
Packit Service db8eaa
	nsec = t.tv_sec * 1000000000;
Packit Service db8eaa
	nsec += t.tv_nsec;
Packit Service db8eaa
Packit Service db8eaa
	return nsec;
Packit Service db8eaa
}
Packit Service db8eaa
Packit Service db8eaa
long long timediff(snd_htimestamp_t t1, snd_htimestamp_t t2)
Packit Service db8eaa
{
Packit Service db8eaa
	long long nsec1, nsec2;
Packit Service db8eaa
Packit Service db8eaa
	nsec1 = timestamp2ns(t1);
Packit Service db8eaa
	nsec2 = timestamp2ns(t2);
Packit Service db8eaa
Packit Service db8eaa
	return nsec1 - nsec2;
Packit Service db8eaa
}
Packit Service db8eaa
Packit Service db8eaa
void _gettimestamp(snd_pcm_t *handle, snd_htimestamp_t *timestamp,
Packit Service db8eaa
		   snd_htimestamp_t *trigger_timestamp,
Packit Service db8eaa
		   snd_htimestamp_t *audio_timestamp,
Packit Service db8eaa
		   snd_pcm_audio_tstamp_config_t  *audio_tstamp_config,
Packit Service db8eaa
		   snd_pcm_audio_tstamp_report_t  *audio_tstamp_report,
Packit Service db8eaa
		   snd_pcm_uframes_t *avail, snd_pcm_sframes_t *delay)
Packit Service db8eaa
{
Packit Service db8eaa
	int err;
Packit Service db8eaa
	snd_pcm_status_t *status;
Packit Service db8eaa
Packit Service db8eaa
	snd_pcm_status_alloca(&status);
Packit Service db8eaa
Packit Service db8eaa
	snd_pcm_status_set_audio_htstamp_config(status, audio_tstamp_config);
Packit Service db8eaa
Packit Service db8eaa
	if ((err = snd_pcm_status(handle, status)) < 0) {
Packit Service db8eaa
		printf("Stream status error: %s\n", snd_strerror(err));
Packit Service db8eaa
		exit(0);
Packit Service db8eaa
	}
Packit Service db8eaa
	snd_pcm_status_get_trigger_htstamp(status, trigger_timestamp);
Packit Service db8eaa
	snd_pcm_status_get_htstamp(status, timestamp);
Packit Service db8eaa
	snd_pcm_status_get_audio_htstamp(status, audio_timestamp);
Packit Service db8eaa
	snd_pcm_status_get_audio_htstamp_report(status, audio_tstamp_report);
Packit Service db8eaa
	*avail = snd_pcm_status_get_avail(status);
Packit Service db8eaa
	*delay = snd_pcm_status_get_delay(status);
Packit Service db8eaa
}
Packit Service db8eaa
Packit Service db8eaa
#define TIMESTAMP_FREQ 8 /* Hz */
Packit Service db8eaa
#define SAMPLE_FREQ 48000
Packit Service db8eaa
#define PERIOD (SAMPLE_FREQ/TIMESTAMP_FREQ)
Packit Service db8eaa
#define PCM_LINK        /* sync start for playback and capture */
Packit Service db8eaa
#define TRACK_CAPTURE   /* dump capture timing info  */
Packit Service db8eaa
#define TRACK_PLAYBACK  /* dump playback timing info */
Packit Service db8eaa
/*#define TRACK_SAMPLE_COUNTS */ /* show difference between sample counters and audiotimestamps returned by driver */
Packit Service db8eaa
#define PLAYBACK_BUFFERS 4
Packit Service db8eaa
#define TSTAMP_TYPE	SND_PCM_TSTAMP_TYPE_MONOTONIC_RAW
Packit Service db8eaa
Packit Service db8eaa
Packit Service db8eaa
int main(int argc, char *argv[])
Packit Service db8eaa
{
Packit Service db8eaa
	int c;
Packit Service db8eaa
	int err;
Packit Service db8eaa
	unsigned int i;
Packit Service db8eaa
	snd_pcm_t *handle_p = NULL;
Packit Service db8eaa
	snd_pcm_t *handle_c = NULL;
Packit Service db8eaa
	snd_pcm_sframes_t frames;
Packit Service db8eaa
	snd_htimestamp_t tstamp_c, tstamp_p;
Packit Service db8eaa
	snd_htimestamp_t trigger_tstamp_c, trigger_tstamp_p;
Packit Service db8eaa
	snd_htimestamp_t audio_tstamp_c, audio_tstamp_p;
Packit Service db8eaa
	unsigned char buffer_p[PERIOD*4*4];
Packit Service db8eaa
	unsigned char buffer_c[PERIOD*4*4];
Packit Service db8eaa
Packit Service db8eaa
	snd_pcm_hw_params_t *hwparams_p;
Packit Service db8eaa
	snd_pcm_hw_params_t *hwparams_c;
Packit Service db8eaa
Packit Service db8eaa
	snd_pcm_sw_params_t *swparams_p;
Packit Service db8eaa
	snd_pcm_sw_params_t *swparams_c;
Packit Service db8eaa
Packit Service db8eaa
	snd_pcm_uframes_t frame_count_c = 0;
Packit Service db8eaa
	snd_pcm_uframes_t frame_count_p = 0;
Packit Service db8eaa
Packit Service db8eaa
	snd_pcm_sframes_t delay_p, delay_c;
Packit Service db8eaa
	snd_pcm_uframes_t avail_p, avail_c;
Packit Service db8eaa
Packit Service db8eaa
	snd_pcm_audio_tstamp_config_t audio_tstamp_config_p;
Packit Service db8eaa
	snd_pcm_audio_tstamp_config_t audio_tstamp_config_c;
Packit Service db8eaa
	snd_pcm_audio_tstamp_report_t audio_tstamp_report_p;
Packit Service db8eaa
	snd_pcm_audio_tstamp_report_t audio_tstamp_report_c;
Packit Service db8eaa
Packit Service db8eaa
	int option_index;
Packit Service db8eaa
	static const char short_options[] = "hcpdrD:t:";
Packit Service db8eaa
Packit Service db8eaa
	static const struct option long_options[] = {
Packit Service db8eaa
		{"capture", 0, 0, 'c'},
Packit Service db8eaa
		{"delay", 0, 0, 'd'},
Packit Service db8eaa
		{"device", required_argument, 0, 'D'},
Packit Service db8eaa
		{"help", no_argument, 0, 'h'},
Packit Service db8eaa
		{"playback", 0, 0, 'p'},
Packit Service db8eaa
		{"ts_type", required_argument, 0, 't'},
Packit Service db8eaa
		{"report", 0, 0, 'r'},
Packit Service db8eaa
		{0, 0, 0, 0}
Packit Service db8eaa
	};
Packit Service db8eaa
Packit Service db8eaa
	int do_delay = 0;
Packit Service db8eaa
	int do_playback = 0;
Packit Service db8eaa
	int do_capture = 0;
Packit Service db8eaa
	int type = 0;
Packit Service db8eaa
	int do_report = 0;
Packit Service db8eaa
Packit Service db8eaa
	while ((c = getopt_long(argc, argv, short_options, long_options, &option_index)) != -1) {
Packit Service db8eaa
		switch (c) {
Packit Service db8eaa
		case 'h':
Packit Service db8eaa
			usage(command);
Packit Service db8eaa
			return 0;
Packit Service db8eaa
		case 'p':
Packit Service db8eaa
			do_playback = 1;
Packit Service db8eaa
			break;
Packit Service db8eaa
		case 'c':
Packit Service db8eaa
			do_capture = 1;
Packit Service db8eaa
			break;
Packit Service db8eaa
		case 'd':
Packit Service db8eaa
			do_delay = 1;
Packit Service db8eaa
			break;
Packit Service db8eaa
		case 'D':
Packit Service db8eaa
			pcm_name = optarg;
Packit Service db8eaa
			break;
Packit Service db8eaa
		case 't':
Packit Service db8eaa
			type = atoi(optarg);
Packit Service db8eaa
			break;
Packit Service db8eaa
		case 'r':
Packit Service db8eaa
			do_report = 1;
Packit Service db8eaa
		}
Packit Service db8eaa
	}
Packit Service db8eaa
Packit Service db8eaa
	memset(&audio_tstamp_config_p, 0, sizeof(snd_pcm_audio_tstamp_config_t));
Packit Service db8eaa
	memset(&audio_tstamp_config_c, 0, sizeof(snd_pcm_audio_tstamp_config_t));
Packit Service db8eaa
	memset(&audio_tstamp_report_p, 0, sizeof(snd_pcm_audio_tstamp_report_t));
Packit Service db8eaa
	memset(&audio_tstamp_report_c, 0, sizeof(snd_pcm_audio_tstamp_report_t));
Packit Service db8eaa
Packit Service db8eaa
	if (do_playback) {
Packit Service db8eaa
		if ((err = snd_pcm_open(&handle_p, pcm_name, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
Packit Service db8eaa
			printf("Playback open error: %s\n", snd_strerror(err));
Packit Service db8eaa
			goto _exit;
Packit Service db8eaa
		}
Packit Service db8eaa
Packit Service db8eaa
		if ((err = snd_pcm_set_params(handle_p,
Packit Service db8eaa
							SND_PCM_FORMAT_S16,
Packit Service db8eaa
							SND_PCM_ACCESS_RW_INTERLEAVED,
Packit Service db8eaa
							2,
Packit Service db8eaa
							SAMPLE_FREQ,
Packit Service db8eaa
							0,
Packit Service db8eaa
							4*1000000/TIMESTAMP_FREQ)) < 0) {
Packit Service db8eaa
			printf("Playback open error: %s\n", snd_strerror(err));
Packit Service db8eaa
			goto _exit;
Packit Service db8eaa
		}
Packit Service db8eaa
Packit Service db8eaa
		snd_pcm_hw_params_alloca(&hwparams_p);
Packit Service db8eaa
/* get the current hwparams */
Packit Service db8eaa
		err = snd_pcm_hw_params_current(handle_p, hwparams_p);
Packit Service db8eaa
		if (err < 0) {
Packit Service db8eaa
			printf("Unable to determine current hwparams_p: %s\n", snd_strerror(err));
Packit Service db8eaa
			goto _exit;
Packit Service db8eaa
		}
Packit Service db8eaa
Packit Service 01c0b7
		if (snd_pcm_hw_params_supports_audio_ts_type(hwparams_p, SND_PCM_AUDIO_TSTAMP_TYPE_COMPAT))
Packit Service db8eaa
			printf("Playback supports audio compat timestamps\n");
Packit Service 01c0b7
		if (snd_pcm_hw_params_supports_audio_ts_type(hwparams_p, SND_PCM_AUDIO_TSTAMP_TYPE_DEFAULT))
Packit Service db8eaa
			printf("Playback supports audio default timestamps\n");
Packit Service 01c0b7
		if (snd_pcm_hw_params_supports_audio_ts_type(hwparams_p, SND_PCM_AUDIO_TSTAMP_TYPE_LINK))
Packit Service db8eaa
			printf("Playback supports audio link timestamps\n");
Packit Service 01c0b7
		if (snd_pcm_hw_params_supports_audio_ts_type(hwparams_p, SND_PCM_AUDIO_TSTAMP_TYPE_LINK_ABSOLUTE))
Packit Service db8eaa
			printf("Playback supports audio link absolute timestamps\n");
Packit Service 01c0b7
		if (snd_pcm_hw_params_supports_audio_ts_type(hwparams_p, SND_PCM_AUDIO_TSTAMP_TYPE_LINK_ESTIMATED))
Packit Service db8eaa
			printf("Playback supports audio link estimated timestamps\n");
Packit Service 01c0b7
		if (snd_pcm_hw_params_supports_audio_ts_type(hwparams_p, SND_PCM_AUDIO_TSTAMP_TYPE_LINK_SYNCHRONIZED))
Packit Service db8eaa
			printf("Playback supports audio link synchronized timestamps\n");
Packit Service db8eaa
Packit Service db8eaa
		snd_pcm_sw_params_alloca(&swparams_p);
Packit Service db8eaa
		/* get the current swparams */
Packit Service db8eaa
		err = snd_pcm_sw_params_current(handle_p, swparams_p);
Packit Service db8eaa
		if (err < 0) {
Packit Service db8eaa
			printf("Unable to determine current swparams_p: %s\n", snd_strerror(err));
Packit Service db8eaa
			goto _exit;
Packit Service db8eaa
		}
Packit Service db8eaa
Packit Service db8eaa
		/* enable tstamp */
Packit Service db8eaa
		err = snd_pcm_sw_params_set_tstamp_mode(handle_p, swparams_p, SND_PCM_TSTAMP_ENABLE);
Packit Service db8eaa
		if (err < 0) {
Packit Service db8eaa
			printf("Unable to set tstamp mode : %s\n", snd_strerror(err));
Packit Service db8eaa
			goto _exit;
Packit Service db8eaa
		}
Packit Service db8eaa
Packit Service db8eaa
		err = snd_pcm_sw_params_set_tstamp_type(handle_p, swparams_p, TSTAMP_TYPE);
Packit Service db8eaa
		if (err < 0) {
Packit Service db8eaa
			printf("Unable to set tstamp type : %s\n", snd_strerror(err));
Packit Service db8eaa
			goto _exit;
Packit Service db8eaa
		}
Packit Service db8eaa
Packit Service db8eaa
		/* write the sw parameters */
Packit Service db8eaa
		err = snd_pcm_sw_params(handle_p, swparams_p);
Packit Service db8eaa
		if (err < 0) {
Packit Service db8eaa
			printf("Unable to set swparams_p : %s\n", snd_strerror(err));
Packit Service db8eaa
			goto _exit;
Packit Service db8eaa
		}
Packit Service db8eaa
Packit Service db8eaa
	}
Packit Service db8eaa
Packit Service db8eaa
	if (do_capture) {
Packit Service db8eaa
Packit Service db8eaa
		if ((err = snd_pcm_open(&handle_c, pcm_name, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK)) < 0) {
Packit Service db8eaa
			printf("Capture open error: %s\n", snd_strerror(err));
Packit Service db8eaa
			goto _exit;
Packit Service db8eaa
		}
Packit Service db8eaa
		if ((err = snd_pcm_set_params(handle_c,
Packit Service db8eaa
							SND_PCM_FORMAT_S16,
Packit Service db8eaa
							SND_PCM_ACCESS_RW_INTERLEAVED,
Packit Service db8eaa
							2,
Packit Service db8eaa
							SAMPLE_FREQ,
Packit Service db8eaa
							0,
Packit Service db8eaa
							4*1000000/TIMESTAMP_FREQ)) < 0) {
Packit Service db8eaa
			printf("Capture open error: %s\n", snd_strerror(err));
Packit Service db8eaa
			goto _exit;
Packit Service db8eaa
		}
Packit Service db8eaa
Packit Service db8eaa
		snd_pcm_hw_params_alloca(&hwparams_c);
Packit Service db8eaa
		/* get the current hwparams */
Packit Service db8eaa
		err = snd_pcm_hw_params_current(handle_c, hwparams_c);
Packit Service db8eaa
		if (err < 0) {
Packit Service db8eaa
			printf("Unable to determine current hwparams_c: %s\n", snd_strerror(err));
Packit Service db8eaa
			goto _exit;
Packit Service db8eaa
		}
Packit Service db8eaa
Packit Service 01c0b7
		if (snd_pcm_hw_params_supports_audio_ts_type(hwparams_c, SND_PCM_AUDIO_TSTAMP_TYPE_COMPAT))
Packit Service db8eaa
			printf("Capture supports audio compat timestamps\n");
Packit Service 01c0b7
		if (snd_pcm_hw_params_supports_audio_ts_type(hwparams_c, SND_PCM_AUDIO_TSTAMP_TYPE_DEFAULT))
Packit Service db8eaa
			printf("Capture supports audio default timestamps\n");
Packit Service 01c0b7
		if (snd_pcm_hw_params_supports_audio_ts_type(hwparams_c, SND_PCM_AUDIO_TSTAMP_TYPE_LINK))
Packit Service db8eaa
			printf("Capture supports audio link timestamps\n");
Packit Service 01c0b7
		if (snd_pcm_hw_params_supports_audio_ts_type(hwparams_c, SND_PCM_AUDIO_TSTAMP_TYPE_LINK_ABSOLUTE))
Packit Service db8eaa
			printf("Capture supports audio link absolute timestamps\n");
Packit Service 01c0b7
		if (snd_pcm_hw_params_supports_audio_ts_type(hwparams_c, SND_PCM_AUDIO_TSTAMP_TYPE_LINK_ESTIMATED))
Packit Service db8eaa
			printf("Capture supports audio link estimated timestamps\n");
Packit Service 01c0b7
		if (snd_pcm_hw_params_supports_audio_ts_type(hwparams_c, SND_PCM_AUDIO_TSTAMP_TYPE_LINK_SYNCHRONIZED))
Packit Service db8eaa
			printf("Capture supports audio link synchronized timestamps\n");
Packit Service db8eaa
Packit Service db8eaa
		snd_pcm_sw_params_alloca(&swparams_c);
Packit Service db8eaa
		/* get the current swparams */
Packit Service db8eaa
		err = snd_pcm_sw_params_current(handle_c, swparams_c);
Packit Service db8eaa
		if (err < 0) {
Packit Service db8eaa
			printf("Unable to determine current swparams_c: %s\n", snd_strerror(err));
Packit Service db8eaa
			goto _exit;
Packit Service db8eaa
		}
Packit Service db8eaa
Packit Service db8eaa
		/* enable tstamp */
Packit Service db8eaa
		err = snd_pcm_sw_params_set_tstamp_mode(handle_c, swparams_c, SND_PCM_TSTAMP_ENABLE);
Packit Service db8eaa
		if (err < 0) {
Packit Service db8eaa
			printf("Unable to set tstamp mode : %s\n", snd_strerror(err));
Packit Service db8eaa
			goto _exit;
Packit Service db8eaa
		}
Packit Service db8eaa
Packit Service db8eaa
		err = snd_pcm_sw_params_set_tstamp_type(handle_c, swparams_c, TSTAMP_TYPE);
Packit Service db8eaa
		if (err < 0) {
Packit Service db8eaa
			printf("Unable to set tstamp type : %s\n", snd_strerror(err));
Packit Service db8eaa
			goto _exit;
Packit Service db8eaa
		}
Packit Service db8eaa
Packit Service db8eaa
		/* write the sw parameters */
Packit Service db8eaa
		err = snd_pcm_sw_params(handle_c, swparams_c);
Packit Service db8eaa
		if (err < 0) {
Packit Service db8eaa
			printf("Unable to set swparams_c : %s\n", snd_strerror(err));
Packit Service db8eaa
			goto _exit;
Packit Service db8eaa
		}
Packit Service db8eaa
	}
Packit Service db8eaa
Packit Service db8eaa
	if (do_playback && do_capture) {
Packit Service db8eaa
#ifdef PCM_LINK
Packit Service db8eaa
		if ((err = snd_pcm_link(handle_c, handle_p)) < 0) {
Packit Service db8eaa
			printf("Streams link error: %s\n", snd_strerror(err));
Packit Service db8eaa
			exit(0);
Packit Service db8eaa
		}
Packit Service db8eaa
#endif
Packit Service db8eaa
	}
Packit Service db8eaa
Packit Service db8eaa
	if (do_playback) {
Packit Service db8eaa
		i = PLAYBACK_BUFFERS;
Packit Service db8eaa
		while (i--) {
Packit Service db8eaa
			frames = snd_pcm_writei(handle_p, buffer_p, PERIOD);
Packit Service db8eaa
			if (frames < 0) {
Packit Service db8eaa
				printf("snd_pcm_writei failed: %s\n", snd_strerror(frames));
Packit Service db8eaa
				goto _exit;
Packit Service db8eaa
			}
Packit Service db8eaa
			frame_count_p += frames;
Packit Service db8eaa
		}
Packit Service db8eaa
Packit Service db8eaa
		if (PLAYBACK_BUFFERS != 4)
Packit Service db8eaa
			snd_pcm_start(handle_p);
Packit Service db8eaa
	}
Packit Service db8eaa
Packit Service db8eaa
	if (do_capture) {
Packit Service db8eaa
#ifndef PCM_LINK
Packit Service db8eaa
		/* need to start capture explicitly */
Packit Service db8eaa
		snd_pcm_start(handle_c);
Packit Service db8eaa
#else
Packit Service db8eaa
		if (!do_playback)
Packit Service db8eaa
			/* need to start capture explicitly */
Packit Service db8eaa
			snd_pcm_start(handle_c);
Packit Service db8eaa
#endif
Packit Service db8eaa
	}
Packit Service db8eaa
Packit Service db8eaa
	while (1) {
Packit Service db8eaa
Packit Service db8eaa
		if (do_capture) {
Packit Service db8eaa
Packit Service db8eaa
			frames = snd_pcm_wait(handle_c, -1);
Packit Service db8eaa
			if (frames < 0) {
Packit Service db8eaa
				printf("snd_pcm_wait failed: %s\n", snd_strerror(frames));
Packit Service db8eaa
				goto _exit;
Packit Service db8eaa
			}
Packit Service db8eaa
Packit Service db8eaa
			frames = snd_pcm_readi(handle_c, buffer_c, PERIOD);
Packit Service db8eaa
			if (frames < 0) {
Packit Service db8eaa
				printf("snd_pcm_readi failed: %s\n", snd_strerror(frames));
Packit Service db8eaa
				goto _exit;
Packit Service db8eaa
			}
Packit Service db8eaa
			frame_count_c += frames;
Packit Service db8eaa
Packit Service db8eaa
#if defined(TRACK_CAPTURE)
Packit Service db8eaa
			audio_tstamp_config_c.type_requested = type;
Packit Service db8eaa
			audio_tstamp_config_c.report_delay = do_delay;
Packit Service db8eaa
			_gettimestamp(handle_c, &tstamp_c, &trigger_tstamp_c,
Packit Service db8eaa
				&audio_tstamp_c, &audio_tstamp_config_c, &audio_tstamp_report_c,
Packit Service db8eaa
				&avail_c, &delay_c);
Packit Service db8eaa
#if defined(TRACK_SAMPLE_COUNTS)
Packit Service db8eaa
			curr_count_c = frame_count_c + delay_c; /* read plus queued */
Packit Service db8eaa
Packit Service db8eaa
Packit Service db8eaa
			printf("capture: curr_count %lli driver count %lli, delta %lli\n",
Packit Service db8eaa
				(long long)curr_count_c * 1000000000LL / SAMPLE_FREQ ,
Packit Service db8eaa
				timestamp2ns(audio_tstamp_c),
Packit Service db8eaa
				(long long)curr_count_c * 1000000000LL / SAMPLE_FREQ - timestamp2ns(audio_tstamp_c)
Packit Service db8eaa
				);
Packit Service db8eaa
#endif
Packit Service db8eaa
			if (do_report) {
Packit Service db8eaa
				if (audio_tstamp_report_c.valid == 0)
Packit Service db8eaa
					printf("Audio capture timestamp report invalid - ");
Packit Service db8eaa
				if (audio_tstamp_report_c.accuracy_report == 0)
Packit Service db8eaa
					printf("Audio capture timestamp accuracy report invalid");
Packit Service db8eaa
				printf("\n");
Packit Service db8eaa
			}
Packit Service db8eaa
Packit Service db8eaa
Packit Service db8eaa
			printf("\t capture: systime: %lli nsec, audio time %lli nsec, \tsystime delta %lli \t resolution %d ns \n", 
Packit Service db8eaa
				timediff(tstamp_c, trigger_tstamp_c),
Packit Service db8eaa
				timestamp2ns(audio_tstamp_c),
Packit Service db8eaa
				timediff(tstamp_c, trigger_tstamp_c) - timestamp2ns(audio_tstamp_c), audio_tstamp_report_c.accuracy
Packit Service db8eaa
				);
Packit Service db8eaa
#endif
Packit Service db8eaa
		}
Packit Service db8eaa
Packit Service db8eaa
		if (do_playback) {
Packit Service db8eaa
			frames = snd_pcm_writei(handle_p, buffer_p, PERIOD);
Packit Service db8eaa
			if (frames < 0) {
Packit Service db8eaa
				printf("snd_pcm_writei failed: %s\n", snd_strerror(frames));
Packit Service db8eaa
				goto _exit;
Packit Service db8eaa
			}
Packit Service db8eaa
Packit Service db8eaa
			frame_count_p += frames;
Packit Service db8eaa
Packit Service db8eaa
#if defined(TRACK_PLAYBACK)
Packit Service db8eaa
Packit Service db8eaa
			audio_tstamp_config_p.type_requested = type;
Packit Service db8eaa
			audio_tstamp_config_p.report_delay = do_delay;
Packit Service db8eaa
			_gettimestamp(handle_p, &tstamp_p, &trigger_tstamp_p,
Packit Service db8eaa
				&audio_tstamp_p, &audio_tstamp_config_p, &audio_tstamp_report_p,
Packit Service db8eaa
				&avail_p, &delay_p);
Packit Service db8eaa
Packit Service db8eaa
#if defined(TRACK_SAMPLE_COUNTS)
Packit Service db8eaa
			curr_count_p = frame_count_p - delay_p; /* written minus queued */
Packit Service db8eaa
Packit Service db8eaa
			printf("playback: curr_count %lli driver count %lli, delta %lli\n",
Packit Service db8eaa
				(long long)curr_count_p * 1000000000LL / SAMPLE_FREQ ,
Packit Service db8eaa
				timestamp2ns(audio_tstamp_p),
Packit Service db8eaa
				(long long)curr_count_p * 1000000000LL / SAMPLE_FREQ - timestamp2ns(audio_tstamp_p)
Packit Service db8eaa
				);
Packit Service db8eaa
#endif
Packit Service db8eaa
			if (do_report) {
Packit Service db8eaa
				if (audio_tstamp_report_p.valid == 0)
Packit Service db8eaa
					printf("Audio playback timestamp report invalid - ");
Packit Service db8eaa
				if (audio_tstamp_report_p.accuracy_report == 0)
Packit Service db8eaa
					printf("Audio playback timestamp accuracy report invalid");
Packit Service db8eaa
				printf("\n");
Packit Service db8eaa
			}
Packit Service db8eaa
Packit Service db8eaa
			printf("playback: systime: %lli nsec, audio time %lli nsec, \tsystime delta %lli resolution %d ns\n",
Packit Service db8eaa
				timediff(tstamp_p, trigger_tstamp_p),
Packit Service db8eaa
				timestamp2ns(audio_tstamp_p),
Packit Service db8eaa
				timediff(tstamp_p, trigger_tstamp_p) - timestamp2ns(audio_tstamp_p), audio_tstamp_report_p.accuracy
Packit Service db8eaa
				);
Packit Service db8eaa
#endif
Packit Service db8eaa
		}
Packit Service db8eaa
Packit Service db8eaa
Packit Service db8eaa
	} /* while(1) */
Packit Service db8eaa
Packit Service db8eaa
_exit:
Packit Service db8eaa
	if (handle_p)
Packit Service db8eaa
		snd_pcm_close(handle_p);
Packit Service db8eaa
	if (handle_c)
Packit Service db8eaa
		snd_pcm_close(handle_c);
Packit Service db8eaa
Packit Service db8eaa
	return 0;
Packit Service db8eaa
}