blob: 19653796dbedb7574a50d6e7c6b09927ce7440c5 [file] [log] [blame]
Christopher N. Hesse41c9f3d2017-02-02 20:48:56 +01001/*
2 * Copyright (C) 2017 Christopher N. Hesse <raymanfx@gmail.com>
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 "audio_hw_voice"
Christopher N. Hesse86f7ea82017-02-15 09:41:05 +010018#define LOG_NDEBUG 0
Christopher N. Hesse41c9f3d2017-02-02 20:48:56 +010019/*#define VERY_VERY_VERBOSE_LOGGING*/
20#ifdef VERY_VERY_VERBOSE_LOGGING
21#define ALOGVV ALOGV
22#else
23#define ALOGVV(a...) do { } while(0)
24#endif
25
26#include <stdlib.h>
27#include <pthread.h>
28
Christopher N. Hesse696959d2017-02-02 20:49:55 +010029#include <cutils/log.h>
30#include <cutils/properties.h>
31
32#include <samsung_audio.h>
33
Christopher N. Hesse41c9f3d2017-02-02 20:48:56 +010034#include "audio_hw.h"
35#include "voice.h"
36
Christopher N. Hesse696959d2017-02-02 20:49:55 +010037static struct pcm_config pcm_config_voicecall = {
38 .channels = 2,
39 .rate = 8000,
40 .period_size = CAPTURE_PERIOD_SIZE_LOW_LATENCY,
41 .period_count = CAPTURE_PERIOD_COUNT_LOW_LATENCY,
42 .format = PCM_FORMAT_S16_LE,
43};
44
45static struct pcm_config pcm_config_voicecall_wideband = {
46 .channels = 2,
47 .rate = 16000,
48 .period_size = CAPTURE_PERIOD_SIZE_LOW_LATENCY,
49 .period_count = CAPTURE_PERIOD_COUNT_LOW_LATENCY,
50 .format = PCM_FORMAT_S16_LE,
51};
52
Andreas Schneider05bc1882017-02-09 14:03:11 +010053struct pcm_config pcm_config_voice_sco = {
54 .channels = 1,
55 .rate = SCO_DEFAULT_SAMPLING_RATE,
56 .period_size = SCO_PERIOD_SIZE,
57 .period_count = SCO_PERIOD_COUNT,
58 .format = PCM_FORMAT_S16_LE,
59};
60
Christopher N. Hesse696959d2017-02-02 20:49:55 +010061/* Prototypes */
62int start_voice_call(struct audio_device *adev);
63int stop_voice_call(struct audio_device *adev);
64
65void set_voice_session_audio_path(struct voice_session *session)
Christopher N. Hesse41c9f3d2017-02-02 20:48:56 +010066{
Christopher N. Hesse696959d2017-02-02 20:49:55 +010067 enum _AudioPath device_type;
Andreas Schneiderd44edaa2017-02-13 16:21:35 +010068 int rc;
Christopher N. Hesse696959d2017-02-02 20:49:55 +010069
70 switch(session->out_device) {
71 case AUDIO_DEVICE_OUT_SPEAKER:
72 device_type = SOUND_AUDIO_PATH_SPEAKER;
73 break;
74 case AUDIO_DEVICE_OUT_EARPIECE:
75 device_type = SOUND_AUDIO_PATH_HANDSET;
76 break;
77 case AUDIO_DEVICE_OUT_WIRED_HEADSET:
78 device_type = SOUND_AUDIO_PATH_HEADSET;
79 break;
80 case AUDIO_DEVICE_OUT_WIRED_HEADPHONE:
81 device_type = SOUND_AUDIO_PATH_HEADPHONE;
82 break;
83 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO:
84 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET:
85 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
86 device_type = SOUND_AUDIO_PATH_BLUETOOTH;
87 break;
88 default:
89 /* if output device isn't supported, use handset by default */
90 device_type = SOUND_AUDIO_PATH_HANDSET;
91 break;
92 }
93
94 ALOGV("%s: ril_set_call_audio_path(%d)", __func__, device_type);
95
Andreas Schneiderd44edaa2017-02-13 16:21:35 +010096 rc = ril_set_call_audio_path(&session->ril, device_type);
97 ALOGE_IF(rc != 0, "Failed to set audio path: (%d)", rc);
Christopher N. Hesse696959d2017-02-02 20:49:55 +010098}
99
100/*
101 * This decides based on the output device, if we enable
102 * two mic control
103 */
104void prepare_voice_session(struct voice_session *session,
105 audio_devices_t active_out_devices)
106{
107 ALOGV("%s: active_out_devices: 0x%x", __func__, active_out_devices);
108
109 session->out_device = active_out_devices;
110
111 switch (session->out_device) {
112 case AUDIO_DEVICE_OUT_EARPIECE:
113 case AUDIO_DEVICE_OUT_SPEAKER:
114 session->two_mic_control = true;
115 break;
116 default:
117 session->two_mic_control = false;
118 break;
119 }
120
121 if (session->two_mic_disabled) {
122 session->two_mic_control = false;
123 }
124}
125
126/*
Andreas Schneider05bc1882017-02-09 14:03:11 +0100127 * This must be called with the hw device mutex locked, OK to hold other
128 * mutexes.
129 */
130static void stop_voice_session_bt_sco(struct voice_session *session) {
131 ALOGV("%s: Closing SCO PCMs", __func__);
132
133 if (session->pcm_sco_rx != NULL) {
134 pcm_stop(session->pcm_sco_rx);
135 pcm_close(session->pcm_sco_rx);
136 session->pcm_sco_rx = NULL;
137 }
138
139 if (session->pcm_sco_tx != NULL) {
140 pcm_stop(session->pcm_sco_tx);
141 pcm_close(session->pcm_sco_tx);
142 session->pcm_sco_tx = NULL;
143 }
144}
145
146/* must be called with the hw device mutex locked, OK to hold other mutexes */
147void start_voice_session_bt_sco(struct voice_session *session)
148{
149 if (session->pcm_sco_rx != NULL || session->pcm_sco_tx != NULL) {
150 ALOGW("%s: SCO PCMs already open!\n", __func__);
151 return;
152 }
153
154 ALOGV("%s: Opening SCO PCMs", __func__);
155
156 /* TODO: Add wideband support */
157
158 session->pcm_sco_rx = pcm_open(SOUND_CARD,
159 SOUND_PLAYBACK_SCO_DEVICE,
160 PCM_OUT|PCM_MONOTONIC,
161 &pcm_config_voice_sco);
162 if (session->pcm_sco_rx != NULL && !pcm_is_ready(session->pcm_sco_rx)) {
163 ALOGE("%s: cannot open PCM SCO RX stream: %s",
164 __func__, pcm_get_error(session->pcm_sco_rx));
165 goto err_sco_rx;
166 }
167
168 session->pcm_sco_tx = pcm_open(SOUND_CARD,
169 SOUND_CAPTURE_SCO_DEVICE,
170 PCM_IN|PCM_MONOTONIC,
171 &pcm_config_voice_sco);
172 if (session->pcm_sco_tx && !pcm_is_ready(session->pcm_sco_tx)) {
173 ALOGE("%s: cannot open PCM SCO TX stream: %s",
174 __func__, pcm_get_error(session->pcm_sco_tx));
175 goto err_sco_tx;
176 }
177
178 pcm_start(session->pcm_sco_rx);
179 pcm_start(session->pcm_sco_tx);
180
181 return;
182
183err_sco_tx:
184 pcm_close(session->pcm_sco_tx);
185 session->pcm_sco_tx = NULL;
186err_sco_rx:
187 pcm_close(session->pcm_sco_rx);
188 session->pcm_sco_rx = NULL;
189}
190/*
Christopher N. Hesse696959d2017-02-02 20:49:55 +0100191 * This function must be called with hw device mutex locked, OK to hold other
192 * mutexes
193 */
194int start_voice_session(struct voice_session *session)
195{
196 struct pcm_config *voice_config;
197
198 if (session->pcm_voice_rx != NULL || session->pcm_voice_tx != NULL) {
199 ALOGW("%s: Voice PCMs already open!\n", __func__);
200 return 0;
201 }
202
203 ALOGV("%s: Opening voice PCMs", __func__);
204
205 if (session->wb_amr) {
206 ALOGV("%s: pcm_config wideband", __func__);
207 voice_config = &pcm_config_voicecall_wideband;
208 } else {
209 ALOGV("%s: pcm_config narrowband", __func__);
210 voice_config = &pcm_config_voicecall;
211 }
212
213 /* Open modem PCM channels */
214 session->pcm_voice_rx = pcm_open(SOUND_CARD,
215 SOUND_PLAYBACK_VOICE_DEVICE,
216 PCM_OUT|PCM_MONOTONIC,
217 voice_config);
218 if (session->pcm_voice_rx != NULL && !pcm_is_ready(session->pcm_voice_rx)) {
219 ALOGE("%s: cannot open PCM voice RX stream: %s",
220 __func__,
221 pcm_get_error(session->pcm_voice_rx));
222
223 pcm_close(session->pcm_voice_tx);
224 session->pcm_voice_tx = NULL;
225
226 return -ENOMEM;
227 }
228
229 session->pcm_voice_tx = pcm_open(SOUND_CARD,
230 SOUND_CAPTURE_VOICE_DEVICE,
231 PCM_IN|PCM_MONOTONIC,
232 voice_config);
233 if (session->pcm_voice_tx != NULL && !pcm_is_ready(session->pcm_voice_tx)) {
234 ALOGE("%s: cannot open PCM voice TX stream: %s",
235 __func__,
236 pcm_get_error(session->pcm_voice_tx));
237
238 pcm_close(session->pcm_voice_rx);
239 session->pcm_voice_rx = NULL;
240
241 return -ENOMEM;
242 }
243
244 pcm_start(session->pcm_voice_rx);
245 pcm_start(session->pcm_voice_tx);
246
Andreas Schneider05bc1882017-02-09 14:03:11 +0100247 if (session->out_device & AUDIO_DEVICE_OUT_ALL_SCO) {
248 start_voice_session_bt_sco(session);
249 }
Christopher N. Hesse696959d2017-02-02 20:49:55 +0100250
251 if (session->two_mic_control) {
252 ALOGV("%s: enabling two mic control", __func__);
253 ril_set_two_mic_control(&session->ril, AUDIENCE, TWO_MIC_SOLUTION_ON);
254 } else {
255 ALOGV("%s: disabling two mic control", __func__);
256 ril_set_two_mic_control(&session->ril, AUDIENCE, TWO_MIC_SOLUTION_OFF);
257 }
258
259 ril_set_call_clock_sync(&session->ril, SOUND_CLOCK_START);
260
261 return 0;
262}
263
264/*
265 * This function must be called with hw device mutex locked, OK to hold other
266 * mutexes
267 */
268void stop_voice_session(struct voice_session *session)
269{
270 int status = 0;
271
272 ALOGV("%s: Closing active PCMs", __func__);
273
274 if (session->pcm_voice_rx != NULL) {
275 pcm_stop(session->pcm_voice_rx);
276 pcm_close(session->pcm_voice_rx);
277 session->pcm_voice_rx = NULL;
278 status++;
279 }
280
281 if (session->pcm_voice_tx != NULL) {
282 pcm_stop(session->pcm_voice_tx);
283 pcm_close(session->pcm_voice_tx);
284 session->pcm_voice_tx = NULL;
285 status++;
286 }
287
Andreas Schneider05bc1882017-02-09 14:03:11 +0100288 if (session->out_device & AUDIO_DEVICE_OUT_ALL_SCO) {
289 stop_voice_session_bt_sco(session);
290 }
291
Christopher N. Hesse696959d2017-02-02 20:49:55 +0100292
293 session->out_device = AUDIO_DEVICE_NONE;
294
295 ALOGV("%s: Successfully closed %d active PCMs", __func__, status);
296}
297
298void set_voice_session_volume(struct voice_session *session, float volume)
299{
300 enum _SoundType sound_type;
301
302 switch (session->out_device) {
303 case AUDIO_DEVICE_OUT_EARPIECE:
304 sound_type = SOUND_TYPE_VOICE;
305 break;
306 case AUDIO_DEVICE_OUT_SPEAKER:
307 sound_type = SOUND_TYPE_SPEAKER;
308 break;
309 case AUDIO_DEVICE_OUT_WIRED_HEADSET:
310 case AUDIO_DEVICE_OUT_WIRED_HEADPHONE:
311 sound_type = SOUND_TYPE_HEADSET;
312 break;
313 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO:
314 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET:
315 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
316 case AUDIO_DEVICE_OUT_ALL_SCO:
317 sound_type = SOUND_TYPE_BTVOICE;
318 break;
319 default:
320 sound_type = SOUND_TYPE_VOICE;
321 }
322
323 ril_set_call_volume(&session->ril, sound_type, volume);
324}
325
Andreas Schneider107a8482017-02-06 12:36:31 +0100326void set_voice_session_mic_mute(struct voice_session *session, bool state)
327{
328 enum _MuteCondition mute_condition = state ? TX_MUTE : TX_UNMUTE;
329
330 ril_set_mute(&session->ril, mute_condition);
331}
332
Andreas Schneider82f32482017-02-06 09:00:48 +0100333bool voice_session_uses_twomic(struct voice_session *session)
334{
335 if (session->two_mic_disabled) {
336 return false;
337 }
338
339 return session->two_mic_control;
340}
341
Andreas Schneider59486fa2017-02-06 09:16:39 +0100342bool voice_session_uses_wideband(struct voice_session *session)
343{
344 return session->wb_amr;
345}
346
Christopher N. Hesse696959d2017-02-02 20:49:55 +0100347static void voice_session_wb_amr_callback(void *data, int enable)
348{
349 struct audio_device *adev = (struct audio_device *)data;
350 struct voice_session *session =
351 (struct voice_session *)adev->voice.session;
352
353 pthread_mutex_lock(&adev->lock);
354
355 if (session->wb_amr != enable) {
356 session->wb_amr = enable;
357
358 /* reopen the modem PCMs at the new rate */
359 if (adev->voice.in_call) {
360 ALOGV("%s: %s wide band voice call",
361 __func__,
362 enable ? "Enable" : "Disable");
363
364 stop_voice_call(adev);
365 start_voice_call(adev);
366 }
367 }
368
369 pthread_mutex_unlock(&adev->lock);
370}
371
372struct voice_session *voice_session_init(struct audio_device *adev)
373{
374 char voice_config[PROPERTY_VALUE_MAX];
Christopher N. Hesse41c9f3d2017-02-02 20:48:56 +0100375 struct voice_session *session;
376 int ret;
377
378 session = calloc(1, sizeof(struct voice_session));
379 if (session == NULL) {
380 return NULL;
381 }
382
Christopher N. Hesse696959d2017-02-02 20:49:55 +0100383 /* Two mic control */
384 ret = property_get_bool("audio_hal.disable_two_mic", false);
385 if (ret > 0) {
386 session->two_mic_disabled = true;
387 }
388
Christopher N. Hesse41c9f3d2017-02-02 20:48:56 +0100389 /* Do this as the last step so we do not have to close it on error */
390 ret = ril_open(&session->ril);
391 if (ret != 0) {
392 free(session);
393 return NULL;
394 }
395
Christopher N. Hesse696959d2017-02-02 20:49:55 +0100396 ret = property_get("audio_hal.force_voice_config", voice_config, "");
397 if (ret > 0) {
398 if ((strncmp(voice_config, "narrow", 6)) == 0)
399 session->wb_amr = false;
400 else if ((strncmp(voice_config, "wide", 4)) == 0)
401 session->wb_amr = true;
402 ALOGV("%s: Forcing voice config: %s", __func__, voice_config);
403 } else {
404 /* register callback for wideband AMR setting */
405 ret = ril_set_wb_amr_callback(&session->ril,
406 voice_session_wb_amr_callback,
407 (void *)adev);
408 if (ret != 0) {
409 ALOGE("%s: Failed to register WB_AMR callback", __func__);
410 free(session);
411 return NULL;
412 }
413
414 ALOGV("%s: Registered WB_AMR callback", __func__);
415 }
416
Christopher N. Hesse41c9f3d2017-02-02 20:48:56 +0100417 return session;
418}
419
420void voice_session_deinit(struct voice_session *session)
421{
422 ril_close(&session->ril);
423 free(session);
424}