blob: dec7ad66ff0dc75e6f6be8a87db021cb18658442 [file] [log] [blame]
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001/*
Ramu Gottipatifa5be522021-12-28 19:18:21 +05302 * Copyright (c) 2013-2022, The Linux Foundation. All rights reserved.
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07003 * Not a Contribution.
4 *
Shiv Maliyappanahalli8911f282014-01-10 15:56:19 -08005 * Copyright (C) 2013 The Android Open Source Project
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Jitendra Naruka1b6513f2014-11-22 19:34:13 -080018 *
19 * This file was modified by DTS, Inc. The portions of the
20 * code modified by DTS, Inc are copyrighted and
21 * licensed separately, as follows:
22 *
23 * (C) 2014 DTS, Inc.
24 *
25 * Licensed under the Apache License, Version 2.0 (the "License");
26 * you may not use this file except in compliance with the License.
27 * You may obtain a copy of the License at
28 *
29 * http://www.apache.org/licenses/LICENSE-2.0
30 *
31 * Unless required by applicable law or agreed to in writing, software
32 * distributed under the License is distributed on an "AS IS" BASIS,
33 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34 * See the License for the specific language governing permissions and
35 * limitations under the License.
Sandhya Mutha Naga Venkataa597a962023-02-07 11:35:51 +053036 *
37 * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080038 */
39
40#define LOG_TAG "audio_hw_primary"
Haynes Mathew George5beddd42016-06-27 18:33:40 -070041#define ATRACE_TAG (ATRACE_TAG_AUDIO|ATRACE_TAG_HAL)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080042/*#define LOG_NDEBUG 0*/
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070043/*#define VERY_VERY_VERBOSE_LOGGING*/
44#ifdef VERY_VERY_VERBOSE_LOGGING
45#define ALOGVV ALOGV
46#else
47#define ALOGVV(a...) do { } while(0)
48#endif
George Gao3018ede2019-10-23 13:23:00 -070049#include <limits.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080050#include <errno.h>
51#include <pthread.h>
52#include <stdint.h>
53#include <sys/time.h>
54#include <stdlib.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080055#include <math.h>
Eric Laurentc4aef752013-09-12 17:45:53 -070056#include <dlfcn.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070057#include <sys/resource.h>
58#include <sys/prctl.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080059
Aalique Grahame22e49102018-12-18 14:23:57 -080060#include <log/log.h>
Haynes Mathew George5beddd42016-06-27 18:33:40 -070061#include <cutils/trace.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080062#include <cutils/str_parms.h>
63#include <cutils/properties.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070064#include <cutils/atomic.h>
65#include <cutils/sched_policy.h>
Eric Laurentb23d5282013-05-14 15:27:20 -070066#include <hardware/audio_effect.h>
Haynes Mathew George484e8d22017-07-31 18:55:17 -070067#include <hardware/audio_alsaops.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070068#include <system/thread_defs.h>
Haynes Mathew George16081042017-05-31 17:16:49 -070069#include <tinyalsa/asoundlib.h>
Andy Hunga1f48fa2019-07-01 18:14:53 -070070#include <utils/Timers.h> // systemTime
Eric Laurentb23d5282013-05-14 15:27:20 -070071#include <audio_effects/effect_aec.h>
72#include <audio_effects/effect_ns.h>
Ashish Jainf1eaa582016-05-23 20:54:24 +053073#include <audio_utils/format.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080074#include "audio_hw.h"
Wei Wangf7ca6c92017-11-21 14:51:20 -080075#include "audio_perf.h"
Eric Laurentb23d5282013-05-14 15:27:20 -070076#include "platform_api.h"
77#include <platform.h>
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -070078#include "audio_extn.h"
Narsinga Rao Chella05573b72013-11-15 15:21:40 -080079#include "voice_extn.h"
Ashish Jaind5694242017-09-05 20:09:06 +053080#include "ip_hdlr_intf.h"
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080081
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070082#include "sound/compress_params.h"
Xiaojun Sang782e5b12020-06-29 21:13:06 +080083
84#ifdef AUDIO_GKI_ENABLED
85#include "sound/audio_compressed_formats.h"
86#endif
87
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -080088#include "sound/asound.h"
ApurupaPattapu2e084df2013-12-18 15:47:59 -080089
Daniel Hillenbrand17873952013-05-23 10:10:00 +053090#include "audio_amplifier.h"
91
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053092#ifdef DYNAMIC_LOG_ENABLED
93#include <log_xml_parser.h>
94#define LOG_MASK HAL_MOD_FILE_AUDIO_HW
95#include <log_utils.h>
96#endif
97
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070098#define COMPRESS_OFFLOAD_NUM_FRAGMENTS 4
Ashish Jain5106d362016-05-11 19:23:33 +053099/*DIRECT PCM has same buffer sizes as DEEP Buffer*/
100#define DIRECT_PCM_NUM_FRAGMENTS 2
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700101#define COMPRESS_PLAYBACK_VOLUME_MAX 0x2000
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700102#define VOIP_PLAYBACK_VOLUME_MAX 0x2000
Arun Mirpuri5d170872019-03-26 13:21:31 -0700103#define MMAP_PLAYBACK_VOLUME_MAX 0x2000
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +0530104#define PCM_PLAYBACK_VOLUME_MAX 0x2000
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530105#define DSD_VOLUME_MIN_DB (-110)
Aniket Kumar Lata8426d1f2019-06-10 15:25:53 -0700106#define INVALID_OUT_VOLUME -1
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700107#define AUDIO_IO_PORTS_MAX 32
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700108
Zhou Songbaddf9f2020-11-20 13:57:39 +0800109#define PLAYBACK_GAIN_MAX 1.0f
Aalique Grahame22e49102018-12-18 14:23:57 -0800110#define RECORD_GAIN_MIN 0.0f
111#define RECORD_GAIN_MAX 1.0f
112#define RECORD_VOLUME_CTL_MAX 0x2000
113
114/* treat as unsigned Q1.13 */
115#define APP_TYPE_GAIN_DEFAULT 0x2000
116
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -0700117#define PROXY_OPEN_RETRY_COUNT 100
118#define PROXY_OPEN_WAIT_TIME 20
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -0800119
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800120#define GET_USECASE_AUDIO_PLAYBACK_PRIMARY(db) \
121 (db)? USECASE_AUDIO_PLAYBACK_DEEP_BUFFER : \
122 USECASE_AUDIO_PLAYBACK_LOW_LATENCY
123#define GET_PCM_CONFIG_AUDIO_PLAYBACK_PRIMARY(db) \
124 (db)? pcm_config_deep_buffer : pcm_config_low_latency
Haynes Mathew Georgebf143712013-12-03 13:02:53 -0800125
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700126#define ULL_PERIOD_SIZE (DEFAULT_OUTPUT_SAMPLING_RATE/1000)
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700127#define DEFAULT_VOIP_BUF_DURATION_MS 20
128#define DEFAULT_VOIP_BIT_DEPTH_BYTE sizeof(int16_t)
129#define DEFAULT_VOIP_SAMP_RATE 48000
130
131#define VOIP_IO_BUF_SIZE(SR, DURATION_MS, BIT_DEPTH) (SR)/1000 * DURATION_MS * BIT_DEPTH
132
133struct pcm_config default_pcm_config_voip_copp = {
134 .channels = 1,
135 .rate = DEFAULT_VOIP_SAMP_RATE, /* changed when the stream is opened */
136 .period_size = VOIP_IO_BUF_SIZE(DEFAULT_VOIP_SAMP_RATE, DEFAULT_VOIP_BUF_DURATION_MS, DEFAULT_VOIP_BIT_DEPTH_BYTE)/2,
137 .period_count = 2,
138 .format = PCM_FORMAT_S16_LE,
kunleiz95b597a2017-10-23 17:07:33 +0800139 .avail_min = VOIP_IO_BUF_SIZE(DEFAULT_VOIP_SAMP_RATE, DEFAULT_VOIP_BUF_DURATION_MS, DEFAULT_VOIP_BIT_DEPTH_BYTE)/2,
140 .stop_threshold = INT_MAX,
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700141};
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700142
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700143#define MIN_CHANNEL_COUNT 1
144#define DEFAULT_CHANNEL_COUNT 2
145#define MAX_HIFI_CHANNEL_COUNT 8
146
Aalique Grahame22e49102018-12-18 14:23:57 -0800147#ifndef MAX_TARGET_SPECIFIC_CHANNEL_CNT
148#define MAX_CHANNEL_COUNT 1
149#else
150#define MAX_CHANNEL_COUNT atoi(XSTR(MAX_TARGET_SPECIFIC_CHANNEL_CNT))
151#define XSTR(x) STR(x)
152#define STR(x) #x
153#endif
154
Avinash Chandrad7296d42021-08-04 15:07:47 +0530155#define IS_USB_HIFI (MAX_HIFI_CHANNEL_COUNT >= MAX_CHANNEL_COUNT) ? \
156 true : false
157
Dechen Chai22768452021-07-30 09:29:16 +0530158#ifdef LINUX_ENABLED
159static inline int64_t audio_utils_ns_from_timespec(const struct timespec *ts)
160{
161 return ts->tv_sec * 1000000000LL + ts->tv_nsec;
162}
163#endif
164
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -0700165static unsigned int configured_low_latency_capture_period_size =
166 LOW_LATENCY_CAPTURE_PERIOD_SIZE;
167
Haynes Mathew George16081042017-05-31 17:16:49 -0700168#define MMAP_PERIOD_SIZE (DEFAULT_OUTPUT_SAMPLING_RATE/1000)
169#define MMAP_PERIOD_COUNT_MIN 32
170#define MMAP_PERIOD_COUNT_MAX 512
171#define MMAP_PERIOD_COUNT_DEFAULT (MMAP_PERIOD_COUNT_MAX)
172
Aalique Grahame22e49102018-12-18 14:23:57 -0800173/* This constant enables extended precision handling.
174 * TODO The flag is off until more testing is done.
175 */
176static const bool k_enable_extended_precision = false;
Arun Mirpurie008ed22019-03-21 11:21:04 -0700177extern int AUDIO_DEVICE_IN_ALL_CODEC_BACKEND;
Aalique Grahame22e49102018-12-18 14:23:57 -0800178
Eric Laurentb23d5282013-05-14 15:27:20 -0700179struct pcm_config pcm_config_deep_buffer = {
180 .channels = 2,
181 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
182 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
183 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
184 .format = PCM_FORMAT_S16_LE,
185 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
186 .stop_threshold = INT_MAX,
187 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
188};
189
190struct pcm_config pcm_config_low_latency = {
191 .channels = 2,
192 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
193 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
194 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
195 .format = PCM_FORMAT_S16_LE,
196 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
197 .stop_threshold = INT_MAX,
198 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
199};
200
Vignesh Kulothungana6927272019-02-20 15:17:07 -0800201struct pcm_config pcm_config_haptics_audio = {
202 .channels = 1,
203 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
204 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
205 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
206 .format = PCM_FORMAT_S16_LE,
207 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
208 .stop_threshold = INT_MAX,
209 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
210};
211
212struct pcm_config pcm_config_haptics = {
213 .channels = 1,
214 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
215 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
216 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
217 .format = PCM_FORMAT_S16_LE,
218 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
219 .stop_threshold = INT_MAX,
220 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
221};
222
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700223static int af_period_multiplier = 4;
224struct pcm_config pcm_config_rt = {
225 .channels = 2,
226 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
227 .period_size = ULL_PERIOD_SIZE, //1 ms
228 .period_count = 512, //=> buffer size is 512ms
229 .format = PCM_FORMAT_S16_LE,
230 .start_threshold = ULL_PERIOD_SIZE*8, //8ms
231 .stop_threshold = INT_MAX,
232 .silence_threshold = 0,
233 .silence_size = 0,
234 .avail_min = ULL_PERIOD_SIZE, //1 ms
235};
236
Eric Laurentb23d5282013-05-14 15:27:20 -0700237struct pcm_config pcm_config_hdmi_multi = {
238 .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
239 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
240 .period_size = HDMI_MULTI_PERIOD_SIZE,
241 .period_count = HDMI_MULTI_PERIOD_COUNT,
242 .format = PCM_FORMAT_S16_LE,
243 .start_threshold = 0,
244 .stop_threshold = INT_MAX,
245 .avail_min = 0,
246};
247
Haynes Mathew George16081042017-05-31 17:16:49 -0700248struct pcm_config pcm_config_mmap_playback = {
249 .channels = 2,
250 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
251 .period_size = MMAP_PERIOD_SIZE,
252 .period_count = MMAP_PERIOD_COUNT_DEFAULT,
253 .format = PCM_FORMAT_S16_LE,
254 .start_threshold = MMAP_PERIOD_SIZE*8,
255 .stop_threshold = INT32_MAX,
256 .silence_threshold = 0,
257 .silence_size = 0,
258 .avail_min = MMAP_PERIOD_SIZE, //1 ms
259};
260
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700261struct pcm_config pcm_config_hifi = {
262 .channels = DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
263 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
264 .period_size = HIFI_BUFFER_OUTPUT_PERIOD_SIZE, /* change #define */
265 .period_count = HIFI_BUFFER_OUTPUT_PERIOD_COUNT,
266 .format = PCM_FORMAT_S24_3LE,
267 .start_threshold = 0,
268 .stop_threshold = INT_MAX,
269 .avail_min = 0,
270};
271
Eric Laurentb23d5282013-05-14 15:27:20 -0700272struct pcm_config pcm_config_audio_capture = {
273 .channels = 2,
Eric Laurentb23d5282013-05-14 15:27:20 -0700274 .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
275 .format = PCM_FORMAT_S16_LE,
276};
277
Haynes Mathew George16081042017-05-31 17:16:49 -0700278struct pcm_config pcm_config_mmap_capture = {
279 .channels = 2,
280 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
281 .period_size = MMAP_PERIOD_SIZE,
282 .period_count = MMAP_PERIOD_COUNT_DEFAULT,
283 .format = PCM_FORMAT_S16_LE,
284 .start_threshold = 0,
285 .stop_threshold = INT_MAX,
286 .silence_threshold = 0,
287 .silence_size = 0,
288 .avail_min = MMAP_PERIOD_SIZE, //1 ms
289};
290
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -0700291#define AFE_PROXY_CHANNEL_COUNT 2
292#define AFE_PROXY_SAMPLING_RATE 48000
293
294#define AFE_PROXY_PLAYBACK_PERIOD_SIZE 768
295#define AFE_PROXY_PLAYBACK_PERIOD_COUNT 4
296
297struct pcm_config pcm_config_afe_proxy_playback = {
298 .channels = AFE_PROXY_CHANNEL_COUNT,
299 .rate = AFE_PROXY_SAMPLING_RATE,
300 .period_size = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
301 .period_count = AFE_PROXY_PLAYBACK_PERIOD_COUNT,
302 .format = PCM_FORMAT_S16_LE,
303 .start_threshold = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
304 .stop_threshold = INT_MAX,
305 .avail_min = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
306};
307
308#define AFE_PROXY_RECORD_PERIOD_SIZE 768
309#define AFE_PROXY_RECORD_PERIOD_COUNT 4
310
Aalique Grahame22e49102018-12-18 14:23:57 -0800311struct pcm_config pcm_config_audio_capture_rt = {
312 .channels = 2,
313 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
314 .period_size = ULL_PERIOD_SIZE,
315 .period_count = 512,
316 .format = PCM_FORMAT_S16_LE,
317 .start_threshold = 0,
318 .stop_threshold = AFE_PROXY_RECORD_PERIOD_SIZE * AFE_PROXY_RECORD_PERIOD_COUNT,
319 .silence_threshold = 0,
320 .silence_size = 0,
321 .avail_min = ULL_PERIOD_SIZE, //1 ms
322};
323
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +0530324struct pcm_config pcm_config_audio_capture_rt_48KHz = {
325 .channels = 2,
326 .rate = 48000,
327 .period_size = 48,
328 .period_count = 512,
329 .format = PCM_FORMAT_S16_LE,
330 .start_threshold = 0,
331 .stop_threshold = AFE_PROXY_RECORD_PERIOD_SIZE * AFE_PROXY_RECORD_PERIOD_COUNT,
332 .silence_threshold = 0,
333 .silence_size = 0,
334 .avail_min = 48, //1 ms
335};
336struct pcm_config pcm_config_audio_capture_rt_32KHz = {
337 .channels = 2,
338 .rate = 32000,
339 .period_size = 32,
340 .period_count = 512,
341 .format = PCM_FORMAT_S16_LE,
342 .start_threshold = 0,
343 .stop_threshold = AFE_PROXY_RECORD_PERIOD_SIZE * AFE_PROXY_RECORD_PERIOD_COUNT,
344 .silence_threshold = 0,
345 .silence_size = 0,
346 .avail_min = 32, //1 ms
347};
348struct pcm_config pcm_config_audio_capture_rt_24KHz = {
349 .channels = 2,
350 .rate = 24000,
351 .period_size = 24,
352 .period_count = 512,
353 .format = PCM_FORMAT_S16_LE,
354 .start_threshold = 0,
355 .stop_threshold = AFE_PROXY_RECORD_PERIOD_SIZE * AFE_PROXY_RECORD_PERIOD_COUNT,
356 .silence_threshold = 0,
357 .silence_size = 0,
358 .avail_min = 24, //1 ms
359};
360struct pcm_config pcm_config_audio_capture_rt_16KHz = {
361 .channels = 2,
362 .rate = 16000,
363 .period_size = 16,
364 .period_count = 512,
365 .format = PCM_FORMAT_S16_LE,
366 .start_threshold = 0,
367 .stop_threshold = AFE_PROXY_RECORD_PERIOD_SIZE * AFE_PROXY_RECORD_PERIOD_COUNT,
368 .silence_threshold = 0,
369 .silence_size = 0,
370 .avail_min = 16, //1 ms
371};
372struct pcm_config pcm_config_audio_capture_rt_8KHz = {
373 .channels = 2,
374 .rate = 8000,
375 .period_size = 8,
376 .period_count = 512,
377 .format = PCM_FORMAT_S16_LE,
378 .start_threshold = 0,
379 .stop_threshold = AFE_PROXY_RECORD_PERIOD_SIZE * AFE_PROXY_RECORD_PERIOD_COUNT,
380 .silence_threshold = 0,
381 .silence_size = 0,
382 .avail_min = 8, //1 ms
383};
384
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -0700385struct pcm_config pcm_config_afe_proxy_record = {
386 .channels = AFE_PROXY_CHANNEL_COUNT,
387 .rate = AFE_PROXY_SAMPLING_RATE,
388 .period_size = AFE_PROXY_RECORD_PERIOD_SIZE,
389 .period_count = AFE_PROXY_RECORD_PERIOD_COUNT,
390 .format = PCM_FORMAT_S16_LE,
391 .start_threshold = AFE_PROXY_RECORD_PERIOD_SIZE,
392 .stop_threshold = INT_MAX,
393 .avail_min = AFE_PROXY_RECORD_PERIOD_SIZE,
394};
395
Ashish Jainf1eaa582016-05-23 20:54:24 +0530396#define AUDIO_MAX_PCM_FORMATS 7
397
398const uint32_t format_to_bitwidth_table[AUDIO_MAX_PCM_FORMATS] = {
399 [AUDIO_FORMAT_DEFAULT] = 0,
400 [AUDIO_FORMAT_PCM_16_BIT] = sizeof(uint16_t),
401 [AUDIO_FORMAT_PCM_8_BIT] = sizeof(uint8_t),
402 [AUDIO_FORMAT_PCM_32_BIT] = sizeof(uint32_t),
403 [AUDIO_FORMAT_PCM_8_24_BIT] = sizeof(uint32_t),
404 [AUDIO_FORMAT_PCM_FLOAT] = sizeof(float),
405 [AUDIO_FORMAT_PCM_24_BIT_PACKED] = sizeof(uint8_t) * 3,
406};
407
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800408const char * const use_case_table[AUDIO_USECASE_MAX] = {
Eric Laurentb23d5282013-05-14 15:27:20 -0700409 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
410 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
Vignesh Kulothungana6927272019-02-20 15:17:07 -0800411 [USECASE_AUDIO_PLAYBACK_WITH_HAPTICS] = "audio-with-haptics-playback",
Meng Wang51d8c2a2020-04-27 15:23:21 +0800412 [USECASE_AUDIO_PLAYBACK_HAPTICS] = "haptics-playback",
Ravi Kumar Alamanda474de5a2015-06-25 20:08:01 -0700413 [USECASE_AUDIO_PLAYBACK_ULL] = "audio-ull-playback",
414 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback",
Shruthi Krishnaace10852013-10-25 14:32:12 -0700415 [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback",
vivek mehta446c3962015-09-14 10:57:35 -0700416 //Enabled for Direct_PCM
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700417 [USECASE_AUDIO_PLAYBACK_OFFLOAD2] = "compress-offload-playback2",
418 [USECASE_AUDIO_PLAYBACK_OFFLOAD3] = "compress-offload-playback3",
419 [USECASE_AUDIO_PLAYBACK_OFFLOAD4] = "compress-offload-playback4",
420 [USECASE_AUDIO_PLAYBACK_OFFLOAD5] = "compress-offload-playback5",
421 [USECASE_AUDIO_PLAYBACK_OFFLOAD6] = "compress-offload-playback6",
422 [USECASE_AUDIO_PLAYBACK_OFFLOAD7] = "compress-offload-playback7",
423 [USECASE_AUDIO_PLAYBACK_OFFLOAD8] = "compress-offload-playback8",
424 [USECASE_AUDIO_PLAYBACK_OFFLOAD9] = "compress-offload-playback9",
Haynes Mathew George16081042017-05-31 17:16:49 -0700425 [USECASE_AUDIO_PLAYBACK_FM] = "play-fm",
426 [USECASE_AUDIO_PLAYBACK_MMAP] = "mmap-playback",
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700427 [USECASE_AUDIO_PLAYBACK_HIFI] = "hifi-playback",
Aalique Grahame22e49102018-12-18 14:23:57 -0800428 [USECASE_AUDIO_PLAYBACK_TTS] = "audio-tts-playback",
vivek mehta0ea887a2015-08-26 14:01:20 -0700429
Eric Laurentb23d5282013-05-14 15:27:20 -0700430 [USECASE_AUDIO_RECORD] = "audio-record",
Krishna Kishor Jha0f4d8482022-02-18 10:56:05 +0530431 [USECASE_AUDIO_RECORD2] = "audio-record2",
432 [USECASE_AUDIO_RECORD3] = "audio-record3",
Mingming Yine62d7842013-10-25 16:26:03 -0700433 [USECASE_AUDIO_RECORD_COMPRESS] = "audio-record-compress",
Dhananjay Kumaree4d2002016-10-25 18:02:58 +0530434 [USECASE_AUDIO_RECORD_COMPRESS2] = "audio-record-compress2",
435 [USECASE_AUDIO_RECORD_COMPRESS3] = "audio-record-compress3",
436 [USECASE_AUDIO_RECORD_COMPRESS4] = "audio-record-compress4",
Dhananjay Kumar376e38b2017-09-28 22:26:23 +0530437 [USECASE_AUDIO_RECORD_COMPRESS5] = "audio-record-compress5",
438 [USECASE_AUDIO_RECORD_COMPRESS6] = "audio-record-compress6",
Eric Laurentb23d5282013-05-14 15:27:20 -0700439 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
Preetam Singh Ranawatde84f1a2013-11-01 14:58:16 -0700440 [USECASE_AUDIO_RECORD_FM_VIRTUAL] = "fm-virtual-record",
Haynes Mathew George16081042017-05-31 17:16:49 -0700441 [USECASE_AUDIO_RECORD_MMAP] = "mmap-record",
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700442 [USECASE_AUDIO_RECORD_HIFI] = "hifi-record",
Haynes Mathew George16081042017-05-31 17:16:49 -0700443
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -0800444 [USECASE_AUDIO_HFP_SCO] = "hfp-sco",
Vimal Puthanveed47e64852013-12-20 13:23:39 -0800445 [USECASE_AUDIO_HFP_SCO_WB] = "hfp-sco-wb",
Derek Chenf7092792017-05-23 12:23:53 -0400446 [USECASE_AUDIO_HFP_SCO_DOWNLINK] = "hfp-sco-downlink",
447 [USECASE_AUDIO_HFP_SCO_WB_DOWNLINK] = "hfp-sco-wb-downlink",
Mingming Yin3ee55c62014-08-04 14:23:35 -0700448
Derek Chenf7092792017-05-23 12:23:53 -0400449 [USECASE_VOICE_CALL] = "voice-call",
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700450 [USECASE_VOICE2_CALL] = "voice2-call",
451 [USECASE_VOLTE_CALL] = "volte-call",
452 [USECASE_QCHAT_CALL] = "qchat-call",
Vicky Sehrawat7e4fc152014-02-12 17:58:59 -0800453 [USECASE_VOWLAN_CALL] = "vowlan-call",
Vidyakumar Athota0e109352015-02-12 17:38:22 -0800454 [USECASE_VOICEMMODE1_CALL] = "voicemmode1-call",
455 [USECASE_VOICEMMODE2_CALL] = "voicemmode2-call",
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800456 [USECASE_COMPRESS_VOIP_CALL] = "compress-voip-call",
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700457 [USECASE_INCALL_REC_UPLINK] = "incall-rec-uplink",
458 [USECASE_INCALL_REC_DOWNLINK] = "incall-rec-downlink",
459 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK] = "incall-rec-uplink-and-downlink",
Helen Zenge56b4852013-12-03 16:54:40 -0800460 [USECASE_INCALL_REC_UPLINK_COMPRESS] = "incall-rec-uplink-compress",
461 [USECASE_INCALL_REC_DOWNLINK_COMPRESS] = "incall-rec-downlink-compress",
462 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK_COMPRESS] = "incall-rec-uplink-and-downlink-compress",
463
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -0700464 [USECASE_INCALL_MUSIC_UPLINK] = "incall_music_uplink",
465 [USECASE_INCALL_MUSIC_UPLINK2] = "incall_music_uplink2",
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700466 [USECASE_AUDIO_SPKR_CALIB_RX] = "spkr-rx-calib",
467 [USECASE_AUDIO_SPKR_CALIB_TX] = "spkr-vi-record",
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -0700468
469 [USECASE_AUDIO_PLAYBACK_AFE_PROXY] = "afe-proxy-playback",
470 [USECASE_AUDIO_RECORD_AFE_PROXY] = "afe-proxy-record",
Zhou Song62ea0282020-03-22 19:53:01 +0800471 [USECASE_AUDIO_RECORD_AFE_PROXY2] = "afe-proxy-record2",
Md Mansoor Ahmeddb1b4f92018-01-25 18:56:31 +0530472 [USECASE_AUDIO_PLAYBACK_SILENCE] = "silence-playback",
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700473
Siddartha Shaik31b530e2017-05-19 15:26:33 +0530474 /* Transcode loopback cases */
Surendar Karka93cd25a2018-08-28 14:21:37 +0530475 [USECASE_AUDIO_TRANSCODE_LOOPBACK_RX] = "audio-transcode-loopback-rx",
476 [USECASE_AUDIO_TRANSCODE_LOOPBACK_TX] = "audio-transcode-loopback-tx",
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700477
478 [USECASE_AUDIO_PLAYBACK_VOIP] = "audio-playback-voip",
479 [USECASE_AUDIO_RECORD_VOIP] = "audio-record-voip",
Revathi Uddarajud9f23d92020-07-27 10:55:06 +0530480 [USECASE_AUDIO_RECORD_VOIP_LOW_LATENCY] = "audio-record-voip-low-latency",
Varun Balaraje49253e2017-07-06 19:48:56 +0530481 /* For Interactive Audio Streams */
482 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM1] = "audio-interactive-stream1",
483 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM2] = "audio-interactive-stream2",
484 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM3] = "audio-interactive-stream3",
485 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM4] = "audio-interactive-stream4",
486 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM5] = "audio-interactive-stream5",
487 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM6] = "audio-interactive-stream6",
488 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM7] = "audio-interactive-stream7",
489 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM8] = "audio-interactive-stream8",
Garmond Leunge2433c32017-09-28 21:51:22 -0700490
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800491 [USECASE_AUDIO_EC_REF_LOOPBACK] = "ec-ref-audio-capture",
492
Derek Chenf6318be2017-06-12 17:16:24 -0400493 [USECASE_AUDIO_A2DP_ABR_FEEDBACK] = "a2dp-abr-feedback",
494
495 [USECASE_AUDIO_PLAYBACK_MEDIA] = "media-playback",
496 [USECASE_AUDIO_PLAYBACK_SYS_NOTIFICATION] = "sys-notification-playback",
497 [USECASE_AUDIO_PLAYBACK_NAV_GUIDANCE] = "nav-guidance-playback",
498 [USECASE_AUDIO_PLAYBACK_PHONE] = "phone-playback",
Susan Wang117cf6f2022-04-06 20:11:46 -0400499 [USECASE_AUDIO_PLAYBACK_ALERTS] = "alerts-playback",
Derek Chen1bcdc6b2020-02-06 22:44:53 -0800500 [USECASE_AUDIO_PLAYBACK_FRONT_PASSENGER] = "front-passenger-playback",
Derek Chendf05eea2019-08-01 13:57:49 -0700501 [USECASE_AUDIO_PLAYBACK_REAR_SEAT] = "rear-seat-playback",
Rahul Sharma99770982019-03-06 17:05:26 +0530502 [USECASE_AUDIO_FM_TUNER_EXT] = "fm-tuner-ext",
Derek Chena30a5f42019-12-03 11:17:09 -0500503 [USECASE_ICC_CALL] = "icc-call",
Huicheng Liu1404ba12020-09-11 01:03:25 -0400504
505 [USECASE_AUDIO_RECORD_BUS] = "audio-record",
506 [USECASE_AUDIO_RECORD_BUS_FRONT_PASSENGER] = "front-passenger-record",
507 [USECASE_AUDIO_RECORD_BUS_REAR_SEAT] = "rear-seat-record",
Fei Tongaffdf732020-02-20 20:39:05 +0800508 [USECASE_AUDIO_PLAYBACK_SYNTHESIZER] = "synth-loopback",
Susan Wange3959562021-03-11 11:50:26 -0500509 [USECASE_AUDIO_RECORD_ECHO_REF_EXT] = "echo-reference-external",
Eric Laurentb23d5282013-05-14 15:27:20 -0700510};
511
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700512static const audio_usecase_t offload_usecases[] = {
513 USECASE_AUDIO_PLAYBACK_OFFLOAD,
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700514 USECASE_AUDIO_PLAYBACK_OFFLOAD2,
515 USECASE_AUDIO_PLAYBACK_OFFLOAD3,
516 USECASE_AUDIO_PLAYBACK_OFFLOAD4,
517 USECASE_AUDIO_PLAYBACK_OFFLOAD5,
518 USECASE_AUDIO_PLAYBACK_OFFLOAD6,
519 USECASE_AUDIO_PLAYBACK_OFFLOAD7,
520 USECASE_AUDIO_PLAYBACK_OFFLOAD8,
521 USECASE_AUDIO_PLAYBACK_OFFLOAD9,
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700522};
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800523
Varun Balaraje49253e2017-07-06 19:48:56 +0530524static const audio_usecase_t interactive_usecases[] = {
525 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM1,
526 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM2,
527 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM3,
528 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM4,
529 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM5,
530 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM6,
531 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM7,
532 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM8,
533};
534
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800535#define STRING_TO_ENUM(string) { #string, string }
536
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800537struct string_to_enum {
538 const char *name;
539 uint32_t value;
540};
541
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700542static const struct string_to_enum channels_name_to_enum_table[] = {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800543 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
Mingming Yin3a941d42016-02-17 18:08:05 -0800544 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_2POINT1),
545 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_QUAD),
546 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_SURROUND),
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -0700547 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_PENTA),
Mingming Yin3a941d42016-02-17 18:08:05 -0800548 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
549 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_6POINT1),
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800550 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700551 STRING_TO_ENUM(AUDIO_CHANNEL_IN_MONO),
552 STRING_TO_ENUM(AUDIO_CHANNEL_IN_STEREO),
553 STRING_TO_ENUM(AUDIO_CHANNEL_IN_FRONT_BACK),
554 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_1),
555 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_2),
556 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_3),
557 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_4),
558 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_5),
559 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_6),
560 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_7),
561 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_8),
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800562};
563
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700564static const struct string_to_enum formats_name_to_enum_table[] = {
565 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
566 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
567 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Pradnya Chaphekar8a9dcd82014-09-09 09:49:10 -0700568 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
569 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
570 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Ben Romberger1aaaf862017-04-06 17:49:46 -0700571 STRING_TO_ENUM(AUDIO_FORMAT_DOLBY_TRUEHD),
Mingming Yin3a941d42016-02-17 18:08:05 -0800572 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
573 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
Naresh Tanniru928f0862017-04-07 16:44:23 -0700574 STRING_TO_ENUM(AUDIO_FORMAT_IEC61937)
Mingming Yin3a941d42016-02-17 18:08:05 -0800575};
576
577//list of all supported sample rates by HDMI specification.
578static const int out_hdmi_sample_rates[] = {
579 32000, 44100, 48000, 88200, 96000, 176400, 192000,
580};
581
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700582static const struct string_to_enum out_sample_rates_name_to_enum_table[] = {
Mingming Yin3a941d42016-02-17 18:08:05 -0800583 STRING_TO_ENUM(32000),
584 STRING_TO_ENUM(44100),
585 STRING_TO_ENUM(48000),
586 STRING_TO_ENUM(88200),
587 STRING_TO_ENUM(96000),
588 STRING_TO_ENUM(176400),
589 STRING_TO_ENUM(192000),
Mingshu pange8aedf12019-10-25 15:38:27 +0800590 STRING_TO_ENUM(352800),
591 STRING_TO_ENUM(384000),
Pradnya Chaphekar8a9dcd82014-09-09 09:49:10 -0700592};
593
Carter Hsu2e429db2019-05-14 18:50:52 +0800594struct in_effect_list {
595 struct listnode list;
596 effect_handle_t handle;
597};
598
Krishna Kishor Jha0f4d8482022-02-18 10:56:05 +0530599static const audio_usecase_t record_usecases[] = {
600 USECASE_AUDIO_RECORD,
601 USECASE_AUDIO_RECORD2,
602 USECASE_AUDIO_RECORD3,
603};
604
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700605static struct audio_device *adev = NULL;
Ben Romberger02ab1192018-05-24 12:10:08 -0700606static pthread_mutex_t adev_init_lock = PTHREAD_MUTEX_INITIALIZER;
Kiran Kandi910e1862013-10-29 13:29:42 -0700607static unsigned int audio_device_ref_count;
vivek mehtab72d08d2016-04-29 03:16:47 -0700608//cache last MBDRC cal step level
609static int last_known_cal_step = -1 ;
Kiran Kandi910e1862013-10-29 13:29:42 -0700610
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +0530611static int out_set_compr_volume(struct audio_stream_out *stream, float left, float right);
Arun Mirpuri5d170872019-03-26 13:21:31 -0700612static int out_set_mmap_volume(struct audio_stream_out *stream, float left, float right);
Zhou Song2b8f28f2017-09-11 10:51:38 +0800613static int out_set_voip_volume(struct audio_stream_out *stream, float left, float right);
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +0530614static int out_set_pcm_volume(struct audio_stream_out *stream, float left, float right);
Krishna Kishor Jha10ad39f2022-02-21 17:02:47 +0530615#ifdef SOFT_VOLUME
616static int out_set_soft_volume_params(struct audio_stream_out *stream);
617#endif
Derek Chen6f293672019-04-01 01:40:24 -0700618static void adev_snd_mon_cb(void *cookie, struct str_parms *parms);
619static void in_snd_mon_cb(void * stream, struct str_parms * parms);
620static void out_snd_mon_cb(void * stream, struct str_parms * parms);
621
Zhou Song331c8e52019-08-26 14:16:12 +0800622static int configure_btsco_sample_rate(snd_device_t snd_device);
623
Vatsal Buchac09ae062018-11-14 13:25:08 +0530624#ifdef AUDIO_FEATURE_ENABLED_GCOV
625extern void __gcov_flush();
626static void enable_gcov()
627{
628 __gcov_flush();
629}
630#else
631static void enable_gcov()
632{
633}
634#endif
635
justinweng20fb6d82019-02-21 18:49:00 -0700636static int in_set_microphone_direction(const struct audio_stream_in *stream,
637 audio_microphone_direction_t dir);
638static int in_set_microphone_field_dimension(const struct audio_stream_in *stream, float zoom);
639
Krishna Kishor Jha0f4d8482022-02-18 10:56:05 +0530640static bool is_pcm_record_usecase(audio_usecase_t uc_id)
641{
642 unsigned int record_uc_index;
643 unsigned int num_usecase = sizeof(record_usecases)/sizeof(record_usecases[0]);
644
645 for (record_uc_index = 0; record_uc_index < num_usecase; record_uc_index++) {
646 if (uc_id == record_usecases[record_uc_index])
647 return true;
648 }
649 return false;
650}
651
652static audio_usecase_t get_record_usecase(struct audio_device *adev)
653{
654 audio_usecase_t ret_uc = USECASE_INVALID;
655 unsigned int record_uc_index;
656 unsigned int num_usecase = sizeof(record_usecases)/sizeof(record_usecases[0]);
657
658 ALOGV("%s: num_usecase: %d", __func__, num_usecase);
659 for (record_uc_index = 0; record_uc_index < num_usecase; record_uc_index++) {
660 if (!(adev->pcm_record_uc_state & (0x1 << record_uc_index))) {
661 adev->pcm_record_uc_state |= 0x1 << record_uc_index;
662 ret_uc = record_usecases[record_uc_index];
663 break;
664 }
665 }
666
667 ALOGV("%s: pcm record usecase is %d", __func__, ret_uc);
668 return ret_uc;
669}
670
671static void free_record_usecase(struct audio_device *adev,
672 audio_usecase_t uc_id)
673{
674 unsigned int record_uc_index;
675 unsigned int num_usecase = sizeof(record_usecases)/sizeof(record_usecases[0]);
676
677 for (record_uc_index = 0; record_uc_index < num_usecase; record_uc_index++) {
678 if (record_usecases[record_uc_index] == uc_id) {
679 adev->pcm_record_uc_state &= ~(0x1 << record_uc_index);
680 break;
681 }
682 }
683 ALOGV("%s: free pcm record usecase %d", __func__, uc_id);
684}
685
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700686static bool may_use_noirq_mode(struct audio_device *adev, audio_usecase_t uc_id,
687 int flags __unused)
688{
689 int dir = 0;
690 switch (uc_id) {
691 case USECASE_AUDIO_RECORD_LOW_LATENCY:
Revathi Uddarajud9f23d92020-07-27 10:55:06 +0530692 case USECASE_AUDIO_RECORD_VOIP_LOW_LATENCY:
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700693 dir = 1;
694 case USECASE_AUDIO_PLAYBACK_ULL:
695 break;
696 default:
697 return false;
698 }
699
700 int dev_id = platform_get_pcm_device_id(uc_id, dir == 0 ?
701 PCM_PLAYBACK : PCM_CAPTURE);
702 if (adev->adm_is_noirq_avail)
703 return adev->adm_is_noirq_avail(adev->adm_data,
704 adev->snd_card, dev_id, dir);
705 return false;
706}
707
708static void register_out_stream(struct stream_out *out)
709{
710 struct audio_device *adev = out->dev;
711 if (is_offload_usecase(out->usecase) ||
712 !adev->adm_register_output_stream)
713 return;
714
715 // register stream first for backward compatibility
716 adev->adm_register_output_stream(adev->adm_data,
717 out->handle,
718 out->flags);
719
720 if (!adev->adm_set_config)
721 return;
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +0530722#ifdef PLATFORM_AUTO
Kogara Naveen Kumar6db5fb02022-05-07 00:22:50 +0530723 if (out->realtime || (out->flags & AUDIO_OUTPUT_FLAG_SYS_NOTIFICATION))
724 adev->adm_set_config(adev->adm_data,
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700725 out->handle,
726 out->pcm, &out->config);
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +0530727#else
728 if (out->realtime)
729 adev->adm_set_config(adev->adm_data,
730 out->handle,
731 out->pcm, &out->config);
732#endif
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700733}
734
735static void register_in_stream(struct stream_in *in)
736{
737 struct audio_device *adev = in->dev;
738 if (!adev->adm_register_input_stream)
739 return;
740
741 adev->adm_register_input_stream(adev->adm_data,
742 in->capture_handle,
743 in->flags);
744
745 if (!adev->adm_set_config)
746 return;
747
748 if (in->realtime)
749 adev->adm_set_config(adev->adm_data,
750 in->capture_handle,
751 in->pcm,
752 &in->config);
753}
754
755static void request_out_focus(struct stream_out *out, long ns)
756{
757 struct audio_device *adev = out->dev;
758
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700759 if (adev->adm_request_focus_v2)
760 adev->adm_request_focus_v2(adev->adm_data, out->handle, ns);
761 else if (adev->adm_request_focus)
762 adev->adm_request_focus(adev->adm_data, out->handle);
763}
764
Aniket Kumar Lata60586a92019-05-22 22:18:55 -0700765static int request_in_focus(struct stream_in *in, long ns)
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700766{
767 struct audio_device *adev = in->dev;
Aniket Kumar Lata60586a92019-05-22 22:18:55 -0700768 int ret = 0;
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700769
Aniket Kumar Lata60586a92019-05-22 22:18:55 -0700770 if (adev->adm_request_focus_v2_1)
771 ret = adev->adm_request_focus_v2_1(adev->adm_data, in->capture_handle, ns);
772 else if (adev->adm_request_focus_v2)
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700773 adev->adm_request_focus_v2(adev->adm_data, in->capture_handle, ns);
774 else if (adev->adm_request_focus)
775 adev->adm_request_focus(adev->adm_data, in->capture_handle);
Aniket Kumar Lata60586a92019-05-22 22:18:55 -0700776
777 return ret;
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700778}
779
780static void release_out_focus(struct stream_out *out)
781{
782 struct audio_device *adev = out->dev;
783
784 if (adev->adm_abandon_focus)
785 adev->adm_abandon_focus(adev->adm_data, out->handle);
786}
787
788static void release_in_focus(struct stream_in *in)
789{
790 struct audio_device *adev = in->dev;
791 if (adev->adm_abandon_focus)
792 adev->adm_abandon_focus(adev->adm_data, in->capture_handle);
793}
794
Dhananjay Kumare6293dd2017-05-25 17:25:30 +0530795static int parse_snd_card_status(struct str_parms *parms, int *card,
796 card_status_t *status)
797{
798 char value[32]={0};
799 char state[32]={0};
800
801 int ret = str_parms_get_str(parms, "SND_CARD_STATUS", value, sizeof(value));
802 if (ret < 0)
803 return -1;
804
805 // sscanf should be okay as value is of max length 32.
806 // same as sizeof state.
807 if (sscanf(value, "%d,%s", card, state) < 2)
808 return -1;
809
810 *status = !strcmp(state, "ONLINE") ? CARD_STATUS_ONLINE :
811 CARD_STATUS_OFFLINE;
812 return 0;
813}
814
Avinash Chandrad7296d42021-08-04 15:07:47 +0530815bool is_combo_audio_input_device(struct listnode *devices){
816
Sandhya Mutha Naga Venkata153d95e2022-07-12 14:54:43 +0530817 if ((devices == NULL) || (!list_empty(devices)))
Avinash Chandrad7296d42021-08-04 15:07:47 +0530818 return false;
819
820 if(compare_device_type(devices, AUDIO_DEVICE_IN_BUILTIN_MIC|AUDIO_DEVICE_IN_SPEAKER_MIC2))
821 return true;
822 else
823 return false;
824}
825
Haynes Mathew Georgeb0f5dc32017-10-06 18:35:12 -0700826static inline void adjust_frames_for_device_delay(struct stream_out *out,
827 uint32_t *dsp_frames) {
828 // Adjustment accounts for A2dp encoder latency with offload usecases
829 // Note: Encoder latency is returned in ms.
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800830 if (is_a2dp_out_device_type(&out->device_list)) {
Haynes Mathew Georgeb0f5dc32017-10-06 18:35:12 -0700831 unsigned long offset =
832 (audio_extn_a2dp_get_encoder_latency() * out->sample_rate / 1000);
833 *dsp_frames = (*dsp_frames > offset) ? (*dsp_frames - offset) : 0;
834 }
835}
836
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700837static inline bool free_entry(void *key __unused,
838 void *value, void *context __unused)
839{
840 free(value);
841 return true;
842}
843
844static inline void free_map(Hashmap *map)
845{
846 if (map) {
847 hashmapForEach(map, free_entry, (void *) NULL);
848 hashmapFree(map);
849 }
850}
851
Aniket Kumar Latabc774842020-01-16 21:22:05 -0800852static inline void patch_map_remove_l(struct audio_device *adev,
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700853 audio_patch_handle_t patch_handle)
854{
855 if (patch_handle == AUDIO_PATCH_HANDLE_NONE)
856 return;
857
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700858 struct audio_patch_info *p_info =
859 hashmapGet(adev->patch_map, (void *) (intptr_t) patch_handle);
860 if (p_info) {
861 ALOGV("%s: Remove patch %d", __func__, patch_handle);
862 hashmapRemove(adev->patch_map, (void *) (intptr_t) patch_handle);
863 free(p_info->patch);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700864 free(p_info);
865 }
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700866}
867
868static inline int io_streams_map_insert(struct audio_device *adev,
869 struct audio_stream *stream,
870 audio_io_handle_t handle,
871 audio_patch_handle_t patch_handle)
872{
873 struct audio_stream_info *s_info =
874 (struct audio_stream_info *) calloc(1, sizeof(struct audio_stream_info));
875
876 if (s_info == NULL) {
877 ALOGE("%s: Could not allocate stream info", __func__);
878 return -ENOMEM;
879 }
880 s_info->stream = stream;
881 s_info->patch_handle = patch_handle;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700882
883 pthread_mutex_lock(&adev->lock);
884 struct audio_stream_info *stream_info =
885 hashmapPut(adev->io_streams_map, (void *) (intptr_t) handle, (void *) s_info);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700886 if (stream_info != NULL)
887 free(stream_info);
Aniket Kumar Latabc774842020-01-16 21:22:05 -0800888 pthread_mutex_unlock(&adev->lock);
Aniket Kumar Latad13758f2020-08-06 15:11:36 -0700889 ALOGV("%s: Added stream in io_streams_map with handle %d", __func__, handle);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700890 return 0;
891}
892
893static inline void io_streams_map_remove(struct audio_device *adev,
894 audio_io_handle_t handle)
895{
896 pthread_mutex_lock(&adev->lock);
897 struct audio_stream_info *s_info =
898 hashmapRemove(adev->io_streams_map, (void *) (intptr_t) handle);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700899 if (s_info == NULL)
Aniket Kumar Latabc774842020-01-16 21:22:05 -0800900 goto done;
Aniket Kumar Latad13758f2020-08-06 15:11:36 -0700901 ALOGV("%s: Removed stream with handle %d", __func__, handle);
Aniket Kumar Latabc774842020-01-16 21:22:05 -0800902 patch_map_remove_l(adev, s_info->patch_handle);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700903 free(s_info);
Aniket Kumar Latabc774842020-01-16 21:22:05 -0800904done:
905 pthread_mutex_unlock(&adev->lock);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700906 return;
907}
908
Aniket Kumar Latabc774842020-01-16 21:22:05 -0800909static struct audio_patch_info* fetch_patch_info_l(struct audio_device *adev,
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700910 audio_patch_handle_t handle)
911{
912 struct audio_patch_info *p_info = NULL;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700913 p_info = (struct audio_patch_info *)
914 hashmapGet(adev->patch_map, (void *) (intptr_t) handle);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700915 return p_info;
916}
917
vivek mehtaa76401a2015-04-24 14:12:15 -0700918__attribute__ ((visibility ("default")))
919bool audio_hw_send_gain_dep_calibration(int level) {
920 bool ret_val = false;
vivek mehtab72d08d2016-04-29 03:16:47 -0700921 ALOGV("%s: called ...", __func__);
vivek mehtaa76401a2015-04-24 14:12:15 -0700922
923 pthread_mutex_lock(&adev_init_lock);
924
925 if (adev != NULL && adev->platform != NULL) {
926 pthread_mutex_lock(&adev->lock);
927 ret_val = platform_send_gain_dep_cal(adev->platform, level);
vivek mehtab72d08d2016-04-29 03:16:47 -0700928
Preetam Singh Ranawatf4ae0222017-05-31 17:07:28 +0530929 // cache level info for any of the use case which
930 // was not started.
931 last_known_cal_step = level;;
vivek mehtab72d08d2016-04-29 03:16:47 -0700932
vivek mehtaa76401a2015-04-24 14:12:15 -0700933 pthread_mutex_unlock(&adev->lock);
934 } else {
935 ALOGE("%s: %s is NULL", __func__, adev == NULL ? "adev" : "adev->platform");
936 }
937
938 pthread_mutex_unlock(&adev_init_lock);
939
940 return ret_val;
941}
942
Ashish Jain5106d362016-05-11 19:23:33 +0530943static int check_and_set_gapless_mode(struct audio_device *adev, bool enable_gapless)
944{
Krishnankutty Kolathappilly6d8788b2014-01-09 12:45:31 -0800945 bool gapless_enabled = false;
946 const char *mixer_ctl_name = "Compress Gapless Playback";
947 struct mixer_ctl *ctl;
948
949 ALOGV("%s:", __func__);
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700950 gapless_enabled = property_get_bool("vendor.audio.offload.gapless.enabled", false);
Ashish Jain5106d362016-05-11 19:23:33 +0530951
952 /*Disable gapless if its AV playback*/
953 gapless_enabled = gapless_enabled && enable_gapless;
Krishnankutty Kolathappilly6d8788b2014-01-09 12:45:31 -0800954
955 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
956 if (!ctl) {
957 ALOGE("%s: Could not get ctl for mixer cmd - %s",
958 __func__, mixer_ctl_name);
959 return -EINVAL;
960 }
961
962 if (mixer_ctl_set_value(ctl, 0, gapless_enabled) < 0) {
963 ALOGE("%s: Could not set gapless mode %d",
964 __func__, gapless_enabled);
965 return -EINVAL;
966 }
967 return 0;
968}
Haynes Mathew George5191a852013-09-11 14:19:36 -0700969
Aniket Kumar Lataf56b6402016-10-27 12:03:18 -0700970__attribute__ ((visibility ("default")))
971int audio_hw_get_gain_level_mapping(struct amp_db_and_gain_table *mapping_tbl,
972 int table_size) {
973 int ret_val = 0;
974 ALOGV("%s: enter ... ", __func__);
975
976 pthread_mutex_lock(&adev_init_lock);
977 if (adev == NULL) {
978 ALOGW("%s: adev is NULL .... ", __func__);
979 goto done;
980 }
981
982 pthread_mutex_lock(&adev->lock);
983 ret_val = platform_get_gain_level_mapping(mapping_tbl, table_size);
984 pthread_mutex_unlock(&adev->lock);
985done:
986 pthread_mutex_unlock(&adev_init_lock);
987 ALOGV("%s: exit ... ", __func__);
988 return ret_val;
989}
990
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800991bool audio_hw_send_qdsp_parameter(int stream_type, float vol, bool active)
Aalique Grahame22e49102018-12-18 14:23:57 -0800992{
993 bool ret = false;
994 ALOGV("%s: enter ...", __func__);
995
996 pthread_mutex_lock(&adev_init_lock);
997
998 if (adev != NULL && adev->platform != NULL) {
999 pthread_mutex_lock(&adev->lock);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001000 ret = audio_extn_qdsp_set_state(adev, stream_type, vol, active);
Aalique Grahame22e49102018-12-18 14:23:57 -08001001 pthread_mutex_unlock(&adev->lock);
1002 }
1003
1004 pthread_mutex_unlock(&adev_init_lock);
1005
1006 ALOGV("%s: exit with ret %d", __func__, ret);
1007 return ret;
1008}
Aalique Grahame22e49102018-12-18 14:23:57 -08001009
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001010static bool is_supported_format(audio_format_t format)
1011{
Eric Laurent86e17132013-09-12 17:49:30 -07001012 if (format == AUDIO_FORMAT_MP3 ||
Satish Babu Patakokila0c313922016-12-08 12:07:08 +05301013 format == AUDIO_FORMAT_MP2 ||
Ashish Jainf9b78162014-08-25 20:36:25 +05301014 format == AUDIO_FORMAT_AAC_LC ||
1015 format == AUDIO_FORMAT_AAC_HE_V1 ||
1016 format == AUDIO_FORMAT_AAC_HE_V2 ||
Manish Dewangana6fc5442015-08-24 20:30:31 +05301017 format == AUDIO_FORMAT_AAC_ADTS_LC ||
1018 format == AUDIO_FORMAT_AAC_ADTS_HE_V1 ||
1019 format == AUDIO_FORMAT_AAC_ADTS_HE_V2 ||
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05301020 format == AUDIO_FORMAT_AAC_LATM_LC ||
1021 format == AUDIO_FORMAT_AAC_LATM_HE_V1 ||
1022 format == AUDIO_FORMAT_AAC_LATM_HE_V2 ||
Ashish Jain5106d362016-05-11 19:23:33 +05301023 format == AUDIO_FORMAT_PCM_24_BIT_PACKED ||
1024 format == AUDIO_FORMAT_PCM_8_24_BIT ||
Ashish Jainf1eaa582016-05-23 20:54:24 +05301025 format == AUDIO_FORMAT_PCM_FLOAT ||
1026 format == AUDIO_FORMAT_PCM_32_BIT ||
vivek mehta0ea887a2015-08-26 14:01:20 -07001027 format == AUDIO_FORMAT_PCM_16_BIT ||
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05301028 format == AUDIO_FORMAT_AC3 ||
1029 format == AUDIO_FORMAT_E_AC3 ||
Ben Romberger1aaaf862017-04-06 17:49:46 -07001030 format == AUDIO_FORMAT_DOLBY_TRUEHD ||
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05301031 format == AUDIO_FORMAT_DTS ||
1032 format == AUDIO_FORMAT_DTS_HD ||
Weiyin Jiang18ac4e92015-03-15 15:03:40 +08001033 format == AUDIO_FORMAT_FLAC ||
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +05301034 format == AUDIO_FORMAT_ALAC ||
1035 format == AUDIO_FORMAT_APE ||
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05301036 format == AUDIO_FORMAT_DSD ||
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +05301037 format == AUDIO_FORMAT_VORBIS ||
Weiyin Jiang18ac4e92015-03-15 15:03:40 +08001038 format == AUDIO_FORMAT_WMA ||
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05301039 format == AUDIO_FORMAT_WMA_PRO ||
Naresh Tanniru928f0862017-04-07 16:44:23 -07001040 format == AUDIO_FORMAT_APTX ||
1041 format == AUDIO_FORMAT_IEC61937)
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08001042 return true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001043
1044 return false;
1045}
1046
Kunlei Zhang67cc7072020-12-18 17:16:49 +08001047static bool is_supported_conc_usecase_for_power_mode_call(struct audio_device *adev)
1048{
1049 struct listnode *node;
1050 struct audio_usecase *usecase;
1051
1052 list_for_each(node, &adev->usecase_list) {
1053 usecase = node_to_item(node, struct audio_usecase, list);
1054 if (usecase->id == USECASE_AUDIO_PLAYBACK_FM) {
1055 ALOGD("%s: FM usecase is active, not setting power mode", __func__);
1056 return false;
1057 }
1058 }
1059
1060 return true;
1061}
Haynes Mathew George5beddd42016-06-27 18:33:40 -07001062static inline bool is_mmap_usecase(audio_usecase_t uc_id)
1063{
1064 return (uc_id == USECASE_AUDIO_RECORD_AFE_PROXY) ||
Zhou Song62ea0282020-03-22 19:53:01 +08001065 (uc_id == USECASE_AUDIO_RECORD_AFE_PROXY2) ||
Haynes Mathew George5beddd42016-06-27 18:33:40 -07001066 (uc_id == USECASE_AUDIO_PLAYBACK_AFE_PROXY);
1067}
1068
Aniket Kumar Lata8426d1f2019-06-10 15:25:53 -07001069static inline bool is_valid_volume(float left, float right)
1070{
1071 return ((left >= 0.0f && right >= 0.0f) ? true : false);
1072}
1073
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +05301074static void enable_asrc_mode(struct audio_device *adev)
Preetam Singh Ranawatb0c0dd72016-08-18 00:32:06 +05301075{
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +05301076 ALOGV("%s", __func__);
1077 audio_route_apply_and_update_path(adev->audio_route,
1078 "asrc-mode");
1079 adev->asrc_mode_enabled = true;
1080}
1081
1082static void disable_asrc_mode(struct audio_device *adev)
1083{
1084 ALOGV("%s", __func__);
1085 audio_route_reset_and_update_path(adev->audio_route,
1086 "asrc-mode");
1087 adev->asrc_mode_enabled = false;
1088}
1089
Saurav Kumarc1411662020-10-14 10:50:45 +05301090static void check_and_configure_headphone(struct audio_device *adev,
1091 struct audio_usecase *uc_info,
1092 snd_device_t snd_device)
1093{
1094 struct listnode *node;
1095 struct audio_usecase *usecase;
1096 int new_backend_idx, usecase_backend_idx;
1097 bool spkr_hph_single_be_native_concurrency;
1098
1099 new_backend_idx = platform_get_backend_index(snd_device);
1100 spkr_hph_single_be_native_concurrency = platform_get_spkr_hph_single_be_native_concurrency_flag();
Zhou Songd4b18c42021-01-14 15:15:29 +08001101 if ((spkr_hph_single_be_native_concurrency && (new_backend_idx == DEFAULT_CODEC_BACKEND)) ||
1102 uc_info->id == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) {
Saurav Kumarc1411662020-10-14 10:50:45 +05301103 list_for_each(node, &adev->usecase_list) {
1104 usecase = node_to_item(node, struct audio_usecase, list);
1105 if ((usecase->type != PCM_CAPTURE) && (usecase != uc_info)) {
1106 usecase_backend_idx = platform_get_backend_index(usecase->out_snd_device);
1107 if (((usecase_backend_idx == HEADPHONE_BACKEND) ||
1108 (usecase_backend_idx == HEADPHONE_44_1_BACKEND)) &&
1109 ((usecase->stream.out->sample_rate % OUTPUT_SAMPLING_RATE_44100) == 0)) {
1110 disable_audio_route(adev, usecase);
1111 disable_snd_device(adev, usecase->out_snd_device);
1112 usecase->stream.out->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Saurav Kumar70902382020-10-28 11:16:04 +05301113 platform_check_and_set_codec_backend_cfg(adev, usecase,
1114 usecase->out_snd_device);
Saurav Kumarc1411662020-10-14 10:50:45 +05301115 enable_snd_device(adev, usecase->out_snd_device);
Zhou Songd4b18c42021-01-14 15:15:29 +08001116 enable_audio_route(adev, usecase);
Saurav Kumarc1411662020-10-14 10:50:45 +05301117 }
1118 }
Revathi Uddaraju2c0eac02021-07-27 02:06:42 -07001119 else if ((usecase->type != PCM_CAPTURE) && (usecase == uc_info)) {
1120 usecase_backend_idx = platform_get_backend_index(usecase->out_snd_device);
1121 if (((usecase_backend_idx == HEADPHONE_BACKEND) ||
1122 (usecase_backend_idx == HEADPHONE_44_1_BACKEND)) &&
1123 ((usecase->stream.out->sample_rate % OUTPUT_SAMPLING_RATE_44100) == 0)) {
1124 usecase->stream.out->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1125 platform_check_and_set_codec_backend_cfg(adev, usecase,
1126 usecase->out_snd_device);
1127 }
1128 }
Saurav Kumarc1411662020-10-14 10:50:45 +05301129 }
1130 }
1131}
1132
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +05301133/*
1134 * - Enable ASRC mode for incoming mix path use case(Headphone backend)if Headphone
1135 * 44.1 or Native DSD backends are enabled for any of current use case.
1136 * e.g. 48-> + (Naitve DSD or Headphone 44.1)
1137 * - Disable current mix path use case(Headphone backend) and re-enable it with
1138 * ASRC mode for incoming Headphone 44.1 or Native DSD use case.
1139 * e.g. Naitve DSD or Headphone 44.1 -> + 48
1140 */
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +05301141static void check_and_set_asrc_mode(struct audio_device *adev,
1142 struct audio_usecase *uc_info,
1143 snd_device_t snd_device)
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +05301144{
1145 ALOGV("%s snd device %d", __func__, snd_device);
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +05301146 int i, num_new_devices = 0;
1147 snd_device_t split_new_snd_devices[SND_DEVICE_OUT_END];
1148 /*
1149 *Split snd device for new combo use case
1150 *e.g. Headphopne 44.1-> + Ringtone (Headphone + Speaker)
1151 */
1152 if (platform_split_snd_device(adev->platform,
1153 snd_device,
1154 &num_new_devices,
1155 split_new_snd_devices) == 0) {
1156 for (i = 0; i < num_new_devices; i++)
1157 check_and_set_asrc_mode(adev, uc_info, split_new_snd_devices[i]);
1158 } else {
1159 int new_backend_idx = platform_get_backend_index(snd_device);
1160 if (((new_backend_idx == HEADPHONE_BACKEND) ||
1161 (new_backend_idx == HEADPHONE_44_1_BACKEND) ||
1162 (new_backend_idx == DSD_NATIVE_BACKEND)) &&
1163 !adev->asrc_mode_enabled) {
1164 struct listnode *node = NULL;
1165 struct audio_usecase *uc = NULL;
1166 struct stream_out *curr_out = NULL;
1167 int usecase_backend_idx = DEFAULT_CODEC_BACKEND;
1168 int i, num_devices, ret = 0;
1169 snd_device_t split_snd_devices[SND_DEVICE_OUT_END];
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +05301170
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +05301171 list_for_each(node, &adev->usecase_list) {
1172 uc = node_to_item(node, struct audio_usecase, list);
1173 curr_out = (struct stream_out*) uc->stream.out;
1174 if (curr_out && PCM_PLAYBACK == uc->type && uc != uc_info) {
1175 /*
1176 *Split snd device for existing combo use case
1177 *e.g. Ringtone (Headphone + Speaker) + Headphopne 44.1
1178 */
1179 ret = platform_split_snd_device(adev->platform,
1180 uc->out_snd_device,
1181 &num_devices,
1182 split_snd_devices);
1183 if (ret < 0 || num_devices == 0) {
1184 ALOGV("%s: Unable to split uc->out_snd_device: %d",__func__, uc->out_snd_device);
1185 split_snd_devices[0] = uc->out_snd_device;
1186 num_devices = 1;
Garmond Leung50058f62017-02-08 09:49:30 -08001187 }
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +05301188 for (i = 0; i < num_devices; i++) {
1189 usecase_backend_idx = platform_get_backend_index(split_snd_devices[i]);
1190 ALOGD("%s:snd_dev %d usecase_backend_idx %d",__func__, split_snd_devices[i],usecase_backend_idx);
1191 if((new_backend_idx == HEADPHONE_BACKEND) &&
1192 ((usecase_backend_idx == HEADPHONE_44_1_BACKEND) ||
1193 (usecase_backend_idx == DSD_NATIVE_BACKEND))) {
Aniket Kumar Latad13758f2020-08-06 15:11:36 -07001194 ALOGV("%s:DSD or native stream detected enabling asrcmode in hardware",
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +05301195 __func__);
1196 enable_asrc_mode(adev);
1197 break;
1198 } else if(((new_backend_idx == HEADPHONE_44_1_BACKEND) ||
1199 (new_backend_idx == DSD_NATIVE_BACKEND)) &&
1200 (usecase_backend_idx == HEADPHONE_BACKEND)) {
Aniket Kumar Latad13758f2020-08-06 15:11:36 -07001201 ALOGV("%s: 48K stream detected, disabling and enabling it \
1202 with asrcmode in hardware", __func__);
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +05301203 disable_audio_route(adev, uc);
1204 disable_snd_device(adev, uc->out_snd_device);
1205 // Apply true-high-quality-mode if DSD or > 44.1KHz or >=24-bit
1206 if (new_backend_idx == DSD_NATIVE_BACKEND)
1207 audio_route_apply_and_update_path(adev->audio_route,
1208 "hph-true-highquality-mode");
1209 else if ((new_backend_idx == HEADPHONE_44_1_BACKEND) &&
1210 (curr_out->bit_width >= 24))
1211 audio_route_apply_and_update_path(adev->audio_route,
1212 "hph-highquality-mode");
1213 enable_asrc_mode(adev);
1214 enable_snd_device(adev, uc->out_snd_device);
1215 enable_audio_route(adev, uc);
1216 break;
1217 }
1218 }
1219 // reset split devices count
1220 num_devices = 0;
Garmond Leung50058f62017-02-08 09:49:30 -08001221 }
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +05301222 if (adev->asrc_mode_enabled)
1223 break;
Preetam Singh Ranawatb0c0dd72016-08-18 00:32:06 +05301224 }
1225 }
1226 }
1227}
1228
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001229static int send_effect_enable_disable_mixer_ctl(struct audio_device *adev,
1230 struct audio_effect_config effect_config,
1231 unsigned int param_value)
1232{
1233 char mixer_ctl_name[] = "Audio Effect";
1234 struct mixer_ctl *ctl;
1235 long set_values[6];
Eric Laurent637e2d42018-11-15 12:24:31 -08001236 struct stream_in *in = adev_get_active_input(adev);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001237
Aniket Kumar Lata7de63522019-06-13 11:05:18 -07001238 if (in == NULL) {
1239 ALOGE("%s: active input stream is NULL", __func__);
1240 return -EINVAL;
1241 }
1242
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001243 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1244 if (!ctl) {
1245 ALOGE("%s: Could not get mixer ctl - %s",
1246 __func__, mixer_ctl_name);
1247 return -EINVAL;
1248 }
1249
1250 set_values[0] = 1; //0:Rx 1:Tx
1251 set_values[1] = in->app_type_cfg.app_type;
1252 set_values[2] = (long)effect_config.module_id;
1253 set_values[3] = (long)effect_config.instance_id;
1254 set_values[4] = (long)effect_config.param_id;
1255 set_values[5] = param_value;
1256
1257 mixer_ctl_set_array(ctl, set_values, ARRAY_SIZE(set_values));
1258
1259 return 0;
1260
1261}
1262
1263static int update_effect_param_ecns(struct audio_device *adev, unsigned int module_id,
1264 int effect_type, unsigned int *param_value)
1265{
1266 int ret = 0;
1267 struct audio_effect_config other_effect_config;
1268 struct audio_usecase *usecase = NULL;
Eric Laurent637e2d42018-11-15 12:24:31 -08001269 struct stream_in *in = adev_get_active_input(adev);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001270
Aniket Kumar Lata7de63522019-06-13 11:05:18 -07001271 if (in == NULL) {
1272 ALOGE("%s: active input stream is NULL", __func__);
1273 return -EINVAL;
1274 }
1275
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001276 usecase = get_usecase_from_list(adev, in->usecase);
1277 if (!usecase)
1278 return -EINVAL;
1279
1280 ret = platform_get_effect_config_data(usecase->in_snd_device, &other_effect_config,
1281 effect_type == EFFECT_AEC ? EFFECT_NS : EFFECT_AEC);
1282 if (ret < 0) {
1283 ALOGE("%s Failed to get effect params %d", __func__, ret);
1284 return ret;
1285 }
1286
1287 if (module_id == other_effect_config.module_id) {
1288 //Same module id for AEC/NS. Values need to be combined
1289 if (((effect_type == EFFECT_AEC) && (in->enable_ns)) ||
1290 ((effect_type == EFFECT_NS) && (in->enable_aec))) {
1291 *param_value |= other_effect_config.param_value;
1292 }
1293 }
1294
1295 return ret;
1296}
1297
1298static int enable_disable_effect(struct audio_device *adev, int effect_type, bool enable)
Gangadhar Sb0210342019-02-22 17:39:41 +05301299{
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001300 struct audio_effect_config effect_config;
1301 struct audio_usecase *usecase = NULL;
1302 int ret = 0;
1303 unsigned int param_value = 0;
Eric Laurent637e2d42018-11-15 12:24:31 -08001304 struct stream_in *in = adev_get_active_input(adev);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001305
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001306 if(!voice_extn_is_dynamic_ecns_enabled())
1307 return ENOSYS;
1308
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001309 if (!in) {
1310 ALOGE("%s: Invalid input stream", __func__);
1311 return -EINVAL;
1312 }
1313
1314 ALOGD("%s: effect_type:%d enable:%d", __func__, effect_type, enable);
1315
1316 usecase = get_usecase_from_list(adev, in->usecase);
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001317 if (usecase == NULL) {
1318 ALOGE("%s: Could not find the usecase (%d) in the list",
1319 __func__, in->usecase);
1320 return -EINVAL;
1321 }
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001322
1323 ret = platform_get_effect_config_data(usecase->in_snd_device, &effect_config, effect_type);
1324 if (ret < 0) {
1325 ALOGE("%s Failed to get module id %d", __func__, ret);
1326 return ret;
1327 }
1328 ALOGV("%s: %d %d usecase->id:%d usecase->in_snd_device:%d", __func__, effect_config.module_id,
1329 in->app_type_cfg.app_type, usecase->id, usecase->in_snd_device);
1330
1331 if(enable)
1332 param_value = effect_config.param_value;
1333
1334 /*Special handling for AEC & NS effects Param values need to be
1335 updated if module ids are same*/
1336
1337 if ((effect_type == EFFECT_AEC) || (effect_type == EFFECT_NS)) {
1338 ret = update_effect_param_ecns(adev, effect_config.module_id, effect_type, &param_value);
1339 if (ret < 0)
1340 return ret;
1341 }
1342
1343 ret = send_effect_enable_disable_mixer_ctl(adev, effect_config, param_value);
1344
1345 return ret;
1346}
1347
1348static void check_and_enable_effect(struct audio_device *adev)
1349{
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001350 if(!voice_extn_is_dynamic_ecns_enabled())
1351 return;
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001352
Eric Laurent637e2d42018-11-15 12:24:31 -08001353 struct stream_in *in = adev_get_active_input(adev);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001354
Eric Laurent637e2d42018-11-15 12:24:31 -08001355 if (in != NULL && !in->standby) {
1356 if (in->enable_aec)
1357 enable_disable_effect(adev, EFFECT_AEC, true);
1358
1359 if (in->enable_ns &&
1360 in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
1361 enable_disable_effect(adev, EFFECT_NS, true);
1362 }
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001363 }
1364}
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001365
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07001366int pcm_ioctl(struct pcm *pcm, int request, ...)
1367{
1368 va_list ap;
1369 void * arg;
1370 int pcm_fd = *(int*)pcm;
1371
1372 va_start(ap, request);
1373 arg = va_arg(ap, void *);
1374 va_end(ap);
1375
1376 return ioctl(pcm_fd, request, arg);
1377}
1378
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -07001379int enable_audio_route(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001380 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001381{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001382 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001383 char mixer_path[MIXER_PATH_MAX_LENGTH];
Manish Dewangan58229382017-02-02 15:48:41 +05301384 struct stream_out *out = NULL;
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05301385 struct stream_in *in = NULL;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001386 struct listnode out_devices;
Soumya Managoli6993b762018-06-28 16:04:57 +05301387 int ret = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001388
1389 if (usecase == NULL)
1390 return -EINVAL;
1391
1392 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
1393
Carter Hsu2e429db2019-05-14 18:50:52 +08001394 if (usecase->type == PCM_CAPTURE) {
1395 struct stream_in *in = usecase->stream.in;
1396 struct audio_usecase *uinfo;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001397 snd_device = usecase->in_snd_device;
Carter Hsu2e429db2019-05-14 18:50:52 +08001398
1399 if (in) {
1400 if (in->enable_aec || in->enable_ec_port) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001401 list_init(&out_devices);
1402 update_device_list(&out_devices, AUDIO_DEVICE_OUT_SPEAKER, "", true);
Carter Hsu2e429db2019-05-14 18:50:52 +08001403 struct listnode *node;
1404 struct audio_usecase *voip_usecase = get_usecase_from_list(adev,
1405 USECASE_AUDIO_PLAYBACK_VOIP);
1406 if (voip_usecase) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001407 assign_devices(&out_devices,
1408 &voip_usecase->stream.out->device_list);
Carter Hsu2e429db2019-05-14 18:50:52 +08001409 } else if (adev->primary_output &&
1410 !adev->primary_output->standby) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001411 assign_devices(&out_devices,
1412 &adev->primary_output->device_list);
Carter Hsu2e429db2019-05-14 18:50:52 +08001413 } else {
1414 list_for_each(node, &adev->usecase_list) {
1415 uinfo = node_to_item(node, struct audio_usecase, list);
1416 if (uinfo->type != PCM_CAPTURE) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001417 assign_devices(&out_devices,
1418 &uinfo->stream.out->device_list);
Carter Hsu2e429db2019-05-14 18:50:52 +08001419 break;
1420 }
1421 }
1422 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001423
1424 platform_set_echo_reference(adev, true, &out_devices);
Carter Hsu2e429db2019-05-14 18:50:52 +08001425 in->ec_opened = true;
Zhenlin Lian4f947842022-05-14 15:50:52 +05301426 clear_devices(&out_devices);
Carter Hsu2e429db2019-05-14 18:50:52 +08001427 }
1428 }
Guodong Huf5e614d2019-06-24 18:42:03 +08001429 } else if ((usecase->type == TRANSCODE_LOOPBACK_TX) || ((usecase->type == PCM_HFP_CALL) &&
1430 ((usecase->id == USECASE_AUDIO_HFP_SCO) || (usecase->id == USECASE_AUDIO_HFP_SCO_WB)) &&
1431 (usecase->in_snd_device == SND_DEVICE_IN_VOICE_SPEAKER_MIC_HFP_MMSECNS))) {
Carter Hsu2e429db2019-05-14 18:50:52 +08001432 snd_device = usecase->in_snd_device;
1433 } else {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001434 snd_device = usecase->out_snd_device;
Carter Hsu2e429db2019-05-14 18:50:52 +08001435 }
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001436
Kunlei Zhanga3c4cb02021-03-05 16:03:54 +08001437 if (usecase->type == PCM_CAPTURE) {
1438 if (platform_get_fluence_nn_state(adev->platform) == 0) {
1439 platform_set_fluence_nn_state(adev->platform, true);
1440 ALOGD("%s: set fluence nn capture state", __func__);
1441 }
1442 }
1443
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08001444#ifdef DS1_DOLBY_DAP_ENABLED
1445 audio_extn_dolby_set_dmid(adev);
1446 audio_extn_dolby_set_endpoint(adev);
1447#endif
Pradnya Chaphekar8a9dcd82014-09-09 09:49:10 -07001448 audio_extn_dolby_ds2_set_endpoint(adev);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001449 audio_extn_sound_trigger_update_stream_status(usecase, ST_EVENT_STREAM_BUSY);
Dhananjay Kumar45b71742014-05-29 21:47:27 +05301450 audio_extn_listen_update_stream_status(usecase, LISTEN_EVENT_STREAM_BUSY);
Ben Romberger1fafdde2015-09-09 19:43:15 -07001451 audio_extn_utils_send_app_type_cfg(adev, usecase);
Jasmine Cha4dcc1092019-03-04 18:12:47 +08001452 if (audio_extn_is_maxx_audio_enabled())
1453 audio_extn_ma_set_device(usecase);
Dhananjay Kumar14170dd2015-08-28 13:24:16 +05301454 audio_extn_utils_send_audio_calibration(adev, usecase);
Zhou Songbaddf9f2020-11-20 13:57:39 +08001455 if ((usecase->type == PCM_PLAYBACK) && is_offload_usecase(usecase->id)) {
1456 out = usecase->stream.out;
1457 if (out && out->compr)
Manish Dewangan58229382017-02-02 15:48:41 +05301458 audio_extn_utils_compress_set_clk_rec_mode(usecase);
1459 }
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05301460
1461 if (usecase->type == PCM_CAPTURE) {
Kunlei Zhanga3c4cb02021-03-05 16:03:54 +08001462 if (platform_get_fluence_nn_state(adev->platform) == 1 &&
1463 adev->fluence_nn_usecase_id == USECASE_INVALID ) {
1464 adev->fluence_nn_usecase_id = usecase->id;
1465 ALOGD("%s: assign fluence nn usecase %d", __func__, usecase->id);
1466 }
1467 }
1468
1469 if (usecase->type == PCM_CAPTURE) {
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05301470 in = usecase->stream.in;
Avinash Chandrad7296d42021-08-04 15:07:47 +05301471 if ((in && is_loopback_input_device(get_device_types(&in->device_list))) ||
1472 (in && is_combo_audio_input_device(&in->device_list)) ||
1473 (in && ((compare_device_type(&in->device_list, AUDIO_DEVICE_IN_BUILTIN_MIC) ||
1474 compare_device_type(&in->device_list, AUDIO_DEVICE_IN_LINE)) &&
1475 (snd_device == SND_DEVICE_IN_HANDSET_GENERIC_6MIC)))) {
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05301476 ALOGD("%s: set custom mtmx params v1", __func__);
1477 audio_extn_set_custom_mtmx_params_v1(adev, usecase, true);
1478 }
1479 } else {
1480 audio_extn_set_custom_mtmx_params_v2(adev, usecase, true);
1481 }
Manish Dewangan58229382017-02-02 15:48:41 +05301482
Andy Hung756ecc12018-10-19 17:47:12 -07001483 // we shouldn't truncate mixer_path
1484 ALOGW_IF(strlcpy(mixer_path, use_case_table[usecase->id], sizeof(mixer_path))
1485 >= sizeof(mixer_path), "%s: truncation on mixer path", __func__);
1486 // this also appends to mixer_path
Banajit Goswami20cdd212015-09-11 01:11:30 -07001487 platform_add_backend_name(mixer_path, snd_device, usecase);
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07001488 ALOGD("%s: apply mixer and update path: %s", __func__, mixer_path);
Soumya Managoli6993b762018-06-28 16:04:57 +05301489 ret = audio_route_apply_and_update_path(adev->audio_route, mixer_path);
1490 if (!ret && usecase->id == USECASE_AUDIO_PLAYBACK_FM) {
1491 struct str_parms *parms = str_parms_create_str("fm_restore_volume=1");
1492 if (parms) {
1493 audio_extn_fm_set_parameters(adev, parms);
1494 str_parms_destroy(parms);
1495 }
1496 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001497 ALOGV("%s: exit", __func__);
1498 return 0;
1499}
1500
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001501int disable_audio_route(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001502 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001503{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001504 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001505 char mixer_path[MIXER_PATH_MAX_LENGTH];
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05301506 struct stream_in *in = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001507
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05301508 if (usecase == NULL || usecase->id == USECASE_INVALID)
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001509 return -EINVAL;
1510
1511 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Surendar Karka93cd25a2018-08-28 14:21:37 +05301512 if (usecase->type == PCM_CAPTURE || usecase->type == TRANSCODE_LOOPBACK_TX)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001513 snd_device = usecase->in_snd_device;
1514 else
1515 snd_device = usecase->out_snd_device;
Kunlei Zhange7cab1a2020-05-17 20:31:05 +08001516
1517 /* disable island and power mode on supported device for voice call */
1518 if (usecase->type == VOICE_CALL) {
1519 if (usecase->in_snd_device != SND_DEVICE_NONE) {
1520 if (platform_get_island_cfg_on_device(adev->platform, usecase->in_snd_device) &&
1521 platform_get_power_mode_on_device(adev->platform, usecase->in_snd_device)) {
1522 platform_set_island_cfg_on_device(adev, usecase->in_snd_device, false);
1523 platform_set_power_mode_on_device(adev, usecase->in_snd_device, false);
1524 platform_reset_island_power_status(adev->platform, usecase->in_snd_device);
Kunlei Zhang91c4b332020-07-20 17:53:14 +08001525 if (voice_is_lte_call_active(adev))
1526 platform_set_tx_lpi_mode(adev->platform, false);
Kunlei Zhange7cab1a2020-05-17 20:31:05 +08001527 ALOGD("%s: disable island cfg and power mode in voice tx path",
1528 __func__);
1529 }
1530 }
1531 if (usecase->out_snd_device != SND_DEVICE_NONE) {
1532 if (platform_get_island_cfg_on_device(adev->platform, usecase->out_snd_device) &&
1533 platform_get_power_mode_on_device(adev->platform, usecase->out_snd_device)) {
1534 platform_set_island_cfg_on_device(adev, usecase->out_snd_device, false);
1535 platform_set_power_mode_on_device(adev, usecase->out_snd_device, false);
1536 platform_reset_island_power_status(adev->platform, usecase->out_snd_device);
1537 ALOGD("%s: disable island cfg and power mode in voice rx path",
1538 __func__);
1539 }
1540 }
1541 }
1542
Andy Hung756ecc12018-10-19 17:47:12 -07001543 // we shouldn't truncate mixer_path
1544 ALOGW_IF(strlcpy(mixer_path, use_case_table[usecase->id], sizeof(mixer_path))
1545 >= sizeof(mixer_path), "%s: truncation on mixer path", __func__);
1546 // this also appends to mixer_path
Banajit Goswami20cdd212015-09-11 01:11:30 -07001547 platform_add_backend_name(mixer_path, snd_device, usecase);
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07001548 ALOGD("%s: reset and update mixer path: %s", __func__, mixer_path);
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07001549 audio_route_reset_and_update_path(adev->audio_route, mixer_path);
Carter Hsu2e429db2019-05-14 18:50:52 +08001550 if (usecase->type == PCM_CAPTURE) {
1551 struct stream_in *in = usecase->stream.in;
1552 if (in && in->ec_opened) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001553 struct listnode out_devices;
1554 list_init(&out_devices);
1555 platform_set_echo_reference(in->dev, false, &out_devices);
Carter Hsu2e429db2019-05-14 18:50:52 +08001556 in->ec_opened = false;
Zhenlin Lian4f947842022-05-14 15:50:52 +05301557 clear_devices(&out_devices);
Carter Hsu2e429db2019-05-14 18:50:52 +08001558 }
1559 }
Kunlei Zhanga3c4cb02021-03-05 16:03:54 +08001560 if (usecase->id == adev->fluence_nn_usecase_id) {
1561 platform_set_fluence_nn_state(adev->platform, false);
1562 adev->fluence_nn_usecase_id = USECASE_INVALID;
1563 ALOGD("%s: reset fluence nn capture state", __func__);
1564 }
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001565 audio_extn_sound_trigger_update_stream_status(usecase, ST_EVENT_STREAM_FREE);
Dhananjay Kumar45b71742014-05-29 21:47:27 +05301566 audio_extn_listen_update_stream_status(usecase, LISTEN_EVENT_STREAM_FREE);
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05301567
1568 if (usecase->type == PCM_CAPTURE) {
1569 in = usecase->stream.in;
Avinash Chandrad7296d42021-08-04 15:07:47 +05301570 if ((in && is_loopback_input_device(get_device_types(&in->device_list))) ||
1571 (in && is_combo_audio_input_device(&in->device_list)) ||
1572 (in && ((compare_device_type(&in->device_list, AUDIO_DEVICE_IN_BUILTIN_MIC) ||
1573 compare_device_type(&in->device_list, AUDIO_DEVICE_IN_LINE)) &&
1574 (snd_device == SND_DEVICE_IN_HANDSET_GENERIC_6MIC)))){
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05301575 ALOGD("%s: reset custom mtmx params v1", __func__);
1576 audio_extn_set_custom_mtmx_params_v1(adev, usecase, false);
1577 }
1578 } else {
1579 audio_extn_set_custom_mtmx_params_v2(adev, usecase, false);
1580 }
1581
Weiyin Jiang298ffd92019-06-03 14:29:30 +08001582 if ((usecase->type == PCM_PLAYBACK) &&
1583 (usecase->stream.out != NULL))
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +05301584 usecase->stream.out->pspd_coeff_sent = false;
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05301585
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001586 ALOGV("%s: exit", __func__);
1587 return 0;
1588}
1589
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -07001590int enable_snd_device(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001591 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001592{
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301593 int i, num_devices = 0;
1594 snd_device_t new_snd_devices[SND_DEVICE_OUT_END];
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001595 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
1596
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001597 if (snd_device < SND_DEVICE_MIN ||
1598 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001599 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001600 return -EINVAL;
1601 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001602
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001603 if (platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0) {
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001604 ALOGE("%s: Invalid sound device returned", __func__);
1605 return -EINVAL;
1606 }
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001607
1608 adev->snd_dev_ref_cnt[snd_device]++;
1609
1610 if ((adev->snd_dev_ref_cnt[snd_device] > 1) &&
1611 (platform_split_snd_device(adev->platform,
1612 snd_device,
1613 &num_devices,
1614 new_snd_devices) != 0)) {
Eric Laurent994a6932013-07-17 11:51:42 -07001615 ALOGV("%s: snd_device(%d: %s) is already active",
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001616 __func__, snd_device, device_name);
Aniket Kumar Lata990de552019-07-11 14:20:23 -07001617 /* Set backend config for A2DP to ensure slimbus configuration
1618 is correct if A2DP is already active and backend is closed
1619 and re-opened */
1620 if (snd_device == SND_DEVICE_OUT_BT_A2DP)
1621 audio_extn_a2dp_set_source_backend_cfg();
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001622 return 0;
1623 }
1624
Gopikrishnaiah Anandane85d0462014-06-30 21:41:20 -07001625 if (audio_extn_spkr_prot_is_enabled())
1626 audio_extn_spkr_prot_calib_cancel(adev);
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -07001627
Aalique Grahame22e49102018-12-18 14:23:57 -08001628 audio_extn_dsm_feedback_enable(adev, snd_device, true);
Luofan Chen6f377262022-08-20 19:10:01 +08001629 amplifier_enable_devices(snd_device, true);
Aalique Grahame22e49102018-12-18 14:23:57 -08001630
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001631 if (platform_can_enable_spkr_prot_on_device(snd_device) &&
1632 audio_extn_spkr_prot_is_enabled()) {
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001633 if (platform_get_spkr_prot_acdb_id(snd_device) < 0) {
1634 goto err;
1635 }
1636 audio_extn_dev_arbi_acquire(snd_device);
1637 if (audio_extn_spkr_prot_start_processing(snd_device)) {
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -08001638 ALOGE("%s: spkr_start_processing failed", __func__);
Ravit Dennisaaee49c2015-02-04 21:26:22 +02001639 audio_extn_dev_arbi_release(snd_device);
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001640 goto err;
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -08001641 }
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001642 } else if (platform_split_snd_device(adev->platform,
1643 snd_device,
1644 &num_devices,
1645 new_snd_devices) == 0) {
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301646 for (i = 0; i < num_devices; i++) {
1647 enable_snd_device(adev, new_snd_devices[i]);
1648 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001649 platform_set_speaker_gain_in_combo(adev, snd_device, true);
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -08001650 } else {
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07001651 ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, device_name);
Naresh Tannirucd2353e2016-08-19 00:37:25 +05301652
Kunlei Zhange7cab1a2020-05-17 20:31:05 +08001653 /* enable island and power mode on supported device */
1654 if (platform_get_island_cfg_on_device(adev->platform, snd_device) &&
1655 platform_get_power_mode_on_device(adev->platform, snd_device)) {
1656 platform_set_island_cfg_on_device(adev, snd_device, true);
1657 platform_set_power_mode_on_device(adev, snd_device, true);
Kunlei Zhang91c4b332020-07-20 17:53:14 +08001658 if (voice_is_lte_call_active(adev) &&
1659 (snd_device >= SND_DEVICE_IN_BEGIN &&
1660 snd_device < SND_DEVICE_IN_END))
1661 platform_set_tx_lpi_mode(adev->platform, true);
Kunlei Zhange7cab1a2020-05-17 20:31:05 +08001662 ALOGD("%s: enable island cfg and power mode on: %s",
1663 __func__, device_name);
1664 }
Preetam Singh Ranawatf1d417c2017-01-10 17:00:32 +05301665
Revathi Uddarajub26e3932020-06-10 14:51:02 +05301666 if (SND_DEVICE_OUT_BT_A2DP == snd_device) {
Ramjee Singh2d2944a2021-12-14 14:13:41 +05301667
1668 struct audio_usecase *usecase;
1669 struct listnode *node;
1670 /* Disable SCO Devices and enable handset mic for active input stream */
1671 list_for_each(node, &adev->usecase_list) {
1672 usecase = node_to_item(node, struct audio_usecase, list);
1673 if (usecase->stream.in && (usecase->type == PCM_CAPTURE) &&
1674 is_sco_in_device_type(&usecase->stream.in->device_list)) {
1675 ALOGD("a2dp resumed, switch bt sco mic to handset mic");
1676 reassign_device_list(&usecase->stream.in->device_list,
1677 AUDIO_DEVICE_IN_BUILTIN_MIC, "");
1678 select_devices(adev, usecase->id);
1679 }
1680 }
Revathi Uddarajub26e3932020-06-10 14:51:02 +05301681 if (audio_extn_a2dp_start_playback() < 0) {
1682 ALOGE(" fail to configure A2dp Source control path ");
1683 goto err;
1684 } else {
1685 adev->a2dp_started = true;
1686 }
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001687 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001688
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001689 if ((SND_DEVICE_IN_BT_A2DP == snd_device) &&
1690 (audio_extn_a2dp_start_capture() < 0)) {
1691 ALOGE(" fail to configure A2dp Sink control path ");
1692 goto err;
1693 }
Naresh Tannirucd2353e2016-08-19 00:37:25 +05301694
Mingshu Pang7be5b1d2020-03-04 15:24:38 +08001695 if ((SND_DEVICE_OUT_BT_SCO_SWB == snd_device) ||
1696 (SND_DEVICE_IN_BT_SCO_MIC_SWB_NREC == snd_device) ||
1697 (SND_DEVICE_IN_BT_SCO_MIC_SWB == snd_device)) {
1698 if (!adev->bt_sco_on || (audio_extn_sco_start_configuration() < 0)) {
1699 ALOGE(" fail to configure sco control path ");
1700 goto err;
1701 }
Zhou Song12c29502019-03-16 10:37:18 +08001702 }
1703
Zhou Song331c8e52019-08-26 14:16:12 +08001704 configure_btsco_sample_rate(snd_device);
Bharath Ramachandramurthy0de16782014-03-28 21:34:33 -07001705 /* due to the possibility of calibration overwrite between listen
1706 and audio, notify listen hal before audio calibration is sent */
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001707 audio_extn_sound_trigger_update_device_status(snd_device,
1708 ST_EVENT_SND_DEVICE_BUSY);
Dhananjay Kumar45b71742014-05-29 21:47:27 +05301709 audio_extn_listen_update_device_status(snd_device,
1710 LISTEN_EVENT_SND_DEVICE_BUSY);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001711 if (platform_get_snd_device_acdb_id(snd_device) < 0) {
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001712 audio_extn_sound_trigger_update_device_status(snd_device,
1713 ST_EVENT_SND_DEVICE_FREE);
Dhananjay Kumar45b71742014-05-29 21:47:27 +05301714 audio_extn_listen_update_device_status(snd_device,
1715 LISTEN_EVENT_SND_DEVICE_FREE);
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001716 goto err;
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -07001717 }
Lior Barenboim0b61bc72014-05-13 13:01:37 +03001718 audio_extn_dev_arbi_acquire(snd_device);
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07001719 audio_route_apply_and_update_path(adev->audio_route, device_name);
Daniel Hillenbrand17873952013-05-23 10:10:00 +05301720 amplifier_set_feedback(adev, snd_device, true);
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301721
1722 if (SND_DEVICE_OUT_HEADPHONES == snd_device &&
1723 !adev->native_playback_enabled &&
1724 audio_is_true_native_stream_active(adev)) {
1725 ALOGD("%s: %d: napb: enabling native mode in hardware",
1726 __func__, __LINE__);
1727 audio_route_apply_and_update_path(adev->audio_route,
1728 "true-native-mode");
1729 adev->native_playback_enabled = true;
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +05301730 }
Dhanalakshmi Siddani10621622018-04-30 15:07:27 +05301731 if (((snd_device == SND_DEVICE_IN_HANDSET_6MIC) ||
1732 (snd_device == SND_DEVICE_IN_HANDSET_QMIC)) &&
Eric Laurent637e2d42018-11-15 12:24:31 -08001733 (audio_extn_ffv_get_stream() == adev_get_active_input(adev))) {
Garmond Leunge2433c32017-09-28 21:51:22 -07001734 ALOGD("%s: init ec ref loopback", __func__);
1735 audio_extn_ffv_init_ec_ref_loopback(adev, snd_device);
1736 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001737 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001738 return 0;
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001739err:
1740 adev->snd_dev_ref_cnt[snd_device]--;
1741 return -EINVAL;;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001742}
1743
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001744int disable_snd_device(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001745 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001746{
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301747 int i, num_devices = 0;
1748 snd_device_t new_snd_devices[SND_DEVICE_OUT_END];
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001749 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
1750
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001751 if (snd_device < SND_DEVICE_MIN ||
1752 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001753 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001754 return -EINVAL;
1755 }
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001756
1757 if (platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0) {
1758 ALOGE("%s: Invalid sound device returned", __func__);
1759 return -EINVAL;
1760 }
1761
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001762 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
1763 ALOGE("%s: device ref cnt is already 0", __func__);
1764 return -EINVAL;
1765 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001766
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001767 adev->snd_dev_ref_cnt[snd_device]--;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -07001768
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001769
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001770 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07001771 ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, device_name);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301772
Aalique Grahame22e49102018-12-18 14:23:57 -08001773 audio_extn_dsm_feedback_enable(adev, snd_device, false);
Luofan Chen6f377262022-08-20 19:10:01 +08001774 amplifier_enable_devices(snd_device, false);
Aalique Grahame22e49102018-12-18 14:23:57 -08001775
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001776 if (platform_can_enable_spkr_prot_on_device(snd_device) &&
1777 audio_extn_spkr_prot_is_enabled()) {
Anish Kumar46c7b872014-09-09 01:49:44 -07001778 audio_extn_spkr_prot_stop_processing(snd_device);
Vignesh Kulothungan3b5fae52017-09-25 12:16:30 -07001779
1780 // when speaker device is disabled, reset swap.
1781 // will be renabled on usecase start
1782 platform_set_swap_channels(adev, false);
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001783 } else if (platform_split_snd_device(adev->platform,
1784 snd_device,
1785 &num_devices,
1786 new_snd_devices) == 0) {
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301787 for (i = 0; i < num_devices; i++) {
1788 disable_snd_device(adev, new_snd_devices[i]);
1789 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001790 platform_set_speaker_gain_in_combo(adev, snd_device, false);
Lior Barenboim0b61bc72014-05-13 13:01:37 +03001791 } else {
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07001792 audio_route_reset_and_update_path(adev->audio_route, device_name);
Lior Barenboim0b61bc72014-05-13 13:01:37 +03001793 }
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001794
Revathi Uddarajub26e3932020-06-10 14:51:02 +05301795 if (snd_device == SND_DEVICE_OUT_BT_A2DP) {
Naresh Tannirucd2353e2016-08-19 00:37:25 +05301796 audio_extn_a2dp_stop_playback();
Revathi Uddarajub26e3932020-06-10 14:51:02 +05301797 adev->a2dp_started = false;
1798 } else if (snd_device == SND_DEVICE_IN_BT_A2DP)
Florian Pfister1a84f312018-07-19 14:38:18 +02001799 audio_extn_a2dp_stop_capture();
Zhou Songd6d71752019-05-21 18:08:51 +08001800 else if ((snd_device == SND_DEVICE_OUT_HDMI) ||
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001801 (snd_device == SND_DEVICE_OUT_DISPLAY_PORT))
Ashish Jain81eb2a82015-05-13 10:52:34 +05301802 adev->is_channel_status_set = false;
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001803 else if ((snd_device == SND_DEVICE_OUT_HEADPHONES) &&
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301804 adev->native_playback_enabled) {
1805 ALOGD("%s: %d: napb: disabling native mode in hardware",
1806 __func__, __LINE__);
1807 audio_route_reset_and_update_path(adev->audio_route,
1808 "true-native-mode");
1809 adev->native_playback_enabled = false;
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001810 } else if ((snd_device == SND_DEVICE_OUT_HEADPHONES) &&
Preetam Singh Ranawatb0c0dd72016-08-18 00:32:06 +05301811 adev->asrc_mode_enabled) {
1812 ALOGD("%s: %d: disabling asrc mode in hardware", __func__, __LINE__);
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +05301813 disable_asrc_mode(adev);
1814 audio_route_apply_and_update_path(adev->audio_route, "hph-lowpower-mode");
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001815 } else if (((snd_device == SND_DEVICE_IN_HANDSET_6MIC) ||
Dhanalakshmi Siddaniaf4bd622019-02-27 16:28:06 +05301816 (snd_device == SND_DEVICE_IN_HANDSET_QMIC)) &&
Eric Laurent637e2d42018-11-15 12:24:31 -08001817 (audio_extn_ffv_get_stream() == adev_get_active_input(adev))) {
Garmond Leunge2433c32017-09-28 21:51:22 -07001818 ALOGD("%s: deinit ec ref loopback", __func__);
1819 audio_extn_ffv_deinit_ec_ref_loopback(adev, snd_device);
1820 }
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001821
1822 audio_extn_utils_release_snd_device(snd_device);
Daniel Hillenbrand17873952013-05-23 10:10:00 +05301823 amplifier_set_feedback(adev, snd_device, false);
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001824 } else {
1825 if (platform_split_snd_device(adev->platform,
1826 snd_device,
1827 &num_devices,
1828 new_snd_devices) == 0) {
1829 for (i = 0; i < num_devices; i++) {
1830 adev->snd_dev_ref_cnt[new_snd_devices[i]]--;
1831 }
1832 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001833 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001834
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001835 return 0;
1836}
1837
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001838/*
1839 legend:
1840 uc - existing usecase
1841 new_uc - new usecase
1842 d1, d11, d2 - SND_DEVICE enums
1843 a1, a2 - corresponding ANDROID device enums
1844 B1, B2 - backend strings
1845
1846case 1
1847 uc->dev d1 (a1) B1
1848 new_uc->dev d1 (a1), d2 (a2) B1, B2
1849
1850 resolution: disable and enable uc->dev on d1
1851
1852case 2
1853 uc->dev d1 (a1) B1
1854 new_uc->dev d11 (a1) B1
1855
1856 resolution: need to switch uc since d1 and d11 are related
1857 (e.g. speaker and voice-speaker)
1858 use ANDROID_DEVICE_OUT enums to match devices since SND_DEVICE enums may vary
1859
1860case 3
1861 uc->dev d1 (a1) B1
1862 new_uc->dev d2 (a2) B2
1863
1864 resolution: no need to switch uc
1865
1866case 4
1867 uc->dev d1 (a1) B1
1868 new_uc->dev d2 (a2) B1
1869
1870 resolution: disable enable uc-dev on d2 since backends match
1871 we cannot enable two streams on two different devices if they
1872 share the same backend. e.g. if offload is on speaker device using
1873 QUAD_MI2S backend and a low-latency stream is started on voice-handset
1874 using the same backend, offload must also be switched to voice-handset.
1875
1876case 5
1877 uc->dev d1 (a1) B1
1878 new_uc->dev d1 (a1), d2 (a2) B1
1879
1880 resolution: disable enable uc-dev on d2 since backends match
1881 we cannot enable two streams on two different devices if they
1882 share the same backend.
1883
1884case 6
1885 uc->dev d1 (a1) B1
1886 new_uc->dev d2 (a1) B2
1887
1888 resolution: no need to switch
1889
1890case 7
1891 uc->dev d1 (a1), d2 (a2) B1, B2
1892 new_uc->dev d1 (a1) B1
1893
1894 resolution: no need to switch
1895
Zhou Song4ba65882018-07-09 14:48:07 +08001896case 8
1897 uc->dev d1 (a1) B1
1898 new_uc->dev d11 (a1), d2 (a2) B1, B2
1899 resolution: compared to case 1, for this case, d1 and d11 are related
1900 then need to do the same as case 2 to siwtch to new uc
Preetam Singh Ranawatd399e6f2020-06-25 12:20:36 +05301901
1902case 9
1903 uc->dev d1 (a1), d2(a2) B1 B2
1904 new_uc->dev d1 (a1), d22 (a2) B1, B2
1905 resolution: disable enable uc-dev on d2 since backends match
1906 we cannot enable two streams on two different devices if they
1907 share the same backend. This is special case for combo use case
1908 with a2dp and sco devices which uses same backend.
1909 e.g. speaker-a2dp and speaker-btsco
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001910*/
1911static snd_device_t derive_playback_snd_device(void * platform,
1912 struct audio_usecase *uc,
1913 struct audio_usecase *new_uc,
1914 snd_device_t new_snd_device)
1915{
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001916 struct listnode a1, a2;
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001917
1918 snd_device_t d1 = uc->out_snd_device;
1919 snd_device_t d2 = new_snd_device;
Sandhya Mutha Naga Venkataa597a962023-02-07 11:35:51 +05301920 int ret = 0;
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001921
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001922 list_init(&a1);
1923 list_init(&a2);
1924
Siddartha Shaik31b530e2017-05-19 15:26:33 +05301925 switch (uc->type) {
Surendar Karka93cd25a2018-08-28 14:21:37 +05301926 case TRANSCODE_LOOPBACK_RX :
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001927 assign_devices(&a1, &uc->stream.inout->out_config.device_list);
1928 assign_devices(&a2, &new_uc->stream.inout->out_config.device_list);
Siddartha Shaik31b530e2017-05-19 15:26:33 +05301929 break;
1930 default :
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001931 assign_devices(&a1, &uc->stream.out->device_list);
1932 assign_devices(&a2, &new_uc->stream.out->device_list);
Siddartha Shaik31b530e2017-05-19 15:26:33 +05301933 break;
1934 }
1935
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001936 // Treat as a special case when a1 and a2 are not disjoint
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001937 if (!compare_devices(&a1, &a2) &&
1938 compare_devices_for_any_match(&a1 ,&a2)) {
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001939 snd_device_t d3[2];
1940 int num_devices = 0;
Sandhya Mutha Naga Venkataa597a962023-02-07 11:35:51 +05301941 ret = platform_split_snd_device(platform,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001942 list_length(&a1) > 1 ? d1 : d2,
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001943 &num_devices,
1944 d3);
1945 if (ret < 0) {
1946 if (ret != -ENOSYS) {
1947 ALOGW("%s failed to split snd_device %d",
1948 __func__,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001949 list_length(&a1) > 1 ? d1 : d2);
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001950 }
Sandhya Mutha Naga Venkata4c6fc9f2022-10-14 19:38:02 +05301951 ret = d2;
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001952 goto end;
1953 }
1954
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001955 if (platform_check_backends_match(d3[0], d3[1])) {
Sandhya Mutha Naga Venkata4c6fc9f2022-10-14 19:38:02 +05301956 ret = d2;
1957 goto end; // case 5
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001958 } else {
Preetam Singh Ranawatd399e6f2020-06-25 12:20:36 +05301959 if ((list_length(&a1) > 1) && (list_length(&a2) > 1) &&
Sandhya Mutha Naga Venkata4c6fc9f2022-10-14 19:38:02 +05301960 platform_check_backends_match(d1, d2)) {
1961 ret = d2;
1962 goto end; //case 9
1963 }
1964 if (list_length(&a1) > 1) {
1965 ret = d1;
1966 goto end; //case 7
1967 }
Garmond Leungb9eeba42018-09-18 11:10:41 -07001968 // check if d1 is related to any of d3's
Sandhya Mutha Naga Venkata4c6fc9f2022-10-14 19:38:02 +05301969 if (d1 == d3[0] || d1 == d3[1]) {
1970 ret = d1;
1971 goto end; // case 1
1972 } else {
1973 ret = d3[1];
1974 goto end; // case 8
1975 }
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001976 }
1977 } else {
1978 if (platform_check_backends_match(d1, d2)) {
Sandhya Mutha Naga Venkata4c6fc9f2022-10-14 19:38:02 +05301979 ret = d2;
1980 goto end; // case 2, 4
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001981 } else {
Sandhya Mutha Naga Venkata4c6fc9f2022-10-14 19:38:02 +05301982 ret = d1;
1983 goto end; // case 6, 3
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001984 }
1985 }
1986
1987end:
Sandhya Mutha Naga Venkata4c6fc9f2022-10-14 19:38:02 +05301988 clear_devices(&a1);
1989 clear_devices(&a2);
1990 return ret; // return whatever was calculated before.
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001991}
1992
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001993static void check_usecases_codec_backend(struct audio_device *adev,
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301994 struct audio_usecase *uc_info,
1995 snd_device_t snd_device)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001996{
1997 struct listnode *node;
1998 struct audio_usecase *usecase;
1999 bool switch_device[AUDIO_USECASE_MAX];
Chaithanya Krishna Bacharaju49e7db02017-03-14 11:57:26 +05302000 snd_device_t uc_derive_snd_device;
2001 snd_device_t derive_snd_device[AUDIO_USECASE_MAX];
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08002002 snd_device_t split_snd_devices[SND_DEVICE_OUT_END];
2003 int i, num_uc_to_switch = 0, num_devices = 0;
kunleiz5cd52b82016-11-07 17:22:52 +08002004 int status = 0;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302005 bool force_restart_session = false;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002006 /*
2007 * This function is to make sure that all the usecases that are active on
2008 * the hardware codec backend are always routed to any one device that is
2009 * handled by the hardware codec.
2010 * For example, if low-latency and deep-buffer usecases are currently active
2011 * on speaker and out_set_parameters(headset) is received on low-latency
2012 * output, then we have to make sure deep-buffer is also switched to headset,
2013 * because of the limitation that both the devices cannot be enabled
2014 * at the same time as they share the same backend.
2015 */
Mingming Yin3ee55c62014-08-04 14:23:35 -07002016 /*
2017 * This call is to check if we need to force routing for a particular stream
2018 * If there is a backend configuration change for the device when a
2019 * new stream starts, then ADM needs to be closed and re-opened with the new
2020 * configuraion. This call check if we need to re-route all the streams
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -08002021 * associated with the backend. Touch tone + 24 bit + native playback.
Mingming Yin3ee55c62014-08-04 14:23:35 -07002022 */
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -08002023 bool force_routing = platform_check_and_set_codec_backend_cfg(adev, uc_info,
2024 snd_device);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302025 /* For a2dp device reconfigure all active sessions
2026 * with new AFE encoder format based on a2dp state
2027 */
2028 if ((SND_DEVICE_OUT_BT_A2DP == snd_device ||
Preetam Singh Ranawatd399e6f2020-06-25 12:20:36 +05302029 SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP == snd_device ||
2030 SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP == snd_device) &&
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302031 audio_extn_a2dp_is_force_device_switch()) {
2032 force_routing = true;
2033 force_restart_session = true;
2034 }
Kunlei Zhange7cab1a2020-05-17 20:31:05 +08002035
2036 /*
2037 * Island cfg and power mode config needs to set before AFE port start.
2038 * Set force routing in case of voice device was enable before.
2039 */
2040 if (uc_info->type == VOICE_CALL &&
2041 voice_extn_is_voice_power_mode_supported() &&
Kunlei Zhang67cc7072020-12-18 17:16:49 +08002042 is_supported_conc_usecase_for_power_mode_call(adev) &&
Kunlei Zhange7cab1a2020-05-17 20:31:05 +08002043 platform_check_and_update_island_power_status(adev->platform,
2044 uc_info,
2045 snd_device)) {
2046 force_routing = true;
2047 ALOGD("%s:becf: force routing %d for power mode supported device",
2048 __func__, force_routing);
2049 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302050 ALOGD("%s:becf: force routing %d", __func__, force_routing);
2051
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002052 /* Disable all the usecases on the shared backend other than the
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -08002053 * specified usecase.
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -08002054 */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002055 for (i = 0; i < AUDIO_USECASE_MAX; i++)
2056 switch_device[i] = false;
2057
2058 list_for_each(node, &adev->usecase_list) {
2059 usecase = node_to_item(node, struct audio_usecase, list);
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -08002060
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05302061 ALOGD("%s:becf: (%d) check_usecases curr device: %s, usecase device:%s "
2062 "backends match %d",__func__, i,
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302063 platform_get_snd_device_name(snd_device),
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05302064 platform_get_snd_device_name(usecase->out_snd_device),
2065 platform_check_backends_match(snd_device, usecase->out_snd_device));
Rahul Sharma99770982019-03-06 17:05:26 +05302066 if ((usecase->type != PCM_CAPTURE) && (usecase != uc_info) &&
2067 (usecase->type != PCM_PASSTHROUGH)) {
Ashish Jain6a65b352017-03-21 17:24:40 +05302068 uc_derive_snd_device = derive_playback_snd_device(adev->platform,
2069 usecase, uc_info, snd_device);
2070 if (((uc_derive_snd_device != usecase->out_snd_device) || force_routing) &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002071 (is_codec_backend_out_device_type(&usecase->device_list) ||
2072 compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_AUX_DIGITAL) ||
2073 compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_USB_DEVICE) ||
2074 compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_USB_HEADSET) ||
2075 is_a2dp_out_device_type(&usecase->device_list) ||
2076 is_sco_out_device_type(&usecase->device_list)) &&
Ashish Jain6a65b352017-03-21 17:24:40 +05302077 ((force_restart_session) ||
2078 (platform_check_backends_match(snd_device, usecase->out_snd_device)))) {
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05302079 ALOGD("%s:becf: check_usecases (%s) is active on (%s) - disabling ..",
2080 __func__, use_case_table[usecase->id],
2081 platform_get_snd_device_name(usecase->out_snd_device));
2082 disable_audio_route(adev, usecase);
2083 switch_device[usecase->id] = true;
Chaithanya Krishna Bacharaju49e7db02017-03-14 11:57:26 +05302084 /* Enable existing usecase on derived playback device */
2085 derive_snd_device[usecase->id] = uc_derive_snd_device;
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05302086 num_uc_to_switch++;
Ashish Jain6a65b352017-03-21 17:24:40 +05302087 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002088 }
2089 }
2090
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302091 ALOGD("%s:becf: check_usecases num.of Usecases to switch %d", __func__,
2092 num_uc_to_switch);
2093
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002094 if (num_uc_to_switch) {
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07002095 /* All streams have been de-routed. Disable the device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002096
Venkata Narendra Kumar Gutta7610e632014-04-14 23:16:38 +05302097 /* Make sure the previous devices to be disabled first and then enable the
2098 selected devices */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -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 /* Check if output sound device to be switched can be split and if any
2103 of the split devices match with derived sound device */
2104 if (platform_split_snd_device(adev->platform, usecase->out_snd_device,
2105 &num_devices, split_snd_devices) == 0) {
2106 adev->snd_dev_ref_cnt[usecase->out_snd_device]--;
2107 for (i = 0; i < num_devices; i++) {
2108 /* Disable devices that do not match with derived sound device */
2109 if (split_snd_devices[i] != derive_snd_device[usecase->id])
2110 disable_snd_device(adev, split_snd_devices[i]);
2111 }
2112 } else {
2113 disable_snd_device(adev, usecase->out_snd_device);
2114 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002115 }
2116 }
2117
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -07002118 list_for_each(node, &adev->usecase_list) {
2119 usecase = node_to_item(node, struct audio_usecase, list);
2120 if (switch_device[usecase->id]) {
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08002121 if (platform_split_snd_device(adev->platform, usecase->out_snd_device,
2122 &num_devices, split_snd_devices) == 0) {
2123 /* Enable derived sound device only if it does not match with
2124 one of the split sound devices. This is because the matching
2125 sound device was not disabled */
2126 bool should_enable = true;
2127 for (i = 0; i < num_devices; i++) {
2128 if (derive_snd_device[usecase->id] == split_snd_devices[i]) {
2129 should_enable = false;
2130 break;
2131 }
2132 }
2133 if (should_enable)
2134 enable_snd_device(adev, derive_snd_device[usecase->id]);
2135 } else {
2136 enable_snd_device(adev, derive_snd_device[usecase->id]);
2137 }
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -07002138 }
2139 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002140
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002141 /* Re-route all the usecases on the shared backend other than the
2142 specified usecase to new snd devices */
2143 list_for_each(node, &adev->usecase_list) {
2144 usecase = node_to_item(node, struct audio_usecase, list);
Karthik Reddy Katta3abfee22016-02-23 10:55:27 +05302145 /* Update the out_snd_device only before enabling the audio route */
2146 if (switch_device[usecase->id]) {
Chaithanya Krishna Bacharaju49e7db02017-03-14 11:57:26 +05302147 usecase->out_snd_device = derive_snd_device[usecase->id];
Lakshman Chaluvarajuab7e5572020-10-28 19:29:23 +05302148 ALOGD("%s:becf: enabling usecase (%s) on (%s)", __func__,
2149 use_case_table[usecase->id],
2150 platform_get_snd_device_name(usecase->out_snd_device));
2151 /* Update voc calibration before enabling Voice/VoIP route */
2152 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL)
2153 status = platform_switch_voice_call_device_post(adev->platform,
2154 usecase->out_snd_device,
2155 platform_get_input_snd_device(
2156 adev->platform, NULL,
2157 &uc_info->device_list,
2158 usecase->type));
2159 enable_audio_route(adev, usecase);
2160 if (usecase->stream.out && usecase->id == USECASE_AUDIO_PLAYBACK_VOIP) {
2161 out_set_voip_volume(&usecase->stream.out->stream,
2162 usecase->stream.out->volume_l,
2163 usecase->stream.out->volume_r);
Karthik Reddy Katta3abfee22016-02-23 10:55:27 +05302164 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002165 }
2166 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002167 }
2168}
2169
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05302170static void check_usecases_capture_codec_backend(struct audio_device *adev,
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002171 struct audio_usecase *uc_info,
2172 snd_device_t snd_device)
2173{
2174 struct listnode *node;
2175 struct audio_usecase *usecase;
2176 bool switch_device[AUDIO_USECASE_MAX];
2177 int i, num_uc_to_switch = 0;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002178 int backend_check_cond = is_codec_backend_out_device_type(&uc_info->device_list);
kunleiz5cd52b82016-11-07 17:22:52 +08002179 int status = 0;
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002180
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05302181 bool force_routing = platform_check_and_set_capture_codec_backend_cfg(adev, uc_info,
2182 snd_device);
2183 ALOGD("%s:becf: force routing %d", __func__, force_routing);
Dhanalakshmi Siddanib678a802016-12-03 11:51:41 +05302184
2185 /*
2186 * Make sure out devices is checked against out codec backend device and
2187 * also in devices against in codec backend. Checking out device against in
2188 * codec backend or vice versa causes issues.
2189 */
2190 if (uc_info->type == PCM_CAPTURE)
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002191 backend_check_cond = is_codec_backend_in_device_type(&uc_info->device_list);
Kunlei Zhange7cab1a2020-05-17 20:31:05 +08002192
2193 /*
2194 * Island cfg and power mode config needs to set before AFE port start.
2195 * Set force routing in case of voice device was enable before.
2196 */
2197
2198 if (uc_info->type == VOICE_CALL &&
2199 voice_extn_is_voice_power_mode_supported() &&
Kunlei Zhang67cc7072020-12-18 17:16:49 +08002200 is_supported_conc_usecase_for_power_mode_call(adev) &&
Kunlei Zhange7cab1a2020-05-17 20:31:05 +08002201 platform_check_and_update_island_power_status(adev->platform,
2202 uc_info,
2203 snd_device)) {
2204 force_routing = true;
2205 ALOGD("%s:becf: force routing %d for power mode supported device",
2206 __func__, force_routing);
2207 }
2208
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002209 /*
2210 * This function is to make sure that all the active capture usecases
2211 * are always routed to the same input sound device.
2212 * For example, if audio-record and voice-call usecases are currently
2213 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
2214 * is received for voice call then we have to make sure that audio-record
2215 * usecase is also switched to earpiece i.e. voice-dmic-ef,
2216 * because of the limitation that two devices cannot be enabled
2217 * at the same time if they share the same backend.
2218 */
2219 for (i = 0; i < AUDIO_USECASE_MAX; i++)
2220 switch_device[i] = false;
2221
2222 list_for_each(node, &adev->usecase_list) {
2223 usecase = node_to_item(node, struct audio_usecase, list);
Dhanalakshmi Siddanib678a802016-12-03 11:51:41 +05302224 /*
2225 * TODO: Enhance below condition to handle BT sco/USB multi recording
2226 */
Jaideep Sharma477917f2020-03-13 18:13:33 +05302227
2228 bool capture_uc_needs_routing = usecase->type != PCM_PLAYBACK && (usecase != uc_info &&
2229 (usecase->in_snd_device != snd_device || force_routing));
2230 bool call_proxy_snd_device = platform_is_call_proxy_snd_device(snd_device) ||
2231 platform_is_call_proxy_snd_device(usecase->in_snd_device);
2232 if (capture_uc_needs_routing && !call_proxy_snd_device &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002233 ((backend_check_cond &&
2234 (is_codec_backend_in_device_type(&usecase->device_list) ||
Aniket Kumar Lata9c2fd892020-01-22 22:20:00 -08002235 (usecase->type == VOIP_CALL))) ||
Carter Hsu1d2a0532018-10-04 09:24:36 +08002236 ((uc_info->type == VOICE_CALL &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002237 is_single_device_type_equal(&usecase->device_list,
2238 AUDIO_DEVICE_IN_VOICE_CALL)) ||
Anver sadhique4fdc6992022-01-21 12:26:28 +05302239 platform_check_all_backends_match(snd_device,\
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002240 usecase->in_snd_device))) &&
Anish Kumara020a7c2014-10-17 11:13:22 -07002241 (usecase->id != USECASE_AUDIO_SPKR_CALIB_TX)) {
Jaideep Sharma477917f2020-03-13 18:13:33 +05302242 ALOGD("%s: Usecase (%s) is active on (%s) - disabling ..",
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002243 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -07002244 platform_get_snd_device_name(usecase->in_snd_device));
Haynes Mathew George1376ca62014-04-24 11:55:48 -07002245 disable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002246 switch_device[usecase->id] = true;
2247 num_uc_to_switch++;
2248 }
2249 }
2250
2251 if (num_uc_to_switch) {
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07002252 /* All streams have been de-routed. Disable the device */
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002253
Venkata Narendra Kumar Gutta7610e632014-04-14 23:16:38 +05302254 /* Make sure the previous devices to be disabled first and then enable the
2255 selected devices */
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002256 list_for_each(node, &adev->usecase_list) {
2257 usecase = node_to_item(node, struct audio_usecase, list);
2258 if (switch_device[usecase->id]) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07002259 disable_snd_device(adev, usecase->in_snd_device);
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -08002260 }
2261 }
2262
2263 list_for_each(node, &adev->usecase_list) {
2264 usecase = node_to_item(node, struct audio_usecase, list);
2265 if (switch_device[usecase->id]) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07002266 enable_snd_device(adev, snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002267 }
2268 }
2269
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002270 /* Re-route all the usecases on the shared backend other than the
2271 specified usecase to new snd devices */
2272 list_for_each(node, &adev->usecase_list) {
2273 usecase = node_to_item(node, struct audio_usecase, list);
2274 /* Update the in_snd_device only before enabling the audio route */
2275 if (switch_device[usecase->id] ) {
2276 usecase->in_snd_device = snd_device;
Lakshman Chaluvarajuab7e5572020-10-28 19:29:23 +05302277 /* Update voc calibration before enabling Voice/VoIP route */
2278 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) {
2279 snd_device_t voip_snd_device;
2280 voip_snd_device = platform_get_output_snd_device(adev->platform,
2281 usecase->stream.out,
2282 usecase->type);
2283 status = platform_switch_voice_call_device_post(adev->platform,
2284 voip_snd_device,
2285 usecase->in_snd_device);
kunleiz5cd52b82016-11-07 17:22:52 +08002286 }
Lakshman Chaluvarajuab7e5572020-10-28 19:29:23 +05302287 enable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002288 }
2289 }
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002290 }
2291}
2292
Mingming Yin3a941d42016-02-17 18:08:05 -08002293static void reset_hdmi_sink_caps(struct stream_out *out) {
2294 int i = 0;
2295
2296 for (i = 0; i<= MAX_SUPPORTED_CHANNEL_MASKS; i++) {
2297 out->supported_channel_masks[i] = 0;
2298 }
2299 for (i = 0; i<= MAX_SUPPORTED_FORMATS; i++) {
2300 out->supported_formats[i] = 0;
2301 }
2302 for (i = 0; i<= MAX_SUPPORTED_SAMPLE_RATES; i++) {
2303 out->supported_sample_rates[i] = 0;
2304 }
2305}
2306
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002307/* must be called with hw device mutex locked */
Mingming Yin3a941d42016-02-17 18:08:05 -08002308static int read_hdmi_sink_caps(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002309{
Mingming Yin3a941d42016-02-17 18:08:05 -08002310 int ret = 0, i = 0, j = 0;
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07002311 int channels = platform_edid_get_max_channels_v2(out->dev->platform,
2312 out->extconn.cs.controller,
2313 out->extconn.cs.stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002314
Mingming Yin3a941d42016-02-17 18:08:05 -08002315 reset_hdmi_sink_caps(out);
2316
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07002317 /* Cache ext disp type */
Vignesh Kulothungan1b3957e2019-12-02 16:16:51 -08002318 ret = platform_get_ext_disp_type_v2(adev->platform,
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07002319 out->extconn.cs.controller,
Vignesh Kulothungan1b3957e2019-12-02 16:16:51 -08002320 out->extconn.cs.stream);
2321 if(ret < 0) {
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07002322 ALOGE("%s: Failed to query disp type, ret:%d", __func__, ret);
Garmond Leung37850ab2016-10-06 11:42:18 -07002323 return -EINVAL;
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07002324 }
2325
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002326 switch (channels) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002327 case 8:
Mingming Yin3a941d42016-02-17 18:08:05 -08002328 ALOGV("%s: HDMI supports 7.1 channels", __func__);
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -07002329 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_7POINT1;
Mingming Yin3a941d42016-02-17 18:08:05 -08002330 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_6POINT1;
2331 case 6:
2332 ALOGV("%s: HDMI supports 5.1 channels", __func__);
2333 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_5POINT1;
2334 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_PENTA;
2335 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_QUAD;
2336 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_SURROUND;
2337 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_2POINT1;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002338 break;
2339 default:
Mingming Yin3a941d42016-02-17 18:08:05 -08002340 ALOGE("invalid/nonstandard channal count[%d]",channels);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002341 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002342 break;
2343 }
Mingming Yin3a941d42016-02-17 18:08:05 -08002344
2345 // check channel format caps
2346 i = 0;
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07002347 if (platform_is_edid_supported_format_v2(out->dev->platform, AUDIO_FORMAT_AC3,
2348 out->extconn.cs.controller,
2349 out->extconn.cs.stream)) {
Mingming Yin3a941d42016-02-17 18:08:05 -08002350 ALOGV(":%s HDMI supports AC3/EAC3 formats", __func__);
2351 out->supported_formats[i++] = AUDIO_FORMAT_AC3;
2352 //Adding EAC3/EAC3_JOC formats if AC3 is supported by the sink.
2353 //EAC3/EAC3_JOC will be converted to AC3 for decoding if needed
2354 out->supported_formats[i++] = AUDIO_FORMAT_E_AC3;
2355 out->supported_formats[i++] = AUDIO_FORMAT_E_AC3_JOC;
2356 }
2357
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07002358 if (platform_is_edid_supported_format_v2(out->dev->platform, AUDIO_FORMAT_DOLBY_TRUEHD,
2359 out->extconn.cs.controller,
2360 out->extconn.cs.stream)) {
Ben Romberger1aaaf862017-04-06 17:49:46 -07002361 ALOGV(":%s HDMI supports TRUE HD format", __func__);
2362 out->supported_formats[i++] = AUDIO_FORMAT_DOLBY_TRUEHD;
2363 }
2364
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07002365 if (platform_is_edid_supported_format_v2(out->dev->platform, AUDIO_FORMAT_DTS,
2366 out->extconn.cs.controller,
2367 out->extconn.cs.stream)) {
Mingming Yin3a941d42016-02-17 18:08:05 -08002368 ALOGV(":%s HDMI supports DTS format", __func__);
2369 out->supported_formats[i++] = AUDIO_FORMAT_DTS;
2370 }
2371
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07002372 if (platform_is_edid_supported_format_v2(out->dev->platform, AUDIO_FORMAT_DTS_HD,
2373 out->extconn.cs.controller,
2374 out->extconn.cs.stream)) {
Mingming Yin3a941d42016-02-17 18:08:05 -08002375 ALOGV(":%s HDMI supports DTS HD format", __func__);
2376 out->supported_formats[i++] = AUDIO_FORMAT_DTS_HD;
2377 }
2378
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07002379 if (platform_is_edid_supported_format_v2(out->dev->platform, AUDIO_FORMAT_IEC61937,
2380 out->extconn.cs.controller,
2381 out->extconn.cs.stream)) {
Naresh Tanniru928f0862017-04-07 16:44:23 -07002382 ALOGV(":%s HDMI supports IEC61937 format", __func__);
2383 out->supported_formats[i++] = AUDIO_FORMAT_IEC61937;
2384 }
2385
Mingming Yin3a941d42016-02-17 18:08:05 -08002386
2387 // check sample rate caps
2388 i = 0;
2389 for (j = 0; j < MAX_SUPPORTED_SAMPLE_RATES; j++) {
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07002390 if (platform_is_edid_supported_sample_rate_v2(out->dev->platform, out_hdmi_sample_rates[j],
2391 out->extconn.cs.controller,
2392 out->extconn.cs.stream)) {
Mingming Yin3a941d42016-02-17 18:08:05 -08002393 ALOGV(":%s HDMI supports sample rate:%d", __func__, out_hdmi_sample_rates[j]);
2394 out->supported_sample_rates[i++] = out_hdmi_sample_rates[j];
2395 }
2396 }
2397
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002398 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002399}
2400
Haynes Mathew George484e8d22017-07-31 18:55:17 -07002401static inline ssize_t read_usb_sup_sample_rates(bool is_playback __unused,
2402 uint32_t *supported_sample_rates __unused,
2403 uint32_t max_rates __unused)
2404{
2405 ssize_t count = audio_extn_usb_get_sup_sample_rates(is_playback,
2406 supported_sample_rates,
2407 max_rates);
Ashish Jain4847e9d2017-08-17 19:16:57 +05302408 ssize_t i = 0;
2409
2410 for (i=0; i<count; i++) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07002411 ALOGV("%s %s %d", __func__, is_playback ? "P" : "C",
2412 supported_sample_rates[i]);
2413 }
2414 return count;
2415}
2416
2417static inline int read_usb_sup_channel_masks(bool is_playback,
2418 audio_channel_mask_t *supported_channel_masks,
2419 uint32_t max_masks)
2420{
2421 int channels = audio_extn_usb_get_max_channels(is_playback);
2422 int channel_count;
2423 uint32_t num_masks = 0;
2424 if (channels > MAX_HIFI_CHANNEL_COUNT)
2425 channels = MAX_HIFI_CHANNEL_COUNT;
2426
2427 if (is_playback) {
Eric Laurent68a87112019-05-01 18:07:29 -07002428 // start from 2 channels as framework currently doesn't support mono.
2429 if (channels >= FCC_2) {
2430 supported_channel_masks[num_masks++] = audio_channel_out_mask_from_count(FCC_2);
2431 }
2432 for (channel_count = FCC_2;
2433 channel_count <= channels && num_masks < max_masks;
2434 ++channel_count) {
2435 supported_channel_masks[num_masks++] =
2436 audio_channel_mask_for_index_assignment_from_count(channel_count);
2437 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07002438 } else {
vincenttewf51c94e2019-05-07 10:28:53 +08002439 // For capture we report all supported channel masks from 1 channel up.
2440 channel_count = MIN_CHANNEL_COUNT;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07002441 // audio_channel_in_mask_from_count() does the right conversion to either positional or
2442 // indexed mask
Eric Laurent68a87112019-05-01 18:07:29 -07002443 for ( ; channel_count <= channels && num_masks < max_masks; channel_count++) {
2444 audio_channel_mask_t mask = AUDIO_CHANNEL_NONE;
2445 if (channel_count <= FCC_2) {
2446 mask = audio_channel_in_mask_from_count(channel_count);
2447 supported_channel_masks[num_masks++] = mask;
2448 }
2449 const audio_channel_mask_t index_mask =
2450 audio_channel_mask_for_index_assignment_from_count(channel_count);
2451 if (mask != index_mask && num_masks < max_masks) { // ensure index mask added.
2452 supported_channel_masks[num_masks++] = index_mask;
2453 }
2454 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07002455 }
Lakshman Chaluvarajub79fafd2018-11-21 10:24:37 +05302456
vincenttewf51c94e2019-05-07 10:28:53 +08002457 for (size_t i = 0; i < num_masks; ++i) {
2458 ALOGV("%s: %s supported ch %d supported_channel_masks[%zu] %08x num_masks %d", __func__,
2459 is_playback ? "P" : "C", channels, i, supported_channel_masks[i], num_masks);
Lakshman Chaluvarajub79fafd2018-11-21 10:24:37 +05302460 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07002461 return num_masks;
2462}
2463
2464static inline int read_usb_sup_formats(bool is_playback __unused,
2465 audio_format_t *supported_formats,
2466 uint32_t max_formats __unused)
2467{
2468 int bitwidth = audio_extn_usb_get_max_bit_width(is_playback);
2469 switch (bitwidth) {
2470 case 24:
2471 // XXX : usb.c returns 24 for s24 and s24_le?
2472 supported_formats[0] = AUDIO_FORMAT_PCM_24_BIT_PACKED;
2473 break;
2474 case 32:
2475 supported_formats[0] = AUDIO_FORMAT_PCM_32_BIT;
2476 break;
2477 case 16:
2478 default :
2479 supported_formats[0] = AUDIO_FORMAT_PCM_16_BIT;
2480 break;
2481 }
2482 ALOGV("%s: %s supported format %d", __func__,
2483 is_playback ? "P" : "C", bitwidth);
2484 return 1;
2485}
2486
2487static inline int read_usb_sup_params_and_compare(bool is_playback,
2488 audio_format_t *format,
2489 audio_format_t *supported_formats,
2490 uint32_t max_formats,
2491 audio_channel_mask_t *mask,
2492 audio_channel_mask_t *supported_channel_masks,
2493 uint32_t max_masks,
2494 uint32_t *rate,
2495 uint32_t *supported_sample_rates,
2496 uint32_t max_rates) {
2497 int ret = 0;
2498 int num_formats;
2499 int num_masks;
2500 int num_rates;
2501 int i;
2502
2503 num_formats = read_usb_sup_formats(is_playback, supported_formats,
2504 max_formats);
2505 num_masks = read_usb_sup_channel_masks(is_playback, supported_channel_masks,
2506 max_masks);
2507
2508 num_rates = read_usb_sup_sample_rates(is_playback,
2509 supported_sample_rates, max_rates);
2510
2511#define LUT(table, len, what, dflt) \
2512 for (i=0; i<len && (table[i] != what); i++); \
2513 if (i==len) { ret |= (what == dflt ? 0 : -1); what=table[0]; }
2514
2515 LUT(supported_formats, num_formats, *format, AUDIO_FORMAT_DEFAULT);
2516 LUT(supported_channel_masks, num_masks, *mask, AUDIO_CHANNEL_NONE);
2517 LUT(supported_sample_rates, num_rates, *rate, 0);
2518
2519#undef LUT
2520 return ret < 0 ? -EINVAL : 0; // HACK TBD
2521}
2522
Alexy Josephb1379942016-01-29 15:49:38 -08002523audio_usecase_t get_usecase_id_from_usecase_type(const struct audio_device *adev,
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -08002524 usecase_type_t type)
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002525{
2526 struct audio_usecase *usecase;
2527 struct listnode *node;
2528
2529 list_for_each(node, &adev->usecase_list) {
2530 usecase = node_to_item(node, struct audio_usecase, list);
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -08002531 if (usecase->type == type) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002532 ALOGV("%s: usecase id %d", __func__, usecase->id);
2533 return usecase->id;
2534 }
2535 }
2536 return USECASE_INVALID;
2537}
2538
Alexy Josephb1379942016-01-29 15:49:38 -08002539struct audio_usecase *get_usecase_from_list(const struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002540 audio_usecase_t uc_id)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002541{
2542 struct audio_usecase *usecase;
2543 struct listnode *node;
2544
2545 list_for_each(node, &adev->usecase_list) {
2546 usecase = node_to_item(node, struct audio_usecase, list);
2547 if (usecase->id == uc_id)
2548 return usecase;
2549 }
2550 return NULL;
2551}
2552
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302553/*
2554 * is a true native playback active
2555 */
2556bool audio_is_true_native_stream_active(struct audio_device *adev)
2557{
2558 bool active = false;
2559 int i = 0;
2560 struct listnode *node;
2561
2562 if (NATIVE_AUDIO_MODE_TRUE_44_1 != platform_get_native_support()) {
2563 ALOGV("%s:napb: not in true mode or non hdphones device",
2564 __func__);
2565 active = false;
2566 goto exit;
2567 }
2568
2569 list_for_each(node, &adev->usecase_list) {
2570 struct audio_usecase *uc;
2571 uc = node_to_item(node, struct audio_usecase, list);
2572 struct stream_out *curr_out =
2573 (struct stream_out*) uc->stream.out;
2574
2575 if (curr_out && PCM_PLAYBACK == uc->type) {
2576 ALOGD("%s:napb: (%d) (%s)id (%d) sr %d bw "
2577 "(%d) device %s", __func__, i++, use_case_table[uc->id],
2578 uc->id, curr_out->sample_rate,
2579 curr_out->bit_width,
2580 platform_get_snd_device_name(uc->out_snd_device));
2581
2582 if (is_offload_usecase(uc->id) &&
2583 (curr_out->sample_rate == OUTPUT_SAMPLING_RATE_44100)) {
2584 active = true;
2585 ALOGD("%s:napb:native stream detected", __func__);
2586 }
2587 }
2588 }
2589exit:
2590 return active;
2591}
2592
Xiaojun Sang785b5da2017-08-03 15:52:29 +08002593uint32_t adev_get_dsp_bit_width_enforce_mode()
2594{
2595 if (adev == NULL) {
2596 ALOGE("%s: adev is null. Disable DSP bit width enforce mode.\n", __func__);
2597 return 0;
2598 }
2599 return adev->dsp_bit_width_enforce_mode;
2600}
2601
2602static uint32_t adev_init_dsp_bit_width_enforce_mode(struct mixer *mixer)
2603{
2604 char value[PROPERTY_VALUE_MAX];
2605 int trial;
2606 uint32_t dsp_bit_width_enforce_mode = 0;
2607
2608 if (!mixer) {
2609 ALOGE("%s: adev mixer is null. cannot update DSP bitwidth.\n",
2610 __func__);
2611 return 0;
2612 }
2613
2614 if (property_get("persist.vendor.audio_hal.dsp_bit_width_enforce_mode",
2615 value, NULL) > 0) {
2616 trial = atoi(value);
2617 switch (trial) {
2618 case 16:
2619 dsp_bit_width_enforce_mode = 16;
2620 break;
2621 case 24:
2622 dsp_bit_width_enforce_mode = 24;
2623 break;
2624 case 32:
2625 dsp_bit_width_enforce_mode = 32;
2626 break;
2627 default:
2628 dsp_bit_width_enforce_mode = 0;
2629 ALOGD("%s Dynamic DSP bitwidth config is disabled.", __func__);
2630 break;
2631 }
2632 }
2633
2634 return dsp_bit_width_enforce_mode;
2635}
2636
2637static void audio_enable_asm_bit_width_enforce_mode(struct mixer *mixer,
2638 uint32_t enforce_mode,
2639 bool enable)
2640{
2641 struct mixer_ctl *ctl = NULL;
2642 const char *mixer_ctl_name = "ASM Bit Width";
2643 uint32_t asm_bit_width_mode = 0;
2644
2645 if (enforce_mode == 0) {
2646 ALOGD("%s: DSP bitwidth feature is disabled.", __func__);
2647 return;
2648 }
2649
2650 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
2651 if (!ctl) {
2652 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2653 __func__, mixer_ctl_name);
2654 return;
2655 }
2656
2657 if (enable)
2658 asm_bit_width_mode = enforce_mode;
2659 else
2660 asm_bit_width_mode = 0;
2661
2662 ALOGV("%s DSP bit width feature status is %d width=%d",
2663 __func__, enable, asm_bit_width_mode);
2664 if (mixer_ctl_set_value(ctl, 0, asm_bit_width_mode) < 0)
2665 ALOGE("%s: Could not set ASM biwidth %d", __func__,
2666 asm_bit_width_mode);
2667
2668 return;
2669}
2670
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05302671/*
2672 * if native DSD playback active
2673 */
2674bool audio_is_dsd_native_stream_active(struct audio_device *adev)
2675{
2676 bool active = false;
2677 struct listnode *node = NULL;
2678 struct audio_usecase *uc = NULL;
2679 struct stream_out *curr_out = NULL;
2680
2681 list_for_each(node, &adev->usecase_list) {
2682 uc = node_to_item(node, struct audio_usecase, list);
2683 curr_out = (struct stream_out*) uc->stream.out;
2684
2685 if (curr_out && PCM_PLAYBACK == uc->type &&
2686 (DSD_NATIVE_BACKEND == platform_get_backend_index(uc->out_snd_device))) {
2687 active = true;
2688 ALOGV("%s:DSD playback is active", __func__);
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05302689 break;
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05302690 }
2691 }
2692 return active;
2693}
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302694
2695static bool force_device_switch(struct audio_usecase *usecase)
2696{
2697 bool ret = false;
2698 bool is_it_true_mode = false;
2699
Zhou Song30f2c3e2018-02-08 14:02:15 +08002700 if (usecase->type == PCM_CAPTURE ||
Surendar Karka93cd25a2018-08-28 14:21:37 +05302701 usecase->type == TRANSCODE_LOOPBACK_RX ||
2702 usecase->type == TRANSCODE_LOOPBACK_TX) {
Zhou Song30f2c3e2018-02-08 14:02:15 +08002703 return false;
2704 }
2705
Aalique Grahamecbc46a22017-10-05 10:30:23 -07002706 if(usecase->stream.out == NULL) {
2707 ALOGE("%s: stream.out is NULL", __func__);
2708 return false;
2709 }
2710
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302711 if (is_offload_usecase(usecase->id) &&
Xiaojun Sang869f2012016-02-23 16:33:07 +08002712 (usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002713 (compare_device_type(&usecase->stream.out->device_list, AUDIO_DEVICE_OUT_WIRED_HEADSET) ||
2714 compare_device_type(&usecase->stream.out->device_list, AUDIO_DEVICE_OUT_WIRED_HEADPHONE))) {
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302715 is_it_true_mode = (NATIVE_AUDIO_MODE_TRUE_44_1 == platform_get_native_support()? true : false);
2716 if ((is_it_true_mode && !adev->native_playback_enabled) ||
2717 (!is_it_true_mode && adev->native_playback_enabled)){
2718 ret = true;
2719 ALOGD("napb: time to toggle native mode");
2720 }
2721 }
2722
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302723 // Force all a2dp output devices to reconfigure for proper AFE encode format
Ashish Jainc597d102016-12-12 10:31:34 +05302724 //Also handle a case where in earlier a2dp start failed as A2DP stream was
2725 //in suspended state, hence try to trigger a retry when we again get a routing request.
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002726 if(is_a2dp_out_device_type(&usecase->stream.out->device_list) &&
Ashish Jainc597d102016-12-12 10:31:34 +05302727 audio_extn_a2dp_is_force_device_switch()) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302728 ALOGD("Force a2dp device switch to update new encoder config");
2729 ret = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002730 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302731
Florian Pfister1a84f312018-07-19 14:38:18 +02002732 if (usecase->stream.out->stream_config_changed) {
Manish Dewangan671a4202017-08-18 17:30:46 +05302733 ALOGD("Force stream_config_changed to update iec61937 transmission config");
2734 return true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002735 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302736 return ret;
2737}
2738
Aalique Grahame22e49102018-12-18 14:23:57 -08002739static void stream_app_type_cfg_init(struct stream_app_type_cfg *cfg)
2740{
2741 cfg->gain[0] = cfg->gain[1] = APP_TYPE_GAIN_DEFAULT;
2742}
2743
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302744bool is_btsco_device(snd_device_t out_snd_device, snd_device_t in_snd_device)
2745{
2746 bool ret=false;
2747 if ((out_snd_device == SND_DEVICE_OUT_BT_SCO ||
Zhou Song5657f492019-08-07 11:30:39 +08002748 out_snd_device == SND_DEVICE_OUT_BT_SCO_WB ||
2749 out_snd_device == SND_DEVICE_OUT_BT_SCO_SWB) ||
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302750 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_WB_NREC ||
2751 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_WB ||
Zhou Song5657f492019-08-07 11:30:39 +08002752 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_SWB ||
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302753 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_NREC ||
Mingshu Pang16093502020-04-20 11:21:16 +08002754 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC ||
2755 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_SWB_NREC)
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302756 ret = true;
2757
2758 return ret;
2759}
2760
2761bool is_a2dp_device(snd_device_t out_snd_device)
2762{
2763 bool ret=false;
2764 if (out_snd_device == SND_DEVICE_OUT_BT_A2DP)
2765 ret = true;
2766
2767 return ret;
2768}
2769
2770bool is_bt_soc_on(struct audio_device *adev)
2771{
2772 struct mixer_ctl *ctl;
2773 char *mixer_ctl_name = "BT SOC status";
2774 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
2775 bool bt_soc_status = true;
2776 if (!ctl) {
2777 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2778 __func__, mixer_ctl_name);
2779 /*This is to ensure we dont break targets which dont have the kernel change*/
2780 return true;
2781 }
2782 bt_soc_status = mixer_ctl_get_value(ctl, 0);
2783 ALOGD("BT SOC status: %d",bt_soc_status);
2784 return bt_soc_status;
2785}
2786
Zhou Song331c8e52019-08-26 14:16:12 +08002787static int configure_btsco_sample_rate(snd_device_t snd_device)
2788{
2789 struct mixer_ctl *ctl = NULL;
2790 struct mixer_ctl *ctl_sr_rx = NULL, *ctl_sr_tx = NULL, *ctl_sr = NULL;
2791 char *rate_str = NULL;
2792 bool is_rx_dev = true;
2793
2794 if (is_btsco_device(snd_device, snd_device)) {
2795 ctl_sr_tx = mixer_get_ctl_by_name(adev->mixer, "BT SampleRate TX");
2796 ctl_sr_rx = mixer_get_ctl_by_name(adev->mixer, "BT SampleRate RX");
2797 if (!ctl_sr_tx || !ctl_sr_rx) {
2798 ctl_sr = mixer_get_ctl_by_name(adev->mixer, "BT SampleRate");
2799 if (!ctl_sr)
2800 return -ENOSYS;
2801 }
2802
2803 switch (snd_device) {
2804 case SND_DEVICE_OUT_BT_SCO:
2805 rate_str = "KHZ_8";
2806 break;
2807 case SND_DEVICE_IN_BT_SCO_MIC_NREC:
2808 case SND_DEVICE_IN_BT_SCO_MIC:
2809 rate_str = "KHZ_8";
2810 is_rx_dev = false;
2811 break;
2812 case SND_DEVICE_OUT_BT_SCO_WB:
2813 rate_str = "KHZ_16";
2814 break;
2815 case SND_DEVICE_IN_BT_SCO_MIC_WB_NREC:
2816 case SND_DEVICE_IN_BT_SCO_MIC_WB:
2817 rate_str = "KHZ_16";
2818 is_rx_dev = false;
2819 break;
2820 default:
2821 return 0;
2822 }
2823
2824 ctl = (ctl_sr == NULL) ? (is_rx_dev ? ctl_sr_rx : ctl_sr_tx) : ctl_sr;
2825 if (mixer_ctl_set_enum_by_string(ctl, rate_str) != 0)
2826 return -ENOSYS;
2827 }
2828 return 0;
2829}
2830
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302831int out_standby_l(struct audio_stream *stream);
2832
Eric Laurent637e2d42018-11-15 12:24:31 -08002833struct stream_in *adev_get_active_input(const struct audio_device *adev)
2834{
2835 struct listnode *node;
2836 struct stream_in *last_active_in = NULL;
2837
2838 /* Get last added active input.
2839 * TODO: We may use a priority mechanism to pick highest priority active source */
2840 list_for_each(node, &adev->usecase_list)
2841 {
2842 struct audio_usecase *usecase = node_to_item(node, struct audio_usecase, list);
2843 if (usecase->type == PCM_CAPTURE && usecase->stream.in != NULL)
2844 last_active_in = usecase->stream.in;
2845 }
2846
2847 return last_active_in;
2848}
2849
2850struct stream_in *get_voice_communication_input(const struct audio_device *adev)
2851{
2852 struct listnode *node;
2853
2854 /* First check active inputs with voice communication source and then
2855 * any input if audio mode is in communication */
2856 list_for_each(node, &adev->usecase_list)
2857 {
2858 struct audio_usecase *usecase = node_to_item(node, struct audio_usecase, list);
2859 if (usecase->type == PCM_CAPTURE && usecase->stream.in != NULL &&
2860 usecase->stream.in->source == AUDIO_SOURCE_VOICE_COMMUNICATION)
2861 return usecase->stream.in;
2862 }
2863 if (adev->mode == AUDIO_MODE_IN_COMMUNICATION)
2864 return adev_get_active_input(adev);
2865
2866 return NULL;
2867}
2868
Carter Hsu2e429db2019-05-14 18:50:52 +08002869/*
2870 * Aligned with policy.h
2871 */
2872static inline int source_priority(int inputSource)
2873{
2874 switch (inputSource) {
2875 case AUDIO_SOURCE_VOICE_COMMUNICATION:
2876 return 9;
2877 case AUDIO_SOURCE_CAMCORDER:
2878 return 8;
2879 case AUDIO_SOURCE_VOICE_PERFORMANCE:
2880 return 7;
2881 case AUDIO_SOURCE_UNPROCESSED:
2882 return 6;
2883 case AUDIO_SOURCE_MIC:
2884 return 5;
2885 case AUDIO_SOURCE_ECHO_REFERENCE:
2886 return 4;
2887 case AUDIO_SOURCE_FM_TUNER:
2888 return 3;
2889 case AUDIO_SOURCE_VOICE_RECOGNITION:
2890 return 2;
2891 case AUDIO_SOURCE_HOTWORD:
2892 return 1;
2893 default:
2894 break;
2895 }
2896 return 0;
2897}
2898
2899static struct stream_in *get_priority_input(struct audio_device *adev)
2900{
2901 struct listnode *node;
2902 struct audio_usecase *usecase;
2903 int last_priority = 0, priority;
2904 struct stream_in *priority_in = NULL;
2905 struct stream_in *in;
2906
2907 list_for_each(node, &adev->usecase_list) {
2908 usecase = node_to_item(node, struct audio_usecase, list);
2909 if (usecase->type == PCM_CAPTURE) {
2910 in = usecase->stream.in;
2911 if (!in)
2912 continue;
Krishna Kishor Jha0b7175d2022-07-28 22:33:46 +05302913
2914 if (USECASE_AUDIO_RECORD_FM_VIRTUAL == usecase->id)
2915 continue;
Carter Hsu2e429db2019-05-14 18:50:52 +08002916 priority = source_priority(in->source);
2917
2918 if (priority > last_priority) {
2919 last_priority = priority;
2920 priority_in = in;
2921 }
2922 }
2923 }
2924 return priority_in;
2925}
2926
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002927int select_devices(struct audio_device *adev, audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002928{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002929 snd_device_t out_snd_device = SND_DEVICE_NONE;
2930 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002931 struct audio_usecase *usecase = NULL;
2932 struct audio_usecase *vc_usecase = NULL;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002933 struct audio_usecase *voip_usecase = NULL;
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -08002934 struct audio_usecase *hfp_usecase = NULL;
Siddartha Shaik44dd7702017-06-14 12:13:25 +05302935 struct stream_out stream_out;
Vimal Puthanveed41fcff22014-01-23 15:56:53 -08002936 audio_usecase_t hfp_ucid;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002937 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002938
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302939 ALOGD("%s for use case (%s)", __func__, use_case_table[uc_id]);
2940
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002941 usecase = get_usecase_from_list(adev, uc_id);
2942 if (usecase == NULL) {
2943 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
2944 return -EINVAL;
2945 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002946
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002947 if ((usecase->type == VOICE_CALL) ||
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -08002948 (usecase->type == VOIP_CALL) ||
Derek Chena30a5f42019-12-03 11:17:09 -05002949 (usecase->type == PCM_HFP_CALL)||
Fei Tongaffdf732020-02-20 20:39:05 +08002950 (usecase->type == ICC_CALL) ||
2951 (usecase->type == SYNTH_LOOPBACK)) {
Aditya Bavanaribdda2f22016-10-19 15:02:05 +05302952 if(usecase->stream.out == NULL) {
2953 ALOGE("%s: stream.out is NULL", __func__);
2954 return -EINVAL;
2955 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002956 if (compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_BUS)) {
Guodong Hu267bdf82019-08-12 19:22:32 +08002957 out_snd_device = audio_extn_auto_hal_get_output_snd_device(adev,
2958 uc_id);
2959 in_snd_device = audio_extn_auto_hal_get_input_snd_device(adev,
2960 uc_id);
2961 } else {
2962 out_snd_device = platform_get_output_snd_device(adev->platform,
Jaideep Sharma477917f2020-03-13 18:13:33 +05302963 usecase->stream.out, usecase->type);
Guodong Hu267bdf82019-08-12 19:22:32 +08002964 in_snd_device = platform_get_input_snd_device(adev->platform,
2965 NULL,
Jaideep Sharma477917f2020-03-13 18:13:33 +05302966 &usecase->stream.out->device_list,
2967 usecase->type);
Guodong Hu267bdf82019-08-12 19:22:32 +08002968 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002969 assign_devices(&usecase->device_list, &usecase->stream.out->device_list);
Surendar Karka93cd25a2018-08-28 14:21:37 +05302970 } else if (usecase->type == TRANSCODE_LOOPBACK_RX) {
Siddartha Shaik31b530e2017-05-19 15:26:33 +05302971 if (usecase->stream.inout == NULL) {
2972 ALOGE("%s: stream.inout is NULL", __func__);
2973 return -EINVAL;
2974 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002975 assign_devices(&stream_out.device_list, &usecase->stream.inout->out_config.device_list);
Siddartha Shaik44dd7702017-06-14 12:13:25 +05302976 stream_out.sample_rate = usecase->stream.inout->out_config.sample_rate;
2977 stream_out.format = usecase->stream.inout->out_config.format;
2978 stream_out.channel_mask = usecase->stream.inout->out_config.channel_mask;
Jaideep Sharma477917f2020-03-13 18:13:33 +05302979 out_snd_device = platform_get_output_snd_device(adev->platform, &stream_out, usecase->type);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002980 assign_devices(&usecase->device_list,
2981 &usecase->stream.inout->out_config.device_list);
Zhenlin Lian4f947842022-05-14 15:50:52 +05302982 clear_devices(&stream_out.device_list);
Surendar Karka93cd25a2018-08-28 14:21:37 +05302983 } else if (usecase->type == TRANSCODE_LOOPBACK_TX ) {
2984 if (usecase->stream.inout == NULL) {
2985 ALOGE("%s: stream.inout is NULL", __func__);
2986 return -EINVAL;
2987 }
Manisha Agarwal03297972020-04-17 15:36:55 +05302988 struct listnode out_devices;
2989 list_init(&out_devices);
2990 in_snd_device = platform_get_input_snd_device(adev->platform, NULL,
2991 &out_devices, usecase->type);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002992 assign_devices(&usecase->device_list,
2993 &usecase->stream.inout->in_config.device_list);
Zhenlin Lian4f947842022-05-14 15:50:52 +05302994 clear_devices(&out_devices);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002995 } else {
2996 /*
2997 * If the voice call is active, use the sound devices of voice call usecase
2998 * so that it would not result any device switch. All the usecases will
2999 * be switched to new device when select_devices() is called for voice call
3000 * usecase. This is to avoid switching devices for voice call when
3001 * check_usecases_codec_backend() is called below.
Alexy Joseph79dfa3c2016-04-20 18:44:56 -07003002 * choose voice call device only if the use case device is
3003 * also using the codec backend
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003004 */
Shiv Maliyappanahallibb4cf0b2016-01-21 11:30:06 -08003005 if (voice_is_in_call(adev) && adev->mode != AUDIO_MODE_NORMAL) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07003006 vc_usecase = get_usecase_from_list(adev,
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -08003007 get_usecase_id_from_usecase_type(adev, VOICE_CALL));
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003008 if ((vc_usecase) && ((is_codec_backend_out_device_type(&vc_usecase->device_list) &&
3009 is_codec_backend_out_device_type(&usecase->device_list)) ||
3010 (is_codec_backend_out_device_type(&vc_usecase->device_list) &&
3011 is_codec_backend_in_device_type(&usecase->device_list)) ||
3012 is_single_device_type_equal(&vc_usecase->device_list,
3013 AUDIO_DEVICE_OUT_HEARING_AID) ||
3014 is_single_device_type_equal(&usecase->device_list,
Robert Lee8a6aaf32019-09-20 16:40:19 +08003015 AUDIO_DEVICE_IN_VOICE_CALL) ||
3016 (is_single_device_type_equal(&usecase->device_list,
Gautam Manam8fa06162021-09-24 10:41:29 +05303017 AUDIO_DEVICE_IN_BUILTIN_MIC) &&
3018 is_single_device_type_equal(&vc_usecase->device_list,
3019 AUDIO_DEVICE_OUT_USB_HEADSET)) ||
3020 (is_single_device_type_equal(&usecase->device_list,
Robert Lee8a6aaf32019-09-20 16:40:19 +08003021 AUDIO_DEVICE_IN_USB_HEADSET) &&
3022 is_single_device_type_equal(&vc_usecase->device_list,
Kamalakar Yalasiri5edba852021-01-28 14:52:38 +05303023 AUDIO_DEVICE_OUT_USB_HEADSET))||
Gautam Manamf4002142021-09-13 22:05:56 +05303024 (is_single_device_type_equal(&usecase->device_list,
3025 AUDIO_DEVICE_IN_USB_HEADSET) &&
3026 is_codec_backend_out_device_type(&vc_usecase->device_list)) ||
Kamalakar Yalasiri5edba852021-01-28 14:52:38 +05303027 (is_single_device_type_equal(&usecase->device_list,
3028 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) &&
3029 is_codec_backend_out_device_type(&vc_usecase->device_list)))) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003030 in_snd_device = vc_usecase->in_snd_device;
3031 out_snd_device = vc_usecase->out_snd_device;
3032 }
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08003033 } else if (voice_extn_compress_voip_is_active(adev)) {
yidongh02ef86f2017-04-21 15:36:04 +08003034 bool out_snd_device_backend_match = true;
yidongh47785a82017-05-08 19:29:29 +08003035 voip_usecase = get_usecase_from_list(adev, USECASE_COMPRESS_VOIP_CALL);
yidongh6261d8e2017-05-15 17:04:02 +08003036 if ((voip_usecase != NULL) &&
3037 (usecase->type == PCM_PLAYBACK) &&
3038 (usecase->stream.out != NULL)) {
yidongh02ef86f2017-04-21 15:36:04 +08003039 out_snd_device_backend_match = platform_check_backends_match(
3040 voip_usecase->out_snd_device,
3041 platform_get_output_snd_device(
3042 adev->platform,
Jaideep Sharma477917f2020-03-13 18:13:33 +05303043 usecase->stream.out, usecase->type));
yidongh02ef86f2017-04-21 15:36:04 +08003044 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003045 if ((voip_usecase) && (is_codec_backend_out_device_type(&voip_usecase->device_list) &&
3046 (is_codec_backend_out_device_type(&usecase->device_list) ||
3047 is_codec_backend_in_device_type(&usecase->device_list)) &&
yidongh02ef86f2017-04-21 15:36:04 +08003048 out_snd_device_backend_match &&
Mingming Yin2d8aa2e2014-08-14 00:00:51 -07003049 (voip_usecase->stream.out != adev->primary_output))) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08003050 in_snd_device = voip_usecase->in_snd_device;
3051 out_snd_device = voip_usecase->out_snd_device;
3052 }
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -08003053 } else if (audio_extn_hfp_is_active(adev)) {
Vimal Puthanveed41fcff22014-01-23 15:56:53 -08003054 hfp_ucid = audio_extn_hfp_get_usecase();
3055 hfp_usecase = get_usecase_from_list(adev, hfp_ucid);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003056 if ((hfp_usecase) && is_codec_backend_out_device_type(&hfp_usecase->device_list)) {
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -08003057 in_snd_device = hfp_usecase->in_snd_device;
3058 out_snd_device = hfp_usecase->out_snd_device;
3059 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003060 }
3061 if (usecase->type == PCM_PLAYBACK) {
Aditya Bavanaribdda2f22016-10-19 15:02:05 +05303062 if (usecase->stream.out == NULL) {
3063 ALOGE("%s: stream.out is NULL", __func__);
3064 return -EINVAL;
3065 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003066 assign_devices(&usecase->device_list, &usecase->stream.out->device_list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003067 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07003068 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurent637e2d42018-11-15 12:24:31 -08003069 struct stream_out *voip_out = adev->primary_output;
3070 struct stream_in *voip_in = get_voice_communication_input(adev);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003071 if (compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_BUS))
Guodong Hu267bdf82019-08-12 19:22:32 +08003072 out_snd_device = audio_extn_auto_hal_get_output_snd_device(adev, uc_id);
3073 else
3074 out_snd_device = platform_get_output_snd_device(adev->platform,
Jaideep Sharma477917f2020-03-13 18:13:33 +05303075 usecase->stream.out,
3076 usecase->type);
kunleizdcf967a2018-08-07 17:09:11 +08003077 voip_usecase = get_usecase_from_list(adev, USECASE_AUDIO_PLAYBACK_VOIP);
kunleizdcf967a2018-08-07 17:09:11 +08003078
Eric Laurent637e2d42018-11-15 12:24:31 -08003079 if (voip_usecase)
3080 voip_out = voip_usecase->stream.out;
3081
3082 if (usecase->stream.out == voip_out && voip_in != NULL)
3083 select_devices(adev, voip_in->usecase);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07003084 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003085 } else if (usecase->type == PCM_CAPTURE) {
Aditya Bavanaribdda2f22016-10-19 15:02:05 +05303086 if (usecase->stream.in == NULL) {
3087 ALOGE("%s: stream.in is NULL", __func__);
3088 return -EINVAL;
3089 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003090 assign_devices(&usecase->device_list, &usecase->stream.in->device_list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003091 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07003092 if (in_snd_device == SND_DEVICE_NONE) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003093 struct listnode out_devices;
Eric Laurent637e2d42018-11-15 12:24:31 -08003094 struct stream_in *voip_in = get_voice_communication_input(adev);
Carter Hsu2e429db2019-05-14 18:50:52 +08003095 struct stream_in *priority_in = NULL;
Eric Laurent637e2d42018-11-15 12:24:31 -08003096
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003097 list_init(&out_devices);
Eric Laurent637e2d42018-11-15 12:24:31 -08003098 if (voip_in != NULL) {
Eric Laurent637e2d42018-11-15 12:24:31 -08003099 struct audio_usecase *voip_usecase = get_usecase_from_list(adev,
3100 USECASE_AUDIO_PLAYBACK_VOIP);
3101
Carter Hsu2e429db2019-05-14 18:50:52 +08003102 usecase->stream.in->enable_ec_port = false;
3103
Zhou Song503196b2021-07-23 17:31:05 +08003104 if (usecase->id == USECASE_AUDIO_RECORD_AFE_PROXY ||
3105 usecase->id == USECASE_AUDIO_RECORD_AFE_PROXY2) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003106 reassign_device_list(&out_devices, AUDIO_DEVICE_OUT_TELEPHONY_TX, "");
Eric Laurent637e2d42018-11-15 12:24:31 -08003107 } else if (voip_usecase) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003108 assign_devices(&out_devices, &voip_usecase->stream.out->device_list);
Eric Laurent637e2d42018-11-15 12:24:31 -08003109 } else if (adev->primary_output &&
3110 !adev->primary_output->standby) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003111 assign_devices(&out_devices, &adev->primary_output->device_list);
Eric Laurent637e2d42018-11-15 12:24:31 -08003112 } else {
3113 /* forcing speaker o/p device to get matching i/p pair
3114 in case o/p is not routed from same primary HAL */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003115 reassign_device_list(&out_devices, AUDIO_DEVICE_OUT_SPEAKER, "");
Eric Laurent637e2d42018-11-15 12:24:31 -08003116 }
Carter Hsu2e429db2019-05-14 18:50:52 +08003117 priority_in = voip_in;
3118 } else {
3119 /* get the input with the highest priority source*/
3120 priority_in = get_priority_input(adev);
3121
Susan Wang727dd6b2021-03-26 11:28:59 -04003122 if (!priority_in ||
3123 audio_extn_auto_hal_overwrite_priority_for_auto(usecase->stream.in))
Carter Hsu2e429db2019-05-14 18:50:52 +08003124 priority_in = usecase->stream.in;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07003125 }
Huicheng Liu1404ba12020-09-11 01:03:25 -04003126 if (compare_device_type(&usecase->device_list, AUDIO_DEVICE_IN_BUS)){
3127 in_snd_device = audio_extn_auto_hal_get_snd_device_for_car_audio_stream(priority_in->car_audio_stream);
3128 }
3129 else
3130 in_snd_device = platform_get_input_snd_device(adev->platform,
3131 priority_in,
3132 &out_devices,
3133 usecase->type);
Zhenlin Lian4f947842022-05-14 15:50:52 +05303134 clear_devices(&out_devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07003135 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003136 }
3137 }
3138
3139 if (out_snd_device == usecase->out_snd_device &&
3140 in_snd_device == usecase->in_snd_device) {
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05303141
3142 if (!force_device_switch(usecase))
3143 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003144 }
3145
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003146 if (!compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_BUS) &&
Guodong Hu267bdf82019-08-12 19:22:32 +08003147 ((is_btsco_device(out_snd_device,in_snd_device) && !adev->bt_sco_on) ||
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003148 (is_a2dp_device(out_snd_device) && !audio_extn_a2dp_source_is_ready()))) {
Guodong Hu267bdf82019-08-12 19:22:32 +08003149 ALOGD("SCO/A2DP is selected but they are not connected/ready hence dont route");
3150 return 0;
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303151 }
3152
Aalique Grahame22e49102018-12-18 14:23:57 -08003153 if (out_snd_device != SND_DEVICE_NONE &&
3154 out_snd_device != adev->last_logged_snd_device[uc_id][0]) {
3155 ALOGD("%s: changing use case %s output device from(%d: %s, acdb %d) to (%d: %s, acdb %d)",
3156 __func__,
3157 use_case_table[uc_id],
3158 adev->last_logged_snd_device[uc_id][0],
3159 platform_get_snd_device_name(adev->last_logged_snd_device[uc_id][0]),
3160 adev->last_logged_snd_device[uc_id][0] != SND_DEVICE_NONE ?
3161 platform_get_snd_device_acdb_id(adev->last_logged_snd_device[uc_id][0]) :
3162 -1,
3163 out_snd_device,
3164 platform_get_snd_device_name(out_snd_device),
3165 platform_get_snd_device_acdb_id(out_snd_device));
3166 adev->last_logged_snd_device[uc_id][0] = out_snd_device;
3167 }
3168 if (in_snd_device != SND_DEVICE_NONE &&
3169 in_snd_device != adev->last_logged_snd_device[uc_id][1]) {
3170 ALOGD("%s: changing use case %s input device from(%d: %s, acdb %d) to (%d: %s, acdb %d)",
3171 __func__,
3172 use_case_table[uc_id],
3173 adev->last_logged_snd_device[uc_id][1],
3174 platform_get_snd_device_name(adev->last_logged_snd_device[uc_id][1]),
3175 adev->last_logged_snd_device[uc_id][1] != SND_DEVICE_NONE ?
3176 platform_get_snd_device_acdb_id(adev->last_logged_snd_device[uc_id][1]) :
3177 -1,
3178 in_snd_device,
3179 platform_get_snd_device_name(in_snd_device),
3180 platform_get_snd_device_acdb_id(in_snd_device));
3181 adev->last_logged_snd_device[uc_id][1] = in_snd_device;
3182 }
3183
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08003184
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003185 /*
3186 * Limitation: While in call, to do a device switch we need to disable
3187 * and enable both RX and TX devices though one of them is same as current
3188 * device.
3189 */
Vidyakumar Athota21b3bb92014-04-25 11:08:08 -07003190 if ((usecase->type == VOICE_CALL) &&
3191 (usecase->in_snd_device != SND_DEVICE_NONE) &&
3192 (usecase->out_snd_device != SND_DEVICE_NONE)) {
Eric Laurentb23d5282013-05-14 15:27:20 -07003193 status = platform_switch_voice_call_device_pre(adev->platform);
Narsinga Rao Chella116142b2015-08-14 18:00:08 -07003194 }
3195
3196 if (((usecase->type == VOICE_CALL) ||
3197 (usecase->type == VOIP_CALL)) &&
3198 (usecase->out_snd_device != SND_DEVICE_NONE)) {
3199 /* Disable sidetone only if voice/voip call already exists */
Jaideep Sharma477917f2020-03-13 18:13:33 +05303200 if (voice_is_call_state_active_in_call(adev) ||
Narsinga Rao Chella116142b2015-08-14 18:00:08 -07003201 voice_extn_compress_voip_is_started(adev))
Bhalchandra Gajare45fee282015-06-09 22:23:45 -07003202 voice_set_sidetone(adev, usecase->out_snd_device, false);
Vidyakumar Athotaea269c62016-10-31 09:05:59 -07003203
3204 /* Disable aanc only if voice call exists */
Jaideep Sharma477917f2020-03-13 18:13:33 +05303205 if (voice_is_call_state_active_in_call(adev))
Vidyakumar Athotaea269c62016-10-31 09:05:59 -07003206 voice_check_and_update_aanc_path(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08003207 }
3208
Aalique Grahame22e49102018-12-18 14:23:57 -08003209 if ((out_snd_device == SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP ||
3210 out_snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP) &&
Florian Pfister1a84f312018-07-19 14:38:18 +02003211 (!audio_extn_a2dp_source_is_ready())) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303212 ALOGW("%s: A2DP profile is not ready, routing to speaker only", __func__);
Aalique Grahame22e49102018-12-18 14:23:57 -08003213 if (out_snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP)
3214 out_snd_device = SND_DEVICE_OUT_SPEAKER_SAFE;
3215 else
3216 out_snd_device = SND_DEVICE_OUT_SPEAKER;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303217 }
3218
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003219 /* Disable current sound devices */
3220 if (usecase->out_snd_device != SND_DEVICE_NONE) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07003221 disable_audio_route(adev, usecase);
3222 disable_snd_device(adev, usecase->out_snd_device);
Gautam Manam274f4752021-09-24 10:58:49 +05303223 if (usecase->id == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS)
3224 disable_snd_device(adev, SND_DEVICE_OUT_HAPTICS);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003225 }
3226
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003227 if (usecase->in_snd_device != SND_DEVICE_NONE) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07003228 disable_audio_route(adev, usecase);
3229 disable_snd_device(adev, usecase->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003230 }
3231
Daniel Hillenbrand17873952013-05-23 10:10:00 +05303232 /* Rely on amplifier_set_devices to distinguish between in/out devices */
3233 amplifier_set_input_devices(in_snd_device);
3234 amplifier_set_output_devices(out_snd_device);
3235
Vidyakumar Athota545dbd32013-11-13 17:30:53 -08003236 /* Applicable only on the targets that has external modem.
3237 * New device information should be sent to modem before enabling
3238 * the devices to reduce in-call device switch time.
3239 */
Vidyakumar Athota21b3bb92014-04-25 11:08:08 -07003240 if ((usecase->type == VOICE_CALL) &&
3241 (usecase->in_snd_device != SND_DEVICE_NONE) &&
3242 (usecase->out_snd_device != SND_DEVICE_NONE)) {
Vidyakumar Athota545dbd32013-11-13 17:30:53 -08003243 status = platform_switch_voice_call_enable_device_config(adev->platform,
3244 out_snd_device,
3245 in_snd_device);
Vidyakumar Athota21b3bb92014-04-25 11:08:08 -07003246 }
Vidyakumar Athota545dbd32013-11-13 17:30:53 -08003247
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003248 /* Enable new sound devices */
3249 if (out_snd_device != SND_DEVICE_NONE) {
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08003250 check_usecases_codec_backend(adev, usecase, out_snd_device);
Saurav Kumarc1411662020-10-14 10:50:45 +05303251 check_and_configure_headphone(adev, usecase, out_snd_device);
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +05303252 if (platform_check_codec_asrc_support(adev->platform))
3253 check_and_set_asrc_mode(adev, usecase, out_snd_device);
Haynes Mathew George1376ca62014-04-24 11:55:48 -07003254 enable_snd_device(adev, out_snd_device);
Zhou Songd9bd9302020-08-04 16:34:45 +08003255 /* Enable haptics device for haptic usecase */
3256 if (usecase->id == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS)
3257 enable_snd_device(adev, SND_DEVICE_OUT_HAPTICS);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003258 }
3259
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07003260 if (in_snd_device != SND_DEVICE_NONE) {
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05303261 check_usecases_capture_codec_backend(adev, usecase, in_snd_device);
Haynes Mathew George1376ca62014-04-24 11:55:48 -07003262 enable_snd_device(adev, in_snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07003263 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003264
Lakshman Chaluvarajuab7e5572020-10-28 19:29:23 +05303265 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL)
Eric Laurentb23d5282013-05-14 15:27:20 -07003266 status = platform_switch_voice_call_device_post(adev->platform,
3267 out_snd_device,
3268 in_snd_device);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08003269
sangwoo170731f2013-06-08 15:36:36 +09003270 usecase->in_snd_device = in_snd_device;
3271 usecase->out_snd_device = out_snd_device;
3272
Dhananjay Kumard6d32152016-10-13 16:11:03 +05303273 audio_extn_utils_update_stream_app_type_cfg_for_usecase(adev,
3274 usecase);
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +05303275 if (usecase->type == PCM_PLAYBACK) {
Weiyin Jiang6f4c8062016-11-23 15:30:29 +08003276 if ((24 == usecase->stream.out->bit_width) &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003277 compare_device_type(&usecase->stream.out->device_list, AUDIO_DEVICE_OUT_SPEAKER)) {
Weiyin Jiang6f4c8062016-11-23 15:30:29 +08003278 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
3279 } else if ((out_snd_device == SND_DEVICE_OUT_HDMI ||
3280 out_snd_device == SND_DEVICE_OUT_USB_HEADSET ||
3281 out_snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
3282 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
3283 /*
3284 * To best utlize DSP, check if the stream sample rate is supported/multiple of
3285 * configured device sample rate, if not update the COPP rate to be equal to the
3286 * device sample rate, else open COPP at stream sample rate
3287 */
3288 platform_check_and_update_copp_sample_rate(adev->platform, out_snd_device,
3289 usecase->stream.out->sample_rate,
3290 &usecase->stream.out->app_type_cfg.sample_rate);
Ashish Jain4826f6c2017-02-06 13:33:20 +05303291 } else if (((out_snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
Preetam Singh Ranawat590d0432019-09-30 14:39:47 +05303292 out_snd_device != SND_DEVICE_OUT_HEADPHONES &&
3293 out_snd_device != SND_DEVICE_OUT_HEADPHONES_HIFI_FILTER &&
Ashish Jain4826f6c2017-02-06 13:33:20 +05303294 !audio_is_true_native_stream_active(adev)) &&
Weiyin Jiang6f4c8062016-11-23 15:30:29 +08003295 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
3296 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +05303297#ifdef PLATFORM_AUTO
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +05303298 if (!(compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_BUS) && ((usecase->stream.out->flags &
3299 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_SYS_NOTIFICATION) || (usecase->stream.out->flags &
3300 (audio_output_flags_t)AUDIO_OUTPUT_FLAG_PHONE)))) {
3301 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
3302 }
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +05303303#else
3304 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
3305#endif
Weiyin Jiang6f4c8062016-11-23 15:30:29 +08003306 }
Weiyin Jiangcdece202019-07-08 16:13:16 +08003307 }
3308 enable_audio_route(adev, usecase);
Weiyin Jiang6f4c8062016-11-23 15:30:29 +08003309
Shalini Manjunatha5e610912020-12-29 13:02:25 +05303310 if (uc_id == USECASE_AUDIO_PLAYBACK_VOIP) {
3311 struct stream_in *voip_in = get_voice_communication_input(adev);
3312 struct audio_usecase *voip_in_usecase = NULL;
3313 voip_in_usecase = get_usecase_from_list(adev, USECASE_AUDIO_RECORD_VOIP);
3314 if (voip_in != NULL &&
3315 voip_in_usecase != NULL &&
3316 !(out_snd_device == AUDIO_DEVICE_OUT_SPEAKER ||
3317 out_snd_device == AUDIO_DEVICE_OUT_SPEAKER_SAFE) &&
3318 (voip_in_usecase->in_snd_device ==
3319 platform_get_input_snd_device(adev->platform, voip_in,
3320 &usecase->stream.out->device_list,usecase->type))) {
3321 /*
3322 * if VOIP TX is enabled before VOIP RX, needs to re-route the TX path
3323 * for enabling echo-reference-voip with correct port
3324 */
3325 ALOGD("%s: VOIP TX is enabled before VOIP RX,needs to re-route the TX path",__func__);
3326 disable_audio_route(adev, voip_in_usecase);
3327 disable_snd_device(adev, voip_in_usecase->in_snd_device);
3328 enable_snd_device(adev, voip_in_usecase->in_snd_device);
3329 enable_audio_route(adev, voip_in_usecase);
3330 }
3331 }
Ramjee Singhcdf67af2021-09-29 14:20:27 +05303332 if (voice_extn_compress_voip_is_active(adev)) {
3333 struct audio_usecase *voip_usecase = get_usecase_from_list(adev,
3334 USECASE_COMPRESS_VOIP_CALL);
3335 /*
3336 * If only compress voip input is opened voip out will be primary out.
3337 * Need to consider re-routing to select correct i/p pair
3338 */
3339 if ((voip_usecase != NULL) &&
3340 (usecase->type == PCM_PLAYBACK) &&
3341 (usecase->stream.out == voip_usecase->stream.out)) {
3342 in_snd_device = platform_get_input_snd_device(adev->platform,
3343 NULL,
3344 &usecase->stream.out->device_list,
3345 usecase->type);
3346 if (voip_usecase->in_snd_device != in_snd_device ) {
3347 ALOGD("%s:Re routing compress voip tx snd device matching voip rx pair",
3348 __func__);
3349 disable_audio_route(adev, voip_usecase);
3350 disable_snd_device(adev, voip_usecase->in_snd_device);
3351 voip_usecase->in_snd_device = in_snd_device;
3352 voip_usecase->out_snd_device = usecase->out_snd_device;
3353 /* Route all TX usecase to Compress voip BE */
3354 check_usecases_capture_codec_backend(adev, voip_usecase, in_snd_device);
3355 enable_snd_device(adev, in_snd_device);
3356 /* Send Voice related calibration for RX /TX pair */
3357 status = platform_switch_voice_call_device_post(adev->platform,
3358 out_snd_device,
3359 in_snd_device);
3360 enable_audio_route(adev, voip_usecase);
3361 }
3362 }
3363 }
Shalini Manjunatha5e610912020-12-29 13:02:25 +05303364
3365
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003366 audio_extn_qdsp_set_device(usecase);
Aalique Grahame22e49102018-12-18 14:23:57 -08003367
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07003368 /* If input stream is already running then effect needs to be
3369 applied on the new input device that's being enabled here. */
Eric Laurent637e2d42018-11-15 12:24:31 -08003370 if (in_snd_device != SND_DEVICE_NONE)
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07003371 check_and_enable_effect(adev);
3372
Vidyakumar Athota493f2892016-08-14 11:56:55 -07003373 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) {
Vidyakumar Athotaea269c62016-10-31 09:05:59 -07003374 /* Enable aanc only if voice call exists */
Jaideep Sharma477917f2020-03-13 18:13:33 +05303375 if (voice_is_call_state_active_in_call(adev))
Vidyakumar Athotaea269c62016-10-31 09:05:59 -07003376 voice_check_and_update_aanc_path(adev, out_snd_device, true);
3377
Vidyakumar Athota493f2892016-08-14 11:56:55 -07003378 /* Enable sidetone only if other voice/voip call already exists */
Jaideep Sharma477917f2020-03-13 18:13:33 +05303379 if (voice_is_call_state_active_in_call(adev) ||
Vidyakumar Athota493f2892016-08-14 11:56:55 -07003380 voice_extn_compress_voip_is_started(adev))
3381 voice_set_sidetone(adev, out_snd_device, true);
3382 }
3383
Vidyakumar Athota1fd21792013-11-15 14:50:57 -08003384 /* Applicable only on the targets that has external modem.
3385 * Enable device command should be sent to modem only after
3386 * enabling voice call mixer controls
3387 */
Vidyakumar Athota339342f2014-07-01 15:30:57 -07003388 if (usecase->type == VOICE_CALL)
Vidyakumar Athota1fd21792013-11-15 14:50:57 -08003389 status = platform_switch_voice_call_usecase_route_post(adev->platform,
3390 out_snd_device,
3391 in_snd_device);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303392
3393 if (is_btsco_device(out_snd_device, in_snd_device) || is_a2dp_device(out_snd_device)) {
Eric Laurent637e2d42018-11-15 12:24:31 -08003394 struct stream_in *in = adev_get_active_input(adev);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303395 if (usecase->type == VOIP_CALL) {
Eric Laurent637e2d42018-11-15 12:24:31 -08003396 if (in != NULL && !in->standby) {
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303397 if (is_bt_soc_on(adev) == false){
3398 ALOGD("BT SCO MIC disconnected while in connection");
Eric Laurent637e2d42018-11-15 12:24:31 -08003399 if (in->pcm != NULL)
3400 pcm_stop(in->pcm);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303401 }
3402 }
3403 if ((usecase->stream.out != NULL) && (usecase->stream.out != adev->primary_output)
3404 && usecase->stream.out->started) {
3405 if (is_bt_soc_on(adev) == false) {
3406 ALOGD("BT SCO/A2DP disconnected while in connection");
3407 out_standby_l(&usecase->stream.out->stream.common);
3408 }
3409 }
3410 } else if ((usecase->stream.out != NULL) &&
3411 !(usecase->stream.out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
Surendar Karkae1dc8742018-11-19 16:23:14 +05303412 (usecase->type != TRANSCODE_LOOPBACK_TX) &&
3413 (usecase->type != TRANSCODE_LOOPBACK_RX) &&
Weiyin Jiang0d373242019-07-25 13:18:17 +08003414 (usecase->type != PCM_CAPTURE) &&
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303415 usecase->stream.out->started) {
3416 if (is_bt_soc_on(adev) == false) {
3417 ALOGD("BT SCO/A2dp disconnected while in connection");
3418 out_standby_l(&usecase->stream.out->stream.common);
3419 }
3420 }
3421 }
3422
Yung Ti Su70cb8242018-06-22 17:38:47 +08003423 if (usecase->type != PCM_CAPTURE && usecase == voip_usecase) {
Aalique Grahame22e49102018-12-18 14:23:57 -08003424 struct stream_out *voip_out = voip_usecase->stream.out;
3425 audio_extn_utils_send_app_type_gain(adev,
3426 voip_out->app_type_cfg.app_type,
3427 &voip_out->app_type_cfg.gain[0]);
3428 }
3429
Ajender Reddyb940b832021-07-07 11:51:42 +05303430 ALOGD("%s: done",__func__);
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05303431
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003432 return status;
3433}
3434
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003435static int stop_input_stream(struct stream_in *in)
3436{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05303437 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003438 struct audio_usecase *uc_info;
Pallavid7c7a272018-01-16 11:22:55 +05303439
3440 if (in == NULL) {
3441 ALOGE("%s: stream_in ptr is NULL", __func__);
3442 return -EINVAL;
3443 }
3444
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003445 struct audio_device *adev = in->dev;
Carter Hsu2e429db2019-05-14 18:50:52 +08003446 struct stream_in *priority_in = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003447
Eric Laurent994a6932013-07-17 11:51:42 -07003448 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003449 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003450 uc_info = get_usecase_from_list(adev, in->usecase);
3451 if (uc_info == NULL) {
3452 ALOGE("%s: Could not find the usecase (%d) in the list",
3453 __func__, in->usecase);
3454 return -EINVAL;
3455 }
3456
Carter Hsu2e429db2019-05-14 18:50:52 +08003457 priority_in = get_priority_input(adev);
3458
Derek Chenea197282019-01-07 17:35:01 -08003459 if (audio_extn_ext_hw_plugin_usecase_stop(adev->ext_hw_plugin, uc_info))
3460 ALOGE("%s: failed to stop ext hw plugin", __func__);
Derek Chend2530072014-11-24 12:39:14 -08003461
Vidyakumar Athota2850d532013-11-19 16:02:12 -08003462 /* Close in-call recording streams */
3463 voice_check_and_stop_incall_rec_usecase(adev, in);
3464
Eric Laurent150dbfe2013-02-27 14:31:02 -08003465 /* 1. Disable stream specific mixer controls */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07003466 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003467
3468 /* 2. Disable the tx device */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07003469 disable_snd_device(adev, uc_info->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003470
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003471 if (is_loopback_input_device(get_device_types(&in->device_list)))
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05303472 audio_extn_keep_alive_stop(KEEP_ALIVE_OUT_PRIMARY);
3473
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08003474 list_remove(&uc_info->list);
Zhenlin Lian4f947842022-05-14 15:50:52 +05303475 clear_devices(&uc_info->device_list);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08003476 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003477
Carter Hsu2e429db2019-05-14 18:50:52 +08003478 if (priority_in == in) {
3479 priority_in = get_priority_input(adev);
Sujin Panicker110f7942021-08-26 17:01:22 +05303480 if (priority_in) {
3481 if (is_usb_in_device_type(&priority_in->device_list)) {
3482 if (audio_extn_usb_connected(NULL))
3483 select_devices(adev, priority_in->usecase);
3484 } else {
3485 select_devices(adev, priority_in->usecase);
3486 }
3487 }
Carter Hsu2e429db2019-05-14 18:50:52 +08003488 }
3489
Vatsal Buchac09ae062018-11-14 13:25:08 +05303490 enable_gcov();
Eric Laurent994a6932013-07-17 11:51:42 -07003491 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003492 return ret;
3493}
3494
3495int start_input_stream(struct stream_in *in)
3496{
3497 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -08003498 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003499 struct audio_usecase *uc_info;
Preetam Singh Ranawata87e9742018-02-13 16:52:53 +05303500
3501 if (in == NULL) {
3502 ALOGE("%s: stream_in ptr is NULL", __func__);
3503 return -EINVAL;
3504 }
3505
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003506 struct audio_device *adev = in->dev;
Garmond Leunge2433c32017-09-28 21:51:22 -07003507 struct pcm_config config = in->config;
Garmond Leung438932f2017-10-04 19:35:18 -07003508 int usecase = platform_update_usecase_from_source(in->source,in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003509
Mingming Yin2664a5b2015-09-03 10:53:11 -07003510 if (get_usecase_from_list(adev, usecase) == NULL)
3511 in->usecase = usecase;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05303512 ALOGD("%s: enter: stream(%p)usecase(%d: %s)",
3513 __func__, &in->stream, in->usecase, use_case_table[in->usecase]);
Shiv Maliyappanahallida107642013-10-17 11:16:13 -07003514
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05303515 if (CARD_STATUS_OFFLINE == in->card_status||
Shubhasini Sugumaran3fde6582022-01-19 15:54:50 -05003516 CARD_STATUS_OFFLINE == adev->card_status ||
3517 POWER_POLICY_STATUS_OFFLINE == adev->in_power_policy) {
3518 ALOGW("in->card_status or adev->card_status or adev->input_power offline, try again");
Dhanalakshmi Siddani4d57e992014-07-17 16:37:51 +05303519 ret = -EIO;
Naresh Tanniru4c630392014-05-12 01:05:52 +05303520 goto error_config;
3521 }
Naresh Tanniru4c630392014-05-12 01:05:52 +05303522
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003523 if (is_sco_in_device_type(&in->device_list)) {
Lakshman Chaluvaraju87f53aa2021-02-02 15:50:11 +05303524 if (!adev->bt_sco_on || audio_extn_a2dp_source_is_ready()) {
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303525 ALOGE("%s: SCO profile is not ready, return error", __func__);
3526 ret = -EIO;
3527 goto error_config;
3528 }
3529 }
3530
Shiv Maliyappanahallida107642013-10-17 11:16:13 -07003531 /* Check if source matches incall recording usecase criteria */
3532 ret = voice_check_and_set_incall_rec_usecase(adev, in);
3533 if (ret)
3534 goto error_config;
3535 else
Mingming Yin2664a5b2015-09-03 10:53:11 -07003536 ALOGV("%s: usecase(%d)", __func__, in->usecase);
3537
Dhananjay Kumar7dbe3562019-08-30 05:49:33 +05303538 if (audio_extn_cin_attached_usecase(in))
3539 audio_extn_cin_acquire_usecase(in);
3540
Mingming Yin2664a5b2015-09-03 10:53:11 -07003541 if (get_usecase_from_list(adev, in->usecase) != NULL) {
3542 ALOGE("%s: use case assigned already in use, stream(%p)usecase(%d: %s)",
3543 __func__, &in->stream, in->usecase, use_case_table[in->usecase]);
Weiyin Jiang38c0e612020-09-10 16:10:51 +08003544 ret = -EINVAL;
3545 goto error_config;
Mingming Yin2664a5b2015-09-03 10:53:11 -07003546 }
Shiv Maliyappanahallida107642013-10-17 11:16:13 -07003547
Eric Laurentb23d5282013-05-14 15:27:20 -07003548 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003549 if (in->pcm_device_id < 0) {
3550 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
3551 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -08003552 ret = -EINVAL;
3553 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003554 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003555
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003556 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07003557
3558 if (!uc_info) {
3559 ret = -ENOMEM;
3560 goto error_config;
3561 }
3562
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003563 uc_info->id = in->usecase;
3564 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08003565 uc_info->stream.in = in;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003566 list_init(&uc_info->device_list);
3567 assign_devices(&uc_info->device_list, &in->device_list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003568 uc_info->in_snd_device = SND_DEVICE_NONE;
3569 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003570
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08003571 list_add_tail(&adev->usecase_list, &uc_info->list);
Wei Wangf7ca6c92017-11-21 14:51:20 -08003572 audio_streaming_hint_start();
Sudheer Papothifa9d2282015-09-17 01:53:25 +05303573 audio_extn_perf_lock_acquire(&adev->perf_lock_handle, 0,
3574 adev->perf_lock_opts,
3575 adev->perf_lock_opts_size);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003576 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003577
Derek Chenea197282019-01-07 17:35:01 -08003578 if (audio_extn_ext_hw_plugin_usecase_start(adev->ext_hw_plugin, uc_info))
3579 ALOGE("%s: failed to start ext hw plugin", __func__);
Derek Chend2530072014-11-24 12:39:14 -08003580
Deeraj Soman30cc7ae2019-03-18 16:26:55 +05303581 android_atomic_acquire_cas(true, false, &(in->capture_stopped));
3582
Dhananjay Kumar7dbe3562019-08-30 05:49:33 +05303583 if (audio_extn_cin_attached_usecase(in)) {
Manish Dewangan46e07982018-12-13 18:18:59 +05303584 ret = audio_extn_cin_open_input_stream(in);
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05303585 if (ret)
3586 goto error_open;
3587 else
3588 goto done_open;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003589 }
3590
Haynes Mathew George16081042017-05-31 17:16:49 -07003591 if (in->usecase == USECASE_AUDIO_RECORD_MMAP) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003592 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) {
Haynes Mathew George16081042017-05-31 17:16:49 -07003593 ALOGE("%s: pcm stream not ready", __func__);
3594 goto error_open;
3595 }
3596 ret = pcm_start(in->pcm);
3597 if (ret < 0) {
3598 ALOGE("%s: MMAP pcm_start failed ret %d", __func__, ret);
3599 goto error_open;
3600 }
3601 } else {
3602 unsigned int flags = PCM_IN | PCM_MONOTONIC;
3603 unsigned int pcm_open_retry_count = 0;
3604
Zhou Song62ea0282020-03-22 19:53:01 +08003605 if ((in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY) ||
3606 (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY2)) {
Haynes Mathew George16081042017-05-31 17:16:49 -07003607 flags |= PCM_MMAP | PCM_NOIRQ;
3608 pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT;
3609 } else if (in->realtime) {
3610 flags |= PCM_MMAP | PCM_NOIRQ;
3611 }
3612
Garmond Leunge2433c32017-09-28 21:51:22 -07003613 if (audio_extn_ffv_get_stream() == in) {
3614 ALOGD("%s: ffv stream, update pcm config", __func__);
3615 audio_extn_ffv_update_pcm_config(&config);
3616 }
Haynes Mathew George16081042017-05-31 17:16:49 -07003617 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
3618 __func__, adev->snd_card, in->pcm_device_id, in->config.channels);
3619
3620 while (1) {
Haynes Mathew George380745d2017-10-04 15:27:45 -07003621 ATRACE_BEGIN("pcm_in_open");
Haynes Mathew George16081042017-05-31 17:16:49 -07003622 in->pcm = pcm_open(adev->snd_card, in->pcm_device_id,
Garmond Leung438932f2017-10-04 19:35:18 -07003623 flags, &config);
Haynes Mathew George380745d2017-10-04 15:27:45 -07003624 ATRACE_END();
Satish Babu Patakokila54ce83d2018-07-06 18:00:37 +05303625 if (errno == ENETRESET && !pcm_is_ready(in->pcm)) {
Sharad Sanglec6f32552018-05-04 16:15:38 +05303626 ALOGE("%s: pcm_open failed errno:%d\n", __func__, errno);
3627 adev->card_status = CARD_STATUS_OFFLINE;
3628 in->card_status = CARD_STATUS_OFFLINE;
3629 ret = -EIO;
3630 goto error_open;
3631 }
3632
Haynes Mathew George16081042017-05-31 17:16:49 -07003633 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) {
3634 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
3635 if (in->pcm != NULL) {
3636 pcm_close(in->pcm);
3637 in->pcm = NULL;
3638 }
Weiyin Jiang72197252019-10-09 11:49:32 +08003639 if (pcm_open_retry_count == 0) {
Haynes Mathew George16081042017-05-31 17:16:49 -07003640 ret = -EIO;
3641 goto error_open;
3642 }
Weiyin Jiang72197252019-10-09 11:49:32 +08003643 pcm_open_retry_count--;
Haynes Mathew George16081042017-05-31 17:16:49 -07003644 usleep(PROXY_OPEN_WAIT_TIME * 1000);
3645 continue;
3646 }
3647 break;
3648 }
3649
3650 ALOGV("%s: pcm_prepare", __func__);
Haynes Mathew George380745d2017-10-04 15:27:45 -07003651 ATRACE_BEGIN("pcm_in_prepare");
Haynes Mathew George16081042017-05-31 17:16:49 -07003652 ret = pcm_prepare(in->pcm);
Haynes Mathew George380745d2017-10-04 15:27:45 -07003653 ATRACE_END();
Haynes Mathew George16081042017-05-31 17:16:49 -07003654 if (ret < 0) {
3655 ALOGE("%s: pcm_prepare returned %d", __func__, ret);
3656 pcm_close(in->pcm);
3657 in->pcm = NULL;
3658 goto error_open;
3659 }
Vaibhav Raut5f3bf222023-02-06 17:45:22 +05303660 if (in->flags & (AUDIO_INPUT_FLAG_FAST | AUDIO_INPUT_FLAG_RAW))
Narsinga Rao Chellaa6e1f702021-10-22 13:57:32 +05303661 register_in_stream(in);
Haynes Mathew George16081042017-05-31 17:16:49 -07003662 if (in->realtime) {
Haynes Mathew George380745d2017-10-04 15:27:45 -07003663 ATRACE_BEGIN("pcm_in_start");
Haynes Mathew George16081042017-05-31 17:16:49 -07003664 ret = pcm_start(in->pcm);
Haynes Mathew George380745d2017-10-04 15:27:45 -07003665 ATRACE_END();
Haynes Mathew George16081042017-05-31 17:16:49 -07003666 if (ret < 0) {
3667 ALOGE("%s: RT pcm_start failed ret %d", __func__, ret);
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003668 pcm_close(in->pcm);
3669 in->pcm = NULL;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003670 goto error_open;
3671 }
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003672 }
Haynes Mathew George5beddd42016-06-27 18:33:40 -07003673 }
3674
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07003675 check_and_enable_effect(adev);
justinweng20fb6d82019-02-21 18:49:00 -07003676 audio_extn_audiozoom_set_microphone_direction(in, in->zoom);
3677 audio_extn_audiozoom_set_microphone_field_dimension(in, in->direction);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07003678
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003679 if (is_loopback_input_device(get_device_types(&in->device_list)))
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05303680 audio_extn_keep_alive_start(KEEP_ALIVE_OUT_PRIMARY);
3681
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05303682done_open:
Wei Wangf7ca6c92017-11-21 14:51:20 -08003683 audio_streaming_hint_end();
Sudheer Papothifa9d2282015-09-17 01:53:25 +05303684 audio_extn_perf_lock_release(&adev->perf_lock_handle);
Manisha Agarwal4c2402e2020-10-21 12:02:57 +05303685 ALOGD("%s: exit", __func__);
Vatsal Buchac09ae062018-11-14 13:25:08 +05303686 enable_gcov();
Eric Laurentc8400632013-02-14 19:04:54 -08003687 return ret;
3688
3689error_open:
Wei Wangf7ca6c92017-11-21 14:51:20 -08003690 audio_streaming_hint_end();
Sudheer Papothifa9d2282015-09-17 01:53:25 +05303691 audio_extn_perf_lock_release(&adev->perf_lock_handle);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003692 stop_input_stream(in);
Wei Wangf7ca6c92017-11-21 14:51:20 -08003693
Eric Laurentc8400632013-02-14 19:04:54 -08003694error_config:
Weiyin Jiang38c0e612020-09-10 16:10:51 +08003695 if (audio_extn_cin_attached_usecase(in))
3696 audio_extn_cin_close_input_stream(in);
Laxminath Kasam2cb4b752015-09-24 03:59:15 +05303697 /*
3698 * sleep 50ms to allow sufficient time for kernel
3699 * drivers to recover incases like SSR.
3700 */
3701 usleep(50000);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003702 ALOGD("%s: exit: status(%d)", __func__, ret);
Vatsal Buchac09ae062018-11-14 13:25:08 +05303703 enable_gcov();
Eric Laurentc8400632013-02-14 19:04:54 -08003704 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003705}
3706
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07003707void lock_input_stream(struct stream_in *in)
3708{
3709 pthread_mutex_lock(&in->pre_lock);
3710 pthread_mutex_lock(&in->lock);
3711 pthread_mutex_unlock(&in->pre_lock);
3712}
3713
3714void lock_output_stream(struct stream_out *out)
3715{
3716 pthread_mutex_lock(&out->pre_lock);
3717 pthread_mutex_lock(&out->lock);
3718 pthread_mutex_unlock(&out->pre_lock);
3719}
3720
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003721/* must be called with out->lock locked */
3722static int send_offload_cmd_l(struct stream_out* out, int command)
3723{
3724 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
3725
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07003726 if (!cmd) {
3727 ALOGE("failed to allocate mem for command 0x%x", command);
3728 return -ENOMEM;
3729 }
3730
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003731 ALOGVV("%s %d", __func__, command);
3732
3733 cmd->cmd = command;
3734 list_add_tail(&out->offload_cmd_list, &cmd->node);
3735 pthread_cond_signal(&out->offload_cond);
3736 return 0;
3737}
3738
Gautam Manam14c198b2020-12-24 14:08:04 +05303739/* must be called with out->lock */
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003740static void stop_compressed_output_l(struct stream_out *out)
3741{
Gautam Manam14c198b2020-12-24 14:08:04 +05303742 pthread_mutex_lock(&out->latch_lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003743 out->offload_state = OFFLOAD_STATE_IDLE;
Gautam Manam14c198b2020-12-24 14:08:04 +05303744 pthread_mutex_unlock(&out->latch_lock);
Weiyin Jiang906db3c2021-03-02 13:17:04 +08003745
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003746 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07003747 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003748 if (out->compr != NULL) {
3749 compress_stop(out->compr);
3750 while (out->offload_thread_blocked) {
3751 pthread_cond_wait(&out->cond, &out->lock);
3752 }
3753 }
3754}
3755
Varun Balaraje49253e2017-07-06 19:48:56 +05303756bool is_interactive_usecase(audio_usecase_t uc_id)
3757{
3758 unsigned int i;
3759 for (i = 0; i < sizeof(interactive_usecases)/sizeof(interactive_usecases[0]); i++) {
3760 if (uc_id == interactive_usecases[i])
3761 return true;
3762 }
3763 return false;
3764}
3765
3766static audio_usecase_t get_interactive_usecase(struct audio_device *adev)
3767{
3768 audio_usecase_t ret_uc = USECASE_INVALID;
3769 unsigned int intract_uc_index;
3770 unsigned int num_usecase = sizeof(interactive_usecases)/sizeof(interactive_usecases[0]);
3771
3772 ALOGV("%s: num_usecase: %d", __func__, num_usecase);
3773 for (intract_uc_index = 0; intract_uc_index < num_usecase; intract_uc_index++) {
3774 if (!(adev->interactive_usecase_state & (0x1 << intract_uc_index))) {
3775 adev->interactive_usecase_state |= 0x1 << intract_uc_index;
3776 ret_uc = interactive_usecases[intract_uc_index];
3777 break;
3778 }
3779 }
3780
3781 ALOGV("%s: Interactive usecase is %d", __func__, ret_uc);
3782 return ret_uc;
3783}
3784
3785static void free_interactive_usecase(struct audio_device *adev,
3786 audio_usecase_t uc_id)
3787{
3788 unsigned int interact_uc_index;
3789 unsigned int num_usecase = sizeof(interactive_usecases)/sizeof(interactive_usecases[0]);
3790
3791 for (interact_uc_index = 0; interact_uc_index < num_usecase; interact_uc_index++) {
3792 if (interactive_usecases[interact_uc_index] == uc_id) {
3793 adev->interactive_usecase_state &= ~(0x1 << interact_uc_index);
3794 break;
3795 }
3796 }
3797 ALOGV("%s: free Interactive usecase %d", __func__, uc_id);
3798}
3799
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003800bool is_offload_usecase(audio_usecase_t uc_id)
3801{
3802 unsigned int i;
3803 for (i = 0; i < sizeof(offload_usecases)/sizeof(offload_usecases[0]); i++) {
3804 if (uc_id == offload_usecases[i])
3805 return true;
3806 }
3807 return false;
3808}
3809
Dhananjay Kumarac341582017-02-23 23:42:25 +05303810static audio_usecase_t get_offload_usecase(struct audio_device *adev, bool is_compress)
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003811{
vivek mehta446c3962015-09-14 10:57:35 -07003812 audio_usecase_t ret_uc = USECASE_INVALID;
3813 unsigned int offload_uc_index;
Alexy Josephb1379942016-01-29 15:49:38 -08003814 unsigned int num_usecase = sizeof(offload_usecases)/sizeof(offload_usecases[0]);
vivek mehta446c3962015-09-14 10:57:35 -07003815 if (!adev->multi_offload_enable) {
Dhananjay Kumarac341582017-02-23 23:42:25 +05303816 if (!is_compress)
vivek mehta446c3962015-09-14 10:57:35 -07003817 ret_uc = USECASE_AUDIO_PLAYBACK_OFFLOAD2;
3818 else
3819 ret_uc = USECASE_AUDIO_PLAYBACK_OFFLOAD;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003820
vivek mehta446c3962015-09-14 10:57:35 -07003821 pthread_mutex_lock(&adev->lock);
3822 if (get_usecase_from_list(adev, ret_uc) != NULL)
3823 ret_uc = USECASE_INVALID;
3824 pthread_mutex_unlock(&adev->lock);
3825
3826 return ret_uc;
3827 }
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003828
3829 ALOGV("%s: num_usecase: %d", __func__, num_usecase);
vivek mehta446c3962015-09-14 10:57:35 -07003830 for (offload_uc_index = 0; offload_uc_index < num_usecase; offload_uc_index++) {
3831 if (!(adev->offload_usecases_state & (0x1 << offload_uc_index))) {
3832 adev->offload_usecases_state |= 0x1 << offload_uc_index;
3833 ret_uc = offload_usecases[offload_uc_index];
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003834 break;
3835 }
3836 }
vivek mehta446c3962015-09-14 10:57:35 -07003837
3838 ALOGV("%s: offload usecase is %d", __func__, ret_uc);
3839 return ret_uc;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003840}
3841
3842static void free_offload_usecase(struct audio_device *adev,
3843 audio_usecase_t uc_id)
3844{
vivek mehta446c3962015-09-14 10:57:35 -07003845 unsigned int offload_uc_index;
Alexy Josephb1379942016-01-29 15:49:38 -08003846 unsigned int num_usecase = sizeof(offload_usecases)/sizeof(offload_usecases[0]);
vivek mehta446c3962015-09-14 10:57:35 -07003847
3848 if (!adev->multi_offload_enable)
3849 return;
3850
3851 for (offload_uc_index = 0; offload_uc_index < num_usecase; offload_uc_index++) {
3852 if (offload_usecases[offload_uc_index] == uc_id) {
3853 adev->offload_usecases_state &= ~(0x1 << offload_uc_index);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003854 break;
3855 }
3856 }
3857 ALOGV("%s: free offload usecase %d", __func__, uc_id);
3858}
3859
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003860static void *offload_thread_loop(void *context)
3861{
3862 struct stream_out *out = (struct stream_out *) context;
3863 struct listnode *item;
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08003864 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003865
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003866 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
Weiyin Jiangd9b5a4e2019-07-18 17:24:21 +08003867 set_sched_policy(0, SP_FOREGROUND);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003868 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
3869
3870 ALOGV("%s", __func__);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07003871 lock_output_stream(out);
juyuchen391b5fa2018-12-12 17:58:09 +08003872 out->offload_state = OFFLOAD_STATE_IDLE;
3873 out->playback_started = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003874 for (;;) {
3875 struct offload_cmd *cmd = NULL;
3876 stream_callback_event_t event;
3877 bool send_callback = false;
3878
3879 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
3880 __func__, list_empty(&out->offload_cmd_list),
3881 out->offload_state);
3882 if (list_empty(&out->offload_cmd_list)) {
3883 ALOGV("%s SLEEPING", __func__);
3884 pthread_cond_wait(&out->offload_cond, &out->lock);
3885 ALOGV("%s RUNNING", __func__);
3886 continue;
3887 }
3888
3889 item = list_head(&out->offload_cmd_list);
3890 cmd = node_to_item(item, struct offload_cmd, node);
3891 list_remove(item);
3892
3893 ALOGVV("%s STATE %d CMD %d out->compr %p",
3894 __func__, out->offload_state, cmd->cmd, out->compr);
3895
3896 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
3897 free(cmd);
3898 break;
3899 }
3900
Haynes Mathew Georgeee5836f2017-11-21 18:02:10 -08003901 // allow OFFLOAD_CMD_ERROR reporting during standby
3902 // this is needed to handle failures during compress_open
3903 // Note however that on a pause timeout, the stream is closed
3904 // and no offload usecase will be active. Therefore this
3905 // special case is needed for compress_open failures alone
3906 if (cmd->cmd != OFFLOAD_CMD_ERROR &&
3907 out->compr == NULL) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003908 ALOGE("%s: Compress handle is NULL", __func__);
Haynes Mathew Georgea9abb202016-06-02 14:13:20 -07003909 free(cmd);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003910 pthread_cond_signal(&out->cond);
3911 continue;
3912 }
3913 out->offload_thread_blocked = true;
3914 pthread_mutex_unlock(&out->lock);
3915 send_callback = false;
3916 switch(cmd->cmd) {
3917 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07003918 ALOGD("copl(%p):calling compress_wait", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003919 compress_wait(out->compr, -1);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07003920 ALOGD("copl(%p):out of compress_wait", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003921 send_callback = true;
3922 event = STREAM_CBK_EVENT_WRITE_READY;
3923 break;
3924 case OFFLOAD_CMD_PARTIAL_DRAIN:
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08003925 ret = compress_next_track(out->compr);
Sidipotu Ashok55820562014-02-10 16:16:38 +05303926 if(ret == 0) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07003927 ALOGD("copl(%p):calling compress_partial_drain", out);
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05303928 ret = compress_partial_drain(out->compr);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07003929 ALOGD("copl(%p):out of compress_partial_drain", out);
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05303930 if (ret < 0)
3931 ret = -errno;
Sidipotu Ashok55820562014-02-10 16:16:38 +05303932 }
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05303933 else if (ret == -ETIMEDOUT)
Aniket Kumar Lata3570fb12017-11-08 15:53:44 -08003934 ret = compress_drain(out->compr);
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08003935 else
3936 ALOGE("%s: Next track returned error %d",__func__, ret);
Aniket Kumar Lata3570fb12017-11-08 15:53:44 -08003937 if (-ENETRESET != ret && !(-EINTR == ret &&
Shubhasini Sugumaran3fde6582022-01-19 15:54:50 -05003938 (CARD_STATUS_OFFLINE == out->card_status ||
3939 POWER_POLICY_STATUS_OFFLINE == adev->out_power_policy))) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05303940 send_callback = true;
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05303941 pthread_mutex_lock(&out->lock);
3942 out->send_new_metadata = 1;
3943 out->send_next_track_params = true;
3944 pthread_mutex_unlock(&out->lock);
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05303945 event = STREAM_CBK_EVENT_DRAIN_READY;
3946 ALOGV("copl(%p):send drain callback, ret %d", out, ret);
3947 } else
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05303948 ALOGI("%s: Block drain ready event during SSR", __func__);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003949 break;
3950 case OFFLOAD_CMD_DRAIN:
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07003951 ALOGD("copl(%p):calling compress_drain", out);
Aniket Kumar Lata3570fb12017-11-08 15:53:44 -08003952 ret = compress_drain(out->compr);
3953 ALOGD("copl(%p):out of compress_drain", out);
3954 // EINTR check avoids drain interruption due to SSR
3955 if (-ENETRESET != ret && !(-EINTR == ret &&
Shubhasini Sugumaran3fde6582022-01-19 15:54:50 -05003956 (CARD_STATUS_OFFLINE == out->card_status ||
3957 POWER_POLICY_STATUS_OFFLINE == adev->out_power_policy))) {
Aniket Kumar Lata3570fb12017-11-08 15:53:44 -08003958 send_callback = true;
3959 event = STREAM_CBK_EVENT_DRAIN_READY;
3960 } else
3961 ALOGI("%s: Block drain ready event during SSR", __func__);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003962 break;
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05303963 case OFFLOAD_CMD_ERROR:
3964 ALOGD("copl(%p): sending error callback to AF", out);
3965 send_callback = true;
3966 event = STREAM_CBK_EVENT_ERROR;
3967 break;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003968 default:
3969 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
3970 break;
3971 }
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07003972 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003973 out->offload_thread_blocked = false;
3974 pthread_cond_signal(&out->cond);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08003975 if (send_callback && out->client_callback) {
3976 ALOGVV("%s: sending client_callback event %d", __func__, event);
3977 out->client_callback(event, NULL, out->client_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -07003978 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003979 free(cmd);
3980 }
3981
3982 pthread_cond_signal(&out->cond);
3983 while (!list_empty(&out->offload_cmd_list)) {
3984 item = list_head(&out->offload_cmd_list);
3985 list_remove(item);
3986 free(node_to_item(item, struct offload_cmd, node));
3987 }
3988 pthread_mutex_unlock(&out->lock);
3989
3990 return NULL;
3991}
3992
3993static int create_offload_callback_thread(struct stream_out *out)
3994{
3995 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
3996 list_init(&out->offload_cmd_list);
3997 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
3998 offload_thread_loop, out);
3999 return 0;
4000}
4001
4002static int destroy_offload_callback_thread(struct stream_out *out)
4003{
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07004004 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004005 stop_compressed_output_l(out);
4006 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
4007
4008 pthread_mutex_unlock(&out->lock);
4009 pthread_join(out->offload_thread, (void **) NULL);
4010 pthread_cond_destroy(&out->offload_cond);
4011
4012 return 0;
4013}
4014
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004015static int stop_output_stream(struct stream_out *out)
4016{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05304017 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004018 struct audio_usecase *uc_info;
4019 struct audio_device *adev = out->dev;
Aalique Grahame22e49102018-12-18 14:23:57 -08004020 bool has_voip_usecase =
4021 get_usecase_from_list(adev, USECASE_AUDIO_PLAYBACK_VOIP) != NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004022
Eric Laurent994a6932013-07-17 11:51:42 -07004023 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07004024 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004025 uc_info = get_usecase_from_list(adev, out->usecase);
4026 if (uc_info == NULL) {
4027 ALOGE("%s: Could not find the usecase (%d) in the list",
4028 __func__, out->usecase);
4029 return -EINVAL;
4030 }
4031
Zhou Songbaddf9f2020-11-20 13:57:39 +08004032 out->a2dp_muted = false;
4033
Derek Chenea197282019-01-07 17:35:01 -08004034 if (audio_extn_ext_hw_plugin_usecase_stop(adev->ext_hw_plugin, uc_info))
4035 ALOGE("%s: failed to stop ext hw plugin", __func__);
Derek Chend2530072014-11-24 12:39:14 -08004036
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004037 if (is_offload_usecase(out->usecase) &&
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05304038 !(audio_extn_passthru_is_passthrough_stream(out))) {
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08004039 if (adev->visualizer_stop_output != NULL)
4040 adev->visualizer_stop_output(out->handle, out->pcm_device_id);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08004041
4042 audio_extn_dts_remove_state_notifier_node(out->usecase);
4043
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08004044 if (adev->offload_effects_stop_output != NULL)
4045 adev->offload_effects_stop_output(out->handle, out->pcm_device_id);
vivek mehtad15d2bf2019-05-17 13:35:10 -07004046 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_ULL ||
4047 out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
4048 audio_low_latency_hint_end();
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08004049 }
Eric Laurentc4aef752013-09-12 17:45:53 -07004050
Arun Mirpuri2ce92a62018-09-12 18:36:10 -07004051 if (out->usecase == USECASE_INCALL_MUSIC_UPLINK ||
4052 out->usecase == USECASE_INCALL_MUSIC_UPLINK2) {
Arun Mirpurief53ce52018-09-11 18:00:09 -07004053 voice_set_device_mute_flag(adev, false);
Arun Mirpuri2ce92a62018-09-12 18:36:10 -07004054 }
Arun Mirpurief53ce52018-09-11 18:00:09 -07004055
Eric Laurent150dbfe2013-02-27 14:31:02 -08004056 /* 1. Get and set stream specific mixer controls */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07004057 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07004058
4059 /* 2. Disable the rx device */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07004060 disable_snd_device(adev, uc_info->out_snd_device);
Zhou Songd9bd9302020-08-04 16:34:45 +08004061 if (out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS)
4062 disable_snd_device(adev, SND_DEVICE_OUT_HAPTICS);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004063
Aalique Grahame22e49102018-12-18 14:23:57 -08004064 audio_extn_extspk_update(adev->extspk);
4065
Xiaojun Sang785b5da2017-08-03 15:52:29 +08004066 if (is_offload_usecase(out->usecase)) {
4067 audio_enable_asm_bit_width_enforce_mode(adev->mixer,
4068 adev->dsp_bit_width_enforce_mode,
4069 false);
4070 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004071 if (is_usb_out_device_type(&out->device_list)) {
Garmond Leung5fd0b552018-04-17 11:56:12 -07004072 ret = audio_extn_usb_check_and_set_svc_int(uc_info,
4073 false);
4074
4075 if (ret != 0)
4076 check_usecases_codec_backend(adev, uc_info, uc_info->out_snd_device);
4077 /* default service interval was successfully updated,
4078 reopen USB backend with new service interval */
4079 ret = 0;
4080 }
Xiaojun Sang785b5da2017-08-03 15:52:29 +08004081
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08004082 list_remove(&uc_info->list);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304083 out->started = 0;
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004084 if (is_offload_usecase(out->usecase) &&
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05304085 (audio_extn_passthru_is_passthrough_stream(out))) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004086 ALOGV("Disable passthrough , reset mixer to pcm");
4087 /* NO_PASSTHROUGH */
Meng Wang4c32fb42020-01-16 17:57:11 +08004088#ifdef AUDIO_GKI_ENABLED
4089 /* out->compr_config.codec->reserved[0] is for compr_passthr */
4090 out->compr_config.codec->reserved[0] = 0;
4091#else
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004092 out->compr_config.codec->compr_passthr = 0;
Meng Wang4c32fb42020-01-16 17:57:11 +08004093#endif
Mingming Yin21854652016-04-13 11:54:02 -07004094 audio_extn_passthru_on_stop(out);
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004095 audio_extn_dolby_set_dap_bypass(adev, DAP_STATE_ON);
4096 }
Eric Laurent07eeafd2013-10-06 12:52:49 -07004097
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05304098 /* Must be called after removing the usecase from list */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004099 if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_AUX_DIGITAL))
Md Mansoor Ahmeddb1b4f92018-01-25 18:56:31 +05304100 audio_extn_keep_alive_start(KEEP_ALIVE_OUT_HDMI);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05304101
Manish Dewangan21a850a2017-08-14 12:03:55 +05304102 if (out->ip_hdlr_handle) {
Naresh Tanniru85819452017-05-04 18:55:45 -07004103 ret = audio_extn_ip_hdlr_intf_close(out->ip_hdlr_handle, true, out);
4104 if (ret < 0)
4105 ALOGE("%s: audio_extn_ip_hdlr_intf_close failed %d",__func__, ret);
4106 }
4107
Zhou Song642ec432020-12-23 16:11:10 +08004108 /* trigger voip input to reroute when voip output changes to hearing aid */
Aalique Grahame22e49102018-12-18 14:23:57 -08004109 if (has_voip_usecase ||
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004110 compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
Aalique Grahame22e49102018-12-18 14:23:57 -08004111 struct listnode *node;
4112 struct audio_usecase *usecase;
4113 list_for_each(node, &adev->usecase_list) {
4114 usecase = node_to_item(node, struct audio_usecase, list);
Zhou Song642ec432020-12-23 16:11:10 +08004115 if (usecase->type == PCM_PLAYBACK || usecase == uc_info ||
4116 (usecase->type == PCM_CAPTURE &&
Revathi Uddarajud9f23d92020-07-27 10:55:06 +05304117 usecase->id != USECASE_AUDIO_RECORD_VOIP &&
Zhou Song642ec432020-12-23 16:11:10 +08004118 usecase->id != USECASE_AUDIO_RECORD_VOIP_LOW_LATENCY))
Aalique Grahame22e49102018-12-18 14:23:57 -08004119 continue;
4120
4121 ALOGD("%s: select_devices at usecase(%d: %s) after removing the usecase(%d: %s)",
4122 __func__, usecase->id, use_case_table[usecase->id],
4123 out->usecase, use_case_table[out->usecase]);
4124 select_devices(adev, usecase->id);
4125 }
4126 }
4127
Zhenlin Lian4f947842022-05-14 15:50:52 +05304128 clear_devices(&uc_info->device_list);
Garmond Leung5fd0b552018-04-17 11:56:12 -07004129 free(uc_info);
Eric Laurent994a6932013-07-17 11:51:42 -07004130 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004131 return ret;
4132}
4133
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004134struct pcm* pcm_open_prepare_helper(unsigned int snd_card, unsigned int pcm_device_id,
4135 unsigned int flags, unsigned int pcm_open_retry_count,
4136 struct pcm_config *config)
4137{
4138 struct pcm* pcm = NULL;
4139
4140 while (1) {
4141 pcm = pcm_open(snd_card, pcm_device_id, flags, config);
4142 if (pcm == NULL || !pcm_is_ready(pcm)) {
4143 ALOGE("%s: %s", __func__, pcm_get_error(pcm));
4144 if (pcm != NULL) {
4145 pcm_close(pcm);
4146 pcm = NULL;
4147 }
Weiyin Jiang72197252019-10-09 11:49:32 +08004148 if (pcm_open_retry_count == 0)
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004149 return NULL;
4150
Weiyin Jiang72197252019-10-09 11:49:32 +08004151 pcm_open_retry_count--;
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004152 usleep(PROXY_OPEN_WAIT_TIME * 1000);
4153 continue;
4154 }
4155 break;
4156 }
4157
4158 if (pcm_is_ready(pcm)) {
4159 int ret = pcm_prepare(pcm);
4160 if (ret < 0) {
4161 ALOGE("%s: pcm_prepare returned %d", __func__, ret);
4162 pcm_close(pcm);
4163 pcm = NULL;
4164 }
4165 }
4166
4167 return pcm;
4168}
4169
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004170int start_output_stream(struct stream_out *out)
4171{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004172 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004173 struct audio_usecase *uc_info;
4174 struct audio_device *adev = out->dev;
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08004175 char mixer_ctl_name[128];
4176 struct mixer_ctl *ctl = NULL;
4177 char* perf_mode[] = {"ULL", "ULL_PP", "LL"};
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304178 bool a2dp_combo = false;
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004179 bool is_haptic_usecase = (out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) ? true: false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004180
Haynes Mathew George380745d2017-10-04 15:27:45 -07004181 ATRACE_BEGIN("start_output_stream");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07004182 if ((out->usecase < 0) || (out->usecase >= AUDIO_USECASE_MAX)) {
4183 ret = -EINVAL;
4184 goto error_config;
4185 }
4186
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004187 ALOGD("%s: enter: stream(%p)usecase(%d: %s) devices(%#x) is_haptic_usecase(%d)",
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05304188 __func__, &out->stream, out->usecase, use_case_table[out->usecase],
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004189 get_device_types(&out->device_list), is_haptic_usecase);
4190
4191 bool is_speaker_active = compare_device_type(&out->device_list,
4192 AUDIO_DEVICE_OUT_SPEAKER);
4193 bool is_speaker_safe_active = compare_device_type(&out->device_list,
4194 AUDIO_DEVICE_OUT_SPEAKER_SAFE);
Naresh Tanniru4c630392014-05-12 01:05:52 +05304195
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304196 if (CARD_STATUS_OFFLINE == out->card_status ||
Shubhasini Sugumaran3fde6582022-01-19 15:54:50 -05004197 CARD_STATUS_OFFLINE == adev->card_status ||
4198 POWER_POLICY_STATUS_OFFLINE == adev->out_power_policy) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304199 ALOGW("out->card_status or adev->card_status offline, try again");
Dhanalakshmi Siddani4d57e992014-07-17 16:37:51 +05304200 ret = -EIO;
Mingshu Pang5fc696f2020-02-28 12:32:00 +08004201 goto error_fatal;
Naresh Tanniru4c630392014-05-12 01:05:52 +05304202 }
Naresh Tanniru4c630392014-05-12 01:05:52 +05304203
Arun Mirpuri2ce92a62018-09-12 18:36:10 -07004204 //Update incall music usecase to reflect correct voice session
4205 if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
4206 ret = voice_extn_check_and_set_incall_music_usecase(adev, out);
4207 if (ret != 0) {
4208 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
4209 __func__, ret);
4210 goto error_config;
4211 }
4212 }
4213
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004214 if (is_a2dp_out_device_type(&out->device_list)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02004215 if (!audio_extn_a2dp_source_is_ready()) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004216 if (is_speaker_active || is_speaker_safe_active) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304217 a2dp_combo = true;
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05304218 } else {
Zhou Songd01e7a22020-09-23 22:49:01 +08004219 if (!is_offload_usecase(out->usecase)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304220 ALOGE("%s: A2DP profile is not ready, return error", __func__);
4221 ret = -EAGAIN;
4222 goto error_config;
4223 }
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05304224 }
4225 }
4226 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004227 if (is_sco_out_device_type(&out->device_list)) {
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304228 if (!adev->bt_sco_on) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004229 if (is_speaker_active) {
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304230 //combo usecase just by pass a2dp
4231 ALOGW("%s: SCO is not connected, route it to speaker", __func__);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004232 reassign_device_list(&out->device_list, AUDIO_DEVICE_OUT_SPEAKER, "");
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304233 } else {
4234 ALOGE("%s: SCO profile is not ready, return error", __func__);
4235 ret = -EAGAIN;
4236 goto error_config;
4237 }
4238 }
4239 }
4240
Eric Laurentb23d5282013-05-14 15:27:20 -07004241 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004242 if (out->pcm_device_id < 0) {
4243 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
4244 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08004245 ret = -EINVAL;
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004246 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004247 }
4248
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004249 if (is_haptic_usecase) {
Meng Wang51d8c2a2020-04-27 15:23:21 +08004250 adev->haptic_pcm_device_id = platform_get_pcm_device_id(
4251 USECASE_AUDIO_PLAYBACK_HAPTICS, PCM_PLAYBACK);
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004252 if (adev->haptic_pcm_device_id < 0) {
4253 ALOGE("%s: Invalid Haptics pcm device id(%d) for the usecase(%d)",
4254 __func__, adev->haptic_pcm_device_id, out->usecase);
4255 ret = -EINVAL;
4256 goto error_config;
4257 }
4258 }
4259
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004260 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07004261
4262 if (!uc_info) {
4263 ret = -ENOMEM;
4264 goto error_config;
4265 }
4266
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004267 uc_info->id = out->usecase;
4268 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08004269 uc_info->stream.out = out;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004270 list_init(&uc_info->device_list);
4271 assign_devices(&uc_info->device_list, &out->device_list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07004272 uc_info->in_snd_device = SND_DEVICE_NONE;
4273 uc_info->out_snd_device = SND_DEVICE_NONE;
Garmond Leung5fd0b552018-04-17 11:56:12 -07004274
4275 /* This must be called before adding this usecase to the list */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004276 if (is_usb_out_device_type(&out->device_list)) {
Garmond Leung5fd0b552018-04-17 11:56:12 -07004277 audio_extn_usb_check_and_set_svc_int(uc_info, true);
4278 /* USB backend is not reopened immediately.
4279 This is eventually done as part of select_devices */
4280 }
4281
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08004282 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004283
Wei Wangf7ca6c92017-11-21 14:51:20 -08004284 audio_streaming_hint_start();
Sudheer Papothifa9d2282015-09-17 01:53:25 +05304285 audio_extn_perf_lock_acquire(&adev->perf_lock_handle, 0,
4286 adev->perf_lock_opts,
4287 adev->perf_lock_opts_size);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05304288
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004289 if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
Md Mansoor Ahmeddb1b4f92018-01-25 18:56:31 +05304290 audio_extn_keep_alive_stop(KEEP_ALIVE_OUT_HDMI);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05304291 if (audio_extn_passthru_is_enabled() &&
4292 audio_extn_passthru_is_passthrough_stream(out)) {
4293 audio_extn_passthru_on_start(out);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05304294 }
4295 }
4296
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004297 if (is_a2dp_out_device_type(&out->device_list) &&
Florian Pfister1a84f312018-07-19 14:38:18 +02004298 (!audio_extn_a2dp_source_is_ready())) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304299 if (!a2dp_combo) {
4300 check_a2dp_restore_l(adev, out, false);
4301 } else {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004302 struct listnode dev;
4303 list_init(&dev);
4304 assign_devices(&dev, &out->device_list);
4305 if (compare_device_type(&dev, AUDIO_DEVICE_OUT_SPEAKER_SAFE))
4306 reassign_device_list(&out->device_list,
4307 AUDIO_DEVICE_OUT_SPEAKER_SAFE, "");
Aalique Grahame22e49102018-12-18 14:23:57 -08004308 else
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004309 reassign_device_list(&out->device_list,
4310 AUDIO_DEVICE_OUT_SPEAKER, "");
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304311 select_devices(adev, out->usecase);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004312 assign_devices(&out->device_list, &dev);
Zhenlin Lian4f947842022-05-14 15:50:52 +05304313 clear_devices(&dev);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304314 }
4315 } else {
Revathi Uddarajub26e3932020-06-10 14:51:02 +05304316 select_devices(adev, out->usecase);
4317 if (is_a2dp_out_device_type(&out->device_list) &&
4318 !adev->a2dp_started) {
4319 if (is_speaker_active || is_speaker_safe_active) {
4320 struct listnode dev;
4321 list_init(&dev);
4322 assign_devices(&dev, &out->device_list);
4323 if (compare_device_type(&dev, AUDIO_DEVICE_OUT_SPEAKER_SAFE))
4324 reassign_device_list(&out->device_list,
4325 AUDIO_DEVICE_OUT_SPEAKER_SAFE, "");
4326 else
4327 reassign_device_list(&out->device_list,
4328 AUDIO_DEVICE_OUT_SPEAKER, "");
4329 select_devices(adev, out->usecase);
4330 assign_devices(&out->device_list, &dev);
Zhenlin Lian4f947842022-05-14 15:50:52 +05304331 clear_devices(&dev);
Revathi Uddarajub26e3932020-06-10 14:51:02 +05304332 } else {
4333 ret = -EINVAL;
4334 goto error_open;
4335 }
4336 }
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304337 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07004338
Arun Mirpuri2ce92a62018-09-12 18:36:10 -07004339 if (out->usecase == USECASE_INCALL_MUSIC_UPLINK ||
4340 out->usecase == USECASE_INCALL_MUSIC_UPLINK2) {
Arun Mirpurief53ce52018-09-11 18:00:09 -07004341 voice_set_device_mute_flag(adev, true);
Arun Mirpuri2ce92a62018-09-12 18:36:10 -07004342 }
Arun Mirpurief53ce52018-09-11 18:00:09 -07004343
Derek Chenea197282019-01-07 17:35:01 -08004344 if (audio_extn_ext_hw_plugin_usecase_start(adev->ext_hw_plugin, uc_info))
4345 ALOGE("%s: failed to start ext hw plugin", __func__);
Derek Chend2530072014-11-24 12:39:14 -08004346
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07004347 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d) format(%#x)",
4348 __func__, adev->snd_card, out->pcm_device_id, out->config.format);
Haynes Mathew George16081042017-05-31 17:16:49 -07004349
4350 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
Arun Mirpuri5d170872019-03-26 13:21:31 -07004351 ALOGD("%s: Starting MMAP stream", __func__);
Haynes Mathew George16081042017-05-31 17:16:49 -07004352 if (out->pcm == NULL || !pcm_is_ready(out->pcm)) {
4353 ALOGE("%s: pcm stream not ready", __func__);
4354 goto error_open;
4355 }
4356 ret = pcm_start(out->pcm);
4357 if (ret < 0) {
4358 ALOGE("%s: MMAP pcm_start failed ret %d", __func__, ret);
4359 goto error_open;
4360 }
Arun Mirpuri5d170872019-03-26 13:21:31 -07004361 out_set_mmap_volume(&out->stream, out->volume_l, out->volume_r);
Haynes Mathew George16081042017-05-31 17:16:49 -07004362 } else if (!is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07004363 unsigned int flags = PCM_OUT;
4364 unsigned int pcm_open_retry_count = 0;
4365 if (out->usecase == USECASE_AUDIO_PLAYBACK_AFE_PROXY) {
4366 flags |= PCM_MMAP | PCM_NOIRQ;
4367 pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT;
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004368 } else if (out->realtime) {
Haynes Mathew George4ab3ba92017-12-11 14:49:43 -08004369 flags |= PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07004370 } else
4371 flags |= PCM_MONOTONIC;
4372
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08004373 if ((adev->vr_audio_mode_enabled) &&
4374 (out->flags & AUDIO_OUTPUT_FLAG_RAW)) {
4375 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
4376 "PCM_Dev %d Topology", out->pcm_device_id);
4377 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
4378 if (!ctl) {
4379 ALOGI("%s: Could not get ctl for mixer cmd might be ULL - %s",
4380 __func__, mixer_ctl_name);
4381 } else {
4382 //if success use ULLPP
4383 ALOGI("%s: mixer ctrl %s succeeded setting up ULL for %d",
4384 __func__, mixer_ctl_name, out->pcm_device_id);
4385 //There is a still a possibility that some sessions
4386 // that request for FAST|RAW when 3D audio is active
4387 //can go through ULLPP. Ideally we expects apps to
4388 //listen to audio focus and stop concurrent playback
4389 //Also, we will look for mode flag (voice_in_communication)
4390 //before enabling the realtime flag.
4391 mixer_ctl_set_enum_by_string(ctl, perf_mode[1]);
4392 }
4393 }
4394
Vatsal Bucha3f39f222021-06-29 16:16:55 +05304395 platform_set_stream_channel_map(adev->platform, out->channel_mask,
4396 out->pcm_device_id, -1, &out->channel_map_param.channel_map[0]);
Surendar Karka91fa3682018-07-02 18:12:12 +05304397
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004398 out->pcm = pcm_open_prepare_helper(adev->snd_card, out->pcm_device_id,
4399 flags, pcm_open_retry_count,
4400 &(out->config));
4401 if (out->pcm == NULL) {
4402 ret = -EIO;
4403 goto error_open;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004404 }
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004405
4406 if (is_haptic_usecase) {
4407 adev->haptic_pcm = pcm_open_prepare_helper(adev->snd_card,
4408 adev->haptic_pcm_device_id,
4409 flags, pcm_open_retry_count,
4410 &(adev->haptics_config));
4411 // failure to open haptics pcm shouldnt stop audio,
4412 // so do not close audio pcm in case of error
Vignesh Kulothungane4039c12019-05-07 15:51:39 -07004413
4414 if (property_get_bool("vendor.audio.enable_haptic_audio_sync", false)) {
4415 ALOGD("%s: enable haptic audio synchronization", __func__);
4416 platform_set_qtime(adev->platform, out->pcm_device_id, adev->haptic_pcm_device_id);
4417 }
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004418 }
4419
Zhou Song2b8f28f2017-09-11 10:51:38 +08004420 // apply volume for voip playback after path is set up
4421 if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP)
4422 out_set_voip_volume(&out->stream, out->volume_l, out->volume_r);
Ramu Gottipati36547092018-12-28 11:32:09 +05304423 else if ((out->usecase == USECASE_AUDIO_PLAYBACK_LOW_LATENCY || out->usecase == USECASE_AUDIO_PLAYBACK_DEEP_BUFFER ||
4424 out->usecase == USECASE_AUDIO_PLAYBACK_ULL) && (out->apply_volume)) {
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05304425 out_set_pcm_volume(&out->stream, out->volume_l, out->volume_r);
4426 out->apply_volume = false;
Derek Chenf13dd492018-11-13 14:53:51 -08004427 } else if (audio_extn_auto_hal_is_bus_device_usecase(out->usecase)) {
4428 out_set_pcm_volume(&out->stream, out->volume_l, out->volume_r);
Krishna Kishor Jha10ad39f2022-02-21 17:02:47 +05304429#ifdef SOFT_VOLUME
4430 out_set_soft_volume_params(&out->stream);
4431#endif
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05304432 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004433 } else {
Zhou Song79929fe2020-01-20 17:57:43 +08004434 /*
4435 * set custom channel map if:
4436 * 1. neither mono nor stereo clips i.e. channels > 2 OR
4437 * 2. custom channel map has been set by client
4438 * else default channel map of FC/FR/FL can always be set to DSP
4439 */
4440 if (popcount(out->channel_mask) > 2 || out->channel_map_param.channel_map[0])
4441 platform_set_stream_channel_map(adev->platform, out->channel_mask,
Weiyin Jiang810a80d2021-01-18 16:04:31 +08004442 out->pcm_device_id, -1, &out->channel_map_param.channel_map[0]);
Xiaojun Sang785b5da2017-08-03 15:52:29 +08004443 audio_enable_asm_bit_width_enforce_mode(adev->mixer,
4444 adev->dsp_bit_width_enforce_mode,
4445 true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004446 out->pcm = NULL;
Haynes Mathew George380745d2017-10-04 15:27:45 -07004447 ATRACE_BEGIN("compress_open");
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08004448 out->compr = compress_open(adev->snd_card,
4449 out->pcm_device_id,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004450 COMPRESS_IN, &out->compr_config);
Haynes Mathew George380745d2017-10-04 15:27:45 -07004451 ATRACE_END();
Satish Babu Patakokila54ce83d2018-07-06 18:00:37 +05304452 if (errno == ENETRESET && !is_compress_ready(out->compr)) {
Sharad Sanglec6f32552018-05-04 16:15:38 +05304453 ALOGE("%s: compress_open failed errno:%d\n", __func__, errno);
4454 adev->card_status = CARD_STATUS_OFFLINE;
4455 out->card_status = CARD_STATUS_OFFLINE;
4456 ret = -EIO;
4457 goto error_open;
4458 }
4459
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004460 if (out->compr && !is_compress_ready(out->compr)) {
Haynes Mathew Georgeee5836f2017-11-21 18:02:10 -08004461 ALOGE("%s: failed /w error %s", __func__, compress_get_error(out->compr));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004462 compress_close(out->compr);
4463 out->compr = NULL;
4464 ret = -EIO;
4465 goto error_open;
4466 }
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05304467 /* compress_open sends params of the track, so reset the flag here */
4468 out->is_compr_metadata_avail = false;
4469
Ben Rombergerd771a7c2017-02-22 18:05:17 -08004470 if (out->client_callback)
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004471 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07004472
Fred Oh3f43e742015-03-04 18:42:34 -08004473 /* Since small bufs uses blocking writes, a write will be blocked
4474 for the default max poll time (20s) in the event of an SSR.
4475 Reduce the poll time to observe and deal with SSR faster.
4476 */
Ashish Jain5106d362016-05-11 19:23:33 +05304477 if (!out->non_blocking) {
Fred Oh3f43e742015-03-04 18:42:34 -08004478 compress_set_max_poll_wait(out->compr, 1000);
4479 }
4480
Manish Dewangan69426c82017-01-30 17:35:36 +05304481 audio_extn_utils_compress_set_render_mode(out);
Manish Dewangan58229382017-02-02 15:48:41 +05304482 audio_extn_utils_compress_set_clk_rec_mode(uc_info);
Manish Dewangan69426c82017-01-30 17:35:36 +05304483
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08004484 audio_extn_dts_create_state_notifier_node(out->usecase);
4485 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
4486 popcount(out->channel_mask),
4487 out->playback_started);
4488
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08004489#ifdef DS1_DOLBY_DDP_ENABLED
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05304490 if (audio_extn_utils_is_dolby_format(out->format))
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08004491 audio_extn_dolby_send_ddp_endp_params(adev);
4492#endif
Preetam Singh Ranawatd18d8832017-02-08 17:34:54 +05304493 if (!(audio_extn_passthru_is_passthrough_stream(out)) &&
4494 (out->sample_rate != 176400 && out->sample_rate <= 192000)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004495 if (adev->visualizer_start_output != NULL)
4496 adev->visualizer_start_output(out->handle, out->pcm_device_id);
4497 if (adev->offload_effects_start_output != NULL)
Ashish Jain5106d362016-05-11 19:23:33 +05304498 adev->offload_effects_start_output(out->handle, out->pcm_device_id, adev->mixer);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08004499 audio_extn_check_and_set_dts_hpx_state(adev);
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004500 }
Derek Chenf13dd492018-11-13 14:53:51 -08004501
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004502 if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_BUS)) {
Derek Chenf13dd492018-11-13 14:53:51 -08004503 /* Update cached volume from media to offload/direct stream */
4504 struct listnode *node = NULL;
4505 list_for_each(node, &adev->active_outputs_list) {
4506 streams_output_ctxt_t *out_ctxt = node_to_item(node,
4507 streams_output_ctxt_t,
4508 list);
4509 if (out_ctxt->output->usecase == USECASE_AUDIO_PLAYBACK_MEDIA) {
4510 out->volume_l = out_ctxt->output->volume_l;
4511 out->volume_r = out_ctxt->output->volume_r;
4512 }
4513 }
4514 out_set_compr_volume(&out->stream,
4515 out->volume_l, out->volume_r);
4516 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004517 }
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004518
4519 if (ret == 0) {
Vaibhav Raut5f3bf222023-02-06 17:45:22 +05304520 if (out->flags & (AUDIO_OUTPUT_FLAG_FAST | AUDIO_OUTPUT_FLAG_RAW))
Narsinga Rao Chellaa6e1f702021-10-22 13:57:32 +05304521 register_out_stream(out);
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004522 if (out->realtime) {
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07004523 if (out->pcm == NULL || !pcm_is_ready(out->pcm)) {
4524 ALOGE("%s: pcm stream not ready", __func__);
4525 goto error_open;
4526 }
Haynes Mathew George380745d2017-10-04 15:27:45 -07004527 ATRACE_BEGIN("pcm_start");
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004528 ret = pcm_start(out->pcm);
Haynes Mathew George380745d2017-10-04 15:27:45 -07004529 ATRACE_END();
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004530 if (ret < 0)
4531 goto error_open;
4532 }
4533 }
Wei Wangf7ca6c92017-11-21 14:51:20 -08004534 audio_streaming_hint_end();
Sudheer Papothifa9d2282015-09-17 01:53:25 +05304535 audio_extn_perf_lock_release(&adev->perf_lock_handle);
Jaideep Sharma0fa53812020-09-17 09:00:11 +05304536 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07004537
vivek mehtad15d2bf2019-05-17 13:35:10 -07004538 if (out->usecase == USECASE_AUDIO_PLAYBACK_ULL ||
4539 out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
4540 audio_low_latency_hint_start();
4541 }
4542
Manish Dewangan21a850a2017-08-14 12:03:55 +05304543 if (out->ip_hdlr_handle) {
Vidyakumar Athota6d655882017-05-22 18:26:24 -07004544 ret = audio_extn_ip_hdlr_intf_open(out->ip_hdlr_handle, true, out, out->usecase);
Naresh Tanniru85819452017-05-04 18:55:45 -07004545 if (ret < 0)
4546 ALOGE("%s: audio_extn_ip_hdlr_intf_open failed %d",__func__, ret);
4547 }
4548
Vignesh Kulothungan3b5fae52017-09-25 12:16:30 -07004549 // consider a scenario where on pause lower layers are tear down.
4550 // so on resume, swap mixer control need to be sent only when
4551 // backend is active, hence rather than sending from enable device
4552 // sending it from start of streamtream
4553
4554 platform_set_swap_channels(adev, true);
4555
Haynes Mathew George380745d2017-10-04 15:27:45 -07004556 ATRACE_END();
Vatsal Buchac09ae062018-11-14 13:25:08 +05304557 enable_gcov();
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004558 return ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004559error_open:
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004560 if (adev->haptic_pcm) {
4561 pcm_close(adev->haptic_pcm);
4562 adev->haptic_pcm = NULL;
4563 }
Wei Wangf7ca6c92017-11-21 14:51:20 -08004564 audio_streaming_hint_end();
Sudheer Papothifa9d2282015-09-17 01:53:25 +05304565 audio_extn_perf_lock_release(&adev->perf_lock_handle);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004566 stop_output_stream(out);
Mingshu Pang5fc696f2020-02-28 12:32:00 +08004567error_fatal:
Laxminath Kasam2cb4b752015-09-24 03:59:15 +05304568 /*
4569 * sleep 50ms to allow sufficient time for kernel
4570 * drivers to recover incases like SSR.
4571 */
4572 usleep(50000);
Mingshu Pang5fc696f2020-02-28 12:32:00 +08004573error_config:
Haynes Mathew George380745d2017-10-04 15:27:45 -07004574 ATRACE_END();
Vatsal Buchac09ae062018-11-14 13:25:08 +05304575 enable_gcov();
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08004576 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004577}
4578
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004579static int check_input_parameters(uint32_t sample_rate,
4580 audio_format_t format,
Aalique Grahame22e49102018-12-18 14:23:57 -08004581 int channel_count,
4582 bool is_usb_hifi)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004583{
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08004584 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004585
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05304586 if (((format != AUDIO_FORMAT_PCM_16_BIT) && (format != AUDIO_FORMAT_PCM_8_24_BIT) &&
4587 (format != AUDIO_FORMAT_PCM_24_BIT_PACKED) && (format != AUDIO_FORMAT_PCM_32_BIT) &&
4588 (format != AUDIO_FORMAT_PCM_FLOAT)) &&
Mingming Yine62d7842013-10-25 16:26:03 -07004589 !voice_extn_compress_voip_is_format_supported(format) &&
Ralf Herzaec80262018-07-03 07:08:49 +02004590 !audio_extn_compr_cap_format_supported(format) &&
4591 !audio_extn_cin_format_supported(format))
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004592 ret = -EINVAL;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08004593
Aalique Grahame22e49102018-12-18 14:23:57 -08004594 int max_channel_count = is_usb_hifi ? MAX_HIFI_CHANNEL_COUNT : MAX_CHANNEL_COUNT;
4595 if ((channel_count < MIN_CHANNEL_COUNT) || (channel_count > max_channel_count)) {
4596 ALOGE("%s: unsupported channel count (%d) passed Min / Max (%d / %d)", __func__,
4597 channel_count, MIN_CHANNEL_COUNT, max_channel_count);
4598 return -EINVAL;
4599 }
4600
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08004601 switch (channel_count) {
4602 case 1:
4603 case 2:
Chaithanya Krishna Bacharaju9955b162016-05-25 16:25:53 +05304604 case 3:
4605 case 4:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08004606 case 6:
Karthikeyan Mani07faa602018-08-20 11:01:32 -07004607 case 8:
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05304608 case 10:
4609 case 12:
4610 case 14:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08004611 break;
4612 default:
4613 ret = -EINVAL;
4614 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004615
4616 switch (sample_rate) {
4617 case 8000:
4618 case 11025:
4619 case 12000:
4620 case 16000:
4621 case 22050:
4622 case 24000:
4623 case 32000:
4624 case 44100:
4625 case 48000:
Haynes Mathew Georgec9253d12017-12-13 15:58:28 -08004626 case 88200:
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05304627 case 96000:
Haynes Mathew Georgec9253d12017-12-13 15:58:28 -08004628 case 176400:
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05304629 case 192000:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004630 break;
4631 default:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08004632 ret = -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004633 }
4634
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08004635 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004636}
4637
Naresh Tanniru04f71882018-06-26 17:46:22 +05304638
4639/** Add a value in a list if not already present.
4640 * @return true if value was successfully inserted or already present,
4641 * false if the list is full and does not contain the value.
4642 */
4643static bool register_uint(uint32_t value, uint32_t* list, size_t list_length) {
4644 for (size_t i = 0; i < list_length; i++) {
4645 if (list[i] == value) return true; // value is already present
4646 if (list[i] == 0) { // no values in this slot
4647 list[i] = value;
4648 return true; // value inserted
4649 }
4650 }
4651 return false; // could not insert value
4652}
4653
4654/** Add channel_mask in supported_channel_masks if not already present.
4655 * @return true if channel_mask was successfully inserted or already present,
4656 * false if supported_channel_masks is full and does not contain channel_mask.
4657 */
4658static void register_channel_mask(audio_channel_mask_t channel_mask,
4659 audio_channel_mask_t supported_channel_masks[static MAX_SUPPORTED_CHANNEL_MASKS]) {
4660 ALOGE_IF(!register_uint(channel_mask, supported_channel_masks, MAX_SUPPORTED_CHANNEL_MASKS),
4661 "%s: stream can not declare supporting its channel_mask %x", __func__, channel_mask);
4662}
4663
4664/** Add format in supported_formats if not already present.
4665 * @return true if format was successfully inserted or already present,
4666 * false if supported_formats is full and does not contain format.
4667 */
4668static void register_format(audio_format_t format,
4669 audio_format_t supported_formats[static MAX_SUPPORTED_FORMATS]) {
4670 ALOGE_IF(!register_uint(format, supported_formats, MAX_SUPPORTED_FORMATS),
4671 "%s: stream can not declare supporting its format %x", __func__, format);
4672}
4673/** Add sample_rate in supported_sample_rates if not already present.
4674 * @return true if sample_rate was successfully inserted or already present,
4675 * false if supported_sample_rates is full and does not contain sample_rate.
4676 */
4677static void register_sample_rate(uint32_t sample_rate,
4678 uint32_t supported_sample_rates[static MAX_SUPPORTED_SAMPLE_RATES]) {
4679 ALOGE_IF(!register_uint(sample_rate, supported_sample_rates, MAX_SUPPORTED_SAMPLE_RATES),
4680 "%s: stream can not declare supporting its sample rate %x", __func__, sample_rate);
4681}
4682
Karthikeyan Manib38769c2018-11-07 15:44:13 -08004683static inline uint32_t lcm(uint32_t num1, uint32_t num2)
4684{
4685 uint32_t high = num1, low = num2, temp = 0;
4686
4687 if (!num1 || !num2)
4688 return 0;
4689
4690 if (num1 < num2) {
4691 high = num2;
4692 low = num1;
4693 }
4694
4695 while (low != 0) {
4696 temp = low;
4697 low = high % low;
4698 high = temp;
4699 }
4700 return (num1 * num2)/high;
4701}
4702
4703static inline uint32_t nearest_multiple(uint32_t num, uint32_t multiplier)
4704{
4705 uint32_t remainder = 0;
4706
4707 if (!multiplier)
4708 return num;
4709
4710 remainder = num % multiplier;
4711 if (remainder)
4712 num += (multiplier - remainder);
4713
4714 return num;
4715}
4716
Aalique Grahame22e49102018-12-18 14:23:57 -08004717static size_t get_stream_buffer_size(size_t duration_ms,
4718 uint32_t sample_rate,
4719 audio_format_t format,
4720 int channel_count,
4721 bool is_low_latency)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004722{
4723 size_t size = 0;
Karthikeyan Manib38769c2018-11-07 15:44:13 -08004724 uint32_t bytes_per_period_sample = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004725
Aalique Grahame22e49102018-12-18 14:23:57 -08004726 size = (sample_rate * duration_ms) / 1000;
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +05304727 if (is_low_latency){
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +05304728#ifndef PLATFORM_AUTO
4729 size = configured_low_latency_capture_period_size;
4730#else
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +05304731 switch(sample_rate) {
4732 case 48000:
4733 size = 240;
4734 break;
4735 case 32000:
4736 size = 160;
4737 break;
4738 case 24000:
4739 size = 120;
4740 break;
4741 case 16000:
4742 size = 80;
4743 break;
4744 case 8000:
4745 size = 40;
4746 break;
4747 default:
4748 size = 240;
4749 }
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +05304750#endif
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +05304751 }
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05304752
Karthikeyan Manib38769c2018-11-07 15:44:13 -08004753 bytes_per_period_sample = audio_bytes_per_sample(format) * channel_count;
Aalique Grahame22e49102018-12-18 14:23:57 -08004754 size *= audio_bytes_per_sample(format) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004755
Ralf Herzbd08d632018-09-28 15:50:49 +02004756 /* make sure the size is multiple of 32 bytes and additionally multiple of
4757 * the frame_size (required for 24bit samples and non-power-of-2 channel counts)
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07004758 * At 48 kHz mono 16-bit PCM:
4759 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
4760 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
Karthikeyan Manib38769c2018-11-07 15:44:13 -08004761 * Also, make sure the size is multiple of bytes per period sample
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07004762 */
Karthikeyan Manib38769c2018-11-07 15:44:13 -08004763 size = nearest_multiple(size, lcm(32, bytes_per_period_sample));
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07004764
4765 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004766}
4767
Aalique Grahame22e49102018-12-18 14:23:57 -08004768static size_t get_input_buffer_size(uint32_t sample_rate,
4769 audio_format_t format,
4770 int channel_count,
4771 bool is_low_latency)
4772{
Avinash Chandrad7296d42021-08-04 15:07:47 +05304773 bool is_usb_hifi = IS_USB_HIFI;
Aalique Grahame22e49102018-12-18 14:23:57 -08004774 /* Don't know if USB HIFI in this context so use true to be conservative */
4775 if (check_input_parameters(sample_rate, format, channel_count,
Avinash Chandrad7296d42021-08-04 15:07:47 +05304776 is_usb_hifi) != 0)
Aalique Grahame22e49102018-12-18 14:23:57 -08004777 return 0;
4778
4779 return get_stream_buffer_size(AUDIO_CAPTURE_PERIOD_DURATION_MSEC,
4780 sample_rate,
4781 format,
4782 channel_count,
4783 is_low_latency);
4784}
4785
Derek Chenf6318be2017-06-12 17:16:24 -04004786size_t get_output_period_size(uint32_t sample_rate,
4787 audio_format_t format,
4788 int channel_count,
4789 int duration /*in millisecs*/)
Ashish Jain058165c2016-09-28 23:18:48 +05304790{
4791 size_t size = 0;
4792 uint32_t bytes_per_sample = audio_bytes_per_sample(format);
4793
4794 if ((duration == 0) || (sample_rate == 0) ||
4795 (bytes_per_sample == 0) || (channel_count == 0)) {
4796 ALOGW("Invalid config duration %d sr %d bps %d ch %d", duration, sample_rate,
4797 bytes_per_sample, channel_count);
4798 return -EINVAL;
4799 }
4800
4801 size = (sample_rate *
4802 duration *
4803 bytes_per_sample *
4804 channel_count) / 1000;
4805 /*
4806 * To have same PCM samples for all channels, the buffer size requires to
4807 * be multiple of (number of channels * bytes per sample)
4808 * For writes to succeed, the buffer must be written at address which is multiple of 32
4809 */
4810 size = ALIGN(size, (bytes_per_sample * channel_count * 32));
4811
4812 return (size/(channel_count * bytes_per_sample));
4813}
4814
Zhou Song48453a02018-01-10 17:50:59 +08004815static uint64_t get_actual_pcm_frames_rendered(struct stream_out *out, struct timespec *timestamp)
Ashish Jain5106d362016-05-11 19:23:33 +05304816{
4817 uint64_t actual_frames_rendered = 0;
Weiyin Jiang4813da12020-05-28 00:37:28 +08004818 uint64_t written_frames = 0;
4819 uint64_t kernel_frames = 0;
4820 uint64_t dsp_frames = 0;
4821 uint64_t signed_frames = 0;
4822 size_t kernel_buffer_size = 0;
Ashish Jain5106d362016-05-11 19:23:33 +05304823
4824 /* This adjustment accounts for buffering after app processor.
4825 * It is based on estimated DSP latency per use case, rather than exact.
4826 */
George Gao9ba8a142020-07-23 14:30:03 -07004827 dsp_frames = platform_render_latency(out) *
Weiyin Jiang4813da12020-05-28 00:37:28 +08004828 out->sample_rate / 1000000LL;
Ashish Jain5106d362016-05-11 19:23:33 +05304829
Zhou Song48453a02018-01-10 17:50:59 +08004830 pthread_mutex_lock(&out->position_query_lock);
Weiyin Jiang4813da12020-05-28 00:37:28 +08004831 written_frames = out->written /
4832 (audio_bytes_per_sample(out->hal_ip_format) * popcount(out->channel_mask));
4833
Ashish Jain5106d362016-05-11 19:23:33 +05304834 /* not querying actual state of buffering in kernel as it would involve an ioctl call
4835 * which then needs protection, this causes delay in TS query for pcm_offload usecase
4836 * hence only estimate.
4837 */
Weiyin Jiang4813da12020-05-28 00:37:28 +08004838 kernel_buffer_size = out->compr_config.fragment_size * out->compr_config.fragments;
4839 kernel_frames = kernel_buffer_size /
4840 (audio_bytes_per_sample(out->hal_op_format) * popcount(out->channel_mask));
Ashish Jain5106d362016-05-11 19:23:33 +05304841
Weiyin Jiang4813da12020-05-28 00:37:28 +08004842 if (written_frames >= (kernel_frames + dsp_frames))
4843 signed_frames = written_frames - kernel_frames - dsp_frames;
Ashish Jain5106d362016-05-11 19:23:33 +05304844
Zhou Song48453a02018-01-10 17:50:59 +08004845 if (signed_frames > 0) {
Ashish Jain5106d362016-05-11 19:23:33 +05304846 actual_frames_rendered = signed_frames;
Zhou Song48453a02018-01-10 17:50:59 +08004847 if (timestamp != NULL )
4848 *timestamp = out->writeAt;
4849 } else if (timestamp != NULL) {
4850 clock_gettime(CLOCK_MONOTONIC, timestamp);
4851 }
4852 pthread_mutex_unlock(&out->position_query_lock);
Ashish Jain5106d362016-05-11 19:23:33 +05304853
Weiyin Jiang4813da12020-05-28 00:37:28 +08004854 ALOGVV("%s signed frames %lld written frames %lld kernel frames %lld dsp frames %lld",
4855 __func__, signed_frames, written_frames, kernel_frames, dsp_frames);
Ashish Jain5106d362016-05-11 19:23:33 +05304856
4857 return actual_frames_rendered;
4858}
4859
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004860static uint32_t out_get_sample_rate(const struct audio_stream *stream)
4861{
4862 struct stream_out *out = (struct stream_out *)stream;
4863
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004864 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004865}
4866
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07004867static int out_set_sample_rate(struct audio_stream *stream __unused,
4868 uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004869{
4870 return -ENOSYS;
4871}
4872
4873static size_t out_get_buffer_size(const struct audio_stream *stream)
4874{
4875 struct stream_out *out = (struct stream_out *)stream;
4876
Varun Balaraje49253e2017-07-06 19:48:56 +05304877 if (is_interactive_usecase(out->usecase)) {
Sri Karri27279e12017-08-07 16:05:20 +05304878 return out->config.period_size * out->config.period_count;
Varun Balaraje49253e2017-07-06 19:48:56 +05304879 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Naresh Tanniruee3499a2017-01-05 14:05:35 +05304880 if (out->flags & AUDIO_OUTPUT_FLAG_TIMESTAMP)
4881 return out->compr_config.fragment_size - sizeof(struct snd_codec_metadata);
4882 else
4883 return out->compr_config.fragment_size;
4884 } else if(out->usecase == USECASE_COMPRESS_VOIP_CALL)
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08004885 return voice_extn_compress_voip_out_get_buffer_size(out);
Dhananjay Kumarac341582017-02-23 23:42:25 +05304886 else if (is_offload_usecase(out->usecase) &&
4887 out->flags == AUDIO_OUTPUT_FLAG_DIRECT)
Ashish Jain83a6cc22016-06-28 14:34:17 +05304888 return out->hal_fragment_size;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004889
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004890 return out->config.period_size * out->af_period_multiplier *
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07004891 audio_stream_out_frame_size((const struct audio_stream_out *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004892}
4893
4894static uint32_t out_get_channels(const struct audio_stream *stream)
4895{
4896 struct stream_out *out = (struct stream_out *)stream;
4897
4898 return out->channel_mask;
4899}
4900
4901static audio_format_t out_get_format(const struct audio_stream *stream)
4902{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004903 struct stream_out *out = (struct stream_out *)stream;
4904
4905 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004906}
4907
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07004908static int out_set_format(struct audio_stream *stream __unused,
4909 audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004910{
4911 return -ENOSYS;
4912}
4913
4914static int out_standby(struct audio_stream *stream)
4915{
4916 struct stream_out *out = (struct stream_out *)stream;
4917 struct audio_device *adev = out->dev;
Haynes Mathew George16081042017-05-31 17:16:49 -07004918 bool do_stop = true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004919
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05304920 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
4921 stream, out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004922
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07004923 lock_output_stream(out);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004924 if (!out->standby) {
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07004925 if (adev->adm_deregister_stream)
4926 adev->adm_deregister_stream(adev->adm_data, out->handle);
4927
Weiyin Jiang280ea742020-09-08 20:28:22 +08004928 if (is_offload_usecase(out->usecase)) {
Haynes Mathew George7fce0a52016-06-23 18:22:27 -07004929 stop_compressed_output_l(out);
Weiyin Jiang280ea742020-09-08 20:28:22 +08004930 }
Haynes Mathew George7fce0a52016-06-23 18:22:27 -07004931
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08004932 pthread_mutex_lock(&adev->lock);
Daniel Hillenbrand17873952013-05-23 10:10:00 +05304933
4934 amplifier_output_stream_standby((struct audio_stream_out *) stream);
4935
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004936 out->standby = true;
Zhou Songa8895042016-07-05 17:54:22 +08004937 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
4938 voice_extn_compress_voip_close_output_stream(stream);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304939 out->started = 0;
Zhou Songa8895042016-07-05 17:54:22 +08004940 pthread_mutex_unlock(&adev->lock);
4941 pthread_mutex_unlock(&out->lock);
4942 ALOGD("VOIP output entered standby");
4943 return 0;
4944 } else if (!is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004945 if (out->pcm) {
4946 pcm_close(out->pcm);
4947 out->pcm = NULL;
4948 }
Meng Wanga09da002020-04-20 12:56:04 +08004949 if (out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) {
4950 if (adev->haptic_pcm) {
4951 pcm_close(adev->haptic_pcm);
4952 adev->haptic_pcm = NULL;
4953 }
4954
4955 if (adev->haptic_buffer != NULL) {
4956 free(adev->haptic_buffer);
4957 adev->haptic_buffer = NULL;
4958 adev->haptic_buffer_size = 0;
4959 }
4960 adev->haptic_pcm_device_id = 0;
4961 }
4962
Haynes Mathew George16081042017-05-31 17:16:49 -07004963 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
4964 do_stop = out->playback_started;
4965 out->playback_started = false;
Phil Burkd898ba62019-06-20 12:49:01 -07004966
4967 if (out->mmap_shared_memory_fd >= 0) {
4968 ALOGV("%s: closing mmap_shared_memory_fd = %d",
4969 __func__, out->mmap_shared_memory_fd);
4970 close(out->mmap_shared_memory_fd);
4971 out->mmap_shared_memory_fd = -1;
4972 }
Haynes Mathew George16081042017-05-31 17:16:49 -07004973 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004974 } else {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07004975 ALOGD("copl(%p):standby", out);
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05304976 out->send_next_track_params = false;
4977 out->is_compr_metadata_avail = false;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004978 out->gapless_mdata.encoder_delay = 0;
4979 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004980 if (out->compr != NULL) {
4981 compress_close(out->compr);
4982 out->compr = NULL;
4983 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08004984 }
Haynes Mathew George16081042017-05-31 17:16:49 -07004985 if (do_stop) {
4986 stop_output_stream(out);
4987 }
Lakshman Chaluvaraju06677b42019-06-24 10:04:52 +05304988 // if fm is active route on selected device in UI
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004989 audio_extn_fm_route_on_selected_device(adev, &out->device_list);
Eric Laurent150dbfe2013-02-27 14:31:02 -08004990 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004991 }
4992 pthread_mutex_unlock(&out->lock);
Aniket Kumar Latad13758f2020-08-06 15:11:36 -07004993 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004994 return 0;
4995}
4996
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304997static int out_on_error(struct audio_stream *stream)
4998{
4999 struct stream_out *out = (struct stream_out *)stream;
Ben Rombergerfd02a2f2018-09-17 10:23:23 -07005000 int status = 0;
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305001
5002 lock_output_stream(out);
Haynes Mathew Georgeee5836f2017-11-21 18:02:10 -08005003 // always send CMD_ERROR for offload streams, this
5004 // is needed e.g. when SSR happens within compress_open
5005 // since the stream is active, offload_callback_thread is also active.
5006 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
5007 stop_compressed_output_l(out);
Ben Rombergerfd02a2f2018-09-17 10:23:23 -07005008 }
5009 pthread_mutex_unlock(&out->lock);
5010
5011 status = out_standby(&out->stream.common);
5012
5013 lock_output_stream(out);
5014 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Haynes Mathew Georgeee5836f2017-11-21 18:02:10 -08005015 send_offload_cmd_l(out, OFFLOAD_CMD_ERROR);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305016 }
Manisha Agarwal7b3e3772019-02-20 14:33:45 +05305017
5018 if (is_offload_usecase(out->usecase) && out->card_status == CARD_STATUS_OFFLINE) {
5019 ALOGD("Setting previous card status if offline");
5020 out->prev_card_status_offline = true;
5021 }
5022
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305023 pthread_mutex_unlock(&out->lock);
5024
Ben Rombergerfd02a2f2018-09-17 10:23:23 -07005025 return status;
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305026}
5027
Ashish Jain1b9b30c2017-05-18 20:57:40 +05305028/*
Weiyin Jiang280ea742020-09-08 20:28:22 +08005029 * standby implementation without locks, assumes that the callee already
5030 * has taken adev and out lock.
Ashish Jain1b9b30c2017-05-18 20:57:40 +05305031 */
5032int out_standby_l(struct audio_stream *stream)
5033{
5034 struct stream_out *out = (struct stream_out *)stream;
5035 struct audio_device *adev = out->dev;
5036
5037 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
5038 stream, out->usecase, use_case_table[out->usecase]);
5039
5040 if (!out->standby) {
Haynes Mathew George380745d2017-10-04 15:27:45 -07005041 ATRACE_BEGIN("out_standby_l");
Ashish Jain1b9b30c2017-05-18 20:57:40 +05305042 if (adev->adm_deregister_stream)
5043 adev->adm_deregister_stream(adev->adm_data, out->handle);
5044
Weiyin Jiang280ea742020-09-08 20:28:22 +08005045 if (is_offload_usecase(out->usecase)) {
Ashish Jain1b9b30c2017-05-18 20:57:40 +05305046 stop_compressed_output_l(out);
Weiyin Jiang280ea742020-09-08 20:28:22 +08005047 }
Ashish Jain1b9b30c2017-05-18 20:57:40 +05305048
5049 out->standby = true;
5050 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
5051 voice_extn_compress_voip_close_output_stream(stream);
5052 out->started = 0;
5053 ALOGD("VOIP output entered standby");
Haynes Mathew George380745d2017-10-04 15:27:45 -07005054 ATRACE_END();
Ashish Jain1b9b30c2017-05-18 20:57:40 +05305055 return 0;
5056 } else if (!is_offload_usecase(out->usecase)) {
5057 if (out->pcm) {
5058 pcm_close(out->pcm);
5059 out->pcm = NULL;
5060 }
Vignesh Kulothungana6927272019-02-20 15:17:07 -08005061 if (out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) {
5062 if (adev->haptic_pcm) {
5063 pcm_close(adev->haptic_pcm);
5064 adev->haptic_pcm = NULL;
5065 }
5066
5067 if (adev->haptic_buffer != NULL) {
5068 free(adev->haptic_buffer);
5069 adev->haptic_buffer = NULL;
5070 adev->haptic_buffer_size = 0;
5071 }
5072 adev->haptic_pcm_device_id = 0;
5073 }
Ashish Jain1b9b30c2017-05-18 20:57:40 +05305074 } else {
5075 ALOGD("copl(%p):standby", out);
5076 out->send_next_track_params = false;
5077 out->is_compr_metadata_avail = false;
5078 out->gapless_mdata.encoder_delay = 0;
5079 out->gapless_mdata.encoder_padding = 0;
5080 if (out->compr != NULL) {
5081 compress_close(out->compr);
5082 out->compr = NULL;
5083 }
5084 }
5085 stop_output_stream(out);
Haynes Mathew George380745d2017-10-04 15:27:45 -07005086 ATRACE_END();
Ashish Jain1b9b30c2017-05-18 20:57:40 +05305087 }
Aniket Kumar Latad13758f2020-08-06 15:11:36 -07005088 ALOGV("%s: exit", __func__);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05305089 return 0;
5090}
5091
Aalique Grahame22e49102018-12-18 14:23:57 -08005092static int out_dump(const struct audio_stream *stream, int fd)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005093{
Aalique Grahame22e49102018-12-18 14:23:57 -08005094 struct stream_out *out = (struct stream_out *)stream;
5095
5096 // We try to get the lock for consistency,
5097 // but it isn't necessary for these variables.
5098 // If we're not in standby, we may be blocked on a write.
5099 const bool locked = (pthread_mutex_trylock(&out->lock) == 0);
5100 dprintf(fd, " Standby: %s\n", out->standby ? "yes" : "no");
5101 dprintf(fd, " Frames written: %lld\n", (long long)out->written);
Dechen Chai22768452021-07-30 09:29:16 +05305102#ifndef LINUX_ENABLED
Andy Hunga1f48fa2019-07-01 18:14:53 -07005103 char buffer[256]; // for statistics formatting
5104 if (!is_offload_usecase(out->usecase)) {
5105 simple_stats_to_string(&out->fifo_underruns, buffer, sizeof(buffer));
5106 dprintf(fd, " Fifo frame underruns: %s\n", buffer);
5107 }
5108
Andy Hungc6bfd4a2019-07-01 18:26:00 -07005109 if (out->start_latency_ms.n > 0) {
5110 simple_stats_to_string(&out->start_latency_ms, buffer, sizeof(buffer));
5111 dprintf(fd, " Start latency ms: %s\n", buffer);
5112 }
Dechen Chai22768452021-07-30 09:29:16 +05305113#endif
Aalique Grahame22e49102018-12-18 14:23:57 -08005114 if (locked) {
5115 pthread_mutex_unlock(&out->lock);
5116 }
5117
Dechen Chai22768452021-07-30 09:29:16 +05305118#ifndef LINUX_ENABLED
Aalique Grahame22e49102018-12-18 14:23:57 -08005119 // dump error info
5120 (void)error_log_dump(
5121 out->error_log, fd, " " /* prefix */, 0 /* lines */, 0 /* limit_ns */);
Dechen Chai22768452021-07-30 09:29:16 +05305122#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005123 return 0;
5124}
5125
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005126static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
5127{
5128 int ret = 0;
5129 char value[32];
ApurupaPattapu2e084df2013-12-18 15:47:59 -08005130
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005131 if (!out || !parms) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08005132 ALOGE("%s: return invalid ",__func__);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005133 return -EINVAL;
5134 }
5135
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +05305136 ret = audio_extn_parse_compress_metadata(out, parms);
Weiyin Jiang18ac4e92015-03-15 15:03:40 +08005137
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005138 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
5139 if (ret >= 0) {
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +05305140 out->gapless_mdata.encoder_delay = atoi(value); //whats a good limit check?
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005141 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005142 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
5143 if (ret >= 0) {
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +05305144 out->gapless_mdata.encoder_padding = atoi(value);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005145 }
5146
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005147 ALOGV("%s new encoder delay %u and padding %u", __func__,
5148 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
5149
5150 return 0;
5151}
5152
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07005153static bool output_drives_call(struct audio_device *adev, struct stream_out *out)
5154{
5155 return out == adev->primary_output || out == adev->voice_tx_output;
5156}
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005157
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305158// note: this call is safe only if the stream_cb is
5159// removed first in close_output_stream (as is done now).
5160static void out_snd_mon_cb(void * stream, struct str_parms * parms)
5161{
5162 if (!stream || !parms)
5163 return;
5164
5165 struct stream_out *out = (struct stream_out *)stream;
5166 struct audio_device *adev = out->dev;
5167
5168 card_status_t status;
5169 int card;
5170 if (parse_snd_card_status(parms, &card, &status) < 0)
5171 return;
5172
5173 pthread_mutex_lock(&adev->lock);
5174 bool valid_cb = (card == adev->snd_card);
5175 pthread_mutex_unlock(&adev->lock);
5176
5177 if (!valid_cb)
5178 return;
5179
5180 lock_output_stream(out);
5181 if (out->card_status != status)
5182 out->card_status = status;
5183 pthread_mutex_unlock(&out->lock);
5184
5185 ALOGI("out_snd_mon_cb for card %d usecase %s, status %s", card,
5186 use_case_table[out->usecase],
5187 status == CARD_STATUS_OFFLINE ? "offline" : "online");
5188
Aditya Bavanari59ebed42019-02-05 17:44:57 +05305189 if (status == CARD_STATUS_OFFLINE) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305190 out_on_error(stream);
Aditya Bavanari59ebed42019-02-05 17:44:57 +05305191 if (voice_is_call_state_active(adev) &&
5192 out == adev->primary_output) {
5193 ALOGD("%s: SSR/PDR occurred, end all calls\n", __func__);
5194 pthread_mutex_lock(&adev->lock);
5195 voice_stop_call(adev);
5196 adev->mode = AUDIO_MODE_NORMAL;
5197 pthread_mutex_unlock(&adev->lock);
5198 }
5199 }
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305200 return;
5201}
5202
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005203int route_output_stream(struct stream_out *out,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005204 struct listnode *devices)
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005205{
5206 struct audio_device *adev = out->dev;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005207 int ret = 0;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005208 struct listnode new_devices;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005209 bool bypass_a2dp = false;
5210 bool reconfig = false;
5211 unsigned long service_interval = 0;
5212
5213 ALOGD("%s: enter: usecase(%d: %s) devices %x",
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005214 __func__, out->usecase, use_case_table[out->usecase], get_device_types(devices));
5215
5216 list_init(&new_devices);
5217 assign_devices(&new_devices, devices);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005218
5219 lock_output_stream(out);
5220 pthread_mutex_lock(&adev->lock);
5221
5222 /*
5223 * When HDMI cable is unplugged the music playback is paused and
5224 * the policy manager sends routing=0. But the audioflinger continues
5225 * to write data until standby time (3sec). As the HDMI core is
5226 * turned off, the write gets blocked.
5227 * Avoid this by routing audio to speaker until standby.
5228 */
Aniket Kumar Lata9c2fd892020-01-22 22:20:00 -08005229 if (is_single_device_type_equal(&out->device_list,
5230 AUDIO_DEVICE_OUT_AUX_DIGITAL) &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005231 list_empty(&new_devices) &&
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005232 !audio_extn_passthru_is_passthrough_stream(out) &&
5233 (platform_get_edid_info(adev->platform) != 0) /* HDMI disconnected */) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005234 reassign_device_list(&new_devices, AUDIO_DEVICE_OUT_SPEAKER, "");
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005235 }
5236 /*
5237 * When A2DP is disconnected the
5238 * music playback is paused and the policy manager sends routing=0
5239 * But the audioflinger continues to write data until standby time
5240 * (3sec). As BT is turned off, the write gets blocked.
5241 * Avoid this by routing audio to speaker until standby.
5242 */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005243 if (is_a2dp_out_device_type(&out->device_list) &&
5244 list_empty(&new_devices) &&
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005245 !audio_extn_a2dp_source_is_ready() &&
5246 !adev->bt_sco_on) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005247 reassign_device_list(&new_devices, AUDIO_DEVICE_OUT_SPEAKER, "");
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005248 }
5249 /*
Weiyin Jiangabedea32020-12-09 12:49:19 +08005250 * When USB headset is disconnected the music playback paused
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005251 * and the policy manager send routing=0. But if the USB is connected
5252 * back before the standby time, AFE is not closed and opened
5253 * when USB is connected back. So routing to speker will guarantee
5254 * AFE reconfiguration and AFE will be opend once USB is connected again
5255 */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005256 if (is_usb_out_device_type(&out->device_list) &&
5257 list_empty(&new_devices) &&
5258 !audio_extn_usb_connected(NULL)) {
Sujin Panicker84d953a2020-11-16 17:39:54 +05305259 if (adev->mode == AUDIO_MODE_IN_CALL || adev->mode == AUDIO_MODE_IN_COMMUNICATION)
5260 reassign_device_list(&new_devices, AUDIO_DEVICE_OUT_EARPIECE, "");
5261 else
5262 reassign_device_list(&new_devices, AUDIO_DEVICE_OUT_SPEAKER, "");
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005263 }
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005264 /* To avoid a2dp to sco overlapping / BT device improper state
5265 * check with BT lib about a2dp streaming support before routing
5266 */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005267 if (is_a2dp_out_device_type(&new_devices)) {
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005268 if (!audio_extn_a2dp_source_is_ready()) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005269 if (compare_device_type(&new_devices, AUDIO_DEVICE_OUT_SPEAKER) ||
5270 compare_device_type(&new_devices, AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005271 //combo usecase just by pass a2dp
5272 ALOGW("%s: A2DP profile is not ready,routing to speaker only", __func__);
5273 bypass_a2dp = true;
5274 } else {
5275 ALOGE("%s: A2DP profile is not ready,ignoring routing request", __func__);
5276 /* update device to a2dp and don't route as BT returned error
5277 * However it is still possible a2dp routing called because
5278 * of current active device disconnection (like wired headset)
5279 */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005280 assign_devices(&out->device_list, &new_devices);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005281 pthread_mutex_unlock(&adev->lock);
5282 pthread_mutex_unlock(&out->lock);
5283 goto error;
5284 }
5285 }
5286 }
5287
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005288 // Workaround: If routing to an non existing usb device, fail gracefully
5289 // The routing request will otherwise block during 10 second
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005290 if (is_usb_out_device_type(&new_devices)) {
5291 struct str_parms *parms =
5292 str_parms_create_str(get_usb_device_address(&new_devices));
5293 if (!parms)
5294 goto error;
Weiyin Jiangabedea32020-12-09 12:49:19 +08005295 if (!audio_extn_usb_connected(NULL)) {
5296 ALOGW("%s: ignoring rerouting to non existing USB card", __func__);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005297 pthread_mutex_unlock(&adev->lock);
5298 pthread_mutex_unlock(&out->lock);
5299 str_parms_destroy(parms);
5300 ret = -ENOSYS;
5301 goto error;
5302 }
5303 str_parms_destroy(parms);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005304 }
5305
Weiyin Jiang80c8f9a2020-01-10 20:42:08 +08005306 // Workaround: If routing to an non existing hdmi device, fail gracefully
5307 if (compare_device_type(&new_devices, AUDIO_DEVICE_OUT_AUX_DIGITAL) &&
5308 (platform_get_edid_info_v2(adev->platform,
5309 out->extconn.cs.controller,
5310 out->extconn.cs.stream) != 0)) {
5311 ALOGW("out_set_parameters() ignoring rerouting to non existing HDMI/DP");
5312 pthread_mutex_unlock(&adev->lock);
5313 pthread_mutex_unlock(&out->lock);
5314 ret = -ENOSYS;
5315 goto error;
5316 }
5317
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005318 /*
5319 * select_devices() call below switches all the usecases on the same
5320 * backend to the new device. Refer to check_usecases_codec_backend() in
5321 * the select_devices(). But how do we undo this?
5322 *
5323 * For example, music playback is active on headset (deep-buffer usecase)
5324 * and if we go to ringtones and select a ringtone, low-latency usecase
5325 * will be started on headset+speaker. As we can't enable headset+speaker
5326 * and headset devices at the same time, select_devices() switches the music
5327 * playback to headset+speaker while starting low-lateny usecase for ringtone.
5328 * So when the ringtone playback is completed, how do we undo the same?
5329 *
5330 * We are relying on the out_set_parameters() call on deep-buffer output,
5331 * once the ringtone playback is ended.
5332 * NOTE: We should not check if the current devices are same as new devices.
5333 * Because select_devices() must be called to switch back the music
5334 * playback to headset.
5335 */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005336 if (!list_empty(&new_devices)) {
5337 bool same_dev = compare_devices(&out->device_list, &new_devices);
5338 assign_devices(&out->device_list, &new_devices);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005339
5340 if (output_drives_call(adev, out)) {
5341 if (!voice_is_call_state_active(adev)) {
5342 if (adev->mode == AUDIO_MODE_IN_CALL) {
5343 adev->current_call_output = out;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005344 ret = voice_start_call(adev);
5345 }
5346 } else {
Kunlei Zhang253ad102020-10-14 16:21:28 +08005347 platform_is_volume_boost_supported_device(adev->platform, &new_devices);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005348 adev->current_call_output = out;
5349 voice_update_devices_for_all_voice_usecases(adev);
5350 }
5351 }
5352
Mingshu Pang971ff702020-09-09 15:28:22 +08005353 if (is_usb_out_device_type(&out->device_list)) {
5354 service_interval = audio_extn_usb_find_service_interval(false, true /*playback*/);
5355 audio_extn_usb_set_service_interval(true /*playback*/,
5356 service_interval,
5357 &reconfig);
5358 ALOGD("%s, svc_int(%ld),reconfig(%d)",__func__,service_interval, reconfig);
5359 }
5360
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005361 if (!out->standby) {
5362 if (!same_dev) {
5363 ALOGV("update routing change");
5364 audio_extn_perf_lock_acquire(&adev->perf_lock_handle, 0,
5365 adev->perf_lock_opts,
5366 adev->perf_lock_opts_size);
5367 if (adev->adm_on_routing_change)
5368 adev->adm_on_routing_change(adev->adm_data,
5369 out->handle);
5370 }
5371 if (!bypass_a2dp) {
5372 select_devices(adev, out->usecase);
5373 } else {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005374 if (compare_device_type(&new_devices, AUDIO_DEVICE_OUT_SPEAKER_SAFE))
5375 reassign_device_list(&out->device_list, AUDIO_DEVICE_OUT_SPEAKER_SAFE, "");
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005376 else
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005377 reassign_device_list(&out->device_list, AUDIO_DEVICE_OUT_SPEAKER, "");
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005378 select_devices(adev, out->usecase);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005379 assign_devices(&out->device_list, &new_devices);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005380 }
5381
5382 if (!same_dev) {
5383 // on device switch force swap, lower functions will make sure
5384 // to check if swap is allowed or not.
5385 platform_set_swap_channels(adev, true);
5386 audio_extn_perf_lock_release(&adev->perf_lock_handle);
5387 }
Zhou Songbaddf9f2020-11-20 13:57:39 +08005388 pthread_mutex_lock(&out->latch_lock);
5389 if (!is_a2dp_out_device_type(&out->device_list) || audio_extn_a2dp_source_is_ready()) {
5390 if (out->a2dp_muted) {
5391 out->a2dp_muted = false;
5392 if (is_offload_usecase(out->usecase))
5393 out_set_compr_volume(&out->stream, out->volume_l, out->volume_r);
5394 else if (out->usecase != USECASE_AUDIO_PLAYBACK_VOIP)
5395 out_set_pcm_volume(&out->stream, out->volume_l, out->volume_r);
Weiyin Jiang280ea742020-09-08 20:28:22 +08005396 }
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005397 }
Zhou Songbaddf9f2020-11-20 13:57:39 +08005398 if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP && !out->a2dp_muted)
5399 out_set_voip_volume(&out->stream, out->volume_l, out->volume_r);
5400 pthread_mutex_unlock(&out->latch_lock);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005401 }
5402 }
5403
5404 pthread_mutex_unlock(&adev->lock);
5405 pthread_mutex_unlock(&out->lock);
5406
5407 /*handles device and call state changes*/
5408 audio_extn_extspk_update(adev->extspk);
5409
Revathi Uddaraju4255a632021-12-02 05:11:13 -08005410 clear_devices(&new_devices);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005411error:
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005412 ALOGV("%s: exit: code(%d)", __func__, ret);
5413 return ret;
5414}
5415
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005416static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
5417{
5418 struct stream_out *out = (struct stream_out *)stream;
5419 struct audio_device *adev = out->dev;
5420 struct str_parms *parms;
5421 char value[32];
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005422 int ret = 0, err;
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07005423 int ext_controller = -1;
5424 int ext_stream = -1;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005425
sangwoobc677242013-08-08 16:53:43 +09005426 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07005427 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005428 parms = str_parms_create_str(kvpairs);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05305429 if (!parms)
5430 goto error;
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07005431
Daniel Hillenbrand17873952013-05-23 10:10:00 +05305432 amplifier_out_set_parameters(parms);
5433
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07005434 err = platform_get_controller_stream_from_params(parms, &ext_controller,
5435 &ext_stream);
Vignesh Kulothungan1b3957e2019-12-02 16:16:51 -08005436 if (err == 0) {
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07005437 out->extconn.cs.controller = ext_controller;
5438 out->extconn.cs.stream = ext_stream;
5439 ALOGD("%s: usecase(%s) new controller/stream (%d/%d)", __func__,
5440 use_case_table[out->usecase], out->extconn.cs.controller,
5441 out->extconn.cs.stream);
5442 }
5443
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07005444 if (out == adev->primary_output) {
5445 pthread_mutex_lock(&adev->lock);
5446 audio_extn_set_parameters(adev, parms);
5447 pthread_mutex_unlock(&adev->lock);
5448 }
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07005449 if (is_offload_usecase(out->usecase)) {
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005450 lock_output_stream(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005451 parse_compress_metadata(out, parms);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08005452
5453 audio_extn_dts_create_state_notifier_node(out->usecase);
5454 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
5455 popcount(out->channel_mask),
5456 out->playback_started);
5457
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08005458 pthread_mutex_unlock(&out->lock);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005459 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07005460
Surendar Karkaf51b5842018-04-26 11:28:38 +05305461 err = str_parms_get_str(parms, AUDIO_PARAMETER_DUAL_MONO, value,
5462 sizeof(value));
5463 if (err >= 0) {
5464 if (!strncmp("true", value, sizeof("true")) || atoi(value))
5465 audio_extn_send_dual_mono_mixing_coefficients(out);
5466 }
5467
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +05305468 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_PROFILE, value, sizeof(value));
5469 if (err >= 0) {
5470 strlcpy(out->profile, value, sizeof(out->profile));
5471 ALOGV("updating stream profile with value '%s'", out->profile);
5472 lock_output_stream(out);
5473 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
5474 &adev->streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005475 &out->device_list, out->flags,
5476 out->hal_op_format,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +05305477 out->sample_rate, out->bit_width,
5478 out->channel_mask, out->profile,
5479 &out->app_type_cfg);
5480 pthread_mutex_unlock(&out->lock);
5481 }
5482
Alexy Joseph98988832017-01-13 14:56:59 -08005483 //suspend, resume handling block
Aniket Kumar Lata932f4872017-11-06 18:29:44 -08005484 //remove QOS only if vendor.audio.hal.dynamic.qos.config.supported is set to true
5485 // and vendor.audio.hal.output.suspend.supported is set to true
5486 if (out->hal_output_suspend_supported && out->dynamic_pm_qos_config_supported) {
Alexy Joseph98988832017-01-13 14:56:59 -08005487 //check suspend parameter only for low latency and if the property
5488 //is enabled
5489 if (str_parms_get_str(parms, "suspend_playback", value, sizeof(value)) >= 0) {
5490 ALOGI("%s: got suspend_playback %s", __func__, value);
5491 lock_output_stream(out);
5492 if (!strncmp(value, "false", 5)) {
5493 //suspend_playback=false is supposed to set QOS value back to 75%
5494 //the mixer control sent with value Enable will achieve that
5495 ret = audio_route_apply_and_update_path(adev->audio_route, out->pm_qos_mixer_path);
5496 } else if (!strncmp (value, "true", 4)) {
5497 //suspend_playback=true is supposed to remove QOS value
5498 //resetting the mixer control will set the default value
5499 //for the mixer control which is Disable and this removes the QOS vote
5500 ret = audio_route_reset_and_update_path(adev->audio_route, out->pm_qos_mixer_path);
5501 } else {
5502 ALOGE("%s: Wrong value sent for suspend_playback, expected true/false,"
5503 " got %s", __func__, value);
5504 ret = -1;
5505 }
5506
5507 if (ret != 0) {
5508 ALOGE("%s: %s mixer ctl failed with %d, ignore suspend/resume setparams",
5509 __func__, out->pm_qos_mixer_path, ret);
5510 }
5511
5512 pthread_mutex_unlock(&out->lock);
5513 }
5514 }
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07005515
Alexy Joseph98988832017-01-13 14:56:59 -08005516 //end suspend, resume handling block
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005517 str_parms_destroy(parms);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05305518error:
Eric Laurent994a6932013-07-17 11:51:42 -07005519 ALOGV("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005520 return ret;
5521}
5522
Paul McLeana50b7332018-12-17 08:24:21 -07005523static int in_set_microphone_direction(const struct audio_stream_in *stream,
5524 audio_microphone_direction_t dir) {
justinweng20fb6d82019-02-21 18:49:00 -07005525 struct stream_in *in = (struct stream_in *)stream;
5526
5527 ALOGVV("%s: standby %d source %d dir %d", __func__, in->standby, in->source, dir);
5528
5529 in->direction = dir;
5530
5531 if (in->standby)
5532 return 0;
5533
5534 return audio_extn_audiozoom_set_microphone_direction(in, dir);
Paul McLeana50b7332018-12-17 08:24:21 -07005535}
5536
5537static int in_set_microphone_field_dimension(const struct audio_stream_in *stream, float zoom) {
justinweng20fb6d82019-02-21 18:49:00 -07005538 struct stream_in *in = (struct stream_in *)stream;
5539
5540 ALOGVV("%s: standby %d source %d zoom %f", __func__, in->standby, in->source, zoom);
5541
5542 if (zoom > 1.0 || zoom < -1.0)
5543 return -EINVAL;
5544
5545 in->zoom = zoom;
5546
5547 if (in->standby)
5548 return 0;
5549
5550 return audio_extn_audiozoom_set_microphone_field_dimension(in, zoom);
Paul McLeana50b7332018-12-17 08:24:21 -07005551}
5552
5553
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005554static bool stream_get_parameter_channels(struct str_parms *query,
5555 struct str_parms *reply,
5556 audio_channel_mask_t *supported_channel_masks) {
5557 int ret = -1;
5558 char value[512];
5559 bool first = true;
5560 size_t i, j;
5561
5562 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) {
5563 ret = 0;
5564 value[0] = '\0';
5565 i = 0;
5566 while (supported_channel_masks[i] != 0) {
5567 for (j = 0; j < ARRAY_SIZE(channels_name_to_enum_table); j++) {
5568 if (channels_name_to_enum_table[j].value == supported_channel_masks[i]) {
5569 if (!first)
Satya Krishna Pindiprolib22ac722017-10-09 15:44:16 +05305570 strlcat(value, "|", sizeof(value));
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005571
Satya Krishna Pindiprolib22ac722017-10-09 15:44:16 +05305572 strlcat(value, channels_name_to_enum_table[j].name, sizeof(value));
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005573 first = false;
5574 break;
5575 }
5576 }
5577 i++;
5578 }
5579 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
5580 }
5581 return ret == 0;
5582}
5583
5584static bool stream_get_parameter_formats(struct str_parms *query,
5585 struct str_parms *reply,
5586 audio_format_t *supported_formats) {
5587 int ret = -1;
5588 char value[256];
5589 size_t i, j;
5590 bool first = true;
5591
5592 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) {
5593 ret = 0;
5594 value[0] = '\0';
5595 i = 0;
5596 while (supported_formats[i] != 0) {
5597 for (j = 0; j < ARRAY_SIZE(formats_name_to_enum_table); j++) {
5598 if (formats_name_to_enum_table[j].value == supported_formats[i]) {
5599 if (!first) {
Satya Krishna Pindiprolib22ac722017-10-09 15:44:16 +05305600 strlcat(value, "|", sizeof(value));
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005601 }
Satya Krishna Pindiprolib22ac722017-10-09 15:44:16 +05305602 strlcat(value, formats_name_to_enum_table[j].name, sizeof(value));
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005603 first = false;
5604 break;
5605 }
5606 }
5607 i++;
5608 }
5609 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_FORMATS, value);
5610 }
5611 return ret == 0;
5612}
5613
5614static bool stream_get_parameter_rates(struct str_parms *query,
5615 struct str_parms *reply,
5616 uint32_t *supported_sample_rates) {
5617
5618 int i;
5619 char value[256];
5620 int ret = -1;
5621 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) {
5622 ret = 0;
5623 value[0] = '\0';
5624 i=0;
5625 int cursor = 0;
5626 while (supported_sample_rates[i]) {
5627 int avail = sizeof(value) - cursor;
5628 ret = snprintf(value + cursor, avail, "%s%d",
5629 cursor > 0 ? "|" : "",
5630 supported_sample_rates[i]);
5631 if (ret < 0 || ret >= avail) {
5632 // if cursor is at the last element of the array
5633 // overwrite with \0 is duplicate work as
5634 // snprintf already put a \0 in place.
5635 // else
5636 // we had space to write the '|' at value[cursor]
5637 // (which will be overwritten) or no space to fill
5638 // the first element (=> cursor == 0)
5639 value[cursor] = '\0';
5640 break;
5641 }
5642 cursor += ret;
5643 ++i;
5644 }
5645 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES,
5646 value);
5647 }
5648 return ret >= 0;
5649}
5650
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005651static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
5652{
5653 struct stream_out *out = (struct stream_out *)stream;
5654 struct str_parms *query = str_parms_create_str(keys);
Alexy Josephaee4fdd2016-01-29 13:02:07 -08005655 char *str = (char*) NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005656 char value[256];
5657 struct str_parms *reply = str_parms_create();
5658 size_t i, j;
5659 int ret;
5660 bool first = true;
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07005661
5662 if (!query || !reply) {
Alexy Josephaee4fdd2016-01-29 13:02:07 -08005663 if (reply) {
5664 str_parms_destroy(reply);
5665 }
5666 if (query) {
5667 str_parms_destroy(query);
5668 }
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07005669 ALOGE("out_get_parameters: failed to allocate mem for query or reply");
5670 return NULL;
5671 }
5672
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005673 ALOGV("%s: %s enter: keys - %s", __func__, use_case_table[out->usecase], keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005674 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
5675 if (ret >= 0) {
5676 value[0] = '\0';
5677 i = 0;
5678 while (out->supported_channel_masks[i] != 0) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005679 for (j = 0; j < ARRAY_SIZE(channels_name_to_enum_table); j++) {
5680 if (channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005681 if (!first) {
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -08005682 strlcat(value, "|", sizeof(value));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005683 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005684 strlcat(value, channels_name_to_enum_table[j].name, sizeof(value));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005685 first = false;
5686 break;
5687 }
5688 }
5689 i++;
5690 }
5691 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
5692 str = str_parms_to_str(reply);
5693 } else {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08005694 voice_extn_out_get_parameters(out, query, reply);
5695 str = str_parms_to_str(reply);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005696 }
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07005697
Alexy Joseph62142aa2015-11-16 15:10:34 -08005698
5699 ret = str_parms_get_str(query, "is_direct_pcm_track", value, sizeof(value));
5700 if (ret >= 0) {
5701 value[0] = '\0';
Dhananjay Kumarac341582017-02-23 23:42:25 +05305702 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
5703 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Alexy Joseph62142aa2015-11-16 15:10:34 -08005704 ALOGV("in direct_pcm");
Satya Krishna Pindiprolib6655542017-07-03 19:38:19 +05305705 strlcat(value, "true", sizeof(value));
Alexy Joseph62142aa2015-11-16 15:10:34 -08005706 } else {
5707 ALOGV("not in direct_pcm");
Sharad Sangle3dd5a4a2015-12-10 18:39:17 +05305708 strlcat(value, "false", sizeof(value));
Alexy Joseph62142aa2015-11-16 15:10:34 -08005709 }
5710 str_parms_add_str(reply, "is_direct_pcm_track", value);
Alexy Josephaee4fdd2016-01-29 13:02:07 -08005711 if (str)
5712 free(str);
Alexy Joseph62142aa2015-11-16 15:10:34 -08005713 str = str_parms_to_str(reply);
5714 }
5715
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07005716 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS, value, sizeof(value));
5717 if (ret >= 0) {
5718 value[0] = '\0';
5719 i = 0;
5720 first = true;
5721 while (out->supported_formats[i] != 0) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005722 for (j = 0; j < ARRAY_SIZE(formats_name_to_enum_table); j++) {
5723 if (formats_name_to_enum_table[j].value == out->supported_formats[i]) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07005724 if (!first) {
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -08005725 strlcat(value, "|", sizeof(value));
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07005726 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005727 strlcat(value, formats_name_to_enum_table[j].name, sizeof(value));
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07005728 first = false;
5729 break;
5730 }
5731 }
5732 i++;
5733 }
5734 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_FORMATS, value);
Alexy Josephaee4fdd2016-01-29 13:02:07 -08005735 if (str)
5736 free(str);
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07005737 str = str_parms_to_str(reply);
5738 }
Mingming Yin3a941d42016-02-17 18:08:05 -08005739
5740 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES, value, sizeof(value));
5741 if (ret >= 0) {
5742 value[0] = '\0';
5743 i = 0;
5744 first = true;
5745 while (out->supported_sample_rates[i] != 0) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005746 for (j = 0; j < ARRAY_SIZE(out_sample_rates_name_to_enum_table); j++) {
5747 if (out_sample_rates_name_to_enum_table[j].value == out->supported_sample_rates[i]) {
Mingming Yin3a941d42016-02-17 18:08:05 -08005748 if (!first) {
5749 strlcat(value, "|", sizeof(value));
5750 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005751 strlcat(value, out_sample_rates_name_to_enum_table[j].name, sizeof(value));
Mingming Yin3a941d42016-02-17 18:08:05 -08005752 first = false;
5753 break;
5754 }
5755 }
5756 i++;
5757 }
5758 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES, value);
5759 if (str)
5760 free(str);
5761 str = str_parms_to_str(reply);
5762 }
5763
Alexy Joseph98988832017-01-13 14:56:59 -08005764 if (str_parms_get_str(query, "supports_hw_suspend", value, sizeof(value)) >= 0) {
5765 //only low latency track supports suspend_resume
5766 str_parms_add_int(reply, "supports_hw_suspend",
Aniket Kumar Lata932f4872017-11-06 18:29:44 -08005767 (out->hal_output_suspend_supported));
Alexy Joseph98988832017-01-13 14:56:59 -08005768 if (str)
5769 free(str);
5770 str = str_parms_to_str(reply);
5771 }
5772
5773
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005774 str_parms_destroy(query);
5775 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07005776 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005777 return str;
5778}
5779
5780static uint32_t out_get_latency(const struct audio_stream_out *stream)
5781{
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005782 uint32_t period_ms;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005783 struct stream_out *out = (struct stream_out *)stream;
Alexy Josephaa54c872014-12-03 02:46:47 -08005784 uint32_t latency = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005785
Alexy Josephaa54c872014-12-03 02:46:47 -08005786 if (is_offload_usecase(out->usecase)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05305787 lock_output_stream(out);
5788 latency = audio_extn_utils_compress_get_dsp_latency(out);
5789 pthread_mutex_unlock(&out->lock);
Haynes Mathew George16081042017-05-31 17:16:49 -07005790 } else if ((out->realtime) ||
5791 (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP)) {
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005792 // since the buffer won't be filled up faster than realtime,
5793 // return a smaller number
5794 if (out->config.rate)
5795 period_ms = (out->af_period_multiplier * out->config.period_size *
5796 1000) / (out->config.rate);
5797 else
5798 period_ms = 0;
George Gao9ba8a142020-07-23 14:30:03 -07005799 latency = period_ms + platform_render_latency(out) / 1000;
Alexy Josephaa54c872014-12-03 02:46:47 -08005800 } else {
5801 latency = (out->config.period_count * out->config.period_size * 1000) /
Mingshu Pangf69a88d2021-04-30 16:14:39 +08005802 (out->config.rate);
pavanisra2d95d82022-02-09 18:55:58 +05305803 if (out->usecase == USECASE_AUDIO_PLAYBACK_DEEP_BUFFER ||
5804 out->usecase == USECASE_AUDIO_PLAYBACK_LOW_LATENCY)
Mingshu Pangf69a88d2021-04-30 16:14:39 +08005805 latency += platform_render_latency(out)/1000;
Alexy Josephaa54c872014-12-03 02:46:47 -08005806 }
5807
Zhou Songd2537a02020-06-11 22:04:46 +08005808 if (!out->standby && is_a2dp_out_device_type(&out->device_list))
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08005809 latency += audio_extn_a2dp_get_encoder_latency();
5810
Anish Kumar50ebcbf2014-12-09 04:01:39 +05305811 ALOGV("%s: Latency %d", __func__, latency);
Alexy Josephaa54c872014-12-03 02:46:47 -08005812 return latency;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005813}
5814
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05305815static float AmpToDb(float amplification)
5816{
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05305817 float db = DSD_VOLUME_MIN_DB;
5818 if (amplification > 0) {
5819 db = 20 * log10(amplification);
5820 if(db < DSD_VOLUME_MIN_DB)
5821 return DSD_VOLUME_MIN_DB;
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05305822 }
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05305823 return db;
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05305824}
5825
Krishna Kishor Jha10ad39f2022-02-21 17:02:47 +05305826#ifdef SOFT_VOLUME
5827static int out_set_soft_volume_params(struct audio_stream_out *stream)
5828{
5829 struct stream_out *out = (struct stream_out *)stream;
5830 int ret = 0;
5831 char mixer_ctl_name[128];
5832 struct audio_device *adev = out->dev;
5833 struct mixer_ctl *ctl = NULL;
5834 struct soft_step_volume_params *volume_params = NULL;
5835
5836 int pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
5837 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name), "Playback %d Soft Vol Params", pcm_device_id);
5838 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
5839 if (!ctl) {
5840 ALOGE("%s : Could not get ctl for mixer cmd - %s", __func__, mixer_ctl_name);
5841 return -EINVAL;
5842 }
5843
5844 volume_params =(struct soft_step_volume_params * ) malloc(sizeof(struct soft_step_volume_params));
5845 if (volume_params == NULL){
5846 ALOGE("%s : malloc is failed for volume params", __func__);
5847 return -EINVAL;
5848 } else {
5849 ret = platform_get_soft_step_volume_params(volume_params,out->usecase);
5850 if (ret < 0) {
5851 ALOGE("%s : platform_get_soft_step_volume_params is fialed", __func__);
Karan Naidu28b335a2022-05-18 23:00:08 +05305852 ret = -EINVAL;
5853 goto ERR_EXIT;
Krishna Kishor Jha10ad39f2022-02-21 17:02:47 +05305854 }
5855
5856 }
5857 ret = mixer_ctl_set_array(ctl, volume_params, sizeof(struct soft_step_volume_params)/sizeof(int));
5858 if (ret < 0) {
5859 ALOGE("%s: Could not set ctl, error:%d ", __func__, ret);
Karan Naidu28b335a2022-05-18 23:00:08 +05305860 ret = -EINVAL;
5861 goto ERR_EXIT;
5862 }
5863
5864 if (volume_params) {
5865 free(volume_params);
Krishna Kishor Jha10ad39f2022-02-21 17:02:47 +05305866 }
5867 return 0;
Karan Naidu28b335a2022-05-18 23:00:08 +05305868
5869ERR_EXIT:
5870 if (volume_params) {
5871 free(volume_params);
5872 }
5873 return ret;
Krishna Kishor Jha10ad39f2022-02-21 17:02:47 +05305874}
5875#endif
5876
Arun Mirpuri5d170872019-03-26 13:21:31 -07005877static int out_set_mmap_volume(struct audio_stream_out *stream, float left,
5878 float right)
5879{
5880 struct stream_out *out = (struct stream_out *)stream;
5881 long volume = 0;
5882 char mixer_ctl_name[128] = "";
5883 struct audio_device *adev = out->dev;
5884 struct mixer_ctl *ctl = NULL;
5885 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
5886 PCM_PLAYBACK);
5887
5888 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
5889 "Playback %d Volume", pcm_device_id);
5890 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
5891 if (!ctl) {
5892 ALOGE("%s: Could not get ctl for mixer cmd - %s",
5893 __func__, mixer_ctl_name);
5894 return -EINVAL;
5895 }
5896 if (left != right)
5897 ALOGW("%s: Left and right channel volume mismatch:%f,%f",
5898 __func__, left, right);
5899 volume = (long)(left * (MMAP_PLAYBACK_VOLUME_MAX*1.0));
5900 if (mixer_ctl_set_value(ctl, 0, volume) < 0){
5901 ALOGE("%s:ctl for mixer cmd - %s, volume %ld returned error",
5902 __func__, mixer_ctl_name, volume);
5903 return -EINVAL;
5904 }
5905 return 0;
5906}
5907
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05305908static int out_set_compr_volume(struct audio_stream_out *stream, float left,
5909 float right)
5910{
5911 struct stream_out *out = (struct stream_out *)stream;
Manish Dewangan338c50a2017-09-12 15:22:03 +05305912 long volume[2];
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05305913 char mixer_ctl_name[128];
5914 struct audio_device *adev = out->dev;
5915 struct mixer_ctl *ctl;
5916 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
5917 PCM_PLAYBACK);
5918
5919 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
5920 "Compress Playback %d Volume", pcm_device_id);
5921 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
5922 if (!ctl) {
5923 ALOGE("%s: Could not get ctl for mixer cmd - %s",
5924 __func__, mixer_ctl_name);
5925 return -EINVAL;
5926 }
5927 ALOGE("%s:ctl for mixer cmd - %s, left %f, right %f",
5928 __func__, mixer_ctl_name, left, right);
5929 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
5930 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
5931 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
5932
5933 return 0;
5934}
5935
Zhou Song2b8f28f2017-09-11 10:51:38 +08005936static int out_set_voip_volume(struct audio_stream_out *stream, float left,
5937 float right)
5938{
5939 struct stream_out *out = (struct stream_out *)stream;
5940 char mixer_ctl_name[] = "App Type Gain";
5941 struct audio_device *adev = out->dev;
5942 struct mixer_ctl *ctl;
Manish Dewangan338c50a2017-09-12 15:22:03 +05305943 long set_values[4];
Zhou Song2b8f28f2017-09-11 10:51:38 +08005944
Aniket Kumar Lata8426d1f2019-06-10 15:25:53 -07005945 if (!is_valid_volume(left, right)) {
5946 ALOGE("%s: Invalid stream volume for left=%f, right=%f",
5947 __func__, left, right);
5948 return -EINVAL;
5949 }
5950
Zhou Song2b8f28f2017-09-11 10:51:38 +08005951 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
5952 if (!ctl) {
5953 ALOGE("%s: Could not get ctl for mixer cmd - %s",
5954 __func__, mixer_ctl_name);
5955 return -EINVAL;
5956 }
5957
5958 set_values[0] = 0; //0: Rx Session 1:Tx Session
5959 set_values[1] = out->app_type_cfg.app_type;
Manish Dewangan338c50a2017-09-12 15:22:03 +05305960 set_values[2] = (long)(left * VOIP_PLAYBACK_VOLUME_MAX);
5961 set_values[3] = (long)(right * VOIP_PLAYBACK_VOLUME_MAX);
Zhou Song2b8f28f2017-09-11 10:51:38 +08005962
5963 mixer_ctl_set_array(ctl, set_values, ARRAY_SIZE(set_values));
5964 return 0;
5965}
5966
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05305967static int out_set_pcm_volume(struct audio_stream_out *stream, float left,
5968 float right)
5969{
5970 struct stream_out *out = (struct stream_out *)stream;
5971 /* Volume control for pcm playback */
5972 if (left != right) {
5973 return -EINVAL;
5974 } else {
5975 char mixer_ctl_name[128];
5976 struct audio_device *adev = out->dev;
5977 struct mixer_ctl *ctl;
5978 int pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
5979 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name), "Playback %d Volume", pcm_device_id);
5980 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
5981 if (!ctl) {
5982 ALOGE("%s : Could not get ctl for mixer cmd - %s", __func__, mixer_ctl_name);
5983 return -EINVAL;
5984 }
5985
5986 int volume = (int) (left * PCM_PLAYBACK_VOLUME_MAX);
5987 int ret = mixer_ctl_set_value(ctl, 0, volume);
5988 if (ret < 0) {
5989 ALOGE("%s: Could not set ctl, error:%d ", __func__, ret);
5990 return -EINVAL;
5991 }
5992
5993 ALOGV("%s : Pcm set volume value %d left %f", __func__, volume, left);
5994
5995 return 0;
5996 }
5997}
5998
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005999static int out_set_volume(struct audio_stream_out *stream, float left,
6000 float right)
6001{
Eric Laurenta9024de2013-04-04 09:19:12 -07006002 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006003 int volume[2];
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05306004 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006005
Arun Mirpuri5d170872019-03-26 13:21:31 -07006006 ALOGD("%s: called with left_vol=%f, right_vol=%f", __func__, left, right);
Eric Laurenta9024de2013-04-04 09:19:12 -07006007 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
6008 /* only take left channel into account: the API is for stereo anyway */
6009 out->muted = (left == 0.0f);
6010 return 0;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07006011 } else if (is_offload_usecase(out->usecase)) {
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05306012 if (audio_extn_passthru_is_passthrough_stream(out)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07006013 /*
6014 * Set mute or umute on HDMI passthrough stream.
6015 * Only take left channel into account.
6016 * Mute is 0 and unmute 1
6017 */
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05306018 audio_extn_passthru_set_volume(out, (left == 0.0f));
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05306019 } else if (out->format == AUDIO_FORMAT_DSD){
6020 char mixer_ctl_name[128] = "DSD Volume";
6021 struct audio_device *adev = out->dev;
6022 struct mixer_ctl *ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
6023
6024 if (!ctl) {
6025 ALOGE("%s: Could not get ctl for mixer cmd - %s",
6026 __func__, mixer_ctl_name);
6027 return -EINVAL;
6028 }
Manish Dewangan338c50a2017-09-12 15:22:03 +05306029 volume[0] = (long)(AmpToDb(left));
6030 volume[1] = (long)(AmpToDb(right));
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05306031 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
6032 return 0;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08006033 } else if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_BUS) &&
Derek Chendf05eea2019-08-01 13:57:49 -07006034 (out->car_audio_stream == CAR_AUDIO_STREAM_MEDIA)) {
Philippe Gravel1c9ac352019-05-28 16:08:37 -07006035 ALOGD("%s: Overriding offload set volume for media bus stream", __func__);
6036 struct listnode *node = NULL;
6037 list_for_each(node, &adev->active_outputs_list) {
6038 streams_output_ctxt_t *out_ctxt = node_to_item(node,
6039 streams_output_ctxt_t,
6040 list);
6041 if (out_ctxt->output->usecase == USECASE_AUDIO_PLAYBACK_MEDIA) {
6042 out->volume_l = out_ctxt->output->volume_l;
6043 out->volume_r = out_ctxt->output->volume_r;
6044 }
6045 }
Weiyin Jiang280ea742020-09-08 20:28:22 +08006046 pthread_mutex_lock(&out->latch_lock);
Zhou Songbaddf9f2020-11-20 13:57:39 +08006047 if (!out->a2dp_muted) {
Philippe Gravel1c9ac352019-05-28 16:08:37 -07006048 ret = out_set_compr_volume(&out->stream, out->volume_l, out->volume_r);
6049 }
Weiyin Jiang280ea742020-09-08 20:28:22 +08006050 pthread_mutex_unlock(&out->latch_lock);
Philippe Gravel1c9ac352019-05-28 16:08:37 -07006051 return ret;
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07006052 } else {
Weiyin Jiang280ea742020-09-08 20:28:22 +08006053 pthread_mutex_lock(&out->latch_lock);
Zhou Songbaddf9f2020-11-20 13:57:39 +08006054 ALOGV("%s: compress mute %d", __func__, out->a2dp_muted);
6055 if (!out->a2dp_muted)
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05306056 ret = out_set_compr_volume(stream, left, right);
6057 out->volume_l = left;
6058 out->volume_r = right;
Weiyin Jiang280ea742020-09-08 20:28:22 +08006059 pthread_mutex_unlock(&out->latch_lock);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05306060 return ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006061 }
Vikram Panduranga93f080e2017-06-07 18:16:14 -07006062 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP) {
Aalique Grahame22e49102018-12-18 14:23:57 -08006063 out->app_type_cfg.gain[0] = (int)(left * VOIP_PLAYBACK_VOLUME_MAX);
6064 out->app_type_cfg.gain[1] = (int)(right * VOIP_PLAYBACK_VOLUME_MAX);
Zhou Songbaddf9f2020-11-20 13:57:39 +08006065 pthread_mutex_lock(&out->latch_lock);
Aalique Grahame22e49102018-12-18 14:23:57 -08006066 if (!out->standby) {
6067 audio_extn_utils_send_app_type_gain(out->dev,
6068 out->app_type_cfg.app_type,
6069 &out->app_type_cfg.gain[0]);
Zhou Songbaddf9f2020-11-20 13:57:39 +08006070 if (!out->a2dp_muted)
6071 ret = out_set_voip_volume(stream, left, right);
Aalique Grahame22e49102018-12-18 14:23:57 -08006072 }
Zhou Song2b8f28f2017-09-11 10:51:38 +08006073 out->volume_l = left;
6074 out->volume_r = right;
Zhou Songbaddf9f2020-11-20 13:57:39 +08006075 pthread_mutex_unlock(&out->latch_lock);
Zhou Song2b8f28f2017-09-11 10:51:38 +08006076 return ret;
Arun Mirpuri5d170872019-03-26 13:21:31 -07006077 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
6078 ALOGV("%s: MMAP set volume called", __func__);
6079 if (!out->standby)
6080 ret = out_set_mmap_volume(stream, left, right);
6081 out->volume_l = left;
6082 out->volume_r = right;
6083 return ret;
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05306084 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_LOW_LATENCY ||
Ramu Gottipati36547092018-12-28 11:32:09 +05306085 out->usecase == USECASE_AUDIO_PLAYBACK_DEEP_BUFFER ||
6086 out->usecase == USECASE_AUDIO_PLAYBACK_ULL) {
Zhou Songbaddf9f2020-11-20 13:57:39 +08006087 pthread_mutex_lock(&out->latch_lock);
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05306088 /* Volume control for pcm playback */
Zhou Songbaddf9f2020-11-20 13:57:39 +08006089 if (!out->standby && !out->a2dp_muted)
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05306090 ret = out_set_pcm_volume(stream, left, right);
6091 else
6092 out->apply_volume = true;
6093
6094 out->volume_l = left;
6095 out->volume_r = right;
Zhou Songbaddf9f2020-11-20 13:57:39 +08006096 pthread_mutex_unlock(&out->latch_lock);
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05306097 return ret;
Derek Chenf13dd492018-11-13 14:53:51 -08006098 } else if (audio_extn_auto_hal_is_bus_device_usecase(out->usecase)) {
6099 ALOGV("%s: bus device set volume called", __func__);
Zhou Songbaddf9f2020-11-20 13:57:39 +08006100 pthread_mutex_lock(&out->latch_lock);
6101 if (!out->standby && !out->a2dp_muted)
Derek Chenf13dd492018-11-13 14:53:51 -08006102 ret = out_set_pcm_volume(stream, left, right);
6103 out->volume_l = left;
6104 out->volume_r = right;
Zhou Songbaddf9f2020-11-20 13:57:39 +08006105 pthread_mutex_unlock(&out->latch_lock);
Derek Chenf13dd492018-11-13 14:53:51 -08006106 return ret;
Eric Laurenta9024de2013-04-04 09:19:12 -07006107 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006108
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006109 return -ENOSYS;
6110}
6111
Zhou Songc9672822017-08-16 16:01:39 +08006112static void update_frames_written(struct stream_out *out, size_t bytes)
6113{
6114 size_t bpf = 0;
6115
6116 if (is_offload_usecase(out->usecase) && !out->non_blocking &&
6117 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD))
6118 bpf = 1;
6119 else if (!is_offload_usecase(out->usecase))
6120 bpf = audio_bytes_per_sample(out->format) *
6121 audio_channel_count_from_out_mask(out->channel_mask);
Zhou Song48453a02018-01-10 17:50:59 +08006122
6123 pthread_mutex_lock(&out->position_query_lock);
6124 if (bpf != 0) {
Zhou Songc9672822017-08-16 16:01:39 +08006125 out->written += bytes / bpf;
Zhou Song48453a02018-01-10 17:50:59 +08006126 clock_gettime(CLOCK_MONOTONIC, &out->writeAt);
6127 }
6128 pthread_mutex_unlock(&out->position_query_lock);
Zhou Songc9672822017-08-16 16:01:39 +08006129}
6130
Vignesh Kulothungana6927272019-02-20 15:17:07 -08006131int split_and_write_audio_haptic_data(struct stream_out *out,
6132 const void *buffer, size_t bytes_to_write)
6133{
6134 struct audio_device *adev = out->dev;
6135
6136 int ret = 0;
6137 size_t channel_count = audio_channel_count_from_out_mask(out->channel_mask);
6138 size_t bytes_per_sample = audio_bytes_per_sample(out->format);
6139 size_t frame_size = channel_count * bytes_per_sample;
6140 size_t frame_count = bytes_to_write / frame_size;
6141
6142 bool force_haptic_path =
6143 property_get_bool("vendor.audio.test_haptic", false);
6144
6145 // extract Haptics data from Audio buffer
6146 bool alloc_haptic_buffer = false;
6147 int haptic_channel_count = adev->haptics_config.channels;
6148 size_t haptic_frame_size = bytes_per_sample * haptic_channel_count;
6149 size_t audio_frame_size = frame_size - haptic_frame_size;
6150 size_t total_haptic_buffer_size = frame_count * haptic_frame_size;
6151
6152 if (adev->haptic_buffer == NULL) {
6153 alloc_haptic_buffer = true;
6154 } else if (adev->haptic_buffer_size < total_haptic_buffer_size) {
6155 free(adev->haptic_buffer);
6156 adev->haptic_buffer_size = 0;
6157 alloc_haptic_buffer = true;
6158 }
6159
6160 if (alloc_haptic_buffer) {
6161 adev->haptic_buffer = (uint8_t *)calloc(1, total_haptic_buffer_size);
Mingshu Pang1513f972019-05-24 12:43:51 +08006162 if(adev->haptic_buffer == NULL) {
6163 ALOGE("%s: failed to allocate mem for dev->haptic_buffer", __func__);
6164 return -ENOMEM;
6165 }
Vignesh Kulothungana6927272019-02-20 15:17:07 -08006166 adev->haptic_buffer_size = total_haptic_buffer_size;
6167 }
6168
6169 size_t src_index = 0, aud_index = 0, hap_index = 0;
6170 uint8_t *audio_buffer = (uint8_t *)buffer;
6171 uint8_t *haptic_buffer = adev->haptic_buffer;
6172
6173 // This is required for testing only. This works for stereo data only.
6174 // One channel is fed to audio stream and other to haptic stream for testing.
6175 if (force_haptic_path)
6176 audio_frame_size = haptic_frame_size = bytes_per_sample;
6177
6178 for (size_t i = 0; i < frame_count; i++) {
6179 memcpy(audio_buffer + aud_index, audio_buffer + src_index,
6180 audio_frame_size);
6181 aud_index += audio_frame_size;
6182 src_index += audio_frame_size;
6183
6184 if (adev->haptic_pcm)
6185 memcpy(haptic_buffer + hap_index, audio_buffer + src_index,
6186 haptic_frame_size);
6187 hap_index += haptic_frame_size;
6188 src_index += haptic_frame_size;
6189
6190 // This is required for testing only.
6191 // Discard haptic channel data.
6192 if (force_haptic_path)
6193 src_index += haptic_frame_size;
6194 }
6195
6196 // write to audio pipeline
6197 ret = pcm_write(out->pcm, (void *)audio_buffer,
6198 frame_count * audio_frame_size);
6199
6200 // write to haptics pipeline
6201 if (adev->haptic_pcm)
6202 ret = pcm_write(adev->haptic_pcm, (void *)adev->haptic_buffer,
6203 frame_count * haptic_frame_size);
6204
6205 return ret;
6206}
6207
Aalique Grahame22e49102018-12-18 14:23:57 -08006208#ifdef NO_AUDIO_OUT
6209static ssize_t out_write_for_no_output(struct audio_stream_out *stream,
6210 const void *buffer __unused, size_t bytes)
6211{
6212 struct stream_out *out = (struct stream_out *)stream;
6213
6214 /* No Output device supported other than BT for playback.
6215 * Sleep for the amount of buffer duration
6216 */
6217 lock_output_stream(out);
6218 usleep(bytes * 1000000 / audio_stream_out_frame_size(
6219 (const struct audio_stream_out *)&out->stream) /
6220 out_get_sample_rate(&out->stream.common));
6221 pthread_mutex_unlock(&out->lock);
6222 return bytes;
6223}
6224#endif
6225
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006226static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
6227 size_t bytes)
6228{
6229 struct stream_out *out = (struct stream_out *)stream;
6230 struct audio_device *adev = out->dev;
Eric Laurent6e895242013-09-05 16:10:57 -07006231 ssize_t ret = 0;
Satish Babu Patakokila715b1422017-08-22 14:33:21 +05306232 int channels = 0;
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006233 const size_t frame_size = audio_stream_out_frame_size(stream);
6234 const size_t frames = (frame_size != 0) ? bytes / frame_size : bytes;
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +05306235 struct audio_usecase *usecase = NULL;
Meng Wang4c32fb42020-01-16 17:57:11 +08006236 uint32_t compr_passthr = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006237
Haynes Mathew George380745d2017-10-04 15:27:45 -07006238 ATRACE_BEGIN("out_write");
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006239 lock_output_stream(out);
Naresh Tanniru4c630392014-05-12 01:05:52 +05306240
Shubhasini Sugumaran3fde6582022-01-19 15:54:50 -05006241 if (CARD_STATUS_OFFLINE == out->card_status ||
6242 POWER_POLICY_STATUS_OFFLINE == adev->out_power_policy) {
Zhou Song0b2e5dc2015-03-16 14:41:38 +08006243
Dhananjay Kumarac341582017-02-23 23:42:25 +05306244 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Ashish Jainbbce4322016-02-16 13:25:27 +05306245 /*during SSR for compress usecase we should return error to flinger*/
Naresh Tanniru80659832014-06-04 18:17:56 +05306246 ALOGD(" copl %s: sound card is not active/SSR state", __func__);
6247 pthread_mutex_unlock(&out->lock);
Haynes Mathew George380745d2017-10-04 15:27:45 -07006248 ATRACE_END();
Naresh Tanniru80659832014-06-04 18:17:56 +05306249 return -ENETRESET;
Ashish Jainbbce4322016-02-16 13:25:27 +05306250 } else {
Ashish Jainbbce4322016-02-16 13:25:27 +05306251 ALOGD(" %s: sound card is not active/SSR state", __func__);
6252 ret= -EIO;
6253 goto exit;
Naresh Tanniru4c630392014-05-12 01:05:52 +05306254 }
6255 }
6256
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05306257 if (audio_extn_passthru_should_drop_data(out)) {
Ashish Jaind84fd6a2016-07-27 12:33:25 +05306258 ALOGV(" %s : Drop data as compress passthrough session is going on", __func__);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05306259 ret = -EIO;
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05306260 goto exit;
6261 }
6262
Haynes Mathew George16081042017-05-31 17:16:49 -07006263 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
6264 ret = -EINVAL;
6265 goto exit;
6266 }
6267
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08006268 if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_AUX_DIGITAL) &&
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05306269 !out->is_iec61937_info_available) {
6270
6271 if (!audio_extn_passthru_is_passthrough_stream(out)) {
6272 out->is_iec61937_info_available = true;
6273 } else if (audio_extn_passthru_is_enabled()) {
6274 audio_extn_passthru_update_stream_configuration(adev, out, buffer, bytes);
Manish Dewangan37864bc2017-06-09 12:28:37 +05306275 out->is_iec61937_info_available = true;
Manish Dewangan671a4202017-08-18 17:30:46 +05306276
6277 if((out->format == AUDIO_FORMAT_DTS) ||
6278 (out->format == AUDIO_FORMAT_DTS_HD)) {
6279 ret = audio_extn_passthru_update_dts_stream_configuration(out,
6280 buffer, bytes);
6281 if (ret) {
6282 if (ret != -ENOSYS) {
6283 out->is_iec61937_info_available = false;
6284 ALOGD("iec61937 transmission info not yet updated retry");
6285 }
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05306286 } else if (!out->standby) {
Manish Dewangan671a4202017-08-18 17:30:46 +05306287 /* if stream has started and after that there is
6288 * stream config change (iec transmission config)
6289 * then trigger select_device to update backend configuration.
6290 */
6291 out->stream_config_changed = true;
6292 pthread_mutex_lock(&adev->lock);
6293 select_devices(adev, out->usecase);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05306294 if (!audio_extn_passthru_is_supported_backend_edid_cfg(adev, out)) {
Weiyin Jiang29c08a42019-04-30 17:11:10 +08006295 pthread_mutex_unlock(&adev->lock);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05306296 ret = -EINVAL;
6297 goto exit;
6298 }
Manish Dewangan671a4202017-08-18 17:30:46 +05306299 pthread_mutex_unlock(&adev->lock);
6300 out->stream_config_changed = false;
6301 out->is_iec61937_info_available = true;
6302 }
6303 }
Satish Babu Patakokila715b1422017-08-22 14:33:21 +05306304
Meng Wang4c32fb42020-01-16 17:57:11 +08006305#ifdef AUDIO_GKI_ENABLED
6306 /* out->compr_config.codec->reserved[0] is for compr_passthr */
6307 compr_passthr = out->compr_config.codec->reserved[0];
6308#else
6309 compr_passthr = out->compr_config.codec->compr_passthr;
6310#endif
6311
Garmond Leung317cbf12017-09-13 16:20:50 -07006312 if ((channels < (int)audio_channel_count_from_out_mask(out->channel_mask)) &&
Meng Wang4c32fb42020-01-16 17:57:11 +08006313 (compr_passthr == PASSTHROUGH) &&
Satish Babu Patakokila715b1422017-08-22 14:33:21 +05306314 (out->is_iec61937_info_available == true)) {
6315 ALOGE("%s: ERROR: Unsupported channel config in passthrough mode", __func__);
6316 ret = -EINVAL;
6317 goto exit;
6318 }
Manish Dewangan37864bc2017-06-09 12:28:37 +05306319 }
6320 }
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05306321
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08006322 if (is_a2dp_out_device_type(&out->device_list) &&
Florian Pfister1a84f312018-07-19 14:38:18 +02006323 (audio_extn_a2dp_source_is_suspended())) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08006324 if (!(compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_SPEAKER) ||
6325 compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_SPEAKER_SAFE))) {
Zhou Songd01e7a22020-09-23 22:49:01 +08006326 if (!is_offload_usecase(out->usecase)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05306327 ret = -EIO;
6328 goto exit;
6329 }
6330 }
6331 }
6332
Weiyin Jiangabedea32020-12-09 12:49:19 +08006333 if (is_usb_out_device_type(&out->device_list) &&
6334 !audio_extn_usb_connected(NULL)) {
6335 ret = -EIO;
6336 goto exit;
6337 }
6338
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006339 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07006340 out->standby = false;
Andy Hungc6bfd4a2019-07-01 18:26:00 -07006341 const int64_t startNs = systemTime(SYSTEM_TIME_MONOTONIC);
6342
Eric Laurent150dbfe2013-02-27 14:31:02 -08006343 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08006344 if (out->usecase == USECASE_COMPRESS_VOIP_CALL)
6345 ret = voice_extn_compress_voip_start_output_stream(out);
6346 else
6347 ret = start_output_stream(out);
Daniel Hillenbrand17873952013-05-23 10:10:00 +05306348
6349 if (ret == 0)
6350 amplifier_output_stream_start(stream,
6351 is_offload_usecase(out->usecase));
6352
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006353 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006354 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07006355 out->standby = true;
Weiyin Jiangac2bae82020-07-29 17:23:53 +08006356 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006357 goto exit;
6358 }
Ashish Jain1b9b30c2017-05-18 20:57:40 +05306359 out->started = 1;
Andy Hunga1f48fa2019-07-01 18:14:53 -07006360 out->last_fifo_valid = false; // we're coming out of standby, last_fifo isn't valid.
Weiyin Jiangac2bae82020-07-29 17:23:53 +08006361
6362 if ((last_known_cal_step != -1) && (adev->platform != NULL)) {
vivek mehtab72d08d2016-04-29 03:16:47 -07006363 ALOGD("%s: retry previous failed cal level set", __func__);
Weiyin Jiangac2bae82020-07-29 17:23:53 +08006364 platform_send_gain_dep_cal(adev->platform, last_known_cal_step);
Preetam Singh Ranawatf4ae0222017-05-31 17:07:28 +05306365 last_known_cal_step = -1;
vivek mehtab72d08d2016-04-29 03:16:47 -07006366 }
Weiyin Jiangac2bae82020-07-29 17:23:53 +08006367 pthread_mutex_unlock(&adev->lock);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05306368
6369 if ((out->is_iec61937_info_available == true) &&
6370 (audio_extn_passthru_is_passthrough_stream(out))&&
6371 (!audio_extn_passthru_is_supported_backend_edid_cfg(adev, out))) {
6372 ret = -EINVAL;
6373 goto exit;
6374 }
Surendar Karkaf51b5842018-04-26 11:28:38 +05306375 if (out->set_dual_mono)
6376 audio_extn_send_dual_mono_mixing_coefficients(out);
Andy Hungc6bfd4a2019-07-01 18:26:00 -07006377
Dechen Chai22768452021-07-30 09:29:16 +05306378#ifndef LINUX_ENABLED
Andy Hungc6bfd4a2019-07-01 18:26:00 -07006379 // log startup time in ms.
6380 simple_stats_log(
6381 &out->start_latency_ms, (systemTime(SYSTEM_TIME_MONOTONIC) - startNs) * 1e-6);
Dechen Chai22768452021-07-30 09:29:16 +05306382#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006383 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006384
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08006385 if (adev->is_channel_status_set == false &&
6386 compare_device_type(&out->device_list,
6387 AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
Alexy Josephb1379942016-01-29 15:49:38 -08006388 audio_utils_set_hdmi_channel_status(out, (void *)buffer, bytes);
Ashish Jain81eb2a82015-05-13 10:52:34 +05306389 adev->is_channel_status_set = true;
6390 }
6391
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +05306392 if ((adev->use_old_pspd_mix_ctrl == true) &&
6393 (out->pspd_coeff_sent == false)) {
6394 /*
6395 * Need to resend pspd coefficients after stream started for
6396 * older kernel version as it does not save the coefficients
6397 * and also stream has to be started for coeff to apply.
6398 */
6399 usecase = get_usecase_from_list(adev, out->usecase);
6400 if (usecase != NULL) {
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05306401 audio_extn_set_custom_mtmx_params_v2(adev, usecase, true);
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +05306402 out->pspd_coeff_sent = true;
6403 }
6404 }
6405
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07006406 if (is_offload_usecase(out->usecase)) {
Alexy Joseph01e54e62015-03-03 19:01:03 -08006407 ALOGVV("copl(%p): writing buffer (%zu bytes) to compress device", out, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07006408 if (out->send_new_metadata) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07006409 ALOGD("copl(%p):send new gapless metadata", out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07006410 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
6411 out->send_new_metadata = 0;
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05306412 if (out->send_next_track_params && out->is_compr_metadata_avail) {
6413 ALOGD("copl(%p):send next track params in gapless", out);
Weiyin Jiangd9b5a4e2019-07-18 17:24:21 +08006414 compress_set_next_track_param(out->compr, &(out->compr_config.codec->options));
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05306415 out->send_next_track_params = false;
6416 out->is_compr_metadata_avail = false;
6417 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07006418 }
Dhananjay Kumarac341582017-02-23 23:42:25 +05306419 if (!(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
Ashish Jain83a6cc22016-06-28 14:34:17 +05306420 (out->convert_buffer) != NULL) {
Haynes Mathew George352f27b2013-07-26 00:00:15 -07006421
Ashish Jain83a6cc22016-06-28 14:34:17 +05306422 if ((bytes > out->hal_fragment_size)) {
Ashish Jainf1eaa582016-05-23 20:54:24 +05306423 ALOGW("Error written bytes %zu > %d (fragment_size)",
Ashish Jain83a6cc22016-06-28 14:34:17 +05306424 bytes, out->hal_fragment_size);
Ashish Jainf1eaa582016-05-23 20:54:24 +05306425 pthread_mutex_unlock(&out->lock);
Haynes Mathew George380745d2017-10-04 15:27:45 -07006426 ATRACE_END();
Ashish Jainf1eaa582016-05-23 20:54:24 +05306427 return -EINVAL;
6428 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05306429 audio_format_t dst_format = out->hal_op_format;
6430 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05306431
Dieter Luecking5d57def2018-09-07 14:23:37 +02006432 /* prevent division-by-zero */
6433 uint32_t bitwidth_src = format_to_bitwidth_table[src_format];
6434 uint32_t bitwidth_dst = format_to_bitwidth_table[dst_format];
6435 if ((bitwidth_src == 0) || (bitwidth_dst == 0)) {
6436 ALOGE("%s: Error bitwidth == 0", __func__);
Ramu Gottipati02809682018-12-19 16:46:12 +05306437 pthread_mutex_unlock(&out->lock);
Dieter Luecking5d57def2018-09-07 14:23:37 +02006438 ATRACE_END();
6439 return -EINVAL;
6440 }
6441
Ashish Jainf1eaa582016-05-23 20:54:24 +05306442 uint32_t frames = bytes / format_to_bitwidth_table[src_format];
6443 uint32_t bytes_to_write = frames * format_to_bitwidth_table[dst_format];
6444
Ashish Jain83a6cc22016-06-28 14:34:17 +05306445 memcpy_by_audio_format(out->convert_buffer,
Ashish Jainf1eaa582016-05-23 20:54:24 +05306446 dst_format,
6447 buffer,
6448 src_format,
6449 frames);
6450
Ashish Jain83a6cc22016-06-28 14:34:17 +05306451 ret = compress_write(out->compr, out->convert_buffer,
Ashish Jainf1eaa582016-05-23 20:54:24 +05306452 bytes_to_write);
6453
6454 /*Convert written bytes in audio flinger format*/
6455 if (ret > 0)
6456 ret = ((ret * format_to_bitwidth_table[out->format]) /
6457 format_to_bitwidth_table[dst_format]);
6458 }
6459 } else
6460 ret = compress_write(out->compr, buffer, bytes);
6461
Zhou Songc9672822017-08-16 16:01:39 +08006462 if ((ret < 0 || ret == (ssize_t)bytes) && !out->non_blocking)
6463 update_frames_written(out, bytes);
6464
Dhanalakshmi Siddani37ca1d62014-08-20 12:28:34 +05306465 if (ret < 0)
6466 ret = -errno;
Weiyin Jiangcc60dbb2018-08-21 13:12:03 +08006467 ALOGVV("%s: writing buffer (%zu bytes) to compress device returned %d", __func__, bytes, (int)ret);
Ashish Jainb26edfb2016-08-25 00:10:11 +05306468 /*msg to cb thread only if non blocking write is enabled*/
6469 if (ret >= 0 && ret < (ssize_t)bytes && out->non_blocking) {
Sidipotu Ashok55820562014-02-10 16:16:38 +05306470 ALOGD("No space available in compress driver, post msg to cb thread");
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006471 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
Naresh Tanniru80659832014-06-04 18:17:56 +05306472 } else if (-ENETRESET == ret) {
6473 ALOGE("copl %s: received sound card offline state on compress write", __func__);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306474 out->card_status = CARD_STATUS_OFFLINE;
Naresh Tanniru80659832014-06-04 18:17:56 +05306475 pthread_mutex_unlock(&out->lock);
Dhananjay Kumar1248dd82017-07-28 21:22:16 +05306476 out_on_error(&out->stream.common);
Haynes Mathew George380745d2017-10-04 15:27:45 -07006477 ATRACE_END();
Naresh Tanniru80659832014-06-04 18:17:56 +05306478 return ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006479 }
Ashish Jain5106d362016-05-11 19:23:33 +05306480
Dhanalakshmi Siddania6b76c72016-09-09 18:10:31 +05306481 /* Call compr start only when non-zero bytes of data is there to be rendered */
6482 if (!out->playback_started && ret > 0) {
6483 int status = compress_start(out->compr);
6484 if (status < 0) {
6485 ret = status;
6486 ALOGE("%s: compr start failed with err %d", __func__, errno);
6487 goto exit;
6488 }
Alexy Joseph7de344d2015-03-30 10:40:03 -07006489 audio_extn_dts_eagle_fade(adev, true, out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006490 out->playback_started = 1;
Weiyin Jiang280ea742020-09-08 20:28:22 +08006491 pthread_mutex_lock(&out->latch_lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006492 out->offload_state = OFFLOAD_STATE_PLAYING;
Weiyin Jiang280ea742020-09-08 20:28:22 +08006493 pthread_mutex_unlock(&out->latch_lock);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08006494
6495 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
6496 popcount(out->channel_mask),
6497 out->playback_started);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006498 }
6499 pthread_mutex_unlock(&out->lock);
Haynes Mathew George380745d2017-10-04 15:27:45 -07006500 ATRACE_END();
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006501 return ret;
6502 } else {
6503 if (out->pcm) {
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006504 size_t bytes_to_write = bytes;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006505 if (out->muted)
6506 memset((void *)buffer, 0, bytes);
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006507 ALOGV("%s: frames=%zu, frame_size=%zu, bytes_to_write=%zu",
6508 __func__, frames, frame_size, bytes_to_write);
6509
Aalique Grahame22e49102018-12-18 14:23:57 -08006510 if (out->usecase == USECASE_INCALL_MUSIC_UPLINK ||
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07006511 out->usecase == USECASE_INCALL_MUSIC_UPLINK2 ||
6512 (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP &&
6513 !audio_extn_utils_is_vendor_enhanced_fwk())) {
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006514 size_t channel_count = audio_channel_count_from_out_mask(out->channel_mask);
6515 int16_t *src = (int16_t *)buffer;
6516 int16_t *dst = (int16_t *)buffer;
6517
Yunfei Zhanga6cd66d2019-07-22 16:15:36 +08006518 LOG_ALWAYS_FATAL_IF(channel_count > 2 ||
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006519 out->format != AUDIO_FORMAT_PCM_16_BIT,
Aalique Grahame22e49102018-12-18 14:23:57 -08006520 "out_write called for %s use case with wrong properties",
6521 use_case_table[out->usecase]);
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006522
6523 /*
6524 * FIXME: this can be removed once audio flinger mixer supports
6525 * mono output
6526 */
6527
6528 /*
6529 * Code below goes over each frame in the buffer and adds both
6530 * L and R samples and then divides by 2 to convert to mono
6531 */
Yunfei Zhanga6cd66d2019-07-22 16:15:36 +08006532 if (channel_count == 2) {
6533 for (size_t i = 0; i < frames ; i++, dst++, src += 2) {
6534 *dst = (int16_t)(((int32_t)src[0] + (int32_t)src[1]) >> 1);
6535 }
6536 bytes_to_write /= 2;
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006537 }
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006538 }
Andy Hunga1f48fa2019-07-01 18:14:53 -07006539
6540 // Note: since out_get_presentation_position() is called alternating with out_write()
6541 // by AudioFlinger, we can check underruns using the prior timestamp read.
6542 // (Alternately we could check if the buffer is empty using pcm_get_htimestamp().
6543 if (out->last_fifo_valid) {
6544 // compute drain to see if there is an underrun.
6545 const int64_t current_ns = systemTime(SYSTEM_TIME_MONOTONIC); // sys call
Dhananjay Kumara4429632020-07-11 02:53:37 +05306546 int64_t time_diff_ns = current_ns - out->last_fifo_time_ns;
6547 int64_t frames_by_time =
6548 ((time_diff_ns > 0) && (time_diff_ns < (INT64_MAX / out->config.rate))) ?
6549 (time_diff_ns * out->config.rate / NANOS_PER_SECOND) : 0;
Andy Hunga1f48fa2019-07-01 18:14:53 -07006550 const int64_t underrun = frames_by_time - out->last_fifo_frames_remaining;
6551
6552 if (underrun > 0) {
Dechen Chai22768452021-07-30 09:29:16 +05306553#ifndef LINUX_ENABLED
Andy Hunga1f48fa2019-07-01 18:14:53 -07006554 simple_stats_log(&out->fifo_underruns, underrun);
Dechen Chai22768452021-07-30 09:29:16 +05306555#endif
Andy Hunga1f48fa2019-07-01 18:14:53 -07006556
6557 ALOGW("%s: underrun(%lld) "
6558 "frames_by_time(%lld) > out->last_fifo_frames_remaining(%lld)",
6559 __func__,
6560 (long long)out->fifo_underruns.n,
6561 (long long)frames_by_time,
6562 (long long)out->last_fifo_frames_remaining);
6563 }
6564 out->last_fifo_valid = false; // we're writing below, mark fifo info as stale.
6565 }
6566
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05306567 ALOGVV("%s: writing buffer (%zu bytes) to pcm device", __func__, bytes);
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07006568
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006569 long ns = 0;
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07006570
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006571 if (out->config.rate)
6572 ns = pcm_bytes_to_frames(out->pcm, bytes)*1000000000LL/
6573 out->config.rate;
6574
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006575 request_out_focus(out, ns);
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006576 bool use_mmap = is_mmap_usecase(out->usecase) || out->realtime;
6577
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006578 if (use_mmap)
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006579 ret = pcm_mmap_write(out->pcm, (void *)buffer, bytes_to_write);
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006580 else if (out->hal_op_format != out->hal_ip_format &&
Ashish Jain83a6cc22016-06-28 14:34:17 +05306581 out->convert_buffer != NULL) {
6582
6583 memcpy_by_audio_format(out->convert_buffer,
6584 out->hal_op_format,
6585 buffer,
6586 out->hal_ip_format,
6587 out->config.period_size * out->config.channels);
6588
6589 ret = pcm_write(out->pcm, out->convert_buffer,
6590 (out->config.period_size *
6591 out->config.channels *
6592 format_to_bitwidth_table[out->hal_op_format]));
6593 } else {
Aditya Bavanarid4db8ee2017-05-29 21:08:03 +05306594 /*
6595 * To avoid underrun in DSP when the application is not pumping
6596 * data at required rate, check for the no. of bytes and ignore
6597 * pcm_write if it is less than actual buffer size.
6598 * It is a work around to a change in compress VOIP driver.
6599 */
6600 if ((out->flags & AUDIO_OUTPUT_FLAG_VOIP_RX) &&
6601 bytes < (out->config.period_size * out->config.channels *
6602 audio_bytes_per_sample(out->format))) {
6603 size_t voip_buf_size =
6604 out->config.period_size * out->config.channels *
6605 audio_bytes_per_sample(out->format);
6606 ALOGE("%s:VOIP underrun: bytes received %zu, required:%zu\n",
6607 __func__, bytes, voip_buf_size);
6608 usleep(((uint64_t)voip_buf_size - bytes) *
6609 1000000 / audio_stream_out_frame_size(stream) /
6610 out_get_sample_rate(&out->stream.common));
6611 ret = 0;
Vignesh Kulothungana6927272019-02-20 15:17:07 -08006612 } else {
6613 if (out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS)
6614 ret = split_and_write_audio_haptic_data(out, buffer, bytes);
6615 else
6616 ret = pcm_write(out->pcm, (void *)buffer, bytes_to_write);
6617 }
Ashish Jain83a6cc22016-06-28 14:34:17 +05306618 }
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07006619
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006620 release_out_focus(out);
6621
Dhanalakshmi Siddani37ca1d62014-08-20 12:28:34 +05306622 if (ret < 0)
6623 ret = -errno;
Zhou Songc9672822017-08-16 16:01:39 +08006624 else if (ret > 0)
Ashish Jain83a6cc22016-06-28 14:34:17 +05306625 ret = -EINVAL;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006626 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006627 }
6628
6629exit:
Zhou Songc9672822017-08-16 16:01:39 +08006630 update_frames_written(out, bytes);
Naresh Tanniru4c630392014-05-12 01:05:52 +05306631 if (-ENETRESET == ret) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306632 out->card_status = CARD_STATUS_OFFLINE;
Naresh Tanniru4c630392014-05-12 01:05:52 +05306633 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006634 pthread_mutex_unlock(&out->lock);
6635
6636 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07006637 if (out->pcm)
Alexy Josephb1379942016-01-29 15:49:38 -08006638 ALOGE("%s: error %d, %s", __func__, (int)ret, pcm_get_error(out->pcm));
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05306639 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05306640 pthread_mutex_lock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05306641 voice_extn_compress_voip_close_output_stream(&out->stream.common);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05306642 out->started = 0;
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05306643 pthread_mutex_unlock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05306644 out->standby = true;
6645 }
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306646 out_on_error(&out->stream.common);
Dieter Luecking5d57def2018-09-07 14:23:37 +02006647 if (!(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
6648 /* prevent division-by-zero */
6649 uint32_t stream_size = audio_stream_out_frame_size(stream);
6650 uint32_t srate = out_get_sample_rate(&out->stream.common);
Vidyakumar Athotaa9d3a5f2017-08-09 12:13:05 -07006651
Dieter Luecking5d57def2018-09-07 14:23:37 +02006652 if ((stream_size == 0) || (srate == 0)) {
6653 ALOGE("%s: stream_size= %d, srate = %d", __func__, stream_size, srate);
6654 ATRACE_END();
6655 return -EINVAL;
6656 }
6657 usleep((uint64_t)bytes * 1000000 / stream_size / srate);
6658 }
Vidyakumar Athotaa9d3a5f2017-08-09 12:13:05 -07006659 if (audio_extn_passthru_is_passthrough_stream(out)) {
Rajshekar Eashwarappa88834522018-04-02 17:20:15 +05306660 //ALOGE("%s: write error, ret = %zd", __func__, ret);
Haynes Mathew George380745d2017-10-04 15:27:45 -07006661 ATRACE_END();
Vidyakumar Athotaa9d3a5f2017-08-09 12:13:05 -07006662 return ret;
6663 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006664 }
Haynes Mathew George380745d2017-10-04 15:27:45 -07006665 ATRACE_END();
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006666 return bytes;
6667}
6668
6669static int out_get_render_position(const struct audio_stream_out *stream,
6670 uint32_t *dsp_frames)
6671{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006672 struct stream_out *out = (struct stream_out *)stream;
Zhou Song32a556e2015-05-05 10:46:56 +08006673
6674 if (dsp_frames == NULL)
6675 return -EINVAL;
6676
6677 *dsp_frames = 0;
6678 if (is_offload_usecase(out->usecase)) {
Mingming Yin9e348b52014-11-19 16:18:55 -08006679 ssize_t ret = 0;
Ashish Jain5106d362016-05-11 19:23:33 +05306680
6681 /* Below piece of code is not guarded against any lock beacuse audioFliner serializes
6682 * this operation and adev_close_output_stream(where out gets reset).
6683 */
Dhananjay Kumarac341582017-02-23 23:42:25 +05306684 if (!out->non_blocking && !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Zhou Song48453a02018-01-10 17:50:59 +08006685 *dsp_frames = get_actual_pcm_frames_rendered(out, NULL);
Ashish Jain5106d362016-05-11 19:23:33 +05306686 ALOGVV("dsp_frames %d sampleRate %d",(int)*dsp_frames,out->sample_rate);
Haynes Mathew Georgeb0f5dc32017-10-06 18:35:12 -07006687 adjust_frames_for_device_delay(out, dsp_frames);
Ashish Jain5106d362016-05-11 19:23:33 +05306688 return 0;
6689 }
6690
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006691 lock_output_stream(out);
Ashish Jain5106d362016-05-11 19:23:33 +05306692 if (out->compr != NULL && out->non_blocking) {
Naresh Tanniru80659832014-06-04 18:17:56 +05306693 ret = compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006694 &out->sample_rate);
Dhanalakshmi Siddani37ca1d62014-08-20 12:28:34 +05306695 if (ret < 0)
6696 ret = -errno;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006697 ALOGVV("%s rendered frames %d sample_rate %d",
Ashish Jain5106d362016-05-11 19:23:33 +05306698 __func__, *dsp_frames, out->sample_rate);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006699 }
Naresh Tanniru80659832014-06-04 18:17:56 +05306700 if (-ENETRESET == ret) {
6701 ALOGE(" ERROR: sound card not active Unable to get time stamp from compress driver");
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306702 out->card_status = CARD_STATUS_OFFLINE;
6703 ret = -EINVAL;
Naresh Tanniru80659832014-06-04 18:17:56 +05306704 } else if(ret < 0) {
6705 ALOGE(" ERROR: Unable to get time stamp from compress driver");
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306706 ret = -EINVAL;
Shubhasini Sugumaran3fde6582022-01-19 15:54:50 -05006707 } else if (out->card_status == CARD_STATUS_OFFLINE ||
6708 adev->out_power_policy == POWER_POLICY_STATUS_OFFLINE) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05306709 /*
6710 * Handle corner case where compress session is closed during SSR
6711 * and timestamp is queried
6712 */
6713 ALOGE(" ERROR: sound card not active, return error");
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306714 ret = -EINVAL;
Manisha Agarwal7b3e3772019-02-20 14:33:45 +05306715 } else if (out->prev_card_status_offline) {
6716 ALOGE("ERROR: previously sound card was offline,return error");
6717 ret = -EINVAL;
Naresh Tanniru80659832014-06-04 18:17:56 +05306718 } else {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306719 ret = 0;
Haynes Mathew Georgeb0f5dc32017-10-06 18:35:12 -07006720 adjust_frames_for_device_delay(out, dsp_frames);
Naresh Tanniru80659832014-06-04 18:17:56 +05306721 }
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306722 pthread_mutex_unlock(&out->lock);
6723 return ret;
Zhou Song32a556e2015-05-05 10:46:56 +08006724 } else if (audio_is_linear_pcm(out->format)) {
6725 *dsp_frames = out->written;
Haynes Mathew Georgeb0f5dc32017-10-06 18:35:12 -07006726 adjust_frames_for_device_delay(out, dsp_frames);
Zhou Song32a556e2015-05-05 10:46:56 +08006727 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006728 } else
6729 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006730}
6731
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07006732static int out_add_audio_effect(const struct audio_stream *stream __unused,
6733 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006734{
6735 return 0;
6736}
6737
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07006738static int out_remove_audio_effect(const struct audio_stream *stream __unused,
6739 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006740{
6741 return 0;
6742}
6743
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07006744static int out_get_next_write_timestamp(const struct audio_stream_out *stream __unused,
6745 int64_t *timestamp __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006746{
Satya Krishna Pindiprolib6655542017-07-03 19:38:19 +05306747 return -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006748}
6749
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07006750static int out_get_presentation_position(const struct audio_stream_out *stream,
6751 uint64_t *frames, struct timespec *timestamp)
6752{
6753 struct stream_out *out = (struct stream_out *)stream;
pavance65c2fe2017-10-18 17:52:01 +05306754 int ret = -ENODATA;
Eric Laurent949a0892013-09-20 09:20:13 -07006755 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07006756
Ashish Jain5106d362016-05-11 19:23:33 +05306757 /* below piece of code is not guarded against any lock because audioFliner serializes
6758 * this operation and adev_close_output_stream( where out gets reset).
6759 */
6760 if (is_offload_usecase(out->usecase) && !out->non_blocking &&
Dhananjay Kumarac341582017-02-23 23:42:25 +05306761 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Zhou Song48453a02018-01-10 17:50:59 +08006762 *frames = get_actual_pcm_frames_rendered(out, timestamp);
Ashish Jain5106d362016-05-11 19:23:33 +05306763 ALOGVV("frames %lld playedat %lld",(long long int)*frames,
6764 timestamp->tv_sec * 1000000LL + timestamp->tv_nsec / 1000);
6765 return 0;
6766 }
6767
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006768 lock_output_stream(out);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07006769
Ashish Jain5106d362016-05-11 19:23:33 +05306770 if (is_offload_usecase(out->usecase) && out->compr != NULL && out->non_blocking) {
6771 ret = compress_get_tstamp(out->compr, &dsp_frames,
6772 &out->sample_rate);
yidongh0515e042017-07-06 15:00:34 +08006773 // Adjustment accounts for A2dp encoder latency with offload usecases
6774 // Note: Encoder latency is returned in ms.
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08006775 if (is_a2dp_out_device_type(&out->device_list)) {
yidongh0515e042017-07-06 15:00:34 +08006776 unsigned long offset =
6777 (audio_extn_a2dp_get_encoder_latency() * out->sample_rate / 1000);
6778 dsp_frames = (dsp_frames > offset) ? (dsp_frames - offset) : 0;
6779 }
Ashish Jain5106d362016-05-11 19:23:33 +05306780 ALOGVV("%s rendered frames %ld sample_rate %d",
6781 __func__, dsp_frames, out->sample_rate);
6782 *frames = dsp_frames;
6783 if (ret < 0)
6784 ret = -errno;
6785 if (-ENETRESET == ret) {
6786 ALOGE(" ERROR: sound card not active Unable to get time stamp from compress driver");
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306787 out->card_status = CARD_STATUS_OFFLINE;
Ashish Jain5106d362016-05-11 19:23:33 +05306788 ret = -EINVAL;
6789 } else
6790 ret = 0;
6791 /* this is the best we can do */
6792 clock_gettime(CLOCK_MONOTONIC, timestamp);
Eric Laurent949a0892013-09-20 09:20:13 -07006793 } else {
6794 if (out->pcm) {
Weiyin Jiangd4633762018-03-16 12:05:03 +08006795 unsigned int avail;
6796 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
George Gao62ebc722019-07-29 16:29:44 -07006797 uint64_t signed_frames = 0;
Weiyin Jiang0d25bcf2019-08-30 15:27:23 +08006798 uint64_t frames_temp = 0;
George Gao62ebc722019-07-29 16:29:44 -07006799
Andy Hunga1f48fa2019-07-01 18:14:53 -07006800 if (out->kernel_buffer_size > avail) {
6801 frames_temp = out->last_fifo_frames_remaining = out->kernel_buffer_size - avail;
6802 } else {
6803 ALOGW("%s: avail:%u > kernel_buffer_size:%zu clamping!",
6804 __func__, avail, out->kernel_buffer_size);
6805 avail = out->kernel_buffer_size;
6806 frames_temp = out->last_fifo_frames_remaining = 0;
6807 }
6808 out->last_fifo_valid = true;
6809 out->last_fifo_time_ns = audio_utils_ns_from_timespec(timestamp);
6810
Weiyin Jiang0d25bcf2019-08-30 15:27:23 +08006811 if (out->written >= frames_temp)
6812 signed_frames = out->written - frames_temp;
George Gao62ebc722019-07-29 16:29:44 -07006813
Andy Hunga1f48fa2019-07-01 18:14:53 -07006814 ALOGVV("%s: frames:%lld avail:%u kernel_buffer_size:%zu",
6815 __func__, (long long)signed_frames, avail, out->kernel_buffer_size);
6816
Weiyin Jiangd4633762018-03-16 12:05:03 +08006817 // This adjustment accounts for buffering after app processor.
6818 // It is based on estimated DSP latency per use case, rather than exact.
George Gao9ba8a142020-07-23 14:30:03 -07006819 frames_temp = platform_render_latency(out) *
Robert Lee58215542019-07-15 20:55:12 +08006820 out->sample_rate / 1000000LL;
Weiyin Jiang0d25bcf2019-08-30 15:27:23 +08006821 if (signed_frames >= frames_temp)
6822 signed_frames -= frames_temp;
Aniket Kumar Lataff613152017-07-18 18:19:21 -07006823
Weiyin Jiangd4633762018-03-16 12:05:03 +08006824 // Adjustment accounts for A2dp encoder latency with non offload usecases
6825 // Note: Encoder latency is returned in ms, while platform_render_latency in us.
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08006826 if (is_a2dp_out_device_type(&out->device_list)) {
Weiyin Jiang0d25bcf2019-08-30 15:27:23 +08006827 frames_temp = audio_extn_a2dp_get_encoder_latency() * out->sample_rate / 1000;
6828 if (signed_frames >= frames_temp)
6829 signed_frames -= frames_temp;
Weiyin Jiangd4633762018-03-16 12:05:03 +08006830 }
6831
6832 // It would be unusual for this value to be negative, but check just in case ...
George Gao62ebc722019-07-29 16:29:44 -07006833 *frames = signed_frames;
6834 ret = 0;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07006835 }
Eric Laurenta7a33042019-07-10 16:20:22 -07006836 } else if (out->card_status == CARD_STATUS_OFFLINE ||
Shubhasini Sugumaran3fde6582022-01-19 15:54:50 -05006837 adev->out_power_policy == POWER_POLICY_STATUS_OFFLINE ||
Eric Laurenta7a33042019-07-10 16:20:22 -07006838 // audioflinger still needs position updates when A2DP is suspended
Jasmine Cha5c2517f2019-09-09 11:07:28 +08006839 (is_a2dp_out_device_type(&out->device_list) && audio_extn_a2dp_source_is_suspended())) {
Ashish Jainbbce4322016-02-16 13:25:27 +05306840 *frames = out->written;
6841 clock_gettime(CLOCK_MONOTONIC, timestamp);
Manisha Agarwal7b3e3772019-02-20 14:33:45 +05306842 if (is_offload_usecase(out->usecase))
6843 ret = -EINVAL;
6844 else
6845 ret = 0;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07006846 }
6847 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07006848 pthread_mutex_unlock(&out->lock);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07006849 return ret;
6850}
6851
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006852static int out_set_callback(struct audio_stream_out *stream,
6853 stream_callback_t callback, void *cookie)
6854{
6855 struct stream_out *out = (struct stream_out *)stream;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08006856 int ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006857
6858 ALOGV("%s", __func__);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006859 lock_output_stream(out);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08006860 out->client_callback = callback;
6861 out->client_cookie = cookie;
6862 if (out->adsp_hdlr_stream_handle) {
6863 ret = audio_extn_adsp_hdlr_stream_set_callback(
6864 out->adsp_hdlr_stream_handle,
6865 callback,
6866 cookie);
6867 if (ret)
6868 ALOGW("%s:adsp hdlr callback registration failed %d",
6869 __func__, ret);
6870 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006871 pthread_mutex_unlock(&out->lock);
6872 return 0;
6873}
6874
6875static int out_pause(struct audio_stream_out* stream)
6876{
6877 struct stream_out *out = (struct stream_out *)stream;
6878 int status = -ENOSYS;
6879 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07006880 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07006881 ALOGD("copl(%p):pause compress driver", out);
Sujin Panicker7c7b6f92020-04-03 12:57:55 +05306882 status = -ENODATA;
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006883 lock_output_stream(out);
Weiyin Jiang280ea742020-09-08 20:28:22 +08006884 pthread_mutex_lock(&out->latch_lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006885 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306886 if (out->card_status != CARD_STATUS_OFFLINE)
Naresh Tanniru80659832014-06-04 18:17:56 +05306887 status = compress_pause(out->compr);
6888
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006889 out->offload_state = OFFLOAD_STATE_PAUSED;
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08006890
Mingming Yin21854652016-04-13 11:54:02 -07006891 if (audio_extn_passthru_is_active()) {
6892 ALOGV("offload use case, pause passthru");
6893 audio_extn_passthru_on_pause(out);
6894 }
6895
Dhanalakshmi Siddani79415e72015-03-23 11:54:47 +05306896 audio_extn_dts_eagle_fade(adev, false, out);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08006897 audio_extn_dts_notify_playback_state(out->usecase, 0,
6898 out->sample_rate, popcount(out->channel_mask),
6899 0);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006900 }
Weiyin Jiang280ea742020-09-08 20:28:22 +08006901 pthread_mutex_unlock(&out->latch_lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006902 pthread_mutex_unlock(&out->lock);
6903 }
6904 return status;
6905}
6906
6907static int out_resume(struct audio_stream_out* stream)
6908{
6909 struct stream_out *out = (struct stream_out *)stream;
6910 int status = -ENOSYS;
6911 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07006912 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07006913 ALOGD("copl(%p):resume compress driver", out);
Sujin Panicker7c7b6f92020-04-03 12:57:55 +05306914 status = -ENODATA;
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006915 lock_output_stream(out);
Weiyin Jiang280ea742020-09-08 20:28:22 +08006916 pthread_mutex_lock(&out->latch_lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006917 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306918 if (out->card_status != CARD_STATUS_OFFLINE) {
Naresh Tanniru80659832014-06-04 18:17:56 +05306919 status = compress_resume(out->compr);
Mingming Yin21854652016-04-13 11:54:02 -07006920 }
6921 if (!status) {
6922 out->offload_state = OFFLOAD_STATE_PLAYING;
6923 }
Dhanalakshmi Siddani79415e72015-03-23 11:54:47 +05306924 audio_extn_dts_eagle_fade(adev, true, out);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08006925 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
6926 popcount(out->channel_mask), 1);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006927 }
Weiyin Jiang280ea742020-09-08 20:28:22 +08006928 pthread_mutex_unlock(&out->latch_lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006929 pthread_mutex_unlock(&out->lock);
6930 }
6931 return status;
6932}
6933
6934static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
6935{
6936 struct stream_out *out = (struct stream_out *)stream;
6937 int status = -ENOSYS;
6938 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07006939 if (is_offload_usecase(out->usecase)) {
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006940 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006941 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
6942 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
6943 else
6944 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
6945 pthread_mutex_unlock(&out->lock);
6946 }
6947 return status;
6948}
6949
6950static int out_flush(struct audio_stream_out* stream)
6951{
6952 struct stream_out *out = (struct stream_out *)stream;
6953 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07006954 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07006955 ALOGD("copl(%p):calling compress flush", out);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006956 lock_output_stream(out);
Weiyin Jiang280ea742020-09-08 20:28:22 +08006957 pthread_mutex_lock(&out->latch_lock);
Haynes Mathew Georgeafe54d82016-09-21 14:39:19 -07006958 if (out->offload_state == OFFLOAD_STATE_PAUSED) {
Gautam Manam14c198b2020-12-24 14:08:04 +05306959 pthread_mutex_unlock(&out->latch_lock);
Haynes Mathew Georgeafe54d82016-09-21 14:39:19 -07006960 stop_compressed_output_l(out);
Haynes Mathew Georgeafe54d82016-09-21 14:39:19 -07006961 } else {
6962 ALOGW("%s called in invalid state %d", __func__, out->offload_state);
Gautam Manam14c198b2020-12-24 14:08:04 +05306963 pthread_mutex_unlock(&out->latch_lock);
Haynes Mathew Georgeafe54d82016-09-21 14:39:19 -07006964 }
Weiyin Jiang547e4152017-09-14 17:24:18 +08006965 out->written = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006966 pthread_mutex_unlock(&out->lock);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07006967 ALOGD("copl(%p):out of compress flush", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006968 return 0;
6969 }
6970 return -ENOSYS;
6971}
6972
Haynes Mathew George16081042017-05-31 17:16:49 -07006973static int out_stop(const struct audio_stream_out* stream)
6974{
6975 struct stream_out *out = (struct stream_out *)stream;
6976 struct audio_device *adev = out->dev;
6977 int ret = -ENOSYS;
6978
6979 ALOGV("%s", __func__);
6980 pthread_mutex_lock(&adev->lock);
6981 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP && !out->standby &&
6982 out->playback_started && out->pcm != NULL) {
6983 pcm_stop(out->pcm);
6984 ret = stop_output_stream(out);
6985 out->playback_started = false;
6986 }
6987 pthread_mutex_unlock(&adev->lock);
6988 return ret;
6989}
6990
6991static int out_start(const struct audio_stream_out* stream)
6992{
6993 struct stream_out *out = (struct stream_out *)stream;
6994 struct audio_device *adev = out->dev;
6995 int ret = -ENOSYS;
6996
6997 ALOGV("%s", __func__);
6998 pthread_mutex_lock(&adev->lock);
6999 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP && !out->standby &&
7000 !out->playback_started && out->pcm != NULL) {
7001 ret = start_output_stream(out);
7002 if (ret == 0) {
7003 out->playback_started = true;
7004 }
7005 }
7006 pthread_mutex_unlock(&adev->lock);
7007 return ret;
7008}
7009
7010/*
7011 * Modify config->period_count based on min_size_frames
7012 */
7013static void adjust_mmap_period_count(struct pcm_config *config, int32_t min_size_frames)
7014{
7015 int periodCountRequested = (min_size_frames + config->period_size - 1)
7016 / config->period_size;
7017 int periodCount = MMAP_PERIOD_COUNT_MIN;
7018
7019 ALOGV("%s original config.period_size = %d config.period_count = %d",
7020 __func__, config->period_size, config->period_count);
7021
7022 while (periodCount < periodCountRequested && (periodCount * 2) < MMAP_PERIOD_COUNT_MAX) {
7023 periodCount *= 2;
7024 }
7025 config->period_count = periodCount;
7026
7027 ALOGV("%s requested config.period_count = %d", __func__, config->period_count);
7028}
7029
Phil Burkfe17efd2019-03-25 10:23:35 -07007030// Read offset for the positional timestamp from a persistent vendor property.
7031// This is to workaround apparent inaccuracies in the timing information that
7032// is used by the AAudio timing model. The inaccuracies can cause glitches.
7033static int64_t get_mmap_out_time_offset() {
7034 const int32_t kDefaultOffsetMicros = 0;
7035 int32_t mmap_time_offset_micros = property_get_int32(
Weiyin Jiangbd68b4d2019-05-17 16:29:50 +08007036 "persist.vendor.audio.out_mmap_delay_micros", kDefaultOffsetMicros);
Phil Burkfe17efd2019-03-25 10:23:35 -07007037 ALOGI("mmap_time_offset_micros = %d for output", mmap_time_offset_micros);
7038 return mmap_time_offset_micros * (int64_t)1000;
7039}
7040
Haynes Mathew George16081042017-05-31 17:16:49 -07007041static int out_create_mmap_buffer(const struct audio_stream_out *stream,
7042 int32_t min_size_frames,
7043 struct audio_mmap_buffer_info *info)
7044{
7045 struct stream_out *out = (struct stream_out *)stream;
7046 struct audio_device *adev = out->dev;
7047 int ret = 0;
Aalique Grahame1f123102017-10-12 10:38:32 -07007048 unsigned int offset1 = 0;
7049 unsigned int frames1 = 0;
Haynes Mathew George16081042017-05-31 17:16:49 -07007050 const char *step = "";
Haynes Mathew Georgeef514882017-05-01 17:46:23 -07007051 uint32_t mmap_size;
Arun Mirpuri5d170872019-03-26 13:21:31 -07007052 uint32_t buffer_size;
Haynes Mathew George16081042017-05-31 17:16:49 -07007053
Arun Mirpuri5d170872019-03-26 13:21:31 -07007054 ALOGD("%s", __func__);
Sharad Sanglec6f32552018-05-04 16:15:38 +05307055 lock_output_stream(out);
Haynes Mathew George16081042017-05-31 17:16:49 -07007056 pthread_mutex_lock(&adev->lock);
7057
Sharad Sanglec6f32552018-05-04 16:15:38 +05307058 if (CARD_STATUS_OFFLINE == out->card_status ||
Shubhasini Sugumaran3fde6582022-01-19 15:54:50 -05007059 CARD_STATUS_OFFLINE == adev->card_status ||
7060 POWER_POLICY_STATUS_OFFLINE == adev->out_power_policy) {
Sharad Sanglec6f32552018-05-04 16:15:38 +05307061 ALOGW("out->card_status or adev->card_status offline, try again");
7062 ret = -EIO;
7063 goto exit;
7064 }
Sujin Panicker7c7b6f92020-04-03 12:57:55 +05307065 if (info == NULL || !(min_size_frames > 0 && min_size_frames < INT32_MAX)) {
Haynes Mathew George16081042017-05-31 17:16:49 -07007066 ALOGE("%s: info = %p, min_size_frames = %d", __func__, info, min_size_frames);
7067 ret = -EINVAL;
7068 goto exit;
7069 }
7070 if (out->usecase != USECASE_AUDIO_PLAYBACK_MMAP || !out->standby) {
7071 ALOGE("%s: usecase = %d, standby = %d", __func__, out->usecase, out->standby);
7072 ret = -ENOSYS;
7073 goto exit;
7074 }
7075 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
7076 if (out->pcm_device_id < 0) {
7077 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
7078 __func__, out->pcm_device_id, out->usecase);
7079 ret = -EINVAL;
7080 goto exit;
7081 }
7082
7083 adjust_mmap_period_count(&out->config, min_size_frames);
7084
Arun Mirpuri5d170872019-03-26 13:21:31 -07007085 ALOGD("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
Haynes Mathew George16081042017-05-31 17:16:49 -07007086 __func__, adev->snd_card, out->pcm_device_id, out->config.channels);
7087 out->pcm = pcm_open(adev->snd_card, out->pcm_device_id,
7088 (PCM_OUT | PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC), &out->config);
Satish Babu Patakokila54ce83d2018-07-06 18:00:37 +05307089 if (errno == ENETRESET && !pcm_is_ready(out->pcm)) {
Sharad Sanglec6f32552018-05-04 16:15:38 +05307090 ALOGE("%s: pcm_open failed errno:%d\n", __func__, errno);
7091 out->card_status = CARD_STATUS_OFFLINE;
7092 adev->card_status = CARD_STATUS_OFFLINE;
7093 ret = -EIO;
7094 goto exit;
7095 }
7096
Haynes Mathew George16081042017-05-31 17:16:49 -07007097 if (out->pcm == NULL || !pcm_is_ready(out->pcm)) {
7098 step = "open";
7099 ret = -ENODEV;
7100 goto exit;
7101 }
7102 ret = pcm_mmap_begin(out->pcm, &info->shared_memory_address, &offset1, &frames1);
7103 if (ret < 0) {
7104 step = "begin";
7105 goto exit;
7106 }
juyuchen626833d2019-06-04 16:48:02 +08007107
7108 info->flags = 0;
Haynes Mathew George16081042017-05-31 17:16:49 -07007109 info->buffer_size_frames = pcm_get_buffer_size(out->pcm);
Arun Mirpuri5d170872019-03-26 13:21:31 -07007110 buffer_size = pcm_frames_to_bytes(out->pcm, info->buffer_size_frames);
Haynes Mathew George16081042017-05-31 17:16:49 -07007111 info->burst_size_frames = out->config.period_size;
Haynes Mathew Georgeef514882017-05-01 17:46:23 -07007112 ret = platform_get_mmap_data_fd(adev->platform,
7113 out->pcm_device_id, 0 /*playback*/,
7114 &info->shared_memory_fd,
7115 &mmap_size);
7116 if (ret < 0) {
Arun Mirpuri5d170872019-03-26 13:21:31 -07007117 // Fall back to non exclusive mode
7118 info->shared_memory_fd = pcm_get_poll_fd(out->pcm);
7119 } else {
Phil Burkd898ba62019-06-20 12:49:01 -07007120 out->mmap_shared_memory_fd = info->shared_memory_fd; // for closing later
7121 ALOGV("%s: opened mmap_shared_memory_fd = %d", __func__, out->mmap_shared_memory_fd);
7122
Arun Mirpuri5d170872019-03-26 13:21:31 -07007123 if (mmap_size < buffer_size) {
7124 step = "mmap";
7125 goto exit;
7126 }
juyuchen626833d2019-06-04 16:48:02 +08007127 info->flags |= AUDIO_MMAP_APPLICATION_SHAREABLE;
Haynes Mathew Georgeef514882017-05-01 17:46:23 -07007128 }
Haynes Mathew George16081042017-05-31 17:16:49 -07007129 memset(info->shared_memory_address, 0, pcm_frames_to_bytes(out->pcm,
Haynes Mathew Georgeef514882017-05-01 17:46:23 -07007130 info->buffer_size_frames));
Haynes Mathew George16081042017-05-31 17:16:49 -07007131
7132 ret = pcm_mmap_commit(out->pcm, 0, MMAP_PERIOD_SIZE);
7133 if (ret < 0) {
7134 step = "commit";
7135 goto exit;
7136 }
7137
Phil Burkfe17efd2019-03-25 10:23:35 -07007138 out->mmap_time_offset_nanos = get_mmap_out_time_offset();
7139
Haynes Mathew George16081042017-05-31 17:16:49 -07007140 out->standby = false;
7141 ret = 0;
7142
Arun Mirpuri5d170872019-03-26 13:21:31 -07007143 ALOGD("%s: got mmap buffer address %p info->buffer_size_frames %d",
Haynes Mathew George16081042017-05-31 17:16:49 -07007144 __func__, info->shared_memory_address, info->buffer_size_frames);
7145
7146exit:
7147 if (ret != 0) {
7148 if (out->pcm == NULL) {
7149 ALOGE("%s: %s - %d", __func__, step, ret);
7150 } else {
7151 ALOGE("%s: %s %s", __func__, step, pcm_get_error(out->pcm));
7152 pcm_close(out->pcm);
7153 out->pcm = NULL;
7154 }
7155 }
7156 pthread_mutex_unlock(&adev->lock);
Sharad Sanglec6f32552018-05-04 16:15:38 +05307157 pthread_mutex_unlock(&out->lock);
Haynes Mathew George16081042017-05-31 17:16:49 -07007158 return ret;
7159}
7160
7161static int out_get_mmap_position(const struct audio_stream_out *stream,
7162 struct audio_mmap_position *position)
7163{
7164 struct stream_out *out = (struct stream_out *)stream;
7165 ALOGVV("%s", __func__);
7166 if (position == NULL) {
7167 return -EINVAL;
7168 }
7169 if (out->usecase != USECASE_AUDIO_PLAYBACK_MMAP) {
Haynes Mathew George4ab3ba92017-12-11 14:49:43 -08007170 ALOGE("%s: called on %s", __func__, use_case_table[out->usecase]);
Haynes Mathew George16081042017-05-31 17:16:49 -07007171 return -ENOSYS;
7172 }
7173 if (out->pcm == NULL) {
7174 return -ENOSYS;
7175 }
7176
7177 struct timespec ts = { 0, 0 };
7178 int ret = pcm_mmap_get_hw_ptr(out->pcm, (unsigned int *)&position->position_frames, &ts);
7179 if (ret < 0) {
7180 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
7181 return ret;
7182 }
Phil Burkfe17efd2019-03-25 10:23:35 -07007183 position->time_nanoseconds = ts.tv_sec*1000000000LL + ts.tv_nsec
7184 + out->mmap_time_offset_nanos;
Haynes Mathew George16081042017-05-31 17:16:49 -07007185 return 0;
7186}
7187
7188
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007189/** audio_stream_in implementation **/
7190static uint32_t in_get_sample_rate(const struct audio_stream *stream)
7191{
7192 struct stream_in *in = (struct stream_in *)stream;
7193
7194 return in->config.rate;
7195}
7196
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07007197static int in_set_sample_rate(struct audio_stream *stream __unused,
7198 uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007199{
7200 return -ENOSYS;
7201}
7202
7203static size_t in_get_buffer_size(const struct audio_stream *stream)
7204{
7205 struct stream_in *in = (struct stream_in *)stream;
7206
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007207 if(in->usecase == USECASE_COMPRESS_VOIP_CALL)
7208 return voice_extn_compress_voip_in_get_buffer_size(in);
Mingming Yine62d7842013-10-25 16:26:03 -07007209 else if(audio_extn_compr_cap_usecase_supported(in->usecase))
Raghu Bankapur37cbf382022-12-01 09:30:00 +05307210 return audio_extn_compr_cap_get_buffer_size(pcm_format_to_audio_format(in->config.format));
Dhananjay Kumar7dbe3562019-08-30 05:49:33 +05307211 else if(audio_extn_cin_attached_usecase(in))
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307212 return audio_extn_cin_get_buffer_size(in);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007213
Haynes Mathew George5beddd42016-06-27 18:33:40 -07007214 return in->config.period_size * in->af_period_multiplier *
7215 audio_stream_in_frame_size((const struct audio_stream_in *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007216}
7217
7218static uint32_t in_get_channels(const struct audio_stream *stream)
7219{
7220 struct stream_in *in = (struct stream_in *)stream;
7221
7222 return in->channel_mask;
7223}
7224
7225static audio_format_t in_get_format(const struct audio_stream *stream)
7226{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007227 struct stream_in *in = (struct stream_in *)stream;
7228
7229 return in->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007230}
7231
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07007232static int in_set_format(struct audio_stream *stream __unused,
7233 audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007234{
7235 return -ENOSYS;
7236}
7237
7238static int in_standby(struct audio_stream *stream)
7239{
7240 struct stream_in *in = (struct stream_in *)stream;
7241 struct audio_device *adev = in->dev;
7242 int status = 0;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05307243 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
7244 stream, in->usecase, use_case_table[in->usecase]);
Haynes Mathew George16081042017-05-31 17:16:49 -07007245 bool do_stop = true;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05307246
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07007247 lock_input_stream(in);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07007248 if (!in->standby && in->is_st_session) {
7249 ALOGD("%s: sound trigger pcm stop lab", __func__);
7250 audio_extn_sound_trigger_stop_lab(in);
George Gao3018ede2019-10-23 13:23:00 -07007251 if (adev->num_va_sessions > 0)
7252 adev->num_va_sessions--;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07007253 in->standby = 1;
7254 }
7255
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007256 if (!in->standby) {
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07007257 if (adev->adm_deregister_stream)
7258 adev->adm_deregister_stream(adev->adm_data, in->capture_handle);
7259
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08007260 pthread_mutex_lock(&adev->lock);
Daniel Hillenbrand17873952013-05-23 10:10:00 +05307261 amplifier_input_stream_standby((struct audio_stream_in *) stream);
7262
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007263 in->standby = true;
Zhou Songa8895042016-07-05 17:54:22 +08007264 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
kunleizbecba2d2017-09-07 13:37:16 +08007265 do_stop = false;
Zhou Songa8895042016-07-05 17:54:22 +08007266 voice_extn_compress_voip_close_input_stream(stream);
7267 ALOGD("VOIP input entered standby");
Haynes Mathew George16081042017-05-31 17:16:49 -07007268 } else if (in->usecase == USECASE_AUDIO_RECORD_MMAP) {
7269 do_stop = in->capture_started;
7270 in->capture_started = false;
Phil Burkd898ba62019-06-20 12:49:01 -07007271 if (in->mmap_shared_memory_fd >= 0) {
7272 ALOGV("%s: closing mmap_shared_memory_fd = %d",
7273 __func__, in->mmap_shared_memory_fd);
7274 close(in->mmap_shared_memory_fd);
7275 in->mmap_shared_memory_fd = -1;
7276 }
Zhou Songa8895042016-07-05 17:54:22 +08007277 } else {
Dhananjay Kumar7dbe3562019-08-30 05:49:33 +05307278 if (audio_extn_cin_attached_usecase(in))
Manish Dewangan46e07982018-12-13 18:18:59 +05307279 audio_extn_cin_close_input_stream(in);
kunleizbecba2d2017-09-07 13:37:16 +08007280 }
7281
Arun Mirpuri5d170872019-03-26 13:21:31 -07007282 if (in->pcm) {
7283 ATRACE_BEGIN("pcm_in_close");
7284 pcm_close(in->pcm);
7285 ATRACE_END();
7286 in->pcm = NULL;
7287 }
7288
Carter Hsu2e429db2019-05-14 18:50:52 +08007289 if (do_stop)
Zhou Songa8895042016-07-05 17:54:22 +08007290 status = stop_input_stream(in);
Quinn Malef6050362019-01-30 15:55:40 -08007291
George Gao3018ede2019-10-23 13:23:00 -07007292 if (in->source == AUDIO_SOURCE_VOICE_RECOGNITION) {
7293 if (adev->num_va_sessions > 0)
7294 adev->num_va_sessions--;
7295 }
Quinn Malef6050362019-01-30 15:55:40 -08007296
Eric Laurent150dbfe2013-02-27 14:31:02 -08007297 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007298 }
7299 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07007300 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007301 return status;
7302}
7303
Aalique Grahame22e49102018-12-18 14:23:57 -08007304static int in_dump(const struct audio_stream *stream,
7305 int fd)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007306{
Aalique Grahame22e49102018-12-18 14:23:57 -08007307 struct stream_in *in = (struct stream_in *)stream;
7308
7309 // We try to get the lock for consistency,
7310 // but it isn't necessary for these variables.
7311 // If we're not in standby, we may be blocked on a read.
7312 const bool locked = (pthread_mutex_trylock(&in->lock) == 0);
7313 dprintf(fd, " Standby: %s\n", in->standby ? "yes" : "no");
7314 dprintf(fd, " Frames read: %lld\n", (long long)in->frames_read);
7315 dprintf(fd, " Frames muted: %lld\n", (long long)in->frames_muted);
Dechen Chai22768452021-07-30 09:29:16 +05307316#ifndef LINUX_ENABLED
Andy Hungc6bfd4a2019-07-01 18:26:00 -07007317 char buffer[256]; // for statistics formatting
7318 if (in->start_latency_ms.n > 0) {
7319 simple_stats_to_string(&in->start_latency_ms, buffer, sizeof(buffer));
7320 dprintf(fd, " Start latency ms: %s\n", buffer);
7321 }
Dechen Chai22768452021-07-30 09:29:16 +05307322#endif
Aalique Grahame22e49102018-12-18 14:23:57 -08007323 if (locked) {
7324 pthread_mutex_unlock(&in->lock);
7325 }
Dechen Chai22768452021-07-30 09:29:16 +05307326#ifndef LINUX_ENABLED
Aalique Grahame22e49102018-12-18 14:23:57 -08007327 // dump error info
7328 (void)error_log_dump(
7329 in->error_log, fd, " " /* prefix */, 0 /* lines */, 0 /* limit_ns */);
Dechen Chai22768452021-07-30 09:29:16 +05307330#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007331 return 0;
7332}
7333
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05307334static void in_snd_mon_cb(void * stream, struct str_parms * parms)
7335{
7336 if (!stream || !parms)
7337 return;
7338
7339 struct stream_in *in = (struct stream_in *)stream;
7340 struct audio_device *adev = in->dev;
7341
7342 card_status_t status;
7343 int card;
7344 if (parse_snd_card_status(parms, &card, &status) < 0)
7345 return;
7346
7347 pthread_mutex_lock(&adev->lock);
7348 bool valid_cb = (card == adev->snd_card);
7349 pthread_mutex_unlock(&adev->lock);
7350
7351 if (!valid_cb)
7352 return;
7353
7354 lock_input_stream(in);
7355 if (in->card_status != status)
7356 in->card_status = status;
7357 pthread_mutex_unlock(&in->lock);
7358
7359 ALOGW("in_snd_mon_cb for card %d usecase %s, status %s", card,
7360 use_case_table[in->usecase],
7361 status == CARD_STATUS_OFFLINE ? "offline" : "online");
7362
7363 // a better solution would be to report error back to AF and let
7364 // it put the stream to standby
7365 if (status == CARD_STATUS_OFFLINE)
7366 in_standby(&in->stream.common);
7367
7368 return;
7369}
7370
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07007371int route_input_stream(struct stream_in *in,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08007372 struct listnode *devices,
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07007373 audio_source_t source)
7374{
7375 struct audio_device *adev = in->dev;
7376 int ret = 0;
7377
7378 lock_input_stream(in);
7379 pthread_mutex_lock(&adev->lock);
7380
7381 /* no audio source uses val == 0 */
7382 if ((in->source != source) && (source != AUDIO_SOURCE_DEFAULT)) {
7383 in->source = source;
7384 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
7385 (in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
7386 (voice_extn_compress_voip_is_format_supported(in->format)) &&
7387 (in->config.rate == 8000 || in->config.rate == 16000 ||
7388 in->config.rate == 32000 || in->config.rate == 48000 ) &&
7389 (audio_channel_count_from_in_mask(in->channel_mask) == 1)) {
7390 ret = voice_extn_compress_voip_open_input_stream(in);
7391 if (ret != 0) {
7392 ALOGE("%s: Compress voip input cannot be opened, error:%d",
7393 __func__, ret);
7394 }
7395 }
7396 }
7397
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08007398 if (!compare_devices(&in->device_list, devices) && !list_empty(devices) &&
7399 is_audio_in_device_type(devices)) {
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07007400 // Workaround: If routing to an non existing usb device, fail gracefully
7401 // The routing request will otherwise block during 10 second
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08007402 struct str_parms *usb_addr =
7403 str_parms_create_str(get_usb_device_address(devices));
7404 if (is_usb_in_device_type(devices) && usb_addr &&
Weiyin Jiangabedea32020-12-09 12:49:19 +08007405 !audio_extn_usb_connected(NULL)) {
7406 ALOGW("%s: ignoring rerouting to non existing USB", __func__);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07007407 ret = -ENOSYS;
7408 } else {
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07007409 /* If recording is in progress, change the tx device to new device */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08007410 assign_devices(&in->device_list, devices);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07007411 if (!in->standby && !in->is_st_session) {
7412 ALOGV("update input routing change");
7413 // inform adm before actual routing to prevent glitches.
7414 if (adev->adm_on_routing_change) {
7415 adev->adm_on_routing_change(adev->adm_data,
7416 in->capture_handle);
7417 ret = select_devices(adev, in->usecase);
7418 if (in->usecase == USECASE_AUDIO_RECORD_LOW_LATENCY)
7419 adev->adm_routing_changed = true;
7420 }
7421 }
7422 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08007423 if (usb_addr)
7424 str_parms_destroy(usb_addr);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07007425 }
7426 pthread_mutex_unlock(&adev->lock);
7427 pthread_mutex_unlock(&in->lock);
7428
Aniket Kumar Latad13758f2020-08-06 15:11:36 -07007429 ALOGV("%s: exit: status(%d)", __func__, ret);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07007430 return ret;
7431}
7432
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007433static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
7434{
7435 struct stream_in *in = (struct stream_in *)stream;
7436 struct audio_device *adev = in->dev;
7437 struct str_parms *parms;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007438 char value[32];
Sujin Panicker7c7b6f92020-04-03 12:57:55 +05307439 int err = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007440
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05307441 ALOGD("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007442 parms = str_parms_create_str(kvpairs);
7443
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05307444 if (!parms)
7445 goto error;
Daniel Hillenbrand17873952013-05-23 10:10:00 +05307446
7447 amplifier_in_set_parameters(parms);
7448
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07007449 lock_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08007450 pthread_mutex_lock(&adev->lock);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08007451
Sujin Panicker7c7b6f92020-04-03 12:57:55 +05307452 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_PROFILE, value, sizeof(value));
7453 if (err >= 0) {
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +05307454 strlcpy(in->profile, value, sizeof(in->profile));
7455 ALOGV("updating stream profile with value '%s'", in->profile);
7456 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
7457 &adev->streams_input_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08007458 &in->device_list, in->flags, in->format,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +05307459 in->sample_rate, in->bit_width,
7460 in->profile, &in->app_type_cfg);
7461 }
7462
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007463 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08007464 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007465
7466 str_parms_destroy(parms);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05307467error:
Sujin Panicker7c7b6f92020-04-03 12:57:55 +05307468 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007469}
7470
7471static char* in_get_parameters(const struct audio_stream *stream,
7472 const char *keys)
7473{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007474 struct stream_in *in = (struct stream_in *)stream;
7475 struct str_parms *query = str_parms_create_str(keys);
7476 char *str;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007477 struct str_parms *reply = str_parms_create();
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07007478
7479 if (!query || !reply) {
Alexy Josephaee4fdd2016-01-29 13:02:07 -08007480 if (reply) {
7481 str_parms_destroy(reply);
7482 }
7483 if (query) {
7484 str_parms_destroy(query);
7485 }
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07007486 ALOGE("in_get_parameters: failed to create query or reply");
7487 return NULL;
7488 }
7489
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007490 ALOGV("%s: enter: keys - %s %s ", __func__, use_case_table[in->usecase], keys);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007491
7492 voice_extn_in_get_parameters(in, query, reply);
7493
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007494 stream_get_parameter_channels(query, reply,
7495 &in->supported_channel_masks[0]);
7496 stream_get_parameter_formats(query, reply,
7497 &in->supported_formats[0]);
7498 stream_get_parameter_rates(query, reply,
7499 &in->supported_sample_rates[0]);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007500 str = str_parms_to_str(reply);
7501 str_parms_destroy(query);
7502 str_parms_destroy(reply);
7503
7504 ALOGV("%s: exit: returns - %s", __func__, str);
7505 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007506}
7507
Aalique Grahame22e49102018-12-18 14:23:57 -08007508static int in_set_gain(struct audio_stream_in *stream,
7509 float gain)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007510{
Aalique Grahame22e49102018-12-18 14:23:57 -08007511 struct stream_in *in = (struct stream_in *)stream;
7512 char mixer_ctl_name[128];
7513 struct mixer_ctl *ctl;
7514 int ctl_value;
7515
7516 ALOGV("%s: gain %f", __func__, gain);
7517
7518 if (stream == NULL)
7519 return -EINVAL;
7520
7521 /* in_set_gain() only used to silence MMAP capture for now */
7522 if (in->usecase != USECASE_AUDIO_RECORD_MMAP)
7523 return -ENOSYS;
7524
7525 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name), "Capture %d Volume", in->pcm_device_id);
7526
7527 ctl = mixer_get_ctl_by_name(in->dev->mixer, mixer_ctl_name);
7528 if (!ctl) {
7529 ALOGW("%s: Could not get ctl for mixer cmd - %s",
7530 __func__, mixer_ctl_name);
7531 return -ENOSYS;
7532 }
7533
7534 if (gain < RECORD_GAIN_MIN)
7535 gain = RECORD_GAIN_MIN;
7536 else if (gain > RECORD_GAIN_MAX)
7537 gain = RECORD_GAIN_MAX;
7538 ctl_value = (int)(RECORD_VOLUME_CTL_MAX * gain);
7539
7540 mixer_ctl_set_value(ctl, 0, ctl_value);
7541
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007542 return 0;
7543}
7544
7545static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
7546 size_t bytes)
7547{
7548 struct stream_in *in = (struct stream_in *)stream;
Pallavid7c7a272018-01-16 11:22:55 +05307549
7550 if (in == NULL) {
7551 ALOGE("%s: stream_in ptr is NULL", __func__);
7552 return -EINVAL;
7553 }
7554
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007555 struct audio_device *adev = in->dev;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05307556 int ret = -1;
Dhananjay Kumar97b81e32019-05-15 21:00:21 +05307557 size_t bytes_read = 0, frame_size = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007558
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07007559 lock_input_stream(in);
Naresh Tanniru4c630392014-05-12 01:05:52 +05307560
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07007561 if (in->is_st_session) {
7562 ALOGVV(" %s: reading on st session bytes=%zu", __func__, bytes);
7563 /* Read from sound trigger HAL */
7564 audio_extn_sound_trigger_read(in, buffer, bytes);
Quinn Malef6050362019-01-30 15:55:40 -08007565 if (in->standby) {
George Gao3018ede2019-10-23 13:23:00 -07007566 if (adev->num_va_sessions < UINT_MAX)
7567 adev->num_va_sessions++;
Quinn Malef6050362019-01-30 15:55:40 -08007568 in->standby = 0;
7569 }
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07007570 pthread_mutex_unlock(&in->lock);
7571 return bytes;
7572 }
7573
Haynes Mathew George16081042017-05-31 17:16:49 -07007574 if (in->usecase == USECASE_AUDIO_RECORD_MMAP) {
7575 ret = -ENOSYS;
7576 goto exit;
7577 }
7578
Aniket Kumar Lata60586a92019-05-22 22:18:55 -07007579 if (in->usecase == USECASE_AUDIO_RECORD_LOW_LATENCY &&
7580 !in->standby && adev->adm_routing_changed) {
7581 ret = -ENOSYS;
7582 goto exit;
7583 }
7584
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007585 if (in->standby) {
Andy Hungc6bfd4a2019-07-01 18:26:00 -07007586 const int64_t startNs = systemTime(SYSTEM_TIME_MONOTONIC);
7587
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07007588 pthread_mutex_lock(&adev->lock);
7589 if (in->usecase == USECASE_COMPRESS_VOIP_CALL)
7590 ret = voice_extn_compress_voip_start_input_stream(in);
7591 else
7592 ret = start_input_stream(in);
George Gao3018ede2019-10-23 13:23:00 -07007593 if (!ret && in->source == AUDIO_SOURCE_VOICE_RECOGNITION) {
7594 if (adev->num_va_sessions < UINT_MAX)
7595 adev->num_va_sessions++;
7596 }
Daniel Hillenbrand17873952013-05-23 10:10:00 +05307597
7598 if (ret == 0)
7599 amplifier_input_stream_start(stream);
7600
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07007601 pthread_mutex_unlock(&adev->lock);
7602 if (ret != 0) {
7603 goto exit;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007604 }
7605 in->standby = 0;
Dechen Chai22768452021-07-30 09:29:16 +05307606#ifndef LINUX_ENABLED
Andy Hungc6bfd4a2019-07-01 18:26:00 -07007607 // log startup time in ms.
7608 simple_stats_log(
7609 &in->start_latency_ms, (systemTime(SYSTEM_TIME_MONOTONIC) - startNs) * 1e-6);
Dechen Chai22768452021-07-30 09:29:16 +05307610#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007611 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007612
Deeraj Soman30cc7ae2019-03-18 16:26:55 +05307613 /* Avoid read if capture_stopped is set */
7614 if (android_atomic_acquire_load(&(in->capture_stopped)) > 0) {
7615 ALOGD("%s: force stopped catpure session, ignoring read request", __func__);
7616 ret = -EINVAL;
7617 goto exit;
7618 }
7619
Haynes Mathew George5beddd42016-06-27 18:33:40 -07007620 // what's the duration requested by the client?
7621 long ns = 0;
7622
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307623 if (in->pcm && in->config.rate)
Haynes Mathew George5beddd42016-06-27 18:33:40 -07007624 ns = pcm_bytes_to_frames(in->pcm, bytes)*1000000000LL/
7625 in->config.rate;
7626
Aniket Kumar Lata60586a92019-05-22 22:18:55 -07007627 ret = request_in_focus(in, ns);
7628 if (ret != 0)
7629 goto exit;
Haynes Mathew George5beddd42016-06-27 18:33:40 -07007630 bool use_mmap = is_mmap_usecase(in->usecase) || in->realtime;
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07007631
Dhananjay Kumar7dbe3562019-08-30 05:49:33 +05307632 if (audio_extn_cin_attached_usecase(in)) {
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307633 ret = audio_extn_cin_read(in, buffer, bytes, &bytes_read);
7634 } else if (in->pcm) {
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05307635 if (audio_extn_ssr_get_stream() == in) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07007636 ret = audio_extn_ssr_read(stream, buffer, bytes);
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05307637 } else if (audio_extn_compr_cap_usecase_supported(in->usecase)) {
Mingming Yine62d7842013-10-25 16:26:03 -07007638 ret = audio_extn_compr_cap_read(in, buffer, bytes);
Haynes Mathew George5beddd42016-06-27 18:33:40 -07007639 } else if (use_mmap) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07007640 ret = pcm_mmap_read(in->pcm, buffer, bytes);
Garmond Leunge2433c32017-09-28 21:51:22 -07007641 } else if (audio_extn_ffv_get_stream() == in) {
7642 ret = audio_extn_ffv_read(stream, buffer, bytes);
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05307643 } else {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07007644 ret = pcm_read(in->pcm, buffer, bytes);
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307645 /* data from DSP comes in 24_8 format, convert it to 8_24 */
7646 if (!ret && bytes > 0 && (in->format == AUDIO_FORMAT_PCM_8_24_BIT)) {
7647 if (audio_extn_utils_convert_format_24_8_to_8_24(buffer, bytes)
7648 != bytes) {
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05307649 ret = -EINVAL;
7650 goto exit;
7651 }
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307652 } else if (ret < 0) {
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05307653 ret = -errno;
7654 }
7655 }
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307656 /* bytes read is always set to bytes for non compress usecases */
7657 bytes_read = bytes;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007658 }
7659
Haynes Mathew George5beddd42016-06-27 18:33:40 -07007660 release_in_focus(in);
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07007661
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007662 /*
Quinn Malef6050362019-01-30 15:55:40 -08007663 * Instead of writing zeroes here, we could trust the hardware to always
7664 * provide zeroes when muted. This is also muted with voice recognition
7665 * usecases so that other clients do not have access to voice recognition
7666 * data.
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007667 */
Quinn Malef6050362019-01-30 15:55:40 -08007668 if ((ret == 0 && voice_get_mic_mute(adev) &&
7669 !voice_is_in_call_rec_stream(in) &&
Zhou Song62ea0282020-03-22 19:53:01 +08007670 (in->usecase != USECASE_AUDIO_RECORD_AFE_PROXY &&
Manoj Kumar N D54cab152023-01-27 16:41:55 +05307671 in->usecase != USECASE_AUDIO_RECORD_AFE_PROXY2 &&
7672 in->source != AUDIO_SOURCE_FM_TUNER &&
7673 !is_single_device_type_equal(&in->device_list, AUDIO_DEVICE_IN_FM_TUNER))) ||
Quinn Malef6050362019-01-30 15:55:40 -08007674 (adev->num_va_sessions &&
7675 in->source != AUDIO_SOURCE_VOICE_RECOGNITION &&
7676 property_get_bool("persist.vendor.audio.va_concurrency_mute_enabled",
7677 false)))
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007678 memset(buffer, 0, bytes);
7679
7680exit:
Dhananjay Kumar97b81e32019-05-15 21:00:21 +05307681 frame_size = audio_stream_in_frame_size(stream);
7682 if (frame_size > 0)
7683 in->frames_read += bytes_read/frame_size;
7684
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07007685 if (-ENETRESET == ret)
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05307686 in->card_status = CARD_STATUS_OFFLINE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007687 pthread_mutex_unlock(&in->lock);
7688
7689 if (ret != 0) {
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05307690 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05307691 pthread_mutex_lock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05307692 voice_extn_compress_voip_close_input_stream(&in->stream.common);
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05307693 pthread_mutex_unlock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05307694 in->standby = true;
7695 }
Dhananjay Kumar7dbe3562019-08-30 05:49:33 +05307696 if (!audio_extn_cin_attached_usecase(in)) {
Sharad Sangled17c9122017-03-20 15:58:52 +05307697 bytes_read = bytes;
7698 memset(buffer, 0, bytes);
7699 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007700 in_standby(&in->stream.common);
Aniket Kumar Lata60586a92019-05-22 22:18:55 -07007701 if (in->usecase == USECASE_AUDIO_RECORD_LOW_LATENCY)
7702 adev->adm_routing_changed = false;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07007703 ALOGV("%s: read failed status %d- sleeping for buffer duration", __func__, ret);
Ashish Jainbbce4322016-02-16 13:25:27 +05307704 usleep((uint64_t)bytes * 1000000 / audio_stream_in_frame_size(stream) /
Naresh Tanniru4c630392014-05-12 01:05:52 +05307705 in_get_sample_rate(&in->stream.common));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007706 }
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307707 return bytes_read;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007708}
7709
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07007710static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007711{
7712 return 0;
7713}
7714
Aalique Grahame22e49102018-12-18 14:23:57 -08007715static int in_get_capture_position(const struct audio_stream_in *stream,
7716 int64_t *frames, int64_t *time)
7717{
7718 if (stream == NULL || frames == NULL || time == NULL) {
7719 return -EINVAL;
7720 }
7721 struct stream_in *in = (struct stream_in *)stream;
7722 int ret = -ENOSYS;
7723
7724 lock_input_stream(in);
7725 // note: ST sessions do not close the alsa pcm driver synchronously
7726 // on standby. Therefore, we may return an error even though the
7727 // pcm stream is still opened.
7728 if (in->standby) {
7729 ALOGE_IF(in->pcm != NULL && !in->is_st_session,
7730 "%s stream in standby but pcm not NULL for non ST session", __func__);
7731 goto exit;
7732 }
7733 if (in->pcm) {
7734 struct timespec timestamp;
7735 unsigned int avail;
7736 if (pcm_get_htimestamp(in->pcm, &avail, &timestamp) == 0) {
7737 *frames = in->frames_read + avail;
Robert Lee58215542019-07-15 20:55:12 +08007738 *time = timestamp.tv_sec * 1000000000LL + timestamp.tv_nsec
George Gao9ba8a142020-07-23 14:30:03 -07007739 - platform_capture_latency(in) * 1000LL;
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +05307740 //Adjustment accounts for A2dp decoder latency for recording usecase
7741 // Note: decoder latency is returned in ms, while platform_capture_latency in ns.
7742 if (is_a2dp_in_device_type(&in->device_list))
7743 *time -= audio_extn_a2dp_get_decoder_latency() * 1000000LL;
Aalique Grahame22e49102018-12-18 14:23:57 -08007744 ret = 0;
7745 }
7746 }
7747exit:
7748 pthread_mutex_unlock(&in->lock);
7749 return ret;
7750}
7751
Carter Hsu2e429db2019-05-14 18:50:52 +08007752static int in_update_effect_list(bool add, effect_handle_t effect,
7753 struct listnode *head)
7754{
7755 struct listnode *node;
7756 struct in_effect_list *elist = NULL;
7757 struct in_effect_list *target = NULL;
7758 int ret = 0;
7759
7760 if (!head)
7761 return ret;
7762
7763 list_for_each(node, head) {
7764 elist = node_to_item(node, struct in_effect_list, list);
7765 if (elist->handle == effect) {
7766 target = elist;
7767 break;
7768 }
7769 }
7770
7771 if (add) {
7772 if (target) {
7773 ALOGD("effect %p already exist", effect);
7774 return ret;
7775 }
7776
7777 target = (struct in_effect_list *)
7778 calloc(1, sizeof(struct in_effect_list));
7779
7780 if (!target) {
7781 ALOGE("%s:fail to allocate memory", __func__);
7782 return -ENOMEM;
7783 }
7784
7785 target->handle = effect;
7786 list_add_tail(head, &target->list);
7787 } else {
7788 if (target) {
7789 list_remove(&target->list);
7790 free(target);
7791 }
7792 }
7793
7794 return ret;
7795}
7796
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007797static int add_remove_audio_effect(const struct audio_stream *stream,
7798 effect_handle_t effect,
7799 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007800{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007801 struct stream_in *in = (struct stream_in *)stream;
7802 int status = 0;
7803 effect_descriptor_t desc;
7804
7805 status = (*effect)->get_descriptor(effect, &desc);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07007806 ALOGV("%s: status %d in->standby %d enable:%d", __func__, status, in->standby, enable);
7807
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007808 if (status != 0)
7809 return status;
7810
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07007811 lock_input_stream(in);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007812 pthread_mutex_lock(&in->dev->lock);
kunleizd96526c2018-04-09 11:12:32 +08007813 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
Carter Hsu2e429db2019-05-14 18:50:52 +08007814 in->source == AUDIO_SOURCE_VOICE_RECOGNITION ||
7815 adev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007816 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
Carter Hsu2e429db2019-05-14 18:50:52 +08007817
7818 in_update_effect_list(enable, effect, &in->aec_list);
7819 enable = !list_empty(&in->aec_list);
7820 if (enable == in->enable_aec)
7821 goto exit;
7822
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007823 in->enable_aec = enable;
Carter Hsu2e429db2019-05-14 18:50:52 +08007824 ALOGD("AEC enable %d", enable);
7825
Aalique Grahame22e49102018-12-18 14:23:57 -08007826 if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
7827 in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) {
7828 in->dev->enable_voicerx = enable;
7829 struct audio_usecase *usecase;
7830 struct listnode *node;
7831 list_for_each(node, &in->dev->usecase_list) {
7832 usecase = node_to_item(node, struct audio_usecase, list);
7833 if (usecase->type == PCM_PLAYBACK)
7834 select_devices(in->dev, usecase->id);
7835 }
7836 }
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07007837 if (!in->standby) {
7838 if (enable_disable_effect(in->dev, EFFECT_AEC, enable) == ENOSYS)
7839 select_devices(in->dev, in->usecase);
7840 }
7841
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007842 }
Carter Hsu2e429db2019-05-14 18:50:52 +08007843 if (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0) {
7844
7845 in_update_effect_list(enable, effect, &in->ns_list);
7846 enable = !list_empty(&in->ns_list);
7847 if (enable == in->enable_ns)
7848 goto exit;
7849
Ravi Kumar Alamanda198185e2013-11-07 15:42:19 -08007850 in->enable_ns = enable;
Carter Hsu2e429db2019-05-14 18:50:52 +08007851 ALOGD("NS enable %d", enable);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07007852 if (!in->standby) {
kunleizd96526c2018-04-09 11:12:32 +08007853 if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
Ramu Gottipatifa5be522021-12-28 19:18:21 +05307854 in->source == AUDIO_SOURCE_VOICE_RECOGNITION ||
kunleizd96526c2018-04-09 11:12:32 +08007855 in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) {
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07007856 if (enable_disable_effect(in->dev, EFFECT_NS, enable) == ENOSYS)
7857 select_devices(in->dev, in->usecase);
7858 } else
7859 select_devices(in->dev, in->usecase);
7860 }
Ravi Kumar Alamanda198185e2013-11-07 15:42:19 -08007861 }
Carter Hsu2e429db2019-05-14 18:50:52 +08007862exit:
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007863 pthread_mutex_unlock(&in->dev->lock);
7864 pthread_mutex_unlock(&in->lock);
7865
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007866 return 0;
7867}
7868
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007869static int in_add_audio_effect(const struct audio_stream *stream,
7870 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007871{
Eric Laurent994a6932013-07-17 11:51:42 -07007872 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007873 return add_remove_audio_effect(stream, effect, true);
7874}
7875
7876static int in_remove_audio_effect(const struct audio_stream *stream,
7877 effect_handle_t effect)
7878{
Eric Laurent994a6932013-07-17 11:51:42 -07007879 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007880 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007881}
7882
Haynes Mathew George16081042017-05-31 17:16:49 -07007883static int in_stop(const struct audio_stream_in* stream)
7884{
7885 struct stream_in *in = (struct stream_in *)stream;
7886 struct audio_device *adev = in->dev;
7887
7888 int ret = -ENOSYS;
7889 ALOGV("%s", __func__);
7890 pthread_mutex_lock(&adev->lock);
7891 if (in->usecase == USECASE_AUDIO_RECORD_MMAP && !in->standby &&
7892 in->capture_started && in->pcm != NULL) {
7893 pcm_stop(in->pcm);
7894 ret = stop_input_stream(in);
7895 in->capture_started = false;
7896 }
7897 pthread_mutex_unlock(&adev->lock);
7898 return ret;
7899}
7900
7901static int in_start(const struct audio_stream_in* stream)
7902{
7903 struct stream_in *in = (struct stream_in *)stream;
7904 struct audio_device *adev = in->dev;
7905 int ret = -ENOSYS;
7906
7907 ALOGV("%s in %p", __func__, in);
7908 pthread_mutex_lock(&adev->lock);
7909 if (in->usecase == USECASE_AUDIO_RECORD_MMAP && !in->standby &&
7910 !in->capture_started && in->pcm != NULL) {
7911 if (!in->capture_started) {
7912 ret = start_input_stream(in);
7913 if (ret == 0) {
7914 in->capture_started = true;
7915 }
7916 }
7917 }
7918 pthread_mutex_unlock(&adev->lock);
7919 return ret;
7920}
7921
Phil Burke0a86d12019-02-16 22:28:11 -08007922// Read offset for the positional timestamp from a persistent vendor property.
7923// This is to workaround apparent inaccuracies in the timing information that
7924// is used by the AAudio timing model. The inaccuracies can cause glitches.
7925static int64_t in_get_mmap_time_offset() {
7926 const int32_t kDefaultOffsetMicros = 0;
7927 int32_t mmap_time_offset_micros = property_get_int32(
Weiyin Jiangbd68b4d2019-05-17 16:29:50 +08007928 "persist.vendor.audio.in_mmap_delay_micros", kDefaultOffsetMicros);
Phil Burke0a86d12019-02-16 22:28:11 -08007929 ALOGI("mmap_time_offset_micros = %d for input", mmap_time_offset_micros);
7930 return mmap_time_offset_micros * (int64_t)1000;
7931}
7932
Haynes Mathew George16081042017-05-31 17:16:49 -07007933static int in_create_mmap_buffer(const struct audio_stream_in *stream,
7934 int32_t min_size_frames,
7935 struct audio_mmap_buffer_info *info)
7936{
7937 struct stream_in *in = (struct stream_in *)stream;
7938 struct audio_device *adev = in->dev;
7939 int ret = 0;
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07007940 unsigned int offset1 = 0;
7941 unsigned int frames1 = 0;
Haynes Mathew George16081042017-05-31 17:16:49 -07007942 const char *step = "";
Arun Mirpuri5d170872019-03-26 13:21:31 -07007943 uint32_t mmap_size = 0;
7944 uint32_t buffer_size = 0;
Haynes Mathew George16081042017-05-31 17:16:49 -07007945
7946 pthread_mutex_lock(&adev->lock);
7947 ALOGV("%s in %p", __func__, in);
7948
Sharad Sanglec6f32552018-05-04 16:15:38 +05307949 if (CARD_STATUS_OFFLINE == in->card_status||
Shubhasini Sugumaran3fde6582022-01-19 15:54:50 -05007950 CARD_STATUS_OFFLINE == adev->card_status ||
7951 POWER_POLICY_STATUS_OFFLINE == adev->in_power_policy) {
Sharad Sanglec6f32552018-05-04 16:15:38 +05307952 ALOGW("in->card_status or adev->card_status offline, try again");
7953 ret = -EIO;
7954 goto exit;
7955 }
7956
Sujin Panicker7c7b6f92020-04-03 12:57:55 +05307957 if (info == NULL || !(min_size_frames > 0 && min_size_frames < INT32_MAX)) {
Haynes Mathew George16081042017-05-31 17:16:49 -07007958 ALOGE("%s invalid argument info %p min_size_frames %d", __func__, info, min_size_frames);
7959 ret = -EINVAL;
7960 goto exit;
7961 }
7962 if (in->usecase != USECASE_AUDIO_RECORD_MMAP || !in->standby) {
7963 ALOGE("%s: usecase = %d, standby = %d", __func__, in->usecase, in->standby);
7964 ALOGV("%s in %p", __func__, in);
7965 ret = -ENOSYS;
7966 goto exit;
7967 }
7968 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
7969 if (in->pcm_device_id < 0) {
7970 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
7971 __func__, in->pcm_device_id, in->usecase);
7972 ret = -EINVAL;
7973 goto exit;
7974 }
7975
7976 adjust_mmap_period_count(&in->config, min_size_frames);
7977
7978 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
7979 __func__, adev->snd_card, in->pcm_device_id, in->config.channels);
7980 in->pcm = pcm_open(adev->snd_card, in->pcm_device_id,
7981 (PCM_IN | PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC), &in->config);
Satish Babu Patakokila54ce83d2018-07-06 18:00:37 +05307982 if (errno == ENETRESET && !pcm_is_ready(in->pcm)) {
Sharad Sanglec6f32552018-05-04 16:15:38 +05307983 ALOGE("%s: pcm_open failed errno:%d\n", __func__, errno);
7984 in->card_status = CARD_STATUS_OFFLINE;
7985 adev->card_status = CARD_STATUS_OFFLINE;
7986 ret = -EIO;
7987 goto exit;
7988 }
7989
Haynes Mathew George16081042017-05-31 17:16:49 -07007990 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) {
7991 step = "open";
7992 ret = -ENODEV;
7993 goto exit;
7994 }
7995
7996 ret = pcm_mmap_begin(in->pcm, &info->shared_memory_address, &offset1, &frames1);
7997 if (ret < 0) {
7998 step = "begin";
7999 goto exit;
8000 }
Haynes Mathew George16081042017-05-31 17:16:49 -07008001
juyuchen626833d2019-06-04 16:48:02 +08008002 info->flags = 0;
Arun Mirpuri5d170872019-03-26 13:21:31 -07008003 info->buffer_size_frames = pcm_get_buffer_size(in->pcm);
8004 buffer_size = pcm_frames_to_bytes(in->pcm, info->buffer_size_frames);
8005 info->burst_size_frames = in->config.period_size;
8006 ret = platform_get_mmap_data_fd(adev->platform,
8007 in->pcm_device_id, 1 /*capture*/,
8008 &info->shared_memory_fd,
8009 &mmap_size);
8010 if (ret < 0) {
8011 // Fall back to non exclusive mode
8012 info->shared_memory_fd = pcm_get_poll_fd(in->pcm);
8013 } else {
Phil Burkd898ba62019-06-20 12:49:01 -07008014 in->mmap_shared_memory_fd = info->shared_memory_fd; // for closing later
8015 ALOGV("%s: opened mmap_shared_memory_fd = %d", __func__, in->mmap_shared_memory_fd);
8016
Arun Mirpuri5d170872019-03-26 13:21:31 -07008017 if (mmap_size < buffer_size) {
8018 step = "mmap";
8019 goto exit;
8020 }
juyuchen626833d2019-06-04 16:48:02 +08008021 info->flags |= AUDIO_MMAP_APPLICATION_SHAREABLE;
Arun Mirpuri5d170872019-03-26 13:21:31 -07008022 }
8023
8024 memset(info->shared_memory_address, 0, buffer_size);
Haynes Mathew George16081042017-05-31 17:16:49 -07008025
8026 ret = pcm_mmap_commit(in->pcm, 0, MMAP_PERIOD_SIZE);
8027 if (ret < 0) {
8028 step = "commit";
8029 goto exit;
8030 }
8031
Phil Burke0a86d12019-02-16 22:28:11 -08008032 in->mmap_time_offset_nanos = in_get_mmap_time_offset();
8033
Haynes Mathew George16081042017-05-31 17:16:49 -07008034 in->standby = false;
8035 ret = 0;
8036
8037 ALOGV("%s: got mmap buffer address %p info->buffer_size_frames %d",
8038 __func__, info->shared_memory_address, info->buffer_size_frames);
8039
8040exit:
8041 if (ret != 0) {
8042 if (in->pcm == NULL) {
8043 ALOGE("%s: %s - %d", __func__, step, ret);
8044 } else {
8045 ALOGE("%s: %s %s", __func__, step, pcm_get_error(in->pcm));
8046 pcm_close(in->pcm);
8047 in->pcm = NULL;
8048 }
8049 }
8050 pthread_mutex_unlock(&adev->lock);
8051 return ret;
8052}
8053
8054static int in_get_mmap_position(const struct audio_stream_in *stream,
8055 struct audio_mmap_position *position)
8056{
8057 struct stream_in *in = (struct stream_in *)stream;
8058 ALOGVV("%s", __func__);
8059 if (position == NULL) {
8060 return -EINVAL;
8061 }
Gautam Manam34d1f542021-01-05 20:24:37 +05308062 lock_input_stream(in);
Haynes Mathew George16081042017-05-31 17:16:49 -07008063 if (in->usecase != USECASE_AUDIO_RECORD_MMAP) {
Gautam Manam34d1f542021-01-05 20:24:37 +05308064 pthread_mutex_unlock(&in->lock);
Haynes Mathew George16081042017-05-31 17:16:49 -07008065 return -ENOSYS;
8066 }
8067 if (in->pcm == NULL) {
Gautam Manam34d1f542021-01-05 20:24:37 +05308068 pthread_mutex_unlock(&in->lock);
Haynes Mathew George16081042017-05-31 17:16:49 -07008069 return -ENOSYS;
8070 }
8071 struct timespec ts = { 0, 0 };
8072 int ret = pcm_mmap_get_hw_ptr(in->pcm, (unsigned int *)&position->position_frames, &ts);
8073 if (ret < 0) {
8074 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
Gautam Manam34d1f542021-01-05 20:24:37 +05308075 pthread_mutex_unlock(&in->lock);
Haynes Mathew George16081042017-05-31 17:16:49 -07008076 return ret;
8077 }
Phil Burke0a86d12019-02-16 22:28:11 -08008078 position->time_nanoseconds = ts.tv_sec*1000000000LL + ts.tv_nsec
8079 + in->mmap_time_offset_nanos;
Gautam Manam34d1f542021-01-05 20:24:37 +05308080 pthread_mutex_unlock(&in->lock);
Haynes Mathew George16081042017-05-31 17:16:49 -07008081 return 0;
8082}
8083
Naresh Tannirudcb47c52018-06-25 16:23:32 +05308084static int in_get_active_microphones(const struct audio_stream_in *stream,
8085 struct audio_microphone_characteristic_t *mic_array,
8086 size_t *mic_count) {
8087 struct stream_in *in = (struct stream_in *)stream;
8088 struct audio_device *adev = in->dev;
8089 ALOGVV("%s", __func__);
8090
8091 lock_input_stream(in);
8092 pthread_mutex_lock(&adev->lock);
8093 int ret = platform_get_active_microphones(adev->platform,
8094 audio_channel_count_from_in_mask(in->channel_mask),
8095 in->usecase, mic_array, mic_count);
8096 pthread_mutex_unlock(&adev->lock);
8097 pthread_mutex_unlock(&in->lock);
8098
8099 return ret;
8100}
8101
8102static int adev_get_microphones(const struct audio_hw_device *dev,
8103 struct audio_microphone_characteristic_t *mic_array,
8104 size_t *mic_count) {
8105 struct audio_device *adev = (struct audio_device *)dev;
8106 ALOGVV("%s", __func__);
8107
8108 pthread_mutex_lock(&adev->lock);
8109 int ret = platform_get_microphones(adev->platform, mic_array, mic_count);
8110 pthread_mutex_unlock(&adev->lock);
8111
8112 return ret;
8113}
juyuchendb308c22019-01-21 11:57:17 -07008114
8115static void in_update_sink_metadata(struct audio_stream_in *stream,
8116 const struct sink_metadata *sink_metadata) {
8117
8118 if (stream == NULL
8119 || sink_metadata == NULL
8120 || sink_metadata->tracks == NULL) {
8121 return;
8122 }
8123
8124 int error = 0;
8125 struct stream_in *in = (struct stream_in *)stream;
8126 struct audio_device *adev = in->dev;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08008127 struct listnode devices;
8128
8129 list_init(&devices);
juyuchendb308c22019-01-21 11:57:17 -07008130
8131 if (sink_metadata->track_count != 0)
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08008132 reassign_device_list(&devices, sink_metadata->tracks->dest_device, "");
juyuchendb308c22019-01-21 11:57:17 -07008133
8134 lock_input_stream(in);
8135 pthread_mutex_lock(&adev->lock);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08008136 ALOGV("%s: in->usecase: %d, device: %x", __func__, in->usecase, get_device_types(&devices));
juyuchendb308c22019-01-21 11:57:17 -07008137
Zhou Song503196b2021-07-23 17:31:05 +08008138 if ((in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY ||
8139 in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY2) &&
8140 !list_empty(&devices) &&
8141 adev->voice_tx_output != NULL) {
juyuchendb308c22019-01-21 11:57:17 -07008142 /* Use the rx device from afe-proxy record to route voice call because
8143 there is no routing if tx device is on primary hal and rx device
8144 is on other hal during voice call. */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08008145 assign_devices(&adev->voice_tx_output->device_list, &devices);
juyuchendb308c22019-01-21 11:57:17 -07008146
8147 if (!voice_is_call_state_active(adev)) {
8148 if (adev->mode == AUDIO_MODE_IN_CALL) {
8149 adev->current_call_output = adev->voice_tx_output;
8150 error = voice_start_call(adev);
8151 if (error != 0)
8152 ALOGE("%s: start voice call failed %d", __func__, error);
8153 }
8154 } else {
8155 adev->current_call_output = adev->voice_tx_output;
8156 voice_update_devices_for_all_voice_usecases(adev);
8157 }
8158 }
8159
Zhenlin Lian4f947842022-05-14 15:50:52 +05308160 clear_devices(&devices);
juyuchendb308c22019-01-21 11:57:17 -07008161 pthread_mutex_unlock(&adev->lock);
8162 pthread_mutex_unlock(&in->lock);
8163}
8164
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05308165int adev_open_output_stream(struct audio_hw_device *dev,
Haynes Mathew George16081042017-05-31 17:16:49 -07008166 audio_io_handle_t handle,
8167 audio_devices_t devices,
8168 audio_output_flags_t flags,
8169 struct audio_config *config,
8170 struct audio_stream_out **stream_out,
Derek Chenf6318be2017-06-12 17:16:24 -04008171 const char *address)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008172{
8173 struct audio_device *adev = (struct audio_device *)dev;
8174 struct stream_out *out;
Gangadhar Sb0210342019-02-22 17:39:41 +05308175 int ret = 0, ip_hdlr_stream = 0, ip_hdlr_dev = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07008176 audio_format_t format;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08008177 struct adsp_hdlr_stream_cfg hdlr_stream_cfg;
Manish Dewangan21a850a2017-08-14 12:03:55 +05308178 bool is_direct_passthough = false;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008179 bool is_hdmi = devices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
8180 bool is_usb_dev = audio_is_usb_out_device(devices) &&
8181 (devices != AUDIO_DEVICE_OUT_USB_ACCESSORY);
8182 bool direct_dev = is_hdmi || is_usb_dev;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008183 bool use_db_as_primary =
vivek mehtaae1018c2019-05-09 12:19:57 -07008184 property_get_bool("vendor.audio.feature.deepbuffer_as_primary.enable",
8185 false);
Vignesh Kulothungana6927272019-02-20 15:17:07 -08008186 bool force_haptic_path =
8187 property_get_bool("vendor.audio.test_haptic", false);
Aniket Kumar Lata23d8cbf2019-04-10 19:54:46 -07008188 bool is_voip_rx = flags & AUDIO_OUTPUT_FLAG_VOIP_RX;
Xiaojun Sang782e5b12020-06-29 21:13:06 +08008189#ifdef AUDIO_GKI_ENABLED
8190 __s32 *generic_dec;
8191#endif
Weiyin Jiang906db3c2021-03-02 13:17:04 +08008192 pthread_mutexattr_t latch_attr;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008193
kunleizdff872d2018-08-20 14:40:33 +08008194 if (is_usb_dev && (!audio_extn_usb_connected(NULL))) {
kunleizd6a9e0c2018-07-30 15:38:52 +08008195 is_usb_dev = false;
8196 devices = AUDIO_DEVICE_OUT_SPEAKER;
8197 ALOGW("%s: ignore set device to non existing USB card, use output device(%#x)",
8198 __func__, devices);
Mingshu Pangdbd20562019-11-25 18:04:39 +08008199 if (config->format == AUDIO_FORMAT_DEFAULT)
8200 config->format = AUDIO_FORMAT_PCM_16_BIT;
8201 if (config->sample_rate == 0)
8202 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
8203 if (config->channel_mask == AUDIO_CHANNEL_NONE)
8204 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
kunleizd6a9e0c2018-07-30 15:38:52 +08008205 }
8206
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008207 *stream_out = NULL;
Naresh Tanniru80659832014-06-04 18:17:56 +05308208
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008209 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
8210
Mingming Yin3a941d42016-02-17 18:08:05 -08008211 ALOGD("%s: enter: format(%#x) sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)\
Derek Chenf6318be2017-06-12 17:16:24 -04008212 stream_handle(%p) address(%s)", __func__, config->format, config->sample_rate, config->channel_mask,
8213 devices, flags, &out->stream, address);
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05308214
8215
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08008216 if (!out) {
8217 return -ENOMEM;
8218 }
8219
Haynes Mathew George204045b2015-02-25 20:32:03 -08008220 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07008221 pthread_mutex_init(&out->pre_lock, (const pthread_mutexattr_t *) NULL);
Weiyin Jiang906db3c2021-03-02 13:17:04 +08008222 pthread_mutexattr_init(&latch_attr);
8223 pthread_mutexattr_settype(&latch_attr, PTHREAD_MUTEX_RECURSIVE);
8224 pthread_mutex_init(&out->latch_lock, &latch_attr);
8225 pthread_mutexattr_destroy(&latch_attr);
Zhou Song48453a02018-01-10 17:50:59 +08008226 pthread_mutex_init(&out->position_query_lock, (const pthread_mutexattr_t *) NULL);
Haynes Mathew George204045b2015-02-25 20:32:03 -08008227 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
8228
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008229 if (devices == AUDIO_DEVICE_NONE)
8230 devices = AUDIO_DEVICE_OUT_SPEAKER;
8231
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008232 out->flags = flags;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08008233 list_init(&out->device_list);
8234 update_device_list(&out->device_list, devices, address, true /* add devices */);
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07008235 out->dev = adev;
Aalique Grahame65780b52017-09-27 14:59:56 -07008236 out->hal_op_format = out->hal_ip_format = format = out->format = config->format;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008237 out->sample_rate = config->sample_rate;
Sachin Mohan Gadag3d09acd2017-06-19 12:43:44 +05308238 out->channel_mask = config->channel_mask;
Ramjee Singh5857aeb2017-08-03 19:18:50 +05308239 if (out->channel_mask == AUDIO_CHANNEL_NONE)
8240 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
8241 else
8242 out->supported_channel_masks[0] = out->channel_mask;
Eric Laurentc4aef752013-09-12 17:45:53 -07008243 out->handle = handle;
Mingming Yin3ee55c62014-08-04 14:23:35 -07008244 out->bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Alexy Josephaa54c872014-12-03 02:46:47 -08008245 out->non_blocking = 0;
Ashish Jain83a6cc22016-06-28 14:34:17 +05308246 out->convert_buffer = NULL;
Ashish Jain1b9b30c2017-05-18 20:57:40 +05308247 out->started = 0;
Zhou Songbaddf9f2020-11-20 13:57:39 +08008248 out->a2dp_muted = false;
Aniket Kumar Lata932f4872017-11-06 18:29:44 -08008249 out->hal_output_suspend_supported = 0;
8250 out->dynamic_pm_qos_config_supported = 0;
Surendar Karkaf51b5842018-04-26 11:28:38 +05308251 out->set_dual_mono = false;
Manisha Agarwal7b3e3772019-02-20 14:33:45 +05308252 out->prev_card_status_offline = false;
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +05308253 out->pspd_coeff_sent = false;
Phil Burkd898ba62019-06-20 12:49:01 -07008254 out->mmap_shared_memory_fd = -1; // not open
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008255
Nikhil Laturkar26b690b2017-07-25 11:06:14 +05308256 if ((flags & AUDIO_OUTPUT_FLAG_BD) &&
Satish Babu Patakokila37e7c482018-02-02 11:50:06 +05308257 (property_get_bool("vendor.audio.matrix.limiter.enable", false)))
Ben Romberger6c4d3812017-06-13 17:46:45 -07008258 platform_set_device_params(out, DEVICE_PARAM_LIMITER_ID, 1);
8259
Aalique Grahame22e49102018-12-18 14:23:57 -08008260 if (direct_dev &&
8261 (audio_is_linear_pcm(out->format) ||
8262 config->format == AUDIO_FORMAT_DEFAULT) &&
8263 out->flags == AUDIO_OUTPUT_FLAG_NONE) {
8264 audio_format_t req_format = config->format;
8265 audio_channel_mask_t req_channel_mask = config->channel_mask;
8266 uint32_t req_sample_rate = config->sample_rate;
8267
8268 pthread_mutex_lock(&adev->lock);
8269 if (is_hdmi) {
8270 ALOGV("AUDIO_DEVICE_OUT_AUX_DIGITAL and DIRECT|OFFLOAD, check hdmi caps");
8271 ret = read_hdmi_sink_caps(out);
8272 if (config->sample_rate == 0)
8273 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
8274 if (config->channel_mask == AUDIO_CHANNEL_NONE)
8275 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
8276 if (config->format == AUDIO_FORMAT_DEFAULT)
8277 config->format = AUDIO_FORMAT_PCM_16_BIT;
8278 } else if (is_usb_dev) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008279 ret = read_usb_sup_params_and_compare(true /*is_playback*/,
8280 &config->format,
8281 &out->supported_formats[0],
8282 MAX_SUPPORTED_FORMATS,
8283 &config->channel_mask,
8284 &out->supported_channel_masks[0],
8285 MAX_SUPPORTED_CHANNEL_MASKS,
8286 &config->sample_rate,
8287 &out->supported_sample_rates[0],
8288 MAX_SUPPORTED_SAMPLE_RATES);
8289 ALOGV("plugged dev USB ret %d", ret);
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008290 }
Aalique Grahame22e49102018-12-18 14:23:57 -08008291
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008292 pthread_mutex_unlock(&adev->lock);
8293 if (ret != 0) {
Mingming Yin3a941d42016-02-17 18:08:05 -08008294 if (ret == -ENOSYS) {
8295 /* ignore and go with default */
8296 ret = 0;
Aalique Grahame22e49102018-12-18 14:23:57 -08008297 }
8298 // For MMAP NO IRQ, allow conversions in ADSP
8299 else if (is_hdmi || (flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) == 0)
8300 goto error_open;
8301 else {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008302 ALOGE("error reading direct dev sink caps");
Mingming Yin3a941d42016-02-17 18:08:05 -08008303 goto error_open;
8304 }
Aalique Grahame22e49102018-12-18 14:23:57 -08008305
8306 if (req_sample_rate != 0 && config->sample_rate != req_sample_rate)
8307 config->sample_rate = req_sample_rate;
8308 if (req_channel_mask != AUDIO_CHANNEL_NONE && config->channel_mask != req_channel_mask)
8309 config->channel_mask = req_channel_mask;
8310 if (req_format != AUDIO_FORMAT_DEFAULT && config->format != req_format)
8311 config->format = req_format;
Mingming Yin3a941d42016-02-17 18:08:05 -08008312 }
Aalique Grahame22e49102018-12-18 14:23:57 -08008313
8314 out->sample_rate = config->sample_rate;
8315 out->channel_mask = config->channel_mask;
8316 out->format = config->format;
8317 if (is_hdmi) {
8318 out->usecase = USECASE_AUDIO_PLAYBACK_HIFI;
8319 out->config = pcm_config_hdmi_multi;
8320 } else if (flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) {
8321 out->usecase = USECASE_AUDIO_PLAYBACK_MMAP;
8322 out->config = pcm_config_mmap_playback;
8323 out->stream.start = out_start;
8324 out->stream.stop = out_stop;
8325 out->stream.create_mmap_buffer = out_create_mmap_buffer;
8326 out->stream.get_mmap_position = out_get_mmap_position;
8327 } else {
8328 out->usecase = USECASE_AUDIO_PLAYBACK_HIFI;
8329 out->config = pcm_config_hifi;
8330 }
8331
8332 out->config.rate = out->sample_rate;
8333 out->config.channels = audio_channel_count_from_out_mask(out->channel_mask);
8334 if (is_hdmi) {
8335 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels *
8336 audio_bytes_per_sample(out->format));
8337 }
8338 out->config.format = pcm_format_from_audio_format(out->format);
Mingming Yin3a941d42016-02-17 18:08:05 -08008339 }
8340
Derek Chenf6318be2017-06-12 17:16:24 -04008341 /* validate bus device address */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08008342 if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_BUS)) {
Derek Chenf6318be2017-06-12 17:16:24 -04008343 /* extract car audio stream index */
8344 out->car_audio_stream =
8345 audio_extn_auto_hal_get_car_audio_stream_from_address(address);
8346 if (out->car_audio_stream < 0) {
8347 ALOGE("%s: invalid car audio stream %x",
8348 __func__, out->car_audio_stream);
8349 ret = -EINVAL;
8350 goto error_open;
8351 }
Derek Chen5f67a942020-02-24 23:08:13 -08008352 ALOGV("%s: car_audio_stream %x", __func__, out->car_audio_stream);
Derek Chenf6318be2017-06-12 17:16:24 -04008353 }
8354
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008355 /* Check for VOIP usecase */
Aniket Kumar Lata23d8cbf2019-04-10 19:54:46 -07008356 if (is_voip_rx) {
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008357 if (!voice_extn_is_compress_voip_supported()) {
8358 if (out->sample_rate == 8000 || out->sample_rate == 16000 ||
8359 out->sample_rate == 32000 || out->sample_rate == 48000) {
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07008360 out->channel_mask = audio_extn_utils_is_vendor_enhanced_fwk() ?
Lakshman Chaluvarajue7fc9482020-05-30 14:29:29 +05308361 config->channel_mask : AUDIO_CHANNEL_OUT_STEREO;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008362 out->usecase = USECASE_AUDIO_PLAYBACK_VOIP;
8363 out->format = AUDIO_FORMAT_PCM_16_BIT;
Aniket Kumar Lata8426d1f2019-06-10 15:25:53 -07008364 out->volume_l = INVALID_OUT_VOLUME;
8365 out->volume_r = INVALID_OUT_VOLUME;
Vikram Panduranga93f080e2017-06-07 18:16:14 -07008366
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008367 out->config = default_pcm_config_voip_copp;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008368 out->config.rate = out->sample_rate;
Aniket Kumar Lata1e344f22019-06-19 17:09:01 -07008369 uint32_t channel_count =
8370 audio_channel_count_from_out_mask(out->channel_mask);
Ramjee Singh968858a2020-08-06 16:30:48 +05308371 out->config.channels = channel_count;
8372
Aniket Kumar Lata1e344f22019-06-19 17:09:01 -07008373 uint32_t buffer_size = get_stream_buffer_size(DEFAULT_VOIP_BUF_DURATION_MS,
8374 out->sample_rate, out->format,
8375 channel_count, false);
8376 uint32_t frame_size = audio_bytes_per_sample(out->format) * channel_count;
8377 if (frame_size != 0)
8378 out->config.period_size = buffer_size / frame_size;
8379 else
8380 ALOGW("%s: frame size is 0 for format %#x", __func__, out->format);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008381 }
8382 } else {
8383 if ((out->dev->mode == AUDIO_MODE_IN_COMMUNICATION ||
8384 voice_extn_compress_voip_is_active(out->dev)) &&
8385 (voice_extn_compress_voip_is_config_supported(config))) {
8386 ret = voice_extn_compress_voip_open_output_stream(out);
8387 if (ret != 0) {
8388 ALOGE("%s: Compress voip output cannot be opened, error:%d",
8389 __func__, ret);
8390 goto error_open;
8391 }
Sujin Panicker19027262019-09-16 18:28:06 +05308392 } else {
8393 out->usecase = GET_USECASE_AUDIO_PLAYBACK_PRIMARY(use_db_as_primary);
8394 out->config = GET_PCM_CONFIG_AUDIO_PLAYBACK_PRIMARY(use_db_as_primary);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008395 }
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08008396 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008397 } else if (audio_is_linear_pcm(out->format) &&
8398 out->flags == AUDIO_OUTPUT_FLAG_NONE && is_usb_dev) {
8399 out->channel_mask = config->channel_mask;
8400 out->sample_rate = config->sample_rate;
8401 out->format = config->format;
8402 out->usecase = USECASE_AUDIO_PLAYBACK_HIFI;
8403 // does this change?
8404 out->config = is_hdmi ? pcm_config_hdmi_multi : pcm_config_hifi;
8405 out->config.rate = config->sample_rate;
8406 out->config.channels = audio_channel_count_from_out_mask(out->channel_mask);
8407 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels *
8408 audio_bytes_per_sample(config->format));
8409 out->config.format = pcm_format_from_audio_format(out->format);
vivek mehta0ea887a2015-08-26 14:01:20 -07008410 } else if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
Dhananjay Kumarac341582017-02-23 23:42:25 +05308411 (out->flags == AUDIO_OUTPUT_FLAG_DIRECT)) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05308412 pthread_mutex_lock(&adev->lock);
8413 bool offline = (adev->card_status == CARD_STATUS_OFFLINE);
8414 pthread_mutex_unlock(&adev->lock);
8415
8416 // reject offload during card offline to allow
8417 // fallback to s/w paths
8418 if (offline) {
8419 ret = -ENODEV;
8420 goto error_open;
8421 }
vivek mehta0ea887a2015-08-26 14:01:20 -07008422
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008423 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
8424 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
8425 ALOGE("%s: Unsupported Offload information", __func__);
8426 ret = -EINVAL;
8427 goto error_open;
8428 }
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07008429
Atul Khare3fa6e542017-08-09 00:56:17 +05308430 if (config->offload_info.format == 0)
8431 config->offload_info.format = config->format;
8432 if (config->offload_info.sample_rate == 0)
8433 config->offload_info.sample_rate = config->sample_rate;
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07008434
Mingming Yin90310102013-11-13 16:57:00 -08008435 if (!is_supported_format(config->offload_info.format) &&
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05308436 !audio_extn_passthru_is_supported_format(config->offload_info.format)) {
vivek mehta0ea887a2015-08-26 14:01:20 -07008437 ALOGE("%s: Unsupported audio format %x " , __func__, config->offload_info.format);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008438 ret = -EINVAL;
8439 goto error_open;
8440 }
8441
Ben Romberger0f8c87b2017-05-24 17:41:11 -07008442 /* TrueHD only supported for 48k multiples (48k, 96k, 192k) */
8443 if ((config->offload_info.format == AUDIO_FORMAT_DOLBY_TRUEHD) &&
8444 (audio_extn_passthru_is_passthrough_stream(out)) &&
8445 !((config->sample_rate == 48000) ||
8446 (config->sample_rate == 96000) ||
8447 (config->sample_rate == 192000))) {
8448 ALOGE("%s: Unsupported sample rate %d for audio format %x",
8449 __func__, config->sample_rate, config->offload_info.format);
8450 ret = -EINVAL;
8451 goto error_open;
8452 }
8453
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008454 out->compr_config.codec = (struct snd_codec *)
8455 calloc(1, sizeof(struct snd_codec));
8456
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07008457 if (!out->compr_config.codec) {
8458 ret = -ENOMEM;
8459 goto error_open;
8460 }
8461
Dhananjay Kumarac341582017-02-23 23:42:25 +05308462 out->stream.pause = out_pause;
8463 out->stream.resume = out_resume;
8464 out->stream.flush = out_flush;
Ashish Jain4847e9d2017-08-17 19:16:57 +05308465 out->stream.set_callback = out_set_callback;
Dhananjay Kumarac341582017-02-23 23:42:25 +05308466 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Mingming Yin21d60472015-09-30 13:56:25 -07008467 out->stream.drain = out_drain;
Dhananjay Kumarac341582017-02-23 23:42:25 +05308468 out->usecase = get_offload_usecase(adev, true /* is_compress */);
vivek mehta446c3962015-09-14 10:57:35 -07008469 ALOGV("Compress Offload usecase .. usecase selected %d", out->usecase);
Dhananjay Kumarac341582017-02-23 23:42:25 +05308470 } else {
8471 out->usecase = get_offload_usecase(adev, false /* is_compress */);
8472 ALOGV("non-offload DIRECT_usecase ... usecase selected %d ", out->usecase);
vivek mehta0ea887a2015-08-26 14:01:20 -07008473 }
vivek mehta446c3962015-09-14 10:57:35 -07008474
Deeraj Somanfa377bf2019-02-06 12:57:59 +05308475 if (out->flags & AUDIO_OUTPUT_FLAG_FAST) {
8476 ALOGD("%s: Setting latency mode to true", __func__);
Meng Wang4c32fb42020-01-16 17:57:11 +08008477#ifdef AUDIO_GKI_ENABLED
8478 /* out->compr_config.codec->reserved[1] is for flags */
8479 out->compr_config.codec->reserved[1] |= audio_extn_utils_get_perf_mode_flag();
8480#else
Deeraj Somanfa377bf2019-02-06 12:57:59 +05308481 out->compr_config.codec->flags |= audio_extn_utils_get_perf_mode_flag();
Meng Wang4c32fb42020-01-16 17:57:11 +08008482#endif
Deeraj Somanfa377bf2019-02-06 12:57:59 +05308483 }
8484
vivek mehta446c3962015-09-14 10:57:35 -07008485 if (out->usecase == USECASE_INVALID) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08008486 if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_AUX_DIGITAL) &&
Mingming Yin3a941d42016-02-17 18:08:05 -08008487 config->format == 0 && config->sample_rate == 0 &&
8488 config->channel_mask == 0) {
Mingming Yin21854652016-04-13 11:54:02 -07008489 ALOGI("%s dummy open to query sink capability",__func__);
Mingming Yin3a941d42016-02-17 18:08:05 -08008490 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
8491 } else {
8492 ALOGE("%s, Max allowed OFFLOAD usecase reached ... ", __func__);
8493 ret = -EEXIST;
8494 goto error_open;
8495 }
vivek mehta446c3962015-09-14 10:57:35 -07008496 }
8497
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008498 if (config->offload_info.channel_mask)
8499 out->channel_mask = config->offload_info.channel_mask;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08008500 else if (config->channel_mask) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008501 out->channel_mask = config->channel_mask;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08008502 config->offload_info.channel_mask = config->channel_mask;
Haynes Mathew Georgea99f7532016-08-24 16:01:21 -07008503 } else {
Dhananjay Kumarac341582017-02-23 23:42:25 +05308504 ALOGE("out->channel_mask not set for OFFLOAD/DIRECT usecase");
Haynes Mathew Georgea99f7532016-08-24 16:01:21 -07008505 ret = -EINVAL;
8506 goto error_open;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08008507 }
Haynes Mathew Georgea99f7532016-08-24 16:01:21 -07008508
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07008509 format = out->format = config->offload_info.format;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008510 out->sample_rate = config->offload_info.sample_rate;
8511
Mingming Yin3ee55c62014-08-04 14:23:35 -07008512 out->bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008513
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05308514 out->compr_config.codec->id = get_snd_codec_id(config->offload_info.format);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05308515 if (audio_extn_utils_is_dolby_format(config->offload_info.format)) {
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05308516 audio_extn_dolby_send_ddp_endp_params(adev);
8517 audio_extn_dolby_set_dmid(adev);
8518 }
vivek mehta0ea887a2015-08-26 14:01:20 -07008519
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008520 out->compr_config.codec->sample_rate =
Ravi Kumar Alamandab91bff32014-11-14 12:05:54 -08008521 config->offload_info.sample_rate;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008522 out->compr_config.codec->bit_rate =
8523 config->offload_info.bit_rate;
8524 out->compr_config.codec->ch_in =
Dhanalakshmi Siddania15c6792016-08-10 15:33:53 +05308525 audio_channel_count_from_out_mask(out->channel_mask);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008526 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
Satish Babu Patakokilaa395a9e2016-11-01 12:18:49 +05308527 /* Update bit width only for non passthrough usecases.
8528 * For passthrough usecases, the output will always be opened @16 bit
8529 */
8530 if (!audio_extn_passthru_is_passthrough_stream(out))
8531 out->bit_width = AUDIO_OUTPUT_BIT_WIDTH;
Naresh Tanniruee3499a2017-01-05 14:05:35 +05308532
8533 if (out->flags & AUDIO_OUTPUT_FLAG_TIMESTAMP)
Meng Wang4c32fb42020-01-16 17:57:11 +08008534#ifdef AUDIO_GKI_ENABLED
8535 /* out->compr_config.codec->reserved[1] is for flags */
8536 out->compr_config.codec->reserved[1] |= COMPRESSED_TIMESTAMP_FLAG;
8537 ALOGVV("%s : out->compr_config.codec->flags -> (%#x) ", __func__, out->compr_config.codec->reserved[1]);
8538#else
Naresh Tanniruee3499a2017-01-05 14:05:35 +05308539 out->compr_config.codec->flags |= COMPRESSED_TIMESTAMP_FLAG;
8540 ALOGVV("%s : out->compr_config.codec->flags -> (%#x) ", __func__, out->compr_config.codec->flags);
Meng Wang4c32fb42020-01-16 17:57:11 +08008541#endif
Naresh Tanniruee3499a2017-01-05 14:05:35 +05308542
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07008543 /*TODO: Do we need to change it for passthrough */
8544 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_RAW;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008545
Manish Dewangana6fc5442015-08-24 20:30:31 +05308546 if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC)
8547 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_RAW;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05308548 else if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS)
Manish Dewangana6fc5442015-08-24 20:30:31 +05308549 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_MP4ADTS;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05308550 else if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_LATM)
8551 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_MP4LATM;
Ashish Jainf1eaa582016-05-23 20:54:24 +05308552
8553 if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) ==
8554 AUDIO_FORMAT_PCM) {
8555
8556 /*Based on platform support, configure appropriate alsa format for corresponding
8557 *hal input format.
8558 */
8559 out->compr_config.codec->format = hal_format_to_alsa(
8560 config->offload_info.format);
8561
Ashish Jain83a6cc22016-06-28 14:34:17 +05308562 out->hal_op_format = alsa_format_to_hal(
Ashish Jainf1eaa582016-05-23 20:54:24 +05308563 out->compr_config.codec->format);
Ashish Jain83a6cc22016-06-28 14:34:17 +05308564 out->hal_ip_format = out->format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05308565
Dhananjay Kumarac341582017-02-23 23:42:25 +05308566 /*for direct non-compress playback populate bit_width based on selected alsa format as
Ashish Jainf1eaa582016-05-23 20:54:24 +05308567 *hal input format and alsa format might differ based on platform support.
8568 */
8569 out->bit_width = audio_bytes_per_sample(
Ashish Jain83a6cc22016-06-28 14:34:17 +05308570 out->hal_op_format) << 3;
Ashish Jainf1eaa582016-05-23 20:54:24 +05308571
8572 out->compr_config.fragments = DIRECT_PCM_NUM_FRAGMENTS;
8573
Deeraj Soman93155a62019-09-30 19:00:37 +05308574 if (property_get_bool("vendor.audio.offload.buffer.duration.enabled", false)) {
8575 if ((config->offload_info.duration_us >= MIN_OFFLOAD_BUFFER_DURATION_MS * 1000) &&
8576 (config->offload_info.duration_us <= MAX_OFFLOAD_BUFFER_DURATION_MS * 1000))
8577 out->info.duration_us = (int64_t)config->offload_info.duration_us;
8578 }
Deeraj Soman65358ab2019-02-07 15:40:49 +05308579
Ashish Jainf1eaa582016-05-23 20:54:24 +05308580 /* Check if alsa session is configured with the same format as HAL input format,
8581 * if not then derive correct fragment size needed to accomodate the
8582 * conversion of HAL input format to alsa format.
8583 */
8584 audio_extn_utils_update_direct_pcm_fragment_size(out);
8585
8586 /*if hal input and output fragment size is different this indicates HAL input format is
8587 *not same as the alsa format
8588 */
Ashish Jain83a6cc22016-06-28 14:34:17 +05308589 if (out->hal_fragment_size != out->compr_config.fragment_size) {
Ashish Jainf1eaa582016-05-23 20:54:24 +05308590 /*Allocate a buffer to convert input data to the alsa configured format.
8591 *size of convert buffer is equal to the size required to hold one fragment size
8592 *worth of pcm data, this is because flinger does not write more than fragment_size
8593 */
Ashish Jain83a6cc22016-06-28 14:34:17 +05308594 out->convert_buffer = calloc(1,out->compr_config.fragment_size);
8595 if (out->convert_buffer == NULL){
Ashish Jainf1eaa582016-05-23 20:54:24 +05308596 ALOGE("Allocation failed for convert buffer for size %d", out->compr_config.fragment_size);
8597 ret = -ENOMEM;
8598 goto error_open;
8599 }
8600 }
8601 } else if (audio_extn_passthru_is_passthrough_stream(out)) {
8602 out->compr_config.fragment_size =
8603 audio_extn_passthru_get_buffer_size(&config->offload_info);
8604 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
8605 } else {
8606 out->compr_config.fragment_size =
8607 platform_get_compress_offload_buffer_size(&config->offload_info);
8608 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
8609 }
Mingming Yin3ee55c62014-08-04 14:23:35 -07008610
Naresh Tanniruee3499a2017-01-05 14:05:35 +05308611 if (out->flags & AUDIO_OUTPUT_FLAG_TIMESTAMP) {
8612 out->compr_config.fragment_size += sizeof(struct snd_codec_metadata);
8613 }
Xiaojun Sang782e5b12020-06-29 21:13:06 +08008614 if (config->offload_info.format == AUDIO_FORMAT_FLAC) {
8615#ifdef AUDIO_GKI_ENABLED
8616 generic_dec =
8617 &(out->compr_config.codec->options.generic.reserved[1]);
8618 ((struct snd_generic_dec_flac *)generic_dec)->sample_size =
8619 AUDIO_OUTPUT_BIT_WIDTH;
8620#else
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +05308621 out->compr_config.codec->options.flac_dec.sample_size = AUDIO_OUTPUT_BIT_WIDTH;
Xiaojun Sang782e5b12020-06-29 21:13:06 +08008622#endif
8623 }
Mingming Yin3ee55c62014-08-04 14:23:35 -07008624
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05308625 if (config->offload_info.format == AUDIO_FORMAT_APTX) {
8626 audio_extn_send_aptx_dec_bt_addr_to_dsp(out);
8627 }
8628
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008629 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
8630 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07008631
Manish Dewangan69426c82017-01-30 17:35:36 +05308632 if ((flags & AUDIO_OUTPUT_FLAG_TIMESTAMP) &&
8633 (flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC)) {
8634 out->render_mode = RENDER_MODE_AUDIO_STC_MASTER;
8635 } else if(flags & AUDIO_OUTPUT_FLAG_TIMESTAMP) {
8636 out->render_mode = RENDER_MODE_AUDIO_MASTER;
8637 } else {
8638 out->render_mode = RENDER_MODE_AUDIO_NO_TIMESTAMP;
8639 }
Alexy Josephaa54c872014-12-03 02:46:47 -08008640
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05308641 memset(&out->channel_map_param, 0,
8642 sizeof(struct audio_out_channel_map_param));
8643
Haynes Mathew George352f27b2013-07-26 00:00:15 -07008644 out->send_new_metadata = 1;
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05308645 out->send_next_track_params = false;
8646 out->is_compr_metadata_avail = false;
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08008647 out->offload_state = OFFLOAD_STATE_IDLE;
8648 out->playback_started = 0;
Zhou Song48453a02018-01-10 17:50:59 +08008649 out->writeAt.tv_sec = 0;
8650 out->writeAt.tv_nsec = 0;
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08008651
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08008652 audio_extn_dts_create_state_notifier_node(out->usecase);
8653
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008654 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
8655 __func__, config->offload_info.version,
8656 config->offload_info.bit_rate);
Ashish Jain5106d362016-05-11 19:23:33 +05308657
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05308658 /* Check if DSD audio format is supported in codec
8659 * and there is no active native DSD use case
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05308660 */
8661
8662 if ((config->format == AUDIO_FORMAT_DSD) &&
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05308663 (!platform_check_codec_dsd_support(adev->platform) ||
8664 audio_is_dsd_native_stream_active(adev))) {
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05308665 ret = -EINVAL;
8666 goto error_open;
8667 }
8668
Ashish Jain5106d362016-05-11 19:23:33 +05308669 /* Disable gapless if any of the following is true
8670 * passthrough playback
8671 * AV playback
Dhananjay Kumarac341582017-02-23 23:42:25 +05308672 * non compressed Direct playback
Ashish Jain5106d362016-05-11 19:23:33 +05308673 */
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05308674 if (audio_extn_passthru_is_passthrough_stream(out) ||
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05308675 (config->format == AUDIO_FORMAT_DSD) ||
Naresh Tanniru928f0862017-04-07 16:44:23 -07008676 (config->format == AUDIO_FORMAT_IEC61937) ||
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05308677 config->offload_info.has_video ||
Dhananjay Kumarac341582017-02-23 23:42:25 +05308678 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Ashish Jain5106d362016-05-11 19:23:33 +05308679 check_and_set_gapless_mode(adev, false);
8680 } else
8681 check_and_set_gapless_mode(adev, true);
Mingming Yin21854652016-04-13 11:54:02 -07008682
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05308683 if (audio_extn_passthru_is_passthrough_stream(out)) {
Mingming Yin21854652016-04-13 11:54:02 -07008684 out->flags |= AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH;
8685 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05308686 if (config->format == AUDIO_FORMAT_DSD) {
8687 out->flags |= AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH;
Meng Wang4c32fb42020-01-16 17:57:11 +08008688#ifdef AUDIO_GKI_ENABLED
8689 /* out->compr_config.codec->reserved[0] is for compr_passthr */
8690 out->compr_config.codec->reserved[0] = PASSTHROUGH_DSD;
8691#else
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05308692 out->compr_config.codec->compr_passthr = PASSTHROUGH_DSD;
Meng Wang4c32fb42020-01-16 17:57:11 +08008693#endif
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05308694 }
Aalique Grahame0359a1f2016-09-08 16:54:22 -07008695
8696 create_offload_callback_thread(out);
8697
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07008698 } else if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
Arun Mirpuri7da752a2018-09-11 18:01:15 -07008699 switch (config->sample_rate) {
8700 case 0:
8701 out->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
8702 break;
8703 case 8000:
8704 case 16000:
8705 case 48000:
8706 out->sample_rate = config->sample_rate;
8707 break;
8708 default:
8709 ALOGE("%s: Unsupported sampling rate %d for Incall Music", __func__,
8710 config->sample_rate);
8711 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
8712 ret = -EINVAL;
8713 goto error_open;
8714 }
8715 //FIXME: add support for MONO stream configuration when audioflinger mixer supports it
8716 switch (config->channel_mask) {
8717 case AUDIO_CHANNEL_NONE:
8718 case AUDIO_CHANNEL_OUT_STEREO:
8719 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
8720 break;
8721 default:
8722 ALOGE("%s: Unsupported channel mask %#x for Incall Music", __func__,
8723 config->channel_mask);
8724 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
8725 ret = -EINVAL;
8726 goto error_open;
8727 }
8728 switch (config->format) {
8729 case AUDIO_FORMAT_DEFAULT:
8730 case AUDIO_FORMAT_PCM_16_BIT:
8731 out->format = AUDIO_FORMAT_PCM_16_BIT;
8732 break;
8733 default:
8734 ALOGE("%s: Unsupported format %#x for Incall Music", __func__,
8735 config->format);
8736 config->format = AUDIO_FORMAT_PCM_16_BIT;
8737 ret = -EINVAL;
8738 goto error_open;
8739 }
8740
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05308741 ret = voice_extn_check_and_set_incall_music_usecase(adev, out);
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07008742 if (ret != 0) {
8743 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
Arun Mirpuri7da752a2018-09-11 18:01:15 -07008744 __func__, ret);
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07008745 goto error_open;
8746 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08008747 } else if (is_single_device_type_equal(&out->device_list,
8748 AUDIO_DEVICE_OUT_TELEPHONY_TX)) {
Aalique Grahame22e49102018-12-18 14:23:57 -08008749 switch (config->sample_rate) {
8750 case 0:
8751 out->sample_rate = AFE_PROXY_SAMPLING_RATE;
8752 break;
8753 case 8000:
8754 case 16000:
8755 case 48000:
8756 out->sample_rate = config->sample_rate;
8757 break;
8758 default:
8759 ALOGE("%s: Unsupported sampling rate %d for Telephony TX", __func__,
8760 config->sample_rate);
8761 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
8762 ret = -EINVAL;
8763 break;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07008764 }
Aalique Grahame22e49102018-12-18 14:23:57 -08008765 //FIXME: add support for MONO stream configuration when audioflinger mixer supports it
8766 switch (config->channel_mask) {
8767 case AUDIO_CHANNEL_NONE:
8768 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
8769 break;
8770 case AUDIO_CHANNEL_OUT_STEREO:
8771 out->channel_mask = config->channel_mask;
8772 break;
8773 default:
8774 ALOGE("%s: Unsupported channel mask %#x for Telephony TX", __func__,
8775 config->channel_mask);
8776 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
8777 ret = -EINVAL;
8778 break;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07008779 }
Aalique Grahame22e49102018-12-18 14:23:57 -08008780 switch (config->format) {
8781 case AUDIO_FORMAT_DEFAULT:
8782 out->format = AUDIO_FORMAT_PCM_16_BIT;
8783 break;
8784 case AUDIO_FORMAT_PCM_16_BIT:
8785 out->format = config->format;
8786 break;
8787 default:
8788 ALOGE("%s: Unsupported format %#x for Telephony TX", __func__,
8789 config->format);
8790 config->format = AUDIO_FORMAT_PCM_16_BIT;
8791 ret = -EINVAL;
8792 break;
8793 }
8794 if (ret != 0)
8795 goto error_open;
8796
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07008797 out->usecase = USECASE_AUDIO_PLAYBACK_AFE_PROXY;
8798 out->config = pcm_config_afe_proxy_playback;
Aalique Grahame22e49102018-12-18 14:23:57 -08008799 out->config.rate = out->sample_rate;
8800 out->config.channels =
8801 audio_channel_count_from_out_mask(out->channel_mask);
8802 out->config.format = pcm_format_from_audio_format(out->format);
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07008803 adev->voice_tx_output = out;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07008804 } else {
Ashish Jain058165c2016-09-28 23:18:48 +05308805 unsigned int channels = 0;
8806 /*Update config params to default if not set by the caller*/
8807 if (config->sample_rate == 0)
8808 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
8809 if (config->channel_mask == AUDIO_CHANNEL_NONE)
8810 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
8811 if (config->format == AUDIO_FORMAT_DEFAULT)
8812 config->format = AUDIO_FORMAT_PCM_16_BIT;
8813
8814 channels = audio_channel_count_from_out_mask(out->channel_mask);
8815
Varun Balaraje49253e2017-07-06 19:48:56 +05308816 if (out->flags & AUDIO_OUTPUT_FLAG_INTERACTIVE) {
8817 out->usecase = get_interactive_usecase(adev);
8818 out->config = pcm_config_low_latency;
8819 } else if (out->flags & AUDIO_OUTPUT_FLAG_RAW) {
Ashish Jain83a6cc22016-06-28 14:34:17 +05308820 out->usecase = USECASE_AUDIO_PLAYBACK_ULL;
Haynes Mathew George5beddd42016-06-27 18:33:40 -07008821 out->realtime = may_use_noirq_mode(adev, USECASE_AUDIO_PLAYBACK_ULL,
8822 out->flags);
8823 out->config = out->realtime ? pcm_config_rt : pcm_config_low_latency;
Haynes Mathew George16081042017-05-31 17:16:49 -07008824 } else if (out->flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) {
8825 out->usecase = USECASE_AUDIO_PLAYBACK_MMAP;
8826 out->config = pcm_config_mmap_playback;
8827 out->stream.start = out_start;
8828 out->stream.stop = out_stop;
8829 out->stream.create_mmap_buffer = out_create_mmap_buffer;
8830 out->stream.get_mmap_position = out_get_mmap_position;
Ashish Jain83a6cc22016-06-28 14:34:17 +05308831 } else if (out->flags & AUDIO_OUTPUT_FLAG_FAST) {
8832 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
Aniket Kumar Lata932f4872017-11-06 18:29:44 -08008833 out->hal_output_suspend_supported =
8834 property_get_bool("vendor.audio.hal.output.suspend.supported", false);
8835 out->dynamic_pm_qos_config_supported =
8836 property_get_bool("vendor.audio.hal.dynamic.qos.config.supported", false);
8837 if (!out->dynamic_pm_qos_config_supported) {
Alexy Joseph98988832017-01-13 14:56:59 -08008838 ALOGI("%s: dynamic qos voting not enabled for platform", __func__);
8839 } else {
8840 ALOGI("%s: dynamic qos voting enabled for platform", __func__);
8841 //the mixer path will be a string similar to "low-latency-playback resume"
8842 strlcpy(out->pm_qos_mixer_path, use_case_table[out->usecase], MAX_MIXER_PATH_LEN);
8843 strlcat(out->pm_qos_mixer_path,
8844 " resume", MAX_MIXER_PATH_LEN);
8845 ALOGI("%s: created %s pm_qos_mixer_path" , __func__,
8846 out->pm_qos_mixer_path);
8847 }
Ashish Jain83a6cc22016-06-28 14:34:17 +05308848 out->config = pcm_config_low_latency;
8849 } else if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
8850 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
8851 out->config = pcm_config_deep_buffer;
Ashish Jain058165c2016-09-28 23:18:48 +05308852 out->config.period_size = get_output_period_size(config->sample_rate, out->format,
8853 channels, DEEP_BUFFER_OUTPUT_PERIOD_DURATION);
8854 if (out->config.period_size <= 0) {
8855 ALOGE("Invalid configuration period size is not valid");
8856 ret = -EINVAL;
8857 goto error_open;
8858 }
Aalique Grahame22e49102018-12-18 14:23:57 -08008859 } else if (flags & AUDIO_OUTPUT_FLAG_TTS) {
8860 out->usecase = USECASE_AUDIO_PLAYBACK_TTS;
8861 out->config = pcm_config_deep_buffer;
Vignesh Kulothungana6927272019-02-20 15:17:07 -08008862 } else if (config->channel_mask & AUDIO_CHANNEL_HAPTIC_ALL) {
8863 out->usecase = USECASE_AUDIO_PLAYBACK_WITH_HAPTICS;
8864 out->config = pcm_config_haptics_audio;
8865 if (force_haptic_path)
8866 adev->haptics_config = pcm_config_haptics_audio;
8867 else
8868 adev->haptics_config = pcm_config_haptics;
8869
Meng Wangd08ce322020-04-02 08:59:20 +08008870 channels =
Vignesh Kulothungana6927272019-02-20 15:17:07 -08008871 audio_channel_count_from_out_mask(out->channel_mask & ~AUDIO_CHANNEL_HAPTIC_ALL);
8872
8873 if (force_haptic_path) {
8874 out->config.channels = 1;
8875 adev->haptics_config.channels = 1;
8876 } else
8877 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 -08008878 } else if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_BUS)) {
Derek Chenf6318be2017-06-12 17:16:24 -04008879 ret = audio_extn_auto_hal_open_output_stream(out);
8880 if (ret) {
8881 ALOGE("%s: Failed to open output stream for bus device", __func__);
8882 ret = -EINVAL;
8883 goto error_open;
8884 }
Ashish Jain83a6cc22016-06-28 14:34:17 +05308885 } else {
8886 /* primary path is the default path selected if no other outputs are available/suitable */
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008887 out->usecase = GET_USECASE_AUDIO_PLAYBACK_PRIMARY(use_db_as_primary);
8888 out->config = GET_PCM_CONFIG_AUDIO_PLAYBACK_PRIMARY(use_db_as_primary);
Ashish Jain83a6cc22016-06-28 14:34:17 +05308889 }
8890 out->hal_ip_format = format = out->format;
8891 out->config.format = hal_format_to_pcm(out->hal_ip_format);
8892 out->hal_op_format = pcm_format_to_hal(out->config.format);
8893 out->bit_width = format_to_bitwidth_table[out->hal_op_format] << 3;
8894 out->config.rate = config->sample_rate;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07008895 out->sample_rate = out->config.rate;
Ashish Jain058165c2016-09-28 23:18:48 +05308896 out->config.channels = channels;
Ashish Jain83a6cc22016-06-28 14:34:17 +05308897 if (out->hal_ip_format != out->hal_op_format) {
8898 uint32_t buffer_size = out->config.period_size *
8899 format_to_bitwidth_table[out->hal_op_format] *
8900 out->config.channels;
8901 out->convert_buffer = calloc(1, buffer_size);
8902 if (out->convert_buffer == NULL){
8903 ALOGE("Allocation failed for convert buffer for size %d",
8904 out->compr_config.fragment_size);
8905 ret = -ENOMEM;
8906 goto error_open;
8907 }
8908 ALOGD("Convert buffer allocated of size %d", buffer_size);
8909 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008910 }
8911
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08008912 ALOGV("%s devices:%d, format:%x, out->sample_rate:%d,out->bit_width:%d out->format:%d out->flags:%x, flags: %x usecase %d",
8913 __func__, devices, format, out->sample_rate, out->bit_width, out->format, out->flags, flags, out->usecase);
Ashish Jain83a6cc22016-06-28 14:34:17 +05308914
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07008915 /* TODO remove this hardcoding and check why width is zero*/
8916 if (out->bit_width == 0)
8917 out->bit_width = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05308918 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07008919 &adev->streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08008920 &out->device_list, out->flags,
8921 out->hal_op_format, out->sample_rate,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +05308922 out->bit_width, out->channel_mask, out->profile,
Manish Dewangan837dc462015-05-27 10:17:41 +05308923 &out->app_type_cfg);
Aalique Grahame6e763712019-01-31 16:18:17 -08008924 if ((out->usecase == (audio_usecase_t)(GET_USECASE_AUDIO_PLAYBACK_PRIMARY(use_db_as_primary))) ||
Haynes Mathew Georgebf143712013-12-03 13:02:53 -08008925 (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
8926 /* Ensure the default output is not selected twice */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08008927 if(adev->primary_output == NULL)
8928 adev->primary_output = out;
8929 else {
8930 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07008931 ret = -EEXIST;
8932 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08008933 }
8934 }
8935
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008936 /* Check if this usecase is already existing */
8937 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella7ce05352014-04-17 20:00:41 -07008938 if ((get_usecase_from_list(adev, out->usecase) != NULL) &&
8939 (out->usecase != USECASE_COMPRESS_VOIP_CALL)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008940 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008941 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07008942 ret = -EEXIST;
8943 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008944 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08008945
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008946 pthread_mutex_unlock(&adev->lock);
8947
8948 out->stream.common.get_sample_rate = out_get_sample_rate;
8949 out->stream.common.set_sample_rate = out_set_sample_rate;
8950 out->stream.common.get_buffer_size = out_get_buffer_size;
8951 out->stream.common.get_channels = out_get_channels;
8952 out->stream.common.get_format = out_get_format;
8953 out->stream.common.set_format = out_set_format;
8954 out->stream.common.standby = out_standby;
8955 out->stream.common.dump = out_dump;
8956 out->stream.common.set_parameters = out_set_parameters;
8957 out->stream.common.get_parameters = out_get_parameters;
8958 out->stream.common.add_audio_effect = out_add_audio_effect;
8959 out->stream.common.remove_audio_effect = out_remove_audio_effect;
8960 out->stream.get_latency = out_get_latency;
8961 out->stream.set_volume = out_set_volume;
Aalique Grahame22e49102018-12-18 14:23:57 -08008962#ifdef NO_AUDIO_OUT
8963 out->stream.write = out_write_for_no_output;
8964#else
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008965 out->stream.write = out_write;
Aalique Grahame22e49102018-12-18 14:23:57 -08008966#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008967 out->stream.get_render_position = out_get_render_position;
8968 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07008969 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008970
Haynes Mathew George16081042017-05-31 17:16:49 -07008971 if (out->realtime)
8972 out->af_period_multiplier = af_period_multiplier;
8973 else
8974 out->af_period_multiplier = 1;
8975
Andy Hunga1f48fa2019-07-01 18:14:53 -07008976 out->kernel_buffer_size = out->config.period_size * out->config.period_count;
8977
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008978 out->standby = 1;
Zhou Songbaddf9f2020-11-20 13:57:39 +08008979 out->volume_l = PLAYBACK_GAIN_MAX;
8980 out->volume_r = PLAYBACK_GAIN_MAX;
Eric Laurenta9024de2013-04-04 09:19:12 -07008981 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07008982 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008983
8984 config->format = out->stream.common.get_format(&out->stream.common);
8985 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
8986 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
Naresh Tanniru04f71882018-06-26 17:46:22 +05308987 register_format(out->format, out->supported_formats);
8988 register_channel_mask(out->channel_mask, out->supported_channel_masks);
8989 register_sample_rate(out->sample_rate, out->supported_sample_rates);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008990
Dechen Chai22768452021-07-30 09:29:16 +05308991#ifndef LINUX_ENABLED
Aalique Grahame22e49102018-12-18 14:23:57 -08008992 out->error_log = error_log_create(
8993 ERROR_LOG_ENTRIES,
8994 1000000000 /* aggregate consecutive identical errors within one second in ns */);
Dechen Chai22768452021-07-30 09:29:16 +05308995#endif
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05308996 /*
8997 By locking output stream before registering, we allow the callback
8998 to update stream's state only after stream's initial state is set to
8999 adev state.
9000 */
9001 lock_output_stream(out);
9002 audio_extn_snd_mon_register_listener(out, out_snd_mon_cb);
9003 pthread_mutex_lock(&adev->lock);
9004 out->card_status = adev->card_status;
9005 pthread_mutex_unlock(&adev->lock);
9006 pthread_mutex_unlock(&out->lock);
9007
Aalique Grahame22e49102018-12-18 14:23:57 -08009008 stream_app_type_cfg_init(&out->app_type_cfg);
9009
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009010 *stream_out = &out->stream;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05309011 ALOGD("%s: Stream (%p) picks up usecase (%s)", __func__, &out->stream,
vivek mehta0ea887a2015-08-26 14:01:20 -07009012 use_case_table[out->usecase]);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08009013
9014 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
9015 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
9016 popcount(out->channel_mask), out->playback_started);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08009017 /* setup a channel for client <--> adsp communication for stream events */
Manish Dewangan21a850a2017-08-14 12:03:55 +05309018 is_direct_passthough = audio_extn_passthru_is_direct_passthrough(out);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08009019 if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
Naresh Tanniru85819452017-05-04 18:55:45 -07009020 (out->flags & AUDIO_OUTPUT_FLAG_DIRECT_PCM) ||
Gangadhar Sb0210342019-02-22 17:39:41 +05309021 audio_extn_ip_hdlr_intf_supported_for_copp(adev->platform) ||
9022 (audio_extn_ip_hdlr_intf_supported(config->format, is_direct_passthough, false))) {
Ben Rombergerd771a7c2017-02-22 18:05:17 -08009023 hdlr_stream_cfg.pcm_device_id = platform_get_pcm_device_id(
9024 out->usecase, PCM_PLAYBACK);
9025 hdlr_stream_cfg.flags = out->flags;
9026 hdlr_stream_cfg.type = PCM_PLAYBACK;
9027 ret = audio_extn_adsp_hdlr_stream_open(&out->adsp_hdlr_stream_handle,
9028 &hdlr_stream_cfg);
9029 if (ret) {
9030 ALOGE("%s: adsp_hdlr_stream_open failed %d",__func__, ret);
9031 out->adsp_hdlr_stream_handle = NULL;
9032 }
9033 }
Gangadhar Sb0210342019-02-22 17:39:41 +05309034 ip_hdlr_stream = audio_extn_ip_hdlr_intf_supported(config->format,
9035 is_direct_passthough, false);
9036 ip_hdlr_dev = audio_extn_ip_hdlr_intf_supported_for_copp(adev->platform);
9037 if (ip_hdlr_stream || ip_hdlr_dev ) {
Vidyakumar Athota2062f912017-06-27 14:46:15 -07009038 ret = audio_extn_ip_hdlr_intf_init(&out->ip_hdlr_handle, NULL, NULL, adev, out->usecase);
Naresh Tanniru85819452017-05-04 18:55:45 -07009039 if (ret < 0) {
9040 ALOGE("%s: audio_extn_ip_hdlr_intf_init failed %d",__func__, ret);
9041 out->ip_hdlr_handle = NULL;
9042 }
9043 }
Derek Chenf939fb72018-11-13 13:34:41 -08009044
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07009045 ret = io_streams_map_insert(adev, &out->stream.common,
9046 out->handle, AUDIO_PATCH_HANDLE_NONE);
9047 if (ret != 0)
9048 goto error_open;
9049
Susan Wang6dd13092021-01-25 10:27:11 -05009050 out->out_ctxt.output = out;
Derek Chenf939fb72018-11-13 13:34:41 -08009051
9052 pthread_mutex_lock(&adev->lock);
Susan Wang6dd13092021-01-25 10:27:11 -05009053 list_add_tail(&adev->active_outputs_list, &out->out_ctxt.list);
Derek Chenf939fb72018-11-13 13:34:41 -08009054 pthread_mutex_unlock(&adev->lock);
9055
Eric Laurent994a6932013-07-17 11:51:42 -07009056 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009057 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07009058
9059error_open:
Ashish Jain83a6cc22016-06-28 14:34:17 +05309060 if (out->convert_buffer)
9061 free(out->convert_buffer);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07009062 free(out);
9063 *stream_out = NULL;
9064 ALOGD("%s: exit: ret %d", __func__, ret);
9065 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009066}
9067
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05309068void adev_close_output_stream(struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009069 struct audio_stream_out *stream)
9070{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07009071 struct stream_out *out = (struct stream_out *)stream;
9072 struct audio_device *adev = out->dev;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08009073 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07009074
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009075 ALOGD("%s: enter:stream_handle(%s)",__func__, use_case_table[out->usecase]);
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05309076
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07009077 io_streams_map_remove(adev, out->handle);
9078
Susan Wang6dd13092021-01-25 10:27:11 -05009079 // remove out_ctxt early to prevent the stream
9080 // being opened in a race condition
9081 pthread_mutex_lock(&adev->lock);
9082 list_remove(&out->out_ctxt.list);
9083 pthread_mutex_unlock(&adev->lock);
9084
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05309085 // must deregister from sndmonitor first to prevent races
9086 // between the callback and close_stream
9087 audio_extn_snd_mon_unregister_listener(out);
9088
Ben Rombergerd771a7c2017-02-22 18:05:17 -08009089 /* close adsp hdrl session before standby */
9090 if (out->adsp_hdlr_stream_handle) {
9091 ret = audio_extn_adsp_hdlr_stream_close(out->adsp_hdlr_stream_handle);
9092 if (ret)
9093 ALOGE("%s: adsp_hdlr_stream_close failed %d",__func__, ret);
9094 out->adsp_hdlr_stream_handle = NULL;
9095 }
9096
Manish Dewangan21a850a2017-08-14 12:03:55 +05309097 if (out->ip_hdlr_handle) {
Naresh Tanniru85819452017-05-04 18:55:45 -07009098 audio_extn_ip_hdlr_intf_deinit(out->ip_hdlr_handle);
9099 out->ip_hdlr_handle = NULL;
9100 }
9101
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08009102 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05309103 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08009104 ret = voice_extn_compress_voip_close_output_stream(&stream->common);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05309105 out->started = 0;
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05309106 pthread_mutex_unlock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08009107 if(ret != 0)
9108 ALOGE("%s: Compress voip output cannot be closed, error:%d",
9109 __func__, ret);
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07009110 } else
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08009111 out_standby(&stream->common);
9112
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07009113 if (is_offload_usecase(out->usecase)) {
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08009114 audio_extn_dts_remove_state_notifier_node(out->usecase);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07009115 destroy_offload_callback_thread(out);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07009116 free_offload_usecase(adev, out->usecase);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07009117 if (out->compr_config.codec != NULL)
9118 free(out->compr_config.codec);
9119 }
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07009120
Zhou Songbaddf9f2020-11-20 13:57:39 +08009121 out->a2dp_muted = false;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05309122
Varun Balaraje49253e2017-07-06 19:48:56 +05309123 if (is_interactive_usecase(out->usecase))
9124 free_interactive_usecase(adev, out->usecase);
9125
Ashish Jain83a6cc22016-06-28 14:34:17 +05309126 if (out->convert_buffer != NULL) {
9127 free(out->convert_buffer);
9128 out->convert_buffer = NULL;
9129 }
9130
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07009131 if (adev->voice_tx_output == out)
9132 adev->voice_tx_output = NULL;
9133
Dechen Chai22768452021-07-30 09:29:16 +05309134#ifndef LINUX_ENABLED
Aalique Grahame22e49102018-12-18 14:23:57 -08009135 error_log_destroy(out->error_log);
9136 out->error_log = NULL;
Dechen Chai22768452021-07-30 09:29:16 +05309137#endif
Dhanalakshmi Siddani6c3d0992017-01-16 16:52:33 +05309138 if (adev->primary_output == out)
9139 adev->primary_output = NULL;
9140
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07009141 pthread_cond_destroy(&out->cond);
9142 pthread_mutex_destroy(&out->lock);
Weiyin Jiang280ea742020-09-08 20:28:22 +08009143 pthread_mutex_destroy(&out->pre_lock);
9144 pthread_mutex_destroy(&out->latch_lock);
9145 pthread_mutex_destroy(&out->position_query_lock);
Derek Chenf939fb72018-11-13 13:34:41 -08009146
9147 pthread_mutex_lock(&adev->lock);
Zhenlin Lian4f947842022-05-14 15:50:52 +05309148 clear_devices(&out->device_list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009149 free(stream);
Derek Chenf939fb72018-11-13 13:34:41 -08009150 pthread_mutex_unlock(&adev->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07009151 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009152}
9153
Shubhasini Sugumaran3fde6582022-01-19 15:54:50 -05009154void in_set_power_policy(uint8_t enable)
9155{
9156 struct listnode *node;
9157
9158 ALOGD("%s: Enter, state %d", __func__, enable);
9159
9160 pthread_mutex_lock(&adev->lock);
9161 adev->in_power_policy = enable ? POWER_POLICY_STATUS_ONLINE : POWER_POLICY_STATUS_OFFLINE;
9162 pthread_mutex_unlock(&adev->lock);
9163
9164 if (!enable) {
9165 list_for_each(node, &adev->active_inputs_list) {
9166 streams_input_ctxt_t *in_ctxt = node_to_item(node,
9167 streams_input_ctxt_t,
9168 list);
9169 struct stream_in *in = in_ctxt->input;
9170 in_standby(&in->stream.common);
9171 }
9172 }
9173
9174 ALOGD("%s: Exit", __func__);
9175}
9176
9177void out_set_power_policy(uint8_t enable)
9178{
9179 struct listnode *node;
9180
9181 ALOGD("%s: Enter, state %d", __func__, enable);
9182
9183 pthread_mutex_lock(&adev->lock);
E V Ravi317be872022-02-23 19:08:15 +05309184 adev->out_power_policy = enable ? POWER_POLICY_STATUS_ONLINE : POWER_POLICY_STATUS_OFFLINE;
Shubhasini Sugumaran3fde6582022-01-19 15:54:50 -05009185 pthread_mutex_unlock(&adev->lock);
9186
9187 if (!enable) {
9188 list_for_each(node, &adev->active_outputs_list) {
9189 streams_output_ctxt_t *out_ctxt = node_to_item(node,
9190 streams_output_ctxt_t,
9191 list);
9192 struct stream_out *out = out_ctxt->output;
9193 out_on_error(&out->stream.common);
9194 }
9195 }
9196
9197 ALOGD("%s: Exit", __func__);
9198}
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009199static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
9200{
9201 struct audio_device *adev = (struct audio_device *)dev;
9202 struct str_parms *parms;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009203 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07009204 int val;
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07009205 int ret;
9206 int status = 0;
Aalique Grahame22e49102018-12-18 14:23:57 -08009207 bool a2dp_reconfig = false;
Zhou Songd6d71752019-05-21 18:08:51 +08009208 struct listnode *node;
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07009209 int controller = -1, stream = -1;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009210
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08009211 ALOGD("%s: enter: %s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009212 parms = str_parms_create_str(kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009213
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05309214 if (!parms)
9215 goto error;
Naresh Tanniru4c630392014-05-12 01:05:52 +05309216
Derek Chen6f293672019-04-01 01:40:24 -07009217 /* notify adev and input/output streams on the snd card status */
9218 adev_snd_mon_cb((void *)adev, parms);
9219
Weiyin Jiang24f55292020-12-22 14:35:46 +08009220 ret = str_parms_get_str(parms, "SND_CARD_STATUS", value, sizeof(value));
9221 if (ret >= 0) {
9222 list_for_each(node, &adev->active_outputs_list) {
9223 streams_output_ctxt_t *out_ctxt = node_to_item(node,
9224 streams_output_ctxt_t,
9225 list);
9226 out_snd_mon_cb((void *)out_ctxt->output, parms);
9227 }
Derek Chen6f293672019-04-01 01:40:24 -07009228
Weiyin Jiang24f55292020-12-22 14:35:46 +08009229 list_for_each(node, &adev->active_inputs_list) {
9230 streams_input_ctxt_t *in_ctxt = node_to_item(node,
9231 streams_input_ctxt_t,
9232 list);
9233 in_snd_mon_cb((void *)in_ctxt->input, parms);
9234 }
Derek Chen6f293672019-04-01 01:40:24 -07009235 }
9236
Zhou Songd6d71752019-05-21 18:08:51 +08009237 pthread_mutex_lock(&adev->lock);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05309238 ret = str_parms_get_str(parms, "BT_SCO", value, sizeof(value));
9239 if (ret >= 0) {
9240 /* When set to false, HAL should disable EC and NS */
Zhou Songd6d71752019-05-21 18:08:51 +08009241 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0){
Ashish Jain1b9b30c2017-05-18 20:57:40 +05309242 adev->bt_sco_on = true;
Shalini Manjunatha3a2e06e2021-02-01 12:23:49 +05309243 /*
9244 * When ever BT_SCO=ON arrives, make sure to route
9245 * all use cases to SCO device, otherwise due to delay in
9246 * BT_SCO=ON and lack of synchronization with create audio patch
9247 * request for SCO device, some times use case not routed properly to
9248 * SCO device
9249 */
9250 struct audio_usecase *usecase;
9251 struct listnode *node;
9252 list_for_each(node, &adev->usecase_list) {
9253 usecase = node_to_item(node, struct audio_usecase, list);
Anver sadhiquef0efca32021-09-03 15:25:44 +05309254 if (usecase->stream.in && (usecase->type == PCM_CAPTURE) &&
Anver sadhique93f3c912021-08-11 11:19:45 +05309255 (!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 +05309256 ALOGD("BT_SCO ON, switch all in use case to it");
9257 select_devices(adev, usecase->id);
9258 }
Mingshu Pangef517202021-04-22 10:35:00 +08009259 if (usecase->stream.out && (usecase->type == PCM_PLAYBACK ||
9260 usecase->type == VOICE_CALL) &&
Anver sadhique93f3c912021-08-11 11:19:45 +05309261 (!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 +05309262 ALOGD("BT_SCO ON, switch all out use case to it");
9263 select_devices(adev, usecase->id);
9264 }
9265 }
9266 }
9267 else {
Ashish Jain1b9b30c2017-05-18 20:57:40 +05309268 adev->bt_sco_on = false;
Zhou Songd6d71752019-05-21 18:08:51 +08009269 audio_extn_sco_reset_configuration();
Kunlei Zhangd96af8f2019-08-27 16:33:29 +08009270 }
9271 }
9272
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07009273 status = voice_set_parameters(adev, parms);
9274 if (status != 0)
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08009275 goto done;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009276
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07009277 status = platform_set_parameters(adev->platform, parms);
9278 if (status != 0)
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08009279 goto done;
9280
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07009281 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
9282 if (ret >= 0) {
Vicky Sehrawate240e5d2014-08-12 17:17:04 -07009283 /* When set to false, HAL should disable EC and NS */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009284 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
9285 adev->bluetooth_nrec = true;
9286 else
9287 adev->bluetooth_nrec = false;
9288 }
9289
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07009290 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
9291 if (ret >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009292 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
9293 adev->screen_off = false;
9294 else
9295 adev->screen_off = true;
Quinn Male70f20f32019-06-26 16:50:26 -07009296 audio_extn_sound_trigger_update_screen_status(adev->screen_off);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009297 }
9298
Eric Laurent4b084132018-10-19 17:33:43 -07009299 ret = str_parms_get_int(parms, "rotation", &val);
9300 if (ret >= 0) {
9301 bool reverse_speakers = false;
9302 int camera_rotation = CAMERA_ROTATION_LANDSCAPE;
9303 switch (val) {
9304 // FIXME: note that the code below assumes that the speakers are in the correct placement
9305 // relative to the user when the device is rotated 90deg from its default rotation. This
9306 // assumption is device-specific, not platform-specific like this code.
9307 case 270:
9308 reverse_speakers = true;
9309 camera_rotation = CAMERA_ROTATION_INVERT_LANDSCAPE;
9310 break;
9311 case 0:
9312 case 180:
9313 camera_rotation = CAMERA_ROTATION_PORTRAIT;
9314 break;
9315 case 90:
9316 camera_rotation = CAMERA_ROTATION_LANDSCAPE;
9317 break;
9318 default:
9319 ALOGE("%s: unexpected rotation of %d", __func__, val);
9320 status = -EINVAL;
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07009321 }
Eric Laurent4b084132018-10-19 17:33:43 -07009322 if (status == 0) {
9323 // check and set swap
9324 // - check if orientation changed and speaker active
9325 // - set rotation and cache the rotation value
9326 adev->camera_orientation =
9327 (adev->camera_orientation & ~CAMERA_ROTATION_MASK) | camera_rotation;
9328 if (!audio_extn_is_maxx_audio_enabled())
9329 platform_check_and_set_swap_lr_channels(adev, reverse_speakers);
9330 }
9331 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07009332
Mingming Yin514a8bc2014-07-29 15:22:21 -07009333 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_SCO_WB, value, sizeof(value));
9334 if (ret >= 0) {
9335 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
9336 adev->bt_wb_speech_enabled = true;
9337 else
9338 adev->bt_wb_speech_enabled = false;
9339 }
9340
Zhou Song12c29502019-03-16 10:37:18 +08009341 ret = str_parms_get_str(parms, "bt_swb", value, sizeof(value));
9342 if (ret >= 0) {
9343 val = atoi(value);
9344 adev->swb_speech_mode = val;
9345 }
9346
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -07009347 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value, sizeof(value));
9348 if (ret >= 0) {
9349 val = atoi(value);
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05309350 audio_devices_t device = (audio_devices_t) val;
Zhou Song681350a2017-10-19 16:28:42 +08009351 if (audio_is_output_device(val) &&
9352 (val & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07009353 ALOGV("cache new ext disp type and edid");
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07009354 platform_get_controller_stream_from_params(parms, &controller, &stream);
9355 platform_set_ext_display_device_v2(adev->platform, controller, stream);
9356 ret = platform_get_ext_disp_type_v2(adev->platform, controller, stream);
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07009357 if (ret < 0) {
9358 ALOGE("%s: Failed to query disp type, ret:%d", __func__, ret);
Manisha Agarwal2f5ff882018-08-08 17:09:29 +05309359 } else {
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07009360 platform_cache_edid_v2(adev->platform, controller, stream);
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07009361 }
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05309362 } else if (audio_is_usb_out_device(device) || audio_is_usb_in_device(device)) {
vivek mehta344576a2016-04-12 18:56:03 -07009363 /*
9364 * Do not allow AFE proxy port usage by WFD source when USB headset is connected.
9365 * Per AudioPolicyManager, USB device is higher priority than WFD.
9366 * For Voice call over USB headset, voice call audio is routed to AFE proxy ports.
9367 * If WFD use case occupies AFE proxy, it may result unintended behavior while
9368 * starting voice call on USB
9369 */
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08009370 ret = str_parms_get_str(parms, "card", value, sizeof(value));
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05309371 if (ret >= 0)
9372 audio_extn_usb_add_device(device, atoi(value));
9373
Zhou Song6f862822017-11-06 17:27:57 +08009374 if (!audio_extn_usb_is_tunnel_supported()) {
9375 ALOGV("detected USB connect .. disable proxy");
9376 adev->allow_afe_proxy_usage = false;
9377 }
Zhou Song503196b2021-07-23 17:31:05 +08009378 } else if (audio_is_hearing_aid_out_device(device) &&
9379 property_get_bool("persist.vendor.audio.ha_proxy.enabled", false)) {
9380 adev->ha_proxy_enable = true;
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -07009381 }
9382 }
9383
9384 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value, sizeof(value));
9385 if (ret >= 0) {
9386 val = atoi(value);
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05309387 audio_devices_t device = (audio_devices_t) val;
Garmond Leunge3b6d482016-10-25 16:48:01 -07009388 /*
9389 * The HDMI / Displayport disconnect handling has been moved to
9390 * audio extension to ensure that its parameters are not
9391 * invalidated prior to updating sysfs of the disconnect event
9392 * Invalidate will be handled by audio_extn_ext_disp_set_parameters()
9393 */
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05309394 if (audio_is_usb_out_device(device) || audio_is_usb_in_device(device)) {
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08009395 ret = str_parms_get_str(parms, "card", value, sizeof(value));
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05309396 if (ret >= 0)
9397 audio_extn_usb_remove_device(device, atoi(value));
9398
Zhou Song6f862822017-11-06 17:27:57 +08009399 if (!audio_extn_usb_is_tunnel_supported()) {
9400 ALOGV("detected USB disconnect .. enable proxy");
9401 adev->allow_afe_proxy_usage = true;
9402 }
Zhou Song503196b2021-07-23 17:31:05 +08009403 } else if (audio_is_hearing_aid_out_device(device)) {
9404 adev->ha_proxy_enable = false;
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -07009405 }
9406 }
9407
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08009408 audio_extn_qdsp_set_parameters(adev, parms);
Aalique Grahame22e49102018-12-18 14:23:57 -08009409
9410 status = audio_extn_a2dp_set_parameters(parms, &a2dp_reconfig);
Aniket Kumar Lata23300322019-02-20 22:25:30 -08009411 if (status >= 0 && a2dp_reconfig) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05309412 struct audio_usecase *usecase;
9413 struct listnode *node;
9414 list_for_each(node, &adev->usecase_list) {
9415 usecase = node_to_item(node, struct audio_usecase, list);
Weiyin Jiangff56ff32020-05-08 14:32:21 +08009416 if ((usecase->stream.out == NULL) || (usecase->type != PCM_PLAYBACK))
9417 continue;
9418
9419 if (is_a2dp_out_device_type(&usecase->device_list)) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05309420 ALOGD("reconfigure a2dp... forcing device switch");
Naresh Tannirucd2353e2016-08-19 00:37:25 +05309421 audio_extn_a2dp_set_handoff_mode(true);
Manisha Agarwalf1c3b6b2019-06-25 13:00:33 +05309422 ALOGD("Switching to speaker and muting the stream before select_devices");
9423 check_a2dp_restore_l(adev, usecase->stream.out, false);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05309424 //force device switch to re configure encoder
9425 select_devices(adev, usecase->id);
Manisha Agarwalf1c3b6b2019-06-25 13:00:33 +05309426 ALOGD("Unmuting the stream after select_devices");
Zhou Songbaddf9f2020-11-20 13:57:39 +08009427 check_a2dp_restore_l(adev, usecase->stream.out, true);
Naresh Tannirucd2353e2016-08-19 00:37:25 +05309428 audio_extn_a2dp_set_handoff_mode(false);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05309429 break;
Zhou Songd01e7a22020-09-23 22:49:01 +08009430 } else if (is_offload_usecase(usecase->stream.out->usecase)) {
Weiyin Jiang280ea742020-09-08 20:28:22 +08009431 pthread_mutex_lock(&usecase->stream.out->latch_lock);
Zhou Songbaddf9f2020-11-20 13:57:39 +08009432 if (usecase->stream.out->a2dp_muted) {
Weiyin Jiang280ea742020-09-08 20:28:22 +08009433 pthread_mutex_unlock(&usecase->stream.out->latch_lock);
9434 reassign_device_list(&usecase->stream.out->device_list,
9435 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP, "");
9436 check_a2dp_restore_l(adev, usecase->stream.out, true);
9437 break;
9438 }
9439 pthread_mutex_unlock(&usecase->stream.out->latch_lock);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05309440 }
9441 }
9442 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08009443
9444 //handle vr audio setparam
9445 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_VR_AUDIO_MODE,
9446 value, sizeof(value));
9447 if (ret >= 0) {
9448 ALOGI("Setting vr mode to be %s", value);
9449 if (!strncmp(value, "true", 4)) {
9450 adev->vr_audio_mode_enabled = true;
9451 ALOGI("Setting vr mode to true");
9452 } else if (!strncmp(value, "false", 5)) {
9453 adev->vr_audio_mode_enabled = false;
9454 ALOGI("Setting vr mode to false");
9455 } else {
9456 ALOGI("wrong vr mode set");
9457 }
9458 }
9459
Eric Laurent4b084132018-10-19 17:33:43 -07009460 //FIXME: to be replaced by proper video capture properties API
9461 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_CAMERA_FACING, value, sizeof(value));
9462 if (ret >= 0) {
9463 int camera_facing = CAMERA_FACING_BACK;
9464 if (strcmp(value, AUDIO_PARAMETER_VALUE_FRONT) == 0)
9465 camera_facing = CAMERA_FACING_FRONT;
9466 else if (strcmp(value, AUDIO_PARAMETER_VALUE_BACK) == 0)
9467 camera_facing = CAMERA_FACING_BACK;
9468 else {
9469 ALOGW("%s: invalid camera facing value: %s", __func__, value);
9470 goto done;
9471 }
9472 adev->camera_orientation =
9473 (adev->camera_orientation & ~CAMERA_FACING_MASK) | camera_facing;
9474 struct audio_usecase *usecase;
9475 struct listnode *node;
9476 list_for_each(node, &adev->usecase_list) {
9477 usecase = node_to_item(node, struct audio_usecase, list);
9478 struct stream_in *in = usecase->stream.in;
9479 if (usecase->type == PCM_CAPTURE && in != NULL &&
9480 in->source == AUDIO_SOURCE_CAMCORDER && !in->standby) {
9481 select_devices(adev, in->usecase);
9482 }
9483 }
9484 }
9485
Daniel Hillenbrand17873952013-05-23 10:10:00 +05309486 amplifier_set_parameters(parms);
Tahir Dawson7fabad42022-06-21 12:37:55 -04009487 audio_extn_auto_hal_set_parameters(adev, parms);
Naresh Tannirucd2353e2016-08-19 00:37:25 +05309488 audio_extn_set_parameters(adev, parms);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08009489done:
9490 str_parms_destroy(parms);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08009491 pthread_mutex_unlock(&adev->lock);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05309492error:
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07009493 ALOGV("%s: exit with code(%d)", __func__, status);
9494 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009495}
9496
9497static char* adev_get_parameters(const struct audio_hw_device *dev,
9498 const char *keys)
9499{
Sidipotu Ashokaa4fa6a2018-12-21 09:19:26 +05309500 ALOGD("%s:%s", __func__, keys);
9501
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07009502 struct audio_device *adev = (struct audio_device *)dev;
9503 struct str_parms *reply = str_parms_create();
9504 struct str_parms *query = str_parms_create_str(keys);
9505 char *str;
Naresh Tannirud7205b62014-06-20 02:54:48 +05309506 char value[256] = {0};
9507 int ret = 0;
9508
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07009509 if (!query || !reply) {
Alexy Josephaee4fdd2016-01-29 13:02:07 -08009510 if (reply) {
9511 str_parms_destroy(reply);
9512 }
9513 if (query) {
9514 str_parms_destroy(query);
9515 }
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07009516 ALOGE("adev_get_parameters: failed to create query or reply");
9517 return NULL;
9518 }
9519
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08009520 //handle vr audio getparam
9521
9522 ret = str_parms_get_str(query,
9523 AUDIO_PARAMETER_KEY_VR_AUDIO_MODE,
9524 value, sizeof(value));
9525
9526 if (ret >= 0) {
9527 bool vr_audio_enabled = false;
9528 pthread_mutex_lock(&adev->lock);
9529 vr_audio_enabled = adev->vr_audio_mode_enabled;
9530 pthread_mutex_unlock(&adev->lock);
9531
Aniket Kumar Latad13758f2020-08-06 15:11:36 -07009532 ALOGV("getting vr mode to %d", vr_audio_enabled);
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08009533
9534 if (vr_audio_enabled) {
9535 str_parms_add_str(reply, AUDIO_PARAMETER_KEY_VR_AUDIO_MODE,
9536 "true");
9537 goto exit;
9538 } else {
9539 str_parms_add_str(reply, AUDIO_PARAMETER_KEY_VR_AUDIO_MODE,
9540 "false");
9541 goto exit;
9542 }
9543 }
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07009544
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08009545 pthread_mutex_lock(&adev->lock);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07009546 audio_extn_get_parameters(adev, query, reply);
Shiv Maliyappanahallif9308492013-12-12 12:18:09 -08009547 voice_get_parameters(adev, query, reply);
Aalique Grahame22e49102018-12-18 14:23:57 -08009548 audio_extn_a2dp_get_parameters(query, reply);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07009549 platform_get_parameters(adev->platform, query, reply);
justinwengd5395152019-11-04 12:23:09 +08009550 audio_extn_ma_get_parameters(adev, query, reply);
Naresh Tanniru80659832014-06-04 18:17:56 +05309551 pthread_mutex_unlock(&adev->lock);
9552
Naresh Tannirud7205b62014-06-20 02:54:48 +05309553exit:
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07009554 str = str_parms_to_str(reply);
9555 str_parms_destroy(query);
9556 str_parms_destroy(reply);
9557
Aniket Kumar Latad13758f2020-08-06 15:11:36 -07009558 ALOGV("%s: exit: returns - %s", __func__, str);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07009559 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009560}
9561
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07009562static int adev_init_check(const struct audio_hw_device *dev __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009563{
9564 return 0;
9565}
9566
9567static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
9568{
Haynes Mathew George5191a852013-09-11 14:19:36 -07009569 int ret;
9570 struct audio_device *adev = (struct audio_device *)dev;
Aalique Grahame22e49102018-12-18 14:23:57 -08009571
9572 audio_extn_extspk_set_voice_vol(adev->extspk, volume);
9573
Haynes Mathew George5191a852013-09-11 14:19:36 -07009574 pthread_mutex_lock(&adev->lock);
9575 /* cache volume */
Shruthi Krishnaace10852013-10-25 14:32:12 -07009576 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07009577 pthread_mutex_unlock(&adev->lock);
9578 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009579}
9580
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07009581static int adev_set_master_volume(struct audio_hw_device *dev __unused,
9582 float volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009583{
9584 return -ENOSYS;
9585}
9586
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07009587static int adev_get_master_volume(struct audio_hw_device *dev __unused,
9588 float *volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009589{
9590 return -ENOSYS;
9591}
9592
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07009593static int adev_set_master_mute(struct audio_hw_device *dev __unused,
9594 bool muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009595{
9596 return -ENOSYS;
9597}
9598
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07009599static int adev_get_master_mute(struct audio_hw_device *dev __unused,
9600 bool *muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009601{
9602 return -ENOSYS;
9603}
9604
9605static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
9606{
9607 struct audio_device *adev = (struct audio_device *)dev;
Garmond Leung5fd0b552018-04-17 11:56:12 -07009608 struct listnode *node;
9609 struct audio_usecase *usecase = NULL;
9610 int ret = 0;
kunleizdc4af9d2017-05-04 12:15:35 +08009611
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009612 pthread_mutex_lock(&adev->lock);
9613 if (adev->mode != mode) {
Jaideep Sharma477917f2020-03-13 18:13:33 +05309614 ALOGD("%s: mode %d , prev_mode %d \n", __func__, mode , adev->mode);
9615 adev->prev_mode = adev->mode; /* prev_mode is kept to handle voip concurrency*/
Daniel Hillenbrand17873952013-05-23 10:10:00 +05309616 if (amplifier_set_mode(mode) != 0)
9617 ALOGE("Failed setting amplifier mode");
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009618 adev->mode = mode;
Weiyin Jiang96b96f72020-09-22 16:48:19 +08009619 if (mode == AUDIO_MODE_CALL_SCREEN) {
Jaideep Sharma477917f2020-03-13 18:13:33 +05309620 adev->current_call_output = adev->primary_output;
9621 voice_start_call(adev);
9622 } else if (voice_is_in_call_or_call_screen(adev) &&
Kunlei Zhang1d5c7f22019-05-21 14:25:57 +08009623 (mode == AUDIO_MODE_NORMAL ||
9624 (mode == AUDIO_MODE_IN_COMMUNICATION && !voice_is_call_state_active(adev)))) {
Garmond Leung5fd0b552018-04-17 11:56:12 -07009625 list_for_each(node, &adev->usecase_list) {
9626 usecase = node_to_item(node, struct audio_usecase, list);
9627 if (usecase->type == VOICE_CALL)
9628 break;
9629 }
9630 if (usecase &&
9631 audio_is_usb_out_device(usecase->out_snd_device & AUDIO_DEVICE_OUT_ALL_USB)) {
9632 ret = audio_extn_usb_check_and_set_svc_int(usecase,
9633 true);
9634 if (ret != 0) {
9635 /* default service interval was successfully updated,
9636 reopen USB backend with new service interval */
9637 check_usecases_codec_backend(adev,
9638 usecase,
9639 usecase->out_snd_device);
9640 }
9641 }
9642
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07009643 voice_stop_call(adev);
Banajit Goswami20cdd212015-09-11 01:11:30 -07009644 platform_set_gsm_mode(adev->platform, false);
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07009645 adev->current_call_output = NULL;
kunleizdc4af9d2017-05-04 12:15:35 +08009646 // restore device for other active usecases after stop call
9647 list_for_each(node, &adev->usecase_list) {
9648 usecase = node_to_item(node, struct audio_usecase, list);
9649 select_devices(adev, usecase->id);
9650 }
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07009651 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009652 }
9653 pthread_mutex_unlock(&adev->lock);
9654 return 0;
9655}
9656
9657static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
9658{
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08009659 int ret;
Aalique Grahame22e49102018-12-18 14:23:57 -08009660 struct audio_device *adev = (struct audio_device *)dev;
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08009661
9662 pthread_mutex_lock(&adev->lock);
Vidyakumar Athota2850d532013-11-19 16:02:12 -08009663 ALOGD("%s state %d\n", __func__, state);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08009664 ret = voice_set_mic_mute((struct audio_device *)dev, state);
Aalique Grahame22e49102018-12-18 14:23:57 -08009665
Derek Chend2530072014-11-24 12:39:14 -08009666 if (adev->ext_hw_plugin)
9667 ret = audio_extn_ext_hw_plugin_set_mic_mute(adev->ext_hw_plugin, state);
Aalique Grahame22e49102018-12-18 14:23:57 -08009668
9669 adev->mic_muted = state;
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08009670 pthread_mutex_unlock(&adev->lock);
9671
9672 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009673}
9674
9675static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
9676{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07009677 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009678 return 0;
9679}
9680
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07009681static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009682 const struct audio_config *config)
9683{
Avinash Chandrad7296d42021-08-04 15:07:47 +05309684 bool is_usb_hifi = IS_USB_HIFI;
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07009685 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009686
Aalique Grahame22e49102018-12-18 14:23:57 -08009687 /* Don't know if USB HIFI in this context so use true to be conservative */
9688 if (check_input_parameters(config->sample_rate, config->format, channel_count,
Avinash Chandrad7296d42021-08-04 15:07:47 +05309689 is_usb_hifi) != 0)
Aalique Grahame22e49102018-12-18 14:23:57 -08009690 return 0;
9691
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07009692 return get_input_buffer_size(config->sample_rate, config->format, channel_count,
9693 false /* is_low_latency: since we don't know, be conservative */);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009694}
9695
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009696static bool adev_input_allow_hifi_record(struct audio_device *adev,
9697 audio_devices_t devices,
9698 audio_input_flags_t flags,
9699 audio_source_t source) {
9700 const bool allowed = true;
9701
9702 if (!audio_is_usb_in_device(devices))
9703 return !allowed;
9704
9705 switch (flags) {
9706 case AUDIO_INPUT_FLAG_NONE:
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009707 break;
Haynes Mathew George59862182017-10-24 16:23:57 -07009708 case AUDIO_INPUT_FLAG_FAST: // disallow hifi record for FAST as
9709 // it affects RTD numbers over USB
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009710 default:
9711 return !allowed;
9712 }
9713
9714 switch (source) {
9715 case AUDIO_SOURCE_DEFAULT:
9716 case AUDIO_SOURCE_MIC:
9717 case AUDIO_SOURCE_UNPROCESSED:
9718 break;
9719 default:
9720 return !allowed;
9721 }
9722
9723 switch (adev->mode) {
9724 case 0:
9725 break;
9726 default:
9727 return !allowed;
9728 }
9729
9730 return allowed;
9731}
9732
Haynes Mathew George4ffef292017-11-21 15:08:02 -08009733static int adev_update_voice_comm_input_stream(struct stream_in *in,
9734 struct audio_config *config)
9735{
9736 bool valid_rate = (config->sample_rate == 8000 ||
9737 config->sample_rate == 16000 ||
9738 config->sample_rate == 32000 ||
9739 config->sample_rate == 48000);
9740 bool valid_ch = audio_channel_count_from_in_mask(in->channel_mask) == 1;
9741
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08009742 if(!voice_extn_is_compress_voip_supported()) {
kunleiz28c73e72019-03-27 17:24:04 +08009743 if (valid_rate && valid_ch) {
Haynes Mathew George4ffef292017-11-21 15:08:02 -08009744 in->usecase = USECASE_AUDIO_RECORD_VOIP;
9745 in->config = default_pcm_config_voip_copp;
9746 in->config.period_size = VOIP_IO_BUF_SIZE(in->sample_rate,
9747 DEFAULT_VOIP_BUF_DURATION_MS,
9748 DEFAULT_VOIP_BIT_DEPTH_BYTE)/2;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08009749 } else {
9750 ALOGW("%s No valid input in voip, use defaults"
9751 "sample rate %u, channel mask 0x%X",
9752 __func__, config->sample_rate, in->channel_mask);
9753 }
Haynes Mathew George4ffef292017-11-21 15:08:02 -08009754 in->config.rate = config->sample_rate;
9755 in->sample_rate = config->sample_rate;
9756 } else {
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08009757 //XXX needed for voice_extn_compress_voip_open_input_stream
9758 in->config.rate = config->sample_rate;
9759 if ((in->dev->mode == AUDIO_MODE_IN_COMMUNICATION ||
Shalini Manjunathaa763cc42019-08-23 15:13:46 +05309760 in->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08009761 voice_extn_compress_voip_is_active(in->dev)) &&
9762 (voice_extn_compress_voip_is_format_supported(in->format)) &&
9763 valid_rate && valid_ch) {
9764 voice_extn_compress_voip_open_input_stream(in);
9765 // update rate entries to match config from AF
9766 in->config.rate = config->sample_rate;
9767 in->sample_rate = config->sample_rate;
9768 } else {
9769 ALOGW("%s compress voip not active, use defaults", __func__);
9770 }
Haynes Mathew George4ffef292017-11-21 15:08:02 -08009771 }
Haynes Mathew George4ffef292017-11-21 15:08:02 -08009772 return 0;
9773}
9774
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009775static int adev_open_input_stream(struct audio_hw_device *dev,
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07009776 audio_io_handle_t handle,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009777 audio_devices_t devices,
9778 struct audio_config *config,
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07009779 struct audio_stream_in **stream_in,
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05309780 audio_input_flags_t flags,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08009781 const char *address,
Vidyakumar Athota5c398212015-03-31 21:53:21 -07009782 audio_source_t source)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009783{
9784 struct audio_device *adev = (struct audio_device *)dev;
9785 struct stream_in *in;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08009786 int ret = 0, buffer_size, frame_size;
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07009787 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07009788 bool is_low_latency = false;
Divya Narayanan Poojary45f19192016-09-30 18:52:13 +05309789 bool channel_mask_updated = false;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009790 bool is_usb_dev = audio_is_usb_in_device(devices);
9791 bool may_use_hifi_record = adev_input_allow_hifi_record(adev,
9792 devices,
9793 flags,
9794 source);
Andy Hung94320602018-10-29 18:31:12 -07009795 ALOGV("%s: enter: flags %#x, is_usb_dev %d, may_use_hifi_record %d,"
9796 " sample_rate %u, channel_mask %#x, format %#x",
9797 __func__, flags, is_usb_dev, may_use_hifi_record,
9798 config->sample_rate, config->channel_mask, config->format);
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05309799
kunleizdff872d2018-08-20 14:40:33 +08009800 if (is_usb_dev && (!audio_extn_usb_connected(NULL))) {
kunleizd6a9e0c2018-07-30 15:38:52 +08009801 is_usb_dev = false;
9802 devices = AUDIO_DEVICE_IN_BUILTIN_MIC;
9803 ALOGW("%s: ignore set device to non existing USB card, use input device(%#x)",
9804 __func__, devices);
9805 }
9806
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009807 *stream_in = NULL;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009808
9809 if (!(is_usb_dev && may_use_hifi_record)) {
9810 if (config->sample_rate == 0)
9811 config->sample_rate = 48000;
9812 if (config->channel_mask == AUDIO_CHANNEL_NONE)
9813 config->channel_mask = AUDIO_CHANNEL_IN_MONO;
9814 if (config->format == AUDIO_FORMAT_DEFAULT)
9815 config->format = AUDIO_FORMAT_PCM_16_BIT;
9816
9817 channel_count = audio_channel_count_from_in_mask(config->channel_mask);
9818
Aalique Grahame22e49102018-12-18 14:23:57 -08009819 if (check_input_parameters(config->sample_rate, config->format, channel_count,
9820 false) != 0)
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009821 return -EINVAL;
Chaithanya Krishna Bacharaju9955b162016-05-25 16:25:53 +05309822 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009823
9824 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07009825
9826 if (!in) {
9827 ALOGE("failed to allocate input stream");
9828 return -ENOMEM;
9829 }
9830
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05309831 ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x)\
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05309832 stream_handle(%p) io_handle(%d) source(%d) format %x",__func__, config->sample_rate,
9833 config->channel_mask, devices, &in->stream, handle, source, config->format);
Ravi Kumar Alamanda40703102014-04-24 10:34:41 -07009834 pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07009835 pthread_mutex_init(&in->pre_lock, (const pthread_mutexattr_t *) NULL);
Ravi Kumar Alamanda40703102014-04-24 10:34:41 -07009836
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009837 in->stream.common.get_sample_rate = in_get_sample_rate;
9838 in->stream.common.set_sample_rate = in_set_sample_rate;
9839 in->stream.common.get_buffer_size = in_get_buffer_size;
9840 in->stream.common.get_channels = in_get_channels;
9841 in->stream.common.get_format = in_get_format;
9842 in->stream.common.set_format = in_set_format;
9843 in->stream.common.standby = in_standby;
9844 in->stream.common.dump = in_dump;
9845 in->stream.common.set_parameters = in_set_parameters;
9846 in->stream.common.get_parameters = in_get_parameters;
9847 in->stream.common.add_audio_effect = in_add_audio_effect;
9848 in->stream.common.remove_audio_effect = in_remove_audio_effect;
9849 in->stream.set_gain = in_set_gain;
9850 in->stream.read = in_read;
9851 in->stream.get_input_frames_lost = in_get_input_frames_lost;
Aalique Grahame22e49102018-12-18 14:23:57 -08009852 in->stream.get_capture_position = in_get_capture_position;
Naresh Tannirudcb47c52018-06-25 16:23:32 +05309853 in->stream.get_active_microphones = in_get_active_microphones;
Paul McLeana50b7332018-12-17 08:24:21 -07009854 in->stream.set_microphone_direction = in_set_microphone_direction;
9855 in->stream.set_microphone_field_dimension = in_set_microphone_field_dimension;
juyuchendb308c22019-01-21 11:57:17 -07009856 in->stream.update_sink_metadata = in_update_sink_metadata;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009857
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08009858 list_init(&in->device_list);
9859 update_device_list(&in->device_list, devices, address, true);
Vidyakumar Athota5c398212015-03-31 21:53:21 -07009860 in->source = source;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009861 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009862 in->standby = 1;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07009863 in->capture_handle = handle;
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07009864 in->flags = flags;
Haynes Mathew George46740472017-10-27 18:40:12 -07009865 in->bit_width = 16;
9866 in->af_period_multiplier = 1;
justinweng20fb6d82019-02-21 18:49:00 -07009867 in->direction = MIC_DIRECTION_UNSPECIFIED;
9868 in->zoom = 0;
Carter Hsu2e429db2019-05-14 18:50:52 +08009869 list_init(&in->aec_list);
9870 list_init(&in->ns_list);
Phil Burkd898ba62019-06-20 12:49:01 -07009871 in->mmap_shared_memory_fd = -1; // not open
Haynes Mathew George46740472017-10-27 18:40:12 -07009872
Andy Hung94320602018-10-29 18:31:12 -07009873 ALOGV("%s: source %d, config->channel_mask %#x", __func__, source, config->channel_mask);
Aalique Grahame22e49102018-12-18 14:23:57 -08009874 if (source == AUDIO_SOURCE_VOICE_UPLINK ||
9875 source == AUDIO_SOURCE_VOICE_DOWNLINK) {
9876 /* Force channel config requested to mono if incall
9877 record is being requested for only uplink/downlink */
9878 if (config->channel_mask != AUDIO_CHANNEL_IN_MONO) {
9879 config->channel_mask = AUDIO_CHANNEL_IN_MONO;
9880 ret = -EINVAL;
9881 goto err_open;
9882 }
9883 }
9884
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009885 if (is_usb_dev && may_use_hifi_record) {
9886 /* HiFi record selects an appropriate format, channel, rate combo
9887 depending on sink capabilities*/
9888 ret = read_usb_sup_params_and_compare(false /*is_playback*/,
9889 &config->format,
9890 &in->supported_formats[0],
9891 MAX_SUPPORTED_FORMATS,
9892 &config->channel_mask,
9893 &in->supported_channel_masks[0],
9894 MAX_SUPPORTED_CHANNEL_MASKS,
9895 &config->sample_rate,
9896 &in->supported_sample_rates[0],
9897 MAX_SUPPORTED_SAMPLE_RATES);
9898 if (ret != 0) {
9899 ret = -EINVAL;
9900 goto err_open;
9901 }
9902 channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Haynes Mathew George4ffef292017-11-21 15:08:02 -08009903 } else if (config->format == AUDIO_FORMAT_DEFAULT) {
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05309904 config->format = AUDIO_FORMAT_PCM_16_BIT;
Surendar karkaaca3d082017-11-09 15:18:37 +05309905 } else if (property_get_bool("vendor.audio.capture.pcm.32bit.enable", false)
9906 && config->format == AUDIO_FORMAT_PCM_32_BIT) {
9907 in->config.format = PCM_FORMAT_S32_LE;
9908 in->bit_width = 32;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05309909 } else if ((config->format == AUDIO_FORMAT_PCM_FLOAT) ||
9910 (config->format == AUDIO_FORMAT_PCM_32_BIT) ||
9911 (config->format == AUDIO_FORMAT_PCM_24_BIT_PACKED) ||
9912 (config->format == AUDIO_FORMAT_PCM_8_24_BIT)) {
9913 bool ret_error = false;
9914 in->bit_width = 24;
9915 /* 24 bit is restricted to UNPROCESSED source only,also format supported
9916 from HAL is 24_packed and 8_24
9917 *> In case of UNPROCESSED source, for 24 bit, if format requested is other than
9918 24_packed return error indicating supported format is 24_packed
9919 *> In case of any other source requesting 24 bit or float return error
9920 indicating format supported is 16 bit only.
9921
9922 on error flinger will retry with supported format passed
9923 */
9924 if ((source != AUDIO_SOURCE_UNPROCESSED) &&
9925 (source != AUDIO_SOURCE_CAMCORDER)) {
9926 config->format = AUDIO_FORMAT_PCM_16_BIT;
9927 if (config->sample_rate > 48000)
9928 config->sample_rate = 48000;
9929 ret_error = true;
Haynes Mathew George46740472017-10-27 18:40:12 -07009930 } else if (!(config->format == AUDIO_FORMAT_PCM_24_BIT_PACKED ||
9931 config->format == AUDIO_FORMAT_PCM_8_24_BIT)) {
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05309932 config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
9933 ret_error = true;
9934 }
9935
9936 if (ret_error) {
9937 ret = -EINVAL;
9938 goto err_open;
9939 }
9940 }
9941
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009942 in->channel_mask = config->channel_mask;
9943 in->format = config->format;
9944
9945 in->usecase = USECASE_AUDIO_RECORD;
Samyak Jain0aa07ab2019-04-04 14:36:32 +05309946
Huicheng Liu1404ba12020-09-11 01:03:25 -04009947 /* validate bus device address */
9948 if (compare_device_type(&in->device_list, AUDIO_DEVICE_IN_BUS)) {
9949 /* extract car audio stream index */
9950 in->car_audio_stream =
9951 audio_extn_auto_hal_get_car_audio_stream_from_address(address);
9952 if (in->car_audio_stream < 0) {
9953 ALOGE("%s: invalid car audio stream %x",
9954 __func__, in->car_audio_stream);
9955 ret = -EINVAL;
9956 goto err_open;
9957 }
9958 ALOGV("%s: car_audio_stream 0x%x", __func__, in->car_audio_stream);
Huicheng Liuea4fd422021-01-12 18:34:50 -05009959 ret = audio_extn_auto_hal_open_input_stream(in);
9960 if (ret) {
9961 ALOGE("%s: Failed to open input stream for bus device", __func__);
9962 ret = -EINVAL;
9963 goto err_open;
9964 }
Huicheng Liu1404ba12020-09-11 01:03:25 -04009965 }
9966
Susan Wange3959562021-03-11 11:50:26 -05009967 /* reassign use case for echo reference stream on automotive platforms */
9968 if (in->source == AUDIO_SOURCE_ECHO_REFERENCE) {
9969 ret = audio_extn_auto_hal_open_echo_reference_stream(in);
9970 }
9971
Kogara Naveen Kumar379ff692022-01-31 11:56:51 +05309972 if ((in->source == AUDIO_SOURCE_FM_TUNER) || (devices == AUDIO_DEVICE_IN_FM_TUNER)) {
Samyak Jain0aa07ab2019-04-04 14:36:32 +05309973 if(!get_usecase_from_list(adev, USECASE_AUDIO_RECORD_FM_VIRTUAL))
9974 in->usecase = USECASE_AUDIO_RECORD_FM_VIRTUAL;
9975 else {
9976 ret = -EINVAL;
9977 goto err_open;
9978 }
9979 }
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +05309980#ifdef PLATFORM_AUTO
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +05309981 if ((config->sample_rate == 48000 ||
9982 config->sample_rate == 32000 ||
9983 config->sample_rate == 24000 ||
9984 config->sample_rate == 16000 ||
9985 config->sample_rate == 8000)&&
Deeraj Somanfa377bf2019-02-06 12:57:59 +05309986 (flags & AUDIO_INPUT_FLAG_TIMESTAMP) == 0 &&
9987 (flags & AUDIO_INPUT_FLAG_COMPRESS) == 0 &&
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +05309988 (flags & AUDIO_INPUT_FLAG_FAST) != 0)
9989#else
9990 if (config->sample_rate == LOW_LATENCY_CAPTURE_SAMPLE_RATE &&
9991 (flags & AUDIO_INPUT_FLAG_TIMESTAMP) == 0 &&
9992 (flags & AUDIO_INPUT_FLAG_COMPRESS) == 0 &&
9993 (flags & AUDIO_INPUT_FLAG_FAST) != 0)
9994#endif
9995{
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009996 is_low_latency = true;
9997#if LOW_LATENCY_CAPTURE_USE_CASE
Revathi Uddarajud9f23d92020-07-27 10:55:06 +05309998 if ((flags & AUDIO_INPUT_FLAG_VOIP_TX) != 0)
9999 in->usecase = USECASE_AUDIO_RECORD_VOIP_LOW_LATENCY;
10000 else
10001 in->usecase = USECASE_AUDIO_RECORD_LOW_LATENCY;
Haynes Mathew George484e8d22017-07-31 18:55:17 -070010002#endif
10003 in->realtime = may_use_noirq_mode(adev, in->usecase, in->flags);
Aalique Grahame22e49102018-12-18 14:23:57 -080010004 if (!in->realtime) {
10005 in->config = pcm_config_audio_capture;
10006 frame_size = audio_stream_in_frame_size(&in->stream);
10007 buffer_size = get_input_buffer_size(config->sample_rate,
10008 config->format,
10009 channel_count,
10010 is_low_latency);
10011 in->config.period_size = buffer_size / frame_size;
10012 in->config.rate = config->sample_rate;
10013 in->af_period_multiplier = 1;
10014 } else {
10015 // period size is left untouched for rt mode playback
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +053010016#ifdef PLATFORM_AUTO
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +053010017 switch(config->sample_rate)
10018 {
10019 case 48000:
10020 in->config = pcm_config_audio_capture_rt_48KHz;
10021 break;
10022 case 32000:
10023 in->config = pcm_config_audio_capture_rt_32KHz;
10024 break;
10025 case 24000:
10026 in->config = pcm_config_audio_capture_rt_24KHz;
10027 break;
10028 case 16000:
10029 in->config = pcm_config_audio_capture_rt_16KHz;
10030 break;
10031 case 8000:
10032 in->config = pcm_config_audio_capture_rt_8KHz;
10033 break;
10034 default:
10035 in->config = pcm_config_audio_capture_rt_48KHz;
10036 }
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +053010037#else
10038 in->config = pcm_config_audio_capture_rt_48KHz;
10039#endif
Aalique Grahame22e49102018-12-18 14:23:57 -080010040 in->af_period_multiplier = af_period_multiplier;
10041 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -070010042 }
10043
Susan Wangb803cb52021-10-14 12:03:36 -040010044 /* Additional sample rates added below must also be present
10045 in audio_policy_configuration.xml for mmap_no_irq_in */
10046 bool valid_mmap_record_rate = (config->sample_rate == 8000 ||
10047 config->sample_rate == 16000 ||
Susan Wang89da9042021-10-14 12:03:36 -040010048 config->sample_rate == 24000 ||
Susan Wangb803cb52021-10-14 12:03:36 -040010049 config->sample_rate == 32000 ||
10050 config->sample_rate == 48000);
10051 if (valid_mmap_record_rate &&
10052 ((in->flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0)) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -070010053 in->realtime = 0;
10054 in->usecase = USECASE_AUDIO_RECORD_MMAP;
10055 in->config = pcm_config_mmap_capture;
Haynes Mathew George46740472017-10-27 18:40:12 -070010056 in->config.format = pcm_format_from_audio_format(config->format);
Haynes Mathew George484e8d22017-07-31 18:55:17 -070010057 in->stream.start = in_start;
10058 in->stream.stop = in_stop;
10059 in->stream.create_mmap_buffer = in_create_mmap_buffer;
10060 in->stream.get_mmap_position = in_get_mmap_position;
Ramjee Singhbcb3b6c2021-11-17 13:19:16 +053010061 in->config.rate = config->sample_rate;
Haynes Mathew George484e8d22017-07-31 18:55:17 -070010062 ALOGV("%s: USECASE_AUDIO_RECORD_MMAP", __func__);
Haynes Mathew George46740472017-10-27 18:40:12 -070010063 } else if (is_usb_dev && may_use_hifi_record) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -070010064 in->usecase = USECASE_AUDIO_RECORD_HIFI;
10065 in->config = pcm_config_audio_capture;
10066 frame_size = audio_stream_in_frame_size(&in->stream);
10067 buffer_size = get_input_buffer_size(config->sample_rate,
10068 config->format,
10069 channel_count,
10070 false /*is_low_latency*/);
10071 in->config.period_size = buffer_size / frame_size;
10072 in->config.rate = config->sample_rate;
Haynes Mathew George484e8d22017-07-31 18:55:17 -070010073 in->config.format = pcm_format_from_audio_format(config->format);
Karthikeyan Mani07faa602018-08-20 11:01:32 -070010074 switch (config->format) {
10075 case AUDIO_FORMAT_PCM_32_BIT:
10076 in->bit_width = 32;
10077 break;
10078 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
10079 case AUDIO_FORMAT_PCM_8_24_BIT:
10080 in->bit_width = 24;
10081 break;
10082 default:
10083 in->bit_width = 16;
10084 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010085 } else if (is_single_device_type_equal(&in->device_list,
10086 AUDIO_DEVICE_IN_TELEPHONY_RX) ||
10087 is_single_device_type_equal(&in->device_list,
10088 AUDIO_DEVICE_IN_PROXY)) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -070010089 if (config->sample_rate == 0)
10090 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
10091 if (config->sample_rate != 48000 && config->sample_rate != 16000 &&
10092 config->sample_rate != 8000) {
10093 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
10094 ret = -EINVAL;
10095 goto err_open;
10096 }
10097 if (config->format == AUDIO_FORMAT_DEFAULT)
10098 config->format = AUDIO_FORMAT_PCM_16_BIT;
10099 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
10100 config->format = AUDIO_FORMAT_PCM_16_BIT;
10101 ret = -EINVAL;
10102 goto err_open;
10103 }
10104
10105 in->usecase = USECASE_AUDIO_RECORD_AFE_PROXY;
Zhou Song62ea0282020-03-22 19:53:01 +080010106 if (adev->ha_proxy_enable &&
10107 is_single_device_type_equal(&in->device_list,
10108 AUDIO_DEVICE_IN_TELEPHONY_RX))
10109 in->usecase = USECASE_AUDIO_RECORD_AFE_PROXY2;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -070010110 in->config = pcm_config_afe_proxy_record;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -070010111 in->config.rate = config->sample_rate;
Aalique Grahame22e49102018-12-18 14:23:57 -080010112 in->af_period_multiplier = 1;
10113 } else if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
Shalini Manjunathaac412b32020-01-03 15:00:14 +053010114 (!voice_extn_is_compress_voip_supported()) &&
Aalique Grahame22e49102018-12-18 14:23:57 -080010115 in->flags & AUDIO_INPUT_FLAG_VOIP_TX &&
10116 (config->sample_rate == 8000 ||
10117 config->sample_rate == 16000 ||
10118 config->sample_rate == 32000 ||
10119 config->sample_rate == 48000) &&
10120 channel_count == 1) {
10121 in->usecase = USECASE_AUDIO_RECORD_VOIP;
10122 in->config = pcm_config_audio_capture;
10123 frame_size = audio_stream_in_frame_size(&in->stream);
10124 buffer_size = get_stream_buffer_size(VOIP_CAPTURE_PERIOD_DURATION_MSEC,
10125 config->sample_rate,
10126 config->format,
10127 channel_count, false /*is_low_latency*/);
10128 in->config.period_size = buffer_size / frame_size;
10129 in->config.period_count = VOIP_CAPTURE_PERIOD_COUNT;
10130 in->config.rate = config->sample_rate;
10131 in->af_period_multiplier = 1;
Mingshu Pangc2d65042021-01-14 16:19:10 +080010132 } else if (in->realtime) {
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +053010133#ifdef PLATFORM_AUTO
Kogara Naveen Kumara688a812022-04-27 16:45:59 +053010134 switch(config->sample_rate)
10135 {
10136 case 48000:
10137 in->config = pcm_config_audio_capture_rt_48KHz;
10138 break;
10139 case 32000:
10140 in->config = pcm_config_audio_capture_rt_32KHz;
10141 break;
10142 case 24000:
10143 in->config = pcm_config_audio_capture_rt_24KHz;
10144 break;
10145 case 16000:
10146 in->config = pcm_config_audio_capture_rt_16KHz;
10147 break;
10148 case 8000:
10149 in->config = pcm_config_audio_capture_rt_8KHz;
10150 break;
10151 default:
10152 in->config = pcm_config_audio_capture_rt_48KHz;
10153 }
Mingshu Pangc2d65042021-01-14 16:19:10 +080010154 in->config.format = pcm_format_from_audio_format(config->format);
10155 in->af_period_multiplier = af_period_multiplier;
Krishna Kishor Jhac1542b22022-08-22 13:12:09 +053010156#else
10157 in->config = pcm_config_audio_capture_rt_48KHz;
10158#endif
10159} else {
Revathi Uddarajud2634032017-12-07 14:42:34 +053010160 int ret_val;
10161 pthread_mutex_lock(&adev->lock);
10162 ret_val = audio_extn_check_and_set_multichannel_usecase(adev,
10163 in, config, &channel_mask_updated);
10164 pthread_mutex_unlock(&adev->lock);
10165
10166 if (!ret_val) {
10167 if (channel_mask_updated == true) {
10168 ALOGD("%s: return error to retry with updated channel mask (%#x)",
10169 __func__, config->channel_mask);
10170 ret = -EINVAL;
10171 goto err_open;
10172 }
10173 ALOGD("%s: created multi-channel session succesfully",__func__);
10174 } else if (audio_extn_compr_cap_enabled() &&
10175 audio_extn_compr_cap_format_supported(config->format) &&
10176 (in->dev->mode != AUDIO_MODE_IN_COMMUNICATION)) {
10177 audio_extn_compr_cap_init(in);
10178 } else if (audio_extn_cin_applicable_stream(in)) {
Deeraj Soman14230922019-01-30 16:39:30 +053010179 ret = audio_extn_cin_configure_input_stream(in, config);
Revathi Uddarajud2634032017-12-07 14:42:34 +053010180 if (ret)
10181 goto err_open;
10182 } else {
10183 in->config = pcm_config_audio_capture;
10184 in->config.rate = config->sample_rate;
10185 in->config.format = pcm_format_from_audio_format(config->format);
Revathi Uddarajud2634032017-12-07 14:42:34 +053010186 in->format = config->format;
10187 frame_size = audio_stream_in_frame_size(&in->stream);
10188 buffer_size = get_input_buffer_size(config->sample_rate,
Haynes Mathew George46740472017-10-27 18:40:12 -070010189 config->format,
10190 channel_count,
10191 is_low_latency);
Dieter Luecking5d57def2018-09-07 14:23:37 +020010192 /* prevent division-by-zero */
10193 if (frame_size == 0) {
10194 ALOGE("%s: Error frame_size==0", __func__);
10195 ret = -EINVAL;
10196 goto err_open;
10197 }
10198
Revathi Uddarajud2634032017-12-07 14:42:34 +053010199 in->config.period_size = buffer_size / frame_size;
Aalique Grahame22e49102018-12-18 14:23:57 -080010200 in->af_period_multiplier = 1;
Haynes Mathew George4ffef292017-11-21 15:08:02 -080010201
Revathi Uddarajud2634032017-12-07 14:42:34 +053010202 if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
10203 /* optionally use VOIP usecase depending on config(s) */
10204 ret = adev_update_voice_comm_input_stream(in, config);
10205 }
Haynes Mathew George4ffef292017-11-21 15:08:02 -080010206
Revathi Uddarajud2634032017-12-07 14:42:34 +053010207 if (ret) {
10208 ALOGE("%s AUDIO_SOURCE_VOICE_COMMUNICATION invalid args", __func__);
10209 goto err_open;
10210 }
Haynes Mathew George4ffef292017-11-21 15:08:02 -080010211 }
Jaideep Sharmad305a4a2020-02-27 14:29:04 +053010212
10213 /* assign concurrent capture usecase if record has to caried out from
10214 * actual hardware input source */
10215 if (audio_extn_is_concurrent_capture_enabled() &&
10216 !audio_is_virtual_input_source(in->source)) {
Samyak Jainc37062f2019-04-25 18:41:06 +053010217 /* Acquire lock to avoid two concurrent use cases initialized to
10218 same pcm record use case */
kunleiz28c73e72019-03-27 17:24:04 +080010219
Samyak Jainc37062f2019-04-25 18:41:06 +053010220 if (in->usecase == USECASE_AUDIO_RECORD) {
10221 pthread_mutex_lock(&adev->lock);
10222 if (!(adev->pcm_record_uc_state)) {
10223 ALOGV("%s: using USECASE_AUDIO_RECORD",__func__);
10224 adev->pcm_record_uc_state = 1;
10225 pthread_mutex_unlock(&adev->lock);
Krishna Kishor Jha0f4d8482022-02-18 10:56:05 +053010226 } else if (audio_extn_is_concurrent_pcm_record_enabled()) {
10227 in->usecase = get_record_usecase(adev);
10228 pthread_mutex_unlock(&adev->lock);
Samyak Jainc37062f2019-04-25 18:41:06 +053010229 } else {
10230 pthread_mutex_unlock(&adev->lock);
10231 /* Assign compress record use case for second record */
10232 in->usecase = USECASE_AUDIO_RECORD_COMPRESS2;
10233 in->flags |= AUDIO_INPUT_FLAG_COMPRESS;
10234 ALOGV("%s: overriding usecase with USECASE_AUDIO_RECORD_COMPRESS2 and appending compress flag", __func__);
10235 if (audio_extn_cin_applicable_stream(in)) {
10236 in->sample_rate = config->sample_rate;
Deeraj Soman14230922019-01-30 16:39:30 +053010237 ret = audio_extn_cin_configure_input_stream(in, config);
Samyak Jainc37062f2019-04-25 18:41:06 +053010238 if (ret)
10239 goto err_open;
10240 }
10241 }
10242 }
kunleiz28c73e72019-03-27 17:24:04 +080010243 }
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -070010244 }
Kogara Naveen Kumar51bb57d2022-02-17 16:40:06 +053010245
Ramjee Singh82fd0c12019-08-21 16:31:33 +053010246 if (audio_extn_ssr_get_stream() != in)
10247 in->config.channels = channel_count;
Aniket Kumar Lata23d8cbf2019-04-10 19:54:46 -070010248
Aniket Kumar Lata23d8cbf2019-04-10 19:54:46 -070010249 in->sample_rate = in->config.rate;
10250
Dhananjay Kumard6d32152016-10-13 16:11:03 +053010251 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
10252 &adev->streams_input_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010253 &in->device_list, flags, in->format,
Haynes Mathew George4ffef292017-11-21 15:08:02 -080010254 in->sample_rate, in->bit_width,
10255 in->profile, &in->app_type_cfg);
Naresh Tanniru04f71882018-06-26 17:46:22 +053010256 register_format(in->format, in->supported_formats);
10257 register_channel_mask(in->channel_mask, in->supported_channel_masks);
10258 register_sample_rate(in->sample_rate, in->supported_sample_rates);
Dhananjay Kumard6d32152016-10-13 16:11:03 +053010259
Dechen Chai22768452021-07-30 09:29:16 +053010260#ifndef LINUX_ENABLED
Aalique Grahame22e49102018-12-18 14:23:57 -080010261 in->error_log = error_log_create(
10262 ERROR_LOG_ENTRIES,
10263 1000000000 /* aggregate consecutive identical errors within one second */);
Dechen Chai22768452021-07-30 09:29:16 +053010264#endif
Aalique Grahame22e49102018-12-18 14:23:57 -080010265
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -070010266 /* This stream could be for sound trigger lab,
10267 get sound trigger pcm if present */
10268 audio_extn_sound_trigger_check_and_get_session(in);
10269
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053010270 lock_input_stream(in);
10271 audio_extn_snd_mon_register_listener(in, in_snd_mon_cb);
10272 pthread_mutex_lock(&adev->lock);
10273 in->card_status = adev->card_status;
10274 pthread_mutex_unlock(&adev->lock);
10275 pthread_mutex_unlock(&in->lock);
10276
Aalique Grahame22e49102018-12-18 14:23:57 -080010277 stream_app_type_cfg_init(&in->app_type_cfg);
10278
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010279 *stream_in = &in->stream;
Derek Chenf939fb72018-11-13 13:34:41 -080010280
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010281 ret = io_streams_map_insert(adev, &in->stream.common,
10282 handle, AUDIO_PATCH_HANDLE_NONE);
10283 if (ret != 0)
10284 goto err_open;
10285
Susan Wang6dd13092021-01-25 10:27:11 -050010286 in->in_ctxt.input = in;
Derek Chenf939fb72018-11-13 13:34:41 -080010287
10288 pthread_mutex_lock(&adev->lock);
Susan Wang6dd13092021-01-25 10:27:11 -050010289 list_add_tail(&adev->active_inputs_list, &in->in_ctxt.list);
Derek Chenf939fb72018-11-13 13:34:41 -080010290 pthread_mutex_unlock(&adev->lock);
10291
Eric Laurent994a6932013-07-17 11:51:42 -070010292 ALOGV("%s: exit", __func__);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -080010293 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010294
10295err_open:
Krishna Kishor Jha0f4d8482022-02-18 10:56:05 +053010296 if (audio_extn_is_concurrent_pcm_record_enabled() && is_pcm_record_usecase(in->usecase)) {
10297 free_record_usecase(adev, in->usecase);
10298 } else if (in->usecase == USECASE_AUDIO_RECORD) {
Samyak Jainc37062f2019-04-25 18:41:06 +053010299 pthread_mutex_lock(&adev->lock);
10300 adev->pcm_record_uc_state = 0;
10301 pthread_mutex_unlock(&adev->lock);
10302 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010303 free(in);
10304 *stream_in = NULL;
10305 return ret;
10306}
10307
10308static void adev_close_input_stream(struct audio_hw_device *dev,
10309 struct audio_stream_in *stream)
10310{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -080010311 int ret;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -070010312 struct stream_in *in = (struct stream_in *)stream;
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -070010313 struct audio_device *adev = (struct audio_device *)dev;
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +053010314
Sidipotu Ashokf43018c2014-05-02 16:21:50 +053010315 ALOGD("%s: enter:stream_handle(%p)",__func__, in);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -080010316
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010317 if (in == NULL) {
10318 ALOGE("%s: audio_stream_in ptr is NULL", __func__);
10319 return;
10320 }
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010321 io_streams_map_remove(adev, in->capture_handle);
10322
Susan Wang6dd13092021-01-25 10:27:11 -050010323 // remove out_ctxt early to prevent the stream
10324 // being opened in a race condition
10325 pthread_mutex_lock(&adev->lock);
10326 list_remove(&in->in_ctxt.list);
10327 pthread_mutex_unlock(&adev->lock);
10328
kunleiz70e57612018-12-28 17:50:23 +080010329 /* must deregister from sndmonitor first to prevent races
10330 * between the callback and close_stream
10331 */
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053010332 audio_extn_snd_mon_unregister_listener(stream);
10333
kunleiz70e57612018-12-28 17:50:23 +080010334 /* Disable echo reference if there are no active input, hfp call
10335 * and sound trigger while closing input stream
10336 */
Eric Laurent637e2d42018-11-15 12:24:31 -080010337 if (adev_get_active_input(adev) == NULL &&
kunleiz70e57612018-12-28 17:50:23 +080010338 !audio_extn_hfp_is_active(adev) &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010339 !audio_extn_sound_trigger_check_ec_ref_enable()) {
10340 struct listnode out_devices;
10341 list_init(&out_devices);
10342 platform_set_echo_reference(adev, false, &out_devices);
Zhenlin Lian4f947842022-05-14 15:50:52 +053010343 clear_devices(&out_devices);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010344 } else
kunleiz70e57612018-12-28 17:50:23 +080010345 audio_extn_sound_trigger_update_ec_ref_status(false);
Venkata Narendra Kumar Gutta1bbbf542014-09-04 19:11:25 +053010346
Dechen Chai22768452021-07-30 09:29:16 +053010347#ifndef LINUX_ENABLED
Weiyin Jiang2995f662019-04-17 14:25:12 +080010348 error_log_destroy(in->error_log);
10349 in->error_log = NULL;
Dechen Chai22768452021-07-30 09:29:16 +053010350#endif
Pallavid7c7a272018-01-16 11:22:55 +053010351
Narsinga Rao Chella05573b72013-11-15 15:21:40 -080010352 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +053010353 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -080010354 ret = voice_extn_compress_voip_close_input_stream(&stream->common);
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +053010355 pthread_mutex_unlock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -080010356 if (ret != 0)
10357 ALOGE("%s: Compress voip input cannot be closed, error:%d",
10358 __func__, ret);
10359 } else
10360 in_standby(&stream->common);
10361
Weiyin Jiang280ea742020-09-08 20:28:22 +080010362 pthread_mutex_destroy(&in->lock);
10363 pthread_mutex_destroy(&in->pre_lock);
10364
Revathi Uddarajud2634032017-12-07 14:42:34 +053010365 pthread_mutex_lock(&adev->lock);
Krishna Kishor Jha0f4d8482022-02-18 10:56:05 +053010366 if (audio_extn_is_concurrent_pcm_record_enabled() && is_pcm_record_usecase(in->usecase)) {
10367 free_record_usecase(adev, in->usecase);
10368 } else if (in->usecase == USECASE_AUDIO_RECORD) {
Samyak Jain15fda662018-12-18 16:40:52 +053010369 adev->pcm_record_uc_state = 0;
10370 }
10371
Kunlei Zhang5d5d8d92020-02-26 15:00:59 +080010372 if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
10373 adev->enable_voicerx = false;
10374 }
10375
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -070010376 if (audio_extn_ssr_get_stream() == in) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -070010377 audio_extn_ssr_deinit();
10378 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010379
Garmond Leunge2433c32017-09-28 21:51:22 -070010380 if (audio_extn_ffv_get_stream() == in) {
10381 audio_extn_ffv_stream_deinit();
10382 }
10383
Dhananjay Kumaree4d2002016-10-25 18:02:58 +053010384 if (audio_extn_compr_cap_enabled() &&
Subhadra Jagadeesan63a1e832023-01-13 11:26:38 +053010385 audio_extn_compr_cap_format_supported(pcm_format_to_audio_format((in->config).format)))
Mingming Yine62d7842013-10-25 16:26:03 -070010386 audio_extn_compr_cap_deinit();
Dhanalakshmi Siddani74cf00b2016-12-02 13:55:57 +053010387
Dhananjay Kumar7dbe3562019-08-30 05:49:33 +053010388 if (audio_extn_cin_attached_usecase(in))
Manish Dewangan46e07982018-12-13 18:18:59 +053010389 audio_extn_cin_free_input_stream_resources(in);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -070010390
Mingming Yinfd7607b2016-01-22 12:48:44 -080010391 if (in->is_st_session) {
10392 ALOGV("%s: sound trigger pcm stop lab", __func__);
10393 audio_extn_sound_trigger_stop_lab(in);
10394 }
Zhenlin Lian4f947842022-05-14 15:50:52 +053010395 clear_devices(&in->device_list);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -070010396 free(stream);
Revathi Uddarajud2634032017-12-07 14:42:34 +053010397 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010398 return;
10399}
10400
Aalique Grahame22e49102018-12-18 14:23:57 -080010401/* verifies input and output devices and their capabilities.
10402 *
10403 * This verification is required when enabling extended bit-depth or
10404 * sampling rates, as not all qcom products support it.
10405 *
10406 * Suitable for calling only on initialization such as adev_open().
10407 * It fills the audio_device use_case_table[] array.
10408 *
10409 * Has a side-effect that it needs to configure audio routing / devices
10410 * in order to power up the devices and read the device parameters.
10411 * It does not acquire any hw device lock. Should restore the devices
10412 * back to "normal state" upon completion.
10413 */
10414static int adev_verify_devices(struct audio_device *adev)
10415{
10416 /* enumeration is a bit difficult because one really wants to pull
10417 * the use_case, device id, etc from the hidden pcm_device_table[].
10418 * In this case there are the following use cases and device ids.
10419 *
10420 * [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = {0, 0},
10421 * [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {15, 15},
10422 * [USECASE_AUDIO_PLAYBACK_HIFI] = {1, 1},
10423 * [USECASE_AUDIO_PLAYBACK_OFFLOAD] = {9, 9},
10424 * [USECASE_AUDIO_RECORD] = {0, 0},
10425 * [USECASE_AUDIO_RECORD_LOW_LATENCY] = {15, 15},
10426 * [USECASE_VOICE_CALL] = {2, 2},
10427 *
10428 * USECASE_AUDIO_PLAYBACK_OFFLOAD, USECASE_AUDIO_PLAYBACK_HIFI omitted.
10429 * USECASE_VOICE_CALL omitted, but possible for either input or output.
10430 */
10431
10432 /* should be the usecases enabled in adev_open_input_stream() */
10433 static const int test_in_usecases[] = {
10434 USECASE_AUDIO_RECORD,
10435 USECASE_AUDIO_RECORD_LOW_LATENCY, /* does not appear to be used */
10436 };
10437 /* should be the usecases enabled in adev_open_output_stream()*/
10438 static const int test_out_usecases[] = {
10439 USECASE_AUDIO_PLAYBACK_DEEP_BUFFER,
10440 USECASE_AUDIO_PLAYBACK_LOW_LATENCY,
10441 };
10442 static const usecase_type_t usecase_type_by_dir[] = {
10443 PCM_PLAYBACK,
10444 PCM_CAPTURE,
10445 };
10446 static const unsigned flags_by_dir[] = {
10447 PCM_OUT,
10448 PCM_IN,
10449 };
10450
10451 size_t i;
10452 unsigned dir;
10453 const unsigned card_id = adev->snd_card;
10454
10455 for (dir = 0; dir < 2; ++dir) {
10456 const usecase_type_t usecase_type = usecase_type_by_dir[dir];
10457 const unsigned flags_dir = flags_by_dir[dir];
10458 const size_t testsize =
10459 dir ? ARRAY_SIZE(test_in_usecases) : ARRAY_SIZE(test_out_usecases);
10460 const int *testcases =
10461 dir ? test_in_usecases : test_out_usecases;
10462 const audio_devices_t audio_device =
10463 dir ? AUDIO_DEVICE_IN_BUILTIN_MIC : AUDIO_DEVICE_OUT_SPEAKER;
10464
10465 for (i = 0; i < testsize; ++i) {
10466 const audio_usecase_t audio_usecase = testcases[i];
10467 int device_id;
10468 struct pcm_params **pparams;
10469 struct stream_out out;
10470 struct stream_in in;
10471 struct audio_usecase uc_info;
10472 int retval;
10473
10474 pparams = &adev->use_case_table[audio_usecase];
10475 pcm_params_free(*pparams); /* can accept null input */
10476 *pparams = NULL;
10477
10478 /* find the device ID for the use case (signed, for error) */
10479 device_id = platform_get_pcm_device_id(audio_usecase, usecase_type);
10480 if (device_id < 0)
10481 continue;
10482
10483 /* prepare structures for device probing */
10484 memset(&uc_info, 0, sizeof(uc_info));
10485 uc_info.id = audio_usecase;
10486 uc_info.type = usecase_type;
Aniket Kumar Lata9c2fd892020-01-22 22:20:00 -080010487 list_init(&uc_info.device_list);
Aalique Grahame22e49102018-12-18 14:23:57 -080010488 if (dir) {
Aalique Grahame22e49102018-12-18 14:23:57 -080010489 memset(&in, 0, sizeof(in));
Aniket Kumar Lata9c2fd892020-01-22 22:20:00 -080010490 list_init(&in.device_list);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010491 update_device_list(&in.device_list, audio_device, "", true);
Aalique Grahame22e49102018-12-18 14:23:57 -080010492 in.source = AUDIO_SOURCE_VOICE_COMMUNICATION;
10493 uc_info.stream.in = &in;
Aalique Grahame22e49102018-12-18 14:23:57 -080010494 }
10495 memset(&out, 0, sizeof(out));
Aniket Kumar Lata9c2fd892020-01-22 22:20:00 -080010496 list_init(&out.device_list);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010497 update_device_list(&out.device_list, audio_device, "", true);
Aalique Grahame22e49102018-12-18 14:23:57 -080010498 uc_info.stream.out = &out;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010499 update_device_list(&uc_info.device_list, audio_device, "", true);
Aalique Grahame22e49102018-12-18 14:23:57 -080010500 uc_info.in_snd_device = SND_DEVICE_NONE;
10501 uc_info.out_snd_device = SND_DEVICE_NONE;
10502 list_add_tail(&adev->usecase_list, &uc_info.list);
10503
10504 /* select device - similar to start_(in/out)put_stream() */
10505 retval = select_devices(adev, audio_usecase);
10506 if (retval >= 0) {
10507 *pparams = pcm_params_get(card_id, device_id, flags_dir);
10508#if LOG_NDEBUG == 0
Aalique Grahame203bee02019-03-13 17:49:36 -070010509 char info[512]; /* for possible debug info */
Aalique Grahame22e49102018-12-18 14:23:57 -080010510 if (*pparams) {
10511 ALOGV("%s: (%s) card %d device %d", __func__,
10512 dir ? "input" : "output", card_id, device_id);
10513 pcm_params_to_string(*pparams, info, ARRAY_SIZE(info));
10514 } else {
10515 ALOGV("%s: cannot locate card %d device %d", __func__, card_id, device_id);
10516 }
10517#endif
10518 }
10519
10520 /* deselect device - similar to stop_(in/out)put_stream() */
10521 /* 1. Get and set stream specific mixer controls */
10522 retval = disable_audio_route(adev, &uc_info);
10523 /* 2. Disable the rx device */
10524 retval = disable_snd_device(adev,
10525 dir ? uc_info.in_snd_device : uc_info.out_snd_device);
10526 list_remove(&uc_info.list);
10527 }
10528 }
Aalique Grahame22e49102018-12-18 14:23:57 -080010529 return 0;
10530}
10531
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010532int update_patch(unsigned int num_sources,
10533 const struct audio_port_config *sources,
10534 unsigned int num_sinks,
10535 const struct audio_port_config *sinks,
10536 audio_patch_handle_t handle,
10537 struct audio_patch_info *p_info,
10538 patch_type_t patch_type, bool new_patch)
10539{
Aniket Kumar Latad13758f2020-08-06 15:11:36 -070010540 ALOGV("%s: enter", __func__);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010541
10542 if (p_info == NULL) {
10543 ALOGE("%s: Invalid patch pointer", __func__);
10544 return -EINVAL;
10545 }
10546
10547 if (new_patch) {
10548 p_info->patch = (struct audio_patch *) calloc(1, sizeof(struct audio_patch));
10549 if (p_info->patch == NULL) {
10550 ALOGE("%s: Could not allocate patch", __func__);
10551 return -ENOMEM;
10552 }
10553 }
10554
10555 p_info->patch->id = handle;
10556 p_info->patch->num_sources = num_sources;
10557 p_info->patch->num_sinks = num_sinks;
10558
10559 for (int i = 0; i < num_sources; i++)
10560 p_info->patch->sources[i] = sources[i];
10561 for (int i = 0; i < num_sinks; i++)
10562 p_info->patch->sinks[i] = sinks[i];
10563
10564 p_info->patch_type = patch_type;
10565 return 0;
10566}
10567
10568audio_patch_handle_t generate_patch_handle()
10569{
10570 static audio_patch_handle_t patch_handle = AUDIO_PATCH_HANDLE_NONE;
10571 if (++patch_handle < 0)
10572 patch_handle = AUDIO_PATCH_HANDLE_NONE + 1;
10573 return patch_handle;
10574}
10575
Siddartha Shaik31b530e2017-05-19 15:26:33 +053010576int adev_create_audio_patch(struct audio_hw_device *dev,
10577 unsigned int num_sources,
10578 const struct audio_port_config *sources,
10579 unsigned int num_sinks,
10580 const struct audio_port_config *sinks,
10581 audio_patch_handle_t *handle)
10582{
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010583 int ret = 0;
10584 struct audio_device *adev = (struct audio_device *)dev;
10585 struct audio_patch_info *p_info = NULL;
10586 patch_type_t patch_type = PATCH_NONE;
10587 audio_io_handle_t io_handle = AUDIO_IO_HANDLE_NONE;
10588 audio_source_t input_source = AUDIO_SOURCE_DEFAULT;
10589 struct audio_stream_info *s_info = NULL;
10590 struct audio_stream *stream = NULL;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010591 struct listnode devices;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010592 audio_devices_t device_type = AUDIO_DEVICE_NONE;
10593 bool new_patch = false;
10594 char addr[AUDIO_DEVICE_MAX_ADDRESS_LEN];
Siddartha Shaik31b530e2017-05-19 15:26:33 +053010595
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010596 ALOGD("%s: enter: num sources %d, num_sinks %d, handle %d", __func__,
10597 num_sources, num_sinks, *handle);
10598
10599 if (num_sources == 0 || num_sources > AUDIO_PATCH_PORTS_MAX ||
10600 num_sinks == 0 || num_sinks > AUDIO_PATCH_PORTS_MAX) {
10601 ALOGE("%s: Invalid patch arguments", __func__);
10602 ret = -EINVAL;
10603 goto done;
10604 }
10605
10606 if (num_sources > 1) {
10607 ALOGE("%s: Multiple sources are not supported", __func__);
10608 ret = -EINVAL;
10609 goto done;
10610 }
10611
10612 if (sources == NULL || sinks == NULL) {
10613 ALOGE("%s: Invalid sources or sinks port config", __func__);
10614 ret = -EINVAL;
10615 goto done;
10616 }
10617
10618 ALOGV("%s: source role %d, source type %d", __func__,
10619 sources[0].type, sources[0].role);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010620 list_init(&devices);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010621
10622 // Populate source/sink information and fetch stream info
10623 switch (sources[0].type) {
10624 case AUDIO_PORT_TYPE_DEVICE: // Patch for audio capture or loopback
10625 device_type = sources[0].ext.device.type;
10626 strlcpy(&addr[0], &sources[0].ext.device.address[0], AUDIO_DEVICE_MAX_ADDRESS_LEN);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010627 update_device_list(&devices, device_type, &addr[0], true);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010628 if (sinks[0].type == AUDIO_PORT_TYPE_MIX) {
10629 patch_type = PATCH_CAPTURE;
10630 io_handle = sinks[0].ext.mix.handle;
10631 input_source = sinks[0].ext.mix.usecase.source;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010632 ALOGD("%s: Capture patch from device %x to mix %d",
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010633 __func__, device_type, io_handle);
10634 } else {
10635 // Device to device patch is not implemented.
10636 // This space will need changes if audio HAL
10637 // handles device to device patches in the future.
10638 patch_type = PATCH_DEVICE_LOOPBACK;
10639 }
10640 break;
10641 case AUDIO_PORT_TYPE_MIX: // Patch for audio playback
10642 io_handle = sources[0].ext.mix.handle;
10643 for (int i = 0; i < num_sinks; i++) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010644 device_type = sinks[i].ext.device.type;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010645 strlcpy(&addr[0], &sinks[i].ext.device.address[0], AUDIO_DEVICE_MAX_ADDRESS_LEN);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010646 update_device_list(&devices, device_type, &addr[0], true);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010647 }
10648 patch_type = PATCH_PLAYBACK;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010649 ALOGD("%s: Playback patch from mix handle %d to device %x",
10650 __func__, io_handle, get_device_types(&devices));
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010651 break;
10652 case AUDIO_PORT_TYPE_SESSION:
10653 case AUDIO_PORT_TYPE_NONE:
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010654 ALOGE("%s: Unsupported source type %d", __func__, sources[0].type);
10655 ret = -EINVAL;
10656 goto done;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010657 }
10658
10659 pthread_mutex_lock(&adev->lock);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010660
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010661 // Generate patch info and update patch
10662 if (*handle == AUDIO_PATCH_HANDLE_NONE) {
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010663 *handle = generate_patch_handle();
10664 p_info = (struct audio_patch_info *)
10665 calloc(1, sizeof(struct audio_patch_info));
10666 if (p_info == NULL) {
10667 ALOGE("%s: Failed to allocate memory", __func__);
10668 pthread_mutex_unlock(&adev->lock);
10669 ret = -ENOMEM;
10670 goto done;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010671 }
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010672 new_patch = true;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010673 } else {
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010674 p_info = fetch_patch_info_l(adev, *handle);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010675 if (p_info == NULL) {
10676 ALOGE("%s: Unable to fetch patch for received patch handle %d",
10677 __func__, *handle);
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010678 pthread_mutex_unlock(&adev->lock);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010679 ret = -EINVAL;
10680 goto done;
10681 }
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010682 }
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010683 update_patch(num_sources, sources, num_sinks, sinks,
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010684 *handle, p_info, patch_type, new_patch);
10685
10686 // Fetch stream info of associated mix for playback or capture patches
10687 if (p_info->patch_type == PATCH_PLAYBACK ||
10688 p_info->patch_type == PATCH_CAPTURE) {
10689 s_info = hashmapGet(adev->io_streams_map, (void *) (intptr_t) io_handle);
10690 if (s_info == NULL) {
10691 ALOGE("%s: Failed to obtain stream info", __func__);
10692 if (new_patch)
10693 free(p_info);
10694 pthread_mutex_unlock(&adev->lock);
10695 ret = -EINVAL;
10696 goto done;
10697 }
10698 ALOGV("%s: Fetched stream info with io_handle %d", __func__, io_handle);
10699 s_info->patch_handle = *handle;
10700 stream = s_info->stream;
10701 }
10702 pthread_mutex_unlock(&adev->lock);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010703
10704 // Update routing for stream
10705 if (stream != NULL) {
Revathi Uddaraju4255a632021-12-02 05:11:13 -080010706 if (p_info->patch_type == PATCH_PLAYBACK) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010707 ret = route_output_stream((struct stream_out *) stream, &devices);
Revathi Uddaraju4255a632021-12-02 05:11:13 -080010708 } else if (p_info->patch_type == PATCH_CAPTURE) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010709 ret = route_input_stream((struct stream_in *) stream, &devices, input_source);
Revathi Uddaraju4255a632021-12-02 05:11:13 -080010710 }
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010711 if (ret < 0) {
10712 pthread_mutex_lock(&adev->lock);
10713 s_info->patch_handle = AUDIO_PATCH_HANDLE_NONE;
10714 if (new_patch)
10715 free(p_info);
10716 pthread_mutex_unlock(&adev->lock);
10717 ALOGE("%s: Stream routing failed for io_handle %d", __func__, io_handle);
10718 goto done;
10719 }
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010720 }
10721
10722 // Add new patch to patch map
10723 if (!ret && new_patch) {
10724 pthread_mutex_lock(&adev->lock);
10725 hashmapPut(adev->patch_map, (void *) (intptr_t) *handle, (void *) p_info);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010726 ALOGD("%s: Added a new patch with handle %d", __func__, *handle);
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010727 pthread_mutex_unlock(&adev->lock);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010728 }
10729
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010730done:
Zhenlin Lian4f947842022-05-14 15:50:52 +053010731 clear_devices(&devices);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010732 audio_extn_hw_loopback_create_audio_patch(dev,
Derek Chenf939fb72018-11-13 13:34:41 -080010733 num_sources,
10734 sources,
10735 num_sinks,
10736 sinks,
10737 handle);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010738 audio_extn_auto_hal_create_audio_patch(dev,
Derek Chenf939fb72018-11-13 13:34:41 -080010739 num_sources,
10740 sources,
10741 num_sinks,
10742 sinks,
10743 handle);
10744 return ret;
Siddartha Shaik31b530e2017-05-19 15:26:33 +053010745}
10746
10747int adev_release_audio_patch(struct audio_hw_device *dev,
10748 audio_patch_handle_t handle)
10749{
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010750 struct audio_device *adev = (struct audio_device *) dev;
10751 int ret = 0;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010752 audio_source_t input_source = AUDIO_SOURCE_DEFAULT;
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010753 struct audio_stream *stream = NULL;
Derek Chenf939fb72018-11-13 13:34:41 -080010754
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010755 if (handle == AUDIO_PATCH_HANDLE_NONE) {
10756 ALOGE("%s: Invalid patch handle %d", __func__, handle);
10757 ret = -EINVAL;
10758 goto done;
10759 }
10760
10761 ALOGD("%s: Remove patch with handle %d", __func__, handle);
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010762 pthread_mutex_lock(&adev->lock);
10763 struct audio_patch_info *p_info = fetch_patch_info_l(adev, handle);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010764 if (p_info == NULL) {
10765 ALOGE("%s: Patch info not found with handle %d", __func__, handle);
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010766 pthread_mutex_unlock(&adev->lock);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010767 ret = -EINVAL;
10768 goto done;
10769 }
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010770 struct audio_patch *patch = p_info->patch;
10771 if (patch == NULL) {
10772 ALOGE("%s: Patch not found for handle %d", __func__, handle);
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010773 pthread_mutex_unlock(&adev->lock);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010774 ret = -EINVAL;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010775 goto done;
10776 }
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010777 audio_io_handle_t io_handle = AUDIO_IO_HANDLE_NONE;
10778 switch (patch->sources[0].type) {
10779 case AUDIO_PORT_TYPE_MIX:
10780 io_handle = patch->sources[0].ext.mix.handle;
10781 break;
10782 case AUDIO_PORT_TYPE_DEVICE:
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010783 if (p_info->patch_type == PATCH_CAPTURE)
10784 io_handle = patch->sinks[0].ext.mix.handle;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010785 break;
10786 case AUDIO_PORT_TYPE_SESSION:
10787 case AUDIO_PORT_TYPE_NONE:
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010788 pthread_mutex_unlock(&adev->lock);
10789 ret = -EINVAL;
10790 goto done;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010791 }
10792
10793 // Remove patch and reset patch handle in stream info
Aniket Kumar Latace2b9fa2020-03-10 15:04:15 -070010794 patch_type_t patch_type = p_info->patch_type;
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010795 patch_map_remove_l(adev, handle);
Aniket Kumar Latace2b9fa2020-03-10 15:04:15 -070010796 if (patch_type == PATCH_PLAYBACK ||
10797 patch_type == PATCH_CAPTURE) {
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010798 struct audio_stream_info *s_info =
10799 hashmapGet(adev->io_streams_map, (void *) (intptr_t) io_handle);
10800 if (s_info == NULL) {
10801 ALOGE("%s: stream for io_handle %d is not available", __func__, io_handle);
10802 pthread_mutex_unlock(&adev->lock);
10803 goto done;
10804 }
10805 s_info->patch_handle = AUDIO_PATCH_HANDLE_NONE;
10806 stream = s_info->stream;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010807 }
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010808 pthread_mutex_unlock(&adev->lock);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010809
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010810 if (stream != NULL) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010811 struct listnode devices;
10812 list_init(&devices);
Aniket Kumar Latace2b9fa2020-03-10 15:04:15 -070010813 if (patch_type == PATCH_PLAYBACK)
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010814 ret = route_output_stream((struct stream_out *) stream, &devices);
Aniket Kumar Latace2b9fa2020-03-10 15:04:15 -070010815 else if (patch_type == PATCH_CAPTURE)
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010816 ret = route_input_stream((struct stream_in *) stream, &devices, input_source);
Zhenlin Lian4f947842022-05-14 15:50:52 +053010817 clear_devices(&devices);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010818 }
10819
10820 if (ret < 0)
10821 ALOGW("%s: Stream routing failed for io_handle %d", __func__, io_handle);
10822
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010823done:
10824 audio_extn_hw_loopback_release_audio_patch(dev, handle);
10825 audio_extn_auto_hal_release_audio_patch(dev, handle);
10826
10827 ALOGV("%s: Successfully released patch %d", __func__, handle);
Derek Chenf939fb72018-11-13 13:34:41 -080010828 return ret;
Siddartha Shaik31b530e2017-05-19 15:26:33 +053010829}
10830
10831int adev_get_audio_port(struct audio_hw_device *dev, struct audio_port *config)
10832{
Derek Chenf13dd492018-11-13 14:53:51 -080010833 int ret = 0;
10834
10835 ret = audio_extn_hw_loopback_get_audio_port(dev, config);
10836 ret |= audio_extn_auto_hal_get_audio_port(dev, config);
10837 return ret;
Siddartha Shaik31b530e2017-05-19 15:26:33 +053010838}
10839
10840int adev_set_audio_port_config(struct audio_hw_device *dev,
10841 const struct audio_port_config *config)
10842{
Derek Chenf13dd492018-11-13 14:53:51 -080010843 int ret = 0;
10844
10845 ret = audio_extn_hw_loopback_set_audio_port_config(dev, config);
10846 ret |= audio_extn_auto_hal_set_audio_port_config(dev, config);
10847 return ret;
Siddartha Shaik31b530e2017-05-19 15:26:33 +053010848}
10849
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -070010850static int adev_dump(const audio_hw_device_t *device __unused,
10851 int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010852{
10853 return 0;
10854}
10855
10856static int adev_close(hw_device_t *device)
10857{
Aalique Grahame22e49102018-12-18 14:23:57 -080010858 size_t i;
Preetam Singh Ranawatacc64542019-06-24 15:11:28 +053010859 struct audio_device *adev_temp = (struct audio_device *)device;
Kiran Kandi910e1862013-10-29 13:29:42 -070010860
Preetam Singh Ranawatacc64542019-06-24 15:11:28 +053010861 if (!adev_temp)
Kiran Kandi910e1862013-10-29 13:29:42 -070010862 return 0;
10863
10864 pthread_mutex_lock(&adev_init_lock);
10865
10866 if ((--audio_device_ref_count) == 0) {
Sujin Panicker390724d2019-04-26 10:43:36 +053010867 if (audio_extn_spkr_prot_is_enabled())
10868 audio_extn_spkr_prot_deinit();
Daniel Hillenbrand17873952013-05-23 10:10:00 +053010869 if (amplifier_close() != 0)
10870 ALOGE("Amplifier close failed");
Jaideep Sharmaa2b49672019-09-10 20:37:03 +053010871 audio_extn_battery_properties_listener_deinit();
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053010872 audio_extn_snd_mon_unregister_listener(adev);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -070010873 audio_extn_sound_trigger_deinit(adev);
Kiran Kandide144c82013-11-20 15:58:32 -080010874 audio_extn_listen_deinit(adev);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -080010875 audio_extn_qdsp_deinit();
Aalique Grahame22e49102018-12-18 14:23:57 -080010876 audio_extn_extspk_deinit(adev->extspk);
Dhananjay Kumard6d32152016-10-13 16:11:03 +053010877 audio_extn_utils_release_streams_cfg_lists(
10878 &adev->streams_output_cfg_list,
10879 &adev->streams_input_cfg_list);
Sidipotu Ashokd2f10ba2019-05-06 15:41:56 +053010880 if (audio_extn_qap_is_enabled())
10881 audio_extn_qap_deinit();
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +053010882 if (audio_extn_qaf_is_enabled())
10883 audio_extn_qaf_deinit();
Kiran Kandi910e1862013-10-29 13:29:42 -070010884 audio_route_free(adev->audio_route);
Weiyin Jiangfa65d3e2019-03-05 23:39:45 +080010885 audio_extn_gef_deinit(adev);
Kiran Kandi910e1862013-10-29 13:29:42 -070010886 free(adev->snd_dev_ref_cnt);
10887 platform_deinit(adev->platform);
Aalique Grahame22e49102018-12-18 14:23:57 -080010888 for (i = 0; i < ARRAY_SIZE(adev->use_case_table); ++i) {
10889 pcm_params_free(adev->use_case_table[i]);
10890 }
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -070010891 if (adev->adm_deinit)
10892 adev->adm_deinit(adev->adm_data);
Dhananjay Kumaree4d2002016-10-25 18:02:58 +053010893 qahwi_deinit(device);
Ben Rombergerd771a7c2017-02-22 18:05:17 -080010894 audio_extn_adsp_hdlr_deinit();
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053010895 audio_extn_snd_mon_deinit();
Siddartha Shaik44dd7702017-06-14 12:13:25 +053010896 audio_extn_hw_loopback_deinit(adev);
Garmond Leunge2433c32017-09-28 21:51:22 -070010897 audio_extn_ffv_deinit();
Satish Babu Patakokilac3c5d432017-07-04 22:48:59 +053010898 if (adev->device_cfg_params) {
10899 free(adev->device_cfg_params);
10900 adev->device_cfg_params = NULL;
10901 }
Derek Chend2530072014-11-24 12:39:14 -080010902 if(adev->ext_hw_plugin)
10903 audio_extn_ext_hw_plugin_deinit(adev->ext_hw_plugin);
Derek Chenae7b0342019-02-08 15:17:04 -080010904 audio_extn_auto_hal_deinit();
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010905 free_map(adev->patch_map);
10906 free_map(adev->io_streams_map);
Kiran Kandi910e1862013-10-29 13:29:42 -070010907 free(device);
10908 adev = NULL;
10909 }
10910 pthread_mutex_unlock(&adev_init_lock);
Vatsal Buchac09ae062018-11-14 13:25:08 +053010911 enable_gcov();
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010912 return 0;
10913}
10914
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -070010915/* This returns 1 if the input parameter looks at all plausible as a low latency period size,
10916 * or 0 otherwise. A return value of 1 doesn't mean the value is guaranteed to work,
10917 * just that it _might_ work.
10918 */
10919static int period_size_is_plausible_for_low_latency(int period_size)
10920{
10921 switch (period_size) {
10922 case 160:
Ravi Kumar Alamanda474de5a2015-06-25 20:08:01 -070010923 case 192:
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -070010924 case 240:
10925 case 320:
10926 case 480:
10927 return 1;
10928 default:
10929 return 0;
10930 }
10931}
10932
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053010933static void adev_snd_mon_cb(void *cookie, struct str_parms *parms)
10934{
10935 bool is_snd_card_status = false;
10936 bool is_ext_device_status = false;
10937 char value[32];
10938 int card = -1;
10939 card_status_t status;
10940
10941 if (cookie != adev || !parms)
10942 return;
10943
10944 if (!parse_snd_card_status(parms, &card, &status)) {
10945 is_snd_card_status = true;
10946 } else if (0 < str_parms_get_str(parms, "ext_audio_device", value, sizeof(value))) {
10947 is_ext_device_status = true;
10948 } else {
10949 // not a valid event
10950 return;
10951 }
10952
10953 pthread_mutex_lock(&adev->lock);
10954 if (card == adev->snd_card || is_ext_device_status) {
10955 if (is_snd_card_status && adev->card_status != status) {
Jaideep Sharmacb402512020-09-24 17:51:07 +053010956 ALOGD("%s card_status %d", __func__, status);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053010957 adev->card_status = status;
10958 platform_snd_card_update(adev->platform, status);
10959 audio_extn_fm_set_parameters(adev, parms);
Derek Chend6f371d2019-03-01 13:45:58 -080010960 audio_extn_auto_hal_set_parameters(adev, parms);
Zhou Song4f43e352019-07-02 00:32:23 +080010961 if (status == CARD_STATUS_OFFLINE)
10962 audio_extn_sco_reset_configuration();
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053010963 } else if (is_ext_device_status) {
10964 platform_set_parameters(adev->platform, parms);
10965 }
10966 }
10967 pthread_mutex_unlock(&adev->lock);
10968 return;
10969}
10970
Weiyin Jiang280ea742020-09-08 20:28:22 +080010971/* adev lock held */
10972int check_a2dp_restore_l(struct audio_device *adev, struct stream_out *out, bool restore)
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010973{
10974 struct audio_usecase *uc_info;
Zhou Song407e5aa2020-12-27 19:13:04 +080010975 struct audio_usecase *usecase;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010976 struct listnode devices;
Zhou Song407e5aa2020-12-27 19:13:04 +080010977 struct listnode *node;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010978
10979 uc_info = get_usecase_from_list(adev, out->usecase);
10980 if (uc_info == NULL) {
10981 ALOGE("%s: Could not find the usecase (%d) in the list",
10982 __func__, out->usecase);
10983 return -EINVAL;
10984 }
Aniket Kumar Lata9c2fd892020-01-22 22:20:00 -080010985 list_init(&devices);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010986
Zhou Songbaddf9f2020-11-20 13:57:39 +080010987 ALOGD("%s: enter: usecase(%d: %s), a2dp muted %d", __func__,
10988 out->usecase, use_case_table[out->usecase], out->a2dp_muted);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010989
10990 if (restore) {
Weiyin Jiang280ea742020-09-08 20:28:22 +080010991 pthread_mutex_lock(&out->latch_lock);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010992 // restore A2DP device for active usecases and unmute if required
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010993 if (is_a2dp_out_device_type(&out->device_list)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010994 ALOGD("%s: restoring A2dp and unmuting stream", __func__);
Zhou Songe5225132019-09-26 15:33:36 +080010995 if (uc_info->out_snd_device != SND_DEVICE_OUT_BT_A2DP)
10996 select_devices(adev, uc_info->id);
Zhou Songbaddf9f2020-11-20 13:57:39 +080010997
10998 if (is_offload_usecase(out->usecase)) {
10999 if (uc_info->out_snd_device == SND_DEVICE_OUT_BT_A2DP)
Weiyin Jiang280ea742020-09-08 20:28:22 +080011000 out_set_compr_volume(&out->stream, out->volume_l, out->volume_r);
Zhou Songbaddf9f2020-11-20 13:57:39 +080011001 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP) {
11002 out_set_voip_volume(&out->stream, out->volume_l, out->volume_r);
11003 } else {
11004 out_set_pcm_volume(&out->stream, out->volume_l, out->volume_r);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053011005 }
Zhou Songbaddf9f2020-11-20 13:57:39 +080011006 out->a2dp_muted = false;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053011007 }
Weiyin Jiang280ea742020-09-08 20:28:22 +080011008 pthread_mutex_unlock(&out->latch_lock);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053011009 } else {
Weiyin Jiang280ea742020-09-08 20:28:22 +080011010 pthread_mutex_lock(&out->latch_lock);
Zhou Songbaddf9f2020-11-20 13:57:39 +080011011 // mute stream and switch to speaker if suspended
11012 if (!out->a2dp_muted && !out->standby) {
Zhou Songbaddf9f2020-11-20 13:57:39 +080011013 assign_devices(&devices, &out->device_list);
11014 reassign_device_list(&out->device_list, AUDIO_DEVICE_OUT_SPEAKER, "");
Zhou Song407e5aa2020-12-27 19:13:04 +080011015 list_for_each(node, &adev->usecase_list) {
11016 usecase = node_to_item(node, struct audio_usecase, list);
Zhou Songe63b6fe2021-04-15 13:54:46 +080011017 if ((usecase->type != PCM_CAPTURE) && (usecase != uc_info) &&
11018 !is_a2dp_out_device_type(&usecase->stream.out->device_list) &&
Zhou Songcf77af02021-05-14 18:21:14 +080011019 !is_sco_out_device_type(&usecase->stream.out->device_list) &&
Zhou Songe63b6fe2021-04-15 13:54:46 +080011020 platform_check_backends_match(SND_DEVICE_OUT_SPEAKER,
11021 usecase->out_snd_device)) {
Zhou Song407e5aa2020-12-27 19:13:04 +080011022 assign_devices(&out->device_list, &usecase->stream.out->device_list);
11023 break;
11024 }
11025 }
Zhou Songcf77af02021-05-14 18:21:14 +080011026 if ((is_a2dp_out_device_type(&devices) && list_length(&devices) == 1) ||
11027 (uc_info->out_snd_device == SND_DEVICE_OUT_BT_A2DP)) {
Zhou Song8edbbdb2021-01-14 16:48:03 +080011028 out->a2dp_muted = true;
11029 if (is_offload_usecase(out->usecase)) {
11030 if (out->offload_state == OFFLOAD_STATE_PLAYING)
11031 compress_pause(out->compr);
11032 out_set_compr_volume(&out->stream, (float)0, (float)0);
Zhou Song8edbbdb2021-01-14 16:48:03 +080011033 } else {
Weiyin Jiang906db3c2021-03-02 13:17:04 +080011034 if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP)
11035 out_set_voip_volume(&out->stream, (float)0, (float)0);
11036 else
11037 out_set_pcm_volume(&out->stream, (float)0, (float)0);
11038
Zhou Song8edbbdb2021-01-14 16:48:03 +080011039 /* wait for stale pcm drained before switching to speaker */
11040 uint32_t latency =
11041 (out->config.period_count * out->config.period_size * 1000) /
11042 (out->config.rate);
11043 usleep(latency * 1000);
11044 }
Zhou Songbaddf9f2020-11-20 13:57:39 +080011045 }
11046 select_devices(adev, out->usecase);
Zhou Song8edbbdb2021-01-14 16:48:03 +080011047 ALOGD("%s: switched to device:%s and stream muted:%d", __func__,
11048 platform_get_snd_device_name(uc_info->out_snd_device), out->a2dp_muted);
Zhou Songbaddf9f2020-11-20 13:57:39 +080011049 if (is_offload_usecase(out->usecase)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053011050 if (out->offload_state == OFFLOAD_STATE_PLAYING)
11051 compress_resume(out->compr);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053011052 }
Zhou Songbaddf9f2020-11-20 13:57:39 +080011053 assign_devices(&out->device_list, &devices);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053011054 }
Weiyin Jiang280ea742020-09-08 20:28:22 +080011055 pthread_mutex_unlock(&out->latch_lock);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053011056 }
Zhenlin Lian4f947842022-05-14 15:50:52 +053011057 clear_devices(&devices);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053011058 ALOGV("%s: exit", __func__);
11059 return 0;
11060}
11061
Haynes Mathew George01156f92018-04-13 15:29:54 -070011062void adev_on_battery_status_changed(bool charging)
11063{
11064 pthread_mutex_lock(&adev->lock);
11065 ALOGI("%s: battery status changed to %scharging", __func__, charging ? "" : "not ");
11066 adev->is_charging = charging;
Zhou Songc1088ea2018-06-12 00:17:29 +080011067 audio_extn_sound_trigger_update_battery_status(charging);
Haynes Mathew George01156f92018-04-13 15:29:54 -070011068 pthread_mutex_unlock(&adev->lock);
11069}
11070
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080011071static int adev_open(const hw_module_t *module, const char *name,
11072 hw_device_t **device)
11073{
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +053011074 int ret;
Derek Chenf939fb72018-11-13 13:34:41 -080011075 char value[PROPERTY_VALUE_MAX] = {0};
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +053011076 char mixer_ctl_name[128] = {0};
11077 struct mixer_ctl *ctl = NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +053011078
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -080011079 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080011080 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
11081
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070011082 pthread_mutex_lock(&adev_init_lock);
Kiran Kandi910e1862013-10-29 13:29:42 -070011083 if (audio_device_ref_count != 0){
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070011084 *device = &adev->device.common;
Kiran Kandi910e1862013-10-29 13:29:42 -070011085 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070011086 ALOGD("%s: returning existing instance of adev", __func__);
11087 ALOGD("%s: exit", __func__);
11088 pthread_mutex_unlock(&adev_init_lock);
11089 return 0;
11090 }
11091
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080011092 adev = calloc(1, sizeof(struct audio_device));
11093
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -070011094 if (!adev) {
11095 pthread_mutex_unlock(&adev_init_lock);
11096 return -ENOMEM;
11097 }
11098
Ravi Kumar Alamanda40703102014-04-24 10:34:41 -070011099 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
11100
Weiyin Jiange6ce6312019-01-28 18:28:22 +080011101 // register audio ext hidl at the earliest
11102 audio_extn_hidl_init();
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053011103#ifdef DYNAMIC_LOG_ENABLED
11104 register_for_dynamic_logging("hal");
11105#endif
11106
Derek Chenf939fb72018-11-13 13:34:41 -080011107 /* default audio HAL major version */
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070011108 uint32_t maj_version = 3;
Derek Chenf939fb72018-11-13 13:34:41 -080011109 if(property_get("vendor.audio.hal.maj.version", value, NULL))
11110 maj_version = atoi(value);
11111
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080011112 adev->device.common.tag = HARDWARE_DEVICE_TAG;
Derek Chenf939fb72018-11-13 13:34:41 -080011113 adev->device.common.version = HARDWARE_DEVICE_API_VERSION(maj_version, 0);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080011114 adev->device.common.module = (struct hw_module_t *)module;
11115 adev->device.common.close = adev_close;
11116
11117 adev->device.init_check = adev_init_check;
11118 adev->device.set_voice_volume = adev_set_voice_volume;
11119 adev->device.set_master_volume = adev_set_master_volume;
11120 adev->device.get_master_volume = adev_get_master_volume;
11121 adev->device.set_master_mute = adev_set_master_mute;
11122 adev->device.get_master_mute = adev_get_master_mute;
11123 adev->device.set_mode = adev_set_mode;
11124 adev->device.set_mic_mute = adev_set_mic_mute;
11125 adev->device.get_mic_mute = adev_get_mic_mute;
11126 adev->device.set_parameters = adev_set_parameters;
11127 adev->device.get_parameters = adev_get_parameters;
11128 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
11129 adev->device.open_output_stream = adev_open_output_stream;
11130 adev->device.close_output_stream = adev_close_output_stream;
11131 adev->device.open_input_stream = adev_open_input_stream;
11132 adev->device.close_input_stream = adev_close_input_stream;
Siddartha Shaik31b530e2017-05-19 15:26:33 +053011133 adev->device.create_audio_patch = adev_create_audio_patch;
11134 adev->device.release_audio_patch = adev_release_audio_patch;
11135 adev->device.get_audio_port = adev_get_audio_port;
11136 adev->device.set_audio_port_config = adev_set_audio_port_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080011137 adev->device.dump = adev_dump;
Naresh Tannirudcb47c52018-06-25 16:23:32 +053011138 adev->device.get_microphones = adev_get_microphones;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080011139
11140 /* Set the default route before the PCM stream is opened */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080011141 adev->mode = AUDIO_MODE_NORMAL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -080011142 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080011143 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080011144 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -080011145 adev->acdb_settings = TTY_MODE_OFF;
vivek mehta344576a2016-04-12 18:56:03 -070011146 adev->allow_afe_proxy_usage = true;
Ashish Jain1b9b30c2017-05-18 20:57:40 +053011147 adev->bt_sco_on = false;
Eric Laurent07eeafd2013-10-06 12:52:49 -070011148 /* adev->cur_hdmi_channels = 0; by calloc() */
Eric Laurentb23d5282013-05-14 15:27:20 -070011149 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
vivek mehtaae1018c2019-05-09 12:19:57 -070011150 /* Init audio and voice feature */
11151 audio_extn_feature_init();
11152 voice_extn_feature_init();
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -070011153 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -080011154 list_init(&adev->usecase_list);
Derek Chenf939fb72018-11-13 13:34:41 -080011155 list_init(&adev->active_inputs_list);
11156 list_init(&adev->active_outputs_list);
Rahul Sharma99770982019-03-06 17:05:26 +053011157 list_init(&adev->audio_patch_record_list);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070011158 adev->io_streams_map = hashmapCreate(AUDIO_IO_PORTS_MAX, audio_extn_utils_hash_fn,
11159 audio_extn_utils_hash_eq);
11160 if (!adev->io_streams_map) {
11161 ALOGE("%s: Could not create io streams map", __func__);
11162 ret = -ENOMEM;
11163 goto adev_open_err;
11164 }
11165 adev->patch_map = hashmapCreate(AUDIO_PATCH_PORTS_MAX, audio_extn_utils_hash_fn,
11166 audio_extn_utils_hash_eq);
11167 if (!adev->patch_map) {
11168 ALOGE("%s: Could not create audio patch map", __func__);
11169 ret = -ENOMEM;
11170 goto adev_open_err;
11171 }
Krishnankutty Kolathappilly0b2de1c2014-02-14 14:45:49 -080011172 adev->cur_wfd_channels = 2;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -070011173 adev->offload_usecases_state = 0;
Samyak Jain15fda662018-12-18 16:40:52 +053011174 adev->pcm_record_uc_state = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +053011175 adev->is_channel_status_set = false;
Sudheer Papothifa9d2282015-09-17 01:53:25 +053011176 adev->perf_lock_opts[0] = 0x101;
11177 adev->perf_lock_opts[1] = 0x20E;
11178 adev->perf_lock_opts_size = 2;
Xiaojun Sang785b5da2017-08-03 15:52:29 +080011179 adev->dsp_bit_width_enforce_mode = 0;
Aalique Grahame552b0832019-03-11 10:16:38 -070011180 adev->enable_hfp = false;
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +053011181 adev->use_old_pspd_mix_ctrl = false;
Aniket Kumar Lata60586a92019-05-22 22:18:55 -070011182 adev->adm_routing_changed = false;
Revathi Uddarajub26e3932020-06-10 14:51:02 +053011183 adev->a2dp_started = false;
Zhou Song503196b2021-07-23 17:31:05 +080011184 adev->ha_proxy_enable = false;
Naresh Tanniru4c630392014-05-12 01:05:52 +053011185
Zhou Song68ebc352019-12-05 17:11:15 +080011186 audio_extn_perf_lock_init();
11187
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080011188 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -070011189 adev->platform = platform_init(adev);
11190 if (!adev->platform) {
Eric Laurentb23d5282013-05-14 15:27:20 -070011191 ALOGE("%s: Failed to init platform data, aborting.", __func__);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070011192 ret = -EINVAL;
11193 goto adev_open_err;
Eric Laurentb23d5282013-05-14 15:27:20 -070011194 }
Eric Laurentc4aef752013-09-12 17:45:53 -070011195
Aalique Grahame22e49102018-12-18 14:23:57 -080011196 adev->extspk = audio_extn_extspk_init(adev);
Sidipotu Ashokd2f10ba2019-05-06 15:41:56 +053011197 if (audio_extn_qap_is_enabled()) {
11198 ret = audio_extn_qap_init(adev);
11199 if (ret < 0) {
Sidipotu Ashokd2f10ba2019-05-06 15:41:56 +053011200 ALOGE("%s: Failed to init platform data, aborting.", __func__);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070011201 goto adev_open_err;
Sidipotu Ashokd2f10ba2019-05-06 15:41:56 +053011202 }
11203 adev->device.open_output_stream = audio_extn_qap_open_output_stream;
11204 adev->device.close_output_stream = audio_extn_qap_close_output_stream;
11205 }
Aalique Grahame22e49102018-12-18 14:23:57 -080011206
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +053011207 if (audio_extn_qaf_is_enabled()) {
11208 ret = audio_extn_qaf_init(adev);
11209 if (ret < 0) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +053011210 ALOGE("%s: Failed to init platform data, aborting.", __func__);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070011211 goto adev_open_err;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +053011212 }
11213
11214 adev->device.open_output_stream = audio_extn_qaf_open_output_stream;
11215 adev->device.close_output_stream = audio_extn_qaf_close_output_stream;
11216 }
11217
Derek Chenae7b0342019-02-08 15:17:04 -080011218 audio_extn_auto_hal_init(adev);
Derek Chend2530072014-11-24 12:39:14 -080011219 adev->ext_hw_plugin = audio_extn_ext_hw_plugin_init(adev);
11220
Eric Laurentc4aef752013-09-12 17:45:53 -070011221 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
11222 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
11223 if (adev->visualizer_lib == NULL) {
11224 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
11225 } else {
11226 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
11227 adev->visualizer_start_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -080011228 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -070011229 "visualizer_hal_start_output");
11230 adev->visualizer_stop_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -080011231 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -070011232 "visualizer_hal_stop_output");
11233 }
11234 }
Dhanalakshmi Siddani21be3ac2016-12-29 14:31:08 +053011235 audio_extn_init(adev);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -080011236 voice_extn_init(adev);
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -080011237 audio_extn_listen_init(adev, adev->snd_card);
Weiyin Jiangaa80acd2016-09-21 16:42:11 +080011238 audio_extn_gef_init(adev);
Siddartha Shaik44dd7702017-06-14 12:13:25 +053011239 audio_extn_hw_loopback_init(adev);
Garmond Leunge2433c32017-09-28 21:51:22 -070011240 audio_extn_ffv_init(adev);
Eric Laurentc4aef752013-09-12 17:45:53 -070011241
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -080011242 if (access(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, R_OK) == 0) {
11243 adev->offload_effects_lib = dlopen(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, RTLD_NOW);
11244 if (adev->offload_effects_lib == NULL) {
11245 ALOGE("%s: DLOPEN failed for %s", __func__,
11246 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
11247 } else {
11248 ALOGV("%s: DLOPEN successful for %s", __func__,
11249 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
11250 adev->offload_effects_start_output =
Ashish Jain5106d362016-05-11 19:23:33 +053011251 (int (*)(audio_io_handle_t, int, struct mixer *))dlsym(adev->offload_effects_lib,
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -080011252 "offload_effects_bundle_hal_start_output");
11253 adev->offload_effects_stop_output =
11254 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
11255 "offload_effects_bundle_hal_stop_output");
Jitendra Naruka1b6513f2014-11-22 19:34:13 -080011256 adev->offload_effects_set_hpx_state =
11257 (int (*)(bool))dlsym(adev->offload_effects_lib,
11258 "offload_effects_bundle_set_hpx_state");
Dhananjay Kumard68883d2015-09-04 13:39:26 +053011259 adev->offload_effects_get_parameters =
11260 (void (*)(struct str_parms *, struct str_parms *))
11261 dlsym(adev->offload_effects_lib,
11262 "offload_effects_bundle_get_parameters");
11263 adev->offload_effects_set_parameters =
11264 (void (*)(struct str_parms *))dlsym(adev->offload_effects_lib,
11265 "offload_effects_bundle_set_parameters");
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -080011266 }
11267 }
11268
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -070011269 if (access(ADM_LIBRARY_PATH, R_OK) == 0) {
11270 adev->adm_lib = dlopen(ADM_LIBRARY_PATH, RTLD_NOW);
11271 if (adev->adm_lib == NULL) {
11272 ALOGE("%s: DLOPEN failed for %s", __func__, ADM_LIBRARY_PATH);
11273 } else {
11274 ALOGV("%s: DLOPEN successful for %s", __func__, ADM_LIBRARY_PATH);
11275 adev->adm_init = (adm_init_t)
11276 dlsym(adev->adm_lib, "adm_init");
11277 adev->adm_deinit = (adm_deinit_t)
11278 dlsym(adev->adm_lib, "adm_deinit");
11279 adev->adm_register_input_stream = (adm_register_input_stream_t)
11280 dlsym(adev->adm_lib, "adm_register_input_stream");
11281 adev->adm_register_output_stream = (adm_register_output_stream_t)
11282 dlsym(adev->adm_lib, "adm_register_output_stream");
11283 adev->adm_deregister_stream = (adm_deregister_stream_t)
11284 dlsym(adev->adm_lib, "adm_deregister_stream");
11285 adev->adm_request_focus = (adm_request_focus_t)
11286 dlsym(adev->adm_lib, "adm_request_focus");
11287 adev->adm_abandon_focus = (adm_abandon_focus_t)
11288 dlsym(adev->adm_lib, "adm_abandon_focus");
Haynes Mathew George5beddd42016-06-27 18:33:40 -070011289 adev->adm_set_config = (adm_set_config_t)
11290 dlsym(adev->adm_lib, "adm_set_config");
11291 adev->adm_request_focus_v2 = (adm_request_focus_v2_t)
11292 dlsym(adev->adm_lib, "adm_request_focus_v2");
11293 adev->adm_is_noirq_avail = (adm_is_noirq_avail_t)
11294 dlsym(adev->adm_lib, "adm_is_noirq_avail");
11295 adev->adm_on_routing_change = (adm_on_routing_change_t)
11296 dlsym(adev->adm_lib, "adm_on_routing_change");
Aniket Kumar Lata60586a92019-05-22 22:18:55 -070011297 adev->adm_request_focus_v2_1 = (adm_request_focus_v2_1_t)
11298 dlsym(adev->adm_lib, "adm_request_focus_v2_1");
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -070011299 }
11300 }
11301
Aalique Grahame22e49102018-12-18 14:23:57 -080011302 adev->enable_voicerx = false;
Mingming Yin514a8bc2014-07-29 15:22:21 -070011303 adev->bt_wb_speech_enabled = false;
Zhou Song12c29502019-03-16 10:37:18 +080011304 adev->swb_speech_mode = SPEECH_MODE_INVALID;
Kunlei Zhanga3c4cb02021-03-05 16:03:54 +080011305 adev->fluence_nn_usecase_id = USECASE_INVALID;
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -080011306 //initialize this to false for now,
11307 //this will be set to true through set param
11308 adev->vr_audio_mode_enabled = false;
Mingming Yin514a8bc2014-07-29 15:22:21 -070011309
Pradnya Chaphekar8a9dcd82014-09-09 09:49:10 -070011310 audio_extn_ds2_enable(adev);
Daniel Hillenbrand17873952013-05-23 10:10:00 +053011311
11312 if (amplifier_open(adev) != 0)
11313 ALOGE("Amplifier initialization failed");
11314
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080011315 *device = &adev->device.common;
Aalique Grahame22e49102018-12-18 14:23:57 -080011316
11317 if (k_enable_extended_precision)
11318 adev_verify_devices(adev);
11319
Xiaojun Sang785b5da2017-08-03 15:52:29 +080011320 adev->dsp_bit_width_enforce_mode =
11321 adev_init_dsp_bit_width_enforce_mode(adev->mixer);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080011322
Dhananjay Kumard6d32152016-10-13 16:11:03 +053011323 audio_extn_utils_update_streams_cfg_lists(adev->platform, adev->mixer,
11324 &adev->streams_output_cfg_list,
11325 &adev->streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070011326
Kiran Kandi910e1862013-10-29 13:29:42 -070011327 audio_device_ref_count++;
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -070011328
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -070011329 int trial;
Manisha Agarwalc75a0202019-12-06 18:48:25 +053011330 if (property_get("vendor.audio_hal.period_size", value, NULL) > 0) {
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -070011331 trial = atoi(value);
11332 if (period_size_is_plausible_for_low_latency(trial)) {
11333 pcm_config_low_latency.period_size = trial;
11334 pcm_config_low_latency.start_threshold = trial / 4;
11335 pcm_config_low_latency.avail_min = trial / 4;
11336 configured_low_latency_capture_period_size = trial;
11337 }
11338 }
ronghuiz93177262021-04-21 19:58:13 +080011339 if (property_get("vendor.audio_hal.in_period_size", value, NULL) > 0) {
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -070011340 trial = atoi(value);
11341 if (period_size_is_plausible_for_low_latency(trial)) {
11342 configured_low_latency_capture_period_size = trial;
11343 }
11344 }
11345
Vignesh Kulothungan7d374312018-02-21 17:12:00 -080011346 adev->mic_break_enabled = property_get_bool("vendor.audio.mic_break", false);
11347
Eric Laurent4b084132018-10-19 17:33:43 -070011348 adev->camera_orientation = CAMERA_DEFAULT;
11349
Manisha Agarwalc75a0202019-12-06 18:48:25 +053011350 if (property_get("vendor.audio_hal.period_multiplier",value,NULL) > 0) {
Haynes Mathew George5beddd42016-06-27 18:33:40 -070011351 af_period_multiplier = atoi(value);
11352 if (af_period_multiplier < 0)
11353 af_period_multiplier = 2;
11354 else if (af_period_multiplier > 4)
11355 af_period_multiplier = 4;
11356
11357 ALOGV("new period_multiplier = %d", af_period_multiplier);
11358 }
11359
Arun Mirpurib1bec9c2019-01-29 16:42:45 -080011360 audio_extn_qdsp_init(adev->platform);
Aalique Grahame22e49102018-12-18 14:23:57 -080011361
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -070011362 adev->multi_offload_enable = property_get_bool("vendor.audio.offload.multiple.enabled", false);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070011363 pthread_mutex_unlock(&adev_init_lock);
11364
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -070011365 if (adev->adm_init)
11366 adev->adm_data = adev->adm_init();
11367
Dhananjay Kumaree4d2002016-10-25 18:02:58 +053011368 qahwi_init(*device);
Ben Rombergerd771a7c2017-02-22 18:05:17 -080011369 audio_extn_adsp_hdlr_init(adev->mixer);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053011370
11371 audio_extn_snd_mon_init();
11372 pthread_mutex_lock(&adev->lock);
11373 audio_extn_snd_mon_register_listener(adev, adev_snd_mon_cb);
11374 adev->card_status = CARD_STATUS_ONLINE;
Shubhasini Sugumaran3fde6582022-01-19 15:54:50 -050011375 adev->out_power_policy = POWER_POLICY_STATUS_ONLINE;
11376 adev->in_power_policy = POWER_POLICY_STATUS_ONLINE;
Haynes Mathew George01156f92018-04-13 15:29:54 -070011377 audio_extn_battery_properties_listener_init(adev_on_battery_status_changed);
11378 /*
11379 * if the battery state callback happens before charging can be queried,
11380 * it will be guarded with the adev->lock held in the cb function and so
11381 * the callback value will reflect the latest state
11382 */
11383 adev->is_charging = audio_extn_battery_properties_is_charging();
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053011384 audio_extn_sound_trigger_init(adev); /* dependent on snd_mon_init() */
Zhou Songc1088ea2018-06-12 00:17:29 +080011385 audio_extn_sound_trigger_update_battery_status(adev->is_charging);
justinweng20fb6d82019-02-21 18:49:00 -070011386 audio_extn_audiozoom_init();
Zhou Songc1088ea2018-06-12 00:17:29 +080011387 pthread_mutex_unlock(&adev->lock);
Satish Babu Patakokilac3c5d432017-07-04 22:48:59 +053011388 /* Allocate memory for Device config params */
11389 adev->device_cfg_params = (struct audio_device_config_param*)
11390 calloc(platform_get_max_codec_backend(),
11391 sizeof(struct audio_device_config_param));
11392 if (adev->device_cfg_params == NULL)
11393 ALOGE("%s: Memory allocation failed for Device config params", __func__);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053011394
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +053011395 /*
11396 * Check if new PSPD matrix mixer control is supported. If not
11397 * supported, then set flag so that old mixer ctrl is sent while
11398 * sending pspd coefficients on older kernel version. Query mixer
11399 * control for default pcm id and channel value one.
11400 */
11401 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
11402 "AudStr %d ChMixer Weight Ch %d", 0, 1);
11403
11404 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
11405 if (!ctl) {
11406 ALOGE("%s: ERROR. Could not get ctl for mixer cmd - %s",
11407 __func__, mixer_ctl_name);
11408 adev->use_old_pspd_mix_ctrl = true;
11409 }
11410
Jaideep Sharma0fa53812020-09-17 09:00:11 +053011411 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080011412 return 0;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070011413
11414adev_open_err:
11415 free_map(adev->patch_map);
11416 free_map(adev->io_streams_map);
Aniket Kumar Latabc774842020-01-16 21:22:05 -080011417 free(adev->snd_dev_ref_cnt);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070011418 pthread_mutex_destroy(&adev->lock);
11419 free(adev);
11420 adev = NULL;
11421 *device = NULL;
11422 pthread_mutex_unlock(&adev_init_lock);
11423 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080011424}
11425
11426static struct hw_module_methods_t hal_module_methods = {
11427 .open = adev_open,
11428};
11429
11430struct audio_module HAL_MODULE_INFO_SYM = {
11431 .common = {
11432 .tag = HARDWARE_MODULE_TAG,
11433 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
11434 .hal_api_version = HARDWARE_HAL_API_VERSION,
11435 .id = AUDIO_HARDWARE_MODULE_ID,
11436 .name = "QCOM Audio HAL",
Duy Truongfae19622013-11-24 02:17:54 -080011437 .author = "The Linux Foundation",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080011438 .methods = &hal_module_methods,
11439 },
11440};