blob: ff0558e562eafc52c301e9c31a8b85cdb0110197 [file] [log] [blame]
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001/*
Zhou Song642ec432020-12-23 16:11:10 +08002 * Copyright (c) 2013-2021, The Linux Foundation. All rights reserved.
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07003 * Not a Contribution.
4 *
Shiv Maliyappanahalli8911f282014-01-10 15:56:19 -08005 * Copyright (C) 2013 The Android Open Source Project
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Jitendra Naruka1b6513f2014-11-22 19:34:13 -080018 *
19 * This file was modified by DTS, Inc. The portions of the
20 * code modified by DTS, Inc are copyrighted and
21 * licensed separately, as follows:
22 *
23 * (C) 2014 DTS, Inc.
24 *
25 * Licensed under the Apache License, Version 2.0 (the "License");
26 * you may not use this file except in compliance with the License.
27 * You may obtain a copy of the License at
28 *
29 * http://www.apache.org/licenses/LICENSE-2.0
30 *
31 * Unless required by applicable law or agreed to in writing, software
32 * distributed under the License is distributed on an "AS IS" BASIS,
33 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34 * See the License for the specific language governing permissions and
35 * limitations under the License.
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080036 */
37
38#define LOG_TAG "audio_hw_primary"
Haynes Mathew George5beddd42016-06-27 18:33:40 -070039#define ATRACE_TAG (ATRACE_TAG_AUDIO|ATRACE_TAG_HAL)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080040/*#define LOG_NDEBUG 0*/
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070041/*#define VERY_VERY_VERBOSE_LOGGING*/
42#ifdef VERY_VERY_VERBOSE_LOGGING
43#define ALOGVV ALOGV
44#else
45#define ALOGVV(a...) do { } while(0)
46#endif
George Gao3018ede2019-10-23 13:23:00 -070047#include <limits.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080048#include <errno.h>
49#include <pthread.h>
50#include <stdint.h>
51#include <sys/time.h>
52#include <stdlib.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080053#include <math.h>
Eric Laurentc4aef752013-09-12 17:45:53 -070054#include <dlfcn.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070055#include <sys/resource.h>
56#include <sys/prctl.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080057
Aalique Grahame22e49102018-12-18 14:23:57 -080058#include <log/log.h>
Haynes Mathew George5beddd42016-06-27 18:33:40 -070059#include <cutils/trace.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080060#include <cutils/str_parms.h>
61#include <cutils/properties.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070062#include <cutils/atomic.h>
63#include <cutils/sched_policy.h>
Eric Laurentb23d5282013-05-14 15:27:20 -070064#include <hardware/audio_effect.h>
Haynes Mathew George484e8d22017-07-31 18:55:17 -070065#include <hardware/audio_alsaops.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070066#include <system/thread_defs.h>
Haynes Mathew George16081042017-05-31 17:16:49 -070067#include <tinyalsa/asoundlib.h>
Andy Hunga1f48fa2019-07-01 18:14:53 -070068#include <utils/Timers.h> // systemTime
Eric Laurentb23d5282013-05-14 15:27:20 -070069#include <audio_effects/effect_aec.h>
70#include <audio_effects/effect_ns.h>
Ashish Jainf1eaa582016-05-23 20:54:24 +053071#include <audio_utils/format.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080072#include "audio_hw.h"
Wei Wangf7ca6c92017-11-21 14:51:20 -080073#include "audio_perf.h"
Eric Laurentb23d5282013-05-14 15:27:20 -070074#include "platform_api.h"
75#include <platform.h>
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -070076#include "audio_extn.h"
Narsinga Rao Chella05573b72013-11-15 15:21:40 -080077#include "voice_extn.h"
Ashish Jaind5694242017-09-05 20:09:06 +053078#include "ip_hdlr_intf.h"
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080079
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070080#include "sound/compress_params.h"
Xiaojun Sang782e5b12020-06-29 21:13:06 +080081
82#ifdef AUDIO_GKI_ENABLED
83#include "sound/audio_compressed_formats.h"
84#endif
85
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -080086#include "sound/asound.h"
ApurupaPattapu2e084df2013-12-18 15:47:59 -080087
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053088#ifdef DYNAMIC_LOG_ENABLED
89#include <log_xml_parser.h>
90#define LOG_MASK HAL_MOD_FILE_AUDIO_HW
91#include <log_utils.h>
92#endif
93
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070094#define COMPRESS_OFFLOAD_NUM_FRAGMENTS 4
Ashish Jain5106d362016-05-11 19:23:33 +053095/*DIRECT PCM has same buffer sizes as DEEP Buffer*/
96#define DIRECT_PCM_NUM_FRAGMENTS 2
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070097#define COMPRESS_PLAYBACK_VOLUME_MAX 0x2000
Vikram Panduranga93f080e2017-06-07 18:16:14 -070098#define VOIP_PLAYBACK_VOLUME_MAX 0x2000
Arun Mirpuri5d170872019-03-26 13:21:31 -070099#define MMAP_PLAYBACK_VOLUME_MAX 0x2000
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +0530100#define PCM_PLAYBACK_VOLUME_MAX 0x2000
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530101#define DSD_VOLUME_MIN_DB (-110)
Aniket Kumar Lata8426d1f2019-06-10 15:25:53 -0700102#define INVALID_OUT_VOLUME -1
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700103#define AUDIO_IO_PORTS_MAX 32
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700104
Zhou Songbaddf9f2020-11-20 13:57:39 +0800105#define PLAYBACK_GAIN_MAX 1.0f
Aalique Grahame22e49102018-12-18 14:23:57 -0800106#define RECORD_GAIN_MIN 0.0f
107#define RECORD_GAIN_MAX 1.0f
108#define RECORD_VOLUME_CTL_MAX 0x2000
109
110/* treat as unsigned Q1.13 */
111#define APP_TYPE_GAIN_DEFAULT 0x2000
112
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -0700113#define PROXY_OPEN_RETRY_COUNT 100
114#define PROXY_OPEN_WAIT_TIME 20
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -0800115
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800116#define GET_USECASE_AUDIO_PLAYBACK_PRIMARY(db) \
117 (db)? USECASE_AUDIO_PLAYBACK_DEEP_BUFFER : \
118 USECASE_AUDIO_PLAYBACK_LOW_LATENCY
119#define GET_PCM_CONFIG_AUDIO_PLAYBACK_PRIMARY(db) \
120 (db)? pcm_config_deep_buffer : pcm_config_low_latency
Haynes Mathew Georgebf143712013-12-03 13:02:53 -0800121
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700122#define ULL_PERIOD_SIZE (DEFAULT_OUTPUT_SAMPLING_RATE/1000)
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700123#define DEFAULT_VOIP_BUF_DURATION_MS 20
124#define DEFAULT_VOIP_BIT_DEPTH_BYTE sizeof(int16_t)
125#define DEFAULT_VOIP_SAMP_RATE 48000
126
127#define VOIP_IO_BUF_SIZE(SR, DURATION_MS, BIT_DEPTH) (SR)/1000 * DURATION_MS * BIT_DEPTH
128
129struct pcm_config default_pcm_config_voip_copp = {
130 .channels = 1,
131 .rate = DEFAULT_VOIP_SAMP_RATE, /* changed when the stream is opened */
132 .period_size = VOIP_IO_BUF_SIZE(DEFAULT_VOIP_SAMP_RATE, DEFAULT_VOIP_BUF_DURATION_MS, DEFAULT_VOIP_BIT_DEPTH_BYTE)/2,
133 .period_count = 2,
134 .format = PCM_FORMAT_S16_LE,
kunleiz95b597a2017-10-23 17:07:33 +0800135 .avail_min = VOIP_IO_BUF_SIZE(DEFAULT_VOIP_SAMP_RATE, DEFAULT_VOIP_BUF_DURATION_MS, DEFAULT_VOIP_BIT_DEPTH_BYTE)/2,
136 .stop_threshold = INT_MAX,
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700137};
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700138
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700139#define MIN_CHANNEL_COUNT 1
140#define DEFAULT_CHANNEL_COUNT 2
141#define MAX_HIFI_CHANNEL_COUNT 8
142
Aalique Grahame22e49102018-12-18 14:23:57 -0800143#ifndef MAX_TARGET_SPECIFIC_CHANNEL_CNT
144#define MAX_CHANNEL_COUNT 1
145#else
146#define MAX_CHANNEL_COUNT atoi(XSTR(MAX_TARGET_SPECIFIC_CHANNEL_CNT))
147#define XSTR(x) STR(x)
148#define STR(x) #x
149#endif
150
Dechen Chai22768452021-07-30 09:29:16 +0530151#ifdef LINUX_ENABLED
152static inline int64_t audio_utils_ns_from_timespec(const struct timespec *ts)
153{
154 return ts->tv_sec * 1000000000LL + ts->tv_nsec;
155}
156#endif
157
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -0700158static unsigned int configured_low_latency_capture_period_size =
159 LOW_LATENCY_CAPTURE_PERIOD_SIZE;
160
Haynes Mathew George16081042017-05-31 17:16:49 -0700161#define MMAP_PERIOD_SIZE (DEFAULT_OUTPUT_SAMPLING_RATE/1000)
162#define MMAP_PERIOD_COUNT_MIN 32
163#define MMAP_PERIOD_COUNT_MAX 512
164#define MMAP_PERIOD_COUNT_DEFAULT (MMAP_PERIOD_COUNT_MAX)
165
Aalique Grahame22e49102018-12-18 14:23:57 -0800166/* This constant enables extended precision handling.
167 * TODO The flag is off until more testing is done.
168 */
169static const bool k_enable_extended_precision = false;
Arun Mirpurie008ed22019-03-21 11:21:04 -0700170extern int AUDIO_DEVICE_IN_ALL_CODEC_BACKEND;
Aalique Grahame22e49102018-12-18 14:23:57 -0800171
Eric Laurentb23d5282013-05-14 15:27:20 -0700172struct pcm_config pcm_config_deep_buffer = {
173 .channels = 2,
174 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
175 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
176 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
177 .format = PCM_FORMAT_S16_LE,
178 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
179 .stop_threshold = INT_MAX,
180 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
181};
182
183struct pcm_config pcm_config_low_latency = {
184 .channels = 2,
185 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
186 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
187 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
188 .format = PCM_FORMAT_S16_LE,
189 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
190 .stop_threshold = INT_MAX,
191 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
192};
193
Vignesh Kulothungana6927272019-02-20 15:17:07 -0800194struct pcm_config pcm_config_haptics_audio = {
195 .channels = 1,
196 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
197 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
198 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
199 .format = PCM_FORMAT_S16_LE,
200 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
201 .stop_threshold = INT_MAX,
202 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
203};
204
205struct pcm_config pcm_config_haptics = {
206 .channels = 1,
207 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
208 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
209 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
210 .format = PCM_FORMAT_S16_LE,
211 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
212 .stop_threshold = INT_MAX,
213 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
214};
215
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700216static int af_period_multiplier = 4;
217struct pcm_config pcm_config_rt = {
218 .channels = 2,
219 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
220 .period_size = ULL_PERIOD_SIZE, //1 ms
221 .period_count = 512, //=> buffer size is 512ms
222 .format = PCM_FORMAT_S16_LE,
223 .start_threshold = ULL_PERIOD_SIZE*8, //8ms
224 .stop_threshold = INT_MAX,
225 .silence_threshold = 0,
226 .silence_size = 0,
227 .avail_min = ULL_PERIOD_SIZE, //1 ms
228};
229
Eric Laurentb23d5282013-05-14 15:27:20 -0700230struct pcm_config pcm_config_hdmi_multi = {
231 .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
232 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
233 .period_size = HDMI_MULTI_PERIOD_SIZE,
234 .period_count = HDMI_MULTI_PERIOD_COUNT,
235 .format = PCM_FORMAT_S16_LE,
236 .start_threshold = 0,
237 .stop_threshold = INT_MAX,
238 .avail_min = 0,
239};
240
Haynes Mathew George16081042017-05-31 17:16:49 -0700241struct pcm_config pcm_config_mmap_playback = {
242 .channels = 2,
243 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
244 .period_size = MMAP_PERIOD_SIZE,
245 .period_count = MMAP_PERIOD_COUNT_DEFAULT,
246 .format = PCM_FORMAT_S16_LE,
247 .start_threshold = MMAP_PERIOD_SIZE*8,
248 .stop_threshold = INT32_MAX,
249 .silence_threshold = 0,
250 .silence_size = 0,
251 .avail_min = MMAP_PERIOD_SIZE, //1 ms
252};
253
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700254struct pcm_config pcm_config_hifi = {
255 .channels = DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
256 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
257 .period_size = HIFI_BUFFER_OUTPUT_PERIOD_SIZE, /* change #define */
258 .period_count = HIFI_BUFFER_OUTPUT_PERIOD_COUNT,
259 .format = PCM_FORMAT_S24_3LE,
260 .start_threshold = 0,
261 .stop_threshold = INT_MAX,
262 .avail_min = 0,
263};
264
Eric Laurentb23d5282013-05-14 15:27:20 -0700265struct pcm_config pcm_config_audio_capture = {
266 .channels = 2,
Eric Laurentb23d5282013-05-14 15:27:20 -0700267 .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
268 .format = PCM_FORMAT_S16_LE,
269};
270
Haynes Mathew George16081042017-05-31 17:16:49 -0700271struct pcm_config pcm_config_mmap_capture = {
272 .channels = 2,
273 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
274 .period_size = MMAP_PERIOD_SIZE,
275 .period_count = MMAP_PERIOD_COUNT_DEFAULT,
276 .format = PCM_FORMAT_S16_LE,
277 .start_threshold = 0,
278 .stop_threshold = INT_MAX,
279 .silence_threshold = 0,
280 .silence_size = 0,
281 .avail_min = MMAP_PERIOD_SIZE, //1 ms
282};
283
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -0700284#define AFE_PROXY_CHANNEL_COUNT 2
285#define AFE_PROXY_SAMPLING_RATE 48000
286
287#define AFE_PROXY_PLAYBACK_PERIOD_SIZE 768
288#define AFE_PROXY_PLAYBACK_PERIOD_COUNT 4
289
290struct pcm_config pcm_config_afe_proxy_playback = {
291 .channels = AFE_PROXY_CHANNEL_COUNT,
292 .rate = AFE_PROXY_SAMPLING_RATE,
293 .period_size = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
294 .period_count = AFE_PROXY_PLAYBACK_PERIOD_COUNT,
295 .format = PCM_FORMAT_S16_LE,
296 .start_threshold = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
297 .stop_threshold = INT_MAX,
298 .avail_min = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
299};
300
301#define AFE_PROXY_RECORD_PERIOD_SIZE 768
302#define AFE_PROXY_RECORD_PERIOD_COUNT 4
303
Aalique Grahame22e49102018-12-18 14:23:57 -0800304struct pcm_config pcm_config_audio_capture_rt = {
305 .channels = 2,
306 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
307 .period_size = ULL_PERIOD_SIZE,
308 .period_count = 512,
309 .format = PCM_FORMAT_S16_LE,
310 .start_threshold = 0,
311 .stop_threshold = AFE_PROXY_RECORD_PERIOD_SIZE * AFE_PROXY_RECORD_PERIOD_COUNT,
312 .silence_threshold = 0,
313 .silence_size = 0,
314 .avail_min = ULL_PERIOD_SIZE, //1 ms
315};
316
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -0700317struct pcm_config pcm_config_afe_proxy_record = {
318 .channels = AFE_PROXY_CHANNEL_COUNT,
319 .rate = AFE_PROXY_SAMPLING_RATE,
320 .period_size = AFE_PROXY_RECORD_PERIOD_SIZE,
321 .period_count = AFE_PROXY_RECORD_PERIOD_COUNT,
322 .format = PCM_FORMAT_S16_LE,
323 .start_threshold = AFE_PROXY_RECORD_PERIOD_SIZE,
324 .stop_threshold = INT_MAX,
325 .avail_min = AFE_PROXY_RECORD_PERIOD_SIZE,
326};
327
Ashish Jainf1eaa582016-05-23 20:54:24 +0530328#define AUDIO_MAX_PCM_FORMATS 7
329
330const uint32_t format_to_bitwidth_table[AUDIO_MAX_PCM_FORMATS] = {
331 [AUDIO_FORMAT_DEFAULT] = 0,
332 [AUDIO_FORMAT_PCM_16_BIT] = sizeof(uint16_t),
333 [AUDIO_FORMAT_PCM_8_BIT] = sizeof(uint8_t),
334 [AUDIO_FORMAT_PCM_32_BIT] = sizeof(uint32_t),
335 [AUDIO_FORMAT_PCM_8_24_BIT] = sizeof(uint32_t),
336 [AUDIO_FORMAT_PCM_FLOAT] = sizeof(float),
337 [AUDIO_FORMAT_PCM_24_BIT_PACKED] = sizeof(uint8_t) * 3,
338};
339
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800340const char * const use_case_table[AUDIO_USECASE_MAX] = {
Eric Laurentb23d5282013-05-14 15:27:20 -0700341 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
342 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
Vignesh Kulothungana6927272019-02-20 15:17:07 -0800343 [USECASE_AUDIO_PLAYBACK_WITH_HAPTICS] = "audio-with-haptics-playback",
Meng Wang51d8c2a2020-04-27 15:23:21 +0800344 [USECASE_AUDIO_PLAYBACK_HAPTICS] = "haptics-playback",
Ravi Kumar Alamanda474de5a2015-06-25 20:08:01 -0700345 [USECASE_AUDIO_PLAYBACK_ULL] = "audio-ull-playback",
346 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback",
Shruthi Krishnaace10852013-10-25 14:32:12 -0700347 [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback",
vivek mehta446c3962015-09-14 10:57:35 -0700348 //Enabled for Direct_PCM
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700349 [USECASE_AUDIO_PLAYBACK_OFFLOAD2] = "compress-offload-playback2",
350 [USECASE_AUDIO_PLAYBACK_OFFLOAD3] = "compress-offload-playback3",
351 [USECASE_AUDIO_PLAYBACK_OFFLOAD4] = "compress-offload-playback4",
352 [USECASE_AUDIO_PLAYBACK_OFFLOAD5] = "compress-offload-playback5",
353 [USECASE_AUDIO_PLAYBACK_OFFLOAD6] = "compress-offload-playback6",
354 [USECASE_AUDIO_PLAYBACK_OFFLOAD7] = "compress-offload-playback7",
355 [USECASE_AUDIO_PLAYBACK_OFFLOAD8] = "compress-offload-playback8",
356 [USECASE_AUDIO_PLAYBACK_OFFLOAD9] = "compress-offload-playback9",
Haynes Mathew George16081042017-05-31 17:16:49 -0700357 [USECASE_AUDIO_PLAYBACK_FM] = "play-fm",
358 [USECASE_AUDIO_PLAYBACK_MMAP] = "mmap-playback",
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700359 [USECASE_AUDIO_PLAYBACK_HIFI] = "hifi-playback",
Aalique Grahame22e49102018-12-18 14:23:57 -0800360 [USECASE_AUDIO_PLAYBACK_TTS] = "audio-tts-playback",
vivek mehta0ea887a2015-08-26 14:01:20 -0700361
Eric Laurentb23d5282013-05-14 15:27:20 -0700362 [USECASE_AUDIO_RECORD] = "audio-record",
Mingming Yine62d7842013-10-25 16:26:03 -0700363 [USECASE_AUDIO_RECORD_COMPRESS] = "audio-record-compress",
Dhananjay Kumaree4d2002016-10-25 18:02:58 +0530364 [USECASE_AUDIO_RECORD_COMPRESS2] = "audio-record-compress2",
365 [USECASE_AUDIO_RECORD_COMPRESS3] = "audio-record-compress3",
366 [USECASE_AUDIO_RECORD_COMPRESS4] = "audio-record-compress4",
Dhananjay Kumar376e38b2017-09-28 22:26:23 +0530367 [USECASE_AUDIO_RECORD_COMPRESS5] = "audio-record-compress5",
368 [USECASE_AUDIO_RECORD_COMPRESS6] = "audio-record-compress6",
Eric Laurentb23d5282013-05-14 15:27:20 -0700369 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
Preetam Singh Ranawatde84f1a2013-11-01 14:58:16 -0700370 [USECASE_AUDIO_RECORD_FM_VIRTUAL] = "fm-virtual-record",
Haynes Mathew George16081042017-05-31 17:16:49 -0700371 [USECASE_AUDIO_RECORD_MMAP] = "mmap-record",
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700372 [USECASE_AUDIO_RECORD_HIFI] = "hifi-record",
Haynes Mathew George16081042017-05-31 17:16:49 -0700373
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -0800374 [USECASE_AUDIO_HFP_SCO] = "hfp-sco",
Vimal Puthanveed47e64852013-12-20 13:23:39 -0800375 [USECASE_AUDIO_HFP_SCO_WB] = "hfp-sco-wb",
Derek Chenf7092792017-05-23 12:23:53 -0400376 [USECASE_AUDIO_HFP_SCO_DOWNLINK] = "hfp-sco-downlink",
377 [USECASE_AUDIO_HFP_SCO_WB_DOWNLINK] = "hfp-sco-wb-downlink",
Mingming Yin3ee55c62014-08-04 14:23:35 -0700378
Derek Chenf7092792017-05-23 12:23:53 -0400379 [USECASE_VOICE_CALL] = "voice-call",
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700380 [USECASE_VOICE2_CALL] = "voice2-call",
381 [USECASE_VOLTE_CALL] = "volte-call",
382 [USECASE_QCHAT_CALL] = "qchat-call",
Vicky Sehrawat7e4fc152014-02-12 17:58:59 -0800383 [USECASE_VOWLAN_CALL] = "vowlan-call",
Vidyakumar Athota0e109352015-02-12 17:38:22 -0800384 [USECASE_VOICEMMODE1_CALL] = "voicemmode1-call",
385 [USECASE_VOICEMMODE2_CALL] = "voicemmode2-call",
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800386 [USECASE_COMPRESS_VOIP_CALL] = "compress-voip-call",
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700387 [USECASE_INCALL_REC_UPLINK] = "incall-rec-uplink",
388 [USECASE_INCALL_REC_DOWNLINK] = "incall-rec-downlink",
389 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK] = "incall-rec-uplink-and-downlink",
Helen Zenge56b4852013-12-03 16:54:40 -0800390 [USECASE_INCALL_REC_UPLINK_COMPRESS] = "incall-rec-uplink-compress",
391 [USECASE_INCALL_REC_DOWNLINK_COMPRESS] = "incall-rec-downlink-compress",
392 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK_COMPRESS] = "incall-rec-uplink-and-downlink-compress",
393
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -0700394 [USECASE_INCALL_MUSIC_UPLINK] = "incall_music_uplink",
395 [USECASE_INCALL_MUSIC_UPLINK2] = "incall_music_uplink2",
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700396 [USECASE_AUDIO_SPKR_CALIB_RX] = "spkr-rx-calib",
397 [USECASE_AUDIO_SPKR_CALIB_TX] = "spkr-vi-record",
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -0700398
399 [USECASE_AUDIO_PLAYBACK_AFE_PROXY] = "afe-proxy-playback",
400 [USECASE_AUDIO_RECORD_AFE_PROXY] = "afe-proxy-record",
Zhou Song62ea0282020-03-22 19:53:01 +0800401 [USECASE_AUDIO_RECORD_AFE_PROXY2] = "afe-proxy-record2",
Md Mansoor Ahmeddb1b4f92018-01-25 18:56:31 +0530402 [USECASE_AUDIO_PLAYBACK_SILENCE] = "silence-playback",
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700403
Siddartha Shaik31b530e2017-05-19 15:26:33 +0530404 /* Transcode loopback cases */
Surendar Karka93cd25a2018-08-28 14:21:37 +0530405 [USECASE_AUDIO_TRANSCODE_LOOPBACK_RX] = "audio-transcode-loopback-rx",
406 [USECASE_AUDIO_TRANSCODE_LOOPBACK_TX] = "audio-transcode-loopback-tx",
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700407
408 [USECASE_AUDIO_PLAYBACK_VOIP] = "audio-playback-voip",
409 [USECASE_AUDIO_RECORD_VOIP] = "audio-record-voip",
Revathi Uddarajud9f23d92020-07-27 10:55:06 +0530410 [USECASE_AUDIO_RECORD_VOIP_LOW_LATENCY] = "audio-record-voip-low-latency",
Varun Balaraje49253e2017-07-06 19:48:56 +0530411 /* For Interactive Audio Streams */
412 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM1] = "audio-interactive-stream1",
413 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM2] = "audio-interactive-stream2",
414 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM3] = "audio-interactive-stream3",
415 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM4] = "audio-interactive-stream4",
416 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM5] = "audio-interactive-stream5",
417 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM6] = "audio-interactive-stream6",
418 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM7] = "audio-interactive-stream7",
419 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM8] = "audio-interactive-stream8",
Garmond Leunge2433c32017-09-28 21:51:22 -0700420
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800421 [USECASE_AUDIO_EC_REF_LOOPBACK] = "ec-ref-audio-capture",
422
Derek Chenf6318be2017-06-12 17:16:24 -0400423 [USECASE_AUDIO_A2DP_ABR_FEEDBACK] = "a2dp-abr-feedback",
424
425 [USECASE_AUDIO_PLAYBACK_MEDIA] = "media-playback",
426 [USECASE_AUDIO_PLAYBACK_SYS_NOTIFICATION] = "sys-notification-playback",
427 [USECASE_AUDIO_PLAYBACK_NAV_GUIDANCE] = "nav-guidance-playback",
428 [USECASE_AUDIO_PLAYBACK_PHONE] = "phone-playback",
Derek Chen1bcdc6b2020-02-06 22:44:53 -0800429 [USECASE_AUDIO_PLAYBACK_FRONT_PASSENGER] = "front-passenger-playback",
Derek Chendf05eea2019-08-01 13:57:49 -0700430 [USECASE_AUDIO_PLAYBACK_REAR_SEAT] = "rear-seat-playback",
Rahul Sharma99770982019-03-06 17:05:26 +0530431 [USECASE_AUDIO_FM_TUNER_EXT] = "fm-tuner-ext",
Derek Chena30a5f42019-12-03 11:17:09 -0500432 [USECASE_ICC_CALL] = "icc-call",
Huicheng Liu1404ba12020-09-11 01:03:25 -0400433
434 [USECASE_AUDIO_RECORD_BUS] = "audio-record",
435 [USECASE_AUDIO_RECORD_BUS_FRONT_PASSENGER] = "front-passenger-record",
436 [USECASE_AUDIO_RECORD_BUS_REAR_SEAT] = "rear-seat-record",
Fei Tongaffdf732020-02-20 20:39:05 +0800437 [USECASE_AUDIO_PLAYBACK_SYNTHESIZER] = "synth-loopback",
Susan Wange3959562021-03-11 11:50:26 -0500438 [USECASE_AUDIO_RECORD_ECHO_REF_EXT] = "echo-reference-external",
Eric Laurentb23d5282013-05-14 15:27:20 -0700439};
440
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700441static const audio_usecase_t offload_usecases[] = {
442 USECASE_AUDIO_PLAYBACK_OFFLOAD,
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700443 USECASE_AUDIO_PLAYBACK_OFFLOAD2,
444 USECASE_AUDIO_PLAYBACK_OFFLOAD3,
445 USECASE_AUDIO_PLAYBACK_OFFLOAD4,
446 USECASE_AUDIO_PLAYBACK_OFFLOAD5,
447 USECASE_AUDIO_PLAYBACK_OFFLOAD6,
448 USECASE_AUDIO_PLAYBACK_OFFLOAD7,
449 USECASE_AUDIO_PLAYBACK_OFFLOAD8,
450 USECASE_AUDIO_PLAYBACK_OFFLOAD9,
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700451};
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800452
Varun Balaraje49253e2017-07-06 19:48:56 +0530453static const audio_usecase_t interactive_usecases[] = {
454 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM1,
455 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM2,
456 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM3,
457 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM4,
458 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM5,
459 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM6,
460 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM7,
461 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM8,
462};
463
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800464#define STRING_TO_ENUM(string) { #string, string }
465
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800466struct string_to_enum {
467 const char *name;
468 uint32_t value;
469};
470
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700471static const struct string_to_enum channels_name_to_enum_table[] = {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800472 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
Mingming Yin3a941d42016-02-17 18:08:05 -0800473 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_2POINT1),
474 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_QUAD),
475 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_SURROUND),
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -0700476 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_PENTA),
Mingming Yin3a941d42016-02-17 18:08:05 -0800477 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
478 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_6POINT1),
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800479 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700480 STRING_TO_ENUM(AUDIO_CHANNEL_IN_MONO),
481 STRING_TO_ENUM(AUDIO_CHANNEL_IN_STEREO),
482 STRING_TO_ENUM(AUDIO_CHANNEL_IN_FRONT_BACK),
483 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_1),
484 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_2),
485 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_3),
486 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_4),
487 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_5),
488 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_6),
489 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_7),
490 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_8),
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800491};
492
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700493static const struct string_to_enum formats_name_to_enum_table[] = {
494 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
495 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
496 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Pradnya Chaphekar8a9dcd82014-09-09 09:49:10 -0700497 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
498 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
499 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Ben Romberger1aaaf862017-04-06 17:49:46 -0700500 STRING_TO_ENUM(AUDIO_FORMAT_DOLBY_TRUEHD),
Mingming Yin3a941d42016-02-17 18:08:05 -0800501 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
502 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
Naresh Tanniru928f0862017-04-07 16:44:23 -0700503 STRING_TO_ENUM(AUDIO_FORMAT_IEC61937)
Mingming Yin3a941d42016-02-17 18:08:05 -0800504};
505
506//list of all supported sample rates by HDMI specification.
507static const int out_hdmi_sample_rates[] = {
508 32000, 44100, 48000, 88200, 96000, 176400, 192000,
509};
510
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700511static const struct string_to_enum out_sample_rates_name_to_enum_table[] = {
Mingming Yin3a941d42016-02-17 18:08:05 -0800512 STRING_TO_ENUM(32000),
513 STRING_TO_ENUM(44100),
514 STRING_TO_ENUM(48000),
515 STRING_TO_ENUM(88200),
516 STRING_TO_ENUM(96000),
517 STRING_TO_ENUM(176400),
518 STRING_TO_ENUM(192000),
Mingshu pange8aedf12019-10-25 15:38:27 +0800519 STRING_TO_ENUM(352800),
520 STRING_TO_ENUM(384000),
Pradnya Chaphekar8a9dcd82014-09-09 09:49:10 -0700521};
522
Carter Hsu2e429db2019-05-14 18:50:52 +0800523struct in_effect_list {
524 struct listnode list;
525 effect_handle_t handle;
526};
527
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700528static struct audio_device *adev = NULL;
Ben Romberger02ab1192018-05-24 12:10:08 -0700529static pthread_mutex_t adev_init_lock = PTHREAD_MUTEX_INITIALIZER;
Kiran Kandi910e1862013-10-29 13:29:42 -0700530static unsigned int audio_device_ref_count;
vivek mehtab72d08d2016-04-29 03:16:47 -0700531//cache last MBDRC cal step level
532static int last_known_cal_step = -1 ;
Kiran Kandi910e1862013-10-29 13:29:42 -0700533
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +0530534static int out_set_compr_volume(struct audio_stream_out *stream, float left, float right);
Arun Mirpuri5d170872019-03-26 13:21:31 -0700535static int out_set_mmap_volume(struct audio_stream_out *stream, float left, float right);
Zhou Song2b8f28f2017-09-11 10:51:38 +0800536static int out_set_voip_volume(struct audio_stream_out *stream, float left, float right);
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +0530537static int out_set_pcm_volume(struct audio_stream_out *stream, float left, float right);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +0530538
Derek Chen6f293672019-04-01 01:40:24 -0700539static void adev_snd_mon_cb(void *cookie, struct str_parms *parms);
540static void in_snd_mon_cb(void * stream, struct str_parms * parms);
541static void out_snd_mon_cb(void * stream, struct str_parms * parms);
542
Zhou Song331c8e52019-08-26 14:16:12 +0800543static int configure_btsco_sample_rate(snd_device_t snd_device);
544
Vatsal Buchac09ae062018-11-14 13:25:08 +0530545#ifdef AUDIO_FEATURE_ENABLED_GCOV
546extern void __gcov_flush();
547static void enable_gcov()
548{
549 __gcov_flush();
550}
551#else
552static void enable_gcov()
553{
554}
555#endif
556
justinweng20fb6d82019-02-21 18:49:00 -0700557static int in_set_microphone_direction(const struct audio_stream_in *stream,
558 audio_microphone_direction_t dir);
559static int in_set_microphone_field_dimension(const struct audio_stream_in *stream, float zoom);
560
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700561static bool may_use_noirq_mode(struct audio_device *adev, audio_usecase_t uc_id,
562 int flags __unused)
563{
564 int dir = 0;
565 switch (uc_id) {
566 case USECASE_AUDIO_RECORD_LOW_LATENCY:
Revathi Uddarajud9f23d92020-07-27 10:55:06 +0530567 case USECASE_AUDIO_RECORD_VOIP_LOW_LATENCY:
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700568 dir = 1;
569 case USECASE_AUDIO_PLAYBACK_ULL:
570 break;
571 default:
572 return false;
573 }
574
575 int dev_id = platform_get_pcm_device_id(uc_id, dir == 0 ?
576 PCM_PLAYBACK : PCM_CAPTURE);
577 if (adev->adm_is_noirq_avail)
578 return adev->adm_is_noirq_avail(adev->adm_data,
579 adev->snd_card, dev_id, dir);
580 return false;
581}
582
583static void register_out_stream(struct stream_out *out)
584{
585 struct audio_device *adev = out->dev;
586 if (is_offload_usecase(out->usecase) ||
587 !adev->adm_register_output_stream)
588 return;
589
590 // register stream first for backward compatibility
591 adev->adm_register_output_stream(adev->adm_data,
592 out->handle,
593 out->flags);
594
595 if (!adev->adm_set_config)
596 return;
597
598 if (out->realtime)
599 adev->adm_set_config(adev->adm_data,
600 out->handle,
601 out->pcm, &out->config);
602}
603
604static void register_in_stream(struct stream_in *in)
605{
606 struct audio_device *adev = in->dev;
607 if (!adev->adm_register_input_stream)
608 return;
609
610 adev->adm_register_input_stream(adev->adm_data,
611 in->capture_handle,
612 in->flags);
613
614 if (!adev->adm_set_config)
615 return;
616
617 if (in->realtime)
618 adev->adm_set_config(adev->adm_data,
619 in->capture_handle,
620 in->pcm,
621 &in->config);
622}
623
624static void request_out_focus(struct stream_out *out, long ns)
625{
626 struct audio_device *adev = out->dev;
627
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700628 if (adev->adm_request_focus_v2)
629 adev->adm_request_focus_v2(adev->adm_data, out->handle, ns);
630 else if (adev->adm_request_focus)
631 adev->adm_request_focus(adev->adm_data, out->handle);
632}
633
Aniket Kumar Lata60586a92019-05-22 22:18:55 -0700634static int request_in_focus(struct stream_in *in, long ns)
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700635{
636 struct audio_device *adev = in->dev;
Aniket Kumar Lata60586a92019-05-22 22:18:55 -0700637 int ret = 0;
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700638
Aniket Kumar Lata60586a92019-05-22 22:18:55 -0700639 if (adev->adm_request_focus_v2_1)
640 ret = adev->adm_request_focus_v2_1(adev->adm_data, in->capture_handle, ns);
641 else if (adev->adm_request_focus_v2)
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700642 adev->adm_request_focus_v2(adev->adm_data, in->capture_handle, ns);
643 else if (adev->adm_request_focus)
644 adev->adm_request_focus(adev->adm_data, in->capture_handle);
Aniket Kumar Lata60586a92019-05-22 22:18:55 -0700645
646 return ret;
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700647}
648
649static void release_out_focus(struct stream_out *out)
650{
651 struct audio_device *adev = out->dev;
652
653 if (adev->adm_abandon_focus)
654 adev->adm_abandon_focus(adev->adm_data, out->handle);
655}
656
657static void release_in_focus(struct stream_in *in)
658{
659 struct audio_device *adev = in->dev;
660 if (adev->adm_abandon_focus)
661 adev->adm_abandon_focus(adev->adm_data, in->capture_handle);
662}
663
Dhananjay Kumare6293dd2017-05-25 17:25:30 +0530664static int parse_snd_card_status(struct str_parms *parms, int *card,
665 card_status_t *status)
666{
667 char value[32]={0};
668 char state[32]={0};
669
670 int ret = str_parms_get_str(parms, "SND_CARD_STATUS", value, sizeof(value));
671 if (ret < 0)
672 return -1;
673
674 // sscanf should be okay as value is of max length 32.
675 // same as sizeof state.
676 if (sscanf(value, "%d,%s", card, state) < 2)
677 return -1;
678
679 *status = !strcmp(state, "ONLINE") ? CARD_STATUS_ONLINE :
680 CARD_STATUS_OFFLINE;
681 return 0;
682}
683
Haynes Mathew Georgeb0f5dc32017-10-06 18:35:12 -0700684static inline void adjust_frames_for_device_delay(struct stream_out *out,
685 uint32_t *dsp_frames) {
686 // Adjustment accounts for A2dp encoder latency with offload usecases
687 // Note: Encoder latency is returned in ms.
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800688 if (is_a2dp_out_device_type(&out->device_list)) {
Haynes Mathew Georgeb0f5dc32017-10-06 18:35:12 -0700689 unsigned long offset =
690 (audio_extn_a2dp_get_encoder_latency() * out->sample_rate / 1000);
691 *dsp_frames = (*dsp_frames > offset) ? (*dsp_frames - offset) : 0;
692 }
693}
694
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700695static inline bool free_entry(void *key __unused,
696 void *value, void *context __unused)
697{
698 free(value);
699 return true;
700}
701
702static inline void free_map(Hashmap *map)
703{
704 if (map) {
705 hashmapForEach(map, free_entry, (void *) NULL);
706 hashmapFree(map);
707 }
708}
709
Aniket Kumar Latabc774842020-01-16 21:22:05 -0800710static inline void patch_map_remove_l(struct audio_device *adev,
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700711 audio_patch_handle_t patch_handle)
712{
713 if (patch_handle == AUDIO_PATCH_HANDLE_NONE)
714 return;
715
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700716 struct audio_patch_info *p_info =
717 hashmapGet(adev->patch_map, (void *) (intptr_t) patch_handle);
718 if (p_info) {
719 ALOGV("%s: Remove patch %d", __func__, patch_handle);
720 hashmapRemove(adev->patch_map, (void *) (intptr_t) patch_handle);
721 free(p_info->patch);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700722 free(p_info);
723 }
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700724}
725
726static inline int io_streams_map_insert(struct audio_device *adev,
727 struct audio_stream *stream,
728 audio_io_handle_t handle,
729 audio_patch_handle_t patch_handle)
730{
731 struct audio_stream_info *s_info =
732 (struct audio_stream_info *) calloc(1, sizeof(struct audio_stream_info));
733
734 if (s_info == NULL) {
735 ALOGE("%s: Could not allocate stream info", __func__);
736 return -ENOMEM;
737 }
738 s_info->stream = stream;
739 s_info->patch_handle = patch_handle;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700740
741 pthread_mutex_lock(&adev->lock);
742 struct audio_stream_info *stream_info =
743 hashmapPut(adev->io_streams_map, (void *) (intptr_t) handle, (void *) s_info);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700744 if (stream_info != NULL)
745 free(stream_info);
Aniket Kumar Latabc774842020-01-16 21:22:05 -0800746 pthread_mutex_unlock(&adev->lock);
Aniket Kumar Latad13758f2020-08-06 15:11:36 -0700747 ALOGV("%s: Added stream in io_streams_map with handle %d", __func__, handle);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700748 return 0;
749}
750
751static inline void io_streams_map_remove(struct audio_device *adev,
752 audio_io_handle_t handle)
753{
754 pthread_mutex_lock(&adev->lock);
755 struct audio_stream_info *s_info =
756 hashmapRemove(adev->io_streams_map, (void *) (intptr_t) handle);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700757 if (s_info == NULL)
Aniket Kumar Latabc774842020-01-16 21:22:05 -0800758 goto done;
Aniket Kumar Latad13758f2020-08-06 15:11:36 -0700759 ALOGV("%s: Removed stream with handle %d", __func__, handle);
Aniket Kumar Latabc774842020-01-16 21:22:05 -0800760 patch_map_remove_l(adev, s_info->patch_handle);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700761 free(s_info);
Aniket Kumar Latabc774842020-01-16 21:22:05 -0800762done:
763 pthread_mutex_unlock(&adev->lock);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700764 return;
765}
766
Aniket Kumar Latabc774842020-01-16 21:22:05 -0800767static struct audio_patch_info* fetch_patch_info_l(struct audio_device *adev,
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700768 audio_patch_handle_t handle)
769{
770 struct audio_patch_info *p_info = NULL;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700771 p_info = (struct audio_patch_info *)
772 hashmapGet(adev->patch_map, (void *) (intptr_t) handle);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700773 return p_info;
774}
775
vivek mehtaa76401a2015-04-24 14:12:15 -0700776__attribute__ ((visibility ("default")))
777bool audio_hw_send_gain_dep_calibration(int level) {
778 bool ret_val = false;
vivek mehtab72d08d2016-04-29 03:16:47 -0700779 ALOGV("%s: called ...", __func__);
vivek mehtaa76401a2015-04-24 14:12:15 -0700780
781 pthread_mutex_lock(&adev_init_lock);
782
783 if (adev != NULL && adev->platform != NULL) {
784 pthread_mutex_lock(&adev->lock);
785 ret_val = platform_send_gain_dep_cal(adev->platform, level);
vivek mehtab72d08d2016-04-29 03:16:47 -0700786
Preetam Singh Ranawatf4ae0222017-05-31 17:07:28 +0530787 // cache level info for any of the use case which
788 // was not started.
789 last_known_cal_step = level;;
vivek mehtab72d08d2016-04-29 03:16:47 -0700790
vivek mehtaa76401a2015-04-24 14:12:15 -0700791 pthread_mutex_unlock(&adev->lock);
792 } else {
793 ALOGE("%s: %s is NULL", __func__, adev == NULL ? "adev" : "adev->platform");
794 }
795
796 pthread_mutex_unlock(&adev_init_lock);
797
798 return ret_val;
799}
800
Ashish Jain5106d362016-05-11 19:23:33 +0530801static int check_and_set_gapless_mode(struct audio_device *adev, bool enable_gapless)
802{
Krishnankutty Kolathappilly6d8788b2014-01-09 12:45:31 -0800803 bool gapless_enabled = false;
804 const char *mixer_ctl_name = "Compress Gapless Playback";
805 struct mixer_ctl *ctl;
806
807 ALOGV("%s:", __func__);
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700808 gapless_enabled = property_get_bool("vendor.audio.offload.gapless.enabled", false);
Ashish Jain5106d362016-05-11 19:23:33 +0530809
810 /*Disable gapless if its AV playback*/
811 gapless_enabled = gapless_enabled && enable_gapless;
Krishnankutty Kolathappilly6d8788b2014-01-09 12:45:31 -0800812
813 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
814 if (!ctl) {
815 ALOGE("%s: Could not get ctl for mixer cmd - %s",
816 __func__, mixer_ctl_name);
817 return -EINVAL;
818 }
819
820 if (mixer_ctl_set_value(ctl, 0, gapless_enabled) < 0) {
821 ALOGE("%s: Could not set gapless mode %d",
822 __func__, gapless_enabled);
823 return -EINVAL;
824 }
825 return 0;
826}
Haynes Mathew George5191a852013-09-11 14:19:36 -0700827
Aniket Kumar Lataf56b6402016-10-27 12:03:18 -0700828__attribute__ ((visibility ("default")))
829int audio_hw_get_gain_level_mapping(struct amp_db_and_gain_table *mapping_tbl,
830 int table_size) {
831 int ret_val = 0;
832 ALOGV("%s: enter ... ", __func__);
833
834 pthread_mutex_lock(&adev_init_lock);
835 if (adev == NULL) {
836 ALOGW("%s: adev is NULL .... ", __func__);
837 goto done;
838 }
839
840 pthread_mutex_lock(&adev->lock);
841 ret_val = platform_get_gain_level_mapping(mapping_tbl, table_size);
842 pthread_mutex_unlock(&adev->lock);
843done:
844 pthread_mutex_unlock(&adev_init_lock);
845 ALOGV("%s: exit ... ", __func__);
846 return ret_val;
847}
848
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800849bool audio_hw_send_qdsp_parameter(int stream_type, float vol, bool active)
Aalique Grahame22e49102018-12-18 14:23:57 -0800850{
851 bool ret = false;
852 ALOGV("%s: enter ...", __func__);
853
854 pthread_mutex_lock(&adev_init_lock);
855
856 if (adev != NULL && adev->platform != NULL) {
857 pthread_mutex_lock(&adev->lock);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800858 ret = audio_extn_qdsp_set_state(adev, stream_type, vol, active);
Aalique Grahame22e49102018-12-18 14:23:57 -0800859 pthread_mutex_unlock(&adev->lock);
860 }
861
862 pthread_mutex_unlock(&adev_init_lock);
863
864 ALOGV("%s: exit with ret %d", __func__, ret);
865 return ret;
866}
Aalique Grahame22e49102018-12-18 14:23:57 -0800867
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700868static bool is_supported_format(audio_format_t format)
869{
Eric Laurent86e17132013-09-12 17:49:30 -0700870 if (format == AUDIO_FORMAT_MP3 ||
Satish Babu Patakokila0c313922016-12-08 12:07:08 +0530871 format == AUDIO_FORMAT_MP2 ||
Ashish Jainf9b78162014-08-25 20:36:25 +0530872 format == AUDIO_FORMAT_AAC_LC ||
873 format == AUDIO_FORMAT_AAC_HE_V1 ||
874 format == AUDIO_FORMAT_AAC_HE_V2 ||
Manish Dewangana6fc5442015-08-24 20:30:31 +0530875 format == AUDIO_FORMAT_AAC_ADTS_LC ||
876 format == AUDIO_FORMAT_AAC_ADTS_HE_V1 ||
877 format == AUDIO_FORMAT_AAC_ADTS_HE_V2 ||
Arun Kumar Dasari3b174182016-12-27 13:01:14 +0530878 format == AUDIO_FORMAT_AAC_LATM_LC ||
879 format == AUDIO_FORMAT_AAC_LATM_HE_V1 ||
880 format == AUDIO_FORMAT_AAC_LATM_HE_V2 ||
Ashish Jain5106d362016-05-11 19:23:33 +0530881 format == AUDIO_FORMAT_PCM_24_BIT_PACKED ||
882 format == AUDIO_FORMAT_PCM_8_24_BIT ||
Ashish Jainf1eaa582016-05-23 20:54:24 +0530883 format == AUDIO_FORMAT_PCM_FLOAT ||
884 format == AUDIO_FORMAT_PCM_32_BIT ||
vivek mehta0ea887a2015-08-26 14:01:20 -0700885 format == AUDIO_FORMAT_PCM_16_BIT ||
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530886 format == AUDIO_FORMAT_AC3 ||
887 format == AUDIO_FORMAT_E_AC3 ||
Ben Romberger1aaaf862017-04-06 17:49:46 -0700888 format == AUDIO_FORMAT_DOLBY_TRUEHD ||
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530889 format == AUDIO_FORMAT_DTS ||
890 format == AUDIO_FORMAT_DTS_HD ||
Weiyin Jiang18ac4e92015-03-15 15:03:40 +0800891 format == AUDIO_FORMAT_FLAC ||
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530892 format == AUDIO_FORMAT_ALAC ||
893 format == AUDIO_FORMAT_APE ||
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530894 format == AUDIO_FORMAT_DSD ||
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530895 format == AUDIO_FORMAT_VORBIS ||
Weiyin Jiang18ac4e92015-03-15 15:03:40 +0800896 format == AUDIO_FORMAT_WMA ||
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +0530897 format == AUDIO_FORMAT_WMA_PRO ||
Naresh Tanniru928f0862017-04-07 16:44:23 -0700898 format == AUDIO_FORMAT_APTX ||
899 format == AUDIO_FORMAT_IEC61937)
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -0800900 return true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700901
902 return false;
903}
904
Kunlei Zhang67cc7072020-12-18 17:16:49 +0800905static bool is_supported_conc_usecase_for_power_mode_call(struct audio_device *adev)
906{
907 struct listnode *node;
908 struct audio_usecase *usecase;
909
910 list_for_each(node, &adev->usecase_list) {
911 usecase = node_to_item(node, struct audio_usecase, list);
912 if (usecase->id == USECASE_AUDIO_PLAYBACK_FM) {
913 ALOGD("%s: FM usecase is active, not setting power mode", __func__);
914 return false;
915 }
916 }
917
918 return true;
919}
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700920static inline bool is_mmap_usecase(audio_usecase_t uc_id)
921{
922 return (uc_id == USECASE_AUDIO_RECORD_AFE_PROXY) ||
Zhou Song62ea0282020-03-22 19:53:01 +0800923 (uc_id == USECASE_AUDIO_RECORD_AFE_PROXY2) ||
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700924 (uc_id == USECASE_AUDIO_PLAYBACK_AFE_PROXY);
925}
926
Aniket Kumar Lata8426d1f2019-06-10 15:25:53 -0700927static inline bool is_valid_volume(float left, float right)
928{
929 return ((left >= 0.0f && right >= 0.0f) ? true : false);
930}
931
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +0530932static void enable_asrc_mode(struct audio_device *adev)
Preetam Singh Ranawatb0c0dd72016-08-18 00:32:06 +0530933{
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +0530934 ALOGV("%s", __func__);
935 audio_route_apply_and_update_path(adev->audio_route,
936 "asrc-mode");
937 adev->asrc_mode_enabled = true;
938}
939
940static void disable_asrc_mode(struct audio_device *adev)
941{
942 ALOGV("%s", __func__);
943 audio_route_reset_and_update_path(adev->audio_route,
944 "asrc-mode");
945 adev->asrc_mode_enabled = false;
946}
947
Saurav Kumarc1411662020-10-14 10:50:45 +0530948static void check_and_configure_headphone(struct audio_device *adev,
949 struct audio_usecase *uc_info,
950 snd_device_t snd_device)
951{
952 struct listnode *node;
953 struct audio_usecase *usecase;
954 int new_backend_idx, usecase_backend_idx;
955 bool spkr_hph_single_be_native_concurrency;
956
957 new_backend_idx = platform_get_backend_index(snd_device);
958 spkr_hph_single_be_native_concurrency = platform_get_spkr_hph_single_be_native_concurrency_flag();
Zhou Songd4b18c42021-01-14 15:15:29 +0800959 if ((spkr_hph_single_be_native_concurrency && (new_backend_idx == DEFAULT_CODEC_BACKEND)) ||
960 uc_info->id == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) {
Saurav Kumarc1411662020-10-14 10:50:45 +0530961 list_for_each(node, &adev->usecase_list) {
962 usecase = node_to_item(node, struct audio_usecase, list);
963 if ((usecase->type != PCM_CAPTURE) && (usecase != uc_info)) {
964 usecase_backend_idx = platform_get_backend_index(usecase->out_snd_device);
965 if (((usecase_backend_idx == HEADPHONE_BACKEND) ||
966 (usecase_backend_idx == HEADPHONE_44_1_BACKEND)) &&
967 ((usecase->stream.out->sample_rate % OUTPUT_SAMPLING_RATE_44100) == 0)) {
968 disable_audio_route(adev, usecase);
969 disable_snd_device(adev, usecase->out_snd_device);
970 usecase->stream.out->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Saurav Kumar70902382020-10-28 11:16:04 +0530971 platform_check_and_set_codec_backend_cfg(adev, usecase,
972 usecase->out_snd_device);
Saurav Kumarc1411662020-10-14 10:50:45 +0530973 enable_snd_device(adev, usecase->out_snd_device);
Zhou Songd4b18c42021-01-14 15:15:29 +0800974 enable_audio_route(adev, usecase);
Saurav Kumarc1411662020-10-14 10:50:45 +0530975 }
976 }
Revathi Uddaraju2c0eac02021-07-27 02:06:42 -0700977 else if ((usecase->type != PCM_CAPTURE) && (usecase == uc_info)) {
978 usecase_backend_idx = platform_get_backend_index(usecase->out_snd_device);
979 if (((usecase_backend_idx == HEADPHONE_BACKEND) ||
980 (usecase_backend_idx == HEADPHONE_44_1_BACKEND)) &&
981 ((usecase->stream.out->sample_rate % OUTPUT_SAMPLING_RATE_44100) == 0)) {
982 usecase->stream.out->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
983 platform_check_and_set_codec_backend_cfg(adev, usecase,
984 usecase->out_snd_device);
985 }
986 }
Saurav Kumarc1411662020-10-14 10:50:45 +0530987 }
988 }
989}
990
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +0530991/*
992 * - Enable ASRC mode for incoming mix path use case(Headphone backend)if Headphone
993 * 44.1 or Native DSD backends are enabled for any of current use case.
994 * e.g. 48-> + (Naitve DSD or Headphone 44.1)
995 * - Disable current mix path use case(Headphone backend) and re-enable it with
996 * ASRC mode for incoming Headphone 44.1 or Native DSD use case.
997 * e.g. Naitve DSD or Headphone 44.1 -> + 48
998 */
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +0530999static void check_and_set_asrc_mode(struct audio_device *adev,
1000 struct audio_usecase *uc_info,
1001 snd_device_t snd_device)
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +05301002{
1003 ALOGV("%s snd device %d", __func__, snd_device);
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +05301004 int i, num_new_devices = 0;
1005 snd_device_t split_new_snd_devices[SND_DEVICE_OUT_END];
1006 /*
1007 *Split snd device for new combo use case
1008 *e.g. Headphopne 44.1-> + Ringtone (Headphone + Speaker)
1009 */
1010 if (platform_split_snd_device(adev->platform,
1011 snd_device,
1012 &num_new_devices,
1013 split_new_snd_devices) == 0) {
1014 for (i = 0; i < num_new_devices; i++)
1015 check_and_set_asrc_mode(adev, uc_info, split_new_snd_devices[i]);
1016 } else {
1017 int new_backend_idx = platform_get_backend_index(snd_device);
1018 if (((new_backend_idx == HEADPHONE_BACKEND) ||
1019 (new_backend_idx == HEADPHONE_44_1_BACKEND) ||
1020 (new_backend_idx == DSD_NATIVE_BACKEND)) &&
1021 !adev->asrc_mode_enabled) {
1022 struct listnode *node = NULL;
1023 struct audio_usecase *uc = NULL;
1024 struct stream_out *curr_out = NULL;
1025 int usecase_backend_idx = DEFAULT_CODEC_BACKEND;
1026 int i, num_devices, ret = 0;
1027 snd_device_t split_snd_devices[SND_DEVICE_OUT_END];
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +05301028
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +05301029 list_for_each(node, &adev->usecase_list) {
1030 uc = node_to_item(node, struct audio_usecase, list);
1031 curr_out = (struct stream_out*) uc->stream.out;
1032 if (curr_out && PCM_PLAYBACK == uc->type && uc != uc_info) {
1033 /*
1034 *Split snd device for existing combo use case
1035 *e.g. Ringtone (Headphone + Speaker) + Headphopne 44.1
1036 */
1037 ret = platform_split_snd_device(adev->platform,
1038 uc->out_snd_device,
1039 &num_devices,
1040 split_snd_devices);
1041 if (ret < 0 || num_devices == 0) {
1042 ALOGV("%s: Unable to split uc->out_snd_device: %d",__func__, uc->out_snd_device);
1043 split_snd_devices[0] = uc->out_snd_device;
1044 num_devices = 1;
Garmond Leung50058f62017-02-08 09:49:30 -08001045 }
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +05301046 for (i = 0; i < num_devices; i++) {
1047 usecase_backend_idx = platform_get_backend_index(split_snd_devices[i]);
1048 ALOGD("%s:snd_dev %d usecase_backend_idx %d",__func__, split_snd_devices[i],usecase_backend_idx);
1049 if((new_backend_idx == HEADPHONE_BACKEND) &&
1050 ((usecase_backend_idx == HEADPHONE_44_1_BACKEND) ||
1051 (usecase_backend_idx == DSD_NATIVE_BACKEND))) {
Aniket Kumar Latad13758f2020-08-06 15:11:36 -07001052 ALOGV("%s:DSD or native stream detected enabling asrcmode in hardware",
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +05301053 __func__);
1054 enable_asrc_mode(adev);
1055 break;
1056 } else if(((new_backend_idx == HEADPHONE_44_1_BACKEND) ||
1057 (new_backend_idx == DSD_NATIVE_BACKEND)) &&
1058 (usecase_backend_idx == HEADPHONE_BACKEND)) {
Aniket Kumar Latad13758f2020-08-06 15:11:36 -07001059 ALOGV("%s: 48K stream detected, disabling and enabling it \
1060 with asrcmode in hardware", __func__);
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +05301061 disable_audio_route(adev, uc);
1062 disable_snd_device(adev, uc->out_snd_device);
1063 // Apply true-high-quality-mode if DSD or > 44.1KHz or >=24-bit
1064 if (new_backend_idx == DSD_NATIVE_BACKEND)
1065 audio_route_apply_and_update_path(adev->audio_route,
1066 "hph-true-highquality-mode");
1067 else if ((new_backend_idx == HEADPHONE_44_1_BACKEND) &&
1068 (curr_out->bit_width >= 24))
1069 audio_route_apply_and_update_path(adev->audio_route,
1070 "hph-highquality-mode");
1071 enable_asrc_mode(adev);
1072 enable_snd_device(adev, uc->out_snd_device);
1073 enable_audio_route(adev, uc);
1074 break;
1075 }
1076 }
1077 // reset split devices count
1078 num_devices = 0;
Garmond Leung50058f62017-02-08 09:49:30 -08001079 }
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +05301080 if (adev->asrc_mode_enabled)
1081 break;
Preetam Singh Ranawatb0c0dd72016-08-18 00:32:06 +05301082 }
1083 }
1084 }
1085}
1086
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001087static int send_effect_enable_disable_mixer_ctl(struct audio_device *adev,
1088 struct audio_effect_config effect_config,
1089 unsigned int param_value)
1090{
1091 char mixer_ctl_name[] = "Audio Effect";
1092 struct mixer_ctl *ctl;
1093 long set_values[6];
Eric Laurent637e2d42018-11-15 12:24:31 -08001094 struct stream_in *in = adev_get_active_input(adev);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001095
Aniket Kumar Lata7de63522019-06-13 11:05:18 -07001096 if (in == NULL) {
1097 ALOGE("%s: active input stream is NULL", __func__);
1098 return -EINVAL;
1099 }
1100
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001101 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1102 if (!ctl) {
1103 ALOGE("%s: Could not get mixer ctl - %s",
1104 __func__, mixer_ctl_name);
1105 return -EINVAL;
1106 }
1107
1108 set_values[0] = 1; //0:Rx 1:Tx
1109 set_values[1] = in->app_type_cfg.app_type;
1110 set_values[2] = (long)effect_config.module_id;
1111 set_values[3] = (long)effect_config.instance_id;
1112 set_values[4] = (long)effect_config.param_id;
1113 set_values[5] = param_value;
1114
1115 mixer_ctl_set_array(ctl, set_values, ARRAY_SIZE(set_values));
1116
1117 return 0;
1118
1119}
1120
1121static int update_effect_param_ecns(struct audio_device *adev, unsigned int module_id,
1122 int effect_type, unsigned int *param_value)
1123{
1124 int ret = 0;
1125 struct audio_effect_config other_effect_config;
1126 struct audio_usecase *usecase = NULL;
Eric Laurent637e2d42018-11-15 12:24:31 -08001127 struct stream_in *in = adev_get_active_input(adev);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001128
Aniket Kumar Lata7de63522019-06-13 11:05:18 -07001129 if (in == NULL) {
1130 ALOGE("%s: active input stream is NULL", __func__);
1131 return -EINVAL;
1132 }
1133
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001134 usecase = get_usecase_from_list(adev, in->usecase);
1135 if (!usecase)
1136 return -EINVAL;
1137
1138 ret = platform_get_effect_config_data(usecase->in_snd_device, &other_effect_config,
1139 effect_type == EFFECT_AEC ? EFFECT_NS : EFFECT_AEC);
1140 if (ret < 0) {
1141 ALOGE("%s Failed to get effect params %d", __func__, ret);
1142 return ret;
1143 }
1144
1145 if (module_id == other_effect_config.module_id) {
1146 //Same module id for AEC/NS. Values need to be combined
1147 if (((effect_type == EFFECT_AEC) && (in->enable_ns)) ||
1148 ((effect_type == EFFECT_NS) && (in->enable_aec))) {
1149 *param_value |= other_effect_config.param_value;
1150 }
1151 }
1152
1153 return ret;
1154}
1155
1156static int enable_disable_effect(struct audio_device *adev, int effect_type, bool enable)
Gangadhar Sb0210342019-02-22 17:39:41 +05301157{
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001158 struct audio_effect_config effect_config;
1159 struct audio_usecase *usecase = NULL;
1160 int ret = 0;
1161 unsigned int param_value = 0;
Eric Laurent637e2d42018-11-15 12:24:31 -08001162 struct stream_in *in = adev_get_active_input(adev);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001163
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001164 if(!voice_extn_is_dynamic_ecns_enabled())
1165 return ENOSYS;
1166
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001167 if (!in) {
1168 ALOGE("%s: Invalid input stream", __func__);
1169 return -EINVAL;
1170 }
1171
1172 ALOGD("%s: effect_type:%d enable:%d", __func__, effect_type, enable);
1173
1174 usecase = get_usecase_from_list(adev, in->usecase);
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001175 if (usecase == NULL) {
1176 ALOGE("%s: Could not find the usecase (%d) in the list",
1177 __func__, in->usecase);
1178 return -EINVAL;
1179 }
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001180
1181 ret = platform_get_effect_config_data(usecase->in_snd_device, &effect_config, effect_type);
1182 if (ret < 0) {
1183 ALOGE("%s Failed to get module id %d", __func__, ret);
1184 return ret;
1185 }
1186 ALOGV("%s: %d %d usecase->id:%d usecase->in_snd_device:%d", __func__, effect_config.module_id,
1187 in->app_type_cfg.app_type, usecase->id, usecase->in_snd_device);
1188
1189 if(enable)
1190 param_value = effect_config.param_value;
1191
1192 /*Special handling for AEC & NS effects Param values need to be
1193 updated if module ids are same*/
1194
1195 if ((effect_type == EFFECT_AEC) || (effect_type == EFFECT_NS)) {
1196 ret = update_effect_param_ecns(adev, effect_config.module_id, effect_type, &param_value);
1197 if (ret < 0)
1198 return ret;
1199 }
1200
1201 ret = send_effect_enable_disable_mixer_ctl(adev, effect_config, param_value);
1202
1203 return ret;
1204}
1205
1206static void check_and_enable_effect(struct audio_device *adev)
1207{
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001208 if(!voice_extn_is_dynamic_ecns_enabled())
1209 return;
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001210
Eric Laurent637e2d42018-11-15 12:24:31 -08001211 struct stream_in *in = adev_get_active_input(adev);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001212
Eric Laurent637e2d42018-11-15 12:24:31 -08001213 if (in != NULL && !in->standby) {
1214 if (in->enable_aec)
1215 enable_disable_effect(adev, EFFECT_AEC, true);
1216
1217 if (in->enable_ns &&
1218 in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
1219 enable_disable_effect(adev, EFFECT_NS, true);
1220 }
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001221 }
1222}
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001223
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07001224int pcm_ioctl(struct pcm *pcm, int request, ...)
1225{
1226 va_list ap;
1227 void * arg;
1228 int pcm_fd = *(int*)pcm;
1229
1230 va_start(ap, request);
1231 arg = va_arg(ap, void *);
1232 va_end(ap);
1233
1234 return ioctl(pcm_fd, request, arg);
1235}
1236
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -07001237int enable_audio_route(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001238 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001239{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001240 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001241 char mixer_path[MIXER_PATH_MAX_LENGTH];
Manish Dewangan58229382017-02-02 15:48:41 +05301242 struct stream_out *out = NULL;
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05301243 struct stream_in *in = NULL;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001244 struct listnode out_devices;
Soumya Managoli6993b762018-06-28 16:04:57 +05301245 int ret = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001246
1247 if (usecase == NULL)
1248 return -EINVAL;
1249
1250 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
1251
Carter Hsu2e429db2019-05-14 18:50:52 +08001252 if (usecase->type == PCM_CAPTURE) {
1253 struct stream_in *in = usecase->stream.in;
1254 struct audio_usecase *uinfo;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001255 snd_device = usecase->in_snd_device;
Carter Hsu2e429db2019-05-14 18:50:52 +08001256
1257 if (in) {
1258 if (in->enable_aec || in->enable_ec_port) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001259 list_init(&out_devices);
1260 update_device_list(&out_devices, AUDIO_DEVICE_OUT_SPEAKER, "", true);
Carter Hsu2e429db2019-05-14 18:50:52 +08001261 struct listnode *node;
1262 struct audio_usecase *voip_usecase = get_usecase_from_list(adev,
1263 USECASE_AUDIO_PLAYBACK_VOIP);
1264 if (voip_usecase) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001265 assign_devices(&out_devices,
1266 &voip_usecase->stream.out->device_list);
Carter Hsu2e429db2019-05-14 18:50:52 +08001267 } else if (adev->primary_output &&
1268 !adev->primary_output->standby) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001269 assign_devices(&out_devices,
1270 &adev->primary_output->device_list);
Carter Hsu2e429db2019-05-14 18:50:52 +08001271 } else {
1272 list_for_each(node, &adev->usecase_list) {
1273 uinfo = node_to_item(node, struct audio_usecase, list);
1274 if (uinfo->type != PCM_CAPTURE) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001275 assign_devices(&out_devices,
1276 &uinfo->stream.out->device_list);
Carter Hsu2e429db2019-05-14 18:50:52 +08001277 break;
1278 }
1279 }
1280 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001281
1282 platform_set_echo_reference(adev, true, &out_devices);
Carter Hsu2e429db2019-05-14 18:50:52 +08001283 in->ec_opened = true;
1284 }
1285 }
Guodong Huf5e614d2019-06-24 18:42:03 +08001286 } else if ((usecase->type == TRANSCODE_LOOPBACK_TX) || ((usecase->type == PCM_HFP_CALL) &&
1287 ((usecase->id == USECASE_AUDIO_HFP_SCO) || (usecase->id == USECASE_AUDIO_HFP_SCO_WB)) &&
1288 (usecase->in_snd_device == SND_DEVICE_IN_VOICE_SPEAKER_MIC_HFP_MMSECNS))) {
Carter Hsu2e429db2019-05-14 18:50:52 +08001289 snd_device = usecase->in_snd_device;
1290 } else {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001291 snd_device = usecase->out_snd_device;
Carter Hsu2e429db2019-05-14 18:50:52 +08001292 }
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001293
Kunlei Zhanga3c4cb02021-03-05 16:03:54 +08001294 if (usecase->type == PCM_CAPTURE) {
1295 if (platform_get_fluence_nn_state(adev->platform) == 0) {
1296 platform_set_fluence_nn_state(adev->platform, true);
1297 ALOGD("%s: set fluence nn capture state", __func__);
1298 }
1299 }
1300
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08001301#ifdef DS1_DOLBY_DAP_ENABLED
1302 audio_extn_dolby_set_dmid(adev);
1303 audio_extn_dolby_set_endpoint(adev);
1304#endif
Pradnya Chaphekar8a9dcd82014-09-09 09:49:10 -07001305 audio_extn_dolby_ds2_set_endpoint(adev);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001306 audio_extn_sound_trigger_update_stream_status(usecase, ST_EVENT_STREAM_BUSY);
Dhananjay Kumar45b71742014-05-29 21:47:27 +05301307 audio_extn_listen_update_stream_status(usecase, LISTEN_EVENT_STREAM_BUSY);
Ben Romberger1fafdde2015-09-09 19:43:15 -07001308 audio_extn_utils_send_app_type_cfg(adev, usecase);
Jasmine Cha4dcc1092019-03-04 18:12:47 +08001309 if (audio_extn_is_maxx_audio_enabled())
1310 audio_extn_ma_set_device(usecase);
Dhananjay Kumar14170dd2015-08-28 13:24:16 +05301311 audio_extn_utils_send_audio_calibration(adev, usecase);
Zhou Songbaddf9f2020-11-20 13:57:39 +08001312 if ((usecase->type == PCM_PLAYBACK) && is_offload_usecase(usecase->id)) {
1313 out = usecase->stream.out;
1314 if (out && out->compr)
Manish Dewangan58229382017-02-02 15:48:41 +05301315 audio_extn_utils_compress_set_clk_rec_mode(usecase);
1316 }
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05301317
1318 if (usecase->type == PCM_CAPTURE) {
Kunlei Zhanga3c4cb02021-03-05 16:03:54 +08001319 if (platform_get_fluence_nn_state(adev->platform) == 1 &&
1320 adev->fluence_nn_usecase_id == USECASE_INVALID ) {
1321 adev->fluence_nn_usecase_id = usecase->id;
1322 ALOGD("%s: assign fluence nn usecase %d", __func__, usecase->id);
1323 }
1324 }
1325
1326 if (usecase->type == PCM_CAPTURE) {
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05301327 in = usecase->stream.in;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001328 if (in && is_loopback_input_device(get_device_types(&in->device_list))) {
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05301329 ALOGD("%s: set custom mtmx params v1", __func__);
1330 audio_extn_set_custom_mtmx_params_v1(adev, usecase, true);
1331 }
1332 } else {
1333 audio_extn_set_custom_mtmx_params_v2(adev, usecase, true);
1334 }
Manish Dewangan58229382017-02-02 15:48:41 +05301335
Andy Hung756ecc12018-10-19 17:47:12 -07001336 // we shouldn't truncate mixer_path
1337 ALOGW_IF(strlcpy(mixer_path, use_case_table[usecase->id], sizeof(mixer_path))
1338 >= sizeof(mixer_path), "%s: truncation on mixer path", __func__);
1339 // this also appends to mixer_path
Banajit Goswami20cdd212015-09-11 01:11:30 -07001340 platform_add_backend_name(mixer_path, snd_device, usecase);
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07001341 ALOGD("%s: apply mixer and update path: %s", __func__, mixer_path);
Soumya Managoli6993b762018-06-28 16:04:57 +05301342 ret = audio_route_apply_and_update_path(adev->audio_route, mixer_path);
1343 if (!ret && usecase->id == USECASE_AUDIO_PLAYBACK_FM) {
1344 struct str_parms *parms = str_parms_create_str("fm_restore_volume=1");
1345 if (parms) {
1346 audio_extn_fm_set_parameters(adev, parms);
1347 str_parms_destroy(parms);
1348 }
1349 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001350 ALOGV("%s: exit", __func__);
1351 return 0;
1352}
1353
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001354int disable_audio_route(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001355 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001356{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001357 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001358 char mixer_path[MIXER_PATH_MAX_LENGTH];
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05301359 struct stream_in *in = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001360
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05301361 if (usecase == NULL || usecase->id == USECASE_INVALID)
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001362 return -EINVAL;
1363
1364 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Surendar Karka93cd25a2018-08-28 14:21:37 +05301365 if (usecase->type == PCM_CAPTURE || usecase->type == TRANSCODE_LOOPBACK_TX)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001366 snd_device = usecase->in_snd_device;
1367 else
1368 snd_device = usecase->out_snd_device;
Kunlei Zhange7cab1a2020-05-17 20:31:05 +08001369
1370 /* disable island and power mode on supported device for voice call */
1371 if (usecase->type == VOICE_CALL) {
1372 if (usecase->in_snd_device != SND_DEVICE_NONE) {
1373 if (platform_get_island_cfg_on_device(adev->platform, usecase->in_snd_device) &&
1374 platform_get_power_mode_on_device(adev->platform, usecase->in_snd_device)) {
1375 platform_set_island_cfg_on_device(adev, usecase->in_snd_device, false);
1376 platform_set_power_mode_on_device(adev, usecase->in_snd_device, false);
1377 platform_reset_island_power_status(adev->platform, usecase->in_snd_device);
Kunlei Zhang91c4b332020-07-20 17:53:14 +08001378 if (voice_is_lte_call_active(adev))
1379 platform_set_tx_lpi_mode(adev->platform, false);
Kunlei Zhange7cab1a2020-05-17 20:31:05 +08001380 ALOGD("%s: disable island cfg and power mode in voice tx path",
1381 __func__);
1382 }
1383 }
1384 if (usecase->out_snd_device != SND_DEVICE_NONE) {
1385 if (platform_get_island_cfg_on_device(adev->platform, usecase->out_snd_device) &&
1386 platform_get_power_mode_on_device(adev->platform, usecase->out_snd_device)) {
1387 platform_set_island_cfg_on_device(adev, usecase->out_snd_device, false);
1388 platform_set_power_mode_on_device(adev, usecase->out_snd_device, false);
1389 platform_reset_island_power_status(adev->platform, usecase->out_snd_device);
1390 ALOGD("%s: disable island cfg and power mode in voice rx path",
1391 __func__);
1392 }
1393 }
1394 }
1395
Andy Hung756ecc12018-10-19 17:47:12 -07001396 // we shouldn't truncate mixer_path
1397 ALOGW_IF(strlcpy(mixer_path, use_case_table[usecase->id], sizeof(mixer_path))
1398 >= sizeof(mixer_path), "%s: truncation on mixer path", __func__);
1399 // this also appends to mixer_path
Banajit Goswami20cdd212015-09-11 01:11:30 -07001400 platform_add_backend_name(mixer_path, snd_device, usecase);
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07001401 ALOGD("%s: reset and update mixer path: %s", __func__, mixer_path);
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07001402 audio_route_reset_and_update_path(adev->audio_route, mixer_path);
Carter Hsu2e429db2019-05-14 18:50:52 +08001403 if (usecase->type == PCM_CAPTURE) {
1404 struct stream_in *in = usecase->stream.in;
1405 if (in && in->ec_opened) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001406 struct listnode out_devices;
1407 list_init(&out_devices);
1408 platform_set_echo_reference(in->dev, false, &out_devices);
Carter Hsu2e429db2019-05-14 18:50:52 +08001409 in->ec_opened = false;
1410 }
1411 }
Kunlei Zhanga3c4cb02021-03-05 16:03:54 +08001412 if (usecase->id == adev->fluence_nn_usecase_id) {
1413 platform_set_fluence_nn_state(adev->platform, false);
1414 adev->fluence_nn_usecase_id = USECASE_INVALID;
1415 ALOGD("%s: reset fluence nn capture state", __func__);
1416 }
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001417 audio_extn_sound_trigger_update_stream_status(usecase, ST_EVENT_STREAM_FREE);
Dhananjay Kumar45b71742014-05-29 21:47:27 +05301418 audio_extn_listen_update_stream_status(usecase, LISTEN_EVENT_STREAM_FREE);
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05301419
1420 if (usecase->type == PCM_CAPTURE) {
1421 in = usecase->stream.in;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001422 if (in && is_loopback_input_device(get_device_types(&in->device_list))) {
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05301423 ALOGD("%s: reset custom mtmx params v1", __func__);
1424 audio_extn_set_custom_mtmx_params_v1(adev, usecase, false);
1425 }
1426 } else {
1427 audio_extn_set_custom_mtmx_params_v2(adev, usecase, false);
1428 }
1429
Weiyin Jiang298ffd92019-06-03 14:29:30 +08001430 if ((usecase->type == PCM_PLAYBACK) &&
1431 (usecase->stream.out != NULL))
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +05301432 usecase->stream.out->pspd_coeff_sent = false;
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05301433
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001434 ALOGV("%s: exit", __func__);
1435 return 0;
1436}
1437
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -07001438int enable_snd_device(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001439 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001440{
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301441 int i, num_devices = 0;
1442 snd_device_t new_snd_devices[SND_DEVICE_OUT_END];
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001443 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
1444
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001445 if (snd_device < SND_DEVICE_MIN ||
1446 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001447 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001448 return -EINVAL;
1449 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001450
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001451 if (platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0) {
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001452 ALOGE("%s: Invalid sound device returned", __func__);
1453 return -EINVAL;
1454 }
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001455
1456 adev->snd_dev_ref_cnt[snd_device]++;
1457
1458 if ((adev->snd_dev_ref_cnt[snd_device] > 1) &&
1459 (platform_split_snd_device(adev->platform,
1460 snd_device,
1461 &num_devices,
1462 new_snd_devices) != 0)) {
Eric Laurent994a6932013-07-17 11:51:42 -07001463 ALOGV("%s: snd_device(%d: %s) is already active",
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001464 __func__, snd_device, device_name);
Aniket Kumar Lata990de552019-07-11 14:20:23 -07001465 /* Set backend config for A2DP to ensure slimbus configuration
1466 is correct if A2DP is already active and backend is closed
1467 and re-opened */
1468 if (snd_device == SND_DEVICE_OUT_BT_A2DP)
1469 audio_extn_a2dp_set_source_backend_cfg();
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001470 return 0;
1471 }
1472
Gopikrishnaiah Anandane85d0462014-06-30 21:41:20 -07001473 if (audio_extn_spkr_prot_is_enabled())
1474 audio_extn_spkr_prot_calib_cancel(adev);
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -07001475
Aalique Grahame22e49102018-12-18 14:23:57 -08001476 audio_extn_dsm_feedback_enable(adev, snd_device, true);
1477
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001478 if (platform_can_enable_spkr_prot_on_device(snd_device) &&
1479 audio_extn_spkr_prot_is_enabled()) {
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001480 if (platform_get_spkr_prot_acdb_id(snd_device) < 0) {
1481 goto err;
1482 }
1483 audio_extn_dev_arbi_acquire(snd_device);
1484 if (audio_extn_spkr_prot_start_processing(snd_device)) {
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -08001485 ALOGE("%s: spkr_start_processing failed", __func__);
Ravit Dennisaaee49c2015-02-04 21:26:22 +02001486 audio_extn_dev_arbi_release(snd_device);
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001487 goto err;
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -08001488 }
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001489 } else if (platform_split_snd_device(adev->platform,
1490 snd_device,
1491 &num_devices,
1492 new_snd_devices) == 0) {
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301493 for (i = 0; i < num_devices; i++) {
1494 enable_snd_device(adev, new_snd_devices[i]);
1495 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001496 platform_set_speaker_gain_in_combo(adev, snd_device, true);
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -08001497 } else {
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07001498 ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, device_name);
Naresh Tannirucd2353e2016-08-19 00:37:25 +05301499
Kunlei Zhange7cab1a2020-05-17 20:31:05 +08001500 /* enable island and power mode on supported device */
1501 if (platform_get_island_cfg_on_device(adev->platform, snd_device) &&
1502 platform_get_power_mode_on_device(adev->platform, snd_device)) {
1503 platform_set_island_cfg_on_device(adev, snd_device, true);
1504 platform_set_power_mode_on_device(adev, snd_device, true);
Kunlei Zhang91c4b332020-07-20 17:53:14 +08001505 if (voice_is_lte_call_active(adev) &&
1506 (snd_device >= SND_DEVICE_IN_BEGIN &&
1507 snd_device < SND_DEVICE_IN_END))
1508 platform_set_tx_lpi_mode(adev->platform, true);
Kunlei Zhange7cab1a2020-05-17 20:31:05 +08001509 ALOGD("%s: enable island cfg and power mode on: %s",
1510 __func__, device_name);
1511 }
Preetam Singh Ranawatf1d417c2017-01-10 17:00:32 +05301512
Revathi Uddarajub26e3932020-06-10 14:51:02 +05301513 if (SND_DEVICE_OUT_BT_A2DP == snd_device) {
1514 if (audio_extn_a2dp_start_playback() < 0) {
1515 ALOGE(" fail to configure A2dp Source control path ");
1516 goto err;
1517 } else {
1518 adev->a2dp_started = true;
1519 }
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001520 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001521
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001522 if ((SND_DEVICE_IN_BT_A2DP == snd_device) &&
1523 (audio_extn_a2dp_start_capture() < 0)) {
1524 ALOGE(" fail to configure A2dp Sink control path ");
1525 goto err;
1526 }
Naresh Tannirucd2353e2016-08-19 00:37:25 +05301527
Mingshu Pang7be5b1d2020-03-04 15:24:38 +08001528 if ((SND_DEVICE_OUT_BT_SCO_SWB == snd_device) ||
1529 (SND_DEVICE_IN_BT_SCO_MIC_SWB_NREC == snd_device) ||
1530 (SND_DEVICE_IN_BT_SCO_MIC_SWB == snd_device)) {
1531 if (!adev->bt_sco_on || (audio_extn_sco_start_configuration() < 0)) {
1532 ALOGE(" fail to configure sco control path ");
1533 goto err;
1534 }
Zhou Song12c29502019-03-16 10:37:18 +08001535 }
1536
Zhou Song331c8e52019-08-26 14:16:12 +08001537 configure_btsco_sample_rate(snd_device);
Bharath Ramachandramurthy0de16782014-03-28 21:34:33 -07001538 /* due to the possibility of calibration overwrite between listen
1539 and audio, notify listen hal before audio calibration is sent */
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001540 audio_extn_sound_trigger_update_device_status(snd_device,
1541 ST_EVENT_SND_DEVICE_BUSY);
Dhananjay Kumar45b71742014-05-29 21:47:27 +05301542 audio_extn_listen_update_device_status(snd_device,
1543 LISTEN_EVENT_SND_DEVICE_BUSY);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001544 if (platform_get_snd_device_acdb_id(snd_device) < 0) {
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001545 audio_extn_sound_trigger_update_device_status(snd_device,
1546 ST_EVENT_SND_DEVICE_FREE);
Dhananjay Kumar45b71742014-05-29 21:47:27 +05301547 audio_extn_listen_update_device_status(snd_device,
1548 LISTEN_EVENT_SND_DEVICE_FREE);
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001549 goto err;
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -07001550 }
Lior Barenboim0b61bc72014-05-13 13:01:37 +03001551 audio_extn_dev_arbi_acquire(snd_device);
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07001552 audio_route_apply_and_update_path(adev->audio_route, device_name);
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301553
1554 if (SND_DEVICE_OUT_HEADPHONES == snd_device &&
1555 !adev->native_playback_enabled &&
1556 audio_is_true_native_stream_active(adev)) {
1557 ALOGD("%s: %d: napb: enabling native mode in hardware",
1558 __func__, __LINE__);
1559 audio_route_apply_and_update_path(adev->audio_route,
1560 "true-native-mode");
1561 adev->native_playback_enabled = true;
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +05301562 }
Dhanalakshmi Siddani10621622018-04-30 15:07:27 +05301563 if (((snd_device == SND_DEVICE_IN_HANDSET_6MIC) ||
1564 (snd_device == SND_DEVICE_IN_HANDSET_QMIC)) &&
Eric Laurent637e2d42018-11-15 12:24:31 -08001565 (audio_extn_ffv_get_stream() == adev_get_active_input(adev))) {
Garmond Leunge2433c32017-09-28 21:51:22 -07001566 ALOGD("%s: init ec ref loopback", __func__);
1567 audio_extn_ffv_init_ec_ref_loopback(adev, snd_device);
1568 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001569 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001570 return 0;
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001571err:
1572 adev->snd_dev_ref_cnt[snd_device]--;
1573 return -EINVAL;;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001574}
1575
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001576int disable_snd_device(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001577 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001578{
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301579 int i, num_devices = 0;
1580 snd_device_t new_snd_devices[SND_DEVICE_OUT_END];
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001581 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
1582
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001583 if (snd_device < SND_DEVICE_MIN ||
1584 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001585 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001586 return -EINVAL;
1587 }
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001588
1589 if (platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0) {
1590 ALOGE("%s: Invalid sound device returned", __func__);
1591 return -EINVAL;
1592 }
1593
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001594 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
1595 ALOGE("%s: device ref cnt is already 0", __func__);
1596 return -EINVAL;
1597 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001598
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001599 adev->snd_dev_ref_cnt[snd_device]--;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -07001600
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001601
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001602 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07001603 ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, device_name);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301604
Aalique Grahame22e49102018-12-18 14:23:57 -08001605 audio_extn_dsm_feedback_enable(adev, snd_device, false);
1606
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001607 if (platform_can_enable_spkr_prot_on_device(snd_device) &&
1608 audio_extn_spkr_prot_is_enabled()) {
Anish Kumar46c7b872014-09-09 01:49:44 -07001609 audio_extn_spkr_prot_stop_processing(snd_device);
Vignesh Kulothungan3b5fae52017-09-25 12:16:30 -07001610
1611 // when speaker device is disabled, reset swap.
1612 // will be renabled on usecase start
1613 platform_set_swap_channels(adev, false);
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001614 } else if (platform_split_snd_device(adev->platform,
1615 snd_device,
1616 &num_devices,
1617 new_snd_devices) == 0) {
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301618 for (i = 0; i < num_devices; i++) {
1619 disable_snd_device(adev, new_snd_devices[i]);
1620 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001621 platform_set_speaker_gain_in_combo(adev, snd_device, false);
Lior Barenboim0b61bc72014-05-13 13:01:37 +03001622 } else {
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07001623 audio_route_reset_and_update_path(adev->audio_route, device_name);
Lior Barenboim0b61bc72014-05-13 13:01:37 +03001624 }
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001625
Revathi Uddarajub26e3932020-06-10 14:51:02 +05301626 if (snd_device == SND_DEVICE_OUT_BT_A2DP) {
Naresh Tannirucd2353e2016-08-19 00:37:25 +05301627 audio_extn_a2dp_stop_playback();
Revathi Uddarajub26e3932020-06-10 14:51:02 +05301628 adev->a2dp_started = false;
1629 } else if (snd_device == SND_DEVICE_IN_BT_A2DP)
Florian Pfister1a84f312018-07-19 14:38:18 +02001630 audio_extn_a2dp_stop_capture();
Zhou Songd6d71752019-05-21 18:08:51 +08001631 else if ((snd_device == SND_DEVICE_OUT_HDMI) ||
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001632 (snd_device == SND_DEVICE_OUT_DISPLAY_PORT))
Ashish Jain81eb2a82015-05-13 10:52:34 +05301633 adev->is_channel_status_set = false;
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001634 else if ((snd_device == SND_DEVICE_OUT_HEADPHONES) &&
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301635 adev->native_playback_enabled) {
1636 ALOGD("%s: %d: napb: disabling native mode in hardware",
1637 __func__, __LINE__);
1638 audio_route_reset_and_update_path(adev->audio_route,
1639 "true-native-mode");
1640 adev->native_playback_enabled = false;
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001641 } else if ((snd_device == SND_DEVICE_OUT_HEADPHONES) &&
Preetam Singh Ranawatb0c0dd72016-08-18 00:32:06 +05301642 adev->asrc_mode_enabled) {
1643 ALOGD("%s: %d: disabling asrc mode in hardware", __func__, __LINE__);
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +05301644 disable_asrc_mode(adev);
1645 audio_route_apply_and_update_path(adev->audio_route, "hph-lowpower-mode");
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001646 } else if (((snd_device == SND_DEVICE_IN_HANDSET_6MIC) ||
Dhanalakshmi Siddaniaf4bd622019-02-27 16:28:06 +05301647 (snd_device == SND_DEVICE_IN_HANDSET_QMIC)) &&
Eric Laurent637e2d42018-11-15 12:24:31 -08001648 (audio_extn_ffv_get_stream() == adev_get_active_input(adev))) {
Garmond Leunge2433c32017-09-28 21:51:22 -07001649 ALOGD("%s: deinit ec ref loopback", __func__);
1650 audio_extn_ffv_deinit_ec_ref_loopback(adev, snd_device);
1651 }
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001652
1653 audio_extn_utils_release_snd_device(snd_device);
1654 } else {
1655 if (platform_split_snd_device(adev->platform,
1656 snd_device,
1657 &num_devices,
1658 new_snd_devices) == 0) {
1659 for (i = 0; i < num_devices; i++) {
1660 adev->snd_dev_ref_cnt[new_snd_devices[i]]--;
1661 }
1662 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001663 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001664
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001665 return 0;
1666}
1667
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001668/*
1669 legend:
1670 uc - existing usecase
1671 new_uc - new usecase
1672 d1, d11, d2 - SND_DEVICE enums
1673 a1, a2 - corresponding ANDROID device enums
1674 B1, B2 - backend strings
1675
1676case 1
1677 uc->dev d1 (a1) B1
1678 new_uc->dev d1 (a1), d2 (a2) B1, B2
1679
1680 resolution: disable and enable uc->dev on d1
1681
1682case 2
1683 uc->dev d1 (a1) B1
1684 new_uc->dev d11 (a1) B1
1685
1686 resolution: need to switch uc since d1 and d11 are related
1687 (e.g. speaker and voice-speaker)
1688 use ANDROID_DEVICE_OUT enums to match devices since SND_DEVICE enums may vary
1689
1690case 3
1691 uc->dev d1 (a1) B1
1692 new_uc->dev d2 (a2) B2
1693
1694 resolution: no need to switch uc
1695
1696case 4
1697 uc->dev d1 (a1) B1
1698 new_uc->dev d2 (a2) B1
1699
1700 resolution: disable enable uc-dev on d2 since backends match
1701 we cannot enable two streams on two different devices if they
1702 share the same backend. e.g. if offload is on speaker device using
1703 QUAD_MI2S backend and a low-latency stream is started on voice-handset
1704 using the same backend, offload must also be switched to voice-handset.
1705
1706case 5
1707 uc->dev d1 (a1) B1
1708 new_uc->dev d1 (a1), d2 (a2) B1
1709
1710 resolution: disable enable uc-dev on d2 since backends match
1711 we cannot enable two streams on two different devices if they
1712 share the same backend.
1713
1714case 6
1715 uc->dev d1 (a1) B1
1716 new_uc->dev d2 (a1) B2
1717
1718 resolution: no need to switch
1719
1720case 7
1721 uc->dev d1 (a1), d2 (a2) B1, B2
1722 new_uc->dev d1 (a1) B1
1723
1724 resolution: no need to switch
1725
Zhou Song4ba65882018-07-09 14:48:07 +08001726case 8
1727 uc->dev d1 (a1) B1
1728 new_uc->dev d11 (a1), d2 (a2) B1, B2
1729 resolution: compared to case 1, for this case, d1 and d11 are related
1730 then need to do the same as case 2 to siwtch to new uc
Preetam Singh Ranawatd399e6f2020-06-25 12:20:36 +05301731
1732case 9
1733 uc->dev d1 (a1), d2(a2) B1 B2
1734 new_uc->dev d1 (a1), d22 (a2) B1, B2
1735 resolution: disable enable uc-dev on d2 since backends match
1736 we cannot enable two streams on two different devices if they
1737 share the same backend. This is special case for combo use case
1738 with a2dp and sco devices which uses same backend.
1739 e.g. speaker-a2dp and speaker-btsco
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001740*/
1741static snd_device_t derive_playback_snd_device(void * platform,
1742 struct audio_usecase *uc,
1743 struct audio_usecase *new_uc,
1744 snd_device_t new_snd_device)
1745{
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001746 struct listnode a1, a2;
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001747
1748 snd_device_t d1 = uc->out_snd_device;
1749 snd_device_t d2 = new_snd_device;
1750
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001751 list_init(&a1);
1752 list_init(&a2);
1753
Siddartha Shaik31b530e2017-05-19 15:26:33 +05301754 switch (uc->type) {
Surendar Karka93cd25a2018-08-28 14:21:37 +05301755 case TRANSCODE_LOOPBACK_RX :
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001756 assign_devices(&a1, &uc->stream.inout->out_config.device_list);
1757 assign_devices(&a2, &new_uc->stream.inout->out_config.device_list);
Siddartha Shaik31b530e2017-05-19 15:26:33 +05301758 break;
1759 default :
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001760 assign_devices(&a1, &uc->stream.out->device_list);
1761 assign_devices(&a2, &new_uc->stream.out->device_list);
Siddartha Shaik31b530e2017-05-19 15:26:33 +05301762 break;
1763 }
1764
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001765 // Treat as a special case when a1 and a2 are not disjoint
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001766 if (!compare_devices(&a1, &a2) &&
1767 compare_devices_for_any_match(&a1 ,&a2)) {
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001768 snd_device_t d3[2];
1769 int num_devices = 0;
1770 int ret = platform_split_snd_device(platform,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001771 list_length(&a1) > 1 ? d1 : d2,
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001772 &num_devices,
1773 d3);
1774 if (ret < 0) {
1775 if (ret != -ENOSYS) {
1776 ALOGW("%s failed to split snd_device %d",
1777 __func__,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001778 list_length(&a1) > 1 ? d1 : d2);
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001779 }
1780 goto end;
1781 }
1782
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001783 if (platform_check_backends_match(d3[0], d3[1])) {
1784 return d2; // case 5
1785 } else {
Preetam Singh Ranawatd399e6f2020-06-25 12:20:36 +05301786 if ((list_length(&a1) > 1) && (list_length(&a2) > 1) &&
1787 platform_check_backends_match(d1, d2))
1788 return d2; //case 9
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001789 if (list_length(&a1) > 1)
Samyak Jaind826b502019-07-17 16:16:42 +05301790 return d1; //case 7
Garmond Leungb9eeba42018-09-18 11:10:41 -07001791 // check if d1 is related to any of d3's
1792 if (d1 == d3[0] || d1 == d3[1])
Zhou Song4ba65882018-07-09 14:48:07 +08001793 return d1; // case 1
1794 else
1795 return d3[1]; // case 8
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001796 }
1797 } else {
1798 if (platform_check_backends_match(d1, d2)) {
1799 return d2; // case 2, 4
1800 } else {
1801 return d1; // case 6, 3
1802 }
1803 }
1804
1805end:
1806 return d2; // return whatever was calculated before.
1807}
1808
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001809static void check_usecases_codec_backend(struct audio_device *adev,
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301810 struct audio_usecase *uc_info,
1811 snd_device_t snd_device)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001812{
1813 struct listnode *node;
1814 struct audio_usecase *usecase;
1815 bool switch_device[AUDIO_USECASE_MAX];
Chaithanya Krishna Bacharaju49e7db02017-03-14 11:57:26 +05301816 snd_device_t uc_derive_snd_device;
1817 snd_device_t derive_snd_device[AUDIO_USECASE_MAX];
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001818 snd_device_t split_snd_devices[SND_DEVICE_OUT_END];
1819 int i, num_uc_to_switch = 0, num_devices = 0;
kunleiz5cd52b82016-11-07 17:22:52 +08001820 int status = 0;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301821 bool force_restart_session = false;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001822 /*
1823 * This function is to make sure that all the usecases that are active on
1824 * the hardware codec backend are always routed to any one device that is
1825 * handled by the hardware codec.
1826 * For example, if low-latency and deep-buffer usecases are currently active
1827 * on speaker and out_set_parameters(headset) is received on low-latency
1828 * output, then we have to make sure deep-buffer is also switched to headset,
1829 * because of the limitation that both the devices cannot be enabled
1830 * at the same time as they share the same backend.
1831 */
Mingming Yin3ee55c62014-08-04 14:23:35 -07001832 /*
1833 * This call is to check if we need to force routing for a particular stream
1834 * If there is a backend configuration change for the device when a
1835 * new stream starts, then ADM needs to be closed and re-opened with the new
1836 * configuraion. This call check if we need to re-route all the streams
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -08001837 * associated with the backend. Touch tone + 24 bit + native playback.
Mingming Yin3ee55c62014-08-04 14:23:35 -07001838 */
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -08001839 bool force_routing = platform_check_and_set_codec_backend_cfg(adev, uc_info,
1840 snd_device);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301841 /* For a2dp device reconfigure all active sessions
1842 * with new AFE encoder format based on a2dp state
1843 */
1844 if ((SND_DEVICE_OUT_BT_A2DP == snd_device ||
Preetam Singh Ranawatd399e6f2020-06-25 12:20:36 +05301845 SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP == snd_device ||
1846 SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP == snd_device) &&
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301847 audio_extn_a2dp_is_force_device_switch()) {
1848 force_routing = true;
1849 force_restart_session = true;
1850 }
Kunlei Zhange7cab1a2020-05-17 20:31:05 +08001851
1852 /*
1853 * Island cfg and power mode config needs to set before AFE port start.
1854 * Set force routing in case of voice device was enable before.
1855 */
1856 if (uc_info->type == VOICE_CALL &&
1857 voice_extn_is_voice_power_mode_supported() &&
Kunlei Zhang67cc7072020-12-18 17:16:49 +08001858 is_supported_conc_usecase_for_power_mode_call(adev) &&
Kunlei Zhange7cab1a2020-05-17 20:31:05 +08001859 platform_check_and_update_island_power_status(adev->platform,
1860 uc_info,
1861 snd_device)) {
1862 force_routing = true;
1863 ALOGD("%s:becf: force routing %d for power mode supported device",
1864 __func__, force_routing);
1865 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301866 ALOGD("%s:becf: force routing %d", __func__, force_routing);
1867
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001868 /* Disable all the usecases on the shared backend other than the
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -08001869 * specified usecase.
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -08001870 */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001871 for (i = 0; i < AUDIO_USECASE_MAX; i++)
1872 switch_device[i] = false;
1873
1874 list_for_each(node, &adev->usecase_list) {
1875 usecase = node_to_item(node, struct audio_usecase, list);
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -08001876
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301877 ALOGD("%s:becf: (%d) check_usecases curr device: %s, usecase device:%s "
1878 "backends match %d",__func__, i,
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301879 platform_get_snd_device_name(snd_device),
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301880 platform_get_snd_device_name(usecase->out_snd_device),
1881 platform_check_backends_match(snd_device, usecase->out_snd_device));
Rahul Sharma99770982019-03-06 17:05:26 +05301882 if ((usecase->type != PCM_CAPTURE) && (usecase != uc_info) &&
1883 (usecase->type != PCM_PASSTHROUGH)) {
Ashish Jain6a65b352017-03-21 17:24:40 +05301884 uc_derive_snd_device = derive_playback_snd_device(adev->platform,
1885 usecase, uc_info, snd_device);
1886 if (((uc_derive_snd_device != usecase->out_snd_device) || force_routing) &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001887 (is_codec_backend_out_device_type(&usecase->device_list) ||
1888 compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_AUX_DIGITAL) ||
1889 compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_USB_DEVICE) ||
1890 compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_USB_HEADSET) ||
1891 is_a2dp_out_device_type(&usecase->device_list) ||
1892 is_sco_out_device_type(&usecase->device_list)) &&
Ashish Jain6a65b352017-03-21 17:24:40 +05301893 ((force_restart_session) ||
1894 (platform_check_backends_match(snd_device, usecase->out_snd_device)))) {
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301895 ALOGD("%s:becf: check_usecases (%s) is active on (%s) - disabling ..",
1896 __func__, use_case_table[usecase->id],
1897 platform_get_snd_device_name(usecase->out_snd_device));
1898 disable_audio_route(adev, usecase);
1899 switch_device[usecase->id] = true;
Chaithanya Krishna Bacharaju49e7db02017-03-14 11:57:26 +05301900 /* Enable existing usecase on derived playback device */
1901 derive_snd_device[usecase->id] = uc_derive_snd_device;
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301902 num_uc_to_switch++;
Ashish Jain6a65b352017-03-21 17:24:40 +05301903 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001904 }
1905 }
1906
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301907 ALOGD("%s:becf: check_usecases num.of Usecases to switch %d", __func__,
1908 num_uc_to_switch);
1909
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001910 if (num_uc_to_switch) {
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07001911 /* All streams have been de-routed. Disable the device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001912
Venkata Narendra Kumar Gutta7610e632014-04-14 23:16:38 +05301913 /* Make sure the previous devices to be disabled first and then enable the
1914 selected devices */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001915 list_for_each(node, &adev->usecase_list) {
1916 usecase = node_to_item(node, struct audio_usecase, list);
1917 if (switch_device[usecase->id]) {
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001918 /* Check if output sound device to be switched can be split and if any
1919 of the split devices match with derived sound device */
1920 if (platform_split_snd_device(adev->platform, usecase->out_snd_device,
1921 &num_devices, split_snd_devices) == 0) {
1922 adev->snd_dev_ref_cnt[usecase->out_snd_device]--;
1923 for (i = 0; i < num_devices; i++) {
1924 /* Disable devices that do not match with derived sound device */
1925 if (split_snd_devices[i] != derive_snd_device[usecase->id])
1926 disable_snd_device(adev, split_snd_devices[i]);
1927 }
1928 } else {
1929 disable_snd_device(adev, usecase->out_snd_device);
1930 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001931 }
1932 }
1933
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -07001934 list_for_each(node, &adev->usecase_list) {
1935 usecase = node_to_item(node, struct audio_usecase, list);
1936 if (switch_device[usecase->id]) {
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001937 if (platform_split_snd_device(adev->platform, usecase->out_snd_device,
1938 &num_devices, split_snd_devices) == 0) {
1939 /* Enable derived sound device only if it does not match with
1940 one of the split sound devices. This is because the matching
1941 sound device was not disabled */
1942 bool should_enable = true;
1943 for (i = 0; i < num_devices; i++) {
1944 if (derive_snd_device[usecase->id] == split_snd_devices[i]) {
1945 should_enable = false;
1946 break;
1947 }
1948 }
1949 if (should_enable)
1950 enable_snd_device(adev, derive_snd_device[usecase->id]);
1951 } else {
1952 enable_snd_device(adev, derive_snd_device[usecase->id]);
1953 }
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -07001954 }
1955 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001956
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001957 /* Re-route all the usecases on the shared backend other than the
1958 specified usecase to new snd devices */
1959 list_for_each(node, &adev->usecase_list) {
1960 usecase = node_to_item(node, struct audio_usecase, list);
Karthik Reddy Katta3abfee22016-02-23 10:55:27 +05301961 /* Update the out_snd_device only before enabling the audio route */
1962 if (switch_device[usecase->id]) {
Chaithanya Krishna Bacharaju49e7db02017-03-14 11:57:26 +05301963 usecase->out_snd_device = derive_snd_device[usecase->id];
Lakshman Chaluvarajuab7e5572020-10-28 19:29:23 +05301964 ALOGD("%s:becf: enabling usecase (%s) on (%s)", __func__,
1965 use_case_table[usecase->id],
1966 platform_get_snd_device_name(usecase->out_snd_device));
1967 /* Update voc calibration before enabling Voice/VoIP route */
1968 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL)
1969 status = platform_switch_voice_call_device_post(adev->platform,
1970 usecase->out_snd_device,
1971 platform_get_input_snd_device(
1972 adev->platform, NULL,
1973 &uc_info->device_list,
1974 usecase->type));
1975 enable_audio_route(adev, usecase);
1976 if (usecase->stream.out && usecase->id == USECASE_AUDIO_PLAYBACK_VOIP) {
1977 out_set_voip_volume(&usecase->stream.out->stream,
1978 usecase->stream.out->volume_l,
1979 usecase->stream.out->volume_r);
Karthik Reddy Katta3abfee22016-02-23 10:55:27 +05301980 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001981 }
1982 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001983 }
1984}
1985
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05301986static void check_usecases_capture_codec_backend(struct audio_device *adev,
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001987 struct audio_usecase *uc_info,
1988 snd_device_t snd_device)
1989{
1990 struct listnode *node;
1991 struct audio_usecase *usecase;
1992 bool switch_device[AUDIO_USECASE_MAX];
1993 int i, num_uc_to_switch = 0;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001994 int backend_check_cond = is_codec_backend_out_device_type(&uc_info->device_list);
kunleiz5cd52b82016-11-07 17:22:52 +08001995 int status = 0;
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001996
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05301997 bool force_routing = platform_check_and_set_capture_codec_backend_cfg(adev, uc_info,
1998 snd_device);
1999 ALOGD("%s:becf: force routing %d", __func__, force_routing);
Dhanalakshmi Siddanib678a802016-12-03 11:51:41 +05302000
2001 /*
2002 * Make sure out devices is checked against out codec backend device and
2003 * also in devices against in codec backend. Checking out device against in
2004 * codec backend or vice versa causes issues.
2005 */
2006 if (uc_info->type == PCM_CAPTURE)
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002007 backend_check_cond = is_codec_backend_in_device_type(&uc_info->device_list);
Kunlei Zhange7cab1a2020-05-17 20:31:05 +08002008
2009 /*
2010 * Island cfg and power mode config needs to set before AFE port start.
2011 * Set force routing in case of voice device was enable before.
2012 */
2013
2014 if (uc_info->type == VOICE_CALL &&
2015 voice_extn_is_voice_power_mode_supported() &&
Kunlei Zhang67cc7072020-12-18 17:16:49 +08002016 is_supported_conc_usecase_for_power_mode_call(adev) &&
Kunlei Zhange7cab1a2020-05-17 20:31:05 +08002017 platform_check_and_update_island_power_status(adev->platform,
2018 uc_info,
2019 snd_device)) {
2020 force_routing = true;
2021 ALOGD("%s:becf: force routing %d for power mode supported device",
2022 __func__, force_routing);
2023 }
2024
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002025 /*
2026 * This function is to make sure that all the active capture usecases
2027 * are always routed to the same input sound device.
2028 * For example, if audio-record and voice-call usecases are currently
2029 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
2030 * is received for voice call then we have to make sure that audio-record
2031 * usecase is also switched to earpiece i.e. voice-dmic-ef,
2032 * because of the limitation that two devices cannot be enabled
2033 * at the same time if they share the same backend.
2034 */
2035 for (i = 0; i < AUDIO_USECASE_MAX; i++)
2036 switch_device[i] = false;
2037
2038 list_for_each(node, &adev->usecase_list) {
2039 usecase = node_to_item(node, struct audio_usecase, list);
Dhanalakshmi Siddanib678a802016-12-03 11:51:41 +05302040 /*
2041 * TODO: Enhance below condition to handle BT sco/USB multi recording
2042 */
Jaideep Sharma477917f2020-03-13 18:13:33 +05302043
2044 bool capture_uc_needs_routing = usecase->type != PCM_PLAYBACK && (usecase != uc_info &&
2045 (usecase->in_snd_device != snd_device || force_routing));
2046 bool call_proxy_snd_device = platform_is_call_proxy_snd_device(snd_device) ||
2047 platform_is_call_proxy_snd_device(usecase->in_snd_device);
2048 if (capture_uc_needs_routing && !call_proxy_snd_device &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002049 ((backend_check_cond &&
2050 (is_codec_backend_in_device_type(&usecase->device_list) ||
Aniket Kumar Lata9c2fd892020-01-22 22:20:00 -08002051 (usecase->type == VOIP_CALL))) ||
Carter Hsu1d2a0532018-10-04 09:24:36 +08002052 ((uc_info->type == VOICE_CALL &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002053 is_single_device_type_equal(&usecase->device_list,
2054 AUDIO_DEVICE_IN_VOICE_CALL)) ||
Carter Hsu1d2a0532018-10-04 09:24:36 +08002055 platform_check_backends_match(snd_device,\
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002056 usecase->in_snd_device))) &&
Anish Kumara020a7c2014-10-17 11:13:22 -07002057 (usecase->id != USECASE_AUDIO_SPKR_CALIB_TX)) {
Jaideep Sharma477917f2020-03-13 18:13:33 +05302058 ALOGD("%s: Usecase (%s) is active on (%s) - disabling ..",
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002059 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -07002060 platform_get_snd_device_name(usecase->in_snd_device));
Haynes Mathew George1376ca62014-04-24 11:55:48 -07002061 disable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002062 switch_device[usecase->id] = true;
2063 num_uc_to_switch++;
2064 }
2065 }
2066
2067 if (num_uc_to_switch) {
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07002068 /* All streams have been de-routed. Disable the device */
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002069
Venkata Narendra Kumar Gutta7610e632014-04-14 23:16:38 +05302070 /* Make sure the previous devices to be disabled first and then enable the
2071 selected devices */
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002072 list_for_each(node, &adev->usecase_list) {
2073 usecase = node_to_item(node, struct audio_usecase, list);
2074 if (switch_device[usecase->id]) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07002075 disable_snd_device(adev, usecase->in_snd_device);
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -08002076 }
2077 }
2078
2079 list_for_each(node, &adev->usecase_list) {
2080 usecase = node_to_item(node, struct audio_usecase, list);
2081 if (switch_device[usecase->id]) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07002082 enable_snd_device(adev, snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002083 }
2084 }
2085
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002086 /* Re-route all the usecases on the shared backend other than the
2087 specified usecase to new snd devices */
2088 list_for_each(node, &adev->usecase_list) {
2089 usecase = node_to_item(node, struct audio_usecase, list);
2090 /* Update the in_snd_device only before enabling the audio route */
2091 if (switch_device[usecase->id] ) {
2092 usecase->in_snd_device = snd_device;
Lakshman Chaluvarajuab7e5572020-10-28 19:29:23 +05302093 /* Update voc calibration before enabling Voice/VoIP route */
2094 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) {
2095 snd_device_t voip_snd_device;
2096 voip_snd_device = platform_get_output_snd_device(adev->platform,
2097 usecase->stream.out,
2098 usecase->type);
2099 status = platform_switch_voice_call_device_post(adev->platform,
2100 voip_snd_device,
2101 usecase->in_snd_device);
kunleiz5cd52b82016-11-07 17:22:52 +08002102 }
Lakshman Chaluvarajuab7e5572020-10-28 19:29:23 +05302103 enable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002104 }
2105 }
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002106 }
2107}
2108
Mingming Yin3a941d42016-02-17 18:08:05 -08002109static void reset_hdmi_sink_caps(struct stream_out *out) {
2110 int i = 0;
2111
2112 for (i = 0; i<= MAX_SUPPORTED_CHANNEL_MASKS; i++) {
2113 out->supported_channel_masks[i] = 0;
2114 }
2115 for (i = 0; i<= MAX_SUPPORTED_FORMATS; i++) {
2116 out->supported_formats[i] = 0;
2117 }
2118 for (i = 0; i<= MAX_SUPPORTED_SAMPLE_RATES; i++) {
2119 out->supported_sample_rates[i] = 0;
2120 }
2121}
2122
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002123/* must be called with hw device mutex locked */
Mingming Yin3a941d42016-02-17 18:08:05 -08002124static int read_hdmi_sink_caps(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002125{
Mingming Yin3a941d42016-02-17 18:08:05 -08002126 int ret = 0, i = 0, j = 0;
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07002127 int channels = platform_edid_get_max_channels_v2(out->dev->platform,
2128 out->extconn.cs.controller,
2129 out->extconn.cs.stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002130
Mingming Yin3a941d42016-02-17 18:08:05 -08002131 reset_hdmi_sink_caps(out);
2132
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07002133 /* Cache ext disp type */
Vignesh Kulothungan1b3957e2019-12-02 16:16:51 -08002134 ret = platform_get_ext_disp_type_v2(adev->platform,
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07002135 out->extconn.cs.controller,
Vignesh Kulothungan1b3957e2019-12-02 16:16:51 -08002136 out->extconn.cs.stream);
2137 if(ret < 0) {
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07002138 ALOGE("%s: Failed to query disp type, ret:%d", __func__, ret);
Garmond Leung37850ab2016-10-06 11:42:18 -07002139 return -EINVAL;
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07002140 }
2141
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002142 switch (channels) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002143 case 8:
Mingming Yin3a941d42016-02-17 18:08:05 -08002144 ALOGV("%s: HDMI supports 7.1 channels", __func__);
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -07002145 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_7POINT1;
Mingming Yin3a941d42016-02-17 18:08:05 -08002146 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_6POINT1;
2147 case 6:
2148 ALOGV("%s: HDMI supports 5.1 channels", __func__);
2149 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_5POINT1;
2150 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_PENTA;
2151 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_QUAD;
2152 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_SURROUND;
2153 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_2POINT1;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002154 break;
2155 default:
Mingming Yin3a941d42016-02-17 18:08:05 -08002156 ALOGE("invalid/nonstandard channal count[%d]",channels);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002157 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002158 break;
2159 }
Mingming Yin3a941d42016-02-17 18:08:05 -08002160
2161 // check channel format caps
2162 i = 0;
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07002163 if (platform_is_edid_supported_format_v2(out->dev->platform, AUDIO_FORMAT_AC3,
2164 out->extconn.cs.controller,
2165 out->extconn.cs.stream)) {
Mingming Yin3a941d42016-02-17 18:08:05 -08002166 ALOGV(":%s HDMI supports AC3/EAC3 formats", __func__);
2167 out->supported_formats[i++] = AUDIO_FORMAT_AC3;
2168 //Adding EAC3/EAC3_JOC formats if AC3 is supported by the sink.
2169 //EAC3/EAC3_JOC will be converted to AC3 for decoding if needed
2170 out->supported_formats[i++] = AUDIO_FORMAT_E_AC3;
2171 out->supported_formats[i++] = AUDIO_FORMAT_E_AC3_JOC;
2172 }
2173
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07002174 if (platform_is_edid_supported_format_v2(out->dev->platform, AUDIO_FORMAT_DOLBY_TRUEHD,
2175 out->extconn.cs.controller,
2176 out->extconn.cs.stream)) {
Ben Romberger1aaaf862017-04-06 17:49:46 -07002177 ALOGV(":%s HDMI supports TRUE HD format", __func__);
2178 out->supported_formats[i++] = AUDIO_FORMAT_DOLBY_TRUEHD;
2179 }
2180
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07002181 if (platform_is_edid_supported_format_v2(out->dev->platform, AUDIO_FORMAT_DTS,
2182 out->extconn.cs.controller,
2183 out->extconn.cs.stream)) {
Mingming Yin3a941d42016-02-17 18:08:05 -08002184 ALOGV(":%s HDMI supports DTS format", __func__);
2185 out->supported_formats[i++] = AUDIO_FORMAT_DTS;
2186 }
2187
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07002188 if (platform_is_edid_supported_format_v2(out->dev->platform, AUDIO_FORMAT_DTS_HD,
2189 out->extconn.cs.controller,
2190 out->extconn.cs.stream)) {
Mingming Yin3a941d42016-02-17 18:08:05 -08002191 ALOGV(":%s HDMI supports DTS HD format", __func__);
2192 out->supported_formats[i++] = AUDIO_FORMAT_DTS_HD;
2193 }
2194
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07002195 if (platform_is_edid_supported_format_v2(out->dev->platform, AUDIO_FORMAT_IEC61937,
2196 out->extconn.cs.controller,
2197 out->extconn.cs.stream)) {
Naresh Tanniru928f0862017-04-07 16:44:23 -07002198 ALOGV(":%s HDMI supports IEC61937 format", __func__);
2199 out->supported_formats[i++] = AUDIO_FORMAT_IEC61937;
2200 }
2201
Mingming Yin3a941d42016-02-17 18:08:05 -08002202
2203 // check sample rate caps
2204 i = 0;
2205 for (j = 0; j < MAX_SUPPORTED_SAMPLE_RATES; j++) {
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07002206 if (platform_is_edid_supported_sample_rate_v2(out->dev->platform, out_hdmi_sample_rates[j],
2207 out->extconn.cs.controller,
2208 out->extconn.cs.stream)) {
Mingming Yin3a941d42016-02-17 18:08:05 -08002209 ALOGV(":%s HDMI supports sample rate:%d", __func__, out_hdmi_sample_rates[j]);
2210 out->supported_sample_rates[i++] = out_hdmi_sample_rates[j];
2211 }
2212 }
2213
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002214 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002215}
2216
Haynes Mathew George484e8d22017-07-31 18:55:17 -07002217static inline ssize_t read_usb_sup_sample_rates(bool is_playback __unused,
2218 uint32_t *supported_sample_rates __unused,
2219 uint32_t max_rates __unused)
2220{
2221 ssize_t count = audio_extn_usb_get_sup_sample_rates(is_playback,
2222 supported_sample_rates,
2223 max_rates);
Ashish Jain4847e9d2017-08-17 19:16:57 +05302224 ssize_t i = 0;
2225
2226 for (i=0; i<count; i++) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07002227 ALOGV("%s %s %d", __func__, is_playback ? "P" : "C",
2228 supported_sample_rates[i]);
2229 }
2230 return count;
2231}
2232
2233static inline int read_usb_sup_channel_masks(bool is_playback,
2234 audio_channel_mask_t *supported_channel_masks,
2235 uint32_t max_masks)
2236{
2237 int channels = audio_extn_usb_get_max_channels(is_playback);
2238 int channel_count;
2239 uint32_t num_masks = 0;
2240 if (channels > MAX_HIFI_CHANNEL_COUNT)
2241 channels = MAX_HIFI_CHANNEL_COUNT;
2242
2243 if (is_playback) {
Eric Laurent68a87112019-05-01 18:07:29 -07002244 // start from 2 channels as framework currently doesn't support mono.
2245 if (channels >= FCC_2) {
2246 supported_channel_masks[num_masks++] = audio_channel_out_mask_from_count(FCC_2);
2247 }
2248 for (channel_count = FCC_2;
2249 channel_count <= channels && num_masks < max_masks;
2250 ++channel_count) {
2251 supported_channel_masks[num_masks++] =
2252 audio_channel_mask_for_index_assignment_from_count(channel_count);
2253 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07002254 } else {
vincenttewf51c94e2019-05-07 10:28:53 +08002255 // For capture we report all supported channel masks from 1 channel up.
2256 channel_count = MIN_CHANNEL_COUNT;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07002257 // audio_channel_in_mask_from_count() does the right conversion to either positional or
2258 // indexed mask
Eric Laurent68a87112019-05-01 18:07:29 -07002259 for ( ; channel_count <= channels && num_masks < max_masks; channel_count++) {
2260 audio_channel_mask_t mask = AUDIO_CHANNEL_NONE;
2261 if (channel_count <= FCC_2) {
2262 mask = audio_channel_in_mask_from_count(channel_count);
2263 supported_channel_masks[num_masks++] = mask;
2264 }
2265 const audio_channel_mask_t index_mask =
2266 audio_channel_mask_for_index_assignment_from_count(channel_count);
2267 if (mask != index_mask && num_masks < max_masks) { // ensure index mask added.
2268 supported_channel_masks[num_masks++] = index_mask;
2269 }
2270 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07002271 }
Lakshman Chaluvarajub79fafd2018-11-21 10:24:37 +05302272
vincenttewf51c94e2019-05-07 10:28:53 +08002273 for (size_t i = 0; i < num_masks; ++i) {
2274 ALOGV("%s: %s supported ch %d supported_channel_masks[%zu] %08x num_masks %d", __func__,
2275 is_playback ? "P" : "C", channels, i, supported_channel_masks[i], num_masks);
Lakshman Chaluvarajub79fafd2018-11-21 10:24:37 +05302276 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07002277 return num_masks;
2278}
2279
2280static inline int read_usb_sup_formats(bool is_playback __unused,
2281 audio_format_t *supported_formats,
2282 uint32_t max_formats __unused)
2283{
2284 int bitwidth = audio_extn_usb_get_max_bit_width(is_playback);
2285 switch (bitwidth) {
2286 case 24:
2287 // XXX : usb.c returns 24 for s24 and s24_le?
2288 supported_formats[0] = AUDIO_FORMAT_PCM_24_BIT_PACKED;
2289 break;
2290 case 32:
2291 supported_formats[0] = AUDIO_FORMAT_PCM_32_BIT;
2292 break;
2293 case 16:
2294 default :
2295 supported_formats[0] = AUDIO_FORMAT_PCM_16_BIT;
2296 break;
2297 }
2298 ALOGV("%s: %s supported format %d", __func__,
2299 is_playback ? "P" : "C", bitwidth);
2300 return 1;
2301}
2302
2303static inline int read_usb_sup_params_and_compare(bool is_playback,
2304 audio_format_t *format,
2305 audio_format_t *supported_formats,
2306 uint32_t max_formats,
2307 audio_channel_mask_t *mask,
2308 audio_channel_mask_t *supported_channel_masks,
2309 uint32_t max_masks,
2310 uint32_t *rate,
2311 uint32_t *supported_sample_rates,
2312 uint32_t max_rates) {
2313 int ret = 0;
2314 int num_formats;
2315 int num_masks;
2316 int num_rates;
2317 int i;
2318
2319 num_formats = read_usb_sup_formats(is_playback, supported_formats,
2320 max_formats);
2321 num_masks = read_usb_sup_channel_masks(is_playback, supported_channel_masks,
2322 max_masks);
2323
2324 num_rates = read_usb_sup_sample_rates(is_playback,
2325 supported_sample_rates, max_rates);
2326
2327#define LUT(table, len, what, dflt) \
2328 for (i=0; i<len && (table[i] != what); i++); \
2329 if (i==len) { ret |= (what == dflt ? 0 : -1); what=table[0]; }
2330
2331 LUT(supported_formats, num_formats, *format, AUDIO_FORMAT_DEFAULT);
2332 LUT(supported_channel_masks, num_masks, *mask, AUDIO_CHANNEL_NONE);
2333 LUT(supported_sample_rates, num_rates, *rate, 0);
2334
2335#undef LUT
2336 return ret < 0 ? -EINVAL : 0; // HACK TBD
2337}
2338
Alexy Josephb1379942016-01-29 15:49:38 -08002339audio_usecase_t get_usecase_id_from_usecase_type(const struct audio_device *adev,
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -08002340 usecase_type_t type)
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002341{
2342 struct audio_usecase *usecase;
2343 struct listnode *node;
2344
2345 list_for_each(node, &adev->usecase_list) {
2346 usecase = node_to_item(node, struct audio_usecase, list);
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -08002347 if (usecase->type == type) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002348 ALOGV("%s: usecase id %d", __func__, usecase->id);
2349 return usecase->id;
2350 }
2351 }
2352 return USECASE_INVALID;
2353}
2354
Alexy Josephb1379942016-01-29 15:49:38 -08002355struct audio_usecase *get_usecase_from_list(const struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002356 audio_usecase_t uc_id)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002357{
2358 struct audio_usecase *usecase;
2359 struct listnode *node;
2360
2361 list_for_each(node, &adev->usecase_list) {
2362 usecase = node_to_item(node, struct audio_usecase, list);
2363 if (usecase->id == uc_id)
2364 return usecase;
2365 }
2366 return NULL;
2367}
2368
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302369/*
2370 * is a true native playback active
2371 */
2372bool audio_is_true_native_stream_active(struct audio_device *adev)
2373{
2374 bool active = false;
2375 int i = 0;
2376 struct listnode *node;
2377
2378 if (NATIVE_AUDIO_MODE_TRUE_44_1 != platform_get_native_support()) {
2379 ALOGV("%s:napb: not in true mode or non hdphones device",
2380 __func__);
2381 active = false;
2382 goto exit;
2383 }
2384
2385 list_for_each(node, &adev->usecase_list) {
2386 struct audio_usecase *uc;
2387 uc = node_to_item(node, struct audio_usecase, list);
2388 struct stream_out *curr_out =
2389 (struct stream_out*) uc->stream.out;
2390
2391 if (curr_out && PCM_PLAYBACK == uc->type) {
2392 ALOGD("%s:napb: (%d) (%s)id (%d) sr %d bw "
2393 "(%d) device %s", __func__, i++, use_case_table[uc->id],
2394 uc->id, curr_out->sample_rate,
2395 curr_out->bit_width,
2396 platform_get_snd_device_name(uc->out_snd_device));
2397
2398 if (is_offload_usecase(uc->id) &&
2399 (curr_out->sample_rate == OUTPUT_SAMPLING_RATE_44100)) {
2400 active = true;
2401 ALOGD("%s:napb:native stream detected", __func__);
2402 }
2403 }
2404 }
2405exit:
2406 return active;
2407}
2408
Xiaojun Sang785b5da2017-08-03 15:52:29 +08002409uint32_t adev_get_dsp_bit_width_enforce_mode()
2410{
2411 if (adev == NULL) {
2412 ALOGE("%s: adev is null. Disable DSP bit width enforce mode.\n", __func__);
2413 return 0;
2414 }
2415 return adev->dsp_bit_width_enforce_mode;
2416}
2417
2418static uint32_t adev_init_dsp_bit_width_enforce_mode(struct mixer *mixer)
2419{
2420 char value[PROPERTY_VALUE_MAX];
2421 int trial;
2422 uint32_t dsp_bit_width_enforce_mode = 0;
2423
2424 if (!mixer) {
2425 ALOGE("%s: adev mixer is null. cannot update DSP bitwidth.\n",
2426 __func__);
2427 return 0;
2428 }
2429
2430 if (property_get("persist.vendor.audio_hal.dsp_bit_width_enforce_mode",
2431 value, NULL) > 0) {
2432 trial = atoi(value);
2433 switch (trial) {
2434 case 16:
2435 dsp_bit_width_enforce_mode = 16;
2436 break;
2437 case 24:
2438 dsp_bit_width_enforce_mode = 24;
2439 break;
2440 case 32:
2441 dsp_bit_width_enforce_mode = 32;
2442 break;
2443 default:
2444 dsp_bit_width_enforce_mode = 0;
2445 ALOGD("%s Dynamic DSP bitwidth config is disabled.", __func__);
2446 break;
2447 }
2448 }
2449
2450 return dsp_bit_width_enforce_mode;
2451}
2452
2453static void audio_enable_asm_bit_width_enforce_mode(struct mixer *mixer,
2454 uint32_t enforce_mode,
2455 bool enable)
2456{
2457 struct mixer_ctl *ctl = NULL;
2458 const char *mixer_ctl_name = "ASM Bit Width";
2459 uint32_t asm_bit_width_mode = 0;
2460
2461 if (enforce_mode == 0) {
2462 ALOGD("%s: DSP bitwidth feature is disabled.", __func__);
2463 return;
2464 }
2465
2466 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
2467 if (!ctl) {
2468 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2469 __func__, mixer_ctl_name);
2470 return;
2471 }
2472
2473 if (enable)
2474 asm_bit_width_mode = enforce_mode;
2475 else
2476 asm_bit_width_mode = 0;
2477
2478 ALOGV("%s DSP bit width feature status is %d width=%d",
2479 __func__, enable, asm_bit_width_mode);
2480 if (mixer_ctl_set_value(ctl, 0, asm_bit_width_mode) < 0)
2481 ALOGE("%s: Could not set ASM biwidth %d", __func__,
2482 asm_bit_width_mode);
2483
2484 return;
2485}
2486
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05302487/*
2488 * if native DSD playback active
2489 */
2490bool audio_is_dsd_native_stream_active(struct audio_device *adev)
2491{
2492 bool active = false;
2493 struct listnode *node = NULL;
2494 struct audio_usecase *uc = NULL;
2495 struct stream_out *curr_out = NULL;
2496
2497 list_for_each(node, &adev->usecase_list) {
2498 uc = node_to_item(node, struct audio_usecase, list);
2499 curr_out = (struct stream_out*) uc->stream.out;
2500
2501 if (curr_out && PCM_PLAYBACK == uc->type &&
2502 (DSD_NATIVE_BACKEND == platform_get_backend_index(uc->out_snd_device))) {
2503 active = true;
2504 ALOGV("%s:DSD playback is active", __func__);
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05302505 break;
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05302506 }
2507 }
2508 return active;
2509}
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302510
2511static bool force_device_switch(struct audio_usecase *usecase)
2512{
2513 bool ret = false;
2514 bool is_it_true_mode = false;
2515
Zhou Song30f2c3e2018-02-08 14:02:15 +08002516 if (usecase->type == PCM_CAPTURE ||
Surendar Karka93cd25a2018-08-28 14:21:37 +05302517 usecase->type == TRANSCODE_LOOPBACK_RX ||
2518 usecase->type == TRANSCODE_LOOPBACK_TX) {
Zhou Song30f2c3e2018-02-08 14:02:15 +08002519 return false;
2520 }
2521
Aalique Grahamecbc46a22017-10-05 10:30:23 -07002522 if(usecase->stream.out == NULL) {
2523 ALOGE("%s: stream.out is NULL", __func__);
2524 return false;
2525 }
2526
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302527 if (is_offload_usecase(usecase->id) &&
Xiaojun Sang869f2012016-02-23 16:33:07 +08002528 (usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002529 (compare_device_type(&usecase->stream.out->device_list, AUDIO_DEVICE_OUT_WIRED_HEADSET) ||
2530 compare_device_type(&usecase->stream.out->device_list, AUDIO_DEVICE_OUT_WIRED_HEADPHONE))) {
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302531 is_it_true_mode = (NATIVE_AUDIO_MODE_TRUE_44_1 == platform_get_native_support()? true : false);
2532 if ((is_it_true_mode && !adev->native_playback_enabled) ||
2533 (!is_it_true_mode && adev->native_playback_enabled)){
2534 ret = true;
2535 ALOGD("napb: time to toggle native mode");
2536 }
2537 }
2538
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302539 // Force all a2dp output devices to reconfigure for proper AFE encode format
Ashish Jainc597d102016-12-12 10:31:34 +05302540 //Also handle a case where in earlier a2dp start failed as A2DP stream was
2541 //in suspended state, hence try to trigger a retry when we again get a routing request.
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002542 if(is_a2dp_out_device_type(&usecase->stream.out->device_list) &&
Ashish Jainc597d102016-12-12 10:31:34 +05302543 audio_extn_a2dp_is_force_device_switch()) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302544 ALOGD("Force a2dp device switch to update new encoder config");
2545 ret = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002546 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302547
Florian Pfister1a84f312018-07-19 14:38:18 +02002548 if (usecase->stream.out->stream_config_changed) {
Manish Dewangan671a4202017-08-18 17:30:46 +05302549 ALOGD("Force stream_config_changed to update iec61937 transmission config");
2550 return true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002551 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302552 return ret;
2553}
2554
Aalique Grahame22e49102018-12-18 14:23:57 -08002555static void stream_app_type_cfg_init(struct stream_app_type_cfg *cfg)
2556{
2557 cfg->gain[0] = cfg->gain[1] = APP_TYPE_GAIN_DEFAULT;
2558}
2559
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302560bool is_btsco_device(snd_device_t out_snd_device, snd_device_t in_snd_device)
2561{
2562 bool ret=false;
2563 if ((out_snd_device == SND_DEVICE_OUT_BT_SCO ||
Zhou Song5657f492019-08-07 11:30:39 +08002564 out_snd_device == SND_DEVICE_OUT_BT_SCO_WB ||
2565 out_snd_device == SND_DEVICE_OUT_BT_SCO_SWB) ||
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302566 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_WB_NREC ||
2567 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_WB ||
Zhou Song5657f492019-08-07 11:30:39 +08002568 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_SWB ||
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302569 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_NREC ||
Mingshu Pang16093502020-04-20 11:21:16 +08002570 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC ||
2571 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_SWB_NREC)
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302572 ret = true;
2573
2574 return ret;
2575}
2576
2577bool is_a2dp_device(snd_device_t out_snd_device)
2578{
2579 bool ret=false;
2580 if (out_snd_device == SND_DEVICE_OUT_BT_A2DP)
2581 ret = true;
2582
2583 return ret;
2584}
2585
2586bool is_bt_soc_on(struct audio_device *adev)
2587{
2588 struct mixer_ctl *ctl;
2589 char *mixer_ctl_name = "BT SOC status";
2590 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
2591 bool bt_soc_status = true;
2592 if (!ctl) {
2593 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2594 __func__, mixer_ctl_name);
2595 /*This is to ensure we dont break targets which dont have the kernel change*/
2596 return true;
2597 }
2598 bt_soc_status = mixer_ctl_get_value(ctl, 0);
2599 ALOGD("BT SOC status: %d",bt_soc_status);
2600 return bt_soc_status;
2601}
2602
Zhou Song331c8e52019-08-26 14:16:12 +08002603static int configure_btsco_sample_rate(snd_device_t snd_device)
2604{
2605 struct mixer_ctl *ctl = NULL;
2606 struct mixer_ctl *ctl_sr_rx = NULL, *ctl_sr_tx = NULL, *ctl_sr = NULL;
2607 char *rate_str = NULL;
2608 bool is_rx_dev = true;
2609
2610 if (is_btsco_device(snd_device, snd_device)) {
2611 ctl_sr_tx = mixer_get_ctl_by_name(adev->mixer, "BT SampleRate TX");
2612 ctl_sr_rx = mixer_get_ctl_by_name(adev->mixer, "BT SampleRate RX");
2613 if (!ctl_sr_tx || !ctl_sr_rx) {
2614 ctl_sr = mixer_get_ctl_by_name(adev->mixer, "BT SampleRate");
2615 if (!ctl_sr)
2616 return -ENOSYS;
2617 }
2618
2619 switch (snd_device) {
2620 case SND_DEVICE_OUT_BT_SCO:
2621 rate_str = "KHZ_8";
2622 break;
2623 case SND_DEVICE_IN_BT_SCO_MIC_NREC:
2624 case SND_DEVICE_IN_BT_SCO_MIC:
2625 rate_str = "KHZ_8";
2626 is_rx_dev = false;
2627 break;
2628 case SND_DEVICE_OUT_BT_SCO_WB:
2629 rate_str = "KHZ_16";
2630 break;
2631 case SND_DEVICE_IN_BT_SCO_MIC_WB_NREC:
2632 case SND_DEVICE_IN_BT_SCO_MIC_WB:
2633 rate_str = "KHZ_16";
2634 is_rx_dev = false;
2635 break;
2636 default:
2637 return 0;
2638 }
2639
2640 ctl = (ctl_sr == NULL) ? (is_rx_dev ? ctl_sr_rx : ctl_sr_tx) : ctl_sr;
2641 if (mixer_ctl_set_enum_by_string(ctl, rate_str) != 0)
2642 return -ENOSYS;
2643 }
2644 return 0;
2645}
2646
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302647int out_standby_l(struct audio_stream *stream);
2648
Eric Laurent637e2d42018-11-15 12:24:31 -08002649struct stream_in *adev_get_active_input(const struct audio_device *adev)
2650{
2651 struct listnode *node;
2652 struct stream_in *last_active_in = NULL;
2653
2654 /* Get last added active input.
2655 * TODO: We may use a priority mechanism to pick highest priority active source */
2656 list_for_each(node, &adev->usecase_list)
2657 {
2658 struct audio_usecase *usecase = node_to_item(node, struct audio_usecase, list);
2659 if (usecase->type == PCM_CAPTURE && usecase->stream.in != NULL)
2660 last_active_in = usecase->stream.in;
2661 }
2662
2663 return last_active_in;
2664}
2665
2666struct stream_in *get_voice_communication_input(const struct audio_device *adev)
2667{
2668 struct listnode *node;
2669
2670 /* First check active inputs with voice communication source and then
2671 * any input if audio mode is in communication */
2672 list_for_each(node, &adev->usecase_list)
2673 {
2674 struct audio_usecase *usecase = node_to_item(node, struct audio_usecase, list);
2675 if (usecase->type == PCM_CAPTURE && usecase->stream.in != NULL &&
2676 usecase->stream.in->source == AUDIO_SOURCE_VOICE_COMMUNICATION)
2677 return usecase->stream.in;
2678 }
2679 if (adev->mode == AUDIO_MODE_IN_COMMUNICATION)
2680 return adev_get_active_input(adev);
2681
2682 return NULL;
2683}
2684
Carter Hsu2e429db2019-05-14 18:50:52 +08002685/*
2686 * Aligned with policy.h
2687 */
2688static inline int source_priority(int inputSource)
2689{
2690 switch (inputSource) {
2691 case AUDIO_SOURCE_VOICE_COMMUNICATION:
2692 return 9;
2693 case AUDIO_SOURCE_CAMCORDER:
2694 return 8;
2695 case AUDIO_SOURCE_VOICE_PERFORMANCE:
2696 return 7;
2697 case AUDIO_SOURCE_UNPROCESSED:
2698 return 6;
2699 case AUDIO_SOURCE_MIC:
2700 return 5;
2701 case AUDIO_SOURCE_ECHO_REFERENCE:
2702 return 4;
2703 case AUDIO_SOURCE_FM_TUNER:
2704 return 3;
2705 case AUDIO_SOURCE_VOICE_RECOGNITION:
2706 return 2;
2707 case AUDIO_SOURCE_HOTWORD:
2708 return 1;
2709 default:
2710 break;
2711 }
2712 return 0;
2713}
2714
2715static struct stream_in *get_priority_input(struct audio_device *adev)
2716{
2717 struct listnode *node;
2718 struct audio_usecase *usecase;
2719 int last_priority = 0, priority;
2720 struct stream_in *priority_in = NULL;
2721 struct stream_in *in;
2722
2723 list_for_each(node, &adev->usecase_list) {
2724 usecase = node_to_item(node, struct audio_usecase, list);
2725 if (usecase->type == PCM_CAPTURE) {
2726 in = usecase->stream.in;
2727 if (!in)
2728 continue;
2729 priority = source_priority(in->source);
2730
2731 if (priority > last_priority) {
2732 last_priority = priority;
2733 priority_in = in;
2734 }
2735 }
2736 }
2737 return priority_in;
2738}
2739
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002740int select_devices(struct audio_device *adev, audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002741{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002742 snd_device_t out_snd_device = SND_DEVICE_NONE;
2743 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002744 struct audio_usecase *usecase = NULL;
2745 struct audio_usecase *vc_usecase = NULL;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002746 struct audio_usecase *voip_usecase = NULL;
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -08002747 struct audio_usecase *hfp_usecase = NULL;
Siddartha Shaik44dd7702017-06-14 12:13:25 +05302748 struct stream_out stream_out;
Vimal Puthanveed41fcff22014-01-23 15:56:53 -08002749 audio_usecase_t hfp_ucid;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002750 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002751
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302752 ALOGD("%s for use case (%s)", __func__, use_case_table[uc_id]);
2753
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002754 usecase = get_usecase_from_list(adev, uc_id);
2755 if (usecase == NULL) {
2756 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
2757 return -EINVAL;
2758 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002759
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002760 if ((usecase->type == VOICE_CALL) ||
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -08002761 (usecase->type == VOIP_CALL) ||
Derek Chena30a5f42019-12-03 11:17:09 -05002762 (usecase->type == PCM_HFP_CALL)||
Fei Tongaffdf732020-02-20 20:39:05 +08002763 (usecase->type == ICC_CALL) ||
2764 (usecase->type == SYNTH_LOOPBACK)) {
Aditya Bavanaribdda2f22016-10-19 15:02:05 +05302765 if(usecase->stream.out == NULL) {
2766 ALOGE("%s: stream.out is NULL", __func__);
2767 return -EINVAL;
2768 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002769 if (compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_BUS)) {
Guodong Hu267bdf82019-08-12 19:22:32 +08002770 out_snd_device = audio_extn_auto_hal_get_output_snd_device(adev,
2771 uc_id);
2772 in_snd_device = audio_extn_auto_hal_get_input_snd_device(adev,
2773 uc_id);
2774 } else {
2775 out_snd_device = platform_get_output_snd_device(adev->platform,
Jaideep Sharma477917f2020-03-13 18:13:33 +05302776 usecase->stream.out, usecase->type);
Guodong Hu267bdf82019-08-12 19:22:32 +08002777 in_snd_device = platform_get_input_snd_device(adev->platform,
2778 NULL,
Jaideep Sharma477917f2020-03-13 18:13:33 +05302779 &usecase->stream.out->device_list,
2780 usecase->type);
Guodong Hu267bdf82019-08-12 19:22:32 +08002781 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002782 assign_devices(&usecase->device_list, &usecase->stream.out->device_list);
Surendar Karka93cd25a2018-08-28 14:21:37 +05302783 } else if (usecase->type == TRANSCODE_LOOPBACK_RX) {
Siddartha Shaik31b530e2017-05-19 15:26:33 +05302784 if (usecase->stream.inout == NULL) {
2785 ALOGE("%s: stream.inout is NULL", __func__);
2786 return -EINVAL;
2787 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002788 assign_devices(&stream_out.device_list, &usecase->stream.inout->out_config.device_list);
Siddartha Shaik44dd7702017-06-14 12:13:25 +05302789 stream_out.sample_rate = usecase->stream.inout->out_config.sample_rate;
2790 stream_out.format = usecase->stream.inout->out_config.format;
2791 stream_out.channel_mask = usecase->stream.inout->out_config.channel_mask;
Jaideep Sharma477917f2020-03-13 18:13:33 +05302792 out_snd_device = platform_get_output_snd_device(adev->platform, &stream_out, usecase->type);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002793 assign_devices(&usecase->device_list,
2794 &usecase->stream.inout->out_config.device_list);
Surendar Karka93cd25a2018-08-28 14:21:37 +05302795 } else if (usecase->type == TRANSCODE_LOOPBACK_TX ) {
2796 if (usecase->stream.inout == NULL) {
2797 ALOGE("%s: stream.inout is NULL", __func__);
2798 return -EINVAL;
2799 }
Manisha Agarwal03297972020-04-17 15:36:55 +05302800 struct listnode out_devices;
2801 list_init(&out_devices);
2802 in_snd_device = platform_get_input_snd_device(adev->platform, NULL,
2803 &out_devices, usecase->type);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002804 assign_devices(&usecase->device_list,
2805 &usecase->stream.inout->in_config.device_list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002806 } else {
2807 /*
2808 * If the voice call is active, use the sound devices of voice call usecase
2809 * so that it would not result any device switch. All the usecases will
2810 * be switched to new device when select_devices() is called for voice call
2811 * usecase. This is to avoid switching devices for voice call when
2812 * check_usecases_codec_backend() is called below.
Alexy Joseph79dfa3c2016-04-20 18:44:56 -07002813 * choose voice call device only if the use case device is
2814 * also using the codec backend
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002815 */
Shiv Maliyappanahallibb4cf0b2016-01-21 11:30:06 -08002816 if (voice_is_in_call(adev) && adev->mode != AUDIO_MODE_NORMAL) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002817 vc_usecase = get_usecase_from_list(adev,
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -08002818 get_usecase_id_from_usecase_type(adev, VOICE_CALL));
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002819 if ((vc_usecase) && ((is_codec_backend_out_device_type(&vc_usecase->device_list) &&
2820 is_codec_backend_out_device_type(&usecase->device_list)) ||
2821 (is_codec_backend_out_device_type(&vc_usecase->device_list) &&
2822 is_codec_backend_in_device_type(&usecase->device_list)) ||
2823 is_single_device_type_equal(&vc_usecase->device_list,
2824 AUDIO_DEVICE_OUT_HEARING_AID) ||
2825 is_single_device_type_equal(&usecase->device_list,
Robert Lee8a6aaf32019-09-20 16:40:19 +08002826 AUDIO_DEVICE_IN_VOICE_CALL) ||
2827 (is_single_device_type_equal(&usecase->device_list,
Gautam Manam8fa06162021-09-24 10:41:29 +05302828 AUDIO_DEVICE_IN_BUILTIN_MIC) &&
2829 is_single_device_type_equal(&vc_usecase->device_list,
2830 AUDIO_DEVICE_OUT_USB_HEADSET)) ||
2831 (is_single_device_type_equal(&usecase->device_list,
Robert Lee8a6aaf32019-09-20 16:40:19 +08002832 AUDIO_DEVICE_IN_USB_HEADSET) &&
2833 is_single_device_type_equal(&vc_usecase->device_list,
Kamalakar Yalasiri5edba852021-01-28 14:52:38 +05302834 AUDIO_DEVICE_OUT_USB_HEADSET))||
Gautam Manamf4002142021-09-13 22:05:56 +05302835 (is_single_device_type_equal(&usecase->device_list,
2836 AUDIO_DEVICE_IN_USB_HEADSET) &&
2837 is_codec_backend_out_device_type(&vc_usecase->device_list)) ||
Kamalakar Yalasiri5edba852021-01-28 14:52:38 +05302838 (is_single_device_type_equal(&usecase->device_list,
2839 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) &&
2840 is_codec_backend_out_device_type(&vc_usecase->device_list)))) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002841 in_snd_device = vc_usecase->in_snd_device;
2842 out_snd_device = vc_usecase->out_snd_device;
2843 }
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002844 } else if (voice_extn_compress_voip_is_active(adev)) {
yidongh02ef86f2017-04-21 15:36:04 +08002845 bool out_snd_device_backend_match = true;
yidongh47785a82017-05-08 19:29:29 +08002846 voip_usecase = get_usecase_from_list(adev, USECASE_COMPRESS_VOIP_CALL);
yidongh6261d8e2017-05-15 17:04:02 +08002847 if ((voip_usecase != NULL) &&
2848 (usecase->type == PCM_PLAYBACK) &&
2849 (usecase->stream.out != NULL)) {
yidongh02ef86f2017-04-21 15:36:04 +08002850 out_snd_device_backend_match = platform_check_backends_match(
2851 voip_usecase->out_snd_device,
2852 platform_get_output_snd_device(
2853 adev->platform,
Jaideep Sharma477917f2020-03-13 18:13:33 +05302854 usecase->stream.out, usecase->type));
yidongh02ef86f2017-04-21 15:36:04 +08002855 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002856 if ((voip_usecase) && (is_codec_backend_out_device_type(&voip_usecase->device_list) &&
2857 (is_codec_backend_out_device_type(&usecase->device_list) ||
2858 is_codec_backend_in_device_type(&usecase->device_list)) &&
yidongh02ef86f2017-04-21 15:36:04 +08002859 out_snd_device_backend_match &&
Mingming Yin2d8aa2e2014-08-14 00:00:51 -07002860 (voip_usecase->stream.out != adev->primary_output))) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002861 in_snd_device = voip_usecase->in_snd_device;
2862 out_snd_device = voip_usecase->out_snd_device;
2863 }
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -08002864 } else if (audio_extn_hfp_is_active(adev)) {
Vimal Puthanveed41fcff22014-01-23 15:56:53 -08002865 hfp_ucid = audio_extn_hfp_get_usecase();
2866 hfp_usecase = get_usecase_from_list(adev, hfp_ucid);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002867 if ((hfp_usecase) && is_codec_backend_out_device_type(&hfp_usecase->device_list)) {
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -08002868 in_snd_device = hfp_usecase->in_snd_device;
2869 out_snd_device = hfp_usecase->out_snd_device;
2870 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002871 }
2872 if (usecase->type == PCM_PLAYBACK) {
Aditya Bavanaribdda2f22016-10-19 15:02:05 +05302873 if (usecase->stream.out == NULL) {
2874 ALOGE("%s: stream.out is NULL", __func__);
2875 return -EINVAL;
2876 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002877 assign_devices(&usecase->device_list, &usecase->stream.out->device_list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002878 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07002879 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurent637e2d42018-11-15 12:24:31 -08002880 struct stream_out *voip_out = adev->primary_output;
2881 struct stream_in *voip_in = get_voice_communication_input(adev);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002882 if (compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_BUS))
Guodong Hu267bdf82019-08-12 19:22:32 +08002883 out_snd_device = audio_extn_auto_hal_get_output_snd_device(adev, uc_id);
2884 else
2885 out_snd_device = platform_get_output_snd_device(adev->platform,
Jaideep Sharma477917f2020-03-13 18:13:33 +05302886 usecase->stream.out,
2887 usecase->type);
kunleizdcf967a2018-08-07 17:09:11 +08002888 voip_usecase = get_usecase_from_list(adev, USECASE_AUDIO_PLAYBACK_VOIP);
kunleizdcf967a2018-08-07 17:09:11 +08002889
Eric Laurent637e2d42018-11-15 12:24:31 -08002890 if (voip_usecase)
2891 voip_out = voip_usecase->stream.out;
2892
2893 if (usecase->stream.out == voip_out && voip_in != NULL)
2894 select_devices(adev, voip_in->usecase);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07002895 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002896 } else if (usecase->type == PCM_CAPTURE) {
Aditya Bavanaribdda2f22016-10-19 15:02:05 +05302897 if (usecase->stream.in == NULL) {
2898 ALOGE("%s: stream.in is NULL", __func__);
2899 return -EINVAL;
2900 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002901 assign_devices(&usecase->device_list, &usecase->stream.in->device_list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002902 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07002903 if (in_snd_device == SND_DEVICE_NONE) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002904 struct listnode out_devices;
Eric Laurent637e2d42018-11-15 12:24:31 -08002905 struct stream_in *voip_in = get_voice_communication_input(adev);
Carter Hsu2e429db2019-05-14 18:50:52 +08002906 struct stream_in *priority_in = NULL;
Eric Laurent637e2d42018-11-15 12:24:31 -08002907
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002908 list_init(&out_devices);
Eric Laurent637e2d42018-11-15 12:24:31 -08002909 if (voip_in != NULL) {
Eric Laurent637e2d42018-11-15 12:24:31 -08002910 struct audio_usecase *voip_usecase = get_usecase_from_list(adev,
2911 USECASE_AUDIO_PLAYBACK_VOIP);
2912
Carter Hsu2e429db2019-05-14 18:50:52 +08002913 usecase->stream.in->enable_ec_port = false;
2914
Zhou Song503196b2021-07-23 17:31:05 +08002915 if (usecase->id == USECASE_AUDIO_RECORD_AFE_PROXY ||
2916 usecase->id == USECASE_AUDIO_RECORD_AFE_PROXY2) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002917 reassign_device_list(&out_devices, AUDIO_DEVICE_OUT_TELEPHONY_TX, "");
Eric Laurent637e2d42018-11-15 12:24:31 -08002918 } else if (voip_usecase) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002919 assign_devices(&out_devices, &voip_usecase->stream.out->device_list);
Eric Laurent637e2d42018-11-15 12:24:31 -08002920 } else if (adev->primary_output &&
2921 !adev->primary_output->standby) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002922 assign_devices(&out_devices, &adev->primary_output->device_list);
Eric Laurent637e2d42018-11-15 12:24:31 -08002923 } else {
2924 /* forcing speaker o/p device to get matching i/p pair
2925 in case o/p is not routed from same primary HAL */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002926 reassign_device_list(&out_devices, AUDIO_DEVICE_OUT_SPEAKER, "");
Eric Laurent637e2d42018-11-15 12:24:31 -08002927 }
Carter Hsu2e429db2019-05-14 18:50:52 +08002928 priority_in = voip_in;
2929 } else {
2930 /* get the input with the highest priority source*/
2931 priority_in = get_priority_input(adev);
2932
Susan Wang727dd6b2021-03-26 11:28:59 -04002933 if (!priority_in ||
2934 audio_extn_auto_hal_overwrite_priority_for_auto(usecase->stream.in))
Carter Hsu2e429db2019-05-14 18:50:52 +08002935 priority_in = usecase->stream.in;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07002936 }
Huicheng Liu1404ba12020-09-11 01:03:25 -04002937 if (compare_device_type(&usecase->device_list, AUDIO_DEVICE_IN_BUS)){
2938 in_snd_device = audio_extn_auto_hal_get_snd_device_for_car_audio_stream(priority_in->car_audio_stream);
2939 }
2940 else
2941 in_snd_device = platform_get_input_snd_device(adev->platform,
2942 priority_in,
2943 &out_devices,
2944 usecase->type);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07002945 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002946 }
2947 }
2948
2949 if (out_snd_device == usecase->out_snd_device &&
2950 in_snd_device == usecase->in_snd_device) {
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302951
2952 if (!force_device_switch(usecase))
2953 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002954 }
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 ((is_btsco_device(out_snd_device,in_snd_device) && !adev->bt_sco_on) ||
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002958 (is_a2dp_device(out_snd_device) && !audio_extn_a2dp_source_is_ready()))) {
Guodong Hu267bdf82019-08-12 19:22:32 +08002959 ALOGD("SCO/A2DP is selected but they are not connected/ready hence dont route");
2960 return 0;
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302961 }
2962
Aalique Grahame22e49102018-12-18 14:23:57 -08002963 if (out_snd_device != SND_DEVICE_NONE &&
2964 out_snd_device != adev->last_logged_snd_device[uc_id][0]) {
2965 ALOGD("%s: changing use case %s output device from(%d: %s, acdb %d) to (%d: %s, acdb %d)",
2966 __func__,
2967 use_case_table[uc_id],
2968 adev->last_logged_snd_device[uc_id][0],
2969 platform_get_snd_device_name(adev->last_logged_snd_device[uc_id][0]),
2970 adev->last_logged_snd_device[uc_id][0] != SND_DEVICE_NONE ?
2971 platform_get_snd_device_acdb_id(adev->last_logged_snd_device[uc_id][0]) :
2972 -1,
2973 out_snd_device,
2974 platform_get_snd_device_name(out_snd_device),
2975 platform_get_snd_device_acdb_id(out_snd_device));
2976 adev->last_logged_snd_device[uc_id][0] = out_snd_device;
2977 }
2978 if (in_snd_device != SND_DEVICE_NONE &&
2979 in_snd_device != adev->last_logged_snd_device[uc_id][1]) {
2980 ALOGD("%s: changing use case %s input device from(%d: %s, acdb %d) to (%d: %s, acdb %d)",
2981 __func__,
2982 use_case_table[uc_id],
2983 adev->last_logged_snd_device[uc_id][1],
2984 platform_get_snd_device_name(adev->last_logged_snd_device[uc_id][1]),
2985 adev->last_logged_snd_device[uc_id][1] != SND_DEVICE_NONE ?
2986 platform_get_snd_device_acdb_id(adev->last_logged_snd_device[uc_id][1]) :
2987 -1,
2988 in_snd_device,
2989 platform_get_snd_device_name(in_snd_device),
2990 platform_get_snd_device_acdb_id(in_snd_device));
2991 adev->last_logged_snd_device[uc_id][1] = in_snd_device;
2992 }
2993
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002994
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002995 /*
2996 * Limitation: While in call, to do a device switch we need to disable
2997 * and enable both RX and TX devices though one of them is same as current
2998 * device.
2999 */
Vidyakumar Athota21b3bb92014-04-25 11:08:08 -07003000 if ((usecase->type == VOICE_CALL) &&
3001 (usecase->in_snd_device != SND_DEVICE_NONE) &&
3002 (usecase->out_snd_device != SND_DEVICE_NONE)) {
Eric Laurentb23d5282013-05-14 15:27:20 -07003003 status = platform_switch_voice_call_device_pre(adev->platform);
Narsinga Rao Chella116142b2015-08-14 18:00:08 -07003004 }
3005
3006 if (((usecase->type == VOICE_CALL) ||
3007 (usecase->type == VOIP_CALL)) &&
3008 (usecase->out_snd_device != SND_DEVICE_NONE)) {
3009 /* Disable sidetone only if voice/voip call already exists */
Jaideep Sharma477917f2020-03-13 18:13:33 +05303010 if (voice_is_call_state_active_in_call(adev) ||
Narsinga Rao Chella116142b2015-08-14 18:00:08 -07003011 voice_extn_compress_voip_is_started(adev))
Bhalchandra Gajare45fee282015-06-09 22:23:45 -07003012 voice_set_sidetone(adev, usecase->out_snd_device, false);
Vidyakumar Athotaea269c62016-10-31 09:05:59 -07003013
3014 /* Disable aanc only if voice call exists */
Jaideep Sharma477917f2020-03-13 18:13:33 +05303015 if (voice_is_call_state_active_in_call(adev))
Vidyakumar Athotaea269c62016-10-31 09:05:59 -07003016 voice_check_and_update_aanc_path(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08003017 }
3018
Aalique Grahame22e49102018-12-18 14:23:57 -08003019 if ((out_snd_device == SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP ||
3020 out_snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP) &&
Florian Pfister1a84f312018-07-19 14:38:18 +02003021 (!audio_extn_a2dp_source_is_ready())) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303022 ALOGW("%s: A2DP profile is not ready, routing to speaker only", __func__);
Aalique Grahame22e49102018-12-18 14:23:57 -08003023 if (out_snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP)
3024 out_snd_device = SND_DEVICE_OUT_SPEAKER_SAFE;
3025 else
3026 out_snd_device = SND_DEVICE_OUT_SPEAKER;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303027 }
3028
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003029 /* Disable current sound devices */
3030 if (usecase->out_snd_device != SND_DEVICE_NONE) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07003031 disable_audio_route(adev, usecase);
3032 disable_snd_device(adev, usecase->out_snd_device);
Gautam Manam274f4752021-09-24 10:58:49 +05303033 if (usecase->id == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS)
3034 disable_snd_device(adev, SND_DEVICE_OUT_HAPTICS);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003035 }
3036
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003037 if (usecase->in_snd_device != SND_DEVICE_NONE) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07003038 disable_audio_route(adev, usecase);
3039 disable_snd_device(adev, usecase->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003040 }
3041
Vidyakumar Athota545dbd32013-11-13 17:30:53 -08003042 /* Applicable only on the targets that has external modem.
3043 * New device information should be sent to modem before enabling
3044 * the devices to reduce in-call device switch time.
3045 */
Vidyakumar Athota21b3bb92014-04-25 11:08:08 -07003046 if ((usecase->type == VOICE_CALL) &&
3047 (usecase->in_snd_device != SND_DEVICE_NONE) &&
3048 (usecase->out_snd_device != SND_DEVICE_NONE)) {
Vidyakumar Athota545dbd32013-11-13 17:30:53 -08003049 status = platform_switch_voice_call_enable_device_config(adev->platform,
3050 out_snd_device,
3051 in_snd_device);
Vidyakumar Athota21b3bb92014-04-25 11:08:08 -07003052 }
Vidyakumar Athota545dbd32013-11-13 17:30:53 -08003053
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003054 /* Enable new sound devices */
3055 if (out_snd_device != SND_DEVICE_NONE) {
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08003056 check_usecases_codec_backend(adev, usecase, out_snd_device);
Saurav Kumarc1411662020-10-14 10:50:45 +05303057 check_and_configure_headphone(adev, usecase, out_snd_device);
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +05303058 if (platform_check_codec_asrc_support(adev->platform))
3059 check_and_set_asrc_mode(adev, usecase, out_snd_device);
Haynes Mathew George1376ca62014-04-24 11:55:48 -07003060 enable_snd_device(adev, out_snd_device);
Zhou Songd9bd9302020-08-04 16:34:45 +08003061 /* Enable haptics device for haptic usecase */
3062 if (usecase->id == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS)
3063 enable_snd_device(adev, SND_DEVICE_OUT_HAPTICS);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003064 }
3065
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07003066 if (in_snd_device != SND_DEVICE_NONE) {
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05303067 check_usecases_capture_codec_backend(adev, usecase, in_snd_device);
Haynes Mathew George1376ca62014-04-24 11:55:48 -07003068 enable_snd_device(adev, in_snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07003069 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003070
Lakshman Chaluvarajuab7e5572020-10-28 19:29:23 +05303071 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL)
Eric Laurentb23d5282013-05-14 15:27:20 -07003072 status = platform_switch_voice_call_device_post(adev->platform,
3073 out_snd_device,
3074 in_snd_device);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08003075
sangwoo170731f2013-06-08 15:36:36 +09003076 usecase->in_snd_device = in_snd_device;
3077 usecase->out_snd_device = out_snd_device;
3078
Dhananjay Kumard6d32152016-10-13 16:11:03 +05303079 audio_extn_utils_update_stream_app_type_cfg_for_usecase(adev,
3080 usecase);
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +05303081 if (usecase->type == PCM_PLAYBACK) {
Weiyin Jiang6f4c8062016-11-23 15:30:29 +08003082 if ((24 == usecase->stream.out->bit_width) &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003083 compare_device_type(&usecase->stream.out->device_list, AUDIO_DEVICE_OUT_SPEAKER)) {
Weiyin Jiang6f4c8062016-11-23 15:30:29 +08003084 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
3085 } else if ((out_snd_device == SND_DEVICE_OUT_HDMI ||
3086 out_snd_device == SND_DEVICE_OUT_USB_HEADSET ||
3087 out_snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
3088 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
3089 /*
3090 * To best utlize DSP, check if the stream sample rate is supported/multiple of
3091 * configured device sample rate, if not update the COPP rate to be equal to the
3092 * device sample rate, else open COPP at stream sample rate
3093 */
3094 platform_check_and_update_copp_sample_rate(adev->platform, out_snd_device,
3095 usecase->stream.out->sample_rate,
3096 &usecase->stream.out->app_type_cfg.sample_rate);
Ashish Jain4826f6c2017-02-06 13:33:20 +05303097 } else if (((out_snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
Preetam Singh Ranawat590d0432019-09-30 14:39:47 +05303098 out_snd_device != SND_DEVICE_OUT_HEADPHONES &&
3099 out_snd_device != SND_DEVICE_OUT_HEADPHONES_HIFI_FILTER &&
Ashish Jain4826f6c2017-02-06 13:33:20 +05303100 !audio_is_true_native_stream_active(adev)) &&
Weiyin Jiang6f4c8062016-11-23 15:30:29 +08003101 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
3102 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
3103 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
3104 }
Weiyin Jiangcdece202019-07-08 16:13:16 +08003105 }
3106 enable_audio_route(adev, usecase);
Weiyin Jiang6f4c8062016-11-23 15:30:29 +08003107
Shalini Manjunatha5e610912020-12-29 13:02:25 +05303108 if (uc_id == USECASE_AUDIO_PLAYBACK_VOIP) {
3109 struct stream_in *voip_in = get_voice_communication_input(adev);
3110 struct audio_usecase *voip_in_usecase = NULL;
3111 voip_in_usecase = get_usecase_from_list(adev, USECASE_AUDIO_RECORD_VOIP);
3112 if (voip_in != NULL &&
3113 voip_in_usecase != NULL &&
3114 !(out_snd_device == AUDIO_DEVICE_OUT_SPEAKER ||
3115 out_snd_device == AUDIO_DEVICE_OUT_SPEAKER_SAFE) &&
3116 (voip_in_usecase->in_snd_device ==
3117 platform_get_input_snd_device(adev->platform, voip_in,
3118 &usecase->stream.out->device_list,usecase->type))) {
3119 /*
3120 * if VOIP TX is enabled before VOIP RX, needs to re-route the TX path
3121 * for enabling echo-reference-voip with correct port
3122 */
3123 ALOGD("%s: VOIP TX is enabled before VOIP RX,needs to re-route the TX path",__func__);
3124 disable_audio_route(adev, voip_in_usecase);
3125 disable_snd_device(adev, voip_in_usecase->in_snd_device);
3126 enable_snd_device(adev, voip_in_usecase->in_snd_device);
3127 enable_audio_route(adev, voip_in_usecase);
3128 }
3129 }
Ramjee Singhcdf67af2021-09-29 14:20:27 +05303130 if (voice_extn_compress_voip_is_active(adev)) {
3131 struct audio_usecase *voip_usecase = get_usecase_from_list(adev,
3132 USECASE_COMPRESS_VOIP_CALL);
3133 /*
3134 * If only compress voip input is opened voip out will be primary out.
3135 * Need to consider re-routing to select correct i/p pair
3136 */
3137 if ((voip_usecase != NULL) &&
3138 (usecase->type == PCM_PLAYBACK) &&
3139 (usecase->stream.out == voip_usecase->stream.out)) {
3140 in_snd_device = platform_get_input_snd_device(adev->platform,
3141 NULL,
3142 &usecase->stream.out->device_list,
3143 usecase->type);
3144 if (voip_usecase->in_snd_device != in_snd_device ) {
3145 ALOGD("%s:Re routing compress voip tx snd device matching voip rx pair",
3146 __func__);
3147 disable_audio_route(adev, voip_usecase);
3148 disable_snd_device(adev, voip_usecase->in_snd_device);
3149 voip_usecase->in_snd_device = in_snd_device;
3150 voip_usecase->out_snd_device = usecase->out_snd_device;
3151 /* Route all TX usecase to Compress voip BE */
3152 check_usecases_capture_codec_backend(adev, voip_usecase, in_snd_device);
3153 enable_snd_device(adev, in_snd_device);
3154 /* Send Voice related calibration for RX /TX pair */
3155 status = platform_switch_voice_call_device_post(adev->platform,
3156 out_snd_device,
3157 in_snd_device);
3158 enable_audio_route(adev, voip_usecase);
3159 }
3160 }
3161 }
Shalini Manjunatha5e610912020-12-29 13:02:25 +05303162
3163
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003164 audio_extn_qdsp_set_device(usecase);
Aalique Grahame22e49102018-12-18 14:23:57 -08003165
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07003166 /* If input stream is already running then effect needs to be
3167 applied on the new input device that's being enabled here. */
Eric Laurent637e2d42018-11-15 12:24:31 -08003168 if (in_snd_device != SND_DEVICE_NONE)
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07003169 check_and_enable_effect(adev);
3170
Vidyakumar Athota493f2892016-08-14 11:56:55 -07003171 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) {
Vidyakumar Athotaea269c62016-10-31 09:05:59 -07003172 /* Enable aanc only if voice call exists */
Jaideep Sharma477917f2020-03-13 18:13:33 +05303173 if (voice_is_call_state_active_in_call(adev))
Vidyakumar Athotaea269c62016-10-31 09:05:59 -07003174 voice_check_and_update_aanc_path(adev, out_snd_device, true);
3175
Vidyakumar Athota493f2892016-08-14 11:56:55 -07003176 /* Enable sidetone only if other voice/voip call already exists */
Jaideep Sharma477917f2020-03-13 18:13:33 +05303177 if (voice_is_call_state_active_in_call(adev) ||
Vidyakumar Athota493f2892016-08-14 11:56:55 -07003178 voice_extn_compress_voip_is_started(adev))
3179 voice_set_sidetone(adev, out_snd_device, true);
3180 }
3181
Vidyakumar Athota1fd21792013-11-15 14:50:57 -08003182 /* Applicable only on the targets that has external modem.
3183 * Enable device command should be sent to modem only after
3184 * enabling voice call mixer controls
3185 */
Vidyakumar Athota339342f2014-07-01 15:30:57 -07003186 if (usecase->type == VOICE_CALL)
Vidyakumar Athota1fd21792013-11-15 14:50:57 -08003187 status = platform_switch_voice_call_usecase_route_post(adev->platform,
3188 out_snd_device,
3189 in_snd_device);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303190
3191 if (is_btsco_device(out_snd_device, in_snd_device) || is_a2dp_device(out_snd_device)) {
Eric Laurent637e2d42018-11-15 12:24:31 -08003192 struct stream_in *in = adev_get_active_input(adev);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303193 if (usecase->type == VOIP_CALL) {
Eric Laurent637e2d42018-11-15 12:24:31 -08003194 if (in != NULL && !in->standby) {
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303195 if (is_bt_soc_on(adev) == false){
3196 ALOGD("BT SCO MIC disconnected while in connection");
Eric Laurent637e2d42018-11-15 12:24:31 -08003197 if (in->pcm != NULL)
3198 pcm_stop(in->pcm);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303199 }
3200 }
3201 if ((usecase->stream.out != NULL) && (usecase->stream.out != adev->primary_output)
3202 && usecase->stream.out->started) {
3203 if (is_bt_soc_on(adev) == false) {
3204 ALOGD("BT SCO/A2DP disconnected while in connection");
3205 out_standby_l(&usecase->stream.out->stream.common);
3206 }
3207 }
3208 } else if ((usecase->stream.out != NULL) &&
3209 !(usecase->stream.out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
Surendar Karkae1dc8742018-11-19 16:23:14 +05303210 (usecase->type != TRANSCODE_LOOPBACK_TX) &&
3211 (usecase->type != TRANSCODE_LOOPBACK_RX) &&
Weiyin Jiang0d373242019-07-25 13:18:17 +08003212 (usecase->type != PCM_CAPTURE) &&
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303213 usecase->stream.out->started) {
3214 if (is_bt_soc_on(adev) == false) {
3215 ALOGD("BT SCO/A2dp disconnected while in connection");
3216 out_standby_l(&usecase->stream.out->stream.common);
3217 }
3218 }
3219 }
3220
Yung Ti Su70cb8242018-06-22 17:38:47 +08003221 if (usecase->type != PCM_CAPTURE && usecase == voip_usecase) {
Aalique Grahame22e49102018-12-18 14:23:57 -08003222 struct stream_out *voip_out = voip_usecase->stream.out;
3223 audio_extn_utils_send_app_type_gain(adev,
3224 voip_out->app_type_cfg.app_type,
3225 &voip_out->app_type_cfg.gain[0]);
3226 }
3227
Ajender Reddyb940b832021-07-07 11:51:42 +05303228 ALOGD("%s: done",__func__);
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05303229
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003230 return status;
3231}
3232
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003233static int stop_input_stream(struct stream_in *in)
3234{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05303235 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003236 struct audio_usecase *uc_info;
Pallavid7c7a272018-01-16 11:22:55 +05303237
3238 if (in == NULL) {
3239 ALOGE("%s: stream_in ptr is NULL", __func__);
3240 return -EINVAL;
3241 }
3242
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003243 struct audio_device *adev = in->dev;
Carter Hsu2e429db2019-05-14 18:50:52 +08003244 struct stream_in *priority_in = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003245
Eric Laurent994a6932013-07-17 11:51:42 -07003246 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003247 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003248 uc_info = get_usecase_from_list(adev, in->usecase);
3249 if (uc_info == NULL) {
3250 ALOGE("%s: Could not find the usecase (%d) in the list",
3251 __func__, in->usecase);
3252 return -EINVAL;
3253 }
3254
Carter Hsu2e429db2019-05-14 18:50:52 +08003255 priority_in = get_priority_input(adev);
3256
Derek Chenea197282019-01-07 17:35:01 -08003257 if (audio_extn_ext_hw_plugin_usecase_stop(adev->ext_hw_plugin, uc_info))
3258 ALOGE("%s: failed to stop ext hw plugin", __func__);
Derek Chend2530072014-11-24 12:39:14 -08003259
Vidyakumar Athota2850d532013-11-19 16:02:12 -08003260 /* Close in-call recording streams */
3261 voice_check_and_stop_incall_rec_usecase(adev, in);
3262
Eric Laurent150dbfe2013-02-27 14:31:02 -08003263 /* 1. Disable stream specific mixer controls */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07003264 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003265
3266 /* 2. Disable the tx device */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07003267 disable_snd_device(adev, uc_info->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003268
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003269 if (is_loopback_input_device(get_device_types(&in->device_list)))
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05303270 audio_extn_keep_alive_stop(KEEP_ALIVE_OUT_PRIMARY);
3271
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08003272 list_remove(&uc_info->list);
3273 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003274
Carter Hsu2e429db2019-05-14 18:50:52 +08003275 if (priority_in == in) {
3276 priority_in = get_priority_input(adev);
Sujin Panicker110f7942021-08-26 17:01:22 +05303277 if (priority_in) {
3278 if (is_usb_in_device_type(&priority_in->device_list)) {
3279 if (audio_extn_usb_connected(NULL))
3280 select_devices(adev, priority_in->usecase);
3281 } else {
3282 select_devices(adev, priority_in->usecase);
3283 }
3284 }
Carter Hsu2e429db2019-05-14 18:50:52 +08003285 }
3286
Vatsal Buchac09ae062018-11-14 13:25:08 +05303287 enable_gcov();
Eric Laurent994a6932013-07-17 11:51:42 -07003288 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003289 return ret;
3290}
3291
3292int start_input_stream(struct stream_in *in)
3293{
3294 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -08003295 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003296 struct audio_usecase *uc_info;
Preetam Singh Ranawata87e9742018-02-13 16:52:53 +05303297
3298 if (in == NULL) {
3299 ALOGE("%s: stream_in ptr is NULL", __func__);
3300 return -EINVAL;
3301 }
3302
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003303 struct audio_device *adev = in->dev;
Garmond Leunge2433c32017-09-28 21:51:22 -07003304 struct pcm_config config = in->config;
Garmond Leung438932f2017-10-04 19:35:18 -07003305 int usecase = platform_update_usecase_from_source(in->source,in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003306
Mingming Yin2664a5b2015-09-03 10:53:11 -07003307 if (get_usecase_from_list(adev, usecase) == NULL)
3308 in->usecase = usecase;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05303309 ALOGD("%s: enter: stream(%p)usecase(%d: %s)",
3310 __func__, &in->stream, in->usecase, use_case_table[in->usecase]);
Shiv Maliyappanahallida107642013-10-17 11:16:13 -07003311
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05303312 if (CARD_STATUS_OFFLINE == in->card_status||
3313 CARD_STATUS_OFFLINE == adev->card_status) {
3314 ALOGW("in->card_status or adev->card_status offline, try again");
Dhanalakshmi Siddani4d57e992014-07-17 16:37:51 +05303315 ret = -EIO;
Naresh Tanniru4c630392014-05-12 01:05:52 +05303316 goto error_config;
3317 }
Naresh Tanniru4c630392014-05-12 01:05:52 +05303318
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003319 if (is_sco_in_device_type(&in->device_list)) {
Lakshman Chaluvaraju87f53aa2021-02-02 15:50:11 +05303320 if (!adev->bt_sco_on || audio_extn_a2dp_source_is_ready()) {
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303321 ALOGE("%s: SCO profile is not ready, return error", __func__);
3322 ret = -EIO;
3323 goto error_config;
3324 }
3325 }
3326
Shiv Maliyappanahallida107642013-10-17 11:16:13 -07003327 /* Check if source matches incall recording usecase criteria */
3328 ret = voice_check_and_set_incall_rec_usecase(adev, in);
3329 if (ret)
3330 goto error_config;
3331 else
Mingming Yin2664a5b2015-09-03 10:53:11 -07003332 ALOGV("%s: usecase(%d)", __func__, in->usecase);
3333
Dhananjay Kumar7dbe3562019-08-30 05:49:33 +05303334 if (audio_extn_cin_attached_usecase(in))
3335 audio_extn_cin_acquire_usecase(in);
3336
Mingming Yin2664a5b2015-09-03 10:53:11 -07003337 if (get_usecase_from_list(adev, in->usecase) != NULL) {
3338 ALOGE("%s: use case assigned already in use, stream(%p)usecase(%d: %s)",
3339 __func__, &in->stream, in->usecase, use_case_table[in->usecase]);
Weiyin Jiang38c0e612020-09-10 16:10:51 +08003340 ret = -EINVAL;
3341 goto error_config;
Mingming Yin2664a5b2015-09-03 10:53:11 -07003342 }
Shiv Maliyappanahallida107642013-10-17 11:16:13 -07003343
Eric Laurentb23d5282013-05-14 15:27:20 -07003344 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003345 if (in->pcm_device_id < 0) {
3346 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
3347 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -08003348 ret = -EINVAL;
3349 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003350 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003351
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003352 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07003353
3354 if (!uc_info) {
3355 ret = -ENOMEM;
3356 goto error_config;
3357 }
3358
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003359 uc_info->id = in->usecase;
3360 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08003361 uc_info->stream.in = in;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003362 list_init(&uc_info->device_list);
3363 assign_devices(&uc_info->device_list, &in->device_list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003364 uc_info->in_snd_device = SND_DEVICE_NONE;
3365 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003366
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08003367 list_add_tail(&adev->usecase_list, &uc_info->list);
Wei Wangf7ca6c92017-11-21 14:51:20 -08003368 audio_streaming_hint_start();
Sudheer Papothifa9d2282015-09-17 01:53:25 +05303369 audio_extn_perf_lock_acquire(&adev->perf_lock_handle, 0,
3370 adev->perf_lock_opts,
3371 adev->perf_lock_opts_size);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003372 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003373
Derek Chenea197282019-01-07 17:35:01 -08003374 if (audio_extn_ext_hw_plugin_usecase_start(adev->ext_hw_plugin, uc_info))
3375 ALOGE("%s: failed to start ext hw plugin", __func__);
Derek Chend2530072014-11-24 12:39:14 -08003376
Deeraj Soman30cc7ae2019-03-18 16:26:55 +05303377 android_atomic_acquire_cas(true, false, &(in->capture_stopped));
3378
Dhananjay Kumar7dbe3562019-08-30 05:49:33 +05303379 if (audio_extn_cin_attached_usecase(in)) {
Manish Dewangan46e07982018-12-13 18:18:59 +05303380 ret = audio_extn_cin_open_input_stream(in);
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05303381 if (ret)
3382 goto error_open;
3383 else
3384 goto done_open;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003385 }
3386
Haynes Mathew George16081042017-05-31 17:16:49 -07003387 if (in->usecase == USECASE_AUDIO_RECORD_MMAP) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003388 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) {
Haynes Mathew George16081042017-05-31 17:16:49 -07003389 ALOGE("%s: pcm stream not ready", __func__);
3390 goto error_open;
3391 }
3392 ret = pcm_start(in->pcm);
3393 if (ret < 0) {
3394 ALOGE("%s: MMAP pcm_start failed ret %d", __func__, ret);
3395 goto error_open;
3396 }
3397 } else {
3398 unsigned int flags = PCM_IN | PCM_MONOTONIC;
3399 unsigned int pcm_open_retry_count = 0;
3400
Zhou Song62ea0282020-03-22 19:53:01 +08003401 if ((in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY) ||
3402 (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY2)) {
Haynes Mathew George16081042017-05-31 17:16:49 -07003403 flags |= PCM_MMAP | PCM_NOIRQ;
3404 pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT;
3405 } else if (in->realtime) {
3406 flags |= PCM_MMAP | PCM_NOIRQ;
3407 }
3408
Garmond Leunge2433c32017-09-28 21:51:22 -07003409 if (audio_extn_ffv_get_stream() == in) {
3410 ALOGD("%s: ffv stream, update pcm config", __func__);
3411 audio_extn_ffv_update_pcm_config(&config);
3412 }
Haynes Mathew George16081042017-05-31 17:16:49 -07003413 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
3414 __func__, adev->snd_card, in->pcm_device_id, in->config.channels);
3415
3416 while (1) {
Haynes Mathew George380745d2017-10-04 15:27:45 -07003417 ATRACE_BEGIN("pcm_in_open");
Haynes Mathew George16081042017-05-31 17:16:49 -07003418 in->pcm = pcm_open(adev->snd_card, in->pcm_device_id,
Garmond Leung438932f2017-10-04 19:35:18 -07003419 flags, &config);
Haynes Mathew George380745d2017-10-04 15:27:45 -07003420 ATRACE_END();
Satish Babu Patakokila54ce83d2018-07-06 18:00:37 +05303421 if (errno == ENETRESET && !pcm_is_ready(in->pcm)) {
Sharad Sanglec6f32552018-05-04 16:15:38 +05303422 ALOGE("%s: pcm_open failed errno:%d\n", __func__, errno);
3423 adev->card_status = CARD_STATUS_OFFLINE;
3424 in->card_status = CARD_STATUS_OFFLINE;
3425 ret = -EIO;
3426 goto error_open;
3427 }
3428
Haynes Mathew George16081042017-05-31 17:16:49 -07003429 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) {
3430 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
3431 if (in->pcm != NULL) {
3432 pcm_close(in->pcm);
3433 in->pcm = NULL;
3434 }
Weiyin Jiang72197252019-10-09 11:49:32 +08003435 if (pcm_open_retry_count == 0) {
Haynes Mathew George16081042017-05-31 17:16:49 -07003436 ret = -EIO;
3437 goto error_open;
3438 }
Weiyin Jiang72197252019-10-09 11:49:32 +08003439 pcm_open_retry_count--;
Haynes Mathew George16081042017-05-31 17:16:49 -07003440 usleep(PROXY_OPEN_WAIT_TIME * 1000);
3441 continue;
3442 }
3443 break;
3444 }
3445
3446 ALOGV("%s: pcm_prepare", __func__);
Haynes Mathew George380745d2017-10-04 15:27:45 -07003447 ATRACE_BEGIN("pcm_in_prepare");
Haynes Mathew George16081042017-05-31 17:16:49 -07003448 ret = pcm_prepare(in->pcm);
Haynes Mathew George380745d2017-10-04 15:27:45 -07003449 ATRACE_END();
Haynes Mathew George16081042017-05-31 17:16:49 -07003450 if (ret < 0) {
3451 ALOGE("%s: pcm_prepare returned %d", __func__, ret);
3452 pcm_close(in->pcm);
3453 in->pcm = NULL;
3454 goto error_open;
3455 }
3456 register_in_stream(in);
3457 if (in->realtime) {
Haynes Mathew George380745d2017-10-04 15:27:45 -07003458 ATRACE_BEGIN("pcm_in_start");
Haynes Mathew George16081042017-05-31 17:16:49 -07003459 ret = pcm_start(in->pcm);
Haynes Mathew George380745d2017-10-04 15:27:45 -07003460 ATRACE_END();
Haynes Mathew George16081042017-05-31 17:16:49 -07003461 if (ret < 0) {
3462 ALOGE("%s: RT pcm_start failed ret %d", __func__, ret);
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003463 pcm_close(in->pcm);
3464 in->pcm = NULL;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003465 goto error_open;
3466 }
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003467 }
Haynes Mathew George5beddd42016-06-27 18:33:40 -07003468 }
3469
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07003470 check_and_enable_effect(adev);
justinweng20fb6d82019-02-21 18:49:00 -07003471 audio_extn_audiozoom_set_microphone_direction(in, in->zoom);
3472 audio_extn_audiozoom_set_microphone_field_dimension(in, in->direction);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07003473
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003474 if (is_loopback_input_device(get_device_types(&in->device_list)))
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05303475 audio_extn_keep_alive_start(KEEP_ALIVE_OUT_PRIMARY);
3476
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05303477done_open:
Wei Wangf7ca6c92017-11-21 14:51:20 -08003478 audio_streaming_hint_end();
Sudheer Papothifa9d2282015-09-17 01:53:25 +05303479 audio_extn_perf_lock_release(&adev->perf_lock_handle);
Manisha Agarwal4c2402e2020-10-21 12:02:57 +05303480 ALOGD("%s: exit", __func__);
Vatsal Buchac09ae062018-11-14 13:25:08 +05303481 enable_gcov();
Eric Laurentc8400632013-02-14 19:04:54 -08003482 return ret;
3483
3484error_open:
Wei Wangf7ca6c92017-11-21 14:51:20 -08003485 audio_streaming_hint_end();
Sudheer Papothifa9d2282015-09-17 01:53:25 +05303486 audio_extn_perf_lock_release(&adev->perf_lock_handle);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003487 stop_input_stream(in);
Wei Wangf7ca6c92017-11-21 14:51:20 -08003488
Eric Laurentc8400632013-02-14 19:04:54 -08003489error_config:
Weiyin Jiang38c0e612020-09-10 16:10:51 +08003490 if (audio_extn_cin_attached_usecase(in))
3491 audio_extn_cin_close_input_stream(in);
Laxminath Kasam2cb4b752015-09-24 03:59:15 +05303492 /*
3493 * sleep 50ms to allow sufficient time for kernel
3494 * drivers to recover incases like SSR.
3495 */
3496 usleep(50000);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003497 ALOGD("%s: exit: status(%d)", __func__, ret);
Vatsal Buchac09ae062018-11-14 13:25:08 +05303498 enable_gcov();
Eric Laurentc8400632013-02-14 19:04:54 -08003499 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003500}
3501
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07003502void lock_input_stream(struct stream_in *in)
3503{
3504 pthread_mutex_lock(&in->pre_lock);
3505 pthread_mutex_lock(&in->lock);
3506 pthread_mutex_unlock(&in->pre_lock);
3507}
3508
3509void lock_output_stream(struct stream_out *out)
3510{
3511 pthread_mutex_lock(&out->pre_lock);
3512 pthread_mutex_lock(&out->lock);
3513 pthread_mutex_unlock(&out->pre_lock);
3514}
3515
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003516/* must be called with out->lock locked */
3517static int send_offload_cmd_l(struct stream_out* out, int command)
3518{
3519 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
3520
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07003521 if (!cmd) {
3522 ALOGE("failed to allocate mem for command 0x%x", command);
3523 return -ENOMEM;
3524 }
3525
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003526 ALOGVV("%s %d", __func__, command);
3527
3528 cmd->cmd = command;
3529 list_add_tail(&out->offload_cmd_list, &cmd->node);
3530 pthread_cond_signal(&out->offload_cond);
3531 return 0;
3532}
3533
Gautam Manam14c198b2020-12-24 14:08:04 +05303534/* must be called with out->lock */
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003535static void stop_compressed_output_l(struct stream_out *out)
3536{
Gautam Manam14c198b2020-12-24 14:08:04 +05303537 pthread_mutex_lock(&out->latch_lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003538 out->offload_state = OFFLOAD_STATE_IDLE;
Gautam Manam14c198b2020-12-24 14:08:04 +05303539 pthread_mutex_unlock(&out->latch_lock);
Weiyin Jiang906db3c2021-03-02 13:17:04 +08003540
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003541 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07003542 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003543 if (out->compr != NULL) {
3544 compress_stop(out->compr);
3545 while (out->offload_thread_blocked) {
3546 pthread_cond_wait(&out->cond, &out->lock);
3547 }
3548 }
3549}
3550
Varun Balaraje49253e2017-07-06 19:48:56 +05303551bool is_interactive_usecase(audio_usecase_t uc_id)
3552{
3553 unsigned int i;
3554 for (i = 0; i < sizeof(interactive_usecases)/sizeof(interactive_usecases[0]); i++) {
3555 if (uc_id == interactive_usecases[i])
3556 return true;
3557 }
3558 return false;
3559}
3560
3561static audio_usecase_t get_interactive_usecase(struct audio_device *adev)
3562{
3563 audio_usecase_t ret_uc = USECASE_INVALID;
3564 unsigned int intract_uc_index;
3565 unsigned int num_usecase = sizeof(interactive_usecases)/sizeof(interactive_usecases[0]);
3566
3567 ALOGV("%s: num_usecase: %d", __func__, num_usecase);
3568 for (intract_uc_index = 0; intract_uc_index < num_usecase; intract_uc_index++) {
3569 if (!(adev->interactive_usecase_state & (0x1 << intract_uc_index))) {
3570 adev->interactive_usecase_state |= 0x1 << intract_uc_index;
3571 ret_uc = interactive_usecases[intract_uc_index];
3572 break;
3573 }
3574 }
3575
3576 ALOGV("%s: Interactive usecase is %d", __func__, ret_uc);
3577 return ret_uc;
3578}
3579
3580static void free_interactive_usecase(struct audio_device *adev,
3581 audio_usecase_t uc_id)
3582{
3583 unsigned int interact_uc_index;
3584 unsigned int num_usecase = sizeof(interactive_usecases)/sizeof(interactive_usecases[0]);
3585
3586 for (interact_uc_index = 0; interact_uc_index < num_usecase; interact_uc_index++) {
3587 if (interactive_usecases[interact_uc_index] == uc_id) {
3588 adev->interactive_usecase_state &= ~(0x1 << interact_uc_index);
3589 break;
3590 }
3591 }
3592 ALOGV("%s: free Interactive usecase %d", __func__, uc_id);
3593}
3594
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003595bool is_offload_usecase(audio_usecase_t uc_id)
3596{
3597 unsigned int i;
3598 for (i = 0; i < sizeof(offload_usecases)/sizeof(offload_usecases[0]); i++) {
3599 if (uc_id == offload_usecases[i])
3600 return true;
3601 }
3602 return false;
3603}
3604
Dhananjay Kumarac341582017-02-23 23:42:25 +05303605static audio_usecase_t get_offload_usecase(struct audio_device *adev, bool is_compress)
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003606{
vivek mehta446c3962015-09-14 10:57:35 -07003607 audio_usecase_t ret_uc = USECASE_INVALID;
3608 unsigned int offload_uc_index;
Alexy Josephb1379942016-01-29 15:49:38 -08003609 unsigned int num_usecase = sizeof(offload_usecases)/sizeof(offload_usecases[0]);
vivek mehta446c3962015-09-14 10:57:35 -07003610 if (!adev->multi_offload_enable) {
Dhananjay Kumarac341582017-02-23 23:42:25 +05303611 if (!is_compress)
vivek mehta446c3962015-09-14 10:57:35 -07003612 ret_uc = USECASE_AUDIO_PLAYBACK_OFFLOAD2;
3613 else
3614 ret_uc = USECASE_AUDIO_PLAYBACK_OFFLOAD;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003615
vivek mehta446c3962015-09-14 10:57:35 -07003616 pthread_mutex_lock(&adev->lock);
3617 if (get_usecase_from_list(adev, ret_uc) != NULL)
3618 ret_uc = USECASE_INVALID;
3619 pthread_mutex_unlock(&adev->lock);
3620
3621 return ret_uc;
3622 }
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003623
3624 ALOGV("%s: num_usecase: %d", __func__, num_usecase);
vivek mehta446c3962015-09-14 10:57:35 -07003625 for (offload_uc_index = 0; offload_uc_index < num_usecase; offload_uc_index++) {
3626 if (!(adev->offload_usecases_state & (0x1 << offload_uc_index))) {
3627 adev->offload_usecases_state |= 0x1 << offload_uc_index;
3628 ret_uc = offload_usecases[offload_uc_index];
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003629 break;
3630 }
3631 }
vivek mehta446c3962015-09-14 10:57:35 -07003632
3633 ALOGV("%s: offload usecase is %d", __func__, ret_uc);
3634 return ret_uc;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003635}
3636
3637static void free_offload_usecase(struct audio_device *adev,
3638 audio_usecase_t uc_id)
3639{
vivek mehta446c3962015-09-14 10:57:35 -07003640 unsigned int offload_uc_index;
Alexy Josephb1379942016-01-29 15:49:38 -08003641 unsigned int num_usecase = sizeof(offload_usecases)/sizeof(offload_usecases[0]);
vivek mehta446c3962015-09-14 10:57:35 -07003642
3643 if (!adev->multi_offload_enable)
3644 return;
3645
3646 for (offload_uc_index = 0; offload_uc_index < num_usecase; offload_uc_index++) {
3647 if (offload_usecases[offload_uc_index] == uc_id) {
3648 adev->offload_usecases_state &= ~(0x1 << offload_uc_index);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003649 break;
3650 }
3651 }
3652 ALOGV("%s: free offload usecase %d", __func__, uc_id);
3653}
3654
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003655static void *offload_thread_loop(void *context)
3656{
3657 struct stream_out *out = (struct stream_out *) context;
3658 struct listnode *item;
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08003659 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003660
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003661 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
Weiyin Jiangd9b5a4e2019-07-18 17:24:21 +08003662 set_sched_policy(0, SP_FOREGROUND);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003663 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
3664
3665 ALOGV("%s", __func__);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07003666 lock_output_stream(out);
juyuchen391b5fa2018-12-12 17:58:09 +08003667 out->offload_state = OFFLOAD_STATE_IDLE;
3668 out->playback_started = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003669 for (;;) {
3670 struct offload_cmd *cmd = NULL;
3671 stream_callback_event_t event;
3672 bool send_callback = false;
3673
3674 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
3675 __func__, list_empty(&out->offload_cmd_list),
3676 out->offload_state);
3677 if (list_empty(&out->offload_cmd_list)) {
3678 ALOGV("%s SLEEPING", __func__);
3679 pthread_cond_wait(&out->offload_cond, &out->lock);
3680 ALOGV("%s RUNNING", __func__);
3681 continue;
3682 }
3683
3684 item = list_head(&out->offload_cmd_list);
3685 cmd = node_to_item(item, struct offload_cmd, node);
3686 list_remove(item);
3687
3688 ALOGVV("%s STATE %d CMD %d out->compr %p",
3689 __func__, out->offload_state, cmd->cmd, out->compr);
3690
3691 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
3692 free(cmd);
3693 break;
3694 }
3695
Haynes Mathew Georgeee5836f2017-11-21 18:02:10 -08003696 // allow OFFLOAD_CMD_ERROR reporting during standby
3697 // this is needed to handle failures during compress_open
3698 // Note however that on a pause timeout, the stream is closed
3699 // and no offload usecase will be active. Therefore this
3700 // special case is needed for compress_open failures alone
3701 if (cmd->cmd != OFFLOAD_CMD_ERROR &&
3702 out->compr == NULL) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003703 ALOGE("%s: Compress handle is NULL", __func__);
Haynes Mathew Georgea9abb202016-06-02 14:13:20 -07003704 free(cmd);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003705 pthread_cond_signal(&out->cond);
3706 continue;
3707 }
3708 out->offload_thread_blocked = true;
3709 pthread_mutex_unlock(&out->lock);
3710 send_callback = false;
3711 switch(cmd->cmd) {
3712 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07003713 ALOGD("copl(%p):calling compress_wait", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003714 compress_wait(out->compr, -1);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07003715 ALOGD("copl(%p):out of compress_wait", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003716 send_callback = true;
3717 event = STREAM_CBK_EVENT_WRITE_READY;
3718 break;
3719 case OFFLOAD_CMD_PARTIAL_DRAIN:
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08003720 ret = compress_next_track(out->compr);
Sidipotu Ashok55820562014-02-10 16:16:38 +05303721 if(ret == 0) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07003722 ALOGD("copl(%p):calling compress_partial_drain", out);
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05303723 ret = compress_partial_drain(out->compr);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07003724 ALOGD("copl(%p):out of compress_partial_drain", out);
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05303725 if (ret < 0)
3726 ret = -errno;
Sidipotu Ashok55820562014-02-10 16:16:38 +05303727 }
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05303728 else if (ret == -ETIMEDOUT)
Aniket Kumar Lata3570fb12017-11-08 15:53:44 -08003729 ret = compress_drain(out->compr);
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08003730 else
3731 ALOGE("%s: Next track returned error %d",__func__, ret);
Aniket Kumar Lata3570fb12017-11-08 15:53:44 -08003732 if (-ENETRESET != ret && !(-EINTR == ret &&
3733 CARD_STATUS_OFFLINE == out->card_status)) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05303734 send_callback = true;
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05303735 pthread_mutex_lock(&out->lock);
3736 out->send_new_metadata = 1;
3737 out->send_next_track_params = true;
3738 pthread_mutex_unlock(&out->lock);
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05303739 event = STREAM_CBK_EVENT_DRAIN_READY;
3740 ALOGV("copl(%p):send drain callback, ret %d", out, ret);
3741 } else
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05303742 ALOGI("%s: Block drain ready event during SSR", __func__);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003743 break;
3744 case OFFLOAD_CMD_DRAIN:
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07003745 ALOGD("copl(%p):calling compress_drain", out);
Aniket Kumar Lata3570fb12017-11-08 15:53:44 -08003746 ret = compress_drain(out->compr);
3747 ALOGD("copl(%p):out of compress_drain", out);
3748 // EINTR check avoids drain interruption due to SSR
3749 if (-ENETRESET != ret && !(-EINTR == ret &&
3750 CARD_STATUS_OFFLINE == out->card_status)) {
3751 send_callback = true;
3752 event = STREAM_CBK_EVENT_DRAIN_READY;
3753 } else
3754 ALOGI("%s: Block drain ready event during SSR", __func__);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003755 break;
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05303756 case OFFLOAD_CMD_ERROR:
3757 ALOGD("copl(%p): sending error callback to AF", out);
3758 send_callback = true;
3759 event = STREAM_CBK_EVENT_ERROR;
3760 break;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003761 default:
3762 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
3763 break;
3764 }
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07003765 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003766 out->offload_thread_blocked = false;
3767 pthread_cond_signal(&out->cond);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08003768 if (send_callback && out->client_callback) {
3769 ALOGVV("%s: sending client_callback event %d", __func__, event);
3770 out->client_callback(event, NULL, out->client_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -07003771 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003772 free(cmd);
3773 }
3774
3775 pthread_cond_signal(&out->cond);
3776 while (!list_empty(&out->offload_cmd_list)) {
3777 item = list_head(&out->offload_cmd_list);
3778 list_remove(item);
3779 free(node_to_item(item, struct offload_cmd, node));
3780 }
3781 pthread_mutex_unlock(&out->lock);
3782
3783 return NULL;
3784}
3785
3786static int create_offload_callback_thread(struct stream_out *out)
3787{
3788 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
3789 list_init(&out->offload_cmd_list);
3790 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
3791 offload_thread_loop, out);
3792 return 0;
3793}
3794
3795static int destroy_offload_callback_thread(struct stream_out *out)
3796{
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07003797 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003798 stop_compressed_output_l(out);
3799 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
3800
3801 pthread_mutex_unlock(&out->lock);
3802 pthread_join(out->offload_thread, (void **) NULL);
3803 pthread_cond_destroy(&out->offload_cond);
3804
3805 return 0;
3806}
3807
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003808static int stop_output_stream(struct stream_out *out)
3809{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05303810 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003811 struct audio_usecase *uc_info;
3812 struct audio_device *adev = out->dev;
Aalique Grahame22e49102018-12-18 14:23:57 -08003813 bool has_voip_usecase =
3814 get_usecase_from_list(adev, USECASE_AUDIO_PLAYBACK_VOIP) != NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003815
Eric Laurent994a6932013-07-17 11:51:42 -07003816 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003817 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003818 uc_info = get_usecase_from_list(adev, out->usecase);
3819 if (uc_info == NULL) {
3820 ALOGE("%s: Could not find the usecase (%d) in the list",
3821 __func__, out->usecase);
3822 return -EINVAL;
3823 }
3824
Zhou Songbaddf9f2020-11-20 13:57:39 +08003825 out->a2dp_muted = false;
3826
Derek Chenea197282019-01-07 17:35:01 -08003827 if (audio_extn_ext_hw_plugin_usecase_stop(adev->ext_hw_plugin, uc_info))
3828 ALOGE("%s: failed to stop ext hw plugin", __func__);
Derek Chend2530072014-11-24 12:39:14 -08003829
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07003830 if (is_offload_usecase(out->usecase) &&
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05303831 !(audio_extn_passthru_is_passthrough_stream(out))) {
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08003832 if (adev->visualizer_stop_output != NULL)
3833 adev->visualizer_stop_output(out->handle, out->pcm_device_id);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08003834
3835 audio_extn_dts_remove_state_notifier_node(out->usecase);
3836
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08003837 if (adev->offload_effects_stop_output != NULL)
3838 adev->offload_effects_stop_output(out->handle, out->pcm_device_id);
vivek mehtad15d2bf2019-05-17 13:35:10 -07003839 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_ULL ||
3840 out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
3841 audio_low_latency_hint_end();
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08003842 }
Eric Laurentc4aef752013-09-12 17:45:53 -07003843
Arun Mirpuri2ce92a62018-09-12 18:36:10 -07003844 if (out->usecase == USECASE_INCALL_MUSIC_UPLINK ||
3845 out->usecase == USECASE_INCALL_MUSIC_UPLINK2) {
Arun Mirpurief53ce52018-09-11 18:00:09 -07003846 voice_set_device_mute_flag(adev, false);
Arun Mirpuri2ce92a62018-09-12 18:36:10 -07003847 }
Arun Mirpurief53ce52018-09-11 18:00:09 -07003848
Eric Laurent150dbfe2013-02-27 14:31:02 -08003849 /* 1. Get and set stream specific mixer controls */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07003850 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003851
3852 /* 2. Disable the rx device */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07003853 disable_snd_device(adev, uc_info->out_snd_device);
Zhou Songd9bd9302020-08-04 16:34:45 +08003854 if (out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS)
3855 disable_snd_device(adev, SND_DEVICE_OUT_HAPTICS);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003856
Aalique Grahame22e49102018-12-18 14:23:57 -08003857 audio_extn_extspk_update(adev->extspk);
3858
Xiaojun Sang785b5da2017-08-03 15:52:29 +08003859 if (is_offload_usecase(out->usecase)) {
3860 audio_enable_asm_bit_width_enforce_mode(adev->mixer,
3861 adev->dsp_bit_width_enforce_mode,
3862 false);
3863 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003864 if (is_usb_out_device_type(&out->device_list)) {
Garmond Leung5fd0b552018-04-17 11:56:12 -07003865 ret = audio_extn_usb_check_and_set_svc_int(uc_info,
3866 false);
3867
3868 if (ret != 0)
3869 check_usecases_codec_backend(adev, uc_info, uc_info->out_snd_device);
3870 /* default service interval was successfully updated,
3871 reopen USB backend with new service interval */
3872 ret = 0;
3873 }
Xiaojun Sang785b5da2017-08-03 15:52:29 +08003874
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08003875 list_remove(&uc_info->list);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303876 out->started = 0;
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07003877 if (is_offload_usecase(out->usecase) &&
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05303878 (audio_extn_passthru_is_passthrough_stream(out))) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07003879 ALOGV("Disable passthrough , reset mixer to pcm");
3880 /* NO_PASSTHROUGH */
Meng Wang4c32fb42020-01-16 17:57:11 +08003881#ifdef AUDIO_GKI_ENABLED
3882 /* out->compr_config.codec->reserved[0] is for compr_passthr */
3883 out->compr_config.codec->reserved[0] = 0;
3884#else
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07003885 out->compr_config.codec->compr_passthr = 0;
Meng Wang4c32fb42020-01-16 17:57:11 +08003886#endif
Mingming Yin21854652016-04-13 11:54:02 -07003887 audio_extn_passthru_on_stop(out);
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07003888 audio_extn_dolby_set_dap_bypass(adev, DAP_STATE_ON);
3889 }
Eric Laurent07eeafd2013-10-06 12:52:49 -07003890
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05303891 /* Must be called after removing the usecase from list */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003892 if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_AUX_DIGITAL))
Md Mansoor Ahmeddb1b4f92018-01-25 18:56:31 +05303893 audio_extn_keep_alive_start(KEEP_ALIVE_OUT_HDMI);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05303894
Manish Dewangan21a850a2017-08-14 12:03:55 +05303895 if (out->ip_hdlr_handle) {
Naresh Tanniru85819452017-05-04 18:55:45 -07003896 ret = audio_extn_ip_hdlr_intf_close(out->ip_hdlr_handle, true, out);
3897 if (ret < 0)
3898 ALOGE("%s: audio_extn_ip_hdlr_intf_close failed %d",__func__, ret);
3899 }
3900
Zhou Song642ec432020-12-23 16:11:10 +08003901 /* trigger voip input to reroute when voip output changes to hearing aid */
Aalique Grahame22e49102018-12-18 14:23:57 -08003902 if (has_voip_usecase ||
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003903 compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
Aalique Grahame22e49102018-12-18 14:23:57 -08003904 struct listnode *node;
3905 struct audio_usecase *usecase;
3906 list_for_each(node, &adev->usecase_list) {
3907 usecase = node_to_item(node, struct audio_usecase, list);
Zhou Song642ec432020-12-23 16:11:10 +08003908 if (usecase->type == PCM_PLAYBACK || usecase == uc_info ||
3909 (usecase->type == PCM_CAPTURE &&
Revathi Uddarajud9f23d92020-07-27 10:55:06 +05303910 usecase->id != USECASE_AUDIO_RECORD_VOIP &&
Zhou Song642ec432020-12-23 16:11:10 +08003911 usecase->id != USECASE_AUDIO_RECORD_VOIP_LOW_LATENCY))
Aalique Grahame22e49102018-12-18 14:23:57 -08003912 continue;
3913
3914 ALOGD("%s: select_devices at usecase(%d: %s) after removing the usecase(%d: %s)",
3915 __func__, usecase->id, use_case_table[usecase->id],
3916 out->usecase, use_case_table[out->usecase]);
3917 select_devices(adev, usecase->id);
3918 }
3919 }
3920
Garmond Leung5fd0b552018-04-17 11:56:12 -07003921 free(uc_info);
Eric Laurent994a6932013-07-17 11:51:42 -07003922 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003923 return ret;
3924}
3925
Vignesh Kulothungana6927272019-02-20 15:17:07 -08003926struct pcm* pcm_open_prepare_helper(unsigned int snd_card, unsigned int pcm_device_id,
3927 unsigned int flags, unsigned int pcm_open_retry_count,
3928 struct pcm_config *config)
3929{
3930 struct pcm* pcm = NULL;
3931
3932 while (1) {
3933 pcm = pcm_open(snd_card, pcm_device_id, flags, config);
3934 if (pcm == NULL || !pcm_is_ready(pcm)) {
3935 ALOGE("%s: %s", __func__, pcm_get_error(pcm));
3936 if (pcm != NULL) {
3937 pcm_close(pcm);
3938 pcm = NULL;
3939 }
Weiyin Jiang72197252019-10-09 11:49:32 +08003940 if (pcm_open_retry_count == 0)
Vignesh Kulothungana6927272019-02-20 15:17:07 -08003941 return NULL;
3942
Weiyin Jiang72197252019-10-09 11:49:32 +08003943 pcm_open_retry_count--;
Vignesh Kulothungana6927272019-02-20 15:17:07 -08003944 usleep(PROXY_OPEN_WAIT_TIME * 1000);
3945 continue;
3946 }
3947 break;
3948 }
3949
3950 if (pcm_is_ready(pcm)) {
3951 int ret = pcm_prepare(pcm);
3952 if (ret < 0) {
3953 ALOGE("%s: pcm_prepare returned %d", __func__, ret);
3954 pcm_close(pcm);
3955 pcm = NULL;
3956 }
3957 }
3958
3959 return pcm;
3960}
3961
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003962int start_output_stream(struct stream_out *out)
3963{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003964 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003965 struct audio_usecase *uc_info;
3966 struct audio_device *adev = out->dev;
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08003967 char mixer_ctl_name[128];
3968 struct mixer_ctl *ctl = NULL;
3969 char* perf_mode[] = {"ULL", "ULL_PP", "LL"};
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303970 bool a2dp_combo = false;
Vignesh Kulothungana6927272019-02-20 15:17:07 -08003971 bool is_haptic_usecase = (out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) ? true: false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003972
Haynes Mathew George380745d2017-10-04 15:27:45 -07003973 ATRACE_BEGIN("start_output_stream");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07003974 if ((out->usecase < 0) || (out->usecase >= AUDIO_USECASE_MAX)) {
3975 ret = -EINVAL;
3976 goto error_config;
3977 }
3978
Vignesh Kulothungana6927272019-02-20 15:17:07 -08003979 ALOGD("%s: enter: stream(%p)usecase(%d: %s) devices(%#x) is_haptic_usecase(%d)",
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05303980 __func__, &out->stream, out->usecase, use_case_table[out->usecase],
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003981 get_device_types(&out->device_list), is_haptic_usecase);
3982
3983 bool is_speaker_active = compare_device_type(&out->device_list,
3984 AUDIO_DEVICE_OUT_SPEAKER);
3985 bool is_speaker_safe_active = compare_device_type(&out->device_list,
3986 AUDIO_DEVICE_OUT_SPEAKER_SAFE);
Naresh Tanniru4c630392014-05-12 01:05:52 +05303987
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05303988 if (CARD_STATUS_OFFLINE == out->card_status ||
3989 CARD_STATUS_OFFLINE == adev->card_status) {
3990 ALOGW("out->card_status or adev->card_status offline, try again");
Dhanalakshmi Siddani4d57e992014-07-17 16:37:51 +05303991 ret = -EIO;
Mingshu Pang5fc696f2020-02-28 12:32:00 +08003992 goto error_fatal;
Naresh Tanniru4c630392014-05-12 01:05:52 +05303993 }
Naresh Tanniru4c630392014-05-12 01:05:52 +05303994
Arun Mirpuri2ce92a62018-09-12 18:36:10 -07003995 //Update incall music usecase to reflect correct voice session
3996 if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
3997 ret = voice_extn_check_and_set_incall_music_usecase(adev, out);
3998 if (ret != 0) {
3999 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
4000 __func__, ret);
4001 goto error_config;
4002 }
4003 }
4004
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004005 if (is_a2dp_out_device_type(&out->device_list)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02004006 if (!audio_extn_a2dp_source_is_ready()) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004007 if (is_speaker_active || is_speaker_safe_active) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304008 a2dp_combo = true;
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05304009 } else {
Zhou Songd01e7a22020-09-23 22:49:01 +08004010 if (!is_offload_usecase(out->usecase)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304011 ALOGE("%s: A2DP profile is not ready, return error", __func__);
4012 ret = -EAGAIN;
4013 goto error_config;
4014 }
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05304015 }
4016 }
4017 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004018 if (is_sco_out_device_type(&out->device_list)) {
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304019 if (!adev->bt_sco_on) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004020 if (is_speaker_active) {
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304021 //combo usecase just by pass a2dp
4022 ALOGW("%s: SCO is not connected, route it to speaker", __func__);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004023 reassign_device_list(&out->device_list, AUDIO_DEVICE_OUT_SPEAKER, "");
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304024 } else {
4025 ALOGE("%s: SCO profile is not ready, return error", __func__);
4026 ret = -EAGAIN;
4027 goto error_config;
4028 }
4029 }
4030 }
4031
Eric Laurentb23d5282013-05-14 15:27:20 -07004032 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004033 if (out->pcm_device_id < 0) {
4034 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
4035 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08004036 ret = -EINVAL;
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004037 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004038 }
4039
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004040 if (is_haptic_usecase) {
Meng Wang51d8c2a2020-04-27 15:23:21 +08004041 adev->haptic_pcm_device_id = platform_get_pcm_device_id(
4042 USECASE_AUDIO_PLAYBACK_HAPTICS, PCM_PLAYBACK);
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004043 if (adev->haptic_pcm_device_id < 0) {
4044 ALOGE("%s: Invalid Haptics pcm device id(%d) for the usecase(%d)",
4045 __func__, adev->haptic_pcm_device_id, out->usecase);
4046 ret = -EINVAL;
4047 goto error_config;
4048 }
4049 }
4050
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004051 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07004052
4053 if (!uc_info) {
4054 ret = -ENOMEM;
4055 goto error_config;
4056 }
4057
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004058 uc_info->id = out->usecase;
4059 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08004060 uc_info->stream.out = out;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004061 list_init(&uc_info->device_list);
4062 assign_devices(&uc_info->device_list, &out->device_list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07004063 uc_info->in_snd_device = SND_DEVICE_NONE;
4064 uc_info->out_snd_device = SND_DEVICE_NONE;
Garmond Leung5fd0b552018-04-17 11:56:12 -07004065
4066 /* This must be called before adding this usecase to the list */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004067 if (is_usb_out_device_type(&out->device_list)) {
Garmond Leung5fd0b552018-04-17 11:56:12 -07004068 audio_extn_usb_check_and_set_svc_int(uc_info, true);
4069 /* USB backend is not reopened immediately.
4070 This is eventually done as part of select_devices */
4071 }
4072
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08004073 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004074
Wei Wangf7ca6c92017-11-21 14:51:20 -08004075 audio_streaming_hint_start();
Sudheer Papothifa9d2282015-09-17 01:53:25 +05304076 audio_extn_perf_lock_acquire(&adev->perf_lock_handle, 0,
4077 adev->perf_lock_opts,
4078 adev->perf_lock_opts_size);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05304079
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004080 if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
Md Mansoor Ahmeddb1b4f92018-01-25 18:56:31 +05304081 audio_extn_keep_alive_stop(KEEP_ALIVE_OUT_HDMI);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05304082 if (audio_extn_passthru_is_enabled() &&
4083 audio_extn_passthru_is_passthrough_stream(out)) {
4084 audio_extn_passthru_on_start(out);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05304085 }
4086 }
4087
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004088 if (is_a2dp_out_device_type(&out->device_list) &&
Florian Pfister1a84f312018-07-19 14:38:18 +02004089 (!audio_extn_a2dp_source_is_ready())) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304090 if (!a2dp_combo) {
4091 check_a2dp_restore_l(adev, out, false);
4092 } else {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004093 struct listnode dev;
4094 list_init(&dev);
4095 assign_devices(&dev, &out->device_list);
4096 if (compare_device_type(&dev, AUDIO_DEVICE_OUT_SPEAKER_SAFE))
4097 reassign_device_list(&out->device_list,
4098 AUDIO_DEVICE_OUT_SPEAKER_SAFE, "");
Aalique Grahame22e49102018-12-18 14:23:57 -08004099 else
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004100 reassign_device_list(&out->device_list,
4101 AUDIO_DEVICE_OUT_SPEAKER, "");
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304102 select_devices(adev, out->usecase);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004103 assign_devices(&out->device_list, &dev);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304104 }
4105 } else {
Revathi Uddarajub26e3932020-06-10 14:51:02 +05304106 select_devices(adev, out->usecase);
4107 if (is_a2dp_out_device_type(&out->device_list) &&
4108 !adev->a2dp_started) {
4109 if (is_speaker_active || is_speaker_safe_active) {
4110 struct listnode dev;
4111 list_init(&dev);
4112 assign_devices(&dev, &out->device_list);
4113 if (compare_device_type(&dev, AUDIO_DEVICE_OUT_SPEAKER_SAFE))
4114 reassign_device_list(&out->device_list,
4115 AUDIO_DEVICE_OUT_SPEAKER_SAFE, "");
4116 else
4117 reassign_device_list(&out->device_list,
4118 AUDIO_DEVICE_OUT_SPEAKER, "");
4119 select_devices(adev, out->usecase);
4120 assign_devices(&out->device_list, &dev);
4121 } else {
4122 ret = -EINVAL;
4123 goto error_open;
4124 }
4125 }
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304126 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07004127
Arun Mirpuri2ce92a62018-09-12 18:36:10 -07004128 if (out->usecase == USECASE_INCALL_MUSIC_UPLINK ||
4129 out->usecase == USECASE_INCALL_MUSIC_UPLINK2) {
Arun Mirpurief53ce52018-09-11 18:00:09 -07004130 voice_set_device_mute_flag(adev, true);
Arun Mirpuri2ce92a62018-09-12 18:36:10 -07004131 }
Arun Mirpurief53ce52018-09-11 18:00:09 -07004132
Derek Chenea197282019-01-07 17:35:01 -08004133 if (audio_extn_ext_hw_plugin_usecase_start(adev->ext_hw_plugin, uc_info))
4134 ALOGE("%s: failed to start ext hw plugin", __func__);
Derek Chend2530072014-11-24 12:39:14 -08004135
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07004136 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d) format(%#x)",
4137 __func__, adev->snd_card, out->pcm_device_id, out->config.format);
Haynes Mathew George16081042017-05-31 17:16:49 -07004138
4139 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
Arun Mirpuri5d170872019-03-26 13:21:31 -07004140 ALOGD("%s: Starting MMAP stream", __func__);
Haynes Mathew George16081042017-05-31 17:16:49 -07004141 if (out->pcm == NULL || !pcm_is_ready(out->pcm)) {
4142 ALOGE("%s: pcm stream not ready", __func__);
4143 goto error_open;
4144 }
4145 ret = pcm_start(out->pcm);
4146 if (ret < 0) {
4147 ALOGE("%s: MMAP pcm_start failed ret %d", __func__, ret);
4148 goto error_open;
4149 }
Arun Mirpuri5d170872019-03-26 13:21:31 -07004150 out_set_mmap_volume(&out->stream, out->volume_l, out->volume_r);
Haynes Mathew George16081042017-05-31 17:16:49 -07004151 } else if (!is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07004152 unsigned int flags = PCM_OUT;
4153 unsigned int pcm_open_retry_count = 0;
4154 if (out->usecase == USECASE_AUDIO_PLAYBACK_AFE_PROXY) {
4155 flags |= PCM_MMAP | PCM_NOIRQ;
4156 pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT;
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004157 } else if (out->realtime) {
Haynes Mathew George4ab3ba92017-12-11 14:49:43 -08004158 flags |= PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07004159 } else
4160 flags |= PCM_MONOTONIC;
4161
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08004162 if ((adev->vr_audio_mode_enabled) &&
4163 (out->flags & AUDIO_OUTPUT_FLAG_RAW)) {
4164 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
4165 "PCM_Dev %d Topology", out->pcm_device_id);
4166 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
4167 if (!ctl) {
4168 ALOGI("%s: Could not get ctl for mixer cmd might be ULL - %s",
4169 __func__, mixer_ctl_name);
4170 } else {
4171 //if success use ULLPP
4172 ALOGI("%s: mixer ctrl %s succeeded setting up ULL for %d",
4173 __func__, mixer_ctl_name, out->pcm_device_id);
4174 //There is a still a possibility that some sessions
4175 // that request for FAST|RAW when 3D audio is active
4176 //can go through ULLPP. Ideally we expects apps to
4177 //listen to audio focus and stop concurrent playback
4178 //Also, we will look for mode flag (voice_in_communication)
4179 //before enabling the realtime flag.
4180 mixer_ctl_set_enum_by_string(ctl, perf_mode[1]);
4181 }
4182 }
4183
Vatsal Bucha3f39f222021-06-29 16:16:55 +05304184 platform_set_stream_channel_map(adev->platform, out->channel_mask,
4185 out->pcm_device_id, -1, &out->channel_map_param.channel_map[0]);
Surendar Karka91fa3682018-07-02 18:12:12 +05304186
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004187 out->pcm = pcm_open_prepare_helper(adev->snd_card, out->pcm_device_id,
4188 flags, pcm_open_retry_count,
4189 &(out->config));
4190 if (out->pcm == NULL) {
4191 ret = -EIO;
4192 goto error_open;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004193 }
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004194
4195 if (is_haptic_usecase) {
4196 adev->haptic_pcm = pcm_open_prepare_helper(adev->snd_card,
4197 adev->haptic_pcm_device_id,
4198 flags, pcm_open_retry_count,
4199 &(adev->haptics_config));
4200 // failure to open haptics pcm shouldnt stop audio,
4201 // so do not close audio pcm in case of error
Vignesh Kulothungane4039c12019-05-07 15:51:39 -07004202
4203 if (property_get_bool("vendor.audio.enable_haptic_audio_sync", false)) {
4204 ALOGD("%s: enable haptic audio synchronization", __func__);
4205 platform_set_qtime(adev->platform, out->pcm_device_id, adev->haptic_pcm_device_id);
4206 }
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004207 }
4208
Zhou Song2b8f28f2017-09-11 10:51:38 +08004209 // apply volume for voip playback after path is set up
4210 if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP)
4211 out_set_voip_volume(&out->stream, out->volume_l, out->volume_r);
Ramu Gottipati36547092018-12-28 11:32:09 +05304212 else if ((out->usecase == USECASE_AUDIO_PLAYBACK_LOW_LATENCY || out->usecase == USECASE_AUDIO_PLAYBACK_DEEP_BUFFER ||
4213 out->usecase == USECASE_AUDIO_PLAYBACK_ULL) && (out->apply_volume)) {
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05304214 out_set_pcm_volume(&out->stream, out->volume_l, out->volume_r);
4215 out->apply_volume = false;
Derek Chenf13dd492018-11-13 14:53:51 -08004216 } else if (audio_extn_auto_hal_is_bus_device_usecase(out->usecase)) {
4217 out_set_pcm_volume(&out->stream, out->volume_l, out->volume_r);
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05304218 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004219 } else {
Zhou Song79929fe2020-01-20 17:57:43 +08004220 /*
4221 * set custom channel map if:
4222 * 1. neither mono nor stereo clips i.e. channels > 2 OR
4223 * 2. custom channel map has been set by client
4224 * else default channel map of FC/FR/FL can always be set to DSP
4225 */
4226 if (popcount(out->channel_mask) > 2 || out->channel_map_param.channel_map[0])
4227 platform_set_stream_channel_map(adev->platform, out->channel_mask,
Weiyin Jiang810a80d2021-01-18 16:04:31 +08004228 out->pcm_device_id, -1, &out->channel_map_param.channel_map[0]);
Xiaojun Sang785b5da2017-08-03 15:52:29 +08004229 audio_enable_asm_bit_width_enforce_mode(adev->mixer,
4230 adev->dsp_bit_width_enforce_mode,
4231 true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004232 out->pcm = NULL;
Haynes Mathew George380745d2017-10-04 15:27:45 -07004233 ATRACE_BEGIN("compress_open");
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08004234 out->compr = compress_open(adev->snd_card,
4235 out->pcm_device_id,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004236 COMPRESS_IN, &out->compr_config);
Haynes Mathew George380745d2017-10-04 15:27:45 -07004237 ATRACE_END();
Satish Babu Patakokila54ce83d2018-07-06 18:00:37 +05304238 if (errno == ENETRESET && !is_compress_ready(out->compr)) {
Sharad Sanglec6f32552018-05-04 16:15:38 +05304239 ALOGE("%s: compress_open failed errno:%d\n", __func__, errno);
4240 adev->card_status = CARD_STATUS_OFFLINE;
4241 out->card_status = CARD_STATUS_OFFLINE;
4242 ret = -EIO;
4243 goto error_open;
4244 }
4245
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004246 if (out->compr && !is_compress_ready(out->compr)) {
Haynes Mathew Georgeee5836f2017-11-21 18:02:10 -08004247 ALOGE("%s: failed /w error %s", __func__, compress_get_error(out->compr));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004248 compress_close(out->compr);
4249 out->compr = NULL;
4250 ret = -EIO;
4251 goto error_open;
4252 }
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05304253 /* compress_open sends params of the track, so reset the flag here */
4254 out->is_compr_metadata_avail = false;
4255
Ben Rombergerd771a7c2017-02-22 18:05:17 -08004256 if (out->client_callback)
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004257 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07004258
Fred Oh3f43e742015-03-04 18:42:34 -08004259 /* Since small bufs uses blocking writes, a write will be blocked
4260 for the default max poll time (20s) in the event of an SSR.
4261 Reduce the poll time to observe and deal with SSR faster.
4262 */
Ashish Jain5106d362016-05-11 19:23:33 +05304263 if (!out->non_blocking) {
Fred Oh3f43e742015-03-04 18:42:34 -08004264 compress_set_max_poll_wait(out->compr, 1000);
4265 }
4266
Manish Dewangan69426c82017-01-30 17:35:36 +05304267 audio_extn_utils_compress_set_render_mode(out);
Manish Dewangan58229382017-02-02 15:48:41 +05304268 audio_extn_utils_compress_set_clk_rec_mode(uc_info);
Manish Dewangan69426c82017-01-30 17:35:36 +05304269
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08004270 audio_extn_dts_create_state_notifier_node(out->usecase);
4271 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
4272 popcount(out->channel_mask),
4273 out->playback_started);
4274
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08004275#ifdef DS1_DOLBY_DDP_ENABLED
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05304276 if (audio_extn_utils_is_dolby_format(out->format))
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08004277 audio_extn_dolby_send_ddp_endp_params(adev);
4278#endif
Preetam Singh Ranawatd18d8832017-02-08 17:34:54 +05304279 if (!(audio_extn_passthru_is_passthrough_stream(out)) &&
4280 (out->sample_rate != 176400 && out->sample_rate <= 192000)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004281 if (adev->visualizer_start_output != NULL)
4282 adev->visualizer_start_output(out->handle, out->pcm_device_id);
4283 if (adev->offload_effects_start_output != NULL)
Ashish Jain5106d362016-05-11 19:23:33 +05304284 adev->offload_effects_start_output(out->handle, out->pcm_device_id, adev->mixer);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08004285 audio_extn_check_and_set_dts_hpx_state(adev);
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004286 }
Derek Chenf13dd492018-11-13 14:53:51 -08004287
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004288 if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_BUS)) {
Derek Chenf13dd492018-11-13 14:53:51 -08004289 /* Update cached volume from media to offload/direct stream */
4290 struct listnode *node = NULL;
4291 list_for_each(node, &adev->active_outputs_list) {
4292 streams_output_ctxt_t *out_ctxt = node_to_item(node,
4293 streams_output_ctxt_t,
4294 list);
4295 if (out_ctxt->output->usecase == USECASE_AUDIO_PLAYBACK_MEDIA) {
4296 out->volume_l = out_ctxt->output->volume_l;
4297 out->volume_r = out_ctxt->output->volume_r;
4298 }
4299 }
4300 out_set_compr_volume(&out->stream,
4301 out->volume_l, out->volume_r);
4302 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004303 }
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004304
4305 if (ret == 0) {
4306 register_out_stream(out);
4307 if (out->realtime) {
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07004308 if (out->pcm == NULL || !pcm_is_ready(out->pcm)) {
4309 ALOGE("%s: pcm stream not ready", __func__);
4310 goto error_open;
4311 }
Haynes Mathew George380745d2017-10-04 15:27:45 -07004312 ATRACE_BEGIN("pcm_start");
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004313 ret = pcm_start(out->pcm);
Haynes Mathew George380745d2017-10-04 15:27:45 -07004314 ATRACE_END();
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004315 if (ret < 0)
4316 goto error_open;
4317 }
4318 }
Wei Wangf7ca6c92017-11-21 14:51:20 -08004319 audio_streaming_hint_end();
Sudheer Papothifa9d2282015-09-17 01:53:25 +05304320 audio_extn_perf_lock_release(&adev->perf_lock_handle);
Jaideep Sharma0fa53812020-09-17 09:00:11 +05304321 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07004322
vivek mehtad15d2bf2019-05-17 13:35:10 -07004323 if (out->usecase == USECASE_AUDIO_PLAYBACK_ULL ||
4324 out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
4325 audio_low_latency_hint_start();
4326 }
4327
Manish Dewangan21a850a2017-08-14 12:03:55 +05304328 if (out->ip_hdlr_handle) {
Vidyakumar Athota6d655882017-05-22 18:26:24 -07004329 ret = audio_extn_ip_hdlr_intf_open(out->ip_hdlr_handle, true, out, out->usecase);
Naresh Tanniru85819452017-05-04 18:55:45 -07004330 if (ret < 0)
4331 ALOGE("%s: audio_extn_ip_hdlr_intf_open failed %d",__func__, ret);
4332 }
4333
Vignesh Kulothungan3b5fae52017-09-25 12:16:30 -07004334 // consider a scenario where on pause lower layers are tear down.
4335 // so on resume, swap mixer control need to be sent only when
4336 // backend is active, hence rather than sending from enable device
4337 // sending it from start of streamtream
4338
4339 platform_set_swap_channels(adev, true);
4340
Haynes Mathew George380745d2017-10-04 15:27:45 -07004341 ATRACE_END();
Vatsal Buchac09ae062018-11-14 13:25:08 +05304342 enable_gcov();
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004343 return ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004344error_open:
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004345 if (adev->haptic_pcm) {
4346 pcm_close(adev->haptic_pcm);
4347 adev->haptic_pcm = NULL;
4348 }
Wei Wangf7ca6c92017-11-21 14:51:20 -08004349 audio_streaming_hint_end();
Sudheer Papothifa9d2282015-09-17 01:53:25 +05304350 audio_extn_perf_lock_release(&adev->perf_lock_handle);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004351 stop_output_stream(out);
Mingshu Pang5fc696f2020-02-28 12:32:00 +08004352error_fatal:
Laxminath Kasam2cb4b752015-09-24 03:59:15 +05304353 /*
4354 * sleep 50ms to allow sufficient time for kernel
4355 * drivers to recover incases like SSR.
4356 */
4357 usleep(50000);
Mingshu Pang5fc696f2020-02-28 12:32:00 +08004358error_config:
Haynes Mathew George380745d2017-10-04 15:27:45 -07004359 ATRACE_END();
Vatsal Buchac09ae062018-11-14 13:25:08 +05304360 enable_gcov();
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08004361 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004362}
4363
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004364static int check_input_parameters(uint32_t sample_rate,
4365 audio_format_t format,
Aalique Grahame22e49102018-12-18 14:23:57 -08004366 int channel_count,
4367 bool is_usb_hifi)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004368{
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08004369 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004370
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05304371 if (((format != AUDIO_FORMAT_PCM_16_BIT) && (format != AUDIO_FORMAT_PCM_8_24_BIT) &&
4372 (format != AUDIO_FORMAT_PCM_24_BIT_PACKED) && (format != AUDIO_FORMAT_PCM_32_BIT) &&
4373 (format != AUDIO_FORMAT_PCM_FLOAT)) &&
Mingming Yine62d7842013-10-25 16:26:03 -07004374 !voice_extn_compress_voip_is_format_supported(format) &&
Ralf Herzaec80262018-07-03 07:08:49 +02004375 !audio_extn_compr_cap_format_supported(format) &&
4376 !audio_extn_cin_format_supported(format))
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004377 ret = -EINVAL;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08004378
Aalique Grahame22e49102018-12-18 14:23:57 -08004379 int max_channel_count = is_usb_hifi ? MAX_HIFI_CHANNEL_COUNT : MAX_CHANNEL_COUNT;
4380 if ((channel_count < MIN_CHANNEL_COUNT) || (channel_count > max_channel_count)) {
4381 ALOGE("%s: unsupported channel count (%d) passed Min / Max (%d / %d)", __func__,
4382 channel_count, MIN_CHANNEL_COUNT, max_channel_count);
4383 return -EINVAL;
4384 }
4385
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08004386 switch (channel_count) {
4387 case 1:
4388 case 2:
Chaithanya Krishna Bacharaju9955b162016-05-25 16:25:53 +05304389 case 3:
4390 case 4:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08004391 case 6:
Karthikeyan Mani07faa602018-08-20 11:01:32 -07004392 case 8:
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05304393 case 10:
4394 case 12:
4395 case 14:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08004396 break;
4397 default:
4398 ret = -EINVAL;
4399 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004400
4401 switch (sample_rate) {
4402 case 8000:
4403 case 11025:
4404 case 12000:
4405 case 16000:
4406 case 22050:
4407 case 24000:
4408 case 32000:
4409 case 44100:
4410 case 48000:
Haynes Mathew Georgec9253d12017-12-13 15:58:28 -08004411 case 88200:
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05304412 case 96000:
Haynes Mathew Georgec9253d12017-12-13 15:58:28 -08004413 case 176400:
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05304414 case 192000:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004415 break;
4416 default:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08004417 ret = -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004418 }
4419
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08004420 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004421}
4422
Naresh Tanniru04f71882018-06-26 17:46:22 +05304423
4424/** Add a value in a list if not already present.
4425 * @return true if value was successfully inserted or already present,
4426 * false if the list is full and does not contain the value.
4427 */
4428static bool register_uint(uint32_t value, uint32_t* list, size_t list_length) {
4429 for (size_t i = 0; i < list_length; i++) {
4430 if (list[i] == value) return true; // value is already present
4431 if (list[i] == 0) { // no values in this slot
4432 list[i] = value;
4433 return true; // value inserted
4434 }
4435 }
4436 return false; // could not insert value
4437}
4438
4439/** Add channel_mask in supported_channel_masks if not already present.
4440 * @return true if channel_mask was successfully inserted or already present,
4441 * false if supported_channel_masks is full and does not contain channel_mask.
4442 */
4443static void register_channel_mask(audio_channel_mask_t channel_mask,
4444 audio_channel_mask_t supported_channel_masks[static MAX_SUPPORTED_CHANNEL_MASKS]) {
4445 ALOGE_IF(!register_uint(channel_mask, supported_channel_masks, MAX_SUPPORTED_CHANNEL_MASKS),
4446 "%s: stream can not declare supporting its channel_mask %x", __func__, channel_mask);
4447}
4448
4449/** Add format in supported_formats if not already present.
4450 * @return true if format was successfully inserted or already present,
4451 * false if supported_formats is full and does not contain format.
4452 */
4453static void register_format(audio_format_t format,
4454 audio_format_t supported_formats[static MAX_SUPPORTED_FORMATS]) {
4455 ALOGE_IF(!register_uint(format, supported_formats, MAX_SUPPORTED_FORMATS),
4456 "%s: stream can not declare supporting its format %x", __func__, format);
4457}
4458/** Add sample_rate in supported_sample_rates if not already present.
4459 * @return true if sample_rate was successfully inserted or already present,
4460 * false if supported_sample_rates is full and does not contain sample_rate.
4461 */
4462static void register_sample_rate(uint32_t sample_rate,
4463 uint32_t supported_sample_rates[static MAX_SUPPORTED_SAMPLE_RATES]) {
4464 ALOGE_IF(!register_uint(sample_rate, supported_sample_rates, MAX_SUPPORTED_SAMPLE_RATES),
4465 "%s: stream can not declare supporting its sample rate %x", __func__, sample_rate);
4466}
4467
Karthikeyan Manib38769c2018-11-07 15:44:13 -08004468static inline uint32_t lcm(uint32_t num1, uint32_t num2)
4469{
4470 uint32_t high = num1, low = num2, temp = 0;
4471
4472 if (!num1 || !num2)
4473 return 0;
4474
4475 if (num1 < num2) {
4476 high = num2;
4477 low = num1;
4478 }
4479
4480 while (low != 0) {
4481 temp = low;
4482 low = high % low;
4483 high = temp;
4484 }
4485 return (num1 * num2)/high;
4486}
4487
4488static inline uint32_t nearest_multiple(uint32_t num, uint32_t multiplier)
4489{
4490 uint32_t remainder = 0;
4491
4492 if (!multiplier)
4493 return num;
4494
4495 remainder = num % multiplier;
4496 if (remainder)
4497 num += (multiplier - remainder);
4498
4499 return num;
4500}
4501
Aalique Grahame22e49102018-12-18 14:23:57 -08004502static size_t get_stream_buffer_size(size_t duration_ms,
4503 uint32_t sample_rate,
4504 audio_format_t format,
4505 int channel_count,
4506 bool is_low_latency)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004507{
4508 size_t size = 0;
Karthikeyan Manib38769c2018-11-07 15:44:13 -08004509 uint32_t bytes_per_period_sample = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004510
Aalique Grahame22e49102018-12-18 14:23:57 -08004511 size = (sample_rate * duration_ms) / 1000;
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07004512 if (is_low_latency)
4513 size = configured_low_latency_capture_period_size;
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05304514
Karthikeyan Manib38769c2018-11-07 15:44:13 -08004515 bytes_per_period_sample = audio_bytes_per_sample(format) * channel_count;
Aalique Grahame22e49102018-12-18 14:23:57 -08004516 size *= audio_bytes_per_sample(format) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004517
Ralf Herzbd08d632018-09-28 15:50:49 +02004518 /* make sure the size is multiple of 32 bytes and additionally multiple of
4519 * the frame_size (required for 24bit samples and non-power-of-2 channel counts)
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07004520 * At 48 kHz mono 16-bit PCM:
4521 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
4522 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
Karthikeyan Manib38769c2018-11-07 15:44:13 -08004523 * Also, make sure the size is multiple of bytes per period sample
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07004524 */
Karthikeyan Manib38769c2018-11-07 15:44:13 -08004525 size = nearest_multiple(size, lcm(32, bytes_per_period_sample));
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07004526
4527 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004528}
4529
Aalique Grahame22e49102018-12-18 14:23:57 -08004530static size_t get_input_buffer_size(uint32_t sample_rate,
4531 audio_format_t format,
4532 int channel_count,
4533 bool is_low_latency)
4534{
4535 /* Don't know if USB HIFI in this context so use true to be conservative */
4536 if (check_input_parameters(sample_rate, format, channel_count,
4537 true /*is_usb_hifi */) != 0)
4538 return 0;
4539
4540 return get_stream_buffer_size(AUDIO_CAPTURE_PERIOD_DURATION_MSEC,
4541 sample_rate,
4542 format,
4543 channel_count,
4544 is_low_latency);
4545}
4546
Derek Chenf6318be2017-06-12 17:16:24 -04004547size_t get_output_period_size(uint32_t sample_rate,
4548 audio_format_t format,
4549 int channel_count,
4550 int duration /*in millisecs*/)
Ashish Jain058165c2016-09-28 23:18:48 +05304551{
4552 size_t size = 0;
4553 uint32_t bytes_per_sample = audio_bytes_per_sample(format);
4554
4555 if ((duration == 0) || (sample_rate == 0) ||
4556 (bytes_per_sample == 0) || (channel_count == 0)) {
4557 ALOGW("Invalid config duration %d sr %d bps %d ch %d", duration, sample_rate,
4558 bytes_per_sample, channel_count);
4559 return -EINVAL;
4560 }
4561
4562 size = (sample_rate *
4563 duration *
4564 bytes_per_sample *
4565 channel_count) / 1000;
4566 /*
4567 * To have same PCM samples for all channels, the buffer size requires to
4568 * be multiple of (number of channels * bytes per sample)
4569 * For writes to succeed, the buffer must be written at address which is multiple of 32
4570 */
4571 size = ALIGN(size, (bytes_per_sample * channel_count * 32));
4572
4573 return (size/(channel_count * bytes_per_sample));
4574}
4575
Zhou Song48453a02018-01-10 17:50:59 +08004576static uint64_t get_actual_pcm_frames_rendered(struct stream_out *out, struct timespec *timestamp)
Ashish Jain5106d362016-05-11 19:23:33 +05304577{
4578 uint64_t actual_frames_rendered = 0;
Weiyin Jiang4813da12020-05-28 00:37:28 +08004579 uint64_t written_frames = 0;
4580 uint64_t kernel_frames = 0;
4581 uint64_t dsp_frames = 0;
4582 uint64_t signed_frames = 0;
4583 size_t kernel_buffer_size = 0;
Ashish Jain5106d362016-05-11 19:23:33 +05304584
4585 /* This adjustment accounts for buffering after app processor.
4586 * It is based on estimated DSP latency per use case, rather than exact.
4587 */
George Gao9ba8a142020-07-23 14:30:03 -07004588 dsp_frames = platform_render_latency(out) *
Weiyin Jiang4813da12020-05-28 00:37:28 +08004589 out->sample_rate / 1000000LL;
Ashish Jain5106d362016-05-11 19:23:33 +05304590
Zhou Song48453a02018-01-10 17:50:59 +08004591 pthread_mutex_lock(&out->position_query_lock);
Weiyin Jiang4813da12020-05-28 00:37:28 +08004592 written_frames = out->written /
4593 (audio_bytes_per_sample(out->hal_ip_format) * popcount(out->channel_mask));
4594
Ashish Jain5106d362016-05-11 19:23:33 +05304595 /* not querying actual state of buffering in kernel as it would involve an ioctl call
4596 * which then needs protection, this causes delay in TS query for pcm_offload usecase
4597 * hence only estimate.
4598 */
Weiyin Jiang4813da12020-05-28 00:37:28 +08004599 kernel_buffer_size = out->compr_config.fragment_size * out->compr_config.fragments;
4600 kernel_frames = kernel_buffer_size /
4601 (audio_bytes_per_sample(out->hal_op_format) * popcount(out->channel_mask));
Ashish Jain5106d362016-05-11 19:23:33 +05304602
Weiyin Jiang4813da12020-05-28 00:37:28 +08004603 if (written_frames >= (kernel_frames + dsp_frames))
4604 signed_frames = written_frames - kernel_frames - dsp_frames;
Ashish Jain5106d362016-05-11 19:23:33 +05304605
Zhou Song48453a02018-01-10 17:50:59 +08004606 if (signed_frames > 0) {
Ashish Jain5106d362016-05-11 19:23:33 +05304607 actual_frames_rendered = signed_frames;
Zhou Song48453a02018-01-10 17:50:59 +08004608 if (timestamp != NULL )
4609 *timestamp = out->writeAt;
4610 } else if (timestamp != NULL) {
4611 clock_gettime(CLOCK_MONOTONIC, timestamp);
4612 }
4613 pthread_mutex_unlock(&out->position_query_lock);
Ashish Jain5106d362016-05-11 19:23:33 +05304614
Weiyin Jiang4813da12020-05-28 00:37:28 +08004615 ALOGVV("%s signed frames %lld written frames %lld kernel frames %lld dsp frames %lld",
4616 __func__, signed_frames, written_frames, kernel_frames, dsp_frames);
Ashish Jain5106d362016-05-11 19:23:33 +05304617
4618 return actual_frames_rendered;
4619}
4620
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004621static uint32_t out_get_sample_rate(const struct audio_stream *stream)
4622{
4623 struct stream_out *out = (struct stream_out *)stream;
4624
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004625 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004626}
4627
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07004628static int out_set_sample_rate(struct audio_stream *stream __unused,
4629 uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004630{
4631 return -ENOSYS;
4632}
4633
4634static size_t out_get_buffer_size(const struct audio_stream *stream)
4635{
4636 struct stream_out *out = (struct stream_out *)stream;
4637
Varun Balaraje49253e2017-07-06 19:48:56 +05304638 if (is_interactive_usecase(out->usecase)) {
Sri Karri27279e12017-08-07 16:05:20 +05304639 return out->config.period_size * out->config.period_count;
Varun Balaraje49253e2017-07-06 19:48:56 +05304640 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Naresh Tanniruee3499a2017-01-05 14:05:35 +05304641 if (out->flags & AUDIO_OUTPUT_FLAG_TIMESTAMP)
4642 return out->compr_config.fragment_size - sizeof(struct snd_codec_metadata);
4643 else
4644 return out->compr_config.fragment_size;
4645 } else if(out->usecase == USECASE_COMPRESS_VOIP_CALL)
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08004646 return voice_extn_compress_voip_out_get_buffer_size(out);
Dhananjay Kumarac341582017-02-23 23:42:25 +05304647 else if (is_offload_usecase(out->usecase) &&
4648 out->flags == AUDIO_OUTPUT_FLAG_DIRECT)
Ashish Jain83a6cc22016-06-28 14:34:17 +05304649 return out->hal_fragment_size;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004650
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004651 return out->config.period_size * out->af_period_multiplier *
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07004652 audio_stream_out_frame_size((const struct audio_stream_out *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004653}
4654
4655static uint32_t out_get_channels(const struct audio_stream *stream)
4656{
4657 struct stream_out *out = (struct stream_out *)stream;
4658
4659 return out->channel_mask;
4660}
4661
4662static audio_format_t out_get_format(const struct audio_stream *stream)
4663{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004664 struct stream_out *out = (struct stream_out *)stream;
4665
4666 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004667}
4668
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07004669static int out_set_format(struct audio_stream *stream __unused,
4670 audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004671{
4672 return -ENOSYS;
4673}
4674
4675static int out_standby(struct audio_stream *stream)
4676{
4677 struct stream_out *out = (struct stream_out *)stream;
4678 struct audio_device *adev = out->dev;
Haynes Mathew George16081042017-05-31 17:16:49 -07004679 bool do_stop = true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004680
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05304681 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
4682 stream, out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004683
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07004684 lock_output_stream(out);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004685 if (!out->standby) {
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07004686 if (adev->adm_deregister_stream)
4687 adev->adm_deregister_stream(adev->adm_data, out->handle);
4688
Weiyin Jiang280ea742020-09-08 20:28:22 +08004689 if (is_offload_usecase(out->usecase)) {
Haynes Mathew George7fce0a52016-06-23 18:22:27 -07004690 stop_compressed_output_l(out);
Weiyin Jiang280ea742020-09-08 20:28:22 +08004691 }
Haynes Mathew George7fce0a52016-06-23 18:22:27 -07004692
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08004693 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004694 out->standby = true;
Zhou Songa8895042016-07-05 17:54:22 +08004695 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
4696 voice_extn_compress_voip_close_output_stream(stream);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304697 out->started = 0;
Zhou Songa8895042016-07-05 17:54:22 +08004698 pthread_mutex_unlock(&adev->lock);
4699 pthread_mutex_unlock(&out->lock);
4700 ALOGD("VOIP output entered standby");
4701 return 0;
4702 } else if (!is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004703 if (out->pcm) {
4704 pcm_close(out->pcm);
4705 out->pcm = NULL;
4706 }
Meng Wanga09da002020-04-20 12:56:04 +08004707 if (out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) {
4708 if (adev->haptic_pcm) {
4709 pcm_close(adev->haptic_pcm);
4710 adev->haptic_pcm = NULL;
4711 }
4712
4713 if (adev->haptic_buffer != NULL) {
4714 free(adev->haptic_buffer);
4715 adev->haptic_buffer = NULL;
4716 adev->haptic_buffer_size = 0;
4717 }
4718 adev->haptic_pcm_device_id = 0;
4719 }
4720
Haynes Mathew George16081042017-05-31 17:16:49 -07004721 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
4722 do_stop = out->playback_started;
4723 out->playback_started = false;
Phil Burkd898ba62019-06-20 12:49:01 -07004724
4725 if (out->mmap_shared_memory_fd >= 0) {
4726 ALOGV("%s: closing mmap_shared_memory_fd = %d",
4727 __func__, out->mmap_shared_memory_fd);
4728 close(out->mmap_shared_memory_fd);
4729 out->mmap_shared_memory_fd = -1;
4730 }
Haynes Mathew George16081042017-05-31 17:16:49 -07004731 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004732 } else {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07004733 ALOGD("copl(%p):standby", out);
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05304734 out->send_next_track_params = false;
4735 out->is_compr_metadata_avail = false;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004736 out->gapless_mdata.encoder_delay = 0;
4737 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004738 if (out->compr != NULL) {
4739 compress_close(out->compr);
4740 out->compr = NULL;
4741 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08004742 }
Haynes Mathew George16081042017-05-31 17:16:49 -07004743 if (do_stop) {
4744 stop_output_stream(out);
4745 }
Lakshman Chaluvaraju06677b42019-06-24 10:04:52 +05304746 // if fm is active route on selected device in UI
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004747 audio_extn_fm_route_on_selected_device(adev, &out->device_list);
Eric Laurent150dbfe2013-02-27 14:31:02 -08004748 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004749 }
4750 pthread_mutex_unlock(&out->lock);
Aniket Kumar Latad13758f2020-08-06 15:11:36 -07004751 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004752 return 0;
4753}
4754
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304755static int out_on_error(struct audio_stream *stream)
4756{
4757 struct stream_out *out = (struct stream_out *)stream;
Ben Rombergerfd02a2f2018-09-17 10:23:23 -07004758 int status = 0;
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304759
4760 lock_output_stream(out);
Haynes Mathew Georgeee5836f2017-11-21 18:02:10 -08004761 // always send CMD_ERROR for offload streams, this
4762 // is needed e.g. when SSR happens within compress_open
4763 // since the stream is active, offload_callback_thread is also active.
4764 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
4765 stop_compressed_output_l(out);
Ben Rombergerfd02a2f2018-09-17 10:23:23 -07004766 }
4767 pthread_mutex_unlock(&out->lock);
4768
4769 status = out_standby(&out->stream.common);
4770
4771 lock_output_stream(out);
4772 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Haynes Mathew Georgeee5836f2017-11-21 18:02:10 -08004773 send_offload_cmd_l(out, OFFLOAD_CMD_ERROR);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304774 }
Manisha Agarwal7b3e3772019-02-20 14:33:45 +05304775
4776 if (is_offload_usecase(out->usecase) && out->card_status == CARD_STATUS_OFFLINE) {
4777 ALOGD("Setting previous card status if offline");
4778 out->prev_card_status_offline = true;
4779 }
4780
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304781 pthread_mutex_unlock(&out->lock);
4782
Ben Rombergerfd02a2f2018-09-17 10:23:23 -07004783 return status;
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304784}
4785
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304786/*
Weiyin Jiang280ea742020-09-08 20:28:22 +08004787 * standby implementation without locks, assumes that the callee already
4788 * has taken adev and out lock.
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304789 */
4790int out_standby_l(struct audio_stream *stream)
4791{
4792 struct stream_out *out = (struct stream_out *)stream;
4793 struct audio_device *adev = out->dev;
4794
4795 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
4796 stream, out->usecase, use_case_table[out->usecase]);
4797
4798 if (!out->standby) {
Haynes Mathew George380745d2017-10-04 15:27:45 -07004799 ATRACE_BEGIN("out_standby_l");
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304800 if (adev->adm_deregister_stream)
4801 adev->adm_deregister_stream(adev->adm_data, out->handle);
4802
Weiyin Jiang280ea742020-09-08 20:28:22 +08004803 if (is_offload_usecase(out->usecase)) {
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304804 stop_compressed_output_l(out);
Weiyin Jiang280ea742020-09-08 20:28:22 +08004805 }
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304806
4807 out->standby = true;
4808 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
4809 voice_extn_compress_voip_close_output_stream(stream);
4810 out->started = 0;
4811 ALOGD("VOIP output entered standby");
Haynes Mathew George380745d2017-10-04 15:27:45 -07004812 ATRACE_END();
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304813 return 0;
4814 } else if (!is_offload_usecase(out->usecase)) {
4815 if (out->pcm) {
4816 pcm_close(out->pcm);
4817 out->pcm = NULL;
4818 }
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004819 if (out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) {
4820 if (adev->haptic_pcm) {
4821 pcm_close(adev->haptic_pcm);
4822 adev->haptic_pcm = NULL;
4823 }
4824
4825 if (adev->haptic_buffer != NULL) {
4826 free(adev->haptic_buffer);
4827 adev->haptic_buffer = NULL;
4828 adev->haptic_buffer_size = 0;
4829 }
4830 adev->haptic_pcm_device_id = 0;
4831 }
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304832 } else {
4833 ALOGD("copl(%p):standby", out);
4834 out->send_next_track_params = false;
4835 out->is_compr_metadata_avail = false;
4836 out->gapless_mdata.encoder_delay = 0;
4837 out->gapless_mdata.encoder_padding = 0;
4838 if (out->compr != NULL) {
4839 compress_close(out->compr);
4840 out->compr = NULL;
4841 }
4842 }
4843 stop_output_stream(out);
Haynes Mathew George380745d2017-10-04 15:27:45 -07004844 ATRACE_END();
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304845 }
Aniket Kumar Latad13758f2020-08-06 15:11:36 -07004846 ALOGV("%s: exit", __func__);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304847 return 0;
4848}
4849
Aalique Grahame22e49102018-12-18 14:23:57 -08004850static int out_dump(const struct audio_stream *stream, int fd)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004851{
Aalique Grahame22e49102018-12-18 14:23:57 -08004852 struct stream_out *out = (struct stream_out *)stream;
4853
4854 // We try to get the lock for consistency,
4855 // but it isn't necessary for these variables.
4856 // If we're not in standby, we may be blocked on a write.
4857 const bool locked = (pthread_mutex_trylock(&out->lock) == 0);
4858 dprintf(fd, " Standby: %s\n", out->standby ? "yes" : "no");
4859 dprintf(fd, " Frames written: %lld\n", (long long)out->written);
Dechen Chai22768452021-07-30 09:29:16 +05304860#ifndef LINUX_ENABLED
Andy Hunga1f48fa2019-07-01 18:14:53 -07004861 char buffer[256]; // for statistics formatting
4862 if (!is_offload_usecase(out->usecase)) {
4863 simple_stats_to_string(&out->fifo_underruns, buffer, sizeof(buffer));
4864 dprintf(fd, " Fifo frame underruns: %s\n", buffer);
4865 }
4866
Andy Hungc6bfd4a2019-07-01 18:26:00 -07004867 if (out->start_latency_ms.n > 0) {
4868 simple_stats_to_string(&out->start_latency_ms, buffer, sizeof(buffer));
4869 dprintf(fd, " Start latency ms: %s\n", buffer);
4870 }
Dechen Chai22768452021-07-30 09:29:16 +05304871#endif
Aalique Grahame22e49102018-12-18 14:23:57 -08004872 if (locked) {
4873 pthread_mutex_unlock(&out->lock);
4874 }
4875
Dechen Chai22768452021-07-30 09:29:16 +05304876#ifndef LINUX_ENABLED
Aalique Grahame22e49102018-12-18 14:23:57 -08004877 // dump error info
4878 (void)error_log_dump(
4879 out->error_log, fd, " " /* prefix */, 0 /* lines */, 0 /* limit_ns */);
Dechen Chai22768452021-07-30 09:29:16 +05304880#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004881 return 0;
4882}
4883
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004884static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
4885{
4886 int ret = 0;
4887 char value[32];
ApurupaPattapu2e084df2013-12-18 15:47:59 -08004888
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004889 if (!out || !parms) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08004890 ALOGE("%s: return invalid ",__func__);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004891 return -EINVAL;
4892 }
4893
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +05304894 ret = audio_extn_parse_compress_metadata(out, parms);
Weiyin Jiang18ac4e92015-03-15 15:03:40 +08004895
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004896 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
4897 if (ret >= 0) {
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +05304898 out->gapless_mdata.encoder_delay = atoi(value); //whats a good limit check?
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004899 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004900 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
4901 if (ret >= 0) {
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +05304902 out->gapless_mdata.encoder_padding = atoi(value);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004903 }
4904
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004905 ALOGV("%s new encoder delay %u and padding %u", __func__,
4906 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
4907
4908 return 0;
4909}
4910
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07004911static bool output_drives_call(struct audio_device *adev, struct stream_out *out)
4912{
4913 return out == adev->primary_output || out == adev->voice_tx_output;
4914}
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004915
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304916// note: this call is safe only if the stream_cb is
4917// removed first in close_output_stream (as is done now).
4918static void out_snd_mon_cb(void * stream, struct str_parms * parms)
4919{
4920 if (!stream || !parms)
4921 return;
4922
4923 struct stream_out *out = (struct stream_out *)stream;
4924 struct audio_device *adev = out->dev;
4925
4926 card_status_t status;
4927 int card;
4928 if (parse_snd_card_status(parms, &card, &status) < 0)
4929 return;
4930
4931 pthread_mutex_lock(&adev->lock);
4932 bool valid_cb = (card == adev->snd_card);
4933 pthread_mutex_unlock(&adev->lock);
4934
4935 if (!valid_cb)
4936 return;
4937
4938 lock_output_stream(out);
4939 if (out->card_status != status)
4940 out->card_status = status;
4941 pthread_mutex_unlock(&out->lock);
4942
4943 ALOGI("out_snd_mon_cb for card %d usecase %s, status %s", card,
4944 use_case_table[out->usecase],
4945 status == CARD_STATUS_OFFLINE ? "offline" : "online");
4946
Aditya Bavanari59ebed42019-02-05 17:44:57 +05304947 if (status == CARD_STATUS_OFFLINE) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304948 out_on_error(stream);
Aditya Bavanari59ebed42019-02-05 17:44:57 +05304949 if (voice_is_call_state_active(adev) &&
4950 out == adev->primary_output) {
4951 ALOGD("%s: SSR/PDR occurred, end all calls\n", __func__);
4952 pthread_mutex_lock(&adev->lock);
4953 voice_stop_call(adev);
4954 adev->mode = AUDIO_MODE_NORMAL;
4955 pthread_mutex_unlock(&adev->lock);
4956 }
4957 }
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304958 return;
4959}
4960
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07004961int route_output_stream(struct stream_out *out,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004962 struct listnode *devices)
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07004963{
4964 struct audio_device *adev = out->dev;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07004965 int ret = 0;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004966 struct listnode new_devices;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07004967 bool bypass_a2dp = false;
4968 bool reconfig = false;
4969 unsigned long service_interval = 0;
4970
4971 ALOGD("%s: enter: usecase(%d: %s) devices %x",
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004972 __func__, out->usecase, use_case_table[out->usecase], get_device_types(devices));
4973
4974 list_init(&new_devices);
4975 assign_devices(&new_devices, devices);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07004976
4977 lock_output_stream(out);
4978 pthread_mutex_lock(&adev->lock);
4979
4980 /*
4981 * When HDMI cable is unplugged the music playback is paused and
4982 * the policy manager sends routing=0. But the audioflinger continues
4983 * to write data until standby time (3sec). As the HDMI core is
4984 * turned off, the write gets blocked.
4985 * Avoid this by routing audio to speaker until standby.
4986 */
Aniket Kumar Lata9c2fd892020-01-22 22:20:00 -08004987 if (is_single_device_type_equal(&out->device_list,
4988 AUDIO_DEVICE_OUT_AUX_DIGITAL) &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004989 list_empty(&new_devices) &&
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07004990 !audio_extn_passthru_is_passthrough_stream(out) &&
4991 (platform_get_edid_info(adev->platform) != 0) /* HDMI disconnected */) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004992 reassign_device_list(&new_devices, AUDIO_DEVICE_OUT_SPEAKER, "");
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07004993 }
4994 /*
4995 * When A2DP is disconnected the
4996 * music playback is paused and the policy manager sends routing=0
4997 * But the audioflinger continues to write data until standby time
4998 * (3sec). As BT is turned off, the write gets blocked.
4999 * Avoid this by routing audio to speaker until standby.
5000 */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005001 if (is_a2dp_out_device_type(&out->device_list) &&
5002 list_empty(&new_devices) &&
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005003 !audio_extn_a2dp_source_is_ready() &&
5004 !adev->bt_sco_on) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005005 reassign_device_list(&new_devices, AUDIO_DEVICE_OUT_SPEAKER, "");
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005006 }
5007 /*
Weiyin Jiangabedea32020-12-09 12:49:19 +08005008 * When USB headset is disconnected the music playback paused
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005009 * and the policy manager send routing=0. But if the USB is connected
5010 * back before the standby time, AFE is not closed and opened
5011 * when USB is connected back. So routing to speker will guarantee
5012 * AFE reconfiguration and AFE will be opend once USB is connected again
5013 */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005014 if (is_usb_out_device_type(&out->device_list) &&
5015 list_empty(&new_devices) &&
5016 !audio_extn_usb_connected(NULL)) {
Sujin Panicker84d953a2020-11-16 17:39:54 +05305017 if (adev->mode == AUDIO_MODE_IN_CALL || adev->mode == AUDIO_MODE_IN_COMMUNICATION)
5018 reassign_device_list(&new_devices, AUDIO_DEVICE_OUT_EARPIECE, "");
5019 else
5020 reassign_device_list(&new_devices, AUDIO_DEVICE_OUT_SPEAKER, "");
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005021 }
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005022 /* To avoid a2dp to sco overlapping / BT device improper state
5023 * check with BT lib about a2dp streaming support before routing
5024 */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005025 if (is_a2dp_out_device_type(&new_devices)) {
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005026 if (!audio_extn_a2dp_source_is_ready()) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005027 if (compare_device_type(&new_devices, AUDIO_DEVICE_OUT_SPEAKER) ||
5028 compare_device_type(&new_devices, AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005029 //combo usecase just by pass a2dp
5030 ALOGW("%s: A2DP profile is not ready,routing to speaker only", __func__);
5031 bypass_a2dp = true;
5032 } else {
5033 ALOGE("%s: A2DP profile is not ready,ignoring routing request", __func__);
5034 /* update device to a2dp and don't route as BT returned error
5035 * However it is still possible a2dp routing called because
5036 * of current active device disconnection (like wired headset)
5037 */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005038 assign_devices(&out->device_list, &new_devices);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005039 pthread_mutex_unlock(&adev->lock);
5040 pthread_mutex_unlock(&out->lock);
5041 goto error;
5042 }
5043 }
5044 }
5045
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005046 // Workaround: If routing to an non existing usb device, fail gracefully
5047 // The routing request will otherwise block during 10 second
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005048 if (is_usb_out_device_type(&new_devices)) {
5049 struct str_parms *parms =
5050 str_parms_create_str(get_usb_device_address(&new_devices));
5051 if (!parms)
5052 goto error;
Weiyin Jiangabedea32020-12-09 12:49:19 +08005053 if (!audio_extn_usb_connected(NULL)) {
5054 ALOGW("%s: ignoring rerouting to non existing USB card", __func__);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005055 pthread_mutex_unlock(&adev->lock);
5056 pthread_mutex_unlock(&out->lock);
5057 str_parms_destroy(parms);
5058 ret = -ENOSYS;
5059 goto error;
5060 }
5061 str_parms_destroy(parms);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005062 }
5063
Weiyin Jiang80c8f9a2020-01-10 20:42:08 +08005064 // Workaround: If routing to an non existing hdmi device, fail gracefully
5065 if (compare_device_type(&new_devices, AUDIO_DEVICE_OUT_AUX_DIGITAL) &&
5066 (platform_get_edid_info_v2(adev->platform,
5067 out->extconn.cs.controller,
5068 out->extconn.cs.stream) != 0)) {
5069 ALOGW("out_set_parameters() ignoring rerouting to non existing HDMI/DP");
5070 pthread_mutex_unlock(&adev->lock);
5071 pthread_mutex_unlock(&out->lock);
5072 ret = -ENOSYS;
5073 goto error;
5074 }
5075
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005076 /*
5077 * select_devices() call below switches all the usecases on the same
5078 * backend to the new device. Refer to check_usecases_codec_backend() in
5079 * the select_devices(). But how do we undo this?
5080 *
5081 * For example, music playback is active on headset (deep-buffer usecase)
5082 * and if we go to ringtones and select a ringtone, low-latency usecase
5083 * will be started on headset+speaker. As we can't enable headset+speaker
5084 * and headset devices at the same time, select_devices() switches the music
5085 * playback to headset+speaker while starting low-lateny usecase for ringtone.
5086 * So when the ringtone playback is completed, how do we undo the same?
5087 *
5088 * We are relying on the out_set_parameters() call on deep-buffer output,
5089 * once the ringtone playback is ended.
5090 * NOTE: We should not check if the current devices are same as new devices.
5091 * Because select_devices() must be called to switch back the music
5092 * playback to headset.
5093 */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005094 if (!list_empty(&new_devices)) {
5095 bool same_dev = compare_devices(&out->device_list, &new_devices);
5096 assign_devices(&out->device_list, &new_devices);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005097
5098 if (output_drives_call(adev, out)) {
5099 if (!voice_is_call_state_active(adev)) {
5100 if (adev->mode == AUDIO_MODE_IN_CALL) {
5101 adev->current_call_output = out;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005102 ret = voice_start_call(adev);
5103 }
5104 } else {
Kunlei Zhang253ad102020-10-14 16:21:28 +08005105 platform_is_volume_boost_supported_device(adev->platform, &new_devices);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005106 adev->current_call_output = out;
5107 voice_update_devices_for_all_voice_usecases(adev);
5108 }
5109 }
5110
Mingshu Pang971ff702020-09-09 15:28:22 +08005111 if (is_usb_out_device_type(&out->device_list)) {
5112 service_interval = audio_extn_usb_find_service_interval(false, true /*playback*/);
5113 audio_extn_usb_set_service_interval(true /*playback*/,
5114 service_interval,
5115 &reconfig);
5116 ALOGD("%s, svc_int(%ld),reconfig(%d)",__func__,service_interval, reconfig);
5117 }
5118
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005119 if (!out->standby) {
5120 if (!same_dev) {
5121 ALOGV("update routing change");
5122 audio_extn_perf_lock_acquire(&adev->perf_lock_handle, 0,
5123 adev->perf_lock_opts,
5124 adev->perf_lock_opts_size);
5125 if (adev->adm_on_routing_change)
5126 adev->adm_on_routing_change(adev->adm_data,
5127 out->handle);
5128 }
5129 if (!bypass_a2dp) {
5130 select_devices(adev, out->usecase);
5131 } else {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005132 if (compare_device_type(&new_devices, AUDIO_DEVICE_OUT_SPEAKER_SAFE))
5133 reassign_device_list(&out->device_list, AUDIO_DEVICE_OUT_SPEAKER_SAFE, "");
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005134 else
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005135 reassign_device_list(&out->device_list, AUDIO_DEVICE_OUT_SPEAKER, "");
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005136 select_devices(adev, out->usecase);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005137 assign_devices(&out->device_list, &new_devices);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005138 }
5139
5140 if (!same_dev) {
5141 // on device switch force swap, lower functions will make sure
5142 // to check if swap is allowed or not.
5143 platform_set_swap_channels(adev, true);
5144 audio_extn_perf_lock_release(&adev->perf_lock_handle);
5145 }
Zhou Songbaddf9f2020-11-20 13:57:39 +08005146 pthread_mutex_lock(&out->latch_lock);
5147 if (!is_a2dp_out_device_type(&out->device_list) || audio_extn_a2dp_source_is_ready()) {
5148 if (out->a2dp_muted) {
5149 out->a2dp_muted = false;
5150 if (is_offload_usecase(out->usecase))
5151 out_set_compr_volume(&out->stream, out->volume_l, out->volume_r);
5152 else if (out->usecase != USECASE_AUDIO_PLAYBACK_VOIP)
5153 out_set_pcm_volume(&out->stream, out->volume_l, out->volume_r);
Weiyin Jiang280ea742020-09-08 20:28:22 +08005154 }
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005155 }
Zhou Songbaddf9f2020-11-20 13:57:39 +08005156 if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP && !out->a2dp_muted)
5157 out_set_voip_volume(&out->stream, out->volume_l, out->volume_r);
5158 pthread_mutex_unlock(&out->latch_lock);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005159 }
5160 }
5161
5162 pthread_mutex_unlock(&adev->lock);
5163 pthread_mutex_unlock(&out->lock);
5164
5165 /*handles device and call state changes*/
5166 audio_extn_extspk_update(adev->extspk);
5167
5168error:
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005169 ALOGV("%s: exit: code(%d)", __func__, ret);
5170 return ret;
5171}
5172
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005173static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
5174{
5175 struct stream_out *out = (struct stream_out *)stream;
5176 struct audio_device *adev = out->dev;
5177 struct str_parms *parms;
5178 char value[32];
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005179 int ret = 0, err;
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07005180 int ext_controller = -1;
5181 int ext_stream = -1;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005182
sangwoobc677242013-08-08 16:53:43 +09005183 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07005184 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005185 parms = str_parms_create_str(kvpairs);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05305186 if (!parms)
5187 goto error;
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07005188
5189 err = platform_get_controller_stream_from_params(parms, &ext_controller,
5190 &ext_stream);
Vignesh Kulothungan1b3957e2019-12-02 16:16:51 -08005191 if (err == 0) {
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07005192 out->extconn.cs.controller = ext_controller;
5193 out->extconn.cs.stream = ext_stream;
5194 ALOGD("%s: usecase(%s) new controller/stream (%d/%d)", __func__,
5195 use_case_table[out->usecase], out->extconn.cs.controller,
5196 out->extconn.cs.stream);
5197 }
5198
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07005199 if (out == adev->primary_output) {
5200 pthread_mutex_lock(&adev->lock);
5201 audio_extn_set_parameters(adev, parms);
5202 pthread_mutex_unlock(&adev->lock);
5203 }
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07005204 if (is_offload_usecase(out->usecase)) {
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005205 lock_output_stream(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005206 parse_compress_metadata(out, parms);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08005207
5208 audio_extn_dts_create_state_notifier_node(out->usecase);
5209 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
5210 popcount(out->channel_mask),
5211 out->playback_started);
5212
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08005213 pthread_mutex_unlock(&out->lock);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005214 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07005215
Surendar Karkaf51b5842018-04-26 11:28:38 +05305216 err = str_parms_get_str(parms, AUDIO_PARAMETER_DUAL_MONO, value,
5217 sizeof(value));
5218 if (err >= 0) {
5219 if (!strncmp("true", value, sizeof("true")) || atoi(value))
5220 audio_extn_send_dual_mono_mixing_coefficients(out);
5221 }
5222
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +05305223 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_PROFILE, value, sizeof(value));
5224 if (err >= 0) {
5225 strlcpy(out->profile, value, sizeof(out->profile));
5226 ALOGV("updating stream profile with value '%s'", out->profile);
5227 lock_output_stream(out);
5228 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
5229 &adev->streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005230 &out->device_list, out->flags,
5231 out->hal_op_format,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +05305232 out->sample_rate, out->bit_width,
5233 out->channel_mask, out->profile,
5234 &out->app_type_cfg);
5235 pthread_mutex_unlock(&out->lock);
5236 }
5237
Alexy Joseph98988832017-01-13 14:56:59 -08005238 //suspend, resume handling block
Aniket Kumar Lata932f4872017-11-06 18:29:44 -08005239 //remove QOS only if vendor.audio.hal.dynamic.qos.config.supported is set to true
5240 // and vendor.audio.hal.output.suspend.supported is set to true
5241 if (out->hal_output_suspend_supported && out->dynamic_pm_qos_config_supported) {
Alexy Joseph98988832017-01-13 14:56:59 -08005242 //check suspend parameter only for low latency and if the property
5243 //is enabled
5244 if (str_parms_get_str(parms, "suspend_playback", value, sizeof(value)) >= 0) {
5245 ALOGI("%s: got suspend_playback %s", __func__, value);
5246 lock_output_stream(out);
5247 if (!strncmp(value, "false", 5)) {
5248 //suspend_playback=false is supposed to set QOS value back to 75%
5249 //the mixer control sent with value Enable will achieve that
5250 ret = audio_route_apply_and_update_path(adev->audio_route, out->pm_qos_mixer_path);
5251 } else if (!strncmp (value, "true", 4)) {
5252 //suspend_playback=true is supposed to remove QOS value
5253 //resetting the mixer control will set the default value
5254 //for the mixer control which is Disable and this removes the QOS vote
5255 ret = audio_route_reset_and_update_path(adev->audio_route, out->pm_qos_mixer_path);
5256 } else {
5257 ALOGE("%s: Wrong value sent for suspend_playback, expected true/false,"
5258 " got %s", __func__, value);
5259 ret = -1;
5260 }
5261
5262 if (ret != 0) {
5263 ALOGE("%s: %s mixer ctl failed with %d, ignore suspend/resume setparams",
5264 __func__, out->pm_qos_mixer_path, ret);
5265 }
5266
5267 pthread_mutex_unlock(&out->lock);
5268 }
5269 }
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07005270
Alexy Joseph98988832017-01-13 14:56:59 -08005271 //end suspend, resume handling block
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005272 str_parms_destroy(parms);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05305273error:
Eric Laurent994a6932013-07-17 11:51:42 -07005274 ALOGV("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005275 return ret;
5276}
5277
Paul McLeana50b7332018-12-17 08:24:21 -07005278static int in_set_microphone_direction(const struct audio_stream_in *stream,
5279 audio_microphone_direction_t dir) {
justinweng20fb6d82019-02-21 18:49:00 -07005280 struct stream_in *in = (struct stream_in *)stream;
5281
5282 ALOGVV("%s: standby %d source %d dir %d", __func__, in->standby, in->source, dir);
5283
5284 in->direction = dir;
5285
5286 if (in->standby)
5287 return 0;
5288
5289 return audio_extn_audiozoom_set_microphone_direction(in, dir);
Paul McLeana50b7332018-12-17 08:24:21 -07005290}
5291
5292static int in_set_microphone_field_dimension(const struct audio_stream_in *stream, float zoom) {
justinweng20fb6d82019-02-21 18:49:00 -07005293 struct stream_in *in = (struct stream_in *)stream;
5294
5295 ALOGVV("%s: standby %d source %d zoom %f", __func__, in->standby, in->source, zoom);
5296
5297 if (zoom > 1.0 || zoom < -1.0)
5298 return -EINVAL;
5299
5300 in->zoom = zoom;
5301
5302 if (in->standby)
5303 return 0;
5304
5305 return audio_extn_audiozoom_set_microphone_field_dimension(in, zoom);
Paul McLeana50b7332018-12-17 08:24:21 -07005306}
5307
5308
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005309static bool stream_get_parameter_channels(struct str_parms *query,
5310 struct str_parms *reply,
5311 audio_channel_mask_t *supported_channel_masks) {
5312 int ret = -1;
5313 char value[512];
5314 bool first = true;
5315 size_t i, j;
5316
5317 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) {
5318 ret = 0;
5319 value[0] = '\0';
5320 i = 0;
5321 while (supported_channel_masks[i] != 0) {
5322 for (j = 0; j < ARRAY_SIZE(channels_name_to_enum_table); j++) {
5323 if (channels_name_to_enum_table[j].value == supported_channel_masks[i]) {
5324 if (!first)
Satya Krishna Pindiprolib22ac722017-10-09 15:44:16 +05305325 strlcat(value, "|", sizeof(value));
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005326
Satya Krishna Pindiprolib22ac722017-10-09 15:44:16 +05305327 strlcat(value, channels_name_to_enum_table[j].name, sizeof(value));
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005328 first = false;
5329 break;
5330 }
5331 }
5332 i++;
5333 }
5334 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
5335 }
5336 return ret == 0;
5337}
5338
5339static bool stream_get_parameter_formats(struct str_parms *query,
5340 struct str_parms *reply,
5341 audio_format_t *supported_formats) {
5342 int ret = -1;
5343 char value[256];
5344 size_t i, j;
5345 bool first = true;
5346
5347 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) {
5348 ret = 0;
5349 value[0] = '\0';
5350 i = 0;
5351 while (supported_formats[i] != 0) {
5352 for (j = 0; j < ARRAY_SIZE(formats_name_to_enum_table); j++) {
5353 if (formats_name_to_enum_table[j].value == supported_formats[i]) {
5354 if (!first) {
Satya Krishna Pindiprolib22ac722017-10-09 15:44:16 +05305355 strlcat(value, "|", sizeof(value));
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005356 }
Satya Krishna Pindiprolib22ac722017-10-09 15:44:16 +05305357 strlcat(value, formats_name_to_enum_table[j].name, sizeof(value));
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005358 first = false;
5359 break;
5360 }
5361 }
5362 i++;
5363 }
5364 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_FORMATS, value);
5365 }
5366 return ret == 0;
5367}
5368
5369static bool stream_get_parameter_rates(struct str_parms *query,
5370 struct str_parms *reply,
5371 uint32_t *supported_sample_rates) {
5372
5373 int i;
5374 char value[256];
5375 int ret = -1;
5376 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) {
5377 ret = 0;
5378 value[0] = '\0';
5379 i=0;
5380 int cursor = 0;
5381 while (supported_sample_rates[i]) {
5382 int avail = sizeof(value) - cursor;
5383 ret = snprintf(value + cursor, avail, "%s%d",
5384 cursor > 0 ? "|" : "",
5385 supported_sample_rates[i]);
5386 if (ret < 0 || ret >= avail) {
5387 // if cursor is at the last element of the array
5388 // overwrite with \0 is duplicate work as
5389 // snprintf already put a \0 in place.
5390 // else
5391 // we had space to write the '|' at value[cursor]
5392 // (which will be overwritten) or no space to fill
5393 // the first element (=> cursor == 0)
5394 value[cursor] = '\0';
5395 break;
5396 }
5397 cursor += ret;
5398 ++i;
5399 }
5400 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES,
5401 value);
5402 }
5403 return ret >= 0;
5404}
5405
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005406static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
5407{
5408 struct stream_out *out = (struct stream_out *)stream;
5409 struct str_parms *query = str_parms_create_str(keys);
Alexy Josephaee4fdd2016-01-29 13:02:07 -08005410 char *str = (char*) NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005411 char value[256];
5412 struct str_parms *reply = str_parms_create();
5413 size_t i, j;
5414 int ret;
5415 bool first = true;
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07005416
5417 if (!query || !reply) {
Alexy Josephaee4fdd2016-01-29 13:02:07 -08005418 if (reply) {
5419 str_parms_destroy(reply);
5420 }
5421 if (query) {
5422 str_parms_destroy(query);
5423 }
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07005424 ALOGE("out_get_parameters: failed to allocate mem for query or reply");
5425 return NULL;
5426 }
5427
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005428 ALOGV("%s: %s enter: keys - %s", __func__, use_case_table[out->usecase], keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005429 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
5430 if (ret >= 0) {
5431 value[0] = '\0';
5432 i = 0;
5433 while (out->supported_channel_masks[i] != 0) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005434 for (j = 0; j < ARRAY_SIZE(channels_name_to_enum_table); j++) {
5435 if (channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005436 if (!first) {
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -08005437 strlcat(value, "|", sizeof(value));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005438 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005439 strlcat(value, channels_name_to_enum_table[j].name, sizeof(value));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005440 first = false;
5441 break;
5442 }
5443 }
5444 i++;
5445 }
5446 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
5447 str = str_parms_to_str(reply);
5448 } else {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08005449 voice_extn_out_get_parameters(out, query, reply);
5450 str = str_parms_to_str(reply);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005451 }
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07005452
Alexy Joseph62142aa2015-11-16 15:10:34 -08005453
5454 ret = str_parms_get_str(query, "is_direct_pcm_track", value, sizeof(value));
5455 if (ret >= 0) {
5456 value[0] = '\0';
Dhananjay Kumarac341582017-02-23 23:42:25 +05305457 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
5458 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Alexy Joseph62142aa2015-11-16 15:10:34 -08005459 ALOGV("in direct_pcm");
Satya Krishna Pindiprolib6655542017-07-03 19:38:19 +05305460 strlcat(value, "true", sizeof(value));
Alexy Joseph62142aa2015-11-16 15:10:34 -08005461 } else {
5462 ALOGV("not in direct_pcm");
Sharad Sangle3dd5a4a2015-12-10 18:39:17 +05305463 strlcat(value, "false", sizeof(value));
Alexy Joseph62142aa2015-11-16 15:10:34 -08005464 }
5465 str_parms_add_str(reply, "is_direct_pcm_track", value);
Alexy Josephaee4fdd2016-01-29 13:02:07 -08005466 if (str)
5467 free(str);
Alexy Joseph62142aa2015-11-16 15:10:34 -08005468 str = str_parms_to_str(reply);
5469 }
5470
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07005471 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS, value, sizeof(value));
5472 if (ret >= 0) {
5473 value[0] = '\0';
5474 i = 0;
5475 first = true;
5476 while (out->supported_formats[i] != 0) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005477 for (j = 0; j < ARRAY_SIZE(formats_name_to_enum_table); j++) {
5478 if (formats_name_to_enum_table[j].value == out->supported_formats[i]) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07005479 if (!first) {
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -08005480 strlcat(value, "|", sizeof(value));
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07005481 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005482 strlcat(value, formats_name_to_enum_table[j].name, sizeof(value));
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07005483 first = false;
5484 break;
5485 }
5486 }
5487 i++;
5488 }
5489 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_FORMATS, value);
Alexy Josephaee4fdd2016-01-29 13:02:07 -08005490 if (str)
5491 free(str);
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07005492 str = str_parms_to_str(reply);
5493 }
Mingming Yin3a941d42016-02-17 18:08:05 -08005494
5495 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES, value, sizeof(value));
5496 if (ret >= 0) {
5497 value[0] = '\0';
5498 i = 0;
5499 first = true;
5500 while (out->supported_sample_rates[i] != 0) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005501 for (j = 0; j < ARRAY_SIZE(out_sample_rates_name_to_enum_table); j++) {
5502 if (out_sample_rates_name_to_enum_table[j].value == out->supported_sample_rates[i]) {
Mingming Yin3a941d42016-02-17 18:08:05 -08005503 if (!first) {
5504 strlcat(value, "|", sizeof(value));
5505 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005506 strlcat(value, out_sample_rates_name_to_enum_table[j].name, sizeof(value));
Mingming Yin3a941d42016-02-17 18:08:05 -08005507 first = false;
5508 break;
5509 }
5510 }
5511 i++;
5512 }
5513 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES, value);
5514 if (str)
5515 free(str);
5516 str = str_parms_to_str(reply);
5517 }
5518
Alexy Joseph98988832017-01-13 14:56:59 -08005519 if (str_parms_get_str(query, "supports_hw_suspend", value, sizeof(value)) >= 0) {
5520 //only low latency track supports suspend_resume
5521 str_parms_add_int(reply, "supports_hw_suspend",
Aniket Kumar Lata932f4872017-11-06 18:29:44 -08005522 (out->hal_output_suspend_supported));
Alexy Joseph98988832017-01-13 14:56:59 -08005523 if (str)
5524 free(str);
5525 str = str_parms_to_str(reply);
5526 }
5527
5528
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005529 str_parms_destroy(query);
5530 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07005531 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005532 return str;
5533}
5534
5535static uint32_t out_get_latency(const struct audio_stream_out *stream)
5536{
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005537 uint32_t period_ms;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005538 struct stream_out *out = (struct stream_out *)stream;
Alexy Josephaa54c872014-12-03 02:46:47 -08005539 uint32_t latency = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005540
Alexy Josephaa54c872014-12-03 02:46:47 -08005541 if (is_offload_usecase(out->usecase)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05305542 lock_output_stream(out);
5543 latency = audio_extn_utils_compress_get_dsp_latency(out);
5544 pthread_mutex_unlock(&out->lock);
Haynes Mathew George16081042017-05-31 17:16:49 -07005545 } else if ((out->realtime) ||
5546 (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP)) {
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005547 // since the buffer won't be filled up faster than realtime,
5548 // return a smaller number
5549 if (out->config.rate)
5550 period_ms = (out->af_period_multiplier * out->config.period_size *
5551 1000) / (out->config.rate);
5552 else
5553 period_ms = 0;
George Gao9ba8a142020-07-23 14:30:03 -07005554 latency = period_ms + platform_render_latency(out) / 1000;
Alexy Josephaa54c872014-12-03 02:46:47 -08005555 } else {
5556 latency = (out->config.period_count * out->config.period_size * 1000) /
Mingshu Pangf69a88d2021-04-30 16:14:39 +08005557 (out->config.rate);
5558 if (out->usecase == USECASE_AUDIO_PLAYBACK_DEEP_BUFFER)
5559 latency += platform_render_latency(out)/1000;
Alexy Josephaa54c872014-12-03 02:46:47 -08005560 }
5561
Zhou Songd2537a02020-06-11 22:04:46 +08005562 if (!out->standby && is_a2dp_out_device_type(&out->device_list))
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08005563 latency += audio_extn_a2dp_get_encoder_latency();
5564
Anish Kumar50ebcbf2014-12-09 04:01:39 +05305565 ALOGV("%s: Latency %d", __func__, latency);
Alexy Josephaa54c872014-12-03 02:46:47 -08005566 return latency;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005567}
5568
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05305569static float AmpToDb(float amplification)
5570{
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05305571 float db = DSD_VOLUME_MIN_DB;
5572 if (amplification > 0) {
5573 db = 20 * log10(amplification);
5574 if(db < DSD_VOLUME_MIN_DB)
5575 return DSD_VOLUME_MIN_DB;
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05305576 }
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05305577 return db;
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05305578}
5579
Arun Mirpuri5d170872019-03-26 13:21:31 -07005580static int out_set_mmap_volume(struct audio_stream_out *stream, float left,
5581 float right)
5582{
5583 struct stream_out *out = (struct stream_out *)stream;
5584 long volume = 0;
5585 char mixer_ctl_name[128] = "";
5586 struct audio_device *adev = out->dev;
5587 struct mixer_ctl *ctl = NULL;
5588 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
5589 PCM_PLAYBACK);
5590
5591 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
5592 "Playback %d Volume", pcm_device_id);
5593 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
5594 if (!ctl) {
5595 ALOGE("%s: Could not get ctl for mixer cmd - %s",
5596 __func__, mixer_ctl_name);
5597 return -EINVAL;
5598 }
5599 if (left != right)
5600 ALOGW("%s: Left and right channel volume mismatch:%f,%f",
5601 __func__, left, right);
5602 volume = (long)(left * (MMAP_PLAYBACK_VOLUME_MAX*1.0));
5603 if (mixer_ctl_set_value(ctl, 0, volume) < 0){
5604 ALOGE("%s:ctl for mixer cmd - %s, volume %ld returned error",
5605 __func__, mixer_ctl_name, volume);
5606 return -EINVAL;
5607 }
5608 return 0;
5609}
5610
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05305611static int out_set_compr_volume(struct audio_stream_out *stream, float left,
5612 float right)
5613{
5614 struct stream_out *out = (struct stream_out *)stream;
Manish Dewangan338c50a2017-09-12 15:22:03 +05305615 long volume[2];
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05305616 char mixer_ctl_name[128];
5617 struct audio_device *adev = out->dev;
5618 struct mixer_ctl *ctl;
5619 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
5620 PCM_PLAYBACK);
5621
5622 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
5623 "Compress Playback %d Volume", pcm_device_id);
5624 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
5625 if (!ctl) {
5626 ALOGE("%s: Could not get ctl for mixer cmd - %s",
5627 __func__, mixer_ctl_name);
5628 return -EINVAL;
5629 }
5630 ALOGE("%s:ctl for mixer cmd - %s, left %f, right %f",
5631 __func__, mixer_ctl_name, left, right);
5632 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
5633 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
5634 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
5635
5636 return 0;
5637}
5638
Zhou Song2b8f28f2017-09-11 10:51:38 +08005639static int out_set_voip_volume(struct audio_stream_out *stream, float left,
5640 float right)
5641{
5642 struct stream_out *out = (struct stream_out *)stream;
5643 char mixer_ctl_name[] = "App Type Gain";
5644 struct audio_device *adev = out->dev;
5645 struct mixer_ctl *ctl;
Manish Dewangan338c50a2017-09-12 15:22:03 +05305646 long set_values[4];
Zhou Song2b8f28f2017-09-11 10:51:38 +08005647
Aniket Kumar Lata8426d1f2019-06-10 15:25:53 -07005648 if (!is_valid_volume(left, right)) {
5649 ALOGE("%s: Invalid stream volume for left=%f, right=%f",
5650 __func__, left, right);
5651 return -EINVAL;
5652 }
5653
Zhou Song2b8f28f2017-09-11 10:51:38 +08005654 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
5655 if (!ctl) {
5656 ALOGE("%s: Could not get ctl for mixer cmd - %s",
5657 __func__, mixer_ctl_name);
5658 return -EINVAL;
5659 }
5660
5661 set_values[0] = 0; //0: Rx Session 1:Tx Session
5662 set_values[1] = out->app_type_cfg.app_type;
Manish Dewangan338c50a2017-09-12 15:22:03 +05305663 set_values[2] = (long)(left * VOIP_PLAYBACK_VOLUME_MAX);
5664 set_values[3] = (long)(right * VOIP_PLAYBACK_VOLUME_MAX);
Zhou Song2b8f28f2017-09-11 10:51:38 +08005665
5666 mixer_ctl_set_array(ctl, set_values, ARRAY_SIZE(set_values));
5667 return 0;
5668}
5669
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05305670static int out_set_pcm_volume(struct audio_stream_out *stream, float left,
5671 float right)
5672{
5673 struct stream_out *out = (struct stream_out *)stream;
5674 /* Volume control for pcm playback */
5675 if (left != right) {
5676 return -EINVAL;
5677 } else {
5678 char mixer_ctl_name[128];
5679 struct audio_device *adev = out->dev;
5680 struct mixer_ctl *ctl;
5681 int pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
5682 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name), "Playback %d Volume", pcm_device_id);
5683 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
5684 if (!ctl) {
5685 ALOGE("%s : Could not get ctl for mixer cmd - %s", __func__, mixer_ctl_name);
5686 return -EINVAL;
5687 }
5688
5689 int volume = (int) (left * PCM_PLAYBACK_VOLUME_MAX);
5690 int ret = mixer_ctl_set_value(ctl, 0, volume);
5691 if (ret < 0) {
5692 ALOGE("%s: Could not set ctl, error:%d ", __func__, ret);
5693 return -EINVAL;
5694 }
5695
5696 ALOGV("%s : Pcm set volume value %d left %f", __func__, volume, left);
5697
5698 return 0;
5699 }
5700}
5701
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005702static int out_set_volume(struct audio_stream_out *stream, float left,
5703 float right)
5704{
Eric Laurenta9024de2013-04-04 09:19:12 -07005705 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005706 int volume[2];
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05305707 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005708
Arun Mirpuri5d170872019-03-26 13:21:31 -07005709 ALOGD("%s: called with left_vol=%f, right_vol=%f", __func__, left, right);
Eric Laurenta9024de2013-04-04 09:19:12 -07005710 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
5711 /* only take left channel into account: the API is for stereo anyway */
5712 out->muted = (left == 0.0f);
5713 return 0;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07005714 } else if (is_offload_usecase(out->usecase)) {
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05305715 if (audio_extn_passthru_is_passthrough_stream(out)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07005716 /*
5717 * Set mute or umute on HDMI passthrough stream.
5718 * Only take left channel into account.
5719 * Mute is 0 and unmute 1
5720 */
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05305721 audio_extn_passthru_set_volume(out, (left == 0.0f));
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05305722 } else if (out->format == AUDIO_FORMAT_DSD){
5723 char mixer_ctl_name[128] = "DSD Volume";
5724 struct audio_device *adev = out->dev;
5725 struct mixer_ctl *ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
5726
5727 if (!ctl) {
5728 ALOGE("%s: Could not get ctl for mixer cmd - %s",
5729 __func__, mixer_ctl_name);
5730 return -EINVAL;
5731 }
Manish Dewangan338c50a2017-09-12 15:22:03 +05305732 volume[0] = (long)(AmpToDb(left));
5733 volume[1] = (long)(AmpToDb(right));
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05305734 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
5735 return 0;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005736 } else if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_BUS) &&
Derek Chendf05eea2019-08-01 13:57:49 -07005737 (out->car_audio_stream == CAR_AUDIO_STREAM_MEDIA)) {
Philippe Gravel1c9ac352019-05-28 16:08:37 -07005738 ALOGD("%s: Overriding offload set volume for media bus stream", __func__);
5739 struct listnode *node = NULL;
5740 list_for_each(node, &adev->active_outputs_list) {
5741 streams_output_ctxt_t *out_ctxt = node_to_item(node,
5742 streams_output_ctxt_t,
5743 list);
5744 if (out_ctxt->output->usecase == USECASE_AUDIO_PLAYBACK_MEDIA) {
5745 out->volume_l = out_ctxt->output->volume_l;
5746 out->volume_r = out_ctxt->output->volume_r;
5747 }
5748 }
Weiyin Jiang280ea742020-09-08 20:28:22 +08005749 pthread_mutex_lock(&out->latch_lock);
Zhou Songbaddf9f2020-11-20 13:57:39 +08005750 if (!out->a2dp_muted) {
Philippe Gravel1c9ac352019-05-28 16:08:37 -07005751 ret = out_set_compr_volume(&out->stream, out->volume_l, out->volume_r);
5752 }
Weiyin Jiang280ea742020-09-08 20:28:22 +08005753 pthread_mutex_unlock(&out->latch_lock);
Philippe Gravel1c9ac352019-05-28 16:08:37 -07005754 return ret;
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07005755 } else {
Weiyin Jiang280ea742020-09-08 20:28:22 +08005756 pthread_mutex_lock(&out->latch_lock);
Zhou Songbaddf9f2020-11-20 13:57:39 +08005757 ALOGV("%s: compress mute %d", __func__, out->a2dp_muted);
5758 if (!out->a2dp_muted)
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05305759 ret = out_set_compr_volume(stream, left, right);
5760 out->volume_l = left;
5761 out->volume_r = right;
Weiyin Jiang280ea742020-09-08 20:28:22 +08005762 pthread_mutex_unlock(&out->latch_lock);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05305763 return ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005764 }
Vikram Panduranga93f080e2017-06-07 18:16:14 -07005765 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP) {
Aalique Grahame22e49102018-12-18 14:23:57 -08005766 out->app_type_cfg.gain[0] = (int)(left * VOIP_PLAYBACK_VOLUME_MAX);
5767 out->app_type_cfg.gain[1] = (int)(right * VOIP_PLAYBACK_VOLUME_MAX);
Zhou Songbaddf9f2020-11-20 13:57:39 +08005768 pthread_mutex_lock(&out->latch_lock);
Aalique Grahame22e49102018-12-18 14:23:57 -08005769 if (!out->standby) {
5770 audio_extn_utils_send_app_type_gain(out->dev,
5771 out->app_type_cfg.app_type,
5772 &out->app_type_cfg.gain[0]);
Zhou Songbaddf9f2020-11-20 13:57:39 +08005773 if (!out->a2dp_muted)
5774 ret = out_set_voip_volume(stream, left, right);
Aalique Grahame22e49102018-12-18 14:23:57 -08005775 }
Zhou Song2b8f28f2017-09-11 10:51:38 +08005776 out->volume_l = left;
5777 out->volume_r = right;
Zhou Songbaddf9f2020-11-20 13:57:39 +08005778 pthread_mutex_unlock(&out->latch_lock);
Zhou Song2b8f28f2017-09-11 10:51:38 +08005779 return ret;
Arun Mirpuri5d170872019-03-26 13:21:31 -07005780 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
5781 ALOGV("%s: MMAP set volume called", __func__);
5782 if (!out->standby)
5783 ret = out_set_mmap_volume(stream, left, right);
5784 out->volume_l = left;
5785 out->volume_r = right;
5786 return ret;
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05305787 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_LOW_LATENCY ||
Ramu Gottipati36547092018-12-28 11:32:09 +05305788 out->usecase == USECASE_AUDIO_PLAYBACK_DEEP_BUFFER ||
5789 out->usecase == USECASE_AUDIO_PLAYBACK_ULL) {
Zhou Songbaddf9f2020-11-20 13:57:39 +08005790 pthread_mutex_lock(&out->latch_lock);
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05305791 /* Volume control for pcm playback */
Zhou Songbaddf9f2020-11-20 13:57:39 +08005792 if (!out->standby && !out->a2dp_muted)
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05305793 ret = out_set_pcm_volume(stream, left, right);
5794 else
5795 out->apply_volume = true;
5796
5797 out->volume_l = left;
5798 out->volume_r = right;
Zhou Songbaddf9f2020-11-20 13:57:39 +08005799 pthread_mutex_unlock(&out->latch_lock);
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05305800 return ret;
Derek Chenf13dd492018-11-13 14:53:51 -08005801 } else if (audio_extn_auto_hal_is_bus_device_usecase(out->usecase)) {
5802 ALOGV("%s: bus device set volume called", __func__);
Zhou Songbaddf9f2020-11-20 13:57:39 +08005803 pthread_mutex_lock(&out->latch_lock);
5804 if (!out->standby && !out->a2dp_muted)
Derek Chenf13dd492018-11-13 14:53:51 -08005805 ret = out_set_pcm_volume(stream, left, right);
5806 out->volume_l = left;
5807 out->volume_r = right;
Zhou Songbaddf9f2020-11-20 13:57:39 +08005808 pthread_mutex_unlock(&out->latch_lock);
Derek Chenf13dd492018-11-13 14:53:51 -08005809 return ret;
Eric Laurenta9024de2013-04-04 09:19:12 -07005810 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005811
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005812 return -ENOSYS;
5813}
5814
Zhou Songc9672822017-08-16 16:01:39 +08005815static void update_frames_written(struct stream_out *out, size_t bytes)
5816{
5817 size_t bpf = 0;
5818
5819 if (is_offload_usecase(out->usecase) && !out->non_blocking &&
5820 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD))
5821 bpf = 1;
5822 else if (!is_offload_usecase(out->usecase))
5823 bpf = audio_bytes_per_sample(out->format) *
5824 audio_channel_count_from_out_mask(out->channel_mask);
Zhou Song48453a02018-01-10 17:50:59 +08005825
5826 pthread_mutex_lock(&out->position_query_lock);
5827 if (bpf != 0) {
Zhou Songc9672822017-08-16 16:01:39 +08005828 out->written += bytes / bpf;
Zhou Song48453a02018-01-10 17:50:59 +08005829 clock_gettime(CLOCK_MONOTONIC, &out->writeAt);
5830 }
5831 pthread_mutex_unlock(&out->position_query_lock);
Zhou Songc9672822017-08-16 16:01:39 +08005832}
5833
Vignesh Kulothungana6927272019-02-20 15:17:07 -08005834int split_and_write_audio_haptic_data(struct stream_out *out,
5835 const void *buffer, size_t bytes_to_write)
5836{
5837 struct audio_device *adev = out->dev;
5838
5839 int ret = 0;
5840 size_t channel_count = audio_channel_count_from_out_mask(out->channel_mask);
5841 size_t bytes_per_sample = audio_bytes_per_sample(out->format);
5842 size_t frame_size = channel_count * bytes_per_sample;
5843 size_t frame_count = bytes_to_write / frame_size;
5844
5845 bool force_haptic_path =
5846 property_get_bool("vendor.audio.test_haptic", false);
5847
5848 // extract Haptics data from Audio buffer
5849 bool alloc_haptic_buffer = false;
5850 int haptic_channel_count = adev->haptics_config.channels;
5851 size_t haptic_frame_size = bytes_per_sample * haptic_channel_count;
5852 size_t audio_frame_size = frame_size - haptic_frame_size;
5853 size_t total_haptic_buffer_size = frame_count * haptic_frame_size;
5854
5855 if (adev->haptic_buffer == NULL) {
5856 alloc_haptic_buffer = true;
5857 } else if (adev->haptic_buffer_size < total_haptic_buffer_size) {
5858 free(adev->haptic_buffer);
5859 adev->haptic_buffer_size = 0;
5860 alloc_haptic_buffer = true;
5861 }
5862
5863 if (alloc_haptic_buffer) {
5864 adev->haptic_buffer = (uint8_t *)calloc(1, total_haptic_buffer_size);
Mingshu Pang1513f972019-05-24 12:43:51 +08005865 if(adev->haptic_buffer == NULL) {
5866 ALOGE("%s: failed to allocate mem for dev->haptic_buffer", __func__);
5867 return -ENOMEM;
5868 }
Vignesh Kulothungana6927272019-02-20 15:17:07 -08005869 adev->haptic_buffer_size = total_haptic_buffer_size;
5870 }
5871
5872 size_t src_index = 0, aud_index = 0, hap_index = 0;
5873 uint8_t *audio_buffer = (uint8_t *)buffer;
5874 uint8_t *haptic_buffer = adev->haptic_buffer;
5875
5876 // This is required for testing only. This works for stereo data only.
5877 // One channel is fed to audio stream and other to haptic stream for testing.
5878 if (force_haptic_path)
5879 audio_frame_size = haptic_frame_size = bytes_per_sample;
5880
5881 for (size_t i = 0; i < frame_count; i++) {
5882 memcpy(audio_buffer + aud_index, audio_buffer + src_index,
5883 audio_frame_size);
5884 aud_index += audio_frame_size;
5885 src_index += audio_frame_size;
5886
5887 if (adev->haptic_pcm)
5888 memcpy(haptic_buffer + hap_index, audio_buffer + src_index,
5889 haptic_frame_size);
5890 hap_index += haptic_frame_size;
5891 src_index += haptic_frame_size;
5892
5893 // This is required for testing only.
5894 // Discard haptic channel data.
5895 if (force_haptic_path)
5896 src_index += haptic_frame_size;
5897 }
5898
5899 // write to audio pipeline
5900 ret = pcm_write(out->pcm, (void *)audio_buffer,
5901 frame_count * audio_frame_size);
5902
5903 // write to haptics pipeline
5904 if (adev->haptic_pcm)
5905 ret = pcm_write(adev->haptic_pcm, (void *)adev->haptic_buffer,
5906 frame_count * haptic_frame_size);
5907
5908 return ret;
5909}
5910
Aalique Grahame22e49102018-12-18 14:23:57 -08005911#ifdef NO_AUDIO_OUT
5912static ssize_t out_write_for_no_output(struct audio_stream_out *stream,
5913 const void *buffer __unused, size_t bytes)
5914{
5915 struct stream_out *out = (struct stream_out *)stream;
5916
5917 /* No Output device supported other than BT for playback.
5918 * Sleep for the amount of buffer duration
5919 */
5920 lock_output_stream(out);
5921 usleep(bytes * 1000000 / audio_stream_out_frame_size(
5922 (const struct audio_stream_out *)&out->stream) /
5923 out_get_sample_rate(&out->stream.common));
5924 pthread_mutex_unlock(&out->lock);
5925 return bytes;
5926}
5927#endif
5928
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005929static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
5930 size_t bytes)
5931{
5932 struct stream_out *out = (struct stream_out *)stream;
5933 struct audio_device *adev = out->dev;
Eric Laurent6e895242013-09-05 16:10:57 -07005934 ssize_t ret = 0;
Satish Babu Patakokila715b1422017-08-22 14:33:21 +05305935 int channels = 0;
Arun Mirpuri7da752a2018-09-11 18:01:15 -07005936 const size_t frame_size = audio_stream_out_frame_size(stream);
5937 const size_t frames = (frame_size != 0) ? bytes / frame_size : bytes;
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +05305938 struct audio_usecase *usecase = NULL;
Meng Wang4c32fb42020-01-16 17:57:11 +08005939 uint32_t compr_passthr = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005940
Haynes Mathew George380745d2017-10-04 15:27:45 -07005941 ATRACE_BEGIN("out_write");
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005942 lock_output_stream(out);
Naresh Tanniru4c630392014-05-12 01:05:52 +05305943
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305944 if (CARD_STATUS_OFFLINE == out->card_status) {
Zhou Song0b2e5dc2015-03-16 14:41:38 +08005945
Dhananjay Kumarac341582017-02-23 23:42:25 +05305946 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Ashish Jainbbce4322016-02-16 13:25:27 +05305947 /*during SSR for compress usecase we should return error to flinger*/
Naresh Tanniru80659832014-06-04 18:17:56 +05305948 ALOGD(" copl %s: sound card is not active/SSR state", __func__);
5949 pthread_mutex_unlock(&out->lock);
Haynes Mathew George380745d2017-10-04 15:27:45 -07005950 ATRACE_END();
Naresh Tanniru80659832014-06-04 18:17:56 +05305951 return -ENETRESET;
Ashish Jainbbce4322016-02-16 13:25:27 +05305952 } else {
Ashish Jainbbce4322016-02-16 13:25:27 +05305953 ALOGD(" %s: sound card is not active/SSR state", __func__);
5954 ret= -EIO;
5955 goto exit;
Naresh Tanniru4c630392014-05-12 01:05:52 +05305956 }
5957 }
5958
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05305959 if (audio_extn_passthru_should_drop_data(out)) {
Ashish Jaind84fd6a2016-07-27 12:33:25 +05305960 ALOGV(" %s : Drop data as compress passthrough session is going on", __func__);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05305961 ret = -EIO;
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05305962 goto exit;
5963 }
5964
Haynes Mathew George16081042017-05-31 17:16:49 -07005965 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
5966 ret = -EINVAL;
5967 goto exit;
5968 }
5969
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005970 if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_AUX_DIGITAL) &&
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05305971 !out->is_iec61937_info_available) {
5972
5973 if (!audio_extn_passthru_is_passthrough_stream(out)) {
5974 out->is_iec61937_info_available = true;
5975 } else if (audio_extn_passthru_is_enabled()) {
5976 audio_extn_passthru_update_stream_configuration(adev, out, buffer, bytes);
Manish Dewangan37864bc2017-06-09 12:28:37 +05305977 out->is_iec61937_info_available = true;
Manish Dewangan671a4202017-08-18 17:30:46 +05305978
5979 if((out->format == AUDIO_FORMAT_DTS) ||
5980 (out->format == AUDIO_FORMAT_DTS_HD)) {
5981 ret = audio_extn_passthru_update_dts_stream_configuration(out,
5982 buffer, bytes);
5983 if (ret) {
5984 if (ret != -ENOSYS) {
5985 out->is_iec61937_info_available = false;
5986 ALOGD("iec61937 transmission info not yet updated retry");
5987 }
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05305988 } else if (!out->standby) {
Manish Dewangan671a4202017-08-18 17:30:46 +05305989 /* if stream has started and after that there is
5990 * stream config change (iec transmission config)
5991 * then trigger select_device to update backend configuration.
5992 */
5993 out->stream_config_changed = true;
5994 pthread_mutex_lock(&adev->lock);
5995 select_devices(adev, out->usecase);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05305996 if (!audio_extn_passthru_is_supported_backend_edid_cfg(adev, out)) {
Weiyin Jiang29c08a42019-04-30 17:11:10 +08005997 pthread_mutex_unlock(&adev->lock);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05305998 ret = -EINVAL;
5999 goto exit;
6000 }
Manish Dewangan671a4202017-08-18 17:30:46 +05306001 pthread_mutex_unlock(&adev->lock);
6002 out->stream_config_changed = false;
6003 out->is_iec61937_info_available = true;
6004 }
6005 }
Satish Babu Patakokila715b1422017-08-22 14:33:21 +05306006
Meng Wang4c32fb42020-01-16 17:57:11 +08006007#ifdef AUDIO_GKI_ENABLED
6008 /* out->compr_config.codec->reserved[0] is for compr_passthr */
6009 compr_passthr = out->compr_config.codec->reserved[0];
6010#else
6011 compr_passthr = out->compr_config.codec->compr_passthr;
6012#endif
6013
Garmond Leung317cbf12017-09-13 16:20:50 -07006014 if ((channels < (int)audio_channel_count_from_out_mask(out->channel_mask)) &&
Meng Wang4c32fb42020-01-16 17:57:11 +08006015 (compr_passthr == PASSTHROUGH) &&
Satish Babu Patakokila715b1422017-08-22 14:33:21 +05306016 (out->is_iec61937_info_available == true)) {
6017 ALOGE("%s: ERROR: Unsupported channel config in passthrough mode", __func__);
6018 ret = -EINVAL;
6019 goto exit;
6020 }
Manish Dewangan37864bc2017-06-09 12:28:37 +05306021 }
6022 }
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05306023
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08006024 if (is_a2dp_out_device_type(&out->device_list) &&
Florian Pfister1a84f312018-07-19 14:38:18 +02006025 (audio_extn_a2dp_source_is_suspended())) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08006026 if (!(compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_SPEAKER) ||
6027 compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_SPEAKER_SAFE))) {
Zhou Songd01e7a22020-09-23 22:49:01 +08006028 if (!is_offload_usecase(out->usecase)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05306029 ret = -EIO;
6030 goto exit;
6031 }
6032 }
6033 }
6034
Weiyin Jiangabedea32020-12-09 12:49:19 +08006035 if (is_usb_out_device_type(&out->device_list) &&
6036 !audio_extn_usb_connected(NULL)) {
6037 ret = -EIO;
6038 goto exit;
6039 }
6040
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006041 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07006042 out->standby = false;
Andy Hungc6bfd4a2019-07-01 18:26:00 -07006043 const int64_t startNs = systemTime(SYSTEM_TIME_MONOTONIC);
6044
Eric Laurent150dbfe2013-02-27 14:31:02 -08006045 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08006046 if (out->usecase == USECASE_COMPRESS_VOIP_CALL)
6047 ret = voice_extn_compress_voip_start_output_stream(out);
6048 else
6049 ret = start_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006050 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006051 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07006052 out->standby = true;
Weiyin Jiangac2bae82020-07-29 17:23:53 +08006053 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006054 goto exit;
6055 }
Ashish Jain1b9b30c2017-05-18 20:57:40 +05306056 out->started = 1;
Andy Hunga1f48fa2019-07-01 18:14:53 -07006057 out->last_fifo_valid = false; // we're coming out of standby, last_fifo isn't valid.
Weiyin Jiangac2bae82020-07-29 17:23:53 +08006058
6059 if ((last_known_cal_step != -1) && (adev->platform != NULL)) {
vivek mehtab72d08d2016-04-29 03:16:47 -07006060 ALOGD("%s: retry previous failed cal level set", __func__);
Weiyin Jiangac2bae82020-07-29 17:23:53 +08006061 platform_send_gain_dep_cal(adev->platform, last_known_cal_step);
Preetam Singh Ranawatf4ae0222017-05-31 17:07:28 +05306062 last_known_cal_step = -1;
vivek mehtab72d08d2016-04-29 03:16:47 -07006063 }
Weiyin Jiangac2bae82020-07-29 17:23:53 +08006064 pthread_mutex_unlock(&adev->lock);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05306065
6066 if ((out->is_iec61937_info_available == true) &&
6067 (audio_extn_passthru_is_passthrough_stream(out))&&
6068 (!audio_extn_passthru_is_supported_backend_edid_cfg(adev, out))) {
6069 ret = -EINVAL;
6070 goto exit;
6071 }
Surendar Karkaf51b5842018-04-26 11:28:38 +05306072 if (out->set_dual_mono)
6073 audio_extn_send_dual_mono_mixing_coefficients(out);
Andy Hungc6bfd4a2019-07-01 18:26:00 -07006074
Dechen Chai22768452021-07-30 09:29:16 +05306075#ifndef LINUX_ENABLED
Andy Hungc6bfd4a2019-07-01 18:26:00 -07006076 // log startup time in ms.
6077 simple_stats_log(
6078 &out->start_latency_ms, (systemTime(SYSTEM_TIME_MONOTONIC) - startNs) * 1e-6);
Dechen Chai22768452021-07-30 09:29:16 +05306079#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006080 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006081
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08006082 if (adev->is_channel_status_set == false &&
6083 compare_device_type(&out->device_list,
6084 AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
Alexy Josephb1379942016-01-29 15:49:38 -08006085 audio_utils_set_hdmi_channel_status(out, (void *)buffer, bytes);
Ashish Jain81eb2a82015-05-13 10:52:34 +05306086 adev->is_channel_status_set = true;
6087 }
6088
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +05306089 if ((adev->use_old_pspd_mix_ctrl == true) &&
6090 (out->pspd_coeff_sent == false)) {
6091 /*
6092 * Need to resend pspd coefficients after stream started for
6093 * older kernel version as it does not save the coefficients
6094 * and also stream has to be started for coeff to apply.
6095 */
6096 usecase = get_usecase_from_list(adev, out->usecase);
6097 if (usecase != NULL) {
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05306098 audio_extn_set_custom_mtmx_params_v2(adev, usecase, true);
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +05306099 out->pspd_coeff_sent = true;
6100 }
6101 }
6102
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07006103 if (is_offload_usecase(out->usecase)) {
Alexy Joseph01e54e62015-03-03 19:01:03 -08006104 ALOGVV("copl(%p): writing buffer (%zu bytes) to compress device", out, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07006105 if (out->send_new_metadata) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07006106 ALOGD("copl(%p):send new gapless metadata", out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07006107 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
6108 out->send_new_metadata = 0;
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05306109 if (out->send_next_track_params && out->is_compr_metadata_avail) {
6110 ALOGD("copl(%p):send next track params in gapless", out);
Weiyin Jiangd9b5a4e2019-07-18 17:24:21 +08006111 compress_set_next_track_param(out->compr, &(out->compr_config.codec->options));
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05306112 out->send_next_track_params = false;
6113 out->is_compr_metadata_avail = false;
6114 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07006115 }
Dhananjay Kumarac341582017-02-23 23:42:25 +05306116 if (!(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
Ashish Jain83a6cc22016-06-28 14:34:17 +05306117 (out->convert_buffer) != NULL) {
Haynes Mathew George352f27b2013-07-26 00:00:15 -07006118
Ashish Jain83a6cc22016-06-28 14:34:17 +05306119 if ((bytes > out->hal_fragment_size)) {
Ashish Jainf1eaa582016-05-23 20:54:24 +05306120 ALOGW("Error written bytes %zu > %d (fragment_size)",
Ashish Jain83a6cc22016-06-28 14:34:17 +05306121 bytes, out->hal_fragment_size);
Ashish Jainf1eaa582016-05-23 20:54:24 +05306122 pthread_mutex_unlock(&out->lock);
Haynes Mathew George380745d2017-10-04 15:27:45 -07006123 ATRACE_END();
Ashish Jainf1eaa582016-05-23 20:54:24 +05306124 return -EINVAL;
6125 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05306126 audio_format_t dst_format = out->hal_op_format;
6127 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05306128
Dieter Luecking5d57def2018-09-07 14:23:37 +02006129 /* prevent division-by-zero */
6130 uint32_t bitwidth_src = format_to_bitwidth_table[src_format];
6131 uint32_t bitwidth_dst = format_to_bitwidth_table[dst_format];
6132 if ((bitwidth_src == 0) || (bitwidth_dst == 0)) {
6133 ALOGE("%s: Error bitwidth == 0", __func__);
Ramu Gottipati02809682018-12-19 16:46:12 +05306134 pthread_mutex_unlock(&out->lock);
Dieter Luecking5d57def2018-09-07 14:23:37 +02006135 ATRACE_END();
6136 return -EINVAL;
6137 }
6138
Ashish Jainf1eaa582016-05-23 20:54:24 +05306139 uint32_t frames = bytes / format_to_bitwidth_table[src_format];
6140 uint32_t bytes_to_write = frames * format_to_bitwidth_table[dst_format];
6141
Ashish Jain83a6cc22016-06-28 14:34:17 +05306142 memcpy_by_audio_format(out->convert_buffer,
Ashish Jainf1eaa582016-05-23 20:54:24 +05306143 dst_format,
6144 buffer,
6145 src_format,
6146 frames);
6147
Ashish Jain83a6cc22016-06-28 14:34:17 +05306148 ret = compress_write(out->compr, out->convert_buffer,
Ashish Jainf1eaa582016-05-23 20:54:24 +05306149 bytes_to_write);
6150
6151 /*Convert written bytes in audio flinger format*/
6152 if (ret > 0)
6153 ret = ((ret * format_to_bitwidth_table[out->format]) /
6154 format_to_bitwidth_table[dst_format]);
6155 }
6156 } else
6157 ret = compress_write(out->compr, buffer, bytes);
6158
Zhou Songc9672822017-08-16 16:01:39 +08006159 if ((ret < 0 || ret == (ssize_t)bytes) && !out->non_blocking)
6160 update_frames_written(out, bytes);
6161
Dhanalakshmi Siddani37ca1d62014-08-20 12:28:34 +05306162 if (ret < 0)
6163 ret = -errno;
Weiyin Jiangcc60dbb2018-08-21 13:12:03 +08006164 ALOGVV("%s: writing buffer (%zu bytes) to compress device returned %d", __func__, bytes, (int)ret);
Ashish Jainb26edfb2016-08-25 00:10:11 +05306165 /*msg to cb thread only if non blocking write is enabled*/
6166 if (ret >= 0 && ret < (ssize_t)bytes && out->non_blocking) {
Sidipotu Ashok55820562014-02-10 16:16:38 +05306167 ALOGD("No space available in compress driver, post msg to cb thread");
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006168 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
Naresh Tanniru80659832014-06-04 18:17:56 +05306169 } else if (-ENETRESET == ret) {
6170 ALOGE("copl %s: received sound card offline state on compress write", __func__);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306171 out->card_status = CARD_STATUS_OFFLINE;
Naresh Tanniru80659832014-06-04 18:17:56 +05306172 pthread_mutex_unlock(&out->lock);
Dhananjay Kumar1248dd82017-07-28 21:22:16 +05306173 out_on_error(&out->stream.common);
Haynes Mathew George380745d2017-10-04 15:27:45 -07006174 ATRACE_END();
Naresh Tanniru80659832014-06-04 18:17:56 +05306175 return ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006176 }
Ashish Jain5106d362016-05-11 19:23:33 +05306177
Dhanalakshmi Siddania6b76c72016-09-09 18:10:31 +05306178 /* Call compr start only when non-zero bytes of data is there to be rendered */
6179 if (!out->playback_started && ret > 0) {
6180 int status = compress_start(out->compr);
6181 if (status < 0) {
6182 ret = status;
6183 ALOGE("%s: compr start failed with err %d", __func__, errno);
6184 goto exit;
6185 }
Alexy Joseph7de344d2015-03-30 10:40:03 -07006186 audio_extn_dts_eagle_fade(adev, true, out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006187 out->playback_started = 1;
Weiyin Jiang280ea742020-09-08 20:28:22 +08006188 pthread_mutex_lock(&out->latch_lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006189 out->offload_state = OFFLOAD_STATE_PLAYING;
Weiyin Jiang280ea742020-09-08 20:28:22 +08006190 pthread_mutex_unlock(&out->latch_lock);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08006191
6192 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
6193 popcount(out->channel_mask),
6194 out->playback_started);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006195 }
6196 pthread_mutex_unlock(&out->lock);
Haynes Mathew George380745d2017-10-04 15:27:45 -07006197 ATRACE_END();
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006198 return ret;
6199 } else {
6200 if (out->pcm) {
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006201 size_t bytes_to_write = bytes;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006202 if (out->muted)
6203 memset((void *)buffer, 0, bytes);
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006204 ALOGV("%s: frames=%zu, frame_size=%zu, bytes_to_write=%zu",
6205 __func__, frames, frame_size, bytes_to_write);
6206
Aalique Grahame22e49102018-12-18 14:23:57 -08006207 if (out->usecase == USECASE_INCALL_MUSIC_UPLINK ||
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07006208 out->usecase == USECASE_INCALL_MUSIC_UPLINK2 ||
6209 (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP &&
6210 !audio_extn_utils_is_vendor_enhanced_fwk())) {
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006211 size_t channel_count = audio_channel_count_from_out_mask(out->channel_mask);
6212 int16_t *src = (int16_t *)buffer;
6213 int16_t *dst = (int16_t *)buffer;
6214
Yunfei Zhanga6cd66d2019-07-22 16:15:36 +08006215 LOG_ALWAYS_FATAL_IF(channel_count > 2 ||
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006216 out->format != AUDIO_FORMAT_PCM_16_BIT,
Aalique Grahame22e49102018-12-18 14:23:57 -08006217 "out_write called for %s use case with wrong properties",
6218 use_case_table[out->usecase]);
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006219
6220 /*
6221 * FIXME: this can be removed once audio flinger mixer supports
6222 * mono output
6223 */
6224
6225 /*
6226 * Code below goes over each frame in the buffer and adds both
6227 * L and R samples and then divides by 2 to convert to mono
6228 */
Yunfei Zhanga6cd66d2019-07-22 16:15:36 +08006229 if (channel_count == 2) {
6230 for (size_t i = 0; i < frames ; i++, dst++, src += 2) {
6231 *dst = (int16_t)(((int32_t)src[0] + (int32_t)src[1]) >> 1);
6232 }
6233 bytes_to_write /= 2;
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006234 }
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006235 }
Andy Hunga1f48fa2019-07-01 18:14:53 -07006236
6237 // Note: since out_get_presentation_position() is called alternating with out_write()
6238 // by AudioFlinger, we can check underruns using the prior timestamp read.
6239 // (Alternately we could check if the buffer is empty using pcm_get_htimestamp().
6240 if (out->last_fifo_valid) {
6241 // compute drain to see if there is an underrun.
6242 const int64_t current_ns = systemTime(SYSTEM_TIME_MONOTONIC); // sys call
Dhananjay Kumara4429632020-07-11 02:53:37 +05306243 int64_t time_diff_ns = current_ns - out->last_fifo_time_ns;
6244 int64_t frames_by_time =
6245 ((time_diff_ns > 0) && (time_diff_ns < (INT64_MAX / out->config.rate))) ?
6246 (time_diff_ns * out->config.rate / NANOS_PER_SECOND) : 0;
Andy Hunga1f48fa2019-07-01 18:14:53 -07006247 const int64_t underrun = frames_by_time - out->last_fifo_frames_remaining;
6248
6249 if (underrun > 0) {
Dechen Chai22768452021-07-30 09:29:16 +05306250#ifndef LINUX_ENABLED
Andy Hunga1f48fa2019-07-01 18:14:53 -07006251 simple_stats_log(&out->fifo_underruns, underrun);
Dechen Chai22768452021-07-30 09:29:16 +05306252#endif
Andy Hunga1f48fa2019-07-01 18:14:53 -07006253
6254 ALOGW("%s: underrun(%lld) "
6255 "frames_by_time(%lld) > out->last_fifo_frames_remaining(%lld)",
6256 __func__,
6257 (long long)out->fifo_underruns.n,
6258 (long long)frames_by_time,
6259 (long long)out->last_fifo_frames_remaining);
6260 }
6261 out->last_fifo_valid = false; // we're writing below, mark fifo info as stale.
6262 }
6263
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05306264 ALOGVV("%s: writing buffer (%zu bytes) to pcm device", __func__, bytes);
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07006265
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006266 long ns = 0;
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07006267
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006268 if (out->config.rate)
6269 ns = pcm_bytes_to_frames(out->pcm, bytes)*1000000000LL/
6270 out->config.rate;
6271
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006272 request_out_focus(out, ns);
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006273 bool use_mmap = is_mmap_usecase(out->usecase) || out->realtime;
6274
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006275 if (use_mmap)
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006276 ret = pcm_mmap_write(out->pcm, (void *)buffer, bytes_to_write);
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006277 else if (out->hal_op_format != out->hal_ip_format &&
Ashish Jain83a6cc22016-06-28 14:34:17 +05306278 out->convert_buffer != NULL) {
6279
6280 memcpy_by_audio_format(out->convert_buffer,
6281 out->hal_op_format,
6282 buffer,
6283 out->hal_ip_format,
6284 out->config.period_size * out->config.channels);
6285
6286 ret = pcm_write(out->pcm, out->convert_buffer,
6287 (out->config.period_size *
6288 out->config.channels *
6289 format_to_bitwidth_table[out->hal_op_format]));
6290 } else {
Aditya Bavanarid4db8ee2017-05-29 21:08:03 +05306291 /*
6292 * To avoid underrun in DSP when the application is not pumping
6293 * data at required rate, check for the no. of bytes and ignore
6294 * pcm_write if it is less than actual buffer size.
6295 * It is a work around to a change in compress VOIP driver.
6296 */
6297 if ((out->flags & AUDIO_OUTPUT_FLAG_VOIP_RX) &&
6298 bytes < (out->config.period_size * out->config.channels *
6299 audio_bytes_per_sample(out->format))) {
6300 size_t voip_buf_size =
6301 out->config.period_size * out->config.channels *
6302 audio_bytes_per_sample(out->format);
6303 ALOGE("%s:VOIP underrun: bytes received %zu, required:%zu\n",
6304 __func__, bytes, voip_buf_size);
6305 usleep(((uint64_t)voip_buf_size - bytes) *
6306 1000000 / audio_stream_out_frame_size(stream) /
6307 out_get_sample_rate(&out->stream.common));
6308 ret = 0;
Vignesh Kulothungana6927272019-02-20 15:17:07 -08006309 } else {
6310 if (out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS)
6311 ret = split_and_write_audio_haptic_data(out, buffer, bytes);
6312 else
6313 ret = pcm_write(out->pcm, (void *)buffer, bytes_to_write);
6314 }
Ashish Jain83a6cc22016-06-28 14:34:17 +05306315 }
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07006316
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006317 release_out_focus(out);
6318
Dhanalakshmi Siddani37ca1d62014-08-20 12:28:34 +05306319 if (ret < 0)
6320 ret = -errno;
Zhou Songc9672822017-08-16 16:01:39 +08006321 else if (ret > 0)
Ashish Jain83a6cc22016-06-28 14:34:17 +05306322 ret = -EINVAL;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006323 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006324 }
6325
6326exit:
Zhou Songc9672822017-08-16 16:01:39 +08006327 update_frames_written(out, bytes);
Naresh Tanniru4c630392014-05-12 01:05:52 +05306328 if (-ENETRESET == ret) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306329 out->card_status = CARD_STATUS_OFFLINE;
Naresh Tanniru4c630392014-05-12 01:05:52 +05306330 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006331 pthread_mutex_unlock(&out->lock);
6332
6333 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07006334 if (out->pcm)
Alexy Josephb1379942016-01-29 15:49:38 -08006335 ALOGE("%s: error %d, %s", __func__, (int)ret, pcm_get_error(out->pcm));
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05306336 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05306337 pthread_mutex_lock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05306338 voice_extn_compress_voip_close_output_stream(&out->stream.common);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05306339 out->started = 0;
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05306340 pthread_mutex_unlock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05306341 out->standby = true;
6342 }
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306343 out_on_error(&out->stream.common);
Dieter Luecking5d57def2018-09-07 14:23:37 +02006344 if (!(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
6345 /* prevent division-by-zero */
6346 uint32_t stream_size = audio_stream_out_frame_size(stream);
6347 uint32_t srate = out_get_sample_rate(&out->stream.common);
Vidyakumar Athotaa9d3a5f2017-08-09 12:13:05 -07006348
Dieter Luecking5d57def2018-09-07 14:23:37 +02006349 if ((stream_size == 0) || (srate == 0)) {
6350 ALOGE("%s: stream_size= %d, srate = %d", __func__, stream_size, srate);
6351 ATRACE_END();
6352 return -EINVAL;
6353 }
6354 usleep((uint64_t)bytes * 1000000 / stream_size / srate);
6355 }
Vidyakumar Athotaa9d3a5f2017-08-09 12:13:05 -07006356 if (audio_extn_passthru_is_passthrough_stream(out)) {
Rajshekar Eashwarappa88834522018-04-02 17:20:15 +05306357 //ALOGE("%s: write error, ret = %zd", __func__, ret);
Haynes Mathew George380745d2017-10-04 15:27:45 -07006358 ATRACE_END();
Vidyakumar Athotaa9d3a5f2017-08-09 12:13:05 -07006359 return ret;
6360 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006361 }
Haynes Mathew George380745d2017-10-04 15:27:45 -07006362 ATRACE_END();
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006363 return bytes;
6364}
6365
6366static int out_get_render_position(const struct audio_stream_out *stream,
6367 uint32_t *dsp_frames)
6368{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006369 struct stream_out *out = (struct stream_out *)stream;
Zhou Song32a556e2015-05-05 10:46:56 +08006370
6371 if (dsp_frames == NULL)
6372 return -EINVAL;
6373
6374 *dsp_frames = 0;
6375 if (is_offload_usecase(out->usecase)) {
Mingming Yin9e348b52014-11-19 16:18:55 -08006376 ssize_t ret = 0;
Ashish Jain5106d362016-05-11 19:23:33 +05306377
6378 /* Below piece of code is not guarded against any lock beacuse audioFliner serializes
6379 * this operation and adev_close_output_stream(where out gets reset).
6380 */
Dhananjay Kumarac341582017-02-23 23:42:25 +05306381 if (!out->non_blocking && !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Zhou Song48453a02018-01-10 17:50:59 +08006382 *dsp_frames = get_actual_pcm_frames_rendered(out, NULL);
Ashish Jain5106d362016-05-11 19:23:33 +05306383 ALOGVV("dsp_frames %d sampleRate %d",(int)*dsp_frames,out->sample_rate);
Haynes Mathew Georgeb0f5dc32017-10-06 18:35:12 -07006384 adjust_frames_for_device_delay(out, dsp_frames);
Ashish Jain5106d362016-05-11 19:23:33 +05306385 return 0;
6386 }
6387
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006388 lock_output_stream(out);
Ashish Jain5106d362016-05-11 19:23:33 +05306389 if (out->compr != NULL && out->non_blocking) {
Naresh Tanniru80659832014-06-04 18:17:56 +05306390 ret = compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006391 &out->sample_rate);
Dhanalakshmi Siddani37ca1d62014-08-20 12:28:34 +05306392 if (ret < 0)
6393 ret = -errno;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006394 ALOGVV("%s rendered frames %d sample_rate %d",
Ashish Jain5106d362016-05-11 19:23:33 +05306395 __func__, *dsp_frames, out->sample_rate);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006396 }
Naresh Tanniru80659832014-06-04 18:17:56 +05306397 if (-ENETRESET == ret) {
6398 ALOGE(" ERROR: sound card not active Unable to get time stamp from compress driver");
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306399 out->card_status = CARD_STATUS_OFFLINE;
6400 ret = -EINVAL;
Naresh Tanniru80659832014-06-04 18:17:56 +05306401 } else if(ret < 0) {
6402 ALOGE(" ERROR: Unable to get time stamp from compress driver");
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306403 ret = -EINVAL;
6404 } else if (out->card_status == CARD_STATUS_OFFLINE) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05306405 /*
6406 * Handle corner case where compress session is closed during SSR
6407 * and timestamp is queried
6408 */
6409 ALOGE(" ERROR: sound card not active, return error");
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306410 ret = -EINVAL;
Manisha Agarwal7b3e3772019-02-20 14:33:45 +05306411 } else if (out->prev_card_status_offline) {
6412 ALOGE("ERROR: previously sound card was offline,return error");
6413 ret = -EINVAL;
Naresh Tanniru80659832014-06-04 18:17:56 +05306414 } else {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306415 ret = 0;
Haynes Mathew Georgeb0f5dc32017-10-06 18:35:12 -07006416 adjust_frames_for_device_delay(out, dsp_frames);
Naresh Tanniru80659832014-06-04 18:17:56 +05306417 }
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306418 pthread_mutex_unlock(&out->lock);
6419 return ret;
Zhou Song32a556e2015-05-05 10:46:56 +08006420 } else if (audio_is_linear_pcm(out->format)) {
6421 *dsp_frames = out->written;
Haynes Mathew Georgeb0f5dc32017-10-06 18:35:12 -07006422 adjust_frames_for_device_delay(out, dsp_frames);
Zhou Song32a556e2015-05-05 10:46:56 +08006423 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006424 } else
6425 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006426}
6427
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07006428static int out_add_audio_effect(const struct audio_stream *stream __unused,
6429 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006430{
6431 return 0;
6432}
6433
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07006434static int out_remove_audio_effect(const struct audio_stream *stream __unused,
6435 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006436{
6437 return 0;
6438}
6439
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07006440static int out_get_next_write_timestamp(const struct audio_stream_out *stream __unused,
6441 int64_t *timestamp __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006442{
Satya Krishna Pindiprolib6655542017-07-03 19:38:19 +05306443 return -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006444}
6445
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07006446static int out_get_presentation_position(const struct audio_stream_out *stream,
6447 uint64_t *frames, struct timespec *timestamp)
6448{
6449 struct stream_out *out = (struct stream_out *)stream;
pavance65c2fe2017-10-18 17:52:01 +05306450 int ret = -ENODATA;
Eric Laurent949a0892013-09-20 09:20:13 -07006451 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07006452
Ashish Jain5106d362016-05-11 19:23:33 +05306453 /* below piece of code is not guarded against any lock because audioFliner serializes
6454 * this operation and adev_close_output_stream( where out gets reset).
6455 */
6456 if (is_offload_usecase(out->usecase) && !out->non_blocking &&
Dhananjay Kumarac341582017-02-23 23:42:25 +05306457 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Zhou Song48453a02018-01-10 17:50:59 +08006458 *frames = get_actual_pcm_frames_rendered(out, timestamp);
Ashish Jain5106d362016-05-11 19:23:33 +05306459 ALOGVV("frames %lld playedat %lld",(long long int)*frames,
6460 timestamp->tv_sec * 1000000LL + timestamp->tv_nsec / 1000);
6461 return 0;
6462 }
6463
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006464 lock_output_stream(out);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07006465
Ashish Jain5106d362016-05-11 19:23:33 +05306466 if (is_offload_usecase(out->usecase) && out->compr != NULL && out->non_blocking) {
6467 ret = compress_get_tstamp(out->compr, &dsp_frames,
6468 &out->sample_rate);
yidongh0515e042017-07-06 15:00:34 +08006469 // Adjustment accounts for A2dp encoder latency with offload usecases
6470 // Note: Encoder latency is returned in ms.
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08006471 if (is_a2dp_out_device_type(&out->device_list)) {
yidongh0515e042017-07-06 15:00:34 +08006472 unsigned long offset =
6473 (audio_extn_a2dp_get_encoder_latency() * out->sample_rate / 1000);
6474 dsp_frames = (dsp_frames > offset) ? (dsp_frames - offset) : 0;
6475 }
Ashish Jain5106d362016-05-11 19:23:33 +05306476 ALOGVV("%s rendered frames %ld sample_rate %d",
6477 __func__, dsp_frames, out->sample_rate);
6478 *frames = dsp_frames;
6479 if (ret < 0)
6480 ret = -errno;
6481 if (-ENETRESET == ret) {
6482 ALOGE(" ERROR: sound card not active Unable to get time stamp from compress driver");
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306483 out->card_status = CARD_STATUS_OFFLINE;
Ashish Jain5106d362016-05-11 19:23:33 +05306484 ret = -EINVAL;
6485 } else
6486 ret = 0;
6487 /* this is the best we can do */
6488 clock_gettime(CLOCK_MONOTONIC, timestamp);
Eric Laurent949a0892013-09-20 09:20:13 -07006489 } else {
6490 if (out->pcm) {
Weiyin Jiangd4633762018-03-16 12:05:03 +08006491 unsigned int avail;
6492 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
George Gao62ebc722019-07-29 16:29:44 -07006493 uint64_t signed_frames = 0;
Weiyin Jiang0d25bcf2019-08-30 15:27:23 +08006494 uint64_t frames_temp = 0;
George Gao62ebc722019-07-29 16:29:44 -07006495
Andy Hunga1f48fa2019-07-01 18:14:53 -07006496 if (out->kernel_buffer_size > avail) {
6497 frames_temp = out->last_fifo_frames_remaining = out->kernel_buffer_size - avail;
6498 } else {
6499 ALOGW("%s: avail:%u > kernel_buffer_size:%zu clamping!",
6500 __func__, avail, out->kernel_buffer_size);
6501 avail = out->kernel_buffer_size;
6502 frames_temp = out->last_fifo_frames_remaining = 0;
6503 }
6504 out->last_fifo_valid = true;
6505 out->last_fifo_time_ns = audio_utils_ns_from_timespec(timestamp);
6506
Weiyin Jiang0d25bcf2019-08-30 15:27:23 +08006507 if (out->written >= frames_temp)
6508 signed_frames = out->written - frames_temp;
George Gao62ebc722019-07-29 16:29:44 -07006509
Andy Hunga1f48fa2019-07-01 18:14:53 -07006510 ALOGVV("%s: frames:%lld avail:%u kernel_buffer_size:%zu",
6511 __func__, (long long)signed_frames, avail, out->kernel_buffer_size);
6512
Weiyin Jiangd4633762018-03-16 12:05:03 +08006513 // This adjustment accounts for buffering after app processor.
6514 // It is based on estimated DSP latency per use case, rather than exact.
George Gao9ba8a142020-07-23 14:30:03 -07006515 frames_temp = platform_render_latency(out) *
Robert Lee58215542019-07-15 20:55:12 +08006516 out->sample_rate / 1000000LL;
Weiyin Jiang0d25bcf2019-08-30 15:27:23 +08006517 if (signed_frames >= frames_temp)
6518 signed_frames -= frames_temp;
Aniket Kumar Lataff613152017-07-18 18:19:21 -07006519
Weiyin Jiangd4633762018-03-16 12:05:03 +08006520 // Adjustment accounts for A2dp encoder latency with non offload usecases
6521 // Note: Encoder latency is returned in ms, while platform_render_latency in us.
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08006522 if (is_a2dp_out_device_type(&out->device_list)) {
Weiyin Jiang0d25bcf2019-08-30 15:27:23 +08006523 frames_temp = audio_extn_a2dp_get_encoder_latency() * out->sample_rate / 1000;
6524 if (signed_frames >= frames_temp)
6525 signed_frames -= frames_temp;
Weiyin Jiangd4633762018-03-16 12:05:03 +08006526 }
6527
6528 // It would be unusual for this value to be negative, but check just in case ...
George Gao62ebc722019-07-29 16:29:44 -07006529 *frames = signed_frames;
6530 ret = 0;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07006531 }
Eric Laurenta7a33042019-07-10 16:20:22 -07006532 } else if (out->card_status == CARD_STATUS_OFFLINE ||
6533 // audioflinger still needs position updates when A2DP is suspended
Jasmine Cha5c2517f2019-09-09 11:07:28 +08006534 (is_a2dp_out_device_type(&out->device_list) && audio_extn_a2dp_source_is_suspended())) {
Ashish Jainbbce4322016-02-16 13:25:27 +05306535 *frames = out->written;
6536 clock_gettime(CLOCK_MONOTONIC, timestamp);
Manisha Agarwal7b3e3772019-02-20 14:33:45 +05306537 if (is_offload_usecase(out->usecase))
6538 ret = -EINVAL;
6539 else
6540 ret = 0;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07006541 }
6542 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07006543 pthread_mutex_unlock(&out->lock);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07006544 return ret;
6545}
6546
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006547static int out_set_callback(struct audio_stream_out *stream,
6548 stream_callback_t callback, void *cookie)
6549{
6550 struct stream_out *out = (struct stream_out *)stream;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08006551 int ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006552
6553 ALOGV("%s", __func__);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006554 lock_output_stream(out);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08006555 out->client_callback = callback;
6556 out->client_cookie = cookie;
6557 if (out->adsp_hdlr_stream_handle) {
6558 ret = audio_extn_adsp_hdlr_stream_set_callback(
6559 out->adsp_hdlr_stream_handle,
6560 callback,
6561 cookie);
6562 if (ret)
6563 ALOGW("%s:adsp hdlr callback registration failed %d",
6564 __func__, ret);
6565 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006566 pthread_mutex_unlock(&out->lock);
6567 return 0;
6568}
6569
6570static int out_pause(struct audio_stream_out* stream)
6571{
6572 struct stream_out *out = (struct stream_out *)stream;
6573 int status = -ENOSYS;
6574 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07006575 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07006576 ALOGD("copl(%p):pause compress driver", out);
Sujin Panicker7c7b6f92020-04-03 12:57:55 +05306577 status = -ENODATA;
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006578 lock_output_stream(out);
Weiyin Jiang280ea742020-09-08 20:28:22 +08006579 pthread_mutex_lock(&out->latch_lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006580 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306581 if (out->card_status != CARD_STATUS_OFFLINE)
Naresh Tanniru80659832014-06-04 18:17:56 +05306582 status = compress_pause(out->compr);
6583
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006584 out->offload_state = OFFLOAD_STATE_PAUSED;
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08006585
Mingming Yin21854652016-04-13 11:54:02 -07006586 if (audio_extn_passthru_is_active()) {
6587 ALOGV("offload use case, pause passthru");
6588 audio_extn_passthru_on_pause(out);
6589 }
6590
Dhanalakshmi Siddani79415e72015-03-23 11:54:47 +05306591 audio_extn_dts_eagle_fade(adev, false, out);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08006592 audio_extn_dts_notify_playback_state(out->usecase, 0,
6593 out->sample_rate, popcount(out->channel_mask),
6594 0);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006595 }
Weiyin Jiang280ea742020-09-08 20:28:22 +08006596 pthread_mutex_unlock(&out->latch_lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006597 pthread_mutex_unlock(&out->lock);
6598 }
6599 return status;
6600}
6601
6602static int out_resume(struct audio_stream_out* stream)
6603{
6604 struct stream_out *out = (struct stream_out *)stream;
6605 int status = -ENOSYS;
6606 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07006607 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07006608 ALOGD("copl(%p):resume compress driver", out);
Sujin Panicker7c7b6f92020-04-03 12:57:55 +05306609 status = -ENODATA;
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006610 lock_output_stream(out);
Weiyin Jiang280ea742020-09-08 20:28:22 +08006611 pthread_mutex_lock(&out->latch_lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006612 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306613 if (out->card_status != CARD_STATUS_OFFLINE) {
Naresh Tanniru80659832014-06-04 18:17:56 +05306614 status = compress_resume(out->compr);
Mingming Yin21854652016-04-13 11:54:02 -07006615 }
6616 if (!status) {
6617 out->offload_state = OFFLOAD_STATE_PLAYING;
6618 }
Dhanalakshmi Siddani79415e72015-03-23 11:54:47 +05306619 audio_extn_dts_eagle_fade(adev, true, out);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08006620 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
6621 popcount(out->channel_mask), 1);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006622 }
Weiyin Jiang280ea742020-09-08 20:28:22 +08006623 pthread_mutex_unlock(&out->latch_lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006624 pthread_mutex_unlock(&out->lock);
6625 }
6626 return status;
6627}
6628
6629static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
6630{
6631 struct stream_out *out = (struct stream_out *)stream;
6632 int status = -ENOSYS;
6633 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07006634 if (is_offload_usecase(out->usecase)) {
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006635 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006636 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
6637 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
6638 else
6639 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
6640 pthread_mutex_unlock(&out->lock);
6641 }
6642 return status;
6643}
6644
6645static int out_flush(struct audio_stream_out* stream)
6646{
6647 struct stream_out *out = (struct stream_out *)stream;
6648 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07006649 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07006650 ALOGD("copl(%p):calling compress flush", out);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006651 lock_output_stream(out);
Weiyin Jiang280ea742020-09-08 20:28:22 +08006652 pthread_mutex_lock(&out->latch_lock);
Haynes Mathew Georgeafe54d82016-09-21 14:39:19 -07006653 if (out->offload_state == OFFLOAD_STATE_PAUSED) {
Gautam Manam14c198b2020-12-24 14:08:04 +05306654 pthread_mutex_unlock(&out->latch_lock);
Haynes Mathew Georgeafe54d82016-09-21 14:39:19 -07006655 stop_compressed_output_l(out);
Haynes Mathew Georgeafe54d82016-09-21 14:39:19 -07006656 } else {
6657 ALOGW("%s called in invalid state %d", __func__, out->offload_state);
Gautam Manam14c198b2020-12-24 14:08:04 +05306658 pthread_mutex_unlock(&out->latch_lock);
Haynes Mathew Georgeafe54d82016-09-21 14:39:19 -07006659 }
Weiyin Jiang547e4152017-09-14 17:24:18 +08006660 out->written = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006661 pthread_mutex_unlock(&out->lock);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07006662 ALOGD("copl(%p):out of compress flush", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006663 return 0;
6664 }
6665 return -ENOSYS;
6666}
6667
Haynes Mathew George16081042017-05-31 17:16:49 -07006668static int out_stop(const struct audio_stream_out* stream)
6669{
6670 struct stream_out *out = (struct stream_out *)stream;
6671 struct audio_device *adev = out->dev;
6672 int ret = -ENOSYS;
6673
6674 ALOGV("%s", __func__);
6675 pthread_mutex_lock(&adev->lock);
6676 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP && !out->standby &&
6677 out->playback_started && out->pcm != NULL) {
6678 pcm_stop(out->pcm);
6679 ret = stop_output_stream(out);
6680 out->playback_started = false;
6681 }
6682 pthread_mutex_unlock(&adev->lock);
6683 return ret;
6684}
6685
6686static int out_start(const struct audio_stream_out* stream)
6687{
6688 struct stream_out *out = (struct stream_out *)stream;
6689 struct audio_device *adev = out->dev;
6690 int ret = -ENOSYS;
6691
6692 ALOGV("%s", __func__);
6693 pthread_mutex_lock(&adev->lock);
6694 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP && !out->standby &&
6695 !out->playback_started && out->pcm != NULL) {
6696 ret = start_output_stream(out);
6697 if (ret == 0) {
6698 out->playback_started = true;
6699 }
6700 }
6701 pthread_mutex_unlock(&adev->lock);
6702 return ret;
6703}
6704
6705/*
6706 * Modify config->period_count based on min_size_frames
6707 */
6708static void adjust_mmap_period_count(struct pcm_config *config, int32_t min_size_frames)
6709{
6710 int periodCountRequested = (min_size_frames + config->period_size - 1)
6711 / config->period_size;
6712 int periodCount = MMAP_PERIOD_COUNT_MIN;
6713
6714 ALOGV("%s original config.period_size = %d config.period_count = %d",
6715 __func__, config->period_size, config->period_count);
6716
6717 while (periodCount < periodCountRequested && (periodCount * 2) < MMAP_PERIOD_COUNT_MAX) {
6718 periodCount *= 2;
6719 }
6720 config->period_count = periodCount;
6721
6722 ALOGV("%s requested config.period_count = %d", __func__, config->period_count);
6723}
6724
Phil Burkfe17efd2019-03-25 10:23:35 -07006725// Read offset for the positional timestamp from a persistent vendor property.
6726// This is to workaround apparent inaccuracies in the timing information that
6727// is used by the AAudio timing model. The inaccuracies can cause glitches.
6728static int64_t get_mmap_out_time_offset() {
6729 const int32_t kDefaultOffsetMicros = 0;
6730 int32_t mmap_time_offset_micros = property_get_int32(
Weiyin Jiangbd68b4d2019-05-17 16:29:50 +08006731 "persist.vendor.audio.out_mmap_delay_micros", kDefaultOffsetMicros);
Phil Burkfe17efd2019-03-25 10:23:35 -07006732 ALOGI("mmap_time_offset_micros = %d for output", mmap_time_offset_micros);
6733 return mmap_time_offset_micros * (int64_t)1000;
6734}
6735
Haynes Mathew George16081042017-05-31 17:16:49 -07006736static int out_create_mmap_buffer(const struct audio_stream_out *stream,
6737 int32_t min_size_frames,
6738 struct audio_mmap_buffer_info *info)
6739{
6740 struct stream_out *out = (struct stream_out *)stream;
6741 struct audio_device *adev = out->dev;
6742 int ret = 0;
Aalique Grahame1f123102017-10-12 10:38:32 -07006743 unsigned int offset1 = 0;
6744 unsigned int frames1 = 0;
Haynes Mathew George16081042017-05-31 17:16:49 -07006745 const char *step = "";
Haynes Mathew Georgeef514882017-05-01 17:46:23 -07006746 uint32_t mmap_size;
Arun Mirpuri5d170872019-03-26 13:21:31 -07006747 uint32_t buffer_size;
Haynes Mathew George16081042017-05-31 17:16:49 -07006748
Arun Mirpuri5d170872019-03-26 13:21:31 -07006749 ALOGD("%s", __func__);
Sharad Sanglec6f32552018-05-04 16:15:38 +05306750 lock_output_stream(out);
Haynes Mathew George16081042017-05-31 17:16:49 -07006751 pthread_mutex_lock(&adev->lock);
6752
Sharad Sanglec6f32552018-05-04 16:15:38 +05306753 if (CARD_STATUS_OFFLINE == out->card_status ||
6754 CARD_STATUS_OFFLINE == adev->card_status) {
6755 ALOGW("out->card_status or adev->card_status offline, try again");
6756 ret = -EIO;
6757 goto exit;
6758 }
Sujin Panicker7c7b6f92020-04-03 12:57:55 +05306759 if (info == NULL || !(min_size_frames > 0 && min_size_frames < INT32_MAX)) {
Haynes Mathew George16081042017-05-31 17:16:49 -07006760 ALOGE("%s: info = %p, min_size_frames = %d", __func__, info, min_size_frames);
6761 ret = -EINVAL;
6762 goto exit;
6763 }
6764 if (out->usecase != USECASE_AUDIO_PLAYBACK_MMAP || !out->standby) {
6765 ALOGE("%s: usecase = %d, standby = %d", __func__, out->usecase, out->standby);
6766 ret = -ENOSYS;
6767 goto exit;
6768 }
6769 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
6770 if (out->pcm_device_id < 0) {
6771 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
6772 __func__, out->pcm_device_id, out->usecase);
6773 ret = -EINVAL;
6774 goto exit;
6775 }
6776
6777 adjust_mmap_period_count(&out->config, min_size_frames);
6778
Arun Mirpuri5d170872019-03-26 13:21:31 -07006779 ALOGD("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
Haynes Mathew George16081042017-05-31 17:16:49 -07006780 __func__, adev->snd_card, out->pcm_device_id, out->config.channels);
6781 out->pcm = pcm_open(adev->snd_card, out->pcm_device_id,
6782 (PCM_OUT | PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC), &out->config);
Satish Babu Patakokila54ce83d2018-07-06 18:00:37 +05306783 if (errno == ENETRESET && !pcm_is_ready(out->pcm)) {
Sharad Sanglec6f32552018-05-04 16:15:38 +05306784 ALOGE("%s: pcm_open failed errno:%d\n", __func__, errno);
6785 out->card_status = CARD_STATUS_OFFLINE;
6786 adev->card_status = CARD_STATUS_OFFLINE;
6787 ret = -EIO;
6788 goto exit;
6789 }
6790
Haynes Mathew George16081042017-05-31 17:16:49 -07006791 if (out->pcm == NULL || !pcm_is_ready(out->pcm)) {
6792 step = "open";
6793 ret = -ENODEV;
6794 goto exit;
6795 }
6796 ret = pcm_mmap_begin(out->pcm, &info->shared_memory_address, &offset1, &frames1);
6797 if (ret < 0) {
6798 step = "begin";
6799 goto exit;
6800 }
juyuchen626833d2019-06-04 16:48:02 +08006801
6802 info->flags = 0;
Haynes Mathew George16081042017-05-31 17:16:49 -07006803 info->buffer_size_frames = pcm_get_buffer_size(out->pcm);
Arun Mirpuri5d170872019-03-26 13:21:31 -07006804 buffer_size = pcm_frames_to_bytes(out->pcm, info->buffer_size_frames);
Haynes Mathew George16081042017-05-31 17:16:49 -07006805 info->burst_size_frames = out->config.period_size;
Haynes Mathew Georgeef514882017-05-01 17:46:23 -07006806 ret = platform_get_mmap_data_fd(adev->platform,
6807 out->pcm_device_id, 0 /*playback*/,
6808 &info->shared_memory_fd,
6809 &mmap_size);
6810 if (ret < 0) {
Arun Mirpuri5d170872019-03-26 13:21:31 -07006811 // Fall back to non exclusive mode
6812 info->shared_memory_fd = pcm_get_poll_fd(out->pcm);
6813 } else {
Phil Burkd898ba62019-06-20 12:49:01 -07006814 out->mmap_shared_memory_fd = info->shared_memory_fd; // for closing later
6815 ALOGV("%s: opened mmap_shared_memory_fd = %d", __func__, out->mmap_shared_memory_fd);
6816
Arun Mirpuri5d170872019-03-26 13:21:31 -07006817 if (mmap_size < buffer_size) {
6818 step = "mmap";
6819 goto exit;
6820 }
juyuchen626833d2019-06-04 16:48:02 +08006821 info->flags |= AUDIO_MMAP_APPLICATION_SHAREABLE;
Haynes Mathew Georgeef514882017-05-01 17:46:23 -07006822 }
Haynes Mathew George16081042017-05-31 17:16:49 -07006823 memset(info->shared_memory_address, 0, pcm_frames_to_bytes(out->pcm,
Haynes Mathew Georgeef514882017-05-01 17:46:23 -07006824 info->buffer_size_frames));
Haynes Mathew George16081042017-05-31 17:16:49 -07006825
6826 ret = pcm_mmap_commit(out->pcm, 0, MMAP_PERIOD_SIZE);
6827 if (ret < 0) {
6828 step = "commit";
6829 goto exit;
6830 }
6831
Phil Burkfe17efd2019-03-25 10:23:35 -07006832 out->mmap_time_offset_nanos = get_mmap_out_time_offset();
6833
Haynes Mathew George16081042017-05-31 17:16:49 -07006834 out->standby = false;
6835 ret = 0;
6836
Arun Mirpuri5d170872019-03-26 13:21:31 -07006837 ALOGD("%s: got mmap buffer address %p info->buffer_size_frames %d",
Haynes Mathew George16081042017-05-31 17:16:49 -07006838 __func__, info->shared_memory_address, info->buffer_size_frames);
6839
6840exit:
6841 if (ret != 0) {
6842 if (out->pcm == NULL) {
6843 ALOGE("%s: %s - %d", __func__, step, ret);
6844 } else {
6845 ALOGE("%s: %s %s", __func__, step, pcm_get_error(out->pcm));
6846 pcm_close(out->pcm);
6847 out->pcm = NULL;
6848 }
6849 }
6850 pthread_mutex_unlock(&adev->lock);
Sharad Sanglec6f32552018-05-04 16:15:38 +05306851 pthread_mutex_unlock(&out->lock);
Haynes Mathew George16081042017-05-31 17:16:49 -07006852 return ret;
6853}
6854
6855static int out_get_mmap_position(const struct audio_stream_out *stream,
6856 struct audio_mmap_position *position)
6857{
6858 struct stream_out *out = (struct stream_out *)stream;
6859 ALOGVV("%s", __func__);
6860 if (position == NULL) {
6861 return -EINVAL;
6862 }
6863 if (out->usecase != USECASE_AUDIO_PLAYBACK_MMAP) {
Haynes Mathew George4ab3ba92017-12-11 14:49:43 -08006864 ALOGE("%s: called on %s", __func__, use_case_table[out->usecase]);
Haynes Mathew George16081042017-05-31 17:16:49 -07006865 return -ENOSYS;
6866 }
6867 if (out->pcm == NULL) {
6868 return -ENOSYS;
6869 }
6870
6871 struct timespec ts = { 0, 0 };
6872 int ret = pcm_mmap_get_hw_ptr(out->pcm, (unsigned int *)&position->position_frames, &ts);
6873 if (ret < 0) {
6874 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
6875 return ret;
6876 }
Phil Burkfe17efd2019-03-25 10:23:35 -07006877 position->time_nanoseconds = ts.tv_sec*1000000000LL + ts.tv_nsec
6878 + out->mmap_time_offset_nanos;
Haynes Mathew George16081042017-05-31 17:16:49 -07006879 return 0;
6880}
6881
6882
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006883/** audio_stream_in implementation **/
6884static uint32_t in_get_sample_rate(const struct audio_stream *stream)
6885{
6886 struct stream_in *in = (struct stream_in *)stream;
6887
6888 return in->config.rate;
6889}
6890
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07006891static int in_set_sample_rate(struct audio_stream *stream __unused,
6892 uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006893{
6894 return -ENOSYS;
6895}
6896
6897static size_t in_get_buffer_size(const struct audio_stream *stream)
6898{
6899 struct stream_in *in = (struct stream_in *)stream;
6900
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08006901 if(in->usecase == USECASE_COMPRESS_VOIP_CALL)
6902 return voice_extn_compress_voip_in_get_buffer_size(in);
Mingming Yine62d7842013-10-25 16:26:03 -07006903 else if(audio_extn_compr_cap_usecase_supported(in->usecase))
6904 return audio_extn_compr_cap_get_buffer_size(in->config.format);
Dhananjay Kumar7dbe3562019-08-30 05:49:33 +05306905 else if(audio_extn_cin_attached_usecase(in))
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05306906 return audio_extn_cin_get_buffer_size(in);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08006907
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006908 return in->config.period_size * in->af_period_multiplier *
6909 audio_stream_in_frame_size((const struct audio_stream_in *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006910}
6911
6912static uint32_t in_get_channels(const struct audio_stream *stream)
6913{
6914 struct stream_in *in = (struct stream_in *)stream;
6915
6916 return in->channel_mask;
6917}
6918
6919static audio_format_t in_get_format(const struct audio_stream *stream)
6920{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08006921 struct stream_in *in = (struct stream_in *)stream;
6922
6923 return in->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006924}
6925
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07006926static int in_set_format(struct audio_stream *stream __unused,
6927 audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006928{
6929 return -ENOSYS;
6930}
6931
6932static int in_standby(struct audio_stream *stream)
6933{
6934 struct stream_in *in = (struct stream_in *)stream;
6935 struct audio_device *adev = in->dev;
6936 int status = 0;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05306937 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
6938 stream, in->usecase, use_case_table[in->usecase]);
Haynes Mathew George16081042017-05-31 17:16:49 -07006939 bool do_stop = true;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05306940
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006941 lock_input_stream(in);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07006942 if (!in->standby && in->is_st_session) {
6943 ALOGD("%s: sound trigger pcm stop lab", __func__);
6944 audio_extn_sound_trigger_stop_lab(in);
George Gao3018ede2019-10-23 13:23:00 -07006945 if (adev->num_va_sessions > 0)
6946 adev->num_va_sessions--;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07006947 in->standby = 1;
6948 }
6949
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006950 if (!in->standby) {
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07006951 if (adev->adm_deregister_stream)
6952 adev->adm_deregister_stream(adev->adm_data, in->capture_handle);
6953
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08006954 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006955 in->standby = true;
Zhou Songa8895042016-07-05 17:54:22 +08006956 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
kunleizbecba2d2017-09-07 13:37:16 +08006957 do_stop = false;
Zhou Songa8895042016-07-05 17:54:22 +08006958 voice_extn_compress_voip_close_input_stream(stream);
6959 ALOGD("VOIP input entered standby");
Haynes Mathew George16081042017-05-31 17:16:49 -07006960 } else if (in->usecase == USECASE_AUDIO_RECORD_MMAP) {
6961 do_stop = in->capture_started;
6962 in->capture_started = false;
Phil Burkd898ba62019-06-20 12:49:01 -07006963 if (in->mmap_shared_memory_fd >= 0) {
6964 ALOGV("%s: closing mmap_shared_memory_fd = %d",
6965 __func__, in->mmap_shared_memory_fd);
6966 close(in->mmap_shared_memory_fd);
6967 in->mmap_shared_memory_fd = -1;
6968 }
Zhou Songa8895042016-07-05 17:54:22 +08006969 } else {
Dhananjay Kumar7dbe3562019-08-30 05:49:33 +05306970 if (audio_extn_cin_attached_usecase(in))
Manish Dewangan46e07982018-12-13 18:18:59 +05306971 audio_extn_cin_close_input_stream(in);
kunleizbecba2d2017-09-07 13:37:16 +08006972 }
6973
Arun Mirpuri5d170872019-03-26 13:21:31 -07006974 if (in->pcm) {
6975 ATRACE_BEGIN("pcm_in_close");
6976 pcm_close(in->pcm);
6977 ATRACE_END();
6978 in->pcm = NULL;
6979 }
6980
Carter Hsu2e429db2019-05-14 18:50:52 +08006981 if (do_stop)
Zhou Songa8895042016-07-05 17:54:22 +08006982 status = stop_input_stream(in);
Quinn Malef6050362019-01-30 15:55:40 -08006983
George Gao3018ede2019-10-23 13:23:00 -07006984 if (in->source == AUDIO_SOURCE_VOICE_RECOGNITION) {
6985 if (adev->num_va_sessions > 0)
6986 adev->num_va_sessions--;
6987 }
Quinn Malef6050362019-01-30 15:55:40 -08006988
Eric Laurent150dbfe2013-02-27 14:31:02 -08006989 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006990 }
6991 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07006992 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006993 return status;
6994}
6995
Aalique Grahame22e49102018-12-18 14:23:57 -08006996static int in_dump(const struct audio_stream *stream,
6997 int fd)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006998{
Aalique Grahame22e49102018-12-18 14:23:57 -08006999 struct stream_in *in = (struct stream_in *)stream;
7000
7001 // We try to get the lock for consistency,
7002 // but it isn't necessary for these variables.
7003 // If we're not in standby, we may be blocked on a read.
7004 const bool locked = (pthread_mutex_trylock(&in->lock) == 0);
7005 dprintf(fd, " Standby: %s\n", in->standby ? "yes" : "no");
7006 dprintf(fd, " Frames read: %lld\n", (long long)in->frames_read);
7007 dprintf(fd, " Frames muted: %lld\n", (long long)in->frames_muted);
Dechen Chai22768452021-07-30 09:29:16 +05307008#ifndef LINUX_ENABLED
Andy Hungc6bfd4a2019-07-01 18:26:00 -07007009 char buffer[256]; // for statistics formatting
7010 if (in->start_latency_ms.n > 0) {
7011 simple_stats_to_string(&in->start_latency_ms, buffer, sizeof(buffer));
7012 dprintf(fd, " Start latency ms: %s\n", buffer);
7013 }
Dechen Chai22768452021-07-30 09:29:16 +05307014#endif
Aalique Grahame22e49102018-12-18 14:23:57 -08007015 if (locked) {
7016 pthread_mutex_unlock(&in->lock);
7017 }
Dechen Chai22768452021-07-30 09:29:16 +05307018#ifndef LINUX_ENABLED
Aalique Grahame22e49102018-12-18 14:23:57 -08007019 // dump error info
7020 (void)error_log_dump(
7021 in->error_log, fd, " " /* prefix */, 0 /* lines */, 0 /* limit_ns */);
Dechen Chai22768452021-07-30 09:29:16 +05307022#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007023 return 0;
7024}
7025
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05307026static void in_snd_mon_cb(void * stream, struct str_parms * parms)
7027{
7028 if (!stream || !parms)
7029 return;
7030
7031 struct stream_in *in = (struct stream_in *)stream;
7032 struct audio_device *adev = in->dev;
7033
7034 card_status_t status;
7035 int card;
7036 if (parse_snd_card_status(parms, &card, &status) < 0)
7037 return;
7038
7039 pthread_mutex_lock(&adev->lock);
7040 bool valid_cb = (card == adev->snd_card);
7041 pthread_mutex_unlock(&adev->lock);
7042
7043 if (!valid_cb)
7044 return;
7045
7046 lock_input_stream(in);
7047 if (in->card_status != status)
7048 in->card_status = status;
7049 pthread_mutex_unlock(&in->lock);
7050
7051 ALOGW("in_snd_mon_cb for card %d usecase %s, status %s", card,
7052 use_case_table[in->usecase],
7053 status == CARD_STATUS_OFFLINE ? "offline" : "online");
7054
7055 // a better solution would be to report error back to AF and let
7056 // it put the stream to standby
7057 if (status == CARD_STATUS_OFFLINE)
7058 in_standby(&in->stream.common);
7059
7060 return;
7061}
7062
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07007063int route_input_stream(struct stream_in *in,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08007064 struct listnode *devices,
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07007065 audio_source_t source)
7066{
7067 struct audio_device *adev = in->dev;
7068 int ret = 0;
7069
7070 lock_input_stream(in);
7071 pthread_mutex_lock(&adev->lock);
7072
7073 /* no audio source uses val == 0 */
7074 if ((in->source != source) && (source != AUDIO_SOURCE_DEFAULT)) {
7075 in->source = source;
7076 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
7077 (in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
7078 (voice_extn_compress_voip_is_format_supported(in->format)) &&
7079 (in->config.rate == 8000 || in->config.rate == 16000 ||
7080 in->config.rate == 32000 || in->config.rate == 48000 ) &&
7081 (audio_channel_count_from_in_mask(in->channel_mask) == 1)) {
7082 ret = voice_extn_compress_voip_open_input_stream(in);
7083 if (ret != 0) {
7084 ALOGE("%s: Compress voip input cannot be opened, error:%d",
7085 __func__, ret);
7086 }
7087 }
7088 }
7089
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08007090 if (!compare_devices(&in->device_list, devices) && !list_empty(devices) &&
7091 is_audio_in_device_type(devices)) {
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07007092 // Workaround: If routing to an non existing usb device, fail gracefully
7093 // The routing request will otherwise block during 10 second
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08007094 struct str_parms *usb_addr =
7095 str_parms_create_str(get_usb_device_address(devices));
7096 if (is_usb_in_device_type(devices) && usb_addr &&
Weiyin Jiangabedea32020-12-09 12:49:19 +08007097 !audio_extn_usb_connected(NULL)) {
7098 ALOGW("%s: ignoring rerouting to non existing USB", __func__);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07007099 ret = -ENOSYS;
7100 } else {
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07007101 /* If recording is in progress, change the tx device to new device */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08007102 assign_devices(&in->device_list, devices);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07007103 if (!in->standby && !in->is_st_session) {
7104 ALOGV("update input routing change");
7105 // inform adm before actual routing to prevent glitches.
7106 if (adev->adm_on_routing_change) {
7107 adev->adm_on_routing_change(adev->adm_data,
7108 in->capture_handle);
7109 ret = select_devices(adev, in->usecase);
7110 if (in->usecase == USECASE_AUDIO_RECORD_LOW_LATENCY)
7111 adev->adm_routing_changed = true;
7112 }
7113 }
7114 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08007115 if (usb_addr)
7116 str_parms_destroy(usb_addr);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07007117 }
7118 pthread_mutex_unlock(&adev->lock);
7119 pthread_mutex_unlock(&in->lock);
7120
Aniket Kumar Latad13758f2020-08-06 15:11:36 -07007121 ALOGV("%s: exit: status(%d)", __func__, ret);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07007122 return ret;
7123}
7124
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007125static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
7126{
7127 struct stream_in *in = (struct stream_in *)stream;
7128 struct audio_device *adev = in->dev;
7129 struct str_parms *parms;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007130 char value[32];
Sujin Panicker7c7b6f92020-04-03 12:57:55 +05307131 int err = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007132
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05307133 ALOGD("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007134 parms = str_parms_create_str(kvpairs);
7135
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05307136 if (!parms)
7137 goto error;
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07007138 lock_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08007139 pthread_mutex_lock(&adev->lock);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08007140
Sujin Panicker7c7b6f92020-04-03 12:57:55 +05307141 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_PROFILE, value, sizeof(value));
7142 if (err >= 0) {
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +05307143 strlcpy(in->profile, value, sizeof(in->profile));
7144 ALOGV("updating stream profile with value '%s'", in->profile);
7145 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
7146 &adev->streams_input_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08007147 &in->device_list, in->flags, in->format,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +05307148 in->sample_rate, in->bit_width,
7149 in->profile, &in->app_type_cfg);
7150 }
7151
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007152 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08007153 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007154
7155 str_parms_destroy(parms);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05307156error:
Sujin Panicker7c7b6f92020-04-03 12:57:55 +05307157 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007158}
7159
7160static char* in_get_parameters(const struct audio_stream *stream,
7161 const char *keys)
7162{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007163 struct stream_in *in = (struct stream_in *)stream;
7164 struct str_parms *query = str_parms_create_str(keys);
7165 char *str;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007166 struct str_parms *reply = str_parms_create();
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07007167
7168 if (!query || !reply) {
Alexy Josephaee4fdd2016-01-29 13:02:07 -08007169 if (reply) {
7170 str_parms_destroy(reply);
7171 }
7172 if (query) {
7173 str_parms_destroy(query);
7174 }
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07007175 ALOGE("in_get_parameters: failed to create query or reply");
7176 return NULL;
7177 }
7178
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007179 ALOGV("%s: enter: keys - %s %s ", __func__, use_case_table[in->usecase], keys);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007180
7181 voice_extn_in_get_parameters(in, query, reply);
7182
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007183 stream_get_parameter_channels(query, reply,
7184 &in->supported_channel_masks[0]);
7185 stream_get_parameter_formats(query, reply,
7186 &in->supported_formats[0]);
7187 stream_get_parameter_rates(query, reply,
7188 &in->supported_sample_rates[0]);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007189 str = str_parms_to_str(reply);
7190 str_parms_destroy(query);
7191 str_parms_destroy(reply);
7192
7193 ALOGV("%s: exit: returns - %s", __func__, str);
7194 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007195}
7196
Aalique Grahame22e49102018-12-18 14:23:57 -08007197static int in_set_gain(struct audio_stream_in *stream,
7198 float gain)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007199{
Aalique Grahame22e49102018-12-18 14:23:57 -08007200 struct stream_in *in = (struct stream_in *)stream;
7201 char mixer_ctl_name[128];
7202 struct mixer_ctl *ctl;
7203 int ctl_value;
7204
7205 ALOGV("%s: gain %f", __func__, gain);
7206
7207 if (stream == NULL)
7208 return -EINVAL;
7209
7210 /* in_set_gain() only used to silence MMAP capture for now */
7211 if (in->usecase != USECASE_AUDIO_RECORD_MMAP)
7212 return -ENOSYS;
7213
7214 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name), "Capture %d Volume", in->pcm_device_id);
7215
7216 ctl = mixer_get_ctl_by_name(in->dev->mixer, mixer_ctl_name);
7217 if (!ctl) {
7218 ALOGW("%s: Could not get ctl for mixer cmd - %s",
7219 __func__, mixer_ctl_name);
7220 return -ENOSYS;
7221 }
7222
7223 if (gain < RECORD_GAIN_MIN)
7224 gain = RECORD_GAIN_MIN;
7225 else if (gain > RECORD_GAIN_MAX)
7226 gain = RECORD_GAIN_MAX;
7227 ctl_value = (int)(RECORD_VOLUME_CTL_MAX * gain);
7228
7229 mixer_ctl_set_value(ctl, 0, ctl_value);
7230
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007231 return 0;
7232}
7233
7234static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
7235 size_t bytes)
7236{
7237 struct stream_in *in = (struct stream_in *)stream;
Pallavid7c7a272018-01-16 11:22:55 +05307238
7239 if (in == NULL) {
7240 ALOGE("%s: stream_in ptr is NULL", __func__);
7241 return -EINVAL;
7242 }
7243
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007244 struct audio_device *adev = in->dev;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05307245 int ret = -1;
Dhananjay Kumar97b81e32019-05-15 21:00:21 +05307246 size_t bytes_read = 0, frame_size = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007247
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07007248 lock_input_stream(in);
Naresh Tanniru4c630392014-05-12 01:05:52 +05307249
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07007250 if (in->is_st_session) {
7251 ALOGVV(" %s: reading on st session bytes=%zu", __func__, bytes);
7252 /* Read from sound trigger HAL */
7253 audio_extn_sound_trigger_read(in, buffer, bytes);
Quinn Malef6050362019-01-30 15:55:40 -08007254 if (in->standby) {
George Gao3018ede2019-10-23 13:23:00 -07007255 if (adev->num_va_sessions < UINT_MAX)
7256 adev->num_va_sessions++;
Quinn Malef6050362019-01-30 15:55:40 -08007257 in->standby = 0;
7258 }
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07007259 pthread_mutex_unlock(&in->lock);
7260 return bytes;
7261 }
7262
Haynes Mathew George16081042017-05-31 17:16:49 -07007263 if (in->usecase == USECASE_AUDIO_RECORD_MMAP) {
7264 ret = -ENOSYS;
7265 goto exit;
7266 }
7267
Aniket Kumar Lata60586a92019-05-22 22:18:55 -07007268 if (in->usecase == USECASE_AUDIO_RECORD_LOW_LATENCY &&
7269 !in->standby && adev->adm_routing_changed) {
7270 ret = -ENOSYS;
7271 goto exit;
7272 }
7273
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007274 if (in->standby) {
Andy Hungc6bfd4a2019-07-01 18:26:00 -07007275 const int64_t startNs = systemTime(SYSTEM_TIME_MONOTONIC);
7276
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07007277 pthread_mutex_lock(&adev->lock);
7278 if (in->usecase == USECASE_COMPRESS_VOIP_CALL)
7279 ret = voice_extn_compress_voip_start_input_stream(in);
7280 else
7281 ret = start_input_stream(in);
George Gao3018ede2019-10-23 13:23:00 -07007282 if (!ret && in->source == AUDIO_SOURCE_VOICE_RECOGNITION) {
7283 if (adev->num_va_sessions < UINT_MAX)
7284 adev->num_va_sessions++;
7285 }
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07007286 pthread_mutex_unlock(&adev->lock);
7287 if (ret != 0) {
7288 goto exit;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007289 }
7290 in->standby = 0;
Dechen Chai22768452021-07-30 09:29:16 +05307291#ifndef LINUX_ENABLED
Andy Hungc6bfd4a2019-07-01 18:26:00 -07007292 // log startup time in ms.
7293 simple_stats_log(
7294 &in->start_latency_ms, (systemTime(SYSTEM_TIME_MONOTONIC) - startNs) * 1e-6);
Dechen Chai22768452021-07-30 09:29:16 +05307295#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007296 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007297
Deeraj Soman30cc7ae2019-03-18 16:26:55 +05307298 /* Avoid read if capture_stopped is set */
7299 if (android_atomic_acquire_load(&(in->capture_stopped)) > 0) {
7300 ALOGD("%s: force stopped catpure session, ignoring read request", __func__);
7301 ret = -EINVAL;
7302 goto exit;
7303 }
7304
Haynes Mathew George5beddd42016-06-27 18:33:40 -07007305 // what's the duration requested by the client?
7306 long ns = 0;
7307
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307308 if (in->pcm && in->config.rate)
Haynes Mathew George5beddd42016-06-27 18:33:40 -07007309 ns = pcm_bytes_to_frames(in->pcm, bytes)*1000000000LL/
7310 in->config.rate;
7311
Aniket Kumar Lata60586a92019-05-22 22:18:55 -07007312 ret = request_in_focus(in, ns);
7313 if (ret != 0)
7314 goto exit;
Haynes Mathew George5beddd42016-06-27 18:33:40 -07007315 bool use_mmap = is_mmap_usecase(in->usecase) || in->realtime;
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07007316
Dhananjay Kumar7dbe3562019-08-30 05:49:33 +05307317 if (audio_extn_cin_attached_usecase(in)) {
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307318 ret = audio_extn_cin_read(in, buffer, bytes, &bytes_read);
7319 } else if (in->pcm) {
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05307320 if (audio_extn_ssr_get_stream() == in) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07007321 ret = audio_extn_ssr_read(stream, buffer, bytes);
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05307322 } else if (audio_extn_compr_cap_usecase_supported(in->usecase)) {
Mingming Yine62d7842013-10-25 16:26:03 -07007323 ret = audio_extn_compr_cap_read(in, buffer, bytes);
Haynes Mathew George5beddd42016-06-27 18:33:40 -07007324 } else if (use_mmap) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07007325 ret = pcm_mmap_read(in->pcm, buffer, bytes);
Garmond Leunge2433c32017-09-28 21:51:22 -07007326 } else if (audio_extn_ffv_get_stream() == in) {
7327 ret = audio_extn_ffv_read(stream, buffer, bytes);
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05307328 } else {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07007329 ret = pcm_read(in->pcm, buffer, bytes);
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307330 /* data from DSP comes in 24_8 format, convert it to 8_24 */
7331 if (!ret && bytes > 0 && (in->format == AUDIO_FORMAT_PCM_8_24_BIT)) {
7332 if (audio_extn_utils_convert_format_24_8_to_8_24(buffer, bytes)
7333 != bytes) {
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05307334 ret = -EINVAL;
7335 goto exit;
7336 }
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307337 } else if (ret < 0) {
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05307338 ret = -errno;
7339 }
7340 }
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307341 /* bytes read is always set to bytes for non compress usecases */
7342 bytes_read = bytes;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007343 }
7344
Haynes Mathew George5beddd42016-06-27 18:33:40 -07007345 release_in_focus(in);
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07007346
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007347 /*
Quinn Malef6050362019-01-30 15:55:40 -08007348 * Instead of writing zeroes here, we could trust the hardware to always
7349 * provide zeroes when muted. This is also muted with voice recognition
7350 * usecases so that other clients do not have access to voice recognition
7351 * data.
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007352 */
Quinn Malef6050362019-01-30 15:55:40 -08007353 if ((ret == 0 && voice_get_mic_mute(adev) &&
7354 !voice_is_in_call_rec_stream(in) &&
Zhou Song62ea0282020-03-22 19:53:01 +08007355 (in->usecase != USECASE_AUDIO_RECORD_AFE_PROXY &&
7356 in->usecase != USECASE_AUDIO_RECORD_AFE_PROXY2)) ||
Quinn Malef6050362019-01-30 15:55:40 -08007357 (adev->num_va_sessions &&
7358 in->source != AUDIO_SOURCE_VOICE_RECOGNITION &&
7359 property_get_bool("persist.vendor.audio.va_concurrency_mute_enabled",
7360 false)))
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007361 memset(buffer, 0, bytes);
7362
7363exit:
Dhananjay Kumar97b81e32019-05-15 21:00:21 +05307364 frame_size = audio_stream_in_frame_size(stream);
7365 if (frame_size > 0)
7366 in->frames_read += bytes_read/frame_size;
7367
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07007368 if (-ENETRESET == ret)
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05307369 in->card_status = CARD_STATUS_OFFLINE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007370 pthread_mutex_unlock(&in->lock);
7371
7372 if (ret != 0) {
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05307373 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05307374 pthread_mutex_lock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05307375 voice_extn_compress_voip_close_input_stream(&in->stream.common);
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05307376 pthread_mutex_unlock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05307377 in->standby = true;
7378 }
Dhananjay Kumar7dbe3562019-08-30 05:49:33 +05307379 if (!audio_extn_cin_attached_usecase(in)) {
Sharad Sangled17c9122017-03-20 15:58:52 +05307380 bytes_read = bytes;
7381 memset(buffer, 0, bytes);
7382 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007383 in_standby(&in->stream.common);
Aniket Kumar Lata60586a92019-05-22 22:18:55 -07007384 if (in->usecase == USECASE_AUDIO_RECORD_LOW_LATENCY)
7385 adev->adm_routing_changed = false;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07007386 ALOGV("%s: read failed status %d- sleeping for buffer duration", __func__, ret);
Ashish Jainbbce4322016-02-16 13:25:27 +05307387 usleep((uint64_t)bytes * 1000000 / audio_stream_in_frame_size(stream) /
Naresh Tanniru4c630392014-05-12 01:05:52 +05307388 in_get_sample_rate(&in->stream.common));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007389 }
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307390 return bytes_read;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007391}
7392
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07007393static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007394{
7395 return 0;
7396}
7397
Aalique Grahame22e49102018-12-18 14:23:57 -08007398static int in_get_capture_position(const struct audio_stream_in *stream,
7399 int64_t *frames, int64_t *time)
7400{
7401 if (stream == NULL || frames == NULL || time == NULL) {
7402 return -EINVAL;
7403 }
7404 struct stream_in *in = (struct stream_in *)stream;
7405 int ret = -ENOSYS;
7406
7407 lock_input_stream(in);
7408 // note: ST sessions do not close the alsa pcm driver synchronously
7409 // on standby. Therefore, we may return an error even though the
7410 // pcm stream is still opened.
7411 if (in->standby) {
7412 ALOGE_IF(in->pcm != NULL && !in->is_st_session,
7413 "%s stream in standby but pcm not NULL for non ST session", __func__);
7414 goto exit;
7415 }
7416 if (in->pcm) {
7417 struct timespec timestamp;
7418 unsigned int avail;
7419 if (pcm_get_htimestamp(in->pcm, &avail, &timestamp) == 0) {
7420 *frames = in->frames_read + avail;
Robert Lee58215542019-07-15 20:55:12 +08007421 *time = timestamp.tv_sec * 1000000000LL + timestamp.tv_nsec
George Gao9ba8a142020-07-23 14:30:03 -07007422 - platform_capture_latency(in) * 1000LL;
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +05307423 //Adjustment accounts for A2dp decoder latency for recording usecase
7424 // Note: decoder latency is returned in ms, while platform_capture_latency in ns.
7425 if (is_a2dp_in_device_type(&in->device_list))
7426 *time -= audio_extn_a2dp_get_decoder_latency() * 1000000LL;
Aalique Grahame22e49102018-12-18 14:23:57 -08007427 ret = 0;
7428 }
7429 }
7430exit:
7431 pthread_mutex_unlock(&in->lock);
7432 return ret;
7433}
7434
Carter Hsu2e429db2019-05-14 18:50:52 +08007435static int in_update_effect_list(bool add, effect_handle_t effect,
7436 struct listnode *head)
7437{
7438 struct listnode *node;
7439 struct in_effect_list *elist = NULL;
7440 struct in_effect_list *target = NULL;
7441 int ret = 0;
7442
7443 if (!head)
7444 return ret;
7445
7446 list_for_each(node, head) {
7447 elist = node_to_item(node, struct in_effect_list, list);
7448 if (elist->handle == effect) {
7449 target = elist;
7450 break;
7451 }
7452 }
7453
7454 if (add) {
7455 if (target) {
7456 ALOGD("effect %p already exist", effect);
7457 return ret;
7458 }
7459
7460 target = (struct in_effect_list *)
7461 calloc(1, sizeof(struct in_effect_list));
7462
7463 if (!target) {
7464 ALOGE("%s:fail to allocate memory", __func__);
7465 return -ENOMEM;
7466 }
7467
7468 target->handle = effect;
7469 list_add_tail(head, &target->list);
7470 } else {
7471 if (target) {
7472 list_remove(&target->list);
7473 free(target);
7474 }
7475 }
7476
7477 return ret;
7478}
7479
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007480static int add_remove_audio_effect(const struct audio_stream *stream,
7481 effect_handle_t effect,
7482 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007483{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007484 struct stream_in *in = (struct stream_in *)stream;
7485 int status = 0;
7486 effect_descriptor_t desc;
7487
7488 status = (*effect)->get_descriptor(effect, &desc);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07007489 ALOGV("%s: status %d in->standby %d enable:%d", __func__, status, in->standby, enable);
7490
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007491 if (status != 0)
7492 return status;
7493
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07007494 lock_input_stream(in);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007495 pthread_mutex_lock(&in->dev->lock);
kunleizd96526c2018-04-09 11:12:32 +08007496 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
Carter Hsu2e429db2019-05-14 18:50:52 +08007497 in->source == AUDIO_SOURCE_VOICE_RECOGNITION ||
7498 adev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007499 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
Carter Hsu2e429db2019-05-14 18:50:52 +08007500
7501 in_update_effect_list(enable, effect, &in->aec_list);
7502 enable = !list_empty(&in->aec_list);
7503 if (enable == in->enable_aec)
7504 goto exit;
7505
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007506 in->enable_aec = enable;
Carter Hsu2e429db2019-05-14 18:50:52 +08007507 ALOGD("AEC enable %d", enable);
7508
Aalique Grahame22e49102018-12-18 14:23:57 -08007509 if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
7510 in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) {
7511 in->dev->enable_voicerx = enable;
7512 struct audio_usecase *usecase;
7513 struct listnode *node;
7514 list_for_each(node, &in->dev->usecase_list) {
7515 usecase = node_to_item(node, struct audio_usecase, list);
7516 if (usecase->type == PCM_PLAYBACK)
7517 select_devices(in->dev, usecase->id);
7518 }
7519 }
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07007520 if (!in->standby) {
7521 if (enable_disable_effect(in->dev, EFFECT_AEC, enable) == ENOSYS)
7522 select_devices(in->dev, in->usecase);
7523 }
7524
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007525 }
Carter Hsu2e429db2019-05-14 18:50:52 +08007526 if (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0) {
7527
7528 in_update_effect_list(enable, effect, &in->ns_list);
7529 enable = !list_empty(&in->ns_list);
7530 if (enable == in->enable_ns)
7531 goto exit;
7532
Ravi Kumar Alamanda198185e2013-11-07 15:42:19 -08007533 in->enable_ns = enable;
Carter Hsu2e429db2019-05-14 18:50:52 +08007534 ALOGD("NS enable %d", enable);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07007535 if (!in->standby) {
kunleizd96526c2018-04-09 11:12:32 +08007536 if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
7537 in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) {
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07007538 if (enable_disable_effect(in->dev, EFFECT_NS, enable) == ENOSYS)
7539 select_devices(in->dev, in->usecase);
7540 } else
7541 select_devices(in->dev, in->usecase);
7542 }
Ravi Kumar Alamanda198185e2013-11-07 15:42:19 -08007543 }
Carter Hsu2e429db2019-05-14 18:50:52 +08007544exit:
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007545 pthread_mutex_unlock(&in->dev->lock);
7546 pthread_mutex_unlock(&in->lock);
7547
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007548 return 0;
7549}
7550
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007551static int in_add_audio_effect(const struct audio_stream *stream,
7552 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007553{
Eric Laurent994a6932013-07-17 11:51:42 -07007554 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007555 return add_remove_audio_effect(stream, effect, true);
7556}
7557
7558static int in_remove_audio_effect(const struct audio_stream *stream,
7559 effect_handle_t effect)
7560{
Eric Laurent994a6932013-07-17 11:51:42 -07007561 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007562 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007563}
7564
Haynes Mathew George16081042017-05-31 17:16:49 -07007565static int in_stop(const struct audio_stream_in* stream)
7566{
7567 struct stream_in *in = (struct stream_in *)stream;
7568 struct audio_device *adev = in->dev;
7569
7570 int ret = -ENOSYS;
7571 ALOGV("%s", __func__);
7572 pthread_mutex_lock(&adev->lock);
7573 if (in->usecase == USECASE_AUDIO_RECORD_MMAP && !in->standby &&
7574 in->capture_started && in->pcm != NULL) {
7575 pcm_stop(in->pcm);
7576 ret = stop_input_stream(in);
7577 in->capture_started = false;
7578 }
7579 pthread_mutex_unlock(&adev->lock);
7580 return ret;
7581}
7582
7583static int in_start(const struct audio_stream_in* stream)
7584{
7585 struct stream_in *in = (struct stream_in *)stream;
7586 struct audio_device *adev = in->dev;
7587 int ret = -ENOSYS;
7588
7589 ALOGV("%s in %p", __func__, in);
7590 pthread_mutex_lock(&adev->lock);
7591 if (in->usecase == USECASE_AUDIO_RECORD_MMAP && !in->standby &&
7592 !in->capture_started && in->pcm != NULL) {
7593 if (!in->capture_started) {
7594 ret = start_input_stream(in);
7595 if (ret == 0) {
7596 in->capture_started = true;
7597 }
7598 }
7599 }
7600 pthread_mutex_unlock(&adev->lock);
7601 return ret;
7602}
7603
Phil Burke0a86d12019-02-16 22:28:11 -08007604// Read offset for the positional timestamp from a persistent vendor property.
7605// This is to workaround apparent inaccuracies in the timing information that
7606// is used by the AAudio timing model. The inaccuracies can cause glitches.
7607static int64_t in_get_mmap_time_offset() {
7608 const int32_t kDefaultOffsetMicros = 0;
7609 int32_t mmap_time_offset_micros = property_get_int32(
Weiyin Jiangbd68b4d2019-05-17 16:29:50 +08007610 "persist.vendor.audio.in_mmap_delay_micros", kDefaultOffsetMicros);
Phil Burke0a86d12019-02-16 22:28:11 -08007611 ALOGI("mmap_time_offset_micros = %d for input", mmap_time_offset_micros);
7612 return mmap_time_offset_micros * (int64_t)1000;
7613}
7614
Haynes Mathew George16081042017-05-31 17:16:49 -07007615static int in_create_mmap_buffer(const struct audio_stream_in *stream,
7616 int32_t min_size_frames,
7617 struct audio_mmap_buffer_info *info)
7618{
7619 struct stream_in *in = (struct stream_in *)stream;
7620 struct audio_device *adev = in->dev;
7621 int ret = 0;
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07007622 unsigned int offset1 = 0;
7623 unsigned int frames1 = 0;
Haynes Mathew George16081042017-05-31 17:16:49 -07007624 const char *step = "";
Arun Mirpuri5d170872019-03-26 13:21:31 -07007625 uint32_t mmap_size = 0;
7626 uint32_t buffer_size = 0;
Haynes Mathew George16081042017-05-31 17:16:49 -07007627
7628 pthread_mutex_lock(&adev->lock);
7629 ALOGV("%s in %p", __func__, in);
7630
Sharad Sanglec6f32552018-05-04 16:15:38 +05307631 if (CARD_STATUS_OFFLINE == in->card_status||
7632 CARD_STATUS_OFFLINE == adev->card_status) {
7633 ALOGW("in->card_status or adev->card_status offline, try again");
7634 ret = -EIO;
7635 goto exit;
7636 }
7637
Sujin Panicker7c7b6f92020-04-03 12:57:55 +05307638 if (info == NULL || !(min_size_frames > 0 && min_size_frames < INT32_MAX)) {
Haynes Mathew George16081042017-05-31 17:16:49 -07007639 ALOGE("%s invalid argument info %p min_size_frames %d", __func__, info, min_size_frames);
7640 ret = -EINVAL;
7641 goto exit;
7642 }
7643 if (in->usecase != USECASE_AUDIO_RECORD_MMAP || !in->standby) {
7644 ALOGE("%s: usecase = %d, standby = %d", __func__, in->usecase, in->standby);
7645 ALOGV("%s in %p", __func__, in);
7646 ret = -ENOSYS;
7647 goto exit;
7648 }
7649 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
7650 if (in->pcm_device_id < 0) {
7651 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
7652 __func__, in->pcm_device_id, in->usecase);
7653 ret = -EINVAL;
7654 goto exit;
7655 }
7656
7657 adjust_mmap_period_count(&in->config, min_size_frames);
7658
7659 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
7660 __func__, adev->snd_card, in->pcm_device_id, in->config.channels);
7661 in->pcm = pcm_open(adev->snd_card, in->pcm_device_id,
7662 (PCM_IN | PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC), &in->config);
Satish Babu Patakokila54ce83d2018-07-06 18:00:37 +05307663 if (errno == ENETRESET && !pcm_is_ready(in->pcm)) {
Sharad Sanglec6f32552018-05-04 16:15:38 +05307664 ALOGE("%s: pcm_open failed errno:%d\n", __func__, errno);
7665 in->card_status = CARD_STATUS_OFFLINE;
7666 adev->card_status = CARD_STATUS_OFFLINE;
7667 ret = -EIO;
7668 goto exit;
7669 }
7670
Haynes Mathew George16081042017-05-31 17:16:49 -07007671 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) {
7672 step = "open";
7673 ret = -ENODEV;
7674 goto exit;
7675 }
7676
7677 ret = pcm_mmap_begin(in->pcm, &info->shared_memory_address, &offset1, &frames1);
7678 if (ret < 0) {
7679 step = "begin";
7680 goto exit;
7681 }
Haynes Mathew George16081042017-05-31 17:16:49 -07007682
juyuchen626833d2019-06-04 16:48:02 +08007683 info->flags = 0;
Arun Mirpuri5d170872019-03-26 13:21:31 -07007684 info->buffer_size_frames = pcm_get_buffer_size(in->pcm);
7685 buffer_size = pcm_frames_to_bytes(in->pcm, info->buffer_size_frames);
7686 info->burst_size_frames = in->config.period_size;
7687 ret = platform_get_mmap_data_fd(adev->platform,
7688 in->pcm_device_id, 1 /*capture*/,
7689 &info->shared_memory_fd,
7690 &mmap_size);
7691 if (ret < 0) {
7692 // Fall back to non exclusive mode
7693 info->shared_memory_fd = pcm_get_poll_fd(in->pcm);
7694 } else {
Phil Burkd898ba62019-06-20 12:49:01 -07007695 in->mmap_shared_memory_fd = info->shared_memory_fd; // for closing later
7696 ALOGV("%s: opened mmap_shared_memory_fd = %d", __func__, in->mmap_shared_memory_fd);
7697
Arun Mirpuri5d170872019-03-26 13:21:31 -07007698 if (mmap_size < buffer_size) {
7699 step = "mmap";
7700 goto exit;
7701 }
juyuchen626833d2019-06-04 16:48:02 +08007702 info->flags |= AUDIO_MMAP_APPLICATION_SHAREABLE;
Arun Mirpuri5d170872019-03-26 13:21:31 -07007703 }
7704
7705 memset(info->shared_memory_address, 0, buffer_size);
Haynes Mathew George16081042017-05-31 17:16:49 -07007706
7707 ret = pcm_mmap_commit(in->pcm, 0, MMAP_PERIOD_SIZE);
7708 if (ret < 0) {
7709 step = "commit";
7710 goto exit;
7711 }
7712
Phil Burke0a86d12019-02-16 22:28:11 -08007713 in->mmap_time_offset_nanos = in_get_mmap_time_offset();
7714
Haynes Mathew George16081042017-05-31 17:16:49 -07007715 in->standby = false;
7716 ret = 0;
7717
7718 ALOGV("%s: got mmap buffer address %p info->buffer_size_frames %d",
7719 __func__, info->shared_memory_address, info->buffer_size_frames);
7720
7721exit:
7722 if (ret != 0) {
7723 if (in->pcm == NULL) {
7724 ALOGE("%s: %s - %d", __func__, step, ret);
7725 } else {
7726 ALOGE("%s: %s %s", __func__, step, pcm_get_error(in->pcm));
7727 pcm_close(in->pcm);
7728 in->pcm = NULL;
7729 }
7730 }
7731 pthread_mutex_unlock(&adev->lock);
7732 return ret;
7733}
7734
7735static int in_get_mmap_position(const struct audio_stream_in *stream,
7736 struct audio_mmap_position *position)
7737{
7738 struct stream_in *in = (struct stream_in *)stream;
7739 ALOGVV("%s", __func__);
7740 if (position == NULL) {
7741 return -EINVAL;
7742 }
Gautam Manam34d1f542021-01-05 20:24:37 +05307743 lock_input_stream(in);
Haynes Mathew George16081042017-05-31 17:16:49 -07007744 if (in->usecase != USECASE_AUDIO_RECORD_MMAP) {
Gautam Manam34d1f542021-01-05 20:24:37 +05307745 pthread_mutex_unlock(&in->lock);
Haynes Mathew George16081042017-05-31 17:16:49 -07007746 return -ENOSYS;
7747 }
7748 if (in->pcm == NULL) {
Gautam Manam34d1f542021-01-05 20:24:37 +05307749 pthread_mutex_unlock(&in->lock);
Haynes Mathew George16081042017-05-31 17:16:49 -07007750 return -ENOSYS;
7751 }
7752 struct timespec ts = { 0, 0 };
7753 int ret = pcm_mmap_get_hw_ptr(in->pcm, (unsigned int *)&position->position_frames, &ts);
7754 if (ret < 0) {
7755 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
Gautam Manam34d1f542021-01-05 20:24:37 +05307756 pthread_mutex_unlock(&in->lock);
Haynes Mathew George16081042017-05-31 17:16:49 -07007757 return ret;
7758 }
Phil Burke0a86d12019-02-16 22:28:11 -08007759 position->time_nanoseconds = ts.tv_sec*1000000000LL + ts.tv_nsec
7760 + in->mmap_time_offset_nanos;
Gautam Manam34d1f542021-01-05 20:24:37 +05307761 pthread_mutex_unlock(&in->lock);
Haynes Mathew George16081042017-05-31 17:16:49 -07007762 return 0;
7763}
7764
Naresh Tannirudcb47c52018-06-25 16:23:32 +05307765static int in_get_active_microphones(const struct audio_stream_in *stream,
7766 struct audio_microphone_characteristic_t *mic_array,
7767 size_t *mic_count) {
7768 struct stream_in *in = (struct stream_in *)stream;
7769 struct audio_device *adev = in->dev;
7770 ALOGVV("%s", __func__);
7771
7772 lock_input_stream(in);
7773 pthread_mutex_lock(&adev->lock);
7774 int ret = platform_get_active_microphones(adev->platform,
7775 audio_channel_count_from_in_mask(in->channel_mask),
7776 in->usecase, mic_array, mic_count);
7777 pthread_mutex_unlock(&adev->lock);
7778 pthread_mutex_unlock(&in->lock);
7779
7780 return ret;
7781}
7782
7783static int adev_get_microphones(const struct audio_hw_device *dev,
7784 struct audio_microphone_characteristic_t *mic_array,
7785 size_t *mic_count) {
7786 struct audio_device *adev = (struct audio_device *)dev;
7787 ALOGVV("%s", __func__);
7788
7789 pthread_mutex_lock(&adev->lock);
7790 int ret = platform_get_microphones(adev->platform, mic_array, mic_count);
7791 pthread_mutex_unlock(&adev->lock);
7792
7793 return ret;
7794}
juyuchendb308c22019-01-21 11:57:17 -07007795
7796static void in_update_sink_metadata(struct audio_stream_in *stream,
7797 const struct sink_metadata *sink_metadata) {
7798
7799 if (stream == NULL
7800 || sink_metadata == NULL
7801 || sink_metadata->tracks == NULL) {
7802 return;
7803 }
7804
7805 int error = 0;
7806 struct stream_in *in = (struct stream_in *)stream;
7807 struct audio_device *adev = in->dev;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08007808 struct listnode devices;
7809
7810 list_init(&devices);
juyuchendb308c22019-01-21 11:57:17 -07007811
7812 if (sink_metadata->track_count != 0)
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08007813 reassign_device_list(&devices, sink_metadata->tracks->dest_device, "");
juyuchendb308c22019-01-21 11:57:17 -07007814
7815 lock_input_stream(in);
7816 pthread_mutex_lock(&adev->lock);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08007817 ALOGV("%s: in->usecase: %d, device: %x", __func__, in->usecase, get_device_types(&devices));
juyuchendb308c22019-01-21 11:57:17 -07007818
Zhou Song503196b2021-07-23 17:31:05 +08007819 if ((in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY ||
7820 in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY2) &&
7821 !list_empty(&devices) &&
7822 adev->voice_tx_output != NULL) {
juyuchendb308c22019-01-21 11:57:17 -07007823 /* Use the rx device from afe-proxy record to route voice call because
7824 there is no routing if tx device is on primary hal and rx device
7825 is on other hal during voice call. */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08007826 assign_devices(&adev->voice_tx_output->device_list, &devices);
juyuchendb308c22019-01-21 11:57:17 -07007827
7828 if (!voice_is_call_state_active(adev)) {
7829 if (adev->mode == AUDIO_MODE_IN_CALL) {
7830 adev->current_call_output = adev->voice_tx_output;
7831 error = voice_start_call(adev);
7832 if (error != 0)
7833 ALOGE("%s: start voice call failed %d", __func__, error);
7834 }
7835 } else {
7836 adev->current_call_output = adev->voice_tx_output;
7837 voice_update_devices_for_all_voice_usecases(adev);
7838 }
7839 }
7840
7841 pthread_mutex_unlock(&adev->lock);
7842 pthread_mutex_unlock(&in->lock);
7843}
7844
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05307845int adev_open_output_stream(struct audio_hw_device *dev,
Haynes Mathew George16081042017-05-31 17:16:49 -07007846 audio_io_handle_t handle,
7847 audio_devices_t devices,
7848 audio_output_flags_t flags,
7849 struct audio_config *config,
7850 struct audio_stream_out **stream_out,
Derek Chenf6318be2017-06-12 17:16:24 -04007851 const char *address)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007852{
7853 struct audio_device *adev = (struct audio_device *)dev;
7854 struct stream_out *out;
Gangadhar Sb0210342019-02-22 17:39:41 +05307855 int ret = 0, ip_hdlr_stream = 0, ip_hdlr_dev = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07007856 audio_format_t format;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08007857 struct adsp_hdlr_stream_cfg hdlr_stream_cfg;
Manish Dewangan21a850a2017-08-14 12:03:55 +05307858 bool is_direct_passthough = false;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007859 bool is_hdmi = devices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
7860 bool is_usb_dev = audio_is_usb_out_device(devices) &&
7861 (devices != AUDIO_DEVICE_OUT_USB_ACCESSORY);
7862 bool direct_dev = is_hdmi || is_usb_dev;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08007863 bool use_db_as_primary =
vivek mehtaae1018c2019-05-09 12:19:57 -07007864 property_get_bool("vendor.audio.feature.deepbuffer_as_primary.enable",
7865 false);
Vignesh Kulothungana6927272019-02-20 15:17:07 -08007866 bool force_haptic_path =
7867 property_get_bool("vendor.audio.test_haptic", false);
Aniket Kumar Lata23d8cbf2019-04-10 19:54:46 -07007868 bool is_voip_rx = flags & AUDIO_OUTPUT_FLAG_VOIP_RX;
Xiaojun Sang782e5b12020-06-29 21:13:06 +08007869#ifdef AUDIO_GKI_ENABLED
7870 __s32 *generic_dec;
7871#endif
Weiyin Jiang906db3c2021-03-02 13:17:04 +08007872 pthread_mutexattr_t latch_attr;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007873
kunleizdff872d2018-08-20 14:40:33 +08007874 if (is_usb_dev && (!audio_extn_usb_connected(NULL))) {
kunleizd6a9e0c2018-07-30 15:38:52 +08007875 is_usb_dev = false;
7876 devices = AUDIO_DEVICE_OUT_SPEAKER;
7877 ALOGW("%s: ignore set device to non existing USB card, use output device(%#x)",
7878 __func__, devices);
Mingshu Pangdbd20562019-11-25 18:04:39 +08007879 if (config->format == AUDIO_FORMAT_DEFAULT)
7880 config->format = AUDIO_FORMAT_PCM_16_BIT;
7881 if (config->sample_rate == 0)
7882 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
7883 if (config->channel_mask == AUDIO_CHANNEL_NONE)
7884 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
kunleizd6a9e0c2018-07-30 15:38:52 +08007885 }
7886
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007887 *stream_out = NULL;
Naresh Tanniru80659832014-06-04 18:17:56 +05307888
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007889 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
7890
Mingming Yin3a941d42016-02-17 18:08:05 -08007891 ALOGD("%s: enter: format(%#x) sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)\
Derek Chenf6318be2017-06-12 17:16:24 -04007892 stream_handle(%p) address(%s)", __func__, config->format, config->sample_rate, config->channel_mask,
7893 devices, flags, &out->stream, address);
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05307894
7895
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08007896 if (!out) {
7897 return -ENOMEM;
7898 }
7899
Haynes Mathew George204045b2015-02-25 20:32:03 -08007900 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07007901 pthread_mutex_init(&out->pre_lock, (const pthread_mutexattr_t *) NULL);
Weiyin Jiang906db3c2021-03-02 13:17:04 +08007902 pthread_mutexattr_init(&latch_attr);
7903 pthread_mutexattr_settype(&latch_attr, PTHREAD_MUTEX_RECURSIVE);
7904 pthread_mutex_init(&out->latch_lock, &latch_attr);
7905 pthread_mutexattr_destroy(&latch_attr);
Zhou Song48453a02018-01-10 17:50:59 +08007906 pthread_mutex_init(&out->position_query_lock, (const pthread_mutexattr_t *) NULL);
Haynes Mathew George204045b2015-02-25 20:32:03 -08007907 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
7908
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007909 if (devices == AUDIO_DEVICE_NONE)
7910 devices = AUDIO_DEVICE_OUT_SPEAKER;
7911
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007912 out->flags = flags;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08007913 list_init(&out->device_list);
7914 update_device_list(&out->device_list, devices, address, true /* add devices */);
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07007915 out->dev = adev;
Aalique Grahame65780b52017-09-27 14:59:56 -07007916 out->hal_op_format = out->hal_ip_format = format = out->format = config->format;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07007917 out->sample_rate = config->sample_rate;
Sachin Mohan Gadag3d09acd2017-06-19 12:43:44 +05307918 out->channel_mask = config->channel_mask;
Ramjee Singh5857aeb2017-08-03 19:18:50 +05307919 if (out->channel_mask == AUDIO_CHANNEL_NONE)
7920 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
7921 else
7922 out->supported_channel_masks[0] = out->channel_mask;
Eric Laurentc4aef752013-09-12 17:45:53 -07007923 out->handle = handle;
Mingming Yin3ee55c62014-08-04 14:23:35 -07007924 out->bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Alexy Josephaa54c872014-12-03 02:46:47 -08007925 out->non_blocking = 0;
Ashish Jain83a6cc22016-06-28 14:34:17 +05307926 out->convert_buffer = NULL;
Ashish Jain1b9b30c2017-05-18 20:57:40 +05307927 out->started = 0;
Zhou Songbaddf9f2020-11-20 13:57:39 +08007928 out->a2dp_muted = false;
Aniket Kumar Lata932f4872017-11-06 18:29:44 -08007929 out->hal_output_suspend_supported = 0;
7930 out->dynamic_pm_qos_config_supported = 0;
Surendar Karkaf51b5842018-04-26 11:28:38 +05307931 out->set_dual_mono = false;
Manisha Agarwal7b3e3772019-02-20 14:33:45 +05307932 out->prev_card_status_offline = false;
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +05307933 out->pspd_coeff_sent = false;
Phil Burkd898ba62019-06-20 12:49:01 -07007934 out->mmap_shared_memory_fd = -1; // not open
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007935
Nikhil Laturkar26b690b2017-07-25 11:06:14 +05307936 if ((flags & AUDIO_OUTPUT_FLAG_BD) &&
Satish Babu Patakokila37e7c482018-02-02 11:50:06 +05307937 (property_get_bool("vendor.audio.matrix.limiter.enable", false)))
Ben Romberger6c4d3812017-06-13 17:46:45 -07007938 platform_set_device_params(out, DEVICE_PARAM_LIMITER_ID, 1);
7939
Aalique Grahame22e49102018-12-18 14:23:57 -08007940 if (direct_dev &&
7941 (audio_is_linear_pcm(out->format) ||
7942 config->format == AUDIO_FORMAT_DEFAULT) &&
7943 out->flags == AUDIO_OUTPUT_FLAG_NONE) {
7944 audio_format_t req_format = config->format;
7945 audio_channel_mask_t req_channel_mask = config->channel_mask;
7946 uint32_t req_sample_rate = config->sample_rate;
7947
7948 pthread_mutex_lock(&adev->lock);
7949 if (is_hdmi) {
7950 ALOGV("AUDIO_DEVICE_OUT_AUX_DIGITAL and DIRECT|OFFLOAD, check hdmi caps");
7951 ret = read_hdmi_sink_caps(out);
7952 if (config->sample_rate == 0)
7953 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
7954 if (config->channel_mask == AUDIO_CHANNEL_NONE)
7955 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
7956 if (config->format == AUDIO_FORMAT_DEFAULT)
7957 config->format = AUDIO_FORMAT_PCM_16_BIT;
7958 } else if (is_usb_dev) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007959 ret = read_usb_sup_params_and_compare(true /*is_playback*/,
7960 &config->format,
7961 &out->supported_formats[0],
7962 MAX_SUPPORTED_FORMATS,
7963 &config->channel_mask,
7964 &out->supported_channel_masks[0],
7965 MAX_SUPPORTED_CHANNEL_MASKS,
7966 &config->sample_rate,
7967 &out->supported_sample_rates[0],
7968 MAX_SUPPORTED_SAMPLE_RATES);
7969 ALOGV("plugged dev USB ret %d", ret);
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007970 }
Aalique Grahame22e49102018-12-18 14:23:57 -08007971
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007972 pthread_mutex_unlock(&adev->lock);
7973 if (ret != 0) {
Mingming Yin3a941d42016-02-17 18:08:05 -08007974 if (ret == -ENOSYS) {
7975 /* ignore and go with default */
7976 ret = 0;
Aalique Grahame22e49102018-12-18 14:23:57 -08007977 }
7978 // For MMAP NO IRQ, allow conversions in ADSP
7979 else if (is_hdmi || (flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) == 0)
7980 goto error_open;
7981 else {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007982 ALOGE("error reading direct dev sink caps");
Mingming Yin3a941d42016-02-17 18:08:05 -08007983 goto error_open;
7984 }
Aalique Grahame22e49102018-12-18 14:23:57 -08007985
7986 if (req_sample_rate != 0 && config->sample_rate != req_sample_rate)
7987 config->sample_rate = req_sample_rate;
7988 if (req_channel_mask != AUDIO_CHANNEL_NONE && config->channel_mask != req_channel_mask)
7989 config->channel_mask = req_channel_mask;
7990 if (req_format != AUDIO_FORMAT_DEFAULT && config->format != req_format)
7991 config->format = req_format;
Mingming Yin3a941d42016-02-17 18:08:05 -08007992 }
Aalique Grahame22e49102018-12-18 14:23:57 -08007993
7994 out->sample_rate = config->sample_rate;
7995 out->channel_mask = config->channel_mask;
7996 out->format = config->format;
7997 if (is_hdmi) {
7998 out->usecase = USECASE_AUDIO_PLAYBACK_HIFI;
7999 out->config = pcm_config_hdmi_multi;
8000 } else if (flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) {
8001 out->usecase = USECASE_AUDIO_PLAYBACK_MMAP;
8002 out->config = pcm_config_mmap_playback;
8003 out->stream.start = out_start;
8004 out->stream.stop = out_stop;
8005 out->stream.create_mmap_buffer = out_create_mmap_buffer;
8006 out->stream.get_mmap_position = out_get_mmap_position;
8007 } else {
8008 out->usecase = USECASE_AUDIO_PLAYBACK_HIFI;
8009 out->config = pcm_config_hifi;
8010 }
8011
8012 out->config.rate = out->sample_rate;
8013 out->config.channels = audio_channel_count_from_out_mask(out->channel_mask);
8014 if (is_hdmi) {
8015 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels *
8016 audio_bytes_per_sample(out->format));
8017 }
8018 out->config.format = pcm_format_from_audio_format(out->format);
Mingming Yin3a941d42016-02-17 18:08:05 -08008019 }
8020
Derek Chenf6318be2017-06-12 17:16:24 -04008021 /* validate bus device address */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08008022 if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_BUS)) {
Derek Chenf6318be2017-06-12 17:16:24 -04008023 /* extract car audio stream index */
8024 out->car_audio_stream =
8025 audio_extn_auto_hal_get_car_audio_stream_from_address(address);
8026 if (out->car_audio_stream < 0) {
8027 ALOGE("%s: invalid car audio stream %x",
8028 __func__, out->car_audio_stream);
8029 ret = -EINVAL;
8030 goto error_open;
8031 }
Derek Chen5f67a942020-02-24 23:08:13 -08008032 ALOGV("%s: car_audio_stream %x", __func__, out->car_audio_stream);
Derek Chenf6318be2017-06-12 17:16:24 -04008033 }
8034
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008035 /* Check for VOIP usecase */
Aniket Kumar Lata23d8cbf2019-04-10 19:54:46 -07008036 if (is_voip_rx) {
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008037 if (!voice_extn_is_compress_voip_supported()) {
8038 if (out->sample_rate == 8000 || out->sample_rate == 16000 ||
8039 out->sample_rate == 32000 || out->sample_rate == 48000) {
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07008040 out->channel_mask = audio_extn_utils_is_vendor_enhanced_fwk() ?
Lakshman Chaluvarajue7fc9482020-05-30 14:29:29 +05308041 config->channel_mask : AUDIO_CHANNEL_OUT_STEREO;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008042 out->usecase = USECASE_AUDIO_PLAYBACK_VOIP;
8043 out->format = AUDIO_FORMAT_PCM_16_BIT;
Aniket Kumar Lata8426d1f2019-06-10 15:25:53 -07008044 out->volume_l = INVALID_OUT_VOLUME;
8045 out->volume_r = INVALID_OUT_VOLUME;
Vikram Panduranga93f080e2017-06-07 18:16:14 -07008046
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008047 out->config = default_pcm_config_voip_copp;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008048 out->config.rate = out->sample_rate;
Aniket Kumar Lata1e344f22019-06-19 17:09:01 -07008049 uint32_t channel_count =
8050 audio_channel_count_from_out_mask(out->channel_mask);
Ramjee Singh968858a2020-08-06 16:30:48 +05308051 out->config.channels = channel_count;
8052
Aniket Kumar Lata1e344f22019-06-19 17:09:01 -07008053 uint32_t buffer_size = get_stream_buffer_size(DEFAULT_VOIP_BUF_DURATION_MS,
8054 out->sample_rate, out->format,
8055 channel_count, false);
8056 uint32_t frame_size = audio_bytes_per_sample(out->format) * channel_count;
8057 if (frame_size != 0)
8058 out->config.period_size = buffer_size / frame_size;
8059 else
8060 ALOGW("%s: frame size is 0 for format %#x", __func__, out->format);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008061 }
8062 } else {
8063 if ((out->dev->mode == AUDIO_MODE_IN_COMMUNICATION ||
8064 voice_extn_compress_voip_is_active(out->dev)) &&
8065 (voice_extn_compress_voip_is_config_supported(config))) {
8066 ret = voice_extn_compress_voip_open_output_stream(out);
8067 if (ret != 0) {
8068 ALOGE("%s: Compress voip output cannot be opened, error:%d",
8069 __func__, ret);
8070 goto error_open;
8071 }
Sujin Panicker19027262019-09-16 18:28:06 +05308072 } else {
8073 out->usecase = GET_USECASE_AUDIO_PLAYBACK_PRIMARY(use_db_as_primary);
8074 out->config = GET_PCM_CONFIG_AUDIO_PLAYBACK_PRIMARY(use_db_as_primary);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008075 }
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08008076 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008077 } else if (audio_is_linear_pcm(out->format) &&
8078 out->flags == AUDIO_OUTPUT_FLAG_NONE && is_usb_dev) {
8079 out->channel_mask = config->channel_mask;
8080 out->sample_rate = config->sample_rate;
8081 out->format = config->format;
8082 out->usecase = USECASE_AUDIO_PLAYBACK_HIFI;
8083 // does this change?
8084 out->config = is_hdmi ? pcm_config_hdmi_multi : pcm_config_hifi;
8085 out->config.rate = config->sample_rate;
8086 out->config.channels = audio_channel_count_from_out_mask(out->channel_mask);
8087 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels *
8088 audio_bytes_per_sample(config->format));
8089 out->config.format = pcm_format_from_audio_format(out->format);
vivek mehta0ea887a2015-08-26 14:01:20 -07008090 } else if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
Dhananjay Kumarac341582017-02-23 23:42:25 +05308091 (out->flags == AUDIO_OUTPUT_FLAG_DIRECT)) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05308092 pthread_mutex_lock(&adev->lock);
8093 bool offline = (adev->card_status == CARD_STATUS_OFFLINE);
8094 pthread_mutex_unlock(&adev->lock);
8095
8096 // reject offload during card offline to allow
8097 // fallback to s/w paths
8098 if (offline) {
8099 ret = -ENODEV;
8100 goto error_open;
8101 }
vivek mehta0ea887a2015-08-26 14:01:20 -07008102
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008103 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
8104 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
8105 ALOGE("%s: Unsupported Offload information", __func__);
8106 ret = -EINVAL;
8107 goto error_open;
8108 }
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07008109
Atul Khare3fa6e542017-08-09 00:56:17 +05308110 if (config->offload_info.format == 0)
8111 config->offload_info.format = config->format;
8112 if (config->offload_info.sample_rate == 0)
8113 config->offload_info.sample_rate = config->sample_rate;
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07008114
Mingming Yin90310102013-11-13 16:57:00 -08008115 if (!is_supported_format(config->offload_info.format) &&
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05308116 !audio_extn_passthru_is_supported_format(config->offload_info.format)) {
vivek mehta0ea887a2015-08-26 14:01:20 -07008117 ALOGE("%s: Unsupported audio format %x " , __func__, config->offload_info.format);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008118 ret = -EINVAL;
8119 goto error_open;
8120 }
8121
Ben Romberger0f8c87b2017-05-24 17:41:11 -07008122 /* TrueHD only supported for 48k multiples (48k, 96k, 192k) */
8123 if ((config->offload_info.format == AUDIO_FORMAT_DOLBY_TRUEHD) &&
8124 (audio_extn_passthru_is_passthrough_stream(out)) &&
8125 !((config->sample_rate == 48000) ||
8126 (config->sample_rate == 96000) ||
8127 (config->sample_rate == 192000))) {
8128 ALOGE("%s: Unsupported sample rate %d for audio format %x",
8129 __func__, config->sample_rate, config->offload_info.format);
8130 ret = -EINVAL;
8131 goto error_open;
8132 }
8133
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008134 out->compr_config.codec = (struct snd_codec *)
8135 calloc(1, sizeof(struct snd_codec));
8136
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07008137 if (!out->compr_config.codec) {
8138 ret = -ENOMEM;
8139 goto error_open;
8140 }
8141
Dhananjay Kumarac341582017-02-23 23:42:25 +05308142 out->stream.pause = out_pause;
8143 out->stream.resume = out_resume;
8144 out->stream.flush = out_flush;
Ashish Jain4847e9d2017-08-17 19:16:57 +05308145 out->stream.set_callback = out_set_callback;
Dhananjay Kumarac341582017-02-23 23:42:25 +05308146 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Mingming Yin21d60472015-09-30 13:56:25 -07008147 out->stream.drain = out_drain;
Dhananjay Kumarac341582017-02-23 23:42:25 +05308148 out->usecase = get_offload_usecase(adev, true /* is_compress */);
vivek mehta446c3962015-09-14 10:57:35 -07008149 ALOGV("Compress Offload usecase .. usecase selected %d", out->usecase);
Dhananjay Kumarac341582017-02-23 23:42:25 +05308150 } else {
8151 out->usecase = get_offload_usecase(adev, false /* is_compress */);
8152 ALOGV("non-offload DIRECT_usecase ... usecase selected %d ", out->usecase);
vivek mehta0ea887a2015-08-26 14:01:20 -07008153 }
vivek mehta446c3962015-09-14 10:57:35 -07008154
Deeraj Somanfa377bf2019-02-06 12:57:59 +05308155 if (out->flags & AUDIO_OUTPUT_FLAG_FAST) {
8156 ALOGD("%s: Setting latency mode to true", __func__);
Meng Wang4c32fb42020-01-16 17:57:11 +08008157#ifdef AUDIO_GKI_ENABLED
8158 /* out->compr_config.codec->reserved[1] is for flags */
8159 out->compr_config.codec->reserved[1] |= audio_extn_utils_get_perf_mode_flag();
8160#else
Deeraj Somanfa377bf2019-02-06 12:57:59 +05308161 out->compr_config.codec->flags |= audio_extn_utils_get_perf_mode_flag();
Meng Wang4c32fb42020-01-16 17:57:11 +08008162#endif
Deeraj Somanfa377bf2019-02-06 12:57:59 +05308163 }
8164
vivek mehta446c3962015-09-14 10:57:35 -07008165 if (out->usecase == USECASE_INVALID) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08008166 if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_AUX_DIGITAL) &&
Mingming Yin3a941d42016-02-17 18:08:05 -08008167 config->format == 0 && config->sample_rate == 0 &&
8168 config->channel_mask == 0) {
Mingming Yin21854652016-04-13 11:54:02 -07008169 ALOGI("%s dummy open to query sink capability",__func__);
Mingming Yin3a941d42016-02-17 18:08:05 -08008170 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
8171 } else {
8172 ALOGE("%s, Max allowed OFFLOAD usecase reached ... ", __func__);
8173 ret = -EEXIST;
8174 goto error_open;
8175 }
vivek mehta446c3962015-09-14 10:57:35 -07008176 }
8177
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008178 if (config->offload_info.channel_mask)
8179 out->channel_mask = config->offload_info.channel_mask;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08008180 else if (config->channel_mask) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008181 out->channel_mask = config->channel_mask;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08008182 config->offload_info.channel_mask = config->channel_mask;
Haynes Mathew Georgea99f7532016-08-24 16:01:21 -07008183 } else {
Dhananjay Kumarac341582017-02-23 23:42:25 +05308184 ALOGE("out->channel_mask not set for OFFLOAD/DIRECT usecase");
Haynes Mathew Georgea99f7532016-08-24 16:01:21 -07008185 ret = -EINVAL;
8186 goto error_open;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08008187 }
Haynes Mathew Georgea99f7532016-08-24 16:01:21 -07008188
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07008189 format = out->format = config->offload_info.format;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008190 out->sample_rate = config->offload_info.sample_rate;
8191
Mingming Yin3ee55c62014-08-04 14:23:35 -07008192 out->bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008193
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05308194 out->compr_config.codec->id = get_snd_codec_id(config->offload_info.format);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05308195 if (audio_extn_utils_is_dolby_format(config->offload_info.format)) {
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05308196 audio_extn_dolby_send_ddp_endp_params(adev);
8197 audio_extn_dolby_set_dmid(adev);
8198 }
vivek mehta0ea887a2015-08-26 14:01:20 -07008199
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008200 out->compr_config.codec->sample_rate =
Ravi Kumar Alamandab91bff32014-11-14 12:05:54 -08008201 config->offload_info.sample_rate;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008202 out->compr_config.codec->bit_rate =
8203 config->offload_info.bit_rate;
8204 out->compr_config.codec->ch_in =
Dhanalakshmi Siddania15c6792016-08-10 15:33:53 +05308205 audio_channel_count_from_out_mask(out->channel_mask);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008206 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
Satish Babu Patakokilaa395a9e2016-11-01 12:18:49 +05308207 /* Update bit width only for non passthrough usecases.
8208 * For passthrough usecases, the output will always be opened @16 bit
8209 */
8210 if (!audio_extn_passthru_is_passthrough_stream(out))
8211 out->bit_width = AUDIO_OUTPUT_BIT_WIDTH;
Naresh Tanniruee3499a2017-01-05 14:05:35 +05308212
8213 if (out->flags & AUDIO_OUTPUT_FLAG_TIMESTAMP)
Meng Wang4c32fb42020-01-16 17:57:11 +08008214#ifdef AUDIO_GKI_ENABLED
8215 /* out->compr_config.codec->reserved[1] is for flags */
8216 out->compr_config.codec->reserved[1] |= COMPRESSED_TIMESTAMP_FLAG;
8217 ALOGVV("%s : out->compr_config.codec->flags -> (%#x) ", __func__, out->compr_config.codec->reserved[1]);
8218#else
Naresh Tanniruee3499a2017-01-05 14:05:35 +05308219 out->compr_config.codec->flags |= COMPRESSED_TIMESTAMP_FLAG;
8220 ALOGVV("%s : out->compr_config.codec->flags -> (%#x) ", __func__, out->compr_config.codec->flags);
Meng Wang4c32fb42020-01-16 17:57:11 +08008221#endif
Naresh Tanniruee3499a2017-01-05 14:05:35 +05308222
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07008223 /*TODO: Do we need to change it for passthrough */
8224 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_RAW;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008225
Manish Dewangana6fc5442015-08-24 20:30:31 +05308226 if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC)
8227 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_RAW;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05308228 else if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS)
Manish Dewangana6fc5442015-08-24 20:30:31 +05308229 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_MP4ADTS;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05308230 else if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_LATM)
8231 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_MP4LATM;
Ashish Jainf1eaa582016-05-23 20:54:24 +05308232
8233 if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) ==
8234 AUDIO_FORMAT_PCM) {
8235
8236 /*Based on platform support, configure appropriate alsa format for corresponding
8237 *hal input format.
8238 */
8239 out->compr_config.codec->format = hal_format_to_alsa(
8240 config->offload_info.format);
8241
Ashish Jain83a6cc22016-06-28 14:34:17 +05308242 out->hal_op_format = alsa_format_to_hal(
Ashish Jainf1eaa582016-05-23 20:54:24 +05308243 out->compr_config.codec->format);
Ashish Jain83a6cc22016-06-28 14:34:17 +05308244 out->hal_ip_format = out->format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05308245
Dhananjay Kumarac341582017-02-23 23:42:25 +05308246 /*for direct non-compress playback populate bit_width based on selected alsa format as
Ashish Jainf1eaa582016-05-23 20:54:24 +05308247 *hal input format and alsa format might differ based on platform support.
8248 */
8249 out->bit_width = audio_bytes_per_sample(
Ashish Jain83a6cc22016-06-28 14:34:17 +05308250 out->hal_op_format) << 3;
Ashish Jainf1eaa582016-05-23 20:54:24 +05308251
8252 out->compr_config.fragments = DIRECT_PCM_NUM_FRAGMENTS;
8253
Deeraj Soman93155a62019-09-30 19:00:37 +05308254 if (property_get_bool("vendor.audio.offload.buffer.duration.enabled", false)) {
8255 if ((config->offload_info.duration_us >= MIN_OFFLOAD_BUFFER_DURATION_MS * 1000) &&
8256 (config->offload_info.duration_us <= MAX_OFFLOAD_BUFFER_DURATION_MS * 1000))
8257 out->info.duration_us = (int64_t)config->offload_info.duration_us;
8258 }
Deeraj Soman65358ab2019-02-07 15:40:49 +05308259
Ashish Jainf1eaa582016-05-23 20:54:24 +05308260 /* Check if alsa session is configured with the same format as HAL input format,
8261 * if not then derive correct fragment size needed to accomodate the
8262 * conversion of HAL input format to alsa format.
8263 */
8264 audio_extn_utils_update_direct_pcm_fragment_size(out);
8265
8266 /*if hal input and output fragment size is different this indicates HAL input format is
8267 *not same as the alsa format
8268 */
Ashish Jain83a6cc22016-06-28 14:34:17 +05308269 if (out->hal_fragment_size != out->compr_config.fragment_size) {
Ashish Jainf1eaa582016-05-23 20:54:24 +05308270 /*Allocate a buffer to convert input data to the alsa configured format.
8271 *size of convert buffer is equal to the size required to hold one fragment size
8272 *worth of pcm data, this is because flinger does not write more than fragment_size
8273 */
Ashish Jain83a6cc22016-06-28 14:34:17 +05308274 out->convert_buffer = calloc(1,out->compr_config.fragment_size);
8275 if (out->convert_buffer == NULL){
Ashish Jainf1eaa582016-05-23 20:54:24 +05308276 ALOGE("Allocation failed for convert buffer for size %d", out->compr_config.fragment_size);
8277 ret = -ENOMEM;
8278 goto error_open;
8279 }
8280 }
8281 } else if (audio_extn_passthru_is_passthrough_stream(out)) {
8282 out->compr_config.fragment_size =
8283 audio_extn_passthru_get_buffer_size(&config->offload_info);
8284 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
8285 } else {
8286 out->compr_config.fragment_size =
8287 platform_get_compress_offload_buffer_size(&config->offload_info);
8288 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
8289 }
Mingming Yin3ee55c62014-08-04 14:23:35 -07008290
Naresh Tanniruee3499a2017-01-05 14:05:35 +05308291 if (out->flags & AUDIO_OUTPUT_FLAG_TIMESTAMP) {
8292 out->compr_config.fragment_size += sizeof(struct snd_codec_metadata);
8293 }
Xiaojun Sang782e5b12020-06-29 21:13:06 +08008294 if (config->offload_info.format == AUDIO_FORMAT_FLAC) {
8295#ifdef AUDIO_GKI_ENABLED
8296 generic_dec =
8297 &(out->compr_config.codec->options.generic.reserved[1]);
8298 ((struct snd_generic_dec_flac *)generic_dec)->sample_size =
8299 AUDIO_OUTPUT_BIT_WIDTH;
8300#else
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +05308301 out->compr_config.codec->options.flac_dec.sample_size = AUDIO_OUTPUT_BIT_WIDTH;
Xiaojun Sang782e5b12020-06-29 21:13:06 +08008302#endif
8303 }
Mingming Yin3ee55c62014-08-04 14:23:35 -07008304
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05308305 if (config->offload_info.format == AUDIO_FORMAT_APTX) {
8306 audio_extn_send_aptx_dec_bt_addr_to_dsp(out);
8307 }
8308
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008309 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
8310 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07008311
Manish Dewangan69426c82017-01-30 17:35:36 +05308312 if ((flags & AUDIO_OUTPUT_FLAG_TIMESTAMP) &&
8313 (flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC)) {
8314 out->render_mode = RENDER_MODE_AUDIO_STC_MASTER;
8315 } else if(flags & AUDIO_OUTPUT_FLAG_TIMESTAMP) {
8316 out->render_mode = RENDER_MODE_AUDIO_MASTER;
8317 } else {
8318 out->render_mode = RENDER_MODE_AUDIO_NO_TIMESTAMP;
8319 }
Alexy Josephaa54c872014-12-03 02:46:47 -08008320
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05308321 memset(&out->channel_map_param, 0,
8322 sizeof(struct audio_out_channel_map_param));
8323
Haynes Mathew George352f27b2013-07-26 00:00:15 -07008324 out->send_new_metadata = 1;
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05308325 out->send_next_track_params = false;
8326 out->is_compr_metadata_avail = false;
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08008327 out->offload_state = OFFLOAD_STATE_IDLE;
8328 out->playback_started = 0;
Zhou Song48453a02018-01-10 17:50:59 +08008329 out->writeAt.tv_sec = 0;
8330 out->writeAt.tv_nsec = 0;
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08008331
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08008332 audio_extn_dts_create_state_notifier_node(out->usecase);
8333
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008334 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
8335 __func__, config->offload_info.version,
8336 config->offload_info.bit_rate);
Ashish Jain5106d362016-05-11 19:23:33 +05308337
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05308338 /* Check if DSD audio format is supported in codec
8339 * and there is no active native DSD use case
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05308340 */
8341
8342 if ((config->format == AUDIO_FORMAT_DSD) &&
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05308343 (!platform_check_codec_dsd_support(adev->platform) ||
8344 audio_is_dsd_native_stream_active(adev))) {
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05308345 ret = -EINVAL;
8346 goto error_open;
8347 }
8348
Ashish Jain5106d362016-05-11 19:23:33 +05308349 /* Disable gapless if any of the following is true
8350 * passthrough playback
8351 * AV playback
Dhananjay Kumarac341582017-02-23 23:42:25 +05308352 * non compressed Direct playback
Ashish Jain5106d362016-05-11 19:23:33 +05308353 */
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05308354 if (audio_extn_passthru_is_passthrough_stream(out) ||
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05308355 (config->format == AUDIO_FORMAT_DSD) ||
Naresh Tanniru928f0862017-04-07 16:44:23 -07008356 (config->format == AUDIO_FORMAT_IEC61937) ||
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05308357 config->offload_info.has_video ||
Dhananjay Kumarac341582017-02-23 23:42:25 +05308358 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Ashish Jain5106d362016-05-11 19:23:33 +05308359 check_and_set_gapless_mode(adev, false);
8360 } else
8361 check_and_set_gapless_mode(adev, true);
Mingming Yin21854652016-04-13 11:54:02 -07008362
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05308363 if (audio_extn_passthru_is_passthrough_stream(out)) {
Mingming Yin21854652016-04-13 11:54:02 -07008364 out->flags |= AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH;
8365 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05308366 if (config->format == AUDIO_FORMAT_DSD) {
8367 out->flags |= AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH;
Meng Wang4c32fb42020-01-16 17:57:11 +08008368#ifdef AUDIO_GKI_ENABLED
8369 /* out->compr_config.codec->reserved[0] is for compr_passthr */
8370 out->compr_config.codec->reserved[0] = PASSTHROUGH_DSD;
8371#else
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05308372 out->compr_config.codec->compr_passthr = PASSTHROUGH_DSD;
Meng Wang4c32fb42020-01-16 17:57:11 +08008373#endif
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05308374 }
Aalique Grahame0359a1f2016-09-08 16:54:22 -07008375
8376 create_offload_callback_thread(out);
8377
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07008378 } else if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
Arun Mirpuri7da752a2018-09-11 18:01:15 -07008379 switch (config->sample_rate) {
8380 case 0:
8381 out->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
8382 break;
8383 case 8000:
8384 case 16000:
8385 case 48000:
8386 out->sample_rate = config->sample_rate;
8387 break;
8388 default:
8389 ALOGE("%s: Unsupported sampling rate %d for Incall Music", __func__,
8390 config->sample_rate);
8391 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
8392 ret = -EINVAL;
8393 goto error_open;
8394 }
8395 //FIXME: add support for MONO stream configuration when audioflinger mixer supports it
8396 switch (config->channel_mask) {
8397 case AUDIO_CHANNEL_NONE:
8398 case AUDIO_CHANNEL_OUT_STEREO:
8399 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
8400 break;
8401 default:
8402 ALOGE("%s: Unsupported channel mask %#x for Incall Music", __func__,
8403 config->channel_mask);
8404 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
8405 ret = -EINVAL;
8406 goto error_open;
8407 }
8408 switch (config->format) {
8409 case AUDIO_FORMAT_DEFAULT:
8410 case AUDIO_FORMAT_PCM_16_BIT:
8411 out->format = AUDIO_FORMAT_PCM_16_BIT;
8412 break;
8413 default:
8414 ALOGE("%s: Unsupported format %#x for Incall Music", __func__,
8415 config->format);
8416 config->format = AUDIO_FORMAT_PCM_16_BIT;
8417 ret = -EINVAL;
8418 goto error_open;
8419 }
8420
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05308421 ret = voice_extn_check_and_set_incall_music_usecase(adev, out);
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07008422 if (ret != 0) {
8423 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
Arun Mirpuri7da752a2018-09-11 18:01:15 -07008424 __func__, ret);
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07008425 goto error_open;
8426 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08008427 } else if (is_single_device_type_equal(&out->device_list,
8428 AUDIO_DEVICE_OUT_TELEPHONY_TX)) {
Aalique Grahame22e49102018-12-18 14:23:57 -08008429 switch (config->sample_rate) {
8430 case 0:
8431 out->sample_rate = AFE_PROXY_SAMPLING_RATE;
8432 break;
8433 case 8000:
8434 case 16000:
8435 case 48000:
8436 out->sample_rate = config->sample_rate;
8437 break;
8438 default:
8439 ALOGE("%s: Unsupported sampling rate %d for Telephony TX", __func__,
8440 config->sample_rate);
8441 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
8442 ret = -EINVAL;
8443 break;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07008444 }
Aalique Grahame22e49102018-12-18 14:23:57 -08008445 //FIXME: add support for MONO stream configuration when audioflinger mixer supports it
8446 switch (config->channel_mask) {
8447 case AUDIO_CHANNEL_NONE:
8448 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
8449 break;
8450 case AUDIO_CHANNEL_OUT_STEREO:
8451 out->channel_mask = config->channel_mask;
8452 break;
8453 default:
8454 ALOGE("%s: Unsupported channel mask %#x for Telephony TX", __func__,
8455 config->channel_mask);
8456 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
8457 ret = -EINVAL;
8458 break;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07008459 }
Aalique Grahame22e49102018-12-18 14:23:57 -08008460 switch (config->format) {
8461 case AUDIO_FORMAT_DEFAULT:
8462 out->format = AUDIO_FORMAT_PCM_16_BIT;
8463 break;
8464 case AUDIO_FORMAT_PCM_16_BIT:
8465 out->format = config->format;
8466 break;
8467 default:
8468 ALOGE("%s: Unsupported format %#x for Telephony TX", __func__,
8469 config->format);
8470 config->format = AUDIO_FORMAT_PCM_16_BIT;
8471 ret = -EINVAL;
8472 break;
8473 }
8474 if (ret != 0)
8475 goto error_open;
8476
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07008477 out->usecase = USECASE_AUDIO_PLAYBACK_AFE_PROXY;
8478 out->config = pcm_config_afe_proxy_playback;
Aalique Grahame22e49102018-12-18 14:23:57 -08008479 out->config.rate = out->sample_rate;
8480 out->config.channels =
8481 audio_channel_count_from_out_mask(out->channel_mask);
8482 out->config.format = pcm_format_from_audio_format(out->format);
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07008483 adev->voice_tx_output = out;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07008484 } else {
Ashish Jain058165c2016-09-28 23:18:48 +05308485 unsigned int channels = 0;
8486 /*Update config params to default if not set by the caller*/
8487 if (config->sample_rate == 0)
8488 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
8489 if (config->channel_mask == AUDIO_CHANNEL_NONE)
8490 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
8491 if (config->format == AUDIO_FORMAT_DEFAULT)
8492 config->format = AUDIO_FORMAT_PCM_16_BIT;
8493
8494 channels = audio_channel_count_from_out_mask(out->channel_mask);
8495
Varun Balaraje49253e2017-07-06 19:48:56 +05308496 if (out->flags & AUDIO_OUTPUT_FLAG_INTERACTIVE) {
8497 out->usecase = get_interactive_usecase(adev);
8498 out->config = pcm_config_low_latency;
8499 } else if (out->flags & AUDIO_OUTPUT_FLAG_RAW) {
Ashish Jain83a6cc22016-06-28 14:34:17 +05308500 out->usecase = USECASE_AUDIO_PLAYBACK_ULL;
Haynes Mathew George5beddd42016-06-27 18:33:40 -07008501 out->realtime = may_use_noirq_mode(adev, USECASE_AUDIO_PLAYBACK_ULL,
8502 out->flags);
8503 out->config = out->realtime ? pcm_config_rt : pcm_config_low_latency;
Haynes Mathew George16081042017-05-31 17:16:49 -07008504 } else if (out->flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) {
8505 out->usecase = USECASE_AUDIO_PLAYBACK_MMAP;
8506 out->config = pcm_config_mmap_playback;
8507 out->stream.start = out_start;
8508 out->stream.stop = out_stop;
8509 out->stream.create_mmap_buffer = out_create_mmap_buffer;
8510 out->stream.get_mmap_position = out_get_mmap_position;
Ashish Jain83a6cc22016-06-28 14:34:17 +05308511 } else if (out->flags & AUDIO_OUTPUT_FLAG_FAST) {
8512 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
Aniket Kumar Lata932f4872017-11-06 18:29:44 -08008513 out->hal_output_suspend_supported =
8514 property_get_bool("vendor.audio.hal.output.suspend.supported", false);
8515 out->dynamic_pm_qos_config_supported =
8516 property_get_bool("vendor.audio.hal.dynamic.qos.config.supported", false);
8517 if (!out->dynamic_pm_qos_config_supported) {
Alexy Joseph98988832017-01-13 14:56:59 -08008518 ALOGI("%s: dynamic qos voting not enabled for platform", __func__);
8519 } else {
8520 ALOGI("%s: dynamic qos voting enabled for platform", __func__);
8521 //the mixer path will be a string similar to "low-latency-playback resume"
8522 strlcpy(out->pm_qos_mixer_path, use_case_table[out->usecase], MAX_MIXER_PATH_LEN);
8523 strlcat(out->pm_qos_mixer_path,
8524 " resume", MAX_MIXER_PATH_LEN);
8525 ALOGI("%s: created %s pm_qos_mixer_path" , __func__,
8526 out->pm_qos_mixer_path);
8527 }
Ashish Jain83a6cc22016-06-28 14:34:17 +05308528 out->config = pcm_config_low_latency;
8529 } else if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
8530 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
8531 out->config = pcm_config_deep_buffer;
Ashish Jain058165c2016-09-28 23:18:48 +05308532 out->config.period_size = get_output_period_size(config->sample_rate, out->format,
8533 channels, DEEP_BUFFER_OUTPUT_PERIOD_DURATION);
8534 if (out->config.period_size <= 0) {
8535 ALOGE("Invalid configuration period size is not valid");
8536 ret = -EINVAL;
8537 goto error_open;
8538 }
Aalique Grahame22e49102018-12-18 14:23:57 -08008539 } else if (flags & AUDIO_OUTPUT_FLAG_TTS) {
8540 out->usecase = USECASE_AUDIO_PLAYBACK_TTS;
8541 out->config = pcm_config_deep_buffer;
Vignesh Kulothungana6927272019-02-20 15:17:07 -08008542 } else if (config->channel_mask & AUDIO_CHANNEL_HAPTIC_ALL) {
8543 out->usecase = USECASE_AUDIO_PLAYBACK_WITH_HAPTICS;
8544 out->config = pcm_config_haptics_audio;
8545 if (force_haptic_path)
8546 adev->haptics_config = pcm_config_haptics_audio;
8547 else
8548 adev->haptics_config = pcm_config_haptics;
8549
Meng Wangd08ce322020-04-02 08:59:20 +08008550 channels =
Vignesh Kulothungana6927272019-02-20 15:17:07 -08008551 audio_channel_count_from_out_mask(out->channel_mask & ~AUDIO_CHANNEL_HAPTIC_ALL);
8552
8553 if (force_haptic_path) {
8554 out->config.channels = 1;
8555 adev->haptics_config.channels = 1;
8556 } else
8557 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 -08008558 } else if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_BUS)) {
Derek Chenf6318be2017-06-12 17:16:24 -04008559 ret = audio_extn_auto_hal_open_output_stream(out);
8560 if (ret) {
8561 ALOGE("%s: Failed to open output stream for bus device", __func__);
8562 ret = -EINVAL;
8563 goto error_open;
8564 }
Ashish Jain83a6cc22016-06-28 14:34:17 +05308565 } else {
8566 /* primary path is the default path selected if no other outputs are available/suitable */
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008567 out->usecase = GET_USECASE_AUDIO_PLAYBACK_PRIMARY(use_db_as_primary);
8568 out->config = GET_PCM_CONFIG_AUDIO_PLAYBACK_PRIMARY(use_db_as_primary);
Ashish Jain83a6cc22016-06-28 14:34:17 +05308569 }
8570 out->hal_ip_format = format = out->format;
8571 out->config.format = hal_format_to_pcm(out->hal_ip_format);
8572 out->hal_op_format = pcm_format_to_hal(out->config.format);
8573 out->bit_width = format_to_bitwidth_table[out->hal_op_format] << 3;
8574 out->config.rate = config->sample_rate;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07008575 out->sample_rate = out->config.rate;
Ashish Jain058165c2016-09-28 23:18:48 +05308576 out->config.channels = channels;
Ashish Jain83a6cc22016-06-28 14:34:17 +05308577 if (out->hal_ip_format != out->hal_op_format) {
8578 uint32_t buffer_size = out->config.period_size *
8579 format_to_bitwidth_table[out->hal_op_format] *
8580 out->config.channels;
8581 out->convert_buffer = calloc(1, buffer_size);
8582 if (out->convert_buffer == NULL){
8583 ALOGE("Allocation failed for convert buffer for size %d",
8584 out->compr_config.fragment_size);
8585 ret = -ENOMEM;
8586 goto error_open;
8587 }
8588 ALOGD("Convert buffer allocated of size %d", buffer_size);
8589 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008590 }
8591
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08008592 ALOGV("%s devices:%d, format:%x, out->sample_rate:%d,out->bit_width:%d out->format:%d out->flags:%x, flags: %x usecase %d",
8593 __func__, devices, format, out->sample_rate, out->bit_width, out->format, out->flags, flags, out->usecase);
Ashish Jain83a6cc22016-06-28 14:34:17 +05308594
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07008595 /* TODO remove this hardcoding and check why width is zero*/
8596 if (out->bit_width == 0)
8597 out->bit_width = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05308598 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07008599 &adev->streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08008600 &out->device_list, out->flags,
8601 out->hal_op_format, out->sample_rate,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +05308602 out->bit_width, out->channel_mask, out->profile,
Manish Dewangan837dc462015-05-27 10:17:41 +05308603 &out->app_type_cfg);
Aalique Grahame6e763712019-01-31 16:18:17 -08008604 if ((out->usecase == (audio_usecase_t)(GET_USECASE_AUDIO_PLAYBACK_PRIMARY(use_db_as_primary))) ||
Haynes Mathew Georgebf143712013-12-03 13:02:53 -08008605 (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
8606 /* Ensure the default output is not selected twice */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08008607 if(adev->primary_output == NULL)
8608 adev->primary_output = out;
8609 else {
8610 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07008611 ret = -EEXIST;
8612 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08008613 }
8614 }
8615
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008616 /* Check if this usecase is already existing */
8617 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella7ce05352014-04-17 20:00:41 -07008618 if ((get_usecase_from_list(adev, out->usecase) != NULL) &&
8619 (out->usecase != USECASE_COMPRESS_VOIP_CALL)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008620 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008621 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07008622 ret = -EEXIST;
8623 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008624 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08008625
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008626 pthread_mutex_unlock(&adev->lock);
8627
8628 out->stream.common.get_sample_rate = out_get_sample_rate;
8629 out->stream.common.set_sample_rate = out_set_sample_rate;
8630 out->stream.common.get_buffer_size = out_get_buffer_size;
8631 out->stream.common.get_channels = out_get_channels;
8632 out->stream.common.get_format = out_get_format;
8633 out->stream.common.set_format = out_set_format;
8634 out->stream.common.standby = out_standby;
8635 out->stream.common.dump = out_dump;
8636 out->stream.common.set_parameters = out_set_parameters;
8637 out->stream.common.get_parameters = out_get_parameters;
8638 out->stream.common.add_audio_effect = out_add_audio_effect;
8639 out->stream.common.remove_audio_effect = out_remove_audio_effect;
8640 out->stream.get_latency = out_get_latency;
8641 out->stream.set_volume = out_set_volume;
Aalique Grahame22e49102018-12-18 14:23:57 -08008642#ifdef NO_AUDIO_OUT
8643 out->stream.write = out_write_for_no_output;
8644#else
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008645 out->stream.write = out_write;
Aalique Grahame22e49102018-12-18 14:23:57 -08008646#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008647 out->stream.get_render_position = out_get_render_position;
8648 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07008649 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008650
Haynes Mathew George16081042017-05-31 17:16:49 -07008651 if (out->realtime)
8652 out->af_period_multiplier = af_period_multiplier;
8653 else
8654 out->af_period_multiplier = 1;
8655
Andy Hunga1f48fa2019-07-01 18:14:53 -07008656 out->kernel_buffer_size = out->config.period_size * out->config.period_count;
8657
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008658 out->standby = 1;
Zhou Songbaddf9f2020-11-20 13:57:39 +08008659 out->volume_l = PLAYBACK_GAIN_MAX;
8660 out->volume_r = PLAYBACK_GAIN_MAX;
Eric Laurenta9024de2013-04-04 09:19:12 -07008661 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07008662 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008663
8664 config->format = out->stream.common.get_format(&out->stream.common);
8665 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
8666 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
Naresh Tanniru04f71882018-06-26 17:46:22 +05308667 register_format(out->format, out->supported_formats);
8668 register_channel_mask(out->channel_mask, out->supported_channel_masks);
8669 register_sample_rate(out->sample_rate, out->supported_sample_rates);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008670
Dechen Chai22768452021-07-30 09:29:16 +05308671#ifndef LINUX_ENABLED
Aalique Grahame22e49102018-12-18 14:23:57 -08008672 out->error_log = error_log_create(
8673 ERROR_LOG_ENTRIES,
8674 1000000000 /* aggregate consecutive identical errors within one second in ns */);
Dechen Chai22768452021-07-30 09:29:16 +05308675#endif
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05308676 /*
8677 By locking output stream before registering, we allow the callback
8678 to update stream's state only after stream's initial state is set to
8679 adev state.
8680 */
8681 lock_output_stream(out);
8682 audio_extn_snd_mon_register_listener(out, out_snd_mon_cb);
8683 pthread_mutex_lock(&adev->lock);
8684 out->card_status = adev->card_status;
8685 pthread_mutex_unlock(&adev->lock);
8686 pthread_mutex_unlock(&out->lock);
8687
Aalique Grahame22e49102018-12-18 14:23:57 -08008688 stream_app_type_cfg_init(&out->app_type_cfg);
8689
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008690 *stream_out = &out->stream;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05308691 ALOGD("%s: Stream (%p) picks up usecase (%s)", __func__, &out->stream,
vivek mehta0ea887a2015-08-26 14:01:20 -07008692 use_case_table[out->usecase]);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08008693
8694 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
8695 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
8696 popcount(out->channel_mask), out->playback_started);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08008697 /* setup a channel for client <--> adsp communication for stream events */
Manish Dewangan21a850a2017-08-14 12:03:55 +05308698 is_direct_passthough = audio_extn_passthru_is_direct_passthrough(out);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08008699 if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
Naresh Tanniru85819452017-05-04 18:55:45 -07008700 (out->flags & AUDIO_OUTPUT_FLAG_DIRECT_PCM) ||
Gangadhar Sb0210342019-02-22 17:39:41 +05308701 audio_extn_ip_hdlr_intf_supported_for_copp(adev->platform) ||
8702 (audio_extn_ip_hdlr_intf_supported(config->format, is_direct_passthough, false))) {
Ben Rombergerd771a7c2017-02-22 18:05:17 -08008703 hdlr_stream_cfg.pcm_device_id = platform_get_pcm_device_id(
8704 out->usecase, PCM_PLAYBACK);
8705 hdlr_stream_cfg.flags = out->flags;
8706 hdlr_stream_cfg.type = PCM_PLAYBACK;
8707 ret = audio_extn_adsp_hdlr_stream_open(&out->adsp_hdlr_stream_handle,
8708 &hdlr_stream_cfg);
8709 if (ret) {
8710 ALOGE("%s: adsp_hdlr_stream_open failed %d",__func__, ret);
8711 out->adsp_hdlr_stream_handle = NULL;
8712 }
8713 }
Gangadhar Sb0210342019-02-22 17:39:41 +05308714 ip_hdlr_stream = audio_extn_ip_hdlr_intf_supported(config->format,
8715 is_direct_passthough, false);
8716 ip_hdlr_dev = audio_extn_ip_hdlr_intf_supported_for_copp(adev->platform);
8717 if (ip_hdlr_stream || ip_hdlr_dev ) {
Vidyakumar Athota2062f912017-06-27 14:46:15 -07008718 ret = audio_extn_ip_hdlr_intf_init(&out->ip_hdlr_handle, NULL, NULL, adev, out->usecase);
Naresh Tanniru85819452017-05-04 18:55:45 -07008719 if (ret < 0) {
8720 ALOGE("%s: audio_extn_ip_hdlr_intf_init failed %d",__func__, ret);
8721 out->ip_hdlr_handle = NULL;
8722 }
8723 }
Derek Chenf939fb72018-11-13 13:34:41 -08008724
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07008725 ret = io_streams_map_insert(adev, &out->stream.common,
8726 out->handle, AUDIO_PATCH_HANDLE_NONE);
8727 if (ret != 0)
8728 goto error_open;
8729
Susan Wang6dd13092021-01-25 10:27:11 -05008730 out->out_ctxt.output = out;
Derek Chenf939fb72018-11-13 13:34:41 -08008731
8732 pthread_mutex_lock(&adev->lock);
Susan Wang6dd13092021-01-25 10:27:11 -05008733 list_add_tail(&adev->active_outputs_list, &out->out_ctxt.list);
Derek Chenf939fb72018-11-13 13:34:41 -08008734 pthread_mutex_unlock(&adev->lock);
8735
Eric Laurent994a6932013-07-17 11:51:42 -07008736 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008737 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07008738
8739error_open:
Ashish Jain83a6cc22016-06-28 14:34:17 +05308740 if (out->convert_buffer)
8741 free(out->convert_buffer);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07008742 free(out);
8743 *stream_out = NULL;
8744 ALOGD("%s: exit: ret %d", __func__, ret);
8745 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008746}
8747
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05308748void adev_close_output_stream(struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008749 struct audio_stream_out *stream)
8750{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008751 struct stream_out *out = (struct stream_out *)stream;
8752 struct audio_device *adev = out->dev;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08008753 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008754
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008755 ALOGD("%s: enter:stream_handle(%s)",__func__, use_case_table[out->usecase]);
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05308756
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07008757 io_streams_map_remove(adev, out->handle);
8758
Susan Wang6dd13092021-01-25 10:27:11 -05008759 // remove out_ctxt early to prevent the stream
8760 // being opened in a race condition
8761 pthread_mutex_lock(&adev->lock);
8762 list_remove(&out->out_ctxt.list);
8763 pthread_mutex_unlock(&adev->lock);
8764
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05308765 // must deregister from sndmonitor first to prevent races
8766 // between the callback and close_stream
8767 audio_extn_snd_mon_unregister_listener(out);
8768
Ben Rombergerd771a7c2017-02-22 18:05:17 -08008769 /* close adsp hdrl session before standby */
8770 if (out->adsp_hdlr_stream_handle) {
8771 ret = audio_extn_adsp_hdlr_stream_close(out->adsp_hdlr_stream_handle);
8772 if (ret)
8773 ALOGE("%s: adsp_hdlr_stream_close failed %d",__func__, ret);
8774 out->adsp_hdlr_stream_handle = NULL;
8775 }
8776
Manish Dewangan21a850a2017-08-14 12:03:55 +05308777 if (out->ip_hdlr_handle) {
Naresh Tanniru85819452017-05-04 18:55:45 -07008778 audio_extn_ip_hdlr_intf_deinit(out->ip_hdlr_handle);
8779 out->ip_hdlr_handle = NULL;
8780 }
8781
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08008782 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05308783 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08008784 ret = voice_extn_compress_voip_close_output_stream(&stream->common);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05308785 out->started = 0;
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05308786 pthread_mutex_unlock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08008787 if(ret != 0)
8788 ALOGE("%s: Compress voip output cannot be closed, error:%d",
8789 __func__, ret);
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07008790 } else
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08008791 out_standby(&stream->common);
8792
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07008793 if (is_offload_usecase(out->usecase)) {
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08008794 audio_extn_dts_remove_state_notifier_node(out->usecase);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008795 destroy_offload_callback_thread(out);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07008796 free_offload_usecase(adev, out->usecase);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008797 if (out->compr_config.codec != NULL)
8798 free(out->compr_config.codec);
8799 }
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07008800
Zhou Songbaddf9f2020-11-20 13:57:39 +08008801 out->a2dp_muted = false;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05308802
Varun Balaraje49253e2017-07-06 19:48:56 +05308803 if (is_interactive_usecase(out->usecase))
8804 free_interactive_usecase(adev, out->usecase);
8805
Ashish Jain83a6cc22016-06-28 14:34:17 +05308806 if (out->convert_buffer != NULL) {
8807 free(out->convert_buffer);
8808 out->convert_buffer = NULL;
8809 }
8810
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07008811 if (adev->voice_tx_output == out)
8812 adev->voice_tx_output = NULL;
8813
Dechen Chai22768452021-07-30 09:29:16 +05308814#ifndef LINUX_ENABLED
Aalique Grahame22e49102018-12-18 14:23:57 -08008815 error_log_destroy(out->error_log);
8816 out->error_log = NULL;
Dechen Chai22768452021-07-30 09:29:16 +05308817#endif
Dhanalakshmi Siddani6c3d0992017-01-16 16:52:33 +05308818 if (adev->primary_output == out)
8819 adev->primary_output = NULL;
8820
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008821 pthread_cond_destroy(&out->cond);
8822 pthread_mutex_destroy(&out->lock);
Weiyin Jiang280ea742020-09-08 20:28:22 +08008823 pthread_mutex_destroy(&out->pre_lock);
8824 pthread_mutex_destroy(&out->latch_lock);
8825 pthread_mutex_destroy(&out->position_query_lock);
Derek Chenf939fb72018-11-13 13:34:41 -08008826
8827 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008828 free(stream);
Derek Chenf939fb72018-11-13 13:34:41 -08008829 pthread_mutex_unlock(&adev->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07008830 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008831}
8832
8833static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
8834{
8835 struct audio_device *adev = (struct audio_device *)dev;
8836 struct str_parms *parms;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008837 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07008838 int val;
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07008839 int ret;
8840 int status = 0;
Aalique Grahame22e49102018-12-18 14:23:57 -08008841 bool a2dp_reconfig = false;
Zhou Songd6d71752019-05-21 18:08:51 +08008842 struct listnode *node;
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07008843 int controller = -1, stream = -1;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008844
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08008845 ALOGD("%s: enter: %s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008846 parms = str_parms_create_str(kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008847
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05308848 if (!parms)
8849 goto error;
Naresh Tanniru4c630392014-05-12 01:05:52 +05308850
Derek Chen6f293672019-04-01 01:40:24 -07008851 /* notify adev and input/output streams on the snd card status */
8852 adev_snd_mon_cb((void *)adev, parms);
8853
Weiyin Jiang24f55292020-12-22 14:35:46 +08008854 ret = str_parms_get_str(parms, "SND_CARD_STATUS", value, sizeof(value));
8855 if (ret >= 0) {
8856 list_for_each(node, &adev->active_outputs_list) {
8857 streams_output_ctxt_t *out_ctxt = node_to_item(node,
8858 streams_output_ctxt_t,
8859 list);
8860 out_snd_mon_cb((void *)out_ctxt->output, parms);
8861 }
Derek Chen6f293672019-04-01 01:40:24 -07008862
Weiyin Jiang24f55292020-12-22 14:35:46 +08008863 list_for_each(node, &adev->active_inputs_list) {
8864 streams_input_ctxt_t *in_ctxt = node_to_item(node,
8865 streams_input_ctxt_t,
8866 list);
8867 in_snd_mon_cb((void *)in_ctxt->input, parms);
8868 }
Derek Chen6f293672019-04-01 01:40:24 -07008869 }
8870
Zhou Songd6d71752019-05-21 18:08:51 +08008871 pthread_mutex_lock(&adev->lock);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05308872 ret = str_parms_get_str(parms, "BT_SCO", value, sizeof(value));
8873 if (ret >= 0) {
8874 /* When set to false, HAL should disable EC and NS */
Zhou Songd6d71752019-05-21 18:08:51 +08008875 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0){
Ashish Jain1b9b30c2017-05-18 20:57:40 +05308876 adev->bt_sco_on = true;
Shalini Manjunatha3a2e06e2021-02-01 12:23:49 +05308877 /*
8878 * When ever BT_SCO=ON arrives, make sure to route
8879 * all use cases to SCO device, otherwise due to delay in
8880 * BT_SCO=ON and lack of synchronization with create audio patch
8881 * request for SCO device, some times use case not routed properly to
8882 * SCO device
8883 */
8884 struct audio_usecase *usecase;
8885 struct listnode *node;
8886 list_for_each(node, &adev->usecase_list) {
8887 usecase = node_to_item(node, struct audio_usecase, list);
Anver sadhiquef0efca32021-09-03 15:25:44 +05308888 if (usecase->stream.in && (usecase->type == PCM_CAPTURE) &&
Anver sadhique93f3c912021-08-11 11:19:45 +05308889 (!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 +05308890 ALOGD("BT_SCO ON, switch all in use case to it");
8891 select_devices(adev, usecase->id);
8892 }
Mingshu Pangef517202021-04-22 10:35:00 +08008893 if (usecase->stream.out && (usecase->type == PCM_PLAYBACK ||
8894 usecase->type == VOICE_CALL) &&
Anver sadhique93f3c912021-08-11 11:19:45 +05308895 (!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 +05308896 ALOGD("BT_SCO ON, switch all out use case to it");
8897 select_devices(adev, usecase->id);
8898 }
8899 }
8900 }
8901 else {
Ashish Jain1b9b30c2017-05-18 20:57:40 +05308902 adev->bt_sco_on = false;
Zhou Songd6d71752019-05-21 18:08:51 +08008903 audio_extn_sco_reset_configuration();
Kunlei Zhangd96af8f2019-08-27 16:33:29 +08008904 }
8905 }
8906
8907 ret = str_parms_get_str(parms, "A2dpSuspended", value, sizeof(value));
Weiyin Jiang280ea742020-09-08 20:28:22 +08008908 if (ret >= 0) {
Kunlei Zhangd96af8f2019-08-27 16:33:29 +08008909 if (!strncmp(value, "false", 5) &&
8910 audio_extn_a2dp_source_is_suspended()) {
8911 struct audio_usecase *usecase;
8912 struct listnode *node;
Zhou Songd6d71752019-05-21 18:08:51 +08008913 list_for_each(node, &adev->usecase_list) {
8914 usecase = node_to_item(node, struct audio_usecase, list);
Kunlei Zhangd96af8f2019-08-27 16:33:29 +08008915 if (usecase->stream.in && (usecase->type == PCM_CAPTURE) &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08008916 is_sco_in_device_type(&usecase->stream.in->device_list)) {
Kunlei Zhangd96af8f2019-08-27 16:33:29 +08008917 ALOGD("a2dp resumed, switch bt sco mic to handset mic");
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08008918 reassign_device_list(&usecase->stream.in->device_list,
8919 AUDIO_DEVICE_IN_BUILTIN_MIC, "");
Kunlei Zhangd96af8f2019-08-27 16:33:29 +08008920 select_devices(adev, usecase->id);
8921 }
Zhou Songd6d71752019-05-21 18:08:51 +08008922 }
8923 }
Ashish Jain1b9b30c2017-05-18 20:57:40 +05308924 }
8925
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07008926 status = voice_set_parameters(adev, parms);
8927 if (status != 0)
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08008928 goto done;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008929
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07008930 status = platform_set_parameters(adev->platform, parms);
8931 if (status != 0)
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08008932 goto done;
8933
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07008934 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
8935 if (ret >= 0) {
Vicky Sehrawate240e5d2014-08-12 17:17:04 -07008936 /* When set to false, HAL should disable EC and NS */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008937 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
8938 adev->bluetooth_nrec = true;
8939 else
8940 adev->bluetooth_nrec = false;
8941 }
8942
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07008943 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
8944 if (ret >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008945 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
8946 adev->screen_off = false;
8947 else
8948 adev->screen_off = true;
Quinn Male70f20f32019-06-26 16:50:26 -07008949 audio_extn_sound_trigger_update_screen_status(adev->screen_off);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008950 }
8951
Eric Laurent4b084132018-10-19 17:33:43 -07008952 ret = str_parms_get_int(parms, "rotation", &val);
8953 if (ret >= 0) {
8954 bool reverse_speakers = false;
8955 int camera_rotation = CAMERA_ROTATION_LANDSCAPE;
8956 switch (val) {
8957 // FIXME: note that the code below assumes that the speakers are in the correct placement
8958 // relative to the user when the device is rotated 90deg from its default rotation. This
8959 // assumption is device-specific, not platform-specific like this code.
8960 case 270:
8961 reverse_speakers = true;
8962 camera_rotation = CAMERA_ROTATION_INVERT_LANDSCAPE;
8963 break;
8964 case 0:
8965 case 180:
8966 camera_rotation = CAMERA_ROTATION_PORTRAIT;
8967 break;
8968 case 90:
8969 camera_rotation = CAMERA_ROTATION_LANDSCAPE;
8970 break;
8971 default:
8972 ALOGE("%s: unexpected rotation of %d", __func__, val);
8973 status = -EINVAL;
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07008974 }
Eric Laurent4b084132018-10-19 17:33:43 -07008975 if (status == 0) {
8976 // check and set swap
8977 // - check if orientation changed and speaker active
8978 // - set rotation and cache the rotation value
8979 adev->camera_orientation =
8980 (adev->camera_orientation & ~CAMERA_ROTATION_MASK) | camera_rotation;
8981 if (!audio_extn_is_maxx_audio_enabled())
8982 platform_check_and_set_swap_lr_channels(adev, reverse_speakers);
8983 }
8984 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07008985
Mingming Yin514a8bc2014-07-29 15:22:21 -07008986 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_SCO_WB, value, sizeof(value));
8987 if (ret >= 0) {
8988 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
8989 adev->bt_wb_speech_enabled = true;
8990 else
8991 adev->bt_wb_speech_enabled = false;
8992 }
8993
Zhou Song12c29502019-03-16 10:37:18 +08008994 ret = str_parms_get_str(parms, "bt_swb", value, sizeof(value));
8995 if (ret >= 0) {
8996 val = atoi(value);
8997 adev->swb_speech_mode = val;
8998 }
8999
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -07009000 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value, sizeof(value));
9001 if (ret >= 0) {
9002 val = atoi(value);
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05309003 audio_devices_t device = (audio_devices_t) val;
Zhou Song681350a2017-10-19 16:28:42 +08009004 if (audio_is_output_device(val) &&
9005 (val & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07009006 ALOGV("cache new ext disp type and edid");
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07009007 platform_get_controller_stream_from_params(parms, &controller, &stream);
9008 platform_set_ext_display_device_v2(adev->platform, controller, stream);
9009 ret = platform_get_ext_disp_type_v2(adev->platform, controller, stream);
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07009010 if (ret < 0) {
9011 ALOGE("%s: Failed to query disp type, ret:%d", __func__, ret);
Manisha Agarwal2f5ff882018-08-08 17:09:29 +05309012 } else {
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07009013 platform_cache_edid_v2(adev->platform, controller, stream);
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07009014 }
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05309015 } else if (audio_is_usb_out_device(device) || audio_is_usb_in_device(device)) {
vivek mehta344576a2016-04-12 18:56:03 -07009016 /*
9017 * Do not allow AFE proxy port usage by WFD source when USB headset is connected.
9018 * Per AudioPolicyManager, USB device is higher priority than WFD.
9019 * For Voice call over USB headset, voice call audio is routed to AFE proxy ports.
9020 * If WFD use case occupies AFE proxy, it may result unintended behavior while
9021 * starting voice call on USB
9022 */
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08009023 ret = str_parms_get_str(parms, "card", value, sizeof(value));
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05309024 if (ret >= 0)
9025 audio_extn_usb_add_device(device, atoi(value));
9026
Zhou Song6f862822017-11-06 17:27:57 +08009027 if (!audio_extn_usb_is_tunnel_supported()) {
9028 ALOGV("detected USB connect .. disable proxy");
9029 adev->allow_afe_proxy_usage = false;
9030 }
Zhou Song503196b2021-07-23 17:31:05 +08009031 } else if (audio_is_hearing_aid_out_device(device) &&
9032 property_get_bool("persist.vendor.audio.ha_proxy.enabled", false)) {
9033 adev->ha_proxy_enable = true;
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -07009034 }
9035 }
9036
9037 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value, sizeof(value));
9038 if (ret >= 0) {
9039 val = atoi(value);
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05309040 audio_devices_t device = (audio_devices_t) val;
Garmond Leunge3b6d482016-10-25 16:48:01 -07009041 /*
9042 * The HDMI / Displayport disconnect handling has been moved to
9043 * audio extension to ensure that its parameters are not
9044 * invalidated prior to updating sysfs of the disconnect event
9045 * Invalidate will be handled by audio_extn_ext_disp_set_parameters()
9046 */
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05309047 if (audio_is_usb_out_device(device) || audio_is_usb_in_device(device)) {
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08009048 ret = str_parms_get_str(parms, "card", value, sizeof(value));
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05309049 if (ret >= 0)
9050 audio_extn_usb_remove_device(device, atoi(value));
9051
Zhou Song6f862822017-11-06 17:27:57 +08009052 if (!audio_extn_usb_is_tunnel_supported()) {
9053 ALOGV("detected USB disconnect .. enable proxy");
9054 adev->allow_afe_proxy_usage = true;
9055 }
Zhou Song503196b2021-07-23 17:31:05 +08009056 } else if (audio_is_hearing_aid_out_device(device)) {
9057 adev->ha_proxy_enable = false;
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -07009058 }
9059 }
9060
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08009061 audio_extn_qdsp_set_parameters(adev, parms);
Aalique Grahame22e49102018-12-18 14:23:57 -08009062
9063 status = audio_extn_a2dp_set_parameters(parms, &a2dp_reconfig);
Aniket Kumar Lata23300322019-02-20 22:25:30 -08009064 if (status >= 0 && a2dp_reconfig) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05309065 struct audio_usecase *usecase;
9066 struct listnode *node;
9067 list_for_each(node, &adev->usecase_list) {
9068 usecase = node_to_item(node, struct audio_usecase, list);
Weiyin Jiangff56ff32020-05-08 14:32:21 +08009069 if ((usecase->stream.out == NULL) || (usecase->type != PCM_PLAYBACK))
9070 continue;
9071
9072 if (is_a2dp_out_device_type(&usecase->device_list)) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05309073 ALOGD("reconfigure a2dp... forcing device switch");
Naresh Tannirucd2353e2016-08-19 00:37:25 +05309074 audio_extn_a2dp_set_handoff_mode(true);
Manisha Agarwalf1c3b6b2019-06-25 13:00:33 +05309075 ALOGD("Switching to speaker and muting the stream before select_devices");
9076 check_a2dp_restore_l(adev, usecase->stream.out, false);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05309077 //force device switch to re configure encoder
9078 select_devices(adev, usecase->id);
Manisha Agarwalf1c3b6b2019-06-25 13:00:33 +05309079 ALOGD("Unmuting the stream after select_devices");
Zhou Songbaddf9f2020-11-20 13:57:39 +08009080 check_a2dp_restore_l(adev, usecase->stream.out, true);
Naresh Tannirucd2353e2016-08-19 00:37:25 +05309081 audio_extn_a2dp_set_handoff_mode(false);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05309082 break;
Zhou Songd01e7a22020-09-23 22:49:01 +08009083 } else if (is_offload_usecase(usecase->stream.out->usecase)) {
Weiyin Jiang280ea742020-09-08 20:28:22 +08009084 pthread_mutex_lock(&usecase->stream.out->latch_lock);
Zhou Songbaddf9f2020-11-20 13:57:39 +08009085 if (usecase->stream.out->a2dp_muted) {
Weiyin Jiang280ea742020-09-08 20:28:22 +08009086 pthread_mutex_unlock(&usecase->stream.out->latch_lock);
9087 reassign_device_list(&usecase->stream.out->device_list,
9088 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP, "");
9089 check_a2dp_restore_l(adev, usecase->stream.out, true);
9090 break;
9091 }
9092 pthread_mutex_unlock(&usecase->stream.out->latch_lock);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05309093 }
9094 }
9095 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08009096
9097 //handle vr audio setparam
9098 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_VR_AUDIO_MODE,
9099 value, sizeof(value));
9100 if (ret >= 0) {
9101 ALOGI("Setting vr mode to be %s", value);
9102 if (!strncmp(value, "true", 4)) {
9103 adev->vr_audio_mode_enabled = true;
9104 ALOGI("Setting vr mode to true");
9105 } else if (!strncmp(value, "false", 5)) {
9106 adev->vr_audio_mode_enabled = false;
9107 ALOGI("Setting vr mode to false");
9108 } else {
9109 ALOGI("wrong vr mode set");
9110 }
9111 }
9112
Eric Laurent4b084132018-10-19 17:33:43 -07009113 //FIXME: to be replaced by proper video capture properties API
9114 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_CAMERA_FACING, value, sizeof(value));
9115 if (ret >= 0) {
9116 int camera_facing = CAMERA_FACING_BACK;
9117 if (strcmp(value, AUDIO_PARAMETER_VALUE_FRONT) == 0)
9118 camera_facing = CAMERA_FACING_FRONT;
9119 else if (strcmp(value, AUDIO_PARAMETER_VALUE_BACK) == 0)
9120 camera_facing = CAMERA_FACING_BACK;
9121 else {
9122 ALOGW("%s: invalid camera facing value: %s", __func__, value);
9123 goto done;
9124 }
9125 adev->camera_orientation =
9126 (adev->camera_orientation & ~CAMERA_FACING_MASK) | camera_facing;
9127 struct audio_usecase *usecase;
9128 struct listnode *node;
9129 list_for_each(node, &adev->usecase_list) {
9130 usecase = node_to_item(node, struct audio_usecase, list);
9131 struct stream_in *in = usecase->stream.in;
9132 if (usecase->type == PCM_CAPTURE && in != NULL &&
9133 in->source == AUDIO_SOURCE_CAMCORDER && !in->standby) {
9134 select_devices(adev, in->usecase);
9135 }
9136 }
9137 }
9138
Naresh Tannirucd2353e2016-08-19 00:37:25 +05309139 audio_extn_set_parameters(adev, parms);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08009140done:
9141 str_parms_destroy(parms);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08009142 pthread_mutex_unlock(&adev->lock);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05309143error:
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07009144 ALOGV("%s: exit with code(%d)", __func__, status);
9145 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009146}
9147
9148static char* adev_get_parameters(const struct audio_hw_device *dev,
9149 const char *keys)
9150{
Sidipotu Ashokaa4fa6a2018-12-21 09:19:26 +05309151 ALOGD("%s:%s", __func__, keys);
9152
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07009153 struct audio_device *adev = (struct audio_device *)dev;
9154 struct str_parms *reply = str_parms_create();
9155 struct str_parms *query = str_parms_create_str(keys);
9156 char *str;
Naresh Tannirud7205b62014-06-20 02:54:48 +05309157 char value[256] = {0};
9158 int ret = 0;
9159
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07009160 if (!query || !reply) {
Alexy Josephaee4fdd2016-01-29 13:02:07 -08009161 if (reply) {
9162 str_parms_destroy(reply);
9163 }
9164 if (query) {
9165 str_parms_destroy(query);
9166 }
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07009167 ALOGE("adev_get_parameters: failed to create query or reply");
9168 return NULL;
9169 }
9170
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08009171 //handle vr audio getparam
9172
9173 ret = str_parms_get_str(query,
9174 AUDIO_PARAMETER_KEY_VR_AUDIO_MODE,
9175 value, sizeof(value));
9176
9177 if (ret >= 0) {
9178 bool vr_audio_enabled = false;
9179 pthread_mutex_lock(&adev->lock);
9180 vr_audio_enabled = adev->vr_audio_mode_enabled;
9181 pthread_mutex_unlock(&adev->lock);
9182
Aniket Kumar Latad13758f2020-08-06 15:11:36 -07009183 ALOGV("getting vr mode to %d", vr_audio_enabled);
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08009184
9185 if (vr_audio_enabled) {
9186 str_parms_add_str(reply, AUDIO_PARAMETER_KEY_VR_AUDIO_MODE,
9187 "true");
9188 goto exit;
9189 } else {
9190 str_parms_add_str(reply, AUDIO_PARAMETER_KEY_VR_AUDIO_MODE,
9191 "false");
9192 goto exit;
9193 }
9194 }
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07009195
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08009196 pthread_mutex_lock(&adev->lock);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07009197 audio_extn_get_parameters(adev, query, reply);
Shiv Maliyappanahallif9308492013-12-12 12:18:09 -08009198 voice_get_parameters(adev, query, reply);
Aalique Grahame22e49102018-12-18 14:23:57 -08009199 audio_extn_a2dp_get_parameters(query, reply);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07009200 platform_get_parameters(adev->platform, query, reply);
justinwengd5395152019-11-04 12:23:09 +08009201 audio_extn_ma_get_parameters(adev, query, reply);
Naresh Tanniru80659832014-06-04 18:17:56 +05309202 pthread_mutex_unlock(&adev->lock);
9203
Naresh Tannirud7205b62014-06-20 02:54:48 +05309204exit:
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07009205 str = str_parms_to_str(reply);
9206 str_parms_destroy(query);
9207 str_parms_destroy(reply);
9208
Aniket Kumar Latad13758f2020-08-06 15:11:36 -07009209 ALOGV("%s: exit: returns - %s", __func__, str);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07009210 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009211}
9212
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07009213static int adev_init_check(const struct audio_hw_device *dev __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009214{
9215 return 0;
9216}
9217
9218static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
9219{
Haynes Mathew George5191a852013-09-11 14:19:36 -07009220 int ret;
9221 struct audio_device *adev = (struct audio_device *)dev;
Aalique Grahame22e49102018-12-18 14:23:57 -08009222
9223 audio_extn_extspk_set_voice_vol(adev->extspk, volume);
9224
Haynes Mathew George5191a852013-09-11 14:19:36 -07009225 pthread_mutex_lock(&adev->lock);
9226 /* cache volume */
Shruthi Krishnaace10852013-10-25 14:32:12 -07009227 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07009228 pthread_mutex_unlock(&adev->lock);
9229 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009230}
9231
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07009232static int adev_set_master_volume(struct audio_hw_device *dev __unused,
9233 float volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009234{
9235 return -ENOSYS;
9236}
9237
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07009238static int adev_get_master_volume(struct audio_hw_device *dev __unused,
9239 float *volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009240{
9241 return -ENOSYS;
9242}
9243
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07009244static int adev_set_master_mute(struct audio_hw_device *dev __unused,
9245 bool muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009246{
9247 return -ENOSYS;
9248}
9249
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07009250static int adev_get_master_mute(struct audio_hw_device *dev __unused,
9251 bool *muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009252{
9253 return -ENOSYS;
9254}
9255
9256static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
9257{
9258 struct audio_device *adev = (struct audio_device *)dev;
Garmond Leung5fd0b552018-04-17 11:56:12 -07009259 struct listnode *node;
9260 struct audio_usecase *usecase = NULL;
9261 int ret = 0;
kunleizdc4af9d2017-05-04 12:15:35 +08009262
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009263 pthread_mutex_lock(&adev->lock);
9264 if (adev->mode != mode) {
Jaideep Sharma477917f2020-03-13 18:13:33 +05309265 ALOGD("%s: mode %d , prev_mode %d \n", __func__, mode , adev->mode);
9266 adev->prev_mode = adev->mode; /* prev_mode is kept to handle voip concurrency*/
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009267 adev->mode = mode;
Weiyin Jiang96b96f72020-09-22 16:48:19 +08009268 if (mode == AUDIO_MODE_CALL_SCREEN) {
Jaideep Sharma477917f2020-03-13 18:13:33 +05309269 adev->current_call_output = adev->primary_output;
9270 voice_start_call(adev);
9271 } else if (voice_is_in_call_or_call_screen(adev) &&
Kunlei Zhang1d5c7f22019-05-21 14:25:57 +08009272 (mode == AUDIO_MODE_NORMAL ||
9273 (mode == AUDIO_MODE_IN_COMMUNICATION && !voice_is_call_state_active(adev)))) {
Garmond Leung5fd0b552018-04-17 11:56:12 -07009274 list_for_each(node, &adev->usecase_list) {
9275 usecase = node_to_item(node, struct audio_usecase, list);
9276 if (usecase->type == VOICE_CALL)
9277 break;
9278 }
9279 if (usecase &&
9280 audio_is_usb_out_device(usecase->out_snd_device & AUDIO_DEVICE_OUT_ALL_USB)) {
9281 ret = audio_extn_usb_check_and_set_svc_int(usecase,
9282 true);
9283 if (ret != 0) {
9284 /* default service interval was successfully updated,
9285 reopen USB backend with new service interval */
9286 check_usecases_codec_backend(adev,
9287 usecase,
9288 usecase->out_snd_device);
9289 }
9290 }
9291
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07009292 voice_stop_call(adev);
Banajit Goswami20cdd212015-09-11 01:11:30 -07009293 platform_set_gsm_mode(adev->platform, false);
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07009294 adev->current_call_output = NULL;
kunleizdc4af9d2017-05-04 12:15:35 +08009295 // restore device for other active usecases after stop call
9296 list_for_each(node, &adev->usecase_list) {
9297 usecase = node_to_item(node, struct audio_usecase, list);
9298 select_devices(adev, usecase->id);
9299 }
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07009300 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009301 }
9302 pthread_mutex_unlock(&adev->lock);
9303 return 0;
9304}
9305
9306static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
9307{
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08009308 int ret;
Aalique Grahame22e49102018-12-18 14:23:57 -08009309 struct audio_device *adev = (struct audio_device *)dev;
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08009310
9311 pthread_mutex_lock(&adev->lock);
Vidyakumar Athota2850d532013-11-19 16:02:12 -08009312 ALOGD("%s state %d\n", __func__, state);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08009313 ret = voice_set_mic_mute((struct audio_device *)dev, state);
Aalique Grahame22e49102018-12-18 14:23:57 -08009314
Derek Chend2530072014-11-24 12:39:14 -08009315 if (adev->ext_hw_plugin)
9316 ret = audio_extn_ext_hw_plugin_set_mic_mute(adev->ext_hw_plugin, state);
Aalique Grahame22e49102018-12-18 14:23:57 -08009317
9318 adev->mic_muted = state;
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08009319 pthread_mutex_unlock(&adev->lock);
9320
9321 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009322}
9323
9324static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
9325{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07009326 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009327 return 0;
9328}
9329
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07009330static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009331 const struct audio_config *config)
9332{
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07009333 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009334
Aalique Grahame22e49102018-12-18 14:23:57 -08009335 /* Don't know if USB HIFI in this context so use true to be conservative */
9336 if (check_input_parameters(config->sample_rate, config->format, channel_count,
9337 true /*is_usb_hifi */) != 0)
9338 return 0;
9339
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07009340 return get_input_buffer_size(config->sample_rate, config->format, channel_count,
9341 false /* is_low_latency: since we don't know, be conservative */);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009342}
9343
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009344static bool adev_input_allow_hifi_record(struct audio_device *adev,
9345 audio_devices_t devices,
9346 audio_input_flags_t flags,
9347 audio_source_t source) {
9348 const bool allowed = true;
9349
9350 if (!audio_is_usb_in_device(devices))
9351 return !allowed;
9352
9353 switch (flags) {
9354 case AUDIO_INPUT_FLAG_NONE:
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009355 break;
Haynes Mathew George59862182017-10-24 16:23:57 -07009356 case AUDIO_INPUT_FLAG_FAST: // disallow hifi record for FAST as
9357 // it affects RTD numbers over USB
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009358 default:
9359 return !allowed;
9360 }
9361
9362 switch (source) {
9363 case AUDIO_SOURCE_DEFAULT:
9364 case AUDIO_SOURCE_MIC:
9365 case AUDIO_SOURCE_UNPROCESSED:
9366 break;
9367 default:
9368 return !allowed;
9369 }
9370
9371 switch (adev->mode) {
9372 case 0:
9373 break;
9374 default:
9375 return !allowed;
9376 }
9377
9378 return allowed;
9379}
9380
Haynes Mathew George4ffef292017-11-21 15:08:02 -08009381static int adev_update_voice_comm_input_stream(struct stream_in *in,
9382 struct audio_config *config)
9383{
9384 bool valid_rate = (config->sample_rate == 8000 ||
9385 config->sample_rate == 16000 ||
9386 config->sample_rate == 32000 ||
9387 config->sample_rate == 48000);
9388 bool valid_ch = audio_channel_count_from_in_mask(in->channel_mask) == 1;
9389
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08009390 if(!voice_extn_is_compress_voip_supported()) {
kunleiz28c73e72019-03-27 17:24:04 +08009391 if (valid_rate && valid_ch) {
Haynes Mathew George4ffef292017-11-21 15:08:02 -08009392 in->usecase = USECASE_AUDIO_RECORD_VOIP;
9393 in->config = default_pcm_config_voip_copp;
9394 in->config.period_size = VOIP_IO_BUF_SIZE(in->sample_rate,
9395 DEFAULT_VOIP_BUF_DURATION_MS,
9396 DEFAULT_VOIP_BIT_DEPTH_BYTE)/2;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08009397 } else {
9398 ALOGW("%s No valid input in voip, use defaults"
9399 "sample rate %u, channel mask 0x%X",
9400 __func__, config->sample_rate, in->channel_mask);
9401 }
Haynes Mathew George4ffef292017-11-21 15:08:02 -08009402 in->config.rate = config->sample_rate;
9403 in->sample_rate = config->sample_rate;
9404 } else {
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08009405 //XXX needed for voice_extn_compress_voip_open_input_stream
9406 in->config.rate = config->sample_rate;
9407 if ((in->dev->mode == AUDIO_MODE_IN_COMMUNICATION ||
Shalini Manjunathaa763cc42019-08-23 15:13:46 +05309408 in->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08009409 voice_extn_compress_voip_is_active(in->dev)) &&
9410 (voice_extn_compress_voip_is_format_supported(in->format)) &&
9411 valid_rate && valid_ch) {
9412 voice_extn_compress_voip_open_input_stream(in);
9413 // update rate entries to match config from AF
9414 in->config.rate = config->sample_rate;
9415 in->sample_rate = config->sample_rate;
9416 } else {
9417 ALOGW("%s compress voip not active, use defaults", __func__);
9418 }
Haynes Mathew George4ffef292017-11-21 15:08:02 -08009419 }
Haynes Mathew George4ffef292017-11-21 15:08:02 -08009420 return 0;
9421}
9422
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009423static int adev_open_input_stream(struct audio_hw_device *dev,
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07009424 audio_io_handle_t handle,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009425 audio_devices_t devices,
9426 struct audio_config *config,
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07009427 struct audio_stream_in **stream_in,
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05309428 audio_input_flags_t flags,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08009429 const char *address,
Vidyakumar Athota5c398212015-03-31 21:53:21 -07009430 audio_source_t source)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009431{
9432 struct audio_device *adev = (struct audio_device *)dev;
9433 struct stream_in *in;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08009434 int ret = 0, buffer_size, frame_size;
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07009435 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07009436 bool is_low_latency = false;
Divya Narayanan Poojary45f19192016-09-30 18:52:13 +05309437 bool channel_mask_updated = false;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009438 bool is_usb_dev = audio_is_usb_in_device(devices);
9439 bool may_use_hifi_record = adev_input_allow_hifi_record(adev,
9440 devices,
9441 flags,
9442 source);
Andy Hung94320602018-10-29 18:31:12 -07009443 ALOGV("%s: enter: flags %#x, is_usb_dev %d, may_use_hifi_record %d,"
9444 " sample_rate %u, channel_mask %#x, format %#x",
9445 __func__, flags, is_usb_dev, may_use_hifi_record,
9446 config->sample_rate, config->channel_mask, config->format);
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05309447
kunleizdff872d2018-08-20 14:40:33 +08009448 if (is_usb_dev && (!audio_extn_usb_connected(NULL))) {
kunleizd6a9e0c2018-07-30 15:38:52 +08009449 is_usb_dev = false;
9450 devices = AUDIO_DEVICE_IN_BUILTIN_MIC;
9451 ALOGW("%s: ignore set device to non existing USB card, use input device(%#x)",
9452 __func__, devices);
9453 }
9454
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009455 *stream_in = NULL;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009456
9457 if (!(is_usb_dev && may_use_hifi_record)) {
9458 if (config->sample_rate == 0)
9459 config->sample_rate = 48000;
9460 if (config->channel_mask == AUDIO_CHANNEL_NONE)
9461 config->channel_mask = AUDIO_CHANNEL_IN_MONO;
9462 if (config->format == AUDIO_FORMAT_DEFAULT)
9463 config->format = AUDIO_FORMAT_PCM_16_BIT;
9464
9465 channel_count = audio_channel_count_from_in_mask(config->channel_mask);
9466
Aalique Grahame22e49102018-12-18 14:23:57 -08009467 if (check_input_parameters(config->sample_rate, config->format, channel_count,
9468 false) != 0)
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009469 return -EINVAL;
Chaithanya Krishna Bacharaju9955b162016-05-25 16:25:53 +05309470 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009471
9472 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07009473
9474 if (!in) {
9475 ALOGE("failed to allocate input stream");
9476 return -ENOMEM;
9477 }
9478
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05309479 ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x)\
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05309480 stream_handle(%p) io_handle(%d) source(%d) format %x",__func__, config->sample_rate,
9481 config->channel_mask, devices, &in->stream, handle, source, config->format);
Ravi Kumar Alamanda40703102014-04-24 10:34:41 -07009482 pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07009483 pthread_mutex_init(&in->pre_lock, (const pthread_mutexattr_t *) NULL);
Ravi Kumar Alamanda40703102014-04-24 10:34:41 -07009484
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009485 in->stream.common.get_sample_rate = in_get_sample_rate;
9486 in->stream.common.set_sample_rate = in_set_sample_rate;
9487 in->stream.common.get_buffer_size = in_get_buffer_size;
9488 in->stream.common.get_channels = in_get_channels;
9489 in->stream.common.get_format = in_get_format;
9490 in->stream.common.set_format = in_set_format;
9491 in->stream.common.standby = in_standby;
9492 in->stream.common.dump = in_dump;
9493 in->stream.common.set_parameters = in_set_parameters;
9494 in->stream.common.get_parameters = in_get_parameters;
9495 in->stream.common.add_audio_effect = in_add_audio_effect;
9496 in->stream.common.remove_audio_effect = in_remove_audio_effect;
9497 in->stream.set_gain = in_set_gain;
9498 in->stream.read = in_read;
9499 in->stream.get_input_frames_lost = in_get_input_frames_lost;
Aalique Grahame22e49102018-12-18 14:23:57 -08009500 in->stream.get_capture_position = in_get_capture_position;
Naresh Tannirudcb47c52018-06-25 16:23:32 +05309501 in->stream.get_active_microphones = in_get_active_microphones;
Paul McLeana50b7332018-12-17 08:24:21 -07009502 in->stream.set_microphone_direction = in_set_microphone_direction;
9503 in->stream.set_microphone_field_dimension = in_set_microphone_field_dimension;
juyuchendb308c22019-01-21 11:57:17 -07009504 in->stream.update_sink_metadata = in_update_sink_metadata;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009505
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08009506 list_init(&in->device_list);
9507 update_device_list(&in->device_list, devices, address, true);
Vidyakumar Athota5c398212015-03-31 21:53:21 -07009508 in->source = source;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009509 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009510 in->standby = 1;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07009511 in->capture_handle = handle;
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07009512 in->flags = flags;
Haynes Mathew George46740472017-10-27 18:40:12 -07009513 in->bit_width = 16;
9514 in->af_period_multiplier = 1;
justinweng20fb6d82019-02-21 18:49:00 -07009515 in->direction = MIC_DIRECTION_UNSPECIFIED;
9516 in->zoom = 0;
Carter Hsu2e429db2019-05-14 18:50:52 +08009517 list_init(&in->aec_list);
9518 list_init(&in->ns_list);
Phil Burkd898ba62019-06-20 12:49:01 -07009519 in->mmap_shared_memory_fd = -1; // not open
Haynes Mathew George46740472017-10-27 18:40:12 -07009520
Andy Hung94320602018-10-29 18:31:12 -07009521 ALOGV("%s: source %d, config->channel_mask %#x", __func__, source, config->channel_mask);
Aalique Grahame22e49102018-12-18 14:23:57 -08009522 if (source == AUDIO_SOURCE_VOICE_UPLINK ||
9523 source == AUDIO_SOURCE_VOICE_DOWNLINK) {
9524 /* Force channel config requested to mono if incall
9525 record is being requested for only uplink/downlink */
9526 if (config->channel_mask != AUDIO_CHANNEL_IN_MONO) {
9527 config->channel_mask = AUDIO_CHANNEL_IN_MONO;
9528 ret = -EINVAL;
9529 goto err_open;
9530 }
9531 }
9532
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009533 if (is_usb_dev && may_use_hifi_record) {
9534 /* HiFi record selects an appropriate format, channel, rate combo
9535 depending on sink capabilities*/
9536 ret = read_usb_sup_params_and_compare(false /*is_playback*/,
9537 &config->format,
9538 &in->supported_formats[0],
9539 MAX_SUPPORTED_FORMATS,
9540 &config->channel_mask,
9541 &in->supported_channel_masks[0],
9542 MAX_SUPPORTED_CHANNEL_MASKS,
9543 &config->sample_rate,
9544 &in->supported_sample_rates[0],
9545 MAX_SUPPORTED_SAMPLE_RATES);
9546 if (ret != 0) {
9547 ret = -EINVAL;
9548 goto err_open;
9549 }
9550 channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Haynes Mathew George4ffef292017-11-21 15:08:02 -08009551 } else if (config->format == AUDIO_FORMAT_DEFAULT) {
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05309552 config->format = AUDIO_FORMAT_PCM_16_BIT;
Surendar karkaaca3d082017-11-09 15:18:37 +05309553 } else if (property_get_bool("vendor.audio.capture.pcm.32bit.enable", false)
9554 && config->format == AUDIO_FORMAT_PCM_32_BIT) {
9555 in->config.format = PCM_FORMAT_S32_LE;
9556 in->bit_width = 32;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05309557 } else if ((config->format == AUDIO_FORMAT_PCM_FLOAT) ||
9558 (config->format == AUDIO_FORMAT_PCM_32_BIT) ||
9559 (config->format == AUDIO_FORMAT_PCM_24_BIT_PACKED) ||
9560 (config->format == AUDIO_FORMAT_PCM_8_24_BIT)) {
9561 bool ret_error = false;
9562 in->bit_width = 24;
9563 /* 24 bit is restricted to UNPROCESSED source only,also format supported
9564 from HAL is 24_packed and 8_24
9565 *> In case of UNPROCESSED source, for 24 bit, if format requested is other than
9566 24_packed return error indicating supported format is 24_packed
9567 *> In case of any other source requesting 24 bit or float return error
9568 indicating format supported is 16 bit only.
9569
9570 on error flinger will retry with supported format passed
9571 */
9572 if ((source != AUDIO_SOURCE_UNPROCESSED) &&
9573 (source != AUDIO_SOURCE_CAMCORDER)) {
9574 config->format = AUDIO_FORMAT_PCM_16_BIT;
9575 if (config->sample_rate > 48000)
9576 config->sample_rate = 48000;
9577 ret_error = true;
Haynes Mathew George46740472017-10-27 18:40:12 -07009578 } else if (!(config->format == AUDIO_FORMAT_PCM_24_BIT_PACKED ||
9579 config->format == AUDIO_FORMAT_PCM_8_24_BIT)) {
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05309580 config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
9581 ret_error = true;
9582 }
9583
9584 if (ret_error) {
9585 ret = -EINVAL;
9586 goto err_open;
9587 }
9588 }
9589
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009590 in->channel_mask = config->channel_mask;
9591 in->format = config->format;
9592
9593 in->usecase = USECASE_AUDIO_RECORD;
Samyak Jain0aa07ab2019-04-04 14:36:32 +05309594
Huicheng Liu1404ba12020-09-11 01:03:25 -04009595 /* validate bus device address */
9596 if (compare_device_type(&in->device_list, AUDIO_DEVICE_IN_BUS)) {
9597 /* extract car audio stream index */
9598 in->car_audio_stream =
9599 audio_extn_auto_hal_get_car_audio_stream_from_address(address);
9600 if (in->car_audio_stream < 0) {
9601 ALOGE("%s: invalid car audio stream %x",
9602 __func__, in->car_audio_stream);
9603 ret = -EINVAL;
9604 goto err_open;
9605 }
9606 ALOGV("%s: car_audio_stream 0x%x", __func__, in->car_audio_stream);
Huicheng Liuea4fd422021-01-12 18:34:50 -05009607 ret = audio_extn_auto_hal_open_input_stream(in);
9608 if (ret) {
9609 ALOGE("%s: Failed to open input stream for bus device", __func__);
9610 ret = -EINVAL;
9611 goto err_open;
9612 }
Huicheng Liu1404ba12020-09-11 01:03:25 -04009613 }
9614
Susan Wange3959562021-03-11 11:50:26 -05009615 /* reassign use case for echo reference stream on automotive platforms */
9616 if (in->source == AUDIO_SOURCE_ECHO_REFERENCE) {
9617 ret = audio_extn_auto_hal_open_echo_reference_stream(in);
9618 }
9619
Samyak Jain0aa07ab2019-04-04 14:36:32 +05309620 if (in->source == AUDIO_SOURCE_FM_TUNER) {
9621 if(!get_usecase_from_list(adev, USECASE_AUDIO_RECORD_FM_VIRTUAL))
9622 in->usecase = USECASE_AUDIO_RECORD_FM_VIRTUAL;
9623 else {
9624 ret = -EINVAL;
9625 goto err_open;
9626 }
9627 }
9628
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009629 if (config->sample_rate == LOW_LATENCY_CAPTURE_SAMPLE_RATE &&
Deeraj Somanfa377bf2019-02-06 12:57:59 +05309630 (flags & AUDIO_INPUT_FLAG_TIMESTAMP) == 0 &&
9631 (flags & AUDIO_INPUT_FLAG_COMPRESS) == 0 &&
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009632 (flags & AUDIO_INPUT_FLAG_FAST) != 0) {
9633 is_low_latency = true;
9634#if LOW_LATENCY_CAPTURE_USE_CASE
Revathi Uddarajud9f23d92020-07-27 10:55:06 +05309635 if ((flags & AUDIO_INPUT_FLAG_VOIP_TX) != 0)
9636 in->usecase = USECASE_AUDIO_RECORD_VOIP_LOW_LATENCY;
9637 else
9638 in->usecase = USECASE_AUDIO_RECORD_LOW_LATENCY;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009639#endif
9640 in->realtime = may_use_noirq_mode(adev, in->usecase, in->flags);
Aalique Grahame22e49102018-12-18 14:23:57 -08009641 if (!in->realtime) {
9642 in->config = pcm_config_audio_capture;
9643 frame_size = audio_stream_in_frame_size(&in->stream);
9644 buffer_size = get_input_buffer_size(config->sample_rate,
9645 config->format,
9646 channel_count,
9647 is_low_latency);
9648 in->config.period_size = buffer_size / frame_size;
9649 in->config.rate = config->sample_rate;
9650 in->af_period_multiplier = 1;
9651 } else {
9652 // period size is left untouched for rt mode playback
9653 in->config = pcm_config_audio_capture_rt;
9654 in->af_period_multiplier = af_period_multiplier;
9655 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009656 }
9657
Susan Wangb803cb52021-10-14 12:03:36 -04009658 /* Additional sample rates added below must also be present
9659 in audio_policy_configuration.xml for mmap_no_irq_in */
9660 bool valid_mmap_record_rate = (config->sample_rate == 8000 ||
9661 config->sample_rate == 16000 ||
Susan Wang89da9042021-10-14 12:03:36 -04009662 config->sample_rate == 24000 ||
Susan Wangb803cb52021-10-14 12:03:36 -04009663 config->sample_rate == 32000 ||
9664 config->sample_rate == 48000);
9665 if (valid_mmap_record_rate &&
9666 ((in->flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0)) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009667 in->realtime = 0;
9668 in->usecase = USECASE_AUDIO_RECORD_MMAP;
9669 in->config = pcm_config_mmap_capture;
Haynes Mathew George46740472017-10-27 18:40:12 -07009670 in->config.format = pcm_format_from_audio_format(config->format);
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009671 in->stream.start = in_start;
9672 in->stream.stop = in_stop;
9673 in->stream.create_mmap_buffer = in_create_mmap_buffer;
9674 in->stream.get_mmap_position = in_get_mmap_position;
Ramjee Singhbcb3b6c2021-11-17 13:19:16 +05309675 in->config.rate = config->sample_rate;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009676 ALOGV("%s: USECASE_AUDIO_RECORD_MMAP", __func__);
Haynes Mathew George46740472017-10-27 18:40:12 -07009677 } else if (is_usb_dev && may_use_hifi_record) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009678 in->usecase = USECASE_AUDIO_RECORD_HIFI;
9679 in->config = pcm_config_audio_capture;
9680 frame_size = audio_stream_in_frame_size(&in->stream);
9681 buffer_size = get_input_buffer_size(config->sample_rate,
9682 config->format,
9683 channel_count,
9684 false /*is_low_latency*/);
9685 in->config.period_size = buffer_size / frame_size;
9686 in->config.rate = config->sample_rate;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009687 in->config.format = pcm_format_from_audio_format(config->format);
Karthikeyan Mani07faa602018-08-20 11:01:32 -07009688 switch (config->format) {
9689 case AUDIO_FORMAT_PCM_32_BIT:
9690 in->bit_width = 32;
9691 break;
9692 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
9693 case AUDIO_FORMAT_PCM_8_24_BIT:
9694 in->bit_width = 24;
9695 break;
9696 default:
9697 in->bit_width = 16;
9698 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08009699 } else if (is_single_device_type_equal(&in->device_list,
9700 AUDIO_DEVICE_IN_TELEPHONY_RX) ||
9701 is_single_device_type_equal(&in->device_list,
9702 AUDIO_DEVICE_IN_PROXY)) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07009703 if (config->sample_rate == 0)
9704 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
9705 if (config->sample_rate != 48000 && config->sample_rate != 16000 &&
9706 config->sample_rate != 8000) {
9707 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
9708 ret = -EINVAL;
9709 goto err_open;
9710 }
9711 if (config->format == AUDIO_FORMAT_DEFAULT)
9712 config->format = AUDIO_FORMAT_PCM_16_BIT;
9713 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
9714 config->format = AUDIO_FORMAT_PCM_16_BIT;
9715 ret = -EINVAL;
9716 goto err_open;
9717 }
9718
9719 in->usecase = USECASE_AUDIO_RECORD_AFE_PROXY;
Zhou Song62ea0282020-03-22 19:53:01 +08009720 if (adev->ha_proxy_enable &&
9721 is_single_device_type_equal(&in->device_list,
9722 AUDIO_DEVICE_IN_TELEPHONY_RX))
9723 in->usecase = USECASE_AUDIO_RECORD_AFE_PROXY2;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07009724 in->config = pcm_config_afe_proxy_record;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07009725 in->config.rate = config->sample_rate;
Aalique Grahame22e49102018-12-18 14:23:57 -08009726 in->af_period_multiplier = 1;
9727 } else if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
Shalini Manjunathaac412b32020-01-03 15:00:14 +05309728 (!voice_extn_is_compress_voip_supported()) &&
Aalique Grahame22e49102018-12-18 14:23:57 -08009729 in->flags & AUDIO_INPUT_FLAG_VOIP_TX &&
9730 (config->sample_rate == 8000 ||
9731 config->sample_rate == 16000 ||
9732 config->sample_rate == 32000 ||
9733 config->sample_rate == 48000) &&
9734 channel_count == 1) {
9735 in->usecase = USECASE_AUDIO_RECORD_VOIP;
9736 in->config = pcm_config_audio_capture;
9737 frame_size = audio_stream_in_frame_size(&in->stream);
9738 buffer_size = get_stream_buffer_size(VOIP_CAPTURE_PERIOD_DURATION_MSEC,
9739 config->sample_rate,
9740 config->format,
9741 channel_count, false /*is_low_latency*/);
9742 in->config.period_size = buffer_size / frame_size;
9743 in->config.period_count = VOIP_CAPTURE_PERIOD_COUNT;
9744 in->config.rate = config->sample_rate;
9745 in->af_period_multiplier = 1;
Mingshu Pangc2d65042021-01-14 16:19:10 +08009746 } else if (in->realtime) {
9747 in->config = pcm_config_audio_capture_rt;
9748 in->config.format = pcm_format_from_audio_format(config->format);
9749 in->af_period_multiplier = af_period_multiplier;
Haynes Mathew George46740472017-10-27 18:40:12 -07009750 } else {
Revathi Uddarajud2634032017-12-07 14:42:34 +05309751 int ret_val;
9752 pthread_mutex_lock(&adev->lock);
9753 ret_val = audio_extn_check_and_set_multichannel_usecase(adev,
9754 in, config, &channel_mask_updated);
9755 pthread_mutex_unlock(&adev->lock);
9756
9757 if (!ret_val) {
9758 if (channel_mask_updated == true) {
9759 ALOGD("%s: return error to retry with updated channel mask (%#x)",
9760 __func__, config->channel_mask);
9761 ret = -EINVAL;
9762 goto err_open;
9763 }
9764 ALOGD("%s: created multi-channel session succesfully",__func__);
9765 } else if (audio_extn_compr_cap_enabled() &&
9766 audio_extn_compr_cap_format_supported(config->format) &&
9767 (in->dev->mode != AUDIO_MODE_IN_COMMUNICATION)) {
9768 audio_extn_compr_cap_init(in);
9769 } else if (audio_extn_cin_applicable_stream(in)) {
Deeraj Soman14230922019-01-30 16:39:30 +05309770 ret = audio_extn_cin_configure_input_stream(in, config);
Revathi Uddarajud2634032017-12-07 14:42:34 +05309771 if (ret)
9772 goto err_open;
9773 } else {
9774 in->config = pcm_config_audio_capture;
9775 in->config.rate = config->sample_rate;
9776 in->config.format = pcm_format_from_audio_format(config->format);
Revathi Uddarajud2634032017-12-07 14:42:34 +05309777 in->format = config->format;
9778 frame_size = audio_stream_in_frame_size(&in->stream);
9779 buffer_size = get_input_buffer_size(config->sample_rate,
Haynes Mathew George46740472017-10-27 18:40:12 -07009780 config->format,
9781 channel_count,
9782 is_low_latency);
Dieter Luecking5d57def2018-09-07 14:23:37 +02009783 /* prevent division-by-zero */
9784 if (frame_size == 0) {
9785 ALOGE("%s: Error frame_size==0", __func__);
9786 ret = -EINVAL;
9787 goto err_open;
9788 }
9789
Revathi Uddarajud2634032017-12-07 14:42:34 +05309790 in->config.period_size = buffer_size / frame_size;
Aalique Grahame22e49102018-12-18 14:23:57 -08009791 in->af_period_multiplier = 1;
Haynes Mathew George4ffef292017-11-21 15:08:02 -08009792
Revathi Uddarajud2634032017-12-07 14:42:34 +05309793 if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
9794 /* optionally use VOIP usecase depending on config(s) */
9795 ret = adev_update_voice_comm_input_stream(in, config);
9796 }
Haynes Mathew George4ffef292017-11-21 15:08:02 -08009797
Revathi Uddarajud2634032017-12-07 14:42:34 +05309798 if (ret) {
9799 ALOGE("%s AUDIO_SOURCE_VOICE_COMMUNICATION invalid args", __func__);
9800 goto err_open;
9801 }
Haynes Mathew George4ffef292017-11-21 15:08:02 -08009802 }
Jaideep Sharmad305a4a2020-02-27 14:29:04 +05309803
9804 /* assign concurrent capture usecase if record has to caried out from
9805 * actual hardware input source */
9806 if (audio_extn_is_concurrent_capture_enabled() &&
9807 !audio_is_virtual_input_source(in->source)) {
Samyak Jainc37062f2019-04-25 18:41:06 +05309808 /* Acquire lock to avoid two concurrent use cases initialized to
9809 same pcm record use case */
kunleiz28c73e72019-03-27 17:24:04 +08009810
Samyak Jainc37062f2019-04-25 18:41:06 +05309811 if (in->usecase == USECASE_AUDIO_RECORD) {
9812 pthread_mutex_lock(&adev->lock);
9813 if (!(adev->pcm_record_uc_state)) {
9814 ALOGV("%s: using USECASE_AUDIO_RECORD",__func__);
9815 adev->pcm_record_uc_state = 1;
9816 pthread_mutex_unlock(&adev->lock);
9817 } else {
9818 pthread_mutex_unlock(&adev->lock);
9819 /* Assign compress record use case for second record */
9820 in->usecase = USECASE_AUDIO_RECORD_COMPRESS2;
9821 in->flags |= AUDIO_INPUT_FLAG_COMPRESS;
9822 ALOGV("%s: overriding usecase with USECASE_AUDIO_RECORD_COMPRESS2 and appending compress flag", __func__);
9823 if (audio_extn_cin_applicable_stream(in)) {
9824 in->sample_rate = config->sample_rate;
Deeraj Soman14230922019-01-30 16:39:30 +05309825 ret = audio_extn_cin_configure_input_stream(in, config);
Samyak Jainc37062f2019-04-25 18:41:06 +05309826 if (ret)
9827 goto err_open;
9828 }
9829 }
9830 }
kunleiz28c73e72019-03-27 17:24:04 +08009831 }
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07009832 }
Ramjee Singh82fd0c12019-08-21 16:31:33 +05309833 if (audio_extn_ssr_get_stream() != in)
9834 in->config.channels = channel_count;
Aniket Kumar Lata23d8cbf2019-04-10 19:54:46 -07009835
Aniket Kumar Lata23d8cbf2019-04-10 19:54:46 -07009836 in->sample_rate = in->config.rate;
9837
Dhananjay Kumard6d32152016-10-13 16:11:03 +05309838 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
9839 &adev->streams_input_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08009840 &in->device_list, flags, in->format,
Haynes Mathew George4ffef292017-11-21 15:08:02 -08009841 in->sample_rate, in->bit_width,
9842 in->profile, &in->app_type_cfg);
Naresh Tanniru04f71882018-06-26 17:46:22 +05309843 register_format(in->format, in->supported_formats);
9844 register_channel_mask(in->channel_mask, in->supported_channel_masks);
9845 register_sample_rate(in->sample_rate, in->supported_sample_rates);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05309846
Dechen Chai22768452021-07-30 09:29:16 +05309847#ifndef LINUX_ENABLED
Aalique Grahame22e49102018-12-18 14:23:57 -08009848 in->error_log = error_log_create(
9849 ERROR_LOG_ENTRIES,
9850 1000000000 /* aggregate consecutive identical errors within one second */);
Dechen Chai22768452021-07-30 09:29:16 +05309851#endif
Aalique Grahame22e49102018-12-18 14:23:57 -08009852
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07009853 /* This stream could be for sound trigger lab,
9854 get sound trigger pcm if present */
9855 audio_extn_sound_trigger_check_and_get_session(in);
9856
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05309857 lock_input_stream(in);
9858 audio_extn_snd_mon_register_listener(in, in_snd_mon_cb);
9859 pthread_mutex_lock(&adev->lock);
9860 in->card_status = adev->card_status;
9861 pthread_mutex_unlock(&adev->lock);
9862 pthread_mutex_unlock(&in->lock);
9863
Aalique Grahame22e49102018-12-18 14:23:57 -08009864 stream_app_type_cfg_init(&in->app_type_cfg);
9865
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009866 *stream_in = &in->stream;
Derek Chenf939fb72018-11-13 13:34:41 -08009867
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07009868 ret = io_streams_map_insert(adev, &in->stream.common,
9869 handle, AUDIO_PATCH_HANDLE_NONE);
9870 if (ret != 0)
9871 goto err_open;
9872
Susan Wang6dd13092021-01-25 10:27:11 -05009873 in->in_ctxt.input = in;
Derek Chenf939fb72018-11-13 13:34:41 -08009874
9875 pthread_mutex_lock(&adev->lock);
Susan Wang6dd13092021-01-25 10:27:11 -05009876 list_add_tail(&adev->active_inputs_list, &in->in_ctxt.list);
Derek Chenf939fb72018-11-13 13:34:41 -08009877 pthread_mutex_unlock(&adev->lock);
9878
Eric Laurent994a6932013-07-17 11:51:42 -07009879 ALOGV("%s: exit", __func__);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08009880 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009881
9882err_open:
Samyak Jainc37062f2019-04-25 18:41:06 +05309883 if (in->usecase == USECASE_AUDIO_RECORD) {
9884 pthread_mutex_lock(&adev->lock);
9885 adev->pcm_record_uc_state = 0;
9886 pthread_mutex_unlock(&adev->lock);
9887 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009888 free(in);
9889 *stream_in = NULL;
9890 return ret;
9891}
9892
9893static void adev_close_input_stream(struct audio_hw_device *dev,
9894 struct audio_stream_in *stream)
9895{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08009896 int ret;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07009897 struct stream_in *in = (struct stream_in *)stream;
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07009898 struct audio_device *adev = (struct audio_device *)dev;
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05309899
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05309900 ALOGD("%s: enter:stream_handle(%p)",__func__, in);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08009901
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08009902 if (in == NULL) {
9903 ALOGE("%s: audio_stream_in ptr is NULL", __func__);
9904 return;
9905 }
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07009906 io_streams_map_remove(adev, in->capture_handle);
9907
Susan Wang6dd13092021-01-25 10:27:11 -05009908 // remove out_ctxt early to prevent the stream
9909 // being opened in a race condition
9910 pthread_mutex_lock(&adev->lock);
9911 list_remove(&in->in_ctxt.list);
9912 pthread_mutex_unlock(&adev->lock);
9913
kunleiz70e57612018-12-28 17:50:23 +08009914 /* must deregister from sndmonitor first to prevent races
9915 * between the callback and close_stream
9916 */
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05309917 audio_extn_snd_mon_unregister_listener(stream);
9918
kunleiz70e57612018-12-28 17:50:23 +08009919 /* Disable echo reference if there are no active input, hfp call
9920 * and sound trigger while closing input stream
9921 */
Eric Laurent637e2d42018-11-15 12:24:31 -08009922 if (adev_get_active_input(adev) == NULL &&
kunleiz70e57612018-12-28 17:50:23 +08009923 !audio_extn_hfp_is_active(adev) &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08009924 !audio_extn_sound_trigger_check_ec_ref_enable()) {
9925 struct listnode out_devices;
9926 list_init(&out_devices);
9927 platform_set_echo_reference(adev, false, &out_devices);
9928 } else
kunleiz70e57612018-12-28 17:50:23 +08009929 audio_extn_sound_trigger_update_ec_ref_status(false);
Venkata Narendra Kumar Gutta1bbbf542014-09-04 19:11:25 +05309930
Dechen Chai22768452021-07-30 09:29:16 +05309931#ifndef LINUX_ENABLED
Weiyin Jiang2995f662019-04-17 14:25:12 +08009932 error_log_destroy(in->error_log);
9933 in->error_log = NULL;
Dechen Chai22768452021-07-30 09:29:16 +05309934#endif
Pallavid7c7a272018-01-16 11:22:55 +05309935
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08009936 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05309937 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08009938 ret = voice_extn_compress_voip_close_input_stream(&stream->common);
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05309939 pthread_mutex_unlock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08009940 if (ret != 0)
9941 ALOGE("%s: Compress voip input cannot be closed, error:%d",
9942 __func__, ret);
9943 } else
9944 in_standby(&stream->common);
9945
Weiyin Jiang280ea742020-09-08 20:28:22 +08009946 pthread_mutex_destroy(&in->lock);
9947 pthread_mutex_destroy(&in->pre_lock);
9948
Revathi Uddarajud2634032017-12-07 14:42:34 +05309949 pthread_mutex_lock(&adev->lock);
Samyak Jain15fda662018-12-18 16:40:52 +05309950 if (in->usecase == USECASE_AUDIO_RECORD) {
9951 adev->pcm_record_uc_state = 0;
9952 }
9953
Kunlei Zhang5d5d8d92020-02-26 15:00:59 +08009954 if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
9955 adev->enable_voicerx = false;
9956 }
9957
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -07009958 if (audio_extn_ssr_get_stream() == in) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07009959 audio_extn_ssr_deinit();
9960 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009961
Garmond Leunge2433c32017-09-28 21:51:22 -07009962 if (audio_extn_ffv_get_stream() == in) {
9963 audio_extn_ffv_stream_deinit();
9964 }
9965
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05309966 if (audio_extn_compr_cap_enabled() &&
Mingming Yine62d7842013-10-25 16:26:03 -07009967 audio_extn_compr_cap_format_supported(in->config.format))
9968 audio_extn_compr_cap_deinit();
Dhanalakshmi Siddani74cf00b2016-12-02 13:55:57 +05309969
Dhananjay Kumar7dbe3562019-08-30 05:49:33 +05309970 if (audio_extn_cin_attached_usecase(in))
Manish Dewangan46e07982018-12-13 18:18:59 +05309971 audio_extn_cin_free_input_stream_resources(in);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07009972
Mingming Yinfd7607b2016-01-22 12:48:44 -08009973 if (in->is_st_session) {
9974 ALOGV("%s: sound trigger pcm stop lab", __func__);
9975 audio_extn_sound_trigger_stop_lab(in);
9976 }
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07009977 free(stream);
Revathi Uddarajud2634032017-12-07 14:42:34 +05309978 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009979 return;
9980}
9981
Aalique Grahame22e49102018-12-18 14:23:57 -08009982/* verifies input and output devices and their capabilities.
9983 *
9984 * This verification is required when enabling extended bit-depth or
9985 * sampling rates, as not all qcom products support it.
9986 *
9987 * Suitable for calling only on initialization such as adev_open().
9988 * It fills the audio_device use_case_table[] array.
9989 *
9990 * Has a side-effect that it needs to configure audio routing / devices
9991 * in order to power up the devices and read the device parameters.
9992 * It does not acquire any hw device lock. Should restore the devices
9993 * back to "normal state" upon completion.
9994 */
9995static int adev_verify_devices(struct audio_device *adev)
9996{
9997 /* enumeration is a bit difficult because one really wants to pull
9998 * the use_case, device id, etc from the hidden pcm_device_table[].
9999 * In this case there are the following use cases and device ids.
10000 *
10001 * [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = {0, 0},
10002 * [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {15, 15},
10003 * [USECASE_AUDIO_PLAYBACK_HIFI] = {1, 1},
10004 * [USECASE_AUDIO_PLAYBACK_OFFLOAD] = {9, 9},
10005 * [USECASE_AUDIO_RECORD] = {0, 0},
10006 * [USECASE_AUDIO_RECORD_LOW_LATENCY] = {15, 15},
10007 * [USECASE_VOICE_CALL] = {2, 2},
10008 *
10009 * USECASE_AUDIO_PLAYBACK_OFFLOAD, USECASE_AUDIO_PLAYBACK_HIFI omitted.
10010 * USECASE_VOICE_CALL omitted, but possible for either input or output.
10011 */
10012
10013 /* should be the usecases enabled in adev_open_input_stream() */
10014 static const int test_in_usecases[] = {
10015 USECASE_AUDIO_RECORD,
10016 USECASE_AUDIO_RECORD_LOW_LATENCY, /* does not appear to be used */
10017 };
10018 /* should be the usecases enabled in adev_open_output_stream()*/
10019 static const int test_out_usecases[] = {
10020 USECASE_AUDIO_PLAYBACK_DEEP_BUFFER,
10021 USECASE_AUDIO_PLAYBACK_LOW_LATENCY,
10022 };
10023 static const usecase_type_t usecase_type_by_dir[] = {
10024 PCM_PLAYBACK,
10025 PCM_CAPTURE,
10026 };
10027 static const unsigned flags_by_dir[] = {
10028 PCM_OUT,
10029 PCM_IN,
10030 };
10031
10032 size_t i;
10033 unsigned dir;
10034 const unsigned card_id = adev->snd_card;
10035
10036 for (dir = 0; dir < 2; ++dir) {
10037 const usecase_type_t usecase_type = usecase_type_by_dir[dir];
10038 const unsigned flags_dir = flags_by_dir[dir];
10039 const size_t testsize =
10040 dir ? ARRAY_SIZE(test_in_usecases) : ARRAY_SIZE(test_out_usecases);
10041 const int *testcases =
10042 dir ? test_in_usecases : test_out_usecases;
10043 const audio_devices_t audio_device =
10044 dir ? AUDIO_DEVICE_IN_BUILTIN_MIC : AUDIO_DEVICE_OUT_SPEAKER;
10045
10046 for (i = 0; i < testsize; ++i) {
10047 const audio_usecase_t audio_usecase = testcases[i];
10048 int device_id;
10049 struct pcm_params **pparams;
10050 struct stream_out out;
10051 struct stream_in in;
10052 struct audio_usecase uc_info;
10053 int retval;
10054
10055 pparams = &adev->use_case_table[audio_usecase];
10056 pcm_params_free(*pparams); /* can accept null input */
10057 *pparams = NULL;
10058
10059 /* find the device ID for the use case (signed, for error) */
10060 device_id = platform_get_pcm_device_id(audio_usecase, usecase_type);
10061 if (device_id < 0)
10062 continue;
10063
10064 /* prepare structures for device probing */
10065 memset(&uc_info, 0, sizeof(uc_info));
10066 uc_info.id = audio_usecase;
10067 uc_info.type = usecase_type;
Aniket Kumar Lata9c2fd892020-01-22 22:20:00 -080010068 list_init(&uc_info.device_list);
Aalique Grahame22e49102018-12-18 14:23:57 -080010069 if (dir) {
Aalique Grahame22e49102018-12-18 14:23:57 -080010070 memset(&in, 0, sizeof(in));
Aniket Kumar Lata9c2fd892020-01-22 22:20:00 -080010071 list_init(&in.device_list);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010072 update_device_list(&in.device_list, audio_device, "", true);
Aalique Grahame22e49102018-12-18 14:23:57 -080010073 in.source = AUDIO_SOURCE_VOICE_COMMUNICATION;
10074 uc_info.stream.in = &in;
Aalique Grahame22e49102018-12-18 14:23:57 -080010075 }
10076 memset(&out, 0, sizeof(out));
Aniket Kumar Lata9c2fd892020-01-22 22:20:00 -080010077 list_init(&out.device_list);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010078 update_device_list(&out.device_list, audio_device, "", true);
Aalique Grahame22e49102018-12-18 14:23:57 -080010079 uc_info.stream.out = &out;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010080 update_device_list(&uc_info.device_list, audio_device, "", true);
Aalique Grahame22e49102018-12-18 14:23:57 -080010081 uc_info.in_snd_device = SND_DEVICE_NONE;
10082 uc_info.out_snd_device = SND_DEVICE_NONE;
10083 list_add_tail(&adev->usecase_list, &uc_info.list);
10084
10085 /* select device - similar to start_(in/out)put_stream() */
10086 retval = select_devices(adev, audio_usecase);
10087 if (retval >= 0) {
10088 *pparams = pcm_params_get(card_id, device_id, flags_dir);
10089#if LOG_NDEBUG == 0
Aalique Grahame203bee02019-03-13 17:49:36 -070010090 char info[512]; /* for possible debug info */
Aalique Grahame22e49102018-12-18 14:23:57 -080010091 if (*pparams) {
10092 ALOGV("%s: (%s) card %d device %d", __func__,
10093 dir ? "input" : "output", card_id, device_id);
10094 pcm_params_to_string(*pparams, info, ARRAY_SIZE(info));
10095 } else {
10096 ALOGV("%s: cannot locate card %d device %d", __func__, card_id, device_id);
10097 }
10098#endif
10099 }
10100
10101 /* deselect device - similar to stop_(in/out)put_stream() */
10102 /* 1. Get and set stream specific mixer controls */
10103 retval = disable_audio_route(adev, &uc_info);
10104 /* 2. Disable the rx device */
10105 retval = disable_snd_device(adev,
10106 dir ? uc_info.in_snd_device : uc_info.out_snd_device);
10107 list_remove(&uc_info.list);
10108 }
10109 }
Aalique Grahame22e49102018-12-18 14:23:57 -080010110 return 0;
10111}
10112
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010113int update_patch(unsigned int num_sources,
10114 const struct audio_port_config *sources,
10115 unsigned int num_sinks,
10116 const struct audio_port_config *sinks,
10117 audio_patch_handle_t handle,
10118 struct audio_patch_info *p_info,
10119 patch_type_t patch_type, bool new_patch)
10120{
Aniket Kumar Latad13758f2020-08-06 15:11:36 -070010121 ALOGV("%s: enter", __func__);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010122
10123 if (p_info == NULL) {
10124 ALOGE("%s: Invalid patch pointer", __func__);
10125 return -EINVAL;
10126 }
10127
10128 if (new_patch) {
10129 p_info->patch = (struct audio_patch *) calloc(1, sizeof(struct audio_patch));
10130 if (p_info->patch == NULL) {
10131 ALOGE("%s: Could not allocate patch", __func__);
10132 return -ENOMEM;
10133 }
10134 }
10135
10136 p_info->patch->id = handle;
10137 p_info->patch->num_sources = num_sources;
10138 p_info->patch->num_sinks = num_sinks;
10139
10140 for (int i = 0; i < num_sources; i++)
10141 p_info->patch->sources[i] = sources[i];
10142 for (int i = 0; i < num_sinks; i++)
10143 p_info->patch->sinks[i] = sinks[i];
10144
10145 p_info->patch_type = patch_type;
10146 return 0;
10147}
10148
10149audio_patch_handle_t generate_patch_handle()
10150{
10151 static audio_patch_handle_t patch_handle = AUDIO_PATCH_HANDLE_NONE;
10152 if (++patch_handle < 0)
10153 patch_handle = AUDIO_PATCH_HANDLE_NONE + 1;
10154 return patch_handle;
10155}
10156
Siddartha Shaik31b530e2017-05-19 15:26:33 +053010157int adev_create_audio_patch(struct audio_hw_device *dev,
10158 unsigned int num_sources,
10159 const struct audio_port_config *sources,
10160 unsigned int num_sinks,
10161 const struct audio_port_config *sinks,
10162 audio_patch_handle_t *handle)
10163{
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010164 int ret = 0;
10165 struct audio_device *adev = (struct audio_device *)dev;
10166 struct audio_patch_info *p_info = NULL;
10167 patch_type_t patch_type = PATCH_NONE;
10168 audio_io_handle_t io_handle = AUDIO_IO_HANDLE_NONE;
10169 audio_source_t input_source = AUDIO_SOURCE_DEFAULT;
10170 struct audio_stream_info *s_info = NULL;
10171 struct audio_stream *stream = NULL;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010172 struct listnode devices;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010173 audio_devices_t device_type = AUDIO_DEVICE_NONE;
10174 bool new_patch = false;
10175 char addr[AUDIO_DEVICE_MAX_ADDRESS_LEN];
Siddartha Shaik31b530e2017-05-19 15:26:33 +053010176
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010177 ALOGD("%s: enter: num sources %d, num_sinks %d, handle %d", __func__,
10178 num_sources, num_sinks, *handle);
10179
10180 if (num_sources == 0 || num_sources > AUDIO_PATCH_PORTS_MAX ||
10181 num_sinks == 0 || num_sinks > AUDIO_PATCH_PORTS_MAX) {
10182 ALOGE("%s: Invalid patch arguments", __func__);
10183 ret = -EINVAL;
10184 goto done;
10185 }
10186
10187 if (num_sources > 1) {
10188 ALOGE("%s: Multiple sources are not supported", __func__);
10189 ret = -EINVAL;
10190 goto done;
10191 }
10192
10193 if (sources == NULL || sinks == NULL) {
10194 ALOGE("%s: Invalid sources or sinks port config", __func__);
10195 ret = -EINVAL;
10196 goto done;
10197 }
10198
10199 ALOGV("%s: source role %d, source type %d", __func__,
10200 sources[0].type, sources[0].role);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010201 list_init(&devices);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010202
10203 // Populate source/sink information and fetch stream info
10204 switch (sources[0].type) {
10205 case AUDIO_PORT_TYPE_DEVICE: // Patch for audio capture or loopback
10206 device_type = sources[0].ext.device.type;
10207 strlcpy(&addr[0], &sources[0].ext.device.address[0], AUDIO_DEVICE_MAX_ADDRESS_LEN);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010208 update_device_list(&devices, device_type, &addr[0], true);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010209 if (sinks[0].type == AUDIO_PORT_TYPE_MIX) {
10210 patch_type = PATCH_CAPTURE;
10211 io_handle = sinks[0].ext.mix.handle;
10212 input_source = sinks[0].ext.mix.usecase.source;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010213 ALOGD("%s: Capture patch from device %x to mix %d",
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010214 __func__, device_type, io_handle);
10215 } else {
10216 // Device to device patch is not implemented.
10217 // This space will need changes if audio HAL
10218 // handles device to device patches in the future.
10219 patch_type = PATCH_DEVICE_LOOPBACK;
10220 }
10221 break;
10222 case AUDIO_PORT_TYPE_MIX: // Patch for audio playback
10223 io_handle = sources[0].ext.mix.handle;
10224 for (int i = 0; i < num_sinks; i++) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010225 device_type = sinks[i].ext.device.type;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010226 strlcpy(&addr[0], &sinks[i].ext.device.address[0], AUDIO_DEVICE_MAX_ADDRESS_LEN);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010227 update_device_list(&devices, device_type, &addr[0], true);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010228 }
10229 patch_type = PATCH_PLAYBACK;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010230 ALOGD("%s: Playback patch from mix handle %d to device %x",
10231 __func__, io_handle, get_device_types(&devices));
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010232 break;
10233 case AUDIO_PORT_TYPE_SESSION:
10234 case AUDIO_PORT_TYPE_NONE:
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010235 ALOGE("%s: Unsupported source type %d", __func__, sources[0].type);
10236 ret = -EINVAL;
10237 goto done;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010238 }
10239
10240 pthread_mutex_lock(&adev->lock);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010241
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010242 // Generate patch info and update patch
10243 if (*handle == AUDIO_PATCH_HANDLE_NONE) {
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010244 *handle = generate_patch_handle();
10245 p_info = (struct audio_patch_info *)
10246 calloc(1, sizeof(struct audio_patch_info));
10247 if (p_info == NULL) {
10248 ALOGE("%s: Failed to allocate memory", __func__);
10249 pthread_mutex_unlock(&adev->lock);
10250 ret = -ENOMEM;
10251 goto done;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010252 }
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010253 new_patch = true;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010254 } else {
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010255 p_info = fetch_patch_info_l(adev, *handle);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010256 if (p_info == NULL) {
10257 ALOGE("%s: Unable to fetch patch for received patch handle %d",
10258 __func__, *handle);
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010259 pthread_mutex_unlock(&adev->lock);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010260 ret = -EINVAL;
10261 goto done;
10262 }
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010263 }
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010264 update_patch(num_sources, sources, num_sinks, sinks,
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010265 *handle, p_info, patch_type, new_patch);
10266
10267 // Fetch stream info of associated mix for playback or capture patches
10268 if (p_info->patch_type == PATCH_PLAYBACK ||
10269 p_info->patch_type == PATCH_CAPTURE) {
10270 s_info = hashmapGet(adev->io_streams_map, (void *) (intptr_t) io_handle);
10271 if (s_info == NULL) {
10272 ALOGE("%s: Failed to obtain stream info", __func__);
10273 if (new_patch)
10274 free(p_info);
10275 pthread_mutex_unlock(&adev->lock);
10276 ret = -EINVAL;
10277 goto done;
10278 }
10279 ALOGV("%s: Fetched stream info with io_handle %d", __func__, io_handle);
10280 s_info->patch_handle = *handle;
10281 stream = s_info->stream;
10282 }
10283 pthread_mutex_unlock(&adev->lock);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010284
10285 // Update routing for stream
10286 if (stream != NULL) {
10287 if (p_info->patch_type == PATCH_PLAYBACK)
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010288 ret = route_output_stream((struct stream_out *) stream, &devices);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010289 else if (p_info->patch_type == PATCH_CAPTURE)
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010290 ret = route_input_stream((struct stream_in *) stream, &devices, input_source);
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010291 if (ret < 0) {
10292 pthread_mutex_lock(&adev->lock);
10293 s_info->patch_handle = AUDIO_PATCH_HANDLE_NONE;
10294 if (new_patch)
10295 free(p_info);
10296 pthread_mutex_unlock(&adev->lock);
10297 ALOGE("%s: Stream routing failed for io_handle %d", __func__, io_handle);
10298 goto done;
10299 }
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010300 }
10301
10302 // Add new patch to patch map
10303 if (!ret && new_patch) {
10304 pthread_mutex_lock(&adev->lock);
10305 hashmapPut(adev->patch_map, (void *) (intptr_t) *handle, (void *) p_info);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010306 ALOGD("%s: Added a new patch with handle %d", __func__, *handle);
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010307 pthread_mutex_unlock(&adev->lock);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010308 }
10309
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010310done:
10311 audio_extn_hw_loopback_create_audio_patch(dev,
Derek Chenf939fb72018-11-13 13:34:41 -080010312 num_sources,
10313 sources,
10314 num_sinks,
10315 sinks,
10316 handle);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010317 audio_extn_auto_hal_create_audio_patch(dev,
Derek Chenf939fb72018-11-13 13:34:41 -080010318 num_sources,
10319 sources,
10320 num_sinks,
10321 sinks,
10322 handle);
10323 return ret;
Siddartha Shaik31b530e2017-05-19 15:26:33 +053010324}
10325
10326int adev_release_audio_patch(struct audio_hw_device *dev,
10327 audio_patch_handle_t handle)
10328{
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010329 struct audio_device *adev = (struct audio_device *) dev;
10330 int ret = 0;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010331 audio_source_t input_source = AUDIO_SOURCE_DEFAULT;
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010332 struct audio_stream *stream = NULL;
Derek Chenf939fb72018-11-13 13:34:41 -080010333
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010334 if (handle == AUDIO_PATCH_HANDLE_NONE) {
10335 ALOGE("%s: Invalid patch handle %d", __func__, handle);
10336 ret = -EINVAL;
10337 goto done;
10338 }
10339
10340 ALOGD("%s: Remove patch with handle %d", __func__, handle);
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010341 pthread_mutex_lock(&adev->lock);
10342 struct audio_patch_info *p_info = fetch_patch_info_l(adev, handle);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010343 if (p_info == NULL) {
10344 ALOGE("%s: Patch info not found with handle %d", __func__, handle);
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010345 pthread_mutex_unlock(&adev->lock);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010346 ret = -EINVAL;
10347 goto done;
10348 }
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010349 struct audio_patch *patch = p_info->patch;
10350 if (patch == NULL) {
10351 ALOGE("%s: Patch not found for handle %d", __func__, handle);
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010352 pthread_mutex_unlock(&adev->lock);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010353 ret = -EINVAL;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010354 goto done;
10355 }
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010356 audio_io_handle_t io_handle = AUDIO_IO_HANDLE_NONE;
10357 switch (patch->sources[0].type) {
10358 case AUDIO_PORT_TYPE_MIX:
10359 io_handle = patch->sources[0].ext.mix.handle;
10360 break;
10361 case AUDIO_PORT_TYPE_DEVICE:
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010362 if (p_info->patch_type == PATCH_CAPTURE)
10363 io_handle = patch->sinks[0].ext.mix.handle;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010364 break;
10365 case AUDIO_PORT_TYPE_SESSION:
10366 case AUDIO_PORT_TYPE_NONE:
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010367 pthread_mutex_unlock(&adev->lock);
10368 ret = -EINVAL;
10369 goto done;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010370 }
10371
10372 // Remove patch and reset patch handle in stream info
Aniket Kumar Latace2b9fa2020-03-10 15:04:15 -070010373 patch_type_t patch_type = p_info->patch_type;
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010374 patch_map_remove_l(adev, handle);
Aniket Kumar Latace2b9fa2020-03-10 15:04:15 -070010375 if (patch_type == PATCH_PLAYBACK ||
10376 patch_type == PATCH_CAPTURE) {
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010377 struct audio_stream_info *s_info =
10378 hashmapGet(adev->io_streams_map, (void *) (intptr_t) io_handle);
10379 if (s_info == NULL) {
10380 ALOGE("%s: stream for io_handle %d is not available", __func__, io_handle);
10381 pthread_mutex_unlock(&adev->lock);
10382 goto done;
10383 }
10384 s_info->patch_handle = AUDIO_PATCH_HANDLE_NONE;
10385 stream = s_info->stream;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010386 }
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010387 pthread_mutex_unlock(&adev->lock);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010388
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010389 if (stream != NULL) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010390 struct listnode devices;
10391 list_init(&devices);
Aniket Kumar Latace2b9fa2020-03-10 15:04:15 -070010392 if (patch_type == PATCH_PLAYBACK)
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010393 ret = route_output_stream((struct stream_out *) stream, &devices);
Aniket Kumar Latace2b9fa2020-03-10 15:04:15 -070010394 else if (patch_type == PATCH_CAPTURE)
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010395 ret = route_input_stream((struct stream_in *) stream, &devices, input_source);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010396 }
10397
10398 if (ret < 0)
10399 ALOGW("%s: Stream routing failed for io_handle %d", __func__, io_handle);
10400
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010401done:
10402 audio_extn_hw_loopback_release_audio_patch(dev, handle);
10403 audio_extn_auto_hal_release_audio_patch(dev, handle);
10404
10405 ALOGV("%s: Successfully released patch %d", __func__, handle);
Derek Chenf939fb72018-11-13 13:34:41 -080010406 return ret;
Siddartha Shaik31b530e2017-05-19 15:26:33 +053010407}
10408
10409int adev_get_audio_port(struct audio_hw_device *dev, struct audio_port *config)
10410{
Derek Chenf13dd492018-11-13 14:53:51 -080010411 int ret = 0;
10412
10413 ret = audio_extn_hw_loopback_get_audio_port(dev, config);
10414 ret |= audio_extn_auto_hal_get_audio_port(dev, config);
10415 return ret;
Siddartha Shaik31b530e2017-05-19 15:26:33 +053010416}
10417
10418int adev_set_audio_port_config(struct audio_hw_device *dev,
10419 const struct audio_port_config *config)
10420{
Derek Chenf13dd492018-11-13 14:53:51 -080010421 int ret = 0;
10422
10423 ret = audio_extn_hw_loopback_set_audio_port_config(dev, config);
10424 ret |= audio_extn_auto_hal_set_audio_port_config(dev, config);
10425 return ret;
Siddartha Shaik31b530e2017-05-19 15:26:33 +053010426}
10427
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -070010428static int adev_dump(const audio_hw_device_t *device __unused,
10429 int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010430{
10431 return 0;
10432}
10433
10434static int adev_close(hw_device_t *device)
10435{
Aalique Grahame22e49102018-12-18 14:23:57 -080010436 size_t i;
Preetam Singh Ranawatacc64542019-06-24 15:11:28 +053010437 struct audio_device *adev_temp = (struct audio_device *)device;
Kiran Kandi910e1862013-10-29 13:29:42 -070010438
Preetam Singh Ranawatacc64542019-06-24 15:11:28 +053010439 if (!adev_temp)
Kiran Kandi910e1862013-10-29 13:29:42 -070010440 return 0;
10441
10442 pthread_mutex_lock(&adev_init_lock);
10443
10444 if ((--audio_device_ref_count) == 0) {
Sujin Panicker390724d2019-04-26 10:43:36 +053010445 if (audio_extn_spkr_prot_is_enabled())
10446 audio_extn_spkr_prot_deinit();
Jaideep Sharmaa2b49672019-09-10 20:37:03 +053010447 audio_extn_battery_properties_listener_deinit();
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053010448 audio_extn_snd_mon_unregister_listener(adev);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -070010449 audio_extn_sound_trigger_deinit(adev);
Kiran Kandide144c82013-11-20 15:58:32 -080010450 audio_extn_listen_deinit(adev);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -080010451 audio_extn_qdsp_deinit();
Aalique Grahame22e49102018-12-18 14:23:57 -080010452 audio_extn_extspk_deinit(adev->extspk);
Dhananjay Kumard6d32152016-10-13 16:11:03 +053010453 audio_extn_utils_release_streams_cfg_lists(
10454 &adev->streams_output_cfg_list,
10455 &adev->streams_input_cfg_list);
Sidipotu Ashokd2f10ba2019-05-06 15:41:56 +053010456 if (audio_extn_qap_is_enabled())
10457 audio_extn_qap_deinit();
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +053010458 if (audio_extn_qaf_is_enabled())
10459 audio_extn_qaf_deinit();
Kiran Kandi910e1862013-10-29 13:29:42 -070010460 audio_route_free(adev->audio_route);
Weiyin Jiangfa65d3e2019-03-05 23:39:45 +080010461 audio_extn_gef_deinit(adev);
Kiran Kandi910e1862013-10-29 13:29:42 -070010462 free(adev->snd_dev_ref_cnt);
10463 platform_deinit(adev->platform);
Aalique Grahame22e49102018-12-18 14:23:57 -080010464 for (i = 0; i < ARRAY_SIZE(adev->use_case_table); ++i) {
10465 pcm_params_free(adev->use_case_table[i]);
10466 }
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -070010467 if (adev->adm_deinit)
10468 adev->adm_deinit(adev->adm_data);
Dhananjay Kumaree4d2002016-10-25 18:02:58 +053010469 qahwi_deinit(device);
Ben Rombergerd771a7c2017-02-22 18:05:17 -080010470 audio_extn_adsp_hdlr_deinit();
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053010471 audio_extn_snd_mon_deinit();
Siddartha Shaik44dd7702017-06-14 12:13:25 +053010472 audio_extn_hw_loopback_deinit(adev);
Garmond Leunge2433c32017-09-28 21:51:22 -070010473 audio_extn_ffv_deinit();
Satish Babu Patakokilac3c5d432017-07-04 22:48:59 +053010474 if (adev->device_cfg_params) {
10475 free(adev->device_cfg_params);
10476 adev->device_cfg_params = NULL;
10477 }
Derek Chend2530072014-11-24 12:39:14 -080010478 if(adev->ext_hw_plugin)
10479 audio_extn_ext_hw_plugin_deinit(adev->ext_hw_plugin);
Derek Chenae7b0342019-02-08 15:17:04 -080010480 audio_extn_auto_hal_deinit();
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010481 free_map(adev->patch_map);
10482 free_map(adev->io_streams_map);
Kiran Kandi910e1862013-10-29 13:29:42 -070010483 free(device);
10484 adev = NULL;
10485 }
10486 pthread_mutex_unlock(&adev_init_lock);
Vatsal Buchac09ae062018-11-14 13:25:08 +053010487 enable_gcov();
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010488 return 0;
10489}
10490
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -070010491/* This returns 1 if the input parameter looks at all plausible as a low latency period size,
10492 * or 0 otherwise. A return value of 1 doesn't mean the value is guaranteed to work,
10493 * just that it _might_ work.
10494 */
10495static int period_size_is_plausible_for_low_latency(int period_size)
10496{
10497 switch (period_size) {
10498 case 160:
Ravi Kumar Alamanda474de5a2015-06-25 20:08:01 -070010499 case 192:
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -070010500 case 240:
10501 case 320:
10502 case 480:
10503 return 1;
10504 default:
10505 return 0;
10506 }
10507}
10508
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053010509static void adev_snd_mon_cb(void *cookie, struct str_parms *parms)
10510{
10511 bool is_snd_card_status = false;
10512 bool is_ext_device_status = false;
10513 char value[32];
10514 int card = -1;
10515 card_status_t status;
10516
10517 if (cookie != adev || !parms)
10518 return;
10519
10520 if (!parse_snd_card_status(parms, &card, &status)) {
10521 is_snd_card_status = true;
10522 } else if (0 < str_parms_get_str(parms, "ext_audio_device", value, sizeof(value))) {
10523 is_ext_device_status = true;
10524 } else {
10525 // not a valid event
10526 return;
10527 }
10528
10529 pthread_mutex_lock(&adev->lock);
10530 if (card == adev->snd_card || is_ext_device_status) {
10531 if (is_snd_card_status && adev->card_status != status) {
Jaideep Sharmacb402512020-09-24 17:51:07 +053010532 ALOGD("%s card_status %d", __func__, status);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053010533 adev->card_status = status;
10534 platform_snd_card_update(adev->platform, status);
10535 audio_extn_fm_set_parameters(adev, parms);
Derek Chend6f371d2019-03-01 13:45:58 -080010536 audio_extn_auto_hal_set_parameters(adev, parms);
Zhou Song4f43e352019-07-02 00:32:23 +080010537 if (status == CARD_STATUS_OFFLINE)
10538 audio_extn_sco_reset_configuration();
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053010539 } else if (is_ext_device_status) {
10540 platform_set_parameters(adev->platform, parms);
10541 }
10542 }
10543 pthread_mutex_unlock(&adev->lock);
10544 return;
10545}
10546
Weiyin Jiang280ea742020-09-08 20:28:22 +080010547/* adev lock held */
10548int check_a2dp_restore_l(struct audio_device *adev, struct stream_out *out, bool restore)
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010549{
10550 struct audio_usecase *uc_info;
Zhou Song407e5aa2020-12-27 19:13:04 +080010551 struct audio_usecase *usecase;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010552 struct listnode devices;
Zhou Song407e5aa2020-12-27 19:13:04 +080010553 struct listnode *node;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010554
10555 uc_info = get_usecase_from_list(adev, out->usecase);
10556 if (uc_info == NULL) {
10557 ALOGE("%s: Could not find the usecase (%d) in the list",
10558 __func__, out->usecase);
10559 return -EINVAL;
10560 }
Aniket Kumar Lata9c2fd892020-01-22 22:20:00 -080010561 list_init(&devices);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010562
Zhou Songbaddf9f2020-11-20 13:57:39 +080010563 ALOGD("%s: enter: usecase(%d: %s), a2dp muted %d", __func__,
10564 out->usecase, use_case_table[out->usecase], out->a2dp_muted);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010565
10566 if (restore) {
Weiyin Jiang280ea742020-09-08 20:28:22 +080010567 pthread_mutex_lock(&out->latch_lock);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010568 // restore A2DP device for active usecases and unmute if required
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010569 if (is_a2dp_out_device_type(&out->device_list)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010570 ALOGD("%s: restoring A2dp and unmuting stream", __func__);
Zhou Songe5225132019-09-26 15:33:36 +080010571 if (uc_info->out_snd_device != SND_DEVICE_OUT_BT_A2DP)
10572 select_devices(adev, uc_info->id);
Zhou Songbaddf9f2020-11-20 13:57:39 +080010573
10574 if (is_offload_usecase(out->usecase)) {
10575 if (uc_info->out_snd_device == SND_DEVICE_OUT_BT_A2DP)
Weiyin Jiang280ea742020-09-08 20:28:22 +080010576 out_set_compr_volume(&out->stream, out->volume_l, out->volume_r);
Zhou Songbaddf9f2020-11-20 13:57:39 +080010577 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP) {
10578 out_set_voip_volume(&out->stream, out->volume_l, out->volume_r);
10579 } else {
10580 out_set_pcm_volume(&out->stream, out->volume_l, out->volume_r);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010581 }
Zhou Songbaddf9f2020-11-20 13:57:39 +080010582 out->a2dp_muted = false;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010583 }
Weiyin Jiang280ea742020-09-08 20:28:22 +080010584 pthread_mutex_unlock(&out->latch_lock);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010585 } else {
Weiyin Jiang280ea742020-09-08 20:28:22 +080010586 pthread_mutex_lock(&out->latch_lock);
Zhou Songbaddf9f2020-11-20 13:57:39 +080010587 // mute stream and switch to speaker if suspended
10588 if (!out->a2dp_muted && !out->standby) {
Zhou Songbaddf9f2020-11-20 13:57:39 +080010589 assign_devices(&devices, &out->device_list);
10590 reassign_device_list(&out->device_list, AUDIO_DEVICE_OUT_SPEAKER, "");
Zhou Song407e5aa2020-12-27 19:13:04 +080010591 list_for_each(node, &adev->usecase_list) {
10592 usecase = node_to_item(node, struct audio_usecase, list);
Zhou Songe63b6fe2021-04-15 13:54:46 +080010593 if ((usecase->type != PCM_CAPTURE) && (usecase != uc_info) &&
10594 !is_a2dp_out_device_type(&usecase->stream.out->device_list) &&
Zhou Songcf77af02021-05-14 18:21:14 +080010595 !is_sco_out_device_type(&usecase->stream.out->device_list) &&
Zhou Songe63b6fe2021-04-15 13:54:46 +080010596 platform_check_backends_match(SND_DEVICE_OUT_SPEAKER,
10597 usecase->out_snd_device)) {
Zhou Song407e5aa2020-12-27 19:13:04 +080010598 assign_devices(&out->device_list, &usecase->stream.out->device_list);
10599 break;
10600 }
10601 }
Zhou Songcf77af02021-05-14 18:21:14 +080010602 if ((is_a2dp_out_device_type(&devices) && list_length(&devices) == 1) ||
10603 (uc_info->out_snd_device == SND_DEVICE_OUT_BT_A2DP)) {
Zhou Song8edbbdb2021-01-14 16:48:03 +080010604 out->a2dp_muted = true;
10605 if (is_offload_usecase(out->usecase)) {
10606 if (out->offload_state == OFFLOAD_STATE_PLAYING)
10607 compress_pause(out->compr);
10608 out_set_compr_volume(&out->stream, (float)0, (float)0);
Zhou Song8edbbdb2021-01-14 16:48:03 +080010609 } else {
Weiyin Jiang906db3c2021-03-02 13:17:04 +080010610 if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP)
10611 out_set_voip_volume(&out->stream, (float)0, (float)0);
10612 else
10613 out_set_pcm_volume(&out->stream, (float)0, (float)0);
10614
Zhou Song8edbbdb2021-01-14 16:48:03 +080010615 /* wait for stale pcm drained before switching to speaker */
10616 uint32_t latency =
10617 (out->config.period_count * out->config.period_size * 1000) /
10618 (out->config.rate);
10619 usleep(latency * 1000);
10620 }
Zhou Songbaddf9f2020-11-20 13:57:39 +080010621 }
10622 select_devices(adev, out->usecase);
Zhou Song8edbbdb2021-01-14 16:48:03 +080010623 ALOGD("%s: switched to device:%s and stream muted:%d", __func__,
10624 platform_get_snd_device_name(uc_info->out_snd_device), out->a2dp_muted);
Zhou Songbaddf9f2020-11-20 13:57:39 +080010625 if (is_offload_usecase(out->usecase)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010626 if (out->offload_state == OFFLOAD_STATE_PLAYING)
10627 compress_resume(out->compr);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010628 }
Zhou Songbaddf9f2020-11-20 13:57:39 +080010629 assign_devices(&out->device_list, &devices);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010630 }
Weiyin Jiang280ea742020-09-08 20:28:22 +080010631 pthread_mutex_unlock(&out->latch_lock);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010632 }
10633 ALOGV("%s: exit", __func__);
10634 return 0;
10635}
10636
Haynes Mathew George01156f92018-04-13 15:29:54 -070010637void adev_on_battery_status_changed(bool charging)
10638{
10639 pthread_mutex_lock(&adev->lock);
10640 ALOGI("%s: battery status changed to %scharging", __func__, charging ? "" : "not ");
10641 adev->is_charging = charging;
Zhou Songc1088ea2018-06-12 00:17:29 +080010642 audio_extn_sound_trigger_update_battery_status(charging);
Haynes Mathew George01156f92018-04-13 15:29:54 -070010643 pthread_mutex_unlock(&adev->lock);
10644}
10645
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010646static int adev_open(const hw_module_t *module, const char *name,
10647 hw_device_t **device)
10648{
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +053010649 int ret;
Derek Chenf939fb72018-11-13 13:34:41 -080010650 char value[PROPERTY_VALUE_MAX] = {0};
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +053010651 char mixer_ctl_name[128] = {0};
10652 struct mixer_ctl *ctl = NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +053010653
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -080010654 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010655 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
10656
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070010657 pthread_mutex_lock(&adev_init_lock);
Kiran Kandi910e1862013-10-29 13:29:42 -070010658 if (audio_device_ref_count != 0){
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070010659 *device = &adev->device.common;
Kiran Kandi910e1862013-10-29 13:29:42 -070010660 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070010661 ALOGD("%s: returning existing instance of adev", __func__);
10662 ALOGD("%s: exit", __func__);
10663 pthread_mutex_unlock(&adev_init_lock);
10664 return 0;
10665 }
10666
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010667 adev = calloc(1, sizeof(struct audio_device));
10668
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -070010669 if (!adev) {
10670 pthread_mutex_unlock(&adev_init_lock);
10671 return -ENOMEM;
10672 }
10673
Ravi Kumar Alamanda40703102014-04-24 10:34:41 -070010674 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
10675
Weiyin Jiange6ce6312019-01-28 18:28:22 +080010676 // register audio ext hidl at the earliest
10677 audio_extn_hidl_init();
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053010678#ifdef DYNAMIC_LOG_ENABLED
10679 register_for_dynamic_logging("hal");
10680#endif
10681
Derek Chenf939fb72018-11-13 13:34:41 -080010682 /* default audio HAL major version */
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010683 uint32_t maj_version = 3;
Derek Chenf939fb72018-11-13 13:34:41 -080010684 if(property_get("vendor.audio.hal.maj.version", value, NULL))
10685 maj_version = atoi(value);
10686
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010687 adev->device.common.tag = HARDWARE_DEVICE_TAG;
Derek Chenf939fb72018-11-13 13:34:41 -080010688 adev->device.common.version = HARDWARE_DEVICE_API_VERSION(maj_version, 0);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010689 adev->device.common.module = (struct hw_module_t *)module;
10690 adev->device.common.close = adev_close;
10691
10692 adev->device.init_check = adev_init_check;
10693 adev->device.set_voice_volume = adev_set_voice_volume;
10694 adev->device.set_master_volume = adev_set_master_volume;
10695 adev->device.get_master_volume = adev_get_master_volume;
10696 adev->device.set_master_mute = adev_set_master_mute;
10697 adev->device.get_master_mute = adev_get_master_mute;
10698 adev->device.set_mode = adev_set_mode;
10699 adev->device.set_mic_mute = adev_set_mic_mute;
10700 adev->device.get_mic_mute = adev_get_mic_mute;
10701 adev->device.set_parameters = adev_set_parameters;
10702 adev->device.get_parameters = adev_get_parameters;
10703 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
10704 adev->device.open_output_stream = adev_open_output_stream;
10705 adev->device.close_output_stream = adev_close_output_stream;
10706 adev->device.open_input_stream = adev_open_input_stream;
10707 adev->device.close_input_stream = adev_close_input_stream;
Siddartha Shaik31b530e2017-05-19 15:26:33 +053010708 adev->device.create_audio_patch = adev_create_audio_patch;
10709 adev->device.release_audio_patch = adev_release_audio_patch;
10710 adev->device.get_audio_port = adev_get_audio_port;
10711 adev->device.set_audio_port_config = adev_set_audio_port_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010712 adev->device.dump = adev_dump;
Naresh Tannirudcb47c52018-06-25 16:23:32 +053010713 adev->device.get_microphones = adev_get_microphones;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010714
10715 /* Set the default route before the PCM stream is opened */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010716 adev->mode = AUDIO_MODE_NORMAL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -080010717 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010718 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010719 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -080010720 adev->acdb_settings = TTY_MODE_OFF;
vivek mehta344576a2016-04-12 18:56:03 -070010721 adev->allow_afe_proxy_usage = true;
Ashish Jain1b9b30c2017-05-18 20:57:40 +053010722 adev->bt_sco_on = false;
Eric Laurent07eeafd2013-10-06 12:52:49 -070010723 /* adev->cur_hdmi_channels = 0; by calloc() */
Eric Laurentb23d5282013-05-14 15:27:20 -070010724 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
vivek mehtaae1018c2019-05-09 12:19:57 -070010725 /* Init audio and voice feature */
10726 audio_extn_feature_init();
10727 voice_extn_feature_init();
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -070010728 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -080010729 list_init(&adev->usecase_list);
Derek Chenf939fb72018-11-13 13:34:41 -080010730 list_init(&adev->active_inputs_list);
10731 list_init(&adev->active_outputs_list);
Rahul Sharma99770982019-03-06 17:05:26 +053010732 list_init(&adev->audio_patch_record_list);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010733 adev->io_streams_map = hashmapCreate(AUDIO_IO_PORTS_MAX, audio_extn_utils_hash_fn,
10734 audio_extn_utils_hash_eq);
10735 if (!adev->io_streams_map) {
10736 ALOGE("%s: Could not create io streams map", __func__);
10737 ret = -ENOMEM;
10738 goto adev_open_err;
10739 }
10740 adev->patch_map = hashmapCreate(AUDIO_PATCH_PORTS_MAX, audio_extn_utils_hash_fn,
10741 audio_extn_utils_hash_eq);
10742 if (!adev->patch_map) {
10743 ALOGE("%s: Could not create audio patch map", __func__);
10744 ret = -ENOMEM;
10745 goto adev_open_err;
10746 }
Krishnankutty Kolathappilly0b2de1c2014-02-14 14:45:49 -080010747 adev->cur_wfd_channels = 2;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -070010748 adev->offload_usecases_state = 0;
Samyak Jain15fda662018-12-18 16:40:52 +053010749 adev->pcm_record_uc_state = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +053010750 adev->is_channel_status_set = false;
Sudheer Papothifa9d2282015-09-17 01:53:25 +053010751 adev->perf_lock_opts[0] = 0x101;
10752 adev->perf_lock_opts[1] = 0x20E;
10753 adev->perf_lock_opts_size = 2;
Xiaojun Sang785b5da2017-08-03 15:52:29 +080010754 adev->dsp_bit_width_enforce_mode = 0;
Aalique Grahame552b0832019-03-11 10:16:38 -070010755 adev->enable_hfp = false;
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +053010756 adev->use_old_pspd_mix_ctrl = false;
Aniket Kumar Lata60586a92019-05-22 22:18:55 -070010757 adev->adm_routing_changed = false;
Revathi Uddarajub26e3932020-06-10 14:51:02 +053010758 adev->a2dp_started = false;
Zhou Song503196b2021-07-23 17:31:05 +080010759 adev->ha_proxy_enable = false;
Naresh Tanniru4c630392014-05-12 01:05:52 +053010760
Zhou Song68ebc352019-12-05 17:11:15 +080010761 audio_extn_perf_lock_init();
10762
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010763 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -070010764 adev->platform = platform_init(adev);
10765 if (!adev->platform) {
Eric Laurentb23d5282013-05-14 15:27:20 -070010766 ALOGE("%s: Failed to init platform data, aborting.", __func__);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010767 ret = -EINVAL;
10768 goto adev_open_err;
Eric Laurentb23d5282013-05-14 15:27:20 -070010769 }
Eric Laurentc4aef752013-09-12 17:45:53 -070010770
Aalique Grahame22e49102018-12-18 14:23:57 -080010771 adev->extspk = audio_extn_extspk_init(adev);
Sidipotu Ashokd2f10ba2019-05-06 15:41:56 +053010772 if (audio_extn_qap_is_enabled()) {
10773 ret = audio_extn_qap_init(adev);
10774 if (ret < 0) {
Sidipotu Ashokd2f10ba2019-05-06 15:41:56 +053010775 ALOGE("%s: Failed to init platform data, aborting.", __func__);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010776 goto adev_open_err;
Sidipotu Ashokd2f10ba2019-05-06 15:41:56 +053010777 }
10778 adev->device.open_output_stream = audio_extn_qap_open_output_stream;
10779 adev->device.close_output_stream = audio_extn_qap_close_output_stream;
10780 }
Aalique Grahame22e49102018-12-18 14:23:57 -080010781
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +053010782 if (audio_extn_qaf_is_enabled()) {
10783 ret = audio_extn_qaf_init(adev);
10784 if (ret < 0) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +053010785 ALOGE("%s: Failed to init platform data, aborting.", __func__);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010786 goto adev_open_err;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +053010787 }
10788
10789 adev->device.open_output_stream = audio_extn_qaf_open_output_stream;
10790 adev->device.close_output_stream = audio_extn_qaf_close_output_stream;
10791 }
10792
Derek Chenae7b0342019-02-08 15:17:04 -080010793 audio_extn_auto_hal_init(adev);
Derek Chend2530072014-11-24 12:39:14 -080010794 adev->ext_hw_plugin = audio_extn_ext_hw_plugin_init(adev);
10795
Eric Laurentc4aef752013-09-12 17:45:53 -070010796 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
10797 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
10798 if (adev->visualizer_lib == NULL) {
10799 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
10800 } else {
10801 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
10802 adev->visualizer_start_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -080010803 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -070010804 "visualizer_hal_start_output");
10805 adev->visualizer_stop_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -080010806 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -070010807 "visualizer_hal_stop_output");
10808 }
10809 }
Dhanalakshmi Siddani21be3ac2016-12-29 14:31:08 +053010810 audio_extn_init(adev);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -080010811 voice_extn_init(adev);
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -080010812 audio_extn_listen_init(adev, adev->snd_card);
Weiyin Jiangaa80acd2016-09-21 16:42:11 +080010813 audio_extn_gef_init(adev);
Siddartha Shaik44dd7702017-06-14 12:13:25 +053010814 audio_extn_hw_loopback_init(adev);
Garmond Leunge2433c32017-09-28 21:51:22 -070010815 audio_extn_ffv_init(adev);
Eric Laurentc4aef752013-09-12 17:45:53 -070010816
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -080010817 if (access(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, R_OK) == 0) {
10818 adev->offload_effects_lib = dlopen(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, RTLD_NOW);
10819 if (adev->offload_effects_lib == NULL) {
10820 ALOGE("%s: DLOPEN failed for %s", __func__,
10821 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
10822 } else {
10823 ALOGV("%s: DLOPEN successful for %s", __func__,
10824 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
10825 adev->offload_effects_start_output =
Ashish Jain5106d362016-05-11 19:23:33 +053010826 (int (*)(audio_io_handle_t, int, struct mixer *))dlsym(adev->offload_effects_lib,
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -080010827 "offload_effects_bundle_hal_start_output");
10828 adev->offload_effects_stop_output =
10829 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
10830 "offload_effects_bundle_hal_stop_output");
Jitendra Naruka1b6513f2014-11-22 19:34:13 -080010831 adev->offload_effects_set_hpx_state =
10832 (int (*)(bool))dlsym(adev->offload_effects_lib,
10833 "offload_effects_bundle_set_hpx_state");
Dhananjay Kumard68883d2015-09-04 13:39:26 +053010834 adev->offload_effects_get_parameters =
10835 (void (*)(struct str_parms *, struct str_parms *))
10836 dlsym(adev->offload_effects_lib,
10837 "offload_effects_bundle_get_parameters");
10838 adev->offload_effects_set_parameters =
10839 (void (*)(struct str_parms *))dlsym(adev->offload_effects_lib,
10840 "offload_effects_bundle_set_parameters");
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -080010841 }
10842 }
10843
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -070010844 if (access(ADM_LIBRARY_PATH, R_OK) == 0) {
10845 adev->adm_lib = dlopen(ADM_LIBRARY_PATH, RTLD_NOW);
10846 if (adev->adm_lib == NULL) {
10847 ALOGE("%s: DLOPEN failed for %s", __func__, ADM_LIBRARY_PATH);
10848 } else {
10849 ALOGV("%s: DLOPEN successful for %s", __func__, ADM_LIBRARY_PATH);
10850 adev->adm_init = (adm_init_t)
10851 dlsym(adev->adm_lib, "adm_init");
10852 adev->adm_deinit = (adm_deinit_t)
10853 dlsym(adev->adm_lib, "adm_deinit");
10854 adev->adm_register_input_stream = (adm_register_input_stream_t)
10855 dlsym(adev->adm_lib, "adm_register_input_stream");
10856 adev->adm_register_output_stream = (adm_register_output_stream_t)
10857 dlsym(adev->adm_lib, "adm_register_output_stream");
10858 adev->adm_deregister_stream = (adm_deregister_stream_t)
10859 dlsym(adev->adm_lib, "adm_deregister_stream");
10860 adev->adm_request_focus = (adm_request_focus_t)
10861 dlsym(adev->adm_lib, "adm_request_focus");
10862 adev->adm_abandon_focus = (adm_abandon_focus_t)
10863 dlsym(adev->adm_lib, "adm_abandon_focus");
Haynes Mathew George5beddd42016-06-27 18:33:40 -070010864 adev->adm_set_config = (adm_set_config_t)
10865 dlsym(adev->adm_lib, "adm_set_config");
10866 adev->adm_request_focus_v2 = (adm_request_focus_v2_t)
10867 dlsym(adev->adm_lib, "adm_request_focus_v2");
10868 adev->adm_is_noirq_avail = (adm_is_noirq_avail_t)
10869 dlsym(adev->adm_lib, "adm_is_noirq_avail");
10870 adev->adm_on_routing_change = (adm_on_routing_change_t)
10871 dlsym(adev->adm_lib, "adm_on_routing_change");
Aniket Kumar Lata60586a92019-05-22 22:18:55 -070010872 adev->adm_request_focus_v2_1 = (adm_request_focus_v2_1_t)
10873 dlsym(adev->adm_lib, "adm_request_focus_v2_1");
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -070010874 }
10875 }
10876
Aalique Grahame22e49102018-12-18 14:23:57 -080010877 adev->enable_voicerx = false;
Mingming Yin514a8bc2014-07-29 15:22:21 -070010878 adev->bt_wb_speech_enabled = false;
Zhou Song12c29502019-03-16 10:37:18 +080010879 adev->swb_speech_mode = SPEECH_MODE_INVALID;
Kunlei Zhanga3c4cb02021-03-05 16:03:54 +080010880 adev->fluence_nn_usecase_id = USECASE_INVALID;
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -080010881 //initialize this to false for now,
10882 //this will be set to true through set param
10883 adev->vr_audio_mode_enabled = false;
Mingming Yin514a8bc2014-07-29 15:22:21 -070010884
Pradnya Chaphekar8a9dcd82014-09-09 09:49:10 -070010885 audio_extn_ds2_enable(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010886 *device = &adev->device.common;
Aalique Grahame22e49102018-12-18 14:23:57 -080010887
10888 if (k_enable_extended_precision)
10889 adev_verify_devices(adev);
10890
Xiaojun Sang785b5da2017-08-03 15:52:29 +080010891 adev->dsp_bit_width_enforce_mode =
10892 adev_init_dsp_bit_width_enforce_mode(adev->mixer);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010893
Dhananjay Kumard6d32152016-10-13 16:11:03 +053010894 audio_extn_utils_update_streams_cfg_lists(adev->platform, adev->mixer,
10895 &adev->streams_output_cfg_list,
10896 &adev->streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070010897
Kiran Kandi910e1862013-10-29 13:29:42 -070010898 audio_device_ref_count++;
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -070010899
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -070010900 int trial;
Manisha Agarwalc75a0202019-12-06 18:48:25 +053010901 if (property_get("vendor.audio_hal.period_size", value, NULL) > 0) {
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -070010902 trial = atoi(value);
10903 if (period_size_is_plausible_for_low_latency(trial)) {
10904 pcm_config_low_latency.period_size = trial;
10905 pcm_config_low_latency.start_threshold = trial / 4;
10906 pcm_config_low_latency.avail_min = trial / 4;
10907 configured_low_latency_capture_period_size = trial;
10908 }
10909 }
ronghuiz93177262021-04-21 19:58:13 +080010910 if (property_get("vendor.audio_hal.in_period_size", value, NULL) > 0) {
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -070010911 trial = atoi(value);
10912 if (period_size_is_plausible_for_low_latency(trial)) {
10913 configured_low_latency_capture_period_size = trial;
10914 }
10915 }
10916
Vignesh Kulothungan7d374312018-02-21 17:12:00 -080010917 adev->mic_break_enabled = property_get_bool("vendor.audio.mic_break", false);
10918
Eric Laurent4b084132018-10-19 17:33:43 -070010919 adev->camera_orientation = CAMERA_DEFAULT;
10920
Manisha Agarwalc75a0202019-12-06 18:48:25 +053010921 if (property_get("vendor.audio_hal.period_multiplier",value,NULL) > 0) {
Haynes Mathew George5beddd42016-06-27 18:33:40 -070010922 af_period_multiplier = atoi(value);
10923 if (af_period_multiplier < 0)
10924 af_period_multiplier = 2;
10925 else if (af_period_multiplier > 4)
10926 af_period_multiplier = 4;
10927
10928 ALOGV("new period_multiplier = %d", af_period_multiplier);
10929 }
10930
Arun Mirpurib1bec9c2019-01-29 16:42:45 -080010931 audio_extn_qdsp_init(adev->platform);
Aalique Grahame22e49102018-12-18 14:23:57 -080010932
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -070010933 adev->multi_offload_enable = property_get_bool("vendor.audio.offload.multiple.enabled", false);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070010934 pthread_mutex_unlock(&adev_init_lock);
10935
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -070010936 if (adev->adm_init)
10937 adev->adm_data = adev->adm_init();
10938
Dhananjay Kumaree4d2002016-10-25 18:02:58 +053010939 qahwi_init(*device);
Ben Rombergerd771a7c2017-02-22 18:05:17 -080010940 audio_extn_adsp_hdlr_init(adev->mixer);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053010941
10942 audio_extn_snd_mon_init();
10943 pthread_mutex_lock(&adev->lock);
10944 audio_extn_snd_mon_register_listener(adev, adev_snd_mon_cb);
10945 adev->card_status = CARD_STATUS_ONLINE;
Haynes Mathew George01156f92018-04-13 15:29:54 -070010946 audio_extn_battery_properties_listener_init(adev_on_battery_status_changed);
10947 /*
10948 * if the battery state callback happens before charging can be queried,
10949 * it will be guarded with the adev->lock held in the cb function and so
10950 * the callback value will reflect the latest state
10951 */
10952 adev->is_charging = audio_extn_battery_properties_is_charging();
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053010953 audio_extn_sound_trigger_init(adev); /* dependent on snd_mon_init() */
Zhou Songc1088ea2018-06-12 00:17:29 +080010954 audio_extn_sound_trigger_update_battery_status(adev->is_charging);
justinweng20fb6d82019-02-21 18:49:00 -070010955 audio_extn_audiozoom_init();
Zhou Songc1088ea2018-06-12 00:17:29 +080010956 pthread_mutex_unlock(&adev->lock);
Satish Babu Patakokilac3c5d432017-07-04 22:48:59 +053010957 /* Allocate memory for Device config params */
10958 adev->device_cfg_params = (struct audio_device_config_param*)
10959 calloc(platform_get_max_codec_backend(),
10960 sizeof(struct audio_device_config_param));
10961 if (adev->device_cfg_params == NULL)
10962 ALOGE("%s: Memory allocation failed for Device config params", __func__);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053010963
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +053010964 /*
10965 * Check if new PSPD matrix mixer control is supported. If not
10966 * supported, then set flag so that old mixer ctrl is sent while
10967 * sending pspd coefficients on older kernel version. Query mixer
10968 * control for default pcm id and channel value one.
10969 */
10970 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
10971 "AudStr %d ChMixer Weight Ch %d", 0, 1);
10972
10973 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
10974 if (!ctl) {
10975 ALOGE("%s: ERROR. Could not get ctl for mixer cmd - %s",
10976 __func__, mixer_ctl_name);
10977 adev->use_old_pspd_mix_ctrl = true;
10978 }
10979
Jaideep Sharma0fa53812020-09-17 09:00:11 +053010980 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010981 return 0;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010982
10983adev_open_err:
10984 free_map(adev->patch_map);
10985 free_map(adev->io_streams_map);
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010986 free(adev->snd_dev_ref_cnt);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010987 pthread_mutex_destroy(&adev->lock);
10988 free(adev);
10989 adev = NULL;
10990 *device = NULL;
10991 pthread_mutex_unlock(&adev_init_lock);
10992 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010993}
10994
10995static struct hw_module_methods_t hal_module_methods = {
10996 .open = adev_open,
10997};
10998
10999struct audio_module HAL_MODULE_INFO_SYM = {
11000 .common = {
11001 .tag = HARDWARE_MODULE_TAG,
11002 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
11003 .hal_api_version = HARDWARE_HAL_API_VERSION,
11004 .id = AUDIO_HARDWARE_MODULE_ID,
11005 .name = "QCOM Audio HAL",
Duy Truongfae19622013-11-24 02:17:54 -080011006 .author = "The Linux Foundation",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080011007 .methods = &hal_module_methods,
11008 },
11009};