ALSA project - the C library reference
/test/timer.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include "../include/asoundlib.h"
void show_status(void *handle)
{
int err;
if ((err = snd_timer_status(handle, status)) < 0) {
fprintf(stderr, "timer status %i (%s)\n", err, snd_strerror(err));
return;
}
printf("STATUS:\n");
printf(" resolution = %li\n", snd_timer_status_get_resolution(status));
printf(" lost = %li\n", snd_timer_status_get_lost(status));
printf(" overrun = %li\n", snd_timer_status_get_overrun(status));
printf(" queue = %li\n", snd_timer_status_get_queue(status));
}
void read_loop(void *handle, int master_ticks, int timeout)
{
int count, err;
struct pollfd *fds;
fds = calloc(count, sizeof(struct pollfd));
if (fds == NULL) {
fprintf(stderr, "malloc error\n");
exit(EXIT_FAILURE);
}
while (master_ticks-- > 0) {
if ((err = snd_timer_poll_descriptors(handle, fds, count)) < 0) {
fprintf(stderr, "snd_timer_poll_descriptors error: %s\n", snd_strerror(err));
exit(EXIT_FAILURE);
}
if ((err = poll(fds, count, timeout)) < 0) {
fprintf(stderr, "poll error %i (%s)\n", err, strerror(err));
exit(EXIT_FAILURE);
}
if (err == 0) {
fprintf(stderr, "timer time out!!\n");
exit(EXIT_FAILURE);
}
while (snd_timer_read(handle, &tr, sizeof(tr)) == sizeof(tr)) {
printf("TIMER: resolution = %uns, ticks = %u\n",
tr.resolution, tr.ticks);
}
}
free(fds);
}
static void async_callback(snd_async_handler_t *ahandler)
{
int *acount = snd_async_handler_get_callback_private(ahandler);
while (snd_timer_read(handle, &tr, sizeof(tr)) == sizeof(tr)) {
printf("TIMER: resolution = %uns, ticks = %u\n",
tr.resolution, tr.ticks);
}
(*acount)++;
}
int main(int argc, char *argv[])
{
int idx, err;
int sclass = SND_TIMER_CLASS_NONE;
int card = 0;
int subdevice = 0;
int list = 0;
int async = 0;
int acount = 0;
snd_timer_t *handle;
char timername[64];
idx = 1;
while (idx < argc) {
if (!strncmp(argv[idx], "class=", 5)) {
class = atoi(argv[idx]+6);
} else if (!strncmp(argv[idx], "sclass=", 6)) {
sclass = atoi(argv[idx]+7);
} else if (!strncmp(argv[idx], "card=", 5)) {
card = atoi(argv[idx]+5);
} else if (!strncmp(argv[idx], "device=", 7)) {
device = atoi(argv[idx]+7);
} else if (!strncmp(argv[idx], "subdevice=", 10)) {
subdevice = atoi(argv[idx]+10);
} else if (!strcmp(argv[idx], "list")) {
list = 1;
} else if (!strcmp(argv[idx], "async")) {
async = 1;
}
idx++;
}
if (class == SND_TIMER_CLASS_SLAVE && sclass == SND_TIMER_SCLASS_NONE) {
fprintf(stderr, "slave class is not set\n");
exit(EXIT_FAILURE);
}
if (list) {
if ((err = snd_timer_query_open(&qhandle, "hw", 0)) < 0) {
fprintf(stderr, "snd_timer_query_open error: %s\n", snd_strerror(err));
exit(EXIT_FAILURE);
}
while (1) {
if ((err = snd_timer_query_next_device(qhandle, id)) < 0) {
fprintf(stderr, "timer next device error: %s\n", snd_strerror(err));
break;
}
break;
printf("Timer device: class %i, sclass %i, card %i, device %i, subdevice %i\n",
}
exit(EXIT_SUCCESS);
}
sprintf(timername, "hw:CLASS=%i,SCLASS=%i,CARD=%i,DEV=%i,SUBDEV=%i", class, sclass, card, device, subdevice);
if ((err = snd_timer_open(&handle, timername, SND_TIMER_OPEN_NONBLOCK))<0) {
fprintf(stderr, "timer open %i (%s)\n", err, snd_strerror(err));
exit(EXIT_FAILURE);
}
printf("Using timer class %i, slave class %i, card %i, device %i, subdevice %i\n", class, sclass, card, device, subdevice);
if ((err = snd_timer_info(handle, info)) < 0) {
fprintf(stderr, "timer info %i (%s)\n", err, snd_strerror(err));
exit(0);
}
printf("Timer info:\n");
printf(" slave = %s\n", snd_timer_info_is_slave(info) ? "yes" : "no");
printf(" card = %i\n", snd_timer_info_get_card(info));
printf(" id = '%s'\n", snd_timer_info_get_id(info));
printf(" name = '%s'\n", snd_timer_info_get_name(info));
printf(" average resolution = %li\n", snd_timer_info_get_resolution(info));
snd_timer_params_set_ticks(params, (1000000000 / snd_timer_info_get_resolution(info)) / 50); /* 50Hz */
if (snd_timer_params_get_ticks(params) < 1)
printf("Using %li tick(s)\n", snd_timer_params_get_ticks(params));
} else {
}
if ((err = snd_timer_params(handle, params)) < 0) {
fprintf(stderr, "timer params %i (%s)\n", err, snd_strerror(err));
exit(0);
}
show_status(handle);
if (async) {
err = snd_async_add_timer_handler(&ahandler, handle, async_callback, &acount);
if (err < 0) {
fprintf(stderr, "unable to add async handler %i (%s)\n", err, snd_strerror(err));
exit(EXIT_FAILURE);
}
}
if ((err = snd_timer_start(handle)) < 0) {
fprintf(stderr, "timer start %i (%s)\n", err, snd_strerror(err));
exit(EXIT_FAILURE);
}
if (async) {
/* because all other work is done in the signal handler,
suspend the process */
while (acount < 25)
sleep(1);
snd_timer_stop(handle);
} else {
read_loop(handle, 25, snd_timer_info_is_slave(info) ? 10000 : 25);
}
show_status(handle);
snd_timer_close(handle);
printf("Done\n");
return EXIT_SUCCESS;
}
const char * snd_strerror(int errnum)
Returns the message for an error code.
Definition: error.c:51
struct _snd_async_handler snd_async_handler_t
Internal structure for an async notification client handler.
Definition: global.h:115
void * snd_async_handler_get_callback_private(snd_async_handler_t *handler)
Returns the private data assigned to an async handler.
Definition: async.c:217
int snd_async_add_timer_handler(snd_async_handler_t **handler, snd_timer_t *timer, snd_async_callback_t callback, void *private_data)
Add an async handler for a timer.
Definition: timer.c:301
int snd_timer_query_close(snd_timer_query_t *handle)
close timer query handle
Definition: timer_query.c:200
int snd_timer_close(snd_timer_t *handle)
close timer handle
Definition: timer.c:248
#define SND_TIMER_GLOBAL_SYSTEM
Definition: timer.h:113
struct _snd_timer_query snd_timer_query_t
Definition: timer.h:137
long snd_timer_params_get_ticks(snd_timer_params_t *params)
get timer ticks
Definition: timer.c:712
#define snd_timer_params_alloca(ptr)
Definition: timer.h:218
int snd_timer_id_get_card(snd_timer_id_t *id)
get timer card
Definition: timer_query.c:544
struct _snd_timer_params snd_timer_params_t
Definition: timer.h:57
long snd_timer_info_get_resolution(snd_timer_info_t *info)
get timer resolution in us
Definition: timer.c:539
int snd_timer_params(snd_timer_t *handle, snd_timer_params_t *params)
set parameters for timer handle
Definition: timer.c:778
int snd_timer_id_get_class(snd_timer_id_t *id)
get timer class
Definition: timer_query.c:500
int snd_timer_query_open(snd_timer_query_t **handle, const char *name, int mode)
Opens a new connection to the timer query interface.
Definition: timer_query.c:160
int snd_timer_query_next_device(snd_timer_query_t *handle, snd_timer_id_t *tid)
obtain the next timer identification
Definition: timer_query.c:221
int snd_timer_info(snd_timer_t *handle, snd_timer_info_t *timer)
get information about timer handle
Definition: timer.c:551
#define snd_timer_status_alloca(ptr)
Definition: timer.h:238
const char * snd_timer_info_get_name(snd_timer_info_t *info)
get timer name
Definition: timer.c:527
long snd_timer_status_get_queue(snd_timer_status_t *status)
get count of used queue elements
Definition: timer.c:886
int snd_timer_stop(snd_timer_t *handle)
stop the timer
Definition: timer.c:921
struct _snd_timer_status snd_timer_status_t
Definition: timer.h:59
ssize_t snd_timer_read(snd_timer_t *handle, void *buffer, size_t size)
read bytes using timer handle
Definition: timer.c:944
struct _snd_timer snd_timer_t
Definition: timer.h:139
int snd_timer_id_get_subdevice(snd_timer_id_t *id)
get timer subdevice
Definition: timer_query.c:588
int snd_timer_id_get_sclass(snd_timer_id_t *id)
get timer sub-class
Definition: timer_query.c:522
struct _snd_timer_id snd_timer_id_t
Definition: timer.h:47
int snd_timer_poll_descriptors(snd_timer_t *handle, struct pollfd *pfds, unsigned int space)
get poll descriptors
Definition: timer.c:358
struct _snd_timer_info snd_timer_info_t
Definition: timer.h:55
void snd_timer_id_set_class(snd_timer_id_t *id, int dev_class)
set timer class
Definition: timer_query.c:489
int snd_timer_open(snd_timer_t **handle, const char *name, int mode)
Opens a new connection to the timer interface.
Definition: timer.c:202
void snd_timer_params_set_ticks(snd_timer_params_t *params, long ticks)
set timer ticks
Definition: timer.c:701
int snd_timer_status(snd_timer_t *handle, snd_timer_status_t *status)
get status from timer handle
Definition: timer.c:898
long snd_timer_status_get_lost(snd_timer_status_t *status)
get master tick lost count
Definition: timer.c:864
int snd_timer_id_get_device(snd_timer_id_t *id)
get timer device
Definition: timer_query.c:566
long snd_timer_status_get_resolution(snd_timer_status_t *status)
get resolution in us
Definition: timer.c:853
snd_timer_t * snd_async_handler_get_timer(snd_async_handler_t *handler)
Return timer handle related to an async handler.
Definition: timer.c:331
#define SND_TIMER_OPEN_NONBLOCK
Definition: timer.h:122
const char * snd_timer_info_get_id(snd_timer_info_t *info)
get timer id
Definition: timer.c:516
int snd_timer_params_set_auto_start(snd_timer_params_t *params, int auto_start)
set timer auto start
Definition: timer.c:613
int snd_timer_info_is_slave(snd_timer_info_t *info)
determine, if timer is slave
Definition: timer.c:494
int snd_timer_info_get_card(snd_timer_info_t *info)
get timer card
Definition: timer.c:505
#define snd_timer_id_alloca(ptr)
Definition: timer.h:169
int snd_timer_poll_descriptors_count(snd_timer_t *handle)
get count of poll descriptors for timer handle
Definition: timer.c:345
long snd_timer_status_get_overrun(snd_timer_status_t *status)
get overrun count
Definition: timer.c:875
#define snd_timer_info_alloca(ptr)
Definition: timer.h:205
int snd_timer_start(snd_timer_t *handle)
start the timer
Definition: timer.c:910
@ SND_TIMER_SCLASS_NONE
Definition: timer.h:72
@ SND_TIMER_CLASS_SLAVE
Definition: timer.h:63
@ SND_TIMER_CLASS_NONE
Definition: timer.h:62
@ SND_TIMER_CLASS_GLOBAL
Definition: timer.h:64
Definition: timer.h:100
unsigned int ticks
Definition: timer.h:102
unsigned int resolution
Definition: timer.h:101