blob: dc5c41779d0380d6edcb0f444aaad5f7c8faa93b [file] [log] [blame]
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001/*
2 * Copyright (c) 2013, The Linux Foundation. All rights reserved.
3 * Not a contribution.
4 *
5 * Copyright (C) 2013 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#define LOG_TAG "voice"
21/*#define LOG_NDEBUG 0*/
22#define LOG_NDDEBUG 0
23
24#include <errno.h>
25#include <math.h>
26#include <cutils/log.h>
27#include <cutils/str_parms.h>
28
29#include "audio_hw.h"
30#include "voice.h"
31#include "voice_extn/voice_extn.h"
32#include "platform.h"
33#include "platform_api.h"
Kiran Kandi910e1862013-10-29 13:29:42 -070034#include "audio_extn.h"
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -070035
36struct pcm_config pcm_config_voice_call = {
37 .channels = 1,
38 .rate = 8000,
39 .period_size = 160,
40 .period_count = 2,
41 .format = PCM_FORMAT_S16_LE,
42};
43
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -080044extern const char * const use_case_table[AUDIO_USECASE_MAX];
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -070045
46static struct voice_session *voice_get_session_from_use_case(struct audio_device *adev,
47 audio_usecase_t usecase_id)
48{
49 struct voice_session *session = NULL;
50 int ret = 0;
51
52 ret = voice_extn_get_session_from_use_case(adev, usecase_id, &session);
53 if (ret == -ENOSYS) {
54 session = &adev->voice.session[VOICE_SESS_IDX];
55 }
56
57 return session;
58}
59
60int stop_call(struct audio_device *adev, audio_usecase_t usecase_id)
61{
62 int i, ret = 0;
63 struct audio_usecase *uc_info;
64 struct voice_session *session = NULL;
65
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -080066 ALOGD("%s: enter usecase:%s", __func__, use_case_table[usecase_id]);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -070067
68 session = (struct voice_session *)voice_get_session_from_use_case(adev, usecase_id);
69 session->state.current = CALL_INACTIVE;
70
Vidyakumar Athotad9d9ff32013-11-13 11:46:52 -080071 ret = platform_stop_voice_call(adev->platform, session->vsid);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -070072
73 /* 1. Close the PCM devices */
74 if (session->pcm_rx) {
75 pcm_close(session->pcm_rx);
76 session->pcm_rx = NULL;
77 }
78 if (session->pcm_tx) {
79 pcm_close(session->pcm_tx);
80 session->pcm_tx = NULL;
81 }
82
83 uc_info = get_usecase_from_list(adev, usecase_id);
84 if (uc_info == NULL) {
85 ALOGE("%s: Could not find the usecase (%d) in the list",
86 __func__, usecase_id);
87 return -EINVAL;
88 }
89
90 /* 2. Get and set stream specific mixer controls */
91 disable_audio_route(adev, uc_info, true);
92
93 /* 3. Disable the rx and tx devices */
94 disable_snd_device(adev, uc_info->out_snd_device, false);
95 disable_snd_device(adev, uc_info->in_snd_device, true);
96
Kiran Kandi910e1862013-10-29 13:29:42 -070097 audio_extn_listen_update_status(uc_info,
98 LISTEN_EVENT_AUDIO_CAPTURE_INACTIVE);
99
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700100 list_remove(&uc_info->list);
101 free(uc_info);
102
103 ALOGD("%s: exit: status(%d)", __func__, ret);
104 return ret;
105}
106
107int start_call(struct audio_device *adev, audio_usecase_t usecase_id)
108{
109 int i, ret = 0;
110 struct audio_usecase *uc_info;
111 int pcm_dev_rx_id, pcm_dev_tx_id;
112 struct voice_session *session = NULL;
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700113 struct pcm_config voice_config = pcm_config_voice_call;
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800114
115 ALOGD("%s: enter usecase:%s", __func__, use_case_table[usecase_id]);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700116
117 session = (struct voice_session *)voice_get_session_from_use_case(adev, usecase_id);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700118 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
119 uc_info->id = usecase_id;
120 uc_info->type = VOICE_CALL;
121 uc_info->stream.out = adev->primary_output;
122 uc_info->devices = adev->primary_output->devices;
123 uc_info->in_snd_device = SND_DEVICE_NONE;
124 uc_info->out_snd_device = SND_DEVICE_NONE;
125
126 list_add_tail(&adev->usecase_list, &uc_info->list);
127
128 select_devices(adev, usecase_id);
129
Kiran Kandi910e1862013-10-29 13:29:42 -0700130 audio_extn_listen_update_status(uc_info,
131 LISTEN_EVENT_AUDIO_CAPTURE_ACTIVE);
132
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700133 pcm_dev_rx_id = platform_get_pcm_device_id(uc_info->id, PCM_PLAYBACK);
134 pcm_dev_tx_id = platform_get_pcm_device_id(uc_info->id, PCM_CAPTURE);
135
136 if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0) {
137 ALOGE("%s: Invalid PCM devices (rx: %d tx: %d) for the usecase(%d)",
138 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
139 ret = -EIO;
140 goto error_start_voice;
141 }
142
143 ALOGV("%s: Opening PCM playback device card_id(%d) device_id(%d)",
144 __func__, SOUND_CARD, pcm_dev_rx_id);
145 session->pcm_rx = pcm_open(SOUND_CARD,
146 pcm_dev_rx_id,
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700147 PCM_OUT, &voice_config);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700148 if (session->pcm_rx && !pcm_is_ready(session->pcm_rx)) {
149 ALOGE("%s: %s", __func__, pcm_get_error(session->pcm_rx));
150 ret = -EIO;
151 goto error_start_voice;
152 }
153
154 ALOGV("%s: Opening PCM capture device card_id(%d) device_id(%d)",
155 __func__, SOUND_CARD, pcm_dev_tx_id);
156 session->pcm_tx = pcm_open(SOUND_CARD,
157 pcm_dev_tx_id,
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700158 PCM_IN, &voice_config);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700159 if (session->pcm_tx && !pcm_is_ready(session->pcm_tx)) {
160 ALOGE("%s: %s", __func__, pcm_get_error(session->pcm_tx));
161 ret = -EIO;
162 goto error_start_voice;
163 }
164 pcm_start(session->pcm_rx);
165 pcm_start(session->pcm_tx);
166
Shruthi Krishnaace10852013-10-25 14:32:12 -0700167 voice_set_volume(adev, adev->voice.volume);
168
Vidyakumar Athotad9d9ff32013-11-13 11:46:52 -0800169 ret = platform_start_voice_call(adev->platform, session->vsid);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700170 if (ret < 0) {
171 ALOGE("%s: platform_start_voice_call error %d\n", __func__, ret);
172 goto error_start_voice;
173 }
174
175 session->state.current = CALL_ACTIVE;
176 return 0;
177
178error_start_voice:
179 stop_call(adev, usecase_id);
180
181 ALOGD("%s: exit: status(%d)", __func__, ret);
182 return ret;
183}
184
185bool voice_is_in_call(struct audio_device *adev)
186{
187 bool in_call = false;
188 int ret = 0;
189
190 ret = voice_extn_is_in_call(adev, &in_call);
191 if (ret == -ENOSYS) {
192 in_call = (adev->voice.session[VOICE_SESS_IDX].state.current == CALL_ACTIVE) ? true : false;
193 }
194
195 return in_call;
196}
197
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700198uint32_t voice_get_active_session_id(struct audio_device *adev)
199{
200 int ret = 0;
201 uint32_t session_id;
202
203 ret = voice_extn_get_active_session_id(adev, &session_id);
204 if (ret == -ENOSYS) {
205 session_id = VOICE_VSID;
206 }
207 return session_id;
208}
209
210int voice_check_and_set_incall_rec_usecase(struct audio_device *adev,
211 struct stream_in *in)
212{
213 int ret = 0;
214 uint32_t session_id;
215 int usecase_id;
216
217 if (voice_is_in_call(adev)) {
218 switch (in->source) {
219 case AUDIO_SOURCE_VOICE_UPLINK:
220 in->usecase = USECASE_INCALL_REC_UPLINK;
221 break;
222 case AUDIO_SOURCE_VOICE_DOWNLINK:
223 in->usecase = USECASE_INCALL_REC_DOWNLINK;
224 break;
225 case AUDIO_SOURCE_VOICE_CALL:
226 in->usecase = USECASE_INCALL_REC_UPLINK_AND_DOWNLINK;
227 break;
228 default:
229 ALOGV("%s: Source type %d doesnt match incall recording criteria",
230 __func__, in->source);
231 return ret;
232 }
233
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700234 session_id = voice_get_active_session_id(adev);
235 ret = platform_set_incall_recoding_session_id(adev->platform,
236 session_id);
237 ALOGV("%s: Update usecase to %d",__func__, in->usecase);
238 } else {
239 ALOGV("%s: voice call not active", __func__);
240 }
241
242 return ret;
243}
244
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -0700245int voice_check_and_set_incall_music_usecase(struct audio_device *adev,
246 struct stream_out *out)
247{
248 int ret = 0;
249
250 ret = voice_extn_check_and_set_incall_music_usecase(adev, out);
251 if (ret == -ENOSYS) {
252 /* Incall music delivery is used only for LCH call state */
253 ret = -EINVAL;
254 }
255
256 return ret;
257}
258
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700259int voice_set_mic_mute(struct audio_device *adev, bool state)
260{
261 int err = 0;
262
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800263 adev->voice.mic_mute = state;
264 if (adev->mode == AUDIO_MODE_IN_CALL)
265 err = platform_set_mic_mute(adev->platform, state);
266 if (adev->mode == AUDIO_MODE_IN_COMMUNICATION)
267 err = voice_extn_compress_voip_set_mic_mute(adev, state);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700268
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700269 return err;
270}
271
272bool voice_get_mic_mute(struct audio_device *adev)
273{
274 return adev->voice.mic_mute;
275}
276
277int voice_set_volume(struct audio_device *adev, float volume)
278{
279 int vol, err = 0;
280
Shruthi Krishnaace10852013-10-25 14:32:12 -0700281 adev->voice.volume = volume;
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700282 if (adev->mode == AUDIO_MODE_IN_CALL) {
283 if (volume < 0.0) {
284 volume = 0.0;
285 } else if (volume > 1.0) {
286 volume = 1.0;
287 }
288
289 vol = lrint(volume * 100.0);
290
291 // Voice volume levels from android are mapped to driver volume levels as follows.
292 // 0 -> 5, 20 -> 4, 40 ->3, 60 -> 2, 80 -> 1, 100 -> 0
293 // So adjust the volume to get the correct volume index in driver
294 vol = 100 - vol;
295
296 err = platform_set_voice_volume(adev->platform, vol);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700297 }
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800298 if (adev->mode == AUDIO_MODE_IN_COMMUNICATION)
299 err = voice_extn_compress_voip_set_volume(adev, volume);
300
Shruthi Krishnaace10852013-10-25 14:32:12 -0700301
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700302 return err;
303}
304
305int voice_start_call(struct audio_device *adev)
306{
307 int ret = 0;
308
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800309 ret = voice_extn_start_call(adev);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700310 if (ret == -ENOSYS) {
311 ret = start_call(adev, USECASE_VOICE_CALL);
312 }
313
314 return ret;
315}
316
317int voice_stop_call(struct audio_device *adev)
318{
319 int ret = 0;
320
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800321 ret = voice_extn_stop_call(adev);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700322 if (ret == -ENOSYS) {
323 ret = stop_call(adev, USECASE_VOICE_CALL);
324 }
325
326 return ret;
327}
328
329int voice_set_parameters(struct audio_device *adev, struct str_parms *parms)
330{
331 char *str;
332 char value[32];
333 int val;
334 int ret = 0;
335
336 ALOGV("%s: enter: %s", __func__, str_parms_to_str(parms));
337
338 voice_extn_set_parameters(adev, parms);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800339 voice_extn_compress_voip_set_parameters(adev, parms);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700340
341 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_TTY_MODE, value, sizeof(value));
342 if (ret >= 0) {
343 int tty_mode;
344 str_parms_del(parms, AUDIO_PARAMETER_KEY_TTY_MODE);
345 if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_OFF) == 0)
346 tty_mode = TTY_MODE_OFF;
347 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_VCO) == 0)
348 tty_mode = TTY_MODE_VCO;
349 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_HCO) == 0)
350 tty_mode = TTY_MODE_HCO;
351 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_FULL) == 0)
352 tty_mode = TTY_MODE_FULL;
353 else {
354 ret = -EINVAL;
355 goto done;
356 }
357
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700358 if (tty_mode != adev->voice.tty_mode) {
359 adev->voice.tty_mode = tty_mode;
360 adev->acdb_settings = (adev->acdb_settings & TTY_MODE_CLEAR) | tty_mode;
361 if (voice_is_in_call(adev))
362 //todo: what about voice2, volte and qchat usecases?
363 select_devices(adev, USECASE_VOICE_CALL);
364 }
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700365 }
366
367done:
368 ALOGV("%s: exit with code(%d)", __func__, ret);
369 return ret;
370}
371
372void voice_init(struct audio_device *adev)
373{
374 int i = 0;
375
376 memset(&adev->voice, 0, sizeof(adev->voice));
377 adev->voice.tty_mode = TTY_MODE_OFF;
378 adev->voice.volume = 1.0f;
379 adev->voice.mic_mute = false;
380 for (i = 0; i < MAX_VOICE_SESSIONS; i++) {
381 adev->voice.session[i].pcm_rx = NULL;
382 adev->voice.session[i].pcm_tx = NULL;
383 adev->voice.session[i].state.current = CALL_INACTIVE;
384 adev->voice.session[i].state.new = CALL_INACTIVE;
385 adev->voice.session[i].vsid = 0;
386 }
387
388 voice_extn_init(adev);
389}
390
391