blob: ff497c7a8e8ecefe22e8568b418d7c1db939261d [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
234 in->config = pcm_config_voice_call;
235 session_id = voice_get_active_session_id(adev);
236 ret = platform_set_incall_recoding_session_id(adev->platform,
237 session_id);
238 ALOGV("%s: Update usecase to %d",__func__, in->usecase);
239 } else {
240 ALOGV("%s: voice call not active", __func__);
241 }
242
243 return ret;
244}
245
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -0700246int voice_check_and_set_incall_music_usecase(struct audio_device *adev,
247 struct stream_out *out)
248{
249 int ret = 0;
250
251 ret = voice_extn_check_and_set_incall_music_usecase(adev, out);
252 if (ret == -ENOSYS) {
253 /* Incall music delivery is used only for LCH call state */
254 ret = -EINVAL;
255 }
256
257 return ret;
258}
259
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700260int voice_set_mic_mute(struct audio_device *adev, bool state)
261{
262 int err = 0;
263
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700264 err = platform_set_mic_mute(adev->platform, state);
265 if (!err) {
266 adev->voice.mic_mute = state;
267 }
268
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 }
Shruthi Krishnaace10852013-10-25 14:32:12 -0700298
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700299 return err;
300}
301
302int voice_start_call(struct audio_device *adev)
303{
304 int ret = 0;
305
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800306 ret = voice_extn_start_call(adev);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700307 if (ret == -ENOSYS) {
308 ret = start_call(adev, USECASE_VOICE_CALL);
309 }
310
311 return ret;
312}
313
314int voice_stop_call(struct audio_device *adev)
315{
316 int ret = 0;
317
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800318 ret = voice_extn_stop_call(adev);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700319 if (ret == -ENOSYS) {
320 ret = stop_call(adev, USECASE_VOICE_CALL);
321 }
322
323 return ret;
324}
325
326int voice_set_parameters(struct audio_device *adev, struct str_parms *parms)
327{
328 char *str;
329 char value[32];
330 int val;
331 int ret = 0;
332
333 ALOGV("%s: enter: %s", __func__, str_parms_to_str(parms));
334
335 voice_extn_set_parameters(adev, parms);
336
337 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_TTY_MODE, value, sizeof(value));
338 if (ret >= 0) {
339 int tty_mode;
340 str_parms_del(parms, AUDIO_PARAMETER_KEY_TTY_MODE);
341 if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_OFF) == 0)
342 tty_mode = TTY_MODE_OFF;
343 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_VCO) == 0)
344 tty_mode = TTY_MODE_VCO;
345 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_HCO) == 0)
346 tty_mode = TTY_MODE_HCO;
347 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_FULL) == 0)
348 tty_mode = TTY_MODE_FULL;
349 else {
350 ret = -EINVAL;
351 goto done;
352 }
353
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700354 if (tty_mode != adev->voice.tty_mode) {
355 adev->voice.tty_mode = tty_mode;
356 adev->acdb_settings = (adev->acdb_settings & TTY_MODE_CLEAR) | tty_mode;
357 if (voice_is_in_call(adev))
358 //todo: what about voice2, volte and qchat usecases?
359 select_devices(adev, USECASE_VOICE_CALL);
360 }
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700361 }
362
363done:
364 ALOGV("%s: exit with code(%d)", __func__, ret);
365 return ret;
366}
367
368void voice_init(struct audio_device *adev)
369{
370 int i = 0;
371
372 memset(&adev->voice, 0, sizeof(adev->voice));
373 adev->voice.tty_mode = TTY_MODE_OFF;
374 adev->voice.volume = 1.0f;
375 adev->voice.mic_mute = false;
376 for (i = 0; i < MAX_VOICE_SESSIONS; i++) {
377 adev->voice.session[i].pcm_rx = NULL;
378 adev->voice.session[i].pcm_tx = NULL;
379 adev->voice.session[i].state.current = CALL_INACTIVE;
380 adev->voice.session[i].state.new = CALL_INACTIVE;
381 adev->voice.session[i].vsid = 0;
382 }
383
384 voice_extn_init(adev);
385}
386
387