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