tizen beta release
[profile/ivi/avsystem.git] / avsys-audio-ascenario.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 #if !defined(_MMFW_I386_ALL_SIMULATOR)
23 #include <alsa/ascenario.h>
24 #endif
25 #include <string.h>
26
27 #include "avsys-audio-ascenario.h"
28 #include "avsys-types.h"
29 #include "avsys-error.h"
30 #include "avsys-debug.h"
31 #if defined(TIME_CHECK)
32 #include <sys/time.h>
33 #include <time.h>
34 #endif
35
36 #define STR_BUFF_MAX 128
37 #define P_STR_MAX 42
38 #define O_STR_MAX 44
39 static int __avsys_audio_ascn_make_scenario_str(int input, char *buf, int buf_len)
40 {
41         char fromStr[P_STR_MAX] = { 0, };
42         char toStr[P_STR_MAX] = { 0, };
43         char optStr[O_STR_MAX] = { 0, };
44
45         if (buf == NULL) {
46                 avsys_error(AVAUDIO, "Input Buf is null\n");
47                 return AVSYS_STATE_ERR_NULL_POINTER;
48         }
49
50         if (input & INPUT_MAIN_MIC) {
51                 strncpy(fromStr, "mainmic", sizeof(fromStr) - 1);
52         }
53         if (input & INPUT_SUB_MIC) {
54                 strncpy(fromStr, "submic", sizeof(fromStr) - 1);
55         }
56         if (input & INPUT_STEREO_MIC) {
57                 strncpy(fromStr, "stereomic", sizeof(fromStr) - 1);
58         }
59         if (input & INPUT_EAR_MIC) {
60                 strncpy(fromStr, "earmic", sizeof(fromStr) - 1);
61         }
62         if (input & INPUT_BT_MIC) {
63                 strncpy(fromStr, "bt", sizeof(fromStr) - 1);
64         }
65         if (input & INPUT_AP) {
66                 strncpy(fromStr, "ap", sizeof(fromStr) - 1);
67         }
68         if (input & INPUT_CP) {
69                 strncpy(fromStr, "cp", sizeof(fromStr) - 1);
70         }
71         if (input & INPUT_FMRADIO) {
72                 strncpy(fromStr, "fmradio", sizeof(fromStr) - 1);
73         }
74
75         if (input & OUTPUT_HEADSET) {
76                 strncpy(toStr, "headset", sizeof(toStr) - 1);
77         }
78         if (input & OUTPUT_LEFT_SPK) {
79                 strncpy(toStr, "speaker_left", sizeof(toStr) - 1);
80         }
81         if (input & OUTPUT_RIGHT_SPK) {
82                 strncpy(toStr, "speaker_right", sizeof(toStr) - 1);
83         }
84         if (input & OUTPUT_STEREO_SPK) {
85                 strncpy(toStr, "speaker", sizeof(toStr) - 1);
86         }
87         if (input & OUTPUT_RECV) {
88                 strncpy(toStr, "receiver", sizeof(toStr) - 1);
89         }
90         if (input & OUTPUT_BT_HEADSET) {
91                 strncpy(toStr, "bt", sizeof(toStr) - 1);
92         }
93         if (input & OUTPUT_CP) {
94                 strncpy(toStr, "cp", sizeof(toStr) - 1);
95         }
96         if (input & OUTPUT_AP) {
97                 strncpy(toStr, "ap", sizeof(toStr) - 1);
98         }
99
100         if (input & GAIN_MODE) {
101                 strncpy(optStr, "_gain", sizeof(optStr) - 1);
102         }
103         if (input & GAIN_VIDEO_CALL) {
104                 strncpy(optStr, "_videocall_gain", sizeof(optStr) - 1);
105         }
106         if (input & GAIN_VOICE_CALL) {
107                 strncpy(optStr, "_voicecall_gain", sizeof(optStr) - 1);
108         }
109         if (input & GAIN_CALLALERT) {
110                 strncpy(optStr, "_ringtone_gain", sizeof(optStr) - 1);
111         }
112
113         snprintf(buf, buf_len - 1, "%s_to_%s%s", fromStr, toStr, optStr);
114         //avsys_info(AVAUDIO, "rslt str : %s\n", buf);
115         return AVSYS_STATE_SUCCESS;
116 }
117
118 int avsys_audio_ascn_bulk_set(int * bulk, int bulk_cnt, AscnResetType clear)
119 {
120         struct snd_scenario *scn = NULL;
121         char card_name[STR_BUFF_MAX] = { 0, };
122         char str_buf[STR_BUFF_MAX] = { 0, };
123         char reset_str[STR_BUFF_MAX] = { 0, };
124         int err = AVSYS_STATE_SUCCESS;
125         int i = 0;
126 #if defined(TIME_CHECK)
127         struct timeval t_start, t_stop;
128         unsigned long check = 0;
129 #endif
130
131         avsys_info(AVAUDIO, "<< %s, clear = %d\n", __func__, clear);
132
133         if (clear < ASCN_RESET_NONE || clear > ASCN_RESET_MODEM) {
134                 avsys_error_r(AVAUDIO, "invalid clear type %d\n", clear);
135                 return AVSYS_STATE_ERR_RANGE_OVER;
136         }
137
138         if (bulk == NULL) {
139                 avsys_error_r(AVAUDIO, "input bulk is null\n");
140                 return AVSYS_STATE_ERR_NULL_POINTER;
141         }
142         if (bulk_cnt < 0) {
143                 avsys_error_r(AVAUDIO, "bulk_cnt is negative\n");
144                 return AVSYS_STATE_ERR_RANGE_OVER;
145         }
146
147         snprintf(card_name, sizeof(card_name) - 1, "default");
148 #if defined(TIME_CHECK)
149         gettimeofday(&t_start, NULL);
150 #endif
151         //avsys_info(AVAUDIO, "snd_scenario_open() with [%s]\n", card_name);
152         scn = snd_scenario_open(card_name);
153 #if defined(TIME_CHECK)
154         gettimeofday(&t_stop, NULL);
155         if (t_stop.tv_sec == t_start.tv_sec)
156                 check = (t_stop.tv_usec - t_start.tv_usec) / 1000;
157         else
158                 check = (t_stop.tv_sec - t_start.tv_sec) * 1000 + (t_stop.tv_usec - t_start.tv_usec) / 1000;
159         avsys_warning(AVAUDIO, "[snd_scenario_open()] takes %u msec\n", check);
160 #endif
161         if (scn == NULL) {
162                 avsys_error_r(AVAUDIO, "alsa scenario open failed %s\n", card_name);
163                 return AVSYS_STATE_ERR_INTERNAL;
164         }
165         //avsys_info(AVAUDIO, "snd_scenario_open() success\n");
166
167         if (clear != ASCN_RESET_NONE) {
168                 switch (clear) {
169                 case ASCN_RESET_ALL:
170                         snprintf(reset_str, sizeof(reset_str) - 1, "%s", ASCN_STR_RESET);
171                         break;
172                 case ASCN_RESET_PLAYBACK:
173                         snprintf(reset_str, sizeof(reset_str) - 1, "%s", ASCN_STR_RESET_PLAYBACK);
174                         break;
175                 case ASCN_RESET_CAPTURE:
176                         snprintf(reset_str, sizeof(reset_str) - 1, "%s", ASCN_STR_RESET_CAPTURE);
177                         break;
178                 }
179 #if defined(TIME_CHECK)
180                 gettimeofday(&t_start, NULL);
181 #endif
182                 err = snd_scenario_set_scn(scn, reset_str);
183 #if defined(TIME_CHECK)
184                 gettimeofday(&t_stop, NULL);
185                 if (t_stop.tv_sec == t_start.tv_sec)
186                         check = (t_stop.tv_usec - t_start.tv_usec) / 1000;
187                 else
188                         check = (t_stop.tv_sec - t_start.tv_sec) * 1000 + (t_stop.tv_usec - t_start.tv_usec) / 1000;
189                 avsys_warning(AVAUDIO, "[%s] takes %u msec\n", reset_str, check);
190 #endif
191                 if (err < 0) {
192                         avsys_error(AVAUDIO, "Alsa sceanrio [%s] failed\n", reset_str);
193                 } else {
194                         avsys_warning(AVAUDIO, "Set [%s] success\n", reset_str);
195                 }
196         }
197
198         for (i = 0; i < bulk_cnt; i++) {
199                 if (bulk[i] == 0)
200                         continue;
201                 memset(str_buf, '\0', sizeof(str_buf));
202                 //avsys_info(AVAUDIO, "make string for 0x%X\n", bulk[i]);
203                 __avsys_audio_ascn_make_scenario_str(bulk[i], str_buf, sizeof(str_buf));
204                 //avsys_info(AVAUDIO, "result string [%s]\n", str_buf);
205 #if defined(TIME_CHECK)
206                 gettimeofday(&t_start, NULL);
207 #endif
208                 err = snd_scenario_set_scn(scn, str_buf);
209 #if defined(TIME_CHECK)
210                 gettimeofday(&t_stop, NULL);
211                 if (t_stop.tv_sec == t_start.tv_sec)
212                         check = (t_stop.tv_usec - t_start.tv_usec) / 1000;
213                 else
214                         check = (t_stop.tv_sec - t_start.tv_sec) * 1000 + (t_stop.tv_usec - t_start.tv_usec) / 1000;
215                 avsys_warning(AVAUDIO, "[%s] takes %u msec\n", str_buf, check);
216 #endif
217                 if (err < 0) {
218                         avsys_error_r(AVAUDIO, "snd_scenario_set_scn(%s) failed with %d\n", str_buf, err);
219                         goto bulk_error;
220                 }
221                 avsys_warning(AVAUDIO, "* [0x%010X] Set [%s] success\n", bulk[i], str_buf);
222         }
223
224 bulk_error:
225 #if defined(TIME_CHECK)
226         gettimeofday(&t_start, NULL);
227 #endif
228         snd_scenario_close(scn);
229 #if defined(TIME_CHECK)
230         gettimeofday(&t_stop, NULL);
231         if (t_stop.tv_sec == t_start.tv_sec)
232                 check = (t_stop.tv_usec - t_start.tv_usec) / 1000;
233         else
234                 check = (t_stop.tv_sec - t_start.tv_sec) * 1000 + (t_stop.tv_usec - t_start.tv_usec) / 1000;
235         avsys_warning(AVAUDIO, "[snd_scenario_close()] takes %u msec\n", check);
236 #endif
237         return err;
238 }
239
240
241 int avsys_audio_ascn_single_set(char * str)
242 {
243         struct snd_scenario *scn = NULL;
244         char card_name[STR_BUFF_MAX] = { 0, };
245         char cmd_str[STR_BUFF_MAX] = { 0, };
246         int err = AVSYS_STATE_SUCCESS;
247
248         if (str == NULL) {
249                 avsys_error_r(AVAUDIO, "input str is null\n");
250                 return AVSYS_STATE_ERR_NULL_POINTER;
251         }
252
253         snprintf(card_name, sizeof(card_name) - 1, "default");
254 //      avsys_info(AVAUDIO, "snd_scenario_open() with [%s]\n", card_name);
255         scn = snd_scenario_open(card_name);
256         if (scn == NULL) {
257                 avsys_error_r(AVAUDIO, "alsa scenario open failed %s\n", card_name);
258                 return AVSYS_STATE_ERR_INTERNAL;
259         }
260 //      avsys_info(AVAUDIO, "snd_scenario_open() success\n");
261
262         strncpy(cmd_str, str, sizeof(cmd_str) - 1);
263         err = snd_scenario_set_scn(scn, str);
264         avsys_warning(AVAUDIO, "alsa scenario set [%s]\n", str);
265
266         if (err < 0) {
267                 avsys_error_r(AVAUDIO, "snd_scenario_set(%s) failed\n", str);
268         }
269
270         snd_scenario_close(scn);
271
272         return err;
273 }