blob: 1c177fc089f03fa2e697fa18b32fdddac83cbae7 [file] [log] [blame]
Uday Kishore Pasupuleti582e0a52016-01-06 19:12:41 -08001/*
2 * Copyright (c) 2013-2015, 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 "audio_hw_primary"
21/*#define LOG_NDEBUG 0*/
22/*#define VERY_VERY_VERBOSE_LOGGING*/
23#ifdef VERY_VERY_VERBOSE_LOGGING
24#define ALOGVV ALOGV
25#else
26#define ALOGVV(a...) do { } while(0)
27#endif
28
29#include <errno.h>
30#include <pthread.h>
31#include <stdint.h>
32#include <sys/time.h>
33#include <stdlib.h>
34#include <math.h>
35#include <dlfcn.h>
36#include <sys/resource.h>
37#include <sys/prctl.h>
38
39#include <cutils/log.h>
40#include <cutils/str_parms.h>
41#include <cutils/properties.h>
42#include <cutils/atomic.h>
43#include <cutils/sched_policy.h>
44
45#include <hardware/audio_effect.h>
46#include <system/thread_defs.h>
47#include <audio_effects/effect_aec.h>
48#include <audio_effects/effect_ns.h>
49#include "audio_hw.h"
50#include "platform_api.h"
51#include <platform.h>
52#include "audio_extn.h"
53#include "voice_extn.h"
54
55#include "sound/compress_params.h"
56#include "sound/asound.h"
57
58#define COMPRESS_OFFLOAD_NUM_FRAGMENTS 4
59/* ToDo: Check and update a proper value in msec */
60#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 96
61#define COMPRESS_PLAYBACK_VOLUME_MAX 0x2000
62
63#define PROXY_OPEN_RETRY_COUNT 100
64#define PROXY_OPEN_WAIT_TIME 20
65
66#define USECASE_AUDIO_PLAYBACK_PRIMARY USECASE_AUDIO_PLAYBACK_DEEP_BUFFER
67
68static unsigned int configured_low_latency_capture_period_size =
69 LOW_LATENCY_CAPTURE_PERIOD_SIZE;
70
71struct pcm_config pcm_config_deep_buffer = {
72 .channels = 2,
73 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
74 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
75 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
76 .format = PCM_FORMAT_S16_LE,
77 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
78 .stop_threshold = INT_MAX,
79 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
80};
81
82struct pcm_config pcm_config_low_latency = {
83 .channels = 2,
84 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
85 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
86 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
87 .format = PCM_FORMAT_S16_LE,
88 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
89 .stop_threshold = INT_MAX,
90 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
91};
92
93struct pcm_config pcm_config_hdmi_multi = {
94 .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
95 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
96 .period_size = HDMI_MULTI_PERIOD_SIZE,
97 .period_count = HDMI_MULTI_PERIOD_COUNT,
98 .format = PCM_FORMAT_S16_LE,
99 .start_threshold = 0,
100 .stop_threshold = INT_MAX,
101 .avail_min = 0,
102};
103
104struct pcm_config pcm_config_audio_capture = {
105 .channels = 2,
106 .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
107 .format = PCM_FORMAT_S16_LE,
108};
109
110#define AFE_PROXY_CHANNEL_COUNT 2
111#define AFE_PROXY_SAMPLING_RATE 48000
112
113#define AFE_PROXY_PLAYBACK_PERIOD_SIZE 768
114#define AFE_PROXY_PLAYBACK_PERIOD_COUNT 4
115
116struct pcm_config pcm_config_afe_proxy_playback = {
117 .channels = AFE_PROXY_CHANNEL_COUNT,
118 .rate = AFE_PROXY_SAMPLING_RATE,
119 .period_size = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
120 .period_count = AFE_PROXY_PLAYBACK_PERIOD_COUNT,
121 .format = PCM_FORMAT_S16_LE,
122 .start_threshold = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
123 .stop_threshold = INT_MAX,
124 .avail_min = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
125};
126
127#define AFE_PROXY_RECORD_PERIOD_SIZE 768
128#define AFE_PROXY_RECORD_PERIOD_COUNT 4
129
130struct pcm_config pcm_config_afe_proxy_record = {
131 .channels = AFE_PROXY_CHANNEL_COUNT,
132 .rate = AFE_PROXY_SAMPLING_RATE,
133 .period_size = AFE_PROXY_RECORD_PERIOD_SIZE,
134 .period_count = AFE_PROXY_RECORD_PERIOD_COUNT,
135 .format = PCM_FORMAT_S16_LE,
136 .start_threshold = AFE_PROXY_RECORD_PERIOD_SIZE,
137 .stop_threshold = INT_MAX,
138 .avail_min = AFE_PROXY_RECORD_PERIOD_SIZE,
139};
140
141const char * const use_case_table[AUDIO_USECASE_MAX] = {
142 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
143 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
144 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback",
145 [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback",
146#ifdef MULTIPLE_OFFLOAD_ENABLED
147 [USECASE_AUDIO_PLAYBACK_OFFLOAD2] = "compress-offload-playback2",
148 [USECASE_AUDIO_PLAYBACK_OFFLOAD3] = "compress-offload-playback3",
149 [USECASE_AUDIO_PLAYBACK_OFFLOAD4] = "compress-offload-playback4",
150 [USECASE_AUDIO_PLAYBACK_OFFLOAD5] = "compress-offload-playback5",
151 [USECASE_AUDIO_PLAYBACK_OFFLOAD6] = "compress-offload-playback6",
152 [USECASE_AUDIO_PLAYBACK_OFFLOAD7] = "compress-offload-playback7",
153 [USECASE_AUDIO_PLAYBACK_OFFLOAD8] = "compress-offload-playback8",
154 [USECASE_AUDIO_PLAYBACK_OFFLOAD9] = "compress-offload-playback9",
155#endif
156 [USECASE_AUDIO_RECORD] = "audio-record",
157 [USECASE_AUDIO_RECORD_COMPRESS] = "audio-record-compress",
158 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
159 [USECASE_AUDIO_RECORD_FM_VIRTUAL] = "fm-virtual-record",
160 [USECASE_AUDIO_PLAYBACK_FM] = "play-fm",
161 [USECASE_AUDIO_HFP_SCO] = "hfp-sco",
162 [USECASE_AUDIO_HFP_SCO_WB] = "hfp-sco-wb",
163 [USECASE_VOICE_CALL] = "voice-call",
164
165 [USECASE_VOICE2_CALL] = "voice2-call",
166 [USECASE_VOLTE_CALL] = "volte-call",
167 [USECASE_QCHAT_CALL] = "qchat-call",
168 [USECASE_VOWLAN_CALL] = "vowlan-call",
169 [USECASE_COMPRESS_VOIP_CALL] = "compress-voip-call",
170 [USECASE_INCALL_REC_UPLINK] = "incall-rec-uplink",
171 [USECASE_INCALL_REC_DOWNLINK] = "incall-rec-downlink",
172 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK] = "incall-rec-uplink-and-downlink",
173 [USECASE_INCALL_REC_UPLINK_COMPRESS] = "incall-rec-uplink-compress",
174 [USECASE_INCALL_REC_DOWNLINK_COMPRESS] = "incall-rec-downlink-compress",
175 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK_COMPRESS] = "incall-rec-uplink-and-downlink-compress",
176
177 [USECASE_INCALL_MUSIC_UPLINK] = "incall_music_uplink",
178 [USECASE_INCALL_MUSIC_UPLINK2] = "incall_music_uplink2",
179 [USECASE_AUDIO_SPKR_CALIB_RX] = "spkr-rx-calib",
180 [USECASE_AUDIO_SPKR_CALIB_TX] = "spkr-vi-record",
181
182 [USECASE_AUDIO_PLAYBACK_AFE_PROXY] = "afe-proxy-playback",
183 [USECASE_AUDIO_RECORD_AFE_PROXY] = "afe-proxy-record",
184};
185
186static const audio_usecase_t offload_usecases[] = {
187 USECASE_AUDIO_PLAYBACK_OFFLOAD,
188#ifdef MULTIPLE_OFFLOAD_ENABLED
189 USECASE_AUDIO_PLAYBACK_OFFLOAD2,
190 USECASE_AUDIO_PLAYBACK_OFFLOAD3,
191 USECASE_AUDIO_PLAYBACK_OFFLOAD4,
192 USECASE_AUDIO_PLAYBACK_OFFLOAD5,
193 USECASE_AUDIO_PLAYBACK_OFFLOAD6,
194 USECASE_AUDIO_PLAYBACK_OFFLOAD7,
195 USECASE_AUDIO_PLAYBACK_OFFLOAD8,
196 USECASE_AUDIO_PLAYBACK_OFFLOAD9,
197#endif
198};
199
200#define STRING_TO_ENUM(string) { #string, string }
201
202struct string_to_enum {
203 const char *name;
204 uint32_t value;
205};
206
207static const struct string_to_enum out_channels_name_to_enum_table[] = {
208 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
209 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
210 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
211};
212
213static const struct string_to_enum out_formats_name_to_enum_table[] = {
214 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
215 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
216 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
217};
218
219static struct audio_device *adev = NULL;
220static pthread_mutex_t adev_init_lock;
221static unsigned int audio_device_ref_count;
222
223static int set_voice_volume_l(struct audio_device *adev, float volume);
224
225static int check_and_set_gapless_mode(struct audio_device *adev) {
226
227
228 char value[PROPERTY_VALUE_MAX] = {0};
229 bool gapless_enabled = false;
230 const char *mixer_ctl_name = "Compress Gapless Playback";
231 struct mixer_ctl *ctl;
232
233 ALOGV("%s:", __func__);
234 property_get("audio.offload.gapless.enabled", value, NULL);
235 gapless_enabled = atoi(value) || !strncmp("true", value, 4);
236
237 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
238 if (!ctl) {
239 ALOGE("%s: Could not get ctl for mixer cmd - %s",
240 __func__, mixer_ctl_name);
241 return -EINVAL;
242 }
243
244 if (mixer_ctl_set_value(ctl, 0, gapless_enabled) < 0) {
245 ALOGE("%s: Could not set gapless mode %d",
246 __func__, gapless_enabled);
247 return -EINVAL;
248 }
249 return 0;
250}
251
252static bool is_supported_format(audio_format_t format)
253{
254 if (format == AUDIO_FORMAT_MP3 ||
255 format == AUDIO_FORMAT_AAC_LC ||
256 format == AUDIO_FORMAT_AAC_HE_V1 ||
257 format == AUDIO_FORMAT_AAC_HE_V2 ||
258 format == AUDIO_FORMAT_PCM_16_BIT_OFFLOAD ||
259 format == AUDIO_FORMAT_PCM_24_BIT_OFFLOAD ||
260 format == AUDIO_FORMAT_FLAC ||
261 format == AUDIO_FORMAT_ALAC ||
262 format == AUDIO_FORMAT_APE ||
263 format == AUDIO_FORMAT_VORBIS ||
264 format == AUDIO_FORMAT_WMA ||
265 format == AUDIO_FORMAT_WMA_PRO)
266 return true;
267
268 return false;
269}
270
271static int get_snd_codec_id(audio_format_t format)
272{
273 int id = 0;
274
275 switch (format & AUDIO_FORMAT_MAIN_MASK) {
276 case AUDIO_FORMAT_MP3:
277 id = SND_AUDIOCODEC_MP3;
278 break;
279 case AUDIO_FORMAT_AAC:
280 id = SND_AUDIOCODEC_AAC;
281 break;
282 case AUDIO_FORMAT_PCM_OFFLOAD:
283 id = SND_AUDIOCODEC_PCM;
284 break;
285 case AUDIO_FORMAT_FLAC:
286 id = SND_AUDIOCODEC_FLAC;
287 break;
288 case AUDIO_FORMAT_ALAC:
289 id = SND_AUDIOCODEC_ALAC;
290 break;
291 case AUDIO_FORMAT_APE:
292 id = SND_AUDIOCODEC_APE;
293 break;
294 case AUDIO_FORMAT_VORBIS:
295 id = SND_AUDIOCODEC_VORBIS;
296 break;
297 case AUDIO_FORMAT_WMA:
298 id = SND_AUDIOCODEC_WMA;
299 break;
300 case AUDIO_FORMAT_WMA_PRO:
301 id = SND_AUDIOCODEC_WMA_PRO;
302 break;
303 default:
304 ALOGE("%s: Unsupported audio format :%x", __func__, format);
305 }
306
307 return id;
308}
309
310int get_snd_card_state(struct audio_device *adev)
311{
312 int snd_scard_state;
313
314 if (!adev)
315 return SND_CARD_STATE_OFFLINE;
316
317 pthread_mutex_lock(&adev->snd_card_status.lock);
318 snd_scard_state = adev->snd_card_status.state;
319 pthread_mutex_unlock(&adev->snd_card_status.lock);
320
321 return snd_scard_state;
322}
323
324static int set_snd_card_state(struct audio_device *adev, int snd_scard_state)
325{
326 if (!adev)
327 return -ENOSYS;
328
329 pthread_mutex_lock(&adev->snd_card_status.lock);
330 adev->snd_card_status.state = snd_scard_state;
331 pthread_mutex_unlock(&adev->snd_card_status.lock);
332
333 return 0;
334}
335
336static int enable_audio_route_for_voice_usecases(struct audio_device *adev,
337 struct audio_usecase *uc_info)
338{
339 struct listnode *node;
340 struct audio_usecase *usecase;
341
342 if (uc_info == NULL)
343 return -EINVAL;
344
345 /* Re-route all voice usecases on the shared backend other than the
346 specified usecase to new snd devices */
347 list_for_each(node, &adev->usecase_list) {
348 usecase = node_to_item(node, struct audio_usecase, list);
349 if ((usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) &&
350 (usecase != uc_info))
351 enable_audio_route(adev, usecase);
352 }
353 return 0;
354}
355
356int pcm_ioctl(struct pcm *pcm, int request, ...)
357{
358 va_list ap;
359 void * arg;
360 int pcm_fd = *(int*)pcm;
361
362 va_start(ap, request);
363 arg = va_arg(ap, void *);
364 va_end(ap);
365
366 return ioctl(pcm_fd, request, arg);
367}
368
369int enable_audio_route(struct audio_device *adev,
370 struct audio_usecase *usecase)
371{
372 snd_device_t snd_device;
373 char mixer_path[MIXER_PATH_MAX_LENGTH];
374
375 if (usecase == NULL)
376 return -EINVAL;
377
378 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
379
380 if (usecase->type == PCM_CAPTURE)
381 snd_device = usecase->in_snd_device;
382 else
383 snd_device = usecase->out_snd_device;
384
385#ifdef DS1_DOLBY_DAP_ENABLED
386 audio_extn_dolby_set_dmid(adev);
387 audio_extn_dolby_set_endpoint(adev);
388#endif
389 audio_extn_dolby_ds2_set_endpoint(adev);
390 audio_extn_sound_trigger_update_stream_status(usecase, ST_EVENT_STREAM_BUSY);
391 audio_extn_listen_update_stream_status(usecase, LISTEN_EVENT_STREAM_BUSY);
392 audio_extn_utils_send_audio_calibration(adev, usecase);
393 audio_extn_utils_send_app_type_cfg(usecase);
394 strcpy(mixer_path, use_case_table[usecase->id]);
395 platform_add_backend_name(mixer_path, snd_device);
396 ALOGD("%s: apply mixer and update path: %s", __func__, mixer_path);
397 audio_route_apply_and_update_path(adev->audio_route, mixer_path);
398 ALOGV("%s: exit", __func__);
399 return 0;
400}
401
402int disable_audio_route(struct audio_device *adev,
403 struct audio_usecase *usecase)
404{
405 snd_device_t snd_device;
406 char mixer_path[MIXER_PATH_MAX_LENGTH];
407
408 if (usecase == NULL || usecase->id == USECASE_INVALID)
409 return -EINVAL;
410
411 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
412 if (usecase->type == PCM_CAPTURE)
413 snd_device = usecase->in_snd_device;
414 else
415 snd_device = usecase->out_snd_device;
416 strcpy(mixer_path, use_case_table[usecase->id]);
417 platform_add_backend_name(mixer_path, snd_device);
418 ALOGD("%s: reset and update mixer path: %s", __func__, mixer_path);
419 audio_route_reset_and_update_path(adev->audio_route, mixer_path);
420 audio_extn_sound_trigger_update_stream_status(usecase, ST_EVENT_STREAM_FREE);
421 audio_extn_listen_update_stream_status(usecase, LISTEN_EVENT_STREAM_FREE);
422 ALOGV("%s: exit", __func__);
423 return 0;
424}
425
426int enable_snd_device(struct audio_device *adev,
427 snd_device_t snd_device)
428{
429 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
430
431 if (snd_device < SND_DEVICE_MIN ||
432 snd_device >= SND_DEVICE_MAX) {
433 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
434 return -EINVAL;
435 }
436
437 adev->snd_dev_ref_cnt[snd_device]++;
438
439 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0 ) {
440 ALOGE("%s: Invalid sound device returned", __func__);
441 return -EINVAL;
442 }
443 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
444 ALOGV("%s: snd_device(%d: %s) is already active",
445 __func__, snd_device, device_name);
446 return 0;
447 }
448
449 if (audio_extn_spkr_prot_is_enabled())
450 audio_extn_spkr_prot_calib_cancel(adev);
451 /* start usb playback thread */
452 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
453 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
454 audio_extn_usb_start_playback(adev);
455
456 /* start usb capture thread */
457 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
458 audio_extn_usb_start_capture(adev);
459
460 if (SND_DEVICE_OUT_BT_A2DP == snd_device ||
461 (SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP) == snd_device)
462 audio_extn_a2dp_start_playback();
463
464 if ((snd_device == SND_DEVICE_OUT_SPEAKER ||
465 snd_device == SND_DEVICE_OUT_VOICE_SPEAKER) &&
466 audio_extn_spkr_prot_is_enabled()) {
467 if (audio_extn_spkr_prot_get_acdb_id(snd_device) < 0) {
468 adev->snd_dev_ref_cnt[snd_device]--;
469 return -EINVAL;
470 }
471 if (audio_extn_spkr_prot_start_processing(snd_device)) {
472 ALOGE("%s: spkr_start_processing failed", __func__);
473 return -EINVAL;
474 }
475 } else {
476 ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, device_name);
477 /* due to the possibility of calibration overwrite between listen
478 and audio, notify listen hal before audio calibration is sent */
479 audio_extn_sound_trigger_update_device_status(snd_device,
480 ST_EVENT_SND_DEVICE_BUSY);
481 audio_extn_listen_update_device_status(snd_device,
482 LISTEN_EVENT_SND_DEVICE_BUSY);
483 if (platform_get_snd_device_acdb_id(snd_device) < 0) {
484 adev->snd_dev_ref_cnt[snd_device]--;
485 audio_extn_sound_trigger_update_device_status(snd_device,
486 ST_EVENT_SND_DEVICE_FREE);
487 audio_extn_listen_update_device_status(snd_device,
488 LISTEN_EVENT_SND_DEVICE_FREE);
489 return -EINVAL;
490 }
491 audio_extn_dev_arbi_acquire(snd_device);
492 audio_route_apply_and_update_path(adev->audio_route, device_name);
493 }
494 return 0;
495}
496
497int disable_snd_device(struct audio_device *adev,
498 snd_device_t snd_device)
499{
500 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
501
502 if (snd_device < SND_DEVICE_MIN ||
503 snd_device >= SND_DEVICE_MAX) {
504 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
505 return -EINVAL;
506 }
507 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
508 ALOGE("%s: device ref cnt is already 0", __func__);
509 return -EINVAL;
510 }
511
512 adev->snd_dev_ref_cnt[snd_device]--;
513
514 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0) {
515 ALOGE("%s: Invalid sound device returned", __func__);
516 return -EINVAL;
517 }
518
519 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
520 ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, device_name);
521 /* exit usb play back thread */
522 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
523 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
524 audio_extn_usb_stop_playback();
525
526 /* exit usb capture thread */
527 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
528 audio_extn_usb_stop_capture();
529
530 if (SND_DEVICE_OUT_BT_A2DP == snd_device ||
531 (SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP) == snd_device)
532 audio_extn_a2dp_stop_playback();
533
534 if ((snd_device == SND_DEVICE_OUT_SPEAKER ||
535 snd_device == SND_DEVICE_OUT_VOICE_SPEAKER) &&
536 audio_extn_spkr_prot_is_enabled()) {
537 audio_extn_spkr_prot_stop_processing(snd_device);
538 } else {
539 audio_route_reset_and_update_path(adev->audio_route, device_name);
540 audio_extn_dev_arbi_release(snd_device);
541 }
542
543 audio_extn_sound_trigger_update_device_status(snd_device,
544 ST_EVENT_SND_DEVICE_FREE);
545 audio_extn_listen_update_device_status(snd_device,
546 LISTEN_EVENT_SND_DEVICE_FREE);
547 }
548
549 return 0;
550}
551
552static void check_usecases_codec_backend(struct audio_device *adev,
553 struct audio_usecase *uc_info,
554 snd_device_t snd_device)
555{
556 struct listnode *node;
557 struct audio_usecase *usecase;
558 bool switch_device[AUDIO_USECASE_MAX];
559 int i, num_uc_to_switch = 0;
560
561 /*
562 * This function is to make sure that all the usecases that are active on
563 * the hardware codec backend are always routed to any one device that is
564 * handled by the hardware codec.
565 * For example, if low-latency and deep-buffer usecases are currently active
566 * on speaker and out_set_parameters(headset) is received on low-latency
567 * output, then we have to make sure deep-buffer is also switched to headset,
568 * because of the limitation that both the devices cannot be enabled
569 * at the same time as they share the same backend.
570 */
571 /*
572 * This call is to check if we need to force routing for a particular stream
573 * If there is a backend configuration change for the device when a
574 * new stream starts, then ADM needs to be closed and re-opened with the new
575 * configuraion. This call check if we need to re-route all the streams
576 * associated with the backend. Touch tone + 24 bit playback.
577 */
578 bool force_routing = platform_check_and_set_codec_backend_cfg(adev, uc_info);
579
580 /* Disable all the usecases on the shared backend other than the
581 specified usecase */
582 for (i = 0; i < AUDIO_USECASE_MAX; i++)
583 switch_device[i] = false;
584
585 list_for_each(node, &adev->usecase_list) {
586 usecase = node_to_item(node, struct audio_usecase, list);
587 if (usecase->type != PCM_CAPTURE &&
588 usecase != uc_info &&
589 (usecase->out_snd_device != snd_device || force_routing) &&
590 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
591 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
592 __func__, use_case_table[usecase->id],
593 platform_get_snd_device_name(usecase->out_snd_device));
594 disable_audio_route(adev, usecase);
595 switch_device[usecase->id] = true;
596 num_uc_to_switch++;
597 }
598 }
599
600 if (num_uc_to_switch) {
601 /* All streams have been de-routed. Disable the device */
602
603 /* Make sure the previous devices to be disabled first and then enable the
604 selected devices */
605 list_for_each(node, &adev->usecase_list) {
606 usecase = node_to_item(node, struct audio_usecase, list);
607 if (switch_device[usecase->id]) {
608 disable_snd_device(adev, usecase->out_snd_device);
609 }
610 }
611
612 list_for_each(node, &adev->usecase_list) {
613 usecase = node_to_item(node, struct audio_usecase, list);
614 if (switch_device[usecase->id]) {
615 enable_snd_device(adev, snd_device);
616 }
617 }
618
619 /* Re-route all the usecases on the shared backend other than the
620 specified usecase to new snd devices */
621 list_for_each(node, &adev->usecase_list) {
622 usecase = node_to_item(node, struct audio_usecase, list);
623 /* Update the out_snd_device only before enabling the audio route */
624 if (switch_device[usecase->id] ) {
625 usecase->out_snd_device = snd_device;
626 if (usecase->type != VOICE_CALL && usecase->type != VOIP_CALL)
627 enable_audio_route(adev, usecase);
628 }
629 }
630 }
631}
632
633static void check_and_route_capture_usecases(struct audio_device *adev,
634 struct audio_usecase *uc_info,
635 snd_device_t snd_device)
636{
637 struct listnode *node;
638 struct audio_usecase *usecase;
639 bool switch_device[AUDIO_USECASE_MAX];
640 int i, num_uc_to_switch = 0;
641
642 /*
643 * This function is to make sure that all the active capture usecases
644 * are always routed to the same input sound device.
645 * For example, if audio-record and voice-call usecases are currently
646 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
647 * is received for voice call then we have to make sure that audio-record
648 * usecase is also switched to earpiece i.e. voice-dmic-ef,
649 * because of the limitation that two devices cannot be enabled
650 * at the same time if they share the same backend.
651 */
652 for (i = 0; i < AUDIO_USECASE_MAX; i++)
653 switch_device[i] = false;
654
655 list_for_each(node, &adev->usecase_list) {
656 usecase = node_to_item(node, struct audio_usecase, list);
657 if (usecase->type != PCM_PLAYBACK &&
658 usecase != uc_info &&
659 usecase->in_snd_device != snd_device) {
660 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
661 __func__, use_case_table[usecase->id],
662 platform_get_snd_device_name(usecase->in_snd_device));
663 disable_audio_route(adev, usecase);
664 switch_device[usecase->id] = true;
665 num_uc_to_switch++;
666 }
667 }
668
669 if (num_uc_to_switch) {
670 /* All streams have been de-routed. Disable the device */
671
672 /* Make sure the previous devices to be disabled first and then enable the
673 selected devices */
674 list_for_each(node, &adev->usecase_list) {
675 usecase = node_to_item(node, struct audio_usecase, list);
676 if (switch_device[usecase->id]) {
677 disable_snd_device(adev, usecase->in_snd_device);
678 }
679 }
680
681 list_for_each(node, &adev->usecase_list) {
682 usecase = node_to_item(node, struct audio_usecase, list);
683 if (switch_device[usecase->id]) {
684 enable_snd_device(adev, snd_device);
685 }
686 }
687
688 /* Re-route all the usecases on the shared backend other than the
689 specified usecase to new snd devices */
690 list_for_each(node, &adev->usecase_list) {
691 usecase = node_to_item(node, struct audio_usecase, list);
692 /* Update the in_snd_device only before enabling the audio route */
693 if (switch_device[usecase->id] ) {
694 usecase->in_snd_device = snd_device;
695 if (usecase->type != VOICE_CALL && usecase->type != VOIP_CALL)
696 enable_audio_route(adev, usecase);
697 }
698 }
699 }
700}
701
702/* must be called with hw device mutex locked */
703static int read_hdmi_channel_masks(struct stream_out *out)
704{
705 int ret = 0;
706 int channels = platform_edid_get_max_channels(out->dev->platform);
707
708 switch (channels) {
709 /*
710 * Do not handle stereo output in Multi-channel cases
711 * Stereo case is handled in normal playback path
712 */
713 case 6:
714 ALOGV("%s: HDMI supports 5.1", __func__);
715 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
716 break;
717 case 8:
718 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
719 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
720 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
721 break;
722 default:
723 ALOGE("HDMI does not support multi channel playback");
724 ret = -ENOSYS;
725 break;
726 }
727 return ret;
728}
729
730static audio_usecase_t get_voice_usecase_id_from_list(struct audio_device *adev)
731{
732 struct audio_usecase *usecase;
733 struct listnode *node;
734
735 list_for_each(node, &adev->usecase_list) {
736 usecase = node_to_item(node, struct audio_usecase, list);
737 if (usecase->type == VOICE_CALL) {
738 ALOGV("%s: usecase id %d", __func__, usecase->id);
739 return usecase->id;
740 }
741 }
742 return USECASE_INVALID;
743}
744
745struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
746 audio_usecase_t uc_id)
747{
748 struct audio_usecase *usecase;
749 struct listnode *node;
750
751 list_for_each(node, &adev->usecase_list) {
752 usecase = node_to_item(node, struct audio_usecase, list);
753 if (usecase->id == uc_id)
754 return usecase;
755 }
756 return NULL;
757}
758
759int select_devices(struct audio_device *adev, audio_usecase_t uc_id)
760{
761 snd_device_t out_snd_device = SND_DEVICE_NONE;
762 snd_device_t in_snd_device = SND_DEVICE_NONE;
763 struct audio_usecase *usecase = NULL;
764 struct audio_usecase *vc_usecase = NULL;
765 struct audio_usecase *voip_usecase = NULL;
766 struct audio_usecase *hfp_usecase = NULL;
767 audio_usecase_t hfp_ucid;
768 struct listnode *node;
769 int status = 0;
770
771 usecase = get_usecase_from_list(adev, uc_id);
772 if (usecase == NULL) {
773 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
774 return -EINVAL;
775 }
776
777 if ((usecase->type == VOICE_CALL) ||
778 (usecase->type == VOIP_CALL) ||
779 (usecase->type == PCM_HFP_CALL)) {
780 out_snd_device = platform_get_output_snd_device(adev->platform,
781 usecase->stream.out->devices);
782 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
783 usecase->devices = usecase->stream.out->devices;
784 } else {
785 /*
786 * If the voice call is active, use the sound devices of voice call usecase
787 * so that it would not result any device switch. All the usecases will
788 * be switched to new device when select_devices() is called for voice call
789 * usecase. This is to avoid switching devices for voice call when
790 * check_usecases_codec_backend() is called below.
791 */
792 if (voice_is_in_call(adev) && adev->mode == AUDIO_MODE_IN_CALL) {
793 vc_usecase = get_usecase_from_list(adev,
794 get_voice_usecase_id_from_list(adev));
795 if ((vc_usecase) && ((vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) ||
796 (usecase->devices == AUDIO_DEVICE_IN_VOICE_CALL))) {
797 in_snd_device = vc_usecase->in_snd_device;
798 out_snd_device = vc_usecase->out_snd_device;
799 }
800 } else if (voice_extn_compress_voip_is_active(adev)) {
801 voip_usecase = get_usecase_from_list(adev, USECASE_COMPRESS_VOIP_CALL);
802 if ((voip_usecase) && ((voip_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) &&
803 (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) &&
804 (voip_usecase->stream.out != adev->primary_output))) {
805 in_snd_device = voip_usecase->in_snd_device;
806 out_snd_device = voip_usecase->out_snd_device;
807 }
808 } else if (audio_extn_hfp_is_active(adev)) {
809 hfp_ucid = audio_extn_hfp_get_usecase();
810 hfp_usecase = get_usecase_from_list(adev, hfp_ucid);
811 if ((hfp_usecase) && (hfp_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)) {
812 in_snd_device = hfp_usecase->in_snd_device;
813 out_snd_device = hfp_usecase->out_snd_device;
814 }
815 }
816 if (usecase->type == PCM_PLAYBACK) {
817 usecase->devices = usecase->stream.out->devices;
818 in_snd_device = SND_DEVICE_NONE;
819 if (out_snd_device == SND_DEVICE_NONE) {
820 out_snd_device = platform_get_output_snd_device(adev->platform,
821 usecase->stream.out->devices);
822 if (usecase->stream.out == adev->primary_output &&
823 adev->active_input &&
824 out_snd_device != usecase->out_snd_device) {
825 select_devices(adev, adev->active_input->usecase);
826 }
827 }
828 } else if (usecase->type == PCM_CAPTURE) {
829 usecase->devices = usecase->stream.in->device;
830 out_snd_device = SND_DEVICE_NONE;
831 if (in_snd_device == SND_DEVICE_NONE) {
832 audio_devices_t out_device = AUDIO_DEVICE_NONE;
833 if ((adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
834 (adev->mode == AUDIO_MODE_IN_COMMUNICATION &&
835 adev->active_input->source == AUDIO_SOURCE_MIC)) &&
836 adev->primary_output && !adev->primary_output->standby) {
837 out_device = adev->primary_output->devices;
838 platform_set_echo_reference(adev->platform, false);
839 } else if (usecase->id == USECASE_AUDIO_RECORD_AFE_PROXY) {
840 out_device = AUDIO_DEVICE_OUT_TELEPHONY_TX;
841 }
842 in_snd_device = platform_get_input_snd_device(adev->platform, out_device);
843 }
844 }
845 }
846
847 if (out_snd_device == usecase->out_snd_device &&
848 in_snd_device == usecase->in_snd_device) {
849 return 0;
850 }
851
852 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
853 out_snd_device, platform_get_snd_device_name(out_snd_device),
854 in_snd_device, platform_get_snd_device_name(in_snd_device));
855
856 /*
857 * Limitation: While in call, to do a device switch we need to disable
858 * and enable both RX and TX devices though one of them is same as current
859 * device.
860 */
861 if ((usecase->type == VOICE_CALL) &&
862 (usecase->in_snd_device != SND_DEVICE_NONE) &&
863 (usecase->out_snd_device != SND_DEVICE_NONE)) {
864 status = platform_switch_voice_call_device_pre(adev->platform);
865 }
866
867 /* Disable current sound devices */
868 if (usecase->out_snd_device != SND_DEVICE_NONE) {
869 disable_audio_route(adev, usecase);
870 disable_snd_device(adev, usecase->out_snd_device);
871 }
872
873 if (usecase->in_snd_device != SND_DEVICE_NONE) {
874 disable_audio_route(adev, usecase);
875 disable_snd_device(adev, usecase->in_snd_device);
876 }
877
878 /* Applicable only on the targets that has external modem.
879 * New device information should be sent to modem before enabling
880 * the devices to reduce in-call device switch time.
881 */
882 if ((usecase->type == VOICE_CALL) &&
883 (usecase->in_snd_device != SND_DEVICE_NONE) &&
884 (usecase->out_snd_device != SND_DEVICE_NONE)) {
885 status = platform_switch_voice_call_enable_device_config(adev->platform,
886 out_snd_device,
887 in_snd_device);
888 }
889
890 /* Enable new sound devices */
891 if (out_snd_device != SND_DEVICE_NONE) {
892 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
893 check_usecases_codec_backend(adev, usecase, out_snd_device);
894 enable_snd_device(adev, out_snd_device);
895 }
896
897 if (in_snd_device != SND_DEVICE_NONE) {
898 check_and_route_capture_usecases(adev, usecase, in_snd_device);
899 enable_snd_device(adev, in_snd_device);
900 }
901
902 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) {
903 status = platform_switch_voice_call_device_post(adev->platform,
904 out_snd_device,
905 in_snd_device);
906 enable_audio_route_for_voice_usecases(adev, usecase);
907 }
908
909 usecase->in_snd_device = in_snd_device;
910 usecase->out_snd_device = out_snd_device;
911
912 if (usecase->type == PCM_PLAYBACK) {
913 audio_extn_utils_update_stream_app_type_cfg(adev->platform,
914 &adev->streams_output_cfg_list,
915 usecase->stream.out->devices,
916 usecase->stream.out->flags,
917 usecase->stream.out->format,
918 usecase->stream.out->sample_rate,
919 usecase->stream.out->bit_width,
920 &usecase->stream.out->app_type_cfg);
921 ALOGI("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
922 }
923
924 enable_audio_route(adev, usecase);
925
926 /* Applicable only on the targets that has external modem.
927 * Enable device command should be sent to modem only after
928 * enabling voice call mixer controls
929 */
930 if (usecase->type == VOICE_CALL)
931 status = platform_switch_voice_call_usecase_route_post(adev->platform,
932 out_snd_device,
933 in_snd_device);
934 ALOGD("%s: done",__func__);
935
936 return status;
937}
938
939static int stop_input_stream(struct stream_in *in)
940{
941 int i, ret = 0;
942 struct audio_usecase *uc_info;
943 struct audio_device *adev = in->dev;
944
945 adev->active_input = NULL;
946
947 ALOGV("%s: enter: usecase(%d: %s)", __func__,
948 in->usecase, use_case_table[in->usecase]);
949 uc_info = get_usecase_from_list(adev, in->usecase);
950 if (uc_info == NULL) {
951 ALOGE("%s: Could not find the usecase (%d) in the list",
952 __func__, in->usecase);
953 return -EINVAL;
954 }
955
956 /* Close in-call recording streams */
957 voice_check_and_stop_incall_rec_usecase(adev, in);
958
959 /* 1. Disable stream specific mixer controls */
960 disable_audio_route(adev, uc_info);
961
962 /* 2. Disable the tx device */
963 disable_snd_device(adev, uc_info->in_snd_device);
964
965 list_remove(&uc_info->list);
966 free(uc_info);
967
968 ALOGV("%s: exit: status(%d)", __func__, ret);
969 return ret;
970}
971
972int start_input_stream(struct stream_in *in)
973{
974 /* 1. Enable output device and stream routing controls */
975 int ret = 0;
976 struct audio_usecase *uc_info;
977 struct audio_device *adev = in->dev;
978 int snd_card_status = get_snd_card_state(adev);
979
980 int usecase = platform_update_usecase_from_source(in->source,in->usecase);
981 if (get_usecase_from_list(adev, usecase) == NULL)
982 in->usecase = usecase;
983
984 ALOGD("%s: enter: stream(%p)usecase(%d: %s)",
985 __func__, &in->stream, in->usecase, use_case_table[in->usecase]);
986
987
988 if (SND_CARD_STATE_OFFLINE == snd_card_status) {
989 ALOGE("%s: sound card is not active/SSR returning error", __func__);
990 ret = -EIO;
991 goto error_config;
992 }
993
994 /* Check if source matches incall recording usecase criteria */
995 ret = voice_check_and_set_incall_rec_usecase(adev, in);
996 if (ret)
997 goto error_config;
998 else
999 ALOGV("%s: usecase(%d)", __func__, in->usecase);
1000
1001 if (get_usecase_from_list(adev, in->usecase) != NULL) {
1002 ALOGE("%s: use case assigned already in use, stream(%p)usecase(%d: %s)",
1003 __func__, &in->stream, in->usecase, use_case_table[in->usecase]);
1004 goto error_config;
1005 }
1006
1007 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
1008 if (in->pcm_device_id < 0) {
1009 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
1010 __func__, in->usecase);
1011 ret = -EINVAL;
1012 goto error_config;
1013 }
1014
1015 adev->active_input = in;
1016 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1017
1018 if (!uc_info) {
1019 ret = -ENOMEM;
1020 goto error_config;
1021 }
1022
1023 uc_info->id = in->usecase;
1024 uc_info->type = PCM_CAPTURE;
1025 uc_info->stream.in = in;
1026 uc_info->devices = in->device;
1027 uc_info->in_snd_device = SND_DEVICE_NONE;
1028 uc_info->out_snd_device = SND_DEVICE_NONE;
1029
1030 list_add_tail(&adev->usecase_list, &uc_info->list);
1031 audio_extn_perf_lock_acquire();
1032 select_devices(adev, in->usecase);
1033
1034 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
1035 __func__, adev->snd_card, in->pcm_device_id, in->config.channels);
1036
1037 unsigned int flags = PCM_IN;
1038 unsigned int pcm_open_retry_count = 0;
1039
1040 if (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY) {
1041 flags |= PCM_MMAP | PCM_NOIRQ;
1042 pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT;
1043 }
1044
1045 while (1) {
1046 in->pcm = pcm_open(adev->snd_card, in->pcm_device_id,
1047 flags, &in->config);
1048 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) {
1049 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
1050 if (in->pcm != NULL) {
1051 pcm_close(in->pcm);
1052 in->pcm = NULL;
1053 }
1054 if (pcm_open_retry_count-- == 0) {
1055 ret = -EIO;
1056 goto error_open;
1057 }
1058 usleep(PROXY_OPEN_WAIT_TIME * 1000);
1059 continue;
1060 }
1061 break;
1062 }
1063
1064 ALOGV("%s: pcm_prepare", __func__);
1065 ret = pcm_prepare(in->pcm);
1066 if (ret < 0) {
1067 ALOGE("%s: pcm_prepare returned %d", __func__, ret);
1068 pcm_close(in->pcm);
1069 in->pcm = NULL;
1070 goto error_open;
1071 }
1072
1073 audio_extn_perf_lock_release();
1074
1075 ALOGD("%s: exit", __func__);
1076
1077 return ret;
1078
1079error_open:
1080 stop_input_stream(in);
1081 audio_extn_perf_lock_release();
1082
1083error_config:
1084 adev->active_input = NULL;
1085 ALOGD("%s: exit: status(%d)", __func__, ret);
1086
1087 return ret;
1088}
1089
1090/* must be called with out->lock locked */
1091static int send_offload_cmd_l(struct stream_out* out, int command)
1092{
1093 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
1094
1095 if (!cmd) {
1096 ALOGE("failed to allocate mem for command 0x%x", command);
1097 return -ENOMEM;
1098 }
1099
1100 ALOGVV("%s %d", __func__, command);
1101
1102 cmd->cmd = command;
1103 list_add_tail(&out->offload_cmd_list, &cmd->node);
1104 pthread_cond_signal(&out->offload_cond);
1105 return 0;
1106}
1107
1108/* must be called iwth out->lock locked */
1109static void stop_compressed_output_l(struct stream_out *out)
1110{
1111 out->offload_state = OFFLOAD_STATE_IDLE;
1112 out->playback_started = 0;
1113 out->send_new_metadata = 1;
1114 if (out->compr != NULL) {
1115 compress_stop(out->compr);
1116 while (out->offload_thread_blocked) {
1117 pthread_cond_wait(&out->cond, &out->lock);
1118 }
1119 }
1120}
1121
1122bool is_offload_usecase(audio_usecase_t uc_id)
1123{
1124 unsigned int i;
1125 for (i = 0; i < sizeof(offload_usecases)/sizeof(offload_usecases[0]); i++) {
1126 if (uc_id == offload_usecases[i])
1127 return true;
1128 }
1129 return false;
1130}
1131
1132static audio_usecase_t get_offload_usecase(struct audio_device *adev)
1133{
1134 audio_usecase_t ret = USECASE_AUDIO_PLAYBACK_OFFLOAD;
1135 unsigned int i, num_usecase = sizeof(offload_usecases)/sizeof(offload_usecases[0]);
1136 char value[PROPERTY_VALUE_MAX] = {0};
1137
1138 property_get("audio.offload.multiple.enabled", value, NULL);
1139 if (!(atoi(value) || !strncmp("true", value, 4)))
1140 num_usecase = 1; /* If prop is not set, limit the num of offload usecases to 1 */
1141
1142 ALOGV("%s: num_usecase: %d", __func__, num_usecase);
1143 for (i = 0; i < num_usecase; i++) {
1144 if (!(adev->offload_usecases_state & (0x1<<i))) {
1145 adev->offload_usecases_state |= 0x1 << i;
1146 ret = offload_usecases[i];
1147 break;
1148 }
1149 }
1150 ALOGV("%s: offload usecase is %d", __func__, ret);
1151 return ret;
1152}
1153
1154static void free_offload_usecase(struct audio_device *adev,
1155 audio_usecase_t uc_id)
1156{
1157 unsigned int i;
1158 for (i = 0; i < sizeof(offload_usecases)/sizeof(offload_usecases[0]); i++) {
1159 if (offload_usecases[i] == uc_id) {
1160 adev->offload_usecases_state &= ~(0x1<<i);
1161 break;
1162 }
1163 }
1164 ALOGV("%s: free offload usecase %d", __func__, uc_id);
1165}
1166
1167static void *offload_thread_loop(void *context)
1168{
1169 struct stream_out *out = (struct stream_out *) context;
1170 struct listnode *item;
1171 int ret = 0;
1172
1173 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
1174 set_sched_policy(0, SP_FOREGROUND);
1175 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
1176
1177 ALOGV("%s", __func__);
1178 pthread_mutex_lock(&out->lock);
1179 for (;;) {
1180 struct offload_cmd *cmd = NULL;
1181 stream_callback_event_t event;
1182 bool send_callback = false;
1183
1184 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
1185 __func__, list_empty(&out->offload_cmd_list),
1186 out->offload_state);
1187 if (list_empty(&out->offload_cmd_list)) {
1188 ALOGV("%s SLEEPING", __func__);
1189 pthread_cond_wait(&out->offload_cond, &out->lock);
1190 ALOGV("%s RUNNING", __func__);
1191 continue;
1192 }
1193
1194 item = list_head(&out->offload_cmd_list);
1195 cmd = node_to_item(item, struct offload_cmd, node);
1196 list_remove(item);
1197
1198 ALOGVV("%s STATE %d CMD %d out->compr %p",
1199 __func__, out->offload_state, cmd->cmd, out->compr);
1200
1201 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
1202 free(cmd);
1203 break;
1204 }
1205
1206 if (out->compr == NULL) {
1207 ALOGE("%s: Compress handle is NULL", __func__);
1208 pthread_cond_signal(&out->cond);
1209 continue;
1210 }
1211 out->offload_thread_blocked = true;
1212 pthread_mutex_unlock(&out->lock);
1213 send_callback = false;
1214 switch(cmd->cmd) {
1215 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
1216 ALOGD("copl(%p):calling compress_wait", out);
1217 compress_wait(out->compr, -1);
1218 ALOGD("copl(%p):out of compress_wait", out);
1219 send_callback = true;
1220 event = STREAM_CBK_EVENT_WRITE_READY;
1221 break;
1222 case OFFLOAD_CMD_PARTIAL_DRAIN:
1223 ret = compress_next_track(out->compr);
1224 if(ret == 0) {
1225 ALOGD("copl(%p):calling compress_partial_drain", out);
1226 ret = compress_partial_drain(out->compr);
1227 ALOGD("copl(%p):out of compress_partial_drain", out);
1228 if (ret < 0)
1229 ret = -errno;
1230 }
1231 else if (ret == -ETIMEDOUT)
1232 compress_drain(out->compr);
1233 else
1234 ALOGE("%s: Next track returned error %d",__func__, ret);
1235
1236 if (ret != -ENETRESET) {
1237 send_callback = true;
1238 event = STREAM_CBK_EVENT_DRAIN_READY;
1239 ALOGV("copl(%p):send drain callback, ret %d", out, ret);
1240 } else
1241 ALOGE("%s: Block drain ready event during SSR", __func__);
1242 break;
1243 case OFFLOAD_CMD_DRAIN:
1244 ALOGD("copl(%p):calling compress_drain", out);
1245 compress_drain(out->compr);
1246 ALOGD("copl(%p):calling compress_drain", out);
1247 send_callback = true;
1248 event = STREAM_CBK_EVENT_DRAIN_READY;
1249 break;
1250 default:
1251 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
1252 break;
1253 }
1254 pthread_mutex_lock(&out->lock);
1255 out->offload_thread_blocked = false;
1256 pthread_cond_signal(&out->cond);
1257 if (send_callback) {
1258 out->offload_callback(event, NULL, out->offload_cookie);
1259 }
1260 free(cmd);
1261 }
1262
1263 pthread_cond_signal(&out->cond);
1264 while (!list_empty(&out->offload_cmd_list)) {
1265 item = list_head(&out->offload_cmd_list);
1266 list_remove(item);
1267 free(node_to_item(item, struct offload_cmd, node));
1268 }
1269 pthread_mutex_unlock(&out->lock);
1270
1271 return NULL;
1272}
1273
1274static int create_offload_callback_thread(struct stream_out *out)
1275{
1276 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
1277 list_init(&out->offload_cmd_list);
1278 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
1279 offload_thread_loop, out);
1280 return 0;
1281}
1282
1283static int destroy_offload_callback_thread(struct stream_out *out)
1284{
1285 pthread_mutex_lock(&out->lock);
1286 stop_compressed_output_l(out);
1287 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
1288
1289 pthread_mutex_unlock(&out->lock);
1290 pthread_join(out->offload_thread, (void **) NULL);
1291 pthread_cond_destroy(&out->offload_cond);
1292
1293 return 0;
1294}
1295
1296static bool allow_hdmi_channel_config(struct audio_device *adev)
1297{
1298 struct listnode *node;
1299 struct audio_usecase *usecase;
1300 bool ret = true;
1301
1302 list_for_each(node, &adev->usecase_list) {
1303 usecase = node_to_item(node, struct audio_usecase, list);
1304 if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1305 /*
1306 * If voice call is already existing, do not proceed further to avoid
1307 * disabling/enabling both RX and TX devices, CSD calls, etc.
1308 * Once the voice call done, the HDMI channels can be configured to
1309 * max channels of remaining use cases.
1310 */
1311 if (usecase->id == USECASE_VOICE_CALL) {
1312 ALOGD("%s: voice call is active, no change in HDMI channels",
1313 __func__);
1314 ret = false;
1315 break;
1316 } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1317 ALOGD("%s: multi channel playback is active, "
1318 "no change in HDMI channels", __func__);
1319 ret = false;
1320 break;
1321 } else if (is_offload_usecase(usecase->id) &&
1322 audio_channel_count_from_out_mask(usecase->stream.out->channel_mask) > 2) {
1323 ALOGD("%s: multi-channel(%x) compress offload playback is active, "
1324 "no change in HDMI channels", __func__, usecase->stream.out->channel_mask);
1325 ret = false;
1326 break;
1327 }
1328 }
1329 }
1330 return ret;
1331}
1332
1333static int check_and_set_hdmi_channels(struct audio_device *adev,
1334 unsigned int channels)
1335{
1336 struct listnode *node;
1337 struct audio_usecase *usecase;
1338
1339 /* Check if change in HDMI channel config is allowed */
1340 if (!allow_hdmi_channel_config(adev))
1341 return 0;
1342
1343 if (channels == adev->cur_hdmi_channels) {
1344 ALOGD("%s: Requested channels are same as current channels(%d)", __func__, channels);
1345 return 0;
1346 }
1347
1348 platform_set_hdmi_channels(adev->platform, channels);
1349 adev->cur_hdmi_channels = channels;
1350
1351 /*
1352 * Deroute all the playback streams routed to HDMI so that
1353 * the back end is deactivated. Note that backend will not
1354 * be deactivated if any one stream is connected to it.
1355 */
1356 list_for_each(node, &adev->usecase_list) {
1357 usecase = node_to_item(node, struct audio_usecase, list);
1358 if (usecase->type == PCM_PLAYBACK &&
1359 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1360 disable_audio_route(adev, usecase);
1361 }
1362 }
1363
1364 /*
1365 * Enable all the streams disabled above. Now the HDMI backend
1366 * will be activated with new channel configuration
1367 */
1368 list_for_each(node, &adev->usecase_list) {
1369 usecase = node_to_item(node, struct audio_usecase, list);
1370 if (usecase->type == PCM_PLAYBACK &&
1371 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1372 enable_audio_route(adev, usecase);
1373 }
1374 }
1375
1376 return 0;
1377}
1378
1379static int stop_output_stream(struct stream_out *out)
1380{
1381 int i, ret = 0;
1382 struct audio_usecase *uc_info;
1383 struct audio_device *adev = out->dev;
1384
1385 ALOGV("%s: enter: usecase(%d: %s)", __func__,
1386 out->usecase, use_case_table[out->usecase]);
1387 uc_info = get_usecase_from_list(adev, out->usecase);
1388 if (uc_info == NULL) {
1389 ALOGE("%s: Could not find the usecase (%d) in the list",
1390 __func__, out->usecase);
1391 return -EINVAL;
1392 }
1393
1394 if (is_offload_usecase(out->usecase)) {
1395 if (adev->visualizer_stop_output != NULL)
1396 adev->visualizer_stop_output(out->handle, out->pcm_device_id);
1397 if (adev->offload_effects_stop_output != NULL)
1398 adev->offload_effects_stop_output(out->handle, out->pcm_device_id);
1399 }
1400
1401 /* 1. Get and set stream specific mixer controls */
1402 disable_audio_route(adev, uc_info);
1403
1404 /* 2. Disable the rx device */
1405 disable_snd_device(adev, uc_info->out_snd_device);
1406
1407 list_remove(&uc_info->list);
1408 free(uc_info);
1409
1410 /* Must be called after removing the usecase from list */
1411 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1412 check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
1413
1414 ALOGV("%s: exit: status(%d)", __func__, ret);
1415 return ret;
1416}
1417
1418int start_output_stream(struct stream_out *out)
1419{
1420 int ret = 0;
1421 int sink_channels = 0;
1422 char prop_value[PROPERTY_VALUE_MAX] = {0};
1423 struct audio_usecase *uc_info;
1424 struct audio_device *adev = out->dev;
1425 int snd_card_status = get_snd_card_state(adev);
1426
1427 if ((out->usecase < 0) || (out->usecase >= AUDIO_USECASE_MAX)) {
1428 ret = -EINVAL;
1429 goto error_config;
1430 }
1431
1432 ALOGD("%s: enter: stream(%p)usecase(%d: %s) devices(%#x)",
1433 __func__, &out->stream, out->usecase, use_case_table[out->usecase],
1434 out->devices);
1435
1436 if (SND_CARD_STATE_OFFLINE == snd_card_status) {
1437 ALOGE("%s: sound card is not active/SSR returning error", __func__);
1438 ret = -EIO;
1439 goto error_config;
1440 }
1441
1442 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
1443 if (out->pcm_device_id < 0) {
1444 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
1445 __func__, out->pcm_device_id, out->usecase);
1446 ret = -EINVAL;
1447 goto error_config;
1448 }
1449
1450 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1451
1452 if (!uc_info) {
1453 ret = -ENOMEM;
1454 goto error_config;
1455 }
1456
1457 uc_info->id = out->usecase;
1458 uc_info->type = PCM_PLAYBACK;
1459 uc_info->stream.out = out;
1460 uc_info->devices = out->devices;
1461 uc_info->in_snd_device = SND_DEVICE_NONE;
1462 uc_info->out_snd_device = SND_DEVICE_NONE;
1463
1464 /* This must be called before adding this usecase to the list */
1465 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1466 property_get("audio.use.hdmi.sink.cap", prop_value, NULL);
1467 if (!strncmp("true", prop_value, 4)) {
1468 sink_channels = platform_edid_get_max_channels(out->dev->platform);
1469 ALOGD("%s: set HDMI channel count[%d] based on sink capability", __func__, sink_channels);
1470 check_and_set_hdmi_channels(adev, sink_channels);
1471 } else {
1472 if (is_offload_usecase(out->usecase))
1473 check_and_set_hdmi_channels(adev, out->compr_config.codec->ch_in);
1474 else
1475 check_and_set_hdmi_channels(adev, out->config.channels);
1476 }
1477 }
1478
1479 list_add_tail(&adev->usecase_list, &uc_info->list);
1480
1481 select_devices(adev, out->usecase);
1482
1483 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d) format(%#x)",
1484 __func__, adev->snd_card, out->pcm_device_id, out->config.format);
1485 if (!is_offload_usecase(out->usecase)) {
1486 unsigned int flags = PCM_OUT;
1487 unsigned int pcm_open_retry_count = 0;
1488 if (out->usecase == USECASE_AUDIO_PLAYBACK_AFE_PROXY) {
1489 flags |= PCM_MMAP | PCM_NOIRQ;
1490 pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT;
1491 } else
1492 flags |= PCM_MONOTONIC;
1493
1494 while (1) {
1495 out->pcm = pcm_open(adev->snd_card, out->pcm_device_id,
1496 flags, &out->config);
1497 if (out->pcm == NULL || !pcm_is_ready(out->pcm)) {
1498 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
1499 if (out->pcm != NULL) {
1500 pcm_close(out->pcm);
1501 out->pcm = NULL;
1502 }
1503 if (pcm_open_retry_count-- == 0) {
1504 ret = -EIO;
1505 goto error_open;
1506 }
1507 usleep(PROXY_OPEN_WAIT_TIME * 1000);
1508 continue;
1509 }
1510 break;
1511 }
1512
1513 ALOGV("%s: pcm_prepare", __func__);
1514 if (pcm_is_ready(out->pcm)) {
1515 ret = pcm_prepare(out->pcm);
1516 if (ret < 0) {
1517 ALOGE("%s: pcm_prepare returned %d", __func__, ret);
1518 pcm_close(out->pcm);
1519 out->pcm = NULL;
1520 goto error_open;
1521 }
1522 }
1523 } else {
1524 out->pcm = NULL;
1525 out->compr = compress_open(adev->snd_card,
1526 out->pcm_device_id,
1527 COMPRESS_IN, &out->compr_config);
1528 if (out->compr && !is_compress_ready(out->compr)) {
1529 ALOGE("%s: %s", __func__, compress_get_error(out->compr));
1530 compress_close(out->compr);
1531 out->compr = NULL;
1532 ret = -EIO;
1533 goto error_open;
1534 }
1535 if (out->offload_callback)
1536 compress_nonblock(out->compr, out->non_blocking);
1537
1538#ifdef DS1_DOLBY_DDP_ENABLED
1539 if (audio_extn_is_dolby_format(out->format))
1540 audio_extn_dolby_send_ddp_endp_params(adev);
1541#endif
1542
1543 if (adev->visualizer_start_output != NULL)
1544 adev->visualizer_start_output(out->handle, out->pcm_device_id);
1545 if (adev->offload_effects_start_output != NULL)
1546 adev->offload_effects_start_output(out->handle, out->pcm_device_id);
1547 }
1548
1549 ALOGD("%s: exit", __func__);
1550
1551 return 0;
1552error_open:
1553 stop_output_stream(out);
1554error_config:
1555 return ret;
1556}
1557
1558static int check_input_parameters(uint32_t sample_rate,
1559 audio_format_t format,
1560 int channel_count)
1561{
1562 int ret = 0;
1563
1564 if ((format != AUDIO_FORMAT_PCM_16_BIT) &&
1565 !voice_extn_compress_voip_is_format_supported(format) &&
1566 !audio_extn_compr_cap_format_supported(format)) ret = -EINVAL;
1567
1568 switch (channel_count) {
1569 case 1:
1570 case 2:
1571 case 6:
1572 break;
1573 default:
1574 ret = -EINVAL;
1575 }
1576
1577 switch (sample_rate) {
1578 case 8000:
1579 case 11025:
1580 case 12000:
1581 case 16000:
1582 case 22050:
1583 case 24000:
1584 case 32000:
1585 case 44100:
1586 case 48000:
1587 break;
1588 default:
1589 ret = -EINVAL;
1590 }
1591
1592 return ret;
1593}
1594
1595static size_t get_input_buffer_size(uint32_t sample_rate,
1596 audio_format_t format,
1597 int channel_count,
1598 bool is_low_latency)
1599{
1600 size_t size = 0;
1601
1602 if (check_input_parameters(sample_rate, format, channel_count) != 0)
1603 return 0;
1604
1605 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
1606 if (is_low_latency)
1607 size = configured_low_latency_capture_period_size;
1608 /* ToDo: should use frame_size computed based on the format and
1609 channel_count here. */
1610 size *= sizeof(short) * channel_count;
1611
1612 /* make sure the size is multiple of 32 bytes
1613 * At 48 kHz mono 16-bit PCM:
1614 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
1615 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
1616 */
1617 size += 0x1f;
1618 size &= ~0x1f;
1619
1620 return size;
1621}
1622
1623static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1624{
1625 struct stream_out *out = (struct stream_out *)stream;
1626
1627 return out->sample_rate;
1628}
1629
1630static int out_set_sample_rate(struct audio_stream *stream __unused,
1631 uint32_t rate __unused)
1632{
1633 return -ENOSYS;
1634}
1635
1636static size_t out_get_buffer_size(const struct audio_stream *stream)
1637{
1638 struct stream_out *out = (struct stream_out *)stream;
1639
1640 if (is_offload_usecase(out->usecase))
1641 return out->compr_config.fragment_size;
1642 else if(out->usecase == USECASE_COMPRESS_VOIP_CALL)
1643 return voice_extn_compress_voip_out_get_buffer_size(out);
1644
1645 return out->config.period_size *
1646 audio_stream_out_frame_size((const struct audio_stream_out *)stream);
1647}
1648
1649static uint32_t out_get_channels(const struct audio_stream *stream)
1650{
1651 struct stream_out *out = (struct stream_out *)stream;
1652
1653 return out->channel_mask;
1654}
1655
1656static audio_format_t out_get_format(const struct audio_stream *stream)
1657{
1658 struct stream_out *out = (struct stream_out *)stream;
1659
1660 return out->format;
1661}
1662
1663static int out_set_format(struct audio_stream *stream __unused,
1664 audio_format_t format __unused)
1665{
1666 return -ENOSYS;
1667}
1668
1669static int out_standby(struct audio_stream *stream)
1670{
1671 struct stream_out *out = (struct stream_out *)stream;
1672 struct audio_device *adev = out->dev;
1673
1674 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
1675 stream, out->usecase, use_case_table[out->usecase]);
1676 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
1677 /* Ignore standby in case of voip call because the voip output
1678 * stream is closed in adev_close_output_stream()
1679 */
1680 ALOGD("%s: Ignore Standby in VOIP call", __func__);
1681 return 0;
1682 }
1683
1684 pthread_mutex_lock(&out->lock);
1685 if (!out->standby) {
1686 pthread_mutex_lock(&adev->lock);
1687 out->standby = true;
1688 if (!is_offload_usecase(out->usecase)) {
1689 if (out->pcm) {
1690 pcm_close(out->pcm);
1691 out->pcm = NULL;
1692 }
1693 } else {
1694 ALOGD("copl(%p):standby", out);
1695 stop_compressed_output_l(out);
1696 out->gapless_mdata.encoder_delay = 0;
1697 out->gapless_mdata.encoder_padding = 0;
1698 if (out->compr != NULL) {
1699 compress_close(out->compr);
1700 out->compr = NULL;
1701 }
1702 }
1703 stop_output_stream(out);
1704 pthread_mutex_unlock(&adev->lock);
1705 }
1706 pthread_mutex_unlock(&out->lock);
1707 ALOGV("%s: exit", __func__);
1708 return 0;
1709}
1710
1711static int out_dump(const struct audio_stream *stream __unused,
1712 int fd __unused)
1713{
1714 return 0;
1715}
1716
1717static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
1718{
1719 int ret = 0;
1720 char value[32];
1721 bool is_meta_data_params = false;
1722
1723 if (!out || !parms) {
1724 ALOGE("%s: return invalid ",__func__);
1725 return -EINVAL;
1726 }
1727
1728 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_FORMAT, value, sizeof(value));
1729 if (ret >= 0) {
1730 if (atoi(value) == SND_AUDIOSTREAMFORMAT_MP4ADTS) {
1731 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_MP4ADTS;
1732 ALOGV("ADTS format is set in offload mode");
1733 }
1734 out->send_new_metadata = 1;
1735 }
1736
1737 ret = audio_extn_parse_compress_metadata(out, parms);
1738
1739 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_SAMPLE_RATE, value, sizeof(value));
1740 if(ret >= 0)
1741 is_meta_data_params = true;
1742 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_NUM_CHANNEL, value, sizeof(value));
1743 if(ret >= 0)
1744 is_meta_data_params = true;
1745 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_AVG_BIT_RATE, value, sizeof(value));
1746 if(ret >= 0)
1747 is_meta_data_params = true;
1748 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
1749 if (ret >= 0) {
1750 is_meta_data_params = true;
1751 out->gapless_mdata.encoder_delay = atoi(value); //whats a good limit check?
1752 }
1753 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
1754 if (ret >= 0) {
1755 is_meta_data_params = true;
1756 out->gapless_mdata.encoder_padding = atoi(value);
1757 }
1758
1759 if(!is_meta_data_params) {
1760 ALOGV("%s: Not gapless meta data params", __func__);
1761 return 0;
1762 }
1763 out->send_new_metadata = 1;
1764 ALOGV("%s new encoder delay %u and padding %u", __func__,
1765 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
1766
1767 return 0;
1768}
1769
1770static bool output_drives_call(struct audio_device *adev, struct stream_out *out)
1771{
1772 return out == adev->primary_output || out == adev->voice_tx_output;
1773}
1774
1775static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1776{
1777 struct stream_out *out = (struct stream_out *)stream;
1778 struct audio_device *adev = out->dev;
1779 struct audio_usecase *usecase;
1780 struct listnode *node;
1781 struct str_parms *parms;
1782 char value[32];
1783 int ret = 0, val = 0, err;
1784 bool select_new_device = false;
1785
1786 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
1787 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
1788 parms = str_parms_create_str(kvpairs);
1789 if (!parms)
1790 goto error;
1791 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1792 if (err >= 0) {
1793 val = atoi(value);
1794 pthread_mutex_lock(&out->lock);
1795 pthread_mutex_lock(&adev->lock);
1796
1797 /*
1798 * When HDMI cable is unplugged/usb hs is disconnected the
1799 * music playback is paused and the policy manager sends routing=0
1800 * But the audioflingercontinues to write data until standby time
1801 * (3sec). As the HDMI core is turned off, the write gets blocked.
1802 * Avoid this by routing audio to speaker until standby.
1803 */
1804 if ((out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL ||
1805 out->devices == AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET) &&
1806 val == AUDIO_DEVICE_NONE) {
1807 val = AUDIO_DEVICE_OUT_SPEAKER;
1808 }
1809
1810 /*
1811 * select_devices() call below switches all the usecases on the same
1812 * backend to the new device. Refer to check_usecases_codec_backend() in
1813 * the select_devices(). But how do we undo this?
1814 *
1815 * For example, music playback is active on headset (deep-buffer usecase)
1816 * and if we go to ringtones and select a ringtone, low-latency usecase
1817 * will be started on headset+speaker. As we can't enable headset+speaker
1818 * and headset devices at the same time, select_devices() switches the music
1819 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1820 * So when the ringtone playback is completed, how do we undo the same?
1821 *
1822 * We are relying on the out_set_parameters() call on deep-buffer output,
1823 * once the ringtone playback is ended.
1824 * NOTE: We should not check if the current devices are same as new devices.
1825 * Because select_devices() must be called to switch back the music
1826 * playback to headset.
1827 */
1828 if (val != 0) {
1829 out->devices = val;
1830
1831 if (!out->standby)
1832 select_devices(adev, out->usecase);
1833
1834 if ((adev->mode == AUDIO_MODE_IN_CALL) &&
1835 output_drives_call(adev, out)) {
1836 adev->current_call_output = out;
1837 if (!voice_is_in_call(adev))
1838 ret = voice_start_call(adev);
1839 else
1840 voice_update_devices_for_all_voice_usecases(adev);
1841 }
1842 }
1843
1844 pthread_mutex_unlock(&adev->lock);
1845 pthread_mutex_unlock(&out->lock);
1846 }
1847
1848 if (out == adev->primary_output) {
1849 pthread_mutex_lock(&adev->lock);
1850 audio_extn_set_parameters(adev, parms);
1851 pthread_mutex_unlock(&adev->lock);
1852 }
1853 if (is_offload_usecase(out->usecase)) {
1854 pthread_mutex_lock(&out->lock);
1855 parse_compress_metadata(out, parms);
1856 pthread_mutex_unlock(&out->lock);
1857 }
1858
1859 str_parms_destroy(parms);
1860error:
1861 ALOGV("%s: exit: code(%d)", __func__, ret);
1862 return ret;
1863}
1864
1865static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1866{
1867 struct stream_out *out = (struct stream_out *)stream;
1868 struct str_parms *query = str_parms_create_str(keys);
1869 char *str;
1870 char value[256];
1871 struct str_parms *reply = str_parms_create();
1872 size_t i, j;
1873 int ret;
1874 bool first = true;
1875
1876 if (!query || !reply) {
1877 ALOGE("out_get_parameters: failed to allocate mem for query or reply");
1878 return NULL;
1879 }
1880
1881 ALOGV("%s: enter: keys - %s", __func__, keys);
1882 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1883 if (ret >= 0) {
1884 value[0] = '\0';
1885 i = 0;
1886 while (out->supported_channel_masks[i] != 0) {
1887 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1888 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1889 if (!first) {
1890 strcat(value, "|");
1891 }
1892 strcat(value, out_channels_name_to_enum_table[j].name);
1893 first = false;
1894 break;
1895 }
1896 }
1897 i++;
1898 }
1899 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1900 str = str_parms_to_str(reply);
1901 } else {
1902 voice_extn_out_get_parameters(out, query, reply);
1903 str = str_parms_to_str(reply);
1904 if (!strncmp(str, "", sizeof(""))) {
1905 free(str);
1906 str = strdup(keys);
1907 }
1908 }
1909 str_parms_destroy(query);
1910 str_parms_destroy(reply);
1911 ALOGV("%s: exit: returns - %s", __func__, str);
1912 return str;
1913}
1914
1915static uint32_t out_get_latency(const struct audio_stream_out *stream)
1916{
1917 struct stream_out *out = (struct stream_out *)stream;
1918
1919 if (is_offload_usecase(out->usecase))
1920 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1921
1922 return (out->config.period_count * out->config.period_size * 1000) /
1923 (out->config.rate);
1924}
1925
1926static int out_set_volume(struct audio_stream_out *stream, float left,
1927 float right)
1928{
1929 struct stream_out *out = (struct stream_out *)stream;
1930 int volume[2];
1931
1932 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1933 /* only take left channel into account: the API is for stereo anyway */
1934 out->muted = (left == 0.0f);
1935 return 0;
1936 } else if (is_offload_usecase(out->usecase)) {
1937 char mixer_ctl_name[128];
1938 struct audio_device *adev = out->dev;
1939 struct mixer_ctl *ctl;
1940 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
1941 PCM_PLAYBACK);
1942
1943 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1944 "Compress Playback %d Volume", pcm_device_id);
1945 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1946 if (!ctl) {
1947 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1948 __func__, mixer_ctl_name);
1949 return -EINVAL;
1950 }
1951 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
1952 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
1953 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
1954 return 0;
1955 }
1956
1957 return -ENOSYS;
1958}
1959
1960static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1961 size_t bytes)
1962{
1963 struct stream_out *out = (struct stream_out *)stream;
1964 struct audio_device *adev = out->dev;
1965 int snd_scard_state = get_snd_card_state(adev);
1966 ssize_t ret = 0;
1967
1968 pthread_mutex_lock(&out->lock);
1969
1970 if (SND_CARD_STATE_OFFLINE == snd_scard_state) {
1971 // increase written size during SSR to avoid mismatch
1972 // with the written frames count in AF
1973 if (!is_offload_usecase(out->usecase))
1974 out->written += bytes / (out->config.channels * sizeof(short));
1975
1976 if (out->pcm) {
1977 ALOGD(" %s: sound card is not active/SSR state", __func__);
1978 ret= -EIO;
1979 goto exit;
1980 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1981 //during SSR for compress usecase we should return error to flinger
1982 ALOGD(" copl %s: sound card is not active/SSR state", __func__);
1983 pthread_mutex_unlock(&out->lock);
1984 return -ENETRESET;
1985 }
1986 }
1987
1988 if (out->standby) {
1989 out->standby = false;
1990 pthread_mutex_lock(&adev->lock);
1991 if (out->usecase == USECASE_COMPRESS_VOIP_CALL)
1992 ret = voice_extn_compress_voip_start_output_stream(out);
1993 else
1994 ret = start_output_stream(out);
1995 pthread_mutex_unlock(&adev->lock);
1996 /* ToDo: If use case is compress offload should return 0 */
1997 if (ret != 0) {
1998 out->standby = true;
1999 goto exit;
2000 }
2001 }
2002
2003 if (is_offload_usecase(out->usecase)) {
2004 ALOGD("copl(%p): writing buffer (%zu bytes) to compress device", out, bytes);
2005 if (out->send_new_metadata) {
2006 ALOGD("copl(%p):send new gapless metadata", out);
2007 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
2008 out->send_new_metadata = 0;
2009 }
2010
2011 ret = compress_write(out->compr, buffer, bytes);
2012 if (ret < 0)
2013 ret = -errno;
2014 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
2015 if (ret >= 0 && ret < (ssize_t)bytes) {
2016 ALOGD("No space available in compress driver, post msg to cb thread");
2017 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
2018 } else if (-ENETRESET == ret) {
2019 ALOGE("copl %s: received sound card offline state on compress write", __func__);
2020 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
2021 pthread_mutex_unlock(&out->lock);
2022 out_standby(&out->stream.common);
2023 return ret;
2024 }
2025 if (!out->playback_started && ret >= 0) {
2026 compress_start(out->compr);
2027 out->playback_started = 1;
2028 out->offload_state = OFFLOAD_STATE_PLAYING;
2029 }
2030 pthread_mutex_unlock(&out->lock);
2031 return ret;
2032 } else {
2033 if (out->pcm) {
2034 if (out->muted)
2035 memset((void *)buffer, 0, bytes);
2036 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
2037 if (out->usecase == USECASE_AUDIO_PLAYBACK_AFE_PROXY)
2038 ret = pcm_mmap_write(out->pcm, (void *)buffer, bytes);
2039 else
2040 ret = pcm_write(out->pcm, (void *)buffer, bytes);
2041 if (ret < 0)
2042 ret = -errno;
2043 else if (ret == 0)
2044 out->written += bytes / (out->config.channels * sizeof(short));
2045 }
2046 }
2047
2048exit:
2049 /* ToDo: There may be a corner case when SSR happens back to back during
2050 start/stop. Need to post different error to handle that. */
2051 if (-ENETRESET == ret) {
2052 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
2053 }
2054
2055 pthread_mutex_unlock(&out->lock);
2056
2057 if (ret != 0) {
2058 if (out->pcm)
2059 ALOGE("%s: error %ld - %s", __func__, ret, pcm_get_error(out->pcm));
2060 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
2061 pthread_mutex_lock(&adev->lock);
2062 voice_extn_compress_voip_close_output_stream(&out->stream.common);
2063 pthread_mutex_unlock(&adev->lock);
2064 out->standby = true;
2065 }
2066 out_standby(&out->stream.common);
2067 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
2068 out_get_sample_rate(&out->stream.common));
2069
2070 }
2071 return bytes;
2072}
2073
2074static int out_get_render_position(const struct audio_stream_out *stream,
2075 uint32_t *dsp_frames)
2076{
2077 struct stream_out *out = (struct stream_out *)stream;
2078 struct audio_device *adev = out->dev;
2079
2080 if (dsp_frames == NULL)
2081 return -EINVAL;
2082
2083 *dsp_frames = 0;
2084 if (is_offload_usecase(out->usecase)) {
2085 ssize_t ret = 0;
2086 pthread_mutex_lock(&out->lock);
2087 if (out->compr != NULL) {
2088 ret = compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
2089 &out->sample_rate);
2090 if (ret < 0)
2091 ret = -errno;
2092 ALOGVV("%s rendered frames %d sample_rate %d",
2093 __func__, *dsp_frames, out->sample_rate);
2094 }
2095 pthread_mutex_unlock(&out->lock);
2096 if (-ENETRESET == ret) {
2097 ALOGE(" ERROR: sound card not active Unable to get time stamp from compress driver");
2098 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
2099 return -EINVAL;
2100 } else if(ret < 0) {
2101 ALOGE(" ERROR: Unable to get time stamp from compress driver");
2102 return -EINVAL;
2103 } else if (get_snd_card_state(adev) == SND_CARD_STATE_OFFLINE){
2104 /*
2105 * Handle corner case where compress session is closed during SSR
2106 * and timestamp is queried
2107 */
2108 ALOGE(" ERROR: sound card not active, return error");
2109 return -EINVAL;
2110 } else {
2111 return 0;
2112 }
2113 } else if (audio_is_linear_pcm(out->format)) {
2114 *dsp_frames = out->written;
2115 return 0;
2116 } else
2117 return -EINVAL;
2118}
2119
2120static int out_add_audio_effect(const struct audio_stream *stream __unused,
2121 effect_handle_t effect __unused)
2122{
2123 return 0;
2124}
2125
2126static int out_remove_audio_effect(const struct audio_stream *stream __unused,
2127 effect_handle_t effect __unused)
2128{
2129 return 0;
2130}
2131
2132static int out_get_next_write_timestamp(const struct audio_stream_out *stream __unused,
2133 int64_t *timestamp __unused)
2134{
2135 return -EINVAL;
2136}
2137
2138static int out_get_presentation_position(const struct audio_stream_out *stream,
2139 uint64_t *frames, struct timespec *timestamp)
2140{
2141 struct stream_out *out = (struct stream_out *)stream;
2142 int ret = -1;
2143 unsigned long dsp_frames;
2144
2145 pthread_mutex_lock(&out->lock);
2146
2147 if (is_offload_usecase(out->usecase)) {
2148 if (out->compr != NULL) {
2149 ret = compress_get_tstamp(out->compr, &dsp_frames,
2150 &out->sample_rate);
2151 ALOGVV("%s rendered frames %ld sample_rate %d",
2152 __func__, dsp_frames, out->sample_rate);
2153 *frames = dsp_frames;
2154 if (ret < 0)
2155 ret = -errno;
2156 if (-ENETRESET == ret) {
2157 ALOGE(" ERROR: sound card not active Unable to get time stamp from compress driver");
2158 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
2159 ret = -EINVAL;
2160 } else
2161 ret = 0;
2162
2163 /* this is the best we can do */
2164 clock_gettime(CLOCK_MONOTONIC, timestamp);
2165 }
2166 } else {
2167 if (out->pcm) {
2168 unsigned int avail;
2169 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
2170 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
2171 int64_t signed_frames = out->written - kernel_buffer_size + avail;
2172 // This adjustment accounts for buffering after app processor.
2173 // It is based on estimated DSP latency per use case, rather than exact.
2174 signed_frames -=
2175 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
2176
2177 // It would be unusual for this value to be negative, but check just in case ...
2178 if (signed_frames >= 0) {
2179 *frames = signed_frames;
2180 ret = 0;
2181 }
2182 }
2183 }
2184 }
2185
2186 pthread_mutex_unlock(&out->lock);
2187
2188 return ret;
2189}
2190
2191static int out_set_callback(struct audio_stream_out *stream,
2192 stream_callback_t callback, void *cookie)
2193{
2194 struct stream_out *out = (struct stream_out *)stream;
2195
2196 ALOGV("%s", __func__);
2197 pthread_mutex_lock(&out->lock);
2198 out->offload_callback = callback;
2199 out->offload_cookie = cookie;
2200 pthread_mutex_unlock(&out->lock);
2201 return 0;
2202}
2203
2204static int out_pause(struct audio_stream_out* stream)
2205{
2206 struct stream_out *out = (struct stream_out *)stream;
2207 int status = -ENOSYS;
2208 ALOGV("%s", __func__);
2209 if (is_offload_usecase(out->usecase)) {
2210 ALOGD("copl(%p):pause compress driver", out);
2211 pthread_mutex_lock(&out->lock);
2212 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
2213 struct audio_device *adev = out->dev;
2214 int snd_scard_state = get_snd_card_state(adev);
2215
2216 if (SND_CARD_STATE_ONLINE == snd_scard_state)
2217 status = compress_pause(out->compr);
2218
2219 out->offload_state = OFFLOAD_STATE_PAUSED;
2220 }
2221 pthread_mutex_unlock(&out->lock);
2222 }
2223 return status;
2224}
2225
2226static int out_resume(struct audio_stream_out* stream)
2227{
2228 struct stream_out *out = (struct stream_out *)stream;
2229 int status = -ENOSYS;
2230 ALOGV("%s", __func__);
2231 if (is_offload_usecase(out->usecase)) {
2232 ALOGD("copl(%p):resume compress driver", out);
2233 status = 0;
2234 pthread_mutex_lock(&out->lock);
2235 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
2236 struct audio_device *adev = out->dev;
2237 int snd_scard_state = get_snd_card_state(adev);
2238
2239 if (SND_CARD_STATE_ONLINE == snd_scard_state)
2240 status = compress_resume(out->compr);
2241
2242 out->offload_state = OFFLOAD_STATE_PLAYING;
2243 }
2244 pthread_mutex_unlock(&out->lock);
2245 }
2246 return status;
2247}
2248
2249static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
2250{
2251 struct stream_out *out = (struct stream_out *)stream;
2252 int status = -ENOSYS;
2253 ALOGV("%s", __func__);
2254 if (is_offload_usecase(out->usecase)) {
2255 pthread_mutex_lock(&out->lock);
2256 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
2257 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
2258 else
2259 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
2260 pthread_mutex_unlock(&out->lock);
2261 }
2262 return status;
2263}
2264
2265static int out_flush(struct audio_stream_out* stream)
2266{
2267 struct stream_out *out = (struct stream_out *)stream;
2268 ALOGV("%s", __func__);
2269 if (is_offload_usecase(out->usecase)) {
2270 ALOGD("copl(%p):calling compress flush", out);
2271 pthread_mutex_lock(&out->lock);
2272 stop_compressed_output_l(out);
2273 pthread_mutex_unlock(&out->lock);
2274 ALOGD("copl(%p):out of compress flush", out);
2275 return 0;
2276 }
2277 return -ENOSYS;
2278}
2279
2280/** audio_stream_in implementation **/
2281static uint32_t in_get_sample_rate(const struct audio_stream *stream)
2282{
2283 struct stream_in *in = (struct stream_in *)stream;
2284
2285 return in->config.rate;
2286}
2287
2288static int in_set_sample_rate(struct audio_stream *stream __unused,
2289 uint32_t rate __unused)
2290{
2291 return -ENOSYS;
2292}
2293
2294static size_t in_get_buffer_size(const struct audio_stream *stream)
2295{
2296 struct stream_in *in = (struct stream_in *)stream;
2297
2298 if(in->usecase == USECASE_COMPRESS_VOIP_CALL)
2299 return voice_extn_compress_voip_in_get_buffer_size(in);
2300 else if(audio_extn_compr_cap_usecase_supported(in->usecase))
2301 return audio_extn_compr_cap_get_buffer_size(in->config.format);
2302
2303 return in->config.period_size *
2304 audio_stream_in_frame_size((const struct audio_stream_in *)stream);
2305}
2306
2307static uint32_t in_get_channels(const struct audio_stream *stream)
2308{
2309 struct stream_in *in = (struct stream_in *)stream;
2310
2311 return in->channel_mask;
2312}
2313
2314static audio_format_t in_get_format(const struct audio_stream *stream)
2315{
2316 struct stream_in *in = (struct stream_in *)stream;
2317
2318 return in->format;
2319}
2320
2321static int in_set_format(struct audio_stream *stream __unused,
2322 audio_format_t format __unused)
2323{
2324 return -ENOSYS;
2325}
2326
2327static int in_standby(struct audio_stream *stream)
2328{
2329 struct stream_in *in = (struct stream_in *)stream;
2330 struct audio_device *adev = in->dev;
2331 int status = 0;
2332 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
2333 stream, in->usecase, use_case_table[in->usecase]);
2334
2335 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
2336 /* Ignore standby in case of voip call because the voip input
2337 * stream is closed in adev_close_input_stream()
2338 */
2339 ALOGV("%s: Ignore Standby in VOIP call", __func__);
2340 return status;
2341 }
2342
2343 pthread_mutex_lock(&in->lock);
2344 if (!in->standby && in->is_st_session) {
2345 ALOGD("%s: sound trigger pcm stop lab", __func__);
2346 audio_extn_sound_trigger_stop_lab(in);
2347 in->standby = 1;
2348 }
2349
2350 if (!in->standby) {
2351 pthread_mutex_lock(&adev->lock);
2352 in->standby = true;
2353 if (in->pcm) {
2354 pcm_close(in->pcm);
2355 in->pcm = NULL;
2356 }
2357 status = stop_input_stream(in);
2358 pthread_mutex_unlock(&adev->lock);
2359 }
2360 pthread_mutex_unlock(&in->lock);
2361 ALOGV("%s: exit: status(%d)", __func__, status);
2362 return status;
2363}
2364
2365static int in_dump(const struct audio_stream *stream __unused,
2366 int fd __unused)
2367{
2368 return 0;
2369}
2370
2371static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
2372{
2373 struct stream_in *in = (struct stream_in *)stream;
2374 struct audio_device *adev = in->dev;
2375 struct str_parms *parms;
2376 char *str;
2377 char value[32];
2378 int ret = 0, val = 0, err;
2379
2380 ALOGD("%s: enter: kvpairs=%s", __func__, kvpairs);
2381 parms = str_parms_create_str(kvpairs);
2382
2383 if (!parms)
2384 goto error;
2385 pthread_mutex_lock(&in->lock);
2386 pthread_mutex_lock(&adev->lock);
2387
2388 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
2389 if (err >= 0) {
2390 val = atoi(value);
2391 /* no audio source uses val == 0 */
2392 if ((in->source != val) && (val != 0)) {
2393 in->source = val;
2394 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
2395 (in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
2396 (voice_extn_compress_voip_is_format_supported(in->format)) &&
2397 (in->config.rate == 8000 || in->config.rate == 16000) &&
2398 (audio_channel_count_from_in_mask(in->channel_mask) == 1)) {
2399 err = voice_extn_compress_voip_open_input_stream(in);
2400 if (err != 0) {
2401 ALOGE("%s: Compress voip input cannot be opened, error:%d",
2402 __func__, err);
2403 }
2404 }
2405 }
2406 }
2407
2408 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
2409 if (err >= 0) {
2410 val = atoi(value);
2411 if (((int)in->device != val) && (val != 0)) {
2412 in->device = val;
2413 /* If recording is in progress, change the tx device to new device */
2414 if (!in->standby && !in->is_st_session)
2415 ret = select_devices(adev, in->usecase);
2416 }
2417 }
2418
2419done:
2420 pthread_mutex_unlock(&adev->lock);
2421 pthread_mutex_unlock(&in->lock);
2422
2423 str_parms_destroy(parms);
2424error:
2425 ALOGV("%s: exit: status(%d)", __func__, ret);
2426 return ret;
2427}
2428
2429static char* in_get_parameters(const struct audio_stream *stream,
2430 const char *keys)
2431{
2432 struct stream_in *in = (struct stream_in *)stream;
2433 struct str_parms *query = str_parms_create_str(keys);
2434 char *str;
2435 char value[256];
2436 struct str_parms *reply = str_parms_create();
2437
2438 if (!query || !reply) {
2439 ALOGE("in_get_parameters: failed to create query or reply");
2440 return NULL;
2441 }
2442
2443 ALOGV("%s: enter: keys - %s", __func__, keys);
2444
2445 voice_extn_in_get_parameters(in, query, reply);
2446
2447 str = str_parms_to_str(reply);
2448 str_parms_destroy(query);
2449 str_parms_destroy(reply);
2450
2451 ALOGV("%s: exit: returns - %s", __func__, str);
2452 return str;
2453}
2454
2455static int in_set_gain(struct audio_stream_in *stream __unused,
2456 float gain __unused)
2457{
2458 return 0;
2459}
2460
2461static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
2462 size_t bytes)
2463{
2464 struct stream_in *in = (struct stream_in *)stream;
2465 struct audio_device *adev = in->dev;
2466 int i, ret = -1;
2467 int snd_scard_state = get_snd_card_state(adev);
2468
2469 pthread_mutex_lock(&in->lock);
2470
2471 if (in->pcm) {
2472 if(SND_CARD_STATE_OFFLINE == snd_scard_state) {
2473 ALOGD(" %s: sound card is not active/SSR state", __func__);
2474 ret= -EIO;;
2475 goto exit;
2476 }
2477 }
2478
2479 if (in->standby) {
2480 if (!in->is_st_session) {
2481 pthread_mutex_lock(&adev->lock);
2482 if (in->usecase == USECASE_COMPRESS_VOIP_CALL)
2483 ret = voice_extn_compress_voip_start_input_stream(in);
2484 else
2485 ret = start_input_stream(in);
2486 pthread_mutex_unlock(&adev->lock);
2487 if (ret != 0) {
2488 goto exit;
2489 }
2490 }
2491 in->standby = 0;
2492 }
2493
2494 if (in->pcm) {
2495 if (audio_extn_ssr_get_enabled() &&
2496 audio_channel_count_from_in_mask(in->channel_mask) == 6)
2497 ret = audio_extn_ssr_read(stream, buffer, bytes);
2498 else if (audio_extn_compr_cap_usecase_supported(in->usecase))
2499 ret = audio_extn_compr_cap_read(in, buffer, bytes);
2500 else if (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY)
2501 ret = pcm_mmap_read(in->pcm, buffer, bytes);
2502 else
2503 ret = pcm_read(in->pcm, buffer, bytes);
2504 if (ret < 0)
2505 ret = -errno;
2506 }
2507
2508 /*
2509 * Instead of writing zeroes here, we could trust the hardware
2510 * to always provide zeroes when muted.
2511 */
2512 if (ret == 0 && voice_get_mic_mute(adev) && !voice_is_in_call_rec_stream(in))
2513 memset(buffer, 0, bytes);
2514
2515exit:
2516 /* ToDo: There may be a corner case when SSR happens back to back during
2517 start/stop. Need to post different error to handle that. */
2518 if (-ENETRESET == ret) {
2519 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
2520 }
2521 pthread_mutex_unlock(&in->lock);
2522
2523 if (ret != 0) {
2524 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
2525 pthread_mutex_lock(&adev->lock);
2526 voice_extn_compress_voip_close_input_stream(&in->stream.common);
2527 pthread_mutex_unlock(&adev->lock);
2528 in->standby = true;
2529 }
2530 memset(buffer, 0, bytes);
2531 in_standby(&in->stream.common);
2532 ALOGV("%s: read failed status %d- sleeping for buffer duration", __func__, ret);
2533 usleep(bytes * 1000000 / audio_stream_in_frame_size(stream) /
2534 in_get_sample_rate(&in->stream.common));
2535 }
2536 return bytes;
2537}
2538
2539static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream __unused)
2540{
2541 return 0;
2542}
2543
2544static int add_remove_audio_effect(const struct audio_stream *stream,
2545 effect_handle_t effect,
2546 bool enable)
2547{
2548 struct stream_in *in = (struct stream_in *)stream;
2549 int status = 0;
2550 effect_descriptor_t desc;
2551
2552 status = (*effect)->get_descriptor(effect, &desc);
2553 if (status != 0)
2554 return status;
2555
2556 pthread_mutex_lock(&in->lock);
2557 pthread_mutex_lock(&in->dev->lock);
2558 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
2559 in->enable_aec != enable &&
2560 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
2561 in->enable_aec = enable;
2562 if (!in->standby)
2563 select_devices(in->dev, in->usecase);
2564 }
2565 if (in->enable_ns != enable &&
2566 (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2567 in->enable_ns = enable;
2568 if (!in->standby)
2569 select_devices(in->dev, in->usecase);
2570 }
2571 pthread_mutex_unlock(&in->dev->lock);
2572 pthread_mutex_unlock(&in->lock);
2573
2574 return 0;
2575}
2576
2577static int in_add_audio_effect(const struct audio_stream *stream,
2578 effect_handle_t effect)
2579{
2580 ALOGV("%s: effect %p", __func__, effect);
2581 return add_remove_audio_effect(stream, effect, true);
2582}
2583
2584static int in_remove_audio_effect(const struct audio_stream *stream,
2585 effect_handle_t effect)
2586{
2587 ALOGV("%s: effect %p", __func__, effect);
2588 return add_remove_audio_effect(stream, effect, false);
2589}
2590
2591static int adev_open_output_stream(struct audio_hw_device *dev,
2592 audio_io_handle_t handle,
2593 audio_devices_t devices,
2594 audio_output_flags_t flags,
2595 struct audio_config *config,
2596 struct audio_stream_out **stream_out,
2597 const char *address __unused)
2598{
2599 struct audio_device *adev = (struct audio_device *)dev;
2600 struct stream_out *out;
2601 int i, ret = 0;
2602 audio_format_t format;
2603
2604 *stream_out = NULL;
2605
2606 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
2607 (SND_CARD_STATE_OFFLINE == get_snd_card_state(adev))) {
2608 ALOGE(" sound card is not active rejecting compress output open request");
2609 return -EINVAL;
2610 }
2611
2612 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
2613
2614 ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)\
2615 stream_handle(%p)",__func__, config->sample_rate, config->channel_mask,
2616 devices, flags, &out->stream);
2617
2618
2619 if (!out) {
2620 return -ENOMEM;
2621 }
2622
2623 if (devices == AUDIO_DEVICE_NONE)
2624 devices = AUDIO_DEVICE_OUT_SPEAKER;
2625
2626 out->flags = flags;
2627 out->devices = devices;
2628 out->dev = adev;
2629 format = out->format = config->format;
2630 out->sample_rate = config->sample_rate;
2631 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2632 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
2633 out->handle = handle;
2634 out->bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
2635
2636 /* Init use case and pcm_config */
2637 if ((out->flags == AUDIO_OUTPUT_FLAG_DIRECT) &&
2638 (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL ||
2639 out->devices & AUDIO_DEVICE_OUT_PROXY)) {
2640
2641 pthread_mutex_lock(&adev->lock);
2642 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
2643 ret = read_hdmi_channel_masks(out);
2644
2645 if (out->devices & AUDIO_DEVICE_OUT_PROXY)
2646 ret = audio_extn_read_afe_proxy_channel_masks(out);
2647 pthread_mutex_unlock(&adev->lock);
2648 if (ret != 0)
2649 goto error_open;
2650
2651 if (config->sample_rate == 0)
2652 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
2653 if (config->channel_mask == 0)
2654 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
2655
2656 out->channel_mask = config->channel_mask;
2657 out->sample_rate = config->sample_rate;
2658 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
2659 out->config = pcm_config_hdmi_multi;
2660 out->config.rate = config->sample_rate;
2661 out->config.channels = audio_channel_count_from_out_mask(out->channel_mask);
2662 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
2663 } else if ((out->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
2664 (out->flags == (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_VOIP_RX)) &&
2665 (voice_extn_compress_voip_is_config_supported(config))) {
2666 ret = voice_extn_compress_voip_open_output_stream(out);
2667 if (ret != 0) {
2668 ALOGE("%s: Compress voip output cannot be opened, error:%d",
2669 __func__, ret);
2670 goto error_open;
2671 }
2672 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2673 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
2674 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
2675 ALOGE("%s: Unsupported Offload information", __func__);
2676 ret = -EINVAL;
2677 goto error_open;
2678 }
2679 if (!is_supported_format(config->offload_info.format) &&
2680 !audio_extn_is_dolby_format(config->offload_info.format)) {
2681 ALOGE("%s: Unsupported audio format", __func__);
2682 ret = -EINVAL;
2683 goto error_open;
2684 }
2685
2686 out->compr_config.codec = (struct snd_codec *)
2687 calloc(1, sizeof(struct snd_codec));
2688
2689 if (!out->compr_config.codec) {
2690 ret = -ENOMEM;
2691 goto error_open;
2692 }
2693
2694 out->usecase = get_offload_usecase(adev);
2695 if (config->offload_info.channel_mask)
2696 out->channel_mask = config->offload_info.channel_mask;
2697 else if (config->channel_mask) {
2698 out->channel_mask = config->channel_mask;
2699 config->offload_info.channel_mask = config->channel_mask;
2700 }
2701 format = out->format = config->offload_info.format;
2702 out->sample_rate = config->offload_info.sample_rate;
2703
2704 out->stream.set_callback = out_set_callback;
2705 out->stream.pause = out_pause;
2706 out->stream.resume = out_resume;
2707 out->stream.drain = out_drain;
2708 out->stream.flush = out_flush;
2709 out->bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
2710
2711 if (audio_extn_is_dolby_format(config->offload_info.format))
2712 out->compr_config.codec->id =
2713 audio_extn_dolby_get_snd_codec_id(adev, out,
2714 config->offload_info.format);
2715 else
2716 out->compr_config.codec->id =
2717 get_snd_codec_id(config->offload_info.format);
2718 if (audio_is_offload_pcm(config->offload_info.format)) {
2719 out->compr_config.fragment_size =
2720 platform_get_pcm_offload_buffer_size(&config->offload_info);
2721 } else {
2722 out->compr_config.fragment_size =
2723 platform_get_compress_offload_buffer_size(&config->offload_info);
2724 }
2725 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
2726 out->compr_config.codec->sample_rate =
2727 compress_get_alsa_rate(config->offload_info.sample_rate);
2728 out->compr_config.codec->bit_rate =
2729 config->offload_info.bit_rate;
2730 out->compr_config.codec->ch_in =
2731 audio_channel_count_from_out_mask(config->channel_mask);
2732 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
2733 out->bit_width = PCM_OUTPUT_BIT_WIDTH;
2734
2735 if (config->offload_info.format == AUDIO_FORMAT_AAC)
2736 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_RAW;
2737 if (config->offload_info.format == AUDIO_FORMAT_PCM_16_BIT_OFFLOAD)
2738 out->compr_config.codec->format = SNDRV_PCM_FORMAT_S16_LE;
2739 if(config->offload_info.format == AUDIO_FORMAT_PCM_24_BIT_OFFLOAD)
2740 out->compr_config.codec->format = SNDRV_PCM_FORMAT_S24_LE;
2741
2742 if (out->bit_width == 24) {
2743 out->compr_config.codec->format = SNDRV_PCM_FORMAT_S24_LE;
2744 }
2745
2746 if (config->offload_info.format == AUDIO_FORMAT_FLAC)
2747 out->compr_config.codec->options.flac_dec.sample_size = PCM_OUTPUT_BIT_WIDTH;
2748
2749 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
2750 out->non_blocking = 1;
2751
2752 out->send_new_metadata = 1;
2753 out->offload_state = OFFLOAD_STATE_IDLE;
2754 out->playback_started = 0;
2755
2756 create_offload_callback_thread(out);
2757 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
2758 __func__, config->offload_info.version,
2759 config->offload_info.bit_rate);
2760 //Decide if we need to use gapless mode by default
2761 check_and_set_gapless_mode(adev);
2762
2763 } else if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
2764 ret = voice_check_and_set_incall_music_usecase(adev, out);
2765 if (ret != 0) {
2766 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
2767 __func__, ret);
2768 goto error_open;
2769 }
2770 } else if (out->devices == AUDIO_DEVICE_OUT_TELEPHONY_TX) {
2771 if (config->sample_rate == 0)
2772 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
2773 if (config->sample_rate != 48000 && config->sample_rate != 16000 &&
2774 config->sample_rate != 8000) {
2775 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
2776 ret = -EINVAL;
2777 goto error_open;
2778 }
2779 out->sample_rate = config->sample_rate;
2780 out->config.rate = config->sample_rate;
2781 if (config->format == AUDIO_FORMAT_DEFAULT)
2782 config->format = AUDIO_FORMAT_PCM_16_BIT;
2783 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
2784 config->format = AUDIO_FORMAT_PCM_16_BIT;
2785 ret = -EINVAL;
2786 goto error_open;
2787 }
2788 out->format = config->format;
2789 out->usecase = USECASE_AUDIO_PLAYBACK_AFE_PROXY;
2790 out->config = pcm_config_afe_proxy_playback;
2791 adev->voice_tx_output = out;
2792 } else if (out->flags & AUDIO_OUTPUT_FLAG_FAST) {
2793 format = AUDIO_FORMAT_PCM_16_BIT;
2794 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
2795 out->config = pcm_config_low_latency;
2796 out->sample_rate = out->config.rate;
2797 } else {
2798 /* primary path is the default path selected if no other outputs are available/suitable */
2799 format = AUDIO_FORMAT_PCM_16_BIT;
2800 out->usecase = USECASE_AUDIO_PLAYBACK_PRIMARY;
2801 out->config = pcm_config_deep_buffer;
2802 out->sample_rate = out->config.rate;
2803 }
2804
2805 ALOGV("%s devices %d,flags %x, format %x, out->sample_rate %d, out->bit_width %d",
2806 __func__, devices, flags, format, out->sample_rate, out->bit_width);
2807 audio_extn_utils_update_stream_app_type_cfg(adev->platform,
2808 &adev->streams_output_cfg_list,
2809 devices, flags, format, out->sample_rate,
2810 out->bit_width, &out->app_type_cfg);
2811 if ((out->usecase == USECASE_AUDIO_PLAYBACK_PRIMARY) ||
2812 (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
2813 /* Ensure the default output is not selected twice */
2814 if(adev->primary_output == NULL)
2815 adev->primary_output = out;
2816 else {
2817 ALOGE("%s: Primary output is already opened", __func__);
2818 ret = -EEXIST;
2819 goto error_open;
2820 }
2821 }
2822
2823 /* Check if this usecase is already existing */
2824 pthread_mutex_lock(&adev->lock);
2825 if ((get_usecase_from_list(adev, out->usecase) != NULL) &&
2826 (out->usecase != USECASE_COMPRESS_VOIP_CALL)) {
2827 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
2828 pthread_mutex_unlock(&adev->lock);
2829 ret = -EEXIST;
2830 goto error_open;
2831 }
2832 pthread_mutex_unlock(&adev->lock);
2833
2834 out->stream.common.get_sample_rate = out_get_sample_rate;
2835 out->stream.common.set_sample_rate = out_set_sample_rate;
2836 out->stream.common.get_buffer_size = out_get_buffer_size;
2837 out->stream.common.get_channels = out_get_channels;
2838 out->stream.common.get_format = out_get_format;
2839 out->stream.common.set_format = out_set_format;
2840 out->stream.common.standby = out_standby;
2841 out->stream.common.dump = out_dump;
2842 out->stream.common.set_parameters = out_set_parameters;
2843 out->stream.common.get_parameters = out_get_parameters;
2844 out->stream.common.add_audio_effect = out_add_audio_effect;
2845 out->stream.common.remove_audio_effect = out_remove_audio_effect;
2846 out->stream.get_latency = out_get_latency;
2847 out->stream.set_volume = out_set_volume;
2848 out->stream.write = out_write;
2849 out->stream.get_render_position = out_get_render_position;
2850 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
2851 out->stream.get_presentation_position = out_get_presentation_position;
2852
2853 out->standby = 1;
2854 /* out->muted = false; by calloc() */
2855 /* out->written = 0; by calloc() */
2856
2857 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
2858 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
2859
2860 config->format = out->stream.common.get_format(&out->stream.common);
2861 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
2862 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
2863
2864 *stream_out = &out->stream;
2865 ALOGD("%s: Stream (%p) picks up usecase (%s)", __func__, &out->stream,
2866 use_case_table[out->usecase]);
2867 ALOGV("%s: exit", __func__);
2868 return 0;
2869
2870error_open:
2871 free(out);
2872 *stream_out = NULL;
2873 ALOGD("%s: exit: ret %d", __func__, ret);
2874 return ret;
2875}
2876
2877static void adev_close_output_stream(struct audio_hw_device *dev __unused,
2878 struct audio_stream_out *stream)
2879{
2880 struct stream_out *out = (struct stream_out *)stream;
2881 struct audio_device *adev = out->dev;
2882 int ret = 0;
2883
2884 ALOGD("%s: enter:stream_handle(%p)",__func__, out);
2885
2886 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
2887 pthread_mutex_lock(&adev->lock);
2888 ret = voice_extn_compress_voip_close_output_stream(&stream->common);
2889 pthread_mutex_unlock(&adev->lock);
2890 if(ret != 0)
2891 ALOGE("%s: Compress voip output cannot be closed, error:%d",
2892 __func__, ret);
2893 } else
2894 out_standby(&stream->common);
2895
2896 if (is_offload_usecase(out->usecase)) {
2897 destroy_offload_callback_thread(out);
2898 free_offload_usecase(adev, out->usecase);
2899 if (out->compr_config.codec != NULL)
2900 free(out->compr_config.codec);
2901 }
2902
2903 if (adev->voice_tx_output == out)
2904 adev->voice_tx_output = NULL;
2905
2906 pthread_cond_destroy(&out->cond);
2907 pthread_mutex_destroy(&out->lock);
2908 free(stream);
2909 ALOGV("%s: exit", __func__);
2910}
2911
2912static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
2913{
2914 struct audio_device *adev = (struct audio_device *)dev;
2915 struct str_parms *parms;
2916 char *str;
2917 char value[32];
2918 int val;
2919 int ret;
2920 int status = 0;
2921
2922 ALOGD("%s: enter: %s", __func__, kvpairs);
2923 parms = str_parms_create_str(kvpairs);
2924
2925 if (!parms)
2926 goto error;
2927 ret = str_parms_get_str(parms, "SND_CARD_STATUS", value, sizeof(value));
2928 if (ret >= 0) {
2929 char *snd_card_status = value+2;
2930 if (strstr(snd_card_status, "OFFLINE")) {
2931 struct listnode *node;
2932 struct audio_usecase *usecase;
2933
2934 ALOGD("Received sound card OFFLINE status");
2935 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
2936
2937 pthread_mutex_lock(&adev->lock);
2938 //close compress session on OFFLINE status
2939 usecase = get_usecase_from_list(adev,USECASE_AUDIO_PLAYBACK_OFFLOAD);
2940 if (usecase && usecase->stream.out) {
2941 ALOGD(" %s closing compress session on OFFLINE state", __func__);
2942
2943 struct stream_out *out = usecase->stream.out;
2944
2945 pthread_mutex_unlock(&adev->lock);
2946 out_standby(&out->stream.common);
2947 } else
2948 pthread_mutex_unlock(&adev->lock);
2949 } else if (strstr(snd_card_status, "ONLINE")) {
2950 ALOGD("Received sound card ONLINE status");
2951 set_snd_card_state(adev,SND_CARD_STATE_ONLINE);
2952 if (!platform_is_acdb_initialized(adev->platform)) {
2953 ret = platform_acdb_init(adev->platform);
2954 if(ret)
2955 ALOGE("acdb initialization is failed");
2956
2957 }
2958 }
2959 }
2960
2961 pthread_mutex_lock(&adev->lock);
2962 status = voice_set_parameters(adev, parms);
2963 if (status != 0)
2964 goto done;
2965
2966 status = platform_set_parameters(adev->platform, parms);
2967 if (status != 0)
2968 goto done;
2969
2970 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
2971 if (ret >= 0) {
2972 /* When set to false, HAL should disable EC and NS */
2973 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2974 adev->bluetooth_nrec = true;
2975 else
2976 adev->bluetooth_nrec = false;
2977 }
2978
2979 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
2980 if (ret >= 0) {
2981 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2982 adev->screen_off = false;
2983 else
2984 adev->screen_off = true;
2985 }
2986
2987 ret = str_parms_get_int(parms, "rotation", &val);
2988 if (ret >= 0) {
2989 bool reverse_speakers = false;
2990 switch(val) {
2991 // FIXME: note that the code below assumes that the speakers are in the correct placement
2992 // relative to the user when the device is rotated 90deg from its default rotation. This
2993 // assumption is device-specific, not platform-specific like this code.
2994 case 270:
2995 reverse_speakers = true;
2996 break;
2997 case 0:
2998 case 90:
2999 case 180:
3000 break;
3001 default:
3002 ALOGE("%s: unexpected rotation of %d", __func__, val);
3003 status = -EINVAL;
3004 }
3005 if (status == 0) {
3006 if (adev->speaker_lr_swap != reverse_speakers) {
3007 adev->speaker_lr_swap = reverse_speakers;
3008 // only update the selected device if there is active pcm playback
3009 struct audio_usecase *usecase;
3010 struct listnode *node;
3011 list_for_each(node, &adev->usecase_list) {
3012 usecase = node_to_item(node, struct audio_usecase, list);
3013 if (usecase->type == PCM_PLAYBACK) {
3014 select_devices(adev, usecase->id);
3015 break;
3016 }
3017 }
3018 }
3019 }
3020 }
3021
3022 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_SCO_WB, value, sizeof(value));
3023 if (ret >= 0) {
3024 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
3025 adev->bt_wb_speech_enabled = true;
3026 else
3027 adev->bt_wb_speech_enabled = false;
3028 }
3029
3030 audio_extn_set_parameters(adev, parms);
3031
3032done:
3033 str_parms_destroy(parms);
3034 pthread_mutex_unlock(&adev->lock);
3035error:
3036 ALOGV("%s: exit with code(%d)", __func__, status);
3037 return status;
3038}
3039
3040static char* adev_get_parameters(const struct audio_hw_device *dev,
3041 const char *keys)
3042{
3043 struct audio_device *adev = (struct audio_device *)dev;
3044 struct str_parms *reply = str_parms_create();
3045 struct str_parms *query = str_parms_create_str(keys);
3046 char *str;
3047 char value[256] = {0};
3048 int ret = 0;
3049
3050 if (!query || !reply) {
3051 ALOGE("adev_get_parameters: failed to create query or reply");
3052 return NULL;
3053 }
3054
3055 ret = str_parms_get_str(query, "SND_CARD_STATUS", value,
3056 sizeof(value));
3057 if (ret >=0) {
3058 int val = 1;
3059 pthread_mutex_lock(&adev->snd_card_status.lock);
3060 if (SND_CARD_STATE_OFFLINE == adev->snd_card_status.state)
3061 val = 0;
3062 pthread_mutex_unlock(&adev->snd_card_status.lock);
3063 str_parms_add_int(reply, "SND_CARD_STATUS", val);
3064 goto exit;
3065 }
3066
3067 pthread_mutex_lock(&adev->lock);
3068 audio_extn_get_parameters(adev, query, reply);
3069 voice_get_parameters(adev, query, reply);
3070 platform_get_parameters(adev->platform, query, reply);
3071 pthread_mutex_unlock(&adev->lock);
3072
3073exit:
3074 str = str_parms_to_str(reply);
3075 str_parms_destroy(query);
3076 str_parms_destroy(reply);
3077
3078 ALOGV("%s: exit: returns - %s", __func__, str);
3079 return str;
3080}
3081
3082static int adev_init_check(const struct audio_hw_device *dev __unused)
3083{
3084 return 0;
3085}
3086
3087static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
3088{
3089 int ret;
3090 struct audio_device *adev = (struct audio_device *)dev;
3091 pthread_mutex_lock(&adev->lock);
3092 /* cache volume */
3093 ret = voice_set_volume(adev, volume);
3094 pthread_mutex_unlock(&adev->lock);
3095 return ret;
3096}
3097
3098static int adev_set_master_volume(struct audio_hw_device *dev __unused,
3099 float volume __unused)
3100{
3101 return -ENOSYS;
3102}
3103
3104static int adev_get_master_volume(struct audio_hw_device *dev __unused,
3105 float *volume __unused)
3106{
3107 return -ENOSYS;
3108}
3109
3110static int adev_set_master_mute(struct audio_hw_device *dev __unused,
3111 bool muted __unused)
3112{
3113 return -ENOSYS;
3114}
3115
3116static int adev_get_master_mute(struct audio_hw_device *dev __unused,
3117 bool *muted __unused)
3118{
3119 return -ENOSYS;
3120}
3121
3122static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
3123{
3124 struct audio_device *adev = (struct audio_device *)dev;
3125
3126 pthread_mutex_lock(&adev->lock);
3127 if (adev->mode != mode) {
3128 ALOGD("%s: mode %d\n", __func__, mode);
3129 adev->mode = mode;
3130 if ((mode == AUDIO_MODE_NORMAL || mode == AUDIO_MODE_IN_COMMUNICATION) &&
3131 voice_is_in_call(adev)) {
3132 voice_stop_call(adev);
3133 adev->current_call_output = NULL;
3134 }
3135 }
3136 pthread_mutex_unlock(&adev->lock);
3137 return 0;
3138}
3139
3140static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
3141{
3142 int ret;
3143
3144 pthread_mutex_lock(&adev->lock);
3145 ALOGD("%s state %d\n", __func__, state);
3146 ret = voice_set_mic_mute((struct audio_device *)dev, state);
3147 pthread_mutex_unlock(&adev->lock);
3148
3149 return ret;
3150}
3151
3152static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
3153{
3154 *state = voice_get_mic_mute((struct audio_device *)dev);
3155 return 0;
3156}
3157
3158static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev __unused,
3159 const struct audio_config *config)
3160{
3161 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
3162
3163 return get_input_buffer_size(config->sample_rate, config->format, channel_count,
3164 false /* is_low_latency: since we don't know, be conservative */);
3165}
3166
3167static int adev_open_input_stream(struct audio_hw_device *dev,
3168 audio_io_handle_t handle __unused,
3169 audio_devices_t devices,
3170 struct audio_config *config,
3171 struct audio_stream_in **stream_in,
3172 audio_input_flags_t flags __unused,
3173 const char *address __unused,
3174 audio_source_t source __unused)
3175{
3176 struct audio_device *adev = (struct audio_device *)dev;
3177 struct stream_in *in;
3178 int ret = 0, buffer_size, frame_size;
3179 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
3180 bool is_low_latency = false;
3181
3182 *stream_in = NULL;
3183 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
3184 return -EINVAL;
3185
3186 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
3187
3188 if (!in) {
3189 ALOGE("failed to allocate input stream");
3190 return -ENOMEM;
3191 }
3192
3193 ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x)\
3194 stream_handle(%p) io_handle(%d)",__func__, config->sample_rate, config->channel_mask,
3195 devices, &in->stream, handle);
3196
3197 pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL);
3198
3199 in->stream.common.get_sample_rate = in_get_sample_rate;
3200 in->stream.common.set_sample_rate = in_set_sample_rate;
3201 in->stream.common.get_buffer_size = in_get_buffer_size;
3202 in->stream.common.get_channels = in_get_channels;
3203 in->stream.common.get_format = in_get_format;
3204 in->stream.common.set_format = in_set_format;
3205 in->stream.common.standby = in_standby;
3206 in->stream.common.dump = in_dump;
3207 in->stream.common.set_parameters = in_set_parameters;
3208 in->stream.common.get_parameters = in_get_parameters;
3209 in->stream.common.add_audio_effect = in_add_audio_effect;
3210 in->stream.common.remove_audio_effect = in_remove_audio_effect;
3211 in->stream.set_gain = in_set_gain;
3212 in->stream.read = in_read;
3213 in->stream.get_input_frames_lost = in_get_input_frames_lost;
3214
3215 in->device = devices;
3216 in->source = AUDIO_SOURCE_DEFAULT;
3217 in->dev = adev;
3218 in->standby = 1;
3219 in->channel_mask = config->channel_mask;
3220 in->capture_handle = handle;
3221
3222 /* Update config params with the requested sample rate and channels */
3223 in->usecase = USECASE_AUDIO_RECORD;
3224 if (config->sample_rate == LOW_LATENCY_CAPTURE_SAMPLE_RATE &&
3225 (flags & AUDIO_INPUT_FLAG_FAST) != 0) {
3226 is_low_latency = true;
3227#if LOW_LATENCY_CAPTURE_USE_CASE
3228 in->usecase = USECASE_AUDIO_RECORD_LOW_LATENCY;
3229#endif
3230 }
3231 in->config = pcm_config_audio_capture;
3232 in->config.rate = config->sample_rate;
3233 in->format = config->format;
3234
3235 if (in->device == AUDIO_DEVICE_IN_TELEPHONY_RX) {
3236 if (config->sample_rate == 0)
3237 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
3238 if (config->sample_rate != 48000 && config->sample_rate != 16000 &&
3239 config->sample_rate != 8000) {
3240 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
3241 ret = -EINVAL;
3242 goto err_open;
3243 }
3244 if (config->format == AUDIO_FORMAT_DEFAULT)
3245 config->format = AUDIO_FORMAT_PCM_16_BIT;
3246 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
3247 config->format = AUDIO_FORMAT_PCM_16_BIT;
3248 ret = -EINVAL;
3249 goto err_open;
3250 }
3251
3252 in->usecase = USECASE_AUDIO_RECORD_AFE_PROXY;
3253 in->config = pcm_config_afe_proxy_record;
3254 in->config.channels = channel_count;
3255 in->config.rate = config->sample_rate;
3256 } else if (channel_count == 6) {
3257 if(audio_extn_ssr_get_enabled()) {
3258 if(audio_extn_ssr_init(in)) {
3259 ALOGE("%s: audio_extn_ssr_init failed", __func__);
3260 ret = -EINVAL;
3261 goto err_open;
3262 }
3263 } else {
3264 ALOGW("%s: surround sound recording is not supported", __func__);
3265 }
3266 } else if (audio_extn_compr_cap_enabled() &&
3267 audio_extn_compr_cap_format_supported(config->format) &&
3268 (in->dev->mode != AUDIO_MODE_IN_COMMUNICATION)) {
3269 audio_extn_compr_cap_init(in);
3270 } else {
3271 in->config.channels = channel_count;
3272 frame_size = audio_stream_in_frame_size(&in->stream);
3273 buffer_size = get_input_buffer_size(config->sample_rate,
3274 config->format,
3275 channel_count,
3276 is_low_latency);
3277 in->config.period_size = buffer_size / frame_size;
3278 }
3279
3280 /* This stream could be for sound trigger lab,
3281 get sound trigger pcm if present */
3282 audio_extn_sound_trigger_check_and_get_session(in);
3283 audio_extn_perf_lock_init();
3284
3285 *stream_in = &in->stream;
3286 ALOGV("%s: exit", __func__);
3287 return ret;
3288
3289err_open:
3290 free(in);
3291 *stream_in = NULL;
3292 return ret;
3293}
3294
3295static void adev_close_input_stream(struct audio_hw_device *dev,
3296 struct audio_stream_in *stream)
3297{
3298 int ret;
3299 struct stream_in *in = (struct stream_in *)stream;
3300 struct audio_device *adev = (struct audio_device *)dev;
3301
3302 ALOGD("%s: enter:stream_handle(%p)",__func__, in);
3303
3304 /* Disable echo reference while closing input stream */
3305 platform_set_echo_reference(adev->platform, false);
3306
3307 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
3308 pthread_mutex_lock(&adev->lock);
3309 ret = voice_extn_compress_voip_close_input_stream(&stream->common);
3310 pthread_mutex_unlock(&adev->lock);
3311 if (ret != 0)
3312 ALOGE("%s: Compress voip input cannot be closed, error:%d",
3313 __func__, ret);
3314 } else
3315 in_standby(&stream->common);
3316
3317 if (audio_extn_ssr_get_enabled() &&
3318 (audio_channel_count_from_in_mask(in->channel_mask) == 6)) {
3319 audio_extn_ssr_deinit();
3320 }
3321
3322 if(audio_extn_compr_cap_enabled() &&
3323 audio_extn_compr_cap_format_supported(in->config.format))
3324 audio_extn_compr_cap_deinit();
3325
3326 free(stream);
3327 return;
3328}
3329
3330static int adev_dump(const audio_hw_device_t *device __unused,
3331 int fd __unused)
3332{
3333 return 0;
3334}
3335
3336static int adev_close(hw_device_t *device)
3337{
3338 struct audio_device *adev = (struct audio_device *)device;
3339
3340 if (!adev)
3341 return 0;
3342
3343 pthread_mutex_lock(&adev_init_lock);
3344
3345 if ((--audio_device_ref_count) == 0) {
3346 audio_extn_sound_trigger_deinit(adev);
3347 audio_extn_listen_deinit(adev);
3348 audio_extn_utils_release_streams_output_cfg_list(&adev->streams_output_cfg_list);
3349 audio_route_free(adev->audio_route);
3350 free(adev->snd_dev_ref_cnt);
3351 platform_deinit(adev->platform);
3352 free(device);
3353 adev = NULL;
3354 }
3355 pthread_mutex_unlock(&adev_init_lock);
3356 return 0;
3357}
3358
3359/* This returns 1 if the input parameter looks at all plausible as a low latency period size,
3360 * or 0 otherwise. A return value of 1 doesn't mean the value is guaranteed to work,
3361 * just that it _might_ work.
3362 */
3363static int period_size_is_plausible_for_low_latency(int period_size)
3364{
3365 switch (period_size) {
3366 case 160:
3367 case 240:
3368 case 320:
3369 case 480:
3370 return 1;
3371 default:
3372 return 0;
3373 }
3374}
3375
3376static int adev_open(const hw_module_t *module, const char *name,
3377 hw_device_t **device)
3378{
3379 int i, ret;
3380
3381 ALOGD("%s: enter", __func__);
3382 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
3383
3384 pthread_mutex_lock(&adev_init_lock);
3385 if (audio_device_ref_count != 0){
3386 *device = &adev->device.common;
3387 audio_device_ref_count++;
3388 ALOGD("%s: returning existing instance of adev", __func__);
3389 ALOGD("%s: exit", __func__);
3390 pthread_mutex_unlock(&adev_init_lock);
3391 return 0;
3392 }
3393
3394 adev = calloc(1, sizeof(struct audio_device));
3395
3396 if (!adev) {
3397 pthread_mutex_unlock(&adev_init_lock);
3398 return -ENOMEM;
3399 }
3400
3401 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
3402
3403 adev->device.common.tag = HARDWARE_DEVICE_TAG;
3404 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
3405 adev->device.common.module = (struct hw_module_t *)module;
3406 adev->device.common.close = adev_close;
3407
3408 adev->device.init_check = adev_init_check;
3409 adev->device.set_voice_volume = adev_set_voice_volume;
3410 adev->device.set_master_volume = adev_set_master_volume;
3411 adev->device.get_master_volume = adev_get_master_volume;
3412 adev->device.set_master_mute = adev_set_master_mute;
3413 adev->device.get_master_mute = adev_get_master_mute;
3414 adev->device.set_mode = adev_set_mode;
3415 adev->device.set_mic_mute = adev_set_mic_mute;
3416 adev->device.get_mic_mute = adev_get_mic_mute;
3417 adev->device.set_parameters = adev_set_parameters;
3418 adev->device.get_parameters = adev_get_parameters;
3419 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
3420 adev->device.open_output_stream = adev_open_output_stream;
3421 adev->device.close_output_stream = adev_close_output_stream;
3422 adev->device.open_input_stream = adev_open_input_stream;
3423 adev->device.close_input_stream = adev_close_input_stream;
3424 adev->device.dump = adev_dump;
3425
3426 /* Set the default route before the PCM stream is opened */
3427 adev->mode = AUDIO_MODE_NORMAL;
3428 adev->active_input = NULL;
3429 adev->primary_output = NULL;
3430 adev->out_device = AUDIO_DEVICE_NONE;
3431 adev->bluetooth_nrec = true;
3432 adev->acdb_settings = TTY_MODE_OFF;
3433 /* adev->cur_hdmi_channels = 0; by calloc() */
3434 adev->cur_codec_backend_samplerate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
3435 adev->cur_codec_backend_bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
3436 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
3437 voice_init(adev);
3438 list_init(&adev->usecase_list);
3439 adev->cur_wfd_channels = 2;
3440 adev->offload_usecases_state = 0;
3441
3442 pthread_mutex_init(&adev->snd_card_status.lock, (const pthread_mutexattr_t *) NULL);
3443 adev->snd_card_status.state = SND_CARD_STATE_OFFLINE;
3444 /* Loads platform specific libraries dynamically */
3445 adev->platform = platform_init(adev);
3446 if (!adev->platform) {
3447 free(adev->snd_dev_ref_cnt);
3448 free(adev);
3449 ALOGE("%s: Failed to init platform data, aborting.", __func__);
3450 *device = NULL;
3451 pthread_mutex_unlock(&adev_init_lock);
3452 return -EINVAL;
3453 }
3454
3455 adev->snd_card_status.state = SND_CARD_STATE_ONLINE;
3456
3457 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
3458 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
3459 if (adev->visualizer_lib == NULL) {
3460 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
3461 } else {
3462 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
3463 adev->visualizer_start_output =
3464 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
3465 "visualizer_hal_start_output");
3466 adev->visualizer_stop_output =
3467 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
3468 "visualizer_hal_stop_output");
3469 }
3470 }
3471 audio_extn_listen_init(adev, adev->snd_card);
3472 audio_extn_sound_trigger_init(adev);
3473
3474 if (access(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, R_OK) == 0) {
3475 adev->offload_effects_lib = dlopen(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, RTLD_NOW);
3476 if (adev->offload_effects_lib == NULL) {
3477 ALOGE("%s: DLOPEN failed for %s", __func__,
3478 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
3479 } else {
3480 ALOGV("%s: DLOPEN successful for %s", __func__,
3481 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
3482 adev->offload_effects_start_output =
3483 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
3484 "offload_effects_bundle_hal_start_output");
3485 adev->offload_effects_stop_output =
3486 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
3487 "offload_effects_bundle_hal_stop_output");
3488 }
3489 }
3490
3491 adev->bt_wb_speech_enabled = false;
3492
3493 audio_extn_ds2_enable(adev);
3494 *device = &adev->device.common;
3495
3496 audio_extn_utils_update_streams_output_cfg_list(adev->platform, adev->mixer,
3497 &adev->streams_output_cfg_list);
3498
3499 audio_device_ref_count++;
3500
3501 char value[PROPERTY_VALUE_MAX];
3502 int trial;
3503 if (property_get("audio_hal.period_size", value, NULL) > 0) {
3504 trial = atoi(value);
3505 if (period_size_is_plausible_for_low_latency(trial)) {
3506 pcm_config_low_latency.period_size = trial;
3507 pcm_config_low_latency.start_threshold = trial / 4;
3508 pcm_config_low_latency.avail_min = trial / 4;
3509 configured_low_latency_capture_period_size = trial;
3510 }
3511 }
3512 if (property_get("audio_hal.in_period_size", value, NULL) > 0) {
3513 trial = atoi(value);
3514 if (period_size_is_plausible_for_low_latency(trial)) {
3515 configured_low_latency_capture_period_size = trial;
3516 }
3517 }
3518
3519 pthread_mutex_unlock(&adev_init_lock);
3520
3521 ALOGV("%s: exit", __func__);
3522 return 0;
3523}
3524
3525static struct hw_module_methods_t hal_module_methods = {
3526 .open = adev_open,
3527};
3528
3529struct audio_module HAL_MODULE_INFO_SYM = {
3530 .common = {
3531 .tag = HARDWARE_MODULE_TAG,
3532 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
3533 .hal_api_version = HARDWARE_HAL_API_VERSION,
3534 .id = AUDIO_HARDWARE_MODULE_ID,
3535 .name = "QCOM Audio HAL",
3536 .author = "The Linux Foundation",
3537 .methods = &hal_module_methods,
3538 },
3539};