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