blob: 607519bbdba073c93216369322d3f240ec2d74ab [file] [log] [blame]
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001/*
Ramu Gottipatifa5be522021-12-28 19:18:21 +05302 * Copyright (c) 2013-2022, The Linux Foundation. All rights reserved.
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07003 * Not a Contribution.
4 *
Shiv Maliyappanahalli8911f282014-01-10 15:56:19 -08005 * Copyright (C) 2013 The Android Open Source Project
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006 *
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.
Jitendra Naruka1b6513f2014-11-22 19:34:13 -080018 *
19 * This file was modified by DTS, Inc. The portions of the
20 * code modified by DTS, Inc are copyrighted and
21 * licensed separately, as follows:
22 *
23 * (C) 2014 DTS, Inc.
24 *
25 * Licensed under the Apache License, Version 2.0 (the "License");
26 * you may not use this file except in compliance with the License.
27 * You may obtain a copy of the License at
28 *
29 * http://www.apache.org/licenses/LICENSE-2.0
30 *
31 * Unless required by applicable law or agreed to in writing, software
32 * distributed under the License is distributed on an "AS IS" BASIS,
33 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34 * See the License for the specific language governing permissions and
35 * limitations under the License.
Sandhya Mutha Naga Venkataa597a962023-02-07 11:35:51 +053036 *
37 * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080038 */
39
40#define LOG_TAG "audio_hw_primary"
Haynes Mathew George5beddd42016-06-27 18:33:40 -070041#define ATRACE_TAG (ATRACE_TAG_AUDIO|ATRACE_TAG_HAL)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080042/*#define LOG_NDEBUG 0*/
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070043/*#define VERY_VERY_VERBOSE_LOGGING*/
44#ifdef VERY_VERY_VERBOSE_LOGGING
45#define ALOGVV ALOGV
46#else
47#define ALOGVV(a...) do { } while(0)
48#endif
George Gao3018ede2019-10-23 13:23:00 -070049#include <limits.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080050#include <errno.h>
51#include <pthread.h>
52#include <stdint.h>
53#include <sys/time.h>
54#include <stdlib.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080055#include <math.h>
Eric Laurentc4aef752013-09-12 17:45:53 -070056#include <dlfcn.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070057#include <sys/resource.h>
58#include <sys/prctl.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080059
Aalique Grahame22e49102018-12-18 14:23:57 -080060#include <log/log.h>
Haynes Mathew George5beddd42016-06-27 18:33:40 -070061#include <cutils/trace.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080062#include <cutils/str_parms.h>
63#include <cutils/properties.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070064#include <cutils/atomic.h>
65#include <cutils/sched_policy.h>
Eric Laurentb23d5282013-05-14 15:27:20 -070066#include <hardware/audio_effect.h>
Haynes Mathew George484e8d22017-07-31 18:55:17 -070067#include <hardware/audio_alsaops.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070068#include <system/thread_defs.h>
Haynes Mathew George16081042017-05-31 17:16:49 -070069#include <tinyalsa/asoundlib.h>
Andy Hunga1f48fa2019-07-01 18:14:53 -070070#include <utils/Timers.h> // systemTime
Eric Laurentb23d5282013-05-14 15:27:20 -070071#include <audio_effects/effect_aec.h>
72#include <audio_effects/effect_ns.h>
Ashish Jainf1eaa582016-05-23 20:54:24 +053073#include <audio_utils/format.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080074#include "audio_hw.h"
Wei Wangf7ca6c92017-11-21 14:51:20 -080075#include "audio_perf.h"
Eric Laurentb23d5282013-05-14 15:27:20 -070076#include "platform_api.h"
77#include <platform.h>
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -070078#include "audio_extn.h"
Narsinga Rao Chella05573b72013-11-15 15:21:40 -080079#include "voice_extn.h"
Ashish Jaind5694242017-09-05 20:09:06 +053080#include "ip_hdlr_intf.h"
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080081
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070082#include "sound/compress_params.h"
Xiaojun Sang782e5b12020-06-29 21:13:06 +080083
84#ifdef AUDIO_GKI_ENABLED
85#include "sound/audio_compressed_formats.h"
86#endif
87
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -080088#include "sound/asound.h"
ApurupaPattapu2e084df2013-12-18 15:47:59 -080089
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053090#ifdef DYNAMIC_LOG_ENABLED
91#include <log_xml_parser.h>
92#define LOG_MASK HAL_MOD_FILE_AUDIO_HW
93#include <log_utils.h>
94#endif
95
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070096#define COMPRESS_OFFLOAD_NUM_FRAGMENTS 4
Ashish Jain5106d362016-05-11 19:23:33 +053097/*DIRECT PCM has same buffer sizes as DEEP Buffer*/
98#define DIRECT_PCM_NUM_FRAGMENTS 2
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070099#define COMPRESS_PLAYBACK_VOLUME_MAX 0x2000
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700100#define VOIP_PLAYBACK_VOLUME_MAX 0x2000
Arun Mirpuri5d170872019-03-26 13:21:31 -0700101#define MMAP_PLAYBACK_VOLUME_MAX 0x2000
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +0530102#define PCM_PLAYBACK_VOLUME_MAX 0x2000
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530103#define DSD_VOLUME_MIN_DB (-110)
Aniket Kumar Lata8426d1f2019-06-10 15:25:53 -0700104#define INVALID_OUT_VOLUME -1
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700105#define AUDIO_IO_PORTS_MAX 32
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700106
Zhou Songbaddf9f2020-11-20 13:57:39 +0800107#define PLAYBACK_GAIN_MAX 1.0f
Aalique Grahame22e49102018-12-18 14:23:57 -0800108#define RECORD_GAIN_MIN 0.0f
109#define RECORD_GAIN_MAX 1.0f
110#define RECORD_VOLUME_CTL_MAX 0x2000
111
112/* treat as unsigned Q1.13 */
113#define APP_TYPE_GAIN_DEFAULT 0x2000
114
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -0700115#define PROXY_OPEN_RETRY_COUNT 100
116#define PROXY_OPEN_WAIT_TIME 20
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -0800117
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800118#define GET_USECASE_AUDIO_PLAYBACK_PRIMARY(db) \
119 (db)? USECASE_AUDIO_PLAYBACK_DEEP_BUFFER : \
120 USECASE_AUDIO_PLAYBACK_LOW_LATENCY
121#define GET_PCM_CONFIG_AUDIO_PLAYBACK_PRIMARY(db) \
122 (db)? pcm_config_deep_buffer : pcm_config_low_latency
Haynes Mathew Georgebf143712013-12-03 13:02:53 -0800123
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700124#define ULL_PERIOD_SIZE (DEFAULT_OUTPUT_SAMPLING_RATE/1000)
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700125#define DEFAULT_VOIP_BUF_DURATION_MS 20
126#define DEFAULT_VOIP_BIT_DEPTH_BYTE sizeof(int16_t)
127#define DEFAULT_VOIP_SAMP_RATE 48000
128
129#define VOIP_IO_BUF_SIZE(SR, DURATION_MS, BIT_DEPTH) (SR)/1000 * DURATION_MS * BIT_DEPTH
130
131struct pcm_config default_pcm_config_voip_copp = {
132 .channels = 1,
133 .rate = DEFAULT_VOIP_SAMP_RATE, /* changed when the stream is opened */
134 .period_size = VOIP_IO_BUF_SIZE(DEFAULT_VOIP_SAMP_RATE, DEFAULT_VOIP_BUF_DURATION_MS, DEFAULT_VOIP_BIT_DEPTH_BYTE)/2,
135 .period_count = 2,
136 .format = PCM_FORMAT_S16_LE,
kunleiz95b597a2017-10-23 17:07:33 +0800137 .avail_min = VOIP_IO_BUF_SIZE(DEFAULT_VOIP_SAMP_RATE, DEFAULT_VOIP_BUF_DURATION_MS, DEFAULT_VOIP_BIT_DEPTH_BYTE)/2,
138 .stop_threshold = INT_MAX,
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700139};
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700140
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700141#define MIN_CHANNEL_COUNT 1
142#define DEFAULT_CHANNEL_COUNT 2
143#define MAX_HIFI_CHANNEL_COUNT 8
144
Aalique Grahame22e49102018-12-18 14:23:57 -0800145#ifndef MAX_TARGET_SPECIFIC_CHANNEL_CNT
146#define MAX_CHANNEL_COUNT 1
147#else
148#define MAX_CHANNEL_COUNT atoi(XSTR(MAX_TARGET_SPECIFIC_CHANNEL_CNT))
149#define XSTR(x) STR(x)
150#define STR(x) #x
151#endif
152
Avinash Chandrad7296d42021-08-04 15:07:47 +0530153#define IS_USB_HIFI (MAX_HIFI_CHANNEL_COUNT >= MAX_CHANNEL_COUNT) ? \
154 true : false
155
Dechen Chai22768452021-07-30 09:29:16 +0530156#ifdef LINUX_ENABLED
157static inline int64_t audio_utils_ns_from_timespec(const struct timespec *ts)
158{
159 return ts->tv_sec * 1000000000LL + ts->tv_nsec;
160}
161#endif
162
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -0700163static unsigned int configured_low_latency_capture_period_size =
164 LOW_LATENCY_CAPTURE_PERIOD_SIZE;
165
Haynes Mathew George16081042017-05-31 17:16:49 -0700166#define MMAP_PERIOD_SIZE (DEFAULT_OUTPUT_SAMPLING_RATE/1000)
167#define MMAP_PERIOD_COUNT_MIN 32
168#define MMAP_PERIOD_COUNT_MAX 512
169#define MMAP_PERIOD_COUNT_DEFAULT (MMAP_PERIOD_COUNT_MAX)
170
Aalique Grahame22e49102018-12-18 14:23:57 -0800171/* This constant enables extended precision handling.
172 * TODO The flag is off until more testing is done.
173 */
174static const bool k_enable_extended_precision = false;
Arun Mirpurie008ed22019-03-21 11:21:04 -0700175extern int AUDIO_DEVICE_IN_ALL_CODEC_BACKEND;
Aalique Grahame22e49102018-12-18 14:23:57 -0800176
Eric Laurentb23d5282013-05-14 15:27:20 -0700177struct pcm_config pcm_config_deep_buffer = {
178 .channels = 2,
179 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
180 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
181 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
182 .format = PCM_FORMAT_S16_LE,
183 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
184 .stop_threshold = INT_MAX,
185 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
186};
187
188struct pcm_config pcm_config_low_latency = {
189 .channels = 2,
190 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
191 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
192 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
193 .format = PCM_FORMAT_S16_LE,
194 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
195 .stop_threshold = INT_MAX,
196 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
197};
198
Vignesh Kulothungana6927272019-02-20 15:17:07 -0800199struct pcm_config pcm_config_haptics_audio = {
200 .channels = 1,
201 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
202 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
203 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
204 .format = PCM_FORMAT_S16_LE,
205 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
206 .stop_threshold = INT_MAX,
207 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
208};
209
210struct pcm_config pcm_config_haptics = {
211 .channels = 1,
212 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
213 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
214 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
215 .format = PCM_FORMAT_S16_LE,
216 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
217 .stop_threshold = INT_MAX,
218 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
219};
220
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700221static int af_period_multiplier = 4;
222struct pcm_config pcm_config_rt = {
223 .channels = 2,
224 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
225 .period_size = ULL_PERIOD_SIZE, //1 ms
226 .period_count = 512, //=> buffer size is 512ms
227 .format = PCM_FORMAT_S16_LE,
228 .start_threshold = ULL_PERIOD_SIZE*8, //8ms
229 .stop_threshold = INT_MAX,
230 .silence_threshold = 0,
231 .silence_size = 0,
232 .avail_min = ULL_PERIOD_SIZE, //1 ms
233};
234
Eric Laurentb23d5282013-05-14 15:27:20 -0700235struct pcm_config pcm_config_hdmi_multi = {
236 .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
237 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
238 .period_size = HDMI_MULTI_PERIOD_SIZE,
239 .period_count = HDMI_MULTI_PERIOD_COUNT,
240 .format = PCM_FORMAT_S16_LE,
241 .start_threshold = 0,
242 .stop_threshold = INT_MAX,
243 .avail_min = 0,
244};
245
Haynes Mathew George16081042017-05-31 17:16:49 -0700246struct pcm_config pcm_config_mmap_playback = {
247 .channels = 2,
248 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
249 .period_size = MMAP_PERIOD_SIZE,
250 .period_count = MMAP_PERIOD_COUNT_DEFAULT,
251 .format = PCM_FORMAT_S16_LE,
252 .start_threshold = MMAP_PERIOD_SIZE*8,
253 .stop_threshold = INT32_MAX,
254 .silence_threshold = 0,
255 .silence_size = 0,
256 .avail_min = MMAP_PERIOD_SIZE, //1 ms
257};
258
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700259struct pcm_config pcm_config_hifi = {
260 .channels = DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
261 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
262 .period_size = HIFI_BUFFER_OUTPUT_PERIOD_SIZE, /* change #define */
263 .period_count = HIFI_BUFFER_OUTPUT_PERIOD_COUNT,
264 .format = PCM_FORMAT_S24_3LE,
265 .start_threshold = 0,
266 .stop_threshold = INT_MAX,
267 .avail_min = 0,
268};
269
Eric Laurentb23d5282013-05-14 15:27:20 -0700270struct pcm_config pcm_config_audio_capture = {
271 .channels = 2,
Eric Laurentb23d5282013-05-14 15:27:20 -0700272 .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
273 .format = PCM_FORMAT_S16_LE,
274};
275
Haynes Mathew George16081042017-05-31 17:16:49 -0700276struct pcm_config pcm_config_mmap_capture = {
277 .channels = 2,
278 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
279 .period_size = MMAP_PERIOD_SIZE,
280 .period_count = MMAP_PERIOD_COUNT_DEFAULT,
281 .format = PCM_FORMAT_S16_LE,
282 .start_threshold = 0,
283 .stop_threshold = INT_MAX,
284 .silence_threshold = 0,
285 .silence_size = 0,
286 .avail_min = MMAP_PERIOD_SIZE, //1 ms
287};
288
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -0700289#define AFE_PROXY_CHANNEL_COUNT 2
290#define AFE_PROXY_SAMPLING_RATE 48000
291
292#define AFE_PROXY_PLAYBACK_PERIOD_SIZE 768
293#define AFE_PROXY_PLAYBACK_PERIOD_COUNT 4
294
295struct pcm_config pcm_config_afe_proxy_playback = {
296 .channels = AFE_PROXY_CHANNEL_COUNT,
297 .rate = AFE_PROXY_SAMPLING_RATE,
298 .period_size = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
299 .period_count = AFE_PROXY_PLAYBACK_PERIOD_COUNT,
300 .format = PCM_FORMAT_S16_LE,
301 .start_threshold = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
302 .stop_threshold = INT_MAX,
303 .avail_min = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
304};
305
306#define AFE_PROXY_RECORD_PERIOD_SIZE 768
307#define AFE_PROXY_RECORD_PERIOD_COUNT 4
308
Aalique Grahame22e49102018-12-18 14:23:57 -0800309struct pcm_config pcm_config_audio_capture_rt = {
310 .channels = 2,
311 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
312 .period_size = ULL_PERIOD_SIZE,
313 .period_count = 512,
314 .format = PCM_FORMAT_S16_LE,
315 .start_threshold = 0,
316 .stop_threshold = AFE_PROXY_RECORD_PERIOD_SIZE * AFE_PROXY_RECORD_PERIOD_COUNT,
317 .silence_threshold = 0,
318 .silence_size = 0,
319 .avail_min = ULL_PERIOD_SIZE, //1 ms
320};
321
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +0530322struct pcm_config pcm_config_audio_capture_rt_48KHz = {
323 .channels = 2,
324 .rate = 48000,
325 .period_size = 48,
326 .period_count = 512,
327 .format = PCM_FORMAT_S16_LE,
328 .start_threshold = 0,
329 .stop_threshold = AFE_PROXY_RECORD_PERIOD_SIZE * AFE_PROXY_RECORD_PERIOD_COUNT,
330 .silence_threshold = 0,
331 .silence_size = 0,
332 .avail_min = 48, //1 ms
333};
334struct pcm_config pcm_config_audio_capture_rt_32KHz = {
335 .channels = 2,
336 .rate = 32000,
337 .period_size = 32,
338 .period_count = 512,
339 .format = PCM_FORMAT_S16_LE,
340 .start_threshold = 0,
341 .stop_threshold = AFE_PROXY_RECORD_PERIOD_SIZE * AFE_PROXY_RECORD_PERIOD_COUNT,
342 .silence_threshold = 0,
343 .silence_size = 0,
344 .avail_min = 32, //1 ms
345};
346struct pcm_config pcm_config_audio_capture_rt_24KHz = {
347 .channels = 2,
348 .rate = 24000,
349 .period_size = 24,
350 .period_count = 512,
351 .format = PCM_FORMAT_S16_LE,
352 .start_threshold = 0,
353 .stop_threshold = AFE_PROXY_RECORD_PERIOD_SIZE * AFE_PROXY_RECORD_PERIOD_COUNT,
354 .silence_threshold = 0,
355 .silence_size = 0,
356 .avail_min = 24, //1 ms
357};
358struct pcm_config pcm_config_audio_capture_rt_16KHz = {
359 .channels = 2,
360 .rate = 16000,
361 .period_size = 16,
362 .period_count = 512,
363 .format = PCM_FORMAT_S16_LE,
364 .start_threshold = 0,
365 .stop_threshold = AFE_PROXY_RECORD_PERIOD_SIZE * AFE_PROXY_RECORD_PERIOD_COUNT,
366 .silence_threshold = 0,
367 .silence_size = 0,
368 .avail_min = 16, //1 ms
369};
370struct pcm_config pcm_config_audio_capture_rt_8KHz = {
371 .channels = 2,
372 .rate = 8000,
373 .period_size = 8,
374 .period_count = 512,
375 .format = PCM_FORMAT_S16_LE,
376 .start_threshold = 0,
377 .stop_threshold = AFE_PROXY_RECORD_PERIOD_SIZE * AFE_PROXY_RECORD_PERIOD_COUNT,
378 .silence_threshold = 0,
379 .silence_size = 0,
380 .avail_min = 8, //1 ms
381};
382
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -0700383struct pcm_config pcm_config_afe_proxy_record = {
384 .channels = AFE_PROXY_CHANNEL_COUNT,
385 .rate = AFE_PROXY_SAMPLING_RATE,
386 .period_size = AFE_PROXY_RECORD_PERIOD_SIZE,
387 .period_count = AFE_PROXY_RECORD_PERIOD_COUNT,
388 .format = PCM_FORMAT_S16_LE,
389 .start_threshold = AFE_PROXY_RECORD_PERIOD_SIZE,
390 .stop_threshold = INT_MAX,
391 .avail_min = AFE_PROXY_RECORD_PERIOD_SIZE,
392};
393
Ashish Jainf1eaa582016-05-23 20:54:24 +0530394#define AUDIO_MAX_PCM_FORMATS 7
395
396const uint32_t format_to_bitwidth_table[AUDIO_MAX_PCM_FORMATS] = {
397 [AUDIO_FORMAT_DEFAULT] = 0,
398 [AUDIO_FORMAT_PCM_16_BIT] = sizeof(uint16_t),
399 [AUDIO_FORMAT_PCM_8_BIT] = sizeof(uint8_t),
400 [AUDIO_FORMAT_PCM_32_BIT] = sizeof(uint32_t),
401 [AUDIO_FORMAT_PCM_8_24_BIT] = sizeof(uint32_t),
402 [AUDIO_FORMAT_PCM_FLOAT] = sizeof(float),
403 [AUDIO_FORMAT_PCM_24_BIT_PACKED] = sizeof(uint8_t) * 3,
404};
405
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800406const char * const use_case_table[AUDIO_USECASE_MAX] = {
Eric Laurentb23d5282013-05-14 15:27:20 -0700407 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
408 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
Vignesh Kulothungana6927272019-02-20 15:17:07 -0800409 [USECASE_AUDIO_PLAYBACK_WITH_HAPTICS] = "audio-with-haptics-playback",
Meng Wang51d8c2a2020-04-27 15:23:21 +0800410 [USECASE_AUDIO_PLAYBACK_HAPTICS] = "haptics-playback",
Ravi Kumar Alamanda474de5a2015-06-25 20:08:01 -0700411 [USECASE_AUDIO_PLAYBACK_ULL] = "audio-ull-playback",
412 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback",
Shruthi Krishnaace10852013-10-25 14:32:12 -0700413 [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback",
vivek mehta446c3962015-09-14 10:57:35 -0700414 //Enabled for Direct_PCM
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700415 [USECASE_AUDIO_PLAYBACK_OFFLOAD2] = "compress-offload-playback2",
416 [USECASE_AUDIO_PLAYBACK_OFFLOAD3] = "compress-offload-playback3",
417 [USECASE_AUDIO_PLAYBACK_OFFLOAD4] = "compress-offload-playback4",
418 [USECASE_AUDIO_PLAYBACK_OFFLOAD5] = "compress-offload-playback5",
419 [USECASE_AUDIO_PLAYBACK_OFFLOAD6] = "compress-offload-playback6",
420 [USECASE_AUDIO_PLAYBACK_OFFLOAD7] = "compress-offload-playback7",
421 [USECASE_AUDIO_PLAYBACK_OFFLOAD8] = "compress-offload-playback8",
422 [USECASE_AUDIO_PLAYBACK_OFFLOAD9] = "compress-offload-playback9",
Haynes Mathew George16081042017-05-31 17:16:49 -0700423 [USECASE_AUDIO_PLAYBACK_FM] = "play-fm",
424 [USECASE_AUDIO_PLAYBACK_MMAP] = "mmap-playback",
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700425 [USECASE_AUDIO_PLAYBACK_HIFI] = "hifi-playback",
Aalique Grahame22e49102018-12-18 14:23:57 -0800426 [USECASE_AUDIO_PLAYBACK_TTS] = "audio-tts-playback",
vivek mehta0ea887a2015-08-26 14:01:20 -0700427
Eric Laurentb23d5282013-05-14 15:27:20 -0700428 [USECASE_AUDIO_RECORD] = "audio-record",
Krishna Kishor Jha0f4d8482022-02-18 10:56:05 +0530429 [USECASE_AUDIO_RECORD2] = "audio-record2",
430 [USECASE_AUDIO_RECORD3] = "audio-record3",
Mingming Yine62d7842013-10-25 16:26:03 -0700431 [USECASE_AUDIO_RECORD_COMPRESS] = "audio-record-compress",
Dhananjay Kumaree4d2002016-10-25 18:02:58 +0530432 [USECASE_AUDIO_RECORD_COMPRESS2] = "audio-record-compress2",
433 [USECASE_AUDIO_RECORD_COMPRESS3] = "audio-record-compress3",
434 [USECASE_AUDIO_RECORD_COMPRESS4] = "audio-record-compress4",
Dhananjay Kumar376e38b2017-09-28 22:26:23 +0530435 [USECASE_AUDIO_RECORD_COMPRESS5] = "audio-record-compress5",
436 [USECASE_AUDIO_RECORD_COMPRESS6] = "audio-record-compress6",
Eric Laurentb23d5282013-05-14 15:27:20 -0700437 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
Kogara Naveen Kumarc5758232022-11-07 20:25:40 +0530438 [USECASE_AUDIO_RECORD_LOW_LATENCY2] = "low-latency-record2",
Preetam Singh Ranawatde84f1a2013-11-01 14:58:16 -0700439 [USECASE_AUDIO_RECORD_FM_VIRTUAL] = "fm-virtual-record",
Haynes Mathew George16081042017-05-31 17:16:49 -0700440 [USECASE_AUDIO_RECORD_MMAP] = "mmap-record",
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700441 [USECASE_AUDIO_RECORD_HIFI] = "hifi-record",
Haynes Mathew George16081042017-05-31 17:16:49 -0700442
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -0800443 [USECASE_AUDIO_HFP_SCO] = "hfp-sco",
Vimal Puthanveed47e64852013-12-20 13:23:39 -0800444 [USECASE_AUDIO_HFP_SCO_WB] = "hfp-sco-wb",
Derek Chenf7092792017-05-23 12:23:53 -0400445 [USECASE_AUDIO_HFP_SCO_DOWNLINK] = "hfp-sco-downlink",
446 [USECASE_AUDIO_HFP_SCO_WB_DOWNLINK] = "hfp-sco-wb-downlink",
Mingming Yin3ee55c62014-08-04 14:23:35 -0700447
Derek Chenf7092792017-05-23 12:23:53 -0400448 [USECASE_VOICE_CALL] = "voice-call",
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700449 [USECASE_VOICE2_CALL] = "voice2-call",
450 [USECASE_VOLTE_CALL] = "volte-call",
451 [USECASE_QCHAT_CALL] = "qchat-call",
Vicky Sehrawat7e4fc152014-02-12 17:58:59 -0800452 [USECASE_VOWLAN_CALL] = "vowlan-call",
Vidyakumar Athota0e109352015-02-12 17:38:22 -0800453 [USECASE_VOICEMMODE1_CALL] = "voicemmode1-call",
454 [USECASE_VOICEMMODE2_CALL] = "voicemmode2-call",
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800455 [USECASE_COMPRESS_VOIP_CALL] = "compress-voip-call",
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700456 [USECASE_INCALL_REC_UPLINK] = "incall-rec-uplink",
457 [USECASE_INCALL_REC_DOWNLINK] = "incall-rec-downlink",
458 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK] = "incall-rec-uplink-and-downlink",
Helen Zenge56b4852013-12-03 16:54:40 -0800459 [USECASE_INCALL_REC_UPLINK_COMPRESS] = "incall-rec-uplink-compress",
460 [USECASE_INCALL_REC_DOWNLINK_COMPRESS] = "incall-rec-downlink-compress",
461 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK_COMPRESS] = "incall-rec-uplink-and-downlink-compress",
462
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -0700463 [USECASE_INCALL_MUSIC_UPLINK] = "incall_music_uplink",
464 [USECASE_INCALL_MUSIC_UPLINK2] = "incall_music_uplink2",
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700465 [USECASE_AUDIO_SPKR_CALIB_RX] = "spkr-rx-calib",
466 [USECASE_AUDIO_SPKR_CALIB_TX] = "spkr-vi-record",
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -0700467
468 [USECASE_AUDIO_PLAYBACK_AFE_PROXY] = "afe-proxy-playback",
469 [USECASE_AUDIO_RECORD_AFE_PROXY] = "afe-proxy-record",
Zhou Song62ea0282020-03-22 19:53:01 +0800470 [USECASE_AUDIO_RECORD_AFE_PROXY2] = "afe-proxy-record2",
Md Mansoor Ahmeddb1b4f92018-01-25 18:56:31 +0530471 [USECASE_AUDIO_PLAYBACK_SILENCE] = "silence-playback",
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700472
Siddartha Shaik31b530e2017-05-19 15:26:33 +0530473 /* Transcode loopback cases */
Surendar Karka93cd25a2018-08-28 14:21:37 +0530474 [USECASE_AUDIO_TRANSCODE_LOOPBACK_RX] = "audio-transcode-loopback-rx",
475 [USECASE_AUDIO_TRANSCODE_LOOPBACK_TX] = "audio-transcode-loopback-tx",
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700476
477 [USECASE_AUDIO_PLAYBACK_VOIP] = "audio-playback-voip",
478 [USECASE_AUDIO_RECORD_VOIP] = "audio-record-voip",
Revathi Uddarajud9f23d92020-07-27 10:55:06 +0530479 [USECASE_AUDIO_RECORD_VOIP_LOW_LATENCY] = "audio-record-voip-low-latency",
Varun Balaraje49253e2017-07-06 19:48:56 +0530480 /* For Interactive Audio Streams */
481 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM1] = "audio-interactive-stream1",
482 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM2] = "audio-interactive-stream2",
483 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM3] = "audio-interactive-stream3",
484 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM4] = "audio-interactive-stream4",
485 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM5] = "audio-interactive-stream5",
486 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM6] = "audio-interactive-stream6",
487 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM7] = "audio-interactive-stream7",
488 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM8] = "audio-interactive-stream8",
Garmond Leunge2433c32017-09-28 21:51:22 -0700489
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800490 [USECASE_AUDIO_EC_REF_LOOPBACK] = "ec-ref-audio-capture",
491
Derek Chenf6318be2017-06-12 17:16:24 -0400492 [USECASE_AUDIO_A2DP_ABR_FEEDBACK] = "a2dp-abr-feedback",
493
494 [USECASE_AUDIO_PLAYBACK_MEDIA] = "media-playback",
Kogara Naveen Kumarc5758232022-11-07 20:25:40 +0530495 [USECASE_AUDIO_PLAYBACK_MEDIA_LL] = "media-playback-ll",
Derek Chenf6318be2017-06-12 17:16:24 -0400496 [USECASE_AUDIO_PLAYBACK_SYS_NOTIFICATION] = "sys-notification-playback",
497 [USECASE_AUDIO_PLAYBACK_NAV_GUIDANCE] = "nav-guidance-playback",
Kogara Naveen Kumarc5758232022-11-07 20:25:40 +0530498 [USECASE_AUDIO_PLAYBACK_NAV_GUIDANCE_LL] = "nav-guidance-playback-ll",
Derek Chenf6318be2017-06-12 17:16:24 -0400499 [USECASE_AUDIO_PLAYBACK_PHONE] = "phone-playback",
Kogara Naveen Kumar65828fe2022-10-14 16:41:04 +0530500 [USECASE_AUDIO_PLAYBACK_PHONE_LL] = "phone-playback-ll",
Susan Wang117cf6f2022-04-06 20:11:46 -0400501 [USECASE_AUDIO_PLAYBACK_ALERTS] = "alerts-playback",
Kogara Naveen Kumarc5758232022-11-07 20:25:40 +0530502 [USECASE_AUDIO_PLAYBACK_ALERTS_LL] = "alerts-playback-ll",
Derek Chen1bcdc6b2020-02-06 22:44:53 -0800503 [USECASE_AUDIO_PLAYBACK_FRONT_PASSENGER] = "front-passenger-playback",
Derek Chendf05eea2019-08-01 13:57:49 -0700504 [USECASE_AUDIO_PLAYBACK_REAR_SEAT] = "rear-seat-playback",
Rahul Sharma99770982019-03-06 17:05:26 +0530505 [USECASE_AUDIO_FM_TUNER_EXT] = "fm-tuner-ext",
Derek Chena30a5f42019-12-03 11:17:09 -0500506 [USECASE_ICC_CALL] = "icc-call",
Huicheng Liu1404ba12020-09-11 01:03:25 -0400507
508 [USECASE_AUDIO_RECORD_BUS] = "audio-record",
509 [USECASE_AUDIO_RECORD_BUS_FRONT_PASSENGER] = "front-passenger-record",
510 [USECASE_AUDIO_RECORD_BUS_REAR_SEAT] = "rear-seat-record",
Fei Tongaffdf732020-02-20 20:39:05 +0800511 [USECASE_AUDIO_PLAYBACK_SYNTHESIZER] = "synth-loopback",
Susan Wange3959562021-03-11 11:50:26 -0500512 [USECASE_AUDIO_RECORD_ECHO_REF_EXT] = "echo-reference-external",
Eric Laurentb23d5282013-05-14 15:27:20 -0700513};
514
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700515static const audio_usecase_t offload_usecases[] = {
516 USECASE_AUDIO_PLAYBACK_OFFLOAD,
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700517 USECASE_AUDIO_PLAYBACK_OFFLOAD2,
518 USECASE_AUDIO_PLAYBACK_OFFLOAD3,
519 USECASE_AUDIO_PLAYBACK_OFFLOAD4,
520 USECASE_AUDIO_PLAYBACK_OFFLOAD5,
521 USECASE_AUDIO_PLAYBACK_OFFLOAD6,
522 USECASE_AUDIO_PLAYBACK_OFFLOAD7,
523 USECASE_AUDIO_PLAYBACK_OFFLOAD8,
524 USECASE_AUDIO_PLAYBACK_OFFLOAD9,
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700525};
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800526
Varun Balaraje49253e2017-07-06 19:48:56 +0530527static const audio_usecase_t interactive_usecases[] = {
528 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM1,
529 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM2,
530 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM3,
531 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM4,
532 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM5,
533 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM6,
534 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM7,
535 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM8,
536};
537
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800538#define STRING_TO_ENUM(string) { #string, string }
539
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800540struct string_to_enum {
541 const char *name;
542 uint32_t value;
543};
544
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700545static const struct string_to_enum channels_name_to_enum_table[] = {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800546 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
Mingming Yin3a941d42016-02-17 18:08:05 -0800547 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_2POINT1),
548 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_QUAD),
549 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_SURROUND),
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -0700550 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_PENTA),
Mingming Yin3a941d42016-02-17 18:08:05 -0800551 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
552 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_6POINT1),
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800553 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700554 STRING_TO_ENUM(AUDIO_CHANNEL_IN_MONO),
555 STRING_TO_ENUM(AUDIO_CHANNEL_IN_STEREO),
556 STRING_TO_ENUM(AUDIO_CHANNEL_IN_FRONT_BACK),
557 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_1),
558 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_2),
559 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_3),
560 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_4),
561 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_5),
562 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_6),
563 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_7),
564 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_8),
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800565};
566
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700567static const struct string_to_enum formats_name_to_enum_table[] = {
568 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
569 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
570 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Pradnya Chaphekar8a9dcd82014-09-09 09:49:10 -0700571 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
572 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
573 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Ben Romberger1aaaf862017-04-06 17:49:46 -0700574 STRING_TO_ENUM(AUDIO_FORMAT_DOLBY_TRUEHD),
Mingming Yin3a941d42016-02-17 18:08:05 -0800575 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
576 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
Naresh Tanniru928f0862017-04-07 16:44:23 -0700577 STRING_TO_ENUM(AUDIO_FORMAT_IEC61937)
Mingming Yin3a941d42016-02-17 18:08:05 -0800578};
579
580//list of all supported sample rates by HDMI specification.
581static const int out_hdmi_sample_rates[] = {
582 32000, 44100, 48000, 88200, 96000, 176400, 192000,
583};
584
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700585static const struct string_to_enum out_sample_rates_name_to_enum_table[] = {
Mingming Yin3a941d42016-02-17 18:08:05 -0800586 STRING_TO_ENUM(32000),
587 STRING_TO_ENUM(44100),
588 STRING_TO_ENUM(48000),
589 STRING_TO_ENUM(88200),
590 STRING_TO_ENUM(96000),
591 STRING_TO_ENUM(176400),
592 STRING_TO_ENUM(192000),
Mingshu pange8aedf12019-10-25 15:38:27 +0800593 STRING_TO_ENUM(352800),
594 STRING_TO_ENUM(384000),
Pradnya Chaphekar8a9dcd82014-09-09 09:49:10 -0700595};
596
Carter Hsu2e429db2019-05-14 18:50:52 +0800597struct in_effect_list {
598 struct listnode list;
599 effect_handle_t handle;
600};
601
Krishna Kishor Jha0f4d8482022-02-18 10:56:05 +0530602static const audio_usecase_t record_usecases[] = {
603 USECASE_AUDIO_RECORD,
604 USECASE_AUDIO_RECORD2,
605 USECASE_AUDIO_RECORD3,
606};
607
Kogara Naveen Kumarc5758232022-11-07 20:25:40 +0530608static const audio_usecase_t low_latency_record_usecases[] = {
609 USECASE_AUDIO_RECORD_LOW_LATENCY,
610 USECASE_AUDIO_RECORD_LOW_LATENCY2,
611};
612
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700613static struct audio_device *adev = NULL;
Ben Romberger02ab1192018-05-24 12:10:08 -0700614static pthread_mutex_t adev_init_lock = PTHREAD_MUTEX_INITIALIZER;
Kiran Kandi910e1862013-10-29 13:29:42 -0700615static unsigned int audio_device_ref_count;
vivek mehtab72d08d2016-04-29 03:16:47 -0700616//cache last MBDRC cal step level
617static int last_known_cal_step = -1 ;
Kiran Kandi910e1862013-10-29 13:29:42 -0700618
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +0530619static int out_set_compr_volume(struct audio_stream_out *stream, float left, float right);
Arun Mirpuri5d170872019-03-26 13:21:31 -0700620static int out_set_mmap_volume(struct audio_stream_out *stream, float left, float right);
Zhou Song2b8f28f2017-09-11 10:51:38 +0800621static int out_set_voip_volume(struct audio_stream_out *stream, float left, float right);
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +0530622static int out_set_pcm_volume(struct audio_stream_out *stream, float left, float right);
Krishna Kishor Jha10ad39f2022-02-21 17:02:47 +0530623#ifdef SOFT_VOLUME
624static int out_set_soft_volume_params(struct audio_stream_out *stream);
625#endif
Derek Chen6f293672019-04-01 01:40:24 -0700626static void adev_snd_mon_cb(void *cookie, struct str_parms *parms);
627static void in_snd_mon_cb(void * stream, struct str_parms * parms);
628static void out_snd_mon_cb(void * stream, struct str_parms * parms);
629
Zhou Song331c8e52019-08-26 14:16:12 +0800630static int configure_btsco_sample_rate(snd_device_t snd_device);
631
Vatsal Buchac09ae062018-11-14 13:25:08 +0530632#ifdef AUDIO_FEATURE_ENABLED_GCOV
633extern void __gcov_flush();
634static void enable_gcov()
635{
636 __gcov_flush();
637}
638#else
639static void enable_gcov()
640{
641}
642#endif
643
justinweng20fb6d82019-02-21 18:49:00 -0700644static int in_set_microphone_direction(const struct audio_stream_in *stream,
645 audio_microphone_direction_t dir);
646static int in_set_microphone_field_dimension(const struct audio_stream_in *stream, float zoom);
647
Krishna Kishor Jha0f4d8482022-02-18 10:56:05 +0530648static bool is_pcm_record_usecase(audio_usecase_t uc_id)
649{
650 unsigned int record_uc_index;
651 unsigned int num_usecase = sizeof(record_usecases)/sizeof(record_usecases[0]);
652
653 for (record_uc_index = 0; record_uc_index < num_usecase; record_uc_index++) {
654 if (uc_id == record_usecases[record_uc_index])
655 return true;
656 }
657 return false;
658}
659
660static audio_usecase_t get_record_usecase(struct audio_device *adev)
661{
662 audio_usecase_t ret_uc = USECASE_INVALID;
663 unsigned int record_uc_index;
664 unsigned int num_usecase = sizeof(record_usecases)/sizeof(record_usecases[0]);
665
666 ALOGV("%s: num_usecase: %d", __func__, num_usecase);
667 for (record_uc_index = 0; record_uc_index < num_usecase; record_uc_index++) {
668 if (!(adev->pcm_record_uc_state & (0x1 << record_uc_index))) {
669 adev->pcm_record_uc_state |= 0x1 << record_uc_index;
670 ret_uc = record_usecases[record_uc_index];
671 break;
672 }
673 }
674
675 ALOGV("%s: pcm record usecase is %d", __func__, ret_uc);
676 return ret_uc;
677}
678
679static void free_record_usecase(struct audio_device *adev,
680 audio_usecase_t uc_id)
681{
682 unsigned int record_uc_index;
683 unsigned int num_usecase = sizeof(record_usecases)/sizeof(record_usecases[0]);
684
685 for (record_uc_index = 0; record_uc_index < num_usecase; record_uc_index++) {
686 if (record_usecases[record_uc_index] == uc_id) {
687 adev->pcm_record_uc_state &= ~(0x1 << record_uc_index);
688 break;
689 }
690 }
691 ALOGV("%s: free pcm record usecase %d", __func__, uc_id);
692}
693
Kogara Naveen Kumarc5758232022-11-07 20:25:40 +0530694static bool is_pcm_low_latency_record_usecase(audio_usecase_t uc_id)
695{
696 unsigned int record_uc_index;
697 unsigned int num_usecase = sizeof(low_latency_record_usecases)/sizeof(low_latency_record_usecases[0]);
698 ALOGD("%s: Check low latency pcm record usecase", __func__);
699
700 for (record_uc_index = 0; record_uc_index < num_usecase; record_uc_index++) {
701 if (uc_id == low_latency_record_usecases[record_uc_index])
702 return true;
703 }
704 return false;
705}
706
707static audio_usecase_t get_low_latency_record_usecase(struct audio_device *adev)
708{
709 audio_usecase_t ret_uc = USECASE_INVALID;
710 unsigned int record_uc_index;
711 unsigned int num_usecase = sizeof(low_latency_record_usecases)/sizeof(low_latency_record_usecases[0]);
712
713 ALOGD("%s: get_low_latency_record_usecase: %d", __func__, num_usecase);
714 for (record_uc_index = 0; record_uc_index < num_usecase; record_uc_index++) {
715 if (!(adev->pcm_low_latency_record_uc_state & (0x1 << record_uc_index))) {
716 adev->pcm_low_latency_record_uc_state |= 0x1 << record_uc_index;
717 ALOGD("%s: get_low_latency_record_usecase: %d", __func__, record_uc_index);
718 ret_uc = low_latency_record_usecases[record_uc_index];
719 break;
720 }
721 }
722
723 ALOGD("%s: low latency pcm record usecase is %d", __func__, ret_uc);
724 return ret_uc;
725}
726
727static void free_low_latency_record_usecase(struct audio_device *adev,
728 audio_usecase_t uc_id)
729{
730 unsigned int record_uc_index;
731 unsigned int num_usecase = sizeof(low_latency_record_usecases)/sizeof(low_latency_record_usecases[0]);
732
733 for (record_uc_index = 0; record_uc_index < num_usecase; record_uc_index++) {
734 if (low_latency_record_usecases[record_uc_index] == uc_id) {
735 adev->pcm_low_latency_record_uc_state &= ~(0x1 << record_uc_index);
736 break;
737 }
738 }
739 ALOGD("%s: free low latency pcm record usecase %d", __func__, uc_id);
740}
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700741static bool may_use_noirq_mode(struct audio_device *adev, audio_usecase_t uc_id,
742 int flags __unused)
743{
744 int dir = 0;
745 switch (uc_id) {
746 case USECASE_AUDIO_RECORD_LOW_LATENCY:
Revathi Uddarajud9f23d92020-07-27 10:55:06 +0530747 case USECASE_AUDIO_RECORD_VOIP_LOW_LATENCY:
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700748 dir = 1;
749 case USECASE_AUDIO_PLAYBACK_ULL:
750 break;
751 default:
752 return false;
753 }
754
755 int dev_id = platform_get_pcm_device_id(uc_id, dir == 0 ?
756 PCM_PLAYBACK : PCM_CAPTURE);
757 if (adev->adm_is_noirq_avail)
758 return adev->adm_is_noirq_avail(adev->adm_data,
759 adev->snd_card, dev_id, dir);
760 return false;
761}
762
763static void register_out_stream(struct stream_out *out)
764{
765 struct audio_device *adev = out->dev;
766 if (is_offload_usecase(out->usecase) ||
767 !adev->adm_register_output_stream)
768 return;
769
770 // register stream first for backward compatibility
771 adev->adm_register_output_stream(adev->adm_data,
772 out->handle,
773 out->flags);
774
775 if (!adev->adm_set_config)
776 return;
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +0530777#ifdef PLATFORM_AUTO
Kogara Naveen Kumar6db5fb02022-05-07 00:22:50 +0530778 if (out->realtime || (out->flags & AUDIO_OUTPUT_FLAG_SYS_NOTIFICATION))
779 adev->adm_set_config(adev->adm_data,
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700780 out->handle,
781 out->pcm, &out->config);
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +0530782#else
783 if (out->realtime)
784 adev->adm_set_config(adev->adm_data,
785 out->handle,
786 out->pcm, &out->config);
787#endif
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700788}
789
790static void register_in_stream(struct stream_in *in)
791{
792 struct audio_device *adev = in->dev;
793 if (!adev->adm_register_input_stream)
794 return;
795
796 adev->adm_register_input_stream(adev->adm_data,
797 in->capture_handle,
798 in->flags);
799
800 if (!adev->adm_set_config)
801 return;
802
803 if (in->realtime)
804 adev->adm_set_config(adev->adm_data,
805 in->capture_handle,
806 in->pcm,
807 &in->config);
808}
809
810static void request_out_focus(struct stream_out *out, long ns)
811{
812 struct audio_device *adev = out->dev;
813
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700814 if (adev->adm_request_focus_v2)
815 adev->adm_request_focus_v2(adev->adm_data, out->handle, ns);
816 else if (adev->adm_request_focus)
817 adev->adm_request_focus(adev->adm_data, out->handle);
818}
819
Aniket Kumar Lata60586a92019-05-22 22:18:55 -0700820static int request_in_focus(struct stream_in *in, long ns)
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700821{
822 struct audio_device *adev = in->dev;
Aniket Kumar Lata60586a92019-05-22 22:18:55 -0700823 int ret = 0;
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700824
Aniket Kumar Lata60586a92019-05-22 22:18:55 -0700825 if (adev->adm_request_focus_v2_1)
826 ret = adev->adm_request_focus_v2_1(adev->adm_data, in->capture_handle, ns);
827 else if (adev->adm_request_focus_v2)
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700828 adev->adm_request_focus_v2(adev->adm_data, in->capture_handle, ns);
829 else if (adev->adm_request_focus)
830 adev->adm_request_focus(adev->adm_data, in->capture_handle);
Aniket Kumar Lata60586a92019-05-22 22:18:55 -0700831
832 return ret;
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700833}
834
835static void release_out_focus(struct stream_out *out)
836{
837 struct audio_device *adev = out->dev;
838
839 if (adev->adm_abandon_focus)
840 adev->adm_abandon_focus(adev->adm_data, out->handle);
841}
842
843static void release_in_focus(struct stream_in *in)
844{
845 struct audio_device *adev = in->dev;
846 if (adev->adm_abandon_focus)
847 adev->adm_abandon_focus(adev->adm_data, in->capture_handle);
848}
849
Dhananjay Kumare6293dd2017-05-25 17:25:30 +0530850static int parse_snd_card_status(struct str_parms *parms, int *card,
851 card_status_t *status)
852{
853 char value[32]={0};
854 char state[32]={0};
855
856 int ret = str_parms_get_str(parms, "SND_CARD_STATUS", value, sizeof(value));
857 if (ret < 0)
858 return -1;
859
860 // sscanf should be okay as value is of max length 32.
861 // same as sizeof state.
862 if (sscanf(value, "%d,%s", card, state) < 2)
863 return -1;
864
865 *status = !strcmp(state, "ONLINE") ? CARD_STATUS_ONLINE :
866 CARD_STATUS_OFFLINE;
867 return 0;
868}
869
Avinash Chandrad7296d42021-08-04 15:07:47 +0530870bool is_combo_audio_input_device(struct listnode *devices){
871
Sandhya Mutha Naga Venkata153d95e2022-07-12 14:54:43 +0530872 if ((devices == NULL) || (!list_empty(devices)))
Avinash Chandrad7296d42021-08-04 15:07:47 +0530873 return false;
874
875 if(compare_device_type(devices, AUDIO_DEVICE_IN_BUILTIN_MIC|AUDIO_DEVICE_IN_SPEAKER_MIC2))
876 return true;
877 else
878 return false;
879}
880
Haynes Mathew Georgeb0f5dc32017-10-06 18:35:12 -0700881static inline void adjust_frames_for_device_delay(struct stream_out *out,
882 uint32_t *dsp_frames) {
883 // Adjustment accounts for A2dp encoder latency with offload usecases
884 // Note: Encoder latency is returned in ms.
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800885 if (is_a2dp_out_device_type(&out->device_list)) {
Haynes Mathew Georgeb0f5dc32017-10-06 18:35:12 -0700886 unsigned long offset =
887 (audio_extn_a2dp_get_encoder_latency() * out->sample_rate / 1000);
888 *dsp_frames = (*dsp_frames > offset) ? (*dsp_frames - offset) : 0;
889 }
890}
891
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700892static inline bool free_entry(void *key __unused,
893 void *value, void *context __unused)
894{
895 free(value);
896 return true;
897}
898
899static inline void free_map(Hashmap *map)
900{
901 if (map) {
902 hashmapForEach(map, free_entry, (void *) NULL);
903 hashmapFree(map);
904 }
905}
906
Aniket Kumar Latabc774842020-01-16 21:22:05 -0800907static inline void patch_map_remove_l(struct audio_device *adev,
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700908 audio_patch_handle_t patch_handle)
909{
910 if (patch_handle == AUDIO_PATCH_HANDLE_NONE)
911 return;
912
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700913 struct audio_patch_info *p_info =
914 hashmapGet(adev->patch_map, (void *) (intptr_t) patch_handle);
915 if (p_info) {
916 ALOGV("%s: Remove patch %d", __func__, patch_handle);
917 hashmapRemove(adev->patch_map, (void *) (intptr_t) patch_handle);
918 free(p_info->patch);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700919 free(p_info);
920 }
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700921}
922
923static inline int io_streams_map_insert(struct audio_device *adev,
924 struct audio_stream *stream,
925 audio_io_handle_t handle,
926 audio_patch_handle_t patch_handle)
927{
928 struct audio_stream_info *s_info =
929 (struct audio_stream_info *) calloc(1, sizeof(struct audio_stream_info));
930
931 if (s_info == NULL) {
932 ALOGE("%s: Could not allocate stream info", __func__);
933 return -ENOMEM;
934 }
935 s_info->stream = stream;
936 s_info->patch_handle = patch_handle;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700937
938 pthread_mutex_lock(&adev->lock);
939 struct audio_stream_info *stream_info =
940 hashmapPut(adev->io_streams_map, (void *) (intptr_t) handle, (void *) s_info);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700941 if (stream_info != NULL)
942 free(stream_info);
Aniket Kumar Latabc774842020-01-16 21:22:05 -0800943 pthread_mutex_unlock(&adev->lock);
Aniket Kumar Latad13758f2020-08-06 15:11:36 -0700944 ALOGV("%s: Added stream in io_streams_map with handle %d", __func__, handle);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700945 return 0;
946}
947
948static inline void io_streams_map_remove(struct audio_device *adev,
949 audio_io_handle_t handle)
950{
951 pthread_mutex_lock(&adev->lock);
952 struct audio_stream_info *s_info =
953 hashmapRemove(adev->io_streams_map, (void *) (intptr_t) handle);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700954 if (s_info == NULL)
Aniket Kumar Latabc774842020-01-16 21:22:05 -0800955 goto done;
Aniket Kumar Latad13758f2020-08-06 15:11:36 -0700956 ALOGV("%s: Removed stream with handle %d", __func__, handle);
Aniket Kumar Latabc774842020-01-16 21:22:05 -0800957 patch_map_remove_l(adev, s_info->patch_handle);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700958 free(s_info);
Aniket Kumar Latabc774842020-01-16 21:22:05 -0800959done:
960 pthread_mutex_unlock(&adev->lock);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700961 return;
962}
963
Aniket Kumar Latabc774842020-01-16 21:22:05 -0800964static struct audio_patch_info* fetch_patch_info_l(struct audio_device *adev,
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700965 audio_patch_handle_t handle)
966{
967 struct audio_patch_info *p_info = NULL;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700968 p_info = (struct audio_patch_info *)
969 hashmapGet(adev->patch_map, (void *) (intptr_t) handle);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700970 return p_info;
971}
972
vivek mehtaa76401a2015-04-24 14:12:15 -0700973__attribute__ ((visibility ("default")))
974bool audio_hw_send_gain_dep_calibration(int level) {
975 bool ret_val = false;
vivek mehtab72d08d2016-04-29 03:16:47 -0700976 ALOGV("%s: called ...", __func__);
vivek mehtaa76401a2015-04-24 14:12:15 -0700977
978 pthread_mutex_lock(&adev_init_lock);
979
980 if (adev != NULL && adev->platform != NULL) {
981 pthread_mutex_lock(&adev->lock);
982 ret_val = platform_send_gain_dep_cal(adev->platform, level);
vivek mehtab72d08d2016-04-29 03:16:47 -0700983
Preetam Singh Ranawatf4ae0222017-05-31 17:07:28 +0530984 // cache level info for any of the use case which
985 // was not started.
986 last_known_cal_step = level;;
vivek mehtab72d08d2016-04-29 03:16:47 -0700987
vivek mehtaa76401a2015-04-24 14:12:15 -0700988 pthread_mutex_unlock(&adev->lock);
989 } else {
990 ALOGE("%s: %s is NULL", __func__, adev == NULL ? "adev" : "adev->platform");
991 }
992
993 pthread_mutex_unlock(&adev_init_lock);
994
995 return ret_val;
996}
997
Ashish Jain5106d362016-05-11 19:23:33 +0530998static int check_and_set_gapless_mode(struct audio_device *adev, bool enable_gapless)
999{
Krishnankutty Kolathappilly6d8788b2014-01-09 12:45:31 -08001000 bool gapless_enabled = false;
1001 const char *mixer_ctl_name = "Compress Gapless Playback";
1002 struct mixer_ctl *ctl;
1003
1004 ALOGV("%s:", __func__);
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07001005 gapless_enabled = property_get_bool("vendor.audio.offload.gapless.enabled", false);
Ashish Jain5106d362016-05-11 19:23:33 +05301006
1007 /*Disable gapless if its AV playback*/
1008 gapless_enabled = gapless_enabled && enable_gapless;
Krishnankutty Kolathappilly6d8788b2014-01-09 12:45:31 -08001009
1010 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1011 if (!ctl) {
1012 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1013 __func__, mixer_ctl_name);
1014 return -EINVAL;
1015 }
1016
1017 if (mixer_ctl_set_value(ctl, 0, gapless_enabled) < 0) {
1018 ALOGE("%s: Could not set gapless mode %d",
1019 __func__, gapless_enabled);
1020 return -EINVAL;
1021 }
1022 return 0;
1023}
Haynes Mathew George5191a852013-09-11 14:19:36 -07001024
Aniket Kumar Lataf56b6402016-10-27 12:03:18 -07001025__attribute__ ((visibility ("default")))
1026int audio_hw_get_gain_level_mapping(struct amp_db_and_gain_table *mapping_tbl,
1027 int table_size) {
1028 int ret_val = 0;
1029 ALOGV("%s: enter ... ", __func__);
1030
1031 pthread_mutex_lock(&adev_init_lock);
1032 if (adev == NULL) {
1033 ALOGW("%s: adev is NULL .... ", __func__);
1034 goto done;
1035 }
1036
1037 pthread_mutex_lock(&adev->lock);
1038 ret_val = platform_get_gain_level_mapping(mapping_tbl, table_size);
1039 pthread_mutex_unlock(&adev->lock);
1040done:
1041 pthread_mutex_unlock(&adev_init_lock);
1042 ALOGV("%s: exit ... ", __func__);
1043 return ret_val;
1044}
1045
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001046bool audio_hw_send_qdsp_parameter(int stream_type, float vol, bool active)
Aalique Grahame22e49102018-12-18 14:23:57 -08001047{
1048 bool ret = false;
1049 ALOGV("%s: enter ...", __func__);
1050
1051 pthread_mutex_lock(&adev_init_lock);
1052
1053 if (adev != NULL && adev->platform != NULL) {
1054 pthread_mutex_lock(&adev->lock);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001055 ret = audio_extn_qdsp_set_state(adev, stream_type, vol, active);
Aalique Grahame22e49102018-12-18 14:23:57 -08001056 pthread_mutex_unlock(&adev->lock);
1057 }
1058
1059 pthread_mutex_unlock(&adev_init_lock);
1060
1061 ALOGV("%s: exit with ret %d", __func__, ret);
1062 return ret;
1063}
Aalique Grahame22e49102018-12-18 14:23:57 -08001064
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001065static bool is_supported_format(audio_format_t format)
1066{
Eric Laurent86e17132013-09-12 17:49:30 -07001067 if (format == AUDIO_FORMAT_MP3 ||
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301068 format == AUDIO_FORMAT_MP2 ||
Ashish Jainf9b78162014-08-25 20:36:25 +05301069 format == AUDIO_FORMAT_AAC_LC ||
1070 format == AUDIO_FORMAT_AAC_HE_V1 ||
1071 format == AUDIO_FORMAT_AAC_HE_V2 ||
Manish Dewangana6fc5442015-08-24 20:30:31 +05301072 format == AUDIO_FORMAT_AAC_ADTS_LC ||
1073 format == AUDIO_FORMAT_AAC_ADTS_HE_V1 ||
1074 format == AUDIO_FORMAT_AAC_ADTS_HE_V2 ||
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301075 format == AUDIO_FORMAT_AAC_LATM_LC ||
1076 format == AUDIO_FORMAT_AAC_LATM_HE_V1 ||
1077 format == AUDIO_FORMAT_AAC_LATM_HE_V2 ||
Ashish Jain5106d362016-05-11 19:23:33 +05301078 format == AUDIO_FORMAT_PCM_24_BIT_PACKED ||
1079 format == AUDIO_FORMAT_PCM_8_24_BIT ||
Ashish Jainf1eaa582016-05-23 20:54:24 +05301080 format == AUDIO_FORMAT_PCM_FLOAT ||
1081 format == AUDIO_FORMAT_PCM_32_BIT ||
vivek mehta0ea887a2015-08-26 14:01:20 -07001082 format == AUDIO_FORMAT_PCM_16_BIT ||
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05301083 format == AUDIO_FORMAT_AC3 ||
1084 format == AUDIO_FORMAT_E_AC3 ||
Ben Romberger1aaaf862017-04-06 17:49:46 -07001085 format == AUDIO_FORMAT_DOLBY_TRUEHD ||
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05301086 format == AUDIO_FORMAT_DTS ||
1087 format == AUDIO_FORMAT_DTS_HD ||
Weiyin Jiang18ac4e92015-03-15 15:03:40 +08001088 format == AUDIO_FORMAT_FLAC ||
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +05301089 format == AUDIO_FORMAT_ALAC ||
1090 format == AUDIO_FORMAT_APE ||
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05301091 format == AUDIO_FORMAT_DSD ||
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +05301092 format == AUDIO_FORMAT_VORBIS ||
Weiyin Jiang18ac4e92015-03-15 15:03:40 +08001093 format == AUDIO_FORMAT_WMA ||
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05301094 format == AUDIO_FORMAT_WMA_PRO ||
Naresh Tanniru928f0862017-04-07 16:44:23 -07001095 format == AUDIO_FORMAT_APTX ||
1096 format == AUDIO_FORMAT_IEC61937)
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08001097 return true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001098
1099 return false;
1100}
1101
Kunlei Zhang67cc7072020-12-18 17:16:49 +08001102static bool is_supported_conc_usecase_for_power_mode_call(struct audio_device *adev)
1103{
1104 struct listnode *node;
1105 struct audio_usecase *usecase;
1106
1107 list_for_each(node, &adev->usecase_list) {
1108 usecase = node_to_item(node, struct audio_usecase, list);
1109 if (usecase->id == USECASE_AUDIO_PLAYBACK_FM) {
1110 ALOGD("%s: FM usecase is active, not setting power mode", __func__);
1111 return false;
1112 }
1113 }
1114
1115 return true;
1116}
Haynes Mathew George5beddd42016-06-27 18:33:40 -07001117static inline bool is_mmap_usecase(audio_usecase_t uc_id)
1118{
1119 return (uc_id == USECASE_AUDIO_RECORD_AFE_PROXY) ||
Zhou Song62ea0282020-03-22 19:53:01 +08001120 (uc_id == USECASE_AUDIO_RECORD_AFE_PROXY2) ||
Haynes Mathew George5beddd42016-06-27 18:33:40 -07001121 (uc_id == USECASE_AUDIO_PLAYBACK_AFE_PROXY);
1122}
1123
Aniket Kumar Lata8426d1f2019-06-10 15:25:53 -07001124static inline bool is_valid_volume(float left, float right)
1125{
1126 return ((left >= 0.0f && right >= 0.0f) ? true : false);
1127}
1128
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +05301129static void enable_asrc_mode(struct audio_device *adev)
Preetam Singh Ranawatb0c0dd72016-08-18 00:32:06 +05301130{
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +05301131 ALOGV("%s", __func__);
1132 audio_route_apply_and_update_path(adev->audio_route,
1133 "asrc-mode");
1134 adev->asrc_mode_enabled = true;
1135}
1136
1137static void disable_asrc_mode(struct audio_device *adev)
1138{
1139 ALOGV("%s", __func__);
1140 audio_route_reset_and_update_path(adev->audio_route,
1141 "asrc-mode");
1142 adev->asrc_mode_enabled = false;
1143}
1144
Saurav Kumarc1411662020-10-14 10:50:45 +05301145static void check_and_configure_headphone(struct audio_device *adev,
1146 struct audio_usecase *uc_info,
1147 snd_device_t snd_device)
1148{
1149 struct listnode *node;
1150 struct audio_usecase *usecase;
1151 int new_backend_idx, usecase_backend_idx;
1152 bool spkr_hph_single_be_native_concurrency;
1153
1154 new_backend_idx = platform_get_backend_index(snd_device);
1155 spkr_hph_single_be_native_concurrency = platform_get_spkr_hph_single_be_native_concurrency_flag();
Zhou Songd4b18c42021-01-14 15:15:29 +08001156 if ((spkr_hph_single_be_native_concurrency && (new_backend_idx == DEFAULT_CODEC_BACKEND)) ||
1157 uc_info->id == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) {
Saurav Kumarc1411662020-10-14 10:50:45 +05301158 list_for_each(node, &adev->usecase_list) {
1159 usecase = node_to_item(node, struct audio_usecase, list);
1160 if ((usecase->type != PCM_CAPTURE) && (usecase != uc_info)) {
1161 usecase_backend_idx = platform_get_backend_index(usecase->out_snd_device);
1162 if (((usecase_backend_idx == HEADPHONE_BACKEND) ||
1163 (usecase_backend_idx == HEADPHONE_44_1_BACKEND)) &&
1164 ((usecase->stream.out->sample_rate % OUTPUT_SAMPLING_RATE_44100) == 0)) {
1165 disable_audio_route(adev, usecase);
1166 disable_snd_device(adev, usecase->out_snd_device);
1167 usecase->stream.out->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Saurav Kumar70902382020-10-28 11:16:04 +05301168 platform_check_and_set_codec_backend_cfg(adev, usecase,
1169 usecase->out_snd_device);
Saurav Kumarc1411662020-10-14 10:50:45 +05301170 enable_snd_device(adev, usecase->out_snd_device);
Zhou Songd4b18c42021-01-14 15:15:29 +08001171 enable_audio_route(adev, usecase);
Saurav Kumarc1411662020-10-14 10:50:45 +05301172 }
1173 }
Revathi Uddaraju2c0eac02021-07-27 02:06:42 -07001174 else if ((usecase->type != PCM_CAPTURE) && (usecase == uc_info)) {
1175 usecase_backend_idx = platform_get_backend_index(usecase->out_snd_device);
1176 if (((usecase_backend_idx == HEADPHONE_BACKEND) ||
1177 (usecase_backend_idx == HEADPHONE_44_1_BACKEND)) &&
1178 ((usecase->stream.out->sample_rate % OUTPUT_SAMPLING_RATE_44100) == 0)) {
1179 usecase->stream.out->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1180 platform_check_and_set_codec_backend_cfg(adev, usecase,
1181 usecase->out_snd_device);
1182 }
1183 }
Saurav Kumarc1411662020-10-14 10:50:45 +05301184 }
1185 }
1186}
1187
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +05301188/*
1189 * - Enable ASRC mode for incoming mix path use case(Headphone backend)if Headphone
1190 * 44.1 or Native DSD backends are enabled for any of current use case.
1191 * e.g. 48-> + (Naitve DSD or Headphone 44.1)
1192 * - Disable current mix path use case(Headphone backend) and re-enable it with
1193 * ASRC mode for incoming Headphone 44.1 or Native DSD use case.
1194 * e.g. Naitve DSD or Headphone 44.1 -> + 48
1195 */
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +05301196static void check_and_set_asrc_mode(struct audio_device *adev,
1197 struct audio_usecase *uc_info,
1198 snd_device_t snd_device)
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +05301199{
1200 ALOGV("%s snd device %d", __func__, snd_device);
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +05301201 int i, num_new_devices = 0;
1202 snd_device_t split_new_snd_devices[SND_DEVICE_OUT_END];
1203 /*
1204 *Split snd device for new combo use case
1205 *e.g. Headphopne 44.1-> + Ringtone (Headphone + Speaker)
1206 */
1207 if (platform_split_snd_device(adev->platform,
1208 snd_device,
1209 &num_new_devices,
1210 split_new_snd_devices) == 0) {
1211 for (i = 0; i < num_new_devices; i++)
1212 check_and_set_asrc_mode(adev, uc_info, split_new_snd_devices[i]);
1213 } else {
1214 int new_backend_idx = platform_get_backend_index(snd_device);
1215 if (((new_backend_idx == HEADPHONE_BACKEND) ||
1216 (new_backend_idx == HEADPHONE_44_1_BACKEND) ||
1217 (new_backend_idx == DSD_NATIVE_BACKEND)) &&
1218 !adev->asrc_mode_enabled) {
1219 struct listnode *node = NULL;
1220 struct audio_usecase *uc = NULL;
1221 struct stream_out *curr_out = NULL;
1222 int usecase_backend_idx = DEFAULT_CODEC_BACKEND;
1223 int i, num_devices, ret = 0;
1224 snd_device_t split_snd_devices[SND_DEVICE_OUT_END];
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +05301225
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +05301226 list_for_each(node, &adev->usecase_list) {
1227 uc = node_to_item(node, struct audio_usecase, list);
1228 curr_out = (struct stream_out*) uc->stream.out;
1229 if (curr_out && PCM_PLAYBACK == uc->type && uc != uc_info) {
1230 /*
1231 *Split snd device for existing combo use case
1232 *e.g. Ringtone (Headphone + Speaker) + Headphopne 44.1
1233 */
1234 ret = platform_split_snd_device(adev->platform,
1235 uc->out_snd_device,
1236 &num_devices,
1237 split_snd_devices);
1238 if (ret < 0 || num_devices == 0) {
1239 ALOGV("%s: Unable to split uc->out_snd_device: %d",__func__, uc->out_snd_device);
1240 split_snd_devices[0] = uc->out_snd_device;
1241 num_devices = 1;
Garmond Leung50058f62017-02-08 09:49:30 -08001242 }
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +05301243 for (i = 0; i < num_devices; i++) {
1244 usecase_backend_idx = platform_get_backend_index(split_snd_devices[i]);
1245 ALOGD("%s:snd_dev %d usecase_backend_idx %d",__func__, split_snd_devices[i],usecase_backend_idx);
1246 if((new_backend_idx == HEADPHONE_BACKEND) &&
1247 ((usecase_backend_idx == HEADPHONE_44_1_BACKEND) ||
1248 (usecase_backend_idx == DSD_NATIVE_BACKEND))) {
Aniket Kumar Latad13758f2020-08-06 15:11:36 -07001249 ALOGV("%s:DSD or native stream detected enabling asrcmode in hardware",
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +05301250 __func__);
1251 enable_asrc_mode(adev);
1252 break;
1253 } else if(((new_backend_idx == HEADPHONE_44_1_BACKEND) ||
1254 (new_backend_idx == DSD_NATIVE_BACKEND)) &&
1255 (usecase_backend_idx == HEADPHONE_BACKEND)) {
Aniket Kumar Latad13758f2020-08-06 15:11:36 -07001256 ALOGV("%s: 48K stream detected, disabling and enabling it \
1257 with asrcmode in hardware", __func__);
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +05301258 disable_audio_route(adev, uc);
1259 disable_snd_device(adev, uc->out_snd_device);
1260 // Apply true-high-quality-mode if DSD or > 44.1KHz or >=24-bit
1261 if (new_backend_idx == DSD_NATIVE_BACKEND)
1262 audio_route_apply_and_update_path(adev->audio_route,
1263 "hph-true-highquality-mode");
1264 else if ((new_backend_idx == HEADPHONE_44_1_BACKEND) &&
1265 (curr_out->bit_width >= 24))
1266 audio_route_apply_and_update_path(adev->audio_route,
1267 "hph-highquality-mode");
1268 enable_asrc_mode(adev);
1269 enable_snd_device(adev, uc->out_snd_device);
1270 enable_audio_route(adev, uc);
1271 break;
1272 }
1273 }
1274 // reset split devices count
1275 num_devices = 0;
Garmond Leung50058f62017-02-08 09:49:30 -08001276 }
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +05301277 if (adev->asrc_mode_enabled)
1278 break;
Preetam Singh Ranawatb0c0dd72016-08-18 00:32:06 +05301279 }
1280 }
1281 }
1282}
1283
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001284static int send_effect_enable_disable_mixer_ctl(struct audio_device *adev,
1285 struct audio_effect_config effect_config,
1286 unsigned int param_value)
1287{
1288 char mixer_ctl_name[] = "Audio Effect";
1289 struct mixer_ctl *ctl;
1290 long set_values[6];
Eric Laurent637e2d42018-11-15 12:24:31 -08001291 struct stream_in *in = adev_get_active_input(adev);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001292
Aniket Kumar Lata7de63522019-06-13 11:05:18 -07001293 if (in == NULL) {
1294 ALOGE("%s: active input stream is NULL", __func__);
1295 return -EINVAL;
1296 }
1297
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001298 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1299 if (!ctl) {
1300 ALOGE("%s: Could not get mixer ctl - %s",
1301 __func__, mixer_ctl_name);
1302 return -EINVAL;
1303 }
1304
1305 set_values[0] = 1; //0:Rx 1:Tx
1306 set_values[1] = in->app_type_cfg.app_type;
1307 set_values[2] = (long)effect_config.module_id;
1308 set_values[3] = (long)effect_config.instance_id;
1309 set_values[4] = (long)effect_config.param_id;
1310 set_values[5] = param_value;
1311
1312 mixer_ctl_set_array(ctl, set_values, ARRAY_SIZE(set_values));
1313
1314 return 0;
1315
1316}
1317
1318static int update_effect_param_ecns(struct audio_device *adev, unsigned int module_id,
1319 int effect_type, unsigned int *param_value)
1320{
1321 int ret = 0;
1322 struct audio_effect_config other_effect_config;
1323 struct audio_usecase *usecase = NULL;
Eric Laurent637e2d42018-11-15 12:24:31 -08001324 struct stream_in *in = adev_get_active_input(adev);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001325
Aniket Kumar Lata7de63522019-06-13 11:05:18 -07001326 if (in == NULL) {
1327 ALOGE("%s: active input stream is NULL", __func__);
1328 return -EINVAL;
1329 }
1330
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001331 usecase = get_usecase_from_list(adev, in->usecase);
1332 if (!usecase)
1333 return -EINVAL;
1334
1335 ret = platform_get_effect_config_data(usecase->in_snd_device, &other_effect_config,
1336 effect_type == EFFECT_AEC ? EFFECT_NS : EFFECT_AEC);
1337 if (ret < 0) {
1338 ALOGE("%s Failed to get effect params %d", __func__, ret);
1339 return ret;
1340 }
1341
1342 if (module_id == other_effect_config.module_id) {
1343 //Same module id for AEC/NS. Values need to be combined
1344 if (((effect_type == EFFECT_AEC) && (in->enable_ns)) ||
1345 ((effect_type == EFFECT_NS) && (in->enable_aec))) {
1346 *param_value |= other_effect_config.param_value;
1347 }
1348 }
1349
1350 return ret;
1351}
1352
1353static int enable_disable_effect(struct audio_device *adev, int effect_type, bool enable)
Gangadhar Sb0210342019-02-22 17:39:41 +05301354{
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001355 struct audio_effect_config effect_config;
1356 struct audio_usecase *usecase = NULL;
1357 int ret = 0;
1358 unsigned int param_value = 0;
Eric Laurent637e2d42018-11-15 12:24:31 -08001359 struct stream_in *in = adev_get_active_input(adev);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001360
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001361 if(!voice_extn_is_dynamic_ecns_enabled())
1362 return ENOSYS;
1363
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001364 if (!in) {
1365 ALOGE("%s: Invalid input stream", __func__);
1366 return -EINVAL;
1367 }
1368
1369 ALOGD("%s: effect_type:%d enable:%d", __func__, effect_type, enable);
1370
1371 usecase = get_usecase_from_list(adev, in->usecase);
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001372 if (usecase == NULL) {
1373 ALOGE("%s: Could not find the usecase (%d) in the list",
1374 __func__, in->usecase);
1375 return -EINVAL;
1376 }
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001377
1378 ret = platform_get_effect_config_data(usecase->in_snd_device, &effect_config, effect_type);
1379 if (ret < 0) {
1380 ALOGE("%s Failed to get module id %d", __func__, ret);
1381 return ret;
1382 }
1383 ALOGV("%s: %d %d usecase->id:%d usecase->in_snd_device:%d", __func__, effect_config.module_id,
1384 in->app_type_cfg.app_type, usecase->id, usecase->in_snd_device);
1385
1386 if(enable)
1387 param_value = effect_config.param_value;
1388
1389 /*Special handling for AEC & NS effects Param values need to be
1390 updated if module ids are same*/
1391
1392 if ((effect_type == EFFECT_AEC) || (effect_type == EFFECT_NS)) {
1393 ret = update_effect_param_ecns(adev, effect_config.module_id, effect_type, &param_value);
1394 if (ret < 0)
1395 return ret;
1396 }
1397
1398 ret = send_effect_enable_disable_mixer_ctl(adev, effect_config, param_value);
1399
1400 return ret;
1401}
1402
1403static void check_and_enable_effect(struct audio_device *adev)
1404{
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001405 if(!voice_extn_is_dynamic_ecns_enabled())
1406 return;
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001407
Eric Laurent637e2d42018-11-15 12:24:31 -08001408 struct stream_in *in = adev_get_active_input(adev);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001409
Eric Laurent637e2d42018-11-15 12:24:31 -08001410 if (in != NULL && !in->standby) {
1411 if (in->enable_aec)
1412 enable_disable_effect(adev, EFFECT_AEC, true);
1413
1414 if (in->enable_ns &&
1415 in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
1416 enable_disable_effect(adev, EFFECT_NS, true);
1417 }
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001418 }
1419}
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001420
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07001421int pcm_ioctl(struct pcm *pcm, int request, ...)
1422{
1423 va_list ap;
1424 void * arg;
1425 int pcm_fd = *(int*)pcm;
1426
1427 va_start(ap, request);
1428 arg = va_arg(ap, void *);
1429 va_end(ap);
1430
1431 return ioctl(pcm_fd, request, arg);
1432}
1433
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -07001434int enable_audio_route(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001435 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001436{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001437 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001438 char mixer_path[MIXER_PATH_MAX_LENGTH];
Manish Dewangan58229382017-02-02 15:48:41 +05301439 struct stream_out *out = NULL;
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05301440 struct stream_in *in = NULL;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001441 struct listnode out_devices;
Soumya Managoli6993b762018-06-28 16:04:57 +05301442 int ret = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001443
1444 if (usecase == NULL)
1445 return -EINVAL;
1446
1447 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
1448
Carter Hsu2e429db2019-05-14 18:50:52 +08001449 if (usecase->type == PCM_CAPTURE) {
1450 struct stream_in *in = usecase->stream.in;
1451 struct audio_usecase *uinfo;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001452 snd_device = usecase->in_snd_device;
Carter Hsu2e429db2019-05-14 18:50:52 +08001453
1454 if (in) {
1455 if (in->enable_aec || in->enable_ec_port) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001456 list_init(&out_devices);
1457 update_device_list(&out_devices, AUDIO_DEVICE_OUT_SPEAKER, "", true);
Carter Hsu2e429db2019-05-14 18:50:52 +08001458 struct listnode *node;
1459 struct audio_usecase *voip_usecase = get_usecase_from_list(adev,
1460 USECASE_AUDIO_PLAYBACK_VOIP);
1461 if (voip_usecase) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001462 assign_devices(&out_devices,
1463 &voip_usecase->stream.out->device_list);
Carter Hsu2e429db2019-05-14 18:50:52 +08001464 } else if (adev->primary_output &&
1465 !adev->primary_output->standby) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001466 assign_devices(&out_devices,
1467 &adev->primary_output->device_list);
Carter Hsu2e429db2019-05-14 18:50:52 +08001468 } else {
1469 list_for_each(node, &adev->usecase_list) {
1470 uinfo = node_to_item(node, struct audio_usecase, list);
1471 if (uinfo->type != PCM_CAPTURE) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001472 assign_devices(&out_devices,
1473 &uinfo->stream.out->device_list);
Carter Hsu2e429db2019-05-14 18:50:52 +08001474 break;
1475 }
1476 }
1477 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001478
1479 platform_set_echo_reference(adev, true, &out_devices);
Carter Hsu2e429db2019-05-14 18:50:52 +08001480 in->ec_opened = true;
Zhenlin Lian4f947842022-05-14 15:50:52 +05301481 clear_devices(&out_devices);
Carter Hsu2e429db2019-05-14 18:50:52 +08001482 }
1483 }
Guodong Huf5e614d2019-06-24 18:42:03 +08001484 } else if ((usecase->type == TRANSCODE_LOOPBACK_TX) || ((usecase->type == PCM_HFP_CALL) &&
1485 ((usecase->id == USECASE_AUDIO_HFP_SCO) || (usecase->id == USECASE_AUDIO_HFP_SCO_WB)) &&
1486 (usecase->in_snd_device == SND_DEVICE_IN_VOICE_SPEAKER_MIC_HFP_MMSECNS))) {
Carter Hsu2e429db2019-05-14 18:50:52 +08001487 snd_device = usecase->in_snd_device;
1488 } else {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001489 snd_device = usecase->out_snd_device;
Carter Hsu2e429db2019-05-14 18:50:52 +08001490 }
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001491
Kunlei Zhanga3c4cb02021-03-05 16:03:54 +08001492 if (usecase->type == PCM_CAPTURE) {
1493 if (platform_get_fluence_nn_state(adev->platform) == 0) {
1494 platform_set_fluence_nn_state(adev->platform, true);
1495 ALOGD("%s: set fluence nn capture state", __func__);
1496 }
1497 }
1498
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08001499#ifdef DS1_DOLBY_DAP_ENABLED
1500 audio_extn_dolby_set_dmid(adev);
1501 audio_extn_dolby_set_endpoint(adev);
1502#endif
Pradnya Chaphekar8a9dcd82014-09-09 09:49:10 -07001503 audio_extn_dolby_ds2_set_endpoint(adev);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001504 audio_extn_sound_trigger_update_stream_status(usecase, ST_EVENT_STREAM_BUSY);
Dhananjay Kumar45b71742014-05-29 21:47:27 +05301505 audio_extn_listen_update_stream_status(usecase, LISTEN_EVENT_STREAM_BUSY);
Ben Romberger1fafdde2015-09-09 19:43:15 -07001506 audio_extn_utils_send_app_type_cfg(adev, usecase);
Jasmine Cha4dcc1092019-03-04 18:12:47 +08001507 if (audio_extn_is_maxx_audio_enabled())
1508 audio_extn_ma_set_device(usecase);
Dhananjay Kumar14170dd2015-08-28 13:24:16 +05301509 audio_extn_utils_send_audio_calibration(adev, usecase);
Zhou Songbaddf9f2020-11-20 13:57:39 +08001510 if ((usecase->type == PCM_PLAYBACK) && is_offload_usecase(usecase->id)) {
1511 out = usecase->stream.out;
1512 if (out && out->compr)
Manish Dewangan58229382017-02-02 15:48:41 +05301513 audio_extn_utils_compress_set_clk_rec_mode(usecase);
1514 }
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05301515
1516 if (usecase->type == PCM_CAPTURE) {
Kunlei Zhanga3c4cb02021-03-05 16:03:54 +08001517 if (platform_get_fluence_nn_state(adev->platform) == 1 &&
1518 adev->fluence_nn_usecase_id == USECASE_INVALID ) {
1519 adev->fluence_nn_usecase_id = usecase->id;
1520 ALOGD("%s: assign fluence nn usecase %d", __func__, usecase->id);
1521 }
1522 }
1523
1524 if (usecase->type == PCM_CAPTURE) {
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05301525 in = usecase->stream.in;
Avinash Chandrad7296d42021-08-04 15:07:47 +05301526 if ((in && is_loopback_input_device(get_device_types(&in->device_list))) ||
1527 (in && is_combo_audio_input_device(&in->device_list)) ||
1528 (in && ((compare_device_type(&in->device_list, AUDIO_DEVICE_IN_BUILTIN_MIC) ||
1529 compare_device_type(&in->device_list, AUDIO_DEVICE_IN_LINE)) &&
1530 (snd_device == SND_DEVICE_IN_HANDSET_GENERIC_6MIC)))) {
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05301531 ALOGD("%s: set custom mtmx params v1", __func__);
1532 audio_extn_set_custom_mtmx_params_v1(adev, usecase, true);
1533 }
1534 } else {
1535 audio_extn_set_custom_mtmx_params_v2(adev, usecase, true);
1536 }
Manish Dewangan58229382017-02-02 15:48:41 +05301537
Andy Hung756ecc12018-10-19 17:47:12 -07001538 // we shouldn't truncate mixer_path
1539 ALOGW_IF(strlcpy(mixer_path, use_case_table[usecase->id], sizeof(mixer_path))
1540 >= sizeof(mixer_path), "%s: truncation on mixer path", __func__);
1541 // this also appends to mixer_path
Banajit Goswami20cdd212015-09-11 01:11:30 -07001542 platform_add_backend_name(mixer_path, snd_device, usecase);
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07001543 ALOGD("%s: apply mixer and update path: %s", __func__, mixer_path);
Soumya Managoli6993b762018-06-28 16:04:57 +05301544 ret = audio_route_apply_and_update_path(adev->audio_route, mixer_path);
1545 if (!ret && usecase->id == USECASE_AUDIO_PLAYBACK_FM) {
1546 struct str_parms *parms = str_parms_create_str("fm_restore_volume=1");
1547 if (parms) {
1548 audio_extn_fm_set_parameters(adev, parms);
1549 str_parms_destroy(parms);
1550 }
1551 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001552 ALOGV("%s: exit", __func__);
1553 return 0;
1554}
1555
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001556int disable_audio_route(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001557 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001558{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001559 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001560 char mixer_path[MIXER_PATH_MAX_LENGTH];
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05301561 struct stream_in *in = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001562
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05301563 if (usecase == NULL || usecase->id == USECASE_INVALID)
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001564 return -EINVAL;
1565
1566 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Surendar Karka93cd25a2018-08-28 14:21:37 +05301567 if (usecase->type == PCM_CAPTURE || usecase->type == TRANSCODE_LOOPBACK_TX)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001568 snd_device = usecase->in_snd_device;
1569 else
1570 snd_device = usecase->out_snd_device;
Kunlei Zhange7cab1a2020-05-17 20:31:05 +08001571
1572 /* disable island and power mode on supported device for voice call */
1573 if (usecase->type == VOICE_CALL) {
1574 if (usecase->in_snd_device != SND_DEVICE_NONE) {
1575 if (platform_get_island_cfg_on_device(adev->platform, usecase->in_snd_device) &&
1576 platform_get_power_mode_on_device(adev->platform, usecase->in_snd_device)) {
1577 platform_set_island_cfg_on_device(adev, usecase->in_snd_device, false);
1578 platform_set_power_mode_on_device(adev, usecase->in_snd_device, false);
1579 platform_reset_island_power_status(adev->platform, usecase->in_snd_device);
Kunlei Zhang91c4b332020-07-20 17:53:14 +08001580 if (voice_is_lte_call_active(adev))
1581 platform_set_tx_lpi_mode(adev->platform, false);
Kunlei Zhange7cab1a2020-05-17 20:31:05 +08001582 ALOGD("%s: disable island cfg and power mode in voice tx path",
1583 __func__);
1584 }
1585 }
1586 if (usecase->out_snd_device != SND_DEVICE_NONE) {
1587 if (platform_get_island_cfg_on_device(adev->platform, usecase->out_snd_device) &&
1588 platform_get_power_mode_on_device(adev->platform, usecase->out_snd_device)) {
1589 platform_set_island_cfg_on_device(adev, usecase->out_snd_device, false);
1590 platform_set_power_mode_on_device(adev, usecase->out_snd_device, false);
1591 platform_reset_island_power_status(adev->platform, usecase->out_snd_device);
1592 ALOGD("%s: disable island cfg and power mode in voice rx path",
1593 __func__);
1594 }
1595 }
1596 }
1597
Andy Hung756ecc12018-10-19 17:47:12 -07001598 // we shouldn't truncate mixer_path
1599 ALOGW_IF(strlcpy(mixer_path, use_case_table[usecase->id], sizeof(mixer_path))
1600 >= sizeof(mixer_path), "%s: truncation on mixer path", __func__);
1601 // this also appends to mixer_path
Banajit Goswami20cdd212015-09-11 01:11:30 -07001602 platform_add_backend_name(mixer_path, snd_device, usecase);
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07001603 ALOGD("%s: reset and update mixer path: %s", __func__, mixer_path);
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07001604 audio_route_reset_and_update_path(adev->audio_route, mixer_path);
Carter Hsu2e429db2019-05-14 18:50:52 +08001605 if (usecase->type == PCM_CAPTURE) {
1606 struct stream_in *in = usecase->stream.in;
1607 if (in && in->ec_opened) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001608 struct listnode out_devices;
1609 list_init(&out_devices);
1610 platform_set_echo_reference(in->dev, false, &out_devices);
Carter Hsu2e429db2019-05-14 18:50:52 +08001611 in->ec_opened = false;
Zhenlin Lian4f947842022-05-14 15:50:52 +05301612 clear_devices(&out_devices);
Carter Hsu2e429db2019-05-14 18:50:52 +08001613 }
1614 }
Kunlei Zhanga3c4cb02021-03-05 16:03:54 +08001615 if (usecase->id == adev->fluence_nn_usecase_id) {
1616 platform_set_fluence_nn_state(adev->platform, false);
1617 adev->fluence_nn_usecase_id = USECASE_INVALID;
1618 ALOGD("%s: reset fluence nn capture state", __func__);
1619 }
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001620 audio_extn_sound_trigger_update_stream_status(usecase, ST_EVENT_STREAM_FREE);
Dhananjay Kumar45b71742014-05-29 21:47:27 +05301621 audio_extn_listen_update_stream_status(usecase, LISTEN_EVENT_STREAM_FREE);
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05301622
1623 if (usecase->type == PCM_CAPTURE) {
1624 in = usecase->stream.in;
Avinash Chandrad7296d42021-08-04 15:07:47 +05301625 if ((in && is_loopback_input_device(get_device_types(&in->device_list))) ||
1626 (in && is_combo_audio_input_device(&in->device_list)) ||
1627 (in && ((compare_device_type(&in->device_list, AUDIO_DEVICE_IN_BUILTIN_MIC) ||
1628 compare_device_type(&in->device_list, AUDIO_DEVICE_IN_LINE)) &&
1629 (snd_device == SND_DEVICE_IN_HANDSET_GENERIC_6MIC)))){
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05301630 ALOGD("%s: reset custom mtmx params v1", __func__);
1631 audio_extn_set_custom_mtmx_params_v1(adev, usecase, false);
1632 }
1633 } else {
1634 audio_extn_set_custom_mtmx_params_v2(adev, usecase, false);
1635 }
1636
Weiyin Jiang298ffd92019-06-03 14:29:30 +08001637 if ((usecase->type == PCM_PLAYBACK) &&
1638 (usecase->stream.out != NULL))
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +05301639 usecase->stream.out->pspd_coeff_sent = false;
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05301640
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001641 ALOGV("%s: exit", __func__);
1642 return 0;
1643}
1644
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -07001645int enable_snd_device(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001646 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001647{
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301648 int i, num_devices = 0;
1649 snd_device_t new_snd_devices[SND_DEVICE_OUT_END];
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001650 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
1651
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001652 if (snd_device < SND_DEVICE_MIN ||
1653 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001654 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001655 return -EINVAL;
1656 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001657
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001658 if (platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0) {
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001659 ALOGE("%s: Invalid sound device returned", __func__);
1660 return -EINVAL;
1661 }
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001662
1663 adev->snd_dev_ref_cnt[snd_device]++;
1664
1665 if ((adev->snd_dev_ref_cnt[snd_device] > 1) &&
1666 (platform_split_snd_device(adev->platform,
1667 snd_device,
1668 &num_devices,
1669 new_snd_devices) != 0)) {
Eric Laurent994a6932013-07-17 11:51:42 -07001670 ALOGV("%s: snd_device(%d: %s) is already active",
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001671 __func__, snd_device, device_name);
Aniket Kumar Lata990de552019-07-11 14:20:23 -07001672 /* Set backend config for A2DP to ensure slimbus configuration
1673 is correct if A2DP is already active and backend is closed
1674 and re-opened */
1675 if (snd_device == SND_DEVICE_OUT_BT_A2DP)
1676 audio_extn_a2dp_set_source_backend_cfg();
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001677 return 0;
1678 }
1679
Gopikrishnaiah Anandane85d0462014-06-30 21:41:20 -07001680 if (audio_extn_spkr_prot_is_enabled())
1681 audio_extn_spkr_prot_calib_cancel(adev);
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -07001682
Aalique Grahame22e49102018-12-18 14:23:57 -08001683 audio_extn_dsm_feedback_enable(adev, snd_device, true);
1684
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001685 if (platform_can_enable_spkr_prot_on_device(snd_device) &&
1686 audio_extn_spkr_prot_is_enabled()) {
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001687 if (platform_get_spkr_prot_acdb_id(snd_device) < 0) {
1688 goto err;
1689 }
1690 audio_extn_dev_arbi_acquire(snd_device);
1691 if (audio_extn_spkr_prot_start_processing(snd_device)) {
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -08001692 ALOGE("%s: spkr_start_processing failed", __func__);
Ravit Dennisaaee49c2015-02-04 21:26:22 +02001693 audio_extn_dev_arbi_release(snd_device);
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001694 goto err;
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -08001695 }
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001696 } else if (platform_split_snd_device(adev->platform,
1697 snd_device,
1698 &num_devices,
1699 new_snd_devices) == 0) {
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301700 for (i = 0; i < num_devices; i++) {
1701 enable_snd_device(adev, new_snd_devices[i]);
1702 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001703 platform_set_speaker_gain_in_combo(adev, snd_device, true);
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -08001704 } else {
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07001705 ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, device_name);
Naresh Tannirucd2353e2016-08-19 00:37:25 +05301706
Kunlei Zhange7cab1a2020-05-17 20:31:05 +08001707 /* enable island and power mode on supported device */
1708 if (platform_get_island_cfg_on_device(adev->platform, snd_device) &&
1709 platform_get_power_mode_on_device(adev->platform, snd_device)) {
1710 platform_set_island_cfg_on_device(adev, snd_device, true);
1711 platform_set_power_mode_on_device(adev, snd_device, true);
Kunlei Zhang91c4b332020-07-20 17:53:14 +08001712 if (voice_is_lte_call_active(adev) &&
1713 (snd_device >= SND_DEVICE_IN_BEGIN &&
1714 snd_device < SND_DEVICE_IN_END))
1715 platform_set_tx_lpi_mode(adev->platform, true);
Kunlei Zhange7cab1a2020-05-17 20:31:05 +08001716 ALOGD("%s: enable island cfg and power mode on: %s",
1717 __func__, device_name);
1718 }
Preetam Singh Ranawatf1d417c2017-01-10 17:00:32 +05301719
Revathi Uddarajub26e3932020-06-10 14:51:02 +05301720 if (SND_DEVICE_OUT_BT_A2DP == snd_device) {
Ramjee Singh2d2944a2021-12-14 14:13:41 +05301721
1722 struct audio_usecase *usecase;
1723 struct listnode *node;
1724 /* Disable SCO Devices and enable handset mic for active input stream */
1725 list_for_each(node, &adev->usecase_list) {
1726 usecase = node_to_item(node, struct audio_usecase, list);
1727 if (usecase->stream.in && (usecase->type == PCM_CAPTURE) &&
1728 is_sco_in_device_type(&usecase->stream.in->device_list)) {
1729 ALOGD("a2dp resumed, switch bt sco mic to handset mic");
1730 reassign_device_list(&usecase->stream.in->device_list,
1731 AUDIO_DEVICE_IN_BUILTIN_MIC, "");
1732 select_devices(adev, usecase->id);
1733 }
1734 }
Revathi Uddarajub26e3932020-06-10 14:51:02 +05301735 if (audio_extn_a2dp_start_playback() < 0) {
1736 ALOGE(" fail to configure A2dp Source control path ");
1737 goto err;
1738 } else {
1739 adev->a2dp_started = true;
1740 }
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001741 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001742
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001743 if ((SND_DEVICE_IN_BT_A2DP == snd_device) &&
1744 (audio_extn_a2dp_start_capture() < 0)) {
1745 ALOGE(" fail to configure A2dp Sink control path ");
1746 goto err;
1747 }
Naresh Tannirucd2353e2016-08-19 00:37:25 +05301748
Mingshu Pang7be5b1d2020-03-04 15:24:38 +08001749 if ((SND_DEVICE_OUT_BT_SCO_SWB == snd_device) ||
1750 (SND_DEVICE_IN_BT_SCO_MIC_SWB_NREC == snd_device) ||
1751 (SND_DEVICE_IN_BT_SCO_MIC_SWB == snd_device)) {
1752 if (!adev->bt_sco_on || (audio_extn_sco_start_configuration() < 0)) {
1753 ALOGE(" fail to configure sco control path ");
1754 goto err;
1755 }
Zhou Song12c29502019-03-16 10:37:18 +08001756 }
1757
Zhou Song331c8e52019-08-26 14:16:12 +08001758 configure_btsco_sample_rate(snd_device);
Bharath Ramachandramurthy0de16782014-03-28 21:34:33 -07001759 /* due to the possibility of calibration overwrite between listen
1760 and audio, notify listen hal before audio calibration is sent */
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001761 audio_extn_sound_trigger_update_device_status(snd_device,
1762 ST_EVENT_SND_DEVICE_BUSY);
Dhananjay Kumar45b71742014-05-29 21:47:27 +05301763 audio_extn_listen_update_device_status(snd_device,
1764 LISTEN_EVENT_SND_DEVICE_BUSY);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001765 if (platform_get_snd_device_acdb_id(snd_device) < 0) {
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001766 audio_extn_sound_trigger_update_device_status(snd_device,
1767 ST_EVENT_SND_DEVICE_FREE);
Dhananjay Kumar45b71742014-05-29 21:47:27 +05301768 audio_extn_listen_update_device_status(snd_device,
1769 LISTEN_EVENT_SND_DEVICE_FREE);
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001770 goto err;
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -07001771 }
Lior Barenboim0b61bc72014-05-13 13:01:37 +03001772 audio_extn_dev_arbi_acquire(snd_device);
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07001773 audio_route_apply_and_update_path(adev->audio_route, device_name);
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301774
1775 if (SND_DEVICE_OUT_HEADPHONES == snd_device &&
1776 !adev->native_playback_enabled &&
1777 audio_is_true_native_stream_active(adev)) {
1778 ALOGD("%s: %d: napb: enabling native mode in hardware",
1779 __func__, __LINE__);
1780 audio_route_apply_and_update_path(adev->audio_route,
1781 "true-native-mode");
1782 adev->native_playback_enabled = true;
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +05301783 }
Dhanalakshmi Siddani10621622018-04-30 15:07:27 +05301784 if (((snd_device == SND_DEVICE_IN_HANDSET_6MIC) ||
1785 (snd_device == SND_DEVICE_IN_HANDSET_QMIC)) &&
Eric Laurent637e2d42018-11-15 12:24:31 -08001786 (audio_extn_ffv_get_stream() == adev_get_active_input(adev))) {
Garmond Leunge2433c32017-09-28 21:51:22 -07001787 ALOGD("%s: init ec ref loopback", __func__);
1788 audio_extn_ffv_init_ec_ref_loopback(adev, snd_device);
1789 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001790 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001791 return 0;
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001792err:
1793 adev->snd_dev_ref_cnt[snd_device]--;
1794 return -EINVAL;;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001795}
1796
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001797int disable_snd_device(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001798 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001799{
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301800 int i, num_devices = 0;
1801 snd_device_t new_snd_devices[SND_DEVICE_OUT_END];
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001802 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
1803
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001804 if (snd_device < SND_DEVICE_MIN ||
1805 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001806 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001807 return -EINVAL;
1808 }
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001809
1810 if (platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0) {
1811 ALOGE("%s: Invalid sound device returned", __func__);
1812 return -EINVAL;
1813 }
1814
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001815 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
1816 ALOGE("%s: device ref cnt is already 0", __func__);
1817 return -EINVAL;
1818 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001819
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001820 adev->snd_dev_ref_cnt[snd_device]--;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -07001821
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001822
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001823 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07001824 ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, device_name);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301825
Aalique Grahame22e49102018-12-18 14:23:57 -08001826 audio_extn_dsm_feedback_enable(adev, snd_device, false);
1827
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001828 if (platform_can_enable_spkr_prot_on_device(snd_device) &&
1829 audio_extn_spkr_prot_is_enabled()) {
Anish Kumar46c7b872014-09-09 01:49:44 -07001830 audio_extn_spkr_prot_stop_processing(snd_device);
Vignesh Kulothungan3b5fae52017-09-25 12:16:30 -07001831
1832 // when speaker device is disabled, reset swap.
1833 // will be renabled on usecase start
1834 platform_set_swap_channels(adev, false);
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001835 } else if (platform_split_snd_device(adev->platform,
1836 snd_device,
1837 &num_devices,
1838 new_snd_devices) == 0) {
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301839 for (i = 0; i < num_devices; i++) {
1840 disable_snd_device(adev, new_snd_devices[i]);
1841 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001842 platform_set_speaker_gain_in_combo(adev, snd_device, false);
Lior Barenboim0b61bc72014-05-13 13:01:37 +03001843 } else {
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07001844 audio_route_reset_and_update_path(adev->audio_route, device_name);
Lior Barenboim0b61bc72014-05-13 13:01:37 +03001845 }
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001846
Revathi Uddarajub26e3932020-06-10 14:51:02 +05301847 if (snd_device == SND_DEVICE_OUT_BT_A2DP) {
Naresh Tannirucd2353e2016-08-19 00:37:25 +05301848 audio_extn_a2dp_stop_playback();
Revathi Uddarajub26e3932020-06-10 14:51:02 +05301849 adev->a2dp_started = false;
1850 } else if (snd_device == SND_DEVICE_IN_BT_A2DP)
Florian Pfister1a84f312018-07-19 14:38:18 +02001851 audio_extn_a2dp_stop_capture();
Zhou Songd6d71752019-05-21 18:08:51 +08001852 else if ((snd_device == SND_DEVICE_OUT_HDMI) ||
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001853 (snd_device == SND_DEVICE_OUT_DISPLAY_PORT))
Ashish Jain81eb2a82015-05-13 10:52:34 +05301854 adev->is_channel_status_set = false;
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001855 else if ((snd_device == SND_DEVICE_OUT_HEADPHONES) &&
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301856 adev->native_playback_enabled) {
1857 ALOGD("%s: %d: napb: disabling native mode in hardware",
1858 __func__, __LINE__);
1859 audio_route_reset_and_update_path(adev->audio_route,
1860 "true-native-mode");
1861 adev->native_playback_enabled = false;
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001862 } else if ((snd_device == SND_DEVICE_OUT_HEADPHONES) &&
Preetam Singh Ranawatb0c0dd72016-08-18 00:32:06 +05301863 adev->asrc_mode_enabled) {
1864 ALOGD("%s: %d: disabling asrc mode in hardware", __func__, __LINE__);
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +05301865 disable_asrc_mode(adev);
1866 audio_route_apply_and_update_path(adev->audio_route, "hph-lowpower-mode");
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001867 } else if (((snd_device == SND_DEVICE_IN_HANDSET_6MIC) ||
Dhanalakshmi Siddaniaf4bd622019-02-27 16:28:06 +05301868 (snd_device == SND_DEVICE_IN_HANDSET_QMIC)) &&
Eric Laurent637e2d42018-11-15 12:24:31 -08001869 (audio_extn_ffv_get_stream() == adev_get_active_input(adev))) {
Garmond Leunge2433c32017-09-28 21:51:22 -07001870 ALOGD("%s: deinit ec ref loopback", __func__);
1871 audio_extn_ffv_deinit_ec_ref_loopback(adev, snd_device);
1872 }
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001873
1874 audio_extn_utils_release_snd_device(snd_device);
1875 } else {
1876 if (platform_split_snd_device(adev->platform,
1877 snd_device,
1878 &num_devices,
1879 new_snd_devices) == 0) {
1880 for (i = 0; i < num_devices; i++) {
1881 adev->snd_dev_ref_cnt[new_snd_devices[i]]--;
1882 }
1883 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001884 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001885
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001886 return 0;
1887}
1888
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001889/*
1890 legend:
1891 uc - existing usecase
1892 new_uc - new usecase
1893 d1, d11, d2 - SND_DEVICE enums
1894 a1, a2 - corresponding ANDROID device enums
1895 B1, B2 - backend strings
1896
1897case 1
1898 uc->dev d1 (a1) B1
1899 new_uc->dev d1 (a1), d2 (a2) B1, B2
1900
1901 resolution: disable and enable uc->dev on d1
1902
1903case 2
1904 uc->dev d1 (a1) B1
1905 new_uc->dev d11 (a1) B1
1906
1907 resolution: need to switch uc since d1 and d11 are related
1908 (e.g. speaker and voice-speaker)
1909 use ANDROID_DEVICE_OUT enums to match devices since SND_DEVICE enums may vary
1910
1911case 3
1912 uc->dev d1 (a1) B1
1913 new_uc->dev d2 (a2) B2
1914
1915 resolution: no need to switch uc
1916
1917case 4
1918 uc->dev d1 (a1) B1
1919 new_uc->dev d2 (a2) B1
1920
1921 resolution: disable enable uc-dev on d2 since backends match
1922 we cannot enable two streams on two different devices if they
1923 share the same backend. e.g. if offload is on speaker device using
1924 QUAD_MI2S backend and a low-latency stream is started on voice-handset
1925 using the same backend, offload must also be switched to voice-handset.
1926
1927case 5
1928 uc->dev d1 (a1) B1
1929 new_uc->dev d1 (a1), d2 (a2) B1
1930
1931 resolution: disable enable uc-dev on d2 since backends match
1932 we cannot enable two streams on two different devices if they
1933 share the same backend.
1934
1935case 6
1936 uc->dev d1 (a1) B1
1937 new_uc->dev d2 (a1) B2
1938
1939 resolution: no need to switch
1940
1941case 7
1942 uc->dev d1 (a1), d2 (a2) B1, B2
1943 new_uc->dev d1 (a1) B1
1944
1945 resolution: no need to switch
1946
Zhou Song4ba65882018-07-09 14:48:07 +08001947case 8
1948 uc->dev d1 (a1) B1
1949 new_uc->dev d11 (a1), d2 (a2) B1, B2
1950 resolution: compared to case 1, for this case, d1 and d11 are related
1951 then need to do the same as case 2 to siwtch to new uc
Preetam Singh Ranawatd399e6f2020-06-25 12:20:36 +05301952
1953case 9
1954 uc->dev d1 (a1), d2(a2) B1 B2
1955 new_uc->dev d1 (a1), d22 (a2) B1, B2
1956 resolution: disable enable uc-dev on d2 since backends match
1957 we cannot enable two streams on two different devices if they
1958 share the same backend. This is special case for combo use case
1959 with a2dp and sco devices which uses same backend.
1960 e.g. speaker-a2dp and speaker-btsco
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001961*/
1962static snd_device_t derive_playback_snd_device(void * platform,
1963 struct audio_usecase *uc,
1964 struct audio_usecase *new_uc,
1965 snd_device_t new_snd_device)
1966{
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001967 struct listnode a1, a2;
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001968
1969 snd_device_t d1 = uc->out_snd_device;
1970 snd_device_t d2 = new_snd_device;
Sandhya Mutha Naga Venkataa597a962023-02-07 11:35:51 +05301971 int ret = 0;
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001972
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001973 list_init(&a1);
1974 list_init(&a2);
1975
Siddartha Shaik31b530e2017-05-19 15:26:33 +05301976 switch (uc->type) {
Surendar Karka93cd25a2018-08-28 14:21:37 +05301977 case TRANSCODE_LOOPBACK_RX :
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001978 assign_devices(&a1, &uc->stream.inout->out_config.device_list);
1979 assign_devices(&a2, &new_uc->stream.inout->out_config.device_list);
Siddartha Shaik31b530e2017-05-19 15:26:33 +05301980 break;
1981 default :
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001982 assign_devices(&a1, &uc->stream.out->device_list);
1983 assign_devices(&a2, &new_uc->stream.out->device_list);
Siddartha Shaik31b530e2017-05-19 15:26:33 +05301984 break;
1985 }
1986
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001987 // Treat as a special case when a1 and a2 are not disjoint
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001988 if (!compare_devices(&a1, &a2) &&
1989 compare_devices_for_any_match(&a1 ,&a2)) {
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001990 snd_device_t d3[2];
1991 int num_devices = 0;
Sandhya Mutha Naga Venkataa597a962023-02-07 11:35:51 +05301992 ret = platform_split_snd_device(platform,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001993 list_length(&a1) > 1 ? d1 : d2,
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001994 &num_devices,
1995 d3);
1996 if (ret < 0) {
1997 if (ret != -ENOSYS) {
1998 ALOGW("%s failed to split snd_device %d",
1999 __func__,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002000 list_length(&a1) > 1 ? d1 : d2);
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07002001 }
Sandhya Mutha Naga Venkata4c6fc9f2022-10-14 19:38:02 +05302002 ret = d2;
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07002003 goto end;
2004 }
2005
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07002006 if (platform_check_backends_match(d3[0], d3[1])) {
Sandhya Mutha Naga Venkata4c6fc9f2022-10-14 19:38:02 +05302007 ret = d2;
2008 goto end; // case 5
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07002009 } else {
Preetam Singh Ranawatd399e6f2020-06-25 12:20:36 +05302010 if ((list_length(&a1) > 1) && (list_length(&a2) > 1) &&
Sandhya Mutha Naga Venkata4c6fc9f2022-10-14 19:38:02 +05302011 platform_check_backends_match(d1, d2)) {
2012 ret = d2;
2013 goto end; //case 9
2014 }
2015 if (list_length(&a1) > 1) {
2016 ret = d1;
2017 goto end; //case 7
2018 }
Garmond Leungb9eeba42018-09-18 11:10:41 -07002019 // check if d1 is related to any of d3's
Sandhya Mutha Naga Venkata4c6fc9f2022-10-14 19:38:02 +05302020 if (d1 == d3[0] || d1 == d3[1]) {
2021 ret = d1;
2022 goto end; // case 1
2023 } else {
2024 ret = d3[1];
2025 goto end; // case 8
2026 }
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07002027 }
2028 } else {
2029 if (platform_check_backends_match(d1, d2)) {
Sandhya Mutha Naga Venkata4c6fc9f2022-10-14 19:38:02 +05302030 ret = d2;
2031 goto end; // case 2, 4
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07002032 } else {
Sandhya Mutha Naga Venkata4c6fc9f2022-10-14 19:38:02 +05302033 ret = d1;
2034 goto end; // case 6, 3
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07002035 }
2036 }
2037
2038end:
Sandhya Mutha Naga Venkata4c6fc9f2022-10-14 19:38:02 +05302039 clear_devices(&a1);
2040 clear_devices(&a2);
2041 return ret; // return whatever was calculated before.
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07002042}
2043
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002044static void check_usecases_codec_backend(struct audio_device *adev,
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05302045 struct audio_usecase *uc_info,
2046 snd_device_t snd_device)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002047{
2048 struct listnode *node;
2049 struct audio_usecase *usecase;
2050 bool switch_device[AUDIO_USECASE_MAX];
Chaithanya Krishna Bacharaju49e7db02017-03-14 11:57:26 +05302051 snd_device_t uc_derive_snd_device;
2052 snd_device_t derive_snd_device[AUDIO_USECASE_MAX];
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08002053 snd_device_t split_snd_devices[SND_DEVICE_OUT_END];
2054 int i, num_uc_to_switch = 0, num_devices = 0;
kunleiz5cd52b82016-11-07 17:22:52 +08002055 int status = 0;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302056 bool force_restart_session = false;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002057 /*
2058 * This function is to make sure that all the usecases that are active on
2059 * the hardware codec backend are always routed to any one device that is
2060 * handled by the hardware codec.
2061 * For example, if low-latency and deep-buffer usecases are currently active
2062 * on speaker and out_set_parameters(headset) is received on low-latency
2063 * output, then we have to make sure deep-buffer is also switched to headset,
2064 * because of the limitation that both the devices cannot be enabled
2065 * at the same time as they share the same backend.
2066 */
Mingming Yin3ee55c62014-08-04 14:23:35 -07002067 /*
2068 * This call is to check if we need to force routing for a particular stream
2069 * If there is a backend configuration change for the device when a
2070 * new stream starts, then ADM needs to be closed and re-opened with the new
2071 * configuraion. This call check if we need to re-route all the streams
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -08002072 * associated with the backend. Touch tone + 24 bit + native playback.
Mingming Yin3ee55c62014-08-04 14:23:35 -07002073 */
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -08002074 bool force_routing = platform_check_and_set_codec_backend_cfg(adev, uc_info,
2075 snd_device);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302076 /* For a2dp device reconfigure all active sessions
2077 * with new AFE encoder format based on a2dp state
2078 */
2079 if ((SND_DEVICE_OUT_BT_A2DP == snd_device ||
Preetam Singh Ranawatd399e6f2020-06-25 12:20:36 +05302080 SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP == snd_device ||
2081 SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP == snd_device) &&
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302082 audio_extn_a2dp_is_force_device_switch()) {
2083 force_routing = true;
2084 force_restart_session = true;
2085 }
Kunlei Zhange7cab1a2020-05-17 20:31:05 +08002086
2087 /*
2088 * Island cfg and power mode config needs to set before AFE port start.
2089 * Set force routing in case of voice device was enable before.
2090 */
2091 if (uc_info->type == VOICE_CALL &&
2092 voice_extn_is_voice_power_mode_supported() &&
Kunlei Zhang67cc7072020-12-18 17:16:49 +08002093 is_supported_conc_usecase_for_power_mode_call(adev) &&
Kunlei Zhange7cab1a2020-05-17 20:31:05 +08002094 platform_check_and_update_island_power_status(adev->platform,
2095 uc_info,
2096 snd_device)) {
2097 force_routing = true;
2098 ALOGD("%s:becf: force routing %d for power mode supported device",
2099 __func__, force_routing);
2100 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302101 ALOGD("%s:becf: force routing %d", __func__, force_routing);
2102
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002103 /* Disable all the usecases on the shared backend other than the
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -08002104 * specified usecase.
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -08002105 */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002106 for (i = 0; i < AUDIO_USECASE_MAX; i++)
2107 switch_device[i] = false;
2108
2109 list_for_each(node, &adev->usecase_list) {
2110 usecase = node_to_item(node, struct audio_usecase, list);
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -08002111
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05302112 ALOGD("%s:becf: (%d) check_usecases curr device: %s, usecase device:%s "
2113 "backends match %d",__func__, i,
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302114 platform_get_snd_device_name(snd_device),
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05302115 platform_get_snd_device_name(usecase->out_snd_device),
2116 platform_check_backends_match(snd_device, usecase->out_snd_device));
Rahul Sharma99770982019-03-06 17:05:26 +05302117 if ((usecase->type != PCM_CAPTURE) && (usecase != uc_info) &&
2118 (usecase->type != PCM_PASSTHROUGH)) {
Ashish Jain6a65b352017-03-21 17:24:40 +05302119 uc_derive_snd_device = derive_playback_snd_device(adev->platform,
2120 usecase, uc_info, snd_device);
2121 if (((uc_derive_snd_device != usecase->out_snd_device) || force_routing) &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002122 (is_codec_backend_out_device_type(&usecase->device_list) ||
2123 compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_AUX_DIGITAL) ||
2124 compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_USB_DEVICE) ||
2125 compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_USB_HEADSET) ||
2126 is_a2dp_out_device_type(&usecase->device_list) ||
2127 is_sco_out_device_type(&usecase->device_list)) &&
Ashish Jain6a65b352017-03-21 17:24:40 +05302128 ((force_restart_session) ||
2129 (platform_check_backends_match(snd_device, usecase->out_snd_device)))) {
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05302130 ALOGD("%s:becf: check_usecases (%s) is active on (%s) - disabling ..",
2131 __func__, use_case_table[usecase->id],
2132 platform_get_snd_device_name(usecase->out_snd_device));
2133 disable_audio_route(adev, usecase);
2134 switch_device[usecase->id] = true;
Chaithanya Krishna Bacharaju49e7db02017-03-14 11:57:26 +05302135 /* Enable existing usecase on derived playback device */
2136 derive_snd_device[usecase->id] = uc_derive_snd_device;
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05302137 num_uc_to_switch++;
Ashish Jain6a65b352017-03-21 17:24:40 +05302138 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002139 }
2140 }
2141
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302142 ALOGD("%s:becf: check_usecases num.of Usecases to switch %d", __func__,
2143 num_uc_to_switch);
2144
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002145 if (num_uc_to_switch) {
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07002146 /* All streams have been de-routed. Disable the device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002147
Venkata Narendra Kumar Gutta7610e632014-04-14 23:16:38 +05302148 /* Make sure the previous devices to be disabled first and then enable the
2149 selected devices */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002150 list_for_each(node, &adev->usecase_list) {
2151 usecase = node_to_item(node, struct audio_usecase, list);
2152 if (switch_device[usecase->id]) {
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08002153 /* Check if output sound device to be switched can be split and if any
2154 of the split devices match with derived sound device */
2155 if (platform_split_snd_device(adev->platform, usecase->out_snd_device,
2156 &num_devices, split_snd_devices) == 0) {
2157 adev->snd_dev_ref_cnt[usecase->out_snd_device]--;
2158 for (i = 0; i < num_devices; i++) {
2159 /* Disable devices that do not match with derived sound device */
2160 if (split_snd_devices[i] != derive_snd_device[usecase->id])
2161 disable_snd_device(adev, split_snd_devices[i]);
2162 }
2163 } else {
2164 disable_snd_device(adev, usecase->out_snd_device);
2165 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002166 }
2167 }
2168
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -07002169 list_for_each(node, &adev->usecase_list) {
2170 usecase = node_to_item(node, struct audio_usecase, list);
2171 if (switch_device[usecase->id]) {
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08002172 if (platform_split_snd_device(adev->platform, usecase->out_snd_device,
2173 &num_devices, split_snd_devices) == 0) {
2174 /* Enable derived sound device only if it does not match with
2175 one of the split sound devices. This is because the matching
2176 sound device was not disabled */
2177 bool should_enable = true;
2178 for (i = 0; i < num_devices; i++) {
2179 if (derive_snd_device[usecase->id] == split_snd_devices[i]) {
2180 should_enable = false;
2181 break;
2182 }
2183 }
2184 if (should_enable)
2185 enable_snd_device(adev, derive_snd_device[usecase->id]);
2186 } else {
2187 enable_snd_device(adev, derive_snd_device[usecase->id]);
2188 }
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -07002189 }
2190 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002191
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002192 /* Re-route all the usecases on the shared backend other than the
2193 specified usecase to new snd devices */
2194 list_for_each(node, &adev->usecase_list) {
2195 usecase = node_to_item(node, struct audio_usecase, list);
Karthik Reddy Katta3abfee22016-02-23 10:55:27 +05302196 /* Update the out_snd_device only before enabling the audio route */
2197 if (switch_device[usecase->id]) {
Chaithanya Krishna Bacharaju49e7db02017-03-14 11:57:26 +05302198 usecase->out_snd_device = derive_snd_device[usecase->id];
Lakshman Chaluvarajuab7e5572020-10-28 19:29:23 +05302199 ALOGD("%s:becf: enabling usecase (%s) on (%s)", __func__,
2200 use_case_table[usecase->id],
2201 platform_get_snd_device_name(usecase->out_snd_device));
2202 /* Update voc calibration before enabling Voice/VoIP route */
2203 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL)
2204 status = platform_switch_voice_call_device_post(adev->platform,
2205 usecase->out_snd_device,
2206 platform_get_input_snd_device(
2207 adev->platform, NULL,
2208 &uc_info->device_list,
2209 usecase->type));
2210 enable_audio_route(adev, usecase);
2211 if (usecase->stream.out && usecase->id == USECASE_AUDIO_PLAYBACK_VOIP) {
2212 out_set_voip_volume(&usecase->stream.out->stream,
2213 usecase->stream.out->volume_l,
2214 usecase->stream.out->volume_r);
Karthik Reddy Katta3abfee22016-02-23 10:55:27 +05302215 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002216 }
2217 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002218 }
2219}
2220
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05302221static void check_usecases_capture_codec_backend(struct audio_device *adev,
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002222 struct audio_usecase *uc_info,
2223 snd_device_t snd_device)
2224{
2225 struct listnode *node;
2226 struct audio_usecase *usecase;
2227 bool switch_device[AUDIO_USECASE_MAX];
2228 int i, num_uc_to_switch = 0;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002229 int backend_check_cond = is_codec_backend_out_device_type(&uc_info->device_list);
kunleiz5cd52b82016-11-07 17:22:52 +08002230 int status = 0;
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002231
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05302232 bool force_routing = platform_check_and_set_capture_codec_backend_cfg(adev, uc_info,
2233 snd_device);
2234 ALOGD("%s:becf: force routing %d", __func__, force_routing);
Dhanalakshmi Siddanib678a802016-12-03 11:51:41 +05302235
2236 /*
2237 * Make sure out devices is checked against out codec backend device and
2238 * also in devices against in codec backend. Checking out device against in
2239 * codec backend or vice versa causes issues.
2240 */
2241 if (uc_info->type == PCM_CAPTURE)
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002242 backend_check_cond = is_codec_backend_in_device_type(&uc_info->device_list);
Kunlei Zhange7cab1a2020-05-17 20:31:05 +08002243
2244 /*
2245 * Island cfg and power mode config needs to set before AFE port start.
2246 * Set force routing in case of voice device was enable before.
2247 */
2248
2249 if (uc_info->type == VOICE_CALL &&
2250 voice_extn_is_voice_power_mode_supported() &&
Kunlei Zhang67cc7072020-12-18 17:16:49 +08002251 is_supported_conc_usecase_for_power_mode_call(adev) &&
Kunlei Zhange7cab1a2020-05-17 20:31:05 +08002252 platform_check_and_update_island_power_status(adev->platform,
2253 uc_info,
2254 snd_device)) {
2255 force_routing = true;
2256 ALOGD("%s:becf: force routing %d for power mode supported device",
2257 __func__, force_routing);
2258 }
2259
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002260 /*
2261 * This function is to make sure that all the active capture usecases
2262 * are always routed to the same input sound device.
2263 * For example, if audio-record and voice-call usecases are currently
2264 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
2265 * is received for voice call then we have to make sure that audio-record
2266 * usecase is also switched to earpiece i.e. voice-dmic-ef,
2267 * because of the limitation that two devices cannot be enabled
2268 * at the same time if they share the same backend.
2269 */
2270 for (i = 0; i < AUDIO_USECASE_MAX; i++)
2271 switch_device[i] = false;
2272
2273 list_for_each(node, &adev->usecase_list) {
2274 usecase = node_to_item(node, struct audio_usecase, list);
Dhanalakshmi Siddanib678a802016-12-03 11:51:41 +05302275 /*
2276 * TODO: Enhance below condition to handle BT sco/USB multi recording
2277 */
Jaideep Sharma477917f2020-03-13 18:13:33 +05302278
2279 bool capture_uc_needs_routing = usecase->type != PCM_PLAYBACK && (usecase != uc_info &&
2280 (usecase->in_snd_device != snd_device || force_routing));
2281 bool call_proxy_snd_device = platform_is_call_proxy_snd_device(snd_device) ||
2282 platform_is_call_proxy_snd_device(usecase->in_snd_device);
2283 if (capture_uc_needs_routing && !call_proxy_snd_device &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002284 ((backend_check_cond &&
2285 (is_codec_backend_in_device_type(&usecase->device_list) ||
Aniket Kumar Lata9c2fd892020-01-22 22:20:00 -08002286 (usecase->type == VOIP_CALL))) ||
Carter Hsu1d2a0532018-10-04 09:24:36 +08002287 ((uc_info->type == VOICE_CALL &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002288 is_single_device_type_equal(&usecase->device_list,
2289 AUDIO_DEVICE_IN_VOICE_CALL)) ||
Anver sadhique4fdc6992022-01-21 12:26:28 +05302290 platform_check_all_backends_match(snd_device,\
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002291 usecase->in_snd_device))) &&
Anish Kumara020a7c2014-10-17 11:13:22 -07002292 (usecase->id != USECASE_AUDIO_SPKR_CALIB_TX)) {
Jaideep Sharma477917f2020-03-13 18:13:33 +05302293 ALOGD("%s: Usecase (%s) is active on (%s) - disabling ..",
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002294 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -07002295 platform_get_snd_device_name(usecase->in_snd_device));
Haynes Mathew George1376ca62014-04-24 11:55:48 -07002296 disable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002297 switch_device[usecase->id] = true;
2298 num_uc_to_switch++;
2299 }
2300 }
2301
2302 if (num_uc_to_switch) {
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07002303 /* All streams have been de-routed. Disable the device */
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002304
Venkata Narendra Kumar Gutta7610e632014-04-14 23:16:38 +05302305 /* Make sure the previous devices to be disabled first and then enable the
2306 selected devices */
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002307 list_for_each(node, &adev->usecase_list) {
2308 usecase = node_to_item(node, struct audio_usecase, list);
2309 if (switch_device[usecase->id]) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07002310 disable_snd_device(adev, usecase->in_snd_device);
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -08002311 }
2312 }
2313
2314 list_for_each(node, &adev->usecase_list) {
2315 usecase = node_to_item(node, struct audio_usecase, list);
2316 if (switch_device[usecase->id]) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07002317 enable_snd_device(adev, snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002318 }
2319 }
2320
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002321 /* Re-route all the usecases on the shared backend other than the
2322 specified usecase to new snd devices */
2323 list_for_each(node, &adev->usecase_list) {
2324 usecase = node_to_item(node, struct audio_usecase, list);
2325 /* Update the in_snd_device only before enabling the audio route */
2326 if (switch_device[usecase->id] ) {
2327 usecase->in_snd_device = snd_device;
Lakshman Chaluvarajuab7e5572020-10-28 19:29:23 +05302328 /* Update voc calibration before enabling Voice/VoIP route */
2329 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) {
2330 snd_device_t voip_snd_device;
2331 voip_snd_device = platform_get_output_snd_device(adev->platform,
2332 usecase->stream.out,
2333 usecase->type);
2334 status = platform_switch_voice_call_device_post(adev->platform,
2335 voip_snd_device,
2336 usecase->in_snd_device);
kunleiz5cd52b82016-11-07 17:22:52 +08002337 }
Lakshman Chaluvarajuab7e5572020-10-28 19:29:23 +05302338 enable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002339 }
2340 }
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002341 }
2342}
2343
Mingming Yin3a941d42016-02-17 18:08:05 -08002344static void reset_hdmi_sink_caps(struct stream_out *out) {
2345 int i = 0;
2346
2347 for (i = 0; i<= MAX_SUPPORTED_CHANNEL_MASKS; i++) {
2348 out->supported_channel_masks[i] = 0;
2349 }
2350 for (i = 0; i<= MAX_SUPPORTED_FORMATS; i++) {
2351 out->supported_formats[i] = 0;
2352 }
2353 for (i = 0; i<= MAX_SUPPORTED_SAMPLE_RATES; i++) {
2354 out->supported_sample_rates[i] = 0;
2355 }
2356}
2357
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002358/* must be called with hw device mutex locked */
Mingming Yin3a941d42016-02-17 18:08:05 -08002359static int read_hdmi_sink_caps(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002360{
Mingming Yin3a941d42016-02-17 18:08:05 -08002361 int ret = 0, i = 0, j = 0;
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07002362 int channels = platform_edid_get_max_channels_v2(out->dev->platform,
2363 out->extconn.cs.controller,
2364 out->extconn.cs.stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002365
Mingming Yin3a941d42016-02-17 18:08:05 -08002366 reset_hdmi_sink_caps(out);
2367
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07002368 /* Cache ext disp type */
Vignesh Kulothungan1b3957e2019-12-02 16:16:51 -08002369 ret = platform_get_ext_disp_type_v2(adev->platform,
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07002370 out->extconn.cs.controller,
Vignesh Kulothungan1b3957e2019-12-02 16:16:51 -08002371 out->extconn.cs.stream);
2372 if(ret < 0) {
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07002373 ALOGE("%s: Failed to query disp type, ret:%d", __func__, ret);
Garmond Leung37850ab2016-10-06 11:42:18 -07002374 return -EINVAL;
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07002375 }
2376
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002377 switch (channels) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002378 case 8:
Mingming Yin3a941d42016-02-17 18:08:05 -08002379 ALOGV("%s: HDMI supports 7.1 channels", __func__);
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -07002380 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_7POINT1;
Mingming Yin3a941d42016-02-17 18:08:05 -08002381 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_6POINT1;
2382 case 6:
2383 ALOGV("%s: HDMI supports 5.1 channels", __func__);
2384 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_5POINT1;
2385 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_PENTA;
2386 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_QUAD;
2387 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_SURROUND;
2388 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_2POINT1;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002389 break;
2390 default:
Mingming Yin3a941d42016-02-17 18:08:05 -08002391 ALOGE("invalid/nonstandard channal count[%d]",channels);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002392 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002393 break;
2394 }
Mingming Yin3a941d42016-02-17 18:08:05 -08002395
2396 // check channel format caps
2397 i = 0;
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07002398 if (platform_is_edid_supported_format_v2(out->dev->platform, AUDIO_FORMAT_AC3,
2399 out->extconn.cs.controller,
2400 out->extconn.cs.stream)) {
Mingming Yin3a941d42016-02-17 18:08:05 -08002401 ALOGV(":%s HDMI supports AC3/EAC3 formats", __func__);
2402 out->supported_formats[i++] = AUDIO_FORMAT_AC3;
2403 //Adding EAC3/EAC3_JOC formats if AC3 is supported by the sink.
2404 //EAC3/EAC3_JOC will be converted to AC3 for decoding if needed
2405 out->supported_formats[i++] = AUDIO_FORMAT_E_AC3;
2406 out->supported_formats[i++] = AUDIO_FORMAT_E_AC3_JOC;
2407 }
2408
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07002409 if (platform_is_edid_supported_format_v2(out->dev->platform, AUDIO_FORMAT_DOLBY_TRUEHD,
2410 out->extconn.cs.controller,
2411 out->extconn.cs.stream)) {
Ben Romberger1aaaf862017-04-06 17:49:46 -07002412 ALOGV(":%s HDMI supports TRUE HD format", __func__);
2413 out->supported_formats[i++] = AUDIO_FORMAT_DOLBY_TRUEHD;
2414 }
2415
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07002416 if (platform_is_edid_supported_format_v2(out->dev->platform, AUDIO_FORMAT_DTS,
2417 out->extconn.cs.controller,
2418 out->extconn.cs.stream)) {
Mingming Yin3a941d42016-02-17 18:08:05 -08002419 ALOGV(":%s HDMI supports DTS format", __func__);
2420 out->supported_formats[i++] = AUDIO_FORMAT_DTS;
2421 }
2422
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07002423 if (platform_is_edid_supported_format_v2(out->dev->platform, AUDIO_FORMAT_DTS_HD,
2424 out->extconn.cs.controller,
2425 out->extconn.cs.stream)) {
Mingming Yin3a941d42016-02-17 18:08:05 -08002426 ALOGV(":%s HDMI supports DTS HD format", __func__);
2427 out->supported_formats[i++] = AUDIO_FORMAT_DTS_HD;
2428 }
2429
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07002430 if (platform_is_edid_supported_format_v2(out->dev->platform, AUDIO_FORMAT_IEC61937,
2431 out->extconn.cs.controller,
2432 out->extconn.cs.stream)) {
Naresh Tanniru928f0862017-04-07 16:44:23 -07002433 ALOGV(":%s HDMI supports IEC61937 format", __func__);
2434 out->supported_formats[i++] = AUDIO_FORMAT_IEC61937;
2435 }
2436
Mingming Yin3a941d42016-02-17 18:08:05 -08002437
2438 // check sample rate caps
2439 i = 0;
2440 for (j = 0; j < MAX_SUPPORTED_SAMPLE_RATES; j++) {
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07002441 if (platform_is_edid_supported_sample_rate_v2(out->dev->platform, out_hdmi_sample_rates[j],
2442 out->extconn.cs.controller,
2443 out->extconn.cs.stream)) {
Mingming Yin3a941d42016-02-17 18:08:05 -08002444 ALOGV(":%s HDMI supports sample rate:%d", __func__, out_hdmi_sample_rates[j]);
2445 out->supported_sample_rates[i++] = out_hdmi_sample_rates[j];
2446 }
2447 }
2448
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002449 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002450}
2451
Haynes Mathew George484e8d22017-07-31 18:55:17 -07002452static inline ssize_t read_usb_sup_sample_rates(bool is_playback __unused,
2453 uint32_t *supported_sample_rates __unused,
2454 uint32_t max_rates __unused)
2455{
2456 ssize_t count = audio_extn_usb_get_sup_sample_rates(is_playback,
2457 supported_sample_rates,
2458 max_rates);
Ashish Jain4847e9d2017-08-17 19:16:57 +05302459 ssize_t i = 0;
2460
2461 for (i=0; i<count; i++) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07002462 ALOGV("%s %s %d", __func__, is_playback ? "P" : "C",
2463 supported_sample_rates[i]);
2464 }
2465 return count;
2466}
2467
2468static inline int read_usb_sup_channel_masks(bool is_playback,
2469 audio_channel_mask_t *supported_channel_masks,
2470 uint32_t max_masks)
2471{
2472 int channels = audio_extn_usb_get_max_channels(is_playback);
2473 int channel_count;
2474 uint32_t num_masks = 0;
2475 if (channels > MAX_HIFI_CHANNEL_COUNT)
2476 channels = MAX_HIFI_CHANNEL_COUNT;
2477
2478 if (is_playback) {
Eric Laurent68a87112019-05-01 18:07:29 -07002479 // start from 2 channels as framework currently doesn't support mono.
2480 if (channels >= FCC_2) {
2481 supported_channel_masks[num_masks++] = audio_channel_out_mask_from_count(FCC_2);
2482 }
2483 for (channel_count = FCC_2;
2484 channel_count <= channels && num_masks < max_masks;
2485 ++channel_count) {
2486 supported_channel_masks[num_masks++] =
2487 audio_channel_mask_for_index_assignment_from_count(channel_count);
2488 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07002489 } else {
vincenttewf51c94e2019-05-07 10:28:53 +08002490 // For capture we report all supported channel masks from 1 channel up.
2491 channel_count = MIN_CHANNEL_COUNT;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07002492 // audio_channel_in_mask_from_count() does the right conversion to either positional or
2493 // indexed mask
Eric Laurent68a87112019-05-01 18:07:29 -07002494 for ( ; channel_count <= channels && num_masks < max_masks; channel_count++) {
2495 audio_channel_mask_t mask = AUDIO_CHANNEL_NONE;
2496 if (channel_count <= FCC_2) {
2497 mask = audio_channel_in_mask_from_count(channel_count);
2498 supported_channel_masks[num_masks++] = mask;
2499 }
2500 const audio_channel_mask_t index_mask =
2501 audio_channel_mask_for_index_assignment_from_count(channel_count);
2502 if (mask != index_mask && num_masks < max_masks) { // ensure index mask added.
2503 supported_channel_masks[num_masks++] = index_mask;
2504 }
2505 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07002506 }
Lakshman Chaluvarajub79fafd2018-11-21 10:24:37 +05302507
vincenttewf51c94e2019-05-07 10:28:53 +08002508 for (size_t i = 0; i < num_masks; ++i) {
2509 ALOGV("%s: %s supported ch %d supported_channel_masks[%zu] %08x num_masks %d", __func__,
2510 is_playback ? "P" : "C", channels, i, supported_channel_masks[i], num_masks);
Lakshman Chaluvarajub79fafd2018-11-21 10:24:37 +05302511 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07002512 return num_masks;
2513}
2514
2515static inline int read_usb_sup_formats(bool is_playback __unused,
2516 audio_format_t *supported_formats,
2517 uint32_t max_formats __unused)
2518{
2519 int bitwidth = audio_extn_usb_get_max_bit_width(is_playback);
2520 switch (bitwidth) {
2521 case 24:
2522 // XXX : usb.c returns 24 for s24 and s24_le?
2523 supported_formats[0] = AUDIO_FORMAT_PCM_24_BIT_PACKED;
2524 break;
2525 case 32:
2526 supported_formats[0] = AUDIO_FORMAT_PCM_32_BIT;
2527 break;
2528 case 16:
2529 default :
2530 supported_formats[0] = AUDIO_FORMAT_PCM_16_BIT;
2531 break;
2532 }
2533 ALOGV("%s: %s supported format %d", __func__,
2534 is_playback ? "P" : "C", bitwidth);
2535 return 1;
2536}
2537
2538static inline int read_usb_sup_params_and_compare(bool is_playback,
2539 audio_format_t *format,
2540 audio_format_t *supported_formats,
2541 uint32_t max_formats,
2542 audio_channel_mask_t *mask,
2543 audio_channel_mask_t *supported_channel_masks,
2544 uint32_t max_masks,
2545 uint32_t *rate,
2546 uint32_t *supported_sample_rates,
2547 uint32_t max_rates) {
2548 int ret = 0;
2549 int num_formats;
2550 int num_masks;
2551 int num_rates;
2552 int i;
2553
2554 num_formats = read_usb_sup_formats(is_playback, supported_formats,
2555 max_formats);
2556 num_masks = read_usb_sup_channel_masks(is_playback, supported_channel_masks,
2557 max_masks);
2558
2559 num_rates = read_usb_sup_sample_rates(is_playback,
2560 supported_sample_rates, max_rates);
2561
2562#define LUT(table, len, what, dflt) \
2563 for (i=0; i<len && (table[i] != what); i++); \
2564 if (i==len) { ret |= (what == dflt ? 0 : -1); what=table[0]; }
2565
2566 LUT(supported_formats, num_formats, *format, AUDIO_FORMAT_DEFAULT);
2567 LUT(supported_channel_masks, num_masks, *mask, AUDIO_CHANNEL_NONE);
2568 LUT(supported_sample_rates, num_rates, *rate, 0);
2569
2570#undef LUT
2571 return ret < 0 ? -EINVAL : 0; // HACK TBD
2572}
2573
Alexy Josephb1379942016-01-29 15:49:38 -08002574audio_usecase_t get_usecase_id_from_usecase_type(const struct audio_device *adev,
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -08002575 usecase_type_t type)
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002576{
2577 struct audio_usecase *usecase;
2578 struct listnode *node;
2579
2580 list_for_each(node, &adev->usecase_list) {
2581 usecase = node_to_item(node, struct audio_usecase, list);
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -08002582 if (usecase->type == type) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002583 ALOGV("%s: usecase id %d", __func__, usecase->id);
2584 return usecase->id;
2585 }
2586 }
2587 return USECASE_INVALID;
2588}
2589
Alexy Josephb1379942016-01-29 15:49:38 -08002590struct audio_usecase *get_usecase_from_list(const struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002591 audio_usecase_t uc_id)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002592{
2593 struct audio_usecase *usecase;
2594 struct listnode *node;
2595
2596 list_for_each(node, &adev->usecase_list) {
2597 usecase = node_to_item(node, struct audio_usecase, list);
2598 if (usecase->id == uc_id)
2599 return usecase;
2600 }
2601 return NULL;
2602}
2603
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302604/*
2605 * is a true native playback active
2606 */
2607bool audio_is_true_native_stream_active(struct audio_device *adev)
2608{
2609 bool active = false;
2610 int i = 0;
2611 struct listnode *node;
2612
2613 if (NATIVE_AUDIO_MODE_TRUE_44_1 != platform_get_native_support()) {
2614 ALOGV("%s:napb: not in true mode or non hdphones device",
2615 __func__);
2616 active = false;
2617 goto exit;
2618 }
2619
2620 list_for_each(node, &adev->usecase_list) {
2621 struct audio_usecase *uc;
2622 uc = node_to_item(node, struct audio_usecase, list);
2623 struct stream_out *curr_out =
2624 (struct stream_out*) uc->stream.out;
2625
2626 if (curr_out && PCM_PLAYBACK == uc->type) {
2627 ALOGD("%s:napb: (%d) (%s)id (%d) sr %d bw "
2628 "(%d) device %s", __func__, i++, use_case_table[uc->id],
2629 uc->id, curr_out->sample_rate,
2630 curr_out->bit_width,
2631 platform_get_snd_device_name(uc->out_snd_device));
2632
2633 if (is_offload_usecase(uc->id) &&
2634 (curr_out->sample_rate == OUTPUT_SAMPLING_RATE_44100)) {
2635 active = true;
2636 ALOGD("%s:napb:native stream detected", __func__);
2637 }
2638 }
2639 }
2640exit:
2641 return active;
2642}
2643
Xiaojun Sang785b5da2017-08-03 15:52:29 +08002644uint32_t adev_get_dsp_bit_width_enforce_mode()
2645{
2646 if (adev == NULL) {
2647 ALOGE("%s: adev is null. Disable DSP bit width enforce mode.\n", __func__);
2648 return 0;
2649 }
2650 return adev->dsp_bit_width_enforce_mode;
2651}
2652
2653static uint32_t adev_init_dsp_bit_width_enforce_mode(struct mixer *mixer)
2654{
2655 char value[PROPERTY_VALUE_MAX];
2656 int trial;
2657 uint32_t dsp_bit_width_enforce_mode = 0;
2658
2659 if (!mixer) {
2660 ALOGE("%s: adev mixer is null. cannot update DSP bitwidth.\n",
2661 __func__);
2662 return 0;
2663 }
2664
2665 if (property_get("persist.vendor.audio_hal.dsp_bit_width_enforce_mode",
2666 value, NULL) > 0) {
2667 trial = atoi(value);
2668 switch (trial) {
2669 case 16:
2670 dsp_bit_width_enforce_mode = 16;
2671 break;
2672 case 24:
2673 dsp_bit_width_enforce_mode = 24;
2674 break;
2675 case 32:
2676 dsp_bit_width_enforce_mode = 32;
2677 break;
2678 default:
2679 dsp_bit_width_enforce_mode = 0;
2680 ALOGD("%s Dynamic DSP bitwidth config is disabled.", __func__);
2681 break;
2682 }
2683 }
2684
2685 return dsp_bit_width_enforce_mode;
2686}
2687
2688static void audio_enable_asm_bit_width_enforce_mode(struct mixer *mixer,
2689 uint32_t enforce_mode,
2690 bool enable)
2691{
2692 struct mixer_ctl *ctl = NULL;
2693 const char *mixer_ctl_name = "ASM Bit Width";
2694 uint32_t asm_bit_width_mode = 0;
2695
2696 if (enforce_mode == 0) {
2697 ALOGD("%s: DSP bitwidth feature is disabled.", __func__);
2698 return;
2699 }
2700
2701 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
2702 if (!ctl) {
2703 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2704 __func__, mixer_ctl_name);
2705 return;
2706 }
2707
2708 if (enable)
2709 asm_bit_width_mode = enforce_mode;
2710 else
2711 asm_bit_width_mode = 0;
2712
2713 ALOGV("%s DSP bit width feature status is %d width=%d",
2714 __func__, enable, asm_bit_width_mode);
2715 if (mixer_ctl_set_value(ctl, 0, asm_bit_width_mode) < 0)
2716 ALOGE("%s: Could not set ASM biwidth %d", __func__,
2717 asm_bit_width_mode);
2718
2719 return;
2720}
2721
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05302722/*
2723 * if native DSD playback active
2724 */
2725bool audio_is_dsd_native_stream_active(struct audio_device *adev)
2726{
2727 bool active = false;
2728 struct listnode *node = NULL;
2729 struct audio_usecase *uc = NULL;
2730 struct stream_out *curr_out = NULL;
2731
2732 list_for_each(node, &adev->usecase_list) {
2733 uc = node_to_item(node, struct audio_usecase, list);
2734 curr_out = (struct stream_out*) uc->stream.out;
2735
2736 if (curr_out && PCM_PLAYBACK == uc->type &&
2737 (DSD_NATIVE_BACKEND == platform_get_backend_index(uc->out_snd_device))) {
2738 active = true;
2739 ALOGV("%s:DSD playback is active", __func__);
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05302740 break;
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05302741 }
2742 }
2743 return active;
2744}
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302745
2746static bool force_device_switch(struct audio_usecase *usecase)
2747{
2748 bool ret = false;
2749 bool is_it_true_mode = false;
2750
Zhou Song30f2c3e2018-02-08 14:02:15 +08002751 if (usecase->type == PCM_CAPTURE ||
Surendar Karka93cd25a2018-08-28 14:21:37 +05302752 usecase->type == TRANSCODE_LOOPBACK_RX ||
2753 usecase->type == TRANSCODE_LOOPBACK_TX) {
Zhou Song30f2c3e2018-02-08 14:02:15 +08002754 return false;
2755 }
2756
Aalique Grahamecbc46a22017-10-05 10:30:23 -07002757 if(usecase->stream.out == NULL) {
2758 ALOGE("%s: stream.out is NULL", __func__);
2759 return false;
2760 }
2761
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302762 if (is_offload_usecase(usecase->id) &&
Xiaojun Sang869f2012016-02-23 16:33:07 +08002763 (usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002764 (compare_device_type(&usecase->stream.out->device_list, AUDIO_DEVICE_OUT_WIRED_HEADSET) ||
2765 compare_device_type(&usecase->stream.out->device_list, AUDIO_DEVICE_OUT_WIRED_HEADPHONE))) {
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302766 is_it_true_mode = (NATIVE_AUDIO_MODE_TRUE_44_1 == platform_get_native_support()? true : false);
2767 if ((is_it_true_mode && !adev->native_playback_enabled) ||
2768 (!is_it_true_mode && adev->native_playback_enabled)){
2769 ret = true;
2770 ALOGD("napb: time to toggle native mode");
2771 }
2772 }
2773
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302774 // Force all a2dp output devices to reconfigure for proper AFE encode format
Ashish Jainc597d102016-12-12 10:31:34 +05302775 //Also handle a case where in earlier a2dp start failed as A2DP stream was
2776 //in suspended state, hence try to trigger a retry when we again get a routing request.
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002777 if(is_a2dp_out_device_type(&usecase->stream.out->device_list) &&
Ashish Jainc597d102016-12-12 10:31:34 +05302778 audio_extn_a2dp_is_force_device_switch()) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302779 ALOGD("Force a2dp device switch to update new encoder config");
2780 ret = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002781 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302782
Florian Pfister1a84f312018-07-19 14:38:18 +02002783 if (usecase->stream.out->stream_config_changed) {
Manish Dewangan671a4202017-08-18 17:30:46 +05302784 ALOGD("Force stream_config_changed to update iec61937 transmission config");
2785 return true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002786 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302787 return ret;
2788}
2789
Aalique Grahame22e49102018-12-18 14:23:57 -08002790static void stream_app_type_cfg_init(struct stream_app_type_cfg *cfg)
2791{
2792 cfg->gain[0] = cfg->gain[1] = APP_TYPE_GAIN_DEFAULT;
2793}
2794
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302795bool is_btsco_device(snd_device_t out_snd_device, snd_device_t in_snd_device)
2796{
2797 bool ret=false;
2798 if ((out_snd_device == SND_DEVICE_OUT_BT_SCO ||
Zhou Song5657f492019-08-07 11:30:39 +08002799 out_snd_device == SND_DEVICE_OUT_BT_SCO_WB ||
2800 out_snd_device == SND_DEVICE_OUT_BT_SCO_SWB) ||
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302801 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_WB_NREC ||
2802 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_WB ||
Zhou Song5657f492019-08-07 11:30:39 +08002803 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_SWB ||
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302804 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_NREC ||
Mingshu Pang16093502020-04-20 11:21:16 +08002805 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC ||
2806 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_SWB_NREC)
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302807 ret = true;
2808
2809 return ret;
2810}
2811
2812bool is_a2dp_device(snd_device_t out_snd_device)
2813{
2814 bool ret=false;
2815 if (out_snd_device == SND_DEVICE_OUT_BT_A2DP)
2816 ret = true;
2817
2818 return ret;
2819}
2820
2821bool is_bt_soc_on(struct audio_device *adev)
2822{
2823 struct mixer_ctl *ctl;
2824 char *mixer_ctl_name = "BT SOC status";
2825 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
2826 bool bt_soc_status = true;
2827 if (!ctl) {
2828 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2829 __func__, mixer_ctl_name);
2830 /*This is to ensure we dont break targets which dont have the kernel change*/
2831 return true;
2832 }
2833 bt_soc_status = mixer_ctl_get_value(ctl, 0);
2834 ALOGD("BT SOC status: %d",bt_soc_status);
2835 return bt_soc_status;
2836}
2837
Zhou Song331c8e52019-08-26 14:16:12 +08002838static int configure_btsco_sample_rate(snd_device_t snd_device)
2839{
2840 struct mixer_ctl *ctl = NULL;
2841 struct mixer_ctl *ctl_sr_rx = NULL, *ctl_sr_tx = NULL, *ctl_sr = NULL;
2842 char *rate_str = NULL;
2843 bool is_rx_dev = true;
2844
2845 if (is_btsco_device(snd_device, snd_device)) {
2846 ctl_sr_tx = mixer_get_ctl_by_name(adev->mixer, "BT SampleRate TX");
2847 ctl_sr_rx = mixer_get_ctl_by_name(adev->mixer, "BT SampleRate RX");
2848 if (!ctl_sr_tx || !ctl_sr_rx) {
2849 ctl_sr = mixer_get_ctl_by_name(adev->mixer, "BT SampleRate");
2850 if (!ctl_sr)
2851 return -ENOSYS;
2852 }
2853
2854 switch (snd_device) {
2855 case SND_DEVICE_OUT_BT_SCO:
2856 rate_str = "KHZ_8";
2857 break;
2858 case SND_DEVICE_IN_BT_SCO_MIC_NREC:
2859 case SND_DEVICE_IN_BT_SCO_MIC:
2860 rate_str = "KHZ_8";
2861 is_rx_dev = false;
2862 break;
2863 case SND_DEVICE_OUT_BT_SCO_WB:
2864 rate_str = "KHZ_16";
2865 break;
2866 case SND_DEVICE_IN_BT_SCO_MIC_WB_NREC:
2867 case SND_DEVICE_IN_BT_SCO_MIC_WB:
2868 rate_str = "KHZ_16";
2869 is_rx_dev = false;
2870 break;
2871 default:
2872 return 0;
2873 }
2874
2875 ctl = (ctl_sr == NULL) ? (is_rx_dev ? ctl_sr_rx : ctl_sr_tx) : ctl_sr;
2876 if (mixer_ctl_set_enum_by_string(ctl, rate_str) != 0)
2877 return -ENOSYS;
2878 }
2879 return 0;
2880}
2881
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302882int out_standby_l(struct audio_stream *stream);
2883
Eric Laurent637e2d42018-11-15 12:24:31 -08002884struct stream_in *adev_get_active_input(const struct audio_device *adev)
2885{
2886 struct listnode *node;
2887 struct stream_in *last_active_in = NULL;
2888
2889 /* Get last added active input.
2890 * TODO: We may use a priority mechanism to pick highest priority active source */
2891 list_for_each(node, &adev->usecase_list)
2892 {
2893 struct audio_usecase *usecase = node_to_item(node, struct audio_usecase, list);
2894 if (usecase->type == PCM_CAPTURE && usecase->stream.in != NULL)
2895 last_active_in = usecase->stream.in;
2896 }
2897
2898 return last_active_in;
2899}
2900
2901struct stream_in *get_voice_communication_input(const struct audio_device *adev)
2902{
2903 struct listnode *node;
2904
2905 /* First check active inputs with voice communication source and then
2906 * any input if audio mode is in communication */
2907 list_for_each(node, &adev->usecase_list)
2908 {
2909 struct audio_usecase *usecase = node_to_item(node, struct audio_usecase, list);
2910 if (usecase->type == PCM_CAPTURE && usecase->stream.in != NULL &&
2911 usecase->stream.in->source == AUDIO_SOURCE_VOICE_COMMUNICATION)
2912 return usecase->stream.in;
2913 }
2914 if (adev->mode == AUDIO_MODE_IN_COMMUNICATION)
2915 return adev_get_active_input(adev);
2916
2917 return NULL;
2918}
2919
Carter Hsu2e429db2019-05-14 18:50:52 +08002920/*
2921 * Aligned with policy.h
2922 */
2923static inline int source_priority(int inputSource)
2924{
2925 switch (inputSource) {
2926 case AUDIO_SOURCE_VOICE_COMMUNICATION:
2927 return 9;
2928 case AUDIO_SOURCE_CAMCORDER:
2929 return 8;
2930 case AUDIO_SOURCE_VOICE_PERFORMANCE:
2931 return 7;
2932 case AUDIO_SOURCE_UNPROCESSED:
2933 return 6;
2934 case AUDIO_SOURCE_MIC:
2935 return 5;
2936 case AUDIO_SOURCE_ECHO_REFERENCE:
2937 return 4;
2938 case AUDIO_SOURCE_FM_TUNER:
2939 return 3;
2940 case AUDIO_SOURCE_VOICE_RECOGNITION:
2941 return 2;
2942 case AUDIO_SOURCE_HOTWORD:
2943 return 1;
2944 default:
2945 break;
2946 }
2947 return 0;
2948}
2949
2950static struct stream_in *get_priority_input(struct audio_device *adev)
2951{
2952 struct listnode *node;
2953 struct audio_usecase *usecase;
2954 int last_priority = 0, priority;
2955 struct stream_in *priority_in = NULL;
2956 struct stream_in *in;
2957
2958 list_for_each(node, &adev->usecase_list) {
2959 usecase = node_to_item(node, struct audio_usecase, list);
2960 if (usecase->type == PCM_CAPTURE) {
2961 in = usecase->stream.in;
2962 if (!in)
2963 continue;
Krishna Kishor Jha0b7175d2022-07-28 22:33:46 +05302964
2965 if (USECASE_AUDIO_RECORD_FM_VIRTUAL == usecase->id)
2966 continue;
Carter Hsu2e429db2019-05-14 18:50:52 +08002967 priority = source_priority(in->source);
2968
2969 if (priority > last_priority) {
2970 last_priority = priority;
2971 priority_in = in;
2972 }
2973 }
2974 }
2975 return priority_in;
2976}
2977
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002978int select_devices(struct audio_device *adev, audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002979{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002980 snd_device_t out_snd_device = SND_DEVICE_NONE;
2981 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002982 struct audio_usecase *usecase = NULL;
2983 struct audio_usecase *vc_usecase = NULL;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002984 struct audio_usecase *voip_usecase = NULL;
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -08002985 struct audio_usecase *hfp_usecase = NULL;
Siddartha Shaik44dd7702017-06-14 12:13:25 +05302986 struct stream_out stream_out;
Vimal Puthanveed41fcff22014-01-23 15:56:53 -08002987 audio_usecase_t hfp_ucid;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002988 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002989
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302990 ALOGD("%s for use case (%s)", __func__, use_case_table[uc_id]);
2991
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002992 usecase = get_usecase_from_list(adev, uc_id);
2993 if (usecase == NULL) {
2994 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
2995 return -EINVAL;
2996 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002997
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002998 if ((usecase->type == VOICE_CALL) ||
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -08002999 (usecase->type == VOIP_CALL) ||
Derek Chena30a5f42019-12-03 11:17:09 -05003000 (usecase->type == PCM_HFP_CALL)||
Fei Tongaffdf732020-02-20 20:39:05 +08003001 (usecase->type == ICC_CALL) ||
3002 (usecase->type == SYNTH_LOOPBACK)) {
Aditya Bavanaribdda2f22016-10-19 15:02:05 +05303003 if(usecase->stream.out == NULL) {
3004 ALOGE("%s: stream.out is NULL", __func__);
3005 return -EINVAL;
3006 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003007 if (compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_BUS)) {
Guodong Hu267bdf82019-08-12 19:22:32 +08003008 out_snd_device = audio_extn_auto_hal_get_output_snd_device(adev,
3009 uc_id);
3010 in_snd_device = audio_extn_auto_hal_get_input_snd_device(adev,
3011 uc_id);
3012 } else {
3013 out_snd_device = platform_get_output_snd_device(adev->platform,
Jaideep Sharma477917f2020-03-13 18:13:33 +05303014 usecase->stream.out, usecase->type);
Guodong Hu267bdf82019-08-12 19:22:32 +08003015 in_snd_device = platform_get_input_snd_device(adev->platform,
3016 NULL,
Jaideep Sharma477917f2020-03-13 18:13:33 +05303017 &usecase->stream.out->device_list,
3018 usecase->type);
Guodong Hu267bdf82019-08-12 19:22:32 +08003019 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003020 assign_devices(&usecase->device_list, &usecase->stream.out->device_list);
Surendar Karka93cd25a2018-08-28 14:21:37 +05303021 } else if (usecase->type == TRANSCODE_LOOPBACK_RX) {
Siddartha Shaik31b530e2017-05-19 15:26:33 +05303022 if (usecase->stream.inout == NULL) {
3023 ALOGE("%s: stream.inout is NULL", __func__);
3024 return -EINVAL;
3025 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003026 assign_devices(&stream_out.device_list, &usecase->stream.inout->out_config.device_list);
Siddartha Shaik44dd7702017-06-14 12:13:25 +05303027 stream_out.sample_rate = usecase->stream.inout->out_config.sample_rate;
3028 stream_out.format = usecase->stream.inout->out_config.format;
3029 stream_out.channel_mask = usecase->stream.inout->out_config.channel_mask;
Jaideep Sharma477917f2020-03-13 18:13:33 +05303030 out_snd_device = platform_get_output_snd_device(adev->platform, &stream_out, usecase->type);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003031 assign_devices(&usecase->device_list,
3032 &usecase->stream.inout->out_config.device_list);
Zhenlin Lian4f947842022-05-14 15:50:52 +05303033 clear_devices(&stream_out.device_list);
Surendar Karka93cd25a2018-08-28 14:21:37 +05303034 } else if (usecase->type == TRANSCODE_LOOPBACK_TX ) {
3035 if (usecase->stream.inout == NULL) {
3036 ALOGE("%s: stream.inout is NULL", __func__);
3037 return -EINVAL;
3038 }
Manisha Agarwal03297972020-04-17 15:36:55 +05303039 struct listnode out_devices;
3040 list_init(&out_devices);
3041 in_snd_device = platform_get_input_snd_device(adev->platform, NULL,
3042 &out_devices, usecase->type);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003043 assign_devices(&usecase->device_list,
3044 &usecase->stream.inout->in_config.device_list);
Zhenlin Lian4f947842022-05-14 15:50:52 +05303045 clear_devices(&out_devices);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003046 } else {
3047 /*
3048 * If the voice call is active, use the sound devices of voice call usecase
3049 * so that it would not result any device switch. All the usecases will
3050 * be switched to new device when select_devices() is called for voice call
3051 * usecase. This is to avoid switching devices for voice call when
3052 * check_usecases_codec_backend() is called below.
Alexy Joseph79dfa3c2016-04-20 18:44:56 -07003053 * choose voice call device only if the use case device is
3054 * also using the codec backend
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003055 */
Shiv Maliyappanahallibb4cf0b2016-01-21 11:30:06 -08003056 if (voice_is_in_call(adev) && adev->mode != AUDIO_MODE_NORMAL) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07003057 vc_usecase = get_usecase_from_list(adev,
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -08003058 get_usecase_id_from_usecase_type(adev, VOICE_CALL));
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003059 if ((vc_usecase) && ((is_codec_backend_out_device_type(&vc_usecase->device_list) &&
3060 is_codec_backend_out_device_type(&usecase->device_list)) ||
3061 (is_codec_backend_out_device_type(&vc_usecase->device_list) &&
3062 is_codec_backend_in_device_type(&usecase->device_list)) ||
3063 is_single_device_type_equal(&vc_usecase->device_list,
3064 AUDIO_DEVICE_OUT_HEARING_AID) ||
3065 is_single_device_type_equal(&usecase->device_list,
Robert Lee8a6aaf32019-09-20 16:40:19 +08003066 AUDIO_DEVICE_IN_VOICE_CALL) ||
3067 (is_single_device_type_equal(&usecase->device_list,
Gautam Manam8fa06162021-09-24 10:41:29 +05303068 AUDIO_DEVICE_IN_BUILTIN_MIC) &&
3069 is_single_device_type_equal(&vc_usecase->device_list,
3070 AUDIO_DEVICE_OUT_USB_HEADSET)) ||
3071 (is_single_device_type_equal(&usecase->device_list,
Robert Lee8a6aaf32019-09-20 16:40:19 +08003072 AUDIO_DEVICE_IN_USB_HEADSET) &&
3073 is_single_device_type_equal(&vc_usecase->device_list,
Kamalakar Yalasiri5edba852021-01-28 14:52:38 +05303074 AUDIO_DEVICE_OUT_USB_HEADSET))||
Gautam Manamf4002142021-09-13 22:05:56 +05303075 (is_single_device_type_equal(&usecase->device_list,
3076 AUDIO_DEVICE_IN_USB_HEADSET) &&
3077 is_codec_backend_out_device_type(&vc_usecase->device_list)) ||
Kamalakar Yalasiri5edba852021-01-28 14:52:38 +05303078 (is_single_device_type_equal(&usecase->device_list,
3079 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) &&
3080 is_codec_backend_out_device_type(&vc_usecase->device_list)))) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003081 in_snd_device = vc_usecase->in_snd_device;
3082 out_snd_device = vc_usecase->out_snd_device;
3083 }
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08003084 } else if (voice_extn_compress_voip_is_active(adev)) {
yidongh02ef86f2017-04-21 15:36:04 +08003085 bool out_snd_device_backend_match = true;
yidongh47785a82017-05-08 19:29:29 +08003086 voip_usecase = get_usecase_from_list(adev, USECASE_COMPRESS_VOIP_CALL);
yidongh6261d8e2017-05-15 17:04:02 +08003087 if ((voip_usecase != NULL) &&
3088 (usecase->type == PCM_PLAYBACK) &&
3089 (usecase->stream.out != NULL)) {
yidongh02ef86f2017-04-21 15:36:04 +08003090 out_snd_device_backend_match = platform_check_backends_match(
3091 voip_usecase->out_snd_device,
3092 platform_get_output_snd_device(
3093 adev->platform,
Jaideep Sharma477917f2020-03-13 18:13:33 +05303094 usecase->stream.out, usecase->type));
yidongh02ef86f2017-04-21 15:36:04 +08003095 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003096 if ((voip_usecase) && (is_codec_backend_out_device_type(&voip_usecase->device_list) &&
3097 (is_codec_backend_out_device_type(&usecase->device_list) ||
3098 is_codec_backend_in_device_type(&usecase->device_list)) &&
yidongh02ef86f2017-04-21 15:36:04 +08003099 out_snd_device_backend_match &&
Mingming Yin2d8aa2e2014-08-14 00:00:51 -07003100 (voip_usecase->stream.out != adev->primary_output))) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08003101 in_snd_device = voip_usecase->in_snd_device;
3102 out_snd_device = voip_usecase->out_snd_device;
3103 }
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -08003104 } else if (audio_extn_hfp_is_active(adev)) {
Vimal Puthanveed41fcff22014-01-23 15:56:53 -08003105 hfp_ucid = audio_extn_hfp_get_usecase();
3106 hfp_usecase = get_usecase_from_list(adev, hfp_ucid);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003107 if ((hfp_usecase) && is_codec_backend_out_device_type(&hfp_usecase->device_list)) {
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -08003108 in_snd_device = hfp_usecase->in_snd_device;
3109 out_snd_device = hfp_usecase->out_snd_device;
3110 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003111 }
3112 if (usecase->type == PCM_PLAYBACK) {
Aditya Bavanaribdda2f22016-10-19 15:02:05 +05303113 if (usecase->stream.out == NULL) {
3114 ALOGE("%s: stream.out is NULL", __func__);
3115 return -EINVAL;
3116 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003117 assign_devices(&usecase->device_list, &usecase->stream.out->device_list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003118 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07003119 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurent637e2d42018-11-15 12:24:31 -08003120 struct stream_out *voip_out = adev->primary_output;
3121 struct stream_in *voip_in = get_voice_communication_input(adev);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003122 if (compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_BUS))
Guodong Hu267bdf82019-08-12 19:22:32 +08003123 out_snd_device = audio_extn_auto_hal_get_output_snd_device(adev, uc_id);
3124 else
3125 out_snd_device = platform_get_output_snd_device(adev->platform,
Jaideep Sharma477917f2020-03-13 18:13:33 +05303126 usecase->stream.out,
3127 usecase->type);
kunleizdcf967a2018-08-07 17:09:11 +08003128 voip_usecase = get_usecase_from_list(adev, USECASE_AUDIO_PLAYBACK_VOIP);
kunleizdcf967a2018-08-07 17:09:11 +08003129
Eric Laurent637e2d42018-11-15 12:24:31 -08003130 if (voip_usecase)
3131 voip_out = voip_usecase->stream.out;
3132
3133 if (usecase->stream.out == voip_out && voip_in != NULL)
3134 select_devices(adev, voip_in->usecase);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07003135 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003136 } else if (usecase->type == PCM_CAPTURE) {
Aditya Bavanaribdda2f22016-10-19 15:02:05 +05303137 if (usecase->stream.in == NULL) {
3138 ALOGE("%s: stream.in is NULL", __func__);
3139 return -EINVAL;
3140 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003141 assign_devices(&usecase->device_list, &usecase->stream.in->device_list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003142 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07003143 if (in_snd_device == SND_DEVICE_NONE) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003144 struct listnode out_devices;
Eric Laurent637e2d42018-11-15 12:24:31 -08003145 struct stream_in *voip_in = get_voice_communication_input(adev);
Carter Hsu2e429db2019-05-14 18:50:52 +08003146 struct stream_in *priority_in = NULL;
Eric Laurent637e2d42018-11-15 12:24:31 -08003147
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003148 list_init(&out_devices);
Eric Laurent637e2d42018-11-15 12:24:31 -08003149 if (voip_in != NULL) {
Eric Laurent637e2d42018-11-15 12:24:31 -08003150 struct audio_usecase *voip_usecase = get_usecase_from_list(adev,
3151 USECASE_AUDIO_PLAYBACK_VOIP);
3152
Carter Hsu2e429db2019-05-14 18:50:52 +08003153 usecase->stream.in->enable_ec_port = false;
3154
Zhou Song503196b2021-07-23 17:31:05 +08003155 if (usecase->id == USECASE_AUDIO_RECORD_AFE_PROXY ||
3156 usecase->id == USECASE_AUDIO_RECORD_AFE_PROXY2) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003157 reassign_device_list(&out_devices, AUDIO_DEVICE_OUT_TELEPHONY_TX, "");
Eric Laurent637e2d42018-11-15 12:24:31 -08003158 } else if (voip_usecase) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003159 assign_devices(&out_devices, &voip_usecase->stream.out->device_list);
Eric Laurent637e2d42018-11-15 12:24:31 -08003160 } else if (adev->primary_output &&
3161 !adev->primary_output->standby) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003162 assign_devices(&out_devices, &adev->primary_output->device_list);
Eric Laurent637e2d42018-11-15 12:24:31 -08003163 } else {
3164 /* forcing speaker o/p device to get matching i/p pair
3165 in case o/p is not routed from same primary HAL */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003166 reassign_device_list(&out_devices, AUDIO_DEVICE_OUT_SPEAKER, "");
Eric Laurent637e2d42018-11-15 12:24:31 -08003167 }
Carter Hsu2e429db2019-05-14 18:50:52 +08003168 priority_in = voip_in;
3169 } else {
3170 /* get the input with the highest priority source*/
3171 priority_in = get_priority_input(adev);
3172
Susan Wang727dd6b2021-03-26 11:28:59 -04003173 if (!priority_in ||
3174 audio_extn_auto_hal_overwrite_priority_for_auto(usecase->stream.in))
Carter Hsu2e429db2019-05-14 18:50:52 +08003175 priority_in = usecase->stream.in;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07003176 }
Huicheng Liu1404ba12020-09-11 01:03:25 -04003177 if (compare_device_type(&usecase->device_list, AUDIO_DEVICE_IN_BUS)){
3178 in_snd_device = audio_extn_auto_hal_get_snd_device_for_car_audio_stream(priority_in->car_audio_stream);
3179 }
3180 else
3181 in_snd_device = platform_get_input_snd_device(adev->platform,
3182 priority_in,
3183 &out_devices,
3184 usecase->type);
Zhenlin Lian4f947842022-05-14 15:50:52 +05303185 clear_devices(&out_devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07003186 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003187 }
3188 }
3189
3190 if (out_snd_device == usecase->out_snd_device &&
3191 in_snd_device == usecase->in_snd_device) {
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05303192
3193 if (!force_device_switch(usecase))
3194 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003195 }
3196
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003197 if (!compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_BUS) &&
Guodong Hu267bdf82019-08-12 19:22:32 +08003198 ((is_btsco_device(out_snd_device,in_snd_device) && !adev->bt_sco_on) ||
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003199 (is_a2dp_device(out_snd_device) && !audio_extn_a2dp_source_is_ready()))) {
Guodong Hu267bdf82019-08-12 19:22:32 +08003200 ALOGD("SCO/A2DP is selected but they are not connected/ready hence dont route");
3201 return 0;
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303202 }
3203
Aalique Grahame22e49102018-12-18 14:23:57 -08003204 if (out_snd_device != SND_DEVICE_NONE &&
3205 out_snd_device != adev->last_logged_snd_device[uc_id][0]) {
3206 ALOGD("%s: changing use case %s output device from(%d: %s, acdb %d) to (%d: %s, acdb %d)",
3207 __func__,
3208 use_case_table[uc_id],
3209 adev->last_logged_snd_device[uc_id][0],
3210 platform_get_snd_device_name(adev->last_logged_snd_device[uc_id][0]),
3211 adev->last_logged_snd_device[uc_id][0] != SND_DEVICE_NONE ?
3212 platform_get_snd_device_acdb_id(adev->last_logged_snd_device[uc_id][0]) :
3213 -1,
3214 out_snd_device,
3215 platform_get_snd_device_name(out_snd_device),
3216 platform_get_snd_device_acdb_id(out_snd_device));
3217 adev->last_logged_snd_device[uc_id][0] = out_snd_device;
3218 }
3219 if (in_snd_device != SND_DEVICE_NONE &&
3220 in_snd_device != adev->last_logged_snd_device[uc_id][1]) {
3221 ALOGD("%s: changing use case %s input device from(%d: %s, acdb %d) to (%d: %s, acdb %d)",
3222 __func__,
3223 use_case_table[uc_id],
3224 adev->last_logged_snd_device[uc_id][1],
3225 platform_get_snd_device_name(adev->last_logged_snd_device[uc_id][1]),
3226 adev->last_logged_snd_device[uc_id][1] != SND_DEVICE_NONE ?
3227 platform_get_snd_device_acdb_id(adev->last_logged_snd_device[uc_id][1]) :
3228 -1,
3229 in_snd_device,
3230 platform_get_snd_device_name(in_snd_device),
3231 platform_get_snd_device_acdb_id(in_snd_device));
3232 adev->last_logged_snd_device[uc_id][1] = in_snd_device;
3233 }
3234
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08003235
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003236 /*
3237 * Limitation: While in call, to do a device switch we need to disable
3238 * and enable both RX and TX devices though one of them is same as current
3239 * device.
3240 */
Vidyakumar Athota21b3bb92014-04-25 11:08:08 -07003241 if ((usecase->type == VOICE_CALL) &&
3242 (usecase->in_snd_device != SND_DEVICE_NONE) &&
3243 (usecase->out_snd_device != SND_DEVICE_NONE)) {
Eric Laurentb23d5282013-05-14 15:27:20 -07003244 status = platform_switch_voice_call_device_pre(adev->platform);
Narsinga Rao Chella116142b2015-08-14 18:00:08 -07003245 }
3246
3247 if (((usecase->type == VOICE_CALL) ||
3248 (usecase->type == VOIP_CALL)) &&
3249 (usecase->out_snd_device != SND_DEVICE_NONE)) {
3250 /* Disable sidetone only if voice/voip call already exists */
Jaideep Sharma477917f2020-03-13 18:13:33 +05303251 if (voice_is_call_state_active_in_call(adev) ||
Narsinga Rao Chella116142b2015-08-14 18:00:08 -07003252 voice_extn_compress_voip_is_started(adev))
Bhalchandra Gajare45fee282015-06-09 22:23:45 -07003253 voice_set_sidetone(adev, usecase->out_snd_device, false);
Vidyakumar Athotaea269c62016-10-31 09:05:59 -07003254
3255 /* Disable aanc only if voice call exists */
Jaideep Sharma477917f2020-03-13 18:13:33 +05303256 if (voice_is_call_state_active_in_call(adev))
Vidyakumar Athotaea269c62016-10-31 09:05:59 -07003257 voice_check_and_update_aanc_path(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08003258 }
3259
Aalique Grahame22e49102018-12-18 14:23:57 -08003260 if ((out_snd_device == SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP ||
3261 out_snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP) &&
Florian Pfister1a84f312018-07-19 14:38:18 +02003262 (!audio_extn_a2dp_source_is_ready())) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303263 ALOGW("%s: A2DP profile is not ready, routing to speaker only", __func__);
Aalique Grahame22e49102018-12-18 14:23:57 -08003264 if (out_snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP)
3265 out_snd_device = SND_DEVICE_OUT_SPEAKER_SAFE;
3266 else
3267 out_snd_device = SND_DEVICE_OUT_SPEAKER;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303268 }
3269
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003270 /* Disable current sound devices */
3271 if (usecase->out_snd_device != SND_DEVICE_NONE) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07003272 disable_audio_route(adev, usecase);
3273 disable_snd_device(adev, usecase->out_snd_device);
Gautam Manam274f4752021-09-24 10:58:49 +05303274 if (usecase->id == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS)
3275 disable_snd_device(adev, SND_DEVICE_OUT_HAPTICS);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003276 }
3277
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003278 if (usecase->in_snd_device != SND_DEVICE_NONE) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07003279 disable_audio_route(adev, usecase);
3280 disable_snd_device(adev, usecase->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003281 }
3282
Vidyakumar Athota545dbd32013-11-13 17:30:53 -08003283 /* Applicable only on the targets that has external modem.
3284 * New device information should be sent to modem before enabling
3285 * the devices to reduce in-call device switch time.
3286 */
Vidyakumar Athota21b3bb92014-04-25 11:08:08 -07003287 if ((usecase->type == VOICE_CALL) &&
3288 (usecase->in_snd_device != SND_DEVICE_NONE) &&
3289 (usecase->out_snd_device != SND_DEVICE_NONE)) {
Vidyakumar Athota545dbd32013-11-13 17:30:53 -08003290 status = platform_switch_voice_call_enable_device_config(adev->platform,
3291 out_snd_device,
3292 in_snd_device);
Vidyakumar Athota21b3bb92014-04-25 11:08:08 -07003293 }
Vidyakumar Athota545dbd32013-11-13 17:30:53 -08003294
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003295 /* Enable new sound devices */
3296 if (out_snd_device != SND_DEVICE_NONE) {
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08003297 check_usecases_codec_backend(adev, usecase, out_snd_device);
Saurav Kumarc1411662020-10-14 10:50:45 +05303298 check_and_configure_headphone(adev, usecase, out_snd_device);
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +05303299 if (platform_check_codec_asrc_support(adev->platform))
3300 check_and_set_asrc_mode(adev, usecase, out_snd_device);
Haynes Mathew George1376ca62014-04-24 11:55:48 -07003301 enable_snd_device(adev, out_snd_device);
Zhou Songd9bd9302020-08-04 16:34:45 +08003302 /* Enable haptics device for haptic usecase */
3303 if (usecase->id == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS)
3304 enable_snd_device(adev, SND_DEVICE_OUT_HAPTICS);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003305 }
3306
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07003307 if (in_snd_device != SND_DEVICE_NONE) {
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05303308 check_usecases_capture_codec_backend(adev, usecase, in_snd_device);
Haynes Mathew George1376ca62014-04-24 11:55:48 -07003309 enable_snd_device(adev, in_snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07003310 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003311
Lakshman Chaluvarajuab7e5572020-10-28 19:29:23 +05303312 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL)
Eric Laurentb23d5282013-05-14 15:27:20 -07003313 status = platform_switch_voice_call_device_post(adev->platform,
3314 out_snd_device,
3315 in_snd_device);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08003316
sangwoo170731f2013-06-08 15:36:36 +09003317 usecase->in_snd_device = in_snd_device;
3318 usecase->out_snd_device = out_snd_device;
3319
Dhananjay Kumard6d32152016-10-13 16:11:03 +05303320 audio_extn_utils_update_stream_app_type_cfg_for_usecase(adev,
3321 usecase);
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +05303322 if (usecase->type == PCM_PLAYBACK) {
Weiyin Jiang6f4c8062016-11-23 15:30:29 +08003323 if ((24 == usecase->stream.out->bit_width) &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003324 compare_device_type(&usecase->stream.out->device_list, AUDIO_DEVICE_OUT_SPEAKER)) {
Weiyin Jiang6f4c8062016-11-23 15:30:29 +08003325 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
3326 } else if ((out_snd_device == SND_DEVICE_OUT_HDMI ||
3327 out_snd_device == SND_DEVICE_OUT_USB_HEADSET ||
3328 out_snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
3329 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
3330 /*
3331 * To best utlize DSP, check if the stream sample rate is supported/multiple of
3332 * configured device sample rate, if not update the COPP rate to be equal to the
3333 * device sample rate, else open COPP at stream sample rate
3334 */
3335 platform_check_and_update_copp_sample_rate(adev->platform, out_snd_device,
3336 usecase->stream.out->sample_rate,
3337 &usecase->stream.out->app_type_cfg.sample_rate);
Ashish Jain4826f6c2017-02-06 13:33:20 +05303338 } else if (((out_snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
Preetam Singh Ranawat590d0432019-09-30 14:39:47 +05303339 out_snd_device != SND_DEVICE_OUT_HEADPHONES &&
3340 out_snd_device != SND_DEVICE_OUT_HEADPHONES_HIFI_FILTER &&
Ashish Jain4826f6c2017-02-06 13:33:20 +05303341 !audio_is_true_native_stream_active(adev)) &&
Weiyin Jiang6f4c8062016-11-23 15:30:29 +08003342 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
3343 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +05303344#ifdef PLATFORM_AUTO
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +05303345 if (!(compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_BUS) && ((usecase->stream.out->flags &
3346 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_SYS_NOTIFICATION) || (usecase->stream.out->flags &
3347 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_PHONE)))) {
3348 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
3349 }
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +05303350#else
3351 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
3352#endif
Weiyin Jiang6f4c8062016-11-23 15:30:29 +08003353 }
Weiyin Jiangcdece202019-07-08 16:13:16 +08003354 }
3355 enable_audio_route(adev, usecase);
Weiyin Jiang6f4c8062016-11-23 15:30:29 +08003356
Shalini Manjunatha5e610912020-12-29 13:02:25 +05303357 if (uc_id == USECASE_AUDIO_PLAYBACK_VOIP) {
3358 struct stream_in *voip_in = get_voice_communication_input(adev);
3359 struct audio_usecase *voip_in_usecase = NULL;
3360 voip_in_usecase = get_usecase_from_list(adev, USECASE_AUDIO_RECORD_VOIP);
3361 if (voip_in != NULL &&
3362 voip_in_usecase != NULL &&
3363 !(out_snd_device == AUDIO_DEVICE_OUT_SPEAKER ||
3364 out_snd_device == AUDIO_DEVICE_OUT_SPEAKER_SAFE) &&
3365 (voip_in_usecase->in_snd_device ==
3366 platform_get_input_snd_device(adev->platform, voip_in,
3367 &usecase->stream.out->device_list,usecase->type))) {
3368 /*
3369 * if VOIP TX is enabled before VOIP RX, needs to re-route the TX path
3370 * for enabling echo-reference-voip with correct port
3371 */
3372 ALOGD("%s: VOIP TX is enabled before VOIP RX,needs to re-route the TX path",__func__);
3373 disable_audio_route(adev, voip_in_usecase);
3374 disable_snd_device(adev, voip_in_usecase->in_snd_device);
3375 enable_snd_device(adev, voip_in_usecase->in_snd_device);
3376 enable_audio_route(adev, voip_in_usecase);
3377 }
3378 }
Ramjee Singhcdf67af2021-09-29 14:20:27 +05303379 if (voice_extn_compress_voip_is_active(adev)) {
3380 struct audio_usecase *voip_usecase = get_usecase_from_list(adev,
3381 USECASE_COMPRESS_VOIP_CALL);
3382 /*
3383 * If only compress voip input is opened voip out will be primary out.
3384 * Need to consider re-routing to select correct i/p pair
3385 */
3386 if ((voip_usecase != NULL) &&
3387 (usecase->type == PCM_PLAYBACK) &&
3388 (usecase->stream.out == voip_usecase->stream.out)) {
3389 in_snd_device = platform_get_input_snd_device(adev->platform,
3390 NULL,
3391 &usecase->stream.out->device_list,
3392 usecase->type);
3393 if (voip_usecase->in_snd_device != in_snd_device ) {
3394 ALOGD("%s:Re routing compress voip tx snd device matching voip rx pair",
3395 __func__);
3396 disable_audio_route(adev, voip_usecase);
3397 disable_snd_device(adev, voip_usecase->in_snd_device);
3398 voip_usecase->in_snd_device = in_snd_device;
3399 voip_usecase->out_snd_device = usecase->out_snd_device;
3400 /* Route all TX usecase to Compress voip BE */
3401 check_usecases_capture_codec_backend(adev, voip_usecase, in_snd_device);
3402 enable_snd_device(adev, in_snd_device);
3403 /* Send Voice related calibration for RX /TX pair */
3404 status = platform_switch_voice_call_device_post(adev->platform,
3405 out_snd_device,
3406 in_snd_device);
3407 enable_audio_route(adev, voip_usecase);
3408 }
3409 }
3410 }
Shalini Manjunatha5e610912020-12-29 13:02:25 +05303411
3412
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003413 audio_extn_qdsp_set_device(usecase);
Aalique Grahame22e49102018-12-18 14:23:57 -08003414
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07003415 /* If input stream is already running then effect needs to be
3416 applied on the new input device that's being enabled here. */
Eric Laurent637e2d42018-11-15 12:24:31 -08003417 if (in_snd_device != SND_DEVICE_NONE)
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07003418 check_and_enable_effect(adev);
3419
Vidyakumar Athota493f2892016-08-14 11:56:55 -07003420 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) {
Vidyakumar Athotaea269c62016-10-31 09:05:59 -07003421 /* Enable aanc only if voice call exists */
Jaideep Sharma477917f2020-03-13 18:13:33 +05303422 if (voice_is_call_state_active_in_call(adev))
Vidyakumar Athotaea269c62016-10-31 09:05:59 -07003423 voice_check_and_update_aanc_path(adev, out_snd_device, true);
3424
Vidyakumar Athota493f2892016-08-14 11:56:55 -07003425 /* Enable sidetone only if other voice/voip call already exists */
Jaideep Sharma477917f2020-03-13 18:13:33 +05303426 if (voice_is_call_state_active_in_call(adev) ||
Vidyakumar Athota493f2892016-08-14 11:56:55 -07003427 voice_extn_compress_voip_is_started(adev))
3428 voice_set_sidetone(adev, out_snd_device, true);
3429 }
3430
Vidyakumar Athota1fd21792013-11-15 14:50:57 -08003431 /* Applicable only on the targets that has external modem.
3432 * Enable device command should be sent to modem only after
3433 * enabling voice call mixer controls
3434 */
Vidyakumar Athota339342f2014-07-01 15:30:57 -07003435 if (usecase->type == VOICE_CALL)
Vidyakumar Athota1fd21792013-11-15 14:50:57 -08003436 status = platform_switch_voice_call_usecase_route_post(adev->platform,
3437 out_snd_device,
3438 in_snd_device);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303439
3440 if (is_btsco_device(out_snd_device, in_snd_device) || is_a2dp_device(out_snd_device)) {
Eric Laurent637e2d42018-11-15 12:24:31 -08003441 struct stream_in *in = adev_get_active_input(adev);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303442 if (usecase->type == VOIP_CALL) {
Eric Laurent637e2d42018-11-15 12:24:31 -08003443 if (in != NULL && !in->standby) {
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303444 if (is_bt_soc_on(adev) == false){
3445 ALOGD("BT SCO MIC disconnected while in connection");
Eric Laurent637e2d42018-11-15 12:24:31 -08003446 if (in->pcm != NULL)
3447 pcm_stop(in->pcm);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303448 }
3449 }
3450 if ((usecase->stream.out != NULL) && (usecase->stream.out != adev->primary_output)
3451 && usecase->stream.out->started) {
3452 if (is_bt_soc_on(adev) == false) {
3453 ALOGD("BT SCO/A2DP disconnected while in connection");
3454 out_standby_l(&usecase->stream.out->stream.common);
3455 }
3456 }
3457 } else if ((usecase->stream.out != NULL) &&
3458 !(usecase->stream.out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
Surendar Karkae1dc8742018-11-19 16:23:14 +05303459 (usecase->type != TRANSCODE_LOOPBACK_TX) &&
3460 (usecase->type != TRANSCODE_LOOPBACK_RX) &&
Weiyin Jiang0d373242019-07-25 13:18:17 +08003461 (usecase->type != PCM_CAPTURE) &&
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303462 usecase->stream.out->started) {
3463 if (is_bt_soc_on(adev) == false) {
3464 ALOGD("BT SCO/A2dp disconnected while in connection");
3465 out_standby_l(&usecase->stream.out->stream.common);
3466 }
3467 }
3468 }
3469
Yung Ti Su70cb8242018-06-22 17:38:47 +08003470 if (usecase->type != PCM_CAPTURE && usecase == voip_usecase) {
Aalique Grahame22e49102018-12-18 14:23:57 -08003471 struct stream_out *voip_out = voip_usecase->stream.out;
3472 audio_extn_utils_send_app_type_gain(adev,
3473 voip_out->app_type_cfg.app_type,
3474 &voip_out->app_type_cfg.gain[0]);
3475 }
3476
Ajender Reddyb940b832021-07-07 11:51:42 +05303477 ALOGD("%s: done",__func__);
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05303478
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003479 return status;
3480}
3481
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003482static int stop_input_stream(struct stream_in *in)
3483{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05303484 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003485 struct audio_usecase *uc_info;
Pallavid7c7a272018-01-16 11:22:55 +05303486
3487 if (in == NULL) {
3488 ALOGE("%s: stream_in ptr is NULL", __func__);
3489 return -EINVAL;
3490 }
3491
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003492 struct audio_device *adev = in->dev;
Carter Hsu2e429db2019-05-14 18:50:52 +08003493 struct stream_in *priority_in = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003494
Eric Laurent994a6932013-07-17 11:51:42 -07003495 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003496 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003497 uc_info = get_usecase_from_list(adev, in->usecase);
3498 if (uc_info == NULL) {
3499 ALOGE("%s: Could not find the usecase (%d) in the list",
3500 __func__, in->usecase);
3501 return -EINVAL;
3502 }
3503
Carter Hsu2e429db2019-05-14 18:50:52 +08003504 priority_in = get_priority_input(adev);
3505
Derek Chenea197282019-01-07 17:35:01 -08003506 if (audio_extn_ext_hw_plugin_usecase_stop(adev->ext_hw_plugin, uc_info))
3507 ALOGE("%s: failed to stop ext hw plugin", __func__);
Derek Chend2530072014-11-24 12:39:14 -08003508
Vidyakumar Athota2850d532013-11-19 16:02:12 -08003509 /* Close in-call recording streams */
3510 voice_check_and_stop_incall_rec_usecase(adev, in);
3511
Eric Laurent150dbfe2013-02-27 14:31:02 -08003512 /* 1. Disable stream specific mixer controls */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07003513 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003514
3515 /* 2. Disable the tx device */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07003516 disable_snd_device(adev, uc_info->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003517
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003518 if (is_loopback_input_device(get_device_types(&in->device_list)))
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05303519 audio_extn_keep_alive_stop(KEEP_ALIVE_OUT_PRIMARY);
3520
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08003521 list_remove(&uc_info->list);
Zhenlin Lian4f947842022-05-14 15:50:52 +05303522 clear_devices(&uc_info->device_list);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08003523 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003524
Carter Hsu2e429db2019-05-14 18:50:52 +08003525 if (priority_in == in) {
3526 priority_in = get_priority_input(adev);
Sujin Panicker110f7942021-08-26 17:01:22 +05303527 if (priority_in) {
3528 if (is_usb_in_device_type(&priority_in->device_list)) {
3529 if (audio_extn_usb_connected(NULL))
3530 select_devices(adev, priority_in->usecase);
3531 } else {
3532 select_devices(adev, priority_in->usecase);
3533 }
3534 }
Carter Hsu2e429db2019-05-14 18:50:52 +08003535 }
3536
Vatsal Buchac09ae062018-11-14 13:25:08 +05303537 enable_gcov();
Eric Laurent994a6932013-07-17 11:51:42 -07003538 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003539 return ret;
3540}
3541
3542int start_input_stream(struct stream_in *in)
3543{
3544 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -08003545 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003546 struct audio_usecase *uc_info;
Preetam Singh Ranawata87e9742018-02-13 16:52:53 +05303547
3548 if (in == NULL) {
3549 ALOGE("%s: stream_in ptr is NULL", __func__);
3550 return -EINVAL;
3551 }
3552
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003553 struct audio_device *adev = in->dev;
Garmond Leunge2433c32017-09-28 21:51:22 -07003554 struct pcm_config config = in->config;
Garmond Leung438932f2017-10-04 19:35:18 -07003555 int usecase = platform_update_usecase_from_source(in->source,in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003556
Mingming Yin2664a5b2015-09-03 10:53:11 -07003557 if (get_usecase_from_list(adev, usecase) == NULL)
3558 in->usecase = usecase;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05303559 ALOGD("%s: enter: stream(%p)usecase(%d: %s)",
3560 __func__, &in->stream, in->usecase, use_case_table[in->usecase]);
Shiv Maliyappanahallida107642013-10-17 11:16:13 -07003561
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05303562 if (CARD_STATUS_OFFLINE == in->card_status||
Shubhasini Sugumaran3fde6582022-01-19 15:54:50 -05003563 CARD_STATUS_OFFLINE == adev->card_status ||
3564 POWER_POLICY_STATUS_OFFLINE == adev->in_power_policy) {
3565 ALOGW("in->card_status or adev->card_status or adev->input_power offline, try again");
Dhanalakshmi Siddani4d57e992014-07-17 16:37:51 +05303566 ret = -EIO;
Naresh Tanniru4c630392014-05-12 01:05:52 +05303567 goto error_config;
3568 }
Naresh Tanniru4c630392014-05-12 01:05:52 +05303569
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003570 if (is_sco_in_device_type(&in->device_list)) {
Lakshman Chaluvaraju87f53aa2021-02-02 15:50:11 +05303571 if (!adev->bt_sco_on || audio_extn_a2dp_source_is_ready()) {
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303572 ALOGE("%s: SCO profile is not ready, return error", __func__);
3573 ret = -EIO;
3574 goto error_config;
3575 }
3576 }
3577
Shiv Maliyappanahallida107642013-10-17 11:16:13 -07003578 /* Check if source matches incall recording usecase criteria */
3579 ret = voice_check_and_set_incall_rec_usecase(adev, in);
3580 if (ret)
3581 goto error_config;
3582 else
Mingming Yin2664a5b2015-09-03 10:53:11 -07003583 ALOGV("%s: usecase(%d)", __func__, in->usecase);
3584
Dhananjay Kumar7dbe3562019-08-30 05:49:33 +05303585 if (audio_extn_cin_attached_usecase(in))
3586 audio_extn_cin_acquire_usecase(in);
3587
Mingming Yin2664a5b2015-09-03 10:53:11 -07003588 if (get_usecase_from_list(adev, in->usecase) != NULL) {
3589 ALOGE("%s: use case assigned already in use, stream(%p)usecase(%d: %s)",
3590 __func__, &in->stream, in->usecase, use_case_table[in->usecase]);
Weiyin Jiang38c0e612020-09-10 16:10:51 +08003591 ret = -EINVAL;
3592 goto error_config;
Mingming Yin2664a5b2015-09-03 10:53:11 -07003593 }
Shiv Maliyappanahallida107642013-10-17 11:16:13 -07003594
Eric Laurentb23d5282013-05-14 15:27:20 -07003595 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003596 if (in->pcm_device_id < 0) {
3597 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
3598 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -08003599 ret = -EINVAL;
3600 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003601 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003602
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003603 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07003604
3605 if (!uc_info) {
3606 ret = -ENOMEM;
3607 goto error_config;
3608 }
3609
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003610 uc_info->id = in->usecase;
3611 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08003612 uc_info->stream.in = in;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003613 list_init(&uc_info->device_list);
3614 assign_devices(&uc_info->device_list, &in->device_list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003615 uc_info->in_snd_device = SND_DEVICE_NONE;
3616 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003617
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08003618 list_add_tail(&adev->usecase_list, &uc_info->list);
Wei Wangf7ca6c92017-11-21 14:51:20 -08003619 audio_streaming_hint_start();
Sudheer Papothifa9d2282015-09-17 01:53:25 +05303620 audio_extn_perf_lock_acquire(&adev->perf_lock_handle, 0,
3621 adev->perf_lock_opts,
3622 adev->perf_lock_opts_size);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003623 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003624
Derek Chenea197282019-01-07 17:35:01 -08003625 if (audio_extn_ext_hw_plugin_usecase_start(adev->ext_hw_plugin, uc_info))
3626 ALOGE("%s: failed to start ext hw plugin", __func__);
Derek Chend2530072014-11-24 12:39:14 -08003627
Deeraj Soman30cc7ae2019-03-18 16:26:55 +05303628 android_atomic_acquire_cas(true, false, &(in->capture_stopped));
3629
Dhananjay Kumar7dbe3562019-08-30 05:49:33 +05303630 if (audio_extn_cin_attached_usecase(in)) {
Manish Dewangan46e07982018-12-13 18:18:59 +05303631 ret = audio_extn_cin_open_input_stream(in);
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05303632 if (ret)
3633 goto error_open;
3634 else
3635 goto done_open;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003636 }
3637
Haynes Mathew George16081042017-05-31 17:16:49 -07003638 if (in->usecase == USECASE_AUDIO_RECORD_MMAP) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003639 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) {
Haynes Mathew George16081042017-05-31 17:16:49 -07003640 ALOGE("%s: pcm stream not ready", __func__);
3641 goto error_open;
3642 }
3643 ret = pcm_start(in->pcm);
3644 if (ret < 0) {
3645 ALOGE("%s: MMAP pcm_start failed ret %d", __func__, ret);
3646 goto error_open;
3647 }
3648 } else {
3649 unsigned int flags = PCM_IN | PCM_MONOTONIC;
3650 unsigned int pcm_open_retry_count = 0;
3651
Zhou Song62ea0282020-03-22 19:53:01 +08003652 if ((in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY) ||
3653 (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY2)) {
Haynes Mathew George16081042017-05-31 17:16:49 -07003654 flags |= PCM_MMAP | PCM_NOIRQ;
3655 pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT;
3656 } else if (in->realtime) {
3657 flags |= PCM_MMAP | PCM_NOIRQ;
3658 }
3659
Garmond Leunge2433c32017-09-28 21:51:22 -07003660 if (audio_extn_ffv_get_stream() == in) {
3661 ALOGD("%s: ffv stream, update pcm config", __func__);
3662 audio_extn_ffv_update_pcm_config(&config);
3663 }
Haynes Mathew George16081042017-05-31 17:16:49 -07003664 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
3665 __func__, adev->snd_card, in->pcm_device_id, in->config.channels);
3666
3667 while (1) {
Haynes Mathew George380745d2017-10-04 15:27:45 -07003668 ATRACE_BEGIN("pcm_in_open");
Haynes Mathew George16081042017-05-31 17:16:49 -07003669 in->pcm = pcm_open(adev->snd_card, in->pcm_device_id,
Garmond Leung438932f2017-10-04 19:35:18 -07003670 flags, &config);
Haynes Mathew George380745d2017-10-04 15:27:45 -07003671 ATRACE_END();
Satish Babu Patakokila54ce83d2018-07-06 18:00:37 +05303672 if (errno == ENETRESET && !pcm_is_ready(in->pcm)) {
Sharad Sanglec6f32552018-05-04 16:15:38 +05303673 ALOGE("%s: pcm_open failed errno:%d\n", __func__, errno);
3674 adev->card_status = CARD_STATUS_OFFLINE;
3675 in->card_status = CARD_STATUS_OFFLINE;
3676 ret = -EIO;
3677 goto error_open;
3678 }
3679
Haynes Mathew George16081042017-05-31 17:16:49 -07003680 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) {
3681 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
3682 if (in->pcm != NULL) {
3683 pcm_close(in->pcm);
3684 in->pcm = NULL;
3685 }
Weiyin Jiang72197252019-10-09 11:49:32 +08003686 if (pcm_open_retry_count == 0) {
Haynes Mathew George16081042017-05-31 17:16:49 -07003687 ret = -EIO;
3688 goto error_open;
3689 }
Weiyin Jiang72197252019-10-09 11:49:32 +08003690 pcm_open_retry_count--;
Haynes Mathew George16081042017-05-31 17:16:49 -07003691 usleep(PROXY_OPEN_WAIT_TIME * 1000);
3692 continue;
3693 }
3694 break;
3695 }
3696
3697 ALOGV("%s: pcm_prepare", __func__);
Haynes Mathew George380745d2017-10-04 15:27:45 -07003698 ATRACE_BEGIN("pcm_in_prepare");
Haynes Mathew George16081042017-05-31 17:16:49 -07003699 ret = pcm_prepare(in->pcm);
Haynes Mathew George380745d2017-10-04 15:27:45 -07003700 ATRACE_END();
Haynes Mathew George16081042017-05-31 17:16:49 -07003701 if (ret < 0) {
3702 ALOGE("%s: pcm_prepare returned %d", __func__, ret);
3703 pcm_close(in->pcm);
3704 in->pcm = NULL;
3705 goto error_open;
3706 }
Vaibhav Raut5f3bf222023-02-06 17:45:22 +05303707 if (in->flags & (AUDIO_INPUT_FLAG_FAST | AUDIO_INPUT_FLAG_RAW))
Narsinga Rao Chellaa6e1f702021-10-22 13:57:32 +05303708 register_in_stream(in);
Haynes Mathew George16081042017-05-31 17:16:49 -07003709 if (in->realtime) {
Haynes Mathew George380745d2017-10-04 15:27:45 -07003710 ATRACE_BEGIN("pcm_in_start");
Haynes Mathew George16081042017-05-31 17:16:49 -07003711 ret = pcm_start(in->pcm);
Haynes Mathew George380745d2017-10-04 15:27:45 -07003712 ATRACE_END();
Haynes Mathew George16081042017-05-31 17:16:49 -07003713 if (ret < 0) {
3714 ALOGE("%s: RT pcm_start failed ret %d", __func__, ret);
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003715 pcm_close(in->pcm);
3716 in->pcm = NULL;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003717 goto error_open;
3718 }
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003719 }
Haynes Mathew George5beddd42016-06-27 18:33:40 -07003720 }
3721
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07003722 check_and_enable_effect(adev);
justinweng20fb6d82019-02-21 18:49:00 -07003723 audio_extn_audiozoom_set_microphone_direction(in, in->zoom);
3724 audio_extn_audiozoom_set_microphone_field_dimension(in, in->direction);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07003725
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003726 if (is_loopback_input_device(get_device_types(&in->device_list)))
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05303727 audio_extn_keep_alive_start(KEEP_ALIVE_OUT_PRIMARY);
3728
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05303729done_open:
Wei Wangf7ca6c92017-11-21 14:51:20 -08003730 audio_streaming_hint_end();
Sudheer Papothifa9d2282015-09-17 01:53:25 +05303731 audio_extn_perf_lock_release(&adev->perf_lock_handle);
Manisha Agarwal4c2402e2020-10-21 12:02:57 +05303732 ALOGD("%s: exit", __func__);
Vatsal Buchac09ae062018-11-14 13:25:08 +05303733 enable_gcov();
Eric Laurentc8400632013-02-14 19:04:54 -08003734 return ret;
3735
3736error_open:
Wei Wangf7ca6c92017-11-21 14:51:20 -08003737 audio_streaming_hint_end();
Sudheer Papothifa9d2282015-09-17 01:53:25 +05303738 audio_extn_perf_lock_release(&adev->perf_lock_handle);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003739 stop_input_stream(in);
Wei Wangf7ca6c92017-11-21 14:51:20 -08003740
Eric Laurentc8400632013-02-14 19:04:54 -08003741error_config:
Weiyin Jiang38c0e612020-09-10 16:10:51 +08003742 if (audio_extn_cin_attached_usecase(in))
3743 audio_extn_cin_close_input_stream(in);
Laxminath Kasam2cb4b752015-09-24 03:59:15 +05303744 /*
3745 * sleep 50ms to allow sufficient time for kernel
3746 * drivers to recover incases like SSR.
3747 */
3748 usleep(50000);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003749 ALOGD("%s: exit: status(%d)", __func__, ret);
Vatsal Buchac09ae062018-11-14 13:25:08 +05303750 enable_gcov();
Eric Laurentc8400632013-02-14 19:04:54 -08003751 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003752}
3753
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07003754void lock_input_stream(struct stream_in *in)
3755{
3756 pthread_mutex_lock(&in->pre_lock);
3757 pthread_mutex_lock(&in->lock);
3758 pthread_mutex_unlock(&in->pre_lock);
3759}
3760
3761void lock_output_stream(struct stream_out *out)
3762{
3763 pthread_mutex_lock(&out->pre_lock);
3764 pthread_mutex_lock(&out->lock);
3765 pthread_mutex_unlock(&out->pre_lock);
3766}
3767
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003768/* must be called with out->lock locked */
3769static int send_offload_cmd_l(struct stream_out* out, int command)
3770{
3771 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
3772
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07003773 if (!cmd) {
3774 ALOGE("failed to allocate mem for command 0x%x", command);
3775 return -ENOMEM;
3776 }
3777
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003778 ALOGVV("%s %d", __func__, command);
3779
3780 cmd->cmd = command;
3781 list_add_tail(&out->offload_cmd_list, &cmd->node);
3782 pthread_cond_signal(&out->offload_cond);
3783 return 0;
3784}
3785
Gautam Manam14c198b2020-12-24 14:08:04 +05303786/* must be called with out->lock */
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003787static void stop_compressed_output_l(struct stream_out *out)
3788{
Gautam Manam14c198b2020-12-24 14:08:04 +05303789 pthread_mutex_lock(&out->latch_lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003790 out->offload_state = OFFLOAD_STATE_IDLE;
Gautam Manam14c198b2020-12-24 14:08:04 +05303791 pthread_mutex_unlock(&out->latch_lock);
Weiyin Jiang906db3c2021-03-02 13:17:04 +08003792
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003793 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07003794 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003795 if (out->compr != NULL) {
3796 compress_stop(out->compr);
3797 while (out->offload_thread_blocked) {
3798 pthread_cond_wait(&out->cond, &out->lock);
3799 }
3800 }
3801}
3802
Varun Balaraje49253e2017-07-06 19:48:56 +05303803bool is_interactive_usecase(audio_usecase_t uc_id)
3804{
3805 unsigned int i;
3806 for (i = 0; i < sizeof(interactive_usecases)/sizeof(interactive_usecases[0]); i++) {
3807 if (uc_id == interactive_usecases[i])
3808 return true;
3809 }
3810 return false;
3811}
3812
3813static audio_usecase_t get_interactive_usecase(struct audio_device *adev)
3814{
3815 audio_usecase_t ret_uc = USECASE_INVALID;
3816 unsigned int intract_uc_index;
3817 unsigned int num_usecase = sizeof(interactive_usecases)/sizeof(interactive_usecases[0]);
3818
3819 ALOGV("%s: num_usecase: %d", __func__, num_usecase);
3820 for (intract_uc_index = 0; intract_uc_index < num_usecase; intract_uc_index++) {
3821 if (!(adev->interactive_usecase_state & (0x1 << intract_uc_index))) {
3822 adev->interactive_usecase_state |= 0x1 << intract_uc_index;
3823 ret_uc = interactive_usecases[intract_uc_index];
3824 break;
3825 }
3826 }
3827
3828 ALOGV("%s: Interactive usecase is %d", __func__, ret_uc);
3829 return ret_uc;
3830}
3831
3832static void free_interactive_usecase(struct audio_device *adev,
3833 audio_usecase_t uc_id)
3834{
3835 unsigned int interact_uc_index;
3836 unsigned int num_usecase = sizeof(interactive_usecases)/sizeof(interactive_usecases[0]);
3837
3838 for (interact_uc_index = 0; interact_uc_index < num_usecase; interact_uc_index++) {
3839 if (interactive_usecases[interact_uc_index] == uc_id) {
3840 adev->interactive_usecase_state &= ~(0x1 << interact_uc_index);
3841 break;
3842 }
3843 }
3844 ALOGV("%s: free Interactive usecase %d", __func__, uc_id);
3845}
3846
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003847bool is_offload_usecase(audio_usecase_t uc_id)
3848{
3849 unsigned int i;
3850 for (i = 0; i < sizeof(offload_usecases)/sizeof(offload_usecases[0]); i++) {
3851 if (uc_id == offload_usecases[i])
3852 return true;
3853 }
3854 return false;
3855}
3856
Dhananjay Kumarac341582017-02-23 23:42:25 +05303857static audio_usecase_t get_offload_usecase(struct audio_device *adev, bool is_compress)
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003858{
vivek mehta446c3962015-09-14 10:57:35 -07003859 audio_usecase_t ret_uc = USECASE_INVALID;
3860 unsigned int offload_uc_index;
Alexy Josephb1379942016-01-29 15:49:38 -08003861 unsigned int num_usecase = sizeof(offload_usecases)/sizeof(offload_usecases[0]);
vivek mehta446c3962015-09-14 10:57:35 -07003862 if (!adev->multi_offload_enable) {
Dhananjay Kumarac341582017-02-23 23:42:25 +05303863 if (!is_compress)
vivek mehta446c3962015-09-14 10:57:35 -07003864 ret_uc = USECASE_AUDIO_PLAYBACK_OFFLOAD2;
3865 else
3866 ret_uc = USECASE_AUDIO_PLAYBACK_OFFLOAD;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003867
vivek mehta446c3962015-09-14 10:57:35 -07003868 pthread_mutex_lock(&adev->lock);
3869 if (get_usecase_from_list(adev, ret_uc) != NULL)
3870 ret_uc = USECASE_INVALID;
3871 pthread_mutex_unlock(&adev->lock);
3872
3873 return ret_uc;
3874 }
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003875
3876 ALOGV("%s: num_usecase: %d", __func__, num_usecase);
vivek mehta446c3962015-09-14 10:57:35 -07003877 for (offload_uc_index = 0; offload_uc_index < num_usecase; offload_uc_index++) {
3878 if (!(adev->offload_usecases_state & (0x1 << offload_uc_index))) {
3879 adev->offload_usecases_state |= 0x1 << offload_uc_index;
3880 ret_uc = offload_usecases[offload_uc_index];
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003881 break;
3882 }
3883 }
vivek mehta446c3962015-09-14 10:57:35 -07003884
3885 ALOGV("%s: offload usecase is %d", __func__, ret_uc);
3886 return ret_uc;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003887}
3888
3889static void free_offload_usecase(struct audio_device *adev,
3890 audio_usecase_t uc_id)
3891{
vivek mehta446c3962015-09-14 10:57:35 -07003892 unsigned int offload_uc_index;
Alexy Josephb1379942016-01-29 15:49:38 -08003893 unsigned int num_usecase = sizeof(offload_usecases)/sizeof(offload_usecases[0]);
vivek mehta446c3962015-09-14 10:57:35 -07003894
3895 if (!adev->multi_offload_enable)
3896 return;
3897
3898 for (offload_uc_index = 0; offload_uc_index < num_usecase; offload_uc_index++) {
3899 if (offload_usecases[offload_uc_index] == uc_id) {
3900 adev->offload_usecases_state &= ~(0x1 << offload_uc_index);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003901 break;
3902 }
3903 }
3904 ALOGV("%s: free offload usecase %d", __func__, uc_id);
3905}
3906
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003907static void *offload_thread_loop(void *context)
3908{
3909 struct stream_out *out = (struct stream_out *) context;
3910 struct listnode *item;
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08003911 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003912
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003913 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
Weiyin Jiangd9b5a4e2019-07-18 17:24:21 +08003914 set_sched_policy(0, SP_FOREGROUND);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003915 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
3916
3917 ALOGV("%s", __func__);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07003918 lock_output_stream(out);
juyuchen391b5fa2018-12-12 17:58:09 +08003919 out->offload_state = OFFLOAD_STATE_IDLE;
3920 out->playback_started = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003921 for (;;) {
3922 struct offload_cmd *cmd = NULL;
3923 stream_callback_event_t event;
3924 bool send_callback = false;
3925
3926 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
3927 __func__, list_empty(&out->offload_cmd_list),
3928 out->offload_state);
3929 if (list_empty(&out->offload_cmd_list)) {
3930 ALOGV("%s SLEEPING", __func__);
3931 pthread_cond_wait(&out->offload_cond, &out->lock);
3932 ALOGV("%s RUNNING", __func__);
3933 continue;
3934 }
3935
3936 item = list_head(&out->offload_cmd_list);
3937 cmd = node_to_item(item, struct offload_cmd, node);
3938 list_remove(item);
3939
3940 ALOGVV("%s STATE %d CMD %d out->compr %p",
3941 __func__, out->offload_state, cmd->cmd, out->compr);
3942
3943 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
3944 free(cmd);
3945 break;
3946 }
3947
Haynes Mathew Georgeee5836f2017-11-21 18:02:10 -08003948 // allow OFFLOAD_CMD_ERROR reporting during standby
3949 // this is needed to handle failures during compress_open
3950 // Note however that on a pause timeout, the stream is closed
3951 // and no offload usecase will be active. Therefore this
3952 // special case is needed for compress_open failures alone
3953 if (cmd->cmd != OFFLOAD_CMD_ERROR &&
3954 out->compr == NULL) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003955 ALOGE("%s: Compress handle is NULL", __func__);
Haynes Mathew Georgea9abb202016-06-02 14:13:20 -07003956 free(cmd);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003957 pthread_cond_signal(&out->cond);
3958 continue;
3959 }
3960 out->offload_thread_blocked = true;
3961 pthread_mutex_unlock(&out->lock);
3962 send_callback = false;
3963 switch(cmd->cmd) {
3964 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07003965 ALOGD("copl(%p):calling compress_wait", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003966 compress_wait(out->compr, -1);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07003967 ALOGD("copl(%p):out of compress_wait", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003968 send_callback = true;
3969 event = STREAM_CBK_EVENT_WRITE_READY;
3970 break;
3971 case OFFLOAD_CMD_PARTIAL_DRAIN:
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08003972 ret = compress_next_track(out->compr);
Sidipotu Ashok55820562014-02-10 16:16:38 +05303973 if(ret == 0) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07003974 ALOGD("copl(%p):calling compress_partial_drain", out);
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05303975 ret = compress_partial_drain(out->compr);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07003976 ALOGD("copl(%p):out of compress_partial_drain", out);
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05303977 if (ret < 0)
3978 ret = -errno;
Sidipotu Ashok55820562014-02-10 16:16:38 +05303979 }
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05303980 else if (ret == -ETIMEDOUT)
Aniket Kumar Lata3570fb12017-11-08 15:53:44 -08003981 ret = compress_drain(out->compr);
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08003982 else
3983 ALOGE("%s: Next track returned error %d",__func__, ret);
Aniket Kumar Lata3570fb12017-11-08 15:53:44 -08003984 if (-ENETRESET != ret && !(-EINTR == ret &&
Shubhasini Sugumaran3fde6582022-01-19 15:54:50 -05003985 (CARD_STATUS_OFFLINE == out->card_status ||
3986 POWER_POLICY_STATUS_OFFLINE == adev->out_power_policy))) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05303987 send_callback = true;
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05303988 pthread_mutex_lock(&out->lock);
3989 out->send_new_metadata = 1;
3990 out->send_next_track_params = true;
3991 pthread_mutex_unlock(&out->lock);
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05303992 event = STREAM_CBK_EVENT_DRAIN_READY;
3993 ALOGV("copl(%p):send drain callback, ret %d", out, ret);
3994 } else
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05303995 ALOGI("%s: Block drain ready event during SSR", __func__);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003996 break;
3997 case OFFLOAD_CMD_DRAIN:
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07003998 ALOGD("copl(%p):calling compress_drain", out);
Aniket Kumar Lata3570fb12017-11-08 15:53:44 -08003999 ret = compress_drain(out->compr);
4000 ALOGD("copl(%p):out of compress_drain", out);
4001 // EINTR check avoids drain interruption due to SSR
4002 if (-ENETRESET != ret && !(-EINTR == ret &&
Shubhasini Sugumaran3fde6582022-01-19 15:54:50 -05004003 (CARD_STATUS_OFFLINE == out->card_status ||
4004 POWER_POLICY_STATUS_OFFLINE == adev->out_power_policy))) {
Aniket Kumar Lata3570fb12017-11-08 15:53:44 -08004005 send_callback = true;
4006 event = STREAM_CBK_EVENT_DRAIN_READY;
4007 } else
4008 ALOGI("%s: Block drain ready event during SSR", __func__);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004009 break;
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304010 case OFFLOAD_CMD_ERROR:
4011 ALOGD("copl(%p): sending error callback to AF", out);
4012 send_callback = true;
4013 event = STREAM_CBK_EVENT_ERROR;
4014 break;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004015 default:
4016 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
4017 break;
4018 }
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07004019 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004020 out->offload_thread_blocked = false;
4021 pthread_cond_signal(&out->cond);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08004022 if (send_callback && out->client_callback) {
4023 ALOGVV("%s: sending client_callback event %d", __func__, event);
4024 out->client_callback(event, NULL, out->client_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -07004025 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004026 free(cmd);
4027 }
4028
4029 pthread_cond_signal(&out->cond);
4030 while (!list_empty(&out->offload_cmd_list)) {
4031 item = list_head(&out->offload_cmd_list);
4032 list_remove(item);
4033 free(node_to_item(item, struct offload_cmd, node));
4034 }
4035 pthread_mutex_unlock(&out->lock);
4036
4037 return NULL;
4038}
4039
4040static int create_offload_callback_thread(struct stream_out *out)
4041{
4042 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
4043 list_init(&out->offload_cmd_list);
4044 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
4045 offload_thread_loop, out);
4046 return 0;
4047}
4048
4049static int destroy_offload_callback_thread(struct stream_out *out)
4050{
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07004051 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004052 stop_compressed_output_l(out);
4053 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
4054
4055 pthread_mutex_unlock(&out->lock);
4056 pthread_join(out->offload_thread, (void **) NULL);
4057 pthread_cond_destroy(&out->offload_cond);
4058
4059 return 0;
4060}
4061
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004062static int stop_output_stream(struct stream_out *out)
4063{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05304064 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004065 struct audio_usecase *uc_info;
4066 struct audio_device *adev = out->dev;
Aalique Grahame22e49102018-12-18 14:23:57 -08004067 bool has_voip_usecase =
4068 get_usecase_from_list(adev, USECASE_AUDIO_PLAYBACK_VOIP) != NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004069
Eric Laurent994a6932013-07-17 11:51:42 -07004070 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07004071 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004072 uc_info = get_usecase_from_list(adev, out->usecase);
4073 if (uc_info == NULL) {
4074 ALOGE("%s: Could not find the usecase (%d) in the list",
4075 __func__, out->usecase);
4076 return -EINVAL;
4077 }
4078
Zhou Songbaddf9f2020-11-20 13:57:39 +08004079 out->a2dp_muted = false;
4080
Derek Chenea197282019-01-07 17:35:01 -08004081 if (audio_extn_ext_hw_plugin_usecase_stop(adev->ext_hw_plugin, uc_info))
4082 ALOGE("%s: failed to stop ext hw plugin", __func__);
Derek Chend2530072014-11-24 12:39:14 -08004083
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004084 if (is_offload_usecase(out->usecase) &&
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05304085 !(audio_extn_passthru_is_passthrough_stream(out))) {
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08004086 if (adev->visualizer_stop_output != NULL)
4087 adev->visualizer_stop_output(out->handle, out->pcm_device_id);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08004088
4089 audio_extn_dts_remove_state_notifier_node(out->usecase);
4090
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08004091 if (adev->offload_effects_stop_output != NULL)
4092 adev->offload_effects_stop_output(out->handle, out->pcm_device_id);
vivek mehtad15d2bf2019-05-17 13:35:10 -07004093 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_ULL ||
4094 out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
4095 audio_low_latency_hint_end();
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08004096 }
Eric Laurentc4aef752013-09-12 17:45:53 -07004097
Arun Mirpuri2ce92a62018-09-12 18:36:10 -07004098 if (out->usecase == USECASE_INCALL_MUSIC_UPLINK ||
4099 out->usecase == USECASE_INCALL_MUSIC_UPLINK2) {
Arun Mirpurief53ce52018-09-11 18:00:09 -07004100 voice_set_device_mute_flag(adev, false);
Arun Mirpuri2ce92a62018-09-12 18:36:10 -07004101 }
Arun Mirpurief53ce52018-09-11 18:00:09 -07004102
Eric Laurent150dbfe2013-02-27 14:31:02 -08004103 /* 1. Get and set stream specific mixer controls */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07004104 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07004105
4106 /* 2. Disable the rx device */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07004107 disable_snd_device(adev, uc_info->out_snd_device);
Zhou Songd9bd9302020-08-04 16:34:45 +08004108 if (out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS)
4109 disable_snd_device(adev, SND_DEVICE_OUT_HAPTICS);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004110
Aalique Grahame22e49102018-12-18 14:23:57 -08004111 audio_extn_extspk_update(adev->extspk);
4112
Xiaojun Sang785b5da2017-08-03 15:52:29 +08004113 if (is_offload_usecase(out->usecase)) {
4114 audio_enable_asm_bit_width_enforce_mode(adev->mixer,
4115 adev->dsp_bit_width_enforce_mode,
4116 false);
4117 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004118 if (is_usb_out_device_type(&out->device_list)) {
Garmond Leung5fd0b552018-04-17 11:56:12 -07004119 ret = audio_extn_usb_check_and_set_svc_int(uc_info,
4120 false);
4121
4122 if (ret != 0)
4123 check_usecases_codec_backend(adev, uc_info, uc_info->out_snd_device);
4124 /* default service interval was successfully updated,
4125 reopen USB backend with new service interval */
4126 ret = 0;
4127 }
Xiaojun Sang785b5da2017-08-03 15:52:29 +08004128
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08004129 list_remove(&uc_info->list);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304130 out->started = 0;
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004131 if (is_offload_usecase(out->usecase) &&
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05304132 (audio_extn_passthru_is_passthrough_stream(out))) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004133 ALOGV("Disable passthrough , reset mixer to pcm");
4134 /* NO_PASSTHROUGH */
Meng Wang4c32fb42020-01-16 17:57:11 +08004135#ifdef AUDIO_GKI_ENABLED
4136 /* out->compr_config.codec->reserved[0] is for compr_passthr */
4137 out->compr_config.codec->reserved[0] = 0;
4138#else
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004139 out->compr_config.codec->compr_passthr = 0;
Meng Wang4c32fb42020-01-16 17:57:11 +08004140#endif
Mingming Yin21854652016-04-13 11:54:02 -07004141 audio_extn_passthru_on_stop(out);
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004142 audio_extn_dolby_set_dap_bypass(adev, DAP_STATE_ON);
4143 }
Eric Laurent07eeafd2013-10-06 12:52:49 -07004144
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05304145 /* Must be called after removing the usecase from list */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004146 if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_AUX_DIGITAL))
Md Mansoor Ahmeddb1b4f92018-01-25 18:56:31 +05304147 audio_extn_keep_alive_start(KEEP_ALIVE_OUT_HDMI);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05304148
Manish Dewangan21a850a2017-08-14 12:03:55 +05304149 if (out->ip_hdlr_handle) {
Naresh Tanniru85819452017-05-04 18:55:45 -07004150 ret = audio_extn_ip_hdlr_intf_close(out->ip_hdlr_handle, true, out);
4151 if (ret < 0)
4152 ALOGE("%s: audio_extn_ip_hdlr_intf_close failed %d",__func__, ret);
4153 }
4154
Zhou Song642ec432020-12-23 16:11:10 +08004155 /* trigger voip input to reroute when voip output changes to hearing aid */
Aalique Grahame22e49102018-12-18 14:23:57 -08004156 if (has_voip_usecase ||
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004157 compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
Aalique Grahame22e49102018-12-18 14:23:57 -08004158 struct listnode *node;
4159 struct audio_usecase *usecase;
4160 list_for_each(node, &adev->usecase_list) {
4161 usecase = node_to_item(node, struct audio_usecase, list);
Zhou Song642ec432020-12-23 16:11:10 +08004162 if (usecase->type == PCM_PLAYBACK || usecase == uc_info ||
4163 (usecase->type == PCM_CAPTURE &&
Revathi Uddarajud9f23d92020-07-27 10:55:06 +05304164 usecase->id != USECASE_AUDIO_RECORD_VOIP &&
Zhou Song642ec432020-12-23 16:11:10 +08004165 usecase->id != USECASE_AUDIO_RECORD_VOIP_LOW_LATENCY))
Aalique Grahame22e49102018-12-18 14:23:57 -08004166 continue;
4167
4168 ALOGD("%s: select_devices at usecase(%d: %s) after removing the usecase(%d: %s)",
4169 __func__, usecase->id, use_case_table[usecase->id],
4170 out->usecase, use_case_table[out->usecase]);
4171 select_devices(adev, usecase->id);
4172 }
4173 }
4174
Zhenlin Lian4f947842022-05-14 15:50:52 +05304175 clear_devices(&uc_info->device_list);
Garmond Leung5fd0b552018-04-17 11:56:12 -07004176 free(uc_info);
Eric Laurent994a6932013-07-17 11:51:42 -07004177 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004178 return ret;
4179}
4180
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004181struct pcm* pcm_open_prepare_helper(unsigned int snd_card, unsigned int pcm_device_id,
4182 unsigned int flags, unsigned int pcm_open_retry_count,
4183 struct pcm_config *config)
4184{
4185 struct pcm* pcm = NULL;
4186
4187 while (1) {
4188 pcm = pcm_open(snd_card, pcm_device_id, flags, config);
4189 if (pcm == NULL || !pcm_is_ready(pcm)) {
4190 ALOGE("%s: %s", __func__, pcm_get_error(pcm));
4191 if (pcm != NULL) {
4192 pcm_close(pcm);
4193 pcm = NULL;
4194 }
Weiyin Jiang72197252019-10-09 11:49:32 +08004195 if (pcm_open_retry_count == 0)
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004196 return NULL;
4197
Weiyin Jiang72197252019-10-09 11:49:32 +08004198 pcm_open_retry_count--;
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004199 usleep(PROXY_OPEN_WAIT_TIME * 1000);
4200 continue;
4201 }
4202 break;
4203 }
4204
4205 if (pcm_is_ready(pcm)) {
4206 int ret = pcm_prepare(pcm);
4207 if (ret < 0) {
4208 ALOGE("%s: pcm_prepare returned %d", __func__, ret);
4209 pcm_close(pcm);
4210 pcm = NULL;
4211 }
4212 }
4213
4214 return pcm;
4215}
4216
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004217int start_output_stream(struct stream_out *out)
4218{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004219 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004220 struct audio_usecase *uc_info;
4221 struct audio_device *adev = out->dev;
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08004222 char mixer_ctl_name[128];
4223 struct mixer_ctl *ctl = NULL;
4224 char* perf_mode[] = {"ULL", "ULL_PP", "LL"};
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304225 bool a2dp_combo = false;
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004226 bool is_haptic_usecase = (out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) ? true: false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004227
Haynes Mathew George380745d2017-10-04 15:27:45 -07004228 ATRACE_BEGIN("start_output_stream");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07004229 if ((out->usecase < 0) || (out->usecase >= AUDIO_USECASE_MAX)) {
4230 ret = -EINVAL;
4231 goto error_config;
4232 }
4233
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004234 ALOGD("%s: enter: stream(%p)usecase(%d: %s) devices(%#x) is_haptic_usecase(%d)",
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05304235 __func__, &out->stream, out->usecase, use_case_table[out->usecase],
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004236 get_device_types(&out->device_list), is_haptic_usecase);
4237
4238 bool is_speaker_active = compare_device_type(&out->device_list,
4239 AUDIO_DEVICE_OUT_SPEAKER);
4240 bool is_speaker_safe_active = compare_device_type(&out->device_list,
4241 AUDIO_DEVICE_OUT_SPEAKER_SAFE);
Naresh Tanniru4c630392014-05-12 01:05:52 +05304242
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304243 if (CARD_STATUS_OFFLINE == out->card_status ||
Shubhasini Sugumaran3fde6582022-01-19 15:54:50 -05004244 CARD_STATUS_OFFLINE == adev->card_status ||
4245 POWER_POLICY_STATUS_OFFLINE == adev->out_power_policy) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304246 ALOGW("out->card_status or adev->card_status offline, try again");
Dhanalakshmi Siddani4d57e992014-07-17 16:37:51 +05304247 ret = -EIO;
Mingshu Pang5fc696f2020-02-28 12:32:00 +08004248 goto error_fatal;
Naresh Tanniru4c630392014-05-12 01:05:52 +05304249 }
Naresh Tanniru4c630392014-05-12 01:05:52 +05304250
Arun Mirpuri2ce92a62018-09-12 18:36:10 -07004251 //Update incall music usecase to reflect correct voice session
4252 if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
4253 ret = voice_extn_check_and_set_incall_music_usecase(adev, out);
4254 if (ret != 0) {
4255 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
4256 __func__, ret);
4257 goto error_config;
4258 }
4259 }
4260
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004261 if (is_a2dp_out_device_type(&out->device_list)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02004262 if (!audio_extn_a2dp_source_is_ready()) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004263 if (is_speaker_active || is_speaker_safe_active) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304264 a2dp_combo = true;
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05304265 } else {
Zhou Songd01e7a22020-09-23 22:49:01 +08004266 if (!is_offload_usecase(out->usecase)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304267 ALOGE("%s: A2DP profile is not ready, return error", __func__);
4268 ret = -EAGAIN;
4269 goto error_config;
4270 }
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05304271 }
4272 }
4273 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004274 if (is_sco_out_device_type(&out->device_list)) {
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304275 if (!adev->bt_sco_on) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004276 if (is_speaker_active) {
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304277 //combo usecase just by pass a2dp
4278 ALOGW("%s: SCO is not connected, route it to speaker", __func__);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004279 reassign_device_list(&out->device_list, AUDIO_DEVICE_OUT_SPEAKER, "");
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304280 } else {
4281 ALOGE("%s: SCO profile is not ready, return error", __func__);
4282 ret = -EAGAIN;
4283 goto error_config;
4284 }
4285 }
4286 }
4287
Eric Laurentb23d5282013-05-14 15:27:20 -07004288 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004289 if (out->pcm_device_id < 0) {
4290 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
4291 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08004292 ret = -EINVAL;
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004293 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004294 }
4295
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004296 if (is_haptic_usecase) {
Meng Wang51d8c2a2020-04-27 15:23:21 +08004297 adev->haptic_pcm_device_id = platform_get_pcm_device_id(
4298 USECASE_AUDIO_PLAYBACK_HAPTICS, PCM_PLAYBACK);
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004299 if (adev->haptic_pcm_device_id < 0) {
4300 ALOGE("%s: Invalid Haptics pcm device id(%d) for the usecase(%d)",
4301 __func__, adev->haptic_pcm_device_id, out->usecase);
4302 ret = -EINVAL;
4303 goto error_config;
4304 }
4305 }
4306
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004307 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07004308
4309 if (!uc_info) {
4310 ret = -ENOMEM;
4311 goto error_config;
4312 }
4313
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004314 uc_info->id = out->usecase;
4315 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08004316 uc_info->stream.out = out;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004317 list_init(&uc_info->device_list);
4318 assign_devices(&uc_info->device_list, &out->device_list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07004319 uc_info->in_snd_device = SND_DEVICE_NONE;
4320 uc_info->out_snd_device = SND_DEVICE_NONE;
Garmond Leung5fd0b552018-04-17 11:56:12 -07004321
4322 /* This must be called before adding this usecase to the list */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004323 if (is_usb_out_device_type(&out->device_list)) {
Garmond Leung5fd0b552018-04-17 11:56:12 -07004324 audio_extn_usb_check_and_set_svc_int(uc_info, true);
4325 /* USB backend is not reopened immediately.
4326 This is eventually done as part of select_devices */
4327 }
4328
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08004329 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004330
Wei Wangf7ca6c92017-11-21 14:51:20 -08004331 audio_streaming_hint_start();
Sudheer Papothifa9d2282015-09-17 01:53:25 +05304332 audio_extn_perf_lock_acquire(&adev->perf_lock_handle, 0,
4333 adev->perf_lock_opts,
4334 adev->perf_lock_opts_size);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05304335
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004336 if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
Md Mansoor Ahmeddb1b4f92018-01-25 18:56:31 +05304337 audio_extn_keep_alive_stop(KEEP_ALIVE_OUT_HDMI);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05304338 if (audio_extn_passthru_is_enabled() &&
4339 audio_extn_passthru_is_passthrough_stream(out)) {
4340 audio_extn_passthru_on_start(out);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05304341 }
4342 }
4343
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004344 if (is_a2dp_out_device_type(&out->device_list) &&
Florian Pfister1a84f312018-07-19 14:38:18 +02004345 (!audio_extn_a2dp_source_is_ready())) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304346 if (!a2dp_combo) {
4347 check_a2dp_restore_l(adev, out, false);
4348 } else {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004349 struct listnode dev;
4350 list_init(&dev);
4351 assign_devices(&dev, &out->device_list);
4352 if (compare_device_type(&dev, AUDIO_DEVICE_OUT_SPEAKER_SAFE))
4353 reassign_device_list(&out->device_list,
4354 AUDIO_DEVICE_OUT_SPEAKER_SAFE, "");
Aalique Grahame22e49102018-12-18 14:23:57 -08004355 else
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004356 reassign_device_list(&out->device_list,
4357 AUDIO_DEVICE_OUT_SPEAKER, "");
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304358 select_devices(adev, out->usecase);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004359 assign_devices(&out->device_list, &dev);
Zhenlin Lian4f947842022-05-14 15:50:52 +05304360 clear_devices(&dev);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304361 }
4362 } else {
Revathi Uddarajub26e3932020-06-10 14:51:02 +05304363 select_devices(adev, out->usecase);
4364 if (is_a2dp_out_device_type(&out->device_list) &&
4365 !adev->a2dp_started) {
4366 if (is_speaker_active || is_speaker_safe_active) {
4367 struct listnode dev;
4368 list_init(&dev);
4369 assign_devices(&dev, &out->device_list);
4370 if (compare_device_type(&dev, AUDIO_DEVICE_OUT_SPEAKER_SAFE))
4371 reassign_device_list(&out->device_list,
4372 AUDIO_DEVICE_OUT_SPEAKER_SAFE, "");
4373 else
4374 reassign_device_list(&out->device_list,
4375 AUDIO_DEVICE_OUT_SPEAKER, "");
4376 select_devices(adev, out->usecase);
4377 assign_devices(&out->device_list, &dev);
Zhenlin Lian4f947842022-05-14 15:50:52 +05304378 clear_devices(&dev);
Revathi Uddarajub26e3932020-06-10 14:51:02 +05304379 } else {
4380 ret = -EINVAL;
4381 goto error_open;
4382 }
4383 }
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304384 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07004385
Arun Mirpuri2ce92a62018-09-12 18:36:10 -07004386 if (out->usecase == USECASE_INCALL_MUSIC_UPLINK ||
4387 out->usecase == USECASE_INCALL_MUSIC_UPLINK2) {
Arun Mirpurief53ce52018-09-11 18:00:09 -07004388 voice_set_device_mute_flag(adev, true);
Arun Mirpuri2ce92a62018-09-12 18:36:10 -07004389 }
Arun Mirpurief53ce52018-09-11 18:00:09 -07004390
Derek Chenea197282019-01-07 17:35:01 -08004391 if (audio_extn_ext_hw_plugin_usecase_start(adev->ext_hw_plugin, uc_info))
4392 ALOGE("%s: failed to start ext hw plugin", __func__);
Derek Chend2530072014-11-24 12:39:14 -08004393
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07004394 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d) format(%#x)",
4395 __func__, adev->snd_card, out->pcm_device_id, out->config.format);
Haynes Mathew George16081042017-05-31 17:16:49 -07004396
4397 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
Arun Mirpuri5d170872019-03-26 13:21:31 -07004398 ALOGD("%s: Starting MMAP stream", __func__);
Haynes Mathew George16081042017-05-31 17:16:49 -07004399 if (out->pcm == NULL || !pcm_is_ready(out->pcm)) {
4400 ALOGE("%s: pcm stream not ready", __func__);
4401 goto error_open;
4402 }
4403 ret = pcm_start(out->pcm);
4404 if (ret < 0) {
4405 ALOGE("%s: MMAP pcm_start failed ret %d", __func__, ret);
4406 goto error_open;
4407 }
Arun Mirpuri5d170872019-03-26 13:21:31 -07004408 out_set_mmap_volume(&out->stream, out->volume_l, out->volume_r);
Haynes Mathew George16081042017-05-31 17:16:49 -07004409 } else if (!is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07004410 unsigned int flags = PCM_OUT;
4411 unsigned int pcm_open_retry_count = 0;
4412 if (out->usecase == USECASE_AUDIO_PLAYBACK_AFE_PROXY) {
4413 flags |= PCM_MMAP | PCM_NOIRQ;
4414 pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT;
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004415 } else if (out->realtime) {
Haynes Mathew George4ab3ba92017-12-11 14:49:43 -08004416 flags |= PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07004417 } else
4418 flags |= PCM_MONOTONIC;
4419
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08004420 if ((adev->vr_audio_mode_enabled) &&
4421 (out->flags & AUDIO_OUTPUT_FLAG_RAW)) {
4422 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
4423 "PCM_Dev %d Topology", out->pcm_device_id);
4424 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
4425 if (!ctl) {
4426 ALOGI("%s: Could not get ctl for mixer cmd might be ULL - %s",
4427 __func__, mixer_ctl_name);
4428 } else {
4429 //if success use ULLPP
4430 ALOGI("%s: mixer ctrl %s succeeded setting up ULL for %d",
4431 __func__, mixer_ctl_name, out->pcm_device_id);
4432 //There is a still a possibility that some sessions
4433 // that request for FAST|RAW when 3D audio is active
4434 //can go through ULLPP. Ideally we expects apps to
4435 //listen to audio focus and stop concurrent playback
4436 //Also, we will look for mode flag (voice_in_communication)
4437 //before enabling the realtime flag.
4438 mixer_ctl_set_enum_by_string(ctl, perf_mode[1]);
4439 }
4440 }
4441
Vatsal Bucha3f39f222021-06-29 16:16:55 +05304442 platform_set_stream_channel_map(adev->platform, out->channel_mask,
4443 out->pcm_device_id, -1, &out->channel_map_param.channel_map[0]);
Surendar Karka91fa3682018-07-02 18:12:12 +05304444
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004445 out->pcm = pcm_open_prepare_helper(adev->snd_card, out->pcm_device_id,
4446 flags, pcm_open_retry_count,
4447 &(out->config));
4448 if (out->pcm == NULL) {
4449 ret = -EIO;
4450 goto error_open;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004451 }
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004452
4453 if (is_haptic_usecase) {
4454 adev->haptic_pcm = pcm_open_prepare_helper(adev->snd_card,
4455 adev->haptic_pcm_device_id,
4456 flags, pcm_open_retry_count,
4457 &(adev->haptics_config));
4458 // failure to open haptics pcm shouldnt stop audio,
4459 // so do not close audio pcm in case of error
Vignesh Kulothungane4039c12019-05-07 15:51:39 -07004460
4461 if (property_get_bool("vendor.audio.enable_haptic_audio_sync", false)) {
4462 ALOGD("%s: enable haptic audio synchronization", __func__);
4463 platform_set_qtime(adev->platform, out->pcm_device_id, adev->haptic_pcm_device_id);
4464 }
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004465 }
4466
Zhou Song2b8f28f2017-09-11 10:51:38 +08004467 // apply volume for voip playback after path is set up
4468 if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP)
4469 out_set_voip_volume(&out->stream, out->volume_l, out->volume_r);
Ramu Gottipati36547092018-12-28 11:32:09 +05304470 else if ((out->usecase == USECASE_AUDIO_PLAYBACK_LOW_LATENCY || out->usecase == USECASE_AUDIO_PLAYBACK_DEEP_BUFFER ||
4471 out->usecase == USECASE_AUDIO_PLAYBACK_ULL) && (out->apply_volume)) {
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05304472 out_set_pcm_volume(&out->stream, out->volume_l, out->volume_r);
4473 out->apply_volume = false;
Derek Chenf13dd492018-11-13 14:53:51 -08004474 } else if (audio_extn_auto_hal_is_bus_device_usecase(out->usecase)) {
4475 out_set_pcm_volume(&out->stream, out->volume_l, out->volume_r);
Krishna Kishor Jha10ad39f2022-02-21 17:02:47 +05304476#ifdef SOFT_VOLUME
4477 out_set_soft_volume_params(&out->stream);
4478#endif
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05304479 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004480 } else {
Zhou Song79929fe2020-01-20 17:57:43 +08004481 /*
4482 * set custom channel map if:
4483 * 1. neither mono nor stereo clips i.e. channels > 2 OR
4484 * 2. custom channel map has been set by client
4485 * else default channel map of FC/FR/FL can always be set to DSP
4486 */
4487 if (popcount(out->channel_mask) > 2 || out->channel_map_param.channel_map[0])
4488 platform_set_stream_channel_map(adev->platform, out->channel_mask,
Weiyin Jiang810a80d2021-01-18 16:04:31 +08004489 out->pcm_device_id, -1, &out->channel_map_param.channel_map[0]);
Xiaojun Sang785b5da2017-08-03 15:52:29 +08004490 audio_enable_asm_bit_width_enforce_mode(adev->mixer,
4491 adev->dsp_bit_width_enforce_mode,
4492 true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004493 out->pcm = NULL;
Haynes Mathew George380745d2017-10-04 15:27:45 -07004494 ATRACE_BEGIN("compress_open");
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08004495 out->compr = compress_open(adev->snd_card,
4496 out->pcm_device_id,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004497 COMPRESS_IN, &out->compr_config);
Haynes Mathew George380745d2017-10-04 15:27:45 -07004498 ATRACE_END();
Satish Babu Patakokila54ce83d2018-07-06 18:00:37 +05304499 if (errno == ENETRESET && !is_compress_ready(out->compr)) {
Sharad Sanglec6f32552018-05-04 16:15:38 +05304500 ALOGE("%s: compress_open failed errno:%d\n", __func__, errno);
4501 adev->card_status = CARD_STATUS_OFFLINE;
4502 out->card_status = CARD_STATUS_OFFLINE;
4503 ret = -EIO;
4504 goto error_open;
4505 }
4506
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004507 if (out->compr && !is_compress_ready(out->compr)) {
Haynes Mathew Georgeee5836f2017-11-21 18:02:10 -08004508 ALOGE("%s: failed /w error %s", __func__, compress_get_error(out->compr));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004509 compress_close(out->compr);
4510 out->compr = NULL;
4511 ret = -EIO;
4512 goto error_open;
4513 }
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05304514 /* compress_open sends params of the track, so reset the flag here */
4515 out->is_compr_metadata_avail = false;
4516
Ben Rombergerd771a7c2017-02-22 18:05:17 -08004517 if (out->client_callback)
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004518 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07004519
Fred Oh3f43e742015-03-04 18:42:34 -08004520 /* Since small bufs uses blocking writes, a write will be blocked
4521 for the default max poll time (20s) in the event of an SSR.
4522 Reduce the poll time to observe and deal with SSR faster.
4523 */
Ashish Jain5106d362016-05-11 19:23:33 +05304524 if (!out->non_blocking) {
Fred Oh3f43e742015-03-04 18:42:34 -08004525 compress_set_max_poll_wait(out->compr, 1000);
4526 }
4527
Manish Dewangan69426c82017-01-30 17:35:36 +05304528 audio_extn_utils_compress_set_render_mode(out);
Manish Dewangan58229382017-02-02 15:48:41 +05304529 audio_extn_utils_compress_set_clk_rec_mode(uc_info);
Manish Dewangan69426c82017-01-30 17:35:36 +05304530
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08004531 audio_extn_dts_create_state_notifier_node(out->usecase);
4532 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
4533 popcount(out->channel_mask),
4534 out->playback_started);
4535
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08004536#ifdef DS1_DOLBY_DDP_ENABLED
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05304537 if (audio_extn_utils_is_dolby_format(out->format))
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08004538 audio_extn_dolby_send_ddp_endp_params(adev);
4539#endif
Preetam Singh Ranawatd18d8832017-02-08 17:34:54 +05304540 if (!(audio_extn_passthru_is_passthrough_stream(out)) &&
4541 (out->sample_rate != 176400 && out->sample_rate <= 192000)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004542 if (adev->visualizer_start_output != NULL)
4543 adev->visualizer_start_output(out->handle, out->pcm_device_id);
4544 if (adev->offload_effects_start_output != NULL)
Ashish Jain5106d362016-05-11 19:23:33 +05304545 adev->offload_effects_start_output(out->handle, out->pcm_device_id, adev->mixer);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08004546 audio_extn_check_and_set_dts_hpx_state(adev);
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004547 }
Derek Chenf13dd492018-11-13 14:53:51 -08004548
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004549 if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_BUS)) {
Derek Chenf13dd492018-11-13 14:53:51 -08004550 /* Update cached volume from media to offload/direct stream */
4551 struct listnode *node = NULL;
4552 list_for_each(node, &adev->active_outputs_list) {
4553 streams_output_ctxt_t *out_ctxt = node_to_item(node,
4554 streams_output_ctxt_t,
4555 list);
4556 if (out_ctxt->output->usecase == USECASE_AUDIO_PLAYBACK_MEDIA) {
4557 out->volume_l = out_ctxt->output->volume_l;
4558 out->volume_r = out_ctxt->output->volume_r;
4559 }
4560 }
4561 out_set_compr_volume(&out->stream,
4562 out->volume_l, out->volume_r);
4563 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004564 }
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004565
4566 if (ret == 0) {
Vaibhav Raut5f3bf222023-02-06 17:45:22 +05304567 if (out->flags & (AUDIO_OUTPUT_FLAG_FAST | AUDIO_OUTPUT_FLAG_RAW))
Narsinga Rao Chellaa6e1f702021-10-22 13:57:32 +05304568 register_out_stream(out);
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004569 if (out->realtime) {
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07004570 if (out->pcm == NULL || !pcm_is_ready(out->pcm)) {
4571 ALOGE("%s: pcm stream not ready", __func__);
4572 goto error_open;
4573 }
Haynes Mathew George380745d2017-10-04 15:27:45 -07004574 ATRACE_BEGIN("pcm_start");
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004575 ret = pcm_start(out->pcm);
Haynes Mathew George380745d2017-10-04 15:27:45 -07004576 ATRACE_END();
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004577 if (ret < 0)
4578 goto error_open;
4579 }
4580 }
Wei Wangf7ca6c92017-11-21 14:51:20 -08004581 audio_streaming_hint_end();
Sudheer Papothifa9d2282015-09-17 01:53:25 +05304582 audio_extn_perf_lock_release(&adev->perf_lock_handle);
Jaideep Sharma0fa53812020-09-17 09:00:11 +05304583 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07004584
vivek mehtad15d2bf2019-05-17 13:35:10 -07004585 if (out->usecase == USECASE_AUDIO_PLAYBACK_ULL ||
4586 out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
4587 audio_low_latency_hint_start();
4588 }
4589
Manish Dewangan21a850a2017-08-14 12:03:55 +05304590 if (out->ip_hdlr_handle) {
Vidyakumar Athota6d655882017-05-22 18:26:24 -07004591 ret = audio_extn_ip_hdlr_intf_open(out->ip_hdlr_handle, true, out, out->usecase);
Naresh Tanniru85819452017-05-04 18:55:45 -07004592 if (ret < 0)
4593 ALOGE("%s: audio_extn_ip_hdlr_intf_open failed %d",__func__, ret);
4594 }
4595
Vignesh Kulothungan3b5fae52017-09-25 12:16:30 -07004596 // consider a scenario where on pause lower layers are tear down.
4597 // so on resume, swap mixer control need to be sent only when
4598 // backend is active, hence rather than sending from enable device
4599 // sending it from start of streamtream
4600
4601 platform_set_swap_channels(adev, true);
4602
Haynes Mathew George380745d2017-10-04 15:27:45 -07004603 ATRACE_END();
Vatsal Buchac09ae062018-11-14 13:25:08 +05304604 enable_gcov();
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004605 return ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004606error_open:
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004607 if (adev->haptic_pcm) {
4608 pcm_close(adev->haptic_pcm);
4609 adev->haptic_pcm = NULL;
4610 }
Wei Wangf7ca6c92017-11-21 14:51:20 -08004611 audio_streaming_hint_end();
Sudheer Papothifa9d2282015-09-17 01:53:25 +05304612 audio_extn_perf_lock_release(&adev->perf_lock_handle);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004613 stop_output_stream(out);
Mingshu Pang5fc696f2020-02-28 12:32:00 +08004614error_fatal:
Laxminath Kasam2cb4b752015-09-24 03:59:15 +05304615 /*
4616 * sleep 50ms to allow sufficient time for kernel
4617 * drivers to recover incases like SSR.
4618 */
4619 usleep(50000);
Mingshu Pang5fc696f2020-02-28 12:32:00 +08004620error_config:
Haynes Mathew George380745d2017-10-04 15:27:45 -07004621 ATRACE_END();
Vatsal Buchac09ae062018-11-14 13:25:08 +05304622 enable_gcov();
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08004623 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004624}
4625
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004626static int check_input_parameters(uint32_t sample_rate,
4627 audio_format_t format,
Aalique Grahame22e49102018-12-18 14:23:57 -08004628 int channel_count,
4629 bool is_usb_hifi)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004630{
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08004631 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004632
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05304633 if (((format != AUDIO_FORMAT_PCM_16_BIT) && (format != AUDIO_FORMAT_PCM_8_24_BIT) &&
4634 (format != AUDIO_FORMAT_PCM_24_BIT_PACKED) && (format != AUDIO_FORMAT_PCM_32_BIT) &&
4635 (format != AUDIO_FORMAT_PCM_FLOAT)) &&
Mingming Yine62d7842013-10-25 16:26:03 -07004636 !voice_extn_compress_voip_is_format_supported(format) &&
Ralf Herzaec80262018-07-03 07:08:49 +02004637 !audio_extn_compr_cap_format_supported(format) &&
4638 !audio_extn_cin_format_supported(format))
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004639 ret = -EINVAL;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08004640
Aalique Grahame22e49102018-12-18 14:23:57 -08004641 int max_channel_count = is_usb_hifi ? MAX_HIFI_CHANNEL_COUNT : MAX_CHANNEL_COUNT;
4642 if ((channel_count < MIN_CHANNEL_COUNT) || (channel_count > max_channel_count)) {
4643 ALOGE("%s: unsupported channel count (%d) passed Min / Max (%d / %d)", __func__,
4644 channel_count, MIN_CHANNEL_COUNT, max_channel_count);
4645 return -EINVAL;
4646 }
4647
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08004648 switch (channel_count) {
4649 case 1:
4650 case 2:
Chaithanya Krishna Bacharaju9955b162016-05-25 16:25:53 +05304651 case 3:
4652 case 4:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08004653 case 6:
Karthikeyan Mani07faa602018-08-20 11:01:32 -07004654 case 8:
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05304655 case 10:
4656 case 12:
4657 case 14:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08004658 break;
4659 default:
4660 ret = -EINVAL;
4661 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004662
4663 switch (sample_rate) {
4664 case 8000:
4665 case 11025:
4666 case 12000:
4667 case 16000:
4668 case 22050:
4669 case 24000:
4670 case 32000:
4671 case 44100:
4672 case 48000:
Haynes Mathew Georgec9253d12017-12-13 15:58:28 -08004673 case 88200:
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05304674 case 96000:
Haynes Mathew Georgec9253d12017-12-13 15:58:28 -08004675 case 176400:
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05304676 case 192000:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004677 break;
4678 default:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08004679 ret = -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004680 }
4681
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08004682 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004683}
4684
Naresh Tanniru04f71882018-06-26 17:46:22 +05304685
4686/** Add a value in a list if not already present.
4687 * @return true if value was successfully inserted or already present,
4688 * false if the list is full and does not contain the value.
4689 */
4690static bool register_uint(uint32_t value, uint32_t* list, size_t list_length) {
4691 for (size_t i = 0; i < list_length; i++) {
4692 if (list[i] == value) return true; // value is already present
4693 if (list[i] == 0) { // no values in this slot
4694 list[i] = value;
4695 return true; // value inserted
4696 }
4697 }
4698 return false; // could not insert value
4699}
4700
4701/** Add channel_mask in supported_channel_masks if not already present.
4702 * @return true if channel_mask was successfully inserted or already present,
4703 * false if supported_channel_masks is full and does not contain channel_mask.
4704 */
4705static void register_channel_mask(audio_channel_mask_t channel_mask,
4706 audio_channel_mask_t supported_channel_masks[static MAX_SUPPORTED_CHANNEL_MASKS]) {
4707 ALOGE_IF(!register_uint(channel_mask, supported_channel_masks, MAX_SUPPORTED_CHANNEL_MASKS),
4708 "%s: stream can not declare supporting its channel_mask %x", __func__, channel_mask);
4709}
4710
4711/** Add format in supported_formats if not already present.
4712 * @return true if format was successfully inserted or already present,
4713 * false if supported_formats is full and does not contain format.
4714 */
4715static void register_format(audio_format_t format,
4716 audio_format_t supported_formats[static MAX_SUPPORTED_FORMATS]) {
4717 ALOGE_IF(!register_uint(format, supported_formats, MAX_SUPPORTED_FORMATS),
4718 "%s: stream can not declare supporting its format %x", __func__, format);
4719}
4720/** Add sample_rate in supported_sample_rates if not already present.
4721 * @return true if sample_rate was successfully inserted or already present,
4722 * false if supported_sample_rates is full and does not contain sample_rate.
4723 */
4724static void register_sample_rate(uint32_t sample_rate,
4725 uint32_t supported_sample_rates[static MAX_SUPPORTED_SAMPLE_RATES]) {
4726 ALOGE_IF(!register_uint(sample_rate, supported_sample_rates, MAX_SUPPORTED_SAMPLE_RATES),
4727 "%s: stream can not declare supporting its sample rate %x", __func__, sample_rate);
4728}
4729
Karthikeyan Manib38769c2018-11-07 15:44:13 -08004730static inline uint32_t lcm(uint32_t num1, uint32_t num2)
4731{
4732 uint32_t high = num1, low = num2, temp = 0;
4733
4734 if (!num1 || !num2)
4735 return 0;
4736
4737 if (num1 < num2) {
4738 high = num2;
4739 low = num1;
4740 }
4741
4742 while (low != 0) {
4743 temp = low;
4744 low = high % low;
4745 high = temp;
4746 }
4747 return (num1 * num2)/high;
4748}
4749
4750static inline uint32_t nearest_multiple(uint32_t num, uint32_t multiplier)
4751{
4752 uint32_t remainder = 0;
4753
4754 if (!multiplier)
4755 return num;
4756
4757 remainder = num % multiplier;
4758 if (remainder)
4759 num += (multiplier - remainder);
4760
4761 return num;
4762}
4763
Aalique Grahame22e49102018-12-18 14:23:57 -08004764static size_t get_stream_buffer_size(size_t duration_ms,
4765 uint32_t sample_rate,
4766 audio_format_t format,
4767 int channel_count,
4768 bool is_low_latency)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004769{
4770 size_t size = 0;
Karthikeyan Manib38769c2018-11-07 15:44:13 -08004771 uint32_t bytes_per_period_sample = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004772
Aalique Grahame22e49102018-12-18 14:23:57 -08004773 size = (sample_rate * duration_ms) / 1000;
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +05304774 if (is_low_latency){
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +05304775#ifndef PLATFORM_AUTO
4776 size = configured_low_latency_capture_period_size;
4777#else
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +05304778 switch(sample_rate) {
4779 case 48000:
4780 size = 240;
4781 break;
4782 case 32000:
4783 size = 160;
4784 break;
4785 case 24000:
4786 size = 120;
4787 break;
4788 case 16000:
4789 size = 80;
4790 break;
4791 case 8000:
4792 size = 40;
4793 break;
4794 default:
4795 size = 240;
4796 }
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +05304797#endif
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +05304798 }
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05304799
Karthikeyan Manib38769c2018-11-07 15:44:13 -08004800 bytes_per_period_sample = audio_bytes_per_sample(format) * channel_count;
Aalique Grahame22e49102018-12-18 14:23:57 -08004801 size *= audio_bytes_per_sample(format) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004802
Ralf Herzbd08d632018-09-28 15:50:49 +02004803 /* make sure the size is multiple of 32 bytes and additionally multiple of
4804 * the frame_size (required for 24bit samples and non-power-of-2 channel counts)
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07004805 * At 48 kHz mono 16-bit PCM:
4806 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
4807 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
Karthikeyan Manib38769c2018-11-07 15:44:13 -08004808 * Also, make sure the size is multiple of bytes per period sample
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07004809 */
Karthikeyan Manib38769c2018-11-07 15:44:13 -08004810 size = nearest_multiple(size, lcm(32, bytes_per_period_sample));
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07004811
4812 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004813}
4814
Aalique Grahame22e49102018-12-18 14:23:57 -08004815static size_t get_input_buffer_size(uint32_t sample_rate,
4816 audio_format_t format,
4817 int channel_count,
4818 bool is_low_latency)
4819{
Avinash Chandrad7296d42021-08-04 15:07:47 +05304820 bool is_usb_hifi = IS_USB_HIFI;
Aalique Grahame22e49102018-12-18 14:23:57 -08004821 /* Don't know if USB HIFI in this context so use true to be conservative */
4822 if (check_input_parameters(sample_rate, format, channel_count,
Avinash Chandrad7296d42021-08-04 15:07:47 +05304823 is_usb_hifi) != 0)
Aalique Grahame22e49102018-12-18 14:23:57 -08004824 return 0;
4825
4826 return get_stream_buffer_size(AUDIO_CAPTURE_PERIOD_DURATION_MSEC,
4827 sample_rate,
4828 format,
4829 channel_count,
4830 is_low_latency);
4831}
4832
Derek Chenf6318be2017-06-12 17:16:24 -04004833size_t get_output_period_size(uint32_t sample_rate,
4834 audio_format_t format,
4835 int channel_count,
4836 int duration /*in millisecs*/)
Ashish Jain058165c2016-09-28 23:18:48 +05304837{
4838 size_t size = 0;
4839 uint32_t bytes_per_sample = audio_bytes_per_sample(format);
4840
4841 if ((duration == 0) || (sample_rate == 0) ||
4842 (bytes_per_sample == 0) || (channel_count == 0)) {
4843 ALOGW("Invalid config duration %d sr %d bps %d ch %d", duration, sample_rate,
4844 bytes_per_sample, channel_count);
4845 return -EINVAL;
4846 }
4847
4848 size = (sample_rate *
4849 duration *
4850 bytes_per_sample *
4851 channel_count) / 1000;
4852 /*
4853 * To have same PCM samples for all channels, the buffer size requires to
4854 * be multiple of (number of channels * bytes per sample)
4855 * For writes to succeed, the buffer must be written at address which is multiple of 32
4856 */
4857 size = ALIGN(size, (bytes_per_sample * channel_count * 32));
4858
4859 return (size/(channel_count * bytes_per_sample));
4860}
4861
Zhou Song48453a02018-01-10 17:50:59 +08004862static uint64_t get_actual_pcm_frames_rendered(struct stream_out *out, struct timespec *timestamp)
Ashish Jain5106d362016-05-11 19:23:33 +05304863{
4864 uint64_t actual_frames_rendered = 0;
Weiyin Jiang4813da12020-05-28 00:37:28 +08004865 uint64_t written_frames = 0;
4866 uint64_t kernel_frames = 0;
4867 uint64_t dsp_frames = 0;
4868 uint64_t signed_frames = 0;
4869 size_t kernel_buffer_size = 0;
Ashish Jain5106d362016-05-11 19:23:33 +05304870
4871 /* This adjustment accounts for buffering after app processor.
4872 * It is based on estimated DSP latency per use case, rather than exact.
4873 */
George Gao9ba8a142020-07-23 14:30:03 -07004874 dsp_frames = platform_render_latency(out) *
Weiyin Jiang4813da12020-05-28 00:37:28 +08004875 out->sample_rate / 1000000LL;
Ashish Jain5106d362016-05-11 19:23:33 +05304876
Zhou Song48453a02018-01-10 17:50:59 +08004877 pthread_mutex_lock(&out->position_query_lock);
Weiyin Jiang4813da12020-05-28 00:37:28 +08004878 written_frames = out->written /
4879 (audio_bytes_per_sample(out->hal_ip_format) * popcount(out->channel_mask));
4880
Ashish Jain5106d362016-05-11 19:23:33 +05304881 /* not querying actual state of buffering in kernel as it would involve an ioctl call
4882 * which then needs protection, this causes delay in TS query for pcm_offload usecase
4883 * hence only estimate.
4884 */
Weiyin Jiang4813da12020-05-28 00:37:28 +08004885 kernel_buffer_size = out->compr_config.fragment_size * out->compr_config.fragments;
4886 kernel_frames = kernel_buffer_size /
4887 (audio_bytes_per_sample(out->hal_op_format) * popcount(out->channel_mask));
Ashish Jain5106d362016-05-11 19:23:33 +05304888
Weiyin Jiang4813da12020-05-28 00:37:28 +08004889 if (written_frames >= (kernel_frames + dsp_frames))
4890 signed_frames = written_frames - kernel_frames - dsp_frames;
Ashish Jain5106d362016-05-11 19:23:33 +05304891
Zhou Song48453a02018-01-10 17:50:59 +08004892 if (signed_frames > 0) {
Ashish Jain5106d362016-05-11 19:23:33 +05304893 actual_frames_rendered = signed_frames;
Zhou Song48453a02018-01-10 17:50:59 +08004894 if (timestamp != NULL )
4895 *timestamp = out->writeAt;
4896 } else if (timestamp != NULL) {
4897 clock_gettime(CLOCK_MONOTONIC, timestamp);
4898 }
4899 pthread_mutex_unlock(&out->position_query_lock);
Ashish Jain5106d362016-05-11 19:23:33 +05304900
Weiyin Jiang4813da12020-05-28 00:37:28 +08004901 ALOGVV("%s signed frames %lld written frames %lld kernel frames %lld dsp frames %lld",
4902 __func__, signed_frames, written_frames, kernel_frames, dsp_frames);
Ashish Jain5106d362016-05-11 19:23:33 +05304903
4904 return actual_frames_rendered;
4905}
4906
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004907static uint32_t out_get_sample_rate(const struct audio_stream *stream)
4908{
4909 struct stream_out *out = (struct stream_out *)stream;
4910
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004911 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004912}
4913
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07004914static int out_set_sample_rate(struct audio_stream *stream __unused,
4915 uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004916{
4917 return -ENOSYS;
4918}
4919
4920static size_t out_get_buffer_size(const struct audio_stream *stream)
4921{
4922 struct stream_out *out = (struct stream_out *)stream;
4923
Varun Balaraje49253e2017-07-06 19:48:56 +05304924 if (is_interactive_usecase(out->usecase)) {
Sri Karri27279e12017-08-07 16:05:20 +05304925 return out->config.period_size * out->config.period_count;
Varun Balaraje49253e2017-07-06 19:48:56 +05304926 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Naresh Tanniruee3499a2017-01-05 14:05:35 +05304927 if (out->flags & AUDIO_OUTPUT_FLAG_TIMESTAMP)
4928 return out->compr_config.fragment_size - sizeof(struct snd_codec_metadata);
4929 else
4930 return out->compr_config.fragment_size;
4931 } else if(out->usecase == USECASE_COMPRESS_VOIP_CALL)
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08004932 return voice_extn_compress_voip_out_get_buffer_size(out);
Dhananjay Kumarac341582017-02-23 23:42:25 +05304933 else if (is_offload_usecase(out->usecase) &&
4934 out->flags == AUDIO_OUTPUT_FLAG_DIRECT)
Ashish Jain83a6cc22016-06-28 14:34:17 +05304935 return out->hal_fragment_size;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004936
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004937 return out->config.period_size * out->af_period_multiplier *
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07004938 audio_stream_out_frame_size((const struct audio_stream_out *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004939}
4940
4941static uint32_t out_get_channels(const struct audio_stream *stream)
4942{
4943 struct stream_out *out = (struct stream_out *)stream;
4944
4945 return out->channel_mask;
4946}
4947
4948static audio_format_t out_get_format(const struct audio_stream *stream)
4949{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004950 struct stream_out *out = (struct stream_out *)stream;
4951
4952 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004953}
4954
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07004955static int out_set_format(struct audio_stream *stream __unused,
4956 audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004957{
4958 return -ENOSYS;
4959}
4960
4961static int out_standby(struct audio_stream *stream)
4962{
4963 struct stream_out *out = (struct stream_out *)stream;
4964 struct audio_device *adev = out->dev;
Haynes Mathew George16081042017-05-31 17:16:49 -07004965 bool do_stop = true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004966
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05304967 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
4968 stream, out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004969
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07004970 lock_output_stream(out);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004971 if (!out->standby) {
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07004972 if (adev->adm_deregister_stream)
4973 adev->adm_deregister_stream(adev->adm_data, out->handle);
4974
Weiyin Jiang280ea742020-09-08 20:28:22 +08004975 if (is_offload_usecase(out->usecase)) {
Haynes Mathew George7fce0a52016-06-23 18:22:27 -07004976 stop_compressed_output_l(out);
Weiyin Jiang280ea742020-09-08 20:28:22 +08004977 }
Haynes Mathew George7fce0a52016-06-23 18:22:27 -07004978
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08004979 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004980 out->standby = true;
Zhou Songa8895042016-07-05 17:54:22 +08004981 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
4982 voice_extn_compress_voip_close_output_stream(stream);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304983 out->started = 0;
Zhou Songa8895042016-07-05 17:54:22 +08004984 pthread_mutex_unlock(&adev->lock);
4985 pthread_mutex_unlock(&out->lock);
4986 ALOGD("VOIP output entered standby");
4987 return 0;
4988 } else if (!is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004989 if (out->pcm) {
4990 pcm_close(out->pcm);
4991 out->pcm = NULL;
4992 }
Meng Wanga09da002020-04-20 12:56:04 +08004993 if (out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) {
4994 if (adev->haptic_pcm) {
4995 pcm_close(adev->haptic_pcm);
4996 adev->haptic_pcm = NULL;
4997 }
4998
4999 if (adev->haptic_buffer != NULL) {
5000 free(adev->haptic_buffer);
5001 adev->haptic_buffer = NULL;
5002 adev->haptic_buffer_size = 0;
5003 }
5004 adev->haptic_pcm_device_id = 0;
5005 }
5006
Haynes Mathew George16081042017-05-31 17:16:49 -07005007 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
5008 do_stop = out->playback_started;
5009 out->playback_started = false;
Phil Burkd898ba62019-06-20 12:49:01 -07005010
5011 if (out->mmap_shared_memory_fd >= 0) {
5012 ALOGV("%s: closing mmap_shared_memory_fd = %d",
5013 __func__, out->mmap_shared_memory_fd);
5014 close(out->mmap_shared_memory_fd);
5015 out->mmap_shared_memory_fd = -1;
5016 }
Haynes Mathew George16081042017-05-31 17:16:49 -07005017 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005018 } else {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07005019 ALOGD("copl(%p):standby", out);
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05305020 out->send_next_track_params = false;
5021 out->is_compr_metadata_avail = false;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005022 out->gapless_mdata.encoder_delay = 0;
5023 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005024 if (out->compr != NULL) {
5025 compress_close(out->compr);
5026 out->compr = NULL;
5027 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08005028 }
Haynes Mathew George16081042017-05-31 17:16:49 -07005029 if (do_stop) {
5030 stop_output_stream(out);
5031 }
Lakshman Chaluvaraju06677b42019-06-24 10:04:52 +05305032 // if fm is active route on selected device in UI
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005033 audio_extn_fm_route_on_selected_device(adev, &out->device_list);
Eric Laurent150dbfe2013-02-27 14:31:02 -08005034 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005035 }
5036 pthread_mutex_unlock(&out->lock);
Aniket Kumar Latad13758f2020-08-06 15:11:36 -07005037 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005038 return 0;
5039}
5040
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305041static int out_on_error(struct audio_stream *stream)
5042{
5043 struct stream_out *out = (struct stream_out *)stream;
Ben Rombergerfd02a2f2018-09-17 10:23:23 -07005044 int status = 0;
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305045
5046 lock_output_stream(out);
Haynes Mathew Georgeee5836f2017-11-21 18:02:10 -08005047 // always send CMD_ERROR for offload streams, this
5048 // is needed e.g. when SSR happens within compress_open
5049 // since the stream is active, offload_callback_thread is also active.
5050 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
5051 stop_compressed_output_l(out);
Ben Rombergerfd02a2f2018-09-17 10:23:23 -07005052 }
5053 pthread_mutex_unlock(&out->lock);
5054
5055 status = out_standby(&out->stream.common);
5056
5057 lock_output_stream(out);
5058 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Haynes Mathew Georgeee5836f2017-11-21 18:02:10 -08005059 send_offload_cmd_l(out, OFFLOAD_CMD_ERROR);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305060 }
Manisha Agarwal7b3e3772019-02-20 14:33:45 +05305061
5062 if (is_offload_usecase(out->usecase) && out->card_status == CARD_STATUS_OFFLINE) {
5063 ALOGD("Setting previous card status if offline");
5064 out->prev_card_status_offline = true;
5065 }
5066
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305067 pthread_mutex_unlock(&out->lock);
5068
Ben Rombergerfd02a2f2018-09-17 10:23:23 -07005069 return status;
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305070}
5071
Ashish Jain1b9b30c2017-05-18 20:57:40 +05305072/*
Weiyin Jiang280ea742020-09-08 20:28:22 +08005073 * standby implementation without locks, assumes that the callee already
5074 * has taken adev and out lock.
Ashish Jain1b9b30c2017-05-18 20:57:40 +05305075 */
5076int out_standby_l(struct audio_stream *stream)
5077{
5078 struct stream_out *out = (struct stream_out *)stream;
5079 struct audio_device *adev = out->dev;
5080
5081 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
5082 stream, out->usecase, use_case_table[out->usecase]);
5083
5084 if (!out->standby) {
Haynes Mathew George380745d2017-10-04 15:27:45 -07005085 ATRACE_BEGIN("out_standby_l");
Ashish Jain1b9b30c2017-05-18 20:57:40 +05305086 if (adev->adm_deregister_stream)
5087 adev->adm_deregister_stream(adev->adm_data, out->handle);
5088
Weiyin Jiang280ea742020-09-08 20:28:22 +08005089 if (is_offload_usecase(out->usecase)) {
Ashish Jain1b9b30c2017-05-18 20:57:40 +05305090 stop_compressed_output_l(out);
Weiyin Jiang280ea742020-09-08 20:28:22 +08005091 }
Ashish Jain1b9b30c2017-05-18 20:57:40 +05305092
5093 out->standby = true;
5094 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
5095 voice_extn_compress_voip_close_output_stream(stream);
5096 out->started = 0;
5097 ALOGD("VOIP output entered standby");
Haynes Mathew George380745d2017-10-04 15:27:45 -07005098 ATRACE_END();
Ashish Jain1b9b30c2017-05-18 20:57:40 +05305099 return 0;
5100 } else if (!is_offload_usecase(out->usecase)) {
5101 if (out->pcm) {
5102 pcm_close(out->pcm);
5103 out->pcm = NULL;
5104 }
Vignesh Kulothungana6927272019-02-20 15:17:07 -08005105 if (out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) {
5106 if (adev->haptic_pcm) {
5107 pcm_close(adev->haptic_pcm);
5108 adev->haptic_pcm = NULL;
5109 }
5110
5111 if (adev->haptic_buffer != NULL) {
5112 free(adev->haptic_buffer);
5113 adev->haptic_buffer = NULL;
5114 adev->haptic_buffer_size = 0;
5115 }
5116 adev->haptic_pcm_device_id = 0;
5117 }
Ashish Jain1b9b30c2017-05-18 20:57:40 +05305118 } else {
5119 ALOGD("copl(%p):standby", out);
5120 out->send_next_track_params = false;
5121 out->is_compr_metadata_avail = false;
5122 out->gapless_mdata.encoder_delay = 0;
5123 out->gapless_mdata.encoder_padding = 0;
5124 if (out->compr != NULL) {
5125 compress_close(out->compr);
5126 out->compr = NULL;
5127 }
5128 }
5129 stop_output_stream(out);
Haynes Mathew George380745d2017-10-04 15:27:45 -07005130 ATRACE_END();
Ashish Jain1b9b30c2017-05-18 20:57:40 +05305131 }
Aniket Kumar Latad13758f2020-08-06 15:11:36 -07005132 ALOGV("%s: exit", __func__);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05305133 return 0;
5134}
5135
Aalique Grahame22e49102018-12-18 14:23:57 -08005136static int out_dump(const struct audio_stream *stream, int fd)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005137{
Aalique Grahame22e49102018-12-18 14:23:57 -08005138 struct stream_out *out = (struct stream_out *)stream;
5139
5140 // We try to get the lock for consistency,
5141 // but it isn't necessary for these variables.
5142 // If we're not in standby, we may be blocked on a write.
5143 const bool locked = (pthread_mutex_trylock(&out->lock) == 0);
5144 dprintf(fd, " Standby: %s\n", out->standby ? "yes" : "no");
5145 dprintf(fd, " Frames written: %lld\n", (long long)out->written);
Dechen Chai22768452021-07-30 09:29:16 +05305146#ifndef LINUX_ENABLED
Andy Hunga1f48fa2019-07-01 18:14:53 -07005147 char buffer[256]; // for statistics formatting
5148 if (!is_offload_usecase(out->usecase)) {
5149 simple_stats_to_string(&out->fifo_underruns, buffer, sizeof(buffer));
5150 dprintf(fd, " Fifo frame underruns: %s\n", buffer);
5151 }
5152
Andy Hungc6bfd4a2019-07-01 18:26:00 -07005153 if (out->start_latency_ms.n > 0) {
5154 simple_stats_to_string(&out->start_latency_ms, buffer, sizeof(buffer));
5155 dprintf(fd, " Start latency ms: %s\n", buffer);
5156 }
Dechen Chai22768452021-07-30 09:29:16 +05305157#endif
Aalique Grahame22e49102018-12-18 14:23:57 -08005158 if (locked) {
5159 pthread_mutex_unlock(&out->lock);
5160 }
5161
Dechen Chai22768452021-07-30 09:29:16 +05305162#ifndef LINUX_ENABLED
Aalique Grahame22e49102018-12-18 14:23:57 -08005163 // dump error info
5164 (void)error_log_dump(
5165 out->error_log, fd, " " /* prefix */, 0 /* lines */, 0 /* limit_ns */);
Dechen Chai22768452021-07-30 09:29:16 +05305166#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005167 return 0;
5168}
5169
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005170static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
5171{
5172 int ret = 0;
5173 char value[32];
ApurupaPattapu2e084df2013-12-18 15:47:59 -08005174
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005175 if (!out || !parms) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08005176 ALOGE("%s: return invalid ",__func__);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005177 return -EINVAL;
5178 }
5179
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +05305180 ret = audio_extn_parse_compress_metadata(out, parms);
Weiyin Jiang18ac4e92015-03-15 15:03:40 +08005181
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005182 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
5183 if (ret >= 0) {
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +05305184 out->gapless_mdata.encoder_delay = atoi(value); //whats a good limit check?
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005185 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005186 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
5187 if (ret >= 0) {
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +05305188 out->gapless_mdata.encoder_padding = atoi(value);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005189 }
5190
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005191 ALOGV("%s new encoder delay %u and padding %u", __func__,
5192 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
5193
5194 return 0;
5195}
5196
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07005197static bool output_drives_call(struct audio_device *adev, struct stream_out *out)
5198{
5199 return out == adev->primary_output || out == adev->voice_tx_output;
5200}
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005201
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305202// note: this call is safe only if the stream_cb is
5203// removed first in close_output_stream (as is done now).
5204static void out_snd_mon_cb(void * stream, struct str_parms * parms)
5205{
5206 if (!stream || !parms)
5207 return;
5208
5209 struct stream_out *out = (struct stream_out *)stream;
5210 struct audio_device *adev = out->dev;
5211
5212 card_status_t status;
5213 int card;
5214 if (parse_snd_card_status(parms, &card, &status) < 0)
5215 return;
5216
5217 pthread_mutex_lock(&adev->lock);
5218 bool valid_cb = (card == adev->snd_card);
5219 pthread_mutex_unlock(&adev->lock);
5220
5221 if (!valid_cb)
5222 return;
5223
5224 lock_output_stream(out);
5225 if (out->card_status != status)
5226 out->card_status = status;
5227 pthread_mutex_unlock(&out->lock);
5228
5229 ALOGI("out_snd_mon_cb for card %d usecase %s, status %s", card,
5230 use_case_table[out->usecase],
5231 status == CARD_STATUS_OFFLINE ? "offline" : "online");
5232
Aditya Bavanari59ebed42019-02-05 17:44:57 +05305233 if (status == CARD_STATUS_OFFLINE) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305234 out_on_error(stream);
Aditya Bavanari59ebed42019-02-05 17:44:57 +05305235 if (voice_is_call_state_active(adev) &&
5236 out == adev->primary_output) {
5237 ALOGD("%s: SSR/PDR occurred, end all calls\n", __func__);
5238 pthread_mutex_lock(&adev->lock);
5239 voice_stop_call(adev);
5240 adev->mode = AUDIO_MODE_NORMAL;
5241 pthread_mutex_unlock(&adev->lock);
5242 }
5243 }
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305244 return;
5245}
5246
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005247int route_output_stream(struct stream_out *out,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005248 struct listnode *devices)
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005249{
5250 struct audio_device *adev = out->dev;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005251 int ret = 0;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005252 struct listnode new_devices;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005253 bool bypass_a2dp = false;
5254 bool reconfig = false;
5255 unsigned long service_interval = 0;
5256
5257 ALOGD("%s: enter: usecase(%d: %s) devices %x",
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005258 __func__, out->usecase, use_case_table[out->usecase], get_device_types(devices));
5259
5260 list_init(&new_devices);
5261 assign_devices(&new_devices, devices);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005262
5263 lock_output_stream(out);
5264 pthread_mutex_lock(&adev->lock);
5265
5266 /*
5267 * When HDMI cable is unplugged the music playback is paused and
5268 * the policy manager sends routing=0. But the audioflinger continues
5269 * to write data until standby time (3sec). As the HDMI core is
5270 * turned off, the write gets blocked.
5271 * Avoid this by routing audio to speaker until standby.
5272 */
Aniket Kumar Lata9c2fd892020-01-22 22:20:00 -08005273 if (is_single_device_type_equal(&out->device_list,
5274 AUDIO_DEVICE_OUT_AUX_DIGITAL) &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005275 list_empty(&new_devices) &&
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005276 !audio_extn_passthru_is_passthrough_stream(out) &&
5277 (platform_get_edid_info(adev->platform) != 0) /* HDMI disconnected */) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005278 reassign_device_list(&new_devices, AUDIO_DEVICE_OUT_SPEAKER, "");
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005279 }
5280 /*
5281 * When A2DP is disconnected the
5282 * music playback is paused and the policy manager sends routing=0
5283 * But the audioflinger continues to write data until standby time
5284 * (3sec). As BT is turned off, the write gets blocked.
5285 * Avoid this by routing audio to speaker until standby.
5286 */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005287 if (is_a2dp_out_device_type(&out->device_list) &&
5288 list_empty(&new_devices) &&
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005289 !audio_extn_a2dp_source_is_ready() &&
5290 !adev->bt_sco_on) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005291 reassign_device_list(&new_devices, AUDIO_DEVICE_OUT_SPEAKER, "");
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005292 }
5293 /*
Weiyin Jiangabedea32020-12-09 12:49:19 +08005294 * When USB headset is disconnected the music playback paused
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005295 * and the policy manager send routing=0. But if the USB is connected
5296 * back before the standby time, AFE is not closed and opened
5297 * when USB is connected back. So routing to speker will guarantee
5298 * AFE reconfiguration and AFE will be opend once USB is connected again
5299 */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005300 if (is_usb_out_device_type(&out->device_list) &&
5301 list_empty(&new_devices) &&
5302 !audio_extn_usb_connected(NULL)) {
Sujin Panicker84d953a2020-11-16 17:39:54 +05305303 if (adev->mode == AUDIO_MODE_IN_CALL || adev->mode == AUDIO_MODE_IN_COMMUNICATION)
5304 reassign_device_list(&new_devices, AUDIO_DEVICE_OUT_EARPIECE, "");
5305 else
5306 reassign_device_list(&new_devices, AUDIO_DEVICE_OUT_SPEAKER, "");
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005307 }
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005308 /* To avoid a2dp to sco overlapping / BT device improper state
5309 * check with BT lib about a2dp streaming support before routing
5310 */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005311 if (is_a2dp_out_device_type(&new_devices)) {
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005312 if (!audio_extn_a2dp_source_is_ready()) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005313 if (compare_device_type(&new_devices, AUDIO_DEVICE_OUT_SPEAKER) ||
5314 compare_device_type(&new_devices, AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005315 //combo usecase just by pass a2dp
5316 ALOGW("%s: A2DP profile is not ready,routing to speaker only", __func__);
5317 bypass_a2dp = true;
5318 } else {
5319 ALOGE("%s: A2DP profile is not ready,ignoring routing request", __func__);
5320 /* update device to a2dp and don't route as BT returned error
5321 * However it is still possible a2dp routing called because
5322 * of current active device disconnection (like wired headset)
5323 */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005324 assign_devices(&out->device_list, &new_devices);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005325 pthread_mutex_unlock(&adev->lock);
5326 pthread_mutex_unlock(&out->lock);
5327 goto error;
5328 }
5329 }
5330 }
5331
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005332 // Workaround: If routing to an non existing usb device, fail gracefully
5333 // The routing request will otherwise block during 10 second
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005334 if (is_usb_out_device_type(&new_devices)) {
5335 struct str_parms *parms =
5336 str_parms_create_str(get_usb_device_address(&new_devices));
5337 if (!parms)
5338 goto error;
Weiyin Jiangabedea32020-12-09 12:49:19 +08005339 if (!audio_extn_usb_connected(NULL)) {
5340 ALOGW("%s: ignoring rerouting to non existing USB card", __func__);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005341 pthread_mutex_unlock(&adev->lock);
5342 pthread_mutex_unlock(&out->lock);
5343 str_parms_destroy(parms);
5344 ret = -ENOSYS;
5345 goto error;
5346 }
5347 str_parms_destroy(parms);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005348 }
5349
Weiyin Jiang80c8f9a2020-01-10 20:42:08 +08005350 // Workaround: If routing to an non existing hdmi device, fail gracefully
5351 if (compare_device_type(&new_devices, AUDIO_DEVICE_OUT_AUX_DIGITAL) &&
5352 (platform_get_edid_info_v2(adev->platform,
5353 out->extconn.cs.controller,
5354 out->extconn.cs.stream) != 0)) {
5355 ALOGW("out_set_parameters() ignoring rerouting to non existing HDMI/DP");
5356 pthread_mutex_unlock(&adev->lock);
5357 pthread_mutex_unlock(&out->lock);
5358 ret = -ENOSYS;
5359 goto error;
5360 }
5361
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005362 /*
5363 * select_devices() call below switches all the usecases on the same
5364 * backend to the new device. Refer to check_usecases_codec_backend() in
5365 * the select_devices(). But how do we undo this?
5366 *
5367 * For example, music playback is active on headset (deep-buffer usecase)
5368 * and if we go to ringtones and select a ringtone, low-latency usecase
5369 * will be started on headset+speaker. As we can't enable headset+speaker
5370 * and headset devices at the same time, select_devices() switches the music
5371 * playback to headset+speaker while starting low-lateny usecase for ringtone.
5372 * So when the ringtone playback is completed, how do we undo the same?
5373 *
5374 * We are relying on the out_set_parameters() call on deep-buffer output,
5375 * once the ringtone playback is ended.
5376 * NOTE: We should not check if the current devices are same as new devices.
5377 * Because select_devices() must be called to switch back the music
5378 * playback to headset.
5379 */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005380 if (!list_empty(&new_devices)) {
5381 bool same_dev = compare_devices(&out->device_list, &new_devices);
5382 assign_devices(&out->device_list, &new_devices);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005383
5384 if (output_drives_call(adev, out)) {
5385 if (!voice_is_call_state_active(adev)) {
5386 if (adev->mode == AUDIO_MODE_IN_CALL) {
5387 adev->current_call_output = out;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005388 ret = voice_start_call(adev);
5389 }
5390 } else {
Kunlei Zhang253ad102020-10-14 16:21:28 +08005391 platform_is_volume_boost_supported_device(adev->platform, &new_devices);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005392 adev->current_call_output = out;
5393 voice_update_devices_for_all_voice_usecases(adev);
5394 }
5395 }
5396
Mingshu Pang971ff702020-09-09 15:28:22 +08005397 if (is_usb_out_device_type(&out->device_list)) {
5398 service_interval = audio_extn_usb_find_service_interval(false, true /*playback*/);
5399 audio_extn_usb_set_service_interval(true /*playback*/,
5400 service_interval,
5401 &reconfig);
5402 ALOGD("%s, svc_int(%ld),reconfig(%d)",__func__,service_interval, reconfig);
5403 }
5404
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005405 if (!out->standby) {
5406 if (!same_dev) {
5407 ALOGV("update routing change");
5408 audio_extn_perf_lock_acquire(&adev->perf_lock_handle, 0,
5409 adev->perf_lock_opts,
5410 adev->perf_lock_opts_size);
5411 if (adev->adm_on_routing_change)
5412 adev->adm_on_routing_change(adev->adm_data,
5413 out->handle);
5414 }
5415 if (!bypass_a2dp) {
5416 select_devices(adev, out->usecase);
5417 } else {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005418 if (compare_device_type(&new_devices, AUDIO_DEVICE_OUT_SPEAKER_SAFE))
5419 reassign_device_list(&out->device_list, AUDIO_DEVICE_OUT_SPEAKER_SAFE, "");
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005420 else
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005421 reassign_device_list(&out->device_list, AUDIO_DEVICE_OUT_SPEAKER, "");
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005422 select_devices(adev, out->usecase);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005423 assign_devices(&out->device_list, &new_devices);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005424 }
5425
5426 if (!same_dev) {
5427 // on device switch force swap, lower functions will make sure
5428 // to check if swap is allowed or not.
5429 platform_set_swap_channels(adev, true);
5430 audio_extn_perf_lock_release(&adev->perf_lock_handle);
5431 }
Zhou Songbaddf9f2020-11-20 13:57:39 +08005432 pthread_mutex_lock(&out->latch_lock);
5433 if (!is_a2dp_out_device_type(&out->device_list) || audio_extn_a2dp_source_is_ready()) {
5434 if (out->a2dp_muted) {
5435 out->a2dp_muted = false;
5436 if (is_offload_usecase(out->usecase))
5437 out_set_compr_volume(&out->stream, out->volume_l, out->volume_r);
5438 else if (out->usecase != USECASE_AUDIO_PLAYBACK_VOIP)
5439 out_set_pcm_volume(&out->stream, out->volume_l, out->volume_r);
Weiyin Jiang280ea742020-09-08 20:28:22 +08005440 }
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005441 }
Zhou Songbaddf9f2020-11-20 13:57:39 +08005442 if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP && !out->a2dp_muted)
5443 out_set_voip_volume(&out->stream, out->volume_l, out->volume_r);
5444 pthread_mutex_unlock(&out->latch_lock);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005445 }
5446 }
5447
5448 pthread_mutex_unlock(&adev->lock);
5449 pthread_mutex_unlock(&out->lock);
5450
5451 /*handles device and call state changes*/
5452 audio_extn_extspk_update(adev->extspk);
5453
Revathi Uddaraju4255a632021-12-02 05:11:13 -08005454 clear_devices(&new_devices);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005455error:
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005456 ALOGV("%s: exit: code(%d)", __func__, ret);
5457 return ret;
5458}
5459
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005460static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
5461{
5462 struct stream_out *out = (struct stream_out *)stream;
5463 struct audio_device *adev = out->dev;
5464 struct str_parms *parms;
5465 char value[32];
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005466 int ret = 0, err;
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07005467 int ext_controller = -1;
5468 int ext_stream = -1;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005469
sangwoobc677242013-08-08 16:53:43 +09005470 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07005471 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005472 parms = str_parms_create_str(kvpairs);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05305473 if (!parms)
5474 goto error;
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07005475
5476 err = platform_get_controller_stream_from_params(parms, &ext_controller,
5477 &ext_stream);
Vignesh Kulothungan1b3957e2019-12-02 16:16:51 -08005478 if (err == 0) {
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07005479 out->extconn.cs.controller = ext_controller;
5480 out->extconn.cs.stream = ext_stream;
5481 ALOGD("%s: usecase(%s) new controller/stream (%d/%d)", __func__,
5482 use_case_table[out->usecase], out->extconn.cs.controller,
5483 out->extconn.cs.stream);
5484 }
5485
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07005486 if (out == adev->primary_output) {
5487 pthread_mutex_lock(&adev->lock);
5488 audio_extn_set_parameters(adev, parms);
5489 pthread_mutex_unlock(&adev->lock);
5490 }
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07005491 if (is_offload_usecase(out->usecase)) {
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005492 lock_output_stream(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005493 parse_compress_metadata(out, parms);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08005494
5495 audio_extn_dts_create_state_notifier_node(out->usecase);
5496 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
5497 popcount(out->channel_mask),
5498 out->playback_started);
5499
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08005500 pthread_mutex_unlock(&out->lock);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005501 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07005502
Surendar Karkaf51b5842018-04-26 11:28:38 +05305503 err = str_parms_get_str(parms, AUDIO_PARAMETER_DUAL_MONO, value,
5504 sizeof(value));
5505 if (err >= 0) {
5506 if (!strncmp("true", value, sizeof("true")) || atoi(value))
5507 audio_extn_send_dual_mono_mixing_coefficients(out);
5508 }
5509
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +05305510 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_PROFILE, value, sizeof(value));
5511 if (err >= 0) {
5512 strlcpy(out->profile, value, sizeof(out->profile));
5513 ALOGV("updating stream profile with value '%s'", out->profile);
5514 lock_output_stream(out);
5515 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
5516 &adev->streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005517 &out->device_list, out->flags,
5518 out->hal_op_format,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +05305519 out->sample_rate, out->bit_width,
5520 out->channel_mask, out->profile,
5521 &out->app_type_cfg);
5522 pthread_mutex_unlock(&out->lock);
5523 }
5524
Alexy Joseph98988832017-01-13 14:56:59 -08005525 //suspend, resume handling block
Aniket Kumar Lata932f4872017-11-06 18:29:44 -08005526 //remove QOS only if vendor.audio.hal.dynamic.qos.config.supported is set to true
5527 // and vendor.audio.hal.output.suspend.supported is set to true
5528 if (out->hal_output_suspend_supported && out->dynamic_pm_qos_config_supported) {
Alexy Joseph98988832017-01-13 14:56:59 -08005529 //check suspend parameter only for low latency and if the property
5530 //is enabled
5531 if (str_parms_get_str(parms, "suspend_playback", value, sizeof(value)) >= 0) {
5532 ALOGI("%s: got suspend_playback %s", __func__, value);
5533 lock_output_stream(out);
5534 if (!strncmp(value, "false", 5)) {
5535 //suspend_playback=false is supposed to set QOS value back to 75%
5536 //the mixer control sent with value Enable will achieve that
5537 ret = audio_route_apply_and_update_path(adev->audio_route, out->pm_qos_mixer_path);
5538 } else if (!strncmp (value, "true", 4)) {
5539 //suspend_playback=true is supposed to remove QOS value
5540 //resetting the mixer control will set the default value
5541 //for the mixer control which is Disable and this removes the QOS vote
5542 ret = audio_route_reset_and_update_path(adev->audio_route, out->pm_qos_mixer_path);
5543 } else {
5544 ALOGE("%s: Wrong value sent for suspend_playback, expected true/false,"
5545 " got %s", __func__, value);
5546 ret = -1;
5547 }
5548
5549 if (ret != 0) {
5550 ALOGE("%s: %s mixer ctl failed with %d, ignore suspend/resume setparams",
5551 __func__, out->pm_qos_mixer_path, ret);
5552 }
5553
5554 pthread_mutex_unlock(&out->lock);
5555 }
5556 }
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07005557
Alexy Joseph98988832017-01-13 14:56:59 -08005558 //end suspend, resume handling block
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005559 str_parms_destroy(parms);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05305560error:
Eric Laurent994a6932013-07-17 11:51:42 -07005561 ALOGV("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005562 return ret;
5563}
5564
Paul McLeana50b7332018-12-17 08:24:21 -07005565static int in_set_microphone_direction(const struct audio_stream_in *stream,
5566 audio_microphone_direction_t dir) {
justinweng20fb6d82019-02-21 18:49:00 -07005567 struct stream_in *in = (struct stream_in *)stream;
5568
5569 ALOGVV("%s: standby %d source %d dir %d", __func__, in->standby, in->source, dir);
5570
5571 in->direction = dir;
5572
5573 if (in->standby)
5574 return 0;
5575
5576 return audio_extn_audiozoom_set_microphone_direction(in, dir);
Paul McLeana50b7332018-12-17 08:24:21 -07005577}
5578
5579static int in_set_microphone_field_dimension(const struct audio_stream_in *stream, float zoom) {
justinweng20fb6d82019-02-21 18:49:00 -07005580 struct stream_in *in = (struct stream_in *)stream;
5581
5582 ALOGVV("%s: standby %d source %d zoom %f", __func__, in->standby, in->source, zoom);
5583
5584 if (zoom > 1.0 || zoom < -1.0)
5585 return -EINVAL;
5586
5587 in->zoom = zoom;
5588
5589 if (in->standby)
5590 return 0;
5591
5592 return audio_extn_audiozoom_set_microphone_field_dimension(in, zoom);
Paul McLeana50b7332018-12-17 08:24:21 -07005593}
5594
5595
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005596static bool stream_get_parameter_channels(struct str_parms *query,
5597 struct str_parms *reply,
5598 audio_channel_mask_t *supported_channel_masks) {
5599 int ret = -1;
5600 char value[512];
5601 bool first = true;
5602 size_t i, j;
5603
5604 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) {
5605 ret = 0;
5606 value[0] = '\0';
5607 i = 0;
5608 while (supported_channel_masks[i] != 0) {
5609 for (j = 0; j < ARRAY_SIZE(channels_name_to_enum_table); j++) {
5610 if (channels_name_to_enum_table[j].value == supported_channel_masks[i]) {
5611 if (!first)
Satya Krishna Pindiprolib22ac722017-10-09 15:44:16 +05305612 strlcat(value, "|", sizeof(value));
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005613
Satya Krishna Pindiprolib22ac722017-10-09 15:44:16 +05305614 strlcat(value, channels_name_to_enum_table[j].name, sizeof(value));
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005615 first = false;
5616 break;
5617 }
5618 }
5619 i++;
5620 }
5621 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
5622 }
5623 return ret == 0;
5624}
5625
5626static bool stream_get_parameter_formats(struct str_parms *query,
5627 struct str_parms *reply,
5628 audio_format_t *supported_formats) {
5629 int ret = -1;
5630 char value[256];
5631 size_t i, j;
5632 bool first = true;
5633
5634 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) {
5635 ret = 0;
5636 value[0] = '\0';
5637 i = 0;
5638 while (supported_formats[i] != 0) {
5639 for (j = 0; j < ARRAY_SIZE(formats_name_to_enum_table); j++) {
5640 if (formats_name_to_enum_table[j].value == supported_formats[i]) {
5641 if (!first) {
Satya Krishna Pindiprolib22ac722017-10-09 15:44:16 +05305642 strlcat(value, "|", sizeof(value));
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005643 }
Satya Krishna Pindiprolib22ac722017-10-09 15:44:16 +05305644 strlcat(value, formats_name_to_enum_table[j].name, sizeof(value));
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005645 first = false;
5646 break;
5647 }
5648 }
5649 i++;
5650 }
5651 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_FORMATS, value);
5652 }
5653 return ret == 0;
5654}
5655
5656static bool stream_get_parameter_rates(struct str_parms *query,
5657 struct str_parms *reply,
5658 uint32_t *supported_sample_rates) {
5659
5660 int i;
5661 char value[256];
5662 int ret = -1;
5663 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) {
5664 ret = 0;
5665 value[0] = '\0';
5666 i=0;
5667 int cursor = 0;
5668 while (supported_sample_rates[i]) {
5669 int avail = sizeof(value) - cursor;
5670 ret = snprintf(value + cursor, avail, "%s%d",
5671 cursor > 0 ? "|" : "",
5672 supported_sample_rates[i]);
5673 if (ret < 0 || ret >= avail) {
5674 // if cursor is at the last element of the array
5675 // overwrite with \0 is duplicate work as
5676 // snprintf already put a \0 in place.
5677 // else
5678 // we had space to write the '|' at value[cursor]
5679 // (which will be overwritten) or no space to fill
5680 // the first element (=> cursor == 0)
5681 value[cursor] = '\0';
5682 break;
5683 }
5684 cursor += ret;
5685 ++i;
5686 }
5687 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES,
5688 value);
5689 }
5690 return ret >= 0;
5691}
5692
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005693static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
5694{
5695 struct stream_out *out = (struct stream_out *)stream;
5696 struct str_parms *query = str_parms_create_str(keys);
Alexy Josephaee4fdd2016-01-29 13:02:07 -08005697 char *str = (char*) NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005698 char value[256];
5699 struct str_parms *reply = str_parms_create();
5700 size_t i, j;
5701 int ret;
5702 bool first = true;
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07005703
5704 if (!query || !reply) {
Alexy Josephaee4fdd2016-01-29 13:02:07 -08005705 if (reply) {
5706 str_parms_destroy(reply);
5707 }
5708 if (query) {
5709 str_parms_destroy(query);
5710 }
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07005711 ALOGE("out_get_parameters: failed to allocate mem for query or reply");
5712 return NULL;
5713 }
5714
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005715 ALOGV("%s: %s enter: keys - %s", __func__, use_case_table[out->usecase], keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005716 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
5717 if (ret >= 0) {
5718 value[0] = '\0';
5719 i = 0;
5720 while (out->supported_channel_masks[i] != 0) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005721 for (j = 0; j < ARRAY_SIZE(channels_name_to_enum_table); j++) {
5722 if (channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005723 if (!first) {
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -08005724 strlcat(value, "|", sizeof(value));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005725 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005726 strlcat(value, channels_name_to_enum_table[j].name, sizeof(value));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005727 first = false;
5728 break;
5729 }
5730 }
5731 i++;
5732 }
5733 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
5734 str = str_parms_to_str(reply);
5735 } else {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08005736 voice_extn_out_get_parameters(out, query, reply);
5737 str = str_parms_to_str(reply);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005738 }
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07005739
Alexy Joseph62142aa2015-11-16 15:10:34 -08005740
5741 ret = str_parms_get_str(query, "is_direct_pcm_track", value, sizeof(value));
5742 if (ret >= 0) {
5743 value[0] = '\0';
Dhananjay Kumarac341582017-02-23 23:42:25 +05305744 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
5745 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Alexy Joseph62142aa2015-11-16 15:10:34 -08005746 ALOGV("in direct_pcm");
Satya Krishna Pindiprolib6655542017-07-03 19:38:19 +05305747 strlcat(value, "true", sizeof(value));
Alexy Joseph62142aa2015-11-16 15:10:34 -08005748 } else {
5749 ALOGV("not in direct_pcm");
Sharad Sangle3dd5a4a2015-12-10 18:39:17 +05305750 strlcat(value, "false", sizeof(value));
Alexy Joseph62142aa2015-11-16 15:10:34 -08005751 }
5752 str_parms_add_str(reply, "is_direct_pcm_track", value);
Alexy Josephaee4fdd2016-01-29 13:02:07 -08005753 if (str)
5754 free(str);
Alexy Joseph62142aa2015-11-16 15:10:34 -08005755 str = str_parms_to_str(reply);
5756 }
5757
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07005758 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS, value, sizeof(value));
5759 if (ret >= 0) {
5760 value[0] = '\0';
5761 i = 0;
5762 first = true;
5763 while (out->supported_formats[i] != 0) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005764 for (j = 0; j < ARRAY_SIZE(formats_name_to_enum_table); j++) {
5765 if (formats_name_to_enum_table[j].value == out->supported_formats[i]) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07005766 if (!first) {
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -08005767 strlcat(value, "|", sizeof(value));
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07005768 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005769 strlcat(value, formats_name_to_enum_table[j].name, sizeof(value));
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07005770 first = false;
5771 break;
5772 }
5773 }
5774 i++;
5775 }
5776 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_FORMATS, value);
Alexy Josephaee4fdd2016-01-29 13:02:07 -08005777 if (str)
5778 free(str);
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07005779 str = str_parms_to_str(reply);
5780 }
Mingming Yin3a941d42016-02-17 18:08:05 -08005781
5782 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES, value, sizeof(value));
5783 if (ret >= 0) {
5784 value[0] = '\0';
5785 i = 0;
5786 first = true;
5787 while (out->supported_sample_rates[i] != 0) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005788 for (j = 0; j < ARRAY_SIZE(out_sample_rates_name_to_enum_table); j++) {
5789 if (out_sample_rates_name_to_enum_table[j].value == out->supported_sample_rates[i]) {
Mingming Yin3a941d42016-02-17 18:08:05 -08005790 if (!first) {
5791 strlcat(value, "|", sizeof(value));
5792 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005793 strlcat(value, out_sample_rates_name_to_enum_table[j].name, sizeof(value));
Mingming Yin3a941d42016-02-17 18:08:05 -08005794 first = false;
5795 break;
5796 }
5797 }
5798 i++;
5799 }
5800 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES, value);
5801 if (str)
5802 free(str);
5803 str = str_parms_to_str(reply);
5804 }
5805
Alexy Joseph98988832017-01-13 14:56:59 -08005806 if (str_parms_get_str(query, "supports_hw_suspend", value, sizeof(value)) >= 0) {
5807 //only low latency track supports suspend_resume
5808 str_parms_add_int(reply, "supports_hw_suspend",
Aniket Kumar Lata932f4872017-11-06 18:29:44 -08005809 (out->hal_output_suspend_supported));
Alexy Joseph98988832017-01-13 14:56:59 -08005810 if (str)
5811 free(str);
5812 str = str_parms_to_str(reply);
5813 }
5814
5815
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005816 str_parms_destroy(query);
5817 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07005818 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005819 return str;
5820}
5821
5822static uint32_t out_get_latency(const struct audio_stream_out *stream)
5823{
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005824 uint32_t period_ms;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005825 struct stream_out *out = (struct stream_out *)stream;
Alexy Josephaa54c872014-12-03 02:46:47 -08005826 uint32_t latency = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005827
Alexy Josephaa54c872014-12-03 02:46:47 -08005828 if (is_offload_usecase(out->usecase)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05305829 lock_output_stream(out);
5830 latency = audio_extn_utils_compress_get_dsp_latency(out);
5831 pthread_mutex_unlock(&out->lock);
Haynes Mathew George16081042017-05-31 17:16:49 -07005832 } else if ((out->realtime) ||
5833 (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP)) {
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005834 // since the buffer won't be filled up faster than realtime,
5835 // return a smaller number
5836 if (out->config.rate)
5837 period_ms = (out->af_period_multiplier * out->config.period_size *
5838 1000) / (out->config.rate);
5839 else
5840 period_ms = 0;
George Gao9ba8a142020-07-23 14:30:03 -07005841 latency = period_ms + platform_render_latency(out) / 1000;
Alexy Josephaa54c872014-12-03 02:46:47 -08005842 } else {
5843 latency = (out->config.period_count * out->config.period_size * 1000) /
Mingshu Pangf69a88d2021-04-30 16:14:39 +08005844 (out->config.rate);
pavanisra2d95d82022-02-09 18:55:58 +05305845 if (out->usecase == USECASE_AUDIO_PLAYBACK_DEEP_BUFFER ||
5846 out->usecase == USECASE_AUDIO_PLAYBACK_LOW_LATENCY)
Mingshu Pangf69a88d2021-04-30 16:14:39 +08005847 latency += platform_render_latency(out)/1000;
Alexy Josephaa54c872014-12-03 02:46:47 -08005848 }
5849
Zhou Songd2537a02020-06-11 22:04:46 +08005850 if (!out->standby && is_a2dp_out_device_type(&out->device_list))
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08005851 latency += audio_extn_a2dp_get_encoder_latency();
5852
Anish Kumar50ebcbf2014-12-09 04:01:39 +05305853 ALOGV("%s: Latency %d", __func__, latency);
Alexy Josephaa54c872014-12-03 02:46:47 -08005854 return latency;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005855}
5856
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05305857static float AmpToDb(float amplification)
5858{
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05305859 float db = DSD_VOLUME_MIN_DB;
5860 if (amplification > 0) {
5861 db = 20 * log10(amplification);
5862 if(db < DSD_VOLUME_MIN_DB)
5863 return DSD_VOLUME_MIN_DB;
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05305864 }
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05305865 return db;
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05305866}
5867
Krishna Kishor Jha10ad39f2022-02-21 17:02:47 +05305868#ifdef SOFT_VOLUME
5869static int out_set_soft_volume_params(struct audio_stream_out *stream)
5870{
5871 struct stream_out *out = (struct stream_out *)stream;
5872 int ret = 0;
5873 char mixer_ctl_name[128];
5874 struct audio_device *adev = out->dev;
5875 struct mixer_ctl *ctl = NULL;
5876 struct soft_step_volume_params *volume_params = NULL;
5877
5878 int pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
5879 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name), "Playback %d Soft Vol Params", pcm_device_id);
5880 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
5881 if (!ctl) {
5882 ALOGE("%s : Could not get ctl for mixer cmd - %s", __func__, mixer_ctl_name);
5883 return -EINVAL;
5884 }
5885
5886 volume_params =(struct soft_step_volume_params * ) malloc(sizeof(struct soft_step_volume_params));
5887 if (volume_params == NULL){
5888 ALOGE("%s : malloc is failed for volume params", __func__);
5889 return -EINVAL;
5890 } else {
5891 ret = platform_get_soft_step_volume_params(volume_params,out->usecase);
5892 if (ret < 0) {
5893 ALOGE("%s : platform_get_soft_step_volume_params is fialed", __func__);
Karan Naidu28b335a2022-05-18 23:00:08 +05305894 ret = -EINVAL;
5895 goto ERR_EXIT;
Krishna Kishor Jha10ad39f2022-02-21 17:02:47 +05305896 }
5897
5898 }
5899 ret = mixer_ctl_set_array(ctl, volume_params, sizeof(struct soft_step_volume_params)/sizeof(int));
5900 if (ret < 0) {
5901 ALOGE("%s: Could not set ctl, error:%d ", __func__, ret);
Karan Naidu28b335a2022-05-18 23:00:08 +05305902 ret = -EINVAL;
5903 goto ERR_EXIT;
5904 }
5905
5906 if (volume_params) {
5907 free(volume_params);
Krishna Kishor Jha10ad39f2022-02-21 17:02:47 +05305908 }
5909 return 0;
Karan Naidu28b335a2022-05-18 23:00:08 +05305910
5911ERR_EXIT:
5912 if (volume_params) {
5913 free(volume_params);
5914 }
5915 return ret;
Krishna Kishor Jha10ad39f2022-02-21 17:02:47 +05305916}
5917#endif
5918
Arun Mirpuri5d170872019-03-26 13:21:31 -07005919static int out_set_mmap_volume(struct audio_stream_out *stream, float left,
5920 float right)
5921{
5922 struct stream_out *out = (struct stream_out *)stream;
5923 long volume = 0;
5924 char mixer_ctl_name[128] = "";
5925 struct audio_device *adev = out->dev;
5926 struct mixer_ctl *ctl = NULL;
5927 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
5928 PCM_PLAYBACK);
5929
5930 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
5931 "Playback %d Volume", pcm_device_id);
5932 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
5933 if (!ctl) {
5934 ALOGE("%s: Could not get ctl for mixer cmd - %s",
5935 __func__, mixer_ctl_name);
5936 return -EINVAL;
5937 }
5938 if (left != right)
5939 ALOGW("%s: Left and right channel volume mismatch:%f,%f",
5940 __func__, left, right);
5941 volume = (long)(left * (MMAP_PLAYBACK_VOLUME_MAX*1.0));
5942 if (mixer_ctl_set_value(ctl, 0, volume) < 0){
5943 ALOGE("%s:ctl for mixer cmd - %s, volume %ld returned error",
5944 __func__, mixer_ctl_name, volume);
5945 return -EINVAL;
5946 }
5947 return 0;
5948}
5949
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05305950static int out_set_compr_volume(struct audio_stream_out *stream, float left,
5951 float right)
5952{
5953 struct stream_out *out = (struct stream_out *)stream;
Manish Dewangan338c50a2017-09-12 15:22:03 +05305954 long volume[2];
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05305955 char mixer_ctl_name[128];
5956 struct audio_device *adev = out->dev;
5957 struct mixer_ctl *ctl;
5958 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
5959 PCM_PLAYBACK);
5960
5961 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
5962 "Compress Playback %d Volume", pcm_device_id);
5963 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
5964 if (!ctl) {
5965 ALOGE("%s: Could not get ctl for mixer cmd - %s",
5966 __func__, mixer_ctl_name);
5967 return -EINVAL;
5968 }
5969 ALOGE("%s:ctl for mixer cmd - %s, left %f, right %f",
5970 __func__, mixer_ctl_name, left, right);
5971 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
5972 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
5973 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
5974
5975 return 0;
5976}
5977
Zhou Song2b8f28f2017-09-11 10:51:38 +08005978static int out_set_voip_volume(struct audio_stream_out *stream, float left,
5979 float right)
5980{
5981 struct stream_out *out = (struct stream_out *)stream;
5982 char mixer_ctl_name[] = "App Type Gain";
5983 struct audio_device *adev = out->dev;
5984 struct mixer_ctl *ctl;
Manish Dewangan338c50a2017-09-12 15:22:03 +05305985 long set_values[4];
Zhou Song2b8f28f2017-09-11 10:51:38 +08005986
Aniket Kumar Lata8426d1f2019-06-10 15:25:53 -07005987 if (!is_valid_volume(left, right)) {
5988 ALOGE("%s: Invalid stream volume for left=%f, right=%f",
5989 __func__, left, right);
5990 return -EINVAL;
5991 }
5992
Zhou Song2b8f28f2017-09-11 10:51:38 +08005993 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
5994 if (!ctl) {
5995 ALOGE("%s: Could not get ctl for mixer cmd - %s",
5996 __func__, mixer_ctl_name);
5997 return -EINVAL;
5998 }
5999
6000 set_values[0] = 0; //0: Rx Session 1:Tx Session
6001 set_values[1] = out->app_type_cfg.app_type;
Manish Dewangan338c50a2017-09-12 15:22:03 +05306002 set_values[2] = (long)(left * VOIP_PLAYBACK_VOLUME_MAX);
6003 set_values[3] = (long)(right * VOIP_PLAYBACK_VOLUME_MAX);
Zhou Song2b8f28f2017-09-11 10:51:38 +08006004
6005 mixer_ctl_set_array(ctl, set_values, ARRAY_SIZE(set_values));
6006 return 0;
6007}
6008
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05306009static int out_set_pcm_volume(struct audio_stream_out *stream, float left,
6010 float right)
6011{
6012 struct stream_out *out = (struct stream_out *)stream;
6013 /* Volume control for pcm playback */
6014 if (left != right) {
6015 return -EINVAL;
6016 } else {
6017 char mixer_ctl_name[128];
6018 struct audio_device *adev = out->dev;
6019 struct mixer_ctl *ctl;
6020 int pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
6021 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name), "Playback %d Volume", pcm_device_id);
6022 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
6023 if (!ctl) {
6024 ALOGE("%s : Could not get ctl for mixer cmd - %s", __func__, mixer_ctl_name);
6025 return -EINVAL;
6026 }
6027
6028 int volume = (int) (left * PCM_PLAYBACK_VOLUME_MAX);
6029 int ret = mixer_ctl_set_value(ctl, 0, volume);
6030 if (ret < 0) {
6031 ALOGE("%s: Could not set ctl, error:%d ", __func__, ret);
6032 return -EINVAL;
6033 }
6034
6035 ALOGV("%s : Pcm set volume value %d left %f", __func__, volume, left);
6036
6037 return 0;
6038 }
6039}
6040
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006041static int out_set_volume(struct audio_stream_out *stream, float left,
6042 float right)
6043{
Eric Laurenta9024de2013-04-04 09:19:12 -07006044 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006045 int volume[2];
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05306046 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006047
Arun Mirpuri5d170872019-03-26 13:21:31 -07006048 ALOGD("%s: called with left_vol=%f, right_vol=%f", __func__, left, right);
Eric Laurenta9024de2013-04-04 09:19:12 -07006049 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
6050 /* only take left channel into account: the API is for stereo anyway */
6051 out->muted = (left == 0.0f);
6052 return 0;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07006053 } else if (is_offload_usecase(out->usecase)) {
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05306054 if (audio_extn_passthru_is_passthrough_stream(out)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07006055 /*
6056 * Set mute or umute on HDMI passthrough stream.
6057 * Only take left channel into account.
6058 * Mute is 0 and unmute 1
6059 */
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05306060 audio_extn_passthru_set_volume(out, (left == 0.0f));
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05306061 } else if (out->format == AUDIO_FORMAT_DSD){
6062 char mixer_ctl_name[128] = "DSD Volume";
6063 struct audio_device *adev = out->dev;
6064 struct mixer_ctl *ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
6065
6066 if (!ctl) {
6067 ALOGE("%s: Could not get ctl for mixer cmd - %s",
6068 __func__, mixer_ctl_name);
6069 return -EINVAL;
6070 }
Manish Dewangan338c50a2017-09-12 15:22:03 +05306071 volume[0] = (long)(AmpToDb(left));
6072 volume[1] = (long)(AmpToDb(right));
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05306073 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
6074 return 0;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08006075 } else if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_BUS) &&
Derek Chendf05eea2019-08-01 13:57:49 -07006076 (out->car_audio_stream == CAR_AUDIO_STREAM_MEDIA)) {
Philippe Gravel1c9ac352019-05-28 16:08:37 -07006077 ALOGD("%s: Overriding offload set volume for media bus stream", __func__);
6078 struct listnode *node = NULL;
6079 list_for_each(node, &adev->active_outputs_list) {
6080 streams_output_ctxt_t *out_ctxt = node_to_item(node,
6081 streams_output_ctxt_t,
6082 list);
6083 if (out_ctxt->output->usecase == USECASE_AUDIO_PLAYBACK_MEDIA) {
6084 out->volume_l = out_ctxt->output->volume_l;
6085 out->volume_r = out_ctxt->output->volume_r;
6086 }
6087 }
Weiyin Jiang280ea742020-09-08 20:28:22 +08006088 pthread_mutex_lock(&out->latch_lock);
Zhou Songbaddf9f2020-11-20 13:57:39 +08006089 if (!out->a2dp_muted) {
Philippe Gravel1c9ac352019-05-28 16:08:37 -07006090 ret = out_set_compr_volume(&out->stream, out->volume_l, out->volume_r);
6091 }
Weiyin Jiang280ea742020-09-08 20:28:22 +08006092 pthread_mutex_unlock(&out->latch_lock);
Philippe Gravel1c9ac352019-05-28 16:08:37 -07006093 return ret;
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07006094 } else {
Weiyin Jiang280ea742020-09-08 20:28:22 +08006095 pthread_mutex_lock(&out->latch_lock);
Zhou Songbaddf9f2020-11-20 13:57:39 +08006096 ALOGV("%s: compress mute %d", __func__, out->a2dp_muted);
6097 if (!out->a2dp_muted)
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05306098 ret = out_set_compr_volume(stream, left, right);
6099 out->volume_l = left;
6100 out->volume_r = right;
Weiyin Jiang280ea742020-09-08 20:28:22 +08006101 pthread_mutex_unlock(&out->latch_lock);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05306102 return ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006103 }
Vikram Panduranga93f080e2017-06-07 18:16:14 -07006104 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP) {
Aalique Grahame22e49102018-12-18 14:23:57 -08006105 out->app_type_cfg.gain[0] = (int)(left * VOIP_PLAYBACK_VOLUME_MAX);
6106 out->app_type_cfg.gain[1] = (int)(right * VOIP_PLAYBACK_VOLUME_MAX);
Zhou Songbaddf9f2020-11-20 13:57:39 +08006107 pthread_mutex_lock(&out->latch_lock);
Aalique Grahame22e49102018-12-18 14:23:57 -08006108 if (!out->standby) {
6109 audio_extn_utils_send_app_type_gain(out->dev,
6110 out->app_type_cfg.app_type,
6111 &out->app_type_cfg.gain[0]);
Zhou Songbaddf9f2020-11-20 13:57:39 +08006112 if (!out->a2dp_muted)
6113 ret = out_set_voip_volume(stream, left, right);
Aalique Grahame22e49102018-12-18 14:23:57 -08006114 }
Zhou Song2b8f28f2017-09-11 10:51:38 +08006115 out->volume_l = left;
6116 out->volume_r = right;
Zhou Songbaddf9f2020-11-20 13:57:39 +08006117 pthread_mutex_unlock(&out->latch_lock);
Zhou Song2b8f28f2017-09-11 10:51:38 +08006118 return ret;
Arun Mirpuri5d170872019-03-26 13:21:31 -07006119 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
6120 ALOGV("%s: MMAP set volume called", __func__);
6121 if (!out->standby)
6122 ret = out_set_mmap_volume(stream, left, right);
6123 out->volume_l = left;
6124 out->volume_r = right;
6125 return ret;
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05306126 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_LOW_LATENCY ||
Ramu Gottipati36547092018-12-28 11:32:09 +05306127 out->usecase == USECASE_AUDIO_PLAYBACK_DEEP_BUFFER ||
6128 out->usecase == USECASE_AUDIO_PLAYBACK_ULL) {
Zhou Songbaddf9f2020-11-20 13:57:39 +08006129 pthread_mutex_lock(&out->latch_lock);
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05306130 /* Volume control for pcm playback */
Zhou Songbaddf9f2020-11-20 13:57:39 +08006131 if (!out->standby && !out->a2dp_muted)
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05306132 ret = out_set_pcm_volume(stream, left, right);
6133 else
6134 out->apply_volume = true;
6135
6136 out->volume_l = left;
6137 out->volume_r = right;
Zhou Songbaddf9f2020-11-20 13:57:39 +08006138 pthread_mutex_unlock(&out->latch_lock);
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05306139 return ret;
Derek Chenf13dd492018-11-13 14:53:51 -08006140 } else if (audio_extn_auto_hal_is_bus_device_usecase(out->usecase)) {
6141 ALOGV("%s: bus device set volume called", __func__);
Zhou Songbaddf9f2020-11-20 13:57:39 +08006142 pthread_mutex_lock(&out->latch_lock);
6143 if (!out->standby && !out->a2dp_muted)
Derek Chenf13dd492018-11-13 14:53:51 -08006144 ret = out_set_pcm_volume(stream, left, right);
6145 out->volume_l = left;
6146 out->volume_r = right;
Zhou Songbaddf9f2020-11-20 13:57:39 +08006147 pthread_mutex_unlock(&out->latch_lock);
Derek Chenf13dd492018-11-13 14:53:51 -08006148 return ret;
Eric Laurenta9024de2013-04-04 09:19:12 -07006149 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006150
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006151 return -ENOSYS;
6152}
6153
Zhou Songc9672822017-08-16 16:01:39 +08006154static void update_frames_written(struct stream_out *out, size_t bytes)
6155{
6156 size_t bpf = 0;
6157
6158 if (is_offload_usecase(out->usecase) && !out->non_blocking &&
6159 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD))
6160 bpf = 1;
6161 else if (!is_offload_usecase(out->usecase))
6162 bpf = audio_bytes_per_sample(out->format) *
6163 audio_channel_count_from_out_mask(out->channel_mask);
Zhou Song48453a02018-01-10 17:50:59 +08006164
6165 pthread_mutex_lock(&out->position_query_lock);
6166 if (bpf != 0) {
Zhou Songc9672822017-08-16 16:01:39 +08006167 out->written += bytes / bpf;
Zhou Song48453a02018-01-10 17:50:59 +08006168 clock_gettime(CLOCK_MONOTONIC, &out->writeAt);
6169 }
6170 pthread_mutex_unlock(&out->position_query_lock);
Zhou Songc9672822017-08-16 16:01:39 +08006171}
6172
Vignesh Kulothungana6927272019-02-20 15:17:07 -08006173int split_and_write_audio_haptic_data(struct stream_out *out,
6174 const void *buffer, size_t bytes_to_write)
6175{
6176 struct audio_device *adev = out->dev;
6177
6178 int ret = 0;
6179 size_t channel_count = audio_channel_count_from_out_mask(out->channel_mask);
6180 size_t bytes_per_sample = audio_bytes_per_sample(out->format);
6181 size_t frame_size = channel_count * bytes_per_sample;
6182 size_t frame_count = bytes_to_write / frame_size;
6183
6184 bool force_haptic_path =
6185 property_get_bool("vendor.audio.test_haptic", false);
6186
6187 // extract Haptics data from Audio buffer
6188 bool alloc_haptic_buffer = false;
6189 int haptic_channel_count = adev->haptics_config.channels;
6190 size_t haptic_frame_size = bytes_per_sample * haptic_channel_count;
6191 size_t audio_frame_size = frame_size - haptic_frame_size;
6192 size_t total_haptic_buffer_size = frame_count * haptic_frame_size;
6193
6194 if (adev->haptic_buffer == NULL) {
6195 alloc_haptic_buffer = true;
6196 } else if (adev->haptic_buffer_size < total_haptic_buffer_size) {
6197 free(adev->haptic_buffer);
6198 adev->haptic_buffer_size = 0;
6199 alloc_haptic_buffer = true;
6200 }
6201
6202 if (alloc_haptic_buffer) {
6203 adev->haptic_buffer = (uint8_t *)calloc(1, total_haptic_buffer_size);
Mingshu Pang1513f972019-05-24 12:43:51 +08006204 if(adev->haptic_buffer == NULL) {
6205 ALOGE("%s: failed to allocate mem for dev->haptic_buffer", __func__);
6206 return -ENOMEM;
6207 }
Vignesh Kulothungana6927272019-02-20 15:17:07 -08006208 adev->haptic_buffer_size = total_haptic_buffer_size;
6209 }
6210
6211 size_t src_index = 0, aud_index = 0, hap_index = 0;
6212 uint8_t *audio_buffer = (uint8_t *)buffer;
6213 uint8_t *haptic_buffer = adev->haptic_buffer;
6214
6215 // This is required for testing only. This works for stereo data only.
6216 // One channel is fed to audio stream and other to haptic stream for testing.
6217 if (force_haptic_path)
6218 audio_frame_size = haptic_frame_size = bytes_per_sample;
6219
6220 for (size_t i = 0; i < frame_count; i++) {
6221 memcpy(audio_buffer + aud_index, audio_buffer + src_index,
6222 audio_frame_size);
6223 aud_index += audio_frame_size;
6224 src_index += audio_frame_size;
6225
6226 if (adev->haptic_pcm)
6227 memcpy(haptic_buffer + hap_index, audio_buffer + src_index,
6228 haptic_frame_size);
6229 hap_index += haptic_frame_size;
6230 src_index += haptic_frame_size;
6231
6232 // This is required for testing only.
6233 // Discard haptic channel data.
6234 if (force_haptic_path)
6235 src_index += haptic_frame_size;
6236 }
6237
6238 // write to audio pipeline
6239 ret = pcm_write(out->pcm, (void *)audio_buffer,
6240 frame_count * audio_frame_size);
6241
6242 // write to haptics pipeline
6243 if (adev->haptic_pcm)
6244 ret = pcm_write(adev->haptic_pcm, (void *)adev->haptic_buffer,
6245 frame_count * haptic_frame_size);
6246
6247 return ret;
6248}
6249
Aalique Grahame22e49102018-12-18 14:23:57 -08006250#ifdef NO_AUDIO_OUT
6251static ssize_t out_write_for_no_output(struct audio_stream_out *stream,
6252 const void *buffer __unused, size_t bytes)
6253{
6254 struct stream_out *out = (struct stream_out *)stream;
6255
6256 /* No Output device supported other than BT for playback.
6257 * Sleep for the amount of buffer duration
6258 */
6259 lock_output_stream(out);
6260 usleep(bytes * 1000000 / audio_stream_out_frame_size(
6261 (const struct audio_stream_out *)&out->stream) /
6262 out_get_sample_rate(&out->stream.common));
6263 pthread_mutex_unlock(&out->lock);
6264 return bytes;
6265}
6266#endif
6267
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006268static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
6269 size_t bytes)
6270{
6271 struct stream_out *out = (struct stream_out *)stream;
6272 struct audio_device *adev = out->dev;
Eric Laurent6e895242013-09-05 16:10:57 -07006273 ssize_t ret = 0;
Satish Babu Patakokila715b1422017-08-22 14:33:21 +05306274 int channels = 0;
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006275 const size_t frame_size = audio_stream_out_frame_size(stream);
6276 const size_t frames = (frame_size != 0) ? bytes / frame_size : bytes;
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +05306277 struct audio_usecase *usecase = NULL;
Meng Wang4c32fb42020-01-16 17:57:11 +08006278 uint32_t compr_passthr = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006279
Haynes Mathew George380745d2017-10-04 15:27:45 -07006280 ATRACE_BEGIN("out_write");
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006281 lock_output_stream(out);
Naresh Tanniru4c630392014-05-12 01:05:52 +05306282
Shubhasini Sugumaran3fde6582022-01-19 15:54:50 -05006283 if (CARD_STATUS_OFFLINE == out->card_status ||
6284 POWER_POLICY_STATUS_OFFLINE == adev->out_power_policy) {
Zhou Song0b2e5dc2015-03-16 14:41:38 +08006285
Dhananjay Kumarac341582017-02-23 23:42:25 +05306286 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Ashish Jainbbce4322016-02-16 13:25:27 +05306287 /*during SSR for compress usecase we should return error to flinger*/
Naresh Tanniru80659832014-06-04 18:17:56 +05306288 ALOGD(" copl %s: sound card is not active/SSR state", __func__);
6289 pthread_mutex_unlock(&out->lock);
Haynes Mathew George380745d2017-10-04 15:27:45 -07006290 ATRACE_END();
Naresh Tanniru80659832014-06-04 18:17:56 +05306291 return -ENETRESET;
Ashish Jainbbce4322016-02-16 13:25:27 +05306292 } else {
Ashish Jainbbce4322016-02-16 13:25:27 +05306293 ALOGD(" %s: sound card is not active/SSR state", __func__);
6294 ret= -EIO;
6295 goto exit;
Naresh Tanniru4c630392014-05-12 01:05:52 +05306296 }
6297 }
6298
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05306299 if (audio_extn_passthru_should_drop_data(out)) {
Ashish Jaind84fd6a2016-07-27 12:33:25 +05306300 ALOGV(" %s : Drop data as compress passthrough session is going on", __func__);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05306301 ret = -EIO;
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05306302 goto exit;
6303 }
6304
Haynes Mathew George16081042017-05-31 17:16:49 -07006305 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
6306 ret = -EINVAL;
6307 goto exit;
6308 }
6309
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08006310 if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_AUX_DIGITAL) &&
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05306311 !out->is_iec61937_info_available) {
6312
6313 if (!audio_extn_passthru_is_passthrough_stream(out)) {
6314 out->is_iec61937_info_available = true;
6315 } else if (audio_extn_passthru_is_enabled()) {
6316 audio_extn_passthru_update_stream_configuration(adev, out, buffer, bytes);
Manish Dewangan37864bc2017-06-09 12:28:37 +05306317 out->is_iec61937_info_available = true;
Manish Dewangan671a4202017-08-18 17:30:46 +05306318
6319 if((out->format == AUDIO_FORMAT_DTS) ||
6320 (out->format == AUDIO_FORMAT_DTS_HD)) {
6321 ret = audio_extn_passthru_update_dts_stream_configuration(out,
6322 buffer, bytes);
6323 if (ret) {
6324 if (ret != -ENOSYS) {
6325 out->is_iec61937_info_available = false;
6326 ALOGD("iec61937 transmission info not yet updated retry");
6327 }
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05306328 } else if (!out->standby) {
Manish Dewangan671a4202017-08-18 17:30:46 +05306329 /* if stream has started and after that there is
6330 * stream config change (iec transmission config)
6331 * then trigger select_device to update backend configuration.
6332 */
6333 out->stream_config_changed = true;
6334 pthread_mutex_lock(&adev->lock);
6335 select_devices(adev, out->usecase);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05306336 if (!audio_extn_passthru_is_supported_backend_edid_cfg(adev, out)) {
Weiyin Jiang29c08a42019-04-30 17:11:10 +08006337 pthread_mutex_unlock(&adev->lock);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05306338 ret = -EINVAL;
6339 goto exit;
6340 }
Manish Dewangan671a4202017-08-18 17:30:46 +05306341 pthread_mutex_unlock(&adev->lock);
6342 out->stream_config_changed = false;
6343 out->is_iec61937_info_available = true;
6344 }
6345 }
Satish Babu Patakokila715b1422017-08-22 14:33:21 +05306346
Meng Wang4c32fb42020-01-16 17:57:11 +08006347#ifdef AUDIO_GKI_ENABLED
6348 /* out->compr_config.codec->reserved[0] is for compr_passthr */
6349 compr_passthr = out->compr_config.codec->reserved[0];
6350#else
6351 compr_passthr = out->compr_config.codec->compr_passthr;
6352#endif
6353
Garmond Leung317cbf12017-09-13 16:20:50 -07006354 if ((channels < (int)audio_channel_count_from_out_mask(out->channel_mask)) &&
Meng Wang4c32fb42020-01-16 17:57:11 +08006355 (compr_passthr == PASSTHROUGH) &&
Satish Babu Patakokila715b1422017-08-22 14:33:21 +05306356 (out->is_iec61937_info_available == true)) {
6357 ALOGE("%s: ERROR: Unsupported channel config in passthrough mode", __func__);
6358 ret = -EINVAL;
6359 goto exit;
6360 }
Manish Dewangan37864bc2017-06-09 12:28:37 +05306361 }
6362 }
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05306363
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08006364 if (is_a2dp_out_device_type(&out->device_list) &&
Florian Pfister1a84f312018-07-19 14:38:18 +02006365 (audio_extn_a2dp_source_is_suspended())) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08006366 if (!(compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_SPEAKER) ||
6367 compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_SPEAKER_SAFE))) {
Zhou Songd01e7a22020-09-23 22:49:01 +08006368 if (!is_offload_usecase(out->usecase)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05306369 ret = -EIO;
6370 goto exit;
6371 }
6372 }
6373 }
6374
Weiyin Jiangabedea32020-12-09 12:49:19 +08006375 if (is_usb_out_device_type(&out->device_list) &&
6376 !audio_extn_usb_connected(NULL)) {
6377 ret = -EIO;
6378 goto exit;
6379 }
6380
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006381 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07006382 out->standby = false;
Andy Hungc6bfd4a2019-07-01 18:26:00 -07006383 const int64_t startNs = systemTime(SYSTEM_TIME_MONOTONIC);
6384
Eric Laurent150dbfe2013-02-27 14:31:02 -08006385 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08006386 if (out->usecase == USECASE_COMPRESS_VOIP_CALL)
6387 ret = voice_extn_compress_voip_start_output_stream(out);
6388 else
6389 ret = start_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006390 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006391 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07006392 out->standby = true;
Weiyin Jiangac2bae82020-07-29 17:23:53 +08006393 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006394 goto exit;
6395 }
Ashish Jain1b9b30c2017-05-18 20:57:40 +05306396 out->started = 1;
Andy Hunga1f48fa2019-07-01 18:14:53 -07006397 out->last_fifo_valid = false; // we're coming out of standby, last_fifo isn't valid.
Weiyin Jiangac2bae82020-07-29 17:23:53 +08006398
6399 if ((last_known_cal_step != -1) && (adev->platform != NULL)) {
vivek mehtab72d08d2016-04-29 03:16:47 -07006400 ALOGD("%s: retry previous failed cal level set", __func__);
Weiyin Jiangac2bae82020-07-29 17:23:53 +08006401 platform_send_gain_dep_cal(adev->platform, last_known_cal_step);
Preetam Singh Ranawatf4ae0222017-05-31 17:07:28 +05306402 last_known_cal_step = -1;
vivek mehtab72d08d2016-04-29 03:16:47 -07006403 }
Weiyin Jiangac2bae82020-07-29 17:23:53 +08006404 pthread_mutex_unlock(&adev->lock);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05306405
6406 if ((out->is_iec61937_info_available == true) &&
6407 (audio_extn_passthru_is_passthrough_stream(out))&&
6408 (!audio_extn_passthru_is_supported_backend_edid_cfg(adev, out))) {
6409 ret = -EINVAL;
6410 goto exit;
6411 }
Surendar Karkaf51b5842018-04-26 11:28:38 +05306412 if (out->set_dual_mono)
6413 audio_extn_send_dual_mono_mixing_coefficients(out);
Andy Hungc6bfd4a2019-07-01 18:26:00 -07006414
Dechen Chai22768452021-07-30 09:29:16 +05306415#ifndef LINUX_ENABLED
Andy Hungc6bfd4a2019-07-01 18:26:00 -07006416 // log startup time in ms.
6417 simple_stats_log(
6418 &out->start_latency_ms, (systemTime(SYSTEM_TIME_MONOTONIC) - startNs) * 1e-6);
Dechen Chai22768452021-07-30 09:29:16 +05306419#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006420 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006421
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08006422 if (adev->is_channel_status_set == false &&
6423 compare_device_type(&out->device_list,
6424 AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
Alexy Josephb1379942016-01-29 15:49:38 -08006425 audio_utils_set_hdmi_channel_status(out, (void *)buffer, bytes);
Ashish Jain81eb2a82015-05-13 10:52:34 +05306426 adev->is_channel_status_set = true;
6427 }
6428
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +05306429 if ((adev->use_old_pspd_mix_ctrl == true) &&
6430 (out->pspd_coeff_sent == false)) {
6431 /*
6432 * Need to resend pspd coefficients after stream started for
6433 * older kernel version as it does not save the coefficients
6434 * and also stream has to be started for coeff to apply.
6435 */
6436 usecase = get_usecase_from_list(adev, out->usecase);
6437 if (usecase != NULL) {
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05306438 audio_extn_set_custom_mtmx_params_v2(adev, usecase, true);
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +05306439 out->pspd_coeff_sent = true;
6440 }
6441 }
6442
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07006443 if (is_offload_usecase(out->usecase)) {
Alexy Joseph01e54e62015-03-03 19:01:03 -08006444 ALOGVV("copl(%p): writing buffer (%zu bytes) to compress device", out, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07006445 if (out->send_new_metadata) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07006446 ALOGD("copl(%p):send new gapless metadata", out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07006447 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
6448 out->send_new_metadata = 0;
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05306449 if (out->send_next_track_params && out->is_compr_metadata_avail) {
6450 ALOGD("copl(%p):send next track params in gapless", out);
Weiyin Jiangd9b5a4e2019-07-18 17:24:21 +08006451 compress_set_next_track_param(out->compr, &(out->compr_config.codec->options));
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05306452 out->send_next_track_params = false;
6453 out->is_compr_metadata_avail = false;
6454 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07006455 }
Dhananjay Kumarac341582017-02-23 23:42:25 +05306456 if (!(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
Ashish Jain83a6cc22016-06-28 14:34:17 +05306457 (out->convert_buffer) != NULL) {
Haynes Mathew George352f27b2013-07-26 00:00:15 -07006458
Ashish Jain83a6cc22016-06-28 14:34:17 +05306459 if ((bytes > out->hal_fragment_size)) {
Ashish Jainf1eaa582016-05-23 20:54:24 +05306460 ALOGW("Error written bytes %zu > %d (fragment_size)",
Ashish Jain83a6cc22016-06-28 14:34:17 +05306461 bytes, out->hal_fragment_size);
Ashish Jainf1eaa582016-05-23 20:54:24 +05306462 pthread_mutex_unlock(&out->lock);
Haynes Mathew George380745d2017-10-04 15:27:45 -07006463 ATRACE_END();
Ashish Jainf1eaa582016-05-23 20:54:24 +05306464 return -EINVAL;
6465 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05306466 audio_format_t dst_format = out->hal_op_format;
6467 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05306468
Dieter Luecking5d57def2018-09-07 14:23:37 +02006469 /* prevent division-by-zero */
6470 uint32_t bitwidth_src = format_to_bitwidth_table[src_format];
6471 uint32_t bitwidth_dst = format_to_bitwidth_table[dst_format];
6472 if ((bitwidth_src == 0) || (bitwidth_dst == 0)) {
6473 ALOGE("%s: Error bitwidth == 0", __func__);
Ramu Gottipati02809682018-12-19 16:46:12 +05306474 pthread_mutex_unlock(&out->lock);
Dieter Luecking5d57def2018-09-07 14:23:37 +02006475 ATRACE_END();
6476 return -EINVAL;
6477 }
6478
Ashish Jainf1eaa582016-05-23 20:54:24 +05306479 uint32_t frames = bytes / format_to_bitwidth_table[src_format];
6480 uint32_t bytes_to_write = frames * format_to_bitwidth_table[dst_format];
6481
Ashish Jain83a6cc22016-06-28 14:34:17 +05306482 memcpy_by_audio_format(out->convert_buffer,
Ashish Jainf1eaa582016-05-23 20:54:24 +05306483 dst_format,
6484 buffer,
6485 src_format,
6486 frames);
6487
Ashish Jain83a6cc22016-06-28 14:34:17 +05306488 ret = compress_write(out->compr, out->convert_buffer,
Ashish Jainf1eaa582016-05-23 20:54:24 +05306489 bytes_to_write);
6490
6491 /*Convert written bytes in audio flinger format*/
6492 if (ret > 0)
6493 ret = ((ret * format_to_bitwidth_table[out->format]) /
6494 format_to_bitwidth_table[dst_format]);
6495 }
6496 } else
6497 ret = compress_write(out->compr, buffer, bytes);
6498
Zhou Songc9672822017-08-16 16:01:39 +08006499 if ((ret < 0 || ret == (ssize_t)bytes) && !out->non_blocking)
6500 update_frames_written(out, bytes);
6501
Dhanalakshmi Siddani37ca1d62014-08-20 12:28:34 +05306502 if (ret < 0)
6503 ret = -errno;
Weiyin Jiangcc60dbb2018-08-21 13:12:03 +08006504 ALOGVV("%s: writing buffer (%zu bytes) to compress device returned %d", __func__, bytes, (int)ret);
Ashish Jainb26edfb2016-08-25 00:10:11 +05306505 /*msg to cb thread only if non blocking write is enabled*/
6506 if (ret >= 0 && ret < (ssize_t)bytes && out->non_blocking) {
Sidipotu Ashok55820562014-02-10 16:16:38 +05306507 ALOGD("No space available in compress driver, post msg to cb thread");
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006508 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
Naresh Tanniru80659832014-06-04 18:17:56 +05306509 } else if (-ENETRESET == ret) {
6510 ALOGE("copl %s: received sound card offline state on compress write", __func__);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306511 out->card_status = CARD_STATUS_OFFLINE;
Naresh Tanniru80659832014-06-04 18:17:56 +05306512 pthread_mutex_unlock(&out->lock);
Dhananjay Kumar1248dd82017-07-28 21:22:16 +05306513 out_on_error(&out->stream.common);
Haynes Mathew George380745d2017-10-04 15:27:45 -07006514 ATRACE_END();
Naresh Tanniru80659832014-06-04 18:17:56 +05306515 return ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006516 }
Ashish Jain5106d362016-05-11 19:23:33 +05306517
Dhanalakshmi Siddania6b76c72016-09-09 18:10:31 +05306518 /* Call compr start only when non-zero bytes of data is there to be rendered */
6519 if (!out->playback_started && ret > 0) {
6520 int status = compress_start(out->compr);
6521 if (status < 0) {
6522 ret = status;
6523 ALOGE("%s: compr start failed with err %d", __func__, errno);
6524 goto exit;
6525 }
Alexy Joseph7de344d2015-03-30 10:40:03 -07006526 audio_extn_dts_eagle_fade(adev, true, out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006527 out->playback_started = 1;
Weiyin Jiang280ea742020-09-08 20:28:22 +08006528 pthread_mutex_lock(&out->latch_lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006529 out->offload_state = OFFLOAD_STATE_PLAYING;
Weiyin Jiang280ea742020-09-08 20:28:22 +08006530 pthread_mutex_unlock(&out->latch_lock);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08006531
6532 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
6533 popcount(out->channel_mask),
6534 out->playback_started);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006535 }
6536 pthread_mutex_unlock(&out->lock);
Haynes Mathew George380745d2017-10-04 15:27:45 -07006537 ATRACE_END();
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006538 return ret;
6539 } else {
6540 if (out->pcm) {
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006541 size_t bytes_to_write = bytes;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006542 if (out->muted)
6543 memset((void *)buffer, 0, bytes);
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006544 ALOGV("%s: frames=%zu, frame_size=%zu, bytes_to_write=%zu",
6545 __func__, frames, frame_size, bytes_to_write);
6546
Aalique Grahame22e49102018-12-18 14:23:57 -08006547 if (out->usecase == USECASE_INCALL_MUSIC_UPLINK ||
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07006548 out->usecase == USECASE_INCALL_MUSIC_UPLINK2 ||
6549 (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP &&
6550 !audio_extn_utils_is_vendor_enhanced_fwk())) {
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006551 size_t channel_count = audio_channel_count_from_out_mask(out->channel_mask);
6552 int16_t *src = (int16_t *)buffer;
6553 int16_t *dst = (int16_t *)buffer;
6554
Yunfei Zhanga6cd66d2019-07-22 16:15:36 +08006555 LOG_ALWAYS_FATAL_IF(channel_count > 2 ||
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006556 out->format != AUDIO_FORMAT_PCM_16_BIT,
Aalique Grahame22e49102018-12-18 14:23:57 -08006557 "out_write called for %s use case with wrong properties",
6558 use_case_table[out->usecase]);
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006559
6560 /*
6561 * FIXME: this can be removed once audio flinger mixer supports
6562 * mono output
6563 */
6564
6565 /*
6566 * Code below goes over each frame in the buffer and adds both
6567 * L and R samples and then divides by 2 to convert to mono
6568 */
Yunfei Zhanga6cd66d2019-07-22 16:15:36 +08006569 if (channel_count == 2) {
6570 for (size_t i = 0; i < frames ; i++, dst++, src += 2) {
6571 *dst = (int16_t)(((int32_t)src[0] + (int32_t)src[1]) >> 1);
6572 }
6573 bytes_to_write /= 2;
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006574 }
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006575 }
Andy Hunga1f48fa2019-07-01 18:14:53 -07006576
6577 // Note: since out_get_presentation_position() is called alternating with out_write()
6578 // by AudioFlinger, we can check underruns using the prior timestamp read.
6579 // (Alternately we could check if the buffer is empty using pcm_get_htimestamp().
6580 if (out->last_fifo_valid) {
6581 // compute drain to see if there is an underrun.
6582 const int64_t current_ns = systemTime(SYSTEM_TIME_MONOTONIC); // sys call
Dhananjay Kumara4429632020-07-11 02:53:37 +05306583 int64_t time_diff_ns = current_ns - out->last_fifo_time_ns;
6584 int64_t frames_by_time =
6585 ((time_diff_ns > 0) && (time_diff_ns < (INT64_MAX / out->config.rate))) ?
6586 (time_diff_ns * out->config.rate / NANOS_PER_SECOND) : 0;
Andy Hunga1f48fa2019-07-01 18:14:53 -07006587 const int64_t underrun = frames_by_time - out->last_fifo_frames_remaining;
6588
6589 if (underrun > 0) {
Dechen Chai22768452021-07-30 09:29:16 +05306590#ifndef LINUX_ENABLED
Andy Hunga1f48fa2019-07-01 18:14:53 -07006591 simple_stats_log(&out->fifo_underruns, underrun);
Dechen Chai22768452021-07-30 09:29:16 +05306592#endif
Andy Hunga1f48fa2019-07-01 18:14:53 -07006593
6594 ALOGW("%s: underrun(%lld) "
6595 "frames_by_time(%lld) > out->last_fifo_frames_remaining(%lld)",
6596 __func__,
6597 (long long)out->fifo_underruns.n,
6598 (long long)frames_by_time,
6599 (long long)out->last_fifo_frames_remaining);
6600 }
6601 out->last_fifo_valid = false; // we're writing below, mark fifo info as stale.
6602 }
6603
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05306604 ALOGVV("%s: writing buffer (%zu bytes) to pcm device", __func__, bytes);
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07006605
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006606 long ns = 0;
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07006607
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006608 if (out->config.rate)
6609 ns = pcm_bytes_to_frames(out->pcm, bytes)*1000000000LL/
6610 out->config.rate;
6611
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006612 request_out_focus(out, ns);
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006613 bool use_mmap = is_mmap_usecase(out->usecase) || out->realtime;
6614
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006615 if (use_mmap)
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006616 ret = pcm_mmap_write(out->pcm, (void *)buffer, bytes_to_write);
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006617 else if (out->hal_op_format != out->hal_ip_format &&
Ashish Jain83a6cc22016-06-28 14:34:17 +05306618 out->convert_buffer != NULL) {
6619
6620 memcpy_by_audio_format(out->convert_buffer,
6621 out->hal_op_format,
6622 buffer,
6623 out->hal_ip_format,
6624 out->config.period_size * out->config.channels);
6625
6626 ret = pcm_write(out->pcm, out->convert_buffer,
6627 (out->config.period_size *
6628 out->config.channels *
6629 format_to_bitwidth_table[out->hal_op_format]));
6630 } else {
Aditya Bavanarid4db8ee2017-05-29 21:08:03 +05306631 /*
6632 * To avoid underrun in DSP when the application is not pumping
6633 * data at required rate, check for the no. of bytes and ignore
6634 * pcm_write if it is less than actual buffer size.
6635 * It is a work around to a change in compress VOIP driver.
6636 */
6637 if ((out->flags & AUDIO_OUTPUT_FLAG_VOIP_RX) &&
6638 bytes < (out->config.period_size * out->config.channels *
6639 audio_bytes_per_sample(out->format))) {
6640 size_t voip_buf_size =
6641 out->config.period_size * out->config.channels *
6642 audio_bytes_per_sample(out->format);
6643 ALOGE("%s:VOIP underrun: bytes received %zu, required:%zu\n",
6644 __func__, bytes, voip_buf_size);
6645 usleep(((uint64_t)voip_buf_size - bytes) *
6646 1000000 / audio_stream_out_frame_size(stream) /
6647 out_get_sample_rate(&out->stream.common));
6648 ret = 0;
Vignesh Kulothungana6927272019-02-20 15:17:07 -08006649 } else {
6650 if (out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS)
6651 ret = split_and_write_audio_haptic_data(out, buffer, bytes);
6652 else
6653 ret = pcm_write(out->pcm, (void *)buffer, bytes_to_write);
6654 }
Ashish Jain83a6cc22016-06-28 14:34:17 +05306655 }
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07006656
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006657 release_out_focus(out);
6658
Dhanalakshmi Siddani37ca1d62014-08-20 12:28:34 +05306659 if (ret < 0)
6660 ret = -errno;
Zhou Songc9672822017-08-16 16:01:39 +08006661 else if (ret > 0)
Ashish Jain83a6cc22016-06-28 14:34:17 +05306662 ret = -EINVAL;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006663 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006664 }
6665
6666exit:
Zhou Songc9672822017-08-16 16:01:39 +08006667 update_frames_written(out, bytes);
Naresh Tanniru4c630392014-05-12 01:05:52 +05306668 if (-ENETRESET == ret) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306669 out->card_status = CARD_STATUS_OFFLINE;
Naresh Tanniru4c630392014-05-12 01:05:52 +05306670 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006671 pthread_mutex_unlock(&out->lock);
6672
6673 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07006674 if (out->pcm)
Alexy Josephb1379942016-01-29 15:49:38 -08006675 ALOGE("%s: error %d, %s", __func__, (int)ret, pcm_get_error(out->pcm));
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05306676 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05306677 pthread_mutex_lock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05306678 voice_extn_compress_voip_close_output_stream(&out->stream.common);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05306679 out->started = 0;
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05306680 pthread_mutex_unlock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05306681 out->standby = true;
6682 }
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306683 out_on_error(&out->stream.common);
Dieter Luecking5d57def2018-09-07 14:23:37 +02006684 if (!(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
6685 /* prevent division-by-zero */
6686 uint32_t stream_size = audio_stream_out_frame_size(stream);
6687 uint32_t srate = out_get_sample_rate(&out->stream.common);
Vidyakumar Athotaa9d3a5f2017-08-09 12:13:05 -07006688
Dieter Luecking5d57def2018-09-07 14:23:37 +02006689 if ((stream_size == 0) || (srate == 0)) {
6690 ALOGE("%s: stream_size= %d, srate = %d", __func__, stream_size, srate);
6691 ATRACE_END();
6692 return -EINVAL;
6693 }
6694 usleep((uint64_t)bytes * 1000000 / stream_size / srate);
6695 }
Vidyakumar Athotaa9d3a5f2017-08-09 12:13:05 -07006696 if (audio_extn_passthru_is_passthrough_stream(out)) {
Rajshekar Eashwarappa88834522018-04-02 17:20:15 +05306697 //ALOGE("%s: write error, ret = %zd", __func__, ret);
Haynes Mathew George380745d2017-10-04 15:27:45 -07006698 ATRACE_END();
Vidyakumar Athotaa9d3a5f2017-08-09 12:13:05 -07006699 return ret;
6700 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006701 }
Haynes Mathew George380745d2017-10-04 15:27:45 -07006702 ATRACE_END();
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006703 return bytes;
6704}
6705
6706static int out_get_render_position(const struct audio_stream_out *stream,
6707 uint32_t *dsp_frames)
6708{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006709 struct stream_out *out = (struct stream_out *)stream;
Zhou Song32a556e2015-05-05 10:46:56 +08006710
6711 if (dsp_frames == NULL)
6712 return -EINVAL;
6713
6714 *dsp_frames = 0;
6715 if (is_offload_usecase(out->usecase)) {
Mingming Yin9e348b52014-11-19 16:18:55 -08006716 ssize_t ret = 0;
Ashish Jain5106d362016-05-11 19:23:33 +05306717
6718 /* Below piece of code is not guarded against any lock beacuse audioFliner serializes
6719 * this operation and adev_close_output_stream(where out gets reset).
6720 */
Dhananjay Kumarac341582017-02-23 23:42:25 +05306721 if (!out->non_blocking && !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Zhou Song48453a02018-01-10 17:50:59 +08006722 *dsp_frames = get_actual_pcm_frames_rendered(out, NULL);
Ashish Jain5106d362016-05-11 19:23:33 +05306723 ALOGVV("dsp_frames %d sampleRate %d",(int)*dsp_frames,out->sample_rate);
Haynes Mathew Georgeb0f5dc32017-10-06 18:35:12 -07006724 adjust_frames_for_device_delay(out, dsp_frames);
Ashish Jain5106d362016-05-11 19:23:33 +05306725 return 0;
6726 }
6727
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006728 lock_output_stream(out);
Ashish Jain5106d362016-05-11 19:23:33 +05306729 if (out->compr != NULL && out->non_blocking) {
Naresh Tanniru80659832014-06-04 18:17:56 +05306730 ret = compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006731 &out->sample_rate);
Dhanalakshmi Siddani37ca1d62014-08-20 12:28:34 +05306732 if (ret < 0)
6733 ret = -errno;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006734 ALOGVV("%s rendered frames %d sample_rate %d",
Ashish Jain5106d362016-05-11 19:23:33 +05306735 __func__, *dsp_frames, out->sample_rate);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006736 }
Naresh Tanniru80659832014-06-04 18:17:56 +05306737 if (-ENETRESET == ret) {
6738 ALOGE(" ERROR: sound card not active Unable to get time stamp from compress driver");
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306739 out->card_status = CARD_STATUS_OFFLINE;
6740 ret = -EINVAL;
Naresh Tanniru80659832014-06-04 18:17:56 +05306741 } else if(ret < 0) {
6742 ALOGE(" ERROR: Unable to get time stamp from compress driver");
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306743 ret = -EINVAL;
Shubhasini Sugumaran3fde6582022-01-19 15:54:50 -05006744 } else if (out->card_status == CARD_STATUS_OFFLINE ||
6745 adev->out_power_policy == POWER_POLICY_STATUS_OFFLINE) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05306746 /*
6747 * Handle corner case where compress session is closed during SSR
6748 * and timestamp is queried
6749 */
6750 ALOGE(" ERROR: sound card not active, return error");
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306751 ret = -EINVAL;
Manisha Agarwal7b3e3772019-02-20 14:33:45 +05306752 } else if (out->prev_card_status_offline) {
6753 ALOGE("ERROR: previously sound card was offline,return error");
6754 ret = -EINVAL;
Naresh Tanniru80659832014-06-04 18:17:56 +05306755 } else {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306756 ret = 0;
Haynes Mathew Georgeb0f5dc32017-10-06 18:35:12 -07006757 adjust_frames_for_device_delay(out, dsp_frames);
Naresh Tanniru80659832014-06-04 18:17:56 +05306758 }
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306759 pthread_mutex_unlock(&out->lock);
6760 return ret;
Zhou Song32a556e2015-05-05 10:46:56 +08006761 } else if (audio_is_linear_pcm(out->format)) {
6762 *dsp_frames = out->written;
Haynes Mathew Georgeb0f5dc32017-10-06 18:35:12 -07006763 adjust_frames_for_device_delay(out, dsp_frames);
Zhou Song32a556e2015-05-05 10:46:56 +08006764 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006765 } else
6766 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006767}
6768
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07006769static int out_add_audio_effect(const struct audio_stream *stream __unused,
6770 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006771{
6772 return 0;
6773}
6774
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07006775static int out_remove_audio_effect(const struct audio_stream *stream __unused,
6776 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006777{
6778 return 0;
6779}
6780
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07006781static int out_get_next_write_timestamp(const struct audio_stream_out *stream __unused,
6782 int64_t *timestamp __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006783{
Satya Krishna Pindiprolib6655542017-07-03 19:38:19 +05306784 return -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006785}
6786
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07006787static int out_get_presentation_position(const struct audio_stream_out *stream,
6788 uint64_t *frames, struct timespec *timestamp)
6789{
6790 struct stream_out *out = (struct stream_out *)stream;
pavance65c2fe2017-10-18 17:52:01 +05306791 int ret = -ENODATA;
Eric Laurent949a0892013-09-20 09:20:13 -07006792 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07006793
Ashish Jain5106d362016-05-11 19:23:33 +05306794 /* below piece of code is not guarded against any lock because audioFliner serializes
6795 * this operation and adev_close_output_stream( where out gets reset).
6796 */
6797 if (is_offload_usecase(out->usecase) && !out->non_blocking &&
Dhananjay Kumarac341582017-02-23 23:42:25 +05306798 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Zhou Song48453a02018-01-10 17:50:59 +08006799 *frames = get_actual_pcm_frames_rendered(out, timestamp);
Ashish Jain5106d362016-05-11 19:23:33 +05306800 ALOGVV("frames %lld playedat %lld",(long long int)*frames,
6801 timestamp->tv_sec * 1000000LL + timestamp->tv_nsec / 1000);
6802 return 0;
6803 }
6804
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006805 lock_output_stream(out);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07006806
Ashish Jain5106d362016-05-11 19:23:33 +05306807 if (is_offload_usecase(out->usecase) && out->compr != NULL && out->non_blocking) {
6808 ret = compress_get_tstamp(out->compr, &dsp_frames,
6809 &out->sample_rate);
yidongh0515e042017-07-06 15:00:34 +08006810 // Adjustment accounts for A2dp encoder latency with offload usecases
6811 // Note: Encoder latency is returned in ms.
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08006812 if (is_a2dp_out_device_type(&out->device_list)) {
yidongh0515e042017-07-06 15:00:34 +08006813 unsigned long offset =
6814 (audio_extn_a2dp_get_encoder_latency() * out->sample_rate / 1000);
6815 dsp_frames = (dsp_frames > offset) ? (dsp_frames - offset) : 0;
6816 }
Ashish Jain5106d362016-05-11 19:23:33 +05306817 ALOGVV("%s rendered frames %ld sample_rate %d",
6818 __func__, dsp_frames, out->sample_rate);
6819 *frames = dsp_frames;
6820 if (ret < 0)
6821 ret = -errno;
6822 if (-ENETRESET == ret) {
6823 ALOGE(" ERROR: sound card not active Unable to get time stamp from compress driver");
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306824 out->card_status = CARD_STATUS_OFFLINE;
Ashish Jain5106d362016-05-11 19:23:33 +05306825 ret = -EINVAL;
6826 } else
6827 ret = 0;
6828 /* this is the best we can do */
6829 clock_gettime(CLOCK_MONOTONIC, timestamp);
Eric Laurent949a0892013-09-20 09:20:13 -07006830 } else {
6831 if (out->pcm) {
Weiyin Jiangd4633762018-03-16 12:05:03 +08006832 unsigned int avail;
6833 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
George Gao62ebc722019-07-29 16:29:44 -07006834 uint64_t signed_frames = 0;
Weiyin Jiang0d25bcf2019-08-30 15:27:23 +08006835 uint64_t frames_temp = 0;
George Gao62ebc722019-07-29 16:29:44 -07006836
Andy Hunga1f48fa2019-07-01 18:14:53 -07006837 if (out->kernel_buffer_size > avail) {
6838 frames_temp = out->last_fifo_frames_remaining = out->kernel_buffer_size - avail;
6839 } else {
6840 ALOGW("%s: avail:%u > kernel_buffer_size:%zu clamping!",
6841 __func__, avail, out->kernel_buffer_size);
6842 avail = out->kernel_buffer_size;
6843 frames_temp = out->last_fifo_frames_remaining = 0;
6844 }
6845 out->last_fifo_valid = true;
6846 out->last_fifo_time_ns = audio_utils_ns_from_timespec(timestamp);
6847
Weiyin Jiang0d25bcf2019-08-30 15:27:23 +08006848 if (out->written >= frames_temp)
6849 signed_frames = out->written - frames_temp;
George Gao62ebc722019-07-29 16:29:44 -07006850
Andy Hunga1f48fa2019-07-01 18:14:53 -07006851 ALOGVV("%s: frames:%lld avail:%u kernel_buffer_size:%zu",
6852 __func__, (long long)signed_frames, avail, out->kernel_buffer_size);
6853
Weiyin Jiangd4633762018-03-16 12:05:03 +08006854 // This adjustment accounts for buffering after app processor.
6855 // It is based on estimated DSP latency per use case, rather than exact.
George Gao9ba8a142020-07-23 14:30:03 -07006856 frames_temp = platform_render_latency(out) *
Robert Lee58215542019-07-15 20:55:12 +08006857 out->sample_rate / 1000000LL;
Weiyin Jiang0d25bcf2019-08-30 15:27:23 +08006858 if (signed_frames >= frames_temp)
6859 signed_frames -= frames_temp;
Aniket Kumar Lataff613152017-07-18 18:19:21 -07006860
Weiyin Jiangd4633762018-03-16 12:05:03 +08006861 // Adjustment accounts for A2dp encoder latency with non offload usecases
6862 // Note: Encoder latency is returned in ms, while platform_render_latency in us.
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08006863 if (is_a2dp_out_device_type(&out->device_list)) {
Weiyin Jiang0d25bcf2019-08-30 15:27:23 +08006864 frames_temp = audio_extn_a2dp_get_encoder_latency() * out->sample_rate / 1000;
6865 if (signed_frames >= frames_temp)
6866 signed_frames -= frames_temp;
Weiyin Jiangd4633762018-03-16 12:05:03 +08006867 }
6868
6869 // It would be unusual for this value to be negative, but check just in case ...
George Gao62ebc722019-07-29 16:29:44 -07006870 *frames = signed_frames;
6871 ret = 0;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07006872 }
Eric Laurenta7a33042019-07-10 16:20:22 -07006873 } else if (out->card_status == CARD_STATUS_OFFLINE ||
Shubhasini Sugumaran3fde6582022-01-19 15:54:50 -05006874 adev->out_power_policy == POWER_POLICY_STATUS_OFFLINE ||
Eric Laurenta7a33042019-07-10 16:20:22 -07006875 // audioflinger still needs position updates when A2DP is suspended
Jasmine Cha5c2517f2019-09-09 11:07:28 +08006876 (is_a2dp_out_device_type(&out->device_list) && audio_extn_a2dp_source_is_suspended())) {
Ashish Jainbbce4322016-02-16 13:25:27 +05306877 *frames = out->written;
6878 clock_gettime(CLOCK_MONOTONIC, timestamp);
Manisha Agarwal7b3e3772019-02-20 14:33:45 +05306879 if (is_offload_usecase(out->usecase))
6880 ret = -EINVAL;
6881 else
6882 ret = 0;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07006883 }
6884 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07006885 pthread_mutex_unlock(&out->lock);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07006886 return ret;
6887}
6888
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006889static int out_set_callback(struct audio_stream_out *stream,
6890 stream_callback_t callback, void *cookie)
6891{
6892 struct stream_out *out = (struct stream_out *)stream;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08006893 int ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006894
6895 ALOGV("%s", __func__);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006896 lock_output_stream(out);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08006897 out->client_callback = callback;
6898 out->client_cookie = cookie;
6899 if (out->adsp_hdlr_stream_handle) {
6900 ret = audio_extn_adsp_hdlr_stream_set_callback(
6901 out->adsp_hdlr_stream_handle,
6902 callback,
6903 cookie);
6904 if (ret)
6905 ALOGW("%s:adsp hdlr callback registration failed %d",
6906 __func__, ret);
6907 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006908 pthread_mutex_unlock(&out->lock);
6909 return 0;
6910}
6911
6912static int out_pause(struct audio_stream_out* stream)
6913{
6914 struct stream_out *out = (struct stream_out *)stream;
6915 int status = -ENOSYS;
6916 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07006917 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07006918 ALOGD("copl(%p):pause compress driver", out);
Sujin Panicker7c7b6f92020-04-03 12:57:55 +05306919 status = -ENODATA;
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006920 lock_output_stream(out);
Weiyin Jiang280ea742020-09-08 20:28:22 +08006921 pthread_mutex_lock(&out->latch_lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006922 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306923 if (out->card_status != CARD_STATUS_OFFLINE)
Naresh Tanniru80659832014-06-04 18:17:56 +05306924 status = compress_pause(out->compr);
6925
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006926 out->offload_state = OFFLOAD_STATE_PAUSED;
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08006927
Mingming Yin21854652016-04-13 11:54:02 -07006928 if (audio_extn_passthru_is_active()) {
6929 ALOGV("offload use case, pause passthru");
6930 audio_extn_passthru_on_pause(out);
6931 }
6932
Dhanalakshmi Siddani79415e72015-03-23 11:54:47 +05306933 audio_extn_dts_eagle_fade(adev, false, out);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08006934 audio_extn_dts_notify_playback_state(out->usecase, 0,
6935 out->sample_rate, popcount(out->channel_mask),
6936 0);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006937 }
Weiyin Jiang280ea742020-09-08 20:28:22 +08006938 pthread_mutex_unlock(&out->latch_lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006939 pthread_mutex_unlock(&out->lock);
6940 }
6941 return status;
6942}
6943
6944static int out_resume(struct audio_stream_out* stream)
6945{
6946 struct stream_out *out = (struct stream_out *)stream;
6947 int status = -ENOSYS;
6948 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07006949 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07006950 ALOGD("copl(%p):resume compress driver", out);
Sujin Panicker7c7b6f92020-04-03 12:57:55 +05306951 status = -ENODATA;
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006952 lock_output_stream(out);
Weiyin Jiang280ea742020-09-08 20:28:22 +08006953 pthread_mutex_lock(&out->latch_lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006954 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306955 if (out->card_status != CARD_STATUS_OFFLINE) {
Naresh Tanniru80659832014-06-04 18:17:56 +05306956 status = compress_resume(out->compr);
Mingming Yin21854652016-04-13 11:54:02 -07006957 }
6958 if (!status) {
6959 out->offload_state = OFFLOAD_STATE_PLAYING;
6960 }
Dhanalakshmi Siddani79415e72015-03-23 11:54:47 +05306961 audio_extn_dts_eagle_fade(adev, true, out);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08006962 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
6963 popcount(out->channel_mask), 1);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006964 }
Weiyin Jiang280ea742020-09-08 20:28:22 +08006965 pthread_mutex_unlock(&out->latch_lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006966 pthread_mutex_unlock(&out->lock);
6967 }
6968 return status;
6969}
6970
6971static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
6972{
6973 struct stream_out *out = (struct stream_out *)stream;
6974 int status = -ENOSYS;
6975 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07006976 if (is_offload_usecase(out->usecase)) {
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006977 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006978 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
6979 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
6980 else
6981 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
6982 pthread_mutex_unlock(&out->lock);
6983 }
6984 return status;
6985}
6986
6987static int out_flush(struct audio_stream_out* stream)
6988{
6989 struct stream_out *out = (struct stream_out *)stream;
6990 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07006991 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07006992 ALOGD("copl(%p):calling compress flush", out);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006993 lock_output_stream(out);
Weiyin Jiang280ea742020-09-08 20:28:22 +08006994 pthread_mutex_lock(&out->latch_lock);
Haynes Mathew Georgeafe54d82016-09-21 14:39:19 -07006995 if (out->offload_state == OFFLOAD_STATE_PAUSED) {
Gautam Manam14c198b2020-12-24 14:08:04 +05306996 pthread_mutex_unlock(&out->latch_lock);
Haynes Mathew Georgeafe54d82016-09-21 14:39:19 -07006997 stop_compressed_output_l(out);
Haynes Mathew Georgeafe54d82016-09-21 14:39:19 -07006998 } else {
6999 ALOGW("%s called in invalid state %d", __func__, out->offload_state);
Gautam Manam14c198b2020-12-24 14:08:04 +05307000 pthread_mutex_unlock(&out->latch_lock);
Haynes Mathew Georgeafe54d82016-09-21 14:39:19 -07007001 }
Weiyin Jiang547e4152017-09-14 17:24:18 +08007002 out->written = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07007003 pthread_mutex_unlock(&out->lock);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07007004 ALOGD("copl(%p):out of compress flush", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07007005 return 0;
7006 }
7007 return -ENOSYS;
7008}
7009
Haynes Mathew George16081042017-05-31 17:16:49 -07007010static int out_stop(const struct audio_stream_out* stream)
7011{
7012 struct stream_out *out = (struct stream_out *)stream;
7013 struct audio_device *adev = out->dev;
7014 int ret = -ENOSYS;
7015
7016 ALOGV("%s", __func__);
7017 pthread_mutex_lock(&adev->lock);
7018 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP && !out->standby &&
7019 out->playback_started && out->pcm != NULL) {
7020 pcm_stop(out->pcm);
7021 ret = stop_output_stream(out);
7022 out->playback_started = false;
7023 }
7024 pthread_mutex_unlock(&adev->lock);
7025 return ret;
7026}
7027
7028static int out_start(const struct audio_stream_out* stream)
7029{
7030 struct stream_out *out = (struct stream_out *)stream;
7031 struct audio_device *adev = out->dev;
7032 int ret = -ENOSYS;
7033
7034 ALOGV("%s", __func__);
7035 pthread_mutex_lock(&adev->lock);
7036 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP && !out->standby &&
7037 !out->playback_started && out->pcm != NULL) {
7038 ret = start_output_stream(out);
7039 if (ret == 0) {
7040 out->playback_started = true;
7041 }
7042 }
7043 pthread_mutex_unlock(&adev->lock);
7044 return ret;
7045}
7046
7047/*
7048 * Modify config->period_count based on min_size_frames
7049 */
7050static void adjust_mmap_period_count(struct pcm_config *config, int32_t min_size_frames)
7051{
7052 int periodCountRequested = (min_size_frames + config->period_size - 1)
7053 / config->period_size;
7054 int periodCount = MMAP_PERIOD_COUNT_MIN;
7055
7056 ALOGV("%s original config.period_size = %d config.period_count = %d",
7057 __func__, config->period_size, config->period_count);
7058
7059 while (periodCount < periodCountRequested && (periodCount * 2) < MMAP_PERIOD_COUNT_MAX) {
7060 periodCount *= 2;
7061 }
7062 config->period_count = periodCount;
7063
7064 ALOGV("%s requested config.period_count = %d", __func__, config->period_count);
7065}
7066
Phil Burkfe17efd2019-03-25 10:23:35 -07007067// Read offset for the positional timestamp from a persistent vendor property.
7068// This is to workaround apparent inaccuracies in the timing information that
7069// is used by the AAudio timing model. The inaccuracies can cause glitches.
7070static int64_t get_mmap_out_time_offset() {
7071 const int32_t kDefaultOffsetMicros = 0;
7072 int32_t mmap_time_offset_micros = property_get_int32(
Weiyin Jiangbd68b4d2019-05-17 16:29:50 +08007073 "persist.vendor.audio.out_mmap_delay_micros", kDefaultOffsetMicros);
Phil Burkfe17efd2019-03-25 10:23:35 -07007074 ALOGI("mmap_time_offset_micros = %d for output", mmap_time_offset_micros);
7075 return mmap_time_offset_micros * (int64_t)1000;
7076}
7077
Haynes Mathew George16081042017-05-31 17:16:49 -07007078static int out_create_mmap_buffer(const struct audio_stream_out *stream,
7079 int32_t min_size_frames,
7080 struct audio_mmap_buffer_info *info)
7081{
7082 struct stream_out *out = (struct stream_out *)stream;
7083 struct audio_device *adev = out->dev;
7084 int ret = 0;
Aalique Grahame1f123102017-10-12 10:38:32 -07007085 unsigned int offset1 = 0;
7086 unsigned int frames1 = 0;
Haynes Mathew George16081042017-05-31 17:16:49 -07007087 const char *step = "";
Haynes Mathew Georgeef514882017-05-01 17:46:23 -07007088 uint32_t mmap_size;
Arun Mirpuri5d170872019-03-26 13:21:31 -07007089 uint32_t buffer_size;
Haynes Mathew George16081042017-05-31 17:16:49 -07007090
Arun Mirpuri5d170872019-03-26 13:21:31 -07007091 ALOGD("%s", __func__);
Sharad Sanglec6f32552018-05-04 16:15:38 +05307092 lock_output_stream(out);
Haynes Mathew George16081042017-05-31 17:16:49 -07007093 pthread_mutex_lock(&adev->lock);
7094
Sharad Sanglec6f32552018-05-04 16:15:38 +05307095 if (CARD_STATUS_OFFLINE == out->card_status ||
Shubhasini Sugumaran3fde6582022-01-19 15:54:50 -05007096 CARD_STATUS_OFFLINE == adev->card_status ||
7097 POWER_POLICY_STATUS_OFFLINE == adev->out_power_policy) {
Sharad Sanglec6f32552018-05-04 16:15:38 +05307098 ALOGW("out->card_status or adev->card_status offline, try again");
7099 ret = -EIO;
7100 goto exit;
7101 }
Sujin Panicker7c7b6f92020-04-03 12:57:55 +05307102 if (info == NULL || !(min_size_frames > 0 && min_size_frames < INT32_MAX)) {
Haynes Mathew George16081042017-05-31 17:16:49 -07007103 ALOGE("%s: info = %p, min_size_frames = %d", __func__, info, min_size_frames);
7104 ret = -EINVAL;
7105 goto exit;
7106 }
7107 if (out->usecase != USECASE_AUDIO_PLAYBACK_MMAP || !out->standby) {
7108 ALOGE("%s: usecase = %d, standby = %d", __func__, out->usecase, out->standby);
7109 ret = -ENOSYS;
7110 goto exit;
7111 }
7112 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
7113 if (out->pcm_device_id < 0) {
7114 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
7115 __func__, out->pcm_device_id, out->usecase);
7116 ret = -EINVAL;
7117 goto exit;
7118 }
7119
7120 adjust_mmap_period_count(&out->config, min_size_frames);
7121
Arun Mirpuri5d170872019-03-26 13:21:31 -07007122 ALOGD("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
Haynes Mathew George16081042017-05-31 17:16:49 -07007123 __func__, adev->snd_card, out->pcm_device_id, out->config.channels);
7124 out->pcm = pcm_open(adev->snd_card, out->pcm_device_id,
7125 (PCM_OUT | PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC), &out->config);
Satish Babu Patakokila54ce83d2018-07-06 18:00:37 +05307126 if (errno == ENETRESET && !pcm_is_ready(out->pcm)) {
Sharad Sanglec6f32552018-05-04 16:15:38 +05307127 ALOGE("%s: pcm_open failed errno:%d\n", __func__, errno);
7128 out->card_status = CARD_STATUS_OFFLINE;
7129 adev->card_status = CARD_STATUS_OFFLINE;
7130 ret = -EIO;
7131 goto exit;
7132 }
7133
Haynes Mathew George16081042017-05-31 17:16:49 -07007134 if (out->pcm == NULL || !pcm_is_ready(out->pcm)) {
7135 step = "open";
7136 ret = -ENODEV;
7137 goto exit;
7138 }
7139 ret = pcm_mmap_begin(out->pcm, &info->shared_memory_address, &offset1, &frames1);
7140 if (ret < 0) {
7141 step = "begin";
7142 goto exit;
7143 }
juyuchen626833d2019-06-04 16:48:02 +08007144
7145 info->flags = 0;
Haynes Mathew George16081042017-05-31 17:16:49 -07007146 info->buffer_size_frames = pcm_get_buffer_size(out->pcm);
Arun Mirpuri5d170872019-03-26 13:21:31 -07007147 buffer_size = pcm_frames_to_bytes(out->pcm, info->buffer_size_frames);
Haynes Mathew George16081042017-05-31 17:16:49 -07007148 info->burst_size_frames = out->config.period_size;
Haynes Mathew Georgeef514882017-05-01 17:46:23 -07007149 ret = platform_get_mmap_data_fd(adev->platform,
7150 out->pcm_device_id, 0 /*playback*/,
7151 &info->shared_memory_fd,
7152 &mmap_size);
7153 if (ret < 0) {
Arun Mirpuri5d170872019-03-26 13:21:31 -07007154 // Fall back to non exclusive mode
7155 info->shared_memory_fd = pcm_get_poll_fd(out->pcm);
7156 } else {
Phil Burkd898ba62019-06-20 12:49:01 -07007157 out->mmap_shared_memory_fd = info->shared_memory_fd; // for closing later
7158 ALOGV("%s: opened mmap_shared_memory_fd = %d", __func__, out->mmap_shared_memory_fd);
7159
Arun Mirpuri5d170872019-03-26 13:21:31 -07007160 if (mmap_size < buffer_size) {
7161 step = "mmap";
7162 goto exit;
7163 }
juyuchen626833d2019-06-04 16:48:02 +08007164 info->flags |= AUDIO_MMAP_APPLICATION_SHAREABLE;
Haynes Mathew Georgeef514882017-05-01 17:46:23 -07007165 }
Haynes Mathew George16081042017-05-31 17:16:49 -07007166 memset(info->shared_memory_address, 0, pcm_frames_to_bytes(out->pcm,
Haynes Mathew Georgeef514882017-05-01 17:46:23 -07007167 info->buffer_size_frames));
Haynes Mathew George16081042017-05-31 17:16:49 -07007168
7169 ret = pcm_mmap_commit(out->pcm, 0, MMAP_PERIOD_SIZE);
7170 if (ret < 0) {
7171 step = "commit";
7172 goto exit;
7173 }
7174
Phil Burkfe17efd2019-03-25 10:23:35 -07007175 out->mmap_time_offset_nanos = get_mmap_out_time_offset();
7176
Haynes Mathew George16081042017-05-31 17:16:49 -07007177 out->standby = false;
7178 ret = 0;
7179
Arun Mirpuri5d170872019-03-26 13:21:31 -07007180 ALOGD("%s: got mmap buffer address %p info->buffer_size_frames %d",
Haynes Mathew George16081042017-05-31 17:16:49 -07007181 __func__, info->shared_memory_address, info->buffer_size_frames);
7182
7183exit:
7184 if (ret != 0) {
7185 if (out->pcm == NULL) {
7186 ALOGE("%s: %s - %d", __func__, step, ret);
7187 } else {
7188 ALOGE("%s: %s %s", __func__, step, pcm_get_error(out->pcm));
7189 pcm_close(out->pcm);
7190 out->pcm = NULL;
7191 }
7192 }
7193 pthread_mutex_unlock(&adev->lock);
Sharad Sanglec6f32552018-05-04 16:15:38 +05307194 pthread_mutex_unlock(&out->lock);
Haynes Mathew George16081042017-05-31 17:16:49 -07007195 return ret;
7196}
7197
7198static int out_get_mmap_position(const struct audio_stream_out *stream,
7199 struct audio_mmap_position *position)
7200{
7201 struct stream_out *out = (struct stream_out *)stream;
7202 ALOGVV("%s", __func__);
7203 if (position == NULL) {
7204 return -EINVAL;
7205 }
7206 if (out->usecase != USECASE_AUDIO_PLAYBACK_MMAP) {
Haynes Mathew George4ab3ba92017-12-11 14:49:43 -08007207 ALOGE("%s: called on %s", __func__, use_case_table[out->usecase]);
Haynes Mathew George16081042017-05-31 17:16:49 -07007208 return -ENOSYS;
7209 }
7210 if (out->pcm == NULL) {
7211 return -ENOSYS;
7212 }
7213
7214 struct timespec ts = { 0, 0 };
7215 int ret = pcm_mmap_get_hw_ptr(out->pcm, (unsigned int *)&position->position_frames, &ts);
7216 if (ret < 0) {
7217 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
7218 return ret;
7219 }
Phil Burkfe17efd2019-03-25 10:23:35 -07007220 position->time_nanoseconds = ts.tv_sec*1000000000LL + ts.tv_nsec
7221 + out->mmap_time_offset_nanos;
Haynes Mathew George16081042017-05-31 17:16:49 -07007222 return 0;
7223}
7224
7225
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007226/** audio_stream_in implementation **/
7227static uint32_t in_get_sample_rate(const struct audio_stream *stream)
7228{
7229 struct stream_in *in = (struct stream_in *)stream;
7230
7231 return in->config.rate;
7232}
7233
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07007234static int in_set_sample_rate(struct audio_stream *stream __unused,
7235 uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007236{
7237 return -ENOSYS;
7238}
7239
7240static size_t in_get_buffer_size(const struct audio_stream *stream)
7241{
7242 struct stream_in *in = (struct stream_in *)stream;
7243
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007244 if(in->usecase == USECASE_COMPRESS_VOIP_CALL)
7245 return voice_extn_compress_voip_in_get_buffer_size(in);
Mingming Yine62d7842013-10-25 16:26:03 -07007246 else if(audio_extn_compr_cap_usecase_supported(in->usecase))
Raghu Bankapur37cbf382022-12-01 09:30:00 +05307247 return audio_extn_compr_cap_get_buffer_size(pcm_format_to_audio_format(in->config.format));
Dhananjay Kumar7dbe3562019-08-30 05:49:33 +05307248 else if(audio_extn_cin_attached_usecase(in))
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307249 return audio_extn_cin_get_buffer_size(in);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007250
Haynes Mathew George5beddd42016-06-27 18:33:40 -07007251 return in->config.period_size * in->af_period_multiplier *
7252 audio_stream_in_frame_size((const struct audio_stream_in *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007253}
7254
7255static uint32_t in_get_channels(const struct audio_stream *stream)
7256{
7257 struct stream_in *in = (struct stream_in *)stream;
7258
7259 return in->channel_mask;
7260}
7261
7262static audio_format_t in_get_format(const struct audio_stream *stream)
7263{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007264 struct stream_in *in = (struct stream_in *)stream;
7265
7266 return in->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007267}
7268
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07007269static int in_set_format(struct audio_stream *stream __unused,
7270 audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007271{
7272 return -ENOSYS;
7273}
7274
7275static int in_standby(struct audio_stream *stream)
7276{
7277 struct stream_in *in = (struct stream_in *)stream;
7278 struct audio_device *adev = in->dev;
7279 int status = 0;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05307280 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
7281 stream, in->usecase, use_case_table[in->usecase]);
Haynes Mathew George16081042017-05-31 17:16:49 -07007282 bool do_stop = true;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05307283
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07007284 lock_input_stream(in);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07007285 if (!in->standby && in->is_st_session) {
7286 ALOGD("%s: sound trigger pcm stop lab", __func__);
7287 audio_extn_sound_trigger_stop_lab(in);
George Gao3018ede2019-10-23 13:23:00 -07007288 if (adev->num_va_sessions > 0)
7289 adev->num_va_sessions--;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07007290 in->standby = 1;
7291 }
7292
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007293 if (!in->standby) {
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07007294 if (adev->adm_deregister_stream)
7295 adev->adm_deregister_stream(adev->adm_data, in->capture_handle);
7296
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08007297 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007298 in->standby = true;
Zhou Songa8895042016-07-05 17:54:22 +08007299 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
kunleizbecba2d2017-09-07 13:37:16 +08007300 do_stop = false;
Zhou Songa8895042016-07-05 17:54:22 +08007301 voice_extn_compress_voip_close_input_stream(stream);
7302 ALOGD("VOIP input entered standby");
Haynes Mathew George16081042017-05-31 17:16:49 -07007303 } else if (in->usecase == USECASE_AUDIO_RECORD_MMAP) {
7304 do_stop = in->capture_started;
7305 in->capture_started = false;
Phil Burkd898ba62019-06-20 12:49:01 -07007306 if (in->mmap_shared_memory_fd >= 0) {
7307 ALOGV("%s: closing mmap_shared_memory_fd = %d",
7308 __func__, in->mmap_shared_memory_fd);
7309 close(in->mmap_shared_memory_fd);
7310 in->mmap_shared_memory_fd = -1;
7311 }
Zhou Songa8895042016-07-05 17:54:22 +08007312 } else {
Dhananjay Kumar7dbe3562019-08-30 05:49:33 +05307313 if (audio_extn_cin_attached_usecase(in))
Manish Dewangan46e07982018-12-13 18:18:59 +05307314 audio_extn_cin_close_input_stream(in);
kunleizbecba2d2017-09-07 13:37:16 +08007315 }
7316
Arun Mirpuri5d170872019-03-26 13:21:31 -07007317 if (in->pcm) {
7318 ATRACE_BEGIN("pcm_in_close");
7319 pcm_close(in->pcm);
7320 ATRACE_END();
7321 in->pcm = NULL;
7322 }
7323
Carter Hsu2e429db2019-05-14 18:50:52 +08007324 if (do_stop)
Zhou Songa8895042016-07-05 17:54:22 +08007325 status = stop_input_stream(in);
Quinn Malef6050362019-01-30 15:55:40 -08007326
George Gao3018ede2019-10-23 13:23:00 -07007327 if (in->source == AUDIO_SOURCE_VOICE_RECOGNITION) {
7328 if (adev->num_va_sessions > 0)
7329 adev->num_va_sessions--;
7330 }
Quinn Malef6050362019-01-30 15:55:40 -08007331
Eric Laurent150dbfe2013-02-27 14:31:02 -08007332 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007333 }
7334 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07007335 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007336 return status;
7337}
7338
Aalique Grahame22e49102018-12-18 14:23:57 -08007339static int in_dump(const struct audio_stream *stream,
7340 int fd)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007341{
Aalique Grahame22e49102018-12-18 14:23:57 -08007342 struct stream_in *in = (struct stream_in *)stream;
7343
7344 // We try to get the lock for consistency,
7345 // but it isn't necessary for these variables.
7346 // If we're not in standby, we may be blocked on a read.
7347 const bool locked = (pthread_mutex_trylock(&in->lock) == 0);
7348 dprintf(fd, " Standby: %s\n", in->standby ? "yes" : "no");
7349 dprintf(fd, " Frames read: %lld\n", (long long)in->frames_read);
7350 dprintf(fd, " Frames muted: %lld\n", (long long)in->frames_muted);
Dechen Chai22768452021-07-30 09:29:16 +05307351#ifndef LINUX_ENABLED
Andy Hungc6bfd4a2019-07-01 18:26:00 -07007352 char buffer[256]; // for statistics formatting
7353 if (in->start_latency_ms.n > 0) {
7354 simple_stats_to_string(&in->start_latency_ms, buffer, sizeof(buffer));
7355 dprintf(fd, " Start latency ms: %s\n", buffer);
7356 }
Dechen Chai22768452021-07-30 09:29:16 +05307357#endif
Aalique Grahame22e49102018-12-18 14:23:57 -08007358 if (locked) {
7359 pthread_mutex_unlock(&in->lock);
7360 }
Dechen Chai22768452021-07-30 09:29:16 +05307361#ifndef LINUX_ENABLED
Aalique Grahame22e49102018-12-18 14:23:57 -08007362 // dump error info
7363 (void)error_log_dump(
7364 in->error_log, fd, " " /* prefix */, 0 /* lines */, 0 /* limit_ns */);
Dechen Chai22768452021-07-30 09:29:16 +05307365#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007366 return 0;
7367}
7368
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05307369static void in_snd_mon_cb(void * stream, struct str_parms * parms)
7370{
7371 if (!stream || !parms)
7372 return;
7373
7374 struct stream_in *in = (struct stream_in *)stream;
7375 struct audio_device *adev = in->dev;
7376
7377 card_status_t status;
7378 int card;
7379 if (parse_snd_card_status(parms, &card, &status) < 0)
7380 return;
7381
7382 pthread_mutex_lock(&adev->lock);
7383 bool valid_cb = (card == adev->snd_card);
7384 pthread_mutex_unlock(&adev->lock);
7385
7386 if (!valid_cb)
7387 return;
7388
7389 lock_input_stream(in);
7390 if (in->card_status != status)
7391 in->card_status = status;
7392 pthread_mutex_unlock(&in->lock);
7393
7394 ALOGW("in_snd_mon_cb for card %d usecase %s, status %s", card,
7395 use_case_table[in->usecase],
7396 status == CARD_STATUS_OFFLINE ? "offline" : "online");
7397
7398 // a better solution would be to report error back to AF and let
7399 // it put the stream to standby
7400 if (status == CARD_STATUS_OFFLINE)
7401 in_standby(&in->stream.common);
7402
7403 return;
7404}
7405
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07007406int route_input_stream(struct stream_in *in,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08007407 struct listnode *devices,
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07007408 audio_source_t source)
7409{
7410 struct audio_device *adev = in->dev;
7411 int ret = 0;
7412
7413 lock_input_stream(in);
7414 pthread_mutex_lock(&adev->lock);
7415
7416 /* no audio source uses val == 0 */
7417 if ((in->source != source) && (source != AUDIO_SOURCE_DEFAULT)) {
7418 in->source = source;
7419 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
7420 (in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
7421 (voice_extn_compress_voip_is_format_supported(in->format)) &&
7422 (in->config.rate == 8000 || in->config.rate == 16000 ||
7423 in->config.rate == 32000 || in->config.rate == 48000 ) &&
7424 (audio_channel_count_from_in_mask(in->channel_mask) == 1)) {
7425 ret = voice_extn_compress_voip_open_input_stream(in);
7426 if (ret != 0) {
7427 ALOGE("%s: Compress voip input cannot be opened, error:%d",
7428 __func__, ret);
7429 }
7430 }
7431 }
7432
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08007433 if (!compare_devices(&in->device_list, devices) && !list_empty(devices) &&
7434 is_audio_in_device_type(devices)) {
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07007435 // Workaround: If routing to an non existing usb device, fail gracefully
7436 // The routing request will otherwise block during 10 second
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08007437 struct str_parms *usb_addr =
7438 str_parms_create_str(get_usb_device_address(devices));
7439 if (is_usb_in_device_type(devices) && usb_addr &&
Weiyin Jiangabedea32020-12-09 12:49:19 +08007440 !audio_extn_usb_connected(NULL)) {
7441 ALOGW("%s: ignoring rerouting to non existing USB", __func__);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07007442 ret = -ENOSYS;
7443 } else {
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07007444 /* If recording is in progress, change the tx device to new device */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08007445 assign_devices(&in->device_list, devices);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07007446 if (!in->standby && !in->is_st_session) {
7447 ALOGV("update input routing change");
7448 // inform adm before actual routing to prevent glitches.
7449 if (adev->adm_on_routing_change) {
7450 adev->adm_on_routing_change(adev->adm_data,
7451 in->capture_handle);
7452 ret = select_devices(adev, in->usecase);
7453 if (in->usecase == USECASE_AUDIO_RECORD_LOW_LATENCY)
7454 adev->adm_routing_changed = true;
7455 }
7456 }
7457 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08007458 if (usb_addr)
7459 str_parms_destroy(usb_addr);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07007460 }
7461 pthread_mutex_unlock(&adev->lock);
7462 pthread_mutex_unlock(&in->lock);
7463
Aniket Kumar Latad13758f2020-08-06 15:11:36 -07007464 ALOGV("%s: exit: status(%d)", __func__, ret);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07007465 return ret;
7466}
7467
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007468static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
7469{
7470 struct stream_in *in = (struct stream_in *)stream;
7471 struct audio_device *adev = in->dev;
7472 struct str_parms *parms;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007473 char value[32];
Sujin Panicker7c7b6f92020-04-03 12:57:55 +05307474 int err = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007475
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05307476 ALOGD("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007477 parms = str_parms_create_str(kvpairs);
7478
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05307479 if (!parms)
7480 goto error;
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07007481 lock_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08007482 pthread_mutex_lock(&adev->lock);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08007483
Sujin Panicker7c7b6f92020-04-03 12:57:55 +05307484 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_PROFILE, value, sizeof(value));
7485 if (err >= 0) {
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +05307486 strlcpy(in->profile, value, sizeof(in->profile));
7487 ALOGV("updating stream profile with value '%s'", in->profile);
7488 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
7489 &adev->streams_input_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08007490 &in->device_list, in->flags, in->format,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +05307491 in->sample_rate, in->bit_width,
7492 in->profile, &in->app_type_cfg);
7493 }
7494
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007495 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08007496 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007497
7498 str_parms_destroy(parms);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05307499error:
Sujin Panicker7c7b6f92020-04-03 12:57:55 +05307500 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007501}
7502
7503static char* in_get_parameters(const struct audio_stream *stream,
7504 const char *keys)
7505{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007506 struct stream_in *in = (struct stream_in *)stream;
7507 struct str_parms *query = str_parms_create_str(keys);
7508 char *str;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007509 struct str_parms *reply = str_parms_create();
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07007510
7511 if (!query || !reply) {
Alexy Josephaee4fdd2016-01-29 13:02:07 -08007512 if (reply) {
7513 str_parms_destroy(reply);
7514 }
7515 if (query) {
7516 str_parms_destroy(query);
7517 }
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07007518 ALOGE("in_get_parameters: failed to create query or reply");
7519 return NULL;
7520 }
7521
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007522 ALOGV("%s: enter: keys - %s %s ", __func__, use_case_table[in->usecase], keys);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007523
7524 voice_extn_in_get_parameters(in, query, reply);
7525
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007526 stream_get_parameter_channels(query, reply,
7527 &in->supported_channel_masks[0]);
7528 stream_get_parameter_formats(query, reply,
7529 &in->supported_formats[0]);
7530 stream_get_parameter_rates(query, reply,
7531 &in->supported_sample_rates[0]);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007532 str = str_parms_to_str(reply);
7533 str_parms_destroy(query);
7534 str_parms_destroy(reply);
7535
7536 ALOGV("%s: exit: returns - %s", __func__, str);
7537 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007538}
7539
Aalique Grahame22e49102018-12-18 14:23:57 -08007540static int in_set_gain(struct audio_stream_in *stream,
7541 float gain)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007542{
Aalique Grahame22e49102018-12-18 14:23:57 -08007543 struct stream_in *in = (struct stream_in *)stream;
7544 char mixer_ctl_name[128];
7545 struct mixer_ctl *ctl;
7546 int ctl_value;
7547
7548 ALOGV("%s: gain %f", __func__, gain);
7549
7550 if (stream == NULL)
7551 return -EINVAL;
7552
7553 /* in_set_gain() only used to silence MMAP capture for now */
7554 if (in->usecase != USECASE_AUDIO_RECORD_MMAP)
7555 return -ENOSYS;
7556
7557 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name), "Capture %d Volume", in->pcm_device_id);
7558
7559 ctl = mixer_get_ctl_by_name(in->dev->mixer, mixer_ctl_name);
7560 if (!ctl) {
7561 ALOGW("%s: Could not get ctl for mixer cmd - %s",
7562 __func__, mixer_ctl_name);
7563 return -ENOSYS;
7564 }
7565
7566 if (gain < RECORD_GAIN_MIN)
7567 gain = RECORD_GAIN_MIN;
7568 else if (gain > RECORD_GAIN_MAX)
7569 gain = RECORD_GAIN_MAX;
7570 ctl_value = (int)(RECORD_VOLUME_CTL_MAX * gain);
7571
7572 mixer_ctl_set_value(ctl, 0, ctl_value);
7573
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007574 return 0;
7575}
7576
7577static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
7578 size_t bytes)
7579{
7580 struct stream_in *in = (struct stream_in *)stream;
Pallavid7c7a272018-01-16 11:22:55 +05307581
7582 if (in == NULL) {
7583 ALOGE("%s: stream_in ptr is NULL", __func__);
7584 return -EINVAL;
7585 }
7586
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007587 struct audio_device *adev = in->dev;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05307588 int ret = -1;
Dhananjay Kumar97b81e32019-05-15 21:00:21 +05307589 size_t bytes_read = 0, frame_size = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007590
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07007591 lock_input_stream(in);
Naresh Tanniru4c630392014-05-12 01:05:52 +05307592
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07007593 if (in->is_st_session) {
7594 ALOGVV(" %s: reading on st session bytes=%zu", __func__, bytes);
7595 /* Read from sound trigger HAL */
7596 audio_extn_sound_trigger_read(in, buffer, bytes);
Quinn Malef6050362019-01-30 15:55:40 -08007597 if (in->standby) {
George Gao3018ede2019-10-23 13:23:00 -07007598 if (adev->num_va_sessions < UINT_MAX)
7599 adev->num_va_sessions++;
Quinn Malef6050362019-01-30 15:55:40 -08007600 in->standby = 0;
7601 }
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07007602 pthread_mutex_unlock(&in->lock);
7603 return bytes;
7604 }
7605
Haynes Mathew George16081042017-05-31 17:16:49 -07007606 if (in->usecase == USECASE_AUDIO_RECORD_MMAP) {
7607 ret = -ENOSYS;
7608 goto exit;
7609 }
7610
Aniket Kumar Lata60586a92019-05-22 22:18:55 -07007611 if (in->usecase == USECASE_AUDIO_RECORD_LOW_LATENCY &&
7612 !in->standby && adev->adm_routing_changed) {
7613 ret = -ENOSYS;
7614 goto exit;
7615 }
7616
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007617 if (in->standby) {
Andy Hungc6bfd4a2019-07-01 18:26:00 -07007618 const int64_t startNs = systemTime(SYSTEM_TIME_MONOTONIC);
7619
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07007620 pthread_mutex_lock(&adev->lock);
7621 if (in->usecase == USECASE_COMPRESS_VOIP_CALL)
7622 ret = voice_extn_compress_voip_start_input_stream(in);
7623 else
7624 ret = start_input_stream(in);
George Gao3018ede2019-10-23 13:23:00 -07007625 if (!ret && in->source == AUDIO_SOURCE_VOICE_RECOGNITION) {
7626 if (adev->num_va_sessions < UINT_MAX)
7627 adev->num_va_sessions++;
7628 }
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07007629 pthread_mutex_unlock(&adev->lock);
7630 if (ret != 0) {
7631 goto exit;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007632 }
7633 in->standby = 0;
Dechen Chai22768452021-07-30 09:29:16 +05307634#ifndef LINUX_ENABLED
Andy Hungc6bfd4a2019-07-01 18:26:00 -07007635 // log startup time in ms.
7636 simple_stats_log(
7637 &in->start_latency_ms, (systemTime(SYSTEM_TIME_MONOTONIC) - startNs) * 1e-6);
Dechen Chai22768452021-07-30 09:29:16 +05307638#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007639 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007640
Deeraj Soman30cc7ae2019-03-18 16:26:55 +05307641 /* Avoid read if capture_stopped is set */
7642 if (android_atomic_acquire_load(&(in->capture_stopped)) > 0) {
7643 ALOGD("%s: force stopped catpure session, ignoring read request", __func__);
7644 ret = -EINVAL;
7645 goto exit;
7646 }
7647
Haynes Mathew George5beddd42016-06-27 18:33:40 -07007648 // what's the duration requested by the client?
7649 long ns = 0;
7650
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307651 if (in->pcm && in->config.rate)
Haynes Mathew George5beddd42016-06-27 18:33:40 -07007652 ns = pcm_bytes_to_frames(in->pcm, bytes)*1000000000LL/
7653 in->config.rate;
7654
Aniket Kumar Lata60586a92019-05-22 22:18:55 -07007655 ret = request_in_focus(in, ns);
7656 if (ret != 0)
7657 goto exit;
Haynes Mathew George5beddd42016-06-27 18:33:40 -07007658 bool use_mmap = is_mmap_usecase(in->usecase) || in->realtime;
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07007659
Dhananjay Kumar7dbe3562019-08-30 05:49:33 +05307660 if (audio_extn_cin_attached_usecase(in)) {
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307661 ret = audio_extn_cin_read(in, buffer, bytes, &bytes_read);
7662 } else if (in->pcm) {
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05307663 if (audio_extn_ssr_get_stream() == in) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07007664 ret = audio_extn_ssr_read(stream, buffer, bytes);
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05307665 } else if (audio_extn_compr_cap_usecase_supported(in->usecase)) {
Mingming Yine62d7842013-10-25 16:26:03 -07007666 ret = audio_extn_compr_cap_read(in, buffer, bytes);
Haynes Mathew George5beddd42016-06-27 18:33:40 -07007667 } else if (use_mmap) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07007668 ret = pcm_mmap_read(in->pcm, buffer, bytes);
Garmond Leunge2433c32017-09-28 21:51:22 -07007669 } else if (audio_extn_ffv_get_stream() == in) {
7670 ret = audio_extn_ffv_read(stream, buffer, bytes);
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05307671 } else {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07007672 ret = pcm_read(in->pcm, buffer, bytes);
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307673 /* data from DSP comes in 24_8 format, convert it to 8_24 */
7674 if (!ret && bytes > 0 && (in->format == AUDIO_FORMAT_PCM_8_24_BIT)) {
7675 if (audio_extn_utils_convert_format_24_8_to_8_24(buffer, bytes)
7676 != bytes) {
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05307677 ret = -EINVAL;
7678 goto exit;
7679 }
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307680 } else if (ret < 0) {
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05307681 ret = -errno;
7682 }
7683 }
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307684 /* bytes read is always set to bytes for non compress usecases */
7685 bytes_read = bytes;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007686 }
7687
Haynes Mathew George5beddd42016-06-27 18:33:40 -07007688 release_in_focus(in);
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07007689
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007690 /*
Quinn Malef6050362019-01-30 15:55:40 -08007691 * Instead of writing zeroes here, we could trust the hardware to always
7692 * provide zeroes when muted. This is also muted with voice recognition
7693 * usecases so that other clients do not have access to voice recognition
7694 * data.
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007695 */
Quinn Malef6050362019-01-30 15:55:40 -08007696 if ((ret == 0 && voice_get_mic_mute(adev) &&
7697 !voice_is_in_call_rec_stream(in) &&
Zhou Song62ea0282020-03-22 19:53:01 +08007698 (in->usecase != USECASE_AUDIO_RECORD_AFE_PROXY &&
Manoj Kumar N D54cab152023-01-27 16:41:55 +05307699 in->usecase != USECASE_AUDIO_RECORD_AFE_PROXY2 &&
7700 in->source != AUDIO_SOURCE_FM_TUNER &&
7701 !is_single_device_type_equal(&in->device_list, AUDIO_DEVICE_IN_FM_TUNER))) ||
Quinn Malef6050362019-01-30 15:55:40 -08007702 (adev->num_va_sessions &&
7703 in->source != AUDIO_SOURCE_VOICE_RECOGNITION &&
7704 property_get_bool("persist.vendor.audio.va_concurrency_mute_enabled",
7705 false)))
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007706 memset(buffer, 0, bytes);
7707
7708exit:
Dhananjay Kumar97b81e32019-05-15 21:00:21 +05307709 frame_size = audio_stream_in_frame_size(stream);
7710 if (frame_size > 0)
7711 in->frames_read += bytes_read/frame_size;
7712
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07007713 if (-ENETRESET == ret)
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05307714 in->card_status = CARD_STATUS_OFFLINE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007715 pthread_mutex_unlock(&in->lock);
7716
7717 if (ret != 0) {
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05307718 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05307719 pthread_mutex_lock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05307720 voice_extn_compress_voip_close_input_stream(&in->stream.common);
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05307721 pthread_mutex_unlock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05307722 in->standby = true;
7723 }
Dhananjay Kumar7dbe3562019-08-30 05:49:33 +05307724 if (!audio_extn_cin_attached_usecase(in)) {
Sharad Sangled17c9122017-03-20 15:58:52 +05307725 bytes_read = bytes;
7726 memset(buffer, 0, bytes);
7727 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007728 in_standby(&in->stream.common);
Aniket Kumar Lata60586a92019-05-22 22:18:55 -07007729 if (in->usecase == USECASE_AUDIO_RECORD_LOW_LATENCY)
7730 adev->adm_routing_changed = false;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07007731 ALOGV("%s: read failed status %d- sleeping for buffer duration", __func__, ret);
Ashish Jainbbce4322016-02-16 13:25:27 +05307732 usleep((uint64_t)bytes * 1000000 / audio_stream_in_frame_size(stream) /
Naresh Tanniru4c630392014-05-12 01:05:52 +05307733 in_get_sample_rate(&in->stream.common));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007734 }
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307735 return bytes_read;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007736}
7737
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07007738static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007739{
7740 return 0;
7741}
7742
Aalique Grahame22e49102018-12-18 14:23:57 -08007743static int in_get_capture_position(const struct audio_stream_in *stream,
7744 int64_t *frames, int64_t *time)
7745{
7746 if (stream == NULL || frames == NULL || time == NULL) {
7747 return -EINVAL;
7748 }
7749 struct stream_in *in = (struct stream_in *)stream;
7750 int ret = -ENOSYS;
7751
7752 lock_input_stream(in);
7753 // note: ST sessions do not close the alsa pcm driver synchronously
7754 // on standby. Therefore, we may return an error even though the
7755 // pcm stream is still opened.
7756 if (in->standby) {
7757 ALOGE_IF(in->pcm != NULL && !in->is_st_session,
7758 "%s stream in standby but pcm not NULL for non ST session", __func__);
7759 goto exit;
7760 }
7761 if (in->pcm) {
7762 struct timespec timestamp;
7763 unsigned int avail;
7764 if (pcm_get_htimestamp(in->pcm, &avail, &timestamp) == 0) {
7765 *frames = in->frames_read + avail;
Robert Lee58215542019-07-15 20:55:12 +08007766 *time = timestamp.tv_sec * 1000000000LL + timestamp.tv_nsec
George Gao9ba8a142020-07-23 14:30:03 -07007767 - platform_capture_latency(in) * 1000LL;
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +05307768 //Adjustment accounts for A2dp decoder latency for recording usecase
7769 // Note: decoder latency is returned in ms, while platform_capture_latency in ns.
7770 if (is_a2dp_in_device_type(&in->device_list))
7771 *time -= audio_extn_a2dp_get_decoder_latency() * 1000000LL;
Aalique Grahame22e49102018-12-18 14:23:57 -08007772 ret = 0;
7773 }
7774 }
7775exit:
7776 pthread_mutex_unlock(&in->lock);
7777 return ret;
7778}
7779
Carter Hsu2e429db2019-05-14 18:50:52 +08007780static int in_update_effect_list(bool add, effect_handle_t effect,
7781 struct listnode *head)
7782{
7783 struct listnode *node;
7784 struct in_effect_list *elist = NULL;
7785 struct in_effect_list *target = NULL;
7786 int ret = 0;
7787
7788 if (!head)
7789 return ret;
7790
7791 list_for_each(node, head) {
7792 elist = node_to_item(node, struct in_effect_list, list);
7793 if (elist->handle == effect) {
7794 target = elist;
7795 break;
7796 }
7797 }
7798
7799 if (add) {
7800 if (target) {
7801 ALOGD("effect %p already exist", effect);
7802 return ret;
7803 }
7804
7805 target = (struct in_effect_list *)
7806 calloc(1, sizeof(struct in_effect_list));
7807
7808 if (!target) {
7809 ALOGE("%s:fail to allocate memory", __func__);
7810 return -ENOMEM;
7811 }
7812
7813 target->handle = effect;
7814 list_add_tail(head, &target->list);
7815 } else {
7816 if (target) {
7817 list_remove(&target->list);
7818 free(target);
7819 }
7820 }
7821
7822 return ret;
7823}
7824
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007825static int add_remove_audio_effect(const struct audio_stream *stream,
7826 effect_handle_t effect,
7827 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007828{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007829 struct stream_in *in = (struct stream_in *)stream;
7830 int status = 0;
7831 effect_descriptor_t desc;
7832
7833 status = (*effect)->get_descriptor(effect, &desc);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07007834 ALOGV("%s: status %d in->standby %d enable:%d", __func__, status, in->standby, enable);
7835
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007836 if (status != 0)
7837 return status;
7838
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07007839 lock_input_stream(in);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007840 pthread_mutex_lock(&in->dev->lock);
kunleizd96526c2018-04-09 11:12:32 +08007841 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
Carter Hsu2e429db2019-05-14 18:50:52 +08007842 in->source == AUDIO_SOURCE_VOICE_RECOGNITION ||
7843 adev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007844 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
Carter Hsu2e429db2019-05-14 18:50:52 +08007845
7846 in_update_effect_list(enable, effect, &in->aec_list);
7847 enable = !list_empty(&in->aec_list);
7848 if (enable == in->enable_aec)
7849 goto exit;
7850
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007851 in->enable_aec = enable;
Carter Hsu2e429db2019-05-14 18:50:52 +08007852 ALOGD("AEC enable %d", enable);
7853
Aalique Grahame22e49102018-12-18 14:23:57 -08007854 if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
7855 in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) {
7856 in->dev->enable_voicerx = enable;
7857 struct audio_usecase *usecase;
7858 struct listnode *node;
7859 list_for_each(node, &in->dev->usecase_list) {
7860 usecase = node_to_item(node, struct audio_usecase, list);
7861 if (usecase->type == PCM_PLAYBACK)
7862 select_devices(in->dev, usecase->id);
7863 }
7864 }
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07007865 if (!in->standby) {
7866 if (enable_disable_effect(in->dev, EFFECT_AEC, enable) == ENOSYS)
7867 select_devices(in->dev, in->usecase);
7868 }
7869
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007870 }
Carter Hsu2e429db2019-05-14 18:50:52 +08007871 if (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0) {
7872
7873 in_update_effect_list(enable, effect, &in->ns_list);
7874 enable = !list_empty(&in->ns_list);
7875 if (enable == in->enable_ns)
7876 goto exit;
7877
Ravi Kumar Alamanda198185e2013-11-07 15:42:19 -08007878 in->enable_ns = enable;
Carter Hsu2e429db2019-05-14 18:50:52 +08007879 ALOGD("NS enable %d", enable);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07007880 if (!in->standby) {
kunleizd96526c2018-04-09 11:12:32 +08007881 if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
Ramu Gottipatifa5be522021-12-28 19:18:21 +05307882 in->source == AUDIO_SOURCE_VOICE_RECOGNITION ||
kunleizd96526c2018-04-09 11:12:32 +08007883 in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) {
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07007884 if (enable_disable_effect(in->dev, EFFECT_NS, enable) == ENOSYS)
7885 select_devices(in->dev, in->usecase);
7886 } else
7887 select_devices(in->dev, in->usecase);
7888 }
Ravi Kumar Alamanda198185e2013-11-07 15:42:19 -08007889 }
Carter Hsu2e429db2019-05-14 18:50:52 +08007890exit:
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007891 pthread_mutex_unlock(&in->dev->lock);
7892 pthread_mutex_unlock(&in->lock);
7893
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007894 return 0;
7895}
7896
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007897static int in_add_audio_effect(const struct audio_stream *stream,
7898 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007899{
Eric Laurent994a6932013-07-17 11:51:42 -07007900 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007901 return add_remove_audio_effect(stream, effect, true);
7902}
7903
7904static int in_remove_audio_effect(const struct audio_stream *stream,
7905 effect_handle_t effect)
7906{
Eric Laurent994a6932013-07-17 11:51:42 -07007907 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007908 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007909}
7910
Haynes Mathew George16081042017-05-31 17:16:49 -07007911static int in_stop(const struct audio_stream_in* stream)
7912{
7913 struct stream_in *in = (struct stream_in *)stream;
7914 struct audio_device *adev = in->dev;
7915
7916 int ret = -ENOSYS;
7917 ALOGV("%s", __func__);
7918 pthread_mutex_lock(&adev->lock);
7919 if (in->usecase == USECASE_AUDIO_RECORD_MMAP && !in->standby &&
7920 in->capture_started && in->pcm != NULL) {
7921 pcm_stop(in->pcm);
7922 ret = stop_input_stream(in);
7923 in->capture_started = false;
7924 }
7925 pthread_mutex_unlock(&adev->lock);
7926 return ret;
7927}
7928
7929static int in_start(const struct audio_stream_in* stream)
7930{
7931 struct stream_in *in = (struct stream_in *)stream;
7932 struct audio_device *adev = in->dev;
7933 int ret = -ENOSYS;
7934
7935 ALOGV("%s in %p", __func__, in);
7936 pthread_mutex_lock(&adev->lock);
7937 if (in->usecase == USECASE_AUDIO_RECORD_MMAP && !in->standby &&
7938 !in->capture_started && in->pcm != NULL) {
7939 if (!in->capture_started) {
7940 ret = start_input_stream(in);
7941 if (ret == 0) {
7942 in->capture_started = true;
7943 }
7944 }
7945 }
7946 pthread_mutex_unlock(&adev->lock);
7947 return ret;
7948}
7949
Phil Burke0a86d12019-02-16 22:28:11 -08007950// Read offset for the positional timestamp from a persistent vendor property.
7951// This is to workaround apparent inaccuracies in the timing information that
7952// is used by the AAudio timing model. The inaccuracies can cause glitches.
7953static int64_t in_get_mmap_time_offset() {
7954 const int32_t kDefaultOffsetMicros = 0;
7955 int32_t mmap_time_offset_micros = property_get_int32(
Weiyin Jiangbd68b4d2019-05-17 16:29:50 +08007956 "persist.vendor.audio.in_mmap_delay_micros", kDefaultOffsetMicros);
Phil Burke0a86d12019-02-16 22:28:11 -08007957 ALOGI("mmap_time_offset_micros = %d for input", mmap_time_offset_micros);
7958 return mmap_time_offset_micros * (int64_t)1000;
7959}
7960
Haynes Mathew George16081042017-05-31 17:16:49 -07007961static int in_create_mmap_buffer(const struct audio_stream_in *stream,
7962 int32_t min_size_frames,
7963 struct audio_mmap_buffer_info *info)
7964{
7965 struct stream_in *in = (struct stream_in *)stream;
7966 struct audio_device *adev = in->dev;
7967 int ret = 0;
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07007968 unsigned int offset1 = 0;
7969 unsigned int frames1 = 0;
Haynes Mathew George16081042017-05-31 17:16:49 -07007970 const char *step = "";
Arun Mirpuri5d170872019-03-26 13:21:31 -07007971 uint32_t mmap_size = 0;
7972 uint32_t buffer_size = 0;
Haynes Mathew George16081042017-05-31 17:16:49 -07007973
7974 pthread_mutex_lock(&adev->lock);
7975 ALOGV("%s in %p", __func__, in);
7976
Sharad Sanglec6f32552018-05-04 16:15:38 +05307977 if (CARD_STATUS_OFFLINE == in->card_status||
Shubhasini Sugumaran3fde6582022-01-19 15:54:50 -05007978 CARD_STATUS_OFFLINE == adev->card_status ||
7979 POWER_POLICY_STATUS_OFFLINE == adev->in_power_policy) {
Sharad Sanglec6f32552018-05-04 16:15:38 +05307980 ALOGW("in->card_status or adev->card_status offline, try again");
7981 ret = -EIO;
7982 goto exit;
7983 }
7984
Sujin Panicker7c7b6f92020-04-03 12:57:55 +05307985 if (info == NULL || !(min_size_frames > 0 && min_size_frames < INT32_MAX)) {
Haynes Mathew George16081042017-05-31 17:16:49 -07007986 ALOGE("%s invalid argument info %p min_size_frames %d", __func__, info, min_size_frames);
7987 ret = -EINVAL;
7988 goto exit;
7989 }
7990 if (in->usecase != USECASE_AUDIO_RECORD_MMAP || !in->standby) {
7991 ALOGE("%s: usecase = %d, standby = %d", __func__, in->usecase, in->standby);
7992 ALOGV("%s in %p", __func__, in);
7993 ret = -ENOSYS;
7994 goto exit;
7995 }
7996 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
7997 if (in->pcm_device_id < 0) {
7998 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
7999 __func__, in->pcm_device_id, in->usecase);
8000 ret = -EINVAL;
8001 goto exit;
8002 }
8003
8004 adjust_mmap_period_count(&in->config, min_size_frames);
8005
8006 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
8007 __func__, adev->snd_card, in->pcm_device_id, in->config.channels);
8008 in->pcm = pcm_open(adev->snd_card, in->pcm_device_id,
8009 (PCM_IN | PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC), &in->config);
Satish Babu Patakokila54ce83d2018-07-06 18:00:37 +05308010 if (errno == ENETRESET && !pcm_is_ready(in->pcm)) {
Sharad Sanglec6f32552018-05-04 16:15:38 +05308011 ALOGE("%s: pcm_open failed errno:%d\n", __func__, errno);
8012 in->card_status = CARD_STATUS_OFFLINE;
8013 adev->card_status = CARD_STATUS_OFFLINE;
8014 ret = -EIO;
8015 goto exit;
8016 }
8017
Haynes Mathew George16081042017-05-31 17:16:49 -07008018 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) {
8019 step = "open";
8020 ret = -ENODEV;
8021 goto exit;
8022 }
8023
8024 ret = pcm_mmap_begin(in->pcm, &info->shared_memory_address, &offset1, &frames1);
8025 if (ret < 0) {
8026 step = "begin";
8027 goto exit;
8028 }
Haynes Mathew George16081042017-05-31 17:16:49 -07008029
juyuchen626833d2019-06-04 16:48:02 +08008030 info->flags = 0;
Arun Mirpuri5d170872019-03-26 13:21:31 -07008031 info->buffer_size_frames = pcm_get_buffer_size(in->pcm);
8032 buffer_size = pcm_frames_to_bytes(in->pcm, info->buffer_size_frames);
8033 info->burst_size_frames = in->config.period_size;
8034 ret = platform_get_mmap_data_fd(adev->platform,
8035 in->pcm_device_id, 1 /*capture*/,
8036 &info->shared_memory_fd,
8037 &mmap_size);
8038 if (ret < 0) {
8039 // Fall back to non exclusive mode
8040 info->shared_memory_fd = pcm_get_poll_fd(in->pcm);
8041 } else {
Phil Burkd898ba62019-06-20 12:49:01 -07008042 in->mmap_shared_memory_fd = info->shared_memory_fd; // for closing later
8043 ALOGV("%s: opened mmap_shared_memory_fd = %d", __func__, in->mmap_shared_memory_fd);
8044
Arun Mirpuri5d170872019-03-26 13:21:31 -07008045 if (mmap_size < buffer_size) {
8046 step = "mmap";
8047 goto exit;
8048 }
juyuchen626833d2019-06-04 16:48:02 +08008049 info->flags |= AUDIO_MMAP_APPLICATION_SHAREABLE;
Arun Mirpuri5d170872019-03-26 13:21:31 -07008050 }
8051
8052 memset(info->shared_memory_address, 0, buffer_size);
Haynes Mathew George16081042017-05-31 17:16:49 -07008053
8054 ret = pcm_mmap_commit(in->pcm, 0, MMAP_PERIOD_SIZE);
8055 if (ret < 0) {
8056 step = "commit";
8057 goto exit;
8058 }
8059
Phil Burke0a86d12019-02-16 22:28:11 -08008060 in->mmap_time_offset_nanos = in_get_mmap_time_offset();
8061
Haynes Mathew George16081042017-05-31 17:16:49 -07008062 in->standby = false;
8063 ret = 0;
8064
8065 ALOGV("%s: got mmap buffer address %p info->buffer_size_frames %d",
8066 __func__, info->shared_memory_address, info->buffer_size_frames);
8067
8068exit:
8069 if (ret != 0) {
8070 if (in->pcm == NULL) {
8071 ALOGE("%s: %s - %d", __func__, step, ret);
8072 } else {
8073 ALOGE("%s: %s %s", __func__, step, pcm_get_error(in->pcm));
8074 pcm_close(in->pcm);
8075 in->pcm = NULL;
8076 }
8077 }
8078 pthread_mutex_unlock(&adev->lock);
8079 return ret;
8080}
8081
8082static int in_get_mmap_position(const struct audio_stream_in *stream,
8083 struct audio_mmap_position *position)
8084{
8085 struct stream_in *in = (struct stream_in *)stream;
8086 ALOGVV("%s", __func__);
8087 if (position == NULL) {
8088 return -EINVAL;
8089 }
Gautam Manam34d1f542021-01-05 20:24:37 +05308090 lock_input_stream(in);
Haynes Mathew George16081042017-05-31 17:16:49 -07008091 if (in->usecase != USECASE_AUDIO_RECORD_MMAP) {
Gautam Manam34d1f542021-01-05 20:24:37 +05308092 pthread_mutex_unlock(&in->lock);
Haynes Mathew George16081042017-05-31 17:16:49 -07008093 return -ENOSYS;
8094 }
8095 if (in->pcm == NULL) {
Gautam Manam34d1f542021-01-05 20:24:37 +05308096 pthread_mutex_unlock(&in->lock);
Haynes Mathew George16081042017-05-31 17:16:49 -07008097 return -ENOSYS;
8098 }
8099 struct timespec ts = { 0, 0 };
8100 int ret = pcm_mmap_get_hw_ptr(in->pcm, (unsigned int *)&position->position_frames, &ts);
8101 if (ret < 0) {
8102 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
Gautam Manam34d1f542021-01-05 20:24:37 +05308103 pthread_mutex_unlock(&in->lock);
Haynes Mathew George16081042017-05-31 17:16:49 -07008104 return ret;
8105 }
Phil Burke0a86d12019-02-16 22:28:11 -08008106 position->time_nanoseconds = ts.tv_sec*1000000000LL + ts.tv_nsec
8107 + in->mmap_time_offset_nanos;
Gautam Manam34d1f542021-01-05 20:24:37 +05308108 pthread_mutex_unlock(&in->lock);
Haynes Mathew George16081042017-05-31 17:16:49 -07008109 return 0;
8110}
8111
Naresh Tannirudcb47c52018-06-25 16:23:32 +05308112static int in_get_active_microphones(const struct audio_stream_in *stream,
8113 struct audio_microphone_characteristic_t *mic_array,
8114 size_t *mic_count) {
8115 struct stream_in *in = (struct stream_in *)stream;
8116 struct audio_device *adev = in->dev;
8117 ALOGVV("%s", __func__);
8118
8119 lock_input_stream(in);
8120 pthread_mutex_lock(&adev->lock);
8121 int ret = platform_get_active_microphones(adev->platform,
8122 audio_channel_count_from_in_mask(in->channel_mask),
8123 in->usecase, mic_array, mic_count);
8124 pthread_mutex_unlock(&adev->lock);
8125 pthread_mutex_unlock(&in->lock);
8126
8127 return ret;
8128}
8129
8130static int adev_get_microphones(const struct audio_hw_device *dev,
8131 struct audio_microphone_characteristic_t *mic_array,
8132 size_t *mic_count) {
8133 struct audio_device *adev = (struct audio_device *)dev;
8134 ALOGVV("%s", __func__);
8135
8136 pthread_mutex_lock(&adev->lock);
8137 int ret = platform_get_microphones(adev->platform, mic_array, mic_count);
8138 pthread_mutex_unlock(&adev->lock);
8139
8140 return ret;
8141}
juyuchendb308c22019-01-21 11:57:17 -07008142
8143static void in_update_sink_metadata(struct audio_stream_in *stream,
8144 const struct sink_metadata *sink_metadata) {
8145
8146 if (stream == NULL
8147 || sink_metadata == NULL
8148 || sink_metadata->tracks == NULL) {
8149 return;
8150 }
8151
8152 int error = 0;
8153 struct stream_in *in = (struct stream_in *)stream;
8154 struct audio_device *adev = in->dev;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08008155 struct listnode devices;
8156
8157 list_init(&devices);
juyuchendb308c22019-01-21 11:57:17 -07008158
8159 if (sink_metadata->track_count != 0)
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08008160 reassign_device_list(&devices, sink_metadata->tracks->dest_device, "");
juyuchendb308c22019-01-21 11:57:17 -07008161
8162 lock_input_stream(in);
8163 pthread_mutex_lock(&adev->lock);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08008164 ALOGV("%s: in->usecase: %d, device: %x", __func__, in->usecase, get_device_types(&devices));
juyuchendb308c22019-01-21 11:57:17 -07008165
Zhou Song503196b2021-07-23 17:31:05 +08008166 if ((in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY ||
8167 in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY2) &&
8168 !list_empty(&devices) &&
8169 adev->voice_tx_output != NULL) {
juyuchendb308c22019-01-21 11:57:17 -07008170 /* Use the rx device from afe-proxy record to route voice call because
8171 there is no routing if tx device is on primary hal and rx device
8172 is on other hal during voice call. */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08008173 assign_devices(&adev->voice_tx_output->device_list, &devices);
juyuchendb308c22019-01-21 11:57:17 -07008174
8175 if (!voice_is_call_state_active(adev)) {
8176 if (adev->mode == AUDIO_MODE_IN_CALL) {
8177 adev->current_call_output = adev->voice_tx_output;
8178 error = voice_start_call(adev);
8179 if (error != 0)
8180 ALOGE("%s: start voice call failed %d", __func__, error);
8181 }
8182 } else {
8183 adev->current_call_output = adev->voice_tx_output;
8184 voice_update_devices_for_all_voice_usecases(adev);
8185 }
8186 }
8187
Zhenlin Lian4f947842022-05-14 15:50:52 +05308188 clear_devices(&devices);
juyuchendb308c22019-01-21 11:57:17 -07008189 pthread_mutex_unlock(&adev->lock);
8190 pthread_mutex_unlock(&in->lock);
8191}
8192
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05308193int adev_open_output_stream(struct audio_hw_device *dev,
Haynes Mathew George16081042017-05-31 17:16:49 -07008194 audio_io_handle_t handle,
8195 audio_devices_t devices,
8196 audio_output_flags_t flags,
8197 struct audio_config *config,
8198 struct audio_stream_out **stream_out,
Derek Chenf6318be2017-06-12 17:16:24 -04008199 const char *address)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008200{
8201 struct audio_device *adev = (struct audio_device *)dev;
8202 struct stream_out *out;
Gangadhar Sb0210342019-02-22 17:39:41 +05308203 int ret = 0, ip_hdlr_stream = 0, ip_hdlr_dev = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07008204 audio_format_t format;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08008205 struct adsp_hdlr_stream_cfg hdlr_stream_cfg;
Manish Dewangan21a850a2017-08-14 12:03:55 +05308206 bool is_direct_passthough = false;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008207 bool is_hdmi = devices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
8208 bool is_usb_dev = audio_is_usb_out_device(devices) &&
8209 (devices != AUDIO_DEVICE_OUT_USB_ACCESSORY);
8210 bool direct_dev = is_hdmi || is_usb_dev;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008211 bool use_db_as_primary =
vivek mehtaae1018c2019-05-09 12:19:57 -07008212 property_get_bool("vendor.audio.feature.deepbuffer_as_primary.enable",
8213 false);
Vignesh Kulothungana6927272019-02-20 15:17:07 -08008214 bool force_haptic_path =
8215 property_get_bool("vendor.audio.test_haptic", false);
Aniket Kumar Lata23d8cbf2019-04-10 19:54:46 -07008216 bool is_voip_rx = flags & AUDIO_OUTPUT_FLAG_VOIP_RX;
Xiaojun Sang782e5b12020-06-29 21:13:06 +08008217#ifdef AUDIO_GKI_ENABLED
8218 __s32 *generic_dec;
8219#endif
Weiyin Jiang906db3c2021-03-02 13:17:04 +08008220 pthread_mutexattr_t latch_attr;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008221
kunleizdff872d2018-08-20 14:40:33 +08008222 if (is_usb_dev && (!audio_extn_usb_connected(NULL))) {
kunleizd6a9e0c2018-07-30 15:38:52 +08008223 is_usb_dev = false;
8224 devices = AUDIO_DEVICE_OUT_SPEAKER;
8225 ALOGW("%s: ignore set device to non existing USB card, use output device(%#x)",
8226 __func__, devices);
Mingshu Pangdbd20562019-11-25 18:04:39 +08008227 if (config->format == AUDIO_FORMAT_DEFAULT)
8228 config->format = AUDIO_FORMAT_PCM_16_BIT;
8229 if (config->sample_rate == 0)
8230 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
8231 if (config->channel_mask == AUDIO_CHANNEL_NONE)
8232 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
kunleizd6a9e0c2018-07-30 15:38:52 +08008233 }
8234
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008235 *stream_out = NULL;
Naresh Tanniru80659832014-06-04 18:17:56 +05308236
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008237 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
8238
Mingming Yin3a941d42016-02-17 18:08:05 -08008239 ALOGD("%s: enter: format(%#x) sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)\
Derek Chenf6318be2017-06-12 17:16:24 -04008240 stream_handle(%p) address(%s)", __func__, config->format, config->sample_rate, config->channel_mask,
8241 devices, flags, &out->stream, address);
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05308242
8243
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08008244 if (!out) {
8245 return -ENOMEM;
8246 }
8247
Haynes Mathew George204045b2015-02-25 20:32:03 -08008248 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07008249 pthread_mutex_init(&out->pre_lock, (const pthread_mutexattr_t *) NULL);
Weiyin Jiang906db3c2021-03-02 13:17:04 +08008250 pthread_mutexattr_init(&latch_attr);
8251 pthread_mutexattr_settype(&latch_attr, PTHREAD_MUTEX_RECURSIVE);
8252 pthread_mutex_init(&out->latch_lock, &latch_attr);
8253 pthread_mutexattr_destroy(&latch_attr);
Zhou Song48453a02018-01-10 17:50:59 +08008254 pthread_mutex_init(&out->position_query_lock, (const pthread_mutexattr_t *) NULL);
Haynes Mathew George204045b2015-02-25 20:32:03 -08008255 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
8256
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008257 if (devices == AUDIO_DEVICE_NONE)
8258 devices = AUDIO_DEVICE_OUT_SPEAKER;
8259
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008260 out->flags = flags;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08008261 list_init(&out->device_list);
8262 update_device_list(&out->device_list, devices, address, true /* add devices */);
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07008263 out->dev = adev;
Aalique Grahame65780b52017-09-27 14:59:56 -07008264 out->hal_op_format = out->hal_ip_format = format = out->format = config->format;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008265 out->sample_rate = config->sample_rate;
Sachin Mohan Gadag3d09acd2017-06-19 12:43:44 +05308266 out->channel_mask = config->channel_mask;
Ramjee Singh5857aeb2017-08-03 19:18:50 +05308267 if (out->channel_mask == AUDIO_CHANNEL_NONE)
8268 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
8269 else
8270 out->supported_channel_masks[0] = out->channel_mask;
Eric Laurentc4aef752013-09-12 17:45:53 -07008271 out->handle = handle;
Mingming Yin3ee55c62014-08-04 14:23:35 -07008272 out->bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Alexy Josephaa54c872014-12-03 02:46:47 -08008273 out->non_blocking = 0;
Ashish Jain83a6cc22016-06-28 14:34:17 +05308274 out->convert_buffer = NULL;
Ashish Jain1b9b30c2017-05-18 20:57:40 +05308275 out->started = 0;
Zhou Songbaddf9f2020-11-20 13:57:39 +08008276 out->a2dp_muted = false;
Aniket Kumar Lata932f4872017-11-06 18:29:44 -08008277 out->hal_output_suspend_supported = 0;
8278 out->dynamic_pm_qos_config_supported = 0;
Surendar Karkaf51b5842018-04-26 11:28:38 +05308279 out->set_dual_mono = false;
Manisha Agarwal7b3e3772019-02-20 14:33:45 +05308280 out->prev_card_status_offline = false;
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +05308281 out->pspd_coeff_sent = false;
Phil Burkd898ba62019-06-20 12:49:01 -07008282 out->mmap_shared_memory_fd = -1; // not open
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008283
Nikhil Laturkar26b690b2017-07-25 11:06:14 +05308284 if ((flags & AUDIO_OUTPUT_FLAG_BD) &&
Satish Babu Patakokila37e7c482018-02-02 11:50:06 +05308285 (property_get_bool("vendor.audio.matrix.limiter.enable", false)))
Ben Romberger6c4d3812017-06-13 17:46:45 -07008286 platform_set_device_params(out, DEVICE_PARAM_LIMITER_ID, 1);
8287
Aalique Grahame22e49102018-12-18 14:23:57 -08008288 if (direct_dev &&
8289 (audio_is_linear_pcm(out->format) ||
8290 config->format == AUDIO_FORMAT_DEFAULT) &&
8291 out->flags == AUDIO_OUTPUT_FLAG_NONE) {
8292 audio_format_t req_format = config->format;
8293 audio_channel_mask_t req_channel_mask = config->channel_mask;
8294 uint32_t req_sample_rate = config->sample_rate;
8295
8296 pthread_mutex_lock(&adev->lock);
8297 if (is_hdmi) {
8298 ALOGV("AUDIO_DEVICE_OUT_AUX_DIGITAL and DIRECT|OFFLOAD, check hdmi caps");
8299 ret = read_hdmi_sink_caps(out);
8300 if (config->sample_rate == 0)
8301 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
8302 if (config->channel_mask == AUDIO_CHANNEL_NONE)
8303 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
8304 if (config->format == AUDIO_FORMAT_DEFAULT)
8305 config->format = AUDIO_FORMAT_PCM_16_BIT;
8306 } else if (is_usb_dev) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008307 ret = read_usb_sup_params_and_compare(true /*is_playback*/,
8308 &config->format,
8309 &out->supported_formats[0],
8310 MAX_SUPPORTED_FORMATS,
8311 &config->channel_mask,
8312 &out->supported_channel_masks[0],
8313 MAX_SUPPORTED_CHANNEL_MASKS,
8314 &config->sample_rate,
8315 &out->supported_sample_rates[0],
8316 MAX_SUPPORTED_SAMPLE_RATES);
8317 ALOGV("plugged dev USB ret %d", ret);
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008318 }
Aalique Grahame22e49102018-12-18 14:23:57 -08008319
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008320 pthread_mutex_unlock(&adev->lock);
8321 if (ret != 0) {
Mingming Yin3a941d42016-02-17 18:08:05 -08008322 if (ret == -ENOSYS) {
8323 /* ignore and go with default */
8324 ret = 0;
Aalique Grahame22e49102018-12-18 14:23:57 -08008325 }
8326 // For MMAP NO IRQ, allow conversions in ADSP
8327 else if (is_hdmi || (flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) == 0)
8328 goto error_open;
8329 else {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008330 ALOGE("error reading direct dev sink caps");
Mingming Yin3a941d42016-02-17 18:08:05 -08008331 goto error_open;
8332 }
Aalique Grahame22e49102018-12-18 14:23:57 -08008333
8334 if (req_sample_rate != 0 && config->sample_rate != req_sample_rate)
8335 config->sample_rate = req_sample_rate;
8336 if (req_channel_mask != AUDIO_CHANNEL_NONE && config->channel_mask != req_channel_mask)
8337 config->channel_mask = req_channel_mask;
8338 if (req_format != AUDIO_FORMAT_DEFAULT && config->format != req_format)
8339 config->format = req_format;
Mingming Yin3a941d42016-02-17 18:08:05 -08008340 }
Aalique Grahame22e49102018-12-18 14:23:57 -08008341
8342 out->sample_rate = config->sample_rate;
8343 out->channel_mask = config->channel_mask;
8344 out->format = config->format;
8345 if (is_hdmi) {
8346 out->usecase = USECASE_AUDIO_PLAYBACK_HIFI;
8347 out->config = pcm_config_hdmi_multi;
8348 } else if (flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) {
8349 out->usecase = USECASE_AUDIO_PLAYBACK_MMAP;
8350 out->config = pcm_config_mmap_playback;
8351 out->stream.start = out_start;
8352 out->stream.stop = out_stop;
8353 out->stream.create_mmap_buffer = out_create_mmap_buffer;
8354 out->stream.get_mmap_position = out_get_mmap_position;
8355 } else {
8356 out->usecase = USECASE_AUDIO_PLAYBACK_HIFI;
8357 out->config = pcm_config_hifi;
8358 }
8359
8360 out->config.rate = out->sample_rate;
8361 out->config.channels = audio_channel_count_from_out_mask(out->channel_mask);
8362 if (is_hdmi) {
8363 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels *
8364 audio_bytes_per_sample(out->format));
8365 }
8366 out->config.format = pcm_format_from_audio_format(out->format);
Mingming Yin3a941d42016-02-17 18:08:05 -08008367 }
8368
Derek Chenf6318be2017-06-12 17:16:24 -04008369 /* validate bus device address */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08008370 if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_BUS)) {
Derek Chenf6318be2017-06-12 17:16:24 -04008371 /* extract car audio stream index */
8372 out->car_audio_stream =
8373 audio_extn_auto_hal_get_car_audio_stream_from_address(address);
8374 if (out->car_audio_stream < 0) {
8375 ALOGE("%s: invalid car audio stream %x",
8376 __func__, out->car_audio_stream);
8377 ret = -EINVAL;
8378 goto error_open;
8379 }
Derek Chen5f67a942020-02-24 23:08:13 -08008380 ALOGV("%s: car_audio_stream %x", __func__, out->car_audio_stream);
Derek Chenf6318be2017-06-12 17:16:24 -04008381 }
8382
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008383 /* Check for VOIP usecase */
Aniket Kumar Lata23d8cbf2019-04-10 19:54:46 -07008384 if (is_voip_rx) {
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008385 if (!voice_extn_is_compress_voip_supported()) {
8386 if (out->sample_rate == 8000 || out->sample_rate == 16000 ||
8387 out->sample_rate == 32000 || out->sample_rate == 48000) {
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07008388 out->channel_mask = audio_extn_utils_is_vendor_enhanced_fwk() ?
Lakshman Chaluvarajue7fc9482020-05-30 14:29:29 +05308389 config->channel_mask : AUDIO_CHANNEL_OUT_STEREO;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008390 out->usecase = USECASE_AUDIO_PLAYBACK_VOIP;
8391 out->format = AUDIO_FORMAT_PCM_16_BIT;
Aniket Kumar Lata8426d1f2019-06-10 15:25:53 -07008392 out->volume_l = INVALID_OUT_VOLUME;
8393 out->volume_r = INVALID_OUT_VOLUME;
Vikram Panduranga93f080e2017-06-07 18:16:14 -07008394
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008395 out->config = default_pcm_config_voip_copp;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008396 out->config.rate = out->sample_rate;
Aniket Kumar Lata1e344f22019-06-19 17:09:01 -07008397 uint32_t channel_count =
8398 audio_channel_count_from_out_mask(out->channel_mask);
Ramjee Singh968858a2020-08-06 16:30:48 +05308399 out->config.channels = channel_count;
8400
Aniket Kumar Lata1e344f22019-06-19 17:09:01 -07008401 uint32_t buffer_size = get_stream_buffer_size(DEFAULT_VOIP_BUF_DURATION_MS,
8402 out->sample_rate, out->format,
8403 channel_count, false);
8404 uint32_t frame_size = audio_bytes_per_sample(out->format) * channel_count;
8405 if (frame_size != 0)
8406 out->config.period_size = buffer_size / frame_size;
8407 else
8408 ALOGW("%s: frame size is 0 for format %#x", __func__, out->format);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008409 }
8410 } else {
8411 if ((out->dev->mode == AUDIO_MODE_IN_COMMUNICATION ||
8412 voice_extn_compress_voip_is_active(out->dev)) &&
8413 (voice_extn_compress_voip_is_config_supported(config))) {
8414 ret = voice_extn_compress_voip_open_output_stream(out);
8415 if (ret != 0) {
8416 ALOGE("%s: Compress voip output cannot be opened, error:%d",
8417 __func__, ret);
8418 goto error_open;
8419 }
Sujin Panicker19027262019-09-16 18:28:06 +05308420 } else {
8421 out->usecase = GET_USECASE_AUDIO_PLAYBACK_PRIMARY(use_db_as_primary);
8422 out->config = GET_PCM_CONFIG_AUDIO_PLAYBACK_PRIMARY(use_db_as_primary);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008423 }
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08008424 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008425 } else if (audio_is_linear_pcm(out->format) &&
8426 out->flags == AUDIO_OUTPUT_FLAG_NONE && is_usb_dev) {
8427 out->channel_mask = config->channel_mask;
8428 out->sample_rate = config->sample_rate;
8429 out->format = config->format;
8430 out->usecase = USECASE_AUDIO_PLAYBACK_HIFI;
8431 // does this change?
8432 out->config = is_hdmi ? pcm_config_hdmi_multi : pcm_config_hifi;
8433 out->config.rate = config->sample_rate;
8434 out->config.channels = audio_channel_count_from_out_mask(out->channel_mask);
8435 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels *
8436 audio_bytes_per_sample(config->format));
8437 out->config.format = pcm_format_from_audio_format(out->format);
vivek mehta0ea887a2015-08-26 14:01:20 -07008438 } else if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
Dhananjay Kumarac341582017-02-23 23:42:25 +05308439 (out->flags == AUDIO_OUTPUT_FLAG_DIRECT)) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05308440 pthread_mutex_lock(&adev->lock);
8441 bool offline = (adev->card_status == CARD_STATUS_OFFLINE);
8442 pthread_mutex_unlock(&adev->lock);
8443
8444 // reject offload during card offline to allow
8445 // fallback to s/w paths
8446 if (offline) {
8447 ret = -ENODEV;
8448 goto error_open;
8449 }
vivek mehta0ea887a2015-08-26 14:01:20 -07008450
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008451 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
8452 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
8453 ALOGE("%s: Unsupported Offload information", __func__);
8454 ret = -EINVAL;
8455 goto error_open;
8456 }
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07008457
Atul Khare3fa6e542017-08-09 00:56:17 +05308458 if (config->offload_info.format == 0)
8459 config->offload_info.format = config->format;
8460 if (config->offload_info.sample_rate == 0)
8461 config->offload_info.sample_rate = config->sample_rate;
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07008462
Mingming Yin90310102013-11-13 16:57:00 -08008463 if (!is_supported_format(config->offload_info.format) &&
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05308464 !audio_extn_passthru_is_supported_format(config->offload_info.format)) {
vivek mehta0ea887a2015-08-26 14:01:20 -07008465 ALOGE("%s: Unsupported audio format %x " , __func__, config->offload_info.format);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008466 ret = -EINVAL;
8467 goto error_open;
8468 }
8469
Ben Romberger0f8c87b2017-05-24 17:41:11 -07008470 /* TrueHD only supported for 48k multiples (48k, 96k, 192k) */
8471 if ((config->offload_info.format == AUDIO_FORMAT_DOLBY_TRUEHD) &&
8472 (audio_extn_passthru_is_passthrough_stream(out)) &&
8473 !((config->sample_rate == 48000) ||
8474 (config->sample_rate == 96000) ||
8475 (config->sample_rate == 192000))) {
8476 ALOGE("%s: Unsupported sample rate %d for audio format %x",
8477 __func__, config->sample_rate, config->offload_info.format);
8478 ret = -EINVAL;
8479 goto error_open;
8480 }
8481
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008482 out->compr_config.codec = (struct snd_codec *)
8483 calloc(1, sizeof(struct snd_codec));
8484
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07008485 if (!out->compr_config.codec) {
8486 ret = -ENOMEM;
8487 goto error_open;
8488 }
8489
Dhananjay Kumarac341582017-02-23 23:42:25 +05308490 out->stream.pause = out_pause;
8491 out->stream.resume = out_resume;
8492 out->stream.flush = out_flush;
Ashish Jain4847e9d2017-08-17 19:16:57 +05308493 out->stream.set_callback = out_set_callback;
Dhananjay Kumarac341582017-02-23 23:42:25 +05308494 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Mingming Yin21d60472015-09-30 13:56:25 -07008495 out->stream.drain = out_drain;
Dhananjay Kumarac341582017-02-23 23:42:25 +05308496 out->usecase = get_offload_usecase(adev, true /* is_compress */);
vivek mehta446c3962015-09-14 10:57:35 -07008497 ALOGV("Compress Offload usecase .. usecase selected %d", out->usecase);
Dhananjay Kumarac341582017-02-23 23:42:25 +05308498 } else {
8499 out->usecase = get_offload_usecase(adev, false /* is_compress */);
8500 ALOGV("non-offload DIRECT_usecase ... usecase selected %d ", out->usecase);
vivek mehta0ea887a2015-08-26 14:01:20 -07008501 }
vivek mehta446c3962015-09-14 10:57:35 -07008502
Deeraj Somanfa377bf2019-02-06 12:57:59 +05308503 if (out->flags & AUDIO_OUTPUT_FLAG_FAST) {
8504 ALOGD("%s: Setting latency mode to true", __func__);
Meng Wang4c32fb42020-01-16 17:57:11 +08008505#ifdef AUDIO_GKI_ENABLED
8506 /* out->compr_config.codec->reserved[1] is for flags */
8507 out->compr_config.codec->reserved[1] |= audio_extn_utils_get_perf_mode_flag();
8508#else
Deeraj Somanfa377bf2019-02-06 12:57:59 +05308509 out->compr_config.codec->flags |= audio_extn_utils_get_perf_mode_flag();
Meng Wang4c32fb42020-01-16 17:57:11 +08008510#endif
Deeraj Somanfa377bf2019-02-06 12:57:59 +05308511 }
8512
vivek mehta446c3962015-09-14 10:57:35 -07008513 if (out->usecase == USECASE_INVALID) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08008514 if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_AUX_DIGITAL) &&
Mingming Yin3a941d42016-02-17 18:08:05 -08008515 config->format == 0 && config->sample_rate == 0 &&
8516 config->channel_mask == 0) {
Mingming Yin21854652016-04-13 11:54:02 -07008517 ALOGI("%s dummy open to query sink capability",__func__);
Mingming Yin3a941d42016-02-17 18:08:05 -08008518 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
8519 } else {
8520 ALOGE("%s, Max allowed OFFLOAD usecase reached ... ", __func__);
8521 ret = -EEXIST;
8522 goto error_open;
8523 }
vivek mehta446c3962015-09-14 10:57:35 -07008524 }
8525
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008526 if (config->offload_info.channel_mask)
8527 out->channel_mask = config->offload_info.channel_mask;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08008528 else if (config->channel_mask) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008529 out->channel_mask = config->channel_mask;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08008530 config->offload_info.channel_mask = config->channel_mask;
Haynes Mathew Georgea99f7532016-08-24 16:01:21 -07008531 } else {
Dhananjay Kumarac341582017-02-23 23:42:25 +05308532 ALOGE("out->channel_mask not set for OFFLOAD/DIRECT usecase");
Haynes Mathew Georgea99f7532016-08-24 16:01:21 -07008533 ret = -EINVAL;
8534 goto error_open;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08008535 }
Haynes Mathew Georgea99f7532016-08-24 16:01:21 -07008536
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07008537 format = out->format = config->offload_info.format;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008538 out->sample_rate = config->offload_info.sample_rate;
8539
Mingming Yin3ee55c62014-08-04 14:23:35 -07008540 out->bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008541
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05308542 out->compr_config.codec->id = get_snd_codec_id(config->offload_info.format);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05308543 if (audio_extn_utils_is_dolby_format(config->offload_info.format)) {
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05308544 audio_extn_dolby_send_ddp_endp_params(adev);
8545 audio_extn_dolby_set_dmid(adev);
8546 }
vivek mehta0ea887a2015-08-26 14:01:20 -07008547
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008548 out->compr_config.codec->sample_rate =
Ravi Kumar Alamandab91bff32014-11-14 12:05:54 -08008549 config->offload_info.sample_rate;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008550 out->compr_config.codec->bit_rate =
8551 config->offload_info.bit_rate;
8552 out->compr_config.codec->ch_in =
Dhanalakshmi Siddania15c6792016-08-10 15:33:53 +05308553 audio_channel_count_from_out_mask(out->channel_mask);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008554 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
Satish Babu Patakokilaa395a9e2016-11-01 12:18:49 +05308555 /* Update bit width only for non passthrough usecases.
8556 * For passthrough usecases, the output will always be opened @16 bit
8557 */
8558 if (!audio_extn_passthru_is_passthrough_stream(out))
8559 out->bit_width = AUDIO_OUTPUT_BIT_WIDTH;
Naresh Tanniruee3499a2017-01-05 14:05:35 +05308560
8561 if (out->flags & AUDIO_OUTPUT_FLAG_TIMESTAMP)
Meng Wang4c32fb42020-01-16 17:57:11 +08008562#ifdef AUDIO_GKI_ENABLED
8563 /* out->compr_config.codec->reserved[1] is for flags */
8564 out->compr_config.codec->reserved[1] |= COMPRESSED_TIMESTAMP_FLAG;
8565 ALOGVV("%s : out->compr_config.codec->flags -> (%#x) ", __func__, out->compr_config.codec->reserved[1]);
8566#else
Naresh Tanniruee3499a2017-01-05 14:05:35 +05308567 out->compr_config.codec->flags |= COMPRESSED_TIMESTAMP_FLAG;
8568 ALOGVV("%s : out->compr_config.codec->flags -> (%#x) ", __func__, out->compr_config.codec->flags);
Meng Wang4c32fb42020-01-16 17:57:11 +08008569#endif
Naresh Tanniruee3499a2017-01-05 14:05:35 +05308570
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07008571 /*TODO: Do we need to change it for passthrough */
8572 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_RAW;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008573
Manish Dewangana6fc5442015-08-24 20:30:31 +05308574 if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC)
8575 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_RAW;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05308576 else if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS)
Manish Dewangana6fc5442015-08-24 20:30:31 +05308577 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_MP4ADTS;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05308578 else if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_LATM)
8579 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_MP4LATM;
Ashish Jainf1eaa582016-05-23 20:54:24 +05308580
8581 if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) ==
8582 AUDIO_FORMAT_PCM) {
8583
8584 /*Based on platform support, configure appropriate alsa format for corresponding
8585 *hal input format.
8586 */
8587 out->compr_config.codec->format = hal_format_to_alsa(
8588 config->offload_info.format);
8589
Ashish Jain83a6cc22016-06-28 14:34:17 +05308590 out->hal_op_format = alsa_format_to_hal(
Ashish Jainf1eaa582016-05-23 20:54:24 +05308591 out->compr_config.codec->format);
Ashish Jain83a6cc22016-06-28 14:34:17 +05308592 out->hal_ip_format = out->format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05308593
Dhananjay Kumarac341582017-02-23 23:42:25 +05308594 /*for direct non-compress playback populate bit_width based on selected alsa format as
Ashish Jainf1eaa582016-05-23 20:54:24 +05308595 *hal input format and alsa format might differ based on platform support.
8596 */
8597 out->bit_width = audio_bytes_per_sample(
Ashish Jain83a6cc22016-06-28 14:34:17 +05308598 out->hal_op_format) << 3;
Ashish Jainf1eaa582016-05-23 20:54:24 +05308599
8600 out->compr_config.fragments = DIRECT_PCM_NUM_FRAGMENTS;
8601
Deeraj Soman93155a62019-09-30 19:00:37 +05308602 if (property_get_bool("vendor.audio.offload.buffer.duration.enabled", false)) {
8603 if ((config->offload_info.duration_us >= MIN_OFFLOAD_BUFFER_DURATION_MS * 1000) &&
8604 (config->offload_info.duration_us <= MAX_OFFLOAD_BUFFER_DURATION_MS * 1000))
8605 out->info.duration_us = (int64_t)config->offload_info.duration_us;
8606 }
Deeraj Soman65358ab2019-02-07 15:40:49 +05308607
Ashish Jainf1eaa582016-05-23 20:54:24 +05308608 /* Check if alsa session is configured with the same format as HAL input format,
8609 * if not then derive correct fragment size needed to accomodate the
8610 * conversion of HAL input format to alsa format.
8611 */
8612 audio_extn_utils_update_direct_pcm_fragment_size(out);
8613
8614 /*if hal input and output fragment size is different this indicates HAL input format is
8615 *not same as the alsa format
8616 */
Ashish Jain83a6cc22016-06-28 14:34:17 +05308617 if (out->hal_fragment_size != out->compr_config.fragment_size) {
Ashish Jainf1eaa582016-05-23 20:54:24 +05308618 /*Allocate a buffer to convert input data to the alsa configured format.
8619 *size of convert buffer is equal to the size required to hold one fragment size
8620 *worth of pcm data, this is because flinger does not write more than fragment_size
8621 */
Ashish Jain83a6cc22016-06-28 14:34:17 +05308622 out->convert_buffer = calloc(1,out->compr_config.fragment_size);
8623 if (out->convert_buffer == NULL){
Ashish Jainf1eaa582016-05-23 20:54:24 +05308624 ALOGE("Allocation failed for convert buffer for size %d", out->compr_config.fragment_size);
8625 ret = -ENOMEM;
8626 goto error_open;
8627 }
8628 }
8629 } else if (audio_extn_passthru_is_passthrough_stream(out)) {
8630 out->compr_config.fragment_size =
8631 audio_extn_passthru_get_buffer_size(&config->offload_info);
8632 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
8633 } else {
8634 out->compr_config.fragment_size =
8635 platform_get_compress_offload_buffer_size(&config->offload_info);
8636 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
8637 }
Mingming Yin3ee55c62014-08-04 14:23:35 -07008638
Naresh Tanniruee3499a2017-01-05 14:05:35 +05308639 if (out->flags & AUDIO_OUTPUT_FLAG_TIMESTAMP) {
8640 out->compr_config.fragment_size += sizeof(struct snd_codec_metadata);
8641 }
Xiaojun Sang782e5b12020-06-29 21:13:06 +08008642 if (config->offload_info.format == AUDIO_FORMAT_FLAC) {
8643#ifdef AUDIO_GKI_ENABLED
8644 generic_dec =
8645 &(out->compr_config.codec->options.generic.reserved[1]);
8646 ((struct snd_generic_dec_flac *)generic_dec)->sample_size =
8647 AUDIO_OUTPUT_BIT_WIDTH;
8648#else
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +05308649 out->compr_config.codec->options.flac_dec.sample_size = AUDIO_OUTPUT_BIT_WIDTH;
Xiaojun Sang782e5b12020-06-29 21:13:06 +08008650#endif
8651 }
Mingming Yin3ee55c62014-08-04 14:23:35 -07008652
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05308653 if (config->offload_info.format == AUDIO_FORMAT_APTX) {
8654 audio_extn_send_aptx_dec_bt_addr_to_dsp(out);
8655 }
8656
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008657 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
8658 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07008659
Manish Dewangan69426c82017-01-30 17:35:36 +05308660 if ((flags & AUDIO_OUTPUT_FLAG_TIMESTAMP) &&
8661 (flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC)) {
8662 out->render_mode = RENDER_MODE_AUDIO_STC_MASTER;
8663 } else if(flags & AUDIO_OUTPUT_FLAG_TIMESTAMP) {
8664 out->render_mode = RENDER_MODE_AUDIO_MASTER;
8665 } else {
8666 out->render_mode = RENDER_MODE_AUDIO_NO_TIMESTAMP;
8667 }
Alexy Josephaa54c872014-12-03 02:46:47 -08008668
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05308669 memset(&out->channel_map_param, 0,
8670 sizeof(struct audio_out_channel_map_param));
8671
Haynes Mathew George352f27b2013-07-26 00:00:15 -07008672 out->send_new_metadata = 1;
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05308673 out->send_next_track_params = false;
8674 out->is_compr_metadata_avail = false;
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08008675 out->offload_state = OFFLOAD_STATE_IDLE;
8676 out->playback_started = 0;
Zhou Song48453a02018-01-10 17:50:59 +08008677 out->writeAt.tv_sec = 0;
8678 out->writeAt.tv_nsec = 0;
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08008679
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08008680 audio_extn_dts_create_state_notifier_node(out->usecase);
8681
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008682 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
8683 __func__, config->offload_info.version,
8684 config->offload_info.bit_rate);
Ashish Jain5106d362016-05-11 19:23:33 +05308685
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05308686 /* Check if DSD audio format is supported in codec
8687 * and there is no active native DSD use case
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05308688 */
8689
8690 if ((config->format == AUDIO_FORMAT_DSD) &&
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05308691 (!platform_check_codec_dsd_support(adev->platform) ||
8692 audio_is_dsd_native_stream_active(adev))) {
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05308693 ret = -EINVAL;
8694 goto error_open;
8695 }
8696
Ashish Jain5106d362016-05-11 19:23:33 +05308697 /* Disable gapless if any of the following is true
8698 * passthrough playback
8699 * AV playback
Dhananjay Kumarac341582017-02-23 23:42:25 +05308700 * non compressed Direct playback
Ashish Jain5106d362016-05-11 19:23:33 +05308701 */
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05308702 if (audio_extn_passthru_is_passthrough_stream(out) ||
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05308703 (config->format == AUDIO_FORMAT_DSD) ||
Naresh Tanniru928f0862017-04-07 16:44:23 -07008704 (config->format == AUDIO_FORMAT_IEC61937) ||
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05308705 config->offload_info.has_video ||
Dhananjay Kumarac341582017-02-23 23:42:25 +05308706 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Ashish Jain5106d362016-05-11 19:23:33 +05308707 check_and_set_gapless_mode(adev, false);
8708 } else
8709 check_and_set_gapless_mode(adev, true);
Mingming Yin21854652016-04-13 11:54:02 -07008710
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05308711 if (audio_extn_passthru_is_passthrough_stream(out)) {
Mingming Yin21854652016-04-13 11:54:02 -07008712 out->flags |= AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH;
8713 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05308714 if (config->format == AUDIO_FORMAT_DSD) {
8715 out->flags |= AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH;
Meng Wang4c32fb42020-01-16 17:57:11 +08008716#ifdef AUDIO_GKI_ENABLED
8717 /* out->compr_config.codec->reserved[0] is for compr_passthr */
8718 out->compr_config.codec->reserved[0] = PASSTHROUGH_DSD;
8719#else
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05308720 out->compr_config.codec->compr_passthr = PASSTHROUGH_DSD;
Meng Wang4c32fb42020-01-16 17:57:11 +08008721#endif
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05308722 }
Aalique Grahame0359a1f2016-09-08 16:54:22 -07008723
8724 create_offload_callback_thread(out);
8725
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07008726 } else if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
Arun Mirpuri7da752a2018-09-11 18:01:15 -07008727 switch (config->sample_rate) {
8728 case 0:
8729 out->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
8730 break;
8731 case 8000:
8732 case 16000:
8733 case 48000:
8734 out->sample_rate = config->sample_rate;
8735 break;
8736 default:
8737 ALOGE("%s: Unsupported sampling rate %d for Incall Music", __func__,
8738 config->sample_rate);
8739 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
8740 ret = -EINVAL;
8741 goto error_open;
8742 }
8743 //FIXME: add support for MONO stream configuration when audioflinger mixer supports it
8744 switch (config->channel_mask) {
8745 case AUDIO_CHANNEL_NONE:
8746 case AUDIO_CHANNEL_OUT_STEREO:
8747 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
8748 break;
8749 default:
8750 ALOGE("%s: Unsupported channel mask %#x for Incall Music", __func__,
8751 config->channel_mask);
8752 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
8753 ret = -EINVAL;
8754 goto error_open;
8755 }
8756 switch (config->format) {
8757 case AUDIO_FORMAT_DEFAULT:
8758 case AUDIO_FORMAT_PCM_16_BIT:
8759 out->format = AUDIO_FORMAT_PCM_16_BIT;
8760 break;
8761 default:
8762 ALOGE("%s: Unsupported format %#x for Incall Music", __func__,
8763 config->format);
8764 config->format = AUDIO_FORMAT_PCM_16_BIT;
8765 ret = -EINVAL;
8766 goto error_open;
8767 }
8768
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05308769 ret = voice_extn_check_and_set_incall_music_usecase(adev, out);
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07008770 if (ret != 0) {
8771 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
Arun Mirpuri7da752a2018-09-11 18:01:15 -07008772 __func__, ret);
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07008773 goto error_open;
8774 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08008775 } else if (is_single_device_type_equal(&out->device_list,
8776 AUDIO_DEVICE_OUT_TELEPHONY_TX)) {
Aalique Grahame22e49102018-12-18 14:23:57 -08008777 switch (config->sample_rate) {
8778 case 0:
8779 out->sample_rate = AFE_PROXY_SAMPLING_RATE;
8780 break;
8781 case 8000:
8782 case 16000:
8783 case 48000:
8784 out->sample_rate = config->sample_rate;
8785 break;
8786 default:
8787 ALOGE("%s: Unsupported sampling rate %d for Telephony TX", __func__,
8788 config->sample_rate);
8789 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
8790 ret = -EINVAL;
8791 break;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07008792 }
Aalique Grahame22e49102018-12-18 14:23:57 -08008793 //FIXME: add support for MONO stream configuration when audioflinger mixer supports it
8794 switch (config->channel_mask) {
8795 case AUDIO_CHANNEL_NONE:
8796 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
8797 break;
8798 case AUDIO_CHANNEL_OUT_STEREO:
8799 out->channel_mask = config->channel_mask;
8800 break;
8801 default:
8802 ALOGE("%s: Unsupported channel mask %#x for Telephony TX", __func__,
8803 config->channel_mask);
8804 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
8805 ret = -EINVAL;
8806 break;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07008807 }
Aalique Grahame22e49102018-12-18 14:23:57 -08008808 switch (config->format) {
8809 case AUDIO_FORMAT_DEFAULT:
8810 out->format = AUDIO_FORMAT_PCM_16_BIT;
8811 break;
8812 case AUDIO_FORMAT_PCM_16_BIT:
8813 out->format = config->format;
8814 break;
8815 default:
8816 ALOGE("%s: Unsupported format %#x for Telephony TX", __func__,
8817 config->format);
8818 config->format = AUDIO_FORMAT_PCM_16_BIT;
8819 ret = -EINVAL;
8820 break;
8821 }
8822 if (ret != 0)
8823 goto error_open;
8824
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07008825 out->usecase = USECASE_AUDIO_PLAYBACK_AFE_PROXY;
8826 out->config = pcm_config_afe_proxy_playback;
Aalique Grahame22e49102018-12-18 14:23:57 -08008827 out->config.rate = out->sample_rate;
8828 out->config.channels =
8829 audio_channel_count_from_out_mask(out->channel_mask);
8830 out->config.format = pcm_format_from_audio_format(out->format);
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07008831 adev->voice_tx_output = out;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07008832 } else {
Ashish Jain058165c2016-09-28 23:18:48 +05308833 unsigned int channels = 0;
8834 /*Update config params to default if not set by the caller*/
8835 if (config->sample_rate == 0)
8836 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
8837 if (config->channel_mask == AUDIO_CHANNEL_NONE)
8838 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
8839 if (config->format == AUDIO_FORMAT_DEFAULT)
8840 config->format = AUDIO_FORMAT_PCM_16_BIT;
8841
8842 channels = audio_channel_count_from_out_mask(out->channel_mask);
8843
Varun Balaraje49253e2017-07-06 19:48:56 +05308844 if (out->flags & AUDIO_OUTPUT_FLAG_INTERACTIVE) {
8845 out->usecase = get_interactive_usecase(adev);
8846 out->config = pcm_config_low_latency;
8847 } else if (out->flags & AUDIO_OUTPUT_FLAG_RAW) {
Ashish Jain83a6cc22016-06-28 14:34:17 +05308848 out->usecase = USECASE_AUDIO_PLAYBACK_ULL;
Haynes Mathew George5beddd42016-06-27 18:33:40 -07008849 out->realtime = may_use_noirq_mode(adev, USECASE_AUDIO_PLAYBACK_ULL,
8850 out->flags);
8851 out->config = out->realtime ? pcm_config_rt : pcm_config_low_latency;
Haynes Mathew George16081042017-05-31 17:16:49 -07008852 } else if (out->flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) {
8853 out->usecase = USECASE_AUDIO_PLAYBACK_MMAP;
8854 out->config = pcm_config_mmap_playback;
8855 out->stream.start = out_start;
8856 out->stream.stop = out_stop;
8857 out->stream.create_mmap_buffer = out_create_mmap_buffer;
8858 out->stream.get_mmap_position = out_get_mmap_position;
Ashish Jain83a6cc22016-06-28 14:34:17 +05308859 } else if (out->flags & AUDIO_OUTPUT_FLAG_FAST) {
8860 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
Aniket Kumar Lata932f4872017-11-06 18:29:44 -08008861 out->hal_output_suspend_supported =
8862 property_get_bool("vendor.audio.hal.output.suspend.supported", false);
8863 out->dynamic_pm_qos_config_supported =
8864 property_get_bool("vendor.audio.hal.dynamic.qos.config.supported", false);
8865 if (!out->dynamic_pm_qos_config_supported) {
Alexy Joseph98988832017-01-13 14:56:59 -08008866 ALOGI("%s: dynamic qos voting not enabled for platform", __func__);
8867 } else {
8868 ALOGI("%s: dynamic qos voting enabled for platform", __func__);
8869 //the mixer path will be a string similar to "low-latency-playback resume"
8870 strlcpy(out->pm_qos_mixer_path, use_case_table[out->usecase], MAX_MIXER_PATH_LEN);
8871 strlcat(out->pm_qos_mixer_path,
8872 " resume", MAX_MIXER_PATH_LEN);
8873 ALOGI("%s: created %s pm_qos_mixer_path" , __func__,
8874 out->pm_qos_mixer_path);
8875 }
Ashish Jain83a6cc22016-06-28 14:34:17 +05308876 out->config = pcm_config_low_latency;
8877 } else if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
8878 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
8879 out->config = pcm_config_deep_buffer;
Ashish Jain058165c2016-09-28 23:18:48 +05308880 out->config.period_size = get_output_period_size(config->sample_rate, out->format,
8881 channels, DEEP_BUFFER_OUTPUT_PERIOD_DURATION);
8882 if (out->config.period_size <= 0) {
8883 ALOGE("Invalid configuration period size is not valid");
8884 ret = -EINVAL;
8885 goto error_open;
8886 }
Aalique Grahame22e49102018-12-18 14:23:57 -08008887 } else if (flags & AUDIO_OUTPUT_FLAG_TTS) {
8888 out->usecase = USECASE_AUDIO_PLAYBACK_TTS;
8889 out->config = pcm_config_deep_buffer;
Vignesh Kulothungana6927272019-02-20 15:17:07 -08008890 } else if (config->channel_mask & AUDIO_CHANNEL_HAPTIC_ALL) {
8891 out->usecase = USECASE_AUDIO_PLAYBACK_WITH_HAPTICS;
8892 out->config = pcm_config_haptics_audio;
8893 if (force_haptic_path)
8894 adev->haptics_config = pcm_config_haptics_audio;
8895 else
8896 adev->haptics_config = pcm_config_haptics;
8897
Meng Wangd08ce322020-04-02 08:59:20 +08008898 channels =
Vignesh Kulothungana6927272019-02-20 15:17:07 -08008899 audio_channel_count_from_out_mask(out->channel_mask & ~AUDIO_CHANNEL_HAPTIC_ALL);
8900
8901 if (force_haptic_path) {
8902 out->config.channels = 1;
8903 adev->haptics_config.channels = 1;
8904 } else
8905 adev->haptics_config.channels = audio_channel_count_from_out_mask(out->channel_mask & AUDIO_CHANNEL_HAPTIC_ALL);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08008906 } else if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_BUS)) {
Derek Chenf6318be2017-06-12 17:16:24 -04008907 ret = audio_extn_auto_hal_open_output_stream(out);
8908 if (ret) {
8909 ALOGE("%s: Failed to open output stream for bus device", __func__);
8910 ret = -EINVAL;
8911 goto error_open;
8912 }
Ashish Jain83a6cc22016-06-28 14:34:17 +05308913 } else {
8914 /* primary path is the default path selected if no other outputs are available/suitable */
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008915 out->usecase = GET_USECASE_AUDIO_PLAYBACK_PRIMARY(use_db_as_primary);
8916 out->config = GET_PCM_CONFIG_AUDIO_PLAYBACK_PRIMARY(use_db_as_primary);
Ashish Jain83a6cc22016-06-28 14:34:17 +05308917 }
8918 out->hal_ip_format = format = out->format;
8919 out->config.format = hal_format_to_pcm(out->hal_ip_format);
8920 out->hal_op_format = pcm_format_to_hal(out->config.format);
8921 out->bit_width = format_to_bitwidth_table[out->hal_op_format] << 3;
8922 out->config.rate = config->sample_rate;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07008923 out->sample_rate = out->config.rate;
Ashish Jain058165c2016-09-28 23:18:48 +05308924 out->config.channels = channels;
Ashish Jain83a6cc22016-06-28 14:34:17 +05308925 if (out->hal_ip_format != out->hal_op_format) {
8926 uint32_t buffer_size = out->config.period_size *
8927 format_to_bitwidth_table[out->hal_op_format] *
8928 out->config.channels;
8929 out->convert_buffer = calloc(1, buffer_size);
8930 if (out->convert_buffer == NULL){
8931 ALOGE("Allocation failed for convert buffer for size %d",
8932 out->compr_config.fragment_size);
8933 ret = -ENOMEM;
8934 goto error_open;
8935 }
8936 ALOGD("Convert buffer allocated of size %d", buffer_size);
8937 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008938 }
8939
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08008940 ALOGV("%s devices:%d, format:%x, out->sample_rate:%d,out->bit_width:%d out->format:%d out->flags:%x, flags: %x usecase %d",
8941 __func__, devices, format, out->sample_rate, out->bit_width, out->format, out->flags, flags, out->usecase);
Ashish Jain83a6cc22016-06-28 14:34:17 +05308942
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07008943 /* TODO remove this hardcoding and check why width is zero*/
8944 if (out->bit_width == 0)
8945 out->bit_width = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05308946 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07008947 &adev->streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08008948 &out->device_list, out->flags,
8949 out->hal_op_format, out->sample_rate,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +05308950 out->bit_width, out->channel_mask, out->profile,
Manish Dewangan837dc462015-05-27 10:17:41 +05308951 &out->app_type_cfg);
Kogara Naveen Kumar65828fe2022-10-14 16:41:04 +05308952 if (((out->usecase == (audio_usecase_t)(GET_USECASE_AUDIO_PLAYBACK_PRIMARY(use_db_as_primary))) ||
8953 (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) && (!compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_BUS))) {
Haynes Mathew Georgebf143712013-12-03 13:02:53 -08008954 /* Ensure the default output is not selected twice */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08008955 if(adev->primary_output == NULL)
8956 adev->primary_output = out;
8957 else {
8958 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07008959 ret = -EEXIST;
8960 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08008961 }
8962 }
8963
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008964 /* Check if this usecase is already existing */
8965 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella7ce05352014-04-17 20:00:41 -07008966 if ((get_usecase_from_list(adev, out->usecase) != NULL) &&
8967 (out->usecase != USECASE_COMPRESS_VOIP_CALL)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008968 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008969 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07008970 ret = -EEXIST;
8971 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008972 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08008973
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008974 pthread_mutex_unlock(&adev->lock);
8975
8976 out->stream.common.get_sample_rate = out_get_sample_rate;
8977 out->stream.common.set_sample_rate = out_set_sample_rate;
8978 out->stream.common.get_buffer_size = out_get_buffer_size;
8979 out->stream.common.get_channels = out_get_channels;
8980 out->stream.common.get_format = out_get_format;
8981 out->stream.common.set_format = out_set_format;
8982 out->stream.common.standby = out_standby;
8983 out->stream.common.dump = out_dump;
8984 out->stream.common.set_parameters = out_set_parameters;
8985 out->stream.common.get_parameters = out_get_parameters;
8986 out->stream.common.add_audio_effect = out_add_audio_effect;
8987 out->stream.common.remove_audio_effect = out_remove_audio_effect;
8988 out->stream.get_latency = out_get_latency;
8989 out->stream.set_volume = out_set_volume;
Aalique Grahame22e49102018-12-18 14:23:57 -08008990#ifdef NO_AUDIO_OUT
8991 out->stream.write = out_write_for_no_output;
8992#else
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008993 out->stream.write = out_write;
Aalique Grahame22e49102018-12-18 14:23:57 -08008994#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008995 out->stream.get_render_position = out_get_render_position;
8996 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07008997 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008998
Haynes Mathew George16081042017-05-31 17:16:49 -07008999 if (out->realtime)
9000 out->af_period_multiplier = af_period_multiplier;
9001 else
9002 out->af_period_multiplier = 1;
9003
Andy Hunga1f48fa2019-07-01 18:14:53 -07009004 out->kernel_buffer_size = out->config.period_size * out->config.period_count;
9005
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009006 out->standby = 1;
Zhou Songbaddf9f2020-11-20 13:57:39 +08009007 out->volume_l = PLAYBACK_GAIN_MAX;
9008 out->volume_r = PLAYBACK_GAIN_MAX;
Eric Laurenta9024de2013-04-04 09:19:12 -07009009 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07009010 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009011
9012 config->format = out->stream.common.get_format(&out->stream.common);
9013 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
9014 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
Naresh Tanniru04f71882018-06-26 17:46:22 +05309015 register_format(out->format, out->supported_formats);
9016 register_channel_mask(out->channel_mask, out->supported_channel_masks);
9017 register_sample_rate(out->sample_rate, out->supported_sample_rates);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009018
Dechen Chai22768452021-07-30 09:29:16 +05309019#ifndef LINUX_ENABLED
Aalique Grahame22e49102018-12-18 14:23:57 -08009020 out->error_log = error_log_create(
9021 ERROR_LOG_ENTRIES,
9022 1000000000 /* aggregate consecutive identical errors within one second in ns */);
Dechen Chai22768452021-07-30 09:29:16 +05309023#endif
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05309024 /*
9025 By locking output stream before registering, we allow the callback
9026 to update stream's state only after stream's initial state is set to
9027 adev state.
9028 */
9029 lock_output_stream(out);
9030 audio_extn_snd_mon_register_listener(out, out_snd_mon_cb);
9031 pthread_mutex_lock(&adev->lock);
9032 out->card_status = adev->card_status;
9033 pthread_mutex_unlock(&adev->lock);
9034 pthread_mutex_unlock(&out->lock);
9035
Aalique Grahame22e49102018-12-18 14:23:57 -08009036 stream_app_type_cfg_init(&out->app_type_cfg);
9037
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009038 *stream_out = &out->stream;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05309039 ALOGD("%s: Stream (%p) picks up usecase (%s)", __func__, &out->stream,
vivek mehta0ea887a2015-08-26 14:01:20 -07009040 use_case_table[out->usecase]);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08009041
9042 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
9043 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
9044 popcount(out->channel_mask), out->playback_started);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08009045 /* setup a channel for client <--> adsp communication for stream events */
Manish Dewangan21a850a2017-08-14 12:03:55 +05309046 is_direct_passthough = audio_extn_passthru_is_direct_passthrough(out);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08009047 if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
Naresh Tanniru85819452017-05-04 18:55:45 -07009048 (out->flags & AUDIO_OUTPUT_FLAG_DIRECT_PCM) ||
Gangadhar Sb0210342019-02-22 17:39:41 +05309049 audio_extn_ip_hdlr_intf_supported_for_copp(adev->platform) ||
9050 (audio_extn_ip_hdlr_intf_supported(config->format, is_direct_passthough, false))) {
Ben Rombergerd771a7c2017-02-22 18:05:17 -08009051 hdlr_stream_cfg.pcm_device_id = platform_get_pcm_device_id(
9052 out->usecase, PCM_PLAYBACK);
9053 hdlr_stream_cfg.flags = out->flags;
9054 hdlr_stream_cfg.type = PCM_PLAYBACK;
9055 ret = audio_extn_adsp_hdlr_stream_open(&out->adsp_hdlr_stream_handle,
9056 &hdlr_stream_cfg);
9057 if (ret) {
9058 ALOGE("%s: adsp_hdlr_stream_open failed %d",__func__, ret);
9059 out->adsp_hdlr_stream_handle = NULL;
9060 }
9061 }
Gangadhar Sb0210342019-02-22 17:39:41 +05309062 ip_hdlr_stream = audio_extn_ip_hdlr_intf_supported(config->format,
9063 is_direct_passthough, false);
9064 ip_hdlr_dev = audio_extn_ip_hdlr_intf_supported_for_copp(adev->platform);
9065 if (ip_hdlr_stream || ip_hdlr_dev ) {
Vidyakumar Athota2062f912017-06-27 14:46:15 -07009066 ret = audio_extn_ip_hdlr_intf_init(&out->ip_hdlr_handle, NULL, NULL, adev, out->usecase);
Naresh Tanniru85819452017-05-04 18:55:45 -07009067 if (ret < 0) {
9068 ALOGE("%s: audio_extn_ip_hdlr_intf_init failed %d",__func__, ret);
9069 out->ip_hdlr_handle = NULL;
9070 }
9071 }
Derek Chenf939fb72018-11-13 13:34:41 -08009072
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07009073 ret = io_streams_map_insert(adev, &out->stream.common,
9074 out->handle, AUDIO_PATCH_HANDLE_NONE);
9075 if (ret != 0)
9076 goto error_open;
9077
Susan Wang6dd13092021-01-25 10:27:11 -05009078 out->out_ctxt.output = out;
Derek Chenf939fb72018-11-13 13:34:41 -08009079
9080 pthread_mutex_lock(&adev->lock);
Susan Wang6dd13092021-01-25 10:27:11 -05009081 list_add_tail(&adev->active_outputs_list, &out->out_ctxt.list);
Derek Chenf939fb72018-11-13 13:34:41 -08009082 pthread_mutex_unlock(&adev->lock);
9083
Eric Laurent994a6932013-07-17 11:51:42 -07009084 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009085 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07009086
9087error_open:
Ashish Jain83a6cc22016-06-28 14:34:17 +05309088 if (out->convert_buffer)
9089 free(out->convert_buffer);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07009090 free(out);
9091 *stream_out = NULL;
9092 ALOGD("%s: exit: ret %d", __func__, ret);
9093 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009094}
9095
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05309096void adev_close_output_stream(struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009097 struct audio_stream_out *stream)
9098{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07009099 struct stream_out *out = (struct stream_out *)stream;
9100 struct audio_device *adev = out->dev;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08009101 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07009102
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009103 ALOGD("%s: enter:stream_handle(%s)",__func__, use_case_table[out->usecase]);
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05309104
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07009105 io_streams_map_remove(adev, out->handle);
9106
Susan Wang6dd13092021-01-25 10:27:11 -05009107 // remove out_ctxt early to prevent the stream
9108 // being opened in a race condition
9109 pthread_mutex_lock(&adev->lock);
9110 list_remove(&out->out_ctxt.list);
9111 pthread_mutex_unlock(&adev->lock);
9112
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05309113 // must deregister from sndmonitor first to prevent races
9114 // between the callback and close_stream
9115 audio_extn_snd_mon_unregister_listener(out);
9116
Ben Rombergerd771a7c2017-02-22 18:05:17 -08009117 /* close adsp hdrl session before standby */
9118 if (out->adsp_hdlr_stream_handle) {
9119 ret = audio_extn_adsp_hdlr_stream_close(out->adsp_hdlr_stream_handle);
9120 if (ret)
9121 ALOGE("%s: adsp_hdlr_stream_close failed %d",__func__, ret);
9122 out->adsp_hdlr_stream_handle = NULL;
9123 }
9124
Manish Dewangan21a850a2017-08-14 12:03:55 +05309125 if (out->ip_hdlr_handle) {
Naresh Tanniru85819452017-05-04 18:55:45 -07009126 audio_extn_ip_hdlr_intf_deinit(out->ip_hdlr_handle);
9127 out->ip_hdlr_handle = NULL;
9128 }
9129
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08009130 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05309131 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08009132 ret = voice_extn_compress_voip_close_output_stream(&stream->common);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05309133 out->started = 0;
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05309134 pthread_mutex_unlock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08009135 if(ret != 0)
9136 ALOGE("%s: Compress voip output cannot be closed, error:%d",
9137 __func__, ret);
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07009138 } else
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08009139 out_standby(&stream->common);
9140
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07009141 if (is_offload_usecase(out->usecase)) {
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08009142 audio_extn_dts_remove_state_notifier_node(out->usecase);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07009143 destroy_offload_callback_thread(out);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07009144 free_offload_usecase(adev, out->usecase);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07009145 if (out->compr_config.codec != NULL)
9146 free(out->compr_config.codec);
9147 }
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07009148
Zhou Songbaddf9f2020-11-20 13:57:39 +08009149 out->a2dp_muted = false;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05309150
Varun Balaraje49253e2017-07-06 19:48:56 +05309151 if (is_interactive_usecase(out->usecase))
9152 free_interactive_usecase(adev, out->usecase);
9153
Ashish Jain83a6cc22016-06-28 14:34:17 +05309154 if (out->convert_buffer != NULL) {
9155 free(out->convert_buffer);
9156 out->convert_buffer = NULL;
9157 }
9158
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07009159 if (adev->voice_tx_output == out)
9160 adev->voice_tx_output = NULL;
9161
Dechen Chai22768452021-07-30 09:29:16 +05309162#ifndef LINUX_ENABLED
Aalique Grahame22e49102018-12-18 14:23:57 -08009163 error_log_destroy(out->error_log);
9164 out->error_log = NULL;
Dechen Chai22768452021-07-30 09:29:16 +05309165#endif
Dhanalakshmi Siddani6c3d0992017-01-16 16:52:33 +05309166 if (adev->primary_output == out)
9167 adev->primary_output = NULL;
9168
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07009169 pthread_cond_destroy(&out->cond);
9170 pthread_mutex_destroy(&out->lock);
Weiyin Jiang280ea742020-09-08 20:28:22 +08009171 pthread_mutex_destroy(&out->pre_lock);
9172 pthread_mutex_destroy(&out->latch_lock);
9173 pthread_mutex_destroy(&out->position_query_lock);
Derek Chenf939fb72018-11-13 13:34:41 -08009174
9175 pthread_mutex_lock(&adev->lock);
Zhenlin Lian4f947842022-05-14 15:50:52 +05309176 clear_devices(&out->device_list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009177 free(stream);
Derek Chenf939fb72018-11-13 13:34:41 -08009178 pthread_mutex_unlock(&adev->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07009179 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009180}
9181
Shubhasini Sugumaran3fde6582022-01-19 15:54:50 -05009182void in_set_power_policy(uint8_t enable)
9183{
9184 struct listnode *node;
9185
9186 ALOGD("%s: Enter, state %d", __func__, enable);
9187
9188 pthread_mutex_lock(&adev->lock);
9189 adev->in_power_policy = enable ? POWER_POLICY_STATUS_ONLINE : POWER_POLICY_STATUS_OFFLINE;
9190 pthread_mutex_unlock(&adev->lock);
9191
9192 if (!enable) {
9193 list_for_each(node, &adev->active_inputs_list) {
9194 streams_input_ctxt_t *in_ctxt = node_to_item(node,
9195 streams_input_ctxt_t,
9196 list);
9197 struct stream_in *in = in_ctxt->input;
9198 in_standby(&in->stream.common);
9199 }
9200 }
9201
9202 ALOGD("%s: Exit", __func__);
9203}
9204
9205void out_set_power_policy(uint8_t enable)
9206{
9207 struct listnode *node;
9208
9209 ALOGD("%s: Enter, state %d", __func__, enable);
9210
9211 pthread_mutex_lock(&adev->lock);
E V Ravi317be872022-02-23 19:08:15 +05309212 adev->out_power_policy = enable ? POWER_POLICY_STATUS_ONLINE : POWER_POLICY_STATUS_OFFLINE;
Shubhasini Sugumaran3fde6582022-01-19 15:54:50 -05009213 pthread_mutex_unlock(&adev->lock);
9214
9215 if (!enable) {
9216 list_for_each(node, &adev->active_outputs_list) {
9217 streams_output_ctxt_t *out_ctxt = node_to_item(node,
9218 streams_output_ctxt_t,
9219 list);
9220 struct stream_out *out = out_ctxt->output;
9221 out_on_error(&out->stream.common);
9222 }
9223 }
9224
9225 ALOGD("%s: Exit", __func__);
9226}
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009227static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
9228{
9229 struct audio_device *adev = (struct audio_device *)dev;
9230 struct str_parms *parms;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009231 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07009232 int val;
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07009233 int ret;
9234 int status = 0;
Aalique Grahame22e49102018-12-18 14:23:57 -08009235 bool a2dp_reconfig = false;
Zhou Songd6d71752019-05-21 18:08:51 +08009236 struct listnode *node;
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07009237 int controller = -1, stream = -1;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009238
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08009239 ALOGD("%s: enter: %s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009240 parms = str_parms_create_str(kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009241
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05309242 if (!parms)
9243 goto error;
Naresh Tanniru4c630392014-05-12 01:05:52 +05309244
Derek Chen6f293672019-04-01 01:40:24 -07009245 /* notify adev and input/output streams on the snd card status */
9246 adev_snd_mon_cb((void *)adev, parms);
9247
Weiyin Jiang24f55292020-12-22 14:35:46 +08009248 ret = str_parms_get_str(parms, "SND_CARD_STATUS", value, sizeof(value));
9249 if (ret >= 0) {
9250 list_for_each(node, &adev->active_outputs_list) {
9251 streams_output_ctxt_t *out_ctxt = node_to_item(node,
9252 streams_output_ctxt_t,
9253 list);
9254 out_snd_mon_cb((void *)out_ctxt->output, parms);
9255 }
Derek Chen6f293672019-04-01 01:40:24 -07009256
Weiyin Jiang24f55292020-12-22 14:35:46 +08009257 list_for_each(node, &adev->active_inputs_list) {
9258 streams_input_ctxt_t *in_ctxt = node_to_item(node,
9259 streams_input_ctxt_t,
9260 list);
9261 in_snd_mon_cb((void *)in_ctxt->input, parms);
9262 }
Derek Chen6f293672019-04-01 01:40:24 -07009263 }
9264
Zhou Songd6d71752019-05-21 18:08:51 +08009265 pthread_mutex_lock(&adev->lock);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05309266 ret = str_parms_get_str(parms, "BT_SCO", value, sizeof(value));
9267 if (ret >= 0) {
9268 /* When set to false, HAL should disable EC and NS */
Zhou Songd6d71752019-05-21 18:08:51 +08009269 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0){
Ashish Jain1b9b30c2017-05-18 20:57:40 +05309270 adev->bt_sco_on = true;
Shalini Manjunatha3a2e06e2021-02-01 12:23:49 +05309271 /*
9272 * When ever BT_SCO=ON arrives, make sure to route
9273 * all use cases to SCO device, otherwise due to delay in
9274 * BT_SCO=ON and lack of synchronization with create audio patch
9275 * request for SCO device, some times use case not routed properly to
9276 * SCO device
9277 */
9278 struct audio_usecase *usecase;
9279 struct listnode *node;
9280 list_for_each(node, &adev->usecase_list) {
9281 usecase = node_to_item(node, struct audio_usecase, list);
Anver sadhiquef0efca32021-09-03 15:25:44 +05309282 if (usecase->stream.in && (usecase->type == PCM_CAPTURE) &&
Anver sadhique93f3c912021-08-11 11:19:45 +05309283 (!is_btsco_device(SND_DEVICE_NONE, usecase->in_snd_device)) && (is_sco_in_device_type(&usecase->stream.in->device_list))) {
Shalini Manjunatha3a2e06e2021-02-01 12:23:49 +05309284 ALOGD("BT_SCO ON, switch all in use case to it");
9285 select_devices(adev, usecase->id);
9286 }
Mingshu Pangef517202021-04-22 10:35:00 +08009287 if (usecase->stream.out && (usecase->type == PCM_PLAYBACK ||
9288 usecase->type == VOICE_CALL) &&
Anver sadhique93f3c912021-08-11 11:19:45 +05309289 (!is_btsco_device(usecase->out_snd_device, SND_DEVICE_NONE)) && (is_sco_out_device_type(&usecase->stream.out->device_list))) {
Shalini Manjunatha3a2e06e2021-02-01 12:23:49 +05309290 ALOGD("BT_SCO ON, switch all out use case to it");
9291 select_devices(adev, usecase->id);
9292 }
9293 }
9294 }
9295 else {
Ashish Jain1b9b30c2017-05-18 20:57:40 +05309296 adev->bt_sco_on = false;
Zhou Songd6d71752019-05-21 18:08:51 +08009297 audio_extn_sco_reset_configuration();
Kunlei Zhangd96af8f2019-08-27 16:33:29 +08009298 }
9299 }
9300
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07009301 status = voice_set_parameters(adev, parms);
9302 if (status != 0)
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08009303 goto done;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009304
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07009305 status = platform_set_parameters(adev->platform, parms);
9306 if (status != 0)
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08009307 goto done;
9308
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07009309 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
9310 if (ret >= 0) {
Vicky Sehrawate240e5d2014-08-12 17:17:04 -07009311 /* When set to false, HAL should disable EC and NS */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009312 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
9313 adev->bluetooth_nrec = true;
9314 else
9315 adev->bluetooth_nrec = false;
9316 }
9317
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07009318 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
9319 if (ret >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009320 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
9321 adev->screen_off = false;
9322 else
9323 adev->screen_off = true;
Quinn Male70f20f32019-06-26 16:50:26 -07009324 audio_extn_sound_trigger_update_screen_status(adev->screen_off);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009325 }
9326
Eric Laurent4b084132018-10-19 17:33:43 -07009327 ret = str_parms_get_int(parms, "rotation", &val);
9328 if (ret >= 0) {
9329 bool reverse_speakers = false;
9330 int camera_rotation = CAMERA_ROTATION_LANDSCAPE;
9331 switch (val) {
9332 // FIXME: note that the code below assumes that the speakers are in the correct placement
9333 // relative to the user when the device is rotated 90deg from its default rotation. This
9334 // assumption is device-specific, not platform-specific like this code.
9335 case 270:
9336 reverse_speakers = true;
9337 camera_rotation = CAMERA_ROTATION_INVERT_LANDSCAPE;
9338 break;
9339 case 0:
9340 case 180:
9341 camera_rotation = CAMERA_ROTATION_PORTRAIT;
9342 break;
9343 case 90:
9344 camera_rotation = CAMERA_ROTATION_LANDSCAPE;
9345 break;
9346 default:
9347 ALOGE("%s: unexpected rotation of %d", __func__, val);
9348 status = -EINVAL;
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07009349 }
Eric Laurent4b084132018-10-19 17:33:43 -07009350 if (status == 0) {
9351 // check and set swap
9352 // - check if orientation changed and speaker active
9353 // - set rotation and cache the rotation value
9354 adev->camera_orientation =
9355 (adev->camera_orientation & ~CAMERA_ROTATION_MASK) | camera_rotation;
9356 if (!audio_extn_is_maxx_audio_enabled())
9357 platform_check_and_set_swap_lr_channels(adev, reverse_speakers);
9358 }
9359 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07009360
Mingming Yin514a8bc2014-07-29 15:22:21 -07009361 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_SCO_WB, value, sizeof(value));
9362 if (ret >= 0) {
9363 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
9364 adev->bt_wb_speech_enabled = true;
9365 else
9366 adev->bt_wb_speech_enabled = false;
9367 }
9368
Zhou Song12c29502019-03-16 10:37:18 +08009369 ret = str_parms_get_str(parms, "bt_swb", value, sizeof(value));
9370 if (ret >= 0) {
9371 val = atoi(value);
9372 adev->swb_speech_mode = val;
9373 }
9374
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -07009375 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value, sizeof(value));
9376 if (ret >= 0) {
9377 val = atoi(value);
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05309378 audio_devices_t device = (audio_devices_t) val;
Zhou Song681350a2017-10-19 16:28:42 +08009379 if (audio_is_output_device(val) &&
9380 (val & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07009381 ALOGV("cache new ext disp type and edid");
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07009382 platform_get_controller_stream_from_params(parms, &controller, &stream);
9383 platform_set_ext_display_device_v2(adev->platform, controller, stream);
9384 ret = platform_get_ext_disp_type_v2(adev->platform, controller, stream);
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07009385 if (ret < 0) {
9386 ALOGE("%s: Failed to query disp type, ret:%d", __func__, ret);
Manisha Agarwal2f5ff882018-08-08 17:09:29 +05309387 } else {
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07009388 platform_cache_edid_v2(adev->platform, controller, stream);
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07009389 }
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05309390 } else if (audio_is_usb_out_device(device) || audio_is_usb_in_device(device)) {
vivek mehta344576a2016-04-12 18:56:03 -07009391 /*
9392 * Do not allow AFE proxy port usage by WFD source when USB headset is connected.
9393 * Per AudioPolicyManager, USB device is higher priority than WFD.
9394 * For Voice call over USB headset, voice call audio is routed to AFE proxy ports.
9395 * If WFD use case occupies AFE proxy, it may result unintended behavior while
9396 * starting voice call on USB
9397 */
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08009398 ret = str_parms_get_str(parms, "card", value, sizeof(value));
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05309399 if (ret >= 0)
9400 audio_extn_usb_add_device(device, atoi(value));
9401
Zhou Song6f862822017-11-06 17:27:57 +08009402 if (!audio_extn_usb_is_tunnel_supported()) {
9403 ALOGV("detected USB connect .. disable proxy");
9404 adev->allow_afe_proxy_usage = false;
9405 }
Zhou Song503196b2021-07-23 17:31:05 +08009406 } else if (audio_is_hearing_aid_out_device(device) &&
9407 property_get_bool("persist.vendor.audio.ha_proxy.enabled", false)) {
9408 adev->ha_proxy_enable = true;
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -07009409 }
9410 }
9411
9412 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value, sizeof(value));
9413 if (ret >= 0) {
9414 val = atoi(value);
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05309415 audio_devices_t device = (audio_devices_t) val;
Garmond Leunge3b6d482016-10-25 16:48:01 -07009416 /*
9417 * The HDMI / Displayport disconnect handling has been moved to
9418 * audio extension to ensure that its parameters are not
9419 * invalidated prior to updating sysfs of the disconnect event
9420 * Invalidate will be handled by audio_extn_ext_disp_set_parameters()
9421 */
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05309422 if (audio_is_usb_out_device(device) || audio_is_usb_in_device(device)) {
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08009423 ret = str_parms_get_str(parms, "card", value, sizeof(value));
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05309424 if (ret >= 0)
9425 audio_extn_usb_remove_device(device, atoi(value));
9426
Zhou Song6f862822017-11-06 17:27:57 +08009427 if (!audio_extn_usb_is_tunnel_supported()) {
9428 ALOGV("detected USB disconnect .. enable proxy");
9429 adev->allow_afe_proxy_usage = true;
9430 }
Zhou Song503196b2021-07-23 17:31:05 +08009431 } else if (audio_is_hearing_aid_out_device(device)) {
9432 adev->ha_proxy_enable = false;
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -07009433 }
9434 }
9435
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08009436 audio_extn_qdsp_set_parameters(adev, parms);
Aalique Grahame22e49102018-12-18 14:23:57 -08009437
9438 status = audio_extn_a2dp_set_parameters(parms, &a2dp_reconfig);
Aniket Kumar Lata23300322019-02-20 22:25:30 -08009439 if (status >= 0 && a2dp_reconfig) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05309440 struct audio_usecase *usecase;
9441 struct listnode *node;
9442 list_for_each(node, &adev->usecase_list) {
9443 usecase = node_to_item(node, struct audio_usecase, list);
Weiyin Jiangff56ff32020-05-08 14:32:21 +08009444 if ((usecase->stream.out == NULL) || (usecase->type != PCM_PLAYBACK))
9445 continue;
9446
9447 if (is_a2dp_out_device_type(&usecase->device_list)) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05309448 ALOGD("reconfigure a2dp... forcing device switch");
Naresh Tannirucd2353e2016-08-19 00:37:25 +05309449 audio_extn_a2dp_set_handoff_mode(true);
Manisha Agarwalf1c3b6b2019-06-25 13:00:33 +05309450 ALOGD("Switching to speaker and muting the stream before select_devices");
9451 check_a2dp_restore_l(adev, usecase->stream.out, false);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05309452 //force device switch to re configure encoder
9453 select_devices(adev, usecase->id);
Manisha Agarwalf1c3b6b2019-06-25 13:00:33 +05309454 ALOGD("Unmuting the stream after select_devices");
Zhou Songbaddf9f2020-11-20 13:57:39 +08009455 check_a2dp_restore_l(adev, usecase->stream.out, true);
Naresh Tannirucd2353e2016-08-19 00:37:25 +05309456 audio_extn_a2dp_set_handoff_mode(false);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05309457 break;
Zhou Songd01e7a22020-09-23 22:49:01 +08009458 } else if (is_offload_usecase(usecase->stream.out->usecase)) {
Weiyin Jiang280ea742020-09-08 20:28:22 +08009459 pthread_mutex_lock(&usecase->stream.out->latch_lock);
Zhou Songbaddf9f2020-11-20 13:57:39 +08009460 if (usecase->stream.out->a2dp_muted) {
Weiyin Jiang280ea742020-09-08 20:28:22 +08009461 pthread_mutex_unlock(&usecase->stream.out->latch_lock);
9462 reassign_device_list(&usecase->stream.out->device_list,
9463 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP, "");
9464 check_a2dp_restore_l(adev, usecase->stream.out, true);
9465 break;
9466 }
9467 pthread_mutex_unlock(&usecase->stream.out->latch_lock);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05309468 }
9469 }
9470 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08009471
9472 //handle vr audio setparam
9473 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_VR_AUDIO_MODE,
9474 value, sizeof(value));
9475 if (ret >= 0) {
9476 ALOGI("Setting vr mode to be %s", value);
9477 if (!strncmp(value, "true", 4)) {
9478 adev->vr_audio_mode_enabled = true;
9479 ALOGI("Setting vr mode to true");
9480 } else if (!strncmp(value, "false", 5)) {
9481 adev->vr_audio_mode_enabled = false;
9482 ALOGI("Setting vr mode to false");
9483 } else {
9484 ALOGI("wrong vr mode set");
9485 }
9486 }
9487
Eric Laurent4b084132018-10-19 17:33:43 -07009488 //FIXME: to be replaced by proper video capture properties API
9489 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_CAMERA_FACING, value, sizeof(value));
9490 if (ret >= 0) {
9491 int camera_facing = CAMERA_FACING_BACK;
9492 if (strcmp(value, AUDIO_PARAMETER_VALUE_FRONT) == 0)
9493 camera_facing = CAMERA_FACING_FRONT;
9494 else if (strcmp(value, AUDIO_PARAMETER_VALUE_BACK) == 0)
9495 camera_facing = CAMERA_FACING_BACK;
9496 else {
9497 ALOGW("%s: invalid camera facing value: %s", __func__, value);
9498 goto done;
9499 }
9500 adev->camera_orientation =
9501 (adev->camera_orientation & ~CAMERA_FACING_MASK) | camera_facing;
9502 struct audio_usecase *usecase;
9503 struct listnode *node;
9504 list_for_each(node, &adev->usecase_list) {
9505 usecase = node_to_item(node, struct audio_usecase, list);
9506 struct stream_in *in = usecase->stream.in;
9507 if (usecase->type == PCM_CAPTURE && in != NULL &&
9508 in->source == AUDIO_SOURCE_CAMCORDER && !in->standby) {
9509 select_devices(adev, in->usecase);
9510 }
9511 }
9512 }
9513
Tahir Dawson7fabad42022-06-21 12:37:55 -04009514 audio_extn_auto_hal_set_parameters(adev, parms);
Naresh Tannirucd2353e2016-08-19 00:37:25 +05309515 audio_extn_set_parameters(adev, parms);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08009516done:
9517 str_parms_destroy(parms);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08009518 pthread_mutex_unlock(&adev->lock);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05309519error:
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07009520 ALOGV("%s: exit with code(%d)", __func__, status);
9521 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009522}
9523
9524static char* adev_get_parameters(const struct audio_hw_device *dev,
9525 const char *keys)
9526{
Sidipotu Ashokaa4fa6a2018-12-21 09:19:26 +05309527 ALOGD("%s:%s", __func__, keys);
9528
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07009529 struct audio_device *adev = (struct audio_device *)dev;
9530 struct str_parms *reply = str_parms_create();
9531 struct str_parms *query = str_parms_create_str(keys);
9532 char *str;
Naresh Tannirud7205b62014-06-20 02:54:48 +05309533 char value[256] = {0};
9534 int ret = 0;
9535
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07009536 if (!query || !reply) {
Alexy Josephaee4fdd2016-01-29 13:02:07 -08009537 if (reply) {
9538 str_parms_destroy(reply);
9539 }
9540 if (query) {
9541 str_parms_destroy(query);
9542 }
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07009543 ALOGE("adev_get_parameters: failed to create query or reply");
9544 return NULL;
9545 }
9546
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08009547 //handle vr audio getparam
9548
9549 ret = str_parms_get_str(query,
9550 AUDIO_PARAMETER_KEY_VR_AUDIO_MODE,
9551 value, sizeof(value));
9552
9553 if (ret >= 0) {
9554 bool vr_audio_enabled = false;
9555 pthread_mutex_lock(&adev->lock);
9556 vr_audio_enabled = adev->vr_audio_mode_enabled;
9557 pthread_mutex_unlock(&adev->lock);
9558
Aniket Kumar Latad13758f2020-08-06 15:11:36 -07009559 ALOGV("getting vr mode to %d", vr_audio_enabled);
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08009560
9561 if (vr_audio_enabled) {
9562 str_parms_add_str(reply, AUDIO_PARAMETER_KEY_VR_AUDIO_MODE,
9563 "true");
9564 goto exit;
9565 } else {
9566 str_parms_add_str(reply, AUDIO_PARAMETER_KEY_VR_AUDIO_MODE,
9567 "false");
9568 goto exit;
9569 }
9570 }
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07009571
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08009572 pthread_mutex_lock(&adev->lock);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07009573 audio_extn_get_parameters(adev, query, reply);
Shiv Maliyappanahallif9308492013-12-12 12:18:09 -08009574 voice_get_parameters(adev, query, reply);
Aalique Grahame22e49102018-12-18 14:23:57 -08009575 audio_extn_a2dp_get_parameters(query, reply);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07009576 platform_get_parameters(adev->platform, query, reply);
justinwengd5395152019-11-04 12:23:09 +08009577 audio_extn_ma_get_parameters(adev, query, reply);
Naresh Tanniru80659832014-06-04 18:17:56 +05309578 pthread_mutex_unlock(&adev->lock);
9579
Naresh Tannirud7205b62014-06-20 02:54:48 +05309580exit:
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07009581 str = str_parms_to_str(reply);
9582 str_parms_destroy(query);
9583 str_parms_destroy(reply);
9584
Aniket Kumar Latad13758f2020-08-06 15:11:36 -07009585 ALOGV("%s: exit: returns - %s", __func__, str);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07009586 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009587}
9588
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07009589static int adev_init_check(const struct audio_hw_device *dev __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009590{
9591 return 0;
9592}
9593
9594static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
9595{
Haynes Mathew George5191a852013-09-11 14:19:36 -07009596 int ret;
9597 struct audio_device *adev = (struct audio_device *)dev;
Aalique Grahame22e49102018-12-18 14:23:57 -08009598
9599 audio_extn_extspk_set_voice_vol(adev->extspk, volume);
9600
Haynes Mathew George5191a852013-09-11 14:19:36 -07009601 pthread_mutex_lock(&adev->lock);
9602 /* cache volume */
Shruthi Krishnaace10852013-10-25 14:32:12 -07009603 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07009604 pthread_mutex_unlock(&adev->lock);
9605 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009606}
9607
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07009608static int adev_set_master_volume(struct audio_hw_device *dev __unused,
9609 float volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009610{
9611 return -ENOSYS;
9612}
9613
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07009614static int adev_get_master_volume(struct audio_hw_device *dev __unused,
9615 float *volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009616{
9617 return -ENOSYS;
9618}
9619
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07009620static int adev_set_master_mute(struct audio_hw_device *dev __unused,
9621 bool muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009622{
9623 return -ENOSYS;
9624}
9625
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07009626static int adev_get_master_mute(struct audio_hw_device *dev __unused,
9627 bool *muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009628{
9629 return -ENOSYS;
9630}
9631
9632static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
9633{
9634 struct audio_device *adev = (struct audio_device *)dev;
Garmond Leung5fd0b552018-04-17 11:56:12 -07009635 struct listnode *node;
9636 struct audio_usecase *usecase = NULL;
9637 int ret = 0;
kunleizdc4af9d2017-05-04 12:15:35 +08009638
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009639 pthread_mutex_lock(&adev->lock);
9640 if (adev->mode != mode) {
Jaideep Sharma477917f2020-03-13 18:13:33 +05309641 ALOGD("%s: mode %d , prev_mode %d \n", __func__, mode , adev->mode);
9642 adev->prev_mode = adev->mode; /* prev_mode is kept to handle voip concurrency*/
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009643 adev->mode = mode;
Weiyin Jiang96b96f72020-09-22 16:48:19 +08009644 if (mode == AUDIO_MODE_CALL_SCREEN) {
Jaideep Sharma477917f2020-03-13 18:13:33 +05309645 adev->current_call_output = adev->primary_output;
9646 voice_start_call(adev);
9647 } else if (voice_is_in_call_or_call_screen(adev) &&
Kunlei Zhang1d5c7f22019-05-21 14:25:57 +08009648 (mode == AUDIO_MODE_NORMAL ||
9649 (mode == AUDIO_MODE_IN_COMMUNICATION && !voice_is_call_state_active(adev)))) {
Garmond Leung5fd0b552018-04-17 11:56:12 -07009650 list_for_each(node, &adev->usecase_list) {
9651 usecase = node_to_item(node, struct audio_usecase, list);
9652 if (usecase->type == VOICE_CALL)
9653 break;
9654 }
9655 if (usecase &&
9656 audio_is_usb_out_device(usecase->out_snd_device & AUDIO_DEVICE_OUT_ALL_USB)) {
9657 ret = audio_extn_usb_check_and_set_svc_int(usecase,
9658 true);
9659 if (ret != 0) {
9660 /* default service interval was successfully updated,
9661 reopen USB backend with new service interval */
9662 check_usecases_codec_backend(adev,
9663 usecase,
9664 usecase->out_snd_device);
9665 }
9666 }
9667
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07009668 voice_stop_call(adev);
Banajit Goswami20cdd212015-09-11 01:11:30 -07009669 platform_set_gsm_mode(adev->platform, false);
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07009670 adev->current_call_output = NULL;
kunleizdc4af9d2017-05-04 12:15:35 +08009671 // restore device for other active usecases after stop call
9672 list_for_each(node, &adev->usecase_list) {
9673 usecase = node_to_item(node, struct audio_usecase, list);
9674 select_devices(adev, usecase->id);
9675 }
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07009676 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009677 }
9678 pthread_mutex_unlock(&adev->lock);
9679 return 0;
9680}
9681
9682static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
9683{
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08009684 int ret;
Aalique Grahame22e49102018-12-18 14:23:57 -08009685 struct audio_device *adev = (struct audio_device *)dev;
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08009686
9687 pthread_mutex_lock(&adev->lock);
Vidyakumar Athota2850d532013-11-19 16:02:12 -08009688 ALOGD("%s state %d\n", __func__, state);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08009689 ret = voice_set_mic_mute((struct audio_device *)dev, state);
Aalique Grahame22e49102018-12-18 14:23:57 -08009690
Derek Chend2530072014-11-24 12:39:14 -08009691 if (adev->ext_hw_plugin)
9692 ret = audio_extn_ext_hw_plugin_set_mic_mute(adev->ext_hw_plugin, state);
Aalique Grahame22e49102018-12-18 14:23:57 -08009693
9694 adev->mic_muted = state;
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08009695 pthread_mutex_unlock(&adev->lock);
9696
9697 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009698}
9699
9700static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
9701{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07009702 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009703 return 0;
9704}
9705
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07009706static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009707 const struct audio_config *config)
9708{
Avinash Chandrad7296d42021-08-04 15:07:47 +05309709 bool is_usb_hifi = IS_USB_HIFI;
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07009710 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009711
Aalique Grahame22e49102018-12-18 14:23:57 -08009712 /* Don't know if USB HIFI in this context so use true to be conservative */
9713 if (check_input_parameters(config->sample_rate, config->format, channel_count,
Avinash Chandrad7296d42021-08-04 15:07:47 +05309714 is_usb_hifi) != 0)
Aalique Grahame22e49102018-12-18 14:23:57 -08009715 return 0;
9716
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07009717 return get_input_buffer_size(config->sample_rate, config->format, channel_count,
9718 false /* is_low_latency: since we don't know, be conservative */);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009719}
9720
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009721static bool adev_input_allow_hifi_record(struct audio_device *adev,
9722 audio_devices_t devices,
9723 audio_input_flags_t flags,
9724 audio_source_t source) {
9725 const bool allowed = true;
9726
9727 if (!audio_is_usb_in_device(devices))
9728 return !allowed;
9729
9730 switch (flags) {
9731 case AUDIO_INPUT_FLAG_NONE:
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009732 break;
Haynes Mathew George59862182017-10-24 16:23:57 -07009733 case AUDIO_INPUT_FLAG_FAST: // disallow hifi record for FAST as
9734 // it affects RTD numbers over USB
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009735 default:
9736 return !allowed;
9737 }
9738
9739 switch (source) {
9740 case AUDIO_SOURCE_DEFAULT:
9741 case AUDIO_SOURCE_MIC:
9742 case AUDIO_SOURCE_UNPROCESSED:
9743 break;
9744 default:
9745 return !allowed;
9746 }
9747
9748 switch (adev->mode) {
9749 case 0:
9750 break;
9751 default:
9752 return !allowed;
9753 }
9754
9755 return allowed;
9756}
9757
Haynes Mathew George4ffef292017-11-21 15:08:02 -08009758static int adev_update_voice_comm_input_stream(struct stream_in *in,
9759 struct audio_config *config)
9760{
9761 bool valid_rate = (config->sample_rate == 8000 ||
9762 config->sample_rate == 16000 ||
9763 config->sample_rate == 32000 ||
9764 config->sample_rate == 48000);
9765 bool valid_ch = audio_channel_count_from_in_mask(in->channel_mask) == 1;
9766
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08009767 if(!voice_extn_is_compress_voip_supported()) {
kunleiz28c73e72019-03-27 17:24:04 +08009768 if (valid_rate && valid_ch) {
Haynes Mathew George4ffef292017-11-21 15:08:02 -08009769 in->usecase = USECASE_AUDIO_RECORD_VOIP;
9770 in->config = default_pcm_config_voip_copp;
9771 in->config.period_size = VOIP_IO_BUF_SIZE(in->sample_rate,
9772 DEFAULT_VOIP_BUF_DURATION_MS,
9773 DEFAULT_VOIP_BIT_DEPTH_BYTE)/2;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08009774 } else {
9775 ALOGW("%s No valid input in voip, use defaults"
9776 "sample rate %u, channel mask 0x%X",
9777 __func__, config->sample_rate, in->channel_mask);
9778 }
Haynes Mathew George4ffef292017-11-21 15:08:02 -08009779 in->config.rate = config->sample_rate;
9780 in->sample_rate = config->sample_rate;
9781 } else {
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08009782 //XXX needed for voice_extn_compress_voip_open_input_stream
9783 in->config.rate = config->sample_rate;
9784 if ((in->dev->mode == AUDIO_MODE_IN_COMMUNICATION ||
Shalini Manjunathaa763cc42019-08-23 15:13:46 +05309785 in->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08009786 voice_extn_compress_voip_is_active(in->dev)) &&
9787 (voice_extn_compress_voip_is_format_supported(in->format)) &&
9788 valid_rate && valid_ch) {
9789 voice_extn_compress_voip_open_input_stream(in);
9790 // update rate entries to match config from AF
9791 in->config.rate = config->sample_rate;
9792 in->sample_rate = config->sample_rate;
9793 } else {
9794 ALOGW("%s compress voip not active, use defaults", __func__);
9795 }
Haynes Mathew George4ffef292017-11-21 15:08:02 -08009796 }
Haynes Mathew George4ffef292017-11-21 15:08:02 -08009797 return 0;
9798}
9799
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009800static int adev_open_input_stream(struct audio_hw_device *dev,
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07009801 audio_io_handle_t handle,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009802 audio_devices_t devices,
9803 struct audio_config *config,
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07009804 struct audio_stream_in **stream_in,
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05309805 audio_input_flags_t flags,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08009806 const char *address,
Vidyakumar Athota5c398212015-03-31 21:53:21 -07009807 audio_source_t source)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009808{
9809 struct audio_device *adev = (struct audio_device *)dev;
9810 struct stream_in *in;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08009811 int ret = 0, buffer_size, frame_size;
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07009812 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07009813 bool is_low_latency = false;
Divya Narayanan Poojary45f19192016-09-30 18:52:13 +05309814 bool channel_mask_updated = false;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009815 bool is_usb_dev = audio_is_usb_in_device(devices);
9816 bool may_use_hifi_record = adev_input_allow_hifi_record(adev,
9817 devices,
9818 flags,
9819 source);
Andy Hung94320602018-10-29 18:31:12 -07009820 ALOGV("%s: enter: flags %#x, is_usb_dev %d, may_use_hifi_record %d,"
9821 " sample_rate %u, channel_mask %#x, format %#x",
9822 __func__, flags, is_usb_dev, may_use_hifi_record,
9823 config->sample_rate, config->channel_mask, config->format);
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05309824
kunleizdff872d2018-08-20 14:40:33 +08009825 if (is_usb_dev && (!audio_extn_usb_connected(NULL))) {
kunleizd6a9e0c2018-07-30 15:38:52 +08009826 is_usb_dev = false;
9827 devices = AUDIO_DEVICE_IN_BUILTIN_MIC;
9828 ALOGW("%s: ignore set device to non existing USB card, use input device(%#x)",
9829 __func__, devices);
9830 }
9831
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009832 *stream_in = NULL;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009833
9834 if (!(is_usb_dev && may_use_hifi_record)) {
9835 if (config->sample_rate == 0)
9836 config->sample_rate = 48000;
9837 if (config->channel_mask == AUDIO_CHANNEL_NONE)
9838 config->channel_mask = AUDIO_CHANNEL_IN_MONO;
9839 if (config->format == AUDIO_FORMAT_DEFAULT)
9840 config->format = AUDIO_FORMAT_PCM_16_BIT;
9841
9842 channel_count = audio_channel_count_from_in_mask(config->channel_mask);
9843
Aalique Grahame22e49102018-12-18 14:23:57 -08009844 if (check_input_parameters(config->sample_rate, config->format, channel_count,
9845 false) != 0)
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009846 return -EINVAL;
Chaithanya Krishna Bacharaju9955b162016-05-25 16:25:53 +05309847 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009848
9849 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07009850
9851 if (!in) {
9852 ALOGE("failed to allocate input stream");
9853 return -ENOMEM;
9854 }
9855
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05309856 ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x)\
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05309857 stream_handle(%p) io_handle(%d) source(%d) format %x",__func__, config->sample_rate,
9858 config->channel_mask, devices, &in->stream, handle, source, config->format);
Ravi Kumar Alamanda40703102014-04-24 10:34:41 -07009859 pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07009860 pthread_mutex_init(&in->pre_lock, (const pthread_mutexattr_t *) NULL);
Ravi Kumar Alamanda40703102014-04-24 10:34:41 -07009861
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009862 in->stream.common.get_sample_rate = in_get_sample_rate;
9863 in->stream.common.set_sample_rate = in_set_sample_rate;
9864 in->stream.common.get_buffer_size = in_get_buffer_size;
9865 in->stream.common.get_channels = in_get_channels;
9866 in->stream.common.get_format = in_get_format;
9867 in->stream.common.set_format = in_set_format;
9868 in->stream.common.standby = in_standby;
9869 in->stream.common.dump = in_dump;
9870 in->stream.common.set_parameters = in_set_parameters;
9871 in->stream.common.get_parameters = in_get_parameters;
9872 in->stream.common.add_audio_effect = in_add_audio_effect;
9873 in->stream.common.remove_audio_effect = in_remove_audio_effect;
9874 in->stream.set_gain = in_set_gain;
9875 in->stream.read = in_read;
9876 in->stream.get_input_frames_lost = in_get_input_frames_lost;
Aalique Grahame22e49102018-12-18 14:23:57 -08009877 in->stream.get_capture_position = in_get_capture_position;
Naresh Tannirudcb47c52018-06-25 16:23:32 +05309878 in->stream.get_active_microphones = in_get_active_microphones;
Paul McLeana50b7332018-12-17 08:24:21 -07009879 in->stream.set_microphone_direction = in_set_microphone_direction;
9880 in->stream.set_microphone_field_dimension = in_set_microphone_field_dimension;
juyuchendb308c22019-01-21 11:57:17 -07009881 in->stream.update_sink_metadata = in_update_sink_metadata;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009882
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08009883 list_init(&in->device_list);
9884 update_device_list(&in->device_list, devices, address, true);
Vidyakumar Athota5c398212015-03-31 21:53:21 -07009885 in->source = source;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009886 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009887 in->standby = 1;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07009888 in->capture_handle = handle;
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07009889 in->flags = flags;
Haynes Mathew George46740472017-10-27 18:40:12 -07009890 in->bit_width = 16;
9891 in->af_period_multiplier = 1;
justinweng20fb6d82019-02-21 18:49:00 -07009892 in->direction = MIC_DIRECTION_UNSPECIFIED;
9893 in->zoom = 0;
Carter Hsu2e429db2019-05-14 18:50:52 +08009894 list_init(&in->aec_list);
9895 list_init(&in->ns_list);
Phil Burkd898ba62019-06-20 12:49:01 -07009896 in->mmap_shared_memory_fd = -1; // not open
Haynes Mathew George46740472017-10-27 18:40:12 -07009897
Andy Hung94320602018-10-29 18:31:12 -07009898 ALOGV("%s: source %d, config->channel_mask %#x", __func__, source, config->channel_mask);
Aalique Grahame22e49102018-12-18 14:23:57 -08009899 if (source == AUDIO_SOURCE_VOICE_UPLINK ||
9900 source == AUDIO_SOURCE_VOICE_DOWNLINK) {
9901 /* Force channel config requested to mono if incall
9902 record is being requested for only uplink/downlink */
9903 if (config->channel_mask != AUDIO_CHANNEL_IN_MONO) {
9904 config->channel_mask = AUDIO_CHANNEL_IN_MONO;
9905 ret = -EINVAL;
9906 goto err_open;
9907 }
9908 }
9909
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009910 if (is_usb_dev && may_use_hifi_record) {
9911 /* HiFi record selects an appropriate format, channel, rate combo
9912 depending on sink capabilities*/
9913 ret = read_usb_sup_params_and_compare(false /*is_playback*/,
9914 &config->format,
9915 &in->supported_formats[0],
9916 MAX_SUPPORTED_FORMATS,
9917 &config->channel_mask,
9918 &in->supported_channel_masks[0],
9919 MAX_SUPPORTED_CHANNEL_MASKS,
9920 &config->sample_rate,
9921 &in->supported_sample_rates[0],
9922 MAX_SUPPORTED_SAMPLE_RATES);
9923 if (ret != 0) {
9924 ret = -EINVAL;
9925 goto err_open;
9926 }
9927 channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Haynes Mathew George4ffef292017-11-21 15:08:02 -08009928 } else if (config->format == AUDIO_FORMAT_DEFAULT) {
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05309929 config->format = AUDIO_FORMAT_PCM_16_BIT;
Surendar karkaaca3d082017-11-09 15:18:37 +05309930 } else if (property_get_bool("vendor.audio.capture.pcm.32bit.enable", false)
9931 && config->format == AUDIO_FORMAT_PCM_32_BIT) {
9932 in->config.format = PCM_FORMAT_S32_LE;
9933 in->bit_width = 32;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05309934 } else if ((config->format == AUDIO_FORMAT_PCM_FLOAT) ||
9935 (config->format == AUDIO_FORMAT_PCM_32_BIT) ||
9936 (config->format == AUDIO_FORMAT_PCM_24_BIT_PACKED) ||
9937 (config->format == AUDIO_FORMAT_PCM_8_24_BIT)) {
9938 bool ret_error = false;
9939 in->bit_width = 24;
9940 /* 24 bit is restricted to UNPROCESSED source only,also format supported
9941 from HAL is 24_packed and 8_24
9942 *> In case of UNPROCESSED source, for 24 bit, if format requested is other than
9943 24_packed return error indicating supported format is 24_packed
9944 *> In case of any other source requesting 24 bit or float return error
9945 indicating format supported is 16 bit only.
9946
9947 on error flinger will retry with supported format passed
9948 */
9949 if ((source != AUDIO_SOURCE_UNPROCESSED) &&
9950 (source != AUDIO_SOURCE_CAMCORDER)) {
9951 config->format = AUDIO_FORMAT_PCM_16_BIT;
9952 if (config->sample_rate > 48000)
9953 config->sample_rate = 48000;
9954 ret_error = true;
Haynes Mathew George46740472017-10-27 18:40:12 -07009955 } else if (!(config->format == AUDIO_FORMAT_PCM_24_BIT_PACKED ||
9956 config->format == AUDIO_FORMAT_PCM_8_24_BIT)) {
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05309957 config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
9958 ret_error = true;
9959 }
9960
9961 if (ret_error) {
9962 ret = -EINVAL;
9963 goto err_open;
9964 }
9965 }
9966
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009967 in->channel_mask = config->channel_mask;
9968 in->format = config->format;
9969
9970 in->usecase = USECASE_AUDIO_RECORD;
Samyak Jain0aa07ab2019-04-04 14:36:32 +05309971
Huicheng Liu1404ba12020-09-11 01:03:25 -04009972 /* validate bus device address */
9973 if (compare_device_type(&in->device_list, AUDIO_DEVICE_IN_BUS)) {
9974 /* extract car audio stream index */
9975 in->car_audio_stream =
9976 audio_extn_auto_hal_get_car_audio_stream_from_address(address);
9977 if (in->car_audio_stream < 0) {
9978 ALOGE("%s: invalid car audio stream %x",
9979 __func__, in->car_audio_stream);
9980 ret = -EINVAL;
9981 goto err_open;
9982 }
9983 ALOGV("%s: car_audio_stream 0x%x", __func__, in->car_audio_stream);
Huicheng Liuea4fd422021-01-12 18:34:50 -05009984 ret = audio_extn_auto_hal_open_input_stream(in);
9985 if (ret) {
9986 ALOGE("%s: Failed to open input stream for bus device", __func__);
9987 ret = -EINVAL;
9988 goto err_open;
9989 }
Huicheng Liu1404ba12020-09-11 01:03:25 -04009990 }
9991
Susan Wange3959562021-03-11 11:50:26 -05009992 /* reassign use case for echo reference stream on automotive platforms */
9993 if (in->source == AUDIO_SOURCE_ECHO_REFERENCE) {
9994 ret = audio_extn_auto_hal_open_echo_reference_stream(in);
9995 }
9996
Kogara Naveen Kumar379ff692022-01-31 11:56:51 +05309997 if ((in->source == AUDIO_SOURCE_FM_TUNER) || (devices == AUDIO_DEVICE_IN_FM_TUNER)) {
Samyak Jain0aa07ab2019-04-04 14:36:32 +05309998 if(!get_usecase_from_list(adev, USECASE_AUDIO_RECORD_FM_VIRTUAL))
9999 in->usecase = USECASE_AUDIO_RECORD_FM_VIRTUAL;
10000 else {
10001 ret = -EINVAL;
10002 goto err_open;
10003 }
10004 }
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +053010005#ifdef PLATFORM_AUTO
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +053010006 if ((config->sample_rate == 48000 ||
10007 config->sample_rate == 32000 ||
10008 config->sample_rate == 24000 ||
10009 config->sample_rate == 16000 ||
10010 config->sample_rate == 8000)&&
Deeraj Somanfa377bf2019-02-06 12:57:59 +053010011 (flags & AUDIO_INPUT_FLAG_TIMESTAMP) == 0 &&
10012 (flags & AUDIO_INPUT_FLAG_COMPRESS) == 0 &&
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +053010013 (flags & AUDIO_INPUT_FLAG_FAST) != 0)
10014#else
10015 if (config->sample_rate == LOW_LATENCY_CAPTURE_SAMPLE_RATE &&
10016 (flags & AUDIO_INPUT_FLAG_TIMESTAMP) == 0 &&
10017 (flags & AUDIO_INPUT_FLAG_COMPRESS) == 0 &&
10018 (flags & AUDIO_INPUT_FLAG_FAST) != 0)
10019#endif
10020{
Haynes Mathew George484e8d22017-07-31 18:55:17 -070010021 is_low_latency = true;
10022#if LOW_LATENCY_CAPTURE_USE_CASE
Revathi Uddarajud9f23d92020-07-27 10:55:06 +053010023 if ((flags & AUDIO_INPUT_FLAG_VOIP_TX) != 0)
10024 in->usecase = USECASE_AUDIO_RECORD_VOIP_LOW_LATENCY;
10025 else
10026 in->usecase = USECASE_AUDIO_RECORD_LOW_LATENCY;
Haynes Mathew George484e8d22017-07-31 18:55:17 -070010027#endif
10028 in->realtime = may_use_noirq_mode(adev, in->usecase, in->flags);
Aalique Grahame22e49102018-12-18 14:23:57 -080010029 if (!in->realtime) {
10030 in->config = pcm_config_audio_capture;
10031 frame_size = audio_stream_in_frame_size(&in->stream);
10032 buffer_size = get_input_buffer_size(config->sample_rate,
10033 config->format,
10034 channel_count,
10035 is_low_latency);
10036 in->config.period_size = buffer_size / frame_size;
10037 in->config.rate = config->sample_rate;
10038 in->af_period_multiplier = 1;
10039 } else {
10040 // period size is left untouched for rt mode playback
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +053010041#ifdef PLATFORM_AUTO
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +053010042 switch(config->sample_rate)
10043 {
10044 case 48000:
10045 in->config = pcm_config_audio_capture_rt_48KHz;
10046 break;
10047 case 32000:
10048 in->config = pcm_config_audio_capture_rt_32KHz;
10049 break;
10050 case 24000:
10051 in->config = pcm_config_audio_capture_rt_24KHz;
10052 break;
10053 case 16000:
10054 in->config = pcm_config_audio_capture_rt_16KHz;
10055 break;
10056 case 8000:
10057 in->config = pcm_config_audio_capture_rt_8KHz;
10058 break;
10059 default:
10060 in->config = pcm_config_audio_capture_rt_48KHz;
10061 }
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +053010062#else
10063 in->config = pcm_config_audio_capture_rt_48KHz;
10064#endif
Aalique Grahame22e49102018-12-18 14:23:57 -080010065 in->af_period_multiplier = af_period_multiplier;
10066 }
Kogara Naveen Kumarc5758232022-11-07 20:25:40 +053010067
10068 /* assign concurrent capture usecase if record has to caried out from
10069 * actual hardware input source */
10070 if (audio_extn_is_concurrent_capture_enabled() &&
10071 !audio_is_virtual_input_source(in->source)) {
10072 /* Acquire lock to avoid two concurrent use cases initialized to
10073 same pcm record use case */
10074 if (in->usecase == USECASE_AUDIO_RECORD_LOW_LATENCY) {
10075 pthread_mutex_lock(&adev->lock);
10076 if (!(adev->pcm_low_latency_record_uc_state)) {
10077 ALOGD("%s: using USECASE_AUDIO_RECORD_LOW_LATENCY",__func__);
10078 adev->pcm_low_latency_record_uc_state = 1;
10079 pthread_mutex_unlock(&adev->lock);
10080 } else if (audio_extn_is_concurrent_low_latency_pcm_record_enabled()) {
10081 in->usecase = get_low_latency_record_usecase(adev);
10082 pthread_mutex_unlock(&adev->lock);
10083 } else {
10084 pthread_mutex_unlock(&adev->lock);
10085 /* Assign compress record use case for second record */
10086 in->usecase = USECASE_AUDIO_RECORD_COMPRESS2;
10087 in->flags |= AUDIO_INPUT_FLAG_COMPRESS;
10088 ALOGV("%s: overriding usecase with USECASE_AUDIO_RECORD_COMPRESS2 and appending compress flag", __func__);
10089 if (audio_extn_cin_applicable_stream(in)) {
10090 in->sample_rate = config->sample_rate;
10091 ret = audio_extn_cin_configure_input_stream(in, config);
10092 if (ret)
10093 goto err_open;
10094 }
10095 }
10096 }
10097 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -070010098 }
10099
Susan Wangb803cb52021-10-14 12:03:36 -040010100 /* Additional sample rates added below must also be present
10101 in audio_policy_configuration.xml for mmap_no_irq_in */
10102 bool valid_mmap_record_rate = (config->sample_rate == 8000 ||
10103 config->sample_rate == 16000 ||
Susan Wang89da9042021-10-14 12:03:36 -040010104 config->sample_rate == 24000 ||
Susan Wangb803cb52021-10-14 12:03:36 -040010105 config->sample_rate == 32000 ||
10106 config->sample_rate == 48000);
10107 if (valid_mmap_record_rate &&
10108 ((in->flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0)) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -070010109 in->realtime = 0;
10110 in->usecase = USECASE_AUDIO_RECORD_MMAP;
10111 in->config = pcm_config_mmap_capture;
Haynes Mathew George46740472017-10-27 18:40:12 -070010112 in->config.format = pcm_format_from_audio_format(config->format);
Haynes Mathew George484e8d22017-07-31 18:55:17 -070010113 in->stream.start = in_start;
10114 in->stream.stop = in_stop;
10115 in->stream.create_mmap_buffer = in_create_mmap_buffer;
10116 in->stream.get_mmap_position = in_get_mmap_position;
Ramjee Singhbcb3b6c2021-11-17 13:19:16 +053010117 in->config.rate = config->sample_rate;
Haynes Mathew George484e8d22017-07-31 18:55:17 -070010118 ALOGV("%s: USECASE_AUDIO_RECORD_MMAP", __func__);
Haynes Mathew George46740472017-10-27 18:40:12 -070010119 } else if (is_usb_dev && may_use_hifi_record) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -070010120 in->usecase = USECASE_AUDIO_RECORD_HIFI;
10121 in->config = pcm_config_audio_capture;
10122 frame_size = audio_stream_in_frame_size(&in->stream);
10123 buffer_size = get_input_buffer_size(config->sample_rate,
10124 config->format,
10125 channel_count,
10126 false /*is_low_latency*/);
10127 in->config.period_size = buffer_size / frame_size;
10128 in->config.rate = config->sample_rate;
Haynes Mathew George484e8d22017-07-31 18:55:17 -070010129 in->config.format = pcm_format_from_audio_format(config->format);
Karthikeyan Mani07faa602018-08-20 11:01:32 -070010130 switch (config->format) {
10131 case AUDIO_FORMAT_PCM_32_BIT:
10132 in->bit_width = 32;
10133 break;
10134 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
10135 case AUDIO_FORMAT_PCM_8_24_BIT:
10136 in->bit_width = 24;
10137 break;
10138 default:
10139 in->bit_width = 16;
10140 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010141 } else if (is_single_device_type_equal(&in->device_list,
10142 AUDIO_DEVICE_IN_TELEPHONY_RX) ||
10143 is_single_device_type_equal(&in->device_list,
10144 AUDIO_DEVICE_IN_PROXY)) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -070010145 if (config->sample_rate == 0)
10146 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
10147 if (config->sample_rate != 48000 && config->sample_rate != 16000 &&
10148 config->sample_rate != 8000) {
10149 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
10150 ret = -EINVAL;
10151 goto err_open;
10152 }
10153 if (config->format == AUDIO_FORMAT_DEFAULT)
10154 config->format = AUDIO_FORMAT_PCM_16_BIT;
10155 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
10156 config->format = AUDIO_FORMAT_PCM_16_BIT;
10157 ret = -EINVAL;
10158 goto err_open;
10159 }
10160
10161 in->usecase = USECASE_AUDIO_RECORD_AFE_PROXY;
Zhou Song62ea0282020-03-22 19:53:01 +080010162 if (adev->ha_proxy_enable &&
10163 is_single_device_type_equal(&in->device_list,
10164 AUDIO_DEVICE_IN_TELEPHONY_RX))
10165 in->usecase = USECASE_AUDIO_RECORD_AFE_PROXY2;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -070010166 in->config = pcm_config_afe_proxy_record;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -070010167 in->config.rate = config->sample_rate;
Aalique Grahame22e49102018-12-18 14:23:57 -080010168 in->af_period_multiplier = 1;
10169 } else if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
Shalini Manjunathaac412b32020-01-03 15:00:14 +053010170 (!voice_extn_is_compress_voip_supported()) &&
Aalique Grahame22e49102018-12-18 14:23:57 -080010171 in->flags & AUDIO_INPUT_FLAG_VOIP_TX &&
10172 (config->sample_rate == 8000 ||
10173 config->sample_rate == 16000 ||
10174 config->sample_rate == 32000 ||
10175 config->sample_rate == 48000) &&
10176 channel_count == 1) {
10177 in->usecase = USECASE_AUDIO_RECORD_VOIP;
10178 in->config = pcm_config_audio_capture;
10179 frame_size = audio_stream_in_frame_size(&in->stream);
10180 buffer_size = get_stream_buffer_size(VOIP_CAPTURE_PERIOD_DURATION_MSEC,
10181 config->sample_rate,
10182 config->format,
10183 channel_count, false /*is_low_latency*/);
10184 in->config.period_size = buffer_size / frame_size;
10185 in->config.period_count = VOIP_CAPTURE_PERIOD_COUNT;
10186 in->config.rate = config->sample_rate;
10187 in->af_period_multiplier = 1;
Mingshu Pangc2d65042021-01-14 16:19:10 +080010188 } else if (in->realtime) {
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +053010189#ifdef PLATFORM_AUTO
Kogara Naveen Kumara688a812022-04-27 16:45:59 +053010190 switch(config->sample_rate)
10191 {
10192 case 48000:
10193 in->config = pcm_config_audio_capture_rt_48KHz;
10194 break;
10195 case 32000:
10196 in->config = pcm_config_audio_capture_rt_32KHz;
10197 break;
10198 case 24000:
10199 in->config = pcm_config_audio_capture_rt_24KHz;
10200 break;
10201 case 16000:
10202 in->config = pcm_config_audio_capture_rt_16KHz;
10203 break;
10204 case 8000:
10205 in->config = pcm_config_audio_capture_rt_8KHz;
10206 break;
10207 default:
10208 in->config = pcm_config_audio_capture_rt_48KHz;
10209 }
Mingshu Pangc2d65042021-01-14 16:19:10 +080010210 in->config.format = pcm_format_from_audio_format(config->format);
10211 in->af_period_multiplier = af_period_multiplier;
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +053010212#else
10213 in->config = pcm_config_audio_capture_rt_48KHz;
10214#endif
10215} else {
Revathi Uddarajud2634032017-12-07 14:42:34 +053010216 int ret_val;
10217 pthread_mutex_lock(&adev->lock);
10218 ret_val = audio_extn_check_and_set_multichannel_usecase(adev,
10219 in, config, &channel_mask_updated);
10220 pthread_mutex_unlock(&adev->lock);
10221
10222 if (!ret_val) {
10223 if (channel_mask_updated == true) {
10224 ALOGD("%s: return error to retry with updated channel mask (%#x)",
10225 __func__, config->channel_mask);
10226 ret = -EINVAL;
10227 goto err_open;
10228 }
10229 ALOGD("%s: created multi-channel session succesfully",__func__);
10230 } else if (audio_extn_compr_cap_enabled() &&
10231 audio_extn_compr_cap_format_supported(config->format) &&
10232 (in->dev->mode != AUDIO_MODE_IN_COMMUNICATION)) {
10233 audio_extn_compr_cap_init(in);
10234 } else if (audio_extn_cin_applicable_stream(in)) {
Deeraj Soman14230922019-01-30 16:39:30 +053010235 ret = audio_extn_cin_configure_input_stream(in, config);
Revathi Uddarajud2634032017-12-07 14:42:34 +053010236 if (ret)
10237 goto err_open;
10238 } else {
10239 in->config = pcm_config_audio_capture;
10240 in->config.rate = config->sample_rate;
10241 in->config.format = pcm_format_from_audio_format(config->format);
Revathi Uddarajud2634032017-12-07 14:42:34 +053010242 in->format = config->format;
10243 frame_size = audio_stream_in_frame_size(&in->stream);
10244 buffer_size = get_input_buffer_size(config->sample_rate,
Haynes Mathew George46740472017-10-27 18:40:12 -070010245 config->format,
10246 channel_count,
10247 is_low_latency);
Dieter Luecking5d57def2018-09-07 14:23:37 +020010248 /* prevent division-by-zero */
10249 if (frame_size == 0) {
10250 ALOGE("%s: Error frame_size==0", __func__);
10251 ret = -EINVAL;
10252 goto err_open;
10253 }
10254
Revathi Uddarajud2634032017-12-07 14:42:34 +053010255 in->config.period_size = buffer_size / frame_size;
Aalique Grahame22e49102018-12-18 14:23:57 -080010256 in->af_period_multiplier = 1;
Haynes Mathew George4ffef292017-11-21 15:08:02 -080010257
Revathi Uddarajud2634032017-12-07 14:42:34 +053010258 if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
10259 /* optionally use VOIP usecase depending on config(s) */
10260 ret = adev_update_voice_comm_input_stream(in, config);
10261 }
Haynes Mathew George4ffef292017-11-21 15:08:02 -080010262
Revathi Uddarajud2634032017-12-07 14:42:34 +053010263 if (ret) {
10264 ALOGE("%s AUDIO_SOURCE_VOICE_COMMUNICATION invalid args", __func__);
10265 goto err_open;
10266 }
Haynes Mathew George4ffef292017-11-21 15:08:02 -080010267 }
Jaideep Sharmad305a4a2020-02-27 14:29:04 +053010268
10269 /* assign concurrent capture usecase if record has to caried out from
10270 * actual hardware input source */
10271 if (audio_extn_is_concurrent_capture_enabled() &&
10272 !audio_is_virtual_input_source(in->source)) {
Samyak Jainc37062f2019-04-25 18:41:06 +053010273 /* Acquire lock to avoid two concurrent use cases initialized to
10274 same pcm record use case */
kunleiz28c73e72019-03-27 17:24:04 +080010275
Samyak Jainc37062f2019-04-25 18:41:06 +053010276 if (in->usecase == USECASE_AUDIO_RECORD) {
10277 pthread_mutex_lock(&adev->lock);
10278 if (!(adev->pcm_record_uc_state)) {
10279 ALOGV("%s: using USECASE_AUDIO_RECORD",__func__);
10280 adev->pcm_record_uc_state = 1;
10281 pthread_mutex_unlock(&adev->lock);
Krishna Kishor Jha0f4d8482022-02-18 10:56:05 +053010282 } else if (audio_extn_is_concurrent_pcm_record_enabled()) {
10283 in->usecase = get_record_usecase(adev);
10284 pthread_mutex_unlock(&adev->lock);
Samyak Jainc37062f2019-04-25 18:41:06 +053010285 } else {
10286 pthread_mutex_unlock(&adev->lock);
10287 /* Assign compress record use case for second record */
10288 in->usecase = USECASE_AUDIO_RECORD_COMPRESS2;
10289 in->flags |= AUDIO_INPUT_FLAG_COMPRESS;
10290 ALOGV("%s: overriding usecase with USECASE_AUDIO_RECORD_COMPRESS2 and appending compress flag", __func__);
10291 if (audio_extn_cin_applicable_stream(in)) {
10292 in->sample_rate = config->sample_rate;
Deeraj Soman14230922019-01-30 16:39:30 +053010293 ret = audio_extn_cin_configure_input_stream(in, config);
Samyak Jainc37062f2019-04-25 18:41:06 +053010294 if (ret)
10295 goto err_open;
10296 }
10297 }
10298 }
kunleiz28c73e72019-03-27 17:24:04 +080010299 }
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -070010300 }
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +053010301
Ramjee Singh82fd0c12019-08-21 16:31:33 +053010302 if (audio_extn_ssr_get_stream() != in)
10303 in->config.channels = channel_count;
Aniket Kumar Lata23d8cbf2019-04-10 19:54:46 -070010304
Aniket Kumar Lata23d8cbf2019-04-10 19:54:46 -070010305 in->sample_rate = in->config.rate;
10306
Dhananjay Kumard6d32152016-10-13 16:11:03 +053010307 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
10308 &adev->streams_input_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010309 &in->device_list, flags, in->format,
Haynes Mathew George4ffef292017-11-21 15:08:02 -080010310 in->sample_rate, in->bit_width,
10311 in->profile, &in->app_type_cfg);
Naresh Tanniru04f71882018-06-26 17:46:22 +053010312 register_format(in->format, in->supported_formats);
10313 register_channel_mask(in->channel_mask, in->supported_channel_masks);
10314 register_sample_rate(in->sample_rate, in->supported_sample_rates);
Dhananjay Kumard6d32152016-10-13 16:11:03 +053010315
Dechen Chai22768452021-07-30 09:29:16 +053010316#ifndef LINUX_ENABLED
Aalique Grahame22e49102018-12-18 14:23:57 -080010317 in->error_log = error_log_create(
10318 ERROR_LOG_ENTRIES,
10319 1000000000 /* aggregate consecutive identical errors within one second */);
Dechen Chai22768452021-07-30 09:29:16 +053010320#endif
Aalique Grahame22e49102018-12-18 14:23:57 -080010321
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -070010322 /* This stream could be for sound trigger lab,
10323 get sound trigger pcm if present */
10324 audio_extn_sound_trigger_check_and_get_session(in);
10325
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053010326 lock_input_stream(in);
10327 audio_extn_snd_mon_register_listener(in, in_snd_mon_cb);
10328 pthread_mutex_lock(&adev->lock);
10329 in->card_status = adev->card_status;
10330 pthread_mutex_unlock(&adev->lock);
10331 pthread_mutex_unlock(&in->lock);
10332
Aalique Grahame22e49102018-12-18 14:23:57 -080010333 stream_app_type_cfg_init(&in->app_type_cfg);
10334
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010335 *stream_in = &in->stream;
Derek Chenf939fb72018-11-13 13:34:41 -080010336
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010337 ret = io_streams_map_insert(adev, &in->stream.common,
10338 handle, AUDIO_PATCH_HANDLE_NONE);
10339 if (ret != 0)
10340 goto err_open;
10341
Susan Wang6dd13092021-01-25 10:27:11 -050010342 in->in_ctxt.input = in;
Derek Chenf939fb72018-11-13 13:34:41 -080010343
10344 pthread_mutex_lock(&adev->lock);
Susan Wang6dd13092021-01-25 10:27:11 -050010345 list_add_tail(&adev->active_inputs_list, &in->in_ctxt.list);
Derek Chenf939fb72018-11-13 13:34:41 -080010346 pthread_mutex_unlock(&adev->lock);
10347
Eric Laurent994a6932013-07-17 11:51:42 -070010348 ALOGV("%s: exit", __func__);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -080010349 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010350
10351err_open:
Krishna Kishor Jha0f4d8482022-02-18 10:56:05 +053010352 if (audio_extn_is_concurrent_pcm_record_enabled() && is_pcm_record_usecase(in->usecase)) {
10353 free_record_usecase(adev, in->usecase);
10354 } else if (in->usecase == USECASE_AUDIO_RECORD) {
Samyak Jainc37062f2019-04-25 18:41:06 +053010355 pthread_mutex_lock(&adev->lock);
10356 adev->pcm_record_uc_state = 0;
10357 pthread_mutex_unlock(&adev->lock);
10358 }
Kogara Naveen Kumarc5758232022-11-07 20:25:40 +053010359 if (audio_extn_is_concurrent_low_latency_pcm_record_enabled() && is_pcm_low_latency_record_usecase(in->usecase)) {
10360 free_low_latency_record_usecase(adev, in->usecase);
10361 } else if (in->usecase == USECASE_AUDIO_RECORD_LOW_LATENCY) {
10362 pthread_mutex_lock(&adev->lock);
10363 adev->pcm_low_latency_record_uc_state = 0;
10364 pthread_mutex_unlock(&adev->lock);
10365 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010366 free(in);
10367 *stream_in = NULL;
10368 return ret;
10369}
10370
10371static void adev_close_input_stream(struct audio_hw_device *dev,
10372 struct audio_stream_in *stream)
10373{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -080010374 int ret;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -070010375 struct stream_in *in = (struct stream_in *)stream;
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -070010376 struct audio_device *adev = (struct audio_device *)dev;
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +053010377
Sidipotu Ashokf43018c2014-05-02 16:21:50 +053010378 ALOGD("%s: enter:stream_handle(%p)",__func__, in);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -080010379
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010380 if (in == NULL) {
10381 ALOGE("%s: audio_stream_in ptr is NULL", __func__);
10382 return;
10383 }
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010384 io_streams_map_remove(adev, in->capture_handle);
10385
Susan Wang6dd13092021-01-25 10:27:11 -050010386 // remove out_ctxt early to prevent the stream
10387 // being opened in a race condition
10388 pthread_mutex_lock(&adev->lock);
10389 list_remove(&in->in_ctxt.list);
10390 pthread_mutex_unlock(&adev->lock);
10391
kunleiz70e57612018-12-28 17:50:23 +080010392 /* must deregister from sndmonitor first to prevent races
10393 * between the callback and close_stream
10394 */
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053010395 audio_extn_snd_mon_unregister_listener(stream);
10396
kunleiz70e57612018-12-28 17:50:23 +080010397 /* Disable echo reference if there are no active input, hfp call
10398 * and sound trigger while closing input stream
10399 */
Eric Laurent637e2d42018-11-15 12:24:31 -080010400 if (adev_get_active_input(adev) == NULL &&
kunleiz70e57612018-12-28 17:50:23 +080010401 !audio_extn_hfp_is_active(adev) &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010402 !audio_extn_sound_trigger_check_ec_ref_enable()) {
10403 struct listnode out_devices;
10404 list_init(&out_devices);
10405 platform_set_echo_reference(adev, false, &out_devices);
Zhenlin Lian4f947842022-05-14 15:50:52 +053010406 clear_devices(&out_devices);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010407 } else
kunleiz70e57612018-12-28 17:50:23 +080010408 audio_extn_sound_trigger_update_ec_ref_status(false);
Venkata Narendra Kumar Gutta1bbbf542014-09-04 19:11:25 +053010409
Dechen Chai22768452021-07-30 09:29:16 +053010410#ifndef LINUX_ENABLED
Weiyin Jiang2995f662019-04-17 14:25:12 +080010411 error_log_destroy(in->error_log);
10412 in->error_log = NULL;
Dechen Chai22768452021-07-30 09:29:16 +053010413#endif
Pallavid7c7a272018-01-16 11:22:55 +053010414
Narsinga Rao Chella05573b72013-11-15 15:21:40 -080010415 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +053010416 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -080010417 ret = voice_extn_compress_voip_close_input_stream(&stream->common);
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +053010418 pthread_mutex_unlock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -080010419 if (ret != 0)
10420 ALOGE("%s: Compress voip input cannot be closed, error:%d",
10421 __func__, ret);
10422 } else
10423 in_standby(&stream->common);
10424
Weiyin Jiang280ea742020-09-08 20:28:22 +080010425 pthread_mutex_destroy(&in->lock);
10426 pthread_mutex_destroy(&in->pre_lock);
10427
Revathi Uddarajud2634032017-12-07 14:42:34 +053010428 pthread_mutex_lock(&adev->lock);
Krishna Kishor Jha0f4d8482022-02-18 10:56:05 +053010429 if (audio_extn_is_concurrent_pcm_record_enabled() && is_pcm_record_usecase(in->usecase)) {
10430 free_record_usecase(adev, in->usecase);
10431 } else if (in->usecase == USECASE_AUDIO_RECORD) {
Samyak Jain15fda662018-12-18 16:40:52 +053010432 adev->pcm_record_uc_state = 0;
10433 }
10434
Kogara Naveen Kumarc5758232022-11-07 20:25:40 +053010435 if (audio_extn_is_concurrent_low_latency_pcm_record_enabled() && is_pcm_low_latency_record_usecase(in->usecase)) {
10436 free_low_latency_record_usecase(adev, in->usecase);
10437 } else if (in->usecase == USECASE_AUDIO_RECORD_LOW_LATENCY) {
10438 adev->pcm_low_latency_record_uc_state = 0;
10439 }
10440
Kunlei Zhang5d5d8d92020-02-26 15:00:59 +080010441 if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
10442 adev->enable_voicerx = false;
10443 }
10444
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -070010445 if (audio_extn_ssr_get_stream() == in) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -070010446 audio_extn_ssr_deinit();
10447 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010448
Garmond Leunge2433c32017-09-28 21:51:22 -070010449 if (audio_extn_ffv_get_stream() == in) {
10450 audio_extn_ffv_stream_deinit();
10451 }
10452
Dhananjay Kumaree4d2002016-10-25 18:02:58 +053010453 if (audio_extn_compr_cap_enabled() &&
Subhadra Jagadeesan63a1e832023-01-13 11:26:38 +053010454 audio_extn_compr_cap_format_supported(pcm_format_to_audio_format((in->config).format)))
Mingming Yine62d7842013-10-25 16:26:03 -070010455 audio_extn_compr_cap_deinit();
Dhanalakshmi Siddani74cf00b2016-12-02 13:55:57 +053010456
Dhananjay Kumar7dbe3562019-08-30 05:49:33 +053010457 if (audio_extn_cin_attached_usecase(in))
Manish Dewangan46e07982018-12-13 18:18:59 +053010458 audio_extn_cin_free_input_stream_resources(in);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -070010459
Mingming Yinfd7607b2016-01-22 12:48:44 -080010460 if (in->is_st_session) {
10461 ALOGV("%s: sound trigger pcm stop lab", __func__);
10462 audio_extn_sound_trigger_stop_lab(in);
10463 }
Zhenlin Lian4f947842022-05-14 15:50:52 +053010464 clear_devices(&in->device_list);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -070010465 free(stream);
Revathi Uddarajud2634032017-12-07 14:42:34 +053010466 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010467 return;
10468}
10469
Aalique Grahame22e49102018-12-18 14:23:57 -080010470/* verifies input and output devices and their capabilities.
10471 *
10472 * This verification is required when enabling extended bit-depth or
10473 * sampling rates, as not all qcom products support it.
10474 *
10475 * Suitable for calling only on initialization such as adev_open().
10476 * It fills the audio_device use_case_table[] array.
10477 *
10478 * Has a side-effect that it needs to configure audio routing / devices
10479 * in order to power up the devices and read the device parameters.
10480 * It does not acquire any hw device lock. Should restore the devices
10481 * back to "normal state" upon completion.
10482 */
10483static int adev_verify_devices(struct audio_device *adev)
10484{
10485 /* enumeration is a bit difficult because one really wants to pull
10486 * the use_case, device id, etc from the hidden pcm_device_table[].
10487 * In this case there are the following use cases and device ids.
10488 *
10489 * [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = {0, 0},
10490 * [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {15, 15},
10491 * [USECASE_AUDIO_PLAYBACK_HIFI] = {1, 1},
10492 * [USECASE_AUDIO_PLAYBACK_OFFLOAD] = {9, 9},
10493 * [USECASE_AUDIO_RECORD] = {0, 0},
10494 * [USECASE_AUDIO_RECORD_LOW_LATENCY] = {15, 15},
10495 * [USECASE_VOICE_CALL] = {2, 2},
10496 *
10497 * USECASE_AUDIO_PLAYBACK_OFFLOAD, USECASE_AUDIO_PLAYBACK_HIFI omitted.
10498 * USECASE_VOICE_CALL omitted, but possible for either input or output.
10499 */
10500
10501 /* should be the usecases enabled in adev_open_input_stream() */
10502 static const int test_in_usecases[] = {
10503 USECASE_AUDIO_RECORD,
10504 USECASE_AUDIO_RECORD_LOW_LATENCY, /* does not appear to be used */
10505 };
10506 /* should be the usecases enabled in adev_open_output_stream()*/
10507 static const int test_out_usecases[] = {
10508 USECASE_AUDIO_PLAYBACK_DEEP_BUFFER,
10509 USECASE_AUDIO_PLAYBACK_LOW_LATENCY,
10510 };
10511 static const usecase_type_t usecase_type_by_dir[] = {
10512 PCM_PLAYBACK,
10513 PCM_CAPTURE,
10514 };
10515 static const unsigned flags_by_dir[] = {
10516 PCM_OUT,
10517 PCM_IN,
10518 };
10519
10520 size_t i;
10521 unsigned dir;
10522 const unsigned card_id = adev->snd_card;
10523
10524 for (dir = 0; dir < 2; ++dir) {
10525 const usecase_type_t usecase_type = usecase_type_by_dir[dir];
10526 const unsigned flags_dir = flags_by_dir[dir];
10527 const size_t testsize =
10528 dir ? ARRAY_SIZE(test_in_usecases) : ARRAY_SIZE(test_out_usecases);
10529 const int *testcases =
10530 dir ? test_in_usecases : test_out_usecases;
10531 const audio_devices_t audio_device =
10532 dir ? AUDIO_DEVICE_IN_BUILTIN_MIC : AUDIO_DEVICE_OUT_SPEAKER;
10533
10534 for (i = 0; i < testsize; ++i) {
10535 const audio_usecase_t audio_usecase = testcases[i];
10536 int device_id;
10537 struct pcm_params **pparams;
10538 struct stream_out out;
10539 struct stream_in in;
10540 struct audio_usecase uc_info;
10541 int retval;
10542
10543 pparams = &adev->use_case_table[audio_usecase];
10544 pcm_params_free(*pparams); /* can accept null input */
10545 *pparams = NULL;
10546
10547 /* find the device ID for the use case (signed, for error) */
10548 device_id = platform_get_pcm_device_id(audio_usecase, usecase_type);
10549 if (device_id < 0)
10550 continue;
10551
10552 /* prepare structures for device probing */
10553 memset(&uc_info, 0, sizeof(uc_info));
10554 uc_info.id = audio_usecase;
10555 uc_info.type = usecase_type;
Aniket Kumar Lata9c2fd892020-01-22 22:20:00 -080010556 list_init(&uc_info.device_list);
Aalique Grahame22e49102018-12-18 14:23:57 -080010557 if (dir) {
Aalique Grahame22e49102018-12-18 14:23:57 -080010558 memset(&in, 0, sizeof(in));
Aniket Kumar Lata9c2fd892020-01-22 22:20:00 -080010559 list_init(&in.device_list);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010560 update_device_list(&in.device_list, audio_device, "", true);
Aalique Grahame22e49102018-12-18 14:23:57 -080010561 in.source = AUDIO_SOURCE_VOICE_COMMUNICATION;
10562 uc_info.stream.in = &in;
Aalique Grahame22e49102018-12-18 14:23:57 -080010563 }
10564 memset(&out, 0, sizeof(out));
Aniket Kumar Lata9c2fd892020-01-22 22:20:00 -080010565 list_init(&out.device_list);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010566 update_device_list(&out.device_list, audio_device, "", true);
Aalique Grahame22e49102018-12-18 14:23:57 -080010567 uc_info.stream.out = &out;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010568 update_device_list(&uc_info.device_list, audio_device, "", true);
Aalique Grahame22e49102018-12-18 14:23:57 -080010569 uc_info.in_snd_device = SND_DEVICE_NONE;
10570 uc_info.out_snd_device = SND_DEVICE_NONE;
10571 list_add_tail(&adev->usecase_list, &uc_info.list);
10572
10573 /* select device - similar to start_(in/out)put_stream() */
10574 retval = select_devices(adev, audio_usecase);
10575 if (retval >= 0) {
10576 *pparams = pcm_params_get(card_id, device_id, flags_dir);
10577#if LOG_NDEBUG == 0
Aalique Grahame203bee02019-03-13 17:49:36 -070010578 char info[512]; /* for possible debug info */
Aalique Grahame22e49102018-12-18 14:23:57 -080010579 if (*pparams) {
10580 ALOGV("%s: (%s) card %d device %d", __func__,
10581 dir ? "input" : "output", card_id, device_id);
10582 pcm_params_to_string(*pparams, info, ARRAY_SIZE(info));
10583 } else {
10584 ALOGV("%s: cannot locate card %d device %d", __func__, card_id, device_id);
10585 }
10586#endif
10587 }
10588
10589 /* deselect device - similar to stop_(in/out)put_stream() */
10590 /* 1. Get and set stream specific mixer controls */
10591 retval = disable_audio_route(adev, &uc_info);
10592 /* 2. Disable the rx device */
10593 retval = disable_snd_device(adev,
10594 dir ? uc_info.in_snd_device : uc_info.out_snd_device);
10595 list_remove(&uc_info.list);
10596 }
10597 }
Aalique Grahame22e49102018-12-18 14:23:57 -080010598 return 0;
10599}
10600
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010601int update_patch(unsigned int num_sources,
10602 const struct audio_port_config *sources,
10603 unsigned int num_sinks,
10604 const struct audio_port_config *sinks,
10605 audio_patch_handle_t handle,
10606 struct audio_patch_info *p_info,
10607 patch_type_t patch_type, bool new_patch)
10608{
Aniket Kumar Latad13758f2020-08-06 15:11:36 -070010609 ALOGV("%s: enter", __func__);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010610
10611 if (p_info == NULL) {
10612 ALOGE("%s: Invalid patch pointer", __func__);
10613 return -EINVAL;
10614 }
10615
10616 if (new_patch) {
10617 p_info->patch = (struct audio_patch *) calloc(1, sizeof(struct audio_patch));
10618 if (p_info->patch == NULL) {
10619 ALOGE("%s: Could not allocate patch", __func__);
10620 return -ENOMEM;
10621 }
10622 }
10623
10624 p_info->patch->id = handle;
10625 p_info->patch->num_sources = num_sources;
10626 p_info->patch->num_sinks = num_sinks;
10627
10628 for (int i = 0; i < num_sources; i++)
10629 p_info->patch->sources[i] = sources[i];
10630 for (int i = 0; i < num_sinks; i++)
10631 p_info->patch->sinks[i] = sinks[i];
10632
10633 p_info->patch_type = patch_type;
10634 return 0;
10635}
10636
10637audio_patch_handle_t generate_patch_handle()
10638{
10639 static audio_patch_handle_t patch_handle = AUDIO_PATCH_HANDLE_NONE;
10640 if (++patch_handle < 0)
10641 patch_handle = AUDIO_PATCH_HANDLE_NONE + 1;
10642 return patch_handle;
10643}
10644
Siddartha Shaik31b530e2017-05-19 15:26:33 +053010645int adev_create_audio_patch(struct audio_hw_device *dev,
10646 unsigned int num_sources,
10647 const struct audio_port_config *sources,
10648 unsigned int num_sinks,
10649 const struct audio_port_config *sinks,
10650 audio_patch_handle_t *handle)
10651{
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010652 int ret = 0;
10653 struct audio_device *adev = (struct audio_device *)dev;
10654 struct audio_patch_info *p_info = NULL;
10655 patch_type_t patch_type = PATCH_NONE;
10656 audio_io_handle_t io_handle = AUDIO_IO_HANDLE_NONE;
10657 audio_source_t input_source = AUDIO_SOURCE_DEFAULT;
10658 struct audio_stream_info *s_info = NULL;
10659 struct audio_stream *stream = NULL;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010660 struct listnode devices;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010661 audio_devices_t device_type = AUDIO_DEVICE_NONE;
10662 bool new_patch = false;
10663 char addr[AUDIO_DEVICE_MAX_ADDRESS_LEN];
Siddartha Shaik31b530e2017-05-19 15:26:33 +053010664
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010665 ALOGD("%s: enter: num sources %d, num_sinks %d, handle %d", __func__,
10666 num_sources, num_sinks, *handle);
10667
10668 if (num_sources == 0 || num_sources > AUDIO_PATCH_PORTS_MAX ||
10669 num_sinks == 0 || num_sinks > AUDIO_PATCH_PORTS_MAX) {
10670 ALOGE("%s: Invalid patch arguments", __func__);
10671 ret = -EINVAL;
10672 goto done;
10673 }
10674
10675 if (num_sources > 1) {
10676 ALOGE("%s: Multiple sources are not supported", __func__);
10677 ret = -EINVAL;
10678 goto done;
10679 }
10680
10681 if (sources == NULL || sinks == NULL) {
10682 ALOGE("%s: Invalid sources or sinks port config", __func__);
10683 ret = -EINVAL;
10684 goto done;
10685 }
10686
10687 ALOGV("%s: source role %d, source type %d", __func__,
10688 sources[0].type, sources[0].role);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010689 list_init(&devices);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010690
10691 // Populate source/sink information and fetch stream info
10692 switch (sources[0].type) {
10693 case AUDIO_PORT_TYPE_DEVICE: // Patch for audio capture or loopback
10694 device_type = sources[0].ext.device.type;
10695 strlcpy(&addr[0], &sources[0].ext.device.address[0], AUDIO_DEVICE_MAX_ADDRESS_LEN);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010696 update_device_list(&devices, device_type, &addr[0], true);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010697 if (sinks[0].type == AUDIO_PORT_TYPE_MIX) {
10698 patch_type = PATCH_CAPTURE;
10699 io_handle = sinks[0].ext.mix.handle;
10700 input_source = sinks[0].ext.mix.usecase.source;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010701 ALOGD("%s: Capture patch from device %x to mix %d",
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010702 __func__, device_type, io_handle);
10703 } else {
10704 // Device to device patch is not implemented.
10705 // This space will need changes if audio HAL
10706 // handles device to device patches in the future.
10707 patch_type = PATCH_DEVICE_LOOPBACK;
10708 }
10709 break;
10710 case AUDIO_PORT_TYPE_MIX: // Patch for audio playback
10711 io_handle = sources[0].ext.mix.handle;
10712 for (int i = 0; i < num_sinks; i++) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010713 device_type = sinks[i].ext.device.type;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010714 strlcpy(&addr[0], &sinks[i].ext.device.address[0], AUDIO_DEVICE_MAX_ADDRESS_LEN);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010715 update_device_list(&devices, device_type, &addr[0], true);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010716 }
10717 patch_type = PATCH_PLAYBACK;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010718 ALOGD("%s: Playback patch from mix handle %d to device %x",
10719 __func__, io_handle, get_device_types(&devices));
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010720 break;
10721 case AUDIO_PORT_TYPE_SESSION:
10722 case AUDIO_PORT_TYPE_NONE:
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010723 ALOGE("%s: Unsupported source type %d", __func__, sources[0].type);
10724 ret = -EINVAL;
10725 goto done;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010726 }
10727
10728 pthread_mutex_lock(&adev->lock);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010729
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010730 // Generate patch info and update patch
10731 if (*handle == AUDIO_PATCH_HANDLE_NONE) {
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010732 *handle = generate_patch_handle();
10733 p_info = (struct audio_patch_info *)
10734 calloc(1, sizeof(struct audio_patch_info));
10735 if (p_info == NULL) {
10736 ALOGE("%s: Failed to allocate memory", __func__);
10737 pthread_mutex_unlock(&adev->lock);
10738 ret = -ENOMEM;
10739 goto done;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010740 }
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010741 new_patch = true;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010742 } else {
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010743 p_info = fetch_patch_info_l(adev, *handle);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010744 if (p_info == NULL) {
10745 ALOGE("%s: Unable to fetch patch for received patch handle %d",
10746 __func__, *handle);
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010747 pthread_mutex_unlock(&adev->lock);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010748 ret = -EINVAL;
10749 goto done;
10750 }
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010751 }
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010752 update_patch(num_sources, sources, num_sinks, sinks,
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010753 *handle, p_info, patch_type, new_patch);
10754
10755 // Fetch stream info of associated mix for playback or capture patches
10756 if (p_info->patch_type == PATCH_PLAYBACK ||
10757 p_info->patch_type == PATCH_CAPTURE) {
10758 s_info = hashmapGet(adev->io_streams_map, (void *) (intptr_t) io_handle);
10759 if (s_info == NULL) {
10760 ALOGE("%s: Failed to obtain stream info", __func__);
10761 if (new_patch)
10762 free(p_info);
10763 pthread_mutex_unlock(&adev->lock);
10764 ret = -EINVAL;
10765 goto done;
10766 }
10767 ALOGV("%s: Fetched stream info with io_handle %d", __func__, io_handle);
10768 s_info->patch_handle = *handle;
10769 stream = s_info->stream;
10770 }
10771 pthread_mutex_unlock(&adev->lock);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010772
10773 // Update routing for stream
10774 if (stream != NULL) {
Revathi Uddaraju4255a632021-12-02 05:11:13 -080010775 if (p_info->patch_type == PATCH_PLAYBACK) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010776 ret = route_output_stream((struct stream_out *) stream, &devices);
Revathi Uddaraju4255a632021-12-02 05:11:13 -080010777 } else if (p_info->patch_type == PATCH_CAPTURE) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010778 ret = route_input_stream((struct stream_in *) stream, &devices, input_source);
Revathi Uddaraju4255a632021-12-02 05:11:13 -080010779 }
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010780 if (ret < 0) {
10781 pthread_mutex_lock(&adev->lock);
10782 s_info->patch_handle = AUDIO_PATCH_HANDLE_NONE;
10783 if (new_patch)
10784 free(p_info);
10785 pthread_mutex_unlock(&adev->lock);
10786 ALOGE("%s: Stream routing failed for io_handle %d", __func__, io_handle);
10787 goto done;
10788 }
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010789 }
10790
10791 // Add new patch to patch map
10792 if (!ret && new_patch) {
10793 pthread_mutex_lock(&adev->lock);
10794 hashmapPut(adev->patch_map, (void *) (intptr_t) *handle, (void *) p_info);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010795 ALOGD("%s: Added a new patch with handle %d", __func__, *handle);
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010796 pthread_mutex_unlock(&adev->lock);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010797 }
10798
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010799done:
Zhenlin Lian4f947842022-05-14 15:50:52 +053010800 clear_devices(&devices);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010801 audio_extn_hw_loopback_create_audio_patch(dev,
Derek Chenf939fb72018-11-13 13:34:41 -080010802 num_sources,
10803 sources,
10804 num_sinks,
10805 sinks,
10806 handle);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010807 audio_extn_auto_hal_create_audio_patch(dev,
Derek Chenf939fb72018-11-13 13:34:41 -080010808 num_sources,
10809 sources,
10810 num_sinks,
10811 sinks,
10812 handle);
10813 return ret;
Siddartha Shaik31b530e2017-05-19 15:26:33 +053010814}
10815
10816int adev_release_audio_patch(struct audio_hw_device *dev,
10817 audio_patch_handle_t handle)
10818{
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010819 struct audio_device *adev = (struct audio_device *) dev;
10820 int ret = 0;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010821 audio_source_t input_source = AUDIO_SOURCE_DEFAULT;
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010822 struct audio_stream *stream = NULL;
Derek Chenf939fb72018-11-13 13:34:41 -080010823
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010824 if (handle == AUDIO_PATCH_HANDLE_NONE) {
10825 ALOGE("%s: Invalid patch handle %d", __func__, handle);
10826 ret = -EINVAL;
10827 goto done;
10828 }
10829
10830 ALOGD("%s: Remove patch with handle %d", __func__, handle);
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010831 pthread_mutex_lock(&adev->lock);
10832 struct audio_patch_info *p_info = fetch_patch_info_l(adev, handle);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010833 if (p_info == NULL) {
10834 ALOGE("%s: Patch info not found with handle %d", __func__, handle);
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010835 pthread_mutex_unlock(&adev->lock);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010836 ret = -EINVAL;
10837 goto done;
10838 }
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010839 struct audio_patch *patch = p_info->patch;
10840 if (patch == NULL) {
10841 ALOGE("%s: Patch not found for handle %d", __func__, handle);
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010842 pthread_mutex_unlock(&adev->lock);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010843 ret = -EINVAL;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010844 goto done;
10845 }
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010846 audio_io_handle_t io_handle = AUDIO_IO_HANDLE_NONE;
10847 switch (patch->sources[0].type) {
10848 case AUDIO_PORT_TYPE_MIX:
10849 io_handle = patch->sources[0].ext.mix.handle;
10850 break;
10851 case AUDIO_PORT_TYPE_DEVICE:
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010852 if (p_info->patch_type == PATCH_CAPTURE)
10853 io_handle = patch->sinks[0].ext.mix.handle;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010854 break;
10855 case AUDIO_PORT_TYPE_SESSION:
10856 case AUDIO_PORT_TYPE_NONE:
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010857 pthread_mutex_unlock(&adev->lock);
10858 ret = -EINVAL;
10859 goto done;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010860 }
10861
10862 // Remove patch and reset patch handle in stream info
Aniket Kumar Latace2b9fa2020-03-10 15:04:15 -070010863 patch_type_t patch_type = p_info->patch_type;
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010864 patch_map_remove_l(adev, handle);
Aniket Kumar Latace2b9fa2020-03-10 15:04:15 -070010865 if (patch_type == PATCH_PLAYBACK ||
10866 patch_type == PATCH_CAPTURE) {
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010867 struct audio_stream_info *s_info =
10868 hashmapGet(adev->io_streams_map, (void *) (intptr_t) io_handle);
10869 if (s_info == NULL) {
10870 ALOGE("%s: stream for io_handle %d is not available", __func__, io_handle);
10871 pthread_mutex_unlock(&adev->lock);
10872 goto done;
10873 }
10874 s_info->patch_handle = AUDIO_PATCH_HANDLE_NONE;
10875 stream = s_info->stream;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010876 }
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010877 pthread_mutex_unlock(&adev->lock);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010878
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010879 if (stream != NULL) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010880 struct listnode devices;
10881 list_init(&devices);
Aniket Kumar Latace2b9fa2020-03-10 15:04:15 -070010882 if (patch_type == PATCH_PLAYBACK)
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010883 ret = route_output_stream((struct stream_out *) stream, &devices);
Aniket Kumar Latace2b9fa2020-03-10 15:04:15 -070010884 else if (patch_type == PATCH_CAPTURE)
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010885 ret = route_input_stream((struct stream_in *) stream, &devices, input_source);
Zhenlin Lian4f947842022-05-14 15:50:52 +053010886 clear_devices(&devices);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010887 }
10888
10889 if (ret < 0)
10890 ALOGW("%s: Stream routing failed for io_handle %d", __func__, io_handle);
10891
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010892done:
10893 audio_extn_hw_loopback_release_audio_patch(dev, handle);
10894 audio_extn_auto_hal_release_audio_patch(dev, handle);
10895
10896 ALOGV("%s: Successfully released patch %d", __func__, handle);
Derek Chenf939fb72018-11-13 13:34:41 -080010897 return ret;
Siddartha Shaik31b530e2017-05-19 15:26:33 +053010898}
10899
10900int adev_get_audio_port(struct audio_hw_device *dev, struct audio_port *config)
10901{
Derek Chenf13dd492018-11-13 14:53:51 -080010902 int ret = 0;
10903
10904 ret = audio_extn_hw_loopback_get_audio_port(dev, config);
10905 ret |= audio_extn_auto_hal_get_audio_port(dev, config);
10906 return ret;
Siddartha Shaik31b530e2017-05-19 15:26:33 +053010907}
10908
10909int adev_set_audio_port_config(struct audio_hw_device *dev,
10910 const struct audio_port_config *config)
10911{
Derek Chenf13dd492018-11-13 14:53:51 -080010912 int ret = 0;
10913
10914 ret = audio_extn_hw_loopback_set_audio_port_config(dev, config);
10915 ret |= audio_extn_auto_hal_set_audio_port_config(dev, config);
10916 return ret;
Siddartha Shaik31b530e2017-05-19 15:26:33 +053010917}
10918
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -070010919static int adev_dump(const audio_hw_device_t *device __unused,
10920 int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010921{
10922 return 0;
10923}
10924
10925static int adev_close(hw_device_t *device)
10926{
Aalique Grahame22e49102018-12-18 14:23:57 -080010927 size_t i;
Preetam Singh Ranawatacc64542019-06-24 15:11:28 +053010928 struct audio_device *adev_temp = (struct audio_device *)device;
Kiran Kandi910e1862013-10-29 13:29:42 -070010929
Preetam Singh Ranawatacc64542019-06-24 15:11:28 +053010930 if (!adev_temp)
Kiran Kandi910e1862013-10-29 13:29:42 -070010931 return 0;
10932
10933 pthread_mutex_lock(&adev_init_lock);
10934
10935 if ((--audio_device_ref_count) == 0) {
Sujin Panicker390724d2019-04-26 10:43:36 +053010936 if (audio_extn_spkr_prot_is_enabled())
10937 audio_extn_spkr_prot_deinit();
Jaideep Sharmaa2b49672019-09-10 20:37:03 +053010938 audio_extn_battery_properties_listener_deinit();
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053010939 audio_extn_snd_mon_unregister_listener(adev);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -070010940 audio_extn_sound_trigger_deinit(adev);
Kiran Kandide144c82013-11-20 15:58:32 -080010941 audio_extn_listen_deinit(adev);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -080010942 audio_extn_qdsp_deinit();
Aalique Grahame22e49102018-12-18 14:23:57 -080010943 audio_extn_extspk_deinit(adev->extspk);
Dhananjay Kumard6d32152016-10-13 16:11:03 +053010944 audio_extn_utils_release_streams_cfg_lists(
10945 &adev->streams_output_cfg_list,
10946 &adev->streams_input_cfg_list);
Sidipotu Ashokd2f10ba2019-05-06 15:41:56 +053010947 if (audio_extn_qap_is_enabled())
10948 audio_extn_qap_deinit();
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +053010949 if (audio_extn_qaf_is_enabled())
10950 audio_extn_qaf_deinit();
Kiran Kandi910e1862013-10-29 13:29:42 -070010951 audio_route_free(adev->audio_route);
Weiyin Jiangfa65d3e2019-03-05 23:39:45 +080010952 audio_extn_gef_deinit(adev);
Kiran Kandi910e1862013-10-29 13:29:42 -070010953 free(adev->snd_dev_ref_cnt);
10954 platform_deinit(adev->platform);
Aalique Grahame22e49102018-12-18 14:23:57 -080010955 for (i = 0; i < ARRAY_SIZE(adev->use_case_table); ++i) {
10956 pcm_params_free(adev->use_case_table[i]);
10957 }
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -070010958 if (adev->adm_deinit)
10959 adev->adm_deinit(adev->adm_data);
Dhananjay Kumaree4d2002016-10-25 18:02:58 +053010960 qahwi_deinit(device);
Ben Rombergerd771a7c2017-02-22 18:05:17 -080010961 audio_extn_adsp_hdlr_deinit();
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053010962 audio_extn_snd_mon_deinit();
Siddartha Shaik44dd7702017-06-14 12:13:25 +053010963 audio_extn_hw_loopback_deinit(adev);
Garmond Leunge2433c32017-09-28 21:51:22 -070010964 audio_extn_ffv_deinit();
Satish Babu Patakokilac3c5d432017-07-04 22:48:59 +053010965 if (adev->device_cfg_params) {
10966 free(adev->device_cfg_params);
10967 adev->device_cfg_params = NULL;
10968 }
Derek Chend2530072014-11-24 12:39:14 -080010969 if(adev->ext_hw_plugin)
10970 audio_extn_ext_hw_plugin_deinit(adev->ext_hw_plugin);
Derek Chenae7b0342019-02-08 15:17:04 -080010971 audio_extn_auto_hal_deinit();
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010972 free_map(adev->patch_map);
10973 free_map(adev->io_streams_map);
Kiran Kandi910e1862013-10-29 13:29:42 -070010974 free(device);
10975 adev = NULL;
10976 }
10977 pthread_mutex_unlock(&adev_init_lock);
Vatsal Buchac09ae062018-11-14 13:25:08 +053010978 enable_gcov();
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010979 return 0;
10980}
10981
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -070010982/* This returns 1 if the input parameter looks at all plausible as a low latency period size,
10983 * or 0 otherwise. A return value of 1 doesn't mean the value is guaranteed to work,
10984 * just that it _might_ work.
10985 */
10986static int period_size_is_plausible_for_low_latency(int period_size)
10987{
10988 switch (period_size) {
10989 case 160:
Ravi Kumar Alamanda474de5a2015-06-25 20:08:01 -070010990 case 192:
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -070010991 case 240:
10992 case 320:
10993 case 480:
10994 return 1;
10995 default:
10996 return 0;
10997 }
10998}
10999
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053011000static void adev_snd_mon_cb(void *cookie, struct str_parms *parms)
11001{
11002 bool is_snd_card_status = false;
11003 bool is_ext_device_status = false;
11004 char value[32];
11005 int card = -1;
11006 card_status_t status;
11007
11008 if (cookie != adev || !parms)
11009 return;
11010
11011 if (!parse_snd_card_status(parms, &card, &status)) {
11012 is_snd_card_status = true;
11013 } else if (0 < str_parms_get_str(parms, "ext_audio_device", value, sizeof(value))) {
11014 is_ext_device_status = true;
11015 } else {
11016 // not a valid event
11017 return;
11018 }
11019
11020 pthread_mutex_lock(&adev->lock);
11021 if (card == adev->snd_card || is_ext_device_status) {
11022 if (is_snd_card_status && adev->card_status != status) {
Jaideep Sharmacb402512020-09-24 17:51:07 +053011023 ALOGD("%s card_status %d", __func__, status);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053011024 adev->card_status = status;
11025 platform_snd_card_update(adev->platform, status);
11026 audio_extn_fm_set_parameters(adev, parms);
Derek Chend6f371d2019-03-01 13:45:58 -080011027 audio_extn_auto_hal_set_parameters(adev, parms);
Zhou Song4f43e352019-07-02 00:32:23 +080011028 if (status == CARD_STATUS_OFFLINE)
11029 audio_extn_sco_reset_configuration();
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053011030 } else if (is_ext_device_status) {
11031 platform_set_parameters(adev->platform, parms);
11032 }
11033 }
11034 pthread_mutex_unlock(&adev->lock);
11035 return;
11036}
11037
Weiyin Jiang280ea742020-09-08 20:28:22 +080011038/* adev lock held */
11039int check_a2dp_restore_l(struct audio_device *adev, struct stream_out *out, bool restore)
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053011040{
11041 struct audio_usecase *uc_info;
Zhou Song407e5aa2020-12-27 19:13:04 +080011042 struct audio_usecase *usecase;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080011043 struct listnode devices;
Zhou Song407e5aa2020-12-27 19:13:04 +080011044 struct listnode *node;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053011045
11046 uc_info = get_usecase_from_list(adev, out->usecase);
11047 if (uc_info == NULL) {
11048 ALOGE("%s: Could not find the usecase (%d) in the list",
11049 __func__, out->usecase);
11050 return -EINVAL;
11051 }
Aniket Kumar Lata9c2fd892020-01-22 22:20:00 -080011052 list_init(&devices);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053011053
Zhou Songbaddf9f2020-11-20 13:57:39 +080011054 ALOGD("%s: enter: usecase(%d: %s), a2dp muted %d", __func__,
11055 out->usecase, use_case_table[out->usecase], out->a2dp_muted);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053011056
11057 if (restore) {
Weiyin Jiang280ea742020-09-08 20:28:22 +080011058 pthread_mutex_lock(&out->latch_lock);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053011059 // restore A2DP device for active usecases and unmute if required
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080011060 if (is_a2dp_out_device_type(&out->device_list)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053011061 ALOGD("%s: restoring A2dp and unmuting stream", __func__);
Zhou Songe5225132019-09-26 15:33:36 +080011062 if (uc_info->out_snd_device != SND_DEVICE_OUT_BT_A2DP)
11063 select_devices(adev, uc_info->id);
Zhou Songbaddf9f2020-11-20 13:57:39 +080011064
11065 if (is_offload_usecase(out->usecase)) {
11066 if (uc_info->out_snd_device == SND_DEVICE_OUT_BT_A2DP)
Weiyin Jiang280ea742020-09-08 20:28:22 +080011067 out_set_compr_volume(&out->stream, out->volume_l, out->volume_r);
Zhou Songbaddf9f2020-11-20 13:57:39 +080011068 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP) {
11069 out_set_voip_volume(&out->stream, out->volume_l, out->volume_r);
11070 } else {
11071 out_set_pcm_volume(&out->stream, out->volume_l, out->volume_r);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053011072 }
Zhou Songbaddf9f2020-11-20 13:57:39 +080011073 out->a2dp_muted = false;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053011074 }
Weiyin Jiang280ea742020-09-08 20:28:22 +080011075 pthread_mutex_unlock(&out->latch_lock);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053011076 } else {
Weiyin Jiang280ea742020-09-08 20:28:22 +080011077 pthread_mutex_lock(&out->latch_lock);
Zhou Songbaddf9f2020-11-20 13:57:39 +080011078 // mute stream and switch to speaker if suspended
11079 if (!out->a2dp_muted && !out->standby) {
Zhou Songbaddf9f2020-11-20 13:57:39 +080011080 assign_devices(&devices, &out->device_list);
11081 reassign_device_list(&out->device_list, AUDIO_DEVICE_OUT_SPEAKER, "");
Zhou Song407e5aa2020-12-27 19:13:04 +080011082 list_for_each(node, &adev->usecase_list) {
11083 usecase = node_to_item(node, struct audio_usecase, list);
Zhou Songe63b6fe2021-04-15 13:54:46 +080011084 if ((usecase->type != PCM_CAPTURE) && (usecase != uc_info) &&
11085 !is_a2dp_out_device_type(&usecase->stream.out->device_list) &&
Zhou Songcf77af02021-05-14 18:21:14 +080011086 !is_sco_out_device_type(&usecase->stream.out->device_list) &&
Zhou Songe63b6fe2021-04-15 13:54:46 +080011087 platform_check_backends_match(SND_DEVICE_OUT_SPEAKER,
11088 usecase->out_snd_device)) {
Zhou Song407e5aa2020-12-27 19:13:04 +080011089 assign_devices(&out->device_list, &usecase->stream.out->device_list);
11090 break;
11091 }
11092 }
Zhou Songcf77af02021-05-14 18:21:14 +080011093 if ((is_a2dp_out_device_type(&devices) && list_length(&devices) == 1) ||
11094 (uc_info->out_snd_device == SND_DEVICE_OUT_BT_A2DP)) {
Zhou Song8edbbdb2021-01-14 16:48:03 +080011095 out->a2dp_muted = true;
11096 if (is_offload_usecase(out->usecase)) {
11097 if (out->offload_state == OFFLOAD_STATE_PLAYING)
11098 compress_pause(out->compr);
11099 out_set_compr_volume(&out->stream, (float)0, (float)0);
Zhou Song8edbbdb2021-01-14 16:48:03 +080011100 } else {
Weiyin Jiang906db3c2021-03-02 13:17:04 +080011101 if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP)
11102 out_set_voip_volume(&out->stream, (float)0, (float)0);
11103 else
11104 out_set_pcm_volume(&out->stream, (float)0, (float)0);
11105
Zhou Song8edbbdb2021-01-14 16:48:03 +080011106 /* wait for stale pcm drained before switching to speaker */
11107 uint32_t latency =
11108 (out->config.period_count * out->config.period_size * 1000) /
11109 (out->config.rate);
11110 usleep(latency * 1000);
11111 }
Zhou Songbaddf9f2020-11-20 13:57:39 +080011112 }
11113 select_devices(adev, out->usecase);
Zhou Song8edbbdb2021-01-14 16:48:03 +080011114 ALOGD("%s: switched to device:%s and stream muted:%d", __func__,
11115 platform_get_snd_device_name(uc_info->out_snd_device), out->a2dp_muted);
Zhou Songbaddf9f2020-11-20 13:57:39 +080011116 if (is_offload_usecase(out->usecase)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053011117 if (out->offload_state == OFFLOAD_STATE_PLAYING)
11118 compress_resume(out->compr);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053011119 }
Zhou Songbaddf9f2020-11-20 13:57:39 +080011120 assign_devices(&out->device_list, &devices);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053011121 }
Weiyin Jiang280ea742020-09-08 20:28:22 +080011122 pthread_mutex_unlock(&out->latch_lock);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053011123 }
Zhenlin Lian4f947842022-05-14 15:50:52 +053011124 clear_devices(&devices);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053011125 ALOGV("%s: exit", __func__);
11126 return 0;
11127}
11128
Haynes Mathew George01156f92018-04-13 15:29:54 -070011129void adev_on_battery_status_changed(bool charging)
11130{
11131 pthread_mutex_lock(&adev->lock);
11132 ALOGI("%s: battery status changed to %scharging", __func__, charging ? "" : "not ");
11133 adev->is_charging = charging;
Zhou Songc1088ea2018-06-12 00:17:29 +080011134 audio_extn_sound_trigger_update_battery_status(charging);
Haynes Mathew George01156f92018-04-13 15:29:54 -070011135 pthread_mutex_unlock(&adev->lock);
11136}
11137
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080011138static int adev_open(const hw_module_t *module, const char *name,
11139 hw_device_t **device)
11140{
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +053011141 int ret;
Derek Chenf939fb72018-11-13 13:34:41 -080011142 char value[PROPERTY_VALUE_MAX] = {0};
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +053011143 char mixer_ctl_name[128] = {0};
11144 struct mixer_ctl *ctl = NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +053011145
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -080011146 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080011147 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
11148
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070011149 pthread_mutex_lock(&adev_init_lock);
Kiran Kandi910e1862013-10-29 13:29:42 -070011150 if (audio_device_ref_count != 0){
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070011151 *device = &adev->device.common;
Kiran Kandi910e1862013-10-29 13:29:42 -070011152 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070011153 ALOGD("%s: returning existing instance of adev", __func__);
11154 ALOGD("%s: exit", __func__);
11155 pthread_mutex_unlock(&adev_init_lock);
11156 return 0;
11157 }
11158
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080011159 adev = calloc(1, sizeof(struct audio_device));
11160
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -070011161 if (!adev) {
11162 pthread_mutex_unlock(&adev_init_lock);
11163 return -ENOMEM;
11164 }
11165
Ravi Kumar Alamanda40703102014-04-24 10:34:41 -070011166 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
11167
Weiyin Jiange6ce6312019-01-28 18:28:22 +080011168 // register audio ext hidl at the earliest
11169 audio_extn_hidl_init();
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053011170#ifdef DYNAMIC_LOG_ENABLED
11171 register_for_dynamic_logging("hal");
11172#endif
11173
Derek Chenf939fb72018-11-13 13:34:41 -080011174 /* default audio HAL major version */
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070011175 uint32_t maj_version = 3;
Derek Chenf939fb72018-11-13 13:34:41 -080011176 if(property_get("vendor.audio.hal.maj.version", value, NULL))
11177 maj_version = atoi(value);
11178
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080011179 adev->device.common.tag = HARDWARE_DEVICE_TAG;
Derek Chenf939fb72018-11-13 13:34:41 -080011180 adev->device.common.version = HARDWARE_DEVICE_API_VERSION(maj_version, 0);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080011181 adev->device.common.module = (struct hw_module_t *)module;
11182 adev->device.common.close = adev_close;
11183
11184 adev->device.init_check = adev_init_check;
11185 adev->device.set_voice_volume = adev_set_voice_volume;
11186 adev->device.set_master_volume = adev_set_master_volume;
11187 adev->device.get_master_volume = adev_get_master_volume;
11188 adev->device.set_master_mute = adev_set_master_mute;
11189 adev->device.get_master_mute = adev_get_master_mute;
11190 adev->device.set_mode = adev_set_mode;
11191 adev->device.set_mic_mute = adev_set_mic_mute;
11192 adev->device.get_mic_mute = adev_get_mic_mute;
11193 adev->device.set_parameters = adev_set_parameters;
11194 adev->device.get_parameters = adev_get_parameters;
11195 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
11196 adev->device.open_output_stream = adev_open_output_stream;
11197 adev->device.close_output_stream = adev_close_output_stream;
11198 adev->device.open_input_stream = adev_open_input_stream;
11199 adev->device.close_input_stream = adev_close_input_stream;
Siddartha Shaik31b530e2017-05-19 15:26:33 +053011200 adev->device.create_audio_patch = adev_create_audio_patch;
11201 adev->device.release_audio_patch = adev_release_audio_patch;
11202 adev->device.get_audio_port = adev_get_audio_port;
11203 adev->device.set_audio_port_config = adev_set_audio_port_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080011204 adev->device.dump = adev_dump;
Naresh Tannirudcb47c52018-06-25 16:23:32 +053011205 adev->device.get_microphones = adev_get_microphones;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080011206
11207 /* Set the default route before the PCM stream is opened */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080011208 adev->mode = AUDIO_MODE_NORMAL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -080011209 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080011210 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080011211 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -080011212 adev->acdb_settings = TTY_MODE_OFF;
vivek mehta344576a2016-04-12 18:56:03 -070011213 adev->allow_afe_proxy_usage = true;
Ashish Jain1b9b30c2017-05-18 20:57:40 +053011214 adev->bt_sco_on = false;
Eric Laurent07eeafd2013-10-06 12:52:49 -070011215 /* adev->cur_hdmi_channels = 0; by calloc() */
Eric Laurentb23d5282013-05-14 15:27:20 -070011216 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
vivek mehtaae1018c2019-05-09 12:19:57 -070011217 /* Init audio and voice feature */
11218 audio_extn_feature_init();
11219 voice_extn_feature_init();
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -070011220 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -080011221 list_init(&adev->usecase_list);
Derek Chenf939fb72018-11-13 13:34:41 -080011222 list_init(&adev->active_inputs_list);
11223 list_init(&adev->active_outputs_list);
Rahul Sharma99770982019-03-06 17:05:26 +053011224 list_init(&adev->audio_patch_record_list);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070011225 adev->io_streams_map = hashmapCreate(AUDIO_IO_PORTS_MAX, audio_extn_utils_hash_fn,
11226 audio_extn_utils_hash_eq);
11227 if (!adev->io_streams_map) {
11228 ALOGE("%s: Could not create io streams map", __func__);
11229 ret = -ENOMEM;
11230 goto adev_open_err;
11231 }
11232 adev->patch_map = hashmapCreate(AUDIO_PATCH_PORTS_MAX, audio_extn_utils_hash_fn,
11233 audio_extn_utils_hash_eq);
11234 if (!adev->patch_map) {
11235 ALOGE("%s: Could not create audio patch map", __func__);
11236 ret = -ENOMEM;
11237 goto adev_open_err;
11238 }
Krishnankutty Kolathappilly0b2de1c2014-02-14 14:45:49 -080011239 adev->cur_wfd_channels = 2;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -070011240 adev->offload_usecases_state = 0;
Samyak Jain15fda662018-12-18 16:40:52 +053011241 adev->pcm_record_uc_state = 0;
Kogara Naveen Kumarc5758232022-11-07 20:25:40 +053011242 adev->pcm_low_latency_record_uc_state = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +053011243 adev->is_channel_status_set = false;
Sudheer Papothifa9d2282015-09-17 01:53:25 +053011244 adev->perf_lock_opts[0] = 0x101;
11245 adev->perf_lock_opts[1] = 0x20E;
11246 adev->perf_lock_opts_size = 2;
Xiaojun Sang785b5da2017-08-03 15:52:29 +080011247 adev->dsp_bit_width_enforce_mode = 0;
Aalique Grahame552b0832019-03-11 10:16:38 -070011248 adev->enable_hfp = false;
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +053011249 adev->use_old_pspd_mix_ctrl = false;
Aniket Kumar Lata60586a92019-05-22 22:18:55 -070011250 adev->adm_routing_changed = false;
Revathi Uddarajub26e3932020-06-10 14:51:02 +053011251 adev->a2dp_started = false;
Zhou Song503196b2021-07-23 17:31:05 +080011252 adev->ha_proxy_enable = false;
Naresh Tanniru4c630392014-05-12 01:05:52 +053011253
Zhou Song68ebc352019-12-05 17:11:15 +080011254 audio_extn_perf_lock_init();
11255
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080011256 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -070011257 adev->platform = platform_init(adev);
11258 if (!adev->platform) {
Eric Laurentb23d5282013-05-14 15:27:20 -070011259 ALOGE("%s: Failed to init platform data, aborting.", __func__);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070011260 ret = -EINVAL;
11261 goto adev_open_err;
Eric Laurentb23d5282013-05-14 15:27:20 -070011262 }
Eric Laurentc4aef752013-09-12 17:45:53 -070011263
Aalique Grahame22e49102018-12-18 14:23:57 -080011264 adev->extspk = audio_extn_extspk_init(adev);
Sidipotu Ashokd2f10ba2019-05-06 15:41:56 +053011265 if (audio_extn_qap_is_enabled()) {
11266 ret = audio_extn_qap_init(adev);
11267 if (ret < 0) {
Sidipotu Ashokd2f10ba2019-05-06 15:41:56 +053011268 ALOGE("%s: Failed to init platform data, aborting.", __func__);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070011269 goto adev_open_err;
Sidipotu Ashokd2f10ba2019-05-06 15:41:56 +053011270 }
11271 adev->device.open_output_stream = audio_extn_qap_open_output_stream;
11272 adev->device.close_output_stream = audio_extn_qap_close_output_stream;
11273 }
Aalique Grahame22e49102018-12-18 14:23:57 -080011274
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +053011275 if (audio_extn_qaf_is_enabled()) {
11276 ret = audio_extn_qaf_init(adev);
11277 if (ret < 0) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +053011278 ALOGE("%s: Failed to init platform data, aborting.", __func__);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070011279 goto adev_open_err;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +053011280 }
11281
11282 adev->device.open_output_stream = audio_extn_qaf_open_output_stream;
11283 adev->device.close_output_stream = audio_extn_qaf_close_output_stream;
11284 }
11285
Derek Chenae7b0342019-02-08 15:17:04 -080011286 audio_extn_auto_hal_init(adev);
Derek Chend2530072014-11-24 12:39:14 -080011287 adev->ext_hw_plugin = audio_extn_ext_hw_plugin_init(adev);
11288
Eric Laurentc4aef752013-09-12 17:45:53 -070011289 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
11290 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
11291 if (adev->visualizer_lib == NULL) {
11292 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
11293 } else {
11294 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
11295 adev->visualizer_start_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -080011296 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -070011297 "visualizer_hal_start_output");
11298 adev->visualizer_stop_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -080011299 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -070011300 "visualizer_hal_stop_output");
11301 }
11302 }
Dhanalakshmi Siddani21be3ac2016-12-29 14:31:08 +053011303 audio_extn_init(adev);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -080011304 voice_extn_init(adev);
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -080011305 audio_extn_listen_init(adev, adev->snd_card);
Weiyin Jiangaa80acd2016-09-21 16:42:11 +080011306 audio_extn_gef_init(adev);
Siddartha Shaik44dd7702017-06-14 12:13:25 +053011307 audio_extn_hw_loopback_init(adev);
Garmond Leunge2433c32017-09-28 21:51:22 -070011308 audio_extn_ffv_init(adev);
Eric Laurentc4aef752013-09-12 17:45:53 -070011309
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -080011310 if (access(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, R_OK) == 0) {
11311 adev->offload_effects_lib = dlopen(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, RTLD_NOW);
11312 if (adev->offload_effects_lib == NULL) {
11313 ALOGE("%s: DLOPEN failed for %s", __func__,
11314 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
11315 } else {
11316 ALOGV("%s: DLOPEN successful for %s", __func__,
11317 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
11318 adev->offload_effects_start_output =
Ashish Jain5106d362016-05-11 19:23:33 +053011319 (int (*)(audio_io_handle_t, int, struct mixer *))dlsym(adev->offload_effects_lib,
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -080011320 "offload_effects_bundle_hal_start_output");
11321 adev->offload_effects_stop_output =
11322 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
11323 "offload_effects_bundle_hal_stop_output");
Jitendra Naruka1b6513f2014-11-22 19:34:13 -080011324 adev->offload_effects_set_hpx_state =
11325 (int (*)(bool))dlsym(adev->offload_effects_lib,
11326 "offload_effects_bundle_set_hpx_state");
Dhananjay Kumard68883d2015-09-04 13:39:26 +053011327 adev->offload_effects_get_parameters =
11328 (void (*)(struct str_parms *, struct str_parms *))
11329 dlsym(adev->offload_effects_lib,
11330 "offload_effects_bundle_get_parameters");
11331 adev->offload_effects_set_parameters =
11332 (void (*)(struct str_parms *))dlsym(adev->offload_effects_lib,
11333 "offload_effects_bundle_set_parameters");
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -080011334 }
11335 }
11336
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -070011337 if (access(ADM_LIBRARY_PATH, R_OK) == 0) {
11338 adev->adm_lib = dlopen(ADM_LIBRARY_PATH, RTLD_NOW);
11339 if (adev->adm_lib == NULL) {
11340 ALOGE("%s: DLOPEN failed for %s", __func__, ADM_LIBRARY_PATH);
11341 } else {
11342 ALOGV("%s: DLOPEN successful for %s", __func__, ADM_LIBRARY_PATH);
11343 adev->adm_init = (adm_init_t)
11344 dlsym(adev->adm_lib, "adm_init");
11345 adev->adm_deinit = (adm_deinit_t)
11346 dlsym(adev->adm_lib, "adm_deinit");
11347 adev->adm_register_input_stream = (adm_register_input_stream_t)
11348 dlsym(adev->adm_lib, "adm_register_input_stream");
11349 adev->adm_register_output_stream = (adm_register_output_stream_t)
11350 dlsym(adev->adm_lib, "adm_register_output_stream");
11351 adev->adm_deregister_stream = (adm_deregister_stream_t)
11352 dlsym(adev->adm_lib, "adm_deregister_stream");
11353 adev->adm_request_focus = (adm_request_focus_t)
11354 dlsym(adev->adm_lib, "adm_request_focus");
11355 adev->adm_abandon_focus = (adm_abandon_focus_t)
11356 dlsym(adev->adm_lib, "adm_abandon_focus");
Haynes Mathew George5beddd42016-06-27 18:33:40 -070011357 adev->adm_set_config = (adm_set_config_t)
11358 dlsym(adev->adm_lib, "adm_set_config");
11359 adev->adm_request_focus_v2 = (adm_request_focus_v2_t)
11360 dlsym(adev->adm_lib, "adm_request_focus_v2");
11361 adev->adm_is_noirq_avail = (adm_is_noirq_avail_t)
11362 dlsym(adev->adm_lib, "adm_is_noirq_avail");
11363 adev->adm_on_routing_change = (adm_on_routing_change_t)
11364 dlsym(adev->adm_lib, "adm_on_routing_change");
Aniket Kumar Lata60586a92019-05-22 22:18:55 -070011365 adev->adm_request_focus_v2_1 = (adm_request_focus_v2_1_t)
11366 dlsym(adev->adm_lib, "adm_request_focus_v2_1");
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -070011367 }
11368 }
11369
Aalique Grahame22e49102018-12-18 14:23:57 -080011370 adev->enable_voicerx = false;
Mingming Yin514a8bc2014-07-29 15:22:21 -070011371 adev->bt_wb_speech_enabled = false;
Zhou Song12c29502019-03-16 10:37:18 +080011372 adev->swb_speech_mode = SPEECH_MODE_INVALID;
Kunlei Zhanga3c4cb02021-03-05 16:03:54 +080011373 adev->fluence_nn_usecase_id = USECASE_INVALID;
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -080011374 //initialize this to false for now,
11375 //this will be set to true through set param
11376 adev->vr_audio_mode_enabled = false;
Mingming Yin514a8bc2014-07-29 15:22:21 -070011377
Pradnya Chaphekar8a9dcd82014-09-09 09:49:10 -070011378 audio_extn_ds2_enable(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080011379 *device = &adev->device.common;
Aalique Grahame22e49102018-12-18 14:23:57 -080011380
11381 if (k_enable_extended_precision)
11382 adev_verify_devices(adev);
11383
Xiaojun Sang785b5da2017-08-03 15:52:29 +080011384 adev->dsp_bit_width_enforce_mode =
11385 adev_init_dsp_bit_width_enforce_mode(adev->mixer);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080011386
Dhananjay Kumard6d32152016-10-13 16:11:03 +053011387 audio_extn_utils_update_streams_cfg_lists(adev->platform, adev->mixer,
11388 &adev->streams_output_cfg_list,
11389 &adev->streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070011390
Kiran Kandi910e1862013-10-29 13:29:42 -070011391 audio_device_ref_count++;
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -070011392
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -070011393 int trial;
Manisha Agarwalc75a0202019-12-06 18:48:25 +053011394 if (property_get("vendor.audio_hal.period_size", value, NULL) > 0) {
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -070011395 trial = atoi(value);
11396 if (period_size_is_plausible_for_low_latency(trial)) {
11397 pcm_config_low_latency.period_size = trial;
11398 pcm_config_low_latency.start_threshold = trial / 4;
11399 pcm_config_low_latency.avail_min = trial / 4;
11400 configured_low_latency_capture_period_size = trial;
11401 }
11402 }
ronghuiz93177262021-04-21 19:58:13 +080011403 if (property_get("vendor.audio_hal.in_period_size", value, NULL) > 0) {
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -070011404 trial = atoi(value);
11405 if (period_size_is_plausible_for_low_latency(trial)) {
11406 configured_low_latency_capture_period_size = trial;
11407 }
11408 }
11409
Vignesh Kulothungan7d374312018-02-21 17:12:00 -080011410 adev->mic_break_enabled = property_get_bool("vendor.audio.mic_break", false);
11411
Eric Laurent4b084132018-10-19 17:33:43 -070011412 adev->camera_orientation = CAMERA_DEFAULT;
11413
Manisha Agarwalc75a0202019-12-06 18:48:25 +053011414 if (property_get("vendor.audio_hal.period_multiplier",value,NULL) > 0) {
Haynes Mathew George5beddd42016-06-27 18:33:40 -070011415 af_period_multiplier = atoi(value);
11416 if (af_period_multiplier < 0)
11417 af_period_multiplier = 2;
11418 else if (af_period_multiplier > 4)
11419 af_period_multiplier = 4;
11420
11421 ALOGV("new period_multiplier = %d", af_period_multiplier);
11422 }
11423
Arun Mirpurib1bec9c2019-01-29 16:42:45 -080011424 audio_extn_qdsp_init(adev->platform);
Aalique Grahame22e49102018-12-18 14:23:57 -080011425
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -070011426 adev->multi_offload_enable = property_get_bool("vendor.audio.offload.multiple.enabled", false);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070011427 pthread_mutex_unlock(&adev_init_lock);
11428
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -070011429 if (adev->adm_init)
11430 adev->adm_data = adev->adm_init();
11431
Dhananjay Kumaree4d2002016-10-25 18:02:58 +053011432 qahwi_init(*device);
Ben Rombergerd771a7c2017-02-22 18:05:17 -080011433 audio_extn_adsp_hdlr_init(adev->mixer);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053011434
11435 audio_extn_snd_mon_init();
11436 pthread_mutex_lock(&adev->lock);
11437 audio_extn_snd_mon_register_listener(adev, adev_snd_mon_cb);
11438 adev->card_status = CARD_STATUS_ONLINE;
Shubhasini Sugumaran3fde6582022-01-19 15:54:50 -050011439 adev->out_power_policy = POWER_POLICY_STATUS_ONLINE;
11440 adev->in_power_policy = POWER_POLICY_STATUS_ONLINE;
Haynes Mathew George01156f92018-04-13 15:29:54 -070011441 audio_extn_battery_properties_listener_init(adev_on_battery_status_changed);
11442 /*
11443 * if the battery state callback happens before charging can be queried,
11444 * it will be guarded with the adev->lock held in the cb function and so
11445 * the callback value will reflect the latest state
11446 */
11447 adev->is_charging = audio_extn_battery_properties_is_charging();
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053011448 audio_extn_sound_trigger_init(adev); /* dependent on snd_mon_init() */
Zhou Songc1088ea2018-06-12 00:17:29 +080011449 audio_extn_sound_trigger_update_battery_status(adev->is_charging);
justinweng20fb6d82019-02-21 18:49:00 -070011450 audio_extn_audiozoom_init();
Zhou Songc1088ea2018-06-12 00:17:29 +080011451 pthread_mutex_unlock(&adev->lock);
Satish Babu Patakokilac3c5d432017-07-04 22:48:59 +053011452 /* Allocate memory for Device config params */
11453 adev->device_cfg_params = (struct audio_device_config_param*)
11454 calloc(platform_get_max_codec_backend(),
11455 sizeof(struct audio_device_config_param));
11456 if (adev->device_cfg_params == NULL)
11457 ALOGE("%s: Memory allocation failed for Device config params", __func__);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053011458
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +053011459 /*
11460 * Check if new PSPD matrix mixer control is supported. If not
11461 * supported, then set flag so that old mixer ctrl is sent while
11462 * sending pspd coefficients on older kernel version. Query mixer
11463 * control for default pcm id and channel value one.
11464 */
11465 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
11466 "AudStr %d ChMixer Weight Ch %d", 0, 1);
11467
11468 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
11469 if (!ctl) {
11470 ALOGE("%s: ERROR. Could not get ctl for mixer cmd - %s",
11471 __func__, mixer_ctl_name);
11472 adev->use_old_pspd_mix_ctrl = true;
11473 }
11474
Jaideep Sharma0fa53812020-09-17 09:00:11 +053011475 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080011476 return 0;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070011477
11478adev_open_err:
11479 free_map(adev->patch_map);
11480 free_map(adev->io_streams_map);
Aniket Kumar Latabc774842020-01-16 21:22:05 -080011481 free(adev->snd_dev_ref_cnt);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070011482 pthread_mutex_destroy(&adev->lock);
11483 free(adev);
11484 adev = NULL;
11485 *device = NULL;
11486 pthread_mutex_unlock(&adev_init_lock);
11487 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080011488}
11489
11490static struct hw_module_methods_t hal_module_methods = {
11491 .open = adev_open,
11492};
11493
11494struct audio_module HAL_MODULE_INFO_SYM = {
11495 .common = {
11496 .tag = HARDWARE_MODULE_TAG,
11497 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
11498 .hal_api_version = HARDWARE_HAL_API_VERSION,
11499 .id = AUDIO_HARDWARE_MODULE_ID,
11500 .name = "QCOM Audio HAL",
Duy Truongfae19622013-11-24 02:17:54 -080011501 .author = "The Linux Foundation",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080011502 .methods = &hal_module_methods,
11503 },
11504};