blob: 2407cfc91d9e779c275a9eaaf08163f8d0f4baa4 [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.
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080036 */
37
38#define LOG_TAG "audio_hw_primary"
Haynes Mathew George5beddd42016-06-27 18:33:40 -070039#define ATRACE_TAG (ATRACE_TAG_AUDIO|ATRACE_TAG_HAL)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080040/*#define LOG_NDEBUG 0*/
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070041/*#define VERY_VERY_VERBOSE_LOGGING*/
42#ifdef VERY_VERY_VERBOSE_LOGGING
43#define ALOGVV ALOGV
44#else
45#define ALOGVV(a...) do { } while(0)
46#endif
George Gao3018ede2019-10-23 13:23:00 -070047#include <limits.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080048#include <errno.h>
49#include <pthread.h>
50#include <stdint.h>
51#include <sys/time.h>
52#include <stdlib.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080053#include <math.h>
Eric Laurentc4aef752013-09-12 17:45:53 -070054#include <dlfcn.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070055#include <sys/resource.h>
56#include <sys/prctl.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080057
Aalique Grahame22e49102018-12-18 14:23:57 -080058#include <log/log.h>
Haynes Mathew George5beddd42016-06-27 18:33:40 -070059#include <cutils/trace.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080060#include <cutils/str_parms.h>
61#include <cutils/properties.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070062#include <cutils/atomic.h>
63#include <cutils/sched_policy.h>
Eric Laurentb23d5282013-05-14 15:27:20 -070064#include <hardware/audio_effect.h>
Haynes Mathew George484e8d22017-07-31 18:55:17 -070065#include <hardware/audio_alsaops.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070066#include <system/thread_defs.h>
Haynes Mathew George16081042017-05-31 17:16:49 -070067#include <tinyalsa/asoundlib.h>
Andy Hunga1f48fa2019-07-01 18:14:53 -070068#include <utils/Timers.h> // systemTime
Eric Laurentb23d5282013-05-14 15:27:20 -070069#include <audio_effects/effect_aec.h>
70#include <audio_effects/effect_ns.h>
Ashish Jainf1eaa582016-05-23 20:54:24 +053071#include <audio_utils/format.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080072#include "audio_hw.h"
Wei Wangf7ca6c92017-11-21 14:51:20 -080073#include "audio_perf.h"
Eric Laurentb23d5282013-05-14 15:27:20 -070074#include "platform_api.h"
75#include <platform.h>
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -070076#include "audio_extn.h"
Narsinga Rao Chella05573b72013-11-15 15:21:40 -080077#include "voice_extn.h"
Ashish Jaind5694242017-09-05 20:09:06 +053078#include "ip_hdlr_intf.h"
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080079
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070080#include "sound/compress_params.h"
Xiaojun Sang782e5b12020-06-29 21:13:06 +080081
82#ifdef AUDIO_GKI_ENABLED
83#include "sound/audio_compressed_formats.h"
84#endif
85
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -080086#include "sound/asound.h"
ApurupaPattapu2e084df2013-12-18 15:47:59 -080087
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053088#ifdef DYNAMIC_LOG_ENABLED
89#include <log_xml_parser.h>
90#define LOG_MASK HAL_MOD_FILE_AUDIO_HW
91#include <log_utils.h>
92#endif
93
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070094#define COMPRESS_OFFLOAD_NUM_FRAGMENTS 4
Ashish Jain5106d362016-05-11 19:23:33 +053095/*DIRECT PCM has same buffer sizes as DEEP Buffer*/
96#define DIRECT_PCM_NUM_FRAGMENTS 2
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070097#define COMPRESS_PLAYBACK_VOLUME_MAX 0x2000
Vikram Panduranga93f080e2017-06-07 18:16:14 -070098#define VOIP_PLAYBACK_VOLUME_MAX 0x2000
Arun Mirpuri5d170872019-03-26 13:21:31 -070099#define MMAP_PLAYBACK_VOLUME_MAX 0x2000
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +0530100#define PCM_PLAYBACK_VOLUME_MAX 0x2000
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530101#define DSD_VOLUME_MIN_DB (-110)
Aniket Kumar Lata8426d1f2019-06-10 15:25:53 -0700102#define INVALID_OUT_VOLUME -1
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700103#define AUDIO_IO_PORTS_MAX 32
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700104
Zhou Songbaddf9f2020-11-20 13:57:39 +0800105#define PLAYBACK_GAIN_MAX 1.0f
Aalique Grahame22e49102018-12-18 14:23:57 -0800106#define RECORD_GAIN_MIN 0.0f
107#define RECORD_GAIN_MAX 1.0f
108#define RECORD_VOLUME_CTL_MAX 0x2000
109
110/* treat as unsigned Q1.13 */
111#define APP_TYPE_GAIN_DEFAULT 0x2000
112
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -0700113#define PROXY_OPEN_RETRY_COUNT 100
114#define PROXY_OPEN_WAIT_TIME 20
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -0800115
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800116#define GET_USECASE_AUDIO_PLAYBACK_PRIMARY(db) \
117 (db)? USECASE_AUDIO_PLAYBACK_DEEP_BUFFER : \
118 USECASE_AUDIO_PLAYBACK_LOW_LATENCY
119#define GET_PCM_CONFIG_AUDIO_PLAYBACK_PRIMARY(db) \
120 (db)? pcm_config_deep_buffer : pcm_config_low_latency
Haynes Mathew Georgebf143712013-12-03 13:02:53 -0800121
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700122#define ULL_PERIOD_SIZE (DEFAULT_OUTPUT_SAMPLING_RATE/1000)
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700123#define DEFAULT_VOIP_BUF_DURATION_MS 20
124#define DEFAULT_VOIP_BIT_DEPTH_BYTE sizeof(int16_t)
125#define DEFAULT_VOIP_SAMP_RATE 48000
126
127#define VOIP_IO_BUF_SIZE(SR, DURATION_MS, BIT_DEPTH) (SR)/1000 * DURATION_MS * BIT_DEPTH
128
129struct pcm_config default_pcm_config_voip_copp = {
130 .channels = 1,
131 .rate = DEFAULT_VOIP_SAMP_RATE, /* changed when the stream is opened */
132 .period_size = VOIP_IO_BUF_SIZE(DEFAULT_VOIP_SAMP_RATE, DEFAULT_VOIP_BUF_DURATION_MS, DEFAULT_VOIP_BIT_DEPTH_BYTE)/2,
133 .period_count = 2,
134 .format = PCM_FORMAT_S16_LE,
kunleiz95b597a2017-10-23 17:07:33 +0800135 .avail_min = VOIP_IO_BUF_SIZE(DEFAULT_VOIP_SAMP_RATE, DEFAULT_VOIP_BUF_DURATION_MS, DEFAULT_VOIP_BIT_DEPTH_BYTE)/2,
136 .stop_threshold = INT_MAX,
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700137};
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700138
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700139#define MIN_CHANNEL_COUNT 1
140#define DEFAULT_CHANNEL_COUNT 2
141#define MAX_HIFI_CHANNEL_COUNT 8
142
Aalique Grahame22e49102018-12-18 14:23:57 -0800143#ifndef MAX_TARGET_SPECIFIC_CHANNEL_CNT
144#define MAX_CHANNEL_COUNT 1
145#else
146#define MAX_CHANNEL_COUNT atoi(XSTR(MAX_TARGET_SPECIFIC_CHANNEL_CNT))
147#define XSTR(x) STR(x)
148#define STR(x) #x
149#endif
150
Avinash Chandrad7296d42021-08-04 15:07:47 +0530151#define IS_USB_HIFI (MAX_HIFI_CHANNEL_COUNT >= MAX_CHANNEL_COUNT) ? \
152 true : false
153
Dechen Chai22768452021-07-30 09:29:16 +0530154#ifdef LINUX_ENABLED
155static inline int64_t audio_utils_ns_from_timespec(const struct timespec *ts)
156{
157 return ts->tv_sec * 1000000000LL + ts->tv_nsec;
158}
159#endif
160
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -0700161static unsigned int configured_low_latency_capture_period_size =
162 LOW_LATENCY_CAPTURE_PERIOD_SIZE;
163
Haynes Mathew George16081042017-05-31 17:16:49 -0700164#define MMAP_PERIOD_SIZE (DEFAULT_OUTPUT_SAMPLING_RATE/1000)
165#define MMAP_PERIOD_COUNT_MIN 32
166#define MMAP_PERIOD_COUNT_MAX 512
167#define MMAP_PERIOD_COUNT_DEFAULT (MMAP_PERIOD_COUNT_MAX)
168
Aalique Grahame22e49102018-12-18 14:23:57 -0800169/* This constant enables extended precision handling.
170 * TODO The flag is off until more testing is done.
171 */
172static const bool k_enable_extended_precision = false;
Arun Mirpurie008ed22019-03-21 11:21:04 -0700173extern int AUDIO_DEVICE_IN_ALL_CODEC_BACKEND;
Aalique Grahame22e49102018-12-18 14:23:57 -0800174
Eric Laurentb23d5282013-05-14 15:27:20 -0700175struct pcm_config pcm_config_deep_buffer = {
176 .channels = 2,
177 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
178 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
179 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
180 .format = PCM_FORMAT_S16_LE,
181 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
182 .stop_threshold = INT_MAX,
183 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
184};
185
186struct pcm_config pcm_config_low_latency = {
187 .channels = 2,
188 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
189 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
190 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
191 .format = PCM_FORMAT_S16_LE,
192 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
193 .stop_threshold = INT_MAX,
194 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
195};
196
Vignesh Kulothungana6927272019-02-20 15:17:07 -0800197struct pcm_config pcm_config_haptics_audio = {
198 .channels = 1,
199 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
200 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
201 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
202 .format = PCM_FORMAT_S16_LE,
203 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
204 .stop_threshold = INT_MAX,
205 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
206};
207
208struct pcm_config pcm_config_haptics = {
209 .channels = 1,
210 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
211 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
212 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
213 .format = PCM_FORMAT_S16_LE,
214 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
215 .stop_threshold = INT_MAX,
216 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
217};
218
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700219static int af_period_multiplier = 4;
220struct pcm_config pcm_config_rt = {
221 .channels = 2,
222 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
223 .period_size = ULL_PERIOD_SIZE, //1 ms
224 .period_count = 512, //=> buffer size is 512ms
225 .format = PCM_FORMAT_S16_LE,
226 .start_threshold = ULL_PERIOD_SIZE*8, //8ms
227 .stop_threshold = INT_MAX,
228 .silence_threshold = 0,
229 .silence_size = 0,
230 .avail_min = ULL_PERIOD_SIZE, //1 ms
231};
232
Eric Laurentb23d5282013-05-14 15:27:20 -0700233struct pcm_config pcm_config_hdmi_multi = {
234 .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
235 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
236 .period_size = HDMI_MULTI_PERIOD_SIZE,
237 .period_count = HDMI_MULTI_PERIOD_COUNT,
238 .format = PCM_FORMAT_S16_LE,
239 .start_threshold = 0,
240 .stop_threshold = INT_MAX,
241 .avail_min = 0,
242};
243
Haynes Mathew George16081042017-05-31 17:16:49 -0700244struct pcm_config pcm_config_mmap_playback = {
245 .channels = 2,
246 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
247 .period_size = MMAP_PERIOD_SIZE,
248 .period_count = MMAP_PERIOD_COUNT_DEFAULT,
249 .format = PCM_FORMAT_S16_LE,
250 .start_threshold = MMAP_PERIOD_SIZE*8,
251 .stop_threshold = INT32_MAX,
252 .silence_threshold = 0,
253 .silence_size = 0,
254 .avail_min = MMAP_PERIOD_SIZE, //1 ms
255};
256
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700257struct pcm_config pcm_config_hifi = {
258 .channels = DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
259 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
260 .period_size = HIFI_BUFFER_OUTPUT_PERIOD_SIZE, /* change #define */
261 .period_count = HIFI_BUFFER_OUTPUT_PERIOD_COUNT,
262 .format = PCM_FORMAT_S24_3LE,
263 .start_threshold = 0,
264 .stop_threshold = INT_MAX,
265 .avail_min = 0,
266};
267
Eric Laurentb23d5282013-05-14 15:27:20 -0700268struct pcm_config pcm_config_audio_capture = {
269 .channels = 2,
Eric Laurentb23d5282013-05-14 15:27:20 -0700270 .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
271 .format = PCM_FORMAT_S16_LE,
272};
273
Haynes Mathew George16081042017-05-31 17:16:49 -0700274struct pcm_config pcm_config_mmap_capture = {
275 .channels = 2,
276 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
277 .period_size = MMAP_PERIOD_SIZE,
278 .period_count = MMAP_PERIOD_COUNT_DEFAULT,
279 .format = PCM_FORMAT_S16_LE,
280 .start_threshold = 0,
281 .stop_threshold = INT_MAX,
282 .silence_threshold = 0,
283 .silence_size = 0,
284 .avail_min = MMAP_PERIOD_SIZE, //1 ms
285};
286
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -0700287#define AFE_PROXY_CHANNEL_COUNT 2
288#define AFE_PROXY_SAMPLING_RATE 48000
289
290#define AFE_PROXY_PLAYBACK_PERIOD_SIZE 768
291#define AFE_PROXY_PLAYBACK_PERIOD_COUNT 4
292
293struct pcm_config pcm_config_afe_proxy_playback = {
294 .channels = AFE_PROXY_CHANNEL_COUNT,
295 .rate = AFE_PROXY_SAMPLING_RATE,
296 .period_size = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
297 .period_count = AFE_PROXY_PLAYBACK_PERIOD_COUNT,
298 .format = PCM_FORMAT_S16_LE,
299 .start_threshold = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
300 .stop_threshold = INT_MAX,
301 .avail_min = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
302};
303
304#define AFE_PROXY_RECORD_PERIOD_SIZE 768
305#define AFE_PROXY_RECORD_PERIOD_COUNT 4
306
Aalique Grahame22e49102018-12-18 14:23:57 -0800307struct pcm_config pcm_config_audio_capture_rt = {
308 .channels = 2,
309 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
310 .period_size = ULL_PERIOD_SIZE,
311 .period_count = 512,
312 .format = PCM_FORMAT_S16_LE,
313 .start_threshold = 0,
314 .stop_threshold = AFE_PROXY_RECORD_PERIOD_SIZE * AFE_PROXY_RECORD_PERIOD_COUNT,
315 .silence_threshold = 0,
316 .silence_size = 0,
317 .avail_min = ULL_PERIOD_SIZE, //1 ms
318};
319
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +0530320struct pcm_config pcm_config_audio_capture_rt_48KHz = {
321 .channels = 2,
322 .rate = 48000,
323 .period_size = 48,
324 .period_count = 512,
325 .format = PCM_FORMAT_S16_LE,
326 .start_threshold = 0,
327 .stop_threshold = AFE_PROXY_RECORD_PERIOD_SIZE * AFE_PROXY_RECORD_PERIOD_COUNT,
328 .silence_threshold = 0,
329 .silence_size = 0,
330 .avail_min = 48, //1 ms
331};
332struct pcm_config pcm_config_audio_capture_rt_32KHz = {
333 .channels = 2,
334 .rate = 32000,
335 .period_size = 32,
336 .period_count = 512,
337 .format = PCM_FORMAT_S16_LE,
338 .start_threshold = 0,
339 .stop_threshold = AFE_PROXY_RECORD_PERIOD_SIZE * AFE_PROXY_RECORD_PERIOD_COUNT,
340 .silence_threshold = 0,
341 .silence_size = 0,
342 .avail_min = 32, //1 ms
343};
344struct pcm_config pcm_config_audio_capture_rt_24KHz = {
345 .channels = 2,
346 .rate = 24000,
347 .period_size = 24,
348 .period_count = 512,
349 .format = PCM_FORMAT_S16_LE,
350 .start_threshold = 0,
351 .stop_threshold = AFE_PROXY_RECORD_PERIOD_SIZE * AFE_PROXY_RECORD_PERIOD_COUNT,
352 .silence_threshold = 0,
353 .silence_size = 0,
354 .avail_min = 24, //1 ms
355};
356struct pcm_config pcm_config_audio_capture_rt_16KHz = {
357 .channels = 2,
358 .rate = 16000,
359 .period_size = 16,
360 .period_count = 512,
361 .format = PCM_FORMAT_S16_LE,
362 .start_threshold = 0,
363 .stop_threshold = AFE_PROXY_RECORD_PERIOD_SIZE * AFE_PROXY_RECORD_PERIOD_COUNT,
364 .silence_threshold = 0,
365 .silence_size = 0,
366 .avail_min = 16, //1 ms
367};
368struct pcm_config pcm_config_audio_capture_rt_8KHz = {
369 .channels = 2,
370 .rate = 8000,
371 .period_size = 8,
372 .period_count = 512,
373 .format = PCM_FORMAT_S16_LE,
374 .start_threshold = 0,
375 .stop_threshold = AFE_PROXY_RECORD_PERIOD_SIZE * AFE_PROXY_RECORD_PERIOD_COUNT,
376 .silence_threshold = 0,
377 .silence_size = 0,
378 .avail_min = 8, //1 ms
379};
380
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -0700381struct pcm_config pcm_config_afe_proxy_record = {
382 .channels = AFE_PROXY_CHANNEL_COUNT,
383 .rate = AFE_PROXY_SAMPLING_RATE,
384 .period_size = AFE_PROXY_RECORD_PERIOD_SIZE,
385 .period_count = AFE_PROXY_RECORD_PERIOD_COUNT,
386 .format = PCM_FORMAT_S16_LE,
387 .start_threshold = AFE_PROXY_RECORD_PERIOD_SIZE,
388 .stop_threshold = INT_MAX,
389 .avail_min = AFE_PROXY_RECORD_PERIOD_SIZE,
390};
391
Ashish Jainf1eaa582016-05-23 20:54:24 +0530392#define AUDIO_MAX_PCM_FORMATS 7
393
394const uint32_t format_to_bitwidth_table[AUDIO_MAX_PCM_FORMATS] = {
395 [AUDIO_FORMAT_DEFAULT] = 0,
396 [AUDIO_FORMAT_PCM_16_BIT] = sizeof(uint16_t),
397 [AUDIO_FORMAT_PCM_8_BIT] = sizeof(uint8_t),
398 [AUDIO_FORMAT_PCM_32_BIT] = sizeof(uint32_t),
399 [AUDIO_FORMAT_PCM_8_24_BIT] = sizeof(uint32_t),
400 [AUDIO_FORMAT_PCM_FLOAT] = sizeof(float),
401 [AUDIO_FORMAT_PCM_24_BIT_PACKED] = sizeof(uint8_t) * 3,
402};
403
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800404const char * const use_case_table[AUDIO_USECASE_MAX] = {
Eric Laurentb23d5282013-05-14 15:27:20 -0700405 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
406 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
Vignesh Kulothungana6927272019-02-20 15:17:07 -0800407 [USECASE_AUDIO_PLAYBACK_WITH_HAPTICS] = "audio-with-haptics-playback",
Meng Wang51d8c2a2020-04-27 15:23:21 +0800408 [USECASE_AUDIO_PLAYBACK_HAPTICS] = "haptics-playback",
Ravi Kumar Alamanda474de5a2015-06-25 20:08:01 -0700409 [USECASE_AUDIO_PLAYBACK_ULL] = "audio-ull-playback",
410 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback",
Shruthi Krishnaace10852013-10-25 14:32:12 -0700411 [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback",
vivek mehta446c3962015-09-14 10:57:35 -0700412 //Enabled for Direct_PCM
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700413 [USECASE_AUDIO_PLAYBACK_OFFLOAD2] = "compress-offload-playback2",
414 [USECASE_AUDIO_PLAYBACK_OFFLOAD3] = "compress-offload-playback3",
415 [USECASE_AUDIO_PLAYBACK_OFFLOAD4] = "compress-offload-playback4",
416 [USECASE_AUDIO_PLAYBACK_OFFLOAD5] = "compress-offload-playback5",
417 [USECASE_AUDIO_PLAYBACK_OFFLOAD6] = "compress-offload-playback6",
418 [USECASE_AUDIO_PLAYBACK_OFFLOAD7] = "compress-offload-playback7",
419 [USECASE_AUDIO_PLAYBACK_OFFLOAD8] = "compress-offload-playback8",
420 [USECASE_AUDIO_PLAYBACK_OFFLOAD9] = "compress-offload-playback9",
Haynes Mathew George16081042017-05-31 17:16:49 -0700421 [USECASE_AUDIO_PLAYBACK_FM] = "play-fm",
422 [USECASE_AUDIO_PLAYBACK_MMAP] = "mmap-playback",
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700423 [USECASE_AUDIO_PLAYBACK_HIFI] = "hifi-playback",
Aalique Grahame22e49102018-12-18 14:23:57 -0800424 [USECASE_AUDIO_PLAYBACK_TTS] = "audio-tts-playback",
vivek mehta0ea887a2015-08-26 14:01:20 -0700425
Eric Laurentb23d5282013-05-14 15:27:20 -0700426 [USECASE_AUDIO_RECORD] = "audio-record",
Krishna Kishor Jha0f4d8482022-02-18 10:56:05 +0530427 [USECASE_AUDIO_RECORD2] = "audio-record2",
428 [USECASE_AUDIO_RECORD3] = "audio-record3",
Mingming Yine62d7842013-10-25 16:26:03 -0700429 [USECASE_AUDIO_RECORD_COMPRESS] = "audio-record-compress",
Dhananjay Kumaree4d2002016-10-25 18:02:58 +0530430 [USECASE_AUDIO_RECORD_COMPRESS2] = "audio-record-compress2",
431 [USECASE_AUDIO_RECORD_COMPRESS3] = "audio-record-compress3",
432 [USECASE_AUDIO_RECORD_COMPRESS4] = "audio-record-compress4",
Dhananjay Kumar376e38b2017-09-28 22:26:23 +0530433 [USECASE_AUDIO_RECORD_COMPRESS5] = "audio-record-compress5",
434 [USECASE_AUDIO_RECORD_COMPRESS6] = "audio-record-compress6",
Eric Laurentb23d5282013-05-14 15:27:20 -0700435 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
Preetam Singh Ranawatde84f1a2013-11-01 14:58:16 -0700436 [USECASE_AUDIO_RECORD_FM_VIRTUAL] = "fm-virtual-record",
Haynes Mathew George16081042017-05-31 17:16:49 -0700437 [USECASE_AUDIO_RECORD_MMAP] = "mmap-record",
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700438 [USECASE_AUDIO_RECORD_HIFI] = "hifi-record",
Haynes Mathew George16081042017-05-31 17:16:49 -0700439
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -0800440 [USECASE_AUDIO_HFP_SCO] = "hfp-sco",
Vimal Puthanveed47e64852013-12-20 13:23:39 -0800441 [USECASE_AUDIO_HFP_SCO_WB] = "hfp-sco-wb",
Derek Chenf7092792017-05-23 12:23:53 -0400442 [USECASE_AUDIO_HFP_SCO_DOWNLINK] = "hfp-sco-downlink",
443 [USECASE_AUDIO_HFP_SCO_WB_DOWNLINK] = "hfp-sco-wb-downlink",
Mingming Yin3ee55c62014-08-04 14:23:35 -0700444
Derek Chenf7092792017-05-23 12:23:53 -0400445 [USECASE_VOICE_CALL] = "voice-call",
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700446 [USECASE_VOICE2_CALL] = "voice2-call",
447 [USECASE_VOLTE_CALL] = "volte-call",
448 [USECASE_QCHAT_CALL] = "qchat-call",
Vicky Sehrawat7e4fc152014-02-12 17:58:59 -0800449 [USECASE_VOWLAN_CALL] = "vowlan-call",
Vidyakumar Athota0e109352015-02-12 17:38:22 -0800450 [USECASE_VOICEMMODE1_CALL] = "voicemmode1-call",
451 [USECASE_VOICEMMODE2_CALL] = "voicemmode2-call",
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800452 [USECASE_COMPRESS_VOIP_CALL] = "compress-voip-call",
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700453 [USECASE_INCALL_REC_UPLINK] = "incall-rec-uplink",
454 [USECASE_INCALL_REC_DOWNLINK] = "incall-rec-downlink",
455 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK] = "incall-rec-uplink-and-downlink",
Helen Zenge56b4852013-12-03 16:54:40 -0800456 [USECASE_INCALL_REC_UPLINK_COMPRESS] = "incall-rec-uplink-compress",
457 [USECASE_INCALL_REC_DOWNLINK_COMPRESS] = "incall-rec-downlink-compress",
458 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK_COMPRESS] = "incall-rec-uplink-and-downlink-compress",
459
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -0700460 [USECASE_INCALL_MUSIC_UPLINK] = "incall_music_uplink",
461 [USECASE_INCALL_MUSIC_UPLINK2] = "incall_music_uplink2",
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700462 [USECASE_AUDIO_SPKR_CALIB_RX] = "spkr-rx-calib",
463 [USECASE_AUDIO_SPKR_CALIB_TX] = "spkr-vi-record",
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -0700464
465 [USECASE_AUDIO_PLAYBACK_AFE_PROXY] = "afe-proxy-playback",
466 [USECASE_AUDIO_RECORD_AFE_PROXY] = "afe-proxy-record",
Zhou Song62ea0282020-03-22 19:53:01 +0800467 [USECASE_AUDIO_RECORD_AFE_PROXY2] = "afe-proxy-record2",
Md Mansoor Ahmeddb1b4f92018-01-25 18:56:31 +0530468 [USECASE_AUDIO_PLAYBACK_SILENCE] = "silence-playback",
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700469
Siddartha Shaik31b530e2017-05-19 15:26:33 +0530470 /* Transcode loopback cases */
Surendar Karka93cd25a2018-08-28 14:21:37 +0530471 [USECASE_AUDIO_TRANSCODE_LOOPBACK_RX] = "audio-transcode-loopback-rx",
472 [USECASE_AUDIO_TRANSCODE_LOOPBACK_TX] = "audio-transcode-loopback-tx",
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700473
474 [USECASE_AUDIO_PLAYBACK_VOIP] = "audio-playback-voip",
475 [USECASE_AUDIO_RECORD_VOIP] = "audio-record-voip",
Revathi Uddarajud9f23d92020-07-27 10:55:06 +0530476 [USECASE_AUDIO_RECORD_VOIP_LOW_LATENCY] = "audio-record-voip-low-latency",
Varun Balaraje49253e2017-07-06 19:48:56 +0530477 /* For Interactive Audio Streams */
478 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM1] = "audio-interactive-stream1",
479 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM2] = "audio-interactive-stream2",
480 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM3] = "audio-interactive-stream3",
481 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM4] = "audio-interactive-stream4",
482 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM5] = "audio-interactive-stream5",
483 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM6] = "audio-interactive-stream6",
484 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM7] = "audio-interactive-stream7",
485 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM8] = "audio-interactive-stream8",
Garmond Leunge2433c32017-09-28 21:51:22 -0700486
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800487 [USECASE_AUDIO_EC_REF_LOOPBACK] = "ec-ref-audio-capture",
488
Derek Chenf6318be2017-06-12 17:16:24 -0400489 [USECASE_AUDIO_A2DP_ABR_FEEDBACK] = "a2dp-abr-feedback",
490
491 [USECASE_AUDIO_PLAYBACK_MEDIA] = "media-playback",
492 [USECASE_AUDIO_PLAYBACK_SYS_NOTIFICATION] = "sys-notification-playback",
493 [USECASE_AUDIO_PLAYBACK_NAV_GUIDANCE] = "nav-guidance-playback",
494 [USECASE_AUDIO_PLAYBACK_PHONE] = "phone-playback",
Susan Wang117cf6f2022-04-06 20:11:46 -0400495 [USECASE_AUDIO_PLAYBACK_ALERTS] = "alerts-playback",
Derek Chen1bcdc6b2020-02-06 22:44:53 -0800496 [USECASE_AUDIO_PLAYBACK_FRONT_PASSENGER] = "front-passenger-playback",
Derek Chendf05eea2019-08-01 13:57:49 -0700497 [USECASE_AUDIO_PLAYBACK_REAR_SEAT] = "rear-seat-playback",
Rahul Sharma99770982019-03-06 17:05:26 +0530498 [USECASE_AUDIO_FM_TUNER_EXT] = "fm-tuner-ext",
Derek Chena30a5f42019-12-03 11:17:09 -0500499 [USECASE_ICC_CALL] = "icc-call",
Huicheng Liu1404ba12020-09-11 01:03:25 -0400500
501 [USECASE_AUDIO_RECORD_BUS] = "audio-record",
502 [USECASE_AUDIO_RECORD_BUS_FRONT_PASSENGER] = "front-passenger-record",
503 [USECASE_AUDIO_RECORD_BUS_REAR_SEAT] = "rear-seat-record",
Fei Tongaffdf732020-02-20 20:39:05 +0800504 [USECASE_AUDIO_PLAYBACK_SYNTHESIZER] = "synth-loopback",
Susan Wange3959562021-03-11 11:50:26 -0500505 [USECASE_AUDIO_RECORD_ECHO_REF_EXT] = "echo-reference-external",
Eric Laurentb23d5282013-05-14 15:27:20 -0700506};
507
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700508static const audio_usecase_t offload_usecases[] = {
509 USECASE_AUDIO_PLAYBACK_OFFLOAD,
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700510 USECASE_AUDIO_PLAYBACK_OFFLOAD2,
511 USECASE_AUDIO_PLAYBACK_OFFLOAD3,
512 USECASE_AUDIO_PLAYBACK_OFFLOAD4,
513 USECASE_AUDIO_PLAYBACK_OFFLOAD5,
514 USECASE_AUDIO_PLAYBACK_OFFLOAD6,
515 USECASE_AUDIO_PLAYBACK_OFFLOAD7,
516 USECASE_AUDIO_PLAYBACK_OFFLOAD8,
517 USECASE_AUDIO_PLAYBACK_OFFLOAD9,
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700518};
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800519
Varun Balaraje49253e2017-07-06 19:48:56 +0530520static const audio_usecase_t interactive_usecases[] = {
521 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM1,
522 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM2,
523 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM3,
524 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM4,
525 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM5,
526 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM6,
527 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM7,
528 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM8,
529};
530
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800531#define STRING_TO_ENUM(string) { #string, string }
532
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800533struct string_to_enum {
534 const char *name;
535 uint32_t value;
536};
537
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700538static const struct string_to_enum channels_name_to_enum_table[] = {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800539 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
Mingming Yin3a941d42016-02-17 18:08:05 -0800540 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_2POINT1),
541 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_QUAD),
542 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_SURROUND),
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -0700543 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_PENTA),
Mingming Yin3a941d42016-02-17 18:08:05 -0800544 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
545 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_6POINT1),
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800546 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700547 STRING_TO_ENUM(AUDIO_CHANNEL_IN_MONO),
548 STRING_TO_ENUM(AUDIO_CHANNEL_IN_STEREO),
549 STRING_TO_ENUM(AUDIO_CHANNEL_IN_FRONT_BACK),
550 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_1),
551 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_2),
552 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_3),
553 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_4),
554 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_5),
555 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_6),
556 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_7),
557 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_8),
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800558};
559
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700560static const struct string_to_enum formats_name_to_enum_table[] = {
561 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
562 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
563 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Pradnya Chaphekar8a9dcd82014-09-09 09:49:10 -0700564 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
565 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
566 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Ben Romberger1aaaf862017-04-06 17:49:46 -0700567 STRING_TO_ENUM(AUDIO_FORMAT_DOLBY_TRUEHD),
Mingming Yin3a941d42016-02-17 18:08:05 -0800568 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
569 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
Naresh Tanniru928f0862017-04-07 16:44:23 -0700570 STRING_TO_ENUM(AUDIO_FORMAT_IEC61937)
Mingming Yin3a941d42016-02-17 18:08:05 -0800571};
572
573//list of all supported sample rates by HDMI specification.
574static const int out_hdmi_sample_rates[] = {
575 32000, 44100, 48000, 88200, 96000, 176400, 192000,
576};
577
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700578static const struct string_to_enum out_sample_rates_name_to_enum_table[] = {
Mingming Yin3a941d42016-02-17 18:08:05 -0800579 STRING_TO_ENUM(32000),
580 STRING_TO_ENUM(44100),
581 STRING_TO_ENUM(48000),
582 STRING_TO_ENUM(88200),
583 STRING_TO_ENUM(96000),
584 STRING_TO_ENUM(176400),
585 STRING_TO_ENUM(192000),
Mingshu pange8aedf12019-10-25 15:38:27 +0800586 STRING_TO_ENUM(352800),
587 STRING_TO_ENUM(384000),
Pradnya Chaphekar8a9dcd82014-09-09 09:49:10 -0700588};
589
Carter Hsu2e429db2019-05-14 18:50:52 +0800590struct in_effect_list {
591 struct listnode list;
592 effect_handle_t handle;
593};
594
Krishna Kishor Jha0f4d8482022-02-18 10:56:05 +0530595static const audio_usecase_t record_usecases[] = {
596 USECASE_AUDIO_RECORD,
597 USECASE_AUDIO_RECORD2,
598 USECASE_AUDIO_RECORD3,
599};
600
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700601static struct audio_device *adev = NULL;
Ben Romberger02ab1192018-05-24 12:10:08 -0700602static pthread_mutex_t adev_init_lock = PTHREAD_MUTEX_INITIALIZER;
Kiran Kandi910e1862013-10-29 13:29:42 -0700603static unsigned int audio_device_ref_count;
vivek mehtab72d08d2016-04-29 03:16:47 -0700604//cache last MBDRC cal step level
605static int last_known_cal_step = -1 ;
Kiran Kandi910e1862013-10-29 13:29:42 -0700606
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +0530607static int out_set_compr_volume(struct audio_stream_out *stream, float left, float right);
Arun Mirpuri5d170872019-03-26 13:21:31 -0700608static int out_set_mmap_volume(struct audio_stream_out *stream, float left, float right);
Zhou Song2b8f28f2017-09-11 10:51:38 +0800609static int out_set_voip_volume(struct audio_stream_out *stream, float left, float right);
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +0530610static int out_set_pcm_volume(struct audio_stream_out *stream, float left, float right);
Krishna Kishor Jha10ad39f2022-02-21 17:02:47 +0530611#ifdef SOFT_VOLUME
612static int out_set_soft_volume_params(struct audio_stream_out *stream);
613#endif
Derek Chen6f293672019-04-01 01:40:24 -0700614static void adev_snd_mon_cb(void *cookie, struct str_parms *parms);
615static void in_snd_mon_cb(void * stream, struct str_parms * parms);
616static void out_snd_mon_cb(void * stream, struct str_parms * parms);
617
Zhou Song331c8e52019-08-26 14:16:12 +0800618static int configure_btsco_sample_rate(snd_device_t snd_device);
619
Vatsal Buchac09ae062018-11-14 13:25:08 +0530620#ifdef AUDIO_FEATURE_ENABLED_GCOV
621extern void __gcov_flush();
622static void enable_gcov()
623{
624 __gcov_flush();
625}
626#else
627static void enable_gcov()
628{
629}
630#endif
631
justinweng20fb6d82019-02-21 18:49:00 -0700632static int in_set_microphone_direction(const struct audio_stream_in *stream,
633 audio_microphone_direction_t dir);
634static int in_set_microphone_field_dimension(const struct audio_stream_in *stream, float zoom);
635
Krishna Kishor Jha0f4d8482022-02-18 10:56:05 +0530636static bool is_pcm_record_usecase(audio_usecase_t uc_id)
637{
638 unsigned int record_uc_index;
639 unsigned int num_usecase = sizeof(record_usecases)/sizeof(record_usecases[0]);
640
641 for (record_uc_index = 0; record_uc_index < num_usecase; record_uc_index++) {
642 if (uc_id == record_usecases[record_uc_index])
643 return true;
644 }
645 return false;
646}
647
648static audio_usecase_t get_record_usecase(struct audio_device *adev)
649{
650 audio_usecase_t ret_uc = USECASE_INVALID;
651 unsigned int record_uc_index;
652 unsigned int num_usecase = sizeof(record_usecases)/sizeof(record_usecases[0]);
653
654 ALOGV("%s: num_usecase: %d", __func__, num_usecase);
655 for (record_uc_index = 0; record_uc_index < num_usecase; record_uc_index++) {
656 if (!(adev->pcm_record_uc_state & (0x1 << record_uc_index))) {
657 adev->pcm_record_uc_state |= 0x1 << record_uc_index;
658 ret_uc = record_usecases[record_uc_index];
659 break;
660 }
661 }
662
663 ALOGV("%s: pcm record usecase is %d", __func__, ret_uc);
664 return ret_uc;
665}
666
667static void free_record_usecase(struct audio_device *adev,
668 audio_usecase_t uc_id)
669{
670 unsigned int record_uc_index;
671 unsigned int num_usecase = sizeof(record_usecases)/sizeof(record_usecases[0]);
672
673 for (record_uc_index = 0; record_uc_index < num_usecase; record_uc_index++) {
674 if (record_usecases[record_uc_index] == uc_id) {
675 adev->pcm_record_uc_state &= ~(0x1 << record_uc_index);
676 break;
677 }
678 }
679 ALOGV("%s: free pcm record usecase %d", __func__, uc_id);
680}
681
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700682static bool may_use_noirq_mode(struct audio_device *adev, audio_usecase_t uc_id,
683 int flags __unused)
684{
685 int dir = 0;
686 switch (uc_id) {
687 case USECASE_AUDIO_RECORD_LOW_LATENCY:
Revathi Uddarajud9f23d92020-07-27 10:55:06 +0530688 case USECASE_AUDIO_RECORD_VOIP_LOW_LATENCY:
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700689 dir = 1;
690 case USECASE_AUDIO_PLAYBACK_ULL:
691 break;
692 default:
693 return false;
694 }
695
696 int dev_id = platform_get_pcm_device_id(uc_id, dir == 0 ?
697 PCM_PLAYBACK : PCM_CAPTURE);
698 if (adev->adm_is_noirq_avail)
699 return adev->adm_is_noirq_avail(adev->adm_data,
700 adev->snd_card, dev_id, dir);
701 return false;
702}
703
704static void register_out_stream(struct stream_out *out)
705{
706 struct audio_device *adev = out->dev;
707 if (is_offload_usecase(out->usecase) ||
708 !adev->adm_register_output_stream)
709 return;
710
711 // register stream first for backward compatibility
712 adev->adm_register_output_stream(adev->adm_data,
713 out->handle,
714 out->flags);
715
716 if (!adev->adm_set_config)
717 return;
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +0530718#ifdef PLATFORM_AUTO
Kogara Naveen Kumar6db5fb02022-05-07 00:22:50 +0530719 if (out->realtime || (out->flags & AUDIO_OUTPUT_FLAG_SYS_NOTIFICATION))
720 adev->adm_set_config(adev->adm_data,
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700721 out->handle,
722 out->pcm, &out->config);
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +0530723#else
724 if (out->realtime)
725 adev->adm_set_config(adev->adm_data,
726 out->handle,
727 out->pcm, &out->config);
728#endif
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700729}
730
731static void register_in_stream(struct stream_in *in)
732{
733 struct audio_device *adev = in->dev;
734 if (!adev->adm_register_input_stream)
735 return;
736
737 adev->adm_register_input_stream(adev->adm_data,
738 in->capture_handle,
739 in->flags);
740
741 if (!adev->adm_set_config)
742 return;
743
744 if (in->realtime)
745 adev->adm_set_config(adev->adm_data,
746 in->capture_handle,
747 in->pcm,
748 &in->config);
749}
750
751static void request_out_focus(struct stream_out *out, long ns)
752{
753 struct audio_device *adev = out->dev;
754
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700755 if (adev->adm_request_focus_v2)
756 adev->adm_request_focus_v2(adev->adm_data, out->handle, ns);
757 else if (adev->adm_request_focus)
758 adev->adm_request_focus(adev->adm_data, out->handle);
759}
760
Aniket Kumar Lata60586a92019-05-22 22:18:55 -0700761static int request_in_focus(struct stream_in *in, long ns)
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700762{
763 struct audio_device *adev = in->dev;
Aniket Kumar Lata60586a92019-05-22 22:18:55 -0700764 int ret = 0;
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700765
Aniket Kumar Lata60586a92019-05-22 22:18:55 -0700766 if (adev->adm_request_focus_v2_1)
767 ret = adev->adm_request_focus_v2_1(adev->adm_data, in->capture_handle, ns);
768 else if (adev->adm_request_focus_v2)
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700769 adev->adm_request_focus_v2(adev->adm_data, in->capture_handle, ns);
770 else if (adev->adm_request_focus)
771 adev->adm_request_focus(adev->adm_data, in->capture_handle);
Aniket Kumar Lata60586a92019-05-22 22:18:55 -0700772
773 return ret;
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700774}
775
776static void release_out_focus(struct stream_out *out)
777{
778 struct audio_device *adev = out->dev;
779
780 if (adev->adm_abandon_focus)
781 adev->adm_abandon_focus(adev->adm_data, out->handle);
782}
783
784static void release_in_focus(struct stream_in *in)
785{
786 struct audio_device *adev = in->dev;
787 if (adev->adm_abandon_focus)
788 adev->adm_abandon_focus(adev->adm_data, in->capture_handle);
789}
790
Dhananjay Kumare6293dd2017-05-25 17:25:30 +0530791static int parse_snd_card_status(struct str_parms *parms, int *card,
792 card_status_t *status)
793{
794 char value[32]={0};
795 char state[32]={0};
796
797 int ret = str_parms_get_str(parms, "SND_CARD_STATUS", value, sizeof(value));
798 if (ret < 0)
799 return -1;
800
801 // sscanf should be okay as value is of max length 32.
802 // same as sizeof state.
803 if (sscanf(value, "%d,%s", card, state) < 2)
804 return -1;
805
806 *status = !strcmp(state, "ONLINE") ? CARD_STATUS_ONLINE :
807 CARD_STATUS_OFFLINE;
808 return 0;
809}
810
Avinash Chandrad7296d42021-08-04 15:07:47 +0530811bool is_combo_audio_input_device(struct listnode *devices){
812
Sandhya Mutha Naga Venkata153d95e2022-07-12 14:54:43 +0530813 if ((devices == NULL) || (!list_empty(devices)))
Avinash Chandrad7296d42021-08-04 15:07:47 +0530814 return false;
815
816 if(compare_device_type(devices, AUDIO_DEVICE_IN_BUILTIN_MIC|AUDIO_DEVICE_IN_SPEAKER_MIC2))
817 return true;
818 else
819 return false;
820}
821
Haynes Mathew Georgeb0f5dc32017-10-06 18:35:12 -0700822static inline void adjust_frames_for_device_delay(struct stream_out *out,
823 uint32_t *dsp_frames) {
824 // Adjustment accounts for A2dp encoder latency with offload usecases
825 // Note: Encoder latency is returned in ms.
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800826 if (is_a2dp_out_device_type(&out->device_list)) {
Haynes Mathew Georgeb0f5dc32017-10-06 18:35:12 -0700827 unsigned long offset =
828 (audio_extn_a2dp_get_encoder_latency() * out->sample_rate / 1000);
829 *dsp_frames = (*dsp_frames > offset) ? (*dsp_frames - offset) : 0;
830 }
831}
832
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700833static inline bool free_entry(void *key __unused,
834 void *value, void *context __unused)
835{
836 free(value);
837 return true;
838}
839
840static inline void free_map(Hashmap *map)
841{
842 if (map) {
843 hashmapForEach(map, free_entry, (void *) NULL);
844 hashmapFree(map);
845 }
846}
847
Aniket Kumar Latabc774842020-01-16 21:22:05 -0800848static inline void patch_map_remove_l(struct audio_device *adev,
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700849 audio_patch_handle_t patch_handle)
850{
851 if (patch_handle == AUDIO_PATCH_HANDLE_NONE)
852 return;
853
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700854 struct audio_patch_info *p_info =
855 hashmapGet(adev->patch_map, (void *) (intptr_t) patch_handle);
856 if (p_info) {
857 ALOGV("%s: Remove patch %d", __func__, patch_handle);
858 hashmapRemove(adev->patch_map, (void *) (intptr_t) patch_handle);
859 free(p_info->patch);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700860 free(p_info);
861 }
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700862}
863
864static inline int io_streams_map_insert(struct audio_device *adev,
865 struct audio_stream *stream,
866 audio_io_handle_t handle,
867 audio_patch_handle_t patch_handle)
868{
869 struct audio_stream_info *s_info =
870 (struct audio_stream_info *) calloc(1, sizeof(struct audio_stream_info));
871
872 if (s_info == NULL) {
873 ALOGE("%s: Could not allocate stream info", __func__);
874 return -ENOMEM;
875 }
876 s_info->stream = stream;
877 s_info->patch_handle = patch_handle;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700878
879 pthread_mutex_lock(&adev->lock);
880 struct audio_stream_info *stream_info =
881 hashmapPut(adev->io_streams_map, (void *) (intptr_t) handle, (void *) s_info);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700882 if (stream_info != NULL)
883 free(stream_info);
Aniket Kumar Latabc774842020-01-16 21:22:05 -0800884 pthread_mutex_unlock(&adev->lock);
Aniket Kumar Latad13758f2020-08-06 15:11:36 -0700885 ALOGV("%s: Added stream in io_streams_map with handle %d", __func__, handle);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700886 return 0;
887}
888
889static inline void io_streams_map_remove(struct audio_device *adev,
890 audio_io_handle_t handle)
891{
892 pthread_mutex_lock(&adev->lock);
893 struct audio_stream_info *s_info =
894 hashmapRemove(adev->io_streams_map, (void *) (intptr_t) handle);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700895 if (s_info == NULL)
Aniket Kumar Latabc774842020-01-16 21:22:05 -0800896 goto done;
Aniket Kumar Latad13758f2020-08-06 15:11:36 -0700897 ALOGV("%s: Removed stream with handle %d", __func__, handle);
Aniket Kumar Latabc774842020-01-16 21:22:05 -0800898 patch_map_remove_l(adev, s_info->patch_handle);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700899 free(s_info);
Aniket Kumar Latabc774842020-01-16 21:22:05 -0800900done:
901 pthread_mutex_unlock(&adev->lock);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700902 return;
903}
904
Aniket Kumar Latabc774842020-01-16 21:22:05 -0800905static struct audio_patch_info* fetch_patch_info_l(struct audio_device *adev,
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700906 audio_patch_handle_t handle)
907{
908 struct audio_patch_info *p_info = NULL;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700909 p_info = (struct audio_patch_info *)
910 hashmapGet(adev->patch_map, (void *) (intptr_t) handle);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700911 return p_info;
912}
913
vivek mehtaa76401a2015-04-24 14:12:15 -0700914__attribute__ ((visibility ("default")))
915bool audio_hw_send_gain_dep_calibration(int level) {
916 bool ret_val = false;
vivek mehtab72d08d2016-04-29 03:16:47 -0700917 ALOGV("%s: called ...", __func__);
vivek mehtaa76401a2015-04-24 14:12:15 -0700918
919 pthread_mutex_lock(&adev_init_lock);
920
921 if (adev != NULL && adev->platform != NULL) {
922 pthread_mutex_lock(&adev->lock);
923 ret_val = platform_send_gain_dep_cal(adev->platform, level);
vivek mehtab72d08d2016-04-29 03:16:47 -0700924
Preetam Singh Ranawatf4ae0222017-05-31 17:07:28 +0530925 // cache level info for any of the use case which
926 // was not started.
927 last_known_cal_step = level;;
vivek mehtab72d08d2016-04-29 03:16:47 -0700928
vivek mehtaa76401a2015-04-24 14:12:15 -0700929 pthread_mutex_unlock(&adev->lock);
930 } else {
931 ALOGE("%s: %s is NULL", __func__, adev == NULL ? "adev" : "adev->platform");
932 }
933
934 pthread_mutex_unlock(&adev_init_lock);
935
936 return ret_val;
937}
938
Ashish Jain5106d362016-05-11 19:23:33 +0530939static int check_and_set_gapless_mode(struct audio_device *adev, bool enable_gapless)
940{
Krishnankutty Kolathappilly6d8788b2014-01-09 12:45:31 -0800941 bool gapless_enabled = false;
942 const char *mixer_ctl_name = "Compress Gapless Playback";
943 struct mixer_ctl *ctl;
944
945 ALOGV("%s:", __func__);
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700946 gapless_enabled = property_get_bool("vendor.audio.offload.gapless.enabled", false);
Ashish Jain5106d362016-05-11 19:23:33 +0530947
948 /*Disable gapless if its AV playback*/
949 gapless_enabled = gapless_enabled && enable_gapless;
Krishnankutty Kolathappilly6d8788b2014-01-09 12:45:31 -0800950
951 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
952 if (!ctl) {
953 ALOGE("%s: Could not get ctl for mixer cmd - %s",
954 __func__, mixer_ctl_name);
955 return -EINVAL;
956 }
957
958 if (mixer_ctl_set_value(ctl, 0, gapless_enabled) < 0) {
959 ALOGE("%s: Could not set gapless mode %d",
960 __func__, gapless_enabled);
961 return -EINVAL;
962 }
963 return 0;
964}
Haynes Mathew George5191a852013-09-11 14:19:36 -0700965
Aniket Kumar Lataf56b6402016-10-27 12:03:18 -0700966__attribute__ ((visibility ("default")))
967int audio_hw_get_gain_level_mapping(struct amp_db_and_gain_table *mapping_tbl,
968 int table_size) {
969 int ret_val = 0;
970 ALOGV("%s: enter ... ", __func__);
971
972 pthread_mutex_lock(&adev_init_lock);
973 if (adev == NULL) {
974 ALOGW("%s: adev is NULL .... ", __func__);
975 goto done;
976 }
977
978 pthread_mutex_lock(&adev->lock);
979 ret_val = platform_get_gain_level_mapping(mapping_tbl, table_size);
980 pthread_mutex_unlock(&adev->lock);
981done:
982 pthread_mutex_unlock(&adev_init_lock);
983 ALOGV("%s: exit ... ", __func__);
984 return ret_val;
985}
986
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800987bool audio_hw_send_qdsp_parameter(int stream_type, float vol, bool active)
Aalique Grahame22e49102018-12-18 14:23:57 -0800988{
989 bool ret = false;
990 ALOGV("%s: enter ...", __func__);
991
992 pthread_mutex_lock(&adev_init_lock);
993
994 if (adev != NULL && adev->platform != NULL) {
995 pthread_mutex_lock(&adev->lock);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800996 ret = audio_extn_qdsp_set_state(adev, stream_type, vol, active);
Aalique Grahame22e49102018-12-18 14:23:57 -0800997 pthread_mutex_unlock(&adev->lock);
998 }
999
1000 pthread_mutex_unlock(&adev_init_lock);
1001
1002 ALOGV("%s: exit with ret %d", __func__, ret);
1003 return ret;
1004}
Aalique Grahame22e49102018-12-18 14:23:57 -08001005
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001006static bool is_supported_format(audio_format_t format)
1007{
Eric Laurent86e17132013-09-12 17:49:30 -07001008 if (format == AUDIO_FORMAT_MP3 ||
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301009 format == AUDIO_FORMAT_MP2 ||
Ashish Jainf9b78162014-08-25 20:36:25 +05301010 format == AUDIO_FORMAT_AAC_LC ||
1011 format == AUDIO_FORMAT_AAC_HE_V1 ||
1012 format == AUDIO_FORMAT_AAC_HE_V2 ||
Manish Dewangana6fc5442015-08-24 20:30:31 +05301013 format == AUDIO_FORMAT_AAC_ADTS_LC ||
1014 format == AUDIO_FORMAT_AAC_ADTS_HE_V1 ||
1015 format == AUDIO_FORMAT_AAC_ADTS_HE_V2 ||
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301016 format == AUDIO_FORMAT_AAC_LATM_LC ||
1017 format == AUDIO_FORMAT_AAC_LATM_HE_V1 ||
1018 format == AUDIO_FORMAT_AAC_LATM_HE_V2 ||
Ashish Jain5106d362016-05-11 19:23:33 +05301019 format == AUDIO_FORMAT_PCM_24_BIT_PACKED ||
1020 format == AUDIO_FORMAT_PCM_8_24_BIT ||
Ashish Jainf1eaa582016-05-23 20:54:24 +05301021 format == AUDIO_FORMAT_PCM_FLOAT ||
1022 format == AUDIO_FORMAT_PCM_32_BIT ||
vivek mehta0ea887a2015-08-26 14:01:20 -07001023 format == AUDIO_FORMAT_PCM_16_BIT ||
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05301024 format == AUDIO_FORMAT_AC3 ||
1025 format == AUDIO_FORMAT_E_AC3 ||
Ben Romberger1aaaf862017-04-06 17:49:46 -07001026 format == AUDIO_FORMAT_DOLBY_TRUEHD ||
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05301027 format == AUDIO_FORMAT_DTS ||
1028 format == AUDIO_FORMAT_DTS_HD ||
Weiyin Jiang18ac4e92015-03-15 15:03:40 +08001029 format == AUDIO_FORMAT_FLAC ||
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +05301030 format == AUDIO_FORMAT_ALAC ||
1031 format == AUDIO_FORMAT_APE ||
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05301032 format == AUDIO_FORMAT_DSD ||
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +05301033 format == AUDIO_FORMAT_VORBIS ||
Weiyin Jiang18ac4e92015-03-15 15:03:40 +08001034 format == AUDIO_FORMAT_WMA ||
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05301035 format == AUDIO_FORMAT_WMA_PRO ||
Naresh Tanniru928f0862017-04-07 16:44:23 -07001036 format == AUDIO_FORMAT_APTX ||
1037 format == AUDIO_FORMAT_IEC61937)
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08001038 return true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001039
1040 return false;
1041}
1042
Kunlei Zhang67cc7072020-12-18 17:16:49 +08001043static bool is_supported_conc_usecase_for_power_mode_call(struct audio_device *adev)
1044{
1045 struct listnode *node;
1046 struct audio_usecase *usecase;
1047
1048 list_for_each(node, &adev->usecase_list) {
1049 usecase = node_to_item(node, struct audio_usecase, list);
1050 if (usecase->id == USECASE_AUDIO_PLAYBACK_FM) {
1051 ALOGD("%s: FM usecase is active, not setting power mode", __func__);
1052 return false;
1053 }
1054 }
1055
1056 return true;
1057}
Haynes Mathew George5beddd42016-06-27 18:33:40 -07001058static inline bool is_mmap_usecase(audio_usecase_t uc_id)
1059{
1060 return (uc_id == USECASE_AUDIO_RECORD_AFE_PROXY) ||
Zhou Song62ea0282020-03-22 19:53:01 +08001061 (uc_id == USECASE_AUDIO_RECORD_AFE_PROXY2) ||
Haynes Mathew George5beddd42016-06-27 18:33:40 -07001062 (uc_id == USECASE_AUDIO_PLAYBACK_AFE_PROXY);
1063}
1064
Aniket Kumar Lata8426d1f2019-06-10 15:25:53 -07001065static inline bool is_valid_volume(float left, float right)
1066{
1067 return ((left >= 0.0f && right >= 0.0f) ? true : false);
1068}
1069
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +05301070static void enable_asrc_mode(struct audio_device *adev)
Preetam Singh Ranawatb0c0dd72016-08-18 00:32:06 +05301071{
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +05301072 ALOGV("%s", __func__);
1073 audio_route_apply_and_update_path(adev->audio_route,
1074 "asrc-mode");
1075 adev->asrc_mode_enabled = true;
1076}
1077
1078static void disable_asrc_mode(struct audio_device *adev)
1079{
1080 ALOGV("%s", __func__);
1081 audio_route_reset_and_update_path(adev->audio_route,
1082 "asrc-mode");
1083 adev->asrc_mode_enabled = false;
1084}
1085
Saurav Kumarc1411662020-10-14 10:50:45 +05301086static void check_and_configure_headphone(struct audio_device *adev,
1087 struct audio_usecase *uc_info,
1088 snd_device_t snd_device)
1089{
1090 struct listnode *node;
1091 struct audio_usecase *usecase;
1092 int new_backend_idx, usecase_backend_idx;
1093 bool spkr_hph_single_be_native_concurrency;
1094
1095 new_backend_idx = platform_get_backend_index(snd_device);
1096 spkr_hph_single_be_native_concurrency = platform_get_spkr_hph_single_be_native_concurrency_flag();
Zhou Songd4b18c42021-01-14 15:15:29 +08001097 if ((spkr_hph_single_be_native_concurrency && (new_backend_idx == DEFAULT_CODEC_BACKEND)) ||
1098 uc_info->id == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) {
Saurav Kumarc1411662020-10-14 10:50:45 +05301099 list_for_each(node, &adev->usecase_list) {
1100 usecase = node_to_item(node, struct audio_usecase, list);
1101 if ((usecase->type != PCM_CAPTURE) && (usecase != uc_info)) {
1102 usecase_backend_idx = platform_get_backend_index(usecase->out_snd_device);
1103 if (((usecase_backend_idx == HEADPHONE_BACKEND) ||
1104 (usecase_backend_idx == HEADPHONE_44_1_BACKEND)) &&
1105 ((usecase->stream.out->sample_rate % OUTPUT_SAMPLING_RATE_44100) == 0)) {
1106 disable_audio_route(adev, usecase);
1107 disable_snd_device(adev, usecase->out_snd_device);
1108 usecase->stream.out->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Saurav Kumar70902382020-10-28 11:16:04 +05301109 platform_check_and_set_codec_backend_cfg(adev, usecase,
1110 usecase->out_snd_device);
Saurav Kumarc1411662020-10-14 10:50:45 +05301111 enable_snd_device(adev, usecase->out_snd_device);
Zhou Songd4b18c42021-01-14 15:15:29 +08001112 enable_audio_route(adev, usecase);
Saurav Kumarc1411662020-10-14 10:50:45 +05301113 }
1114 }
Revathi Uddaraju2c0eac02021-07-27 02:06:42 -07001115 else if ((usecase->type != PCM_CAPTURE) && (usecase == uc_info)) {
1116 usecase_backend_idx = platform_get_backend_index(usecase->out_snd_device);
1117 if (((usecase_backend_idx == HEADPHONE_BACKEND) ||
1118 (usecase_backend_idx == HEADPHONE_44_1_BACKEND)) &&
1119 ((usecase->stream.out->sample_rate % OUTPUT_SAMPLING_RATE_44100) == 0)) {
1120 usecase->stream.out->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1121 platform_check_and_set_codec_backend_cfg(adev, usecase,
1122 usecase->out_snd_device);
1123 }
1124 }
Saurav Kumarc1411662020-10-14 10:50:45 +05301125 }
1126 }
1127}
1128
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +05301129/*
1130 * - Enable ASRC mode for incoming mix path use case(Headphone backend)if Headphone
1131 * 44.1 or Native DSD backends are enabled for any of current use case.
1132 * e.g. 48-> + (Naitve DSD or Headphone 44.1)
1133 * - Disable current mix path use case(Headphone backend) and re-enable it with
1134 * ASRC mode for incoming Headphone 44.1 or Native DSD use case.
1135 * e.g. Naitve DSD or Headphone 44.1 -> + 48
1136 */
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +05301137static void check_and_set_asrc_mode(struct audio_device *adev,
1138 struct audio_usecase *uc_info,
1139 snd_device_t snd_device)
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +05301140{
1141 ALOGV("%s snd device %d", __func__, snd_device);
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +05301142 int i, num_new_devices = 0;
1143 snd_device_t split_new_snd_devices[SND_DEVICE_OUT_END];
1144 /*
1145 *Split snd device for new combo use case
1146 *e.g. Headphopne 44.1-> + Ringtone (Headphone + Speaker)
1147 */
1148 if (platform_split_snd_device(adev->platform,
1149 snd_device,
1150 &num_new_devices,
1151 split_new_snd_devices) == 0) {
1152 for (i = 0; i < num_new_devices; i++)
1153 check_and_set_asrc_mode(adev, uc_info, split_new_snd_devices[i]);
1154 } else {
1155 int new_backend_idx = platform_get_backend_index(snd_device);
1156 if (((new_backend_idx == HEADPHONE_BACKEND) ||
1157 (new_backend_idx == HEADPHONE_44_1_BACKEND) ||
1158 (new_backend_idx == DSD_NATIVE_BACKEND)) &&
1159 !adev->asrc_mode_enabled) {
1160 struct listnode *node = NULL;
1161 struct audio_usecase *uc = NULL;
1162 struct stream_out *curr_out = NULL;
1163 int usecase_backend_idx = DEFAULT_CODEC_BACKEND;
1164 int i, num_devices, ret = 0;
1165 snd_device_t split_snd_devices[SND_DEVICE_OUT_END];
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +05301166
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +05301167 list_for_each(node, &adev->usecase_list) {
1168 uc = node_to_item(node, struct audio_usecase, list);
1169 curr_out = (struct stream_out*) uc->stream.out;
1170 if (curr_out && PCM_PLAYBACK == uc->type && uc != uc_info) {
1171 /*
1172 *Split snd device for existing combo use case
1173 *e.g. Ringtone (Headphone + Speaker) + Headphopne 44.1
1174 */
1175 ret = platform_split_snd_device(adev->platform,
1176 uc->out_snd_device,
1177 &num_devices,
1178 split_snd_devices);
1179 if (ret < 0 || num_devices == 0) {
1180 ALOGV("%s: Unable to split uc->out_snd_device: %d",__func__, uc->out_snd_device);
1181 split_snd_devices[0] = uc->out_snd_device;
1182 num_devices = 1;
Garmond Leung50058f62017-02-08 09:49:30 -08001183 }
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +05301184 for (i = 0; i < num_devices; i++) {
1185 usecase_backend_idx = platform_get_backend_index(split_snd_devices[i]);
1186 ALOGD("%s:snd_dev %d usecase_backend_idx %d",__func__, split_snd_devices[i],usecase_backend_idx);
1187 if((new_backend_idx == HEADPHONE_BACKEND) &&
1188 ((usecase_backend_idx == HEADPHONE_44_1_BACKEND) ||
1189 (usecase_backend_idx == DSD_NATIVE_BACKEND))) {
Aniket Kumar Latad13758f2020-08-06 15:11:36 -07001190 ALOGV("%s:DSD or native stream detected enabling asrcmode in hardware",
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +05301191 __func__);
1192 enable_asrc_mode(adev);
1193 break;
1194 } else if(((new_backend_idx == HEADPHONE_44_1_BACKEND) ||
1195 (new_backend_idx == DSD_NATIVE_BACKEND)) &&
1196 (usecase_backend_idx == HEADPHONE_BACKEND)) {
Aniket Kumar Latad13758f2020-08-06 15:11:36 -07001197 ALOGV("%s: 48K stream detected, disabling and enabling it \
1198 with asrcmode in hardware", __func__);
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +05301199 disable_audio_route(adev, uc);
1200 disable_snd_device(adev, uc->out_snd_device);
1201 // Apply true-high-quality-mode if DSD or > 44.1KHz or >=24-bit
1202 if (new_backend_idx == DSD_NATIVE_BACKEND)
1203 audio_route_apply_and_update_path(adev->audio_route,
1204 "hph-true-highquality-mode");
1205 else if ((new_backend_idx == HEADPHONE_44_1_BACKEND) &&
1206 (curr_out->bit_width >= 24))
1207 audio_route_apply_and_update_path(adev->audio_route,
1208 "hph-highquality-mode");
1209 enable_asrc_mode(adev);
1210 enable_snd_device(adev, uc->out_snd_device);
1211 enable_audio_route(adev, uc);
1212 break;
1213 }
1214 }
1215 // reset split devices count
1216 num_devices = 0;
Garmond Leung50058f62017-02-08 09:49:30 -08001217 }
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +05301218 if (adev->asrc_mode_enabled)
1219 break;
Preetam Singh Ranawatb0c0dd72016-08-18 00:32:06 +05301220 }
1221 }
1222 }
1223}
1224
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001225static int send_effect_enable_disable_mixer_ctl(struct audio_device *adev,
1226 struct audio_effect_config effect_config,
1227 unsigned int param_value)
1228{
1229 char mixer_ctl_name[] = "Audio Effect";
1230 struct mixer_ctl *ctl;
1231 long set_values[6];
Eric Laurent637e2d42018-11-15 12:24:31 -08001232 struct stream_in *in = adev_get_active_input(adev);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001233
Aniket Kumar Lata7de63522019-06-13 11:05:18 -07001234 if (in == NULL) {
1235 ALOGE("%s: active input stream is NULL", __func__);
1236 return -EINVAL;
1237 }
1238
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001239 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1240 if (!ctl) {
1241 ALOGE("%s: Could not get mixer ctl - %s",
1242 __func__, mixer_ctl_name);
1243 return -EINVAL;
1244 }
1245
1246 set_values[0] = 1; //0:Rx 1:Tx
1247 set_values[1] = in->app_type_cfg.app_type;
1248 set_values[2] = (long)effect_config.module_id;
1249 set_values[3] = (long)effect_config.instance_id;
1250 set_values[4] = (long)effect_config.param_id;
1251 set_values[5] = param_value;
1252
1253 mixer_ctl_set_array(ctl, set_values, ARRAY_SIZE(set_values));
1254
1255 return 0;
1256
1257}
1258
1259static int update_effect_param_ecns(struct audio_device *adev, unsigned int module_id,
1260 int effect_type, unsigned int *param_value)
1261{
1262 int ret = 0;
1263 struct audio_effect_config other_effect_config;
1264 struct audio_usecase *usecase = NULL;
Eric Laurent637e2d42018-11-15 12:24:31 -08001265 struct stream_in *in = adev_get_active_input(adev);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001266
Aniket Kumar Lata7de63522019-06-13 11:05:18 -07001267 if (in == NULL) {
1268 ALOGE("%s: active input stream is NULL", __func__);
1269 return -EINVAL;
1270 }
1271
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001272 usecase = get_usecase_from_list(adev, in->usecase);
1273 if (!usecase)
1274 return -EINVAL;
1275
1276 ret = platform_get_effect_config_data(usecase->in_snd_device, &other_effect_config,
1277 effect_type == EFFECT_AEC ? EFFECT_NS : EFFECT_AEC);
1278 if (ret < 0) {
1279 ALOGE("%s Failed to get effect params %d", __func__, ret);
1280 return ret;
1281 }
1282
1283 if (module_id == other_effect_config.module_id) {
1284 //Same module id for AEC/NS. Values need to be combined
1285 if (((effect_type == EFFECT_AEC) && (in->enable_ns)) ||
1286 ((effect_type == EFFECT_NS) && (in->enable_aec))) {
1287 *param_value |= other_effect_config.param_value;
1288 }
1289 }
1290
1291 return ret;
1292}
1293
1294static int enable_disable_effect(struct audio_device *adev, int effect_type, bool enable)
Gangadhar Sb0210342019-02-22 17:39:41 +05301295{
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001296 struct audio_effect_config effect_config;
1297 struct audio_usecase *usecase = NULL;
1298 int ret = 0;
1299 unsigned int param_value = 0;
Eric Laurent637e2d42018-11-15 12:24:31 -08001300 struct stream_in *in = adev_get_active_input(adev);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001301
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001302 if(!voice_extn_is_dynamic_ecns_enabled())
1303 return ENOSYS;
1304
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001305 if (!in) {
1306 ALOGE("%s: Invalid input stream", __func__);
1307 return -EINVAL;
1308 }
1309
1310 ALOGD("%s: effect_type:%d enable:%d", __func__, effect_type, enable);
1311
1312 usecase = get_usecase_from_list(adev, in->usecase);
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001313 if (usecase == NULL) {
1314 ALOGE("%s: Could not find the usecase (%d) in the list",
1315 __func__, in->usecase);
1316 return -EINVAL;
1317 }
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001318
1319 ret = platform_get_effect_config_data(usecase->in_snd_device, &effect_config, effect_type);
1320 if (ret < 0) {
1321 ALOGE("%s Failed to get module id %d", __func__, ret);
1322 return ret;
1323 }
1324 ALOGV("%s: %d %d usecase->id:%d usecase->in_snd_device:%d", __func__, effect_config.module_id,
1325 in->app_type_cfg.app_type, usecase->id, usecase->in_snd_device);
1326
1327 if(enable)
1328 param_value = effect_config.param_value;
1329
1330 /*Special handling for AEC & NS effects Param values need to be
1331 updated if module ids are same*/
1332
1333 if ((effect_type == EFFECT_AEC) || (effect_type == EFFECT_NS)) {
1334 ret = update_effect_param_ecns(adev, effect_config.module_id, effect_type, &param_value);
1335 if (ret < 0)
1336 return ret;
1337 }
1338
1339 ret = send_effect_enable_disable_mixer_ctl(adev, effect_config, param_value);
1340
1341 return ret;
1342}
1343
1344static void check_and_enable_effect(struct audio_device *adev)
1345{
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001346 if(!voice_extn_is_dynamic_ecns_enabled())
1347 return;
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001348
Eric Laurent637e2d42018-11-15 12:24:31 -08001349 struct stream_in *in = adev_get_active_input(adev);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001350
Eric Laurent637e2d42018-11-15 12:24:31 -08001351 if (in != NULL && !in->standby) {
1352 if (in->enable_aec)
1353 enable_disable_effect(adev, EFFECT_AEC, true);
1354
1355 if (in->enable_ns &&
1356 in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
1357 enable_disable_effect(adev, EFFECT_NS, true);
1358 }
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001359 }
1360}
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001361
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07001362int pcm_ioctl(struct pcm *pcm, int request, ...)
1363{
1364 va_list ap;
1365 void * arg;
1366 int pcm_fd = *(int*)pcm;
1367
1368 va_start(ap, request);
1369 arg = va_arg(ap, void *);
1370 va_end(ap);
1371
1372 return ioctl(pcm_fd, request, arg);
1373}
1374
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -07001375int enable_audio_route(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001376 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001377{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001378 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001379 char mixer_path[MIXER_PATH_MAX_LENGTH];
Manish Dewangan58229382017-02-02 15:48:41 +05301380 struct stream_out *out = NULL;
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05301381 struct stream_in *in = NULL;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001382 struct listnode out_devices;
Soumya Managoli6993b762018-06-28 16:04:57 +05301383 int ret = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001384
1385 if (usecase == NULL)
1386 return -EINVAL;
1387
1388 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
1389
Carter Hsu2e429db2019-05-14 18:50:52 +08001390 if (usecase->type == PCM_CAPTURE) {
1391 struct stream_in *in = usecase->stream.in;
1392 struct audio_usecase *uinfo;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001393 snd_device = usecase->in_snd_device;
Carter Hsu2e429db2019-05-14 18:50:52 +08001394
1395 if (in) {
1396 if (in->enable_aec || in->enable_ec_port) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001397 list_init(&out_devices);
1398 update_device_list(&out_devices, AUDIO_DEVICE_OUT_SPEAKER, "", true);
Carter Hsu2e429db2019-05-14 18:50:52 +08001399 struct listnode *node;
1400 struct audio_usecase *voip_usecase = get_usecase_from_list(adev,
1401 USECASE_AUDIO_PLAYBACK_VOIP);
1402 if (voip_usecase) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001403 assign_devices(&out_devices,
1404 &voip_usecase->stream.out->device_list);
Carter Hsu2e429db2019-05-14 18:50:52 +08001405 } else if (adev->primary_output &&
1406 !adev->primary_output->standby) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001407 assign_devices(&out_devices,
1408 &adev->primary_output->device_list);
Carter Hsu2e429db2019-05-14 18:50:52 +08001409 } else {
1410 list_for_each(node, &adev->usecase_list) {
1411 uinfo = node_to_item(node, struct audio_usecase, list);
1412 if (uinfo->type != PCM_CAPTURE) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001413 assign_devices(&out_devices,
1414 &uinfo->stream.out->device_list);
Carter Hsu2e429db2019-05-14 18:50:52 +08001415 break;
1416 }
1417 }
1418 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001419
1420 platform_set_echo_reference(adev, true, &out_devices);
Carter Hsu2e429db2019-05-14 18:50:52 +08001421 in->ec_opened = true;
Zhenlin Lian4f947842022-05-14 15:50:52 +05301422 clear_devices(&out_devices);
Carter Hsu2e429db2019-05-14 18:50:52 +08001423 }
1424 }
Guodong Huf5e614d2019-06-24 18:42:03 +08001425 } else if ((usecase->type == TRANSCODE_LOOPBACK_TX) || ((usecase->type == PCM_HFP_CALL) &&
1426 ((usecase->id == USECASE_AUDIO_HFP_SCO) || (usecase->id == USECASE_AUDIO_HFP_SCO_WB)) &&
1427 (usecase->in_snd_device == SND_DEVICE_IN_VOICE_SPEAKER_MIC_HFP_MMSECNS))) {
Carter Hsu2e429db2019-05-14 18:50:52 +08001428 snd_device = usecase->in_snd_device;
1429 } else {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001430 snd_device = usecase->out_snd_device;
Carter Hsu2e429db2019-05-14 18:50:52 +08001431 }
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001432
Kunlei Zhanga3c4cb02021-03-05 16:03:54 +08001433 if (usecase->type == PCM_CAPTURE) {
1434 if (platform_get_fluence_nn_state(adev->platform) == 0) {
1435 platform_set_fluence_nn_state(adev->platform, true);
1436 ALOGD("%s: set fluence nn capture state", __func__);
1437 }
1438 }
1439
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08001440#ifdef DS1_DOLBY_DAP_ENABLED
1441 audio_extn_dolby_set_dmid(adev);
1442 audio_extn_dolby_set_endpoint(adev);
1443#endif
Pradnya Chaphekar8a9dcd82014-09-09 09:49:10 -07001444 audio_extn_dolby_ds2_set_endpoint(adev);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001445 audio_extn_sound_trigger_update_stream_status(usecase, ST_EVENT_STREAM_BUSY);
Dhananjay Kumar45b71742014-05-29 21:47:27 +05301446 audio_extn_listen_update_stream_status(usecase, LISTEN_EVENT_STREAM_BUSY);
Ben Romberger1fafdde2015-09-09 19:43:15 -07001447 audio_extn_utils_send_app_type_cfg(adev, usecase);
Jasmine Cha4dcc1092019-03-04 18:12:47 +08001448 if (audio_extn_is_maxx_audio_enabled())
1449 audio_extn_ma_set_device(usecase);
Dhananjay Kumar14170dd2015-08-28 13:24:16 +05301450 audio_extn_utils_send_audio_calibration(adev, usecase);
Zhou Songbaddf9f2020-11-20 13:57:39 +08001451 if ((usecase->type == PCM_PLAYBACK) && is_offload_usecase(usecase->id)) {
1452 out = usecase->stream.out;
1453 if (out && out->compr)
Manish Dewangan58229382017-02-02 15:48:41 +05301454 audio_extn_utils_compress_set_clk_rec_mode(usecase);
1455 }
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05301456
1457 if (usecase->type == PCM_CAPTURE) {
Kunlei Zhanga3c4cb02021-03-05 16:03:54 +08001458 if (platform_get_fluence_nn_state(adev->platform) == 1 &&
1459 adev->fluence_nn_usecase_id == USECASE_INVALID ) {
1460 adev->fluence_nn_usecase_id = usecase->id;
1461 ALOGD("%s: assign fluence nn usecase %d", __func__, usecase->id);
1462 }
1463 }
1464
1465 if (usecase->type == PCM_CAPTURE) {
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05301466 in = usecase->stream.in;
Avinash Chandrad7296d42021-08-04 15:07:47 +05301467 if ((in && is_loopback_input_device(get_device_types(&in->device_list))) ||
1468 (in && is_combo_audio_input_device(&in->device_list)) ||
1469 (in && ((compare_device_type(&in->device_list, AUDIO_DEVICE_IN_BUILTIN_MIC) ||
1470 compare_device_type(&in->device_list, AUDIO_DEVICE_IN_LINE)) &&
1471 (snd_device == SND_DEVICE_IN_HANDSET_GENERIC_6MIC)))) {
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05301472 ALOGD("%s: set custom mtmx params v1", __func__);
1473 audio_extn_set_custom_mtmx_params_v1(adev, usecase, true);
1474 }
1475 } else {
1476 audio_extn_set_custom_mtmx_params_v2(adev, usecase, true);
1477 }
Manish Dewangan58229382017-02-02 15:48:41 +05301478
Andy Hung756ecc12018-10-19 17:47:12 -07001479 // we shouldn't truncate mixer_path
1480 ALOGW_IF(strlcpy(mixer_path, use_case_table[usecase->id], sizeof(mixer_path))
1481 >= sizeof(mixer_path), "%s: truncation on mixer path", __func__);
1482 // this also appends to mixer_path
Banajit Goswami20cdd212015-09-11 01:11:30 -07001483 platform_add_backend_name(mixer_path, snd_device, usecase);
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07001484 ALOGD("%s: apply mixer and update path: %s", __func__, mixer_path);
Soumya Managoli6993b762018-06-28 16:04:57 +05301485 ret = audio_route_apply_and_update_path(adev->audio_route, mixer_path);
1486 if (!ret && usecase->id == USECASE_AUDIO_PLAYBACK_FM) {
1487 struct str_parms *parms = str_parms_create_str("fm_restore_volume=1");
1488 if (parms) {
1489 audio_extn_fm_set_parameters(adev, parms);
1490 str_parms_destroy(parms);
1491 }
1492 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001493 ALOGV("%s: exit", __func__);
1494 return 0;
1495}
1496
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001497int disable_audio_route(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001498 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001499{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001500 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001501 char mixer_path[MIXER_PATH_MAX_LENGTH];
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05301502 struct stream_in *in = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001503
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05301504 if (usecase == NULL || usecase->id == USECASE_INVALID)
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001505 return -EINVAL;
1506
1507 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Surendar Karka93cd25a2018-08-28 14:21:37 +05301508 if (usecase->type == PCM_CAPTURE || usecase->type == TRANSCODE_LOOPBACK_TX)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001509 snd_device = usecase->in_snd_device;
1510 else
1511 snd_device = usecase->out_snd_device;
Kunlei Zhange7cab1a2020-05-17 20:31:05 +08001512
1513 /* disable island and power mode on supported device for voice call */
1514 if (usecase->type == VOICE_CALL) {
1515 if (usecase->in_snd_device != SND_DEVICE_NONE) {
1516 if (platform_get_island_cfg_on_device(adev->platform, usecase->in_snd_device) &&
1517 platform_get_power_mode_on_device(adev->platform, usecase->in_snd_device)) {
1518 platform_set_island_cfg_on_device(adev, usecase->in_snd_device, false);
1519 platform_set_power_mode_on_device(adev, usecase->in_snd_device, false);
1520 platform_reset_island_power_status(adev->platform, usecase->in_snd_device);
Kunlei Zhang91c4b332020-07-20 17:53:14 +08001521 if (voice_is_lte_call_active(adev))
1522 platform_set_tx_lpi_mode(adev->platform, false);
Kunlei Zhange7cab1a2020-05-17 20:31:05 +08001523 ALOGD("%s: disable island cfg and power mode in voice tx path",
1524 __func__);
1525 }
1526 }
1527 if (usecase->out_snd_device != SND_DEVICE_NONE) {
1528 if (platform_get_island_cfg_on_device(adev->platform, usecase->out_snd_device) &&
1529 platform_get_power_mode_on_device(adev->platform, usecase->out_snd_device)) {
1530 platform_set_island_cfg_on_device(adev, usecase->out_snd_device, false);
1531 platform_set_power_mode_on_device(adev, usecase->out_snd_device, false);
1532 platform_reset_island_power_status(adev->platform, usecase->out_snd_device);
1533 ALOGD("%s: disable island cfg and power mode in voice rx path",
1534 __func__);
1535 }
1536 }
1537 }
1538
Andy Hung756ecc12018-10-19 17:47:12 -07001539 // we shouldn't truncate mixer_path
1540 ALOGW_IF(strlcpy(mixer_path, use_case_table[usecase->id], sizeof(mixer_path))
1541 >= sizeof(mixer_path), "%s: truncation on mixer path", __func__);
1542 // this also appends to mixer_path
Banajit Goswami20cdd212015-09-11 01:11:30 -07001543 platform_add_backend_name(mixer_path, snd_device, usecase);
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07001544 ALOGD("%s: reset and update mixer path: %s", __func__, mixer_path);
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07001545 audio_route_reset_and_update_path(adev->audio_route, mixer_path);
Carter Hsu2e429db2019-05-14 18:50:52 +08001546 if (usecase->type == PCM_CAPTURE) {
1547 struct stream_in *in = usecase->stream.in;
1548 if (in && in->ec_opened) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001549 struct listnode out_devices;
1550 list_init(&out_devices);
1551 platform_set_echo_reference(in->dev, false, &out_devices);
Carter Hsu2e429db2019-05-14 18:50:52 +08001552 in->ec_opened = false;
Zhenlin Lian4f947842022-05-14 15:50:52 +05301553 clear_devices(&out_devices);
Carter Hsu2e429db2019-05-14 18:50:52 +08001554 }
1555 }
Kunlei Zhanga3c4cb02021-03-05 16:03:54 +08001556 if (usecase->id == adev->fluence_nn_usecase_id) {
1557 platform_set_fluence_nn_state(adev->platform, false);
1558 adev->fluence_nn_usecase_id = USECASE_INVALID;
1559 ALOGD("%s: reset fluence nn capture state", __func__);
1560 }
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001561 audio_extn_sound_trigger_update_stream_status(usecase, ST_EVENT_STREAM_FREE);
Dhananjay Kumar45b71742014-05-29 21:47:27 +05301562 audio_extn_listen_update_stream_status(usecase, LISTEN_EVENT_STREAM_FREE);
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05301563
1564 if (usecase->type == PCM_CAPTURE) {
1565 in = usecase->stream.in;
Avinash Chandrad7296d42021-08-04 15:07:47 +05301566 if ((in && is_loopback_input_device(get_device_types(&in->device_list))) ||
1567 (in && is_combo_audio_input_device(&in->device_list)) ||
1568 (in && ((compare_device_type(&in->device_list, AUDIO_DEVICE_IN_BUILTIN_MIC) ||
1569 compare_device_type(&in->device_list, AUDIO_DEVICE_IN_LINE)) &&
1570 (snd_device == SND_DEVICE_IN_HANDSET_GENERIC_6MIC)))){
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05301571 ALOGD("%s: reset custom mtmx params v1", __func__);
1572 audio_extn_set_custom_mtmx_params_v1(adev, usecase, false);
1573 }
1574 } else {
1575 audio_extn_set_custom_mtmx_params_v2(adev, usecase, false);
1576 }
1577
Weiyin Jiang298ffd92019-06-03 14:29:30 +08001578 if ((usecase->type == PCM_PLAYBACK) &&
1579 (usecase->stream.out != NULL))
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +05301580 usecase->stream.out->pspd_coeff_sent = false;
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05301581
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001582 ALOGV("%s: exit", __func__);
1583 return 0;
1584}
1585
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -07001586int enable_snd_device(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001587 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001588{
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301589 int i, num_devices = 0;
1590 snd_device_t new_snd_devices[SND_DEVICE_OUT_END];
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001591 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
1592
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001593 if (snd_device < SND_DEVICE_MIN ||
1594 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001595 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001596 return -EINVAL;
1597 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001598
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001599 if (platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0) {
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001600 ALOGE("%s: Invalid sound device returned", __func__);
1601 return -EINVAL;
1602 }
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001603
1604 adev->snd_dev_ref_cnt[snd_device]++;
1605
1606 if ((adev->snd_dev_ref_cnt[snd_device] > 1) &&
1607 (platform_split_snd_device(adev->platform,
1608 snd_device,
1609 &num_devices,
1610 new_snd_devices) != 0)) {
Eric Laurent994a6932013-07-17 11:51:42 -07001611 ALOGV("%s: snd_device(%d: %s) is already active",
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001612 __func__, snd_device, device_name);
Aniket Kumar Lata990de552019-07-11 14:20:23 -07001613 /* Set backend config for A2DP to ensure slimbus configuration
1614 is correct if A2DP is already active and backend is closed
1615 and re-opened */
1616 if (snd_device == SND_DEVICE_OUT_BT_A2DP)
1617 audio_extn_a2dp_set_source_backend_cfg();
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001618 return 0;
1619 }
1620
Gopikrishnaiah Anandane85d0462014-06-30 21:41:20 -07001621 if (audio_extn_spkr_prot_is_enabled())
1622 audio_extn_spkr_prot_calib_cancel(adev);
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -07001623
Aalique Grahame22e49102018-12-18 14:23:57 -08001624 audio_extn_dsm_feedback_enable(adev, snd_device, true);
1625
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001626 if (platform_can_enable_spkr_prot_on_device(snd_device) &&
1627 audio_extn_spkr_prot_is_enabled()) {
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001628 if (platform_get_spkr_prot_acdb_id(snd_device) < 0) {
1629 goto err;
1630 }
1631 audio_extn_dev_arbi_acquire(snd_device);
1632 if (audio_extn_spkr_prot_start_processing(snd_device)) {
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -08001633 ALOGE("%s: spkr_start_processing failed", __func__);
Ravit Dennisaaee49c2015-02-04 21:26:22 +02001634 audio_extn_dev_arbi_release(snd_device);
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001635 goto err;
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -08001636 }
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001637 } else if (platform_split_snd_device(adev->platform,
1638 snd_device,
1639 &num_devices,
1640 new_snd_devices) == 0) {
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301641 for (i = 0; i < num_devices; i++) {
1642 enable_snd_device(adev, new_snd_devices[i]);
1643 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001644 platform_set_speaker_gain_in_combo(adev, snd_device, true);
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -08001645 } else {
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07001646 ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, device_name);
Naresh Tannirucd2353e2016-08-19 00:37:25 +05301647
Kunlei Zhange7cab1a2020-05-17 20:31:05 +08001648 /* enable island and power mode on supported device */
1649 if (platform_get_island_cfg_on_device(adev->platform, snd_device) &&
1650 platform_get_power_mode_on_device(adev->platform, snd_device)) {
1651 platform_set_island_cfg_on_device(adev, snd_device, true);
1652 platform_set_power_mode_on_device(adev, snd_device, true);
Kunlei Zhang91c4b332020-07-20 17:53:14 +08001653 if (voice_is_lte_call_active(adev) &&
1654 (snd_device >= SND_DEVICE_IN_BEGIN &&
1655 snd_device < SND_DEVICE_IN_END))
1656 platform_set_tx_lpi_mode(adev->platform, true);
Kunlei Zhange7cab1a2020-05-17 20:31:05 +08001657 ALOGD("%s: enable island cfg and power mode on: %s",
1658 __func__, device_name);
1659 }
Preetam Singh Ranawatf1d417c2017-01-10 17:00:32 +05301660
Revathi Uddarajub26e3932020-06-10 14:51:02 +05301661 if (SND_DEVICE_OUT_BT_A2DP == snd_device) {
Ramjee Singh2d2944a2021-12-14 14:13:41 +05301662
1663 struct audio_usecase *usecase;
1664 struct listnode *node;
1665 /* Disable SCO Devices and enable handset mic for active input stream */
1666 list_for_each(node, &adev->usecase_list) {
1667 usecase = node_to_item(node, struct audio_usecase, list);
1668 if (usecase->stream.in && (usecase->type == PCM_CAPTURE) &&
1669 is_sco_in_device_type(&usecase->stream.in->device_list)) {
1670 ALOGD("a2dp resumed, switch bt sco mic to handset mic");
1671 reassign_device_list(&usecase->stream.in->device_list,
1672 AUDIO_DEVICE_IN_BUILTIN_MIC, "");
1673 select_devices(adev, usecase->id);
1674 }
1675 }
Revathi Uddarajub26e3932020-06-10 14:51:02 +05301676 if (audio_extn_a2dp_start_playback() < 0) {
1677 ALOGE(" fail to configure A2dp Source control path ");
1678 goto err;
1679 } else {
1680 adev->a2dp_started = true;
1681 }
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001682 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001683
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001684 if ((SND_DEVICE_IN_BT_A2DP == snd_device) &&
1685 (audio_extn_a2dp_start_capture() < 0)) {
1686 ALOGE(" fail to configure A2dp Sink control path ");
1687 goto err;
1688 }
Naresh Tannirucd2353e2016-08-19 00:37:25 +05301689
Mingshu Pang7be5b1d2020-03-04 15:24:38 +08001690 if ((SND_DEVICE_OUT_BT_SCO_SWB == snd_device) ||
1691 (SND_DEVICE_IN_BT_SCO_MIC_SWB_NREC == snd_device) ||
1692 (SND_DEVICE_IN_BT_SCO_MIC_SWB == snd_device)) {
1693 if (!adev->bt_sco_on || (audio_extn_sco_start_configuration() < 0)) {
1694 ALOGE(" fail to configure sco control path ");
1695 goto err;
1696 }
Zhou Song12c29502019-03-16 10:37:18 +08001697 }
1698
Zhou Song331c8e52019-08-26 14:16:12 +08001699 configure_btsco_sample_rate(snd_device);
Bharath Ramachandramurthy0de16782014-03-28 21:34:33 -07001700 /* due to the possibility of calibration overwrite between listen
1701 and audio, notify listen hal before audio calibration is sent */
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001702 audio_extn_sound_trigger_update_device_status(snd_device,
1703 ST_EVENT_SND_DEVICE_BUSY);
Dhananjay Kumar45b71742014-05-29 21:47:27 +05301704 audio_extn_listen_update_device_status(snd_device,
1705 LISTEN_EVENT_SND_DEVICE_BUSY);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001706 if (platform_get_snd_device_acdb_id(snd_device) < 0) {
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001707 audio_extn_sound_trigger_update_device_status(snd_device,
1708 ST_EVENT_SND_DEVICE_FREE);
Dhananjay Kumar45b71742014-05-29 21:47:27 +05301709 audio_extn_listen_update_device_status(snd_device,
1710 LISTEN_EVENT_SND_DEVICE_FREE);
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001711 goto err;
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -07001712 }
Lior Barenboim0b61bc72014-05-13 13:01:37 +03001713 audio_extn_dev_arbi_acquire(snd_device);
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07001714 audio_route_apply_and_update_path(adev->audio_route, device_name);
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301715
1716 if (SND_DEVICE_OUT_HEADPHONES == snd_device &&
1717 !adev->native_playback_enabled &&
1718 audio_is_true_native_stream_active(adev)) {
1719 ALOGD("%s: %d: napb: enabling native mode in hardware",
1720 __func__, __LINE__);
1721 audio_route_apply_and_update_path(adev->audio_route,
1722 "true-native-mode");
1723 adev->native_playback_enabled = true;
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +05301724 }
Dhanalakshmi Siddani10621622018-04-30 15:07:27 +05301725 if (((snd_device == SND_DEVICE_IN_HANDSET_6MIC) ||
1726 (snd_device == SND_DEVICE_IN_HANDSET_QMIC)) &&
Eric Laurent637e2d42018-11-15 12:24:31 -08001727 (audio_extn_ffv_get_stream() == adev_get_active_input(adev))) {
Garmond Leunge2433c32017-09-28 21:51:22 -07001728 ALOGD("%s: init ec ref loopback", __func__);
1729 audio_extn_ffv_init_ec_ref_loopback(adev, snd_device);
1730 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001731 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001732 return 0;
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001733err:
1734 adev->snd_dev_ref_cnt[snd_device]--;
1735 return -EINVAL;;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001736}
1737
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001738int disable_snd_device(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001739 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001740{
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301741 int i, num_devices = 0;
1742 snd_device_t new_snd_devices[SND_DEVICE_OUT_END];
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001743 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
1744
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001745 if (snd_device < SND_DEVICE_MIN ||
1746 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001747 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001748 return -EINVAL;
1749 }
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001750
1751 if (platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0) {
1752 ALOGE("%s: Invalid sound device returned", __func__);
1753 return -EINVAL;
1754 }
1755
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001756 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
1757 ALOGE("%s: device ref cnt is already 0", __func__);
1758 return -EINVAL;
1759 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001760
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001761 adev->snd_dev_ref_cnt[snd_device]--;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -07001762
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001763
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001764 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07001765 ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, device_name);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301766
Aalique Grahame22e49102018-12-18 14:23:57 -08001767 audio_extn_dsm_feedback_enable(adev, snd_device, false);
1768
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001769 if (platform_can_enable_spkr_prot_on_device(snd_device) &&
1770 audio_extn_spkr_prot_is_enabled()) {
Anish Kumar46c7b872014-09-09 01:49:44 -07001771 audio_extn_spkr_prot_stop_processing(snd_device);
Vignesh Kulothungan3b5fae52017-09-25 12:16:30 -07001772
1773 // when speaker device is disabled, reset swap.
1774 // will be renabled on usecase start
1775 platform_set_swap_channels(adev, false);
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001776 } else if (platform_split_snd_device(adev->platform,
1777 snd_device,
1778 &num_devices,
1779 new_snd_devices) == 0) {
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301780 for (i = 0; i < num_devices; i++) {
1781 disable_snd_device(adev, new_snd_devices[i]);
1782 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001783 platform_set_speaker_gain_in_combo(adev, snd_device, false);
Lior Barenboim0b61bc72014-05-13 13:01:37 +03001784 } else {
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07001785 audio_route_reset_and_update_path(adev->audio_route, device_name);
Lior Barenboim0b61bc72014-05-13 13:01:37 +03001786 }
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001787
Revathi Uddarajub26e3932020-06-10 14:51:02 +05301788 if (snd_device == SND_DEVICE_OUT_BT_A2DP) {
Naresh Tannirucd2353e2016-08-19 00:37:25 +05301789 audio_extn_a2dp_stop_playback();
Revathi Uddarajub26e3932020-06-10 14:51:02 +05301790 adev->a2dp_started = false;
1791 } else if (snd_device == SND_DEVICE_IN_BT_A2DP)
Florian Pfister1a84f312018-07-19 14:38:18 +02001792 audio_extn_a2dp_stop_capture();
Zhou Songd6d71752019-05-21 18:08:51 +08001793 else if ((snd_device == SND_DEVICE_OUT_HDMI) ||
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001794 (snd_device == SND_DEVICE_OUT_DISPLAY_PORT))
Ashish Jain81eb2a82015-05-13 10:52:34 +05301795 adev->is_channel_status_set = false;
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001796 else if ((snd_device == SND_DEVICE_OUT_HEADPHONES) &&
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301797 adev->native_playback_enabled) {
1798 ALOGD("%s: %d: napb: disabling native mode in hardware",
1799 __func__, __LINE__);
1800 audio_route_reset_and_update_path(adev->audio_route,
1801 "true-native-mode");
1802 adev->native_playback_enabled = false;
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001803 } else if ((snd_device == SND_DEVICE_OUT_HEADPHONES) &&
Preetam Singh Ranawatb0c0dd72016-08-18 00:32:06 +05301804 adev->asrc_mode_enabled) {
1805 ALOGD("%s: %d: disabling asrc mode in hardware", __func__, __LINE__);
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +05301806 disable_asrc_mode(adev);
1807 audio_route_apply_and_update_path(adev->audio_route, "hph-lowpower-mode");
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001808 } else if (((snd_device == SND_DEVICE_IN_HANDSET_6MIC) ||
Dhanalakshmi Siddaniaf4bd622019-02-27 16:28:06 +05301809 (snd_device == SND_DEVICE_IN_HANDSET_QMIC)) &&
Eric Laurent637e2d42018-11-15 12:24:31 -08001810 (audio_extn_ffv_get_stream() == adev_get_active_input(adev))) {
Garmond Leunge2433c32017-09-28 21:51:22 -07001811 ALOGD("%s: deinit ec ref loopback", __func__);
1812 audio_extn_ffv_deinit_ec_ref_loopback(adev, snd_device);
1813 }
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001814
1815 audio_extn_utils_release_snd_device(snd_device);
1816 } else {
1817 if (platform_split_snd_device(adev->platform,
1818 snd_device,
1819 &num_devices,
1820 new_snd_devices) == 0) {
1821 for (i = 0; i < num_devices; i++) {
1822 adev->snd_dev_ref_cnt[new_snd_devices[i]]--;
1823 }
1824 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001825 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001826
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001827 return 0;
1828}
1829
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001830/*
1831 legend:
1832 uc - existing usecase
1833 new_uc - new usecase
1834 d1, d11, d2 - SND_DEVICE enums
1835 a1, a2 - corresponding ANDROID device enums
1836 B1, B2 - backend strings
1837
1838case 1
1839 uc->dev d1 (a1) B1
1840 new_uc->dev d1 (a1), d2 (a2) B1, B2
1841
1842 resolution: disable and enable uc->dev on d1
1843
1844case 2
1845 uc->dev d1 (a1) B1
1846 new_uc->dev d11 (a1) B1
1847
1848 resolution: need to switch uc since d1 and d11 are related
1849 (e.g. speaker and voice-speaker)
1850 use ANDROID_DEVICE_OUT enums to match devices since SND_DEVICE enums may vary
1851
1852case 3
1853 uc->dev d1 (a1) B1
1854 new_uc->dev d2 (a2) B2
1855
1856 resolution: no need to switch uc
1857
1858case 4
1859 uc->dev d1 (a1) B1
1860 new_uc->dev d2 (a2) B1
1861
1862 resolution: disable enable uc-dev on d2 since backends match
1863 we cannot enable two streams on two different devices if they
1864 share the same backend. e.g. if offload is on speaker device using
1865 QUAD_MI2S backend and a low-latency stream is started on voice-handset
1866 using the same backend, offload must also be switched to voice-handset.
1867
1868case 5
1869 uc->dev d1 (a1) B1
1870 new_uc->dev d1 (a1), d2 (a2) B1
1871
1872 resolution: disable enable uc-dev on d2 since backends match
1873 we cannot enable two streams on two different devices if they
1874 share the same backend.
1875
1876case 6
1877 uc->dev d1 (a1) B1
1878 new_uc->dev d2 (a1) B2
1879
1880 resolution: no need to switch
1881
1882case 7
1883 uc->dev d1 (a1), d2 (a2) B1, B2
1884 new_uc->dev d1 (a1) B1
1885
1886 resolution: no need to switch
1887
Zhou Song4ba65882018-07-09 14:48:07 +08001888case 8
1889 uc->dev d1 (a1) B1
1890 new_uc->dev d11 (a1), d2 (a2) B1, B2
1891 resolution: compared to case 1, for this case, d1 and d11 are related
1892 then need to do the same as case 2 to siwtch to new uc
Preetam Singh Ranawatd399e6f2020-06-25 12:20:36 +05301893
1894case 9
1895 uc->dev d1 (a1), d2(a2) B1 B2
1896 new_uc->dev d1 (a1), d22 (a2) B1, B2
1897 resolution: disable enable uc-dev on d2 since backends match
1898 we cannot enable two streams on two different devices if they
1899 share the same backend. This is special case for combo use case
1900 with a2dp and sco devices which uses same backend.
1901 e.g. speaker-a2dp and speaker-btsco
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001902*/
1903static snd_device_t derive_playback_snd_device(void * platform,
1904 struct audio_usecase *uc,
1905 struct audio_usecase *new_uc,
1906 snd_device_t new_snd_device)
1907{
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001908 struct listnode a1, a2;
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001909
1910 snd_device_t d1 = uc->out_snd_device;
1911 snd_device_t d2 = new_snd_device;
1912
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001913 list_init(&a1);
1914 list_init(&a2);
1915
Siddartha Shaik31b530e2017-05-19 15:26:33 +05301916 switch (uc->type) {
Surendar Karka93cd25a2018-08-28 14:21:37 +05301917 case TRANSCODE_LOOPBACK_RX :
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001918 assign_devices(&a1, &uc->stream.inout->out_config.device_list);
1919 assign_devices(&a2, &new_uc->stream.inout->out_config.device_list);
Siddartha Shaik31b530e2017-05-19 15:26:33 +05301920 break;
1921 default :
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001922 assign_devices(&a1, &uc->stream.out->device_list);
1923 assign_devices(&a2, &new_uc->stream.out->device_list);
Siddartha Shaik31b530e2017-05-19 15:26:33 +05301924 break;
1925 }
1926
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001927 // Treat as a special case when a1 and a2 are not disjoint
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001928 if (!compare_devices(&a1, &a2) &&
1929 compare_devices_for_any_match(&a1 ,&a2)) {
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001930 snd_device_t d3[2];
1931 int num_devices = 0;
1932 int ret = platform_split_snd_device(platform,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001933 list_length(&a1) > 1 ? d1 : d2,
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001934 &num_devices,
1935 d3);
1936 if (ret < 0) {
1937 if (ret != -ENOSYS) {
1938 ALOGW("%s failed to split snd_device %d",
1939 __func__,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001940 list_length(&a1) > 1 ? d1 : d2);
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001941 }
1942 goto end;
1943 }
1944
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001945 if (platform_check_backends_match(d3[0], d3[1])) {
1946 return d2; // case 5
1947 } else {
Preetam Singh Ranawatd399e6f2020-06-25 12:20:36 +05301948 if ((list_length(&a1) > 1) && (list_length(&a2) > 1) &&
1949 platform_check_backends_match(d1, d2))
1950 return d2; //case 9
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001951 if (list_length(&a1) > 1)
Samyak Jaind826b502019-07-17 16:16:42 +05301952 return d1; //case 7
Garmond Leungb9eeba42018-09-18 11:10:41 -07001953 // check if d1 is related to any of d3's
1954 if (d1 == d3[0] || d1 == d3[1])
Zhou Song4ba65882018-07-09 14:48:07 +08001955 return d1; // case 1
1956 else
1957 return d3[1]; // case 8
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001958 }
1959 } else {
1960 if (platform_check_backends_match(d1, d2)) {
1961 return d2; // case 2, 4
1962 } else {
1963 return d1; // case 6, 3
1964 }
1965 }
1966
Zhenlin Lian4f947842022-05-14 15:50:52 +05301967 clear_devices(&a1);
1968 clear_devices(&a2);
1969
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001970end:
1971 return d2; // return whatever was calculated before.
1972}
1973
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001974static void check_usecases_codec_backend(struct audio_device *adev,
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301975 struct audio_usecase *uc_info,
1976 snd_device_t snd_device)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001977{
1978 struct listnode *node;
1979 struct audio_usecase *usecase;
1980 bool switch_device[AUDIO_USECASE_MAX];
Chaithanya Krishna Bacharaju49e7db02017-03-14 11:57:26 +05301981 snd_device_t uc_derive_snd_device;
1982 snd_device_t derive_snd_device[AUDIO_USECASE_MAX];
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001983 snd_device_t split_snd_devices[SND_DEVICE_OUT_END];
1984 int i, num_uc_to_switch = 0, num_devices = 0;
kunleiz5cd52b82016-11-07 17:22:52 +08001985 int status = 0;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301986 bool force_restart_session = false;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001987 /*
1988 * This function is to make sure that all the usecases that are active on
1989 * the hardware codec backend are always routed to any one device that is
1990 * handled by the hardware codec.
1991 * For example, if low-latency and deep-buffer usecases are currently active
1992 * on speaker and out_set_parameters(headset) is received on low-latency
1993 * output, then we have to make sure deep-buffer is also switched to headset,
1994 * because of the limitation that both the devices cannot be enabled
1995 * at the same time as they share the same backend.
1996 */
Mingming Yin3ee55c62014-08-04 14:23:35 -07001997 /*
1998 * This call is to check if we need to force routing for a particular stream
1999 * If there is a backend configuration change for the device when a
2000 * new stream starts, then ADM needs to be closed and re-opened with the new
2001 * configuraion. This call check if we need to re-route all the streams
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -08002002 * associated with the backend. Touch tone + 24 bit + native playback.
Mingming Yin3ee55c62014-08-04 14:23:35 -07002003 */
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -08002004 bool force_routing = platform_check_and_set_codec_backend_cfg(adev, uc_info,
2005 snd_device);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302006 /* For a2dp device reconfigure all active sessions
2007 * with new AFE encoder format based on a2dp state
2008 */
2009 if ((SND_DEVICE_OUT_BT_A2DP == snd_device ||
Preetam Singh Ranawatd399e6f2020-06-25 12:20:36 +05302010 SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP == snd_device ||
2011 SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP == snd_device) &&
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302012 audio_extn_a2dp_is_force_device_switch()) {
2013 force_routing = true;
2014 force_restart_session = true;
2015 }
Kunlei Zhange7cab1a2020-05-17 20:31:05 +08002016
2017 /*
2018 * Island cfg and power mode config needs to set before AFE port start.
2019 * Set force routing in case of voice device was enable before.
2020 */
2021 if (uc_info->type == VOICE_CALL &&
2022 voice_extn_is_voice_power_mode_supported() &&
Kunlei Zhang67cc7072020-12-18 17:16:49 +08002023 is_supported_conc_usecase_for_power_mode_call(adev) &&
Kunlei Zhange7cab1a2020-05-17 20:31:05 +08002024 platform_check_and_update_island_power_status(adev->platform,
2025 uc_info,
2026 snd_device)) {
2027 force_routing = true;
2028 ALOGD("%s:becf: force routing %d for power mode supported device",
2029 __func__, force_routing);
2030 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302031 ALOGD("%s:becf: force routing %d", __func__, force_routing);
2032
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002033 /* Disable all the usecases on the shared backend other than the
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -08002034 * specified usecase.
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -08002035 */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002036 for (i = 0; i < AUDIO_USECASE_MAX; i++)
2037 switch_device[i] = false;
2038
2039 list_for_each(node, &adev->usecase_list) {
2040 usecase = node_to_item(node, struct audio_usecase, list);
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -08002041
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05302042 ALOGD("%s:becf: (%d) check_usecases curr device: %s, usecase device:%s "
2043 "backends match %d",__func__, i,
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302044 platform_get_snd_device_name(snd_device),
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05302045 platform_get_snd_device_name(usecase->out_snd_device),
2046 platform_check_backends_match(snd_device, usecase->out_snd_device));
Rahul Sharma99770982019-03-06 17:05:26 +05302047 if ((usecase->type != PCM_CAPTURE) && (usecase != uc_info) &&
2048 (usecase->type != PCM_PASSTHROUGH)) {
Ashish Jain6a65b352017-03-21 17:24:40 +05302049 uc_derive_snd_device = derive_playback_snd_device(adev->platform,
2050 usecase, uc_info, snd_device);
2051 if (((uc_derive_snd_device != usecase->out_snd_device) || force_routing) &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002052 (is_codec_backend_out_device_type(&usecase->device_list) ||
2053 compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_AUX_DIGITAL) ||
2054 compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_USB_DEVICE) ||
2055 compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_USB_HEADSET) ||
2056 is_a2dp_out_device_type(&usecase->device_list) ||
2057 is_sco_out_device_type(&usecase->device_list)) &&
Ashish Jain6a65b352017-03-21 17:24:40 +05302058 ((force_restart_session) ||
2059 (platform_check_backends_match(snd_device, usecase->out_snd_device)))) {
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05302060 ALOGD("%s:becf: check_usecases (%s) is active on (%s) - disabling ..",
2061 __func__, use_case_table[usecase->id],
2062 platform_get_snd_device_name(usecase->out_snd_device));
2063 disable_audio_route(adev, usecase);
2064 switch_device[usecase->id] = true;
Chaithanya Krishna Bacharaju49e7db02017-03-14 11:57:26 +05302065 /* Enable existing usecase on derived playback device */
2066 derive_snd_device[usecase->id] = uc_derive_snd_device;
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05302067 num_uc_to_switch++;
Ashish Jain6a65b352017-03-21 17:24:40 +05302068 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002069 }
2070 }
2071
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302072 ALOGD("%s:becf: check_usecases num.of Usecases to switch %d", __func__,
2073 num_uc_to_switch);
2074
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002075 if (num_uc_to_switch) {
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07002076 /* All streams have been de-routed. Disable the device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002077
Venkata Narendra Kumar Gutta7610e632014-04-14 23:16:38 +05302078 /* Make sure the previous devices to be disabled first and then enable the
2079 selected devices */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002080 list_for_each(node, &adev->usecase_list) {
2081 usecase = node_to_item(node, struct audio_usecase, list);
2082 if (switch_device[usecase->id]) {
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08002083 /* Check if output sound device to be switched can be split and if any
2084 of the split devices match with derived sound device */
2085 if (platform_split_snd_device(adev->platform, usecase->out_snd_device,
2086 &num_devices, split_snd_devices) == 0) {
2087 adev->snd_dev_ref_cnt[usecase->out_snd_device]--;
2088 for (i = 0; i < num_devices; i++) {
2089 /* Disable devices that do not match with derived sound device */
2090 if (split_snd_devices[i] != derive_snd_device[usecase->id])
2091 disable_snd_device(adev, split_snd_devices[i]);
2092 }
2093 } else {
2094 disable_snd_device(adev, usecase->out_snd_device);
2095 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002096 }
2097 }
2098
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -07002099 list_for_each(node, &adev->usecase_list) {
2100 usecase = node_to_item(node, struct audio_usecase, list);
2101 if (switch_device[usecase->id]) {
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08002102 if (platform_split_snd_device(adev->platform, usecase->out_snd_device,
2103 &num_devices, split_snd_devices) == 0) {
2104 /* Enable derived sound device only if it does not match with
2105 one of the split sound devices. This is because the matching
2106 sound device was not disabled */
2107 bool should_enable = true;
2108 for (i = 0; i < num_devices; i++) {
2109 if (derive_snd_device[usecase->id] == split_snd_devices[i]) {
2110 should_enable = false;
2111 break;
2112 }
2113 }
2114 if (should_enable)
2115 enable_snd_device(adev, derive_snd_device[usecase->id]);
2116 } else {
2117 enable_snd_device(adev, derive_snd_device[usecase->id]);
2118 }
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -07002119 }
2120 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002121
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002122 /* Re-route all the usecases on the shared backend other than the
2123 specified usecase to new snd devices */
2124 list_for_each(node, &adev->usecase_list) {
2125 usecase = node_to_item(node, struct audio_usecase, list);
Karthik Reddy Katta3abfee22016-02-23 10:55:27 +05302126 /* Update the out_snd_device only before enabling the audio route */
2127 if (switch_device[usecase->id]) {
Chaithanya Krishna Bacharaju49e7db02017-03-14 11:57:26 +05302128 usecase->out_snd_device = derive_snd_device[usecase->id];
Lakshman Chaluvarajuab7e5572020-10-28 19:29:23 +05302129 ALOGD("%s:becf: enabling usecase (%s) on (%s)", __func__,
2130 use_case_table[usecase->id],
2131 platform_get_snd_device_name(usecase->out_snd_device));
2132 /* Update voc calibration before enabling Voice/VoIP route */
2133 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL)
2134 status = platform_switch_voice_call_device_post(adev->platform,
2135 usecase->out_snd_device,
2136 platform_get_input_snd_device(
2137 adev->platform, NULL,
2138 &uc_info->device_list,
2139 usecase->type));
2140 enable_audio_route(adev, usecase);
2141 if (usecase->stream.out && usecase->id == USECASE_AUDIO_PLAYBACK_VOIP) {
2142 out_set_voip_volume(&usecase->stream.out->stream,
2143 usecase->stream.out->volume_l,
2144 usecase->stream.out->volume_r);
Karthik Reddy Katta3abfee22016-02-23 10:55:27 +05302145 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002146 }
2147 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002148 }
2149}
2150
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05302151static void check_usecases_capture_codec_backend(struct audio_device *adev,
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002152 struct audio_usecase *uc_info,
2153 snd_device_t snd_device)
2154{
2155 struct listnode *node;
2156 struct audio_usecase *usecase;
2157 bool switch_device[AUDIO_USECASE_MAX];
2158 int i, num_uc_to_switch = 0;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002159 int backend_check_cond = is_codec_backend_out_device_type(&uc_info->device_list);
kunleiz5cd52b82016-11-07 17:22:52 +08002160 int status = 0;
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002161
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05302162 bool force_routing = platform_check_and_set_capture_codec_backend_cfg(adev, uc_info,
2163 snd_device);
2164 ALOGD("%s:becf: force routing %d", __func__, force_routing);
Dhanalakshmi Siddanib678a802016-12-03 11:51:41 +05302165
2166 /*
2167 * Make sure out devices is checked against out codec backend device and
2168 * also in devices against in codec backend. Checking out device against in
2169 * codec backend or vice versa causes issues.
2170 */
2171 if (uc_info->type == PCM_CAPTURE)
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002172 backend_check_cond = is_codec_backend_in_device_type(&uc_info->device_list);
Kunlei Zhange7cab1a2020-05-17 20:31:05 +08002173
2174 /*
2175 * Island cfg and power mode config needs to set before AFE port start.
2176 * Set force routing in case of voice device was enable before.
2177 */
2178
2179 if (uc_info->type == VOICE_CALL &&
2180 voice_extn_is_voice_power_mode_supported() &&
Kunlei Zhang67cc7072020-12-18 17:16:49 +08002181 is_supported_conc_usecase_for_power_mode_call(adev) &&
Kunlei Zhange7cab1a2020-05-17 20:31:05 +08002182 platform_check_and_update_island_power_status(adev->platform,
2183 uc_info,
2184 snd_device)) {
2185 force_routing = true;
2186 ALOGD("%s:becf: force routing %d for power mode supported device",
2187 __func__, force_routing);
2188 }
2189
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002190 /*
2191 * This function is to make sure that all the active capture usecases
2192 * are always routed to the same input sound device.
2193 * For example, if audio-record and voice-call usecases are currently
2194 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
2195 * is received for voice call then we have to make sure that audio-record
2196 * usecase is also switched to earpiece i.e. voice-dmic-ef,
2197 * because of the limitation that two devices cannot be enabled
2198 * at the same time if they share the same backend.
2199 */
2200 for (i = 0; i < AUDIO_USECASE_MAX; i++)
2201 switch_device[i] = false;
2202
2203 list_for_each(node, &adev->usecase_list) {
2204 usecase = node_to_item(node, struct audio_usecase, list);
Dhanalakshmi Siddanib678a802016-12-03 11:51:41 +05302205 /*
2206 * TODO: Enhance below condition to handle BT sco/USB multi recording
2207 */
Jaideep Sharma477917f2020-03-13 18:13:33 +05302208
2209 bool capture_uc_needs_routing = usecase->type != PCM_PLAYBACK && (usecase != uc_info &&
2210 (usecase->in_snd_device != snd_device || force_routing));
2211 bool call_proxy_snd_device = platform_is_call_proxy_snd_device(snd_device) ||
2212 platform_is_call_proxy_snd_device(usecase->in_snd_device);
2213 if (capture_uc_needs_routing && !call_proxy_snd_device &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002214 ((backend_check_cond &&
2215 (is_codec_backend_in_device_type(&usecase->device_list) ||
Aniket Kumar Lata9c2fd892020-01-22 22:20:00 -08002216 (usecase->type == VOIP_CALL))) ||
Carter Hsu1d2a0532018-10-04 09:24:36 +08002217 ((uc_info->type == VOICE_CALL &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002218 is_single_device_type_equal(&usecase->device_list,
2219 AUDIO_DEVICE_IN_VOICE_CALL)) ||
Anver sadhique4fdc6992022-01-21 12:26:28 +05302220 platform_check_all_backends_match(snd_device,\
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002221 usecase->in_snd_device))) &&
Anish Kumara020a7c2014-10-17 11:13:22 -07002222 (usecase->id != USECASE_AUDIO_SPKR_CALIB_TX)) {
Jaideep Sharma477917f2020-03-13 18:13:33 +05302223 ALOGD("%s: Usecase (%s) is active on (%s) - disabling ..",
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002224 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -07002225 platform_get_snd_device_name(usecase->in_snd_device));
Haynes Mathew George1376ca62014-04-24 11:55:48 -07002226 disable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002227 switch_device[usecase->id] = true;
2228 num_uc_to_switch++;
2229 }
2230 }
2231
2232 if (num_uc_to_switch) {
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07002233 /* All streams have been de-routed. Disable the device */
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002234
Venkata Narendra Kumar Gutta7610e632014-04-14 23:16:38 +05302235 /* Make sure the previous devices to be disabled first and then enable the
2236 selected devices */
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002237 list_for_each(node, &adev->usecase_list) {
2238 usecase = node_to_item(node, struct audio_usecase, list);
2239 if (switch_device[usecase->id]) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07002240 disable_snd_device(adev, usecase->in_snd_device);
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -08002241 }
2242 }
2243
2244 list_for_each(node, &adev->usecase_list) {
2245 usecase = node_to_item(node, struct audio_usecase, list);
2246 if (switch_device[usecase->id]) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07002247 enable_snd_device(adev, snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002248 }
2249 }
2250
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002251 /* Re-route all the usecases on the shared backend other than the
2252 specified usecase to new snd devices */
2253 list_for_each(node, &adev->usecase_list) {
2254 usecase = node_to_item(node, struct audio_usecase, list);
2255 /* Update the in_snd_device only before enabling the audio route */
2256 if (switch_device[usecase->id] ) {
2257 usecase->in_snd_device = snd_device;
Lakshman Chaluvarajuab7e5572020-10-28 19:29:23 +05302258 /* Update voc calibration before enabling Voice/VoIP route */
2259 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) {
2260 snd_device_t voip_snd_device;
2261 voip_snd_device = platform_get_output_snd_device(adev->platform,
2262 usecase->stream.out,
2263 usecase->type);
2264 status = platform_switch_voice_call_device_post(adev->platform,
2265 voip_snd_device,
2266 usecase->in_snd_device);
kunleiz5cd52b82016-11-07 17:22:52 +08002267 }
Lakshman Chaluvarajuab7e5572020-10-28 19:29:23 +05302268 enable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002269 }
2270 }
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002271 }
2272}
2273
Mingming Yin3a941d42016-02-17 18:08:05 -08002274static void reset_hdmi_sink_caps(struct stream_out *out) {
2275 int i = 0;
2276
2277 for (i = 0; i<= MAX_SUPPORTED_CHANNEL_MASKS; i++) {
2278 out->supported_channel_masks[i] = 0;
2279 }
2280 for (i = 0; i<= MAX_SUPPORTED_FORMATS; i++) {
2281 out->supported_formats[i] = 0;
2282 }
2283 for (i = 0; i<= MAX_SUPPORTED_SAMPLE_RATES; i++) {
2284 out->supported_sample_rates[i] = 0;
2285 }
2286}
2287
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002288/* must be called with hw device mutex locked */
Mingming Yin3a941d42016-02-17 18:08:05 -08002289static int read_hdmi_sink_caps(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002290{
Mingming Yin3a941d42016-02-17 18:08:05 -08002291 int ret = 0, i = 0, j = 0;
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07002292 int channels = platform_edid_get_max_channels_v2(out->dev->platform,
2293 out->extconn.cs.controller,
2294 out->extconn.cs.stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002295
Mingming Yin3a941d42016-02-17 18:08:05 -08002296 reset_hdmi_sink_caps(out);
2297
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07002298 /* Cache ext disp type */
Vignesh Kulothungan1b3957e2019-12-02 16:16:51 -08002299 ret = platform_get_ext_disp_type_v2(adev->platform,
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07002300 out->extconn.cs.controller,
Vignesh Kulothungan1b3957e2019-12-02 16:16:51 -08002301 out->extconn.cs.stream);
2302 if(ret < 0) {
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07002303 ALOGE("%s: Failed to query disp type, ret:%d", __func__, ret);
Garmond Leung37850ab2016-10-06 11:42:18 -07002304 return -EINVAL;
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07002305 }
2306
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002307 switch (channels) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002308 case 8:
Mingming Yin3a941d42016-02-17 18:08:05 -08002309 ALOGV("%s: HDMI supports 7.1 channels", __func__);
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -07002310 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_7POINT1;
Mingming Yin3a941d42016-02-17 18:08:05 -08002311 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_6POINT1;
2312 case 6:
2313 ALOGV("%s: HDMI supports 5.1 channels", __func__);
2314 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_5POINT1;
2315 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_PENTA;
2316 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_QUAD;
2317 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_SURROUND;
2318 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_2POINT1;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002319 break;
2320 default:
Mingming Yin3a941d42016-02-17 18:08:05 -08002321 ALOGE("invalid/nonstandard channal count[%d]",channels);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002322 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002323 break;
2324 }
Mingming Yin3a941d42016-02-17 18:08:05 -08002325
2326 // check channel format caps
2327 i = 0;
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07002328 if (platform_is_edid_supported_format_v2(out->dev->platform, AUDIO_FORMAT_AC3,
2329 out->extconn.cs.controller,
2330 out->extconn.cs.stream)) {
Mingming Yin3a941d42016-02-17 18:08:05 -08002331 ALOGV(":%s HDMI supports AC3/EAC3 formats", __func__);
2332 out->supported_formats[i++] = AUDIO_FORMAT_AC3;
2333 //Adding EAC3/EAC3_JOC formats if AC3 is supported by the sink.
2334 //EAC3/EAC3_JOC will be converted to AC3 for decoding if needed
2335 out->supported_formats[i++] = AUDIO_FORMAT_E_AC3;
2336 out->supported_formats[i++] = AUDIO_FORMAT_E_AC3_JOC;
2337 }
2338
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07002339 if (platform_is_edid_supported_format_v2(out->dev->platform, AUDIO_FORMAT_DOLBY_TRUEHD,
2340 out->extconn.cs.controller,
2341 out->extconn.cs.stream)) {
Ben Romberger1aaaf862017-04-06 17:49:46 -07002342 ALOGV(":%s HDMI supports TRUE HD format", __func__);
2343 out->supported_formats[i++] = AUDIO_FORMAT_DOLBY_TRUEHD;
2344 }
2345
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07002346 if (platform_is_edid_supported_format_v2(out->dev->platform, AUDIO_FORMAT_DTS,
2347 out->extconn.cs.controller,
2348 out->extconn.cs.stream)) {
Mingming Yin3a941d42016-02-17 18:08:05 -08002349 ALOGV(":%s HDMI supports DTS format", __func__);
2350 out->supported_formats[i++] = AUDIO_FORMAT_DTS;
2351 }
2352
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07002353 if (platform_is_edid_supported_format_v2(out->dev->platform, AUDIO_FORMAT_DTS_HD,
2354 out->extconn.cs.controller,
2355 out->extconn.cs.stream)) {
Mingming Yin3a941d42016-02-17 18:08:05 -08002356 ALOGV(":%s HDMI supports DTS HD format", __func__);
2357 out->supported_formats[i++] = AUDIO_FORMAT_DTS_HD;
2358 }
2359
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07002360 if (platform_is_edid_supported_format_v2(out->dev->platform, AUDIO_FORMAT_IEC61937,
2361 out->extconn.cs.controller,
2362 out->extconn.cs.stream)) {
Naresh Tanniru928f0862017-04-07 16:44:23 -07002363 ALOGV(":%s HDMI supports IEC61937 format", __func__);
2364 out->supported_formats[i++] = AUDIO_FORMAT_IEC61937;
2365 }
2366
Mingming Yin3a941d42016-02-17 18:08:05 -08002367
2368 // check sample rate caps
2369 i = 0;
2370 for (j = 0; j < MAX_SUPPORTED_SAMPLE_RATES; j++) {
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07002371 if (platform_is_edid_supported_sample_rate_v2(out->dev->platform, out_hdmi_sample_rates[j],
2372 out->extconn.cs.controller,
2373 out->extconn.cs.stream)) {
Mingming Yin3a941d42016-02-17 18:08:05 -08002374 ALOGV(":%s HDMI supports sample rate:%d", __func__, out_hdmi_sample_rates[j]);
2375 out->supported_sample_rates[i++] = out_hdmi_sample_rates[j];
2376 }
2377 }
2378
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002379 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002380}
2381
Haynes Mathew George484e8d22017-07-31 18:55:17 -07002382static inline ssize_t read_usb_sup_sample_rates(bool is_playback __unused,
2383 uint32_t *supported_sample_rates __unused,
2384 uint32_t max_rates __unused)
2385{
2386 ssize_t count = audio_extn_usb_get_sup_sample_rates(is_playback,
2387 supported_sample_rates,
2388 max_rates);
Ashish Jain4847e9d2017-08-17 19:16:57 +05302389 ssize_t i = 0;
2390
2391 for (i=0; i<count; i++) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07002392 ALOGV("%s %s %d", __func__, is_playback ? "P" : "C",
2393 supported_sample_rates[i]);
2394 }
2395 return count;
2396}
2397
2398static inline int read_usb_sup_channel_masks(bool is_playback,
2399 audio_channel_mask_t *supported_channel_masks,
2400 uint32_t max_masks)
2401{
2402 int channels = audio_extn_usb_get_max_channels(is_playback);
2403 int channel_count;
2404 uint32_t num_masks = 0;
2405 if (channels > MAX_HIFI_CHANNEL_COUNT)
2406 channels = MAX_HIFI_CHANNEL_COUNT;
2407
2408 if (is_playback) {
Eric Laurent68a87112019-05-01 18:07:29 -07002409 // start from 2 channels as framework currently doesn't support mono.
2410 if (channels >= FCC_2) {
2411 supported_channel_masks[num_masks++] = audio_channel_out_mask_from_count(FCC_2);
2412 }
2413 for (channel_count = FCC_2;
2414 channel_count <= channels && num_masks < max_masks;
2415 ++channel_count) {
2416 supported_channel_masks[num_masks++] =
2417 audio_channel_mask_for_index_assignment_from_count(channel_count);
2418 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07002419 } else {
vincenttewf51c94e2019-05-07 10:28:53 +08002420 // For capture we report all supported channel masks from 1 channel up.
2421 channel_count = MIN_CHANNEL_COUNT;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07002422 // audio_channel_in_mask_from_count() does the right conversion to either positional or
2423 // indexed mask
Eric Laurent68a87112019-05-01 18:07:29 -07002424 for ( ; channel_count <= channels && num_masks < max_masks; channel_count++) {
2425 audio_channel_mask_t mask = AUDIO_CHANNEL_NONE;
2426 if (channel_count <= FCC_2) {
2427 mask = audio_channel_in_mask_from_count(channel_count);
2428 supported_channel_masks[num_masks++] = mask;
2429 }
2430 const audio_channel_mask_t index_mask =
2431 audio_channel_mask_for_index_assignment_from_count(channel_count);
2432 if (mask != index_mask && num_masks < max_masks) { // ensure index mask added.
2433 supported_channel_masks[num_masks++] = index_mask;
2434 }
2435 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07002436 }
Lakshman Chaluvarajub79fafd2018-11-21 10:24:37 +05302437
vincenttewf51c94e2019-05-07 10:28:53 +08002438 for (size_t i = 0; i < num_masks; ++i) {
2439 ALOGV("%s: %s supported ch %d supported_channel_masks[%zu] %08x num_masks %d", __func__,
2440 is_playback ? "P" : "C", channels, i, supported_channel_masks[i], num_masks);
Lakshman Chaluvarajub79fafd2018-11-21 10:24:37 +05302441 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07002442 return num_masks;
2443}
2444
2445static inline int read_usb_sup_formats(bool is_playback __unused,
2446 audio_format_t *supported_formats,
2447 uint32_t max_formats __unused)
2448{
2449 int bitwidth = audio_extn_usb_get_max_bit_width(is_playback);
2450 switch (bitwidth) {
2451 case 24:
2452 // XXX : usb.c returns 24 for s24 and s24_le?
2453 supported_formats[0] = AUDIO_FORMAT_PCM_24_BIT_PACKED;
2454 break;
2455 case 32:
2456 supported_formats[0] = AUDIO_FORMAT_PCM_32_BIT;
2457 break;
2458 case 16:
2459 default :
2460 supported_formats[0] = AUDIO_FORMAT_PCM_16_BIT;
2461 break;
2462 }
2463 ALOGV("%s: %s supported format %d", __func__,
2464 is_playback ? "P" : "C", bitwidth);
2465 return 1;
2466}
2467
2468static inline int read_usb_sup_params_and_compare(bool is_playback,
2469 audio_format_t *format,
2470 audio_format_t *supported_formats,
2471 uint32_t max_formats,
2472 audio_channel_mask_t *mask,
2473 audio_channel_mask_t *supported_channel_masks,
2474 uint32_t max_masks,
2475 uint32_t *rate,
2476 uint32_t *supported_sample_rates,
2477 uint32_t max_rates) {
2478 int ret = 0;
2479 int num_formats;
2480 int num_masks;
2481 int num_rates;
2482 int i;
2483
2484 num_formats = read_usb_sup_formats(is_playback, supported_formats,
2485 max_formats);
2486 num_masks = read_usb_sup_channel_masks(is_playback, supported_channel_masks,
2487 max_masks);
2488
2489 num_rates = read_usb_sup_sample_rates(is_playback,
2490 supported_sample_rates, max_rates);
2491
2492#define LUT(table, len, what, dflt) \
2493 for (i=0; i<len && (table[i] != what); i++); \
2494 if (i==len) { ret |= (what == dflt ? 0 : -1); what=table[0]; }
2495
2496 LUT(supported_formats, num_formats, *format, AUDIO_FORMAT_DEFAULT);
2497 LUT(supported_channel_masks, num_masks, *mask, AUDIO_CHANNEL_NONE);
2498 LUT(supported_sample_rates, num_rates, *rate, 0);
2499
2500#undef LUT
2501 return ret < 0 ? -EINVAL : 0; // HACK TBD
2502}
2503
Alexy Josephb1379942016-01-29 15:49:38 -08002504audio_usecase_t get_usecase_id_from_usecase_type(const struct audio_device *adev,
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -08002505 usecase_type_t type)
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002506{
2507 struct audio_usecase *usecase;
2508 struct listnode *node;
2509
2510 list_for_each(node, &adev->usecase_list) {
2511 usecase = node_to_item(node, struct audio_usecase, list);
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -08002512 if (usecase->type == type) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002513 ALOGV("%s: usecase id %d", __func__, usecase->id);
2514 return usecase->id;
2515 }
2516 }
2517 return USECASE_INVALID;
2518}
2519
Alexy Josephb1379942016-01-29 15:49:38 -08002520struct audio_usecase *get_usecase_from_list(const struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002521 audio_usecase_t uc_id)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002522{
2523 struct audio_usecase *usecase;
2524 struct listnode *node;
2525
2526 list_for_each(node, &adev->usecase_list) {
2527 usecase = node_to_item(node, struct audio_usecase, list);
2528 if (usecase->id == uc_id)
2529 return usecase;
2530 }
2531 return NULL;
2532}
2533
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302534/*
2535 * is a true native playback active
2536 */
2537bool audio_is_true_native_stream_active(struct audio_device *adev)
2538{
2539 bool active = false;
2540 int i = 0;
2541 struct listnode *node;
2542
2543 if (NATIVE_AUDIO_MODE_TRUE_44_1 != platform_get_native_support()) {
2544 ALOGV("%s:napb: not in true mode or non hdphones device",
2545 __func__);
2546 active = false;
2547 goto exit;
2548 }
2549
2550 list_for_each(node, &adev->usecase_list) {
2551 struct audio_usecase *uc;
2552 uc = node_to_item(node, struct audio_usecase, list);
2553 struct stream_out *curr_out =
2554 (struct stream_out*) uc->stream.out;
2555
2556 if (curr_out && PCM_PLAYBACK == uc->type) {
2557 ALOGD("%s:napb: (%d) (%s)id (%d) sr %d bw "
2558 "(%d) device %s", __func__, i++, use_case_table[uc->id],
2559 uc->id, curr_out->sample_rate,
2560 curr_out->bit_width,
2561 platform_get_snd_device_name(uc->out_snd_device));
2562
2563 if (is_offload_usecase(uc->id) &&
2564 (curr_out->sample_rate == OUTPUT_SAMPLING_RATE_44100)) {
2565 active = true;
2566 ALOGD("%s:napb:native stream detected", __func__);
2567 }
2568 }
2569 }
2570exit:
2571 return active;
2572}
2573
Xiaojun Sang785b5da2017-08-03 15:52:29 +08002574uint32_t adev_get_dsp_bit_width_enforce_mode()
2575{
2576 if (adev == NULL) {
2577 ALOGE("%s: adev is null. Disable DSP bit width enforce mode.\n", __func__);
2578 return 0;
2579 }
2580 return adev->dsp_bit_width_enforce_mode;
2581}
2582
2583static uint32_t adev_init_dsp_bit_width_enforce_mode(struct mixer *mixer)
2584{
2585 char value[PROPERTY_VALUE_MAX];
2586 int trial;
2587 uint32_t dsp_bit_width_enforce_mode = 0;
2588
2589 if (!mixer) {
2590 ALOGE("%s: adev mixer is null. cannot update DSP bitwidth.\n",
2591 __func__);
2592 return 0;
2593 }
2594
2595 if (property_get("persist.vendor.audio_hal.dsp_bit_width_enforce_mode",
2596 value, NULL) > 0) {
2597 trial = atoi(value);
2598 switch (trial) {
2599 case 16:
2600 dsp_bit_width_enforce_mode = 16;
2601 break;
2602 case 24:
2603 dsp_bit_width_enforce_mode = 24;
2604 break;
2605 case 32:
2606 dsp_bit_width_enforce_mode = 32;
2607 break;
2608 default:
2609 dsp_bit_width_enforce_mode = 0;
2610 ALOGD("%s Dynamic DSP bitwidth config is disabled.", __func__);
2611 break;
2612 }
2613 }
2614
2615 return dsp_bit_width_enforce_mode;
2616}
2617
2618static void audio_enable_asm_bit_width_enforce_mode(struct mixer *mixer,
2619 uint32_t enforce_mode,
2620 bool enable)
2621{
2622 struct mixer_ctl *ctl = NULL;
2623 const char *mixer_ctl_name = "ASM Bit Width";
2624 uint32_t asm_bit_width_mode = 0;
2625
2626 if (enforce_mode == 0) {
2627 ALOGD("%s: DSP bitwidth feature is disabled.", __func__);
2628 return;
2629 }
2630
2631 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
2632 if (!ctl) {
2633 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2634 __func__, mixer_ctl_name);
2635 return;
2636 }
2637
2638 if (enable)
2639 asm_bit_width_mode = enforce_mode;
2640 else
2641 asm_bit_width_mode = 0;
2642
2643 ALOGV("%s DSP bit width feature status is %d width=%d",
2644 __func__, enable, asm_bit_width_mode);
2645 if (mixer_ctl_set_value(ctl, 0, asm_bit_width_mode) < 0)
2646 ALOGE("%s: Could not set ASM biwidth %d", __func__,
2647 asm_bit_width_mode);
2648
2649 return;
2650}
2651
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05302652/*
2653 * if native DSD playback active
2654 */
2655bool audio_is_dsd_native_stream_active(struct audio_device *adev)
2656{
2657 bool active = false;
2658 struct listnode *node = NULL;
2659 struct audio_usecase *uc = NULL;
2660 struct stream_out *curr_out = NULL;
2661
2662 list_for_each(node, &adev->usecase_list) {
2663 uc = node_to_item(node, struct audio_usecase, list);
2664 curr_out = (struct stream_out*) uc->stream.out;
2665
2666 if (curr_out && PCM_PLAYBACK == uc->type &&
2667 (DSD_NATIVE_BACKEND == platform_get_backend_index(uc->out_snd_device))) {
2668 active = true;
2669 ALOGV("%s:DSD playback is active", __func__);
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05302670 break;
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05302671 }
2672 }
2673 return active;
2674}
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302675
2676static bool force_device_switch(struct audio_usecase *usecase)
2677{
2678 bool ret = false;
2679 bool is_it_true_mode = false;
2680
Zhou Song30f2c3e2018-02-08 14:02:15 +08002681 if (usecase->type == PCM_CAPTURE ||
Surendar Karka93cd25a2018-08-28 14:21:37 +05302682 usecase->type == TRANSCODE_LOOPBACK_RX ||
2683 usecase->type == TRANSCODE_LOOPBACK_TX) {
Zhou Song30f2c3e2018-02-08 14:02:15 +08002684 return false;
2685 }
2686
Aalique Grahamecbc46a22017-10-05 10:30:23 -07002687 if(usecase->stream.out == NULL) {
2688 ALOGE("%s: stream.out is NULL", __func__);
2689 return false;
2690 }
2691
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302692 if (is_offload_usecase(usecase->id) &&
Xiaojun Sang869f2012016-02-23 16:33:07 +08002693 (usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002694 (compare_device_type(&usecase->stream.out->device_list, AUDIO_DEVICE_OUT_WIRED_HEADSET) ||
2695 compare_device_type(&usecase->stream.out->device_list, AUDIO_DEVICE_OUT_WIRED_HEADPHONE))) {
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302696 is_it_true_mode = (NATIVE_AUDIO_MODE_TRUE_44_1 == platform_get_native_support()? true : false);
2697 if ((is_it_true_mode && !adev->native_playback_enabled) ||
2698 (!is_it_true_mode && adev->native_playback_enabled)){
2699 ret = true;
2700 ALOGD("napb: time to toggle native mode");
2701 }
2702 }
2703
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302704 // Force all a2dp output devices to reconfigure for proper AFE encode format
Ashish Jainc597d102016-12-12 10:31:34 +05302705 //Also handle a case where in earlier a2dp start failed as A2DP stream was
2706 //in suspended state, hence try to trigger a retry when we again get a routing request.
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002707 if(is_a2dp_out_device_type(&usecase->stream.out->device_list) &&
Ashish Jainc597d102016-12-12 10:31:34 +05302708 audio_extn_a2dp_is_force_device_switch()) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302709 ALOGD("Force a2dp device switch to update new encoder config");
2710 ret = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002711 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302712
Florian Pfister1a84f312018-07-19 14:38:18 +02002713 if (usecase->stream.out->stream_config_changed) {
Manish Dewangan671a4202017-08-18 17:30:46 +05302714 ALOGD("Force stream_config_changed to update iec61937 transmission config");
2715 return true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002716 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302717 return ret;
2718}
2719
Aalique Grahame22e49102018-12-18 14:23:57 -08002720static void stream_app_type_cfg_init(struct stream_app_type_cfg *cfg)
2721{
2722 cfg->gain[0] = cfg->gain[1] = APP_TYPE_GAIN_DEFAULT;
2723}
2724
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302725bool is_btsco_device(snd_device_t out_snd_device, snd_device_t in_snd_device)
2726{
2727 bool ret=false;
2728 if ((out_snd_device == SND_DEVICE_OUT_BT_SCO ||
Zhou Song5657f492019-08-07 11:30:39 +08002729 out_snd_device == SND_DEVICE_OUT_BT_SCO_WB ||
2730 out_snd_device == SND_DEVICE_OUT_BT_SCO_SWB) ||
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302731 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_WB_NREC ||
2732 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_WB ||
Zhou Song5657f492019-08-07 11:30:39 +08002733 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_SWB ||
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302734 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_NREC ||
Mingshu Pang16093502020-04-20 11:21:16 +08002735 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC ||
2736 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_SWB_NREC)
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302737 ret = true;
2738
2739 return ret;
2740}
2741
2742bool is_a2dp_device(snd_device_t out_snd_device)
2743{
2744 bool ret=false;
2745 if (out_snd_device == SND_DEVICE_OUT_BT_A2DP)
2746 ret = true;
2747
2748 return ret;
2749}
2750
2751bool is_bt_soc_on(struct audio_device *adev)
2752{
2753 struct mixer_ctl *ctl;
2754 char *mixer_ctl_name = "BT SOC status";
2755 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
2756 bool bt_soc_status = true;
2757 if (!ctl) {
2758 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2759 __func__, mixer_ctl_name);
2760 /*This is to ensure we dont break targets which dont have the kernel change*/
2761 return true;
2762 }
2763 bt_soc_status = mixer_ctl_get_value(ctl, 0);
2764 ALOGD("BT SOC status: %d",bt_soc_status);
2765 return bt_soc_status;
2766}
2767
Zhou Song331c8e52019-08-26 14:16:12 +08002768static int configure_btsco_sample_rate(snd_device_t snd_device)
2769{
2770 struct mixer_ctl *ctl = NULL;
2771 struct mixer_ctl *ctl_sr_rx = NULL, *ctl_sr_tx = NULL, *ctl_sr = NULL;
2772 char *rate_str = NULL;
2773 bool is_rx_dev = true;
2774
2775 if (is_btsco_device(snd_device, snd_device)) {
2776 ctl_sr_tx = mixer_get_ctl_by_name(adev->mixer, "BT SampleRate TX");
2777 ctl_sr_rx = mixer_get_ctl_by_name(adev->mixer, "BT SampleRate RX");
2778 if (!ctl_sr_tx || !ctl_sr_rx) {
2779 ctl_sr = mixer_get_ctl_by_name(adev->mixer, "BT SampleRate");
2780 if (!ctl_sr)
2781 return -ENOSYS;
2782 }
2783
2784 switch (snd_device) {
2785 case SND_DEVICE_OUT_BT_SCO:
2786 rate_str = "KHZ_8";
2787 break;
2788 case SND_DEVICE_IN_BT_SCO_MIC_NREC:
2789 case SND_DEVICE_IN_BT_SCO_MIC:
2790 rate_str = "KHZ_8";
2791 is_rx_dev = false;
2792 break;
2793 case SND_DEVICE_OUT_BT_SCO_WB:
2794 rate_str = "KHZ_16";
2795 break;
2796 case SND_DEVICE_IN_BT_SCO_MIC_WB_NREC:
2797 case SND_DEVICE_IN_BT_SCO_MIC_WB:
2798 rate_str = "KHZ_16";
2799 is_rx_dev = false;
2800 break;
2801 default:
2802 return 0;
2803 }
2804
2805 ctl = (ctl_sr == NULL) ? (is_rx_dev ? ctl_sr_rx : ctl_sr_tx) : ctl_sr;
2806 if (mixer_ctl_set_enum_by_string(ctl, rate_str) != 0)
2807 return -ENOSYS;
2808 }
2809 return 0;
2810}
2811
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302812int out_standby_l(struct audio_stream *stream);
2813
Eric Laurent637e2d42018-11-15 12:24:31 -08002814struct stream_in *adev_get_active_input(const struct audio_device *adev)
2815{
2816 struct listnode *node;
2817 struct stream_in *last_active_in = NULL;
2818
2819 /* Get last added active input.
2820 * TODO: We may use a priority mechanism to pick highest priority active source */
2821 list_for_each(node, &adev->usecase_list)
2822 {
2823 struct audio_usecase *usecase = node_to_item(node, struct audio_usecase, list);
2824 if (usecase->type == PCM_CAPTURE && usecase->stream.in != NULL)
2825 last_active_in = usecase->stream.in;
2826 }
2827
2828 return last_active_in;
2829}
2830
2831struct stream_in *get_voice_communication_input(const struct audio_device *adev)
2832{
2833 struct listnode *node;
2834
2835 /* First check active inputs with voice communication source and then
2836 * any input if audio mode is in communication */
2837 list_for_each(node, &adev->usecase_list)
2838 {
2839 struct audio_usecase *usecase = node_to_item(node, struct audio_usecase, list);
2840 if (usecase->type == PCM_CAPTURE && usecase->stream.in != NULL &&
2841 usecase->stream.in->source == AUDIO_SOURCE_VOICE_COMMUNICATION)
2842 return usecase->stream.in;
2843 }
2844 if (adev->mode == AUDIO_MODE_IN_COMMUNICATION)
2845 return adev_get_active_input(adev);
2846
2847 return NULL;
2848}
2849
Carter Hsu2e429db2019-05-14 18:50:52 +08002850/*
2851 * Aligned with policy.h
2852 */
2853static inline int source_priority(int inputSource)
2854{
2855 switch (inputSource) {
2856 case AUDIO_SOURCE_VOICE_COMMUNICATION:
2857 return 9;
2858 case AUDIO_SOURCE_CAMCORDER:
2859 return 8;
2860 case AUDIO_SOURCE_VOICE_PERFORMANCE:
2861 return 7;
2862 case AUDIO_SOURCE_UNPROCESSED:
2863 return 6;
2864 case AUDIO_SOURCE_MIC:
2865 return 5;
2866 case AUDIO_SOURCE_ECHO_REFERENCE:
2867 return 4;
2868 case AUDIO_SOURCE_FM_TUNER:
2869 return 3;
2870 case AUDIO_SOURCE_VOICE_RECOGNITION:
2871 return 2;
2872 case AUDIO_SOURCE_HOTWORD:
2873 return 1;
2874 default:
2875 break;
2876 }
2877 return 0;
2878}
2879
2880static struct stream_in *get_priority_input(struct audio_device *adev)
2881{
2882 struct listnode *node;
2883 struct audio_usecase *usecase;
2884 int last_priority = 0, priority;
2885 struct stream_in *priority_in = NULL;
2886 struct stream_in *in;
2887
2888 list_for_each(node, &adev->usecase_list) {
2889 usecase = node_to_item(node, struct audio_usecase, list);
2890 if (usecase->type == PCM_CAPTURE) {
2891 in = usecase->stream.in;
2892 if (!in)
2893 continue;
Krishna Kishor Jha0b7175d2022-07-28 22:33:46 +05302894
2895 if (USECASE_AUDIO_RECORD_FM_VIRTUAL == usecase->id)
2896 continue;
Carter Hsu2e429db2019-05-14 18:50:52 +08002897 priority = source_priority(in->source);
2898
2899 if (priority > last_priority) {
2900 last_priority = priority;
2901 priority_in = in;
2902 }
2903 }
2904 }
2905 return priority_in;
2906}
2907
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002908int select_devices(struct audio_device *adev, audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002909{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002910 snd_device_t out_snd_device = SND_DEVICE_NONE;
2911 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002912 struct audio_usecase *usecase = NULL;
2913 struct audio_usecase *vc_usecase = NULL;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002914 struct audio_usecase *voip_usecase = NULL;
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -08002915 struct audio_usecase *hfp_usecase = NULL;
Siddartha Shaik44dd7702017-06-14 12:13:25 +05302916 struct stream_out stream_out;
Vimal Puthanveed41fcff22014-01-23 15:56:53 -08002917 audio_usecase_t hfp_ucid;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002918 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002919
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302920 ALOGD("%s for use case (%s)", __func__, use_case_table[uc_id]);
2921
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002922 usecase = get_usecase_from_list(adev, uc_id);
2923 if (usecase == NULL) {
2924 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
2925 return -EINVAL;
2926 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002927
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002928 if ((usecase->type == VOICE_CALL) ||
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -08002929 (usecase->type == VOIP_CALL) ||
Derek Chena30a5f42019-12-03 11:17:09 -05002930 (usecase->type == PCM_HFP_CALL)||
Fei Tongaffdf732020-02-20 20:39:05 +08002931 (usecase->type == ICC_CALL) ||
2932 (usecase->type == SYNTH_LOOPBACK)) {
Aditya Bavanaribdda2f22016-10-19 15:02:05 +05302933 if(usecase->stream.out == NULL) {
2934 ALOGE("%s: stream.out is NULL", __func__);
2935 return -EINVAL;
2936 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002937 if (compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_BUS)) {
Guodong Hu267bdf82019-08-12 19:22:32 +08002938 out_snd_device = audio_extn_auto_hal_get_output_snd_device(adev,
2939 uc_id);
2940 in_snd_device = audio_extn_auto_hal_get_input_snd_device(adev,
2941 uc_id);
2942 } else {
2943 out_snd_device = platform_get_output_snd_device(adev->platform,
Jaideep Sharma477917f2020-03-13 18:13:33 +05302944 usecase->stream.out, usecase->type);
Guodong Hu267bdf82019-08-12 19:22:32 +08002945 in_snd_device = platform_get_input_snd_device(adev->platform,
2946 NULL,
Jaideep Sharma477917f2020-03-13 18:13:33 +05302947 &usecase->stream.out->device_list,
2948 usecase->type);
Guodong Hu267bdf82019-08-12 19:22:32 +08002949 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002950 assign_devices(&usecase->device_list, &usecase->stream.out->device_list);
Surendar Karka93cd25a2018-08-28 14:21:37 +05302951 } else if (usecase->type == TRANSCODE_LOOPBACK_RX) {
Siddartha Shaik31b530e2017-05-19 15:26:33 +05302952 if (usecase->stream.inout == NULL) {
2953 ALOGE("%s: stream.inout is NULL", __func__);
2954 return -EINVAL;
2955 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002956 assign_devices(&stream_out.device_list, &usecase->stream.inout->out_config.device_list);
Siddartha Shaik44dd7702017-06-14 12:13:25 +05302957 stream_out.sample_rate = usecase->stream.inout->out_config.sample_rate;
2958 stream_out.format = usecase->stream.inout->out_config.format;
2959 stream_out.channel_mask = usecase->stream.inout->out_config.channel_mask;
Jaideep Sharma477917f2020-03-13 18:13:33 +05302960 out_snd_device = platform_get_output_snd_device(adev->platform, &stream_out, usecase->type);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002961 assign_devices(&usecase->device_list,
2962 &usecase->stream.inout->out_config.device_list);
Zhenlin Lian4f947842022-05-14 15:50:52 +05302963 clear_devices(&stream_out.device_list);
Surendar Karka93cd25a2018-08-28 14:21:37 +05302964 } else if (usecase->type == TRANSCODE_LOOPBACK_TX ) {
2965 if (usecase->stream.inout == NULL) {
2966 ALOGE("%s: stream.inout is NULL", __func__);
2967 return -EINVAL;
2968 }
Manisha Agarwal03297972020-04-17 15:36:55 +05302969 struct listnode out_devices;
2970 list_init(&out_devices);
2971 in_snd_device = platform_get_input_snd_device(adev->platform, NULL,
2972 &out_devices, usecase->type);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002973 assign_devices(&usecase->device_list,
2974 &usecase->stream.inout->in_config.device_list);
Zhenlin Lian4f947842022-05-14 15:50:52 +05302975 clear_devices(&out_devices);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002976 } else {
2977 /*
2978 * If the voice call is active, use the sound devices of voice call usecase
2979 * so that it would not result any device switch. All the usecases will
2980 * be switched to new device when select_devices() is called for voice call
2981 * usecase. This is to avoid switching devices for voice call when
2982 * check_usecases_codec_backend() is called below.
Alexy Joseph79dfa3c2016-04-20 18:44:56 -07002983 * choose voice call device only if the use case device is
2984 * also using the codec backend
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002985 */
Shiv Maliyappanahallibb4cf0b2016-01-21 11:30:06 -08002986 if (voice_is_in_call(adev) && adev->mode != AUDIO_MODE_NORMAL) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002987 vc_usecase = get_usecase_from_list(adev,
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -08002988 get_usecase_id_from_usecase_type(adev, VOICE_CALL));
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002989 if ((vc_usecase) && ((is_codec_backend_out_device_type(&vc_usecase->device_list) &&
2990 is_codec_backend_out_device_type(&usecase->device_list)) ||
2991 (is_codec_backend_out_device_type(&vc_usecase->device_list) &&
2992 is_codec_backend_in_device_type(&usecase->device_list)) ||
2993 is_single_device_type_equal(&vc_usecase->device_list,
2994 AUDIO_DEVICE_OUT_HEARING_AID) ||
2995 is_single_device_type_equal(&usecase->device_list,
Robert Lee8a6aaf32019-09-20 16:40:19 +08002996 AUDIO_DEVICE_IN_VOICE_CALL) ||
2997 (is_single_device_type_equal(&usecase->device_list,
Gautam Manam8fa06162021-09-24 10:41:29 +05302998 AUDIO_DEVICE_IN_BUILTIN_MIC) &&
2999 is_single_device_type_equal(&vc_usecase->device_list,
3000 AUDIO_DEVICE_OUT_USB_HEADSET)) ||
3001 (is_single_device_type_equal(&usecase->device_list,
Robert Lee8a6aaf32019-09-20 16:40:19 +08003002 AUDIO_DEVICE_IN_USB_HEADSET) &&
3003 is_single_device_type_equal(&vc_usecase->device_list,
Kamalakar Yalasiri5edba852021-01-28 14:52:38 +05303004 AUDIO_DEVICE_OUT_USB_HEADSET))||
Gautam Manamf4002142021-09-13 22:05:56 +05303005 (is_single_device_type_equal(&usecase->device_list,
3006 AUDIO_DEVICE_IN_USB_HEADSET) &&
3007 is_codec_backend_out_device_type(&vc_usecase->device_list)) ||
Kamalakar Yalasiri5edba852021-01-28 14:52:38 +05303008 (is_single_device_type_equal(&usecase->device_list,
3009 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) &&
3010 is_codec_backend_out_device_type(&vc_usecase->device_list)))) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003011 in_snd_device = vc_usecase->in_snd_device;
3012 out_snd_device = vc_usecase->out_snd_device;
3013 }
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08003014 } else if (voice_extn_compress_voip_is_active(adev)) {
yidongh02ef86f2017-04-21 15:36:04 +08003015 bool out_snd_device_backend_match = true;
yidongh47785a82017-05-08 19:29:29 +08003016 voip_usecase = get_usecase_from_list(adev, USECASE_COMPRESS_VOIP_CALL);
yidongh6261d8e2017-05-15 17:04:02 +08003017 if ((voip_usecase != NULL) &&
3018 (usecase->type == PCM_PLAYBACK) &&
3019 (usecase->stream.out != NULL)) {
yidongh02ef86f2017-04-21 15:36:04 +08003020 out_snd_device_backend_match = platform_check_backends_match(
3021 voip_usecase->out_snd_device,
3022 platform_get_output_snd_device(
3023 adev->platform,
Jaideep Sharma477917f2020-03-13 18:13:33 +05303024 usecase->stream.out, usecase->type));
yidongh02ef86f2017-04-21 15:36:04 +08003025 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003026 if ((voip_usecase) && (is_codec_backend_out_device_type(&voip_usecase->device_list) &&
3027 (is_codec_backend_out_device_type(&usecase->device_list) ||
3028 is_codec_backend_in_device_type(&usecase->device_list)) &&
yidongh02ef86f2017-04-21 15:36:04 +08003029 out_snd_device_backend_match &&
Mingming Yin2d8aa2e2014-08-14 00:00:51 -07003030 (voip_usecase->stream.out != adev->primary_output))) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08003031 in_snd_device = voip_usecase->in_snd_device;
3032 out_snd_device = voip_usecase->out_snd_device;
3033 }
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -08003034 } else if (audio_extn_hfp_is_active(adev)) {
Vimal Puthanveed41fcff22014-01-23 15:56:53 -08003035 hfp_ucid = audio_extn_hfp_get_usecase();
3036 hfp_usecase = get_usecase_from_list(adev, hfp_ucid);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003037 if ((hfp_usecase) && is_codec_backend_out_device_type(&hfp_usecase->device_list)) {
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -08003038 in_snd_device = hfp_usecase->in_snd_device;
3039 out_snd_device = hfp_usecase->out_snd_device;
3040 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003041 }
3042 if (usecase->type == PCM_PLAYBACK) {
Aditya Bavanaribdda2f22016-10-19 15:02:05 +05303043 if (usecase->stream.out == NULL) {
3044 ALOGE("%s: stream.out is NULL", __func__);
3045 return -EINVAL;
3046 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003047 assign_devices(&usecase->device_list, &usecase->stream.out->device_list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003048 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07003049 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurent637e2d42018-11-15 12:24:31 -08003050 struct stream_out *voip_out = adev->primary_output;
3051 struct stream_in *voip_in = get_voice_communication_input(adev);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003052 if (compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_BUS))
Guodong Hu267bdf82019-08-12 19:22:32 +08003053 out_snd_device = audio_extn_auto_hal_get_output_snd_device(adev, uc_id);
3054 else
3055 out_snd_device = platform_get_output_snd_device(adev->platform,
Jaideep Sharma477917f2020-03-13 18:13:33 +05303056 usecase->stream.out,
3057 usecase->type);
kunleizdcf967a2018-08-07 17:09:11 +08003058 voip_usecase = get_usecase_from_list(adev, USECASE_AUDIO_PLAYBACK_VOIP);
kunleizdcf967a2018-08-07 17:09:11 +08003059
Eric Laurent637e2d42018-11-15 12:24:31 -08003060 if (voip_usecase)
3061 voip_out = voip_usecase->stream.out;
3062
3063 if (usecase->stream.out == voip_out && voip_in != NULL)
3064 select_devices(adev, voip_in->usecase);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07003065 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003066 } else if (usecase->type == PCM_CAPTURE) {
Aditya Bavanaribdda2f22016-10-19 15:02:05 +05303067 if (usecase->stream.in == NULL) {
3068 ALOGE("%s: stream.in is NULL", __func__);
3069 return -EINVAL;
3070 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003071 assign_devices(&usecase->device_list, &usecase->stream.in->device_list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003072 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07003073 if (in_snd_device == SND_DEVICE_NONE) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003074 struct listnode out_devices;
Eric Laurent637e2d42018-11-15 12:24:31 -08003075 struct stream_in *voip_in = get_voice_communication_input(adev);
Carter Hsu2e429db2019-05-14 18:50:52 +08003076 struct stream_in *priority_in = NULL;
Eric Laurent637e2d42018-11-15 12:24:31 -08003077
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003078 list_init(&out_devices);
Eric Laurent637e2d42018-11-15 12:24:31 -08003079 if (voip_in != NULL) {
Eric Laurent637e2d42018-11-15 12:24:31 -08003080 struct audio_usecase *voip_usecase = get_usecase_from_list(adev,
3081 USECASE_AUDIO_PLAYBACK_VOIP);
3082
Carter Hsu2e429db2019-05-14 18:50:52 +08003083 usecase->stream.in->enable_ec_port = false;
3084
Zhou Song503196b2021-07-23 17:31:05 +08003085 if (usecase->id == USECASE_AUDIO_RECORD_AFE_PROXY ||
3086 usecase->id == USECASE_AUDIO_RECORD_AFE_PROXY2) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003087 reassign_device_list(&out_devices, AUDIO_DEVICE_OUT_TELEPHONY_TX, "");
Eric Laurent637e2d42018-11-15 12:24:31 -08003088 } else if (voip_usecase) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003089 assign_devices(&out_devices, &voip_usecase->stream.out->device_list);
Eric Laurent637e2d42018-11-15 12:24:31 -08003090 } else if (adev->primary_output &&
3091 !adev->primary_output->standby) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003092 assign_devices(&out_devices, &adev->primary_output->device_list);
Eric Laurent637e2d42018-11-15 12:24:31 -08003093 } else {
3094 /* forcing speaker o/p device to get matching i/p pair
3095 in case o/p is not routed from same primary HAL */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003096 reassign_device_list(&out_devices, AUDIO_DEVICE_OUT_SPEAKER, "");
Eric Laurent637e2d42018-11-15 12:24:31 -08003097 }
Carter Hsu2e429db2019-05-14 18:50:52 +08003098 priority_in = voip_in;
3099 } else {
3100 /* get the input with the highest priority source*/
3101 priority_in = get_priority_input(adev);
3102
Susan Wang727dd6b2021-03-26 11:28:59 -04003103 if (!priority_in ||
3104 audio_extn_auto_hal_overwrite_priority_for_auto(usecase->stream.in))
Carter Hsu2e429db2019-05-14 18:50:52 +08003105 priority_in = usecase->stream.in;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07003106 }
Huicheng Liu1404ba12020-09-11 01:03:25 -04003107 if (compare_device_type(&usecase->device_list, AUDIO_DEVICE_IN_BUS)){
3108 in_snd_device = audio_extn_auto_hal_get_snd_device_for_car_audio_stream(priority_in->car_audio_stream);
3109 }
3110 else
3111 in_snd_device = platform_get_input_snd_device(adev->platform,
3112 priority_in,
3113 &out_devices,
3114 usecase->type);
Zhenlin Lian4f947842022-05-14 15:50:52 +05303115 clear_devices(&out_devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07003116 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003117 }
3118 }
3119
3120 if (out_snd_device == usecase->out_snd_device &&
3121 in_snd_device == usecase->in_snd_device) {
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05303122
3123 if (!force_device_switch(usecase))
3124 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003125 }
3126
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003127 if (!compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_BUS) &&
Guodong Hu267bdf82019-08-12 19:22:32 +08003128 ((is_btsco_device(out_snd_device,in_snd_device) && !adev->bt_sco_on) ||
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003129 (is_a2dp_device(out_snd_device) && !audio_extn_a2dp_source_is_ready()))) {
Guodong Hu267bdf82019-08-12 19:22:32 +08003130 ALOGD("SCO/A2DP is selected but they are not connected/ready hence dont route");
3131 return 0;
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303132 }
3133
Aalique Grahame22e49102018-12-18 14:23:57 -08003134 if (out_snd_device != SND_DEVICE_NONE &&
3135 out_snd_device != adev->last_logged_snd_device[uc_id][0]) {
3136 ALOGD("%s: changing use case %s output device from(%d: %s, acdb %d) to (%d: %s, acdb %d)",
3137 __func__,
3138 use_case_table[uc_id],
3139 adev->last_logged_snd_device[uc_id][0],
3140 platform_get_snd_device_name(adev->last_logged_snd_device[uc_id][0]),
3141 adev->last_logged_snd_device[uc_id][0] != SND_DEVICE_NONE ?
3142 platform_get_snd_device_acdb_id(adev->last_logged_snd_device[uc_id][0]) :
3143 -1,
3144 out_snd_device,
3145 platform_get_snd_device_name(out_snd_device),
3146 platform_get_snd_device_acdb_id(out_snd_device));
3147 adev->last_logged_snd_device[uc_id][0] = out_snd_device;
3148 }
3149 if (in_snd_device != SND_DEVICE_NONE &&
3150 in_snd_device != adev->last_logged_snd_device[uc_id][1]) {
3151 ALOGD("%s: changing use case %s input device from(%d: %s, acdb %d) to (%d: %s, acdb %d)",
3152 __func__,
3153 use_case_table[uc_id],
3154 adev->last_logged_snd_device[uc_id][1],
3155 platform_get_snd_device_name(adev->last_logged_snd_device[uc_id][1]),
3156 adev->last_logged_snd_device[uc_id][1] != SND_DEVICE_NONE ?
3157 platform_get_snd_device_acdb_id(adev->last_logged_snd_device[uc_id][1]) :
3158 -1,
3159 in_snd_device,
3160 platform_get_snd_device_name(in_snd_device),
3161 platform_get_snd_device_acdb_id(in_snd_device));
3162 adev->last_logged_snd_device[uc_id][1] = in_snd_device;
3163 }
3164
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08003165
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003166 /*
3167 * Limitation: While in call, to do a device switch we need to disable
3168 * and enable both RX and TX devices though one of them is same as current
3169 * device.
3170 */
Vidyakumar Athota21b3bb92014-04-25 11:08:08 -07003171 if ((usecase->type == VOICE_CALL) &&
3172 (usecase->in_snd_device != SND_DEVICE_NONE) &&
3173 (usecase->out_snd_device != SND_DEVICE_NONE)) {
Eric Laurentb23d5282013-05-14 15:27:20 -07003174 status = platform_switch_voice_call_device_pre(adev->platform);
Narsinga Rao Chella116142b2015-08-14 18:00:08 -07003175 }
3176
3177 if (((usecase->type == VOICE_CALL) ||
3178 (usecase->type == VOIP_CALL)) &&
3179 (usecase->out_snd_device != SND_DEVICE_NONE)) {
3180 /* Disable sidetone only if voice/voip call already exists */
Jaideep Sharma477917f2020-03-13 18:13:33 +05303181 if (voice_is_call_state_active_in_call(adev) ||
Narsinga Rao Chella116142b2015-08-14 18:00:08 -07003182 voice_extn_compress_voip_is_started(adev))
Bhalchandra Gajare45fee282015-06-09 22:23:45 -07003183 voice_set_sidetone(adev, usecase->out_snd_device, false);
Vidyakumar Athotaea269c62016-10-31 09:05:59 -07003184
3185 /* Disable aanc only if voice call exists */
Jaideep Sharma477917f2020-03-13 18:13:33 +05303186 if (voice_is_call_state_active_in_call(adev))
Vidyakumar Athotaea269c62016-10-31 09:05:59 -07003187 voice_check_and_update_aanc_path(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08003188 }
3189
Aalique Grahame22e49102018-12-18 14:23:57 -08003190 if ((out_snd_device == SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP ||
3191 out_snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP) &&
Florian Pfister1a84f312018-07-19 14:38:18 +02003192 (!audio_extn_a2dp_source_is_ready())) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303193 ALOGW("%s: A2DP profile is not ready, routing to speaker only", __func__);
Aalique Grahame22e49102018-12-18 14:23:57 -08003194 if (out_snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP)
3195 out_snd_device = SND_DEVICE_OUT_SPEAKER_SAFE;
3196 else
3197 out_snd_device = SND_DEVICE_OUT_SPEAKER;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303198 }
3199
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003200 /* Disable current sound devices */
3201 if (usecase->out_snd_device != SND_DEVICE_NONE) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07003202 disable_audio_route(adev, usecase);
3203 disable_snd_device(adev, usecase->out_snd_device);
Gautam Manam274f4752021-09-24 10:58:49 +05303204 if (usecase->id == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS)
3205 disable_snd_device(adev, SND_DEVICE_OUT_HAPTICS);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003206 }
3207
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003208 if (usecase->in_snd_device != SND_DEVICE_NONE) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07003209 disable_audio_route(adev, usecase);
3210 disable_snd_device(adev, usecase->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003211 }
3212
Vidyakumar Athota545dbd32013-11-13 17:30:53 -08003213 /* Applicable only on the targets that has external modem.
3214 * New device information should be sent to modem before enabling
3215 * the devices to reduce in-call device switch time.
3216 */
Vidyakumar Athota21b3bb92014-04-25 11:08:08 -07003217 if ((usecase->type == VOICE_CALL) &&
3218 (usecase->in_snd_device != SND_DEVICE_NONE) &&
3219 (usecase->out_snd_device != SND_DEVICE_NONE)) {
Vidyakumar Athota545dbd32013-11-13 17:30:53 -08003220 status = platform_switch_voice_call_enable_device_config(adev->platform,
3221 out_snd_device,
3222 in_snd_device);
Vidyakumar Athota21b3bb92014-04-25 11:08:08 -07003223 }
Vidyakumar Athota545dbd32013-11-13 17:30:53 -08003224
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003225 /* Enable new sound devices */
3226 if (out_snd_device != SND_DEVICE_NONE) {
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08003227 check_usecases_codec_backend(adev, usecase, out_snd_device);
Saurav Kumarc1411662020-10-14 10:50:45 +05303228 check_and_configure_headphone(adev, usecase, out_snd_device);
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +05303229 if (platform_check_codec_asrc_support(adev->platform))
3230 check_and_set_asrc_mode(adev, usecase, out_snd_device);
Haynes Mathew George1376ca62014-04-24 11:55:48 -07003231 enable_snd_device(adev, out_snd_device);
Zhou Songd9bd9302020-08-04 16:34:45 +08003232 /* Enable haptics device for haptic usecase */
3233 if (usecase->id == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS)
3234 enable_snd_device(adev, SND_DEVICE_OUT_HAPTICS);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003235 }
3236
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07003237 if (in_snd_device != SND_DEVICE_NONE) {
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05303238 check_usecases_capture_codec_backend(adev, usecase, in_snd_device);
Haynes Mathew George1376ca62014-04-24 11:55:48 -07003239 enable_snd_device(adev, in_snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07003240 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003241
Lakshman Chaluvarajuab7e5572020-10-28 19:29:23 +05303242 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL)
Eric Laurentb23d5282013-05-14 15:27:20 -07003243 status = platform_switch_voice_call_device_post(adev->platform,
3244 out_snd_device,
3245 in_snd_device);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08003246
sangwoo170731f2013-06-08 15:36:36 +09003247 usecase->in_snd_device = in_snd_device;
3248 usecase->out_snd_device = out_snd_device;
3249
Dhananjay Kumard6d32152016-10-13 16:11:03 +05303250 audio_extn_utils_update_stream_app_type_cfg_for_usecase(adev,
3251 usecase);
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +05303252 if (usecase->type == PCM_PLAYBACK) {
Weiyin Jiang6f4c8062016-11-23 15:30:29 +08003253 if ((24 == usecase->stream.out->bit_width) &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003254 compare_device_type(&usecase->stream.out->device_list, AUDIO_DEVICE_OUT_SPEAKER)) {
Weiyin Jiang6f4c8062016-11-23 15:30:29 +08003255 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
3256 } else if ((out_snd_device == SND_DEVICE_OUT_HDMI ||
3257 out_snd_device == SND_DEVICE_OUT_USB_HEADSET ||
3258 out_snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
3259 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
3260 /*
3261 * To best utlize DSP, check if the stream sample rate is supported/multiple of
3262 * configured device sample rate, if not update the COPP rate to be equal to the
3263 * device sample rate, else open COPP at stream sample rate
3264 */
3265 platform_check_and_update_copp_sample_rate(adev->platform, out_snd_device,
3266 usecase->stream.out->sample_rate,
3267 &usecase->stream.out->app_type_cfg.sample_rate);
Ashish Jain4826f6c2017-02-06 13:33:20 +05303268 } else if (((out_snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
Preetam Singh Ranawat590d0432019-09-30 14:39:47 +05303269 out_snd_device != SND_DEVICE_OUT_HEADPHONES &&
3270 out_snd_device != SND_DEVICE_OUT_HEADPHONES_HIFI_FILTER &&
Ashish Jain4826f6c2017-02-06 13:33:20 +05303271 !audio_is_true_native_stream_active(adev)) &&
Weiyin Jiang6f4c8062016-11-23 15:30:29 +08003272 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
3273 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +05303274#ifdef PLATFORM_AUTO
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +05303275 if (!(compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_BUS) && ((usecase->stream.out->flags &
3276 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_SYS_NOTIFICATION) || (usecase->stream.out->flags &
3277 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_PHONE)))) {
3278 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
3279 }
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +05303280#else
3281 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
3282#endif
Weiyin Jiang6f4c8062016-11-23 15:30:29 +08003283 }
Weiyin Jiangcdece202019-07-08 16:13:16 +08003284 }
3285 enable_audio_route(adev, usecase);
Weiyin Jiang6f4c8062016-11-23 15:30:29 +08003286
Shalini Manjunatha5e610912020-12-29 13:02:25 +05303287 if (uc_id == USECASE_AUDIO_PLAYBACK_VOIP) {
3288 struct stream_in *voip_in = get_voice_communication_input(adev);
3289 struct audio_usecase *voip_in_usecase = NULL;
3290 voip_in_usecase = get_usecase_from_list(adev, USECASE_AUDIO_RECORD_VOIP);
3291 if (voip_in != NULL &&
3292 voip_in_usecase != NULL &&
3293 !(out_snd_device == AUDIO_DEVICE_OUT_SPEAKER ||
3294 out_snd_device == AUDIO_DEVICE_OUT_SPEAKER_SAFE) &&
3295 (voip_in_usecase->in_snd_device ==
3296 platform_get_input_snd_device(adev->platform, voip_in,
3297 &usecase->stream.out->device_list,usecase->type))) {
3298 /*
3299 * if VOIP TX is enabled before VOIP RX, needs to re-route the TX path
3300 * for enabling echo-reference-voip with correct port
3301 */
3302 ALOGD("%s: VOIP TX is enabled before VOIP RX,needs to re-route the TX path",__func__);
3303 disable_audio_route(adev, voip_in_usecase);
3304 disable_snd_device(adev, voip_in_usecase->in_snd_device);
3305 enable_snd_device(adev, voip_in_usecase->in_snd_device);
3306 enable_audio_route(adev, voip_in_usecase);
3307 }
3308 }
Ramjee Singhcdf67af2021-09-29 14:20:27 +05303309 if (voice_extn_compress_voip_is_active(adev)) {
3310 struct audio_usecase *voip_usecase = get_usecase_from_list(adev,
3311 USECASE_COMPRESS_VOIP_CALL);
3312 /*
3313 * If only compress voip input is opened voip out will be primary out.
3314 * Need to consider re-routing to select correct i/p pair
3315 */
3316 if ((voip_usecase != NULL) &&
3317 (usecase->type == PCM_PLAYBACK) &&
3318 (usecase->stream.out == voip_usecase->stream.out)) {
3319 in_snd_device = platform_get_input_snd_device(adev->platform,
3320 NULL,
3321 &usecase->stream.out->device_list,
3322 usecase->type);
3323 if (voip_usecase->in_snd_device != in_snd_device ) {
3324 ALOGD("%s:Re routing compress voip tx snd device matching voip rx pair",
3325 __func__);
3326 disable_audio_route(adev, voip_usecase);
3327 disable_snd_device(adev, voip_usecase->in_snd_device);
3328 voip_usecase->in_snd_device = in_snd_device;
3329 voip_usecase->out_snd_device = usecase->out_snd_device;
3330 /* Route all TX usecase to Compress voip BE */
3331 check_usecases_capture_codec_backend(adev, voip_usecase, in_snd_device);
3332 enable_snd_device(adev, in_snd_device);
3333 /* Send Voice related calibration for RX /TX pair */
3334 status = platform_switch_voice_call_device_post(adev->platform,
3335 out_snd_device,
3336 in_snd_device);
3337 enable_audio_route(adev, voip_usecase);
3338 }
3339 }
3340 }
Shalini Manjunatha5e610912020-12-29 13:02:25 +05303341
3342
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003343 audio_extn_qdsp_set_device(usecase);
Aalique Grahame22e49102018-12-18 14:23:57 -08003344
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07003345 /* If input stream is already running then effect needs to be
3346 applied on the new input device that's being enabled here. */
Eric Laurent637e2d42018-11-15 12:24:31 -08003347 if (in_snd_device != SND_DEVICE_NONE)
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07003348 check_and_enable_effect(adev);
3349
Vidyakumar Athota493f2892016-08-14 11:56:55 -07003350 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) {
Vidyakumar Athotaea269c62016-10-31 09:05:59 -07003351 /* Enable aanc only if voice call exists */
Jaideep Sharma477917f2020-03-13 18:13:33 +05303352 if (voice_is_call_state_active_in_call(adev))
Vidyakumar Athotaea269c62016-10-31 09:05:59 -07003353 voice_check_and_update_aanc_path(adev, out_snd_device, true);
3354
Vidyakumar Athota493f2892016-08-14 11:56:55 -07003355 /* Enable sidetone only if other voice/voip call already exists */
Jaideep Sharma477917f2020-03-13 18:13:33 +05303356 if (voice_is_call_state_active_in_call(adev) ||
Vidyakumar Athota493f2892016-08-14 11:56:55 -07003357 voice_extn_compress_voip_is_started(adev))
3358 voice_set_sidetone(adev, out_snd_device, true);
3359 }
3360
Vidyakumar Athota1fd21792013-11-15 14:50:57 -08003361 /* Applicable only on the targets that has external modem.
3362 * Enable device command should be sent to modem only after
3363 * enabling voice call mixer controls
3364 */
Vidyakumar Athota339342f2014-07-01 15:30:57 -07003365 if (usecase->type == VOICE_CALL)
Vidyakumar Athota1fd21792013-11-15 14:50:57 -08003366 status = platform_switch_voice_call_usecase_route_post(adev->platform,
3367 out_snd_device,
3368 in_snd_device);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303369
3370 if (is_btsco_device(out_snd_device, in_snd_device) || is_a2dp_device(out_snd_device)) {
Eric Laurent637e2d42018-11-15 12:24:31 -08003371 struct stream_in *in = adev_get_active_input(adev);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303372 if (usecase->type == VOIP_CALL) {
Eric Laurent637e2d42018-11-15 12:24:31 -08003373 if (in != NULL && !in->standby) {
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303374 if (is_bt_soc_on(adev) == false){
3375 ALOGD("BT SCO MIC disconnected while in connection");
Eric Laurent637e2d42018-11-15 12:24:31 -08003376 if (in->pcm != NULL)
3377 pcm_stop(in->pcm);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303378 }
3379 }
3380 if ((usecase->stream.out != NULL) && (usecase->stream.out != adev->primary_output)
3381 && usecase->stream.out->started) {
3382 if (is_bt_soc_on(adev) == false) {
3383 ALOGD("BT SCO/A2DP disconnected while in connection");
3384 out_standby_l(&usecase->stream.out->stream.common);
3385 }
3386 }
3387 } else if ((usecase->stream.out != NULL) &&
3388 !(usecase->stream.out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
Surendar Karkae1dc8742018-11-19 16:23:14 +05303389 (usecase->type != TRANSCODE_LOOPBACK_TX) &&
3390 (usecase->type != TRANSCODE_LOOPBACK_RX) &&
Weiyin Jiang0d373242019-07-25 13:18:17 +08003391 (usecase->type != PCM_CAPTURE) &&
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303392 usecase->stream.out->started) {
3393 if (is_bt_soc_on(adev) == false) {
3394 ALOGD("BT SCO/A2dp disconnected while in connection");
3395 out_standby_l(&usecase->stream.out->stream.common);
3396 }
3397 }
3398 }
3399
Yung Ti Su70cb8242018-06-22 17:38:47 +08003400 if (usecase->type != PCM_CAPTURE && usecase == voip_usecase) {
Aalique Grahame22e49102018-12-18 14:23:57 -08003401 struct stream_out *voip_out = voip_usecase->stream.out;
3402 audio_extn_utils_send_app_type_gain(adev,
3403 voip_out->app_type_cfg.app_type,
3404 &voip_out->app_type_cfg.gain[0]);
3405 }
3406
Ajender Reddyb940b832021-07-07 11:51:42 +05303407 ALOGD("%s: done",__func__);
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05303408
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003409 return status;
3410}
3411
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003412static int stop_input_stream(struct stream_in *in)
3413{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05303414 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003415 struct audio_usecase *uc_info;
Pallavid7c7a272018-01-16 11:22:55 +05303416
3417 if (in == NULL) {
3418 ALOGE("%s: stream_in ptr is NULL", __func__);
3419 return -EINVAL;
3420 }
3421
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003422 struct audio_device *adev = in->dev;
Carter Hsu2e429db2019-05-14 18:50:52 +08003423 struct stream_in *priority_in = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003424
Eric Laurent994a6932013-07-17 11:51:42 -07003425 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003426 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003427 uc_info = get_usecase_from_list(adev, in->usecase);
3428 if (uc_info == NULL) {
3429 ALOGE("%s: Could not find the usecase (%d) in the list",
3430 __func__, in->usecase);
3431 return -EINVAL;
3432 }
3433
Carter Hsu2e429db2019-05-14 18:50:52 +08003434 priority_in = get_priority_input(adev);
3435
Derek Chenea197282019-01-07 17:35:01 -08003436 if (audio_extn_ext_hw_plugin_usecase_stop(adev->ext_hw_plugin, uc_info))
3437 ALOGE("%s: failed to stop ext hw plugin", __func__);
Derek Chend2530072014-11-24 12:39:14 -08003438
Vidyakumar Athota2850d532013-11-19 16:02:12 -08003439 /* Close in-call recording streams */
3440 voice_check_and_stop_incall_rec_usecase(adev, in);
3441
Eric Laurent150dbfe2013-02-27 14:31:02 -08003442 /* 1. Disable stream specific mixer controls */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07003443 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003444
3445 /* 2. Disable the tx device */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07003446 disable_snd_device(adev, uc_info->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003447
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003448 if (is_loopback_input_device(get_device_types(&in->device_list)))
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05303449 audio_extn_keep_alive_stop(KEEP_ALIVE_OUT_PRIMARY);
3450
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08003451 list_remove(&uc_info->list);
Zhenlin Lian4f947842022-05-14 15:50:52 +05303452 clear_devices(&uc_info->device_list);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08003453 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003454
Carter Hsu2e429db2019-05-14 18:50:52 +08003455 if (priority_in == in) {
3456 priority_in = get_priority_input(adev);
Sujin Panicker110f7942021-08-26 17:01:22 +05303457 if (priority_in) {
3458 if (is_usb_in_device_type(&priority_in->device_list)) {
3459 if (audio_extn_usb_connected(NULL))
3460 select_devices(adev, priority_in->usecase);
3461 } else {
3462 select_devices(adev, priority_in->usecase);
3463 }
3464 }
Carter Hsu2e429db2019-05-14 18:50:52 +08003465 }
3466
Vatsal Buchac09ae062018-11-14 13:25:08 +05303467 enable_gcov();
Eric Laurent994a6932013-07-17 11:51:42 -07003468 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003469 return ret;
3470}
3471
3472int start_input_stream(struct stream_in *in)
3473{
3474 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -08003475 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003476 struct audio_usecase *uc_info;
Preetam Singh Ranawata87e9742018-02-13 16:52:53 +05303477
3478 if (in == NULL) {
3479 ALOGE("%s: stream_in ptr is NULL", __func__);
3480 return -EINVAL;
3481 }
3482
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003483 struct audio_device *adev = in->dev;
Garmond Leunge2433c32017-09-28 21:51:22 -07003484 struct pcm_config config = in->config;
Garmond Leung438932f2017-10-04 19:35:18 -07003485 int usecase = platform_update_usecase_from_source(in->source,in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003486
Mingming Yin2664a5b2015-09-03 10:53:11 -07003487 if (get_usecase_from_list(adev, usecase) == NULL)
3488 in->usecase = usecase;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05303489 ALOGD("%s: enter: stream(%p)usecase(%d: %s)",
3490 __func__, &in->stream, in->usecase, use_case_table[in->usecase]);
Shiv Maliyappanahallida107642013-10-17 11:16:13 -07003491
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05303492 if (CARD_STATUS_OFFLINE == in->card_status||
Shubhasini Sugumaran3fde6582022-01-19 15:54:50 -05003493 CARD_STATUS_OFFLINE == adev->card_status ||
3494 POWER_POLICY_STATUS_OFFLINE == adev->in_power_policy) {
3495 ALOGW("in->card_status or adev->card_status or adev->input_power offline, try again");
Dhanalakshmi Siddani4d57e992014-07-17 16:37:51 +05303496 ret = -EIO;
Naresh Tanniru4c630392014-05-12 01:05:52 +05303497 goto error_config;
3498 }
Naresh Tanniru4c630392014-05-12 01:05:52 +05303499
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003500 if (is_sco_in_device_type(&in->device_list)) {
Lakshman Chaluvaraju87f53aa2021-02-02 15:50:11 +05303501 if (!adev->bt_sco_on || audio_extn_a2dp_source_is_ready()) {
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303502 ALOGE("%s: SCO profile is not ready, return error", __func__);
3503 ret = -EIO;
3504 goto error_config;
3505 }
3506 }
3507
Shiv Maliyappanahallida107642013-10-17 11:16:13 -07003508 /* Check if source matches incall recording usecase criteria */
3509 ret = voice_check_and_set_incall_rec_usecase(adev, in);
3510 if (ret)
3511 goto error_config;
3512 else
Mingming Yin2664a5b2015-09-03 10:53:11 -07003513 ALOGV("%s: usecase(%d)", __func__, in->usecase);
3514
Dhananjay Kumar7dbe3562019-08-30 05:49:33 +05303515 if (audio_extn_cin_attached_usecase(in))
3516 audio_extn_cin_acquire_usecase(in);
3517
Mingming Yin2664a5b2015-09-03 10:53:11 -07003518 if (get_usecase_from_list(adev, in->usecase) != NULL) {
3519 ALOGE("%s: use case assigned already in use, stream(%p)usecase(%d: %s)",
3520 __func__, &in->stream, in->usecase, use_case_table[in->usecase]);
Weiyin Jiang38c0e612020-09-10 16:10:51 +08003521 ret = -EINVAL;
3522 goto error_config;
Mingming Yin2664a5b2015-09-03 10:53:11 -07003523 }
Shiv Maliyappanahallida107642013-10-17 11:16:13 -07003524
Eric Laurentb23d5282013-05-14 15:27:20 -07003525 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003526 if (in->pcm_device_id < 0) {
3527 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
3528 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -08003529 ret = -EINVAL;
3530 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003531 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003532
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003533 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07003534
3535 if (!uc_info) {
3536 ret = -ENOMEM;
3537 goto error_config;
3538 }
3539
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003540 uc_info->id = in->usecase;
3541 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08003542 uc_info->stream.in = in;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003543 list_init(&uc_info->device_list);
3544 assign_devices(&uc_info->device_list, &in->device_list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003545 uc_info->in_snd_device = SND_DEVICE_NONE;
3546 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003547
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08003548 list_add_tail(&adev->usecase_list, &uc_info->list);
Wei Wangf7ca6c92017-11-21 14:51:20 -08003549 audio_streaming_hint_start();
Sudheer Papothifa9d2282015-09-17 01:53:25 +05303550 audio_extn_perf_lock_acquire(&adev->perf_lock_handle, 0,
3551 adev->perf_lock_opts,
3552 adev->perf_lock_opts_size);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003553 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003554
Derek Chenea197282019-01-07 17:35:01 -08003555 if (audio_extn_ext_hw_plugin_usecase_start(adev->ext_hw_plugin, uc_info))
3556 ALOGE("%s: failed to start ext hw plugin", __func__);
Derek Chend2530072014-11-24 12:39:14 -08003557
Deeraj Soman30cc7ae2019-03-18 16:26:55 +05303558 android_atomic_acquire_cas(true, false, &(in->capture_stopped));
3559
Dhananjay Kumar7dbe3562019-08-30 05:49:33 +05303560 if (audio_extn_cin_attached_usecase(in)) {
Manish Dewangan46e07982018-12-13 18:18:59 +05303561 ret = audio_extn_cin_open_input_stream(in);
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05303562 if (ret)
3563 goto error_open;
3564 else
3565 goto done_open;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003566 }
3567
Haynes Mathew George16081042017-05-31 17:16:49 -07003568 if (in->usecase == USECASE_AUDIO_RECORD_MMAP) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003569 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) {
Haynes Mathew George16081042017-05-31 17:16:49 -07003570 ALOGE("%s: pcm stream not ready", __func__);
3571 goto error_open;
3572 }
3573 ret = pcm_start(in->pcm);
3574 if (ret < 0) {
3575 ALOGE("%s: MMAP pcm_start failed ret %d", __func__, ret);
3576 goto error_open;
3577 }
3578 } else {
3579 unsigned int flags = PCM_IN | PCM_MONOTONIC;
3580 unsigned int pcm_open_retry_count = 0;
3581
Zhou Song62ea0282020-03-22 19:53:01 +08003582 if ((in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY) ||
3583 (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY2)) {
Haynes Mathew George16081042017-05-31 17:16:49 -07003584 flags |= PCM_MMAP | PCM_NOIRQ;
3585 pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT;
3586 } else if (in->realtime) {
3587 flags |= PCM_MMAP | PCM_NOIRQ;
3588 }
3589
Garmond Leunge2433c32017-09-28 21:51:22 -07003590 if (audio_extn_ffv_get_stream() == in) {
3591 ALOGD("%s: ffv stream, update pcm config", __func__);
3592 audio_extn_ffv_update_pcm_config(&config);
3593 }
Haynes Mathew George16081042017-05-31 17:16:49 -07003594 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
3595 __func__, adev->snd_card, in->pcm_device_id, in->config.channels);
3596
3597 while (1) {
Haynes Mathew George380745d2017-10-04 15:27:45 -07003598 ATRACE_BEGIN("pcm_in_open");
Haynes Mathew George16081042017-05-31 17:16:49 -07003599 in->pcm = pcm_open(adev->snd_card, in->pcm_device_id,
Garmond Leung438932f2017-10-04 19:35:18 -07003600 flags, &config);
Haynes Mathew George380745d2017-10-04 15:27:45 -07003601 ATRACE_END();
Satish Babu Patakokila54ce83d2018-07-06 18:00:37 +05303602 if (errno == ENETRESET && !pcm_is_ready(in->pcm)) {
Sharad Sanglec6f32552018-05-04 16:15:38 +05303603 ALOGE("%s: pcm_open failed errno:%d\n", __func__, errno);
3604 adev->card_status = CARD_STATUS_OFFLINE;
3605 in->card_status = CARD_STATUS_OFFLINE;
3606 ret = -EIO;
3607 goto error_open;
3608 }
3609
Haynes Mathew George16081042017-05-31 17:16:49 -07003610 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) {
3611 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
3612 if (in->pcm != NULL) {
3613 pcm_close(in->pcm);
3614 in->pcm = NULL;
3615 }
Weiyin Jiang72197252019-10-09 11:49:32 +08003616 if (pcm_open_retry_count == 0) {
Haynes Mathew George16081042017-05-31 17:16:49 -07003617 ret = -EIO;
3618 goto error_open;
3619 }
Weiyin Jiang72197252019-10-09 11:49:32 +08003620 pcm_open_retry_count--;
Haynes Mathew George16081042017-05-31 17:16:49 -07003621 usleep(PROXY_OPEN_WAIT_TIME * 1000);
3622 continue;
3623 }
3624 break;
3625 }
3626
3627 ALOGV("%s: pcm_prepare", __func__);
Haynes Mathew George380745d2017-10-04 15:27:45 -07003628 ATRACE_BEGIN("pcm_in_prepare");
Haynes Mathew George16081042017-05-31 17:16:49 -07003629 ret = pcm_prepare(in->pcm);
Haynes Mathew George380745d2017-10-04 15:27:45 -07003630 ATRACE_END();
Haynes Mathew George16081042017-05-31 17:16:49 -07003631 if (ret < 0) {
3632 ALOGE("%s: pcm_prepare returned %d", __func__, ret);
3633 pcm_close(in->pcm);
3634 in->pcm = NULL;
3635 goto error_open;
3636 }
Narsinga Rao Chellaa6e1f702021-10-22 13:57:32 +05303637 if (in->flags == AUDIO_INPUT_FLAG_FAST)
3638 register_in_stream(in);
Haynes Mathew George16081042017-05-31 17:16:49 -07003639 if (in->realtime) {
Haynes Mathew George380745d2017-10-04 15:27:45 -07003640 ATRACE_BEGIN("pcm_in_start");
Haynes Mathew George16081042017-05-31 17:16:49 -07003641 ret = pcm_start(in->pcm);
Haynes Mathew George380745d2017-10-04 15:27:45 -07003642 ATRACE_END();
Haynes Mathew George16081042017-05-31 17:16:49 -07003643 if (ret < 0) {
3644 ALOGE("%s: RT pcm_start failed ret %d", __func__, ret);
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003645 pcm_close(in->pcm);
3646 in->pcm = NULL;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003647 goto error_open;
3648 }
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003649 }
Haynes Mathew George5beddd42016-06-27 18:33:40 -07003650 }
3651
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07003652 check_and_enable_effect(adev);
justinweng20fb6d82019-02-21 18:49:00 -07003653 audio_extn_audiozoom_set_microphone_direction(in, in->zoom);
3654 audio_extn_audiozoom_set_microphone_field_dimension(in, in->direction);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07003655
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003656 if (is_loopback_input_device(get_device_types(&in->device_list)))
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05303657 audio_extn_keep_alive_start(KEEP_ALIVE_OUT_PRIMARY);
3658
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05303659done_open:
Wei Wangf7ca6c92017-11-21 14:51:20 -08003660 audio_streaming_hint_end();
Sudheer Papothifa9d2282015-09-17 01:53:25 +05303661 audio_extn_perf_lock_release(&adev->perf_lock_handle);
Manisha Agarwal4c2402e2020-10-21 12:02:57 +05303662 ALOGD("%s: exit", __func__);
Vatsal Buchac09ae062018-11-14 13:25:08 +05303663 enable_gcov();
Eric Laurentc8400632013-02-14 19:04:54 -08003664 return ret;
3665
3666error_open:
Wei Wangf7ca6c92017-11-21 14:51:20 -08003667 audio_streaming_hint_end();
Sudheer Papothifa9d2282015-09-17 01:53:25 +05303668 audio_extn_perf_lock_release(&adev->perf_lock_handle);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003669 stop_input_stream(in);
Wei Wangf7ca6c92017-11-21 14:51:20 -08003670
Eric Laurentc8400632013-02-14 19:04:54 -08003671error_config:
Weiyin Jiang38c0e612020-09-10 16:10:51 +08003672 if (audio_extn_cin_attached_usecase(in))
3673 audio_extn_cin_close_input_stream(in);
Laxminath Kasam2cb4b752015-09-24 03:59:15 +05303674 /*
3675 * sleep 50ms to allow sufficient time for kernel
3676 * drivers to recover incases like SSR.
3677 */
3678 usleep(50000);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003679 ALOGD("%s: exit: status(%d)", __func__, ret);
Vatsal Buchac09ae062018-11-14 13:25:08 +05303680 enable_gcov();
Eric Laurentc8400632013-02-14 19:04:54 -08003681 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003682}
3683
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07003684void lock_input_stream(struct stream_in *in)
3685{
3686 pthread_mutex_lock(&in->pre_lock);
3687 pthread_mutex_lock(&in->lock);
3688 pthread_mutex_unlock(&in->pre_lock);
3689}
3690
3691void lock_output_stream(struct stream_out *out)
3692{
3693 pthread_mutex_lock(&out->pre_lock);
3694 pthread_mutex_lock(&out->lock);
3695 pthread_mutex_unlock(&out->pre_lock);
3696}
3697
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003698/* must be called with out->lock locked */
3699static int send_offload_cmd_l(struct stream_out* out, int command)
3700{
3701 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
3702
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07003703 if (!cmd) {
3704 ALOGE("failed to allocate mem for command 0x%x", command);
3705 return -ENOMEM;
3706 }
3707
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003708 ALOGVV("%s %d", __func__, command);
3709
3710 cmd->cmd = command;
3711 list_add_tail(&out->offload_cmd_list, &cmd->node);
3712 pthread_cond_signal(&out->offload_cond);
3713 return 0;
3714}
3715
Gautam Manam14c198b2020-12-24 14:08:04 +05303716/* must be called with out->lock */
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003717static void stop_compressed_output_l(struct stream_out *out)
3718{
Gautam Manam14c198b2020-12-24 14:08:04 +05303719 pthread_mutex_lock(&out->latch_lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003720 out->offload_state = OFFLOAD_STATE_IDLE;
Gautam Manam14c198b2020-12-24 14:08:04 +05303721 pthread_mutex_unlock(&out->latch_lock);
Weiyin Jiang906db3c2021-03-02 13:17:04 +08003722
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003723 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07003724 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003725 if (out->compr != NULL) {
3726 compress_stop(out->compr);
3727 while (out->offload_thread_blocked) {
3728 pthread_cond_wait(&out->cond, &out->lock);
3729 }
3730 }
3731}
3732
Varun Balaraje49253e2017-07-06 19:48:56 +05303733bool is_interactive_usecase(audio_usecase_t uc_id)
3734{
3735 unsigned int i;
3736 for (i = 0; i < sizeof(interactive_usecases)/sizeof(interactive_usecases[0]); i++) {
3737 if (uc_id == interactive_usecases[i])
3738 return true;
3739 }
3740 return false;
3741}
3742
3743static audio_usecase_t get_interactive_usecase(struct audio_device *adev)
3744{
3745 audio_usecase_t ret_uc = USECASE_INVALID;
3746 unsigned int intract_uc_index;
3747 unsigned int num_usecase = sizeof(interactive_usecases)/sizeof(interactive_usecases[0]);
3748
3749 ALOGV("%s: num_usecase: %d", __func__, num_usecase);
3750 for (intract_uc_index = 0; intract_uc_index < num_usecase; intract_uc_index++) {
3751 if (!(adev->interactive_usecase_state & (0x1 << intract_uc_index))) {
3752 adev->interactive_usecase_state |= 0x1 << intract_uc_index;
3753 ret_uc = interactive_usecases[intract_uc_index];
3754 break;
3755 }
3756 }
3757
3758 ALOGV("%s: Interactive usecase is %d", __func__, ret_uc);
3759 return ret_uc;
3760}
3761
3762static void free_interactive_usecase(struct audio_device *adev,
3763 audio_usecase_t uc_id)
3764{
3765 unsigned int interact_uc_index;
3766 unsigned int num_usecase = sizeof(interactive_usecases)/sizeof(interactive_usecases[0]);
3767
3768 for (interact_uc_index = 0; interact_uc_index < num_usecase; interact_uc_index++) {
3769 if (interactive_usecases[interact_uc_index] == uc_id) {
3770 adev->interactive_usecase_state &= ~(0x1 << interact_uc_index);
3771 break;
3772 }
3773 }
3774 ALOGV("%s: free Interactive usecase %d", __func__, uc_id);
3775}
3776
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003777bool is_offload_usecase(audio_usecase_t uc_id)
3778{
3779 unsigned int i;
3780 for (i = 0; i < sizeof(offload_usecases)/sizeof(offload_usecases[0]); i++) {
3781 if (uc_id == offload_usecases[i])
3782 return true;
3783 }
3784 return false;
3785}
3786
Dhananjay Kumarac341582017-02-23 23:42:25 +05303787static audio_usecase_t get_offload_usecase(struct audio_device *adev, bool is_compress)
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003788{
vivek mehta446c3962015-09-14 10:57:35 -07003789 audio_usecase_t ret_uc = USECASE_INVALID;
3790 unsigned int offload_uc_index;
Alexy Josephb1379942016-01-29 15:49:38 -08003791 unsigned int num_usecase = sizeof(offload_usecases)/sizeof(offload_usecases[0]);
vivek mehta446c3962015-09-14 10:57:35 -07003792 if (!adev->multi_offload_enable) {
Dhananjay Kumarac341582017-02-23 23:42:25 +05303793 if (!is_compress)
vivek mehta446c3962015-09-14 10:57:35 -07003794 ret_uc = USECASE_AUDIO_PLAYBACK_OFFLOAD2;
3795 else
3796 ret_uc = USECASE_AUDIO_PLAYBACK_OFFLOAD;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003797
vivek mehta446c3962015-09-14 10:57:35 -07003798 pthread_mutex_lock(&adev->lock);
3799 if (get_usecase_from_list(adev, ret_uc) != NULL)
3800 ret_uc = USECASE_INVALID;
3801 pthread_mutex_unlock(&adev->lock);
3802
3803 return ret_uc;
3804 }
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003805
3806 ALOGV("%s: num_usecase: %d", __func__, num_usecase);
vivek mehta446c3962015-09-14 10:57:35 -07003807 for (offload_uc_index = 0; offload_uc_index < num_usecase; offload_uc_index++) {
3808 if (!(adev->offload_usecases_state & (0x1 << offload_uc_index))) {
3809 adev->offload_usecases_state |= 0x1 << offload_uc_index;
3810 ret_uc = offload_usecases[offload_uc_index];
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003811 break;
3812 }
3813 }
vivek mehta446c3962015-09-14 10:57:35 -07003814
3815 ALOGV("%s: offload usecase is %d", __func__, ret_uc);
3816 return ret_uc;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003817}
3818
3819static void free_offload_usecase(struct audio_device *adev,
3820 audio_usecase_t uc_id)
3821{
vivek mehta446c3962015-09-14 10:57:35 -07003822 unsigned int offload_uc_index;
Alexy Josephb1379942016-01-29 15:49:38 -08003823 unsigned int num_usecase = sizeof(offload_usecases)/sizeof(offload_usecases[0]);
vivek mehta446c3962015-09-14 10:57:35 -07003824
3825 if (!adev->multi_offload_enable)
3826 return;
3827
3828 for (offload_uc_index = 0; offload_uc_index < num_usecase; offload_uc_index++) {
3829 if (offload_usecases[offload_uc_index] == uc_id) {
3830 adev->offload_usecases_state &= ~(0x1 << offload_uc_index);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003831 break;
3832 }
3833 }
3834 ALOGV("%s: free offload usecase %d", __func__, uc_id);
3835}
3836
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003837static void *offload_thread_loop(void *context)
3838{
3839 struct stream_out *out = (struct stream_out *) context;
3840 struct listnode *item;
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08003841 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003842
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003843 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
Weiyin Jiangd9b5a4e2019-07-18 17:24:21 +08003844 set_sched_policy(0, SP_FOREGROUND);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003845 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
3846
3847 ALOGV("%s", __func__);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07003848 lock_output_stream(out);
juyuchen391b5fa2018-12-12 17:58:09 +08003849 out->offload_state = OFFLOAD_STATE_IDLE;
3850 out->playback_started = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003851 for (;;) {
3852 struct offload_cmd *cmd = NULL;
3853 stream_callback_event_t event;
3854 bool send_callback = false;
3855
3856 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
3857 __func__, list_empty(&out->offload_cmd_list),
3858 out->offload_state);
3859 if (list_empty(&out->offload_cmd_list)) {
3860 ALOGV("%s SLEEPING", __func__);
3861 pthread_cond_wait(&out->offload_cond, &out->lock);
3862 ALOGV("%s RUNNING", __func__);
3863 continue;
3864 }
3865
3866 item = list_head(&out->offload_cmd_list);
3867 cmd = node_to_item(item, struct offload_cmd, node);
3868 list_remove(item);
3869
3870 ALOGVV("%s STATE %d CMD %d out->compr %p",
3871 __func__, out->offload_state, cmd->cmd, out->compr);
3872
3873 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
3874 free(cmd);
3875 break;
3876 }
3877
Haynes Mathew Georgeee5836f2017-11-21 18:02:10 -08003878 // allow OFFLOAD_CMD_ERROR reporting during standby
3879 // this is needed to handle failures during compress_open
3880 // Note however that on a pause timeout, the stream is closed
3881 // and no offload usecase will be active. Therefore this
3882 // special case is needed for compress_open failures alone
3883 if (cmd->cmd != OFFLOAD_CMD_ERROR &&
3884 out->compr == NULL) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003885 ALOGE("%s: Compress handle is NULL", __func__);
Haynes Mathew Georgea9abb202016-06-02 14:13:20 -07003886 free(cmd);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003887 pthread_cond_signal(&out->cond);
3888 continue;
3889 }
3890 out->offload_thread_blocked = true;
3891 pthread_mutex_unlock(&out->lock);
3892 send_callback = false;
3893 switch(cmd->cmd) {
3894 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07003895 ALOGD("copl(%p):calling compress_wait", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003896 compress_wait(out->compr, -1);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07003897 ALOGD("copl(%p):out of compress_wait", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003898 send_callback = true;
3899 event = STREAM_CBK_EVENT_WRITE_READY;
3900 break;
3901 case OFFLOAD_CMD_PARTIAL_DRAIN:
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08003902 ret = compress_next_track(out->compr);
Sidipotu Ashok55820562014-02-10 16:16:38 +05303903 if(ret == 0) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07003904 ALOGD("copl(%p):calling compress_partial_drain", out);
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05303905 ret = compress_partial_drain(out->compr);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07003906 ALOGD("copl(%p):out of compress_partial_drain", out);
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05303907 if (ret < 0)
3908 ret = -errno;
Sidipotu Ashok55820562014-02-10 16:16:38 +05303909 }
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05303910 else if (ret == -ETIMEDOUT)
Aniket Kumar Lata3570fb12017-11-08 15:53:44 -08003911 ret = compress_drain(out->compr);
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08003912 else
3913 ALOGE("%s: Next track returned error %d",__func__, ret);
Aniket Kumar Lata3570fb12017-11-08 15:53:44 -08003914 if (-ENETRESET != ret && !(-EINTR == ret &&
Shubhasini Sugumaran3fde6582022-01-19 15:54:50 -05003915 (CARD_STATUS_OFFLINE == out->card_status ||
3916 POWER_POLICY_STATUS_OFFLINE == adev->out_power_policy))) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05303917 send_callback = true;
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05303918 pthread_mutex_lock(&out->lock);
3919 out->send_new_metadata = 1;
3920 out->send_next_track_params = true;
3921 pthread_mutex_unlock(&out->lock);
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05303922 event = STREAM_CBK_EVENT_DRAIN_READY;
3923 ALOGV("copl(%p):send drain callback, ret %d", out, ret);
3924 } else
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05303925 ALOGI("%s: Block drain ready event during SSR", __func__);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003926 break;
3927 case OFFLOAD_CMD_DRAIN:
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07003928 ALOGD("copl(%p):calling compress_drain", out);
Aniket Kumar Lata3570fb12017-11-08 15:53:44 -08003929 ret = compress_drain(out->compr);
3930 ALOGD("copl(%p):out of compress_drain", out);
3931 // EINTR check avoids drain interruption due to SSR
3932 if (-ENETRESET != ret && !(-EINTR == ret &&
Shubhasini Sugumaran3fde6582022-01-19 15:54:50 -05003933 (CARD_STATUS_OFFLINE == out->card_status ||
3934 POWER_POLICY_STATUS_OFFLINE == adev->out_power_policy))) {
Aniket Kumar Lata3570fb12017-11-08 15:53:44 -08003935 send_callback = true;
3936 event = STREAM_CBK_EVENT_DRAIN_READY;
3937 } else
3938 ALOGI("%s: Block drain ready event during SSR", __func__);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003939 break;
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05303940 case OFFLOAD_CMD_ERROR:
3941 ALOGD("copl(%p): sending error callback to AF", out);
3942 send_callback = true;
3943 event = STREAM_CBK_EVENT_ERROR;
3944 break;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003945 default:
3946 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
3947 break;
3948 }
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07003949 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003950 out->offload_thread_blocked = false;
3951 pthread_cond_signal(&out->cond);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08003952 if (send_callback && out->client_callback) {
3953 ALOGVV("%s: sending client_callback event %d", __func__, event);
3954 out->client_callback(event, NULL, out->client_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -07003955 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003956 free(cmd);
3957 }
3958
3959 pthread_cond_signal(&out->cond);
3960 while (!list_empty(&out->offload_cmd_list)) {
3961 item = list_head(&out->offload_cmd_list);
3962 list_remove(item);
3963 free(node_to_item(item, struct offload_cmd, node));
3964 }
3965 pthread_mutex_unlock(&out->lock);
3966
3967 return NULL;
3968}
3969
3970static int create_offload_callback_thread(struct stream_out *out)
3971{
3972 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
3973 list_init(&out->offload_cmd_list);
3974 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
3975 offload_thread_loop, out);
3976 return 0;
3977}
3978
3979static int destroy_offload_callback_thread(struct stream_out *out)
3980{
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07003981 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003982 stop_compressed_output_l(out);
3983 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
3984
3985 pthread_mutex_unlock(&out->lock);
3986 pthread_join(out->offload_thread, (void **) NULL);
3987 pthread_cond_destroy(&out->offload_cond);
3988
3989 return 0;
3990}
3991
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003992static int stop_output_stream(struct stream_out *out)
3993{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05303994 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003995 struct audio_usecase *uc_info;
3996 struct audio_device *adev = out->dev;
Aalique Grahame22e49102018-12-18 14:23:57 -08003997 bool has_voip_usecase =
3998 get_usecase_from_list(adev, USECASE_AUDIO_PLAYBACK_VOIP) != NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003999
Eric Laurent994a6932013-07-17 11:51:42 -07004000 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07004001 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004002 uc_info = get_usecase_from_list(adev, out->usecase);
4003 if (uc_info == NULL) {
4004 ALOGE("%s: Could not find the usecase (%d) in the list",
4005 __func__, out->usecase);
4006 return -EINVAL;
4007 }
4008
Zhou Songbaddf9f2020-11-20 13:57:39 +08004009 out->a2dp_muted = false;
4010
Derek Chenea197282019-01-07 17:35:01 -08004011 if (audio_extn_ext_hw_plugin_usecase_stop(adev->ext_hw_plugin, uc_info))
4012 ALOGE("%s: failed to stop ext hw plugin", __func__);
Derek Chend2530072014-11-24 12:39:14 -08004013
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004014 if (is_offload_usecase(out->usecase) &&
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05304015 !(audio_extn_passthru_is_passthrough_stream(out))) {
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08004016 if (adev->visualizer_stop_output != NULL)
4017 adev->visualizer_stop_output(out->handle, out->pcm_device_id);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08004018
4019 audio_extn_dts_remove_state_notifier_node(out->usecase);
4020
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08004021 if (adev->offload_effects_stop_output != NULL)
4022 adev->offload_effects_stop_output(out->handle, out->pcm_device_id);
vivek mehtad15d2bf2019-05-17 13:35:10 -07004023 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_ULL ||
4024 out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
4025 audio_low_latency_hint_end();
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08004026 }
Eric Laurentc4aef752013-09-12 17:45:53 -07004027
Arun Mirpuri2ce92a62018-09-12 18:36:10 -07004028 if (out->usecase == USECASE_INCALL_MUSIC_UPLINK ||
4029 out->usecase == USECASE_INCALL_MUSIC_UPLINK2) {
Arun Mirpurief53ce52018-09-11 18:00:09 -07004030 voice_set_device_mute_flag(adev, false);
Arun Mirpuri2ce92a62018-09-12 18:36:10 -07004031 }
Arun Mirpurief53ce52018-09-11 18:00:09 -07004032
Eric Laurent150dbfe2013-02-27 14:31:02 -08004033 /* 1. Get and set stream specific mixer controls */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07004034 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07004035
4036 /* 2. Disable the rx device */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07004037 disable_snd_device(adev, uc_info->out_snd_device);
Zhou Songd9bd9302020-08-04 16:34:45 +08004038 if (out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS)
4039 disable_snd_device(adev, SND_DEVICE_OUT_HAPTICS);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004040
Aalique Grahame22e49102018-12-18 14:23:57 -08004041 audio_extn_extspk_update(adev->extspk);
4042
Xiaojun Sang785b5da2017-08-03 15:52:29 +08004043 if (is_offload_usecase(out->usecase)) {
4044 audio_enable_asm_bit_width_enforce_mode(adev->mixer,
4045 adev->dsp_bit_width_enforce_mode,
4046 false);
4047 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004048 if (is_usb_out_device_type(&out->device_list)) {
Garmond Leung5fd0b552018-04-17 11:56:12 -07004049 ret = audio_extn_usb_check_and_set_svc_int(uc_info,
4050 false);
4051
4052 if (ret != 0)
4053 check_usecases_codec_backend(adev, uc_info, uc_info->out_snd_device);
4054 /* default service interval was successfully updated,
4055 reopen USB backend with new service interval */
4056 ret = 0;
4057 }
Xiaojun Sang785b5da2017-08-03 15:52:29 +08004058
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08004059 list_remove(&uc_info->list);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304060 out->started = 0;
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004061 if (is_offload_usecase(out->usecase) &&
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05304062 (audio_extn_passthru_is_passthrough_stream(out))) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004063 ALOGV("Disable passthrough , reset mixer to pcm");
4064 /* NO_PASSTHROUGH */
Meng Wang4c32fb42020-01-16 17:57:11 +08004065#ifdef AUDIO_GKI_ENABLED
4066 /* out->compr_config.codec->reserved[0] is for compr_passthr */
4067 out->compr_config.codec->reserved[0] = 0;
4068#else
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004069 out->compr_config.codec->compr_passthr = 0;
Meng Wang4c32fb42020-01-16 17:57:11 +08004070#endif
Mingming Yin21854652016-04-13 11:54:02 -07004071 audio_extn_passthru_on_stop(out);
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004072 audio_extn_dolby_set_dap_bypass(adev, DAP_STATE_ON);
4073 }
Eric Laurent07eeafd2013-10-06 12:52:49 -07004074
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05304075 /* Must be called after removing the usecase from list */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004076 if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_AUX_DIGITAL))
Md Mansoor Ahmeddb1b4f92018-01-25 18:56:31 +05304077 audio_extn_keep_alive_start(KEEP_ALIVE_OUT_HDMI);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05304078
Manish Dewangan21a850a2017-08-14 12:03:55 +05304079 if (out->ip_hdlr_handle) {
Naresh Tanniru85819452017-05-04 18:55:45 -07004080 ret = audio_extn_ip_hdlr_intf_close(out->ip_hdlr_handle, true, out);
4081 if (ret < 0)
4082 ALOGE("%s: audio_extn_ip_hdlr_intf_close failed %d",__func__, ret);
4083 }
4084
Zhou Song642ec432020-12-23 16:11:10 +08004085 /* trigger voip input to reroute when voip output changes to hearing aid */
Aalique Grahame22e49102018-12-18 14:23:57 -08004086 if (has_voip_usecase ||
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004087 compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
Aalique Grahame22e49102018-12-18 14:23:57 -08004088 struct listnode *node;
4089 struct audio_usecase *usecase;
4090 list_for_each(node, &adev->usecase_list) {
4091 usecase = node_to_item(node, struct audio_usecase, list);
Zhou Song642ec432020-12-23 16:11:10 +08004092 if (usecase->type == PCM_PLAYBACK || usecase == uc_info ||
4093 (usecase->type == PCM_CAPTURE &&
Revathi Uddarajud9f23d92020-07-27 10:55:06 +05304094 usecase->id != USECASE_AUDIO_RECORD_VOIP &&
Zhou Song642ec432020-12-23 16:11:10 +08004095 usecase->id != USECASE_AUDIO_RECORD_VOIP_LOW_LATENCY))
Aalique Grahame22e49102018-12-18 14:23:57 -08004096 continue;
4097
4098 ALOGD("%s: select_devices at usecase(%d: %s) after removing the usecase(%d: %s)",
4099 __func__, usecase->id, use_case_table[usecase->id],
4100 out->usecase, use_case_table[out->usecase]);
4101 select_devices(adev, usecase->id);
4102 }
4103 }
4104
Zhenlin Lian4f947842022-05-14 15:50:52 +05304105 clear_devices(&uc_info->device_list);
Garmond Leung5fd0b552018-04-17 11:56:12 -07004106 free(uc_info);
Eric Laurent994a6932013-07-17 11:51:42 -07004107 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004108 return ret;
4109}
4110
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004111struct pcm* pcm_open_prepare_helper(unsigned int snd_card, unsigned int pcm_device_id,
4112 unsigned int flags, unsigned int pcm_open_retry_count,
4113 struct pcm_config *config)
4114{
4115 struct pcm* pcm = NULL;
4116
4117 while (1) {
4118 pcm = pcm_open(snd_card, pcm_device_id, flags, config);
4119 if (pcm == NULL || !pcm_is_ready(pcm)) {
4120 ALOGE("%s: %s", __func__, pcm_get_error(pcm));
4121 if (pcm != NULL) {
4122 pcm_close(pcm);
4123 pcm = NULL;
4124 }
Weiyin Jiang72197252019-10-09 11:49:32 +08004125 if (pcm_open_retry_count == 0)
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004126 return NULL;
4127
Weiyin Jiang72197252019-10-09 11:49:32 +08004128 pcm_open_retry_count--;
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004129 usleep(PROXY_OPEN_WAIT_TIME * 1000);
4130 continue;
4131 }
4132 break;
4133 }
4134
4135 if (pcm_is_ready(pcm)) {
4136 int ret = pcm_prepare(pcm);
4137 if (ret < 0) {
4138 ALOGE("%s: pcm_prepare returned %d", __func__, ret);
4139 pcm_close(pcm);
4140 pcm = NULL;
4141 }
4142 }
4143
4144 return pcm;
4145}
4146
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004147int start_output_stream(struct stream_out *out)
4148{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004149 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004150 struct audio_usecase *uc_info;
4151 struct audio_device *adev = out->dev;
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08004152 char mixer_ctl_name[128];
4153 struct mixer_ctl *ctl = NULL;
4154 char* perf_mode[] = {"ULL", "ULL_PP", "LL"};
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304155 bool a2dp_combo = false;
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004156 bool is_haptic_usecase = (out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) ? true: false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004157
Haynes Mathew George380745d2017-10-04 15:27:45 -07004158 ATRACE_BEGIN("start_output_stream");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07004159 if ((out->usecase < 0) || (out->usecase >= AUDIO_USECASE_MAX)) {
4160 ret = -EINVAL;
4161 goto error_config;
4162 }
4163
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004164 ALOGD("%s: enter: stream(%p)usecase(%d: %s) devices(%#x) is_haptic_usecase(%d)",
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05304165 __func__, &out->stream, out->usecase, use_case_table[out->usecase],
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004166 get_device_types(&out->device_list), is_haptic_usecase);
4167
4168 bool is_speaker_active = compare_device_type(&out->device_list,
4169 AUDIO_DEVICE_OUT_SPEAKER);
4170 bool is_speaker_safe_active = compare_device_type(&out->device_list,
4171 AUDIO_DEVICE_OUT_SPEAKER_SAFE);
Naresh Tanniru4c630392014-05-12 01:05:52 +05304172
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304173 if (CARD_STATUS_OFFLINE == out->card_status ||
Shubhasini Sugumaran3fde6582022-01-19 15:54:50 -05004174 CARD_STATUS_OFFLINE == adev->card_status ||
4175 POWER_POLICY_STATUS_OFFLINE == adev->out_power_policy) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304176 ALOGW("out->card_status or adev->card_status offline, try again");
Dhanalakshmi Siddani4d57e992014-07-17 16:37:51 +05304177 ret = -EIO;
Mingshu Pang5fc696f2020-02-28 12:32:00 +08004178 goto error_fatal;
Naresh Tanniru4c630392014-05-12 01:05:52 +05304179 }
Naresh Tanniru4c630392014-05-12 01:05:52 +05304180
Arun Mirpuri2ce92a62018-09-12 18:36:10 -07004181 //Update incall music usecase to reflect correct voice session
4182 if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
4183 ret = voice_extn_check_and_set_incall_music_usecase(adev, out);
4184 if (ret != 0) {
4185 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
4186 __func__, ret);
4187 goto error_config;
4188 }
4189 }
4190
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004191 if (is_a2dp_out_device_type(&out->device_list)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02004192 if (!audio_extn_a2dp_source_is_ready()) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004193 if (is_speaker_active || is_speaker_safe_active) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304194 a2dp_combo = true;
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05304195 } else {
Zhou Songd01e7a22020-09-23 22:49:01 +08004196 if (!is_offload_usecase(out->usecase)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304197 ALOGE("%s: A2DP profile is not ready, return error", __func__);
4198 ret = -EAGAIN;
4199 goto error_config;
4200 }
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05304201 }
4202 }
4203 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004204 if (is_sco_out_device_type(&out->device_list)) {
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304205 if (!adev->bt_sco_on) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004206 if (is_speaker_active) {
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304207 //combo usecase just by pass a2dp
4208 ALOGW("%s: SCO is not connected, route it to speaker", __func__);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004209 reassign_device_list(&out->device_list, AUDIO_DEVICE_OUT_SPEAKER, "");
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304210 } else {
4211 ALOGE("%s: SCO profile is not ready, return error", __func__);
4212 ret = -EAGAIN;
4213 goto error_config;
4214 }
4215 }
4216 }
4217
Eric Laurentb23d5282013-05-14 15:27:20 -07004218 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004219 if (out->pcm_device_id < 0) {
4220 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
4221 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08004222 ret = -EINVAL;
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004223 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004224 }
4225
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004226 if (is_haptic_usecase) {
Meng Wang51d8c2a2020-04-27 15:23:21 +08004227 adev->haptic_pcm_device_id = platform_get_pcm_device_id(
4228 USECASE_AUDIO_PLAYBACK_HAPTICS, PCM_PLAYBACK);
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004229 if (adev->haptic_pcm_device_id < 0) {
4230 ALOGE("%s: Invalid Haptics pcm device id(%d) for the usecase(%d)",
4231 __func__, adev->haptic_pcm_device_id, out->usecase);
4232 ret = -EINVAL;
4233 goto error_config;
4234 }
4235 }
4236
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004237 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07004238
4239 if (!uc_info) {
4240 ret = -ENOMEM;
4241 goto error_config;
4242 }
4243
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004244 uc_info->id = out->usecase;
4245 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08004246 uc_info->stream.out = out;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004247 list_init(&uc_info->device_list);
4248 assign_devices(&uc_info->device_list, &out->device_list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07004249 uc_info->in_snd_device = SND_DEVICE_NONE;
4250 uc_info->out_snd_device = SND_DEVICE_NONE;
Garmond Leung5fd0b552018-04-17 11:56:12 -07004251
4252 /* This must be called before adding this usecase to the list */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004253 if (is_usb_out_device_type(&out->device_list)) {
Garmond Leung5fd0b552018-04-17 11:56:12 -07004254 audio_extn_usb_check_and_set_svc_int(uc_info, true);
4255 /* USB backend is not reopened immediately.
4256 This is eventually done as part of select_devices */
4257 }
4258
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08004259 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004260
Wei Wangf7ca6c92017-11-21 14:51:20 -08004261 audio_streaming_hint_start();
Sudheer Papothifa9d2282015-09-17 01:53:25 +05304262 audio_extn_perf_lock_acquire(&adev->perf_lock_handle, 0,
4263 adev->perf_lock_opts,
4264 adev->perf_lock_opts_size);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05304265
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004266 if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
Md Mansoor Ahmeddb1b4f92018-01-25 18:56:31 +05304267 audio_extn_keep_alive_stop(KEEP_ALIVE_OUT_HDMI);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05304268 if (audio_extn_passthru_is_enabled() &&
4269 audio_extn_passthru_is_passthrough_stream(out)) {
4270 audio_extn_passthru_on_start(out);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05304271 }
4272 }
4273
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004274 if (is_a2dp_out_device_type(&out->device_list) &&
Florian Pfister1a84f312018-07-19 14:38:18 +02004275 (!audio_extn_a2dp_source_is_ready())) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304276 if (!a2dp_combo) {
4277 check_a2dp_restore_l(adev, out, false);
4278 } else {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004279 struct listnode dev;
4280 list_init(&dev);
4281 assign_devices(&dev, &out->device_list);
4282 if (compare_device_type(&dev, AUDIO_DEVICE_OUT_SPEAKER_SAFE))
4283 reassign_device_list(&out->device_list,
4284 AUDIO_DEVICE_OUT_SPEAKER_SAFE, "");
Aalique Grahame22e49102018-12-18 14:23:57 -08004285 else
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004286 reassign_device_list(&out->device_list,
4287 AUDIO_DEVICE_OUT_SPEAKER, "");
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304288 select_devices(adev, out->usecase);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004289 assign_devices(&out->device_list, &dev);
Zhenlin Lian4f947842022-05-14 15:50:52 +05304290 clear_devices(&dev);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304291 }
4292 } else {
Revathi Uddarajub26e3932020-06-10 14:51:02 +05304293 select_devices(adev, out->usecase);
4294 if (is_a2dp_out_device_type(&out->device_list) &&
4295 !adev->a2dp_started) {
4296 if (is_speaker_active || is_speaker_safe_active) {
4297 struct listnode dev;
4298 list_init(&dev);
4299 assign_devices(&dev, &out->device_list);
4300 if (compare_device_type(&dev, AUDIO_DEVICE_OUT_SPEAKER_SAFE))
4301 reassign_device_list(&out->device_list,
4302 AUDIO_DEVICE_OUT_SPEAKER_SAFE, "");
4303 else
4304 reassign_device_list(&out->device_list,
4305 AUDIO_DEVICE_OUT_SPEAKER, "");
4306 select_devices(adev, out->usecase);
4307 assign_devices(&out->device_list, &dev);
Zhenlin Lian4f947842022-05-14 15:50:52 +05304308 clear_devices(&dev);
Revathi Uddarajub26e3932020-06-10 14:51:02 +05304309 } else {
4310 ret = -EINVAL;
4311 goto error_open;
4312 }
4313 }
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304314 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07004315
Arun Mirpuri2ce92a62018-09-12 18:36:10 -07004316 if (out->usecase == USECASE_INCALL_MUSIC_UPLINK ||
4317 out->usecase == USECASE_INCALL_MUSIC_UPLINK2) {
Arun Mirpurief53ce52018-09-11 18:00:09 -07004318 voice_set_device_mute_flag(adev, true);
Arun Mirpuri2ce92a62018-09-12 18:36:10 -07004319 }
Arun Mirpurief53ce52018-09-11 18:00:09 -07004320
Derek Chenea197282019-01-07 17:35:01 -08004321 if (audio_extn_ext_hw_plugin_usecase_start(adev->ext_hw_plugin, uc_info))
4322 ALOGE("%s: failed to start ext hw plugin", __func__);
Derek Chend2530072014-11-24 12:39:14 -08004323
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07004324 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d) format(%#x)",
4325 __func__, adev->snd_card, out->pcm_device_id, out->config.format);
Haynes Mathew George16081042017-05-31 17:16:49 -07004326
4327 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
Arun Mirpuri5d170872019-03-26 13:21:31 -07004328 ALOGD("%s: Starting MMAP stream", __func__);
Haynes Mathew George16081042017-05-31 17:16:49 -07004329 if (out->pcm == NULL || !pcm_is_ready(out->pcm)) {
4330 ALOGE("%s: pcm stream not ready", __func__);
4331 goto error_open;
4332 }
4333 ret = pcm_start(out->pcm);
4334 if (ret < 0) {
4335 ALOGE("%s: MMAP pcm_start failed ret %d", __func__, ret);
4336 goto error_open;
4337 }
Arun Mirpuri5d170872019-03-26 13:21:31 -07004338 out_set_mmap_volume(&out->stream, out->volume_l, out->volume_r);
Haynes Mathew George16081042017-05-31 17:16:49 -07004339 } else if (!is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07004340 unsigned int flags = PCM_OUT;
4341 unsigned int pcm_open_retry_count = 0;
4342 if (out->usecase == USECASE_AUDIO_PLAYBACK_AFE_PROXY) {
4343 flags |= PCM_MMAP | PCM_NOIRQ;
4344 pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT;
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004345 } else if (out->realtime) {
Haynes Mathew George4ab3ba92017-12-11 14:49:43 -08004346 flags |= PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07004347 } else
4348 flags |= PCM_MONOTONIC;
4349
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08004350 if ((adev->vr_audio_mode_enabled) &&
4351 (out->flags & AUDIO_OUTPUT_FLAG_RAW)) {
4352 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
4353 "PCM_Dev %d Topology", out->pcm_device_id);
4354 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
4355 if (!ctl) {
4356 ALOGI("%s: Could not get ctl for mixer cmd might be ULL - %s",
4357 __func__, mixer_ctl_name);
4358 } else {
4359 //if success use ULLPP
4360 ALOGI("%s: mixer ctrl %s succeeded setting up ULL for %d",
4361 __func__, mixer_ctl_name, out->pcm_device_id);
4362 //There is a still a possibility that some sessions
4363 // that request for FAST|RAW when 3D audio is active
4364 //can go through ULLPP. Ideally we expects apps to
4365 //listen to audio focus and stop concurrent playback
4366 //Also, we will look for mode flag (voice_in_communication)
4367 //before enabling the realtime flag.
4368 mixer_ctl_set_enum_by_string(ctl, perf_mode[1]);
4369 }
4370 }
4371
Vatsal Bucha3f39f222021-06-29 16:16:55 +05304372 platform_set_stream_channel_map(adev->platform, out->channel_mask,
4373 out->pcm_device_id, -1, &out->channel_map_param.channel_map[0]);
Surendar Karka91fa3682018-07-02 18:12:12 +05304374
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004375 out->pcm = pcm_open_prepare_helper(adev->snd_card, out->pcm_device_id,
4376 flags, pcm_open_retry_count,
4377 &(out->config));
4378 if (out->pcm == NULL) {
4379 ret = -EIO;
4380 goto error_open;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004381 }
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004382
4383 if (is_haptic_usecase) {
4384 adev->haptic_pcm = pcm_open_prepare_helper(adev->snd_card,
4385 adev->haptic_pcm_device_id,
4386 flags, pcm_open_retry_count,
4387 &(adev->haptics_config));
4388 // failure to open haptics pcm shouldnt stop audio,
4389 // so do not close audio pcm in case of error
Vignesh Kulothungane4039c12019-05-07 15:51:39 -07004390
4391 if (property_get_bool("vendor.audio.enable_haptic_audio_sync", false)) {
4392 ALOGD("%s: enable haptic audio synchronization", __func__);
4393 platform_set_qtime(adev->platform, out->pcm_device_id, adev->haptic_pcm_device_id);
4394 }
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004395 }
4396
Zhou Song2b8f28f2017-09-11 10:51:38 +08004397 // apply volume for voip playback after path is set up
4398 if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP)
4399 out_set_voip_volume(&out->stream, out->volume_l, out->volume_r);
Ramu Gottipati36547092018-12-28 11:32:09 +05304400 else if ((out->usecase == USECASE_AUDIO_PLAYBACK_LOW_LATENCY || out->usecase == USECASE_AUDIO_PLAYBACK_DEEP_BUFFER ||
4401 out->usecase == USECASE_AUDIO_PLAYBACK_ULL) && (out->apply_volume)) {
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05304402 out_set_pcm_volume(&out->stream, out->volume_l, out->volume_r);
4403 out->apply_volume = false;
Derek Chenf13dd492018-11-13 14:53:51 -08004404 } else if (audio_extn_auto_hal_is_bus_device_usecase(out->usecase)) {
4405 out_set_pcm_volume(&out->stream, out->volume_l, out->volume_r);
Krishna Kishor Jha10ad39f2022-02-21 17:02:47 +05304406#ifdef SOFT_VOLUME
4407 out_set_soft_volume_params(&out->stream);
4408#endif
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05304409 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004410 } else {
Zhou Song79929fe2020-01-20 17:57:43 +08004411 /*
4412 * set custom channel map if:
4413 * 1. neither mono nor stereo clips i.e. channels > 2 OR
4414 * 2. custom channel map has been set by client
4415 * else default channel map of FC/FR/FL can always be set to DSP
4416 */
4417 if (popcount(out->channel_mask) > 2 || out->channel_map_param.channel_map[0])
4418 platform_set_stream_channel_map(adev->platform, out->channel_mask,
Weiyin Jiang810a80d2021-01-18 16:04:31 +08004419 out->pcm_device_id, -1, &out->channel_map_param.channel_map[0]);
Xiaojun Sang785b5da2017-08-03 15:52:29 +08004420 audio_enable_asm_bit_width_enforce_mode(adev->mixer,
4421 adev->dsp_bit_width_enforce_mode,
4422 true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004423 out->pcm = NULL;
Haynes Mathew George380745d2017-10-04 15:27:45 -07004424 ATRACE_BEGIN("compress_open");
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08004425 out->compr = compress_open(adev->snd_card,
4426 out->pcm_device_id,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004427 COMPRESS_IN, &out->compr_config);
Haynes Mathew George380745d2017-10-04 15:27:45 -07004428 ATRACE_END();
Satish Babu Patakokila54ce83d2018-07-06 18:00:37 +05304429 if (errno == ENETRESET && !is_compress_ready(out->compr)) {
Sharad Sanglec6f32552018-05-04 16:15:38 +05304430 ALOGE("%s: compress_open failed errno:%d\n", __func__, errno);
4431 adev->card_status = CARD_STATUS_OFFLINE;
4432 out->card_status = CARD_STATUS_OFFLINE;
4433 ret = -EIO;
4434 goto error_open;
4435 }
4436
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004437 if (out->compr && !is_compress_ready(out->compr)) {
Haynes Mathew Georgeee5836f2017-11-21 18:02:10 -08004438 ALOGE("%s: failed /w error %s", __func__, compress_get_error(out->compr));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004439 compress_close(out->compr);
4440 out->compr = NULL;
4441 ret = -EIO;
4442 goto error_open;
4443 }
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05304444 /* compress_open sends params of the track, so reset the flag here */
4445 out->is_compr_metadata_avail = false;
4446
Ben Rombergerd771a7c2017-02-22 18:05:17 -08004447 if (out->client_callback)
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004448 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07004449
Fred Oh3f43e742015-03-04 18:42:34 -08004450 /* Since small bufs uses blocking writes, a write will be blocked
4451 for the default max poll time (20s) in the event of an SSR.
4452 Reduce the poll time to observe and deal with SSR faster.
4453 */
Ashish Jain5106d362016-05-11 19:23:33 +05304454 if (!out->non_blocking) {
Fred Oh3f43e742015-03-04 18:42:34 -08004455 compress_set_max_poll_wait(out->compr, 1000);
4456 }
4457
Manish Dewangan69426c82017-01-30 17:35:36 +05304458 audio_extn_utils_compress_set_render_mode(out);
Manish Dewangan58229382017-02-02 15:48:41 +05304459 audio_extn_utils_compress_set_clk_rec_mode(uc_info);
Manish Dewangan69426c82017-01-30 17:35:36 +05304460
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08004461 audio_extn_dts_create_state_notifier_node(out->usecase);
4462 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
4463 popcount(out->channel_mask),
4464 out->playback_started);
4465
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08004466#ifdef DS1_DOLBY_DDP_ENABLED
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05304467 if (audio_extn_utils_is_dolby_format(out->format))
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08004468 audio_extn_dolby_send_ddp_endp_params(adev);
4469#endif
Preetam Singh Ranawatd18d8832017-02-08 17:34:54 +05304470 if (!(audio_extn_passthru_is_passthrough_stream(out)) &&
4471 (out->sample_rate != 176400 && out->sample_rate <= 192000)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004472 if (adev->visualizer_start_output != NULL)
4473 adev->visualizer_start_output(out->handle, out->pcm_device_id);
4474 if (adev->offload_effects_start_output != NULL)
Ashish Jain5106d362016-05-11 19:23:33 +05304475 adev->offload_effects_start_output(out->handle, out->pcm_device_id, adev->mixer);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08004476 audio_extn_check_and_set_dts_hpx_state(adev);
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004477 }
Derek Chenf13dd492018-11-13 14:53:51 -08004478
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004479 if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_BUS)) {
Derek Chenf13dd492018-11-13 14:53:51 -08004480 /* Update cached volume from media to offload/direct stream */
4481 struct listnode *node = NULL;
4482 list_for_each(node, &adev->active_outputs_list) {
4483 streams_output_ctxt_t *out_ctxt = node_to_item(node,
4484 streams_output_ctxt_t,
4485 list);
4486 if (out_ctxt->output->usecase == USECASE_AUDIO_PLAYBACK_MEDIA) {
4487 out->volume_l = out_ctxt->output->volume_l;
4488 out->volume_r = out_ctxt->output->volume_r;
4489 }
4490 }
4491 out_set_compr_volume(&out->stream,
4492 out->volume_l, out->volume_r);
4493 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004494 }
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004495
4496 if (ret == 0) {
Narsinga Rao Chellaa6e1f702021-10-22 13:57:32 +05304497 if (out->flags == AUDIO_OUTPUT_FLAG_FAST)
4498 register_out_stream(out);
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004499 if (out->realtime) {
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07004500 if (out->pcm == NULL || !pcm_is_ready(out->pcm)) {
4501 ALOGE("%s: pcm stream not ready", __func__);
4502 goto error_open;
4503 }
Haynes Mathew George380745d2017-10-04 15:27:45 -07004504 ATRACE_BEGIN("pcm_start");
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004505 ret = pcm_start(out->pcm);
Haynes Mathew George380745d2017-10-04 15:27:45 -07004506 ATRACE_END();
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004507 if (ret < 0)
4508 goto error_open;
4509 }
4510 }
Wei Wangf7ca6c92017-11-21 14:51:20 -08004511 audio_streaming_hint_end();
Sudheer Papothifa9d2282015-09-17 01:53:25 +05304512 audio_extn_perf_lock_release(&adev->perf_lock_handle);
Jaideep Sharma0fa53812020-09-17 09:00:11 +05304513 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07004514
vivek mehtad15d2bf2019-05-17 13:35:10 -07004515 if (out->usecase == USECASE_AUDIO_PLAYBACK_ULL ||
4516 out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
4517 audio_low_latency_hint_start();
4518 }
4519
Manish Dewangan21a850a2017-08-14 12:03:55 +05304520 if (out->ip_hdlr_handle) {
Vidyakumar Athota6d655882017-05-22 18:26:24 -07004521 ret = audio_extn_ip_hdlr_intf_open(out->ip_hdlr_handle, true, out, out->usecase);
Naresh Tanniru85819452017-05-04 18:55:45 -07004522 if (ret < 0)
4523 ALOGE("%s: audio_extn_ip_hdlr_intf_open failed %d",__func__, ret);
4524 }
4525
Vignesh Kulothungan3b5fae52017-09-25 12:16:30 -07004526 // consider a scenario where on pause lower layers are tear down.
4527 // so on resume, swap mixer control need to be sent only when
4528 // backend is active, hence rather than sending from enable device
4529 // sending it from start of streamtream
4530
4531 platform_set_swap_channels(adev, true);
4532
Haynes Mathew George380745d2017-10-04 15:27:45 -07004533 ATRACE_END();
Vatsal Buchac09ae062018-11-14 13:25:08 +05304534 enable_gcov();
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004535 return ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004536error_open:
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004537 if (adev->haptic_pcm) {
4538 pcm_close(adev->haptic_pcm);
4539 adev->haptic_pcm = NULL;
4540 }
Wei Wangf7ca6c92017-11-21 14:51:20 -08004541 audio_streaming_hint_end();
Sudheer Papothifa9d2282015-09-17 01:53:25 +05304542 audio_extn_perf_lock_release(&adev->perf_lock_handle);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004543 stop_output_stream(out);
Mingshu Pang5fc696f2020-02-28 12:32:00 +08004544error_fatal:
Laxminath Kasam2cb4b752015-09-24 03:59:15 +05304545 /*
4546 * sleep 50ms to allow sufficient time for kernel
4547 * drivers to recover incases like SSR.
4548 */
4549 usleep(50000);
Mingshu Pang5fc696f2020-02-28 12:32:00 +08004550error_config:
Haynes Mathew George380745d2017-10-04 15:27:45 -07004551 ATRACE_END();
Vatsal Buchac09ae062018-11-14 13:25:08 +05304552 enable_gcov();
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08004553 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004554}
4555
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004556static int check_input_parameters(uint32_t sample_rate,
4557 audio_format_t format,
Aalique Grahame22e49102018-12-18 14:23:57 -08004558 int channel_count,
4559 bool is_usb_hifi)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004560{
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08004561 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004562
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05304563 if (((format != AUDIO_FORMAT_PCM_16_BIT) && (format != AUDIO_FORMAT_PCM_8_24_BIT) &&
4564 (format != AUDIO_FORMAT_PCM_24_BIT_PACKED) && (format != AUDIO_FORMAT_PCM_32_BIT) &&
4565 (format != AUDIO_FORMAT_PCM_FLOAT)) &&
Mingming Yine62d7842013-10-25 16:26:03 -07004566 !voice_extn_compress_voip_is_format_supported(format) &&
Ralf Herzaec80262018-07-03 07:08:49 +02004567 !audio_extn_compr_cap_format_supported(format) &&
4568 !audio_extn_cin_format_supported(format))
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004569 ret = -EINVAL;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08004570
Aalique Grahame22e49102018-12-18 14:23:57 -08004571 int max_channel_count = is_usb_hifi ? MAX_HIFI_CHANNEL_COUNT : MAX_CHANNEL_COUNT;
4572 if ((channel_count < MIN_CHANNEL_COUNT) || (channel_count > max_channel_count)) {
4573 ALOGE("%s: unsupported channel count (%d) passed Min / Max (%d / %d)", __func__,
4574 channel_count, MIN_CHANNEL_COUNT, max_channel_count);
4575 return -EINVAL;
4576 }
4577
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08004578 switch (channel_count) {
4579 case 1:
4580 case 2:
Chaithanya Krishna Bacharaju9955b162016-05-25 16:25:53 +05304581 case 3:
4582 case 4:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08004583 case 6:
Karthikeyan Mani07faa602018-08-20 11:01:32 -07004584 case 8:
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05304585 case 10:
4586 case 12:
4587 case 14:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08004588 break;
4589 default:
4590 ret = -EINVAL;
4591 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004592
4593 switch (sample_rate) {
4594 case 8000:
4595 case 11025:
4596 case 12000:
4597 case 16000:
4598 case 22050:
4599 case 24000:
4600 case 32000:
4601 case 44100:
4602 case 48000:
Haynes Mathew Georgec9253d12017-12-13 15:58:28 -08004603 case 88200:
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05304604 case 96000:
Haynes Mathew Georgec9253d12017-12-13 15:58:28 -08004605 case 176400:
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05304606 case 192000:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004607 break;
4608 default:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08004609 ret = -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004610 }
4611
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08004612 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004613}
4614
Naresh Tanniru04f71882018-06-26 17:46:22 +05304615
4616/** Add a value in a list if not already present.
4617 * @return true if value was successfully inserted or already present,
4618 * false if the list is full and does not contain the value.
4619 */
4620static bool register_uint(uint32_t value, uint32_t* list, size_t list_length) {
4621 for (size_t i = 0; i < list_length; i++) {
4622 if (list[i] == value) return true; // value is already present
4623 if (list[i] == 0) { // no values in this slot
4624 list[i] = value;
4625 return true; // value inserted
4626 }
4627 }
4628 return false; // could not insert value
4629}
4630
4631/** Add channel_mask in supported_channel_masks if not already present.
4632 * @return true if channel_mask was successfully inserted or already present,
4633 * false if supported_channel_masks is full and does not contain channel_mask.
4634 */
4635static void register_channel_mask(audio_channel_mask_t channel_mask,
4636 audio_channel_mask_t supported_channel_masks[static MAX_SUPPORTED_CHANNEL_MASKS]) {
4637 ALOGE_IF(!register_uint(channel_mask, supported_channel_masks, MAX_SUPPORTED_CHANNEL_MASKS),
4638 "%s: stream can not declare supporting its channel_mask %x", __func__, channel_mask);
4639}
4640
4641/** Add format in supported_formats if not already present.
4642 * @return true if format was successfully inserted or already present,
4643 * false if supported_formats is full and does not contain format.
4644 */
4645static void register_format(audio_format_t format,
4646 audio_format_t supported_formats[static MAX_SUPPORTED_FORMATS]) {
4647 ALOGE_IF(!register_uint(format, supported_formats, MAX_SUPPORTED_FORMATS),
4648 "%s: stream can not declare supporting its format %x", __func__, format);
4649}
4650/** Add sample_rate in supported_sample_rates if not already present.
4651 * @return true if sample_rate was successfully inserted or already present,
4652 * false if supported_sample_rates is full and does not contain sample_rate.
4653 */
4654static void register_sample_rate(uint32_t sample_rate,
4655 uint32_t supported_sample_rates[static MAX_SUPPORTED_SAMPLE_RATES]) {
4656 ALOGE_IF(!register_uint(sample_rate, supported_sample_rates, MAX_SUPPORTED_SAMPLE_RATES),
4657 "%s: stream can not declare supporting its sample rate %x", __func__, sample_rate);
4658}
4659
Karthikeyan Manib38769c2018-11-07 15:44:13 -08004660static inline uint32_t lcm(uint32_t num1, uint32_t num2)
4661{
4662 uint32_t high = num1, low = num2, temp = 0;
4663
4664 if (!num1 || !num2)
4665 return 0;
4666
4667 if (num1 < num2) {
4668 high = num2;
4669 low = num1;
4670 }
4671
4672 while (low != 0) {
4673 temp = low;
4674 low = high % low;
4675 high = temp;
4676 }
4677 return (num1 * num2)/high;
4678}
4679
4680static inline uint32_t nearest_multiple(uint32_t num, uint32_t multiplier)
4681{
4682 uint32_t remainder = 0;
4683
4684 if (!multiplier)
4685 return num;
4686
4687 remainder = num % multiplier;
4688 if (remainder)
4689 num += (multiplier - remainder);
4690
4691 return num;
4692}
4693
Aalique Grahame22e49102018-12-18 14:23:57 -08004694static size_t get_stream_buffer_size(size_t duration_ms,
4695 uint32_t sample_rate,
4696 audio_format_t format,
4697 int channel_count,
4698 bool is_low_latency)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004699{
4700 size_t size = 0;
Karthikeyan Manib38769c2018-11-07 15:44:13 -08004701 uint32_t bytes_per_period_sample = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004702
Aalique Grahame22e49102018-12-18 14:23:57 -08004703 size = (sample_rate * duration_ms) / 1000;
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +05304704 if (is_low_latency){
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +05304705#ifndef PLATFORM_AUTO
4706 size = configured_low_latency_capture_period_size;
4707#else
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +05304708 switch(sample_rate) {
4709 case 48000:
4710 size = 240;
4711 break;
4712 case 32000:
4713 size = 160;
4714 break;
4715 case 24000:
4716 size = 120;
4717 break;
4718 case 16000:
4719 size = 80;
4720 break;
4721 case 8000:
4722 size = 40;
4723 break;
4724 default:
4725 size = 240;
4726 }
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +05304727#endif
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +05304728 }
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05304729
Karthikeyan Manib38769c2018-11-07 15:44:13 -08004730 bytes_per_period_sample = audio_bytes_per_sample(format) * channel_count;
Aalique Grahame22e49102018-12-18 14:23:57 -08004731 size *= audio_bytes_per_sample(format) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004732
Ralf Herzbd08d632018-09-28 15:50:49 +02004733 /* make sure the size is multiple of 32 bytes and additionally multiple of
4734 * the frame_size (required for 24bit samples and non-power-of-2 channel counts)
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07004735 * At 48 kHz mono 16-bit PCM:
4736 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
4737 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
Karthikeyan Manib38769c2018-11-07 15:44:13 -08004738 * Also, make sure the size is multiple of bytes per period sample
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07004739 */
Karthikeyan Manib38769c2018-11-07 15:44:13 -08004740 size = nearest_multiple(size, lcm(32, bytes_per_period_sample));
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07004741
4742 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004743}
4744
Aalique Grahame22e49102018-12-18 14:23:57 -08004745static size_t get_input_buffer_size(uint32_t sample_rate,
4746 audio_format_t format,
4747 int channel_count,
4748 bool is_low_latency)
4749{
Avinash Chandrad7296d42021-08-04 15:07:47 +05304750 bool is_usb_hifi = IS_USB_HIFI;
Aalique Grahame22e49102018-12-18 14:23:57 -08004751 /* Don't know if USB HIFI in this context so use true to be conservative */
4752 if (check_input_parameters(sample_rate, format, channel_count,
Avinash Chandrad7296d42021-08-04 15:07:47 +05304753 is_usb_hifi) != 0)
Aalique Grahame22e49102018-12-18 14:23:57 -08004754 return 0;
4755
4756 return get_stream_buffer_size(AUDIO_CAPTURE_PERIOD_DURATION_MSEC,
4757 sample_rate,
4758 format,
4759 channel_count,
4760 is_low_latency);
4761}
4762
Derek Chenf6318be2017-06-12 17:16:24 -04004763size_t get_output_period_size(uint32_t sample_rate,
4764 audio_format_t format,
4765 int channel_count,
4766 int duration /*in millisecs*/)
Ashish Jain058165c2016-09-28 23:18:48 +05304767{
4768 size_t size = 0;
4769 uint32_t bytes_per_sample = audio_bytes_per_sample(format);
4770
4771 if ((duration == 0) || (sample_rate == 0) ||
4772 (bytes_per_sample == 0) || (channel_count == 0)) {
4773 ALOGW("Invalid config duration %d sr %d bps %d ch %d", duration, sample_rate,
4774 bytes_per_sample, channel_count);
4775 return -EINVAL;
4776 }
4777
4778 size = (sample_rate *
4779 duration *
4780 bytes_per_sample *
4781 channel_count) / 1000;
4782 /*
4783 * To have same PCM samples for all channels, the buffer size requires to
4784 * be multiple of (number of channels * bytes per sample)
4785 * For writes to succeed, the buffer must be written at address which is multiple of 32
4786 */
4787 size = ALIGN(size, (bytes_per_sample * channel_count * 32));
4788
4789 return (size/(channel_count * bytes_per_sample));
4790}
4791
Zhou Song48453a02018-01-10 17:50:59 +08004792static uint64_t get_actual_pcm_frames_rendered(struct stream_out *out, struct timespec *timestamp)
Ashish Jain5106d362016-05-11 19:23:33 +05304793{
4794 uint64_t actual_frames_rendered = 0;
Weiyin Jiang4813da12020-05-28 00:37:28 +08004795 uint64_t written_frames = 0;
4796 uint64_t kernel_frames = 0;
4797 uint64_t dsp_frames = 0;
4798 uint64_t signed_frames = 0;
4799 size_t kernel_buffer_size = 0;
Ashish Jain5106d362016-05-11 19:23:33 +05304800
4801 /* This adjustment accounts for buffering after app processor.
4802 * It is based on estimated DSP latency per use case, rather than exact.
4803 */
George Gao9ba8a142020-07-23 14:30:03 -07004804 dsp_frames = platform_render_latency(out) *
Weiyin Jiang4813da12020-05-28 00:37:28 +08004805 out->sample_rate / 1000000LL;
Ashish Jain5106d362016-05-11 19:23:33 +05304806
Zhou Song48453a02018-01-10 17:50:59 +08004807 pthread_mutex_lock(&out->position_query_lock);
Weiyin Jiang4813da12020-05-28 00:37:28 +08004808 written_frames = out->written /
4809 (audio_bytes_per_sample(out->hal_ip_format) * popcount(out->channel_mask));
4810
Ashish Jain5106d362016-05-11 19:23:33 +05304811 /* not querying actual state of buffering in kernel as it would involve an ioctl call
4812 * which then needs protection, this causes delay in TS query for pcm_offload usecase
4813 * hence only estimate.
4814 */
Weiyin Jiang4813da12020-05-28 00:37:28 +08004815 kernel_buffer_size = out->compr_config.fragment_size * out->compr_config.fragments;
4816 kernel_frames = kernel_buffer_size /
4817 (audio_bytes_per_sample(out->hal_op_format) * popcount(out->channel_mask));
Ashish Jain5106d362016-05-11 19:23:33 +05304818
Weiyin Jiang4813da12020-05-28 00:37:28 +08004819 if (written_frames >= (kernel_frames + dsp_frames))
4820 signed_frames = written_frames - kernel_frames - dsp_frames;
Ashish Jain5106d362016-05-11 19:23:33 +05304821
Zhou Song48453a02018-01-10 17:50:59 +08004822 if (signed_frames > 0) {
Ashish Jain5106d362016-05-11 19:23:33 +05304823 actual_frames_rendered = signed_frames;
Zhou Song48453a02018-01-10 17:50:59 +08004824 if (timestamp != NULL )
4825 *timestamp = out->writeAt;
4826 } else if (timestamp != NULL) {
4827 clock_gettime(CLOCK_MONOTONIC, timestamp);
4828 }
4829 pthread_mutex_unlock(&out->position_query_lock);
Ashish Jain5106d362016-05-11 19:23:33 +05304830
Weiyin Jiang4813da12020-05-28 00:37:28 +08004831 ALOGVV("%s signed frames %lld written frames %lld kernel frames %lld dsp frames %lld",
4832 __func__, signed_frames, written_frames, kernel_frames, dsp_frames);
Ashish Jain5106d362016-05-11 19:23:33 +05304833
4834 return actual_frames_rendered;
4835}
4836
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004837static uint32_t out_get_sample_rate(const struct audio_stream *stream)
4838{
4839 struct stream_out *out = (struct stream_out *)stream;
4840
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004841 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004842}
4843
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07004844static int out_set_sample_rate(struct audio_stream *stream __unused,
4845 uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004846{
4847 return -ENOSYS;
4848}
4849
4850static size_t out_get_buffer_size(const struct audio_stream *stream)
4851{
4852 struct stream_out *out = (struct stream_out *)stream;
4853
Varun Balaraje49253e2017-07-06 19:48:56 +05304854 if (is_interactive_usecase(out->usecase)) {
Sri Karri27279e12017-08-07 16:05:20 +05304855 return out->config.period_size * out->config.period_count;
Varun Balaraje49253e2017-07-06 19:48:56 +05304856 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Naresh Tanniruee3499a2017-01-05 14:05:35 +05304857 if (out->flags & AUDIO_OUTPUT_FLAG_TIMESTAMP)
4858 return out->compr_config.fragment_size - sizeof(struct snd_codec_metadata);
4859 else
4860 return out->compr_config.fragment_size;
4861 } else if(out->usecase == USECASE_COMPRESS_VOIP_CALL)
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08004862 return voice_extn_compress_voip_out_get_buffer_size(out);
Dhananjay Kumarac341582017-02-23 23:42:25 +05304863 else if (is_offload_usecase(out->usecase) &&
4864 out->flags == AUDIO_OUTPUT_FLAG_DIRECT)
Ashish Jain83a6cc22016-06-28 14:34:17 +05304865 return out->hal_fragment_size;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004866
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004867 return out->config.period_size * out->af_period_multiplier *
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07004868 audio_stream_out_frame_size((const struct audio_stream_out *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004869}
4870
4871static uint32_t out_get_channels(const struct audio_stream *stream)
4872{
4873 struct stream_out *out = (struct stream_out *)stream;
4874
4875 return out->channel_mask;
4876}
4877
4878static audio_format_t out_get_format(const struct audio_stream *stream)
4879{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004880 struct stream_out *out = (struct stream_out *)stream;
4881
4882 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004883}
4884
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07004885static int out_set_format(struct audio_stream *stream __unused,
4886 audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004887{
4888 return -ENOSYS;
4889}
4890
4891static int out_standby(struct audio_stream *stream)
4892{
4893 struct stream_out *out = (struct stream_out *)stream;
4894 struct audio_device *adev = out->dev;
Haynes Mathew George16081042017-05-31 17:16:49 -07004895 bool do_stop = true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004896
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05304897 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
4898 stream, out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004899
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07004900 lock_output_stream(out);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004901 if (!out->standby) {
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07004902 if (adev->adm_deregister_stream)
4903 adev->adm_deregister_stream(adev->adm_data, out->handle);
4904
Weiyin Jiang280ea742020-09-08 20:28:22 +08004905 if (is_offload_usecase(out->usecase)) {
Haynes Mathew George7fce0a52016-06-23 18:22:27 -07004906 stop_compressed_output_l(out);
Weiyin Jiang280ea742020-09-08 20:28:22 +08004907 }
Haynes Mathew George7fce0a52016-06-23 18:22:27 -07004908
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08004909 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004910 out->standby = true;
Zhou Songa8895042016-07-05 17:54:22 +08004911 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
4912 voice_extn_compress_voip_close_output_stream(stream);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304913 out->started = 0;
Zhou Songa8895042016-07-05 17:54:22 +08004914 pthread_mutex_unlock(&adev->lock);
4915 pthread_mutex_unlock(&out->lock);
4916 ALOGD("VOIP output entered standby");
4917 return 0;
4918 } else if (!is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004919 if (out->pcm) {
4920 pcm_close(out->pcm);
4921 out->pcm = NULL;
4922 }
Meng Wanga09da002020-04-20 12:56:04 +08004923 if (out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) {
4924 if (adev->haptic_pcm) {
4925 pcm_close(adev->haptic_pcm);
4926 adev->haptic_pcm = NULL;
4927 }
4928
4929 if (adev->haptic_buffer != NULL) {
4930 free(adev->haptic_buffer);
4931 adev->haptic_buffer = NULL;
4932 adev->haptic_buffer_size = 0;
4933 }
4934 adev->haptic_pcm_device_id = 0;
4935 }
4936
Haynes Mathew George16081042017-05-31 17:16:49 -07004937 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
4938 do_stop = out->playback_started;
4939 out->playback_started = false;
Phil Burkd898ba62019-06-20 12:49:01 -07004940
4941 if (out->mmap_shared_memory_fd >= 0) {
4942 ALOGV("%s: closing mmap_shared_memory_fd = %d",
4943 __func__, out->mmap_shared_memory_fd);
4944 close(out->mmap_shared_memory_fd);
4945 out->mmap_shared_memory_fd = -1;
4946 }
Haynes Mathew George16081042017-05-31 17:16:49 -07004947 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004948 } else {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07004949 ALOGD("copl(%p):standby", out);
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05304950 out->send_next_track_params = false;
4951 out->is_compr_metadata_avail = false;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004952 out->gapless_mdata.encoder_delay = 0;
4953 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004954 if (out->compr != NULL) {
4955 compress_close(out->compr);
4956 out->compr = NULL;
4957 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08004958 }
Haynes Mathew George16081042017-05-31 17:16:49 -07004959 if (do_stop) {
4960 stop_output_stream(out);
4961 }
Lakshman Chaluvaraju06677b42019-06-24 10:04:52 +05304962 // if fm is active route on selected device in UI
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004963 audio_extn_fm_route_on_selected_device(adev, &out->device_list);
Eric Laurent150dbfe2013-02-27 14:31:02 -08004964 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004965 }
4966 pthread_mutex_unlock(&out->lock);
Aniket Kumar Latad13758f2020-08-06 15:11:36 -07004967 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004968 return 0;
4969}
4970
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304971static int out_on_error(struct audio_stream *stream)
4972{
4973 struct stream_out *out = (struct stream_out *)stream;
Ben Rombergerfd02a2f2018-09-17 10:23:23 -07004974 int status = 0;
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304975
4976 lock_output_stream(out);
Haynes Mathew Georgeee5836f2017-11-21 18:02:10 -08004977 // always send CMD_ERROR for offload streams, this
4978 // is needed e.g. when SSR happens within compress_open
4979 // since the stream is active, offload_callback_thread is also active.
4980 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
4981 stop_compressed_output_l(out);
Ben Rombergerfd02a2f2018-09-17 10:23:23 -07004982 }
4983 pthread_mutex_unlock(&out->lock);
4984
4985 status = out_standby(&out->stream.common);
4986
4987 lock_output_stream(out);
4988 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Haynes Mathew Georgeee5836f2017-11-21 18:02:10 -08004989 send_offload_cmd_l(out, OFFLOAD_CMD_ERROR);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304990 }
Manisha Agarwal7b3e3772019-02-20 14:33:45 +05304991
4992 if (is_offload_usecase(out->usecase) && out->card_status == CARD_STATUS_OFFLINE) {
4993 ALOGD("Setting previous card status if offline");
4994 out->prev_card_status_offline = true;
4995 }
4996
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304997 pthread_mutex_unlock(&out->lock);
4998
Ben Rombergerfd02a2f2018-09-17 10:23:23 -07004999 return status;
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305000}
5001
Ashish Jain1b9b30c2017-05-18 20:57:40 +05305002/*
Weiyin Jiang280ea742020-09-08 20:28:22 +08005003 * standby implementation without locks, assumes that the callee already
5004 * has taken adev and out lock.
Ashish Jain1b9b30c2017-05-18 20:57:40 +05305005 */
5006int out_standby_l(struct audio_stream *stream)
5007{
5008 struct stream_out *out = (struct stream_out *)stream;
5009 struct audio_device *adev = out->dev;
5010
5011 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
5012 stream, out->usecase, use_case_table[out->usecase]);
5013
5014 if (!out->standby) {
Haynes Mathew George380745d2017-10-04 15:27:45 -07005015 ATRACE_BEGIN("out_standby_l");
Ashish Jain1b9b30c2017-05-18 20:57:40 +05305016 if (adev->adm_deregister_stream)
5017 adev->adm_deregister_stream(adev->adm_data, out->handle);
5018
Weiyin Jiang280ea742020-09-08 20:28:22 +08005019 if (is_offload_usecase(out->usecase)) {
Ashish Jain1b9b30c2017-05-18 20:57:40 +05305020 stop_compressed_output_l(out);
Weiyin Jiang280ea742020-09-08 20:28:22 +08005021 }
Ashish Jain1b9b30c2017-05-18 20:57:40 +05305022
5023 out->standby = true;
5024 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
5025 voice_extn_compress_voip_close_output_stream(stream);
5026 out->started = 0;
5027 ALOGD("VOIP output entered standby");
Haynes Mathew George380745d2017-10-04 15:27:45 -07005028 ATRACE_END();
Ashish Jain1b9b30c2017-05-18 20:57:40 +05305029 return 0;
5030 } else if (!is_offload_usecase(out->usecase)) {
5031 if (out->pcm) {
5032 pcm_close(out->pcm);
5033 out->pcm = NULL;
5034 }
Vignesh Kulothungana6927272019-02-20 15:17:07 -08005035 if (out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) {
5036 if (adev->haptic_pcm) {
5037 pcm_close(adev->haptic_pcm);
5038 adev->haptic_pcm = NULL;
5039 }
5040
5041 if (adev->haptic_buffer != NULL) {
5042 free(adev->haptic_buffer);
5043 adev->haptic_buffer = NULL;
5044 adev->haptic_buffer_size = 0;
5045 }
5046 adev->haptic_pcm_device_id = 0;
5047 }
Ashish Jain1b9b30c2017-05-18 20:57:40 +05305048 } else {
5049 ALOGD("copl(%p):standby", out);
5050 out->send_next_track_params = false;
5051 out->is_compr_metadata_avail = false;
5052 out->gapless_mdata.encoder_delay = 0;
5053 out->gapless_mdata.encoder_padding = 0;
5054 if (out->compr != NULL) {
5055 compress_close(out->compr);
5056 out->compr = NULL;
5057 }
5058 }
5059 stop_output_stream(out);
Haynes Mathew George380745d2017-10-04 15:27:45 -07005060 ATRACE_END();
Ashish Jain1b9b30c2017-05-18 20:57:40 +05305061 }
Aniket Kumar Latad13758f2020-08-06 15:11:36 -07005062 ALOGV("%s: exit", __func__);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05305063 return 0;
5064}
5065
Aalique Grahame22e49102018-12-18 14:23:57 -08005066static int out_dump(const struct audio_stream *stream, int fd)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005067{
Aalique Grahame22e49102018-12-18 14:23:57 -08005068 struct stream_out *out = (struct stream_out *)stream;
5069
5070 // We try to get the lock for consistency,
5071 // but it isn't necessary for these variables.
5072 // If we're not in standby, we may be blocked on a write.
5073 const bool locked = (pthread_mutex_trylock(&out->lock) == 0);
5074 dprintf(fd, " Standby: %s\n", out->standby ? "yes" : "no");
5075 dprintf(fd, " Frames written: %lld\n", (long long)out->written);
Dechen Chai22768452021-07-30 09:29:16 +05305076#ifndef LINUX_ENABLED
Andy Hunga1f48fa2019-07-01 18:14:53 -07005077 char buffer[256]; // for statistics formatting
5078 if (!is_offload_usecase(out->usecase)) {
5079 simple_stats_to_string(&out->fifo_underruns, buffer, sizeof(buffer));
5080 dprintf(fd, " Fifo frame underruns: %s\n", buffer);
5081 }
5082
Andy Hungc6bfd4a2019-07-01 18:26:00 -07005083 if (out->start_latency_ms.n > 0) {
5084 simple_stats_to_string(&out->start_latency_ms, buffer, sizeof(buffer));
5085 dprintf(fd, " Start latency ms: %s\n", buffer);
5086 }
Dechen Chai22768452021-07-30 09:29:16 +05305087#endif
Aalique Grahame22e49102018-12-18 14:23:57 -08005088 if (locked) {
5089 pthread_mutex_unlock(&out->lock);
5090 }
5091
Dechen Chai22768452021-07-30 09:29:16 +05305092#ifndef LINUX_ENABLED
Aalique Grahame22e49102018-12-18 14:23:57 -08005093 // dump error info
5094 (void)error_log_dump(
5095 out->error_log, fd, " " /* prefix */, 0 /* lines */, 0 /* limit_ns */);
Dechen Chai22768452021-07-30 09:29:16 +05305096#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005097 return 0;
5098}
5099
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005100static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
5101{
5102 int ret = 0;
5103 char value[32];
ApurupaPattapu2e084df2013-12-18 15:47:59 -08005104
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005105 if (!out || !parms) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08005106 ALOGE("%s: return invalid ",__func__);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005107 return -EINVAL;
5108 }
5109
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +05305110 ret = audio_extn_parse_compress_metadata(out, parms);
Weiyin Jiang18ac4e92015-03-15 15:03:40 +08005111
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005112 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
5113 if (ret >= 0) {
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +05305114 out->gapless_mdata.encoder_delay = atoi(value); //whats a good limit check?
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005115 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005116 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
5117 if (ret >= 0) {
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +05305118 out->gapless_mdata.encoder_padding = atoi(value);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005119 }
5120
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005121 ALOGV("%s new encoder delay %u and padding %u", __func__,
5122 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
5123
5124 return 0;
5125}
5126
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07005127static bool output_drives_call(struct audio_device *adev, struct stream_out *out)
5128{
5129 return out == adev->primary_output || out == adev->voice_tx_output;
5130}
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005131
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305132// note: this call is safe only if the stream_cb is
5133// removed first in close_output_stream (as is done now).
5134static void out_snd_mon_cb(void * stream, struct str_parms * parms)
5135{
5136 if (!stream || !parms)
5137 return;
5138
5139 struct stream_out *out = (struct stream_out *)stream;
5140 struct audio_device *adev = out->dev;
5141
5142 card_status_t status;
5143 int card;
5144 if (parse_snd_card_status(parms, &card, &status) < 0)
5145 return;
5146
5147 pthread_mutex_lock(&adev->lock);
5148 bool valid_cb = (card == adev->snd_card);
5149 pthread_mutex_unlock(&adev->lock);
5150
5151 if (!valid_cb)
5152 return;
5153
5154 lock_output_stream(out);
5155 if (out->card_status != status)
5156 out->card_status = status;
5157 pthread_mutex_unlock(&out->lock);
5158
5159 ALOGI("out_snd_mon_cb for card %d usecase %s, status %s", card,
5160 use_case_table[out->usecase],
5161 status == CARD_STATUS_OFFLINE ? "offline" : "online");
5162
Aditya Bavanari59ebed42019-02-05 17:44:57 +05305163 if (status == CARD_STATUS_OFFLINE) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305164 out_on_error(stream);
Aditya Bavanari59ebed42019-02-05 17:44:57 +05305165 if (voice_is_call_state_active(adev) &&
5166 out == adev->primary_output) {
5167 ALOGD("%s: SSR/PDR occurred, end all calls\n", __func__);
5168 pthread_mutex_lock(&adev->lock);
5169 voice_stop_call(adev);
5170 adev->mode = AUDIO_MODE_NORMAL;
5171 pthread_mutex_unlock(&adev->lock);
5172 }
5173 }
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305174 return;
5175}
5176
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005177int route_output_stream(struct stream_out *out,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005178 struct listnode *devices)
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005179{
5180 struct audio_device *adev = out->dev;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005181 int ret = 0;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005182 struct listnode new_devices;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005183 bool bypass_a2dp = false;
5184 bool reconfig = false;
5185 unsigned long service_interval = 0;
5186
5187 ALOGD("%s: enter: usecase(%d: %s) devices %x",
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005188 __func__, out->usecase, use_case_table[out->usecase], get_device_types(devices));
5189
5190 list_init(&new_devices);
5191 assign_devices(&new_devices, devices);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005192
5193 lock_output_stream(out);
5194 pthread_mutex_lock(&adev->lock);
5195
5196 /*
5197 * When HDMI cable is unplugged the music playback is paused and
5198 * the policy manager sends routing=0. But the audioflinger continues
5199 * to write data until standby time (3sec). As the HDMI core is
5200 * turned off, the write gets blocked.
5201 * Avoid this by routing audio to speaker until standby.
5202 */
Aniket Kumar Lata9c2fd892020-01-22 22:20:00 -08005203 if (is_single_device_type_equal(&out->device_list,
5204 AUDIO_DEVICE_OUT_AUX_DIGITAL) &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005205 list_empty(&new_devices) &&
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005206 !audio_extn_passthru_is_passthrough_stream(out) &&
5207 (platform_get_edid_info(adev->platform) != 0) /* HDMI disconnected */) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005208 reassign_device_list(&new_devices, AUDIO_DEVICE_OUT_SPEAKER, "");
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005209 }
5210 /*
5211 * When A2DP is disconnected the
5212 * music playback is paused and the policy manager sends routing=0
5213 * But the audioflinger continues to write data until standby time
5214 * (3sec). As BT is turned off, the write gets blocked.
5215 * Avoid this by routing audio to speaker until standby.
5216 */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005217 if (is_a2dp_out_device_type(&out->device_list) &&
5218 list_empty(&new_devices) &&
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005219 !audio_extn_a2dp_source_is_ready() &&
5220 !adev->bt_sco_on) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005221 reassign_device_list(&new_devices, AUDIO_DEVICE_OUT_SPEAKER, "");
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005222 }
5223 /*
Weiyin Jiangabedea32020-12-09 12:49:19 +08005224 * When USB headset is disconnected the music playback paused
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005225 * and the policy manager send routing=0. But if the USB is connected
5226 * back before the standby time, AFE is not closed and opened
5227 * when USB is connected back. So routing to speker will guarantee
5228 * AFE reconfiguration and AFE will be opend once USB is connected again
5229 */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005230 if (is_usb_out_device_type(&out->device_list) &&
5231 list_empty(&new_devices) &&
5232 !audio_extn_usb_connected(NULL)) {
Sujin Panicker84d953a2020-11-16 17:39:54 +05305233 if (adev->mode == AUDIO_MODE_IN_CALL || adev->mode == AUDIO_MODE_IN_COMMUNICATION)
5234 reassign_device_list(&new_devices, AUDIO_DEVICE_OUT_EARPIECE, "");
5235 else
5236 reassign_device_list(&new_devices, AUDIO_DEVICE_OUT_SPEAKER, "");
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005237 }
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005238 /* To avoid a2dp to sco overlapping / BT device improper state
5239 * check with BT lib about a2dp streaming support before routing
5240 */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005241 if (is_a2dp_out_device_type(&new_devices)) {
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005242 if (!audio_extn_a2dp_source_is_ready()) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005243 if (compare_device_type(&new_devices, AUDIO_DEVICE_OUT_SPEAKER) ||
5244 compare_device_type(&new_devices, AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005245 //combo usecase just by pass a2dp
5246 ALOGW("%s: A2DP profile is not ready,routing to speaker only", __func__);
5247 bypass_a2dp = true;
5248 } else {
5249 ALOGE("%s: A2DP profile is not ready,ignoring routing request", __func__);
5250 /* update device to a2dp and don't route as BT returned error
5251 * However it is still possible a2dp routing called because
5252 * of current active device disconnection (like wired headset)
5253 */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005254 assign_devices(&out->device_list, &new_devices);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005255 pthread_mutex_unlock(&adev->lock);
5256 pthread_mutex_unlock(&out->lock);
5257 goto error;
5258 }
5259 }
5260 }
5261
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005262 // Workaround: If routing to an non existing usb device, fail gracefully
5263 // The routing request will otherwise block during 10 second
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005264 if (is_usb_out_device_type(&new_devices)) {
5265 struct str_parms *parms =
5266 str_parms_create_str(get_usb_device_address(&new_devices));
5267 if (!parms)
5268 goto error;
Weiyin Jiangabedea32020-12-09 12:49:19 +08005269 if (!audio_extn_usb_connected(NULL)) {
5270 ALOGW("%s: ignoring rerouting to non existing USB card", __func__);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005271 pthread_mutex_unlock(&adev->lock);
5272 pthread_mutex_unlock(&out->lock);
5273 str_parms_destroy(parms);
5274 ret = -ENOSYS;
5275 goto error;
5276 }
5277 str_parms_destroy(parms);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005278 }
5279
Weiyin Jiang80c8f9a2020-01-10 20:42:08 +08005280 // Workaround: If routing to an non existing hdmi device, fail gracefully
5281 if (compare_device_type(&new_devices, AUDIO_DEVICE_OUT_AUX_DIGITAL) &&
5282 (platform_get_edid_info_v2(adev->platform,
5283 out->extconn.cs.controller,
5284 out->extconn.cs.stream) != 0)) {
5285 ALOGW("out_set_parameters() ignoring rerouting to non existing HDMI/DP");
5286 pthread_mutex_unlock(&adev->lock);
5287 pthread_mutex_unlock(&out->lock);
5288 ret = -ENOSYS;
5289 goto error;
5290 }
5291
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005292 /*
5293 * select_devices() call below switches all the usecases on the same
5294 * backend to the new device. Refer to check_usecases_codec_backend() in
5295 * the select_devices(). But how do we undo this?
5296 *
5297 * For example, music playback is active on headset (deep-buffer usecase)
5298 * and if we go to ringtones and select a ringtone, low-latency usecase
5299 * will be started on headset+speaker. As we can't enable headset+speaker
5300 * and headset devices at the same time, select_devices() switches the music
5301 * playback to headset+speaker while starting low-lateny usecase for ringtone.
5302 * So when the ringtone playback is completed, how do we undo the same?
5303 *
5304 * We are relying on the out_set_parameters() call on deep-buffer output,
5305 * once the ringtone playback is ended.
5306 * NOTE: We should not check if the current devices are same as new devices.
5307 * Because select_devices() must be called to switch back the music
5308 * playback to headset.
5309 */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005310 if (!list_empty(&new_devices)) {
5311 bool same_dev = compare_devices(&out->device_list, &new_devices);
5312 assign_devices(&out->device_list, &new_devices);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005313
5314 if (output_drives_call(adev, out)) {
5315 if (!voice_is_call_state_active(adev)) {
5316 if (adev->mode == AUDIO_MODE_IN_CALL) {
5317 adev->current_call_output = out;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005318 ret = voice_start_call(adev);
5319 }
5320 } else {
Kunlei Zhang253ad102020-10-14 16:21:28 +08005321 platform_is_volume_boost_supported_device(adev->platform, &new_devices);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005322 adev->current_call_output = out;
5323 voice_update_devices_for_all_voice_usecases(adev);
5324 }
5325 }
5326
Mingshu Pang971ff702020-09-09 15:28:22 +08005327 if (is_usb_out_device_type(&out->device_list)) {
5328 service_interval = audio_extn_usb_find_service_interval(false, true /*playback*/);
5329 audio_extn_usb_set_service_interval(true /*playback*/,
5330 service_interval,
5331 &reconfig);
5332 ALOGD("%s, svc_int(%ld),reconfig(%d)",__func__,service_interval, reconfig);
5333 }
5334
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005335 if (!out->standby) {
5336 if (!same_dev) {
5337 ALOGV("update routing change");
5338 audio_extn_perf_lock_acquire(&adev->perf_lock_handle, 0,
5339 adev->perf_lock_opts,
5340 adev->perf_lock_opts_size);
5341 if (adev->adm_on_routing_change)
5342 adev->adm_on_routing_change(adev->adm_data,
5343 out->handle);
5344 }
5345 if (!bypass_a2dp) {
5346 select_devices(adev, out->usecase);
5347 } else {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005348 if (compare_device_type(&new_devices, AUDIO_DEVICE_OUT_SPEAKER_SAFE))
5349 reassign_device_list(&out->device_list, AUDIO_DEVICE_OUT_SPEAKER_SAFE, "");
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005350 else
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005351 reassign_device_list(&out->device_list, AUDIO_DEVICE_OUT_SPEAKER, "");
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005352 select_devices(adev, out->usecase);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005353 assign_devices(&out->device_list, &new_devices);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005354 }
5355
5356 if (!same_dev) {
5357 // on device switch force swap, lower functions will make sure
5358 // to check if swap is allowed or not.
5359 platform_set_swap_channels(adev, true);
5360 audio_extn_perf_lock_release(&adev->perf_lock_handle);
5361 }
Zhou Songbaddf9f2020-11-20 13:57:39 +08005362 pthread_mutex_lock(&out->latch_lock);
5363 if (!is_a2dp_out_device_type(&out->device_list) || audio_extn_a2dp_source_is_ready()) {
5364 if (out->a2dp_muted) {
5365 out->a2dp_muted = false;
5366 if (is_offload_usecase(out->usecase))
5367 out_set_compr_volume(&out->stream, out->volume_l, out->volume_r);
5368 else if (out->usecase != USECASE_AUDIO_PLAYBACK_VOIP)
5369 out_set_pcm_volume(&out->stream, out->volume_l, out->volume_r);
Weiyin Jiang280ea742020-09-08 20:28:22 +08005370 }
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005371 }
Zhou Songbaddf9f2020-11-20 13:57:39 +08005372 if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP && !out->a2dp_muted)
5373 out_set_voip_volume(&out->stream, out->volume_l, out->volume_r);
5374 pthread_mutex_unlock(&out->latch_lock);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005375 }
5376 }
5377
5378 pthread_mutex_unlock(&adev->lock);
5379 pthread_mutex_unlock(&out->lock);
5380
5381 /*handles device and call state changes*/
5382 audio_extn_extspk_update(adev->extspk);
5383
Revathi Uddaraju4255a632021-12-02 05:11:13 -08005384 clear_devices(&new_devices);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005385error:
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005386 ALOGV("%s: exit: code(%d)", __func__, ret);
5387 return ret;
5388}
5389
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005390static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
5391{
5392 struct stream_out *out = (struct stream_out *)stream;
5393 struct audio_device *adev = out->dev;
5394 struct str_parms *parms;
5395 char value[32];
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005396 int ret = 0, err;
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07005397 int ext_controller = -1;
5398 int ext_stream = -1;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005399
sangwoobc677242013-08-08 16:53:43 +09005400 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07005401 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005402 parms = str_parms_create_str(kvpairs);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05305403 if (!parms)
5404 goto error;
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07005405
5406 err = platform_get_controller_stream_from_params(parms, &ext_controller,
5407 &ext_stream);
Vignesh Kulothungan1b3957e2019-12-02 16:16:51 -08005408 if (err == 0) {
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07005409 out->extconn.cs.controller = ext_controller;
5410 out->extconn.cs.stream = ext_stream;
5411 ALOGD("%s: usecase(%s) new controller/stream (%d/%d)", __func__,
5412 use_case_table[out->usecase], out->extconn.cs.controller,
5413 out->extconn.cs.stream);
5414 }
5415
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07005416 if (out == adev->primary_output) {
5417 pthread_mutex_lock(&adev->lock);
5418 audio_extn_set_parameters(adev, parms);
5419 pthread_mutex_unlock(&adev->lock);
5420 }
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07005421 if (is_offload_usecase(out->usecase)) {
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005422 lock_output_stream(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005423 parse_compress_metadata(out, parms);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08005424
5425 audio_extn_dts_create_state_notifier_node(out->usecase);
5426 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
5427 popcount(out->channel_mask),
5428 out->playback_started);
5429
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08005430 pthread_mutex_unlock(&out->lock);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005431 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07005432
Surendar Karkaf51b5842018-04-26 11:28:38 +05305433 err = str_parms_get_str(parms, AUDIO_PARAMETER_DUAL_MONO, value,
5434 sizeof(value));
5435 if (err >= 0) {
5436 if (!strncmp("true", value, sizeof("true")) || atoi(value))
5437 audio_extn_send_dual_mono_mixing_coefficients(out);
5438 }
5439
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +05305440 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_PROFILE, value, sizeof(value));
5441 if (err >= 0) {
5442 strlcpy(out->profile, value, sizeof(out->profile));
5443 ALOGV("updating stream profile with value '%s'", out->profile);
5444 lock_output_stream(out);
5445 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
5446 &adev->streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005447 &out->device_list, out->flags,
5448 out->hal_op_format,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +05305449 out->sample_rate, out->bit_width,
5450 out->channel_mask, out->profile,
5451 &out->app_type_cfg);
5452 pthread_mutex_unlock(&out->lock);
5453 }
5454
Alexy Joseph98988832017-01-13 14:56:59 -08005455 //suspend, resume handling block
Aniket Kumar Lata932f4872017-11-06 18:29:44 -08005456 //remove QOS only if vendor.audio.hal.dynamic.qos.config.supported is set to true
5457 // and vendor.audio.hal.output.suspend.supported is set to true
5458 if (out->hal_output_suspend_supported && out->dynamic_pm_qos_config_supported) {
Alexy Joseph98988832017-01-13 14:56:59 -08005459 //check suspend parameter only for low latency and if the property
5460 //is enabled
5461 if (str_parms_get_str(parms, "suspend_playback", value, sizeof(value)) >= 0) {
5462 ALOGI("%s: got suspend_playback %s", __func__, value);
5463 lock_output_stream(out);
5464 if (!strncmp(value, "false", 5)) {
5465 //suspend_playback=false is supposed to set QOS value back to 75%
5466 //the mixer control sent with value Enable will achieve that
5467 ret = audio_route_apply_and_update_path(adev->audio_route, out->pm_qos_mixer_path);
5468 } else if (!strncmp (value, "true", 4)) {
5469 //suspend_playback=true is supposed to remove QOS value
5470 //resetting the mixer control will set the default value
5471 //for the mixer control which is Disable and this removes the QOS vote
5472 ret = audio_route_reset_and_update_path(adev->audio_route, out->pm_qos_mixer_path);
5473 } else {
5474 ALOGE("%s: Wrong value sent for suspend_playback, expected true/false,"
5475 " got %s", __func__, value);
5476 ret = -1;
5477 }
5478
5479 if (ret != 0) {
5480 ALOGE("%s: %s mixer ctl failed with %d, ignore suspend/resume setparams",
5481 __func__, out->pm_qos_mixer_path, ret);
5482 }
5483
5484 pthread_mutex_unlock(&out->lock);
5485 }
5486 }
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07005487
Alexy Joseph98988832017-01-13 14:56:59 -08005488 //end suspend, resume handling block
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005489 str_parms_destroy(parms);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05305490error:
Eric Laurent994a6932013-07-17 11:51:42 -07005491 ALOGV("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005492 return ret;
5493}
5494
Paul McLeana50b7332018-12-17 08:24:21 -07005495static int in_set_microphone_direction(const struct audio_stream_in *stream,
5496 audio_microphone_direction_t dir) {
justinweng20fb6d82019-02-21 18:49:00 -07005497 struct stream_in *in = (struct stream_in *)stream;
5498
5499 ALOGVV("%s: standby %d source %d dir %d", __func__, in->standby, in->source, dir);
5500
5501 in->direction = dir;
5502
5503 if (in->standby)
5504 return 0;
5505
5506 return audio_extn_audiozoom_set_microphone_direction(in, dir);
Paul McLeana50b7332018-12-17 08:24:21 -07005507}
5508
5509static int in_set_microphone_field_dimension(const struct audio_stream_in *stream, float zoom) {
justinweng20fb6d82019-02-21 18:49:00 -07005510 struct stream_in *in = (struct stream_in *)stream;
5511
5512 ALOGVV("%s: standby %d source %d zoom %f", __func__, in->standby, in->source, zoom);
5513
5514 if (zoom > 1.0 || zoom < -1.0)
5515 return -EINVAL;
5516
5517 in->zoom = zoom;
5518
5519 if (in->standby)
5520 return 0;
5521
5522 return audio_extn_audiozoom_set_microphone_field_dimension(in, zoom);
Paul McLeana50b7332018-12-17 08:24:21 -07005523}
5524
5525
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005526static bool stream_get_parameter_channels(struct str_parms *query,
5527 struct str_parms *reply,
5528 audio_channel_mask_t *supported_channel_masks) {
5529 int ret = -1;
5530 char value[512];
5531 bool first = true;
5532 size_t i, j;
5533
5534 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) {
5535 ret = 0;
5536 value[0] = '\0';
5537 i = 0;
5538 while (supported_channel_masks[i] != 0) {
5539 for (j = 0; j < ARRAY_SIZE(channels_name_to_enum_table); j++) {
5540 if (channels_name_to_enum_table[j].value == supported_channel_masks[i]) {
5541 if (!first)
Satya Krishna Pindiprolib22ac722017-10-09 15:44:16 +05305542 strlcat(value, "|", sizeof(value));
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005543
Satya Krishna Pindiprolib22ac722017-10-09 15:44:16 +05305544 strlcat(value, channels_name_to_enum_table[j].name, sizeof(value));
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005545 first = false;
5546 break;
5547 }
5548 }
5549 i++;
5550 }
5551 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
5552 }
5553 return ret == 0;
5554}
5555
5556static bool stream_get_parameter_formats(struct str_parms *query,
5557 struct str_parms *reply,
5558 audio_format_t *supported_formats) {
5559 int ret = -1;
5560 char value[256];
5561 size_t i, j;
5562 bool first = true;
5563
5564 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) {
5565 ret = 0;
5566 value[0] = '\0';
5567 i = 0;
5568 while (supported_formats[i] != 0) {
5569 for (j = 0; j < ARRAY_SIZE(formats_name_to_enum_table); j++) {
5570 if (formats_name_to_enum_table[j].value == supported_formats[i]) {
5571 if (!first) {
Satya Krishna Pindiprolib22ac722017-10-09 15:44:16 +05305572 strlcat(value, "|", sizeof(value));
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005573 }
Satya Krishna Pindiprolib22ac722017-10-09 15:44:16 +05305574 strlcat(value, formats_name_to_enum_table[j].name, sizeof(value));
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005575 first = false;
5576 break;
5577 }
5578 }
5579 i++;
5580 }
5581 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_FORMATS, value);
5582 }
5583 return ret == 0;
5584}
5585
5586static bool stream_get_parameter_rates(struct str_parms *query,
5587 struct str_parms *reply,
5588 uint32_t *supported_sample_rates) {
5589
5590 int i;
5591 char value[256];
5592 int ret = -1;
5593 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) {
5594 ret = 0;
5595 value[0] = '\0';
5596 i=0;
5597 int cursor = 0;
5598 while (supported_sample_rates[i]) {
5599 int avail = sizeof(value) - cursor;
5600 ret = snprintf(value + cursor, avail, "%s%d",
5601 cursor > 0 ? "|" : "",
5602 supported_sample_rates[i]);
5603 if (ret < 0 || ret >= avail) {
5604 // if cursor is at the last element of the array
5605 // overwrite with \0 is duplicate work as
5606 // snprintf already put a \0 in place.
5607 // else
5608 // we had space to write the '|' at value[cursor]
5609 // (which will be overwritten) or no space to fill
5610 // the first element (=> cursor == 0)
5611 value[cursor] = '\0';
5612 break;
5613 }
5614 cursor += ret;
5615 ++i;
5616 }
5617 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES,
5618 value);
5619 }
5620 return ret >= 0;
5621}
5622
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005623static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
5624{
5625 struct stream_out *out = (struct stream_out *)stream;
5626 struct str_parms *query = str_parms_create_str(keys);
Alexy Josephaee4fdd2016-01-29 13:02:07 -08005627 char *str = (char*) NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005628 char value[256];
5629 struct str_parms *reply = str_parms_create();
5630 size_t i, j;
5631 int ret;
5632 bool first = true;
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07005633
5634 if (!query || !reply) {
Alexy Josephaee4fdd2016-01-29 13:02:07 -08005635 if (reply) {
5636 str_parms_destroy(reply);
5637 }
5638 if (query) {
5639 str_parms_destroy(query);
5640 }
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07005641 ALOGE("out_get_parameters: failed to allocate mem for query or reply");
5642 return NULL;
5643 }
5644
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005645 ALOGV("%s: %s enter: keys - %s", __func__, use_case_table[out->usecase], keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005646 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
5647 if (ret >= 0) {
5648 value[0] = '\0';
5649 i = 0;
5650 while (out->supported_channel_masks[i] != 0) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005651 for (j = 0; j < ARRAY_SIZE(channels_name_to_enum_table); j++) {
5652 if (channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005653 if (!first) {
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -08005654 strlcat(value, "|", sizeof(value));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005655 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005656 strlcat(value, channels_name_to_enum_table[j].name, sizeof(value));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005657 first = false;
5658 break;
5659 }
5660 }
5661 i++;
5662 }
5663 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
5664 str = str_parms_to_str(reply);
5665 } else {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08005666 voice_extn_out_get_parameters(out, query, reply);
5667 str = str_parms_to_str(reply);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005668 }
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07005669
Alexy Joseph62142aa2015-11-16 15:10:34 -08005670
5671 ret = str_parms_get_str(query, "is_direct_pcm_track", value, sizeof(value));
5672 if (ret >= 0) {
5673 value[0] = '\0';
Dhananjay Kumarac341582017-02-23 23:42:25 +05305674 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
5675 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Alexy Joseph62142aa2015-11-16 15:10:34 -08005676 ALOGV("in direct_pcm");
Satya Krishna Pindiprolib6655542017-07-03 19:38:19 +05305677 strlcat(value, "true", sizeof(value));
Alexy Joseph62142aa2015-11-16 15:10:34 -08005678 } else {
5679 ALOGV("not in direct_pcm");
Sharad Sangle3dd5a4a2015-12-10 18:39:17 +05305680 strlcat(value, "false", sizeof(value));
Alexy Joseph62142aa2015-11-16 15:10:34 -08005681 }
5682 str_parms_add_str(reply, "is_direct_pcm_track", value);
Alexy Josephaee4fdd2016-01-29 13:02:07 -08005683 if (str)
5684 free(str);
Alexy Joseph62142aa2015-11-16 15:10:34 -08005685 str = str_parms_to_str(reply);
5686 }
5687
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07005688 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS, value, sizeof(value));
5689 if (ret >= 0) {
5690 value[0] = '\0';
5691 i = 0;
5692 first = true;
5693 while (out->supported_formats[i] != 0) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005694 for (j = 0; j < ARRAY_SIZE(formats_name_to_enum_table); j++) {
5695 if (formats_name_to_enum_table[j].value == out->supported_formats[i]) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07005696 if (!first) {
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -08005697 strlcat(value, "|", sizeof(value));
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07005698 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005699 strlcat(value, formats_name_to_enum_table[j].name, sizeof(value));
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07005700 first = false;
5701 break;
5702 }
5703 }
5704 i++;
5705 }
5706 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_FORMATS, value);
Alexy Josephaee4fdd2016-01-29 13:02:07 -08005707 if (str)
5708 free(str);
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07005709 str = str_parms_to_str(reply);
5710 }
Mingming Yin3a941d42016-02-17 18:08:05 -08005711
5712 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES, value, sizeof(value));
5713 if (ret >= 0) {
5714 value[0] = '\0';
5715 i = 0;
5716 first = true;
5717 while (out->supported_sample_rates[i] != 0) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005718 for (j = 0; j < ARRAY_SIZE(out_sample_rates_name_to_enum_table); j++) {
5719 if (out_sample_rates_name_to_enum_table[j].value == out->supported_sample_rates[i]) {
Mingming Yin3a941d42016-02-17 18:08:05 -08005720 if (!first) {
5721 strlcat(value, "|", sizeof(value));
5722 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005723 strlcat(value, out_sample_rates_name_to_enum_table[j].name, sizeof(value));
Mingming Yin3a941d42016-02-17 18:08:05 -08005724 first = false;
5725 break;
5726 }
5727 }
5728 i++;
5729 }
5730 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES, value);
5731 if (str)
5732 free(str);
5733 str = str_parms_to_str(reply);
5734 }
5735
Alexy Joseph98988832017-01-13 14:56:59 -08005736 if (str_parms_get_str(query, "supports_hw_suspend", value, sizeof(value)) >= 0) {
5737 //only low latency track supports suspend_resume
5738 str_parms_add_int(reply, "supports_hw_suspend",
Aniket Kumar Lata932f4872017-11-06 18:29:44 -08005739 (out->hal_output_suspend_supported));
Alexy Joseph98988832017-01-13 14:56:59 -08005740 if (str)
5741 free(str);
5742 str = str_parms_to_str(reply);
5743 }
5744
5745
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005746 str_parms_destroy(query);
5747 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07005748 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005749 return str;
5750}
5751
5752static uint32_t out_get_latency(const struct audio_stream_out *stream)
5753{
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005754 uint32_t period_ms;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005755 struct stream_out *out = (struct stream_out *)stream;
Alexy Josephaa54c872014-12-03 02:46:47 -08005756 uint32_t latency = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005757
Alexy Josephaa54c872014-12-03 02:46:47 -08005758 if (is_offload_usecase(out->usecase)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05305759 lock_output_stream(out);
5760 latency = audio_extn_utils_compress_get_dsp_latency(out);
5761 pthread_mutex_unlock(&out->lock);
Haynes Mathew George16081042017-05-31 17:16:49 -07005762 } else if ((out->realtime) ||
5763 (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP)) {
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005764 // since the buffer won't be filled up faster than realtime,
5765 // return a smaller number
5766 if (out->config.rate)
5767 period_ms = (out->af_period_multiplier * out->config.period_size *
5768 1000) / (out->config.rate);
5769 else
5770 period_ms = 0;
George Gao9ba8a142020-07-23 14:30:03 -07005771 latency = period_ms + platform_render_latency(out) / 1000;
Alexy Josephaa54c872014-12-03 02:46:47 -08005772 } else {
5773 latency = (out->config.period_count * out->config.period_size * 1000) /
Mingshu Pangf69a88d2021-04-30 16:14:39 +08005774 (out->config.rate);
pavanisra2d95d82022-02-09 18:55:58 +05305775 if (out->usecase == USECASE_AUDIO_PLAYBACK_DEEP_BUFFER ||
5776 out->usecase == USECASE_AUDIO_PLAYBACK_LOW_LATENCY)
Mingshu Pangf69a88d2021-04-30 16:14:39 +08005777 latency += platform_render_latency(out)/1000;
Alexy Josephaa54c872014-12-03 02:46:47 -08005778 }
5779
Zhou Songd2537a02020-06-11 22:04:46 +08005780 if (!out->standby && is_a2dp_out_device_type(&out->device_list))
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08005781 latency += audio_extn_a2dp_get_encoder_latency();
5782
Anish Kumar50ebcbf2014-12-09 04:01:39 +05305783 ALOGV("%s: Latency %d", __func__, latency);
Alexy Josephaa54c872014-12-03 02:46:47 -08005784 return latency;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005785}
5786
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05305787static float AmpToDb(float amplification)
5788{
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05305789 float db = DSD_VOLUME_MIN_DB;
5790 if (amplification > 0) {
5791 db = 20 * log10(amplification);
5792 if(db < DSD_VOLUME_MIN_DB)
5793 return DSD_VOLUME_MIN_DB;
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05305794 }
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05305795 return db;
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05305796}
5797
Krishna Kishor Jha10ad39f2022-02-21 17:02:47 +05305798#ifdef SOFT_VOLUME
5799static int out_set_soft_volume_params(struct audio_stream_out *stream)
5800{
5801 struct stream_out *out = (struct stream_out *)stream;
5802 int ret = 0;
5803 char mixer_ctl_name[128];
5804 struct audio_device *adev = out->dev;
5805 struct mixer_ctl *ctl = NULL;
5806 struct soft_step_volume_params *volume_params = NULL;
5807
5808 int pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
5809 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name), "Playback %d Soft Vol Params", pcm_device_id);
5810 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
5811 if (!ctl) {
5812 ALOGE("%s : Could not get ctl for mixer cmd - %s", __func__, mixer_ctl_name);
5813 return -EINVAL;
5814 }
5815
5816 volume_params =(struct soft_step_volume_params * ) malloc(sizeof(struct soft_step_volume_params));
5817 if (volume_params == NULL){
5818 ALOGE("%s : malloc is failed for volume params", __func__);
5819 return -EINVAL;
5820 } else {
5821 ret = platform_get_soft_step_volume_params(volume_params,out->usecase);
5822 if (ret < 0) {
5823 ALOGE("%s : platform_get_soft_step_volume_params is fialed", __func__);
Karan Naidu28b335a2022-05-18 23:00:08 +05305824 ret = -EINVAL;
5825 goto ERR_EXIT;
Krishna Kishor Jha10ad39f2022-02-21 17:02:47 +05305826 }
5827
5828 }
5829 ret = mixer_ctl_set_array(ctl, volume_params, sizeof(struct soft_step_volume_params)/sizeof(int));
5830 if (ret < 0) {
5831 ALOGE("%s: Could not set ctl, error:%d ", __func__, ret);
Karan Naidu28b335a2022-05-18 23:00:08 +05305832 ret = -EINVAL;
5833 goto ERR_EXIT;
5834 }
5835
5836 if (volume_params) {
5837 free(volume_params);
Krishna Kishor Jha10ad39f2022-02-21 17:02:47 +05305838 }
5839 return 0;
Karan Naidu28b335a2022-05-18 23:00:08 +05305840
5841ERR_EXIT:
5842 if (volume_params) {
5843 free(volume_params);
5844 }
5845 return ret;
Krishna Kishor Jha10ad39f2022-02-21 17:02:47 +05305846}
5847#endif
5848
Arun Mirpuri5d170872019-03-26 13:21:31 -07005849static int out_set_mmap_volume(struct audio_stream_out *stream, float left,
5850 float right)
5851{
5852 struct stream_out *out = (struct stream_out *)stream;
5853 long volume = 0;
5854 char mixer_ctl_name[128] = "";
5855 struct audio_device *adev = out->dev;
5856 struct mixer_ctl *ctl = NULL;
5857 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
5858 PCM_PLAYBACK);
5859
5860 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
5861 "Playback %d Volume", pcm_device_id);
5862 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
5863 if (!ctl) {
5864 ALOGE("%s: Could not get ctl for mixer cmd - %s",
5865 __func__, mixer_ctl_name);
5866 return -EINVAL;
5867 }
5868 if (left != right)
5869 ALOGW("%s: Left and right channel volume mismatch:%f,%f",
5870 __func__, left, right);
5871 volume = (long)(left * (MMAP_PLAYBACK_VOLUME_MAX*1.0));
5872 if (mixer_ctl_set_value(ctl, 0, volume) < 0){
5873 ALOGE("%s:ctl for mixer cmd - %s, volume %ld returned error",
5874 __func__, mixer_ctl_name, volume);
5875 return -EINVAL;
5876 }
5877 return 0;
5878}
5879
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05305880static int out_set_compr_volume(struct audio_stream_out *stream, float left,
5881 float right)
5882{
5883 struct stream_out *out = (struct stream_out *)stream;
Manish Dewangan338c50a2017-09-12 15:22:03 +05305884 long volume[2];
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05305885 char mixer_ctl_name[128];
5886 struct audio_device *adev = out->dev;
5887 struct mixer_ctl *ctl;
5888 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
5889 PCM_PLAYBACK);
5890
5891 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
5892 "Compress Playback %d Volume", pcm_device_id);
5893 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
5894 if (!ctl) {
5895 ALOGE("%s: Could not get ctl for mixer cmd - %s",
5896 __func__, mixer_ctl_name);
5897 return -EINVAL;
5898 }
5899 ALOGE("%s:ctl for mixer cmd - %s, left %f, right %f",
5900 __func__, mixer_ctl_name, left, right);
5901 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
5902 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
5903 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
5904
5905 return 0;
5906}
5907
Zhou Song2b8f28f2017-09-11 10:51:38 +08005908static int out_set_voip_volume(struct audio_stream_out *stream, float left,
5909 float right)
5910{
5911 struct stream_out *out = (struct stream_out *)stream;
5912 char mixer_ctl_name[] = "App Type Gain";
5913 struct audio_device *adev = out->dev;
5914 struct mixer_ctl *ctl;
Manish Dewangan338c50a2017-09-12 15:22:03 +05305915 long set_values[4];
Zhou Song2b8f28f2017-09-11 10:51:38 +08005916
Aniket Kumar Lata8426d1f2019-06-10 15:25:53 -07005917 if (!is_valid_volume(left, right)) {
5918 ALOGE("%s: Invalid stream volume for left=%f, right=%f",
5919 __func__, left, right);
5920 return -EINVAL;
5921 }
5922
Zhou Song2b8f28f2017-09-11 10:51:38 +08005923 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
5924 if (!ctl) {
5925 ALOGE("%s: Could not get ctl for mixer cmd - %s",
5926 __func__, mixer_ctl_name);
5927 return -EINVAL;
5928 }
5929
5930 set_values[0] = 0; //0: Rx Session 1:Tx Session
5931 set_values[1] = out->app_type_cfg.app_type;
Manish Dewangan338c50a2017-09-12 15:22:03 +05305932 set_values[2] = (long)(left * VOIP_PLAYBACK_VOLUME_MAX);
5933 set_values[3] = (long)(right * VOIP_PLAYBACK_VOLUME_MAX);
Zhou Song2b8f28f2017-09-11 10:51:38 +08005934
5935 mixer_ctl_set_array(ctl, set_values, ARRAY_SIZE(set_values));
5936 return 0;
5937}
5938
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05305939static int out_set_pcm_volume(struct audio_stream_out *stream, float left,
5940 float right)
5941{
5942 struct stream_out *out = (struct stream_out *)stream;
5943 /* Volume control for pcm playback */
5944 if (left != right) {
5945 return -EINVAL;
5946 } else {
5947 char mixer_ctl_name[128];
5948 struct audio_device *adev = out->dev;
5949 struct mixer_ctl *ctl;
5950 int pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
5951 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name), "Playback %d Volume", pcm_device_id);
5952 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
5953 if (!ctl) {
5954 ALOGE("%s : Could not get ctl for mixer cmd - %s", __func__, mixer_ctl_name);
5955 return -EINVAL;
5956 }
5957
5958 int volume = (int) (left * PCM_PLAYBACK_VOLUME_MAX);
5959 int ret = mixer_ctl_set_value(ctl, 0, volume);
5960 if (ret < 0) {
5961 ALOGE("%s: Could not set ctl, error:%d ", __func__, ret);
5962 return -EINVAL;
5963 }
5964
5965 ALOGV("%s : Pcm set volume value %d left %f", __func__, volume, left);
5966
5967 return 0;
5968 }
5969}
5970
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005971static int out_set_volume(struct audio_stream_out *stream, float left,
5972 float right)
5973{
Eric Laurenta9024de2013-04-04 09:19:12 -07005974 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005975 int volume[2];
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05305976 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005977
Arun Mirpuri5d170872019-03-26 13:21:31 -07005978 ALOGD("%s: called with left_vol=%f, right_vol=%f", __func__, left, right);
Eric Laurenta9024de2013-04-04 09:19:12 -07005979 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
5980 /* only take left channel into account: the API is for stereo anyway */
5981 out->muted = (left == 0.0f);
5982 return 0;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07005983 } else if (is_offload_usecase(out->usecase)) {
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05305984 if (audio_extn_passthru_is_passthrough_stream(out)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07005985 /*
5986 * Set mute or umute on HDMI passthrough stream.
5987 * Only take left channel into account.
5988 * Mute is 0 and unmute 1
5989 */
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05305990 audio_extn_passthru_set_volume(out, (left == 0.0f));
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05305991 } else if (out->format == AUDIO_FORMAT_DSD){
5992 char mixer_ctl_name[128] = "DSD Volume";
5993 struct audio_device *adev = out->dev;
5994 struct mixer_ctl *ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
5995
5996 if (!ctl) {
5997 ALOGE("%s: Could not get ctl for mixer cmd - %s",
5998 __func__, mixer_ctl_name);
5999 return -EINVAL;
6000 }
Manish Dewangan338c50a2017-09-12 15:22:03 +05306001 volume[0] = (long)(AmpToDb(left));
6002 volume[1] = (long)(AmpToDb(right));
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05306003 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
6004 return 0;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08006005 } else if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_BUS) &&
Derek Chendf05eea2019-08-01 13:57:49 -07006006 (out->car_audio_stream == CAR_AUDIO_STREAM_MEDIA)) {
Philippe Gravel1c9ac352019-05-28 16:08:37 -07006007 ALOGD("%s: Overriding offload set volume for media bus stream", __func__);
6008 struct listnode *node = NULL;
6009 list_for_each(node, &adev->active_outputs_list) {
6010 streams_output_ctxt_t *out_ctxt = node_to_item(node,
6011 streams_output_ctxt_t,
6012 list);
6013 if (out_ctxt->output->usecase == USECASE_AUDIO_PLAYBACK_MEDIA) {
6014 out->volume_l = out_ctxt->output->volume_l;
6015 out->volume_r = out_ctxt->output->volume_r;
6016 }
6017 }
Weiyin Jiang280ea742020-09-08 20:28:22 +08006018 pthread_mutex_lock(&out->latch_lock);
Zhou Songbaddf9f2020-11-20 13:57:39 +08006019 if (!out->a2dp_muted) {
Philippe Gravel1c9ac352019-05-28 16:08:37 -07006020 ret = out_set_compr_volume(&out->stream, out->volume_l, out->volume_r);
6021 }
Weiyin Jiang280ea742020-09-08 20:28:22 +08006022 pthread_mutex_unlock(&out->latch_lock);
Philippe Gravel1c9ac352019-05-28 16:08:37 -07006023 return ret;
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07006024 } else {
Weiyin Jiang280ea742020-09-08 20:28:22 +08006025 pthread_mutex_lock(&out->latch_lock);
Zhou Songbaddf9f2020-11-20 13:57:39 +08006026 ALOGV("%s: compress mute %d", __func__, out->a2dp_muted);
6027 if (!out->a2dp_muted)
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05306028 ret = out_set_compr_volume(stream, left, right);
6029 out->volume_l = left;
6030 out->volume_r = right;
Weiyin Jiang280ea742020-09-08 20:28:22 +08006031 pthread_mutex_unlock(&out->latch_lock);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05306032 return ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006033 }
Vikram Panduranga93f080e2017-06-07 18:16:14 -07006034 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP) {
Aalique Grahame22e49102018-12-18 14:23:57 -08006035 out->app_type_cfg.gain[0] = (int)(left * VOIP_PLAYBACK_VOLUME_MAX);
6036 out->app_type_cfg.gain[1] = (int)(right * VOIP_PLAYBACK_VOLUME_MAX);
Zhou Songbaddf9f2020-11-20 13:57:39 +08006037 pthread_mutex_lock(&out->latch_lock);
Aalique Grahame22e49102018-12-18 14:23:57 -08006038 if (!out->standby) {
6039 audio_extn_utils_send_app_type_gain(out->dev,
6040 out->app_type_cfg.app_type,
6041 &out->app_type_cfg.gain[0]);
Zhou Songbaddf9f2020-11-20 13:57:39 +08006042 if (!out->a2dp_muted)
6043 ret = out_set_voip_volume(stream, left, right);
Aalique Grahame22e49102018-12-18 14:23:57 -08006044 }
Zhou Song2b8f28f2017-09-11 10:51:38 +08006045 out->volume_l = left;
6046 out->volume_r = right;
Zhou Songbaddf9f2020-11-20 13:57:39 +08006047 pthread_mutex_unlock(&out->latch_lock);
Zhou Song2b8f28f2017-09-11 10:51:38 +08006048 return ret;
Arun Mirpuri5d170872019-03-26 13:21:31 -07006049 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
6050 ALOGV("%s: MMAP set volume called", __func__);
6051 if (!out->standby)
6052 ret = out_set_mmap_volume(stream, left, right);
6053 out->volume_l = left;
6054 out->volume_r = right;
6055 return ret;
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05306056 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_LOW_LATENCY ||
Ramu Gottipati36547092018-12-28 11:32:09 +05306057 out->usecase == USECASE_AUDIO_PLAYBACK_DEEP_BUFFER ||
6058 out->usecase == USECASE_AUDIO_PLAYBACK_ULL) {
Zhou Songbaddf9f2020-11-20 13:57:39 +08006059 pthread_mutex_lock(&out->latch_lock);
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05306060 /* Volume control for pcm playback */
Zhou Songbaddf9f2020-11-20 13:57:39 +08006061 if (!out->standby && !out->a2dp_muted)
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05306062 ret = out_set_pcm_volume(stream, left, right);
6063 else
6064 out->apply_volume = true;
6065
6066 out->volume_l = left;
6067 out->volume_r = right;
Zhou Songbaddf9f2020-11-20 13:57:39 +08006068 pthread_mutex_unlock(&out->latch_lock);
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05306069 return ret;
Derek Chenf13dd492018-11-13 14:53:51 -08006070 } else if (audio_extn_auto_hal_is_bus_device_usecase(out->usecase)) {
6071 ALOGV("%s: bus device set volume called", __func__);
Zhou Songbaddf9f2020-11-20 13:57:39 +08006072 pthread_mutex_lock(&out->latch_lock);
6073 if (!out->standby && !out->a2dp_muted)
Derek Chenf13dd492018-11-13 14:53:51 -08006074 ret = out_set_pcm_volume(stream, left, right);
6075 out->volume_l = left;
6076 out->volume_r = right;
Zhou Songbaddf9f2020-11-20 13:57:39 +08006077 pthread_mutex_unlock(&out->latch_lock);
Derek Chenf13dd492018-11-13 14:53:51 -08006078 return ret;
Eric Laurenta9024de2013-04-04 09:19:12 -07006079 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006080
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006081 return -ENOSYS;
6082}
6083
Zhou Songc9672822017-08-16 16:01:39 +08006084static void update_frames_written(struct stream_out *out, size_t bytes)
6085{
6086 size_t bpf = 0;
6087
6088 if (is_offload_usecase(out->usecase) && !out->non_blocking &&
6089 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD))
6090 bpf = 1;
6091 else if (!is_offload_usecase(out->usecase))
6092 bpf = audio_bytes_per_sample(out->format) *
6093 audio_channel_count_from_out_mask(out->channel_mask);
Zhou Song48453a02018-01-10 17:50:59 +08006094
6095 pthread_mutex_lock(&out->position_query_lock);
6096 if (bpf != 0) {
Zhou Songc9672822017-08-16 16:01:39 +08006097 out->written += bytes / bpf;
Zhou Song48453a02018-01-10 17:50:59 +08006098 clock_gettime(CLOCK_MONOTONIC, &out->writeAt);
6099 }
6100 pthread_mutex_unlock(&out->position_query_lock);
Zhou Songc9672822017-08-16 16:01:39 +08006101}
6102
Vignesh Kulothungana6927272019-02-20 15:17:07 -08006103int split_and_write_audio_haptic_data(struct stream_out *out,
6104 const void *buffer, size_t bytes_to_write)
6105{
6106 struct audio_device *adev = out->dev;
6107
6108 int ret = 0;
6109 size_t channel_count = audio_channel_count_from_out_mask(out->channel_mask);
6110 size_t bytes_per_sample = audio_bytes_per_sample(out->format);
6111 size_t frame_size = channel_count * bytes_per_sample;
6112 size_t frame_count = bytes_to_write / frame_size;
6113
6114 bool force_haptic_path =
6115 property_get_bool("vendor.audio.test_haptic", false);
6116
6117 // extract Haptics data from Audio buffer
6118 bool alloc_haptic_buffer = false;
6119 int haptic_channel_count = adev->haptics_config.channels;
6120 size_t haptic_frame_size = bytes_per_sample * haptic_channel_count;
6121 size_t audio_frame_size = frame_size - haptic_frame_size;
6122 size_t total_haptic_buffer_size = frame_count * haptic_frame_size;
6123
6124 if (adev->haptic_buffer == NULL) {
6125 alloc_haptic_buffer = true;
6126 } else if (adev->haptic_buffer_size < total_haptic_buffer_size) {
6127 free(adev->haptic_buffer);
6128 adev->haptic_buffer_size = 0;
6129 alloc_haptic_buffer = true;
6130 }
6131
6132 if (alloc_haptic_buffer) {
6133 adev->haptic_buffer = (uint8_t *)calloc(1, total_haptic_buffer_size);
Mingshu Pang1513f972019-05-24 12:43:51 +08006134 if(adev->haptic_buffer == NULL) {
6135 ALOGE("%s: failed to allocate mem for dev->haptic_buffer", __func__);
6136 return -ENOMEM;
6137 }
Vignesh Kulothungana6927272019-02-20 15:17:07 -08006138 adev->haptic_buffer_size = total_haptic_buffer_size;
6139 }
6140
6141 size_t src_index = 0, aud_index = 0, hap_index = 0;
6142 uint8_t *audio_buffer = (uint8_t *)buffer;
6143 uint8_t *haptic_buffer = adev->haptic_buffer;
6144
6145 // This is required for testing only. This works for stereo data only.
6146 // One channel is fed to audio stream and other to haptic stream for testing.
6147 if (force_haptic_path)
6148 audio_frame_size = haptic_frame_size = bytes_per_sample;
6149
6150 for (size_t i = 0; i < frame_count; i++) {
6151 memcpy(audio_buffer + aud_index, audio_buffer + src_index,
6152 audio_frame_size);
6153 aud_index += audio_frame_size;
6154 src_index += audio_frame_size;
6155
6156 if (adev->haptic_pcm)
6157 memcpy(haptic_buffer + hap_index, audio_buffer + src_index,
6158 haptic_frame_size);
6159 hap_index += haptic_frame_size;
6160 src_index += haptic_frame_size;
6161
6162 // This is required for testing only.
6163 // Discard haptic channel data.
6164 if (force_haptic_path)
6165 src_index += haptic_frame_size;
6166 }
6167
6168 // write to audio pipeline
6169 ret = pcm_write(out->pcm, (void *)audio_buffer,
6170 frame_count * audio_frame_size);
6171
6172 // write to haptics pipeline
6173 if (adev->haptic_pcm)
6174 ret = pcm_write(adev->haptic_pcm, (void *)adev->haptic_buffer,
6175 frame_count * haptic_frame_size);
6176
6177 return ret;
6178}
6179
Aalique Grahame22e49102018-12-18 14:23:57 -08006180#ifdef NO_AUDIO_OUT
6181static ssize_t out_write_for_no_output(struct audio_stream_out *stream,
6182 const void *buffer __unused, size_t bytes)
6183{
6184 struct stream_out *out = (struct stream_out *)stream;
6185
6186 /* No Output device supported other than BT for playback.
6187 * Sleep for the amount of buffer duration
6188 */
6189 lock_output_stream(out);
6190 usleep(bytes * 1000000 / audio_stream_out_frame_size(
6191 (const struct audio_stream_out *)&out->stream) /
6192 out_get_sample_rate(&out->stream.common));
6193 pthread_mutex_unlock(&out->lock);
6194 return bytes;
6195}
6196#endif
6197
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006198static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
6199 size_t bytes)
6200{
6201 struct stream_out *out = (struct stream_out *)stream;
6202 struct audio_device *adev = out->dev;
Eric Laurent6e895242013-09-05 16:10:57 -07006203 ssize_t ret = 0;
Satish Babu Patakokila715b1422017-08-22 14:33:21 +05306204 int channels = 0;
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006205 const size_t frame_size = audio_stream_out_frame_size(stream);
6206 const size_t frames = (frame_size != 0) ? bytes / frame_size : bytes;
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +05306207 struct audio_usecase *usecase = NULL;
Meng Wang4c32fb42020-01-16 17:57:11 +08006208 uint32_t compr_passthr = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006209
Haynes Mathew George380745d2017-10-04 15:27:45 -07006210 ATRACE_BEGIN("out_write");
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006211 lock_output_stream(out);
Naresh Tanniru4c630392014-05-12 01:05:52 +05306212
Shubhasini Sugumaran3fde6582022-01-19 15:54:50 -05006213 if (CARD_STATUS_OFFLINE == out->card_status ||
6214 POWER_POLICY_STATUS_OFFLINE == adev->out_power_policy) {
Zhou Song0b2e5dc2015-03-16 14:41:38 +08006215
Dhananjay Kumarac341582017-02-23 23:42:25 +05306216 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Ashish Jainbbce4322016-02-16 13:25:27 +05306217 /*during SSR for compress usecase we should return error to flinger*/
Naresh Tanniru80659832014-06-04 18:17:56 +05306218 ALOGD(" copl %s: sound card is not active/SSR state", __func__);
6219 pthread_mutex_unlock(&out->lock);
Haynes Mathew George380745d2017-10-04 15:27:45 -07006220 ATRACE_END();
Naresh Tanniru80659832014-06-04 18:17:56 +05306221 return -ENETRESET;
Ashish Jainbbce4322016-02-16 13:25:27 +05306222 } else {
Ashish Jainbbce4322016-02-16 13:25:27 +05306223 ALOGD(" %s: sound card is not active/SSR state", __func__);
6224 ret= -EIO;
6225 goto exit;
Naresh Tanniru4c630392014-05-12 01:05:52 +05306226 }
6227 }
6228
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05306229 if (audio_extn_passthru_should_drop_data(out)) {
Ashish Jaind84fd6a2016-07-27 12:33:25 +05306230 ALOGV(" %s : Drop data as compress passthrough session is going on", __func__);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05306231 ret = -EIO;
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05306232 goto exit;
6233 }
6234
Haynes Mathew George16081042017-05-31 17:16:49 -07006235 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
6236 ret = -EINVAL;
6237 goto exit;
6238 }
6239
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08006240 if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_AUX_DIGITAL) &&
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05306241 !out->is_iec61937_info_available) {
6242
6243 if (!audio_extn_passthru_is_passthrough_stream(out)) {
6244 out->is_iec61937_info_available = true;
6245 } else if (audio_extn_passthru_is_enabled()) {
6246 audio_extn_passthru_update_stream_configuration(adev, out, buffer, bytes);
Manish Dewangan37864bc2017-06-09 12:28:37 +05306247 out->is_iec61937_info_available = true;
Manish Dewangan671a4202017-08-18 17:30:46 +05306248
6249 if((out->format == AUDIO_FORMAT_DTS) ||
6250 (out->format == AUDIO_FORMAT_DTS_HD)) {
6251 ret = audio_extn_passthru_update_dts_stream_configuration(out,
6252 buffer, bytes);
6253 if (ret) {
6254 if (ret != -ENOSYS) {
6255 out->is_iec61937_info_available = false;
6256 ALOGD("iec61937 transmission info not yet updated retry");
6257 }
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05306258 } else if (!out->standby) {
Manish Dewangan671a4202017-08-18 17:30:46 +05306259 /* if stream has started and after that there is
6260 * stream config change (iec transmission config)
6261 * then trigger select_device to update backend configuration.
6262 */
6263 out->stream_config_changed = true;
6264 pthread_mutex_lock(&adev->lock);
6265 select_devices(adev, out->usecase);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05306266 if (!audio_extn_passthru_is_supported_backend_edid_cfg(adev, out)) {
Weiyin Jiang29c08a42019-04-30 17:11:10 +08006267 pthread_mutex_unlock(&adev->lock);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05306268 ret = -EINVAL;
6269 goto exit;
6270 }
Manish Dewangan671a4202017-08-18 17:30:46 +05306271 pthread_mutex_unlock(&adev->lock);
6272 out->stream_config_changed = false;
6273 out->is_iec61937_info_available = true;
6274 }
6275 }
Satish Babu Patakokila715b1422017-08-22 14:33:21 +05306276
Meng Wang4c32fb42020-01-16 17:57:11 +08006277#ifdef AUDIO_GKI_ENABLED
6278 /* out->compr_config.codec->reserved[0] is for compr_passthr */
6279 compr_passthr = out->compr_config.codec->reserved[0];
6280#else
6281 compr_passthr = out->compr_config.codec->compr_passthr;
6282#endif
6283
Garmond Leung317cbf12017-09-13 16:20:50 -07006284 if ((channels < (int)audio_channel_count_from_out_mask(out->channel_mask)) &&
Meng Wang4c32fb42020-01-16 17:57:11 +08006285 (compr_passthr == PASSTHROUGH) &&
Satish Babu Patakokila715b1422017-08-22 14:33:21 +05306286 (out->is_iec61937_info_available == true)) {
6287 ALOGE("%s: ERROR: Unsupported channel config in passthrough mode", __func__);
6288 ret = -EINVAL;
6289 goto exit;
6290 }
Manish Dewangan37864bc2017-06-09 12:28:37 +05306291 }
6292 }
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05306293
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08006294 if (is_a2dp_out_device_type(&out->device_list) &&
Florian Pfister1a84f312018-07-19 14:38:18 +02006295 (audio_extn_a2dp_source_is_suspended())) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08006296 if (!(compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_SPEAKER) ||
6297 compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_SPEAKER_SAFE))) {
Zhou Songd01e7a22020-09-23 22:49:01 +08006298 if (!is_offload_usecase(out->usecase)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05306299 ret = -EIO;
6300 goto exit;
6301 }
6302 }
6303 }
6304
Weiyin Jiangabedea32020-12-09 12:49:19 +08006305 if (is_usb_out_device_type(&out->device_list) &&
6306 !audio_extn_usb_connected(NULL)) {
6307 ret = -EIO;
6308 goto exit;
6309 }
6310
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006311 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07006312 out->standby = false;
Andy Hungc6bfd4a2019-07-01 18:26:00 -07006313 const int64_t startNs = systemTime(SYSTEM_TIME_MONOTONIC);
6314
Eric Laurent150dbfe2013-02-27 14:31:02 -08006315 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08006316 if (out->usecase == USECASE_COMPRESS_VOIP_CALL)
6317 ret = voice_extn_compress_voip_start_output_stream(out);
6318 else
6319 ret = start_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006320 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006321 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07006322 out->standby = true;
Weiyin Jiangac2bae82020-07-29 17:23:53 +08006323 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006324 goto exit;
6325 }
Ashish Jain1b9b30c2017-05-18 20:57:40 +05306326 out->started = 1;
Andy Hunga1f48fa2019-07-01 18:14:53 -07006327 out->last_fifo_valid = false; // we're coming out of standby, last_fifo isn't valid.
Weiyin Jiangac2bae82020-07-29 17:23:53 +08006328
6329 if ((last_known_cal_step != -1) && (adev->platform != NULL)) {
vivek mehtab72d08d2016-04-29 03:16:47 -07006330 ALOGD("%s: retry previous failed cal level set", __func__);
Weiyin Jiangac2bae82020-07-29 17:23:53 +08006331 platform_send_gain_dep_cal(adev->platform, last_known_cal_step);
Preetam Singh Ranawatf4ae0222017-05-31 17:07:28 +05306332 last_known_cal_step = -1;
vivek mehtab72d08d2016-04-29 03:16:47 -07006333 }
Weiyin Jiangac2bae82020-07-29 17:23:53 +08006334 pthread_mutex_unlock(&adev->lock);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05306335
6336 if ((out->is_iec61937_info_available == true) &&
6337 (audio_extn_passthru_is_passthrough_stream(out))&&
6338 (!audio_extn_passthru_is_supported_backend_edid_cfg(adev, out))) {
6339 ret = -EINVAL;
6340 goto exit;
6341 }
Surendar Karkaf51b5842018-04-26 11:28:38 +05306342 if (out->set_dual_mono)
6343 audio_extn_send_dual_mono_mixing_coefficients(out);
Andy Hungc6bfd4a2019-07-01 18:26:00 -07006344
Dechen Chai22768452021-07-30 09:29:16 +05306345#ifndef LINUX_ENABLED
Andy Hungc6bfd4a2019-07-01 18:26:00 -07006346 // log startup time in ms.
6347 simple_stats_log(
6348 &out->start_latency_ms, (systemTime(SYSTEM_TIME_MONOTONIC) - startNs) * 1e-6);
Dechen Chai22768452021-07-30 09:29:16 +05306349#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006350 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006351
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08006352 if (adev->is_channel_status_set == false &&
6353 compare_device_type(&out->device_list,
6354 AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
Alexy Josephb1379942016-01-29 15:49:38 -08006355 audio_utils_set_hdmi_channel_status(out, (void *)buffer, bytes);
Ashish Jain81eb2a82015-05-13 10:52:34 +05306356 adev->is_channel_status_set = true;
6357 }
6358
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +05306359 if ((adev->use_old_pspd_mix_ctrl == true) &&
6360 (out->pspd_coeff_sent == false)) {
6361 /*
6362 * Need to resend pspd coefficients after stream started for
6363 * older kernel version as it does not save the coefficients
6364 * and also stream has to be started for coeff to apply.
6365 */
6366 usecase = get_usecase_from_list(adev, out->usecase);
6367 if (usecase != NULL) {
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05306368 audio_extn_set_custom_mtmx_params_v2(adev, usecase, true);
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +05306369 out->pspd_coeff_sent = true;
6370 }
6371 }
6372
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07006373 if (is_offload_usecase(out->usecase)) {
Alexy Joseph01e54e62015-03-03 19:01:03 -08006374 ALOGVV("copl(%p): writing buffer (%zu bytes) to compress device", out, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07006375 if (out->send_new_metadata) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07006376 ALOGD("copl(%p):send new gapless metadata", out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07006377 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
6378 out->send_new_metadata = 0;
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05306379 if (out->send_next_track_params && out->is_compr_metadata_avail) {
6380 ALOGD("copl(%p):send next track params in gapless", out);
Weiyin Jiangd9b5a4e2019-07-18 17:24:21 +08006381 compress_set_next_track_param(out->compr, &(out->compr_config.codec->options));
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05306382 out->send_next_track_params = false;
6383 out->is_compr_metadata_avail = false;
6384 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07006385 }
Dhananjay Kumarac341582017-02-23 23:42:25 +05306386 if (!(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
Ashish Jain83a6cc22016-06-28 14:34:17 +05306387 (out->convert_buffer) != NULL) {
Haynes Mathew George352f27b2013-07-26 00:00:15 -07006388
Ashish Jain83a6cc22016-06-28 14:34:17 +05306389 if ((bytes > out->hal_fragment_size)) {
Ashish Jainf1eaa582016-05-23 20:54:24 +05306390 ALOGW("Error written bytes %zu > %d (fragment_size)",
Ashish Jain83a6cc22016-06-28 14:34:17 +05306391 bytes, out->hal_fragment_size);
Ashish Jainf1eaa582016-05-23 20:54:24 +05306392 pthread_mutex_unlock(&out->lock);
Haynes Mathew George380745d2017-10-04 15:27:45 -07006393 ATRACE_END();
Ashish Jainf1eaa582016-05-23 20:54:24 +05306394 return -EINVAL;
6395 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05306396 audio_format_t dst_format = out->hal_op_format;
6397 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05306398
Dieter Luecking5d57def2018-09-07 14:23:37 +02006399 /* prevent division-by-zero */
6400 uint32_t bitwidth_src = format_to_bitwidth_table[src_format];
6401 uint32_t bitwidth_dst = format_to_bitwidth_table[dst_format];
6402 if ((bitwidth_src == 0) || (bitwidth_dst == 0)) {
6403 ALOGE("%s: Error bitwidth == 0", __func__);
Ramu Gottipati02809682018-12-19 16:46:12 +05306404 pthread_mutex_unlock(&out->lock);
Dieter Luecking5d57def2018-09-07 14:23:37 +02006405 ATRACE_END();
6406 return -EINVAL;
6407 }
6408
Ashish Jainf1eaa582016-05-23 20:54:24 +05306409 uint32_t frames = bytes / format_to_bitwidth_table[src_format];
6410 uint32_t bytes_to_write = frames * format_to_bitwidth_table[dst_format];
6411
Ashish Jain83a6cc22016-06-28 14:34:17 +05306412 memcpy_by_audio_format(out->convert_buffer,
Ashish Jainf1eaa582016-05-23 20:54:24 +05306413 dst_format,
6414 buffer,
6415 src_format,
6416 frames);
6417
Ashish Jain83a6cc22016-06-28 14:34:17 +05306418 ret = compress_write(out->compr, out->convert_buffer,
Ashish Jainf1eaa582016-05-23 20:54:24 +05306419 bytes_to_write);
6420
6421 /*Convert written bytes in audio flinger format*/
6422 if (ret > 0)
6423 ret = ((ret * format_to_bitwidth_table[out->format]) /
6424 format_to_bitwidth_table[dst_format]);
6425 }
6426 } else
6427 ret = compress_write(out->compr, buffer, bytes);
6428
Zhou Songc9672822017-08-16 16:01:39 +08006429 if ((ret < 0 || ret == (ssize_t)bytes) && !out->non_blocking)
6430 update_frames_written(out, bytes);
6431
Dhanalakshmi Siddani37ca1d62014-08-20 12:28:34 +05306432 if (ret < 0)
6433 ret = -errno;
Weiyin Jiangcc60dbb2018-08-21 13:12:03 +08006434 ALOGVV("%s: writing buffer (%zu bytes) to compress device returned %d", __func__, bytes, (int)ret);
Ashish Jainb26edfb2016-08-25 00:10:11 +05306435 /*msg to cb thread only if non blocking write is enabled*/
6436 if (ret >= 0 && ret < (ssize_t)bytes && out->non_blocking) {
Sidipotu Ashok55820562014-02-10 16:16:38 +05306437 ALOGD("No space available in compress driver, post msg to cb thread");
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006438 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
Naresh Tanniru80659832014-06-04 18:17:56 +05306439 } else if (-ENETRESET == ret) {
6440 ALOGE("copl %s: received sound card offline state on compress write", __func__);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306441 out->card_status = CARD_STATUS_OFFLINE;
Naresh Tanniru80659832014-06-04 18:17:56 +05306442 pthread_mutex_unlock(&out->lock);
Dhananjay Kumar1248dd82017-07-28 21:22:16 +05306443 out_on_error(&out->stream.common);
Haynes Mathew George380745d2017-10-04 15:27:45 -07006444 ATRACE_END();
Naresh Tanniru80659832014-06-04 18:17:56 +05306445 return ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006446 }
Ashish Jain5106d362016-05-11 19:23:33 +05306447
Dhanalakshmi Siddania6b76c72016-09-09 18:10:31 +05306448 /* Call compr start only when non-zero bytes of data is there to be rendered */
6449 if (!out->playback_started && ret > 0) {
6450 int status = compress_start(out->compr);
6451 if (status < 0) {
6452 ret = status;
6453 ALOGE("%s: compr start failed with err %d", __func__, errno);
6454 goto exit;
6455 }
Alexy Joseph7de344d2015-03-30 10:40:03 -07006456 audio_extn_dts_eagle_fade(adev, true, out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006457 out->playback_started = 1;
Weiyin Jiang280ea742020-09-08 20:28:22 +08006458 pthread_mutex_lock(&out->latch_lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006459 out->offload_state = OFFLOAD_STATE_PLAYING;
Weiyin Jiang280ea742020-09-08 20:28:22 +08006460 pthread_mutex_unlock(&out->latch_lock);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08006461
6462 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
6463 popcount(out->channel_mask),
6464 out->playback_started);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006465 }
6466 pthread_mutex_unlock(&out->lock);
Haynes Mathew George380745d2017-10-04 15:27:45 -07006467 ATRACE_END();
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006468 return ret;
6469 } else {
6470 if (out->pcm) {
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006471 size_t bytes_to_write = bytes;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006472 if (out->muted)
6473 memset((void *)buffer, 0, bytes);
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006474 ALOGV("%s: frames=%zu, frame_size=%zu, bytes_to_write=%zu",
6475 __func__, frames, frame_size, bytes_to_write);
6476
Aalique Grahame22e49102018-12-18 14:23:57 -08006477 if (out->usecase == USECASE_INCALL_MUSIC_UPLINK ||
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07006478 out->usecase == USECASE_INCALL_MUSIC_UPLINK2 ||
6479 (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP &&
6480 !audio_extn_utils_is_vendor_enhanced_fwk())) {
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006481 size_t channel_count = audio_channel_count_from_out_mask(out->channel_mask);
6482 int16_t *src = (int16_t *)buffer;
6483 int16_t *dst = (int16_t *)buffer;
6484
Yunfei Zhanga6cd66d2019-07-22 16:15:36 +08006485 LOG_ALWAYS_FATAL_IF(channel_count > 2 ||
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006486 out->format != AUDIO_FORMAT_PCM_16_BIT,
Aalique Grahame22e49102018-12-18 14:23:57 -08006487 "out_write called for %s use case with wrong properties",
6488 use_case_table[out->usecase]);
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006489
6490 /*
6491 * FIXME: this can be removed once audio flinger mixer supports
6492 * mono output
6493 */
6494
6495 /*
6496 * Code below goes over each frame in the buffer and adds both
6497 * L and R samples and then divides by 2 to convert to mono
6498 */
Yunfei Zhanga6cd66d2019-07-22 16:15:36 +08006499 if (channel_count == 2) {
6500 for (size_t i = 0; i < frames ; i++, dst++, src += 2) {
6501 *dst = (int16_t)(((int32_t)src[0] + (int32_t)src[1]) >> 1);
6502 }
6503 bytes_to_write /= 2;
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006504 }
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006505 }
Andy Hunga1f48fa2019-07-01 18:14:53 -07006506
6507 // Note: since out_get_presentation_position() is called alternating with out_write()
6508 // by AudioFlinger, we can check underruns using the prior timestamp read.
6509 // (Alternately we could check if the buffer is empty using pcm_get_htimestamp().
6510 if (out->last_fifo_valid) {
6511 // compute drain to see if there is an underrun.
6512 const int64_t current_ns = systemTime(SYSTEM_TIME_MONOTONIC); // sys call
Dhananjay Kumara4429632020-07-11 02:53:37 +05306513 int64_t time_diff_ns = current_ns - out->last_fifo_time_ns;
6514 int64_t frames_by_time =
6515 ((time_diff_ns > 0) && (time_diff_ns < (INT64_MAX / out->config.rate))) ?
6516 (time_diff_ns * out->config.rate / NANOS_PER_SECOND) : 0;
Andy Hunga1f48fa2019-07-01 18:14:53 -07006517 const int64_t underrun = frames_by_time - out->last_fifo_frames_remaining;
6518
6519 if (underrun > 0) {
Dechen Chai22768452021-07-30 09:29:16 +05306520#ifndef LINUX_ENABLED
Andy Hunga1f48fa2019-07-01 18:14:53 -07006521 simple_stats_log(&out->fifo_underruns, underrun);
Dechen Chai22768452021-07-30 09:29:16 +05306522#endif
Andy Hunga1f48fa2019-07-01 18:14:53 -07006523
6524 ALOGW("%s: underrun(%lld) "
6525 "frames_by_time(%lld) > out->last_fifo_frames_remaining(%lld)",
6526 __func__,
6527 (long long)out->fifo_underruns.n,
6528 (long long)frames_by_time,
6529 (long long)out->last_fifo_frames_remaining);
6530 }
6531 out->last_fifo_valid = false; // we're writing below, mark fifo info as stale.
6532 }
6533
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05306534 ALOGVV("%s: writing buffer (%zu bytes) to pcm device", __func__, bytes);
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07006535
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006536 long ns = 0;
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07006537
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006538 if (out->config.rate)
6539 ns = pcm_bytes_to_frames(out->pcm, bytes)*1000000000LL/
6540 out->config.rate;
6541
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006542 request_out_focus(out, ns);
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006543 bool use_mmap = is_mmap_usecase(out->usecase) || out->realtime;
6544
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006545 if (use_mmap)
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006546 ret = pcm_mmap_write(out->pcm, (void *)buffer, bytes_to_write);
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006547 else if (out->hal_op_format != out->hal_ip_format &&
Ashish Jain83a6cc22016-06-28 14:34:17 +05306548 out->convert_buffer != NULL) {
6549
6550 memcpy_by_audio_format(out->convert_buffer,
6551 out->hal_op_format,
6552 buffer,
6553 out->hal_ip_format,
6554 out->config.period_size * out->config.channels);
6555
6556 ret = pcm_write(out->pcm, out->convert_buffer,
6557 (out->config.period_size *
6558 out->config.channels *
6559 format_to_bitwidth_table[out->hal_op_format]));
6560 } else {
Aditya Bavanarid4db8ee2017-05-29 21:08:03 +05306561 /*
6562 * To avoid underrun in DSP when the application is not pumping
6563 * data at required rate, check for the no. of bytes and ignore
6564 * pcm_write if it is less than actual buffer size.
6565 * It is a work around to a change in compress VOIP driver.
6566 */
6567 if ((out->flags & AUDIO_OUTPUT_FLAG_VOIP_RX) &&
6568 bytes < (out->config.period_size * out->config.channels *
6569 audio_bytes_per_sample(out->format))) {
6570 size_t voip_buf_size =
6571 out->config.period_size * out->config.channels *
6572 audio_bytes_per_sample(out->format);
6573 ALOGE("%s:VOIP underrun: bytes received %zu, required:%zu\n",
6574 __func__, bytes, voip_buf_size);
6575 usleep(((uint64_t)voip_buf_size - bytes) *
6576 1000000 / audio_stream_out_frame_size(stream) /
6577 out_get_sample_rate(&out->stream.common));
6578 ret = 0;
Vignesh Kulothungana6927272019-02-20 15:17:07 -08006579 } else {
6580 if (out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS)
6581 ret = split_and_write_audio_haptic_data(out, buffer, bytes);
6582 else
6583 ret = pcm_write(out->pcm, (void *)buffer, bytes_to_write);
6584 }
Ashish Jain83a6cc22016-06-28 14:34:17 +05306585 }
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07006586
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006587 release_out_focus(out);
6588
Dhanalakshmi Siddani37ca1d62014-08-20 12:28:34 +05306589 if (ret < 0)
6590 ret = -errno;
Zhou Songc9672822017-08-16 16:01:39 +08006591 else if (ret > 0)
Ashish Jain83a6cc22016-06-28 14:34:17 +05306592 ret = -EINVAL;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006593 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006594 }
6595
6596exit:
Zhou Songc9672822017-08-16 16:01:39 +08006597 update_frames_written(out, bytes);
Naresh Tanniru4c630392014-05-12 01:05:52 +05306598 if (-ENETRESET == ret) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306599 out->card_status = CARD_STATUS_OFFLINE;
Naresh Tanniru4c630392014-05-12 01:05:52 +05306600 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006601 pthread_mutex_unlock(&out->lock);
6602
6603 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07006604 if (out->pcm)
Alexy Josephb1379942016-01-29 15:49:38 -08006605 ALOGE("%s: error %d, %s", __func__, (int)ret, pcm_get_error(out->pcm));
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05306606 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05306607 pthread_mutex_lock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05306608 voice_extn_compress_voip_close_output_stream(&out->stream.common);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05306609 out->started = 0;
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05306610 pthread_mutex_unlock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05306611 out->standby = true;
6612 }
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306613 out_on_error(&out->stream.common);
Dieter Luecking5d57def2018-09-07 14:23:37 +02006614 if (!(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
6615 /* prevent division-by-zero */
6616 uint32_t stream_size = audio_stream_out_frame_size(stream);
6617 uint32_t srate = out_get_sample_rate(&out->stream.common);
Vidyakumar Athotaa9d3a5f2017-08-09 12:13:05 -07006618
Dieter Luecking5d57def2018-09-07 14:23:37 +02006619 if ((stream_size == 0) || (srate == 0)) {
6620 ALOGE("%s: stream_size= %d, srate = %d", __func__, stream_size, srate);
6621 ATRACE_END();
6622 return -EINVAL;
6623 }
6624 usleep((uint64_t)bytes * 1000000 / stream_size / srate);
6625 }
Vidyakumar Athotaa9d3a5f2017-08-09 12:13:05 -07006626 if (audio_extn_passthru_is_passthrough_stream(out)) {
Rajshekar Eashwarappa88834522018-04-02 17:20:15 +05306627 //ALOGE("%s: write error, ret = %zd", __func__, ret);
Haynes Mathew George380745d2017-10-04 15:27:45 -07006628 ATRACE_END();
Vidyakumar Athotaa9d3a5f2017-08-09 12:13:05 -07006629 return ret;
6630 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006631 }
Haynes Mathew George380745d2017-10-04 15:27:45 -07006632 ATRACE_END();
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006633 return bytes;
6634}
6635
6636static int out_get_render_position(const struct audio_stream_out *stream,
6637 uint32_t *dsp_frames)
6638{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006639 struct stream_out *out = (struct stream_out *)stream;
Zhou Song32a556e2015-05-05 10:46:56 +08006640
6641 if (dsp_frames == NULL)
6642 return -EINVAL;
6643
6644 *dsp_frames = 0;
6645 if (is_offload_usecase(out->usecase)) {
Mingming Yin9e348b52014-11-19 16:18:55 -08006646 ssize_t ret = 0;
Ashish Jain5106d362016-05-11 19:23:33 +05306647
6648 /* Below piece of code is not guarded against any lock beacuse audioFliner serializes
6649 * this operation and adev_close_output_stream(where out gets reset).
6650 */
Dhananjay Kumarac341582017-02-23 23:42:25 +05306651 if (!out->non_blocking && !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Zhou Song48453a02018-01-10 17:50:59 +08006652 *dsp_frames = get_actual_pcm_frames_rendered(out, NULL);
Ashish Jain5106d362016-05-11 19:23:33 +05306653 ALOGVV("dsp_frames %d sampleRate %d",(int)*dsp_frames,out->sample_rate);
Haynes Mathew Georgeb0f5dc32017-10-06 18:35:12 -07006654 adjust_frames_for_device_delay(out, dsp_frames);
Ashish Jain5106d362016-05-11 19:23:33 +05306655 return 0;
6656 }
6657
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006658 lock_output_stream(out);
Ashish Jain5106d362016-05-11 19:23:33 +05306659 if (out->compr != NULL && out->non_blocking) {
Naresh Tanniru80659832014-06-04 18:17:56 +05306660 ret = compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006661 &out->sample_rate);
Dhanalakshmi Siddani37ca1d62014-08-20 12:28:34 +05306662 if (ret < 0)
6663 ret = -errno;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006664 ALOGVV("%s rendered frames %d sample_rate %d",
Ashish Jain5106d362016-05-11 19:23:33 +05306665 __func__, *dsp_frames, out->sample_rate);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006666 }
Naresh Tanniru80659832014-06-04 18:17:56 +05306667 if (-ENETRESET == ret) {
6668 ALOGE(" ERROR: sound card not active Unable to get time stamp from compress driver");
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306669 out->card_status = CARD_STATUS_OFFLINE;
6670 ret = -EINVAL;
Naresh Tanniru80659832014-06-04 18:17:56 +05306671 } else if(ret < 0) {
6672 ALOGE(" ERROR: Unable to get time stamp from compress driver");
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306673 ret = -EINVAL;
Shubhasini Sugumaran3fde6582022-01-19 15:54:50 -05006674 } else if (out->card_status == CARD_STATUS_OFFLINE ||
6675 adev->out_power_policy == POWER_POLICY_STATUS_OFFLINE) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05306676 /*
6677 * Handle corner case where compress session is closed during SSR
6678 * and timestamp is queried
6679 */
6680 ALOGE(" ERROR: sound card not active, return error");
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306681 ret = -EINVAL;
Manisha Agarwal7b3e3772019-02-20 14:33:45 +05306682 } else if (out->prev_card_status_offline) {
6683 ALOGE("ERROR: previously sound card was offline,return error");
6684 ret = -EINVAL;
Naresh Tanniru80659832014-06-04 18:17:56 +05306685 } else {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306686 ret = 0;
Haynes Mathew Georgeb0f5dc32017-10-06 18:35:12 -07006687 adjust_frames_for_device_delay(out, dsp_frames);
Naresh Tanniru80659832014-06-04 18:17:56 +05306688 }
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306689 pthread_mutex_unlock(&out->lock);
6690 return ret;
Zhou Song32a556e2015-05-05 10:46:56 +08006691 } else if (audio_is_linear_pcm(out->format)) {
6692 *dsp_frames = out->written;
Haynes Mathew Georgeb0f5dc32017-10-06 18:35:12 -07006693 adjust_frames_for_device_delay(out, dsp_frames);
Zhou Song32a556e2015-05-05 10:46:56 +08006694 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006695 } else
6696 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006697}
6698
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07006699static int out_add_audio_effect(const struct audio_stream *stream __unused,
6700 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006701{
6702 return 0;
6703}
6704
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07006705static int out_remove_audio_effect(const struct audio_stream *stream __unused,
6706 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006707{
6708 return 0;
6709}
6710
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07006711static int out_get_next_write_timestamp(const struct audio_stream_out *stream __unused,
6712 int64_t *timestamp __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006713{
Satya Krishna Pindiprolib6655542017-07-03 19:38:19 +05306714 return -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006715}
6716
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07006717static int out_get_presentation_position(const struct audio_stream_out *stream,
6718 uint64_t *frames, struct timespec *timestamp)
6719{
6720 struct stream_out *out = (struct stream_out *)stream;
pavance65c2fe2017-10-18 17:52:01 +05306721 int ret = -ENODATA;
Eric Laurent949a0892013-09-20 09:20:13 -07006722 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07006723
Ashish Jain5106d362016-05-11 19:23:33 +05306724 /* below piece of code is not guarded against any lock because audioFliner serializes
6725 * this operation and adev_close_output_stream( where out gets reset).
6726 */
6727 if (is_offload_usecase(out->usecase) && !out->non_blocking &&
Dhananjay Kumarac341582017-02-23 23:42:25 +05306728 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Zhou Song48453a02018-01-10 17:50:59 +08006729 *frames = get_actual_pcm_frames_rendered(out, timestamp);
Ashish Jain5106d362016-05-11 19:23:33 +05306730 ALOGVV("frames %lld playedat %lld",(long long int)*frames,
6731 timestamp->tv_sec * 1000000LL + timestamp->tv_nsec / 1000);
6732 return 0;
6733 }
6734
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006735 lock_output_stream(out);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07006736
Ashish Jain5106d362016-05-11 19:23:33 +05306737 if (is_offload_usecase(out->usecase) && out->compr != NULL && out->non_blocking) {
6738 ret = compress_get_tstamp(out->compr, &dsp_frames,
6739 &out->sample_rate);
yidongh0515e042017-07-06 15:00:34 +08006740 // Adjustment accounts for A2dp encoder latency with offload usecases
6741 // Note: Encoder latency is returned in ms.
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08006742 if (is_a2dp_out_device_type(&out->device_list)) {
yidongh0515e042017-07-06 15:00:34 +08006743 unsigned long offset =
6744 (audio_extn_a2dp_get_encoder_latency() * out->sample_rate / 1000);
6745 dsp_frames = (dsp_frames > offset) ? (dsp_frames - offset) : 0;
6746 }
Ashish Jain5106d362016-05-11 19:23:33 +05306747 ALOGVV("%s rendered frames %ld sample_rate %d",
6748 __func__, dsp_frames, out->sample_rate);
6749 *frames = dsp_frames;
6750 if (ret < 0)
6751 ret = -errno;
6752 if (-ENETRESET == ret) {
6753 ALOGE(" ERROR: sound card not active Unable to get time stamp from compress driver");
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306754 out->card_status = CARD_STATUS_OFFLINE;
Ashish Jain5106d362016-05-11 19:23:33 +05306755 ret = -EINVAL;
6756 } else
6757 ret = 0;
6758 /* this is the best we can do */
6759 clock_gettime(CLOCK_MONOTONIC, timestamp);
Eric Laurent949a0892013-09-20 09:20:13 -07006760 } else {
6761 if (out->pcm) {
Weiyin Jiangd4633762018-03-16 12:05:03 +08006762 unsigned int avail;
6763 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
George Gao62ebc722019-07-29 16:29:44 -07006764 uint64_t signed_frames = 0;
Weiyin Jiang0d25bcf2019-08-30 15:27:23 +08006765 uint64_t frames_temp = 0;
George Gao62ebc722019-07-29 16:29:44 -07006766
Andy Hunga1f48fa2019-07-01 18:14:53 -07006767 if (out->kernel_buffer_size > avail) {
6768 frames_temp = out->last_fifo_frames_remaining = out->kernel_buffer_size - avail;
6769 } else {
6770 ALOGW("%s: avail:%u > kernel_buffer_size:%zu clamping!",
6771 __func__, avail, out->kernel_buffer_size);
6772 avail = out->kernel_buffer_size;
6773 frames_temp = out->last_fifo_frames_remaining = 0;
6774 }
6775 out->last_fifo_valid = true;
6776 out->last_fifo_time_ns = audio_utils_ns_from_timespec(timestamp);
6777
Weiyin Jiang0d25bcf2019-08-30 15:27:23 +08006778 if (out->written >= frames_temp)
6779 signed_frames = out->written - frames_temp;
George Gao62ebc722019-07-29 16:29:44 -07006780
Andy Hunga1f48fa2019-07-01 18:14:53 -07006781 ALOGVV("%s: frames:%lld avail:%u kernel_buffer_size:%zu",
6782 __func__, (long long)signed_frames, avail, out->kernel_buffer_size);
6783
Weiyin Jiangd4633762018-03-16 12:05:03 +08006784 // This adjustment accounts for buffering after app processor.
6785 // It is based on estimated DSP latency per use case, rather than exact.
George Gao9ba8a142020-07-23 14:30:03 -07006786 frames_temp = platform_render_latency(out) *
Robert Lee58215542019-07-15 20:55:12 +08006787 out->sample_rate / 1000000LL;
Weiyin Jiang0d25bcf2019-08-30 15:27:23 +08006788 if (signed_frames >= frames_temp)
6789 signed_frames -= frames_temp;
Aniket Kumar Lataff613152017-07-18 18:19:21 -07006790
Weiyin Jiangd4633762018-03-16 12:05:03 +08006791 // Adjustment accounts for A2dp encoder latency with non offload usecases
6792 // Note: Encoder latency is returned in ms, while platform_render_latency in us.
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08006793 if (is_a2dp_out_device_type(&out->device_list)) {
Weiyin Jiang0d25bcf2019-08-30 15:27:23 +08006794 frames_temp = audio_extn_a2dp_get_encoder_latency() * out->sample_rate / 1000;
6795 if (signed_frames >= frames_temp)
6796 signed_frames -= frames_temp;
Weiyin Jiangd4633762018-03-16 12:05:03 +08006797 }
6798
6799 // It would be unusual for this value to be negative, but check just in case ...
George Gao62ebc722019-07-29 16:29:44 -07006800 *frames = signed_frames;
6801 ret = 0;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07006802 }
Eric Laurenta7a33042019-07-10 16:20:22 -07006803 } else if (out->card_status == CARD_STATUS_OFFLINE ||
Shubhasini Sugumaran3fde6582022-01-19 15:54:50 -05006804 adev->out_power_policy == POWER_POLICY_STATUS_OFFLINE ||
Eric Laurenta7a33042019-07-10 16:20:22 -07006805 // audioflinger still needs position updates when A2DP is suspended
Jasmine Cha5c2517f2019-09-09 11:07:28 +08006806 (is_a2dp_out_device_type(&out->device_list) && audio_extn_a2dp_source_is_suspended())) {
Ashish Jainbbce4322016-02-16 13:25:27 +05306807 *frames = out->written;
6808 clock_gettime(CLOCK_MONOTONIC, timestamp);
Manisha Agarwal7b3e3772019-02-20 14:33:45 +05306809 if (is_offload_usecase(out->usecase))
6810 ret = -EINVAL;
6811 else
6812 ret = 0;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07006813 }
6814 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07006815 pthread_mutex_unlock(&out->lock);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07006816 return ret;
6817}
6818
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006819static int out_set_callback(struct audio_stream_out *stream,
6820 stream_callback_t callback, void *cookie)
6821{
6822 struct stream_out *out = (struct stream_out *)stream;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08006823 int ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006824
6825 ALOGV("%s", __func__);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006826 lock_output_stream(out);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08006827 out->client_callback = callback;
6828 out->client_cookie = cookie;
6829 if (out->adsp_hdlr_stream_handle) {
6830 ret = audio_extn_adsp_hdlr_stream_set_callback(
6831 out->adsp_hdlr_stream_handle,
6832 callback,
6833 cookie);
6834 if (ret)
6835 ALOGW("%s:adsp hdlr callback registration failed %d",
6836 __func__, ret);
6837 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006838 pthread_mutex_unlock(&out->lock);
6839 return 0;
6840}
6841
6842static int out_pause(struct audio_stream_out* stream)
6843{
6844 struct stream_out *out = (struct stream_out *)stream;
6845 int status = -ENOSYS;
6846 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07006847 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07006848 ALOGD("copl(%p):pause compress driver", out);
Sujin Panicker7c7b6f92020-04-03 12:57:55 +05306849 status = -ENODATA;
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006850 lock_output_stream(out);
Weiyin Jiang280ea742020-09-08 20:28:22 +08006851 pthread_mutex_lock(&out->latch_lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006852 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306853 if (out->card_status != CARD_STATUS_OFFLINE)
Naresh Tanniru80659832014-06-04 18:17:56 +05306854 status = compress_pause(out->compr);
6855
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006856 out->offload_state = OFFLOAD_STATE_PAUSED;
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08006857
Mingming Yin21854652016-04-13 11:54:02 -07006858 if (audio_extn_passthru_is_active()) {
6859 ALOGV("offload use case, pause passthru");
6860 audio_extn_passthru_on_pause(out);
6861 }
6862
Dhanalakshmi Siddani79415e72015-03-23 11:54:47 +05306863 audio_extn_dts_eagle_fade(adev, false, out);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08006864 audio_extn_dts_notify_playback_state(out->usecase, 0,
6865 out->sample_rate, popcount(out->channel_mask),
6866 0);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006867 }
Weiyin Jiang280ea742020-09-08 20:28:22 +08006868 pthread_mutex_unlock(&out->latch_lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006869 pthread_mutex_unlock(&out->lock);
6870 }
6871 return status;
6872}
6873
6874static int out_resume(struct audio_stream_out* stream)
6875{
6876 struct stream_out *out = (struct stream_out *)stream;
6877 int status = -ENOSYS;
6878 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07006879 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07006880 ALOGD("copl(%p):resume compress driver", out);
Sujin Panicker7c7b6f92020-04-03 12:57:55 +05306881 status = -ENODATA;
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006882 lock_output_stream(out);
Weiyin Jiang280ea742020-09-08 20:28:22 +08006883 pthread_mutex_lock(&out->latch_lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006884 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306885 if (out->card_status != CARD_STATUS_OFFLINE) {
Naresh Tanniru80659832014-06-04 18:17:56 +05306886 status = compress_resume(out->compr);
Mingming Yin21854652016-04-13 11:54:02 -07006887 }
6888 if (!status) {
6889 out->offload_state = OFFLOAD_STATE_PLAYING;
6890 }
Dhanalakshmi Siddani79415e72015-03-23 11:54:47 +05306891 audio_extn_dts_eagle_fade(adev, true, out);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08006892 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
6893 popcount(out->channel_mask), 1);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006894 }
Weiyin Jiang280ea742020-09-08 20:28:22 +08006895 pthread_mutex_unlock(&out->latch_lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006896 pthread_mutex_unlock(&out->lock);
6897 }
6898 return status;
6899}
6900
6901static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
6902{
6903 struct stream_out *out = (struct stream_out *)stream;
6904 int status = -ENOSYS;
6905 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07006906 if (is_offload_usecase(out->usecase)) {
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006907 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006908 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
6909 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
6910 else
6911 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
6912 pthread_mutex_unlock(&out->lock);
6913 }
6914 return status;
6915}
6916
6917static int out_flush(struct audio_stream_out* stream)
6918{
6919 struct stream_out *out = (struct stream_out *)stream;
6920 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07006921 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07006922 ALOGD("copl(%p):calling compress flush", out);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006923 lock_output_stream(out);
Weiyin Jiang280ea742020-09-08 20:28:22 +08006924 pthread_mutex_lock(&out->latch_lock);
Haynes Mathew Georgeafe54d82016-09-21 14:39:19 -07006925 if (out->offload_state == OFFLOAD_STATE_PAUSED) {
Gautam Manam14c198b2020-12-24 14:08:04 +05306926 pthread_mutex_unlock(&out->latch_lock);
Haynes Mathew Georgeafe54d82016-09-21 14:39:19 -07006927 stop_compressed_output_l(out);
Haynes Mathew Georgeafe54d82016-09-21 14:39:19 -07006928 } else {
6929 ALOGW("%s called in invalid state %d", __func__, out->offload_state);
Gautam Manam14c198b2020-12-24 14:08:04 +05306930 pthread_mutex_unlock(&out->latch_lock);
Haynes Mathew Georgeafe54d82016-09-21 14:39:19 -07006931 }
Weiyin Jiang547e4152017-09-14 17:24:18 +08006932 out->written = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006933 pthread_mutex_unlock(&out->lock);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07006934 ALOGD("copl(%p):out of compress flush", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006935 return 0;
6936 }
6937 return -ENOSYS;
6938}
6939
Haynes Mathew George16081042017-05-31 17:16:49 -07006940static int out_stop(const struct audio_stream_out* stream)
6941{
6942 struct stream_out *out = (struct stream_out *)stream;
6943 struct audio_device *adev = out->dev;
6944 int ret = -ENOSYS;
6945
6946 ALOGV("%s", __func__);
6947 pthread_mutex_lock(&adev->lock);
6948 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP && !out->standby &&
6949 out->playback_started && out->pcm != NULL) {
6950 pcm_stop(out->pcm);
6951 ret = stop_output_stream(out);
6952 out->playback_started = false;
6953 }
6954 pthread_mutex_unlock(&adev->lock);
6955 return ret;
6956}
6957
6958static int out_start(const struct audio_stream_out* stream)
6959{
6960 struct stream_out *out = (struct stream_out *)stream;
6961 struct audio_device *adev = out->dev;
6962 int ret = -ENOSYS;
6963
6964 ALOGV("%s", __func__);
6965 pthread_mutex_lock(&adev->lock);
6966 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP && !out->standby &&
6967 !out->playback_started && out->pcm != NULL) {
6968 ret = start_output_stream(out);
6969 if (ret == 0) {
6970 out->playback_started = true;
6971 }
6972 }
6973 pthread_mutex_unlock(&adev->lock);
6974 return ret;
6975}
6976
6977/*
6978 * Modify config->period_count based on min_size_frames
6979 */
6980static void adjust_mmap_period_count(struct pcm_config *config, int32_t min_size_frames)
6981{
6982 int periodCountRequested = (min_size_frames + config->period_size - 1)
6983 / config->period_size;
6984 int periodCount = MMAP_PERIOD_COUNT_MIN;
6985
6986 ALOGV("%s original config.period_size = %d config.period_count = %d",
6987 __func__, config->period_size, config->period_count);
6988
6989 while (periodCount < periodCountRequested && (periodCount * 2) < MMAP_PERIOD_COUNT_MAX) {
6990 periodCount *= 2;
6991 }
6992 config->period_count = periodCount;
6993
6994 ALOGV("%s requested config.period_count = %d", __func__, config->period_count);
6995}
6996
Phil Burkfe17efd2019-03-25 10:23:35 -07006997// Read offset for the positional timestamp from a persistent vendor property.
6998// This is to workaround apparent inaccuracies in the timing information that
6999// is used by the AAudio timing model. The inaccuracies can cause glitches.
7000static int64_t get_mmap_out_time_offset() {
7001 const int32_t kDefaultOffsetMicros = 0;
7002 int32_t mmap_time_offset_micros = property_get_int32(
Weiyin Jiangbd68b4d2019-05-17 16:29:50 +08007003 "persist.vendor.audio.out_mmap_delay_micros", kDefaultOffsetMicros);
Phil Burkfe17efd2019-03-25 10:23:35 -07007004 ALOGI("mmap_time_offset_micros = %d for output", mmap_time_offset_micros);
7005 return mmap_time_offset_micros * (int64_t)1000;
7006}
7007
Haynes Mathew George16081042017-05-31 17:16:49 -07007008static int out_create_mmap_buffer(const struct audio_stream_out *stream,
7009 int32_t min_size_frames,
7010 struct audio_mmap_buffer_info *info)
7011{
7012 struct stream_out *out = (struct stream_out *)stream;
7013 struct audio_device *adev = out->dev;
7014 int ret = 0;
Aalique Grahame1f123102017-10-12 10:38:32 -07007015 unsigned int offset1 = 0;
7016 unsigned int frames1 = 0;
Haynes Mathew George16081042017-05-31 17:16:49 -07007017 const char *step = "";
Haynes Mathew Georgeef514882017-05-01 17:46:23 -07007018 uint32_t mmap_size;
Arun Mirpuri5d170872019-03-26 13:21:31 -07007019 uint32_t buffer_size;
Haynes Mathew George16081042017-05-31 17:16:49 -07007020
Arun Mirpuri5d170872019-03-26 13:21:31 -07007021 ALOGD("%s", __func__);
Sharad Sanglec6f32552018-05-04 16:15:38 +05307022 lock_output_stream(out);
Haynes Mathew George16081042017-05-31 17:16:49 -07007023 pthread_mutex_lock(&adev->lock);
7024
Sharad Sanglec6f32552018-05-04 16:15:38 +05307025 if (CARD_STATUS_OFFLINE == out->card_status ||
Shubhasini Sugumaran3fde6582022-01-19 15:54:50 -05007026 CARD_STATUS_OFFLINE == adev->card_status ||
7027 POWER_POLICY_STATUS_OFFLINE == adev->out_power_policy) {
Sharad Sanglec6f32552018-05-04 16:15:38 +05307028 ALOGW("out->card_status or adev->card_status offline, try again");
7029 ret = -EIO;
7030 goto exit;
7031 }
Sujin Panicker7c7b6f92020-04-03 12:57:55 +05307032 if (info == NULL || !(min_size_frames > 0 && min_size_frames < INT32_MAX)) {
Haynes Mathew George16081042017-05-31 17:16:49 -07007033 ALOGE("%s: info = %p, min_size_frames = %d", __func__, info, min_size_frames);
7034 ret = -EINVAL;
7035 goto exit;
7036 }
7037 if (out->usecase != USECASE_AUDIO_PLAYBACK_MMAP || !out->standby) {
7038 ALOGE("%s: usecase = %d, standby = %d", __func__, out->usecase, out->standby);
7039 ret = -ENOSYS;
7040 goto exit;
7041 }
7042 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
7043 if (out->pcm_device_id < 0) {
7044 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
7045 __func__, out->pcm_device_id, out->usecase);
7046 ret = -EINVAL;
7047 goto exit;
7048 }
7049
7050 adjust_mmap_period_count(&out->config, min_size_frames);
7051
Arun Mirpuri5d170872019-03-26 13:21:31 -07007052 ALOGD("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
Haynes Mathew George16081042017-05-31 17:16:49 -07007053 __func__, adev->snd_card, out->pcm_device_id, out->config.channels);
7054 out->pcm = pcm_open(adev->snd_card, out->pcm_device_id,
7055 (PCM_OUT | PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC), &out->config);
Satish Babu Patakokila54ce83d2018-07-06 18:00:37 +05307056 if (errno == ENETRESET && !pcm_is_ready(out->pcm)) {
Sharad Sanglec6f32552018-05-04 16:15:38 +05307057 ALOGE("%s: pcm_open failed errno:%d\n", __func__, errno);
7058 out->card_status = CARD_STATUS_OFFLINE;
7059 adev->card_status = CARD_STATUS_OFFLINE;
7060 ret = -EIO;
7061 goto exit;
7062 }
7063
Haynes Mathew George16081042017-05-31 17:16:49 -07007064 if (out->pcm == NULL || !pcm_is_ready(out->pcm)) {
7065 step = "open";
7066 ret = -ENODEV;
7067 goto exit;
7068 }
7069 ret = pcm_mmap_begin(out->pcm, &info->shared_memory_address, &offset1, &frames1);
7070 if (ret < 0) {
7071 step = "begin";
7072 goto exit;
7073 }
juyuchen626833d2019-06-04 16:48:02 +08007074
7075 info->flags = 0;
Haynes Mathew George16081042017-05-31 17:16:49 -07007076 info->buffer_size_frames = pcm_get_buffer_size(out->pcm);
Arun Mirpuri5d170872019-03-26 13:21:31 -07007077 buffer_size = pcm_frames_to_bytes(out->pcm, info->buffer_size_frames);
Haynes Mathew George16081042017-05-31 17:16:49 -07007078 info->burst_size_frames = out->config.period_size;
Haynes Mathew Georgeef514882017-05-01 17:46:23 -07007079 ret = platform_get_mmap_data_fd(adev->platform,
7080 out->pcm_device_id, 0 /*playback*/,
7081 &info->shared_memory_fd,
7082 &mmap_size);
7083 if (ret < 0) {
Arun Mirpuri5d170872019-03-26 13:21:31 -07007084 // Fall back to non exclusive mode
7085 info->shared_memory_fd = pcm_get_poll_fd(out->pcm);
7086 } else {
Phil Burkd898ba62019-06-20 12:49:01 -07007087 out->mmap_shared_memory_fd = info->shared_memory_fd; // for closing later
7088 ALOGV("%s: opened mmap_shared_memory_fd = %d", __func__, out->mmap_shared_memory_fd);
7089
Arun Mirpuri5d170872019-03-26 13:21:31 -07007090 if (mmap_size < buffer_size) {
7091 step = "mmap";
7092 goto exit;
7093 }
juyuchen626833d2019-06-04 16:48:02 +08007094 info->flags |= AUDIO_MMAP_APPLICATION_SHAREABLE;
Haynes Mathew Georgeef514882017-05-01 17:46:23 -07007095 }
Haynes Mathew George16081042017-05-31 17:16:49 -07007096 memset(info->shared_memory_address, 0, pcm_frames_to_bytes(out->pcm,
Haynes Mathew Georgeef514882017-05-01 17:46:23 -07007097 info->buffer_size_frames));
Haynes Mathew George16081042017-05-31 17:16:49 -07007098
7099 ret = pcm_mmap_commit(out->pcm, 0, MMAP_PERIOD_SIZE);
7100 if (ret < 0) {
7101 step = "commit";
7102 goto exit;
7103 }
7104
Phil Burkfe17efd2019-03-25 10:23:35 -07007105 out->mmap_time_offset_nanos = get_mmap_out_time_offset();
7106
Haynes Mathew George16081042017-05-31 17:16:49 -07007107 out->standby = false;
7108 ret = 0;
7109
Arun Mirpuri5d170872019-03-26 13:21:31 -07007110 ALOGD("%s: got mmap buffer address %p info->buffer_size_frames %d",
Haynes Mathew George16081042017-05-31 17:16:49 -07007111 __func__, info->shared_memory_address, info->buffer_size_frames);
7112
7113exit:
7114 if (ret != 0) {
7115 if (out->pcm == NULL) {
7116 ALOGE("%s: %s - %d", __func__, step, ret);
7117 } else {
7118 ALOGE("%s: %s %s", __func__, step, pcm_get_error(out->pcm));
7119 pcm_close(out->pcm);
7120 out->pcm = NULL;
7121 }
7122 }
7123 pthread_mutex_unlock(&adev->lock);
Sharad Sanglec6f32552018-05-04 16:15:38 +05307124 pthread_mutex_unlock(&out->lock);
Haynes Mathew George16081042017-05-31 17:16:49 -07007125 return ret;
7126}
7127
7128static int out_get_mmap_position(const struct audio_stream_out *stream,
7129 struct audio_mmap_position *position)
7130{
7131 struct stream_out *out = (struct stream_out *)stream;
7132 ALOGVV("%s", __func__);
7133 if (position == NULL) {
7134 return -EINVAL;
7135 }
7136 if (out->usecase != USECASE_AUDIO_PLAYBACK_MMAP) {
Haynes Mathew George4ab3ba92017-12-11 14:49:43 -08007137 ALOGE("%s: called on %s", __func__, use_case_table[out->usecase]);
Haynes Mathew George16081042017-05-31 17:16:49 -07007138 return -ENOSYS;
7139 }
7140 if (out->pcm == NULL) {
7141 return -ENOSYS;
7142 }
7143
7144 struct timespec ts = { 0, 0 };
7145 int ret = pcm_mmap_get_hw_ptr(out->pcm, (unsigned int *)&position->position_frames, &ts);
7146 if (ret < 0) {
7147 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
7148 return ret;
7149 }
Phil Burkfe17efd2019-03-25 10:23:35 -07007150 position->time_nanoseconds = ts.tv_sec*1000000000LL + ts.tv_nsec
7151 + out->mmap_time_offset_nanos;
Haynes Mathew George16081042017-05-31 17:16:49 -07007152 return 0;
7153}
7154
7155
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007156/** audio_stream_in implementation **/
7157static uint32_t in_get_sample_rate(const struct audio_stream *stream)
7158{
7159 struct stream_in *in = (struct stream_in *)stream;
7160
7161 return in->config.rate;
7162}
7163
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07007164static int in_set_sample_rate(struct audio_stream *stream __unused,
7165 uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007166{
7167 return -ENOSYS;
7168}
7169
7170static size_t in_get_buffer_size(const struct audio_stream *stream)
7171{
7172 struct stream_in *in = (struct stream_in *)stream;
7173
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007174 if(in->usecase == USECASE_COMPRESS_VOIP_CALL)
7175 return voice_extn_compress_voip_in_get_buffer_size(in);
Mingming Yine62d7842013-10-25 16:26:03 -07007176 else if(audio_extn_compr_cap_usecase_supported(in->usecase))
7177 return audio_extn_compr_cap_get_buffer_size(in->config.format);
Dhananjay Kumar7dbe3562019-08-30 05:49:33 +05307178 else if(audio_extn_cin_attached_usecase(in))
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307179 return audio_extn_cin_get_buffer_size(in);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007180
Haynes Mathew George5beddd42016-06-27 18:33:40 -07007181 return in->config.period_size * in->af_period_multiplier *
7182 audio_stream_in_frame_size((const struct audio_stream_in *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007183}
7184
7185static uint32_t in_get_channels(const struct audio_stream *stream)
7186{
7187 struct stream_in *in = (struct stream_in *)stream;
7188
7189 return in->channel_mask;
7190}
7191
7192static audio_format_t in_get_format(const struct audio_stream *stream)
7193{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007194 struct stream_in *in = (struct stream_in *)stream;
7195
7196 return in->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007197}
7198
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07007199static int in_set_format(struct audio_stream *stream __unused,
7200 audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007201{
7202 return -ENOSYS;
7203}
7204
7205static int in_standby(struct audio_stream *stream)
7206{
7207 struct stream_in *in = (struct stream_in *)stream;
7208 struct audio_device *adev = in->dev;
7209 int status = 0;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05307210 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
7211 stream, in->usecase, use_case_table[in->usecase]);
Haynes Mathew George16081042017-05-31 17:16:49 -07007212 bool do_stop = true;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05307213
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07007214 lock_input_stream(in);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07007215 if (!in->standby && in->is_st_session) {
7216 ALOGD("%s: sound trigger pcm stop lab", __func__);
7217 audio_extn_sound_trigger_stop_lab(in);
George Gao3018ede2019-10-23 13:23:00 -07007218 if (adev->num_va_sessions > 0)
7219 adev->num_va_sessions--;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07007220 in->standby = 1;
7221 }
7222
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007223 if (!in->standby) {
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07007224 if (adev->adm_deregister_stream)
7225 adev->adm_deregister_stream(adev->adm_data, in->capture_handle);
7226
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08007227 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007228 in->standby = true;
Zhou Songa8895042016-07-05 17:54:22 +08007229 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
kunleizbecba2d2017-09-07 13:37:16 +08007230 do_stop = false;
Zhou Songa8895042016-07-05 17:54:22 +08007231 voice_extn_compress_voip_close_input_stream(stream);
7232 ALOGD("VOIP input entered standby");
Haynes Mathew George16081042017-05-31 17:16:49 -07007233 } else if (in->usecase == USECASE_AUDIO_RECORD_MMAP) {
7234 do_stop = in->capture_started;
7235 in->capture_started = false;
Phil Burkd898ba62019-06-20 12:49:01 -07007236 if (in->mmap_shared_memory_fd >= 0) {
7237 ALOGV("%s: closing mmap_shared_memory_fd = %d",
7238 __func__, in->mmap_shared_memory_fd);
7239 close(in->mmap_shared_memory_fd);
7240 in->mmap_shared_memory_fd = -1;
7241 }
Zhou Songa8895042016-07-05 17:54:22 +08007242 } else {
Dhananjay Kumar7dbe3562019-08-30 05:49:33 +05307243 if (audio_extn_cin_attached_usecase(in))
Manish Dewangan46e07982018-12-13 18:18:59 +05307244 audio_extn_cin_close_input_stream(in);
kunleizbecba2d2017-09-07 13:37:16 +08007245 }
7246
Arun Mirpuri5d170872019-03-26 13:21:31 -07007247 if (in->pcm) {
7248 ATRACE_BEGIN("pcm_in_close");
7249 pcm_close(in->pcm);
7250 ATRACE_END();
7251 in->pcm = NULL;
7252 }
7253
Carter Hsu2e429db2019-05-14 18:50:52 +08007254 if (do_stop)
Zhou Songa8895042016-07-05 17:54:22 +08007255 status = stop_input_stream(in);
Quinn Malef6050362019-01-30 15:55:40 -08007256
George Gao3018ede2019-10-23 13:23:00 -07007257 if (in->source == AUDIO_SOURCE_VOICE_RECOGNITION) {
7258 if (adev->num_va_sessions > 0)
7259 adev->num_va_sessions--;
7260 }
Quinn Malef6050362019-01-30 15:55:40 -08007261
Eric Laurent150dbfe2013-02-27 14:31:02 -08007262 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007263 }
7264 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07007265 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007266 return status;
7267}
7268
Aalique Grahame22e49102018-12-18 14:23:57 -08007269static int in_dump(const struct audio_stream *stream,
7270 int fd)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007271{
Aalique Grahame22e49102018-12-18 14:23:57 -08007272 struct stream_in *in = (struct stream_in *)stream;
7273
7274 // We try to get the lock for consistency,
7275 // but it isn't necessary for these variables.
7276 // If we're not in standby, we may be blocked on a read.
7277 const bool locked = (pthread_mutex_trylock(&in->lock) == 0);
7278 dprintf(fd, " Standby: %s\n", in->standby ? "yes" : "no");
7279 dprintf(fd, " Frames read: %lld\n", (long long)in->frames_read);
7280 dprintf(fd, " Frames muted: %lld\n", (long long)in->frames_muted);
Dechen Chai22768452021-07-30 09:29:16 +05307281#ifndef LINUX_ENABLED
Andy Hungc6bfd4a2019-07-01 18:26:00 -07007282 char buffer[256]; // for statistics formatting
7283 if (in->start_latency_ms.n > 0) {
7284 simple_stats_to_string(&in->start_latency_ms, buffer, sizeof(buffer));
7285 dprintf(fd, " Start latency ms: %s\n", buffer);
7286 }
Dechen Chai22768452021-07-30 09:29:16 +05307287#endif
Aalique Grahame22e49102018-12-18 14:23:57 -08007288 if (locked) {
7289 pthread_mutex_unlock(&in->lock);
7290 }
Dechen Chai22768452021-07-30 09:29:16 +05307291#ifndef LINUX_ENABLED
Aalique Grahame22e49102018-12-18 14:23:57 -08007292 // dump error info
7293 (void)error_log_dump(
7294 in->error_log, fd, " " /* prefix */, 0 /* lines */, 0 /* limit_ns */);
Dechen Chai22768452021-07-30 09:29:16 +05307295#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007296 return 0;
7297}
7298
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05307299static void in_snd_mon_cb(void * stream, struct str_parms * parms)
7300{
7301 if (!stream || !parms)
7302 return;
7303
7304 struct stream_in *in = (struct stream_in *)stream;
7305 struct audio_device *adev = in->dev;
7306
7307 card_status_t status;
7308 int card;
7309 if (parse_snd_card_status(parms, &card, &status) < 0)
7310 return;
7311
7312 pthread_mutex_lock(&adev->lock);
7313 bool valid_cb = (card == adev->snd_card);
7314 pthread_mutex_unlock(&adev->lock);
7315
7316 if (!valid_cb)
7317 return;
7318
7319 lock_input_stream(in);
7320 if (in->card_status != status)
7321 in->card_status = status;
7322 pthread_mutex_unlock(&in->lock);
7323
7324 ALOGW("in_snd_mon_cb for card %d usecase %s, status %s", card,
7325 use_case_table[in->usecase],
7326 status == CARD_STATUS_OFFLINE ? "offline" : "online");
7327
7328 // a better solution would be to report error back to AF and let
7329 // it put the stream to standby
7330 if (status == CARD_STATUS_OFFLINE)
7331 in_standby(&in->stream.common);
7332
7333 return;
7334}
7335
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07007336int route_input_stream(struct stream_in *in,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08007337 struct listnode *devices,
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07007338 audio_source_t source)
7339{
7340 struct audio_device *adev = in->dev;
7341 int ret = 0;
7342
7343 lock_input_stream(in);
7344 pthread_mutex_lock(&adev->lock);
7345
7346 /* no audio source uses val == 0 */
7347 if ((in->source != source) && (source != AUDIO_SOURCE_DEFAULT)) {
7348 in->source = source;
7349 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
7350 (in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
7351 (voice_extn_compress_voip_is_format_supported(in->format)) &&
7352 (in->config.rate == 8000 || in->config.rate == 16000 ||
7353 in->config.rate == 32000 || in->config.rate == 48000 ) &&
7354 (audio_channel_count_from_in_mask(in->channel_mask) == 1)) {
7355 ret = voice_extn_compress_voip_open_input_stream(in);
7356 if (ret != 0) {
7357 ALOGE("%s: Compress voip input cannot be opened, error:%d",
7358 __func__, ret);
7359 }
7360 }
7361 }
7362
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08007363 if (!compare_devices(&in->device_list, devices) && !list_empty(devices) &&
7364 is_audio_in_device_type(devices)) {
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07007365 // Workaround: If routing to an non existing usb device, fail gracefully
7366 // The routing request will otherwise block during 10 second
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08007367 struct str_parms *usb_addr =
7368 str_parms_create_str(get_usb_device_address(devices));
7369 if (is_usb_in_device_type(devices) && usb_addr &&
Weiyin Jiangabedea32020-12-09 12:49:19 +08007370 !audio_extn_usb_connected(NULL)) {
7371 ALOGW("%s: ignoring rerouting to non existing USB", __func__);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07007372 ret = -ENOSYS;
7373 } else {
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07007374 /* If recording is in progress, change the tx device to new device */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08007375 assign_devices(&in->device_list, devices);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07007376 if (!in->standby && !in->is_st_session) {
7377 ALOGV("update input routing change");
7378 // inform adm before actual routing to prevent glitches.
7379 if (adev->adm_on_routing_change) {
7380 adev->adm_on_routing_change(adev->adm_data,
7381 in->capture_handle);
7382 ret = select_devices(adev, in->usecase);
7383 if (in->usecase == USECASE_AUDIO_RECORD_LOW_LATENCY)
7384 adev->adm_routing_changed = true;
7385 }
7386 }
7387 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08007388 if (usb_addr)
7389 str_parms_destroy(usb_addr);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07007390 }
7391 pthread_mutex_unlock(&adev->lock);
7392 pthread_mutex_unlock(&in->lock);
7393
Aniket Kumar Latad13758f2020-08-06 15:11:36 -07007394 ALOGV("%s: exit: status(%d)", __func__, ret);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07007395 return ret;
7396}
7397
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007398static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
7399{
7400 struct stream_in *in = (struct stream_in *)stream;
7401 struct audio_device *adev = in->dev;
7402 struct str_parms *parms;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007403 char value[32];
Sujin Panicker7c7b6f92020-04-03 12:57:55 +05307404 int err = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007405
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05307406 ALOGD("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007407 parms = str_parms_create_str(kvpairs);
7408
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05307409 if (!parms)
7410 goto error;
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07007411 lock_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08007412 pthread_mutex_lock(&adev->lock);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08007413
Sujin Panicker7c7b6f92020-04-03 12:57:55 +05307414 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_PROFILE, value, sizeof(value));
7415 if (err >= 0) {
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +05307416 strlcpy(in->profile, value, sizeof(in->profile));
7417 ALOGV("updating stream profile with value '%s'", in->profile);
7418 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
7419 &adev->streams_input_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08007420 &in->device_list, in->flags, in->format,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +05307421 in->sample_rate, in->bit_width,
7422 in->profile, &in->app_type_cfg);
7423 }
7424
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007425 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08007426 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007427
7428 str_parms_destroy(parms);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05307429error:
Sujin Panicker7c7b6f92020-04-03 12:57:55 +05307430 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007431}
7432
7433static char* in_get_parameters(const struct audio_stream *stream,
7434 const char *keys)
7435{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007436 struct stream_in *in = (struct stream_in *)stream;
7437 struct str_parms *query = str_parms_create_str(keys);
7438 char *str;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007439 struct str_parms *reply = str_parms_create();
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07007440
7441 if (!query || !reply) {
Alexy Josephaee4fdd2016-01-29 13:02:07 -08007442 if (reply) {
7443 str_parms_destroy(reply);
7444 }
7445 if (query) {
7446 str_parms_destroy(query);
7447 }
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07007448 ALOGE("in_get_parameters: failed to create query or reply");
7449 return NULL;
7450 }
7451
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007452 ALOGV("%s: enter: keys - %s %s ", __func__, use_case_table[in->usecase], keys);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007453
7454 voice_extn_in_get_parameters(in, query, reply);
7455
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007456 stream_get_parameter_channels(query, reply,
7457 &in->supported_channel_masks[0]);
7458 stream_get_parameter_formats(query, reply,
7459 &in->supported_formats[0]);
7460 stream_get_parameter_rates(query, reply,
7461 &in->supported_sample_rates[0]);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007462 str = str_parms_to_str(reply);
7463 str_parms_destroy(query);
7464 str_parms_destroy(reply);
7465
7466 ALOGV("%s: exit: returns - %s", __func__, str);
7467 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007468}
7469
Aalique Grahame22e49102018-12-18 14:23:57 -08007470static int in_set_gain(struct audio_stream_in *stream,
7471 float gain)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007472{
Aalique Grahame22e49102018-12-18 14:23:57 -08007473 struct stream_in *in = (struct stream_in *)stream;
7474 char mixer_ctl_name[128];
7475 struct mixer_ctl *ctl;
7476 int ctl_value;
7477
7478 ALOGV("%s: gain %f", __func__, gain);
7479
7480 if (stream == NULL)
7481 return -EINVAL;
7482
7483 /* in_set_gain() only used to silence MMAP capture for now */
7484 if (in->usecase != USECASE_AUDIO_RECORD_MMAP)
7485 return -ENOSYS;
7486
7487 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name), "Capture %d Volume", in->pcm_device_id);
7488
7489 ctl = mixer_get_ctl_by_name(in->dev->mixer, mixer_ctl_name);
7490 if (!ctl) {
7491 ALOGW("%s: Could not get ctl for mixer cmd - %s",
7492 __func__, mixer_ctl_name);
7493 return -ENOSYS;
7494 }
7495
7496 if (gain < RECORD_GAIN_MIN)
7497 gain = RECORD_GAIN_MIN;
7498 else if (gain > RECORD_GAIN_MAX)
7499 gain = RECORD_GAIN_MAX;
7500 ctl_value = (int)(RECORD_VOLUME_CTL_MAX * gain);
7501
7502 mixer_ctl_set_value(ctl, 0, ctl_value);
7503
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007504 return 0;
7505}
7506
7507static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
7508 size_t bytes)
7509{
7510 struct stream_in *in = (struct stream_in *)stream;
Pallavid7c7a272018-01-16 11:22:55 +05307511
7512 if (in == NULL) {
7513 ALOGE("%s: stream_in ptr is NULL", __func__);
7514 return -EINVAL;
7515 }
7516
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007517 struct audio_device *adev = in->dev;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05307518 int ret = -1;
Dhananjay Kumar97b81e32019-05-15 21:00:21 +05307519 size_t bytes_read = 0, frame_size = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007520
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07007521 lock_input_stream(in);
Naresh Tanniru4c630392014-05-12 01:05:52 +05307522
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07007523 if (in->is_st_session) {
7524 ALOGVV(" %s: reading on st session bytes=%zu", __func__, bytes);
7525 /* Read from sound trigger HAL */
7526 audio_extn_sound_trigger_read(in, buffer, bytes);
Quinn Malef6050362019-01-30 15:55:40 -08007527 if (in->standby) {
George Gao3018ede2019-10-23 13:23:00 -07007528 if (adev->num_va_sessions < UINT_MAX)
7529 adev->num_va_sessions++;
Quinn Malef6050362019-01-30 15:55:40 -08007530 in->standby = 0;
7531 }
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07007532 pthread_mutex_unlock(&in->lock);
7533 return bytes;
7534 }
7535
Haynes Mathew George16081042017-05-31 17:16:49 -07007536 if (in->usecase == USECASE_AUDIO_RECORD_MMAP) {
7537 ret = -ENOSYS;
7538 goto exit;
7539 }
7540
Aniket Kumar Lata60586a92019-05-22 22:18:55 -07007541 if (in->usecase == USECASE_AUDIO_RECORD_LOW_LATENCY &&
7542 !in->standby && adev->adm_routing_changed) {
7543 ret = -ENOSYS;
7544 goto exit;
7545 }
7546
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007547 if (in->standby) {
Andy Hungc6bfd4a2019-07-01 18:26:00 -07007548 const int64_t startNs = systemTime(SYSTEM_TIME_MONOTONIC);
7549
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07007550 pthread_mutex_lock(&adev->lock);
7551 if (in->usecase == USECASE_COMPRESS_VOIP_CALL)
7552 ret = voice_extn_compress_voip_start_input_stream(in);
7553 else
7554 ret = start_input_stream(in);
George Gao3018ede2019-10-23 13:23:00 -07007555 if (!ret && in->source == AUDIO_SOURCE_VOICE_RECOGNITION) {
7556 if (adev->num_va_sessions < UINT_MAX)
7557 adev->num_va_sessions++;
7558 }
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07007559 pthread_mutex_unlock(&adev->lock);
7560 if (ret != 0) {
7561 goto exit;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007562 }
7563 in->standby = 0;
Dechen Chai22768452021-07-30 09:29:16 +05307564#ifndef LINUX_ENABLED
Andy Hungc6bfd4a2019-07-01 18:26:00 -07007565 // log startup time in ms.
7566 simple_stats_log(
7567 &in->start_latency_ms, (systemTime(SYSTEM_TIME_MONOTONIC) - startNs) * 1e-6);
Dechen Chai22768452021-07-30 09:29:16 +05307568#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007569 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007570
Deeraj Soman30cc7ae2019-03-18 16:26:55 +05307571 /* Avoid read if capture_stopped is set */
7572 if (android_atomic_acquire_load(&(in->capture_stopped)) > 0) {
7573 ALOGD("%s: force stopped catpure session, ignoring read request", __func__);
7574 ret = -EINVAL;
7575 goto exit;
7576 }
7577
Haynes Mathew George5beddd42016-06-27 18:33:40 -07007578 // what's the duration requested by the client?
7579 long ns = 0;
7580
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307581 if (in->pcm && in->config.rate)
Haynes Mathew George5beddd42016-06-27 18:33:40 -07007582 ns = pcm_bytes_to_frames(in->pcm, bytes)*1000000000LL/
7583 in->config.rate;
7584
Aniket Kumar Lata60586a92019-05-22 22:18:55 -07007585 ret = request_in_focus(in, ns);
7586 if (ret != 0)
7587 goto exit;
Haynes Mathew George5beddd42016-06-27 18:33:40 -07007588 bool use_mmap = is_mmap_usecase(in->usecase) || in->realtime;
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07007589
Dhananjay Kumar7dbe3562019-08-30 05:49:33 +05307590 if (audio_extn_cin_attached_usecase(in)) {
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307591 ret = audio_extn_cin_read(in, buffer, bytes, &bytes_read);
7592 } else if (in->pcm) {
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05307593 if (audio_extn_ssr_get_stream() == in) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07007594 ret = audio_extn_ssr_read(stream, buffer, bytes);
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05307595 } else if (audio_extn_compr_cap_usecase_supported(in->usecase)) {
Mingming Yine62d7842013-10-25 16:26:03 -07007596 ret = audio_extn_compr_cap_read(in, buffer, bytes);
Haynes Mathew George5beddd42016-06-27 18:33:40 -07007597 } else if (use_mmap) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07007598 ret = pcm_mmap_read(in->pcm, buffer, bytes);
Garmond Leunge2433c32017-09-28 21:51:22 -07007599 } else if (audio_extn_ffv_get_stream() == in) {
7600 ret = audio_extn_ffv_read(stream, buffer, bytes);
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05307601 } else {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07007602 ret = pcm_read(in->pcm, buffer, bytes);
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307603 /* data from DSP comes in 24_8 format, convert it to 8_24 */
7604 if (!ret && bytes > 0 && (in->format == AUDIO_FORMAT_PCM_8_24_BIT)) {
7605 if (audio_extn_utils_convert_format_24_8_to_8_24(buffer, bytes)
7606 != bytes) {
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05307607 ret = -EINVAL;
7608 goto exit;
7609 }
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307610 } else if (ret < 0) {
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05307611 ret = -errno;
7612 }
7613 }
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307614 /* bytes read is always set to bytes for non compress usecases */
7615 bytes_read = bytes;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007616 }
7617
Haynes Mathew George5beddd42016-06-27 18:33:40 -07007618 release_in_focus(in);
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07007619
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007620 /*
Quinn Malef6050362019-01-30 15:55:40 -08007621 * Instead of writing zeroes here, we could trust the hardware to always
7622 * provide zeroes when muted. This is also muted with voice recognition
7623 * usecases so that other clients do not have access to voice recognition
7624 * data.
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007625 */
Quinn Malef6050362019-01-30 15:55:40 -08007626 if ((ret == 0 && voice_get_mic_mute(adev) &&
7627 !voice_is_in_call_rec_stream(in) &&
Zhou Song62ea0282020-03-22 19:53:01 +08007628 (in->usecase != USECASE_AUDIO_RECORD_AFE_PROXY &&
7629 in->usecase != USECASE_AUDIO_RECORD_AFE_PROXY2)) ||
Quinn Malef6050362019-01-30 15:55:40 -08007630 (adev->num_va_sessions &&
7631 in->source != AUDIO_SOURCE_VOICE_RECOGNITION &&
7632 property_get_bool("persist.vendor.audio.va_concurrency_mute_enabled",
7633 false)))
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007634 memset(buffer, 0, bytes);
7635
7636exit:
Dhananjay Kumar97b81e32019-05-15 21:00:21 +05307637 frame_size = audio_stream_in_frame_size(stream);
7638 if (frame_size > 0)
7639 in->frames_read += bytes_read/frame_size;
7640
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07007641 if (-ENETRESET == ret)
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05307642 in->card_status = CARD_STATUS_OFFLINE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007643 pthread_mutex_unlock(&in->lock);
7644
7645 if (ret != 0) {
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05307646 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05307647 pthread_mutex_lock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05307648 voice_extn_compress_voip_close_input_stream(&in->stream.common);
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05307649 pthread_mutex_unlock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05307650 in->standby = true;
7651 }
Dhananjay Kumar7dbe3562019-08-30 05:49:33 +05307652 if (!audio_extn_cin_attached_usecase(in)) {
Sharad Sangled17c9122017-03-20 15:58:52 +05307653 bytes_read = bytes;
7654 memset(buffer, 0, bytes);
7655 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007656 in_standby(&in->stream.common);
Aniket Kumar Lata60586a92019-05-22 22:18:55 -07007657 if (in->usecase == USECASE_AUDIO_RECORD_LOW_LATENCY)
7658 adev->adm_routing_changed = false;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07007659 ALOGV("%s: read failed status %d- sleeping for buffer duration", __func__, ret);
Ashish Jainbbce4322016-02-16 13:25:27 +05307660 usleep((uint64_t)bytes * 1000000 / audio_stream_in_frame_size(stream) /
Naresh Tanniru4c630392014-05-12 01:05:52 +05307661 in_get_sample_rate(&in->stream.common));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007662 }
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307663 return bytes_read;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007664}
7665
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07007666static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007667{
7668 return 0;
7669}
7670
Aalique Grahame22e49102018-12-18 14:23:57 -08007671static int in_get_capture_position(const struct audio_stream_in *stream,
7672 int64_t *frames, int64_t *time)
7673{
7674 if (stream == NULL || frames == NULL || time == NULL) {
7675 return -EINVAL;
7676 }
7677 struct stream_in *in = (struct stream_in *)stream;
7678 int ret = -ENOSYS;
7679
7680 lock_input_stream(in);
7681 // note: ST sessions do not close the alsa pcm driver synchronously
7682 // on standby. Therefore, we may return an error even though the
7683 // pcm stream is still opened.
7684 if (in->standby) {
7685 ALOGE_IF(in->pcm != NULL && !in->is_st_session,
7686 "%s stream in standby but pcm not NULL for non ST session", __func__);
7687 goto exit;
7688 }
7689 if (in->pcm) {
7690 struct timespec timestamp;
7691 unsigned int avail;
7692 if (pcm_get_htimestamp(in->pcm, &avail, &timestamp) == 0) {
7693 *frames = in->frames_read + avail;
Robert Lee58215542019-07-15 20:55:12 +08007694 *time = timestamp.tv_sec * 1000000000LL + timestamp.tv_nsec
George Gao9ba8a142020-07-23 14:30:03 -07007695 - platform_capture_latency(in) * 1000LL;
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +05307696 //Adjustment accounts for A2dp decoder latency for recording usecase
7697 // Note: decoder latency is returned in ms, while platform_capture_latency in ns.
7698 if (is_a2dp_in_device_type(&in->device_list))
7699 *time -= audio_extn_a2dp_get_decoder_latency() * 1000000LL;
Aalique Grahame22e49102018-12-18 14:23:57 -08007700 ret = 0;
7701 }
7702 }
7703exit:
7704 pthread_mutex_unlock(&in->lock);
7705 return ret;
7706}
7707
Carter Hsu2e429db2019-05-14 18:50:52 +08007708static int in_update_effect_list(bool add, effect_handle_t effect,
7709 struct listnode *head)
7710{
7711 struct listnode *node;
7712 struct in_effect_list *elist = NULL;
7713 struct in_effect_list *target = NULL;
7714 int ret = 0;
7715
7716 if (!head)
7717 return ret;
7718
7719 list_for_each(node, head) {
7720 elist = node_to_item(node, struct in_effect_list, list);
7721 if (elist->handle == effect) {
7722 target = elist;
7723 break;
7724 }
7725 }
7726
7727 if (add) {
7728 if (target) {
7729 ALOGD("effect %p already exist", effect);
7730 return ret;
7731 }
7732
7733 target = (struct in_effect_list *)
7734 calloc(1, sizeof(struct in_effect_list));
7735
7736 if (!target) {
7737 ALOGE("%s:fail to allocate memory", __func__);
7738 return -ENOMEM;
7739 }
7740
7741 target->handle = effect;
7742 list_add_tail(head, &target->list);
7743 } else {
7744 if (target) {
7745 list_remove(&target->list);
7746 free(target);
7747 }
7748 }
7749
7750 return ret;
7751}
7752
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007753static int add_remove_audio_effect(const struct audio_stream *stream,
7754 effect_handle_t effect,
7755 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007756{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007757 struct stream_in *in = (struct stream_in *)stream;
7758 int status = 0;
7759 effect_descriptor_t desc;
7760
7761 status = (*effect)->get_descriptor(effect, &desc);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07007762 ALOGV("%s: status %d in->standby %d enable:%d", __func__, status, in->standby, enable);
7763
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007764 if (status != 0)
7765 return status;
7766
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07007767 lock_input_stream(in);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007768 pthread_mutex_lock(&in->dev->lock);
kunleizd96526c2018-04-09 11:12:32 +08007769 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
Carter Hsu2e429db2019-05-14 18:50:52 +08007770 in->source == AUDIO_SOURCE_VOICE_RECOGNITION ||
7771 adev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007772 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
Carter Hsu2e429db2019-05-14 18:50:52 +08007773
7774 in_update_effect_list(enable, effect, &in->aec_list);
7775 enable = !list_empty(&in->aec_list);
7776 if (enable == in->enable_aec)
7777 goto exit;
7778
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007779 in->enable_aec = enable;
Carter Hsu2e429db2019-05-14 18:50:52 +08007780 ALOGD("AEC enable %d", enable);
7781
Aalique Grahame22e49102018-12-18 14:23:57 -08007782 if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
7783 in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) {
7784 in->dev->enable_voicerx = enable;
7785 struct audio_usecase *usecase;
7786 struct listnode *node;
7787 list_for_each(node, &in->dev->usecase_list) {
7788 usecase = node_to_item(node, struct audio_usecase, list);
7789 if (usecase->type == PCM_PLAYBACK)
7790 select_devices(in->dev, usecase->id);
7791 }
7792 }
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07007793 if (!in->standby) {
7794 if (enable_disable_effect(in->dev, EFFECT_AEC, enable) == ENOSYS)
7795 select_devices(in->dev, in->usecase);
7796 }
7797
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007798 }
Carter Hsu2e429db2019-05-14 18:50:52 +08007799 if (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0) {
7800
7801 in_update_effect_list(enable, effect, &in->ns_list);
7802 enable = !list_empty(&in->ns_list);
7803 if (enable == in->enable_ns)
7804 goto exit;
7805
Ravi Kumar Alamanda198185e2013-11-07 15:42:19 -08007806 in->enable_ns = enable;
Carter Hsu2e429db2019-05-14 18:50:52 +08007807 ALOGD("NS enable %d", enable);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07007808 if (!in->standby) {
kunleizd96526c2018-04-09 11:12:32 +08007809 if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
Ramu Gottipatifa5be522021-12-28 19:18:21 +05307810 in->source == AUDIO_SOURCE_VOICE_RECOGNITION ||
kunleizd96526c2018-04-09 11:12:32 +08007811 in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) {
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07007812 if (enable_disable_effect(in->dev, EFFECT_NS, enable) == ENOSYS)
7813 select_devices(in->dev, in->usecase);
7814 } else
7815 select_devices(in->dev, in->usecase);
7816 }
Ravi Kumar Alamanda198185e2013-11-07 15:42:19 -08007817 }
Carter Hsu2e429db2019-05-14 18:50:52 +08007818exit:
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007819 pthread_mutex_unlock(&in->dev->lock);
7820 pthread_mutex_unlock(&in->lock);
7821
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007822 return 0;
7823}
7824
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007825static int in_add_audio_effect(const struct audio_stream *stream,
7826 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007827{
Eric Laurent994a6932013-07-17 11:51:42 -07007828 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007829 return add_remove_audio_effect(stream, effect, true);
7830}
7831
7832static int in_remove_audio_effect(const struct audio_stream *stream,
7833 effect_handle_t effect)
7834{
Eric Laurent994a6932013-07-17 11:51:42 -07007835 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007836 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007837}
7838
Haynes Mathew George16081042017-05-31 17:16:49 -07007839static int in_stop(const struct audio_stream_in* stream)
7840{
7841 struct stream_in *in = (struct stream_in *)stream;
7842 struct audio_device *adev = in->dev;
7843
7844 int ret = -ENOSYS;
7845 ALOGV("%s", __func__);
7846 pthread_mutex_lock(&adev->lock);
7847 if (in->usecase == USECASE_AUDIO_RECORD_MMAP && !in->standby &&
7848 in->capture_started && in->pcm != NULL) {
7849 pcm_stop(in->pcm);
7850 ret = stop_input_stream(in);
7851 in->capture_started = false;
7852 }
7853 pthread_mutex_unlock(&adev->lock);
7854 return ret;
7855}
7856
7857static int in_start(const struct audio_stream_in* stream)
7858{
7859 struct stream_in *in = (struct stream_in *)stream;
7860 struct audio_device *adev = in->dev;
7861 int ret = -ENOSYS;
7862
7863 ALOGV("%s in %p", __func__, in);
7864 pthread_mutex_lock(&adev->lock);
7865 if (in->usecase == USECASE_AUDIO_RECORD_MMAP && !in->standby &&
7866 !in->capture_started && in->pcm != NULL) {
7867 if (!in->capture_started) {
7868 ret = start_input_stream(in);
7869 if (ret == 0) {
7870 in->capture_started = true;
7871 }
7872 }
7873 }
7874 pthread_mutex_unlock(&adev->lock);
7875 return ret;
7876}
7877
Phil Burke0a86d12019-02-16 22:28:11 -08007878// Read offset for the positional timestamp from a persistent vendor property.
7879// This is to workaround apparent inaccuracies in the timing information that
7880// is used by the AAudio timing model. The inaccuracies can cause glitches.
7881static int64_t in_get_mmap_time_offset() {
7882 const int32_t kDefaultOffsetMicros = 0;
7883 int32_t mmap_time_offset_micros = property_get_int32(
Weiyin Jiangbd68b4d2019-05-17 16:29:50 +08007884 "persist.vendor.audio.in_mmap_delay_micros", kDefaultOffsetMicros);
Phil Burke0a86d12019-02-16 22:28:11 -08007885 ALOGI("mmap_time_offset_micros = %d for input", mmap_time_offset_micros);
7886 return mmap_time_offset_micros * (int64_t)1000;
7887}
7888
Haynes Mathew George16081042017-05-31 17:16:49 -07007889static int in_create_mmap_buffer(const struct audio_stream_in *stream,
7890 int32_t min_size_frames,
7891 struct audio_mmap_buffer_info *info)
7892{
7893 struct stream_in *in = (struct stream_in *)stream;
7894 struct audio_device *adev = in->dev;
7895 int ret = 0;
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07007896 unsigned int offset1 = 0;
7897 unsigned int frames1 = 0;
Haynes Mathew George16081042017-05-31 17:16:49 -07007898 const char *step = "";
Arun Mirpuri5d170872019-03-26 13:21:31 -07007899 uint32_t mmap_size = 0;
7900 uint32_t buffer_size = 0;
Haynes Mathew George16081042017-05-31 17:16:49 -07007901
7902 pthread_mutex_lock(&adev->lock);
7903 ALOGV("%s in %p", __func__, in);
7904
Sharad Sanglec6f32552018-05-04 16:15:38 +05307905 if (CARD_STATUS_OFFLINE == in->card_status||
Shubhasini Sugumaran3fde6582022-01-19 15:54:50 -05007906 CARD_STATUS_OFFLINE == adev->card_status ||
7907 POWER_POLICY_STATUS_OFFLINE == adev->in_power_policy) {
Sharad Sanglec6f32552018-05-04 16:15:38 +05307908 ALOGW("in->card_status or adev->card_status offline, try again");
7909 ret = -EIO;
7910 goto exit;
7911 }
7912
Sujin Panicker7c7b6f92020-04-03 12:57:55 +05307913 if (info == NULL || !(min_size_frames > 0 && min_size_frames < INT32_MAX)) {
Haynes Mathew George16081042017-05-31 17:16:49 -07007914 ALOGE("%s invalid argument info %p min_size_frames %d", __func__, info, min_size_frames);
7915 ret = -EINVAL;
7916 goto exit;
7917 }
7918 if (in->usecase != USECASE_AUDIO_RECORD_MMAP || !in->standby) {
7919 ALOGE("%s: usecase = %d, standby = %d", __func__, in->usecase, in->standby);
7920 ALOGV("%s in %p", __func__, in);
7921 ret = -ENOSYS;
7922 goto exit;
7923 }
7924 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
7925 if (in->pcm_device_id < 0) {
7926 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
7927 __func__, in->pcm_device_id, in->usecase);
7928 ret = -EINVAL;
7929 goto exit;
7930 }
7931
7932 adjust_mmap_period_count(&in->config, min_size_frames);
7933
7934 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
7935 __func__, adev->snd_card, in->pcm_device_id, in->config.channels);
7936 in->pcm = pcm_open(adev->snd_card, in->pcm_device_id,
7937 (PCM_IN | PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC), &in->config);
Satish Babu Patakokila54ce83d2018-07-06 18:00:37 +05307938 if (errno == ENETRESET && !pcm_is_ready(in->pcm)) {
Sharad Sanglec6f32552018-05-04 16:15:38 +05307939 ALOGE("%s: pcm_open failed errno:%d\n", __func__, errno);
7940 in->card_status = CARD_STATUS_OFFLINE;
7941 adev->card_status = CARD_STATUS_OFFLINE;
7942 ret = -EIO;
7943 goto exit;
7944 }
7945
Haynes Mathew George16081042017-05-31 17:16:49 -07007946 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) {
7947 step = "open";
7948 ret = -ENODEV;
7949 goto exit;
7950 }
7951
7952 ret = pcm_mmap_begin(in->pcm, &info->shared_memory_address, &offset1, &frames1);
7953 if (ret < 0) {
7954 step = "begin";
7955 goto exit;
7956 }
Haynes Mathew George16081042017-05-31 17:16:49 -07007957
juyuchen626833d2019-06-04 16:48:02 +08007958 info->flags = 0;
Arun Mirpuri5d170872019-03-26 13:21:31 -07007959 info->buffer_size_frames = pcm_get_buffer_size(in->pcm);
7960 buffer_size = pcm_frames_to_bytes(in->pcm, info->buffer_size_frames);
7961 info->burst_size_frames = in->config.period_size;
7962 ret = platform_get_mmap_data_fd(adev->platform,
7963 in->pcm_device_id, 1 /*capture*/,
7964 &info->shared_memory_fd,
7965 &mmap_size);
7966 if (ret < 0) {
7967 // Fall back to non exclusive mode
7968 info->shared_memory_fd = pcm_get_poll_fd(in->pcm);
7969 } else {
Phil Burkd898ba62019-06-20 12:49:01 -07007970 in->mmap_shared_memory_fd = info->shared_memory_fd; // for closing later
7971 ALOGV("%s: opened mmap_shared_memory_fd = %d", __func__, in->mmap_shared_memory_fd);
7972
Arun Mirpuri5d170872019-03-26 13:21:31 -07007973 if (mmap_size < buffer_size) {
7974 step = "mmap";
7975 goto exit;
7976 }
juyuchen626833d2019-06-04 16:48:02 +08007977 info->flags |= AUDIO_MMAP_APPLICATION_SHAREABLE;
Arun Mirpuri5d170872019-03-26 13:21:31 -07007978 }
7979
7980 memset(info->shared_memory_address, 0, buffer_size);
Haynes Mathew George16081042017-05-31 17:16:49 -07007981
7982 ret = pcm_mmap_commit(in->pcm, 0, MMAP_PERIOD_SIZE);
7983 if (ret < 0) {
7984 step = "commit";
7985 goto exit;
7986 }
7987
Phil Burke0a86d12019-02-16 22:28:11 -08007988 in->mmap_time_offset_nanos = in_get_mmap_time_offset();
7989
Haynes Mathew George16081042017-05-31 17:16:49 -07007990 in->standby = false;
7991 ret = 0;
7992
7993 ALOGV("%s: got mmap buffer address %p info->buffer_size_frames %d",
7994 __func__, info->shared_memory_address, info->buffer_size_frames);
7995
7996exit:
7997 if (ret != 0) {
7998 if (in->pcm == NULL) {
7999 ALOGE("%s: %s - %d", __func__, step, ret);
8000 } else {
8001 ALOGE("%s: %s %s", __func__, step, pcm_get_error(in->pcm));
8002 pcm_close(in->pcm);
8003 in->pcm = NULL;
8004 }
8005 }
8006 pthread_mutex_unlock(&adev->lock);
8007 return ret;
8008}
8009
8010static int in_get_mmap_position(const struct audio_stream_in *stream,
8011 struct audio_mmap_position *position)
8012{
8013 struct stream_in *in = (struct stream_in *)stream;
8014 ALOGVV("%s", __func__);
8015 if (position == NULL) {
8016 return -EINVAL;
8017 }
Gautam Manam34d1f542021-01-05 20:24:37 +05308018 lock_input_stream(in);
Haynes Mathew George16081042017-05-31 17:16:49 -07008019 if (in->usecase != USECASE_AUDIO_RECORD_MMAP) {
Gautam Manam34d1f542021-01-05 20:24:37 +05308020 pthread_mutex_unlock(&in->lock);
Haynes Mathew George16081042017-05-31 17:16:49 -07008021 return -ENOSYS;
8022 }
8023 if (in->pcm == NULL) {
Gautam Manam34d1f542021-01-05 20:24:37 +05308024 pthread_mutex_unlock(&in->lock);
Haynes Mathew George16081042017-05-31 17:16:49 -07008025 return -ENOSYS;
8026 }
8027 struct timespec ts = { 0, 0 };
8028 int ret = pcm_mmap_get_hw_ptr(in->pcm, (unsigned int *)&position->position_frames, &ts);
8029 if (ret < 0) {
8030 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
Gautam Manam34d1f542021-01-05 20:24:37 +05308031 pthread_mutex_unlock(&in->lock);
Haynes Mathew George16081042017-05-31 17:16:49 -07008032 return ret;
8033 }
Phil Burke0a86d12019-02-16 22:28:11 -08008034 position->time_nanoseconds = ts.tv_sec*1000000000LL + ts.tv_nsec
8035 + in->mmap_time_offset_nanos;
Gautam Manam34d1f542021-01-05 20:24:37 +05308036 pthread_mutex_unlock(&in->lock);
Haynes Mathew George16081042017-05-31 17:16:49 -07008037 return 0;
8038}
8039
Naresh Tannirudcb47c52018-06-25 16:23:32 +05308040static int in_get_active_microphones(const struct audio_stream_in *stream,
8041 struct audio_microphone_characteristic_t *mic_array,
8042 size_t *mic_count) {
8043 struct stream_in *in = (struct stream_in *)stream;
8044 struct audio_device *adev = in->dev;
8045 ALOGVV("%s", __func__);
8046
8047 lock_input_stream(in);
8048 pthread_mutex_lock(&adev->lock);
8049 int ret = platform_get_active_microphones(adev->platform,
8050 audio_channel_count_from_in_mask(in->channel_mask),
8051 in->usecase, mic_array, mic_count);
8052 pthread_mutex_unlock(&adev->lock);
8053 pthread_mutex_unlock(&in->lock);
8054
8055 return ret;
8056}
8057
8058static int adev_get_microphones(const struct audio_hw_device *dev,
8059 struct audio_microphone_characteristic_t *mic_array,
8060 size_t *mic_count) {
8061 struct audio_device *adev = (struct audio_device *)dev;
8062 ALOGVV("%s", __func__);
8063
8064 pthread_mutex_lock(&adev->lock);
8065 int ret = platform_get_microphones(adev->platform, mic_array, mic_count);
8066 pthread_mutex_unlock(&adev->lock);
8067
8068 return ret;
8069}
juyuchendb308c22019-01-21 11:57:17 -07008070
8071static void in_update_sink_metadata(struct audio_stream_in *stream,
8072 const struct sink_metadata *sink_metadata) {
8073
8074 if (stream == NULL
8075 || sink_metadata == NULL
8076 || sink_metadata->tracks == NULL) {
8077 return;
8078 }
8079
8080 int error = 0;
8081 struct stream_in *in = (struct stream_in *)stream;
8082 struct audio_device *adev = in->dev;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08008083 struct listnode devices;
8084
8085 list_init(&devices);
juyuchendb308c22019-01-21 11:57:17 -07008086
8087 if (sink_metadata->track_count != 0)
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08008088 reassign_device_list(&devices, sink_metadata->tracks->dest_device, "");
juyuchendb308c22019-01-21 11:57:17 -07008089
8090 lock_input_stream(in);
8091 pthread_mutex_lock(&adev->lock);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08008092 ALOGV("%s: in->usecase: %d, device: %x", __func__, in->usecase, get_device_types(&devices));
juyuchendb308c22019-01-21 11:57:17 -07008093
Zhou Song503196b2021-07-23 17:31:05 +08008094 if ((in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY ||
8095 in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY2) &&
8096 !list_empty(&devices) &&
8097 adev->voice_tx_output != NULL) {
juyuchendb308c22019-01-21 11:57:17 -07008098 /* Use the rx device from afe-proxy record to route voice call because
8099 there is no routing if tx device is on primary hal and rx device
8100 is on other hal during voice call. */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08008101 assign_devices(&adev->voice_tx_output->device_list, &devices);
juyuchendb308c22019-01-21 11:57:17 -07008102
8103 if (!voice_is_call_state_active(adev)) {
8104 if (adev->mode == AUDIO_MODE_IN_CALL) {
8105 adev->current_call_output = adev->voice_tx_output;
8106 error = voice_start_call(adev);
8107 if (error != 0)
8108 ALOGE("%s: start voice call failed %d", __func__, error);
8109 }
8110 } else {
8111 adev->current_call_output = adev->voice_tx_output;
8112 voice_update_devices_for_all_voice_usecases(adev);
8113 }
8114 }
8115
Zhenlin Lian4f947842022-05-14 15:50:52 +05308116 clear_devices(&devices);
juyuchendb308c22019-01-21 11:57:17 -07008117 pthread_mutex_unlock(&adev->lock);
8118 pthread_mutex_unlock(&in->lock);
8119}
8120
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05308121int adev_open_output_stream(struct audio_hw_device *dev,
Haynes Mathew George16081042017-05-31 17:16:49 -07008122 audio_io_handle_t handle,
8123 audio_devices_t devices,
8124 audio_output_flags_t flags,
8125 struct audio_config *config,
8126 struct audio_stream_out **stream_out,
Derek Chenf6318be2017-06-12 17:16:24 -04008127 const char *address)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008128{
8129 struct audio_device *adev = (struct audio_device *)dev;
8130 struct stream_out *out;
Gangadhar Sb0210342019-02-22 17:39:41 +05308131 int ret = 0, ip_hdlr_stream = 0, ip_hdlr_dev = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07008132 audio_format_t format;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08008133 struct adsp_hdlr_stream_cfg hdlr_stream_cfg;
Manish Dewangan21a850a2017-08-14 12:03:55 +05308134 bool is_direct_passthough = false;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008135 bool is_hdmi = devices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
8136 bool is_usb_dev = audio_is_usb_out_device(devices) &&
8137 (devices != AUDIO_DEVICE_OUT_USB_ACCESSORY);
8138 bool direct_dev = is_hdmi || is_usb_dev;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008139 bool use_db_as_primary =
vivek mehtaae1018c2019-05-09 12:19:57 -07008140 property_get_bool("vendor.audio.feature.deepbuffer_as_primary.enable",
8141 false);
Vignesh Kulothungana6927272019-02-20 15:17:07 -08008142 bool force_haptic_path =
8143 property_get_bool("vendor.audio.test_haptic", false);
Aniket Kumar Lata23d8cbf2019-04-10 19:54:46 -07008144 bool is_voip_rx = flags & AUDIO_OUTPUT_FLAG_VOIP_RX;
Xiaojun Sang782e5b12020-06-29 21:13:06 +08008145#ifdef AUDIO_GKI_ENABLED
8146 __s32 *generic_dec;
8147#endif
Weiyin Jiang906db3c2021-03-02 13:17:04 +08008148 pthread_mutexattr_t latch_attr;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008149
kunleizdff872d2018-08-20 14:40:33 +08008150 if (is_usb_dev && (!audio_extn_usb_connected(NULL))) {
kunleizd6a9e0c2018-07-30 15:38:52 +08008151 is_usb_dev = false;
8152 devices = AUDIO_DEVICE_OUT_SPEAKER;
8153 ALOGW("%s: ignore set device to non existing USB card, use output device(%#x)",
8154 __func__, devices);
Mingshu Pangdbd20562019-11-25 18:04:39 +08008155 if (config->format == AUDIO_FORMAT_DEFAULT)
8156 config->format = AUDIO_FORMAT_PCM_16_BIT;
8157 if (config->sample_rate == 0)
8158 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
8159 if (config->channel_mask == AUDIO_CHANNEL_NONE)
8160 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
kunleizd6a9e0c2018-07-30 15:38:52 +08008161 }
8162
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008163 *stream_out = NULL;
Naresh Tanniru80659832014-06-04 18:17:56 +05308164
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008165 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
8166
Mingming Yin3a941d42016-02-17 18:08:05 -08008167 ALOGD("%s: enter: format(%#x) sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)\
Derek Chenf6318be2017-06-12 17:16:24 -04008168 stream_handle(%p) address(%s)", __func__, config->format, config->sample_rate, config->channel_mask,
8169 devices, flags, &out->stream, address);
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05308170
8171
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08008172 if (!out) {
8173 return -ENOMEM;
8174 }
8175
Haynes Mathew George204045b2015-02-25 20:32:03 -08008176 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07008177 pthread_mutex_init(&out->pre_lock, (const pthread_mutexattr_t *) NULL);
Weiyin Jiang906db3c2021-03-02 13:17:04 +08008178 pthread_mutexattr_init(&latch_attr);
8179 pthread_mutexattr_settype(&latch_attr, PTHREAD_MUTEX_RECURSIVE);
8180 pthread_mutex_init(&out->latch_lock, &latch_attr);
8181 pthread_mutexattr_destroy(&latch_attr);
Zhou Song48453a02018-01-10 17:50:59 +08008182 pthread_mutex_init(&out->position_query_lock, (const pthread_mutexattr_t *) NULL);
Haynes Mathew George204045b2015-02-25 20:32:03 -08008183 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
8184
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008185 if (devices == AUDIO_DEVICE_NONE)
8186 devices = AUDIO_DEVICE_OUT_SPEAKER;
8187
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008188 out->flags = flags;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08008189 list_init(&out->device_list);
8190 update_device_list(&out->device_list, devices, address, true /* add devices */);
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07008191 out->dev = adev;
Aalique Grahame65780b52017-09-27 14:59:56 -07008192 out->hal_op_format = out->hal_ip_format = format = out->format = config->format;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008193 out->sample_rate = config->sample_rate;
Sachin Mohan Gadag3d09acd2017-06-19 12:43:44 +05308194 out->channel_mask = config->channel_mask;
Ramjee Singh5857aeb2017-08-03 19:18:50 +05308195 if (out->channel_mask == AUDIO_CHANNEL_NONE)
8196 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
8197 else
8198 out->supported_channel_masks[0] = out->channel_mask;
Eric Laurentc4aef752013-09-12 17:45:53 -07008199 out->handle = handle;
Mingming Yin3ee55c62014-08-04 14:23:35 -07008200 out->bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Alexy Josephaa54c872014-12-03 02:46:47 -08008201 out->non_blocking = 0;
Ashish Jain83a6cc22016-06-28 14:34:17 +05308202 out->convert_buffer = NULL;
Ashish Jain1b9b30c2017-05-18 20:57:40 +05308203 out->started = 0;
Zhou Songbaddf9f2020-11-20 13:57:39 +08008204 out->a2dp_muted = false;
Aniket Kumar Lata932f4872017-11-06 18:29:44 -08008205 out->hal_output_suspend_supported = 0;
8206 out->dynamic_pm_qos_config_supported = 0;
Surendar Karkaf51b5842018-04-26 11:28:38 +05308207 out->set_dual_mono = false;
Manisha Agarwal7b3e3772019-02-20 14:33:45 +05308208 out->prev_card_status_offline = false;
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +05308209 out->pspd_coeff_sent = false;
Phil Burkd898ba62019-06-20 12:49:01 -07008210 out->mmap_shared_memory_fd = -1; // not open
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008211
Nikhil Laturkar26b690b2017-07-25 11:06:14 +05308212 if ((flags & AUDIO_OUTPUT_FLAG_BD) &&
Satish Babu Patakokila37e7c482018-02-02 11:50:06 +05308213 (property_get_bool("vendor.audio.matrix.limiter.enable", false)))
Ben Romberger6c4d3812017-06-13 17:46:45 -07008214 platform_set_device_params(out, DEVICE_PARAM_LIMITER_ID, 1);
8215
Aalique Grahame22e49102018-12-18 14:23:57 -08008216 if (direct_dev &&
8217 (audio_is_linear_pcm(out->format) ||
8218 config->format == AUDIO_FORMAT_DEFAULT) &&
8219 out->flags == AUDIO_OUTPUT_FLAG_NONE) {
8220 audio_format_t req_format = config->format;
8221 audio_channel_mask_t req_channel_mask = config->channel_mask;
8222 uint32_t req_sample_rate = config->sample_rate;
8223
8224 pthread_mutex_lock(&adev->lock);
8225 if (is_hdmi) {
8226 ALOGV("AUDIO_DEVICE_OUT_AUX_DIGITAL and DIRECT|OFFLOAD, check hdmi caps");
8227 ret = read_hdmi_sink_caps(out);
8228 if (config->sample_rate == 0)
8229 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
8230 if (config->channel_mask == AUDIO_CHANNEL_NONE)
8231 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
8232 if (config->format == AUDIO_FORMAT_DEFAULT)
8233 config->format = AUDIO_FORMAT_PCM_16_BIT;
8234 } else if (is_usb_dev) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008235 ret = read_usb_sup_params_and_compare(true /*is_playback*/,
8236 &config->format,
8237 &out->supported_formats[0],
8238 MAX_SUPPORTED_FORMATS,
8239 &config->channel_mask,
8240 &out->supported_channel_masks[0],
8241 MAX_SUPPORTED_CHANNEL_MASKS,
8242 &config->sample_rate,
8243 &out->supported_sample_rates[0],
8244 MAX_SUPPORTED_SAMPLE_RATES);
8245 ALOGV("plugged dev USB ret %d", ret);
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008246 }
Aalique Grahame22e49102018-12-18 14:23:57 -08008247
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008248 pthread_mutex_unlock(&adev->lock);
8249 if (ret != 0) {
Mingming Yin3a941d42016-02-17 18:08:05 -08008250 if (ret == -ENOSYS) {
8251 /* ignore and go with default */
8252 ret = 0;
Aalique Grahame22e49102018-12-18 14:23:57 -08008253 }
8254 // For MMAP NO IRQ, allow conversions in ADSP
8255 else if (is_hdmi || (flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) == 0)
8256 goto error_open;
8257 else {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008258 ALOGE("error reading direct dev sink caps");
Mingming Yin3a941d42016-02-17 18:08:05 -08008259 goto error_open;
8260 }
Aalique Grahame22e49102018-12-18 14:23:57 -08008261
8262 if (req_sample_rate != 0 && config->sample_rate != req_sample_rate)
8263 config->sample_rate = req_sample_rate;
8264 if (req_channel_mask != AUDIO_CHANNEL_NONE && config->channel_mask != req_channel_mask)
8265 config->channel_mask = req_channel_mask;
8266 if (req_format != AUDIO_FORMAT_DEFAULT && config->format != req_format)
8267 config->format = req_format;
Mingming Yin3a941d42016-02-17 18:08:05 -08008268 }
Aalique Grahame22e49102018-12-18 14:23:57 -08008269
8270 out->sample_rate = config->sample_rate;
8271 out->channel_mask = config->channel_mask;
8272 out->format = config->format;
8273 if (is_hdmi) {
8274 out->usecase = USECASE_AUDIO_PLAYBACK_HIFI;
8275 out->config = pcm_config_hdmi_multi;
8276 } else if (flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) {
8277 out->usecase = USECASE_AUDIO_PLAYBACK_MMAP;
8278 out->config = pcm_config_mmap_playback;
8279 out->stream.start = out_start;
8280 out->stream.stop = out_stop;
8281 out->stream.create_mmap_buffer = out_create_mmap_buffer;
8282 out->stream.get_mmap_position = out_get_mmap_position;
8283 } else {
8284 out->usecase = USECASE_AUDIO_PLAYBACK_HIFI;
8285 out->config = pcm_config_hifi;
8286 }
8287
8288 out->config.rate = out->sample_rate;
8289 out->config.channels = audio_channel_count_from_out_mask(out->channel_mask);
8290 if (is_hdmi) {
8291 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels *
8292 audio_bytes_per_sample(out->format));
8293 }
8294 out->config.format = pcm_format_from_audio_format(out->format);
Mingming Yin3a941d42016-02-17 18:08:05 -08008295 }
8296
Derek Chenf6318be2017-06-12 17:16:24 -04008297 /* validate bus device address */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08008298 if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_BUS)) {
Derek Chenf6318be2017-06-12 17:16:24 -04008299 /* extract car audio stream index */
8300 out->car_audio_stream =
8301 audio_extn_auto_hal_get_car_audio_stream_from_address(address);
8302 if (out->car_audio_stream < 0) {
8303 ALOGE("%s: invalid car audio stream %x",
8304 __func__, out->car_audio_stream);
8305 ret = -EINVAL;
8306 goto error_open;
8307 }
Derek Chen5f67a942020-02-24 23:08:13 -08008308 ALOGV("%s: car_audio_stream %x", __func__, out->car_audio_stream);
Derek Chenf6318be2017-06-12 17:16:24 -04008309 }
8310
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008311 /* Check for VOIP usecase */
Aniket Kumar Lata23d8cbf2019-04-10 19:54:46 -07008312 if (is_voip_rx) {
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008313 if (!voice_extn_is_compress_voip_supported()) {
8314 if (out->sample_rate == 8000 || out->sample_rate == 16000 ||
8315 out->sample_rate == 32000 || out->sample_rate == 48000) {
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07008316 out->channel_mask = audio_extn_utils_is_vendor_enhanced_fwk() ?
Lakshman Chaluvarajue7fc9482020-05-30 14:29:29 +05308317 config->channel_mask : AUDIO_CHANNEL_OUT_STEREO;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008318 out->usecase = USECASE_AUDIO_PLAYBACK_VOIP;
8319 out->format = AUDIO_FORMAT_PCM_16_BIT;
Aniket Kumar Lata8426d1f2019-06-10 15:25:53 -07008320 out->volume_l = INVALID_OUT_VOLUME;
8321 out->volume_r = INVALID_OUT_VOLUME;
Vikram Panduranga93f080e2017-06-07 18:16:14 -07008322
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008323 out->config = default_pcm_config_voip_copp;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008324 out->config.rate = out->sample_rate;
Aniket Kumar Lata1e344f22019-06-19 17:09:01 -07008325 uint32_t channel_count =
8326 audio_channel_count_from_out_mask(out->channel_mask);
Ramjee Singh968858a2020-08-06 16:30:48 +05308327 out->config.channels = channel_count;
8328
Aniket Kumar Lata1e344f22019-06-19 17:09:01 -07008329 uint32_t buffer_size = get_stream_buffer_size(DEFAULT_VOIP_BUF_DURATION_MS,
8330 out->sample_rate, out->format,
8331 channel_count, false);
8332 uint32_t frame_size = audio_bytes_per_sample(out->format) * channel_count;
8333 if (frame_size != 0)
8334 out->config.period_size = buffer_size / frame_size;
8335 else
8336 ALOGW("%s: frame size is 0 for format %#x", __func__, out->format);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008337 }
8338 } else {
8339 if ((out->dev->mode == AUDIO_MODE_IN_COMMUNICATION ||
8340 voice_extn_compress_voip_is_active(out->dev)) &&
8341 (voice_extn_compress_voip_is_config_supported(config))) {
8342 ret = voice_extn_compress_voip_open_output_stream(out);
8343 if (ret != 0) {
8344 ALOGE("%s: Compress voip output cannot be opened, error:%d",
8345 __func__, ret);
8346 goto error_open;
8347 }
Sujin Panicker19027262019-09-16 18:28:06 +05308348 } else {
8349 out->usecase = GET_USECASE_AUDIO_PLAYBACK_PRIMARY(use_db_as_primary);
8350 out->config = GET_PCM_CONFIG_AUDIO_PLAYBACK_PRIMARY(use_db_as_primary);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008351 }
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08008352 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008353 } else if (audio_is_linear_pcm(out->format) &&
8354 out->flags == AUDIO_OUTPUT_FLAG_NONE && is_usb_dev) {
8355 out->channel_mask = config->channel_mask;
8356 out->sample_rate = config->sample_rate;
8357 out->format = config->format;
8358 out->usecase = USECASE_AUDIO_PLAYBACK_HIFI;
8359 // does this change?
8360 out->config = is_hdmi ? pcm_config_hdmi_multi : pcm_config_hifi;
8361 out->config.rate = config->sample_rate;
8362 out->config.channels = audio_channel_count_from_out_mask(out->channel_mask);
8363 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels *
8364 audio_bytes_per_sample(config->format));
8365 out->config.format = pcm_format_from_audio_format(out->format);
vivek mehta0ea887a2015-08-26 14:01:20 -07008366 } else if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
Dhananjay Kumarac341582017-02-23 23:42:25 +05308367 (out->flags == AUDIO_OUTPUT_FLAG_DIRECT)) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05308368 pthread_mutex_lock(&adev->lock);
8369 bool offline = (adev->card_status == CARD_STATUS_OFFLINE);
8370 pthread_mutex_unlock(&adev->lock);
8371
8372 // reject offload during card offline to allow
8373 // fallback to s/w paths
8374 if (offline) {
8375 ret = -ENODEV;
8376 goto error_open;
8377 }
vivek mehta0ea887a2015-08-26 14:01:20 -07008378
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008379 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
8380 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
8381 ALOGE("%s: Unsupported Offload information", __func__);
8382 ret = -EINVAL;
8383 goto error_open;
8384 }
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07008385
Atul Khare3fa6e542017-08-09 00:56:17 +05308386 if (config->offload_info.format == 0)
8387 config->offload_info.format = config->format;
8388 if (config->offload_info.sample_rate == 0)
8389 config->offload_info.sample_rate = config->sample_rate;
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07008390
Mingming Yin90310102013-11-13 16:57:00 -08008391 if (!is_supported_format(config->offload_info.format) &&
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05308392 !audio_extn_passthru_is_supported_format(config->offload_info.format)) {
vivek mehta0ea887a2015-08-26 14:01:20 -07008393 ALOGE("%s: Unsupported audio format %x " , __func__, config->offload_info.format);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008394 ret = -EINVAL;
8395 goto error_open;
8396 }
8397
Ben Romberger0f8c87b2017-05-24 17:41:11 -07008398 /* TrueHD only supported for 48k multiples (48k, 96k, 192k) */
8399 if ((config->offload_info.format == AUDIO_FORMAT_DOLBY_TRUEHD) &&
8400 (audio_extn_passthru_is_passthrough_stream(out)) &&
8401 !((config->sample_rate == 48000) ||
8402 (config->sample_rate == 96000) ||
8403 (config->sample_rate == 192000))) {
8404 ALOGE("%s: Unsupported sample rate %d for audio format %x",
8405 __func__, config->sample_rate, config->offload_info.format);
8406 ret = -EINVAL;
8407 goto error_open;
8408 }
8409
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008410 out->compr_config.codec = (struct snd_codec *)
8411 calloc(1, sizeof(struct snd_codec));
8412
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07008413 if (!out->compr_config.codec) {
8414 ret = -ENOMEM;
8415 goto error_open;
8416 }
8417
Dhananjay Kumarac341582017-02-23 23:42:25 +05308418 out->stream.pause = out_pause;
8419 out->stream.resume = out_resume;
8420 out->stream.flush = out_flush;
Ashish Jain4847e9d2017-08-17 19:16:57 +05308421 out->stream.set_callback = out_set_callback;
Dhananjay Kumarac341582017-02-23 23:42:25 +05308422 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Mingming Yin21d60472015-09-30 13:56:25 -07008423 out->stream.drain = out_drain;
Dhananjay Kumarac341582017-02-23 23:42:25 +05308424 out->usecase = get_offload_usecase(adev, true /* is_compress */);
vivek mehta446c3962015-09-14 10:57:35 -07008425 ALOGV("Compress Offload usecase .. usecase selected %d", out->usecase);
Dhananjay Kumarac341582017-02-23 23:42:25 +05308426 } else {
8427 out->usecase = get_offload_usecase(adev, false /* is_compress */);
8428 ALOGV("non-offload DIRECT_usecase ... usecase selected %d ", out->usecase);
vivek mehta0ea887a2015-08-26 14:01:20 -07008429 }
vivek mehta446c3962015-09-14 10:57:35 -07008430
Deeraj Somanfa377bf2019-02-06 12:57:59 +05308431 if (out->flags & AUDIO_OUTPUT_FLAG_FAST) {
8432 ALOGD("%s: Setting latency mode to true", __func__);
Meng Wang4c32fb42020-01-16 17:57:11 +08008433#ifdef AUDIO_GKI_ENABLED
8434 /* out->compr_config.codec->reserved[1] is for flags */
8435 out->compr_config.codec->reserved[1] |= audio_extn_utils_get_perf_mode_flag();
8436#else
Deeraj Somanfa377bf2019-02-06 12:57:59 +05308437 out->compr_config.codec->flags |= audio_extn_utils_get_perf_mode_flag();
Meng Wang4c32fb42020-01-16 17:57:11 +08008438#endif
Deeraj Somanfa377bf2019-02-06 12:57:59 +05308439 }
8440
vivek mehta446c3962015-09-14 10:57:35 -07008441 if (out->usecase == USECASE_INVALID) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08008442 if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_AUX_DIGITAL) &&
Mingming Yin3a941d42016-02-17 18:08:05 -08008443 config->format == 0 && config->sample_rate == 0 &&
8444 config->channel_mask == 0) {
Mingming Yin21854652016-04-13 11:54:02 -07008445 ALOGI("%s dummy open to query sink capability",__func__);
Mingming Yin3a941d42016-02-17 18:08:05 -08008446 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
8447 } else {
8448 ALOGE("%s, Max allowed OFFLOAD usecase reached ... ", __func__);
8449 ret = -EEXIST;
8450 goto error_open;
8451 }
vivek mehta446c3962015-09-14 10:57:35 -07008452 }
8453
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008454 if (config->offload_info.channel_mask)
8455 out->channel_mask = config->offload_info.channel_mask;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08008456 else if (config->channel_mask) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008457 out->channel_mask = config->channel_mask;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08008458 config->offload_info.channel_mask = config->channel_mask;
Haynes Mathew Georgea99f7532016-08-24 16:01:21 -07008459 } else {
Dhananjay Kumarac341582017-02-23 23:42:25 +05308460 ALOGE("out->channel_mask not set for OFFLOAD/DIRECT usecase");
Haynes Mathew Georgea99f7532016-08-24 16:01:21 -07008461 ret = -EINVAL;
8462 goto error_open;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08008463 }
Haynes Mathew Georgea99f7532016-08-24 16:01:21 -07008464
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07008465 format = out->format = config->offload_info.format;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008466 out->sample_rate = config->offload_info.sample_rate;
8467
Mingming Yin3ee55c62014-08-04 14:23:35 -07008468 out->bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008469
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05308470 out->compr_config.codec->id = get_snd_codec_id(config->offload_info.format);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05308471 if (audio_extn_utils_is_dolby_format(config->offload_info.format)) {
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05308472 audio_extn_dolby_send_ddp_endp_params(adev);
8473 audio_extn_dolby_set_dmid(adev);
8474 }
vivek mehta0ea887a2015-08-26 14:01:20 -07008475
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008476 out->compr_config.codec->sample_rate =
Ravi Kumar Alamandab91bff32014-11-14 12:05:54 -08008477 config->offload_info.sample_rate;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008478 out->compr_config.codec->bit_rate =
8479 config->offload_info.bit_rate;
8480 out->compr_config.codec->ch_in =
Dhanalakshmi Siddania15c6792016-08-10 15:33:53 +05308481 audio_channel_count_from_out_mask(out->channel_mask);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008482 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
Satish Babu Patakokilaa395a9e2016-11-01 12:18:49 +05308483 /* Update bit width only for non passthrough usecases.
8484 * For passthrough usecases, the output will always be opened @16 bit
8485 */
8486 if (!audio_extn_passthru_is_passthrough_stream(out))
8487 out->bit_width = AUDIO_OUTPUT_BIT_WIDTH;
Naresh Tanniruee3499a2017-01-05 14:05:35 +05308488
8489 if (out->flags & AUDIO_OUTPUT_FLAG_TIMESTAMP)
Meng Wang4c32fb42020-01-16 17:57:11 +08008490#ifdef AUDIO_GKI_ENABLED
8491 /* out->compr_config.codec->reserved[1] is for flags */
8492 out->compr_config.codec->reserved[1] |= COMPRESSED_TIMESTAMP_FLAG;
8493 ALOGVV("%s : out->compr_config.codec->flags -> (%#x) ", __func__, out->compr_config.codec->reserved[1]);
8494#else
Naresh Tanniruee3499a2017-01-05 14:05:35 +05308495 out->compr_config.codec->flags |= COMPRESSED_TIMESTAMP_FLAG;
8496 ALOGVV("%s : out->compr_config.codec->flags -> (%#x) ", __func__, out->compr_config.codec->flags);
Meng Wang4c32fb42020-01-16 17:57:11 +08008497#endif
Naresh Tanniruee3499a2017-01-05 14:05:35 +05308498
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07008499 /*TODO: Do we need to change it for passthrough */
8500 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_RAW;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008501
Manish Dewangana6fc5442015-08-24 20:30:31 +05308502 if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC)
8503 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_RAW;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05308504 else if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS)
Manish Dewangana6fc5442015-08-24 20:30:31 +05308505 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_MP4ADTS;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05308506 else if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_LATM)
8507 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_MP4LATM;
Ashish Jainf1eaa582016-05-23 20:54:24 +05308508
8509 if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) ==
8510 AUDIO_FORMAT_PCM) {
8511
8512 /*Based on platform support, configure appropriate alsa format for corresponding
8513 *hal input format.
8514 */
8515 out->compr_config.codec->format = hal_format_to_alsa(
8516 config->offload_info.format);
8517
Ashish Jain83a6cc22016-06-28 14:34:17 +05308518 out->hal_op_format = alsa_format_to_hal(
Ashish Jainf1eaa582016-05-23 20:54:24 +05308519 out->compr_config.codec->format);
Ashish Jain83a6cc22016-06-28 14:34:17 +05308520 out->hal_ip_format = out->format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05308521
Dhananjay Kumarac341582017-02-23 23:42:25 +05308522 /*for direct non-compress playback populate bit_width based on selected alsa format as
Ashish Jainf1eaa582016-05-23 20:54:24 +05308523 *hal input format and alsa format might differ based on platform support.
8524 */
8525 out->bit_width = audio_bytes_per_sample(
Ashish Jain83a6cc22016-06-28 14:34:17 +05308526 out->hal_op_format) << 3;
Ashish Jainf1eaa582016-05-23 20:54:24 +05308527
8528 out->compr_config.fragments = DIRECT_PCM_NUM_FRAGMENTS;
8529
Deeraj Soman93155a62019-09-30 19:00:37 +05308530 if (property_get_bool("vendor.audio.offload.buffer.duration.enabled", false)) {
8531 if ((config->offload_info.duration_us >= MIN_OFFLOAD_BUFFER_DURATION_MS * 1000) &&
8532 (config->offload_info.duration_us <= MAX_OFFLOAD_BUFFER_DURATION_MS * 1000))
8533 out->info.duration_us = (int64_t)config->offload_info.duration_us;
8534 }
Deeraj Soman65358ab2019-02-07 15:40:49 +05308535
Ashish Jainf1eaa582016-05-23 20:54:24 +05308536 /* Check if alsa session is configured with the same format as HAL input format,
8537 * if not then derive correct fragment size needed to accomodate the
8538 * conversion of HAL input format to alsa format.
8539 */
8540 audio_extn_utils_update_direct_pcm_fragment_size(out);
8541
8542 /*if hal input and output fragment size is different this indicates HAL input format is
8543 *not same as the alsa format
8544 */
Ashish Jain83a6cc22016-06-28 14:34:17 +05308545 if (out->hal_fragment_size != out->compr_config.fragment_size) {
Ashish Jainf1eaa582016-05-23 20:54:24 +05308546 /*Allocate a buffer to convert input data to the alsa configured format.
8547 *size of convert buffer is equal to the size required to hold one fragment size
8548 *worth of pcm data, this is because flinger does not write more than fragment_size
8549 */
Ashish Jain83a6cc22016-06-28 14:34:17 +05308550 out->convert_buffer = calloc(1,out->compr_config.fragment_size);
8551 if (out->convert_buffer == NULL){
Ashish Jainf1eaa582016-05-23 20:54:24 +05308552 ALOGE("Allocation failed for convert buffer for size %d", out->compr_config.fragment_size);
8553 ret = -ENOMEM;
8554 goto error_open;
8555 }
8556 }
8557 } else if (audio_extn_passthru_is_passthrough_stream(out)) {
8558 out->compr_config.fragment_size =
8559 audio_extn_passthru_get_buffer_size(&config->offload_info);
8560 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
8561 } else {
8562 out->compr_config.fragment_size =
8563 platform_get_compress_offload_buffer_size(&config->offload_info);
8564 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
8565 }
Mingming Yin3ee55c62014-08-04 14:23:35 -07008566
Naresh Tanniruee3499a2017-01-05 14:05:35 +05308567 if (out->flags & AUDIO_OUTPUT_FLAG_TIMESTAMP) {
8568 out->compr_config.fragment_size += sizeof(struct snd_codec_metadata);
8569 }
Xiaojun Sang782e5b12020-06-29 21:13:06 +08008570 if (config->offload_info.format == AUDIO_FORMAT_FLAC) {
8571#ifdef AUDIO_GKI_ENABLED
8572 generic_dec =
8573 &(out->compr_config.codec->options.generic.reserved[1]);
8574 ((struct snd_generic_dec_flac *)generic_dec)->sample_size =
8575 AUDIO_OUTPUT_BIT_WIDTH;
8576#else
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +05308577 out->compr_config.codec->options.flac_dec.sample_size = AUDIO_OUTPUT_BIT_WIDTH;
Xiaojun Sang782e5b12020-06-29 21:13:06 +08008578#endif
8579 }
Mingming Yin3ee55c62014-08-04 14:23:35 -07008580
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05308581 if (config->offload_info.format == AUDIO_FORMAT_APTX) {
8582 audio_extn_send_aptx_dec_bt_addr_to_dsp(out);
8583 }
8584
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008585 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
8586 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07008587
Manish Dewangan69426c82017-01-30 17:35:36 +05308588 if ((flags & AUDIO_OUTPUT_FLAG_TIMESTAMP) &&
8589 (flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC)) {
8590 out->render_mode = RENDER_MODE_AUDIO_STC_MASTER;
8591 } else if(flags & AUDIO_OUTPUT_FLAG_TIMESTAMP) {
8592 out->render_mode = RENDER_MODE_AUDIO_MASTER;
8593 } else {
8594 out->render_mode = RENDER_MODE_AUDIO_NO_TIMESTAMP;
8595 }
Alexy Josephaa54c872014-12-03 02:46:47 -08008596
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05308597 memset(&out->channel_map_param, 0,
8598 sizeof(struct audio_out_channel_map_param));
8599
Haynes Mathew George352f27b2013-07-26 00:00:15 -07008600 out->send_new_metadata = 1;
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05308601 out->send_next_track_params = false;
8602 out->is_compr_metadata_avail = false;
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08008603 out->offload_state = OFFLOAD_STATE_IDLE;
8604 out->playback_started = 0;
Zhou Song48453a02018-01-10 17:50:59 +08008605 out->writeAt.tv_sec = 0;
8606 out->writeAt.tv_nsec = 0;
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08008607
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08008608 audio_extn_dts_create_state_notifier_node(out->usecase);
8609
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008610 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
8611 __func__, config->offload_info.version,
8612 config->offload_info.bit_rate);
Ashish Jain5106d362016-05-11 19:23:33 +05308613
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05308614 /* Check if DSD audio format is supported in codec
8615 * and there is no active native DSD use case
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05308616 */
8617
8618 if ((config->format == AUDIO_FORMAT_DSD) &&
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05308619 (!platform_check_codec_dsd_support(adev->platform) ||
8620 audio_is_dsd_native_stream_active(adev))) {
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05308621 ret = -EINVAL;
8622 goto error_open;
8623 }
8624
Ashish Jain5106d362016-05-11 19:23:33 +05308625 /* Disable gapless if any of the following is true
8626 * passthrough playback
8627 * AV playback
Dhananjay Kumarac341582017-02-23 23:42:25 +05308628 * non compressed Direct playback
Ashish Jain5106d362016-05-11 19:23:33 +05308629 */
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05308630 if (audio_extn_passthru_is_passthrough_stream(out) ||
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05308631 (config->format == AUDIO_FORMAT_DSD) ||
Naresh Tanniru928f0862017-04-07 16:44:23 -07008632 (config->format == AUDIO_FORMAT_IEC61937) ||
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05308633 config->offload_info.has_video ||
Dhananjay Kumarac341582017-02-23 23:42:25 +05308634 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Ashish Jain5106d362016-05-11 19:23:33 +05308635 check_and_set_gapless_mode(adev, false);
8636 } else
8637 check_and_set_gapless_mode(adev, true);
Mingming Yin21854652016-04-13 11:54:02 -07008638
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05308639 if (audio_extn_passthru_is_passthrough_stream(out)) {
Mingming Yin21854652016-04-13 11:54:02 -07008640 out->flags |= AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH;
8641 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05308642 if (config->format == AUDIO_FORMAT_DSD) {
8643 out->flags |= AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH;
Meng Wang4c32fb42020-01-16 17:57:11 +08008644#ifdef AUDIO_GKI_ENABLED
8645 /* out->compr_config.codec->reserved[0] is for compr_passthr */
8646 out->compr_config.codec->reserved[0] = PASSTHROUGH_DSD;
8647#else
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05308648 out->compr_config.codec->compr_passthr = PASSTHROUGH_DSD;
Meng Wang4c32fb42020-01-16 17:57:11 +08008649#endif
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05308650 }
Aalique Grahame0359a1f2016-09-08 16:54:22 -07008651
8652 create_offload_callback_thread(out);
8653
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07008654 } else if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
Arun Mirpuri7da752a2018-09-11 18:01:15 -07008655 switch (config->sample_rate) {
8656 case 0:
8657 out->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
8658 break;
8659 case 8000:
8660 case 16000:
8661 case 48000:
8662 out->sample_rate = config->sample_rate;
8663 break;
8664 default:
8665 ALOGE("%s: Unsupported sampling rate %d for Incall Music", __func__,
8666 config->sample_rate);
8667 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
8668 ret = -EINVAL;
8669 goto error_open;
8670 }
8671 //FIXME: add support for MONO stream configuration when audioflinger mixer supports it
8672 switch (config->channel_mask) {
8673 case AUDIO_CHANNEL_NONE:
8674 case AUDIO_CHANNEL_OUT_STEREO:
8675 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
8676 break;
8677 default:
8678 ALOGE("%s: Unsupported channel mask %#x for Incall Music", __func__,
8679 config->channel_mask);
8680 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
8681 ret = -EINVAL;
8682 goto error_open;
8683 }
8684 switch (config->format) {
8685 case AUDIO_FORMAT_DEFAULT:
8686 case AUDIO_FORMAT_PCM_16_BIT:
8687 out->format = AUDIO_FORMAT_PCM_16_BIT;
8688 break;
8689 default:
8690 ALOGE("%s: Unsupported format %#x for Incall Music", __func__,
8691 config->format);
8692 config->format = AUDIO_FORMAT_PCM_16_BIT;
8693 ret = -EINVAL;
8694 goto error_open;
8695 }
8696
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05308697 ret = voice_extn_check_and_set_incall_music_usecase(adev, out);
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07008698 if (ret != 0) {
8699 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
Arun Mirpuri7da752a2018-09-11 18:01:15 -07008700 __func__, ret);
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07008701 goto error_open;
8702 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08008703 } else if (is_single_device_type_equal(&out->device_list,
8704 AUDIO_DEVICE_OUT_TELEPHONY_TX)) {
Aalique Grahame22e49102018-12-18 14:23:57 -08008705 switch (config->sample_rate) {
8706 case 0:
8707 out->sample_rate = AFE_PROXY_SAMPLING_RATE;
8708 break;
8709 case 8000:
8710 case 16000:
8711 case 48000:
8712 out->sample_rate = config->sample_rate;
8713 break;
8714 default:
8715 ALOGE("%s: Unsupported sampling rate %d for Telephony TX", __func__,
8716 config->sample_rate);
8717 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
8718 ret = -EINVAL;
8719 break;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07008720 }
Aalique Grahame22e49102018-12-18 14:23:57 -08008721 //FIXME: add support for MONO stream configuration when audioflinger mixer supports it
8722 switch (config->channel_mask) {
8723 case AUDIO_CHANNEL_NONE:
8724 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
8725 break;
8726 case AUDIO_CHANNEL_OUT_STEREO:
8727 out->channel_mask = config->channel_mask;
8728 break;
8729 default:
8730 ALOGE("%s: Unsupported channel mask %#x for Telephony TX", __func__,
8731 config->channel_mask);
8732 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
8733 ret = -EINVAL;
8734 break;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07008735 }
Aalique Grahame22e49102018-12-18 14:23:57 -08008736 switch (config->format) {
8737 case AUDIO_FORMAT_DEFAULT:
8738 out->format = AUDIO_FORMAT_PCM_16_BIT;
8739 break;
8740 case AUDIO_FORMAT_PCM_16_BIT:
8741 out->format = config->format;
8742 break;
8743 default:
8744 ALOGE("%s: Unsupported format %#x for Telephony TX", __func__,
8745 config->format);
8746 config->format = AUDIO_FORMAT_PCM_16_BIT;
8747 ret = -EINVAL;
8748 break;
8749 }
8750 if (ret != 0)
8751 goto error_open;
8752
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07008753 out->usecase = USECASE_AUDIO_PLAYBACK_AFE_PROXY;
8754 out->config = pcm_config_afe_proxy_playback;
Aalique Grahame22e49102018-12-18 14:23:57 -08008755 out->config.rate = out->sample_rate;
8756 out->config.channels =
8757 audio_channel_count_from_out_mask(out->channel_mask);
8758 out->config.format = pcm_format_from_audio_format(out->format);
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07008759 adev->voice_tx_output = out;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07008760 } else {
Ashish Jain058165c2016-09-28 23:18:48 +05308761 unsigned int channels = 0;
8762 /*Update config params to default if not set by the caller*/
8763 if (config->sample_rate == 0)
8764 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
8765 if (config->channel_mask == AUDIO_CHANNEL_NONE)
8766 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
8767 if (config->format == AUDIO_FORMAT_DEFAULT)
8768 config->format = AUDIO_FORMAT_PCM_16_BIT;
8769
8770 channels = audio_channel_count_from_out_mask(out->channel_mask);
8771
Varun Balaraje49253e2017-07-06 19:48:56 +05308772 if (out->flags & AUDIO_OUTPUT_FLAG_INTERACTIVE) {
8773 out->usecase = get_interactive_usecase(adev);
8774 out->config = pcm_config_low_latency;
8775 } else if (out->flags & AUDIO_OUTPUT_FLAG_RAW) {
Ashish Jain83a6cc22016-06-28 14:34:17 +05308776 out->usecase = USECASE_AUDIO_PLAYBACK_ULL;
Haynes Mathew George5beddd42016-06-27 18:33:40 -07008777 out->realtime = may_use_noirq_mode(adev, USECASE_AUDIO_PLAYBACK_ULL,
8778 out->flags);
8779 out->config = out->realtime ? pcm_config_rt : pcm_config_low_latency;
Haynes Mathew George16081042017-05-31 17:16:49 -07008780 } else if (out->flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) {
8781 out->usecase = USECASE_AUDIO_PLAYBACK_MMAP;
8782 out->config = pcm_config_mmap_playback;
8783 out->stream.start = out_start;
8784 out->stream.stop = out_stop;
8785 out->stream.create_mmap_buffer = out_create_mmap_buffer;
8786 out->stream.get_mmap_position = out_get_mmap_position;
Ashish Jain83a6cc22016-06-28 14:34:17 +05308787 } else if (out->flags & AUDIO_OUTPUT_FLAG_FAST) {
8788 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
Aniket Kumar Lata932f4872017-11-06 18:29:44 -08008789 out->hal_output_suspend_supported =
8790 property_get_bool("vendor.audio.hal.output.suspend.supported", false);
8791 out->dynamic_pm_qos_config_supported =
8792 property_get_bool("vendor.audio.hal.dynamic.qos.config.supported", false);
8793 if (!out->dynamic_pm_qos_config_supported) {
Alexy Joseph98988832017-01-13 14:56:59 -08008794 ALOGI("%s: dynamic qos voting not enabled for platform", __func__);
8795 } else {
8796 ALOGI("%s: dynamic qos voting enabled for platform", __func__);
8797 //the mixer path will be a string similar to "low-latency-playback resume"
8798 strlcpy(out->pm_qos_mixer_path, use_case_table[out->usecase], MAX_MIXER_PATH_LEN);
8799 strlcat(out->pm_qos_mixer_path,
8800 " resume", MAX_MIXER_PATH_LEN);
8801 ALOGI("%s: created %s pm_qos_mixer_path" , __func__,
8802 out->pm_qos_mixer_path);
8803 }
Ashish Jain83a6cc22016-06-28 14:34:17 +05308804 out->config = pcm_config_low_latency;
8805 } else if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
8806 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
8807 out->config = pcm_config_deep_buffer;
Ashish Jain058165c2016-09-28 23:18:48 +05308808 out->config.period_size = get_output_period_size(config->sample_rate, out->format,
8809 channels, DEEP_BUFFER_OUTPUT_PERIOD_DURATION);
8810 if (out->config.period_size <= 0) {
8811 ALOGE("Invalid configuration period size is not valid");
8812 ret = -EINVAL;
8813 goto error_open;
8814 }
Aalique Grahame22e49102018-12-18 14:23:57 -08008815 } else if (flags & AUDIO_OUTPUT_FLAG_TTS) {
8816 out->usecase = USECASE_AUDIO_PLAYBACK_TTS;
8817 out->config = pcm_config_deep_buffer;
Vignesh Kulothungana6927272019-02-20 15:17:07 -08008818 } else if (config->channel_mask & AUDIO_CHANNEL_HAPTIC_ALL) {
8819 out->usecase = USECASE_AUDIO_PLAYBACK_WITH_HAPTICS;
8820 out->config = pcm_config_haptics_audio;
8821 if (force_haptic_path)
8822 adev->haptics_config = pcm_config_haptics_audio;
8823 else
8824 adev->haptics_config = pcm_config_haptics;
8825
Meng Wangd08ce322020-04-02 08:59:20 +08008826 channels =
Vignesh Kulothungana6927272019-02-20 15:17:07 -08008827 audio_channel_count_from_out_mask(out->channel_mask & ~AUDIO_CHANNEL_HAPTIC_ALL);
8828
8829 if (force_haptic_path) {
8830 out->config.channels = 1;
8831 adev->haptics_config.channels = 1;
8832 } else
8833 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 -08008834 } else if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_BUS)) {
Derek Chenf6318be2017-06-12 17:16:24 -04008835 ret = audio_extn_auto_hal_open_output_stream(out);
8836 if (ret) {
8837 ALOGE("%s: Failed to open output stream for bus device", __func__);
8838 ret = -EINVAL;
8839 goto error_open;
8840 }
Ashish Jain83a6cc22016-06-28 14:34:17 +05308841 } else {
8842 /* primary path is the default path selected if no other outputs are available/suitable */
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008843 out->usecase = GET_USECASE_AUDIO_PLAYBACK_PRIMARY(use_db_as_primary);
8844 out->config = GET_PCM_CONFIG_AUDIO_PLAYBACK_PRIMARY(use_db_as_primary);
Ashish Jain83a6cc22016-06-28 14:34:17 +05308845 }
8846 out->hal_ip_format = format = out->format;
8847 out->config.format = hal_format_to_pcm(out->hal_ip_format);
8848 out->hal_op_format = pcm_format_to_hal(out->config.format);
8849 out->bit_width = format_to_bitwidth_table[out->hal_op_format] << 3;
8850 out->config.rate = config->sample_rate;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07008851 out->sample_rate = out->config.rate;
Ashish Jain058165c2016-09-28 23:18:48 +05308852 out->config.channels = channels;
Ashish Jain83a6cc22016-06-28 14:34:17 +05308853 if (out->hal_ip_format != out->hal_op_format) {
8854 uint32_t buffer_size = out->config.period_size *
8855 format_to_bitwidth_table[out->hal_op_format] *
8856 out->config.channels;
8857 out->convert_buffer = calloc(1, buffer_size);
8858 if (out->convert_buffer == NULL){
8859 ALOGE("Allocation failed for convert buffer for size %d",
8860 out->compr_config.fragment_size);
8861 ret = -ENOMEM;
8862 goto error_open;
8863 }
8864 ALOGD("Convert buffer allocated of size %d", buffer_size);
8865 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008866 }
8867
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08008868 ALOGV("%s devices:%d, format:%x, out->sample_rate:%d,out->bit_width:%d out->format:%d out->flags:%x, flags: %x usecase %d",
8869 __func__, devices, format, out->sample_rate, out->bit_width, out->format, out->flags, flags, out->usecase);
Ashish Jain83a6cc22016-06-28 14:34:17 +05308870
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07008871 /* TODO remove this hardcoding and check why width is zero*/
8872 if (out->bit_width == 0)
8873 out->bit_width = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05308874 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07008875 &adev->streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08008876 &out->device_list, out->flags,
8877 out->hal_op_format, out->sample_rate,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +05308878 out->bit_width, out->channel_mask, out->profile,
Manish Dewangan837dc462015-05-27 10:17:41 +05308879 &out->app_type_cfg);
Aalique Grahame6e763712019-01-31 16:18:17 -08008880 if ((out->usecase == (audio_usecase_t)(GET_USECASE_AUDIO_PLAYBACK_PRIMARY(use_db_as_primary))) ||
Haynes Mathew Georgebf143712013-12-03 13:02:53 -08008881 (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
8882 /* Ensure the default output is not selected twice */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08008883 if(adev->primary_output == NULL)
8884 adev->primary_output = out;
8885 else {
8886 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07008887 ret = -EEXIST;
8888 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08008889 }
8890 }
8891
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008892 /* Check if this usecase is already existing */
8893 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella7ce05352014-04-17 20:00:41 -07008894 if ((get_usecase_from_list(adev, out->usecase) != NULL) &&
8895 (out->usecase != USECASE_COMPRESS_VOIP_CALL)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008896 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008897 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07008898 ret = -EEXIST;
8899 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008900 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08008901
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008902 pthread_mutex_unlock(&adev->lock);
8903
8904 out->stream.common.get_sample_rate = out_get_sample_rate;
8905 out->stream.common.set_sample_rate = out_set_sample_rate;
8906 out->stream.common.get_buffer_size = out_get_buffer_size;
8907 out->stream.common.get_channels = out_get_channels;
8908 out->stream.common.get_format = out_get_format;
8909 out->stream.common.set_format = out_set_format;
8910 out->stream.common.standby = out_standby;
8911 out->stream.common.dump = out_dump;
8912 out->stream.common.set_parameters = out_set_parameters;
8913 out->stream.common.get_parameters = out_get_parameters;
8914 out->stream.common.add_audio_effect = out_add_audio_effect;
8915 out->stream.common.remove_audio_effect = out_remove_audio_effect;
8916 out->stream.get_latency = out_get_latency;
8917 out->stream.set_volume = out_set_volume;
Aalique Grahame22e49102018-12-18 14:23:57 -08008918#ifdef NO_AUDIO_OUT
8919 out->stream.write = out_write_for_no_output;
8920#else
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008921 out->stream.write = out_write;
Aalique Grahame22e49102018-12-18 14:23:57 -08008922#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008923 out->stream.get_render_position = out_get_render_position;
8924 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07008925 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008926
Haynes Mathew George16081042017-05-31 17:16:49 -07008927 if (out->realtime)
8928 out->af_period_multiplier = af_period_multiplier;
8929 else
8930 out->af_period_multiplier = 1;
8931
Andy Hunga1f48fa2019-07-01 18:14:53 -07008932 out->kernel_buffer_size = out->config.period_size * out->config.period_count;
8933
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008934 out->standby = 1;
Zhou Songbaddf9f2020-11-20 13:57:39 +08008935 out->volume_l = PLAYBACK_GAIN_MAX;
8936 out->volume_r = PLAYBACK_GAIN_MAX;
Eric Laurenta9024de2013-04-04 09:19:12 -07008937 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07008938 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008939
8940 config->format = out->stream.common.get_format(&out->stream.common);
8941 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
8942 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
Naresh Tanniru04f71882018-06-26 17:46:22 +05308943 register_format(out->format, out->supported_formats);
8944 register_channel_mask(out->channel_mask, out->supported_channel_masks);
8945 register_sample_rate(out->sample_rate, out->supported_sample_rates);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008946
Dechen Chai22768452021-07-30 09:29:16 +05308947#ifndef LINUX_ENABLED
Aalique Grahame22e49102018-12-18 14:23:57 -08008948 out->error_log = error_log_create(
8949 ERROR_LOG_ENTRIES,
8950 1000000000 /* aggregate consecutive identical errors within one second in ns */);
Dechen Chai22768452021-07-30 09:29:16 +05308951#endif
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05308952 /*
8953 By locking output stream before registering, we allow the callback
8954 to update stream's state only after stream's initial state is set to
8955 adev state.
8956 */
8957 lock_output_stream(out);
8958 audio_extn_snd_mon_register_listener(out, out_snd_mon_cb);
8959 pthread_mutex_lock(&adev->lock);
8960 out->card_status = adev->card_status;
8961 pthread_mutex_unlock(&adev->lock);
8962 pthread_mutex_unlock(&out->lock);
8963
Aalique Grahame22e49102018-12-18 14:23:57 -08008964 stream_app_type_cfg_init(&out->app_type_cfg);
8965
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008966 *stream_out = &out->stream;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05308967 ALOGD("%s: Stream (%p) picks up usecase (%s)", __func__, &out->stream,
vivek mehta0ea887a2015-08-26 14:01:20 -07008968 use_case_table[out->usecase]);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08008969
8970 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
8971 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
8972 popcount(out->channel_mask), out->playback_started);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08008973 /* setup a channel for client <--> adsp communication for stream events */
Manish Dewangan21a850a2017-08-14 12:03:55 +05308974 is_direct_passthough = audio_extn_passthru_is_direct_passthrough(out);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08008975 if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
Naresh Tanniru85819452017-05-04 18:55:45 -07008976 (out->flags & AUDIO_OUTPUT_FLAG_DIRECT_PCM) ||
Gangadhar Sb0210342019-02-22 17:39:41 +05308977 audio_extn_ip_hdlr_intf_supported_for_copp(adev->platform) ||
8978 (audio_extn_ip_hdlr_intf_supported(config->format, is_direct_passthough, false))) {
Ben Rombergerd771a7c2017-02-22 18:05:17 -08008979 hdlr_stream_cfg.pcm_device_id = platform_get_pcm_device_id(
8980 out->usecase, PCM_PLAYBACK);
8981 hdlr_stream_cfg.flags = out->flags;
8982 hdlr_stream_cfg.type = PCM_PLAYBACK;
8983 ret = audio_extn_adsp_hdlr_stream_open(&out->adsp_hdlr_stream_handle,
8984 &hdlr_stream_cfg);
8985 if (ret) {
8986 ALOGE("%s: adsp_hdlr_stream_open failed %d",__func__, ret);
8987 out->adsp_hdlr_stream_handle = NULL;
8988 }
8989 }
Gangadhar Sb0210342019-02-22 17:39:41 +05308990 ip_hdlr_stream = audio_extn_ip_hdlr_intf_supported(config->format,
8991 is_direct_passthough, false);
8992 ip_hdlr_dev = audio_extn_ip_hdlr_intf_supported_for_copp(adev->platform);
8993 if (ip_hdlr_stream || ip_hdlr_dev ) {
Vidyakumar Athota2062f912017-06-27 14:46:15 -07008994 ret = audio_extn_ip_hdlr_intf_init(&out->ip_hdlr_handle, NULL, NULL, adev, out->usecase);
Naresh Tanniru85819452017-05-04 18:55:45 -07008995 if (ret < 0) {
8996 ALOGE("%s: audio_extn_ip_hdlr_intf_init failed %d",__func__, ret);
8997 out->ip_hdlr_handle = NULL;
8998 }
8999 }
Derek Chenf939fb72018-11-13 13:34:41 -08009000
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07009001 ret = io_streams_map_insert(adev, &out->stream.common,
9002 out->handle, AUDIO_PATCH_HANDLE_NONE);
9003 if (ret != 0)
9004 goto error_open;
9005
Susan Wang6dd13092021-01-25 10:27:11 -05009006 out->out_ctxt.output = out;
Derek Chenf939fb72018-11-13 13:34:41 -08009007
9008 pthread_mutex_lock(&adev->lock);
Susan Wang6dd13092021-01-25 10:27:11 -05009009 list_add_tail(&adev->active_outputs_list, &out->out_ctxt.list);
Derek Chenf939fb72018-11-13 13:34:41 -08009010 pthread_mutex_unlock(&adev->lock);
9011
Eric Laurent994a6932013-07-17 11:51:42 -07009012 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009013 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07009014
9015error_open:
Ashish Jain83a6cc22016-06-28 14:34:17 +05309016 if (out->convert_buffer)
9017 free(out->convert_buffer);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07009018 free(out);
9019 *stream_out = NULL;
9020 ALOGD("%s: exit: ret %d", __func__, ret);
9021 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009022}
9023
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05309024void adev_close_output_stream(struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009025 struct audio_stream_out *stream)
9026{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07009027 struct stream_out *out = (struct stream_out *)stream;
9028 struct audio_device *adev = out->dev;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08009029 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07009030
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009031 ALOGD("%s: enter:stream_handle(%s)",__func__, use_case_table[out->usecase]);
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05309032
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07009033 io_streams_map_remove(adev, out->handle);
9034
Susan Wang6dd13092021-01-25 10:27:11 -05009035 // remove out_ctxt early to prevent the stream
9036 // being opened in a race condition
9037 pthread_mutex_lock(&adev->lock);
9038 list_remove(&out->out_ctxt.list);
9039 pthread_mutex_unlock(&adev->lock);
9040
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05309041 // must deregister from sndmonitor first to prevent races
9042 // between the callback and close_stream
9043 audio_extn_snd_mon_unregister_listener(out);
9044
Ben Rombergerd771a7c2017-02-22 18:05:17 -08009045 /* close adsp hdrl session before standby */
9046 if (out->adsp_hdlr_stream_handle) {
9047 ret = audio_extn_adsp_hdlr_stream_close(out->adsp_hdlr_stream_handle);
9048 if (ret)
9049 ALOGE("%s: adsp_hdlr_stream_close failed %d",__func__, ret);
9050 out->adsp_hdlr_stream_handle = NULL;
9051 }
9052
Manish Dewangan21a850a2017-08-14 12:03:55 +05309053 if (out->ip_hdlr_handle) {
Naresh Tanniru85819452017-05-04 18:55:45 -07009054 audio_extn_ip_hdlr_intf_deinit(out->ip_hdlr_handle);
9055 out->ip_hdlr_handle = NULL;
9056 }
9057
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08009058 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05309059 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08009060 ret = voice_extn_compress_voip_close_output_stream(&stream->common);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05309061 out->started = 0;
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05309062 pthread_mutex_unlock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08009063 if(ret != 0)
9064 ALOGE("%s: Compress voip output cannot be closed, error:%d",
9065 __func__, ret);
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07009066 } else
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08009067 out_standby(&stream->common);
9068
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07009069 if (is_offload_usecase(out->usecase)) {
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08009070 audio_extn_dts_remove_state_notifier_node(out->usecase);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07009071 destroy_offload_callback_thread(out);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07009072 free_offload_usecase(adev, out->usecase);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07009073 if (out->compr_config.codec != NULL)
9074 free(out->compr_config.codec);
9075 }
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07009076
Zhou Songbaddf9f2020-11-20 13:57:39 +08009077 out->a2dp_muted = false;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05309078
Varun Balaraje49253e2017-07-06 19:48:56 +05309079 if (is_interactive_usecase(out->usecase))
9080 free_interactive_usecase(adev, out->usecase);
9081
Ashish Jain83a6cc22016-06-28 14:34:17 +05309082 if (out->convert_buffer != NULL) {
9083 free(out->convert_buffer);
9084 out->convert_buffer = NULL;
9085 }
9086
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07009087 if (adev->voice_tx_output == out)
9088 adev->voice_tx_output = NULL;
9089
Dechen Chai22768452021-07-30 09:29:16 +05309090#ifndef LINUX_ENABLED
Aalique Grahame22e49102018-12-18 14:23:57 -08009091 error_log_destroy(out->error_log);
9092 out->error_log = NULL;
Dechen Chai22768452021-07-30 09:29:16 +05309093#endif
Dhanalakshmi Siddani6c3d0992017-01-16 16:52:33 +05309094 if (adev->primary_output == out)
9095 adev->primary_output = NULL;
9096
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07009097 pthread_cond_destroy(&out->cond);
9098 pthread_mutex_destroy(&out->lock);
Weiyin Jiang280ea742020-09-08 20:28:22 +08009099 pthread_mutex_destroy(&out->pre_lock);
9100 pthread_mutex_destroy(&out->latch_lock);
9101 pthread_mutex_destroy(&out->position_query_lock);
Derek Chenf939fb72018-11-13 13:34:41 -08009102
9103 pthread_mutex_lock(&adev->lock);
Zhenlin Lian4f947842022-05-14 15:50:52 +05309104 clear_devices(&out->device_list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009105 free(stream);
Derek Chenf939fb72018-11-13 13:34:41 -08009106 pthread_mutex_unlock(&adev->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07009107 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009108}
9109
Shubhasini Sugumaran3fde6582022-01-19 15:54:50 -05009110void in_set_power_policy(uint8_t enable)
9111{
9112 struct listnode *node;
9113
9114 ALOGD("%s: Enter, state %d", __func__, enable);
9115
9116 pthread_mutex_lock(&adev->lock);
9117 adev->in_power_policy = enable ? POWER_POLICY_STATUS_ONLINE : POWER_POLICY_STATUS_OFFLINE;
9118 pthread_mutex_unlock(&adev->lock);
9119
9120 if (!enable) {
9121 list_for_each(node, &adev->active_inputs_list) {
9122 streams_input_ctxt_t *in_ctxt = node_to_item(node,
9123 streams_input_ctxt_t,
9124 list);
9125 struct stream_in *in = in_ctxt->input;
9126 in_standby(&in->stream.common);
9127 }
9128 }
9129
9130 ALOGD("%s: Exit", __func__);
9131}
9132
9133void out_set_power_policy(uint8_t enable)
9134{
9135 struct listnode *node;
9136
9137 ALOGD("%s: Enter, state %d", __func__, enable);
9138
9139 pthread_mutex_lock(&adev->lock);
E V Ravi317be872022-02-23 19:08:15 +05309140 adev->out_power_policy = enable ? POWER_POLICY_STATUS_ONLINE : POWER_POLICY_STATUS_OFFLINE;
Shubhasini Sugumaran3fde6582022-01-19 15:54:50 -05009141 pthread_mutex_unlock(&adev->lock);
9142
9143 if (!enable) {
9144 list_for_each(node, &adev->active_outputs_list) {
9145 streams_output_ctxt_t *out_ctxt = node_to_item(node,
9146 streams_output_ctxt_t,
9147 list);
9148 struct stream_out *out = out_ctxt->output;
9149 out_on_error(&out->stream.common);
9150 }
9151 }
9152
9153 ALOGD("%s: Exit", __func__);
9154}
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009155static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
9156{
9157 struct audio_device *adev = (struct audio_device *)dev;
9158 struct str_parms *parms;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009159 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07009160 int val;
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07009161 int ret;
9162 int status = 0;
Aalique Grahame22e49102018-12-18 14:23:57 -08009163 bool a2dp_reconfig = false;
Zhou Songd6d71752019-05-21 18:08:51 +08009164 struct listnode *node;
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07009165 int controller = -1, stream = -1;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009166
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08009167 ALOGD("%s: enter: %s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009168 parms = str_parms_create_str(kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009169
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05309170 if (!parms)
9171 goto error;
Naresh Tanniru4c630392014-05-12 01:05:52 +05309172
Derek Chen6f293672019-04-01 01:40:24 -07009173 /* notify adev and input/output streams on the snd card status */
9174 adev_snd_mon_cb((void *)adev, parms);
9175
Weiyin Jiang24f55292020-12-22 14:35:46 +08009176 ret = str_parms_get_str(parms, "SND_CARD_STATUS", value, sizeof(value));
9177 if (ret >= 0) {
9178 list_for_each(node, &adev->active_outputs_list) {
9179 streams_output_ctxt_t *out_ctxt = node_to_item(node,
9180 streams_output_ctxt_t,
9181 list);
9182 out_snd_mon_cb((void *)out_ctxt->output, parms);
9183 }
Derek Chen6f293672019-04-01 01:40:24 -07009184
Weiyin Jiang24f55292020-12-22 14:35:46 +08009185 list_for_each(node, &adev->active_inputs_list) {
9186 streams_input_ctxt_t *in_ctxt = node_to_item(node,
9187 streams_input_ctxt_t,
9188 list);
9189 in_snd_mon_cb((void *)in_ctxt->input, parms);
9190 }
Derek Chen6f293672019-04-01 01:40:24 -07009191 }
9192
Zhou Songd6d71752019-05-21 18:08:51 +08009193 pthread_mutex_lock(&adev->lock);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05309194 ret = str_parms_get_str(parms, "BT_SCO", value, sizeof(value));
9195 if (ret >= 0) {
9196 /* When set to false, HAL should disable EC and NS */
Zhou Songd6d71752019-05-21 18:08:51 +08009197 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0){
Ashish Jain1b9b30c2017-05-18 20:57:40 +05309198 adev->bt_sco_on = true;
Shalini Manjunatha3a2e06e2021-02-01 12:23:49 +05309199 /*
9200 * When ever BT_SCO=ON arrives, make sure to route
9201 * all use cases to SCO device, otherwise due to delay in
9202 * BT_SCO=ON and lack of synchronization with create audio patch
9203 * request for SCO device, some times use case not routed properly to
9204 * SCO device
9205 */
9206 struct audio_usecase *usecase;
9207 struct listnode *node;
9208 list_for_each(node, &adev->usecase_list) {
9209 usecase = node_to_item(node, struct audio_usecase, list);
Anver sadhiquef0efca32021-09-03 15:25:44 +05309210 if (usecase->stream.in && (usecase->type == PCM_CAPTURE) &&
Anver sadhique93f3c912021-08-11 11:19:45 +05309211 (!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 +05309212 ALOGD("BT_SCO ON, switch all in use case to it");
9213 select_devices(adev, usecase->id);
9214 }
Mingshu Pangef517202021-04-22 10:35:00 +08009215 if (usecase->stream.out && (usecase->type == PCM_PLAYBACK ||
9216 usecase->type == VOICE_CALL) &&
Anver sadhique93f3c912021-08-11 11:19:45 +05309217 (!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 +05309218 ALOGD("BT_SCO ON, switch all out use case to it");
9219 select_devices(adev, usecase->id);
9220 }
9221 }
9222 }
9223 else {
Ashish Jain1b9b30c2017-05-18 20:57:40 +05309224 adev->bt_sco_on = false;
Zhou Songd6d71752019-05-21 18:08:51 +08009225 audio_extn_sco_reset_configuration();
Kunlei Zhangd96af8f2019-08-27 16:33:29 +08009226 }
9227 }
9228
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07009229 status = voice_set_parameters(adev, parms);
9230 if (status != 0)
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08009231 goto done;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009232
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07009233 status = platform_set_parameters(adev->platform, parms);
9234 if (status != 0)
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08009235 goto done;
9236
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07009237 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
9238 if (ret >= 0) {
Vicky Sehrawate240e5d2014-08-12 17:17:04 -07009239 /* When set to false, HAL should disable EC and NS */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009240 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
9241 adev->bluetooth_nrec = true;
9242 else
9243 adev->bluetooth_nrec = false;
9244 }
9245
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07009246 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
9247 if (ret >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009248 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
9249 adev->screen_off = false;
9250 else
9251 adev->screen_off = true;
Quinn Male70f20f32019-06-26 16:50:26 -07009252 audio_extn_sound_trigger_update_screen_status(adev->screen_off);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009253 }
9254
Eric Laurent4b084132018-10-19 17:33:43 -07009255 ret = str_parms_get_int(parms, "rotation", &val);
9256 if (ret >= 0) {
9257 bool reverse_speakers = false;
9258 int camera_rotation = CAMERA_ROTATION_LANDSCAPE;
9259 switch (val) {
9260 // FIXME: note that the code below assumes that the speakers are in the correct placement
9261 // relative to the user when the device is rotated 90deg from its default rotation. This
9262 // assumption is device-specific, not platform-specific like this code.
9263 case 270:
9264 reverse_speakers = true;
9265 camera_rotation = CAMERA_ROTATION_INVERT_LANDSCAPE;
9266 break;
9267 case 0:
9268 case 180:
9269 camera_rotation = CAMERA_ROTATION_PORTRAIT;
9270 break;
9271 case 90:
9272 camera_rotation = CAMERA_ROTATION_LANDSCAPE;
9273 break;
9274 default:
9275 ALOGE("%s: unexpected rotation of %d", __func__, val);
9276 status = -EINVAL;
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07009277 }
Eric Laurent4b084132018-10-19 17:33:43 -07009278 if (status == 0) {
9279 // check and set swap
9280 // - check if orientation changed and speaker active
9281 // - set rotation and cache the rotation value
9282 adev->camera_orientation =
9283 (adev->camera_orientation & ~CAMERA_ROTATION_MASK) | camera_rotation;
9284 if (!audio_extn_is_maxx_audio_enabled())
9285 platform_check_and_set_swap_lr_channels(adev, reverse_speakers);
9286 }
9287 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07009288
Mingming Yin514a8bc2014-07-29 15:22:21 -07009289 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_SCO_WB, value, sizeof(value));
9290 if (ret >= 0) {
9291 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
9292 adev->bt_wb_speech_enabled = true;
9293 else
9294 adev->bt_wb_speech_enabled = false;
9295 }
9296
Zhou Song12c29502019-03-16 10:37:18 +08009297 ret = str_parms_get_str(parms, "bt_swb", value, sizeof(value));
9298 if (ret >= 0) {
9299 val = atoi(value);
9300 adev->swb_speech_mode = val;
9301 }
9302
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -07009303 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value, sizeof(value));
9304 if (ret >= 0) {
9305 val = atoi(value);
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05309306 audio_devices_t device = (audio_devices_t) val;
Zhou Song681350a2017-10-19 16:28:42 +08009307 if (audio_is_output_device(val) &&
9308 (val & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07009309 ALOGV("cache new ext disp type and edid");
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07009310 platform_get_controller_stream_from_params(parms, &controller, &stream);
9311 platform_set_ext_display_device_v2(adev->platform, controller, stream);
9312 ret = platform_get_ext_disp_type_v2(adev->platform, controller, stream);
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07009313 if (ret < 0) {
9314 ALOGE("%s: Failed to query disp type, ret:%d", __func__, ret);
Manisha Agarwal2f5ff882018-08-08 17:09:29 +05309315 } else {
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07009316 platform_cache_edid_v2(adev->platform, controller, stream);
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07009317 }
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05309318 } else if (audio_is_usb_out_device(device) || audio_is_usb_in_device(device)) {
vivek mehta344576a2016-04-12 18:56:03 -07009319 /*
9320 * Do not allow AFE proxy port usage by WFD source when USB headset is connected.
9321 * Per AudioPolicyManager, USB device is higher priority than WFD.
9322 * For Voice call over USB headset, voice call audio is routed to AFE proxy ports.
9323 * If WFD use case occupies AFE proxy, it may result unintended behavior while
9324 * starting voice call on USB
9325 */
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08009326 ret = str_parms_get_str(parms, "card", value, sizeof(value));
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05309327 if (ret >= 0)
9328 audio_extn_usb_add_device(device, atoi(value));
9329
Zhou Song6f862822017-11-06 17:27:57 +08009330 if (!audio_extn_usb_is_tunnel_supported()) {
9331 ALOGV("detected USB connect .. disable proxy");
9332 adev->allow_afe_proxy_usage = false;
9333 }
Zhou Song503196b2021-07-23 17:31:05 +08009334 } else if (audio_is_hearing_aid_out_device(device) &&
9335 property_get_bool("persist.vendor.audio.ha_proxy.enabled", false)) {
9336 adev->ha_proxy_enable = true;
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -07009337 }
9338 }
9339
9340 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value, sizeof(value));
9341 if (ret >= 0) {
9342 val = atoi(value);
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05309343 audio_devices_t device = (audio_devices_t) val;
Garmond Leunge3b6d482016-10-25 16:48:01 -07009344 /*
9345 * The HDMI / Displayport disconnect handling has been moved to
9346 * audio extension to ensure that its parameters are not
9347 * invalidated prior to updating sysfs of the disconnect event
9348 * Invalidate will be handled by audio_extn_ext_disp_set_parameters()
9349 */
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05309350 if (audio_is_usb_out_device(device) || audio_is_usb_in_device(device)) {
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08009351 ret = str_parms_get_str(parms, "card", value, sizeof(value));
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05309352 if (ret >= 0)
9353 audio_extn_usb_remove_device(device, atoi(value));
9354
Zhou Song6f862822017-11-06 17:27:57 +08009355 if (!audio_extn_usb_is_tunnel_supported()) {
9356 ALOGV("detected USB disconnect .. enable proxy");
9357 adev->allow_afe_proxy_usage = true;
9358 }
Zhou Song503196b2021-07-23 17:31:05 +08009359 } else if (audio_is_hearing_aid_out_device(device)) {
9360 adev->ha_proxy_enable = false;
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -07009361 }
9362 }
9363
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08009364 audio_extn_qdsp_set_parameters(adev, parms);
Aalique Grahame22e49102018-12-18 14:23:57 -08009365
9366 status = audio_extn_a2dp_set_parameters(parms, &a2dp_reconfig);
Aniket Kumar Lata23300322019-02-20 22:25:30 -08009367 if (status >= 0 && a2dp_reconfig) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05309368 struct audio_usecase *usecase;
9369 struct listnode *node;
9370 list_for_each(node, &adev->usecase_list) {
9371 usecase = node_to_item(node, struct audio_usecase, list);
Weiyin Jiangff56ff32020-05-08 14:32:21 +08009372 if ((usecase->stream.out == NULL) || (usecase->type != PCM_PLAYBACK))
9373 continue;
9374
9375 if (is_a2dp_out_device_type(&usecase->device_list)) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05309376 ALOGD("reconfigure a2dp... forcing device switch");
Naresh Tannirucd2353e2016-08-19 00:37:25 +05309377 audio_extn_a2dp_set_handoff_mode(true);
Manisha Agarwalf1c3b6b2019-06-25 13:00:33 +05309378 ALOGD("Switching to speaker and muting the stream before select_devices");
9379 check_a2dp_restore_l(adev, usecase->stream.out, false);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05309380 //force device switch to re configure encoder
9381 select_devices(adev, usecase->id);
Manisha Agarwalf1c3b6b2019-06-25 13:00:33 +05309382 ALOGD("Unmuting the stream after select_devices");
Zhou Songbaddf9f2020-11-20 13:57:39 +08009383 check_a2dp_restore_l(adev, usecase->stream.out, true);
Naresh Tannirucd2353e2016-08-19 00:37:25 +05309384 audio_extn_a2dp_set_handoff_mode(false);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05309385 break;
Zhou Songd01e7a22020-09-23 22:49:01 +08009386 } else if (is_offload_usecase(usecase->stream.out->usecase)) {
Weiyin Jiang280ea742020-09-08 20:28:22 +08009387 pthread_mutex_lock(&usecase->stream.out->latch_lock);
Zhou Songbaddf9f2020-11-20 13:57:39 +08009388 if (usecase->stream.out->a2dp_muted) {
Weiyin Jiang280ea742020-09-08 20:28:22 +08009389 pthread_mutex_unlock(&usecase->stream.out->latch_lock);
9390 reassign_device_list(&usecase->stream.out->device_list,
9391 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP, "");
9392 check_a2dp_restore_l(adev, usecase->stream.out, true);
9393 break;
9394 }
9395 pthread_mutex_unlock(&usecase->stream.out->latch_lock);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05309396 }
9397 }
9398 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08009399
9400 //handle vr audio setparam
9401 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_VR_AUDIO_MODE,
9402 value, sizeof(value));
9403 if (ret >= 0) {
9404 ALOGI("Setting vr mode to be %s", value);
9405 if (!strncmp(value, "true", 4)) {
9406 adev->vr_audio_mode_enabled = true;
9407 ALOGI("Setting vr mode to true");
9408 } else if (!strncmp(value, "false", 5)) {
9409 adev->vr_audio_mode_enabled = false;
9410 ALOGI("Setting vr mode to false");
9411 } else {
9412 ALOGI("wrong vr mode set");
9413 }
9414 }
9415
Eric Laurent4b084132018-10-19 17:33:43 -07009416 //FIXME: to be replaced by proper video capture properties API
9417 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_CAMERA_FACING, value, sizeof(value));
9418 if (ret >= 0) {
9419 int camera_facing = CAMERA_FACING_BACK;
9420 if (strcmp(value, AUDIO_PARAMETER_VALUE_FRONT) == 0)
9421 camera_facing = CAMERA_FACING_FRONT;
9422 else if (strcmp(value, AUDIO_PARAMETER_VALUE_BACK) == 0)
9423 camera_facing = CAMERA_FACING_BACK;
9424 else {
9425 ALOGW("%s: invalid camera facing value: %s", __func__, value);
9426 goto done;
9427 }
9428 adev->camera_orientation =
9429 (adev->camera_orientation & ~CAMERA_FACING_MASK) | camera_facing;
9430 struct audio_usecase *usecase;
9431 struct listnode *node;
9432 list_for_each(node, &adev->usecase_list) {
9433 usecase = node_to_item(node, struct audio_usecase, list);
9434 struct stream_in *in = usecase->stream.in;
9435 if (usecase->type == PCM_CAPTURE && in != NULL &&
9436 in->source == AUDIO_SOURCE_CAMCORDER && !in->standby) {
9437 select_devices(adev, in->usecase);
9438 }
9439 }
9440 }
9441
Tahir Dawson7fabad42022-06-21 12:37:55 -04009442 audio_extn_auto_hal_set_parameters(adev, parms);
Naresh Tannirucd2353e2016-08-19 00:37:25 +05309443 audio_extn_set_parameters(adev, parms);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08009444done:
9445 str_parms_destroy(parms);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08009446 pthread_mutex_unlock(&adev->lock);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05309447error:
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07009448 ALOGV("%s: exit with code(%d)", __func__, status);
9449 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009450}
9451
9452static char* adev_get_parameters(const struct audio_hw_device *dev,
9453 const char *keys)
9454{
Sidipotu Ashokaa4fa6a2018-12-21 09:19:26 +05309455 ALOGD("%s:%s", __func__, keys);
9456
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07009457 struct audio_device *adev = (struct audio_device *)dev;
9458 struct str_parms *reply = str_parms_create();
9459 struct str_parms *query = str_parms_create_str(keys);
9460 char *str;
Naresh Tannirud7205b62014-06-20 02:54:48 +05309461 char value[256] = {0};
9462 int ret = 0;
9463
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07009464 if (!query || !reply) {
Alexy Josephaee4fdd2016-01-29 13:02:07 -08009465 if (reply) {
9466 str_parms_destroy(reply);
9467 }
9468 if (query) {
9469 str_parms_destroy(query);
9470 }
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07009471 ALOGE("adev_get_parameters: failed to create query or reply");
9472 return NULL;
9473 }
9474
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08009475 //handle vr audio getparam
9476
9477 ret = str_parms_get_str(query,
9478 AUDIO_PARAMETER_KEY_VR_AUDIO_MODE,
9479 value, sizeof(value));
9480
9481 if (ret >= 0) {
9482 bool vr_audio_enabled = false;
9483 pthread_mutex_lock(&adev->lock);
9484 vr_audio_enabled = adev->vr_audio_mode_enabled;
9485 pthread_mutex_unlock(&adev->lock);
9486
Aniket Kumar Latad13758f2020-08-06 15:11:36 -07009487 ALOGV("getting vr mode to %d", vr_audio_enabled);
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08009488
9489 if (vr_audio_enabled) {
9490 str_parms_add_str(reply, AUDIO_PARAMETER_KEY_VR_AUDIO_MODE,
9491 "true");
9492 goto exit;
9493 } else {
9494 str_parms_add_str(reply, AUDIO_PARAMETER_KEY_VR_AUDIO_MODE,
9495 "false");
9496 goto exit;
9497 }
9498 }
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07009499
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08009500 pthread_mutex_lock(&adev->lock);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07009501 audio_extn_get_parameters(adev, query, reply);
Shiv Maliyappanahallif9308492013-12-12 12:18:09 -08009502 voice_get_parameters(adev, query, reply);
Aalique Grahame22e49102018-12-18 14:23:57 -08009503 audio_extn_a2dp_get_parameters(query, reply);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07009504 platform_get_parameters(adev->platform, query, reply);
justinwengd5395152019-11-04 12:23:09 +08009505 audio_extn_ma_get_parameters(adev, query, reply);
Naresh Tanniru80659832014-06-04 18:17:56 +05309506 pthread_mutex_unlock(&adev->lock);
9507
Naresh Tannirud7205b62014-06-20 02:54:48 +05309508exit:
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07009509 str = str_parms_to_str(reply);
9510 str_parms_destroy(query);
9511 str_parms_destroy(reply);
9512
Aniket Kumar Latad13758f2020-08-06 15:11:36 -07009513 ALOGV("%s: exit: returns - %s", __func__, str);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07009514 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009515}
9516
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07009517static int adev_init_check(const struct audio_hw_device *dev __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009518{
9519 return 0;
9520}
9521
9522static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
9523{
Haynes Mathew George5191a852013-09-11 14:19:36 -07009524 int ret;
9525 struct audio_device *adev = (struct audio_device *)dev;
Aalique Grahame22e49102018-12-18 14:23:57 -08009526
9527 audio_extn_extspk_set_voice_vol(adev->extspk, volume);
9528
Haynes Mathew George5191a852013-09-11 14:19:36 -07009529 pthread_mutex_lock(&adev->lock);
9530 /* cache volume */
Shruthi Krishnaace10852013-10-25 14:32:12 -07009531 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07009532 pthread_mutex_unlock(&adev->lock);
9533 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009534}
9535
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07009536static int adev_set_master_volume(struct audio_hw_device *dev __unused,
9537 float volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009538{
9539 return -ENOSYS;
9540}
9541
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07009542static int adev_get_master_volume(struct audio_hw_device *dev __unused,
9543 float *volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009544{
9545 return -ENOSYS;
9546}
9547
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07009548static int adev_set_master_mute(struct audio_hw_device *dev __unused,
9549 bool muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009550{
9551 return -ENOSYS;
9552}
9553
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07009554static int adev_get_master_mute(struct audio_hw_device *dev __unused,
9555 bool *muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009556{
9557 return -ENOSYS;
9558}
9559
9560static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
9561{
9562 struct audio_device *adev = (struct audio_device *)dev;
Garmond Leung5fd0b552018-04-17 11:56:12 -07009563 struct listnode *node;
9564 struct audio_usecase *usecase = NULL;
9565 int ret = 0;
kunleizdc4af9d2017-05-04 12:15:35 +08009566
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009567 pthread_mutex_lock(&adev->lock);
9568 if (adev->mode != mode) {
Jaideep Sharma477917f2020-03-13 18:13:33 +05309569 ALOGD("%s: mode %d , prev_mode %d \n", __func__, mode , adev->mode);
9570 adev->prev_mode = adev->mode; /* prev_mode is kept to handle voip concurrency*/
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009571 adev->mode = mode;
Weiyin Jiang96b96f72020-09-22 16:48:19 +08009572 if (mode == AUDIO_MODE_CALL_SCREEN) {
Jaideep Sharma477917f2020-03-13 18:13:33 +05309573 adev->current_call_output = adev->primary_output;
9574 voice_start_call(adev);
9575 } else if (voice_is_in_call_or_call_screen(adev) &&
Kunlei Zhang1d5c7f22019-05-21 14:25:57 +08009576 (mode == AUDIO_MODE_NORMAL ||
9577 (mode == AUDIO_MODE_IN_COMMUNICATION && !voice_is_call_state_active(adev)))) {
Garmond Leung5fd0b552018-04-17 11:56:12 -07009578 list_for_each(node, &adev->usecase_list) {
9579 usecase = node_to_item(node, struct audio_usecase, list);
9580 if (usecase->type == VOICE_CALL)
9581 break;
9582 }
9583 if (usecase &&
9584 audio_is_usb_out_device(usecase->out_snd_device & AUDIO_DEVICE_OUT_ALL_USB)) {
9585 ret = audio_extn_usb_check_and_set_svc_int(usecase,
9586 true);
9587 if (ret != 0) {
9588 /* default service interval was successfully updated,
9589 reopen USB backend with new service interval */
9590 check_usecases_codec_backend(adev,
9591 usecase,
9592 usecase->out_snd_device);
9593 }
9594 }
9595
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07009596 voice_stop_call(adev);
Banajit Goswami20cdd212015-09-11 01:11:30 -07009597 platform_set_gsm_mode(adev->platform, false);
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07009598 adev->current_call_output = NULL;
kunleizdc4af9d2017-05-04 12:15:35 +08009599 // restore device for other active usecases after stop call
9600 list_for_each(node, &adev->usecase_list) {
9601 usecase = node_to_item(node, struct audio_usecase, list);
9602 select_devices(adev, usecase->id);
9603 }
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07009604 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009605 }
9606 pthread_mutex_unlock(&adev->lock);
9607 return 0;
9608}
9609
9610static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
9611{
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08009612 int ret;
Aalique Grahame22e49102018-12-18 14:23:57 -08009613 struct audio_device *adev = (struct audio_device *)dev;
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08009614
9615 pthread_mutex_lock(&adev->lock);
Vidyakumar Athota2850d532013-11-19 16:02:12 -08009616 ALOGD("%s state %d\n", __func__, state);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08009617 ret = voice_set_mic_mute((struct audio_device *)dev, state);
Aalique Grahame22e49102018-12-18 14:23:57 -08009618
Derek Chend2530072014-11-24 12:39:14 -08009619 if (adev->ext_hw_plugin)
9620 ret = audio_extn_ext_hw_plugin_set_mic_mute(adev->ext_hw_plugin, state);
Aalique Grahame22e49102018-12-18 14:23:57 -08009621
9622 adev->mic_muted = state;
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08009623 pthread_mutex_unlock(&adev->lock);
9624
9625 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009626}
9627
9628static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
9629{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07009630 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009631 return 0;
9632}
9633
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07009634static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009635 const struct audio_config *config)
9636{
Avinash Chandrad7296d42021-08-04 15:07:47 +05309637 bool is_usb_hifi = IS_USB_HIFI;
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07009638 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009639
Aalique Grahame22e49102018-12-18 14:23:57 -08009640 /* Don't know if USB HIFI in this context so use true to be conservative */
9641 if (check_input_parameters(config->sample_rate, config->format, channel_count,
Avinash Chandrad7296d42021-08-04 15:07:47 +05309642 is_usb_hifi) != 0)
Aalique Grahame22e49102018-12-18 14:23:57 -08009643 return 0;
9644
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07009645 return get_input_buffer_size(config->sample_rate, config->format, channel_count,
9646 false /* is_low_latency: since we don't know, be conservative */);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009647}
9648
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009649static bool adev_input_allow_hifi_record(struct audio_device *adev,
9650 audio_devices_t devices,
9651 audio_input_flags_t flags,
9652 audio_source_t source) {
9653 const bool allowed = true;
9654
9655 if (!audio_is_usb_in_device(devices))
9656 return !allowed;
9657
9658 switch (flags) {
9659 case AUDIO_INPUT_FLAG_NONE:
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009660 break;
Haynes Mathew George59862182017-10-24 16:23:57 -07009661 case AUDIO_INPUT_FLAG_FAST: // disallow hifi record for FAST as
9662 // it affects RTD numbers over USB
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009663 default:
9664 return !allowed;
9665 }
9666
9667 switch (source) {
9668 case AUDIO_SOURCE_DEFAULT:
9669 case AUDIO_SOURCE_MIC:
9670 case AUDIO_SOURCE_UNPROCESSED:
9671 break;
9672 default:
9673 return !allowed;
9674 }
9675
9676 switch (adev->mode) {
9677 case 0:
9678 break;
9679 default:
9680 return !allowed;
9681 }
9682
9683 return allowed;
9684}
9685
Haynes Mathew George4ffef292017-11-21 15:08:02 -08009686static int adev_update_voice_comm_input_stream(struct stream_in *in,
9687 struct audio_config *config)
9688{
9689 bool valid_rate = (config->sample_rate == 8000 ||
9690 config->sample_rate == 16000 ||
9691 config->sample_rate == 32000 ||
9692 config->sample_rate == 48000);
9693 bool valid_ch = audio_channel_count_from_in_mask(in->channel_mask) == 1;
9694
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08009695 if(!voice_extn_is_compress_voip_supported()) {
kunleiz28c73e72019-03-27 17:24:04 +08009696 if (valid_rate && valid_ch) {
Haynes Mathew George4ffef292017-11-21 15:08:02 -08009697 in->usecase = USECASE_AUDIO_RECORD_VOIP;
9698 in->config = default_pcm_config_voip_copp;
9699 in->config.period_size = VOIP_IO_BUF_SIZE(in->sample_rate,
9700 DEFAULT_VOIP_BUF_DURATION_MS,
9701 DEFAULT_VOIP_BIT_DEPTH_BYTE)/2;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08009702 } else {
9703 ALOGW("%s No valid input in voip, use defaults"
9704 "sample rate %u, channel mask 0x%X",
9705 __func__, config->sample_rate, in->channel_mask);
9706 }
Haynes Mathew George4ffef292017-11-21 15:08:02 -08009707 in->config.rate = config->sample_rate;
9708 in->sample_rate = config->sample_rate;
9709 } else {
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08009710 //XXX needed for voice_extn_compress_voip_open_input_stream
9711 in->config.rate = config->sample_rate;
9712 if ((in->dev->mode == AUDIO_MODE_IN_COMMUNICATION ||
Shalini Manjunathaa763cc42019-08-23 15:13:46 +05309713 in->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08009714 voice_extn_compress_voip_is_active(in->dev)) &&
9715 (voice_extn_compress_voip_is_format_supported(in->format)) &&
9716 valid_rate && valid_ch) {
9717 voice_extn_compress_voip_open_input_stream(in);
9718 // update rate entries to match config from AF
9719 in->config.rate = config->sample_rate;
9720 in->sample_rate = config->sample_rate;
9721 } else {
9722 ALOGW("%s compress voip not active, use defaults", __func__);
9723 }
Haynes Mathew George4ffef292017-11-21 15:08:02 -08009724 }
Haynes Mathew George4ffef292017-11-21 15:08:02 -08009725 return 0;
9726}
9727
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009728static int adev_open_input_stream(struct audio_hw_device *dev,
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07009729 audio_io_handle_t handle,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009730 audio_devices_t devices,
9731 struct audio_config *config,
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07009732 struct audio_stream_in **stream_in,
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05309733 audio_input_flags_t flags,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08009734 const char *address,
Vidyakumar Athota5c398212015-03-31 21:53:21 -07009735 audio_source_t source)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009736{
9737 struct audio_device *adev = (struct audio_device *)dev;
9738 struct stream_in *in;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08009739 int ret = 0, buffer_size, frame_size;
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07009740 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07009741 bool is_low_latency = false;
Divya Narayanan Poojary45f19192016-09-30 18:52:13 +05309742 bool channel_mask_updated = false;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009743 bool is_usb_dev = audio_is_usb_in_device(devices);
9744 bool may_use_hifi_record = adev_input_allow_hifi_record(adev,
9745 devices,
9746 flags,
9747 source);
Andy Hung94320602018-10-29 18:31:12 -07009748 ALOGV("%s: enter: flags %#x, is_usb_dev %d, may_use_hifi_record %d,"
9749 " sample_rate %u, channel_mask %#x, format %#x",
9750 __func__, flags, is_usb_dev, may_use_hifi_record,
9751 config->sample_rate, config->channel_mask, config->format);
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05309752
kunleizdff872d2018-08-20 14:40:33 +08009753 if (is_usb_dev && (!audio_extn_usb_connected(NULL))) {
kunleizd6a9e0c2018-07-30 15:38:52 +08009754 is_usb_dev = false;
9755 devices = AUDIO_DEVICE_IN_BUILTIN_MIC;
9756 ALOGW("%s: ignore set device to non existing USB card, use input device(%#x)",
9757 __func__, devices);
9758 }
9759
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009760 *stream_in = NULL;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009761
9762 if (!(is_usb_dev && may_use_hifi_record)) {
9763 if (config->sample_rate == 0)
9764 config->sample_rate = 48000;
9765 if (config->channel_mask == AUDIO_CHANNEL_NONE)
9766 config->channel_mask = AUDIO_CHANNEL_IN_MONO;
9767 if (config->format == AUDIO_FORMAT_DEFAULT)
9768 config->format = AUDIO_FORMAT_PCM_16_BIT;
9769
9770 channel_count = audio_channel_count_from_in_mask(config->channel_mask);
9771
Aalique Grahame22e49102018-12-18 14:23:57 -08009772 if (check_input_parameters(config->sample_rate, config->format, channel_count,
9773 false) != 0)
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009774 return -EINVAL;
Chaithanya Krishna Bacharaju9955b162016-05-25 16:25:53 +05309775 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009776
9777 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07009778
9779 if (!in) {
9780 ALOGE("failed to allocate input stream");
9781 return -ENOMEM;
9782 }
9783
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05309784 ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x)\
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05309785 stream_handle(%p) io_handle(%d) source(%d) format %x",__func__, config->sample_rate,
9786 config->channel_mask, devices, &in->stream, handle, source, config->format);
Ravi Kumar Alamanda40703102014-04-24 10:34:41 -07009787 pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07009788 pthread_mutex_init(&in->pre_lock, (const pthread_mutexattr_t *) NULL);
Ravi Kumar Alamanda40703102014-04-24 10:34:41 -07009789
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009790 in->stream.common.get_sample_rate = in_get_sample_rate;
9791 in->stream.common.set_sample_rate = in_set_sample_rate;
9792 in->stream.common.get_buffer_size = in_get_buffer_size;
9793 in->stream.common.get_channels = in_get_channels;
9794 in->stream.common.get_format = in_get_format;
9795 in->stream.common.set_format = in_set_format;
9796 in->stream.common.standby = in_standby;
9797 in->stream.common.dump = in_dump;
9798 in->stream.common.set_parameters = in_set_parameters;
9799 in->stream.common.get_parameters = in_get_parameters;
9800 in->stream.common.add_audio_effect = in_add_audio_effect;
9801 in->stream.common.remove_audio_effect = in_remove_audio_effect;
9802 in->stream.set_gain = in_set_gain;
9803 in->stream.read = in_read;
9804 in->stream.get_input_frames_lost = in_get_input_frames_lost;
Aalique Grahame22e49102018-12-18 14:23:57 -08009805 in->stream.get_capture_position = in_get_capture_position;
Naresh Tannirudcb47c52018-06-25 16:23:32 +05309806 in->stream.get_active_microphones = in_get_active_microphones;
Paul McLeana50b7332018-12-17 08:24:21 -07009807 in->stream.set_microphone_direction = in_set_microphone_direction;
9808 in->stream.set_microphone_field_dimension = in_set_microphone_field_dimension;
juyuchendb308c22019-01-21 11:57:17 -07009809 in->stream.update_sink_metadata = in_update_sink_metadata;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009810
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08009811 list_init(&in->device_list);
9812 update_device_list(&in->device_list, devices, address, true);
Vidyakumar Athota5c398212015-03-31 21:53:21 -07009813 in->source = source;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009814 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009815 in->standby = 1;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07009816 in->capture_handle = handle;
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07009817 in->flags = flags;
Haynes Mathew George46740472017-10-27 18:40:12 -07009818 in->bit_width = 16;
9819 in->af_period_multiplier = 1;
justinweng20fb6d82019-02-21 18:49:00 -07009820 in->direction = MIC_DIRECTION_UNSPECIFIED;
9821 in->zoom = 0;
Carter Hsu2e429db2019-05-14 18:50:52 +08009822 list_init(&in->aec_list);
9823 list_init(&in->ns_list);
Phil Burkd898ba62019-06-20 12:49:01 -07009824 in->mmap_shared_memory_fd = -1; // not open
Haynes Mathew George46740472017-10-27 18:40:12 -07009825
Andy Hung94320602018-10-29 18:31:12 -07009826 ALOGV("%s: source %d, config->channel_mask %#x", __func__, source, config->channel_mask);
Aalique Grahame22e49102018-12-18 14:23:57 -08009827 if (source == AUDIO_SOURCE_VOICE_UPLINK ||
9828 source == AUDIO_SOURCE_VOICE_DOWNLINK) {
9829 /* Force channel config requested to mono if incall
9830 record is being requested for only uplink/downlink */
9831 if (config->channel_mask != AUDIO_CHANNEL_IN_MONO) {
9832 config->channel_mask = AUDIO_CHANNEL_IN_MONO;
9833 ret = -EINVAL;
9834 goto err_open;
9835 }
9836 }
9837
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009838 if (is_usb_dev && may_use_hifi_record) {
9839 /* HiFi record selects an appropriate format, channel, rate combo
9840 depending on sink capabilities*/
9841 ret = read_usb_sup_params_and_compare(false /*is_playback*/,
9842 &config->format,
9843 &in->supported_formats[0],
9844 MAX_SUPPORTED_FORMATS,
9845 &config->channel_mask,
9846 &in->supported_channel_masks[0],
9847 MAX_SUPPORTED_CHANNEL_MASKS,
9848 &config->sample_rate,
9849 &in->supported_sample_rates[0],
9850 MAX_SUPPORTED_SAMPLE_RATES);
9851 if (ret != 0) {
9852 ret = -EINVAL;
9853 goto err_open;
9854 }
9855 channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Haynes Mathew George4ffef292017-11-21 15:08:02 -08009856 } else if (config->format == AUDIO_FORMAT_DEFAULT) {
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05309857 config->format = AUDIO_FORMAT_PCM_16_BIT;
Surendar karkaaca3d082017-11-09 15:18:37 +05309858 } else if (property_get_bool("vendor.audio.capture.pcm.32bit.enable", false)
9859 && config->format == AUDIO_FORMAT_PCM_32_BIT) {
9860 in->config.format = PCM_FORMAT_S32_LE;
9861 in->bit_width = 32;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05309862 } else if ((config->format == AUDIO_FORMAT_PCM_FLOAT) ||
9863 (config->format == AUDIO_FORMAT_PCM_32_BIT) ||
9864 (config->format == AUDIO_FORMAT_PCM_24_BIT_PACKED) ||
9865 (config->format == AUDIO_FORMAT_PCM_8_24_BIT)) {
9866 bool ret_error = false;
9867 in->bit_width = 24;
9868 /* 24 bit is restricted to UNPROCESSED source only,also format supported
9869 from HAL is 24_packed and 8_24
9870 *> In case of UNPROCESSED source, for 24 bit, if format requested is other than
9871 24_packed return error indicating supported format is 24_packed
9872 *> In case of any other source requesting 24 bit or float return error
9873 indicating format supported is 16 bit only.
9874
9875 on error flinger will retry with supported format passed
9876 */
9877 if ((source != AUDIO_SOURCE_UNPROCESSED) &&
9878 (source != AUDIO_SOURCE_CAMCORDER)) {
9879 config->format = AUDIO_FORMAT_PCM_16_BIT;
9880 if (config->sample_rate > 48000)
9881 config->sample_rate = 48000;
9882 ret_error = true;
Haynes Mathew George46740472017-10-27 18:40:12 -07009883 } else if (!(config->format == AUDIO_FORMAT_PCM_24_BIT_PACKED ||
9884 config->format == AUDIO_FORMAT_PCM_8_24_BIT)) {
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05309885 config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
9886 ret_error = true;
9887 }
9888
9889 if (ret_error) {
9890 ret = -EINVAL;
9891 goto err_open;
9892 }
9893 }
9894
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009895 in->channel_mask = config->channel_mask;
9896 in->format = config->format;
9897
9898 in->usecase = USECASE_AUDIO_RECORD;
Samyak Jain0aa07ab2019-04-04 14:36:32 +05309899
Huicheng Liu1404ba12020-09-11 01:03:25 -04009900 /* validate bus device address */
9901 if (compare_device_type(&in->device_list, AUDIO_DEVICE_IN_BUS)) {
9902 /* extract car audio stream index */
9903 in->car_audio_stream =
9904 audio_extn_auto_hal_get_car_audio_stream_from_address(address);
9905 if (in->car_audio_stream < 0) {
9906 ALOGE("%s: invalid car audio stream %x",
9907 __func__, in->car_audio_stream);
9908 ret = -EINVAL;
9909 goto err_open;
9910 }
9911 ALOGV("%s: car_audio_stream 0x%x", __func__, in->car_audio_stream);
Huicheng Liuea4fd422021-01-12 18:34:50 -05009912 ret = audio_extn_auto_hal_open_input_stream(in);
9913 if (ret) {
9914 ALOGE("%s: Failed to open input stream for bus device", __func__);
9915 ret = -EINVAL;
9916 goto err_open;
9917 }
Huicheng Liu1404ba12020-09-11 01:03:25 -04009918 }
9919
Susan Wange3959562021-03-11 11:50:26 -05009920 /* reassign use case for echo reference stream on automotive platforms */
9921 if (in->source == AUDIO_SOURCE_ECHO_REFERENCE) {
9922 ret = audio_extn_auto_hal_open_echo_reference_stream(in);
9923 }
9924
Kogara Naveen Kumar379ff692022-01-31 11:56:51 +05309925 if ((in->source == AUDIO_SOURCE_FM_TUNER) || (devices == AUDIO_DEVICE_IN_FM_TUNER)) {
Samyak Jain0aa07ab2019-04-04 14:36:32 +05309926 if(!get_usecase_from_list(adev, USECASE_AUDIO_RECORD_FM_VIRTUAL))
9927 in->usecase = USECASE_AUDIO_RECORD_FM_VIRTUAL;
9928 else {
9929 ret = -EINVAL;
9930 goto err_open;
9931 }
9932 }
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +05309933#ifdef PLATFORM_AUTO
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +05309934 if ((config->sample_rate == 48000 ||
9935 config->sample_rate == 32000 ||
9936 config->sample_rate == 24000 ||
9937 config->sample_rate == 16000 ||
9938 config->sample_rate == 8000)&&
Deeraj Somanfa377bf2019-02-06 12:57:59 +05309939 (flags & AUDIO_INPUT_FLAG_TIMESTAMP) == 0 &&
9940 (flags & AUDIO_INPUT_FLAG_COMPRESS) == 0 &&
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +05309941 (flags & AUDIO_INPUT_FLAG_FAST) != 0)
9942#else
9943 if (config->sample_rate == LOW_LATENCY_CAPTURE_SAMPLE_RATE &&
9944 (flags & AUDIO_INPUT_FLAG_TIMESTAMP) == 0 &&
9945 (flags & AUDIO_INPUT_FLAG_COMPRESS) == 0 &&
9946 (flags & AUDIO_INPUT_FLAG_FAST) != 0)
9947#endif
9948{
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009949 is_low_latency = true;
9950#if LOW_LATENCY_CAPTURE_USE_CASE
Revathi Uddarajud9f23d92020-07-27 10:55:06 +05309951 if ((flags & AUDIO_INPUT_FLAG_VOIP_TX) != 0)
9952 in->usecase = USECASE_AUDIO_RECORD_VOIP_LOW_LATENCY;
9953 else
9954 in->usecase = USECASE_AUDIO_RECORD_LOW_LATENCY;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009955#endif
9956 in->realtime = may_use_noirq_mode(adev, in->usecase, in->flags);
Aalique Grahame22e49102018-12-18 14:23:57 -08009957 if (!in->realtime) {
9958 in->config = pcm_config_audio_capture;
9959 frame_size = audio_stream_in_frame_size(&in->stream);
9960 buffer_size = get_input_buffer_size(config->sample_rate,
9961 config->format,
9962 channel_count,
9963 is_low_latency);
9964 in->config.period_size = buffer_size / frame_size;
9965 in->config.rate = config->sample_rate;
9966 in->af_period_multiplier = 1;
9967 } else {
9968 // period size is left untouched for rt mode playback
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +05309969#ifdef PLATFORM_AUTO
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +05309970 switch(config->sample_rate)
9971 {
9972 case 48000:
9973 in->config = pcm_config_audio_capture_rt_48KHz;
9974 break;
9975 case 32000:
9976 in->config = pcm_config_audio_capture_rt_32KHz;
9977 break;
9978 case 24000:
9979 in->config = pcm_config_audio_capture_rt_24KHz;
9980 break;
9981 case 16000:
9982 in->config = pcm_config_audio_capture_rt_16KHz;
9983 break;
9984 case 8000:
9985 in->config = pcm_config_audio_capture_rt_8KHz;
9986 break;
9987 default:
9988 in->config = pcm_config_audio_capture_rt_48KHz;
9989 }
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +05309990#else
9991 in->config = pcm_config_audio_capture_rt_48KHz;
9992#endif
Aalique Grahame22e49102018-12-18 14:23:57 -08009993 in->af_period_multiplier = af_period_multiplier;
9994 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009995 }
9996
Susan Wangb803cb52021-10-14 12:03:36 -04009997 /* Additional sample rates added below must also be present
9998 in audio_policy_configuration.xml for mmap_no_irq_in */
9999 bool valid_mmap_record_rate = (config->sample_rate == 8000 ||
10000 config->sample_rate == 16000 ||
Susan Wang89da9042021-10-14 12:03:36 -040010001 config->sample_rate == 24000 ||
Susan Wangb803cb52021-10-14 12:03:36 -040010002 config->sample_rate == 32000 ||
10003 config->sample_rate == 48000);
10004 if (valid_mmap_record_rate &&
10005 ((in->flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0)) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -070010006 in->realtime = 0;
10007 in->usecase = USECASE_AUDIO_RECORD_MMAP;
10008 in->config = pcm_config_mmap_capture;
Haynes Mathew George46740472017-10-27 18:40:12 -070010009 in->config.format = pcm_format_from_audio_format(config->format);
Haynes Mathew George484e8d22017-07-31 18:55:17 -070010010 in->stream.start = in_start;
10011 in->stream.stop = in_stop;
10012 in->stream.create_mmap_buffer = in_create_mmap_buffer;
10013 in->stream.get_mmap_position = in_get_mmap_position;
Ramjee Singhbcb3b6c2021-11-17 13:19:16 +053010014 in->config.rate = config->sample_rate;
Haynes Mathew George484e8d22017-07-31 18:55:17 -070010015 ALOGV("%s: USECASE_AUDIO_RECORD_MMAP", __func__);
Haynes Mathew George46740472017-10-27 18:40:12 -070010016 } else if (is_usb_dev && may_use_hifi_record) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -070010017 in->usecase = USECASE_AUDIO_RECORD_HIFI;
10018 in->config = pcm_config_audio_capture;
10019 frame_size = audio_stream_in_frame_size(&in->stream);
10020 buffer_size = get_input_buffer_size(config->sample_rate,
10021 config->format,
10022 channel_count,
10023 false /*is_low_latency*/);
10024 in->config.period_size = buffer_size / frame_size;
10025 in->config.rate = config->sample_rate;
Haynes Mathew George484e8d22017-07-31 18:55:17 -070010026 in->config.format = pcm_format_from_audio_format(config->format);
Karthikeyan Mani07faa602018-08-20 11:01:32 -070010027 switch (config->format) {
10028 case AUDIO_FORMAT_PCM_32_BIT:
10029 in->bit_width = 32;
10030 break;
10031 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
10032 case AUDIO_FORMAT_PCM_8_24_BIT:
10033 in->bit_width = 24;
10034 break;
10035 default:
10036 in->bit_width = 16;
10037 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010038 } else if (is_single_device_type_equal(&in->device_list,
10039 AUDIO_DEVICE_IN_TELEPHONY_RX) ||
10040 is_single_device_type_equal(&in->device_list,
10041 AUDIO_DEVICE_IN_PROXY)) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -070010042 if (config->sample_rate == 0)
10043 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
10044 if (config->sample_rate != 48000 && config->sample_rate != 16000 &&
10045 config->sample_rate != 8000) {
10046 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
10047 ret = -EINVAL;
10048 goto err_open;
10049 }
10050 if (config->format == AUDIO_FORMAT_DEFAULT)
10051 config->format = AUDIO_FORMAT_PCM_16_BIT;
10052 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
10053 config->format = AUDIO_FORMAT_PCM_16_BIT;
10054 ret = -EINVAL;
10055 goto err_open;
10056 }
10057
10058 in->usecase = USECASE_AUDIO_RECORD_AFE_PROXY;
Zhou Song62ea0282020-03-22 19:53:01 +080010059 if (adev->ha_proxy_enable &&
10060 is_single_device_type_equal(&in->device_list,
10061 AUDIO_DEVICE_IN_TELEPHONY_RX))
10062 in->usecase = USECASE_AUDIO_RECORD_AFE_PROXY2;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -070010063 in->config = pcm_config_afe_proxy_record;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -070010064 in->config.rate = config->sample_rate;
Aalique Grahame22e49102018-12-18 14:23:57 -080010065 in->af_period_multiplier = 1;
10066 } else if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
Shalini Manjunathaac412b32020-01-03 15:00:14 +053010067 (!voice_extn_is_compress_voip_supported()) &&
Aalique Grahame22e49102018-12-18 14:23:57 -080010068 in->flags & AUDIO_INPUT_FLAG_VOIP_TX &&
10069 (config->sample_rate == 8000 ||
10070 config->sample_rate == 16000 ||
10071 config->sample_rate == 32000 ||
10072 config->sample_rate == 48000) &&
10073 channel_count == 1) {
10074 in->usecase = USECASE_AUDIO_RECORD_VOIP;
10075 in->config = pcm_config_audio_capture;
10076 frame_size = audio_stream_in_frame_size(&in->stream);
10077 buffer_size = get_stream_buffer_size(VOIP_CAPTURE_PERIOD_DURATION_MSEC,
10078 config->sample_rate,
10079 config->format,
10080 channel_count, false /*is_low_latency*/);
10081 in->config.period_size = buffer_size / frame_size;
10082 in->config.period_count = VOIP_CAPTURE_PERIOD_COUNT;
10083 in->config.rate = config->sample_rate;
10084 in->af_period_multiplier = 1;
Mingshu Pangc2d65042021-01-14 16:19:10 +080010085 } else if (in->realtime) {
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +053010086#ifdef PLATFORM_AUTO
Kogara Naveen Kumara688a812022-04-27 16:45:59 +053010087 switch(config->sample_rate)
10088 {
10089 case 48000:
10090 in->config = pcm_config_audio_capture_rt_48KHz;
10091 break;
10092 case 32000:
10093 in->config = pcm_config_audio_capture_rt_32KHz;
10094 break;
10095 case 24000:
10096 in->config = pcm_config_audio_capture_rt_24KHz;
10097 break;
10098 case 16000:
10099 in->config = pcm_config_audio_capture_rt_16KHz;
10100 break;
10101 case 8000:
10102 in->config = pcm_config_audio_capture_rt_8KHz;
10103 break;
10104 default:
10105 in->config = pcm_config_audio_capture_rt_48KHz;
10106 }
Mingshu Pangc2d65042021-01-14 16:19:10 +080010107 in->config.format = pcm_format_from_audio_format(config->format);
10108 in->af_period_multiplier = af_period_multiplier;
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +053010109#else
10110 in->config = pcm_config_audio_capture_rt_48KHz;
10111#endif
10112} else {
Revathi Uddarajud2634032017-12-07 14:42:34 +053010113 int ret_val;
10114 pthread_mutex_lock(&adev->lock);
10115 ret_val = audio_extn_check_and_set_multichannel_usecase(adev,
10116 in, config, &channel_mask_updated);
10117 pthread_mutex_unlock(&adev->lock);
10118
10119 if (!ret_val) {
10120 if (channel_mask_updated == true) {
10121 ALOGD("%s: return error to retry with updated channel mask (%#x)",
10122 __func__, config->channel_mask);
10123 ret = -EINVAL;
10124 goto err_open;
10125 }
10126 ALOGD("%s: created multi-channel session succesfully",__func__);
10127 } else if (audio_extn_compr_cap_enabled() &&
10128 audio_extn_compr_cap_format_supported(config->format) &&
10129 (in->dev->mode != AUDIO_MODE_IN_COMMUNICATION)) {
10130 audio_extn_compr_cap_init(in);
10131 } else if (audio_extn_cin_applicable_stream(in)) {
Deeraj Soman14230922019-01-30 16:39:30 +053010132 ret = audio_extn_cin_configure_input_stream(in, config);
Revathi Uddarajud2634032017-12-07 14:42:34 +053010133 if (ret)
10134 goto err_open;
10135 } else {
10136 in->config = pcm_config_audio_capture;
10137 in->config.rate = config->sample_rate;
10138 in->config.format = pcm_format_from_audio_format(config->format);
Revathi Uddarajud2634032017-12-07 14:42:34 +053010139 in->format = config->format;
10140 frame_size = audio_stream_in_frame_size(&in->stream);
10141 buffer_size = get_input_buffer_size(config->sample_rate,
Haynes Mathew George46740472017-10-27 18:40:12 -070010142 config->format,
10143 channel_count,
10144 is_low_latency);
Dieter Luecking5d57def2018-09-07 14:23:37 +020010145 /* prevent division-by-zero */
10146 if (frame_size == 0) {
10147 ALOGE("%s: Error frame_size==0", __func__);
10148 ret = -EINVAL;
10149 goto err_open;
10150 }
10151
Revathi Uddarajud2634032017-12-07 14:42:34 +053010152 in->config.period_size = buffer_size / frame_size;
Aalique Grahame22e49102018-12-18 14:23:57 -080010153 in->af_period_multiplier = 1;
Haynes Mathew George4ffef292017-11-21 15:08:02 -080010154
Revathi Uddarajud2634032017-12-07 14:42:34 +053010155 if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
10156 /* optionally use VOIP usecase depending on config(s) */
10157 ret = adev_update_voice_comm_input_stream(in, config);
10158 }
Haynes Mathew George4ffef292017-11-21 15:08:02 -080010159
Revathi Uddarajud2634032017-12-07 14:42:34 +053010160 if (ret) {
10161 ALOGE("%s AUDIO_SOURCE_VOICE_COMMUNICATION invalid args", __func__);
10162 goto err_open;
10163 }
Haynes Mathew George4ffef292017-11-21 15:08:02 -080010164 }
Jaideep Sharmad305a4a2020-02-27 14:29:04 +053010165
10166 /* assign concurrent capture usecase if record has to caried out from
10167 * actual hardware input source */
10168 if (audio_extn_is_concurrent_capture_enabled() &&
10169 !audio_is_virtual_input_source(in->source)) {
Samyak Jainc37062f2019-04-25 18:41:06 +053010170 /* Acquire lock to avoid two concurrent use cases initialized to
10171 same pcm record use case */
kunleiz28c73e72019-03-27 17:24:04 +080010172
Samyak Jainc37062f2019-04-25 18:41:06 +053010173 if (in->usecase == USECASE_AUDIO_RECORD) {
10174 pthread_mutex_lock(&adev->lock);
10175 if (!(adev->pcm_record_uc_state)) {
10176 ALOGV("%s: using USECASE_AUDIO_RECORD",__func__);
10177 adev->pcm_record_uc_state = 1;
10178 pthread_mutex_unlock(&adev->lock);
Krishna Kishor Jha0f4d8482022-02-18 10:56:05 +053010179 } else if (audio_extn_is_concurrent_pcm_record_enabled()) {
10180 in->usecase = get_record_usecase(adev);
10181 pthread_mutex_unlock(&adev->lock);
Samyak Jainc37062f2019-04-25 18:41:06 +053010182 } else {
10183 pthread_mutex_unlock(&adev->lock);
10184 /* Assign compress record use case for second record */
10185 in->usecase = USECASE_AUDIO_RECORD_COMPRESS2;
10186 in->flags |= AUDIO_INPUT_FLAG_COMPRESS;
10187 ALOGV("%s: overriding usecase with USECASE_AUDIO_RECORD_COMPRESS2 and appending compress flag", __func__);
10188 if (audio_extn_cin_applicable_stream(in)) {
10189 in->sample_rate = config->sample_rate;
Deeraj Soman14230922019-01-30 16:39:30 +053010190 ret = audio_extn_cin_configure_input_stream(in, config);
Samyak Jainc37062f2019-04-25 18:41:06 +053010191 if (ret)
10192 goto err_open;
10193 }
10194 }
10195 }
kunleiz28c73e72019-03-27 17:24:04 +080010196 }
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -070010197 }
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +053010198
Ramjee Singh82fd0c12019-08-21 16:31:33 +053010199 if (audio_extn_ssr_get_stream() != in)
10200 in->config.channels = channel_count;
Aniket Kumar Lata23d8cbf2019-04-10 19:54:46 -070010201
Aniket Kumar Lata23d8cbf2019-04-10 19:54:46 -070010202 in->sample_rate = in->config.rate;
10203
Dhananjay Kumard6d32152016-10-13 16:11:03 +053010204 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
10205 &adev->streams_input_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010206 &in->device_list, flags, in->format,
Haynes Mathew George4ffef292017-11-21 15:08:02 -080010207 in->sample_rate, in->bit_width,
10208 in->profile, &in->app_type_cfg);
Naresh Tanniru04f71882018-06-26 17:46:22 +053010209 register_format(in->format, in->supported_formats);
10210 register_channel_mask(in->channel_mask, in->supported_channel_masks);
10211 register_sample_rate(in->sample_rate, in->supported_sample_rates);
Dhananjay Kumard6d32152016-10-13 16:11:03 +053010212
Dechen Chai22768452021-07-30 09:29:16 +053010213#ifndef LINUX_ENABLED
Aalique Grahame22e49102018-12-18 14:23:57 -080010214 in->error_log = error_log_create(
10215 ERROR_LOG_ENTRIES,
10216 1000000000 /* aggregate consecutive identical errors within one second */);
Dechen Chai22768452021-07-30 09:29:16 +053010217#endif
Aalique Grahame22e49102018-12-18 14:23:57 -080010218
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -070010219 /* This stream could be for sound trigger lab,
10220 get sound trigger pcm if present */
10221 audio_extn_sound_trigger_check_and_get_session(in);
10222
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053010223 lock_input_stream(in);
10224 audio_extn_snd_mon_register_listener(in, in_snd_mon_cb);
10225 pthread_mutex_lock(&adev->lock);
10226 in->card_status = adev->card_status;
10227 pthread_mutex_unlock(&adev->lock);
10228 pthread_mutex_unlock(&in->lock);
10229
Aalique Grahame22e49102018-12-18 14:23:57 -080010230 stream_app_type_cfg_init(&in->app_type_cfg);
10231
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010232 *stream_in = &in->stream;
Derek Chenf939fb72018-11-13 13:34:41 -080010233
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010234 ret = io_streams_map_insert(adev, &in->stream.common,
10235 handle, AUDIO_PATCH_HANDLE_NONE);
10236 if (ret != 0)
10237 goto err_open;
10238
Susan Wang6dd13092021-01-25 10:27:11 -050010239 in->in_ctxt.input = in;
Derek Chenf939fb72018-11-13 13:34:41 -080010240
10241 pthread_mutex_lock(&adev->lock);
Susan Wang6dd13092021-01-25 10:27:11 -050010242 list_add_tail(&adev->active_inputs_list, &in->in_ctxt.list);
Derek Chenf939fb72018-11-13 13:34:41 -080010243 pthread_mutex_unlock(&adev->lock);
10244
Eric Laurent994a6932013-07-17 11:51:42 -070010245 ALOGV("%s: exit", __func__);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -080010246 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010247
10248err_open:
Krishna Kishor Jha0f4d8482022-02-18 10:56:05 +053010249 if (audio_extn_is_concurrent_pcm_record_enabled() && is_pcm_record_usecase(in->usecase)) {
10250 free_record_usecase(adev, in->usecase);
10251 } else if (in->usecase == USECASE_AUDIO_RECORD) {
Samyak Jainc37062f2019-04-25 18:41:06 +053010252 pthread_mutex_lock(&adev->lock);
10253 adev->pcm_record_uc_state = 0;
10254 pthread_mutex_unlock(&adev->lock);
10255 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010256 free(in);
10257 *stream_in = NULL;
10258 return ret;
10259}
10260
10261static void adev_close_input_stream(struct audio_hw_device *dev,
10262 struct audio_stream_in *stream)
10263{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -080010264 int ret;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -070010265 struct stream_in *in = (struct stream_in *)stream;
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -070010266 struct audio_device *adev = (struct audio_device *)dev;
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +053010267
Sidipotu Ashokf43018c2014-05-02 16:21:50 +053010268 ALOGD("%s: enter:stream_handle(%p)",__func__, in);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -080010269
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010270 if (in == NULL) {
10271 ALOGE("%s: audio_stream_in ptr is NULL", __func__);
10272 return;
10273 }
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010274 io_streams_map_remove(adev, in->capture_handle);
10275
Susan Wang6dd13092021-01-25 10:27:11 -050010276 // remove out_ctxt early to prevent the stream
10277 // being opened in a race condition
10278 pthread_mutex_lock(&adev->lock);
10279 list_remove(&in->in_ctxt.list);
10280 pthread_mutex_unlock(&adev->lock);
10281
kunleiz70e57612018-12-28 17:50:23 +080010282 /* must deregister from sndmonitor first to prevent races
10283 * between the callback and close_stream
10284 */
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053010285 audio_extn_snd_mon_unregister_listener(stream);
10286
kunleiz70e57612018-12-28 17:50:23 +080010287 /* Disable echo reference if there are no active input, hfp call
10288 * and sound trigger while closing input stream
10289 */
Eric Laurent637e2d42018-11-15 12:24:31 -080010290 if (adev_get_active_input(adev) == NULL &&
kunleiz70e57612018-12-28 17:50:23 +080010291 !audio_extn_hfp_is_active(adev) &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010292 !audio_extn_sound_trigger_check_ec_ref_enable()) {
10293 struct listnode out_devices;
10294 list_init(&out_devices);
10295 platform_set_echo_reference(adev, false, &out_devices);
Zhenlin Lian4f947842022-05-14 15:50:52 +053010296 clear_devices(&out_devices);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010297 } else
kunleiz70e57612018-12-28 17:50:23 +080010298 audio_extn_sound_trigger_update_ec_ref_status(false);
Venkata Narendra Kumar Gutta1bbbf542014-09-04 19:11:25 +053010299
Dechen Chai22768452021-07-30 09:29:16 +053010300#ifndef LINUX_ENABLED
Weiyin Jiang2995f662019-04-17 14:25:12 +080010301 error_log_destroy(in->error_log);
10302 in->error_log = NULL;
Dechen Chai22768452021-07-30 09:29:16 +053010303#endif
Pallavid7c7a272018-01-16 11:22:55 +053010304
Narsinga Rao Chella05573b72013-11-15 15:21:40 -080010305 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +053010306 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -080010307 ret = voice_extn_compress_voip_close_input_stream(&stream->common);
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +053010308 pthread_mutex_unlock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -080010309 if (ret != 0)
10310 ALOGE("%s: Compress voip input cannot be closed, error:%d",
10311 __func__, ret);
10312 } else
10313 in_standby(&stream->common);
10314
Weiyin Jiang280ea742020-09-08 20:28:22 +080010315 pthread_mutex_destroy(&in->lock);
10316 pthread_mutex_destroy(&in->pre_lock);
10317
Revathi Uddarajud2634032017-12-07 14:42:34 +053010318 pthread_mutex_lock(&adev->lock);
Krishna Kishor Jha0f4d8482022-02-18 10:56:05 +053010319 if (audio_extn_is_concurrent_pcm_record_enabled() && is_pcm_record_usecase(in->usecase)) {
10320 free_record_usecase(adev, in->usecase);
10321 } else if (in->usecase == USECASE_AUDIO_RECORD) {
Samyak Jain15fda662018-12-18 16:40:52 +053010322 adev->pcm_record_uc_state = 0;
10323 }
10324
Kunlei Zhang5d5d8d92020-02-26 15:00:59 +080010325 if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
10326 adev->enable_voicerx = false;
10327 }
10328
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -070010329 if (audio_extn_ssr_get_stream() == in) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -070010330 audio_extn_ssr_deinit();
10331 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010332
Garmond Leunge2433c32017-09-28 21:51:22 -070010333 if (audio_extn_ffv_get_stream() == in) {
10334 audio_extn_ffv_stream_deinit();
10335 }
10336
Dhananjay Kumaree4d2002016-10-25 18:02:58 +053010337 if (audio_extn_compr_cap_enabled() &&
Mingming Yine62d7842013-10-25 16:26:03 -070010338 audio_extn_compr_cap_format_supported(in->config.format))
10339 audio_extn_compr_cap_deinit();
Dhanalakshmi Siddani74cf00b2016-12-02 13:55:57 +053010340
Dhananjay Kumar7dbe3562019-08-30 05:49:33 +053010341 if (audio_extn_cin_attached_usecase(in))
Manish Dewangan46e07982018-12-13 18:18:59 +053010342 audio_extn_cin_free_input_stream_resources(in);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -070010343
Mingming Yinfd7607b2016-01-22 12:48:44 -080010344 if (in->is_st_session) {
10345 ALOGV("%s: sound trigger pcm stop lab", __func__);
10346 audio_extn_sound_trigger_stop_lab(in);
10347 }
Zhenlin Lian4f947842022-05-14 15:50:52 +053010348 clear_devices(&in->device_list);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -070010349 free(stream);
Revathi Uddarajud2634032017-12-07 14:42:34 +053010350 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010351 return;
10352}
10353
Aalique Grahame22e49102018-12-18 14:23:57 -080010354/* verifies input and output devices and their capabilities.
10355 *
10356 * This verification is required when enabling extended bit-depth or
10357 * sampling rates, as not all qcom products support it.
10358 *
10359 * Suitable for calling only on initialization such as adev_open().
10360 * It fills the audio_device use_case_table[] array.
10361 *
10362 * Has a side-effect that it needs to configure audio routing / devices
10363 * in order to power up the devices and read the device parameters.
10364 * It does not acquire any hw device lock. Should restore the devices
10365 * back to "normal state" upon completion.
10366 */
10367static int adev_verify_devices(struct audio_device *adev)
10368{
10369 /* enumeration is a bit difficult because one really wants to pull
10370 * the use_case, device id, etc from the hidden pcm_device_table[].
10371 * In this case there are the following use cases and device ids.
10372 *
10373 * [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = {0, 0},
10374 * [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {15, 15},
10375 * [USECASE_AUDIO_PLAYBACK_HIFI] = {1, 1},
10376 * [USECASE_AUDIO_PLAYBACK_OFFLOAD] = {9, 9},
10377 * [USECASE_AUDIO_RECORD] = {0, 0},
10378 * [USECASE_AUDIO_RECORD_LOW_LATENCY] = {15, 15},
10379 * [USECASE_VOICE_CALL] = {2, 2},
10380 *
10381 * USECASE_AUDIO_PLAYBACK_OFFLOAD, USECASE_AUDIO_PLAYBACK_HIFI omitted.
10382 * USECASE_VOICE_CALL omitted, but possible for either input or output.
10383 */
10384
10385 /* should be the usecases enabled in adev_open_input_stream() */
10386 static const int test_in_usecases[] = {
10387 USECASE_AUDIO_RECORD,
10388 USECASE_AUDIO_RECORD_LOW_LATENCY, /* does not appear to be used */
10389 };
10390 /* should be the usecases enabled in adev_open_output_stream()*/
10391 static const int test_out_usecases[] = {
10392 USECASE_AUDIO_PLAYBACK_DEEP_BUFFER,
10393 USECASE_AUDIO_PLAYBACK_LOW_LATENCY,
10394 };
10395 static const usecase_type_t usecase_type_by_dir[] = {
10396 PCM_PLAYBACK,
10397 PCM_CAPTURE,
10398 };
10399 static const unsigned flags_by_dir[] = {
10400 PCM_OUT,
10401 PCM_IN,
10402 };
10403
10404 size_t i;
10405 unsigned dir;
10406 const unsigned card_id = adev->snd_card;
10407
10408 for (dir = 0; dir < 2; ++dir) {
10409 const usecase_type_t usecase_type = usecase_type_by_dir[dir];
10410 const unsigned flags_dir = flags_by_dir[dir];
10411 const size_t testsize =
10412 dir ? ARRAY_SIZE(test_in_usecases) : ARRAY_SIZE(test_out_usecases);
10413 const int *testcases =
10414 dir ? test_in_usecases : test_out_usecases;
10415 const audio_devices_t audio_device =
10416 dir ? AUDIO_DEVICE_IN_BUILTIN_MIC : AUDIO_DEVICE_OUT_SPEAKER;
10417
10418 for (i = 0; i < testsize; ++i) {
10419 const audio_usecase_t audio_usecase = testcases[i];
10420 int device_id;
10421 struct pcm_params **pparams;
10422 struct stream_out out;
10423 struct stream_in in;
10424 struct audio_usecase uc_info;
10425 int retval;
10426
10427 pparams = &adev->use_case_table[audio_usecase];
10428 pcm_params_free(*pparams); /* can accept null input */
10429 *pparams = NULL;
10430
10431 /* find the device ID for the use case (signed, for error) */
10432 device_id = platform_get_pcm_device_id(audio_usecase, usecase_type);
10433 if (device_id < 0)
10434 continue;
10435
10436 /* prepare structures for device probing */
10437 memset(&uc_info, 0, sizeof(uc_info));
10438 uc_info.id = audio_usecase;
10439 uc_info.type = usecase_type;
Aniket Kumar Lata9c2fd892020-01-22 22:20:00 -080010440 list_init(&uc_info.device_list);
Aalique Grahame22e49102018-12-18 14:23:57 -080010441 if (dir) {
Aalique Grahame22e49102018-12-18 14:23:57 -080010442 memset(&in, 0, sizeof(in));
Aniket Kumar Lata9c2fd892020-01-22 22:20:00 -080010443 list_init(&in.device_list);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010444 update_device_list(&in.device_list, audio_device, "", true);
Aalique Grahame22e49102018-12-18 14:23:57 -080010445 in.source = AUDIO_SOURCE_VOICE_COMMUNICATION;
10446 uc_info.stream.in = &in;
Aalique Grahame22e49102018-12-18 14:23:57 -080010447 }
10448 memset(&out, 0, sizeof(out));
Aniket Kumar Lata9c2fd892020-01-22 22:20:00 -080010449 list_init(&out.device_list);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010450 update_device_list(&out.device_list, audio_device, "", true);
Aalique Grahame22e49102018-12-18 14:23:57 -080010451 uc_info.stream.out = &out;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010452 update_device_list(&uc_info.device_list, audio_device, "", true);
Aalique Grahame22e49102018-12-18 14:23:57 -080010453 uc_info.in_snd_device = SND_DEVICE_NONE;
10454 uc_info.out_snd_device = SND_DEVICE_NONE;
10455 list_add_tail(&adev->usecase_list, &uc_info.list);
10456
10457 /* select device - similar to start_(in/out)put_stream() */
10458 retval = select_devices(adev, audio_usecase);
10459 if (retval >= 0) {
10460 *pparams = pcm_params_get(card_id, device_id, flags_dir);
10461#if LOG_NDEBUG == 0
Aalique Grahame203bee02019-03-13 17:49:36 -070010462 char info[512]; /* for possible debug info */
Aalique Grahame22e49102018-12-18 14:23:57 -080010463 if (*pparams) {
10464 ALOGV("%s: (%s) card %d device %d", __func__,
10465 dir ? "input" : "output", card_id, device_id);
10466 pcm_params_to_string(*pparams, info, ARRAY_SIZE(info));
10467 } else {
10468 ALOGV("%s: cannot locate card %d device %d", __func__, card_id, device_id);
10469 }
10470#endif
10471 }
10472
10473 /* deselect device - similar to stop_(in/out)put_stream() */
10474 /* 1. Get and set stream specific mixer controls */
10475 retval = disable_audio_route(adev, &uc_info);
10476 /* 2. Disable the rx device */
10477 retval = disable_snd_device(adev,
10478 dir ? uc_info.in_snd_device : uc_info.out_snd_device);
10479 list_remove(&uc_info.list);
10480 }
10481 }
Aalique Grahame22e49102018-12-18 14:23:57 -080010482 return 0;
10483}
10484
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010485int update_patch(unsigned int num_sources,
10486 const struct audio_port_config *sources,
10487 unsigned int num_sinks,
10488 const struct audio_port_config *sinks,
10489 audio_patch_handle_t handle,
10490 struct audio_patch_info *p_info,
10491 patch_type_t patch_type, bool new_patch)
10492{
Aniket Kumar Latad13758f2020-08-06 15:11:36 -070010493 ALOGV("%s: enter", __func__);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010494
10495 if (p_info == NULL) {
10496 ALOGE("%s: Invalid patch pointer", __func__);
10497 return -EINVAL;
10498 }
10499
10500 if (new_patch) {
10501 p_info->patch = (struct audio_patch *) calloc(1, sizeof(struct audio_patch));
10502 if (p_info->patch == NULL) {
10503 ALOGE("%s: Could not allocate patch", __func__);
10504 return -ENOMEM;
10505 }
10506 }
10507
10508 p_info->patch->id = handle;
10509 p_info->patch->num_sources = num_sources;
10510 p_info->patch->num_sinks = num_sinks;
10511
10512 for (int i = 0; i < num_sources; i++)
10513 p_info->patch->sources[i] = sources[i];
10514 for (int i = 0; i < num_sinks; i++)
10515 p_info->patch->sinks[i] = sinks[i];
10516
10517 p_info->patch_type = patch_type;
10518 return 0;
10519}
10520
10521audio_patch_handle_t generate_patch_handle()
10522{
10523 static audio_patch_handle_t patch_handle = AUDIO_PATCH_HANDLE_NONE;
10524 if (++patch_handle < 0)
10525 patch_handle = AUDIO_PATCH_HANDLE_NONE + 1;
10526 return patch_handle;
10527}
10528
Siddartha Shaik31b530e2017-05-19 15:26:33 +053010529int adev_create_audio_patch(struct audio_hw_device *dev,
10530 unsigned int num_sources,
10531 const struct audio_port_config *sources,
10532 unsigned int num_sinks,
10533 const struct audio_port_config *sinks,
10534 audio_patch_handle_t *handle)
10535{
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010536 int ret = 0;
10537 struct audio_device *adev = (struct audio_device *)dev;
10538 struct audio_patch_info *p_info = NULL;
10539 patch_type_t patch_type = PATCH_NONE;
10540 audio_io_handle_t io_handle = AUDIO_IO_HANDLE_NONE;
10541 audio_source_t input_source = AUDIO_SOURCE_DEFAULT;
10542 struct audio_stream_info *s_info = NULL;
10543 struct audio_stream *stream = NULL;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010544 struct listnode devices;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010545 audio_devices_t device_type = AUDIO_DEVICE_NONE;
10546 bool new_patch = false;
10547 char addr[AUDIO_DEVICE_MAX_ADDRESS_LEN];
Siddartha Shaik31b530e2017-05-19 15:26:33 +053010548
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010549 ALOGD("%s: enter: num sources %d, num_sinks %d, handle %d", __func__,
10550 num_sources, num_sinks, *handle);
10551
10552 if (num_sources == 0 || num_sources > AUDIO_PATCH_PORTS_MAX ||
10553 num_sinks == 0 || num_sinks > AUDIO_PATCH_PORTS_MAX) {
10554 ALOGE("%s: Invalid patch arguments", __func__);
10555 ret = -EINVAL;
10556 goto done;
10557 }
10558
10559 if (num_sources > 1) {
10560 ALOGE("%s: Multiple sources are not supported", __func__);
10561 ret = -EINVAL;
10562 goto done;
10563 }
10564
10565 if (sources == NULL || sinks == NULL) {
10566 ALOGE("%s: Invalid sources or sinks port config", __func__);
10567 ret = -EINVAL;
10568 goto done;
10569 }
10570
10571 ALOGV("%s: source role %d, source type %d", __func__,
10572 sources[0].type, sources[0].role);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010573 list_init(&devices);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010574
10575 // Populate source/sink information and fetch stream info
10576 switch (sources[0].type) {
10577 case AUDIO_PORT_TYPE_DEVICE: // Patch for audio capture or loopback
10578 device_type = sources[0].ext.device.type;
10579 strlcpy(&addr[0], &sources[0].ext.device.address[0], AUDIO_DEVICE_MAX_ADDRESS_LEN);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010580 update_device_list(&devices, device_type, &addr[0], true);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010581 if (sinks[0].type == AUDIO_PORT_TYPE_MIX) {
10582 patch_type = PATCH_CAPTURE;
10583 io_handle = sinks[0].ext.mix.handle;
10584 input_source = sinks[0].ext.mix.usecase.source;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010585 ALOGD("%s: Capture patch from device %x to mix %d",
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010586 __func__, device_type, io_handle);
10587 } else {
10588 // Device to device patch is not implemented.
10589 // This space will need changes if audio HAL
10590 // handles device to device patches in the future.
10591 patch_type = PATCH_DEVICE_LOOPBACK;
10592 }
10593 break;
10594 case AUDIO_PORT_TYPE_MIX: // Patch for audio playback
10595 io_handle = sources[0].ext.mix.handle;
10596 for (int i = 0; i < num_sinks; i++) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010597 device_type = sinks[i].ext.device.type;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010598 strlcpy(&addr[0], &sinks[i].ext.device.address[0], AUDIO_DEVICE_MAX_ADDRESS_LEN);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010599 update_device_list(&devices, device_type, &addr[0], true);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010600 }
10601 patch_type = PATCH_PLAYBACK;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010602 ALOGD("%s: Playback patch from mix handle %d to device %x",
10603 __func__, io_handle, get_device_types(&devices));
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010604 break;
10605 case AUDIO_PORT_TYPE_SESSION:
10606 case AUDIO_PORT_TYPE_NONE:
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010607 ALOGE("%s: Unsupported source type %d", __func__, sources[0].type);
10608 ret = -EINVAL;
10609 goto done;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010610 }
10611
10612 pthread_mutex_lock(&adev->lock);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010613
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010614 // Generate patch info and update patch
10615 if (*handle == AUDIO_PATCH_HANDLE_NONE) {
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010616 *handle = generate_patch_handle();
10617 p_info = (struct audio_patch_info *)
10618 calloc(1, sizeof(struct audio_patch_info));
10619 if (p_info == NULL) {
10620 ALOGE("%s: Failed to allocate memory", __func__);
10621 pthread_mutex_unlock(&adev->lock);
10622 ret = -ENOMEM;
10623 goto done;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010624 }
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010625 new_patch = true;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010626 } else {
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010627 p_info = fetch_patch_info_l(adev, *handle);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010628 if (p_info == NULL) {
10629 ALOGE("%s: Unable to fetch patch for received patch handle %d",
10630 __func__, *handle);
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010631 pthread_mutex_unlock(&adev->lock);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010632 ret = -EINVAL;
10633 goto done;
10634 }
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010635 }
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010636 update_patch(num_sources, sources, num_sinks, sinks,
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010637 *handle, p_info, patch_type, new_patch);
10638
10639 // Fetch stream info of associated mix for playback or capture patches
10640 if (p_info->patch_type == PATCH_PLAYBACK ||
10641 p_info->patch_type == PATCH_CAPTURE) {
10642 s_info = hashmapGet(adev->io_streams_map, (void *) (intptr_t) io_handle);
10643 if (s_info == NULL) {
10644 ALOGE("%s: Failed to obtain stream info", __func__);
10645 if (new_patch)
10646 free(p_info);
10647 pthread_mutex_unlock(&adev->lock);
10648 ret = -EINVAL;
10649 goto done;
10650 }
10651 ALOGV("%s: Fetched stream info with io_handle %d", __func__, io_handle);
10652 s_info->patch_handle = *handle;
10653 stream = s_info->stream;
10654 }
10655 pthread_mutex_unlock(&adev->lock);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010656
10657 // Update routing for stream
10658 if (stream != NULL) {
Revathi Uddaraju4255a632021-12-02 05:11:13 -080010659 if (p_info->patch_type == PATCH_PLAYBACK) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010660 ret = route_output_stream((struct stream_out *) stream, &devices);
Revathi Uddaraju4255a632021-12-02 05:11:13 -080010661 } else if (p_info->patch_type == PATCH_CAPTURE) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010662 ret = route_input_stream((struct stream_in *) stream, &devices, input_source);
Revathi Uddaraju4255a632021-12-02 05:11:13 -080010663 }
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010664 if (ret < 0) {
10665 pthread_mutex_lock(&adev->lock);
10666 s_info->patch_handle = AUDIO_PATCH_HANDLE_NONE;
10667 if (new_patch)
10668 free(p_info);
10669 pthread_mutex_unlock(&adev->lock);
10670 ALOGE("%s: Stream routing failed for io_handle %d", __func__, io_handle);
10671 goto done;
10672 }
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010673 }
10674
10675 // Add new patch to patch map
10676 if (!ret && new_patch) {
10677 pthread_mutex_lock(&adev->lock);
10678 hashmapPut(adev->patch_map, (void *) (intptr_t) *handle, (void *) p_info);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010679 ALOGD("%s: Added a new patch with handle %d", __func__, *handle);
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010680 pthread_mutex_unlock(&adev->lock);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010681 }
10682
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010683done:
Zhenlin Lian4f947842022-05-14 15:50:52 +053010684 clear_devices(&devices);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010685 audio_extn_hw_loopback_create_audio_patch(dev,
Derek Chenf939fb72018-11-13 13:34:41 -080010686 num_sources,
10687 sources,
10688 num_sinks,
10689 sinks,
10690 handle);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010691 audio_extn_auto_hal_create_audio_patch(dev,
Derek Chenf939fb72018-11-13 13:34:41 -080010692 num_sources,
10693 sources,
10694 num_sinks,
10695 sinks,
10696 handle);
10697 return ret;
Siddartha Shaik31b530e2017-05-19 15:26:33 +053010698}
10699
10700int adev_release_audio_patch(struct audio_hw_device *dev,
10701 audio_patch_handle_t handle)
10702{
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010703 struct audio_device *adev = (struct audio_device *) dev;
10704 int ret = 0;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010705 audio_source_t input_source = AUDIO_SOURCE_DEFAULT;
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010706 struct audio_stream *stream = NULL;
Derek Chenf939fb72018-11-13 13:34:41 -080010707
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010708 if (handle == AUDIO_PATCH_HANDLE_NONE) {
10709 ALOGE("%s: Invalid patch handle %d", __func__, handle);
10710 ret = -EINVAL;
10711 goto done;
10712 }
10713
10714 ALOGD("%s: Remove patch with handle %d", __func__, handle);
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010715 pthread_mutex_lock(&adev->lock);
10716 struct audio_patch_info *p_info = fetch_patch_info_l(adev, handle);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010717 if (p_info == NULL) {
10718 ALOGE("%s: Patch info not found with handle %d", __func__, handle);
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010719 pthread_mutex_unlock(&adev->lock);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010720 ret = -EINVAL;
10721 goto done;
10722 }
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010723 struct audio_patch *patch = p_info->patch;
10724 if (patch == NULL) {
10725 ALOGE("%s: Patch not found for handle %d", __func__, handle);
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010726 pthread_mutex_unlock(&adev->lock);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010727 ret = -EINVAL;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010728 goto done;
10729 }
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010730 audio_io_handle_t io_handle = AUDIO_IO_HANDLE_NONE;
10731 switch (patch->sources[0].type) {
10732 case AUDIO_PORT_TYPE_MIX:
10733 io_handle = patch->sources[0].ext.mix.handle;
10734 break;
10735 case AUDIO_PORT_TYPE_DEVICE:
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010736 if (p_info->patch_type == PATCH_CAPTURE)
10737 io_handle = patch->sinks[0].ext.mix.handle;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010738 break;
10739 case AUDIO_PORT_TYPE_SESSION:
10740 case AUDIO_PORT_TYPE_NONE:
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010741 pthread_mutex_unlock(&adev->lock);
10742 ret = -EINVAL;
10743 goto done;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010744 }
10745
10746 // Remove patch and reset patch handle in stream info
Aniket Kumar Latace2b9fa2020-03-10 15:04:15 -070010747 patch_type_t patch_type = p_info->patch_type;
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010748 patch_map_remove_l(adev, handle);
Aniket Kumar Latace2b9fa2020-03-10 15:04:15 -070010749 if (patch_type == PATCH_PLAYBACK ||
10750 patch_type == PATCH_CAPTURE) {
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010751 struct audio_stream_info *s_info =
10752 hashmapGet(adev->io_streams_map, (void *) (intptr_t) io_handle);
10753 if (s_info == NULL) {
10754 ALOGE("%s: stream for io_handle %d is not available", __func__, io_handle);
10755 pthread_mutex_unlock(&adev->lock);
10756 goto done;
10757 }
10758 s_info->patch_handle = AUDIO_PATCH_HANDLE_NONE;
10759 stream = s_info->stream;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010760 }
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010761 pthread_mutex_unlock(&adev->lock);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010762
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010763 if (stream != NULL) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010764 struct listnode devices;
10765 list_init(&devices);
Aniket Kumar Latace2b9fa2020-03-10 15:04:15 -070010766 if (patch_type == PATCH_PLAYBACK)
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010767 ret = route_output_stream((struct stream_out *) stream, &devices);
Aniket Kumar Latace2b9fa2020-03-10 15:04:15 -070010768 else if (patch_type == PATCH_CAPTURE)
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010769 ret = route_input_stream((struct stream_in *) stream, &devices, input_source);
Zhenlin Lian4f947842022-05-14 15:50:52 +053010770 clear_devices(&devices);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010771 }
10772
10773 if (ret < 0)
10774 ALOGW("%s: Stream routing failed for io_handle %d", __func__, io_handle);
10775
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010776done:
10777 audio_extn_hw_loopback_release_audio_patch(dev, handle);
10778 audio_extn_auto_hal_release_audio_patch(dev, handle);
10779
10780 ALOGV("%s: Successfully released patch %d", __func__, handle);
Derek Chenf939fb72018-11-13 13:34:41 -080010781 return ret;
Siddartha Shaik31b530e2017-05-19 15:26:33 +053010782}
10783
10784int adev_get_audio_port(struct audio_hw_device *dev, struct audio_port *config)
10785{
Derek Chenf13dd492018-11-13 14:53:51 -080010786 int ret = 0;
10787
10788 ret = audio_extn_hw_loopback_get_audio_port(dev, config);
10789 ret |= audio_extn_auto_hal_get_audio_port(dev, config);
10790 return ret;
Siddartha Shaik31b530e2017-05-19 15:26:33 +053010791}
10792
10793int adev_set_audio_port_config(struct audio_hw_device *dev,
10794 const struct audio_port_config *config)
10795{
Derek Chenf13dd492018-11-13 14:53:51 -080010796 int ret = 0;
10797
10798 ret = audio_extn_hw_loopback_set_audio_port_config(dev, config);
10799 ret |= audio_extn_auto_hal_set_audio_port_config(dev, config);
10800 return ret;
Siddartha Shaik31b530e2017-05-19 15:26:33 +053010801}
10802
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -070010803static int adev_dump(const audio_hw_device_t *device __unused,
10804 int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010805{
10806 return 0;
10807}
10808
10809static int adev_close(hw_device_t *device)
10810{
Aalique Grahame22e49102018-12-18 14:23:57 -080010811 size_t i;
Preetam Singh Ranawatacc64542019-06-24 15:11:28 +053010812 struct audio_device *adev_temp = (struct audio_device *)device;
Kiran Kandi910e1862013-10-29 13:29:42 -070010813
Preetam Singh Ranawatacc64542019-06-24 15:11:28 +053010814 if (!adev_temp)
Kiran Kandi910e1862013-10-29 13:29:42 -070010815 return 0;
10816
10817 pthread_mutex_lock(&adev_init_lock);
10818
10819 if ((--audio_device_ref_count) == 0) {
Sujin Panicker390724d2019-04-26 10:43:36 +053010820 if (audio_extn_spkr_prot_is_enabled())
10821 audio_extn_spkr_prot_deinit();
Jaideep Sharmaa2b49672019-09-10 20:37:03 +053010822 audio_extn_battery_properties_listener_deinit();
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053010823 audio_extn_snd_mon_unregister_listener(adev);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -070010824 audio_extn_sound_trigger_deinit(adev);
Kiran Kandide144c82013-11-20 15:58:32 -080010825 audio_extn_listen_deinit(adev);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -080010826 audio_extn_qdsp_deinit();
Aalique Grahame22e49102018-12-18 14:23:57 -080010827 audio_extn_extspk_deinit(adev->extspk);
Dhananjay Kumard6d32152016-10-13 16:11:03 +053010828 audio_extn_utils_release_streams_cfg_lists(
10829 &adev->streams_output_cfg_list,
10830 &adev->streams_input_cfg_list);
Sidipotu Ashokd2f10ba2019-05-06 15:41:56 +053010831 if (audio_extn_qap_is_enabled())
10832 audio_extn_qap_deinit();
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +053010833 if (audio_extn_qaf_is_enabled())
10834 audio_extn_qaf_deinit();
Kiran Kandi910e1862013-10-29 13:29:42 -070010835 audio_route_free(adev->audio_route);
Weiyin Jiangfa65d3e2019-03-05 23:39:45 +080010836 audio_extn_gef_deinit(adev);
Kiran Kandi910e1862013-10-29 13:29:42 -070010837 free(adev->snd_dev_ref_cnt);
10838 platform_deinit(adev->platform);
Aalique Grahame22e49102018-12-18 14:23:57 -080010839 for (i = 0; i < ARRAY_SIZE(adev->use_case_table); ++i) {
10840 pcm_params_free(adev->use_case_table[i]);
10841 }
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -070010842 if (adev->adm_deinit)
10843 adev->adm_deinit(adev->adm_data);
Dhananjay Kumaree4d2002016-10-25 18:02:58 +053010844 qahwi_deinit(device);
Ben Rombergerd771a7c2017-02-22 18:05:17 -080010845 audio_extn_adsp_hdlr_deinit();
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053010846 audio_extn_snd_mon_deinit();
Siddartha Shaik44dd7702017-06-14 12:13:25 +053010847 audio_extn_hw_loopback_deinit(adev);
Garmond Leunge2433c32017-09-28 21:51:22 -070010848 audio_extn_ffv_deinit();
Satish Babu Patakokilac3c5d432017-07-04 22:48:59 +053010849 if (adev->device_cfg_params) {
10850 free(adev->device_cfg_params);
10851 adev->device_cfg_params = NULL;
10852 }
Derek Chend2530072014-11-24 12:39:14 -080010853 if(adev->ext_hw_plugin)
10854 audio_extn_ext_hw_plugin_deinit(adev->ext_hw_plugin);
Derek Chenae7b0342019-02-08 15:17:04 -080010855 audio_extn_auto_hal_deinit();
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010856 free_map(adev->patch_map);
10857 free_map(adev->io_streams_map);
Kiran Kandi910e1862013-10-29 13:29:42 -070010858 free(device);
10859 adev = NULL;
10860 }
10861 pthread_mutex_unlock(&adev_init_lock);
Vatsal Buchac09ae062018-11-14 13:25:08 +053010862 enable_gcov();
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010863 return 0;
10864}
10865
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -070010866/* This returns 1 if the input parameter looks at all plausible as a low latency period size,
10867 * or 0 otherwise. A return value of 1 doesn't mean the value is guaranteed to work,
10868 * just that it _might_ work.
10869 */
10870static int period_size_is_plausible_for_low_latency(int period_size)
10871{
10872 switch (period_size) {
10873 case 160:
Ravi Kumar Alamanda474de5a2015-06-25 20:08:01 -070010874 case 192:
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -070010875 case 240:
10876 case 320:
10877 case 480:
10878 return 1;
10879 default:
10880 return 0;
10881 }
10882}
10883
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053010884static void adev_snd_mon_cb(void *cookie, struct str_parms *parms)
10885{
10886 bool is_snd_card_status = false;
10887 bool is_ext_device_status = false;
10888 char value[32];
10889 int card = -1;
10890 card_status_t status;
10891
10892 if (cookie != adev || !parms)
10893 return;
10894
10895 if (!parse_snd_card_status(parms, &card, &status)) {
10896 is_snd_card_status = true;
10897 } else if (0 < str_parms_get_str(parms, "ext_audio_device", value, sizeof(value))) {
10898 is_ext_device_status = true;
10899 } else {
10900 // not a valid event
10901 return;
10902 }
10903
10904 pthread_mutex_lock(&adev->lock);
10905 if (card == adev->snd_card || is_ext_device_status) {
10906 if (is_snd_card_status && adev->card_status != status) {
Jaideep Sharmacb402512020-09-24 17:51:07 +053010907 ALOGD("%s card_status %d", __func__, status);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053010908 adev->card_status = status;
10909 platform_snd_card_update(adev->platform, status);
10910 audio_extn_fm_set_parameters(adev, parms);
Derek Chend6f371d2019-03-01 13:45:58 -080010911 audio_extn_auto_hal_set_parameters(adev, parms);
Zhou Song4f43e352019-07-02 00:32:23 +080010912 if (status == CARD_STATUS_OFFLINE)
10913 audio_extn_sco_reset_configuration();
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053010914 } else if (is_ext_device_status) {
10915 platform_set_parameters(adev->platform, parms);
10916 }
10917 }
10918 pthread_mutex_unlock(&adev->lock);
10919 return;
10920}
10921
Weiyin Jiang280ea742020-09-08 20:28:22 +080010922/* adev lock held */
10923int check_a2dp_restore_l(struct audio_device *adev, struct stream_out *out, bool restore)
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010924{
10925 struct audio_usecase *uc_info;
Zhou Song407e5aa2020-12-27 19:13:04 +080010926 struct audio_usecase *usecase;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010927 struct listnode devices;
Zhou Song407e5aa2020-12-27 19:13:04 +080010928 struct listnode *node;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010929
10930 uc_info = get_usecase_from_list(adev, out->usecase);
10931 if (uc_info == NULL) {
10932 ALOGE("%s: Could not find the usecase (%d) in the list",
10933 __func__, out->usecase);
10934 return -EINVAL;
10935 }
Aniket Kumar Lata9c2fd892020-01-22 22:20:00 -080010936 list_init(&devices);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010937
Zhou Songbaddf9f2020-11-20 13:57:39 +080010938 ALOGD("%s: enter: usecase(%d: %s), a2dp muted %d", __func__,
10939 out->usecase, use_case_table[out->usecase], out->a2dp_muted);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010940
10941 if (restore) {
Weiyin Jiang280ea742020-09-08 20:28:22 +080010942 pthread_mutex_lock(&out->latch_lock);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010943 // restore A2DP device for active usecases and unmute if required
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010944 if (is_a2dp_out_device_type(&out->device_list)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010945 ALOGD("%s: restoring A2dp and unmuting stream", __func__);
Zhou Songe5225132019-09-26 15:33:36 +080010946 if (uc_info->out_snd_device != SND_DEVICE_OUT_BT_A2DP)
10947 select_devices(adev, uc_info->id);
Zhou Songbaddf9f2020-11-20 13:57:39 +080010948
10949 if (is_offload_usecase(out->usecase)) {
10950 if (uc_info->out_snd_device == SND_DEVICE_OUT_BT_A2DP)
Weiyin Jiang280ea742020-09-08 20:28:22 +080010951 out_set_compr_volume(&out->stream, out->volume_l, out->volume_r);
Zhou Songbaddf9f2020-11-20 13:57:39 +080010952 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP) {
10953 out_set_voip_volume(&out->stream, out->volume_l, out->volume_r);
10954 } else {
10955 out_set_pcm_volume(&out->stream, out->volume_l, out->volume_r);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010956 }
Zhou Songbaddf9f2020-11-20 13:57:39 +080010957 out->a2dp_muted = false;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010958 }
Weiyin Jiang280ea742020-09-08 20:28:22 +080010959 pthread_mutex_unlock(&out->latch_lock);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010960 } else {
Weiyin Jiang280ea742020-09-08 20:28:22 +080010961 pthread_mutex_lock(&out->latch_lock);
Zhou Songbaddf9f2020-11-20 13:57:39 +080010962 // mute stream and switch to speaker if suspended
10963 if (!out->a2dp_muted && !out->standby) {
Zhou Songbaddf9f2020-11-20 13:57:39 +080010964 assign_devices(&devices, &out->device_list);
10965 reassign_device_list(&out->device_list, AUDIO_DEVICE_OUT_SPEAKER, "");
Zhou Song407e5aa2020-12-27 19:13:04 +080010966 list_for_each(node, &adev->usecase_list) {
10967 usecase = node_to_item(node, struct audio_usecase, list);
Zhou Songe63b6fe2021-04-15 13:54:46 +080010968 if ((usecase->type != PCM_CAPTURE) && (usecase != uc_info) &&
10969 !is_a2dp_out_device_type(&usecase->stream.out->device_list) &&
Zhou Songcf77af02021-05-14 18:21:14 +080010970 !is_sco_out_device_type(&usecase->stream.out->device_list) &&
Zhou Songe63b6fe2021-04-15 13:54:46 +080010971 platform_check_backends_match(SND_DEVICE_OUT_SPEAKER,
10972 usecase->out_snd_device)) {
Zhou Song407e5aa2020-12-27 19:13:04 +080010973 assign_devices(&out->device_list, &usecase->stream.out->device_list);
10974 break;
10975 }
10976 }
Zhou Songcf77af02021-05-14 18:21:14 +080010977 if ((is_a2dp_out_device_type(&devices) && list_length(&devices) == 1) ||
10978 (uc_info->out_snd_device == SND_DEVICE_OUT_BT_A2DP)) {
Zhou Song8edbbdb2021-01-14 16:48:03 +080010979 out->a2dp_muted = true;
10980 if (is_offload_usecase(out->usecase)) {
10981 if (out->offload_state == OFFLOAD_STATE_PLAYING)
10982 compress_pause(out->compr);
10983 out_set_compr_volume(&out->stream, (float)0, (float)0);
Zhou Song8edbbdb2021-01-14 16:48:03 +080010984 } else {
Weiyin Jiang906db3c2021-03-02 13:17:04 +080010985 if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP)
10986 out_set_voip_volume(&out->stream, (float)0, (float)0);
10987 else
10988 out_set_pcm_volume(&out->stream, (float)0, (float)0);
10989
Zhou Song8edbbdb2021-01-14 16:48:03 +080010990 /* wait for stale pcm drained before switching to speaker */
10991 uint32_t latency =
10992 (out->config.period_count * out->config.period_size * 1000) /
10993 (out->config.rate);
10994 usleep(latency * 1000);
10995 }
Zhou Songbaddf9f2020-11-20 13:57:39 +080010996 }
10997 select_devices(adev, out->usecase);
Zhou Song8edbbdb2021-01-14 16:48:03 +080010998 ALOGD("%s: switched to device:%s and stream muted:%d", __func__,
10999 platform_get_snd_device_name(uc_info->out_snd_device), out->a2dp_muted);
Zhou Songbaddf9f2020-11-20 13:57:39 +080011000 if (is_offload_usecase(out->usecase)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053011001 if (out->offload_state == OFFLOAD_STATE_PLAYING)
11002 compress_resume(out->compr);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053011003 }
Zhou Songbaddf9f2020-11-20 13:57:39 +080011004 assign_devices(&out->device_list, &devices);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053011005 }
Weiyin Jiang280ea742020-09-08 20:28:22 +080011006 pthread_mutex_unlock(&out->latch_lock);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053011007 }
Zhenlin Lian4f947842022-05-14 15:50:52 +053011008 clear_devices(&devices);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053011009 ALOGV("%s: exit", __func__);
11010 return 0;
11011}
11012
Haynes Mathew George01156f92018-04-13 15:29:54 -070011013void adev_on_battery_status_changed(bool charging)
11014{
11015 pthread_mutex_lock(&adev->lock);
11016 ALOGI("%s: battery status changed to %scharging", __func__, charging ? "" : "not ");
11017 adev->is_charging = charging;
Zhou Songc1088ea2018-06-12 00:17:29 +080011018 audio_extn_sound_trigger_update_battery_status(charging);
Haynes Mathew George01156f92018-04-13 15:29:54 -070011019 pthread_mutex_unlock(&adev->lock);
11020}
11021
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080011022static int adev_open(const hw_module_t *module, const char *name,
11023 hw_device_t **device)
11024{
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +053011025 int ret;
Derek Chenf939fb72018-11-13 13:34:41 -080011026 char value[PROPERTY_VALUE_MAX] = {0};
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +053011027 char mixer_ctl_name[128] = {0};
11028 struct mixer_ctl *ctl = NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +053011029
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -080011030 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080011031 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
11032
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070011033 pthread_mutex_lock(&adev_init_lock);
Kiran Kandi910e1862013-10-29 13:29:42 -070011034 if (audio_device_ref_count != 0){
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070011035 *device = &adev->device.common;
Kiran Kandi910e1862013-10-29 13:29:42 -070011036 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070011037 ALOGD("%s: returning existing instance of adev", __func__);
11038 ALOGD("%s: exit", __func__);
11039 pthread_mutex_unlock(&adev_init_lock);
11040 return 0;
11041 }
11042
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080011043 adev = calloc(1, sizeof(struct audio_device));
11044
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -070011045 if (!adev) {
11046 pthread_mutex_unlock(&adev_init_lock);
11047 return -ENOMEM;
11048 }
11049
Ravi Kumar Alamanda40703102014-04-24 10:34:41 -070011050 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
11051
Weiyin Jiange6ce6312019-01-28 18:28:22 +080011052 // register audio ext hidl at the earliest
11053 audio_extn_hidl_init();
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053011054#ifdef DYNAMIC_LOG_ENABLED
11055 register_for_dynamic_logging("hal");
11056#endif
11057
Derek Chenf939fb72018-11-13 13:34:41 -080011058 /* default audio HAL major version */
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070011059 uint32_t maj_version = 3;
Derek Chenf939fb72018-11-13 13:34:41 -080011060 if(property_get("vendor.audio.hal.maj.version", value, NULL))
11061 maj_version = atoi(value);
11062
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080011063 adev->device.common.tag = HARDWARE_DEVICE_TAG;
Derek Chenf939fb72018-11-13 13:34:41 -080011064 adev->device.common.version = HARDWARE_DEVICE_API_VERSION(maj_version, 0);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080011065 adev->device.common.module = (struct hw_module_t *)module;
11066 adev->device.common.close = adev_close;
11067
11068 adev->device.init_check = adev_init_check;
11069 adev->device.set_voice_volume = adev_set_voice_volume;
11070 adev->device.set_master_volume = adev_set_master_volume;
11071 adev->device.get_master_volume = adev_get_master_volume;
11072 adev->device.set_master_mute = adev_set_master_mute;
11073 adev->device.get_master_mute = adev_get_master_mute;
11074 adev->device.set_mode = adev_set_mode;
11075 adev->device.set_mic_mute = adev_set_mic_mute;
11076 adev->device.get_mic_mute = adev_get_mic_mute;
11077 adev->device.set_parameters = adev_set_parameters;
11078 adev->device.get_parameters = adev_get_parameters;
11079 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
11080 adev->device.open_output_stream = adev_open_output_stream;
11081 adev->device.close_output_stream = adev_close_output_stream;
11082 adev->device.open_input_stream = adev_open_input_stream;
11083 adev->device.close_input_stream = adev_close_input_stream;
Siddartha Shaik31b530e2017-05-19 15:26:33 +053011084 adev->device.create_audio_patch = adev_create_audio_patch;
11085 adev->device.release_audio_patch = adev_release_audio_patch;
11086 adev->device.get_audio_port = adev_get_audio_port;
11087 adev->device.set_audio_port_config = adev_set_audio_port_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080011088 adev->device.dump = adev_dump;
Naresh Tannirudcb47c52018-06-25 16:23:32 +053011089 adev->device.get_microphones = adev_get_microphones;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080011090
11091 /* Set the default route before the PCM stream is opened */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080011092 adev->mode = AUDIO_MODE_NORMAL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -080011093 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080011094 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080011095 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -080011096 adev->acdb_settings = TTY_MODE_OFF;
vivek mehta344576a2016-04-12 18:56:03 -070011097 adev->allow_afe_proxy_usage = true;
Ashish Jain1b9b30c2017-05-18 20:57:40 +053011098 adev->bt_sco_on = false;
Eric Laurent07eeafd2013-10-06 12:52:49 -070011099 /* adev->cur_hdmi_channels = 0; by calloc() */
Eric Laurentb23d5282013-05-14 15:27:20 -070011100 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
vivek mehtaae1018c2019-05-09 12:19:57 -070011101 /* Init audio and voice feature */
11102 audio_extn_feature_init();
11103 voice_extn_feature_init();
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -070011104 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -080011105 list_init(&adev->usecase_list);
Derek Chenf939fb72018-11-13 13:34:41 -080011106 list_init(&adev->active_inputs_list);
11107 list_init(&adev->active_outputs_list);
Rahul Sharma99770982019-03-06 17:05:26 +053011108 list_init(&adev->audio_patch_record_list);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070011109 adev->io_streams_map = hashmapCreate(AUDIO_IO_PORTS_MAX, audio_extn_utils_hash_fn,
11110 audio_extn_utils_hash_eq);
11111 if (!adev->io_streams_map) {
11112 ALOGE("%s: Could not create io streams map", __func__);
11113 ret = -ENOMEM;
11114 goto adev_open_err;
11115 }
11116 adev->patch_map = hashmapCreate(AUDIO_PATCH_PORTS_MAX, audio_extn_utils_hash_fn,
11117 audio_extn_utils_hash_eq);
11118 if (!adev->patch_map) {
11119 ALOGE("%s: Could not create audio patch map", __func__);
11120 ret = -ENOMEM;
11121 goto adev_open_err;
11122 }
Krishnankutty Kolathappilly0b2de1c2014-02-14 14:45:49 -080011123 adev->cur_wfd_channels = 2;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -070011124 adev->offload_usecases_state = 0;
Samyak Jain15fda662018-12-18 16:40:52 +053011125 adev->pcm_record_uc_state = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +053011126 adev->is_channel_status_set = false;
Sudheer Papothifa9d2282015-09-17 01:53:25 +053011127 adev->perf_lock_opts[0] = 0x101;
11128 adev->perf_lock_opts[1] = 0x20E;
11129 adev->perf_lock_opts_size = 2;
Xiaojun Sang785b5da2017-08-03 15:52:29 +080011130 adev->dsp_bit_width_enforce_mode = 0;
Aalique Grahame552b0832019-03-11 10:16:38 -070011131 adev->enable_hfp = false;
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +053011132 adev->use_old_pspd_mix_ctrl = false;
Aniket Kumar Lata60586a92019-05-22 22:18:55 -070011133 adev->adm_routing_changed = false;
Revathi Uddarajub26e3932020-06-10 14:51:02 +053011134 adev->a2dp_started = false;
Zhou Song503196b2021-07-23 17:31:05 +080011135 adev->ha_proxy_enable = false;
Naresh Tanniru4c630392014-05-12 01:05:52 +053011136
Zhou Song68ebc352019-12-05 17:11:15 +080011137 audio_extn_perf_lock_init();
11138
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080011139 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -070011140 adev->platform = platform_init(adev);
11141 if (!adev->platform) {
Eric Laurentb23d5282013-05-14 15:27:20 -070011142 ALOGE("%s: Failed to init platform data, aborting.", __func__);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070011143 ret = -EINVAL;
11144 goto adev_open_err;
Eric Laurentb23d5282013-05-14 15:27:20 -070011145 }
Eric Laurentc4aef752013-09-12 17:45:53 -070011146
Aalique Grahame22e49102018-12-18 14:23:57 -080011147 adev->extspk = audio_extn_extspk_init(adev);
Sidipotu Ashokd2f10ba2019-05-06 15:41:56 +053011148 if (audio_extn_qap_is_enabled()) {
11149 ret = audio_extn_qap_init(adev);
11150 if (ret < 0) {
Sidipotu Ashokd2f10ba2019-05-06 15:41:56 +053011151 ALOGE("%s: Failed to init platform data, aborting.", __func__);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070011152 goto adev_open_err;
Sidipotu Ashokd2f10ba2019-05-06 15:41:56 +053011153 }
11154 adev->device.open_output_stream = audio_extn_qap_open_output_stream;
11155 adev->device.close_output_stream = audio_extn_qap_close_output_stream;
11156 }
Aalique Grahame22e49102018-12-18 14:23:57 -080011157
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +053011158 if (audio_extn_qaf_is_enabled()) {
11159 ret = audio_extn_qaf_init(adev);
11160 if (ret < 0) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +053011161 ALOGE("%s: Failed to init platform data, aborting.", __func__);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070011162 goto adev_open_err;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +053011163 }
11164
11165 adev->device.open_output_stream = audio_extn_qaf_open_output_stream;
11166 adev->device.close_output_stream = audio_extn_qaf_close_output_stream;
11167 }
11168
Derek Chenae7b0342019-02-08 15:17:04 -080011169 audio_extn_auto_hal_init(adev);
Derek Chend2530072014-11-24 12:39:14 -080011170 adev->ext_hw_plugin = audio_extn_ext_hw_plugin_init(adev);
11171
Eric Laurentc4aef752013-09-12 17:45:53 -070011172 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
11173 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
11174 if (adev->visualizer_lib == NULL) {
11175 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
11176 } else {
11177 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
11178 adev->visualizer_start_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -080011179 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -070011180 "visualizer_hal_start_output");
11181 adev->visualizer_stop_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -080011182 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -070011183 "visualizer_hal_stop_output");
11184 }
11185 }
Dhanalakshmi Siddani21be3ac2016-12-29 14:31:08 +053011186 audio_extn_init(adev);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -080011187 voice_extn_init(adev);
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -080011188 audio_extn_listen_init(adev, adev->snd_card);
Weiyin Jiangaa80acd2016-09-21 16:42:11 +080011189 audio_extn_gef_init(adev);
Siddartha Shaik44dd7702017-06-14 12:13:25 +053011190 audio_extn_hw_loopback_init(adev);
Garmond Leunge2433c32017-09-28 21:51:22 -070011191 audio_extn_ffv_init(adev);
Eric Laurentc4aef752013-09-12 17:45:53 -070011192
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -080011193 if (access(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, R_OK) == 0) {
11194 adev->offload_effects_lib = dlopen(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, RTLD_NOW);
11195 if (adev->offload_effects_lib == NULL) {
11196 ALOGE("%s: DLOPEN failed for %s", __func__,
11197 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
11198 } else {
11199 ALOGV("%s: DLOPEN successful for %s", __func__,
11200 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
11201 adev->offload_effects_start_output =
Ashish Jain5106d362016-05-11 19:23:33 +053011202 (int (*)(audio_io_handle_t, int, struct mixer *))dlsym(adev->offload_effects_lib,
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -080011203 "offload_effects_bundle_hal_start_output");
11204 adev->offload_effects_stop_output =
11205 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
11206 "offload_effects_bundle_hal_stop_output");
Jitendra Naruka1b6513f2014-11-22 19:34:13 -080011207 adev->offload_effects_set_hpx_state =
11208 (int (*)(bool))dlsym(adev->offload_effects_lib,
11209 "offload_effects_bundle_set_hpx_state");
Dhananjay Kumard68883d2015-09-04 13:39:26 +053011210 adev->offload_effects_get_parameters =
11211 (void (*)(struct str_parms *, struct str_parms *))
11212 dlsym(adev->offload_effects_lib,
11213 "offload_effects_bundle_get_parameters");
11214 adev->offload_effects_set_parameters =
11215 (void (*)(struct str_parms *))dlsym(adev->offload_effects_lib,
11216 "offload_effects_bundle_set_parameters");
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -080011217 }
11218 }
11219
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -070011220 if (access(ADM_LIBRARY_PATH, R_OK) == 0) {
11221 adev->adm_lib = dlopen(ADM_LIBRARY_PATH, RTLD_NOW);
11222 if (adev->adm_lib == NULL) {
11223 ALOGE("%s: DLOPEN failed for %s", __func__, ADM_LIBRARY_PATH);
11224 } else {
11225 ALOGV("%s: DLOPEN successful for %s", __func__, ADM_LIBRARY_PATH);
11226 adev->adm_init = (adm_init_t)
11227 dlsym(adev->adm_lib, "adm_init");
11228 adev->adm_deinit = (adm_deinit_t)
11229 dlsym(adev->adm_lib, "adm_deinit");
11230 adev->adm_register_input_stream = (adm_register_input_stream_t)
11231 dlsym(adev->adm_lib, "adm_register_input_stream");
11232 adev->adm_register_output_stream = (adm_register_output_stream_t)
11233 dlsym(adev->adm_lib, "adm_register_output_stream");
11234 adev->adm_deregister_stream = (adm_deregister_stream_t)
11235 dlsym(adev->adm_lib, "adm_deregister_stream");
11236 adev->adm_request_focus = (adm_request_focus_t)
11237 dlsym(adev->adm_lib, "adm_request_focus");
11238 adev->adm_abandon_focus = (adm_abandon_focus_t)
11239 dlsym(adev->adm_lib, "adm_abandon_focus");
Haynes Mathew George5beddd42016-06-27 18:33:40 -070011240 adev->adm_set_config = (adm_set_config_t)
11241 dlsym(adev->adm_lib, "adm_set_config");
11242 adev->adm_request_focus_v2 = (adm_request_focus_v2_t)
11243 dlsym(adev->adm_lib, "adm_request_focus_v2");
11244 adev->adm_is_noirq_avail = (adm_is_noirq_avail_t)
11245 dlsym(adev->adm_lib, "adm_is_noirq_avail");
11246 adev->adm_on_routing_change = (adm_on_routing_change_t)
11247 dlsym(adev->adm_lib, "adm_on_routing_change");
Aniket Kumar Lata60586a92019-05-22 22:18:55 -070011248 adev->adm_request_focus_v2_1 = (adm_request_focus_v2_1_t)
11249 dlsym(adev->adm_lib, "adm_request_focus_v2_1");
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -070011250 }
11251 }
11252
Aalique Grahame22e49102018-12-18 14:23:57 -080011253 adev->enable_voicerx = false;
Mingming Yin514a8bc2014-07-29 15:22:21 -070011254 adev->bt_wb_speech_enabled = false;
Zhou Song12c29502019-03-16 10:37:18 +080011255 adev->swb_speech_mode = SPEECH_MODE_INVALID;
Kunlei Zhanga3c4cb02021-03-05 16:03:54 +080011256 adev->fluence_nn_usecase_id = USECASE_INVALID;
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -080011257 //initialize this to false for now,
11258 //this will be set to true through set param
11259 adev->vr_audio_mode_enabled = false;
Mingming Yin514a8bc2014-07-29 15:22:21 -070011260
Pradnya Chaphekar8a9dcd82014-09-09 09:49:10 -070011261 audio_extn_ds2_enable(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080011262 *device = &adev->device.common;
Aalique Grahame22e49102018-12-18 14:23:57 -080011263
11264 if (k_enable_extended_precision)
11265 adev_verify_devices(adev);
11266
Xiaojun Sang785b5da2017-08-03 15:52:29 +080011267 adev->dsp_bit_width_enforce_mode =
11268 adev_init_dsp_bit_width_enforce_mode(adev->mixer);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080011269
Dhananjay Kumard6d32152016-10-13 16:11:03 +053011270 audio_extn_utils_update_streams_cfg_lists(adev->platform, adev->mixer,
11271 &adev->streams_output_cfg_list,
11272 &adev->streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070011273
Kiran Kandi910e1862013-10-29 13:29:42 -070011274 audio_device_ref_count++;
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -070011275
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -070011276 int trial;
Manisha Agarwalc75a0202019-12-06 18:48:25 +053011277 if (property_get("vendor.audio_hal.period_size", value, NULL) > 0) {
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -070011278 trial = atoi(value);
11279 if (period_size_is_plausible_for_low_latency(trial)) {
11280 pcm_config_low_latency.period_size = trial;
11281 pcm_config_low_latency.start_threshold = trial / 4;
11282 pcm_config_low_latency.avail_min = trial / 4;
11283 configured_low_latency_capture_period_size = trial;
11284 }
11285 }
ronghuiz93177262021-04-21 19:58:13 +080011286 if (property_get("vendor.audio_hal.in_period_size", value, NULL) > 0) {
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -070011287 trial = atoi(value);
11288 if (period_size_is_plausible_for_low_latency(trial)) {
11289 configured_low_latency_capture_period_size = trial;
11290 }
11291 }
11292
Vignesh Kulothungan7d374312018-02-21 17:12:00 -080011293 adev->mic_break_enabled = property_get_bool("vendor.audio.mic_break", false);
11294
Eric Laurent4b084132018-10-19 17:33:43 -070011295 adev->camera_orientation = CAMERA_DEFAULT;
11296
Manisha Agarwalc75a0202019-12-06 18:48:25 +053011297 if (property_get("vendor.audio_hal.period_multiplier",value,NULL) > 0) {
Haynes Mathew George5beddd42016-06-27 18:33:40 -070011298 af_period_multiplier = atoi(value);
11299 if (af_period_multiplier < 0)
11300 af_period_multiplier = 2;
11301 else if (af_period_multiplier > 4)
11302 af_period_multiplier = 4;
11303
11304 ALOGV("new period_multiplier = %d", af_period_multiplier);
11305 }
11306
Arun Mirpurib1bec9c2019-01-29 16:42:45 -080011307 audio_extn_qdsp_init(adev->platform);
Aalique Grahame22e49102018-12-18 14:23:57 -080011308
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -070011309 adev->multi_offload_enable = property_get_bool("vendor.audio.offload.multiple.enabled", false);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070011310 pthread_mutex_unlock(&adev_init_lock);
11311
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -070011312 if (adev->adm_init)
11313 adev->adm_data = adev->adm_init();
11314
Dhananjay Kumaree4d2002016-10-25 18:02:58 +053011315 qahwi_init(*device);
Ben Rombergerd771a7c2017-02-22 18:05:17 -080011316 audio_extn_adsp_hdlr_init(adev->mixer);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053011317
11318 audio_extn_snd_mon_init();
11319 pthread_mutex_lock(&adev->lock);
11320 audio_extn_snd_mon_register_listener(adev, adev_snd_mon_cb);
11321 adev->card_status = CARD_STATUS_ONLINE;
Shubhasini Sugumaran3fde6582022-01-19 15:54:50 -050011322 adev->out_power_policy = POWER_POLICY_STATUS_ONLINE;
11323 adev->in_power_policy = POWER_POLICY_STATUS_ONLINE;
Haynes Mathew George01156f92018-04-13 15:29:54 -070011324 audio_extn_battery_properties_listener_init(adev_on_battery_status_changed);
11325 /*
11326 * if the battery state callback happens before charging can be queried,
11327 * it will be guarded with the adev->lock held in the cb function and so
11328 * the callback value will reflect the latest state
11329 */
11330 adev->is_charging = audio_extn_battery_properties_is_charging();
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053011331 audio_extn_sound_trigger_init(adev); /* dependent on snd_mon_init() */
Zhou Songc1088ea2018-06-12 00:17:29 +080011332 audio_extn_sound_trigger_update_battery_status(adev->is_charging);
justinweng20fb6d82019-02-21 18:49:00 -070011333 audio_extn_audiozoom_init();
Zhou Songc1088ea2018-06-12 00:17:29 +080011334 pthread_mutex_unlock(&adev->lock);
Satish Babu Patakokilac3c5d432017-07-04 22:48:59 +053011335 /* Allocate memory for Device config params */
11336 adev->device_cfg_params = (struct audio_device_config_param*)
11337 calloc(platform_get_max_codec_backend(),
11338 sizeof(struct audio_device_config_param));
11339 if (adev->device_cfg_params == NULL)
11340 ALOGE("%s: Memory allocation failed for Device config params", __func__);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053011341
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +053011342 /*
11343 * Check if new PSPD matrix mixer control is supported. If not
11344 * supported, then set flag so that old mixer ctrl is sent while
11345 * sending pspd coefficients on older kernel version. Query mixer
11346 * control for default pcm id and channel value one.
11347 */
11348 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
11349 "AudStr %d ChMixer Weight Ch %d", 0, 1);
11350
11351 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
11352 if (!ctl) {
11353 ALOGE("%s: ERROR. Could not get ctl for mixer cmd - %s",
11354 __func__, mixer_ctl_name);
11355 adev->use_old_pspd_mix_ctrl = true;
11356 }
11357
Jaideep Sharma0fa53812020-09-17 09:00:11 +053011358 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080011359 return 0;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070011360
11361adev_open_err:
11362 free_map(adev->patch_map);
11363 free_map(adev->io_streams_map);
Aniket Kumar Latabc774842020-01-16 21:22:05 -080011364 free(adev->snd_dev_ref_cnt);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070011365 pthread_mutex_destroy(&adev->lock);
11366 free(adev);
11367 adev = NULL;
11368 *device = NULL;
11369 pthread_mutex_unlock(&adev_init_lock);
11370 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080011371}
11372
11373static struct hw_module_methods_t hal_module_methods = {
11374 .open = adev_open,
11375};
11376
11377struct audio_module HAL_MODULE_INFO_SYM = {
11378 .common = {
11379 .tag = HARDWARE_MODULE_TAG,
11380 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
11381 .hal_api_version = HARDWARE_HAL_API_VERSION,
11382 .id = AUDIO_HARDWARE_MODULE_ID,
11383 .name = "QCOM Audio HAL",
Duy Truongfae19622013-11-24 02:17:54 -080011384 .author = "The Linux Foundation",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080011385 .methods = &hal_module_methods,
11386 },
11387};