blob: 8fc29614667c85d1a44fa1b68c9f82683f8b21aa [file] [log] [blame]
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001/*
Shalini Manjunatha8ae54892020-12-29 13:02:25 +05302 * 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 Song1f93fa52020-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
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -0700151static unsigned int configured_low_latency_capture_period_size =
152 LOW_LATENCY_CAPTURE_PERIOD_SIZE;
153
Haynes Mathew George16081042017-05-31 17:16:49 -0700154#define MMAP_PERIOD_SIZE (DEFAULT_OUTPUT_SAMPLING_RATE/1000)
155#define MMAP_PERIOD_COUNT_MIN 32
156#define MMAP_PERIOD_COUNT_MAX 512
157#define MMAP_PERIOD_COUNT_DEFAULT (MMAP_PERIOD_COUNT_MAX)
158
Aalique Grahame22e49102018-12-18 14:23:57 -0800159/* This constant enables extended precision handling.
160 * TODO The flag is off until more testing is done.
161 */
162static const bool k_enable_extended_precision = false;
Arun Mirpurie008ed22019-03-21 11:21:04 -0700163extern int AUDIO_DEVICE_IN_ALL_CODEC_BACKEND;
Aalique Grahame22e49102018-12-18 14:23:57 -0800164
Eric Laurentb23d5282013-05-14 15:27:20 -0700165struct pcm_config pcm_config_deep_buffer = {
166 .channels = 2,
167 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
168 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
169 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
170 .format = PCM_FORMAT_S16_LE,
171 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
172 .stop_threshold = INT_MAX,
173 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
174};
175
176struct pcm_config pcm_config_low_latency = {
177 .channels = 2,
178 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
179 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
180 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
181 .format = PCM_FORMAT_S16_LE,
182 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
183 .stop_threshold = INT_MAX,
184 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
185};
186
Vignesh Kulothungana6927272019-02-20 15:17:07 -0800187struct pcm_config pcm_config_haptics_audio = {
188 .channels = 1,
189 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
190 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
191 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
192 .format = PCM_FORMAT_S16_LE,
193 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
194 .stop_threshold = INT_MAX,
195 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
196};
197
198struct pcm_config pcm_config_haptics = {
199 .channels = 1,
200 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
201 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
202 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
203 .format = PCM_FORMAT_S16_LE,
204 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
205 .stop_threshold = INT_MAX,
206 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
207};
208
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700209static int af_period_multiplier = 4;
210struct pcm_config pcm_config_rt = {
211 .channels = 2,
212 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
213 .period_size = ULL_PERIOD_SIZE, //1 ms
214 .period_count = 512, //=> buffer size is 512ms
215 .format = PCM_FORMAT_S16_LE,
216 .start_threshold = ULL_PERIOD_SIZE*8, //8ms
217 .stop_threshold = INT_MAX,
218 .silence_threshold = 0,
219 .silence_size = 0,
220 .avail_min = ULL_PERIOD_SIZE, //1 ms
221};
222
Eric Laurentb23d5282013-05-14 15:27:20 -0700223struct pcm_config pcm_config_hdmi_multi = {
224 .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
225 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
226 .period_size = HDMI_MULTI_PERIOD_SIZE,
227 .period_count = HDMI_MULTI_PERIOD_COUNT,
228 .format = PCM_FORMAT_S16_LE,
229 .start_threshold = 0,
230 .stop_threshold = INT_MAX,
231 .avail_min = 0,
232};
233
Haynes Mathew George16081042017-05-31 17:16:49 -0700234struct pcm_config pcm_config_mmap_playback = {
235 .channels = 2,
236 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
237 .period_size = MMAP_PERIOD_SIZE,
238 .period_count = MMAP_PERIOD_COUNT_DEFAULT,
239 .format = PCM_FORMAT_S16_LE,
240 .start_threshold = MMAP_PERIOD_SIZE*8,
241 .stop_threshold = INT32_MAX,
242 .silence_threshold = 0,
243 .silence_size = 0,
244 .avail_min = MMAP_PERIOD_SIZE, //1 ms
245};
246
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700247struct pcm_config pcm_config_hifi = {
248 .channels = DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
249 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
250 .period_size = HIFI_BUFFER_OUTPUT_PERIOD_SIZE, /* change #define */
251 .period_count = HIFI_BUFFER_OUTPUT_PERIOD_COUNT,
252 .format = PCM_FORMAT_S24_3LE,
253 .start_threshold = 0,
254 .stop_threshold = INT_MAX,
255 .avail_min = 0,
256};
257
Eric Laurentb23d5282013-05-14 15:27:20 -0700258struct pcm_config pcm_config_audio_capture = {
259 .channels = 2,
Eric Laurentb23d5282013-05-14 15:27:20 -0700260 .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
261 .format = PCM_FORMAT_S16_LE,
262};
263
Haynes Mathew George16081042017-05-31 17:16:49 -0700264struct pcm_config pcm_config_mmap_capture = {
265 .channels = 2,
266 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
267 .period_size = MMAP_PERIOD_SIZE,
268 .period_count = MMAP_PERIOD_COUNT_DEFAULT,
269 .format = PCM_FORMAT_S16_LE,
270 .start_threshold = 0,
271 .stop_threshold = INT_MAX,
272 .silence_threshold = 0,
273 .silence_size = 0,
274 .avail_min = MMAP_PERIOD_SIZE, //1 ms
275};
276
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -0700277#define AFE_PROXY_CHANNEL_COUNT 2
278#define AFE_PROXY_SAMPLING_RATE 48000
279
280#define AFE_PROXY_PLAYBACK_PERIOD_SIZE 768
281#define AFE_PROXY_PLAYBACK_PERIOD_COUNT 4
282
283struct pcm_config pcm_config_afe_proxy_playback = {
284 .channels = AFE_PROXY_CHANNEL_COUNT,
285 .rate = AFE_PROXY_SAMPLING_RATE,
286 .period_size = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
287 .period_count = AFE_PROXY_PLAYBACK_PERIOD_COUNT,
288 .format = PCM_FORMAT_S16_LE,
289 .start_threshold = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
290 .stop_threshold = INT_MAX,
291 .avail_min = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
292};
293
294#define AFE_PROXY_RECORD_PERIOD_SIZE 768
295#define AFE_PROXY_RECORD_PERIOD_COUNT 4
296
Aalique Grahame22e49102018-12-18 14:23:57 -0800297struct pcm_config pcm_config_audio_capture_rt = {
298 .channels = 2,
299 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
300 .period_size = ULL_PERIOD_SIZE,
301 .period_count = 512,
302 .format = PCM_FORMAT_S16_LE,
303 .start_threshold = 0,
304 .stop_threshold = AFE_PROXY_RECORD_PERIOD_SIZE * AFE_PROXY_RECORD_PERIOD_COUNT,
305 .silence_threshold = 0,
306 .silence_size = 0,
307 .avail_min = ULL_PERIOD_SIZE, //1 ms
308};
309
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -0700310struct pcm_config pcm_config_afe_proxy_record = {
311 .channels = AFE_PROXY_CHANNEL_COUNT,
312 .rate = AFE_PROXY_SAMPLING_RATE,
313 .period_size = AFE_PROXY_RECORD_PERIOD_SIZE,
314 .period_count = AFE_PROXY_RECORD_PERIOD_COUNT,
315 .format = PCM_FORMAT_S16_LE,
316 .start_threshold = AFE_PROXY_RECORD_PERIOD_SIZE,
317 .stop_threshold = INT_MAX,
318 .avail_min = AFE_PROXY_RECORD_PERIOD_SIZE,
319};
320
Ashish Jainf1eaa582016-05-23 20:54:24 +0530321#define AUDIO_MAX_PCM_FORMATS 7
322
323const uint32_t format_to_bitwidth_table[AUDIO_MAX_PCM_FORMATS] = {
324 [AUDIO_FORMAT_DEFAULT] = 0,
325 [AUDIO_FORMAT_PCM_16_BIT] = sizeof(uint16_t),
326 [AUDIO_FORMAT_PCM_8_BIT] = sizeof(uint8_t),
327 [AUDIO_FORMAT_PCM_32_BIT] = sizeof(uint32_t),
328 [AUDIO_FORMAT_PCM_8_24_BIT] = sizeof(uint32_t),
329 [AUDIO_FORMAT_PCM_FLOAT] = sizeof(float),
330 [AUDIO_FORMAT_PCM_24_BIT_PACKED] = sizeof(uint8_t) * 3,
331};
332
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800333const char * const use_case_table[AUDIO_USECASE_MAX] = {
Eric Laurentb23d5282013-05-14 15:27:20 -0700334 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
335 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
Vignesh Kulothungana6927272019-02-20 15:17:07 -0800336 [USECASE_AUDIO_PLAYBACK_WITH_HAPTICS] = "audio-with-haptics-playback",
Meng Wang51d8c2a2020-04-27 15:23:21 +0800337 [USECASE_AUDIO_PLAYBACK_HAPTICS] = "haptics-playback",
Ravi Kumar Alamanda474de5a2015-06-25 20:08:01 -0700338 [USECASE_AUDIO_PLAYBACK_ULL] = "audio-ull-playback",
339 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback",
Shruthi Krishnaace10852013-10-25 14:32:12 -0700340 [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback",
vivek mehta446c3962015-09-14 10:57:35 -0700341 //Enabled for Direct_PCM
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700342 [USECASE_AUDIO_PLAYBACK_OFFLOAD2] = "compress-offload-playback2",
343 [USECASE_AUDIO_PLAYBACK_OFFLOAD3] = "compress-offload-playback3",
344 [USECASE_AUDIO_PLAYBACK_OFFLOAD4] = "compress-offload-playback4",
345 [USECASE_AUDIO_PLAYBACK_OFFLOAD5] = "compress-offload-playback5",
346 [USECASE_AUDIO_PLAYBACK_OFFLOAD6] = "compress-offload-playback6",
347 [USECASE_AUDIO_PLAYBACK_OFFLOAD7] = "compress-offload-playback7",
348 [USECASE_AUDIO_PLAYBACK_OFFLOAD8] = "compress-offload-playback8",
349 [USECASE_AUDIO_PLAYBACK_OFFLOAD9] = "compress-offload-playback9",
Haynes Mathew George16081042017-05-31 17:16:49 -0700350 [USECASE_AUDIO_PLAYBACK_FM] = "play-fm",
351 [USECASE_AUDIO_PLAYBACK_MMAP] = "mmap-playback",
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700352 [USECASE_AUDIO_PLAYBACK_HIFI] = "hifi-playback",
Aalique Grahame22e49102018-12-18 14:23:57 -0800353 [USECASE_AUDIO_PLAYBACK_TTS] = "audio-tts-playback",
vivek mehta0ea887a2015-08-26 14:01:20 -0700354
Eric Laurentb23d5282013-05-14 15:27:20 -0700355 [USECASE_AUDIO_RECORD] = "audio-record",
Mingming Yine62d7842013-10-25 16:26:03 -0700356 [USECASE_AUDIO_RECORD_COMPRESS] = "audio-record-compress",
Dhananjay Kumaree4d2002016-10-25 18:02:58 +0530357 [USECASE_AUDIO_RECORD_COMPRESS2] = "audio-record-compress2",
358 [USECASE_AUDIO_RECORD_COMPRESS3] = "audio-record-compress3",
359 [USECASE_AUDIO_RECORD_COMPRESS4] = "audio-record-compress4",
Dhananjay Kumar376e38b2017-09-28 22:26:23 +0530360 [USECASE_AUDIO_RECORD_COMPRESS5] = "audio-record-compress5",
361 [USECASE_AUDIO_RECORD_COMPRESS6] = "audio-record-compress6",
Eric Laurentb23d5282013-05-14 15:27:20 -0700362 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
Preetam Singh Ranawatde84f1a2013-11-01 14:58:16 -0700363 [USECASE_AUDIO_RECORD_FM_VIRTUAL] = "fm-virtual-record",
Haynes Mathew George16081042017-05-31 17:16:49 -0700364 [USECASE_AUDIO_RECORD_MMAP] = "mmap-record",
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700365 [USECASE_AUDIO_RECORD_HIFI] = "hifi-record",
Haynes Mathew George16081042017-05-31 17:16:49 -0700366
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -0800367 [USECASE_AUDIO_HFP_SCO] = "hfp-sco",
Vimal Puthanveed47e64852013-12-20 13:23:39 -0800368 [USECASE_AUDIO_HFP_SCO_WB] = "hfp-sco-wb",
Derek Chenf7092792017-05-23 12:23:53 -0400369 [USECASE_AUDIO_HFP_SCO_DOWNLINK] = "hfp-sco-downlink",
370 [USECASE_AUDIO_HFP_SCO_WB_DOWNLINK] = "hfp-sco-wb-downlink",
Mingming Yin3ee55c62014-08-04 14:23:35 -0700371
Derek Chenf7092792017-05-23 12:23:53 -0400372 [USECASE_VOICE_CALL] = "voice-call",
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700373 [USECASE_VOICE2_CALL] = "voice2-call",
374 [USECASE_VOLTE_CALL] = "volte-call",
375 [USECASE_QCHAT_CALL] = "qchat-call",
Vicky Sehrawat7e4fc152014-02-12 17:58:59 -0800376 [USECASE_VOWLAN_CALL] = "vowlan-call",
Vidyakumar Athota0e109352015-02-12 17:38:22 -0800377 [USECASE_VOICEMMODE1_CALL] = "voicemmode1-call",
378 [USECASE_VOICEMMODE2_CALL] = "voicemmode2-call",
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800379 [USECASE_COMPRESS_VOIP_CALL] = "compress-voip-call",
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700380 [USECASE_INCALL_REC_UPLINK] = "incall-rec-uplink",
381 [USECASE_INCALL_REC_DOWNLINK] = "incall-rec-downlink",
382 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK] = "incall-rec-uplink-and-downlink",
Helen Zenge56b4852013-12-03 16:54:40 -0800383 [USECASE_INCALL_REC_UPLINK_COMPRESS] = "incall-rec-uplink-compress",
384 [USECASE_INCALL_REC_DOWNLINK_COMPRESS] = "incall-rec-downlink-compress",
385 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK_COMPRESS] = "incall-rec-uplink-and-downlink-compress",
386
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -0700387 [USECASE_INCALL_MUSIC_UPLINK] = "incall_music_uplink",
388 [USECASE_INCALL_MUSIC_UPLINK2] = "incall_music_uplink2",
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700389 [USECASE_AUDIO_SPKR_CALIB_RX] = "spkr-rx-calib",
390 [USECASE_AUDIO_SPKR_CALIB_TX] = "spkr-vi-record",
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -0700391
392 [USECASE_AUDIO_PLAYBACK_AFE_PROXY] = "afe-proxy-playback",
393 [USECASE_AUDIO_RECORD_AFE_PROXY] = "afe-proxy-record",
Zhou Song62ea0282020-03-22 19:53:01 +0800394 [USECASE_AUDIO_RECORD_AFE_PROXY2] = "afe-proxy-record2",
Md Mansoor Ahmeddb1b4f92018-01-25 18:56:31 +0530395 [USECASE_AUDIO_PLAYBACK_SILENCE] = "silence-playback",
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700396
Siddartha Shaik31b530e2017-05-19 15:26:33 +0530397 /* Transcode loopback cases */
Surendar Karka93cd25a2018-08-28 14:21:37 +0530398 [USECASE_AUDIO_TRANSCODE_LOOPBACK_RX] = "audio-transcode-loopback-rx",
399 [USECASE_AUDIO_TRANSCODE_LOOPBACK_TX] = "audio-transcode-loopback-tx",
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700400
401 [USECASE_AUDIO_PLAYBACK_VOIP] = "audio-playback-voip",
402 [USECASE_AUDIO_RECORD_VOIP] = "audio-record-voip",
Varun Balaraje49253e2017-07-06 19:48:56 +0530403 /* For Interactive Audio Streams */
404 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM1] = "audio-interactive-stream1",
405 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM2] = "audio-interactive-stream2",
406 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM3] = "audio-interactive-stream3",
407 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM4] = "audio-interactive-stream4",
408 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM5] = "audio-interactive-stream5",
409 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM6] = "audio-interactive-stream6",
410 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM7] = "audio-interactive-stream7",
411 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM8] = "audio-interactive-stream8",
Garmond Leunge2433c32017-09-28 21:51:22 -0700412
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800413 [USECASE_AUDIO_EC_REF_LOOPBACK] = "ec-ref-audio-capture",
414
Derek Chenf6318be2017-06-12 17:16:24 -0400415 [USECASE_AUDIO_A2DP_ABR_FEEDBACK] = "a2dp-abr-feedback",
416
417 [USECASE_AUDIO_PLAYBACK_MEDIA] = "media-playback",
418 [USECASE_AUDIO_PLAYBACK_SYS_NOTIFICATION] = "sys-notification-playback",
419 [USECASE_AUDIO_PLAYBACK_NAV_GUIDANCE] = "nav-guidance-playback",
420 [USECASE_AUDIO_PLAYBACK_PHONE] = "phone-playback",
Derek Chen1bcdc6b2020-02-06 22:44:53 -0800421 [USECASE_AUDIO_PLAYBACK_FRONT_PASSENGER] = "front-passenger-playback",
Derek Chendf05eea2019-08-01 13:57:49 -0700422 [USECASE_AUDIO_PLAYBACK_REAR_SEAT] = "rear-seat-playback",
Rahul Sharma99770982019-03-06 17:05:26 +0530423 [USECASE_AUDIO_FM_TUNER_EXT] = "fm-tuner-ext",
Eric Laurentb23d5282013-05-14 15:27:20 -0700424};
425
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700426static const audio_usecase_t offload_usecases[] = {
427 USECASE_AUDIO_PLAYBACK_OFFLOAD,
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700428 USECASE_AUDIO_PLAYBACK_OFFLOAD2,
429 USECASE_AUDIO_PLAYBACK_OFFLOAD3,
430 USECASE_AUDIO_PLAYBACK_OFFLOAD4,
431 USECASE_AUDIO_PLAYBACK_OFFLOAD5,
432 USECASE_AUDIO_PLAYBACK_OFFLOAD6,
433 USECASE_AUDIO_PLAYBACK_OFFLOAD7,
434 USECASE_AUDIO_PLAYBACK_OFFLOAD8,
435 USECASE_AUDIO_PLAYBACK_OFFLOAD9,
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700436};
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800437
Varun Balaraje49253e2017-07-06 19:48:56 +0530438static const audio_usecase_t interactive_usecases[] = {
439 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM1,
440 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM2,
441 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM3,
442 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM4,
443 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM5,
444 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM6,
445 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM7,
446 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM8,
447};
448
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800449#define STRING_TO_ENUM(string) { #string, string }
450
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800451struct string_to_enum {
452 const char *name;
453 uint32_t value;
454};
455
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700456static const struct string_to_enum channels_name_to_enum_table[] = {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800457 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
Mingming Yin3a941d42016-02-17 18:08:05 -0800458 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_2POINT1),
459 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_QUAD),
460 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_SURROUND),
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -0700461 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_PENTA),
Mingming Yin3a941d42016-02-17 18:08:05 -0800462 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
463 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_6POINT1),
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800464 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700465 STRING_TO_ENUM(AUDIO_CHANNEL_IN_MONO),
466 STRING_TO_ENUM(AUDIO_CHANNEL_IN_STEREO),
467 STRING_TO_ENUM(AUDIO_CHANNEL_IN_FRONT_BACK),
468 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_1),
469 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_2),
470 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_3),
471 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_4),
472 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_5),
473 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_6),
474 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_7),
475 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_8),
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800476};
477
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700478static const struct string_to_enum formats_name_to_enum_table[] = {
479 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
480 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
481 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Pradnya Chaphekar8a9dcd82014-09-09 09:49:10 -0700482 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
483 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
484 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Ben Romberger1aaaf862017-04-06 17:49:46 -0700485 STRING_TO_ENUM(AUDIO_FORMAT_DOLBY_TRUEHD),
Mingming Yin3a941d42016-02-17 18:08:05 -0800486 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
487 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
Naresh Tanniru928f0862017-04-07 16:44:23 -0700488 STRING_TO_ENUM(AUDIO_FORMAT_IEC61937)
Mingming Yin3a941d42016-02-17 18:08:05 -0800489};
490
491//list of all supported sample rates by HDMI specification.
492static const int out_hdmi_sample_rates[] = {
493 32000, 44100, 48000, 88200, 96000, 176400, 192000,
494};
495
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700496static const struct string_to_enum out_sample_rates_name_to_enum_table[] = {
Mingming Yin3a941d42016-02-17 18:08:05 -0800497 STRING_TO_ENUM(32000),
498 STRING_TO_ENUM(44100),
499 STRING_TO_ENUM(48000),
500 STRING_TO_ENUM(88200),
501 STRING_TO_ENUM(96000),
502 STRING_TO_ENUM(176400),
503 STRING_TO_ENUM(192000),
Mingshu pange8aedf12019-10-25 15:38:27 +0800504 STRING_TO_ENUM(352800),
505 STRING_TO_ENUM(384000),
Pradnya Chaphekar8a9dcd82014-09-09 09:49:10 -0700506};
507
Carter Hsu2e429db2019-05-14 18:50:52 +0800508struct in_effect_list {
509 struct listnode list;
510 effect_handle_t handle;
511};
512
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700513static struct audio_device *adev = NULL;
Ben Romberger02ab1192018-05-24 12:10:08 -0700514static pthread_mutex_t adev_init_lock = PTHREAD_MUTEX_INITIALIZER;
Kiran Kandi910e1862013-10-29 13:29:42 -0700515static unsigned int audio_device_ref_count;
vivek mehtab72d08d2016-04-29 03:16:47 -0700516//cache last MBDRC cal step level
517static int last_known_cal_step = -1 ;
Kiran Kandi910e1862013-10-29 13:29:42 -0700518
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +0530519static int out_set_compr_volume(struct audio_stream_out *stream, float left, float right);
Arun Mirpuri5d170872019-03-26 13:21:31 -0700520static int out_set_mmap_volume(struct audio_stream_out *stream, float left, float right);
Zhou Song2b8f28f2017-09-11 10:51:38 +0800521static int out_set_voip_volume(struct audio_stream_out *stream, float left, float right);
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +0530522static int out_set_pcm_volume(struct audio_stream_out *stream, float left, float right);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +0530523
Derek Chen6f293672019-04-01 01:40:24 -0700524static void adev_snd_mon_cb(void *cookie, struct str_parms *parms);
525static void in_snd_mon_cb(void * stream, struct str_parms * parms);
526static void out_snd_mon_cb(void * stream, struct str_parms * parms);
527
Zhou Song331c8e52019-08-26 14:16:12 +0800528static int configure_btsco_sample_rate(snd_device_t snd_device);
529
Vatsal Buchac09ae062018-11-14 13:25:08 +0530530#ifdef AUDIO_FEATURE_ENABLED_GCOV
531extern void __gcov_flush();
532static void enable_gcov()
533{
534 __gcov_flush();
535}
536#else
537static void enable_gcov()
538{
539}
540#endif
541
justinweng20fb6d82019-02-21 18:49:00 -0700542static int in_set_microphone_direction(const struct audio_stream_in *stream,
543 audio_microphone_direction_t dir);
544static int in_set_microphone_field_dimension(const struct audio_stream_in *stream, float zoom);
545
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700546static bool may_use_noirq_mode(struct audio_device *adev, audio_usecase_t uc_id,
547 int flags __unused)
548{
549 int dir = 0;
550 switch (uc_id) {
551 case USECASE_AUDIO_RECORD_LOW_LATENCY:
552 dir = 1;
553 case USECASE_AUDIO_PLAYBACK_ULL:
554 break;
555 default:
556 return false;
557 }
558
559 int dev_id = platform_get_pcm_device_id(uc_id, dir == 0 ?
560 PCM_PLAYBACK : PCM_CAPTURE);
561 if (adev->adm_is_noirq_avail)
562 return adev->adm_is_noirq_avail(adev->adm_data,
563 adev->snd_card, dev_id, dir);
564 return false;
565}
566
567static void register_out_stream(struct stream_out *out)
568{
569 struct audio_device *adev = out->dev;
570 if (is_offload_usecase(out->usecase) ||
571 !adev->adm_register_output_stream)
572 return;
573
574 // register stream first for backward compatibility
575 adev->adm_register_output_stream(adev->adm_data,
576 out->handle,
577 out->flags);
578
579 if (!adev->adm_set_config)
580 return;
581
582 if (out->realtime)
583 adev->adm_set_config(adev->adm_data,
584 out->handle,
585 out->pcm, &out->config);
586}
587
588static void register_in_stream(struct stream_in *in)
589{
590 struct audio_device *adev = in->dev;
591 if (!adev->adm_register_input_stream)
592 return;
593
594 adev->adm_register_input_stream(adev->adm_data,
595 in->capture_handle,
596 in->flags);
597
598 if (!adev->adm_set_config)
599 return;
600
601 if (in->realtime)
602 adev->adm_set_config(adev->adm_data,
603 in->capture_handle,
604 in->pcm,
605 &in->config);
606}
607
608static void request_out_focus(struct stream_out *out, long ns)
609{
610 struct audio_device *adev = out->dev;
611
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700612 if (adev->adm_request_focus_v2)
613 adev->adm_request_focus_v2(adev->adm_data, out->handle, ns);
614 else if (adev->adm_request_focus)
615 adev->adm_request_focus(adev->adm_data, out->handle);
616}
617
Aniket Kumar Lata60586a92019-05-22 22:18:55 -0700618static int request_in_focus(struct stream_in *in, long ns)
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700619{
620 struct audio_device *adev = in->dev;
Aniket Kumar Lata60586a92019-05-22 22:18:55 -0700621 int ret = 0;
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700622
Aniket Kumar Lata60586a92019-05-22 22:18:55 -0700623 if (adev->adm_request_focus_v2_1)
624 ret = adev->adm_request_focus_v2_1(adev->adm_data, in->capture_handle, ns);
625 else if (adev->adm_request_focus_v2)
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700626 adev->adm_request_focus_v2(adev->adm_data, in->capture_handle, ns);
627 else if (adev->adm_request_focus)
628 adev->adm_request_focus(adev->adm_data, in->capture_handle);
Aniket Kumar Lata60586a92019-05-22 22:18:55 -0700629
630 return ret;
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700631}
632
633static void release_out_focus(struct stream_out *out)
634{
635 struct audio_device *adev = out->dev;
636
637 if (adev->adm_abandon_focus)
638 adev->adm_abandon_focus(adev->adm_data, out->handle);
639}
640
641static void release_in_focus(struct stream_in *in)
642{
643 struct audio_device *adev = in->dev;
644 if (adev->adm_abandon_focus)
645 adev->adm_abandon_focus(adev->adm_data, in->capture_handle);
646}
647
Dhananjay Kumare6293dd2017-05-25 17:25:30 +0530648static int parse_snd_card_status(struct str_parms *parms, int *card,
649 card_status_t *status)
650{
651 char value[32]={0};
652 char state[32]={0};
653
654 int ret = str_parms_get_str(parms, "SND_CARD_STATUS", value, sizeof(value));
655 if (ret < 0)
656 return -1;
657
658 // sscanf should be okay as value is of max length 32.
659 // same as sizeof state.
660 if (sscanf(value, "%d,%s", card, state) < 2)
661 return -1;
662
663 *status = !strcmp(state, "ONLINE") ? CARD_STATUS_ONLINE :
664 CARD_STATUS_OFFLINE;
665 return 0;
666}
667
Haynes Mathew Georgeb0f5dc32017-10-06 18:35:12 -0700668static inline void adjust_frames_for_device_delay(struct stream_out *out,
669 uint32_t *dsp_frames) {
670 // Adjustment accounts for A2dp encoder latency with offload usecases
671 // Note: Encoder latency is returned in ms.
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800672 if (is_a2dp_out_device_type(&out->device_list)) {
Haynes Mathew Georgeb0f5dc32017-10-06 18:35:12 -0700673 unsigned long offset =
674 (audio_extn_a2dp_get_encoder_latency() * out->sample_rate / 1000);
675 *dsp_frames = (*dsp_frames > offset) ? (*dsp_frames - offset) : 0;
676 }
677}
678
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700679static inline bool free_entry(void *key __unused,
680 void *value, void *context __unused)
681{
682 free(value);
683 return true;
684}
685
686static inline void free_map(Hashmap *map)
687{
688 if (map) {
689 hashmapForEach(map, free_entry, (void *) NULL);
690 hashmapFree(map);
691 }
692}
693
Aniket Kumar Latabc774842020-01-16 21:22:05 -0800694static inline void patch_map_remove_l(struct audio_device *adev,
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700695 audio_patch_handle_t patch_handle)
696{
697 if (patch_handle == AUDIO_PATCH_HANDLE_NONE)
698 return;
699
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700700 struct audio_patch_info *p_info =
701 hashmapGet(adev->patch_map, (void *) (intptr_t) patch_handle);
702 if (p_info) {
703 ALOGV("%s: Remove patch %d", __func__, patch_handle);
704 hashmapRemove(adev->patch_map, (void *) (intptr_t) patch_handle);
705 free(p_info->patch);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700706 free(p_info);
707 }
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700708}
709
710static inline int io_streams_map_insert(struct audio_device *adev,
711 struct audio_stream *stream,
712 audio_io_handle_t handle,
713 audio_patch_handle_t patch_handle)
714{
715 struct audio_stream_info *s_info =
716 (struct audio_stream_info *) calloc(1, sizeof(struct audio_stream_info));
717
718 if (s_info == NULL) {
719 ALOGE("%s: Could not allocate stream info", __func__);
720 return -ENOMEM;
721 }
722 s_info->stream = stream;
723 s_info->patch_handle = patch_handle;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700724
725 pthread_mutex_lock(&adev->lock);
726 struct audio_stream_info *stream_info =
727 hashmapPut(adev->io_streams_map, (void *) (intptr_t) handle, (void *) s_info);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700728 if (stream_info != NULL)
729 free(stream_info);
Aniket Kumar Latabc774842020-01-16 21:22:05 -0800730 pthread_mutex_unlock(&adev->lock);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700731 ALOGD("%s: Added stream in io_streams_map with handle %d", __func__, handle);
732 return 0;
733}
734
735static inline void io_streams_map_remove(struct audio_device *adev,
736 audio_io_handle_t handle)
737{
738 pthread_mutex_lock(&adev->lock);
739 struct audio_stream_info *s_info =
740 hashmapRemove(adev->io_streams_map, (void *) (intptr_t) handle);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700741 if (s_info == NULL)
Aniket Kumar Latabc774842020-01-16 21:22:05 -0800742 goto done;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700743 ALOGD("%s: Removed stream with handle %d", __func__, handle);
Aniket Kumar Latabc774842020-01-16 21:22:05 -0800744 patch_map_remove_l(adev, s_info->patch_handle);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700745 free(s_info);
Aniket Kumar Latabc774842020-01-16 21:22:05 -0800746done:
747 pthread_mutex_unlock(&adev->lock);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700748 return;
749}
750
Aniket Kumar Latabc774842020-01-16 21:22:05 -0800751static struct audio_patch_info* fetch_patch_info_l(struct audio_device *adev,
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700752 audio_patch_handle_t handle)
753{
754 struct audio_patch_info *p_info = NULL;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700755 p_info = (struct audio_patch_info *)
756 hashmapGet(adev->patch_map, (void *) (intptr_t) handle);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700757 return p_info;
758}
759
vivek mehtaa76401a2015-04-24 14:12:15 -0700760__attribute__ ((visibility ("default")))
761bool audio_hw_send_gain_dep_calibration(int level) {
762 bool ret_val = false;
vivek mehtab72d08d2016-04-29 03:16:47 -0700763 ALOGV("%s: called ...", __func__);
vivek mehtaa76401a2015-04-24 14:12:15 -0700764
765 pthread_mutex_lock(&adev_init_lock);
766
767 if (adev != NULL && adev->platform != NULL) {
768 pthread_mutex_lock(&adev->lock);
769 ret_val = platform_send_gain_dep_cal(adev->platform, level);
vivek mehtab72d08d2016-04-29 03:16:47 -0700770
Preetam Singh Ranawatf4ae0222017-05-31 17:07:28 +0530771 // cache level info for any of the use case which
772 // was not started.
773 last_known_cal_step = level;;
vivek mehtab72d08d2016-04-29 03:16:47 -0700774
vivek mehtaa76401a2015-04-24 14:12:15 -0700775 pthread_mutex_unlock(&adev->lock);
776 } else {
777 ALOGE("%s: %s is NULL", __func__, adev == NULL ? "adev" : "adev->platform");
778 }
779
780 pthread_mutex_unlock(&adev_init_lock);
781
782 return ret_val;
783}
784
Ashish Jain5106d362016-05-11 19:23:33 +0530785static int check_and_set_gapless_mode(struct audio_device *adev, bool enable_gapless)
786{
Krishnankutty Kolathappilly6d8788b2014-01-09 12:45:31 -0800787 bool gapless_enabled = false;
788 const char *mixer_ctl_name = "Compress Gapless Playback";
789 struct mixer_ctl *ctl;
790
791 ALOGV("%s:", __func__);
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700792 gapless_enabled = property_get_bool("vendor.audio.offload.gapless.enabled", false);
Ashish Jain5106d362016-05-11 19:23:33 +0530793
794 /*Disable gapless if its AV playback*/
795 gapless_enabled = gapless_enabled && enable_gapless;
Krishnankutty Kolathappilly6d8788b2014-01-09 12:45:31 -0800796
797 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
798 if (!ctl) {
799 ALOGE("%s: Could not get ctl for mixer cmd - %s",
800 __func__, mixer_ctl_name);
801 return -EINVAL;
802 }
803
804 if (mixer_ctl_set_value(ctl, 0, gapless_enabled) < 0) {
805 ALOGE("%s: Could not set gapless mode %d",
806 __func__, gapless_enabled);
807 return -EINVAL;
808 }
809 return 0;
810}
Haynes Mathew George5191a852013-09-11 14:19:36 -0700811
Aniket Kumar Lataf56b6402016-10-27 12:03:18 -0700812__attribute__ ((visibility ("default")))
813int audio_hw_get_gain_level_mapping(struct amp_db_and_gain_table *mapping_tbl,
814 int table_size) {
815 int ret_val = 0;
816 ALOGV("%s: enter ... ", __func__);
817
818 pthread_mutex_lock(&adev_init_lock);
819 if (adev == NULL) {
820 ALOGW("%s: adev is NULL .... ", __func__);
821 goto done;
822 }
823
824 pthread_mutex_lock(&adev->lock);
825 ret_val = platform_get_gain_level_mapping(mapping_tbl, table_size);
826 pthread_mutex_unlock(&adev->lock);
827done:
828 pthread_mutex_unlock(&adev_init_lock);
829 ALOGV("%s: exit ... ", __func__);
830 return ret_val;
831}
832
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800833bool audio_hw_send_qdsp_parameter(int stream_type, float vol, bool active)
Aalique Grahame22e49102018-12-18 14:23:57 -0800834{
835 bool ret = false;
836 ALOGV("%s: enter ...", __func__);
837
838 pthread_mutex_lock(&adev_init_lock);
839
840 if (adev != NULL && adev->platform != NULL) {
841 pthread_mutex_lock(&adev->lock);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800842 ret = audio_extn_qdsp_set_state(adev, stream_type, vol, active);
Aalique Grahame22e49102018-12-18 14:23:57 -0800843 pthread_mutex_unlock(&adev->lock);
844 }
845
846 pthread_mutex_unlock(&adev_init_lock);
847
848 ALOGV("%s: exit with ret %d", __func__, ret);
849 return ret;
850}
Aalique Grahame22e49102018-12-18 14:23:57 -0800851
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700852static bool is_supported_format(audio_format_t format)
853{
Eric Laurent86e17132013-09-12 17:49:30 -0700854 if (format == AUDIO_FORMAT_MP3 ||
Satish Babu Patakokila0c313922016-12-08 12:07:08 +0530855 format == AUDIO_FORMAT_MP2 ||
Ashish Jainf9b78162014-08-25 20:36:25 +0530856 format == AUDIO_FORMAT_AAC_LC ||
857 format == AUDIO_FORMAT_AAC_HE_V1 ||
858 format == AUDIO_FORMAT_AAC_HE_V2 ||
Manish Dewangana6fc5442015-08-24 20:30:31 +0530859 format == AUDIO_FORMAT_AAC_ADTS_LC ||
860 format == AUDIO_FORMAT_AAC_ADTS_HE_V1 ||
861 format == AUDIO_FORMAT_AAC_ADTS_HE_V2 ||
Arun Kumar Dasari3b174182016-12-27 13:01:14 +0530862 format == AUDIO_FORMAT_AAC_LATM_LC ||
863 format == AUDIO_FORMAT_AAC_LATM_HE_V1 ||
864 format == AUDIO_FORMAT_AAC_LATM_HE_V2 ||
Ashish Jain5106d362016-05-11 19:23:33 +0530865 format == AUDIO_FORMAT_PCM_24_BIT_PACKED ||
866 format == AUDIO_FORMAT_PCM_8_24_BIT ||
Ashish Jainf1eaa582016-05-23 20:54:24 +0530867 format == AUDIO_FORMAT_PCM_FLOAT ||
868 format == AUDIO_FORMAT_PCM_32_BIT ||
vivek mehta0ea887a2015-08-26 14:01:20 -0700869 format == AUDIO_FORMAT_PCM_16_BIT ||
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530870 format == AUDIO_FORMAT_AC3 ||
871 format == AUDIO_FORMAT_E_AC3 ||
Ben Romberger1aaaf862017-04-06 17:49:46 -0700872 format == AUDIO_FORMAT_DOLBY_TRUEHD ||
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530873 format == AUDIO_FORMAT_DTS ||
874 format == AUDIO_FORMAT_DTS_HD ||
Weiyin Jiang18ac4e92015-03-15 15:03:40 +0800875 format == AUDIO_FORMAT_FLAC ||
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530876 format == AUDIO_FORMAT_ALAC ||
877 format == AUDIO_FORMAT_APE ||
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530878 format == AUDIO_FORMAT_DSD ||
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530879 format == AUDIO_FORMAT_VORBIS ||
Weiyin Jiang18ac4e92015-03-15 15:03:40 +0800880 format == AUDIO_FORMAT_WMA ||
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +0530881 format == AUDIO_FORMAT_WMA_PRO ||
Naresh Tanniru928f0862017-04-07 16:44:23 -0700882 format == AUDIO_FORMAT_APTX ||
883 format == AUDIO_FORMAT_IEC61937)
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -0800884 return true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700885
886 return false;
887}
888
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700889static inline bool is_mmap_usecase(audio_usecase_t uc_id)
890{
891 return (uc_id == USECASE_AUDIO_RECORD_AFE_PROXY) ||
Zhou Song62ea0282020-03-22 19:53:01 +0800892 (uc_id == USECASE_AUDIO_RECORD_AFE_PROXY2) ||
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700893 (uc_id == USECASE_AUDIO_PLAYBACK_AFE_PROXY);
894}
895
Aniket Kumar Lata8426d1f2019-06-10 15:25:53 -0700896static inline bool is_valid_volume(float left, float right)
897{
898 return ((left >= 0.0f && right >= 0.0f) ? true : false);
899}
900
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +0530901static void enable_asrc_mode(struct audio_device *adev)
Preetam Singh Ranawatb0c0dd72016-08-18 00:32:06 +0530902{
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +0530903 ALOGV("%s", __func__);
904 audio_route_apply_and_update_path(adev->audio_route,
905 "asrc-mode");
906 adev->asrc_mode_enabled = true;
907}
908
909static void disable_asrc_mode(struct audio_device *adev)
910{
911 ALOGV("%s", __func__);
912 audio_route_reset_and_update_path(adev->audio_route,
913 "asrc-mode");
914 adev->asrc_mode_enabled = false;
915}
916
917/*
918 * - Enable ASRC mode for incoming mix path use case(Headphone backend)if Headphone
919 * 44.1 or Native DSD backends are enabled for any of current use case.
920 * e.g. 48-> + (Naitve DSD or Headphone 44.1)
921 * - Disable current mix path use case(Headphone backend) and re-enable it with
922 * ASRC mode for incoming Headphone 44.1 or Native DSD use case.
923 * e.g. Naitve DSD or Headphone 44.1 -> + 48
924 */
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +0530925static void check_and_set_asrc_mode(struct audio_device *adev,
926 struct audio_usecase *uc_info,
927 snd_device_t snd_device)
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +0530928{
929 ALOGV("%s snd device %d", __func__, snd_device);
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +0530930 int i, num_new_devices = 0;
931 snd_device_t split_new_snd_devices[SND_DEVICE_OUT_END];
932 /*
933 *Split snd device for new combo use case
934 *e.g. Headphopne 44.1-> + Ringtone (Headphone + Speaker)
935 */
936 if (platform_split_snd_device(adev->platform,
937 snd_device,
938 &num_new_devices,
939 split_new_snd_devices) == 0) {
940 for (i = 0; i < num_new_devices; i++)
941 check_and_set_asrc_mode(adev, uc_info, split_new_snd_devices[i]);
942 } else {
943 int new_backend_idx = platform_get_backend_index(snd_device);
944 if (((new_backend_idx == HEADPHONE_BACKEND) ||
945 (new_backend_idx == HEADPHONE_44_1_BACKEND) ||
946 (new_backend_idx == DSD_NATIVE_BACKEND)) &&
947 !adev->asrc_mode_enabled) {
948 struct listnode *node = NULL;
949 struct audio_usecase *uc = NULL;
950 struct stream_out *curr_out = NULL;
951 int usecase_backend_idx = DEFAULT_CODEC_BACKEND;
952 int i, num_devices, ret = 0;
953 snd_device_t split_snd_devices[SND_DEVICE_OUT_END];
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +0530954
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +0530955 list_for_each(node, &adev->usecase_list) {
956 uc = node_to_item(node, struct audio_usecase, list);
957 curr_out = (struct stream_out*) uc->stream.out;
958 if (curr_out && PCM_PLAYBACK == uc->type && uc != uc_info) {
959 /*
960 *Split snd device for existing combo use case
961 *e.g. Ringtone (Headphone + Speaker) + Headphopne 44.1
962 */
963 ret = platform_split_snd_device(adev->platform,
964 uc->out_snd_device,
965 &num_devices,
966 split_snd_devices);
967 if (ret < 0 || num_devices == 0) {
968 ALOGV("%s: Unable to split uc->out_snd_device: %d",__func__, uc->out_snd_device);
969 split_snd_devices[0] = uc->out_snd_device;
970 num_devices = 1;
Garmond Leung50058f62017-02-08 09:49:30 -0800971 }
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +0530972 for (i = 0; i < num_devices; i++) {
973 usecase_backend_idx = platform_get_backend_index(split_snd_devices[i]);
974 ALOGD("%s:snd_dev %d usecase_backend_idx %d",__func__, split_snd_devices[i],usecase_backend_idx);
975 if((new_backend_idx == HEADPHONE_BACKEND) &&
976 ((usecase_backend_idx == HEADPHONE_44_1_BACKEND) ||
977 (usecase_backend_idx == DSD_NATIVE_BACKEND))) {
978 ALOGD("%s:DSD or native stream detected enabling asrcmode in hardware",
979 __func__);
980 enable_asrc_mode(adev);
981 break;
982 } else if(((new_backend_idx == HEADPHONE_44_1_BACKEND) ||
983 (new_backend_idx == DSD_NATIVE_BACKEND)) &&
984 (usecase_backend_idx == HEADPHONE_BACKEND)) {
985 ALOGD("%s:48K stream detected, disabling and enabling it with asrcmode in hardware",
986 __func__);
987 disable_audio_route(adev, uc);
988 disable_snd_device(adev, uc->out_snd_device);
989 // Apply true-high-quality-mode if DSD or > 44.1KHz or >=24-bit
990 if (new_backend_idx == DSD_NATIVE_BACKEND)
991 audio_route_apply_and_update_path(adev->audio_route,
992 "hph-true-highquality-mode");
993 else if ((new_backend_idx == HEADPHONE_44_1_BACKEND) &&
994 (curr_out->bit_width >= 24))
995 audio_route_apply_and_update_path(adev->audio_route,
996 "hph-highquality-mode");
997 enable_asrc_mode(adev);
998 enable_snd_device(adev, uc->out_snd_device);
999 enable_audio_route(adev, uc);
1000 break;
1001 }
1002 }
1003 // reset split devices count
1004 num_devices = 0;
Garmond Leung50058f62017-02-08 09:49:30 -08001005 }
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +05301006 if (adev->asrc_mode_enabled)
1007 break;
Preetam Singh Ranawatb0c0dd72016-08-18 00:32:06 +05301008 }
1009 }
1010 }
1011}
1012
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001013static int send_effect_enable_disable_mixer_ctl(struct audio_device *adev,
1014 struct audio_effect_config effect_config,
1015 unsigned int param_value)
1016{
1017 char mixer_ctl_name[] = "Audio Effect";
1018 struct mixer_ctl *ctl;
1019 long set_values[6];
Eric Laurent637e2d42018-11-15 12:24:31 -08001020 struct stream_in *in = adev_get_active_input(adev);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001021
Aniket Kumar Lata7de63522019-06-13 11:05:18 -07001022 if (in == NULL) {
1023 ALOGE("%s: active input stream is NULL", __func__);
1024 return -EINVAL;
1025 }
1026
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001027 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1028 if (!ctl) {
1029 ALOGE("%s: Could not get mixer ctl - %s",
1030 __func__, mixer_ctl_name);
1031 return -EINVAL;
1032 }
1033
1034 set_values[0] = 1; //0:Rx 1:Tx
1035 set_values[1] = in->app_type_cfg.app_type;
1036 set_values[2] = (long)effect_config.module_id;
1037 set_values[3] = (long)effect_config.instance_id;
1038 set_values[4] = (long)effect_config.param_id;
1039 set_values[5] = param_value;
1040
1041 mixer_ctl_set_array(ctl, set_values, ARRAY_SIZE(set_values));
1042
1043 return 0;
1044
1045}
1046
1047static int update_effect_param_ecns(struct audio_device *adev, unsigned int module_id,
1048 int effect_type, unsigned int *param_value)
1049{
1050 int ret = 0;
1051 struct audio_effect_config other_effect_config;
1052 struct audio_usecase *usecase = NULL;
Eric Laurent637e2d42018-11-15 12:24:31 -08001053 struct stream_in *in = adev_get_active_input(adev);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001054
Aniket Kumar Lata7de63522019-06-13 11:05:18 -07001055 if (in == NULL) {
1056 ALOGE("%s: active input stream is NULL", __func__);
1057 return -EINVAL;
1058 }
1059
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001060 usecase = get_usecase_from_list(adev, in->usecase);
1061 if (!usecase)
1062 return -EINVAL;
1063
1064 ret = platform_get_effect_config_data(usecase->in_snd_device, &other_effect_config,
1065 effect_type == EFFECT_AEC ? EFFECT_NS : EFFECT_AEC);
1066 if (ret < 0) {
1067 ALOGE("%s Failed to get effect params %d", __func__, ret);
1068 return ret;
1069 }
1070
1071 if (module_id == other_effect_config.module_id) {
1072 //Same module id for AEC/NS. Values need to be combined
1073 if (((effect_type == EFFECT_AEC) && (in->enable_ns)) ||
1074 ((effect_type == EFFECT_NS) && (in->enable_aec))) {
1075 *param_value |= other_effect_config.param_value;
1076 }
1077 }
1078
1079 return ret;
1080}
1081
1082static int enable_disable_effect(struct audio_device *adev, int effect_type, bool enable)
Gangadhar Sb0210342019-02-22 17:39:41 +05301083{
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001084 struct audio_effect_config effect_config;
1085 struct audio_usecase *usecase = NULL;
1086 int ret = 0;
1087 unsigned int param_value = 0;
Eric Laurent637e2d42018-11-15 12:24:31 -08001088 struct stream_in *in = adev_get_active_input(adev);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001089
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001090 if(!voice_extn_is_dynamic_ecns_enabled())
1091 return ENOSYS;
1092
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001093 if (!in) {
1094 ALOGE("%s: Invalid input stream", __func__);
1095 return -EINVAL;
1096 }
1097
1098 ALOGD("%s: effect_type:%d enable:%d", __func__, effect_type, enable);
1099
1100 usecase = get_usecase_from_list(adev, in->usecase);
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001101 if (usecase == NULL) {
1102 ALOGE("%s: Could not find the usecase (%d) in the list",
1103 __func__, in->usecase);
1104 return -EINVAL;
1105 }
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001106
1107 ret = platform_get_effect_config_data(usecase->in_snd_device, &effect_config, effect_type);
1108 if (ret < 0) {
1109 ALOGE("%s Failed to get module id %d", __func__, ret);
1110 return ret;
1111 }
1112 ALOGV("%s: %d %d usecase->id:%d usecase->in_snd_device:%d", __func__, effect_config.module_id,
1113 in->app_type_cfg.app_type, usecase->id, usecase->in_snd_device);
1114
1115 if(enable)
1116 param_value = effect_config.param_value;
1117
1118 /*Special handling for AEC & NS effects Param values need to be
1119 updated if module ids are same*/
1120
1121 if ((effect_type == EFFECT_AEC) || (effect_type == EFFECT_NS)) {
1122 ret = update_effect_param_ecns(adev, effect_config.module_id, effect_type, &param_value);
1123 if (ret < 0)
1124 return ret;
1125 }
1126
1127 ret = send_effect_enable_disable_mixer_ctl(adev, effect_config, param_value);
1128
1129 return ret;
1130}
1131
1132static void check_and_enable_effect(struct audio_device *adev)
1133{
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001134 if(!voice_extn_is_dynamic_ecns_enabled())
1135 return;
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001136
Eric Laurent637e2d42018-11-15 12:24:31 -08001137 struct stream_in *in = adev_get_active_input(adev);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001138
Eric Laurent637e2d42018-11-15 12:24:31 -08001139 if (in != NULL && !in->standby) {
1140 if (in->enable_aec)
1141 enable_disable_effect(adev, EFFECT_AEC, true);
1142
1143 if (in->enable_ns &&
1144 in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
1145 enable_disable_effect(adev, EFFECT_NS, true);
1146 }
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001147 }
1148}
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001149
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07001150int pcm_ioctl(struct pcm *pcm, int request, ...)
1151{
1152 va_list ap;
1153 void * arg;
1154 int pcm_fd = *(int*)pcm;
1155
1156 va_start(ap, request);
1157 arg = va_arg(ap, void *);
1158 va_end(ap);
1159
1160 return ioctl(pcm_fd, request, arg);
1161}
1162
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -07001163int enable_audio_route(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001164 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001165{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001166 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001167 char mixer_path[MIXER_PATH_MAX_LENGTH];
Manish Dewangan58229382017-02-02 15:48:41 +05301168 struct stream_out *out = NULL;
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05301169 struct stream_in *in = NULL;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001170 struct listnode out_devices;
Soumya Managoli6993b762018-06-28 16:04:57 +05301171 int ret = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001172
1173 if (usecase == NULL)
1174 return -EINVAL;
1175
1176 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
1177
Carter Hsu2e429db2019-05-14 18:50:52 +08001178 if (usecase->type == PCM_CAPTURE) {
1179 struct stream_in *in = usecase->stream.in;
1180 struct audio_usecase *uinfo;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001181 snd_device = usecase->in_snd_device;
Carter Hsu2e429db2019-05-14 18:50:52 +08001182
1183 if (in) {
1184 if (in->enable_aec || in->enable_ec_port) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001185 list_init(&out_devices);
1186 update_device_list(&out_devices, AUDIO_DEVICE_OUT_SPEAKER, "", true);
Carter Hsu2e429db2019-05-14 18:50:52 +08001187 struct listnode *node;
1188 struct audio_usecase *voip_usecase = get_usecase_from_list(adev,
1189 USECASE_AUDIO_PLAYBACK_VOIP);
1190 if (voip_usecase) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001191 assign_devices(&out_devices,
1192 &voip_usecase->stream.out->device_list);
Carter Hsu2e429db2019-05-14 18:50:52 +08001193 } else if (adev->primary_output &&
1194 !adev->primary_output->standby) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001195 assign_devices(&out_devices,
1196 &adev->primary_output->device_list);
Carter Hsu2e429db2019-05-14 18:50:52 +08001197 } else {
1198 list_for_each(node, &adev->usecase_list) {
1199 uinfo = node_to_item(node, struct audio_usecase, list);
1200 if (uinfo->type != PCM_CAPTURE) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001201 assign_devices(&out_devices,
1202 &uinfo->stream.out->device_list);
Carter Hsu2e429db2019-05-14 18:50:52 +08001203 break;
1204 }
1205 }
1206 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001207
1208 platform_set_echo_reference(adev, true, &out_devices);
Carter Hsu2e429db2019-05-14 18:50:52 +08001209 in->ec_opened = true;
1210 }
1211 }
Guodong Huf5e614d2019-06-24 18:42:03 +08001212 } else if ((usecase->type == TRANSCODE_LOOPBACK_TX) || ((usecase->type == PCM_HFP_CALL) &&
1213 ((usecase->id == USECASE_AUDIO_HFP_SCO) || (usecase->id == USECASE_AUDIO_HFP_SCO_WB)) &&
1214 (usecase->in_snd_device == SND_DEVICE_IN_VOICE_SPEAKER_MIC_HFP_MMSECNS))) {
Carter Hsu2e429db2019-05-14 18:50:52 +08001215 snd_device = usecase->in_snd_device;
1216 } else {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001217 snd_device = usecase->out_snd_device;
Carter Hsu2e429db2019-05-14 18:50:52 +08001218 }
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001219
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08001220#ifdef DS1_DOLBY_DAP_ENABLED
1221 audio_extn_dolby_set_dmid(adev);
1222 audio_extn_dolby_set_endpoint(adev);
1223#endif
Pradnya Chaphekar8a9dcd82014-09-09 09:49:10 -07001224 audio_extn_dolby_ds2_set_endpoint(adev);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001225 audio_extn_sound_trigger_update_stream_status(usecase, ST_EVENT_STREAM_BUSY);
Dhananjay Kumar45b71742014-05-29 21:47:27 +05301226 audio_extn_listen_update_stream_status(usecase, LISTEN_EVENT_STREAM_BUSY);
Ben Romberger1fafdde2015-09-09 19:43:15 -07001227 audio_extn_utils_send_app_type_cfg(adev, usecase);
Jasmine Cha4dcc1092019-03-04 18:12:47 +08001228 if (audio_extn_is_maxx_audio_enabled())
1229 audio_extn_ma_set_device(usecase);
Dhananjay Kumar14170dd2015-08-28 13:24:16 +05301230 audio_extn_utils_send_audio_calibration(adev, usecase);
Zhou Song1f93fa52020-11-20 13:57:39 +08001231 if ((usecase->type == PCM_PLAYBACK) && is_offload_usecase(usecase->id)) {
1232 out = usecase->stream.out;
1233 if (out && out->compr)
Manish Dewangan58229382017-02-02 15:48:41 +05301234 audio_extn_utils_compress_set_clk_rec_mode(usecase);
1235 }
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05301236
1237 if (usecase->type == PCM_CAPTURE) {
1238 in = usecase->stream.in;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001239 if (in && is_loopback_input_device(get_device_types(&in->device_list))) {
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05301240 ALOGD("%s: set custom mtmx params v1", __func__);
1241 audio_extn_set_custom_mtmx_params_v1(adev, usecase, true);
1242 }
1243 } else {
1244 audio_extn_set_custom_mtmx_params_v2(adev, usecase, true);
1245 }
Manish Dewangan58229382017-02-02 15:48:41 +05301246
Andy Hung756ecc12018-10-19 17:47:12 -07001247 // we shouldn't truncate mixer_path
1248 ALOGW_IF(strlcpy(mixer_path, use_case_table[usecase->id], sizeof(mixer_path))
1249 >= sizeof(mixer_path), "%s: truncation on mixer path", __func__);
1250 // this also appends to mixer_path
Banajit Goswami20cdd212015-09-11 01:11:30 -07001251 platform_add_backend_name(mixer_path, snd_device, usecase);
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07001252 ALOGD("%s: apply mixer and update path: %s", __func__, mixer_path);
Soumya Managoli6993b762018-06-28 16:04:57 +05301253 ret = audio_route_apply_and_update_path(adev->audio_route, mixer_path);
1254 if (!ret && usecase->id == USECASE_AUDIO_PLAYBACK_FM) {
1255 struct str_parms *parms = str_parms_create_str("fm_restore_volume=1");
1256 if (parms) {
1257 audio_extn_fm_set_parameters(adev, parms);
1258 str_parms_destroy(parms);
1259 }
1260 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001261 ALOGV("%s: exit", __func__);
1262 return 0;
1263}
1264
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001265int disable_audio_route(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001266 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001267{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001268 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001269 char mixer_path[MIXER_PATH_MAX_LENGTH];
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05301270 struct stream_in *in = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001271
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05301272 if (usecase == NULL || usecase->id == USECASE_INVALID)
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001273 return -EINVAL;
1274
1275 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Surendar Karka93cd25a2018-08-28 14:21:37 +05301276 if (usecase->type == PCM_CAPTURE || usecase->type == TRANSCODE_LOOPBACK_TX)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001277 snd_device = usecase->in_snd_device;
1278 else
1279 snd_device = usecase->out_snd_device;
Kunlei Zhange7cab1a2020-05-17 20:31:05 +08001280
1281 /* disable island and power mode on supported device for voice call */
1282 if (usecase->type == VOICE_CALL) {
1283 if (usecase->in_snd_device != SND_DEVICE_NONE) {
1284 if (platform_get_island_cfg_on_device(adev->platform, usecase->in_snd_device) &&
1285 platform_get_power_mode_on_device(adev->platform, usecase->in_snd_device)) {
1286 platform_set_island_cfg_on_device(adev, usecase->in_snd_device, false);
1287 platform_set_power_mode_on_device(adev, usecase->in_snd_device, false);
1288 platform_reset_island_power_status(adev->platform, usecase->in_snd_device);
1289 ALOGD("%s: disable island cfg and power mode in voice tx path",
1290 __func__);
1291 }
1292 }
1293 if (usecase->out_snd_device != SND_DEVICE_NONE) {
1294 if (platform_get_island_cfg_on_device(adev->platform, usecase->out_snd_device) &&
1295 platform_get_power_mode_on_device(adev->platform, usecase->out_snd_device)) {
1296 platform_set_island_cfg_on_device(adev, usecase->out_snd_device, false);
1297 platform_set_power_mode_on_device(adev, usecase->out_snd_device, false);
1298 platform_reset_island_power_status(adev->platform, usecase->out_snd_device);
1299 ALOGD("%s: disable island cfg and power mode in voice rx path",
1300 __func__);
1301 }
1302 }
1303 }
1304
Andy Hung756ecc12018-10-19 17:47:12 -07001305 // we shouldn't truncate mixer_path
1306 ALOGW_IF(strlcpy(mixer_path, use_case_table[usecase->id], sizeof(mixer_path))
1307 >= sizeof(mixer_path), "%s: truncation on mixer path", __func__);
1308 // this also appends to mixer_path
Banajit Goswami20cdd212015-09-11 01:11:30 -07001309 platform_add_backend_name(mixer_path, snd_device, usecase);
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07001310 ALOGD("%s: reset and update mixer path: %s", __func__, mixer_path);
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07001311 audio_route_reset_and_update_path(adev->audio_route, mixer_path);
Carter Hsu2e429db2019-05-14 18:50:52 +08001312 if (usecase->type == PCM_CAPTURE) {
1313 struct stream_in *in = usecase->stream.in;
1314 if (in && in->ec_opened) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001315 struct listnode out_devices;
1316 list_init(&out_devices);
1317 platform_set_echo_reference(in->dev, false, &out_devices);
Carter Hsu2e429db2019-05-14 18:50:52 +08001318 in->ec_opened = false;
1319 }
1320 }
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001321 audio_extn_sound_trigger_update_stream_status(usecase, ST_EVENT_STREAM_FREE);
Dhananjay Kumar45b71742014-05-29 21:47:27 +05301322 audio_extn_listen_update_stream_status(usecase, LISTEN_EVENT_STREAM_FREE);
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05301323
1324 if (usecase->type == PCM_CAPTURE) {
1325 in = usecase->stream.in;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001326 if (in && is_loopback_input_device(get_device_types(&in->device_list))) {
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05301327 ALOGD("%s: reset custom mtmx params v1", __func__);
1328 audio_extn_set_custom_mtmx_params_v1(adev, usecase, false);
1329 }
1330 } else {
1331 audio_extn_set_custom_mtmx_params_v2(adev, usecase, false);
1332 }
1333
Weiyin Jiang298ffd92019-06-03 14:29:30 +08001334 if ((usecase->type == PCM_PLAYBACK) &&
1335 (usecase->stream.out != NULL))
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +05301336 usecase->stream.out->pspd_coeff_sent = false;
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05301337
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001338 ALOGV("%s: exit", __func__);
1339 return 0;
1340}
1341
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -07001342int enable_snd_device(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001343 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001344{
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301345 int i, num_devices = 0;
1346 snd_device_t new_snd_devices[SND_DEVICE_OUT_END];
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001347 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
1348
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001349 if (snd_device < SND_DEVICE_MIN ||
1350 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001351 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001352 return -EINVAL;
1353 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001354
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001355 if (platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0) {
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001356 ALOGE("%s: Invalid sound device returned", __func__);
1357 return -EINVAL;
1358 }
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001359
1360 adev->snd_dev_ref_cnt[snd_device]++;
1361
1362 if ((adev->snd_dev_ref_cnt[snd_device] > 1) &&
1363 (platform_split_snd_device(adev->platform,
1364 snd_device,
1365 &num_devices,
1366 new_snd_devices) != 0)) {
Eric Laurent994a6932013-07-17 11:51:42 -07001367 ALOGV("%s: snd_device(%d: %s) is already active",
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001368 __func__, snd_device, device_name);
Aniket Kumar Latabce0be62019-07-11 14:20:23 -07001369 /* Set backend config for A2DP to ensure slimbus configuration
1370 is correct if A2DP is already active and backend is closed
1371 and re-opened */
1372 if (snd_device == SND_DEVICE_OUT_BT_A2DP)
1373 audio_extn_a2dp_set_source_backend_cfg();
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001374 return 0;
1375 }
1376
Gopikrishnaiah Anandane85d0462014-06-30 21:41:20 -07001377 if (audio_extn_spkr_prot_is_enabled())
1378 audio_extn_spkr_prot_calib_cancel(adev);
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -07001379
Aalique Grahame22e49102018-12-18 14:23:57 -08001380 audio_extn_dsm_feedback_enable(adev, snd_device, true);
1381
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001382 if (platform_can_enable_spkr_prot_on_device(snd_device) &&
1383 audio_extn_spkr_prot_is_enabled()) {
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001384 if (platform_get_spkr_prot_acdb_id(snd_device) < 0) {
1385 goto err;
1386 }
1387 audio_extn_dev_arbi_acquire(snd_device);
1388 if (audio_extn_spkr_prot_start_processing(snd_device)) {
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -08001389 ALOGE("%s: spkr_start_processing failed", __func__);
Ravit Dennisaaee49c2015-02-04 21:26:22 +02001390 audio_extn_dev_arbi_release(snd_device);
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001391 goto err;
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -08001392 }
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001393 } else if (platform_split_snd_device(adev->platform,
1394 snd_device,
1395 &num_devices,
1396 new_snd_devices) == 0) {
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301397 for (i = 0; i < num_devices; i++) {
1398 enable_snd_device(adev, new_snd_devices[i]);
1399 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001400 platform_set_speaker_gain_in_combo(adev, snd_device, true);
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -08001401 } else {
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07001402 ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, device_name);
Naresh Tannirucd2353e2016-08-19 00:37:25 +05301403
Kunlei Zhange7cab1a2020-05-17 20:31:05 +08001404 /* enable island and power mode on supported device */
1405 if (platform_get_island_cfg_on_device(adev->platform, snd_device) &&
1406 platform_get_power_mode_on_device(adev->platform, snd_device)) {
1407 platform_set_island_cfg_on_device(adev, snd_device, true);
1408 platform_set_power_mode_on_device(adev, snd_device, true);
1409 ALOGD("%s: enable island cfg and power mode on: %s",
1410 __func__, device_name);
1411 }
Preetam Singh Ranawatf1d417c2017-01-10 17:00:32 +05301412
Revathi Uddarajub26e3932020-06-10 14:51:02 +05301413 if (SND_DEVICE_OUT_BT_A2DP == snd_device) {
1414 if (audio_extn_a2dp_start_playback() < 0) {
1415 ALOGE(" fail to configure A2dp Source control path ");
1416 goto err;
1417 } else {
1418 adev->a2dp_started = true;
1419 }
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001420 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001421
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001422 if ((SND_DEVICE_IN_BT_A2DP == snd_device) &&
1423 (audio_extn_a2dp_start_capture() < 0)) {
1424 ALOGE(" fail to configure A2dp Sink control path ");
1425 goto err;
1426 }
Naresh Tannirucd2353e2016-08-19 00:37:25 +05301427
Mingshu Pang7be5b1d2020-03-04 15:24:38 +08001428 if ((SND_DEVICE_OUT_BT_SCO_SWB == snd_device) ||
1429 (SND_DEVICE_IN_BT_SCO_MIC_SWB_NREC == snd_device) ||
1430 (SND_DEVICE_IN_BT_SCO_MIC_SWB == snd_device)) {
1431 if (!adev->bt_sco_on || (audio_extn_sco_start_configuration() < 0)) {
1432 ALOGE(" fail to configure sco control path ");
1433 goto err;
1434 }
Zhou Song12c29502019-03-16 10:37:18 +08001435 }
1436
Zhou Song331c8e52019-08-26 14:16:12 +08001437 configure_btsco_sample_rate(snd_device);
Bharath Ramachandramurthy0de16782014-03-28 21:34:33 -07001438 /* due to the possibility of calibration overwrite between listen
1439 and audio, notify listen hal before audio calibration is sent */
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001440 audio_extn_sound_trigger_update_device_status(snd_device,
1441 ST_EVENT_SND_DEVICE_BUSY);
Dhananjay Kumar45b71742014-05-29 21:47:27 +05301442 audio_extn_listen_update_device_status(snd_device,
1443 LISTEN_EVENT_SND_DEVICE_BUSY);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001444 if (platform_get_snd_device_acdb_id(snd_device) < 0) {
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001445 audio_extn_sound_trigger_update_device_status(snd_device,
1446 ST_EVENT_SND_DEVICE_FREE);
Dhananjay Kumar45b71742014-05-29 21:47:27 +05301447 audio_extn_listen_update_device_status(snd_device,
1448 LISTEN_EVENT_SND_DEVICE_FREE);
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001449 goto err;
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -07001450 }
Lior Barenboim0b61bc72014-05-13 13:01:37 +03001451 audio_extn_dev_arbi_acquire(snd_device);
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07001452 audio_route_apply_and_update_path(adev->audio_route, device_name);
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301453
1454 if (SND_DEVICE_OUT_HEADPHONES == snd_device &&
1455 !adev->native_playback_enabled &&
1456 audio_is_true_native_stream_active(adev)) {
1457 ALOGD("%s: %d: napb: enabling native mode in hardware",
1458 __func__, __LINE__);
1459 audio_route_apply_and_update_path(adev->audio_route,
1460 "true-native-mode");
1461 adev->native_playback_enabled = true;
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +05301462 }
Dhanalakshmi Siddani10621622018-04-30 15:07:27 +05301463 if (((snd_device == SND_DEVICE_IN_HANDSET_6MIC) ||
1464 (snd_device == SND_DEVICE_IN_HANDSET_QMIC)) &&
Eric Laurent637e2d42018-11-15 12:24:31 -08001465 (audio_extn_ffv_get_stream() == adev_get_active_input(adev))) {
Garmond Leunge2433c32017-09-28 21:51:22 -07001466 ALOGD("%s: init ec ref loopback", __func__);
1467 audio_extn_ffv_init_ec_ref_loopback(adev, snd_device);
1468 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001469 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001470 return 0;
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001471err:
1472 adev->snd_dev_ref_cnt[snd_device]--;
1473 return -EINVAL;;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001474}
1475
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001476int disable_snd_device(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001477 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001478{
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301479 int i, num_devices = 0;
1480 snd_device_t new_snd_devices[SND_DEVICE_OUT_END];
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001481 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
1482
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001483 if (snd_device < SND_DEVICE_MIN ||
1484 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001485 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001486 return -EINVAL;
1487 }
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001488
1489 if (platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0) {
1490 ALOGE("%s: Invalid sound device returned", __func__);
1491 return -EINVAL;
1492 }
1493
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001494 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
1495 ALOGE("%s: device ref cnt is already 0", __func__);
1496 return -EINVAL;
1497 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001498
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001499 adev->snd_dev_ref_cnt[snd_device]--;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -07001500
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001501
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001502 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07001503 ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, device_name);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301504
Aalique Grahame22e49102018-12-18 14:23:57 -08001505 audio_extn_dsm_feedback_enable(adev, snd_device, false);
1506
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001507 if (platform_can_enable_spkr_prot_on_device(snd_device) &&
1508 audio_extn_spkr_prot_is_enabled()) {
Anish Kumar46c7b872014-09-09 01:49:44 -07001509 audio_extn_spkr_prot_stop_processing(snd_device);
Vignesh Kulothungan3b5fae52017-09-25 12:16:30 -07001510
1511 // when speaker device is disabled, reset swap.
1512 // will be renabled on usecase start
1513 platform_set_swap_channels(adev, false);
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001514 } else if (platform_split_snd_device(adev->platform,
1515 snd_device,
1516 &num_devices,
1517 new_snd_devices) == 0) {
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301518 for (i = 0; i < num_devices; i++) {
1519 disable_snd_device(adev, new_snd_devices[i]);
1520 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001521 platform_set_speaker_gain_in_combo(adev, snd_device, false);
Lior Barenboim0b61bc72014-05-13 13:01:37 +03001522 } else {
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07001523 audio_route_reset_and_update_path(adev->audio_route, device_name);
Lior Barenboim0b61bc72014-05-13 13:01:37 +03001524 }
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001525
Revathi Uddarajub26e3932020-06-10 14:51:02 +05301526 if (snd_device == SND_DEVICE_OUT_BT_A2DP) {
Naresh Tannirucd2353e2016-08-19 00:37:25 +05301527 audio_extn_a2dp_stop_playback();
Revathi Uddarajub26e3932020-06-10 14:51:02 +05301528 adev->a2dp_started = false;
1529 } else if (snd_device == SND_DEVICE_IN_BT_A2DP)
Florian Pfister1a84f312018-07-19 14:38:18 +02001530 audio_extn_a2dp_stop_capture();
Zhou Songd6d71752019-05-21 18:08:51 +08001531 else if ((snd_device == SND_DEVICE_OUT_HDMI) ||
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001532 (snd_device == SND_DEVICE_OUT_DISPLAY_PORT))
Ashish Jain81eb2a82015-05-13 10:52:34 +05301533 adev->is_channel_status_set = false;
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001534 else if ((snd_device == SND_DEVICE_OUT_HEADPHONES) &&
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301535 adev->native_playback_enabled) {
1536 ALOGD("%s: %d: napb: disabling native mode in hardware",
1537 __func__, __LINE__);
1538 audio_route_reset_and_update_path(adev->audio_route,
1539 "true-native-mode");
1540 adev->native_playback_enabled = false;
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001541 } else if ((snd_device == SND_DEVICE_OUT_HEADPHONES) &&
Preetam Singh Ranawatb0c0dd72016-08-18 00:32:06 +05301542 adev->asrc_mode_enabled) {
1543 ALOGD("%s: %d: disabling asrc mode in hardware", __func__, __LINE__);
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +05301544 disable_asrc_mode(adev);
1545 audio_route_apply_and_update_path(adev->audio_route, "hph-lowpower-mode");
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001546 } else if (((snd_device == SND_DEVICE_IN_HANDSET_6MIC) ||
Dhanalakshmi Siddaniaf4bd622019-02-27 16:28:06 +05301547 (snd_device == SND_DEVICE_IN_HANDSET_QMIC)) &&
Eric Laurent637e2d42018-11-15 12:24:31 -08001548 (audio_extn_ffv_get_stream() == adev_get_active_input(adev))) {
Garmond Leunge2433c32017-09-28 21:51:22 -07001549 ALOGD("%s: deinit ec ref loopback", __func__);
1550 audio_extn_ffv_deinit_ec_ref_loopback(adev, snd_device);
1551 }
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001552
1553 audio_extn_utils_release_snd_device(snd_device);
1554 } else {
1555 if (platform_split_snd_device(adev->platform,
1556 snd_device,
1557 &num_devices,
1558 new_snd_devices) == 0) {
1559 for (i = 0; i < num_devices; i++) {
1560 adev->snd_dev_ref_cnt[new_snd_devices[i]]--;
1561 }
1562 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001563 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001564
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001565 return 0;
1566}
1567
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001568/*
1569 legend:
1570 uc - existing usecase
1571 new_uc - new usecase
1572 d1, d11, d2 - SND_DEVICE enums
1573 a1, a2 - corresponding ANDROID device enums
1574 B1, B2 - backend strings
1575
1576case 1
1577 uc->dev d1 (a1) B1
1578 new_uc->dev d1 (a1), d2 (a2) B1, B2
1579
1580 resolution: disable and enable uc->dev on d1
1581
1582case 2
1583 uc->dev d1 (a1) B1
1584 new_uc->dev d11 (a1) B1
1585
1586 resolution: need to switch uc since d1 and d11 are related
1587 (e.g. speaker and voice-speaker)
1588 use ANDROID_DEVICE_OUT enums to match devices since SND_DEVICE enums may vary
1589
1590case 3
1591 uc->dev d1 (a1) B1
1592 new_uc->dev d2 (a2) B2
1593
1594 resolution: no need to switch uc
1595
1596case 4
1597 uc->dev d1 (a1) B1
1598 new_uc->dev d2 (a2) B1
1599
1600 resolution: disable enable uc-dev on d2 since backends match
1601 we cannot enable two streams on two different devices if they
1602 share the same backend. e.g. if offload is on speaker device using
1603 QUAD_MI2S backend and a low-latency stream is started on voice-handset
1604 using the same backend, offload must also be switched to voice-handset.
1605
1606case 5
1607 uc->dev d1 (a1) B1
1608 new_uc->dev d1 (a1), d2 (a2) B1
1609
1610 resolution: disable enable uc-dev on d2 since backends match
1611 we cannot enable two streams on two different devices if they
1612 share the same backend.
1613
1614case 6
1615 uc->dev d1 (a1) B1
1616 new_uc->dev d2 (a1) B2
1617
1618 resolution: no need to switch
1619
1620case 7
1621 uc->dev d1 (a1), d2 (a2) B1, B2
1622 new_uc->dev d1 (a1) B1
1623
1624 resolution: no need to switch
1625
Zhou Song4ba65882018-07-09 14:48:07 +08001626case 8
1627 uc->dev d1 (a1) B1
1628 new_uc->dev d11 (a1), d2 (a2) B1, B2
1629 resolution: compared to case 1, for this case, d1 and d11 are related
1630 then need to do the same as case 2 to siwtch to new uc
Preetam Singh Ranawatd399e6f2020-06-25 12:20:36 +05301631
1632case 9
1633 uc->dev d1 (a1), d2(a2) B1 B2
1634 new_uc->dev d1 (a1), d22 (a2) B1, B2
1635 resolution: disable enable uc-dev on d2 since backends match
1636 we cannot enable two streams on two different devices if they
1637 share the same backend. This is special case for combo use case
1638 with a2dp and sco devices which uses same backend.
1639 e.g. speaker-a2dp and speaker-btsco
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001640*/
1641static snd_device_t derive_playback_snd_device(void * platform,
1642 struct audio_usecase *uc,
1643 struct audio_usecase *new_uc,
1644 snd_device_t new_snd_device)
1645{
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001646 struct listnode a1, a2;
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001647
1648 snd_device_t d1 = uc->out_snd_device;
1649 snd_device_t d2 = new_snd_device;
1650
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001651 list_init(&a1);
1652 list_init(&a2);
1653
Siddartha Shaik31b530e2017-05-19 15:26:33 +05301654 switch (uc->type) {
Surendar Karka93cd25a2018-08-28 14:21:37 +05301655 case TRANSCODE_LOOPBACK_RX :
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001656 assign_devices(&a1, &uc->stream.inout->out_config.device_list);
1657 assign_devices(&a2, &new_uc->stream.inout->out_config.device_list);
Siddartha Shaik31b530e2017-05-19 15:26:33 +05301658 break;
1659 default :
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001660 assign_devices(&a1, &uc->stream.out->device_list);
1661 assign_devices(&a2, &new_uc->stream.out->device_list);
Siddartha Shaik31b530e2017-05-19 15:26:33 +05301662 break;
1663 }
1664
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001665 // Treat as a special case when a1 and a2 are not disjoint
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001666 if (!compare_devices(&a1, &a2) &&
1667 compare_devices_for_any_match(&a1 ,&a2)) {
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001668 snd_device_t d3[2];
1669 int num_devices = 0;
1670 int ret = platform_split_snd_device(platform,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001671 list_length(&a1) > 1 ? d1 : d2,
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001672 &num_devices,
1673 d3);
1674 if (ret < 0) {
1675 if (ret != -ENOSYS) {
1676 ALOGW("%s failed to split snd_device %d",
1677 __func__,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001678 list_length(&a1) > 1 ? d1 : d2);
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001679 }
1680 goto end;
1681 }
1682
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001683 if (platform_check_backends_match(d3[0], d3[1])) {
1684 return d2; // case 5
1685 } else {
Preetam Singh Ranawatd399e6f2020-06-25 12:20:36 +05301686 if ((list_length(&a1) > 1) && (list_length(&a2) > 1) &&
1687 platform_check_backends_match(d1, d2))
1688 return d2; //case 9
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001689 if (list_length(&a1) > 1)
Samyak Jaind826b502019-07-17 16:16:42 +05301690 return d1; //case 7
Garmond Leungb9eeba42018-09-18 11:10:41 -07001691 // check if d1 is related to any of d3's
1692 if (d1 == d3[0] || d1 == d3[1])
Zhou Song4ba65882018-07-09 14:48:07 +08001693 return d1; // case 1
1694 else
1695 return d3[1]; // case 8
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001696 }
1697 } else {
1698 if (platform_check_backends_match(d1, d2)) {
1699 return d2; // case 2, 4
1700 } else {
1701 return d1; // case 6, 3
1702 }
1703 }
1704
1705end:
1706 return d2; // return whatever was calculated before.
1707}
1708
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001709static void check_usecases_codec_backend(struct audio_device *adev,
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301710 struct audio_usecase *uc_info,
1711 snd_device_t snd_device)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001712{
1713 struct listnode *node;
1714 struct audio_usecase *usecase;
1715 bool switch_device[AUDIO_USECASE_MAX];
Chaithanya Krishna Bacharaju49e7db02017-03-14 11:57:26 +05301716 snd_device_t uc_derive_snd_device;
1717 snd_device_t derive_snd_device[AUDIO_USECASE_MAX];
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001718 snd_device_t split_snd_devices[SND_DEVICE_OUT_END];
1719 int i, num_uc_to_switch = 0, num_devices = 0;
kunleiz5cd52b82016-11-07 17:22:52 +08001720 int status = 0;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301721 bool force_restart_session = false;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001722 /*
1723 * This function is to make sure that all the usecases that are active on
1724 * the hardware codec backend are always routed to any one device that is
1725 * handled by the hardware codec.
1726 * For example, if low-latency and deep-buffer usecases are currently active
1727 * on speaker and out_set_parameters(headset) is received on low-latency
1728 * output, then we have to make sure deep-buffer is also switched to headset,
1729 * because of the limitation that both the devices cannot be enabled
1730 * at the same time as they share the same backend.
1731 */
Mingming Yin3ee55c62014-08-04 14:23:35 -07001732 /*
1733 * This call is to check if we need to force routing for a particular stream
1734 * If there is a backend configuration change for the device when a
1735 * new stream starts, then ADM needs to be closed and re-opened with the new
1736 * configuraion. This call check if we need to re-route all the streams
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -08001737 * associated with the backend. Touch tone + 24 bit + native playback.
Mingming Yin3ee55c62014-08-04 14:23:35 -07001738 */
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -08001739 bool force_routing = platform_check_and_set_codec_backend_cfg(adev, uc_info,
1740 snd_device);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301741 /* For a2dp device reconfigure all active sessions
1742 * with new AFE encoder format based on a2dp state
1743 */
1744 if ((SND_DEVICE_OUT_BT_A2DP == snd_device ||
Preetam Singh Ranawatd399e6f2020-06-25 12:20:36 +05301745 SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP == snd_device ||
1746 SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP == snd_device) &&
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301747 audio_extn_a2dp_is_force_device_switch()) {
1748 force_routing = true;
1749 force_restart_session = true;
1750 }
Kunlei Zhange7cab1a2020-05-17 20:31:05 +08001751
1752 /*
1753 * Island cfg and power mode config needs to set before AFE port start.
1754 * Set force routing in case of voice device was enable before.
1755 */
1756 if (uc_info->type == VOICE_CALL &&
1757 voice_extn_is_voice_power_mode_supported() &&
1758 platform_check_and_update_island_power_status(adev->platform,
1759 uc_info,
1760 snd_device)) {
1761 force_routing = true;
1762 ALOGD("%s:becf: force routing %d for power mode supported device",
1763 __func__, force_routing);
1764 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301765 ALOGD("%s:becf: force routing %d", __func__, force_routing);
1766
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001767 /* Disable all the usecases on the shared backend other than the
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -08001768 * specified usecase.
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -08001769 */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001770 for (i = 0; i < AUDIO_USECASE_MAX; i++)
1771 switch_device[i] = false;
1772
1773 list_for_each(node, &adev->usecase_list) {
1774 usecase = node_to_item(node, struct audio_usecase, list);
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -08001775
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301776 ALOGD("%s:becf: (%d) check_usecases curr device: %s, usecase device:%s "
1777 "backends match %d",__func__, i,
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301778 platform_get_snd_device_name(snd_device),
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301779 platform_get_snd_device_name(usecase->out_snd_device),
1780 platform_check_backends_match(snd_device, usecase->out_snd_device));
Rahul Sharma99770982019-03-06 17:05:26 +05301781 if ((usecase->type != PCM_CAPTURE) && (usecase != uc_info) &&
1782 (usecase->type != PCM_PASSTHROUGH)) {
Ashish Jain6a65b352017-03-21 17:24:40 +05301783 uc_derive_snd_device = derive_playback_snd_device(adev->platform,
1784 usecase, uc_info, snd_device);
1785 if (((uc_derive_snd_device != usecase->out_snd_device) || force_routing) &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001786 (is_codec_backend_out_device_type(&usecase->device_list) ||
1787 compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_AUX_DIGITAL) ||
1788 compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_USB_DEVICE) ||
1789 compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_USB_HEADSET) ||
1790 is_a2dp_out_device_type(&usecase->device_list) ||
1791 is_sco_out_device_type(&usecase->device_list)) &&
Ashish Jain6a65b352017-03-21 17:24:40 +05301792 ((force_restart_session) ||
1793 (platform_check_backends_match(snd_device, usecase->out_snd_device)))) {
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301794 ALOGD("%s:becf: check_usecases (%s) is active on (%s) - disabling ..",
1795 __func__, use_case_table[usecase->id],
1796 platform_get_snd_device_name(usecase->out_snd_device));
1797 disable_audio_route(adev, usecase);
1798 switch_device[usecase->id] = true;
Chaithanya Krishna Bacharaju49e7db02017-03-14 11:57:26 +05301799 /* Enable existing usecase on derived playback device */
1800 derive_snd_device[usecase->id] = uc_derive_snd_device;
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301801 num_uc_to_switch++;
Ashish Jain6a65b352017-03-21 17:24:40 +05301802 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001803 }
1804 }
1805
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301806 ALOGD("%s:becf: check_usecases num.of Usecases to switch %d", __func__,
1807 num_uc_to_switch);
1808
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001809 if (num_uc_to_switch) {
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07001810 /* All streams have been de-routed. Disable the device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001811
Venkata Narendra Kumar Gutta7610e632014-04-14 23:16:38 +05301812 /* Make sure the previous devices to be disabled first and then enable the
1813 selected devices */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001814 list_for_each(node, &adev->usecase_list) {
1815 usecase = node_to_item(node, struct audio_usecase, list);
1816 if (switch_device[usecase->id]) {
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001817 /* Check if output sound device to be switched can be split and if any
1818 of the split devices match with derived sound device */
1819 if (platform_split_snd_device(adev->platform, usecase->out_snd_device,
1820 &num_devices, split_snd_devices) == 0) {
1821 adev->snd_dev_ref_cnt[usecase->out_snd_device]--;
1822 for (i = 0; i < num_devices; i++) {
1823 /* Disable devices that do not match with derived sound device */
1824 if (split_snd_devices[i] != derive_snd_device[usecase->id])
1825 disable_snd_device(adev, split_snd_devices[i]);
1826 }
1827 } else {
1828 disable_snd_device(adev, usecase->out_snd_device);
1829 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001830 }
1831 }
1832
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -07001833 list_for_each(node, &adev->usecase_list) {
1834 usecase = node_to_item(node, struct audio_usecase, list);
1835 if (switch_device[usecase->id]) {
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001836 if (platform_split_snd_device(adev->platform, usecase->out_snd_device,
1837 &num_devices, split_snd_devices) == 0) {
1838 /* Enable derived sound device only if it does not match with
1839 one of the split sound devices. This is because the matching
1840 sound device was not disabled */
1841 bool should_enable = true;
1842 for (i = 0; i < num_devices; i++) {
1843 if (derive_snd_device[usecase->id] == split_snd_devices[i]) {
1844 should_enable = false;
1845 break;
1846 }
1847 }
1848 if (should_enable)
1849 enable_snd_device(adev, derive_snd_device[usecase->id]);
1850 } else {
1851 enable_snd_device(adev, derive_snd_device[usecase->id]);
1852 }
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -07001853 }
1854 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001855
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001856 /* Re-route all the usecases on the shared backend other than the
1857 specified usecase to new snd devices */
1858 list_for_each(node, &adev->usecase_list) {
1859 usecase = node_to_item(node, struct audio_usecase, list);
Karthik Reddy Katta3abfee22016-02-23 10:55:27 +05301860 /* Update the out_snd_device only before enabling the audio route */
1861 if (switch_device[usecase->id]) {
Chaithanya Krishna Bacharaju49e7db02017-03-14 11:57:26 +05301862 usecase->out_snd_device = derive_snd_device[usecase->id];
Lakshman Chaluvaraju4c29a492020-10-28 19:29:23 +05301863 ALOGD("%s:becf: enabling usecase (%s) on (%s)", __func__,
1864 use_case_table[usecase->id],
1865 platform_get_snd_device_name(usecase->out_snd_device));
1866 /* Update voc calibration before enabling Voice/VoIP route */
1867 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL)
1868 status = platform_switch_voice_call_device_post(adev->platform,
1869 usecase->out_snd_device,
1870 platform_get_input_snd_device(
1871 adev->platform, NULL,
1872 &uc_info->device_list,
1873 usecase->type));
1874 enable_audio_route(adev, usecase);
1875 if (usecase->stream.out && usecase->id == USECASE_AUDIO_PLAYBACK_VOIP) {
1876 out_set_voip_volume(&usecase->stream.out->stream,
1877 usecase->stream.out->volume_l,
1878 usecase->stream.out->volume_r);
Karthik Reddy Katta3abfee22016-02-23 10:55:27 +05301879 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001880 }
1881 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001882 }
1883}
1884
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05301885static void check_usecases_capture_codec_backend(struct audio_device *adev,
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001886 struct audio_usecase *uc_info,
1887 snd_device_t snd_device)
1888{
1889 struct listnode *node;
1890 struct audio_usecase *usecase;
1891 bool switch_device[AUDIO_USECASE_MAX];
1892 int i, num_uc_to_switch = 0;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001893 int backend_check_cond = is_codec_backend_out_device_type(&uc_info->device_list);
kunleiz5cd52b82016-11-07 17:22:52 +08001894 int status = 0;
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001895
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05301896 bool force_routing = platform_check_and_set_capture_codec_backend_cfg(adev, uc_info,
1897 snd_device);
1898 ALOGD("%s:becf: force routing %d", __func__, force_routing);
Dhanalakshmi Siddanib678a802016-12-03 11:51:41 +05301899
1900 /*
1901 * Make sure out devices is checked against out codec backend device and
1902 * also in devices against in codec backend. Checking out device against in
1903 * codec backend or vice versa causes issues.
1904 */
1905 if (uc_info->type == PCM_CAPTURE)
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001906 backend_check_cond = is_codec_backend_in_device_type(&uc_info->device_list);
Kunlei Zhange7cab1a2020-05-17 20:31:05 +08001907
1908 /*
1909 * Island cfg and power mode config needs to set before AFE port start.
1910 * Set force routing in case of voice device was enable before.
1911 */
1912
1913 if (uc_info->type == VOICE_CALL &&
1914 voice_extn_is_voice_power_mode_supported() &&
1915 platform_check_and_update_island_power_status(adev->platform,
1916 uc_info,
1917 snd_device)) {
1918 force_routing = true;
1919 ALOGD("%s:becf: force routing %d for power mode supported device",
1920 __func__, force_routing);
1921 }
1922
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001923 /*
1924 * This function is to make sure that all the active capture usecases
1925 * are always routed to the same input sound device.
1926 * For example, if audio-record and voice-call usecases are currently
1927 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
1928 * is received for voice call then we have to make sure that audio-record
1929 * usecase is also switched to earpiece i.e. voice-dmic-ef,
1930 * because of the limitation that two devices cannot be enabled
1931 * at the same time if they share the same backend.
1932 */
1933 for (i = 0; i < AUDIO_USECASE_MAX; i++)
1934 switch_device[i] = false;
1935
1936 list_for_each(node, &adev->usecase_list) {
1937 usecase = node_to_item(node, struct audio_usecase, list);
Dhanalakshmi Siddanib678a802016-12-03 11:51:41 +05301938 /*
1939 * TODO: Enhance below condition to handle BT sco/USB multi recording
1940 */
Jaideep Sharma477917f2020-03-13 18:13:33 +05301941
1942 bool capture_uc_needs_routing = usecase->type != PCM_PLAYBACK && (usecase != uc_info &&
1943 (usecase->in_snd_device != snd_device || force_routing));
1944 bool call_proxy_snd_device = platform_is_call_proxy_snd_device(snd_device) ||
1945 platform_is_call_proxy_snd_device(usecase->in_snd_device);
1946 if (capture_uc_needs_routing && !call_proxy_snd_device &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001947 ((backend_check_cond &&
1948 (is_codec_backend_in_device_type(&usecase->device_list) ||
Aniket Kumar Lata9c2fd892020-01-22 22:20:00 -08001949 (usecase->type == VOIP_CALL))) ||
Carter Hsu1d2a0532018-10-04 09:24:36 +08001950 ((uc_info->type == VOICE_CALL &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001951 is_single_device_type_equal(&usecase->device_list,
1952 AUDIO_DEVICE_IN_VOICE_CALL)) ||
Carter Hsu1d2a0532018-10-04 09:24:36 +08001953 platform_check_backends_match(snd_device,\
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001954 usecase->in_snd_device))) &&
Anish Kumara020a7c2014-10-17 11:13:22 -07001955 (usecase->id != USECASE_AUDIO_SPKR_CALIB_TX)) {
Jaideep Sharma477917f2020-03-13 18:13:33 +05301956 ALOGD("%s: Usecase (%s) is active on (%s) - disabling ..",
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001957 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -07001958 platform_get_snd_device_name(usecase->in_snd_device));
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001959 disable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001960 switch_device[usecase->id] = true;
1961 num_uc_to_switch++;
1962 }
1963 }
1964
1965 if (num_uc_to_switch) {
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07001966 /* All streams have been de-routed. Disable the device */
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001967
Venkata Narendra Kumar Gutta7610e632014-04-14 23:16:38 +05301968 /* Make sure the previous devices to be disabled first and then enable the
1969 selected devices */
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001970 list_for_each(node, &adev->usecase_list) {
1971 usecase = node_to_item(node, struct audio_usecase, list);
1972 if (switch_device[usecase->id]) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001973 disable_snd_device(adev, usecase->in_snd_device);
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -08001974 }
1975 }
1976
1977 list_for_each(node, &adev->usecase_list) {
1978 usecase = node_to_item(node, struct audio_usecase, list);
1979 if (switch_device[usecase->id]) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001980 enable_snd_device(adev, snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001981 }
1982 }
1983
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001984 /* Re-route all the usecases on the shared backend other than the
1985 specified usecase to new snd devices */
1986 list_for_each(node, &adev->usecase_list) {
1987 usecase = node_to_item(node, struct audio_usecase, list);
1988 /* Update the in_snd_device only before enabling the audio route */
1989 if (switch_device[usecase->id] ) {
1990 usecase->in_snd_device = snd_device;
Lakshman Chaluvaraju4c29a492020-10-28 19:29:23 +05301991 /* Update voc calibration before enabling Voice/VoIP route */
1992 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) {
1993 snd_device_t voip_snd_device;
1994 voip_snd_device = platform_get_output_snd_device(adev->platform,
1995 usecase->stream.out,
1996 usecase->type);
1997 status = platform_switch_voice_call_device_post(adev->platform,
1998 voip_snd_device,
1999 usecase->in_snd_device);
kunleiz5cd52b82016-11-07 17:22:52 +08002000 }
Lakshman Chaluvaraju4c29a492020-10-28 19:29:23 +05302001 enable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002002 }
2003 }
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002004 }
2005}
2006
Mingming Yin3a941d42016-02-17 18:08:05 -08002007static void reset_hdmi_sink_caps(struct stream_out *out) {
2008 int i = 0;
2009
2010 for (i = 0; i<= MAX_SUPPORTED_CHANNEL_MASKS; i++) {
2011 out->supported_channel_masks[i] = 0;
2012 }
2013 for (i = 0; i<= MAX_SUPPORTED_FORMATS; i++) {
2014 out->supported_formats[i] = 0;
2015 }
2016 for (i = 0; i<= MAX_SUPPORTED_SAMPLE_RATES; i++) {
2017 out->supported_sample_rates[i] = 0;
2018 }
2019}
2020
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002021/* must be called with hw device mutex locked */
Mingming Yin3a941d42016-02-17 18:08:05 -08002022static int read_hdmi_sink_caps(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002023{
Mingming Yin3a941d42016-02-17 18:08:05 -08002024 int ret = 0, i = 0, j = 0;
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07002025 int channels = platform_edid_get_max_channels_v2(out->dev->platform,
2026 out->extconn.cs.controller,
2027 out->extconn.cs.stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002028
Mingming Yin3a941d42016-02-17 18:08:05 -08002029 reset_hdmi_sink_caps(out);
2030
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07002031 /* Cache ext disp type */
Vignesh Kulothungan1b3957e2019-12-02 16:16:51 -08002032 ret = platform_get_ext_disp_type_v2(adev->platform,
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07002033 out->extconn.cs.controller,
Vignesh Kulothungan1b3957e2019-12-02 16:16:51 -08002034 out->extconn.cs.stream);
2035 if(ret < 0) {
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07002036 ALOGE("%s: Failed to query disp type, ret:%d", __func__, ret);
Garmond Leung37850ab2016-10-06 11:42:18 -07002037 return -EINVAL;
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07002038 }
2039
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002040 switch (channels) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002041 case 8:
Mingming Yin3a941d42016-02-17 18:08:05 -08002042 ALOGV("%s: HDMI supports 7.1 channels", __func__);
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -07002043 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_7POINT1;
Mingming Yin3a941d42016-02-17 18:08:05 -08002044 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_6POINT1;
2045 case 6:
2046 ALOGV("%s: HDMI supports 5.1 channels", __func__);
2047 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_5POINT1;
2048 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_PENTA;
2049 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_QUAD;
2050 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_SURROUND;
2051 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_2POINT1;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002052 break;
2053 default:
Mingming Yin3a941d42016-02-17 18:08:05 -08002054 ALOGE("invalid/nonstandard channal count[%d]",channels);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002055 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002056 break;
2057 }
Mingming Yin3a941d42016-02-17 18:08:05 -08002058
2059 // check channel format caps
2060 i = 0;
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07002061 if (platform_is_edid_supported_format_v2(out->dev->platform, AUDIO_FORMAT_AC3,
2062 out->extconn.cs.controller,
2063 out->extconn.cs.stream)) {
Mingming Yin3a941d42016-02-17 18:08:05 -08002064 ALOGV(":%s HDMI supports AC3/EAC3 formats", __func__);
2065 out->supported_formats[i++] = AUDIO_FORMAT_AC3;
2066 //Adding EAC3/EAC3_JOC formats if AC3 is supported by the sink.
2067 //EAC3/EAC3_JOC will be converted to AC3 for decoding if needed
2068 out->supported_formats[i++] = AUDIO_FORMAT_E_AC3;
2069 out->supported_formats[i++] = AUDIO_FORMAT_E_AC3_JOC;
2070 }
2071
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07002072 if (platform_is_edid_supported_format_v2(out->dev->platform, AUDIO_FORMAT_DOLBY_TRUEHD,
2073 out->extconn.cs.controller,
2074 out->extconn.cs.stream)) {
Ben Romberger1aaaf862017-04-06 17:49:46 -07002075 ALOGV(":%s HDMI supports TRUE HD format", __func__);
2076 out->supported_formats[i++] = AUDIO_FORMAT_DOLBY_TRUEHD;
2077 }
2078
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07002079 if (platform_is_edid_supported_format_v2(out->dev->platform, AUDIO_FORMAT_DTS,
2080 out->extconn.cs.controller,
2081 out->extconn.cs.stream)) {
Mingming Yin3a941d42016-02-17 18:08:05 -08002082 ALOGV(":%s HDMI supports DTS format", __func__);
2083 out->supported_formats[i++] = AUDIO_FORMAT_DTS;
2084 }
2085
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07002086 if (platform_is_edid_supported_format_v2(out->dev->platform, AUDIO_FORMAT_DTS_HD,
2087 out->extconn.cs.controller,
2088 out->extconn.cs.stream)) {
Mingming Yin3a941d42016-02-17 18:08:05 -08002089 ALOGV(":%s HDMI supports DTS HD format", __func__);
2090 out->supported_formats[i++] = AUDIO_FORMAT_DTS_HD;
2091 }
2092
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07002093 if (platform_is_edid_supported_format_v2(out->dev->platform, AUDIO_FORMAT_IEC61937,
2094 out->extconn.cs.controller,
2095 out->extconn.cs.stream)) {
Naresh Tanniru928f0862017-04-07 16:44:23 -07002096 ALOGV(":%s HDMI supports IEC61937 format", __func__);
2097 out->supported_formats[i++] = AUDIO_FORMAT_IEC61937;
2098 }
2099
Mingming Yin3a941d42016-02-17 18:08:05 -08002100
2101 // check sample rate caps
2102 i = 0;
2103 for (j = 0; j < MAX_SUPPORTED_SAMPLE_RATES; j++) {
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07002104 if (platform_is_edid_supported_sample_rate_v2(out->dev->platform, out_hdmi_sample_rates[j],
2105 out->extconn.cs.controller,
2106 out->extconn.cs.stream)) {
Mingming Yin3a941d42016-02-17 18:08:05 -08002107 ALOGV(":%s HDMI supports sample rate:%d", __func__, out_hdmi_sample_rates[j]);
2108 out->supported_sample_rates[i++] = out_hdmi_sample_rates[j];
2109 }
2110 }
2111
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002112 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002113}
2114
Haynes Mathew George484e8d22017-07-31 18:55:17 -07002115static inline ssize_t read_usb_sup_sample_rates(bool is_playback __unused,
2116 uint32_t *supported_sample_rates __unused,
2117 uint32_t max_rates __unused)
2118{
2119 ssize_t count = audio_extn_usb_get_sup_sample_rates(is_playback,
2120 supported_sample_rates,
2121 max_rates);
Ashish Jain4847e9d2017-08-17 19:16:57 +05302122 ssize_t i = 0;
2123
2124 for (i=0; i<count; i++) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07002125 ALOGV("%s %s %d", __func__, is_playback ? "P" : "C",
2126 supported_sample_rates[i]);
2127 }
2128 return count;
2129}
2130
2131static inline int read_usb_sup_channel_masks(bool is_playback,
2132 audio_channel_mask_t *supported_channel_masks,
2133 uint32_t max_masks)
2134{
2135 int channels = audio_extn_usb_get_max_channels(is_playback);
2136 int channel_count;
2137 uint32_t num_masks = 0;
2138 if (channels > MAX_HIFI_CHANNEL_COUNT)
2139 channels = MAX_HIFI_CHANNEL_COUNT;
2140
2141 if (is_playback) {
Eric Laurent68a87112019-05-01 18:07:29 -07002142 // start from 2 channels as framework currently doesn't support mono.
2143 if (channels >= FCC_2) {
2144 supported_channel_masks[num_masks++] = audio_channel_out_mask_from_count(FCC_2);
2145 }
2146 for (channel_count = FCC_2;
2147 channel_count <= channels && num_masks < max_masks;
2148 ++channel_count) {
2149 supported_channel_masks[num_masks++] =
2150 audio_channel_mask_for_index_assignment_from_count(channel_count);
2151 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07002152 } else {
vincenttewf51c94e2019-05-07 10:28:53 +08002153 // For capture we report all supported channel masks from 1 channel up.
2154 channel_count = MIN_CHANNEL_COUNT;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07002155 // audio_channel_in_mask_from_count() does the right conversion to either positional or
2156 // indexed mask
Eric Laurent68a87112019-05-01 18:07:29 -07002157 for ( ; channel_count <= channels && num_masks < max_masks; channel_count++) {
2158 audio_channel_mask_t mask = AUDIO_CHANNEL_NONE;
2159 if (channel_count <= FCC_2) {
2160 mask = audio_channel_in_mask_from_count(channel_count);
2161 supported_channel_masks[num_masks++] = mask;
2162 }
2163 const audio_channel_mask_t index_mask =
2164 audio_channel_mask_for_index_assignment_from_count(channel_count);
2165 if (mask != index_mask && num_masks < max_masks) { // ensure index mask added.
2166 supported_channel_masks[num_masks++] = index_mask;
2167 }
2168 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07002169 }
Lakshman Chaluvarajub79fafd2018-11-21 10:24:37 +05302170
vincenttewf51c94e2019-05-07 10:28:53 +08002171 for (size_t i = 0; i < num_masks; ++i) {
2172 ALOGV("%s: %s supported ch %d supported_channel_masks[%zu] %08x num_masks %d", __func__,
2173 is_playback ? "P" : "C", channels, i, supported_channel_masks[i], num_masks);
Lakshman Chaluvarajub79fafd2018-11-21 10:24:37 +05302174 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07002175 return num_masks;
2176}
2177
2178static inline int read_usb_sup_formats(bool is_playback __unused,
2179 audio_format_t *supported_formats,
2180 uint32_t max_formats __unused)
2181{
2182 int bitwidth = audio_extn_usb_get_max_bit_width(is_playback);
2183 switch (bitwidth) {
2184 case 24:
2185 // XXX : usb.c returns 24 for s24 and s24_le?
2186 supported_formats[0] = AUDIO_FORMAT_PCM_24_BIT_PACKED;
2187 break;
2188 case 32:
2189 supported_formats[0] = AUDIO_FORMAT_PCM_32_BIT;
2190 break;
2191 case 16:
2192 default :
2193 supported_formats[0] = AUDIO_FORMAT_PCM_16_BIT;
2194 break;
2195 }
2196 ALOGV("%s: %s supported format %d", __func__,
2197 is_playback ? "P" : "C", bitwidth);
2198 return 1;
2199}
2200
2201static inline int read_usb_sup_params_and_compare(bool is_playback,
2202 audio_format_t *format,
2203 audio_format_t *supported_formats,
2204 uint32_t max_formats,
2205 audio_channel_mask_t *mask,
2206 audio_channel_mask_t *supported_channel_masks,
2207 uint32_t max_masks,
2208 uint32_t *rate,
2209 uint32_t *supported_sample_rates,
2210 uint32_t max_rates) {
2211 int ret = 0;
2212 int num_formats;
2213 int num_masks;
2214 int num_rates;
2215 int i;
2216
2217 num_formats = read_usb_sup_formats(is_playback, supported_formats,
2218 max_formats);
2219 num_masks = read_usb_sup_channel_masks(is_playback, supported_channel_masks,
2220 max_masks);
2221
2222 num_rates = read_usb_sup_sample_rates(is_playback,
2223 supported_sample_rates, max_rates);
2224
2225#define LUT(table, len, what, dflt) \
2226 for (i=0; i<len && (table[i] != what); i++); \
2227 if (i==len) { ret |= (what == dflt ? 0 : -1); what=table[0]; }
2228
2229 LUT(supported_formats, num_formats, *format, AUDIO_FORMAT_DEFAULT);
2230 LUT(supported_channel_masks, num_masks, *mask, AUDIO_CHANNEL_NONE);
2231 LUT(supported_sample_rates, num_rates, *rate, 0);
2232
2233#undef LUT
2234 return ret < 0 ? -EINVAL : 0; // HACK TBD
2235}
2236
Alexy Josephb1379942016-01-29 15:49:38 -08002237audio_usecase_t get_usecase_id_from_usecase_type(const struct audio_device *adev,
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -08002238 usecase_type_t type)
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002239{
2240 struct audio_usecase *usecase;
2241 struct listnode *node;
2242
2243 list_for_each(node, &adev->usecase_list) {
2244 usecase = node_to_item(node, struct audio_usecase, list);
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -08002245 if (usecase->type == type) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002246 ALOGV("%s: usecase id %d", __func__, usecase->id);
2247 return usecase->id;
2248 }
2249 }
2250 return USECASE_INVALID;
2251}
2252
Alexy Josephb1379942016-01-29 15:49:38 -08002253struct audio_usecase *get_usecase_from_list(const struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002254 audio_usecase_t uc_id)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002255{
2256 struct audio_usecase *usecase;
2257 struct listnode *node;
2258
2259 list_for_each(node, &adev->usecase_list) {
2260 usecase = node_to_item(node, struct audio_usecase, list);
2261 if (usecase->id == uc_id)
2262 return usecase;
2263 }
2264 return NULL;
2265}
2266
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302267/*
2268 * is a true native playback active
2269 */
2270bool audio_is_true_native_stream_active(struct audio_device *adev)
2271{
2272 bool active = false;
2273 int i = 0;
2274 struct listnode *node;
2275
2276 if (NATIVE_AUDIO_MODE_TRUE_44_1 != platform_get_native_support()) {
2277 ALOGV("%s:napb: not in true mode or non hdphones device",
2278 __func__);
2279 active = false;
2280 goto exit;
2281 }
2282
2283 list_for_each(node, &adev->usecase_list) {
2284 struct audio_usecase *uc;
2285 uc = node_to_item(node, struct audio_usecase, list);
2286 struct stream_out *curr_out =
2287 (struct stream_out*) uc->stream.out;
2288
2289 if (curr_out && PCM_PLAYBACK == uc->type) {
2290 ALOGD("%s:napb: (%d) (%s)id (%d) sr %d bw "
2291 "(%d) device %s", __func__, i++, use_case_table[uc->id],
2292 uc->id, curr_out->sample_rate,
2293 curr_out->bit_width,
2294 platform_get_snd_device_name(uc->out_snd_device));
2295
2296 if (is_offload_usecase(uc->id) &&
2297 (curr_out->sample_rate == OUTPUT_SAMPLING_RATE_44100)) {
2298 active = true;
2299 ALOGD("%s:napb:native stream detected", __func__);
2300 }
2301 }
2302 }
2303exit:
2304 return active;
2305}
2306
Xiaojun Sang785b5da2017-08-03 15:52:29 +08002307uint32_t adev_get_dsp_bit_width_enforce_mode()
2308{
2309 if (adev == NULL) {
2310 ALOGE("%s: adev is null. Disable DSP bit width enforce mode.\n", __func__);
2311 return 0;
2312 }
2313 return adev->dsp_bit_width_enforce_mode;
2314}
2315
2316static uint32_t adev_init_dsp_bit_width_enforce_mode(struct mixer *mixer)
2317{
2318 char value[PROPERTY_VALUE_MAX];
2319 int trial;
2320 uint32_t dsp_bit_width_enforce_mode = 0;
2321
2322 if (!mixer) {
2323 ALOGE("%s: adev mixer is null. cannot update DSP bitwidth.\n",
2324 __func__);
2325 return 0;
2326 }
2327
2328 if (property_get("persist.vendor.audio_hal.dsp_bit_width_enforce_mode",
2329 value, NULL) > 0) {
2330 trial = atoi(value);
2331 switch (trial) {
2332 case 16:
2333 dsp_bit_width_enforce_mode = 16;
2334 break;
2335 case 24:
2336 dsp_bit_width_enforce_mode = 24;
2337 break;
2338 case 32:
2339 dsp_bit_width_enforce_mode = 32;
2340 break;
2341 default:
2342 dsp_bit_width_enforce_mode = 0;
2343 ALOGD("%s Dynamic DSP bitwidth config is disabled.", __func__);
2344 break;
2345 }
2346 }
2347
2348 return dsp_bit_width_enforce_mode;
2349}
2350
2351static void audio_enable_asm_bit_width_enforce_mode(struct mixer *mixer,
2352 uint32_t enforce_mode,
2353 bool enable)
2354{
2355 struct mixer_ctl *ctl = NULL;
2356 const char *mixer_ctl_name = "ASM Bit Width";
2357 uint32_t asm_bit_width_mode = 0;
2358
2359 if (enforce_mode == 0) {
2360 ALOGD("%s: DSP bitwidth feature is disabled.", __func__);
2361 return;
2362 }
2363
2364 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
2365 if (!ctl) {
2366 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2367 __func__, mixer_ctl_name);
2368 return;
2369 }
2370
2371 if (enable)
2372 asm_bit_width_mode = enforce_mode;
2373 else
2374 asm_bit_width_mode = 0;
2375
2376 ALOGV("%s DSP bit width feature status is %d width=%d",
2377 __func__, enable, asm_bit_width_mode);
2378 if (mixer_ctl_set_value(ctl, 0, asm_bit_width_mode) < 0)
2379 ALOGE("%s: Could not set ASM biwidth %d", __func__,
2380 asm_bit_width_mode);
2381
2382 return;
2383}
2384
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05302385/*
2386 * if native DSD playback active
2387 */
2388bool audio_is_dsd_native_stream_active(struct audio_device *adev)
2389{
2390 bool active = false;
2391 struct listnode *node = NULL;
2392 struct audio_usecase *uc = NULL;
2393 struct stream_out *curr_out = NULL;
2394
2395 list_for_each(node, &adev->usecase_list) {
2396 uc = node_to_item(node, struct audio_usecase, list);
2397 curr_out = (struct stream_out*) uc->stream.out;
2398
2399 if (curr_out && PCM_PLAYBACK == uc->type &&
2400 (DSD_NATIVE_BACKEND == platform_get_backend_index(uc->out_snd_device))) {
2401 active = true;
2402 ALOGV("%s:DSD playback is active", __func__);
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05302403 break;
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05302404 }
2405 }
2406 return active;
2407}
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302408
2409static bool force_device_switch(struct audio_usecase *usecase)
2410{
2411 bool ret = false;
2412 bool is_it_true_mode = false;
2413
Zhou Song30f2c3e2018-02-08 14:02:15 +08002414 if (usecase->type == PCM_CAPTURE ||
Surendar Karka93cd25a2018-08-28 14:21:37 +05302415 usecase->type == TRANSCODE_LOOPBACK_RX ||
2416 usecase->type == TRANSCODE_LOOPBACK_TX) {
Zhou Song30f2c3e2018-02-08 14:02:15 +08002417 return false;
2418 }
2419
Aalique Grahamecbc46a22017-10-05 10:30:23 -07002420 if(usecase->stream.out == NULL) {
2421 ALOGE("%s: stream.out is NULL", __func__);
2422 return false;
2423 }
2424
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302425 if (is_offload_usecase(usecase->id) &&
Xiaojun Sang869f2012016-02-23 16:33:07 +08002426 (usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002427 (compare_device_type(&usecase->stream.out->device_list, AUDIO_DEVICE_OUT_WIRED_HEADSET) ||
2428 compare_device_type(&usecase->stream.out->device_list, AUDIO_DEVICE_OUT_WIRED_HEADPHONE))) {
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302429 is_it_true_mode = (NATIVE_AUDIO_MODE_TRUE_44_1 == platform_get_native_support()? true : false);
2430 if ((is_it_true_mode && !adev->native_playback_enabled) ||
2431 (!is_it_true_mode && adev->native_playback_enabled)){
2432 ret = true;
2433 ALOGD("napb: time to toggle native mode");
2434 }
2435 }
2436
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302437 // Force all a2dp output devices to reconfigure for proper AFE encode format
Ashish Jainc597d102016-12-12 10:31:34 +05302438 //Also handle a case where in earlier a2dp start failed as A2DP stream was
2439 //in suspended state, hence try to trigger a retry when we again get a routing request.
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002440 if(is_a2dp_out_device_type(&usecase->stream.out->device_list) &&
Ashish Jainc597d102016-12-12 10:31:34 +05302441 audio_extn_a2dp_is_force_device_switch()) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302442 ALOGD("Force a2dp device switch to update new encoder config");
2443 ret = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002444 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302445
Florian Pfister1a84f312018-07-19 14:38:18 +02002446 if (usecase->stream.out->stream_config_changed) {
Manish Dewangan671a4202017-08-18 17:30:46 +05302447 ALOGD("Force stream_config_changed to update iec61937 transmission config");
2448 return true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002449 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302450 return ret;
2451}
2452
Aalique Grahame22e49102018-12-18 14:23:57 -08002453static void stream_app_type_cfg_init(struct stream_app_type_cfg *cfg)
2454{
2455 cfg->gain[0] = cfg->gain[1] = APP_TYPE_GAIN_DEFAULT;
2456}
2457
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302458bool is_btsco_device(snd_device_t out_snd_device, snd_device_t in_snd_device)
2459{
2460 bool ret=false;
2461 if ((out_snd_device == SND_DEVICE_OUT_BT_SCO ||
Zhou Song5657f492019-08-07 11:30:39 +08002462 out_snd_device == SND_DEVICE_OUT_BT_SCO_WB ||
2463 out_snd_device == SND_DEVICE_OUT_BT_SCO_SWB) ||
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302464 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_WB_NREC ||
2465 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_WB ||
Zhou Song5657f492019-08-07 11:30:39 +08002466 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_SWB ||
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302467 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_NREC ||
Mingshu Pang16093502020-04-20 11:21:16 +08002468 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC ||
2469 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_SWB_NREC)
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302470 ret = true;
2471
2472 return ret;
2473}
2474
2475bool is_a2dp_device(snd_device_t out_snd_device)
2476{
2477 bool ret=false;
2478 if (out_snd_device == SND_DEVICE_OUT_BT_A2DP)
2479 ret = true;
2480
2481 return ret;
2482}
2483
2484bool is_bt_soc_on(struct audio_device *adev)
2485{
2486 struct mixer_ctl *ctl;
2487 char *mixer_ctl_name = "BT SOC status";
2488 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
2489 bool bt_soc_status = true;
2490 if (!ctl) {
2491 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2492 __func__, mixer_ctl_name);
2493 /*This is to ensure we dont break targets which dont have the kernel change*/
2494 return true;
2495 }
2496 bt_soc_status = mixer_ctl_get_value(ctl, 0);
2497 ALOGD("BT SOC status: %d",bt_soc_status);
2498 return bt_soc_status;
2499}
2500
Zhou Song331c8e52019-08-26 14:16:12 +08002501static int configure_btsco_sample_rate(snd_device_t snd_device)
2502{
2503 struct mixer_ctl *ctl = NULL;
2504 struct mixer_ctl *ctl_sr_rx = NULL, *ctl_sr_tx = NULL, *ctl_sr = NULL;
2505 char *rate_str = NULL;
2506 bool is_rx_dev = true;
2507
2508 if (is_btsco_device(snd_device, snd_device)) {
2509 ctl_sr_tx = mixer_get_ctl_by_name(adev->mixer, "BT SampleRate TX");
2510 ctl_sr_rx = mixer_get_ctl_by_name(adev->mixer, "BT SampleRate RX");
2511 if (!ctl_sr_tx || !ctl_sr_rx) {
2512 ctl_sr = mixer_get_ctl_by_name(adev->mixer, "BT SampleRate");
2513 if (!ctl_sr)
2514 return -ENOSYS;
2515 }
2516
2517 switch (snd_device) {
2518 case SND_DEVICE_OUT_BT_SCO:
2519 rate_str = "KHZ_8";
2520 break;
2521 case SND_DEVICE_IN_BT_SCO_MIC_NREC:
2522 case SND_DEVICE_IN_BT_SCO_MIC:
2523 rate_str = "KHZ_8";
2524 is_rx_dev = false;
2525 break;
2526 case SND_DEVICE_OUT_BT_SCO_WB:
2527 rate_str = "KHZ_16";
2528 break;
2529 case SND_DEVICE_IN_BT_SCO_MIC_WB_NREC:
2530 case SND_DEVICE_IN_BT_SCO_MIC_WB:
2531 rate_str = "KHZ_16";
2532 is_rx_dev = false;
2533 break;
2534 default:
2535 return 0;
2536 }
2537
2538 ctl = (ctl_sr == NULL) ? (is_rx_dev ? ctl_sr_rx : ctl_sr_tx) : ctl_sr;
2539 if (mixer_ctl_set_enum_by_string(ctl, rate_str) != 0)
2540 return -ENOSYS;
2541 }
2542 return 0;
2543}
2544
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302545int out_standby_l(struct audio_stream *stream);
2546
Eric Laurent637e2d42018-11-15 12:24:31 -08002547struct stream_in *adev_get_active_input(const struct audio_device *adev)
2548{
2549 struct listnode *node;
2550 struct stream_in *last_active_in = NULL;
2551
2552 /* Get last added active input.
2553 * TODO: We may use a priority mechanism to pick highest priority active source */
2554 list_for_each(node, &adev->usecase_list)
2555 {
2556 struct audio_usecase *usecase = node_to_item(node, struct audio_usecase, list);
2557 if (usecase->type == PCM_CAPTURE && usecase->stream.in != NULL)
2558 last_active_in = usecase->stream.in;
2559 }
2560
2561 return last_active_in;
2562}
2563
2564struct stream_in *get_voice_communication_input(const struct audio_device *adev)
2565{
2566 struct listnode *node;
2567
2568 /* First check active inputs with voice communication source and then
2569 * any input if audio mode is in communication */
2570 list_for_each(node, &adev->usecase_list)
2571 {
2572 struct audio_usecase *usecase = node_to_item(node, struct audio_usecase, list);
2573 if (usecase->type == PCM_CAPTURE && usecase->stream.in != NULL &&
2574 usecase->stream.in->source == AUDIO_SOURCE_VOICE_COMMUNICATION)
2575 return usecase->stream.in;
2576 }
2577 if (adev->mode == AUDIO_MODE_IN_COMMUNICATION)
2578 return adev_get_active_input(adev);
2579
2580 return NULL;
2581}
2582
Carter Hsu2e429db2019-05-14 18:50:52 +08002583/*
2584 * Aligned with policy.h
2585 */
2586static inline int source_priority(int inputSource)
2587{
2588 switch (inputSource) {
2589 case AUDIO_SOURCE_VOICE_COMMUNICATION:
2590 return 9;
2591 case AUDIO_SOURCE_CAMCORDER:
2592 return 8;
2593 case AUDIO_SOURCE_VOICE_PERFORMANCE:
2594 return 7;
2595 case AUDIO_SOURCE_UNPROCESSED:
2596 return 6;
2597 case AUDIO_SOURCE_MIC:
2598 return 5;
2599 case AUDIO_SOURCE_ECHO_REFERENCE:
2600 return 4;
2601 case AUDIO_SOURCE_FM_TUNER:
2602 return 3;
2603 case AUDIO_SOURCE_VOICE_RECOGNITION:
2604 return 2;
2605 case AUDIO_SOURCE_HOTWORD:
2606 return 1;
2607 default:
2608 break;
2609 }
2610 return 0;
2611}
2612
2613static struct stream_in *get_priority_input(struct audio_device *adev)
2614{
2615 struct listnode *node;
2616 struct audio_usecase *usecase;
2617 int last_priority = 0, priority;
2618 struct stream_in *priority_in = NULL;
2619 struct stream_in *in;
2620
2621 list_for_each(node, &adev->usecase_list) {
2622 usecase = node_to_item(node, struct audio_usecase, list);
2623 if (usecase->type == PCM_CAPTURE) {
2624 in = usecase->stream.in;
2625 if (!in)
2626 continue;
2627 priority = source_priority(in->source);
2628
2629 if (priority > last_priority) {
2630 last_priority = priority;
2631 priority_in = in;
2632 }
2633 }
2634 }
2635 return priority_in;
2636}
2637
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002638int select_devices(struct audio_device *adev, audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002639{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002640 snd_device_t out_snd_device = SND_DEVICE_NONE;
2641 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002642 struct audio_usecase *usecase = NULL;
2643 struct audio_usecase *vc_usecase = NULL;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002644 struct audio_usecase *voip_usecase = NULL;
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -08002645 struct audio_usecase *hfp_usecase = NULL;
Siddartha Shaik44dd7702017-06-14 12:13:25 +05302646 struct stream_out stream_out;
Vimal Puthanveed41fcff22014-01-23 15:56:53 -08002647 audio_usecase_t hfp_ucid;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002648 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002649
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302650 ALOGD("%s for use case (%s)", __func__, use_case_table[uc_id]);
2651
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002652 usecase = get_usecase_from_list(adev, uc_id);
2653 if (usecase == NULL) {
2654 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
2655 return -EINVAL;
2656 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002657
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002658 if ((usecase->type == VOICE_CALL) ||
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -08002659 (usecase->type == VOIP_CALL) ||
2660 (usecase->type == PCM_HFP_CALL)) {
Aditya Bavanaribdda2f22016-10-19 15:02:05 +05302661 if(usecase->stream.out == NULL) {
2662 ALOGE("%s: stream.out is NULL", __func__);
2663 return -EINVAL;
2664 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002665 if (compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_BUS)) {
Guodong Hu267bdf82019-08-12 19:22:32 +08002666 out_snd_device = audio_extn_auto_hal_get_output_snd_device(adev,
2667 uc_id);
2668 in_snd_device = audio_extn_auto_hal_get_input_snd_device(adev,
2669 uc_id);
2670 } else {
2671 out_snd_device = platform_get_output_snd_device(adev->platform,
Jaideep Sharma477917f2020-03-13 18:13:33 +05302672 usecase->stream.out, usecase->type);
Guodong Hu267bdf82019-08-12 19:22:32 +08002673 in_snd_device = platform_get_input_snd_device(adev->platform,
2674 NULL,
Jaideep Sharma477917f2020-03-13 18:13:33 +05302675 &usecase->stream.out->device_list,
2676 usecase->type);
Guodong Hu267bdf82019-08-12 19:22:32 +08002677 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002678 assign_devices(&usecase->device_list, &usecase->stream.out->device_list);
Surendar Karka93cd25a2018-08-28 14:21:37 +05302679 } else if (usecase->type == TRANSCODE_LOOPBACK_RX) {
Siddartha Shaik31b530e2017-05-19 15:26:33 +05302680 if (usecase->stream.inout == NULL) {
2681 ALOGE("%s: stream.inout is NULL", __func__);
2682 return -EINVAL;
2683 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002684 assign_devices(&stream_out.device_list, &usecase->stream.inout->out_config.device_list);
Siddartha Shaik44dd7702017-06-14 12:13:25 +05302685 stream_out.sample_rate = usecase->stream.inout->out_config.sample_rate;
2686 stream_out.format = usecase->stream.inout->out_config.format;
2687 stream_out.channel_mask = usecase->stream.inout->out_config.channel_mask;
Jaideep Sharma477917f2020-03-13 18:13:33 +05302688 out_snd_device = platform_get_output_snd_device(adev->platform, &stream_out, usecase->type);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002689 assign_devices(&usecase->device_list,
2690 &usecase->stream.inout->out_config.device_list);
Surendar Karka93cd25a2018-08-28 14:21:37 +05302691 } else if (usecase->type == TRANSCODE_LOOPBACK_TX ) {
2692 if (usecase->stream.inout == NULL) {
2693 ALOGE("%s: stream.inout is NULL", __func__);
2694 return -EINVAL;
2695 }
Manisha Agarwal03297972020-04-17 15:36:55 +05302696 struct listnode out_devices;
2697 list_init(&out_devices);
2698 in_snd_device = platform_get_input_snd_device(adev->platform, NULL,
2699 &out_devices, usecase->type);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002700 assign_devices(&usecase->device_list,
2701 &usecase->stream.inout->in_config.device_list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002702 } else {
2703 /*
2704 * If the voice call is active, use the sound devices of voice call usecase
2705 * so that it would not result any device switch. All the usecases will
2706 * be switched to new device when select_devices() is called for voice call
2707 * usecase. This is to avoid switching devices for voice call when
2708 * check_usecases_codec_backend() is called below.
Alexy Joseph79dfa3c2016-04-20 18:44:56 -07002709 * choose voice call device only if the use case device is
2710 * also using the codec backend
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002711 */
Shiv Maliyappanahallibb4cf0b2016-01-21 11:30:06 -08002712 if (voice_is_in_call(adev) && adev->mode != AUDIO_MODE_NORMAL) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002713 vc_usecase = get_usecase_from_list(adev,
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -08002714 get_usecase_id_from_usecase_type(adev, VOICE_CALL));
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002715 if ((vc_usecase) && ((is_codec_backend_out_device_type(&vc_usecase->device_list) &&
2716 is_codec_backend_out_device_type(&usecase->device_list)) ||
2717 (is_codec_backend_out_device_type(&vc_usecase->device_list) &&
2718 is_codec_backend_in_device_type(&usecase->device_list)) ||
2719 is_single_device_type_equal(&vc_usecase->device_list,
2720 AUDIO_DEVICE_OUT_HEARING_AID) ||
2721 is_single_device_type_equal(&usecase->device_list,
Robert Lee8a6aaf32019-09-20 16:40:19 +08002722 AUDIO_DEVICE_IN_VOICE_CALL) ||
2723 (is_single_device_type_equal(&usecase->device_list,
2724 AUDIO_DEVICE_IN_USB_HEADSET) &&
2725 is_single_device_type_equal(&vc_usecase->device_list,
Kamalakar Yalasirid2323462021-01-28 14:52:38 +05302726 AUDIO_DEVICE_OUT_USB_HEADSET))||
2727 (is_single_device_type_equal(&usecase->device_list,
2728 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) &&
2729 is_codec_backend_out_device_type(&vc_usecase->device_list)))) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002730 in_snd_device = vc_usecase->in_snd_device;
2731 out_snd_device = vc_usecase->out_snd_device;
2732 }
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002733 } else if (voice_extn_compress_voip_is_active(adev)) {
yidongh02ef86f2017-04-21 15:36:04 +08002734 bool out_snd_device_backend_match = true;
yidongh47785a82017-05-08 19:29:29 +08002735 voip_usecase = get_usecase_from_list(adev, USECASE_COMPRESS_VOIP_CALL);
yidongh6261d8e2017-05-15 17:04:02 +08002736 if ((voip_usecase != NULL) &&
2737 (usecase->type == PCM_PLAYBACK) &&
2738 (usecase->stream.out != NULL)) {
yidongh02ef86f2017-04-21 15:36:04 +08002739 out_snd_device_backend_match = platform_check_backends_match(
2740 voip_usecase->out_snd_device,
2741 platform_get_output_snd_device(
2742 adev->platform,
Jaideep Sharma477917f2020-03-13 18:13:33 +05302743 usecase->stream.out, usecase->type));
yidongh02ef86f2017-04-21 15:36:04 +08002744 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002745 if ((voip_usecase) && (is_codec_backend_out_device_type(&voip_usecase->device_list) &&
2746 (is_codec_backend_out_device_type(&usecase->device_list) ||
2747 is_codec_backend_in_device_type(&usecase->device_list)) &&
yidongh02ef86f2017-04-21 15:36:04 +08002748 out_snd_device_backend_match &&
Mingming Yin2d8aa2e2014-08-14 00:00:51 -07002749 (voip_usecase->stream.out != adev->primary_output))) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002750 in_snd_device = voip_usecase->in_snd_device;
2751 out_snd_device = voip_usecase->out_snd_device;
2752 }
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -08002753 } else if (audio_extn_hfp_is_active(adev)) {
Vimal Puthanveed41fcff22014-01-23 15:56:53 -08002754 hfp_ucid = audio_extn_hfp_get_usecase();
2755 hfp_usecase = get_usecase_from_list(adev, hfp_ucid);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002756 if ((hfp_usecase) && is_codec_backend_out_device_type(&hfp_usecase->device_list)) {
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -08002757 in_snd_device = hfp_usecase->in_snd_device;
2758 out_snd_device = hfp_usecase->out_snd_device;
2759 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002760 }
2761 if (usecase->type == PCM_PLAYBACK) {
Aditya Bavanaribdda2f22016-10-19 15:02:05 +05302762 if (usecase->stream.out == NULL) {
2763 ALOGE("%s: stream.out is NULL", __func__);
2764 return -EINVAL;
2765 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002766 assign_devices(&usecase->device_list, &usecase->stream.out->device_list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002767 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07002768 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurent637e2d42018-11-15 12:24:31 -08002769 struct stream_out *voip_out = adev->primary_output;
2770 struct stream_in *voip_in = get_voice_communication_input(adev);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002771 if (compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_BUS))
Guodong Hu267bdf82019-08-12 19:22:32 +08002772 out_snd_device = audio_extn_auto_hal_get_output_snd_device(adev, uc_id);
2773 else
2774 out_snd_device = platform_get_output_snd_device(adev->platform,
Jaideep Sharma477917f2020-03-13 18:13:33 +05302775 usecase->stream.out,
2776 usecase->type);
kunleizdcf967a2018-08-07 17:09:11 +08002777 voip_usecase = get_usecase_from_list(adev, USECASE_AUDIO_PLAYBACK_VOIP);
kunleizdcf967a2018-08-07 17:09:11 +08002778
Eric Laurent637e2d42018-11-15 12:24:31 -08002779 if (voip_usecase)
2780 voip_out = voip_usecase->stream.out;
2781
2782 if (usecase->stream.out == voip_out && voip_in != NULL)
2783 select_devices(adev, voip_in->usecase);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07002784 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002785 } else if (usecase->type == PCM_CAPTURE) {
Aditya Bavanaribdda2f22016-10-19 15:02:05 +05302786 if (usecase->stream.in == NULL) {
2787 ALOGE("%s: stream.in is NULL", __func__);
2788 return -EINVAL;
2789 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002790 assign_devices(&usecase->device_list, &usecase->stream.in->device_list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002791 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07002792 if (in_snd_device == SND_DEVICE_NONE) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002793 struct listnode out_devices;
Eric Laurent637e2d42018-11-15 12:24:31 -08002794 struct stream_in *voip_in = get_voice_communication_input(adev);
Carter Hsu2e429db2019-05-14 18:50:52 +08002795 struct stream_in *priority_in = NULL;
Eric Laurent637e2d42018-11-15 12:24:31 -08002796
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002797 list_init(&out_devices);
Eric Laurent637e2d42018-11-15 12:24:31 -08002798 if (voip_in != NULL) {
Eric Laurent637e2d42018-11-15 12:24:31 -08002799 struct audio_usecase *voip_usecase = get_usecase_from_list(adev,
2800 USECASE_AUDIO_PLAYBACK_VOIP);
2801
Carter Hsu2e429db2019-05-14 18:50:52 +08002802 usecase->stream.in->enable_ec_port = false;
2803
Zhou Song62ea0282020-03-22 19:53:01 +08002804 bool is_ha_usecase = adev->ha_proxy_enable ?
2805 usecase->id == USECASE_AUDIO_RECORD_AFE_PROXY2 :
2806 usecase->id == USECASE_AUDIO_RECORD_AFE_PROXY;
2807 if (is_ha_usecase) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002808 reassign_device_list(&out_devices, AUDIO_DEVICE_OUT_TELEPHONY_TX, "");
Eric Laurent637e2d42018-11-15 12:24:31 -08002809 } else if (voip_usecase) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002810 assign_devices(&out_devices, &voip_usecase->stream.out->device_list);
Eric Laurent637e2d42018-11-15 12:24:31 -08002811 } else if (adev->primary_output &&
2812 !adev->primary_output->standby) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002813 assign_devices(&out_devices, &adev->primary_output->device_list);
Eric Laurent637e2d42018-11-15 12:24:31 -08002814 } else {
2815 /* forcing speaker o/p device to get matching i/p pair
2816 in case o/p is not routed from same primary HAL */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002817 reassign_device_list(&out_devices, AUDIO_DEVICE_OUT_SPEAKER, "");
Eric Laurent637e2d42018-11-15 12:24:31 -08002818 }
Carter Hsu2e429db2019-05-14 18:50:52 +08002819 priority_in = voip_in;
2820 } else {
2821 /* get the input with the highest priority source*/
2822 priority_in = get_priority_input(adev);
2823
2824 if (!priority_in)
2825 priority_in = usecase->stream.in;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07002826 }
Carter Hsu2e429db2019-05-14 18:50:52 +08002827
Eric Laurent637e2d42018-11-15 12:24:31 -08002828 in_snd_device = platform_get_input_snd_device(adev->platform,
Carter Hsu2e429db2019-05-14 18:50:52 +08002829 priority_in,
Jaideep Sharma477917f2020-03-13 18:13:33 +05302830 &out_devices,
2831 usecase->type);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07002832 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002833 }
2834 }
2835
2836 if (out_snd_device == usecase->out_snd_device &&
2837 in_snd_device == usecase->in_snd_device) {
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302838
2839 if (!force_device_switch(usecase))
2840 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002841 }
2842
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002843 if (!compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_BUS) &&
Guodong Hu267bdf82019-08-12 19:22:32 +08002844 ((is_btsco_device(out_snd_device,in_snd_device) && !adev->bt_sco_on) ||
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002845 (is_a2dp_device(out_snd_device) && !audio_extn_a2dp_source_is_ready()))) {
Guodong Hu267bdf82019-08-12 19:22:32 +08002846 ALOGD("SCO/A2DP is selected but they are not connected/ready hence dont route");
2847 return 0;
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302848 }
2849
Aalique Grahame22e49102018-12-18 14:23:57 -08002850 if (out_snd_device != SND_DEVICE_NONE &&
2851 out_snd_device != adev->last_logged_snd_device[uc_id][0]) {
2852 ALOGD("%s: changing use case %s output device from(%d: %s, acdb %d) to (%d: %s, acdb %d)",
2853 __func__,
2854 use_case_table[uc_id],
2855 adev->last_logged_snd_device[uc_id][0],
2856 platform_get_snd_device_name(adev->last_logged_snd_device[uc_id][0]),
2857 adev->last_logged_snd_device[uc_id][0] != SND_DEVICE_NONE ?
2858 platform_get_snd_device_acdb_id(adev->last_logged_snd_device[uc_id][0]) :
2859 -1,
2860 out_snd_device,
2861 platform_get_snd_device_name(out_snd_device),
2862 platform_get_snd_device_acdb_id(out_snd_device));
2863 adev->last_logged_snd_device[uc_id][0] = out_snd_device;
2864 }
2865 if (in_snd_device != SND_DEVICE_NONE &&
2866 in_snd_device != adev->last_logged_snd_device[uc_id][1]) {
2867 ALOGD("%s: changing use case %s input device from(%d: %s, acdb %d) to (%d: %s, acdb %d)",
2868 __func__,
2869 use_case_table[uc_id],
2870 adev->last_logged_snd_device[uc_id][1],
2871 platform_get_snd_device_name(adev->last_logged_snd_device[uc_id][1]),
2872 adev->last_logged_snd_device[uc_id][1] != SND_DEVICE_NONE ?
2873 platform_get_snd_device_acdb_id(adev->last_logged_snd_device[uc_id][1]) :
2874 -1,
2875 in_snd_device,
2876 platform_get_snd_device_name(in_snd_device),
2877 platform_get_snd_device_acdb_id(in_snd_device));
2878 adev->last_logged_snd_device[uc_id][1] = in_snd_device;
2879 }
2880
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002881
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002882 /*
2883 * Limitation: While in call, to do a device switch we need to disable
2884 * and enable both RX and TX devices though one of them is same as current
2885 * device.
2886 */
Vidyakumar Athota21b3bb92014-04-25 11:08:08 -07002887 if ((usecase->type == VOICE_CALL) &&
2888 (usecase->in_snd_device != SND_DEVICE_NONE) &&
2889 (usecase->out_snd_device != SND_DEVICE_NONE)) {
Eric Laurentb23d5282013-05-14 15:27:20 -07002890 status = platform_switch_voice_call_device_pre(adev->platform);
Narsinga Rao Chella116142b2015-08-14 18:00:08 -07002891 }
2892
2893 if (((usecase->type == VOICE_CALL) ||
2894 (usecase->type == VOIP_CALL)) &&
2895 (usecase->out_snd_device != SND_DEVICE_NONE)) {
2896 /* Disable sidetone only if voice/voip call already exists */
Jaideep Sharma477917f2020-03-13 18:13:33 +05302897 if (voice_is_call_state_active_in_call(adev) ||
Narsinga Rao Chella116142b2015-08-14 18:00:08 -07002898 voice_extn_compress_voip_is_started(adev))
Bhalchandra Gajare45fee282015-06-09 22:23:45 -07002899 voice_set_sidetone(adev, usecase->out_snd_device, false);
Vidyakumar Athotaea269c62016-10-31 09:05:59 -07002900
2901 /* Disable aanc only if voice call exists */
Jaideep Sharma477917f2020-03-13 18:13:33 +05302902 if (voice_is_call_state_active_in_call(adev))
Vidyakumar Athotaea269c62016-10-31 09:05:59 -07002903 voice_check_and_update_aanc_path(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002904 }
2905
Aalique Grahame22e49102018-12-18 14:23:57 -08002906 if ((out_snd_device == SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP ||
2907 out_snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP) &&
Florian Pfister1a84f312018-07-19 14:38:18 +02002908 (!audio_extn_a2dp_source_is_ready())) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302909 ALOGW("%s: A2DP profile is not ready, routing to speaker only", __func__);
Aalique Grahame22e49102018-12-18 14:23:57 -08002910 if (out_snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP)
2911 out_snd_device = SND_DEVICE_OUT_SPEAKER_SAFE;
2912 else
2913 out_snd_device = SND_DEVICE_OUT_SPEAKER;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302914 }
2915
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002916 /* Disable current sound devices */
2917 if (usecase->out_snd_device != SND_DEVICE_NONE) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07002918 disable_audio_route(adev, usecase);
2919 disable_snd_device(adev, usecase->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002920 }
2921
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002922 if (usecase->in_snd_device != SND_DEVICE_NONE) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07002923 disable_audio_route(adev, usecase);
2924 disable_snd_device(adev, usecase->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002925 }
2926
Vidyakumar Athota545dbd32013-11-13 17:30:53 -08002927 /* Applicable only on the targets that has external modem.
2928 * New device information should be sent to modem before enabling
2929 * the devices to reduce in-call device switch time.
2930 */
Vidyakumar Athota21b3bb92014-04-25 11:08:08 -07002931 if ((usecase->type == VOICE_CALL) &&
2932 (usecase->in_snd_device != SND_DEVICE_NONE) &&
2933 (usecase->out_snd_device != SND_DEVICE_NONE)) {
Vidyakumar Athota545dbd32013-11-13 17:30:53 -08002934 status = platform_switch_voice_call_enable_device_config(adev->platform,
2935 out_snd_device,
2936 in_snd_device);
Vidyakumar Athota21b3bb92014-04-25 11:08:08 -07002937 }
Vidyakumar Athota545dbd32013-11-13 17:30:53 -08002938
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002939 /* Enable new sound devices */
2940 if (out_snd_device != SND_DEVICE_NONE) {
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08002941 check_usecases_codec_backend(adev, usecase, out_snd_device);
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +05302942 if (platform_check_codec_asrc_support(adev->platform))
2943 check_and_set_asrc_mode(adev, usecase, out_snd_device);
Haynes Mathew George1376ca62014-04-24 11:55:48 -07002944 enable_snd_device(adev, out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002945 }
2946
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002947 if (in_snd_device != SND_DEVICE_NONE) {
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05302948 check_usecases_capture_codec_backend(adev, usecase, in_snd_device);
Haynes Mathew George1376ca62014-04-24 11:55:48 -07002949 enable_snd_device(adev, in_snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002950 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002951
Lakshman Chaluvaraju4c29a492020-10-28 19:29:23 +05302952 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL)
Eric Laurentb23d5282013-05-14 15:27:20 -07002953 status = platform_switch_voice_call_device_post(adev->platform,
2954 out_snd_device,
2955 in_snd_device);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002956
sangwoo170731f2013-06-08 15:36:36 +09002957 usecase->in_snd_device = in_snd_device;
2958 usecase->out_snd_device = out_snd_device;
2959
Dhananjay Kumard6d32152016-10-13 16:11:03 +05302960 audio_extn_utils_update_stream_app_type_cfg_for_usecase(adev,
2961 usecase);
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +05302962 if (usecase->type == PCM_PLAYBACK) {
Weiyin Jiang6f4c8062016-11-23 15:30:29 +08002963 if ((24 == usecase->stream.out->bit_width) &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002964 compare_device_type(&usecase->stream.out->device_list, AUDIO_DEVICE_OUT_SPEAKER)) {
Weiyin Jiang6f4c8062016-11-23 15:30:29 +08002965 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
2966 } else if ((out_snd_device == SND_DEVICE_OUT_HDMI ||
2967 out_snd_device == SND_DEVICE_OUT_USB_HEADSET ||
2968 out_snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
2969 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
2970 /*
2971 * To best utlize DSP, check if the stream sample rate is supported/multiple of
2972 * configured device sample rate, if not update the COPP rate to be equal to the
2973 * device sample rate, else open COPP at stream sample rate
2974 */
2975 platform_check_and_update_copp_sample_rate(adev->platform, out_snd_device,
2976 usecase->stream.out->sample_rate,
2977 &usecase->stream.out->app_type_cfg.sample_rate);
Ashish Jain4826f6c2017-02-06 13:33:20 +05302978 } else if (((out_snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
Preetam Singh Ranawat590d0432019-09-30 14:39:47 +05302979 out_snd_device != SND_DEVICE_OUT_HEADPHONES &&
2980 out_snd_device != SND_DEVICE_OUT_HEADPHONES_HIFI_FILTER &&
Ashish Jain4826f6c2017-02-06 13:33:20 +05302981 !audio_is_true_native_stream_active(adev)) &&
Weiyin Jiang6f4c8062016-11-23 15:30:29 +08002982 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
2983 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
2984 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
2985 }
Weiyin Jiangcdece202019-07-08 16:13:16 +08002986 }
2987 enable_audio_route(adev, usecase);
Weiyin Jiang6f4c8062016-11-23 15:30:29 +08002988
Shalini Manjunatha8ae54892020-12-29 13:02:25 +05302989 if (uc_id == USECASE_AUDIO_PLAYBACK_VOIP) {
2990 struct stream_in *voip_in = get_voice_communication_input(adev);
2991 struct audio_usecase *voip_in_usecase = NULL;
2992 voip_in_usecase = get_usecase_from_list(adev, USECASE_AUDIO_RECORD_VOIP);
2993 if (voip_in != NULL &&
2994 voip_in_usecase != NULL &&
2995 !(out_snd_device == AUDIO_DEVICE_OUT_SPEAKER ||
2996 out_snd_device == AUDIO_DEVICE_OUT_SPEAKER_SAFE) &&
2997 (voip_in_usecase->in_snd_device ==
2998 platform_get_input_snd_device(adev->platform, voip_in,
2999 &usecase->stream.out->device_list,usecase->type))) {
3000 /*
3001 * if VOIP TX is enabled before VOIP RX, needs to re-route the TX path
3002 * for enabling echo-reference-voip with correct port
3003 */
3004 ALOGD("%s: VOIP TX is enabled before VOIP RX,needs to re-route the TX path",__func__);
3005 disable_audio_route(adev, voip_in_usecase);
3006 disable_snd_device(adev, voip_in_usecase->in_snd_device);
3007 enable_snd_device(adev, voip_in_usecase->in_snd_device);
3008 enable_audio_route(adev, voip_in_usecase);
3009 }
3010 }
3011
3012
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003013 audio_extn_qdsp_set_device(usecase);
Aalique Grahame22e49102018-12-18 14:23:57 -08003014
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07003015 /* If input stream is already running then effect needs to be
3016 applied on the new input device that's being enabled here. */
Eric Laurent637e2d42018-11-15 12:24:31 -08003017 if (in_snd_device != SND_DEVICE_NONE)
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07003018 check_and_enable_effect(adev);
3019
Vidyakumar Athota493f2892016-08-14 11:56:55 -07003020 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) {
Vidyakumar Athotaea269c62016-10-31 09:05:59 -07003021 /* Enable aanc only if voice call exists */
Jaideep Sharma477917f2020-03-13 18:13:33 +05303022 if (voice_is_call_state_active_in_call(adev))
Vidyakumar Athotaea269c62016-10-31 09:05:59 -07003023 voice_check_and_update_aanc_path(adev, out_snd_device, true);
3024
Vidyakumar Athota493f2892016-08-14 11:56:55 -07003025 /* Enable sidetone only if other voice/voip call already exists */
Jaideep Sharma477917f2020-03-13 18:13:33 +05303026 if (voice_is_call_state_active_in_call(adev) ||
Vidyakumar Athota493f2892016-08-14 11:56:55 -07003027 voice_extn_compress_voip_is_started(adev))
3028 voice_set_sidetone(adev, out_snd_device, true);
3029 }
3030
Vidyakumar Athota1fd21792013-11-15 14:50:57 -08003031 /* Applicable only on the targets that has external modem.
3032 * Enable device command should be sent to modem only after
3033 * enabling voice call mixer controls
3034 */
Vidyakumar Athota339342f2014-07-01 15:30:57 -07003035 if (usecase->type == VOICE_CALL)
Vidyakumar Athota1fd21792013-11-15 14:50:57 -08003036 status = platform_switch_voice_call_usecase_route_post(adev->platform,
3037 out_snd_device,
3038 in_snd_device);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303039
3040 if (is_btsco_device(out_snd_device, in_snd_device) || is_a2dp_device(out_snd_device)) {
Eric Laurent637e2d42018-11-15 12:24:31 -08003041 struct stream_in *in = adev_get_active_input(adev);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303042 if (usecase->type == VOIP_CALL) {
Eric Laurent637e2d42018-11-15 12:24:31 -08003043 if (in != NULL && !in->standby) {
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303044 if (is_bt_soc_on(adev) == false){
3045 ALOGD("BT SCO MIC disconnected while in connection");
Eric Laurent637e2d42018-11-15 12:24:31 -08003046 if (in->pcm != NULL)
3047 pcm_stop(in->pcm);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303048 }
3049 }
3050 if ((usecase->stream.out != NULL) && (usecase->stream.out != adev->primary_output)
3051 && usecase->stream.out->started) {
3052 if (is_bt_soc_on(adev) == false) {
3053 ALOGD("BT SCO/A2DP disconnected while in connection");
3054 out_standby_l(&usecase->stream.out->stream.common);
3055 }
3056 }
3057 } else if ((usecase->stream.out != NULL) &&
3058 !(usecase->stream.out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
Surendar Karkae1dc8742018-11-19 16:23:14 +05303059 (usecase->type != TRANSCODE_LOOPBACK_TX) &&
3060 (usecase->type != TRANSCODE_LOOPBACK_RX) &&
Weiyin Jiang0d373242019-07-25 13:18:17 +08003061 (usecase->type != PCM_CAPTURE) &&
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303062 usecase->stream.out->started) {
3063 if (is_bt_soc_on(adev) == false) {
3064 ALOGD("BT SCO/A2dp disconnected while in connection");
3065 out_standby_l(&usecase->stream.out->stream.common);
3066 }
3067 }
3068 }
3069
Yung Ti Su70cb8242018-06-22 17:38:47 +08003070 if (usecase->type != PCM_CAPTURE && usecase == voip_usecase) {
Aalique Grahame22e49102018-12-18 14:23:57 -08003071 struct stream_out *voip_out = voip_usecase->stream.out;
3072 audio_extn_utils_send_app_type_gain(adev,
3073 voip_out->app_type_cfg.app_type,
3074 &voip_out->app_type_cfg.gain[0]);
3075 }
3076
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05303077 ALOGD("%s: done",__func__);
3078
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003079 return status;
3080}
3081
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003082static int stop_input_stream(struct stream_in *in)
3083{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05303084 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003085 struct audio_usecase *uc_info;
Pallavid7c7a272018-01-16 11:22:55 +05303086
3087 if (in == NULL) {
3088 ALOGE("%s: stream_in ptr is NULL", __func__);
3089 return -EINVAL;
3090 }
3091
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003092 struct audio_device *adev = in->dev;
Carter Hsu2e429db2019-05-14 18:50:52 +08003093 struct stream_in *priority_in = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003094
Eric Laurent994a6932013-07-17 11:51:42 -07003095 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003096 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003097 uc_info = get_usecase_from_list(adev, in->usecase);
3098 if (uc_info == NULL) {
3099 ALOGE("%s: Could not find the usecase (%d) in the list",
3100 __func__, in->usecase);
3101 return -EINVAL;
3102 }
3103
Carter Hsu2e429db2019-05-14 18:50:52 +08003104 priority_in = get_priority_input(adev);
3105
Derek Chenea197282019-01-07 17:35:01 -08003106 if (audio_extn_ext_hw_plugin_usecase_stop(adev->ext_hw_plugin, uc_info))
3107 ALOGE("%s: failed to stop ext hw plugin", __func__);
Derek Chend2530072014-11-24 12:39:14 -08003108
Vidyakumar Athota2850d532013-11-19 16:02:12 -08003109 /* Close in-call recording streams */
3110 voice_check_and_stop_incall_rec_usecase(adev, in);
3111
Eric Laurent150dbfe2013-02-27 14:31:02 -08003112 /* 1. Disable stream specific mixer controls */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07003113 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003114
3115 /* 2. Disable the tx device */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07003116 disable_snd_device(adev, uc_info->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003117
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003118 if (is_loopback_input_device(get_device_types(&in->device_list)))
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05303119 audio_extn_keep_alive_stop(KEEP_ALIVE_OUT_PRIMARY);
3120
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08003121 list_remove(&uc_info->list);
3122 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003123
Carter Hsu2e429db2019-05-14 18:50:52 +08003124 if (priority_in == in) {
3125 priority_in = get_priority_input(adev);
3126 if (priority_in)
3127 select_devices(adev, priority_in->usecase);
3128 }
3129
Vatsal Buchac09ae062018-11-14 13:25:08 +05303130 enable_gcov();
Eric Laurent994a6932013-07-17 11:51:42 -07003131 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003132 return ret;
3133}
3134
3135int start_input_stream(struct stream_in *in)
3136{
3137 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -08003138 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003139 struct audio_usecase *uc_info;
Preetam Singh Ranawata87e9742018-02-13 16:52:53 +05303140
3141 if (in == NULL) {
3142 ALOGE("%s: stream_in ptr is NULL", __func__);
3143 return -EINVAL;
3144 }
3145
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003146 struct audio_device *adev = in->dev;
Garmond Leunge2433c32017-09-28 21:51:22 -07003147 struct pcm_config config = in->config;
Garmond Leung438932f2017-10-04 19:35:18 -07003148 int usecase = platform_update_usecase_from_source(in->source,in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003149
Mingming Yin2664a5b2015-09-03 10:53:11 -07003150 if (get_usecase_from_list(adev, usecase) == NULL)
3151 in->usecase = usecase;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05303152 ALOGD("%s: enter: stream(%p)usecase(%d: %s)",
3153 __func__, &in->stream, in->usecase, use_case_table[in->usecase]);
Shiv Maliyappanahallida107642013-10-17 11:16:13 -07003154
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05303155 if (CARD_STATUS_OFFLINE == in->card_status||
3156 CARD_STATUS_OFFLINE == adev->card_status) {
3157 ALOGW("in->card_status or adev->card_status offline, try again");
Dhanalakshmi Siddani4d57e992014-07-17 16:37:51 +05303158 ret = -EIO;
Naresh Tanniru4c630392014-05-12 01:05:52 +05303159 goto error_config;
3160 }
Naresh Tanniru4c630392014-05-12 01:05:52 +05303161
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003162 if (is_sco_in_device_type(&in->device_list)) {
Lakshman Chaluvaraju88bb65c2021-02-02 15:50:11 +05303163 if (!adev->bt_sco_on || audio_extn_a2dp_source_is_ready()) {
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303164 ALOGE("%s: SCO profile is not ready, return error", __func__);
3165 ret = -EIO;
3166 goto error_config;
3167 }
3168 }
3169
Shiv Maliyappanahallida107642013-10-17 11:16:13 -07003170 /* Check if source matches incall recording usecase criteria */
3171 ret = voice_check_and_set_incall_rec_usecase(adev, in);
3172 if (ret)
3173 goto error_config;
3174 else
Mingming Yin2664a5b2015-09-03 10:53:11 -07003175 ALOGV("%s: usecase(%d)", __func__, in->usecase);
3176
Dhananjay Kumar7dbe3562019-08-30 05:49:33 +05303177 if (audio_extn_cin_attached_usecase(in))
3178 audio_extn_cin_acquire_usecase(in);
3179
Mingming Yin2664a5b2015-09-03 10:53:11 -07003180 if (get_usecase_from_list(adev, in->usecase) != NULL) {
3181 ALOGE("%s: use case assigned already in use, stream(%p)usecase(%d: %s)",
3182 __func__, &in->stream, in->usecase, use_case_table[in->usecase]);
Weiyin Jiang04949102020-09-10 16:10:51 +08003183 ret = -EINVAL;
3184 goto error_config;
Mingming Yin2664a5b2015-09-03 10:53:11 -07003185 }
Shiv Maliyappanahallida107642013-10-17 11:16:13 -07003186
Eric Laurentb23d5282013-05-14 15:27:20 -07003187 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003188 if (in->pcm_device_id < 0) {
3189 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
3190 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -08003191 ret = -EINVAL;
3192 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003193 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003194
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003195 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07003196
3197 if (!uc_info) {
3198 ret = -ENOMEM;
3199 goto error_config;
3200 }
3201
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003202 uc_info->id = in->usecase;
3203 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08003204 uc_info->stream.in = in;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003205 list_init(&uc_info->device_list);
3206 assign_devices(&uc_info->device_list, &in->device_list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003207 uc_info->in_snd_device = SND_DEVICE_NONE;
3208 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003209
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08003210 list_add_tail(&adev->usecase_list, &uc_info->list);
Wei Wangf7ca6c92017-11-21 14:51:20 -08003211 audio_streaming_hint_start();
Sudheer Papothifa9d2282015-09-17 01:53:25 +05303212 audio_extn_perf_lock_acquire(&adev->perf_lock_handle, 0,
3213 adev->perf_lock_opts,
3214 adev->perf_lock_opts_size);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003215 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003216
Derek Chenea197282019-01-07 17:35:01 -08003217 if (audio_extn_ext_hw_plugin_usecase_start(adev->ext_hw_plugin, uc_info))
3218 ALOGE("%s: failed to start ext hw plugin", __func__);
Derek Chend2530072014-11-24 12:39:14 -08003219
Deeraj Soman30cc7ae2019-03-18 16:26:55 +05303220 android_atomic_acquire_cas(true, false, &(in->capture_stopped));
3221
Dhananjay Kumar7dbe3562019-08-30 05:49:33 +05303222 if (audio_extn_cin_attached_usecase(in)) {
Manish Dewangan46e07982018-12-13 18:18:59 +05303223 ret = audio_extn_cin_open_input_stream(in);
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05303224 if (ret)
3225 goto error_open;
3226 else
3227 goto done_open;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003228 }
3229
Haynes Mathew George16081042017-05-31 17:16:49 -07003230 if (in->usecase == USECASE_AUDIO_RECORD_MMAP) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003231 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) {
Haynes Mathew George16081042017-05-31 17:16:49 -07003232 ALOGE("%s: pcm stream not ready", __func__);
3233 goto error_open;
3234 }
3235 ret = pcm_start(in->pcm);
3236 if (ret < 0) {
3237 ALOGE("%s: MMAP pcm_start failed ret %d", __func__, ret);
3238 goto error_open;
3239 }
3240 } else {
3241 unsigned int flags = PCM_IN | PCM_MONOTONIC;
3242 unsigned int pcm_open_retry_count = 0;
3243
Zhou Song62ea0282020-03-22 19:53:01 +08003244 if ((in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY) ||
3245 (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY2)) {
Haynes Mathew George16081042017-05-31 17:16:49 -07003246 flags |= PCM_MMAP | PCM_NOIRQ;
3247 pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT;
3248 } else if (in->realtime) {
3249 flags |= PCM_MMAP | PCM_NOIRQ;
3250 }
3251
Garmond Leunge2433c32017-09-28 21:51:22 -07003252 if (audio_extn_ffv_get_stream() == in) {
3253 ALOGD("%s: ffv stream, update pcm config", __func__);
3254 audio_extn_ffv_update_pcm_config(&config);
3255 }
Haynes Mathew George16081042017-05-31 17:16:49 -07003256 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
3257 __func__, adev->snd_card, in->pcm_device_id, in->config.channels);
3258
3259 while (1) {
Haynes Mathew George380745d2017-10-04 15:27:45 -07003260 ATRACE_BEGIN("pcm_in_open");
Haynes Mathew George16081042017-05-31 17:16:49 -07003261 in->pcm = pcm_open(adev->snd_card, in->pcm_device_id,
Garmond Leung438932f2017-10-04 19:35:18 -07003262 flags, &config);
Haynes Mathew George380745d2017-10-04 15:27:45 -07003263 ATRACE_END();
Satish Babu Patakokila54ce83d2018-07-06 18:00:37 +05303264 if (errno == ENETRESET && !pcm_is_ready(in->pcm)) {
Sharad Sanglec6f32552018-05-04 16:15:38 +05303265 ALOGE("%s: pcm_open failed errno:%d\n", __func__, errno);
3266 adev->card_status = CARD_STATUS_OFFLINE;
3267 in->card_status = CARD_STATUS_OFFLINE;
3268 ret = -EIO;
3269 goto error_open;
3270 }
3271
Haynes Mathew George16081042017-05-31 17:16:49 -07003272 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) {
3273 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
3274 if (in->pcm != NULL) {
3275 pcm_close(in->pcm);
3276 in->pcm = NULL;
3277 }
Weiyin Jiang72197252019-10-09 11:49:32 +08003278 if (pcm_open_retry_count == 0) {
Haynes Mathew George16081042017-05-31 17:16:49 -07003279 ret = -EIO;
3280 goto error_open;
3281 }
Weiyin Jiang72197252019-10-09 11:49:32 +08003282 pcm_open_retry_count--;
Haynes Mathew George16081042017-05-31 17:16:49 -07003283 usleep(PROXY_OPEN_WAIT_TIME * 1000);
3284 continue;
3285 }
3286 break;
3287 }
3288
3289 ALOGV("%s: pcm_prepare", __func__);
Haynes Mathew George380745d2017-10-04 15:27:45 -07003290 ATRACE_BEGIN("pcm_in_prepare");
Haynes Mathew George16081042017-05-31 17:16:49 -07003291 ret = pcm_prepare(in->pcm);
Haynes Mathew George380745d2017-10-04 15:27:45 -07003292 ATRACE_END();
Haynes Mathew George16081042017-05-31 17:16:49 -07003293 if (ret < 0) {
3294 ALOGE("%s: pcm_prepare returned %d", __func__, ret);
3295 pcm_close(in->pcm);
3296 in->pcm = NULL;
3297 goto error_open;
3298 }
3299 register_in_stream(in);
3300 if (in->realtime) {
Haynes Mathew George380745d2017-10-04 15:27:45 -07003301 ATRACE_BEGIN("pcm_in_start");
Haynes Mathew George16081042017-05-31 17:16:49 -07003302 ret = pcm_start(in->pcm);
Haynes Mathew George380745d2017-10-04 15:27:45 -07003303 ATRACE_END();
Haynes Mathew George16081042017-05-31 17:16:49 -07003304 if (ret < 0) {
3305 ALOGE("%s: RT pcm_start failed ret %d", __func__, ret);
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003306 pcm_close(in->pcm);
3307 in->pcm = NULL;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003308 goto error_open;
3309 }
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003310 }
Haynes Mathew George5beddd42016-06-27 18:33:40 -07003311 }
3312
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07003313 check_and_enable_effect(adev);
justinweng20fb6d82019-02-21 18:49:00 -07003314 audio_extn_audiozoom_set_microphone_direction(in, in->zoom);
3315 audio_extn_audiozoom_set_microphone_field_dimension(in, in->direction);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07003316
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003317 if (is_loopback_input_device(get_device_types(&in->device_list)))
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05303318 audio_extn_keep_alive_start(KEEP_ALIVE_OUT_PRIMARY);
3319
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05303320done_open:
Wei Wangf7ca6c92017-11-21 14:51:20 -08003321 audio_streaming_hint_end();
Sudheer Papothifa9d2282015-09-17 01:53:25 +05303322 audio_extn_perf_lock_release(&adev->perf_lock_handle);
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07003323 ALOGD("%s: exit", __func__);
Vatsal Buchac09ae062018-11-14 13:25:08 +05303324 enable_gcov();
Eric Laurentc8400632013-02-14 19:04:54 -08003325 return ret;
3326
3327error_open:
Wei Wangf7ca6c92017-11-21 14:51:20 -08003328 audio_streaming_hint_end();
Sudheer Papothifa9d2282015-09-17 01:53:25 +05303329 audio_extn_perf_lock_release(&adev->perf_lock_handle);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003330 stop_input_stream(in);
Wei Wangf7ca6c92017-11-21 14:51:20 -08003331
Eric Laurentc8400632013-02-14 19:04:54 -08003332error_config:
Weiyin Jiang04949102020-09-10 16:10:51 +08003333 if (audio_extn_cin_attached_usecase(in))
3334 audio_extn_cin_close_input_stream(in);
Laxminath Kasam2cb4b752015-09-24 03:59:15 +05303335 /*
3336 * sleep 50ms to allow sufficient time for kernel
3337 * drivers to recover incases like SSR.
3338 */
3339 usleep(50000);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003340 ALOGD("%s: exit: status(%d)", __func__, ret);
Vatsal Buchac09ae062018-11-14 13:25:08 +05303341 enable_gcov();
Eric Laurentc8400632013-02-14 19:04:54 -08003342 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003343}
3344
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07003345void lock_input_stream(struct stream_in *in)
3346{
3347 pthread_mutex_lock(&in->pre_lock);
3348 pthread_mutex_lock(&in->lock);
3349 pthread_mutex_unlock(&in->pre_lock);
3350}
3351
3352void lock_output_stream(struct stream_out *out)
3353{
3354 pthread_mutex_lock(&out->pre_lock);
3355 pthread_mutex_lock(&out->lock);
3356 pthread_mutex_unlock(&out->pre_lock);
3357}
3358
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003359/* must be called with out->lock locked */
3360static int send_offload_cmd_l(struct stream_out* out, int command)
3361{
3362 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
3363
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07003364 if (!cmd) {
3365 ALOGE("failed to allocate mem for command 0x%x", command);
3366 return -ENOMEM;
3367 }
3368
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003369 ALOGVV("%s %d", __func__, command);
3370
3371 cmd->cmd = command;
3372 list_add_tail(&out->offload_cmd_list, &cmd->node);
3373 pthread_cond_signal(&out->offload_cond);
3374 return 0;
3375}
3376
Gautam Manamfeeb1162020-12-24 14:08:04 +05303377/* must be called with out->lock */
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003378static void stop_compressed_output_l(struct stream_out *out)
3379{
Gautam Manamfeeb1162020-12-24 14:08:04 +05303380 pthread_mutex_lock(&out->latch_lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003381 out->offload_state = OFFLOAD_STATE_IDLE;
Gautam Manamfeeb1162020-12-24 14:08:04 +05303382 pthread_mutex_unlock(&out->latch_lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003383 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07003384 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003385 if (out->compr != NULL) {
3386 compress_stop(out->compr);
3387 while (out->offload_thread_blocked) {
3388 pthread_cond_wait(&out->cond, &out->lock);
3389 }
3390 }
3391}
3392
Varun Balaraje49253e2017-07-06 19:48:56 +05303393bool is_interactive_usecase(audio_usecase_t uc_id)
3394{
3395 unsigned int i;
3396 for (i = 0; i < sizeof(interactive_usecases)/sizeof(interactive_usecases[0]); i++) {
3397 if (uc_id == interactive_usecases[i])
3398 return true;
3399 }
3400 return false;
3401}
3402
3403static audio_usecase_t get_interactive_usecase(struct audio_device *adev)
3404{
3405 audio_usecase_t ret_uc = USECASE_INVALID;
3406 unsigned int intract_uc_index;
3407 unsigned int num_usecase = sizeof(interactive_usecases)/sizeof(interactive_usecases[0]);
3408
3409 ALOGV("%s: num_usecase: %d", __func__, num_usecase);
3410 for (intract_uc_index = 0; intract_uc_index < num_usecase; intract_uc_index++) {
3411 if (!(adev->interactive_usecase_state & (0x1 << intract_uc_index))) {
3412 adev->interactive_usecase_state |= 0x1 << intract_uc_index;
3413 ret_uc = interactive_usecases[intract_uc_index];
3414 break;
3415 }
3416 }
3417
3418 ALOGV("%s: Interactive usecase is %d", __func__, ret_uc);
3419 return ret_uc;
3420}
3421
3422static void free_interactive_usecase(struct audio_device *adev,
3423 audio_usecase_t uc_id)
3424{
3425 unsigned int interact_uc_index;
3426 unsigned int num_usecase = sizeof(interactive_usecases)/sizeof(interactive_usecases[0]);
3427
3428 for (interact_uc_index = 0; interact_uc_index < num_usecase; interact_uc_index++) {
3429 if (interactive_usecases[interact_uc_index] == uc_id) {
3430 adev->interactive_usecase_state &= ~(0x1 << interact_uc_index);
3431 break;
3432 }
3433 }
3434 ALOGV("%s: free Interactive usecase %d", __func__, uc_id);
3435}
3436
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003437bool is_offload_usecase(audio_usecase_t uc_id)
3438{
3439 unsigned int i;
3440 for (i = 0; i < sizeof(offload_usecases)/sizeof(offload_usecases[0]); i++) {
3441 if (uc_id == offload_usecases[i])
3442 return true;
3443 }
3444 return false;
3445}
3446
Dhananjay Kumarac341582017-02-23 23:42:25 +05303447static audio_usecase_t get_offload_usecase(struct audio_device *adev, bool is_compress)
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003448{
vivek mehta446c3962015-09-14 10:57:35 -07003449 audio_usecase_t ret_uc = USECASE_INVALID;
3450 unsigned int offload_uc_index;
Alexy Josephb1379942016-01-29 15:49:38 -08003451 unsigned int num_usecase = sizeof(offload_usecases)/sizeof(offload_usecases[0]);
vivek mehta446c3962015-09-14 10:57:35 -07003452 if (!adev->multi_offload_enable) {
Dhananjay Kumarac341582017-02-23 23:42:25 +05303453 if (!is_compress)
vivek mehta446c3962015-09-14 10:57:35 -07003454 ret_uc = USECASE_AUDIO_PLAYBACK_OFFLOAD2;
3455 else
3456 ret_uc = USECASE_AUDIO_PLAYBACK_OFFLOAD;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003457
vivek mehta446c3962015-09-14 10:57:35 -07003458 pthread_mutex_lock(&adev->lock);
3459 if (get_usecase_from_list(adev, ret_uc) != NULL)
3460 ret_uc = USECASE_INVALID;
3461 pthread_mutex_unlock(&adev->lock);
3462
3463 return ret_uc;
3464 }
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003465
3466 ALOGV("%s: num_usecase: %d", __func__, num_usecase);
vivek mehta446c3962015-09-14 10:57:35 -07003467 for (offload_uc_index = 0; offload_uc_index < num_usecase; offload_uc_index++) {
3468 if (!(adev->offload_usecases_state & (0x1 << offload_uc_index))) {
3469 adev->offload_usecases_state |= 0x1 << offload_uc_index;
3470 ret_uc = offload_usecases[offload_uc_index];
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003471 break;
3472 }
3473 }
vivek mehta446c3962015-09-14 10:57:35 -07003474
3475 ALOGV("%s: offload usecase is %d", __func__, ret_uc);
3476 return ret_uc;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003477}
3478
3479static void free_offload_usecase(struct audio_device *adev,
3480 audio_usecase_t uc_id)
3481{
vivek mehta446c3962015-09-14 10:57:35 -07003482 unsigned int offload_uc_index;
Alexy Josephb1379942016-01-29 15:49:38 -08003483 unsigned int num_usecase = sizeof(offload_usecases)/sizeof(offload_usecases[0]);
vivek mehta446c3962015-09-14 10:57:35 -07003484
3485 if (!adev->multi_offload_enable)
3486 return;
3487
3488 for (offload_uc_index = 0; offload_uc_index < num_usecase; offload_uc_index++) {
3489 if (offload_usecases[offload_uc_index] == uc_id) {
3490 adev->offload_usecases_state &= ~(0x1 << offload_uc_index);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003491 break;
3492 }
3493 }
3494 ALOGV("%s: free offload usecase %d", __func__, uc_id);
3495}
3496
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003497static void *offload_thread_loop(void *context)
3498{
3499 struct stream_out *out = (struct stream_out *) context;
3500 struct listnode *item;
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08003501 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003502
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003503 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
Weiyin Jiangd9b5a4e2019-07-18 17:24:21 +08003504 set_sched_policy(0, SP_FOREGROUND);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003505 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
3506
3507 ALOGV("%s", __func__);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07003508 lock_output_stream(out);
juyuchen391b5fa2018-12-12 17:58:09 +08003509 out->offload_state = OFFLOAD_STATE_IDLE;
3510 out->playback_started = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003511 for (;;) {
3512 struct offload_cmd *cmd = NULL;
3513 stream_callback_event_t event;
3514 bool send_callback = false;
3515
3516 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
3517 __func__, list_empty(&out->offload_cmd_list),
3518 out->offload_state);
3519 if (list_empty(&out->offload_cmd_list)) {
3520 ALOGV("%s SLEEPING", __func__);
3521 pthread_cond_wait(&out->offload_cond, &out->lock);
3522 ALOGV("%s RUNNING", __func__);
3523 continue;
3524 }
3525
3526 item = list_head(&out->offload_cmd_list);
3527 cmd = node_to_item(item, struct offload_cmd, node);
3528 list_remove(item);
3529
3530 ALOGVV("%s STATE %d CMD %d out->compr %p",
3531 __func__, out->offload_state, cmd->cmd, out->compr);
3532
3533 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
3534 free(cmd);
3535 break;
3536 }
3537
Haynes Mathew Georgeee5836f2017-11-21 18:02:10 -08003538 // allow OFFLOAD_CMD_ERROR reporting during standby
3539 // this is needed to handle failures during compress_open
3540 // Note however that on a pause timeout, the stream is closed
3541 // and no offload usecase will be active. Therefore this
3542 // special case is needed for compress_open failures alone
3543 if (cmd->cmd != OFFLOAD_CMD_ERROR &&
3544 out->compr == NULL) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003545 ALOGE("%s: Compress handle is NULL", __func__);
Haynes Mathew Georgea9abb202016-06-02 14:13:20 -07003546 free(cmd);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003547 pthread_cond_signal(&out->cond);
3548 continue;
3549 }
3550 out->offload_thread_blocked = true;
3551 pthread_mutex_unlock(&out->lock);
3552 send_callback = false;
3553 switch(cmd->cmd) {
3554 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07003555 ALOGD("copl(%p):calling compress_wait", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003556 compress_wait(out->compr, -1);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07003557 ALOGD("copl(%p):out of compress_wait", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003558 send_callback = true;
3559 event = STREAM_CBK_EVENT_WRITE_READY;
3560 break;
3561 case OFFLOAD_CMD_PARTIAL_DRAIN:
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08003562 ret = compress_next_track(out->compr);
Sidipotu Ashok55820562014-02-10 16:16:38 +05303563 if(ret == 0) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07003564 ALOGD("copl(%p):calling compress_partial_drain", out);
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05303565 ret = compress_partial_drain(out->compr);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07003566 ALOGD("copl(%p):out of compress_partial_drain", out);
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05303567 if (ret < 0)
3568 ret = -errno;
Sidipotu Ashok55820562014-02-10 16:16:38 +05303569 }
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05303570 else if (ret == -ETIMEDOUT)
Aniket Kumar Lata3570fb12017-11-08 15:53:44 -08003571 ret = compress_drain(out->compr);
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08003572 else
3573 ALOGE("%s: Next track returned error %d",__func__, ret);
Aniket Kumar Lata3570fb12017-11-08 15:53:44 -08003574 if (-ENETRESET != ret && !(-EINTR == ret &&
3575 CARD_STATUS_OFFLINE == out->card_status)) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05303576 send_callback = true;
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05303577 pthread_mutex_lock(&out->lock);
3578 out->send_new_metadata = 1;
3579 out->send_next_track_params = true;
3580 pthread_mutex_unlock(&out->lock);
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05303581 event = STREAM_CBK_EVENT_DRAIN_READY;
3582 ALOGV("copl(%p):send drain callback, ret %d", out, ret);
3583 } else
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05303584 ALOGI("%s: Block drain ready event during SSR", __func__);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003585 break;
3586 case OFFLOAD_CMD_DRAIN:
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07003587 ALOGD("copl(%p):calling compress_drain", out);
Aniket Kumar Lata3570fb12017-11-08 15:53:44 -08003588 ret = compress_drain(out->compr);
3589 ALOGD("copl(%p):out of compress_drain", out);
3590 // EINTR check avoids drain interruption due to SSR
3591 if (-ENETRESET != ret && !(-EINTR == ret &&
3592 CARD_STATUS_OFFLINE == out->card_status)) {
3593 send_callback = true;
3594 event = STREAM_CBK_EVENT_DRAIN_READY;
3595 } else
3596 ALOGI("%s: Block drain ready event during SSR", __func__);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003597 break;
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05303598 case OFFLOAD_CMD_ERROR:
3599 ALOGD("copl(%p): sending error callback to AF", out);
3600 send_callback = true;
3601 event = STREAM_CBK_EVENT_ERROR;
3602 break;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003603 default:
3604 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
3605 break;
3606 }
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07003607 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003608 out->offload_thread_blocked = false;
3609 pthread_cond_signal(&out->cond);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08003610 if (send_callback && out->client_callback) {
3611 ALOGVV("%s: sending client_callback event %d", __func__, event);
3612 out->client_callback(event, NULL, out->client_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -07003613 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003614 free(cmd);
3615 }
3616
3617 pthread_cond_signal(&out->cond);
3618 while (!list_empty(&out->offload_cmd_list)) {
3619 item = list_head(&out->offload_cmd_list);
3620 list_remove(item);
3621 free(node_to_item(item, struct offload_cmd, node));
3622 }
3623 pthread_mutex_unlock(&out->lock);
3624
3625 return NULL;
3626}
3627
3628static int create_offload_callback_thread(struct stream_out *out)
3629{
3630 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
3631 list_init(&out->offload_cmd_list);
3632 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
3633 offload_thread_loop, out);
3634 return 0;
3635}
3636
3637static int destroy_offload_callback_thread(struct stream_out *out)
3638{
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07003639 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003640 stop_compressed_output_l(out);
3641 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
3642
3643 pthread_mutex_unlock(&out->lock);
3644 pthread_join(out->offload_thread, (void **) NULL);
3645 pthread_cond_destroy(&out->offload_cond);
3646
3647 return 0;
3648}
3649
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003650static int stop_output_stream(struct stream_out *out)
3651{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05303652 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003653 struct audio_usecase *uc_info;
3654 struct audio_device *adev = out->dev;
Aalique Grahame22e49102018-12-18 14:23:57 -08003655 bool has_voip_usecase =
3656 get_usecase_from_list(adev, USECASE_AUDIO_PLAYBACK_VOIP) != NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003657
Eric Laurent994a6932013-07-17 11:51:42 -07003658 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003659 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003660 uc_info = get_usecase_from_list(adev, out->usecase);
3661 if (uc_info == NULL) {
3662 ALOGE("%s: Could not find the usecase (%d) in the list",
3663 __func__, out->usecase);
3664 return -EINVAL;
3665 }
3666
Zhou Song1f93fa52020-11-20 13:57:39 +08003667 out->a2dp_muted = false;
3668
Derek Chenea197282019-01-07 17:35:01 -08003669 if (audio_extn_ext_hw_plugin_usecase_stop(adev->ext_hw_plugin, uc_info))
3670 ALOGE("%s: failed to stop ext hw plugin", __func__);
Derek Chend2530072014-11-24 12:39:14 -08003671
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07003672 if (is_offload_usecase(out->usecase) &&
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05303673 !(audio_extn_passthru_is_passthrough_stream(out))) {
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08003674 if (adev->visualizer_stop_output != NULL)
3675 adev->visualizer_stop_output(out->handle, out->pcm_device_id);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08003676
3677 audio_extn_dts_remove_state_notifier_node(out->usecase);
3678
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08003679 if (adev->offload_effects_stop_output != NULL)
3680 adev->offload_effects_stop_output(out->handle, out->pcm_device_id);
vivek mehtad15d2bf2019-05-17 13:35:10 -07003681 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_ULL ||
3682 out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
3683 audio_low_latency_hint_end();
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08003684 }
Eric Laurentc4aef752013-09-12 17:45:53 -07003685
Arun Mirpuri2ce92a62018-09-12 18:36:10 -07003686 if (out->usecase == USECASE_INCALL_MUSIC_UPLINK ||
3687 out->usecase == USECASE_INCALL_MUSIC_UPLINK2) {
Arun Mirpurief53ce52018-09-11 18:00:09 -07003688 voice_set_device_mute_flag(adev, false);
Arun Mirpuri2ce92a62018-09-12 18:36:10 -07003689 }
Arun Mirpurief53ce52018-09-11 18:00:09 -07003690
Eric Laurent150dbfe2013-02-27 14:31:02 -08003691 /* 1. Get and set stream specific mixer controls */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07003692 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003693
3694 /* 2. Disable the rx device */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07003695 disable_snd_device(adev, uc_info->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003696
Aalique Grahame22e49102018-12-18 14:23:57 -08003697 audio_extn_extspk_update(adev->extspk);
3698
Xiaojun Sang785b5da2017-08-03 15:52:29 +08003699 if (is_offload_usecase(out->usecase)) {
3700 audio_enable_asm_bit_width_enforce_mode(adev->mixer,
3701 adev->dsp_bit_width_enforce_mode,
3702 false);
3703 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003704 if (is_usb_out_device_type(&out->device_list)) {
Garmond Leung5fd0b552018-04-17 11:56:12 -07003705 ret = audio_extn_usb_check_and_set_svc_int(uc_info,
3706 false);
3707
3708 if (ret != 0)
3709 check_usecases_codec_backend(adev, uc_info, uc_info->out_snd_device);
3710 /* default service interval was successfully updated,
3711 reopen USB backend with new service interval */
3712 ret = 0;
3713 }
Xiaojun Sang785b5da2017-08-03 15:52:29 +08003714
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08003715 list_remove(&uc_info->list);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303716 out->started = 0;
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07003717 if (is_offload_usecase(out->usecase) &&
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05303718 (audio_extn_passthru_is_passthrough_stream(out))) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07003719 ALOGV("Disable passthrough , reset mixer to pcm");
3720 /* NO_PASSTHROUGH */
Meng Wang4c32fb42020-01-16 17:57:11 +08003721#ifdef AUDIO_GKI_ENABLED
3722 /* out->compr_config.codec->reserved[0] is for compr_passthr */
3723 out->compr_config.codec->reserved[0] = 0;
3724#else
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07003725 out->compr_config.codec->compr_passthr = 0;
Meng Wang4c32fb42020-01-16 17:57:11 +08003726#endif
Mingming Yin21854652016-04-13 11:54:02 -07003727 audio_extn_passthru_on_stop(out);
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07003728 audio_extn_dolby_set_dap_bypass(adev, DAP_STATE_ON);
3729 }
Eric Laurent07eeafd2013-10-06 12:52:49 -07003730
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05303731 /* Must be called after removing the usecase from list */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003732 if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_AUX_DIGITAL))
Md Mansoor Ahmeddb1b4f92018-01-25 18:56:31 +05303733 audio_extn_keep_alive_start(KEEP_ALIVE_OUT_HDMI);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05303734
Manish Dewangan21a850a2017-08-14 12:03:55 +05303735 if (out->ip_hdlr_handle) {
Naresh Tanniru85819452017-05-04 18:55:45 -07003736 ret = audio_extn_ip_hdlr_intf_close(out->ip_hdlr_handle, true, out);
3737 if (ret < 0)
3738 ALOGE("%s: audio_extn_ip_hdlr_intf_close failed %d",__func__, ret);
3739 }
3740
juyuchen2d415992018-11-16 14:15:16 +08003741 /* 1) media + voip output routing to handset must route media back to
3742 speaker when voip stops.
3743 2) trigger voip input to reroute when voip output changes to
3744 hearing aid. */
Aalique Grahame22e49102018-12-18 14:23:57 -08003745 if (has_voip_usecase ||
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003746 compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
Aalique Grahame22e49102018-12-18 14:23:57 -08003747 struct listnode *node;
3748 struct audio_usecase *usecase;
3749 list_for_each(node, &adev->usecase_list) {
3750 usecase = node_to_item(node, struct audio_usecase, list);
juyuchen2d415992018-11-16 14:15:16 +08003751 if ((usecase->type == PCM_CAPTURE &&
3752 usecase->id != USECASE_AUDIO_RECORD_VOIP)
3753 || usecase == uc_info)
Aalique Grahame22e49102018-12-18 14:23:57 -08003754 continue;
3755
3756 ALOGD("%s: select_devices at usecase(%d: %s) after removing the usecase(%d: %s)",
3757 __func__, usecase->id, use_case_table[usecase->id],
3758 out->usecase, use_case_table[out->usecase]);
3759 select_devices(adev, usecase->id);
3760 }
3761 }
3762
Garmond Leung5fd0b552018-04-17 11:56:12 -07003763 free(uc_info);
Eric Laurent994a6932013-07-17 11:51:42 -07003764 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003765 return ret;
3766}
3767
Vignesh Kulothungana6927272019-02-20 15:17:07 -08003768struct pcm* pcm_open_prepare_helper(unsigned int snd_card, unsigned int pcm_device_id,
3769 unsigned int flags, unsigned int pcm_open_retry_count,
3770 struct pcm_config *config)
3771{
3772 struct pcm* pcm = NULL;
3773
3774 while (1) {
3775 pcm = pcm_open(snd_card, pcm_device_id, flags, config);
3776 if (pcm == NULL || !pcm_is_ready(pcm)) {
3777 ALOGE("%s: %s", __func__, pcm_get_error(pcm));
3778 if (pcm != NULL) {
3779 pcm_close(pcm);
3780 pcm = NULL;
3781 }
Weiyin Jiang72197252019-10-09 11:49:32 +08003782 if (pcm_open_retry_count == 0)
Vignesh Kulothungana6927272019-02-20 15:17:07 -08003783 return NULL;
3784
Weiyin Jiang72197252019-10-09 11:49:32 +08003785 pcm_open_retry_count--;
Vignesh Kulothungana6927272019-02-20 15:17:07 -08003786 usleep(PROXY_OPEN_WAIT_TIME * 1000);
3787 continue;
3788 }
3789 break;
3790 }
3791
3792 if (pcm_is_ready(pcm)) {
3793 int ret = pcm_prepare(pcm);
3794 if (ret < 0) {
3795 ALOGE("%s: pcm_prepare returned %d", __func__, ret);
3796 pcm_close(pcm);
3797 pcm = NULL;
3798 }
3799 }
3800
3801 return pcm;
3802}
3803
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003804int start_output_stream(struct stream_out *out)
3805{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003806 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003807 struct audio_usecase *uc_info;
3808 struct audio_device *adev = out->dev;
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08003809 char mixer_ctl_name[128];
3810 struct mixer_ctl *ctl = NULL;
3811 char* perf_mode[] = {"ULL", "ULL_PP", "LL"};
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303812 bool a2dp_combo = false;
Vignesh Kulothungana6927272019-02-20 15:17:07 -08003813 bool is_haptic_usecase = (out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) ? true: false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003814
Haynes Mathew George380745d2017-10-04 15:27:45 -07003815 ATRACE_BEGIN("start_output_stream");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07003816 if ((out->usecase < 0) || (out->usecase >= AUDIO_USECASE_MAX)) {
3817 ret = -EINVAL;
3818 goto error_config;
3819 }
3820
Vignesh Kulothungana6927272019-02-20 15:17:07 -08003821 ALOGD("%s: enter: stream(%p)usecase(%d: %s) devices(%#x) is_haptic_usecase(%d)",
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05303822 __func__, &out->stream, out->usecase, use_case_table[out->usecase],
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003823 get_device_types(&out->device_list), is_haptic_usecase);
3824
3825 bool is_speaker_active = compare_device_type(&out->device_list,
3826 AUDIO_DEVICE_OUT_SPEAKER);
3827 bool is_speaker_safe_active = compare_device_type(&out->device_list,
3828 AUDIO_DEVICE_OUT_SPEAKER_SAFE);
Naresh Tanniru4c630392014-05-12 01:05:52 +05303829
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05303830 if (CARD_STATUS_OFFLINE == out->card_status ||
3831 CARD_STATUS_OFFLINE == adev->card_status) {
3832 ALOGW("out->card_status or adev->card_status offline, try again");
Dhanalakshmi Siddani4d57e992014-07-17 16:37:51 +05303833 ret = -EIO;
Mingshu Pang5fc696f2020-02-28 12:32:00 +08003834 goto error_fatal;
Naresh Tanniru4c630392014-05-12 01:05:52 +05303835 }
Naresh Tanniru4c630392014-05-12 01:05:52 +05303836
Arun Mirpuri2ce92a62018-09-12 18:36:10 -07003837 //Update incall music usecase to reflect correct voice session
3838 if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
3839 ret = voice_extn_check_and_set_incall_music_usecase(adev, out);
3840 if (ret != 0) {
3841 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
3842 __func__, ret);
3843 goto error_config;
3844 }
3845 }
3846
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003847 if (is_a2dp_out_device_type(&out->device_list)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02003848 if (!audio_extn_a2dp_source_is_ready()) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003849 if (is_speaker_active || is_speaker_safe_active) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303850 a2dp_combo = true;
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05303851 } else {
Zhou Song9ebf6792020-09-23 22:49:01 +08003852 if (!is_offload_usecase(out->usecase)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303853 ALOGE("%s: A2DP profile is not ready, return error", __func__);
3854 ret = -EAGAIN;
3855 goto error_config;
3856 }
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05303857 }
3858 }
3859 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003860 if (is_sco_out_device_type(&out->device_list)) {
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303861 if (!adev->bt_sco_on) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003862 if (is_speaker_active) {
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303863 //combo usecase just by pass a2dp
3864 ALOGW("%s: SCO is not connected, route it to speaker", __func__);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003865 reassign_device_list(&out->device_list, AUDIO_DEVICE_OUT_SPEAKER, "");
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303866 } else {
3867 ALOGE("%s: SCO profile is not ready, return error", __func__);
3868 ret = -EAGAIN;
3869 goto error_config;
3870 }
3871 }
3872 }
3873
Eric Laurentb23d5282013-05-14 15:27:20 -07003874 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003875 if (out->pcm_device_id < 0) {
3876 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
3877 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08003878 ret = -EINVAL;
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07003879 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003880 }
3881
Vignesh Kulothungana6927272019-02-20 15:17:07 -08003882 if (is_haptic_usecase) {
Meng Wang51d8c2a2020-04-27 15:23:21 +08003883 adev->haptic_pcm_device_id = platform_get_pcm_device_id(
3884 USECASE_AUDIO_PLAYBACK_HAPTICS, PCM_PLAYBACK);
Vignesh Kulothungana6927272019-02-20 15:17:07 -08003885 if (adev->haptic_pcm_device_id < 0) {
3886 ALOGE("%s: Invalid Haptics pcm device id(%d) for the usecase(%d)",
3887 __func__, adev->haptic_pcm_device_id, out->usecase);
3888 ret = -EINVAL;
3889 goto error_config;
3890 }
3891 }
3892
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003893 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07003894
3895 if (!uc_info) {
3896 ret = -ENOMEM;
3897 goto error_config;
3898 }
3899
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003900 uc_info->id = out->usecase;
3901 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08003902 uc_info->stream.out = out;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003903 list_init(&uc_info->device_list);
3904 assign_devices(&uc_info->device_list, &out->device_list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003905 uc_info->in_snd_device = SND_DEVICE_NONE;
3906 uc_info->out_snd_device = SND_DEVICE_NONE;
Garmond Leung5fd0b552018-04-17 11:56:12 -07003907
3908 /* This must be called before adding this usecase to the list */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003909 if (is_usb_out_device_type(&out->device_list)) {
Garmond Leung5fd0b552018-04-17 11:56:12 -07003910 audio_extn_usb_check_and_set_svc_int(uc_info, true);
3911 /* USB backend is not reopened immediately.
3912 This is eventually done as part of select_devices */
3913 }
3914
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08003915 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003916
Wei Wangf7ca6c92017-11-21 14:51:20 -08003917 audio_streaming_hint_start();
Sudheer Papothifa9d2282015-09-17 01:53:25 +05303918 audio_extn_perf_lock_acquire(&adev->perf_lock_handle, 0,
3919 adev->perf_lock_opts,
3920 adev->perf_lock_opts_size);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05303921
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003922 if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
Md Mansoor Ahmeddb1b4f92018-01-25 18:56:31 +05303923 audio_extn_keep_alive_stop(KEEP_ALIVE_OUT_HDMI);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05303924 if (audio_extn_passthru_is_enabled() &&
3925 audio_extn_passthru_is_passthrough_stream(out)) {
3926 audio_extn_passthru_on_start(out);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05303927 }
3928 }
3929
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003930 if (is_a2dp_out_device_type(&out->device_list) &&
Florian Pfister1a84f312018-07-19 14:38:18 +02003931 (!audio_extn_a2dp_source_is_ready())) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303932 if (!a2dp_combo) {
3933 check_a2dp_restore_l(adev, out, false);
3934 } else {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003935 struct listnode dev;
3936 list_init(&dev);
3937 assign_devices(&dev, &out->device_list);
3938 if (compare_device_type(&dev, AUDIO_DEVICE_OUT_SPEAKER_SAFE))
3939 reassign_device_list(&out->device_list,
3940 AUDIO_DEVICE_OUT_SPEAKER_SAFE, "");
Aalique Grahame22e49102018-12-18 14:23:57 -08003941 else
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003942 reassign_device_list(&out->device_list,
3943 AUDIO_DEVICE_OUT_SPEAKER, "");
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303944 select_devices(adev, out->usecase);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003945 assign_devices(&out->device_list, &dev);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303946 }
3947 } else {
Revathi Uddarajub26e3932020-06-10 14:51:02 +05303948 select_devices(adev, out->usecase);
3949 if (is_a2dp_out_device_type(&out->device_list) &&
3950 !adev->a2dp_started) {
3951 if (is_speaker_active || is_speaker_safe_active) {
3952 struct listnode dev;
3953 list_init(&dev);
3954 assign_devices(&dev, &out->device_list);
3955 if (compare_device_type(&dev, AUDIO_DEVICE_OUT_SPEAKER_SAFE))
3956 reassign_device_list(&out->device_list,
3957 AUDIO_DEVICE_OUT_SPEAKER_SAFE, "");
3958 else
3959 reassign_device_list(&out->device_list,
3960 AUDIO_DEVICE_OUT_SPEAKER, "");
3961 select_devices(adev, out->usecase);
3962 assign_devices(&out->device_list, &dev);
3963 } else {
3964 ret = -EINVAL;
3965 goto error_open;
3966 }
3967 }
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303968 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003969
Arun Mirpuri2ce92a62018-09-12 18:36:10 -07003970 if (out->usecase == USECASE_INCALL_MUSIC_UPLINK ||
3971 out->usecase == USECASE_INCALL_MUSIC_UPLINK2) {
Arun Mirpurief53ce52018-09-11 18:00:09 -07003972 voice_set_device_mute_flag(adev, true);
Arun Mirpuri2ce92a62018-09-12 18:36:10 -07003973 }
Arun Mirpurief53ce52018-09-11 18:00:09 -07003974
Derek Chenea197282019-01-07 17:35:01 -08003975 if (audio_extn_ext_hw_plugin_usecase_start(adev->ext_hw_plugin, uc_info))
3976 ALOGE("%s: failed to start ext hw plugin", __func__);
Derek Chend2530072014-11-24 12:39:14 -08003977
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003978 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d) format(%#x)",
3979 __func__, adev->snd_card, out->pcm_device_id, out->config.format);
Haynes Mathew George16081042017-05-31 17:16:49 -07003980
3981 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
Arun Mirpuri5d170872019-03-26 13:21:31 -07003982 ALOGD("%s: Starting MMAP stream", __func__);
Haynes Mathew George16081042017-05-31 17:16:49 -07003983 if (out->pcm == NULL || !pcm_is_ready(out->pcm)) {
3984 ALOGE("%s: pcm stream not ready", __func__);
3985 goto error_open;
3986 }
3987 ret = pcm_start(out->pcm);
3988 if (ret < 0) {
3989 ALOGE("%s: MMAP pcm_start failed ret %d", __func__, ret);
3990 goto error_open;
3991 }
Arun Mirpuri5d170872019-03-26 13:21:31 -07003992 out_set_mmap_volume(&out->stream, out->volume_l, out->volume_r);
Haynes Mathew George16081042017-05-31 17:16:49 -07003993 } else if (!is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003994 unsigned int flags = PCM_OUT;
3995 unsigned int pcm_open_retry_count = 0;
3996 if (out->usecase == USECASE_AUDIO_PLAYBACK_AFE_PROXY) {
3997 flags |= PCM_MMAP | PCM_NOIRQ;
3998 pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT;
Haynes Mathew George5beddd42016-06-27 18:33:40 -07003999 } else if (out->realtime) {
Haynes Mathew George4ab3ba92017-12-11 14:49:43 -08004000 flags |= PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07004001 } else
4002 flags |= PCM_MONOTONIC;
4003
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08004004 if ((adev->vr_audio_mode_enabled) &&
4005 (out->flags & AUDIO_OUTPUT_FLAG_RAW)) {
4006 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
4007 "PCM_Dev %d Topology", out->pcm_device_id);
4008 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
4009 if (!ctl) {
4010 ALOGI("%s: Could not get ctl for mixer cmd might be ULL - %s",
4011 __func__, mixer_ctl_name);
4012 } else {
4013 //if success use ULLPP
4014 ALOGI("%s: mixer ctrl %s succeeded setting up ULL for %d",
4015 __func__, mixer_ctl_name, out->pcm_device_id);
4016 //There is a still a possibility that some sessions
4017 // that request for FAST|RAW when 3D audio is active
4018 //can go through ULLPP. Ideally we expects apps to
4019 //listen to audio focus and stop concurrent playback
4020 //Also, we will look for mode flag (voice_in_communication)
4021 //before enabling the realtime flag.
4022 mixer_ctl_set_enum_by_string(ctl, perf_mode[1]);
4023 }
4024 }
4025
Surendar Karka91fa3682018-07-02 18:12:12 +05304026 if (out->realtime)
4027 platform_set_stream_channel_map(adev->platform, out->channel_mask,
4028 out->pcm_device_id, &out->channel_map_param.channel_map[0]);
4029
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004030 out->pcm = pcm_open_prepare_helper(adev->snd_card, out->pcm_device_id,
4031 flags, pcm_open_retry_count,
4032 &(out->config));
4033 if (out->pcm == NULL) {
4034 ret = -EIO;
4035 goto error_open;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004036 }
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004037
4038 if (is_haptic_usecase) {
4039 adev->haptic_pcm = pcm_open_prepare_helper(adev->snd_card,
4040 adev->haptic_pcm_device_id,
4041 flags, pcm_open_retry_count,
4042 &(adev->haptics_config));
4043 // failure to open haptics pcm shouldnt stop audio,
4044 // so do not close audio pcm in case of error
Vignesh Kulothungane4039c12019-05-07 15:51:39 -07004045
4046 if (property_get_bool("vendor.audio.enable_haptic_audio_sync", false)) {
4047 ALOGD("%s: enable haptic audio synchronization", __func__);
4048 platform_set_qtime(adev->platform, out->pcm_device_id, adev->haptic_pcm_device_id);
4049 }
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004050 }
4051
Surendar Karka91fa3682018-07-02 18:12:12 +05304052 if (!out->realtime)
4053 platform_set_stream_channel_map(adev->platform, out->channel_mask,
Surendar Karkaf51b5842018-04-26 11:28:38 +05304054 out->pcm_device_id, &out->channel_map_param.channel_map[0]);
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07004055
Zhou Song2b8f28f2017-09-11 10:51:38 +08004056 // apply volume for voip playback after path is set up
4057 if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP)
4058 out_set_voip_volume(&out->stream, out->volume_l, out->volume_r);
Ramu Gottipati36547092018-12-28 11:32:09 +05304059 else if ((out->usecase == USECASE_AUDIO_PLAYBACK_LOW_LATENCY || out->usecase == USECASE_AUDIO_PLAYBACK_DEEP_BUFFER ||
4060 out->usecase == USECASE_AUDIO_PLAYBACK_ULL) && (out->apply_volume)) {
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05304061 out_set_pcm_volume(&out->stream, out->volume_l, out->volume_r);
4062 out->apply_volume = false;
Derek Chenf13dd492018-11-13 14:53:51 -08004063 } else if (audio_extn_auto_hal_is_bus_device_usecase(out->usecase)) {
4064 out_set_pcm_volume(&out->stream, out->volume_l, out->volume_r);
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05304065 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004066 } else {
Zhou Song79929fe2020-01-20 17:57:43 +08004067 /*
4068 * set custom channel map if:
4069 * 1. neither mono nor stereo clips i.e. channels > 2 OR
4070 * 2. custom channel map has been set by client
4071 * else default channel map of FC/FR/FL can always be set to DSP
4072 */
4073 if (popcount(out->channel_mask) > 2 || out->channel_map_param.channel_map[0])
4074 platform_set_stream_channel_map(adev->platform, out->channel_mask,
4075 out->pcm_device_id, &out->channel_map_param.channel_map[0]);
Xiaojun Sang785b5da2017-08-03 15:52:29 +08004076 audio_enable_asm_bit_width_enforce_mode(adev->mixer,
4077 adev->dsp_bit_width_enforce_mode,
4078 true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004079 out->pcm = NULL;
Haynes Mathew George380745d2017-10-04 15:27:45 -07004080 ATRACE_BEGIN("compress_open");
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08004081 out->compr = compress_open(adev->snd_card,
4082 out->pcm_device_id,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004083 COMPRESS_IN, &out->compr_config);
Haynes Mathew George380745d2017-10-04 15:27:45 -07004084 ATRACE_END();
Satish Babu Patakokila54ce83d2018-07-06 18:00:37 +05304085 if (errno == ENETRESET && !is_compress_ready(out->compr)) {
Sharad Sanglec6f32552018-05-04 16:15:38 +05304086 ALOGE("%s: compress_open failed errno:%d\n", __func__, errno);
4087 adev->card_status = CARD_STATUS_OFFLINE;
4088 out->card_status = CARD_STATUS_OFFLINE;
4089 ret = -EIO;
4090 goto error_open;
4091 }
4092
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004093 if (out->compr && !is_compress_ready(out->compr)) {
Haynes Mathew Georgeee5836f2017-11-21 18:02:10 -08004094 ALOGE("%s: failed /w error %s", __func__, compress_get_error(out->compr));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004095 compress_close(out->compr);
4096 out->compr = NULL;
4097 ret = -EIO;
4098 goto error_open;
4099 }
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05304100 /* compress_open sends params of the track, so reset the flag here */
4101 out->is_compr_metadata_avail = false;
4102
Ben Rombergerd771a7c2017-02-22 18:05:17 -08004103 if (out->client_callback)
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004104 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07004105
Fred Oh3f43e742015-03-04 18:42:34 -08004106 /* Since small bufs uses blocking writes, a write will be blocked
4107 for the default max poll time (20s) in the event of an SSR.
4108 Reduce the poll time to observe and deal with SSR faster.
4109 */
Ashish Jain5106d362016-05-11 19:23:33 +05304110 if (!out->non_blocking) {
Fred Oh3f43e742015-03-04 18:42:34 -08004111 compress_set_max_poll_wait(out->compr, 1000);
4112 }
4113
Manish Dewangan69426c82017-01-30 17:35:36 +05304114 audio_extn_utils_compress_set_render_mode(out);
Manish Dewangan58229382017-02-02 15:48:41 +05304115 audio_extn_utils_compress_set_clk_rec_mode(uc_info);
Manish Dewangan69426c82017-01-30 17:35:36 +05304116
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08004117 audio_extn_dts_create_state_notifier_node(out->usecase);
4118 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
4119 popcount(out->channel_mask),
4120 out->playback_started);
4121
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08004122#ifdef DS1_DOLBY_DDP_ENABLED
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05304123 if (audio_extn_utils_is_dolby_format(out->format))
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08004124 audio_extn_dolby_send_ddp_endp_params(adev);
4125#endif
Preetam Singh Ranawatd18d8832017-02-08 17:34:54 +05304126 if (!(audio_extn_passthru_is_passthrough_stream(out)) &&
4127 (out->sample_rate != 176400 && out->sample_rate <= 192000)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004128 if (adev->visualizer_start_output != NULL)
4129 adev->visualizer_start_output(out->handle, out->pcm_device_id);
4130 if (adev->offload_effects_start_output != NULL)
Ashish Jain5106d362016-05-11 19:23:33 +05304131 adev->offload_effects_start_output(out->handle, out->pcm_device_id, adev->mixer);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08004132 audio_extn_check_and_set_dts_hpx_state(adev);
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004133 }
Derek Chenf13dd492018-11-13 14:53:51 -08004134
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004135 if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_BUS)) {
Derek Chenf13dd492018-11-13 14:53:51 -08004136 /* Update cached volume from media to offload/direct stream */
4137 struct listnode *node = NULL;
4138 list_for_each(node, &adev->active_outputs_list) {
4139 streams_output_ctxt_t *out_ctxt = node_to_item(node,
4140 streams_output_ctxt_t,
4141 list);
4142 if (out_ctxt->output->usecase == USECASE_AUDIO_PLAYBACK_MEDIA) {
4143 out->volume_l = out_ctxt->output->volume_l;
4144 out->volume_r = out_ctxt->output->volume_r;
4145 }
4146 }
4147 out_set_compr_volume(&out->stream,
4148 out->volume_l, out->volume_r);
4149 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004150 }
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004151
4152 if (ret == 0) {
4153 register_out_stream(out);
4154 if (out->realtime) {
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07004155 if (out->pcm == NULL || !pcm_is_ready(out->pcm)) {
4156 ALOGE("%s: pcm stream not ready", __func__);
4157 goto error_open;
4158 }
Haynes Mathew George380745d2017-10-04 15:27:45 -07004159 ATRACE_BEGIN("pcm_start");
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004160 ret = pcm_start(out->pcm);
Haynes Mathew George380745d2017-10-04 15:27:45 -07004161 ATRACE_END();
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004162 if (ret < 0)
4163 goto error_open;
4164 }
4165 }
Wei Wangf7ca6c92017-11-21 14:51:20 -08004166 audio_streaming_hint_end();
Sudheer Papothifa9d2282015-09-17 01:53:25 +05304167 audio_extn_perf_lock_release(&adev->perf_lock_handle);
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07004168 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07004169
vivek mehtad15d2bf2019-05-17 13:35:10 -07004170 if (out->usecase == USECASE_AUDIO_PLAYBACK_ULL ||
4171 out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
4172 audio_low_latency_hint_start();
4173 }
4174
Manish Dewangan21a850a2017-08-14 12:03:55 +05304175 if (out->ip_hdlr_handle) {
Vidyakumar Athota6d655882017-05-22 18:26:24 -07004176 ret = audio_extn_ip_hdlr_intf_open(out->ip_hdlr_handle, true, out, out->usecase);
Naresh Tanniru85819452017-05-04 18:55:45 -07004177 if (ret < 0)
4178 ALOGE("%s: audio_extn_ip_hdlr_intf_open failed %d",__func__, ret);
4179 }
4180
Vignesh Kulothungan3b5fae52017-09-25 12:16:30 -07004181 // consider a scenario where on pause lower layers are tear down.
4182 // so on resume, swap mixer control need to be sent only when
4183 // backend is active, hence rather than sending from enable device
4184 // sending it from start of streamtream
4185
4186 platform_set_swap_channels(adev, true);
4187
Haynes Mathew George380745d2017-10-04 15:27:45 -07004188 ATRACE_END();
Vatsal Buchac09ae062018-11-14 13:25:08 +05304189 enable_gcov();
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004190 return ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004191error_open:
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004192 if (adev->haptic_pcm) {
4193 pcm_close(adev->haptic_pcm);
4194 adev->haptic_pcm = NULL;
4195 }
Wei Wangf7ca6c92017-11-21 14:51:20 -08004196 audio_streaming_hint_end();
Sudheer Papothifa9d2282015-09-17 01:53:25 +05304197 audio_extn_perf_lock_release(&adev->perf_lock_handle);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004198 stop_output_stream(out);
Mingshu Pang5fc696f2020-02-28 12:32:00 +08004199error_fatal:
Laxminath Kasam2cb4b752015-09-24 03:59:15 +05304200 /*
4201 * sleep 50ms to allow sufficient time for kernel
4202 * drivers to recover incases like SSR.
4203 */
4204 usleep(50000);
Mingshu Pang5fc696f2020-02-28 12:32:00 +08004205error_config:
Haynes Mathew George380745d2017-10-04 15:27:45 -07004206 ATRACE_END();
Vatsal Buchac09ae062018-11-14 13:25:08 +05304207 enable_gcov();
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08004208 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004209}
4210
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004211static int check_input_parameters(uint32_t sample_rate,
4212 audio_format_t format,
Aalique Grahame22e49102018-12-18 14:23:57 -08004213 int channel_count,
4214 bool is_usb_hifi)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004215{
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08004216 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004217
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05304218 if (((format != AUDIO_FORMAT_PCM_16_BIT) && (format != AUDIO_FORMAT_PCM_8_24_BIT) &&
4219 (format != AUDIO_FORMAT_PCM_24_BIT_PACKED) && (format != AUDIO_FORMAT_PCM_32_BIT) &&
4220 (format != AUDIO_FORMAT_PCM_FLOAT)) &&
Mingming Yine62d7842013-10-25 16:26:03 -07004221 !voice_extn_compress_voip_is_format_supported(format) &&
Ralf Herzaec80262018-07-03 07:08:49 +02004222 !audio_extn_compr_cap_format_supported(format) &&
4223 !audio_extn_cin_format_supported(format))
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004224 ret = -EINVAL;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08004225
Aalique Grahame22e49102018-12-18 14:23:57 -08004226 int max_channel_count = is_usb_hifi ? MAX_HIFI_CHANNEL_COUNT : MAX_CHANNEL_COUNT;
4227 if ((channel_count < MIN_CHANNEL_COUNT) || (channel_count > max_channel_count)) {
4228 ALOGE("%s: unsupported channel count (%d) passed Min / Max (%d / %d)", __func__,
4229 channel_count, MIN_CHANNEL_COUNT, max_channel_count);
4230 return -EINVAL;
4231 }
4232
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08004233 switch (channel_count) {
4234 case 1:
4235 case 2:
Chaithanya Krishna Bacharaju9955b162016-05-25 16:25:53 +05304236 case 3:
4237 case 4:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08004238 case 6:
Karthikeyan Mani07faa602018-08-20 11:01:32 -07004239 case 8:
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05304240 case 10:
4241 case 12:
4242 case 14:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08004243 break;
4244 default:
4245 ret = -EINVAL;
4246 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004247
4248 switch (sample_rate) {
4249 case 8000:
4250 case 11025:
4251 case 12000:
4252 case 16000:
4253 case 22050:
4254 case 24000:
4255 case 32000:
4256 case 44100:
4257 case 48000:
Haynes Mathew Georgec9253d12017-12-13 15:58:28 -08004258 case 88200:
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05304259 case 96000:
Haynes Mathew Georgec9253d12017-12-13 15:58:28 -08004260 case 176400:
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05304261 case 192000:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004262 break;
4263 default:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08004264 ret = -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004265 }
4266
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08004267 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004268}
4269
Naresh Tanniru04f71882018-06-26 17:46:22 +05304270
4271/** Add a value in a list if not already present.
4272 * @return true if value was successfully inserted or already present,
4273 * false if the list is full and does not contain the value.
4274 */
4275static bool register_uint(uint32_t value, uint32_t* list, size_t list_length) {
4276 for (size_t i = 0; i < list_length; i++) {
4277 if (list[i] == value) return true; // value is already present
4278 if (list[i] == 0) { // no values in this slot
4279 list[i] = value;
4280 return true; // value inserted
4281 }
4282 }
4283 return false; // could not insert value
4284}
4285
4286/** Add channel_mask in supported_channel_masks if not already present.
4287 * @return true if channel_mask was successfully inserted or already present,
4288 * false if supported_channel_masks is full and does not contain channel_mask.
4289 */
4290static void register_channel_mask(audio_channel_mask_t channel_mask,
4291 audio_channel_mask_t supported_channel_masks[static MAX_SUPPORTED_CHANNEL_MASKS]) {
4292 ALOGE_IF(!register_uint(channel_mask, supported_channel_masks, MAX_SUPPORTED_CHANNEL_MASKS),
4293 "%s: stream can not declare supporting its channel_mask %x", __func__, channel_mask);
4294}
4295
4296/** Add format in supported_formats if not already present.
4297 * @return true if format was successfully inserted or already present,
4298 * false if supported_formats is full and does not contain format.
4299 */
4300static void register_format(audio_format_t format,
4301 audio_format_t supported_formats[static MAX_SUPPORTED_FORMATS]) {
4302 ALOGE_IF(!register_uint(format, supported_formats, MAX_SUPPORTED_FORMATS),
4303 "%s: stream can not declare supporting its format %x", __func__, format);
4304}
4305/** Add sample_rate in supported_sample_rates if not already present.
4306 * @return true if sample_rate was successfully inserted or already present,
4307 * false if supported_sample_rates is full and does not contain sample_rate.
4308 */
4309static void register_sample_rate(uint32_t sample_rate,
4310 uint32_t supported_sample_rates[static MAX_SUPPORTED_SAMPLE_RATES]) {
4311 ALOGE_IF(!register_uint(sample_rate, supported_sample_rates, MAX_SUPPORTED_SAMPLE_RATES),
4312 "%s: stream can not declare supporting its sample rate %x", __func__, sample_rate);
4313}
4314
Karthikeyan Manib38769c2018-11-07 15:44:13 -08004315static inline uint32_t lcm(uint32_t num1, uint32_t num2)
4316{
4317 uint32_t high = num1, low = num2, temp = 0;
4318
4319 if (!num1 || !num2)
4320 return 0;
4321
4322 if (num1 < num2) {
4323 high = num2;
4324 low = num1;
4325 }
4326
4327 while (low != 0) {
4328 temp = low;
4329 low = high % low;
4330 high = temp;
4331 }
4332 return (num1 * num2)/high;
4333}
4334
4335static inline uint32_t nearest_multiple(uint32_t num, uint32_t multiplier)
4336{
4337 uint32_t remainder = 0;
4338
4339 if (!multiplier)
4340 return num;
4341
4342 remainder = num % multiplier;
4343 if (remainder)
4344 num += (multiplier - remainder);
4345
4346 return num;
4347}
4348
Aalique Grahame22e49102018-12-18 14:23:57 -08004349static size_t get_stream_buffer_size(size_t duration_ms,
4350 uint32_t sample_rate,
4351 audio_format_t format,
4352 int channel_count,
4353 bool is_low_latency)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004354{
4355 size_t size = 0;
Karthikeyan Manib38769c2018-11-07 15:44:13 -08004356 uint32_t bytes_per_period_sample = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004357
Aalique Grahame22e49102018-12-18 14:23:57 -08004358 size = (sample_rate * duration_ms) / 1000;
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07004359 if (is_low_latency)
4360 size = configured_low_latency_capture_period_size;
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05304361
Karthikeyan Manib38769c2018-11-07 15:44:13 -08004362 bytes_per_period_sample = audio_bytes_per_sample(format) * channel_count;
Aalique Grahame22e49102018-12-18 14:23:57 -08004363 size *= audio_bytes_per_sample(format) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004364
Ralf Herzbd08d632018-09-28 15:50:49 +02004365 /* make sure the size is multiple of 32 bytes and additionally multiple of
4366 * the frame_size (required for 24bit samples and non-power-of-2 channel counts)
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07004367 * At 48 kHz mono 16-bit PCM:
4368 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
4369 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
Karthikeyan Manib38769c2018-11-07 15:44:13 -08004370 * Also, make sure the size is multiple of bytes per period sample
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07004371 */
Karthikeyan Manib38769c2018-11-07 15:44:13 -08004372 size = nearest_multiple(size, lcm(32, bytes_per_period_sample));
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07004373
4374 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004375}
4376
Aalique Grahame22e49102018-12-18 14:23:57 -08004377static size_t get_input_buffer_size(uint32_t sample_rate,
4378 audio_format_t format,
4379 int channel_count,
4380 bool is_low_latency)
4381{
4382 /* Don't know if USB HIFI in this context so use true to be conservative */
4383 if (check_input_parameters(sample_rate, format, channel_count,
4384 true /*is_usb_hifi */) != 0)
4385 return 0;
4386
4387 return get_stream_buffer_size(AUDIO_CAPTURE_PERIOD_DURATION_MSEC,
4388 sample_rate,
4389 format,
4390 channel_count,
4391 is_low_latency);
4392}
4393
Derek Chenf6318be2017-06-12 17:16:24 -04004394size_t get_output_period_size(uint32_t sample_rate,
4395 audio_format_t format,
4396 int channel_count,
4397 int duration /*in millisecs*/)
Ashish Jain058165c2016-09-28 23:18:48 +05304398{
4399 size_t size = 0;
4400 uint32_t bytes_per_sample = audio_bytes_per_sample(format);
4401
4402 if ((duration == 0) || (sample_rate == 0) ||
4403 (bytes_per_sample == 0) || (channel_count == 0)) {
4404 ALOGW("Invalid config duration %d sr %d bps %d ch %d", duration, sample_rate,
4405 bytes_per_sample, channel_count);
4406 return -EINVAL;
4407 }
4408
4409 size = (sample_rate *
4410 duration *
4411 bytes_per_sample *
4412 channel_count) / 1000;
4413 /*
4414 * To have same PCM samples for all channels, the buffer size requires to
4415 * be multiple of (number of channels * bytes per sample)
4416 * For writes to succeed, the buffer must be written at address which is multiple of 32
4417 */
4418 size = ALIGN(size, (bytes_per_sample * channel_count * 32));
4419
4420 return (size/(channel_count * bytes_per_sample));
4421}
4422
Zhou Song48453a02018-01-10 17:50:59 +08004423static uint64_t get_actual_pcm_frames_rendered(struct stream_out *out, struct timespec *timestamp)
Ashish Jain5106d362016-05-11 19:23:33 +05304424{
4425 uint64_t actual_frames_rendered = 0;
Weiyin Jiang4813da12020-05-28 00:37:28 +08004426 uint64_t written_frames = 0;
4427 uint64_t kernel_frames = 0;
4428 uint64_t dsp_frames = 0;
4429 uint64_t signed_frames = 0;
4430 size_t kernel_buffer_size = 0;
Ashish Jain5106d362016-05-11 19:23:33 +05304431
4432 /* This adjustment accounts for buffering after app processor.
4433 * It is based on estimated DSP latency per use case, rather than exact.
4434 */
George Gao9ba8a142020-07-23 14:30:03 -07004435 dsp_frames = platform_render_latency(out) *
Weiyin Jiang4813da12020-05-28 00:37:28 +08004436 out->sample_rate / 1000000LL;
Ashish Jain5106d362016-05-11 19:23:33 +05304437
Zhou Song48453a02018-01-10 17:50:59 +08004438 pthread_mutex_lock(&out->position_query_lock);
Weiyin Jiang4813da12020-05-28 00:37:28 +08004439 written_frames = out->written /
4440 (audio_bytes_per_sample(out->hal_ip_format) * popcount(out->channel_mask));
4441
Ashish Jain5106d362016-05-11 19:23:33 +05304442 /* not querying actual state of buffering in kernel as it would involve an ioctl call
4443 * which then needs protection, this causes delay in TS query for pcm_offload usecase
4444 * hence only estimate.
4445 */
Weiyin Jiang4813da12020-05-28 00:37:28 +08004446 kernel_buffer_size = out->compr_config.fragment_size * out->compr_config.fragments;
4447 kernel_frames = kernel_buffer_size /
4448 (audio_bytes_per_sample(out->hal_op_format) * popcount(out->channel_mask));
Ashish Jain5106d362016-05-11 19:23:33 +05304449
Weiyin Jiang4813da12020-05-28 00:37:28 +08004450 if (written_frames >= (kernel_frames + dsp_frames))
4451 signed_frames = written_frames - kernel_frames - dsp_frames;
Ashish Jain5106d362016-05-11 19:23:33 +05304452
Zhou Song48453a02018-01-10 17:50:59 +08004453 if (signed_frames > 0) {
Ashish Jain5106d362016-05-11 19:23:33 +05304454 actual_frames_rendered = signed_frames;
Zhou Song48453a02018-01-10 17:50:59 +08004455 if (timestamp != NULL )
4456 *timestamp = out->writeAt;
4457 } else if (timestamp != NULL) {
4458 clock_gettime(CLOCK_MONOTONIC, timestamp);
4459 }
4460 pthread_mutex_unlock(&out->position_query_lock);
Ashish Jain5106d362016-05-11 19:23:33 +05304461
Weiyin Jiang4813da12020-05-28 00:37:28 +08004462 ALOGVV("%s signed frames %lld written frames %lld kernel frames %lld dsp frames %lld",
4463 __func__, signed_frames, written_frames, kernel_frames, dsp_frames);
Ashish Jain5106d362016-05-11 19:23:33 +05304464
4465 return actual_frames_rendered;
4466}
4467
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004468static uint32_t out_get_sample_rate(const struct audio_stream *stream)
4469{
4470 struct stream_out *out = (struct stream_out *)stream;
4471
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004472 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004473}
4474
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07004475static int out_set_sample_rate(struct audio_stream *stream __unused,
4476 uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004477{
4478 return -ENOSYS;
4479}
4480
4481static size_t out_get_buffer_size(const struct audio_stream *stream)
4482{
4483 struct stream_out *out = (struct stream_out *)stream;
4484
Varun Balaraje49253e2017-07-06 19:48:56 +05304485 if (is_interactive_usecase(out->usecase)) {
Sri Karri27279e12017-08-07 16:05:20 +05304486 return out->config.period_size * out->config.period_count;
Varun Balaraje49253e2017-07-06 19:48:56 +05304487 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Naresh Tanniruee3499a2017-01-05 14:05:35 +05304488 if (out->flags & AUDIO_OUTPUT_FLAG_TIMESTAMP)
4489 return out->compr_config.fragment_size - sizeof(struct snd_codec_metadata);
4490 else
4491 return out->compr_config.fragment_size;
4492 } else if(out->usecase == USECASE_COMPRESS_VOIP_CALL)
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08004493 return voice_extn_compress_voip_out_get_buffer_size(out);
Dhananjay Kumarac341582017-02-23 23:42:25 +05304494 else if (is_offload_usecase(out->usecase) &&
4495 out->flags == AUDIO_OUTPUT_FLAG_DIRECT)
Ashish Jain83a6cc22016-06-28 14:34:17 +05304496 return out->hal_fragment_size;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004497
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004498 return out->config.period_size * out->af_period_multiplier *
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07004499 audio_stream_out_frame_size((const struct audio_stream_out *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004500}
4501
4502static uint32_t out_get_channels(const struct audio_stream *stream)
4503{
4504 struct stream_out *out = (struct stream_out *)stream;
4505
4506 return out->channel_mask;
4507}
4508
4509static audio_format_t out_get_format(const struct audio_stream *stream)
4510{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004511 struct stream_out *out = (struct stream_out *)stream;
4512
4513 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004514}
4515
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07004516static int out_set_format(struct audio_stream *stream __unused,
4517 audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004518{
4519 return -ENOSYS;
4520}
4521
4522static int out_standby(struct audio_stream *stream)
4523{
4524 struct stream_out *out = (struct stream_out *)stream;
4525 struct audio_device *adev = out->dev;
Haynes Mathew George16081042017-05-31 17:16:49 -07004526 bool do_stop = true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004527
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05304528 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
4529 stream, out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004530
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07004531 lock_output_stream(out);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004532 if (!out->standby) {
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07004533 if (adev->adm_deregister_stream)
4534 adev->adm_deregister_stream(adev->adm_data, out->handle);
4535
Weiyin Jiangd5974e62020-09-08 20:28:22 +08004536 if (is_offload_usecase(out->usecase)) {
Haynes Mathew George7fce0a52016-06-23 18:22:27 -07004537 stop_compressed_output_l(out);
Weiyin Jiangd5974e62020-09-08 20:28:22 +08004538 }
Haynes Mathew George7fce0a52016-06-23 18:22:27 -07004539
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08004540 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004541 out->standby = true;
Zhou Songa8895042016-07-05 17:54:22 +08004542 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
4543 voice_extn_compress_voip_close_output_stream(stream);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304544 out->started = 0;
Zhou Songa8895042016-07-05 17:54:22 +08004545 pthread_mutex_unlock(&adev->lock);
4546 pthread_mutex_unlock(&out->lock);
4547 ALOGD("VOIP output entered standby");
4548 return 0;
4549 } else if (!is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004550 if (out->pcm) {
4551 pcm_close(out->pcm);
4552 out->pcm = NULL;
4553 }
Meng Wanga09da002020-04-20 12:56:04 +08004554 if (out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) {
4555 if (adev->haptic_pcm) {
4556 pcm_close(adev->haptic_pcm);
4557 adev->haptic_pcm = NULL;
4558 }
4559
4560 if (adev->haptic_buffer != NULL) {
4561 free(adev->haptic_buffer);
4562 adev->haptic_buffer = NULL;
4563 adev->haptic_buffer_size = 0;
4564 }
4565 adev->haptic_pcm_device_id = 0;
4566 }
4567
Haynes Mathew George16081042017-05-31 17:16:49 -07004568 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
4569 do_stop = out->playback_started;
4570 out->playback_started = false;
Phil Burkd898ba62019-06-20 12:49:01 -07004571
4572 if (out->mmap_shared_memory_fd >= 0) {
4573 ALOGV("%s: closing mmap_shared_memory_fd = %d",
4574 __func__, out->mmap_shared_memory_fd);
4575 close(out->mmap_shared_memory_fd);
4576 out->mmap_shared_memory_fd = -1;
4577 }
Haynes Mathew George16081042017-05-31 17:16:49 -07004578 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004579 } else {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07004580 ALOGD("copl(%p):standby", out);
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05304581 out->send_next_track_params = false;
4582 out->is_compr_metadata_avail = false;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004583 out->gapless_mdata.encoder_delay = 0;
4584 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004585 if (out->compr != NULL) {
4586 compress_close(out->compr);
4587 out->compr = NULL;
4588 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08004589 }
Haynes Mathew George16081042017-05-31 17:16:49 -07004590 if (do_stop) {
4591 stop_output_stream(out);
4592 }
Lakshman Chaluvaraju06677b42019-06-24 10:04:52 +05304593 // if fm is active route on selected device in UI
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004594 audio_extn_fm_route_on_selected_device(adev, &out->device_list);
Eric Laurent150dbfe2013-02-27 14:31:02 -08004595 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004596 }
4597 pthread_mutex_unlock(&out->lock);
Ashish Jainbbce4322016-02-16 13:25:27 +05304598 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004599 return 0;
4600}
4601
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304602static int out_on_error(struct audio_stream *stream)
4603{
4604 struct stream_out *out = (struct stream_out *)stream;
Ben Rombergerfd02a2f2018-09-17 10:23:23 -07004605 int status = 0;
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304606
4607 lock_output_stream(out);
Haynes Mathew Georgeee5836f2017-11-21 18:02:10 -08004608 // always send CMD_ERROR for offload streams, this
4609 // is needed e.g. when SSR happens within compress_open
4610 // since the stream is active, offload_callback_thread is also active.
4611 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
4612 stop_compressed_output_l(out);
Ben Rombergerfd02a2f2018-09-17 10:23:23 -07004613 }
4614 pthread_mutex_unlock(&out->lock);
4615
4616 status = out_standby(&out->stream.common);
4617
4618 lock_output_stream(out);
4619 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Haynes Mathew Georgeee5836f2017-11-21 18:02:10 -08004620 send_offload_cmd_l(out, OFFLOAD_CMD_ERROR);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304621 }
Manisha Agarwal7b3e3772019-02-20 14:33:45 +05304622
4623 if (is_offload_usecase(out->usecase) && out->card_status == CARD_STATUS_OFFLINE) {
4624 ALOGD("Setting previous card status if offline");
4625 out->prev_card_status_offline = true;
4626 }
4627
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304628 pthread_mutex_unlock(&out->lock);
4629
Ben Rombergerfd02a2f2018-09-17 10:23:23 -07004630 return status;
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304631}
4632
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304633/*
Weiyin Jiangd5974e62020-09-08 20:28:22 +08004634 * standby implementation without locks, assumes that the callee already
4635 * has taken adev and out lock.
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304636 */
4637int out_standby_l(struct audio_stream *stream)
4638{
4639 struct stream_out *out = (struct stream_out *)stream;
4640 struct audio_device *adev = out->dev;
4641
4642 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
4643 stream, out->usecase, use_case_table[out->usecase]);
4644
4645 if (!out->standby) {
Haynes Mathew George380745d2017-10-04 15:27:45 -07004646 ATRACE_BEGIN("out_standby_l");
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304647 if (adev->adm_deregister_stream)
4648 adev->adm_deregister_stream(adev->adm_data, out->handle);
4649
Weiyin Jiangd5974e62020-09-08 20:28:22 +08004650 if (is_offload_usecase(out->usecase)) {
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304651 stop_compressed_output_l(out);
Weiyin Jiangd5974e62020-09-08 20:28:22 +08004652 }
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304653
4654 out->standby = true;
4655 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
4656 voice_extn_compress_voip_close_output_stream(stream);
4657 out->started = 0;
4658 ALOGD("VOIP output entered standby");
Haynes Mathew George380745d2017-10-04 15:27:45 -07004659 ATRACE_END();
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304660 return 0;
4661 } else if (!is_offload_usecase(out->usecase)) {
4662 if (out->pcm) {
4663 pcm_close(out->pcm);
4664 out->pcm = NULL;
4665 }
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004666 if (out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) {
4667 if (adev->haptic_pcm) {
4668 pcm_close(adev->haptic_pcm);
4669 adev->haptic_pcm = NULL;
4670 }
4671
4672 if (adev->haptic_buffer != NULL) {
4673 free(adev->haptic_buffer);
4674 adev->haptic_buffer = NULL;
4675 adev->haptic_buffer_size = 0;
4676 }
4677 adev->haptic_pcm_device_id = 0;
4678 }
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304679 } else {
4680 ALOGD("copl(%p):standby", out);
4681 out->send_next_track_params = false;
4682 out->is_compr_metadata_avail = false;
4683 out->gapless_mdata.encoder_delay = 0;
4684 out->gapless_mdata.encoder_padding = 0;
4685 if (out->compr != NULL) {
4686 compress_close(out->compr);
4687 out->compr = NULL;
4688 }
4689 }
4690 stop_output_stream(out);
Haynes Mathew George380745d2017-10-04 15:27:45 -07004691 ATRACE_END();
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304692 }
4693 ALOGD("%s: exit", __func__);
4694 return 0;
4695}
4696
Aalique Grahame22e49102018-12-18 14:23:57 -08004697static int out_dump(const struct audio_stream *stream, int fd)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004698{
Aalique Grahame22e49102018-12-18 14:23:57 -08004699 struct stream_out *out = (struct stream_out *)stream;
4700
4701 // We try to get the lock for consistency,
4702 // but it isn't necessary for these variables.
4703 // If we're not in standby, we may be blocked on a write.
4704 const bool locked = (pthread_mutex_trylock(&out->lock) == 0);
4705 dprintf(fd, " Standby: %s\n", out->standby ? "yes" : "no");
4706 dprintf(fd, " Frames written: %lld\n", (long long)out->written);
4707
Andy Hunga1f48fa2019-07-01 18:14:53 -07004708 char buffer[256]; // for statistics formatting
4709 if (!is_offload_usecase(out->usecase)) {
4710 simple_stats_to_string(&out->fifo_underruns, buffer, sizeof(buffer));
4711 dprintf(fd, " Fifo frame underruns: %s\n", buffer);
4712 }
4713
Andy Hungc6bfd4a2019-07-01 18:26:00 -07004714 if (out->start_latency_ms.n > 0) {
4715 simple_stats_to_string(&out->start_latency_ms, buffer, sizeof(buffer));
4716 dprintf(fd, " Start latency ms: %s\n", buffer);
4717 }
4718
Aalique Grahame22e49102018-12-18 14:23:57 -08004719 if (locked) {
4720 pthread_mutex_unlock(&out->lock);
4721 }
4722
4723 // dump error info
4724 (void)error_log_dump(
4725 out->error_log, fd, " " /* prefix */, 0 /* lines */, 0 /* limit_ns */);
4726
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004727 return 0;
4728}
4729
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004730static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
4731{
4732 int ret = 0;
4733 char value[32];
ApurupaPattapu2e084df2013-12-18 15:47:59 -08004734
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004735 if (!out || !parms) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08004736 ALOGE("%s: return invalid ",__func__);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004737 return -EINVAL;
4738 }
4739
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +05304740 ret = audio_extn_parse_compress_metadata(out, parms);
Weiyin Jiang18ac4e92015-03-15 15:03:40 +08004741
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004742 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
4743 if (ret >= 0) {
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +05304744 out->gapless_mdata.encoder_delay = atoi(value); //whats a good limit check?
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004745 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004746 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
4747 if (ret >= 0) {
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +05304748 out->gapless_mdata.encoder_padding = atoi(value);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004749 }
4750
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004751 ALOGV("%s new encoder delay %u and padding %u", __func__,
4752 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
4753
4754 return 0;
4755}
4756
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07004757static bool output_drives_call(struct audio_device *adev, struct stream_out *out)
4758{
4759 return out == adev->primary_output || out == adev->voice_tx_output;
4760}
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004761
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304762// note: this call is safe only if the stream_cb is
4763// removed first in close_output_stream (as is done now).
4764static void out_snd_mon_cb(void * stream, struct str_parms * parms)
4765{
4766 if (!stream || !parms)
4767 return;
4768
4769 struct stream_out *out = (struct stream_out *)stream;
4770 struct audio_device *adev = out->dev;
4771
4772 card_status_t status;
4773 int card;
4774 if (parse_snd_card_status(parms, &card, &status) < 0)
4775 return;
4776
4777 pthread_mutex_lock(&adev->lock);
4778 bool valid_cb = (card == adev->snd_card);
4779 pthread_mutex_unlock(&adev->lock);
4780
4781 if (!valid_cb)
4782 return;
4783
4784 lock_output_stream(out);
4785 if (out->card_status != status)
4786 out->card_status = status;
4787 pthread_mutex_unlock(&out->lock);
4788
4789 ALOGI("out_snd_mon_cb for card %d usecase %s, status %s", card,
4790 use_case_table[out->usecase],
4791 status == CARD_STATUS_OFFLINE ? "offline" : "online");
4792
Aditya Bavanari59ebed42019-02-05 17:44:57 +05304793 if (status == CARD_STATUS_OFFLINE) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304794 out_on_error(stream);
Aditya Bavanari59ebed42019-02-05 17:44:57 +05304795 if (voice_is_call_state_active(adev) &&
4796 out == adev->primary_output) {
4797 ALOGD("%s: SSR/PDR occurred, end all calls\n", __func__);
4798 pthread_mutex_lock(&adev->lock);
4799 voice_stop_call(adev);
4800 adev->mode = AUDIO_MODE_NORMAL;
4801 pthread_mutex_unlock(&adev->lock);
4802 }
4803 }
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304804 return;
4805}
4806
Kevin Rocardfce19002017-08-07 19:21:36 -07004807static int get_alive_usb_card(struct str_parms* parms) {
4808 int card;
4809 if ((str_parms_get_int(parms, "card", &card) >= 0) &&
4810 !audio_extn_usb_alive(card)) {
4811 return card;
4812 }
4813 return -ENODEV;
4814}
4815
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07004816int route_output_stream(struct stream_out *out,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004817 struct listnode *devices)
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07004818{
4819 struct audio_device *adev = out->dev;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07004820 int ret = 0;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004821 struct listnode new_devices;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07004822 bool bypass_a2dp = false;
4823 bool reconfig = false;
4824 unsigned long service_interval = 0;
4825
4826 ALOGD("%s: enter: usecase(%d: %s) devices %x",
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004827 __func__, out->usecase, use_case_table[out->usecase], get_device_types(devices));
4828
4829 list_init(&new_devices);
4830 assign_devices(&new_devices, devices);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07004831
4832 lock_output_stream(out);
4833 pthread_mutex_lock(&adev->lock);
4834
4835 /*
4836 * When HDMI cable is unplugged the music playback is paused and
4837 * the policy manager sends routing=0. But the audioflinger continues
4838 * to write data until standby time (3sec). As the HDMI core is
4839 * turned off, the write gets blocked.
4840 * Avoid this by routing audio to speaker until standby.
4841 */
Aniket Kumar Lata9c2fd892020-01-22 22:20:00 -08004842 if (is_single_device_type_equal(&out->device_list,
4843 AUDIO_DEVICE_OUT_AUX_DIGITAL) &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004844 list_empty(&new_devices) &&
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07004845 !audio_extn_passthru_is_passthrough_stream(out) &&
4846 (platform_get_edid_info(adev->platform) != 0) /* HDMI disconnected */) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004847 reassign_device_list(&new_devices, AUDIO_DEVICE_OUT_SPEAKER, "");
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07004848 }
4849 /*
4850 * When A2DP is disconnected the
4851 * music playback is paused and the policy manager sends routing=0
4852 * But the audioflinger continues to write data until standby time
4853 * (3sec). As BT is turned off, the write gets blocked.
4854 * Avoid this by routing audio to speaker until standby.
4855 */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004856 if (is_a2dp_out_device_type(&out->device_list) &&
4857 list_empty(&new_devices) &&
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07004858 !audio_extn_a2dp_source_is_ready() &&
4859 !adev->bt_sco_on) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004860 reassign_device_list(&new_devices, AUDIO_DEVICE_OUT_SPEAKER, "");
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07004861 }
4862 /*
4863 * When USB headset is disconnected the music platback paused
4864 * and the policy manager send routing=0. But if the USB is connected
4865 * back before the standby time, AFE is not closed and opened
4866 * when USB is connected back. So routing to speker will guarantee
4867 * AFE reconfiguration and AFE will be opend once USB is connected again
4868 */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004869 if (is_usb_out_device_type(&out->device_list) &&
4870 list_empty(&new_devices) &&
4871 !audio_extn_usb_connected(NULL)) {
4872 reassign_device_list(&new_devices, AUDIO_DEVICE_OUT_SPEAKER, "");
4873 }
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07004874 /* To avoid a2dp to sco overlapping / BT device improper state
4875 * check with BT lib about a2dp streaming support before routing
4876 */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004877 if (is_a2dp_out_device_type(&new_devices)) {
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07004878 if (!audio_extn_a2dp_source_is_ready()) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004879 if (compare_device_type(&new_devices, AUDIO_DEVICE_OUT_SPEAKER) ||
4880 compare_device_type(&new_devices, AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07004881 //combo usecase just by pass a2dp
4882 ALOGW("%s: A2DP profile is not ready,routing to speaker only", __func__);
4883 bypass_a2dp = true;
4884 } else {
4885 ALOGE("%s: A2DP profile is not ready,ignoring routing request", __func__);
4886 /* update device to a2dp and don't route as BT returned error
4887 * However it is still possible a2dp routing called because
4888 * of current active device disconnection (like wired headset)
4889 */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004890 assign_devices(&out->device_list, &new_devices);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07004891 pthread_mutex_unlock(&adev->lock);
4892 pthread_mutex_unlock(&out->lock);
4893 goto error;
4894 }
4895 }
4896 }
4897
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07004898 // Workaround: If routing to an non existing usb device, fail gracefully
4899 // The routing request will otherwise block during 10 second
4900 int card;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004901 if (is_usb_out_device_type(&new_devices)) {
4902 struct str_parms *parms =
4903 str_parms_create_str(get_usb_device_address(&new_devices));
4904 if (!parms)
4905 goto error;
4906 if ((card = get_alive_usb_card(parms)) >= 0) {
4907 ALOGW("%s: ignoring rerouting to non existing USB card %d", __func__, card);
4908 pthread_mutex_unlock(&adev->lock);
4909 pthread_mutex_unlock(&out->lock);
4910 str_parms_destroy(parms);
4911 ret = -ENOSYS;
4912 goto error;
4913 }
4914 str_parms_destroy(parms);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07004915 }
4916
Weiyin Jiang80c8f9a2020-01-10 20:42:08 +08004917 // Workaround: If routing to an non existing hdmi device, fail gracefully
4918 if (compare_device_type(&new_devices, AUDIO_DEVICE_OUT_AUX_DIGITAL) &&
4919 (platform_get_edid_info_v2(adev->platform,
4920 out->extconn.cs.controller,
4921 out->extconn.cs.stream) != 0)) {
4922 ALOGW("out_set_parameters() ignoring rerouting to non existing HDMI/DP");
4923 pthread_mutex_unlock(&adev->lock);
4924 pthread_mutex_unlock(&out->lock);
4925 ret = -ENOSYS;
4926 goto error;
4927 }
4928
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07004929 /*
4930 * select_devices() call below switches all the usecases on the same
4931 * backend to the new device. Refer to check_usecases_codec_backend() in
4932 * the select_devices(). But how do we undo this?
4933 *
4934 * For example, music playback is active on headset (deep-buffer usecase)
4935 * and if we go to ringtones and select a ringtone, low-latency usecase
4936 * will be started on headset+speaker. As we can't enable headset+speaker
4937 * and headset devices at the same time, select_devices() switches the music
4938 * playback to headset+speaker while starting low-lateny usecase for ringtone.
4939 * So when the ringtone playback is completed, how do we undo the same?
4940 *
4941 * We are relying on the out_set_parameters() call on deep-buffer output,
4942 * once the ringtone playback is ended.
4943 * NOTE: We should not check if the current devices are same as new devices.
4944 * Because select_devices() must be called to switch back the music
4945 * playback to headset.
4946 */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004947 if (!list_empty(&new_devices)) {
4948 bool same_dev = compare_devices(&out->device_list, &new_devices);
4949 assign_devices(&out->device_list, &new_devices);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07004950
4951 if (output_drives_call(adev, out)) {
4952 if (!voice_is_call_state_active(adev)) {
4953 if (adev->mode == AUDIO_MODE_IN_CALL) {
4954 adev->current_call_output = out;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07004955 ret = voice_start_call(adev);
4956 }
4957 } else {
4958 adev->current_call_output = out;
4959 voice_update_devices_for_all_voice_usecases(adev);
4960 }
4961 }
4962
Mingshu Pang60536d72020-09-09 15:28:22 +08004963 if (is_usb_out_device_type(&out->device_list)) {
4964 service_interval = audio_extn_usb_find_service_interval(false, true /*playback*/);
4965 audio_extn_usb_set_service_interval(true /*playback*/,
4966 service_interval,
4967 &reconfig);
4968 ALOGD("%s, svc_int(%ld),reconfig(%d)",__func__,service_interval, reconfig);
4969 }
4970
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07004971 if (!out->standby) {
4972 if (!same_dev) {
4973 ALOGV("update routing change");
4974 audio_extn_perf_lock_acquire(&adev->perf_lock_handle, 0,
4975 adev->perf_lock_opts,
4976 adev->perf_lock_opts_size);
4977 if (adev->adm_on_routing_change)
4978 adev->adm_on_routing_change(adev->adm_data,
4979 out->handle);
4980 }
4981 if (!bypass_a2dp) {
4982 select_devices(adev, out->usecase);
4983 } else {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004984 if (compare_device_type(&new_devices, AUDIO_DEVICE_OUT_SPEAKER_SAFE))
4985 reassign_device_list(&out->device_list, AUDIO_DEVICE_OUT_SPEAKER_SAFE, "");
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07004986 else
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004987 reassign_device_list(&out->device_list, AUDIO_DEVICE_OUT_SPEAKER, "");
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07004988 select_devices(adev, out->usecase);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004989 assign_devices(&out->device_list, &new_devices);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07004990 }
4991
4992 if (!same_dev) {
4993 // on device switch force swap, lower functions will make sure
4994 // to check if swap is allowed or not.
4995 platform_set_swap_channels(adev, true);
4996 audio_extn_perf_lock_release(&adev->perf_lock_handle);
4997 }
Zhou Song1f93fa52020-11-20 13:57:39 +08004998 pthread_mutex_lock(&out->latch_lock);
4999 if (!is_a2dp_out_device_type(&out->device_list) || audio_extn_a2dp_source_is_ready()) {
5000 if (out->a2dp_muted) {
5001 out->a2dp_muted = false;
5002 if (is_offload_usecase(out->usecase))
5003 out_set_compr_volume(&out->stream, out->volume_l, out->volume_r);
5004 else if (out->usecase != USECASE_AUDIO_PLAYBACK_VOIP)
5005 out_set_pcm_volume(&out->stream, out->volume_l, out->volume_r);
Weiyin Jiangd5974e62020-09-08 20:28:22 +08005006 }
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005007 }
Zhou Song1f93fa52020-11-20 13:57:39 +08005008 if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP && !out->a2dp_muted)
5009 out_set_voip_volume(&out->stream, out->volume_l, out->volume_r);
5010 pthread_mutex_unlock(&out->latch_lock);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005011 }
5012 }
5013
5014 pthread_mutex_unlock(&adev->lock);
5015 pthread_mutex_unlock(&out->lock);
5016
5017 /*handles device and call state changes*/
5018 audio_extn_extspk_update(adev->extspk);
5019
5020error:
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005021 ALOGV("%s: exit: code(%d)", __func__, ret);
5022 return ret;
5023}
5024
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005025static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
5026{
5027 struct stream_out *out = (struct stream_out *)stream;
5028 struct audio_device *adev = out->dev;
5029 struct str_parms *parms;
5030 char value[32];
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005031 int ret = 0, err;
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07005032 int ext_controller = -1;
5033 int ext_stream = -1;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005034
sangwoobc677242013-08-08 16:53:43 +09005035 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07005036 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005037 parms = str_parms_create_str(kvpairs);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05305038 if (!parms)
5039 goto error;
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07005040
5041 err = platform_get_controller_stream_from_params(parms, &ext_controller,
5042 &ext_stream);
Vignesh Kulothungan1b3957e2019-12-02 16:16:51 -08005043 if (err == 0) {
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07005044 out->extconn.cs.controller = ext_controller;
5045 out->extconn.cs.stream = ext_stream;
5046 ALOGD("%s: usecase(%s) new controller/stream (%d/%d)", __func__,
5047 use_case_table[out->usecase], out->extconn.cs.controller,
5048 out->extconn.cs.stream);
5049 }
5050
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07005051 if (out == adev->primary_output) {
5052 pthread_mutex_lock(&adev->lock);
5053 audio_extn_set_parameters(adev, parms);
5054 pthread_mutex_unlock(&adev->lock);
5055 }
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07005056 if (is_offload_usecase(out->usecase)) {
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005057 lock_output_stream(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005058 parse_compress_metadata(out, parms);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08005059
5060 audio_extn_dts_create_state_notifier_node(out->usecase);
5061 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
5062 popcount(out->channel_mask),
5063 out->playback_started);
5064
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08005065 pthread_mutex_unlock(&out->lock);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005066 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07005067
Surendar Karkaf51b5842018-04-26 11:28:38 +05305068 err = str_parms_get_str(parms, AUDIO_PARAMETER_DUAL_MONO, value,
5069 sizeof(value));
5070 if (err >= 0) {
5071 if (!strncmp("true", value, sizeof("true")) || atoi(value))
5072 audio_extn_send_dual_mono_mixing_coefficients(out);
5073 }
5074
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +05305075 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_PROFILE, value, sizeof(value));
5076 if (err >= 0) {
5077 strlcpy(out->profile, value, sizeof(out->profile));
5078 ALOGV("updating stream profile with value '%s'", out->profile);
5079 lock_output_stream(out);
5080 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
5081 &adev->streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005082 &out->device_list, out->flags,
5083 out->hal_op_format,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +05305084 out->sample_rate, out->bit_width,
5085 out->channel_mask, out->profile,
5086 &out->app_type_cfg);
5087 pthread_mutex_unlock(&out->lock);
5088 }
5089
Alexy Joseph98988832017-01-13 14:56:59 -08005090 //suspend, resume handling block
Aniket Kumar Lata932f4872017-11-06 18:29:44 -08005091 //remove QOS only if vendor.audio.hal.dynamic.qos.config.supported is set to true
5092 // and vendor.audio.hal.output.suspend.supported is set to true
5093 if (out->hal_output_suspend_supported && out->dynamic_pm_qos_config_supported) {
Alexy Joseph98988832017-01-13 14:56:59 -08005094 //check suspend parameter only for low latency and if the property
5095 //is enabled
5096 if (str_parms_get_str(parms, "suspend_playback", value, sizeof(value)) >= 0) {
5097 ALOGI("%s: got suspend_playback %s", __func__, value);
5098 lock_output_stream(out);
5099 if (!strncmp(value, "false", 5)) {
5100 //suspend_playback=false is supposed to set QOS value back to 75%
5101 //the mixer control sent with value Enable will achieve that
5102 ret = audio_route_apply_and_update_path(adev->audio_route, out->pm_qos_mixer_path);
5103 } else if (!strncmp (value, "true", 4)) {
5104 //suspend_playback=true is supposed to remove QOS value
5105 //resetting the mixer control will set the default value
5106 //for the mixer control which is Disable and this removes the QOS vote
5107 ret = audio_route_reset_and_update_path(adev->audio_route, out->pm_qos_mixer_path);
5108 } else {
5109 ALOGE("%s: Wrong value sent for suspend_playback, expected true/false,"
5110 " got %s", __func__, value);
5111 ret = -1;
5112 }
5113
5114 if (ret != 0) {
5115 ALOGE("%s: %s mixer ctl failed with %d, ignore suspend/resume setparams",
5116 __func__, out->pm_qos_mixer_path, ret);
5117 }
5118
5119 pthread_mutex_unlock(&out->lock);
5120 }
5121 }
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07005122
Alexy Joseph98988832017-01-13 14:56:59 -08005123 //end suspend, resume handling block
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005124 str_parms_destroy(parms);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05305125error:
Eric Laurent994a6932013-07-17 11:51:42 -07005126 ALOGV("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005127 return ret;
5128}
5129
Paul McLeana50b7332018-12-17 08:24:21 -07005130static int in_set_microphone_direction(const struct audio_stream_in *stream,
5131 audio_microphone_direction_t dir) {
justinweng20fb6d82019-02-21 18:49:00 -07005132 struct stream_in *in = (struct stream_in *)stream;
5133
5134 ALOGVV("%s: standby %d source %d dir %d", __func__, in->standby, in->source, dir);
5135
5136 in->direction = dir;
5137
5138 if (in->standby)
5139 return 0;
5140
5141 return audio_extn_audiozoom_set_microphone_direction(in, dir);
Paul McLeana50b7332018-12-17 08:24:21 -07005142}
5143
5144static int in_set_microphone_field_dimension(const struct audio_stream_in *stream, float zoom) {
justinweng20fb6d82019-02-21 18:49:00 -07005145 struct stream_in *in = (struct stream_in *)stream;
5146
5147 ALOGVV("%s: standby %d source %d zoom %f", __func__, in->standby, in->source, zoom);
5148
5149 if (zoom > 1.0 || zoom < -1.0)
5150 return -EINVAL;
5151
5152 in->zoom = zoom;
5153
5154 if (in->standby)
5155 return 0;
5156
5157 return audio_extn_audiozoom_set_microphone_field_dimension(in, zoom);
Paul McLeana50b7332018-12-17 08:24:21 -07005158}
5159
5160
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005161static bool stream_get_parameter_channels(struct str_parms *query,
5162 struct str_parms *reply,
5163 audio_channel_mask_t *supported_channel_masks) {
5164 int ret = -1;
5165 char value[512];
5166 bool first = true;
5167 size_t i, j;
5168
5169 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) {
5170 ret = 0;
5171 value[0] = '\0';
5172 i = 0;
5173 while (supported_channel_masks[i] != 0) {
5174 for (j = 0; j < ARRAY_SIZE(channels_name_to_enum_table); j++) {
5175 if (channels_name_to_enum_table[j].value == supported_channel_masks[i]) {
5176 if (!first)
Satya Krishna Pindiprolib22ac722017-10-09 15:44:16 +05305177 strlcat(value, "|", sizeof(value));
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005178
Satya Krishna Pindiprolib22ac722017-10-09 15:44:16 +05305179 strlcat(value, channels_name_to_enum_table[j].name, sizeof(value));
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005180 first = false;
5181 break;
5182 }
5183 }
5184 i++;
5185 }
5186 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
5187 }
5188 return ret == 0;
5189}
5190
5191static bool stream_get_parameter_formats(struct str_parms *query,
5192 struct str_parms *reply,
5193 audio_format_t *supported_formats) {
5194 int ret = -1;
5195 char value[256];
5196 size_t i, j;
5197 bool first = true;
5198
5199 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) {
5200 ret = 0;
5201 value[0] = '\0';
5202 i = 0;
5203 while (supported_formats[i] != 0) {
5204 for (j = 0; j < ARRAY_SIZE(formats_name_to_enum_table); j++) {
5205 if (formats_name_to_enum_table[j].value == supported_formats[i]) {
5206 if (!first) {
Satya Krishna Pindiprolib22ac722017-10-09 15:44:16 +05305207 strlcat(value, "|", sizeof(value));
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005208 }
Satya Krishna Pindiprolib22ac722017-10-09 15:44:16 +05305209 strlcat(value, formats_name_to_enum_table[j].name, sizeof(value));
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005210 first = false;
5211 break;
5212 }
5213 }
5214 i++;
5215 }
5216 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_FORMATS, value);
5217 }
5218 return ret == 0;
5219}
5220
5221static bool stream_get_parameter_rates(struct str_parms *query,
5222 struct str_parms *reply,
5223 uint32_t *supported_sample_rates) {
5224
5225 int i;
5226 char value[256];
5227 int ret = -1;
5228 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) {
5229 ret = 0;
5230 value[0] = '\0';
5231 i=0;
5232 int cursor = 0;
5233 while (supported_sample_rates[i]) {
5234 int avail = sizeof(value) - cursor;
5235 ret = snprintf(value + cursor, avail, "%s%d",
5236 cursor > 0 ? "|" : "",
5237 supported_sample_rates[i]);
5238 if (ret < 0 || ret >= avail) {
5239 // if cursor is at the last element of the array
5240 // overwrite with \0 is duplicate work as
5241 // snprintf already put a \0 in place.
5242 // else
5243 // we had space to write the '|' at value[cursor]
5244 // (which will be overwritten) or no space to fill
5245 // the first element (=> cursor == 0)
5246 value[cursor] = '\0';
5247 break;
5248 }
5249 cursor += ret;
5250 ++i;
5251 }
5252 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES,
5253 value);
5254 }
5255 return ret >= 0;
5256}
5257
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005258static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
5259{
5260 struct stream_out *out = (struct stream_out *)stream;
5261 struct str_parms *query = str_parms_create_str(keys);
Alexy Josephaee4fdd2016-01-29 13:02:07 -08005262 char *str = (char*) NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005263 char value[256];
5264 struct str_parms *reply = str_parms_create();
5265 size_t i, j;
5266 int ret;
5267 bool first = true;
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07005268
5269 if (!query || !reply) {
Alexy Josephaee4fdd2016-01-29 13:02:07 -08005270 if (reply) {
5271 str_parms_destroy(reply);
5272 }
5273 if (query) {
5274 str_parms_destroy(query);
5275 }
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07005276 ALOGE("out_get_parameters: failed to allocate mem for query or reply");
5277 return NULL;
5278 }
5279
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005280 ALOGV("%s: %s enter: keys - %s", __func__, use_case_table[out->usecase], keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005281 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
5282 if (ret >= 0) {
5283 value[0] = '\0';
5284 i = 0;
5285 while (out->supported_channel_masks[i] != 0) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005286 for (j = 0; j < ARRAY_SIZE(channels_name_to_enum_table); j++) {
5287 if (channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005288 if (!first) {
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -08005289 strlcat(value, "|", sizeof(value));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005290 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005291 strlcat(value, channels_name_to_enum_table[j].name, sizeof(value));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005292 first = false;
5293 break;
5294 }
5295 }
5296 i++;
5297 }
5298 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
5299 str = str_parms_to_str(reply);
5300 } else {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08005301 voice_extn_out_get_parameters(out, query, reply);
5302 str = str_parms_to_str(reply);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005303 }
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07005304
Alexy Joseph62142aa2015-11-16 15:10:34 -08005305
5306 ret = str_parms_get_str(query, "is_direct_pcm_track", value, sizeof(value));
5307 if (ret >= 0) {
5308 value[0] = '\0';
Dhananjay Kumarac341582017-02-23 23:42:25 +05305309 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
5310 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Alexy Joseph62142aa2015-11-16 15:10:34 -08005311 ALOGV("in direct_pcm");
Satya Krishna Pindiprolib6655542017-07-03 19:38:19 +05305312 strlcat(value, "true", sizeof(value));
Alexy Joseph62142aa2015-11-16 15:10:34 -08005313 } else {
5314 ALOGV("not in direct_pcm");
Sharad Sangle3dd5a4a2015-12-10 18:39:17 +05305315 strlcat(value, "false", sizeof(value));
Alexy Joseph62142aa2015-11-16 15:10:34 -08005316 }
5317 str_parms_add_str(reply, "is_direct_pcm_track", value);
Alexy Josephaee4fdd2016-01-29 13:02:07 -08005318 if (str)
5319 free(str);
Alexy Joseph62142aa2015-11-16 15:10:34 -08005320 str = str_parms_to_str(reply);
5321 }
5322
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07005323 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS, value, sizeof(value));
5324 if (ret >= 0) {
5325 value[0] = '\0';
5326 i = 0;
5327 first = true;
5328 while (out->supported_formats[i] != 0) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005329 for (j = 0; j < ARRAY_SIZE(formats_name_to_enum_table); j++) {
5330 if (formats_name_to_enum_table[j].value == out->supported_formats[i]) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07005331 if (!first) {
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -08005332 strlcat(value, "|", sizeof(value));
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07005333 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005334 strlcat(value, formats_name_to_enum_table[j].name, sizeof(value));
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07005335 first = false;
5336 break;
5337 }
5338 }
5339 i++;
5340 }
5341 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_FORMATS, value);
Alexy Josephaee4fdd2016-01-29 13:02:07 -08005342 if (str)
5343 free(str);
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07005344 str = str_parms_to_str(reply);
5345 }
Mingming Yin3a941d42016-02-17 18:08:05 -08005346
5347 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES, value, sizeof(value));
5348 if (ret >= 0) {
5349 value[0] = '\0';
5350 i = 0;
5351 first = true;
5352 while (out->supported_sample_rates[i] != 0) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005353 for (j = 0; j < ARRAY_SIZE(out_sample_rates_name_to_enum_table); j++) {
5354 if (out_sample_rates_name_to_enum_table[j].value == out->supported_sample_rates[i]) {
Mingming Yin3a941d42016-02-17 18:08:05 -08005355 if (!first) {
5356 strlcat(value, "|", sizeof(value));
5357 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005358 strlcat(value, out_sample_rates_name_to_enum_table[j].name, sizeof(value));
Mingming Yin3a941d42016-02-17 18:08:05 -08005359 first = false;
5360 break;
5361 }
5362 }
5363 i++;
5364 }
5365 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES, value);
5366 if (str)
5367 free(str);
5368 str = str_parms_to_str(reply);
5369 }
5370
Alexy Joseph98988832017-01-13 14:56:59 -08005371 if (str_parms_get_str(query, "supports_hw_suspend", value, sizeof(value)) >= 0) {
5372 //only low latency track supports suspend_resume
5373 str_parms_add_int(reply, "supports_hw_suspend",
Aniket Kumar Lata932f4872017-11-06 18:29:44 -08005374 (out->hal_output_suspend_supported));
Alexy Joseph98988832017-01-13 14:56:59 -08005375 if (str)
5376 free(str);
5377 str = str_parms_to_str(reply);
5378 }
5379
5380
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005381 str_parms_destroy(query);
5382 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07005383 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005384 return str;
5385}
5386
5387static uint32_t out_get_latency(const struct audio_stream_out *stream)
5388{
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005389 uint32_t period_ms;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005390 struct stream_out *out = (struct stream_out *)stream;
Alexy Josephaa54c872014-12-03 02:46:47 -08005391 uint32_t latency = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005392
Alexy Josephaa54c872014-12-03 02:46:47 -08005393 if (is_offload_usecase(out->usecase)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05305394 lock_output_stream(out);
5395 latency = audio_extn_utils_compress_get_dsp_latency(out);
5396 pthread_mutex_unlock(&out->lock);
Haynes Mathew George16081042017-05-31 17:16:49 -07005397 } else if ((out->realtime) ||
5398 (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP)) {
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005399 // since the buffer won't be filled up faster than realtime,
5400 // return a smaller number
5401 if (out->config.rate)
5402 period_ms = (out->af_period_multiplier * out->config.period_size *
5403 1000) / (out->config.rate);
5404 else
5405 period_ms = 0;
George Gao9ba8a142020-07-23 14:30:03 -07005406 latency = period_ms + platform_render_latency(out) / 1000;
Alexy Josephaa54c872014-12-03 02:46:47 -08005407 } else {
5408 latency = (out->config.period_count * out->config.period_size * 1000) /
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005409 (out->config.rate);
Alexy Josephaa54c872014-12-03 02:46:47 -08005410 }
5411
Zhou Songd2537a02020-06-11 22:04:46 +08005412 if (!out->standby && is_a2dp_out_device_type(&out->device_list))
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08005413 latency += audio_extn_a2dp_get_encoder_latency();
5414
Anish Kumar50ebcbf2014-12-09 04:01:39 +05305415 ALOGV("%s: Latency %d", __func__, latency);
Alexy Josephaa54c872014-12-03 02:46:47 -08005416 return latency;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005417}
5418
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05305419static float AmpToDb(float amplification)
5420{
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05305421 float db = DSD_VOLUME_MIN_DB;
5422 if (amplification > 0) {
5423 db = 20 * log10(amplification);
5424 if(db < DSD_VOLUME_MIN_DB)
5425 return DSD_VOLUME_MIN_DB;
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05305426 }
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05305427 return db;
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05305428}
5429
Arun Mirpuri5d170872019-03-26 13:21:31 -07005430static int out_set_mmap_volume(struct audio_stream_out *stream, float left,
5431 float right)
5432{
5433 struct stream_out *out = (struct stream_out *)stream;
5434 long volume = 0;
5435 char mixer_ctl_name[128] = "";
5436 struct audio_device *adev = out->dev;
5437 struct mixer_ctl *ctl = NULL;
5438 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
5439 PCM_PLAYBACK);
5440
5441 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
5442 "Playback %d Volume", pcm_device_id);
5443 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
5444 if (!ctl) {
5445 ALOGE("%s: Could not get ctl for mixer cmd - %s",
5446 __func__, mixer_ctl_name);
5447 return -EINVAL;
5448 }
5449 if (left != right)
5450 ALOGW("%s: Left and right channel volume mismatch:%f,%f",
5451 __func__, left, right);
5452 volume = (long)(left * (MMAP_PLAYBACK_VOLUME_MAX*1.0));
5453 if (mixer_ctl_set_value(ctl, 0, volume) < 0){
5454 ALOGE("%s:ctl for mixer cmd - %s, volume %ld returned error",
5455 __func__, mixer_ctl_name, volume);
5456 return -EINVAL;
5457 }
5458 return 0;
5459}
5460
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05305461static int out_set_compr_volume(struct audio_stream_out *stream, float left,
5462 float right)
5463{
5464 struct stream_out *out = (struct stream_out *)stream;
Manish Dewangan338c50a2017-09-12 15:22:03 +05305465 long volume[2];
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05305466 char mixer_ctl_name[128];
5467 struct audio_device *adev = out->dev;
5468 struct mixer_ctl *ctl;
5469 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
5470 PCM_PLAYBACK);
5471
5472 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
5473 "Compress Playback %d Volume", pcm_device_id);
5474 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
5475 if (!ctl) {
5476 ALOGE("%s: Could not get ctl for mixer cmd - %s",
5477 __func__, mixer_ctl_name);
5478 return -EINVAL;
5479 }
5480 ALOGE("%s:ctl for mixer cmd - %s, left %f, right %f",
5481 __func__, mixer_ctl_name, left, right);
5482 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
5483 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
5484 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
5485
5486 return 0;
5487}
5488
Zhou Song2b8f28f2017-09-11 10:51:38 +08005489static int out_set_voip_volume(struct audio_stream_out *stream, float left,
5490 float right)
5491{
5492 struct stream_out *out = (struct stream_out *)stream;
5493 char mixer_ctl_name[] = "App Type Gain";
5494 struct audio_device *adev = out->dev;
5495 struct mixer_ctl *ctl;
Manish Dewangan338c50a2017-09-12 15:22:03 +05305496 long set_values[4];
Zhou Song2b8f28f2017-09-11 10:51:38 +08005497
Aniket Kumar Lata8426d1f2019-06-10 15:25:53 -07005498 if (!is_valid_volume(left, right)) {
5499 ALOGE("%s: Invalid stream volume for left=%f, right=%f",
5500 __func__, left, right);
5501 return -EINVAL;
5502 }
5503
Zhou Song2b8f28f2017-09-11 10:51:38 +08005504 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
5505 if (!ctl) {
5506 ALOGE("%s: Could not get ctl for mixer cmd - %s",
5507 __func__, mixer_ctl_name);
5508 return -EINVAL;
5509 }
5510
5511 set_values[0] = 0; //0: Rx Session 1:Tx Session
5512 set_values[1] = out->app_type_cfg.app_type;
Manish Dewangan338c50a2017-09-12 15:22:03 +05305513 set_values[2] = (long)(left * VOIP_PLAYBACK_VOLUME_MAX);
5514 set_values[3] = (long)(right * VOIP_PLAYBACK_VOLUME_MAX);
Zhou Song2b8f28f2017-09-11 10:51:38 +08005515
5516 mixer_ctl_set_array(ctl, set_values, ARRAY_SIZE(set_values));
5517 return 0;
5518}
5519
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05305520static int out_set_pcm_volume(struct audio_stream_out *stream, float left,
5521 float right)
5522{
5523 struct stream_out *out = (struct stream_out *)stream;
5524 /* Volume control for pcm playback */
5525 if (left != right) {
5526 return -EINVAL;
5527 } else {
5528 char mixer_ctl_name[128];
5529 struct audio_device *adev = out->dev;
5530 struct mixer_ctl *ctl;
5531 int pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
5532 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name), "Playback %d Volume", pcm_device_id);
5533 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
5534 if (!ctl) {
5535 ALOGE("%s : Could not get ctl for mixer cmd - %s", __func__, mixer_ctl_name);
5536 return -EINVAL;
5537 }
5538
5539 int volume = (int) (left * PCM_PLAYBACK_VOLUME_MAX);
5540 int ret = mixer_ctl_set_value(ctl, 0, volume);
5541 if (ret < 0) {
5542 ALOGE("%s: Could not set ctl, error:%d ", __func__, ret);
5543 return -EINVAL;
5544 }
5545
5546 ALOGV("%s : Pcm set volume value %d left %f", __func__, volume, left);
5547
5548 return 0;
5549 }
5550}
5551
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005552static int out_set_volume(struct audio_stream_out *stream, float left,
5553 float right)
5554{
Eric Laurenta9024de2013-04-04 09:19:12 -07005555 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005556 int volume[2];
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05305557 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005558
Arun Mirpuri5d170872019-03-26 13:21:31 -07005559 ALOGD("%s: called with left_vol=%f, right_vol=%f", __func__, left, right);
Eric Laurenta9024de2013-04-04 09:19:12 -07005560 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
5561 /* only take left channel into account: the API is for stereo anyway */
5562 out->muted = (left == 0.0f);
5563 return 0;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07005564 } else if (is_offload_usecase(out->usecase)) {
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05305565 if (audio_extn_passthru_is_passthrough_stream(out)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07005566 /*
5567 * Set mute or umute on HDMI passthrough stream.
5568 * Only take left channel into account.
5569 * Mute is 0 and unmute 1
5570 */
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05305571 audio_extn_passthru_set_volume(out, (left == 0.0f));
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05305572 } else if (out->format == AUDIO_FORMAT_DSD){
5573 char mixer_ctl_name[128] = "DSD Volume";
5574 struct audio_device *adev = out->dev;
5575 struct mixer_ctl *ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
5576
5577 if (!ctl) {
5578 ALOGE("%s: Could not get ctl for mixer cmd - %s",
5579 __func__, mixer_ctl_name);
5580 return -EINVAL;
5581 }
Manish Dewangan338c50a2017-09-12 15:22:03 +05305582 volume[0] = (long)(AmpToDb(left));
5583 volume[1] = (long)(AmpToDb(right));
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05305584 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
5585 return 0;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005586 } else if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_BUS) &&
Derek Chendf05eea2019-08-01 13:57:49 -07005587 (out->car_audio_stream == CAR_AUDIO_STREAM_MEDIA)) {
Philippe Gravel1c9ac352019-05-28 16:08:37 -07005588 ALOGD("%s: Overriding offload set volume for media bus stream", __func__);
5589 struct listnode *node = NULL;
5590 list_for_each(node, &adev->active_outputs_list) {
5591 streams_output_ctxt_t *out_ctxt = node_to_item(node,
5592 streams_output_ctxt_t,
5593 list);
5594 if (out_ctxt->output->usecase == USECASE_AUDIO_PLAYBACK_MEDIA) {
5595 out->volume_l = out_ctxt->output->volume_l;
5596 out->volume_r = out_ctxt->output->volume_r;
5597 }
5598 }
Weiyin Jiangd5974e62020-09-08 20:28:22 +08005599 pthread_mutex_lock(&out->latch_lock);
Zhou Song1f93fa52020-11-20 13:57:39 +08005600 if (!out->a2dp_muted) {
Philippe Gravel1c9ac352019-05-28 16:08:37 -07005601 ret = out_set_compr_volume(&out->stream, out->volume_l, out->volume_r);
5602 }
Weiyin Jiangd5974e62020-09-08 20:28:22 +08005603 pthread_mutex_unlock(&out->latch_lock);
Philippe Gravel1c9ac352019-05-28 16:08:37 -07005604 return ret;
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07005605 } else {
Weiyin Jiangd5974e62020-09-08 20:28:22 +08005606 pthread_mutex_lock(&out->latch_lock);
Zhou Song1f93fa52020-11-20 13:57:39 +08005607 ALOGV("%s: compress mute %d", __func__, out->a2dp_muted);
5608 if (!out->a2dp_muted)
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05305609 ret = out_set_compr_volume(stream, left, right);
5610 out->volume_l = left;
5611 out->volume_r = right;
Weiyin Jiangd5974e62020-09-08 20:28:22 +08005612 pthread_mutex_unlock(&out->latch_lock);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05305613 return ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005614 }
Vikram Panduranga93f080e2017-06-07 18:16:14 -07005615 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP) {
Aalique Grahame22e49102018-12-18 14:23:57 -08005616 out->app_type_cfg.gain[0] = (int)(left * VOIP_PLAYBACK_VOLUME_MAX);
5617 out->app_type_cfg.gain[1] = (int)(right * VOIP_PLAYBACK_VOLUME_MAX);
Zhou Song1f93fa52020-11-20 13:57:39 +08005618 pthread_mutex_lock(&out->latch_lock);
Aalique Grahame22e49102018-12-18 14:23:57 -08005619 if (!out->standby) {
5620 audio_extn_utils_send_app_type_gain(out->dev,
5621 out->app_type_cfg.app_type,
5622 &out->app_type_cfg.gain[0]);
Zhou Song1f93fa52020-11-20 13:57:39 +08005623 if (!out->a2dp_muted)
5624 ret = out_set_voip_volume(stream, left, right);
Aalique Grahame22e49102018-12-18 14:23:57 -08005625 }
Zhou Song2b8f28f2017-09-11 10:51:38 +08005626 out->volume_l = left;
5627 out->volume_r = right;
Zhou Song1f93fa52020-11-20 13:57:39 +08005628 pthread_mutex_unlock(&out->latch_lock);
Zhou Song2b8f28f2017-09-11 10:51:38 +08005629 return ret;
Arun Mirpuri5d170872019-03-26 13:21:31 -07005630 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
5631 ALOGV("%s: MMAP set volume called", __func__);
5632 if (!out->standby)
5633 ret = out_set_mmap_volume(stream, left, right);
5634 out->volume_l = left;
5635 out->volume_r = right;
5636 return ret;
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05305637 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_LOW_LATENCY ||
Ramu Gottipati36547092018-12-28 11:32:09 +05305638 out->usecase == USECASE_AUDIO_PLAYBACK_DEEP_BUFFER ||
5639 out->usecase == USECASE_AUDIO_PLAYBACK_ULL) {
Zhou Song1f93fa52020-11-20 13:57:39 +08005640 pthread_mutex_lock(&out->latch_lock);
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05305641 /* Volume control for pcm playback */
Zhou Song1f93fa52020-11-20 13:57:39 +08005642 if (!out->standby && !out->a2dp_muted)
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05305643 ret = out_set_pcm_volume(stream, left, right);
5644 else
5645 out->apply_volume = true;
5646
5647 out->volume_l = left;
5648 out->volume_r = right;
Zhou Song1f93fa52020-11-20 13:57:39 +08005649 pthread_mutex_unlock(&out->latch_lock);
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05305650 return ret;
Derek Chenf13dd492018-11-13 14:53:51 -08005651 } else if (audio_extn_auto_hal_is_bus_device_usecase(out->usecase)) {
5652 ALOGV("%s: bus device set volume called", __func__);
Zhou Song1f93fa52020-11-20 13:57:39 +08005653 pthread_mutex_lock(&out->latch_lock);
5654 if (!out->standby && !out->a2dp_muted)
Derek Chenf13dd492018-11-13 14:53:51 -08005655 ret = out_set_pcm_volume(stream, left, right);
5656 out->volume_l = left;
5657 out->volume_r = right;
Zhou Song1f93fa52020-11-20 13:57:39 +08005658 pthread_mutex_unlock(&out->latch_lock);
Derek Chenf13dd492018-11-13 14:53:51 -08005659 return ret;
Eric Laurenta9024de2013-04-04 09:19:12 -07005660 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005661
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005662 return -ENOSYS;
5663}
5664
Zhou Songc9672822017-08-16 16:01:39 +08005665static void update_frames_written(struct stream_out *out, size_t bytes)
5666{
5667 size_t bpf = 0;
5668
5669 if (is_offload_usecase(out->usecase) && !out->non_blocking &&
5670 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD))
5671 bpf = 1;
5672 else if (!is_offload_usecase(out->usecase))
5673 bpf = audio_bytes_per_sample(out->format) *
5674 audio_channel_count_from_out_mask(out->channel_mask);
Zhou Song48453a02018-01-10 17:50:59 +08005675
5676 pthread_mutex_lock(&out->position_query_lock);
5677 if (bpf != 0) {
Zhou Songc9672822017-08-16 16:01:39 +08005678 out->written += bytes / bpf;
Zhou Song48453a02018-01-10 17:50:59 +08005679 clock_gettime(CLOCK_MONOTONIC, &out->writeAt);
5680 }
5681 pthread_mutex_unlock(&out->position_query_lock);
Zhou Songc9672822017-08-16 16:01:39 +08005682}
5683
Vignesh Kulothungana6927272019-02-20 15:17:07 -08005684int split_and_write_audio_haptic_data(struct stream_out *out,
5685 const void *buffer, size_t bytes_to_write)
5686{
5687 struct audio_device *adev = out->dev;
5688
5689 int ret = 0;
5690 size_t channel_count = audio_channel_count_from_out_mask(out->channel_mask);
5691 size_t bytes_per_sample = audio_bytes_per_sample(out->format);
5692 size_t frame_size = channel_count * bytes_per_sample;
5693 size_t frame_count = bytes_to_write / frame_size;
5694
5695 bool force_haptic_path =
5696 property_get_bool("vendor.audio.test_haptic", false);
5697
5698 // extract Haptics data from Audio buffer
5699 bool alloc_haptic_buffer = false;
5700 int haptic_channel_count = adev->haptics_config.channels;
5701 size_t haptic_frame_size = bytes_per_sample * haptic_channel_count;
5702 size_t audio_frame_size = frame_size - haptic_frame_size;
5703 size_t total_haptic_buffer_size = frame_count * haptic_frame_size;
5704
5705 if (adev->haptic_buffer == NULL) {
5706 alloc_haptic_buffer = true;
5707 } else if (adev->haptic_buffer_size < total_haptic_buffer_size) {
5708 free(adev->haptic_buffer);
5709 adev->haptic_buffer_size = 0;
5710 alloc_haptic_buffer = true;
5711 }
5712
5713 if (alloc_haptic_buffer) {
5714 adev->haptic_buffer = (uint8_t *)calloc(1, total_haptic_buffer_size);
Mingshu Pang1513f972019-05-24 12:43:51 +08005715 if(adev->haptic_buffer == NULL) {
5716 ALOGE("%s: failed to allocate mem for dev->haptic_buffer", __func__);
5717 return -ENOMEM;
5718 }
Vignesh Kulothungana6927272019-02-20 15:17:07 -08005719 adev->haptic_buffer_size = total_haptic_buffer_size;
5720 }
5721
5722 size_t src_index = 0, aud_index = 0, hap_index = 0;
5723 uint8_t *audio_buffer = (uint8_t *)buffer;
5724 uint8_t *haptic_buffer = adev->haptic_buffer;
5725
5726 // This is required for testing only. This works for stereo data only.
5727 // One channel is fed to audio stream and other to haptic stream for testing.
5728 if (force_haptic_path)
5729 audio_frame_size = haptic_frame_size = bytes_per_sample;
5730
5731 for (size_t i = 0; i < frame_count; i++) {
5732 memcpy(audio_buffer + aud_index, audio_buffer + src_index,
5733 audio_frame_size);
5734 aud_index += audio_frame_size;
5735 src_index += audio_frame_size;
5736
5737 if (adev->haptic_pcm)
5738 memcpy(haptic_buffer + hap_index, audio_buffer + src_index,
5739 haptic_frame_size);
5740 hap_index += haptic_frame_size;
5741 src_index += haptic_frame_size;
5742
5743 // This is required for testing only.
5744 // Discard haptic channel data.
5745 if (force_haptic_path)
5746 src_index += haptic_frame_size;
5747 }
5748
5749 // write to audio pipeline
5750 ret = pcm_write(out->pcm, (void *)audio_buffer,
5751 frame_count * audio_frame_size);
5752
5753 // write to haptics pipeline
5754 if (adev->haptic_pcm)
5755 ret = pcm_write(adev->haptic_pcm, (void *)adev->haptic_buffer,
5756 frame_count * haptic_frame_size);
5757
5758 return ret;
5759}
5760
Aalique Grahame22e49102018-12-18 14:23:57 -08005761#ifdef NO_AUDIO_OUT
5762static ssize_t out_write_for_no_output(struct audio_stream_out *stream,
5763 const void *buffer __unused, size_t bytes)
5764{
5765 struct stream_out *out = (struct stream_out *)stream;
5766
5767 /* No Output device supported other than BT for playback.
5768 * Sleep for the amount of buffer duration
5769 */
5770 lock_output_stream(out);
5771 usleep(bytes * 1000000 / audio_stream_out_frame_size(
5772 (const struct audio_stream_out *)&out->stream) /
5773 out_get_sample_rate(&out->stream.common));
5774 pthread_mutex_unlock(&out->lock);
5775 return bytes;
5776}
5777#endif
5778
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005779static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
5780 size_t bytes)
5781{
5782 struct stream_out *out = (struct stream_out *)stream;
5783 struct audio_device *adev = out->dev;
Eric Laurent6e895242013-09-05 16:10:57 -07005784 ssize_t ret = 0;
Satish Babu Patakokila715b1422017-08-22 14:33:21 +05305785 int channels = 0;
Arun Mirpuri7da752a2018-09-11 18:01:15 -07005786 const size_t frame_size = audio_stream_out_frame_size(stream);
5787 const size_t frames = (frame_size != 0) ? bytes / frame_size : bytes;
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +05305788 struct audio_usecase *usecase = NULL;
Meng Wang4c32fb42020-01-16 17:57:11 +08005789 uint32_t compr_passthr = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005790
Haynes Mathew George380745d2017-10-04 15:27:45 -07005791 ATRACE_BEGIN("out_write");
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005792 lock_output_stream(out);
Naresh Tanniru4c630392014-05-12 01:05:52 +05305793
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305794 if (CARD_STATUS_OFFLINE == out->card_status) {
Zhou Song0b2e5dc2015-03-16 14:41:38 +08005795
Dhananjay Kumarac341582017-02-23 23:42:25 +05305796 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Ashish Jainbbce4322016-02-16 13:25:27 +05305797 /*during SSR for compress usecase we should return error to flinger*/
Naresh Tanniru80659832014-06-04 18:17:56 +05305798 ALOGD(" copl %s: sound card is not active/SSR state", __func__);
5799 pthread_mutex_unlock(&out->lock);
Haynes Mathew George380745d2017-10-04 15:27:45 -07005800 ATRACE_END();
Naresh Tanniru80659832014-06-04 18:17:56 +05305801 return -ENETRESET;
Ashish Jainbbce4322016-02-16 13:25:27 +05305802 } else {
Ashish Jainbbce4322016-02-16 13:25:27 +05305803 ALOGD(" %s: sound card is not active/SSR state", __func__);
5804 ret= -EIO;
5805 goto exit;
Naresh Tanniru4c630392014-05-12 01:05:52 +05305806 }
5807 }
5808
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05305809 if (audio_extn_passthru_should_drop_data(out)) {
Ashish Jaind84fd6a2016-07-27 12:33:25 +05305810 ALOGV(" %s : Drop data as compress passthrough session is going on", __func__);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05305811 ret = -EIO;
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05305812 goto exit;
5813 }
5814
Haynes Mathew George16081042017-05-31 17:16:49 -07005815 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
5816 ret = -EINVAL;
5817 goto exit;
5818 }
5819
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005820 if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_AUX_DIGITAL) &&
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05305821 !out->is_iec61937_info_available) {
5822
5823 if (!audio_extn_passthru_is_passthrough_stream(out)) {
5824 out->is_iec61937_info_available = true;
5825 } else if (audio_extn_passthru_is_enabled()) {
5826 audio_extn_passthru_update_stream_configuration(adev, out, buffer, bytes);
Manish Dewangan37864bc2017-06-09 12:28:37 +05305827 out->is_iec61937_info_available = true;
Manish Dewangan671a4202017-08-18 17:30:46 +05305828
5829 if((out->format == AUDIO_FORMAT_DTS) ||
5830 (out->format == AUDIO_FORMAT_DTS_HD)) {
5831 ret = audio_extn_passthru_update_dts_stream_configuration(out,
5832 buffer, bytes);
5833 if (ret) {
5834 if (ret != -ENOSYS) {
5835 out->is_iec61937_info_available = false;
5836 ALOGD("iec61937 transmission info not yet updated retry");
5837 }
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05305838 } else if (!out->standby) {
Manish Dewangan671a4202017-08-18 17:30:46 +05305839 /* if stream has started and after that there is
5840 * stream config change (iec transmission config)
5841 * then trigger select_device to update backend configuration.
5842 */
5843 out->stream_config_changed = true;
5844 pthread_mutex_lock(&adev->lock);
5845 select_devices(adev, out->usecase);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05305846 if (!audio_extn_passthru_is_supported_backend_edid_cfg(adev, out)) {
Weiyin Jiang29c08a42019-04-30 17:11:10 +08005847 pthread_mutex_unlock(&adev->lock);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05305848 ret = -EINVAL;
5849 goto exit;
5850 }
Manish Dewangan671a4202017-08-18 17:30:46 +05305851 pthread_mutex_unlock(&adev->lock);
5852 out->stream_config_changed = false;
5853 out->is_iec61937_info_available = true;
5854 }
5855 }
Satish Babu Patakokila715b1422017-08-22 14:33:21 +05305856
Meng Wang4c32fb42020-01-16 17:57:11 +08005857#ifdef AUDIO_GKI_ENABLED
5858 /* out->compr_config.codec->reserved[0] is for compr_passthr */
5859 compr_passthr = out->compr_config.codec->reserved[0];
5860#else
5861 compr_passthr = out->compr_config.codec->compr_passthr;
5862#endif
5863
Garmond Leung317cbf12017-09-13 16:20:50 -07005864 if ((channels < (int)audio_channel_count_from_out_mask(out->channel_mask)) &&
Meng Wang4c32fb42020-01-16 17:57:11 +08005865 (compr_passthr == PASSTHROUGH) &&
Satish Babu Patakokila715b1422017-08-22 14:33:21 +05305866 (out->is_iec61937_info_available == true)) {
5867 ALOGE("%s: ERROR: Unsupported channel config in passthrough mode", __func__);
5868 ret = -EINVAL;
5869 goto exit;
5870 }
Manish Dewangan37864bc2017-06-09 12:28:37 +05305871 }
5872 }
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05305873
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005874 if (is_a2dp_out_device_type(&out->device_list) &&
Florian Pfister1a84f312018-07-19 14:38:18 +02005875 (audio_extn_a2dp_source_is_suspended())) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005876 if (!(compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_SPEAKER) ||
5877 compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_SPEAKER_SAFE))) {
Zhou Song9ebf6792020-09-23 22:49:01 +08005878 if (!is_offload_usecase(out->usecase)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05305879 ret = -EIO;
5880 goto exit;
5881 }
5882 }
5883 }
5884
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005885 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07005886 out->standby = false;
Andy Hungc6bfd4a2019-07-01 18:26:00 -07005887 const int64_t startNs = systemTime(SYSTEM_TIME_MONOTONIC);
5888
Eric Laurent150dbfe2013-02-27 14:31:02 -08005889 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08005890 if (out->usecase == USECASE_COMPRESS_VOIP_CALL)
5891 ret = voice_extn_compress_voip_start_output_stream(out);
5892 else
5893 ret = start_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005894 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005895 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07005896 out->standby = true;
Weiyin Jiangac2bae82020-07-29 17:23:53 +08005897 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005898 goto exit;
5899 }
Ashish Jain1b9b30c2017-05-18 20:57:40 +05305900 out->started = 1;
Andy Hunga1f48fa2019-07-01 18:14:53 -07005901 out->last_fifo_valid = false; // we're coming out of standby, last_fifo isn't valid.
Weiyin Jiangac2bae82020-07-29 17:23:53 +08005902
5903 if ((last_known_cal_step != -1) && (adev->platform != NULL)) {
vivek mehtab72d08d2016-04-29 03:16:47 -07005904 ALOGD("%s: retry previous failed cal level set", __func__);
Weiyin Jiangac2bae82020-07-29 17:23:53 +08005905 platform_send_gain_dep_cal(adev->platform, last_known_cal_step);
Preetam Singh Ranawatf4ae0222017-05-31 17:07:28 +05305906 last_known_cal_step = -1;
vivek mehtab72d08d2016-04-29 03:16:47 -07005907 }
Weiyin Jiangac2bae82020-07-29 17:23:53 +08005908 pthread_mutex_unlock(&adev->lock);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05305909
5910 if ((out->is_iec61937_info_available == true) &&
5911 (audio_extn_passthru_is_passthrough_stream(out))&&
5912 (!audio_extn_passthru_is_supported_backend_edid_cfg(adev, out))) {
5913 ret = -EINVAL;
5914 goto exit;
5915 }
Surendar Karkaf51b5842018-04-26 11:28:38 +05305916 if (out->set_dual_mono)
5917 audio_extn_send_dual_mono_mixing_coefficients(out);
Andy Hungc6bfd4a2019-07-01 18:26:00 -07005918
5919 // log startup time in ms.
5920 simple_stats_log(
5921 &out->start_latency_ms, (systemTime(SYSTEM_TIME_MONOTONIC) - startNs) * 1e-6);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005922 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005923
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005924 if (adev->is_channel_status_set == false &&
5925 compare_device_type(&out->device_list,
5926 AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
Alexy Josephb1379942016-01-29 15:49:38 -08005927 audio_utils_set_hdmi_channel_status(out, (void *)buffer, bytes);
Ashish Jain81eb2a82015-05-13 10:52:34 +05305928 adev->is_channel_status_set = true;
5929 }
5930
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +05305931 if ((adev->use_old_pspd_mix_ctrl == true) &&
5932 (out->pspd_coeff_sent == false)) {
5933 /*
5934 * Need to resend pspd coefficients after stream started for
5935 * older kernel version as it does not save the coefficients
5936 * and also stream has to be started for coeff to apply.
5937 */
5938 usecase = get_usecase_from_list(adev, out->usecase);
5939 if (usecase != NULL) {
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05305940 audio_extn_set_custom_mtmx_params_v2(adev, usecase, true);
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +05305941 out->pspd_coeff_sent = true;
5942 }
5943 }
5944
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07005945 if (is_offload_usecase(out->usecase)) {
Alexy Joseph01e54e62015-03-03 19:01:03 -08005946 ALOGVV("copl(%p): writing buffer (%zu bytes) to compress device", out, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005947 if (out->send_new_metadata) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07005948 ALOGD("copl(%p):send new gapless metadata", out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005949 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
5950 out->send_new_metadata = 0;
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05305951 if (out->send_next_track_params && out->is_compr_metadata_avail) {
5952 ALOGD("copl(%p):send next track params in gapless", out);
Weiyin Jiangd9b5a4e2019-07-18 17:24:21 +08005953 compress_set_next_track_param(out->compr, &(out->compr_config.codec->options));
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05305954 out->send_next_track_params = false;
5955 out->is_compr_metadata_avail = false;
5956 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005957 }
Dhananjay Kumarac341582017-02-23 23:42:25 +05305958 if (!(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
Ashish Jain83a6cc22016-06-28 14:34:17 +05305959 (out->convert_buffer) != NULL) {
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005960
Ashish Jain83a6cc22016-06-28 14:34:17 +05305961 if ((bytes > out->hal_fragment_size)) {
Ashish Jainf1eaa582016-05-23 20:54:24 +05305962 ALOGW("Error written bytes %zu > %d (fragment_size)",
Ashish Jain83a6cc22016-06-28 14:34:17 +05305963 bytes, out->hal_fragment_size);
Ashish Jainf1eaa582016-05-23 20:54:24 +05305964 pthread_mutex_unlock(&out->lock);
Haynes Mathew George380745d2017-10-04 15:27:45 -07005965 ATRACE_END();
Ashish Jainf1eaa582016-05-23 20:54:24 +05305966 return -EINVAL;
5967 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05305968 audio_format_t dst_format = out->hal_op_format;
5969 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05305970
Dieter Luecking5d57def2018-09-07 14:23:37 +02005971 /* prevent division-by-zero */
5972 uint32_t bitwidth_src = format_to_bitwidth_table[src_format];
5973 uint32_t bitwidth_dst = format_to_bitwidth_table[dst_format];
5974 if ((bitwidth_src == 0) || (bitwidth_dst == 0)) {
5975 ALOGE("%s: Error bitwidth == 0", __func__);
Ramu Gottipati02809682018-12-19 16:46:12 +05305976 pthread_mutex_unlock(&out->lock);
Dieter Luecking5d57def2018-09-07 14:23:37 +02005977 ATRACE_END();
5978 return -EINVAL;
5979 }
5980
Ashish Jainf1eaa582016-05-23 20:54:24 +05305981 uint32_t frames = bytes / format_to_bitwidth_table[src_format];
5982 uint32_t bytes_to_write = frames * format_to_bitwidth_table[dst_format];
5983
Ashish Jain83a6cc22016-06-28 14:34:17 +05305984 memcpy_by_audio_format(out->convert_buffer,
Ashish Jainf1eaa582016-05-23 20:54:24 +05305985 dst_format,
5986 buffer,
5987 src_format,
5988 frames);
5989
Ashish Jain83a6cc22016-06-28 14:34:17 +05305990 ret = compress_write(out->compr, out->convert_buffer,
Ashish Jainf1eaa582016-05-23 20:54:24 +05305991 bytes_to_write);
5992
5993 /*Convert written bytes in audio flinger format*/
5994 if (ret > 0)
5995 ret = ((ret * format_to_bitwidth_table[out->format]) /
5996 format_to_bitwidth_table[dst_format]);
5997 }
5998 } else
5999 ret = compress_write(out->compr, buffer, bytes);
6000
Zhou Songc9672822017-08-16 16:01:39 +08006001 if ((ret < 0 || ret == (ssize_t)bytes) && !out->non_blocking)
6002 update_frames_written(out, bytes);
6003
Dhanalakshmi Siddani37ca1d62014-08-20 12:28:34 +05306004 if (ret < 0)
6005 ret = -errno;
Weiyin Jiangcc60dbb2018-08-21 13:12:03 +08006006 ALOGVV("%s: writing buffer (%zu bytes) to compress device returned %d", __func__, bytes, (int)ret);
Ashish Jainb26edfb2016-08-25 00:10:11 +05306007 /*msg to cb thread only if non blocking write is enabled*/
6008 if (ret >= 0 && ret < (ssize_t)bytes && out->non_blocking) {
Sidipotu Ashok55820562014-02-10 16:16:38 +05306009 ALOGD("No space available in compress driver, post msg to cb thread");
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006010 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
Naresh Tanniru80659832014-06-04 18:17:56 +05306011 } else if (-ENETRESET == ret) {
6012 ALOGE("copl %s: received sound card offline state on compress write", __func__);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306013 out->card_status = CARD_STATUS_OFFLINE;
Naresh Tanniru80659832014-06-04 18:17:56 +05306014 pthread_mutex_unlock(&out->lock);
Dhananjay Kumar1248dd82017-07-28 21:22:16 +05306015 out_on_error(&out->stream.common);
Haynes Mathew George380745d2017-10-04 15:27:45 -07006016 ATRACE_END();
Naresh Tanniru80659832014-06-04 18:17:56 +05306017 return ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006018 }
Ashish Jain5106d362016-05-11 19:23:33 +05306019
Dhanalakshmi Siddania6b76c72016-09-09 18:10:31 +05306020 /* Call compr start only when non-zero bytes of data is there to be rendered */
6021 if (!out->playback_started && ret > 0) {
6022 int status = compress_start(out->compr);
6023 if (status < 0) {
6024 ret = status;
6025 ALOGE("%s: compr start failed with err %d", __func__, errno);
6026 goto exit;
6027 }
Alexy Joseph7de344d2015-03-30 10:40:03 -07006028 audio_extn_dts_eagle_fade(adev, true, out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006029 out->playback_started = 1;
Weiyin Jiangd5974e62020-09-08 20:28:22 +08006030 pthread_mutex_lock(&out->latch_lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006031 out->offload_state = OFFLOAD_STATE_PLAYING;
Weiyin Jiangd5974e62020-09-08 20:28:22 +08006032 pthread_mutex_unlock(&out->latch_lock);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08006033
6034 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
6035 popcount(out->channel_mask),
6036 out->playback_started);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006037 }
6038 pthread_mutex_unlock(&out->lock);
Haynes Mathew George380745d2017-10-04 15:27:45 -07006039 ATRACE_END();
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006040 return ret;
6041 } else {
6042 if (out->pcm) {
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006043 size_t bytes_to_write = bytes;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006044 if (out->muted)
6045 memset((void *)buffer, 0, bytes);
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006046 ALOGV("%s: frames=%zu, frame_size=%zu, bytes_to_write=%zu",
6047 __func__, frames, frame_size, bytes_to_write);
6048
Aalique Grahame22e49102018-12-18 14:23:57 -08006049 if (out->usecase == USECASE_INCALL_MUSIC_UPLINK ||
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07006050 out->usecase == USECASE_INCALL_MUSIC_UPLINK2 ||
6051 (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP &&
6052 !audio_extn_utils_is_vendor_enhanced_fwk())) {
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006053 size_t channel_count = audio_channel_count_from_out_mask(out->channel_mask);
6054 int16_t *src = (int16_t *)buffer;
6055 int16_t *dst = (int16_t *)buffer;
6056
Yunfei Zhanga6cd66d2019-07-22 16:15:36 +08006057 LOG_ALWAYS_FATAL_IF(channel_count > 2 ||
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006058 out->format != AUDIO_FORMAT_PCM_16_BIT,
Aalique Grahame22e49102018-12-18 14:23:57 -08006059 "out_write called for %s use case with wrong properties",
6060 use_case_table[out->usecase]);
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006061
6062 /*
6063 * FIXME: this can be removed once audio flinger mixer supports
6064 * mono output
6065 */
6066
6067 /*
6068 * Code below goes over each frame in the buffer and adds both
6069 * L and R samples and then divides by 2 to convert to mono
6070 */
Yunfei Zhanga6cd66d2019-07-22 16:15:36 +08006071 if (channel_count == 2) {
6072 for (size_t i = 0; i < frames ; i++, dst++, src += 2) {
6073 *dst = (int16_t)(((int32_t)src[0] + (int32_t)src[1]) >> 1);
6074 }
6075 bytes_to_write /= 2;
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006076 }
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006077 }
Andy Hunga1f48fa2019-07-01 18:14:53 -07006078
6079 // Note: since out_get_presentation_position() is called alternating with out_write()
6080 // by AudioFlinger, we can check underruns using the prior timestamp read.
6081 // (Alternately we could check if the buffer is empty using pcm_get_htimestamp().
6082 if (out->last_fifo_valid) {
6083 // compute drain to see if there is an underrun.
6084 const int64_t current_ns = systemTime(SYSTEM_TIME_MONOTONIC); // sys call
Dhananjay Kumara4429632020-07-11 02:53:37 +05306085 int64_t time_diff_ns = current_ns - out->last_fifo_time_ns;
6086 int64_t frames_by_time =
6087 ((time_diff_ns > 0) && (time_diff_ns < (INT64_MAX / out->config.rate))) ?
6088 (time_diff_ns * out->config.rate / NANOS_PER_SECOND) : 0;
Andy Hunga1f48fa2019-07-01 18:14:53 -07006089 const int64_t underrun = frames_by_time - out->last_fifo_frames_remaining;
6090
6091 if (underrun > 0) {
6092 simple_stats_log(&out->fifo_underruns, underrun);
6093
6094 ALOGW("%s: underrun(%lld) "
6095 "frames_by_time(%lld) > out->last_fifo_frames_remaining(%lld)",
6096 __func__,
6097 (long long)out->fifo_underruns.n,
6098 (long long)frames_by_time,
6099 (long long)out->last_fifo_frames_remaining);
6100 }
6101 out->last_fifo_valid = false; // we're writing below, mark fifo info as stale.
6102 }
6103
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05306104 ALOGVV("%s: writing buffer (%zu bytes) to pcm device", __func__, bytes);
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07006105
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006106 long ns = 0;
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07006107
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006108 if (out->config.rate)
6109 ns = pcm_bytes_to_frames(out->pcm, bytes)*1000000000LL/
6110 out->config.rate;
6111
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006112 request_out_focus(out, ns);
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006113 bool use_mmap = is_mmap_usecase(out->usecase) || out->realtime;
6114
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006115 if (use_mmap)
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006116 ret = pcm_mmap_write(out->pcm, (void *)buffer, bytes_to_write);
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006117 else if (out->hal_op_format != out->hal_ip_format &&
Ashish Jain83a6cc22016-06-28 14:34:17 +05306118 out->convert_buffer != NULL) {
6119
6120 memcpy_by_audio_format(out->convert_buffer,
6121 out->hal_op_format,
6122 buffer,
6123 out->hal_ip_format,
6124 out->config.period_size * out->config.channels);
6125
6126 ret = pcm_write(out->pcm, out->convert_buffer,
6127 (out->config.period_size *
6128 out->config.channels *
6129 format_to_bitwidth_table[out->hal_op_format]));
6130 } else {
Aditya Bavanarid4db8ee2017-05-29 21:08:03 +05306131 /*
6132 * To avoid underrun in DSP when the application is not pumping
6133 * data at required rate, check for the no. of bytes and ignore
6134 * pcm_write if it is less than actual buffer size.
6135 * It is a work around to a change in compress VOIP driver.
6136 */
6137 if ((out->flags & AUDIO_OUTPUT_FLAG_VOIP_RX) &&
6138 bytes < (out->config.period_size * out->config.channels *
6139 audio_bytes_per_sample(out->format))) {
6140 size_t voip_buf_size =
6141 out->config.period_size * out->config.channels *
6142 audio_bytes_per_sample(out->format);
6143 ALOGE("%s:VOIP underrun: bytes received %zu, required:%zu\n",
6144 __func__, bytes, voip_buf_size);
6145 usleep(((uint64_t)voip_buf_size - bytes) *
6146 1000000 / audio_stream_out_frame_size(stream) /
6147 out_get_sample_rate(&out->stream.common));
6148 ret = 0;
Vignesh Kulothungana6927272019-02-20 15:17:07 -08006149 } else {
6150 if (out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS)
6151 ret = split_and_write_audio_haptic_data(out, buffer, bytes);
6152 else
6153 ret = pcm_write(out->pcm, (void *)buffer, bytes_to_write);
6154 }
Ashish Jain83a6cc22016-06-28 14:34:17 +05306155 }
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07006156
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006157 release_out_focus(out);
6158
Dhanalakshmi Siddani37ca1d62014-08-20 12:28:34 +05306159 if (ret < 0)
6160 ret = -errno;
Zhou Songc9672822017-08-16 16:01:39 +08006161 else if (ret > 0)
Ashish Jain83a6cc22016-06-28 14:34:17 +05306162 ret = -EINVAL;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006163 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006164 }
6165
6166exit:
Zhou Songc9672822017-08-16 16:01:39 +08006167 update_frames_written(out, bytes);
Naresh Tanniru4c630392014-05-12 01:05:52 +05306168 if (-ENETRESET == ret) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306169 out->card_status = CARD_STATUS_OFFLINE;
Naresh Tanniru4c630392014-05-12 01:05:52 +05306170 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006171 pthread_mutex_unlock(&out->lock);
6172
6173 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07006174 if (out->pcm)
Alexy Josephb1379942016-01-29 15:49:38 -08006175 ALOGE("%s: error %d, %s", __func__, (int)ret, pcm_get_error(out->pcm));
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05306176 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05306177 pthread_mutex_lock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05306178 voice_extn_compress_voip_close_output_stream(&out->stream.common);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05306179 out->started = 0;
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05306180 pthread_mutex_unlock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05306181 out->standby = true;
6182 }
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306183 out_on_error(&out->stream.common);
Dieter Luecking5d57def2018-09-07 14:23:37 +02006184 if (!(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
6185 /* prevent division-by-zero */
6186 uint32_t stream_size = audio_stream_out_frame_size(stream);
6187 uint32_t srate = out_get_sample_rate(&out->stream.common);
Vidyakumar Athotaa9d3a5f2017-08-09 12:13:05 -07006188
Dieter Luecking5d57def2018-09-07 14:23:37 +02006189 if ((stream_size == 0) || (srate == 0)) {
6190 ALOGE("%s: stream_size= %d, srate = %d", __func__, stream_size, srate);
6191 ATRACE_END();
6192 return -EINVAL;
6193 }
6194 usleep((uint64_t)bytes * 1000000 / stream_size / srate);
6195 }
Vidyakumar Athotaa9d3a5f2017-08-09 12:13:05 -07006196 if (audio_extn_passthru_is_passthrough_stream(out)) {
Rajshekar Eashwarappa88834522018-04-02 17:20:15 +05306197 //ALOGE("%s: write error, ret = %zd", __func__, ret);
Haynes Mathew George380745d2017-10-04 15:27:45 -07006198 ATRACE_END();
Vidyakumar Athotaa9d3a5f2017-08-09 12:13:05 -07006199 return ret;
6200 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006201 }
Haynes Mathew George380745d2017-10-04 15:27:45 -07006202 ATRACE_END();
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006203 return bytes;
6204}
6205
6206static int out_get_render_position(const struct audio_stream_out *stream,
6207 uint32_t *dsp_frames)
6208{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006209 struct stream_out *out = (struct stream_out *)stream;
Zhou Song32a556e2015-05-05 10:46:56 +08006210
6211 if (dsp_frames == NULL)
6212 return -EINVAL;
6213
6214 *dsp_frames = 0;
6215 if (is_offload_usecase(out->usecase)) {
Mingming Yin9e348b52014-11-19 16:18:55 -08006216 ssize_t ret = 0;
Ashish Jain5106d362016-05-11 19:23:33 +05306217
6218 /* Below piece of code is not guarded against any lock beacuse audioFliner serializes
6219 * this operation and adev_close_output_stream(where out gets reset).
6220 */
Dhananjay Kumarac341582017-02-23 23:42:25 +05306221 if (!out->non_blocking && !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Zhou Song48453a02018-01-10 17:50:59 +08006222 *dsp_frames = get_actual_pcm_frames_rendered(out, NULL);
Ashish Jain5106d362016-05-11 19:23:33 +05306223 ALOGVV("dsp_frames %d sampleRate %d",(int)*dsp_frames,out->sample_rate);
Haynes Mathew Georgeb0f5dc32017-10-06 18:35:12 -07006224 adjust_frames_for_device_delay(out, dsp_frames);
Ashish Jain5106d362016-05-11 19:23:33 +05306225 return 0;
6226 }
6227
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006228 lock_output_stream(out);
Ashish Jain5106d362016-05-11 19:23:33 +05306229 if (out->compr != NULL && out->non_blocking) {
Naresh Tanniru80659832014-06-04 18:17:56 +05306230 ret = compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006231 &out->sample_rate);
Dhanalakshmi Siddani37ca1d62014-08-20 12:28:34 +05306232 if (ret < 0)
6233 ret = -errno;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006234 ALOGVV("%s rendered frames %d sample_rate %d",
Ashish Jain5106d362016-05-11 19:23:33 +05306235 __func__, *dsp_frames, out->sample_rate);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006236 }
Naresh Tanniru80659832014-06-04 18:17:56 +05306237 if (-ENETRESET == ret) {
6238 ALOGE(" ERROR: sound card not active Unable to get time stamp from compress driver");
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306239 out->card_status = CARD_STATUS_OFFLINE;
6240 ret = -EINVAL;
Naresh Tanniru80659832014-06-04 18:17:56 +05306241 } else if(ret < 0) {
6242 ALOGE(" ERROR: Unable to get time stamp from compress driver");
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306243 ret = -EINVAL;
6244 } else if (out->card_status == CARD_STATUS_OFFLINE) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05306245 /*
6246 * Handle corner case where compress session is closed during SSR
6247 * and timestamp is queried
6248 */
6249 ALOGE(" ERROR: sound card not active, return error");
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306250 ret = -EINVAL;
Manisha Agarwal7b3e3772019-02-20 14:33:45 +05306251 } else if (out->prev_card_status_offline) {
6252 ALOGE("ERROR: previously sound card was offline,return error");
6253 ret = -EINVAL;
Naresh Tanniru80659832014-06-04 18:17:56 +05306254 } else {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306255 ret = 0;
Haynes Mathew Georgeb0f5dc32017-10-06 18:35:12 -07006256 adjust_frames_for_device_delay(out, dsp_frames);
Naresh Tanniru80659832014-06-04 18:17:56 +05306257 }
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306258 pthread_mutex_unlock(&out->lock);
6259 return ret;
Zhou Song32a556e2015-05-05 10:46:56 +08006260 } else if (audio_is_linear_pcm(out->format)) {
6261 *dsp_frames = out->written;
Haynes Mathew Georgeb0f5dc32017-10-06 18:35:12 -07006262 adjust_frames_for_device_delay(out, dsp_frames);
Zhou Song32a556e2015-05-05 10:46:56 +08006263 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006264 } else
6265 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006266}
6267
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07006268static int out_add_audio_effect(const struct audio_stream *stream __unused,
6269 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006270{
6271 return 0;
6272}
6273
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07006274static int out_remove_audio_effect(const struct audio_stream *stream __unused,
6275 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006276{
6277 return 0;
6278}
6279
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07006280static int out_get_next_write_timestamp(const struct audio_stream_out *stream __unused,
6281 int64_t *timestamp __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006282{
Satya Krishna Pindiprolib6655542017-07-03 19:38:19 +05306283 return -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006284}
6285
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07006286static int out_get_presentation_position(const struct audio_stream_out *stream,
6287 uint64_t *frames, struct timespec *timestamp)
6288{
6289 struct stream_out *out = (struct stream_out *)stream;
pavance65c2fe2017-10-18 17:52:01 +05306290 int ret = -ENODATA;
Eric Laurent949a0892013-09-20 09:20:13 -07006291 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07006292
Ashish Jain5106d362016-05-11 19:23:33 +05306293 /* below piece of code is not guarded against any lock because audioFliner serializes
6294 * this operation and adev_close_output_stream( where out gets reset).
6295 */
6296 if (is_offload_usecase(out->usecase) && !out->non_blocking &&
Dhananjay Kumarac341582017-02-23 23:42:25 +05306297 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Zhou Song48453a02018-01-10 17:50:59 +08006298 *frames = get_actual_pcm_frames_rendered(out, timestamp);
Ashish Jain5106d362016-05-11 19:23:33 +05306299 ALOGVV("frames %lld playedat %lld",(long long int)*frames,
6300 timestamp->tv_sec * 1000000LL + timestamp->tv_nsec / 1000);
6301 return 0;
6302 }
6303
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006304 lock_output_stream(out);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07006305
Ashish Jain5106d362016-05-11 19:23:33 +05306306 if (is_offload_usecase(out->usecase) && out->compr != NULL && out->non_blocking) {
6307 ret = compress_get_tstamp(out->compr, &dsp_frames,
6308 &out->sample_rate);
yidongh0515e042017-07-06 15:00:34 +08006309 // Adjustment accounts for A2dp encoder latency with offload usecases
6310 // Note: Encoder latency is returned in ms.
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08006311 if (is_a2dp_out_device_type(&out->device_list)) {
yidongh0515e042017-07-06 15:00:34 +08006312 unsigned long offset =
6313 (audio_extn_a2dp_get_encoder_latency() * out->sample_rate / 1000);
6314 dsp_frames = (dsp_frames > offset) ? (dsp_frames - offset) : 0;
6315 }
Ashish Jain5106d362016-05-11 19:23:33 +05306316 ALOGVV("%s rendered frames %ld sample_rate %d",
6317 __func__, dsp_frames, out->sample_rate);
6318 *frames = dsp_frames;
6319 if (ret < 0)
6320 ret = -errno;
6321 if (-ENETRESET == ret) {
6322 ALOGE(" ERROR: sound card not active Unable to get time stamp from compress driver");
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306323 out->card_status = CARD_STATUS_OFFLINE;
Ashish Jain5106d362016-05-11 19:23:33 +05306324 ret = -EINVAL;
6325 } else
6326 ret = 0;
6327 /* this is the best we can do */
6328 clock_gettime(CLOCK_MONOTONIC, timestamp);
Eric Laurent949a0892013-09-20 09:20:13 -07006329 } else {
6330 if (out->pcm) {
Weiyin Jiangd4633762018-03-16 12:05:03 +08006331 unsigned int avail;
6332 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
George Gao62ebc722019-07-29 16:29:44 -07006333 uint64_t signed_frames = 0;
Weiyin Jiang0d25bcf2019-08-30 15:27:23 +08006334 uint64_t frames_temp = 0;
George Gao62ebc722019-07-29 16:29:44 -07006335
Andy Hunga1f48fa2019-07-01 18:14:53 -07006336 if (out->kernel_buffer_size > avail) {
6337 frames_temp = out->last_fifo_frames_remaining = out->kernel_buffer_size - avail;
6338 } else {
6339 ALOGW("%s: avail:%u > kernel_buffer_size:%zu clamping!",
6340 __func__, avail, out->kernel_buffer_size);
6341 avail = out->kernel_buffer_size;
6342 frames_temp = out->last_fifo_frames_remaining = 0;
6343 }
6344 out->last_fifo_valid = true;
6345 out->last_fifo_time_ns = audio_utils_ns_from_timespec(timestamp);
6346
Weiyin Jiang0d25bcf2019-08-30 15:27:23 +08006347 if (out->written >= frames_temp)
6348 signed_frames = out->written - frames_temp;
George Gao62ebc722019-07-29 16:29:44 -07006349
Andy Hunga1f48fa2019-07-01 18:14:53 -07006350 ALOGVV("%s: frames:%lld avail:%u kernel_buffer_size:%zu",
6351 __func__, (long long)signed_frames, avail, out->kernel_buffer_size);
6352
Weiyin Jiangd4633762018-03-16 12:05:03 +08006353 // This adjustment accounts for buffering after app processor.
6354 // It is based on estimated DSP latency per use case, rather than exact.
George Gao9ba8a142020-07-23 14:30:03 -07006355 frames_temp = platform_render_latency(out) *
Robert Lee58215542019-07-15 20:55:12 +08006356 out->sample_rate / 1000000LL;
Weiyin Jiang0d25bcf2019-08-30 15:27:23 +08006357 if (signed_frames >= frames_temp)
6358 signed_frames -= frames_temp;
Aniket Kumar Lataff613152017-07-18 18:19:21 -07006359
Weiyin Jiangd4633762018-03-16 12:05:03 +08006360 // Adjustment accounts for A2dp encoder latency with non offload usecases
6361 // Note: Encoder latency is returned in ms, while platform_render_latency in us.
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08006362 if (is_a2dp_out_device_type(&out->device_list)) {
Weiyin Jiang0d25bcf2019-08-30 15:27:23 +08006363 frames_temp = audio_extn_a2dp_get_encoder_latency() * out->sample_rate / 1000;
6364 if (signed_frames >= frames_temp)
6365 signed_frames -= frames_temp;
Weiyin Jiangd4633762018-03-16 12:05:03 +08006366 }
6367
6368 // It would be unusual for this value to be negative, but check just in case ...
George Gao62ebc722019-07-29 16:29:44 -07006369 *frames = signed_frames;
6370 ret = 0;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07006371 }
Eric Laurenta7a33042019-07-10 16:20:22 -07006372 } else if (out->card_status == CARD_STATUS_OFFLINE ||
6373 // audioflinger still needs position updates when A2DP is suspended
Jasmine Cha5c2517f2019-09-09 11:07:28 +08006374 (is_a2dp_out_device_type(&out->device_list) && audio_extn_a2dp_source_is_suspended())) {
Ashish Jainbbce4322016-02-16 13:25:27 +05306375 *frames = out->written;
6376 clock_gettime(CLOCK_MONOTONIC, timestamp);
Manisha Agarwal7b3e3772019-02-20 14:33:45 +05306377 if (is_offload_usecase(out->usecase))
6378 ret = -EINVAL;
6379 else
6380 ret = 0;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07006381 }
6382 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07006383 pthread_mutex_unlock(&out->lock);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07006384 return ret;
6385}
6386
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006387static int out_set_callback(struct audio_stream_out *stream,
6388 stream_callback_t callback, void *cookie)
6389{
6390 struct stream_out *out = (struct stream_out *)stream;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08006391 int ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006392
6393 ALOGV("%s", __func__);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006394 lock_output_stream(out);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08006395 out->client_callback = callback;
6396 out->client_cookie = cookie;
6397 if (out->adsp_hdlr_stream_handle) {
6398 ret = audio_extn_adsp_hdlr_stream_set_callback(
6399 out->adsp_hdlr_stream_handle,
6400 callback,
6401 cookie);
6402 if (ret)
6403 ALOGW("%s:adsp hdlr callback registration failed %d",
6404 __func__, ret);
6405 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006406 pthread_mutex_unlock(&out->lock);
6407 return 0;
6408}
6409
6410static int out_pause(struct audio_stream_out* stream)
6411{
6412 struct stream_out *out = (struct stream_out *)stream;
6413 int status = -ENOSYS;
6414 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07006415 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07006416 ALOGD("copl(%p):pause compress driver", out);
Sujin Panicker7c7b6f92020-04-03 12:57:55 +05306417 status = -ENODATA;
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006418 lock_output_stream(out);
Weiyin Jiangd5974e62020-09-08 20:28:22 +08006419 pthread_mutex_lock(&out->latch_lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006420 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306421 if (out->card_status != CARD_STATUS_OFFLINE)
Naresh Tanniru80659832014-06-04 18:17:56 +05306422 status = compress_pause(out->compr);
6423
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006424 out->offload_state = OFFLOAD_STATE_PAUSED;
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08006425
Mingming Yin21854652016-04-13 11:54:02 -07006426 if (audio_extn_passthru_is_active()) {
6427 ALOGV("offload use case, pause passthru");
6428 audio_extn_passthru_on_pause(out);
6429 }
6430
Dhanalakshmi Siddani79415e72015-03-23 11:54:47 +05306431 audio_extn_dts_eagle_fade(adev, false, out);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08006432 audio_extn_dts_notify_playback_state(out->usecase, 0,
6433 out->sample_rate, popcount(out->channel_mask),
6434 0);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006435 }
Weiyin Jiangd5974e62020-09-08 20:28:22 +08006436 pthread_mutex_unlock(&out->latch_lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006437 pthread_mutex_unlock(&out->lock);
6438 }
6439 return status;
6440}
6441
6442static int out_resume(struct audio_stream_out* stream)
6443{
6444 struct stream_out *out = (struct stream_out *)stream;
6445 int status = -ENOSYS;
6446 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07006447 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07006448 ALOGD("copl(%p):resume compress driver", out);
Sujin Panicker7c7b6f92020-04-03 12:57:55 +05306449 status = -ENODATA;
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006450 lock_output_stream(out);
Weiyin Jiangd5974e62020-09-08 20:28:22 +08006451 pthread_mutex_lock(&out->latch_lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006452 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306453 if (out->card_status != CARD_STATUS_OFFLINE) {
Naresh Tanniru80659832014-06-04 18:17:56 +05306454 status = compress_resume(out->compr);
Mingming Yin21854652016-04-13 11:54:02 -07006455 }
6456 if (!status) {
6457 out->offload_state = OFFLOAD_STATE_PLAYING;
6458 }
Dhanalakshmi Siddani79415e72015-03-23 11:54:47 +05306459 audio_extn_dts_eagle_fade(adev, true, out);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08006460 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
6461 popcount(out->channel_mask), 1);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006462 }
Weiyin Jiangd5974e62020-09-08 20:28:22 +08006463 pthread_mutex_unlock(&out->latch_lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006464 pthread_mutex_unlock(&out->lock);
6465 }
6466 return status;
6467}
6468
6469static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
6470{
6471 struct stream_out *out = (struct stream_out *)stream;
6472 int status = -ENOSYS;
6473 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07006474 if (is_offload_usecase(out->usecase)) {
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006475 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006476 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
6477 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
6478 else
6479 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
6480 pthread_mutex_unlock(&out->lock);
6481 }
6482 return status;
6483}
6484
6485static int out_flush(struct audio_stream_out* stream)
6486{
6487 struct stream_out *out = (struct stream_out *)stream;
6488 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07006489 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07006490 ALOGD("copl(%p):calling compress flush", out);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006491 lock_output_stream(out);
Weiyin Jiangd5974e62020-09-08 20:28:22 +08006492 pthread_mutex_lock(&out->latch_lock);
Haynes Mathew Georgeafe54d82016-09-21 14:39:19 -07006493 if (out->offload_state == OFFLOAD_STATE_PAUSED) {
Gautam Manamfeeb1162020-12-24 14:08:04 +05306494 pthread_mutex_unlock(&out->latch_lock);
Haynes Mathew Georgeafe54d82016-09-21 14:39:19 -07006495 stop_compressed_output_l(out);
Haynes Mathew Georgeafe54d82016-09-21 14:39:19 -07006496 } else {
6497 ALOGW("%s called in invalid state %d", __func__, out->offload_state);
Gautam Manamfeeb1162020-12-24 14:08:04 +05306498 pthread_mutex_unlock(&out->latch_lock);
Haynes Mathew Georgeafe54d82016-09-21 14:39:19 -07006499 }
Weiyin Jiang547e4152017-09-14 17:24:18 +08006500 out->written = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006501 pthread_mutex_unlock(&out->lock);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07006502 ALOGD("copl(%p):out of compress flush", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006503 return 0;
6504 }
6505 return -ENOSYS;
6506}
6507
Haynes Mathew George16081042017-05-31 17:16:49 -07006508static int out_stop(const struct audio_stream_out* stream)
6509{
6510 struct stream_out *out = (struct stream_out *)stream;
6511 struct audio_device *adev = out->dev;
6512 int ret = -ENOSYS;
6513
6514 ALOGV("%s", __func__);
6515 pthread_mutex_lock(&adev->lock);
6516 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP && !out->standby &&
6517 out->playback_started && out->pcm != NULL) {
6518 pcm_stop(out->pcm);
6519 ret = stop_output_stream(out);
6520 out->playback_started = false;
6521 }
6522 pthread_mutex_unlock(&adev->lock);
6523 return ret;
6524}
6525
6526static int out_start(const struct audio_stream_out* stream)
6527{
6528 struct stream_out *out = (struct stream_out *)stream;
6529 struct audio_device *adev = out->dev;
6530 int ret = -ENOSYS;
6531
6532 ALOGV("%s", __func__);
6533 pthread_mutex_lock(&adev->lock);
6534 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP && !out->standby &&
6535 !out->playback_started && out->pcm != NULL) {
6536 ret = start_output_stream(out);
6537 if (ret == 0) {
6538 out->playback_started = true;
6539 }
6540 }
6541 pthread_mutex_unlock(&adev->lock);
6542 return ret;
6543}
6544
6545/*
6546 * Modify config->period_count based on min_size_frames
6547 */
6548static void adjust_mmap_period_count(struct pcm_config *config, int32_t min_size_frames)
6549{
6550 int periodCountRequested = (min_size_frames + config->period_size - 1)
6551 / config->period_size;
6552 int periodCount = MMAP_PERIOD_COUNT_MIN;
6553
6554 ALOGV("%s original config.period_size = %d config.period_count = %d",
6555 __func__, config->period_size, config->period_count);
6556
6557 while (periodCount < periodCountRequested && (periodCount * 2) < MMAP_PERIOD_COUNT_MAX) {
6558 periodCount *= 2;
6559 }
6560 config->period_count = periodCount;
6561
6562 ALOGV("%s requested config.period_count = %d", __func__, config->period_count);
6563}
6564
Phil Burkfe17efd2019-03-25 10:23:35 -07006565// Read offset for the positional timestamp from a persistent vendor property.
6566// This is to workaround apparent inaccuracies in the timing information that
6567// is used by the AAudio timing model. The inaccuracies can cause glitches.
6568static int64_t get_mmap_out_time_offset() {
6569 const int32_t kDefaultOffsetMicros = 0;
6570 int32_t mmap_time_offset_micros = property_get_int32(
Weiyin Jiangbd68b4d2019-05-17 16:29:50 +08006571 "persist.vendor.audio.out_mmap_delay_micros", kDefaultOffsetMicros);
Phil Burkfe17efd2019-03-25 10:23:35 -07006572 ALOGI("mmap_time_offset_micros = %d for output", mmap_time_offset_micros);
6573 return mmap_time_offset_micros * (int64_t)1000;
6574}
6575
Haynes Mathew George16081042017-05-31 17:16:49 -07006576static int out_create_mmap_buffer(const struct audio_stream_out *stream,
6577 int32_t min_size_frames,
6578 struct audio_mmap_buffer_info *info)
6579{
6580 struct stream_out *out = (struct stream_out *)stream;
6581 struct audio_device *adev = out->dev;
6582 int ret = 0;
Aalique Grahame1f123102017-10-12 10:38:32 -07006583 unsigned int offset1 = 0;
6584 unsigned int frames1 = 0;
Haynes Mathew George16081042017-05-31 17:16:49 -07006585 const char *step = "";
Haynes Mathew Georgeef514882017-05-01 17:46:23 -07006586 uint32_t mmap_size;
Arun Mirpuri5d170872019-03-26 13:21:31 -07006587 uint32_t buffer_size;
Haynes Mathew George16081042017-05-31 17:16:49 -07006588
Arun Mirpuri5d170872019-03-26 13:21:31 -07006589 ALOGD("%s", __func__);
Sharad Sanglec6f32552018-05-04 16:15:38 +05306590 lock_output_stream(out);
Haynes Mathew George16081042017-05-31 17:16:49 -07006591 pthread_mutex_lock(&adev->lock);
6592
Sharad Sanglec6f32552018-05-04 16:15:38 +05306593 if (CARD_STATUS_OFFLINE == out->card_status ||
6594 CARD_STATUS_OFFLINE == adev->card_status) {
6595 ALOGW("out->card_status or adev->card_status offline, try again");
6596 ret = -EIO;
6597 goto exit;
6598 }
Sujin Panicker7c7b6f92020-04-03 12:57:55 +05306599 if (info == NULL || !(min_size_frames > 0 && min_size_frames < INT32_MAX)) {
Haynes Mathew George16081042017-05-31 17:16:49 -07006600 ALOGE("%s: info = %p, min_size_frames = %d", __func__, info, min_size_frames);
6601 ret = -EINVAL;
6602 goto exit;
6603 }
6604 if (out->usecase != USECASE_AUDIO_PLAYBACK_MMAP || !out->standby) {
6605 ALOGE("%s: usecase = %d, standby = %d", __func__, out->usecase, out->standby);
6606 ret = -ENOSYS;
6607 goto exit;
6608 }
6609 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
6610 if (out->pcm_device_id < 0) {
6611 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
6612 __func__, out->pcm_device_id, out->usecase);
6613 ret = -EINVAL;
6614 goto exit;
6615 }
6616
6617 adjust_mmap_period_count(&out->config, min_size_frames);
6618
Arun Mirpuri5d170872019-03-26 13:21:31 -07006619 ALOGD("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
Haynes Mathew George16081042017-05-31 17:16:49 -07006620 __func__, adev->snd_card, out->pcm_device_id, out->config.channels);
6621 out->pcm = pcm_open(adev->snd_card, out->pcm_device_id,
6622 (PCM_OUT | PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC), &out->config);
Satish Babu Patakokila54ce83d2018-07-06 18:00:37 +05306623 if (errno == ENETRESET && !pcm_is_ready(out->pcm)) {
Sharad Sanglec6f32552018-05-04 16:15:38 +05306624 ALOGE("%s: pcm_open failed errno:%d\n", __func__, errno);
6625 out->card_status = CARD_STATUS_OFFLINE;
6626 adev->card_status = CARD_STATUS_OFFLINE;
6627 ret = -EIO;
6628 goto exit;
6629 }
6630
Haynes Mathew George16081042017-05-31 17:16:49 -07006631 if (out->pcm == NULL || !pcm_is_ready(out->pcm)) {
6632 step = "open";
6633 ret = -ENODEV;
6634 goto exit;
6635 }
6636 ret = pcm_mmap_begin(out->pcm, &info->shared_memory_address, &offset1, &frames1);
6637 if (ret < 0) {
6638 step = "begin";
6639 goto exit;
6640 }
juyuchen626833d2019-06-04 16:48:02 +08006641
6642 info->flags = 0;
Haynes Mathew George16081042017-05-31 17:16:49 -07006643 info->buffer_size_frames = pcm_get_buffer_size(out->pcm);
Arun Mirpuri5d170872019-03-26 13:21:31 -07006644 buffer_size = pcm_frames_to_bytes(out->pcm, info->buffer_size_frames);
Haynes Mathew George16081042017-05-31 17:16:49 -07006645 info->burst_size_frames = out->config.period_size;
Haynes Mathew Georgeef514882017-05-01 17:46:23 -07006646 ret = platform_get_mmap_data_fd(adev->platform,
6647 out->pcm_device_id, 0 /*playback*/,
6648 &info->shared_memory_fd,
6649 &mmap_size);
6650 if (ret < 0) {
Arun Mirpuri5d170872019-03-26 13:21:31 -07006651 // Fall back to non exclusive mode
6652 info->shared_memory_fd = pcm_get_poll_fd(out->pcm);
6653 } else {
Phil Burkd898ba62019-06-20 12:49:01 -07006654 out->mmap_shared_memory_fd = info->shared_memory_fd; // for closing later
6655 ALOGV("%s: opened mmap_shared_memory_fd = %d", __func__, out->mmap_shared_memory_fd);
6656
Arun Mirpuri5d170872019-03-26 13:21:31 -07006657 if (mmap_size < buffer_size) {
6658 step = "mmap";
6659 goto exit;
6660 }
juyuchen626833d2019-06-04 16:48:02 +08006661 info->flags |= AUDIO_MMAP_APPLICATION_SHAREABLE;
Haynes Mathew Georgeef514882017-05-01 17:46:23 -07006662 }
Haynes Mathew George16081042017-05-31 17:16:49 -07006663 memset(info->shared_memory_address, 0, pcm_frames_to_bytes(out->pcm,
Haynes Mathew Georgeef514882017-05-01 17:46:23 -07006664 info->buffer_size_frames));
Haynes Mathew George16081042017-05-31 17:16:49 -07006665
6666 ret = pcm_mmap_commit(out->pcm, 0, MMAP_PERIOD_SIZE);
6667 if (ret < 0) {
6668 step = "commit";
6669 goto exit;
6670 }
6671
Phil Burkfe17efd2019-03-25 10:23:35 -07006672 out->mmap_time_offset_nanos = get_mmap_out_time_offset();
6673
Haynes Mathew George16081042017-05-31 17:16:49 -07006674 out->standby = false;
6675 ret = 0;
6676
Arun Mirpuri5d170872019-03-26 13:21:31 -07006677 ALOGD("%s: got mmap buffer address %p info->buffer_size_frames %d",
Haynes Mathew George16081042017-05-31 17:16:49 -07006678 __func__, info->shared_memory_address, info->buffer_size_frames);
6679
6680exit:
6681 if (ret != 0) {
6682 if (out->pcm == NULL) {
6683 ALOGE("%s: %s - %d", __func__, step, ret);
6684 } else {
6685 ALOGE("%s: %s %s", __func__, step, pcm_get_error(out->pcm));
6686 pcm_close(out->pcm);
6687 out->pcm = NULL;
6688 }
6689 }
6690 pthread_mutex_unlock(&adev->lock);
Sharad Sanglec6f32552018-05-04 16:15:38 +05306691 pthread_mutex_unlock(&out->lock);
Haynes Mathew George16081042017-05-31 17:16:49 -07006692 return ret;
6693}
6694
6695static int out_get_mmap_position(const struct audio_stream_out *stream,
6696 struct audio_mmap_position *position)
6697{
6698 struct stream_out *out = (struct stream_out *)stream;
6699 ALOGVV("%s", __func__);
6700 if (position == NULL) {
6701 return -EINVAL;
6702 }
6703 if (out->usecase != USECASE_AUDIO_PLAYBACK_MMAP) {
Haynes Mathew George4ab3ba92017-12-11 14:49:43 -08006704 ALOGE("%s: called on %s", __func__, use_case_table[out->usecase]);
Haynes Mathew George16081042017-05-31 17:16:49 -07006705 return -ENOSYS;
6706 }
6707 if (out->pcm == NULL) {
6708 return -ENOSYS;
6709 }
6710
6711 struct timespec ts = { 0, 0 };
6712 int ret = pcm_mmap_get_hw_ptr(out->pcm, (unsigned int *)&position->position_frames, &ts);
6713 if (ret < 0) {
6714 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
6715 return ret;
6716 }
Phil Burkfe17efd2019-03-25 10:23:35 -07006717 position->time_nanoseconds = ts.tv_sec*1000000000LL + ts.tv_nsec
6718 + out->mmap_time_offset_nanos;
Haynes Mathew George16081042017-05-31 17:16:49 -07006719 return 0;
6720}
6721
6722
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006723/** audio_stream_in implementation **/
6724static uint32_t in_get_sample_rate(const struct audio_stream *stream)
6725{
6726 struct stream_in *in = (struct stream_in *)stream;
6727
6728 return in->config.rate;
6729}
6730
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07006731static int in_set_sample_rate(struct audio_stream *stream __unused,
6732 uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006733{
6734 return -ENOSYS;
6735}
6736
6737static size_t in_get_buffer_size(const struct audio_stream *stream)
6738{
6739 struct stream_in *in = (struct stream_in *)stream;
6740
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08006741 if(in->usecase == USECASE_COMPRESS_VOIP_CALL)
6742 return voice_extn_compress_voip_in_get_buffer_size(in);
Mingming Yine62d7842013-10-25 16:26:03 -07006743 else if(audio_extn_compr_cap_usecase_supported(in->usecase))
6744 return audio_extn_compr_cap_get_buffer_size(in->config.format);
Dhananjay Kumar7dbe3562019-08-30 05:49:33 +05306745 else if(audio_extn_cin_attached_usecase(in))
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05306746 return audio_extn_cin_get_buffer_size(in);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08006747
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006748 return in->config.period_size * in->af_period_multiplier *
6749 audio_stream_in_frame_size((const struct audio_stream_in *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006750}
6751
6752static uint32_t in_get_channels(const struct audio_stream *stream)
6753{
6754 struct stream_in *in = (struct stream_in *)stream;
6755
6756 return in->channel_mask;
6757}
6758
6759static audio_format_t in_get_format(const struct audio_stream *stream)
6760{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08006761 struct stream_in *in = (struct stream_in *)stream;
6762
6763 return in->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006764}
6765
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07006766static int in_set_format(struct audio_stream *stream __unused,
6767 audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006768{
6769 return -ENOSYS;
6770}
6771
6772static int in_standby(struct audio_stream *stream)
6773{
6774 struct stream_in *in = (struct stream_in *)stream;
6775 struct audio_device *adev = in->dev;
6776 int status = 0;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05306777 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
6778 stream, in->usecase, use_case_table[in->usecase]);
Haynes Mathew George16081042017-05-31 17:16:49 -07006779 bool do_stop = true;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05306780
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006781 lock_input_stream(in);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07006782 if (!in->standby && in->is_st_session) {
6783 ALOGD("%s: sound trigger pcm stop lab", __func__);
6784 audio_extn_sound_trigger_stop_lab(in);
George Gao3018ede2019-10-23 13:23:00 -07006785 if (adev->num_va_sessions > 0)
6786 adev->num_va_sessions--;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07006787 in->standby = 1;
6788 }
6789
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006790 if (!in->standby) {
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07006791 if (adev->adm_deregister_stream)
6792 adev->adm_deregister_stream(adev->adm_data, in->capture_handle);
6793
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08006794 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006795 in->standby = true;
Zhou Songa8895042016-07-05 17:54:22 +08006796 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
kunleizbecba2d2017-09-07 13:37:16 +08006797 do_stop = false;
Zhou Songa8895042016-07-05 17:54:22 +08006798 voice_extn_compress_voip_close_input_stream(stream);
6799 ALOGD("VOIP input entered standby");
Haynes Mathew George16081042017-05-31 17:16:49 -07006800 } else if (in->usecase == USECASE_AUDIO_RECORD_MMAP) {
6801 do_stop = in->capture_started;
6802 in->capture_started = false;
Phil Burkd898ba62019-06-20 12:49:01 -07006803 if (in->mmap_shared_memory_fd >= 0) {
6804 ALOGV("%s: closing mmap_shared_memory_fd = %d",
6805 __func__, in->mmap_shared_memory_fd);
6806 close(in->mmap_shared_memory_fd);
6807 in->mmap_shared_memory_fd = -1;
6808 }
Zhou Songa8895042016-07-05 17:54:22 +08006809 } else {
Dhananjay Kumar7dbe3562019-08-30 05:49:33 +05306810 if (audio_extn_cin_attached_usecase(in))
Manish Dewangan46e07982018-12-13 18:18:59 +05306811 audio_extn_cin_close_input_stream(in);
kunleizbecba2d2017-09-07 13:37:16 +08006812 }
6813
Arun Mirpuri5d170872019-03-26 13:21:31 -07006814 if (in->pcm) {
6815 ATRACE_BEGIN("pcm_in_close");
6816 pcm_close(in->pcm);
6817 ATRACE_END();
6818 in->pcm = NULL;
6819 }
6820
Carter Hsu2e429db2019-05-14 18:50:52 +08006821 if (do_stop)
Zhou Songa8895042016-07-05 17:54:22 +08006822 status = stop_input_stream(in);
Quinn Malef6050362019-01-30 15:55:40 -08006823
George Gao3018ede2019-10-23 13:23:00 -07006824 if (in->source == AUDIO_SOURCE_VOICE_RECOGNITION) {
6825 if (adev->num_va_sessions > 0)
6826 adev->num_va_sessions--;
6827 }
Quinn Malef6050362019-01-30 15:55:40 -08006828
Eric Laurent150dbfe2013-02-27 14:31:02 -08006829 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006830 }
6831 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07006832 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006833 return status;
6834}
6835
Aalique Grahame22e49102018-12-18 14:23:57 -08006836static int in_dump(const struct audio_stream *stream,
6837 int fd)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006838{
Aalique Grahame22e49102018-12-18 14:23:57 -08006839 struct stream_in *in = (struct stream_in *)stream;
6840
6841 // We try to get the lock for consistency,
6842 // but it isn't necessary for these variables.
6843 // If we're not in standby, we may be blocked on a read.
6844 const bool locked = (pthread_mutex_trylock(&in->lock) == 0);
6845 dprintf(fd, " Standby: %s\n", in->standby ? "yes" : "no");
6846 dprintf(fd, " Frames read: %lld\n", (long long)in->frames_read);
6847 dprintf(fd, " Frames muted: %lld\n", (long long)in->frames_muted);
6848
Andy Hungc6bfd4a2019-07-01 18:26:00 -07006849 char buffer[256]; // for statistics formatting
6850 if (in->start_latency_ms.n > 0) {
6851 simple_stats_to_string(&in->start_latency_ms, buffer, sizeof(buffer));
6852 dprintf(fd, " Start latency ms: %s\n", buffer);
6853 }
6854
Aalique Grahame22e49102018-12-18 14:23:57 -08006855 if (locked) {
6856 pthread_mutex_unlock(&in->lock);
6857 }
6858
6859 // dump error info
6860 (void)error_log_dump(
6861 in->error_log, fd, " " /* prefix */, 0 /* lines */, 0 /* limit_ns */);
6862
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006863 return 0;
6864}
6865
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306866static void in_snd_mon_cb(void * stream, struct str_parms * parms)
6867{
6868 if (!stream || !parms)
6869 return;
6870
6871 struct stream_in *in = (struct stream_in *)stream;
6872 struct audio_device *adev = in->dev;
6873
6874 card_status_t status;
6875 int card;
6876 if (parse_snd_card_status(parms, &card, &status) < 0)
6877 return;
6878
6879 pthread_mutex_lock(&adev->lock);
6880 bool valid_cb = (card == adev->snd_card);
6881 pthread_mutex_unlock(&adev->lock);
6882
6883 if (!valid_cb)
6884 return;
6885
6886 lock_input_stream(in);
6887 if (in->card_status != status)
6888 in->card_status = status;
6889 pthread_mutex_unlock(&in->lock);
6890
6891 ALOGW("in_snd_mon_cb for card %d usecase %s, status %s", card,
6892 use_case_table[in->usecase],
6893 status == CARD_STATUS_OFFLINE ? "offline" : "online");
6894
6895 // a better solution would be to report error back to AF and let
6896 // it put the stream to standby
6897 if (status == CARD_STATUS_OFFLINE)
6898 in_standby(&in->stream.common);
6899
6900 return;
6901}
6902
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07006903int route_input_stream(struct stream_in *in,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08006904 struct listnode *devices,
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07006905 audio_source_t source)
6906{
6907 struct audio_device *adev = in->dev;
6908 int ret = 0;
6909
6910 lock_input_stream(in);
6911 pthread_mutex_lock(&adev->lock);
6912
6913 /* no audio source uses val == 0 */
6914 if ((in->source != source) && (source != AUDIO_SOURCE_DEFAULT)) {
6915 in->source = source;
6916 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
6917 (in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
6918 (voice_extn_compress_voip_is_format_supported(in->format)) &&
6919 (in->config.rate == 8000 || in->config.rate == 16000 ||
6920 in->config.rate == 32000 || in->config.rate == 48000 ) &&
6921 (audio_channel_count_from_in_mask(in->channel_mask) == 1)) {
6922 ret = voice_extn_compress_voip_open_input_stream(in);
6923 if (ret != 0) {
6924 ALOGE("%s: Compress voip input cannot be opened, error:%d",
6925 __func__, ret);
6926 }
6927 }
6928 }
6929
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08006930 if (!compare_devices(&in->device_list, devices) && !list_empty(devices) &&
6931 is_audio_in_device_type(devices)) {
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07006932 // Workaround: If routing to an non existing usb device, fail gracefully
6933 // The routing request will otherwise block during 10 second
6934 int card;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08006935 struct str_parms *usb_addr =
6936 str_parms_create_str(get_usb_device_address(devices));
6937 if (is_usb_in_device_type(devices) && usb_addr &&
6938 (card = get_alive_usb_card(usb_addr)) >= 0) {
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07006939 ALOGW("%s: ignoring rerouting to non existing USB card %d", __func__, card);
6940 ret = -ENOSYS;
6941 } else {
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07006942 /* If recording is in progress, change the tx device to new device */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08006943 assign_devices(&in->device_list, devices);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07006944 if (!in->standby && !in->is_st_session) {
6945 ALOGV("update input routing change");
6946 // inform adm before actual routing to prevent glitches.
6947 if (adev->adm_on_routing_change) {
6948 adev->adm_on_routing_change(adev->adm_data,
6949 in->capture_handle);
6950 ret = select_devices(adev, in->usecase);
6951 if (in->usecase == USECASE_AUDIO_RECORD_LOW_LATENCY)
6952 adev->adm_routing_changed = true;
6953 }
6954 }
6955 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08006956 if (usb_addr)
6957 str_parms_destroy(usb_addr);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07006958 }
6959 pthread_mutex_unlock(&adev->lock);
6960 pthread_mutex_unlock(&in->lock);
6961
6962 ALOGD("%s: exit: status(%d)", __func__, ret);
6963 return ret;
6964}
6965
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006966static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
6967{
6968 struct stream_in *in = (struct stream_in *)stream;
6969 struct audio_device *adev = in->dev;
6970 struct str_parms *parms;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006971 char value[32];
Sujin Panicker7c7b6f92020-04-03 12:57:55 +05306972 int err = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006973
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05306974 ALOGD("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006975 parms = str_parms_create_str(kvpairs);
6976
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05306977 if (!parms)
6978 goto error;
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006979 lock_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08006980 pthread_mutex_lock(&adev->lock);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08006981
Sujin Panicker7c7b6f92020-04-03 12:57:55 +05306982 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_PROFILE, value, sizeof(value));
6983 if (err >= 0) {
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +05306984 strlcpy(in->profile, value, sizeof(in->profile));
6985 ALOGV("updating stream profile with value '%s'", in->profile);
6986 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
6987 &adev->streams_input_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08006988 &in->device_list, in->flags, in->format,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +05306989 in->sample_rate, in->bit_width,
6990 in->profile, &in->app_type_cfg);
6991 }
6992
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006993 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08006994 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006995
6996 str_parms_destroy(parms);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05306997error:
Sujin Panicker7c7b6f92020-04-03 12:57:55 +05306998 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006999}
7000
7001static char* in_get_parameters(const struct audio_stream *stream,
7002 const char *keys)
7003{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007004 struct stream_in *in = (struct stream_in *)stream;
7005 struct str_parms *query = str_parms_create_str(keys);
7006 char *str;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007007 struct str_parms *reply = str_parms_create();
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07007008
7009 if (!query || !reply) {
Alexy Josephaee4fdd2016-01-29 13:02:07 -08007010 if (reply) {
7011 str_parms_destroy(reply);
7012 }
7013 if (query) {
7014 str_parms_destroy(query);
7015 }
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07007016 ALOGE("in_get_parameters: failed to create query or reply");
7017 return NULL;
7018 }
7019
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007020 ALOGV("%s: enter: keys - %s %s ", __func__, use_case_table[in->usecase], keys);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007021
7022 voice_extn_in_get_parameters(in, query, reply);
7023
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007024 stream_get_parameter_channels(query, reply,
7025 &in->supported_channel_masks[0]);
7026 stream_get_parameter_formats(query, reply,
7027 &in->supported_formats[0]);
7028 stream_get_parameter_rates(query, reply,
7029 &in->supported_sample_rates[0]);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007030 str = str_parms_to_str(reply);
7031 str_parms_destroy(query);
7032 str_parms_destroy(reply);
7033
7034 ALOGV("%s: exit: returns - %s", __func__, str);
7035 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007036}
7037
Aalique Grahame22e49102018-12-18 14:23:57 -08007038static int in_set_gain(struct audio_stream_in *stream,
7039 float gain)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007040{
Aalique Grahame22e49102018-12-18 14:23:57 -08007041 struct stream_in *in = (struct stream_in *)stream;
7042 char mixer_ctl_name[128];
7043 struct mixer_ctl *ctl;
7044 int ctl_value;
7045
7046 ALOGV("%s: gain %f", __func__, gain);
7047
7048 if (stream == NULL)
7049 return -EINVAL;
7050
7051 /* in_set_gain() only used to silence MMAP capture for now */
7052 if (in->usecase != USECASE_AUDIO_RECORD_MMAP)
7053 return -ENOSYS;
7054
7055 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name), "Capture %d Volume", in->pcm_device_id);
7056
7057 ctl = mixer_get_ctl_by_name(in->dev->mixer, mixer_ctl_name);
7058 if (!ctl) {
7059 ALOGW("%s: Could not get ctl for mixer cmd - %s",
7060 __func__, mixer_ctl_name);
7061 return -ENOSYS;
7062 }
7063
7064 if (gain < RECORD_GAIN_MIN)
7065 gain = RECORD_GAIN_MIN;
7066 else if (gain > RECORD_GAIN_MAX)
7067 gain = RECORD_GAIN_MAX;
7068 ctl_value = (int)(RECORD_VOLUME_CTL_MAX * gain);
7069
7070 mixer_ctl_set_value(ctl, 0, ctl_value);
7071
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007072 return 0;
7073}
7074
7075static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
7076 size_t bytes)
7077{
7078 struct stream_in *in = (struct stream_in *)stream;
Pallavid7c7a272018-01-16 11:22:55 +05307079
7080 if (in == NULL) {
7081 ALOGE("%s: stream_in ptr is NULL", __func__);
7082 return -EINVAL;
7083 }
7084
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007085 struct audio_device *adev = in->dev;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05307086 int ret = -1;
Dhananjay Kumar97b81e32019-05-15 21:00:21 +05307087 size_t bytes_read = 0, frame_size = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007088
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07007089 lock_input_stream(in);
Naresh Tanniru4c630392014-05-12 01:05:52 +05307090
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07007091 if (in->is_st_session) {
7092 ALOGVV(" %s: reading on st session bytes=%zu", __func__, bytes);
7093 /* Read from sound trigger HAL */
7094 audio_extn_sound_trigger_read(in, buffer, bytes);
Quinn Malef6050362019-01-30 15:55:40 -08007095 if (in->standby) {
George Gao3018ede2019-10-23 13:23:00 -07007096 if (adev->num_va_sessions < UINT_MAX)
7097 adev->num_va_sessions++;
Quinn Malef6050362019-01-30 15:55:40 -08007098 in->standby = 0;
7099 }
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07007100 pthread_mutex_unlock(&in->lock);
7101 return bytes;
7102 }
7103
Haynes Mathew George16081042017-05-31 17:16:49 -07007104 if (in->usecase == USECASE_AUDIO_RECORD_MMAP) {
7105 ret = -ENOSYS;
7106 goto exit;
7107 }
7108
Aniket Kumar Lata60586a92019-05-22 22:18:55 -07007109 if (in->usecase == USECASE_AUDIO_RECORD_LOW_LATENCY &&
7110 !in->standby && adev->adm_routing_changed) {
7111 ret = -ENOSYS;
7112 goto exit;
7113 }
7114
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007115 if (in->standby) {
Andy Hungc6bfd4a2019-07-01 18:26:00 -07007116 const int64_t startNs = systemTime(SYSTEM_TIME_MONOTONIC);
7117
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07007118 pthread_mutex_lock(&adev->lock);
7119 if (in->usecase == USECASE_COMPRESS_VOIP_CALL)
7120 ret = voice_extn_compress_voip_start_input_stream(in);
7121 else
7122 ret = start_input_stream(in);
George Gao3018ede2019-10-23 13:23:00 -07007123 if (!ret && in->source == AUDIO_SOURCE_VOICE_RECOGNITION) {
7124 if (adev->num_va_sessions < UINT_MAX)
7125 adev->num_va_sessions++;
7126 }
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07007127 pthread_mutex_unlock(&adev->lock);
7128 if (ret != 0) {
7129 goto exit;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007130 }
7131 in->standby = 0;
Andy Hungc6bfd4a2019-07-01 18:26:00 -07007132
7133 // log startup time in ms.
7134 simple_stats_log(
7135 &in->start_latency_ms, (systemTime(SYSTEM_TIME_MONOTONIC) - startNs) * 1e-6);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007136 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007137
Deeraj Soman30cc7ae2019-03-18 16:26:55 +05307138 /* Avoid read if capture_stopped is set */
7139 if (android_atomic_acquire_load(&(in->capture_stopped)) > 0) {
7140 ALOGD("%s: force stopped catpure session, ignoring read request", __func__);
7141 ret = -EINVAL;
7142 goto exit;
7143 }
7144
Haynes Mathew George5beddd42016-06-27 18:33:40 -07007145 // what's the duration requested by the client?
7146 long ns = 0;
7147
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307148 if (in->pcm && in->config.rate)
Haynes Mathew George5beddd42016-06-27 18:33:40 -07007149 ns = pcm_bytes_to_frames(in->pcm, bytes)*1000000000LL/
7150 in->config.rate;
7151
Aniket Kumar Lata60586a92019-05-22 22:18:55 -07007152 ret = request_in_focus(in, ns);
7153 if (ret != 0)
7154 goto exit;
Haynes Mathew George5beddd42016-06-27 18:33:40 -07007155 bool use_mmap = is_mmap_usecase(in->usecase) || in->realtime;
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07007156
Dhananjay Kumar7dbe3562019-08-30 05:49:33 +05307157 if (audio_extn_cin_attached_usecase(in)) {
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307158 ret = audio_extn_cin_read(in, buffer, bytes, &bytes_read);
7159 } else if (in->pcm) {
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05307160 if (audio_extn_ssr_get_stream() == in) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07007161 ret = audio_extn_ssr_read(stream, buffer, bytes);
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05307162 } else if (audio_extn_compr_cap_usecase_supported(in->usecase)) {
Mingming Yine62d7842013-10-25 16:26:03 -07007163 ret = audio_extn_compr_cap_read(in, buffer, bytes);
Haynes Mathew George5beddd42016-06-27 18:33:40 -07007164 } else if (use_mmap) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07007165 ret = pcm_mmap_read(in->pcm, buffer, bytes);
Garmond Leunge2433c32017-09-28 21:51:22 -07007166 } else if (audio_extn_ffv_get_stream() == in) {
7167 ret = audio_extn_ffv_read(stream, buffer, bytes);
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05307168 } else {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07007169 ret = pcm_read(in->pcm, buffer, bytes);
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307170 /* data from DSP comes in 24_8 format, convert it to 8_24 */
7171 if (!ret && bytes > 0 && (in->format == AUDIO_FORMAT_PCM_8_24_BIT)) {
7172 if (audio_extn_utils_convert_format_24_8_to_8_24(buffer, bytes)
7173 != bytes) {
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05307174 ret = -EINVAL;
7175 goto exit;
7176 }
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307177 } else if (ret < 0) {
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05307178 ret = -errno;
7179 }
7180 }
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307181 /* bytes read is always set to bytes for non compress usecases */
7182 bytes_read = bytes;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007183 }
7184
Haynes Mathew George5beddd42016-06-27 18:33:40 -07007185 release_in_focus(in);
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07007186
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007187 /*
Quinn Malef6050362019-01-30 15:55:40 -08007188 * Instead of writing zeroes here, we could trust the hardware to always
7189 * provide zeroes when muted. This is also muted with voice recognition
7190 * usecases so that other clients do not have access to voice recognition
7191 * data.
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007192 */
Quinn Malef6050362019-01-30 15:55:40 -08007193 if ((ret == 0 && voice_get_mic_mute(adev) &&
7194 !voice_is_in_call_rec_stream(in) &&
Zhou Song62ea0282020-03-22 19:53:01 +08007195 (in->usecase != USECASE_AUDIO_RECORD_AFE_PROXY &&
7196 in->usecase != USECASE_AUDIO_RECORD_AFE_PROXY2)) ||
Quinn Malef6050362019-01-30 15:55:40 -08007197 (adev->num_va_sessions &&
7198 in->source != AUDIO_SOURCE_VOICE_RECOGNITION &&
7199 property_get_bool("persist.vendor.audio.va_concurrency_mute_enabled",
7200 false)))
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007201 memset(buffer, 0, bytes);
7202
7203exit:
Dhananjay Kumar97b81e32019-05-15 21:00:21 +05307204 frame_size = audio_stream_in_frame_size(stream);
7205 if (frame_size > 0)
7206 in->frames_read += bytes_read/frame_size;
7207
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07007208 if (-ENETRESET == ret)
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05307209 in->card_status = CARD_STATUS_OFFLINE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007210 pthread_mutex_unlock(&in->lock);
7211
7212 if (ret != 0) {
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05307213 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05307214 pthread_mutex_lock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05307215 voice_extn_compress_voip_close_input_stream(&in->stream.common);
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05307216 pthread_mutex_unlock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05307217 in->standby = true;
7218 }
Dhananjay Kumar7dbe3562019-08-30 05:49:33 +05307219 if (!audio_extn_cin_attached_usecase(in)) {
Sharad Sangled17c9122017-03-20 15:58:52 +05307220 bytes_read = bytes;
7221 memset(buffer, 0, bytes);
7222 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007223 in_standby(&in->stream.common);
Aniket Kumar Lata60586a92019-05-22 22:18:55 -07007224 if (in->usecase == USECASE_AUDIO_RECORD_LOW_LATENCY)
7225 adev->adm_routing_changed = false;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07007226 ALOGV("%s: read failed status %d- sleeping for buffer duration", __func__, ret);
Ashish Jainbbce4322016-02-16 13:25:27 +05307227 usleep((uint64_t)bytes * 1000000 / audio_stream_in_frame_size(stream) /
Naresh Tanniru4c630392014-05-12 01:05:52 +05307228 in_get_sample_rate(&in->stream.common));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007229 }
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307230 return bytes_read;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007231}
7232
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07007233static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007234{
7235 return 0;
7236}
7237
Aalique Grahame22e49102018-12-18 14:23:57 -08007238static int in_get_capture_position(const struct audio_stream_in *stream,
7239 int64_t *frames, int64_t *time)
7240{
7241 if (stream == NULL || frames == NULL || time == NULL) {
7242 return -EINVAL;
7243 }
7244 struct stream_in *in = (struct stream_in *)stream;
7245 int ret = -ENOSYS;
7246
7247 lock_input_stream(in);
7248 // note: ST sessions do not close the alsa pcm driver synchronously
7249 // on standby. Therefore, we may return an error even though the
7250 // pcm stream is still opened.
7251 if (in->standby) {
7252 ALOGE_IF(in->pcm != NULL && !in->is_st_session,
7253 "%s stream in standby but pcm not NULL for non ST session", __func__);
7254 goto exit;
7255 }
7256 if (in->pcm) {
7257 struct timespec timestamp;
7258 unsigned int avail;
7259 if (pcm_get_htimestamp(in->pcm, &avail, &timestamp) == 0) {
7260 *frames = in->frames_read + avail;
Robert Lee58215542019-07-15 20:55:12 +08007261 *time = timestamp.tv_sec * 1000000000LL + timestamp.tv_nsec
George Gao9ba8a142020-07-23 14:30:03 -07007262 - platform_capture_latency(in) * 1000LL;
Aalique Grahame22e49102018-12-18 14:23:57 -08007263 ret = 0;
7264 }
7265 }
7266exit:
7267 pthread_mutex_unlock(&in->lock);
7268 return ret;
7269}
7270
Carter Hsu2e429db2019-05-14 18:50:52 +08007271static int in_update_effect_list(bool add, effect_handle_t effect,
7272 struct listnode *head)
7273{
7274 struct listnode *node;
7275 struct in_effect_list *elist = NULL;
7276 struct in_effect_list *target = NULL;
7277 int ret = 0;
7278
7279 if (!head)
7280 return ret;
7281
7282 list_for_each(node, head) {
7283 elist = node_to_item(node, struct in_effect_list, list);
7284 if (elist->handle == effect) {
7285 target = elist;
7286 break;
7287 }
7288 }
7289
7290 if (add) {
7291 if (target) {
7292 ALOGD("effect %p already exist", effect);
7293 return ret;
7294 }
7295
7296 target = (struct in_effect_list *)
7297 calloc(1, sizeof(struct in_effect_list));
7298
7299 if (!target) {
7300 ALOGE("%s:fail to allocate memory", __func__);
7301 return -ENOMEM;
7302 }
7303
7304 target->handle = effect;
7305 list_add_tail(head, &target->list);
7306 } else {
7307 if (target) {
7308 list_remove(&target->list);
7309 free(target);
7310 }
7311 }
7312
7313 return ret;
7314}
7315
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007316static int add_remove_audio_effect(const struct audio_stream *stream,
7317 effect_handle_t effect,
7318 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007319{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007320 struct stream_in *in = (struct stream_in *)stream;
7321 int status = 0;
7322 effect_descriptor_t desc;
7323
7324 status = (*effect)->get_descriptor(effect, &desc);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07007325 ALOGV("%s: status %d in->standby %d enable:%d", __func__, status, in->standby, enable);
7326
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007327 if (status != 0)
7328 return status;
7329
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07007330 lock_input_stream(in);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007331 pthread_mutex_lock(&in->dev->lock);
kunleizd96526c2018-04-09 11:12:32 +08007332 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
Carter Hsu2e429db2019-05-14 18:50:52 +08007333 in->source == AUDIO_SOURCE_VOICE_RECOGNITION ||
7334 adev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007335 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
Carter Hsu2e429db2019-05-14 18:50:52 +08007336
7337 in_update_effect_list(enable, effect, &in->aec_list);
7338 enable = !list_empty(&in->aec_list);
7339 if (enable == in->enable_aec)
7340 goto exit;
7341
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007342 in->enable_aec = enable;
Carter Hsu2e429db2019-05-14 18:50:52 +08007343 ALOGD("AEC enable %d", enable);
7344
Aalique Grahame22e49102018-12-18 14:23:57 -08007345 if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
7346 in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) {
7347 in->dev->enable_voicerx = enable;
7348 struct audio_usecase *usecase;
7349 struct listnode *node;
7350 list_for_each(node, &in->dev->usecase_list) {
7351 usecase = node_to_item(node, struct audio_usecase, list);
7352 if (usecase->type == PCM_PLAYBACK)
7353 select_devices(in->dev, usecase->id);
7354 }
7355 }
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07007356 if (!in->standby) {
7357 if (enable_disable_effect(in->dev, EFFECT_AEC, enable) == ENOSYS)
7358 select_devices(in->dev, in->usecase);
7359 }
7360
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007361 }
Carter Hsu2e429db2019-05-14 18:50:52 +08007362 if (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0) {
7363
7364 in_update_effect_list(enable, effect, &in->ns_list);
7365 enable = !list_empty(&in->ns_list);
7366 if (enable == in->enable_ns)
7367 goto exit;
7368
Ravi Kumar Alamanda198185e2013-11-07 15:42:19 -08007369 in->enable_ns = enable;
Carter Hsu2e429db2019-05-14 18:50:52 +08007370 ALOGD("NS enable %d", enable);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07007371 if (!in->standby) {
kunleizd96526c2018-04-09 11:12:32 +08007372 if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
7373 in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) {
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07007374 if (enable_disable_effect(in->dev, EFFECT_NS, enable) == ENOSYS)
7375 select_devices(in->dev, in->usecase);
7376 } else
7377 select_devices(in->dev, in->usecase);
7378 }
Ravi Kumar Alamanda198185e2013-11-07 15:42:19 -08007379 }
Carter Hsu2e429db2019-05-14 18:50:52 +08007380exit:
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007381 pthread_mutex_unlock(&in->dev->lock);
7382 pthread_mutex_unlock(&in->lock);
7383
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007384 return 0;
7385}
7386
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007387static int in_add_audio_effect(const struct audio_stream *stream,
7388 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007389{
Eric Laurent994a6932013-07-17 11:51:42 -07007390 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007391 return add_remove_audio_effect(stream, effect, true);
7392}
7393
7394static int in_remove_audio_effect(const struct audio_stream *stream,
7395 effect_handle_t effect)
7396{
Eric Laurent994a6932013-07-17 11:51:42 -07007397 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007398 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007399}
7400
Derek Chenf939fb72018-11-13 13:34:41 -08007401streams_input_ctxt_t *in_get_stream(struct audio_device *dev,
7402 audio_io_handle_t input)
7403{
7404 struct listnode *node;
7405
7406 list_for_each(node, &dev->active_inputs_list) {
7407 streams_input_ctxt_t *in_ctxt = node_to_item(node,
7408 streams_input_ctxt_t,
7409 list);
7410 if (in_ctxt->input->capture_handle == input) {
7411 return in_ctxt;
7412 }
7413 }
7414 return NULL;
7415}
7416
7417streams_output_ctxt_t *out_get_stream(struct audio_device *dev,
7418 audio_io_handle_t output)
7419{
7420 struct listnode *node;
7421
7422 list_for_each(node, &dev->active_outputs_list) {
7423 streams_output_ctxt_t *out_ctxt = node_to_item(node,
7424 streams_output_ctxt_t,
7425 list);
7426 if (out_ctxt->output->handle == output) {
7427 return out_ctxt;
7428 }
7429 }
7430 return NULL;
7431}
7432
Haynes Mathew George16081042017-05-31 17:16:49 -07007433static int in_stop(const struct audio_stream_in* stream)
7434{
7435 struct stream_in *in = (struct stream_in *)stream;
7436 struct audio_device *adev = in->dev;
7437
7438 int ret = -ENOSYS;
7439 ALOGV("%s", __func__);
7440 pthread_mutex_lock(&adev->lock);
7441 if (in->usecase == USECASE_AUDIO_RECORD_MMAP && !in->standby &&
7442 in->capture_started && in->pcm != NULL) {
7443 pcm_stop(in->pcm);
7444 ret = stop_input_stream(in);
7445 in->capture_started = false;
7446 }
7447 pthread_mutex_unlock(&adev->lock);
7448 return ret;
7449}
7450
7451static int in_start(const struct audio_stream_in* stream)
7452{
7453 struct stream_in *in = (struct stream_in *)stream;
7454 struct audio_device *adev = in->dev;
7455 int ret = -ENOSYS;
7456
7457 ALOGV("%s in %p", __func__, in);
7458 pthread_mutex_lock(&adev->lock);
7459 if (in->usecase == USECASE_AUDIO_RECORD_MMAP && !in->standby &&
7460 !in->capture_started && in->pcm != NULL) {
7461 if (!in->capture_started) {
7462 ret = start_input_stream(in);
7463 if (ret == 0) {
7464 in->capture_started = true;
7465 }
7466 }
7467 }
7468 pthread_mutex_unlock(&adev->lock);
7469 return ret;
7470}
7471
Phil Burke0a86d12019-02-16 22:28:11 -08007472// Read offset for the positional timestamp from a persistent vendor property.
7473// This is to workaround apparent inaccuracies in the timing information that
7474// is used by the AAudio timing model. The inaccuracies can cause glitches.
7475static int64_t in_get_mmap_time_offset() {
7476 const int32_t kDefaultOffsetMicros = 0;
7477 int32_t mmap_time_offset_micros = property_get_int32(
Weiyin Jiangbd68b4d2019-05-17 16:29:50 +08007478 "persist.vendor.audio.in_mmap_delay_micros", kDefaultOffsetMicros);
Phil Burke0a86d12019-02-16 22:28:11 -08007479 ALOGI("mmap_time_offset_micros = %d for input", mmap_time_offset_micros);
7480 return mmap_time_offset_micros * (int64_t)1000;
7481}
7482
Haynes Mathew George16081042017-05-31 17:16:49 -07007483static int in_create_mmap_buffer(const struct audio_stream_in *stream,
7484 int32_t min_size_frames,
7485 struct audio_mmap_buffer_info *info)
7486{
7487 struct stream_in *in = (struct stream_in *)stream;
7488 struct audio_device *adev = in->dev;
7489 int ret = 0;
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07007490 unsigned int offset1 = 0;
7491 unsigned int frames1 = 0;
Haynes Mathew George16081042017-05-31 17:16:49 -07007492 const char *step = "";
Arun Mirpuri5d170872019-03-26 13:21:31 -07007493 uint32_t mmap_size = 0;
7494 uint32_t buffer_size = 0;
Haynes Mathew George16081042017-05-31 17:16:49 -07007495
7496 pthread_mutex_lock(&adev->lock);
7497 ALOGV("%s in %p", __func__, in);
7498
Sharad Sanglec6f32552018-05-04 16:15:38 +05307499 if (CARD_STATUS_OFFLINE == in->card_status||
7500 CARD_STATUS_OFFLINE == adev->card_status) {
7501 ALOGW("in->card_status or adev->card_status offline, try again");
7502 ret = -EIO;
7503 goto exit;
7504 }
7505
Sujin Panicker7c7b6f92020-04-03 12:57:55 +05307506 if (info == NULL || !(min_size_frames > 0 && min_size_frames < INT32_MAX)) {
Haynes Mathew George16081042017-05-31 17:16:49 -07007507 ALOGE("%s invalid argument info %p min_size_frames %d", __func__, info, min_size_frames);
7508 ret = -EINVAL;
7509 goto exit;
7510 }
7511 if (in->usecase != USECASE_AUDIO_RECORD_MMAP || !in->standby) {
7512 ALOGE("%s: usecase = %d, standby = %d", __func__, in->usecase, in->standby);
7513 ALOGV("%s in %p", __func__, in);
7514 ret = -ENOSYS;
7515 goto exit;
7516 }
7517 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
7518 if (in->pcm_device_id < 0) {
7519 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
7520 __func__, in->pcm_device_id, in->usecase);
7521 ret = -EINVAL;
7522 goto exit;
7523 }
7524
7525 adjust_mmap_period_count(&in->config, min_size_frames);
7526
7527 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
7528 __func__, adev->snd_card, in->pcm_device_id, in->config.channels);
7529 in->pcm = pcm_open(adev->snd_card, in->pcm_device_id,
7530 (PCM_IN | PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC), &in->config);
Satish Babu Patakokila54ce83d2018-07-06 18:00:37 +05307531 if (errno == ENETRESET && !pcm_is_ready(in->pcm)) {
Sharad Sanglec6f32552018-05-04 16:15:38 +05307532 ALOGE("%s: pcm_open failed errno:%d\n", __func__, errno);
7533 in->card_status = CARD_STATUS_OFFLINE;
7534 adev->card_status = CARD_STATUS_OFFLINE;
7535 ret = -EIO;
7536 goto exit;
7537 }
7538
Haynes Mathew George16081042017-05-31 17:16:49 -07007539 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) {
7540 step = "open";
7541 ret = -ENODEV;
7542 goto exit;
7543 }
7544
7545 ret = pcm_mmap_begin(in->pcm, &info->shared_memory_address, &offset1, &frames1);
7546 if (ret < 0) {
7547 step = "begin";
7548 goto exit;
7549 }
Haynes Mathew George16081042017-05-31 17:16:49 -07007550
juyuchen626833d2019-06-04 16:48:02 +08007551 info->flags = 0;
Arun Mirpuri5d170872019-03-26 13:21:31 -07007552 info->buffer_size_frames = pcm_get_buffer_size(in->pcm);
7553 buffer_size = pcm_frames_to_bytes(in->pcm, info->buffer_size_frames);
7554 info->burst_size_frames = in->config.period_size;
7555 ret = platform_get_mmap_data_fd(adev->platform,
7556 in->pcm_device_id, 1 /*capture*/,
7557 &info->shared_memory_fd,
7558 &mmap_size);
7559 if (ret < 0) {
7560 // Fall back to non exclusive mode
7561 info->shared_memory_fd = pcm_get_poll_fd(in->pcm);
7562 } else {
Phil Burkd898ba62019-06-20 12:49:01 -07007563 in->mmap_shared_memory_fd = info->shared_memory_fd; // for closing later
7564 ALOGV("%s: opened mmap_shared_memory_fd = %d", __func__, in->mmap_shared_memory_fd);
7565
Arun Mirpuri5d170872019-03-26 13:21:31 -07007566 if (mmap_size < buffer_size) {
7567 step = "mmap";
7568 goto exit;
7569 }
juyuchen626833d2019-06-04 16:48:02 +08007570 info->flags |= AUDIO_MMAP_APPLICATION_SHAREABLE;
Arun Mirpuri5d170872019-03-26 13:21:31 -07007571 }
7572
7573 memset(info->shared_memory_address, 0, buffer_size);
Haynes Mathew George16081042017-05-31 17:16:49 -07007574
7575 ret = pcm_mmap_commit(in->pcm, 0, MMAP_PERIOD_SIZE);
7576 if (ret < 0) {
7577 step = "commit";
7578 goto exit;
7579 }
7580
Phil Burke0a86d12019-02-16 22:28:11 -08007581 in->mmap_time_offset_nanos = in_get_mmap_time_offset();
7582
Haynes Mathew George16081042017-05-31 17:16:49 -07007583 in->standby = false;
7584 ret = 0;
7585
7586 ALOGV("%s: got mmap buffer address %p info->buffer_size_frames %d",
7587 __func__, info->shared_memory_address, info->buffer_size_frames);
7588
7589exit:
7590 if (ret != 0) {
7591 if (in->pcm == NULL) {
7592 ALOGE("%s: %s - %d", __func__, step, ret);
7593 } else {
7594 ALOGE("%s: %s %s", __func__, step, pcm_get_error(in->pcm));
7595 pcm_close(in->pcm);
7596 in->pcm = NULL;
7597 }
7598 }
7599 pthread_mutex_unlock(&adev->lock);
7600 return ret;
7601}
7602
7603static int in_get_mmap_position(const struct audio_stream_in *stream,
7604 struct audio_mmap_position *position)
7605{
7606 struct stream_in *in = (struct stream_in *)stream;
7607 ALOGVV("%s", __func__);
7608 if (position == NULL) {
7609 return -EINVAL;
7610 }
7611 if (in->usecase != USECASE_AUDIO_RECORD_MMAP) {
7612 return -ENOSYS;
7613 }
7614 if (in->pcm == NULL) {
7615 return -ENOSYS;
7616 }
7617 struct timespec ts = { 0, 0 };
7618 int ret = pcm_mmap_get_hw_ptr(in->pcm, (unsigned int *)&position->position_frames, &ts);
7619 if (ret < 0) {
7620 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
7621 return ret;
7622 }
Phil Burke0a86d12019-02-16 22:28:11 -08007623 position->time_nanoseconds = ts.tv_sec*1000000000LL + ts.tv_nsec
7624 + in->mmap_time_offset_nanos;
Haynes Mathew George16081042017-05-31 17:16:49 -07007625 return 0;
7626}
7627
Naresh Tannirudcb47c52018-06-25 16:23:32 +05307628static int in_get_active_microphones(const struct audio_stream_in *stream,
7629 struct audio_microphone_characteristic_t *mic_array,
7630 size_t *mic_count) {
7631 struct stream_in *in = (struct stream_in *)stream;
7632 struct audio_device *adev = in->dev;
7633 ALOGVV("%s", __func__);
7634
7635 lock_input_stream(in);
7636 pthread_mutex_lock(&adev->lock);
7637 int ret = platform_get_active_microphones(adev->platform,
7638 audio_channel_count_from_in_mask(in->channel_mask),
7639 in->usecase, mic_array, mic_count);
7640 pthread_mutex_unlock(&adev->lock);
7641 pthread_mutex_unlock(&in->lock);
7642
7643 return ret;
7644}
7645
7646static int adev_get_microphones(const struct audio_hw_device *dev,
7647 struct audio_microphone_characteristic_t *mic_array,
7648 size_t *mic_count) {
7649 struct audio_device *adev = (struct audio_device *)dev;
7650 ALOGVV("%s", __func__);
7651
7652 pthread_mutex_lock(&adev->lock);
7653 int ret = platform_get_microphones(adev->platform, mic_array, mic_count);
7654 pthread_mutex_unlock(&adev->lock);
7655
7656 return ret;
7657}
juyuchendb308c22019-01-21 11:57:17 -07007658
7659static void in_update_sink_metadata(struct audio_stream_in *stream,
7660 const struct sink_metadata *sink_metadata) {
7661
7662 if (stream == NULL
7663 || sink_metadata == NULL
7664 || sink_metadata->tracks == NULL) {
7665 return;
7666 }
7667
7668 int error = 0;
7669 struct stream_in *in = (struct stream_in *)stream;
7670 struct audio_device *adev = in->dev;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08007671 struct listnode devices;
Zhou Song62ea0282020-03-22 19:53:01 +08007672 bool is_ha_usecase = false;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08007673
7674 list_init(&devices);
juyuchendb308c22019-01-21 11:57:17 -07007675
7676 if (sink_metadata->track_count != 0)
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08007677 reassign_device_list(&devices, sink_metadata->tracks->dest_device, "");
juyuchendb308c22019-01-21 11:57:17 -07007678
7679 lock_input_stream(in);
7680 pthread_mutex_lock(&adev->lock);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08007681 ALOGV("%s: in->usecase: %d, device: %x", __func__, in->usecase, get_device_types(&devices));
juyuchendb308c22019-01-21 11:57:17 -07007682
Zhou Song62ea0282020-03-22 19:53:01 +08007683 is_ha_usecase = adev->ha_proxy_enable ?
7684 in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY2 :
7685 in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY;
7686 if (is_ha_usecase && !list_empty(&devices)
juyuchendb308c22019-01-21 11:57:17 -07007687 && adev->voice_tx_output != NULL) {
7688 /* Use the rx device from afe-proxy record to route voice call because
7689 there is no routing if tx device is on primary hal and rx device
7690 is on other hal during voice call. */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08007691 assign_devices(&adev->voice_tx_output->device_list, &devices);
juyuchendb308c22019-01-21 11:57:17 -07007692
7693 if (!voice_is_call_state_active(adev)) {
7694 if (adev->mode == AUDIO_MODE_IN_CALL) {
7695 adev->current_call_output = adev->voice_tx_output;
7696 error = voice_start_call(adev);
7697 if (error != 0)
7698 ALOGE("%s: start voice call failed %d", __func__, error);
7699 }
7700 } else {
7701 adev->current_call_output = adev->voice_tx_output;
7702 voice_update_devices_for_all_voice_usecases(adev);
7703 }
7704 }
7705
7706 pthread_mutex_unlock(&adev->lock);
7707 pthread_mutex_unlock(&in->lock);
7708}
7709
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05307710int adev_open_output_stream(struct audio_hw_device *dev,
Haynes Mathew George16081042017-05-31 17:16:49 -07007711 audio_io_handle_t handle,
7712 audio_devices_t devices,
7713 audio_output_flags_t flags,
7714 struct audio_config *config,
7715 struct audio_stream_out **stream_out,
Derek Chenf6318be2017-06-12 17:16:24 -04007716 const char *address)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007717{
7718 struct audio_device *adev = (struct audio_device *)dev;
7719 struct stream_out *out;
Gangadhar Sb0210342019-02-22 17:39:41 +05307720 int ret = 0, ip_hdlr_stream = 0, ip_hdlr_dev = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07007721 audio_format_t format;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08007722 struct adsp_hdlr_stream_cfg hdlr_stream_cfg;
Manish Dewangan21a850a2017-08-14 12:03:55 +05307723 bool is_direct_passthough = false;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007724 bool is_hdmi = devices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
7725 bool is_usb_dev = audio_is_usb_out_device(devices) &&
7726 (devices != AUDIO_DEVICE_OUT_USB_ACCESSORY);
7727 bool direct_dev = is_hdmi || is_usb_dev;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08007728 bool use_db_as_primary =
vivek mehtaae1018c2019-05-09 12:19:57 -07007729 property_get_bool("vendor.audio.feature.deepbuffer_as_primary.enable",
7730 false);
Vignesh Kulothungana6927272019-02-20 15:17:07 -08007731 bool force_haptic_path =
7732 property_get_bool("vendor.audio.test_haptic", false);
Aniket Kumar Lata23d8cbf2019-04-10 19:54:46 -07007733 bool is_voip_rx = flags & AUDIO_OUTPUT_FLAG_VOIP_RX;
Xiaojun Sang782e5b12020-06-29 21:13:06 +08007734#ifdef AUDIO_GKI_ENABLED
7735 __s32 *generic_dec;
7736#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007737
kunleizdff872d2018-08-20 14:40:33 +08007738 if (is_usb_dev && (!audio_extn_usb_connected(NULL))) {
kunleizd6a9e0c2018-07-30 15:38:52 +08007739 is_usb_dev = false;
7740 devices = AUDIO_DEVICE_OUT_SPEAKER;
7741 ALOGW("%s: ignore set device to non existing USB card, use output device(%#x)",
7742 __func__, devices);
Mingshu Pangdbd20562019-11-25 18:04:39 +08007743 if (config->format == AUDIO_FORMAT_DEFAULT)
7744 config->format = AUDIO_FORMAT_PCM_16_BIT;
7745 if (config->sample_rate == 0)
7746 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
7747 if (config->channel_mask == AUDIO_CHANNEL_NONE)
7748 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
kunleizd6a9e0c2018-07-30 15:38:52 +08007749 }
7750
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007751 *stream_out = NULL;
Naresh Tanniru80659832014-06-04 18:17:56 +05307752
Rahul Sharma99770982019-03-06 17:05:26 +05307753 pthread_mutex_lock(&adev->lock);
7754 if (out_get_stream(adev, handle) != NULL) {
7755 ALOGW("%s, output stream already opened", __func__);
7756 ret = -EEXIST;
7757 }
7758 pthread_mutex_unlock(&adev->lock);
7759 if (ret)
7760 return ret;
7761
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007762 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
7763
Mingming Yin3a941d42016-02-17 18:08:05 -08007764 ALOGD("%s: enter: format(%#x) sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)\
Derek Chenf6318be2017-06-12 17:16:24 -04007765 stream_handle(%p) address(%s)", __func__, config->format, config->sample_rate, config->channel_mask,
7766 devices, flags, &out->stream, address);
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05307767
7768
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08007769 if (!out) {
7770 return -ENOMEM;
7771 }
7772
Haynes Mathew George204045b2015-02-25 20:32:03 -08007773 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07007774 pthread_mutex_init(&out->pre_lock, (const pthread_mutexattr_t *) NULL);
Weiyin Jiangd5974e62020-09-08 20:28:22 +08007775 pthread_mutex_init(&out->latch_lock, (const pthread_mutexattr_t *) NULL);
Zhou Song48453a02018-01-10 17:50:59 +08007776 pthread_mutex_init(&out->position_query_lock, (const pthread_mutexattr_t *) NULL);
Haynes Mathew George204045b2015-02-25 20:32:03 -08007777 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
7778
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007779 if (devices == AUDIO_DEVICE_NONE)
7780 devices = AUDIO_DEVICE_OUT_SPEAKER;
7781
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007782 out->flags = flags;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08007783 list_init(&out->device_list);
7784 update_device_list(&out->device_list, devices, address, true /* add devices */);
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07007785 out->dev = adev;
Aalique Grahame65780b52017-09-27 14:59:56 -07007786 out->hal_op_format = out->hal_ip_format = format = out->format = config->format;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07007787 out->sample_rate = config->sample_rate;
Sachin Mohan Gadag3d09acd2017-06-19 12:43:44 +05307788 out->channel_mask = config->channel_mask;
Ramjee Singh5857aeb2017-08-03 19:18:50 +05307789 if (out->channel_mask == AUDIO_CHANNEL_NONE)
7790 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
7791 else
7792 out->supported_channel_masks[0] = out->channel_mask;
Eric Laurentc4aef752013-09-12 17:45:53 -07007793 out->handle = handle;
Mingming Yin3ee55c62014-08-04 14:23:35 -07007794 out->bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Alexy Josephaa54c872014-12-03 02:46:47 -08007795 out->non_blocking = 0;
Ashish Jain83a6cc22016-06-28 14:34:17 +05307796 out->convert_buffer = NULL;
Ashish Jain1b9b30c2017-05-18 20:57:40 +05307797 out->started = 0;
Zhou Song1f93fa52020-11-20 13:57:39 +08007798 out->a2dp_muted = false;
Aniket Kumar Lata932f4872017-11-06 18:29:44 -08007799 out->hal_output_suspend_supported = 0;
7800 out->dynamic_pm_qos_config_supported = 0;
Surendar Karkaf51b5842018-04-26 11:28:38 +05307801 out->set_dual_mono = false;
Manisha Agarwal7b3e3772019-02-20 14:33:45 +05307802 out->prev_card_status_offline = false;
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +05307803 out->pspd_coeff_sent = false;
Phil Burkd898ba62019-06-20 12:49:01 -07007804 out->mmap_shared_memory_fd = -1; // not open
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007805
Nikhil Laturkar26b690b2017-07-25 11:06:14 +05307806 if ((flags & AUDIO_OUTPUT_FLAG_BD) &&
Satish Babu Patakokila37e7c482018-02-02 11:50:06 +05307807 (property_get_bool("vendor.audio.matrix.limiter.enable", false)))
Ben Romberger6c4d3812017-06-13 17:46:45 -07007808 platform_set_device_params(out, DEVICE_PARAM_LIMITER_ID, 1);
7809
Aalique Grahame22e49102018-12-18 14:23:57 -08007810 if (direct_dev &&
7811 (audio_is_linear_pcm(out->format) ||
7812 config->format == AUDIO_FORMAT_DEFAULT) &&
7813 out->flags == AUDIO_OUTPUT_FLAG_NONE) {
7814 audio_format_t req_format = config->format;
7815 audio_channel_mask_t req_channel_mask = config->channel_mask;
7816 uint32_t req_sample_rate = config->sample_rate;
7817
7818 pthread_mutex_lock(&adev->lock);
7819 if (is_hdmi) {
7820 ALOGV("AUDIO_DEVICE_OUT_AUX_DIGITAL and DIRECT|OFFLOAD, check hdmi caps");
7821 ret = read_hdmi_sink_caps(out);
7822 if (config->sample_rate == 0)
7823 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
7824 if (config->channel_mask == AUDIO_CHANNEL_NONE)
7825 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
7826 if (config->format == AUDIO_FORMAT_DEFAULT)
7827 config->format = AUDIO_FORMAT_PCM_16_BIT;
7828 } else if (is_usb_dev) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007829 ret = read_usb_sup_params_and_compare(true /*is_playback*/,
7830 &config->format,
7831 &out->supported_formats[0],
7832 MAX_SUPPORTED_FORMATS,
7833 &config->channel_mask,
7834 &out->supported_channel_masks[0],
7835 MAX_SUPPORTED_CHANNEL_MASKS,
7836 &config->sample_rate,
7837 &out->supported_sample_rates[0],
7838 MAX_SUPPORTED_SAMPLE_RATES);
7839 ALOGV("plugged dev USB ret %d", ret);
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007840 }
Aalique Grahame22e49102018-12-18 14:23:57 -08007841
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007842 pthread_mutex_unlock(&adev->lock);
7843 if (ret != 0) {
Mingming Yin3a941d42016-02-17 18:08:05 -08007844 if (ret == -ENOSYS) {
7845 /* ignore and go with default */
7846 ret = 0;
Aalique Grahame22e49102018-12-18 14:23:57 -08007847 }
7848 // For MMAP NO IRQ, allow conversions in ADSP
7849 else if (is_hdmi || (flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) == 0)
7850 goto error_open;
7851 else {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007852 ALOGE("error reading direct dev sink caps");
Mingming Yin3a941d42016-02-17 18:08:05 -08007853 goto error_open;
7854 }
Aalique Grahame22e49102018-12-18 14:23:57 -08007855
7856 if (req_sample_rate != 0 && config->sample_rate != req_sample_rate)
7857 config->sample_rate = req_sample_rate;
7858 if (req_channel_mask != AUDIO_CHANNEL_NONE && config->channel_mask != req_channel_mask)
7859 config->channel_mask = req_channel_mask;
7860 if (req_format != AUDIO_FORMAT_DEFAULT && config->format != req_format)
7861 config->format = req_format;
Mingming Yin3a941d42016-02-17 18:08:05 -08007862 }
Aalique Grahame22e49102018-12-18 14:23:57 -08007863
7864 out->sample_rate = config->sample_rate;
7865 out->channel_mask = config->channel_mask;
7866 out->format = config->format;
7867 if (is_hdmi) {
7868 out->usecase = USECASE_AUDIO_PLAYBACK_HIFI;
7869 out->config = pcm_config_hdmi_multi;
7870 } else if (flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) {
7871 out->usecase = USECASE_AUDIO_PLAYBACK_MMAP;
7872 out->config = pcm_config_mmap_playback;
7873 out->stream.start = out_start;
7874 out->stream.stop = out_stop;
7875 out->stream.create_mmap_buffer = out_create_mmap_buffer;
7876 out->stream.get_mmap_position = out_get_mmap_position;
7877 } else {
7878 out->usecase = USECASE_AUDIO_PLAYBACK_HIFI;
7879 out->config = pcm_config_hifi;
7880 }
7881
7882 out->config.rate = out->sample_rate;
7883 out->config.channels = audio_channel_count_from_out_mask(out->channel_mask);
7884 if (is_hdmi) {
7885 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels *
7886 audio_bytes_per_sample(out->format));
7887 }
7888 out->config.format = pcm_format_from_audio_format(out->format);
Mingming Yin3a941d42016-02-17 18:08:05 -08007889 }
7890
Derek Chenf6318be2017-06-12 17:16:24 -04007891 /* validate bus device address */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08007892 if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_BUS)) {
Derek Chenf6318be2017-06-12 17:16:24 -04007893 /* extract car audio stream index */
7894 out->car_audio_stream =
7895 audio_extn_auto_hal_get_car_audio_stream_from_address(address);
7896 if (out->car_audio_stream < 0) {
7897 ALOGE("%s: invalid car audio stream %x",
7898 __func__, out->car_audio_stream);
7899 ret = -EINVAL;
7900 goto error_open;
7901 }
Derek Chen5f67a942020-02-24 23:08:13 -08007902 ALOGV("%s: car_audio_stream %x", __func__, out->car_audio_stream);
Derek Chenf6318be2017-06-12 17:16:24 -04007903 }
7904
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08007905 /* Check for VOIP usecase */
Aniket Kumar Lata23d8cbf2019-04-10 19:54:46 -07007906 if (is_voip_rx) {
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08007907 if (!voice_extn_is_compress_voip_supported()) {
7908 if (out->sample_rate == 8000 || out->sample_rate == 16000 ||
7909 out->sample_rate == 32000 || out->sample_rate == 48000) {
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07007910 out->channel_mask = audio_extn_utils_is_vendor_enhanced_fwk() ?
Lakshman Chaluvarajue7fc9482020-05-30 14:29:29 +05307911 config->channel_mask : AUDIO_CHANNEL_OUT_STEREO;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08007912 out->usecase = USECASE_AUDIO_PLAYBACK_VOIP;
7913 out->format = AUDIO_FORMAT_PCM_16_BIT;
Aniket Kumar Lata8426d1f2019-06-10 15:25:53 -07007914 out->volume_l = INVALID_OUT_VOLUME;
7915 out->volume_r = INVALID_OUT_VOLUME;
Vikram Panduranga93f080e2017-06-07 18:16:14 -07007916
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08007917 out->config = default_pcm_config_voip_copp;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08007918 out->config.rate = out->sample_rate;
Aniket Kumar Lata1e344f22019-06-19 17:09:01 -07007919 uint32_t channel_count =
7920 audio_channel_count_from_out_mask(out->channel_mask);
Ramjee Singh8e34a2f2020-08-06 16:30:48 +05307921 out->config.channels = channel_count;
7922
Aniket Kumar Lata1e344f22019-06-19 17:09:01 -07007923 uint32_t buffer_size = get_stream_buffer_size(DEFAULT_VOIP_BUF_DURATION_MS,
7924 out->sample_rate, out->format,
7925 channel_count, false);
7926 uint32_t frame_size = audio_bytes_per_sample(out->format) * channel_count;
7927 if (frame_size != 0)
7928 out->config.period_size = buffer_size / frame_size;
7929 else
7930 ALOGW("%s: frame size is 0 for format %#x", __func__, out->format);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08007931 }
7932 } else {
7933 if ((out->dev->mode == AUDIO_MODE_IN_COMMUNICATION ||
7934 voice_extn_compress_voip_is_active(out->dev)) &&
7935 (voice_extn_compress_voip_is_config_supported(config))) {
7936 ret = voice_extn_compress_voip_open_output_stream(out);
7937 if (ret != 0) {
7938 ALOGE("%s: Compress voip output cannot be opened, error:%d",
7939 __func__, ret);
7940 goto error_open;
7941 }
Sujin Panicker19027262019-09-16 18:28:06 +05307942 } else {
7943 out->usecase = GET_USECASE_AUDIO_PLAYBACK_PRIMARY(use_db_as_primary);
7944 out->config = GET_PCM_CONFIG_AUDIO_PLAYBACK_PRIMARY(use_db_as_primary);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08007945 }
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007946 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007947 } else if (audio_is_linear_pcm(out->format) &&
7948 out->flags == AUDIO_OUTPUT_FLAG_NONE && is_usb_dev) {
7949 out->channel_mask = config->channel_mask;
7950 out->sample_rate = config->sample_rate;
7951 out->format = config->format;
7952 out->usecase = USECASE_AUDIO_PLAYBACK_HIFI;
7953 // does this change?
7954 out->config = is_hdmi ? pcm_config_hdmi_multi : pcm_config_hifi;
7955 out->config.rate = config->sample_rate;
7956 out->config.channels = audio_channel_count_from_out_mask(out->channel_mask);
7957 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels *
7958 audio_bytes_per_sample(config->format));
7959 out->config.format = pcm_format_from_audio_format(out->format);
vivek mehta0ea887a2015-08-26 14:01:20 -07007960 } else if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
Dhananjay Kumarac341582017-02-23 23:42:25 +05307961 (out->flags == AUDIO_OUTPUT_FLAG_DIRECT)) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05307962 pthread_mutex_lock(&adev->lock);
7963 bool offline = (adev->card_status == CARD_STATUS_OFFLINE);
7964 pthread_mutex_unlock(&adev->lock);
7965
7966 // reject offload during card offline to allow
7967 // fallback to s/w paths
7968 if (offline) {
7969 ret = -ENODEV;
7970 goto error_open;
7971 }
vivek mehta0ea887a2015-08-26 14:01:20 -07007972
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07007973 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
7974 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
7975 ALOGE("%s: Unsupported Offload information", __func__);
7976 ret = -EINVAL;
7977 goto error_open;
7978 }
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07007979
Atul Khare3fa6e542017-08-09 00:56:17 +05307980 if (config->offload_info.format == 0)
7981 config->offload_info.format = config->format;
7982 if (config->offload_info.sample_rate == 0)
7983 config->offload_info.sample_rate = config->sample_rate;
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07007984
Mingming Yin90310102013-11-13 16:57:00 -08007985 if (!is_supported_format(config->offload_info.format) &&
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05307986 !audio_extn_passthru_is_supported_format(config->offload_info.format)) {
vivek mehta0ea887a2015-08-26 14:01:20 -07007987 ALOGE("%s: Unsupported audio format %x " , __func__, config->offload_info.format);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07007988 ret = -EINVAL;
7989 goto error_open;
7990 }
7991
Ben Romberger0f8c87b2017-05-24 17:41:11 -07007992 /* TrueHD only supported for 48k multiples (48k, 96k, 192k) */
7993 if ((config->offload_info.format == AUDIO_FORMAT_DOLBY_TRUEHD) &&
7994 (audio_extn_passthru_is_passthrough_stream(out)) &&
7995 !((config->sample_rate == 48000) ||
7996 (config->sample_rate == 96000) ||
7997 (config->sample_rate == 192000))) {
7998 ALOGE("%s: Unsupported sample rate %d for audio format %x",
7999 __func__, config->sample_rate, config->offload_info.format);
8000 ret = -EINVAL;
8001 goto error_open;
8002 }
8003
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008004 out->compr_config.codec = (struct snd_codec *)
8005 calloc(1, sizeof(struct snd_codec));
8006
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07008007 if (!out->compr_config.codec) {
8008 ret = -ENOMEM;
8009 goto error_open;
8010 }
8011
Dhananjay Kumarac341582017-02-23 23:42:25 +05308012 out->stream.pause = out_pause;
8013 out->stream.resume = out_resume;
8014 out->stream.flush = out_flush;
Ashish Jain4847e9d2017-08-17 19:16:57 +05308015 out->stream.set_callback = out_set_callback;
Dhananjay Kumarac341582017-02-23 23:42:25 +05308016 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Mingming Yin21d60472015-09-30 13:56:25 -07008017 out->stream.drain = out_drain;
Dhananjay Kumarac341582017-02-23 23:42:25 +05308018 out->usecase = get_offload_usecase(adev, true /* is_compress */);
vivek mehta446c3962015-09-14 10:57:35 -07008019 ALOGV("Compress Offload usecase .. usecase selected %d", out->usecase);
Dhananjay Kumarac341582017-02-23 23:42:25 +05308020 } else {
8021 out->usecase = get_offload_usecase(adev, false /* is_compress */);
8022 ALOGV("non-offload DIRECT_usecase ... usecase selected %d ", out->usecase);
vivek mehta0ea887a2015-08-26 14:01:20 -07008023 }
vivek mehta446c3962015-09-14 10:57:35 -07008024
Deeraj Somanfa377bf2019-02-06 12:57:59 +05308025 if (out->flags & AUDIO_OUTPUT_FLAG_FAST) {
8026 ALOGD("%s: Setting latency mode to true", __func__);
Meng Wang4c32fb42020-01-16 17:57:11 +08008027#ifdef AUDIO_GKI_ENABLED
8028 /* out->compr_config.codec->reserved[1] is for flags */
8029 out->compr_config.codec->reserved[1] |= audio_extn_utils_get_perf_mode_flag();
8030#else
Deeraj Somanfa377bf2019-02-06 12:57:59 +05308031 out->compr_config.codec->flags |= audio_extn_utils_get_perf_mode_flag();
Meng Wang4c32fb42020-01-16 17:57:11 +08008032#endif
Deeraj Somanfa377bf2019-02-06 12:57:59 +05308033 }
8034
vivek mehta446c3962015-09-14 10:57:35 -07008035 if (out->usecase == USECASE_INVALID) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08008036 if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_AUX_DIGITAL) &&
Mingming Yin3a941d42016-02-17 18:08:05 -08008037 config->format == 0 && config->sample_rate == 0 &&
8038 config->channel_mask == 0) {
Mingming Yin21854652016-04-13 11:54:02 -07008039 ALOGI("%s dummy open to query sink capability",__func__);
Mingming Yin3a941d42016-02-17 18:08:05 -08008040 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
8041 } else {
8042 ALOGE("%s, Max allowed OFFLOAD usecase reached ... ", __func__);
8043 ret = -EEXIST;
8044 goto error_open;
8045 }
vivek mehta446c3962015-09-14 10:57:35 -07008046 }
8047
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008048 if (config->offload_info.channel_mask)
8049 out->channel_mask = config->offload_info.channel_mask;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08008050 else if (config->channel_mask) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008051 out->channel_mask = config->channel_mask;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08008052 config->offload_info.channel_mask = config->channel_mask;
Haynes Mathew Georgea99f7532016-08-24 16:01:21 -07008053 } else {
Dhananjay Kumarac341582017-02-23 23:42:25 +05308054 ALOGE("out->channel_mask not set for OFFLOAD/DIRECT usecase");
Haynes Mathew Georgea99f7532016-08-24 16:01:21 -07008055 ret = -EINVAL;
8056 goto error_open;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08008057 }
Haynes Mathew Georgea99f7532016-08-24 16:01:21 -07008058
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07008059 format = out->format = config->offload_info.format;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008060 out->sample_rate = config->offload_info.sample_rate;
8061
Mingming Yin3ee55c62014-08-04 14:23:35 -07008062 out->bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008063
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05308064 out->compr_config.codec->id = get_snd_codec_id(config->offload_info.format);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05308065 if (audio_extn_utils_is_dolby_format(config->offload_info.format)) {
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05308066 audio_extn_dolby_send_ddp_endp_params(adev);
8067 audio_extn_dolby_set_dmid(adev);
8068 }
vivek mehta0ea887a2015-08-26 14:01:20 -07008069
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008070 out->compr_config.codec->sample_rate =
Ravi Kumar Alamandab91bff32014-11-14 12:05:54 -08008071 config->offload_info.sample_rate;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008072 out->compr_config.codec->bit_rate =
8073 config->offload_info.bit_rate;
8074 out->compr_config.codec->ch_in =
Dhanalakshmi Siddania15c6792016-08-10 15:33:53 +05308075 audio_channel_count_from_out_mask(out->channel_mask);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008076 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
Satish Babu Patakokilaa395a9e2016-11-01 12:18:49 +05308077 /* Update bit width only for non passthrough usecases.
8078 * For passthrough usecases, the output will always be opened @16 bit
8079 */
8080 if (!audio_extn_passthru_is_passthrough_stream(out))
8081 out->bit_width = AUDIO_OUTPUT_BIT_WIDTH;
Naresh Tanniruee3499a2017-01-05 14:05:35 +05308082
8083 if (out->flags & AUDIO_OUTPUT_FLAG_TIMESTAMP)
Meng Wang4c32fb42020-01-16 17:57:11 +08008084#ifdef AUDIO_GKI_ENABLED
8085 /* out->compr_config.codec->reserved[1] is for flags */
8086 out->compr_config.codec->reserved[1] |= COMPRESSED_TIMESTAMP_FLAG;
8087 ALOGVV("%s : out->compr_config.codec->flags -> (%#x) ", __func__, out->compr_config.codec->reserved[1]);
8088#else
Naresh Tanniruee3499a2017-01-05 14:05:35 +05308089 out->compr_config.codec->flags |= COMPRESSED_TIMESTAMP_FLAG;
8090 ALOGVV("%s : out->compr_config.codec->flags -> (%#x) ", __func__, out->compr_config.codec->flags);
Meng Wang4c32fb42020-01-16 17:57:11 +08008091#endif
Naresh Tanniruee3499a2017-01-05 14:05:35 +05308092
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07008093 /*TODO: Do we need to change it for passthrough */
8094 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_RAW;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008095
Manish Dewangana6fc5442015-08-24 20:30:31 +05308096 if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC)
8097 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_RAW;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05308098 else if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS)
Manish Dewangana6fc5442015-08-24 20:30:31 +05308099 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_MP4ADTS;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05308100 else if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_LATM)
8101 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_MP4LATM;
Ashish Jainf1eaa582016-05-23 20:54:24 +05308102
8103 if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) ==
8104 AUDIO_FORMAT_PCM) {
8105
8106 /*Based on platform support, configure appropriate alsa format for corresponding
8107 *hal input format.
8108 */
8109 out->compr_config.codec->format = hal_format_to_alsa(
8110 config->offload_info.format);
8111
Ashish Jain83a6cc22016-06-28 14:34:17 +05308112 out->hal_op_format = alsa_format_to_hal(
Ashish Jainf1eaa582016-05-23 20:54:24 +05308113 out->compr_config.codec->format);
Ashish Jain83a6cc22016-06-28 14:34:17 +05308114 out->hal_ip_format = out->format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05308115
Dhananjay Kumarac341582017-02-23 23:42:25 +05308116 /*for direct non-compress playback populate bit_width based on selected alsa format as
Ashish Jainf1eaa582016-05-23 20:54:24 +05308117 *hal input format and alsa format might differ based on platform support.
8118 */
8119 out->bit_width = audio_bytes_per_sample(
Ashish Jain83a6cc22016-06-28 14:34:17 +05308120 out->hal_op_format) << 3;
Ashish Jainf1eaa582016-05-23 20:54:24 +05308121
8122 out->compr_config.fragments = DIRECT_PCM_NUM_FRAGMENTS;
8123
Deeraj Soman93155a62019-09-30 19:00:37 +05308124 if (property_get_bool("vendor.audio.offload.buffer.duration.enabled", false)) {
8125 if ((config->offload_info.duration_us >= MIN_OFFLOAD_BUFFER_DURATION_MS * 1000) &&
8126 (config->offload_info.duration_us <= MAX_OFFLOAD_BUFFER_DURATION_MS * 1000))
8127 out->info.duration_us = (int64_t)config->offload_info.duration_us;
8128 }
Deeraj Soman65358ab2019-02-07 15:40:49 +05308129
Ashish Jainf1eaa582016-05-23 20:54:24 +05308130 /* Check if alsa session is configured with the same format as HAL input format,
8131 * if not then derive correct fragment size needed to accomodate the
8132 * conversion of HAL input format to alsa format.
8133 */
8134 audio_extn_utils_update_direct_pcm_fragment_size(out);
8135
8136 /*if hal input and output fragment size is different this indicates HAL input format is
8137 *not same as the alsa format
8138 */
Ashish Jain83a6cc22016-06-28 14:34:17 +05308139 if (out->hal_fragment_size != out->compr_config.fragment_size) {
Ashish Jainf1eaa582016-05-23 20:54:24 +05308140 /*Allocate a buffer to convert input data to the alsa configured format.
8141 *size of convert buffer is equal to the size required to hold one fragment size
8142 *worth of pcm data, this is because flinger does not write more than fragment_size
8143 */
Ashish Jain83a6cc22016-06-28 14:34:17 +05308144 out->convert_buffer = calloc(1,out->compr_config.fragment_size);
8145 if (out->convert_buffer == NULL){
Ashish Jainf1eaa582016-05-23 20:54:24 +05308146 ALOGE("Allocation failed for convert buffer for size %d", out->compr_config.fragment_size);
8147 ret = -ENOMEM;
8148 goto error_open;
8149 }
8150 }
8151 } else if (audio_extn_passthru_is_passthrough_stream(out)) {
8152 out->compr_config.fragment_size =
8153 audio_extn_passthru_get_buffer_size(&config->offload_info);
8154 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
8155 } else {
8156 out->compr_config.fragment_size =
8157 platform_get_compress_offload_buffer_size(&config->offload_info);
8158 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
8159 }
Mingming Yin3ee55c62014-08-04 14:23:35 -07008160
Naresh Tanniruee3499a2017-01-05 14:05:35 +05308161 if (out->flags & AUDIO_OUTPUT_FLAG_TIMESTAMP) {
8162 out->compr_config.fragment_size += sizeof(struct snd_codec_metadata);
8163 }
Xiaojun Sang782e5b12020-06-29 21:13:06 +08008164 if (config->offload_info.format == AUDIO_FORMAT_FLAC) {
8165#ifdef AUDIO_GKI_ENABLED
8166 generic_dec =
8167 &(out->compr_config.codec->options.generic.reserved[1]);
8168 ((struct snd_generic_dec_flac *)generic_dec)->sample_size =
8169 AUDIO_OUTPUT_BIT_WIDTH;
8170#else
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +05308171 out->compr_config.codec->options.flac_dec.sample_size = AUDIO_OUTPUT_BIT_WIDTH;
Xiaojun Sang782e5b12020-06-29 21:13:06 +08008172#endif
8173 }
Mingming Yin3ee55c62014-08-04 14:23:35 -07008174
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05308175 if (config->offload_info.format == AUDIO_FORMAT_APTX) {
8176 audio_extn_send_aptx_dec_bt_addr_to_dsp(out);
8177 }
8178
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008179 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
8180 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07008181
Manish Dewangan69426c82017-01-30 17:35:36 +05308182 if ((flags & AUDIO_OUTPUT_FLAG_TIMESTAMP) &&
8183 (flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC)) {
8184 out->render_mode = RENDER_MODE_AUDIO_STC_MASTER;
8185 } else if(flags & AUDIO_OUTPUT_FLAG_TIMESTAMP) {
8186 out->render_mode = RENDER_MODE_AUDIO_MASTER;
8187 } else {
8188 out->render_mode = RENDER_MODE_AUDIO_NO_TIMESTAMP;
8189 }
Alexy Josephaa54c872014-12-03 02:46:47 -08008190
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05308191 memset(&out->channel_map_param, 0,
8192 sizeof(struct audio_out_channel_map_param));
8193
Haynes Mathew George352f27b2013-07-26 00:00:15 -07008194 out->send_new_metadata = 1;
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05308195 out->send_next_track_params = false;
8196 out->is_compr_metadata_avail = false;
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08008197 out->offload_state = OFFLOAD_STATE_IDLE;
8198 out->playback_started = 0;
Zhou Song48453a02018-01-10 17:50:59 +08008199 out->writeAt.tv_sec = 0;
8200 out->writeAt.tv_nsec = 0;
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08008201
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08008202 audio_extn_dts_create_state_notifier_node(out->usecase);
8203
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008204 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
8205 __func__, config->offload_info.version,
8206 config->offload_info.bit_rate);
Ashish Jain5106d362016-05-11 19:23:33 +05308207
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05308208 /* Check if DSD audio format is supported in codec
8209 * and there is no active native DSD use case
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05308210 */
8211
8212 if ((config->format == AUDIO_FORMAT_DSD) &&
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05308213 (!platform_check_codec_dsd_support(adev->platform) ||
8214 audio_is_dsd_native_stream_active(adev))) {
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05308215 ret = -EINVAL;
8216 goto error_open;
8217 }
8218
Ashish Jain5106d362016-05-11 19:23:33 +05308219 /* Disable gapless if any of the following is true
8220 * passthrough playback
8221 * AV playback
Dhananjay Kumarac341582017-02-23 23:42:25 +05308222 * non compressed Direct playback
Ashish Jain5106d362016-05-11 19:23:33 +05308223 */
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05308224 if (audio_extn_passthru_is_passthrough_stream(out) ||
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05308225 (config->format == AUDIO_FORMAT_DSD) ||
Naresh Tanniru928f0862017-04-07 16:44:23 -07008226 (config->format == AUDIO_FORMAT_IEC61937) ||
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05308227 config->offload_info.has_video ||
Dhananjay Kumarac341582017-02-23 23:42:25 +05308228 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Ashish Jain5106d362016-05-11 19:23:33 +05308229 check_and_set_gapless_mode(adev, false);
8230 } else
8231 check_and_set_gapless_mode(adev, true);
Mingming Yin21854652016-04-13 11:54:02 -07008232
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05308233 if (audio_extn_passthru_is_passthrough_stream(out)) {
Mingming Yin21854652016-04-13 11:54:02 -07008234 out->flags |= AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH;
8235 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05308236 if (config->format == AUDIO_FORMAT_DSD) {
8237 out->flags |= AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH;
Meng Wang4c32fb42020-01-16 17:57:11 +08008238#ifdef AUDIO_GKI_ENABLED
8239 /* out->compr_config.codec->reserved[0] is for compr_passthr */
8240 out->compr_config.codec->reserved[0] = PASSTHROUGH_DSD;
8241#else
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05308242 out->compr_config.codec->compr_passthr = PASSTHROUGH_DSD;
Meng Wang4c32fb42020-01-16 17:57:11 +08008243#endif
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05308244 }
Aalique Grahame0359a1f2016-09-08 16:54:22 -07008245
8246 create_offload_callback_thread(out);
8247
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07008248 } else if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
Arun Mirpuri7da752a2018-09-11 18:01:15 -07008249 switch (config->sample_rate) {
8250 case 0:
8251 out->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
8252 break;
8253 case 8000:
8254 case 16000:
8255 case 48000:
8256 out->sample_rate = config->sample_rate;
8257 break;
8258 default:
8259 ALOGE("%s: Unsupported sampling rate %d for Incall Music", __func__,
8260 config->sample_rate);
8261 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
8262 ret = -EINVAL;
8263 goto error_open;
8264 }
8265 //FIXME: add support for MONO stream configuration when audioflinger mixer supports it
8266 switch (config->channel_mask) {
8267 case AUDIO_CHANNEL_NONE:
8268 case AUDIO_CHANNEL_OUT_STEREO:
8269 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
8270 break;
8271 default:
8272 ALOGE("%s: Unsupported channel mask %#x for Incall Music", __func__,
8273 config->channel_mask);
8274 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
8275 ret = -EINVAL;
8276 goto error_open;
8277 }
8278 switch (config->format) {
8279 case AUDIO_FORMAT_DEFAULT:
8280 case AUDIO_FORMAT_PCM_16_BIT:
8281 out->format = AUDIO_FORMAT_PCM_16_BIT;
8282 break;
8283 default:
8284 ALOGE("%s: Unsupported format %#x for Incall Music", __func__,
8285 config->format);
8286 config->format = AUDIO_FORMAT_PCM_16_BIT;
8287 ret = -EINVAL;
8288 goto error_open;
8289 }
8290
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05308291 ret = voice_extn_check_and_set_incall_music_usecase(adev, out);
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07008292 if (ret != 0) {
8293 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
Arun Mirpuri7da752a2018-09-11 18:01:15 -07008294 __func__, ret);
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07008295 goto error_open;
8296 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08008297 } else if (is_single_device_type_equal(&out->device_list,
8298 AUDIO_DEVICE_OUT_TELEPHONY_TX)) {
Aalique Grahame22e49102018-12-18 14:23:57 -08008299 switch (config->sample_rate) {
8300 case 0:
8301 out->sample_rate = AFE_PROXY_SAMPLING_RATE;
8302 break;
8303 case 8000:
8304 case 16000:
8305 case 48000:
8306 out->sample_rate = config->sample_rate;
8307 break;
8308 default:
8309 ALOGE("%s: Unsupported sampling rate %d for Telephony TX", __func__,
8310 config->sample_rate);
8311 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
8312 ret = -EINVAL;
8313 break;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07008314 }
Aalique Grahame22e49102018-12-18 14:23:57 -08008315 //FIXME: add support for MONO stream configuration when audioflinger mixer supports it
8316 switch (config->channel_mask) {
8317 case AUDIO_CHANNEL_NONE:
8318 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
8319 break;
8320 case AUDIO_CHANNEL_OUT_STEREO:
8321 out->channel_mask = config->channel_mask;
8322 break;
8323 default:
8324 ALOGE("%s: Unsupported channel mask %#x for Telephony TX", __func__,
8325 config->channel_mask);
8326 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
8327 ret = -EINVAL;
8328 break;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07008329 }
Aalique Grahame22e49102018-12-18 14:23:57 -08008330 switch (config->format) {
8331 case AUDIO_FORMAT_DEFAULT:
8332 out->format = AUDIO_FORMAT_PCM_16_BIT;
8333 break;
8334 case AUDIO_FORMAT_PCM_16_BIT:
8335 out->format = config->format;
8336 break;
8337 default:
8338 ALOGE("%s: Unsupported format %#x for Telephony TX", __func__,
8339 config->format);
8340 config->format = AUDIO_FORMAT_PCM_16_BIT;
8341 ret = -EINVAL;
8342 break;
8343 }
8344 if (ret != 0)
8345 goto error_open;
8346
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07008347 out->usecase = USECASE_AUDIO_PLAYBACK_AFE_PROXY;
8348 out->config = pcm_config_afe_proxy_playback;
Aalique Grahame22e49102018-12-18 14:23:57 -08008349 out->config.rate = out->sample_rate;
8350 out->config.channels =
8351 audio_channel_count_from_out_mask(out->channel_mask);
8352 out->config.format = pcm_format_from_audio_format(out->format);
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07008353 adev->voice_tx_output = out;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07008354 } else {
Ashish Jain058165c2016-09-28 23:18:48 +05308355 unsigned int channels = 0;
8356 /*Update config params to default if not set by the caller*/
8357 if (config->sample_rate == 0)
8358 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
8359 if (config->channel_mask == AUDIO_CHANNEL_NONE)
8360 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
8361 if (config->format == AUDIO_FORMAT_DEFAULT)
8362 config->format = AUDIO_FORMAT_PCM_16_BIT;
8363
8364 channels = audio_channel_count_from_out_mask(out->channel_mask);
8365
Varun Balaraje49253e2017-07-06 19:48:56 +05308366 if (out->flags & AUDIO_OUTPUT_FLAG_INTERACTIVE) {
8367 out->usecase = get_interactive_usecase(adev);
8368 out->config = pcm_config_low_latency;
8369 } else if (out->flags & AUDIO_OUTPUT_FLAG_RAW) {
Ashish Jain83a6cc22016-06-28 14:34:17 +05308370 out->usecase = USECASE_AUDIO_PLAYBACK_ULL;
Haynes Mathew George5beddd42016-06-27 18:33:40 -07008371 out->realtime = may_use_noirq_mode(adev, USECASE_AUDIO_PLAYBACK_ULL,
8372 out->flags);
8373 out->config = out->realtime ? pcm_config_rt : pcm_config_low_latency;
Haynes Mathew George16081042017-05-31 17:16:49 -07008374 } else if (out->flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) {
8375 out->usecase = USECASE_AUDIO_PLAYBACK_MMAP;
8376 out->config = pcm_config_mmap_playback;
8377 out->stream.start = out_start;
8378 out->stream.stop = out_stop;
8379 out->stream.create_mmap_buffer = out_create_mmap_buffer;
8380 out->stream.get_mmap_position = out_get_mmap_position;
Ashish Jain83a6cc22016-06-28 14:34:17 +05308381 } else if (out->flags & AUDIO_OUTPUT_FLAG_FAST) {
8382 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
Aniket Kumar Lata932f4872017-11-06 18:29:44 -08008383 out->hal_output_suspend_supported =
8384 property_get_bool("vendor.audio.hal.output.suspend.supported", false);
8385 out->dynamic_pm_qos_config_supported =
8386 property_get_bool("vendor.audio.hal.dynamic.qos.config.supported", false);
8387 if (!out->dynamic_pm_qos_config_supported) {
Alexy Joseph98988832017-01-13 14:56:59 -08008388 ALOGI("%s: dynamic qos voting not enabled for platform", __func__);
8389 } else {
8390 ALOGI("%s: dynamic qos voting enabled for platform", __func__);
8391 //the mixer path will be a string similar to "low-latency-playback resume"
8392 strlcpy(out->pm_qos_mixer_path, use_case_table[out->usecase], MAX_MIXER_PATH_LEN);
8393 strlcat(out->pm_qos_mixer_path,
8394 " resume", MAX_MIXER_PATH_LEN);
8395 ALOGI("%s: created %s pm_qos_mixer_path" , __func__,
8396 out->pm_qos_mixer_path);
8397 }
Ashish Jain83a6cc22016-06-28 14:34:17 +05308398 out->config = pcm_config_low_latency;
8399 } else if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
8400 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
8401 out->config = pcm_config_deep_buffer;
Ashish Jain058165c2016-09-28 23:18:48 +05308402 out->config.period_size = get_output_period_size(config->sample_rate, out->format,
8403 channels, DEEP_BUFFER_OUTPUT_PERIOD_DURATION);
8404 if (out->config.period_size <= 0) {
8405 ALOGE("Invalid configuration period size is not valid");
8406 ret = -EINVAL;
8407 goto error_open;
8408 }
Aalique Grahame22e49102018-12-18 14:23:57 -08008409 } else if (flags & AUDIO_OUTPUT_FLAG_TTS) {
8410 out->usecase = USECASE_AUDIO_PLAYBACK_TTS;
8411 out->config = pcm_config_deep_buffer;
Vignesh Kulothungana6927272019-02-20 15:17:07 -08008412 } else if (config->channel_mask & AUDIO_CHANNEL_HAPTIC_ALL) {
8413 out->usecase = USECASE_AUDIO_PLAYBACK_WITH_HAPTICS;
8414 out->config = pcm_config_haptics_audio;
8415 if (force_haptic_path)
8416 adev->haptics_config = pcm_config_haptics_audio;
8417 else
8418 adev->haptics_config = pcm_config_haptics;
8419
Meng Wangd08ce322020-04-02 08:59:20 +08008420 channels =
Vignesh Kulothungana6927272019-02-20 15:17:07 -08008421 audio_channel_count_from_out_mask(out->channel_mask & ~AUDIO_CHANNEL_HAPTIC_ALL);
8422
8423 if (force_haptic_path) {
8424 out->config.channels = 1;
8425 adev->haptics_config.channels = 1;
8426 } else
8427 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 -08008428 } else if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_BUS)) {
Derek Chenf6318be2017-06-12 17:16:24 -04008429 ret = audio_extn_auto_hal_open_output_stream(out);
8430 if (ret) {
8431 ALOGE("%s: Failed to open output stream for bus device", __func__);
8432 ret = -EINVAL;
8433 goto error_open;
8434 }
Ashish Jain83a6cc22016-06-28 14:34:17 +05308435 } else {
8436 /* primary path is the default path selected if no other outputs are available/suitable */
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008437 out->usecase = GET_USECASE_AUDIO_PLAYBACK_PRIMARY(use_db_as_primary);
8438 out->config = GET_PCM_CONFIG_AUDIO_PLAYBACK_PRIMARY(use_db_as_primary);
Ashish Jain83a6cc22016-06-28 14:34:17 +05308439 }
8440 out->hal_ip_format = format = out->format;
8441 out->config.format = hal_format_to_pcm(out->hal_ip_format);
8442 out->hal_op_format = pcm_format_to_hal(out->config.format);
8443 out->bit_width = format_to_bitwidth_table[out->hal_op_format] << 3;
8444 out->config.rate = config->sample_rate;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07008445 out->sample_rate = out->config.rate;
Ashish Jain058165c2016-09-28 23:18:48 +05308446 out->config.channels = channels;
Ashish Jain83a6cc22016-06-28 14:34:17 +05308447 if (out->hal_ip_format != out->hal_op_format) {
8448 uint32_t buffer_size = out->config.period_size *
8449 format_to_bitwidth_table[out->hal_op_format] *
8450 out->config.channels;
8451 out->convert_buffer = calloc(1, buffer_size);
8452 if (out->convert_buffer == NULL){
8453 ALOGE("Allocation failed for convert buffer for size %d",
8454 out->compr_config.fragment_size);
8455 ret = -ENOMEM;
8456 goto error_open;
8457 }
8458 ALOGD("Convert buffer allocated of size %d", buffer_size);
8459 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008460 }
8461
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08008462 ALOGV("%s devices:%d, format:%x, out->sample_rate:%d,out->bit_width:%d out->format:%d out->flags:%x, flags: %x usecase %d",
8463 __func__, devices, format, out->sample_rate, out->bit_width, out->format, out->flags, flags, out->usecase);
Ashish Jain83a6cc22016-06-28 14:34:17 +05308464
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07008465 /* TODO remove this hardcoding and check why width is zero*/
8466 if (out->bit_width == 0)
8467 out->bit_width = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05308468 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07008469 &adev->streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08008470 &out->device_list, out->flags,
8471 out->hal_op_format, out->sample_rate,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +05308472 out->bit_width, out->channel_mask, out->profile,
Manish Dewangan837dc462015-05-27 10:17:41 +05308473 &out->app_type_cfg);
Aalique Grahame6e763712019-01-31 16:18:17 -08008474 if ((out->usecase == (audio_usecase_t)(GET_USECASE_AUDIO_PLAYBACK_PRIMARY(use_db_as_primary))) ||
Haynes Mathew Georgebf143712013-12-03 13:02:53 -08008475 (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
8476 /* Ensure the default output is not selected twice */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08008477 if(adev->primary_output == NULL)
8478 adev->primary_output = out;
8479 else {
8480 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07008481 ret = -EEXIST;
8482 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08008483 }
8484 }
8485
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008486 /* Check if this usecase is already existing */
8487 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella7ce05352014-04-17 20:00:41 -07008488 if ((get_usecase_from_list(adev, out->usecase) != NULL) &&
8489 (out->usecase != USECASE_COMPRESS_VOIP_CALL)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008490 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008491 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07008492 ret = -EEXIST;
8493 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008494 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08008495
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008496 pthread_mutex_unlock(&adev->lock);
8497
8498 out->stream.common.get_sample_rate = out_get_sample_rate;
8499 out->stream.common.set_sample_rate = out_set_sample_rate;
8500 out->stream.common.get_buffer_size = out_get_buffer_size;
8501 out->stream.common.get_channels = out_get_channels;
8502 out->stream.common.get_format = out_get_format;
8503 out->stream.common.set_format = out_set_format;
8504 out->stream.common.standby = out_standby;
8505 out->stream.common.dump = out_dump;
8506 out->stream.common.set_parameters = out_set_parameters;
8507 out->stream.common.get_parameters = out_get_parameters;
8508 out->stream.common.add_audio_effect = out_add_audio_effect;
8509 out->stream.common.remove_audio_effect = out_remove_audio_effect;
8510 out->stream.get_latency = out_get_latency;
8511 out->stream.set_volume = out_set_volume;
Aalique Grahame22e49102018-12-18 14:23:57 -08008512#ifdef NO_AUDIO_OUT
8513 out->stream.write = out_write_for_no_output;
8514#else
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008515 out->stream.write = out_write;
Aalique Grahame22e49102018-12-18 14:23:57 -08008516#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008517 out->stream.get_render_position = out_get_render_position;
8518 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07008519 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008520
Haynes Mathew George16081042017-05-31 17:16:49 -07008521 if (out->realtime)
8522 out->af_period_multiplier = af_period_multiplier;
8523 else
8524 out->af_period_multiplier = 1;
8525
Andy Hunga1f48fa2019-07-01 18:14:53 -07008526 out->kernel_buffer_size = out->config.period_size * out->config.period_count;
8527
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008528 out->standby = 1;
Zhou Song1f93fa52020-11-20 13:57:39 +08008529 out->volume_l = PLAYBACK_GAIN_MAX;
8530 out->volume_r = PLAYBACK_GAIN_MAX;
Eric Laurenta9024de2013-04-04 09:19:12 -07008531 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07008532 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008533
8534 config->format = out->stream.common.get_format(&out->stream.common);
8535 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
8536 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
Naresh Tanniru04f71882018-06-26 17:46:22 +05308537 register_format(out->format, out->supported_formats);
8538 register_channel_mask(out->channel_mask, out->supported_channel_masks);
8539 register_sample_rate(out->sample_rate, out->supported_sample_rates);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008540
Aalique Grahame22e49102018-12-18 14:23:57 -08008541 out->error_log = error_log_create(
8542 ERROR_LOG_ENTRIES,
8543 1000000000 /* aggregate consecutive identical errors within one second in ns */);
8544
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05308545 /*
8546 By locking output stream before registering, we allow the callback
8547 to update stream's state only after stream's initial state is set to
8548 adev state.
8549 */
8550 lock_output_stream(out);
8551 audio_extn_snd_mon_register_listener(out, out_snd_mon_cb);
8552 pthread_mutex_lock(&adev->lock);
8553 out->card_status = adev->card_status;
8554 pthread_mutex_unlock(&adev->lock);
8555 pthread_mutex_unlock(&out->lock);
8556
Aalique Grahame22e49102018-12-18 14:23:57 -08008557 stream_app_type_cfg_init(&out->app_type_cfg);
8558
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008559 *stream_out = &out->stream;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05308560 ALOGD("%s: Stream (%p) picks up usecase (%s)", __func__, &out->stream,
vivek mehta0ea887a2015-08-26 14:01:20 -07008561 use_case_table[out->usecase]);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08008562
8563 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
8564 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
8565 popcount(out->channel_mask), out->playback_started);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08008566 /* setup a channel for client <--> adsp communication for stream events */
Manish Dewangan21a850a2017-08-14 12:03:55 +05308567 is_direct_passthough = audio_extn_passthru_is_direct_passthrough(out);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08008568 if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
Naresh Tanniru85819452017-05-04 18:55:45 -07008569 (out->flags & AUDIO_OUTPUT_FLAG_DIRECT_PCM) ||
Gangadhar Sb0210342019-02-22 17:39:41 +05308570 audio_extn_ip_hdlr_intf_supported_for_copp(adev->platform) ||
8571 (audio_extn_ip_hdlr_intf_supported(config->format, is_direct_passthough, false))) {
Ben Rombergerd771a7c2017-02-22 18:05:17 -08008572 hdlr_stream_cfg.pcm_device_id = platform_get_pcm_device_id(
8573 out->usecase, PCM_PLAYBACK);
8574 hdlr_stream_cfg.flags = out->flags;
8575 hdlr_stream_cfg.type = PCM_PLAYBACK;
8576 ret = audio_extn_adsp_hdlr_stream_open(&out->adsp_hdlr_stream_handle,
8577 &hdlr_stream_cfg);
8578 if (ret) {
8579 ALOGE("%s: adsp_hdlr_stream_open failed %d",__func__, ret);
8580 out->adsp_hdlr_stream_handle = NULL;
8581 }
8582 }
Gangadhar Sb0210342019-02-22 17:39:41 +05308583 ip_hdlr_stream = audio_extn_ip_hdlr_intf_supported(config->format,
8584 is_direct_passthough, false);
8585 ip_hdlr_dev = audio_extn_ip_hdlr_intf_supported_for_copp(adev->platform);
8586 if (ip_hdlr_stream || ip_hdlr_dev ) {
Vidyakumar Athota2062f912017-06-27 14:46:15 -07008587 ret = audio_extn_ip_hdlr_intf_init(&out->ip_hdlr_handle, NULL, NULL, adev, out->usecase);
Naresh Tanniru85819452017-05-04 18:55:45 -07008588 if (ret < 0) {
8589 ALOGE("%s: audio_extn_ip_hdlr_intf_init failed %d",__func__, ret);
8590 out->ip_hdlr_handle = NULL;
8591 }
8592 }
Derek Chenf939fb72018-11-13 13:34:41 -08008593
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07008594 ret = io_streams_map_insert(adev, &out->stream.common,
8595 out->handle, AUDIO_PATCH_HANDLE_NONE);
8596 if (ret != 0)
8597 goto error_open;
8598
Derek Chenf939fb72018-11-13 13:34:41 -08008599 streams_output_ctxt_t *out_ctxt = (streams_output_ctxt_t *)
8600 calloc(1, sizeof(streams_output_ctxt_t));
8601 if (out_ctxt == NULL) {
8602 ALOGE("%s fail to allocate output ctxt", __func__);
8603 ret = -ENOMEM;
8604 goto error_open;
8605 }
8606 out_ctxt->output = out;
8607
8608 pthread_mutex_lock(&adev->lock);
8609 list_add_tail(&adev->active_outputs_list, &out_ctxt->list);
8610 pthread_mutex_unlock(&adev->lock);
8611
Eric Laurent994a6932013-07-17 11:51:42 -07008612 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008613 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07008614
8615error_open:
Ashish Jain83a6cc22016-06-28 14:34:17 +05308616 if (out->convert_buffer)
8617 free(out->convert_buffer);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07008618 free(out);
8619 *stream_out = NULL;
8620 ALOGD("%s: exit: ret %d", __func__, ret);
8621 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008622}
8623
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05308624void adev_close_output_stream(struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008625 struct audio_stream_out *stream)
8626{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008627 struct stream_out *out = (struct stream_out *)stream;
8628 struct audio_device *adev = out->dev;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08008629 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008630
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008631 ALOGD("%s: enter:stream_handle(%s)",__func__, use_case_table[out->usecase]);
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05308632
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07008633 io_streams_map_remove(adev, out->handle);
8634
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05308635 // must deregister from sndmonitor first to prevent races
8636 // between the callback and close_stream
8637 audio_extn_snd_mon_unregister_listener(out);
8638
Ben Rombergerd771a7c2017-02-22 18:05:17 -08008639 /* close adsp hdrl session before standby */
8640 if (out->adsp_hdlr_stream_handle) {
8641 ret = audio_extn_adsp_hdlr_stream_close(out->adsp_hdlr_stream_handle);
8642 if (ret)
8643 ALOGE("%s: adsp_hdlr_stream_close failed %d",__func__, ret);
8644 out->adsp_hdlr_stream_handle = NULL;
8645 }
8646
Manish Dewangan21a850a2017-08-14 12:03:55 +05308647 if (out->ip_hdlr_handle) {
Naresh Tanniru85819452017-05-04 18:55:45 -07008648 audio_extn_ip_hdlr_intf_deinit(out->ip_hdlr_handle);
8649 out->ip_hdlr_handle = NULL;
8650 }
8651
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08008652 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05308653 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08008654 ret = voice_extn_compress_voip_close_output_stream(&stream->common);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05308655 out->started = 0;
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05308656 pthread_mutex_unlock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08008657 if(ret != 0)
8658 ALOGE("%s: Compress voip output cannot be closed, error:%d",
8659 __func__, ret);
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07008660 } else
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08008661 out_standby(&stream->common);
8662
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07008663 if (is_offload_usecase(out->usecase)) {
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08008664 audio_extn_dts_remove_state_notifier_node(out->usecase);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008665 destroy_offload_callback_thread(out);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07008666 free_offload_usecase(adev, out->usecase);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008667 if (out->compr_config.codec != NULL)
8668 free(out->compr_config.codec);
8669 }
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07008670
Zhou Song1f93fa52020-11-20 13:57:39 +08008671 out->a2dp_muted = false;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05308672
Varun Balaraje49253e2017-07-06 19:48:56 +05308673 if (is_interactive_usecase(out->usecase))
8674 free_interactive_usecase(adev, out->usecase);
8675
Ashish Jain83a6cc22016-06-28 14:34:17 +05308676 if (out->convert_buffer != NULL) {
8677 free(out->convert_buffer);
8678 out->convert_buffer = NULL;
8679 }
8680
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07008681 if (adev->voice_tx_output == out)
8682 adev->voice_tx_output = NULL;
8683
Aalique Grahame22e49102018-12-18 14:23:57 -08008684 error_log_destroy(out->error_log);
8685 out->error_log = NULL;
8686
Dhanalakshmi Siddani6c3d0992017-01-16 16:52:33 +05308687 if (adev->primary_output == out)
8688 adev->primary_output = NULL;
8689
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008690 pthread_cond_destroy(&out->cond);
8691 pthread_mutex_destroy(&out->lock);
Weiyin Jiangd5974e62020-09-08 20:28:22 +08008692 pthread_mutex_destroy(&out->pre_lock);
8693 pthread_mutex_destroy(&out->latch_lock);
8694 pthread_mutex_destroy(&out->position_query_lock);
Derek Chenf939fb72018-11-13 13:34:41 -08008695
8696 pthread_mutex_lock(&adev->lock);
8697 streams_output_ctxt_t *out_ctxt = out_get_stream(adev, out->handle);
8698 if (out_ctxt != NULL) {
8699 list_remove(&out_ctxt->list);
8700 free(out_ctxt);
8701 } else {
8702 ALOGW("%s, output stream already closed", __func__);
8703 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008704 free(stream);
Derek Chenf939fb72018-11-13 13:34:41 -08008705 pthread_mutex_unlock(&adev->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07008706 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008707}
8708
8709static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
8710{
8711 struct audio_device *adev = (struct audio_device *)dev;
8712 struct str_parms *parms;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008713 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07008714 int val;
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07008715 int ret;
8716 int status = 0;
Aalique Grahame22e49102018-12-18 14:23:57 -08008717 bool a2dp_reconfig = false;
Zhou Songd6d71752019-05-21 18:08:51 +08008718 struct listnode *node;
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07008719 int controller = -1, stream = -1;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008720
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08008721 ALOGD("%s: enter: %s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008722 parms = str_parms_create_str(kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008723
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05308724 if (!parms)
8725 goto error;
Naresh Tanniru4c630392014-05-12 01:05:52 +05308726
Derek Chen6f293672019-04-01 01:40:24 -07008727 /* notify adev and input/output streams on the snd card status */
8728 adev_snd_mon_cb((void *)adev, parms);
8729
Weiyin Jiangd4e8ced2020-12-22 14:35:46 +08008730 ret = str_parms_get_str(parms, "SND_CARD_STATUS", value, sizeof(value));
8731 if (ret >= 0) {
8732 list_for_each(node, &adev->active_outputs_list) {
8733 streams_output_ctxt_t *out_ctxt = node_to_item(node,
8734 streams_output_ctxt_t,
8735 list);
8736 out_snd_mon_cb((void *)out_ctxt->output, parms);
8737 }
Derek Chen6f293672019-04-01 01:40:24 -07008738
Weiyin Jiangd4e8ced2020-12-22 14:35:46 +08008739 list_for_each(node, &adev->active_inputs_list) {
8740 streams_input_ctxt_t *in_ctxt = node_to_item(node,
8741 streams_input_ctxt_t,
8742 list);
8743 in_snd_mon_cb((void *)in_ctxt->input, parms);
8744 }
Derek Chen6f293672019-04-01 01:40:24 -07008745 }
8746
Zhou Songd6d71752019-05-21 18:08:51 +08008747 pthread_mutex_lock(&adev->lock);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05308748 ret = str_parms_get_str(parms, "BT_SCO", value, sizeof(value));
8749 if (ret >= 0) {
8750 /* When set to false, HAL should disable EC and NS */
Zhou Songd6d71752019-05-21 18:08:51 +08008751 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0){
Ashish Jain1b9b30c2017-05-18 20:57:40 +05308752 adev->bt_sco_on = true;
Shalini Manjunathac8cd3e92021-02-01 12:23:49 +05308753 /*
8754 * When ever BT_SCO=ON arrives, make sure to route
8755 * all use cases to SCO device, otherwise due to delay in
8756 * BT_SCO=ON and lack of synchronization with create audio patch
8757 * request for SCO device, some times use case not routed properly to
8758 * SCO device
8759 */
8760 struct audio_usecase *usecase;
8761 struct listnode *node;
8762 list_for_each(node, &adev->usecase_list) {
8763 usecase = node_to_item(node, struct audio_usecase, list);
Mingshu Pang651036e2021-04-22 10:35:00 +08008764 if (usecase->stream.in && (usecase->type == PCM_CAPTURE ||
8765 usecase->type == VOICE_CALL) &&
Shalini Manjunathac8cd3e92021-02-01 12:23:49 +05308766 (!is_btsco_device(SND_DEVICE_NONE, usecase->in_snd_device))) {
8767 ALOGD("BT_SCO ON, switch all in use case to it");
8768 select_devices(adev, usecase->id);
8769 }
Mingshu Pang651036e2021-04-22 10:35:00 +08008770 if (usecase->stream.out && (usecase->type == PCM_PLAYBACK ||
8771 usecase->type == VOICE_CALL) &&
Shalini Manjunathac8cd3e92021-02-01 12:23:49 +05308772 (!is_btsco_device(usecase->out_snd_device, SND_DEVICE_NONE))) {
8773 ALOGD("BT_SCO ON, switch all out use case to it");
8774 select_devices(adev, usecase->id);
8775 }
8776 }
8777 }
8778 else {
Ashish Jain1b9b30c2017-05-18 20:57:40 +05308779 adev->bt_sco_on = false;
Zhou Songd6d71752019-05-21 18:08:51 +08008780 audio_extn_sco_reset_configuration();
Kunlei Zhangd96af8f2019-08-27 16:33:29 +08008781 }
8782 }
8783
8784 ret = str_parms_get_str(parms, "A2dpSuspended", value, sizeof(value));
Weiyin Jiangd5974e62020-09-08 20:28:22 +08008785 if (ret >= 0) {
Kunlei Zhangd96af8f2019-08-27 16:33:29 +08008786 if (!strncmp(value, "false", 5) &&
8787 audio_extn_a2dp_source_is_suspended()) {
8788 struct audio_usecase *usecase;
8789 struct listnode *node;
Zhou Songd6d71752019-05-21 18:08:51 +08008790 list_for_each(node, &adev->usecase_list) {
8791 usecase = node_to_item(node, struct audio_usecase, list);
Kunlei Zhangd96af8f2019-08-27 16:33:29 +08008792 if (usecase->stream.in && (usecase->type == PCM_CAPTURE) &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08008793 is_sco_in_device_type(&usecase->stream.in->device_list)) {
Kunlei Zhangd96af8f2019-08-27 16:33:29 +08008794 ALOGD("a2dp resumed, switch bt sco mic to handset mic");
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08008795 reassign_device_list(&usecase->stream.in->device_list,
8796 AUDIO_DEVICE_IN_BUILTIN_MIC, "");
Kunlei Zhangd96af8f2019-08-27 16:33:29 +08008797 select_devices(adev, usecase->id);
8798 }
Zhou Songd6d71752019-05-21 18:08:51 +08008799 }
8800 }
Ashish Jain1b9b30c2017-05-18 20:57:40 +05308801 }
8802
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07008803 status = voice_set_parameters(adev, parms);
8804 if (status != 0)
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08008805 goto done;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008806
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07008807 status = platform_set_parameters(adev->platform, parms);
8808 if (status != 0)
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08008809 goto done;
8810
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07008811 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
8812 if (ret >= 0) {
Vicky Sehrawate240e5d2014-08-12 17:17:04 -07008813 /* When set to false, HAL should disable EC and NS */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008814 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
8815 adev->bluetooth_nrec = true;
8816 else
8817 adev->bluetooth_nrec = false;
8818 }
8819
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07008820 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
8821 if (ret >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008822 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
8823 adev->screen_off = false;
8824 else
8825 adev->screen_off = true;
Quinn Male70f20f32019-06-26 16:50:26 -07008826 audio_extn_sound_trigger_update_screen_status(adev->screen_off);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008827 }
8828
Eric Laurent4b084132018-10-19 17:33:43 -07008829 ret = str_parms_get_int(parms, "rotation", &val);
8830 if (ret >= 0) {
8831 bool reverse_speakers = false;
8832 int camera_rotation = CAMERA_ROTATION_LANDSCAPE;
8833 switch (val) {
8834 // FIXME: note that the code below assumes that the speakers are in the correct placement
8835 // relative to the user when the device is rotated 90deg from its default rotation. This
8836 // assumption is device-specific, not platform-specific like this code.
8837 case 270:
8838 reverse_speakers = true;
8839 camera_rotation = CAMERA_ROTATION_INVERT_LANDSCAPE;
8840 break;
8841 case 0:
8842 case 180:
8843 camera_rotation = CAMERA_ROTATION_PORTRAIT;
8844 break;
8845 case 90:
8846 camera_rotation = CAMERA_ROTATION_LANDSCAPE;
8847 break;
8848 default:
8849 ALOGE("%s: unexpected rotation of %d", __func__, val);
8850 status = -EINVAL;
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07008851 }
Eric Laurent4b084132018-10-19 17:33:43 -07008852 if (status == 0) {
8853 // check and set swap
8854 // - check if orientation changed and speaker active
8855 // - set rotation and cache the rotation value
8856 adev->camera_orientation =
8857 (adev->camera_orientation & ~CAMERA_ROTATION_MASK) | camera_rotation;
8858 if (!audio_extn_is_maxx_audio_enabled())
8859 platform_check_and_set_swap_lr_channels(adev, reverse_speakers);
8860 }
8861 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07008862
Mingming Yin514a8bc2014-07-29 15:22:21 -07008863 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_SCO_WB, value, sizeof(value));
8864 if (ret >= 0) {
8865 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
8866 adev->bt_wb_speech_enabled = true;
8867 else
8868 adev->bt_wb_speech_enabled = false;
8869 }
8870
Zhou Song12c29502019-03-16 10:37:18 +08008871 ret = str_parms_get_str(parms, "bt_swb", value, sizeof(value));
8872 if (ret >= 0) {
8873 val = atoi(value);
8874 adev->swb_speech_mode = val;
8875 }
8876
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -07008877 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value, sizeof(value));
8878 if (ret >= 0) {
8879 val = atoi(value);
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05308880 audio_devices_t device = (audio_devices_t) val;
Zhou Song681350a2017-10-19 16:28:42 +08008881 if (audio_is_output_device(val) &&
8882 (val & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07008883 ALOGV("cache new ext disp type and edid");
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07008884 platform_get_controller_stream_from_params(parms, &controller, &stream);
8885 platform_set_ext_display_device_v2(adev->platform, controller, stream);
8886 ret = platform_get_ext_disp_type_v2(adev->platform, controller, stream);
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07008887 if (ret < 0) {
8888 ALOGE("%s: Failed to query disp type, ret:%d", __func__, ret);
Manisha Agarwal2f5ff882018-08-08 17:09:29 +05308889 } else {
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07008890 platform_cache_edid_v2(adev->platform, controller, stream);
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07008891 }
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05308892 } else if (audio_is_usb_out_device(device) || audio_is_usb_in_device(device)) {
vivek mehta344576a2016-04-12 18:56:03 -07008893 /*
8894 * Do not allow AFE proxy port usage by WFD source when USB headset is connected.
8895 * Per AudioPolicyManager, USB device is higher priority than WFD.
8896 * For Voice call over USB headset, voice call audio is routed to AFE proxy ports.
8897 * If WFD use case occupies AFE proxy, it may result unintended behavior while
8898 * starting voice call on USB
8899 */
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08008900 ret = str_parms_get_str(parms, "card", value, sizeof(value));
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05308901 if (ret >= 0)
8902 audio_extn_usb_add_device(device, atoi(value));
8903
Zhou Song6f862822017-11-06 17:27:57 +08008904 if (!audio_extn_usb_is_tunnel_supported()) {
8905 ALOGV("detected USB connect .. disable proxy");
8906 adev->allow_afe_proxy_usage = false;
8907 }
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -07008908 }
8909 }
8910
8911 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value, sizeof(value));
8912 if (ret >= 0) {
8913 val = atoi(value);
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05308914 audio_devices_t device = (audio_devices_t) val;
Garmond Leunge3b6d482016-10-25 16:48:01 -07008915 /*
8916 * The HDMI / Displayport disconnect handling has been moved to
8917 * audio extension to ensure that its parameters are not
8918 * invalidated prior to updating sysfs of the disconnect event
8919 * Invalidate will be handled by audio_extn_ext_disp_set_parameters()
8920 */
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05308921 if (audio_is_usb_out_device(device) || audio_is_usb_in_device(device)) {
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08008922 ret = str_parms_get_str(parms, "card", value, sizeof(value));
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05308923 if (ret >= 0)
8924 audio_extn_usb_remove_device(device, atoi(value));
8925
Zhou Song6f862822017-11-06 17:27:57 +08008926 if (!audio_extn_usb_is_tunnel_supported()) {
8927 ALOGV("detected USB disconnect .. enable proxy");
8928 adev->allow_afe_proxy_usage = true;
8929 }
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -07008930 }
8931 }
8932
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008933 audio_extn_qdsp_set_parameters(adev, parms);
Aalique Grahame22e49102018-12-18 14:23:57 -08008934
8935 status = audio_extn_a2dp_set_parameters(parms, &a2dp_reconfig);
Aniket Kumar Lata23300322019-02-20 22:25:30 -08008936 if (status >= 0 && a2dp_reconfig) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05308937 struct audio_usecase *usecase;
8938 struct listnode *node;
8939 list_for_each(node, &adev->usecase_list) {
8940 usecase = node_to_item(node, struct audio_usecase, list);
Weiyin Jiangff56ff32020-05-08 14:32:21 +08008941 if ((usecase->stream.out == NULL) || (usecase->type != PCM_PLAYBACK))
8942 continue;
8943
8944 if (is_a2dp_out_device_type(&usecase->device_list)) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05308945 ALOGD("reconfigure a2dp... forcing device switch");
Naresh Tannirucd2353e2016-08-19 00:37:25 +05308946 audio_extn_a2dp_set_handoff_mode(true);
Manisha Agarwalf1c3b6b2019-06-25 13:00:33 +05308947 ALOGD("Switching to speaker and muting the stream before select_devices");
8948 check_a2dp_restore_l(adev, usecase->stream.out, false);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05308949 //force device switch to re configure encoder
8950 select_devices(adev, usecase->id);
Manisha Agarwalf1c3b6b2019-06-25 13:00:33 +05308951 ALOGD("Unmuting the stream after select_devices");
Zhou Song1f93fa52020-11-20 13:57:39 +08008952 check_a2dp_restore_l(adev, usecase->stream.out, true);
Naresh Tannirucd2353e2016-08-19 00:37:25 +05308953 audio_extn_a2dp_set_handoff_mode(false);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05308954 break;
Zhou Song9ebf6792020-09-23 22:49:01 +08008955 } else if (is_offload_usecase(usecase->stream.out->usecase)) {
Weiyin Jiangd5974e62020-09-08 20:28:22 +08008956 pthread_mutex_lock(&usecase->stream.out->latch_lock);
Zhou Song1f93fa52020-11-20 13:57:39 +08008957 if (usecase->stream.out->a2dp_muted) {
Weiyin Jiangd5974e62020-09-08 20:28:22 +08008958 pthread_mutex_unlock(&usecase->stream.out->latch_lock);
8959 reassign_device_list(&usecase->stream.out->device_list,
8960 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP, "");
8961 check_a2dp_restore_l(adev, usecase->stream.out, true);
8962 break;
8963 }
8964 pthread_mutex_unlock(&usecase->stream.out->latch_lock);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05308965 }
8966 }
8967 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08008968
8969 //handle vr audio setparam
8970 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_VR_AUDIO_MODE,
8971 value, sizeof(value));
8972 if (ret >= 0) {
8973 ALOGI("Setting vr mode to be %s", value);
8974 if (!strncmp(value, "true", 4)) {
8975 adev->vr_audio_mode_enabled = true;
8976 ALOGI("Setting vr mode to true");
8977 } else if (!strncmp(value, "false", 5)) {
8978 adev->vr_audio_mode_enabled = false;
8979 ALOGI("Setting vr mode to false");
8980 } else {
8981 ALOGI("wrong vr mode set");
8982 }
8983 }
8984
Eric Laurent4b084132018-10-19 17:33:43 -07008985 //FIXME: to be replaced by proper video capture properties API
8986 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_CAMERA_FACING, value, sizeof(value));
8987 if (ret >= 0) {
8988 int camera_facing = CAMERA_FACING_BACK;
8989 if (strcmp(value, AUDIO_PARAMETER_VALUE_FRONT) == 0)
8990 camera_facing = CAMERA_FACING_FRONT;
8991 else if (strcmp(value, AUDIO_PARAMETER_VALUE_BACK) == 0)
8992 camera_facing = CAMERA_FACING_BACK;
8993 else {
8994 ALOGW("%s: invalid camera facing value: %s", __func__, value);
8995 goto done;
8996 }
8997 adev->camera_orientation =
8998 (adev->camera_orientation & ~CAMERA_FACING_MASK) | camera_facing;
8999 struct audio_usecase *usecase;
9000 struct listnode *node;
9001 list_for_each(node, &adev->usecase_list) {
9002 usecase = node_to_item(node, struct audio_usecase, list);
9003 struct stream_in *in = usecase->stream.in;
9004 if (usecase->type == PCM_CAPTURE && in != NULL &&
9005 in->source == AUDIO_SOURCE_CAMCORDER && !in->standby) {
9006 select_devices(adev, in->usecase);
9007 }
9008 }
9009 }
9010
Naresh Tannirucd2353e2016-08-19 00:37:25 +05309011 audio_extn_set_parameters(adev, parms);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08009012done:
9013 str_parms_destroy(parms);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08009014 pthread_mutex_unlock(&adev->lock);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05309015error:
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07009016 ALOGV("%s: exit with code(%d)", __func__, status);
9017 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009018}
9019
9020static char* adev_get_parameters(const struct audio_hw_device *dev,
9021 const char *keys)
9022{
Sidipotu Ashokaa4fa6a2018-12-21 09:19:26 +05309023 ALOGD("%s:%s", __func__, keys);
9024
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07009025 struct audio_device *adev = (struct audio_device *)dev;
9026 struct str_parms *reply = str_parms_create();
9027 struct str_parms *query = str_parms_create_str(keys);
9028 char *str;
Naresh Tannirud7205b62014-06-20 02:54:48 +05309029 char value[256] = {0};
9030 int ret = 0;
9031
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07009032 if (!query || !reply) {
Alexy Josephaee4fdd2016-01-29 13:02:07 -08009033 if (reply) {
9034 str_parms_destroy(reply);
9035 }
9036 if (query) {
9037 str_parms_destroy(query);
9038 }
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07009039 ALOGE("adev_get_parameters: failed to create query or reply");
9040 return NULL;
9041 }
9042
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08009043 //handle vr audio getparam
9044
9045 ret = str_parms_get_str(query,
9046 AUDIO_PARAMETER_KEY_VR_AUDIO_MODE,
9047 value, sizeof(value));
9048
9049 if (ret >= 0) {
9050 bool vr_audio_enabled = false;
9051 pthread_mutex_lock(&adev->lock);
9052 vr_audio_enabled = adev->vr_audio_mode_enabled;
9053 pthread_mutex_unlock(&adev->lock);
9054
9055 ALOGI("getting vr mode to %d", vr_audio_enabled);
9056
9057 if (vr_audio_enabled) {
9058 str_parms_add_str(reply, AUDIO_PARAMETER_KEY_VR_AUDIO_MODE,
9059 "true");
9060 goto exit;
9061 } else {
9062 str_parms_add_str(reply, AUDIO_PARAMETER_KEY_VR_AUDIO_MODE,
9063 "false");
9064 goto exit;
9065 }
9066 }
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07009067
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08009068 pthread_mutex_lock(&adev->lock);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07009069 audio_extn_get_parameters(adev, query, reply);
Shiv Maliyappanahallif9308492013-12-12 12:18:09 -08009070 voice_get_parameters(adev, query, reply);
Aalique Grahame22e49102018-12-18 14:23:57 -08009071 audio_extn_a2dp_get_parameters(query, reply);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07009072 platform_get_parameters(adev->platform, query, reply);
justinwengd5395152019-11-04 12:23:09 +08009073 audio_extn_ma_get_parameters(adev, query, reply);
Naresh Tanniru80659832014-06-04 18:17:56 +05309074 pthread_mutex_unlock(&adev->lock);
9075
Naresh Tannirud7205b62014-06-20 02:54:48 +05309076exit:
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07009077 str = str_parms_to_str(reply);
9078 str_parms_destroy(query);
9079 str_parms_destroy(reply);
9080
Sidipotu Ashokaa4fa6a2018-12-21 09:19:26 +05309081 ALOGD("%s: exit: returns - %s", __func__, str);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07009082 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009083}
9084
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07009085static int adev_init_check(const struct audio_hw_device *dev __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009086{
9087 return 0;
9088}
9089
9090static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
9091{
Haynes Mathew George5191a852013-09-11 14:19:36 -07009092 int ret;
9093 struct audio_device *adev = (struct audio_device *)dev;
Aalique Grahame22e49102018-12-18 14:23:57 -08009094
9095 audio_extn_extspk_set_voice_vol(adev->extspk, volume);
9096
Haynes Mathew George5191a852013-09-11 14:19:36 -07009097 pthread_mutex_lock(&adev->lock);
9098 /* cache volume */
Shruthi Krishnaace10852013-10-25 14:32:12 -07009099 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07009100 pthread_mutex_unlock(&adev->lock);
9101 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009102}
9103
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07009104static int adev_set_master_volume(struct audio_hw_device *dev __unused,
9105 float volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009106{
9107 return -ENOSYS;
9108}
9109
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07009110static int adev_get_master_volume(struct audio_hw_device *dev __unused,
9111 float *volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009112{
9113 return -ENOSYS;
9114}
9115
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07009116static int adev_set_master_mute(struct audio_hw_device *dev __unused,
9117 bool muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009118{
9119 return -ENOSYS;
9120}
9121
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07009122static int adev_get_master_mute(struct audio_hw_device *dev __unused,
9123 bool *muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009124{
9125 return -ENOSYS;
9126}
9127
9128static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
9129{
9130 struct audio_device *adev = (struct audio_device *)dev;
Garmond Leung5fd0b552018-04-17 11:56:12 -07009131 struct listnode *node;
9132 struct audio_usecase *usecase = NULL;
9133 int ret = 0;
kunleizdc4af9d2017-05-04 12:15:35 +08009134
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009135 pthread_mutex_lock(&adev->lock);
9136 if (adev->mode != mode) {
Jaideep Sharma477917f2020-03-13 18:13:33 +05309137 ALOGD("%s: mode %d , prev_mode %d \n", __func__, mode , adev->mode);
9138 adev->prev_mode = adev->mode; /* prev_mode is kept to handle voip concurrency*/
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009139 adev->mode = mode;
Weiyin Jiangc3a9c812020-09-22 16:48:19 +08009140 if (mode == AUDIO_MODE_CALL_SCREEN) {
Jaideep Sharma477917f2020-03-13 18:13:33 +05309141 adev->current_call_output = adev->primary_output;
9142 voice_start_call(adev);
9143 } else if (voice_is_in_call_or_call_screen(adev) &&
Kunlei Zhang1d5c7f22019-05-21 14:25:57 +08009144 (mode == AUDIO_MODE_NORMAL ||
9145 (mode == AUDIO_MODE_IN_COMMUNICATION && !voice_is_call_state_active(adev)))) {
Garmond Leung5fd0b552018-04-17 11:56:12 -07009146 list_for_each(node, &adev->usecase_list) {
9147 usecase = node_to_item(node, struct audio_usecase, list);
9148 if (usecase->type == VOICE_CALL)
9149 break;
9150 }
9151 if (usecase &&
9152 audio_is_usb_out_device(usecase->out_snd_device & AUDIO_DEVICE_OUT_ALL_USB)) {
9153 ret = audio_extn_usb_check_and_set_svc_int(usecase,
9154 true);
9155 if (ret != 0) {
9156 /* default service interval was successfully updated,
9157 reopen USB backend with new service interval */
9158 check_usecases_codec_backend(adev,
9159 usecase,
9160 usecase->out_snd_device);
9161 }
9162 }
9163
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07009164 voice_stop_call(adev);
Banajit Goswami20cdd212015-09-11 01:11:30 -07009165 platform_set_gsm_mode(adev->platform, false);
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07009166 adev->current_call_output = NULL;
kunleizdc4af9d2017-05-04 12:15:35 +08009167 // restore device for other active usecases after stop call
9168 list_for_each(node, &adev->usecase_list) {
9169 usecase = node_to_item(node, struct audio_usecase, list);
9170 select_devices(adev, usecase->id);
9171 }
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07009172 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009173 }
9174 pthread_mutex_unlock(&adev->lock);
9175 return 0;
9176}
9177
9178static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
9179{
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08009180 int ret;
Aalique Grahame22e49102018-12-18 14:23:57 -08009181 struct audio_device *adev = (struct audio_device *)dev;
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08009182
9183 pthread_mutex_lock(&adev->lock);
Vidyakumar Athota2850d532013-11-19 16:02:12 -08009184 ALOGD("%s state %d\n", __func__, state);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08009185 ret = voice_set_mic_mute((struct audio_device *)dev, state);
Aalique Grahame22e49102018-12-18 14:23:57 -08009186
Derek Chend2530072014-11-24 12:39:14 -08009187 if (adev->ext_hw_plugin)
9188 ret = audio_extn_ext_hw_plugin_set_mic_mute(adev->ext_hw_plugin, state);
Aalique Grahame22e49102018-12-18 14:23:57 -08009189
9190 adev->mic_muted = state;
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08009191 pthread_mutex_unlock(&adev->lock);
9192
9193 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009194}
9195
9196static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
9197{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07009198 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009199 return 0;
9200}
9201
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07009202static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009203 const struct audio_config *config)
9204{
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07009205 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009206
Aalique Grahame22e49102018-12-18 14:23:57 -08009207 /* Don't know if USB HIFI in this context so use true to be conservative */
9208 if (check_input_parameters(config->sample_rate, config->format, channel_count,
9209 true /*is_usb_hifi */) != 0)
9210 return 0;
9211
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07009212 return get_input_buffer_size(config->sample_rate, config->format, channel_count,
9213 false /* is_low_latency: since we don't know, be conservative */);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009214}
9215
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009216static bool adev_input_allow_hifi_record(struct audio_device *adev,
9217 audio_devices_t devices,
9218 audio_input_flags_t flags,
9219 audio_source_t source) {
9220 const bool allowed = true;
9221
9222 if (!audio_is_usb_in_device(devices))
9223 return !allowed;
9224
9225 switch (flags) {
9226 case AUDIO_INPUT_FLAG_NONE:
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009227 break;
Haynes Mathew George59862182017-10-24 16:23:57 -07009228 case AUDIO_INPUT_FLAG_FAST: // disallow hifi record for FAST as
9229 // it affects RTD numbers over USB
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009230 default:
9231 return !allowed;
9232 }
9233
9234 switch (source) {
9235 case AUDIO_SOURCE_DEFAULT:
9236 case AUDIO_SOURCE_MIC:
9237 case AUDIO_SOURCE_UNPROCESSED:
9238 break;
9239 default:
9240 return !allowed;
9241 }
9242
9243 switch (adev->mode) {
9244 case 0:
9245 break;
9246 default:
9247 return !allowed;
9248 }
9249
9250 return allowed;
9251}
9252
Haynes Mathew George4ffef292017-11-21 15:08:02 -08009253static int adev_update_voice_comm_input_stream(struct stream_in *in,
9254 struct audio_config *config)
9255{
9256 bool valid_rate = (config->sample_rate == 8000 ||
9257 config->sample_rate == 16000 ||
9258 config->sample_rate == 32000 ||
9259 config->sample_rate == 48000);
9260 bool valid_ch = audio_channel_count_from_in_mask(in->channel_mask) == 1;
9261
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08009262 if(!voice_extn_is_compress_voip_supported()) {
kunleiz28c73e72019-03-27 17:24:04 +08009263 if (valid_rate && valid_ch) {
Haynes Mathew George4ffef292017-11-21 15:08:02 -08009264 in->usecase = USECASE_AUDIO_RECORD_VOIP;
9265 in->config = default_pcm_config_voip_copp;
9266 in->config.period_size = VOIP_IO_BUF_SIZE(in->sample_rate,
9267 DEFAULT_VOIP_BUF_DURATION_MS,
9268 DEFAULT_VOIP_BIT_DEPTH_BYTE)/2;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08009269 } else {
9270 ALOGW("%s No valid input in voip, use defaults"
9271 "sample rate %u, channel mask 0x%X",
9272 __func__, config->sample_rate, in->channel_mask);
9273 }
Haynes Mathew George4ffef292017-11-21 15:08:02 -08009274 in->config.rate = config->sample_rate;
9275 in->sample_rate = config->sample_rate;
9276 } else {
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08009277 //XXX needed for voice_extn_compress_voip_open_input_stream
9278 in->config.rate = config->sample_rate;
9279 if ((in->dev->mode == AUDIO_MODE_IN_COMMUNICATION ||
Shalini Manjunathaa763cc42019-08-23 15:13:46 +05309280 in->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08009281 voice_extn_compress_voip_is_active(in->dev)) &&
9282 (voice_extn_compress_voip_is_format_supported(in->format)) &&
9283 valid_rate && valid_ch) {
9284 voice_extn_compress_voip_open_input_stream(in);
9285 // update rate entries to match config from AF
9286 in->config.rate = config->sample_rate;
9287 in->sample_rate = config->sample_rate;
9288 } else {
9289 ALOGW("%s compress voip not active, use defaults", __func__);
9290 }
Haynes Mathew George4ffef292017-11-21 15:08:02 -08009291 }
Haynes Mathew George4ffef292017-11-21 15:08:02 -08009292 return 0;
9293}
9294
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009295static int adev_open_input_stream(struct audio_hw_device *dev,
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07009296 audio_io_handle_t handle,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009297 audio_devices_t devices,
9298 struct audio_config *config,
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07009299 struct audio_stream_in **stream_in,
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05309300 audio_input_flags_t flags,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08009301 const char *address,
Vidyakumar Athota5c398212015-03-31 21:53:21 -07009302 audio_source_t source)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009303{
9304 struct audio_device *adev = (struct audio_device *)dev;
9305 struct stream_in *in;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08009306 int ret = 0, buffer_size, frame_size;
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07009307 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07009308 bool is_low_latency = false;
Divya Narayanan Poojary45f19192016-09-30 18:52:13 +05309309 bool channel_mask_updated = false;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009310 bool is_usb_dev = audio_is_usb_in_device(devices);
9311 bool may_use_hifi_record = adev_input_allow_hifi_record(adev,
9312 devices,
9313 flags,
9314 source);
Andy Hung94320602018-10-29 18:31:12 -07009315 ALOGV("%s: enter: flags %#x, is_usb_dev %d, may_use_hifi_record %d,"
9316 " sample_rate %u, channel_mask %#x, format %#x",
9317 __func__, flags, is_usb_dev, may_use_hifi_record,
9318 config->sample_rate, config->channel_mask, config->format);
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05309319
kunleizdff872d2018-08-20 14:40:33 +08009320 if (is_usb_dev && (!audio_extn_usb_connected(NULL))) {
kunleizd6a9e0c2018-07-30 15:38:52 +08009321 is_usb_dev = false;
9322 devices = AUDIO_DEVICE_IN_BUILTIN_MIC;
9323 ALOGW("%s: ignore set device to non existing USB card, use input device(%#x)",
9324 __func__, devices);
9325 }
9326
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009327 *stream_in = NULL;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009328
9329 if (!(is_usb_dev && may_use_hifi_record)) {
9330 if (config->sample_rate == 0)
9331 config->sample_rate = 48000;
9332 if (config->channel_mask == AUDIO_CHANNEL_NONE)
9333 config->channel_mask = AUDIO_CHANNEL_IN_MONO;
9334 if (config->format == AUDIO_FORMAT_DEFAULT)
9335 config->format = AUDIO_FORMAT_PCM_16_BIT;
9336
9337 channel_count = audio_channel_count_from_in_mask(config->channel_mask);
9338
Aalique Grahame22e49102018-12-18 14:23:57 -08009339 if (check_input_parameters(config->sample_rate, config->format, channel_count,
9340 false) != 0)
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009341 return -EINVAL;
Chaithanya Krishna Bacharaju9955b162016-05-25 16:25:53 +05309342 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009343
Rahul Sharma99770982019-03-06 17:05:26 +05309344 pthread_mutex_lock(&adev->lock);
9345 if (in_get_stream(adev, handle) != NULL) {
9346 ALOGW("%s, input stream already opened", __func__);
9347 ret = -EEXIST;
9348 }
9349 pthread_mutex_unlock(&adev->lock);
9350 if (ret)
9351 return ret;
9352
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009353 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07009354
9355 if (!in) {
9356 ALOGE("failed to allocate input stream");
9357 return -ENOMEM;
9358 }
9359
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05309360 ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x)\
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05309361 stream_handle(%p) io_handle(%d) source(%d) format %x",__func__, config->sample_rate,
9362 config->channel_mask, devices, &in->stream, handle, source, config->format);
Ravi Kumar Alamanda40703102014-04-24 10:34:41 -07009363 pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07009364 pthread_mutex_init(&in->pre_lock, (const pthread_mutexattr_t *) NULL);
Ravi Kumar Alamanda40703102014-04-24 10:34:41 -07009365
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009366 in->stream.common.get_sample_rate = in_get_sample_rate;
9367 in->stream.common.set_sample_rate = in_set_sample_rate;
9368 in->stream.common.get_buffer_size = in_get_buffer_size;
9369 in->stream.common.get_channels = in_get_channels;
9370 in->stream.common.get_format = in_get_format;
9371 in->stream.common.set_format = in_set_format;
9372 in->stream.common.standby = in_standby;
9373 in->stream.common.dump = in_dump;
9374 in->stream.common.set_parameters = in_set_parameters;
9375 in->stream.common.get_parameters = in_get_parameters;
9376 in->stream.common.add_audio_effect = in_add_audio_effect;
9377 in->stream.common.remove_audio_effect = in_remove_audio_effect;
9378 in->stream.set_gain = in_set_gain;
9379 in->stream.read = in_read;
9380 in->stream.get_input_frames_lost = in_get_input_frames_lost;
Aalique Grahame22e49102018-12-18 14:23:57 -08009381 in->stream.get_capture_position = in_get_capture_position;
Naresh Tannirudcb47c52018-06-25 16:23:32 +05309382 in->stream.get_active_microphones = in_get_active_microphones;
Paul McLeana50b7332018-12-17 08:24:21 -07009383 in->stream.set_microphone_direction = in_set_microphone_direction;
9384 in->stream.set_microphone_field_dimension = in_set_microphone_field_dimension;
juyuchendb308c22019-01-21 11:57:17 -07009385 in->stream.update_sink_metadata = in_update_sink_metadata;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009386
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08009387 list_init(&in->device_list);
9388 update_device_list(&in->device_list, devices, address, true);
Vidyakumar Athota5c398212015-03-31 21:53:21 -07009389 in->source = source;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009390 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009391 in->standby = 1;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07009392 in->capture_handle = handle;
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07009393 in->flags = flags;
Haynes Mathew George46740472017-10-27 18:40:12 -07009394 in->bit_width = 16;
9395 in->af_period_multiplier = 1;
justinweng20fb6d82019-02-21 18:49:00 -07009396 in->direction = MIC_DIRECTION_UNSPECIFIED;
9397 in->zoom = 0;
Carter Hsu2e429db2019-05-14 18:50:52 +08009398 list_init(&in->aec_list);
9399 list_init(&in->ns_list);
Phil Burkd898ba62019-06-20 12:49:01 -07009400 in->mmap_shared_memory_fd = -1; // not open
Haynes Mathew George46740472017-10-27 18:40:12 -07009401
Andy Hung94320602018-10-29 18:31:12 -07009402 ALOGV("%s: source %d, config->channel_mask %#x", __func__, source, config->channel_mask);
Aalique Grahame22e49102018-12-18 14:23:57 -08009403 if (source == AUDIO_SOURCE_VOICE_UPLINK ||
9404 source == AUDIO_SOURCE_VOICE_DOWNLINK) {
9405 /* Force channel config requested to mono if incall
9406 record is being requested for only uplink/downlink */
9407 if (config->channel_mask != AUDIO_CHANNEL_IN_MONO) {
9408 config->channel_mask = AUDIO_CHANNEL_IN_MONO;
9409 ret = -EINVAL;
9410 goto err_open;
9411 }
9412 }
9413
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009414 if (is_usb_dev && may_use_hifi_record) {
9415 /* HiFi record selects an appropriate format, channel, rate combo
9416 depending on sink capabilities*/
9417 ret = read_usb_sup_params_and_compare(false /*is_playback*/,
9418 &config->format,
9419 &in->supported_formats[0],
9420 MAX_SUPPORTED_FORMATS,
9421 &config->channel_mask,
9422 &in->supported_channel_masks[0],
9423 MAX_SUPPORTED_CHANNEL_MASKS,
9424 &config->sample_rate,
9425 &in->supported_sample_rates[0],
9426 MAX_SUPPORTED_SAMPLE_RATES);
9427 if (ret != 0) {
9428 ret = -EINVAL;
9429 goto err_open;
9430 }
9431 channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Haynes Mathew George4ffef292017-11-21 15:08:02 -08009432 } else if (config->format == AUDIO_FORMAT_DEFAULT) {
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05309433 config->format = AUDIO_FORMAT_PCM_16_BIT;
Surendar karkaaca3d082017-11-09 15:18:37 +05309434 } else if (property_get_bool("vendor.audio.capture.pcm.32bit.enable", false)
9435 && config->format == AUDIO_FORMAT_PCM_32_BIT) {
9436 in->config.format = PCM_FORMAT_S32_LE;
9437 in->bit_width = 32;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05309438 } else if ((config->format == AUDIO_FORMAT_PCM_FLOAT) ||
9439 (config->format == AUDIO_FORMAT_PCM_32_BIT) ||
9440 (config->format == AUDIO_FORMAT_PCM_24_BIT_PACKED) ||
9441 (config->format == AUDIO_FORMAT_PCM_8_24_BIT)) {
9442 bool ret_error = false;
9443 in->bit_width = 24;
9444 /* 24 bit is restricted to UNPROCESSED source only,also format supported
9445 from HAL is 24_packed and 8_24
9446 *> In case of UNPROCESSED source, for 24 bit, if format requested is other than
9447 24_packed return error indicating supported format is 24_packed
9448 *> In case of any other source requesting 24 bit or float return error
9449 indicating format supported is 16 bit only.
9450
9451 on error flinger will retry with supported format passed
9452 */
9453 if ((source != AUDIO_SOURCE_UNPROCESSED) &&
9454 (source != AUDIO_SOURCE_CAMCORDER)) {
9455 config->format = AUDIO_FORMAT_PCM_16_BIT;
9456 if (config->sample_rate > 48000)
9457 config->sample_rate = 48000;
9458 ret_error = true;
Haynes Mathew George46740472017-10-27 18:40:12 -07009459 } else if (!(config->format == AUDIO_FORMAT_PCM_24_BIT_PACKED ||
9460 config->format == AUDIO_FORMAT_PCM_8_24_BIT)) {
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05309461 config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
9462 ret_error = true;
9463 }
9464
9465 if (ret_error) {
9466 ret = -EINVAL;
9467 goto err_open;
9468 }
9469 }
9470
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009471 in->channel_mask = config->channel_mask;
9472 in->format = config->format;
9473
9474 in->usecase = USECASE_AUDIO_RECORD;
Samyak Jain0aa07ab2019-04-04 14:36:32 +05309475
9476 if (in->source == AUDIO_SOURCE_FM_TUNER) {
9477 if(!get_usecase_from_list(adev, USECASE_AUDIO_RECORD_FM_VIRTUAL))
9478 in->usecase = USECASE_AUDIO_RECORD_FM_VIRTUAL;
9479 else {
9480 ret = -EINVAL;
9481 goto err_open;
9482 }
9483 }
9484
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009485 if (config->sample_rate == LOW_LATENCY_CAPTURE_SAMPLE_RATE &&
Deeraj Somanfa377bf2019-02-06 12:57:59 +05309486 (flags & AUDIO_INPUT_FLAG_TIMESTAMP) == 0 &&
9487 (flags & AUDIO_INPUT_FLAG_COMPRESS) == 0 &&
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009488 (flags & AUDIO_INPUT_FLAG_FAST) != 0) {
9489 is_low_latency = true;
9490#if LOW_LATENCY_CAPTURE_USE_CASE
9491 in->usecase = USECASE_AUDIO_RECORD_LOW_LATENCY;
9492#endif
9493 in->realtime = may_use_noirq_mode(adev, in->usecase, in->flags);
Aalique Grahame22e49102018-12-18 14:23:57 -08009494 if (!in->realtime) {
9495 in->config = pcm_config_audio_capture;
9496 frame_size = audio_stream_in_frame_size(&in->stream);
9497 buffer_size = get_input_buffer_size(config->sample_rate,
9498 config->format,
9499 channel_count,
9500 is_low_latency);
9501 in->config.period_size = buffer_size / frame_size;
9502 in->config.rate = config->sample_rate;
9503 in->af_period_multiplier = 1;
9504 } else {
9505 // period size is left untouched for rt mode playback
9506 in->config = pcm_config_audio_capture_rt;
9507 in->af_period_multiplier = af_period_multiplier;
9508 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009509 }
9510
9511 if ((config->sample_rate == LOW_LATENCY_CAPTURE_SAMPLE_RATE) &&
9512 ((in->flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0)) {
9513 in->realtime = 0;
9514 in->usecase = USECASE_AUDIO_RECORD_MMAP;
9515 in->config = pcm_config_mmap_capture;
Haynes Mathew George46740472017-10-27 18:40:12 -07009516 in->config.format = pcm_format_from_audio_format(config->format);
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009517 in->stream.start = in_start;
9518 in->stream.stop = in_stop;
9519 in->stream.create_mmap_buffer = in_create_mmap_buffer;
9520 in->stream.get_mmap_position = in_get_mmap_position;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009521 ALOGV("%s: USECASE_AUDIO_RECORD_MMAP", __func__);
Haynes Mathew George46740472017-10-27 18:40:12 -07009522 } else if (is_usb_dev && may_use_hifi_record) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009523 in->usecase = USECASE_AUDIO_RECORD_HIFI;
9524 in->config = pcm_config_audio_capture;
9525 frame_size = audio_stream_in_frame_size(&in->stream);
9526 buffer_size = get_input_buffer_size(config->sample_rate,
9527 config->format,
9528 channel_count,
9529 false /*is_low_latency*/);
9530 in->config.period_size = buffer_size / frame_size;
9531 in->config.rate = config->sample_rate;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009532 in->config.format = pcm_format_from_audio_format(config->format);
Karthikeyan Mani07faa602018-08-20 11:01:32 -07009533 switch (config->format) {
9534 case AUDIO_FORMAT_PCM_32_BIT:
9535 in->bit_width = 32;
9536 break;
9537 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
9538 case AUDIO_FORMAT_PCM_8_24_BIT:
9539 in->bit_width = 24;
9540 break;
9541 default:
9542 in->bit_width = 16;
9543 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08009544 } else if (is_single_device_type_equal(&in->device_list,
9545 AUDIO_DEVICE_IN_TELEPHONY_RX) ||
9546 is_single_device_type_equal(&in->device_list,
9547 AUDIO_DEVICE_IN_PROXY)) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07009548 if (config->sample_rate == 0)
9549 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
9550 if (config->sample_rate != 48000 && config->sample_rate != 16000 &&
9551 config->sample_rate != 8000) {
9552 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
9553 ret = -EINVAL;
9554 goto err_open;
9555 }
9556 if (config->format == AUDIO_FORMAT_DEFAULT)
9557 config->format = AUDIO_FORMAT_PCM_16_BIT;
9558 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
9559 config->format = AUDIO_FORMAT_PCM_16_BIT;
9560 ret = -EINVAL;
9561 goto err_open;
9562 }
9563
9564 in->usecase = USECASE_AUDIO_RECORD_AFE_PROXY;
Zhou Song62ea0282020-03-22 19:53:01 +08009565 if (adev->ha_proxy_enable &&
9566 is_single_device_type_equal(&in->device_list,
9567 AUDIO_DEVICE_IN_TELEPHONY_RX))
9568 in->usecase = USECASE_AUDIO_RECORD_AFE_PROXY2;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07009569 in->config = pcm_config_afe_proxy_record;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07009570 in->config.rate = config->sample_rate;
Aalique Grahame22e49102018-12-18 14:23:57 -08009571 in->af_period_multiplier = 1;
9572 } else if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
Shalini Manjunathaabdf7482020-01-03 15:00:14 +05309573 (!voice_extn_is_compress_voip_supported()) &&
Aalique Grahame22e49102018-12-18 14:23:57 -08009574 in->flags & AUDIO_INPUT_FLAG_VOIP_TX &&
9575 (config->sample_rate == 8000 ||
9576 config->sample_rate == 16000 ||
9577 config->sample_rate == 32000 ||
9578 config->sample_rate == 48000) &&
9579 channel_count == 1) {
9580 in->usecase = USECASE_AUDIO_RECORD_VOIP;
9581 in->config = pcm_config_audio_capture;
9582 frame_size = audio_stream_in_frame_size(&in->stream);
9583 buffer_size = get_stream_buffer_size(VOIP_CAPTURE_PERIOD_DURATION_MSEC,
9584 config->sample_rate,
9585 config->format,
9586 channel_count, false /*is_low_latency*/);
9587 in->config.period_size = buffer_size / frame_size;
9588 in->config.period_count = VOIP_CAPTURE_PERIOD_COUNT;
9589 in->config.rate = config->sample_rate;
9590 in->af_period_multiplier = 1;
Mingshu Pang7b59b5e2021-01-14 16:19:10 +08009591 } else if (in->realtime) {
9592 in->config = pcm_config_audio_capture_rt;
9593 in->config.format = pcm_format_from_audio_format(config->format);
9594 in->af_period_multiplier = af_period_multiplier;
Haynes Mathew George46740472017-10-27 18:40:12 -07009595 } else {
Revathi Uddarajud2634032017-12-07 14:42:34 +05309596 int ret_val;
9597 pthread_mutex_lock(&adev->lock);
9598 ret_val = audio_extn_check_and_set_multichannel_usecase(adev,
9599 in, config, &channel_mask_updated);
9600 pthread_mutex_unlock(&adev->lock);
9601
9602 if (!ret_val) {
9603 if (channel_mask_updated == true) {
9604 ALOGD("%s: return error to retry with updated channel mask (%#x)",
9605 __func__, config->channel_mask);
9606 ret = -EINVAL;
9607 goto err_open;
9608 }
9609 ALOGD("%s: created multi-channel session succesfully",__func__);
9610 } else if (audio_extn_compr_cap_enabled() &&
9611 audio_extn_compr_cap_format_supported(config->format) &&
9612 (in->dev->mode != AUDIO_MODE_IN_COMMUNICATION)) {
9613 audio_extn_compr_cap_init(in);
9614 } else if (audio_extn_cin_applicable_stream(in)) {
Deeraj Soman14230922019-01-30 16:39:30 +05309615 ret = audio_extn_cin_configure_input_stream(in, config);
Revathi Uddarajud2634032017-12-07 14:42:34 +05309616 if (ret)
9617 goto err_open;
9618 } else {
9619 in->config = pcm_config_audio_capture;
9620 in->config.rate = config->sample_rate;
9621 in->config.format = pcm_format_from_audio_format(config->format);
Revathi Uddarajud2634032017-12-07 14:42:34 +05309622 in->format = config->format;
9623 frame_size = audio_stream_in_frame_size(&in->stream);
9624 buffer_size = get_input_buffer_size(config->sample_rate,
Haynes Mathew George46740472017-10-27 18:40:12 -07009625 config->format,
9626 channel_count,
9627 is_low_latency);
Dieter Luecking5d57def2018-09-07 14:23:37 +02009628 /* prevent division-by-zero */
9629 if (frame_size == 0) {
9630 ALOGE("%s: Error frame_size==0", __func__);
9631 ret = -EINVAL;
9632 goto err_open;
9633 }
9634
Revathi Uddarajud2634032017-12-07 14:42:34 +05309635 in->config.period_size = buffer_size / frame_size;
Aalique Grahame22e49102018-12-18 14:23:57 -08009636 in->af_period_multiplier = 1;
Haynes Mathew George4ffef292017-11-21 15:08:02 -08009637
Revathi Uddarajud2634032017-12-07 14:42:34 +05309638 if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
9639 /* optionally use VOIP usecase depending on config(s) */
9640 ret = adev_update_voice_comm_input_stream(in, config);
9641 }
Haynes Mathew George4ffef292017-11-21 15:08:02 -08009642
Revathi Uddarajud2634032017-12-07 14:42:34 +05309643 if (ret) {
9644 ALOGE("%s AUDIO_SOURCE_VOICE_COMMUNICATION invalid args", __func__);
9645 goto err_open;
9646 }
Haynes Mathew George4ffef292017-11-21 15:08:02 -08009647 }
Jaideep Sharmad305a4a2020-02-27 14:29:04 +05309648
9649 /* assign concurrent capture usecase if record has to caried out from
9650 * actual hardware input source */
9651 if (audio_extn_is_concurrent_capture_enabled() &&
9652 !audio_is_virtual_input_source(in->source)) {
Samyak Jainc37062f2019-04-25 18:41:06 +05309653 /* Acquire lock to avoid two concurrent use cases initialized to
9654 same pcm record use case */
kunleiz28c73e72019-03-27 17:24:04 +08009655
Samyak Jainc37062f2019-04-25 18:41:06 +05309656 if (in->usecase == USECASE_AUDIO_RECORD) {
9657 pthread_mutex_lock(&adev->lock);
9658 if (!(adev->pcm_record_uc_state)) {
9659 ALOGV("%s: using USECASE_AUDIO_RECORD",__func__);
9660 adev->pcm_record_uc_state = 1;
9661 pthread_mutex_unlock(&adev->lock);
9662 } else {
9663 pthread_mutex_unlock(&adev->lock);
9664 /* Assign compress record use case for second record */
9665 in->usecase = USECASE_AUDIO_RECORD_COMPRESS2;
9666 in->flags |= AUDIO_INPUT_FLAG_COMPRESS;
9667 ALOGV("%s: overriding usecase with USECASE_AUDIO_RECORD_COMPRESS2 and appending compress flag", __func__);
9668 if (audio_extn_cin_applicable_stream(in)) {
9669 in->sample_rate = config->sample_rate;
Deeraj Soman14230922019-01-30 16:39:30 +05309670 ret = audio_extn_cin_configure_input_stream(in, config);
Samyak Jainc37062f2019-04-25 18:41:06 +05309671 if (ret)
9672 goto err_open;
9673 }
9674 }
9675 }
kunleiz28c73e72019-03-27 17:24:04 +08009676 }
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07009677 }
Ramjee Singh82fd0c12019-08-21 16:31:33 +05309678 if (audio_extn_ssr_get_stream() != in)
9679 in->config.channels = channel_count;
Aniket Kumar Lata23d8cbf2019-04-10 19:54:46 -07009680
Aniket Kumar Lata23d8cbf2019-04-10 19:54:46 -07009681 in->sample_rate = in->config.rate;
9682
Dhananjay Kumard6d32152016-10-13 16:11:03 +05309683 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
9684 &adev->streams_input_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08009685 &in->device_list, flags, in->format,
Haynes Mathew George4ffef292017-11-21 15:08:02 -08009686 in->sample_rate, in->bit_width,
9687 in->profile, &in->app_type_cfg);
Naresh Tanniru04f71882018-06-26 17:46:22 +05309688 register_format(in->format, in->supported_formats);
9689 register_channel_mask(in->channel_mask, in->supported_channel_masks);
9690 register_sample_rate(in->sample_rate, in->supported_sample_rates);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05309691
Aalique Grahame22e49102018-12-18 14:23:57 -08009692 in->error_log = error_log_create(
9693 ERROR_LOG_ENTRIES,
9694 1000000000 /* aggregate consecutive identical errors within one second */);
9695
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07009696 /* This stream could be for sound trigger lab,
9697 get sound trigger pcm if present */
9698 audio_extn_sound_trigger_check_and_get_session(in);
9699
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05309700 lock_input_stream(in);
9701 audio_extn_snd_mon_register_listener(in, in_snd_mon_cb);
9702 pthread_mutex_lock(&adev->lock);
9703 in->card_status = adev->card_status;
9704 pthread_mutex_unlock(&adev->lock);
9705 pthread_mutex_unlock(&in->lock);
9706
Aalique Grahame22e49102018-12-18 14:23:57 -08009707 stream_app_type_cfg_init(&in->app_type_cfg);
9708
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009709 *stream_in = &in->stream;
Derek Chenf939fb72018-11-13 13:34:41 -08009710
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07009711 ret = io_streams_map_insert(adev, &in->stream.common,
9712 handle, AUDIO_PATCH_HANDLE_NONE);
9713 if (ret != 0)
9714 goto err_open;
9715
Derek Chenf939fb72018-11-13 13:34:41 -08009716 streams_input_ctxt_t *in_ctxt = (streams_input_ctxt_t *)
9717 calloc(1, sizeof(streams_input_ctxt_t));
9718 if (in_ctxt == NULL) {
9719 ALOGE("%s fail to allocate input ctxt", __func__);
9720 ret = -ENOMEM;
9721 goto err_open;
9722 }
9723 in_ctxt->input = in;
9724
9725 pthread_mutex_lock(&adev->lock);
9726 list_add_tail(&adev->active_inputs_list, &in_ctxt->list);
9727 pthread_mutex_unlock(&adev->lock);
9728
Eric Laurent994a6932013-07-17 11:51:42 -07009729 ALOGV("%s: exit", __func__);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08009730 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009731
9732err_open:
Samyak Jainc37062f2019-04-25 18:41:06 +05309733 if (in->usecase == USECASE_AUDIO_RECORD) {
9734 pthread_mutex_lock(&adev->lock);
9735 adev->pcm_record_uc_state = 0;
9736 pthread_mutex_unlock(&adev->lock);
9737 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009738 free(in);
9739 *stream_in = NULL;
9740 return ret;
9741}
9742
9743static void adev_close_input_stream(struct audio_hw_device *dev,
9744 struct audio_stream_in *stream)
9745{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08009746 int ret;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07009747 struct stream_in *in = (struct stream_in *)stream;
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07009748 struct audio_device *adev = (struct audio_device *)dev;
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05309749
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05309750 ALOGD("%s: enter:stream_handle(%p)",__func__, in);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08009751
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08009752 if (in == NULL) {
9753 ALOGE("%s: audio_stream_in ptr is NULL", __func__);
9754 return;
9755 }
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07009756 io_streams_map_remove(adev, in->capture_handle);
9757
kunleiz70e57612018-12-28 17:50:23 +08009758 /* must deregister from sndmonitor first to prevent races
9759 * between the callback and close_stream
9760 */
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05309761 audio_extn_snd_mon_unregister_listener(stream);
9762
kunleiz70e57612018-12-28 17:50:23 +08009763 /* Disable echo reference if there are no active input, hfp call
9764 * and sound trigger while closing input stream
9765 */
Eric Laurent637e2d42018-11-15 12:24:31 -08009766 if (adev_get_active_input(adev) == NULL &&
kunleiz70e57612018-12-28 17:50:23 +08009767 !audio_extn_hfp_is_active(adev) &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08009768 !audio_extn_sound_trigger_check_ec_ref_enable()) {
9769 struct listnode out_devices;
9770 list_init(&out_devices);
9771 platform_set_echo_reference(adev, false, &out_devices);
9772 } else
kunleiz70e57612018-12-28 17:50:23 +08009773 audio_extn_sound_trigger_update_ec_ref_status(false);
Venkata Narendra Kumar Gutta1bbbf542014-09-04 19:11:25 +05309774
Weiyin Jiang2995f662019-04-17 14:25:12 +08009775 error_log_destroy(in->error_log);
9776 in->error_log = NULL;
9777
Pallavid7c7a272018-01-16 11:22:55 +05309778
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08009779 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05309780 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08009781 ret = voice_extn_compress_voip_close_input_stream(&stream->common);
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05309782 pthread_mutex_unlock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08009783 if (ret != 0)
9784 ALOGE("%s: Compress voip input cannot be closed, error:%d",
9785 __func__, ret);
9786 } else
9787 in_standby(&stream->common);
9788
Weiyin Jiangd5974e62020-09-08 20:28:22 +08009789 pthread_mutex_destroy(&in->lock);
9790 pthread_mutex_destroy(&in->pre_lock);
9791
Revathi Uddarajud2634032017-12-07 14:42:34 +05309792 pthread_mutex_lock(&adev->lock);
Samyak Jain15fda662018-12-18 16:40:52 +05309793 if (in->usecase == USECASE_AUDIO_RECORD) {
9794 adev->pcm_record_uc_state = 0;
9795 }
9796
Kunlei Zhang5d5d8d92020-02-26 15:00:59 +08009797 if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
9798 adev->enable_voicerx = false;
9799 }
9800
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -07009801 if (audio_extn_ssr_get_stream() == in) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07009802 audio_extn_ssr_deinit();
9803 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009804
Garmond Leunge2433c32017-09-28 21:51:22 -07009805 if (audio_extn_ffv_get_stream() == in) {
9806 audio_extn_ffv_stream_deinit();
9807 }
9808
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05309809 if (audio_extn_compr_cap_enabled() &&
Mingming Yine62d7842013-10-25 16:26:03 -07009810 audio_extn_compr_cap_format_supported(in->config.format))
9811 audio_extn_compr_cap_deinit();
Dhanalakshmi Siddani74cf00b2016-12-02 13:55:57 +05309812
Dhananjay Kumar7dbe3562019-08-30 05:49:33 +05309813 if (audio_extn_cin_attached_usecase(in))
Manish Dewangan46e07982018-12-13 18:18:59 +05309814 audio_extn_cin_free_input_stream_resources(in);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07009815
Mingming Yinfd7607b2016-01-22 12:48:44 -08009816 if (in->is_st_session) {
9817 ALOGV("%s: sound trigger pcm stop lab", __func__);
9818 audio_extn_sound_trigger_stop_lab(in);
9819 }
Derek Chenf939fb72018-11-13 13:34:41 -08009820 streams_input_ctxt_t *in_ctxt = in_get_stream(adev, in->capture_handle);
9821 if (in_ctxt != NULL) {
9822 list_remove(&in_ctxt->list);
9823 free(in_ctxt);
9824 } else {
9825 ALOGW("%s, input stream already closed", __func__);
9826 }
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07009827 free(stream);
Revathi Uddarajud2634032017-12-07 14:42:34 +05309828 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009829 return;
9830}
9831
Aalique Grahame22e49102018-12-18 14:23:57 -08009832/* verifies input and output devices and their capabilities.
9833 *
9834 * This verification is required when enabling extended bit-depth or
9835 * sampling rates, as not all qcom products support it.
9836 *
9837 * Suitable for calling only on initialization such as adev_open().
9838 * It fills the audio_device use_case_table[] array.
9839 *
9840 * Has a side-effect that it needs to configure audio routing / devices
9841 * in order to power up the devices and read the device parameters.
9842 * It does not acquire any hw device lock. Should restore the devices
9843 * back to "normal state" upon completion.
9844 */
9845static int adev_verify_devices(struct audio_device *adev)
9846{
9847 /* enumeration is a bit difficult because one really wants to pull
9848 * the use_case, device id, etc from the hidden pcm_device_table[].
9849 * In this case there are the following use cases and device ids.
9850 *
9851 * [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = {0, 0},
9852 * [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {15, 15},
9853 * [USECASE_AUDIO_PLAYBACK_HIFI] = {1, 1},
9854 * [USECASE_AUDIO_PLAYBACK_OFFLOAD] = {9, 9},
9855 * [USECASE_AUDIO_RECORD] = {0, 0},
9856 * [USECASE_AUDIO_RECORD_LOW_LATENCY] = {15, 15},
9857 * [USECASE_VOICE_CALL] = {2, 2},
9858 *
9859 * USECASE_AUDIO_PLAYBACK_OFFLOAD, USECASE_AUDIO_PLAYBACK_HIFI omitted.
9860 * USECASE_VOICE_CALL omitted, but possible for either input or output.
9861 */
9862
9863 /* should be the usecases enabled in adev_open_input_stream() */
9864 static const int test_in_usecases[] = {
9865 USECASE_AUDIO_RECORD,
9866 USECASE_AUDIO_RECORD_LOW_LATENCY, /* does not appear to be used */
9867 };
9868 /* should be the usecases enabled in adev_open_output_stream()*/
9869 static const int test_out_usecases[] = {
9870 USECASE_AUDIO_PLAYBACK_DEEP_BUFFER,
9871 USECASE_AUDIO_PLAYBACK_LOW_LATENCY,
9872 };
9873 static const usecase_type_t usecase_type_by_dir[] = {
9874 PCM_PLAYBACK,
9875 PCM_CAPTURE,
9876 };
9877 static const unsigned flags_by_dir[] = {
9878 PCM_OUT,
9879 PCM_IN,
9880 };
9881
9882 size_t i;
9883 unsigned dir;
9884 const unsigned card_id = adev->snd_card;
9885
9886 for (dir = 0; dir < 2; ++dir) {
9887 const usecase_type_t usecase_type = usecase_type_by_dir[dir];
9888 const unsigned flags_dir = flags_by_dir[dir];
9889 const size_t testsize =
9890 dir ? ARRAY_SIZE(test_in_usecases) : ARRAY_SIZE(test_out_usecases);
9891 const int *testcases =
9892 dir ? test_in_usecases : test_out_usecases;
9893 const audio_devices_t audio_device =
9894 dir ? AUDIO_DEVICE_IN_BUILTIN_MIC : AUDIO_DEVICE_OUT_SPEAKER;
9895
9896 for (i = 0; i < testsize; ++i) {
9897 const audio_usecase_t audio_usecase = testcases[i];
9898 int device_id;
9899 struct pcm_params **pparams;
9900 struct stream_out out;
9901 struct stream_in in;
9902 struct audio_usecase uc_info;
9903 int retval;
9904
9905 pparams = &adev->use_case_table[audio_usecase];
9906 pcm_params_free(*pparams); /* can accept null input */
9907 *pparams = NULL;
9908
9909 /* find the device ID for the use case (signed, for error) */
9910 device_id = platform_get_pcm_device_id(audio_usecase, usecase_type);
9911 if (device_id < 0)
9912 continue;
9913
9914 /* prepare structures for device probing */
9915 memset(&uc_info, 0, sizeof(uc_info));
9916 uc_info.id = audio_usecase;
9917 uc_info.type = usecase_type;
Aniket Kumar Lata9c2fd892020-01-22 22:20:00 -08009918 list_init(&uc_info.device_list);
Aalique Grahame22e49102018-12-18 14:23:57 -08009919 if (dir) {
Aalique Grahame22e49102018-12-18 14:23:57 -08009920 memset(&in, 0, sizeof(in));
Aniket Kumar Lata9c2fd892020-01-22 22:20:00 -08009921 list_init(&in.device_list);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08009922 update_device_list(&in.device_list, audio_device, "", true);
Aalique Grahame22e49102018-12-18 14:23:57 -08009923 in.source = AUDIO_SOURCE_VOICE_COMMUNICATION;
9924 uc_info.stream.in = &in;
Aalique Grahame22e49102018-12-18 14:23:57 -08009925 }
9926 memset(&out, 0, sizeof(out));
Aniket Kumar Lata9c2fd892020-01-22 22:20:00 -08009927 list_init(&out.device_list);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08009928 update_device_list(&out.device_list, audio_device, "", true);
Aalique Grahame22e49102018-12-18 14:23:57 -08009929 uc_info.stream.out = &out;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08009930 update_device_list(&uc_info.device_list, audio_device, "", true);
Aalique Grahame22e49102018-12-18 14:23:57 -08009931 uc_info.in_snd_device = SND_DEVICE_NONE;
9932 uc_info.out_snd_device = SND_DEVICE_NONE;
9933 list_add_tail(&adev->usecase_list, &uc_info.list);
9934
9935 /* select device - similar to start_(in/out)put_stream() */
9936 retval = select_devices(adev, audio_usecase);
9937 if (retval >= 0) {
9938 *pparams = pcm_params_get(card_id, device_id, flags_dir);
9939#if LOG_NDEBUG == 0
Aalique Grahame203bee02019-03-13 17:49:36 -07009940 char info[512]; /* for possible debug info */
Aalique Grahame22e49102018-12-18 14:23:57 -08009941 if (*pparams) {
9942 ALOGV("%s: (%s) card %d device %d", __func__,
9943 dir ? "input" : "output", card_id, device_id);
9944 pcm_params_to_string(*pparams, info, ARRAY_SIZE(info));
9945 } else {
9946 ALOGV("%s: cannot locate card %d device %d", __func__, card_id, device_id);
9947 }
9948#endif
9949 }
9950
9951 /* deselect device - similar to stop_(in/out)put_stream() */
9952 /* 1. Get and set stream specific mixer controls */
9953 retval = disable_audio_route(adev, &uc_info);
9954 /* 2. Disable the rx device */
9955 retval = disable_snd_device(adev,
9956 dir ? uc_info.in_snd_device : uc_info.out_snd_device);
9957 list_remove(&uc_info.list);
9958 }
9959 }
Aalique Grahame22e49102018-12-18 14:23:57 -08009960 return 0;
9961}
9962
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07009963int update_patch(unsigned int num_sources,
9964 const struct audio_port_config *sources,
9965 unsigned int num_sinks,
9966 const struct audio_port_config *sinks,
9967 audio_patch_handle_t handle,
9968 struct audio_patch_info *p_info,
9969 patch_type_t patch_type, bool new_patch)
9970{
9971 ALOGD("%s: enter", __func__);
9972
9973 if (p_info == NULL) {
9974 ALOGE("%s: Invalid patch pointer", __func__);
9975 return -EINVAL;
9976 }
9977
9978 if (new_patch) {
9979 p_info->patch = (struct audio_patch *) calloc(1, sizeof(struct audio_patch));
9980 if (p_info->patch == NULL) {
9981 ALOGE("%s: Could not allocate patch", __func__);
9982 return -ENOMEM;
9983 }
9984 }
9985
9986 p_info->patch->id = handle;
9987 p_info->patch->num_sources = num_sources;
9988 p_info->patch->num_sinks = num_sinks;
9989
9990 for (int i = 0; i < num_sources; i++)
9991 p_info->patch->sources[i] = sources[i];
9992 for (int i = 0; i < num_sinks; i++)
9993 p_info->patch->sinks[i] = sinks[i];
9994
9995 p_info->patch_type = patch_type;
9996 return 0;
9997}
9998
9999audio_patch_handle_t generate_patch_handle()
10000{
10001 static audio_patch_handle_t patch_handle = AUDIO_PATCH_HANDLE_NONE;
10002 if (++patch_handle < 0)
10003 patch_handle = AUDIO_PATCH_HANDLE_NONE + 1;
10004 return patch_handle;
10005}
10006
Siddartha Shaik31b530e2017-05-19 15:26:33 +053010007int adev_create_audio_patch(struct audio_hw_device *dev,
10008 unsigned int num_sources,
10009 const struct audio_port_config *sources,
10010 unsigned int num_sinks,
10011 const struct audio_port_config *sinks,
10012 audio_patch_handle_t *handle)
10013{
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010014 int ret = 0;
10015 struct audio_device *adev = (struct audio_device *)dev;
10016 struct audio_patch_info *p_info = NULL;
10017 patch_type_t patch_type = PATCH_NONE;
10018 audio_io_handle_t io_handle = AUDIO_IO_HANDLE_NONE;
10019 audio_source_t input_source = AUDIO_SOURCE_DEFAULT;
10020 struct audio_stream_info *s_info = NULL;
10021 struct audio_stream *stream = NULL;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010022 struct listnode devices;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010023 audio_devices_t device_type = AUDIO_DEVICE_NONE;
10024 bool new_patch = false;
10025 char addr[AUDIO_DEVICE_MAX_ADDRESS_LEN];
Siddartha Shaik31b530e2017-05-19 15:26:33 +053010026
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010027 ALOGD("%s: enter: num sources %d, num_sinks %d, handle %d", __func__,
10028 num_sources, num_sinks, *handle);
10029
10030 if (num_sources == 0 || num_sources > AUDIO_PATCH_PORTS_MAX ||
10031 num_sinks == 0 || num_sinks > AUDIO_PATCH_PORTS_MAX) {
10032 ALOGE("%s: Invalid patch arguments", __func__);
10033 ret = -EINVAL;
10034 goto done;
10035 }
10036
10037 if (num_sources > 1) {
10038 ALOGE("%s: Multiple sources are not supported", __func__);
10039 ret = -EINVAL;
10040 goto done;
10041 }
10042
10043 if (sources == NULL || sinks == NULL) {
10044 ALOGE("%s: Invalid sources or sinks port config", __func__);
10045 ret = -EINVAL;
10046 goto done;
10047 }
10048
10049 ALOGV("%s: source role %d, source type %d", __func__,
10050 sources[0].type, sources[0].role);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010051 list_init(&devices);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010052
10053 // Populate source/sink information and fetch stream info
10054 switch (sources[0].type) {
10055 case AUDIO_PORT_TYPE_DEVICE: // Patch for audio capture or loopback
10056 device_type = sources[0].ext.device.type;
10057 strlcpy(&addr[0], &sources[0].ext.device.address[0], AUDIO_DEVICE_MAX_ADDRESS_LEN);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010058 update_device_list(&devices, device_type, &addr[0], true);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010059 if (sinks[0].type == AUDIO_PORT_TYPE_MIX) {
10060 patch_type = PATCH_CAPTURE;
10061 io_handle = sinks[0].ext.mix.handle;
10062 input_source = sinks[0].ext.mix.usecase.source;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010063 ALOGD("%s: Capture patch from device %x to mix %d",
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010064 __func__, device_type, io_handle);
10065 } else {
10066 // Device to device patch is not implemented.
10067 // This space will need changes if audio HAL
10068 // handles device to device patches in the future.
10069 patch_type = PATCH_DEVICE_LOOPBACK;
10070 }
10071 break;
10072 case AUDIO_PORT_TYPE_MIX: // Patch for audio playback
10073 io_handle = sources[0].ext.mix.handle;
10074 for (int i = 0; i < num_sinks; i++) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010075 device_type = sinks[i].ext.device.type;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010076 strlcpy(&addr[0], &sinks[i].ext.device.address[0], AUDIO_DEVICE_MAX_ADDRESS_LEN);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010077 update_device_list(&devices, device_type, &addr[0], true);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010078 }
10079 patch_type = PATCH_PLAYBACK;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010080 ALOGD("%s: Playback patch from mix handle %d to device %x",
10081 __func__, io_handle, get_device_types(&devices));
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010082 break;
10083 case AUDIO_PORT_TYPE_SESSION:
10084 case AUDIO_PORT_TYPE_NONE:
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010085 ALOGE("%s: Unsupported source type %d", __func__, sources[0].type);
10086 ret = -EINVAL;
10087 goto done;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010088 }
10089
10090 pthread_mutex_lock(&adev->lock);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010091
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010092 // Generate patch info and update patch
10093 if (*handle == AUDIO_PATCH_HANDLE_NONE) {
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010094 *handle = generate_patch_handle();
10095 p_info = (struct audio_patch_info *)
10096 calloc(1, sizeof(struct audio_patch_info));
10097 if (p_info == NULL) {
10098 ALOGE("%s: Failed to allocate memory", __func__);
10099 pthread_mutex_unlock(&adev->lock);
10100 ret = -ENOMEM;
10101 goto done;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010102 }
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010103 new_patch = true;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010104 } else {
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010105 p_info = fetch_patch_info_l(adev, *handle);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010106 if (p_info == NULL) {
10107 ALOGE("%s: Unable to fetch patch for received patch handle %d",
10108 __func__, *handle);
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010109 pthread_mutex_unlock(&adev->lock);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010110 ret = -EINVAL;
10111 goto done;
10112 }
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010113 }
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010114 update_patch(num_sources, sources, num_sinks, sinks,
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010115 *handle, p_info, patch_type, new_patch);
10116
10117 // Fetch stream info of associated mix for playback or capture patches
10118 if (p_info->patch_type == PATCH_PLAYBACK ||
10119 p_info->patch_type == PATCH_CAPTURE) {
10120 s_info = hashmapGet(adev->io_streams_map, (void *) (intptr_t) io_handle);
10121 if (s_info == NULL) {
10122 ALOGE("%s: Failed to obtain stream info", __func__);
10123 if (new_patch)
10124 free(p_info);
10125 pthread_mutex_unlock(&adev->lock);
10126 ret = -EINVAL;
10127 goto done;
10128 }
10129 ALOGV("%s: Fetched stream info with io_handle %d", __func__, io_handle);
10130 s_info->patch_handle = *handle;
10131 stream = s_info->stream;
10132 }
10133 pthread_mutex_unlock(&adev->lock);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010134
10135 // Update routing for stream
10136 if (stream != NULL) {
10137 if (p_info->patch_type == PATCH_PLAYBACK)
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010138 ret = route_output_stream((struct stream_out *) stream, &devices);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010139 else if (p_info->patch_type == PATCH_CAPTURE)
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010140 ret = route_input_stream((struct stream_in *) stream, &devices, input_source);
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010141 if (ret < 0) {
10142 pthread_mutex_lock(&adev->lock);
10143 s_info->patch_handle = AUDIO_PATCH_HANDLE_NONE;
10144 if (new_patch)
10145 free(p_info);
10146 pthread_mutex_unlock(&adev->lock);
10147 ALOGE("%s: Stream routing failed for io_handle %d", __func__, io_handle);
10148 goto done;
10149 }
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010150 }
10151
10152 // Add new patch to patch map
10153 if (!ret && new_patch) {
10154 pthread_mutex_lock(&adev->lock);
10155 hashmapPut(adev->patch_map, (void *) (intptr_t) *handle, (void *) p_info);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010156 ALOGD("%s: Added a new patch with handle %d", __func__, *handle);
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010157 pthread_mutex_unlock(&adev->lock);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010158 }
10159
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010160done:
10161 audio_extn_hw_loopback_create_audio_patch(dev,
Derek Chenf939fb72018-11-13 13:34:41 -080010162 num_sources,
10163 sources,
10164 num_sinks,
10165 sinks,
10166 handle);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010167 audio_extn_auto_hal_create_audio_patch(dev,
Derek Chenf939fb72018-11-13 13:34:41 -080010168 num_sources,
10169 sources,
10170 num_sinks,
10171 sinks,
10172 handle);
10173 return ret;
Siddartha Shaik31b530e2017-05-19 15:26:33 +053010174}
10175
10176int adev_release_audio_patch(struct audio_hw_device *dev,
10177 audio_patch_handle_t handle)
10178{
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010179 struct audio_device *adev = (struct audio_device *) dev;
10180 int ret = 0;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010181 audio_source_t input_source = AUDIO_SOURCE_DEFAULT;
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010182 struct audio_stream *stream = NULL;
Derek Chenf939fb72018-11-13 13:34:41 -080010183
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010184 if (handle == AUDIO_PATCH_HANDLE_NONE) {
10185 ALOGE("%s: Invalid patch handle %d", __func__, handle);
10186 ret = -EINVAL;
10187 goto done;
10188 }
10189
10190 ALOGD("%s: Remove patch with handle %d", __func__, handle);
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010191 pthread_mutex_lock(&adev->lock);
10192 struct audio_patch_info *p_info = fetch_patch_info_l(adev, handle);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010193 if (p_info == NULL) {
10194 ALOGE("%s: Patch info not found with handle %d", __func__, handle);
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010195 pthread_mutex_unlock(&adev->lock);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010196 ret = -EINVAL;
10197 goto done;
10198 }
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010199 struct audio_patch *patch = p_info->patch;
10200 if (patch == NULL) {
10201 ALOGE("%s: Patch not found for handle %d", __func__, handle);
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010202 pthread_mutex_unlock(&adev->lock);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010203 ret = -EINVAL;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010204 goto done;
10205 }
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010206 audio_io_handle_t io_handle = AUDIO_IO_HANDLE_NONE;
10207 switch (patch->sources[0].type) {
10208 case AUDIO_PORT_TYPE_MIX:
10209 io_handle = patch->sources[0].ext.mix.handle;
10210 break;
10211 case AUDIO_PORT_TYPE_DEVICE:
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010212 if (p_info->patch_type == PATCH_CAPTURE)
10213 io_handle = patch->sinks[0].ext.mix.handle;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010214 break;
10215 case AUDIO_PORT_TYPE_SESSION:
10216 case AUDIO_PORT_TYPE_NONE:
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010217 pthread_mutex_unlock(&adev->lock);
10218 ret = -EINVAL;
10219 goto done;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010220 }
10221
10222 // Remove patch and reset patch handle in stream info
Aniket Kumar Latace2b9fa2020-03-10 15:04:15 -070010223 patch_type_t patch_type = p_info->patch_type;
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010224 patch_map_remove_l(adev, handle);
Aniket Kumar Latace2b9fa2020-03-10 15:04:15 -070010225 if (patch_type == PATCH_PLAYBACK ||
10226 patch_type == PATCH_CAPTURE) {
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010227 struct audio_stream_info *s_info =
10228 hashmapGet(adev->io_streams_map, (void *) (intptr_t) io_handle);
10229 if (s_info == NULL) {
10230 ALOGE("%s: stream for io_handle %d is not available", __func__, io_handle);
10231 pthread_mutex_unlock(&adev->lock);
10232 goto done;
10233 }
10234 s_info->patch_handle = AUDIO_PATCH_HANDLE_NONE;
10235 stream = s_info->stream;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010236 }
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010237 pthread_mutex_unlock(&adev->lock);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010238
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010239 if (stream != NULL) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010240 struct listnode devices;
10241 list_init(&devices);
Aniket Kumar Latace2b9fa2020-03-10 15:04:15 -070010242 if (patch_type == PATCH_PLAYBACK)
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010243 ret = route_output_stream((struct stream_out *) stream, &devices);
Aniket Kumar Latace2b9fa2020-03-10 15:04:15 -070010244 else if (patch_type == PATCH_CAPTURE)
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010245 ret = route_input_stream((struct stream_in *) stream, &devices, input_source);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010246 }
10247
10248 if (ret < 0)
10249 ALOGW("%s: Stream routing failed for io_handle %d", __func__, io_handle);
10250
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010251done:
10252 audio_extn_hw_loopback_release_audio_patch(dev, handle);
10253 audio_extn_auto_hal_release_audio_patch(dev, handle);
10254
10255 ALOGV("%s: Successfully released patch %d", __func__, handle);
Derek Chenf939fb72018-11-13 13:34:41 -080010256 return ret;
Siddartha Shaik31b530e2017-05-19 15:26:33 +053010257}
10258
10259int adev_get_audio_port(struct audio_hw_device *dev, struct audio_port *config)
10260{
Derek Chenf13dd492018-11-13 14:53:51 -080010261 int ret = 0;
10262
10263 ret = audio_extn_hw_loopback_get_audio_port(dev, config);
10264 ret |= audio_extn_auto_hal_get_audio_port(dev, config);
10265 return ret;
Siddartha Shaik31b530e2017-05-19 15:26:33 +053010266}
10267
10268int adev_set_audio_port_config(struct audio_hw_device *dev,
10269 const struct audio_port_config *config)
10270{
Derek Chenf13dd492018-11-13 14:53:51 -080010271 int ret = 0;
10272
10273 ret = audio_extn_hw_loopback_set_audio_port_config(dev, config);
10274 ret |= audio_extn_auto_hal_set_audio_port_config(dev, config);
10275 return ret;
Siddartha Shaik31b530e2017-05-19 15:26:33 +053010276}
10277
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -070010278static int adev_dump(const audio_hw_device_t *device __unused,
10279 int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010280{
10281 return 0;
10282}
10283
10284static int adev_close(hw_device_t *device)
10285{
Aalique Grahame22e49102018-12-18 14:23:57 -080010286 size_t i;
Preetam Singh Ranawatacc64542019-06-24 15:11:28 +053010287 struct audio_device *adev_temp = (struct audio_device *)device;
Kiran Kandi910e1862013-10-29 13:29:42 -070010288
Preetam Singh Ranawatacc64542019-06-24 15:11:28 +053010289 if (!adev_temp)
Kiran Kandi910e1862013-10-29 13:29:42 -070010290 return 0;
10291
10292 pthread_mutex_lock(&adev_init_lock);
10293
10294 if ((--audio_device_ref_count) == 0) {
Sujin Panicker390724d2019-04-26 10:43:36 +053010295 if (audio_extn_spkr_prot_is_enabled())
10296 audio_extn_spkr_prot_deinit();
Jaideep Sharmaa2b49672019-09-10 20:37:03 +053010297 audio_extn_battery_properties_listener_deinit();
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053010298 audio_extn_snd_mon_unregister_listener(adev);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -070010299 audio_extn_sound_trigger_deinit(adev);
Kiran Kandide144c82013-11-20 15:58:32 -080010300 audio_extn_listen_deinit(adev);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -080010301 audio_extn_qdsp_deinit();
Aalique Grahame22e49102018-12-18 14:23:57 -080010302 audio_extn_extspk_deinit(adev->extspk);
Dhananjay Kumard6d32152016-10-13 16:11:03 +053010303 audio_extn_utils_release_streams_cfg_lists(
10304 &adev->streams_output_cfg_list,
10305 &adev->streams_input_cfg_list);
Sidipotu Ashokd2f10ba2019-05-06 15:41:56 +053010306 if (audio_extn_qap_is_enabled())
10307 audio_extn_qap_deinit();
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +053010308 if (audio_extn_qaf_is_enabled())
10309 audio_extn_qaf_deinit();
Kiran Kandi910e1862013-10-29 13:29:42 -070010310 audio_route_free(adev->audio_route);
Weiyin Jiangfa65d3e2019-03-05 23:39:45 +080010311 audio_extn_gef_deinit(adev);
Kiran Kandi910e1862013-10-29 13:29:42 -070010312 free(adev->snd_dev_ref_cnt);
10313 platform_deinit(adev->platform);
Aalique Grahame22e49102018-12-18 14:23:57 -080010314 for (i = 0; i < ARRAY_SIZE(adev->use_case_table); ++i) {
10315 pcm_params_free(adev->use_case_table[i]);
10316 }
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -070010317 if (adev->adm_deinit)
10318 adev->adm_deinit(adev->adm_data);
Dhananjay Kumaree4d2002016-10-25 18:02:58 +053010319 qahwi_deinit(device);
Ben Rombergerd771a7c2017-02-22 18:05:17 -080010320 audio_extn_adsp_hdlr_deinit();
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053010321 audio_extn_snd_mon_deinit();
Siddartha Shaik44dd7702017-06-14 12:13:25 +053010322 audio_extn_hw_loopback_deinit(adev);
Garmond Leunge2433c32017-09-28 21:51:22 -070010323 audio_extn_ffv_deinit();
Satish Babu Patakokilac3c5d432017-07-04 22:48:59 +053010324 if (adev->device_cfg_params) {
10325 free(adev->device_cfg_params);
10326 adev->device_cfg_params = NULL;
10327 }
Derek Chend2530072014-11-24 12:39:14 -080010328 if(adev->ext_hw_plugin)
10329 audio_extn_ext_hw_plugin_deinit(adev->ext_hw_plugin);
Derek Chenae7b0342019-02-08 15:17:04 -080010330 audio_extn_auto_hal_deinit();
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010331 free_map(adev->patch_map);
10332 free_map(adev->io_streams_map);
Kiran Kandi910e1862013-10-29 13:29:42 -070010333 free(device);
10334 adev = NULL;
10335 }
10336 pthread_mutex_unlock(&adev_init_lock);
Vatsal Buchac09ae062018-11-14 13:25:08 +053010337 enable_gcov();
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010338 return 0;
10339}
10340
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -070010341/* This returns 1 if the input parameter looks at all plausible as a low latency period size,
10342 * or 0 otherwise. A return value of 1 doesn't mean the value is guaranteed to work,
10343 * just that it _might_ work.
10344 */
10345static int period_size_is_plausible_for_low_latency(int period_size)
10346{
10347 switch (period_size) {
10348 case 160:
Ravi Kumar Alamanda474de5a2015-06-25 20:08:01 -070010349 case 192:
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -070010350 case 240:
10351 case 320:
10352 case 480:
10353 return 1;
10354 default:
10355 return 0;
10356 }
10357}
10358
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053010359static void adev_snd_mon_cb(void *cookie, struct str_parms *parms)
10360{
10361 bool is_snd_card_status = false;
10362 bool is_ext_device_status = false;
10363 char value[32];
10364 int card = -1;
10365 card_status_t status;
10366
10367 if (cookie != adev || !parms)
10368 return;
10369
10370 if (!parse_snd_card_status(parms, &card, &status)) {
10371 is_snd_card_status = true;
10372 } else if (0 < str_parms_get_str(parms, "ext_audio_device", value, sizeof(value))) {
10373 is_ext_device_status = true;
10374 } else {
10375 // not a valid event
10376 return;
10377 }
10378
10379 pthread_mutex_lock(&adev->lock);
10380 if (card == adev->snd_card || is_ext_device_status) {
10381 if (is_snd_card_status && adev->card_status != status) {
10382 adev->card_status = status;
10383 platform_snd_card_update(adev->platform, status);
10384 audio_extn_fm_set_parameters(adev, parms);
Derek Chend6f371d2019-03-01 13:45:58 -080010385 audio_extn_auto_hal_set_parameters(adev, parms);
Zhou Song4f43e352019-07-02 00:32:23 +080010386 if (status == CARD_STATUS_OFFLINE)
10387 audio_extn_sco_reset_configuration();
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053010388 } else if (is_ext_device_status) {
10389 platform_set_parameters(adev->platform, parms);
10390 }
10391 }
10392 pthread_mutex_unlock(&adev->lock);
10393 return;
10394}
10395
Weiyin Jiangd5974e62020-09-08 20:28:22 +080010396/* adev lock held */
10397int check_a2dp_restore_l(struct audio_device *adev, struct stream_out *out, bool restore)
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010398{
10399 struct audio_usecase *uc_info;
Zhou Song3bbcee72020-12-27 19:13:04 +080010400 struct audio_usecase *usecase;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010401 struct listnode devices;
Zhou Song3bbcee72020-12-27 19:13:04 +080010402 struct listnode *node;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010403
10404 uc_info = get_usecase_from_list(adev, out->usecase);
10405 if (uc_info == NULL) {
10406 ALOGE("%s: Could not find the usecase (%d) in the list",
10407 __func__, out->usecase);
10408 return -EINVAL;
10409 }
Aniket Kumar Lata9c2fd892020-01-22 22:20:00 -080010410 list_init(&devices);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010411
Zhou Song1f93fa52020-11-20 13:57:39 +080010412 ALOGD("%s: enter: usecase(%d: %s), a2dp muted %d", __func__,
10413 out->usecase, use_case_table[out->usecase], out->a2dp_muted);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010414
10415 if (restore) {
Weiyin Jiangd5974e62020-09-08 20:28:22 +080010416 pthread_mutex_lock(&out->latch_lock);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010417 // restore A2DP device for active usecases and unmute if required
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010418 if (is_a2dp_out_device_type(&out->device_list)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010419 ALOGD("%s: restoring A2dp and unmuting stream", __func__);
Zhou Songe5225132019-09-26 15:33:36 +080010420 if (uc_info->out_snd_device != SND_DEVICE_OUT_BT_A2DP)
10421 select_devices(adev, uc_info->id);
Zhou Song1f93fa52020-11-20 13:57:39 +080010422
10423 if (is_offload_usecase(out->usecase)) {
10424 if (uc_info->out_snd_device == SND_DEVICE_OUT_BT_A2DP)
Weiyin Jiangd5974e62020-09-08 20:28:22 +080010425 out_set_compr_volume(&out->stream, out->volume_l, out->volume_r);
Zhou Song1f93fa52020-11-20 13:57:39 +080010426 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP) {
10427 out_set_voip_volume(&out->stream, out->volume_l, out->volume_r);
10428 } else {
10429 out_set_pcm_volume(&out->stream, out->volume_l, out->volume_r);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010430 }
Zhou Song1f93fa52020-11-20 13:57:39 +080010431 out->a2dp_muted = false;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010432 }
Weiyin Jiangd5974e62020-09-08 20:28:22 +080010433 pthread_mutex_unlock(&out->latch_lock);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010434 } else {
Weiyin Jiangd5974e62020-09-08 20:28:22 +080010435 pthread_mutex_lock(&out->latch_lock);
Zhou Song1f93fa52020-11-20 13:57:39 +080010436 // mute stream and switch to speaker if suspended
10437 if (!out->a2dp_muted && !out->standby) {
Zhou Song1f93fa52020-11-20 13:57:39 +080010438 assign_devices(&devices, &out->device_list);
10439 reassign_device_list(&out->device_list, AUDIO_DEVICE_OUT_SPEAKER, "");
Zhou Song3bbcee72020-12-27 19:13:04 +080010440 list_for_each(node, &adev->usecase_list) {
10441 usecase = node_to_item(node, struct audio_usecase, list);
10442 if ((usecase != uc_info) &&
10443 platform_check_backends_match(SND_DEVICE_OUT_SPEAKER,
10444 usecase->out_snd_device)) {
10445 assign_devices(&out->device_list, &usecase->stream.out->device_list);
10446 break;
10447 }
10448 }
Zhou Song5e44b782021-01-14 16:48:03 +080010449 if (uc_info->out_snd_device == SND_DEVICE_OUT_BT_A2DP) {
10450 out->a2dp_muted = true;
10451 if (is_offload_usecase(out->usecase)) {
10452 if (out->offload_state == OFFLOAD_STATE_PLAYING)
10453 compress_pause(out->compr);
10454 out_set_compr_volume(&out->stream, (float)0, (float)0);
10455 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP) {
10456 out_set_voip_volume(&out->stream, (float)0, (float)0);
10457 } else {
10458 out_set_pcm_volume(&out->stream, (float)0, (float)0);
10459 /* wait for stale pcm drained before switching to speaker */
10460 uint32_t latency =
10461 (out->config.period_count * out->config.period_size * 1000) /
10462 (out->config.rate);
10463 usleep(latency * 1000);
10464 }
Zhou Song1f93fa52020-11-20 13:57:39 +080010465 }
10466 select_devices(adev, out->usecase);
Zhou Song5e44b782021-01-14 16:48:03 +080010467 ALOGD("%s: switched to device:%s and stream muted:%d", __func__,
10468 platform_get_snd_device_name(uc_info->out_snd_device), out->a2dp_muted);
Zhou Song1f93fa52020-11-20 13:57:39 +080010469 if (is_offload_usecase(out->usecase)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010470 if (out->offload_state == OFFLOAD_STATE_PLAYING)
10471 compress_resume(out->compr);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010472 }
Zhou Song1f93fa52020-11-20 13:57:39 +080010473 assign_devices(&out->device_list, &devices);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010474 }
Weiyin Jiangd5974e62020-09-08 20:28:22 +080010475 pthread_mutex_unlock(&out->latch_lock);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010476 }
10477 ALOGV("%s: exit", __func__);
10478 return 0;
10479}
10480
Haynes Mathew George01156f92018-04-13 15:29:54 -070010481void adev_on_battery_status_changed(bool charging)
10482{
10483 pthread_mutex_lock(&adev->lock);
10484 ALOGI("%s: battery status changed to %scharging", __func__, charging ? "" : "not ");
10485 adev->is_charging = charging;
Zhou Songc1088ea2018-06-12 00:17:29 +080010486 audio_extn_sound_trigger_update_battery_status(charging);
Haynes Mathew George01156f92018-04-13 15:29:54 -070010487 pthread_mutex_unlock(&adev->lock);
10488}
10489
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010490static int adev_open(const hw_module_t *module, const char *name,
10491 hw_device_t **device)
10492{
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +053010493 int ret;
Derek Chenf939fb72018-11-13 13:34:41 -080010494 char value[PROPERTY_VALUE_MAX] = {0};
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +053010495 char mixer_ctl_name[128] = {0};
10496 struct mixer_ctl *ctl = NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +053010497
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -080010498 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010499 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
10500
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070010501 pthread_mutex_lock(&adev_init_lock);
Kiran Kandi910e1862013-10-29 13:29:42 -070010502 if (audio_device_ref_count != 0){
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070010503 *device = &adev->device.common;
Kiran Kandi910e1862013-10-29 13:29:42 -070010504 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070010505 ALOGD("%s: returning existing instance of adev", __func__);
10506 ALOGD("%s: exit", __func__);
10507 pthread_mutex_unlock(&adev_init_lock);
10508 return 0;
10509 }
10510
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010511 adev = calloc(1, sizeof(struct audio_device));
10512
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -070010513 if (!adev) {
10514 pthread_mutex_unlock(&adev_init_lock);
10515 return -ENOMEM;
10516 }
10517
Ravi Kumar Alamanda40703102014-04-24 10:34:41 -070010518 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
10519
Weiyin Jiange6ce6312019-01-28 18:28:22 +080010520 // register audio ext hidl at the earliest
10521 audio_extn_hidl_init();
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053010522#ifdef DYNAMIC_LOG_ENABLED
10523 register_for_dynamic_logging("hal");
10524#endif
10525
Derek Chenf939fb72018-11-13 13:34:41 -080010526 /* default audio HAL major version */
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010527 uint32_t maj_version = 3;
Derek Chenf939fb72018-11-13 13:34:41 -080010528 if(property_get("vendor.audio.hal.maj.version", value, NULL))
10529 maj_version = atoi(value);
10530
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010531 adev->device.common.tag = HARDWARE_DEVICE_TAG;
Derek Chenf939fb72018-11-13 13:34:41 -080010532 adev->device.common.version = HARDWARE_DEVICE_API_VERSION(maj_version, 0);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010533 adev->device.common.module = (struct hw_module_t *)module;
10534 adev->device.common.close = adev_close;
10535
10536 adev->device.init_check = adev_init_check;
10537 adev->device.set_voice_volume = adev_set_voice_volume;
10538 adev->device.set_master_volume = adev_set_master_volume;
10539 adev->device.get_master_volume = adev_get_master_volume;
10540 adev->device.set_master_mute = adev_set_master_mute;
10541 adev->device.get_master_mute = adev_get_master_mute;
10542 adev->device.set_mode = adev_set_mode;
10543 adev->device.set_mic_mute = adev_set_mic_mute;
10544 adev->device.get_mic_mute = adev_get_mic_mute;
10545 adev->device.set_parameters = adev_set_parameters;
10546 adev->device.get_parameters = adev_get_parameters;
10547 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
10548 adev->device.open_output_stream = adev_open_output_stream;
10549 adev->device.close_output_stream = adev_close_output_stream;
10550 adev->device.open_input_stream = adev_open_input_stream;
10551 adev->device.close_input_stream = adev_close_input_stream;
Siddartha Shaik31b530e2017-05-19 15:26:33 +053010552 adev->device.create_audio_patch = adev_create_audio_patch;
10553 adev->device.release_audio_patch = adev_release_audio_patch;
10554 adev->device.get_audio_port = adev_get_audio_port;
10555 adev->device.set_audio_port_config = adev_set_audio_port_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010556 adev->device.dump = adev_dump;
Naresh Tannirudcb47c52018-06-25 16:23:32 +053010557 adev->device.get_microphones = adev_get_microphones;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010558
10559 /* Set the default route before the PCM stream is opened */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010560 adev->mode = AUDIO_MODE_NORMAL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -080010561 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010562 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010563 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -080010564 adev->acdb_settings = TTY_MODE_OFF;
vivek mehta344576a2016-04-12 18:56:03 -070010565 adev->allow_afe_proxy_usage = true;
Ashish Jain1b9b30c2017-05-18 20:57:40 +053010566 adev->bt_sco_on = false;
Eric Laurent07eeafd2013-10-06 12:52:49 -070010567 /* adev->cur_hdmi_channels = 0; by calloc() */
Eric Laurentb23d5282013-05-14 15:27:20 -070010568 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
vivek mehtaae1018c2019-05-09 12:19:57 -070010569 /* Init audio and voice feature */
10570 audio_extn_feature_init();
10571 voice_extn_feature_init();
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -070010572 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -080010573 list_init(&adev->usecase_list);
Derek Chenf939fb72018-11-13 13:34:41 -080010574 list_init(&adev->active_inputs_list);
10575 list_init(&adev->active_outputs_list);
Rahul Sharma99770982019-03-06 17:05:26 +053010576 list_init(&adev->audio_patch_record_list);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010577 adev->io_streams_map = hashmapCreate(AUDIO_IO_PORTS_MAX, audio_extn_utils_hash_fn,
10578 audio_extn_utils_hash_eq);
10579 if (!adev->io_streams_map) {
10580 ALOGE("%s: Could not create io streams map", __func__);
10581 ret = -ENOMEM;
10582 goto adev_open_err;
10583 }
10584 adev->patch_map = hashmapCreate(AUDIO_PATCH_PORTS_MAX, audio_extn_utils_hash_fn,
10585 audio_extn_utils_hash_eq);
10586 if (!adev->patch_map) {
10587 ALOGE("%s: Could not create audio patch map", __func__);
10588 ret = -ENOMEM;
10589 goto adev_open_err;
10590 }
Krishnankutty Kolathappilly0b2de1c2014-02-14 14:45:49 -080010591 adev->cur_wfd_channels = 2;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -070010592 adev->offload_usecases_state = 0;
Samyak Jain15fda662018-12-18 16:40:52 +053010593 adev->pcm_record_uc_state = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +053010594 adev->is_channel_status_set = false;
Sudheer Papothifa9d2282015-09-17 01:53:25 +053010595 adev->perf_lock_opts[0] = 0x101;
10596 adev->perf_lock_opts[1] = 0x20E;
10597 adev->perf_lock_opts_size = 2;
Xiaojun Sang785b5da2017-08-03 15:52:29 +080010598 adev->dsp_bit_width_enforce_mode = 0;
Aalique Grahame552b0832019-03-11 10:16:38 -070010599 adev->enable_hfp = false;
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +053010600 adev->use_old_pspd_mix_ctrl = false;
Aniket Kumar Lata60586a92019-05-22 22:18:55 -070010601 adev->adm_routing_changed = false;
Revathi Uddarajub26e3932020-06-10 14:51:02 +053010602 adev->a2dp_started = false;
Naresh Tanniru4c630392014-05-12 01:05:52 +053010603
Zhou Song68ebc352019-12-05 17:11:15 +080010604 audio_extn_perf_lock_init();
10605
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010606 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -070010607 adev->platform = platform_init(adev);
10608 if (!adev->platform) {
Eric Laurentb23d5282013-05-14 15:27:20 -070010609 ALOGE("%s: Failed to init platform data, aborting.", __func__);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010610 ret = -EINVAL;
10611 goto adev_open_err;
Eric Laurentb23d5282013-05-14 15:27:20 -070010612 }
Eric Laurentc4aef752013-09-12 17:45:53 -070010613
Aalique Grahame22e49102018-12-18 14:23:57 -080010614 adev->extspk = audio_extn_extspk_init(adev);
Sidipotu Ashokd2f10ba2019-05-06 15:41:56 +053010615 if (audio_extn_qap_is_enabled()) {
10616 ret = audio_extn_qap_init(adev);
10617 if (ret < 0) {
Sidipotu Ashokd2f10ba2019-05-06 15:41:56 +053010618 ALOGE("%s: Failed to init platform data, aborting.", __func__);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010619 goto adev_open_err;
Sidipotu Ashokd2f10ba2019-05-06 15:41:56 +053010620 }
10621 adev->device.open_output_stream = audio_extn_qap_open_output_stream;
10622 adev->device.close_output_stream = audio_extn_qap_close_output_stream;
10623 }
Aalique Grahame22e49102018-12-18 14:23:57 -080010624
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +053010625 if (audio_extn_qaf_is_enabled()) {
10626 ret = audio_extn_qaf_init(adev);
10627 if (ret < 0) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +053010628 ALOGE("%s: Failed to init platform data, aborting.", __func__);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010629 goto adev_open_err;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +053010630 }
10631
10632 adev->device.open_output_stream = audio_extn_qaf_open_output_stream;
10633 adev->device.close_output_stream = audio_extn_qaf_close_output_stream;
10634 }
10635
Derek Chenae7b0342019-02-08 15:17:04 -080010636 audio_extn_auto_hal_init(adev);
Derek Chend2530072014-11-24 12:39:14 -080010637 adev->ext_hw_plugin = audio_extn_ext_hw_plugin_init(adev);
10638
Eric Laurentc4aef752013-09-12 17:45:53 -070010639 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
10640 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
10641 if (adev->visualizer_lib == NULL) {
10642 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
10643 } else {
10644 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
10645 adev->visualizer_start_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -080010646 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -070010647 "visualizer_hal_start_output");
10648 adev->visualizer_stop_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -080010649 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -070010650 "visualizer_hal_stop_output");
10651 }
10652 }
Dhanalakshmi Siddani21be3ac2016-12-29 14:31:08 +053010653 audio_extn_init(adev);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -080010654 voice_extn_init(adev);
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -080010655 audio_extn_listen_init(adev, adev->snd_card);
Weiyin Jiangaa80acd2016-09-21 16:42:11 +080010656 audio_extn_gef_init(adev);
Siddartha Shaik44dd7702017-06-14 12:13:25 +053010657 audio_extn_hw_loopback_init(adev);
Garmond Leunge2433c32017-09-28 21:51:22 -070010658 audio_extn_ffv_init(adev);
Eric Laurentc4aef752013-09-12 17:45:53 -070010659
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -080010660 if (access(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, R_OK) == 0) {
10661 adev->offload_effects_lib = dlopen(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, RTLD_NOW);
10662 if (adev->offload_effects_lib == NULL) {
10663 ALOGE("%s: DLOPEN failed for %s", __func__,
10664 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
10665 } else {
10666 ALOGV("%s: DLOPEN successful for %s", __func__,
10667 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
10668 adev->offload_effects_start_output =
Ashish Jain5106d362016-05-11 19:23:33 +053010669 (int (*)(audio_io_handle_t, int, struct mixer *))dlsym(adev->offload_effects_lib,
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -080010670 "offload_effects_bundle_hal_start_output");
10671 adev->offload_effects_stop_output =
10672 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
10673 "offload_effects_bundle_hal_stop_output");
Jitendra Naruka1b6513f2014-11-22 19:34:13 -080010674 adev->offload_effects_set_hpx_state =
10675 (int (*)(bool))dlsym(adev->offload_effects_lib,
10676 "offload_effects_bundle_set_hpx_state");
Dhananjay Kumard68883d2015-09-04 13:39:26 +053010677 adev->offload_effects_get_parameters =
10678 (void (*)(struct str_parms *, struct str_parms *))
10679 dlsym(adev->offload_effects_lib,
10680 "offload_effects_bundle_get_parameters");
10681 adev->offload_effects_set_parameters =
10682 (void (*)(struct str_parms *))dlsym(adev->offload_effects_lib,
10683 "offload_effects_bundle_set_parameters");
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -080010684 }
10685 }
10686
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -070010687 if (access(ADM_LIBRARY_PATH, R_OK) == 0) {
10688 adev->adm_lib = dlopen(ADM_LIBRARY_PATH, RTLD_NOW);
10689 if (adev->adm_lib == NULL) {
10690 ALOGE("%s: DLOPEN failed for %s", __func__, ADM_LIBRARY_PATH);
10691 } else {
10692 ALOGV("%s: DLOPEN successful for %s", __func__, ADM_LIBRARY_PATH);
10693 adev->adm_init = (adm_init_t)
10694 dlsym(adev->adm_lib, "adm_init");
10695 adev->adm_deinit = (adm_deinit_t)
10696 dlsym(adev->adm_lib, "adm_deinit");
10697 adev->adm_register_input_stream = (adm_register_input_stream_t)
10698 dlsym(adev->adm_lib, "adm_register_input_stream");
10699 adev->adm_register_output_stream = (adm_register_output_stream_t)
10700 dlsym(adev->adm_lib, "adm_register_output_stream");
10701 adev->adm_deregister_stream = (adm_deregister_stream_t)
10702 dlsym(adev->adm_lib, "adm_deregister_stream");
10703 adev->adm_request_focus = (adm_request_focus_t)
10704 dlsym(adev->adm_lib, "adm_request_focus");
10705 adev->adm_abandon_focus = (adm_abandon_focus_t)
10706 dlsym(adev->adm_lib, "adm_abandon_focus");
Haynes Mathew George5beddd42016-06-27 18:33:40 -070010707 adev->adm_set_config = (adm_set_config_t)
10708 dlsym(adev->adm_lib, "adm_set_config");
10709 adev->adm_request_focus_v2 = (adm_request_focus_v2_t)
10710 dlsym(adev->adm_lib, "adm_request_focus_v2");
10711 adev->adm_is_noirq_avail = (adm_is_noirq_avail_t)
10712 dlsym(adev->adm_lib, "adm_is_noirq_avail");
10713 adev->adm_on_routing_change = (adm_on_routing_change_t)
10714 dlsym(adev->adm_lib, "adm_on_routing_change");
Aniket Kumar Lata60586a92019-05-22 22:18:55 -070010715 adev->adm_request_focus_v2_1 = (adm_request_focus_v2_1_t)
10716 dlsym(adev->adm_lib, "adm_request_focus_v2_1");
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -070010717 }
10718 }
10719
Aalique Grahame22e49102018-12-18 14:23:57 -080010720 adev->enable_voicerx = false;
Mingming Yin514a8bc2014-07-29 15:22:21 -070010721 adev->bt_wb_speech_enabled = false;
Zhou Song12c29502019-03-16 10:37:18 +080010722 adev->swb_speech_mode = SPEECH_MODE_INVALID;
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -080010723 //initialize this to false for now,
10724 //this will be set to true through set param
10725 adev->vr_audio_mode_enabled = false;
Mingming Yin514a8bc2014-07-29 15:22:21 -070010726
Pradnya Chaphekar8a9dcd82014-09-09 09:49:10 -070010727 audio_extn_ds2_enable(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010728 *device = &adev->device.common;
Aalique Grahame22e49102018-12-18 14:23:57 -080010729
10730 if (k_enable_extended_precision)
10731 adev_verify_devices(adev);
10732
Xiaojun Sang785b5da2017-08-03 15:52:29 +080010733 adev->dsp_bit_width_enforce_mode =
10734 adev_init_dsp_bit_width_enforce_mode(adev->mixer);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010735
Dhananjay Kumard6d32152016-10-13 16:11:03 +053010736 audio_extn_utils_update_streams_cfg_lists(adev->platform, adev->mixer,
10737 &adev->streams_output_cfg_list,
10738 &adev->streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070010739
Kiran Kandi910e1862013-10-29 13:29:42 -070010740 audio_device_ref_count++;
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -070010741
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -070010742 int trial;
Manisha Agarwalc75a0202019-12-06 18:48:25 +053010743 if (property_get("vendor.audio_hal.period_size", value, NULL) > 0) {
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -070010744 trial = atoi(value);
10745 if (period_size_is_plausible_for_low_latency(trial)) {
10746 pcm_config_low_latency.period_size = trial;
10747 pcm_config_low_latency.start_threshold = trial / 4;
10748 pcm_config_low_latency.avail_min = trial / 4;
10749 configured_low_latency_capture_period_size = trial;
10750 }
10751 }
Aalique Grahame22e49102018-12-18 14:23:57 -080010752 if ((property_get("vendor.audio_hal.in_period_size", value, NULL) > 0) ||
10753 (property_get("audio_hal.in_period_size", value, NULL) > 0)) {
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -070010754 trial = atoi(value);
10755 if (period_size_is_plausible_for_low_latency(trial)) {
10756 configured_low_latency_capture_period_size = trial;
10757 }
10758 }
10759
Vignesh Kulothungan7d374312018-02-21 17:12:00 -080010760 adev->mic_break_enabled = property_get_bool("vendor.audio.mic_break", false);
10761
Eric Laurent4b084132018-10-19 17:33:43 -070010762 adev->camera_orientation = CAMERA_DEFAULT;
10763
Manisha Agarwalc75a0202019-12-06 18:48:25 +053010764 if (property_get("vendor.audio_hal.period_multiplier",value,NULL) > 0) {
Haynes Mathew George5beddd42016-06-27 18:33:40 -070010765 af_period_multiplier = atoi(value);
10766 if (af_period_multiplier < 0)
10767 af_period_multiplier = 2;
10768 else if (af_period_multiplier > 4)
10769 af_period_multiplier = 4;
10770
10771 ALOGV("new period_multiplier = %d", af_period_multiplier);
10772 }
10773
Arun Mirpurib1bec9c2019-01-29 16:42:45 -080010774 audio_extn_qdsp_init(adev->platform);
Aalique Grahame22e49102018-12-18 14:23:57 -080010775
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -070010776 adev->multi_offload_enable = property_get_bool("vendor.audio.offload.multiple.enabled", false);
Zhou Song62ea0282020-03-22 19:53:01 +080010777 adev->ha_proxy_enable = property_get_bool("persist.vendor.audio.ha_proxy.enabled", false);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070010778 pthread_mutex_unlock(&adev_init_lock);
10779
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -070010780 if (adev->adm_init)
10781 adev->adm_data = adev->adm_init();
10782
Dhananjay Kumaree4d2002016-10-25 18:02:58 +053010783 qahwi_init(*device);
Ben Rombergerd771a7c2017-02-22 18:05:17 -080010784 audio_extn_adsp_hdlr_init(adev->mixer);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053010785
10786 audio_extn_snd_mon_init();
10787 pthread_mutex_lock(&adev->lock);
10788 audio_extn_snd_mon_register_listener(adev, adev_snd_mon_cb);
10789 adev->card_status = CARD_STATUS_ONLINE;
Haynes Mathew George01156f92018-04-13 15:29:54 -070010790 audio_extn_battery_properties_listener_init(adev_on_battery_status_changed);
10791 /*
10792 * if the battery state callback happens before charging can be queried,
10793 * it will be guarded with the adev->lock held in the cb function and so
10794 * the callback value will reflect the latest state
10795 */
10796 adev->is_charging = audio_extn_battery_properties_is_charging();
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053010797 audio_extn_sound_trigger_init(adev); /* dependent on snd_mon_init() */
Zhou Songc1088ea2018-06-12 00:17:29 +080010798 audio_extn_sound_trigger_update_battery_status(adev->is_charging);
justinweng20fb6d82019-02-21 18:49:00 -070010799 audio_extn_audiozoom_init();
Zhou Songc1088ea2018-06-12 00:17:29 +080010800 pthread_mutex_unlock(&adev->lock);
Satish Babu Patakokilac3c5d432017-07-04 22:48:59 +053010801 /* Allocate memory for Device config params */
10802 adev->device_cfg_params = (struct audio_device_config_param*)
10803 calloc(platform_get_max_codec_backend(),
10804 sizeof(struct audio_device_config_param));
10805 if (adev->device_cfg_params == NULL)
10806 ALOGE("%s: Memory allocation failed for Device config params", __func__);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053010807
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +053010808 /*
10809 * Check if new PSPD matrix mixer control is supported. If not
10810 * supported, then set flag so that old mixer ctrl is sent while
10811 * sending pspd coefficients on older kernel version. Query mixer
10812 * control for default pcm id and channel value one.
10813 */
10814 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
10815 "AudStr %d ChMixer Weight Ch %d", 0, 1);
10816
10817 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
10818 if (!ctl) {
10819 ALOGE("%s: ERROR. Could not get ctl for mixer cmd - %s",
10820 __func__, mixer_ctl_name);
10821 adev->use_old_pspd_mix_ctrl = true;
10822 }
10823
Eric Laurent994a6932013-07-17 11:51:42 -070010824 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010825 return 0;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010826
10827adev_open_err:
10828 free_map(adev->patch_map);
10829 free_map(adev->io_streams_map);
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010830 free(adev->snd_dev_ref_cnt);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010831 pthread_mutex_destroy(&adev->lock);
10832 free(adev);
10833 adev = NULL;
10834 *device = NULL;
10835 pthread_mutex_unlock(&adev_init_lock);
10836 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010837}
10838
10839static struct hw_module_methods_t hal_module_methods = {
10840 .open = adev_open,
10841};
10842
10843struct audio_module HAL_MODULE_INFO_SYM = {
10844 .common = {
10845 .tag = HARDWARE_MODULE_TAG,
10846 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
10847 .hal_api_version = HARDWARE_HAL_API_VERSION,
10848 .id = AUDIO_HARDWARE_MODULE_ID,
10849 .name = "QCOM Audio HAL",
Duy Truongfae19622013-11-24 02:17:54 -080010850 .author = "The Linux Foundation",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010851 .methods = &hal_module_methods,
10852 },
10853};