1 /*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define LOG_TAG "voice"
18 /*#define LOG_NDEBUG 0*/
19 #define LOG_NDDEBUG 0
20
21 #include <stdlib.h>
22 #include <errno.h>
23 #include <math.h>
24 #include <cutils/log.h>
25 #include <cutils/str_parms.h>
26
27 #include "audio_hw.h"
28 #include "voice.h"
29 #include "voice_extn/voice_extn.h"
30 #include "platform.h"
31 #include "platform_api.h"
32
33 struct pcm_config pcm_config_voice_call = {
34 .channels = 1,
35 .rate = 8000,
36 .period_size = 160,
37 .period_count = 2,
38 .format = PCM_FORMAT_S16_LE,
39 };
40
voice_get_session_from_use_case(struct audio_device * adev,audio_usecase_t usecase_id)41 static struct voice_session *voice_get_session_from_use_case(struct audio_device *adev,
42 audio_usecase_t usecase_id)
43 {
44 struct voice_session *session = NULL;
45 int ret = 0;
46
47 ret = voice_extn_get_session_from_use_case(adev, usecase_id, &session);
48 if (ret == -ENOSYS) {
49 session = &adev->voice.session[VOICE_SESS_IDX];
50 }
51
52 return session;
53 }
54
voice_is_sidetone_device(snd_device_t out_device,char * mixer_path)55 static bool voice_is_sidetone_device(snd_device_t out_device,
56 char *mixer_path)
57 {
58 bool is_sidetone_dev = true;
59
60 switch (out_device) {
61 case SND_DEVICE_OUT_VOICE_HAC_HANDSET:
62 strlcpy(mixer_path, "sidetone-hac-handset", MIXER_PATH_MAX_LENGTH);
63 break;
64 case SND_DEVICE_OUT_VOICE_HANDSET:
65 strlcpy(mixer_path, "sidetone-handset", MIXER_PATH_MAX_LENGTH);
66 break;
67 case SND_DEVICE_OUT_VOICE_HEADPHONES:
68 strlcpy(mixer_path, "sidetone-headphones", MIXER_PATH_MAX_LENGTH);
69 break;
70 default:
71 is_sidetone_dev = false;
72 break;
73 }
74
75 return is_sidetone_dev;
76 }
77
voice_set_sidetone(struct audio_device * adev,snd_device_t out_snd_device,bool enable)78 void voice_set_sidetone(struct audio_device *adev,
79 snd_device_t out_snd_device, bool enable)
80 {
81 char mixer_path[MIXER_PATH_MAX_LENGTH];
82 bool is_sidetone_dev;
83
84 ALOGD("%s: %s, out_snd_device: %d\n",
85 __func__, (enable ? "enable" : "disable"),
86 out_snd_device);
87
88 is_sidetone_dev = voice_is_sidetone_device(out_snd_device, mixer_path);
89
90 if (!is_sidetone_dev) {
91 ALOGD("%s: device %d does not support sidetone\n",
92 __func__, out_snd_device);
93 return;
94 }
95
96 ALOGD("%s: sidetone out device = %s\n",
97 __func__, mixer_path);
98
99 if (enable)
100 audio_route_apply_and_update_path(adev->audio_route, mixer_path);
101 else
102 audio_route_reset_and_update_path(adev->audio_route, mixer_path);
103
104 return;
105 }
106
voice_stop_usecase(struct audio_device * adev,audio_usecase_t usecase_id)107 int voice_stop_usecase(struct audio_device *adev, audio_usecase_t usecase_id)
108 {
109 int i, ret = 0;
110 struct audio_usecase *uc_info;
111 struct voice_session *session = NULL;
112
113 ALOGD("%s: enter usecase:%s", __func__, use_case_table[usecase_id]);
114
115 session = (struct voice_session *)voice_get_session_from_use_case(adev, usecase_id);
116
117 uc_info = get_usecase_from_list(adev, usecase_id);
118 if (uc_info == NULL) {
119 ALOGE("%s: Could not find the usecase (%d) in the list",
120 __func__, usecase_id);
121 return -EINVAL;
122 }
123
124 session->state.current = CALL_INACTIVE;
125
126 /* Disable sidetone only when no calls are active */
127 if (!voice_is_call_state_active(adev))
128 voice_set_sidetone(adev, uc_info->out_snd_device, false);
129
130 ret = platform_stop_voice_call(adev->platform, session->vsid);
131
132 /* 1. Close the PCM devices */
133 if (session->pcm_rx) {
134 pcm_close(session->pcm_rx);
135 session->pcm_rx = NULL;
136 }
137 if (session->pcm_tx) {
138 pcm_close(session->pcm_tx);
139 session->pcm_tx = NULL;
140 }
141
142 /* 2. Get and set stream specific mixer controls */
143 disable_audio_route(adev, uc_info);
144
145 /* 3. Disable the rx and tx devices */
146 disable_snd_device(adev, uc_info->out_snd_device);
147 disable_snd_device(adev, uc_info->in_snd_device);
148
149 list_remove(&uc_info->list);
150 free(uc_info);
151
152 ALOGD("%s: exit: status(%d)", __func__, ret);
153 return ret;
154 }
155
voice_start_usecase(struct audio_device * adev,audio_usecase_t usecase_id)156 int voice_start_usecase(struct audio_device *adev, audio_usecase_t usecase_id)
157 {
158 int i, ret = 0;
159 struct audio_usecase *uc_info;
160 int pcm_dev_rx_id, pcm_dev_tx_id;
161 struct voice_session *session = NULL;
162 struct pcm_config voice_config = pcm_config_voice_call;
163
164 ALOGD("%s: enter usecase:%s", __func__, use_case_table[usecase_id]);
165
166 session = (struct voice_session *)voice_get_session_from_use_case(adev, usecase_id);
167 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
168 uc_info->id = usecase_id;
169 uc_info->type = VOICE_CALL;
170 uc_info->stream.out = adev->current_call_output ;
171 uc_info->devices = adev->current_call_output ->devices;
172 uc_info->in_snd_device = SND_DEVICE_NONE;
173 uc_info->out_snd_device = SND_DEVICE_NONE;
174
175 list_add_tail(&adev->usecase_list, &uc_info->list);
176
177 select_devices(adev, usecase_id);
178
179 pcm_dev_rx_id = platform_get_pcm_device_id(uc_info->id, PCM_PLAYBACK);
180 pcm_dev_tx_id = platform_get_pcm_device_id(uc_info->id, PCM_CAPTURE);
181
182 if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0) {
183 ALOGE("%s: Invalid PCM devices (rx: %d tx: %d) for the usecase(%d)",
184 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
185 ret = -EIO;
186 goto error_start_voice;
187 }
188
189 ALOGV("%s: Opening PCM capture device card_id(%d) device_id(%d)",
190 __func__, adev->snd_card, pcm_dev_tx_id);
191 session->pcm_tx = pcm_open(adev->snd_card,
192 pcm_dev_tx_id,
193 PCM_IN, &voice_config);
194 if (session->pcm_tx && !pcm_is_ready(session->pcm_tx)) {
195 ALOGE("%s: %s", __func__, pcm_get_error(session->pcm_tx));
196 ret = -EIO;
197 goto error_start_voice;
198 }
199
200 ALOGV("%s: Opening PCM playback device card_id(%d) device_id(%d)",
201 __func__, adev->snd_card, pcm_dev_rx_id);
202 session->pcm_rx = pcm_open(adev->snd_card,
203 pcm_dev_rx_id,
204 PCM_OUT, &voice_config);
205 if (session->pcm_rx && !pcm_is_ready(session->pcm_rx)) {
206 ALOGE("%s: %s", __func__, pcm_get_error(session->pcm_rx));
207 ret = -EIO;
208 goto error_start_voice;
209 }
210
211 pcm_start(session->pcm_tx);
212 pcm_start(session->pcm_rx);
213
214 /* Enable sidetone only when no calls are already active */
215 if (!voice_is_call_state_active(adev))
216 voice_set_sidetone(adev, uc_info->out_snd_device, true);
217
218 voice_set_volume(adev, adev->voice.volume);
219
220 ret = platform_start_voice_call(adev->platform, session->vsid);
221 if (ret < 0) {
222 ALOGE("%s: platform_start_voice_call error %d\n", __func__, ret);
223 goto error_start_voice;
224 }
225
226 session->state.current = CALL_ACTIVE;
227 goto done;
228
229 error_start_voice:
230 voice_stop_usecase(adev, usecase_id);
231
232 done:
233 ALOGD("%s: exit: status(%d)", __func__, ret);
234 return ret;
235 }
236
voice_is_call_state_active(struct audio_device * adev)237 bool voice_is_call_state_active(struct audio_device *adev)
238 {
239 bool call_state = false;
240 int ret = 0;
241
242 ret = voice_extn_is_call_state_active(adev, &call_state);
243 if (ret == -ENOSYS) {
244 call_state = (adev->voice.session[VOICE_SESS_IDX].state.current == CALL_ACTIVE) ? true : false;
245 }
246
247 return call_state;
248 }
249
voice_is_in_call(struct audio_device * adev)250 bool voice_is_in_call(struct audio_device *adev)
251 {
252 return adev->voice.in_call;
253 }
254
voice_is_in_call_rec_stream(struct stream_in * in)255 bool voice_is_in_call_rec_stream(struct stream_in *in)
256 {
257 bool in_call_rec = false;
258 int ret = 0;
259
260 ret = voice_extn_is_in_call_rec_stream(in, &in_call_rec);
261 if (ret == -ENOSYS) {
262 in_call_rec = false;
263 }
264
265 return in_call_rec;
266 }
267
voice_get_active_session_id(struct audio_device * adev)268 uint32_t voice_get_active_session_id(struct audio_device *adev)
269 {
270 int ret = 0;
271 uint32_t session_id;
272
273 ret = voice_extn_get_active_session_id(adev, &session_id);
274 if (ret == -ENOSYS) {
275 session_id = VOICE_VSID;
276 }
277 return session_id;
278 }
279
voice_check_and_set_incall_rec_usecase(struct audio_device * adev,struct stream_in * in)280 int voice_check_and_set_incall_rec_usecase(struct audio_device *adev,
281 struct stream_in *in)
282 {
283 int ret = 0;
284 uint32_t session_id;
285 int usecase_id;
286 int rec_mode = INCALL_REC_NONE;
287
288 if (voice_is_call_state_active(adev)) {
289 switch (in->source) {
290 case AUDIO_SOURCE_VOICE_UPLINK:
291 in->usecase = USECASE_INCALL_REC_UPLINK;
292 rec_mode = INCALL_REC_UPLINK;
293 break;
294 case AUDIO_SOURCE_VOICE_DOWNLINK:
295 in->usecase = USECASE_INCALL_REC_DOWNLINK;
296 rec_mode = INCALL_REC_DOWNLINK;
297 break;
298 case AUDIO_SOURCE_VOICE_CALL:
299 in->usecase = USECASE_INCALL_REC_UPLINK_AND_DOWNLINK;
300 rec_mode = INCALL_REC_UPLINK_AND_DOWNLINK;
301 break;
302 default:
303 ALOGV("%s: Source type %d doesnt match incall recording criteria",
304 __func__, in->source);
305 return ret;
306 }
307
308 session_id = voice_get_active_session_id(adev);
309 ret = platform_set_incall_recording_session_id(adev->platform,
310 session_id, rec_mode);
311 ALOGV("%s: Update usecase to %d",__func__, in->usecase);
312 } else {
313 ALOGV("%s: voice call not active", __func__);
314 }
315
316 return ret;
317 }
318
voice_check_and_stop_incall_rec_usecase(struct audio_device * adev,struct stream_in * in)319 int voice_check_and_stop_incall_rec_usecase(struct audio_device *adev,
320 struct stream_in *in)
321 {
322 int ret = 0;
323
324 if (in->source == AUDIO_SOURCE_VOICE_UPLINK ||
325 in->source == AUDIO_SOURCE_VOICE_DOWNLINK ||
326 in->source == AUDIO_SOURCE_VOICE_CALL) {
327 ret = platform_stop_incall_recording_usecase(adev->platform);
328 ALOGV("%s: Stop In-call recording", __func__);
329 }
330
331 return ret;
332 }
333
voice_check_and_set_incall_music_usecase(struct audio_device * adev,struct stream_out * out)334 int voice_check_and_set_incall_music_usecase(struct audio_device *adev,
335 struct stream_out *out)
336 {
337 int ret = 0;
338
339 ret = voice_extn_check_and_set_incall_music_usecase(adev, out);
340 if (ret == -ENOSYS) {
341 /* Incall music delivery is used only for LCH call state */
342 ret = -EINVAL;
343 }
344
345 return ret;
346 }
347
voice_set_mic_mute(struct audio_device * adev,bool state)348 int voice_set_mic_mute(struct audio_device *adev, bool state)
349 {
350 int err = 0;
351
352 adev->voice.mic_mute = state;
353 if (adev->mode == AUDIO_MODE_IN_CALL)
354 err = platform_set_mic_mute(adev->platform, state);
355
356 return err;
357 }
358
voice_get_mic_mute(struct audio_device * adev)359 bool voice_get_mic_mute(struct audio_device *adev)
360 {
361 return adev->voice.mic_mute;
362 }
363
voice_set_volume(struct audio_device * adev,float volume)364 int voice_set_volume(struct audio_device *adev, float volume)
365 {
366 int vol, err = 0;
367
368 adev->voice.volume = volume;
369 if (adev->mode == AUDIO_MODE_IN_CALL) {
370 if (volume < 0.0) {
371 volume = 0.0;
372 } else if (volume > 1.0) {
373 volume = 1.0;
374 }
375
376 vol = lrint(volume * 100.0);
377
378 // Voice volume levels from android are mapped to driver volume levels as follows.
379 // 0 -> 5, 20 -> 4, 40 ->3, 60 -> 2, 80 -> 1, 100 -> 0
380 // So adjust the volume to get the correct volume index in driver
381 vol = 100 - vol;
382
383 err = platform_set_voice_volume(adev->platform, vol);
384 }
385
386 return err;
387 }
388
voice_start_call(struct audio_device * adev)389 int voice_start_call(struct audio_device *adev)
390 {
391 int ret = 0;
392
393 adev->voice.in_call = true;
394
395 voice_set_mic_mute(adev, adev->voice.mic_mute);
396
397 ret = voice_extn_start_call(adev);
398 if (ret == -ENOSYS) {
399 ret = voice_start_usecase(adev, USECASE_VOICE_CALL);
400 }
401
402 return ret;
403 }
404
voice_stop_call(struct audio_device * adev)405 int voice_stop_call(struct audio_device *adev)
406 {
407 int ret = 0;
408
409 adev->voice.in_call = false;
410 ret = voice_extn_stop_call(adev);
411 if (ret == -ENOSYS) {
412 ret = voice_stop_usecase(adev, USECASE_VOICE_CALL);
413 }
414
415 return ret;
416 }
417
voice_get_parameters(struct audio_device * adev,struct str_parms * query,struct str_parms * reply)418 void voice_get_parameters(struct audio_device *adev,
419 struct str_parms *query,
420 struct str_parms *reply)
421 {
422 voice_extn_get_parameters(adev, query, reply);
423 }
424
voice_set_parameters(struct audio_device * adev,struct str_parms * parms)425 int voice_set_parameters(struct audio_device *adev, struct str_parms *parms)
426 {
427 char *str;
428 char value[32];
429 int val;
430 int ret = 0, err;
431 char *kv_pairs = str_parms_to_str(parms);
432
433 ALOGV_IF(kv_pairs != NULL, "%s: enter: %s", __func__, kv_pairs);
434
435 ret = voice_extn_set_parameters(adev, parms);
436 if (ret != 0) {
437 if (ret == -ENOSYS) {
438 ret = 0; /* ignore error */
439 } else {
440 goto done;
441 }
442 }
443
444 err = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_TTY_MODE, value, sizeof(value));
445 if (err >= 0) {
446 int tty_mode;
447 str_parms_del(parms, AUDIO_PARAMETER_KEY_TTY_MODE);
448 if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_OFF) == 0)
449 tty_mode = TTY_MODE_OFF;
450 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_VCO) == 0)
451 tty_mode = TTY_MODE_VCO;
452 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_HCO) == 0)
453 tty_mode = TTY_MODE_HCO;
454 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_FULL) == 0)
455 tty_mode = TTY_MODE_FULL;
456 else {
457 ret = -EINVAL;
458 goto done;
459 }
460
461 if (tty_mode != adev->voice.tty_mode) {
462 adev->voice.tty_mode = tty_mode;
463 adev->acdb_settings = (adev->acdb_settings & TTY_MODE_CLEAR) | tty_mode;
464 if (voice_is_call_state_active(adev))
465 voice_update_devices_for_all_voice_usecases(adev);
466 }
467 }
468
469 err = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_HAC,
470 value, sizeof(value));
471 if (err >= 0) {
472 bool hac = false;
473 str_parms_del(parms, AUDIO_PARAMETER_KEY_HAC);
474 if (strcmp(value, AUDIO_PARAMETER_VALUE_HAC_ON) == 0)
475 hac = true;
476
477 if (hac != adev->voice.hac) {
478 adev->voice.hac = hac;
479 if (voice_is_in_call(adev))
480 voice_update_devices_for_all_voice_usecases(adev);
481 }
482 }
483
484 err = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_INCALLMUSIC,
485 value, sizeof(value));
486 if (err >= 0) {
487 str_parms_del(parms, AUDIO_PARAMETER_KEY_INCALLMUSIC);
488 if (strcmp(value, AUDIO_PARAMETER_VALUE_TRUE) == 0)
489 platform_start_incall_music_usecase(adev->platform);
490 else
491 platform_stop_incall_music_usecase(adev->platform);
492 }
493
494 done:
495 ALOGV("%s: exit with code(%d)", __func__, ret);
496 free(kv_pairs);
497 return ret;
498 }
499
voice_init(struct audio_device * adev)500 void voice_init(struct audio_device *adev)
501 {
502 int i = 0;
503
504 memset(&adev->voice, 0, sizeof(adev->voice));
505 adev->voice.tty_mode = TTY_MODE_OFF;
506 adev->voice.hac = false;
507 adev->voice.volume = 1.0f;
508 adev->voice.mic_mute = false;
509 adev->voice.in_call = false;
510 for (i = 0; i < MAX_VOICE_SESSIONS; i++) {
511 adev->voice.session[i].pcm_rx = NULL;
512 adev->voice.session[i].pcm_tx = NULL;
513 adev->voice.session[i].state.current = CALL_INACTIVE;
514 adev->voice.session[i].state.new = CALL_INACTIVE;
515 adev->voice.session[i].vsid = VOICE_VSID;
516 }
517
518 voice_extn_init(adev);
519 }
520
voice_update_devices_for_all_voice_usecases(struct audio_device * adev)521 void voice_update_devices_for_all_voice_usecases(struct audio_device *adev)
522 {
523 struct listnode *node;
524 struct audio_usecase *usecase;
525
526 list_for_each(node, &adev->usecase_list) {
527 usecase = node_to_item(node, struct audio_usecase, list);
528 if (usecase->type == VOICE_CALL) {
529 ALOGV("%s: updating device for usecase:%s", __func__,
530 use_case_table[usecase->id]);
531 usecase->stream.out = adev->current_call_output;
532 select_devices(adev, usecase->id);
533 }
534 }
535 }
536
537
538