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