blob: 4f3af90f4fcf524a93bc255388f65c2365d55c3a [file] [log] [blame]
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001/*
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07002 * Copyright (c) 2013-2020, 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
Daniel Hillenbrand21752f02013-05-23 10:10:00 +053088#include "audio_amplifier.h"
89
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053090#ifdef DYNAMIC_LOG_ENABLED
91#include <log_xml_parser.h>
92#define LOG_MASK HAL_MOD_FILE_AUDIO_HW
93#include <log_utils.h>
94#endif
95
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070096#define COMPRESS_OFFLOAD_NUM_FRAGMENTS 4
Ashish Jain5106d362016-05-11 19:23:33 +053097/*DIRECT PCM has same buffer sizes as DEEP Buffer*/
98#define DIRECT_PCM_NUM_FRAGMENTS 2
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070099#define COMPRESS_PLAYBACK_VOLUME_MAX 0x2000
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700100#define VOIP_PLAYBACK_VOLUME_MAX 0x2000
Arun Mirpuri5d170872019-03-26 13:21:31 -0700101#define MMAP_PLAYBACK_VOLUME_MAX 0x2000
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +0530102#define PCM_PLAYBACK_VOLUME_MAX 0x2000
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530103#define DSD_VOLUME_MIN_DB (-110)
Aniket Kumar Lata8426d1f2019-06-10 15:25:53 -0700104#define INVALID_OUT_VOLUME -1
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700105#define AUDIO_IO_PORTS_MAX 32
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700106
Aalique Grahame22e49102018-12-18 14:23:57 -0800107#define RECORD_GAIN_MIN 0.0f
108#define RECORD_GAIN_MAX 1.0f
109#define RECORD_VOLUME_CTL_MAX 0x2000
110
111/* treat as unsigned Q1.13 */
112#define APP_TYPE_GAIN_DEFAULT 0x2000
113
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -0700114#define PROXY_OPEN_RETRY_COUNT 100
115#define PROXY_OPEN_WAIT_TIME 20
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -0800116
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800117#define GET_USECASE_AUDIO_PLAYBACK_PRIMARY(db) \
118 (db)? USECASE_AUDIO_PLAYBACK_DEEP_BUFFER : \
119 USECASE_AUDIO_PLAYBACK_LOW_LATENCY
120#define GET_PCM_CONFIG_AUDIO_PLAYBACK_PRIMARY(db) \
121 (db)? pcm_config_deep_buffer : pcm_config_low_latency
Haynes Mathew Georgebf143712013-12-03 13:02:53 -0800122
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700123#define ULL_PERIOD_SIZE (DEFAULT_OUTPUT_SAMPLING_RATE/1000)
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700124#define DEFAULT_VOIP_BUF_DURATION_MS 20
125#define DEFAULT_VOIP_BIT_DEPTH_BYTE sizeof(int16_t)
126#define DEFAULT_VOIP_SAMP_RATE 48000
127
128#define VOIP_IO_BUF_SIZE(SR, DURATION_MS, BIT_DEPTH) (SR)/1000 * DURATION_MS * BIT_DEPTH
129
130struct pcm_config default_pcm_config_voip_copp = {
131 .channels = 1,
132 .rate = DEFAULT_VOIP_SAMP_RATE, /* changed when the stream is opened */
133 .period_size = VOIP_IO_BUF_SIZE(DEFAULT_VOIP_SAMP_RATE, DEFAULT_VOIP_BUF_DURATION_MS, DEFAULT_VOIP_BIT_DEPTH_BYTE)/2,
134 .period_count = 2,
135 .format = PCM_FORMAT_S16_LE,
kunleiz95b597a2017-10-23 17:07:33 +0800136 .avail_min = VOIP_IO_BUF_SIZE(DEFAULT_VOIP_SAMP_RATE, DEFAULT_VOIP_BUF_DURATION_MS, DEFAULT_VOIP_BIT_DEPTH_BYTE)/2,
137 .stop_threshold = INT_MAX,
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700138};
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700139
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700140#define MIN_CHANNEL_COUNT 1
141#define DEFAULT_CHANNEL_COUNT 2
142#define MAX_HIFI_CHANNEL_COUNT 8
143
Aalique Grahame22e49102018-12-18 14:23:57 -0800144#ifndef MAX_TARGET_SPECIFIC_CHANNEL_CNT
145#define MAX_CHANNEL_COUNT 1
146#else
147#define MAX_CHANNEL_COUNT atoi(XSTR(MAX_TARGET_SPECIFIC_CHANNEL_CNT))
148#define XSTR(x) STR(x)
149#define STR(x) #x
150#endif
151
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -0700152static unsigned int configured_low_latency_capture_period_size =
153 LOW_LATENCY_CAPTURE_PERIOD_SIZE;
154
Haynes Mathew George16081042017-05-31 17:16:49 -0700155#define MMAP_PERIOD_SIZE (DEFAULT_OUTPUT_SAMPLING_RATE/1000)
156#define MMAP_PERIOD_COUNT_MIN 32
157#define MMAP_PERIOD_COUNT_MAX 512
158#define MMAP_PERIOD_COUNT_DEFAULT (MMAP_PERIOD_COUNT_MAX)
159
Aalique Grahame22e49102018-12-18 14:23:57 -0800160/* This constant enables extended precision handling.
161 * TODO The flag is off until more testing is done.
162 */
163static const bool k_enable_extended_precision = false;
Arun Mirpurie008ed22019-03-21 11:21:04 -0700164extern int AUDIO_DEVICE_IN_ALL_CODEC_BACKEND;
Aalique Grahame22e49102018-12-18 14:23:57 -0800165
Eric Laurentb23d5282013-05-14 15:27:20 -0700166struct pcm_config pcm_config_deep_buffer = {
167 .channels = 2,
168 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
169 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
170 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
171 .format = PCM_FORMAT_S16_LE,
172 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
173 .stop_threshold = INT_MAX,
174 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
175};
176
177struct pcm_config pcm_config_low_latency = {
178 .channels = 2,
179 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
180 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
181 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
182 .format = PCM_FORMAT_S16_LE,
183 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
184 .stop_threshold = INT_MAX,
185 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
186};
187
Vignesh Kulothungana6927272019-02-20 15:17:07 -0800188struct pcm_config pcm_config_haptics_audio = {
189 .channels = 1,
190 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
191 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
192 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
193 .format = PCM_FORMAT_S16_LE,
194 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
195 .stop_threshold = INT_MAX,
196 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
197};
198
199struct pcm_config pcm_config_haptics = {
200 .channels = 1,
201 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
202 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
203 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
204 .format = PCM_FORMAT_S16_LE,
205 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
206 .stop_threshold = INT_MAX,
207 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
208};
209
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700210static int af_period_multiplier = 4;
211struct pcm_config pcm_config_rt = {
212 .channels = 2,
213 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
214 .period_size = ULL_PERIOD_SIZE, //1 ms
215 .period_count = 512, //=> buffer size is 512ms
216 .format = PCM_FORMAT_S16_LE,
217 .start_threshold = ULL_PERIOD_SIZE*8, //8ms
218 .stop_threshold = INT_MAX,
219 .silence_threshold = 0,
220 .silence_size = 0,
221 .avail_min = ULL_PERIOD_SIZE, //1 ms
222};
223
Eric Laurentb23d5282013-05-14 15:27:20 -0700224struct pcm_config pcm_config_hdmi_multi = {
225 .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
226 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
227 .period_size = HDMI_MULTI_PERIOD_SIZE,
228 .period_count = HDMI_MULTI_PERIOD_COUNT,
229 .format = PCM_FORMAT_S16_LE,
230 .start_threshold = 0,
231 .stop_threshold = INT_MAX,
232 .avail_min = 0,
233};
234
Haynes Mathew George16081042017-05-31 17:16:49 -0700235struct pcm_config pcm_config_mmap_playback = {
236 .channels = 2,
237 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
238 .period_size = MMAP_PERIOD_SIZE,
239 .period_count = MMAP_PERIOD_COUNT_DEFAULT,
240 .format = PCM_FORMAT_S16_LE,
241 .start_threshold = MMAP_PERIOD_SIZE*8,
242 .stop_threshold = INT32_MAX,
243 .silence_threshold = 0,
244 .silence_size = 0,
245 .avail_min = MMAP_PERIOD_SIZE, //1 ms
246};
247
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700248struct pcm_config pcm_config_hifi = {
249 .channels = DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
250 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
251 .period_size = HIFI_BUFFER_OUTPUT_PERIOD_SIZE, /* change #define */
252 .period_count = HIFI_BUFFER_OUTPUT_PERIOD_COUNT,
253 .format = PCM_FORMAT_S24_3LE,
254 .start_threshold = 0,
255 .stop_threshold = INT_MAX,
256 .avail_min = 0,
257};
258
Eric Laurentb23d5282013-05-14 15:27:20 -0700259struct pcm_config pcm_config_audio_capture = {
260 .channels = 2,
Eric Laurentb23d5282013-05-14 15:27:20 -0700261 .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
262 .format = PCM_FORMAT_S16_LE,
263};
264
Haynes Mathew George16081042017-05-31 17:16:49 -0700265struct pcm_config pcm_config_mmap_capture = {
266 .channels = 2,
267 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
268 .period_size = MMAP_PERIOD_SIZE,
269 .period_count = MMAP_PERIOD_COUNT_DEFAULT,
270 .format = PCM_FORMAT_S16_LE,
271 .start_threshold = 0,
272 .stop_threshold = INT_MAX,
273 .silence_threshold = 0,
274 .silence_size = 0,
275 .avail_min = MMAP_PERIOD_SIZE, //1 ms
276};
277
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -0700278#define AFE_PROXY_CHANNEL_COUNT 2
279#define AFE_PROXY_SAMPLING_RATE 48000
280
281#define AFE_PROXY_PLAYBACK_PERIOD_SIZE 768
282#define AFE_PROXY_PLAYBACK_PERIOD_COUNT 4
283
284struct pcm_config pcm_config_afe_proxy_playback = {
285 .channels = AFE_PROXY_CHANNEL_COUNT,
286 .rate = AFE_PROXY_SAMPLING_RATE,
287 .period_size = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
288 .period_count = AFE_PROXY_PLAYBACK_PERIOD_COUNT,
289 .format = PCM_FORMAT_S16_LE,
290 .start_threshold = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
291 .stop_threshold = INT_MAX,
292 .avail_min = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
293};
294
295#define AFE_PROXY_RECORD_PERIOD_SIZE 768
296#define AFE_PROXY_RECORD_PERIOD_COUNT 4
297
Aalique Grahame22e49102018-12-18 14:23:57 -0800298struct pcm_config pcm_config_audio_capture_rt = {
299 .channels = 2,
300 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
301 .period_size = ULL_PERIOD_SIZE,
302 .period_count = 512,
303 .format = PCM_FORMAT_S16_LE,
304 .start_threshold = 0,
305 .stop_threshold = AFE_PROXY_RECORD_PERIOD_SIZE * AFE_PROXY_RECORD_PERIOD_COUNT,
306 .silence_threshold = 0,
307 .silence_size = 0,
308 .avail_min = ULL_PERIOD_SIZE, //1 ms
309};
310
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -0700311struct pcm_config pcm_config_afe_proxy_record = {
312 .channels = AFE_PROXY_CHANNEL_COUNT,
313 .rate = AFE_PROXY_SAMPLING_RATE,
314 .period_size = AFE_PROXY_RECORD_PERIOD_SIZE,
315 .period_count = AFE_PROXY_RECORD_PERIOD_COUNT,
316 .format = PCM_FORMAT_S16_LE,
317 .start_threshold = AFE_PROXY_RECORD_PERIOD_SIZE,
318 .stop_threshold = INT_MAX,
319 .avail_min = AFE_PROXY_RECORD_PERIOD_SIZE,
320};
321
Ashish Jainf1eaa582016-05-23 20:54:24 +0530322#define AUDIO_MAX_PCM_FORMATS 7
323
324const uint32_t format_to_bitwidth_table[AUDIO_MAX_PCM_FORMATS] = {
325 [AUDIO_FORMAT_DEFAULT] = 0,
326 [AUDIO_FORMAT_PCM_16_BIT] = sizeof(uint16_t),
327 [AUDIO_FORMAT_PCM_8_BIT] = sizeof(uint8_t),
328 [AUDIO_FORMAT_PCM_32_BIT] = sizeof(uint32_t),
329 [AUDIO_FORMAT_PCM_8_24_BIT] = sizeof(uint32_t),
330 [AUDIO_FORMAT_PCM_FLOAT] = sizeof(float),
331 [AUDIO_FORMAT_PCM_24_BIT_PACKED] = sizeof(uint8_t) * 3,
332};
333
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800334const char * const use_case_table[AUDIO_USECASE_MAX] = {
Eric Laurentb23d5282013-05-14 15:27:20 -0700335 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
336 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
Vignesh Kulothungana6927272019-02-20 15:17:07 -0800337 [USECASE_AUDIO_PLAYBACK_WITH_HAPTICS] = "audio-with-haptics-playback",
Meng Wang51d8c2a2020-04-27 15:23:21 +0800338 [USECASE_AUDIO_PLAYBACK_HAPTICS] = "haptics-playback",
Ravi Kumar Alamanda474de5a2015-06-25 20:08:01 -0700339 [USECASE_AUDIO_PLAYBACK_ULL] = "audio-ull-playback",
340 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback",
Shruthi Krishnaace10852013-10-25 14:32:12 -0700341 [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback",
vivek mehta446c3962015-09-14 10:57:35 -0700342 //Enabled for Direct_PCM
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700343 [USECASE_AUDIO_PLAYBACK_OFFLOAD2] = "compress-offload-playback2",
344 [USECASE_AUDIO_PLAYBACK_OFFLOAD3] = "compress-offload-playback3",
345 [USECASE_AUDIO_PLAYBACK_OFFLOAD4] = "compress-offload-playback4",
346 [USECASE_AUDIO_PLAYBACK_OFFLOAD5] = "compress-offload-playback5",
347 [USECASE_AUDIO_PLAYBACK_OFFLOAD6] = "compress-offload-playback6",
348 [USECASE_AUDIO_PLAYBACK_OFFLOAD7] = "compress-offload-playback7",
349 [USECASE_AUDIO_PLAYBACK_OFFLOAD8] = "compress-offload-playback8",
350 [USECASE_AUDIO_PLAYBACK_OFFLOAD9] = "compress-offload-playback9",
Haynes Mathew George16081042017-05-31 17:16:49 -0700351 [USECASE_AUDIO_PLAYBACK_FM] = "play-fm",
352 [USECASE_AUDIO_PLAYBACK_MMAP] = "mmap-playback",
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700353 [USECASE_AUDIO_PLAYBACK_HIFI] = "hifi-playback",
Aalique Grahame22e49102018-12-18 14:23:57 -0800354 [USECASE_AUDIO_PLAYBACK_TTS] = "audio-tts-playback",
vivek mehta0ea887a2015-08-26 14:01:20 -0700355
Eric Laurentb23d5282013-05-14 15:27:20 -0700356 [USECASE_AUDIO_RECORD] = "audio-record",
Mingming Yine62d7842013-10-25 16:26:03 -0700357 [USECASE_AUDIO_RECORD_COMPRESS] = "audio-record-compress",
Dhananjay Kumaree4d2002016-10-25 18:02:58 +0530358 [USECASE_AUDIO_RECORD_COMPRESS2] = "audio-record-compress2",
359 [USECASE_AUDIO_RECORD_COMPRESS3] = "audio-record-compress3",
360 [USECASE_AUDIO_RECORD_COMPRESS4] = "audio-record-compress4",
Dhananjay Kumar376e38b2017-09-28 22:26:23 +0530361 [USECASE_AUDIO_RECORD_COMPRESS5] = "audio-record-compress5",
362 [USECASE_AUDIO_RECORD_COMPRESS6] = "audio-record-compress6",
Eric Laurentb23d5282013-05-14 15:27:20 -0700363 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
Preetam Singh Ranawatde84f1a2013-11-01 14:58:16 -0700364 [USECASE_AUDIO_RECORD_FM_VIRTUAL] = "fm-virtual-record",
Haynes Mathew George16081042017-05-31 17:16:49 -0700365 [USECASE_AUDIO_RECORD_MMAP] = "mmap-record",
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700366 [USECASE_AUDIO_RECORD_HIFI] = "hifi-record",
Haynes Mathew George16081042017-05-31 17:16:49 -0700367
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -0800368 [USECASE_AUDIO_HFP_SCO] = "hfp-sco",
Vimal Puthanveed47e64852013-12-20 13:23:39 -0800369 [USECASE_AUDIO_HFP_SCO_WB] = "hfp-sco-wb",
Derek Chenf7092792017-05-23 12:23:53 -0400370 [USECASE_AUDIO_HFP_SCO_DOWNLINK] = "hfp-sco-downlink",
371 [USECASE_AUDIO_HFP_SCO_WB_DOWNLINK] = "hfp-sco-wb-downlink",
Mingming Yin3ee55c62014-08-04 14:23:35 -0700372
Derek Chenf7092792017-05-23 12:23:53 -0400373 [USECASE_VOICE_CALL] = "voice-call",
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700374 [USECASE_VOICE2_CALL] = "voice2-call",
375 [USECASE_VOLTE_CALL] = "volte-call",
376 [USECASE_QCHAT_CALL] = "qchat-call",
Vicky Sehrawat7e4fc152014-02-12 17:58:59 -0800377 [USECASE_VOWLAN_CALL] = "vowlan-call",
Vidyakumar Athota0e109352015-02-12 17:38:22 -0800378 [USECASE_VOICEMMODE1_CALL] = "voicemmode1-call",
379 [USECASE_VOICEMMODE2_CALL] = "voicemmode2-call",
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800380 [USECASE_COMPRESS_VOIP_CALL] = "compress-voip-call",
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700381 [USECASE_INCALL_REC_UPLINK] = "incall-rec-uplink",
382 [USECASE_INCALL_REC_DOWNLINK] = "incall-rec-downlink",
383 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK] = "incall-rec-uplink-and-downlink",
Helen Zenge56b4852013-12-03 16:54:40 -0800384 [USECASE_INCALL_REC_UPLINK_COMPRESS] = "incall-rec-uplink-compress",
385 [USECASE_INCALL_REC_DOWNLINK_COMPRESS] = "incall-rec-downlink-compress",
386 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK_COMPRESS] = "incall-rec-uplink-and-downlink-compress",
387
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -0700388 [USECASE_INCALL_MUSIC_UPLINK] = "incall_music_uplink",
389 [USECASE_INCALL_MUSIC_UPLINK2] = "incall_music_uplink2",
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700390 [USECASE_AUDIO_SPKR_CALIB_RX] = "spkr-rx-calib",
391 [USECASE_AUDIO_SPKR_CALIB_TX] = "spkr-vi-record",
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -0700392
393 [USECASE_AUDIO_PLAYBACK_AFE_PROXY] = "afe-proxy-playback",
394 [USECASE_AUDIO_RECORD_AFE_PROXY] = "afe-proxy-record",
Zhou Song62ea0282020-03-22 19:53:01 +0800395 [USECASE_AUDIO_RECORD_AFE_PROXY2] = "afe-proxy-record2",
Md Mansoor Ahmeddb1b4f92018-01-25 18:56:31 +0530396 [USECASE_AUDIO_PLAYBACK_SILENCE] = "silence-playback",
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700397
Siddartha Shaik31b530e2017-05-19 15:26:33 +0530398 /* Transcode loopback cases */
Surendar Karka93cd25a2018-08-28 14:21:37 +0530399 [USECASE_AUDIO_TRANSCODE_LOOPBACK_RX] = "audio-transcode-loopback-rx",
400 [USECASE_AUDIO_TRANSCODE_LOOPBACK_TX] = "audio-transcode-loopback-tx",
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700401
402 [USECASE_AUDIO_PLAYBACK_VOIP] = "audio-playback-voip",
403 [USECASE_AUDIO_RECORD_VOIP] = "audio-record-voip",
Varun Balaraje49253e2017-07-06 19:48:56 +0530404 /* For Interactive Audio Streams */
405 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM1] = "audio-interactive-stream1",
406 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM2] = "audio-interactive-stream2",
407 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM3] = "audio-interactive-stream3",
408 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM4] = "audio-interactive-stream4",
409 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM5] = "audio-interactive-stream5",
410 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM6] = "audio-interactive-stream6",
411 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM7] = "audio-interactive-stream7",
412 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM8] = "audio-interactive-stream8",
Garmond Leunge2433c32017-09-28 21:51:22 -0700413
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800414 [USECASE_AUDIO_EC_REF_LOOPBACK] = "ec-ref-audio-capture",
415
Derek Chenf6318be2017-06-12 17:16:24 -0400416 [USECASE_AUDIO_A2DP_ABR_FEEDBACK] = "a2dp-abr-feedback",
417
418 [USECASE_AUDIO_PLAYBACK_MEDIA] = "media-playback",
419 [USECASE_AUDIO_PLAYBACK_SYS_NOTIFICATION] = "sys-notification-playback",
420 [USECASE_AUDIO_PLAYBACK_NAV_GUIDANCE] = "nav-guidance-playback",
421 [USECASE_AUDIO_PLAYBACK_PHONE] = "phone-playback",
Derek Chen1bcdc6b2020-02-06 22:44:53 -0800422 [USECASE_AUDIO_PLAYBACK_FRONT_PASSENGER] = "front-passenger-playback",
Derek Chendf05eea2019-08-01 13:57:49 -0700423 [USECASE_AUDIO_PLAYBACK_REAR_SEAT] = "rear-seat-playback",
Rahul Sharma99770982019-03-06 17:05:26 +0530424 [USECASE_AUDIO_FM_TUNER_EXT] = "fm-tuner-ext",
Eric Laurentb23d5282013-05-14 15:27:20 -0700425};
426
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700427static const audio_usecase_t offload_usecases[] = {
428 USECASE_AUDIO_PLAYBACK_OFFLOAD,
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700429 USECASE_AUDIO_PLAYBACK_OFFLOAD2,
430 USECASE_AUDIO_PLAYBACK_OFFLOAD3,
431 USECASE_AUDIO_PLAYBACK_OFFLOAD4,
432 USECASE_AUDIO_PLAYBACK_OFFLOAD5,
433 USECASE_AUDIO_PLAYBACK_OFFLOAD6,
434 USECASE_AUDIO_PLAYBACK_OFFLOAD7,
435 USECASE_AUDIO_PLAYBACK_OFFLOAD8,
436 USECASE_AUDIO_PLAYBACK_OFFLOAD9,
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700437};
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800438
Varun Balaraje49253e2017-07-06 19:48:56 +0530439static const audio_usecase_t interactive_usecases[] = {
440 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM1,
441 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM2,
442 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM3,
443 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM4,
444 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM5,
445 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM6,
446 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM7,
447 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM8,
448};
449
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800450#define STRING_TO_ENUM(string) { #string, string }
451
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800452struct string_to_enum {
453 const char *name;
454 uint32_t value;
455};
456
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700457static const struct string_to_enum channels_name_to_enum_table[] = {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800458 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
Mingming Yin3a941d42016-02-17 18:08:05 -0800459 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_2POINT1),
460 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_QUAD),
461 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_SURROUND),
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -0700462 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_PENTA),
Mingming Yin3a941d42016-02-17 18:08:05 -0800463 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
464 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_6POINT1),
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800465 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700466 STRING_TO_ENUM(AUDIO_CHANNEL_IN_MONO),
467 STRING_TO_ENUM(AUDIO_CHANNEL_IN_STEREO),
468 STRING_TO_ENUM(AUDIO_CHANNEL_IN_FRONT_BACK),
469 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_1),
470 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_2),
471 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_3),
472 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_4),
473 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_5),
474 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_6),
475 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_7),
476 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_8),
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800477};
478
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700479static const struct string_to_enum formats_name_to_enum_table[] = {
480 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
481 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
482 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Pradnya Chaphekar8a9dcd82014-09-09 09:49:10 -0700483 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
484 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
485 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Ben Romberger1aaaf862017-04-06 17:49:46 -0700486 STRING_TO_ENUM(AUDIO_FORMAT_DOLBY_TRUEHD),
Mingming Yin3a941d42016-02-17 18:08:05 -0800487 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
488 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
Naresh Tanniru928f0862017-04-07 16:44:23 -0700489 STRING_TO_ENUM(AUDIO_FORMAT_IEC61937)
Mingming Yin3a941d42016-02-17 18:08:05 -0800490};
491
492//list of all supported sample rates by HDMI specification.
493static const int out_hdmi_sample_rates[] = {
494 32000, 44100, 48000, 88200, 96000, 176400, 192000,
495};
496
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700497static const struct string_to_enum out_sample_rates_name_to_enum_table[] = {
Mingming Yin3a941d42016-02-17 18:08:05 -0800498 STRING_TO_ENUM(32000),
499 STRING_TO_ENUM(44100),
500 STRING_TO_ENUM(48000),
501 STRING_TO_ENUM(88200),
502 STRING_TO_ENUM(96000),
503 STRING_TO_ENUM(176400),
504 STRING_TO_ENUM(192000),
Mingshu pange8aedf12019-10-25 15:38:27 +0800505 STRING_TO_ENUM(352800),
506 STRING_TO_ENUM(384000),
Pradnya Chaphekar8a9dcd82014-09-09 09:49:10 -0700507};
508
Carter Hsu2e429db2019-05-14 18:50:52 +0800509struct in_effect_list {
510 struct listnode list;
511 effect_handle_t handle;
512};
513
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700514static struct audio_device *adev = NULL;
Ben Romberger02ab1192018-05-24 12:10:08 -0700515static pthread_mutex_t adev_init_lock = PTHREAD_MUTEX_INITIALIZER;
Kiran Kandi910e1862013-10-29 13:29:42 -0700516static unsigned int audio_device_ref_count;
vivek mehtab72d08d2016-04-29 03:16:47 -0700517//cache last MBDRC cal step level
518static int last_known_cal_step = -1 ;
Kiran Kandi910e1862013-10-29 13:29:42 -0700519
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +0530520static int out_set_compr_volume(struct audio_stream_out *stream, float left, float right);
Arun Mirpuri5d170872019-03-26 13:21:31 -0700521static int out_set_mmap_volume(struct audio_stream_out *stream, float left, float right);
Zhou Song2b8f28f2017-09-11 10:51:38 +0800522static int out_set_voip_volume(struct audio_stream_out *stream, float left, float right);
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +0530523static int out_set_pcm_volume(struct audio_stream_out *stream, float left, float right);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +0530524
Derek Chen6f293672019-04-01 01:40:24 -0700525static void adev_snd_mon_cb(void *cookie, struct str_parms *parms);
526static void in_snd_mon_cb(void * stream, struct str_parms * parms);
527static void out_snd_mon_cb(void * stream, struct str_parms * parms);
528
Zhou Song331c8e52019-08-26 14:16:12 +0800529static int configure_btsco_sample_rate(snd_device_t snd_device);
530
Vatsal Buchac09ae062018-11-14 13:25:08 +0530531#ifdef AUDIO_FEATURE_ENABLED_GCOV
532extern void __gcov_flush();
533static void enable_gcov()
534{
535 __gcov_flush();
536}
537#else
538static void enable_gcov()
539{
540}
541#endif
542
justinweng20fb6d82019-02-21 18:49:00 -0700543static int in_set_microphone_direction(const struct audio_stream_in *stream,
544 audio_microphone_direction_t dir);
545static int in_set_microphone_field_dimension(const struct audio_stream_in *stream, float zoom);
546
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700547static bool may_use_noirq_mode(struct audio_device *adev, audio_usecase_t uc_id,
548 int flags __unused)
549{
550 int dir = 0;
551 switch (uc_id) {
552 case USECASE_AUDIO_RECORD_LOW_LATENCY:
553 dir = 1;
554 case USECASE_AUDIO_PLAYBACK_ULL:
555 break;
556 default:
557 return false;
558 }
559
560 int dev_id = platform_get_pcm_device_id(uc_id, dir == 0 ?
561 PCM_PLAYBACK : PCM_CAPTURE);
562 if (adev->adm_is_noirq_avail)
563 return adev->adm_is_noirq_avail(adev->adm_data,
564 adev->snd_card, dev_id, dir);
565 return false;
566}
567
568static void register_out_stream(struct stream_out *out)
569{
570 struct audio_device *adev = out->dev;
571 if (is_offload_usecase(out->usecase) ||
572 !adev->adm_register_output_stream)
573 return;
574
575 // register stream first for backward compatibility
576 adev->adm_register_output_stream(adev->adm_data,
577 out->handle,
578 out->flags);
579
580 if (!adev->adm_set_config)
581 return;
582
583 if (out->realtime)
584 adev->adm_set_config(adev->adm_data,
585 out->handle,
586 out->pcm, &out->config);
587}
588
589static void register_in_stream(struct stream_in *in)
590{
591 struct audio_device *adev = in->dev;
592 if (!adev->adm_register_input_stream)
593 return;
594
595 adev->adm_register_input_stream(adev->adm_data,
596 in->capture_handle,
597 in->flags);
598
599 if (!adev->adm_set_config)
600 return;
601
602 if (in->realtime)
603 adev->adm_set_config(adev->adm_data,
604 in->capture_handle,
605 in->pcm,
606 &in->config);
607}
608
609static void request_out_focus(struct stream_out *out, long ns)
610{
611 struct audio_device *adev = out->dev;
612
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700613 if (adev->adm_request_focus_v2)
614 adev->adm_request_focus_v2(adev->adm_data, out->handle, ns);
615 else if (adev->adm_request_focus)
616 adev->adm_request_focus(adev->adm_data, out->handle);
617}
618
Aniket Kumar Lata60586a92019-05-22 22:18:55 -0700619static int request_in_focus(struct stream_in *in, long ns)
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700620{
621 struct audio_device *adev = in->dev;
Aniket Kumar Lata60586a92019-05-22 22:18:55 -0700622 int ret = 0;
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700623
Aniket Kumar Lata60586a92019-05-22 22:18:55 -0700624 if (adev->adm_request_focus_v2_1)
625 ret = adev->adm_request_focus_v2_1(adev->adm_data, in->capture_handle, ns);
626 else if (adev->adm_request_focus_v2)
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700627 adev->adm_request_focus_v2(adev->adm_data, in->capture_handle, ns);
628 else if (adev->adm_request_focus)
629 adev->adm_request_focus(adev->adm_data, in->capture_handle);
Aniket Kumar Lata60586a92019-05-22 22:18:55 -0700630
631 return ret;
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700632}
633
634static void release_out_focus(struct stream_out *out)
635{
636 struct audio_device *adev = out->dev;
637
638 if (adev->adm_abandon_focus)
639 adev->adm_abandon_focus(adev->adm_data, out->handle);
640}
641
642static void release_in_focus(struct stream_in *in)
643{
644 struct audio_device *adev = in->dev;
645 if (adev->adm_abandon_focus)
646 adev->adm_abandon_focus(adev->adm_data, in->capture_handle);
647}
648
Dhananjay Kumare6293dd2017-05-25 17:25:30 +0530649static int parse_snd_card_status(struct str_parms *parms, int *card,
650 card_status_t *status)
651{
652 char value[32]={0};
653 char state[32]={0};
654
655 int ret = str_parms_get_str(parms, "SND_CARD_STATUS", value, sizeof(value));
656 if (ret < 0)
657 return -1;
658
659 // sscanf should be okay as value is of max length 32.
660 // same as sizeof state.
661 if (sscanf(value, "%d,%s", card, state) < 2)
662 return -1;
663
664 *status = !strcmp(state, "ONLINE") ? CARD_STATUS_ONLINE :
665 CARD_STATUS_OFFLINE;
666 return 0;
667}
668
Haynes Mathew Georgeb0f5dc32017-10-06 18:35:12 -0700669static inline void adjust_frames_for_device_delay(struct stream_out *out,
670 uint32_t *dsp_frames) {
671 // Adjustment accounts for A2dp encoder latency with offload usecases
672 // Note: Encoder latency is returned in ms.
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800673 if (is_a2dp_out_device_type(&out->device_list)) {
Haynes Mathew Georgeb0f5dc32017-10-06 18:35:12 -0700674 unsigned long offset =
675 (audio_extn_a2dp_get_encoder_latency() * out->sample_rate / 1000);
676 *dsp_frames = (*dsp_frames > offset) ? (*dsp_frames - offset) : 0;
677 }
678}
679
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700680static inline bool free_entry(void *key __unused,
681 void *value, void *context __unused)
682{
683 free(value);
684 return true;
685}
686
687static inline void free_map(Hashmap *map)
688{
689 if (map) {
690 hashmapForEach(map, free_entry, (void *) NULL);
691 hashmapFree(map);
692 }
693}
694
Aniket Kumar Latabc774842020-01-16 21:22:05 -0800695static inline void patch_map_remove_l(struct audio_device *adev,
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700696 audio_patch_handle_t patch_handle)
697{
698 if (patch_handle == AUDIO_PATCH_HANDLE_NONE)
699 return;
700
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700701 struct audio_patch_info *p_info =
702 hashmapGet(adev->patch_map, (void *) (intptr_t) patch_handle);
703 if (p_info) {
704 ALOGV("%s: Remove patch %d", __func__, patch_handle);
705 hashmapRemove(adev->patch_map, (void *) (intptr_t) patch_handle);
706 free(p_info->patch);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700707 free(p_info);
708 }
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700709}
710
711static inline int io_streams_map_insert(struct audio_device *adev,
712 struct audio_stream *stream,
713 audio_io_handle_t handle,
714 audio_patch_handle_t patch_handle)
715{
716 struct audio_stream_info *s_info =
717 (struct audio_stream_info *) calloc(1, sizeof(struct audio_stream_info));
718
719 if (s_info == NULL) {
720 ALOGE("%s: Could not allocate stream info", __func__);
721 return -ENOMEM;
722 }
723 s_info->stream = stream;
724 s_info->patch_handle = patch_handle;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700725
726 pthread_mutex_lock(&adev->lock);
727 struct audio_stream_info *stream_info =
728 hashmapPut(adev->io_streams_map, (void *) (intptr_t) handle, (void *) s_info);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700729 if (stream_info != NULL)
730 free(stream_info);
Aniket Kumar Latabc774842020-01-16 21:22:05 -0800731 pthread_mutex_unlock(&adev->lock);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700732 ALOGD("%s: Added stream in io_streams_map with handle %d", __func__, handle);
733 return 0;
734}
735
736static inline void io_streams_map_remove(struct audio_device *adev,
737 audio_io_handle_t handle)
738{
739 pthread_mutex_lock(&adev->lock);
740 struct audio_stream_info *s_info =
741 hashmapRemove(adev->io_streams_map, (void *) (intptr_t) handle);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700742 if (s_info == NULL)
Aniket Kumar Latabc774842020-01-16 21:22:05 -0800743 goto done;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700744 ALOGD("%s: Removed stream with handle %d", __func__, handle);
Aniket Kumar Latabc774842020-01-16 21:22:05 -0800745 patch_map_remove_l(adev, s_info->patch_handle);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700746 free(s_info);
Aniket Kumar Latabc774842020-01-16 21:22:05 -0800747done:
748 pthread_mutex_unlock(&adev->lock);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700749 return;
750}
751
Aniket Kumar Latabc774842020-01-16 21:22:05 -0800752static struct audio_patch_info* fetch_patch_info_l(struct audio_device *adev,
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700753 audio_patch_handle_t handle)
754{
755 struct audio_patch_info *p_info = NULL;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700756 p_info = (struct audio_patch_info *)
757 hashmapGet(adev->patch_map, (void *) (intptr_t) handle);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -0700758 return p_info;
759}
760
vivek mehtaa76401a2015-04-24 14:12:15 -0700761__attribute__ ((visibility ("default")))
762bool audio_hw_send_gain_dep_calibration(int level) {
763 bool ret_val = false;
vivek mehtab72d08d2016-04-29 03:16:47 -0700764 ALOGV("%s: called ...", __func__);
vivek mehtaa76401a2015-04-24 14:12:15 -0700765
766 pthread_mutex_lock(&adev_init_lock);
767
768 if (adev != NULL && adev->platform != NULL) {
769 pthread_mutex_lock(&adev->lock);
770 ret_val = platform_send_gain_dep_cal(adev->platform, level);
vivek mehtab72d08d2016-04-29 03:16:47 -0700771
Preetam Singh Ranawatf4ae0222017-05-31 17:07:28 +0530772 // cache level info for any of the use case which
773 // was not started.
774 last_known_cal_step = level;;
vivek mehtab72d08d2016-04-29 03:16:47 -0700775
vivek mehtaa76401a2015-04-24 14:12:15 -0700776 pthread_mutex_unlock(&adev->lock);
777 } else {
778 ALOGE("%s: %s is NULL", __func__, adev == NULL ? "adev" : "adev->platform");
779 }
780
781 pthread_mutex_unlock(&adev_init_lock);
782
783 return ret_val;
784}
785
Ashish Jain5106d362016-05-11 19:23:33 +0530786static int check_and_set_gapless_mode(struct audio_device *adev, bool enable_gapless)
787{
Krishnankutty Kolathappilly6d8788b2014-01-09 12:45:31 -0800788 bool gapless_enabled = false;
789 const char *mixer_ctl_name = "Compress Gapless Playback";
790 struct mixer_ctl *ctl;
791
792 ALOGV("%s:", __func__);
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700793 gapless_enabled = property_get_bool("vendor.audio.offload.gapless.enabled", false);
Ashish Jain5106d362016-05-11 19:23:33 +0530794
795 /*Disable gapless if its AV playback*/
796 gapless_enabled = gapless_enabled && enable_gapless;
Krishnankutty Kolathappilly6d8788b2014-01-09 12:45:31 -0800797
798 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
799 if (!ctl) {
800 ALOGE("%s: Could not get ctl for mixer cmd - %s",
801 __func__, mixer_ctl_name);
802 return -EINVAL;
803 }
804
805 if (mixer_ctl_set_value(ctl, 0, gapless_enabled) < 0) {
806 ALOGE("%s: Could not set gapless mode %d",
807 __func__, gapless_enabled);
808 return -EINVAL;
809 }
810 return 0;
811}
Haynes Mathew George5191a852013-09-11 14:19:36 -0700812
Aniket Kumar Lataf56b6402016-10-27 12:03:18 -0700813__attribute__ ((visibility ("default")))
814int audio_hw_get_gain_level_mapping(struct amp_db_and_gain_table *mapping_tbl,
815 int table_size) {
816 int ret_val = 0;
817 ALOGV("%s: enter ... ", __func__);
818
819 pthread_mutex_lock(&adev_init_lock);
820 if (adev == NULL) {
821 ALOGW("%s: adev is NULL .... ", __func__);
822 goto done;
823 }
824
825 pthread_mutex_lock(&adev->lock);
826 ret_val = platform_get_gain_level_mapping(mapping_tbl, table_size);
827 pthread_mutex_unlock(&adev->lock);
828done:
829 pthread_mutex_unlock(&adev_init_lock);
830 ALOGV("%s: exit ... ", __func__);
831 return ret_val;
832}
833
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800834bool audio_hw_send_qdsp_parameter(int stream_type, float vol, bool active)
Aalique Grahame22e49102018-12-18 14:23:57 -0800835{
836 bool ret = false;
837 ALOGV("%s: enter ...", __func__);
838
839 pthread_mutex_lock(&adev_init_lock);
840
841 if (adev != NULL && adev->platform != NULL) {
842 pthread_mutex_lock(&adev->lock);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800843 ret = audio_extn_qdsp_set_state(adev, stream_type, vol, active);
Aalique Grahame22e49102018-12-18 14:23:57 -0800844 pthread_mutex_unlock(&adev->lock);
845 }
846
847 pthread_mutex_unlock(&adev_init_lock);
848
849 ALOGV("%s: exit with ret %d", __func__, ret);
850 return ret;
851}
Aalique Grahame22e49102018-12-18 14:23:57 -0800852
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700853static bool is_supported_format(audio_format_t format)
854{
Eric Laurent86e17132013-09-12 17:49:30 -0700855 if (format == AUDIO_FORMAT_MP3 ||
Satish Babu Patakokila0c313922016-12-08 12:07:08 +0530856 format == AUDIO_FORMAT_MP2 ||
Ashish Jainf9b78162014-08-25 20:36:25 +0530857 format == AUDIO_FORMAT_AAC_LC ||
858 format == AUDIO_FORMAT_AAC_HE_V1 ||
859 format == AUDIO_FORMAT_AAC_HE_V2 ||
Manish Dewangana6fc5442015-08-24 20:30:31 +0530860 format == AUDIO_FORMAT_AAC_ADTS_LC ||
861 format == AUDIO_FORMAT_AAC_ADTS_HE_V1 ||
862 format == AUDIO_FORMAT_AAC_ADTS_HE_V2 ||
Arun Kumar Dasari3b174182016-12-27 13:01:14 +0530863 format == AUDIO_FORMAT_AAC_LATM_LC ||
864 format == AUDIO_FORMAT_AAC_LATM_HE_V1 ||
865 format == AUDIO_FORMAT_AAC_LATM_HE_V2 ||
Ashish Jain5106d362016-05-11 19:23:33 +0530866 format == AUDIO_FORMAT_PCM_24_BIT_PACKED ||
867 format == AUDIO_FORMAT_PCM_8_24_BIT ||
Ashish Jainf1eaa582016-05-23 20:54:24 +0530868 format == AUDIO_FORMAT_PCM_FLOAT ||
869 format == AUDIO_FORMAT_PCM_32_BIT ||
vivek mehta0ea887a2015-08-26 14:01:20 -0700870 format == AUDIO_FORMAT_PCM_16_BIT ||
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530871 format == AUDIO_FORMAT_AC3 ||
872 format == AUDIO_FORMAT_E_AC3 ||
Ben Romberger1aaaf862017-04-06 17:49:46 -0700873 format == AUDIO_FORMAT_DOLBY_TRUEHD ||
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530874 format == AUDIO_FORMAT_DTS ||
875 format == AUDIO_FORMAT_DTS_HD ||
Weiyin Jiang18ac4e92015-03-15 15:03:40 +0800876 format == AUDIO_FORMAT_FLAC ||
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530877 format == AUDIO_FORMAT_ALAC ||
878 format == AUDIO_FORMAT_APE ||
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530879 format == AUDIO_FORMAT_DSD ||
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530880 format == AUDIO_FORMAT_VORBIS ||
Weiyin Jiang18ac4e92015-03-15 15:03:40 +0800881 format == AUDIO_FORMAT_WMA ||
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +0530882 format == AUDIO_FORMAT_WMA_PRO ||
Naresh Tanniru928f0862017-04-07 16:44:23 -0700883 format == AUDIO_FORMAT_APTX ||
884 format == AUDIO_FORMAT_IEC61937)
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -0800885 return true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700886
887 return false;
888}
889
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700890static inline bool is_mmap_usecase(audio_usecase_t uc_id)
891{
892 return (uc_id == USECASE_AUDIO_RECORD_AFE_PROXY) ||
Zhou Song62ea0282020-03-22 19:53:01 +0800893 (uc_id == USECASE_AUDIO_RECORD_AFE_PROXY2) ||
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700894 (uc_id == USECASE_AUDIO_PLAYBACK_AFE_PROXY);
895}
896
Aniket Kumar Lata8426d1f2019-06-10 15:25:53 -0700897static inline bool is_valid_volume(float left, float right)
898{
899 return ((left >= 0.0f && right >= 0.0f) ? true : false);
900}
901
Avinash Vaish71a8b972014-07-24 15:36:33 +0530902static int enable_audio_route_for_voice_usecases(struct audio_device *adev,
903 struct audio_usecase *uc_info)
904{
905 struct listnode *node;
906 struct audio_usecase *usecase;
907
908 if (uc_info == NULL)
909 return -EINVAL;
910
911 /* Re-route all voice usecases on the shared backend other than the
912 specified usecase to new snd devices */
913 list_for_each(node, &adev->usecase_list) {
914 usecase = node_to_item(node, struct audio_usecase, list);
Narsinga Rao Chellae7f33c12015-01-28 15:23:34 -0800915 if ((usecase->type == VOICE_CALL) && (usecase != uc_info))
Avinash Vaish71a8b972014-07-24 15:36:33 +0530916 enable_audio_route(adev, usecase);
917 }
918 return 0;
919}
920
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +0530921static void enable_asrc_mode(struct audio_device *adev)
Preetam Singh Ranawatb0c0dd72016-08-18 00:32:06 +0530922{
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +0530923 ALOGV("%s", __func__);
924 audio_route_apply_and_update_path(adev->audio_route,
925 "asrc-mode");
926 adev->asrc_mode_enabled = true;
927}
928
929static void disable_asrc_mode(struct audio_device *adev)
930{
931 ALOGV("%s", __func__);
932 audio_route_reset_and_update_path(adev->audio_route,
933 "asrc-mode");
934 adev->asrc_mode_enabled = false;
935}
936
937/*
938 * - Enable ASRC mode for incoming mix path use case(Headphone backend)if Headphone
939 * 44.1 or Native DSD backends are enabled for any of current use case.
940 * e.g. 48-> + (Naitve DSD or Headphone 44.1)
941 * - Disable current mix path use case(Headphone backend) and re-enable it with
942 * ASRC mode for incoming Headphone 44.1 or Native DSD use case.
943 * e.g. Naitve DSD or Headphone 44.1 -> + 48
944 */
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +0530945static void check_and_set_asrc_mode(struct audio_device *adev,
946 struct audio_usecase *uc_info,
947 snd_device_t snd_device)
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +0530948{
949 ALOGV("%s snd device %d", __func__, snd_device);
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +0530950 int i, num_new_devices = 0;
951 snd_device_t split_new_snd_devices[SND_DEVICE_OUT_END];
952 /*
953 *Split snd device for new combo use case
954 *e.g. Headphopne 44.1-> + Ringtone (Headphone + Speaker)
955 */
956 if (platform_split_snd_device(adev->platform,
957 snd_device,
958 &num_new_devices,
959 split_new_snd_devices) == 0) {
960 for (i = 0; i < num_new_devices; i++)
961 check_and_set_asrc_mode(adev, uc_info, split_new_snd_devices[i]);
962 } else {
963 int new_backend_idx = platform_get_backend_index(snd_device);
964 if (((new_backend_idx == HEADPHONE_BACKEND) ||
965 (new_backend_idx == HEADPHONE_44_1_BACKEND) ||
966 (new_backend_idx == DSD_NATIVE_BACKEND)) &&
967 !adev->asrc_mode_enabled) {
968 struct listnode *node = NULL;
969 struct audio_usecase *uc = NULL;
970 struct stream_out *curr_out = NULL;
971 int usecase_backend_idx = DEFAULT_CODEC_BACKEND;
972 int i, num_devices, ret = 0;
973 snd_device_t split_snd_devices[SND_DEVICE_OUT_END];
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +0530974
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +0530975 list_for_each(node, &adev->usecase_list) {
976 uc = node_to_item(node, struct audio_usecase, list);
977 curr_out = (struct stream_out*) uc->stream.out;
978 if (curr_out && PCM_PLAYBACK == uc->type && uc != uc_info) {
979 /*
980 *Split snd device for existing combo use case
981 *e.g. Ringtone (Headphone + Speaker) + Headphopne 44.1
982 */
983 ret = platform_split_snd_device(adev->platform,
984 uc->out_snd_device,
985 &num_devices,
986 split_snd_devices);
987 if (ret < 0 || num_devices == 0) {
988 ALOGV("%s: Unable to split uc->out_snd_device: %d",__func__, uc->out_snd_device);
989 split_snd_devices[0] = uc->out_snd_device;
990 num_devices = 1;
Garmond Leung50058f62017-02-08 09:49:30 -0800991 }
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +0530992 for (i = 0; i < num_devices; i++) {
993 usecase_backend_idx = platform_get_backend_index(split_snd_devices[i]);
994 ALOGD("%s:snd_dev %d usecase_backend_idx %d",__func__, split_snd_devices[i],usecase_backend_idx);
995 if((new_backend_idx == HEADPHONE_BACKEND) &&
996 ((usecase_backend_idx == HEADPHONE_44_1_BACKEND) ||
997 (usecase_backend_idx == DSD_NATIVE_BACKEND))) {
998 ALOGD("%s:DSD or native stream detected enabling asrcmode in hardware",
999 __func__);
1000 enable_asrc_mode(adev);
1001 break;
1002 } else if(((new_backend_idx == HEADPHONE_44_1_BACKEND) ||
1003 (new_backend_idx == DSD_NATIVE_BACKEND)) &&
1004 (usecase_backend_idx == HEADPHONE_BACKEND)) {
1005 ALOGD("%s:48K stream detected, disabling and enabling it with asrcmode in hardware",
1006 __func__);
1007 disable_audio_route(adev, uc);
1008 disable_snd_device(adev, uc->out_snd_device);
1009 // Apply true-high-quality-mode if DSD or > 44.1KHz or >=24-bit
1010 if (new_backend_idx == DSD_NATIVE_BACKEND)
1011 audio_route_apply_and_update_path(adev->audio_route,
1012 "hph-true-highquality-mode");
1013 else if ((new_backend_idx == HEADPHONE_44_1_BACKEND) &&
1014 (curr_out->bit_width >= 24))
1015 audio_route_apply_and_update_path(adev->audio_route,
1016 "hph-highquality-mode");
1017 enable_asrc_mode(adev);
1018 enable_snd_device(adev, uc->out_snd_device);
1019 enable_audio_route(adev, uc);
1020 break;
1021 }
1022 }
1023 // reset split devices count
1024 num_devices = 0;
Garmond Leung50058f62017-02-08 09:49:30 -08001025 }
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +05301026 if (adev->asrc_mode_enabled)
1027 break;
Preetam Singh Ranawatb0c0dd72016-08-18 00:32:06 +05301028 }
1029 }
1030 }
1031}
1032
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001033static int send_effect_enable_disable_mixer_ctl(struct audio_device *adev,
1034 struct audio_effect_config effect_config,
1035 unsigned int param_value)
1036{
1037 char mixer_ctl_name[] = "Audio Effect";
1038 struct mixer_ctl *ctl;
1039 long set_values[6];
Eric Laurent637e2d42018-11-15 12:24:31 -08001040 struct stream_in *in = adev_get_active_input(adev);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001041
Aniket Kumar Lata7de63522019-06-13 11:05:18 -07001042 if (in == NULL) {
1043 ALOGE("%s: active input stream is NULL", __func__);
1044 return -EINVAL;
1045 }
1046
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001047 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1048 if (!ctl) {
1049 ALOGE("%s: Could not get mixer ctl - %s",
1050 __func__, mixer_ctl_name);
1051 return -EINVAL;
1052 }
1053
1054 set_values[0] = 1; //0:Rx 1:Tx
1055 set_values[1] = in->app_type_cfg.app_type;
1056 set_values[2] = (long)effect_config.module_id;
1057 set_values[3] = (long)effect_config.instance_id;
1058 set_values[4] = (long)effect_config.param_id;
1059 set_values[5] = param_value;
1060
1061 mixer_ctl_set_array(ctl, set_values, ARRAY_SIZE(set_values));
1062
1063 return 0;
1064
1065}
1066
1067static int update_effect_param_ecns(struct audio_device *adev, unsigned int module_id,
1068 int effect_type, unsigned int *param_value)
1069{
1070 int ret = 0;
1071 struct audio_effect_config other_effect_config;
1072 struct audio_usecase *usecase = NULL;
Eric Laurent637e2d42018-11-15 12:24:31 -08001073 struct stream_in *in = adev_get_active_input(adev);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001074
Aniket Kumar Lata7de63522019-06-13 11:05:18 -07001075 if (in == NULL) {
1076 ALOGE("%s: active input stream is NULL", __func__);
1077 return -EINVAL;
1078 }
1079
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001080 usecase = get_usecase_from_list(adev, in->usecase);
1081 if (!usecase)
1082 return -EINVAL;
1083
1084 ret = platform_get_effect_config_data(usecase->in_snd_device, &other_effect_config,
1085 effect_type == EFFECT_AEC ? EFFECT_NS : EFFECT_AEC);
1086 if (ret < 0) {
1087 ALOGE("%s Failed to get effect params %d", __func__, ret);
1088 return ret;
1089 }
1090
1091 if (module_id == other_effect_config.module_id) {
1092 //Same module id for AEC/NS. Values need to be combined
1093 if (((effect_type == EFFECT_AEC) && (in->enable_ns)) ||
1094 ((effect_type == EFFECT_NS) && (in->enable_aec))) {
1095 *param_value |= other_effect_config.param_value;
1096 }
1097 }
1098
1099 return ret;
1100}
1101
1102static int enable_disable_effect(struct audio_device *adev, int effect_type, bool enable)
Gangadhar Sb0210342019-02-22 17:39:41 +05301103{
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001104 struct audio_effect_config effect_config;
1105 struct audio_usecase *usecase = NULL;
1106 int ret = 0;
1107 unsigned int param_value = 0;
Eric Laurent637e2d42018-11-15 12:24:31 -08001108 struct stream_in *in = adev_get_active_input(adev);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001109
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001110 if(!voice_extn_is_dynamic_ecns_enabled())
1111 return ENOSYS;
1112
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001113 if (!in) {
1114 ALOGE("%s: Invalid input stream", __func__);
1115 return -EINVAL;
1116 }
1117
1118 ALOGD("%s: effect_type:%d enable:%d", __func__, effect_type, enable);
1119
1120 usecase = get_usecase_from_list(adev, in->usecase);
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001121 if (usecase == NULL) {
1122 ALOGE("%s: Could not find the usecase (%d) in the list",
1123 __func__, in->usecase);
1124 return -EINVAL;
1125 }
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001126
1127 ret = platform_get_effect_config_data(usecase->in_snd_device, &effect_config, effect_type);
1128 if (ret < 0) {
1129 ALOGE("%s Failed to get module id %d", __func__, ret);
1130 return ret;
1131 }
1132 ALOGV("%s: %d %d usecase->id:%d usecase->in_snd_device:%d", __func__, effect_config.module_id,
1133 in->app_type_cfg.app_type, usecase->id, usecase->in_snd_device);
1134
1135 if(enable)
1136 param_value = effect_config.param_value;
1137
1138 /*Special handling for AEC & NS effects Param values need to be
1139 updated if module ids are same*/
1140
1141 if ((effect_type == EFFECT_AEC) || (effect_type == EFFECT_NS)) {
1142 ret = update_effect_param_ecns(adev, effect_config.module_id, effect_type, &param_value);
1143 if (ret < 0)
1144 return ret;
1145 }
1146
1147 ret = send_effect_enable_disable_mixer_ctl(adev, effect_config, param_value);
1148
1149 return ret;
1150}
1151
1152static void check_and_enable_effect(struct audio_device *adev)
1153{
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001154 if(!voice_extn_is_dynamic_ecns_enabled())
1155 return;
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001156
Eric Laurent637e2d42018-11-15 12:24:31 -08001157 struct stream_in *in = adev_get_active_input(adev);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001158
Eric Laurent637e2d42018-11-15 12:24:31 -08001159 if (in != NULL && !in->standby) {
1160 if (in->enable_aec)
1161 enable_disable_effect(adev, EFFECT_AEC, true);
1162
1163 if (in->enable_ns &&
1164 in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
1165 enable_disable_effect(adev, EFFECT_NS, true);
1166 }
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001167 }
1168}
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001169
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07001170int pcm_ioctl(struct pcm *pcm, int request, ...)
1171{
1172 va_list ap;
1173 void * arg;
1174 int pcm_fd = *(int*)pcm;
1175
1176 va_start(ap, request);
1177 arg = va_arg(ap, void *);
1178 va_end(ap);
1179
1180 return ioctl(pcm_fd, request, arg);
1181}
1182
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -07001183int enable_audio_route(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001184 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001185{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001186 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001187 char mixer_path[MIXER_PATH_MAX_LENGTH];
Manish Dewangan58229382017-02-02 15:48:41 +05301188 struct stream_out *out = NULL;
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05301189 struct stream_in *in = NULL;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001190 struct listnode out_devices;
Soumya Managoli6993b762018-06-28 16:04:57 +05301191 int ret = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001192
1193 if (usecase == NULL)
1194 return -EINVAL;
1195
1196 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
1197
Carter Hsu2e429db2019-05-14 18:50:52 +08001198 if (usecase->type == PCM_CAPTURE) {
1199 struct stream_in *in = usecase->stream.in;
1200 struct audio_usecase *uinfo;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001201 snd_device = usecase->in_snd_device;
Carter Hsu2e429db2019-05-14 18:50:52 +08001202
1203 if (in) {
1204 if (in->enable_aec || in->enable_ec_port) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001205 list_init(&out_devices);
1206 update_device_list(&out_devices, AUDIO_DEVICE_OUT_SPEAKER, "", true);
Carter Hsu2e429db2019-05-14 18:50:52 +08001207 struct listnode *node;
1208 struct audio_usecase *voip_usecase = get_usecase_from_list(adev,
1209 USECASE_AUDIO_PLAYBACK_VOIP);
1210 if (voip_usecase) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001211 assign_devices(&out_devices,
1212 &voip_usecase->stream.out->device_list);
Carter Hsu2e429db2019-05-14 18:50:52 +08001213 } else if (adev->primary_output &&
1214 !adev->primary_output->standby) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001215 assign_devices(&out_devices,
1216 &adev->primary_output->device_list);
Carter Hsu2e429db2019-05-14 18:50:52 +08001217 } else {
1218 list_for_each(node, &adev->usecase_list) {
1219 uinfo = node_to_item(node, struct audio_usecase, list);
1220 if (uinfo->type != PCM_CAPTURE) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001221 assign_devices(&out_devices,
1222 &uinfo->stream.out->device_list);
Carter Hsu2e429db2019-05-14 18:50:52 +08001223 break;
1224 }
1225 }
1226 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001227
1228 platform_set_echo_reference(adev, true, &out_devices);
Carter Hsu2e429db2019-05-14 18:50:52 +08001229 in->ec_opened = true;
1230 }
1231 }
Guodong Huf5e614d2019-06-24 18:42:03 +08001232 } else if ((usecase->type == TRANSCODE_LOOPBACK_TX) || ((usecase->type == PCM_HFP_CALL) &&
1233 ((usecase->id == USECASE_AUDIO_HFP_SCO) || (usecase->id == USECASE_AUDIO_HFP_SCO_WB)) &&
1234 (usecase->in_snd_device == SND_DEVICE_IN_VOICE_SPEAKER_MIC_HFP_MMSECNS))) {
Carter Hsu2e429db2019-05-14 18:50:52 +08001235 snd_device = usecase->in_snd_device;
1236 } else {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001237 snd_device = usecase->out_snd_device;
Carter Hsu2e429db2019-05-14 18:50:52 +08001238 }
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001239
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08001240#ifdef DS1_DOLBY_DAP_ENABLED
1241 audio_extn_dolby_set_dmid(adev);
1242 audio_extn_dolby_set_endpoint(adev);
1243#endif
Pradnya Chaphekar8a9dcd82014-09-09 09:49:10 -07001244 audio_extn_dolby_ds2_set_endpoint(adev);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001245 audio_extn_sound_trigger_update_stream_status(usecase, ST_EVENT_STREAM_BUSY);
Dhananjay Kumar45b71742014-05-29 21:47:27 +05301246 audio_extn_listen_update_stream_status(usecase, LISTEN_EVENT_STREAM_BUSY);
Ben Romberger1fafdde2015-09-09 19:43:15 -07001247 audio_extn_utils_send_app_type_cfg(adev, usecase);
Jasmine Cha4dcc1092019-03-04 18:12:47 +08001248 if (audio_extn_is_maxx_audio_enabled())
1249 audio_extn_ma_set_device(usecase);
Dhananjay Kumar14170dd2015-08-28 13:24:16 +05301250 audio_extn_utils_send_audio_calibration(adev, usecase);
Manish Dewangan58229382017-02-02 15:48:41 +05301251 if ((usecase->type == PCM_PLAYBACK) && is_offload_usecase(usecase->id)) {
1252 out = usecase->stream.out;
1253 if (out && out->compr)
1254 audio_extn_utils_compress_set_clk_rec_mode(usecase);
1255 }
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05301256
1257 if (usecase->type == PCM_CAPTURE) {
1258 in = usecase->stream.in;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001259 if (in && is_loopback_input_device(get_device_types(&in->device_list))) {
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05301260 ALOGD("%s: set custom mtmx params v1", __func__);
1261 audio_extn_set_custom_mtmx_params_v1(adev, usecase, true);
1262 }
1263 } else {
1264 audio_extn_set_custom_mtmx_params_v2(adev, usecase, true);
1265 }
Manish Dewangan58229382017-02-02 15:48:41 +05301266
Andy Hung756ecc12018-10-19 17:47:12 -07001267 // we shouldn't truncate mixer_path
1268 ALOGW_IF(strlcpy(mixer_path, use_case_table[usecase->id], sizeof(mixer_path))
1269 >= sizeof(mixer_path), "%s: truncation on mixer path", __func__);
1270 // this also appends to mixer_path
Banajit Goswami20cdd212015-09-11 01:11:30 -07001271 platform_add_backend_name(mixer_path, snd_device, usecase);
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07001272 ALOGD("%s: apply mixer and update path: %s", __func__, mixer_path);
Soumya Managoli6993b762018-06-28 16:04:57 +05301273 ret = audio_route_apply_and_update_path(adev->audio_route, mixer_path);
1274 if (!ret && usecase->id == USECASE_AUDIO_PLAYBACK_FM) {
1275 struct str_parms *parms = str_parms_create_str("fm_restore_volume=1");
1276 if (parms) {
1277 audio_extn_fm_set_parameters(adev, parms);
1278 str_parms_destroy(parms);
1279 }
1280 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001281 ALOGV("%s: exit", __func__);
1282 return 0;
1283}
1284
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001285int disable_audio_route(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001286 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001287{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001288 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001289 char mixer_path[MIXER_PATH_MAX_LENGTH];
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05301290 struct stream_in *in = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001291
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05301292 if (usecase == NULL || usecase->id == USECASE_INVALID)
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001293 return -EINVAL;
1294
1295 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Surendar Karka93cd25a2018-08-28 14:21:37 +05301296 if (usecase->type == PCM_CAPTURE || usecase->type == TRANSCODE_LOOPBACK_TX)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001297 snd_device = usecase->in_snd_device;
1298 else
1299 snd_device = usecase->out_snd_device;
Kunlei Zhange7cab1a2020-05-17 20:31:05 +08001300
1301 /* disable island and power mode on supported device for voice call */
1302 if (usecase->type == VOICE_CALL) {
1303 if (usecase->in_snd_device != SND_DEVICE_NONE) {
1304 if (platform_get_island_cfg_on_device(adev->platform, usecase->in_snd_device) &&
1305 platform_get_power_mode_on_device(adev->platform, usecase->in_snd_device)) {
1306 platform_set_island_cfg_on_device(adev, usecase->in_snd_device, false);
1307 platform_set_power_mode_on_device(adev, usecase->in_snd_device, false);
1308 platform_reset_island_power_status(adev->platform, usecase->in_snd_device);
1309 ALOGD("%s: disable island cfg and power mode in voice tx path",
1310 __func__);
1311 }
1312 }
1313 if (usecase->out_snd_device != SND_DEVICE_NONE) {
1314 if (platform_get_island_cfg_on_device(adev->platform, usecase->out_snd_device) &&
1315 platform_get_power_mode_on_device(adev->platform, usecase->out_snd_device)) {
1316 platform_set_island_cfg_on_device(adev, usecase->out_snd_device, false);
1317 platform_set_power_mode_on_device(adev, usecase->out_snd_device, false);
1318 platform_reset_island_power_status(adev->platform, usecase->out_snd_device);
1319 ALOGD("%s: disable island cfg and power mode in voice rx path",
1320 __func__);
1321 }
1322 }
1323 }
1324
Andy Hung756ecc12018-10-19 17:47:12 -07001325 // we shouldn't truncate mixer_path
1326 ALOGW_IF(strlcpy(mixer_path, use_case_table[usecase->id], sizeof(mixer_path))
1327 >= sizeof(mixer_path), "%s: truncation on mixer path", __func__);
1328 // this also appends to mixer_path
Banajit Goswami20cdd212015-09-11 01:11:30 -07001329 platform_add_backend_name(mixer_path, snd_device, usecase);
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07001330 ALOGD("%s: reset and update mixer path: %s", __func__, mixer_path);
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07001331 audio_route_reset_and_update_path(adev->audio_route, mixer_path);
Carter Hsu2e429db2019-05-14 18:50:52 +08001332 if (usecase->type == PCM_CAPTURE) {
1333 struct stream_in *in = usecase->stream.in;
1334 if (in && in->ec_opened) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001335 struct listnode out_devices;
1336 list_init(&out_devices);
1337 platform_set_echo_reference(in->dev, false, &out_devices);
Carter Hsu2e429db2019-05-14 18:50:52 +08001338 in->ec_opened = false;
1339 }
1340 }
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001341 audio_extn_sound_trigger_update_stream_status(usecase, ST_EVENT_STREAM_FREE);
Dhananjay Kumar45b71742014-05-29 21:47:27 +05301342 audio_extn_listen_update_stream_status(usecase, LISTEN_EVENT_STREAM_FREE);
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05301343
1344 if (usecase->type == PCM_CAPTURE) {
1345 in = usecase->stream.in;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001346 if (in && is_loopback_input_device(get_device_types(&in->device_list))) {
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05301347 ALOGD("%s: reset custom mtmx params v1", __func__);
1348 audio_extn_set_custom_mtmx_params_v1(adev, usecase, false);
1349 }
1350 } else {
1351 audio_extn_set_custom_mtmx_params_v2(adev, usecase, false);
1352 }
1353
Weiyin Jiang298ffd92019-06-03 14:29:30 +08001354 if ((usecase->type == PCM_PLAYBACK) &&
1355 (usecase->stream.out != NULL))
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +05301356 usecase->stream.out->pspd_coeff_sent = false;
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05301357
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001358 ALOGV("%s: exit", __func__);
1359 return 0;
1360}
1361
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -07001362int enable_snd_device(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001363 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001364{
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301365 int i, num_devices = 0;
1366 snd_device_t new_snd_devices[SND_DEVICE_OUT_END];
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001367 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
1368
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001369 if (snd_device < SND_DEVICE_MIN ||
1370 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001371 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001372 return -EINVAL;
1373 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001374
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001375 if (platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0) {
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001376 ALOGE("%s: Invalid sound device returned", __func__);
1377 return -EINVAL;
1378 }
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001379
1380 adev->snd_dev_ref_cnt[snd_device]++;
1381
1382 if ((adev->snd_dev_ref_cnt[snd_device] > 1) &&
1383 (platform_split_snd_device(adev->platform,
1384 snd_device,
1385 &num_devices,
1386 new_snd_devices) != 0)) {
Eric Laurent994a6932013-07-17 11:51:42 -07001387 ALOGV("%s: snd_device(%d: %s) is already active",
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001388 __func__, snd_device, device_name);
Aniket Kumar Latabce0be62019-07-11 14:20:23 -07001389 /* Set backend config for A2DP to ensure slimbus configuration
1390 is correct if A2DP is already active and backend is closed
1391 and re-opened */
1392 if (snd_device == SND_DEVICE_OUT_BT_A2DP)
1393 audio_extn_a2dp_set_source_backend_cfg();
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001394 return 0;
1395 }
1396
Gopikrishnaiah Anandane85d0462014-06-30 21:41:20 -07001397 if (audio_extn_spkr_prot_is_enabled())
1398 audio_extn_spkr_prot_calib_cancel(adev);
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -07001399
Aalique Grahame22e49102018-12-18 14:23:57 -08001400 audio_extn_dsm_feedback_enable(adev, snd_device, true);
1401
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001402 if (platform_can_enable_spkr_prot_on_device(snd_device) &&
1403 audio_extn_spkr_prot_is_enabled()) {
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001404 if (platform_get_spkr_prot_acdb_id(snd_device) < 0) {
1405 goto err;
1406 }
1407 audio_extn_dev_arbi_acquire(snd_device);
Daniel Hillenbrand21752f02013-05-23 10:10:00 +05301408 amplifier_enable_devices(snd_device, true);
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001409 if (audio_extn_spkr_prot_start_processing(snd_device)) {
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -08001410 ALOGE("%s: spkr_start_processing failed", __func__);
Ravit Dennisaaee49c2015-02-04 21:26:22 +02001411 audio_extn_dev_arbi_release(snd_device);
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001412 goto err;
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -08001413 }
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001414 } else if (platform_split_snd_device(adev->platform,
1415 snd_device,
1416 &num_devices,
1417 new_snd_devices) == 0) {
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301418 for (i = 0; i < num_devices; i++) {
1419 enable_snd_device(adev, new_snd_devices[i]);
1420 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001421 platform_set_speaker_gain_in_combo(adev, snd_device, true);
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -08001422 } else {
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07001423 ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, device_name);
Naresh Tannirucd2353e2016-08-19 00:37:25 +05301424
Kunlei Zhange7cab1a2020-05-17 20:31:05 +08001425 /* enable island and power mode on supported device */
1426 if (platform_get_island_cfg_on_device(adev->platform, snd_device) &&
1427 platform_get_power_mode_on_device(adev->platform, snd_device)) {
1428 platform_set_island_cfg_on_device(adev, snd_device, true);
1429 platform_set_power_mode_on_device(adev, snd_device, true);
1430 ALOGD("%s: enable island cfg and power mode on: %s",
1431 __func__, device_name);
1432 }
Preetam Singh Ranawatf1d417c2017-01-10 17:00:32 +05301433
Revathi Uddarajub26e3932020-06-10 14:51:02 +05301434 if (SND_DEVICE_OUT_BT_A2DP == snd_device) {
1435 if (audio_extn_a2dp_start_playback() < 0) {
1436 ALOGE(" fail to configure A2dp Source control path ");
1437 goto err;
1438 } else {
1439 adev->a2dp_started = true;
1440 }
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001441 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001442
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001443 if ((SND_DEVICE_IN_BT_A2DP == snd_device) &&
1444 (audio_extn_a2dp_start_capture() < 0)) {
1445 ALOGE(" fail to configure A2dp Sink control path ");
1446 goto err;
1447 }
Naresh Tannirucd2353e2016-08-19 00:37:25 +05301448
Mingshu Pang7be5b1d2020-03-04 15:24:38 +08001449 if ((SND_DEVICE_OUT_BT_SCO_SWB == snd_device) ||
1450 (SND_DEVICE_IN_BT_SCO_MIC_SWB_NREC == snd_device) ||
1451 (SND_DEVICE_IN_BT_SCO_MIC_SWB == snd_device)) {
1452 if (!adev->bt_sco_on || (audio_extn_sco_start_configuration() < 0)) {
1453 ALOGE(" fail to configure sco control path ");
1454 goto err;
1455 }
Zhou Song12c29502019-03-16 10:37:18 +08001456 }
1457
Zhou Song331c8e52019-08-26 14:16:12 +08001458 configure_btsco_sample_rate(snd_device);
Bharath Ramachandramurthy0de16782014-03-28 21:34:33 -07001459 /* due to the possibility of calibration overwrite between listen
1460 and audio, notify listen hal before audio calibration is sent */
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001461 audio_extn_sound_trigger_update_device_status(snd_device,
1462 ST_EVENT_SND_DEVICE_BUSY);
Dhananjay Kumar45b71742014-05-29 21:47:27 +05301463 audio_extn_listen_update_device_status(snd_device,
1464 LISTEN_EVENT_SND_DEVICE_BUSY);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001465 if (platform_get_snd_device_acdb_id(snd_device) < 0) {
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001466 audio_extn_sound_trigger_update_device_status(snd_device,
1467 ST_EVENT_SND_DEVICE_FREE);
Dhananjay Kumar45b71742014-05-29 21:47:27 +05301468 audio_extn_listen_update_device_status(snd_device,
1469 LISTEN_EVENT_SND_DEVICE_FREE);
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001470 goto err;
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -07001471 }
Lior Barenboim0b61bc72014-05-13 13:01:37 +03001472 audio_extn_dev_arbi_acquire(snd_device);
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07001473 audio_route_apply_and_update_path(adev->audio_route, device_name);
Daniel Hillenbrand21752f02013-05-23 10:10:00 +05301474 amplifier_set_feedback(adev, snd_device, true);
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301475
1476 if (SND_DEVICE_OUT_HEADPHONES == snd_device &&
1477 !adev->native_playback_enabled &&
1478 audio_is_true_native_stream_active(adev)) {
1479 ALOGD("%s: %d: napb: enabling native mode in hardware",
1480 __func__, __LINE__);
1481 audio_route_apply_and_update_path(adev->audio_route,
1482 "true-native-mode");
1483 adev->native_playback_enabled = true;
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +05301484 }
Dhanalakshmi Siddani10621622018-04-30 15:07:27 +05301485 if (((snd_device == SND_DEVICE_IN_HANDSET_6MIC) ||
1486 (snd_device == SND_DEVICE_IN_HANDSET_QMIC)) &&
Eric Laurent637e2d42018-11-15 12:24:31 -08001487 (audio_extn_ffv_get_stream() == adev_get_active_input(adev))) {
Garmond Leunge2433c32017-09-28 21:51:22 -07001488 ALOGD("%s: init ec ref loopback", __func__);
1489 audio_extn_ffv_init_ec_ref_loopback(adev, snd_device);
1490 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001491 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001492 return 0;
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001493err:
1494 adev->snd_dev_ref_cnt[snd_device]--;
1495 return -EINVAL;;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001496}
1497
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001498int disable_snd_device(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001499 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001500{
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301501 int i, num_devices = 0;
1502 snd_device_t new_snd_devices[SND_DEVICE_OUT_END];
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001503 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
1504
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001505 if (snd_device < SND_DEVICE_MIN ||
1506 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001507 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001508 return -EINVAL;
1509 }
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001510
1511 if (platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0) {
1512 ALOGE("%s: Invalid sound device returned", __func__);
1513 return -EINVAL;
1514 }
1515
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001516 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
1517 ALOGE("%s: device ref cnt is already 0", __func__);
1518 return -EINVAL;
1519 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001520
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001521 adev->snd_dev_ref_cnt[snd_device]--;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -07001522
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001523
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001524 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07001525 ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, device_name);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301526
Aalique Grahame22e49102018-12-18 14:23:57 -08001527 audio_extn_dsm_feedback_enable(adev, snd_device, false);
1528
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001529 if (platform_can_enable_spkr_prot_on_device(snd_device) &&
1530 audio_extn_spkr_prot_is_enabled()) {
Anish Kumar46c7b872014-09-09 01:49:44 -07001531 audio_extn_spkr_prot_stop_processing(snd_device);
Vignesh Kulothungan3b5fae52017-09-25 12:16:30 -07001532
1533 // when speaker device is disabled, reset swap.
1534 // will be renabled on usecase start
1535 platform_set_swap_channels(adev, false);
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001536 } else if (platform_split_snd_device(adev->platform,
1537 snd_device,
1538 &num_devices,
1539 new_snd_devices) == 0) {
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301540 for (i = 0; i < num_devices; i++) {
1541 disable_snd_device(adev, new_snd_devices[i]);
1542 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001543 platform_set_speaker_gain_in_combo(adev, snd_device, false);
Lior Barenboim0b61bc72014-05-13 13:01:37 +03001544 } else {
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07001545 audio_route_reset_and_update_path(adev->audio_route, device_name);
Daniel Hillenbrand21752f02013-05-23 10:10:00 +05301546 amplifier_enable_devices(snd_device, false);
Lior Barenboim0b61bc72014-05-13 13:01:37 +03001547 }
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001548
Revathi Uddarajub26e3932020-06-10 14:51:02 +05301549 if (snd_device == SND_DEVICE_OUT_BT_A2DP) {
Naresh Tannirucd2353e2016-08-19 00:37:25 +05301550 audio_extn_a2dp_stop_playback();
Revathi Uddarajub26e3932020-06-10 14:51:02 +05301551 adev->a2dp_started = false;
1552 } else if (snd_device == SND_DEVICE_IN_BT_A2DP)
Florian Pfister1a84f312018-07-19 14:38:18 +02001553 audio_extn_a2dp_stop_capture();
Zhou Songd6d71752019-05-21 18:08:51 +08001554 else if ((snd_device == SND_DEVICE_OUT_HDMI) ||
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001555 (snd_device == SND_DEVICE_OUT_DISPLAY_PORT))
Ashish Jain81eb2a82015-05-13 10:52:34 +05301556 adev->is_channel_status_set = false;
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001557 else if ((snd_device == SND_DEVICE_OUT_HEADPHONES) &&
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301558 adev->native_playback_enabled) {
1559 ALOGD("%s: %d: napb: disabling native mode in hardware",
1560 __func__, __LINE__);
1561 audio_route_reset_and_update_path(adev->audio_route,
1562 "true-native-mode");
1563 adev->native_playback_enabled = false;
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001564 } else if ((snd_device == SND_DEVICE_OUT_HEADPHONES) &&
Preetam Singh Ranawatb0c0dd72016-08-18 00:32:06 +05301565 adev->asrc_mode_enabled) {
1566 ALOGD("%s: %d: disabling asrc mode in hardware", __func__, __LINE__);
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +05301567 disable_asrc_mode(adev);
1568 audio_route_apply_and_update_path(adev->audio_route, "hph-lowpower-mode");
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001569 } else if (((snd_device == SND_DEVICE_IN_HANDSET_6MIC) ||
Dhanalakshmi Siddaniaf4bd622019-02-27 16:28:06 +05301570 (snd_device == SND_DEVICE_IN_HANDSET_QMIC)) &&
Eric Laurent637e2d42018-11-15 12:24:31 -08001571 (audio_extn_ffv_get_stream() == adev_get_active_input(adev))) {
Garmond Leunge2433c32017-09-28 21:51:22 -07001572 ALOGD("%s: deinit ec ref loopback", __func__);
1573 audio_extn_ffv_deinit_ec_ref_loopback(adev, snd_device);
1574 }
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001575
1576 audio_extn_utils_release_snd_device(snd_device);
Daniel Hillenbrand21752f02013-05-23 10:10:00 +05301577 amplifier_set_feedback(adev, snd_device, false);
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001578 } else {
1579 if (platform_split_snd_device(adev->platform,
1580 snd_device,
1581 &num_devices,
1582 new_snd_devices) == 0) {
1583 for (i = 0; i < num_devices; i++) {
1584 adev->snd_dev_ref_cnt[new_snd_devices[i]]--;
1585 }
1586 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001587 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001588
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001589 return 0;
1590}
1591
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001592/*
1593 legend:
1594 uc - existing usecase
1595 new_uc - new usecase
1596 d1, d11, d2 - SND_DEVICE enums
1597 a1, a2 - corresponding ANDROID device enums
1598 B1, B2 - backend strings
1599
1600case 1
1601 uc->dev d1 (a1) B1
1602 new_uc->dev d1 (a1), d2 (a2) B1, B2
1603
1604 resolution: disable and enable uc->dev on d1
1605
1606case 2
1607 uc->dev d1 (a1) B1
1608 new_uc->dev d11 (a1) B1
1609
1610 resolution: need to switch uc since d1 and d11 are related
1611 (e.g. speaker and voice-speaker)
1612 use ANDROID_DEVICE_OUT enums to match devices since SND_DEVICE enums may vary
1613
1614case 3
1615 uc->dev d1 (a1) B1
1616 new_uc->dev d2 (a2) B2
1617
1618 resolution: no need to switch uc
1619
1620case 4
1621 uc->dev d1 (a1) B1
1622 new_uc->dev d2 (a2) B1
1623
1624 resolution: disable enable uc-dev on d2 since backends match
1625 we cannot enable two streams on two different devices if they
1626 share the same backend. e.g. if offload is on speaker device using
1627 QUAD_MI2S backend and a low-latency stream is started on voice-handset
1628 using the same backend, offload must also be switched to voice-handset.
1629
1630case 5
1631 uc->dev d1 (a1) B1
1632 new_uc->dev d1 (a1), d2 (a2) B1
1633
1634 resolution: disable enable uc-dev on d2 since backends match
1635 we cannot enable two streams on two different devices if they
1636 share the same backend.
1637
1638case 6
1639 uc->dev d1 (a1) B1
1640 new_uc->dev d2 (a1) B2
1641
1642 resolution: no need to switch
1643
1644case 7
1645 uc->dev d1 (a1), d2 (a2) B1, B2
1646 new_uc->dev d1 (a1) B1
1647
1648 resolution: no need to switch
1649
Zhou Song4ba65882018-07-09 14:48:07 +08001650case 8
1651 uc->dev d1 (a1) B1
1652 new_uc->dev d11 (a1), d2 (a2) B1, B2
1653 resolution: compared to case 1, for this case, d1 and d11 are related
1654 then need to do the same as case 2 to siwtch to new uc
Preetam Singh Ranawatd399e6f2020-06-25 12:20:36 +05301655
1656case 9
1657 uc->dev d1 (a1), d2(a2) B1 B2
1658 new_uc->dev d1 (a1), d22 (a2) B1, B2
1659 resolution: disable enable uc-dev on d2 since backends match
1660 we cannot enable two streams on two different devices if they
1661 share the same backend. This is special case for combo use case
1662 with a2dp and sco devices which uses same backend.
1663 e.g. speaker-a2dp and speaker-btsco
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001664*/
1665static snd_device_t derive_playback_snd_device(void * platform,
1666 struct audio_usecase *uc,
1667 struct audio_usecase *new_uc,
1668 snd_device_t new_snd_device)
1669{
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001670 struct listnode a1, a2;
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001671
1672 snd_device_t d1 = uc->out_snd_device;
1673 snd_device_t d2 = new_snd_device;
1674
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001675 list_init(&a1);
1676 list_init(&a2);
1677
Siddartha Shaik31b530e2017-05-19 15:26:33 +05301678 switch (uc->type) {
Surendar Karka93cd25a2018-08-28 14:21:37 +05301679 case TRANSCODE_LOOPBACK_RX :
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001680 assign_devices(&a1, &uc->stream.inout->out_config.device_list);
1681 assign_devices(&a2, &new_uc->stream.inout->out_config.device_list);
Siddartha Shaik31b530e2017-05-19 15:26:33 +05301682 break;
1683 default :
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001684 assign_devices(&a1, &uc->stream.out->device_list);
1685 assign_devices(&a2, &new_uc->stream.out->device_list);
Siddartha Shaik31b530e2017-05-19 15:26:33 +05301686 break;
1687 }
1688
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001689 // Treat as a special case when a1 and a2 are not disjoint
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001690 if (!compare_devices(&a1, &a2) &&
1691 compare_devices_for_any_match(&a1 ,&a2)) {
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001692 snd_device_t d3[2];
1693 int num_devices = 0;
1694 int ret = platform_split_snd_device(platform,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001695 list_length(&a1) > 1 ? d1 : d2,
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001696 &num_devices,
1697 d3);
1698 if (ret < 0) {
1699 if (ret != -ENOSYS) {
1700 ALOGW("%s failed to split snd_device %d",
1701 __func__,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001702 list_length(&a1) > 1 ? d1 : d2);
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001703 }
1704 goto end;
1705 }
1706
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001707 if (platform_check_backends_match(d3[0], d3[1])) {
1708 return d2; // case 5
1709 } else {
Preetam Singh Ranawatd399e6f2020-06-25 12:20:36 +05301710 if ((list_length(&a1) > 1) && (list_length(&a2) > 1) &&
1711 platform_check_backends_match(d1, d2))
1712 return d2; //case 9
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001713 if (list_length(&a1) > 1)
Samyak Jaind826b502019-07-17 16:16:42 +05301714 return d1; //case 7
Garmond Leungb9eeba42018-09-18 11:10:41 -07001715 // check if d1 is related to any of d3's
1716 if (d1 == d3[0] || d1 == d3[1])
Zhou Song4ba65882018-07-09 14:48:07 +08001717 return d1; // case 1
1718 else
1719 return d3[1]; // case 8
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001720 }
1721 } else {
1722 if (platform_check_backends_match(d1, d2)) {
1723 return d2; // case 2, 4
1724 } else {
1725 return d1; // case 6, 3
1726 }
1727 }
1728
1729end:
1730 return d2; // return whatever was calculated before.
1731}
1732
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001733static void check_usecases_codec_backend(struct audio_device *adev,
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301734 struct audio_usecase *uc_info,
1735 snd_device_t snd_device)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001736{
1737 struct listnode *node;
1738 struct audio_usecase *usecase;
1739 bool switch_device[AUDIO_USECASE_MAX];
Chaithanya Krishna Bacharaju49e7db02017-03-14 11:57:26 +05301740 snd_device_t uc_derive_snd_device;
1741 snd_device_t derive_snd_device[AUDIO_USECASE_MAX];
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001742 snd_device_t split_snd_devices[SND_DEVICE_OUT_END];
1743 int i, num_uc_to_switch = 0, num_devices = 0;
kunleiz5cd52b82016-11-07 17:22:52 +08001744 int status = 0;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301745 bool force_restart_session = false;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001746 /*
1747 * This function is to make sure that all the usecases that are active on
1748 * the hardware codec backend are always routed to any one device that is
1749 * handled by the hardware codec.
1750 * For example, if low-latency and deep-buffer usecases are currently active
1751 * on speaker and out_set_parameters(headset) is received on low-latency
1752 * output, then we have to make sure deep-buffer is also switched to headset,
1753 * because of the limitation that both the devices cannot be enabled
1754 * at the same time as they share the same backend.
1755 */
Mingming Yin3ee55c62014-08-04 14:23:35 -07001756 /*
1757 * This call is to check if we need to force routing for a particular stream
1758 * If there is a backend configuration change for the device when a
1759 * new stream starts, then ADM needs to be closed and re-opened with the new
1760 * configuraion. This call check if we need to re-route all the streams
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -08001761 * associated with the backend. Touch tone + 24 bit + native playback.
Mingming Yin3ee55c62014-08-04 14:23:35 -07001762 */
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -08001763 bool force_routing = platform_check_and_set_codec_backend_cfg(adev, uc_info,
1764 snd_device);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301765 /* For a2dp device reconfigure all active sessions
1766 * with new AFE encoder format based on a2dp state
1767 */
1768 if ((SND_DEVICE_OUT_BT_A2DP == snd_device ||
Preetam Singh Ranawatd399e6f2020-06-25 12:20:36 +05301769 SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP == snd_device ||
1770 SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP == snd_device) &&
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301771 audio_extn_a2dp_is_force_device_switch()) {
1772 force_routing = true;
1773 force_restart_session = true;
1774 }
Kunlei Zhange7cab1a2020-05-17 20:31:05 +08001775
1776 /*
1777 * Island cfg and power mode config needs to set before AFE port start.
1778 * Set force routing in case of voice device was enable before.
1779 */
1780 if (uc_info->type == VOICE_CALL &&
1781 voice_extn_is_voice_power_mode_supported() &&
1782 platform_check_and_update_island_power_status(adev->platform,
1783 uc_info,
1784 snd_device)) {
1785 force_routing = true;
1786 ALOGD("%s:becf: force routing %d for power mode supported device",
1787 __func__, force_routing);
1788 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301789 ALOGD("%s:becf: force routing %d", __func__, force_routing);
1790
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001791 /* Disable all the usecases on the shared backend other than the
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -08001792 * specified usecase.
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -08001793 */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001794 for (i = 0; i < AUDIO_USECASE_MAX; i++)
1795 switch_device[i] = false;
1796
1797 list_for_each(node, &adev->usecase_list) {
1798 usecase = node_to_item(node, struct audio_usecase, list);
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -08001799
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301800 ALOGD("%s:becf: (%d) check_usecases curr device: %s, usecase device:%s "
1801 "backends match %d",__func__, i,
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301802 platform_get_snd_device_name(snd_device),
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301803 platform_get_snd_device_name(usecase->out_snd_device),
1804 platform_check_backends_match(snd_device, usecase->out_snd_device));
Rahul Sharma99770982019-03-06 17:05:26 +05301805 if ((usecase->type != PCM_CAPTURE) && (usecase != uc_info) &&
1806 (usecase->type != PCM_PASSTHROUGH)) {
Ashish Jain6a65b352017-03-21 17:24:40 +05301807 uc_derive_snd_device = derive_playback_snd_device(adev->platform,
1808 usecase, uc_info, snd_device);
1809 if (((uc_derive_snd_device != usecase->out_snd_device) || force_routing) &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001810 (is_codec_backend_out_device_type(&usecase->device_list) ||
1811 compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_AUX_DIGITAL) ||
1812 compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_USB_DEVICE) ||
1813 compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_USB_HEADSET) ||
1814 is_a2dp_out_device_type(&usecase->device_list) ||
1815 is_sco_out_device_type(&usecase->device_list)) &&
Ashish Jain6a65b352017-03-21 17:24:40 +05301816 ((force_restart_session) ||
1817 (platform_check_backends_match(snd_device, usecase->out_snd_device)))) {
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301818 ALOGD("%s:becf: check_usecases (%s) is active on (%s) - disabling ..",
1819 __func__, use_case_table[usecase->id],
1820 platform_get_snd_device_name(usecase->out_snd_device));
1821 disable_audio_route(adev, usecase);
1822 switch_device[usecase->id] = true;
Chaithanya Krishna Bacharaju49e7db02017-03-14 11:57:26 +05301823 /* Enable existing usecase on derived playback device */
1824 derive_snd_device[usecase->id] = uc_derive_snd_device;
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301825 num_uc_to_switch++;
Ashish Jain6a65b352017-03-21 17:24:40 +05301826 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001827 }
1828 }
1829
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301830 ALOGD("%s:becf: check_usecases num.of Usecases to switch %d", __func__,
1831 num_uc_to_switch);
1832
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001833 if (num_uc_to_switch) {
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07001834 /* All streams have been de-routed. Disable the device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001835
Venkata Narendra Kumar Gutta7610e632014-04-14 23:16:38 +05301836 /* Make sure the previous devices to be disabled first and then enable the
1837 selected devices */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001838 list_for_each(node, &adev->usecase_list) {
1839 usecase = node_to_item(node, struct audio_usecase, list);
1840 if (switch_device[usecase->id]) {
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001841 /* Check if output sound device to be switched can be split and if any
1842 of the split devices match with derived sound device */
1843 if (platform_split_snd_device(adev->platform, usecase->out_snd_device,
1844 &num_devices, split_snd_devices) == 0) {
1845 adev->snd_dev_ref_cnt[usecase->out_snd_device]--;
1846 for (i = 0; i < num_devices; i++) {
1847 /* Disable devices that do not match with derived sound device */
1848 if (split_snd_devices[i] != derive_snd_device[usecase->id])
1849 disable_snd_device(adev, split_snd_devices[i]);
1850 }
1851 } else {
1852 disable_snd_device(adev, usecase->out_snd_device);
1853 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001854 }
1855 }
1856
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -07001857 list_for_each(node, &adev->usecase_list) {
1858 usecase = node_to_item(node, struct audio_usecase, list);
1859 if (switch_device[usecase->id]) {
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001860 if (platform_split_snd_device(adev->platform, usecase->out_snd_device,
1861 &num_devices, split_snd_devices) == 0) {
1862 /* Enable derived sound device only if it does not match with
1863 one of the split sound devices. This is because the matching
1864 sound device was not disabled */
1865 bool should_enable = true;
1866 for (i = 0; i < num_devices; i++) {
1867 if (derive_snd_device[usecase->id] == split_snd_devices[i]) {
1868 should_enable = false;
1869 break;
1870 }
1871 }
1872 if (should_enable)
1873 enable_snd_device(adev, derive_snd_device[usecase->id]);
1874 } else {
1875 enable_snd_device(adev, derive_snd_device[usecase->id]);
1876 }
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -07001877 }
1878 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001879
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001880 /* Re-route all the usecases on the shared backend other than the
1881 specified usecase to new snd devices */
1882 list_for_each(node, &adev->usecase_list) {
1883 usecase = node_to_item(node, struct audio_usecase, list);
Karthik Reddy Katta3abfee22016-02-23 10:55:27 +05301884 /* Update the out_snd_device only before enabling the audio route */
1885 if (switch_device[usecase->id]) {
Chaithanya Krishna Bacharaju49e7db02017-03-14 11:57:26 +05301886 usecase->out_snd_device = derive_snd_device[usecase->id];
Karthik Reddy Katta3abfee22016-02-23 10:55:27 +05301887 if (usecase->type != VOICE_CALL) {
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301888 ALOGD("%s:becf: enabling usecase (%s) on (%s)", __func__,
Karthik Reddy Katta3abfee22016-02-23 10:55:27 +05301889 use_case_table[usecase->id],
1890 platform_get_snd_device_name(usecase->out_snd_device));
kunleiz5cd52b82016-11-07 17:22:52 +08001891 /* Update voc calibration before enabling VoIP route */
1892 if (usecase->type == VOIP_CALL)
1893 status = platform_switch_voice_call_device_post(adev->platform,
Eric Laurent637e2d42018-11-15 12:24:31 -08001894 usecase->out_snd_device,
1895 platform_get_input_snd_device(
1896 adev->platform, NULL,
Jaideep Sharma477917f2020-03-13 18:13:33 +05301897 &uc_info->device_list,
1898 usecase->type));
Avinash Vaish71a8b972014-07-24 15:36:33 +05301899 enable_audio_route(adev, usecase);
Sujin Panicker390724d2019-04-26 10:43:36 +05301900 if (usecase->stream.out && usecase->id == USECASE_AUDIO_PLAYBACK_VOIP) {
kunleiz46c47dd2018-03-19 16:28:09 +08001901 out_set_voip_volume(&usecase->stream.out->stream,
1902 usecase->stream.out->volume_l,
1903 usecase->stream.out->volume_r);
1904 }
Karthik Reddy Katta3abfee22016-02-23 10:55:27 +05301905 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001906 }
1907 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001908 }
1909}
1910
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05301911static void check_usecases_capture_codec_backend(struct audio_device *adev,
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001912 struct audio_usecase *uc_info,
1913 snd_device_t snd_device)
1914{
1915 struct listnode *node;
1916 struct audio_usecase *usecase;
1917 bool switch_device[AUDIO_USECASE_MAX];
1918 int i, num_uc_to_switch = 0;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001919 int backend_check_cond = is_codec_backend_out_device_type(&uc_info->device_list);
kunleiz5cd52b82016-11-07 17:22:52 +08001920 int status = 0;
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001921
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05301922 bool force_routing = platform_check_and_set_capture_codec_backend_cfg(adev, uc_info,
1923 snd_device);
1924 ALOGD("%s:becf: force routing %d", __func__, force_routing);
Dhanalakshmi Siddanib678a802016-12-03 11:51:41 +05301925
1926 /*
1927 * Make sure out devices is checked against out codec backend device and
1928 * also in devices against in codec backend. Checking out device against in
1929 * codec backend or vice versa causes issues.
1930 */
1931 if (uc_info->type == PCM_CAPTURE)
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001932 backend_check_cond = is_codec_backend_in_device_type(&uc_info->device_list);
Kunlei Zhange7cab1a2020-05-17 20:31:05 +08001933
1934 /*
1935 * Island cfg and power mode config needs to set before AFE port start.
1936 * Set force routing in case of voice device was enable before.
1937 */
1938
1939 if (uc_info->type == VOICE_CALL &&
1940 voice_extn_is_voice_power_mode_supported() &&
1941 platform_check_and_update_island_power_status(adev->platform,
1942 uc_info,
1943 snd_device)) {
1944 force_routing = true;
1945 ALOGD("%s:becf: force routing %d for power mode supported device",
1946 __func__, force_routing);
1947 }
1948
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001949 /*
1950 * This function is to make sure that all the active capture usecases
1951 * are always routed to the same input sound device.
1952 * For example, if audio-record and voice-call usecases are currently
1953 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
1954 * is received for voice call then we have to make sure that audio-record
1955 * usecase is also switched to earpiece i.e. voice-dmic-ef,
1956 * because of the limitation that two devices cannot be enabled
1957 * at the same time if they share the same backend.
1958 */
1959 for (i = 0; i < AUDIO_USECASE_MAX; i++)
1960 switch_device[i] = false;
1961
1962 list_for_each(node, &adev->usecase_list) {
1963 usecase = node_to_item(node, struct audio_usecase, list);
Dhanalakshmi Siddanib678a802016-12-03 11:51:41 +05301964 /*
1965 * TODO: Enhance below condition to handle BT sco/USB multi recording
1966 */
Jaideep Sharma477917f2020-03-13 18:13:33 +05301967
1968 bool capture_uc_needs_routing = usecase->type != PCM_PLAYBACK && (usecase != uc_info &&
1969 (usecase->in_snd_device != snd_device || force_routing));
1970 bool call_proxy_snd_device = platform_is_call_proxy_snd_device(snd_device) ||
1971 platform_is_call_proxy_snd_device(usecase->in_snd_device);
1972 if (capture_uc_needs_routing && !call_proxy_snd_device &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001973 ((backend_check_cond &&
1974 (is_codec_backend_in_device_type(&usecase->device_list) ||
Aniket Kumar Lata9c2fd892020-01-22 22:20:00 -08001975 (usecase->type == VOIP_CALL))) ||
Carter Hsu1d2a0532018-10-04 09:24:36 +08001976 ((uc_info->type == VOICE_CALL &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001977 is_single_device_type_equal(&usecase->device_list,
1978 AUDIO_DEVICE_IN_VOICE_CALL)) ||
Carter Hsu1d2a0532018-10-04 09:24:36 +08001979 platform_check_backends_match(snd_device,\
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08001980 usecase->in_snd_device))) &&
Anish Kumara020a7c2014-10-17 11:13:22 -07001981 (usecase->id != USECASE_AUDIO_SPKR_CALIB_TX)) {
Jaideep Sharma477917f2020-03-13 18:13:33 +05301982 ALOGD("%s: Usecase (%s) is active on (%s) - disabling ..",
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001983 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -07001984 platform_get_snd_device_name(usecase->in_snd_device));
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001985 disable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001986 switch_device[usecase->id] = true;
1987 num_uc_to_switch++;
1988 }
1989 }
1990
1991 if (num_uc_to_switch) {
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07001992 /* All streams have been de-routed. Disable the device */
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001993
Venkata Narendra Kumar Gutta7610e632014-04-14 23:16:38 +05301994 /* Make sure the previous devices to be disabled first and then enable the
1995 selected devices */
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001996 list_for_each(node, &adev->usecase_list) {
1997 usecase = node_to_item(node, struct audio_usecase, list);
1998 if (switch_device[usecase->id]) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001999 disable_snd_device(adev, usecase->in_snd_device);
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -08002000 }
2001 }
2002
2003 list_for_each(node, &adev->usecase_list) {
2004 usecase = node_to_item(node, struct audio_usecase, list);
2005 if (switch_device[usecase->id]) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07002006 enable_snd_device(adev, snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002007 }
2008 }
2009
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002010 /* Re-route all the usecases on the shared backend other than the
2011 specified usecase to new snd devices */
2012 list_for_each(node, &adev->usecase_list) {
2013 usecase = node_to_item(node, struct audio_usecase, list);
2014 /* Update the in_snd_device only before enabling the audio route */
2015 if (switch_device[usecase->id] ) {
2016 usecase->in_snd_device = snd_device;
kunleiz5cd52b82016-11-07 17:22:52 +08002017 if (usecase->type != VOICE_CALL) {
2018 /* Update voc calibration before enabling VoIP route */
Jaideep Sharma477917f2020-03-13 18:13:33 +05302019 if (usecase->type == VOIP_CALL) {
2020 snd_device_t voip_snd_device;
2021 voip_snd_device = platform_get_output_snd_device(adev->platform,
2022 uc_info->stream.out,
2023 usecase->type);
kunleiz5cd52b82016-11-07 17:22:52 +08002024 status = platform_switch_voice_call_device_post(adev->platform,
Jaideep Sharma477917f2020-03-13 18:13:33 +05302025 voip_snd_device,
kunleiz5cd52b82016-11-07 17:22:52 +08002026 usecase->in_snd_device);
Jaideep Sharma477917f2020-03-13 18:13:33 +05302027 }
Avinash Vaish71a8b972014-07-24 15:36:33 +05302028 enable_audio_route(adev, usecase);
kunleiz5cd52b82016-11-07 17:22:52 +08002029 }
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002030 }
2031 }
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002032 }
2033}
2034
Mingming Yin3a941d42016-02-17 18:08:05 -08002035static void reset_hdmi_sink_caps(struct stream_out *out) {
2036 int i = 0;
2037
2038 for (i = 0; i<= MAX_SUPPORTED_CHANNEL_MASKS; i++) {
2039 out->supported_channel_masks[i] = 0;
2040 }
2041 for (i = 0; i<= MAX_SUPPORTED_FORMATS; i++) {
2042 out->supported_formats[i] = 0;
2043 }
2044 for (i = 0; i<= MAX_SUPPORTED_SAMPLE_RATES; i++) {
2045 out->supported_sample_rates[i] = 0;
2046 }
2047}
2048
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002049/* must be called with hw device mutex locked */
Mingming Yin3a941d42016-02-17 18:08:05 -08002050static int read_hdmi_sink_caps(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002051{
Mingming Yin3a941d42016-02-17 18:08:05 -08002052 int ret = 0, i = 0, j = 0;
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07002053 int channels = platform_edid_get_max_channels_v2(out->dev->platform,
2054 out->extconn.cs.controller,
2055 out->extconn.cs.stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002056
Mingming Yin3a941d42016-02-17 18:08:05 -08002057 reset_hdmi_sink_caps(out);
2058
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07002059 /* Cache ext disp type */
Vignesh Kulothungan1b3957e2019-12-02 16:16:51 -08002060 ret = platform_get_ext_disp_type_v2(adev->platform,
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07002061 out->extconn.cs.controller,
Vignesh Kulothungan1b3957e2019-12-02 16:16:51 -08002062 out->extconn.cs.stream);
2063 if(ret < 0) {
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07002064 ALOGE("%s: Failed to query disp type, ret:%d", __func__, ret);
Garmond Leung37850ab2016-10-06 11:42:18 -07002065 return -EINVAL;
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07002066 }
2067
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002068 switch (channels) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002069 case 8:
Mingming Yin3a941d42016-02-17 18:08:05 -08002070 ALOGV("%s: HDMI supports 7.1 channels", __func__);
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -07002071 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_7POINT1;
Mingming Yin3a941d42016-02-17 18:08:05 -08002072 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_6POINT1;
2073 case 6:
2074 ALOGV("%s: HDMI supports 5.1 channels", __func__);
2075 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_5POINT1;
2076 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_PENTA;
2077 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_QUAD;
2078 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_SURROUND;
2079 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_2POINT1;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002080 break;
2081 default:
Mingming Yin3a941d42016-02-17 18:08:05 -08002082 ALOGE("invalid/nonstandard channal count[%d]",channels);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002083 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002084 break;
2085 }
Mingming Yin3a941d42016-02-17 18:08:05 -08002086
2087 // check channel format caps
2088 i = 0;
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07002089 if (platform_is_edid_supported_format_v2(out->dev->platform, AUDIO_FORMAT_AC3,
2090 out->extconn.cs.controller,
2091 out->extconn.cs.stream)) {
Mingming Yin3a941d42016-02-17 18:08:05 -08002092 ALOGV(":%s HDMI supports AC3/EAC3 formats", __func__);
2093 out->supported_formats[i++] = AUDIO_FORMAT_AC3;
2094 //Adding EAC3/EAC3_JOC formats if AC3 is supported by the sink.
2095 //EAC3/EAC3_JOC will be converted to AC3 for decoding if needed
2096 out->supported_formats[i++] = AUDIO_FORMAT_E_AC3;
2097 out->supported_formats[i++] = AUDIO_FORMAT_E_AC3_JOC;
2098 }
2099
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07002100 if (platform_is_edid_supported_format_v2(out->dev->platform, AUDIO_FORMAT_DOLBY_TRUEHD,
2101 out->extconn.cs.controller,
2102 out->extconn.cs.stream)) {
Ben Romberger1aaaf862017-04-06 17:49:46 -07002103 ALOGV(":%s HDMI supports TRUE HD format", __func__);
2104 out->supported_formats[i++] = AUDIO_FORMAT_DOLBY_TRUEHD;
2105 }
2106
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07002107 if (platform_is_edid_supported_format_v2(out->dev->platform, AUDIO_FORMAT_DTS,
2108 out->extconn.cs.controller,
2109 out->extconn.cs.stream)) {
Mingming Yin3a941d42016-02-17 18:08:05 -08002110 ALOGV(":%s HDMI supports DTS format", __func__);
2111 out->supported_formats[i++] = AUDIO_FORMAT_DTS;
2112 }
2113
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07002114 if (platform_is_edid_supported_format_v2(out->dev->platform, AUDIO_FORMAT_DTS_HD,
2115 out->extconn.cs.controller,
2116 out->extconn.cs.stream)) {
Mingming Yin3a941d42016-02-17 18:08:05 -08002117 ALOGV(":%s HDMI supports DTS HD format", __func__);
2118 out->supported_formats[i++] = AUDIO_FORMAT_DTS_HD;
2119 }
2120
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07002121 if (platform_is_edid_supported_format_v2(out->dev->platform, AUDIO_FORMAT_IEC61937,
2122 out->extconn.cs.controller,
2123 out->extconn.cs.stream)) {
Naresh Tanniru928f0862017-04-07 16:44:23 -07002124 ALOGV(":%s HDMI supports IEC61937 format", __func__);
2125 out->supported_formats[i++] = AUDIO_FORMAT_IEC61937;
2126 }
2127
Mingming Yin3a941d42016-02-17 18:08:05 -08002128
2129 // check sample rate caps
2130 i = 0;
2131 for (j = 0; j < MAX_SUPPORTED_SAMPLE_RATES; j++) {
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07002132 if (platform_is_edid_supported_sample_rate_v2(out->dev->platform, out_hdmi_sample_rates[j],
2133 out->extconn.cs.controller,
2134 out->extconn.cs.stream)) {
Mingming Yin3a941d42016-02-17 18:08:05 -08002135 ALOGV(":%s HDMI supports sample rate:%d", __func__, out_hdmi_sample_rates[j]);
2136 out->supported_sample_rates[i++] = out_hdmi_sample_rates[j];
2137 }
2138 }
2139
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002140 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002141}
2142
Haynes Mathew George484e8d22017-07-31 18:55:17 -07002143static inline ssize_t read_usb_sup_sample_rates(bool is_playback __unused,
2144 uint32_t *supported_sample_rates __unused,
2145 uint32_t max_rates __unused)
2146{
2147 ssize_t count = audio_extn_usb_get_sup_sample_rates(is_playback,
2148 supported_sample_rates,
2149 max_rates);
Ashish Jain4847e9d2017-08-17 19:16:57 +05302150 ssize_t i = 0;
2151
2152 for (i=0; i<count; i++) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07002153 ALOGV("%s %s %d", __func__, is_playback ? "P" : "C",
2154 supported_sample_rates[i]);
2155 }
2156 return count;
2157}
2158
2159static inline int read_usb_sup_channel_masks(bool is_playback,
2160 audio_channel_mask_t *supported_channel_masks,
2161 uint32_t max_masks)
2162{
2163 int channels = audio_extn_usb_get_max_channels(is_playback);
2164 int channel_count;
2165 uint32_t num_masks = 0;
2166 if (channels > MAX_HIFI_CHANNEL_COUNT)
2167 channels = MAX_HIFI_CHANNEL_COUNT;
2168
2169 if (is_playback) {
Eric Laurent68a87112019-05-01 18:07:29 -07002170 // start from 2 channels as framework currently doesn't support mono.
2171 if (channels >= FCC_2) {
2172 supported_channel_masks[num_masks++] = audio_channel_out_mask_from_count(FCC_2);
2173 }
2174 for (channel_count = FCC_2;
2175 channel_count <= channels && num_masks < max_masks;
2176 ++channel_count) {
2177 supported_channel_masks[num_masks++] =
2178 audio_channel_mask_for_index_assignment_from_count(channel_count);
2179 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07002180 } else {
vincenttewf51c94e2019-05-07 10:28:53 +08002181 // For capture we report all supported channel masks from 1 channel up.
2182 channel_count = MIN_CHANNEL_COUNT;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07002183 // audio_channel_in_mask_from_count() does the right conversion to either positional or
2184 // indexed mask
Eric Laurent68a87112019-05-01 18:07:29 -07002185 for ( ; channel_count <= channels && num_masks < max_masks; channel_count++) {
2186 audio_channel_mask_t mask = AUDIO_CHANNEL_NONE;
2187 if (channel_count <= FCC_2) {
2188 mask = audio_channel_in_mask_from_count(channel_count);
2189 supported_channel_masks[num_masks++] = mask;
2190 }
2191 const audio_channel_mask_t index_mask =
2192 audio_channel_mask_for_index_assignment_from_count(channel_count);
2193 if (mask != index_mask && num_masks < max_masks) { // ensure index mask added.
2194 supported_channel_masks[num_masks++] = index_mask;
2195 }
2196 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07002197 }
Lakshman Chaluvarajub79fafd2018-11-21 10:24:37 +05302198
vincenttewf51c94e2019-05-07 10:28:53 +08002199 for (size_t i = 0; i < num_masks; ++i) {
2200 ALOGV("%s: %s supported ch %d supported_channel_masks[%zu] %08x num_masks %d", __func__,
2201 is_playback ? "P" : "C", channels, i, supported_channel_masks[i], num_masks);
Lakshman Chaluvarajub79fafd2018-11-21 10:24:37 +05302202 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07002203 return num_masks;
2204}
2205
2206static inline int read_usb_sup_formats(bool is_playback __unused,
2207 audio_format_t *supported_formats,
2208 uint32_t max_formats __unused)
2209{
2210 int bitwidth = audio_extn_usb_get_max_bit_width(is_playback);
2211 switch (bitwidth) {
2212 case 24:
2213 // XXX : usb.c returns 24 for s24 and s24_le?
2214 supported_formats[0] = AUDIO_FORMAT_PCM_24_BIT_PACKED;
2215 break;
2216 case 32:
2217 supported_formats[0] = AUDIO_FORMAT_PCM_32_BIT;
2218 break;
2219 case 16:
2220 default :
2221 supported_formats[0] = AUDIO_FORMAT_PCM_16_BIT;
2222 break;
2223 }
2224 ALOGV("%s: %s supported format %d", __func__,
2225 is_playback ? "P" : "C", bitwidth);
2226 return 1;
2227}
2228
2229static inline int read_usb_sup_params_and_compare(bool is_playback,
2230 audio_format_t *format,
2231 audio_format_t *supported_formats,
2232 uint32_t max_formats,
2233 audio_channel_mask_t *mask,
2234 audio_channel_mask_t *supported_channel_masks,
2235 uint32_t max_masks,
2236 uint32_t *rate,
2237 uint32_t *supported_sample_rates,
2238 uint32_t max_rates) {
2239 int ret = 0;
2240 int num_formats;
2241 int num_masks;
2242 int num_rates;
2243 int i;
2244
2245 num_formats = read_usb_sup_formats(is_playback, supported_formats,
2246 max_formats);
2247 num_masks = read_usb_sup_channel_masks(is_playback, supported_channel_masks,
2248 max_masks);
2249
2250 num_rates = read_usb_sup_sample_rates(is_playback,
2251 supported_sample_rates, max_rates);
2252
2253#define LUT(table, len, what, dflt) \
2254 for (i=0; i<len && (table[i] != what); i++); \
2255 if (i==len) { ret |= (what == dflt ? 0 : -1); what=table[0]; }
2256
2257 LUT(supported_formats, num_formats, *format, AUDIO_FORMAT_DEFAULT);
2258 LUT(supported_channel_masks, num_masks, *mask, AUDIO_CHANNEL_NONE);
2259 LUT(supported_sample_rates, num_rates, *rate, 0);
2260
2261#undef LUT
2262 return ret < 0 ? -EINVAL : 0; // HACK TBD
2263}
2264
Alexy Josephb1379942016-01-29 15:49:38 -08002265audio_usecase_t get_usecase_id_from_usecase_type(const struct audio_device *adev,
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -08002266 usecase_type_t type)
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002267{
2268 struct audio_usecase *usecase;
2269 struct listnode *node;
2270
2271 list_for_each(node, &adev->usecase_list) {
2272 usecase = node_to_item(node, struct audio_usecase, list);
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -08002273 if (usecase->type == type) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002274 ALOGV("%s: usecase id %d", __func__, usecase->id);
2275 return usecase->id;
2276 }
2277 }
2278 return USECASE_INVALID;
2279}
2280
Alexy Josephb1379942016-01-29 15:49:38 -08002281struct audio_usecase *get_usecase_from_list(const struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002282 audio_usecase_t uc_id)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002283{
2284 struct audio_usecase *usecase;
2285 struct listnode *node;
2286
2287 list_for_each(node, &adev->usecase_list) {
2288 usecase = node_to_item(node, struct audio_usecase, list);
2289 if (usecase->id == uc_id)
2290 return usecase;
2291 }
2292 return NULL;
2293}
2294
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302295/*
2296 * is a true native playback active
2297 */
2298bool audio_is_true_native_stream_active(struct audio_device *adev)
2299{
2300 bool active = false;
2301 int i = 0;
2302 struct listnode *node;
2303
2304 if (NATIVE_AUDIO_MODE_TRUE_44_1 != platform_get_native_support()) {
2305 ALOGV("%s:napb: not in true mode or non hdphones device",
2306 __func__);
2307 active = false;
2308 goto exit;
2309 }
2310
2311 list_for_each(node, &adev->usecase_list) {
2312 struct audio_usecase *uc;
2313 uc = node_to_item(node, struct audio_usecase, list);
2314 struct stream_out *curr_out =
2315 (struct stream_out*) uc->stream.out;
2316
2317 if (curr_out && PCM_PLAYBACK == uc->type) {
2318 ALOGD("%s:napb: (%d) (%s)id (%d) sr %d bw "
2319 "(%d) device %s", __func__, i++, use_case_table[uc->id],
2320 uc->id, curr_out->sample_rate,
2321 curr_out->bit_width,
2322 platform_get_snd_device_name(uc->out_snd_device));
2323
2324 if (is_offload_usecase(uc->id) &&
2325 (curr_out->sample_rate == OUTPUT_SAMPLING_RATE_44100)) {
2326 active = true;
2327 ALOGD("%s:napb:native stream detected", __func__);
2328 }
2329 }
2330 }
2331exit:
2332 return active;
2333}
2334
Xiaojun Sang785b5da2017-08-03 15:52:29 +08002335uint32_t adev_get_dsp_bit_width_enforce_mode()
2336{
2337 if (adev == NULL) {
2338 ALOGE("%s: adev is null. Disable DSP bit width enforce mode.\n", __func__);
2339 return 0;
2340 }
2341 return adev->dsp_bit_width_enforce_mode;
2342}
2343
2344static uint32_t adev_init_dsp_bit_width_enforce_mode(struct mixer *mixer)
2345{
2346 char value[PROPERTY_VALUE_MAX];
2347 int trial;
2348 uint32_t dsp_bit_width_enforce_mode = 0;
2349
2350 if (!mixer) {
2351 ALOGE("%s: adev mixer is null. cannot update DSP bitwidth.\n",
2352 __func__);
2353 return 0;
2354 }
2355
2356 if (property_get("persist.vendor.audio_hal.dsp_bit_width_enforce_mode",
2357 value, NULL) > 0) {
2358 trial = atoi(value);
2359 switch (trial) {
2360 case 16:
2361 dsp_bit_width_enforce_mode = 16;
2362 break;
2363 case 24:
2364 dsp_bit_width_enforce_mode = 24;
2365 break;
2366 case 32:
2367 dsp_bit_width_enforce_mode = 32;
2368 break;
2369 default:
2370 dsp_bit_width_enforce_mode = 0;
2371 ALOGD("%s Dynamic DSP bitwidth config is disabled.", __func__);
2372 break;
2373 }
2374 }
2375
2376 return dsp_bit_width_enforce_mode;
2377}
2378
2379static void audio_enable_asm_bit_width_enforce_mode(struct mixer *mixer,
2380 uint32_t enforce_mode,
2381 bool enable)
2382{
2383 struct mixer_ctl *ctl = NULL;
2384 const char *mixer_ctl_name = "ASM Bit Width";
2385 uint32_t asm_bit_width_mode = 0;
2386
2387 if (enforce_mode == 0) {
2388 ALOGD("%s: DSP bitwidth feature is disabled.", __func__);
2389 return;
2390 }
2391
2392 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
2393 if (!ctl) {
2394 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2395 __func__, mixer_ctl_name);
2396 return;
2397 }
2398
2399 if (enable)
2400 asm_bit_width_mode = enforce_mode;
2401 else
2402 asm_bit_width_mode = 0;
2403
2404 ALOGV("%s DSP bit width feature status is %d width=%d",
2405 __func__, enable, asm_bit_width_mode);
2406 if (mixer_ctl_set_value(ctl, 0, asm_bit_width_mode) < 0)
2407 ALOGE("%s: Could not set ASM biwidth %d", __func__,
2408 asm_bit_width_mode);
2409
2410 return;
2411}
2412
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05302413/*
2414 * if native DSD playback active
2415 */
2416bool audio_is_dsd_native_stream_active(struct audio_device *adev)
2417{
2418 bool active = false;
2419 struct listnode *node = NULL;
2420 struct audio_usecase *uc = NULL;
2421 struct stream_out *curr_out = NULL;
2422
2423 list_for_each(node, &adev->usecase_list) {
2424 uc = node_to_item(node, struct audio_usecase, list);
2425 curr_out = (struct stream_out*) uc->stream.out;
2426
2427 if (curr_out && PCM_PLAYBACK == uc->type &&
2428 (DSD_NATIVE_BACKEND == platform_get_backend_index(uc->out_snd_device))) {
2429 active = true;
2430 ALOGV("%s:DSD playback is active", __func__);
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05302431 break;
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05302432 }
2433 }
2434 return active;
2435}
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302436
2437static bool force_device_switch(struct audio_usecase *usecase)
2438{
2439 bool ret = false;
2440 bool is_it_true_mode = false;
2441
Zhou Song30f2c3e2018-02-08 14:02:15 +08002442 if (usecase->type == PCM_CAPTURE ||
Surendar Karka93cd25a2018-08-28 14:21:37 +05302443 usecase->type == TRANSCODE_LOOPBACK_RX ||
2444 usecase->type == TRANSCODE_LOOPBACK_TX) {
Zhou Song30f2c3e2018-02-08 14:02:15 +08002445 return false;
2446 }
2447
Aalique Grahamecbc46a22017-10-05 10:30:23 -07002448 if(usecase->stream.out == NULL) {
2449 ALOGE("%s: stream.out is NULL", __func__);
2450 return false;
2451 }
2452
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302453 if (is_offload_usecase(usecase->id) &&
Xiaojun Sang869f2012016-02-23 16:33:07 +08002454 (usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002455 (compare_device_type(&usecase->stream.out->device_list, AUDIO_DEVICE_OUT_WIRED_HEADSET) ||
2456 compare_device_type(&usecase->stream.out->device_list, AUDIO_DEVICE_OUT_WIRED_HEADPHONE))) {
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302457 is_it_true_mode = (NATIVE_AUDIO_MODE_TRUE_44_1 == platform_get_native_support()? true : false);
2458 if ((is_it_true_mode && !adev->native_playback_enabled) ||
2459 (!is_it_true_mode && adev->native_playback_enabled)){
2460 ret = true;
2461 ALOGD("napb: time to toggle native mode");
2462 }
2463 }
2464
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302465 // Force all a2dp output devices to reconfigure for proper AFE encode format
Ashish Jainc597d102016-12-12 10:31:34 +05302466 //Also handle a case where in earlier a2dp start failed as A2DP stream was
2467 //in suspended state, hence try to trigger a retry when we again get a routing request.
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002468 if(is_a2dp_out_device_type(&usecase->stream.out->device_list) &&
Ashish Jainc597d102016-12-12 10:31:34 +05302469 audio_extn_a2dp_is_force_device_switch()) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302470 ALOGD("Force a2dp device switch to update new encoder config");
2471 ret = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002472 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302473
Florian Pfister1a84f312018-07-19 14:38:18 +02002474 if (usecase->stream.out->stream_config_changed) {
Manish Dewangan671a4202017-08-18 17:30:46 +05302475 ALOGD("Force stream_config_changed to update iec61937 transmission config");
2476 return true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002477 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302478 return ret;
2479}
2480
Aalique Grahame22e49102018-12-18 14:23:57 -08002481static void stream_app_type_cfg_init(struct stream_app_type_cfg *cfg)
2482{
2483 cfg->gain[0] = cfg->gain[1] = APP_TYPE_GAIN_DEFAULT;
2484}
2485
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302486bool is_btsco_device(snd_device_t out_snd_device, snd_device_t in_snd_device)
2487{
2488 bool ret=false;
2489 if ((out_snd_device == SND_DEVICE_OUT_BT_SCO ||
Zhou Song5657f492019-08-07 11:30:39 +08002490 out_snd_device == SND_DEVICE_OUT_BT_SCO_WB ||
2491 out_snd_device == SND_DEVICE_OUT_BT_SCO_SWB) ||
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302492 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_WB_NREC ||
2493 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_WB ||
Zhou Song5657f492019-08-07 11:30:39 +08002494 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_SWB ||
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302495 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_NREC ||
Mingshu Pang16093502020-04-20 11:21:16 +08002496 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC ||
2497 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_SWB_NREC)
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302498 ret = true;
2499
2500 return ret;
2501}
2502
2503bool is_a2dp_device(snd_device_t out_snd_device)
2504{
2505 bool ret=false;
2506 if (out_snd_device == SND_DEVICE_OUT_BT_A2DP)
2507 ret = true;
2508
2509 return ret;
2510}
2511
2512bool is_bt_soc_on(struct audio_device *adev)
2513{
2514 struct mixer_ctl *ctl;
2515 char *mixer_ctl_name = "BT SOC status";
2516 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
2517 bool bt_soc_status = true;
2518 if (!ctl) {
2519 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2520 __func__, mixer_ctl_name);
2521 /*This is to ensure we dont break targets which dont have the kernel change*/
2522 return true;
2523 }
2524 bt_soc_status = mixer_ctl_get_value(ctl, 0);
2525 ALOGD("BT SOC status: %d",bt_soc_status);
2526 return bt_soc_status;
2527}
2528
Zhou Song331c8e52019-08-26 14:16:12 +08002529static int configure_btsco_sample_rate(snd_device_t snd_device)
2530{
2531 struct mixer_ctl *ctl = NULL;
2532 struct mixer_ctl *ctl_sr_rx = NULL, *ctl_sr_tx = NULL, *ctl_sr = NULL;
2533 char *rate_str = NULL;
2534 bool is_rx_dev = true;
2535
2536 if (is_btsco_device(snd_device, snd_device)) {
2537 ctl_sr_tx = mixer_get_ctl_by_name(adev->mixer, "BT SampleRate TX");
2538 ctl_sr_rx = mixer_get_ctl_by_name(adev->mixer, "BT SampleRate RX");
2539 if (!ctl_sr_tx || !ctl_sr_rx) {
2540 ctl_sr = mixer_get_ctl_by_name(adev->mixer, "BT SampleRate");
2541 if (!ctl_sr)
2542 return -ENOSYS;
2543 }
2544
2545 switch (snd_device) {
2546 case SND_DEVICE_OUT_BT_SCO:
2547 rate_str = "KHZ_8";
2548 break;
2549 case SND_DEVICE_IN_BT_SCO_MIC_NREC:
2550 case SND_DEVICE_IN_BT_SCO_MIC:
2551 rate_str = "KHZ_8";
2552 is_rx_dev = false;
2553 break;
2554 case SND_DEVICE_OUT_BT_SCO_WB:
2555 rate_str = "KHZ_16";
2556 break;
2557 case SND_DEVICE_IN_BT_SCO_MIC_WB_NREC:
2558 case SND_DEVICE_IN_BT_SCO_MIC_WB:
2559 rate_str = "KHZ_16";
2560 is_rx_dev = false;
2561 break;
2562 default:
2563 return 0;
2564 }
2565
2566 ctl = (ctl_sr == NULL) ? (is_rx_dev ? ctl_sr_rx : ctl_sr_tx) : ctl_sr;
2567 if (mixer_ctl_set_enum_by_string(ctl, rate_str) != 0)
2568 return -ENOSYS;
2569 }
2570 return 0;
2571}
2572
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302573int out_standby_l(struct audio_stream *stream);
2574
Eric Laurent637e2d42018-11-15 12:24:31 -08002575struct stream_in *adev_get_active_input(const struct audio_device *adev)
2576{
2577 struct listnode *node;
2578 struct stream_in *last_active_in = NULL;
2579
2580 /* Get last added active input.
2581 * TODO: We may use a priority mechanism to pick highest priority active source */
2582 list_for_each(node, &adev->usecase_list)
2583 {
2584 struct audio_usecase *usecase = node_to_item(node, struct audio_usecase, list);
2585 if (usecase->type == PCM_CAPTURE && usecase->stream.in != NULL)
2586 last_active_in = usecase->stream.in;
2587 }
2588
2589 return last_active_in;
2590}
2591
2592struct stream_in *get_voice_communication_input(const struct audio_device *adev)
2593{
2594 struct listnode *node;
2595
2596 /* First check active inputs with voice communication source and then
2597 * any input if audio mode is in communication */
2598 list_for_each(node, &adev->usecase_list)
2599 {
2600 struct audio_usecase *usecase = node_to_item(node, struct audio_usecase, list);
2601 if (usecase->type == PCM_CAPTURE && usecase->stream.in != NULL &&
2602 usecase->stream.in->source == AUDIO_SOURCE_VOICE_COMMUNICATION)
2603 return usecase->stream.in;
2604 }
2605 if (adev->mode == AUDIO_MODE_IN_COMMUNICATION)
2606 return adev_get_active_input(adev);
2607
2608 return NULL;
2609}
2610
Carter Hsu2e429db2019-05-14 18:50:52 +08002611/*
2612 * Aligned with policy.h
2613 */
2614static inline int source_priority(int inputSource)
2615{
2616 switch (inputSource) {
2617 case AUDIO_SOURCE_VOICE_COMMUNICATION:
2618 return 9;
2619 case AUDIO_SOURCE_CAMCORDER:
2620 return 8;
2621 case AUDIO_SOURCE_VOICE_PERFORMANCE:
2622 return 7;
2623 case AUDIO_SOURCE_UNPROCESSED:
2624 return 6;
2625 case AUDIO_SOURCE_MIC:
2626 return 5;
2627 case AUDIO_SOURCE_ECHO_REFERENCE:
2628 return 4;
2629 case AUDIO_SOURCE_FM_TUNER:
2630 return 3;
2631 case AUDIO_SOURCE_VOICE_RECOGNITION:
2632 return 2;
2633 case AUDIO_SOURCE_HOTWORD:
2634 return 1;
2635 default:
2636 break;
2637 }
2638 return 0;
2639}
2640
2641static struct stream_in *get_priority_input(struct audio_device *adev)
2642{
2643 struct listnode *node;
2644 struct audio_usecase *usecase;
2645 int last_priority = 0, priority;
2646 struct stream_in *priority_in = NULL;
2647 struct stream_in *in;
2648
2649 list_for_each(node, &adev->usecase_list) {
2650 usecase = node_to_item(node, struct audio_usecase, list);
2651 if (usecase->type == PCM_CAPTURE) {
2652 in = usecase->stream.in;
2653 if (!in)
2654 continue;
2655 priority = source_priority(in->source);
2656
2657 if (priority > last_priority) {
2658 last_priority = priority;
2659 priority_in = in;
2660 }
2661 }
2662 }
2663 return priority_in;
2664}
2665
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002666int select_devices(struct audio_device *adev, audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002667{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002668 snd_device_t out_snd_device = SND_DEVICE_NONE;
2669 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002670 struct audio_usecase *usecase = NULL;
2671 struct audio_usecase *vc_usecase = NULL;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002672 struct audio_usecase *voip_usecase = NULL;
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -08002673 struct audio_usecase *hfp_usecase = NULL;
Siddartha Shaik44dd7702017-06-14 12:13:25 +05302674 struct stream_out stream_out;
Vimal Puthanveed41fcff22014-01-23 15:56:53 -08002675 audio_usecase_t hfp_ucid;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002676 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002677
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302678 ALOGD("%s for use case (%s)", __func__, use_case_table[uc_id]);
2679
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002680 usecase = get_usecase_from_list(adev, uc_id);
2681 if (usecase == NULL) {
2682 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
2683 return -EINVAL;
2684 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002685
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002686 if ((usecase->type == VOICE_CALL) ||
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -08002687 (usecase->type == VOIP_CALL) ||
2688 (usecase->type == PCM_HFP_CALL)) {
Aditya Bavanaribdda2f22016-10-19 15:02:05 +05302689 if(usecase->stream.out == NULL) {
2690 ALOGE("%s: stream.out is NULL", __func__);
2691 return -EINVAL;
2692 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002693 if (compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_BUS)) {
Guodong Hu267bdf82019-08-12 19:22:32 +08002694 out_snd_device = audio_extn_auto_hal_get_output_snd_device(adev,
2695 uc_id);
2696 in_snd_device = audio_extn_auto_hal_get_input_snd_device(adev,
2697 uc_id);
2698 } else {
2699 out_snd_device = platform_get_output_snd_device(adev->platform,
Jaideep Sharma477917f2020-03-13 18:13:33 +05302700 usecase->stream.out, usecase->type);
Guodong Hu267bdf82019-08-12 19:22:32 +08002701 in_snd_device = platform_get_input_snd_device(adev->platform,
2702 NULL,
Jaideep Sharma477917f2020-03-13 18:13:33 +05302703 &usecase->stream.out->device_list,
2704 usecase->type);
Guodong Hu267bdf82019-08-12 19:22:32 +08002705 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002706 assign_devices(&usecase->device_list, &usecase->stream.out->device_list);
Surendar Karka93cd25a2018-08-28 14:21:37 +05302707 } else if (usecase->type == TRANSCODE_LOOPBACK_RX) {
Siddartha Shaik31b530e2017-05-19 15:26:33 +05302708 if (usecase->stream.inout == NULL) {
2709 ALOGE("%s: stream.inout is NULL", __func__);
2710 return -EINVAL;
2711 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002712 assign_devices(&stream_out.device_list, &usecase->stream.inout->out_config.device_list);
Siddartha Shaik44dd7702017-06-14 12:13:25 +05302713 stream_out.sample_rate = usecase->stream.inout->out_config.sample_rate;
2714 stream_out.format = usecase->stream.inout->out_config.format;
2715 stream_out.channel_mask = usecase->stream.inout->out_config.channel_mask;
Jaideep Sharma477917f2020-03-13 18:13:33 +05302716 out_snd_device = platform_get_output_snd_device(adev->platform, &stream_out, usecase->type);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002717 assign_devices(&usecase->device_list,
2718 &usecase->stream.inout->out_config.device_list);
Surendar Karka93cd25a2018-08-28 14:21:37 +05302719 } else if (usecase->type == TRANSCODE_LOOPBACK_TX ) {
2720 if (usecase->stream.inout == NULL) {
2721 ALOGE("%s: stream.inout is NULL", __func__);
2722 return -EINVAL;
2723 }
Manisha Agarwal03297972020-04-17 15:36:55 +05302724 struct listnode out_devices;
2725 list_init(&out_devices);
2726 in_snd_device = platform_get_input_snd_device(adev->platform, NULL,
2727 &out_devices, usecase->type);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002728 assign_devices(&usecase->device_list,
2729 &usecase->stream.inout->in_config.device_list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002730 } else {
2731 /*
2732 * If the voice call is active, use the sound devices of voice call usecase
2733 * so that it would not result any device switch. All the usecases will
2734 * be switched to new device when select_devices() is called for voice call
2735 * usecase. This is to avoid switching devices for voice call when
2736 * check_usecases_codec_backend() is called below.
Alexy Joseph79dfa3c2016-04-20 18:44:56 -07002737 * choose voice call device only if the use case device is
2738 * also using the codec backend
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002739 */
Shiv Maliyappanahallibb4cf0b2016-01-21 11:30:06 -08002740 if (voice_is_in_call(adev) && adev->mode != AUDIO_MODE_NORMAL) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002741 vc_usecase = get_usecase_from_list(adev,
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -08002742 get_usecase_id_from_usecase_type(adev, VOICE_CALL));
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002743 if ((vc_usecase) && ((is_codec_backend_out_device_type(&vc_usecase->device_list) &&
2744 is_codec_backend_out_device_type(&usecase->device_list)) ||
2745 (is_codec_backend_out_device_type(&vc_usecase->device_list) &&
2746 is_codec_backend_in_device_type(&usecase->device_list)) ||
2747 is_single_device_type_equal(&vc_usecase->device_list,
2748 AUDIO_DEVICE_OUT_HEARING_AID) ||
2749 is_single_device_type_equal(&usecase->device_list,
Robert Lee8a6aaf32019-09-20 16:40:19 +08002750 AUDIO_DEVICE_IN_VOICE_CALL) ||
2751 (is_single_device_type_equal(&usecase->device_list,
2752 AUDIO_DEVICE_IN_USB_HEADSET) &&
2753 is_single_device_type_equal(&vc_usecase->device_list,
2754 AUDIO_DEVICE_OUT_USB_HEADSET)))) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002755 in_snd_device = vc_usecase->in_snd_device;
2756 out_snd_device = vc_usecase->out_snd_device;
2757 }
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002758 } else if (voice_extn_compress_voip_is_active(adev)) {
yidongh02ef86f2017-04-21 15:36:04 +08002759 bool out_snd_device_backend_match = true;
yidongh47785a82017-05-08 19:29:29 +08002760 voip_usecase = get_usecase_from_list(adev, USECASE_COMPRESS_VOIP_CALL);
yidongh6261d8e2017-05-15 17:04:02 +08002761 if ((voip_usecase != NULL) &&
2762 (usecase->type == PCM_PLAYBACK) &&
2763 (usecase->stream.out != NULL)) {
yidongh02ef86f2017-04-21 15:36:04 +08002764 out_snd_device_backend_match = platform_check_backends_match(
2765 voip_usecase->out_snd_device,
2766 platform_get_output_snd_device(
2767 adev->platform,
Jaideep Sharma477917f2020-03-13 18:13:33 +05302768 usecase->stream.out, usecase->type));
yidongh02ef86f2017-04-21 15:36:04 +08002769 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002770 if ((voip_usecase) && (is_codec_backend_out_device_type(&voip_usecase->device_list) &&
2771 (is_codec_backend_out_device_type(&usecase->device_list) ||
2772 is_codec_backend_in_device_type(&usecase->device_list)) &&
yidongh02ef86f2017-04-21 15:36:04 +08002773 out_snd_device_backend_match &&
Mingming Yin2d8aa2e2014-08-14 00:00:51 -07002774 (voip_usecase->stream.out != adev->primary_output))) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002775 in_snd_device = voip_usecase->in_snd_device;
2776 out_snd_device = voip_usecase->out_snd_device;
2777 }
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -08002778 } else if (audio_extn_hfp_is_active(adev)) {
Vimal Puthanveed41fcff22014-01-23 15:56:53 -08002779 hfp_ucid = audio_extn_hfp_get_usecase();
2780 hfp_usecase = get_usecase_from_list(adev, hfp_ucid);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002781 if ((hfp_usecase) && is_codec_backend_out_device_type(&hfp_usecase->device_list)) {
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -08002782 in_snd_device = hfp_usecase->in_snd_device;
2783 out_snd_device = hfp_usecase->out_snd_device;
2784 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002785 }
2786 if (usecase->type == PCM_PLAYBACK) {
Aditya Bavanaribdda2f22016-10-19 15:02:05 +05302787 if (usecase->stream.out == NULL) {
2788 ALOGE("%s: stream.out is NULL", __func__);
2789 return -EINVAL;
2790 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002791 assign_devices(&usecase->device_list, &usecase->stream.out->device_list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002792 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07002793 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurent637e2d42018-11-15 12:24:31 -08002794 struct stream_out *voip_out = adev->primary_output;
2795 struct stream_in *voip_in = get_voice_communication_input(adev);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002796 if (compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_BUS))
Guodong Hu267bdf82019-08-12 19:22:32 +08002797 out_snd_device = audio_extn_auto_hal_get_output_snd_device(adev, uc_id);
2798 else
2799 out_snd_device = platform_get_output_snd_device(adev->platform,
Jaideep Sharma477917f2020-03-13 18:13:33 +05302800 usecase->stream.out,
2801 usecase->type);
kunleizdcf967a2018-08-07 17:09:11 +08002802 voip_usecase = get_usecase_from_list(adev, USECASE_AUDIO_PLAYBACK_VOIP);
kunleizdcf967a2018-08-07 17:09:11 +08002803
Eric Laurent637e2d42018-11-15 12:24:31 -08002804 if (voip_usecase)
2805 voip_out = voip_usecase->stream.out;
2806
2807 if (usecase->stream.out == voip_out && voip_in != NULL)
2808 select_devices(adev, voip_in->usecase);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07002809 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002810 } else if (usecase->type == PCM_CAPTURE) {
Aditya Bavanaribdda2f22016-10-19 15:02:05 +05302811 if (usecase->stream.in == NULL) {
2812 ALOGE("%s: stream.in is NULL", __func__);
2813 return -EINVAL;
2814 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002815 assign_devices(&usecase->device_list, &usecase->stream.in->device_list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002816 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07002817 if (in_snd_device == SND_DEVICE_NONE) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002818 struct listnode out_devices;
Eric Laurent637e2d42018-11-15 12:24:31 -08002819 struct stream_in *voip_in = get_voice_communication_input(adev);
Carter Hsu2e429db2019-05-14 18:50:52 +08002820 struct stream_in *priority_in = NULL;
Eric Laurent637e2d42018-11-15 12:24:31 -08002821
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002822 list_init(&out_devices);
Eric Laurent637e2d42018-11-15 12:24:31 -08002823 if (voip_in != NULL) {
Eric Laurent637e2d42018-11-15 12:24:31 -08002824 struct audio_usecase *voip_usecase = get_usecase_from_list(adev,
2825 USECASE_AUDIO_PLAYBACK_VOIP);
2826
Carter Hsu2e429db2019-05-14 18:50:52 +08002827 usecase->stream.in->enable_ec_port = false;
2828
Zhou Song62ea0282020-03-22 19:53:01 +08002829 bool is_ha_usecase = adev->ha_proxy_enable ?
2830 usecase->id == USECASE_AUDIO_RECORD_AFE_PROXY2 :
2831 usecase->id == USECASE_AUDIO_RECORD_AFE_PROXY;
2832 if (is_ha_usecase) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002833 reassign_device_list(&out_devices, AUDIO_DEVICE_OUT_TELEPHONY_TX, "");
Eric Laurent637e2d42018-11-15 12:24:31 -08002834 } else if (voip_usecase) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002835 assign_devices(&out_devices, &voip_usecase->stream.out->device_list);
Eric Laurent637e2d42018-11-15 12:24:31 -08002836 } else if (adev->primary_output &&
2837 !adev->primary_output->standby) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002838 assign_devices(&out_devices, &adev->primary_output->device_list);
Eric Laurent637e2d42018-11-15 12:24:31 -08002839 } else {
2840 /* forcing speaker o/p device to get matching i/p pair
2841 in case o/p is not routed from same primary HAL */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002842 reassign_device_list(&out_devices, AUDIO_DEVICE_OUT_SPEAKER, "");
Eric Laurent637e2d42018-11-15 12:24:31 -08002843 }
Carter Hsu2e429db2019-05-14 18:50:52 +08002844 priority_in = voip_in;
2845 } else {
2846 /* get the input with the highest priority source*/
2847 priority_in = get_priority_input(adev);
2848
2849 if (!priority_in)
2850 priority_in = usecase->stream.in;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07002851 }
Carter Hsu2e429db2019-05-14 18:50:52 +08002852
Eric Laurent637e2d42018-11-15 12:24:31 -08002853 in_snd_device = platform_get_input_snd_device(adev->platform,
Carter Hsu2e429db2019-05-14 18:50:52 +08002854 priority_in,
Jaideep Sharma477917f2020-03-13 18:13:33 +05302855 &out_devices,
2856 usecase->type);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07002857 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002858 }
2859 }
2860
2861 if (out_snd_device == usecase->out_snd_device &&
2862 in_snd_device == usecase->in_snd_device) {
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302863
2864 if (!force_device_switch(usecase))
2865 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002866 }
2867
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002868 if (!compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_BUS) &&
Guodong Hu267bdf82019-08-12 19:22:32 +08002869 ((is_btsco_device(out_snd_device,in_snd_device) && !adev->bt_sco_on) ||
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002870 (is_a2dp_device(out_snd_device) && !audio_extn_a2dp_source_is_ready()))) {
Guodong Hu267bdf82019-08-12 19:22:32 +08002871 ALOGD("SCO/A2DP is selected but they are not connected/ready hence dont route");
2872 return 0;
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302873 }
2874
Aalique Grahame22e49102018-12-18 14:23:57 -08002875 if (out_snd_device != SND_DEVICE_NONE &&
2876 out_snd_device != adev->last_logged_snd_device[uc_id][0]) {
2877 ALOGD("%s: changing use case %s output device from(%d: %s, acdb %d) to (%d: %s, acdb %d)",
2878 __func__,
2879 use_case_table[uc_id],
2880 adev->last_logged_snd_device[uc_id][0],
2881 platform_get_snd_device_name(adev->last_logged_snd_device[uc_id][0]),
2882 adev->last_logged_snd_device[uc_id][0] != SND_DEVICE_NONE ?
2883 platform_get_snd_device_acdb_id(adev->last_logged_snd_device[uc_id][0]) :
2884 -1,
2885 out_snd_device,
2886 platform_get_snd_device_name(out_snd_device),
2887 platform_get_snd_device_acdb_id(out_snd_device));
2888 adev->last_logged_snd_device[uc_id][0] = out_snd_device;
2889 }
2890 if (in_snd_device != SND_DEVICE_NONE &&
2891 in_snd_device != adev->last_logged_snd_device[uc_id][1]) {
2892 ALOGD("%s: changing use case %s input device from(%d: %s, acdb %d) to (%d: %s, acdb %d)",
2893 __func__,
2894 use_case_table[uc_id],
2895 adev->last_logged_snd_device[uc_id][1],
2896 platform_get_snd_device_name(adev->last_logged_snd_device[uc_id][1]),
2897 adev->last_logged_snd_device[uc_id][1] != SND_DEVICE_NONE ?
2898 platform_get_snd_device_acdb_id(adev->last_logged_snd_device[uc_id][1]) :
2899 -1,
2900 in_snd_device,
2901 platform_get_snd_device_name(in_snd_device),
2902 platform_get_snd_device_acdb_id(in_snd_device));
2903 adev->last_logged_snd_device[uc_id][1] = in_snd_device;
2904 }
2905
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002906
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002907 /*
2908 * Limitation: While in call, to do a device switch we need to disable
2909 * and enable both RX and TX devices though one of them is same as current
2910 * device.
2911 */
Vidyakumar Athota21b3bb92014-04-25 11:08:08 -07002912 if ((usecase->type == VOICE_CALL) &&
2913 (usecase->in_snd_device != SND_DEVICE_NONE) &&
2914 (usecase->out_snd_device != SND_DEVICE_NONE)) {
Eric Laurentb23d5282013-05-14 15:27:20 -07002915 status = platform_switch_voice_call_device_pre(adev->platform);
Narsinga Rao Chella116142b2015-08-14 18:00:08 -07002916 }
2917
2918 if (((usecase->type == VOICE_CALL) ||
2919 (usecase->type == VOIP_CALL)) &&
2920 (usecase->out_snd_device != SND_DEVICE_NONE)) {
2921 /* Disable sidetone only if voice/voip call already exists */
Jaideep Sharma477917f2020-03-13 18:13:33 +05302922 if (voice_is_call_state_active_in_call(adev) ||
Narsinga Rao Chella116142b2015-08-14 18:00:08 -07002923 voice_extn_compress_voip_is_started(adev))
Bhalchandra Gajare45fee282015-06-09 22:23:45 -07002924 voice_set_sidetone(adev, usecase->out_snd_device, false);
Vidyakumar Athotaea269c62016-10-31 09:05:59 -07002925
2926 /* Disable aanc only if voice call exists */
Jaideep Sharma477917f2020-03-13 18:13:33 +05302927 if (voice_is_call_state_active_in_call(adev))
Vidyakumar Athotaea269c62016-10-31 09:05:59 -07002928 voice_check_and_update_aanc_path(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002929 }
2930
Aalique Grahame22e49102018-12-18 14:23:57 -08002931 if ((out_snd_device == SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP ||
2932 out_snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP) &&
Florian Pfister1a84f312018-07-19 14:38:18 +02002933 (!audio_extn_a2dp_source_is_ready())) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302934 ALOGW("%s: A2DP profile is not ready, routing to speaker only", __func__);
Aalique Grahame22e49102018-12-18 14:23:57 -08002935 if (out_snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP)
2936 out_snd_device = SND_DEVICE_OUT_SPEAKER_SAFE;
2937 else
2938 out_snd_device = SND_DEVICE_OUT_SPEAKER;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302939 }
2940
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002941 /* Disable current sound devices */
2942 if (usecase->out_snd_device != SND_DEVICE_NONE) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07002943 disable_audio_route(adev, usecase);
2944 disable_snd_device(adev, usecase->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002945 }
2946
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002947 if (usecase->in_snd_device != SND_DEVICE_NONE) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07002948 disable_audio_route(adev, usecase);
2949 disable_snd_device(adev, usecase->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002950 }
2951
Daniel Hillenbrand21752f02013-05-23 10:10:00 +05302952 /* Rely on amplifier_set_devices to distinguish between in/out devices */
2953 amplifier_set_input_devices(in_snd_device);
2954 amplifier_set_output_devices(out_snd_device);
2955
Vidyakumar Athota545dbd32013-11-13 17:30:53 -08002956 /* Applicable only on the targets that has external modem.
2957 * New device information should be sent to modem before enabling
2958 * the devices to reduce in-call device switch time.
2959 */
Vidyakumar Athota21b3bb92014-04-25 11:08:08 -07002960 if ((usecase->type == VOICE_CALL) &&
2961 (usecase->in_snd_device != SND_DEVICE_NONE) &&
2962 (usecase->out_snd_device != SND_DEVICE_NONE)) {
Vidyakumar Athota545dbd32013-11-13 17:30:53 -08002963 status = platform_switch_voice_call_enable_device_config(adev->platform,
2964 out_snd_device,
2965 in_snd_device);
Vidyakumar Athota21b3bb92014-04-25 11:08:08 -07002966 }
Vidyakumar Athota545dbd32013-11-13 17:30:53 -08002967
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002968 /* Enable new sound devices */
2969 if (out_snd_device != SND_DEVICE_NONE) {
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08002970 check_usecases_codec_backend(adev, usecase, out_snd_device);
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +05302971 if (platform_check_codec_asrc_support(adev->platform))
2972 check_and_set_asrc_mode(adev, usecase, out_snd_device);
Haynes Mathew George1376ca62014-04-24 11:55:48 -07002973 enable_snd_device(adev, out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002974 }
2975
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002976 if (in_snd_device != SND_DEVICE_NONE) {
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05302977 check_usecases_capture_codec_backend(adev, usecase, in_snd_device);
Haynes Mathew George1376ca62014-04-24 11:55:48 -07002978 enable_snd_device(adev, in_snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002979 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002980
Avinash Vaish71a8b972014-07-24 15:36:33 +05302981 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) {
Eric Laurentb23d5282013-05-14 15:27:20 -07002982 status = platform_switch_voice_call_device_post(adev->platform,
2983 out_snd_device,
2984 in_snd_device);
Avinash Vaish71a8b972014-07-24 15:36:33 +05302985 enable_audio_route_for_voice_usecases(adev, usecase);
2986 }
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002987
sangwoo170731f2013-06-08 15:36:36 +09002988 usecase->in_snd_device = in_snd_device;
2989 usecase->out_snd_device = out_snd_device;
2990
Dhananjay Kumard6d32152016-10-13 16:11:03 +05302991 audio_extn_utils_update_stream_app_type_cfg_for_usecase(adev,
2992 usecase);
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +05302993 if (usecase->type == PCM_PLAYBACK) {
Weiyin Jiang6f4c8062016-11-23 15:30:29 +08002994 if ((24 == usecase->stream.out->bit_width) &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002995 compare_device_type(&usecase->stream.out->device_list, AUDIO_DEVICE_OUT_SPEAKER)) {
Weiyin Jiang6f4c8062016-11-23 15:30:29 +08002996 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
2997 } else if ((out_snd_device == SND_DEVICE_OUT_HDMI ||
2998 out_snd_device == SND_DEVICE_OUT_USB_HEADSET ||
2999 out_snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
3000 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
3001 /*
3002 * To best utlize DSP, check if the stream sample rate is supported/multiple of
3003 * configured device sample rate, if not update the COPP rate to be equal to the
3004 * device sample rate, else open COPP at stream sample rate
3005 */
3006 platform_check_and_update_copp_sample_rate(adev->platform, out_snd_device,
3007 usecase->stream.out->sample_rate,
3008 &usecase->stream.out->app_type_cfg.sample_rate);
Ashish Jain4826f6c2017-02-06 13:33:20 +05303009 } else if (((out_snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
Preetam Singh Ranawat590d0432019-09-30 14:39:47 +05303010 out_snd_device != SND_DEVICE_OUT_HEADPHONES &&
3011 out_snd_device != SND_DEVICE_OUT_HEADPHONES_HIFI_FILTER &&
Ashish Jain4826f6c2017-02-06 13:33:20 +05303012 !audio_is_true_native_stream_active(adev)) &&
Weiyin Jiang6f4c8062016-11-23 15:30:29 +08003013 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
3014 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
3015 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
3016 }
Weiyin Jiangcdece202019-07-08 16:13:16 +08003017 }
3018 enable_audio_route(adev, usecase);
Weiyin Jiang6f4c8062016-11-23 15:30:29 +08003019
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003020 audio_extn_qdsp_set_device(usecase);
Aalique Grahame22e49102018-12-18 14:23:57 -08003021
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07003022 /* If input stream is already running then effect needs to be
3023 applied on the new input device that's being enabled here. */
Eric Laurent637e2d42018-11-15 12:24:31 -08003024 if (in_snd_device != SND_DEVICE_NONE)
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07003025 check_and_enable_effect(adev);
3026
Vidyakumar Athota493f2892016-08-14 11:56:55 -07003027 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) {
Vidyakumar Athotaea269c62016-10-31 09:05:59 -07003028 /* Enable aanc only if voice call exists */
Jaideep Sharma477917f2020-03-13 18:13:33 +05303029 if (voice_is_call_state_active_in_call(adev))
Vidyakumar Athotaea269c62016-10-31 09:05:59 -07003030 voice_check_and_update_aanc_path(adev, out_snd_device, true);
3031
Vidyakumar Athota493f2892016-08-14 11:56:55 -07003032 /* Enable sidetone only if other voice/voip call already exists */
Jaideep Sharma477917f2020-03-13 18:13:33 +05303033 if (voice_is_call_state_active_in_call(adev) ||
Vidyakumar Athota493f2892016-08-14 11:56:55 -07003034 voice_extn_compress_voip_is_started(adev))
3035 voice_set_sidetone(adev, out_snd_device, true);
3036 }
3037
Vidyakumar Athota1fd21792013-11-15 14:50:57 -08003038 /* Applicable only on the targets that has external modem.
3039 * Enable device command should be sent to modem only after
3040 * enabling voice call mixer controls
3041 */
Vidyakumar Athota339342f2014-07-01 15:30:57 -07003042 if (usecase->type == VOICE_CALL)
Vidyakumar Athota1fd21792013-11-15 14:50:57 -08003043 status = platform_switch_voice_call_usecase_route_post(adev->platform,
3044 out_snd_device,
3045 in_snd_device);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303046
3047 if (is_btsco_device(out_snd_device, in_snd_device) || is_a2dp_device(out_snd_device)) {
Eric Laurent637e2d42018-11-15 12:24:31 -08003048 struct stream_in *in = adev_get_active_input(adev);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303049 if (usecase->type == VOIP_CALL) {
Eric Laurent637e2d42018-11-15 12:24:31 -08003050 if (in != NULL && !in->standby) {
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303051 if (is_bt_soc_on(adev) == false){
3052 ALOGD("BT SCO MIC disconnected while in connection");
Eric Laurent637e2d42018-11-15 12:24:31 -08003053 if (in->pcm != NULL)
3054 pcm_stop(in->pcm);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303055 }
3056 }
3057 if ((usecase->stream.out != NULL) && (usecase->stream.out != adev->primary_output)
3058 && usecase->stream.out->started) {
3059 if (is_bt_soc_on(adev) == false) {
3060 ALOGD("BT SCO/A2DP disconnected while in connection");
3061 out_standby_l(&usecase->stream.out->stream.common);
3062 }
3063 }
3064 } else if ((usecase->stream.out != NULL) &&
3065 !(usecase->stream.out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
Surendar Karkae1dc8742018-11-19 16:23:14 +05303066 (usecase->type != TRANSCODE_LOOPBACK_TX) &&
3067 (usecase->type != TRANSCODE_LOOPBACK_RX) &&
Weiyin Jiang0d373242019-07-25 13:18:17 +08003068 (usecase->type != PCM_CAPTURE) &&
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303069 usecase->stream.out->started) {
3070 if (is_bt_soc_on(adev) == false) {
3071 ALOGD("BT SCO/A2dp disconnected while in connection");
3072 out_standby_l(&usecase->stream.out->stream.common);
3073 }
3074 }
3075 }
3076
Yung Ti Su70cb8242018-06-22 17:38:47 +08003077 if (usecase->type != PCM_CAPTURE && usecase == voip_usecase) {
Aalique Grahame22e49102018-12-18 14:23:57 -08003078 struct stream_out *voip_out = voip_usecase->stream.out;
3079 audio_extn_utils_send_app_type_gain(adev,
3080 voip_out->app_type_cfg.app_type,
3081 &voip_out->app_type_cfg.gain[0]);
3082 }
3083
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05303084 ALOGD("%s: done",__func__);
3085
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003086 return status;
3087}
3088
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003089static int stop_input_stream(struct stream_in *in)
3090{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05303091 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003092 struct audio_usecase *uc_info;
Pallavid7c7a272018-01-16 11:22:55 +05303093
3094 if (in == NULL) {
3095 ALOGE("%s: stream_in ptr is NULL", __func__);
3096 return -EINVAL;
3097 }
3098
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003099 struct audio_device *adev = in->dev;
Carter Hsu2e429db2019-05-14 18:50:52 +08003100 struct stream_in *priority_in = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003101
Eric Laurent994a6932013-07-17 11:51:42 -07003102 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003103 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003104 uc_info = get_usecase_from_list(adev, in->usecase);
3105 if (uc_info == NULL) {
3106 ALOGE("%s: Could not find the usecase (%d) in the list",
3107 __func__, in->usecase);
3108 return -EINVAL;
3109 }
3110
Carter Hsu2e429db2019-05-14 18:50:52 +08003111 priority_in = get_priority_input(adev);
3112
Derek Chenea197282019-01-07 17:35:01 -08003113 if (audio_extn_ext_hw_plugin_usecase_stop(adev->ext_hw_plugin, uc_info))
3114 ALOGE("%s: failed to stop ext hw plugin", __func__);
Derek Chend2530072014-11-24 12:39:14 -08003115
Vidyakumar Athota2850d532013-11-19 16:02:12 -08003116 /* Close in-call recording streams */
3117 voice_check_and_stop_incall_rec_usecase(adev, in);
3118
Eric Laurent150dbfe2013-02-27 14:31:02 -08003119 /* 1. Disable stream specific mixer controls */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07003120 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003121
3122 /* 2. Disable the tx device */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07003123 disable_snd_device(adev, uc_info->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003124
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003125 if (is_loopback_input_device(get_device_types(&in->device_list)))
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05303126 audio_extn_keep_alive_stop(KEEP_ALIVE_OUT_PRIMARY);
3127
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08003128 list_remove(&uc_info->list);
3129 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003130
Carter Hsu2e429db2019-05-14 18:50:52 +08003131 if (priority_in == in) {
3132 priority_in = get_priority_input(adev);
3133 if (priority_in)
3134 select_devices(adev, priority_in->usecase);
3135 }
3136
Vatsal Buchac09ae062018-11-14 13:25:08 +05303137 enable_gcov();
Eric Laurent994a6932013-07-17 11:51:42 -07003138 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003139 return ret;
3140}
3141
3142int start_input_stream(struct stream_in *in)
3143{
3144 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -08003145 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003146 struct audio_usecase *uc_info;
Preetam Singh Ranawata87e9742018-02-13 16:52:53 +05303147
3148 if (in == NULL) {
3149 ALOGE("%s: stream_in ptr is NULL", __func__);
3150 return -EINVAL;
3151 }
3152
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003153 struct audio_device *adev = in->dev;
Garmond Leunge2433c32017-09-28 21:51:22 -07003154 struct pcm_config config = in->config;
Garmond Leung438932f2017-10-04 19:35:18 -07003155 int usecase = platform_update_usecase_from_source(in->source,in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003156
Mingming Yin2664a5b2015-09-03 10:53:11 -07003157 if (get_usecase_from_list(adev, usecase) == NULL)
3158 in->usecase = usecase;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05303159 ALOGD("%s: enter: stream(%p)usecase(%d: %s)",
3160 __func__, &in->stream, in->usecase, use_case_table[in->usecase]);
Shiv Maliyappanahallida107642013-10-17 11:16:13 -07003161
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05303162 if (CARD_STATUS_OFFLINE == in->card_status||
3163 CARD_STATUS_OFFLINE == adev->card_status) {
3164 ALOGW("in->card_status or adev->card_status offline, try again");
Dhanalakshmi Siddani4d57e992014-07-17 16:37:51 +05303165 ret = -EIO;
Naresh Tanniru4c630392014-05-12 01:05:52 +05303166 goto error_config;
3167 }
Naresh Tanniru4c630392014-05-12 01:05:52 +05303168
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003169 if (is_sco_in_device_type(&in->device_list)) {
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303170 if (!adev->bt_sco_on) {
3171 ALOGE("%s: SCO profile is not ready, return error", __func__);
3172 ret = -EIO;
3173 goto error_config;
3174 }
3175 }
3176
Shiv Maliyappanahallida107642013-10-17 11:16:13 -07003177 /* Check if source matches incall recording usecase criteria */
3178 ret = voice_check_and_set_incall_rec_usecase(adev, in);
3179 if (ret)
3180 goto error_config;
3181 else
Mingming Yin2664a5b2015-09-03 10:53:11 -07003182 ALOGV("%s: usecase(%d)", __func__, in->usecase);
3183
Dhananjay Kumar7dbe3562019-08-30 05:49:33 +05303184 if (audio_extn_cin_attached_usecase(in))
3185 audio_extn_cin_acquire_usecase(in);
3186
Mingming Yin2664a5b2015-09-03 10:53:11 -07003187 if (get_usecase_from_list(adev, in->usecase) != NULL) {
3188 ALOGE("%s: use case assigned already in use, stream(%p)usecase(%d: %s)",
3189 __func__, &in->stream, in->usecase, use_case_table[in->usecase]);
Weiyin Jiang04949102020-09-10 16:10:51 +08003190 ret = -EINVAL;
3191 goto error_config;
Mingming Yin2664a5b2015-09-03 10:53:11 -07003192 }
Shiv Maliyappanahallida107642013-10-17 11:16:13 -07003193
Eric Laurentb23d5282013-05-14 15:27:20 -07003194 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003195 if (in->pcm_device_id < 0) {
3196 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
3197 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -08003198 ret = -EINVAL;
3199 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003200 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003201
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003202 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07003203
3204 if (!uc_info) {
3205 ret = -ENOMEM;
3206 goto error_config;
3207 }
3208
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003209 uc_info->id = in->usecase;
3210 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08003211 uc_info->stream.in = in;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003212 list_init(&uc_info->device_list);
3213 assign_devices(&uc_info->device_list, &in->device_list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003214 uc_info->in_snd_device = SND_DEVICE_NONE;
3215 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003216
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08003217 list_add_tail(&adev->usecase_list, &uc_info->list);
Wei Wangf7ca6c92017-11-21 14:51:20 -08003218 audio_streaming_hint_start();
Sudheer Papothifa9d2282015-09-17 01:53:25 +05303219 audio_extn_perf_lock_acquire(&adev->perf_lock_handle, 0,
3220 adev->perf_lock_opts,
3221 adev->perf_lock_opts_size);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003222 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003223
Derek Chenea197282019-01-07 17:35:01 -08003224 if (audio_extn_ext_hw_plugin_usecase_start(adev->ext_hw_plugin, uc_info))
3225 ALOGE("%s: failed to start ext hw plugin", __func__);
Derek Chend2530072014-11-24 12:39:14 -08003226
Deeraj Soman30cc7ae2019-03-18 16:26:55 +05303227 android_atomic_acquire_cas(true, false, &(in->capture_stopped));
3228
Dhananjay Kumar7dbe3562019-08-30 05:49:33 +05303229 if (audio_extn_cin_attached_usecase(in)) {
Manish Dewangan46e07982018-12-13 18:18:59 +05303230 ret = audio_extn_cin_open_input_stream(in);
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05303231 if (ret)
3232 goto error_open;
3233 else
3234 goto done_open;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003235 }
3236
Haynes Mathew George16081042017-05-31 17:16:49 -07003237 if (in->usecase == USECASE_AUDIO_RECORD_MMAP) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003238 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) {
Haynes Mathew George16081042017-05-31 17:16:49 -07003239 ALOGE("%s: pcm stream not ready", __func__);
3240 goto error_open;
3241 }
3242 ret = pcm_start(in->pcm);
3243 if (ret < 0) {
3244 ALOGE("%s: MMAP pcm_start failed ret %d", __func__, ret);
3245 goto error_open;
3246 }
3247 } else {
3248 unsigned int flags = PCM_IN | PCM_MONOTONIC;
3249 unsigned int pcm_open_retry_count = 0;
3250
Zhou Song62ea0282020-03-22 19:53:01 +08003251 if ((in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY) ||
3252 (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY2)) {
Haynes Mathew George16081042017-05-31 17:16:49 -07003253 flags |= PCM_MMAP | PCM_NOIRQ;
3254 pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT;
3255 } else if (in->realtime) {
3256 flags |= PCM_MMAP | PCM_NOIRQ;
3257 }
3258
Garmond Leunge2433c32017-09-28 21:51:22 -07003259 if (audio_extn_ffv_get_stream() == in) {
3260 ALOGD("%s: ffv stream, update pcm config", __func__);
3261 audio_extn_ffv_update_pcm_config(&config);
3262 }
Haynes Mathew George16081042017-05-31 17:16:49 -07003263 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
3264 __func__, adev->snd_card, in->pcm_device_id, in->config.channels);
3265
3266 while (1) {
Haynes Mathew George380745d2017-10-04 15:27:45 -07003267 ATRACE_BEGIN("pcm_in_open");
Haynes Mathew George16081042017-05-31 17:16:49 -07003268 in->pcm = pcm_open(adev->snd_card, in->pcm_device_id,
Garmond Leung438932f2017-10-04 19:35:18 -07003269 flags, &config);
Haynes Mathew George380745d2017-10-04 15:27:45 -07003270 ATRACE_END();
Satish Babu Patakokila54ce83d2018-07-06 18:00:37 +05303271 if (errno == ENETRESET && !pcm_is_ready(in->pcm)) {
Sharad Sanglec6f32552018-05-04 16:15:38 +05303272 ALOGE("%s: pcm_open failed errno:%d\n", __func__, errno);
3273 adev->card_status = CARD_STATUS_OFFLINE;
3274 in->card_status = CARD_STATUS_OFFLINE;
3275 ret = -EIO;
3276 goto error_open;
3277 }
3278
Haynes Mathew George16081042017-05-31 17:16:49 -07003279 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) {
3280 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
3281 if (in->pcm != NULL) {
3282 pcm_close(in->pcm);
3283 in->pcm = NULL;
3284 }
Weiyin Jiang72197252019-10-09 11:49:32 +08003285 if (pcm_open_retry_count == 0) {
Haynes Mathew George16081042017-05-31 17:16:49 -07003286 ret = -EIO;
3287 goto error_open;
3288 }
Weiyin Jiang72197252019-10-09 11:49:32 +08003289 pcm_open_retry_count--;
Haynes Mathew George16081042017-05-31 17:16:49 -07003290 usleep(PROXY_OPEN_WAIT_TIME * 1000);
3291 continue;
3292 }
3293 break;
3294 }
3295
3296 ALOGV("%s: pcm_prepare", __func__);
Haynes Mathew George380745d2017-10-04 15:27:45 -07003297 ATRACE_BEGIN("pcm_in_prepare");
Haynes Mathew George16081042017-05-31 17:16:49 -07003298 ret = pcm_prepare(in->pcm);
Haynes Mathew George380745d2017-10-04 15:27:45 -07003299 ATRACE_END();
Haynes Mathew George16081042017-05-31 17:16:49 -07003300 if (ret < 0) {
3301 ALOGE("%s: pcm_prepare returned %d", __func__, ret);
3302 pcm_close(in->pcm);
3303 in->pcm = NULL;
3304 goto error_open;
3305 }
3306 register_in_stream(in);
3307 if (in->realtime) {
Haynes Mathew George380745d2017-10-04 15:27:45 -07003308 ATRACE_BEGIN("pcm_in_start");
Haynes Mathew George16081042017-05-31 17:16:49 -07003309 ret = pcm_start(in->pcm);
Haynes Mathew George380745d2017-10-04 15:27:45 -07003310 ATRACE_END();
Haynes Mathew George16081042017-05-31 17:16:49 -07003311 if (ret < 0) {
3312 ALOGE("%s: RT pcm_start failed ret %d", __func__, ret);
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003313 pcm_close(in->pcm);
3314 in->pcm = NULL;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003315 goto error_open;
3316 }
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003317 }
Haynes Mathew George5beddd42016-06-27 18:33:40 -07003318 }
3319
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07003320 check_and_enable_effect(adev);
justinweng20fb6d82019-02-21 18:49:00 -07003321 audio_extn_audiozoom_set_microphone_direction(in, in->zoom);
3322 audio_extn_audiozoom_set_microphone_field_dimension(in, in->direction);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07003323
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003324 if (is_loopback_input_device(get_device_types(&in->device_list)))
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05303325 audio_extn_keep_alive_start(KEEP_ALIVE_OUT_PRIMARY);
3326
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05303327done_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 Alamandac7d9bef2015-09-30 22:27:26 -07003330 ALOGD("%s: exit", __func__);
Vatsal Buchac09ae062018-11-14 13:25:08 +05303331 enable_gcov();
Eric Laurentc8400632013-02-14 19:04:54 -08003332 return ret;
3333
3334error_open:
Wei Wangf7ca6c92017-11-21 14:51:20 -08003335 audio_streaming_hint_end();
Sudheer Papothifa9d2282015-09-17 01:53:25 +05303336 audio_extn_perf_lock_release(&adev->perf_lock_handle);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003337 stop_input_stream(in);
Wei Wangf7ca6c92017-11-21 14:51:20 -08003338
Eric Laurentc8400632013-02-14 19:04:54 -08003339error_config:
Weiyin Jiang04949102020-09-10 16:10:51 +08003340 if (audio_extn_cin_attached_usecase(in))
3341 audio_extn_cin_close_input_stream(in);
Laxminath Kasam2cb4b752015-09-24 03:59:15 +05303342 /*
3343 * sleep 50ms to allow sufficient time for kernel
3344 * drivers to recover incases like SSR.
3345 */
3346 usleep(50000);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003347 ALOGD("%s: exit: status(%d)", __func__, ret);
Vatsal Buchac09ae062018-11-14 13:25:08 +05303348 enable_gcov();
Eric Laurentc8400632013-02-14 19:04:54 -08003349 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003350}
3351
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07003352void lock_input_stream(struct stream_in *in)
3353{
3354 pthread_mutex_lock(&in->pre_lock);
3355 pthread_mutex_lock(&in->lock);
3356 pthread_mutex_unlock(&in->pre_lock);
3357}
3358
3359void lock_output_stream(struct stream_out *out)
3360{
3361 pthread_mutex_lock(&out->pre_lock);
3362 pthread_mutex_lock(&out->lock);
3363 pthread_mutex_unlock(&out->pre_lock);
3364}
3365
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003366/* must be called with out->lock locked */
3367static int send_offload_cmd_l(struct stream_out* out, int command)
3368{
3369 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
3370
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07003371 if (!cmd) {
3372 ALOGE("failed to allocate mem for command 0x%x", command);
3373 return -ENOMEM;
3374 }
3375
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003376 ALOGVV("%s %d", __func__, command);
3377
3378 cmd->cmd = command;
3379 list_add_tail(&out->offload_cmd_list, &cmd->node);
3380 pthread_cond_signal(&out->offload_cond);
3381 return 0;
3382}
3383
Weiyin Jiangd5974e62020-09-08 20:28:22 +08003384/* must be called with out->lock and latch lock */
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003385static void stop_compressed_output_l(struct stream_out *out)
3386{
3387 out->offload_state = OFFLOAD_STATE_IDLE;
3388 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07003389 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003390 if (out->compr != NULL) {
3391 compress_stop(out->compr);
3392 while (out->offload_thread_blocked) {
3393 pthread_cond_wait(&out->cond, &out->lock);
3394 }
3395 }
3396}
3397
Varun Balaraje49253e2017-07-06 19:48:56 +05303398bool is_interactive_usecase(audio_usecase_t uc_id)
3399{
3400 unsigned int i;
3401 for (i = 0; i < sizeof(interactive_usecases)/sizeof(interactive_usecases[0]); i++) {
3402 if (uc_id == interactive_usecases[i])
3403 return true;
3404 }
3405 return false;
3406}
3407
3408static audio_usecase_t get_interactive_usecase(struct audio_device *adev)
3409{
3410 audio_usecase_t ret_uc = USECASE_INVALID;
3411 unsigned int intract_uc_index;
3412 unsigned int num_usecase = sizeof(interactive_usecases)/sizeof(interactive_usecases[0]);
3413
3414 ALOGV("%s: num_usecase: %d", __func__, num_usecase);
3415 for (intract_uc_index = 0; intract_uc_index < num_usecase; intract_uc_index++) {
3416 if (!(adev->interactive_usecase_state & (0x1 << intract_uc_index))) {
3417 adev->interactive_usecase_state |= 0x1 << intract_uc_index;
3418 ret_uc = interactive_usecases[intract_uc_index];
3419 break;
3420 }
3421 }
3422
3423 ALOGV("%s: Interactive usecase is %d", __func__, ret_uc);
3424 return ret_uc;
3425}
3426
3427static void free_interactive_usecase(struct audio_device *adev,
3428 audio_usecase_t uc_id)
3429{
3430 unsigned int interact_uc_index;
3431 unsigned int num_usecase = sizeof(interactive_usecases)/sizeof(interactive_usecases[0]);
3432
3433 for (interact_uc_index = 0; interact_uc_index < num_usecase; interact_uc_index++) {
3434 if (interactive_usecases[interact_uc_index] == uc_id) {
3435 adev->interactive_usecase_state &= ~(0x1 << interact_uc_index);
3436 break;
3437 }
3438 }
3439 ALOGV("%s: free Interactive usecase %d", __func__, uc_id);
3440}
3441
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003442bool is_offload_usecase(audio_usecase_t uc_id)
3443{
3444 unsigned int i;
3445 for (i = 0; i < sizeof(offload_usecases)/sizeof(offload_usecases[0]); i++) {
3446 if (uc_id == offload_usecases[i])
3447 return true;
3448 }
3449 return false;
3450}
3451
Dhananjay Kumarac341582017-02-23 23:42:25 +05303452static audio_usecase_t get_offload_usecase(struct audio_device *adev, bool is_compress)
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003453{
vivek mehta446c3962015-09-14 10:57:35 -07003454 audio_usecase_t ret_uc = USECASE_INVALID;
3455 unsigned int offload_uc_index;
Alexy Josephb1379942016-01-29 15:49:38 -08003456 unsigned int num_usecase = sizeof(offload_usecases)/sizeof(offload_usecases[0]);
vivek mehta446c3962015-09-14 10:57:35 -07003457 if (!adev->multi_offload_enable) {
Dhananjay Kumarac341582017-02-23 23:42:25 +05303458 if (!is_compress)
vivek mehta446c3962015-09-14 10:57:35 -07003459 ret_uc = USECASE_AUDIO_PLAYBACK_OFFLOAD2;
3460 else
3461 ret_uc = USECASE_AUDIO_PLAYBACK_OFFLOAD;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003462
vivek mehta446c3962015-09-14 10:57:35 -07003463 pthread_mutex_lock(&adev->lock);
3464 if (get_usecase_from_list(adev, ret_uc) != NULL)
3465 ret_uc = USECASE_INVALID;
3466 pthread_mutex_unlock(&adev->lock);
3467
3468 return ret_uc;
3469 }
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003470
3471 ALOGV("%s: num_usecase: %d", __func__, num_usecase);
vivek mehta446c3962015-09-14 10:57:35 -07003472 for (offload_uc_index = 0; offload_uc_index < num_usecase; offload_uc_index++) {
3473 if (!(adev->offload_usecases_state & (0x1 << offload_uc_index))) {
3474 adev->offload_usecases_state |= 0x1 << offload_uc_index;
3475 ret_uc = offload_usecases[offload_uc_index];
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003476 break;
3477 }
3478 }
vivek mehta446c3962015-09-14 10:57:35 -07003479
3480 ALOGV("%s: offload usecase is %d", __func__, ret_uc);
3481 return ret_uc;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003482}
3483
3484static void free_offload_usecase(struct audio_device *adev,
3485 audio_usecase_t uc_id)
3486{
vivek mehta446c3962015-09-14 10:57:35 -07003487 unsigned int offload_uc_index;
Alexy Josephb1379942016-01-29 15:49:38 -08003488 unsigned int num_usecase = sizeof(offload_usecases)/sizeof(offload_usecases[0]);
vivek mehta446c3962015-09-14 10:57:35 -07003489
3490 if (!adev->multi_offload_enable)
3491 return;
3492
3493 for (offload_uc_index = 0; offload_uc_index < num_usecase; offload_uc_index++) {
3494 if (offload_usecases[offload_uc_index] == uc_id) {
3495 adev->offload_usecases_state &= ~(0x1 << offload_uc_index);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003496 break;
3497 }
3498 }
3499 ALOGV("%s: free offload usecase %d", __func__, uc_id);
3500}
3501
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003502static void *offload_thread_loop(void *context)
3503{
3504 struct stream_out *out = (struct stream_out *) context;
3505 struct listnode *item;
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08003506 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003507
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003508 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
Weiyin Jiangd9b5a4e2019-07-18 17:24:21 +08003509 set_sched_policy(0, SP_FOREGROUND);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003510 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
3511
3512 ALOGV("%s", __func__);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07003513 lock_output_stream(out);
juyuchen391b5fa2018-12-12 17:58:09 +08003514 out->offload_state = OFFLOAD_STATE_IDLE;
3515 out->playback_started = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003516 for (;;) {
3517 struct offload_cmd *cmd = NULL;
3518 stream_callback_event_t event;
3519 bool send_callback = false;
3520
3521 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
3522 __func__, list_empty(&out->offload_cmd_list),
3523 out->offload_state);
3524 if (list_empty(&out->offload_cmd_list)) {
3525 ALOGV("%s SLEEPING", __func__);
3526 pthread_cond_wait(&out->offload_cond, &out->lock);
3527 ALOGV("%s RUNNING", __func__);
3528 continue;
3529 }
3530
3531 item = list_head(&out->offload_cmd_list);
3532 cmd = node_to_item(item, struct offload_cmd, node);
3533 list_remove(item);
3534
3535 ALOGVV("%s STATE %d CMD %d out->compr %p",
3536 __func__, out->offload_state, cmd->cmd, out->compr);
3537
3538 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
3539 free(cmd);
3540 break;
3541 }
3542
Haynes Mathew Georgeee5836f2017-11-21 18:02:10 -08003543 // allow OFFLOAD_CMD_ERROR reporting during standby
3544 // this is needed to handle failures during compress_open
3545 // Note however that on a pause timeout, the stream is closed
3546 // and no offload usecase will be active. Therefore this
3547 // special case is needed for compress_open failures alone
3548 if (cmd->cmd != OFFLOAD_CMD_ERROR &&
3549 out->compr == NULL) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003550 ALOGE("%s: Compress handle is NULL", __func__);
Haynes Mathew Georgea9abb202016-06-02 14:13:20 -07003551 free(cmd);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003552 pthread_cond_signal(&out->cond);
3553 continue;
3554 }
3555 out->offload_thread_blocked = true;
3556 pthread_mutex_unlock(&out->lock);
3557 send_callback = false;
3558 switch(cmd->cmd) {
3559 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07003560 ALOGD("copl(%p):calling compress_wait", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003561 compress_wait(out->compr, -1);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07003562 ALOGD("copl(%p):out of compress_wait", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003563 send_callback = true;
3564 event = STREAM_CBK_EVENT_WRITE_READY;
3565 break;
3566 case OFFLOAD_CMD_PARTIAL_DRAIN:
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08003567 ret = compress_next_track(out->compr);
Sidipotu Ashok55820562014-02-10 16:16:38 +05303568 if(ret == 0) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07003569 ALOGD("copl(%p):calling compress_partial_drain", out);
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05303570 ret = compress_partial_drain(out->compr);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07003571 ALOGD("copl(%p):out of compress_partial_drain", out);
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05303572 if (ret < 0)
3573 ret = -errno;
Sidipotu Ashok55820562014-02-10 16:16:38 +05303574 }
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05303575 else if (ret == -ETIMEDOUT)
Aniket Kumar Lata3570fb12017-11-08 15:53:44 -08003576 ret = compress_drain(out->compr);
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08003577 else
3578 ALOGE("%s: Next track returned error %d",__func__, ret);
Aniket Kumar Lata3570fb12017-11-08 15:53:44 -08003579 if (-ENETRESET != ret && !(-EINTR == ret &&
3580 CARD_STATUS_OFFLINE == out->card_status)) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05303581 send_callback = true;
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05303582 pthread_mutex_lock(&out->lock);
3583 out->send_new_metadata = 1;
3584 out->send_next_track_params = true;
3585 pthread_mutex_unlock(&out->lock);
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05303586 event = STREAM_CBK_EVENT_DRAIN_READY;
3587 ALOGV("copl(%p):send drain callback, ret %d", out, ret);
3588 } else
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05303589 ALOGI("%s: Block drain ready event during SSR", __func__);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003590 break;
3591 case OFFLOAD_CMD_DRAIN:
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07003592 ALOGD("copl(%p):calling compress_drain", out);
Aniket Kumar Lata3570fb12017-11-08 15:53:44 -08003593 ret = compress_drain(out->compr);
3594 ALOGD("copl(%p):out of compress_drain", out);
3595 // EINTR check avoids drain interruption due to SSR
3596 if (-ENETRESET != ret && !(-EINTR == ret &&
3597 CARD_STATUS_OFFLINE == out->card_status)) {
3598 send_callback = true;
3599 event = STREAM_CBK_EVENT_DRAIN_READY;
3600 } else
3601 ALOGI("%s: Block drain ready event during SSR", __func__);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003602 break;
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05303603 case OFFLOAD_CMD_ERROR:
3604 ALOGD("copl(%p): sending error callback to AF", out);
3605 send_callback = true;
3606 event = STREAM_CBK_EVENT_ERROR;
3607 break;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003608 default:
3609 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
3610 break;
3611 }
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07003612 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003613 out->offload_thread_blocked = false;
3614 pthread_cond_signal(&out->cond);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08003615 if (send_callback && out->client_callback) {
3616 ALOGVV("%s: sending client_callback event %d", __func__, event);
3617 out->client_callback(event, NULL, out->client_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -07003618 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003619 free(cmd);
3620 }
3621
3622 pthread_cond_signal(&out->cond);
3623 while (!list_empty(&out->offload_cmd_list)) {
3624 item = list_head(&out->offload_cmd_list);
3625 list_remove(item);
3626 free(node_to_item(item, struct offload_cmd, node));
3627 }
3628 pthread_mutex_unlock(&out->lock);
3629
3630 return NULL;
3631}
3632
3633static int create_offload_callback_thread(struct stream_out *out)
3634{
3635 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
3636 list_init(&out->offload_cmd_list);
3637 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
3638 offload_thread_loop, out);
3639 return 0;
3640}
3641
3642static int destroy_offload_callback_thread(struct stream_out *out)
3643{
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07003644 lock_output_stream(out);
Weiyin Jiangd5974e62020-09-08 20:28:22 +08003645 pthread_mutex_lock(&out->latch_lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003646 stop_compressed_output_l(out);
3647 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
3648
Weiyin Jiangd5974e62020-09-08 20:28:22 +08003649 pthread_mutex_unlock(&out->latch_lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003650 pthread_mutex_unlock(&out->lock);
3651 pthread_join(out->offload_thread, (void **) NULL);
3652 pthread_cond_destroy(&out->offload_cond);
3653
3654 return 0;
3655}
3656
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003657static int stop_output_stream(struct stream_out *out)
3658{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05303659 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003660 struct audio_usecase *uc_info;
3661 struct audio_device *adev = out->dev;
Aalique Grahame22e49102018-12-18 14:23:57 -08003662 bool has_voip_usecase =
3663 get_usecase_from_list(adev, USECASE_AUDIO_PLAYBACK_VOIP) != NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003664
Eric Laurent994a6932013-07-17 11:51:42 -07003665 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003666 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003667 uc_info = get_usecase_from_list(adev, out->usecase);
3668 if (uc_info == NULL) {
3669 ALOGE("%s: Could not find the usecase (%d) in the list",
3670 __func__, out->usecase);
3671 return -EINVAL;
3672 }
3673
Derek Chenea197282019-01-07 17:35:01 -08003674 if (audio_extn_ext_hw_plugin_usecase_stop(adev->ext_hw_plugin, uc_info))
3675 ALOGE("%s: failed to stop ext hw plugin", __func__);
Derek Chend2530072014-11-24 12:39:14 -08003676
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07003677 if (is_offload_usecase(out->usecase) &&
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05303678 !(audio_extn_passthru_is_passthrough_stream(out))) {
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08003679 if (adev->visualizer_stop_output != NULL)
3680 adev->visualizer_stop_output(out->handle, out->pcm_device_id);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08003681
3682 audio_extn_dts_remove_state_notifier_node(out->usecase);
3683
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08003684 if (adev->offload_effects_stop_output != NULL)
3685 adev->offload_effects_stop_output(out->handle, out->pcm_device_id);
vivek mehtad15d2bf2019-05-17 13:35:10 -07003686 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_ULL ||
3687 out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
3688 audio_low_latency_hint_end();
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08003689 }
Eric Laurentc4aef752013-09-12 17:45:53 -07003690
Arun Mirpuri2ce92a62018-09-12 18:36:10 -07003691 if (out->usecase == USECASE_INCALL_MUSIC_UPLINK ||
3692 out->usecase == USECASE_INCALL_MUSIC_UPLINK2) {
Arun Mirpurief53ce52018-09-11 18:00:09 -07003693 voice_set_device_mute_flag(adev, false);
Arun Mirpuri2ce92a62018-09-12 18:36:10 -07003694 }
Arun Mirpurief53ce52018-09-11 18:00:09 -07003695
Eric Laurent150dbfe2013-02-27 14:31:02 -08003696 /* 1. Get and set stream specific mixer controls */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07003697 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003698
3699 /* 2. Disable the rx device */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07003700 disable_snd_device(adev, uc_info->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003701
Aalique Grahame22e49102018-12-18 14:23:57 -08003702 audio_extn_extspk_update(adev->extspk);
3703
Xiaojun Sang785b5da2017-08-03 15:52:29 +08003704 if (is_offload_usecase(out->usecase)) {
3705 audio_enable_asm_bit_width_enforce_mode(adev->mixer,
3706 adev->dsp_bit_width_enforce_mode,
3707 false);
3708 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003709 if (is_usb_out_device_type(&out->device_list)) {
Garmond Leung5fd0b552018-04-17 11:56:12 -07003710 ret = audio_extn_usb_check_and_set_svc_int(uc_info,
3711 false);
3712
3713 if (ret != 0)
3714 check_usecases_codec_backend(adev, uc_info, uc_info->out_snd_device);
3715 /* default service interval was successfully updated,
3716 reopen USB backend with new service interval */
3717 ret = 0;
3718 }
Xiaojun Sang785b5da2017-08-03 15:52:29 +08003719
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08003720 list_remove(&uc_info->list);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303721 out->started = 0;
Weiyin Jiangd5974e62020-09-08 20:28:22 +08003722 pthread_mutex_lock(&out->latch_lock);
3723 out->muted = false;
3724 pthread_mutex_unlock(&out->latch_lock);
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07003725 if (is_offload_usecase(out->usecase) &&
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05303726 (audio_extn_passthru_is_passthrough_stream(out))) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07003727 ALOGV("Disable passthrough , reset mixer to pcm");
3728 /* NO_PASSTHROUGH */
Meng Wang4c32fb42020-01-16 17:57:11 +08003729#ifdef AUDIO_GKI_ENABLED
3730 /* out->compr_config.codec->reserved[0] is for compr_passthr */
3731 out->compr_config.codec->reserved[0] = 0;
3732#else
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07003733 out->compr_config.codec->compr_passthr = 0;
Meng Wang4c32fb42020-01-16 17:57:11 +08003734#endif
Mingming Yin21854652016-04-13 11:54:02 -07003735 audio_extn_passthru_on_stop(out);
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07003736 audio_extn_dolby_set_dap_bypass(adev, DAP_STATE_ON);
3737 }
Eric Laurent07eeafd2013-10-06 12:52:49 -07003738
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05303739 /* Must be called after removing the usecase from list */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003740 if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_AUX_DIGITAL))
Md Mansoor Ahmeddb1b4f92018-01-25 18:56:31 +05303741 audio_extn_keep_alive_start(KEEP_ALIVE_OUT_HDMI);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05303742
Manish Dewangan21a850a2017-08-14 12:03:55 +05303743 if (out->ip_hdlr_handle) {
Naresh Tanniru85819452017-05-04 18:55:45 -07003744 ret = audio_extn_ip_hdlr_intf_close(out->ip_hdlr_handle, true, out);
3745 if (ret < 0)
3746 ALOGE("%s: audio_extn_ip_hdlr_intf_close failed %d",__func__, ret);
3747 }
3748
juyuchen2d415992018-11-16 14:15:16 +08003749 /* 1) media + voip output routing to handset must route media back to
3750 speaker when voip stops.
3751 2) trigger voip input to reroute when voip output changes to
3752 hearing aid. */
Aalique Grahame22e49102018-12-18 14:23:57 -08003753 if (has_voip_usecase ||
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003754 compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
Aalique Grahame22e49102018-12-18 14:23:57 -08003755 struct listnode *node;
3756 struct audio_usecase *usecase;
3757 list_for_each(node, &adev->usecase_list) {
3758 usecase = node_to_item(node, struct audio_usecase, list);
juyuchen2d415992018-11-16 14:15:16 +08003759 if ((usecase->type == PCM_CAPTURE &&
3760 usecase->id != USECASE_AUDIO_RECORD_VOIP)
3761 || usecase == uc_info)
Aalique Grahame22e49102018-12-18 14:23:57 -08003762 continue;
3763
3764 ALOGD("%s: select_devices at usecase(%d: %s) after removing the usecase(%d: %s)",
3765 __func__, usecase->id, use_case_table[usecase->id],
3766 out->usecase, use_case_table[out->usecase]);
3767 select_devices(adev, usecase->id);
3768 }
3769 }
3770
Garmond Leung5fd0b552018-04-17 11:56:12 -07003771 free(uc_info);
Eric Laurent994a6932013-07-17 11:51:42 -07003772 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003773 return ret;
3774}
3775
Vignesh Kulothungana6927272019-02-20 15:17:07 -08003776struct pcm* pcm_open_prepare_helper(unsigned int snd_card, unsigned int pcm_device_id,
3777 unsigned int flags, unsigned int pcm_open_retry_count,
3778 struct pcm_config *config)
3779{
3780 struct pcm* pcm = NULL;
3781
3782 while (1) {
3783 pcm = pcm_open(snd_card, pcm_device_id, flags, config);
3784 if (pcm == NULL || !pcm_is_ready(pcm)) {
3785 ALOGE("%s: %s", __func__, pcm_get_error(pcm));
3786 if (pcm != NULL) {
3787 pcm_close(pcm);
3788 pcm = NULL;
3789 }
Weiyin Jiang72197252019-10-09 11:49:32 +08003790 if (pcm_open_retry_count == 0)
Vignesh Kulothungana6927272019-02-20 15:17:07 -08003791 return NULL;
3792
Weiyin Jiang72197252019-10-09 11:49:32 +08003793 pcm_open_retry_count--;
Vignesh Kulothungana6927272019-02-20 15:17:07 -08003794 usleep(PROXY_OPEN_WAIT_TIME * 1000);
3795 continue;
3796 }
3797 break;
3798 }
3799
3800 if (pcm_is_ready(pcm)) {
3801 int ret = pcm_prepare(pcm);
3802 if (ret < 0) {
3803 ALOGE("%s: pcm_prepare returned %d", __func__, ret);
3804 pcm_close(pcm);
3805 pcm = NULL;
3806 }
3807 }
3808
3809 return pcm;
3810}
3811
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003812int start_output_stream(struct stream_out *out)
3813{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003814 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003815 struct audio_usecase *uc_info;
3816 struct audio_device *adev = out->dev;
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08003817 char mixer_ctl_name[128];
3818 struct mixer_ctl *ctl = NULL;
3819 char* perf_mode[] = {"ULL", "ULL_PP", "LL"};
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303820 bool a2dp_combo = false;
Vignesh Kulothungana6927272019-02-20 15:17:07 -08003821 bool is_haptic_usecase = (out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) ? true: false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003822
Haynes Mathew George380745d2017-10-04 15:27:45 -07003823 ATRACE_BEGIN("start_output_stream");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07003824 if ((out->usecase < 0) || (out->usecase >= AUDIO_USECASE_MAX)) {
3825 ret = -EINVAL;
3826 goto error_config;
3827 }
3828
Vignesh Kulothungana6927272019-02-20 15:17:07 -08003829 ALOGD("%s: enter: stream(%p)usecase(%d: %s) devices(%#x) is_haptic_usecase(%d)",
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05303830 __func__, &out->stream, out->usecase, use_case_table[out->usecase],
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003831 get_device_types(&out->device_list), is_haptic_usecase);
3832
3833 bool is_speaker_active = compare_device_type(&out->device_list,
3834 AUDIO_DEVICE_OUT_SPEAKER);
3835 bool is_speaker_safe_active = compare_device_type(&out->device_list,
3836 AUDIO_DEVICE_OUT_SPEAKER_SAFE);
Naresh Tanniru4c630392014-05-12 01:05:52 +05303837
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05303838 if (CARD_STATUS_OFFLINE == out->card_status ||
3839 CARD_STATUS_OFFLINE == adev->card_status) {
3840 ALOGW("out->card_status or adev->card_status offline, try again");
Dhanalakshmi Siddani4d57e992014-07-17 16:37:51 +05303841 ret = -EIO;
Mingshu Pang5fc696f2020-02-28 12:32:00 +08003842 goto error_fatal;
Naresh Tanniru4c630392014-05-12 01:05:52 +05303843 }
Naresh Tanniru4c630392014-05-12 01:05:52 +05303844
Arun Mirpuri2ce92a62018-09-12 18:36:10 -07003845 //Update incall music usecase to reflect correct voice session
3846 if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
3847 ret = voice_extn_check_and_set_incall_music_usecase(adev, out);
3848 if (ret != 0) {
3849 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
3850 __func__, ret);
3851 goto error_config;
3852 }
3853 }
3854
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003855 if (is_a2dp_out_device_type(&out->device_list)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02003856 if (!audio_extn_a2dp_source_is_ready()) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003857 if (is_speaker_active || is_speaker_safe_active) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303858 a2dp_combo = true;
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05303859 } else {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303860 if (!(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
3861 ALOGE("%s: A2DP profile is not ready, return error", __func__);
3862 ret = -EAGAIN;
3863 goto error_config;
3864 }
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05303865 }
3866 }
3867 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003868 if (is_sco_out_device_type(&out->device_list)) {
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303869 if (!adev->bt_sco_on) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003870 if (is_speaker_active) {
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303871 //combo usecase just by pass a2dp
3872 ALOGW("%s: SCO is not connected, route it to speaker", __func__);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003873 reassign_device_list(&out->device_list, AUDIO_DEVICE_OUT_SPEAKER, "");
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303874 } else {
3875 ALOGE("%s: SCO profile is not ready, return error", __func__);
3876 ret = -EAGAIN;
3877 goto error_config;
3878 }
3879 }
3880 }
3881
Eric Laurentb23d5282013-05-14 15:27:20 -07003882 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003883 if (out->pcm_device_id < 0) {
3884 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
3885 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08003886 ret = -EINVAL;
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07003887 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003888 }
3889
Vignesh Kulothungana6927272019-02-20 15:17:07 -08003890 if (is_haptic_usecase) {
Meng Wang51d8c2a2020-04-27 15:23:21 +08003891 adev->haptic_pcm_device_id = platform_get_pcm_device_id(
3892 USECASE_AUDIO_PLAYBACK_HAPTICS, PCM_PLAYBACK);
Vignesh Kulothungana6927272019-02-20 15:17:07 -08003893 if (adev->haptic_pcm_device_id < 0) {
3894 ALOGE("%s: Invalid Haptics pcm device id(%d) for the usecase(%d)",
3895 __func__, adev->haptic_pcm_device_id, out->usecase);
3896 ret = -EINVAL;
3897 goto error_config;
3898 }
3899 }
3900
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003901 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07003902
3903 if (!uc_info) {
3904 ret = -ENOMEM;
3905 goto error_config;
3906 }
3907
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003908 uc_info->id = out->usecase;
3909 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08003910 uc_info->stream.out = out;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003911 list_init(&uc_info->device_list);
3912 assign_devices(&uc_info->device_list, &out->device_list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003913 uc_info->in_snd_device = SND_DEVICE_NONE;
3914 uc_info->out_snd_device = SND_DEVICE_NONE;
Garmond Leung5fd0b552018-04-17 11:56:12 -07003915
3916 /* This must be called before adding this usecase to the list */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003917 if (is_usb_out_device_type(&out->device_list)) {
Garmond Leung5fd0b552018-04-17 11:56:12 -07003918 audio_extn_usb_check_and_set_svc_int(uc_info, true);
3919 /* USB backend is not reopened immediately.
3920 This is eventually done as part of select_devices */
3921 }
3922
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08003923 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003924
Wei Wangf7ca6c92017-11-21 14:51:20 -08003925 audio_streaming_hint_start();
Sudheer Papothifa9d2282015-09-17 01:53:25 +05303926 audio_extn_perf_lock_acquire(&adev->perf_lock_handle, 0,
3927 adev->perf_lock_opts,
3928 adev->perf_lock_opts_size);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05303929
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003930 if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
Md Mansoor Ahmeddb1b4f92018-01-25 18:56:31 +05303931 audio_extn_keep_alive_stop(KEEP_ALIVE_OUT_HDMI);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05303932 if (audio_extn_passthru_is_enabled() &&
3933 audio_extn_passthru_is_passthrough_stream(out)) {
3934 audio_extn_passthru_on_start(out);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05303935 }
3936 }
3937
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003938 if (is_a2dp_out_device_type(&out->device_list) &&
Florian Pfister1a84f312018-07-19 14:38:18 +02003939 (!audio_extn_a2dp_source_is_ready())) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303940 if (!a2dp_combo) {
3941 check_a2dp_restore_l(adev, out, false);
3942 } else {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003943 struct listnode dev;
3944 list_init(&dev);
3945 assign_devices(&dev, &out->device_list);
3946 if (compare_device_type(&dev, AUDIO_DEVICE_OUT_SPEAKER_SAFE))
3947 reassign_device_list(&out->device_list,
3948 AUDIO_DEVICE_OUT_SPEAKER_SAFE, "");
Aalique Grahame22e49102018-12-18 14:23:57 -08003949 else
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003950 reassign_device_list(&out->device_list,
3951 AUDIO_DEVICE_OUT_SPEAKER, "");
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303952 select_devices(adev, out->usecase);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08003953 assign_devices(&out->device_list, &dev);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303954 }
3955 } else {
Revathi Uddarajub26e3932020-06-10 14:51:02 +05303956 select_devices(adev, out->usecase);
3957 if (is_a2dp_out_device_type(&out->device_list) &&
3958 !adev->a2dp_started) {
3959 if (is_speaker_active || is_speaker_safe_active) {
3960 struct listnode dev;
3961 list_init(&dev);
3962 assign_devices(&dev, &out->device_list);
3963 if (compare_device_type(&dev, AUDIO_DEVICE_OUT_SPEAKER_SAFE))
3964 reassign_device_list(&out->device_list,
3965 AUDIO_DEVICE_OUT_SPEAKER_SAFE, "");
3966 else
3967 reassign_device_list(&out->device_list,
3968 AUDIO_DEVICE_OUT_SPEAKER, "");
3969 select_devices(adev, out->usecase);
3970 assign_devices(&out->device_list, &dev);
3971 } else {
3972 ret = -EINVAL;
3973 goto error_open;
3974 }
3975 }
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303976 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003977
Arun Mirpuri2ce92a62018-09-12 18:36:10 -07003978 if (out->usecase == USECASE_INCALL_MUSIC_UPLINK ||
3979 out->usecase == USECASE_INCALL_MUSIC_UPLINK2) {
Arun Mirpurief53ce52018-09-11 18:00:09 -07003980 voice_set_device_mute_flag(adev, true);
Arun Mirpuri2ce92a62018-09-12 18:36:10 -07003981 }
Arun Mirpurief53ce52018-09-11 18:00:09 -07003982
Derek Chenea197282019-01-07 17:35:01 -08003983 if (audio_extn_ext_hw_plugin_usecase_start(adev->ext_hw_plugin, uc_info))
3984 ALOGE("%s: failed to start ext hw plugin", __func__);
Derek Chend2530072014-11-24 12:39:14 -08003985
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003986 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d) format(%#x)",
3987 __func__, adev->snd_card, out->pcm_device_id, out->config.format);
Haynes Mathew George16081042017-05-31 17:16:49 -07003988
3989 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
Arun Mirpuri5d170872019-03-26 13:21:31 -07003990 ALOGD("%s: Starting MMAP stream", __func__);
Haynes Mathew George16081042017-05-31 17:16:49 -07003991 if (out->pcm == NULL || !pcm_is_ready(out->pcm)) {
3992 ALOGE("%s: pcm stream not ready", __func__);
3993 goto error_open;
3994 }
3995 ret = pcm_start(out->pcm);
3996 if (ret < 0) {
3997 ALOGE("%s: MMAP pcm_start failed ret %d", __func__, ret);
3998 goto error_open;
3999 }
Arun Mirpuri5d170872019-03-26 13:21:31 -07004000 out_set_mmap_volume(&out->stream, out->volume_l, out->volume_r);
Haynes Mathew George16081042017-05-31 17:16:49 -07004001 } else if (!is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07004002 unsigned int flags = PCM_OUT;
4003 unsigned int pcm_open_retry_count = 0;
4004 if (out->usecase == USECASE_AUDIO_PLAYBACK_AFE_PROXY) {
4005 flags |= PCM_MMAP | PCM_NOIRQ;
4006 pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT;
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004007 } else if (out->realtime) {
Haynes Mathew George4ab3ba92017-12-11 14:49:43 -08004008 flags |= PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07004009 } else
4010 flags |= PCM_MONOTONIC;
4011
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08004012 if ((adev->vr_audio_mode_enabled) &&
4013 (out->flags & AUDIO_OUTPUT_FLAG_RAW)) {
4014 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
4015 "PCM_Dev %d Topology", out->pcm_device_id);
4016 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
4017 if (!ctl) {
4018 ALOGI("%s: Could not get ctl for mixer cmd might be ULL - %s",
4019 __func__, mixer_ctl_name);
4020 } else {
4021 //if success use ULLPP
4022 ALOGI("%s: mixer ctrl %s succeeded setting up ULL for %d",
4023 __func__, mixer_ctl_name, out->pcm_device_id);
4024 //There is a still a possibility that some sessions
4025 // that request for FAST|RAW when 3D audio is active
4026 //can go through ULLPP. Ideally we expects apps to
4027 //listen to audio focus and stop concurrent playback
4028 //Also, we will look for mode flag (voice_in_communication)
4029 //before enabling the realtime flag.
4030 mixer_ctl_set_enum_by_string(ctl, perf_mode[1]);
4031 }
4032 }
4033
Surendar Karka91fa3682018-07-02 18:12:12 +05304034 if (out->realtime)
4035 platform_set_stream_channel_map(adev->platform, out->channel_mask,
4036 out->pcm_device_id, &out->channel_map_param.channel_map[0]);
4037
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004038 out->pcm = pcm_open_prepare_helper(adev->snd_card, out->pcm_device_id,
4039 flags, pcm_open_retry_count,
4040 &(out->config));
4041 if (out->pcm == NULL) {
4042 ret = -EIO;
4043 goto error_open;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004044 }
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004045
4046 if (is_haptic_usecase) {
4047 adev->haptic_pcm = pcm_open_prepare_helper(adev->snd_card,
4048 adev->haptic_pcm_device_id,
4049 flags, pcm_open_retry_count,
4050 &(adev->haptics_config));
4051 // failure to open haptics pcm shouldnt stop audio,
4052 // so do not close audio pcm in case of error
Vignesh Kulothungane4039c12019-05-07 15:51:39 -07004053
4054 if (property_get_bool("vendor.audio.enable_haptic_audio_sync", false)) {
4055 ALOGD("%s: enable haptic audio synchronization", __func__);
4056 platform_set_qtime(adev->platform, out->pcm_device_id, adev->haptic_pcm_device_id);
4057 }
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004058 }
4059
Surendar Karka91fa3682018-07-02 18:12:12 +05304060 if (!out->realtime)
4061 platform_set_stream_channel_map(adev->platform, out->channel_mask,
Surendar Karkaf51b5842018-04-26 11:28:38 +05304062 out->pcm_device_id, &out->channel_map_param.channel_map[0]);
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07004063
Zhou Song2b8f28f2017-09-11 10:51:38 +08004064 // apply volume for voip playback after path is set up
4065 if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP)
4066 out_set_voip_volume(&out->stream, out->volume_l, out->volume_r);
Ramu Gottipati36547092018-12-28 11:32:09 +05304067 else if ((out->usecase == USECASE_AUDIO_PLAYBACK_LOW_LATENCY || out->usecase == USECASE_AUDIO_PLAYBACK_DEEP_BUFFER ||
4068 out->usecase == USECASE_AUDIO_PLAYBACK_ULL) && (out->apply_volume)) {
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05304069 out_set_pcm_volume(&out->stream, out->volume_l, out->volume_r);
4070 out->apply_volume = false;
Derek Chenf13dd492018-11-13 14:53:51 -08004071 } else if (audio_extn_auto_hal_is_bus_device_usecase(out->usecase)) {
4072 out_set_pcm_volume(&out->stream, out->volume_l, out->volume_r);
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05304073 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004074 } else {
Zhou Song79929fe2020-01-20 17:57:43 +08004075 /*
4076 * set custom channel map if:
4077 * 1. neither mono nor stereo clips i.e. channels > 2 OR
4078 * 2. custom channel map has been set by client
4079 * else default channel map of FC/FR/FL can always be set to DSP
4080 */
4081 if (popcount(out->channel_mask) > 2 || out->channel_map_param.channel_map[0])
4082 platform_set_stream_channel_map(adev->platform, out->channel_mask,
4083 out->pcm_device_id, &out->channel_map_param.channel_map[0]);
Xiaojun Sang785b5da2017-08-03 15:52:29 +08004084 audio_enable_asm_bit_width_enforce_mode(adev->mixer,
4085 adev->dsp_bit_width_enforce_mode,
4086 true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004087 out->pcm = NULL;
Haynes Mathew George380745d2017-10-04 15:27:45 -07004088 ATRACE_BEGIN("compress_open");
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08004089 out->compr = compress_open(adev->snd_card,
4090 out->pcm_device_id,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004091 COMPRESS_IN, &out->compr_config);
Haynes Mathew George380745d2017-10-04 15:27:45 -07004092 ATRACE_END();
Satish Babu Patakokila54ce83d2018-07-06 18:00:37 +05304093 if (errno == ENETRESET && !is_compress_ready(out->compr)) {
Sharad Sanglec6f32552018-05-04 16:15:38 +05304094 ALOGE("%s: compress_open failed errno:%d\n", __func__, errno);
4095 adev->card_status = CARD_STATUS_OFFLINE;
4096 out->card_status = CARD_STATUS_OFFLINE;
4097 ret = -EIO;
4098 goto error_open;
4099 }
4100
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004101 if (out->compr && !is_compress_ready(out->compr)) {
Haynes Mathew Georgeee5836f2017-11-21 18:02:10 -08004102 ALOGE("%s: failed /w error %s", __func__, compress_get_error(out->compr));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004103 compress_close(out->compr);
4104 out->compr = NULL;
4105 ret = -EIO;
4106 goto error_open;
4107 }
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05304108 /* compress_open sends params of the track, so reset the flag here */
4109 out->is_compr_metadata_avail = false;
4110
Ben Rombergerd771a7c2017-02-22 18:05:17 -08004111 if (out->client_callback)
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004112 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07004113
Fred Oh3f43e742015-03-04 18:42:34 -08004114 /* Since small bufs uses blocking writes, a write will be blocked
4115 for the default max poll time (20s) in the event of an SSR.
4116 Reduce the poll time to observe and deal with SSR faster.
4117 */
Ashish Jain5106d362016-05-11 19:23:33 +05304118 if (!out->non_blocking) {
Fred Oh3f43e742015-03-04 18:42:34 -08004119 compress_set_max_poll_wait(out->compr, 1000);
4120 }
4121
Manish Dewangan69426c82017-01-30 17:35:36 +05304122 audio_extn_utils_compress_set_render_mode(out);
Manish Dewangan58229382017-02-02 15:48:41 +05304123 audio_extn_utils_compress_set_clk_rec_mode(uc_info);
Manish Dewangan69426c82017-01-30 17:35:36 +05304124
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08004125 audio_extn_dts_create_state_notifier_node(out->usecase);
4126 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
4127 popcount(out->channel_mask),
4128 out->playback_started);
4129
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08004130#ifdef DS1_DOLBY_DDP_ENABLED
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05304131 if (audio_extn_utils_is_dolby_format(out->format))
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08004132 audio_extn_dolby_send_ddp_endp_params(adev);
4133#endif
Preetam Singh Ranawatd18d8832017-02-08 17:34:54 +05304134 if (!(audio_extn_passthru_is_passthrough_stream(out)) &&
4135 (out->sample_rate != 176400 && out->sample_rate <= 192000)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004136 if (adev->visualizer_start_output != NULL)
4137 adev->visualizer_start_output(out->handle, out->pcm_device_id);
4138 if (adev->offload_effects_start_output != NULL)
Ashish Jain5106d362016-05-11 19:23:33 +05304139 adev->offload_effects_start_output(out->handle, out->pcm_device_id, adev->mixer);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08004140 audio_extn_check_and_set_dts_hpx_state(adev);
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004141 }
Derek Chenf13dd492018-11-13 14:53:51 -08004142
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004143 if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_BUS)) {
Derek Chenf13dd492018-11-13 14:53:51 -08004144 /* Update cached volume from media to offload/direct stream */
4145 struct listnode *node = NULL;
4146 list_for_each(node, &adev->active_outputs_list) {
4147 streams_output_ctxt_t *out_ctxt = node_to_item(node,
4148 streams_output_ctxt_t,
4149 list);
4150 if (out_ctxt->output->usecase == USECASE_AUDIO_PLAYBACK_MEDIA) {
4151 out->volume_l = out_ctxt->output->volume_l;
4152 out->volume_r = out_ctxt->output->volume_r;
4153 }
4154 }
4155 out_set_compr_volume(&out->stream,
4156 out->volume_l, out->volume_r);
4157 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004158 }
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004159
4160 if (ret == 0) {
4161 register_out_stream(out);
4162 if (out->realtime) {
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07004163 if (out->pcm == NULL || !pcm_is_ready(out->pcm)) {
4164 ALOGE("%s: pcm stream not ready", __func__);
4165 goto error_open;
4166 }
Haynes Mathew George380745d2017-10-04 15:27:45 -07004167 ATRACE_BEGIN("pcm_start");
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004168 ret = pcm_start(out->pcm);
Haynes Mathew George380745d2017-10-04 15:27:45 -07004169 ATRACE_END();
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004170 if (ret < 0)
4171 goto error_open;
4172 }
4173 }
Wei Wangf7ca6c92017-11-21 14:51:20 -08004174 audio_streaming_hint_end();
Sudheer Papothifa9d2282015-09-17 01:53:25 +05304175 audio_extn_perf_lock_release(&adev->perf_lock_handle);
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07004176 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07004177
vivek mehtad15d2bf2019-05-17 13:35:10 -07004178 if (out->usecase == USECASE_AUDIO_PLAYBACK_ULL ||
4179 out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
4180 audio_low_latency_hint_start();
4181 }
4182
Manish Dewangan21a850a2017-08-14 12:03:55 +05304183 if (out->ip_hdlr_handle) {
Vidyakumar Athota6d655882017-05-22 18:26:24 -07004184 ret = audio_extn_ip_hdlr_intf_open(out->ip_hdlr_handle, true, out, out->usecase);
Naresh Tanniru85819452017-05-04 18:55:45 -07004185 if (ret < 0)
4186 ALOGE("%s: audio_extn_ip_hdlr_intf_open failed %d",__func__, ret);
4187 }
4188
Vignesh Kulothungan3b5fae52017-09-25 12:16:30 -07004189 // consider a scenario where on pause lower layers are tear down.
4190 // so on resume, swap mixer control need to be sent only when
4191 // backend is active, hence rather than sending from enable device
4192 // sending it from start of streamtream
4193
4194 platform_set_swap_channels(adev, true);
4195
Haynes Mathew George380745d2017-10-04 15:27:45 -07004196 ATRACE_END();
Vatsal Buchac09ae062018-11-14 13:25:08 +05304197 enable_gcov();
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004198 return ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004199error_open:
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004200 if (adev->haptic_pcm) {
4201 pcm_close(adev->haptic_pcm);
4202 adev->haptic_pcm = NULL;
4203 }
Wei Wangf7ca6c92017-11-21 14:51:20 -08004204 audio_streaming_hint_end();
Sudheer Papothifa9d2282015-09-17 01:53:25 +05304205 audio_extn_perf_lock_release(&adev->perf_lock_handle);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004206 stop_output_stream(out);
Mingshu Pang5fc696f2020-02-28 12:32:00 +08004207error_fatal:
Laxminath Kasam2cb4b752015-09-24 03:59:15 +05304208 /*
4209 * sleep 50ms to allow sufficient time for kernel
4210 * drivers to recover incases like SSR.
4211 */
4212 usleep(50000);
Mingshu Pang5fc696f2020-02-28 12:32:00 +08004213error_config:
Haynes Mathew George380745d2017-10-04 15:27:45 -07004214 ATRACE_END();
Vatsal Buchac09ae062018-11-14 13:25:08 +05304215 enable_gcov();
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08004216 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004217}
4218
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004219static int check_input_parameters(uint32_t sample_rate,
4220 audio_format_t format,
Aalique Grahame22e49102018-12-18 14:23:57 -08004221 int channel_count,
4222 bool is_usb_hifi)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004223{
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08004224 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004225
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05304226 if (((format != AUDIO_FORMAT_PCM_16_BIT) && (format != AUDIO_FORMAT_PCM_8_24_BIT) &&
4227 (format != AUDIO_FORMAT_PCM_24_BIT_PACKED) && (format != AUDIO_FORMAT_PCM_32_BIT) &&
4228 (format != AUDIO_FORMAT_PCM_FLOAT)) &&
Mingming Yine62d7842013-10-25 16:26:03 -07004229 !voice_extn_compress_voip_is_format_supported(format) &&
Ralf Herzaec80262018-07-03 07:08:49 +02004230 !audio_extn_compr_cap_format_supported(format) &&
4231 !audio_extn_cin_format_supported(format))
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004232 ret = -EINVAL;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08004233
Aalique Grahame22e49102018-12-18 14:23:57 -08004234 int max_channel_count = is_usb_hifi ? MAX_HIFI_CHANNEL_COUNT : MAX_CHANNEL_COUNT;
4235 if ((channel_count < MIN_CHANNEL_COUNT) || (channel_count > max_channel_count)) {
4236 ALOGE("%s: unsupported channel count (%d) passed Min / Max (%d / %d)", __func__,
4237 channel_count, MIN_CHANNEL_COUNT, max_channel_count);
4238 return -EINVAL;
4239 }
4240
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08004241 switch (channel_count) {
4242 case 1:
4243 case 2:
Chaithanya Krishna Bacharaju9955b162016-05-25 16:25:53 +05304244 case 3:
4245 case 4:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08004246 case 6:
Karthikeyan Mani07faa602018-08-20 11:01:32 -07004247 case 8:
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05304248 case 10:
4249 case 12:
4250 case 14:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08004251 break;
4252 default:
4253 ret = -EINVAL;
4254 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004255
4256 switch (sample_rate) {
4257 case 8000:
4258 case 11025:
4259 case 12000:
4260 case 16000:
4261 case 22050:
4262 case 24000:
4263 case 32000:
4264 case 44100:
4265 case 48000:
Haynes Mathew Georgec9253d12017-12-13 15:58:28 -08004266 case 88200:
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05304267 case 96000:
Haynes Mathew Georgec9253d12017-12-13 15:58:28 -08004268 case 176400:
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05304269 case 192000:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004270 break;
4271 default:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08004272 ret = -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004273 }
4274
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08004275 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004276}
4277
Naresh Tanniru04f71882018-06-26 17:46:22 +05304278
4279/** Add a value in a list if not already present.
4280 * @return true if value was successfully inserted or already present,
4281 * false if the list is full and does not contain the value.
4282 */
4283static bool register_uint(uint32_t value, uint32_t* list, size_t list_length) {
4284 for (size_t i = 0; i < list_length; i++) {
4285 if (list[i] == value) return true; // value is already present
4286 if (list[i] == 0) { // no values in this slot
4287 list[i] = value;
4288 return true; // value inserted
4289 }
4290 }
4291 return false; // could not insert value
4292}
4293
4294/** Add channel_mask in supported_channel_masks if not already present.
4295 * @return true if channel_mask was successfully inserted or already present,
4296 * false if supported_channel_masks is full and does not contain channel_mask.
4297 */
4298static void register_channel_mask(audio_channel_mask_t channel_mask,
4299 audio_channel_mask_t supported_channel_masks[static MAX_SUPPORTED_CHANNEL_MASKS]) {
4300 ALOGE_IF(!register_uint(channel_mask, supported_channel_masks, MAX_SUPPORTED_CHANNEL_MASKS),
4301 "%s: stream can not declare supporting its channel_mask %x", __func__, channel_mask);
4302}
4303
4304/** Add format in supported_formats if not already present.
4305 * @return true if format was successfully inserted or already present,
4306 * false if supported_formats is full and does not contain format.
4307 */
4308static void register_format(audio_format_t format,
4309 audio_format_t supported_formats[static MAX_SUPPORTED_FORMATS]) {
4310 ALOGE_IF(!register_uint(format, supported_formats, MAX_SUPPORTED_FORMATS),
4311 "%s: stream can not declare supporting its format %x", __func__, format);
4312}
4313/** Add sample_rate in supported_sample_rates if not already present.
4314 * @return true if sample_rate was successfully inserted or already present,
4315 * false if supported_sample_rates is full and does not contain sample_rate.
4316 */
4317static void register_sample_rate(uint32_t sample_rate,
4318 uint32_t supported_sample_rates[static MAX_SUPPORTED_SAMPLE_RATES]) {
4319 ALOGE_IF(!register_uint(sample_rate, supported_sample_rates, MAX_SUPPORTED_SAMPLE_RATES),
4320 "%s: stream can not declare supporting its sample rate %x", __func__, sample_rate);
4321}
4322
Karthikeyan Manib38769c2018-11-07 15:44:13 -08004323static inline uint32_t lcm(uint32_t num1, uint32_t num2)
4324{
4325 uint32_t high = num1, low = num2, temp = 0;
4326
4327 if (!num1 || !num2)
4328 return 0;
4329
4330 if (num1 < num2) {
4331 high = num2;
4332 low = num1;
4333 }
4334
4335 while (low != 0) {
4336 temp = low;
4337 low = high % low;
4338 high = temp;
4339 }
4340 return (num1 * num2)/high;
4341}
4342
4343static inline uint32_t nearest_multiple(uint32_t num, uint32_t multiplier)
4344{
4345 uint32_t remainder = 0;
4346
4347 if (!multiplier)
4348 return num;
4349
4350 remainder = num % multiplier;
4351 if (remainder)
4352 num += (multiplier - remainder);
4353
4354 return num;
4355}
4356
Aalique Grahame22e49102018-12-18 14:23:57 -08004357static size_t get_stream_buffer_size(size_t duration_ms,
4358 uint32_t sample_rate,
4359 audio_format_t format,
4360 int channel_count,
4361 bool is_low_latency)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004362{
4363 size_t size = 0;
Karthikeyan Manib38769c2018-11-07 15:44:13 -08004364 uint32_t bytes_per_period_sample = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004365
Aalique Grahame22e49102018-12-18 14:23:57 -08004366 size = (sample_rate * duration_ms) / 1000;
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07004367 if (is_low_latency)
4368 size = configured_low_latency_capture_period_size;
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05304369
Karthikeyan Manib38769c2018-11-07 15:44:13 -08004370 bytes_per_period_sample = audio_bytes_per_sample(format) * channel_count;
Aalique Grahame22e49102018-12-18 14:23:57 -08004371 size *= audio_bytes_per_sample(format) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004372
Ralf Herzbd08d632018-09-28 15:50:49 +02004373 /* make sure the size is multiple of 32 bytes and additionally multiple of
4374 * the frame_size (required for 24bit samples and non-power-of-2 channel counts)
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07004375 * At 48 kHz mono 16-bit PCM:
4376 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
4377 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
Karthikeyan Manib38769c2018-11-07 15:44:13 -08004378 * Also, make sure the size is multiple of bytes per period sample
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07004379 */
Karthikeyan Manib38769c2018-11-07 15:44:13 -08004380 size = nearest_multiple(size, lcm(32, bytes_per_period_sample));
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07004381
4382 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004383}
4384
Aalique Grahame22e49102018-12-18 14:23:57 -08004385static size_t get_input_buffer_size(uint32_t sample_rate,
4386 audio_format_t format,
4387 int channel_count,
4388 bool is_low_latency)
4389{
4390 /* Don't know if USB HIFI in this context so use true to be conservative */
4391 if (check_input_parameters(sample_rate, format, channel_count,
4392 true /*is_usb_hifi */) != 0)
4393 return 0;
4394
4395 return get_stream_buffer_size(AUDIO_CAPTURE_PERIOD_DURATION_MSEC,
4396 sample_rate,
4397 format,
4398 channel_count,
4399 is_low_latency);
4400}
4401
Derek Chenf6318be2017-06-12 17:16:24 -04004402size_t get_output_period_size(uint32_t sample_rate,
4403 audio_format_t format,
4404 int channel_count,
4405 int duration /*in millisecs*/)
Ashish Jain058165c2016-09-28 23:18:48 +05304406{
4407 size_t size = 0;
4408 uint32_t bytes_per_sample = audio_bytes_per_sample(format);
4409
4410 if ((duration == 0) || (sample_rate == 0) ||
4411 (bytes_per_sample == 0) || (channel_count == 0)) {
4412 ALOGW("Invalid config duration %d sr %d bps %d ch %d", duration, sample_rate,
4413 bytes_per_sample, channel_count);
4414 return -EINVAL;
4415 }
4416
4417 size = (sample_rate *
4418 duration *
4419 bytes_per_sample *
4420 channel_count) / 1000;
4421 /*
4422 * To have same PCM samples for all channels, the buffer size requires to
4423 * be multiple of (number of channels * bytes per sample)
4424 * For writes to succeed, the buffer must be written at address which is multiple of 32
4425 */
4426 size = ALIGN(size, (bytes_per_sample * channel_count * 32));
4427
4428 return (size/(channel_count * bytes_per_sample));
4429}
4430
Zhou Song48453a02018-01-10 17:50:59 +08004431static uint64_t get_actual_pcm_frames_rendered(struct stream_out *out, struct timespec *timestamp)
Ashish Jain5106d362016-05-11 19:23:33 +05304432{
4433 uint64_t actual_frames_rendered = 0;
Weiyin Jiang4813da12020-05-28 00:37:28 +08004434 uint64_t written_frames = 0;
4435 uint64_t kernel_frames = 0;
4436 uint64_t dsp_frames = 0;
4437 uint64_t signed_frames = 0;
4438 size_t kernel_buffer_size = 0;
Ashish Jain5106d362016-05-11 19:23:33 +05304439
4440 /* This adjustment accounts for buffering after app processor.
4441 * It is based on estimated DSP latency per use case, rather than exact.
4442 */
George Gao9ba8a142020-07-23 14:30:03 -07004443 dsp_frames = platform_render_latency(out) *
Weiyin Jiang4813da12020-05-28 00:37:28 +08004444 out->sample_rate / 1000000LL;
Ashish Jain5106d362016-05-11 19:23:33 +05304445
Zhou Song48453a02018-01-10 17:50:59 +08004446 pthread_mutex_lock(&out->position_query_lock);
Weiyin Jiang4813da12020-05-28 00:37:28 +08004447 written_frames = out->written /
4448 (audio_bytes_per_sample(out->hal_ip_format) * popcount(out->channel_mask));
4449
Ashish Jain5106d362016-05-11 19:23:33 +05304450 /* not querying actual state of buffering in kernel as it would involve an ioctl call
4451 * which then needs protection, this causes delay in TS query for pcm_offload usecase
4452 * hence only estimate.
4453 */
Weiyin Jiang4813da12020-05-28 00:37:28 +08004454 kernel_buffer_size = out->compr_config.fragment_size * out->compr_config.fragments;
4455 kernel_frames = kernel_buffer_size /
4456 (audio_bytes_per_sample(out->hal_op_format) * popcount(out->channel_mask));
Ashish Jain5106d362016-05-11 19:23:33 +05304457
Weiyin Jiang4813da12020-05-28 00:37:28 +08004458 if (written_frames >= (kernel_frames + dsp_frames))
4459 signed_frames = written_frames - kernel_frames - dsp_frames;
Ashish Jain5106d362016-05-11 19:23:33 +05304460
Zhou Song48453a02018-01-10 17:50:59 +08004461 if (signed_frames > 0) {
Ashish Jain5106d362016-05-11 19:23:33 +05304462 actual_frames_rendered = signed_frames;
Zhou Song48453a02018-01-10 17:50:59 +08004463 if (timestamp != NULL )
4464 *timestamp = out->writeAt;
4465 } else if (timestamp != NULL) {
4466 clock_gettime(CLOCK_MONOTONIC, timestamp);
4467 }
4468 pthread_mutex_unlock(&out->position_query_lock);
Ashish Jain5106d362016-05-11 19:23:33 +05304469
Weiyin Jiang4813da12020-05-28 00:37:28 +08004470 ALOGVV("%s signed frames %lld written frames %lld kernel frames %lld dsp frames %lld",
4471 __func__, signed_frames, written_frames, kernel_frames, dsp_frames);
Ashish Jain5106d362016-05-11 19:23:33 +05304472
4473 return actual_frames_rendered;
4474}
4475
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004476static uint32_t out_get_sample_rate(const struct audio_stream *stream)
4477{
4478 struct stream_out *out = (struct stream_out *)stream;
4479
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004480 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004481}
4482
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07004483static int out_set_sample_rate(struct audio_stream *stream __unused,
4484 uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004485{
4486 return -ENOSYS;
4487}
4488
4489static size_t out_get_buffer_size(const struct audio_stream *stream)
4490{
4491 struct stream_out *out = (struct stream_out *)stream;
4492
Varun Balaraje49253e2017-07-06 19:48:56 +05304493 if (is_interactive_usecase(out->usecase)) {
Sri Karri27279e12017-08-07 16:05:20 +05304494 return out->config.period_size * out->config.period_count;
Varun Balaraje49253e2017-07-06 19:48:56 +05304495 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Naresh Tanniruee3499a2017-01-05 14:05:35 +05304496 if (out->flags & AUDIO_OUTPUT_FLAG_TIMESTAMP)
4497 return out->compr_config.fragment_size - sizeof(struct snd_codec_metadata);
4498 else
4499 return out->compr_config.fragment_size;
4500 } else if(out->usecase == USECASE_COMPRESS_VOIP_CALL)
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08004501 return voice_extn_compress_voip_out_get_buffer_size(out);
Dhananjay Kumarac341582017-02-23 23:42:25 +05304502 else if (is_offload_usecase(out->usecase) &&
4503 out->flags == AUDIO_OUTPUT_FLAG_DIRECT)
Ashish Jain83a6cc22016-06-28 14:34:17 +05304504 return out->hal_fragment_size;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004505
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004506 return out->config.period_size * out->af_period_multiplier *
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07004507 audio_stream_out_frame_size((const struct audio_stream_out *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004508}
4509
4510static uint32_t out_get_channels(const struct audio_stream *stream)
4511{
4512 struct stream_out *out = (struct stream_out *)stream;
4513
4514 return out->channel_mask;
4515}
4516
4517static audio_format_t out_get_format(const struct audio_stream *stream)
4518{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004519 struct stream_out *out = (struct stream_out *)stream;
4520
4521 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004522}
4523
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07004524static int out_set_format(struct audio_stream *stream __unused,
4525 audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004526{
4527 return -ENOSYS;
4528}
4529
4530static int out_standby(struct audio_stream *stream)
4531{
4532 struct stream_out *out = (struct stream_out *)stream;
4533 struct audio_device *adev = out->dev;
Haynes Mathew George16081042017-05-31 17:16:49 -07004534 bool do_stop = true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004535
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05304536 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
4537 stream, out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004538
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07004539 lock_output_stream(out);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004540 if (!out->standby) {
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07004541 if (adev->adm_deregister_stream)
4542 adev->adm_deregister_stream(adev->adm_data, out->handle);
4543
Weiyin Jiangd5974e62020-09-08 20:28:22 +08004544 if (is_offload_usecase(out->usecase)) {
4545 pthread_mutex_lock(&out->latch_lock);
Haynes Mathew George7fce0a52016-06-23 18:22:27 -07004546 stop_compressed_output_l(out);
Weiyin Jiangd5974e62020-09-08 20:28:22 +08004547 pthread_mutex_unlock(&out->latch_lock);
4548 }
Haynes Mathew George7fce0a52016-06-23 18:22:27 -07004549
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08004550 pthread_mutex_lock(&adev->lock);
Daniel Hillenbrand21752f02013-05-23 10:10:00 +05304551
4552 amplifier_output_stream_standby((struct audio_stream_out *) stream);
4553
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004554 out->standby = true;
Zhou Songa8895042016-07-05 17:54:22 +08004555 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
4556 voice_extn_compress_voip_close_output_stream(stream);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304557 out->started = 0;
Zhou Songa8895042016-07-05 17:54:22 +08004558 pthread_mutex_unlock(&adev->lock);
4559 pthread_mutex_unlock(&out->lock);
4560 ALOGD("VOIP output entered standby");
4561 return 0;
4562 } else if (!is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004563 if (out->pcm) {
4564 pcm_close(out->pcm);
4565 out->pcm = NULL;
4566 }
Meng Wanga09da002020-04-20 12:56:04 +08004567 if (out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) {
4568 if (adev->haptic_pcm) {
4569 pcm_close(adev->haptic_pcm);
4570 adev->haptic_pcm = NULL;
4571 }
4572
4573 if (adev->haptic_buffer != NULL) {
4574 free(adev->haptic_buffer);
4575 adev->haptic_buffer = NULL;
4576 adev->haptic_buffer_size = 0;
4577 }
4578 adev->haptic_pcm_device_id = 0;
4579 }
4580
Haynes Mathew George16081042017-05-31 17:16:49 -07004581 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
4582 do_stop = out->playback_started;
4583 out->playback_started = false;
Phil Burkd898ba62019-06-20 12:49:01 -07004584
4585 if (out->mmap_shared_memory_fd >= 0) {
4586 ALOGV("%s: closing mmap_shared_memory_fd = %d",
4587 __func__, out->mmap_shared_memory_fd);
4588 close(out->mmap_shared_memory_fd);
4589 out->mmap_shared_memory_fd = -1;
4590 }
Haynes Mathew George16081042017-05-31 17:16:49 -07004591 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004592 } else {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07004593 ALOGD("copl(%p):standby", out);
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05304594 out->send_next_track_params = false;
4595 out->is_compr_metadata_avail = false;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004596 out->gapless_mdata.encoder_delay = 0;
4597 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004598 if (out->compr != NULL) {
4599 compress_close(out->compr);
4600 out->compr = NULL;
4601 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08004602 }
Haynes Mathew George16081042017-05-31 17:16:49 -07004603 if (do_stop) {
4604 stop_output_stream(out);
4605 }
Lakshman Chaluvaraju06677b42019-06-24 10:04:52 +05304606 // if fm is active route on selected device in UI
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004607 audio_extn_fm_route_on_selected_device(adev, &out->device_list);
Eric Laurent150dbfe2013-02-27 14:31:02 -08004608 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004609 }
4610 pthread_mutex_unlock(&out->lock);
Ashish Jainbbce4322016-02-16 13:25:27 +05304611 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004612 return 0;
4613}
4614
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304615static int out_on_error(struct audio_stream *stream)
4616{
4617 struct stream_out *out = (struct stream_out *)stream;
Ben Rombergerfd02a2f2018-09-17 10:23:23 -07004618 int status = 0;
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304619
4620 lock_output_stream(out);
Haynes Mathew Georgeee5836f2017-11-21 18:02:10 -08004621 // always send CMD_ERROR for offload streams, this
4622 // is needed e.g. when SSR happens within compress_open
4623 // since the stream is active, offload_callback_thread is also active.
4624 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Weiyin Jiangd5974e62020-09-08 20:28:22 +08004625 pthread_mutex_lock(&out->latch_lock);
Haynes Mathew Georgeee5836f2017-11-21 18:02:10 -08004626 stop_compressed_output_l(out);
Weiyin Jiangd5974e62020-09-08 20:28:22 +08004627 pthread_mutex_unlock(&out->latch_lock);
Ben Rombergerfd02a2f2018-09-17 10:23:23 -07004628 }
4629 pthread_mutex_unlock(&out->lock);
4630
4631 status = out_standby(&out->stream.common);
4632
4633 lock_output_stream(out);
4634 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Haynes Mathew Georgeee5836f2017-11-21 18:02:10 -08004635 send_offload_cmd_l(out, OFFLOAD_CMD_ERROR);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304636 }
Manisha Agarwal7b3e3772019-02-20 14:33:45 +05304637
4638 if (is_offload_usecase(out->usecase) && out->card_status == CARD_STATUS_OFFLINE) {
4639 ALOGD("Setting previous card status if offline");
4640 out->prev_card_status_offline = true;
4641 }
4642
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304643 pthread_mutex_unlock(&out->lock);
4644
Ben Rombergerfd02a2f2018-09-17 10:23:23 -07004645 return status;
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304646}
4647
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304648/*
Weiyin Jiangd5974e62020-09-08 20:28:22 +08004649 * standby implementation without locks, assumes that the callee already
4650 * has taken adev and out lock.
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304651 */
4652int out_standby_l(struct audio_stream *stream)
4653{
4654 struct stream_out *out = (struct stream_out *)stream;
4655 struct audio_device *adev = out->dev;
4656
4657 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
4658 stream, out->usecase, use_case_table[out->usecase]);
4659
4660 if (!out->standby) {
Haynes Mathew George380745d2017-10-04 15:27:45 -07004661 ATRACE_BEGIN("out_standby_l");
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304662 if (adev->adm_deregister_stream)
4663 adev->adm_deregister_stream(adev->adm_data, out->handle);
4664
Weiyin Jiangd5974e62020-09-08 20:28:22 +08004665 if (is_offload_usecase(out->usecase)) {
4666 pthread_mutex_lock(&out->latch_lock);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304667 stop_compressed_output_l(out);
Weiyin Jiangd5974e62020-09-08 20:28:22 +08004668 pthread_mutex_unlock(&out->latch_lock);
4669 }
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304670
4671 out->standby = true;
4672 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
4673 voice_extn_compress_voip_close_output_stream(stream);
4674 out->started = 0;
4675 ALOGD("VOIP output entered standby");
Haynes Mathew George380745d2017-10-04 15:27:45 -07004676 ATRACE_END();
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304677 return 0;
4678 } else if (!is_offload_usecase(out->usecase)) {
4679 if (out->pcm) {
4680 pcm_close(out->pcm);
4681 out->pcm = NULL;
4682 }
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004683 if (out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) {
4684 if (adev->haptic_pcm) {
4685 pcm_close(adev->haptic_pcm);
4686 adev->haptic_pcm = NULL;
4687 }
4688
4689 if (adev->haptic_buffer != NULL) {
4690 free(adev->haptic_buffer);
4691 adev->haptic_buffer = NULL;
4692 adev->haptic_buffer_size = 0;
4693 }
4694 adev->haptic_pcm_device_id = 0;
4695 }
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304696 } else {
4697 ALOGD("copl(%p):standby", out);
4698 out->send_next_track_params = false;
4699 out->is_compr_metadata_avail = false;
4700 out->gapless_mdata.encoder_delay = 0;
4701 out->gapless_mdata.encoder_padding = 0;
4702 if (out->compr != NULL) {
4703 compress_close(out->compr);
4704 out->compr = NULL;
4705 }
4706 }
4707 stop_output_stream(out);
Haynes Mathew George380745d2017-10-04 15:27:45 -07004708 ATRACE_END();
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304709 }
4710 ALOGD("%s: exit", __func__);
4711 return 0;
4712}
4713
Aalique Grahame22e49102018-12-18 14:23:57 -08004714static int out_dump(const struct audio_stream *stream, int fd)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004715{
Aalique Grahame22e49102018-12-18 14:23:57 -08004716 struct stream_out *out = (struct stream_out *)stream;
4717
4718 // We try to get the lock for consistency,
4719 // but it isn't necessary for these variables.
4720 // If we're not in standby, we may be blocked on a write.
4721 const bool locked = (pthread_mutex_trylock(&out->lock) == 0);
4722 dprintf(fd, " Standby: %s\n", out->standby ? "yes" : "no");
4723 dprintf(fd, " Frames written: %lld\n", (long long)out->written);
4724
Andy Hunga1f48fa2019-07-01 18:14:53 -07004725 char buffer[256]; // for statistics formatting
4726 if (!is_offload_usecase(out->usecase)) {
4727 simple_stats_to_string(&out->fifo_underruns, buffer, sizeof(buffer));
4728 dprintf(fd, " Fifo frame underruns: %s\n", buffer);
4729 }
4730
Andy Hungc6bfd4a2019-07-01 18:26:00 -07004731 if (out->start_latency_ms.n > 0) {
4732 simple_stats_to_string(&out->start_latency_ms, buffer, sizeof(buffer));
4733 dprintf(fd, " Start latency ms: %s\n", buffer);
4734 }
4735
Aalique Grahame22e49102018-12-18 14:23:57 -08004736 if (locked) {
4737 pthread_mutex_unlock(&out->lock);
4738 }
4739
4740 // dump error info
4741 (void)error_log_dump(
4742 out->error_log, fd, " " /* prefix */, 0 /* lines */, 0 /* limit_ns */);
4743
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004744 return 0;
4745}
4746
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004747static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
4748{
4749 int ret = 0;
4750 char value[32];
ApurupaPattapu2e084df2013-12-18 15:47:59 -08004751
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004752 if (!out || !parms) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08004753 ALOGE("%s: return invalid ",__func__);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004754 return -EINVAL;
4755 }
4756
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +05304757 ret = audio_extn_parse_compress_metadata(out, parms);
Weiyin Jiang18ac4e92015-03-15 15:03:40 +08004758
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004759 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
4760 if (ret >= 0) {
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +05304761 out->gapless_mdata.encoder_delay = atoi(value); //whats a good limit check?
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004762 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004763 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
4764 if (ret >= 0) {
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +05304765 out->gapless_mdata.encoder_padding = atoi(value);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004766 }
4767
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004768 ALOGV("%s new encoder delay %u and padding %u", __func__,
4769 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
4770
4771 return 0;
4772}
4773
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07004774static bool output_drives_call(struct audio_device *adev, struct stream_out *out)
4775{
4776 return out == adev->primary_output || out == adev->voice_tx_output;
4777}
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004778
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304779// note: this call is safe only if the stream_cb is
4780// removed first in close_output_stream (as is done now).
4781static void out_snd_mon_cb(void * stream, struct str_parms * parms)
4782{
4783 if (!stream || !parms)
4784 return;
4785
4786 struct stream_out *out = (struct stream_out *)stream;
4787 struct audio_device *adev = out->dev;
4788
4789 card_status_t status;
4790 int card;
4791 if (parse_snd_card_status(parms, &card, &status) < 0)
4792 return;
4793
4794 pthread_mutex_lock(&adev->lock);
4795 bool valid_cb = (card == adev->snd_card);
4796 pthread_mutex_unlock(&adev->lock);
4797
4798 if (!valid_cb)
4799 return;
4800
4801 lock_output_stream(out);
4802 if (out->card_status != status)
4803 out->card_status = status;
4804 pthread_mutex_unlock(&out->lock);
4805
4806 ALOGI("out_snd_mon_cb for card %d usecase %s, status %s", card,
4807 use_case_table[out->usecase],
4808 status == CARD_STATUS_OFFLINE ? "offline" : "online");
4809
Aditya Bavanari59ebed42019-02-05 17:44:57 +05304810 if (status == CARD_STATUS_OFFLINE) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304811 out_on_error(stream);
Aditya Bavanari59ebed42019-02-05 17:44:57 +05304812 if (voice_is_call_state_active(adev) &&
4813 out == adev->primary_output) {
4814 ALOGD("%s: SSR/PDR occurred, end all calls\n", __func__);
4815 pthread_mutex_lock(&adev->lock);
4816 voice_stop_call(adev);
4817 adev->mode = AUDIO_MODE_NORMAL;
4818 pthread_mutex_unlock(&adev->lock);
4819 }
4820 }
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304821 return;
4822}
4823
Kevin Rocardfce19002017-08-07 19:21:36 -07004824static int get_alive_usb_card(struct str_parms* parms) {
4825 int card;
4826 if ((str_parms_get_int(parms, "card", &card) >= 0) &&
4827 !audio_extn_usb_alive(card)) {
4828 return card;
4829 }
4830 return -ENODEV;
4831}
4832
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07004833int route_output_stream(struct stream_out *out,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004834 struct listnode *devices)
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07004835{
4836 struct audio_device *adev = out->dev;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07004837 int ret = 0;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004838 struct listnode new_devices;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07004839 bool bypass_a2dp = false;
4840 bool reconfig = false;
4841 unsigned long service_interval = 0;
4842
4843 ALOGD("%s: enter: usecase(%d: %s) devices %x",
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004844 __func__, out->usecase, use_case_table[out->usecase], get_device_types(devices));
4845
4846 list_init(&new_devices);
4847 assign_devices(&new_devices, devices);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07004848
4849 lock_output_stream(out);
4850 pthread_mutex_lock(&adev->lock);
4851
4852 /*
4853 * When HDMI cable is unplugged the music playback is paused and
4854 * the policy manager sends routing=0. But the audioflinger continues
4855 * to write data until standby time (3sec). As the HDMI core is
4856 * turned off, the write gets blocked.
4857 * Avoid this by routing audio to speaker until standby.
4858 */
Aniket Kumar Lata9c2fd892020-01-22 22:20:00 -08004859 if (is_single_device_type_equal(&out->device_list,
4860 AUDIO_DEVICE_OUT_AUX_DIGITAL) &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004861 list_empty(&new_devices) &&
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07004862 !audio_extn_passthru_is_passthrough_stream(out) &&
4863 (platform_get_edid_info(adev->platform) != 0) /* HDMI disconnected */) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004864 reassign_device_list(&new_devices, AUDIO_DEVICE_OUT_SPEAKER, "");
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07004865 }
4866 /*
4867 * When A2DP is disconnected the
4868 * music playback is paused and the policy manager sends routing=0
4869 * But the audioflinger continues to write data until standby time
4870 * (3sec). As BT is turned off, the write gets blocked.
4871 * Avoid this by routing audio to speaker until standby.
4872 */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004873 if (is_a2dp_out_device_type(&out->device_list) &&
4874 list_empty(&new_devices) &&
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07004875 !audio_extn_a2dp_source_is_ready() &&
4876 !adev->bt_sco_on) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004877 reassign_device_list(&new_devices, AUDIO_DEVICE_OUT_SPEAKER, "");
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07004878 }
4879 /*
4880 * When USB headset is disconnected the music platback paused
4881 * and the policy manager send routing=0. But if the USB is connected
4882 * back before the standby time, AFE is not closed and opened
4883 * when USB is connected back. So routing to speker will guarantee
4884 * AFE reconfiguration and AFE will be opend once USB is connected again
4885 */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004886 if (is_usb_out_device_type(&out->device_list) &&
4887 list_empty(&new_devices) &&
4888 !audio_extn_usb_connected(NULL)) {
4889 reassign_device_list(&new_devices, AUDIO_DEVICE_OUT_SPEAKER, "");
4890 }
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07004891 /* To avoid a2dp to sco overlapping / BT device improper state
4892 * check with BT lib about a2dp streaming support before routing
4893 */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004894 if (is_a2dp_out_device_type(&new_devices)) {
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07004895 if (!audio_extn_a2dp_source_is_ready()) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004896 if (compare_device_type(&new_devices, AUDIO_DEVICE_OUT_SPEAKER) ||
4897 compare_device_type(&new_devices, AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07004898 //combo usecase just by pass a2dp
4899 ALOGW("%s: A2DP profile is not ready,routing to speaker only", __func__);
4900 bypass_a2dp = true;
4901 } else {
4902 ALOGE("%s: A2DP profile is not ready,ignoring routing request", __func__);
4903 /* update device to a2dp and don't route as BT returned error
4904 * However it is still possible a2dp routing called because
4905 * of current active device disconnection (like wired headset)
4906 */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004907 assign_devices(&out->device_list, &new_devices);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07004908 pthread_mutex_unlock(&adev->lock);
4909 pthread_mutex_unlock(&out->lock);
4910 goto error;
4911 }
4912 }
4913 }
4914
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07004915 // Workaround: If routing to an non existing usb device, fail gracefully
4916 // The routing request will otherwise block during 10 second
4917 int card;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004918 if (is_usb_out_device_type(&new_devices)) {
4919 struct str_parms *parms =
4920 str_parms_create_str(get_usb_device_address(&new_devices));
4921 if (!parms)
4922 goto error;
4923 if ((card = get_alive_usb_card(parms)) >= 0) {
4924 ALOGW("%s: ignoring rerouting to non existing USB card %d", __func__, card);
4925 pthread_mutex_unlock(&adev->lock);
4926 pthread_mutex_unlock(&out->lock);
4927 str_parms_destroy(parms);
4928 ret = -ENOSYS;
4929 goto error;
4930 }
4931 str_parms_destroy(parms);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07004932 }
4933
Weiyin Jiang80c8f9a2020-01-10 20:42:08 +08004934 // Workaround: If routing to an non existing hdmi device, fail gracefully
4935 if (compare_device_type(&new_devices, AUDIO_DEVICE_OUT_AUX_DIGITAL) &&
4936 (platform_get_edid_info_v2(adev->platform,
4937 out->extconn.cs.controller,
4938 out->extconn.cs.stream) != 0)) {
4939 ALOGW("out_set_parameters() ignoring rerouting to non existing HDMI/DP");
4940 pthread_mutex_unlock(&adev->lock);
4941 pthread_mutex_unlock(&out->lock);
4942 ret = -ENOSYS;
4943 goto error;
4944 }
4945
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07004946 /*
4947 * select_devices() call below switches all the usecases on the same
4948 * backend to the new device. Refer to check_usecases_codec_backend() in
4949 * the select_devices(). But how do we undo this?
4950 *
4951 * For example, music playback is active on headset (deep-buffer usecase)
4952 * and if we go to ringtones and select a ringtone, low-latency usecase
4953 * will be started on headset+speaker. As we can't enable headset+speaker
4954 * and headset devices at the same time, select_devices() switches the music
4955 * playback to headset+speaker while starting low-lateny usecase for ringtone.
4956 * So when the ringtone playback is completed, how do we undo the same?
4957 *
4958 * We are relying on the out_set_parameters() call on deep-buffer output,
4959 * once the ringtone playback is ended.
4960 * NOTE: We should not check if the current devices are same as new devices.
4961 * Because select_devices() must be called to switch back the music
4962 * playback to headset.
4963 */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08004964 if (!list_empty(&new_devices)) {
4965 bool same_dev = compare_devices(&out->device_list, &new_devices);
4966 assign_devices(&out->device_list, &new_devices);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07004967
4968 if (output_drives_call(adev, out)) {
4969 if (!voice_is_call_state_active(adev)) {
4970 if (adev->mode == AUDIO_MODE_IN_CALL) {
4971 adev->current_call_output = out;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07004972 ret = voice_start_call(adev);
4973 }
4974 } else {
4975 adev->current_call_output = out;
4976 voice_update_devices_for_all_voice_usecases(adev);
4977 }
4978 }
4979
Mingshu Pang60536d72020-09-09 15:28:22 +08004980 if (is_usb_out_device_type(&out->device_list)) {
4981 service_interval = audio_extn_usb_find_service_interval(false, true /*playback*/);
4982 audio_extn_usb_set_service_interval(true /*playback*/,
4983 service_interval,
4984 &reconfig);
4985 ALOGD("%s, svc_int(%ld),reconfig(%d)",__func__,service_interval, reconfig);
4986 }
4987
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07004988 if (!out->standby) {
4989 if (!same_dev) {
4990 ALOGV("update routing change");
4991 audio_extn_perf_lock_acquire(&adev->perf_lock_handle, 0,
4992 adev->perf_lock_opts,
4993 adev->perf_lock_opts_size);
4994 if (adev->adm_on_routing_change)
4995 adev->adm_on_routing_change(adev->adm_data,
4996 out->handle);
4997 }
4998 if (!bypass_a2dp) {
4999 select_devices(adev, out->usecase);
5000 } else {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005001 if (compare_device_type(&new_devices, AUDIO_DEVICE_OUT_SPEAKER_SAFE))
5002 reassign_device_list(&out->device_list, AUDIO_DEVICE_OUT_SPEAKER_SAFE, "");
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005003 else
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005004 reassign_device_list(&out->device_list, AUDIO_DEVICE_OUT_SPEAKER, "");
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005005 select_devices(adev, out->usecase);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005006 assign_devices(&out->device_list, &new_devices);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005007 }
5008
5009 if (!same_dev) {
5010 // on device switch force swap, lower functions will make sure
5011 // to check if swap is allowed or not.
5012 platform_set_swap_channels(adev, true);
5013 audio_extn_perf_lock_release(&adev->perf_lock_handle);
5014 }
5015 if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005016 (!is_a2dp_out_device_type(&out->device_list) || audio_extn_a2dp_source_is_ready())) {
Weiyin Jiangd5974e62020-09-08 20:28:22 +08005017 pthread_mutex_lock(&out->latch_lock);
5018 if (out->a2dp_compress_mute) {
5019 out->a2dp_compress_mute = false;
5020 out_set_compr_volume(&out->stream, out->volume_l, out->volume_r);
5021 }
5022 pthread_mutex_unlock(&out->latch_lock);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005023 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP) {
5024 out_set_voip_volume(&out->stream, out->volume_l, out->volume_r);
5025 }
5026 }
5027 }
5028
5029 pthread_mutex_unlock(&adev->lock);
5030 pthread_mutex_unlock(&out->lock);
5031
5032 /*handles device and call state changes*/
5033 audio_extn_extspk_update(adev->extspk);
5034
5035error:
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005036 ALOGV("%s: exit: code(%d)", __func__, ret);
5037 return ret;
5038}
5039
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005040static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
5041{
5042 struct stream_out *out = (struct stream_out *)stream;
5043 struct audio_device *adev = out->dev;
5044 struct str_parms *parms;
5045 char value[32];
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07005046 int ret = 0, err;
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07005047 int ext_controller = -1;
5048 int ext_stream = -1;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005049
sangwoobc677242013-08-08 16:53:43 +09005050 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07005051 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005052 parms = str_parms_create_str(kvpairs);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05305053 if (!parms)
5054 goto error;
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07005055
Daniel Hillenbrand21752f02013-05-23 10:10:00 +05305056 amplifier_out_set_parameters(parms);
5057
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07005058 err = platform_get_controller_stream_from_params(parms, &ext_controller,
5059 &ext_stream);
Vignesh Kulothungan1b3957e2019-12-02 16:16:51 -08005060 if (err == 0) {
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07005061 out->extconn.cs.controller = ext_controller;
5062 out->extconn.cs.stream = ext_stream;
5063 ALOGD("%s: usecase(%s) new controller/stream (%d/%d)", __func__,
5064 use_case_table[out->usecase], out->extconn.cs.controller,
5065 out->extconn.cs.stream);
5066 }
5067
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07005068 if (out == adev->primary_output) {
5069 pthread_mutex_lock(&adev->lock);
5070 audio_extn_set_parameters(adev, parms);
5071 pthread_mutex_unlock(&adev->lock);
5072 }
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07005073 if (is_offload_usecase(out->usecase)) {
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005074 lock_output_stream(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005075 parse_compress_metadata(out, parms);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08005076
5077 audio_extn_dts_create_state_notifier_node(out->usecase);
5078 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
5079 popcount(out->channel_mask),
5080 out->playback_started);
5081
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08005082 pthread_mutex_unlock(&out->lock);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005083 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07005084
Surendar Karkaf51b5842018-04-26 11:28:38 +05305085 err = str_parms_get_str(parms, AUDIO_PARAMETER_DUAL_MONO, value,
5086 sizeof(value));
5087 if (err >= 0) {
5088 if (!strncmp("true", value, sizeof("true")) || atoi(value))
5089 audio_extn_send_dual_mono_mixing_coefficients(out);
5090 }
5091
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +05305092 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_PROFILE, value, sizeof(value));
5093 if (err >= 0) {
5094 strlcpy(out->profile, value, sizeof(out->profile));
5095 ALOGV("updating stream profile with value '%s'", out->profile);
5096 lock_output_stream(out);
5097 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
5098 &adev->streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005099 &out->device_list, out->flags,
5100 out->hal_op_format,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +05305101 out->sample_rate, out->bit_width,
5102 out->channel_mask, out->profile,
5103 &out->app_type_cfg);
5104 pthread_mutex_unlock(&out->lock);
5105 }
5106
Alexy Joseph98988832017-01-13 14:56:59 -08005107 //suspend, resume handling block
Aniket Kumar Lata932f4872017-11-06 18:29:44 -08005108 //remove QOS only if vendor.audio.hal.dynamic.qos.config.supported is set to true
5109 // and vendor.audio.hal.output.suspend.supported is set to true
5110 if (out->hal_output_suspend_supported && out->dynamic_pm_qos_config_supported) {
Alexy Joseph98988832017-01-13 14:56:59 -08005111 //check suspend parameter only for low latency and if the property
5112 //is enabled
5113 if (str_parms_get_str(parms, "suspend_playback", value, sizeof(value)) >= 0) {
5114 ALOGI("%s: got suspend_playback %s", __func__, value);
5115 lock_output_stream(out);
5116 if (!strncmp(value, "false", 5)) {
5117 //suspend_playback=false is supposed to set QOS value back to 75%
5118 //the mixer control sent with value Enable will achieve that
5119 ret = audio_route_apply_and_update_path(adev->audio_route, out->pm_qos_mixer_path);
5120 } else if (!strncmp (value, "true", 4)) {
5121 //suspend_playback=true is supposed to remove QOS value
5122 //resetting the mixer control will set the default value
5123 //for the mixer control which is Disable and this removes the QOS vote
5124 ret = audio_route_reset_and_update_path(adev->audio_route, out->pm_qos_mixer_path);
5125 } else {
5126 ALOGE("%s: Wrong value sent for suspend_playback, expected true/false,"
5127 " got %s", __func__, value);
5128 ret = -1;
5129 }
5130
5131 if (ret != 0) {
5132 ALOGE("%s: %s mixer ctl failed with %d, ignore suspend/resume setparams",
5133 __func__, out->pm_qos_mixer_path, ret);
5134 }
5135
5136 pthread_mutex_unlock(&out->lock);
5137 }
5138 }
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07005139
Alexy Joseph98988832017-01-13 14:56:59 -08005140 //end suspend, resume handling block
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005141 str_parms_destroy(parms);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05305142error:
Eric Laurent994a6932013-07-17 11:51:42 -07005143 ALOGV("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005144 return ret;
5145}
5146
Paul McLeana50b7332018-12-17 08:24:21 -07005147static int in_set_microphone_direction(const struct audio_stream_in *stream,
5148 audio_microphone_direction_t dir) {
justinweng20fb6d82019-02-21 18:49:00 -07005149 struct stream_in *in = (struct stream_in *)stream;
5150
5151 ALOGVV("%s: standby %d source %d dir %d", __func__, in->standby, in->source, dir);
5152
5153 in->direction = dir;
5154
5155 if (in->standby)
5156 return 0;
5157
5158 return audio_extn_audiozoom_set_microphone_direction(in, dir);
Paul McLeana50b7332018-12-17 08:24:21 -07005159}
5160
5161static int in_set_microphone_field_dimension(const struct audio_stream_in *stream, float zoom) {
justinweng20fb6d82019-02-21 18:49:00 -07005162 struct stream_in *in = (struct stream_in *)stream;
5163
5164 ALOGVV("%s: standby %d source %d zoom %f", __func__, in->standby, in->source, zoom);
5165
5166 if (zoom > 1.0 || zoom < -1.0)
5167 return -EINVAL;
5168
5169 in->zoom = zoom;
5170
5171 if (in->standby)
5172 return 0;
5173
5174 return audio_extn_audiozoom_set_microphone_field_dimension(in, zoom);
Paul McLeana50b7332018-12-17 08:24:21 -07005175}
5176
5177
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005178static bool stream_get_parameter_channels(struct str_parms *query,
5179 struct str_parms *reply,
5180 audio_channel_mask_t *supported_channel_masks) {
5181 int ret = -1;
5182 char value[512];
5183 bool first = true;
5184 size_t i, j;
5185
5186 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) {
5187 ret = 0;
5188 value[0] = '\0';
5189 i = 0;
5190 while (supported_channel_masks[i] != 0) {
5191 for (j = 0; j < ARRAY_SIZE(channels_name_to_enum_table); j++) {
5192 if (channels_name_to_enum_table[j].value == supported_channel_masks[i]) {
5193 if (!first)
Satya Krishna Pindiprolib22ac722017-10-09 15:44:16 +05305194 strlcat(value, "|", sizeof(value));
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005195
Satya Krishna Pindiprolib22ac722017-10-09 15:44:16 +05305196 strlcat(value, channels_name_to_enum_table[j].name, sizeof(value));
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005197 first = false;
5198 break;
5199 }
5200 }
5201 i++;
5202 }
5203 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
5204 }
5205 return ret == 0;
5206}
5207
5208static bool stream_get_parameter_formats(struct str_parms *query,
5209 struct str_parms *reply,
5210 audio_format_t *supported_formats) {
5211 int ret = -1;
5212 char value[256];
5213 size_t i, j;
5214 bool first = true;
5215
5216 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) {
5217 ret = 0;
5218 value[0] = '\0';
5219 i = 0;
5220 while (supported_formats[i] != 0) {
5221 for (j = 0; j < ARRAY_SIZE(formats_name_to_enum_table); j++) {
5222 if (formats_name_to_enum_table[j].value == supported_formats[i]) {
5223 if (!first) {
Satya Krishna Pindiprolib22ac722017-10-09 15:44:16 +05305224 strlcat(value, "|", sizeof(value));
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005225 }
Satya Krishna Pindiprolib22ac722017-10-09 15:44:16 +05305226 strlcat(value, formats_name_to_enum_table[j].name, sizeof(value));
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005227 first = false;
5228 break;
5229 }
5230 }
5231 i++;
5232 }
5233 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_FORMATS, value);
5234 }
5235 return ret == 0;
5236}
5237
5238static bool stream_get_parameter_rates(struct str_parms *query,
5239 struct str_parms *reply,
5240 uint32_t *supported_sample_rates) {
5241
5242 int i;
5243 char value[256];
5244 int ret = -1;
5245 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) {
5246 ret = 0;
5247 value[0] = '\0';
5248 i=0;
5249 int cursor = 0;
5250 while (supported_sample_rates[i]) {
5251 int avail = sizeof(value) - cursor;
5252 ret = snprintf(value + cursor, avail, "%s%d",
5253 cursor > 0 ? "|" : "",
5254 supported_sample_rates[i]);
5255 if (ret < 0 || ret >= avail) {
5256 // if cursor is at the last element of the array
5257 // overwrite with \0 is duplicate work as
5258 // snprintf already put a \0 in place.
5259 // else
5260 // we had space to write the '|' at value[cursor]
5261 // (which will be overwritten) or no space to fill
5262 // the first element (=> cursor == 0)
5263 value[cursor] = '\0';
5264 break;
5265 }
5266 cursor += ret;
5267 ++i;
5268 }
5269 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES,
5270 value);
5271 }
5272 return ret >= 0;
5273}
5274
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005275static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
5276{
5277 struct stream_out *out = (struct stream_out *)stream;
5278 struct str_parms *query = str_parms_create_str(keys);
Alexy Josephaee4fdd2016-01-29 13:02:07 -08005279 char *str = (char*) NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005280 char value[256];
5281 struct str_parms *reply = str_parms_create();
5282 size_t i, j;
5283 int ret;
5284 bool first = true;
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07005285
5286 if (!query || !reply) {
Alexy Josephaee4fdd2016-01-29 13:02:07 -08005287 if (reply) {
5288 str_parms_destroy(reply);
5289 }
5290 if (query) {
5291 str_parms_destroy(query);
5292 }
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07005293 ALOGE("out_get_parameters: failed to allocate mem for query or reply");
5294 return NULL;
5295 }
5296
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005297 ALOGV("%s: %s enter: keys - %s", __func__, use_case_table[out->usecase], keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005298 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
5299 if (ret >= 0) {
5300 value[0] = '\0';
5301 i = 0;
5302 while (out->supported_channel_masks[i] != 0) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005303 for (j = 0; j < ARRAY_SIZE(channels_name_to_enum_table); j++) {
5304 if (channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005305 if (!first) {
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -08005306 strlcat(value, "|", sizeof(value));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005307 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005308 strlcat(value, channels_name_to_enum_table[j].name, sizeof(value));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005309 first = false;
5310 break;
5311 }
5312 }
5313 i++;
5314 }
5315 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
5316 str = str_parms_to_str(reply);
5317 } else {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08005318 voice_extn_out_get_parameters(out, query, reply);
5319 str = str_parms_to_str(reply);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005320 }
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07005321
Alexy Joseph62142aa2015-11-16 15:10:34 -08005322
5323 ret = str_parms_get_str(query, "is_direct_pcm_track", value, sizeof(value));
5324 if (ret >= 0) {
5325 value[0] = '\0';
Dhananjay Kumarac341582017-02-23 23:42:25 +05305326 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
5327 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Alexy Joseph62142aa2015-11-16 15:10:34 -08005328 ALOGV("in direct_pcm");
Satya Krishna Pindiprolib6655542017-07-03 19:38:19 +05305329 strlcat(value, "true", sizeof(value));
Alexy Joseph62142aa2015-11-16 15:10:34 -08005330 } else {
5331 ALOGV("not in direct_pcm");
Sharad Sangle3dd5a4a2015-12-10 18:39:17 +05305332 strlcat(value, "false", sizeof(value));
Alexy Joseph62142aa2015-11-16 15:10:34 -08005333 }
5334 str_parms_add_str(reply, "is_direct_pcm_track", value);
Alexy Josephaee4fdd2016-01-29 13:02:07 -08005335 if (str)
5336 free(str);
Alexy Joseph62142aa2015-11-16 15:10:34 -08005337 str = str_parms_to_str(reply);
5338 }
5339
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07005340 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS, value, sizeof(value));
5341 if (ret >= 0) {
5342 value[0] = '\0';
5343 i = 0;
5344 first = true;
5345 while (out->supported_formats[i] != 0) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005346 for (j = 0; j < ARRAY_SIZE(formats_name_to_enum_table); j++) {
5347 if (formats_name_to_enum_table[j].value == out->supported_formats[i]) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07005348 if (!first) {
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -08005349 strlcat(value, "|", sizeof(value));
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07005350 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005351 strlcat(value, formats_name_to_enum_table[j].name, sizeof(value));
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07005352 first = false;
5353 break;
5354 }
5355 }
5356 i++;
5357 }
5358 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_FORMATS, value);
Alexy Josephaee4fdd2016-01-29 13:02:07 -08005359 if (str)
5360 free(str);
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07005361 str = str_parms_to_str(reply);
5362 }
Mingming Yin3a941d42016-02-17 18:08:05 -08005363
5364 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES, value, sizeof(value));
5365 if (ret >= 0) {
5366 value[0] = '\0';
5367 i = 0;
5368 first = true;
5369 while (out->supported_sample_rates[i] != 0) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005370 for (j = 0; j < ARRAY_SIZE(out_sample_rates_name_to_enum_table); j++) {
5371 if (out_sample_rates_name_to_enum_table[j].value == out->supported_sample_rates[i]) {
Mingming Yin3a941d42016-02-17 18:08:05 -08005372 if (!first) {
5373 strlcat(value, "|", sizeof(value));
5374 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005375 strlcat(value, out_sample_rates_name_to_enum_table[j].name, sizeof(value));
Mingming Yin3a941d42016-02-17 18:08:05 -08005376 first = false;
5377 break;
5378 }
5379 }
5380 i++;
5381 }
5382 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES, value);
5383 if (str)
5384 free(str);
5385 str = str_parms_to_str(reply);
5386 }
5387
Alexy Joseph98988832017-01-13 14:56:59 -08005388 if (str_parms_get_str(query, "supports_hw_suspend", value, sizeof(value)) >= 0) {
5389 //only low latency track supports suspend_resume
5390 str_parms_add_int(reply, "supports_hw_suspend",
Aniket Kumar Lata932f4872017-11-06 18:29:44 -08005391 (out->hal_output_suspend_supported));
Alexy Joseph98988832017-01-13 14:56:59 -08005392 if (str)
5393 free(str);
5394 str = str_parms_to_str(reply);
5395 }
5396
5397
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005398 str_parms_destroy(query);
5399 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07005400 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005401 return str;
5402}
5403
5404static uint32_t out_get_latency(const struct audio_stream_out *stream)
5405{
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005406 uint32_t period_ms;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005407 struct stream_out *out = (struct stream_out *)stream;
Alexy Josephaa54c872014-12-03 02:46:47 -08005408 uint32_t latency = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005409
Alexy Josephaa54c872014-12-03 02:46:47 -08005410 if (is_offload_usecase(out->usecase)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05305411 lock_output_stream(out);
5412 latency = audio_extn_utils_compress_get_dsp_latency(out);
5413 pthread_mutex_unlock(&out->lock);
Haynes Mathew George16081042017-05-31 17:16:49 -07005414 } else if ((out->realtime) ||
5415 (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP)) {
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005416 // since the buffer won't be filled up faster than realtime,
5417 // return a smaller number
5418 if (out->config.rate)
5419 period_ms = (out->af_period_multiplier * out->config.period_size *
5420 1000) / (out->config.rate);
5421 else
5422 period_ms = 0;
George Gao9ba8a142020-07-23 14:30:03 -07005423 latency = period_ms + platform_render_latency(out) / 1000;
Alexy Josephaa54c872014-12-03 02:46:47 -08005424 } else {
5425 latency = (out->config.period_count * out->config.period_size * 1000) /
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005426 (out->config.rate);
Alexy Josephaa54c872014-12-03 02:46:47 -08005427 }
5428
Zhou Songd2537a02020-06-11 22:04:46 +08005429 if (!out->standby && is_a2dp_out_device_type(&out->device_list))
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08005430 latency += audio_extn_a2dp_get_encoder_latency();
5431
Anish Kumar50ebcbf2014-12-09 04:01:39 +05305432 ALOGV("%s: Latency %d", __func__, latency);
Alexy Josephaa54c872014-12-03 02:46:47 -08005433 return latency;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005434}
5435
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05305436static float AmpToDb(float amplification)
5437{
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05305438 float db = DSD_VOLUME_MIN_DB;
5439 if (amplification > 0) {
5440 db = 20 * log10(amplification);
5441 if(db < DSD_VOLUME_MIN_DB)
5442 return DSD_VOLUME_MIN_DB;
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05305443 }
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05305444 return db;
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05305445}
5446
Arun Mirpuri5d170872019-03-26 13:21:31 -07005447static int out_set_mmap_volume(struct audio_stream_out *stream, float left,
5448 float right)
5449{
5450 struct stream_out *out = (struct stream_out *)stream;
5451 long volume = 0;
5452 char mixer_ctl_name[128] = "";
5453 struct audio_device *adev = out->dev;
5454 struct mixer_ctl *ctl = NULL;
5455 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
5456 PCM_PLAYBACK);
5457
5458 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
5459 "Playback %d Volume", pcm_device_id);
5460 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
5461 if (!ctl) {
5462 ALOGE("%s: Could not get ctl for mixer cmd - %s",
5463 __func__, mixer_ctl_name);
5464 return -EINVAL;
5465 }
5466 if (left != right)
5467 ALOGW("%s: Left and right channel volume mismatch:%f,%f",
5468 __func__, left, right);
5469 volume = (long)(left * (MMAP_PLAYBACK_VOLUME_MAX*1.0));
5470 if (mixer_ctl_set_value(ctl, 0, volume) < 0){
5471 ALOGE("%s:ctl for mixer cmd - %s, volume %ld returned error",
5472 __func__, mixer_ctl_name, volume);
5473 return -EINVAL;
5474 }
5475 return 0;
5476}
5477
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05305478static int out_set_compr_volume(struct audio_stream_out *stream, float left,
5479 float right)
5480{
5481 struct stream_out *out = (struct stream_out *)stream;
Manish Dewangan338c50a2017-09-12 15:22:03 +05305482 long volume[2];
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05305483 char mixer_ctl_name[128];
5484 struct audio_device *adev = out->dev;
5485 struct mixer_ctl *ctl;
5486 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
5487 PCM_PLAYBACK);
5488
5489 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
5490 "Compress Playback %d Volume", pcm_device_id);
5491 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
5492 if (!ctl) {
5493 ALOGE("%s: Could not get ctl for mixer cmd - %s",
5494 __func__, mixer_ctl_name);
5495 return -EINVAL;
5496 }
5497 ALOGE("%s:ctl for mixer cmd - %s, left %f, right %f",
5498 __func__, mixer_ctl_name, left, right);
5499 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
5500 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
5501 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
5502
5503 return 0;
5504}
5505
Zhou Song2b8f28f2017-09-11 10:51:38 +08005506static int out_set_voip_volume(struct audio_stream_out *stream, float left,
5507 float right)
5508{
5509 struct stream_out *out = (struct stream_out *)stream;
5510 char mixer_ctl_name[] = "App Type Gain";
5511 struct audio_device *adev = out->dev;
5512 struct mixer_ctl *ctl;
Manish Dewangan338c50a2017-09-12 15:22:03 +05305513 long set_values[4];
Zhou Song2b8f28f2017-09-11 10:51:38 +08005514
Aniket Kumar Lata8426d1f2019-06-10 15:25:53 -07005515 if (!is_valid_volume(left, right)) {
5516 ALOGE("%s: Invalid stream volume for left=%f, right=%f",
5517 __func__, left, right);
5518 return -EINVAL;
5519 }
5520
Zhou Song2b8f28f2017-09-11 10:51:38 +08005521 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
5522 if (!ctl) {
5523 ALOGE("%s: Could not get ctl for mixer cmd - %s",
5524 __func__, mixer_ctl_name);
5525 return -EINVAL;
5526 }
5527
5528 set_values[0] = 0; //0: Rx Session 1:Tx Session
5529 set_values[1] = out->app_type_cfg.app_type;
Manish Dewangan338c50a2017-09-12 15:22:03 +05305530 set_values[2] = (long)(left * VOIP_PLAYBACK_VOLUME_MAX);
5531 set_values[3] = (long)(right * VOIP_PLAYBACK_VOLUME_MAX);
Zhou Song2b8f28f2017-09-11 10:51:38 +08005532
5533 mixer_ctl_set_array(ctl, set_values, ARRAY_SIZE(set_values));
5534 return 0;
5535}
5536
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05305537static int out_set_pcm_volume(struct audio_stream_out *stream, float left,
5538 float right)
5539{
5540 struct stream_out *out = (struct stream_out *)stream;
5541 /* Volume control for pcm playback */
5542 if (left != right) {
5543 return -EINVAL;
5544 } else {
5545 char mixer_ctl_name[128];
5546 struct audio_device *adev = out->dev;
5547 struct mixer_ctl *ctl;
5548 int pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
5549 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name), "Playback %d Volume", pcm_device_id);
5550 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
5551 if (!ctl) {
5552 ALOGE("%s : Could not get ctl for mixer cmd - %s", __func__, mixer_ctl_name);
5553 return -EINVAL;
5554 }
5555
5556 int volume = (int) (left * PCM_PLAYBACK_VOLUME_MAX);
5557 int ret = mixer_ctl_set_value(ctl, 0, volume);
5558 if (ret < 0) {
5559 ALOGE("%s: Could not set ctl, error:%d ", __func__, ret);
5560 return -EINVAL;
5561 }
5562
5563 ALOGV("%s : Pcm set volume value %d left %f", __func__, volume, left);
5564
5565 return 0;
5566 }
5567}
5568
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005569static int out_set_volume(struct audio_stream_out *stream, float left,
5570 float right)
5571{
Eric Laurenta9024de2013-04-04 09:19:12 -07005572 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005573 int volume[2];
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05305574 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005575
Arun Mirpuri5d170872019-03-26 13:21:31 -07005576 ALOGD("%s: called with left_vol=%f, right_vol=%f", __func__, left, right);
Eric Laurenta9024de2013-04-04 09:19:12 -07005577 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
5578 /* only take left channel into account: the API is for stereo anyway */
Weiyin Jiangd5974e62020-09-08 20:28:22 +08005579 pthread_mutex_lock(&out->latch_lock);
Eric Laurenta9024de2013-04-04 09:19:12 -07005580 out->muted = (left == 0.0f);
Weiyin Jiangd5974e62020-09-08 20:28:22 +08005581 pthread_mutex_unlock(&out->latch_lock);
Eric Laurenta9024de2013-04-04 09:19:12 -07005582 return 0;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07005583 } else if (is_offload_usecase(out->usecase)) {
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05305584 if (audio_extn_passthru_is_passthrough_stream(out)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07005585 /*
5586 * Set mute or umute on HDMI passthrough stream.
5587 * Only take left channel into account.
5588 * Mute is 0 and unmute 1
5589 */
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05305590 audio_extn_passthru_set_volume(out, (left == 0.0f));
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05305591 } else if (out->format == AUDIO_FORMAT_DSD){
5592 char mixer_ctl_name[128] = "DSD Volume";
5593 struct audio_device *adev = out->dev;
5594 struct mixer_ctl *ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
5595
5596 if (!ctl) {
5597 ALOGE("%s: Could not get ctl for mixer cmd - %s",
5598 __func__, mixer_ctl_name);
5599 return -EINVAL;
5600 }
Manish Dewangan338c50a2017-09-12 15:22:03 +05305601 volume[0] = (long)(AmpToDb(left));
5602 volume[1] = (long)(AmpToDb(right));
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05305603 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
5604 return 0;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005605 } else if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_BUS) &&
Derek Chendf05eea2019-08-01 13:57:49 -07005606 (out->car_audio_stream == CAR_AUDIO_STREAM_MEDIA)) {
Philippe Gravel1c9ac352019-05-28 16:08:37 -07005607 ALOGD("%s: Overriding offload set volume for media bus stream", __func__);
5608 struct listnode *node = NULL;
5609 list_for_each(node, &adev->active_outputs_list) {
5610 streams_output_ctxt_t *out_ctxt = node_to_item(node,
5611 streams_output_ctxt_t,
5612 list);
5613 if (out_ctxt->output->usecase == USECASE_AUDIO_PLAYBACK_MEDIA) {
5614 out->volume_l = out_ctxt->output->volume_l;
5615 out->volume_r = out_ctxt->output->volume_r;
5616 }
5617 }
Weiyin Jiangd5974e62020-09-08 20:28:22 +08005618 pthread_mutex_lock(&out->latch_lock);
Philippe Gravel1c9ac352019-05-28 16:08:37 -07005619 if (!out->a2dp_compress_mute) {
5620 ret = out_set_compr_volume(&out->stream, out->volume_l, out->volume_r);
5621 }
Weiyin Jiangd5974e62020-09-08 20:28:22 +08005622 pthread_mutex_unlock(&out->latch_lock);
Philippe Gravel1c9ac352019-05-28 16:08:37 -07005623 return ret;
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07005624 } else {
Weiyin Jiangd5974e62020-09-08 20:28:22 +08005625 pthread_mutex_lock(&out->latch_lock);
Arun Mirpuri5d170872019-03-26 13:21:31 -07005626 ALOGV("%s: compress mute %d", __func__, out->a2dp_compress_mute);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05305627 if (!out->a2dp_compress_mute)
5628 ret = out_set_compr_volume(stream, left, right);
5629 out->volume_l = left;
5630 out->volume_r = right;
Weiyin Jiangd5974e62020-09-08 20:28:22 +08005631 pthread_mutex_unlock(&out->latch_lock);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05305632 return ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005633 }
Vikram Panduranga93f080e2017-06-07 18:16:14 -07005634 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP) {
Aalique Grahame22e49102018-12-18 14:23:57 -08005635 out->app_type_cfg.gain[0] = (int)(left * VOIP_PLAYBACK_VOLUME_MAX);
5636 out->app_type_cfg.gain[1] = (int)(right * VOIP_PLAYBACK_VOLUME_MAX);
5637 if (!out->standby) {
5638 audio_extn_utils_send_app_type_gain(out->dev,
5639 out->app_type_cfg.app_type,
5640 &out->app_type_cfg.gain[0]);
Zhou Song2b8f28f2017-09-11 10:51:38 +08005641 ret = out_set_voip_volume(stream, left, right);
Aalique Grahame22e49102018-12-18 14:23:57 -08005642 }
Zhou Song2b8f28f2017-09-11 10:51:38 +08005643 out->volume_l = left;
5644 out->volume_r = right;
5645 return ret;
Arun Mirpuri5d170872019-03-26 13:21:31 -07005646 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
5647 ALOGV("%s: MMAP set volume called", __func__);
5648 if (!out->standby)
5649 ret = out_set_mmap_volume(stream, left, right);
5650 out->volume_l = left;
5651 out->volume_r = right;
5652 return ret;
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05305653 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_LOW_LATENCY ||
Ramu Gottipati36547092018-12-28 11:32:09 +05305654 out->usecase == USECASE_AUDIO_PLAYBACK_DEEP_BUFFER ||
5655 out->usecase == USECASE_AUDIO_PLAYBACK_ULL) {
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05305656 /* Volume control for pcm playback */
5657 if (!out->standby)
5658 ret = out_set_pcm_volume(stream, left, right);
5659 else
5660 out->apply_volume = true;
5661
5662 out->volume_l = left;
5663 out->volume_r = right;
5664 return ret;
Derek Chenf13dd492018-11-13 14:53:51 -08005665 } else if (audio_extn_auto_hal_is_bus_device_usecase(out->usecase)) {
5666 ALOGV("%s: bus device set volume called", __func__);
5667 if (!out->standby)
5668 ret = out_set_pcm_volume(stream, left, right);
5669 out->volume_l = left;
5670 out->volume_r = right;
5671 return ret;
Eric Laurenta9024de2013-04-04 09:19:12 -07005672 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005673
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005674 return -ENOSYS;
5675}
5676
Zhou Songc9672822017-08-16 16:01:39 +08005677static void update_frames_written(struct stream_out *out, size_t bytes)
5678{
5679 size_t bpf = 0;
5680
5681 if (is_offload_usecase(out->usecase) && !out->non_blocking &&
5682 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD))
5683 bpf = 1;
5684 else if (!is_offload_usecase(out->usecase))
5685 bpf = audio_bytes_per_sample(out->format) *
5686 audio_channel_count_from_out_mask(out->channel_mask);
Zhou Song48453a02018-01-10 17:50:59 +08005687
5688 pthread_mutex_lock(&out->position_query_lock);
5689 if (bpf != 0) {
Zhou Songc9672822017-08-16 16:01:39 +08005690 out->written += bytes / bpf;
Zhou Song48453a02018-01-10 17:50:59 +08005691 clock_gettime(CLOCK_MONOTONIC, &out->writeAt);
5692 }
5693 pthread_mutex_unlock(&out->position_query_lock);
Zhou Songc9672822017-08-16 16:01:39 +08005694}
5695
Vignesh Kulothungana6927272019-02-20 15:17:07 -08005696int split_and_write_audio_haptic_data(struct stream_out *out,
5697 const void *buffer, size_t bytes_to_write)
5698{
5699 struct audio_device *adev = out->dev;
5700
5701 int ret = 0;
5702 size_t channel_count = audio_channel_count_from_out_mask(out->channel_mask);
5703 size_t bytes_per_sample = audio_bytes_per_sample(out->format);
5704 size_t frame_size = channel_count * bytes_per_sample;
5705 size_t frame_count = bytes_to_write / frame_size;
5706
5707 bool force_haptic_path =
5708 property_get_bool("vendor.audio.test_haptic", false);
5709
5710 // extract Haptics data from Audio buffer
5711 bool alloc_haptic_buffer = false;
5712 int haptic_channel_count = adev->haptics_config.channels;
5713 size_t haptic_frame_size = bytes_per_sample * haptic_channel_count;
5714 size_t audio_frame_size = frame_size - haptic_frame_size;
5715 size_t total_haptic_buffer_size = frame_count * haptic_frame_size;
5716
5717 if (adev->haptic_buffer == NULL) {
5718 alloc_haptic_buffer = true;
5719 } else if (adev->haptic_buffer_size < total_haptic_buffer_size) {
5720 free(adev->haptic_buffer);
5721 adev->haptic_buffer_size = 0;
5722 alloc_haptic_buffer = true;
5723 }
5724
5725 if (alloc_haptic_buffer) {
5726 adev->haptic_buffer = (uint8_t *)calloc(1, total_haptic_buffer_size);
Mingshu Pang1513f972019-05-24 12:43:51 +08005727 if(adev->haptic_buffer == NULL) {
5728 ALOGE("%s: failed to allocate mem for dev->haptic_buffer", __func__);
5729 return -ENOMEM;
5730 }
Vignesh Kulothungana6927272019-02-20 15:17:07 -08005731 adev->haptic_buffer_size = total_haptic_buffer_size;
5732 }
5733
5734 size_t src_index = 0, aud_index = 0, hap_index = 0;
5735 uint8_t *audio_buffer = (uint8_t *)buffer;
5736 uint8_t *haptic_buffer = adev->haptic_buffer;
5737
5738 // This is required for testing only. This works for stereo data only.
5739 // One channel is fed to audio stream and other to haptic stream for testing.
5740 if (force_haptic_path)
5741 audio_frame_size = haptic_frame_size = bytes_per_sample;
5742
5743 for (size_t i = 0; i < frame_count; i++) {
5744 memcpy(audio_buffer + aud_index, audio_buffer + src_index,
5745 audio_frame_size);
5746 aud_index += audio_frame_size;
5747 src_index += audio_frame_size;
5748
5749 if (adev->haptic_pcm)
5750 memcpy(haptic_buffer + hap_index, audio_buffer + src_index,
5751 haptic_frame_size);
5752 hap_index += haptic_frame_size;
5753 src_index += haptic_frame_size;
5754
5755 // This is required for testing only.
5756 // Discard haptic channel data.
5757 if (force_haptic_path)
5758 src_index += haptic_frame_size;
5759 }
5760
5761 // write to audio pipeline
5762 ret = pcm_write(out->pcm, (void *)audio_buffer,
5763 frame_count * audio_frame_size);
5764
5765 // write to haptics pipeline
5766 if (adev->haptic_pcm)
5767 ret = pcm_write(adev->haptic_pcm, (void *)adev->haptic_buffer,
5768 frame_count * haptic_frame_size);
5769
5770 return ret;
5771}
5772
Aalique Grahame22e49102018-12-18 14:23:57 -08005773#ifdef NO_AUDIO_OUT
5774static ssize_t out_write_for_no_output(struct audio_stream_out *stream,
5775 const void *buffer __unused, size_t bytes)
5776{
5777 struct stream_out *out = (struct stream_out *)stream;
5778
5779 /* No Output device supported other than BT for playback.
5780 * Sleep for the amount of buffer duration
5781 */
5782 lock_output_stream(out);
5783 usleep(bytes * 1000000 / audio_stream_out_frame_size(
5784 (const struct audio_stream_out *)&out->stream) /
5785 out_get_sample_rate(&out->stream.common));
5786 pthread_mutex_unlock(&out->lock);
5787 return bytes;
5788}
5789#endif
5790
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005791static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
5792 size_t bytes)
5793{
5794 struct stream_out *out = (struct stream_out *)stream;
5795 struct audio_device *adev = out->dev;
Eric Laurent6e895242013-09-05 16:10:57 -07005796 ssize_t ret = 0;
Satish Babu Patakokila715b1422017-08-22 14:33:21 +05305797 int channels = 0;
Arun Mirpuri7da752a2018-09-11 18:01:15 -07005798 const size_t frame_size = audio_stream_out_frame_size(stream);
5799 const size_t frames = (frame_size != 0) ? bytes / frame_size : bytes;
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +05305800 struct audio_usecase *usecase = NULL;
Meng Wang4c32fb42020-01-16 17:57:11 +08005801 uint32_t compr_passthr = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005802
Haynes Mathew George380745d2017-10-04 15:27:45 -07005803 ATRACE_BEGIN("out_write");
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005804 lock_output_stream(out);
Naresh Tanniru4c630392014-05-12 01:05:52 +05305805
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305806 if (CARD_STATUS_OFFLINE == out->card_status) {
Zhou Song0b2e5dc2015-03-16 14:41:38 +08005807
Dhananjay Kumarac341582017-02-23 23:42:25 +05305808 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Ashish Jainbbce4322016-02-16 13:25:27 +05305809 /*during SSR for compress usecase we should return error to flinger*/
Naresh Tanniru80659832014-06-04 18:17:56 +05305810 ALOGD(" copl %s: sound card is not active/SSR state", __func__);
5811 pthread_mutex_unlock(&out->lock);
Haynes Mathew George380745d2017-10-04 15:27:45 -07005812 ATRACE_END();
Naresh Tanniru80659832014-06-04 18:17:56 +05305813 return -ENETRESET;
Ashish Jainbbce4322016-02-16 13:25:27 +05305814 } else {
Ashish Jainbbce4322016-02-16 13:25:27 +05305815 ALOGD(" %s: sound card is not active/SSR state", __func__);
5816 ret= -EIO;
5817 goto exit;
Naresh Tanniru4c630392014-05-12 01:05:52 +05305818 }
5819 }
5820
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05305821 if (audio_extn_passthru_should_drop_data(out)) {
Ashish Jaind84fd6a2016-07-27 12:33:25 +05305822 ALOGV(" %s : Drop data as compress passthrough session is going on", __func__);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05305823 ret = -EIO;
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05305824 goto exit;
5825 }
5826
Haynes Mathew George16081042017-05-31 17:16:49 -07005827 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
5828 ret = -EINVAL;
5829 goto exit;
5830 }
5831
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005832 if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_AUX_DIGITAL) &&
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05305833 !out->is_iec61937_info_available) {
5834
5835 if (!audio_extn_passthru_is_passthrough_stream(out)) {
5836 out->is_iec61937_info_available = true;
5837 } else if (audio_extn_passthru_is_enabled()) {
5838 audio_extn_passthru_update_stream_configuration(adev, out, buffer, bytes);
Manish Dewangan37864bc2017-06-09 12:28:37 +05305839 out->is_iec61937_info_available = true;
Manish Dewangan671a4202017-08-18 17:30:46 +05305840
5841 if((out->format == AUDIO_FORMAT_DTS) ||
5842 (out->format == AUDIO_FORMAT_DTS_HD)) {
5843 ret = audio_extn_passthru_update_dts_stream_configuration(out,
5844 buffer, bytes);
5845 if (ret) {
5846 if (ret != -ENOSYS) {
5847 out->is_iec61937_info_available = false;
5848 ALOGD("iec61937 transmission info not yet updated retry");
5849 }
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05305850 } else if (!out->standby) {
Manish Dewangan671a4202017-08-18 17:30:46 +05305851 /* if stream has started and after that there is
5852 * stream config change (iec transmission config)
5853 * then trigger select_device to update backend configuration.
5854 */
5855 out->stream_config_changed = true;
5856 pthread_mutex_lock(&adev->lock);
5857 select_devices(adev, out->usecase);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05305858 if (!audio_extn_passthru_is_supported_backend_edid_cfg(adev, out)) {
Weiyin Jiang29c08a42019-04-30 17:11:10 +08005859 pthread_mutex_unlock(&adev->lock);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05305860 ret = -EINVAL;
5861 goto exit;
5862 }
Manish Dewangan671a4202017-08-18 17:30:46 +05305863 pthread_mutex_unlock(&adev->lock);
5864 out->stream_config_changed = false;
5865 out->is_iec61937_info_available = true;
5866 }
5867 }
Satish Babu Patakokila715b1422017-08-22 14:33:21 +05305868
Meng Wang4c32fb42020-01-16 17:57:11 +08005869#ifdef AUDIO_GKI_ENABLED
5870 /* out->compr_config.codec->reserved[0] is for compr_passthr */
5871 compr_passthr = out->compr_config.codec->reserved[0];
5872#else
5873 compr_passthr = out->compr_config.codec->compr_passthr;
5874#endif
5875
Garmond Leung317cbf12017-09-13 16:20:50 -07005876 if ((channels < (int)audio_channel_count_from_out_mask(out->channel_mask)) &&
Meng Wang4c32fb42020-01-16 17:57:11 +08005877 (compr_passthr == PASSTHROUGH) &&
Satish Babu Patakokila715b1422017-08-22 14:33:21 +05305878 (out->is_iec61937_info_available == true)) {
5879 ALOGE("%s: ERROR: Unsupported channel config in passthrough mode", __func__);
5880 ret = -EINVAL;
5881 goto exit;
5882 }
Manish Dewangan37864bc2017-06-09 12:28:37 +05305883 }
5884 }
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05305885
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005886 if (is_a2dp_out_device_type(&out->device_list) &&
Florian Pfister1a84f312018-07-19 14:38:18 +02005887 (audio_extn_a2dp_source_is_suspended())) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005888 if (!(compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_SPEAKER) ||
5889 compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_SPEAKER_SAFE))) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05305890 if (!(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05305891 ret = -EIO;
5892 goto exit;
5893 }
5894 }
5895 }
5896
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005897 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07005898 out->standby = false;
Andy Hungc6bfd4a2019-07-01 18:26:00 -07005899 const int64_t startNs = systemTime(SYSTEM_TIME_MONOTONIC);
5900
Eric Laurent150dbfe2013-02-27 14:31:02 -08005901 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08005902 if (out->usecase == USECASE_COMPRESS_VOIP_CALL)
5903 ret = voice_extn_compress_voip_start_output_stream(out);
5904 else
5905 ret = start_output_stream(out);
Daniel Hillenbrand21752f02013-05-23 10:10:00 +05305906
5907 if (ret == 0)
5908 amplifier_output_stream_start(stream,
5909 is_offload_usecase(out->usecase));
5910
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005911 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005912 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07005913 out->standby = true;
Weiyin Jiangac2bae82020-07-29 17:23:53 +08005914 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005915 goto exit;
5916 }
Ashish Jain1b9b30c2017-05-18 20:57:40 +05305917 out->started = 1;
Andy Hunga1f48fa2019-07-01 18:14:53 -07005918 out->last_fifo_valid = false; // we're coming out of standby, last_fifo isn't valid.
Weiyin Jiangac2bae82020-07-29 17:23:53 +08005919
5920 if ((last_known_cal_step != -1) && (adev->platform != NULL)) {
vivek mehtab72d08d2016-04-29 03:16:47 -07005921 ALOGD("%s: retry previous failed cal level set", __func__);
Weiyin Jiangac2bae82020-07-29 17:23:53 +08005922 platform_send_gain_dep_cal(adev->platform, last_known_cal_step);
Preetam Singh Ranawatf4ae0222017-05-31 17:07:28 +05305923 last_known_cal_step = -1;
vivek mehtab72d08d2016-04-29 03:16:47 -07005924 }
Weiyin Jiangac2bae82020-07-29 17:23:53 +08005925 pthread_mutex_unlock(&adev->lock);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05305926
5927 if ((out->is_iec61937_info_available == true) &&
5928 (audio_extn_passthru_is_passthrough_stream(out))&&
5929 (!audio_extn_passthru_is_supported_backend_edid_cfg(adev, out))) {
5930 ret = -EINVAL;
5931 goto exit;
5932 }
Surendar Karkaf51b5842018-04-26 11:28:38 +05305933 if (out->set_dual_mono)
5934 audio_extn_send_dual_mono_mixing_coefficients(out);
Andy Hungc6bfd4a2019-07-01 18:26:00 -07005935
5936 // log startup time in ms.
5937 simple_stats_log(
5938 &out->start_latency_ms, (systemTime(SYSTEM_TIME_MONOTONIC) - startNs) * 1e-6);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005939 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005940
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08005941 if (adev->is_channel_status_set == false &&
5942 compare_device_type(&out->device_list,
5943 AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
Alexy Josephb1379942016-01-29 15:49:38 -08005944 audio_utils_set_hdmi_channel_status(out, (void *)buffer, bytes);
Ashish Jain81eb2a82015-05-13 10:52:34 +05305945 adev->is_channel_status_set = true;
5946 }
5947
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +05305948 if ((adev->use_old_pspd_mix_ctrl == true) &&
5949 (out->pspd_coeff_sent == false)) {
5950 /*
5951 * Need to resend pspd coefficients after stream started for
5952 * older kernel version as it does not save the coefficients
5953 * and also stream has to be started for coeff to apply.
5954 */
5955 usecase = get_usecase_from_list(adev, out->usecase);
5956 if (usecase != NULL) {
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05305957 audio_extn_set_custom_mtmx_params_v2(adev, usecase, true);
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +05305958 out->pspd_coeff_sent = true;
5959 }
5960 }
5961
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07005962 if (is_offload_usecase(out->usecase)) {
Alexy Joseph01e54e62015-03-03 19:01:03 -08005963 ALOGVV("copl(%p): writing buffer (%zu bytes) to compress device", out, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005964 if (out->send_new_metadata) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07005965 ALOGD("copl(%p):send new gapless metadata", out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005966 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
5967 out->send_new_metadata = 0;
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05305968 if (out->send_next_track_params && out->is_compr_metadata_avail) {
5969 ALOGD("copl(%p):send next track params in gapless", out);
Weiyin Jiangd9b5a4e2019-07-18 17:24:21 +08005970 compress_set_next_track_param(out->compr, &(out->compr_config.codec->options));
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05305971 out->send_next_track_params = false;
5972 out->is_compr_metadata_avail = false;
5973 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005974 }
Dhananjay Kumarac341582017-02-23 23:42:25 +05305975 if (!(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
Ashish Jain83a6cc22016-06-28 14:34:17 +05305976 (out->convert_buffer) != NULL) {
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005977
Ashish Jain83a6cc22016-06-28 14:34:17 +05305978 if ((bytes > out->hal_fragment_size)) {
Ashish Jainf1eaa582016-05-23 20:54:24 +05305979 ALOGW("Error written bytes %zu > %d (fragment_size)",
Ashish Jain83a6cc22016-06-28 14:34:17 +05305980 bytes, out->hal_fragment_size);
Ashish Jainf1eaa582016-05-23 20:54:24 +05305981 pthread_mutex_unlock(&out->lock);
Haynes Mathew George380745d2017-10-04 15:27:45 -07005982 ATRACE_END();
Ashish Jainf1eaa582016-05-23 20:54:24 +05305983 return -EINVAL;
5984 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05305985 audio_format_t dst_format = out->hal_op_format;
5986 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05305987
Dieter Luecking5d57def2018-09-07 14:23:37 +02005988 /* prevent division-by-zero */
5989 uint32_t bitwidth_src = format_to_bitwidth_table[src_format];
5990 uint32_t bitwidth_dst = format_to_bitwidth_table[dst_format];
5991 if ((bitwidth_src == 0) || (bitwidth_dst == 0)) {
5992 ALOGE("%s: Error bitwidth == 0", __func__);
Ramu Gottipati02809682018-12-19 16:46:12 +05305993 pthread_mutex_unlock(&out->lock);
Dieter Luecking5d57def2018-09-07 14:23:37 +02005994 ATRACE_END();
5995 return -EINVAL;
5996 }
5997
Ashish Jainf1eaa582016-05-23 20:54:24 +05305998 uint32_t frames = bytes / format_to_bitwidth_table[src_format];
5999 uint32_t bytes_to_write = frames * format_to_bitwidth_table[dst_format];
6000
Ashish Jain83a6cc22016-06-28 14:34:17 +05306001 memcpy_by_audio_format(out->convert_buffer,
Ashish Jainf1eaa582016-05-23 20:54:24 +05306002 dst_format,
6003 buffer,
6004 src_format,
6005 frames);
6006
Ashish Jain83a6cc22016-06-28 14:34:17 +05306007 ret = compress_write(out->compr, out->convert_buffer,
Ashish Jainf1eaa582016-05-23 20:54:24 +05306008 bytes_to_write);
6009
6010 /*Convert written bytes in audio flinger format*/
6011 if (ret > 0)
6012 ret = ((ret * format_to_bitwidth_table[out->format]) /
6013 format_to_bitwidth_table[dst_format]);
6014 }
6015 } else
6016 ret = compress_write(out->compr, buffer, bytes);
6017
Zhou Songc9672822017-08-16 16:01:39 +08006018 if ((ret < 0 || ret == (ssize_t)bytes) && !out->non_blocking)
6019 update_frames_written(out, bytes);
6020
Dhanalakshmi Siddani37ca1d62014-08-20 12:28:34 +05306021 if (ret < 0)
6022 ret = -errno;
Weiyin Jiangcc60dbb2018-08-21 13:12:03 +08006023 ALOGVV("%s: writing buffer (%zu bytes) to compress device returned %d", __func__, bytes, (int)ret);
Ashish Jainb26edfb2016-08-25 00:10:11 +05306024 /*msg to cb thread only if non blocking write is enabled*/
6025 if (ret >= 0 && ret < (ssize_t)bytes && out->non_blocking) {
Sidipotu Ashok55820562014-02-10 16:16:38 +05306026 ALOGD("No space available in compress driver, post msg to cb thread");
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006027 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
Naresh Tanniru80659832014-06-04 18:17:56 +05306028 } else if (-ENETRESET == ret) {
6029 ALOGE("copl %s: received sound card offline state on compress write", __func__);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306030 out->card_status = CARD_STATUS_OFFLINE;
Naresh Tanniru80659832014-06-04 18:17:56 +05306031 pthread_mutex_unlock(&out->lock);
Dhananjay Kumar1248dd82017-07-28 21:22:16 +05306032 out_on_error(&out->stream.common);
Haynes Mathew George380745d2017-10-04 15:27:45 -07006033 ATRACE_END();
Naresh Tanniru80659832014-06-04 18:17:56 +05306034 return ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006035 }
Ashish Jain5106d362016-05-11 19:23:33 +05306036
Dhanalakshmi Siddania6b76c72016-09-09 18:10:31 +05306037 /* Call compr start only when non-zero bytes of data is there to be rendered */
6038 if (!out->playback_started && ret > 0) {
6039 int status = compress_start(out->compr);
6040 if (status < 0) {
6041 ret = status;
6042 ALOGE("%s: compr start failed with err %d", __func__, errno);
6043 goto exit;
6044 }
Alexy Joseph7de344d2015-03-30 10:40:03 -07006045 audio_extn_dts_eagle_fade(adev, true, out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006046 out->playback_started = 1;
Weiyin Jiangd5974e62020-09-08 20:28:22 +08006047 pthread_mutex_lock(&out->latch_lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006048 out->offload_state = OFFLOAD_STATE_PLAYING;
Weiyin Jiangd5974e62020-09-08 20:28:22 +08006049 pthread_mutex_unlock(&out->latch_lock);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08006050
6051 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
6052 popcount(out->channel_mask),
6053 out->playback_started);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006054 }
6055 pthread_mutex_unlock(&out->lock);
Haynes Mathew George380745d2017-10-04 15:27:45 -07006056 ATRACE_END();
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006057 return ret;
6058 } else {
6059 if (out->pcm) {
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006060 size_t bytes_to_write = bytes;
Weiyin Jiangd5974e62020-09-08 20:28:22 +08006061 pthread_mutex_lock(&out->latch_lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006062 if (out->muted)
6063 memset((void *)buffer, 0, bytes);
Weiyin Jiangd5974e62020-09-08 20:28:22 +08006064 pthread_mutex_unlock(&out->latch_lock);
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006065 ALOGV("%s: frames=%zu, frame_size=%zu, bytes_to_write=%zu",
6066 __func__, frames, frame_size, bytes_to_write);
6067
Aalique Grahame22e49102018-12-18 14:23:57 -08006068 if (out->usecase == USECASE_INCALL_MUSIC_UPLINK ||
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07006069 out->usecase == USECASE_INCALL_MUSIC_UPLINK2 ||
6070 (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP &&
6071 !audio_extn_utils_is_vendor_enhanced_fwk())) {
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006072 size_t channel_count = audio_channel_count_from_out_mask(out->channel_mask);
6073 int16_t *src = (int16_t *)buffer;
6074 int16_t *dst = (int16_t *)buffer;
6075
Yunfei Zhanga6cd66d2019-07-22 16:15:36 +08006076 LOG_ALWAYS_FATAL_IF(channel_count > 2 ||
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006077 out->format != AUDIO_FORMAT_PCM_16_BIT,
Aalique Grahame22e49102018-12-18 14:23:57 -08006078 "out_write called for %s use case with wrong properties",
6079 use_case_table[out->usecase]);
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006080
6081 /*
6082 * FIXME: this can be removed once audio flinger mixer supports
6083 * mono output
6084 */
6085
6086 /*
6087 * Code below goes over each frame in the buffer and adds both
6088 * L and R samples and then divides by 2 to convert to mono
6089 */
Yunfei Zhanga6cd66d2019-07-22 16:15:36 +08006090 if (channel_count == 2) {
6091 for (size_t i = 0; i < frames ; i++, dst++, src += 2) {
6092 *dst = (int16_t)(((int32_t)src[0] + (int32_t)src[1]) >> 1);
6093 }
6094 bytes_to_write /= 2;
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006095 }
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006096 }
Andy Hunga1f48fa2019-07-01 18:14:53 -07006097
6098 // Note: since out_get_presentation_position() is called alternating with out_write()
6099 // by AudioFlinger, we can check underruns using the prior timestamp read.
6100 // (Alternately we could check if the buffer is empty using pcm_get_htimestamp().
6101 if (out->last_fifo_valid) {
6102 // compute drain to see if there is an underrun.
6103 const int64_t current_ns = systemTime(SYSTEM_TIME_MONOTONIC); // sys call
Dhananjay Kumara4429632020-07-11 02:53:37 +05306104 int64_t time_diff_ns = current_ns - out->last_fifo_time_ns;
6105 int64_t frames_by_time =
6106 ((time_diff_ns > 0) && (time_diff_ns < (INT64_MAX / out->config.rate))) ?
6107 (time_diff_ns * out->config.rate / NANOS_PER_SECOND) : 0;
Andy Hunga1f48fa2019-07-01 18:14:53 -07006108 const int64_t underrun = frames_by_time - out->last_fifo_frames_remaining;
6109
6110 if (underrun > 0) {
6111 simple_stats_log(&out->fifo_underruns, underrun);
6112
6113 ALOGW("%s: underrun(%lld) "
6114 "frames_by_time(%lld) > out->last_fifo_frames_remaining(%lld)",
6115 __func__,
6116 (long long)out->fifo_underruns.n,
6117 (long long)frames_by_time,
6118 (long long)out->last_fifo_frames_remaining);
6119 }
6120 out->last_fifo_valid = false; // we're writing below, mark fifo info as stale.
6121 }
6122
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05306123 ALOGVV("%s: writing buffer (%zu bytes) to pcm device", __func__, bytes);
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07006124
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006125 long ns = 0;
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07006126
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006127 if (out->config.rate)
6128 ns = pcm_bytes_to_frames(out->pcm, bytes)*1000000000LL/
6129 out->config.rate;
6130
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006131 request_out_focus(out, ns);
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006132 bool use_mmap = is_mmap_usecase(out->usecase) || out->realtime;
6133
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006134 if (use_mmap)
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006135 ret = pcm_mmap_write(out->pcm, (void *)buffer, bytes_to_write);
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006136 else if (out->hal_op_format != out->hal_ip_format &&
Ashish Jain83a6cc22016-06-28 14:34:17 +05306137 out->convert_buffer != NULL) {
6138
6139 memcpy_by_audio_format(out->convert_buffer,
6140 out->hal_op_format,
6141 buffer,
6142 out->hal_ip_format,
6143 out->config.period_size * out->config.channels);
6144
6145 ret = pcm_write(out->pcm, out->convert_buffer,
6146 (out->config.period_size *
6147 out->config.channels *
6148 format_to_bitwidth_table[out->hal_op_format]));
6149 } else {
Aditya Bavanarid4db8ee2017-05-29 21:08:03 +05306150 /*
6151 * To avoid underrun in DSP when the application is not pumping
6152 * data at required rate, check for the no. of bytes and ignore
6153 * pcm_write if it is less than actual buffer size.
6154 * It is a work around to a change in compress VOIP driver.
6155 */
6156 if ((out->flags & AUDIO_OUTPUT_FLAG_VOIP_RX) &&
6157 bytes < (out->config.period_size * out->config.channels *
6158 audio_bytes_per_sample(out->format))) {
6159 size_t voip_buf_size =
6160 out->config.period_size * out->config.channels *
6161 audio_bytes_per_sample(out->format);
6162 ALOGE("%s:VOIP underrun: bytes received %zu, required:%zu\n",
6163 __func__, bytes, voip_buf_size);
6164 usleep(((uint64_t)voip_buf_size - bytes) *
6165 1000000 / audio_stream_out_frame_size(stream) /
6166 out_get_sample_rate(&out->stream.common));
6167 ret = 0;
Vignesh Kulothungana6927272019-02-20 15:17:07 -08006168 } else {
6169 if (out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS)
6170 ret = split_and_write_audio_haptic_data(out, buffer, bytes);
6171 else
6172 ret = pcm_write(out->pcm, (void *)buffer, bytes_to_write);
6173 }
Ashish Jain83a6cc22016-06-28 14:34:17 +05306174 }
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07006175
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006176 release_out_focus(out);
6177
Dhanalakshmi Siddani37ca1d62014-08-20 12:28:34 +05306178 if (ret < 0)
6179 ret = -errno;
Zhou Songc9672822017-08-16 16:01:39 +08006180 else if (ret > 0)
Ashish Jain83a6cc22016-06-28 14:34:17 +05306181 ret = -EINVAL;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006182 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006183 }
6184
6185exit:
Zhou Songc9672822017-08-16 16:01:39 +08006186 update_frames_written(out, bytes);
Naresh Tanniru4c630392014-05-12 01:05:52 +05306187 if (-ENETRESET == ret) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306188 out->card_status = CARD_STATUS_OFFLINE;
Naresh Tanniru4c630392014-05-12 01:05:52 +05306189 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006190 pthread_mutex_unlock(&out->lock);
6191
6192 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07006193 if (out->pcm)
Alexy Josephb1379942016-01-29 15:49:38 -08006194 ALOGE("%s: error %d, %s", __func__, (int)ret, pcm_get_error(out->pcm));
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05306195 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05306196 pthread_mutex_lock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05306197 voice_extn_compress_voip_close_output_stream(&out->stream.common);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05306198 out->started = 0;
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05306199 pthread_mutex_unlock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05306200 out->standby = true;
6201 }
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306202 out_on_error(&out->stream.common);
Dieter Luecking5d57def2018-09-07 14:23:37 +02006203 if (!(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
6204 /* prevent division-by-zero */
6205 uint32_t stream_size = audio_stream_out_frame_size(stream);
6206 uint32_t srate = out_get_sample_rate(&out->stream.common);
Vidyakumar Athotaa9d3a5f2017-08-09 12:13:05 -07006207
Dieter Luecking5d57def2018-09-07 14:23:37 +02006208 if ((stream_size == 0) || (srate == 0)) {
6209 ALOGE("%s: stream_size= %d, srate = %d", __func__, stream_size, srate);
6210 ATRACE_END();
6211 return -EINVAL;
6212 }
6213 usleep((uint64_t)bytes * 1000000 / stream_size / srate);
6214 }
Vidyakumar Athotaa9d3a5f2017-08-09 12:13:05 -07006215 if (audio_extn_passthru_is_passthrough_stream(out)) {
Rajshekar Eashwarappa88834522018-04-02 17:20:15 +05306216 //ALOGE("%s: write error, ret = %zd", __func__, ret);
Haynes Mathew George380745d2017-10-04 15:27:45 -07006217 ATRACE_END();
Vidyakumar Athotaa9d3a5f2017-08-09 12:13:05 -07006218 return ret;
6219 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006220 }
Haynes Mathew George380745d2017-10-04 15:27:45 -07006221 ATRACE_END();
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006222 return bytes;
6223}
6224
6225static int out_get_render_position(const struct audio_stream_out *stream,
6226 uint32_t *dsp_frames)
6227{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006228 struct stream_out *out = (struct stream_out *)stream;
Zhou Song32a556e2015-05-05 10:46:56 +08006229
6230 if (dsp_frames == NULL)
6231 return -EINVAL;
6232
6233 *dsp_frames = 0;
6234 if (is_offload_usecase(out->usecase)) {
Mingming Yin9e348b52014-11-19 16:18:55 -08006235 ssize_t ret = 0;
Ashish Jain5106d362016-05-11 19:23:33 +05306236
6237 /* Below piece of code is not guarded against any lock beacuse audioFliner serializes
6238 * this operation and adev_close_output_stream(where out gets reset).
6239 */
Dhananjay Kumarac341582017-02-23 23:42:25 +05306240 if (!out->non_blocking && !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Zhou Song48453a02018-01-10 17:50:59 +08006241 *dsp_frames = get_actual_pcm_frames_rendered(out, NULL);
Ashish Jain5106d362016-05-11 19:23:33 +05306242 ALOGVV("dsp_frames %d sampleRate %d",(int)*dsp_frames,out->sample_rate);
Haynes Mathew Georgeb0f5dc32017-10-06 18:35:12 -07006243 adjust_frames_for_device_delay(out, dsp_frames);
Ashish Jain5106d362016-05-11 19:23:33 +05306244 return 0;
6245 }
6246
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006247 lock_output_stream(out);
Ashish Jain5106d362016-05-11 19:23:33 +05306248 if (out->compr != NULL && out->non_blocking) {
Naresh Tanniru80659832014-06-04 18:17:56 +05306249 ret = compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006250 &out->sample_rate);
Dhanalakshmi Siddani37ca1d62014-08-20 12:28:34 +05306251 if (ret < 0)
6252 ret = -errno;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006253 ALOGVV("%s rendered frames %d sample_rate %d",
Ashish Jain5106d362016-05-11 19:23:33 +05306254 __func__, *dsp_frames, out->sample_rate);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006255 }
Naresh Tanniru80659832014-06-04 18:17:56 +05306256 if (-ENETRESET == ret) {
6257 ALOGE(" ERROR: sound card not active Unable to get time stamp from compress driver");
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306258 out->card_status = CARD_STATUS_OFFLINE;
6259 ret = -EINVAL;
Naresh Tanniru80659832014-06-04 18:17:56 +05306260 } else if(ret < 0) {
6261 ALOGE(" ERROR: Unable to get time stamp from compress driver");
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306262 ret = -EINVAL;
6263 } else if (out->card_status == CARD_STATUS_OFFLINE) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05306264 /*
6265 * Handle corner case where compress session is closed during SSR
6266 * and timestamp is queried
6267 */
6268 ALOGE(" ERROR: sound card not active, return error");
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306269 ret = -EINVAL;
Manisha Agarwal7b3e3772019-02-20 14:33:45 +05306270 } else if (out->prev_card_status_offline) {
6271 ALOGE("ERROR: previously sound card was offline,return error");
6272 ret = -EINVAL;
Naresh Tanniru80659832014-06-04 18:17:56 +05306273 } else {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306274 ret = 0;
Haynes Mathew Georgeb0f5dc32017-10-06 18:35:12 -07006275 adjust_frames_for_device_delay(out, dsp_frames);
Naresh Tanniru80659832014-06-04 18:17:56 +05306276 }
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306277 pthread_mutex_unlock(&out->lock);
6278 return ret;
Zhou Song32a556e2015-05-05 10:46:56 +08006279 } else if (audio_is_linear_pcm(out->format)) {
6280 *dsp_frames = out->written;
Haynes Mathew Georgeb0f5dc32017-10-06 18:35:12 -07006281 adjust_frames_for_device_delay(out, dsp_frames);
Zhou Song32a556e2015-05-05 10:46:56 +08006282 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006283 } else
6284 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006285}
6286
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07006287static int out_add_audio_effect(const struct audio_stream *stream __unused,
6288 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006289{
6290 return 0;
6291}
6292
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07006293static int out_remove_audio_effect(const struct audio_stream *stream __unused,
6294 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006295{
6296 return 0;
6297}
6298
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07006299static int out_get_next_write_timestamp(const struct audio_stream_out *stream __unused,
6300 int64_t *timestamp __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006301{
Satya Krishna Pindiprolib6655542017-07-03 19:38:19 +05306302 return -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006303}
6304
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07006305static int out_get_presentation_position(const struct audio_stream_out *stream,
6306 uint64_t *frames, struct timespec *timestamp)
6307{
6308 struct stream_out *out = (struct stream_out *)stream;
pavance65c2fe2017-10-18 17:52:01 +05306309 int ret = -ENODATA;
Eric Laurent949a0892013-09-20 09:20:13 -07006310 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07006311
Ashish Jain5106d362016-05-11 19:23:33 +05306312 /* below piece of code is not guarded against any lock because audioFliner serializes
6313 * this operation and adev_close_output_stream( where out gets reset).
6314 */
6315 if (is_offload_usecase(out->usecase) && !out->non_blocking &&
Dhananjay Kumarac341582017-02-23 23:42:25 +05306316 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Zhou Song48453a02018-01-10 17:50:59 +08006317 *frames = get_actual_pcm_frames_rendered(out, timestamp);
Ashish Jain5106d362016-05-11 19:23:33 +05306318 ALOGVV("frames %lld playedat %lld",(long long int)*frames,
6319 timestamp->tv_sec * 1000000LL + timestamp->tv_nsec / 1000);
6320 return 0;
6321 }
6322
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006323 lock_output_stream(out);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07006324
Ashish Jain5106d362016-05-11 19:23:33 +05306325 if (is_offload_usecase(out->usecase) && out->compr != NULL && out->non_blocking) {
6326 ret = compress_get_tstamp(out->compr, &dsp_frames,
6327 &out->sample_rate);
yidongh0515e042017-07-06 15:00:34 +08006328 // Adjustment accounts for A2dp encoder latency with offload usecases
6329 // Note: Encoder latency is returned in ms.
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08006330 if (is_a2dp_out_device_type(&out->device_list)) {
yidongh0515e042017-07-06 15:00:34 +08006331 unsigned long offset =
6332 (audio_extn_a2dp_get_encoder_latency() * out->sample_rate / 1000);
6333 dsp_frames = (dsp_frames > offset) ? (dsp_frames - offset) : 0;
6334 }
Ashish Jain5106d362016-05-11 19:23:33 +05306335 ALOGVV("%s rendered frames %ld sample_rate %d",
6336 __func__, dsp_frames, out->sample_rate);
6337 *frames = dsp_frames;
6338 if (ret < 0)
6339 ret = -errno;
6340 if (-ENETRESET == ret) {
6341 ALOGE(" ERROR: sound card not active Unable to get time stamp from compress driver");
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306342 out->card_status = CARD_STATUS_OFFLINE;
Ashish Jain5106d362016-05-11 19:23:33 +05306343 ret = -EINVAL;
6344 } else
6345 ret = 0;
6346 /* this is the best we can do */
6347 clock_gettime(CLOCK_MONOTONIC, timestamp);
Eric Laurent949a0892013-09-20 09:20:13 -07006348 } else {
6349 if (out->pcm) {
Weiyin Jiangd4633762018-03-16 12:05:03 +08006350 unsigned int avail;
6351 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
George Gao62ebc722019-07-29 16:29:44 -07006352 uint64_t signed_frames = 0;
Weiyin Jiang0d25bcf2019-08-30 15:27:23 +08006353 uint64_t frames_temp = 0;
George Gao62ebc722019-07-29 16:29:44 -07006354
Andy Hunga1f48fa2019-07-01 18:14:53 -07006355 if (out->kernel_buffer_size > avail) {
6356 frames_temp = out->last_fifo_frames_remaining = out->kernel_buffer_size - avail;
6357 } else {
6358 ALOGW("%s: avail:%u > kernel_buffer_size:%zu clamping!",
6359 __func__, avail, out->kernel_buffer_size);
6360 avail = out->kernel_buffer_size;
6361 frames_temp = out->last_fifo_frames_remaining = 0;
6362 }
6363 out->last_fifo_valid = true;
6364 out->last_fifo_time_ns = audio_utils_ns_from_timespec(timestamp);
6365
Weiyin Jiang0d25bcf2019-08-30 15:27:23 +08006366 if (out->written >= frames_temp)
6367 signed_frames = out->written - frames_temp;
George Gao62ebc722019-07-29 16:29:44 -07006368
Andy Hunga1f48fa2019-07-01 18:14:53 -07006369 ALOGVV("%s: frames:%lld avail:%u kernel_buffer_size:%zu",
6370 __func__, (long long)signed_frames, avail, out->kernel_buffer_size);
6371
Weiyin Jiangd4633762018-03-16 12:05:03 +08006372 // This adjustment accounts for buffering after app processor.
6373 // It is based on estimated DSP latency per use case, rather than exact.
George Gao9ba8a142020-07-23 14:30:03 -07006374 frames_temp = platform_render_latency(out) *
Robert Lee58215542019-07-15 20:55:12 +08006375 out->sample_rate / 1000000LL;
Weiyin Jiang0d25bcf2019-08-30 15:27:23 +08006376 if (signed_frames >= frames_temp)
6377 signed_frames -= frames_temp;
Aniket Kumar Lataff613152017-07-18 18:19:21 -07006378
Weiyin Jiangd4633762018-03-16 12:05:03 +08006379 // Adjustment accounts for A2dp encoder latency with non offload usecases
6380 // Note: Encoder latency is returned in ms, while platform_render_latency in us.
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08006381 if (is_a2dp_out_device_type(&out->device_list)) {
Weiyin Jiang0d25bcf2019-08-30 15:27:23 +08006382 frames_temp = audio_extn_a2dp_get_encoder_latency() * out->sample_rate / 1000;
6383 if (signed_frames >= frames_temp)
6384 signed_frames -= frames_temp;
Weiyin Jiangd4633762018-03-16 12:05:03 +08006385 }
6386
6387 // It would be unusual for this value to be negative, but check just in case ...
George Gao62ebc722019-07-29 16:29:44 -07006388 *frames = signed_frames;
6389 ret = 0;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07006390 }
Eric Laurenta7a33042019-07-10 16:20:22 -07006391 } else if (out->card_status == CARD_STATUS_OFFLINE ||
6392 // audioflinger still needs position updates when A2DP is suspended
Jasmine Cha5c2517f2019-09-09 11:07:28 +08006393 (is_a2dp_out_device_type(&out->device_list) && audio_extn_a2dp_source_is_suspended())) {
Ashish Jainbbce4322016-02-16 13:25:27 +05306394 *frames = out->written;
6395 clock_gettime(CLOCK_MONOTONIC, timestamp);
Manisha Agarwal7b3e3772019-02-20 14:33:45 +05306396 if (is_offload_usecase(out->usecase))
6397 ret = -EINVAL;
6398 else
6399 ret = 0;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07006400 }
6401 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07006402 pthread_mutex_unlock(&out->lock);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07006403 return ret;
6404}
6405
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006406static int out_set_callback(struct audio_stream_out *stream,
6407 stream_callback_t callback, void *cookie)
6408{
6409 struct stream_out *out = (struct stream_out *)stream;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08006410 int ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006411
6412 ALOGV("%s", __func__);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006413 lock_output_stream(out);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08006414 out->client_callback = callback;
6415 out->client_cookie = cookie;
6416 if (out->adsp_hdlr_stream_handle) {
6417 ret = audio_extn_adsp_hdlr_stream_set_callback(
6418 out->adsp_hdlr_stream_handle,
6419 callback,
6420 cookie);
6421 if (ret)
6422 ALOGW("%s:adsp hdlr callback registration failed %d",
6423 __func__, ret);
6424 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006425 pthread_mutex_unlock(&out->lock);
6426 return 0;
6427}
6428
6429static int out_pause(struct audio_stream_out* stream)
6430{
6431 struct stream_out *out = (struct stream_out *)stream;
6432 int status = -ENOSYS;
6433 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07006434 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07006435 ALOGD("copl(%p):pause compress driver", out);
Sujin Panicker7c7b6f92020-04-03 12:57:55 +05306436 status = -ENODATA;
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006437 lock_output_stream(out);
Weiyin Jiangd5974e62020-09-08 20:28:22 +08006438 pthread_mutex_lock(&out->latch_lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006439 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306440 if (out->card_status != CARD_STATUS_OFFLINE)
Naresh Tanniru80659832014-06-04 18:17:56 +05306441 status = compress_pause(out->compr);
6442
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006443 out->offload_state = OFFLOAD_STATE_PAUSED;
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08006444
Mingming Yin21854652016-04-13 11:54:02 -07006445 if (audio_extn_passthru_is_active()) {
6446 ALOGV("offload use case, pause passthru");
6447 audio_extn_passthru_on_pause(out);
6448 }
6449
Dhanalakshmi Siddani79415e72015-03-23 11:54:47 +05306450 audio_extn_dts_eagle_fade(adev, false, out);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08006451 audio_extn_dts_notify_playback_state(out->usecase, 0,
6452 out->sample_rate, popcount(out->channel_mask),
6453 0);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006454 }
Weiyin Jiangd5974e62020-09-08 20:28:22 +08006455 pthread_mutex_unlock(&out->latch_lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006456 pthread_mutex_unlock(&out->lock);
6457 }
6458 return status;
6459}
6460
6461static int out_resume(struct audio_stream_out* stream)
6462{
6463 struct stream_out *out = (struct stream_out *)stream;
6464 int status = -ENOSYS;
6465 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07006466 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07006467 ALOGD("copl(%p):resume compress driver", out);
Sujin Panicker7c7b6f92020-04-03 12:57:55 +05306468 status = -ENODATA;
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006469 lock_output_stream(out);
Weiyin Jiangd5974e62020-09-08 20:28:22 +08006470 pthread_mutex_lock(&out->latch_lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006471 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306472 if (out->card_status != CARD_STATUS_OFFLINE) {
Naresh Tanniru80659832014-06-04 18:17:56 +05306473 status = compress_resume(out->compr);
Mingming Yin21854652016-04-13 11:54:02 -07006474 }
6475 if (!status) {
6476 out->offload_state = OFFLOAD_STATE_PLAYING;
6477 }
Dhanalakshmi Siddani79415e72015-03-23 11:54:47 +05306478 audio_extn_dts_eagle_fade(adev, true, out);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08006479 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
6480 popcount(out->channel_mask), 1);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006481 }
Weiyin Jiangd5974e62020-09-08 20:28:22 +08006482 pthread_mutex_unlock(&out->latch_lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006483 pthread_mutex_unlock(&out->lock);
6484 }
6485 return status;
6486}
6487
6488static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
6489{
6490 struct stream_out *out = (struct stream_out *)stream;
6491 int status = -ENOSYS;
6492 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07006493 if (is_offload_usecase(out->usecase)) {
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006494 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006495 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
6496 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
6497 else
6498 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
6499 pthread_mutex_unlock(&out->lock);
6500 }
6501 return status;
6502}
6503
6504static int out_flush(struct audio_stream_out* stream)
6505{
6506 struct stream_out *out = (struct stream_out *)stream;
6507 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07006508 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07006509 ALOGD("copl(%p):calling compress flush", out);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006510 lock_output_stream(out);
Weiyin Jiangd5974e62020-09-08 20:28:22 +08006511 pthread_mutex_lock(&out->latch_lock);
Haynes Mathew Georgeafe54d82016-09-21 14:39:19 -07006512 if (out->offload_state == OFFLOAD_STATE_PAUSED) {
6513 stop_compressed_output_l(out);
Haynes Mathew Georgeafe54d82016-09-21 14:39:19 -07006514 } else {
6515 ALOGW("%s called in invalid state %d", __func__, out->offload_state);
6516 }
Weiyin Jiang547e4152017-09-14 17:24:18 +08006517 out->written = 0;
Weiyin Jiangd5974e62020-09-08 20:28:22 +08006518 pthread_mutex_unlock(&out->latch_lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006519 pthread_mutex_unlock(&out->lock);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07006520 ALOGD("copl(%p):out of compress flush", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006521 return 0;
6522 }
6523 return -ENOSYS;
6524}
6525
Haynes Mathew George16081042017-05-31 17:16:49 -07006526static int out_stop(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 pcm_stop(out->pcm);
6537 ret = stop_output_stream(out);
6538 out->playback_started = false;
6539 }
6540 pthread_mutex_unlock(&adev->lock);
6541 return ret;
6542}
6543
6544static int out_start(const struct audio_stream_out* stream)
6545{
6546 struct stream_out *out = (struct stream_out *)stream;
6547 struct audio_device *adev = out->dev;
6548 int ret = -ENOSYS;
6549
6550 ALOGV("%s", __func__);
6551 pthread_mutex_lock(&adev->lock);
6552 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP && !out->standby &&
6553 !out->playback_started && out->pcm != NULL) {
6554 ret = start_output_stream(out);
6555 if (ret == 0) {
6556 out->playback_started = true;
6557 }
6558 }
6559 pthread_mutex_unlock(&adev->lock);
6560 return ret;
6561}
6562
6563/*
6564 * Modify config->period_count based on min_size_frames
6565 */
6566static void adjust_mmap_period_count(struct pcm_config *config, int32_t min_size_frames)
6567{
6568 int periodCountRequested = (min_size_frames + config->period_size - 1)
6569 / config->period_size;
6570 int periodCount = MMAP_PERIOD_COUNT_MIN;
6571
6572 ALOGV("%s original config.period_size = %d config.period_count = %d",
6573 __func__, config->period_size, config->period_count);
6574
6575 while (periodCount < periodCountRequested && (periodCount * 2) < MMAP_PERIOD_COUNT_MAX) {
6576 periodCount *= 2;
6577 }
6578 config->period_count = periodCount;
6579
6580 ALOGV("%s requested config.period_count = %d", __func__, config->period_count);
6581}
6582
Phil Burkfe17efd2019-03-25 10:23:35 -07006583// Read offset for the positional timestamp from a persistent vendor property.
6584// This is to workaround apparent inaccuracies in the timing information that
6585// is used by the AAudio timing model. The inaccuracies can cause glitches.
6586static int64_t get_mmap_out_time_offset() {
6587 const int32_t kDefaultOffsetMicros = 0;
6588 int32_t mmap_time_offset_micros = property_get_int32(
Weiyin Jiangbd68b4d2019-05-17 16:29:50 +08006589 "persist.vendor.audio.out_mmap_delay_micros", kDefaultOffsetMicros);
Phil Burkfe17efd2019-03-25 10:23:35 -07006590 ALOGI("mmap_time_offset_micros = %d for output", mmap_time_offset_micros);
6591 return mmap_time_offset_micros * (int64_t)1000;
6592}
6593
Haynes Mathew George16081042017-05-31 17:16:49 -07006594static int out_create_mmap_buffer(const struct audio_stream_out *stream,
6595 int32_t min_size_frames,
6596 struct audio_mmap_buffer_info *info)
6597{
6598 struct stream_out *out = (struct stream_out *)stream;
6599 struct audio_device *adev = out->dev;
6600 int ret = 0;
Aalique Grahame1f123102017-10-12 10:38:32 -07006601 unsigned int offset1 = 0;
6602 unsigned int frames1 = 0;
Haynes Mathew George16081042017-05-31 17:16:49 -07006603 const char *step = "";
Haynes Mathew Georgeef514882017-05-01 17:46:23 -07006604 uint32_t mmap_size;
Arun Mirpuri5d170872019-03-26 13:21:31 -07006605 uint32_t buffer_size;
Haynes Mathew George16081042017-05-31 17:16:49 -07006606
Arun Mirpuri5d170872019-03-26 13:21:31 -07006607 ALOGD("%s", __func__);
Sharad Sanglec6f32552018-05-04 16:15:38 +05306608 lock_output_stream(out);
Haynes Mathew George16081042017-05-31 17:16:49 -07006609 pthread_mutex_lock(&adev->lock);
6610
Sharad Sanglec6f32552018-05-04 16:15:38 +05306611 if (CARD_STATUS_OFFLINE == out->card_status ||
6612 CARD_STATUS_OFFLINE == adev->card_status) {
6613 ALOGW("out->card_status or adev->card_status offline, try again");
6614 ret = -EIO;
6615 goto exit;
6616 }
Sujin Panicker7c7b6f92020-04-03 12:57:55 +05306617 if (info == NULL || !(min_size_frames > 0 && min_size_frames < INT32_MAX)) {
Haynes Mathew George16081042017-05-31 17:16:49 -07006618 ALOGE("%s: info = %p, min_size_frames = %d", __func__, info, min_size_frames);
6619 ret = -EINVAL;
6620 goto exit;
6621 }
6622 if (out->usecase != USECASE_AUDIO_PLAYBACK_MMAP || !out->standby) {
6623 ALOGE("%s: usecase = %d, standby = %d", __func__, out->usecase, out->standby);
6624 ret = -ENOSYS;
6625 goto exit;
6626 }
6627 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
6628 if (out->pcm_device_id < 0) {
6629 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
6630 __func__, out->pcm_device_id, out->usecase);
6631 ret = -EINVAL;
6632 goto exit;
6633 }
6634
6635 adjust_mmap_period_count(&out->config, min_size_frames);
6636
Arun Mirpuri5d170872019-03-26 13:21:31 -07006637 ALOGD("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
Haynes Mathew George16081042017-05-31 17:16:49 -07006638 __func__, adev->snd_card, out->pcm_device_id, out->config.channels);
6639 out->pcm = pcm_open(adev->snd_card, out->pcm_device_id,
6640 (PCM_OUT | PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC), &out->config);
Satish Babu Patakokila54ce83d2018-07-06 18:00:37 +05306641 if (errno == ENETRESET && !pcm_is_ready(out->pcm)) {
Sharad Sanglec6f32552018-05-04 16:15:38 +05306642 ALOGE("%s: pcm_open failed errno:%d\n", __func__, errno);
6643 out->card_status = CARD_STATUS_OFFLINE;
6644 adev->card_status = CARD_STATUS_OFFLINE;
6645 ret = -EIO;
6646 goto exit;
6647 }
6648
Haynes Mathew George16081042017-05-31 17:16:49 -07006649 if (out->pcm == NULL || !pcm_is_ready(out->pcm)) {
6650 step = "open";
6651 ret = -ENODEV;
6652 goto exit;
6653 }
6654 ret = pcm_mmap_begin(out->pcm, &info->shared_memory_address, &offset1, &frames1);
6655 if (ret < 0) {
6656 step = "begin";
6657 goto exit;
6658 }
juyuchen626833d2019-06-04 16:48:02 +08006659
6660 info->flags = 0;
Haynes Mathew George16081042017-05-31 17:16:49 -07006661 info->buffer_size_frames = pcm_get_buffer_size(out->pcm);
Arun Mirpuri5d170872019-03-26 13:21:31 -07006662 buffer_size = pcm_frames_to_bytes(out->pcm, info->buffer_size_frames);
Haynes Mathew George16081042017-05-31 17:16:49 -07006663 info->burst_size_frames = out->config.period_size;
Haynes Mathew Georgeef514882017-05-01 17:46:23 -07006664 ret = platform_get_mmap_data_fd(adev->platform,
6665 out->pcm_device_id, 0 /*playback*/,
6666 &info->shared_memory_fd,
6667 &mmap_size);
6668 if (ret < 0) {
Arun Mirpuri5d170872019-03-26 13:21:31 -07006669 // Fall back to non exclusive mode
6670 info->shared_memory_fd = pcm_get_poll_fd(out->pcm);
6671 } else {
Phil Burkd898ba62019-06-20 12:49:01 -07006672 out->mmap_shared_memory_fd = info->shared_memory_fd; // for closing later
6673 ALOGV("%s: opened mmap_shared_memory_fd = %d", __func__, out->mmap_shared_memory_fd);
6674
Arun Mirpuri5d170872019-03-26 13:21:31 -07006675 if (mmap_size < buffer_size) {
6676 step = "mmap";
6677 goto exit;
6678 }
juyuchen626833d2019-06-04 16:48:02 +08006679 info->flags |= AUDIO_MMAP_APPLICATION_SHAREABLE;
Haynes Mathew Georgeef514882017-05-01 17:46:23 -07006680 }
Haynes Mathew George16081042017-05-31 17:16:49 -07006681 memset(info->shared_memory_address, 0, pcm_frames_to_bytes(out->pcm,
Haynes Mathew Georgeef514882017-05-01 17:46:23 -07006682 info->buffer_size_frames));
Haynes Mathew George16081042017-05-31 17:16:49 -07006683
6684 ret = pcm_mmap_commit(out->pcm, 0, MMAP_PERIOD_SIZE);
6685 if (ret < 0) {
6686 step = "commit";
6687 goto exit;
6688 }
6689
Phil Burkfe17efd2019-03-25 10:23:35 -07006690 out->mmap_time_offset_nanos = get_mmap_out_time_offset();
6691
Haynes Mathew George16081042017-05-31 17:16:49 -07006692 out->standby = false;
6693 ret = 0;
6694
Arun Mirpuri5d170872019-03-26 13:21:31 -07006695 ALOGD("%s: got mmap buffer address %p info->buffer_size_frames %d",
Haynes Mathew George16081042017-05-31 17:16:49 -07006696 __func__, info->shared_memory_address, info->buffer_size_frames);
6697
6698exit:
6699 if (ret != 0) {
6700 if (out->pcm == NULL) {
6701 ALOGE("%s: %s - %d", __func__, step, ret);
6702 } else {
6703 ALOGE("%s: %s %s", __func__, step, pcm_get_error(out->pcm));
6704 pcm_close(out->pcm);
6705 out->pcm = NULL;
6706 }
6707 }
6708 pthread_mutex_unlock(&adev->lock);
Sharad Sanglec6f32552018-05-04 16:15:38 +05306709 pthread_mutex_unlock(&out->lock);
Haynes Mathew George16081042017-05-31 17:16:49 -07006710 return ret;
6711}
6712
6713static int out_get_mmap_position(const struct audio_stream_out *stream,
6714 struct audio_mmap_position *position)
6715{
6716 struct stream_out *out = (struct stream_out *)stream;
6717 ALOGVV("%s", __func__);
6718 if (position == NULL) {
6719 return -EINVAL;
6720 }
6721 if (out->usecase != USECASE_AUDIO_PLAYBACK_MMAP) {
Haynes Mathew George4ab3ba92017-12-11 14:49:43 -08006722 ALOGE("%s: called on %s", __func__, use_case_table[out->usecase]);
Haynes Mathew George16081042017-05-31 17:16:49 -07006723 return -ENOSYS;
6724 }
6725 if (out->pcm == NULL) {
6726 return -ENOSYS;
6727 }
6728
6729 struct timespec ts = { 0, 0 };
6730 int ret = pcm_mmap_get_hw_ptr(out->pcm, (unsigned int *)&position->position_frames, &ts);
6731 if (ret < 0) {
6732 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
6733 return ret;
6734 }
Phil Burkfe17efd2019-03-25 10:23:35 -07006735 position->time_nanoseconds = ts.tv_sec*1000000000LL + ts.tv_nsec
6736 + out->mmap_time_offset_nanos;
Haynes Mathew George16081042017-05-31 17:16:49 -07006737 return 0;
6738}
6739
6740
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006741/** audio_stream_in implementation **/
6742static uint32_t in_get_sample_rate(const struct audio_stream *stream)
6743{
6744 struct stream_in *in = (struct stream_in *)stream;
6745
6746 return in->config.rate;
6747}
6748
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07006749static int in_set_sample_rate(struct audio_stream *stream __unused,
6750 uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006751{
6752 return -ENOSYS;
6753}
6754
6755static size_t in_get_buffer_size(const struct audio_stream *stream)
6756{
6757 struct stream_in *in = (struct stream_in *)stream;
6758
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08006759 if(in->usecase == USECASE_COMPRESS_VOIP_CALL)
6760 return voice_extn_compress_voip_in_get_buffer_size(in);
Mingming Yine62d7842013-10-25 16:26:03 -07006761 else if(audio_extn_compr_cap_usecase_supported(in->usecase))
6762 return audio_extn_compr_cap_get_buffer_size(in->config.format);
Dhananjay Kumar7dbe3562019-08-30 05:49:33 +05306763 else if(audio_extn_cin_attached_usecase(in))
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05306764 return audio_extn_cin_get_buffer_size(in);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08006765
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006766 return in->config.period_size * in->af_period_multiplier *
6767 audio_stream_in_frame_size((const struct audio_stream_in *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006768}
6769
6770static uint32_t in_get_channels(const struct audio_stream *stream)
6771{
6772 struct stream_in *in = (struct stream_in *)stream;
6773
6774 return in->channel_mask;
6775}
6776
6777static audio_format_t in_get_format(const struct audio_stream *stream)
6778{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08006779 struct stream_in *in = (struct stream_in *)stream;
6780
6781 return in->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006782}
6783
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07006784static int in_set_format(struct audio_stream *stream __unused,
6785 audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006786{
6787 return -ENOSYS;
6788}
6789
6790static int in_standby(struct audio_stream *stream)
6791{
6792 struct stream_in *in = (struct stream_in *)stream;
6793 struct audio_device *adev = in->dev;
6794 int status = 0;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05306795 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
6796 stream, in->usecase, use_case_table[in->usecase]);
Haynes Mathew George16081042017-05-31 17:16:49 -07006797 bool do_stop = true;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05306798
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006799 lock_input_stream(in);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07006800 if (!in->standby && in->is_st_session) {
6801 ALOGD("%s: sound trigger pcm stop lab", __func__);
6802 audio_extn_sound_trigger_stop_lab(in);
George Gao3018ede2019-10-23 13:23:00 -07006803 if (adev->num_va_sessions > 0)
6804 adev->num_va_sessions--;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07006805 in->standby = 1;
6806 }
6807
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006808 if (!in->standby) {
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07006809 if (adev->adm_deregister_stream)
6810 adev->adm_deregister_stream(adev->adm_data, in->capture_handle);
6811
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08006812 pthread_mutex_lock(&adev->lock);
Daniel Hillenbrand21752f02013-05-23 10:10:00 +05306813 amplifier_input_stream_standby((struct audio_stream_in *) stream);
6814
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006815 in->standby = true;
Zhou Songa8895042016-07-05 17:54:22 +08006816 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
kunleizbecba2d2017-09-07 13:37:16 +08006817 do_stop = false;
Zhou Songa8895042016-07-05 17:54:22 +08006818 voice_extn_compress_voip_close_input_stream(stream);
6819 ALOGD("VOIP input entered standby");
Haynes Mathew George16081042017-05-31 17:16:49 -07006820 } else if (in->usecase == USECASE_AUDIO_RECORD_MMAP) {
6821 do_stop = in->capture_started;
6822 in->capture_started = false;
Phil Burkd898ba62019-06-20 12:49:01 -07006823 if (in->mmap_shared_memory_fd >= 0) {
6824 ALOGV("%s: closing mmap_shared_memory_fd = %d",
6825 __func__, in->mmap_shared_memory_fd);
6826 close(in->mmap_shared_memory_fd);
6827 in->mmap_shared_memory_fd = -1;
6828 }
Zhou Songa8895042016-07-05 17:54:22 +08006829 } else {
Dhananjay Kumar7dbe3562019-08-30 05:49:33 +05306830 if (audio_extn_cin_attached_usecase(in))
Manish Dewangan46e07982018-12-13 18:18:59 +05306831 audio_extn_cin_close_input_stream(in);
kunleizbecba2d2017-09-07 13:37:16 +08006832 }
6833
Arun Mirpuri5d170872019-03-26 13:21:31 -07006834 if (in->pcm) {
6835 ATRACE_BEGIN("pcm_in_close");
6836 pcm_close(in->pcm);
6837 ATRACE_END();
6838 in->pcm = NULL;
6839 }
6840
Carter Hsu2e429db2019-05-14 18:50:52 +08006841 if (do_stop)
Zhou Songa8895042016-07-05 17:54:22 +08006842 status = stop_input_stream(in);
Quinn Malef6050362019-01-30 15:55:40 -08006843
George Gao3018ede2019-10-23 13:23:00 -07006844 if (in->source == AUDIO_SOURCE_VOICE_RECOGNITION) {
6845 if (adev->num_va_sessions > 0)
6846 adev->num_va_sessions--;
6847 }
Quinn Malef6050362019-01-30 15:55:40 -08006848
Eric Laurent150dbfe2013-02-27 14:31:02 -08006849 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006850 }
6851 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07006852 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006853 return status;
6854}
6855
Aalique Grahame22e49102018-12-18 14:23:57 -08006856static int in_dump(const struct audio_stream *stream,
6857 int fd)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006858{
Aalique Grahame22e49102018-12-18 14:23:57 -08006859 struct stream_in *in = (struct stream_in *)stream;
6860
6861 // We try to get the lock for consistency,
6862 // but it isn't necessary for these variables.
6863 // If we're not in standby, we may be blocked on a read.
6864 const bool locked = (pthread_mutex_trylock(&in->lock) == 0);
6865 dprintf(fd, " Standby: %s\n", in->standby ? "yes" : "no");
6866 dprintf(fd, " Frames read: %lld\n", (long long)in->frames_read);
6867 dprintf(fd, " Frames muted: %lld\n", (long long)in->frames_muted);
6868
Andy Hungc6bfd4a2019-07-01 18:26:00 -07006869 char buffer[256]; // for statistics formatting
6870 if (in->start_latency_ms.n > 0) {
6871 simple_stats_to_string(&in->start_latency_ms, buffer, sizeof(buffer));
6872 dprintf(fd, " Start latency ms: %s\n", buffer);
6873 }
6874
Aalique Grahame22e49102018-12-18 14:23:57 -08006875 if (locked) {
6876 pthread_mutex_unlock(&in->lock);
6877 }
6878
6879 // dump error info
6880 (void)error_log_dump(
6881 in->error_log, fd, " " /* prefix */, 0 /* lines */, 0 /* limit_ns */);
6882
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006883 return 0;
6884}
6885
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306886static void in_snd_mon_cb(void * stream, struct str_parms * parms)
6887{
6888 if (!stream || !parms)
6889 return;
6890
6891 struct stream_in *in = (struct stream_in *)stream;
6892 struct audio_device *adev = in->dev;
6893
6894 card_status_t status;
6895 int card;
6896 if (parse_snd_card_status(parms, &card, &status) < 0)
6897 return;
6898
6899 pthread_mutex_lock(&adev->lock);
6900 bool valid_cb = (card == adev->snd_card);
6901 pthread_mutex_unlock(&adev->lock);
6902
6903 if (!valid_cb)
6904 return;
6905
6906 lock_input_stream(in);
6907 if (in->card_status != status)
6908 in->card_status = status;
6909 pthread_mutex_unlock(&in->lock);
6910
6911 ALOGW("in_snd_mon_cb for card %d usecase %s, status %s", card,
6912 use_case_table[in->usecase],
6913 status == CARD_STATUS_OFFLINE ? "offline" : "online");
6914
6915 // a better solution would be to report error back to AF and let
6916 // it put the stream to standby
6917 if (status == CARD_STATUS_OFFLINE)
6918 in_standby(&in->stream.common);
6919
6920 return;
6921}
6922
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07006923int route_input_stream(struct stream_in *in,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08006924 struct listnode *devices,
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07006925 audio_source_t source)
6926{
6927 struct audio_device *adev = in->dev;
6928 int ret = 0;
6929
6930 lock_input_stream(in);
6931 pthread_mutex_lock(&adev->lock);
6932
6933 /* no audio source uses val == 0 */
6934 if ((in->source != source) && (source != AUDIO_SOURCE_DEFAULT)) {
6935 in->source = source;
6936 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
6937 (in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
6938 (voice_extn_compress_voip_is_format_supported(in->format)) &&
6939 (in->config.rate == 8000 || in->config.rate == 16000 ||
6940 in->config.rate == 32000 || in->config.rate == 48000 ) &&
6941 (audio_channel_count_from_in_mask(in->channel_mask) == 1)) {
6942 ret = voice_extn_compress_voip_open_input_stream(in);
6943 if (ret != 0) {
6944 ALOGE("%s: Compress voip input cannot be opened, error:%d",
6945 __func__, ret);
6946 }
6947 }
6948 }
6949
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08006950 if (!compare_devices(&in->device_list, devices) && !list_empty(devices) &&
6951 is_audio_in_device_type(devices)) {
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07006952 // Workaround: If routing to an non existing usb device, fail gracefully
6953 // The routing request will otherwise block during 10 second
6954 int card;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08006955 struct str_parms *usb_addr =
6956 str_parms_create_str(get_usb_device_address(devices));
6957 if (is_usb_in_device_type(devices) && usb_addr &&
6958 (card = get_alive_usb_card(usb_addr)) >= 0) {
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07006959 ALOGW("%s: ignoring rerouting to non existing USB card %d", __func__, card);
6960 ret = -ENOSYS;
6961 } else {
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07006962 /* If recording is in progress, change the tx device to new device */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08006963 assign_devices(&in->device_list, devices);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07006964 if (!in->standby && !in->is_st_session) {
6965 ALOGV("update input routing change");
6966 // inform adm before actual routing to prevent glitches.
6967 if (adev->adm_on_routing_change) {
6968 adev->adm_on_routing_change(adev->adm_data,
6969 in->capture_handle);
6970 ret = select_devices(adev, in->usecase);
6971 if (in->usecase == USECASE_AUDIO_RECORD_LOW_LATENCY)
6972 adev->adm_routing_changed = true;
6973 }
6974 }
6975 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08006976 if (usb_addr)
6977 str_parms_destroy(usb_addr);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07006978 }
6979 pthread_mutex_unlock(&adev->lock);
6980 pthread_mutex_unlock(&in->lock);
6981
6982 ALOGD("%s: exit: status(%d)", __func__, ret);
6983 return ret;
6984}
6985
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006986static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
6987{
6988 struct stream_in *in = (struct stream_in *)stream;
6989 struct audio_device *adev = in->dev;
6990 struct str_parms *parms;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006991 char value[32];
Sujin Panicker7c7b6f92020-04-03 12:57:55 +05306992 int err = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006993
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05306994 ALOGD("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006995 parms = str_parms_create_str(kvpairs);
6996
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05306997 if (!parms)
6998 goto error;
Daniel Hillenbrand21752f02013-05-23 10:10:00 +05306999
7000 amplifier_in_set_parameters(parms);
7001
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07007002 lock_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08007003 pthread_mutex_lock(&adev->lock);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08007004
Sujin Panicker7c7b6f92020-04-03 12:57:55 +05307005 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_PROFILE, value, sizeof(value));
7006 if (err >= 0) {
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +05307007 strlcpy(in->profile, value, sizeof(in->profile));
7008 ALOGV("updating stream profile with value '%s'", in->profile);
7009 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
7010 &adev->streams_input_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08007011 &in->device_list, in->flags, in->format,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +05307012 in->sample_rate, in->bit_width,
7013 in->profile, &in->app_type_cfg);
7014 }
7015
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007016 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08007017 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007018
7019 str_parms_destroy(parms);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05307020error:
Sujin Panicker7c7b6f92020-04-03 12:57:55 +05307021 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007022}
7023
7024static char* in_get_parameters(const struct audio_stream *stream,
7025 const char *keys)
7026{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007027 struct stream_in *in = (struct stream_in *)stream;
7028 struct str_parms *query = str_parms_create_str(keys);
7029 char *str;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007030 struct str_parms *reply = str_parms_create();
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07007031
7032 if (!query || !reply) {
Alexy Josephaee4fdd2016-01-29 13:02:07 -08007033 if (reply) {
7034 str_parms_destroy(reply);
7035 }
7036 if (query) {
7037 str_parms_destroy(query);
7038 }
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07007039 ALOGE("in_get_parameters: failed to create query or reply");
7040 return NULL;
7041 }
7042
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007043 ALOGV("%s: enter: keys - %s %s ", __func__, use_case_table[in->usecase], keys);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007044
7045 voice_extn_in_get_parameters(in, query, reply);
7046
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007047 stream_get_parameter_channels(query, reply,
7048 &in->supported_channel_masks[0]);
7049 stream_get_parameter_formats(query, reply,
7050 &in->supported_formats[0]);
7051 stream_get_parameter_rates(query, reply,
7052 &in->supported_sample_rates[0]);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007053 str = str_parms_to_str(reply);
7054 str_parms_destroy(query);
7055 str_parms_destroy(reply);
7056
7057 ALOGV("%s: exit: returns - %s", __func__, str);
7058 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007059}
7060
Aalique Grahame22e49102018-12-18 14:23:57 -08007061static int in_set_gain(struct audio_stream_in *stream,
7062 float gain)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007063{
Aalique Grahame22e49102018-12-18 14:23:57 -08007064 struct stream_in *in = (struct stream_in *)stream;
7065 char mixer_ctl_name[128];
7066 struct mixer_ctl *ctl;
7067 int ctl_value;
7068
7069 ALOGV("%s: gain %f", __func__, gain);
7070
7071 if (stream == NULL)
7072 return -EINVAL;
7073
7074 /* in_set_gain() only used to silence MMAP capture for now */
7075 if (in->usecase != USECASE_AUDIO_RECORD_MMAP)
7076 return -ENOSYS;
7077
7078 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name), "Capture %d Volume", in->pcm_device_id);
7079
7080 ctl = mixer_get_ctl_by_name(in->dev->mixer, mixer_ctl_name);
7081 if (!ctl) {
7082 ALOGW("%s: Could not get ctl for mixer cmd - %s",
7083 __func__, mixer_ctl_name);
7084 return -ENOSYS;
7085 }
7086
7087 if (gain < RECORD_GAIN_MIN)
7088 gain = RECORD_GAIN_MIN;
7089 else if (gain > RECORD_GAIN_MAX)
7090 gain = RECORD_GAIN_MAX;
7091 ctl_value = (int)(RECORD_VOLUME_CTL_MAX * gain);
7092
7093 mixer_ctl_set_value(ctl, 0, ctl_value);
7094
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007095 return 0;
7096}
7097
7098static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
7099 size_t bytes)
7100{
7101 struct stream_in *in = (struct stream_in *)stream;
Pallavid7c7a272018-01-16 11:22:55 +05307102
7103 if (in == NULL) {
7104 ALOGE("%s: stream_in ptr is NULL", __func__);
7105 return -EINVAL;
7106 }
7107
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007108 struct audio_device *adev = in->dev;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05307109 int ret = -1;
Dhananjay Kumar97b81e32019-05-15 21:00:21 +05307110 size_t bytes_read = 0, frame_size = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007111
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07007112 lock_input_stream(in);
Naresh Tanniru4c630392014-05-12 01:05:52 +05307113
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07007114 if (in->is_st_session) {
7115 ALOGVV(" %s: reading on st session bytes=%zu", __func__, bytes);
7116 /* Read from sound trigger HAL */
7117 audio_extn_sound_trigger_read(in, buffer, bytes);
Quinn Malef6050362019-01-30 15:55:40 -08007118 if (in->standby) {
George Gao3018ede2019-10-23 13:23:00 -07007119 if (adev->num_va_sessions < UINT_MAX)
7120 adev->num_va_sessions++;
Quinn Malef6050362019-01-30 15:55:40 -08007121 in->standby = 0;
7122 }
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07007123 pthread_mutex_unlock(&in->lock);
7124 return bytes;
7125 }
7126
Haynes Mathew George16081042017-05-31 17:16:49 -07007127 if (in->usecase == USECASE_AUDIO_RECORD_MMAP) {
7128 ret = -ENOSYS;
7129 goto exit;
7130 }
7131
Aniket Kumar Lata60586a92019-05-22 22:18:55 -07007132 if (in->usecase == USECASE_AUDIO_RECORD_LOW_LATENCY &&
7133 !in->standby && adev->adm_routing_changed) {
7134 ret = -ENOSYS;
7135 goto exit;
7136 }
7137
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007138 if (in->standby) {
Andy Hungc6bfd4a2019-07-01 18:26:00 -07007139 const int64_t startNs = systemTime(SYSTEM_TIME_MONOTONIC);
7140
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07007141 pthread_mutex_lock(&adev->lock);
7142 if (in->usecase == USECASE_COMPRESS_VOIP_CALL)
7143 ret = voice_extn_compress_voip_start_input_stream(in);
7144 else
7145 ret = start_input_stream(in);
George Gao3018ede2019-10-23 13:23:00 -07007146 if (!ret && in->source == AUDIO_SOURCE_VOICE_RECOGNITION) {
7147 if (adev->num_va_sessions < UINT_MAX)
7148 adev->num_va_sessions++;
7149 }
Daniel Hillenbrand21752f02013-05-23 10:10:00 +05307150
7151 if (ret == 0)
7152 amplifier_input_stream_start(stream);
7153
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07007154 pthread_mutex_unlock(&adev->lock);
7155 if (ret != 0) {
7156 goto exit;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007157 }
7158 in->standby = 0;
Andy Hungc6bfd4a2019-07-01 18:26:00 -07007159
7160 // log startup time in ms.
7161 simple_stats_log(
7162 &in->start_latency_ms, (systemTime(SYSTEM_TIME_MONOTONIC) - startNs) * 1e-6);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007163 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007164
Deeraj Soman30cc7ae2019-03-18 16:26:55 +05307165 /* Avoid read if capture_stopped is set */
7166 if (android_atomic_acquire_load(&(in->capture_stopped)) > 0) {
7167 ALOGD("%s: force stopped catpure session, ignoring read request", __func__);
7168 ret = -EINVAL;
7169 goto exit;
7170 }
7171
Haynes Mathew George5beddd42016-06-27 18:33:40 -07007172 // what's the duration requested by the client?
7173 long ns = 0;
7174
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307175 if (in->pcm && in->config.rate)
Haynes Mathew George5beddd42016-06-27 18:33:40 -07007176 ns = pcm_bytes_to_frames(in->pcm, bytes)*1000000000LL/
7177 in->config.rate;
7178
Aniket Kumar Lata60586a92019-05-22 22:18:55 -07007179 ret = request_in_focus(in, ns);
7180 if (ret != 0)
7181 goto exit;
Haynes Mathew George5beddd42016-06-27 18:33:40 -07007182 bool use_mmap = is_mmap_usecase(in->usecase) || in->realtime;
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07007183
Dhananjay Kumar7dbe3562019-08-30 05:49:33 +05307184 if (audio_extn_cin_attached_usecase(in)) {
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307185 ret = audio_extn_cin_read(in, buffer, bytes, &bytes_read);
7186 } else if (in->pcm) {
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05307187 if (audio_extn_ssr_get_stream() == in) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07007188 ret = audio_extn_ssr_read(stream, buffer, bytes);
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05307189 } else if (audio_extn_compr_cap_usecase_supported(in->usecase)) {
Mingming Yine62d7842013-10-25 16:26:03 -07007190 ret = audio_extn_compr_cap_read(in, buffer, bytes);
Haynes Mathew George5beddd42016-06-27 18:33:40 -07007191 } else if (use_mmap) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07007192 ret = pcm_mmap_read(in->pcm, buffer, bytes);
Garmond Leunge2433c32017-09-28 21:51:22 -07007193 } else if (audio_extn_ffv_get_stream() == in) {
7194 ret = audio_extn_ffv_read(stream, buffer, bytes);
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05307195 } else {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07007196 ret = pcm_read(in->pcm, buffer, bytes);
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307197 /* data from DSP comes in 24_8 format, convert it to 8_24 */
7198 if (!ret && bytes > 0 && (in->format == AUDIO_FORMAT_PCM_8_24_BIT)) {
7199 if (audio_extn_utils_convert_format_24_8_to_8_24(buffer, bytes)
7200 != bytes) {
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05307201 ret = -EINVAL;
7202 goto exit;
7203 }
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307204 } else if (ret < 0) {
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05307205 ret = -errno;
7206 }
7207 }
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307208 /* bytes read is always set to bytes for non compress usecases */
7209 bytes_read = bytes;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007210 }
7211
Haynes Mathew George5beddd42016-06-27 18:33:40 -07007212 release_in_focus(in);
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07007213
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007214 /*
Quinn Malef6050362019-01-30 15:55:40 -08007215 * Instead of writing zeroes here, we could trust the hardware to always
7216 * provide zeroes when muted. This is also muted with voice recognition
7217 * usecases so that other clients do not have access to voice recognition
7218 * data.
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007219 */
Quinn Malef6050362019-01-30 15:55:40 -08007220 if ((ret == 0 && voice_get_mic_mute(adev) &&
7221 !voice_is_in_call_rec_stream(in) &&
Zhou Song62ea0282020-03-22 19:53:01 +08007222 (in->usecase != USECASE_AUDIO_RECORD_AFE_PROXY &&
7223 in->usecase != USECASE_AUDIO_RECORD_AFE_PROXY2)) ||
Quinn Malef6050362019-01-30 15:55:40 -08007224 (adev->num_va_sessions &&
7225 in->source != AUDIO_SOURCE_VOICE_RECOGNITION &&
7226 property_get_bool("persist.vendor.audio.va_concurrency_mute_enabled",
7227 false)))
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007228 memset(buffer, 0, bytes);
7229
7230exit:
Dhananjay Kumar97b81e32019-05-15 21:00:21 +05307231 frame_size = audio_stream_in_frame_size(stream);
7232 if (frame_size > 0)
7233 in->frames_read += bytes_read/frame_size;
7234
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07007235 if (-ENETRESET == ret)
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05307236 in->card_status = CARD_STATUS_OFFLINE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007237 pthread_mutex_unlock(&in->lock);
7238
7239 if (ret != 0) {
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05307240 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05307241 pthread_mutex_lock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05307242 voice_extn_compress_voip_close_input_stream(&in->stream.common);
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05307243 pthread_mutex_unlock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05307244 in->standby = true;
7245 }
Dhananjay Kumar7dbe3562019-08-30 05:49:33 +05307246 if (!audio_extn_cin_attached_usecase(in)) {
Sharad Sangled17c9122017-03-20 15:58:52 +05307247 bytes_read = bytes;
7248 memset(buffer, 0, bytes);
7249 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007250 in_standby(&in->stream.common);
Aniket Kumar Lata60586a92019-05-22 22:18:55 -07007251 if (in->usecase == USECASE_AUDIO_RECORD_LOW_LATENCY)
7252 adev->adm_routing_changed = false;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07007253 ALOGV("%s: read failed status %d- sleeping for buffer duration", __func__, ret);
Ashish Jainbbce4322016-02-16 13:25:27 +05307254 usleep((uint64_t)bytes * 1000000 / audio_stream_in_frame_size(stream) /
Naresh Tanniru4c630392014-05-12 01:05:52 +05307255 in_get_sample_rate(&in->stream.common));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007256 }
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307257 return bytes_read;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007258}
7259
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07007260static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007261{
7262 return 0;
7263}
7264
Aalique Grahame22e49102018-12-18 14:23:57 -08007265static int in_get_capture_position(const struct audio_stream_in *stream,
7266 int64_t *frames, int64_t *time)
7267{
7268 if (stream == NULL || frames == NULL || time == NULL) {
7269 return -EINVAL;
7270 }
7271 struct stream_in *in = (struct stream_in *)stream;
7272 int ret = -ENOSYS;
7273
7274 lock_input_stream(in);
7275 // note: ST sessions do not close the alsa pcm driver synchronously
7276 // on standby. Therefore, we may return an error even though the
7277 // pcm stream is still opened.
7278 if (in->standby) {
7279 ALOGE_IF(in->pcm != NULL && !in->is_st_session,
7280 "%s stream in standby but pcm not NULL for non ST session", __func__);
7281 goto exit;
7282 }
7283 if (in->pcm) {
7284 struct timespec timestamp;
7285 unsigned int avail;
7286 if (pcm_get_htimestamp(in->pcm, &avail, &timestamp) == 0) {
7287 *frames = in->frames_read + avail;
Robert Lee58215542019-07-15 20:55:12 +08007288 *time = timestamp.tv_sec * 1000000000LL + timestamp.tv_nsec
George Gao9ba8a142020-07-23 14:30:03 -07007289 - platform_capture_latency(in) * 1000LL;
Aalique Grahame22e49102018-12-18 14:23:57 -08007290 ret = 0;
7291 }
7292 }
7293exit:
7294 pthread_mutex_unlock(&in->lock);
7295 return ret;
7296}
7297
Carter Hsu2e429db2019-05-14 18:50:52 +08007298static int in_update_effect_list(bool add, effect_handle_t effect,
7299 struct listnode *head)
7300{
7301 struct listnode *node;
7302 struct in_effect_list *elist = NULL;
7303 struct in_effect_list *target = NULL;
7304 int ret = 0;
7305
7306 if (!head)
7307 return ret;
7308
7309 list_for_each(node, head) {
7310 elist = node_to_item(node, struct in_effect_list, list);
7311 if (elist->handle == effect) {
7312 target = elist;
7313 break;
7314 }
7315 }
7316
7317 if (add) {
7318 if (target) {
7319 ALOGD("effect %p already exist", effect);
7320 return ret;
7321 }
7322
7323 target = (struct in_effect_list *)
7324 calloc(1, sizeof(struct in_effect_list));
7325
7326 if (!target) {
7327 ALOGE("%s:fail to allocate memory", __func__);
7328 return -ENOMEM;
7329 }
7330
7331 target->handle = effect;
7332 list_add_tail(head, &target->list);
7333 } else {
7334 if (target) {
7335 list_remove(&target->list);
7336 free(target);
7337 }
7338 }
7339
7340 return ret;
7341}
7342
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007343static int add_remove_audio_effect(const struct audio_stream *stream,
7344 effect_handle_t effect,
7345 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007346{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007347 struct stream_in *in = (struct stream_in *)stream;
7348 int status = 0;
7349 effect_descriptor_t desc;
7350
7351 status = (*effect)->get_descriptor(effect, &desc);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07007352 ALOGV("%s: status %d in->standby %d enable:%d", __func__, status, in->standby, enable);
7353
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007354 if (status != 0)
7355 return status;
7356
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07007357 lock_input_stream(in);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007358 pthread_mutex_lock(&in->dev->lock);
kunleizd96526c2018-04-09 11:12:32 +08007359 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
Carter Hsu2e429db2019-05-14 18:50:52 +08007360 in->source == AUDIO_SOURCE_VOICE_RECOGNITION ||
7361 adev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007362 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
Carter Hsu2e429db2019-05-14 18:50:52 +08007363
7364 in_update_effect_list(enable, effect, &in->aec_list);
7365 enable = !list_empty(&in->aec_list);
7366 if (enable == in->enable_aec)
7367 goto exit;
7368
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007369 in->enable_aec = enable;
Carter Hsu2e429db2019-05-14 18:50:52 +08007370 ALOGD("AEC enable %d", enable);
7371
Aalique Grahame22e49102018-12-18 14:23:57 -08007372 if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
7373 in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) {
7374 in->dev->enable_voicerx = enable;
7375 struct audio_usecase *usecase;
7376 struct listnode *node;
7377 list_for_each(node, &in->dev->usecase_list) {
7378 usecase = node_to_item(node, struct audio_usecase, list);
7379 if (usecase->type == PCM_PLAYBACK)
7380 select_devices(in->dev, usecase->id);
7381 }
7382 }
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07007383 if (!in->standby) {
7384 if (enable_disable_effect(in->dev, EFFECT_AEC, enable) == ENOSYS)
7385 select_devices(in->dev, in->usecase);
7386 }
7387
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007388 }
Carter Hsu2e429db2019-05-14 18:50:52 +08007389 if (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0) {
7390
7391 in_update_effect_list(enable, effect, &in->ns_list);
7392 enable = !list_empty(&in->ns_list);
7393 if (enable == in->enable_ns)
7394 goto exit;
7395
Ravi Kumar Alamanda198185e2013-11-07 15:42:19 -08007396 in->enable_ns = enable;
Carter Hsu2e429db2019-05-14 18:50:52 +08007397 ALOGD("NS enable %d", enable);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07007398 if (!in->standby) {
kunleizd96526c2018-04-09 11:12:32 +08007399 if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
7400 in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) {
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07007401 if (enable_disable_effect(in->dev, EFFECT_NS, enable) == ENOSYS)
7402 select_devices(in->dev, in->usecase);
7403 } else
7404 select_devices(in->dev, in->usecase);
7405 }
Ravi Kumar Alamanda198185e2013-11-07 15:42:19 -08007406 }
Carter Hsu2e429db2019-05-14 18:50:52 +08007407exit:
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007408 pthread_mutex_unlock(&in->dev->lock);
7409 pthread_mutex_unlock(&in->lock);
7410
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007411 return 0;
7412}
7413
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007414static int in_add_audio_effect(const struct audio_stream *stream,
7415 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007416{
Eric Laurent994a6932013-07-17 11:51:42 -07007417 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007418 return add_remove_audio_effect(stream, effect, true);
7419}
7420
7421static int in_remove_audio_effect(const struct audio_stream *stream,
7422 effect_handle_t effect)
7423{
Eric Laurent994a6932013-07-17 11:51:42 -07007424 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07007425 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007426}
7427
Derek Chenf939fb72018-11-13 13:34:41 -08007428streams_input_ctxt_t *in_get_stream(struct audio_device *dev,
7429 audio_io_handle_t input)
7430{
7431 struct listnode *node;
7432
7433 list_for_each(node, &dev->active_inputs_list) {
7434 streams_input_ctxt_t *in_ctxt = node_to_item(node,
7435 streams_input_ctxt_t,
7436 list);
7437 if (in_ctxt->input->capture_handle == input) {
7438 return in_ctxt;
7439 }
7440 }
7441 return NULL;
7442}
7443
7444streams_output_ctxt_t *out_get_stream(struct audio_device *dev,
7445 audio_io_handle_t output)
7446{
7447 struct listnode *node;
7448
7449 list_for_each(node, &dev->active_outputs_list) {
7450 streams_output_ctxt_t *out_ctxt = node_to_item(node,
7451 streams_output_ctxt_t,
7452 list);
7453 if (out_ctxt->output->handle == output) {
7454 return out_ctxt;
7455 }
7456 }
7457 return NULL;
7458}
7459
Haynes Mathew George16081042017-05-31 17:16:49 -07007460static int in_stop(const struct audio_stream_in* stream)
7461{
7462 struct stream_in *in = (struct stream_in *)stream;
7463 struct audio_device *adev = in->dev;
7464
7465 int ret = -ENOSYS;
7466 ALOGV("%s", __func__);
7467 pthread_mutex_lock(&adev->lock);
7468 if (in->usecase == USECASE_AUDIO_RECORD_MMAP && !in->standby &&
7469 in->capture_started && in->pcm != NULL) {
7470 pcm_stop(in->pcm);
7471 ret = stop_input_stream(in);
7472 in->capture_started = false;
7473 }
7474 pthread_mutex_unlock(&adev->lock);
7475 return ret;
7476}
7477
7478static int in_start(const struct audio_stream_in* stream)
7479{
7480 struct stream_in *in = (struct stream_in *)stream;
7481 struct audio_device *adev = in->dev;
7482 int ret = -ENOSYS;
7483
7484 ALOGV("%s in %p", __func__, in);
7485 pthread_mutex_lock(&adev->lock);
7486 if (in->usecase == USECASE_AUDIO_RECORD_MMAP && !in->standby &&
7487 !in->capture_started && in->pcm != NULL) {
7488 if (!in->capture_started) {
7489 ret = start_input_stream(in);
7490 if (ret == 0) {
7491 in->capture_started = true;
7492 }
7493 }
7494 }
7495 pthread_mutex_unlock(&adev->lock);
7496 return ret;
7497}
7498
Phil Burke0a86d12019-02-16 22:28:11 -08007499// Read offset for the positional timestamp from a persistent vendor property.
7500// This is to workaround apparent inaccuracies in the timing information that
7501// is used by the AAudio timing model. The inaccuracies can cause glitches.
7502static int64_t in_get_mmap_time_offset() {
7503 const int32_t kDefaultOffsetMicros = 0;
7504 int32_t mmap_time_offset_micros = property_get_int32(
Weiyin Jiangbd68b4d2019-05-17 16:29:50 +08007505 "persist.vendor.audio.in_mmap_delay_micros", kDefaultOffsetMicros);
Phil Burke0a86d12019-02-16 22:28:11 -08007506 ALOGI("mmap_time_offset_micros = %d for input", mmap_time_offset_micros);
7507 return mmap_time_offset_micros * (int64_t)1000;
7508}
7509
Haynes Mathew George16081042017-05-31 17:16:49 -07007510static int in_create_mmap_buffer(const struct audio_stream_in *stream,
7511 int32_t min_size_frames,
7512 struct audio_mmap_buffer_info *info)
7513{
7514 struct stream_in *in = (struct stream_in *)stream;
7515 struct audio_device *adev = in->dev;
7516 int ret = 0;
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07007517 unsigned int offset1 = 0;
7518 unsigned int frames1 = 0;
Haynes Mathew George16081042017-05-31 17:16:49 -07007519 const char *step = "";
Arun Mirpuri5d170872019-03-26 13:21:31 -07007520 uint32_t mmap_size = 0;
7521 uint32_t buffer_size = 0;
Haynes Mathew George16081042017-05-31 17:16:49 -07007522
7523 pthread_mutex_lock(&adev->lock);
7524 ALOGV("%s in %p", __func__, in);
7525
Sharad Sanglec6f32552018-05-04 16:15:38 +05307526 if (CARD_STATUS_OFFLINE == in->card_status||
7527 CARD_STATUS_OFFLINE == adev->card_status) {
7528 ALOGW("in->card_status or adev->card_status offline, try again");
7529 ret = -EIO;
7530 goto exit;
7531 }
7532
Sujin Panicker7c7b6f92020-04-03 12:57:55 +05307533 if (info == NULL || !(min_size_frames > 0 && min_size_frames < INT32_MAX)) {
Haynes Mathew George16081042017-05-31 17:16:49 -07007534 ALOGE("%s invalid argument info %p min_size_frames %d", __func__, info, min_size_frames);
7535 ret = -EINVAL;
7536 goto exit;
7537 }
7538 if (in->usecase != USECASE_AUDIO_RECORD_MMAP || !in->standby) {
7539 ALOGE("%s: usecase = %d, standby = %d", __func__, in->usecase, in->standby);
7540 ALOGV("%s in %p", __func__, in);
7541 ret = -ENOSYS;
7542 goto exit;
7543 }
7544 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
7545 if (in->pcm_device_id < 0) {
7546 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
7547 __func__, in->pcm_device_id, in->usecase);
7548 ret = -EINVAL;
7549 goto exit;
7550 }
7551
7552 adjust_mmap_period_count(&in->config, min_size_frames);
7553
7554 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
7555 __func__, adev->snd_card, in->pcm_device_id, in->config.channels);
7556 in->pcm = pcm_open(adev->snd_card, in->pcm_device_id,
7557 (PCM_IN | PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC), &in->config);
Satish Babu Patakokila54ce83d2018-07-06 18:00:37 +05307558 if (errno == ENETRESET && !pcm_is_ready(in->pcm)) {
Sharad Sanglec6f32552018-05-04 16:15:38 +05307559 ALOGE("%s: pcm_open failed errno:%d\n", __func__, errno);
7560 in->card_status = CARD_STATUS_OFFLINE;
7561 adev->card_status = CARD_STATUS_OFFLINE;
7562 ret = -EIO;
7563 goto exit;
7564 }
7565
Haynes Mathew George16081042017-05-31 17:16:49 -07007566 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) {
7567 step = "open";
7568 ret = -ENODEV;
7569 goto exit;
7570 }
7571
7572 ret = pcm_mmap_begin(in->pcm, &info->shared_memory_address, &offset1, &frames1);
7573 if (ret < 0) {
7574 step = "begin";
7575 goto exit;
7576 }
Haynes Mathew George16081042017-05-31 17:16:49 -07007577
juyuchen626833d2019-06-04 16:48:02 +08007578 info->flags = 0;
Arun Mirpuri5d170872019-03-26 13:21:31 -07007579 info->buffer_size_frames = pcm_get_buffer_size(in->pcm);
7580 buffer_size = pcm_frames_to_bytes(in->pcm, info->buffer_size_frames);
7581 info->burst_size_frames = in->config.period_size;
7582 ret = platform_get_mmap_data_fd(adev->platform,
7583 in->pcm_device_id, 1 /*capture*/,
7584 &info->shared_memory_fd,
7585 &mmap_size);
7586 if (ret < 0) {
7587 // Fall back to non exclusive mode
7588 info->shared_memory_fd = pcm_get_poll_fd(in->pcm);
7589 } else {
Phil Burkd898ba62019-06-20 12:49:01 -07007590 in->mmap_shared_memory_fd = info->shared_memory_fd; // for closing later
7591 ALOGV("%s: opened mmap_shared_memory_fd = %d", __func__, in->mmap_shared_memory_fd);
7592
Arun Mirpuri5d170872019-03-26 13:21:31 -07007593 if (mmap_size < buffer_size) {
7594 step = "mmap";
7595 goto exit;
7596 }
juyuchen626833d2019-06-04 16:48:02 +08007597 info->flags |= AUDIO_MMAP_APPLICATION_SHAREABLE;
Arun Mirpuri5d170872019-03-26 13:21:31 -07007598 }
7599
7600 memset(info->shared_memory_address, 0, buffer_size);
Haynes Mathew George16081042017-05-31 17:16:49 -07007601
7602 ret = pcm_mmap_commit(in->pcm, 0, MMAP_PERIOD_SIZE);
7603 if (ret < 0) {
7604 step = "commit";
7605 goto exit;
7606 }
7607
Phil Burke0a86d12019-02-16 22:28:11 -08007608 in->mmap_time_offset_nanos = in_get_mmap_time_offset();
7609
Haynes Mathew George16081042017-05-31 17:16:49 -07007610 in->standby = false;
7611 ret = 0;
7612
7613 ALOGV("%s: got mmap buffer address %p info->buffer_size_frames %d",
7614 __func__, info->shared_memory_address, info->buffer_size_frames);
7615
7616exit:
7617 if (ret != 0) {
7618 if (in->pcm == NULL) {
7619 ALOGE("%s: %s - %d", __func__, step, ret);
7620 } else {
7621 ALOGE("%s: %s %s", __func__, step, pcm_get_error(in->pcm));
7622 pcm_close(in->pcm);
7623 in->pcm = NULL;
7624 }
7625 }
7626 pthread_mutex_unlock(&adev->lock);
7627 return ret;
7628}
7629
7630static int in_get_mmap_position(const struct audio_stream_in *stream,
7631 struct audio_mmap_position *position)
7632{
7633 struct stream_in *in = (struct stream_in *)stream;
7634 ALOGVV("%s", __func__);
7635 if (position == NULL) {
7636 return -EINVAL;
7637 }
7638 if (in->usecase != USECASE_AUDIO_RECORD_MMAP) {
7639 return -ENOSYS;
7640 }
7641 if (in->pcm == NULL) {
7642 return -ENOSYS;
7643 }
7644 struct timespec ts = { 0, 0 };
7645 int ret = pcm_mmap_get_hw_ptr(in->pcm, (unsigned int *)&position->position_frames, &ts);
7646 if (ret < 0) {
7647 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
7648 return ret;
7649 }
Phil Burke0a86d12019-02-16 22:28:11 -08007650 position->time_nanoseconds = ts.tv_sec*1000000000LL + ts.tv_nsec
7651 + in->mmap_time_offset_nanos;
Haynes Mathew George16081042017-05-31 17:16:49 -07007652 return 0;
7653}
7654
Naresh Tannirudcb47c52018-06-25 16:23:32 +05307655static int in_get_active_microphones(const struct audio_stream_in *stream,
7656 struct audio_microphone_characteristic_t *mic_array,
7657 size_t *mic_count) {
7658 struct stream_in *in = (struct stream_in *)stream;
7659 struct audio_device *adev = in->dev;
7660 ALOGVV("%s", __func__);
7661
7662 lock_input_stream(in);
7663 pthread_mutex_lock(&adev->lock);
7664 int ret = platform_get_active_microphones(adev->platform,
7665 audio_channel_count_from_in_mask(in->channel_mask),
7666 in->usecase, mic_array, mic_count);
7667 pthread_mutex_unlock(&adev->lock);
7668 pthread_mutex_unlock(&in->lock);
7669
7670 return ret;
7671}
7672
7673static int adev_get_microphones(const struct audio_hw_device *dev,
7674 struct audio_microphone_characteristic_t *mic_array,
7675 size_t *mic_count) {
7676 struct audio_device *adev = (struct audio_device *)dev;
7677 ALOGVV("%s", __func__);
7678
7679 pthread_mutex_lock(&adev->lock);
7680 int ret = platform_get_microphones(adev->platform, mic_array, mic_count);
7681 pthread_mutex_unlock(&adev->lock);
7682
7683 return ret;
7684}
juyuchendb308c22019-01-21 11:57:17 -07007685
7686static void in_update_sink_metadata(struct audio_stream_in *stream,
7687 const struct sink_metadata *sink_metadata) {
7688
7689 if (stream == NULL
7690 || sink_metadata == NULL
7691 || sink_metadata->tracks == NULL) {
7692 return;
7693 }
7694
7695 int error = 0;
7696 struct stream_in *in = (struct stream_in *)stream;
7697 struct audio_device *adev = in->dev;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08007698 struct listnode devices;
Zhou Song62ea0282020-03-22 19:53:01 +08007699 bool is_ha_usecase = false;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08007700
7701 list_init(&devices);
juyuchendb308c22019-01-21 11:57:17 -07007702
7703 if (sink_metadata->track_count != 0)
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08007704 reassign_device_list(&devices, sink_metadata->tracks->dest_device, "");
juyuchendb308c22019-01-21 11:57:17 -07007705
7706 lock_input_stream(in);
7707 pthread_mutex_lock(&adev->lock);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08007708 ALOGV("%s: in->usecase: %d, device: %x", __func__, in->usecase, get_device_types(&devices));
juyuchendb308c22019-01-21 11:57:17 -07007709
Zhou Song62ea0282020-03-22 19:53:01 +08007710 is_ha_usecase = adev->ha_proxy_enable ?
7711 in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY2 :
7712 in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY;
7713 if (is_ha_usecase && !list_empty(&devices)
juyuchendb308c22019-01-21 11:57:17 -07007714 && adev->voice_tx_output != NULL) {
7715 /* Use the rx device from afe-proxy record to route voice call because
7716 there is no routing if tx device is on primary hal and rx device
7717 is on other hal during voice call. */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08007718 assign_devices(&adev->voice_tx_output->device_list, &devices);
juyuchendb308c22019-01-21 11:57:17 -07007719
7720 if (!voice_is_call_state_active(adev)) {
7721 if (adev->mode == AUDIO_MODE_IN_CALL) {
7722 adev->current_call_output = adev->voice_tx_output;
7723 error = voice_start_call(adev);
7724 if (error != 0)
7725 ALOGE("%s: start voice call failed %d", __func__, error);
7726 }
7727 } else {
7728 adev->current_call_output = adev->voice_tx_output;
7729 voice_update_devices_for_all_voice_usecases(adev);
7730 }
7731 }
7732
7733 pthread_mutex_unlock(&adev->lock);
7734 pthread_mutex_unlock(&in->lock);
7735}
7736
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05307737int adev_open_output_stream(struct audio_hw_device *dev,
Haynes Mathew George16081042017-05-31 17:16:49 -07007738 audio_io_handle_t handle,
7739 audio_devices_t devices,
7740 audio_output_flags_t flags,
7741 struct audio_config *config,
7742 struct audio_stream_out **stream_out,
Derek Chenf6318be2017-06-12 17:16:24 -04007743 const char *address)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007744{
7745 struct audio_device *adev = (struct audio_device *)dev;
7746 struct stream_out *out;
Gangadhar Sb0210342019-02-22 17:39:41 +05307747 int ret = 0, ip_hdlr_stream = 0, ip_hdlr_dev = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07007748 audio_format_t format;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08007749 struct adsp_hdlr_stream_cfg hdlr_stream_cfg;
Manish Dewangan21a850a2017-08-14 12:03:55 +05307750 bool is_direct_passthough = false;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007751 bool is_hdmi = devices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
7752 bool is_usb_dev = audio_is_usb_out_device(devices) &&
7753 (devices != AUDIO_DEVICE_OUT_USB_ACCESSORY);
7754 bool direct_dev = is_hdmi || is_usb_dev;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08007755 bool use_db_as_primary =
vivek mehtaae1018c2019-05-09 12:19:57 -07007756 property_get_bool("vendor.audio.feature.deepbuffer_as_primary.enable",
7757 false);
Vignesh Kulothungana6927272019-02-20 15:17:07 -08007758 bool force_haptic_path =
7759 property_get_bool("vendor.audio.test_haptic", false);
Aniket Kumar Lata23d8cbf2019-04-10 19:54:46 -07007760 bool is_voip_rx = flags & AUDIO_OUTPUT_FLAG_VOIP_RX;
Xiaojun Sang782e5b12020-06-29 21:13:06 +08007761#ifdef AUDIO_GKI_ENABLED
7762 __s32 *generic_dec;
7763#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007764
kunleizdff872d2018-08-20 14:40:33 +08007765 if (is_usb_dev && (!audio_extn_usb_connected(NULL))) {
kunleizd6a9e0c2018-07-30 15:38:52 +08007766 is_usb_dev = false;
7767 devices = AUDIO_DEVICE_OUT_SPEAKER;
7768 ALOGW("%s: ignore set device to non existing USB card, use output device(%#x)",
7769 __func__, devices);
Mingshu Pangdbd20562019-11-25 18:04:39 +08007770 if (config->format == AUDIO_FORMAT_DEFAULT)
7771 config->format = AUDIO_FORMAT_PCM_16_BIT;
7772 if (config->sample_rate == 0)
7773 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
7774 if (config->channel_mask == AUDIO_CHANNEL_NONE)
7775 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
kunleizd6a9e0c2018-07-30 15:38:52 +08007776 }
7777
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007778 *stream_out = NULL;
Naresh Tanniru80659832014-06-04 18:17:56 +05307779
Rahul Sharma99770982019-03-06 17:05:26 +05307780 pthread_mutex_lock(&adev->lock);
7781 if (out_get_stream(adev, handle) != NULL) {
7782 ALOGW("%s, output stream already opened", __func__);
7783 ret = -EEXIST;
7784 }
7785 pthread_mutex_unlock(&adev->lock);
7786 if (ret)
7787 return ret;
7788
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007789 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
7790
Mingming Yin3a941d42016-02-17 18:08:05 -08007791 ALOGD("%s: enter: format(%#x) sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)\
Derek Chenf6318be2017-06-12 17:16:24 -04007792 stream_handle(%p) address(%s)", __func__, config->format, config->sample_rate, config->channel_mask,
7793 devices, flags, &out->stream, address);
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05307794
7795
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08007796 if (!out) {
7797 return -ENOMEM;
7798 }
7799
Haynes Mathew George204045b2015-02-25 20:32:03 -08007800 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07007801 pthread_mutex_init(&out->pre_lock, (const pthread_mutexattr_t *) NULL);
Weiyin Jiangd5974e62020-09-08 20:28:22 +08007802 pthread_mutex_init(&out->latch_lock, (const pthread_mutexattr_t *) NULL);
Zhou Song48453a02018-01-10 17:50:59 +08007803 pthread_mutex_init(&out->position_query_lock, (const pthread_mutexattr_t *) NULL);
Haynes Mathew George204045b2015-02-25 20:32:03 -08007804 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
7805
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007806 if (devices == AUDIO_DEVICE_NONE)
7807 devices = AUDIO_DEVICE_OUT_SPEAKER;
7808
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007809 out->flags = flags;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08007810 list_init(&out->device_list);
7811 update_device_list(&out->device_list, devices, address, true /* add devices */);
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07007812 out->dev = adev;
Aalique Grahame65780b52017-09-27 14:59:56 -07007813 out->hal_op_format = out->hal_ip_format = format = out->format = config->format;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07007814 out->sample_rate = config->sample_rate;
Sachin Mohan Gadag3d09acd2017-06-19 12:43:44 +05307815 out->channel_mask = config->channel_mask;
Ramjee Singh5857aeb2017-08-03 19:18:50 +05307816 if (out->channel_mask == AUDIO_CHANNEL_NONE)
7817 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
7818 else
7819 out->supported_channel_masks[0] = out->channel_mask;
Eric Laurentc4aef752013-09-12 17:45:53 -07007820 out->handle = handle;
Mingming Yin3ee55c62014-08-04 14:23:35 -07007821 out->bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Alexy Josephaa54c872014-12-03 02:46:47 -08007822 out->non_blocking = 0;
Ashish Jain83a6cc22016-06-28 14:34:17 +05307823 out->convert_buffer = NULL;
Ashish Jain1b9b30c2017-05-18 20:57:40 +05307824 out->started = 0;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05307825 out->a2dp_compress_mute = false;
Aniket Kumar Lata932f4872017-11-06 18:29:44 -08007826 out->hal_output_suspend_supported = 0;
7827 out->dynamic_pm_qos_config_supported = 0;
Surendar Karkaf51b5842018-04-26 11:28:38 +05307828 out->set_dual_mono = false;
Manisha Agarwal7b3e3772019-02-20 14:33:45 +05307829 out->prev_card_status_offline = false;
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +05307830 out->pspd_coeff_sent = false;
Phil Burkd898ba62019-06-20 12:49:01 -07007831 out->mmap_shared_memory_fd = -1; // not open
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007832
Nikhil Laturkar26b690b2017-07-25 11:06:14 +05307833 if ((flags & AUDIO_OUTPUT_FLAG_BD) &&
Satish Babu Patakokila37e7c482018-02-02 11:50:06 +05307834 (property_get_bool("vendor.audio.matrix.limiter.enable", false)))
Ben Romberger6c4d3812017-06-13 17:46:45 -07007835 platform_set_device_params(out, DEVICE_PARAM_LIMITER_ID, 1);
7836
Aalique Grahame22e49102018-12-18 14:23:57 -08007837 if (direct_dev &&
7838 (audio_is_linear_pcm(out->format) ||
7839 config->format == AUDIO_FORMAT_DEFAULT) &&
7840 out->flags == AUDIO_OUTPUT_FLAG_NONE) {
7841 audio_format_t req_format = config->format;
7842 audio_channel_mask_t req_channel_mask = config->channel_mask;
7843 uint32_t req_sample_rate = config->sample_rate;
7844
7845 pthread_mutex_lock(&adev->lock);
7846 if (is_hdmi) {
7847 ALOGV("AUDIO_DEVICE_OUT_AUX_DIGITAL and DIRECT|OFFLOAD, check hdmi caps");
7848 ret = read_hdmi_sink_caps(out);
7849 if (config->sample_rate == 0)
7850 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
7851 if (config->channel_mask == AUDIO_CHANNEL_NONE)
7852 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
7853 if (config->format == AUDIO_FORMAT_DEFAULT)
7854 config->format = AUDIO_FORMAT_PCM_16_BIT;
7855 } else if (is_usb_dev) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007856 ret = read_usb_sup_params_and_compare(true /*is_playback*/,
7857 &config->format,
7858 &out->supported_formats[0],
7859 MAX_SUPPORTED_FORMATS,
7860 &config->channel_mask,
7861 &out->supported_channel_masks[0],
7862 MAX_SUPPORTED_CHANNEL_MASKS,
7863 &config->sample_rate,
7864 &out->supported_sample_rates[0],
7865 MAX_SUPPORTED_SAMPLE_RATES);
7866 ALOGV("plugged dev USB ret %d", ret);
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007867 }
Aalique Grahame22e49102018-12-18 14:23:57 -08007868
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007869 pthread_mutex_unlock(&adev->lock);
7870 if (ret != 0) {
Mingming Yin3a941d42016-02-17 18:08:05 -08007871 if (ret == -ENOSYS) {
7872 /* ignore and go with default */
7873 ret = 0;
Aalique Grahame22e49102018-12-18 14:23:57 -08007874 }
7875 // For MMAP NO IRQ, allow conversions in ADSP
7876 else if (is_hdmi || (flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) == 0)
7877 goto error_open;
7878 else {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007879 ALOGE("error reading direct dev sink caps");
Mingming Yin3a941d42016-02-17 18:08:05 -08007880 goto error_open;
7881 }
Aalique Grahame22e49102018-12-18 14:23:57 -08007882
7883 if (req_sample_rate != 0 && config->sample_rate != req_sample_rate)
7884 config->sample_rate = req_sample_rate;
7885 if (req_channel_mask != AUDIO_CHANNEL_NONE && config->channel_mask != req_channel_mask)
7886 config->channel_mask = req_channel_mask;
7887 if (req_format != AUDIO_FORMAT_DEFAULT && config->format != req_format)
7888 config->format = req_format;
Mingming Yin3a941d42016-02-17 18:08:05 -08007889 }
Aalique Grahame22e49102018-12-18 14:23:57 -08007890
7891 out->sample_rate = config->sample_rate;
7892 out->channel_mask = config->channel_mask;
7893 out->format = config->format;
7894 if (is_hdmi) {
7895 out->usecase = USECASE_AUDIO_PLAYBACK_HIFI;
7896 out->config = pcm_config_hdmi_multi;
7897 } else if (flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) {
7898 out->usecase = USECASE_AUDIO_PLAYBACK_MMAP;
7899 out->config = pcm_config_mmap_playback;
7900 out->stream.start = out_start;
7901 out->stream.stop = out_stop;
7902 out->stream.create_mmap_buffer = out_create_mmap_buffer;
7903 out->stream.get_mmap_position = out_get_mmap_position;
7904 } else {
7905 out->usecase = USECASE_AUDIO_PLAYBACK_HIFI;
7906 out->config = pcm_config_hifi;
7907 }
7908
7909 out->config.rate = out->sample_rate;
7910 out->config.channels = audio_channel_count_from_out_mask(out->channel_mask);
7911 if (is_hdmi) {
7912 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels *
7913 audio_bytes_per_sample(out->format));
7914 }
7915 out->config.format = pcm_format_from_audio_format(out->format);
Mingming Yin3a941d42016-02-17 18:08:05 -08007916 }
7917
Derek Chenf6318be2017-06-12 17:16:24 -04007918 /* validate bus device address */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08007919 if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_BUS)) {
Derek Chenf6318be2017-06-12 17:16:24 -04007920 /* extract car audio stream index */
7921 out->car_audio_stream =
7922 audio_extn_auto_hal_get_car_audio_stream_from_address(address);
7923 if (out->car_audio_stream < 0) {
7924 ALOGE("%s: invalid car audio stream %x",
7925 __func__, out->car_audio_stream);
7926 ret = -EINVAL;
7927 goto error_open;
7928 }
Derek Chen5f67a942020-02-24 23:08:13 -08007929 ALOGV("%s: car_audio_stream %x", __func__, out->car_audio_stream);
Derek Chenf6318be2017-06-12 17:16:24 -04007930 }
7931
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08007932 /* Check for VOIP usecase */
Aniket Kumar Lata23d8cbf2019-04-10 19:54:46 -07007933 if (is_voip_rx) {
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08007934 if (!voice_extn_is_compress_voip_supported()) {
7935 if (out->sample_rate == 8000 || out->sample_rate == 16000 ||
7936 out->sample_rate == 32000 || out->sample_rate == 48000) {
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07007937 out->channel_mask = audio_extn_utils_is_vendor_enhanced_fwk() ?
Lakshman Chaluvarajue7fc9482020-05-30 14:29:29 +05307938 config->channel_mask : AUDIO_CHANNEL_OUT_STEREO;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08007939 out->usecase = USECASE_AUDIO_PLAYBACK_VOIP;
7940 out->format = AUDIO_FORMAT_PCM_16_BIT;
Aniket Kumar Lata8426d1f2019-06-10 15:25:53 -07007941 out->volume_l = INVALID_OUT_VOLUME;
7942 out->volume_r = INVALID_OUT_VOLUME;
Vikram Panduranga93f080e2017-06-07 18:16:14 -07007943
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08007944 out->config = default_pcm_config_voip_copp;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08007945 out->config.rate = out->sample_rate;
Aniket Kumar Lata1e344f22019-06-19 17:09:01 -07007946 uint32_t channel_count =
7947 audio_channel_count_from_out_mask(out->channel_mask);
Ramjee Singh8e34a2f2020-08-06 16:30:48 +05307948 out->config.channels = channel_count;
7949
Aniket Kumar Lata1e344f22019-06-19 17:09:01 -07007950 uint32_t buffer_size = get_stream_buffer_size(DEFAULT_VOIP_BUF_DURATION_MS,
7951 out->sample_rate, out->format,
7952 channel_count, false);
7953 uint32_t frame_size = audio_bytes_per_sample(out->format) * channel_count;
7954 if (frame_size != 0)
7955 out->config.period_size = buffer_size / frame_size;
7956 else
7957 ALOGW("%s: frame size is 0 for format %#x", __func__, out->format);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08007958 }
7959 } else {
7960 if ((out->dev->mode == AUDIO_MODE_IN_COMMUNICATION ||
7961 voice_extn_compress_voip_is_active(out->dev)) &&
7962 (voice_extn_compress_voip_is_config_supported(config))) {
7963 ret = voice_extn_compress_voip_open_output_stream(out);
7964 if (ret != 0) {
7965 ALOGE("%s: Compress voip output cannot be opened, error:%d",
7966 __func__, ret);
7967 goto error_open;
7968 }
Sujin Panicker19027262019-09-16 18:28:06 +05307969 } else {
7970 out->usecase = GET_USECASE_AUDIO_PLAYBACK_PRIMARY(use_db_as_primary);
7971 out->config = GET_PCM_CONFIG_AUDIO_PLAYBACK_PRIMARY(use_db_as_primary);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08007972 }
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007973 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007974 } else if (audio_is_linear_pcm(out->format) &&
7975 out->flags == AUDIO_OUTPUT_FLAG_NONE && is_usb_dev) {
7976 out->channel_mask = config->channel_mask;
7977 out->sample_rate = config->sample_rate;
7978 out->format = config->format;
7979 out->usecase = USECASE_AUDIO_PLAYBACK_HIFI;
7980 // does this change?
7981 out->config = is_hdmi ? pcm_config_hdmi_multi : pcm_config_hifi;
7982 out->config.rate = config->sample_rate;
7983 out->config.channels = audio_channel_count_from_out_mask(out->channel_mask);
7984 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels *
7985 audio_bytes_per_sample(config->format));
7986 out->config.format = pcm_format_from_audio_format(out->format);
vivek mehta0ea887a2015-08-26 14:01:20 -07007987 } else if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
Dhananjay Kumarac341582017-02-23 23:42:25 +05307988 (out->flags == AUDIO_OUTPUT_FLAG_DIRECT)) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05307989 pthread_mutex_lock(&adev->lock);
7990 bool offline = (adev->card_status == CARD_STATUS_OFFLINE);
7991 pthread_mutex_unlock(&adev->lock);
7992
7993 // reject offload during card offline to allow
7994 // fallback to s/w paths
7995 if (offline) {
7996 ret = -ENODEV;
7997 goto error_open;
7998 }
vivek mehta0ea887a2015-08-26 14:01:20 -07007999
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008000 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
8001 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
8002 ALOGE("%s: Unsupported Offload information", __func__);
8003 ret = -EINVAL;
8004 goto error_open;
8005 }
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07008006
Atul Khare3fa6e542017-08-09 00:56:17 +05308007 if (config->offload_info.format == 0)
8008 config->offload_info.format = config->format;
8009 if (config->offload_info.sample_rate == 0)
8010 config->offload_info.sample_rate = config->sample_rate;
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07008011
Mingming Yin90310102013-11-13 16:57:00 -08008012 if (!is_supported_format(config->offload_info.format) &&
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05308013 !audio_extn_passthru_is_supported_format(config->offload_info.format)) {
vivek mehta0ea887a2015-08-26 14:01:20 -07008014 ALOGE("%s: Unsupported audio format %x " , __func__, config->offload_info.format);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008015 ret = -EINVAL;
8016 goto error_open;
8017 }
8018
Ben Romberger0f8c87b2017-05-24 17:41:11 -07008019 /* TrueHD only supported for 48k multiples (48k, 96k, 192k) */
8020 if ((config->offload_info.format == AUDIO_FORMAT_DOLBY_TRUEHD) &&
8021 (audio_extn_passthru_is_passthrough_stream(out)) &&
8022 !((config->sample_rate == 48000) ||
8023 (config->sample_rate == 96000) ||
8024 (config->sample_rate == 192000))) {
8025 ALOGE("%s: Unsupported sample rate %d for audio format %x",
8026 __func__, config->sample_rate, config->offload_info.format);
8027 ret = -EINVAL;
8028 goto error_open;
8029 }
8030
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008031 out->compr_config.codec = (struct snd_codec *)
8032 calloc(1, sizeof(struct snd_codec));
8033
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07008034 if (!out->compr_config.codec) {
8035 ret = -ENOMEM;
8036 goto error_open;
8037 }
8038
Dhananjay Kumarac341582017-02-23 23:42:25 +05308039 out->stream.pause = out_pause;
8040 out->stream.resume = out_resume;
8041 out->stream.flush = out_flush;
Ashish Jain4847e9d2017-08-17 19:16:57 +05308042 out->stream.set_callback = out_set_callback;
Dhananjay Kumarac341582017-02-23 23:42:25 +05308043 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Mingming Yin21d60472015-09-30 13:56:25 -07008044 out->stream.drain = out_drain;
Dhananjay Kumarac341582017-02-23 23:42:25 +05308045 out->usecase = get_offload_usecase(adev, true /* is_compress */);
vivek mehta446c3962015-09-14 10:57:35 -07008046 ALOGV("Compress Offload usecase .. usecase selected %d", out->usecase);
Dhananjay Kumarac341582017-02-23 23:42:25 +05308047 } else {
8048 out->usecase = get_offload_usecase(adev, false /* is_compress */);
8049 ALOGV("non-offload DIRECT_usecase ... usecase selected %d ", out->usecase);
vivek mehta0ea887a2015-08-26 14:01:20 -07008050 }
vivek mehta446c3962015-09-14 10:57:35 -07008051
Deeraj Somanfa377bf2019-02-06 12:57:59 +05308052 if (out->flags & AUDIO_OUTPUT_FLAG_FAST) {
8053 ALOGD("%s: Setting latency mode to true", __func__);
Meng Wang4c32fb42020-01-16 17:57:11 +08008054#ifdef AUDIO_GKI_ENABLED
8055 /* out->compr_config.codec->reserved[1] is for flags */
8056 out->compr_config.codec->reserved[1] |= audio_extn_utils_get_perf_mode_flag();
8057#else
Deeraj Somanfa377bf2019-02-06 12:57:59 +05308058 out->compr_config.codec->flags |= audio_extn_utils_get_perf_mode_flag();
Meng Wang4c32fb42020-01-16 17:57:11 +08008059#endif
Deeraj Somanfa377bf2019-02-06 12:57:59 +05308060 }
8061
vivek mehta446c3962015-09-14 10:57:35 -07008062 if (out->usecase == USECASE_INVALID) {
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08008063 if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_AUX_DIGITAL) &&
Mingming Yin3a941d42016-02-17 18:08:05 -08008064 config->format == 0 && config->sample_rate == 0 &&
8065 config->channel_mask == 0) {
Mingming Yin21854652016-04-13 11:54:02 -07008066 ALOGI("%s dummy open to query sink capability",__func__);
Mingming Yin3a941d42016-02-17 18:08:05 -08008067 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
8068 } else {
8069 ALOGE("%s, Max allowed OFFLOAD usecase reached ... ", __func__);
8070 ret = -EEXIST;
8071 goto error_open;
8072 }
vivek mehta446c3962015-09-14 10:57:35 -07008073 }
8074
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008075 if (config->offload_info.channel_mask)
8076 out->channel_mask = config->offload_info.channel_mask;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08008077 else if (config->channel_mask) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008078 out->channel_mask = config->channel_mask;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08008079 config->offload_info.channel_mask = config->channel_mask;
Haynes Mathew Georgea99f7532016-08-24 16:01:21 -07008080 } else {
Dhananjay Kumarac341582017-02-23 23:42:25 +05308081 ALOGE("out->channel_mask not set for OFFLOAD/DIRECT usecase");
Haynes Mathew Georgea99f7532016-08-24 16:01:21 -07008082 ret = -EINVAL;
8083 goto error_open;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08008084 }
Haynes Mathew Georgea99f7532016-08-24 16:01:21 -07008085
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07008086 format = out->format = config->offload_info.format;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008087 out->sample_rate = config->offload_info.sample_rate;
8088
Mingming Yin3ee55c62014-08-04 14:23:35 -07008089 out->bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008090
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05308091 out->compr_config.codec->id = get_snd_codec_id(config->offload_info.format);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05308092 if (audio_extn_utils_is_dolby_format(config->offload_info.format)) {
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05308093 audio_extn_dolby_send_ddp_endp_params(adev);
8094 audio_extn_dolby_set_dmid(adev);
8095 }
vivek mehta0ea887a2015-08-26 14:01:20 -07008096
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008097 out->compr_config.codec->sample_rate =
Ravi Kumar Alamandab91bff32014-11-14 12:05:54 -08008098 config->offload_info.sample_rate;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008099 out->compr_config.codec->bit_rate =
8100 config->offload_info.bit_rate;
8101 out->compr_config.codec->ch_in =
Dhanalakshmi Siddania15c6792016-08-10 15:33:53 +05308102 audio_channel_count_from_out_mask(out->channel_mask);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008103 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
Satish Babu Patakokilaa395a9e2016-11-01 12:18:49 +05308104 /* Update bit width only for non passthrough usecases.
8105 * For passthrough usecases, the output will always be opened @16 bit
8106 */
8107 if (!audio_extn_passthru_is_passthrough_stream(out))
8108 out->bit_width = AUDIO_OUTPUT_BIT_WIDTH;
Naresh Tanniruee3499a2017-01-05 14:05:35 +05308109
8110 if (out->flags & AUDIO_OUTPUT_FLAG_TIMESTAMP)
Meng Wang4c32fb42020-01-16 17:57:11 +08008111#ifdef AUDIO_GKI_ENABLED
8112 /* out->compr_config.codec->reserved[1] is for flags */
8113 out->compr_config.codec->reserved[1] |= COMPRESSED_TIMESTAMP_FLAG;
8114 ALOGVV("%s : out->compr_config.codec->flags -> (%#x) ", __func__, out->compr_config.codec->reserved[1]);
8115#else
Naresh Tanniruee3499a2017-01-05 14:05:35 +05308116 out->compr_config.codec->flags |= COMPRESSED_TIMESTAMP_FLAG;
8117 ALOGVV("%s : out->compr_config.codec->flags -> (%#x) ", __func__, out->compr_config.codec->flags);
Meng Wang4c32fb42020-01-16 17:57:11 +08008118#endif
Naresh Tanniruee3499a2017-01-05 14:05:35 +05308119
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07008120 /*TODO: Do we need to change it for passthrough */
8121 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_RAW;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008122
Manish Dewangana6fc5442015-08-24 20:30:31 +05308123 if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC)
8124 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_RAW;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05308125 else if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS)
Manish Dewangana6fc5442015-08-24 20:30:31 +05308126 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_MP4ADTS;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05308127 else if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_LATM)
8128 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_MP4LATM;
Ashish Jainf1eaa582016-05-23 20:54:24 +05308129
8130 if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) ==
8131 AUDIO_FORMAT_PCM) {
8132
8133 /*Based on platform support, configure appropriate alsa format for corresponding
8134 *hal input format.
8135 */
8136 out->compr_config.codec->format = hal_format_to_alsa(
8137 config->offload_info.format);
8138
Ashish Jain83a6cc22016-06-28 14:34:17 +05308139 out->hal_op_format = alsa_format_to_hal(
Ashish Jainf1eaa582016-05-23 20:54:24 +05308140 out->compr_config.codec->format);
Ashish Jain83a6cc22016-06-28 14:34:17 +05308141 out->hal_ip_format = out->format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05308142
Dhananjay Kumarac341582017-02-23 23:42:25 +05308143 /*for direct non-compress playback populate bit_width based on selected alsa format as
Ashish Jainf1eaa582016-05-23 20:54:24 +05308144 *hal input format and alsa format might differ based on platform support.
8145 */
8146 out->bit_width = audio_bytes_per_sample(
Ashish Jain83a6cc22016-06-28 14:34:17 +05308147 out->hal_op_format) << 3;
Ashish Jainf1eaa582016-05-23 20:54:24 +05308148
8149 out->compr_config.fragments = DIRECT_PCM_NUM_FRAGMENTS;
8150
Deeraj Soman93155a62019-09-30 19:00:37 +05308151 if (property_get_bool("vendor.audio.offload.buffer.duration.enabled", false)) {
8152 if ((config->offload_info.duration_us >= MIN_OFFLOAD_BUFFER_DURATION_MS * 1000) &&
8153 (config->offload_info.duration_us <= MAX_OFFLOAD_BUFFER_DURATION_MS * 1000))
8154 out->info.duration_us = (int64_t)config->offload_info.duration_us;
8155 }
Deeraj Soman65358ab2019-02-07 15:40:49 +05308156
Ashish Jainf1eaa582016-05-23 20:54:24 +05308157 /* Check if alsa session is configured with the same format as HAL input format,
8158 * if not then derive correct fragment size needed to accomodate the
8159 * conversion of HAL input format to alsa format.
8160 */
8161 audio_extn_utils_update_direct_pcm_fragment_size(out);
8162
8163 /*if hal input and output fragment size is different this indicates HAL input format is
8164 *not same as the alsa format
8165 */
Ashish Jain83a6cc22016-06-28 14:34:17 +05308166 if (out->hal_fragment_size != out->compr_config.fragment_size) {
Ashish Jainf1eaa582016-05-23 20:54:24 +05308167 /*Allocate a buffer to convert input data to the alsa configured format.
8168 *size of convert buffer is equal to the size required to hold one fragment size
8169 *worth of pcm data, this is because flinger does not write more than fragment_size
8170 */
Ashish Jain83a6cc22016-06-28 14:34:17 +05308171 out->convert_buffer = calloc(1,out->compr_config.fragment_size);
8172 if (out->convert_buffer == NULL){
Ashish Jainf1eaa582016-05-23 20:54:24 +05308173 ALOGE("Allocation failed for convert buffer for size %d", out->compr_config.fragment_size);
8174 ret = -ENOMEM;
8175 goto error_open;
8176 }
8177 }
8178 } else if (audio_extn_passthru_is_passthrough_stream(out)) {
8179 out->compr_config.fragment_size =
8180 audio_extn_passthru_get_buffer_size(&config->offload_info);
8181 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
8182 } else {
8183 out->compr_config.fragment_size =
8184 platform_get_compress_offload_buffer_size(&config->offload_info);
8185 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
8186 }
Mingming Yin3ee55c62014-08-04 14:23:35 -07008187
Naresh Tanniruee3499a2017-01-05 14:05:35 +05308188 if (out->flags & AUDIO_OUTPUT_FLAG_TIMESTAMP) {
8189 out->compr_config.fragment_size += sizeof(struct snd_codec_metadata);
8190 }
Xiaojun Sang782e5b12020-06-29 21:13:06 +08008191 if (config->offload_info.format == AUDIO_FORMAT_FLAC) {
8192#ifdef AUDIO_GKI_ENABLED
8193 generic_dec =
8194 &(out->compr_config.codec->options.generic.reserved[1]);
8195 ((struct snd_generic_dec_flac *)generic_dec)->sample_size =
8196 AUDIO_OUTPUT_BIT_WIDTH;
8197#else
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +05308198 out->compr_config.codec->options.flac_dec.sample_size = AUDIO_OUTPUT_BIT_WIDTH;
Xiaojun Sang782e5b12020-06-29 21:13:06 +08008199#endif
8200 }
Mingming Yin3ee55c62014-08-04 14:23:35 -07008201
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05308202 if (config->offload_info.format == AUDIO_FORMAT_APTX) {
8203 audio_extn_send_aptx_dec_bt_addr_to_dsp(out);
8204 }
8205
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008206 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
8207 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07008208
Manish Dewangan69426c82017-01-30 17:35:36 +05308209 if ((flags & AUDIO_OUTPUT_FLAG_TIMESTAMP) &&
8210 (flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC)) {
8211 out->render_mode = RENDER_MODE_AUDIO_STC_MASTER;
8212 } else if(flags & AUDIO_OUTPUT_FLAG_TIMESTAMP) {
8213 out->render_mode = RENDER_MODE_AUDIO_MASTER;
8214 } else {
8215 out->render_mode = RENDER_MODE_AUDIO_NO_TIMESTAMP;
8216 }
Alexy Josephaa54c872014-12-03 02:46:47 -08008217
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05308218 memset(&out->channel_map_param, 0,
8219 sizeof(struct audio_out_channel_map_param));
8220
Haynes Mathew George352f27b2013-07-26 00:00:15 -07008221 out->send_new_metadata = 1;
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05308222 out->send_next_track_params = false;
8223 out->is_compr_metadata_avail = false;
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08008224 out->offload_state = OFFLOAD_STATE_IDLE;
8225 out->playback_started = 0;
Zhou Song48453a02018-01-10 17:50:59 +08008226 out->writeAt.tv_sec = 0;
8227 out->writeAt.tv_nsec = 0;
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08008228
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08008229 audio_extn_dts_create_state_notifier_node(out->usecase);
8230
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008231 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
8232 __func__, config->offload_info.version,
8233 config->offload_info.bit_rate);
Ashish Jain5106d362016-05-11 19:23:33 +05308234
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05308235 /* Check if DSD audio format is supported in codec
8236 * and there is no active native DSD use case
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05308237 */
8238
8239 if ((config->format == AUDIO_FORMAT_DSD) &&
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05308240 (!platform_check_codec_dsd_support(adev->platform) ||
8241 audio_is_dsd_native_stream_active(adev))) {
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05308242 ret = -EINVAL;
8243 goto error_open;
8244 }
8245
Ashish Jain5106d362016-05-11 19:23:33 +05308246 /* Disable gapless if any of the following is true
8247 * passthrough playback
8248 * AV playback
Dhananjay Kumarac341582017-02-23 23:42:25 +05308249 * non compressed Direct playback
Ashish Jain5106d362016-05-11 19:23:33 +05308250 */
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05308251 if (audio_extn_passthru_is_passthrough_stream(out) ||
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05308252 (config->format == AUDIO_FORMAT_DSD) ||
Naresh Tanniru928f0862017-04-07 16:44:23 -07008253 (config->format == AUDIO_FORMAT_IEC61937) ||
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05308254 config->offload_info.has_video ||
Dhananjay Kumarac341582017-02-23 23:42:25 +05308255 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Ashish Jain5106d362016-05-11 19:23:33 +05308256 check_and_set_gapless_mode(adev, false);
8257 } else
8258 check_and_set_gapless_mode(adev, true);
Mingming Yin21854652016-04-13 11:54:02 -07008259
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05308260 if (audio_extn_passthru_is_passthrough_stream(out)) {
Mingming Yin21854652016-04-13 11:54:02 -07008261 out->flags |= AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH;
8262 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05308263 if (config->format == AUDIO_FORMAT_DSD) {
8264 out->flags |= AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH;
Meng Wang4c32fb42020-01-16 17:57:11 +08008265#ifdef AUDIO_GKI_ENABLED
8266 /* out->compr_config.codec->reserved[0] is for compr_passthr */
8267 out->compr_config.codec->reserved[0] = PASSTHROUGH_DSD;
8268#else
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05308269 out->compr_config.codec->compr_passthr = PASSTHROUGH_DSD;
Meng Wang4c32fb42020-01-16 17:57:11 +08008270#endif
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05308271 }
Aalique Grahame0359a1f2016-09-08 16:54:22 -07008272
8273 create_offload_callback_thread(out);
8274
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07008275 } else if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
Arun Mirpuri7da752a2018-09-11 18:01:15 -07008276 switch (config->sample_rate) {
8277 case 0:
8278 out->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
8279 break;
8280 case 8000:
8281 case 16000:
8282 case 48000:
8283 out->sample_rate = config->sample_rate;
8284 break;
8285 default:
8286 ALOGE("%s: Unsupported sampling rate %d for Incall Music", __func__,
8287 config->sample_rate);
8288 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
8289 ret = -EINVAL;
8290 goto error_open;
8291 }
8292 //FIXME: add support for MONO stream configuration when audioflinger mixer supports it
8293 switch (config->channel_mask) {
8294 case AUDIO_CHANNEL_NONE:
8295 case AUDIO_CHANNEL_OUT_STEREO:
8296 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
8297 break;
8298 default:
8299 ALOGE("%s: Unsupported channel mask %#x for Incall Music", __func__,
8300 config->channel_mask);
8301 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
8302 ret = -EINVAL;
8303 goto error_open;
8304 }
8305 switch (config->format) {
8306 case AUDIO_FORMAT_DEFAULT:
8307 case AUDIO_FORMAT_PCM_16_BIT:
8308 out->format = AUDIO_FORMAT_PCM_16_BIT;
8309 break;
8310 default:
8311 ALOGE("%s: Unsupported format %#x for Incall Music", __func__,
8312 config->format);
8313 config->format = AUDIO_FORMAT_PCM_16_BIT;
8314 ret = -EINVAL;
8315 goto error_open;
8316 }
8317
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05308318 ret = voice_extn_check_and_set_incall_music_usecase(adev, out);
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07008319 if (ret != 0) {
8320 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
Arun Mirpuri7da752a2018-09-11 18:01:15 -07008321 __func__, ret);
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07008322 goto error_open;
8323 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08008324 } else if (is_single_device_type_equal(&out->device_list,
8325 AUDIO_DEVICE_OUT_TELEPHONY_TX)) {
Aalique Grahame22e49102018-12-18 14:23:57 -08008326 switch (config->sample_rate) {
8327 case 0:
8328 out->sample_rate = AFE_PROXY_SAMPLING_RATE;
8329 break;
8330 case 8000:
8331 case 16000:
8332 case 48000:
8333 out->sample_rate = config->sample_rate;
8334 break;
8335 default:
8336 ALOGE("%s: Unsupported sampling rate %d for Telephony TX", __func__,
8337 config->sample_rate);
8338 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
8339 ret = -EINVAL;
8340 break;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07008341 }
Aalique Grahame22e49102018-12-18 14:23:57 -08008342 //FIXME: add support for MONO stream configuration when audioflinger mixer supports it
8343 switch (config->channel_mask) {
8344 case AUDIO_CHANNEL_NONE:
8345 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
8346 break;
8347 case AUDIO_CHANNEL_OUT_STEREO:
8348 out->channel_mask = config->channel_mask;
8349 break;
8350 default:
8351 ALOGE("%s: Unsupported channel mask %#x for Telephony TX", __func__,
8352 config->channel_mask);
8353 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
8354 ret = -EINVAL;
8355 break;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07008356 }
Aalique Grahame22e49102018-12-18 14:23:57 -08008357 switch (config->format) {
8358 case AUDIO_FORMAT_DEFAULT:
8359 out->format = AUDIO_FORMAT_PCM_16_BIT;
8360 break;
8361 case AUDIO_FORMAT_PCM_16_BIT:
8362 out->format = config->format;
8363 break;
8364 default:
8365 ALOGE("%s: Unsupported format %#x for Telephony TX", __func__,
8366 config->format);
8367 config->format = AUDIO_FORMAT_PCM_16_BIT;
8368 ret = -EINVAL;
8369 break;
8370 }
8371 if (ret != 0)
8372 goto error_open;
8373
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07008374 out->usecase = USECASE_AUDIO_PLAYBACK_AFE_PROXY;
8375 out->config = pcm_config_afe_proxy_playback;
Aalique Grahame22e49102018-12-18 14:23:57 -08008376 out->config.rate = out->sample_rate;
8377 out->config.channels =
8378 audio_channel_count_from_out_mask(out->channel_mask);
8379 out->config.format = pcm_format_from_audio_format(out->format);
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07008380 adev->voice_tx_output = out;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07008381 } else {
Ashish Jain058165c2016-09-28 23:18:48 +05308382 unsigned int channels = 0;
8383 /*Update config params to default if not set by the caller*/
8384 if (config->sample_rate == 0)
8385 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
8386 if (config->channel_mask == AUDIO_CHANNEL_NONE)
8387 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
8388 if (config->format == AUDIO_FORMAT_DEFAULT)
8389 config->format = AUDIO_FORMAT_PCM_16_BIT;
8390
8391 channels = audio_channel_count_from_out_mask(out->channel_mask);
8392
Varun Balaraje49253e2017-07-06 19:48:56 +05308393 if (out->flags & AUDIO_OUTPUT_FLAG_INTERACTIVE) {
8394 out->usecase = get_interactive_usecase(adev);
8395 out->config = pcm_config_low_latency;
8396 } else if (out->flags & AUDIO_OUTPUT_FLAG_RAW) {
Ashish Jain83a6cc22016-06-28 14:34:17 +05308397 out->usecase = USECASE_AUDIO_PLAYBACK_ULL;
Haynes Mathew George5beddd42016-06-27 18:33:40 -07008398 out->realtime = may_use_noirq_mode(adev, USECASE_AUDIO_PLAYBACK_ULL,
8399 out->flags);
8400 out->config = out->realtime ? pcm_config_rt : pcm_config_low_latency;
Haynes Mathew George16081042017-05-31 17:16:49 -07008401 } else if (out->flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) {
8402 out->usecase = USECASE_AUDIO_PLAYBACK_MMAP;
8403 out->config = pcm_config_mmap_playback;
8404 out->stream.start = out_start;
8405 out->stream.stop = out_stop;
8406 out->stream.create_mmap_buffer = out_create_mmap_buffer;
8407 out->stream.get_mmap_position = out_get_mmap_position;
Ashish Jain83a6cc22016-06-28 14:34:17 +05308408 } else if (out->flags & AUDIO_OUTPUT_FLAG_FAST) {
8409 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
Aniket Kumar Lata932f4872017-11-06 18:29:44 -08008410 out->hal_output_suspend_supported =
8411 property_get_bool("vendor.audio.hal.output.suspend.supported", false);
8412 out->dynamic_pm_qos_config_supported =
8413 property_get_bool("vendor.audio.hal.dynamic.qos.config.supported", false);
8414 if (!out->dynamic_pm_qos_config_supported) {
Alexy Joseph98988832017-01-13 14:56:59 -08008415 ALOGI("%s: dynamic qos voting not enabled for platform", __func__);
8416 } else {
8417 ALOGI("%s: dynamic qos voting enabled for platform", __func__);
8418 //the mixer path will be a string similar to "low-latency-playback resume"
8419 strlcpy(out->pm_qos_mixer_path, use_case_table[out->usecase], MAX_MIXER_PATH_LEN);
8420 strlcat(out->pm_qos_mixer_path,
8421 " resume", MAX_MIXER_PATH_LEN);
8422 ALOGI("%s: created %s pm_qos_mixer_path" , __func__,
8423 out->pm_qos_mixer_path);
8424 }
Ashish Jain83a6cc22016-06-28 14:34:17 +05308425 out->config = pcm_config_low_latency;
8426 } else if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
8427 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
8428 out->config = pcm_config_deep_buffer;
Ashish Jain058165c2016-09-28 23:18:48 +05308429 out->config.period_size = get_output_period_size(config->sample_rate, out->format,
8430 channels, DEEP_BUFFER_OUTPUT_PERIOD_DURATION);
8431 if (out->config.period_size <= 0) {
8432 ALOGE("Invalid configuration period size is not valid");
8433 ret = -EINVAL;
8434 goto error_open;
8435 }
Aalique Grahame22e49102018-12-18 14:23:57 -08008436 } else if (flags & AUDIO_OUTPUT_FLAG_TTS) {
8437 out->usecase = USECASE_AUDIO_PLAYBACK_TTS;
8438 out->config = pcm_config_deep_buffer;
Vignesh Kulothungana6927272019-02-20 15:17:07 -08008439 } else if (config->channel_mask & AUDIO_CHANNEL_HAPTIC_ALL) {
8440 out->usecase = USECASE_AUDIO_PLAYBACK_WITH_HAPTICS;
8441 out->config = pcm_config_haptics_audio;
8442 if (force_haptic_path)
8443 adev->haptics_config = pcm_config_haptics_audio;
8444 else
8445 adev->haptics_config = pcm_config_haptics;
8446
Meng Wangd08ce322020-04-02 08:59:20 +08008447 channels =
Vignesh Kulothungana6927272019-02-20 15:17:07 -08008448 audio_channel_count_from_out_mask(out->channel_mask & ~AUDIO_CHANNEL_HAPTIC_ALL);
8449
8450 if (force_haptic_path) {
8451 out->config.channels = 1;
8452 adev->haptics_config.channels = 1;
8453 } else
8454 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 -08008455 } else if (compare_device_type(&out->device_list, AUDIO_DEVICE_OUT_BUS)) {
Derek Chenf6318be2017-06-12 17:16:24 -04008456 ret = audio_extn_auto_hal_open_output_stream(out);
8457 if (ret) {
8458 ALOGE("%s: Failed to open output stream for bus device", __func__);
8459 ret = -EINVAL;
8460 goto error_open;
8461 }
Ashish Jain83a6cc22016-06-28 14:34:17 +05308462 } else {
8463 /* primary path is the default path selected if no other outputs are available/suitable */
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008464 out->usecase = GET_USECASE_AUDIO_PLAYBACK_PRIMARY(use_db_as_primary);
8465 out->config = GET_PCM_CONFIG_AUDIO_PLAYBACK_PRIMARY(use_db_as_primary);
Ashish Jain83a6cc22016-06-28 14:34:17 +05308466 }
8467 out->hal_ip_format = format = out->format;
8468 out->config.format = hal_format_to_pcm(out->hal_ip_format);
8469 out->hal_op_format = pcm_format_to_hal(out->config.format);
8470 out->bit_width = format_to_bitwidth_table[out->hal_op_format] << 3;
8471 out->config.rate = config->sample_rate;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07008472 out->sample_rate = out->config.rate;
Ashish Jain058165c2016-09-28 23:18:48 +05308473 out->config.channels = channels;
Ashish Jain83a6cc22016-06-28 14:34:17 +05308474 if (out->hal_ip_format != out->hal_op_format) {
8475 uint32_t buffer_size = out->config.period_size *
8476 format_to_bitwidth_table[out->hal_op_format] *
8477 out->config.channels;
8478 out->convert_buffer = calloc(1, buffer_size);
8479 if (out->convert_buffer == NULL){
8480 ALOGE("Allocation failed for convert buffer for size %d",
8481 out->compr_config.fragment_size);
8482 ret = -ENOMEM;
8483 goto error_open;
8484 }
8485 ALOGD("Convert buffer allocated of size %d", buffer_size);
8486 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008487 }
8488
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08008489 ALOGV("%s devices:%d, format:%x, out->sample_rate:%d,out->bit_width:%d out->format:%d out->flags:%x, flags: %x usecase %d",
8490 __func__, devices, format, out->sample_rate, out->bit_width, out->format, out->flags, flags, out->usecase);
Ashish Jain83a6cc22016-06-28 14:34:17 +05308491
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07008492 /* TODO remove this hardcoding and check why width is zero*/
8493 if (out->bit_width == 0)
8494 out->bit_width = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05308495 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07008496 &adev->streams_output_cfg_list,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08008497 &out->device_list, out->flags,
8498 out->hal_op_format, out->sample_rate,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +05308499 out->bit_width, out->channel_mask, out->profile,
Manish Dewangan837dc462015-05-27 10:17:41 +05308500 &out->app_type_cfg);
Aalique Grahame6e763712019-01-31 16:18:17 -08008501 if ((out->usecase == (audio_usecase_t)(GET_USECASE_AUDIO_PLAYBACK_PRIMARY(use_db_as_primary))) ||
Haynes Mathew Georgebf143712013-12-03 13:02:53 -08008502 (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
8503 /* Ensure the default output is not selected twice */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08008504 if(adev->primary_output == NULL)
8505 adev->primary_output = out;
8506 else {
8507 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07008508 ret = -EEXIST;
8509 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08008510 }
8511 }
8512
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008513 /* Check if this usecase is already existing */
8514 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella7ce05352014-04-17 20:00:41 -07008515 if ((get_usecase_from_list(adev, out->usecase) != NULL) &&
8516 (out->usecase != USECASE_COMPRESS_VOIP_CALL)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008517 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008518 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07008519 ret = -EEXIST;
8520 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008521 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08008522
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008523 pthread_mutex_unlock(&adev->lock);
8524
8525 out->stream.common.get_sample_rate = out_get_sample_rate;
8526 out->stream.common.set_sample_rate = out_set_sample_rate;
8527 out->stream.common.get_buffer_size = out_get_buffer_size;
8528 out->stream.common.get_channels = out_get_channels;
8529 out->stream.common.get_format = out_get_format;
8530 out->stream.common.set_format = out_set_format;
8531 out->stream.common.standby = out_standby;
8532 out->stream.common.dump = out_dump;
8533 out->stream.common.set_parameters = out_set_parameters;
8534 out->stream.common.get_parameters = out_get_parameters;
8535 out->stream.common.add_audio_effect = out_add_audio_effect;
8536 out->stream.common.remove_audio_effect = out_remove_audio_effect;
8537 out->stream.get_latency = out_get_latency;
8538 out->stream.set_volume = out_set_volume;
Aalique Grahame22e49102018-12-18 14:23:57 -08008539#ifdef NO_AUDIO_OUT
8540 out->stream.write = out_write_for_no_output;
8541#else
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008542 out->stream.write = out_write;
Aalique Grahame22e49102018-12-18 14:23:57 -08008543#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008544 out->stream.get_render_position = out_get_render_position;
8545 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07008546 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008547
Haynes Mathew George16081042017-05-31 17:16:49 -07008548 if (out->realtime)
8549 out->af_period_multiplier = af_period_multiplier;
8550 else
8551 out->af_period_multiplier = 1;
8552
Andy Hunga1f48fa2019-07-01 18:14:53 -07008553 out->kernel_buffer_size = out->config.period_size * out->config.period_count;
8554
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008555 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07008556 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07008557 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008558
8559 config->format = out->stream.common.get_format(&out->stream.common);
8560 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
8561 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
Naresh Tanniru04f71882018-06-26 17:46:22 +05308562 register_format(out->format, out->supported_formats);
8563 register_channel_mask(out->channel_mask, out->supported_channel_masks);
8564 register_sample_rate(out->sample_rate, out->supported_sample_rates);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008565
Aalique Grahame22e49102018-12-18 14:23:57 -08008566 out->error_log = error_log_create(
8567 ERROR_LOG_ENTRIES,
8568 1000000000 /* aggregate consecutive identical errors within one second in ns */);
8569
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05308570 /*
8571 By locking output stream before registering, we allow the callback
8572 to update stream's state only after stream's initial state is set to
8573 adev state.
8574 */
8575 lock_output_stream(out);
8576 audio_extn_snd_mon_register_listener(out, out_snd_mon_cb);
8577 pthread_mutex_lock(&adev->lock);
8578 out->card_status = adev->card_status;
8579 pthread_mutex_unlock(&adev->lock);
8580 pthread_mutex_unlock(&out->lock);
8581
Aalique Grahame22e49102018-12-18 14:23:57 -08008582 stream_app_type_cfg_init(&out->app_type_cfg);
8583
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008584 *stream_out = &out->stream;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05308585 ALOGD("%s: Stream (%p) picks up usecase (%s)", __func__, &out->stream,
vivek mehta0ea887a2015-08-26 14:01:20 -07008586 use_case_table[out->usecase]);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08008587
8588 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
8589 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
8590 popcount(out->channel_mask), out->playback_started);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08008591 /* setup a channel for client <--> adsp communication for stream events */
Manish Dewangan21a850a2017-08-14 12:03:55 +05308592 is_direct_passthough = audio_extn_passthru_is_direct_passthrough(out);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08008593 if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
Naresh Tanniru85819452017-05-04 18:55:45 -07008594 (out->flags & AUDIO_OUTPUT_FLAG_DIRECT_PCM) ||
Gangadhar Sb0210342019-02-22 17:39:41 +05308595 audio_extn_ip_hdlr_intf_supported_for_copp(adev->platform) ||
8596 (audio_extn_ip_hdlr_intf_supported(config->format, is_direct_passthough, false))) {
Ben Rombergerd771a7c2017-02-22 18:05:17 -08008597 hdlr_stream_cfg.pcm_device_id = platform_get_pcm_device_id(
8598 out->usecase, PCM_PLAYBACK);
8599 hdlr_stream_cfg.flags = out->flags;
8600 hdlr_stream_cfg.type = PCM_PLAYBACK;
8601 ret = audio_extn_adsp_hdlr_stream_open(&out->adsp_hdlr_stream_handle,
8602 &hdlr_stream_cfg);
8603 if (ret) {
8604 ALOGE("%s: adsp_hdlr_stream_open failed %d",__func__, ret);
8605 out->adsp_hdlr_stream_handle = NULL;
8606 }
8607 }
Gangadhar Sb0210342019-02-22 17:39:41 +05308608 ip_hdlr_stream = audio_extn_ip_hdlr_intf_supported(config->format,
8609 is_direct_passthough, false);
8610 ip_hdlr_dev = audio_extn_ip_hdlr_intf_supported_for_copp(adev->platform);
8611 if (ip_hdlr_stream || ip_hdlr_dev ) {
Vidyakumar Athota2062f912017-06-27 14:46:15 -07008612 ret = audio_extn_ip_hdlr_intf_init(&out->ip_hdlr_handle, NULL, NULL, adev, out->usecase);
Naresh Tanniru85819452017-05-04 18:55:45 -07008613 if (ret < 0) {
8614 ALOGE("%s: audio_extn_ip_hdlr_intf_init failed %d",__func__, ret);
8615 out->ip_hdlr_handle = NULL;
8616 }
8617 }
Derek Chenf939fb72018-11-13 13:34:41 -08008618
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07008619 ret = io_streams_map_insert(adev, &out->stream.common,
8620 out->handle, AUDIO_PATCH_HANDLE_NONE);
8621 if (ret != 0)
8622 goto error_open;
8623
Derek Chenf939fb72018-11-13 13:34:41 -08008624 streams_output_ctxt_t *out_ctxt = (streams_output_ctxt_t *)
8625 calloc(1, sizeof(streams_output_ctxt_t));
8626 if (out_ctxt == NULL) {
8627 ALOGE("%s fail to allocate output ctxt", __func__);
8628 ret = -ENOMEM;
8629 goto error_open;
8630 }
8631 out_ctxt->output = out;
8632
8633 pthread_mutex_lock(&adev->lock);
8634 list_add_tail(&adev->active_outputs_list, &out_ctxt->list);
8635 pthread_mutex_unlock(&adev->lock);
8636
Eric Laurent994a6932013-07-17 11:51:42 -07008637 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008638 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07008639
8640error_open:
Ashish Jain83a6cc22016-06-28 14:34:17 +05308641 if (out->convert_buffer)
8642 free(out->convert_buffer);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07008643 free(out);
8644 *stream_out = NULL;
8645 ALOGD("%s: exit: ret %d", __func__, ret);
8646 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008647}
8648
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05308649void adev_close_output_stream(struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008650 struct audio_stream_out *stream)
8651{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008652 struct stream_out *out = (struct stream_out *)stream;
8653 struct audio_device *adev = out->dev;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08008654 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008655
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008656 ALOGD("%s: enter:stream_handle(%s)",__func__, use_case_table[out->usecase]);
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05308657
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -07008658 io_streams_map_remove(adev, out->handle);
8659
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05308660 // must deregister from sndmonitor first to prevent races
8661 // between the callback and close_stream
8662 audio_extn_snd_mon_unregister_listener(out);
8663
Ben Rombergerd771a7c2017-02-22 18:05:17 -08008664 /* close adsp hdrl session before standby */
8665 if (out->adsp_hdlr_stream_handle) {
8666 ret = audio_extn_adsp_hdlr_stream_close(out->adsp_hdlr_stream_handle);
8667 if (ret)
8668 ALOGE("%s: adsp_hdlr_stream_close failed %d",__func__, ret);
8669 out->adsp_hdlr_stream_handle = NULL;
8670 }
8671
Manish Dewangan21a850a2017-08-14 12:03:55 +05308672 if (out->ip_hdlr_handle) {
Naresh Tanniru85819452017-05-04 18:55:45 -07008673 audio_extn_ip_hdlr_intf_deinit(out->ip_hdlr_handle);
8674 out->ip_hdlr_handle = NULL;
8675 }
8676
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08008677 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05308678 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08008679 ret = voice_extn_compress_voip_close_output_stream(&stream->common);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05308680 out->started = 0;
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05308681 pthread_mutex_unlock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08008682 if(ret != 0)
8683 ALOGE("%s: Compress voip output cannot be closed, error:%d",
8684 __func__, ret);
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07008685 } else
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08008686 out_standby(&stream->common);
8687
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07008688 if (is_offload_usecase(out->usecase)) {
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08008689 audio_extn_dts_remove_state_notifier_node(out->usecase);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008690 destroy_offload_callback_thread(out);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07008691 free_offload_usecase(adev, out->usecase);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008692 if (out->compr_config.codec != NULL)
8693 free(out->compr_config.codec);
8694 }
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07008695
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05308696 out->a2dp_compress_mute = false;
8697
Varun Balaraje49253e2017-07-06 19:48:56 +05308698 if (is_interactive_usecase(out->usecase))
8699 free_interactive_usecase(adev, out->usecase);
8700
Ashish Jain83a6cc22016-06-28 14:34:17 +05308701 if (out->convert_buffer != NULL) {
8702 free(out->convert_buffer);
8703 out->convert_buffer = NULL;
8704 }
8705
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07008706 if (adev->voice_tx_output == out)
8707 adev->voice_tx_output = NULL;
8708
Aalique Grahame22e49102018-12-18 14:23:57 -08008709 error_log_destroy(out->error_log);
8710 out->error_log = NULL;
8711
Dhanalakshmi Siddani6c3d0992017-01-16 16:52:33 +05308712 if (adev->primary_output == out)
8713 adev->primary_output = NULL;
8714
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008715 pthread_cond_destroy(&out->cond);
8716 pthread_mutex_destroy(&out->lock);
Weiyin Jiangd5974e62020-09-08 20:28:22 +08008717 pthread_mutex_destroy(&out->pre_lock);
8718 pthread_mutex_destroy(&out->latch_lock);
8719 pthread_mutex_destroy(&out->position_query_lock);
Derek Chenf939fb72018-11-13 13:34:41 -08008720
8721 pthread_mutex_lock(&adev->lock);
8722 streams_output_ctxt_t *out_ctxt = out_get_stream(adev, out->handle);
8723 if (out_ctxt != NULL) {
8724 list_remove(&out_ctxt->list);
8725 free(out_ctxt);
8726 } else {
8727 ALOGW("%s, output stream already closed", __func__);
8728 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008729 free(stream);
Derek Chenf939fb72018-11-13 13:34:41 -08008730 pthread_mutex_unlock(&adev->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07008731 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008732}
8733
8734static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
8735{
8736 struct audio_device *adev = (struct audio_device *)dev;
8737 struct str_parms *parms;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008738 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07008739 int val;
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07008740 int ret;
8741 int status = 0;
Aalique Grahame22e49102018-12-18 14:23:57 -08008742 bool a2dp_reconfig = false;
Zhou Songd6d71752019-05-21 18:08:51 +08008743 struct listnode *node;
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07008744 int controller = -1, stream = -1;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008745
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08008746 ALOGD("%s: enter: %s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008747 parms = str_parms_create_str(kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008748
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05308749 if (!parms)
8750 goto error;
Naresh Tanniru4c630392014-05-12 01:05:52 +05308751
Derek Chen6f293672019-04-01 01:40:24 -07008752 /* notify adev and input/output streams on the snd card status */
8753 adev_snd_mon_cb((void *)adev, parms);
8754
8755 list_for_each(node, &adev->active_outputs_list) {
8756 streams_output_ctxt_t *out_ctxt = node_to_item(node,
8757 streams_output_ctxt_t,
8758 list);
8759 out_snd_mon_cb((void *)out_ctxt->output, parms);
8760 }
8761
8762 list_for_each(node, &adev->active_inputs_list) {
8763 streams_input_ctxt_t *in_ctxt = node_to_item(node,
8764 streams_input_ctxt_t,
8765 list);
8766 in_snd_mon_cb((void *)in_ctxt->input, parms);
8767 }
8768
Zhou Songd6d71752019-05-21 18:08:51 +08008769 pthread_mutex_lock(&adev->lock);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05308770 ret = str_parms_get_str(parms, "BT_SCO", value, sizeof(value));
8771 if (ret >= 0) {
8772 /* When set to false, HAL should disable EC and NS */
Zhou Songd6d71752019-05-21 18:08:51 +08008773 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0){
Ashish Jain1b9b30c2017-05-18 20:57:40 +05308774 adev->bt_sco_on = true;
Zhou Songd6d71752019-05-21 18:08:51 +08008775 } else {
Ashish Jain1b9b30c2017-05-18 20:57:40 +05308776 adev->bt_sco_on = false;
Zhou Songd6d71752019-05-21 18:08:51 +08008777 audio_extn_sco_reset_configuration();
Kunlei Zhangd96af8f2019-08-27 16:33:29 +08008778 }
8779 }
8780
8781 ret = str_parms_get_str(parms, "A2dpSuspended", value, sizeof(value));
Weiyin Jiangd5974e62020-09-08 20:28:22 +08008782 if (ret >= 0) {
Kunlei Zhangd96af8f2019-08-27 16:33:29 +08008783 if (!strncmp(value, "false", 5) &&
8784 audio_extn_a2dp_source_is_suspended()) {
8785 struct audio_usecase *usecase;
8786 struct listnode *node;
Zhou Songd6d71752019-05-21 18:08:51 +08008787 list_for_each(node, &adev->usecase_list) {
8788 usecase = node_to_item(node, struct audio_usecase, list);
Kunlei Zhangd96af8f2019-08-27 16:33:29 +08008789 if (usecase->stream.in && (usecase->type == PCM_CAPTURE) &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08008790 is_sco_in_device_type(&usecase->stream.in->device_list)) {
Kunlei Zhangd96af8f2019-08-27 16:33:29 +08008791 ALOGD("a2dp resumed, switch bt sco mic to handset mic");
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08008792 reassign_device_list(&usecase->stream.in->device_list,
8793 AUDIO_DEVICE_IN_BUILTIN_MIC, "");
Kunlei Zhangd96af8f2019-08-27 16:33:29 +08008794 select_devices(adev, usecase->id);
8795 }
Zhou Songd6d71752019-05-21 18:08:51 +08008796 }
8797 }
Ashish Jain1b9b30c2017-05-18 20:57:40 +05308798 }
8799
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07008800 status = voice_set_parameters(adev, parms);
8801 if (status != 0)
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08008802 goto done;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008803
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07008804 status = platform_set_parameters(adev->platform, parms);
8805 if (status != 0)
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08008806 goto done;
8807
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07008808 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
8809 if (ret >= 0) {
Vicky Sehrawate240e5d2014-08-12 17:17:04 -07008810 /* When set to false, HAL should disable EC and NS */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008811 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
8812 adev->bluetooth_nrec = true;
8813 else
8814 adev->bluetooth_nrec = false;
8815 }
8816
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07008817 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
8818 if (ret >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008819 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
8820 adev->screen_off = false;
8821 else
8822 adev->screen_off = true;
Quinn Male70f20f32019-06-26 16:50:26 -07008823 audio_extn_sound_trigger_update_screen_status(adev->screen_off);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008824 }
8825
Eric Laurent4b084132018-10-19 17:33:43 -07008826 ret = str_parms_get_int(parms, "rotation", &val);
8827 if (ret >= 0) {
8828 bool reverse_speakers = false;
8829 int camera_rotation = CAMERA_ROTATION_LANDSCAPE;
8830 switch (val) {
8831 // FIXME: note that the code below assumes that the speakers are in the correct placement
8832 // relative to the user when the device is rotated 90deg from its default rotation. This
8833 // assumption is device-specific, not platform-specific like this code.
8834 case 270:
8835 reverse_speakers = true;
8836 camera_rotation = CAMERA_ROTATION_INVERT_LANDSCAPE;
8837 break;
8838 case 0:
8839 case 180:
8840 camera_rotation = CAMERA_ROTATION_PORTRAIT;
8841 break;
8842 case 90:
8843 camera_rotation = CAMERA_ROTATION_LANDSCAPE;
8844 break;
8845 default:
8846 ALOGE("%s: unexpected rotation of %d", __func__, val);
8847 status = -EINVAL;
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07008848 }
Eric Laurent4b084132018-10-19 17:33:43 -07008849 if (status == 0) {
8850 // check and set swap
8851 // - check if orientation changed and speaker active
8852 // - set rotation and cache the rotation value
8853 adev->camera_orientation =
8854 (adev->camera_orientation & ~CAMERA_ROTATION_MASK) | camera_rotation;
8855 if (!audio_extn_is_maxx_audio_enabled())
8856 platform_check_and_set_swap_lr_channels(adev, reverse_speakers);
8857 }
8858 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07008859
Mingming Yin514a8bc2014-07-29 15:22:21 -07008860 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_SCO_WB, value, sizeof(value));
8861 if (ret >= 0) {
8862 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
8863 adev->bt_wb_speech_enabled = true;
8864 else
8865 adev->bt_wb_speech_enabled = false;
8866 }
8867
Zhou Song12c29502019-03-16 10:37:18 +08008868 ret = str_parms_get_str(parms, "bt_swb", value, sizeof(value));
8869 if (ret >= 0) {
8870 val = atoi(value);
8871 adev->swb_speech_mode = val;
8872 }
8873
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -07008874 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value, sizeof(value));
8875 if (ret >= 0) {
8876 val = atoi(value);
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05308877 audio_devices_t device = (audio_devices_t) val;
Zhou Song681350a2017-10-19 16:28:42 +08008878 if (audio_is_output_device(val) &&
8879 (val & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07008880 ALOGV("cache new ext disp type and edid");
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07008881 platform_get_controller_stream_from_params(parms, &controller, &stream);
8882 platform_set_ext_display_device_v2(adev->platform, controller, stream);
8883 ret = platform_get_ext_disp_type_v2(adev->platform, controller, stream);
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07008884 if (ret < 0) {
8885 ALOGE("%s: Failed to query disp type, ret:%d", __func__, ret);
Manisha Agarwal2f5ff882018-08-08 17:09:29 +05308886 } else {
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07008887 platform_cache_edid_v2(adev->platform, controller, stream);
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07008888 }
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05308889 } else if (audio_is_usb_out_device(device) || audio_is_usb_in_device(device)) {
vivek mehta344576a2016-04-12 18:56:03 -07008890 /*
8891 * Do not allow AFE proxy port usage by WFD source when USB headset is connected.
8892 * Per AudioPolicyManager, USB device is higher priority than WFD.
8893 * For Voice call over USB headset, voice call audio is routed to AFE proxy ports.
8894 * If WFD use case occupies AFE proxy, it may result unintended behavior while
8895 * starting voice call on USB
8896 */
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08008897 ret = str_parms_get_str(parms, "card", value, sizeof(value));
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05308898 if (ret >= 0)
8899 audio_extn_usb_add_device(device, atoi(value));
8900
Zhou Song6f862822017-11-06 17:27:57 +08008901 if (!audio_extn_usb_is_tunnel_supported()) {
8902 ALOGV("detected USB connect .. disable proxy");
8903 adev->allow_afe_proxy_usage = false;
8904 }
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -07008905 }
8906 }
8907
8908 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value, sizeof(value));
8909 if (ret >= 0) {
8910 val = atoi(value);
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05308911 audio_devices_t device = (audio_devices_t) val;
Garmond Leunge3b6d482016-10-25 16:48:01 -07008912 /*
8913 * The HDMI / Displayport disconnect handling has been moved to
8914 * audio extension to ensure that its parameters are not
8915 * invalidated prior to updating sysfs of the disconnect event
8916 * Invalidate will be handled by audio_extn_ext_disp_set_parameters()
8917 */
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05308918 if (audio_is_usb_out_device(device) || audio_is_usb_in_device(device)) {
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08008919 ret = str_parms_get_str(parms, "card", value, sizeof(value));
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05308920 if (ret >= 0)
8921 audio_extn_usb_remove_device(device, atoi(value));
8922
Zhou Song6f862822017-11-06 17:27:57 +08008923 if (!audio_extn_usb_is_tunnel_supported()) {
8924 ALOGV("detected USB disconnect .. enable proxy");
8925 adev->allow_afe_proxy_usage = true;
8926 }
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -07008927 }
8928 }
8929
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008930 audio_extn_qdsp_set_parameters(adev, parms);
Aalique Grahame22e49102018-12-18 14:23:57 -08008931
8932 status = audio_extn_a2dp_set_parameters(parms, &a2dp_reconfig);
Aniket Kumar Lata23300322019-02-20 22:25:30 -08008933 if (status >= 0 && a2dp_reconfig) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05308934 struct audio_usecase *usecase;
8935 struct listnode *node;
8936 list_for_each(node, &adev->usecase_list) {
8937 usecase = node_to_item(node, struct audio_usecase, list);
Weiyin Jiangff56ff32020-05-08 14:32:21 +08008938 if ((usecase->stream.out == NULL) || (usecase->type != PCM_PLAYBACK))
8939 continue;
8940
8941 if (is_a2dp_out_device_type(&usecase->device_list)) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05308942 ALOGD("reconfigure a2dp... forcing device switch");
Naresh Tannirucd2353e2016-08-19 00:37:25 +05308943 audio_extn_a2dp_set_handoff_mode(true);
Manisha Agarwalf1c3b6b2019-06-25 13:00:33 +05308944 ALOGD("Switching to speaker and muting the stream before select_devices");
8945 check_a2dp_restore_l(adev, usecase->stream.out, false);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05308946 //force device switch to re configure encoder
8947 select_devices(adev, usecase->id);
Manisha Agarwalf1c3b6b2019-06-25 13:00:33 +05308948 ALOGD("Unmuting the stream after select_devices");
Weiyin Jiangd5974e62020-09-08 20:28:22 +08008949 pthread_mutex_lock(&usecase->stream.out->latch_lock);
Manisha Agarwalf1c3b6b2019-06-25 13:00:33 +05308950 usecase->stream.out->a2dp_compress_mute = false;
Weiyin Jiangd5974e62020-09-08 20:28:22 +08008951 out_set_compr_volume(&usecase->stream.out->stream,
8952 usecase->stream.out->volume_l,
8953 usecase->stream.out->volume_r);
8954 pthread_mutex_unlock(&usecase->stream.out->latch_lock);
Naresh Tannirucd2353e2016-08-19 00:37:25 +05308955 audio_extn_a2dp_set_handoff_mode(false);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05308956 break;
Weiyin Jiangd5974e62020-09-08 20:28:22 +08008957 } else if (usecase->stream.out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
8958 pthread_mutex_lock(&usecase->stream.out->latch_lock);
8959 if (usecase->stream.out->a2dp_compress_mute) {
8960 pthread_mutex_unlock(&usecase->stream.out->latch_lock);
8961 reassign_device_list(&usecase->stream.out->device_list,
8962 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP, "");
8963 check_a2dp_restore_l(adev, usecase->stream.out, true);
8964 break;
8965 }
8966 pthread_mutex_unlock(&usecase->stream.out->latch_lock);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05308967 }
8968 }
8969 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08008970
8971 //handle vr audio setparam
8972 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_VR_AUDIO_MODE,
8973 value, sizeof(value));
8974 if (ret >= 0) {
8975 ALOGI("Setting vr mode to be %s", value);
8976 if (!strncmp(value, "true", 4)) {
8977 adev->vr_audio_mode_enabled = true;
8978 ALOGI("Setting vr mode to true");
8979 } else if (!strncmp(value, "false", 5)) {
8980 adev->vr_audio_mode_enabled = false;
8981 ALOGI("Setting vr mode to false");
8982 } else {
8983 ALOGI("wrong vr mode set");
8984 }
8985 }
8986
Eric Laurent4b084132018-10-19 17:33:43 -07008987 //FIXME: to be replaced by proper video capture properties API
8988 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_CAMERA_FACING, value, sizeof(value));
8989 if (ret >= 0) {
8990 int camera_facing = CAMERA_FACING_BACK;
8991 if (strcmp(value, AUDIO_PARAMETER_VALUE_FRONT) == 0)
8992 camera_facing = CAMERA_FACING_FRONT;
8993 else if (strcmp(value, AUDIO_PARAMETER_VALUE_BACK) == 0)
8994 camera_facing = CAMERA_FACING_BACK;
8995 else {
8996 ALOGW("%s: invalid camera facing value: %s", __func__, value);
8997 goto done;
8998 }
8999 adev->camera_orientation =
9000 (adev->camera_orientation & ~CAMERA_FACING_MASK) | camera_facing;
9001 struct audio_usecase *usecase;
9002 struct listnode *node;
9003 list_for_each(node, &adev->usecase_list) {
9004 usecase = node_to_item(node, struct audio_usecase, list);
9005 struct stream_in *in = usecase->stream.in;
9006 if (usecase->type == PCM_CAPTURE && in != NULL &&
9007 in->source == AUDIO_SOURCE_CAMCORDER && !in->standby) {
9008 select_devices(adev, in->usecase);
9009 }
9010 }
9011 }
9012
Daniel Hillenbrand21752f02013-05-23 10:10:00 +05309013 amplifier_set_parameters(parms);
Naresh Tannirucd2353e2016-08-19 00:37:25 +05309014 audio_extn_set_parameters(adev, parms);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08009015done:
9016 str_parms_destroy(parms);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08009017 pthread_mutex_unlock(&adev->lock);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05309018error:
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07009019 ALOGV("%s: exit with code(%d)", __func__, status);
9020 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009021}
9022
9023static char* adev_get_parameters(const struct audio_hw_device *dev,
9024 const char *keys)
9025{
Sidipotu Ashokaa4fa6a2018-12-21 09:19:26 +05309026 ALOGD("%s:%s", __func__, keys);
9027
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07009028 struct audio_device *adev = (struct audio_device *)dev;
9029 struct str_parms *reply = str_parms_create();
9030 struct str_parms *query = str_parms_create_str(keys);
9031 char *str;
Naresh Tannirud7205b62014-06-20 02:54:48 +05309032 char value[256] = {0};
9033 int ret = 0;
9034
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07009035 if (!query || !reply) {
Alexy Josephaee4fdd2016-01-29 13:02:07 -08009036 if (reply) {
9037 str_parms_destroy(reply);
9038 }
9039 if (query) {
9040 str_parms_destroy(query);
9041 }
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07009042 ALOGE("adev_get_parameters: failed to create query or reply");
9043 return NULL;
9044 }
9045
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08009046 //handle vr audio getparam
9047
9048 ret = str_parms_get_str(query,
9049 AUDIO_PARAMETER_KEY_VR_AUDIO_MODE,
9050 value, sizeof(value));
9051
9052 if (ret >= 0) {
9053 bool vr_audio_enabled = false;
9054 pthread_mutex_lock(&adev->lock);
9055 vr_audio_enabled = adev->vr_audio_mode_enabled;
9056 pthread_mutex_unlock(&adev->lock);
9057
9058 ALOGI("getting vr mode to %d", vr_audio_enabled);
9059
9060 if (vr_audio_enabled) {
9061 str_parms_add_str(reply, AUDIO_PARAMETER_KEY_VR_AUDIO_MODE,
9062 "true");
9063 goto exit;
9064 } else {
9065 str_parms_add_str(reply, AUDIO_PARAMETER_KEY_VR_AUDIO_MODE,
9066 "false");
9067 goto exit;
9068 }
9069 }
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07009070
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08009071 pthread_mutex_lock(&adev->lock);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07009072 audio_extn_get_parameters(adev, query, reply);
Shiv Maliyappanahallif9308492013-12-12 12:18:09 -08009073 voice_get_parameters(adev, query, reply);
Aalique Grahame22e49102018-12-18 14:23:57 -08009074 audio_extn_a2dp_get_parameters(query, reply);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07009075 platform_get_parameters(adev->platform, query, reply);
justinwengd5395152019-11-04 12:23:09 +08009076 audio_extn_ma_get_parameters(adev, query, reply);
Naresh Tanniru80659832014-06-04 18:17:56 +05309077 pthread_mutex_unlock(&adev->lock);
9078
Naresh Tannirud7205b62014-06-20 02:54:48 +05309079exit:
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07009080 str = str_parms_to_str(reply);
9081 str_parms_destroy(query);
9082 str_parms_destroy(reply);
9083
Sidipotu Ashokaa4fa6a2018-12-21 09:19:26 +05309084 ALOGD("%s: exit: returns - %s", __func__, str);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07009085 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009086}
9087
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07009088static int adev_init_check(const struct audio_hw_device *dev __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009089{
9090 return 0;
9091}
9092
9093static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
9094{
Haynes Mathew George5191a852013-09-11 14:19:36 -07009095 int ret;
9096 struct audio_device *adev = (struct audio_device *)dev;
Aalique Grahame22e49102018-12-18 14:23:57 -08009097
9098 audio_extn_extspk_set_voice_vol(adev->extspk, volume);
9099
Haynes Mathew George5191a852013-09-11 14:19:36 -07009100 pthread_mutex_lock(&adev->lock);
9101 /* cache volume */
Shruthi Krishnaace10852013-10-25 14:32:12 -07009102 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07009103 pthread_mutex_unlock(&adev->lock);
9104 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009105}
9106
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07009107static int adev_set_master_volume(struct audio_hw_device *dev __unused,
9108 float volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009109{
9110 return -ENOSYS;
9111}
9112
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07009113static int adev_get_master_volume(struct audio_hw_device *dev __unused,
9114 float *volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009115{
9116 return -ENOSYS;
9117}
9118
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07009119static int adev_set_master_mute(struct audio_hw_device *dev __unused,
9120 bool muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009121{
9122 return -ENOSYS;
9123}
9124
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07009125static int adev_get_master_mute(struct audio_hw_device *dev __unused,
9126 bool *muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009127{
9128 return -ENOSYS;
9129}
9130
9131static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
9132{
9133 struct audio_device *adev = (struct audio_device *)dev;
Garmond Leung5fd0b552018-04-17 11:56:12 -07009134 struct listnode *node;
9135 struct audio_usecase *usecase = NULL;
9136 int ret = 0;
kunleizdc4af9d2017-05-04 12:15:35 +08009137
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009138 pthread_mutex_lock(&adev->lock);
9139 if (adev->mode != mode) {
Jaideep Sharma477917f2020-03-13 18:13:33 +05309140 ALOGD("%s: mode %d , prev_mode %d \n", __func__, mode , adev->mode);
9141 adev->prev_mode = adev->mode; /* prev_mode is kept to handle voip concurrency*/
Daniel Hillenbrand21752f02013-05-23 10:10:00 +05309142 if (amplifier_set_mode(mode) != 0)
9143 ALOGE("Failed setting amplifier mode");
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009144 adev->mode = mode;
Weiyin Jiangc3a9c812020-09-22 16:48:19 +08009145 if (mode == AUDIO_MODE_CALL_SCREEN) {
Jaideep Sharma477917f2020-03-13 18:13:33 +05309146 adev->current_call_output = adev->primary_output;
9147 voice_start_call(adev);
9148 } else if (voice_is_in_call_or_call_screen(adev) &&
Kunlei Zhang1d5c7f22019-05-21 14:25:57 +08009149 (mode == AUDIO_MODE_NORMAL ||
9150 (mode == AUDIO_MODE_IN_COMMUNICATION && !voice_is_call_state_active(adev)))) {
Garmond Leung5fd0b552018-04-17 11:56:12 -07009151 list_for_each(node, &adev->usecase_list) {
9152 usecase = node_to_item(node, struct audio_usecase, list);
9153 if (usecase->type == VOICE_CALL)
9154 break;
9155 }
9156 if (usecase &&
9157 audio_is_usb_out_device(usecase->out_snd_device & AUDIO_DEVICE_OUT_ALL_USB)) {
9158 ret = audio_extn_usb_check_and_set_svc_int(usecase,
9159 true);
9160 if (ret != 0) {
9161 /* default service interval was successfully updated,
9162 reopen USB backend with new service interval */
9163 check_usecases_codec_backend(adev,
9164 usecase,
9165 usecase->out_snd_device);
9166 }
9167 }
9168
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07009169 voice_stop_call(adev);
Banajit Goswami20cdd212015-09-11 01:11:30 -07009170 platform_set_gsm_mode(adev->platform, false);
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07009171 adev->current_call_output = NULL;
kunleizdc4af9d2017-05-04 12:15:35 +08009172 // restore device for other active usecases after stop call
9173 list_for_each(node, &adev->usecase_list) {
9174 usecase = node_to_item(node, struct audio_usecase, list);
9175 select_devices(adev, usecase->id);
9176 }
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07009177 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009178 }
9179 pthread_mutex_unlock(&adev->lock);
9180 return 0;
9181}
9182
9183static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
9184{
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08009185 int ret;
Aalique Grahame22e49102018-12-18 14:23:57 -08009186 struct audio_device *adev = (struct audio_device *)dev;
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08009187
9188 pthread_mutex_lock(&adev->lock);
Vidyakumar Athota2850d532013-11-19 16:02:12 -08009189 ALOGD("%s state %d\n", __func__, state);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08009190 ret = voice_set_mic_mute((struct audio_device *)dev, state);
Aalique Grahame22e49102018-12-18 14:23:57 -08009191
Derek Chend2530072014-11-24 12:39:14 -08009192 if (adev->ext_hw_plugin)
9193 ret = audio_extn_ext_hw_plugin_set_mic_mute(adev->ext_hw_plugin, state);
Aalique Grahame22e49102018-12-18 14:23:57 -08009194
9195 adev->mic_muted = state;
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08009196 pthread_mutex_unlock(&adev->lock);
9197
9198 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009199}
9200
9201static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
9202{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07009203 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009204 return 0;
9205}
9206
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07009207static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009208 const struct audio_config *config)
9209{
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07009210 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009211
Aalique Grahame22e49102018-12-18 14:23:57 -08009212 /* Don't know if USB HIFI in this context so use true to be conservative */
9213 if (check_input_parameters(config->sample_rate, config->format, channel_count,
9214 true /*is_usb_hifi */) != 0)
9215 return 0;
9216
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07009217 return get_input_buffer_size(config->sample_rate, config->format, channel_count,
9218 false /* is_low_latency: since we don't know, be conservative */);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009219}
9220
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009221static bool adev_input_allow_hifi_record(struct audio_device *adev,
9222 audio_devices_t devices,
9223 audio_input_flags_t flags,
9224 audio_source_t source) {
9225 const bool allowed = true;
9226
9227 if (!audio_is_usb_in_device(devices))
9228 return !allowed;
9229
9230 switch (flags) {
9231 case AUDIO_INPUT_FLAG_NONE:
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009232 break;
Haynes Mathew George59862182017-10-24 16:23:57 -07009233 case AUDIO_INPUT_FLAG_FAST: // disallow hifi record for FAST as
9234 // it affects RTD numbers over USB
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009235 default:
9236 return !allowed;
9237 }
9238
9239 switch (source) {
9240 case AUDIO_SOURCE_DEFAULT:
9241 case AUDIO_SOURCE_MIC:
9242 case AUDIO_SOURCE_UNPROCESSED:
9243 break;
9244 default:
9245 return !allowed;
9246 }
9247
9248 switch (adev->mode) {
9249 case 0:
9250 break;
9251 default:
9252 return !allowed;
9253 }
9254
9255 return allowed;
9256}
9257
Haynes Mathew George4ffef292017-11-21 15:08:02 -08009258static int adev_update_voice_comm_input_stream(struct stream_in *in,
9259 struct audio_config *config)
9260{
9261 bool valid_rate = (config->sample_rate == 8000 ||
9262 config->sample_rate == 16000 ||
9263 config->sample_rate == 32000 ||
9264 config->sample_rate == 48000);
9265 bool valid_ch = audio_channel_count_from_in_mask(in->channel_mask) == 1;
9266
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08009267 if(!voice_extn_is_compress_voip_supported()) {
kunleiz28c73e72019-03-27 17:24:04 +08009268 if (valid_rate && valid_ch) {
Haynes Mathew George4ffef292017-11-21 15:08:02 -08009269 in->usecase = USECASE_AUDIO_RECORD_VOIP;
9270 in->config = default_pcm_config_voip_copp;
9271 in->config.period_size = VOIP_IO_BUF_SIZE(in->sample_rate,
9272 DEFAULT_VOIP_BUF_DURATION_MS,
9273 DEFAULT_VOIP_BIT_DEPTH_BYTE)/2;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08009274 } else {
9275 ALOGW("%s No valid input in voip, use defaults"
9276 "sample rate %u, channel mask 0x%X",
9277 __func__, config->sample_rate, in->channel_mask);
9278 }
Haynes Mathew George4ffef292017-11-21 15:08:02 -08009279 in->config.rate = config->sample_rate;
9280 in->sample_rate = config->sample_rate;
9281 } else {
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08009282 //XXX needed for voice_extn_compress_voip_open_input_stream
9283 in->config.rate = config->sample_rate;
9284 if ((in->dev->mode == AUDIO_MODE_IN_COMMUNICATION ||
Shalini Manjunathaa763cc42019-08-23 15:13:46 +05309285 in->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08009286 voice_extn_compress_voip_is_active(in->dev)) &&
9287 (voice_extn_compress_voip_is_format_supported(in->format)) &&
9288 valid_rate && valid_ch) {
9289 voice_extn_compress_voip_open_input_stream(in);
9290 // update rate entries to match config from AF
9291 in->config.rate = config->sample_rate;
9292 in->sample_rate = config->sample_rate;
9293 } else {
9294 ALOGW("%s compress voip not active, use defaults", __func__);
9295 }
Haynes Mathew George4ffef292017-11-21 15:08:02 -08009296 }
Haynes Mathew George4ffef292017-11-21 15:08:02 -08009297 return 0;
9298}
9299
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009300static int adev_open_input_stream(struct audio_hw_device *dev,
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07009301 audio_io_handle_t handle,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009302 audio_devices_t devices,
9303 struct audio_config *config,
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07009304 struct audio_stream_in **stream_in,
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05309305 audio_input_flags_t flags,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08009306 const char *address,
Vidyakumar Athota5c398212015-03-31 21:53:21 -07009307 audio_source_t source)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009308{
9309 struct audio_device *adev = (struct audio_device *)dev;
9310 struct stream_in *in;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08009311 int ret = 0, buffer_size, frame_size;
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07009312 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07009313 bool is_low_latency = false;
Divya Narayanan Poojary45f19192016-09-30 18:52:13 +05309314 bool channel_mask_updated = false;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009315 bool is_usb_dev = audio_is_usb_in_device(devices);
9316 bool may_use_hifi_record = adev_input_allow_hifi_record(adev,
9317 devices,
9318 flags,
9319 source);
Andy Hung94320602018-10-29 18:31:12 -07009320 ALOGV("%s: enter: flags %#x, is_usb_dev %d, may_use_hifi_record %d,"
9321 " sample_rate %u, channel_mask %#x, format %#x",
9322 __func__, flags, is_usb_dev, may_use_hifi_record,
9323 config->sample_rate, config->channel_mask, config->format);
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05309324
kunleizdff872d2018-08-20 14:40:33 +08009325 if (is_usb_dev && (!audio_extn_usb_connected(NULL))) {
kunleizd6a9e0c2018-07-30 15:38:52 +08009326 is_usb_dev = false;
9327 devices = AUDIO_DEVICE_IN_BUILTIN_MIC;
9328 ALOGW("%s: ignore set device to non existing USB card, use input device(%#x)",
9329 __func__, devices);
9330 }
9331
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009332 *stream_in = NULL;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009333
9334 if (!(is_usb_dev && may_use_hifi_record)) {
9335 if (config->sample_rate == 0)
9336 config->sample_rate = 48000;
9337 if (config->channel_mask == AUDIO_CHANNEL_NONE)
9338 config->channel_mask = AUDIO_CHANNEL_IN_MONO;
9339 if (config->format == AUDIO_FORMAT_DEFAULT)
9340 config->format = AUDIO_FORMAT_PCM_16_BIT;
9341
9342 channel_count = audio_channel_count_from_in_mask(config->channel_mask);
9343
Aalique Grahame22e49102018-12-18 14:23:57 -08009344 if (check_input_parameters(config->sample_rate, config->format, channel_count,
9345 false) != 0)
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009346 return -EINVAL;
Chaithanya Krishna Bacharaju9955b162016-05-25 16:25:53 +05309347 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009348
Rahul Sharma99770982019-03-06 17:05:26 +05309349 pthread_mutex_lock(&adev->lock);
9350 if (in_get_stream(adev, handle) != NULL) {
9351 ALOGW("%s, input stream already opened", __func__);
9352 ret = -EEXIST;
9353 }
9354 pthread_mutex_unlock(&adev->lock);
9355 if (ret)
9356 return ret;
9357
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009358 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07009359
9360 if (!in) {
9361 ALOGE("failed to allocate input stream");
9362 return -ENOMEM;
9363 }
9364
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05309365 ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x)\
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05309366 stream_handle(%p) io_handle(%d) source(%d) format %x",__func__, config->sample_rate,
9367 config->channel_mask, devices, &in->stream, handle, source, config->format);
Ravi Kumar Alamanda40703102014-04-24 10:34:41 -07009368 pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07009369 pthread_mutex_init(&in->pre_lock, (const pthread_mutexattr_t *) NULL);
Ravi Kumar Alamanda40703102014-04-24 10:34:41 -07009370
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009371 in->stream.common.get_sample_rate = in_get_sample_rate;
9372 in->stream.common.set_sample_rate = in_set_sample_rate;
9373 in->stream.common.get_buffer_size = in_get_buffer_size;
9374 in->stream.common.get_channels = in_get_channels;
9375 in->stream.common.get_format = in_get_format;
9376 in->stream.common.set_format = in_set_format;
9377 in->stream.common.standby = in_standby;
9378 in->stream.common.dump = in_dump;
9379 in->stream.common.set_parameters = in_set_parameters;
9380 in->stream.common.get_parameters = in_get_parameters;
9381 in->stream.common.add_audio_effect = in_add_audio_effect;
9382 in->stream.common.remove_audio_effect = in_remove_audio_effect;
9383 in->stream.set_gain = in_set_gain;
9384 in->stream.read = in_read;
9385 in->stream.get_input_frames_lost = in_get_input_frames_lost;
Aalique Grahame22e49102018-12-18 14:23:57 -08009386 in->stream.get_capture_position = in_get_capture_position;
Naresh Tannirudcb47c52018-06-25 16:23:32 +05309387 in->stream.get_active_microphones = in_get_active_microphones;
Paul McLeana50b7332018-12-17 08:24:21 -07009388 in->stream.set_microphone_direction = in_set_microphone_direction;
9389 in->stream.set_microphone_field_dimension = in_set_microphone_field_dimension;
juyuchendb308c22019-01-21 11:57:17 -07009390 in->stream.update_sink_metadata = in_update_sink_metadata;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009391
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08009392 list_init(&in->device_list);
9393 update_device_list(&in->device_list, devices, address, true);
Vidyakumar Athota5c398212015-03-31 21:53:21 -07009394 in->source = source;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009395 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009396 in->standby = 1;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07009397 in->capture_handle = handle;
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07009398 in->flags = flags;
Haynes Mathew George46740472017-10-27 18:40:12 -07009399 in->bit_width = 16;
9400 in->af_period_multiplier = 1;
justinweng20fb6d82019-02-21 18:49:00 -07009401 in->direction = MIC_DIRECTION_UNSPECIFIED;
9402 in->zoom = 0;
Carter Hsu2e429db2019-05-14 18:50:52 +08009403 list_init(&in->aec_list);
9404 list_init(&in->ns_list);
Phil Burkd898ba62019-06-20 12:49:01 -07009405 in->mmap_shared_memory_fd = -1; // not open
Haynes Mathew George46740472017-10-27 18:40:12 -07009406
Andy Hung94320602018-10-29 18:31:12 -07009407 ALOGV("%s: source %d, config->channel_mask %#x", __func__, source, config->channel_mask);
Aalique Grahame22e49102018-12-18 14:23:57 -08009408 if (source == AUDIO_SOURCE_VOICE_UPLINK ||
9409 source == AUDIO_SOURCE_VOICE_DOWNLINK) {
9410 /* Force channel config requested to mono if incall
9411 record is being requested for only uplink/downlink */
9412 if (config->channel_mask != AUDIO_CHANNEL_IN_MONO) {
9413 config->channel_mask = AUDIO_CHANNEL_IN_MONO;
9414 ret = -EINVAL;
9415 goto err_open;
9416 }
9417 }
9418
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009419 if (is_usb_dev && may_use_hifi_record) {
9420 /* HiFi record selects an appropriate format, channel, rate combo
9421 depending on sink capabilities*/
9422 ret = read_usb_sup_params_and_compare(false /*is_playback*/,
9423 &config->format,
9424 &in->supported_formats[0],
9425 MAX_SUPPORTED_FORMATS,
9426 &config->channel_mask,
9427 &in->supported_channel_masks[0],
9428 MAX_SUPPORTED_CHANNEL_MASKS,
9429 &config->sample_rate,
9430 &in->supported_sample_rates[0],
9431 MAX_SUPPORTED_SAMPLE_RATES);
9432 if (ret != 0) {
9433 ret = -EINVAL;
9434 goto err_open;
9435 }
9436 channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Haynes Mathew George4ffef292017-11-21 15:08:02 -08009437 } else if (config->format == AUDIO_FORMAT_DEFAULT) {
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05309438 config->format = AUDIO_FORMAT_PCM_16_BIT;
Surendar karkaaca3d082017-11-09 15:18:37 +05309439 } else if (property_get_bool("vendor.audio.capture.pcm.32bit.enable", false)
9440 && config->format == AUDIO_FORMAT_PCM_32_BIT) {
9441 in->config.format = PCM_FORMAT_S32_LE;
9442 in->bit_width = 32;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05309443 } else if ((config->format == AUDIO_FORMAT_PCM_FLOAT) ||
9444 (config->format == AUDIO_FORMAT_PCM_32_BIT) ||
9445 (config->format == AUDIO_FORMAT_PCM_24_BIT_PACKED) ||
9446 (config->format == AUDIO_FORMAT_PCM_8_24_BIT)) {
9447 bool ret_error = false;
9448 in->bit_width = 24;
9449 /* 24 bit is restricted to UNPROCESSED source only,also format supported
9450 from HAL is 24_packed and 8_24
9451 *> In case of UNPROCESSED source, for 24 bit, if format requested is other than
9452 24_packed return error indicating supported format is 24_packed
9453 *> In case of any other source requesting 24 bit or float return error
9454 indicating format supported is 16 bit only.
9455
9456 on error flinger will retry with supported format passed
9457 */
9458 if ((source != AUDIO_SOURCE_UNPROCESSED) &&
9459 (source != AUDIO_SOURCE_CAMCORDER)) {
9460 config->format = AUDIO_FORMAT_PCM_16_BIT;
9461 if (config->sample_rate > 48000)
9462 config->sample_rate = 48000;
9463 ret_error = true;
Haynes Mathew George46740472017-10-27 18:40:12 -07009464 } else if (!(config->format == AUDIO_FORMAT_PCM_24_BIT_PACKED ||
9465 config->format == AUDIO_FORMAT_PCM_8_24_BIT)) {
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05309466 config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
9467 ret_error = true;
9468 }
9469
9470 if (ret_error) {
9471 ret = -EINVAL;
9472 goto err_open;
9473 }
9474 }
9475
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009476 in->channel_mask = config->channel_mask;
9477 in->format = config->format;
9478
9479 in->usecase = USECASE_AUDIO_RECORD;
Samyak Jain0aa07ab2019-04-04 14:36:32 +05309480
9481 if (in->source == AUDIO_SOURCE_FM_TUNER) {
9482 if(!get_usecase_from_list(adev, USECASE_AUDIO_RECORD_FM_VIRTUAL))
9483 in->usecase = USECASE_AUDIO_RECORD_FM_VIRTUAL;
9484 else {
9485 ret = -EINVAL;
9486 goto err_open;
9487 }
9488 }
9489
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009490 if (config->sample_rate == LOW_LATENCY_CAPTURE_SAMPLE_RATE &&
Deeraj Somanfa377bf2019-02-06 12:57:59 +05309491 (flags & AUDIO_INPUT_FLAG_TIMESTAMP) == 0 &&
9492 (flags & AUDIO_INPUT_FLAG_COMPRESS) == 0 &&
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009493 (flags & AUDIO_INPUT_FLAG_FAST) != 0) {
9494 is_low_latency = true;
9495#if LOW_LATENCY_CAPTURE_USE_CASE
9496 in->usecase = USECASE_AUDIO_RECORD_LOW_LATENCY;
9497#endif
9498 in->realtime = may_use_noirq_mode(adev, in->usecase, in->flags);
Aalique Grahame22e49102018-12-18 14:23:57 -08009499 if (!in->realtime) {
9500 in->config = pcm_config_audio_capture;
9501 frame_size = audio_stream_in_frame_size(&in->stream);
9502 buffer_size = get_input_buffer_size(config->sample_rate,
9503 config->format,
9504 channel_count,
9505 is_low_latency);
9506 in->config.period_size = buffer_size / frame_size;
9507 in->config.rate = config->sample_rate;
9508 in->af_period_multiplier = 1;
9509 } else {
9510 // period size is left untouched for rt mode playback
9511 in->config = pcm_config_audio_capture_rt;
9512 in->af_period_multiplier = af_period_multiplier;
9513 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009514 }
9515
9516 if ((config->sample_rate == LOW_LATENCY_CAPTURE_SAMPLE_RATE) &&
9517 ((in->flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0)) {
9518 in->realtime = 0;
9519 in->usecase = USECASE_AUDIO_RECORD_MMAP;
9520 in->config = pcm_config_mmap_capture;
Haynes Mathew George46740472017-10-27 18:40:12 -07009521 in->config.format = pcm_format_from_audio_format(config->format);
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009522 in->stream.start = in_start;
9523 in->stream.stop = in_stop;
9524 in->stream.create_mmap_buffer = in_create_mmap_buffer;
9525 in->stream.get_mmap_position = in_get_mmap_position;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009526 ALOGV("%s: USECASE_AUDIO_RECORD_MMAP", __func__);
Haynes Mathew George46740472017-10-27 18:40:12 -07009527 } else if (is_usb_dev && may_use_hifi_record) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009528 in->usecase = USECASE_AUDIO_RECORD_HIFI;
9529 in->config = pcm_config_audio_capture;
9530 frame_size = audio_stream_in_frame_size(&in->stream);
9531 buffer_size = get_input_buffer_size(config->sample_rate,
9532 config->format,
9533 channel_count,
9534 false /*is_low_latency*/);
9535 in->config.period_size = buffer_size / frame_size;
9536 in->config.rate = config->sample_rate;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009537 in->config.format = pcm_format_from_audio_format(config->format);
Karthikeyan Mani07faa602018-08-20 11:01:32 -07009538 switch (config->format) {
9539 case AUDIO_FORMAT_PCM_32_BIT:
9540 in->bit_width = 32;
9541 break;
9542 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
9543 case AUDIO_FORMAT_PCM_8_24_BIT:
9544 in->bit_width = 24;
9545 break;
9546 default:
9547 in->bit_width = 16;
9548 }
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08009549 } else if (is_single_device_type_equal(&in->device_list,
9550 AUDIO_DEVICE_IN_TELEPHONY_RX) ||
9551 is_single_device_type_equal(&in->device_list,
9552 AUDIO_DEVICE_IN_PROXY)) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07009553 if (config->sample_rate == 0)
9554 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
9555 if (config->sample_rate != 48000 && config->sample_rate != 16000 &&
9556 config->sample_rate != 8000) {
9557 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
9558 ret = -EINVAL;
9559 goto err_open;
9560 }
9561 if (config->format == AUDIO_FORMAT_DEFAULT)
9562 config->format = AUDIO_FORMAT_PCM_16_BIT;
9563 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
9564 config->format = AUDIO_FORMAT_PCM_16_BIT;
9565 ret = -EINVAL;
9566 goto err_open;
9567 }
9568
9569 in->usecase = USECASE_AUDIO_RECORD_AFE_PROXY;
Zhou Song62ea0282020-03-22 19:53:01 +08009570 if (adev->ha_proxy_enable &&
9571 is_single_device_type_equal(&in->device_list,
9572 AUDIO_DEVICE_IN_TELEPHONY_RX))
9573 in->usecase = USECASE_AUDIO_RECORD_AFE_PROXY2;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07009574 in->config = pcm_config_afe_proxy_record;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07009575 in->config.rate = config->sample_rate;
Aalique Grahame22e49102018-12-18 14:23:57 -08009576 in->af_period_multiplier = 1;
9577 } else if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
9578 in->flags & AUDIO_INPUT_FLAG_VOIP_TX &&
9579 (config->sample_rate == 8000 ||
9580 config->sample_rate == 16000 ||
9581 config->sample_rate == 32000 ||
9582 config->sample_rate == 48000) &&
9583 channel_count == 1) {
9584 in->usecase = USECASE_AUDIO_RECORD_VOIP;
9585 in->config = pcm_config_audio_capture;
9586 frame_size = audio_stream_in_frame_size(&in->stream);
9587 buffer_size = get_stream_buffer_size(VOIP_CAPTURE_PERIOD_DURATION_MSEC,
9588 config->sample_rate,
9589 config->format,
9590 channel_count, false /*is_low_latency*/);
9591 in->config.period_size = buffer_size / frame_size;
9592 in->config.period_count = VOIP_CAPTURE_PERIOD_COUNT;
9593 in->config.rate = config->sample_rate;
9594 in->af_period_multiplier = 1;
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();
Daniel Hillenbrand21752f02013-05-23 10:10:00 +053010297 if (amplifier_close() != 0)
10298 ALOGE("Amplifier close failed");
Jaideep Sharmaa2b49672019-09-10 20:37:03 +053010299 audio_extn_battery_properties_listener_deinit();
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053010300 audio_extn_snd_mon_unregister_listener(adev);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -070010301 audio_extn_sound_trigger_deinit(adev);
Kiran Kandide144c82013-11-20 15:58:32 -080010302 audio_extn_listen_deinit(adev);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -080010303 audio_extn_qdsp_deinit();
Aalique Grahame22e49102018-12-18 14:23:57 -080010304 audio_extn_extspk_deinit(adev->extspk);
Dhananjay Kumard6d32152016-10-13 16:11:03 +053010305 audio_extn_utils_release_streams_cfg_lists(
10306 &adev->streams_output_cfg_list,
10307 &adev->streams_input_cfg_list);
Sidipotu Ashokd2f10ba2019-05-06 15:41:56 +053010308 if (audio_extn_qap_is_enabled())
10309 audio_extn_qap_deinit();
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +053010310 if (audio_extn_qaf_is_enabled())
10311 audio_extn_qaf_deinit();
Kiran Kandi910e1862013-10-29 13:29:42 -070010312 audio_route_free(adev->audio_route);
Weiyin Jiangfa65d3e2019-03-05 23:39:45 +080010313 audio_extn_gef_deinit(adev);
Kiran Kandi910e1862013-10-29 13:29:42 -070010314 free(adev->snd_dev_ref_cnt);
10315 platform_deinit(adev->platform);
Aalique Grahame22e49102018-12-18 14:23:57 -080010316 for (i = 0; i < ARRAY_SIZE(adev->use_case_table); ++i) {
10317 pcm_params_free(adev->use_case_table[i]);
10318 }
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -070010319 if (adev->adm_deinit)
10320 adev->adm_deinit(adev->adm_data);
Dhananjay Kumaree4d2002016-10-25 18:02:58 +053010321 qahwi_deinit(device);
Ben Rombergerd771a7c2017-02-22 18:05:17 -080010322 audio_extn_adsp_hdlr_deinit();
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053010323 audio_extn_snd_mon_deinit();
Siddartha Shaik44dd7702017-06-14 12:13:25 +053010324 audio_extn_hw_loopback_deinit(adev);
Garmond Leunge2433c32017-09-28 21:51:22 -070010325 audio_extn_ffv_deinit();
Satish Babu Patakokilac3c5d432017-07-04 22:48:59 +053010326 if (adev->device_cfg_params) {
10327 free(adev->device_cfg_params);
10328 adev->device_cfg_params = NULL;
10329 }
Derek Chend2530072014-11-24 12:39:14 -080010330 if(adev->ext_hw_plugin)
10331 audio_extn_ext_hw_plugin_deinit(adev->ext_hw_plugin);
Derek Chenae7b0342019-02-08 15:17:04 -080010332 audio_extn_auto_hal_deinit();
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010333 free_map(adev->patch_map);
10334 free_map(adev->io_streams_map);
Kiran Kandi910e1862013-10-29 13:29:42 -070010335 free(device);
10336 adev = NULL;
10337 }
10338 pthread_mutex_unlock(&adev_init_lock);
Vatsal Buchac09ae062018-11-14 13:25:08 +053010339 enable_gcov();
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010340 return 0;
10341}
10342
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -070010343/* This returns 1 if the input parameter looks at all plausible as a low latency period size,
10344 * or 0 otherwise. A return value of 1 doesn't mean the value is guaranteed to work,
10345 * just that it _might_ work.
10346 */
10347static int period_size_is_plausible_for_low_latency(int period_size)
10348{
10349 switch (period_size) {
10350 case 160:
Ravi Kumar Alamanda474de5a2015-06-25 20:08:01 -070010351 case 192:
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -070010352 case 240:
10353 case 320:
10354 case 480:
10355 return 1;
10356 default:
10357 return 0;
10358 }
10359}
10360
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053010361static void adev_snd_mon_cb(void *cookie, struct str_parms *parms)
10362{
10363 bool is_snd_card_status = false;
10364 bool is_ext_device_status = false;
10365 char value[32];
10366 int card = -1;
10367 card_status_t status;
10368
10369 if (cookie != adev || !parms)
10370 return;
10371
10372 if (!parse_snd_card_status(parms, &card, &status)) {
10373 is_snd_card_status = true;
10374 } else if (0 < str_parms_get_str(parms, "ext_audio_device", value, sizeof(value))) {
10375 is_ext_device_status = true;
10376 } else {
10377 // not a valid event
10378 return;
10379 }
10380
10381 pthread_mutex_lock(&adev->lock);
10382 if (card == adev->snd_card || is_ext_device_status) {
10383 if (is_snd_card_status && adev->card_status != status) {
10384 adev->card_status = status;
10385 platform_snd_card_update(adev->platform, status);
10386 audio_extn_fm_set_parameters(adev, parms);
Derek Chend6f371d2019-03-01 13:45:58 -080010387 audio_extn_auto_hal_set_parameters(adev, parms);
Zhou Song4f43e352019-07-02 00:32:23 +080010388 if (status == CARD_STATUS_OFFLINE)
10389 audio_extn_sco_reset_configuration();
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053010390 } else if (is_ext_device_status) {
10391 platform_set_parameters(adev->platform, parms);
10392 }
10393 }
10394 pthread_mutex_unlock(&adev->lock);
10395 return;
10396}
10397
Weiyin Jiangd5974e62020-09-08 20:28:22 +080010398/* adev lock held */
10399int check_a2dp_restore_l(struct audio_device *adev, struct stream_out *out, bool restore)
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010400{
10401 struct audio_usecase *uc_info;
10402 float left_p;
10403 float right_p;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010404 struct listnode devices;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010405
10406 uc_info = get_usecase_from_list(adev, out->usecase);
10407 if (uc_info == NULL) {
10408 ALOGE("%s: Could not find the usecase (%d) in the list",
10409 __func__, out->usecase);
10410 return -EINVAL;
10411 }
Aniket Kumar Lata9c2fd892020-01-22 22:20:00 -080010412 list_init(&devices);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010413
10414 ALOGD("%s: enter: usecase(%d: %s)", __func__,
10415 out->usecase, use_case_table[out->usecase]);
10416
10417 if (restore) {
Weiyin Jiangd5974e62020-09-08 20:28:22 +080010418 pthread_mutex_lock(&out->latch_lock);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010419 // restore A2DP device for active usecases and unmute if required
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010420 if (is_a2dp_out_device_type(&out->device_list)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010421 ALOGD("%s: restoring A2dp and unmuting stream", __func__);
Zhou Songe5225132019-09-26 15:33:36 +080010422 if (uc_info->out_snd_device != SND_DEVICE_OUT_BT_A2DP)
10423 select_devices(adev, uc_info->id);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010424 if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
Weiyin Jiangd5974e62020-09-08 20:28:22 +080010425 (uc_info->out_snd_device == SND_DEVICE_OUT_BT_A2DP)) {
10426 if (out->a2dp_compress_mute) {
10427 out->a2dp_compress_mute = false;
10428 out_set_compr_volume(&out->stream, out->volume_l, out->volume_r);
10429 }
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010430 }
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010431 }
Weiyin Jiangd5974e62020-09-08 20:28:22 +080010432 out->muted = false;
10433 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 Songc576a452019-09-09 14:17:40 +080010436 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
10437 // mute compress stream if suspended
Zhou Songc576a452019-09-09 14:17:40 +080010438 if (!out->a2dp_compress_mute && !out->standby) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010439 ALOGD("%s: selecting speaker and muting stream", __func__);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010440 assign_devices(&devices, &out->device_list);
10441 reassign_device_list(&out->device_list, AUDIO_DEVICE_OUT_SPEAKER, "");
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010442 left_p = out->volume_l;
10443 right_p = out->volume_r;
10444 if (out->offload_state == OFFLOAD_STATE_PLAYING)
10445 compress_pause(out->compr);
10446 out_set_compr_volume(&out->stream, (float)0, (float)0);
10447 out->a2dp_compress_mute = true;
10448 select_devices(adev, out->usecase);
10449 if (out->offload_state == OFFLOAD_STATE_PLAYING)
10450 compress_resume(out->compr);
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -080010451 assign_devices(&out->device_list, &devices);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010452 out->volume_l = left_p;
10453 out->volume_r = right_p;
10454 }
Zhou Songc576a452019-09-09 14:17:40 +080010455 } else {
Weiyin Jiangd5974e62020-09-08 20:28:22 +080010456 // mute for non offloaded streams
10457 if (audio_extn_a2dp_source_is_suspended()) {
10458 out->muted = true;
10459 }
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010460 }
Weiyin Jiangd5974e62020-09-08 20:28:22 +080010461 pthread_mutex_unlock(&out->latch_lock);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +053010462 }
10463 ALOGV("%s: exit", __func__);
10464 return 0;
10465}
10466
Haynes Mathew George01156f92018-04-13 15:29:54 -070010467void adev_on_battery_status_changed(bool charging)
10468{
10469 pthread_mutex_lock(&adev->lock);
10470 ALOGI("%s: battery status changed to %scharging", __func__, charging ? "" : "not ");
10471 adev->is_charging = charging;
Zhou Songc1088ea2018-06-12 00:17:29 +080010472 audio_extn_sound_trigger_update_battery_status(charging);
Haynes Mathew George01156f92018-04-13 15:29:54 -070010473 pthread_mutex_unlock(&adev->lock);
10474}
10475
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010476static int adev_open(const hw_module_t *module, const char *name,
10477 hw_device_t **device)
10478{
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +053010479 int ret;
Derek Chenf939fb72018-11-13 13:34:41 -080010480 char value[PROPERTY_VALUE_MAX] = {0};
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +053010481 char mixer_ctl_name[128] = {0};
10482 struct mixer_ctl *ctl = NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +053010483
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -080010484 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010485 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
10486
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070010487 pthread_mutex_lock(&adev_init_lock);
Kiran Kandi910e1862013-10-29 13:29:42 -070010488 if (audio_device_ref_count != 0){
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070010489 *device = &adev->device.common;
Kiran Kandi910e1862013-10-29 13:29:42 -070010490 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070010491 ALOGD("%s: returning existing instance of adev", __func__);
10492 ALOGD("%s: exit", __func__);
10493 pthread_mutex_unlock(&adev_init_lock);
10494 return 0;
10495 }
10496
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010497 adev = calloc(1, sizeof(struct audio_device));
10498
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -070010499 if (!adev) {
10500 pthread_mutex_unlock(&adev_init_lock);
10501 return -ENOMEM;
10502 }
10503
Ravi Kumar Alamanda40703102014-04-24 10:34:41 -070010504 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
10505
Weiyin Jiange6ce6312019-01-28 18:28:22 +080010506 // register audio ext hidl at the earliest
10507 audio_extn_hidl_init();
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053010508#ifdef DYNAMIC_LOG_ENABLED
10509 register_for_dynamic_logging("hal");
10510#endif
10511
Derek Chenf939fb72018-11-13 13:34:41 -080010512 /* default audio HAL major version */
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010513 uint32_t maj_version = 3;
Derek Chenf939fb72018-11-13 13:34:41 -080010514 if(property_get("vendor.audio.hal.maj.version", value, NULL))
10515 maj_version = atoi(value);
10516
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010517 adev->device.common.tag = HARDWARE_DEVICE_TAG;
Derek Chenf939fb72018-11-13 13:34:41 -080010518 adev->device.common.version = HARDWARE_DEVICE_API_VERSION(maj_version, 0);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010519 adev->device.common.module = (struct hw_module_t *)module;
10520 adev->device.common.close = adev_close;
10521
10522 adev->device.init_check = adev_init_check;
10523 adev->device.set_voice_volume = adev_set_voice_volume;
10524 adev->device.set_master_volume = adev_set_master_volume;
10525 adev->device.get_master_volume = adev_get_master_volume;
10526 adev->device.set_master_mute = adev_set_master_mute;
10527 adev->device.get_master_mute = adev_get_master_mute;
10528 adev->device.set_mode = adev_set_mode;
10529 adev->device.set_mic_mute = adev_set_mic_mute;
10530 adev->device.get_mic_mute = adev_get_mic_mute;
10531 adev->device.set_parameters = adev_set_parameters;
10532 adev->device.get_parameters = adev_get_parameters;
10533 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
10534 adev->device.open_output_stream = adev_open_output_stream;
10535 adev->device.close_output_stream = adev_close_output_stream;
10536 adev->device.open_input_stream = adev_open_input_stream;
10537 adev->device.close_input_stream = adev_close_input_stream;
Siddartha Shaik31b530e2017-05-19 15:26:33 +053010538 adev->device.create_audio_patch = adev_create_audio_patch;
10539 adev->device.release_audio_patch = adev_release_audio_patch;
10540 adev->device.get_audio_port = adev_get_audio_port;
10541 adev->device.set_audio_port_config = adev_set_audio_port_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010542 adev->device.dump = adev_dump;
Naresh Tannirudcb47c52018-06-25 16:23:32 +053010543 adev->device.get_microphones = adev_get_microphones;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010544
10545 /* Set the default route before the PCM stream is opened */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010546 adev->mode = AUDIO_MODE_NORMAL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -080010547 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010548 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010549 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -080010550 adev->acdb_settings = TTY_MODE_OFF;
vivek mehta344576a2016-04-12 18:56:03 -070010551 adev->allow_afe_proxy_usage = true;
Ashish Jain1b9b30c2017-05-18 20:57:40 +053010552 adev->bt_sco_on = false;
Eric Laurent07eeafd2013-10-06 12:52:49 -070010553 /* adev->cur_hdmi_channels = 0; by calloc() */
Eric Laurentb23d5282013-05-14 15:27:20 -070010554 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
vivek mehtaae1018c2019-05-09 12:19:57 -070010555 /* Init audio and voice feature */
10556 audio_extn_feature_init();
10557 voice_extn_feature_init();
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -070010558 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -080010559 list_init(&adev->usecase_list);
Derek Chenf939fb72018-11-13 13:34:41 -080010560 list_init(&adev->active_inputs_list);
10561 list_init(&adev->active_outputs_list);
Rahul Sharma99770982019-03-06 17:05:26 +053010562 list_init(&adev->audio_patch_record_list);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010563 adev->io_streams_map = hashmapCreate(AUDIO_IO_PORTS_MAX, audio_extn_utils_hash_fn,
10564 audio_extn_utils_hash_eq);
10565 if (!adev->io_streams_map) {
10566 ALOGE("%s: Could not create io streams map", __func__);
10567 ret = -ENOMEM;
10568 goto adev_open_err;
10569 }
10570 adev->patch_map = hashmapCreate(AUDIO_PATCH_PORTS_MAX, audio_extn_utils_hash_fn,
10571 audio_extn_utils_hash_eq);
10572 if (!adev->patch_map) {
10573 ALOGE("%s: Could not create audio patch map", __func__);
10574 ret = -ENOMEM;
10575 goto adev_open_err;
10576 }
Krishnankutty Kolathappilly0b2de1c2014-02-14 14:45:49 -080010577 adev->cur_wfd_channels = 2;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -070010578 adev->offload_usecases_state = 0;
Samyak Jain15fda662018-12-18 16:40:52 +053010579 adev->pcm_record_uc_state = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +053010580 adev->is_channel_status_set = false;
Sudheer Papothifa9d2282015-09-17 01:53:25 +053010581 adev->perf_lock_opts[0] = 0x101;
10582 adev->perf_lock_opts[1] = 0x20E;
10583 adev->perf_lock_opts_size = 2;
Xiaojun Sang785b5da2017-08-03 15:52:29 +080010584 adev->dsp_bit_width_enforce_mode = 0;
Aalique Grahame552b0832019-03-11 10:16:38 -070010585 adev->enable_hfp = false;
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +053010586 adev->use_old_pspd_mix_ctrl = false;
Aniket Kumar Lata60586a92019-05-22 22:18:55 -070010587 adev->adm_routing_changed = false;
Revathi Uddarajub26e3932020-06-10 14:51:02 +053010588 adev->a2dp_started = false;
Naresh Tanniru4c630392014-05-12 01:05:52 +053010589
Zhou Song68ebc352019-12-05 17:11:15 +080010590 audio_extn_perf_lock_init();
10591
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010592 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -070010593 adev->platform = platform_init(adev);
10594 if (!adev->platform) {
Eric Laurentb23d5282013-05-14 15:27:20 -070010595 ALOGE("%s: Failed to init platform data, aborting.", __func__);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010596 ret = -EINVAL;
10597 goto adev_open_err;
Eric Laurentb23d5282013-05-14 15:27:20 -070010598 }
Eric Laurentc4aef752013-09-12 17:45:53 -070010599
Aalique Grahame22e49102018-12-18 14:23:57 -080010600 adev->extspk = audio_extn_extspk_init(adev);
Sidipotu Ashokd2f10ba2019-05-06 15:41:56 +053010601 if (audio_extn_qap_is_enabled()) {
10602 ret = audio_extn_qap_init(adev);
10603 if (ret < 0) {
Sidipotu Ashokd2f10ba2019-05-06 15:41:56 +053010604 ALOGE("%s: Failed to init platform data, aborting.", __func__);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010605 goto adev_open_err;
Sidipotu Ashokd2f10ba2019-05-06 15:41:56 +053010606 }
10607 adev->device.open_output_stream = audio_extn_qap_open_output_stream;
10608 adev->device.close_output_stream = audio_extn_qap_close_output_stream;
10609 }
Aalique Grahame22e49102018-12-18 14:23:57 -080010610
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +053010611 if (audio_extn_qaf_is_enabled()) {
10612 ret = audio_extn_qaf_init(adev);
10613 if (ret < 0) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +053010614 ALOGE("%s: Failed to init platform data, aborting.", __func__);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010615 goto adev_open_err;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +053010616 }
10617
10618 adev->device.open_output_stream = audio_extn_qaf_open_output_stream;
10619 adev->device.close_output_stream = audio_extn_qaf_close_output_stream;
10620 }
10621
Derek Chenae7b0342019-02-08 15:17:04 -080010622 audio_extn_auto_hal_init(adev);
Derek Chend2530072014-11-24 12:39:14 -080010623 adev->ext_hw_plugin = audio_extn_ext_hw_plugin_init(adev);
10624
Eric Laurentc4aef752013-09-12 17:45:53 -070010625 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
10626 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
10627 if (adev->visualizer_lib == NULL) {
10628 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
10629 } else {
10630 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
10631 adev->visualizer_start_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -080010632 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -070010633 "visualizer_hal_start_output");
10634 adev->visualizer_stop_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -080010635 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -070010636 "visualizer_hal_stop_output");
10637 }
10638 }
Dhanalakshmi Siddani21be3ac2016-12-29 14:31:08 +053010639 audio_extn_init(adev);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -080010640 voice_extn_init(adev);
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -080010641 audio_extn_listen_init(adev, adev->snd_card);
Weiyin Jiangaa80acd2016-09-21 16:42:11 +080010642 audio_extn_gef_init(adev);
Siddartha Shaik44dd7702017-06-14 12:13:25 +053010643 audio_extn_hw_loopback_init(adev);
Garmond Leunge2433c32017-09-28 21:51:22 -070010644 audio_extn_ffv_init(adev);
Eric Laurentc4aef752013-09-12 17:45:53 -070010645
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -080010646 if (access(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, R_OK) == 0) {
10647 adev->offload_effects_lib = dlopen(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, RTLD_NOW);
10648 if (adev->offload_effects_lib == NULL) {
10649 ALOGE("%s: DLOPEN failed for %s", __func__,
10650 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
10651 } else {
10652 ALOGV("%s: DLOPEN successful for %s", __func__,
10653 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
10654 adev->offload_effects_start_output =
Ashish Jain5106d362016-05-11 19:23:33 +053010655 (int (*)(audio_io_handle_t, int, struct mixer *))dlsym(adev->offload_effects_lib,
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -080010656 "offload_effects_bundle_hal_start_output");
10657 adev->offload_effects_stop_output =
10658 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
10659 "offload_effects_bundle_hal_stop_output");
Jitendra Naruka1b6513f2014-11-22 19:34:13 -080010660 adev->offload_effects_set_hpx_state =
10661 (int (*)(bool))dlsym(adev->offload_effects_lib,
10662 "offload_effects_bundle_set_hpx_state");
Dhananjay Kumard68883d2015-09-04 13:39:26 +053010663 adev->offload_effects_get_parameters =
10664 (void (*)(struct str_parms *, struct str_parms *))
10665 dlsym(adev->offload_effects_lib,
10666 "offload_effects_bundle_get_parameters");
10667 adev->offload_effects_set_parameters =
10668 (void (*)(struct str_parms *))dlsym(adev->offload_effects_lib,
10669 "offload_effects_bundle_set_parameters");
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -080010670 }
10671 }
10672
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -070010673 if (access(ADM_LIBRARY_PATH, R_OK) == 0) {
10674 adev->adm_lib = dlopen(ADM_LIBRARY_PATH, RTLD_NOW);
10675 if (adev->adm_lib == NULL) {
10676 ALOGE("%s: DLOPEN failed for %s", __func__, ADM_LIBRARY_PATH);
10677 } else {
10678 ALOGV("%s: DLOPEN successful for %s", __func__, ADM_LIBRARY_PATH);
10679 adev->adm_init = (adm_init_t)
10680 dlsym(adev->adm_lib, "adm_init");
10681 adev->adm_deinit = (adm_deinit_t)
10682 dlsym(adev->adm_lib, "adm_deinit");
10683 adev->adm_register_input_stream = (adm_register_input_stream_t)
10684 dlsym(adev->adm_lib, "adm_register_input_stream");
10685 adev->adm_register_output_stream = (adm_register_output_stream_t)
10686 dlsym(adev->adm_lib, "adm_register_output_stream");
10687 adev->adm_deregister_stream = (adm_deregister_stream_t)
10688 dlsym(adev->adm_lib, "adm_deregister_stream");
10689 adev->adm_request_focus = (adm_request_focus_t)
10690 dlsym(adev->adm_lib, "adm_request_focus");
10691 adev->adm_abandon_focus = (adm_abandon_focus_t)
10692 dlsym(adev->adm_lib, "adm_abandon_focus");
Haynes Mathew George5beddd42016-06-27 18:33:40 -070010693 adev->adm_set_config = (adm_set_config_t)
10694 dlsym(adev->adm_lib, "adm_set_config");
10695 adev->adm_request_focus_v2 = (adm_request_focus_v2_t)
10696 dlsym(adev->adm_lib, "adm_request_focus_v2");
10697 adev->adm_is_noirq_avail = (adm_is_noirq_avail_t)
10698 dlsym(adev->adm_lib, "adm_is_noirq_avail");
10699 adev->adm_on_routing_change = (adm_on_routing_change_t)
10700 dlsym(adev->adm_lib, "adm_on_routing_change");
Aniket Kumar Lata60586a92019-05-22 22:18:55 -070010701 adev->adm_request_focus_v2_1 = (adm_request_focus_v2_1_t)
10702 dlsym(adev->adm_lib, "adm_request_focus_v2_1");
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -070010703 }
10704 }
10705
Aalique Grahame22e49102018-12-18 14:23:57 -080010706 adev->enable_voicerx = false;
Mingming Yin514a8bc2014-07-29 15:22:21 -070010707 adev->bt_wb_speech_enabled = false;
Zhou Song12c29502019-03-16 10:37:18 +080010708 adev->swb_speech_mode = SPEECH_MODE_INVALID;
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -080010709 //initialize this to false for now,
10710 //this will be set to true through set param
10711 adev->vr_audio_mode_enabled = false;
Mingming Yin514a8bc2014-07-29 15:22:21 -070010712
Pradnya Chaphekar8a9dcd82014-09-09 09:49:10 -070010713 audio_extn_ds2_enable(adev);
Daniel Hillenbrand21752f02013-05-23 10:10:00 +053010714
10715 if (amplifier_open(adev) != 0)
10716 ALOGE("Amplifier initialization failed");
10717
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010718 *device = &adev->device.common;
Aalique Grahame22e49102018-12-18 14:23:57 -080010719
10720 if (k_enable_extended_precision)
10721 adev_verify_devices(adev);
10722
Xiaojun Sang785b5da2017-08-03 15:52:29 +080010723 adev->dsp_bit_width_enforce_mode =
10724 adev_init_dsp_bit_width_enforce_mode(adev->mixer);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010725
Dhananjay Kumard6d32152016-10-13 16:11:03 +053010726 audio_extn_utils_update_streams_cfg_lists(adev->platform, adev->mixer,
10727 &adev->streams_output_cfg_list,
10728 &adev->streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -070010729
Kiran Kandi910e1862013-10-29 13:29:42 -070010730 audio_device_ref_count++;
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -070010731
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -070010732 int trial;
Manisha Agarwalc75a0202019-12-06 18:48:25 +053010733 if (property_get("vendor.audio_hal.period_size", value, NULL) > 0) {
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -070010734 trial = atoi(value);
10735 if (period_size_is_plausible_for_low_latency(trial)) {
10736 pcm_config_low_latency.period_size = trial;
10737 pcm_config_low_latency.start_threshold = trial / 4;
10738 pcm_config_low_latency.avail_min = trial / 4;
10739 configured_low_latency_capture_period_size = trial;
10740 }
10741 }
Aalique Grahame22e49102018-12-18 14:23:57 -080010742 if ((property_get("vendor.audio_hal.in_period_size", value, NULL) > 0) ||
10743 (property_get("audio_hal.in_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 configured_low_latency_capture_period_size = trial;
10747 }
10748 }
10749
Vignesh Kulothungan7d374312018-02-21 17:12:00 -080010750 adev->mic_break_enabled = property_get_bool("vendor.audio.mic_break", false);
10751
Eric Laurent4b084132018-10-19 17:33:43 -070010752 adev->camera_orientation = CAMERA_DEFAULT;
10753
Manisha Agarwalc75a0202019-12-06 18:48:25 +053010754 if (property_get("vendor.audio_hal.period_multiplier",value,NULL) > 0) {
Haynes Mathew George5beddd42016-06-27 18:33:40 -070010755 af_period_multiplier = atoi(value);
10756 if (af_period_multiplier < 0)
10757 af_period_multiplier = 2;
10758 else if (af_period_multiplier > 4)
10759 af_period_multiplier = 4;
10760
10761 ALOGV("new period_multiplier = %d", af_period_multiplier);
10762 }
10763
Arun Mirpurib1bec9c2019-01-29 16:42:45 -080010764 audio_extn_qdsp_init(adev->platform);
Aalique Grahame22e49102018-12-18 14:23:57 -080010765
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -070010766 adev->multi_offload_enable = property_get_bool("vendor.audio.offload.multiple.enabled", false);
Zhou Song62ea0282020-03-22 19:53:01 +080010767 adev->ha_proxy_enable = property_get_bool("persist.vendor.audio.ha_proxy.enabled", false);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070010768 pthread_mutex_unlock(&adev_init_lock);
10769
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -070010770 if (adev->adm_init)
10771 adev->adm_data = adev->adm_init();
10772
Dhananjay Kumaree4d2002016-10-25 18:02:58 +053010773 qahwi_init(*device);
Ben Rombergerd771a7c2017-02-22 18:05:17 -080010774 audio_extn_adsp_hdlr_init(adev->mixer);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053010775
10776 audio_extn_snd_mon_init();
10777 pthread_mutex_lock(&adev->lock);
10778 audio_extn_snd_mon_register_listener(adev, adev_snd_mon_cb);
10779 adev->card_status = CARD_STATUS_ONLINE;
Haynes Mathew George01156f92018-04-13 15:29:54 -070010780 audio_extn_battery_properties_listener_init(adev_on_battery_status_changed);
10781 /*
10782 * if the battery state callback happens before charging can be queried,
10783 * it will be guarded with the adev->lock held in the cb function and so
10784 * the callback value will reflect the latest state
10785 */
10786 adev->is_charging = audio_extn_battery_properties_is_charging();
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053010787 audio_extn_sound_trigger_init(adev); /* dependent on snd_mon_init() */
Zhou Songc1088ea2018-06-12 00:17:29 +080010788 audio_extn_sound_trigger_update_battery_status(adev->is_charging);
justinweng20fb6d82019-02-21 18:49:00 -070010789 audio_extn_audiozoom_init();
Zhou Songc1088ea2018-06-12 00:17:29 +080010790 pthread_mutex_unlock(&adev->lock);
Satish Babu Patakokilac3c5d432017-07-04 22:48:59 +053010791 /* Allocate memory for Device config params */
10792 adev->device_cfg_params = (struct audio_device_config_param*)
10793 calloc(platform_get_max_codec_backend(),
10794 sizeof(struct audio_device_config_param));
10795 if (adev->device_cfg_params == NULL)
10796 ALOGE("%s: Memory allocation failed for Device config params", __func__);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +053010797
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +053010798 /*
10799 * Check if new PSPD matrix mixer control is supported. If not
10800 * supported, then set flag so that old mixer ctrl is sent while
10801 * sending pspd coefficients on older kernel version. Query mixer
10802 * control for default pcm id and channel value one.
10803 */
10804 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
10805 "AudStr %d ChMixer Weight Ch %d", 0, 1);
10806
10807 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
10808 if (!ctl) {
10809 ALOGE("%s: ERROR. Could not get ctl for mixer cmd - %s",
10810 __func__, mixer_ctl_name);
10811 adev->use_old_pspd_mix_ctrl = true;
10812 }
10813
Eric Laurent994a6932013-07-17 11:51:42 -070010814 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010815 return 0;
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010816
10817adev_open_err:
10818 free_map(adev->patch_map);
10819 free_map(adev->io_streams_map);
Aniket Kumar Latabc774842020-01-16 21:22:05 -080010820 free(adev->snd_dev_ref_cnt);
Aniket Kumar Lata1fda9432019-11-01 17:08:33 -070010821 pthread_mutex_destroy(&adev->lock);
10822 free(adev);
10823 adev = NULL;
10824 *device = NULL;
10825 pthread_mutex_unlock(&adev_init_lock);
10826 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010827}
10828
10829static struct hw_module_methods_t hal_module_methods = {
10830 .open = adev_open,
10831};
10832
10833struct audio_module HAL_MODULE_INFO_SYM = {
10834 .common = {
10835 .tag = HARDWARE_MODULE_TAG,
10836 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
10837 .hal_api_version = HARDWARE_HAL_API_VERSION,
10838 .id = AUDIO_HARDWARE_MODULE_ID,
10839 .name = "QCOM Audio HAL",
Duy Truongfae19622013-11-24 02:17:54 -080010840 .author = "The Linux Foundation",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010841 .methods = &hal_module_methods,
10842 },
10843};