OSDN Git Service

Switch from old to new PCM API retaining binary compatibility
[android-x86/external-alsa-lib.git] / test / timer.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <sys/time.h>
5 #include "../include/asoundlib.h"
6
7 void show_status(void *handle)
8 {
9         int err;
10         snd_timer_status_t *status;
11         
12         snd_timer_status_alloca(&status);
13         if ((err = snd_timer_status(handle, status)) < 0) {
14                 fprintf(stderr, "timer status %i (%s)\n", err, snd_strerror(err));
15                 return;
16         }
17         printf("STATUS:\n");
18         printf("  resolution = %li\n", snd_timer_status_get_resolution(status));
19         printf("  lost = %li\n", snd_timer_status_get_lost(status));
20         printf("  overrun = %li\n", snd_timer_status_get_overrun(status));
21         printf("  queue = %li\n", snd_timer_status_get_queue(status));
22 }
23
24 void read_loop(void *handle, int master_ticks, int timeout)
25 {
26         int count, err;
27         struct pollfd *fds;
28         snd_timer_read_t tr;
29         
30         count = snd_timer_poll_descriptors_count(handle);
31         fds = calloc(count, sizeof(struct pollfd));
32         if (fds == NULL) {
33                 fprintf(stderr, "malloc error\n");
34                 exit(EXIT_FAILURE);
35         }
36         while (master_ticks-- > 0) {
37                 if ((err = snd_timer_poll_descriptors(handle, fds, count)) < 0) {
38                         fprintf(stderr, "snd_timer_poll_descriptors error: %s\n", snd_strerror(err));
39                         exit(EXIT_FAILURE);
40                 }
41                 if ((err = poll(fds, count, timeout)) < 0) {
42                         fprintf(stderr, "poll error %i (%s)\n", err, strerror(err));
43                         exit(EXIT_FAILURE);
44                 }
45                 if (err == 0) {
46                         fprintf(stderr, "timer time out!!\n");
47                         exit(EXIT_FAILURE);
48                 }
49                 while (snd_timer_read(handle, &tr, sizeof(tr)) == sizeof(tr)) {
50                         printf("TIMER: resolution = %uns, ticks = %u\n",
51                                 tr.resolution, tr.ticks);
52                 }
53         }
54         free(fds);
55 }
56
57 int main(int argc, char *argv[])
58 {
59         int idx, err;
60         int class = SND_TIMER_CLASS_GLOBAL;
61         int sclass = SND_TIMER_CLASS_NONE;
62         int card = 0;
63         int device = SND_TIMER_GLOBAL_SYSTEM;
64         int subdevice = 0;
65         int list = 0;
66         snd_timer_t *handle;
67         snd_timer_id_t *id;
68         snd_timer_info_t *info;
69         snd_timer_params_t *params;
70         char timername[64];
71
72         snd_timer_id_alloca(&id);
73         snd_timer_info_alloca(&info);
74         snd_timer_params_alloca(&params);
75
76         idx = 1;
77         while (idx < argc) {
78                 if (!strncmp(argv[idx], "class=", 5)) {
79                         class = atoi(argv[idx]+6);
80                 } else if (!strncmp(argv[idx], "sclass=", 6)) {
81                         sclass = atoi(argv[idx]+7);
82                 } else if (!strncmp(argv[idx], "card=", 5)) {
83                         card = atoi(argv[idx]+5);
84                 } else if (!strncmp(argv[idx], "device=", 7)) {
85                         device = atoi(argv[idx]+7);
86                 } else if (!strncmp(argv[idx], "subdevice=", 10)) {
87                         subdevice = atoi(argv[idx]+10);
88                 } else if (!strcmp(argv[idx], "list")) {
89                         list = 1;
90                 }
91                 idx++;
92         }
93         if (class == SND_TIMER_CLASS_SLAVE && sclass == SND_TIMER_SCLASS_NONE) {
94                 fprintf(stderr, "slave class is not set\n");
95                 exit(EXIT_FAILURE);
96         }
97         if (list) {
98                 snd_timer_query_t *qhandle;
99                 if ((err = snd_timer_query_open(&qhandle, "hw", 0)) < 0) {
100                         fprintf(stderr, "snd_timer_query_open error: %s\n", snd_strerror(err));
101                         exit(EXIT_FAILURE);
102                 }
103                 snd_timer_id_set_class(id, SND_TIMER_CLASS_NONE);
104                 while (1) {
105                         if ((err = snd_timer_query_next_device(qhandle, id)) < 0) {
106                                 fprintf(stderr, "timer next device error: %s\n", snd_strerror(err));
107                                 break;
108                         }
109                         if (snd_timer_id_get_class(id) < 0)
110                                 break;
111                         printf("Timer device: class %i, sclass %i, card %i, device %i, subdevice %i\n",
112                                         snd_timer_id_get_class(id),
113                                         snd_timer_id_get_sclass(id),
114                                         snd_timer_id_get_card(id),
115                                         snd_timer_id_get_device(id),
116                                         snd_timer_id_get_subdevice(id));
117                 }
118                 snd_timer_query_close(qhandle);
119                 exit(EXIT_SUCCESS);
120         }
121         sprintf(timername, "hw:CLASS=%i,SCLASS=%i,CARD=%i,DEV=%i,SUBDEV=%i", class, sclass, card, device, subdevice);
122         if ((err = snd_timer_open(&handle, timername, SND_TIMER_OPEN_NONBLOCK))<0) {
123                 fprintf(stderr, "timer open %i (%s)\n", err, snd_strerror(err));
124                 exit(EXIT_FAILURE);
125         }
126         printf("Using timer class %i, slave class %i, card %i, device %i, subdevice %i\n", class, sclass, card, device, subdevice);
127         if ((err = snd_timer_info(handle, info)) < 0) {
128                 fprintf(stderr, "timer info %i (%s)\n", err, snd_strerror(err));
129                 exit(0);
130         }
131         printf("Timer info:\n");
132         printf("  slave = %s\n", snd_timer_info_is_slave(info) ? "yes" : "no");
133         printf("  card = %i\n", snd_timer_info_get_card(info));
134         printf("  id = '%s'\n", snd_timer_info_get_id(info));
135         printf("  name = '%s'\n", snd_timer_info_get_name(info));
136         printf("  average resolution = %li\n", snd_timer_info_get_resolution(info));
137         snd_timer_params_set_auto_start(params, 1);
138         if (!snd_timer_info_is_slave(info)) {
139                 snd_timer_params_set_ticks(params, (1000000000 / snd_timer_info_get_resolution(info)) / 50); /* 50Hz */
140                 if (snd_timer_params_get_ticks(params) < 1)
141                         snd_timer_params_set_ticks(params, 1);
142                 printf("Using %li tick(s)\n", snd_timer_params_get_ticks(params));
143         } else {
144                 snd_timer_params_set_ticks(params, 1);
145         }
146         if ((err = snd_timer_params(handle, params)) < 0) {
147                 fprintf(stderr, "timer params %i (%s)\n", err, snd_strerror(err));
148                 exit(0);
149         }
150         show_status(handle);
151         if ((err = snd_timer_start(handle)) < 0) {
152                 fprintf(stderr, "timer start %i (%s)\n", err, snd_strerror(err));
153                 exit(EXIT_FAILURE);
154         }
155         read_loop(handle, 25, snd_timer_info_is_slave(info) ? 10000 : 25);
156         show_status(handle);
157         snd_timer_close(handle);
158         printf("Done\n");
159         return EXIT_SUCCESS;
160 }