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 <log/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
34struct 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
42static 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
56static 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    case SND_DEVICE_OUT_VOICE_USB_HEADSET:
72    case SND_DEVICE_OUT_USB_HEADSET:
73        // USB does not use a QC mixer.
74        mixer_path[0] = '\0';
75        break;
76    default:
77        ALOGW("%s: %d is not a sidetone device", __func__, out_device);
78        is_sidetone_dev = false;
79        break;
80    }
81
82    return is_sidetone_dev;
83}
84
85void voice_set_sidetone(struct audio_device *adev,
86        snd_device_t out_snd_device, bool enable)
87{
88    char mixer_path[MIXER_PATH_MAX_LENGTH];
89    bool is_sidetone_dev;
90
91    ALOGD("%s: %s, out_snd_device: %d\n",
92          __func__, (enable ? "enable" : "disable"),
93          out_snd_device);
94
95    if (voice_is_sidetone_device(out_snd_device, mixer_path))
96        platform_set_sidetone(adev, out_snd_device, enable, mixer_path);
97
98    return;
99}
100
101int voice_stop_usecase(struct audio_device *adev, audio_usecase_t usecase_id)
102{
103    int i, ret = 0;
104    struct audio_usecase *uc_info;
105    struct voice_session *session = NULL;
106
107    ALOGD("%s: enter usecase:%s", __func__, use_case_table[usecase_id]);
108
109    session = (struct voice_session *)voice_get_session_from_use_case(adev, usecase_id);
110
111    uc_info = get_usecase_from_list(adev, usecase_id);
112    if (uc_info == NULL) {
113        ALOGE("%s: Could not find the usecase (%d) in the list",
114              __func__, usecase_id);
115        return -EINVAL;
116    }
117
118    session->state.current = CALL_INACTIVE;
119
120    /* Disable sidetone only when no calls are active */
121    if (!voice_is_call_state_active(adev))
122        voice_set_sidetone(adev, uc_info->out_snd_device, false);
123
124    ret = platform_stop_voice_call(adev->platform, session->vsid);
125
126    /* 1. Close the PCM devices */
127    if (session->pcm_rx) {
128        pcm_close(session->pcm_rx);
129        session->pcm_rx = NULL;
130    }
131    if (session->pcm_tx) {
132        pcm_close(session->pcm_tx);
133        session->pcm_tx = NULL;
134    }
135
136    /* 2. Get and set stream specific mixer controls */
137    disable_audio_route(adev, uc_info);
138
139    /* 3. Disable the rx and tx devices */
140    disable_snd_device(adev, uc_info->out_snd_device);
141    disable_snd_device(adev, uc_info->in_snd_device);
142
143    if (audio_extn_tfa_98xx_is_supported() && voice_get_mic_mute(adev)) {
144        voice_set_mic_mute(adev, false);
145        ALOGD("%s: unMute voice Tx", __func__);
146    }
147
148    list_remove(&uc_info->list);
149    free(uc_info);
150
151    ALOGD("%s: exit: status(%d)", __func__, ret);
152    return ret;
153}
154
155int voice_start_usecase(struct audio_device *adev, audio_usecase_t usecase_id)
156{
157    int i, ret = 0;
158    struct audio_usecase *uc_info;
159    int pcm_dev_rx_id, pcm_dev_tx_id;
160    struct voice_session *session = NULL;
161    struct pcm_config voice_config = pcm_config_voice_call;
162
163    ALOGD("%s: enter usecase:%s", __func__, use_case_table[usecase_id]);
164
165    session = (struct voice_session *)voice_get_session_from_use_case(adev, usecase_id);
166    uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
167    uc_info->id = usecase_id;
168    uc_info->type = VOICE_CALL;
169    uc_info->stream.out = adev->current_call_output ;
170    uc_info->devices = adev->current_call_output ->devices;
171    uc_info->in_snd_device = SND_DEVICE_NONE;
172    uc_info->out_snd_device = SND_DEVICE_NONE;
173
174    list_add_tail(&adev->usecase_list, &uc_info->list);
175
176    select_devices(adev, usecase_id);
177
178    pcm_dev_rx_id = platform_get_pcm_device_id(uc_info->id, PCM_PLAYBACK);
179    pcm_dev_tx_id = platform_get_pcm_device_id(uc_info->id, PCM_CAPTURE);
180
181    if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0) {
182        ALOGE("%s: Invalid PCM devices (rx: %d tx: %d) for the usecase(%d)",
183              __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
184        ret = -EIO;
185        goto error_start_voice;
186    }
187
188    ALOGV("%s: Opening PCM capture device card_id(%d) device_id(%d)",
189          __func__, adev->snd_card, pcm_dev_tx_id);
190    session->pcm_tx = pcm_open(adev->snd_card,
191                               pcm_dev_tx_id,
192                               PCM_IN, &voice_config);
193    if (session->pcm_tx && !pcm_is_ready(session->pcm_tx)) {
194        ALOGE("%s: %s", __func__, pcm_get_error(session->pcm_tx));
195        ret = -EIO;
196        goto error_start_voice;
197    }
198
199    ALOGV("%s: Opening PCM playback device card_id(%d) device_id(%d)",
200          __func__, adev->snd_card, pcm_dev_rx_id);
201    session->pcm_rx = pcm_open(adev->snd_card,
202                               pcm_dev_rx_id,
203                               PCM_OUT, &voice_config);
204    if (session->pcm_rx && !pcm_is_ready(session->pcm_rx)) {
205        ALOGE("%s: %s", __func__, pcm_get_error(session->pcm_rx));
206        ret = -EIO;
207        goto error_start_voice;
208    }
209
210    if (adev->mic_break_enabled)
211        platform_set_mic_break_det(adev->platform, true);
212
213    pcm_start(session->pcm_tx);
214    pcm_start(session->pcm_rx);
215
216    audio_extn_tfa_98xx_enable_speaker();
217
218    /* Enable sidetone only when no calls are already active */
219    if (!voice_is_call_state_active(adev))
220        voice_set_sidetone(adev, uc_info->out_snd_device, true);
221
222    voice_set_volume(adev, adev->voice.volume);
223
224    ret = platform_start_voice_call(adev->platform, session->vsid);
225    if (ret < 0) {
226        ALOGE("%s: platform_start_voice_call error %d\n", __func__, ret);
227        goto error_start_voice;
228    }
229
230    session->state.current = CALL_ACTIVE;
231    goto done;
232
233error_start_voice:
234    voice_stop_usecase(adev, usecase_id);
235
236done:
237    ALOGD("%s: exit: status(%d)", __func__, ret);
238    return ret;
239}
240
241bool voice_is_call_state_active(struct audio_device *adev)
242{
243    bool call_state = false;
244    int ret = 0;
245
246    ret = voice_extn_is_call_state_active(adev, &call_state);
247    if (ret == -ENOSYS) {
248        call_state = (adev->voice.session[VOICE_SESS_IDX].state.current == CALL_ACTIVE) ? true : false;
249    }
250
251    return call_state;
252}
253
254bool voice_is_in_call(struct audio_device *adev)
255{
256    return adev->voice.in_call;
257}
258
259bool voice_is_in_call_rec_stream(struct stream_in *in)
260{
261    bool in_call_rec = false;
262    int ret = 0;
263
264    ret = voice_extn_is_in_call_rec_stream(in, &in_call_rec);
265    if (ret == -ENOSYS) {
266        in_call_rec = false;
267    }
268
269    return in_call_rec;
270}
271
272uint32_t voice_get_active_session_id(struct audio_device *adev)
273{
274    int ret = 0;
275    uint32_t session_id;
276
277    ret = voice_extn_get_active_session_id(adev, &session_id);
278    if (ret == -ENOSYS) {
279        session_id = VOICE_VSID;
280    }
281    return session_id;
282}
283
284int voice_check_and_set_incall_rec_usecase(struct audio_device *adev,
285                                           struct stream_in *in)
286{
287    int ret = 0;
288    uint32_t session_id;
289    int usecase_id;
290    int rec_mode = INCALL_REC_NONE;
291
292    if (voice_is_call_state_active(adev)) {
293        switch (in->source) {
294        case AUDIO_SOURCE_VOICE_UPLINK:
295            in->usecase = USECASE_INCALL_REC_UPLINK;
296            rec_mode = INCALL_REC_UPLINK;
297            break;
298        case AUDIO_SOURCE_VOICE_DOWNLINK:
299            in->usecase = USECASE_INCALL_REC_DOWNLINK;
300            rec_mode = INCALL_REC_DOWNLINK;
301            break;
302        case AUDIO_SOURCE_VOICE_CALL:
303            in->usecase = USECASE_INCALL_REC_UPLINK_AND_DOWNLINK;
304            rec_mode = INCALL_REC_UPLINK_AND_DOWNLINK;
305            break;
306        default:
307            ALOGV("%s: Source type %d doesnt match incall recording criteria",
308                  __func__, in->source);
309            return ret;
310        }
311
312        session_id = voice_get_active_session_id(adev);
313        ret = platform_set_incall_recording_session_id(adev->platform,
314                                                       session_id, rec_mode);
315#ifdef INCALL_STEREO_CAPTURE_ENABLED
316        ret = platform_set_incall_recording_session_channels(adev->platform,
317                                                        in->config.channels);
318#endif
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
327int 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
342int 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
356int 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
368bool voice_get_mic_mute(struct audio_device *adev)
369{
370    return adev->voice.mic_mute;
371}
372
373int 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
398int 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
414int 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
427void 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
434int 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
503done:
504    ALOGV("%s: exit with code(%d)", __func__, ret);
505    free(kv_pairs);
506    return ret;
507}
508
509void 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
530void 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