c1ecdd9a6d2c6a007153ee78799426c06227932e
[profile/ivi/avsystem.git] / audiotest / avsys-audio-test.c
1 /*
2  * avsystem
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jonghyuk Choi <jhchoi.choi@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <stdio.h>
23 #include <unistd.h>
24 #include <alloca.h>
25 #include <memory.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <signal.h>
31 #include <avsys-audio.h>
32
33 #define VERSION (0001)
34
35 void usage(char *name)
36 {
37         printf("Usage: %s [OPTION]... [FILE]...\n\n", name);
38         printf("-h\t\thelp\n");
39         printf("-p\t\tplay raw file (default mode 0) \n");
40         printf("-c\t\tcapture audio to raw file (default mode 0)\n");
41         printf("-m\t\tmode\n");
42         printf("\t\tmode 0 : S16LE / Stereo / 44100Hz\n");
43         printf("\t\tmode 1 : S16LE / Mono / 8000Hz\n");
44         printf("\t\tmode 2 : S16LE / Mono / 22050Hz\n");
45         printf("\t\tmode 3 : U8 / Mono / 8000Hz\n");
46         printf("-r\t\trouting\n");
47         printf("\t\trouting 0 : following policy\n");
48         printf("\t\trouting 1 : forced set to alsa\n");
49         printf("-v\t\volume type\n");
50         printf("\t\tvolume type 0 : system\n");
51         printf("\t\tvolume type 1 : notification\n");
52         printf("\t\tvolume type 2 : alarm\n");
53         printf("\t\tvolume type 3 : ringtone\n");
54         printf("\t\tvolume type 4 : media\n");
55         printf("\t\tvolume type 5 : call\n");
56         printf("\n");
57         return;
58 }
59
60 enum {
61         OP_NONE = -1,
62         OP_PLAYBACK,
63         OP_CAPTURE,
64 };
65
66 #define MODE_MIN 0
67 #define MODE_MAX 3
68
69 static struct sigaction sigterm_action; /* Backup pointer of SIGTERM signal handler */
70 int g_interrupted;
71
72 int __make_param(int op, int mode, int routing, int voltyp, avsys_audio_param_t *param)
73 {
74         if (!param)
75                 return -1;
76
77         if (op == OP_PLAYBACK)
78                 param->mode = AVSYS_AUDIO_MODE_OUTPUT;
79         else if (op == OP_CAPTURE)
80                 param->mode = AVSYS_AUDIO_MODE_INPUT;
81         else
82                 return -1;
83
84         param->priority = AVSYS_AUDIO_PRIORITY_NORMAL;
85         param->vol_type = voltyp;
86         param->handle_route = routing;
87
88         switch (mode) {
89         case 0:
90                 param->format = AVSYS_AUDIO_FORMAT_16BIT;
91                 param->samplerate = 44100;
92                 param->channels = 2;
93                 break;
94         case 1:
95                 param->format = AVSYS_AUDIO_FORMAT_16BIT;
96                 param->samplerate = 8000;
97                 param->channels = 1;
98                 break;
99         case 2:
100                 param->format = AVSYS_AUDIO_FORMAT_16BIT;
101                 param->samplerate = 22050;
102                 param->channels = 1;
103                 break;
104         case 3:
105                 param->format = AVSYS_AUDIO_FORMAT_8BIT;
106                 param->samplerate = 8000;
107                 param->channels = 1;
108                 break;
109         default:
110                 return -1;
111                 break;
112         }
113         return 0;
114 }
115 int _playback(int mode, int routing, int voltyp, char *filename)
116 {
117         int res = 0;
118         avsys_audio_param_t param;
119         avsys_handle_t handle = -1;
120         int recommended_period_size = 0;
121         int fd = -1;
122         char *pcmbuf = NULL;
123         int size = 0;
124
125         if (!filename)
126                 return -1;
127
128         memset(&param, sizeof(avsys_audio_param_t), '\0');
129
130         if (__make_param(OP_PLAYBACK, mode, routing, voltyp, &param)) {
131                 printf("Can not make audio parameter\n");
132                 return -1;
133         }
134
135         res = avsys_audio_open(&param, &handle, &recommended_period_size);
136         if (res != 0) {
137                 printf("Can not open handle 0x%x\n", res);
138                 goto FAIL;
139         }
140
141         pcmbuf = alloca(recommended_period_size);
142         if (!pcmbuf)
143                 goto FAIL;
144
145         fd = open(filename, O_RDONLY);
146         if (fd == -1)
147                 goto FAIL;
148
149         while (((size = read(fd, pcmbuf, recommended_period_size)) > 0) && !g_interrupted) {
150                 if (AVSYS_FAIL(avsys_audio_write(handle, pcmbuf, recommended_period_size))) {
151                         printf("Oops!! audio play fail\n");
152                         break;
153                 }
154         }
155
156         close(fd);
157         avsys_audio_close(handle);
158         return 0;
159 FAIL:
160         if (handle != -1)
161                 avsys_audio_close(handle);
162
163         if (fd != -1)
164                 close(fd);
165
166         return res;
167 }
168
169 int _capture(int mode, int routing, int voltyp, char *filename)
170 {
171         int res = 0;
172         avsys_audio_param_t param;
173         avsys_handle_t handle = -1;
174         int recommended_period_size = 0;
175         int fd = -1;
176         char *pcmbuf = NULL;
177         int size = 0;
178         char namebuffer[64]= {0,};
179
180         if (!filename)
181                 return -1;
182         else
183                 snprintf(namebuffer, sizeof(namebuffer)-1, "%s.raw_%1d", filename, mode);
184
185         printf("[%s] real filename :%s\n", __func__, namebuffer);
186
187         memset(&param, sizeof(avsys_audio_param_t), '\0');
188
189         if (__make_param(OP_CAPTURE, mode, routing, voltyp, &param)) {
190                 printf("Can not make audio parameter\n");
191                 return -1;
192         }
193
194         res = avsys_audio_open(&param, &handle, &recommended_period_size);
195         if (res != 0) {
196                 printf("Can not open handle 0x%x\n", res);
197                 goto FAIL;
198         }
199
200         pcmbuf = alloca(recommended_period_size);
201         if (!pcmbuf) {
202                 printf("Can not alloca pcm buffer\n");
203                 goto FAIL;
204         }
205
206         fd = open(namebuffer, O_WRONLY | O_CREAT, 0644);
207         if (fd == -1) {
208                 printf("Can not open file %s, %s\n", namebuffer, strerror(errno));
209                 goto FAIL;
210         }
211
212         while (((size = avsys_audio_read(handle, pcmbuf, recommended_period_size)) > 0) && !g_interrupted) {
213                 if (-1 == write(fd, pcmbuf, size))
214                         break;
215         }
216
217         close(fd);
218         avsys_audio_close(handle);
219         return 0;
220 FAIL:
221         printf("[%s] FAIL\n",__func__);
222         if (handle != -1)
223                 avsys_audio_close(handle);
224
225         if (fd != -1)
226                 close(fd);
227
228         return res;
229 }
230
231 void sig_handler(int signo)
232 {
233         g_interrupted = 1;
234 }
235
236 int main(int argc, char *argv[])
237 {
238         int opt = 0;
239         int operation = OP_NONE;
240         int mode = 0;
241         int tmp = 0;
242         int routing = 0;
243         int voltyp = 4; /* media */
244         struct sigaction action;
245
246         while ((opt = getopt(argc, argv, "hpcm:r:v:")) != -1) {
247                 switch (opt) {
248                 case 'h':
249                         usage(argv[0]);
250                         return 0;
251                         break;
252                 case 'p':
253                         if (operation != OP_NONE) {
254                                 usage(argv[0]);
255                                 return 0;
256                         }
257                         operation = OP_PLAYBACK;
258                         break;
259                 case 'c':
260                         if (operation != OP_NONE) {
261                                 usage(argv[0]);
262                                 return 0;
263                         }
264                         operation = OP_CAPTURE;
265                         break;
266                 case 'm':
267                         tmp = atoi(optarg);
268                         if (tmp < MODE_MIN || tmp > MODE_MAX) {
269                                 usage(argv[0]);
270                                 printf("MESSAGE : Unsupported mode number\n");
271                                 return -1;
272                         }
273                         mode = tmp;
274                         break;
275                 case 'r':
276                         tmp = atoi(optarg);
277                         if (tmp < 0 || tmp > 1) {
278                                 usage(argv[0]);
279                                 printf("MESSAGE : Unsupported mode number\n");
280                                 return -1;
281                         }
282                         routing = tmp;
283                         break;
284                 case 'v':
285                     tmp = atoi(optarg);
286                     if (tmp < 0 || tmp > AVSYS_AUDIO_VOLUME_TYPE_NUM) {
287                                 usage(argv[0]);
288                                 printf("MESSAGE : invalid volume type\n");
289                                 return -1;
290                     }
291                     voltyp = tmp;
292                     break;
293                 default:
294                         usage(argv[0]);
295                         break;
296                 }
297         }
298
299         action.sa_handler = sig_handler;
300         action.sa_flags = 0;
301         sigemptyset(&action.sa_mask);
302         sigaction(SIGINT, &action, &sigterm_action);
303
304         if (operation == OP_NONE) {
305                 usage(argv[0]);
306                 printf("MESSAGE : Operation is not determined\n");
307                 return -1;
308         } else if (operation == OP_PLAYBACK) {
309                 char *name = NULL;
310                 name = argv[optind++];
311                 printf("op %u, mode %u, routing %d, voltyp %d, name %s\n", operation, mode, routing, voltyp, name);
312                 _playback(mode, routing, voltyp, name);
313         } else if (operation == OP_CAPTURE) {
314                 char *name = NULL;
315                 name = argv[optind++];
316                 if (strlen(name) < 2) {
317                         printf("Insufficient filename length\n");
318                         return -2;
319                 }
320                 printf("op %u, mode %u, routing %d, voltyp %d name %s\n", operation, mode, routing, voltyp, name);
321                 _capture(mode, routing, voltyp, name);
322         }
323
324         return 0;
325 }