e9f06472d8423f14d8de51d4640e3d42f850c48f
[platform/adaptation/spreadtrum/audio-hal-sc7727.git] / tizen-audio-internal.h
1 #ifndef footizenaudiointernalfoo
2 #define footizenaudiointernalfoo
3
4 /*
5  * audio-hal
6  *
7  * Copyright (c) 2015 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  */
22
23 #include <dlog.h>
24 #include <time.h>
25 #include <sys/types.h>
26 #include <asoundlib.h>
27 #ifdef __USE_TINYALSA__
28 #include <tinyalsa/asoundlib.h>
29 #endif
30 #include <time.h>
31 #include <pthread.h>
32 #include <use-case.h>
33 #include "tizen-audio.h"
34 #include "vb_control_parameters.h"
35
36 /* Debug */
37
38 //#define AUDIO_DEBUG
39 #define PROPERTY_VALUE_MAX 92
40 #define BUF_SIZE 1024
41 #define AUDIO_DUMP_STR_LEN              256
42 #define AUDIO_DEVICE_INFO_LIST_MAX      16
43 #ifdef USE_DLOG
44 #ifdef DLOG_TAG
45 #undef DLOG_TAG
46 #endif
47 #define DLOG_TAG "AUDIO_HAL"
48 #define AUDIO_LOG_ERROR(...)            SLOG(LOG_ERROR, DLOG_TAG, __VA_ARGS__)
49 #define AUDIO_LOG_WARN(...)             SLOG(LOG_WARN, DLOG_TAG, __VA_ARGS__)
50 #define AUDIO_LOG_INFO(...)             SLOG(LOG_INFO, DLOG_TAG, __VA_ARGS__)
51 #define AUDIO_LOG_DEBUG(...)            SLOG(LOG_DEBUG, DLOG_TAG, __VA_ARGS__)
52 #define AUDIO_LOG_VERBOSE(...)          SLOG(LOG_DEBUG, DLOG_TAG, __VA_ARGS__)
53 #else
54 #define AUDIO_LOG_ERROR(...)            fprintf(stderr, __VA_ARGS__)
55 #define AUDIO_LOG_WARN(...)             fprintf(stderr, __VA_ARGS__)
56 #define AUDIO_LOG_INFO(...)             fprintf(stdout, __VA_ARGS__)
57 #define AUDIO_LOG_DEBUG(...)            fprintf(stdout, __VA_ARGS__)
58 #define AUDIO_LOG_VERBOSE(...)          fprintf(stdout, __VA_ARGS__)
59 #endif
60
61 #define AUDIO_RETURN_IF_FAIL(expr) do { \
62     if (!expr) { \
63         AUDIO_LOG_ERROR("%s failed", #expr); \
64         return; \
65     } \
66 } while (0)
67 #define AUDIO_RETURN_VAL_IF_FAIL(expr, val) do { \
68     if (!expr) { \
69         AUDIO_LOG_ERROR("%s failed", #expr); \
70         return val; \
71     } \
72 } while (0)
73 #define AUDIO_RETURN_NULL_IF_FAIL(expr) do { \
74     if (!expr) { \
75         AUDIO_LOG_ERROR("%s failed", #expr); \
76         return NULL; \
77     } \
78 } while (0)
79
80 #define MUTEX_LOCK(x_lock, lock_name) { \
81     AUDIO_LOG_DEBUG("try lock [%s]", lock_name); \
82     pthread_mutex_lock(&(x_lock)); \
83     AUDIO_LOG_DEBUG("after lock [%s]", lock_name); \
84 }
85 #define MUTEX_UNLOCK(x_lock, lock_name) { \
86     AUDIO_LOG_DEBUG("try unlock [%s]", lock_name); \
87     pthread_mutex_unlock(&(x_lock)); \
88     AUDIO_LOG_DEBUG("after unlock [%s]", lock_name); \
89 }
90 #define COND_TIMEDWAIT(x_cond, x_lock, x_cond_name, x_timeout_sec) { \
91     AUDIO_LOG_DEBUG("try cond wait [%s]", x_cond_name); \
92     struct timespec ts; \
93     clock_gettime(CLOCK_REALTIME, &ts); \
94     ts.tv_sec += x_timeout_sec; \
95     if (!pthread_cond_timedwait(&(x_cond), &(x_lock), &ts)) \
96         AUDIO_LOG_DEBUG("awaken cond [%s]", x_cond_name); \
97     else \
98         AUDIO_LOG_ERROR("awaken cond [%s] by timeout(%d sec)", x_cond_name, x_timeout_sec); \
99 }
100 #define COND_SIGNAL(x_cond, x_cond_name) { \
101     AUDIO_LOG_DEBUG("send signal to cond [%s]", x_cond_name); \
102     pthread_cond_signal(&(x_cond)); \
103 }
104 #define TIMEOUT_SEC 5
105
106 /* Signal */
107 #define SIGNAL_ROUTING_FOR_VOICE_CALL    "routing_voice_call"
108
109 /* Devices : Normal  */
110 #define AUDIO_DEVICE_OUT               0x00000000
111 #define AUDIO_DEVICE_IN                0x80000000
112 enum audio_device_type {
113     AUDIO_DEVICE_NONE                 = 0,
114
115     /* output devices */
116     AUDIO_DEVICE_OUT_SPEAKER          = AUDIO_DEVICE_OUT | 0x00000001,
117     AUDIO_DEVICE_OUT_RECEIVER         = AUDIO_DEVICE_OUT | 0x00000002,
118     AUDIO_DEVICE_OUT_JACK             = AUDIO_DEVICE_OUT | 0x00000004,
119     AUDIO_DEVICE_OUT_BT_SCO           = AUDIO_DEVICE_OUT | 0x00000008,
120     AUDIO_DEVICE_OUT_ALL              = (AUDIO_DEVICE_OUT_SPEAKER |
121                                          AUDIO_DEVICE_OUT_RECEIVER |
122                                          AUDIO_DEVICE_OUT_JACK |
123                                          AUDIO_DEVICE_OUT_BT_SCO),
124     /* input devices */
125     AUDIO_DEVICE_IN_MAIN_MIC          = AUDIO_DEVICE_IN | 0x00000001,
126     AUDIO_DEVICE_IN_SUB_MIC           = AUDIO_DEVICE_IN | 0x00000002,
127     AUDIO_DEVICE_IN_JACK              = AUDIO_DEVICE_IN | 0x00000004,
128     AUDIO_DEVICE_IN_BT_SCO            = AUDIO_DEVICE_IN | 0x00000008,
129     AUDIO_DEVICE_IN_ALL               = (AUDIO_DEVICE_IN_MAIN_MIC |
130                                          AUDIO_DEVICE_IN_SUB_MIC |
131                                          AUDIO_DEVICE_IN_JACK |
132                                          AUDIO_DEVICE_IN_BT_SCO),
133 };
134
135 typedef struct device_type {
136     uint32_t type;
137     const char *name;
138 } device_type_t;
139
140 /* Verbs */
141 #define AUDIO_USE_CASE_VERB_INACTIVE                "Inactive"
142 #define AUDIO_USE_CASE_VERB_HIFI                    "HiFi"
143 #define AUDIO_USE_CASE_VERB_VOICECALL               "Voice"
144 #define AUDIO_USE_CASE_VERB_VIDEOCALL               "Video"
145 #define AUDIO_USE_CASE_VERB_VOIP                    "VoIP"
146 #define AUDIO_USE_CASE_VERB_LOOPBACK                "Loopback"
147
148 /* Modifiers */
149 #define AUDIO_USE_CASE_MODIFIER_VOICESEARCH              "VoiceSearch"
150 #define AUDIO_USE_CASE_MODIFIER_CAMCORDING               "Camcording"
151 #define AUDIO_USE_CASE_MODIFIER_RINGTONE                 "Ringtone"
152
153 #define streq !strcmp
154 #define strneq strcmp
155
156 #define ALSA_DEFAULT_CARD       "sprdphone"
157 #define VOICE_PCM_DEVICE        "hw:sprdphone,1"
158 #define PLAYBACK_PCM_DEVICE     "hw:sprdphone,0"
159 #define CAPTURE_PCM_DEVICE      "hw:sprdphone,0"
160
161 #define PLAYBACK_CARD_ID        ALSA_DEFAULT_CARD
162 #define PLAYBACK_PCM_DEVICE_ID  0
163
164 #define CAPTURE_CARD_ID         ALSA_DEFAULT_CARD
165 #define CAPTURE_PCM_DEVICE_ID   0
166
167 #define MAX_DEVICES             5
168 #define MAX_MODIFIERS           5
169 #define MAX_NAME_LEN           32
170
171 /* type definitions */
172 typedef signed char int8_t;
173
174 /* PCM */
175 typedef struct {
176     snd_pcm_format_t format;
177     uint32_t rate;
178     uint8_t channels;
179 } audio_pcm_sample_spec_t;
180
181 /* Routing */
182 typedef enum audio_route_mode {
183     VERB_NORMAL,
184     VERB_VOICECALL,
185     VERB_VIDEOCALL,
186     VERB_VOIP
187 } audio_route_mode_t;
188
189 typedef struct audio_hal_device {
190     uint32_t active_in;
191     uint32_t active_out;
192     snd_pcm_t *pcm_in;
193     snd_pcm_t *pcm_out;
194     pthread_mutex_t pcm_lock;
195     uint32_t pcm_count;
196     device_info_t *init_call_devices;
197     uint32_t num_of_call_devices;
198     audio_route_mode_t mode;
199     pthread_cond_t device_cond;
200     pthread_mutex_t device_lock;
201 } audio_hal_device_t;
202
203 /* Volume */
204 #define AUDIO_VOLUME_LEVEL_MAX 16
205
206 typedef enum audio_volume {
207     AUDIO_VOLUME_TYPE_SYSTEM,           /**< System volume type */
208     AUDIO_VOLUME_TYPE_NOTIFICATION,     /**< Notification volume type */
209     AUDIO_VOLUME_TYPE_ALARM,            /**< Alarm volume type */
210     AUDIO_VOLUME_TYPE_RINGTONE,         /**< Ringtone volume type */
211     AUDIO_VOLUME_TYPE_MEDIA,            /**< Media volume type */
212     AUDIO_VOLUME_TYPE_CALL,             /**< Call volume type */
213     AUDIO_VOLUME_TYPE_VOIP,             /**< VOIP volume type */
214     AUDIO_VOLUME_TYPE_VOICE,            /**< Voice volume type */
215     AUDIO_VOLUME_TYPE_MAX,              /**< Volume type count */
216 } audio_volume_t;
217
218 typedef enum audio_gain {
219     AUDIO_GAIN_TYPE_DEFAULT,
220     AUDIO_GAIN_TYPE_DIALER,
221     AUDIO_GAIN_TYPE_TOUCH,
222     AUDIO_GAIN_TYPE_AF,
223     AUDIO_GAIN_TYPE_SHUTTER1,
224     AUDIO_GAIN_TYPE_SHUTTER2,
225     AUDIO_GAIN_TYPE_CAMCODING,
226     AUDIO_GAIN_TYPE_MIDI,
227     AUDIO_GAIN_TYPE_BOOTING,
228     AUDIO_GAIN_TYPE_VIDEO,
229     AUDIO_GAIN_TYPE_TTS,
230     AUDIO_GAIN_TYPE_MAX,
231 } audio_gain_t;
232
233 typedef struct audio_volume_value_table {
234     double volume[AUDIO_VOLUME_TYPE_MAX][AUDIO_VOLUME_LEVEL_MAX];
235     uint32_t volume_level_max[AUDIO_VOLUME_LEVEL_MAX];
236     double gain[AUDIO_GAIN_TYPE_MAX];
237 } audio_volume_value_table_t;
238
239 enum {
240     AUDIO_VOLUME_DEVICE_DEFAULT,
241     AUDIO_VOLUME_DEVICE_MAX,
242 };
243
244 typedef struct audio_hal_volume {
245     uint32_t volume_level[AUDIO_VOLUME_TYPE_MAX];
246     audio_volume_value_table_t *volume_value_table;
247 } audio_hal_volume_t;
248
249 /* UCM */
250 typedef struct audio_hal_ucm {
251     snd_use_case_mgr_t* uc_mgr;
252 } audio_hal_ucm_t;
253
254 /* Mixer */
255 typedef struct audio_hal_mixer {
256     snd_mixer_t *mixer;
257     pthread_mutex_t mutex;
258     struct {
259         snd_ctl_elem_value_t *value;
260         snd_ctl_elem_id_t *id;
261         snd_ctl_elem_info_t *info;
262     } control;
263 } audio_hal_mixer_t;
264
265 /* Audio format */
266 typedef enum audio_sample_format {
267     AUDIO_SAMPLE_U8,
268     AUDIO_SAMPLE_ALAW,
269     AUDIO_SAMPLE_ULAW,
270     AUDIO_SAMPLE_S16LE,
271     AUDIO_SAMPLE_S16BE,
272     AUDIO_SAMPLE_FLOAT32LE,
273     AUDIO_SAMPLE_FLOAT32BE,
274     AUDIO_SAMPLE_S32LE,
275     AUDIO_SAMPLE_S32BE,
276     AUDIO_SAMPLE_S24LE,
277     AUDIO_SAMPLE_S24BE,
278     AUDIO_SAMPLE_S24_32LE,
279     AUDIO_SAMPLE_S24_32BE,
280     AUDIO_SAMPLE_MAX,
281     AUDIO_SAMPLE_INVALID = -1
282 } audio_sample_format_t;
283
284 /* Modem */
285 typedef struct audio_hal_modem {
286     struct {
287         pthread_t voice_thread_handle;
288         pthread_t voip_thread_handle;
289         snd_pcm_t *voice_pcm_handle_p;
290         snd_pcm_t *voice_pcm_handle_c;
291         int exit_vbc_thread;
292         int vbpipe_fd;
293         int vbpipe_voip_fd;
294         unsigned short vbpipe_count;
295     } vbc;
296
297     struct {
298         int fd;
299     } at_cmd;
300
301     audio_modem_t  *cp;
302     cp_type_t cp_type;
303
304     int samplerate;
305     int sim_id;
306     int is_connected;
307 } audio_hal_modem_t;
308
309 typedef struct audio_hal_comm {
310     message_cb msg_cb;
311     void *user_data;
312 } audio_hal_comm_t;
313
314 /* Overall */
315 typedef struct audio_hal {
316     audio_hal_device_t device;
317     audio_hal_volume_t volume;
318     audio_hal_ucm_t ucm;
319     audio_hal_mixer_t mixer;
320     audio_hal_modem_t modem;
321     audio_hal_comm_t comm;
322 } audio_hal_t;
323
324 audio_return_t _audio_volume_init(audio_hal_t *ah);
325 audio_return_t _audio_volume_deinit(audio_hal_t *ah);
326 audio_return_t _audio_routing_init(audio_hal_t *ah);
327 audio_return_t _audio_routing_deinit(audio_hal_t *ah);
328 audio_return_t _audio_stream_init(audio_hal_t *ah);
329 audio_return_t _audio_stream_deinit(audio_hal_t *ah);
330 audio_return_t _audio_pcm_init(audio_hal_t *ah);
331 audio_return_t _audio_pcm_deinit(audio_hal_t *ah);
332 audio_return_t _audio_modem_init(audio_hal_t *ah);
333 audio_return_t _audio_modem_deinit(audio_hal_t *ah);
334 audio_return_t _audio_comm_init(audio_hal_t *ah);
335 audio_return_t _audio_comm_deinit(audio_hal_t *ah);
336
337 audio_return_t _audio_update_route_voicecall(audio_hal_t *ah, device_info_t *devices, int32_t num_of_devices);
338 int _audio_modem_is_call_connected(audio_hal_t *ah);
339 audio_return_t _audio_comm_send_message(audio_hal_t *ah, const char *name, int value);
340
341 typedef struct _dump_data {
342     char *strbuf;
343     int left;
344     char *p;
345 } dump_data_t;
346
347 dump_data_t* _audio_dump_new(int length);
348 void _audio_dump_add_str(dump_data_t *dump, const char *fmt, ...);
349 char* _audio_dump_get_str(dump_data_t *dump);
350 void _audio_dump_free(dump_data_t *dump);
351
352 #endif