blob: 0df198105bbd7abbafc9eefb2024d1b2094d2cc4 [file] [log] [blame]
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001/*
Aalique Grahame22e49102018-12-18 14:23:57 -08002 * Copyright (c) 2013-2019, 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
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080047
48#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>
Eric Laurentb23d5282013-05-14 15:27:20 -070068#include <audio_effects/effect_aec.h>
69#include <audio_effects/effect_ns.h>
Ashish Jainf1eaa582016-05-23 20:54:24 +053070#include <audio_utils/format.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080071#include "audio_hw.h"
Wei Wangf7ca6c92017-11-21 14:51:20 -080072#include "audio_perf.h"
Eric Laurentb23d5282013-05-14 15:27:20 -070073#include "platform_api.h"
74#include <platform.h>
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -070075#include "audio_extn.h"
Narsinga Rao Chella05573b72013-11-15 15:21:40 -080076#include "voice_extn.h"
Ashish Jaind5694242017-09-05 20:09:06 +053077#include "ip_hdlr_intf.h"
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080078
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070079#include "sound/compress_params.h"
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -080080#include "sound/asound.h"
ApurupaPattapu2e084df2013-12-18 15:47:59 -080081
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053082#ifdef DYNAMIC_LOG_ENABLED
83#include <log_xml_parser.h>
84#define LOG_MASK HAL_MOD_FILE_AUDIO_HW
85#include <log_utils.h>
86#endif
87
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070088#define COMPRESS_OFFLOAD_NUM_FRAGMENTS 4
Ashish Jain5106d362016-05-11 19:23:33 +053089/*DIRECT PCM has same buffer sizes as DEEP Buffer*/
90#define DIRECT_PCM_NUM_FRAGMENTS 2
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070091#define COMPRESS_PLAYBACK_VOLUME_MAX 0x2000
Vikram Panduranga93f080e2017-06-07 18:16:14 -070092#define VOIP_PLAYBACK_VOLUME_MAX 0x2000
Arun Mirpuri5d170872019-03-26 13:21:31 -070093#define MMAP_PLAYBACK_VOLUME_MAX 0x2000
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +053094#define PCM_PLAYBACK_VOLUME_MAX 0x2000
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +053095#define DSD_VOLUME_MIN_DB (-110)
Aniket Kumar Lata8426d1f2019-06-10 15:25:53 -070096#define INVALID_OUT_VOLUME -1
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070097
Aalique Grahame22e49102018-12-18 14:23:57 -080098#define RECORD_GAIN_MIN 0.0f
99#define RECORD_GAIN_MAX 1.0f
100#define RECORD_VOLUME_CTL_MAX 0x2000
101
102/* treat as unsigned Q1.13 */
103#define APP_TYPE_GAIN_DEFAULT 0x2000
104
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -0700105#define PROXY_OPEN_RETRY_COUNT 100
106#define PROXY_OPEN_WAIT_TIME 20
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -0800107
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800108#define GET_USECASE_AUDIO_PLAYBACK_PRIMARY(db) \
109 (db)? USECASE_AUDIO_PLAYBACK_DEEP_BUFFER : \
110 USECASE_AUDIO_PLAYBACK_LOW_LATENCY
111#define GET_PCM_CONFIG_AUDIO_PLAYBACK_PRIMARY(db) \
112 (db)? pcm_config_deep_buffer : pcm_config_low_latency
Haynes Mathew Georgebf143712013-12-03 13:02:53 -0800113
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700114#define ULL_PERIOD_SIZE (DEFAULT_OUTPUT_SAMPLING_RATE/1000)
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700115#define DEFAULT_VOIP_BUF_DURATION_MS 20
116#define DEFAULT_VOIP_BIT_DEPTH_BYTE sizeof(int16_t)
117#define DEFAULT_VOIP_SAMP_RATE 48000
118
119#define VOIP_IO_BUF_SIZE(SR, DURATION_MS, BIT_DEPTH) (SR)/1000 * DURATION_MS * BIT_DEPTH
120
121struct pcm_config default_pcm_config_voip_copp = {
122 .channels = 1,
123 .rate = DEFAULT_VOIP_SAMP_RATE, /* changed when the stream is opened */
124 .period_size = VOIP_IO_BUF_SIZE(DEFAULT_VOIP_SAMP_RATE, DEFAULT_VOIP_BUF_DURATION_MS, DEFAULT_VOIP_BIT_DEPTH_BYTE)/2,
125 .period_count = 2,
126 .format = PCM_FORMAT_S16_LE,
kunleiz95b597a2017-10-23 17:07:33 +0800127 .avail_min = VOIP_IO_BUF_SIZE(DEFAULT_VOIP_SAMP_RATE, DEFAULT_VOIP_BUF_DURATION_MS, DEFAULT_VOIP_BIT_DEPTH_BYTE)/2,
128 .stop_threshold = INT_MAX,
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700129};
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700130
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700131#define MIN_CHANNEL_COUNT 1
132#define DEFAULT_CHANNEL_COUNT 2
133#define MAX_HIFI_CHANNEL_COUNT 8
134
Aalique Grahame22e49102018-12-18 14:23:57 -0800135#ifndef MAX_TARGET_SPECIFIC_CHANNEL_CNT
136#define MAX_CHANNEL_COUNT 1
137#else
138#define MAX_CHANNEL_COUNT atoi(XSTR(MAX_TARGET_SPECIFIC_CHANNEL_CNT))
139#define XSTR(x) STR(x)
140#define STR(x) #x
141#endif
142
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -0700143static unsigned int configured_low_latency_capture_period_size =
144 LOW_LATENCY_CAPTURE_PERIOD_SIZE;
145
Haynes Mathew George16081042017-05-31 17:16:49 -0700146#define MMAP_PERIOD_SIZE (DEFAULT_OUTPUT_SAMPLING_RATE/1000)
147#define MMAP_PERIOD_COUNT_MIN 32
148#define MMAP_PERIOD_COUNT_MAX 512
149#define MMAP_PERIOD_COUNT_DEFAULT (MMAP_PERIOD_COUNT_MAX)
150
Aalique Grahame22e49102018-12-18 14:23:57 -0800151/* This constant enables extended precision handling.
152 * TODO The flag is off until more testing is done.
153 */
154static const bool k_enable_extended_precision = false;
Arun Mirpurie008ed22019-03-21 11:21:04 -0700155extern int AUDIO_DEVICE_IN_ALL_CODEC_BACKEND;
Aalique Grahame22e49102018-12-18 14:23:57 -0800156
Eric Laurentb23d5282013-05-14 15:27:20 -0700157struct pcm_config pcm_config_deep_buffer = {
158 .channels = 2,
159 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
160 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
161 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
162 .format = PCM_FORMAT_S16_LE,
163 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
164 .stop_threshold = INT_MAX,
165 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
166};
167
168struct pcm_config pcm_config_low_latency = {
169 .channels = 2,
170 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
171 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
172 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
173 .format = PCM_FORMAT_S16_LE,
174 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
175 .stop_threshold = INT_MAX,
176 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
177};
178
Vignesh Kulothungana6927272019-02-20 15:17:07 -0800179struct pcm_config pcm_config_haptics_audio = {
180 .channels = 1,
181 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
182 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
183 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
184 .format = PCM_FORMAT_S16_LE,
185 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
186 .stop_threshold = INT_MAX,
187 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
188};
189
190struct pcm_config pcm_config_haptics = {
191 .channels = 1,
192 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
193 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
194 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
195 .format = PCM_FORMAT_S16_LE,
196 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
197 .stop_threshold = INT_MAX,
198 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
199};
200
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700201static int af_period_multiplier = 4;
202struct pcm_config pcm_config_rt = {
203 .channels = 2,
204 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
205 .period_size = ULL_PERIOD_SIZE, //1 ms
206 .period_count = 512, //=> buffer size is 512ms
207 .format = PCM_FORMAT_S16_LE,
208 .start_threshold = ULL_PERIOD_SIZE*8, //8ms
209 .stop_threshold = INT_MAX,
210 .silence_threshold = 0,
211 .silence_size = 0,
212 .avail_min = ULL_PERIOD_SIZE, //1 ms
213};
214
Eric Laurentb23d5282013-05-14 15:27:20 -0700215struct pcm_config pcm_config_hdmi_multi = {
216 .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
217 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
218 .period_size = HDMI_MULTI_PERIOD_SIZE,
219 .period_count = HDMI_MULTI_PERIOD_COUNT,
220 .format = PCM_FORMAT_S16_LE,
221 .start_threshold = 0,
222 .stop_threshold = INT_MAX,
223 .avail_min = 0,
224};
225
Haynes Mathew George16081042017-05-31 17:16:49 -0700226struct pcm_config pcm_config_mmap_playback = {
227 .channels = 2,
228 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
229 .period_size = MMAP_PERIOD_SIZE,
230 .period_count = MMAP_PERIOD_COUNT_DEFAULT,
231 .format = PCM_FORMAT_S16_LE,
232 .start_threshold = MMAP_PERIOD_SIZE*8,
233 .stop_threshold = INT32_MAX,
234 .silence_threshold = 0,
235 .silence_size = 0,
236 .avail_min = MMAP_PERIOD_SIZE, //1 ms
237};
238
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700239struct pcm_config pcm_config_hifi = {
240 .channels = DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
241 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
242 .period_size = HIFI_BUFFER_OUTPUT_PERIOD_SIZE, /* change #define */
243 .period_count = HIFI_BUFFER_OUTPUT_PERIOD_COUNT,
244 .format = PCM_FORMAT_S24_3LE,
245 .start_threshold = 0,
246 .stop_threshold = INT_MAX,
247 .avail_min = 0,
248};
249
Eric Laurentb23d5282013-05-14 15:27:20 -0700250struct pcm_config pcm_config_audio_capture = {
251 .channels = 2,
Eric Laurentb23d5282013-05-14 15:27:20 -0700252 .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
253 .format = PCM_FORMAT_S16_LE,
254};
255
Haynes Mathew George16081042017-05-31 17:16:49 -0700256struct pcm_config pcm_config_mmap_capture = {
257 .channels = 2,
258 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
259 .period_size = MMAP_PERIOD_SIZE,
260 .period_count = MMAP_PERIOD_COUNT_DEFAULT,
261 .format = PCM_FORMAT_S16_LE,
262 .start_threshold = 0,
263 .stop_threshold = INT_MAX,
264 .silence_threshold = 0,
265 .silence_size = 0,
266 .avail_min = MMAP_PERIOD_SIZE, //1 ms
267};
268
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -0700269#define AFE_PROXY_CHANNEL_COUNT 2
270#define AFE_PROXY_SAMPLING_RATE 48000
271
272#define AFE_PROXY_PLAYBACK_PERIOD_SIZE 768
273#define AFE_PROXY_PLAYBACK_PERIOD_COUNT 4
274
275struct pcm_config pcm_config_afe_proxy_playback = {
276 .channels = AFE_PROXY_CHANNEL_COUNT,
277 .rate = AFE_PROXY_SAMPLING_RATE,
278 .period_size = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
279 .period_count = AFE_PROXY_PLAYBACK_PERIOD_COUNT,
280 .format = PCM_FORMAT_S16_LE,
281 .start_threshold = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
282 .stop_threshold = INT_MAX,
283 .avail_min = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
284};
285
286#define AFE_PROXY_RECORD_PERIOD_SIZE 768
287#define AFE_PROXY_RECORD_PERIOD_COUNT 4
288
Aalique Grahame22e49102018-12-18 14:23:57 -0800289struct pcm_config pcm_config_audio_capture_rt = {
290 .channels = 2,
291 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
292 .period_size = ULL_PERIOD_SIZE,
293 .period_count = 512,
294 .format = PCM_FORMAT_S16_LE,
295 .start_threshold = 0,
296 .stop_threshold = AFE_PROXY_RECORD_PERIOD_SIZE * AFE_PROXY_RECORD_PERIOD_COUNT,
297 .silence_threshold = 0,
298 .silence_size = 0,
299 .avail_min = ULL_PERIOD_SIZE, //1 ms
300};
301
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -0700302struct pcm_config pcm_config_afe_proxy_record = {
303 .channels = AFE_PROXY_CHANNEL_COUNT,
304 .rate = AFE_PROXY_SAMPLING_RATE,
305 .period_size = AFE_PROXY_RECORD_PERIOD_SIZE,
306 .period_count = AFE_PROXY_RECORD_PERIOD_COUNT,
307 .format = PCM_FORMAT_S16_LE,
308 .start_threshold = AFE_PROXY_RECORD_PERIOD_SIZE,
309 .stop_threshold = INT_MAX,
310 .avail_min = AFE_PROXY_RECORD_PERIOD_SIZE,
311};
312
Ashish Jainf1eaa582016-05-23 20:54:24 +0530313#define AUDIO_MAX_PCM_FORMATS 7
314
315const uint32_t format_to_bitwidth_table[AUDIO_MAX_PCM_FORMATS] = {
316 [AUDIO_FORMAT_DEFAULT] = 0,
317 [AUDIO_FORMAT_PCM_16_BIT] = sizeof(uint16_t),
318 [AUDIO_FORMAT_PCM_8_BIT] = sizeof(uint8_t),
319 [AUDIO_FORMAT_PCM_32_BIT] = sizeof(uint32_t),
320 [AUDIO_FORMAT_PCM_8_24_BIT] = sizeof(uint32_t),
321 [AUDIO_FORMAT_PCM_FLOAT] = sizeof(float),
322 [AUDIO_FORMAT_PCM_24_BIT_PACKED] = sizeof(uint8_t) * 3,
323};
324
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800325const char * const use_case_table[AUDIO_USECASE_MAX] = {
Eric Laurentb23d5282013-05-14 15:27:20 -0700326 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
327 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
Vignesh Kulothungana6927272019-02-20 15:17:07 -0800328 [USECASE_AUDIO_PLAYBACK_WITH_HAPTICS] = "audio-with-haptics-playback",
Ravi Kumar Alamanda474de5a2015-06-25 20:08:01 -0700329 [USECASE_AUDIO_PLAYBACK_ULL] = "audio-ull-playback",
330 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback",
Shruthi Krishnaace10852013-10-25 14:32:12 -0700331 [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback",
vivek mehta446c3962015-09-14 10:57:35 -0700332 //Enabled for Direct_PCM
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700333 [USECASE_AUDIO_PLAYBACK_OFFLOAD2] = "compress-offload-playback2",
334 [USECASE_AUDIO_PLAYBACK_OFFLOAD3] = "compress-offload-playback3",
335 [USECASE_AUDIO_PLAYBACK_OFFLOAD4] = "compress-offload-playback4",
336 [USECASE_AUDIO_PLAYBACK_OFFLOAD5] = "compress-offload-playback5",
337 [USECASE_AUDIO_PLAYBACK_OFFLOAD6] = "compress-offload-playback6",
338 [USECASE_AUDIO_PLAYBACK_OFFLOAD7] = "compress-offload-playback7",
339 [USECASE_AUDIO_PLAYBACK_OFFLOAD8] = "compress-offload-playback8",
340 [USECASE_AUDIO_PLAYBACK_OFFLOAD9] = "compress-offload-playback9",
Haynes Mathew George16081042017-05-31 17:16:49 -0700341 [USECASE_AUDIO_PLAYBACK_FM] = "play-fm",
342 [USECASE_AUDIO_PLAYBACK_MMAP] = "mmap-playback",
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700343 [USECASE_AUDIO_PLAYBACK_HIFI] = "hifi-playback",
Aalique Grahame22e49102018-12-18 14:23:57 -0800344 [USECASE_AUDIO_PLAYBACK_TTS] = "audio-tts-playback",
vivek mehta0ea887a2015-08-26 14:01:20 -0700345
Eric Laurentb23d5282013-05-14 15:27:20 -0700346 [USECASE_AUDIO_RECORD] = "audio-record",
Mingming Yine62d7842013-10-25 16:26:03 -0700347 [USECASE_AUDIO_RECORD_COMPRESS] = "audio-record-compress",
Dhananjay Kumaree4d2002016-10-25 18:02:58 +0530348 [USECASE_AUDIO_RECORD_COMPRESS2] = "audio-record-compress2",
349 [USECASE_AUDIO_RECORD_COMPRESS3] = "audio-record-compress3",
350 [USECASE_AUDIO_RECORD_COMPRESS4] = "audio-record-compress4",
Dhananjay Kumar376e38b2017-09-28 22:26:23 +0530351 [USECASE_AUDIO_RECORD_COMPRESS5] = "audio-record-compress5",
352 [USECASE_AUDIO_RECORD_COMPRESS6] = "audio-record-compress6",
Eric Laurentb23d5282013-05-14 15:27:20 -0700353 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
Preetam Singh Ranawatde84f1a2013-11-01 14:58:16 -0700354 [USECASE_AUDIO_RECORD_FM_VIRTUAL] = "fm-virtual-record",
Haynes Mathew George16081042017-05-31 17:16:49 -0700355 [USECASE_AUDIO_RECORD_MMAP] = "mmap-record",
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700356 [USECASE_AUDIO_RECORD_HIFI] = "hifi-record",
Haynes Mathew George16081042017-05-31 17:16:49 -0700357
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -0800358 [USECASE_AUDIO_HFP_SCO] = "hfp-sco",
Vimal Puthanveed47e64852013-12-20 13:23:39 -0800359 [USECASE_AUDIO_HFP_SCO_WB] = "hfp-sco-wb",
Derek Chenf7092792017-05-23 12:23:53 -0400360 [USECASE_AUDIO_HFP_SCO_DOWNLINK] = "hfp-sco-downlink",
361 [USECASE_AUDIO_HFP_SCO_WB_DOWNLINK] = "hfp-sco-wb-downlink",
Mingming Yin3ee55c62014-08-04 14:23:35 -0700362
Derek Chenf7092792017-05-23 12:23:53 -0400363 [USECASE_VOICE_CALL] = "voice-call",
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700364 [USECASE_VOICE2_CALL] = "voice2-call",
365 [USECASE_VOLTE_CALL] = "volte-call",
366 [USECASE_QCHAT_CALL] = "qchat-call",
Vicky Sehrawat7e4fc152014-02-12 17:58:59 -0800367 [USECASE_VOWLAN_CALL] = "vowlan-call",
Vidyakumar Athota0e109352015-02-12 17:38:22 -0800368 [USECASE_VOICEMMODE1_CALL] = "voicemmode1-call",
369 [USECASE_VOICEMMODE2_CALL] = "voicemmode2-call",
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800370 [USECASE_COMPRESS_VOIP_CALL] = "compress-voip-call",
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700371 [USECASE_INCALL_REC_UPLINK] = "incall-rec-uplink",
372 [USECASE_INCALL_REC_DOWNLINK] = "incall-rec-downlink",
373 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK] = "incall-rec-uplink-and-downlink",
Helen Zenge56b4852013-12-03 16:54:40 -0800374 [USECASE_INCALL_REC_UPLINK_COMPRESS] = "incall-rec-uplink-compress",
375 [USECASE_INCALL_REC_DOWNLINK_COMPRESS] = "incall-rec-downlink-compress",
376 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK_COMPRESS] = "incall-rec-uplink-and-downlink-compress",
377
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -0700378 [USECASE_INCALL_MUSIC_UPLINK] = "incall_music_uplink",
379 [USECASE_INCALL_MUSIC_UPLINK2] = "incall_music_uplink2",
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700380 [USECASE_AUDIO_SPKR_CALIB_RX] = "spkr-rx-calib",
381 [USECASE_AUDIO_SPKR_CALIB_TX] = "spkr-vi-record",
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -0700382
383 [USECASE_AUDIO_PLAYBACK_AFE_PROXY] = "afe-proxy-playback",
384 [USECASE_AUDIO_RECORD_AFE_PROXY] = "afe-proxy-record",
Md Mansoor Ahmeddb1b4f92018-01-25 18:56:31 +0530385 [USECASE_AUDIO_PLAYBACK_SILENCE] = "silence-playback",
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700386
Siddartha Shaik31b530e2017-05-19 15:26:33 +0530387 /* Transcode loopback cases */
Surendar Karka93cd25a2018-08-28 14:21:37 +0530388 [USECASE_AUDIO_TRANSCODE_LOOPBACK_RX] = "audio-transcode-loopback-rx",
389 [USECASE_AUDIO_TRANSCODE_LOOPBACK_TX] = "audio-transcode-loopback-tx",
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700390
391 [USECASE_AUDIO_PLAYBACK_VOIP] = "audio-playback-voip",
392 [USECASE_AUDIO_RECORD_VOIP] = "audio-record-voip",
Varun Balaraje49253e2017-07-06 19:48:56 +0530393 /* For Interactive Audio Streams */
394 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM1] = "audio-interactive-stream1",
395 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM2] = "audio-interactive-stream2",
396 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM3] = "audio-interactive-stream3",
397 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM4] = "audio-interactive-stream4",
398 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM5] = "audio-interactive-stream5",
399 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM6] = "audio-interactive-stream6",
400 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM7] = "audio-interactive-stream7",
401 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM8] = "audio-interactive-stream8",
Garmond Leunge2433c32017-09-28 21:51:22 -0700402
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800403 [USECASE_AUDIO_EC_REF_LOOPBACK] = "ec-ref-audio-capture",
404
Derek Chenf6318be2017-06-12 17:16:24 -0400405 [USECASE_AUDIO_A2DP_ABR_FEEDBACK] = "a2dp-abr-feedback",
406
407 [USECASE_AUDIO_PLAYBACK_MEDIA] = "media-playback",
408 [USECASE_AUDIO_PLAYBACK_SYS_NOTIFICATION] = "sys-notification-playback",
409 [USECASE_AUDIO_PLAYBACK_NAV_GUIDANCE] = "nav-guidance-playback",
410 [USECASE_AUDIO_PLAYBACK_PHONE] = "phone-playback",
Derek Chendf05eea2019-08-01 13:57:49 -0700411 [USECASE_AUDIO_PLAYBACK_REAR_SEAT] = "rear-seat-playback",
Rahul Sharma99770982019-03-06 17:05:26 +0530412 [USECASE_AUDIO_FM_TUNER_EXT] = "fm-tuner-ext",
Eric Laurentb23d5282013-05-14 15:27:20 -0700413};
414
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700415static const audio_usecase_t offload_usecases[] = {
416 USECASE_AUDIO_PLAYBACK_OFFLOAD,
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700417 USECASE_AUDIO_PLAYBACK_OFFLOAD2,
418 USECASE_AUDIO_PLAYBACK_OFFLOAD3,
419 USECASE_AUDIO_PLAYBACK_OFFLOAD4,
420 USECASE_AUDIO_PLAYBACK_OFFLOAD5,
421 USECASE_AUDIO_PLAYBACK_OFFLOAD6,
422 USECASE_AUDIO_PLAYBACK_OFFLOAD7,
423 USECASE_AUDIO_PLAYBACK_OFFLOAD8,
424 USECASE_AUDIO_PLAYBACK_OFFLOAD9,
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700425};
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800426
Varun Balaraje49253e2017-07-06 19:48:56 +0530427static const audio_usecase_t interactive_usecases[] = {
428 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM1,
429 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM2,
430 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM3,
431 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM4,
432 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM5,
433 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM6,
434 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM7,
435 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM8,
436};
437
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800438#define STRING_TO_ENUM(string) { #string, string }
439
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800440struct string_to_enum {
441 const char *name;
442 uint32_t value;
443};
444
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700445static const struct string_to_enum channels_name_to_enum_table[] = {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800446 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
Mingming Yin3a941d42016-02-17 18:08:05 -0800447 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_2POINT1),
448 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_QUAD),
449 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_SURROUND),
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -0700450 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_PENTA),
Mingming Yin3a941d42016-02-17 18:08:05 -0800451 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
452 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_6POINT1),
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800453 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700454 STRING_TO_ENUM(AUDIO_CHANNEL_IN_MONO),
455 STRING_TO_ENUM(AUDIO_CHANNEL_IN_STEREO),
456 STRING_TO_ENUM(AUDIO_CHANNEL_IN_FRONT_BACK),
457 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_1),
458 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_2),
459 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_3),
460 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_4),
461 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_5),
462 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_6),
463 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_7),
464 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_8),
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800465};
466
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700467static const struct string_to_enum formats_name_to_enum_table[] = {
468 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
469 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
470 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Pradnya Chaphekar8a9dcd82014-09-09 09:49:10 -0700471 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
472 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
473 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Ben Romberger1aaaf862017-04-06 17:49:46 -0700474 STRING_TO_ENUM(AUDIO_FORMAT_DOLBY_TRUEHD),
Mingming Yin3a941d42016-02-17 18:08:05 -0800475 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
476 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
Naresh Tanniru928f0862017-04-07 16:44:23 -0700477 STRING_TO_ENUM(AUDIO_FORMAT_IEC61937)
Mingming Yin3a941d42016-02-17 18:08:05 -0800478};
479
480//list of all supported sample rates by HDMI specification.
481static const int out_hdmi_sample_rates[] = {
482 32000, 44100, 48000, 88200, 96000, 176400, 192000,
483};
484
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700485static const struct string_to_enum out_sample_rates_name_to_enum_table[] = {
Mingming Yin3a941d42016-02-17 18:08:05 -0800486 STRING_TO_ENUM(32000),
487 STRING_TO_ENUM(44100),
488 STRING_TO_ENUM(48000),
489 STRING_TO_ENUM(88200),
490 STRING_TO_ENUM(96000),
491 STRING_TO_ENUM(176400),
492 STRING_TO_ENUM(192000),
Pradnya Chaphekar8a9dcd82014-09-09 09:49:10 -0700493};
494
Carter Hsu2e429db2019-05-14 18:50:52 +0800495struct in_effect_list {
496 struct listnode list;
497 effect_handle_t handle;
498};
499
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700500static struct audio_device *adev = NULL;
Ben Romberger02ab1192018-05-24 12:10:08 -0700501static pthread_mutex_t adev_init_lock = PTHREAD_MUTEX_INITIALIZER;
Kiran Kandi910e1862013-10-29 13:29:42 -0700502static unsigned int audio_device_ref_count;
vivek mehtab72d08d2016-04-29 03:16:47 -0700503//cache last MBDRC cal step level
504static int last_known_cal_step = -1 ;
Kiran Kandi910e1862013-10-29 13:29:42 -0700505
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +0530506static int check_a2dp_restore_l(struct audio_device *adev, struct stream_out *out, bool restore);
507static int out_set_compr_volume(struct audio_stream_out *stream, float left, float right);
Arun Mirpuri5d170872019-03-26 13:21:31 -0700508static int out_set_mmap_volume(struct audio_stream_out *stream, float left, float right);
Zhou Song2b8f28f2017-09-11 10:51:38 +0800509static int out_set_voip_volume(struct audio_stream_out *stream, float left, float right);
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +0530510static int out_set_pcm_volume(struct audio_stream_out *stream, float left, float right);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +0530511
Derek Chen6f293672019-04-01 01:40:24 -0700512static void adev_snd_mon_cb(void *cookie, struct str_parms *parms);
513static void in_snd_mon_cb(void * stream, struct str_parms * parms);
514static void out_snd_mon_cb(void * stream, struct str_parms * parms);
515
Zhou Song331c8e52019-08-26 14:16:12 +0800516static int configure_btsco_sample_rate(snd_device_t snd_device);
517
Vatsal Buchac09ae062018-11-14 13:25:08 +0530518#ifdef AUDIO_FEATURE_ENABLED_GCOV
519extern void __gcov_flush();
520static void enable_gcov()
521{
522 __gcov_flush();
523}
524#else
525static void enable_gcov()
526{
527}
528#endif
529
justinweng20fb6d82019-02-21 18:49:00 -0700530static int in_set_microphone_direction(const struct audio_stream_in *stream,
531 audio_microphone_direction_t dir);
532static int in_set_microphone_field_dimension(const struct audio_stream_in *stream, float zoom);
533
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700534static bool may_use_noirq_mode(struct audio_device *adev, audio_usecase_t uc_id,
535 int flags __unused)
536{
537 int dir = 0;
538 switch (uc_id) {
539 case USECASE_AUDIO_RECORD_LOW_LATENCY:
540 dir = 1;
541 case USECASE_AUDIO_PLAYBACK_ULL:
542 break;
543 default:
544 return false;
545 }
546
547 int dev_id = platform_get_pcm_device_id(uc_id, dir == 0 ?
548 PCM_PLAYBACK : PCM_CAPTURE);
549 if (adev->adm_is_noirq_avail)
550 return adev->adm_is_noirq_avail(adev->adm_data,
551 adev->snd_card, dev_id, dir);
552 return false;
553}
554
555static void register_out_stream(struct stream_out *out)
556{
557 struct audio_device *adev = out->dev;
558 if (is_offload_usecase(out->usecase) ||
559 !adev->adm_register_output_stream)
560 return;
561
562 // register stream first for backward compatibility
563 adev->adm_register_output_stream(adev->adm_data,
564 out->handle,
565 out->flags);
566
567 if (!adev->adm_set_config)
568 return;
569
570 if (out->realtime)
571 adev->adm_set_config(adev->adm_data,
572 out->handle,
573 out->pcm, &out->config);
574}
575
576static void register_in_stream(struct stream_in *in)
577{
578 struct audio_device *adev = in->dev;
579 if (!adev->adm_register_input_stream)
580 return;
581
582 adev->adm_register_input_stream(adev->adm_data,
583 in->capture_handle,
584 in->flags);
585
586 if (!adev->adm_set_config)
587 return;
588
589 if (in->realtime)
590 adev->adm_set_config(adev->adm_data,
591 in->capture_handle,
592 in->pcm,
593 &in->config);
594}
595
596static void request_out_focus(struct stream_out *out, long ns)
597{
598 struct audio_device *adev = out->dev;
599
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700600 if (adev->adm_request_focus_v2)
601 adev->adm_request_focus_v2(adev->adm_data, out->handle, ns);
602 else if (adev->adm_request_focus)
603 adev->adm_request_focus(adev->adm_data, out->handle);
604}
605
Aniket Kumar Lata60586a92019-05-22 22:18:55 -0700606static int request_in_focus(struct stream_in *in, long ns)
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700607{
608 struct audio_device *adev = in->dev;
Aniket Kumar Lata60586a92019-05-22 22:18:55 -0700609 int ret = 0;
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700610
Aniket Kumar Lata60586a92019-05-22 22:18:55 -0700611 if (adev->adm_request_focus_v2_1)
612 ret = adev->adm_request_focus_v2_1(adev->adm_data, in->capture_handle, ns);
613 else if (adev->adm_request_focus_v2)
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700614 adev->adm_request_focus_v2(adev->adm_data, in->capture_handle, ns);
615 else if (adev->adm_request_focus)
616 adev->adm_request_focus(adev->adm_data, in->capture_handle);
Aniket Kumar Lata60586a92019-05-22 22:18:55 -0700617
618 return ret;
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700619}
620
621static void release_out_focus(struct stream_out *out)
622{
623 struct audio_device *adev = out->dev;
624
625 if (adev->adm_abandon_focus)
626 adev->adm_abandon_focus(adev->adm_data, out->handle);
627}
628
629static void release_in_focus(struct stream_in *in)
630{
631 struct audio_device *adev = in->dev;
632 if (adev->adm_abandon_focus)
633 adev->adm_abandon_focus(adev->adm_data, in->capture_handle);
634}
635
Dhananjay Kumare6293dd2017-05-25 17:25:30 +0530636static int parse_snd_card_status(struct str_parms *parms, int *card,
637 card_status_t *status)
638{
639 char value[32]={0};
640 char state[32]={0};
641
642 int ret = str_parms_get_str(parms, "SND_CARD_STATUS", value, sizeof(value));
643 if (ret < 0)
644 return -1;
645
646 // sscanf should be okay as value is of max length 32.
647 // same as sizeof state.
648 if (sscanf(value, "%d,%s", card, state) < 2)
649 return -1;
650
651 *status = !strcmp(state, "ONLINE") ? CARD_STATUS_ONLINE :
652 CARD_STATUS_OFFLINE;
653 return 0;
654}
655
Haynes Mathew Georgeb0f5dc32017-10-06 18:35:12 -0700656static inline void adjust_frames_for_device_delay(struct stream_out *out,
657 uint32_t *dsp_frames) {
658 // Adjustment accounts for A2dp encoder latency with offload usecases
659 // Note: Encoder latency is returned in ms.
660 if (AUDIO_DEVICE_OUT_ALL_A2DP & out->devices) {
661 unsigned long offset =
662 (audio_extn_a2dp_get_encoder_latency() * out->sample_rate / 1000);
663 *dsp_frames = (*dsp_frames > offset) ? (*dsp_frames - offset) : 0;
664 }
665}
666
vivek mehtaa76401a2015-04-24 14:12:15 -0700667__attribute__ ((visibility ("default")))
668bool audio_hw_send_gain_dep_calibration(int level) {
669 bool ret_val = false;
vivek mehtab72d08d2016-04-29 03:16:47 -0700670 ALOGV("%s: called ...", __func__);
vivek mehtaa76401a2015-04-24 14:12:15 -0700671
672 pthread_mutex_lock(&adev_init_lock);
673
674 if (adev != NULL && adev->platform != NULL) {
675 pthread_mutex_lock(&adev->lock);
676 ret_val = platform_send_gain_dep_cal(adev->platform, level);
vivek mehtab72d08d2016-04-29 03:16:47 -0700677
Preetam Singh Ranawatf4ae0222017-05-31 17:07:28 +0530678 // cache level info for any of the use case which
679 // was not started.
680 last_known_cal_step = level;;
vivek mehtab72d08d2016-04-29 03:16:47 -0700681
vivek mehtaa76401a2015-04-24 14:12:15 -0700682 pthread_mutex_unlock(&adev->lock);
683 } else {
684 ALOGE("%s: %s is NULL", __func__, adev == NULL ? "adev" : "adev->platform");
685 }
686
687 pthread_mutex_unlock(&adev_init_lock);
688
689 return ret_val;
690}
691
Ashish Jain5106d362016-05-11 19:23:33 +0530692static int check_and_set_gapless_mode(struct audio_device *adev, bool enable_gapless)
693{
Krishnankutty Kolathappilly6d8788b2014-01-09 12:45:31 -0800694 bool gapless_enabled = false;
695 const char *mixer_ctl_name = "Compress Gapless Playback";
696 struct mixer_ctl *ctl;
697
698 ALOGV("%s:", __func__);
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700699 gapless_enabled = property_get_bool("vendor.audio.offload.gapless.enabled", false);
Ashish Jain5106d362016-05-11 19:23:33 +0530700
701 /*Disable gapless if its AV playback*/
702 gapless_enabled = gapless_enabled && enable_gapless;
Krishnankutty Kolathappilly6d8788b2014-01-09 12:45:31 -0800703
704 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
705 if (!ctl) {
706 ALOGE("%s: Could not get ctl for mixer cmd - %s",
707 __func__, mixer_ctl_name);
708 return -EINVAL;
709 }
710
711 if (mixer_ctl_set_value(ctl, 0, gapless_enabled) < 0) {
712 ALOGE("%s: Could not set gapless mode %d",
713 __func__, gapless_enabled);
714 return -EINVAL;
715 }
716 return 0;
717}
Haynes Mathew George5191a852013-09-11 14:19:36 -0700718
Aniket Kumar Lataf56b6402016-10-27 12:03:18 -0700719__attribute__ ((visibility ("default")))
720int audio_hw_get_gain_level_mapping(struct amp_db_and_gain_table *mapping_tbl,
721 int table_size) {
722 int ret_val = 0;
723 ALOGV("%s: enter ... ", __func__);
724
725 pthread_mutex_lock(&adev_init_lock);
726 if (adev == NULL) {
727 ALOGW("%s: adev is NULL .... ", __func__);
728 goto done;
729 }
730
731 pthread_mutex_lock(&adev->lock);
732 ret_val = platform_get_gain_level_mapping(mapping_tbl, table_size);
733 pthread_mutex_unlock(&adev->lock);
734done:
735 pthread_mutex_unlock(&adev_init_lock);
736 ALOGV("%s: exit ... ", __func__);
737 return ret_val;
738}
739
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800740bool audio_hw_send_qdsp_parameter(int stream_type, float vol, bool active)
Aalique Grahame22e49102018-12-18 14:23:57 -0800741{
742 bool ret = false;
743 ALOGV("%s: enter ...", __func__);
744
745 pthread_mutex_lock(&adev_init_lock);
746
747 if (adev != NULL && adev->platform != NULL) {
748 pthread_mutex_lock(&adev->lock);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800749 ret = audio_extn_qdsp_set_state(adev, stream_type, vol, active);
Aalique Grahame22e49102018-12-18 14:23:57 -0800750 pthread_mutex_unlock(&adev->lock);
751 }
752
753 pthread_mutex_unlock(&adev_init_lock);
754
755 ALOGV("%s: exit with ret %d", __func__, ret);
756 return ret;
757}
Aalique Grahame22e49102018-12-18 14:23:57 -0800758
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700759static bool is_supported_format(audio_format_t format)
760{
Eric Laurent86e17132013-09-12 17:49:30 -0700761 if (format == AUDIO_FORMAT_MP3 ||
Satish Babu Patakokila0c313922016-12-08 12:07:08 +0530762 format == AUDIO_FORMAT_MP2 ||
Ashish Jainf9b78162014-08-25 20:36:25 +0530763 format == AUDIO_FORMAT_AAC_LC ||
764 format == AUDIO_FORMAT_AAC_HE_V1 ||
765 format == AUDIO_FORMAT_AAC_HE_V2 ||
Manish Dewangana6fc5442015-08-24 20:30:31 +0530766 format == AUDIO_FORMAT_AAC_ADTS_LC ||
767 format == AUDIO_FORMAT_AAC_ADTS_HE_V1 ||
768 format == AUDIO_FORMAT_AAC_ADTS_HE_V2 ||
Arun Kumar Dasari3b174182016-12-27 13:01:14 +0530769 format == AUDIO_FORMAT_AAC_LATM_LC ||
770 format == AUDIO_FORMAT_AAC_LATM_HE_V1 ||
771 format == AUDIO_FORMAT_AAC_LATM_HE_V2 ||
Ashish Jain5106d362016-05-11 19:23:33 +0530772 format == AUDIO_FORMAT_PCM_24_BIT_PACKED ||
773 format == AUDIO_FORMAT_PCM_8_24_BIT ||
Ashish Jainf1eaa582016-05-23 20:54:24 +0530774 format == AUDIO_FORMAT_PCM_FLOAT ||
775 format == AUDIO_FORMAT_PCM_32_BIT ||
vivek mehta0ea887a2015-08-26 14:01:20 -0700776 format == AUDIO_FORMAT_PCM_16_BIT ||
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530777 format == AUDIO_FORMAT_AC3 ||
778 format == AUDIO_FORMAT_E_AC3 ||
Ben Romberger1aaaf862017-04-06 17:49:46 -0700779 format == AUDIO_FORMAT_DOLBY_TRUEHD ||
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530780 format == AUDIO_FORMAT_DTS ||
781 format == AUDIO_FORMAT_DTS_HD ||
Weiyin Jiang18ac4e92015-03-15 15:03:40 +0800782 format == AUDIO_FORMAT_FLAC ||
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530783 format == AUDIO_FORMAT_ALAC ||
784 format == AUDIO_FORMAT_APE ||
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530785 format == AUDIO_FORMAT_DSD ||
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530786 format == AUDIO_FORMAT_VORBIS ||
Weiyin Jiang18ac4e92015-03-15 15:03:40 +0800787 format == AUDIO_FORMAT_WMA ||
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +0530788 format == AUDIO_FORMAT_WMA_PRO ||
Naresh Tanniru928f0862017-04-07 16:44:23 -0700789 format == AUDIO_FORMAT_APTX ||
790 format == AUDIO_FORMAT_IEC61937)
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -0800791 return true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700792
793 return false;
794}
795
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700796static inline bool is_mmap_usecase(audio_usecase_t uc_id)
797{
798 return (uc_id == USECASE_AUDIO_RECORD_AFE_PROXY) ||
799 (uc_id == USECASE_AUDIO_PLAYBACK_AFE_PROXY);
800}
801
Aniket Kumar Lata8426d1f2019-06-10 15:25:53 -0700802static inline bool is_valid_volume(float left, float right)
803{
804 return ((left >= 0.0f && right >= 0.0f) ? true : false);
805}
806
Avinash Vaish71a8b972014-07-24 15:36:33 +0530807static int enable_audio_route_for_voice_usecases(struct audio_device *adev,
808 struct audio_usecase *uc_info)
809{
810 struct listnode *node;
811 struct audio_usecase *usecase;
812
813 if (uc_info == NULL)
814 return -EINVAL;
815
816 /* Re-route all voice usecases on the shared backend other than the
817 specified usecase to new snd devices */
818 list_for_each(node, &adev->usecase_list) {
819 usecase = node_to_item(node, struct audio_usecase, list);
Narsinga Rao Chellae7f33c12015-01-28 15:23:34 -0800820 if ((usecase->type == VOICE_CALL) && (usecase != uc_info))
Avinash Vaish71a8b972014-07-24 15:36:33 +0530821 enable_audio_route(adev, usecase);
822 }
823 return 0;
824}
825
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +0530826static void enable_asrc_mode(struct audio_device *adev)
Preetam Singh Ranawatb0c0dd72016-08-18 00:32:06 +0530827{
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +0530828 ALOGV("%s", __func__);
829 audio_route_apply_and_update_path(adev->audio_route,
830 "asrc-mode");
831 adev->asrc_mode_enabled = true;
832}
833
834static void disable_asrc_mode(struct audio_device *adev)
835{
836 ALOGV("%s", __func__);
837 audio_route_reset_and_update_path(adev->audio_route,
838 "asrc-mode");
839 adev->asrc_mode_enabled = false;
840}
841
842/*
843 * - Enable ASRC mode for incoming mix path use case(Headphone backend)if Headphone
844 * 44.1 or Native DSD backends are enabled for any of current use case.
845 * e.g. 48-> + (Naitve DSD or Headphone 44.1)
846 * - Disable current mix path use case(Headphone backend) and re-enable it with
847 * ASRC mode for incoming Headphone 44.1 or Native DSD use case.
848 * e.g. Naitve DSD or Headphone 44.1 -> + 48
849 */
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +0530850static void check_and_set_asrc_mode(struct audio_device *adev,
851 struct audio_usecase *uc_info,
852 snd_device_t snd_device)
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +0530853{
854 ALOGV("%s snd device %d", __func__, snd_device);
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +0530855 int i, num_new_devices = 0;
856 snd_device_t split_new_snd_devices[SND_DEVICE_OUT_END];
857 /*
858 *Split snd device for new combo use case
859 *e.g. Headphopne 44.1-> + Ringtone (Headphone + Speaker)
860 */
861 if (platform_split_snd_device(adev->platform,
862 snd_device,
863 &num_new_devices,
864 split_new_snd_devices) == 0) {
865 for (i = 0; i < num_new_devices; i++)
866 check_and_set_asrc_mode(adev, uc_info, split_new_snd_devices[i]);
867 } else {
868 int new_backend_idx = platform_get_backend_index(snd_device);
869 if (((new_backend_idx == HEADPHONE_BACKEND) ||
870 (new_backend_idx == HEADPHONE_44_1_BACKEND) ||
871 (new_backend_idx == DSD_NATIVE_BACKEND)) &&
872 !adev->asrc_mode_enabled) {
873 struct listnode *node = NULL;
874 struct audio_usecase *uc = NULL;
875 struct stream_out *curr_out = NULL;
876 int usecase_backend_idx = DEFAULT_CODEC_BACKEND;
877 int i, num_devices, ret = 0;
878 snd_device_t split_snd_devices[SND_DEVICE_OUT_END];
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +0530879
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +0530880 list_for_each(node, &adev->usecase_list) {
881 uc = node_to_item(node, struct audio_usecase, list);
882 curr_out = (struct stream_out*) uc->stream.out;
883 if (curr_out && PCM_PLAYBACK == uc->type && uc != uc_info) {
884 /*
885 *Split snd device for existing combo use case
886 *e.g. Ringtone (Headphone + Speaker) + Headphopne 44.1
887 */
888 ret = platform_split_snd_device(adev->platform,
889 uc->out_snd_device,
890 &num_devices,
891 split_snd_devices);
892 if (ret < 0 || num_devices == 0) {
893 ALOGV("%s: Unable to split uc->out_snd_device: %d",__func__, uc->out_snd_device);
894 split_snd_devices[0] = uc->out_snd_device;
895 num_devices = 1;
Garmond Leung50058f62017-02-08 09:49:30 -0800896 }
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +0530897 for (i = 0; i < num_devices; i++) {
898 usecase_backend_idx = platform_get_backend_index(split_snd_devices[i]);
899 ALOGD("%s:snd_dev %d usecase_backend_idx %d",__func__, split_snd_devices[i],usecase_backend_idx);
900 if((new_backend_idx == HEADPHONE_BACKEND) &&
901 ((usecase_backend_idx == HEADPHONE_44_1_BACKEND) ||
902 (usecase_backend_idx == DSD_NATIVE_BACKEND))) {
903 ALOGD("%s:DSD or native stream detected enabling asrcmode in hardware",
904 __func__);
905 enable_asrc_mode(adev);
906 break;
907 } else if(((new_backend_idx == HEADPHONE_44_1_BACKEND) ||
908 (new_backend_idx == DSD_NATIVE_BACKEND)) &&
909 (usecase_backend_idx == HEADPHONE_BACKEND)) {
910 ALOGD("%s:48K stream detected, disabling and enabling it with asrcmode in hardware",
911 __func__);
912 disable_audio_route(adev, uc);
913 disable_snd_device(adev, uc->out_snd_device);
914 // Apply true-high-quality-mode if DSD or > 44.1KHz or >=24-bit
915 if (new_backend_idx == DSD_NATIVE_BACKEND)
916 audio_route_apply_and_update_path(adev->audio_route,
917 "hph-true-highquality-mode");
918 else if ((new_backend_idx == HEADPHONE_44_1_BACKEND) &&
919 (curr_out->bit_width >= 24))
920 audio_route_apply_and_update_path(adev->audio_route,
921 "hph-highquality-mode");
922 enable_asrc_mode(adev);
923 enable_snd_device(adev, uc->out_snd_device);
924 enable_audio_route(adev, uc);
925 break;
926 }
927 }
928 // reset split devices count
929 num_devices = 0;
Garmond Leung50058f62017-02-08 09:49:30 -0800930 }
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +0530931 if (adev->asrc_mode_enabled)
932 break;
Preetam Singh Ranawatb0c0dd72016-08-18 00:32:06 +0530933 }
934 }
935 }
936}
937
Vikram Pandurangadf59cae2017-08-03 18:04:55 -0700938static int send_effect_enable_disable_mixer_ctl(struct audio_device *adev,
939 struct audio_effect_config effect_config,
940 unsigned int param_value)
941{
942 char mixer_ctl_name[] = "Audio Effect";
943 struct mixer_ctl *ctl;
944 long set_values[6];
Eric Laurent637e2d42018-11-15 12:24:31 -0800945 struct stream_in *in = adev_get_active_input(adev);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -0700946
Aniket Kumar Lata7de63522019-06-13 11:05:18 -0700947 if (in == NULL) {
948 ALOGE("%s: active input stream is NULL", __func__);
949 return -EINVAL;
950 }
951
Vikram Pandurangadf59cae2017-08-03 18:04:55 -0700952 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
953 if (!ctl) {
954 ALOGE("%s: Could not get mixer ctl - %s",
955 __func__, mixer_ctl_name);
956 return -EINVAL;
957 }
958
959 set_values[0] = 1; //0:Rx 1:Tx
960 set_values[1] = in->app_type_cfg.app_type;
961 set_values[2] = (long)effect_config.module_id;
962 set_values[3] = (long)effect_config.instance_id;
963 set_values[4] = (long)effect_config.param_id;
964 set_values[5] = param_value;
965
966 mixer_ctl_set_array(ctl, set_values, ARRAY_SIZE(set_values));
967
968 return 0;
969
970}
971
972static int update_effect_param_ecns(struct audio_device *adev, unsigned int module_id,
973 int effect_type, unsigned int *param_value)
974{
975 int ret = 0;
976 struct audio_effect_config other_effect_config;
977 struct audio_usecase *usecase = NULL;
Eric Laurent637e2d42018-11-15 12:24:31 -0800978 struct stream_in *in = adev_get_active_input(adev);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -0700979
Aniket Kumar Lata7de63522019-06-13 11:05:18 -0700980 if (in == NULL) {
981 ALOGE("%s: active input stream is NULL", __func__);
982 return -EINVAL;
983 }
984
Vikram Pandurangadf59cae2017-08-03 18:04:55 -0700985 usecase = get_usecase_from_list(adev, in->usecase);
986 if (!usecase)
987 return -EINVAL;
988
989 ret = platform_get_effect_config_data(usecase->in_snd_device, &other_effect_config,
990 effect_type == EFFECT_AEC ? EFFECT_NS : EFFECT_AEC);
991 if (ret < 0) {
992 ALOGE("%s Failed to get effect params %d", __func__, ret);
993 return ret;
994 }
995
996 if (module_id == other_effect_config.module_id) {
997 //Same module id for AEC/NS. Values need to be combined
998 if (((effect_type == EFFECT_AEC) && (in->enable_ns)) ||
999 ((effect_type == EFFECT_NS) && (in->enable_aec))) {
1000 *param_value |= other_effect_config.param_value;
1001 }
1002 }
1003
1004 return ret;
1005}
1006
1007static int enable_disable_effect(struct audio_device *adev, int effect_type, bool enable)
Gangadhar Sb0210342019-02-22 17:39:41 +05301008{
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001009 struct audio_effect_config effect_config;
1010 struct audio_usecase *usecase = NULL;
1011 int ret = 0;
1012 unsigned int param_value = 0;
Eric Laurent637e2d42018-11-15 12:24:31 -08001013 struct stream_in *in = adev_get_active_input(adev);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001014
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001015 if(!voice_extn_is_dynamic_ecns_enabled())
1016 return ENOSYS;
1017
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001018 if (!in) {
1019 ALOGE("%s: Invalid input stream", __func__);
1020 return -EINVAL;
1021 }
1022
1023 ALOGD("%s: effect_type:%d enable:%d", __func__, effect_type, enable);
1024
1025 usecase = get_usecase_from_list(adev, in->usecase);
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001026 if (usecase == NULL) {
1027 ALOGE("%s: Could not find the usecase (%d) in the list",
1028 __func__, in->usecase);
1029 return -EINVAL;
1030 }
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001031
1032 ret = platform_get_effect_config_data(usecase->in_snd_device, &effect_config, effect_type);
1033 if (ret < 0) {
1034 ALOGE("%s Failed to get module id %d", __func__, ret);
1035 return ret;
1036 }
1037 ALOGV("%s: %d %d usecase->id:%d usecase->in_snd_device:%d", __func__, effect_config.module_id,
1038 in->app_type_cfg.app_type, usecase->id, usecase->in_snd_device);
1039
1040 if(enable)
1041 param_value = effect_config.param_value;
1042
1043 /*Special handling for AEC & NS effects Param values need to be
1044 updated if module ids are same*/
1045
1046 if ((effect_type == EFFECT_AEC) || (effect_type == EFFECT_NS)) {
1047 ret = update_effect_param_ecns(adev, effect_config.module_id, effect_type, &param_value);
1048 if (ret < 0)
1049 return ret;
1050 }
1051
1052 ret = send_effect_enable_disable_mixer_ctl(adev, effect_config, param_value);
1053
1054 return ret;
1055}
1056
1057static void check_and_enable_effect(struct audio_device *adev)
1058{
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001059 if(!voice_extn_is_dynamic_ecns_enabled())
1060 return;
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001061
Eric Laurent637e2d42018-11-15 12:24:31 -08001062 struct stream_in *in = adev_get_active_input(adev);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001063
Eric Laurent637e2d42018-11-15 12:24:31 -08001064 if (in != NULL && !in->standby) {
1065 if (in->enable_aec)
1066 enable_disable_effect(adev, EFFECT_AEC, true);
1067
1068 if (in->enable_ns &&
1069 in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
1070 enable_disable_effect(adev, EFFECT_NS, true);
1071 }
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001072 }
1073}
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001074
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07001075int pcm_ioctl(struct pcm *pcm, int request, ...)
1076{
1077 va_list ap;
1078 void * arg;
1079 int pcm_fd = *(int*)pcm;
1080
1081 va_start(ap, request);
1082 arg = va_arg(ap, void *);
1083 va_end(ap);
1084
1085 return ioctl(pcm_fd, request, arg);
1086}
1087
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -07001088int enable_audio_route(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001089 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001090{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001091 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001092 char mixer_path[MIXER_PATH_MAX_LENGTH];
Manish Dewangan58229382017-02-02 15:48:41 +05301093 struct stream_out *out = NULL;
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05301094 struct stream_in *in = NULL;
Soumya Managoli6993b762018-06-28 16:04:57 +05301095 int ret = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001096
1097 if (usecase == NULL)
1098 return -EINVAL;
1099
1100 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
1101
Carter Hsu2e429db2019-05-14 18:50:52 +08001102 if (usecase->type == PCM_CAPTURE) {
1103 struct stream_in *in = usecase->stream.in;
1104 struct audio_usecase *uinfo;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001105 snd_device = usecase->in_snd_device;
Carter Hsu2e429db2019-05-14 18:50:52 +08001106
1107 if (in) {
1108 if (in->enable_aec || in->enable_ec_port) {
1109 audio_devices_t out_device = AUDIO_DEVICE_OUT_SPEAKER;
1110 struct listnode *node;
1111 struct audio_usecase *voip_usecase = get_usecase_from_list(adev,
1112 USECASE_AUDIO_PLAYBACK_VOIP);
1113 if (voip_usecase) {
1114 out_device = voip_usecase->stream.out->devices;
1115 } else if (adev->primary_output &&
1116 !adev->primary_output->standby) {
1117 out_device = adev->primary_output->devices;
1118 } else {
1119 list_for_each(node, &adev->usecase_list) {
1120 uinfo = node_to_item(node, struct audio_usecase, list);
1121 if (uinfo->type != PCM_CAPTURE) {
1122 out_device = uinfo->stream.out->devices;
1123 break;
1124 }
1125 }
1126 }
1127 platform_set_echo_reference(adev, true, out_device);
1128 in->ec_opened = true;
1129 }
1130 }
Guodong Huf5e614d2019-06-24 18:42:03 +08001131 } else if ((usecase->type == TRANSCODE_LOOPBACK_TX) || ((usecase->type == PCM_HFP_CALL) &&
1132 ((usecase->id == USECASE_AUDIO_HFP_SCO) || (usecase->id == USECASE_AUDIO_HFP_SCO_WB)) &&
1133 (usecase->in_snd_device == SND_DEVICE_IN_VOICE_SPEAKER_MIC_HFP_MMSECNS))) {
Carter Hsu2e429db2019-05-14 18:50:52 +08001134 snd_device = usecase->in_snd_device;
1135 } else {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001136 snd_device = usecase->out_snd_device;
Carter Hsu2e429db2019-05-14 18:50:52 +08001137 }
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001138
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08001139#ifdef DS1_DOLBY_DAP_ENABLED
1140 audio_extn_dolby_set_dmid(adev);
1141 audio_extn_dolby_set_endpoint(adev);
1142#endif
Pradnya Chaphekar8a9dcd82014-09-09 09:49:10 -07001143 audio_extn_dolby_ds2_set_endpoint(adev);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001144 audio_extn_sound_trigger_update_stream_status(usecase, ST_EVENT_STREAM_BUSY);
Dhananjay Kumar45b71742014-05-29 21:47:27 +05301145 audio_extn_listen_update_stream_status(usecase, LISTEN_EVENT_STREAM_BUSY);
Ben Romberger1fafdde2015-09-09 19:43:15 -07001146 audio_extn_utils_send_app_type_cfg(adev, usecase);
Jasmine Cha4dcc1092019-03-04 18:12:47 +08001147 if (audio_extn_is_maxx_audio_enabled())
1148 audio_extn_ma_set_device(usecase);
Dhananjay Kumar14170dd2015-08-28 13:24:16 +05301149 audio_extn_utils_send_audio_calibration(adev, usecase);
Manish Dewangan58229382017-02-02 15:48:41 +05301150 if ((usecase->type == PCM_PLAYBACK) && is_offload_usecase(usecase->id)) {
1151 out = usecase->stream.out;
1152 if (out && out->compr)
1153 audio_extn_utils_compress_set_clk_rec_mode(usecase);
1154 }
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05301155
1156 if (usecase->type == PCM_CAPTURE) {
1157 in = usecase->stream.in;
1158 if (in && is_loopback_input_device(in->device)) {
1159 ALOGD("%s: set custom mtmx params v1", __func__);
1160 audio_extn_set_custom_mtmx_params_v1(adev, usecase, true);
1161 }
1162 } else {
1163 audio_extn_set_custom_mtmx_params_v2(adev, usecase, true);
1164 }
Manish Dewangan58229382017-02-02 15:48:41 +05301165
Andy Hung756ecc12018-10-19 17:47:12 -07001166 // we shouldn't truncate mixer_path
1167 ALOGW_IF(strlcpy(mixer_path, use_case_table[usecase->id], sizeof(mixer_path))
1168 >= sizeof(mixer_path), "%s: truncation on mixer path", __func__);
1169 // this also appends to mixer_path
Banajit Goswami20cdd212015-09-11 01:11:30 -07001170 platform_add_backend_name(mixer_path, snd_device, usecase);
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07001171 ALOGD("%s: apply mixer and update path: %s", __func__, mixer_path);
Soumya Managoli6993b762018-06-28 16:04:57 +05301172 ret = audio_route_apply_and_update_path(adev->audio_route, mixer_path);
1173 if (!ret && usecase->id == USECASE_AUDIO_PLAYBACK_FM) {
1174 struct str_parms *parms = str_parms_create_str("fm_restore_volume=1");
1175 if (parms) {
1176 audio_extn_fm_set_parameters(adev, parms);
1177 str_parms_destroy(parms);
1178 }
1179 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001180 ALOGV("%s: exit", __func__);
1181 return 0;
1182}
1183
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001184int disable_audio_route(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001185 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001186{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001187 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001188 char mixer_path[MIXER_PATH_MAX_LENGTH];
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05301189 struct stream_in *in = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001190
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05301191 if (usecase == NULL || usecase->id == USECASE_INVALID)
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001192 return -EINVAL;
1193
1194 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Surendar Karka93cd25a2018-08-28 14:21:37 +05301195 if (usecase->type == PCM_CAPTURE || usecase->type == TRANSCODE_LOOPBACK_TX)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001196 snd_device = usecase->in_snd_device;
1197 else
1198 snd_device = usecase->out_snd_device;
Andy Hung756ecc12018-10-19 17:47:12 -07001199 // we shouldn't truncate mixer_path
1200 ALOGW_IF(strlcpy(mixer_path, use_case_table[usecase->id], sizeof(mixer_path))
1201 >= sizeof(mixer_path), "%s: truncation on mixer path", __func__);
1202 // this also appends to mixer_path
Banajit Goswami20cdd212015-09-11 01:11:30 -07001203 platform_add_backend_name(mixer_path, snd_device, usecase);
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07001204 ALOGD("%s: reset and update mixer path: %s", __func__, mixer_path);
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07001205 audio_route_reset_and_update_path(adev->audio_route, mixer_path);
Carter Hsu2e429db2019-05-14 18:50:52 +08001206 if (usecase->type == PCM_CAPTURE) {
1207 struct stream_in *in = usecase->stream.in;
1208 if (in && in->ec_opened) {
1209 platform_set_echo_reference(in->dev, false, AUDIO_DEVICE_NONE);
1210 in->ec_opened = false;
1211 }
1212 }
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001213 audio_extn_sound_trigger_update_stream_status(usecase, ST_EVENT_STREAM_FREE);
Dhananjay Kumar45b71742014-05-29 21:47:27 +05301214 audio_extn_listen_update_stream_status(usecase, LISTEN_EVENT_STREAM_FREE);
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05301215
1216 if (usecase->type == PCM_CAPTURE) {
1217 in = usecase->stream.in;
1218 if (in && is_loopback_input_device(in->device)) {
1219 ALOGD("%s: reset custom mtmx params v1", __func__);
1220 audio_extn_set_custom_mtmx_params_v1(adev, usecase, false);
1221 }
1222 } else {
1223 audio_extn_set_custom_mtmx_params_v2(adev, usecase, false);
1224 }
1225
Weiyin Jiang298ffd92019-06-03 14:29:30 +08001226 if ((usecase->type == PCM_PLAYBACK) &&
1227 (usecase->stream.out != NULL))
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +05301228 usecase->stream.out->pspd_coeff_sent = false;
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05301229
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001230 ALOGV("%s: exit", __func__);
1231 return 0;
1232}
1233
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -07001234int enable_snd_device(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001235 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001236{
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301237 int i, num_devices = 0;
1238 snd_device_t new_snd_devices[SND_DEVICE_OUT_END];
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001239 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
1240
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001241 if (snd_device < SND_DEVICE_MIN ||
1242 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001243 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001244 return -EINVAL;
1245 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001246
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001247 if (platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0) {
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001248 ALOGE("%s: Invalid sound device returned", __func__);
1249 return -EINVAL;
1250 }
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001251
1252 adev->snd_dev_ref_cnt[snd_device]++;
1253
1254 if ((adev->snd_dev_ref_cnt[snd_device] > 1) &&
1255 (platform_split_snd_device(adev->platform,
1256 snd_device,
1257 &num_devices,
1258 new_snd_devices) != 0)) {
Eric Laurent994a6932013-07-17 11:51:42 -07001259 ALOGV("%s: snd_device(%d: %s) is already active",
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001260 __func__, snd_device, device_name);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001261 return 0;
1262 }
1263
Gopikrishnaiah Anandane85d0462014-06-30 21:41:20 -07001264 if (audio_extn_spkr_prot_is_enabled())
1265 audio_extn_spkr_prot_calib_cancel(adev);
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -07001266
Aalique Grahame22e49102018-12-18 14:23:57 -08001267 audio_extn_dsm_feedback_enable(adev, snd_device, true);
1268
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001269 if (platform_can_enable_spkr_prot_on_device(snd_device) &&
1270 audio_extn_spkr_prot_is_enabled()) {
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001271 if (platform_get_spkr_prot_acdb_id(snd_device) < 0) {
1272 goto err;
1273 }
1274 audio_extn_dev_arbi_acquire(snd_device);
1275 if (audio_extn_spkr_prot_start_processing(snd_device)) {
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -08001276 ALOGE("%s: spkr_start_processing failed", __func__);
Ravit Dennisaaee49c2015-02-04 21:26:22 +02001277 audio_extn_dev_arbi_release(snd_device);
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001278 goto err;
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -08001279 }
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001280 } else if (platform_split_snd_device(adev->platform,
1281 snd_device,
1282 &num_devices,
1283 new_snd_devices) == 0) {
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301284 for (i = 0; i < num_devices; i++) {
1285 enable_snd_device(adev, new_snd_devices[i]);
1286 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001287 platform_set_speaker_gain_in_combo(adev, snd_device, true);
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -08001288 } else {
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07001289 ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, device_name);
Naresh Tannirucd2353e2016-08-19 00:37:25 +05301290
Preetam Singh Ranawatf1d417c2017-01-10 17:00:32 +05301291
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001292 if ((SND_DEVICE_OUT_BT_A2DP == snd_device) &&
1293 (audio_extn_a2dp_start_playback() < 0)) {
1294 ALOGE(" fail to configure A2dp Source control path ");
1295 goto err;
1296 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001297
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001298 if ((SND_DEVICE_IN_BT_A2DP == snd_device) &&
1299 (audio_extn_a2dp_start_capture() < 0)) {
1300 ALOGE(" fail to configure A2dp Sink control path ");
1301 goto err;
1302 }
Naresh Tannirucd2353e2016-08-19 00:37:25 +05301303
Zhou Song12c29502019-03-16 10:37:18 +08001304 if (((SND_DEVICE_OUT_BT_SCO_SWB == snd_device) ||
1305 (SND_DEVICE_IN_BT_SCO_MIC_SWB_NREC == snd_device) ||
1306 (SND_DEVICE_IN_BT_SCO_MIC_SWB == snd_device)) &&
1307 (audio_extn_sco_start_configuration() < 0)) {
1308 ALOGE(" fail to configure sco control path ");
1309 goto err;
1310 }
1311
Zhou Song331c8e52019-08-26 14:16:12 +08001312 configure_btsco_sample_rate(snd_device);
Bharath Ramachandramurthy0de16782014-03-28 21:34:33 -07001313 /* due to the possibility of calibration overwrite between listen
1314 and audio, notify listen hal before audio calibration is sent */
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001315 audio_extn_sound_trigger_update_device_status(snd_device,
1316 ST_EVENT_SND_DEVICE_BUSY);
Dhananjay Kumar45b71742014-05-29 21:47:27 +05301317 audio_extn_listen_update_device_status(snd_device,
1318 LISTEN_EVENT_SND_DEVICE_BUSY);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001319 if (platform_get_snd_device_acdb_id(snd_device) < 0) {
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001320 audio_extn_sound_trigger_update_device_status(snd_device,
1321 ST_EVENT_SND_DEVICE_FREE);
Dhananjay Kumar45b71742014-05-29 21:47:27 +05301322 audio_extn_listen_update_device_status(snd_device,
1323 LISTEN_EVENT_SND_DEVICE_FREE);
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001324 goto err;
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -07001325 }
Lior Barenboim0b61bc72014-05-13 13:01:37 +03001326 audio_extn_dev_arbi_acquire(snd_device);
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07001327 audio_route_apply_and_update_path(adev->audio_route, device_name);
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301328
1329 if (SND_DEVICE_OUT_HEADPHONES == snd_device &&
1330 !adev->native_playback_enabled &&
1331 audio_is_true_native_stream_active(adev)) {
1332 ALOGD("%s: %d: napb: enabling native mode in hardware",
1333 __func__, __LINE__);
1334 audio_route_apply_and_update_path(adev->audio_route,
1335 "true-native-mode");
1336 adev->native_playback_enabled = true;
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +05301337 }
Dhanalakshmi Siddani10621622018-04-30 15:07:27 +05301338 if (((snd_device == SND_DEVICE_IN_HANDSET_6MIC) ||
1339 (snd_device == SND_DEVICE_IN_HANDSET_QMIC)) &&
Eric Laurent637e2d42018-11-15 12:24:31 -08001340 (audio_extn_ffv_get_stream() == adev_get_active_input(adev))) {
Garmond Leunge2433c32017-09-28 21:51:22 -07001341 ALOGD("%s: init ec ref loopback", __func__);
1342 audio_extn_ffv_init_ec_ref_loopback(adev, snd_device);
1343 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001344 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001345 return 0;
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001346err:
1347 adev->snd_dev_ref_cnt[snd_device]--;
1348 return -EINVAL;;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001349}
1350
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001351int disable_snd_device(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001352 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001353{
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301354 int i, num_devices = 0;
1355 snd_device_t new_snd_devices[SND_DEVICE_OUT_END];
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001356 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
1357
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001358 if (snd_device < SND_DEVICE_MIN ||
1359 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001360 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001361 return -EINVAL;
1362 }
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001363
1364 if (platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0) {
1365 ALOGE("%s: Invalid sound device returned", __func__);
1366 return -EINVAL;
1367 }
1368
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001369 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
1370 ALOGE("%s: device ref cnt is already 0", __func__);
1371 return -EINVAL;
1372 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001373
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001374 adev->snd_dev_ref_cnt[snd_device]--;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -07001375
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001376
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001377 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07001378 ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, device_name);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301379
Aalique Grahame22e49102018-12-18 14:23:57 -08001380 audio_extn_dsm_feedback_enable(adev, snd_device, false);
1381
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001382 if (platform_can_enable_spkr_prot_on_device(snd_device) &&
1383 audio_extn_spkr_prot_is_enabled()) {
Anish Kumar46c7b872014-09-09 01:49:44 -07001384 audio_extn_spkr_prot_stop_processing(snd_device);
Vignesh Kulothungan3b5fae52017-09-25 12:16:30 -07001385
1386 // when speaker device is disabled, reset swap.
1387 // will be renabled on usecase start
1388 platform_set_swap_channels(adev, false);
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001389 } else if (platform_split_snd_device(adev->platform,
1390 snd_device,
1391 &num_devices,
1392 new_snd_devices) == 0) {
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301393 for (i = 0; i < num_devices; i++) {
1394 disable_snd_device(adev, new_snd_devices[i]);
1395 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001396 platform_set_speaker_gain_in_combo(adev, snd_device, false);
Lior Barenboim0b61bc72014-05-13 13:01:37 +03001397 } else {
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07001398 audio_route_reset_and_update_path(adev->audio_route, device_name);
Lior Barenboim0b61bc72014-05-13 13:01:37 +03001399 }
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001400
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001401 if (snd_device == SND_DEVICE_OUT_BT_A2DP)
Naresh Tannirucd2353e2016-08-19 00:37:25 +05301402 audio_extn_a2dp_stop_playback();
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001403 else if (snd_device == SND_DEVICE_IN_BT_A2DP)
Florian Pfister1a84f312018-07-19 14:38:18 +02001404 audio_extn_a2dp_stop_capture();
Zhou Songd6d71752019-05-21 18:08:51 +08001405 else if ((snd_device == SND_DEVICE_OUT_HDMI) ||
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001406 (snd_device == SND_DEVICE_OUT_DISPLAY_PORT))
Ashish Jain81eb2a82015-05-13 10:52:34 +05301407 adev->is_channel_status_set = false;
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001408 else if ((snd_device == SND_DEVICE_OUT_HEADPHONES) &&
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301409 adev->native_playback_enabled) {
1410 ALOGD("%s: %d: napb: disabling native mode in hardware",
1411 __func__, __LINE__);
1412 audio_route_reset_and_update_path(adev->audio_route,
1413 "true-native-mode");
1414 adev->native_playback_enabled = false;
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001415 } else if ((snd_device == SND_DEVICE_OUT_HEADPHONES) &&
Preetam Singh Ranawatb0c0dd72016-08-18 00:32:06 +05301416 adev->asrc_mode_enabled) {
1417 ALOGD("%s: %d: disabling asrc mode in hardware", __func__, __LINE__);
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +05301418 disable_asrc_mode(adev);
1419 audio_route_apply_and_update_path(adev->audio_route, "hph-lowpower-mode");
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001420 } else if (((snd_device == SND_DEVICE_IN_HANDSET_6MIC) ||
Dhanalakshmi Siddaniaf4bd622019-02-27 16:28:06 +05301421 (snd_device == SND_DEVICE_IN_HANDSET_QMIC)) &&
Eric Laurent637e2d42018-11-15 12:24:31 -08001422 (audio_extn_ffv_get_stream() == adev_get_active_input(adev))) {
Garmond Leunge2433c32017-09-28 21:51:22 -07001423 ALOGD("%s: deinit ec ref loopback", __func__);
1424 audio_extn_ffv_deinit_ec_ref_loopback(adev, snd_device);
1425 }
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001426
1427 audio_extn_utils_release_snd_device(snd_device);
1428 } else {
1429 if (platform_split_snd_device(adev->platform,
1430 snd_device,
1431 &num_devices,
1432 new_snd_devices) == 0) {
1433 for (i = 0; i < num_devices; i++) {
1434 adev->snd_dev_ref_cnt[new_snd_devices[i]]--;
1435 }
1436 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001437 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001438
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001439 return 0;
1440}
1441
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001442/*
1443 legend:
1444 uc - existing usecase
1445 new_uc - new usecase
1446 d1, d11, d2 - SND_DEVICE enums
1447 a1, a2 - corresponding ANDROID device enums
1448 B1, B2 - backend strings
1449
1450case 1
1451 uc->dev d1 (a1) B1
1452 new_uc->dev d1 (a1), d2 (a2) B1, B2
1453
1454 resolution: disable and enable uc->dev on d1
1455
1456case 2
1457 uc->dev d1 (a1) B1
1458 new_uc->dev d11 (a1) B1
1459
1460 resolution: need to switch uc since d1 and d11 are related
1461 (e.g. speaker and voice-speaker)
1462 use ANDROID_DEVICE_OUT enums to match devices since SND_DEVICE enums may vary
1463
1464case 3
1465 uc->dev d1 (a1) B1
1466 new_uc->dev d2 (a2) B2
1467
1468 resolution: no need to switch uc
1469
1470case 4
1471 uc->dev d1 (a1) B1
1472 new_uc->dev d2 (a2) B1
1473
1474 resolution: disable enable uc-dev on d2 since backends match
1475 we cannot enable two streams on two different devices if they
1476 share the same backend. e.g. if offload is on speaker device using
1477 QUAD_MI2S backend and a low-latency stream is started on voice-handset
1478 using the same backend, offload must also be switched to voice-handset.
1479
1480case 5
1481 uc->dev d1 (a1) B1
1482 new_uc->dev d1 (a1), d2 (a2) B1
1483
1484 resolution: disable enable uc-dev on d2 since backends match
1485 we cannot enable two streams on two different devices if they
1486 share the same backend.
1487
1488case 6
1489 uc->dev d1 (a1) B1
1490 new_uc->dev d2 (a1) B2
1491
1492 resolution: no need to switch
1493
1494case 7
1495 uc->dev d1 (a1), d2 (a2) B1, B2
1496 new_uc->dev d1 (a1) B1
1497
1498 resolution: no need to switch
1499
Zhou Song4ba65882018-07-09 14:48:07 +08001500case 8
1501 uc->dev d1 (a1) B1
1502 new_uc->dev d11 (a1), d2 (a2) B1, B2
1503 resolution: compared to case 1, for this case, d1 and d11 are related
1504 then need to do the same as case 2 to siwtch to new uc
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001505*/
1506static snd_device_t derive_playback_snd_device(void * platform,
1507 struct audio_usecase *uc,
1508 struct audio_usecase *new_uc,
1509 snd_device_t new_snd_device)
1510{
Siddartha Shaik31b530e2017-05-19 15:26:33 +05301511 audio_devices_t a1, a2;
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001512
1513 snd_device_t d1 = uc->out_snd_device;
1514 snd_device_t d2 = new_snd_device;
1515
Siddartha Shaik31b530e2017-05-19 15:26:33 +05301516 switch (uc->type) {
Surendar Karka93cd25a2018-08-28 14:21:37 +05301517 case TRANSCODE_LOOPBACK_RX :
Siddartha Shaik31b530e2017-05-19 15:26:33 +05301518 a1 = uc->stream.inout->out_config.devices;
1519 a2 = new_uc->stream.inout->out_config.devices;
1520 break;
1521 default :
1522 a1 = uc->stream.out->devices;
1523 a2 = new_uc->stream.out->devices;
1524 break;
1525 }
1526
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001527 // Treat as a special case when a1 and a2 are not disjoint
1528 if ((a1 != a2) && (a1 & a2)) {
1529 snd_device_t d3[2];
1530 int num_devices = 0;
1531 int ret = platform_split_snd_device(platform,
1532 popcount(a1) > 1 ? d1 : d2,
1533 &num_devices,
1534 d3);
1535 if (ret < 0) {
1536 if (ret != -ENOSYS) {
1537 ALOGW("%s failed to split snd_device %d",
1538 __func__,
1539 popcount(a1) > 1 ? d1 : d2);
1540 }
1541 goto end;
1542 }
1543
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001544 if (platform_check_backends_match(d3[0], d3[1])) {
1545 return d2; // case 5
1546 } else {
Samyak Jaind826b502019-07-17 16:16:42 +05301547 if (popcount(a1) > 1)
1548 return d1; //case 7
Garmond Leungb9eeba42018-09-18 11:10:41 -07001549 // check if d1 is related to any of d3's
1550 if (d1 == d3[0] || d1 == d3[1])
Zhou Song4ba65882018-07-09 14:48:07 +08001551 return d1; // case 1
1552 else
1553 return d3[1]; // case 8
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001554 }
1555 } else {
1556 if (platform_check_backends_match(d1, d2)) {
1557 return d2; // case 2, 4
1558 } else {
1559 return d1; // case 6, 3
1560 }
1561 }
1562
1563end:
1564 return d2; // return whatever was calculated before.
1565}
1566
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001567static void check_usecases_codec_backend(struct audio_device *adev,
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301568 struct audio_usecase *uc_info,
1569 snd_device_t snd_device)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001570{
1571 struct listnode *node;
1572 struct audio_usecase *usecase;
1573 bool switch_device[AUDIO_USECASE_MAX];
Chaithanya Krishna Bacharaju49e7db02017-03-14 11:57:26 +05301574 snd_device_t uc_derive_snd_device;
1575 snd_device_t derive_snd_device[AUDIO_USECASE_MAX];
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001576 snd_device_t split_snd_devices[SND_DEVICE_OUT_END];
1577 int i, num_uc_to_switch = 0, num_devices = 0;
kunleiz5cd52b82016-11-07 17:22:52 +08001578 int status = 0;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301579 bool force_restart_session = false;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001580 /*
1581 * This function is to make sure that all the usecases that are active on
1582 * the hardware codec backend are always routed to any one device that is
1583 * handled by the hardware codec.
1584 * For example, if low-latency and deep-buffer usecases are currently active
1585 * on speaker and out_set_parameters(headset) is received on low-latency
1586 * output, then we have to make sure deep-buffer is also switched to headset,
1587 * because of the limitation that both the devices cannot be enabled
1588 * at the same time as they share the same backend.
1589 */
Mingming Yin3ee55c62014-08-04 14:23:35 -07001590 /*
1591 * This call is to check if we need to force routing for a particular stream
1592 * If there is a backend configuration change for the device when a
1593 * new stream starts, then ADM needs to be closed and re-opened with the new
1594 * configuraion. This call check if we need to re-route all the streams
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -08001595 * associated with the backend. Touch tone + 24 bit + native playback.
Mingming Yin3ee55c62014-08-04 14:23:35 -07001596 */
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -08001597 bool force_routing = platform_check_and_set_codec_backend_cfg(adev, uc_info,
1598 snd_device);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301599 /* For a2dp device reconfigure all active sessions
1600 * with new AFE encoder format based on a2dp state
1601 */
1602 if ((SND_DEVICE_OUT_BT_A2DP == snd_device ||
1603 SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP == snd_device) &&
1604 audio_extn_a2dp_is_force_device_switch()) {
1605 force_routing = true;
1606 force_restart_session = true;
1607 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301608 ALOGD("%s:becf: force routing %d", __func__, force_routing);
1609
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001610 /* Disable all the usecases on the shared backend other than the
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -08001611 * specified usecase.
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -08001612 */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001613 for (i = 0; i < AUDIO_USECASE_MAX; i++)
1614 switch_device[i] = false;
1615
1616 list_for_each(node, &adev->usecase_list) {
1617 usecase = node_to_item(node, struct audio_usecase, list);
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -08001618
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301619 ALOGD("%s:becf: (%d) check_usecases curr device: %s, usecase device:%s "
1620 "backends match %d",__func__, i,
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301621 platform_get_snd_device_name(snd_device),
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301622 platform_get_snd_device_name(usecase->out_snd_device),
1623 platform_check_backends_match(snd_device, usecase->out_snd_device));
Rahul Sharma99770982019-03-06 17:05:26 +05301624 if ((usecase->type != PCM_CAPTURE) && (usecase != uc_info) &&
1625 (usecase->type != PCM_PASSTHROUGH)) {
Ashish Jain6a65b352017-03-21 17:24:40 +05301626 uc_derive_snd_device = derive_playback_snd_device(adev->platform,
1627 usecase, uc_info, snd_device);
1628 if (((uc_derive_snd_device != usecase->out_snd_device) || force_routing) &&
1629 ((usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) ||
1630 (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) ||
1631 (usecase->devices & AUDIO_DEVICE_OUT_USB_DEVICE) ||
Garmond Leung2fd4f122018-06-02 16:04:53 -07001632 (usecase->devices & AUDIO_DEVICE_OUT_USB_HEADSET) ||
Ashish Jain6a65b352017-03-21 17:24:40 +05301633 (usecase->devices & AUDIO_DEVICE_OUT_ALL_A2DP) ||
1634 (usecase->devices & AUDIO_DEVICE_OUT_ALL_SCO)) &&
1635 ((force_restart_session) ||
1636 (platform_check_backends_match(snd_device, usecase->out_snd_device)))) {
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301637 ALOGD("%s:becf: check_usecases (%s) is active on (%s) - disabling ..",
1638 __func__, use_case_table[usecase->id],
1639 platform_get_snd_device_name(usecase->out_snd_device));
1640 disable_audio_route(adev, usecase);
1641 switch_device[usecase->id] = true;
Chaithanya Krishna Bacharaju49e7db02017-03-14 11:57:26 +05301642 /* Enable existing usecase on derived playback device */
1643 derive_snd_device[usecase->id] = uc_derive_snd_device;
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301644 num_uc_to_switch++;
Ashish Jain6a65b352017-03-21 17:24:40 +05301645 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001646 }
1647 }
1648
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301649 ALOGD("%s:becf: check_usecases num.of Usecases to switch %d", __func__,
1650 num_uc_to_switch);
1651
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001652 if (num_uc_to_switch) {
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07001653 /* All streams have been de-routed. Disable the device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001654
Venkata Narendra Kumar Gutta7610e632014-04-14 23:16:38 +05301655 /* Make sure the previous devices to be disabled first and then enable the
1656 selected devices */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001657 list_for_each(node, &adev->usecase_list) {
1658 usecase = node_to_item(node, struct audio_usecase, list);
1659 if (switch_device[usecase->id]) {
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001660 /* Check if output sound device to be switched can be split and if any
1661 of the split devices match with derived sound device */
1662 if (platform_split_snd_device(adev->platform, usecase->out_snd_device,
1663 &num_devices, split_snd_devices) == 0) {
1664 adev->snd_dev_ref_cnt[usecase->out_snd_device]--;
1665 for (i = 0; i < num_devices; i++) {
1666 /* Disable devices that do not match with derived sound device */
1667 if (split_snd_devices[i] != derive_snd_device[usecase->id])
1668 disable_snd_device(adev, split_snd_devices[i]);
1669 }
1670 } else {
1671 disable_snd_device(adev, usecase->out_snd_device);
1672 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001673 }
1674 }
1675
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -07001676 list_for_each(node, &adev->usecase_list) {
1677 usecase = node_to_item(node, struct audio_usecase, list);
1678 if (switch_device[usecase->id]) {
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001679 if (platform_split_snd_device(adev->platform, usecase->out_snd_device,
1680 &num_devices, split_snd_devices) == 0) {
1681 /* Enable derived sound device only if it does not match with
1682 one of the split sound devices. This is because the matching
1683 sound device was not disabled */
1684 bool should_enable = true;
1685 for (i = 0; i < num_devices; i++) {
1686 if (derive_snd_device[usecase->id] == split_snd_devices[i]) {
1687 should_enable = false;
1688 break;
1689 }
1690 }
1691 if (should_enable)
1692 enable_snd_device(adev, derive_snd_device[usecase->id]);
1693 } else {
1694 enable_snd_device(adev, derive_snd_device[usecase->id]);
1695 }
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -07001696 }
1697 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001698
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001699 /* Re-route all the usecases on the shared backend other than the
1700 specified usecase to new snd devices */
1701 list_for_each(node, &adev->usecase_list) {
1702 usecase = node_to_item(node, struct audio_usecase, list);
Karthik Reddy Katta3abfee22016-02-23 10:55:27 +05301703 /* Update the out_snd_device only before enabling the audio route */
1704 if (switch_device[usecase->id]) {
Chaithanya Krishna Bacharaju49e7db02017-03-14 11:57:26 +05301705 usecase->out_snd_device = derive_snd_device[usecase->id];
Karthik Reddy Katta3abfee22016-02-23 10:55:27 +05301706 if (usecase->type != VOICE_CALL) {
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301707 ALOGD("%s:becf: enabling usecase (%s) on (%s)", __func__,
Karthik Reddy Katta3abfee22016-02-23 10:55:27 +05301708 use_case_table[usecase->id],
1709 platform_get_snd_device_name(usecase->out_snd_device));
kunleiz5cd52b82016-11-07 17:22:52 +08001710 /* Update voc calibration before enabling VoIP route */
1711 if (usecase->type == VOIP_CALL)
1712 status = platform_switch_voice_call_device_post(adev->platform,
Eric Laurent637e2d42018-11-15 12:24:31 -08001713 usecase->out_snd_device,
1714 platform_get_input_snd_device(
1715 adev->platform, NULL,
1716 uc_info->devices));
Avinash Vaish71a8b972014-07-24 15:36:33 +05301717 enable_audio_route(adev, usecase);
Sujin Panicker390724d2019-04-26 10:43:36 +05301718 if (usecase->stream.out && usecase->id == USECASE_AUDIO_PLAYBACK_VOIP) {
kunleiz46c47dd2018-03-19 16:28:09 +08001719 out_set_voip_volume(&usecase->stream.out->stream,
1720 usecase->stream.out->volume_l,
1721 usecase->stream.out->volume_r);
1722 }
Karthik Reddy Katta3abfee22016-02-23 10:55:27 +05301723 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001724 }
1725 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001726 }
1727}
1728
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05301729static void check_usecases_capture_codec_backend(struct audio_device *adev,
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001730 struct audio_usecase *uc_info,
1731 snd_device_t snd_device)
1732{
1733 struct listnode *node;
1734 struct audio_usecase *usecase;
1735 bool switch_device[AUDIO_USECASE_MAX];
1736 int i, num_uc_to_switch = 0;
Dhanalakshmi Siddanib678a802016-12-03 11:51:41 +05301737 int backend_check_cond = AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND;
kunleiz5cd52b82016-11-07 17:22:52 +08001738 int status = 0;
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001739
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05301740 bool force_routing = platform_check_and_set_capture_codec_backend_cfg(adev, uc_info,
1741 snd_device);
1742 ALOGD("%s:becf: force routing %d", __func__, force_routing);
Dhanalakshmi Siddanib678a802016-12-03 11:51:41 +05301743
1744 /*
1745 * Make sure out devices is checked against out codec backend device and
1746 * also in devices against in codec backend. Checking out device against in
1747 * codec backend or vice versa causes issues.
1748 */
1749 if (uc_info->type == PCM_CAPTURE)
1750 backend_check_cond = AUDIO_DEVICE_IN_ALL_CODEC_BACKEND;
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001751 /*
1752 * This function is to make sure that all the active capture usecases
1753 * are always routed to the same input sound device.
1754 * For example, if audio-record and voice-call usecases are currently
1755 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
1756 * is received for voice call then we have to make sure that audio-record
1757 * usecase is also switched to earpiece i.e. voice-dmic-ef,
1758 * because of the limitation that two devices cannot be enabled
1759 * at the same time if they share the same backend.
1760 */
1761 for (i = 0; i < AUDIO_USECASE_MAX; i++)
1762 switch_device[i] = false;
1763
1764 list_for_each(node, &adev->usecase_list) {
1765 usecase = node_to_item(node, struct audio_usecase, list);
Dhanalakshmi Siddanib678a802016-12-03 11:51:41 +05301766 /*
1767 * TODO: Enhance below condition to handle BT sco/USB multi recording
1768 */
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -08001769 if (usecase->type != PCM_PLAYBACK &&
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001770 usecase != uc_info &&
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05301771 (usecase->in_snd_device != snd_device || force_routing) &&
Dhanalakshmi Siddanib678a802016-12-03 11:51:41 +05301772 ((uc_info->devices & backend_check_cond) &&
Karthik Reddy Katta0d471762015-08-20 13:36:07 +05301773 (((usecase->devices & ~AUDIO_DEVICE_BIT_IN) & AUDIO_DEVICE_IN_ALL_CODEC_BACKEND) ||
Satya Krishna Pindiproli458b5a72017-03-13 15:14:24 +05301774 (usecase->type == VOIP_CALL))) &&
Carter Hsu1d2a0532018-10-04 09:24:36 +08001775 ((uc_info->type == VOICE_CALL &&
1776 usecase->devices == AUDIO_DEVICE_IN_VOICE_CALL) ||
1777 platform_check_backends_match(snd_device,\
1778 usecase->in_snd_device)) &&
Anish Kumara020a7c2014-10-17 11:13:22 -07001779 (usecase->id != USECASE_AUDIO_SPKR_CALIB_TX)) {
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001780 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
1781 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -07001782 platform_get_snd_device_name(usecase->in_snd_device));
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001783 disable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001784 switch_device[usecase->id] = true;
1785 num_uc_to_switch++;
1786 }
1787 }
1788
1789 if (num_uc_to_switch) {
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07001790 /* All streams have been de-routed. Disable the device */
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001791
Venkata Narendra Kumar Gutta7610e632014-04-14 23:16:38 +05301792 /* Make sure the previous devices to be disabled first and then enable the
1793 selected devices */
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001794 list_for_each(node, &adev->usecase_list) {
1795 usecase = node_to_item(node, struct audio_usecase, list);
1796 if (switch_device[usecase->id]) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001797 disable_snd_device(adev, usecase->in_snd_device);
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -08001798 }
1799 }
1800
1801 list_for_each(node, &adev->usecase_list) {
1802 usecase = node_to_item(node, struct audio_usecase, list);
1803 if (switch_device[usecase->id]) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001804 enable_snd_device(adev, snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001805 }
1806 }
1807
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001808 /* Re-route all the usecases on the shared backend other than the
1809 specified usecase to new snd devices */
1810 list_for_each(node, &adev->usecase_list) {
1811 usecase = node_to_item(node, struct audio_usecase, list);
1812 /* Update the in_snd_device only before enabling the audio route */
1813 if (switch_device[usecase->id] ) {
1814 usecase->in_snd_device = snd_device;
kunleiz5cd52b82016-11-07 17:22:52 +08001815 if (usecase->type != VOICE_CALL) {
1816 /* Update voc calibration before enabling VoIP route */
1817 if (usecase->type == VOIP_CALL)
1818 status = platform_switch_voice_call_device_post(adev->platform,
Zhou Song557e7282017-05-05 17:18:18 +08001819 platform_get_output_snd_device(adev->platform, uc_info->stream.out),
kunleiz5cd52b82016-11-07 17:22:52 +08001820 usecase->in_snd_device);
Avinash Vaish71a8b972014-07-24 15:36:33 +05301821 enable_audio_route(adev, usecase);
kunleiz5cd52b82016-11-07 17:22:52 +08001822 }
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001823 }
1824 }
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001825 }
1826}
1827
Mingming Yin3a941d42016-02-17 18:08:05 -08001828static void reset_hdmi_sink_caps(struct stream_out *out) {
1829 int i = 0;
1830
1831 for (i = 0; i<= MAX_SUPPORTED_CHANNEL_MASKS; i++) {
1832 out->supported_channel_masks[i] = 0;
1833 }
1834 for (i = 0; i<= MAX_SUPPORTED_FORMATS; i++) {
1835 out->supported_formats[i] = 0;
1836 }
1837 for (i = 0; i<= MAX_SUPPORTED_SAMPLE_RATES; i++) {
1838 out->supported_sample_rates[i] = 0;
1839 }
1840}
1841
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001842/* must be called with hw device mutex locked */
Mingming Yin3a941d42016-02-17 18:08:05 -08001843static int read_hdmi_sink_caps(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001844{
Mingming Yin3a941d42016-02-17 18:08:05 -08001845 int ret = 0, i = 0, j = 0;
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07001846 int channels = platform_edid_get_max_channels_v2(out->dev->platform,
1847 out->extconn.cs.controller,
1848 out->extconn.cs.stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001849
Mingming Yin3a941d42016-02-17 18:08:05 -08001850 reset_hdmi_sink_caps(out);
1851
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07001852 /* Cache ext disp type */
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07001853 if (platform_get_ext_disp_type_v2(adev->platform,
1854 out->extconn.cs.controller,
1855 out->extconn.cs.stream <= 0)) {
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07001856 ALOGE("%s: Failed to query disp type, ret:%d", __func__, ret);
Garmond Leung37850ab2016-10-06 11:42:18 -07001857 return -EINVAL;
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07001858 }
1859
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001860 switch (channels) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001861 case 8:
Mingming Yin3a941d42016-02-17 18:08:05 -08001862 ALOGV("%s: HDMI supports 7.1 channels", __func__);
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -07001863 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_7POINT1;
Mingming Yin3a941d42016-02-17 18:08:05 -08001864 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_6POINT1;
1865 case 6:
1866 ALOGV("%s: HDMI supports 5.1 channels", __func__);
1867 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_5POINT1;
1868 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_PENTA;
1869 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_QUAD;
1870 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_SURROUND;
1871 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_2POINT1;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001872 break;
1873 default:
Mingming Yin3a941d42016-02-17 18:08:05 -08001874 ALOGE("invalid/nonstandard channal count[%d]",channels);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001875 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001876 break;
1877 }
Mingming Yin3a941d42016-02-17 18:08:05 -08001878
1879 // check channel format caps
1880 i = 0;
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07001881 if (platform_is_edid_supported_format_v2(out->dev->platform, AUDIO_FORMAT_AC3,
1882 out->extconn.cs.controller,
1883 out->extconn.cs.stream)) {
Mingming Yin3a941d42016-02-17 18:08:05 -08001884 ALOGV(":%s HDMI supports AC3/EAC3 formats", __func__);
1885 out->supported_formats[i++] = AUDIO_FORMAT_AC3;
1886 //Adding EAC3/EAC3_JOC formats if AC3 is supported by the sink.
1887 //EAC3/EAC3_JOC will be converted to AC3 for decoding if needed
1888 out->supported_formats[i++] = AUDIO_FORMAT_E_AC3;
1889 out->supported_formats[i++] = AUDIO_FORMAT_E_AC3_JOC;
1890 }
1891
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07001892 if (platform_is_edid_supported_format_v2(out->dev->platform, AUDIO_FORMAT_DOLBY_TRUEHD,
1893 out->extconn.cs.controller,
1894 out->extconn.cs.stream)) {
Ben Romberger1aaaf862017-04-06 17:49:46 -07001895 ALOGV(":%s HDMI supports TRUE HD format", __func__);
1896 out->supported_formats[i++] = AUDIO_FORMAT_DOLBY_TRUEHD;
1897 }
1898
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07001899 if (platform_is_edid_supported_format_v2(out->dev->platform, AUDIO_FORMAT_DTS,
1900 out->extconn.cs.controller,
1901 out->extconn.cs.stream)) {
Mingming Yin3a941d42016-02-17 18:08:05 -08001902 ALOGV(":%s HDMI supports DTS format", __func__);
1903 out->supported_formats[i++] = AUDIO_FORMAT_DTS;
1904 }
1905
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07001906 if (platform_is_edid_supported_format_v2(out->dev->platform, AUDIO_FORMAT_DTS_HD,
1907 out->extconn.cs.controller,
1908 out->extconn.cs.stream)) {
Mingming Yin3a941d42016-02-17 18:08:05 -08001909 ALOGV(":%s HDMI supports DTS HD format", __func__);
1910 out->supported_formats[i++] = AUDIO_FORMAT_DTS_HD;
1911 }
1912
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07001913 if (platform_is_edid_supported_format_v2(out->dev->platform, AUDIO_FORMAT_IEC61937,
1914 out->extconn.cs.controller,
1915 out->extconn.cs.stream)) {
Naresh Tanniru928f0862017-04-07 16:44:23 -07001916 ALOGV(":%s HDMI supports IEC61937 format", __func__);
1917 out->supported_formats[i++] = AUDIO_FORMAT_IEC61937;
1918 }
1919
Mingming Yin3a941d42016-02-17 18:08:05 -08001920
1921 // check sample rate caps
1922 i = 0;
1923 for (j = 0; j < MAX_SUPPORTED_SAMPLE_RATES; j++) {
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07001924 if (platform_is_edid_supported_sample_rate_v2(out->dev->platform, out_hdmi_sample_rates[j],
1925 out->extconn.cs.controller,
1926 out->extconn.cs.stream)) {
Mingming Yin3a941d42016-02-17 18:08:05 -08001927 ALOGV(":%s HDMI supports sample rate:%d", __func__, out_hdmi_sample_rates[j]);
1928 out->supported_sample_rates[i++] = out_hdmi_sample_rates[j];
1929 }
1930 }
1931
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001932 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001933}
1934
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001935static inline ssize_t read_usb_sup_sample_rates(bool is_playback __unused,
1936 uint32_t *supported_sample_rates __unused,
1937 uint32_t max_rates __unused)
1938{
1939 ssize_t count = audio_extn_usb_get_sup_sample_rates(is_playback,
1940 supported_sample_rates,
1941 max_rates);
Ashish Jain4847e9d2017-08-17 19:16:57 +05301942 ssize_t i = 0;
1943
1944 for (i=0; i<count; i++) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001945 ALOGV("%s %s %d", __func__, is_playback ? "P" : "C",
1946 supported_sample_rates[i]);
1947 }
1948 return count;
1949}
1950
1951static inline int read_usb_sup_channel_masks(bool is_playback,
1952 audio_channel_mask_t *supported_channel_masks,
1953 uint32_t max_masks)
1954{
1955 int channels = audio_extn_usb_get_max_channels(is_playback);
1956 int channel_count;
1957 uint32_t num_masks = 0;
1958 if (channels > MAX_HIFI_CHANNEL_COUNT)
1959 channels = MAX_HIFI_CHANNEL_COUNT;
1960
1961 if (is_playback) {
Eric Laurent68a87112019-05-01 18:07:29 -07001962 // start from 2 channels as framework currently doesn't support mono.
1963 if (channels >= FCC_2) {
1964 supported_channel_masks[num_masks++] = audio_channel_out_mask_from_count(FCC_2);
1965 }
1966 for (channel_count = FCC_2;
1967 channel_count <= channels && num_masks < max_masks;
1968 ++channel_count) {
1969 supported_channel_masks[num_masks++] =
1970 audio_channel_mask_for_index_assignment_from_count(channel_count);
1971 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001972 } else {
vincenttewf51c94e2019-05-07 10:28:53 +08001973 // For capture we report all supported channel masks from 1 channel up.
1974 channel_count = MIN_CHANNEL_COUNT;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001975 // audio_channel_in_mask_from_count() does the right conversion to either positional or
1976 // indexed mask
Eric Laurent68a87112019-05-01 18:07:29 -07001977 for ( ; channel_count <= channels && num_masks < max_masks; channel_count++) {
1978 audio_channel_mask_t mask = AUDIO_CHANNEL_NONE;
1979 if (channel_count <= FCC_2) {
1980 mask = audio_channel_in_mask_from_count(channel_count);
1981 supported_channel_masks[num_masks++] = mask;
1982 }
1983 const audio_channel_mask_t index_mask =
1984 audio_channel_mask_for_index_assignment_from_count(channel_count);
1985 if (mask != index_mask && num_masks < max_masks) { // ensure index mask added.
1986 supported_channel_masks[num_masks++] = index_mask;
1987 }
1988 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001989 }
Lakshman Chaluvarajub79fafd2018-11-21 10:24:37 +05301990
vincenttewf51c94e2019-05-07 10:28:53 +08001991 for (size_t i = 0; i < num_masks; ++i) {
1992 ALOGV("%s: %s supported ch %d supported_channel_masks[%zu] %08x num_masks %d", __func__,
1993 is_playback ? "P" : "C", channels, i, supported_channel_masks[i], num_masks);
Lakshman Chaluvarajub79fafd2018-11-21 10:24:37 +05301994 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001995 return num_masks;
1996}
1997
1998static inline int read_usb_sup_formats(bool is_playback __unused,
1999 audio_format_t *supported_formats,
2000 uint32_t max_formats __unused)
2001{
2002 int bitwidth = audio_extn_usb_get_max_bit_width(is_playback);
2003 switch (bitwidth) {
2004 case 24:
2005 // XXX : usb.c returns 24 for s24 and s24_le?
2006 supported_formats[0] = AUDIO_FORMAT_PCM_24_BIT_PACKED;
2007 break;
2008 case 32:
2009 supported_formats[0] = AUDIO_FORMAT_PCM_32_BIT;
2010 break;
2011 case 16:
2012 default :
2013 supported_formats[0] = AUDIO_FORMAT_PCM_16_BIT;
2014 break;
2015 }
2016 ALOGV("%s: %s supported format %d", __func__,
2017 is_playback ? "P" : "C", bitwidth);
2018 return 1;
2019}
2020
2021static inline int read_usb_sup_params_and_compare(bool is_playback,
2022 audio_format_t *format,
2023 audio_format_t *supported_formats,
2024 uint32_t max_formats,
2025 audio_channel_mask_t *mask,
2026 audio_channel_mask_t *supported_channel_masks,
2027 uint32_t max_masks,
2028 uint32_t *rate,
2029 uint32_t *supported_sample_rates,
2030 uint32_t max_rates) {
2031 int ret = 0;
2032 int num_formats;
2033 int num_masks;
2034 int num_rates;
2035 int i;
2036
2037 num_formats = read_usb_sup_formats(is_playback, supported_formats,
2038 max_formats);
2039 num_masks = read_usb_sup_channel_masks(is_playback, supported_channel_masks,
2040 max_masks);
2041
2042 num_rates = read_usb_sup_sample_rates(is_playback,
2043 supported_sample_rates, max_rates);
2044
2045#define LUT(table, len, what, dflt) \
2046 for (i=0; i<len && (table[i] != what); i++); \
2047 if (i==len) { ret |= (what == dflt ? 0 : -1); what=table[0]; }
2048
2049 LUT(supported_formats, num_formats, *format, AUDIO_FORMAT_DEFAULT);
2050 LUT(supported_channel_masks, num_masks, *mask, AUDIO_CHANNEL_NONE);
2051 LUT(supported_sample_rates, num_rates, *rate, 0);
2052
2053#undef LUT
2054 return ret < 0 ? -EINVAL : 0; // HACK TBD
2055}
2056
Alexy Josephb1379942016-01-29 15:49:38 -08002057audio_usecase_t get_usecase_id_from_usecase_type(const struct audio_device *adev,
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -08002058 usecase_type_t type)
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002059{
2060 struct audio_usecase *usecase;
2061 struct listnode *node;
2062
2063 list_for_each(node, &adev->usecase_list) {
2064 usecase = node_to_item(node, struct audio_usecase, list);
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -08002065 if (usecase->type == type) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002066 ALOGV("%s: usecase id %d", __func__, usecase->id);
2067 return usecase->id;
2068 }
2069 }
2070 return USECASE_INVALID;
2071}
2072
Alexy Josephb1379942016-01-29 15:49:38 -08002073struct audio_usecase *get_usecase_from_list(const struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002074 audio_usecase_t uc_id)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002075{
2076 struct audio_usecase *usecase;
2077 struct listnode *node;
2078
2079 list_for_each(node, &adev->usecase_list) {
2080 usecase = node_to_item(node, struct audio_usecase, list);
2081 if (usecase->id == uc_id)
2082 return usecase;
2083 }
2084 return NULL;
2085}
2086
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302087/*
2088 * is a true native playback active
2089 */
2090bool audio_is_true_native_stream_active(struct audio_device *adev)
2091{
2092 bool active = false;
2093 int i = 0;
2094 struct listnode *node;
2095
2096 if (NATIVE_AUDIO_MODE_TRUE_44_1 != platform_get_native_support()) {
2097 ALOGV("%s:napb: not in true mode or non hdphones device",
2098 __func__);
2099 active = false;
2100 goto exit;
2101 }
2102
2103 list_for_each(node, &adev->usecase_list) {
2104 struct audio_usecase *uc;
2105 uc = node_to_item(node, struct audio_usecase, list);
2106 struct stream_out *curr_out =
2107 (struct stream_out*) uc->stream.out;
2108
2109 if (curr_out && PCM_PLAYBACK == uc->type) {
2110 ALOGD("%s:napb: (%d) (%s)id (%d) sr %d bw "
2111 "(%d) device %s", __func__, i++, use_case_table[uc->id],
2112 uc->id, curr_out->sample_rate,
2113 curr_out->bit_width,
2114 platform_get_snd_device_name(uc->out_snd_device));
2115
2116 if (is_offload_usecase(uc->id) &&
2117 (curr_out->sample_rate == OUTPUT_SAMPLING_RATE_44100)) {
2118 active = true;
2119 ALOGD("%s:napb:native stream detected", __func__);
2120 }
2121 }
2122 }
2123exit:
2124 return active;
2125}
2126
Xiaojun Sang785b5da2017-08-03 15:52:29 +08002127uint32_t adev_get_dsp_bit_width_enforce_mode()
2128{
2129 if (adev == NULL) {
2130 ALOGE("%s: adev is null. Disable DSP bit width enforce mode.\n", __func__);
2131 return 0;
2132 }
2133 return adev->dsp_bit_width_enforce_mode;
2134}
2135
2136static uint32_t adev_init_dsp_bit_width_enforce_mode(struct mixer *mixer)
2137{
2138 char value[PROPERTY_VALUE_MAX];
2139 int trial;
2140 uint32_t dsp_bit_width_enforce_mode = 0;
2141
2142 if (!mixer) {
2143 ALOGE("%s: adev mixer is null. cannot update DSP bitwidth.\n",
2144 __func__);
2145 return 0;
2146 }
2147
2148 if (property_get("persist.vendor.audio_hal.dsp_bit_width_enforce_mode",
2149 value, NULL) > 0) {
2150 trial = atoi(value);
2151 switch (trial) {
2152 case 16:
2153 dsp_bit_width_enforce_mode = 16;
2154 break;
2155 case 24:
2156 dsp_bit_width_enforce_mode = 24;
2157 break;
2158 case 32:
2159 dsp_bit_width_enforce_mode = 32;
2160 break;
2161 default:
2162 dsp_bit_width_enforce_mode = 0;
2163 ALOGD("%s Dynamic DSP bitwidth config is disabled.", __func__);
2164 break;
2165 }
2166 }
2167
2168 return dsp_bit_width_enforce_mode;
2169}
2170
2171static void audio_enable_asm_bit_width_enforce_mode(struct mixer *mixer,
2172 uint32_t enforce_mode,
2173 bool enable)
2174{
2175 struct mixer_ctl *ctl = NULL;
2176 const char *mixer_ctl_name = "ASM Bit Width";
2177 uint32_t asm_bit_width_mode = 0;
2178
2179 if (enforce_mode == 0) {
2180 ALOGD("%s: DSP bitwidth feature is disabled.", __func__);
2181 return;
2182 }
2183
2184 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
2185 if (!ctl) {
2186 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2187 __func__, mixer_ctl_name);
2188 return;
2189 }
2190
2191 if (enable)
2192 asm_bit_width_mode = enforce_mode;
2193 else
2194 asm_bit_width_mode = 0;
2195
2196 ALOGV("%s DSP bit width feature status is %d width=%d",
2197 __func__, enable, asm_bit_width_mode);
2198 if (mixer_ctl_set_value(ctl, 0, asm_bit_width_mode) < 0)
2199 ALOGE("%s: Could not set ASM biwidth %d", __func__,
2200 asm_bit_width_mode);
2201
2202 return;
2203}
2204
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05302205/*
2206 * if native DSD playback active
2207 */
2208bool audio_is_dsd_native_stream_active(struct audio_device *adev)
2209{
2210 bool active = false;
2211 struct listnode *node = NULL;
2212 struct audio_usecase *uc = NULL;
2213 struct stream_out *curr_out = NULL;
2214
2215 list_for_each(node, &adev->usecase_list) {
2216 uc = node_to_item(node, struct audio_usecase, list);
2217 curr_out = (struct stream_out*) uc->stream.out;
2218
2219 if (curr_out && PCM_PLAYBACK == uc->type &&
2220 (DSD_NATIVE_BACKEND == platform_get_backend_index(uc->out_snd_device))) {
2221 active = true;
2222 ALOGV("%s:DSD playback is active", __func__);
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05302223 break;
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05302224 }
2225 }
2226 return active;
2227}
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302228
2229static bool force_device_switch(struct audio_usecase *usecase)
2230{
2231 bool ret = false;
2232 bool is_it_true_mode = false;
2233
Zhou Song30f2c3e2018-02-08 14:02:15 +08002234 if (usecase->type == PCM_CAPTURE ||
Surendar Karka93cd25a2018-08-28 14:21:37 +05302235 usecase->type == TRANSCODE_LOOPBACK_RX ||
2236 usecase->type == TRANSCODE_LOOPBACK_TX) {
Zhou Song30f2c3e2018-02-08 14:02:15 +08002237 return false;
2238 }
2239
Aalique Grahamecbc46a22017-10-05 10:30:23 -07002240 if(usecase->stream.out == NULL) {
2241 ALOGE("%s: stream.out is NULL", __func__);
2242 return false;
2243 }
2244
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302245 if (is_offload_usecase(usecase->id) &&
Xiaojun Sang869f2012016-02-23 16:33:07 +08002246 (usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) &&
2247 (usecase->stream.out->devices == AUDIO_DEVICE_OUT_WIRED_HEADSET ||
2248 usecase->stream.out->devices == AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) {
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302249 is_it_true_mode = (NATIVE_AUDIO_MODE_TRUE_44_1 == platform_get_native_support()? true : false);
2250 if ((is_it_true_mode && !adev->native_playback_enabled) ||
2251 (!is_it_true_mode && adev->native_playback_enabled)){
2252 ret = true;
2253 ALOGD("napb: time to toggle native mode");
2254 }
2255 }
2256
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302257 // Force all a2dp output devices to reconfigure for proper AFE encode format
Ashish Jainc597d102016-12-12 10:31:34 +05302258 //Also handle a case where in earlier a2dp start failed as A2DP stream was
2259 //in suspended state, hence try to trigger a retry when we again get a routing request.
Aalique Grahamecbc46a22017-10-05 10:30:23 -07002260 if((usecase->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) &&
Ashish Jainc597d102016-12-12 10:31:34 +05302261 audio_extn_a2dp_is_force_device_switch()) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302262 ALOGD("Force a2dp device switch to update new encoder config");
2263 ret = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002264 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302265
Florian Pfister1a84f312018-07-19 14:38:18 +02002266 if (usecase->stream.out->stream_config_changed) {
Manish Dewangan671a4202017-08-18 17:30:46 +05302267 ALOGD("Force stream_config_changed to update iec61937 transmission config");
2268 return true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002269 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302270 return ret;
2271}
2272
Aalique Grahame22e49102018-12-18 14:23:57 -08002273static void stream_app_type_cfg_init(struct stream_app_type_cfg *cfg)
2274{
2275 cfg->gain[0] = cfg->gain[1] = APP_TYPE_GAIN_DEFAULT;
2276}
2277
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302278bool is_btsco_device(snd_device_t out_snd_device, snd_device_t in_snd_device)
2279{
2280 bool ret=false;
2281 if ((out_snd_device == SND_DEVICE_OUT_BT_SCO ||
Zhou Song5657f492019-08-07 11:30:39 +08002282 out_snd_device == SND_DEVICE_OUT_BT_SCO_WB ||
2283 out_snd_device == SND_DEVICE_OUT_BT_SCO_SWB) ||
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302284 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_WB_NREC ||
2285 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_WB ||
Zhou Song5657f492019-08-07 11:30:39 +08002286 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_SWB ||
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302287 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_NREC ||
2288 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC)
2289 ret = true;
2290
2291 return ret;
2292}
2293
2294bool is_a2dp_device(snd_device_t out_snd_device)
2295{
2296 bool ret=false;
2297 if (out_snd_device == SND_DEVICE_OUT_BT_A2DP)
2298 ret = true;
2299
2300 return ret;
2301}
2302
2303bool is_bt_soc_on(struct audio_device *adev)
2304{
2305 struct mixer_ctl *ctl;
2306 char *mixer_ctl_name = "BT SOC status";
2307 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
2308 bool bt_soc_status = true;
2309 if (!ctl) {
2310 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2311 __func__, mixer_ctl_name);
2312 /*This is to ensure we dont break targets which dont have the kernel change*/
2313 return true;
2314 }
2315 bt_soc_status = mixer_ctl_get_value(ctl, 0);
2316 ALOGD("BT SOC status: %d",bt_soc_status);
2317 return bt_soc_status;
2318}
2319
Zhou Song331c8e52019-08-26 14:16:12 +08002320static int configure_btsco_sample_rate(snd_device_t snd_device)
2321{
2322 struct mixer_ctl *ctl = NULL;
2323 struct mixer_ctl *ctl_sr_rx = NULL, *ctl_sr_tx = NULL, *ctl_sr = NULL;
2324 char *rate_str = NULL;
2325 bool is_rx_dev = true;
2326
2327 if (is_btsco_device(snd_device, snd_device)) {
2328 ctl_sr_tx = mixer_get_ctl_by_name(adev->mixer, "BT SampleRate TX");
2329 ctl_sr_rx = mixer_get_ctl_by_name(adev->mixer, "BT SampleRate RX");
2330 if (!ctl_sr_tx || !ctl_sr_rx) {
2331 ctl_sr = mixer_get_ctl_by_name(adev->mixer, "BT SampleRate");
2332 if (!ctl_sr)
2333 return -ENOSYS;
2334 }
2335
2336 switch (snd_device) {
2337 case SND_DEVICE_OUT_BT_SCO:
2338 rate_str = "KHZ_8";
2339 break;
2340 case SND_DEVICE_IN_BT_SCO_MIC_NREC:
2341 case SND_DEVICE_IN_BT_SCO_MIC:
2342 rate_str = "KHZ_8";
2343 is_rx_dev = false;
2344 break;
2345 case SND_DEVICE_OUT_BT_SCO_WB:
2346 rate_str = "KHZ_16";
2347 break;
2348 case SND_DEVICE_IN_BT_SCO_MIC_WB_NREC:
2349 case SND_DEVICE_IN_BT_SCO_MIC_WB:
2350 rate_str = "KHZ_16";
2351 is_rx_dev = false;
2352 break;
2353 default:
2354 return 0;
2355 }
2356
2357 ctl = (ctl_sr == NULL) ? (is_rx_dev ? ctl_sr_rx : ctl_sr_tx) : ctl_sr;
2358 if (mixer_ctl_set_enum_by_string(ctl, rate_str) != 0)
2359 return -ENOSYS;
2360 }
2361 return 0;
2362}
2363
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302364int out_standby_l(struct audio_stream *stream);
2365
Eric Laurent637e2d42018-11-15 12:24:31 -08002366struct stream_in *adev_get_active_input(const struct audio_device *adev)
2367{
2368 struct listnode *node;
2369 struct stream_in *last_active_in = NULL;
2370
2371 /* Get last added active input.
2372 * TODO: We may use a priority mechanism to pick highest priority active source */
2373 list_for_each(node, &adev->usecase_list)
2374 {
2375 struct audio_usecase *usecase = node_to_item(node, struct audio_usecase, list);
2376 if (usecase->type == PCM_CAPTURE && usecase->stream.in != NULL)
2377 last_active_in = usecase->stream.in;
2378 }
2379
2380 return last_active_in;
2381}
2382
2383struct stream_in *get_voice_communication_input(const struct audio_device *adev)
2384{
2385 struct listnode *node;
2386
2387 /* First check active inputs with voice communication source and then
2388 * any input if audio mode is in communication */
2389 list_for_each(node, &adev->usecase_list)
2390 {
2391 struct audio_usecase *usecase = node_to_item(node, struct audio_usecase, list);
2392 if (usecase->type == PCM_CAPTURE && usecase->stream.in != NULL &&
2393 usecase->stream.in->source == AUDIO_SOURCE_VOICE_COMMUNICATION)
2394 return usecase->stream.in;
2395 }
2396 if (adev->mode == AUDIO_MODE_IN_COMMUNICATION)
2397 return adev_get_active_input(adev);
2398
2399 return NULL;
2400}
2401
Carter Hsu2e429db2019-05-14 18:50:52 +08002402/*
2403 * Aligned with policy.h
2404 */
2405static inline int source_priority(int inputSource)
2406{
2407 switch (inputSource) {
2408 case AUDIO_SOURCE_VOICE_COMMUNICATION:
2409 return 9;
2410 case AUDIO_SOURCE_CAMCORDER:
2411 return 8;
2412 case AUDIO_SOURCE_VOICE_PERFORMANCE:
2413 return 7;
2414 case AUDIO_SOURCE_UNPROCESSED:
2415 return 6;
2416 case AUDIO_SOURCE_MIC:
2417 return 5;
2418 case AUDIO_SOURCE_ECHO_REFERENCE:
2419 return 4;
2420 case AUDIO_SOURCE_FM_TUNER:
2421 return 3;
2422 case AUDIO_SOURCE_VOICE_RECOGNITION:
2423 return 2;
2424 case AUDIO_SOURCE_HOTWORD:
2425 return 1;
2426 default:
2427 break;
2428 }
2429 return 0;
2430}
2431
2432static struct stream_in *get_priority_input(struct audio_device *adev)
2433{
2434 struct listnode *node;
2435 struct audio_usecase *usecase;
2436 int last_priority = 0, priority;
2437 struct stream_in *priority_in = NULL;
2438 struct stream_in *in;
2439
2440 list_for_each(node, &adev->usecase_list) {
2441 usecase = node_to_item(node, struct audio_usecase, list);
2442 if (usecase->type == PCM_CAPTURE) {
2443 in = usecase->stream.in;
2444 if (!in)
2445 continue;
2446 priority = source_priority(in->source);
2447
2448 if (priority > last_priority) {
2449 last_priority = priority;
2450 priority_in = in;
2451 }
2452 }
2453 }
2454 return priority_in;
2455}
2456
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002457int select_devices(struct audio_device *adev, audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002458{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002459 snd_device_t out_snd_device = SND_DEVICE_NONE;
2460 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002461 struct audio_usecase *usecase = NULL;
2462 struct audio_usecase *vc_usecase = NULL;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002463 struct audio_usecase *voip_usecase = NULL;
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -08002464 struct audio_usecase *hfp_usecase = NULL;
Siddartha Shaik44dd7702017-06-14 12:13:25 +05302465 struct stream_out stream_out;
Vimal Puthanveed41fcff22014-01-23 15:56:53 -08002466 audio_usecase_t hfp_ucid;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002467 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002468
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302469 ALOGD("%s for use case (%s)", __func__, use_case_table[uc_id]);
2470
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002471 usecase = get_usecase_from_list(adev, uc_id);
2472 if (usecase == NULL) {
2473 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
2474 return -EINVAL;
2475 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002476
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002477 if ((usecase->type == VOICE_CALL) ||
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -08002478 (usecase->type == VOIP_CALL) ||
2479 (usecase->type == PCM_HFP_CALL)) {
Aditya Bavanaribdda2f22016-10-19 15:02:05 +05302480 if(usecase->stream.out == NULL) {
2481 ALOGE("%s: stream.out is NULL", __func__);
2482 return -EINVAL;
2483 }
Guodong Hu267bdf82019-08-12 19:22:32 +08002484 if (usecase->devices & AUDIO_DEVICE_OUT_BUS) {
2485 out_snd_device = audio_extn_auto_hal_get_output_snd_device(adev,
2486 uc_id);
2487 in_snd_device = audio_extn_auto_hal_get_input_snd_device(adev,
2488 uc_id);
2489 } else {
2490 out_snd_device = platform_get_output_snd_device(adev->platform,
2491 usecase->stream.out);
2492 in_snd_device = platform_get_input_snd_device(adev->platform,
2493 NULL,
2494 usecase->stream.out->devices);
2495 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002496 usecase->devices = usecase->stream.out->devices;
Surendar Karka93cd25a2018-08-28 14:21:37 +05302497 } else if (usecase->type == TRANSCODE_LOOPBACK_RX) {
Siddartha Shaik31b530e2017-05-19 15:26:33 +05302498 if (usecase->stream.inout == NULL) {
2499 ALOGE("%s: stream.inout is NULL", __func__);
2500 return -EINVAL;
2501 }
Siddartha Shaik44dd7702017-06-14 12:13:25 +05302502 stream_out.devices = usecase->stream.inout->out_config.devices;
2503 stream_out.sample_rate = usecase->stream.inout->out_config.sample_rate;
2504 stream_out.format = usecase->stream.inout->out_config.format;
2505 stream_out.channel_mask = usecase->stream.inout->out_config.channel_mask;
2506 out_snd_device = platform_get_output_snd_device(adev->platform,
2507 &stream_out);
Surendar Karka93cd25a2018-08-28 14:21:37 +05302508 usecase->devices = out_snd_device;
2509 } else if (usecase->type == TRANSCODE_LOOPBACK_TX ) {
2510 if (usecase->stream.inout == NULL) {
2511 ALOGE("%s: stream.inout is NULL", __func__);
2512 return -EINVAL;
2513 }
Eric Laurent637e2d42018-11-15 12:24:31 -08002514 in_snd_device = platform_get_input_snd_device(adev->platform, NULL, AUDIO_DEVICE_NONE);
Surendar Karka93cd25a2018-08-28 14:21:37 +05302515 usecase->devices = in_snd_device;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002516 } else {
2517 /*
2518 * If the voice call is active, use the sound devices of voice call usecase
2519 * so that it would not result any device switch. All the usecases will
2520 * be switched to new device when select_devices() is called for voice call
2521 * usecase. This is to avoid switching devices for voice call when
2522 * check_usecases_codec_backend() is called below.
Alexy Joseph79dfa3c2016-04-20 18:44:56 -07002523 * choose voice call device only if the use case device is
2524 * also using the codec backend
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002525 */
Shiv Maliyappanahallibb4cf0b2016-01-21 11:30:06 -08002526 if (voice_is_in_call(adev) && adev->mode != AUDIO_MODE_NORMAL) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002527 vc_usecase = get_usecase_from_list(adev,
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -08002528 get_usecase_id_from_usecase_type(adev, VOICE_CALL));
Alexy Josephdbcddf22016-05-16 17:54:09 -07002529 if ((vc_usecase) && (((vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) &&
2530 (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)) ||
Preetam Singh Ranawat097cb1f2016-07-19 13:49:04 +05302531 ((vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) &&
2532 (usecase->devices & AUDIO_DEVICE_IN_ALL_CODEC_BACKEND)) ||
juyuchen2d415992018-11-16 14:15:16 +08002533 (vc_usecase->devices == AUDIO_DEVICE_OUT_HEARING_AID) ||
2534 (usecase->devices == AUDIO_DEVICE_IN_VOICE_CALL))) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002535 in_snd_device = vc_usecase->in_snd_device;
2536 out_snd_device = vc_usecase->out_snd_device;
2537 }
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002538 } else if (voice_extn_compress_voip_is_active(adev)) {
yidongh02ef86f2017-04-21 15:36:04 +08002539 bool out_snd_device_backend_match = true;
yidongh47785a82017-05-08 19:29:29 +08002540 voip_usecase = get_usecase_from_list(adev, USECASE_COMPRESS_VOIP_CALL);
yidongh6261d8e2017-05-15 17:04:02 +08002541 if ((voip_usecase != NULL) &&
2542 (usecase->type == PCM_PLAYBACK) &&
2543 (usecase->stream.out != NULL)) {
yidongh02ef86f2017-04-21 15:36:04 +08002544 out_snd_device_backend_match = platform_check_backends_match(
2545 voip_usecase->out_snd_device,
2546 platform_get_output_snd_device(
2547 adev->platform,
2548 usecase->stream.out));
2549 }
Mingming Yin2d8aa2e2014-08-14 00:00:51 -07002550 if ((voip_usecase) && ((voip_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) &&
Zhou Song0154bf12016-08-04 10:48:16 +08002551 ((usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) ||
2552 ((usecase->devices & ~AUDIO_DEVICE_BIT_IN) & AUDIO_DEVICE_IN_ALL_CODEC_BACKEND)) &&
yidongh02ef86f2017-04-21 15:36:04 +08002553 out_snd_device_backend_match &&
Mingming Yin2d8aa2e2014-08-14 00:00:51 -07002554 (voip_usecase->stream.out != adev->primary_output))) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002555 in_snd_device = voip_usecase->in_snd_device;
2556 out_snd_device = voip_usecase->out_snd_device;
2557 }
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -08002558 } else if (audio_extn_hfp_is_active(adev)) {
Vimal Puthanveed41fcff22014-01-23 15:56:53 -08002559 hfp_ucid = audio_extn_hfp_get_usecase();
2560 hfp_usecase = get_usecase_from_list(adev, hfp_ucid);
Mingming Yin2d8aa2e2014-08-14 00:00:51 -07002561 if ((hfp_usecase) && (hfp_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)) {
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -08002562 in_snd_device = hfp_usecase->in_snd_device;
2563 out_snd_device = hfp_usecase->out_snd_device;
2564 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002565 }
2566 if (usecase->type == PCM_PLAYBACK) {
Aditya Bavanaribdda2f22016-10-19 15:02:05 +05302567 if (usecase->stream.out == NULL) {
2568 ALOGE("%s: stream.out is NULL", __func__);
2569 return -EINVAL;
2570 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002571 usecase->devices = usecase->stream.out->devices;
2572 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07002573 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurent637e2d42018-11-15 12:24:31 -08002574 struct stream_out *voip_out = adev->primary_output;
2575 struct stream_in *voip_in = get_voice_communication_input(adev);
Guodong Hu267bdf82019-08-12 19:22:32 +08002576 if (usecase->devices & AUDIO_DEVICE_OUT_BUS)
2577 out_snd_device = audio_extn_auto_hal_get_output_snd_device(adev, uc_id);
2578 else
2579 out_snd_device = platform_get_output_snd_device(adev->platform,
2580 usecase->stream.out);
kunleizdcf967a2018-08-07 17:09:11 +08002581 voip_usecase = get_usecase_from_list(adev, USECASE_AUDIO_PLAYBACK_VOIP);
kunleizdcf967a2018-08-07 17:09:11 +08002582
Eric Laurent637e2d42018-11-15 12:24:31 -08002583 if (voip_usecase)
2584 voip_out = voip_usecase->stream.out;
2585
2586 if (usecase->stream.out == voip_out && voip_in != NULL)
2587 select_devices(adev, voip_in->usecase);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07002588 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002589 } else if (usecase->type == PCM_CAPTURE) {
Aditya Bavanaribdda2f22016-10-19 15:02:05 +05302590 if (usecase->stream.in == NULL) {
2591 ALOGE("%s: stream.in is NULL", __func__);
2592 return -EINVAL;
2593 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002594 usecase->devices = usecase->stream.in->device;
2595 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07002596 if (in_snd_device == SND_DEVICE_NONE) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07002597 audio_devices_t out_device = AUDIO_DEVICE_NONE;
Eric Laurent637e2d42018-11-15 12:24:31 -08002598 struct stream_in *voip_in = get_voice_communication_input(adev);
Carter Hsu2e429db2019-05-14 18:50:52 +08002599 struct stream_in *priority_in = NULL;
Eric Laurent637e2d42018-11-15 12:24:31 -08002600
2601 if (voip_in != NULL) {
Eric Laurent637e2d42018-11-15 12:24:31 -08002602 struct audio_usecase *voip_usecase = get_usecase_from_list(adev,
2603 USECASE_AUDIO_PLAYBACK_VOIP);
2604
Carter Hsu2e429db2019-05-14 18:50:52 +08002605 usecase->stream.in->enable_ec_port = false;
2606
Eric Laurent637e2d42018-11-15 12:24:31 -08002607 if (usecase->id == USECASE_AUDIO_RECORD_AFE_PROXY) {
2608 out_device = AUDIO_DEVICE_OUT_TELEPHONY_TX;
2609 } else if (voip_usecase) {
2610 out_device = voip_usecase->stream.out->devices;
2611 } else if (adev->primary_output &&
2612 !adev->primary_output->standby) {
2613 out_device = adev->primary_output->devices;
2614 } else {
2615 /* forcing speaker o/p device to get matching i/p pair
2616 in case o/p is not routed from same primary HAL */
2617 out_device = AUDIO_DEVICE_OUT_SPEAKER;
2618 }
Carter Hsu2e429db2019-05-14 18:50:52 +08002619 priority_in = voip_in;
2620 } else {
2621 /* get the input with the highest priority source*/
2622 priority_in = get_priority_input(adev);
2623
2624 if (!priority_in)
2625 priority_in = usecase->stream.in;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07002626 }
Carter Hsu2e429db2019-05-14 18:50:52 +08002627
Eric Laurent637e2d42018-11-15 12:24:31 -08002628 in_snd_device = platform_get_input_snd_device(adev->platform,
Carter Hsu2e429db2019-05-14 18:50:52 +08002629 priority_in,
Eric Laurent637e2d42018-11-15 12:24:31 -08002630 out_device);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07002631 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002632 }
2633 }
2634
2635 if (out_snd_device == usecase->out_snd_device &&
2636 in_snd_device == usecase->in_snd_device) {
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302637
2638 if (!force_device_switch(usecase))
2639 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002640 }
2641
Guodong Hu267bdf82019-08-12 19:22:32 +08002642 if (!(usecase->devices & AUDIO_DEVICE_OUT_BUS) &&
2643 ((is_btsco_device(out_snd_device,in_snd_device) && !adev->bt_sco_on) ||
2644 (is_a2dp_device(out_snd_device) && !audio_extn_a2dp_source_is_ready()))) {
2645 ALOGD("SCO/A2DP is selected but they are not connected/ready hence dont route");
2646 return 0;
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302647 }
2648
Aalique Grahame22e49102018-12-18 14:23:57 -08002649 if (out_snd_device != SND_DEVICE_NONE &&
2650 out_snd_device != adev->last_logged_snd_device[uc_id][0]) {
2651 ALOGD("%s: changing use case %s output device from(%d: %s, acdb %d) to (%d: %s, acdb %d)",
2652 __func__,
2653 use_case_table[uc_id],
2654 adev->last_logged_snd_device[uc_id][0],
2655 platform_get_snd_device_name(adev->last_logged_snd_device[uc_id][0]),
2656 adev->last_logged_snd_device[uc_id][0] != SND_DEVICE_NONE ?
2657 platform_get_snd_device_acdb_id(adev->last_logged_snd_device[uc_id][0]) :
2658 -1,
2659 out_snd_device,
2660 platform_get_snd_device_name(out_snd_device),
2661 platform_get_snd_device_acdb_id(out_snd_device));
2662 adev->last_logged_snd_device[uc_id][0] = out_snd_device;
2663 }
2664 if (in_snd_device != SND_DEVICE_NONE &&
2665 in_snd_device != adev->last_logged_snd_device[uc_id][1]) {
2666 ALOGD("%s: changing use case %s input device from(%d: %s, acdb %d) to (%d: %s, acdb %d)",
2667 __func__,
2668 use_case_table[uc_id],
2669 adev->last_logged_snd_device[uc_id][1],
2670 platform_get_snd_device_name(adev->last_logged_snd_device[uc_id][1]),
2671 adev->last_logged_snd_device[uc_id][1] != SND_DEVICE_NONE ?
2672 platform_get_snd_device_acdb_id(adev->last_logged_snd_device[uc_id][1]) :
2673 -1,
2674 in_snd_device,
2675 platform_get_snd_device_name(in_snd_device),
2676 platform_get_snd_device_acdb_id(in_snd_device));
2677 adev->last_logged_snd_device[uc_id][1] = in_snd_device;
2678 }
2679
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002680
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002681 /*
2682 * Limitation: While in call, to do a device switch we need to disable
2683 * and enable both RX and TX devices though one of them is same as current
2684 * device.
2685 */
Vidyakumar Athota21b3bb92014-04-25 11:08:08 -07002686 if ((usecase->type == VOICE_CALL) &&
2687 (usecase->in_snd_device != SND_DEVICE_NONE) &&
2688 (usecase->out_snd_device != SND_DEVICE_NONE)) {
Eric Laurentb23d5282013-05-14 15:27:20 -07002689 status = platform_switch_voice_call_device_pre(adev->platform);
Narsinga Rao Chella116142b2015-08-14 18:00:08 -07002690 }
2691
2692 if (((usecase->type == VOICE_CALL) ||
2693 (usecase->type == VOIP_CALL)) &&
2694 (usecase->out_snd_device != SND_DEVICE_NONE)) {
2695 /* Disable sidetone only if voice/voip call already exists */
2696 if (voice_is_call_state_active(adev) ||
2697 voice_extn_compress_voip_is_started(adev))
Bhalchandra Gajare45fee282015-06-09 22:23:45 -07002698 voice_set_sidetone(adev, usecase->out_snd_device, false);
Vidyakumar Athotaea269c62016-10-31 09:05:59 -07002699
2700 /* Disable aanc only if voice call exists */
2701 if (voice_is_call_state_active(adev))
2702 voice_check_and_update_aanc_path(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002703 }
2704
Aalique Grahame22e49102018-12-18 14:23:57 -08002705 if ((out_snd_device == SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP ||
2706 out_snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP) &&
Florian Pfister1a84f312018-07-19 14:38:18 +02002707 (!audio_extn_a2dp_source_is_ready())) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302708 ALOGW("%s: A2DP profile is not ready, routing to speaker only", __func__);
Aalique Grahame22e49102018-12-18 14:23:57 -08002709 if (out_snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP)
2710 out_snd_device = SND_DEVICE_OUT_SPEAKER_SAFE;
2711 else
2712 out_snd_device = SND_DEVICE_OUT_SPEAKER;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302713 }
2714
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002715 /* Disable current sound devices */
2716 if (usecase->out_snd_device != SND_DEVICE_NONE) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07002717 disable_audio_route(adev, usecase);
2718 disable_snd_device(adev, usecase->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002719 }
2720
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002721 if (usecase->in_snd_device != SND_DEVICE_NONE) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07002722 disable_audio_route(adev, usecase);
2723 disable_snd_device(adev, usecase->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002724 }
2725
Vidyakumar Athota545dbd32013-11-13 17:30:53 -08002726 /* Applicable only on the targets that has external modem.
2727 * New device information should be sent to modem before enabling
2728 * the devices to reduce in-call device switch time.
2729 */
Vidyakumar Athota21b3bb92014-04-25 11:08:08 -07002730 if ((usecase->type == VOICE_CALL) &&
2731 (usecase->in_snd_device != SND_DEVICE_NONE) &&
2732 (usecase->out_snd_device != SND_DEVICE_NONE)) {
Vidyakumar Athota545dbd32013-11-13 17:30:53 -08002733 status = platform_switch_voice_call_enable_device_config(adev->platform,
2734 out_snd_device,
2735 in_snd_device);
Vidyakumar Athota21b3bb92014-04-25 11:08:08 -07002736 }
Vidyakumar Athota545dbd32013-11-13 17:30:53 -08002737
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002738 /* Enable new sound devices */
2739 if (out_snd_device != SND_DEVICE_NONE) {
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08002740 check_usecases_codec_backend(adev, usecase, out_snd_device);
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +05302741 if (platform_check_codec_asrc_support(adev->platform))
2742 check_and_set_asrc_mode(adev, usecase, out_snd_device);
Haynes Mathew George1376ca62014-04-24 11:55:48 -07002743 enable_snd_device(adev, out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002744 }
2745
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002746 if (in_snd_device != SND_DEVICE_NONE) {
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05302747 check_usecases_capture_codec_backend(adev, usecase, in_snd_device);
Haynes Mathew George1376ca62014-04-24 11:55:48 -07002748 enable_snd_device(adev, in_snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002749 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002750
Avinash Vaish71a8b972014-07-24 15:36:33 +05302751 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) {
Eric Laurentb23d5282013-05-14 15:27:20 -07002752 status = platform_switch_voice_call_device_post(adev->platform,
2753 out_snd_device,
2754 in_snd_device);
Avinash Vaish71a8b972014-07-24 15:36:33 +05302755 enable_audio_route_for_voice_usecases(adev, usecase);
2756 }
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002757
sangwoo170731f2013-06-08 15:36:36 +09002758 usecase->in_snd_device = in_snd_device;
2759 usecase->out_snd_device = out_snd_device;
2760
Dhananjay Kumard6d32152016-10-13 16:11:03 +05302761 audio_extn_utils_update_stream_app_type_cfg_for_usecase(adev,
2762 usecase);
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +05302763 if (usecase->type == PCM_PLAYBACK) {
Weiyin Jiang6f4c8062016-11-23 15:30:29 +08002764 if ((24 == usecase->stream.out->bit_width) &&
2765 (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER)) {
2766 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
2767 } else if ((out_snd_device == SND_DEVICE_OUT_HDMI ||
2768 out_snd_device == SND_DEVICE_OUT_USB_HEADSET ||
2769 out_snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
2770 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
2771 /*
2772 * To best utlize DSP, check if the stream sample rate is supported/multiple of
2773 * configured device sample rate, if not update the COPP rate to be equal to the
2774 * device sample rate, else open COPP at stream sample rate
2775 */
2776 platform_check_and_update_copp_sample_rate(adev->platform, out_snd_device,
2777 usecase->stream.out->sample_rate,
2778 &usecase->stream.out->app_type_cfg.sample_rate);
Ashish Jain4826f6c2017-02-06 13:33:20 +05302779 } else if (((out_snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
Preetam Singh Ranawat590d0432019-09-30 14:39:47 +05302780 out_snd_device != SND_DEVICE_OUT_HEADPHONES &&
2781 out_snd_device != SND_DEVICE_OUT_HEADPHONES_HIFI_FILTER &&
Ashish Jain4826f6c2017-02-06 13:33:20 +05302782 !audio_is_true_native_stream_active(adev)) &&
Weiyin Jiang6f4c8062016-11-23 15:30:29 +08002783 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
2784 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
2785 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
2786 }
Weiyin Jiangcdece202019-07-08 16:13:16 +08002787 }
2788 enable_audio_route(adev, usecase);
Weiyin Jiang6f4c8062016-11-23 15:30:29 +08002789
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002790 audio_extn_qdsp_set_device(usecase);
Aalique Grahame22e49102018-12-18 14:23:57 -08002791
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07002792 /* If input stream is already running then effect needs to be
2793 applied on the new input device that's being enabled here. */
Eric Laurent637e2d42018-11-15 12:24:31 -08002794 if (in_snd_device != SND_DEVICE_NONE)
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07002795 check_and_enable_effect(adev);
2796
Vidyakumar Athota493f2892016-08-14 11:56:55 -07002797 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) {
Vidyakumar Athotaea269c62016-10-31 09:05:59 -07002798 /* Enable aanc only if voice call exists */
2799 if (voice_is_call_state_active(adev))
2800 voice_check_and_update_aanc_path(adev, out_snd_device, true);
2801
Vidyakumar Athota493f2892016-08-14 11:56:55 -07002802 /* Enable sidetone only if other voice/voip call already exists */
2803 if (voice_is_call_state_active(adev) ||
2804 voice_extn_compress_voip_is_started(adev))
2805 voice_set_sidetone(adev, out_snd_device, true);
2806 }
2807
Vidyakumar Athota1fd21792013-11-15 14:50:57 -08002808 /* Applicable only on the targets that has external modem.
2809 * Enable device command should be sent to modem only after
2810 * enabling voice call mixer controls
2811 */
Vidyakumar Athota339342f2014-07-01 15:30:57 -07002812 if (usecase->type == VOICE_CALL)
Vidyakumar Athota1fd21792013-11-15 14:50:57 -08002813 status = platform_switch_voice_call_usecase_route_post(adev->platform,
2814 out_snd_device,
2815 in_snd_device);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302816
2817 if (is_btsco_device(out_snd_device, in_snd_device) || is_a2dp_device(out_snd_device)) {
Eric Laurent637e2d42018-11-15 12:24:31 -08002818 struct stream_in *in = adev_get_active_input(adev);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302819 if (usecase->type == VOIP_CALL) {
Eric Laurent637e2d42018-11-15 12:24:31 -08002820 if (in != NULL && !in->standby) {
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302821 if (is_bt_soc_on(adev) == false){
2822 ALOGD("BT SCO MIC disconnected while in connection");
Eric Laurent637e2d42018-11-15 12:24:31 -08002823 if (in->pcm != NULL)
2824 pcm_stop(in->pcm);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302825 }
2826 }
2827 if ((usecase->stream.out != NULL) && (usecase->stream.out != adev->primary_output)
2828 && usecase->stream.out->started) {
2829 if (is_bt_soc_on(adev) == false) {
2830 ALOGD("BT SCO/A2DP disconnected while in connection");
2831 out_standby_l(&usecase->stream.out->stream.common);
2832 }
2833 }
2834 } else if ((usecase->stream.out != NULL) &&
2835 !(usecase->stream.out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
Surendar Karkae1dc8742018-11-19 16:23:14 +05302836 (usecase->type != TRANSCODE_LOOPBACK_TX) &&
2837 (usecase->type != TRANSCODE_LOOPBACK_RX) &&
Weiyin Jiang0d373242019-07-25 13:18:17 +08002838 (usecase->type != PCM_CAPTURE) &&
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302839 usecase->stream.out->started) {
2840 if (is_bt_soc_on(adev) == false) {
2841 ALOGD("BT SCO/A2dp disconnected while in connection");
2842 out_standby_l(&usecase->stream.out->stream.common);
2843 }
2844 }
2845 }
2846
Yung Ti Su70cb8242018-06-22 17:38:47 +08002847 if (usecase->type != PCM_CAPTURE && usecase == voip_usecase) {
Aalique Grahame22e49102018-12-18 14:23:57 -08002848 struct stream_out *voip_out = voip_usecase->stream.out;
2849 audio_extn_utils_send_app_type_gain(adev,
2850 voip_out->app_type_cfg.app_type,
2851 &voip_out->app_type_cfg.gain[0]);
2852 }
2853
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302854 ALOGD("%s: done",__func__);
2855
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002856 return status;
2857}
2858
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002859static int stop_input_stream(struct stream_in *in)
2860{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05302861 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002862 struct audio_usecase *uc_info;
Pallavid7c7a272018-01-16 11:22:55 +05302863
2864 if (in == NULL) {
2865 ALOGE("%s: stream_in ptr is NULL", __func__);
2866 return -EINVAL;
2867 }
2868
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002869 struct audio_device *adev = in->dev;
Carter Hsu2e429db2019-05-14 18:50:52 +08002870 struct stream_in *priority_in = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002871
Eric Laurent994a6932013-07-17 11:51:42 -07002872 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002873 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002874 uc_info = get_usecase_from_list(adev, in->usecase);
2875 if (uc_info == NULL) {
2876 ALOGE("%s: Could not find the usecase (%d) in the list",
2877 __func__, in->usecase);
2878 return -EINVAL;
2879 }
2880
Carter Hsu2e429db2019-05-14 18:50:52 +08002881 priority_in = get_priority_input(adev);
2882
Derek Chenea197282019-01-07 17:35:01 -08002883 if (audio_extn_ext_hw_plugin_usecase_stop(adev->ext_hw_plugin, uc_info))
2884 ALOGE("%s: failed to stop ext hw plugin", __func__);
Derek Chend2530072014-11-24 12:39:14 -08002885
Vidyakumar Athota2850d532013-11-19 16:02:12 -08002886 /* Close in-call recording streams */
2887 voice_check_and_stop_incall_rec_usecase(adev, in);
2888
Eric Laurent150dbfe2013-02-27 14:31:02 -08002889 /* 1. Disable stream specific mixer controls */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07002890 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002891
2892 /* 2. Disable the tx device */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07002893 disable_snd_device(adev, uc_info->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002894
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05302895 if (is_loopback_input_device(in->device))
2896 audio_extn_keep_alive_stop(KEEP_ALIVE_OUT_PRIMARY);
2897
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002898 list_remove(&uc_info->list);
2899 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002900
Carter Hsu2e429db2019-05-14 18:50:52 +08002901 if (priority_in == in) {
2902 priority_in = get_priority_input(adev);
2903 if (priority_in)
2904 select_devices(adev, priority_in->usecase);
2905 }
2906
Vatsal Buchac09ae062018-11-14 13:25:08 +05302907 enable_gcov();
Eric Laurent994a6932013-07-17 11:51:42 -07002908 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002909 return ret;
2910}
2911
2912int start_input_stream(struct stream_in *in)
2913{
2914 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -08002915 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002916 struct audio_usecase *uc_info;
Preetam Singh Ranawata87e9742018-02-13 16:52:53 +05302917
2918 if (in == NULL) {
2919 ALOGE("%s: stream_in ptr is NULL", __func__);
2920 return -EINVAL;
2921 }
2922
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002923 struct audio_device *adev = in->dev;
Garmond Leunge2433c32017-09-28 21:51:22 -07002924 struct pcm_config config = in->config;
Garmond Leung438932f2017-10-04 19:35:18 -07002925 int usecase = platform_update_usecase_from_source(in->source,in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002926
Mingming Yin2664a5b2015-09-03 10:53:11 -07002927 if (get_usecase_from_list(adev, usecase) == NULL)
2928 in->usecase = usecase;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302929 ALOGD("%s: enter: stream(%p)usecase(%d: %s)",
2930 __func__, &in->stream, in->usecase, use_case_table[in->usecase]);
Shiv Maliyappanahallida107642013-10-17 11:16:13 -07002931
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05302932 if (CARD_STATUS_OFFLINE == in->card_status||
2933 CARD_STATUS_OFFLINE == adev->card_status) {
2934 ALOGW("in->card_status or adev->card_status offline, try again");
Dhanalakshmi Siddani4d57e992014-07-17 16:37:51 +05302935 ret = -EIO;
Naresh Tanniru4c630392014-05-12 01:05:52 +05302936 goto error_config;
2937 }
Naresh Tanniru4c630392014-05-12 01:05:52 +05302938
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302939 if (audio_is_bluetooth_sco_device(in->device)) {
2940 if (!adev->bt_sco_on) {
2941 ALOGE("%s: SCO profile is not ready, return error", __func__);
2942 ret = -EIO;
2943 goto error_config;
2944 }
2945 }
2946
Shiv Maliyappanahallida107642013-10-17 11:16:13 -07002947 /* Check if source matches incall recording usecase criteria */
2948 ret = voice_check_and_set_incall_rec_usecase(adev, in);
2949 if (ret)
2950 goto error_config;
2951 else
Mingming Yin2664a5b2015-09-03 10:53:11 -07002952 ALOGV("%s: usecase(%d)", __func__, in->usecase);
2953
2954 if (get_usecase_from_list(adev, in->usecase) != NULL) {
2955 ALOGE("%s: use case assigned already in use, stream(%p)usecase(%d: %s)",
2956 __func__, &in->stream, in->usecase, use_case_table[in->usecase]);
Zhou Song4e0704d2016-04-20 13:00:14 +08002957 return -EINVAL;
Mingming Yin2664a5b2015-09-03 10:53:11 -07002958 }
Shiv Maliyappanahallida107642013-10-17 11:16:13 -07002959
Eric Laurentb23d5282013-05-14 15:27:20 -07002960 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002961 if (in->pcm_device_id < 0) {
2962 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
2963 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -08002964 ret = -EINVAL;
2965 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002966 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002967
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002968 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07002969
2970 if (!uc_info) {
2971 ret = -ENOMEM;
2972 goto error_config;
2973 }
2974
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002975 uc_info->id = in->usecase;
2976 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002977 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002978 uc_info->devices = in->device;
2979 uc_info->in_snd_device = SND_DEVICE_NONE;
2980 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002981
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002982 list_add_tail(&adev->usecase_list, &uc_info->list);
Wei Wangf7ca6c92017-11-21 14:51:20 -08002983 audio_streaming_hint_start();
Sudheer Papothifa9d2282015-09-17 01:53:25 +05302984 audio_extn_perf_lock_acquire(&adev->perf_lock_handle, 0,
2985 adev->perf_lock_opts,
2986 adev->perf_lock_opts_size);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002987 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002988
Derek Chenea197282019-01-07 17:35:01 -08002989 if (audio_extn_ext_hw_plugin_usecase_start(adev->ext_hw_plugin, uc_info))
2990 ALOGE("%s: failed to start ext hw plugin", __func__);
Derek Chend2530072014-11-24 12:39:14 -08002991
Deeraj Soman30cc7ae2019-03-18 16:26:55 +05302992 android_atomic_acquire_cas(true, false, &(in->capture_stopped));
2993
Haynes Mathew George16081042017-05-31 17:16:49 -07002994 if (audio_extn_cin_attached_usecase(in->usecase)) {
Manish Dewangan46e07982018-12-13 18:18:59 +05302995 ret = audio_extn_cin_open_input_stream(in);
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05302996 if (ret)
2997 goto error_open;
2998 else
2999 goto done_open;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003000 }
3001
Haynes Mathew George16081042017-05-31 17:16:49 -07003002 if (in->usecase == USECASE_AUDIO_RECORD_MMAP) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003003 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) {
Haynes Mathew George16081042017-05-31 17:16:49 -07003004 ALOGE("%s: pcm stream not ready", __func__);
3005 goto error_open;
3006 }
3007 ret = pcm_start(in->pcm);
3008 if (ret < 0) {
3009 ALOGE("%s: MMAP pcm_start failed ret %d", __func__, ret);
3010 goto error_open;
3011 }
3012 } else {
3013 unsigned int flags = PCM_IN | PCM_MONOTONIC;
3014 unsigned int pcm_open_retry_count = 0;
3015
3016 if (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY) {
3017 flags |= PCM_MMAP | PCM_NOIRQ;
3018 pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT;
3019 } else if (in->realtime) {
3020 flags |= PCM_MMAP | PCM_NOIRQ;
3021 }
3022
Garmond Leunge2433c32017-09-28 21:51:22 -07003023 if (audio_extn_ffv_get_stream() == in) {
3024 ALOGD("%s: ffv stream, update pcm config", __func__);
3025 audio_extn_ffv_update_pcm_config(&config);
3026 }
Haynes Mathew George16081042017-05-31 17:16:49 -07003027 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
3028 __func__, adev->snd_card, in->pcm_device_id, in->config.channels);
3029
3030 while (1) {
Haynes Mathew George380745d2017-10-04 15:27:45 -07003031 ATRACE_BEGIN("pcm_in_open");
Haynes Mathew George16081042017-05-31 17:16:49 -07003032 in->pcm = pcm_open(adev->snd_card, in->pcm_device_id,
Garmond Leung438932f2017-10-04 19:35:18 -07003033 flags, &config);
Haynes Mathew George380745d2017-10-04 15:27:45 -07003034 ATRACE_END();
Satish Babu Patakokila54ce83d2018-07-06 18:00:37 +05303035 if (errno == ENETRESET && !pcm_is_ready(in->pcm)) {
Sharad Sanglec6f32552018-05-04 16:15:38 +05303036 ALOGE("%s: pcm_open failed errno:%d\n", __func__, errno);
3037 adev->card_status = CARD_STATUS_OFFLINE;
3038 in->card_status = CARD_STATUS_OFFLINE;
3039 ret = -EIO;
3040 goto error_open;
3041 }
3042
Haynes Mathew George16081042017-05-31 17:16:49 -07003043 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) {
3044 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
3045 if (in->pcm != NULL) {
3046 pcm_close(in->pcm);
3047 in->pcm = NULL;
3048 }
Weiyin Jiang72197252019-10-09 11:49:32 +08003049 if (pcm_open_retry_count == 0) {
Haynes Mathew George16081042017-05-31 17:16:49 -07003050 ret = -EIO;
3051 goto error_open;
3052 }
Weiyin Jiang72197252019-10-09 11:49:32 +08003053 pcm_open_retry_count--;
Haynes Mathew George16081042017-05-31 17:16:49 -07003054 usleep(PROXY_OPEN_WAIT_TIME * 1000);
3055 continue;
3056 }
3057 break;
3058 }
3059
3060 ALOGV("%s: pcm_prepare", __func__);
Haynes Mathew George380745d2017-10-04 15:27:45 -07003061 ATRACE_BEGIN("pcm_in_prepare");
Haynes Mathew George16081042017-05-31 17:16:49 -07003062 ret = pcm_prepare(in->pcm);
Haynes Mathew George380745d2017-10-04 15:27:45 -07003063 ATRACE_END();
Haynes Mathew George16081042017-05-31 17:16:49 -07003064 if (ret < 0) {
3065 ALOGE("%s: pcm_prepare returned %d", __func__, ret);
3066 pcm_close(in->pcm);
3067 in->pcm = NULL;
3068 goto error_open;
3069 }
3070 register_in_stream(in);
3071 if (in->realtime) {
Haynes Mathew George380745d2017-10-04 15:27:45 -07003072 ATRACE_BEGIN("pcm_in_start");
Haynes Mathew George16081042017-05-31 17:16:49 -07003073 ret = pcm_start(in->pcm);
Haynes Mathew George380745d2017-10-04 15:27:45 -07003074 ATRACE_END();
Haynes Mathew George16081042017-05-31 17:16:49 -07003075 if (ret < 0) {
3076 ALOGE("%s: RT pcm_start failed ret %d", __func__, ret);
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003077 pcm_close(in->pcm);
3078 in->pcm = NULL;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003079 goto error_open;
3080 }
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003081 }
Haynes Mathew George5beddd42016-06-27 18:33:40 -07003082 }
3083
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07003084 check_and_enable_effect(adev);
justinweng20fb6d82019-02-21 18:49:00 -07003085 audio_extn_audiozoom_set_microphone_direction(in, in->zoom);
3086 audio_extn_audiozoom_set_microphone_field_dimension(in, in->direction);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07003087
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05303088 if (is_loopback_input_device(in->device))
3089 audio_extn_keep_alive_start(KEEP_ALIVE_OUT_PRIMARY);
3090
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05303091done_open:
Wei Wangf7ca6c92017-11-21 14:51:20 -08003092 audio_streaming_hint_end();
Sudheer Papothifa9d2282015-09-17 01:53:25 +05303093 audio_extn_perf_lock_release(&adev->perf_lock_handle);
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07003094 ALOGD("%s: exit", __func__);
Vatsal Buchac09ae062018-11-14 13:25:08 +05303095 enable_gcov();
Eric Laurentc8400632013-02-14 19:04:54 -08003096 return ret;
3097
3098error_open:
Wei Wangf7ca6c92017-11-21 14:51:20 -08003099 audio_streaming_hint_end();
Sudheer Papothifa9d2282015-09-17 01:53:25 +05303100 audio_extn_perf_lock_release(&adev->perf_lock_handle);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003101 stop_input_stream(in);
Wei Wangf7ca6c92017-11-21 14:51:20 -08003102
Eric Laurentc8400632013-02-14 19:04:54 -08003103error_config:
Laxminath Kasam2cb4b752015-09-24 03:59:15 +05303104 /*
3105 * sleep 50ms to allow sufficient time for kernel
3106 * drivers to recover incases like SSR.
3107 */
3108 usleep(50000);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003109 ALOGD("%s: exit: status(%d)", __func__, ret);
Vatsal Buchac09ae062018-11-14 13:25:08 +05303110 enable_gcov();
Eric Laurentc8400632013-02-14 19:04:54 -08003111 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003112}
3113
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07003114void lock_input_stream(struct stream_in *in)
3115{
3116 pthread_mutex_lock(&in->pre_lock);
3117 pthread_mutex_lock(&in->lock);
3118 pthread_mutex_unlock(&in->pre_lock);
3119}
3120
3121void lock_output_stream(struct stream_out *out)
3122{
3123 pthread_mutex_lock(&out->pre_lock);
3124 pthread_mutex_lock(&out->lock);
3125 pthread_mutex_unlock(&out->pre_lock);
3126}
3127
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003128/* must be called with out->lock locked */
3129static int send_offload_cmd_l(struct stream_out* out, int command)
3130{
3131 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
3132
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07003133 if (!cmd) {
3134 ALOGE("failed to allocate mem for command 0x%x", command);
3135 return -ENOMEM;
3136 }
3137
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003138 ALOGVV("%s %d", __func__, command);
3139
3140 cmd->cmd = command;
3141 list_add_tail(&out->offload_cmd_list, &cmd->node);
3142 pthread_cond_signal(&out->offload_cond);
3143 return 0;
3144}
3145
3146/* must be called iwth out->lock locked */
3147static void stop_compressed_output_l(struct stream_out *out)
3148{
3149 out->offload_state = OFFLOAD_STATE_IDLE;
3150 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07003151 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003152 if (out->compr != NULL) {
3153 compress_stop(out->compr);
3154 while (out->offload_thread_blocked) {
3155 pthread_cond_wait(&out->cond, &out->lock);
3156 }
3157 }
3158}
3159
Varun Balaraje49253e2017-07-06 19:48:56 +05303160bool is_interactive_usecase(audio_usecase_t uc_id)
3161{
3162 unsigned int i;
3163 for (i = 0; i < sizeof(interactive_usecases)/sizeof(interactive_usecases[0]); i++) {
3164 if (uc_id == interactive_usecases[i])
3165 return true;
3166 }
3167 return false;
3168}
3169
3170static audio_usecase_t get_interactive_usecase(struct audio_device *adev)
3171{
3172 audio_usecase_t ret_uc = USECASE_INVALID;
3173 unsigned int intract_uc_index;
3174 unsigned int num_usecase = sizeof(interactive_usecases)/sizeof(interactive_usecases[0]);
3175
3176 ALOGV("%s: num_usecase: %d", __func__, num_usecase);
3177 for (intract_uc_index = 0; intract_uc_index < num_usecase; intract_uc_index++) {
3178 if (!(adev->interactive_usecase_state & (0x1 << intract_uc_index))) {
3179 adev->interactive_usecase_state |= 0x1 << intract_uc_index;
3180 ret_uc = interactive_usecases[intract_uc_index];
3181 break;
3182 }
3183 }
3184
3185 ALOGV("%s: Interactive usecase is %d", __func__, ret_uc);
3186 return ret_uc;
3187}
3188
3189static void free_interactive_usecase(struct audio_device *adev,
3190 audio_usecase_t uc_id)
3191{
3192 unsigned int interact_uc_index;
3193 unsigned int num_usecase = sizeof(interactive_usecases)/sizeof(interactive_usecases[0]);
3194
3195 for (interact_uc_index = 0; interact_uc_index < num_usecase; interact_uc_index++) {
3196 if (interactive_usecases[interact_uc_index] == uc_id) {
3197 adev->interactive_usecase_state &= ~(0x1 << interact_uc_index);
3198 break;
3199 }
3200 }
3201 ALOGV("%s: free Interactive usecase %d", __func__, uc_id);
3202}
3203
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003204bool is_offload_usecase(audio_usecase_t uc_id)
3205{
3206 unsigned int i;
3207 for (i = 0; i < sizeof(offload_usecases)/sizeof(offload_usecases[0]); i++) {
3208 if (uc_id == offload_usecases[i])
3209 return true;
3210 }
3211 return false;
3212}
3213
Dhananjay Kumarac341582017-02-23 23:42:25 +05303214static audio_usecase_t get_offload_usecase(struct audio_device *adev, bool is_compress)
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003215{
vivek mehta446c3962015-09-14 10:57:35 -07003216 audio_usecase_t ret_uc = USECASE_INVALID;
3217 unsigned int offload_uc_index;
Alexy Josephb1379942016-01-29 15:49:38 -08003218 unsigned int num_usecase = sizeof(offload_usecases)/sizeof(offload_usecases[0]);
vivek mehta446c3962015-09-14 10:57:35 -07003219 if (!adev->multi_offload_enable) {
Dhananjay Kumarac341582017-02-23 23:42:25 +05303220 if (!is_compress)
vivek mehta446c3962015-09-14 10:57:35 -07003221 ret_uc = USECASE_AUDIO_PLAYBACK_OFFLOAD2;
3222 else
3223 ret_uc = USECASE_AUDIO_PLAYBACK_OFFLOAD;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003224
vivek mehta446c3962015-09-14 10:57:35 -07003225 pthread_mutex_lock(&adev->lock);
3226 if (get_usecase_from_list(adev, ret_uc) != NULL)
3227 ret_uc = USECASE_INVALID;
3228 pthread_mutex_unlock(&adev->lock);
3229
3230 return ret_uc;
3231 }
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003232
3233 ALOGV("%s: num_usecase: %d", __func__, num_usecase);
vivek mehta446c3962015-09-14 10:57:35 -07003234 for (offload_uc_index = 0; offload_uc_index < num_usecase; offload_uc_index++) {
3235 if (!(adev->offload_usecases_state & (0x1 << offload_uc_index))) {
3236 adev->offload_usecases_state |= 0x1 << offload_uc_index;
3237 ret_uc = offload_usecases[offload_uc_index];
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003238 break;
3239 }
3240 }
vivek mehta446c3962015-09-14 10:57:35 -07003241
3242 ALOGV("%s: offload usecase is %d", __func__, ret_uc);
3243 return ret_uc;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003244}
3245
3246static void free_offload_usecase(struct audio_device *adev,
3247 audio_usecase_t uc_id)
3248{
vivek mehta446c3962015-09-14 10:57:35 -07003249 unsigned int offload_uc_index;
Alexy Josephb1379942016-01-29 15:49:38 -08003250 unsigned int num_usecase = sizeof(offload_usecases)/sizeof(offload_usecases[0]);
vivek mehta446c3962015-09-14 10:57:35 -07003251
3252 if (!adev->multi_offload_enable)
3253 return;
3254
3255 for (offload_uc_index = 0; offload_uc_index < num_usecase; offload_uc_index++) {
3256 if (offload_usecases[offload_uc_index] == uc_id) {
3257 adev->offload_usecases_state &= ~(0x1 << offload_uc_index);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003258 break;
3259 }
3260 }
3261 ALOGV("%s: free offload usecase %d", __func__, uc_id);
3262}
3263
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003264static void *offload_thread_loop(void *context)
3265{
3266 struct stream_out *out = (struct stream_out *) context;
3267 struct listnode *item;
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08003268 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003269
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003270 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
Weiyin Jiangd9b5a4e2019-07-18 17:24:21 +08003271 set_sched_policy(0, SP_FOREGROUND);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003272 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
3273
3274 ALOGV("%s", __func__);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07003275 lock_output_stream(out);
juyuchen391b5fa2018-12-12 17:58:09 +08003276 out->offload_state = OFFLOAD_STATE_IDLE;
3277 out->playback_started = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003278 for (;;) {
3279 struct offload_cmd *cmd = NULL;
3280 stream_callback_event_t event;
3281 bool send_callback = false;
3282
3283 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
3284 __func__, list_empty(&out->offload_cmd_list),
3285 out->offload_state);
3286 if (list_empty(&out->offload_cmd_list)) {
3287 ALOGV("%s SLEEPING", __func__);
3288 pthread_cond_wait(&out->offload_cond, &out->lock);
3289 ALOGV("%s RUNNING", __func__);
3290 continue;
3291 }
3292
3293 item = list_head(&out->offload_cmd_list);
3294 cmd = node_to_item(item, struct offload_cmd, node);
3295 list_remove(item);
3296
3297 ALOGVV("%s STATE %d CMD %d out->compr %p",
3298 __func__, out->offload_state, cmd->cmd, out->compr);
3299
3300 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
3301 free(cmd);
3302 break;
3303 }
3304
Haynes Mathew Georgeee5836f2017-11-21 18:02:10 -08003305 // allow OFFLOAD_CMD_ERROR reporting during standby
3306 // this is needed to handle failures during compress_open
3307 // Note however that on a pause timeout, the stream is closed
3308 // and no offload usecase will be active. Therefore this
3309 // special case is needed for compress_open failures alone
3310 if (cmd->cmd != OFFLOAD_CMD_ERROR &&
3311 out->compr == NULL) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003312 ALOGE("%s: Compress handle is NULL", __func__);
Haynes Mathew Georgea9abb202016-06-02 14:13:20 -07003313 free(cmd);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003314 pthread_cond_signal(&out->cond);
3315 continue;
3316 }
3317 out->offload_thread_blocked = true;
3318 pthread_mutex_unlock(&out->lock);
3319 send_callback = false;
3320 switch(cmd->cmd) {
3321 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07003322 ALOGD("copl(%p):calling compress_wait", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003323 compress_wait(out->compr, -1);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07003324 ALOGD("copl(%p):out of compress_wait", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003325 send_callback = true;
3326 event = STREAM_CBK_EVENT_WRITE_READY;
3327 break;
3328 case OFFLOAD_CMD_PARTIAL_DRAIN:
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08003329 ret = compress_next_track(out->compr);
Sidipotu Ashok55820562014-02-10 16:16:38 +05303330 if(ret == 0) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07003331 ALOGD("copl(%p):calling compress_partial_drain", out);
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05303332 ret = compress_partial_drain(out->compr);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07003333 ALOGD("copl(%p):out of compress_partial_drain", out);
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05303334 if (ret < 0)
3335 ret = -errno;
Sidipotu Ashok55820562014-02-10 16:16:38 +05303336 }
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05303337 else if (ret == -ETIMEDOUT)
Aniket Kumar Lata3570fb12017-11-08 15:53:44 -08003338 ret = compress_drain(out->compr);
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08003339 else
3340 ALOGE("%s: Next track returned error %d",__func__, ret);
Aniket Kumar Lata3570fb12017-11-08 15:53:44 -08003341 if (-ENETRESET != ret && !(-EINTR == ret &&
3342 CARD_STATUS_OFFLINE == out->card_status)) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05303343 send_callback = true;
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05303344 pthread_mutex_lock(&out->lock);
3345 out->send_new_metadata = 1;
3346 out->send_next_track_params = true;
3347 pthread_mutex_unlock(&out->lock);
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05303348 event = STREAM_CBK_EVENT_DRAIN_READY;
3349 ALOGV("copl(%p):send drain callback, ret %d", out, ret);
3350 } else
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05303351 ALOGI("%s: Block drain ready event during SSR", __func__);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003352 break;
3353 case OFFLOAD_CMD_DRAIN:
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07003354 ALOGD("copl(%p):calling compress_drain", out);
Aniket Kumar Lata3570fb12017-11-08 15:53:44 -08003355 ret = compress_drain(out->compr);
3356 ALOGD("copl(%p):out of compress_drain", out);
3357 // EINTR check avoids drain interruption due to SSR
3358 if (-ENETRESET != ret && !(-EINTR == ret &&
3359 CARD_STATUS_OFFLINE == out->card_status)) {
3360 send_callback = true;
3361 event = STREAM_CBK_EVENT_DRAIN_READY;
3362 } else
3363 ALOGI("%s: Block drain ready event during SSR", __func__);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003364 break;
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05303365 case OFFLOAD_CMD_ERROR:
3366 ALOGD("copl(%p): sending error callback to AF", out);
3367 send_callback = true;
3368 event = STREAM_CBK_EVENT_ERROR;
3369 break;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003370 default:
3371 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
3372 break;
3373 }
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07003374 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003375 out->offload_thread_blocked = false;
3376 pthread_cond_signal(&out->cond);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08003377 if (send_callback && out->client_callback) {
3378 ALOGVV("%s: sending client_callback event %d", __func__, event);
3379 out->client_callback(event, NULL, out->client_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -07003380 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003381 free(cmd);
3382 }
3383
3384 pthread_cond_signal(&out->cond);
3385 while (!list_empty(&out->offload_cmd_list)) {
3386 item = list_head(&out->offload_cmd_list);
3387 list_remove(item);
3388 free(node_to_item(item, struct offload_cmd, node));
3389 }
3390 pthread_mutex_unlock(&out->lock);
3391
3392 return NULL;
3393}
3394
3395static int create_offload_callback_thread(struct stream_out *out)
3396{
3397 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
3398 list_init(&out->offload_cmd_list);
3399 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
3400 offload_thread_loop, out);
3401 return 0;
3402}
3403
3404static int destroy_offload_callback_thread(struct stream_out *out)
3405{
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07003406 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003407 stop_compressed_output_l(out);
3408 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
3409
3410 pthread_mutex_unlock(&out->lock);
3411 pthread_join(out->offload_thread, (void **) NULL);
3412 pthread_cond_destroy(&out->offload_cond);
3413
3414 return 0;
3415}
3416
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003417static int stop_output_stream(struct stream_out *out)
3418{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05303419 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003420 struct audio_usecase *uc_info;
3421 struct audio_device *adev = out->dev;
Aalique Grahame22e49102018-12-18 14:23:57 -08003422 bool has_voip_usecase =
3423 get_usecase_from_list(adev, USECASE_AUDIO_PLAYBACK_VOIP) != NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003424
Eric Laurent994a6932013-07-17 11:51:42 -07003425 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003426 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003427 uc_info = get_usecase_from_list(adev, out->usecase);
3428 if (uc_info == NULL) {
3429 ALOGE("%s: Could not find the usecase (%d) in the list",
3430 __func__, out->usecase);
3431 return -EINVAL;
3432 }
3433
Derek Chenea197282019-01-07 17:35:01 -08003434 if (audio_extn_ext_hw_plugin_usecase_stop(adev->ext_hw_plugin, uc_info))
3435 ALOGE("%s: failed to stop ext hw plugin", __func__);
Derek Chend2530072014-11-24 12:39:14 -08003436
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07003437 if (is_offload_usecase(out->usecase) &&
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05303438 !(audio_extn_passthru_is_passthrough_stream(out))) {
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08003439 if (adev->visualizer_stop_output != NULL)
3440 adev->visualizer_stop_output(out->handle, out->pcm_device_id);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08003441
3442 audio_extn_dts_remove_state_notifier_node(out->usecase);
3443
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08003444 if (adev->offload_effects_stop_output != NULL)
3445 adev->offload_effects_stop_output(out->handle, out->pcm_device_id);
vivek mehtad15d2bf2019-05-17 13:35:10 -07003446 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_ULL ||
3447 out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
3448 audio_low_latency_hint_end();
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08003449 }
Eric Laurentc4aef752013-09-12 17:45:53 -07003450
Arun Mirpuri2ce92a62018-09-12 18:36:10 -07003451 if (out->usecase == USECASE_INCALL_MUSIC_UPLINK ||
3452 out->usecase == USECASE_INCALL_MUSIC_UPLINK2) {
Arun Mirpurief53ce52018-09-11 18:00:09 -07003453 voice_set_device_mute_flag(adev, false);
Arun Mirpuri2ce92a62018-09-12 18:36:10 -07003454 }
Arun Mirpurief53ce52018-09-11 18:00:09 -07003455
Eric Laurent150dbfe2013-02-27 14:31:02 -08003456 /* 1. Get and set stream specific mixer controls */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07003457 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003458
3459 /* 2. Disable the rx device */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07003460 disable_snd_device(adev, uc_info->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003461
Aalique Grahame22e49102018-12-18 14:23:57 -08003462 audio_extn_extspk_update(adev->extspk);
3463
Xiaojun Sang785b5da2017-08-03 15:52:29 +08003464 if (is_offload_usecase(out->usecase)) {
3465 audio_enable_asm_bit_width_enforce_mode(adev->mixer,
3466 adev->dsp_bit_width_enforce_mode,
3467 false);
3468 }
Garmond Leung5fd0b552018-04-17 11:56:12 -07003469 if (audio_is_usb_out_device(out->devices & AUDIO_DEVICE_OUT_ALL_USB)) {
3470 ret = audio_extn_usb_check_and_set_svc_int(uc_info,
3471 false);
3472
3473 if (ret != 0)
3474 check_usecases_codec_backend(adev, uc_info, uc_info->out_snd_device);
3475 /* default service interval was successfully updated,
3476 reopen USB backend with new service interval */
3477 ret = 0;
3478 }
Xiaojun Sang785b5da2017-08-03 15:52:29 +08003479
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08003480 list_remove(&uc_info->list);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303481 out->started = 0;
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07003482 if (is_offload_usecase(out->usecase) &&
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05303483 (audio_extn_passthru_is_passthrough_stream(out))) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07003484 ALOGV("Disable passthrough , reset mixer to pcm");
3485 /* NO_PASSTHROUGH */
3486 out->compr_config.codec->compr_passthr = 0;
Mingming Yin21854652016-04-13 11:54:02 -07003487 audio_extn_passthru_on_stop(out);
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07003488 audio_extn_dolby_set_dap_bypass(adev, DAP_STATE_ON);
3489 }
Eric Laurent07eeafd2013-10-06 12:52:49 -07003490
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05303491 /* Must be called after removing the usecase from list */
3492 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
Md Mansoor Ahmeddb1b4f92018-01-25 18:56:31 +05303493 audio_extn_keep_alive_start(KEEP_ALIVE_OUT_HDMI);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05303494
Manish Dewangan21a850a2017-08-14 12:03:55 +05303495 if (out->ip_hdlr_handle) {
Naresh Tanniru85819452017-05-04 18:55:45 -07003496 ret = audio_extn_ip_hdlr_intf_close(out->ip_hdlr_handle, true, out);
3497 if (ret < 0)
3498 ALOGE("%s: audio_extn_ip_hdlr_intf_close failed %d",__func__, ret);
3499 }
3500
juyuchen2d415992018-11-16 14:15:16 +08003501 /* 1) media + voip output routing to handset must route media back to
3502 speaker when voip stops.
3503 2) trigger voip input to reroute when voip output changes to
3504 hearing aid. */
Aalique Grahame22e49102018-12-18 14:23:57 -08003505 if (has_voip_usecase ||
3506 out->devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
3507 struct listnode *node;
3508 struct audio_usecase *usecase;
3509 list_for_each(node, &adev->usecase_list) {
3510 usecase = node_to_item(node, struct audio_usecase, list);
juyuchen2d415992018-11-16 14:15:16 +08003511 if ((usecase->type == PCM_CAPTURE &&
3512 usecase->id != USECASE_AUDIO_RECORD_VOIP)
3513 || usecase == uc_info)
Aalique Grahame22e49102018-12-18 14:23:57 -08003514 continue;
3515
3516 ALOGD("%s: select_devices at usecase(%d: %s) after removing the usecase(%d: %s)",
3517 __func__, usecase->id, use_case_table[usecase->id],
3518 out->usecase, use_case_table[out->usecase]);
3519 select_devices(adev, usecase->id);
3520 }
3521 }
3522
Garmond Leung5fd0b552018-04-17 11:56:12 -07003523 free(uc_info);
Eric Laurent994a6932013-07-17 11:51:42 -07003524 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003525 return ret;
3526}
3527
Vignesh Kulothungana6927272019-02-20 15:17:07 -08003528struct pcm* pcm_open_prepare_helper(unsigned int snd_card, unsigned int pcm_device_id,
3529 unsigned int flags, unsigned int pcm_open_retry_count,
3530 struct pcm_config *config)
3531{
3532 struct pcm* pcm = NULL;
3533
3534 while (1) {
3535 pcm = pcm_open(snd_card, pcm_device_id, flags, config);
3536 if (pcm == NULL || !pcm_is_ready(pcm)) {
3537 ALOGE("%s: %s", __func__, pcm_get_error(pcm));
3538 if (pcm != NULL) {
3539 pcm_close(pcm);
3540 pcm = NULL;
3541 }
Weiyin Jiang72197252019-10-09 11:49:32 +08003542 if (pcm_open_retry_count == 0)
Vignesh Kulothungana6927272019-02-20 15:17:07 -08003543 return NULL;
3544
Weiyin Jiang72197252019-10-09 11:49:32 +08003545 pcm_open_retry_count--;
Vignesh Kulothungana6927272019-02-20 15:17:07 -08003546 usleep(PROXY_OPEN_WAIT_TIME * 1000);
3547 continue;
3548 }
3549 break;
3550 }
3551
3552 if (pcm_is_ready(pcm)) {
3553 int ret = pcm_prepare(pcm);
3554 if (ret < 0) {
3555 ALOGE("%s: pcm_prepare returned %d", __func__, ret);
3556 pcm_close(pcm);
3557 pcm = NULL;
3558 }
3559 }
3560
3561 return pcm;
3562}
3563
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003564int start_output_stream(struct stream_out *out)
3565{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003566 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003567 struct audio_usecase *uc_info;
3568 struct audio_device *adev = out->dev;
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08003569 char mixer_ctl_name[128];
3570 struct mixer_ctl *ctl = NULL;
3571 char* perf_mode[] = {"ULL", "ULL_PP", "LL"};
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303572 bool a2dp_combo = false;
Vignesh Kulothungana6927272019-02-20 15:17:07 -08003573 bool is_haptic_usecase = (out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) ? true: false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003574
Haynes Mathew George380745d2017-10-04 15:27:45 -07003575 ATRACE_BEGIN("start_output_stream");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07003576 if ((out->usecase < 0) || (out->usecase >= AUDIO_USECASE_MAX)) {
3577 ret = -EINVAL;
3578 goto error_config;
3579 }
3580
Vignesh Kulothungana6927272019-02-20 15:17:07 -08003581 ALOGD("%s: enter: stream(%p)usecase(%d: %s) devices(%#x) is_haptic_usecase(%d)",
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05303582 __func__, &out->stream, out->usecase, use_case_table[out->usecase],
Vignesh Kulothungana6927272019-02-20 15:17:07 -08003583 out->devices, is_haptic_usecase);
Naresh Tanniru4c630392014-05-12 01:05:52 +05303584
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05303585 if (CARD_STATUS_OFFLINE == out->card_status ||
3586 CARD_STATUS_OFFLINE == adev->card_status) {
3587 ALOGW("out->card_status or adev->card_status offline, try again");
Dhanalakshmi Siddani4d57e992014-07-17 16:37:51 +05303588 ret = -EIO;
Naresh Tanniru4c630392014-05-12 01:05:52 +05303589 goto error_config;
3590 }
Naresh Tanniru4c630392014-05-12 01:05:52 +05303591
Arun Mirpuri2ce92a62018-09-12 18:36:10 -07003592 //Update incall music usecase to reflect correct voice session
3593 if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
3594 ret = voice_extn_check_and_set_incall_music_usecase(adev, out);
3595 if (ret != 0) {
3596 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
3597 __func__, ret);
3598 goto error_config;
3599 }
3600 }
3601
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05303602 if (out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
Florian Pfister1a84f312018-07-19 14:38:18 +02003603 if (!audio_extn_a2dp_source_is_ready()) {
Aalique Grahame22e49102018-12-18 14:23:57 -08003604 if (out->devices &
3605 (AUDIO_DEVICE_OUT_SPEAKER | AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303606 a2dp_combo = true;
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05303607 } else {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303608 if (!(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
3609 ALOGE("%s: A2DP profile is not ready, return error", __func__);
3610 ret = -EAGAIN;
3611 goto error_config;
3612 }
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05303613 }
3614 }
3615 }
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303616 if (out->devices & AUDIO_DEVICE_OUT_ALL_SCO) {
3617 if (!adev->bt_sco_on) {
3618 if (out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
3619 //combo usecase just by pass a2dp
3620 ALOGW("%s: SCO is not connected, route it to speaker", __func__);
3621 out->devices = AUDIO_DEVICE_OUT_SPEAKER;
3622 } else {
3623 ALOGE("%s: SCO profile is not ready, return error", __func__);
3624 ret = -EAGAIN;
3625 goto error_config;
3626 }
3627 }
3628 }
3629
Eric Laurentb23d5282013-05-14 15:27:20 -07003630 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003631 if (out->pcm_device_id < 0) {
3632 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
3633 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08003634 ret = -EINVAL;
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07003635 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003636 }
3637
Vignesh Kulothungana6927272019-02-20 15:17:07 -08003638 if (is_haptic_usecase) {
3639 adev->haptic_pcm_device_id = platform_get_haptics_pcm_device_id();
3640 if (adev->haptic_pcm_device_id < 0) {
3641 ALOGE("%s: Invalid Haptics pcm device id(%d) for the usecase(%d)",
3642 __func__, adev->haptic_pcm_device_id, out->usecase);
3643 ret = -EINVAL;
3644 goto error_config;
3645 }
3646 }
3647
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003648 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07003649
3650 if (!uc_info) {
3651 ret = -ENOMEM;
3652 goto error_config;
3653 }
3654
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003655 uc_info->id = out->usecase;
3656 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08003657 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003658 uc_info->devices = out->devices;
3659 uc_info->in_snd_device = SND_DEVICE_NONE;
3660 uc_info->out_snd_device = SND_DEVICE_NONE;
Garmond Leung5fd0b552018-04-17 11:56:12 -07003661
3662 /* This must be called before adding this usecase to the list */
3663 if (audio_is_usb_out_device(out->devices & AUDIO_DEVICE_OUT_ALL_USB)) {
3664 audio_extn_usb_check_and_set_svc_int(uc_info, true);
3665 /* USB backend is not reopened immediately.
3666 This is eventually done as part of select_devices */
3667 }
3668
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08003669 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003670
Wei Wangf7ca6c92017-11-21 14:51:20 -08003671 audio_streaming_hint_start();
Sudheer Papothifa9d2282015-09-17 01:53:25 +05303672 audio_extn_perf_lock_acquire(&adev->perf_lock_handle, 0,
3673 adev->perf_lock_opts,
3674 adev->perf_lock_opts_size);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05303675
3676 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Md Mansoor Ahmeddb1b4f92018-01-25 18:56:31 +05303677 audio_extn_keep_alive_stop(KEEP_ALIVE_OUT_HDMI);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05303678 if (audio_extn_passthru_is_enabled() &&
3679 audio_extn_passthru_is_passthrough_stream(out)) {
3680 audio_extn_passthru_on_start(out);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05303681 }
3682 }
3683
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303684 if ((out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) &&
Florian Pfister1a84f312018-07-19 14:38:18 +02003685 (!audio_extn_a2dp_source_is_ready())) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303686 if (!a2dp_combo) {
3687 check_a2dp_restore_l(adev, out, false);
3688 } else {
3689 audio_devices_t dev = out->devices;
Aalique Grahame22e49102018-12-18 14:23:57 -08003690 if (dev & AUDIO_DEVICE_OUT_SPEAKER_SAFE)
3691 out->devices = AUDIO_DEVICE_OUT_SPEAKER_SAFE;
3692 else
3693 out->devices = AUDIO_DEVICE_OUT_SPEAKER;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303694 select_devices(adev, out->usecase);
3695 out->devices = dev;
3696 }
3697 } else {
3698 select_devices(adev, out->usecase);
3699 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003700
Arun Mirpuri2ce92a62018-09-12 18:36:10 -07003701 if (out->usecase == USECASE_INCALL_MUSIC_UPLINK ||
3702 out->usecase == USECASE_INCALL_MUSIC_UPLINK2) {
Arun Mirpurief53ce52018-09-11 18:00:09 -07003703 voice_set_device_mute_flag(adev, true);
Arun Mirpuri2ce92a62018-09-12 18:36:10 -07003704 }
Arun Mirpurief53ce52018-09-11 18:00:09 -07003705
Derek Chenea197282019-01-07 17:35:01 -08003706 if (audio_extn_ext_hw_plugin_usecase_start(adev->ext_hw_plugin, uc_info))
3707 ALOGE("%s: failed to start ext hw plugin", __func__);
Derek Chend2530072014-11-24 12:39:14 -08003708
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003709 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d) format(%#x)",
3710 __func__, adev->snd_card, out->pcm_device_id, out->config.format);
Haynes Mathew George16081042017-05-31 17:16:49 -07003711
3712 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
Arun Mirpuri5d170872019-03-26 13:21:31 -07003713 ALOGD("%s: Starting MMAP stream", __func__);
Haynes Mathew George16081042017-05-31 17:16:49 -07003714 if (out->pcm == NULL || !pcm_is_ready(out->pcm)) {
3715 ALOGE("%s: pcm stream not ready", __func__);
3716 goto error_open;
3717 }
3718 ret = pcm_start(out->pcm);
3719 if (ret < 0) {
3720 ALOGE("%s: MMAP pcm_start failed ret %d", __func__, ret);
3721 goto error_open;
3722 }
Arun Mirpuri5d170872019-03-26 13:21:31 -07003723 out_set_mmap_volume(&out->stream, out->volume_l, out->volume_r);
Haynes Mathew George16081042017-05-31 17:16:49 -07003724 } else if (!is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003725 unsigned int flags = PCM_OUT;
3726 unsigned int pcm_open_retry_count = 0;
3727 if (out->usecase == USECASE_AUDIO_PLAYBACK_AFE_PROXY) {
3728 flags |= PCM_MMAP | PCM_NOIRQ;
3729 pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT;
Haynes Mathew George5beddd42016-06-27 18:33:40 -07003730 } else if (out->realtime) {
Haynes Mathew George4ab3ba92017-12-11 14:49:43 -08003731 flags |= PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003732 } else
3733 flags |= PCM_MONOTONIC;
3734
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08003735 if ((adev->vr_audio_mode_enabled) &&
3736 (out->flags & AUDIO_OUTPUT_FLAG_RAW)) {
3737 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
3738 "PCM_Dev %d Topology", out->pcm_device_id);
3739 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
3740 if (!ctl) {
3741 ALOGI("%s: Could not get ctl for mixer cmd might be ULL - %s",
3742 __func__, mixer_ctl_name);
3743 } else {
3744 //if success use ULLPP
3745 ALOGI("%s: mixer ctrl %s succeeded setting up ULL for %d",
3746 __func__, mixer_ctl_name, out->pcm_device_id);
3747 //There is a still a possibility that some sessions
3748 // that request for FAST|RAW when 3D audio is active
3749 //can go through ULLPP. Ideally we expects apps to
3750 //listen to audio focus and stop concurrent playback
3751 //Also, we will look for mode flag (voice_in_communication)
3752 //before enabling the realtime flag.
3753 mixer_ctl_set_enum_by_string(ctl, perf_mode[1]);
3754 }
3755 }
3756
Surendar Karka91fa3682018-07-02 18:12:12 +05303757 if (out->realtime)
3758 platform_set_stream_channel_map(adev->platform, out->channel_mask,
3759 out->pcm_device_id, &out->channel_map_param.channel_map[0]);
3760
Vignesh Kulothungana6927272019-02-20 15:17:07 -08003761 out->pcm = pcm_open_prepare_helper(adev->snd_card, out->pcm_device_id,
3762 flags, pcm_open_retry_count,
3763 &(out->config));
3764 if (out->pcm == NULL) {
3765 ret = -EIO;
3766 goto error_open;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003767 }
Vignesh Kulothungana6927272019-02-20 15:17:07 -08003768
3769 if (is_haptic_usecase) {
3770 adev->haptic_pcm = pcm_open_prepare_helper(adev->snd_card,
3771 adev->haptic_pcm_device_id,
3772 flags, pcm_open_retry_count,
3773 &(adev->haptics_config));
3774 // failure to open haptics pcm shouldnt stop audio,
3775 // so do not close audio pcm in case of error
Vignesh Kulothungane4039c12019-05-07 15:51:39 -07003776
3777 if (property_get_bool("vendor.audio.enable_haptic_audio_sync", false)) {
3778 ALOGD("%s: enable haptic audio synchronization", __func__);
3779 platform_set_qtime(adev->platform, out->pcm_device_id, adev->haptic_pcm_device_id);
3780 }
Vignesh Kulothungana6927272019-02-20 15:17:07 -08003781 }
3782
Surendar Karka91fa3682018-07-02 18:12:12 +05303783 if (!out->realtime)
3784 platform_set_stream_channel_map(adev->platform, out->channel_mask,
Surendar Karkaf51b5842018-04-26 11:28:38 +05303785 out->pcm_device_id, &out->channel_map_param.channel_map[0]);
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07003786
Zhou Song2b8f28f2017-09-11 10:51:38 +08003787 // apply volume for voip playback after path is set up
3788 if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP)
3789 out_set_voip_volume(&out->stream, out->volume_l, out->volume_r);
Ramu Gottipati36547092018-12-28 11:32:09 +05303790 else if ((out->usecase == USECASE_AUDIO_PLAYBACK_LOW_LATENCY || out->usecase == USECASE_AUDIO_PLAYBACK_DEEP_BUFFER ||
3791 out->usecase == USECASE_AUDIO_PLAYBACK_ULL) && (out->apply_volume)) {
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05303792 out_set_pcm_volume(&out->stream, out->volume_l, out->volume_r);
3793 out->apply_volume = false;
Derek Chenf13dd492018-11-13 14:53:51 -08003794 } else if (audio_extn_auto_hal_is_bus_device_usecase(out->usecase)) {
3795 out_set_pcm_volume(&out->stream, out->volume_l, out->volume_r);
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05303796 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003797 } else {
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -07003798 platform_set_stream_channel_map(adev->platform, out->channel_mask,
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05303799 out->pcm_device_id, &out->channel_map_param.channel_map[0]);
Xiaojun Sang785b5da2017-08-03 15:52:29 +08003800 audio_enable_asm_bit_width_enforce_mode(adev->mixer,
3801 adev->dsp_bit_width_enforce_mode,
3802 true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003803 out->pcm = NULL;
Haynes Mathew George380745d2017-10-04 15:27:45 -07003804 ATRACE_BEGIN("compress_open");
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08003805 out->compr = compress_open(adev->snd_card,
3806 out->pcm_device_id,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003807 COMPRESS_IN, &out->compr_config);
Haynes Mathew George380745d2017-10-04 15:27:45 -07003808 ATRACE_END();
Satish Babu Patakokila54ce83d2018-07-06 18:00:37 +05303809 if (errno == ENETRESET && !is_compress_ready(out->compr)) {
Sharad Sanglec6f32552018-05-04 16:15:38 +05303810 ALOGE("%s: compress_open failed errno:%d\n", __func__, errno);
3811 adev->card_status = CARD_STATUS_OFFLINE;
3812 out->card_status = CARD_STATUS_OFFLINE;
3813 ret = -EIO;
3814 goto error_open;
3815 }
3816
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003817 if (out->compr && !is_compress_ready(out->compr)) {
Haynes Mathew Georgeee5836f2017-11-21 18:02:10 -08003818 ALOGE("%s: failed /w error %s", __func__, compress_get_error(out->compr));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003819 compress_close(out->compr);
3820 out->compr = NULL;
3821 ret = -EIO;
3822 goto error_open;
3823 }
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05303824 /* compress_open sends params of the track, so reset the flag here */
3825 out->is_compr_metadata_avail = false;
3826
Ben Rombergerd771a7c2017-02-22 18:05:17 -08003827 if (out->client_callback)
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003828 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07003829
Fred Oh3f43e742015-03-04 18:42:34 -08003830 /* Since small bufs uses blocking writes, a write will be blocked
3831 for the default max poll time (20s) in the event of an SSR.
3832 Reduce the poll time to observe and deal with SSR faster.
3833 */
Ashish Jain5106d362016-05-11 19:23:33 +05303834 if (!out->non_blocking) {
Fred Oh3f43e742015-03-04 18:42:34 -08003835 compress_set_max_poll_wait(out->compr, 1000);
3836 }
3837
Manish Dewangan69426c82017-01-30 17:35:36 +05303838 audio_extn_utils_compress_set_render_mode(out);
Manish Dewangan58229382017-02-02 15:48:41 +05303839 audio_extn_utils_compress_set_clk_rec_mode(uc_info);
Manish Dewangan69426c82017-01-30 17:35:36 +05303840
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08003841 audio_extn_dts_create_state_notifier_node(out->usecase);
3842 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
3843 popcount(out->channel_mask),
3844 out->playback_started);
3845
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08003846#ifdef DS1_DOLBY_DDP_ENABLED
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05303847 if (audio_extn_utils_is_dolby_format(out->format))
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08003848 audio_extn_dolby_send_ddp_endp_params(adev);
3849#endif
Preetam Singh Ranawatd18d8832017-02-08 17:34:54 +05303850 if (!(audio_extn_passthru_is_passthrough_stream(out)) &&
3851 (out->sample_rate != 176400 && out->sample_rate <= 192000)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07003852 if (adev->visualizer_start_output != NULL)
3853 adev->visualizer_start_output(out->handle, out->pcm_device_id);
3854 if (adev->offload_effects_start_output != NULL)
Ashish Jain5106d362016-05-11 19:23:33 +05303855 adev->offload_effects_start_output(out->handle, out->pcm_device_id, adev->mixer);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08003856 audio_extn_check_and_set_dts_hpx_state(adev);
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07003857 }
Derek Chenf13dd492018-11-13 14:53:51 -08003858
3859 if (out->devices & AUDIO_DEVICE_OUT_BUS) {
3860 /* Update cached volume from media to offload/direct stream */
3861 struct listnode *node = NULL;
3862 list_for_each(node, &adev->active_outputs_list) {
3863 streams_output_ctxt_t *out_ctxt = node_to_item(node,
3864 streams_output_ctxt_t,
3865 list);
3866 if (out_ctxt->output->usecase == USECASE_AUDIO_PLAYBACK_MEDIA) {
3867 out->volume_l = out_ctxt->output->volume_l;
3868 out->volume_r = out_ctxt->output->volume_r;
3869 }
3870 }
3871 out_set_compr_volume(&out->stream,
3872 out->volume_l, out->volume_r);
3873 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003874 }
Haynes Mathew George5beddd42016-06-27 18:33:40 -07003875
3876 if (ret == 0) {
3877 register_out_stream(out);
3878 if (out->realtime) {
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07003879 if (out->pcm == NULL || !pcm_is_ready(out->pcm)) {
3880 ALOGE("%s: pcm stream not ready", __func__);
3881 goto error_open;
3882 }
Haynes Mathew George380745d2017-10-04 15:27:45 -07003883 ATRACE_BEGIN("pcm_start");
Haynes Mathew George5beddd42016-06-27 18:33:40 -07003884 ret = pcm_start(out->pcm);
Haynes Mathew George380745d2017-10-04 15:27:45 -07003885 ATRACE_END();
Haynes Mathew George5beddd42016-06-27 18:33:40 -07003886 if (ret < 0)
3887 goto error_open;
3888 }
3889 }
Wei Wangf7ca6c92017-11-21 14:51:20 -08003890 audio_streaming_hint_end();
Sudheer Papothifa9d2282015-09-17 01:53:25 +05303891 audio_extn_perf_lock_release(&adev->perf_lock_handle);
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07003892 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07003893
vivek mehtad15d2bf2019-05-17 13:35:10 -07003894 if (out->usecase == USECASE_AUDIO_PLAYBACK_ULL ||
3895 out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
3896 audio_low_latency_hint_start();
3897 }
3898
Manish Dewangan21a850a2017-08-14 12:03:55 +05303899 if (out->ip_hdlr_handle) {
Vidyakumar Athota6d655882017-05-22 18:26:24 -07003900 ret = audio_extn_ip_hdlr_intf_open(out->ip_hdlr_handle, true, out, out->usecase);
Naresh Tanniru85819452017-05-04 18:55:45 -07003901 if (ret < 0)
3902 ALOGE("%s: audio_extn_ip_hdlr_intf_open failed %d",__func__, ret);
3903 }
3904
Vignesh Kulothungan3b5fae52017-09-25 12:16:30 -07003905 // consider a scenario where on pause lower layers are tear down.
3906 // so on resume, swap mixer control need to be sent only when
3907 // backend is active, hence rather than sending from enable device
3908 // sending it from start of streamtream
3909
3910 platform_set_swap_channels(adev, true);
3911
Haynes Mathew George380745d2017-10-04 15:27:45 -07003912 ATRACE_END();
Vatsal Buchac09ae062018-11-14 13:25:08 +05303913 enable_gcov();
Haynes Mathew George5beddd42016-06-27 18:33:40 -07003914 return ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003915error_open:
Vignesh Kulothungana6927272019-02-20 15:17:07 -08003916 if (adev->haptic_pcm) {
3917 pcm_close(adev->haptic_pcm);
3918 adev->haptic_pcm = NULL;
3919 }
Wei Wangf7ca6c92017-11-21 14:51:20 -08003920 audio_streaming_hint_end();
Sudheer Papothifa9d2282015-09-17 01:53:25 +05303921 audio_extn_perf_lock_release(&adev->perf_lock_handle);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003922 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08003923error_config:
Laxminath Kasam2cb4b752015-09-24 03:59:15 +05303924 /*
3925 * sleep 50ms to allow sufficient time for kernel
3926 * drivers to recover incases like SSR.
3927 */
3928 usleep(50000);
Haynes Mathew George380745d2017-10-04 15:27:45 -07003929 ATRACE_END();
Vatsal Buchac09ae062018-11-14 13:25:08 +05303930 enable_gcov();
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08003931 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003932}
3933
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003934static int check_input_parameters(uint32_t sample_rate,
3935 audio_format_t format,
Aalique Grahame22e49102018-12-18 14:23:57 -08003936 int channel_count,
3937 bool is_usb_hifi)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003938{
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003939 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003940
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05303941 if (((format != AUDIO_FORMAT_PCM_16_BIT) && (format != AUDIO_FORMAT_PCM_8_24_BIT) &&
3942 (format != AUDIO_FORMAT_PCM_24_BIT_PACKED) && (format != AUDIO_FORMAT_PCM_32_BIT) &&
3943 (format != AUDIO_FORMAT_PCM_FLOAT)) &&
Mingming Yine62d7842013-10-25 16:26:03 -07003944 !voice_extn_compress_voip_is_format_supported(format) &&
Ralf Herzaec80262018-07-03 07:08:49 +02003945 !audio_extn_compr_cap_format_supported(format) &&
3946 !audio_extn_cin_format_supported(format))
Haynes Mathew George484e8d22017-07-31 18:55:17 -07003947 ret = -EINVAL;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003948
Aalique Grahame22e49102018-12-18 14:23:57 -08003949 int max_channel_count = is_usb_hifi ? MAX_HIFI_CHANNEL_COUNT : MAX_CHANNEL_COUNT;
3950 if ((channel_count < MIN_CHANNEL_COUNT) || (channel_count > max_channel_count)) {
3951 ALOGE("%s: unsupported channel count (%d) passed Min / Max (%d / %d)", __func__,
3952 channel_count, MIN_CHANNEL_COUNT, max_channel_count);
3953 return -EINVAL;
3954 }
3955
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003956 switch (channel_count) {
3957 case 1:
3958 case 2:
Chaithanya Krishna Bacharaju9955b162016-05-25 16:25:53 +05303959 case 3:
3960 case 4:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003961 case 6:
Karthikeyan Mani07faa602018-08-20 11:01:32 -07003962 case 8:
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05303963 case 10:
3964 case 12:
3965 case 14:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003966 break;
3967 default:
3968 ret = -EINVAL;
3969 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003970
3971 switch (sample_rate) {
3972 case 8000:
3973 case 11025:
3974 case 12000:
3975 case 16000:
3976 case 22050:
3977 case 24000:
3978 case 32000:
3979 case 44100:
3980 case 48000:
Haynes Mathew Georgec9253d12017-12-13 15:58:28 -08003981 case 88200:
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05303982 case 96000:
Haynes Mathew Georgec9253d12017-12-13 15:58:28 -08003983 case 176400:
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05303984 case 192000:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003985 break;
3986 default:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003987 ret = -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003988 }
3989
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003990 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003991}
3992
Naresh Tanniru04f71882018-06-26 17:46:22 +05303993
3994/** Add a value in a list if not already present.
3995 * @return true if value was successfully inserted or already present,
3996 * false if the list is full and does not contain the value.
3997 */
3998static bool register_uint(uint32_t value, uint32_t* list, size_t list_length) {
3999 for (size_t i = 0; i < list_length; i++) {
4000 if (list[i] == value) return true; // value is already present
4001 if (list[i] == 0) { // no values in this slot
4002 list[i] = value;
4003 return true; // value inserted
4004 }
4005 }
4006 return false; // could not insert value
4007}
4008
4009/** Add channel_mask in supported_channel_masks if not already present.
4010 * @return true if channel_mask was successfully inserted or already present,
4011 * false if supported_channel_masks is full and does not contain channel_mask.
4012 */
4013static void register_channel_mask(audio_channel_mask_t channel_mask,
4014 audio_channel_mask_t supported_channel_masks[static MAX_SUPPORTED_CHANNEL_MASKS]) {
4015 ALOGE_IF(!register_uint(channel_mask, supported_channel_masks, MAX_SUPPORTED_CHANNEL_MASKS),
4016 "%s: stream can not declare supporting its channel_mask %x", __func__, channel_mask);
4017}
4018
4019/** Add format in supported_formats if not already present.
4020 * @return true if format was successfully inserted or already present,
4021 * false if supported_formats is full and does not contain format.
4022 */
4023static void register_format(audio_format_t format,
4024 audio_format_t supported_formats[static MAX_SUPPORTED_FORMATS]) {
4025 ALOGE_IF(!register_uint(format, supported_formats, MAX_SUPPORTED_FORMATS),
4026 "%s: stream can not declare supporting its format %x", __func__, format);
4027}
4028/** Add sample_rate in supported_sample_rates if not already present.
4029 * @return true if sample_rate was successfully inserted or already present,
4030 * false if supported_sample_rates is full and does not contain sample_rate.
4031 */
4032static void register_sample_rate(uint32_t sample_rate,
4033 uint32_t supported_sample_rates[static MAX_SUPPORTED_SAMPLE_RATES]) {
4034 ALOGE_IF(!register_uint(sample_rate, supported_sample_rates, MAX_SUPPORTED_SAMPLE_RATES),
4035 "%s: stream can not declare supporting its sample rate %x", __func__, sample_rate);
4036}
4037
Karthikeyan Manib38769c2018-11-07 15:44:13 -08004038static inline uint32_t lcm(uint32_t num1, uint32_t num2)
4039{
4040 uint32_t high = num1, low = num2, temp = 0;
4041
4042 if (!num1 || !num2)
4043 return 0;
4044
4045 if (num1 < num2) {
4046 high = num2;
4047 low = num1;
4048 }
4049
4050 while (low != 0) {
4051 temp = low;
4052 low = high % low;
4053 high = temp;
4054 }
4055 return (num1 * num2)/high;
4056}
4057
4058static inline uint32_t nearest_multiple(uint32_t num, uint32_t multiplier)
4059{
4060 uint32_t remainder = 0;
4061
4062 if (!multiplier)
4063 return num;
4064
4065 remainder = num % multiplier;
4066 if (remainder)
4067 num += (multiplier - remainder);
4068
4069 return num;
4070}
4071
Aalique Grahame22e49102018-12-18 14:23:57 -08004072static size_t get_stream_buffer_size(size_t duration_ms,
4073 uint32_t sample_rate,
4074 audio_format_t format,
4075 int channel_count,
4076 bool is_low_latency)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004077{
4078 size_t size = 0;
Karthikeyan Manib38769c2018-11-07 15:44:13 -08004079 uint32_t bytes_per_period_sample = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004080
Aalique Grahame22e49102018-12-18 14:23:57 -08004081 size = (sample_rate * duration_ms) / 1000;
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07004082 if (is_low_latency)
4083 size = configured_low_latency_capture_period_size;
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05304084
Karthikeyan Manib38769c2018-11-07 15:44:13 -08004085 bytes_per_period_sample = audio_bytes_per_sample(format) * channel_count;
Aalique Grahame22e49102018-12-18 14:23:57 -08004086 size *= audio_bytes_per_sample(format) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004087
Ralf Herzbd08d632018-09-28 15:50:49 +02004088 /* make sure the size is multiple of 32 bytes and additionally multiple of
4089 * the frame_size (required for 24bit samples and non-power-of-2 channel counts)
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07004090 * At 48 kHz mono 16-bit PCM:
4091 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
4092 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
Karthikeyan Manib38769c2018-11-07 15:44:13 -08004093 * Also, make sure the size is multiple of bytes per period sample
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07004094 */
Karthikeyan Manib38769c2018-11-07 15:44:13 -08004095 size = nearest_multiple(size, lcm(32, bytes_per_period_sample));
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07004096
4097 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004098}
4099
Aalique Grahame22e49102018-12-18 14:23:57 -08004100static size_t get_input_buffer_size(uint32_t sample_rate,
4101 audio_format_t format,
4102 int channel_count,
4103 bool is_low_latency)
4104{
4105 /* Don't know if USB HIFI in this context so use true to be conservative */
4106 if (check_input_parameters(sample_rate, format, channel_count,
4107 true /*is_usb_hifi */) != 0)
4108 return 0;
4109
4110 return get_stream_buffer_size(AUDIO_CAPTURE_PERIOD_DURATION_MSEC,
4111 sample_rate,
4112 format,
4113 channel_count,
4114 is_low_latency);
4115}
4116
Derek Chenf6318be2017-06-12 17:16:24 -04004117size_t get_output_period_size(uint32_t sample_rate,
4118 audio_format_t format,
4119 int channel_count,
4120 int duration /*in millisecs*/)
Ashish Jain058165c2016-09-28 23:18:48 +05304121{
4122 size_t size = 0;
4123 uint32_t bytes_per_sample = audio_bytes_per_sample(format);
4124
4125 if ((duration == 0) || (sample_rate == 0) ||
4126 (bytes_per_sample == 0) || (channel_count == 0)) {
4127 ALOGW("Invalid config duration %d sr %d bps %d ch %d", duration, sample_rate,
4128 bytes_per_sample, channel_count);
4129 return -EINVAL;
4130 }
4131
4132 size = (sample_rate *
4133 duration *
4134 bytes_per_sample *
4135 channel_count) / 1000;
4136 /*
4137 * To have same PCM samples for all channels, the buffer size requires to
4138 * be multiple of (number of channels * bytes per sample)
4139 * For writes to succeed, the buffer must be written at address which is multiple of 32
4140 */
4141 size = ALIGN(size, (bytes_per_sample * channel_count * 32));
4142
4143 return (size/(channel_count * bytes_per_sample));
4144}
4145
Zhou Song48453a02018-01-10 17:50:59 +08004146static uint64_t get_actual_pcm_frames_rendered(struct stream_out *out, struct timespec *timestamp)
Ashish Jain5106d362016-05-11 19:23:33 +05304147{
4148 uint64_t actual_frames_rendered = 0;
4149 size_t kernel_buffer_size = out->compr_config.fragment_size * out->compr_config.fragments;
4150
4151 /* This adjustment accounts for buffering after app processor.
4152 * It is based on estimated DSP latency per use case, rather than exact.
4153 */
4154 int64_t platform_latency = platform_render_latency(out->usecase) *
4155 out->sample_rate / 1000000LL;
4156
Zhou Song48453a02018-01-10 17:50:59 +08004157 pthread_mutex_lock(&out->position_query_lock);
Ashish Jain5106d362016-05-11 19:23:33 +05304158 /* not querying actual state of buffering in kernel as it would involve an ioctl call
4159 * which then needs protection, this causes delay in TS query for pcm_offload usecase
4160 * hence only estimate.
4161 */
George Gao62ebc722019-07-29 16:29:44 -07004162 uint64_t signed_frames = 0;
4163 if (out->written >= kernel_buffer_size)
4164 signed_frames = out->written - kernel_buffer_size;
Ashish Jain5106d362016-05-11 19:23:33 +05304165
George Gao62ebc722019-07-29 16:29:44 -07004166 signed_frames = signed_frames / (audio_bytes_per_sample(out->format) * popcount(out->channel_mask));
4167 if (signed_frames >= platform_latency)
4168 signed_frames = signed_frames - platform_latency;
Ashish Jain5106d362016-05-11 19:23:33 +05304169
Zhou Song48453a02018-01-10 17:50:59 +08004170 if (signed_frames > 0) {
Ashish Jain5106d362016-05-11 19:23:33 +05304171 actual_frames_rendered = signed_frames;
Zhou Song48453a02018-01-10 17:50:59 +08004172 if (timestamp != NULL )
4173 *timestamp = out->writeAt;
4174 } else if (timestamp != NULL) {
4175 clock_gettime(CLOCK_MONOTONIC, timestamp);
4176 }
4177 pthread_mutex_unlock(&out->position_query_lock);
Ashish Jain5106d362016-05-11 19:23:33 +05304178
4179 ALOGVV("%s signed frames %lld out_written %lld kernel_buffer_size %d"
George Gao62ebc722019-07-29 16:29:44 -07004180 "bytes/sample %zu channel count %d", __func__, signed_frames,
Ashish Jain5106d362016-05-11 19:23:33 +05304181 (long long int)out->written, (int)kernel_buffer_size,
4182 audio_bytes_per_sample(out->compr_config.codec->format),
4183 popcount(out->channel_mask));
4184
4185 return actual_frames_rendered;
4186}
4187
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004188static uint32_t out_get_sample_rate(const struct audio_stream *stream)
4189{
4190 struct stream_out *out = (struct stream_out *)stream;
4191
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004192 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004193}
4194
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07004195static int out_set_sample_rate(struct audio_stream *stream __unused,
4196 uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004197{
4198 return -ENOSYS;
4199}
4200
4201static size_t out_get_buffer_size(const struct audio_stream *stream)
4202{
4203 struct stream_out *out = (struct stream_out *)stream;
4204
Varun Balaraje49253e2017-07-06 19:48:56 +05304205 if (is_interactive_usecase(out->usecase)) {
Sri Karri27279e12017-08-07 16:05:20 +05304206 return out->config.period_size * out->config.period_count;
Varun Balaraje49253e2017-07-06 19:48:56 +05304207 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Naresh Tanniruee3499a2017-01-05 14:05:35 +05304208 if (out->flags & AUDIO_OUTPUT_FLAG_TIMESTAMP)
4209 return out->compr_config.fragment_size - sizeof(struct snd_codec_metadata);
4210 else
4211 return out->compr_config.fragment_size;
4212 } else if(out->usecase == USECASE_COMPRESS_VOIP_CALL)
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08004213 return voice_extn_compress_voip_out_get_buffer_size(out);
Dhananjay Kumarac341582017-02-23 23:42:25 +05304214 else if (is_offload_usecase(out->usecase) &&
4215 out->flags == AUDIO_OUTPUT_FLAG_DIRECT)
Ashish Jain83a6cc22016-06-28 14:34:17 +05304216 return out->hal_fragment_size;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004217
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004218 return out->config.period_size * out->af_period_multiplier *
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07004219 audio_stream_out_frame_size((const struct audio_stream_out *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004220}
4221
4222static uint32_t out_get_channels(const struct audio_stream *stream)
4223{
4224 struct stream_out *out = (struct stream_out *)stream;
4225
4226 return out->channel_mask;
4227}
4228
4229static audio_format_t out_get_format(const struct audio_stream *stream)
4230{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004231 struct stream_out *out = (struct stream_out *)stream;
4232
4233 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004234}
4235
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07004236static int out_set_format(struct audio_stream *stream __unused,
4237 audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004238{
4239 return -ENOSYS;
4240}
4241
4242static int out_standby(struct audio_stream *stream)
4243{
4244 struct stream_out *out = (struct stream_out *)stream;
4245 struct audio_device *adev = out->dev;
Haynes Mathew George16081042017-05-31 17:16:49 -07004246 bool do_stop = true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004247
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05304248 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
4249 stream, out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004250
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07004251 lock_output_stream(out);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004252 if (!out->standby) {
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07004253 if (adev->adm_deregister_stream)
4254 adev->adm_deregister_stream(adev->adm_data, out->handle);
4255
Haynes Mathew George7fce0a52016-06-23 18:22:27 -07004256 if (is_offload_usecase(out->usecase))
4257 stop_compressed_output_l(out);
4258
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08004259 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004260 out->standby = true;
Zhou Songa8895042016-07-05 17:54:22 +08004261 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
4262 voice_extn_compress_voip_close_output_stream(stream);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304263 out->started = 0;
Zhou Songa8895042016-07-05 17:54:22 +08004264 pthread_mutex_unlock(&adev->lock);
4265 pthread_mutex_unlock(&out->lock);
4266 ALOGD("VOIP output entered standby");
4267 return 0;
4268 } else if (!is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004269 if (out->pcm) {
4270 pcm_close(out->pcm);
4271 out->pcm = NULL;
4272 }
Haynes Mathew George16081042017-05-31 17:16:49 -07004273 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
4274 do_stop = out->playback_started;
4275 out->playback_started = false;
Phil Burkd898ba62019-06-20 12:49:01 -07004276
4277 if (out->mmap_shared_memory_fd >= 0) {
4278 ALOGV("%s: closing mmap_shared_memory_fd = %d",
4279 __func__, out->mmap_shared_memory_fd);
4280 close(out->mmap_shared_memory_fd);
4281 out->mmap_shared_memory_fd = -1;
4282 }
Haynes Mathew George16081042017-05-31 17:16:49 -07004283 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004284 } else {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07004285 ALOGD("copl(%p):standby", out);
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05304286 out->send_next_track_params = false;
4287 out->is_compr_metadata_avail = false;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004288 out->gapless_mdata.encoder_delay = 0;
4289 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004290 if (out->compr != NULL) {
4291 compress_close(out->compr);
4292 out->compr = NULL;
4293 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08004294 }
Haynes Mathew George16081042017-05-31 17:16:49 -07004295 if (do_stop) {
4296 stop_output_stream(out);
4297 }
Lakshman Chaluvaraju06677b42019-06-24 10:04:52 +05304298 // if fm is active route on selected device in UI
4299 audio_extn_fm_route_on_selected_device(adev, out->devices);
Eric Laurent150dbfe2013-02-27 14:31:02 -08004300 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004301 }
4302 pthread_mutex_unlock(&out->lock);
Ashish Jainbbce4322016-02-16 13:25:27 +05304303 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004304 return 0;
4305}
4306
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304307static int out_on_error(struct audio_stream *stream)
4308{
4309 struct stream_out *out = (struct stream_out *)stream;
Ben Rombergerfd02a2f2018-09-17 10:23:23 -07004310 int status = 0;
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304311
4312 lock_output_stream(out);
Haynes Mathew Georgeee5836f2017-11-21 18:02:10 -08004313 // always send CMD_ERROR for offload streams, this
4314 // is needed e.g. when SSR happens within compress_open
4315 // since the stream is active, offload_callback_thread is also active.
4316 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
4317 stop_compressed_output_l(out);
Ben Rombergerfd02a2f2018-09-17 10:23:23 -07004318 }
4319 pthread_mutex_unlock(&out->lock);
4320
4321 status = out_standby(&out->stream.common);
4322
4323 lock_output_stream(out);
4324 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Haynes Mathew Georgeee5836f2017-11-21 18:02:10 -08004325 send_offload_cmd_l(out, OFFLOAD_CMD_ERROR);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304326 }
Manisha Agarwal7b3e3772019-02-20 14:33:45 +05304327
4328 if (is_offload_usecase(out->usecase) && out->card_status == CARD_STATUS_OFFLINE) {
4329 ALOGD("Setting previous card status if offline");
4330 out->prev_card_status_offline = true;
4331 }
4332
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304333 pthread_mutex_unlock(&out->lock);
4334
Ben Rombergerfd02a2f2018-09-17 10:23:23 -07004335 return status;
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304336}
4337
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304338/*
4339 *standby implementation without locks, assumes that the callee already
4340 *has taken adev and out lock.
4341 */
4342int out_standby_l(struct audio_stream *stream)
4343{
4344 struct stream_out *out = (struct stream_out *)stream;
4345 struct audio_device *adev = out->dev;
4346
4347 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
4348 stream, out->usecase, use_case_table[out->usecase]);
4349
4350 if (!out->standby) {
Haynes Mathew George380745d2017-10-04 15:27:45 -07004351 ATRACE_BEGIN("out_standby_l");
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304352 if (adev->adm_deregister_stream)
4353 adev->adm_deregister_stream(adev->adm_data, out->handle);
4354
4355 if (is_offload_usecase(out->usecase))
4356 stop_compressed_output_l(out);
4357
4358 out->standby = true;
4359 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
4360 voice_extn_compress_voip_close_output_stream(stream);
4361 out->started = 0;
4362 ALOGD("VOIP output entered standby");
Haynes Mathew George380745d2017-10-04 15:27:45 -07004363 ATRACE_END();
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304364 return 0;
4365 } else if (!is_offload_usecase(out->usecase)) {
4366 if (out->pcm) {
4367 pcm_close(out->pcm);
4368 out->pcm = NULL;
4369 }
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004370 if (out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) {
4371 if (adev->haptic_pcm) {
4372 pcm_close(adev->haptic_pcm);
4373 adev->haptic_pcm = NULL;
4374 }
4375
4376 if (adev->haptic_buffer != NULL) {
4377 free(adev->haptic_buffer);
4378 adev->haptic_buffer = NULL;
4379 adev->haptic_buffer_size = 0;
4380 }
4381 adev->haptic_pcm_device_id = 0;
4382 }
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304383 } else {
4384 ALOGD("copl(%p):standby", out);
4385 out->send_next_track_params = false;
4386 out->is_compr_metadata_avail = false;
4387 out->gapless_mdata.encoder_delay = 0;
4388 out->gapless_mdata.encoder_padding = 0;
4389 if (out->compr != NULL) {
4390 compress_close(out->compr);
4391 out->compr = NULL;
4392 }
4393 }
4394 stop_output_stream(out);
Haynes Mathew George380745d2017-10-04 15:27:45 -07004395 ATRACE_END();
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304396 }
4397 ALOGD("%s: exit", __func__);
4398 return 0;
4399}
4400
Aalique Grahame22e49102018-12-18 14:23:57 -08004401static int out_dump(const struct audio_stream *stream, int fd)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004402{
Aalique Grahame22e49102018-12-18 14:23:57 -08004403 struct stream_out *out = (struct stream_out *)stream;
4404
4405 // We try to get the lock for consistency,
4406 // but it isn't necessary for these variables.
4407 // If we're not in standby, we may be blocked on a write.
4408 const bool locked = (pthread_mutex_trylock(&out->lock) == 0);
4409 dprintf(fd, " Standby: %s\n", out->standby ? "yes" : "no");
4410 dprintf(fd, " Frames written: %lld\n", (long long)out->written);
4411
4412 if (locked) {
4413 pthread_mutex_unlock(&out->lock);
4414 }
4415
4416 // dump error info
4417 (void)error_log_dump(
4418 out->error_log, fd, " " /* prefix */, 0 /* lines */, 0 /* limit_ns */);
4419
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004420 return 0;
4421}
4422
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004423static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
4424{
4425 int ret = 0;
4426 char value[32];
ApurupaPattapu2e084df2013-12-18 15:47:59 -08004427
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004428 if (!out || !parms) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08004429 ALOGE("%s: return invalid ",__func__);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004430 return -EINVAL;
4431 }
4432
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +05304433 ret = audio_extn_parse_compress_metadata(out, parms);
Weiyin Jiang18ac4e92015-03-15 15:03:40 +08004434
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004435 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
4436 if (ret >= 0) {
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +05304437 out->gapless_mdata.encoder_delay = atoi(value); //whats a good limit check?
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004438 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004439 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
4440 if (ret >= 0) {
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +05304441 out->gapless_mdata.encoder_padding = atoi(value);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004442 }
4443
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004444 ALOGV("%s new encoder delay %u and padding %u", __func__,
4445 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
4446
4447 return 0;
4448}
4449
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07004450static bool output_drives_call(struct audio_device *adev, struct stream_out *out)
4451{
4452 return out == adev->primary_output || out == adev->voice_tx_output;
4453}
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004454
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304455// note: this call is safe only if the stream_cb is
4456// removed first in close_output_stream (as is done now).
4457static void out_snd_mon_cb(void * stream, struct str_parms * parms)
4458{
4459 if (!stream || !parms)
4460 return;
4461
4462 struct stream_out *out = (struct stream_out *)stream;
4463 struct audio_device *adev = out->dev;
4464
4465 card_status_t status;
4466 int card;
4467 if (parse_snd_card_status(parms, &card, &status) < 0)
4468 return;
4469
4470 pthread_mutex_lock(&adev->lock);
4471 bool valid_cb = (card == adev->snd_card);
4472 pthread_mutex_unlock(&adev->lock);
4473
4474 if (!valid_cb)
4475 return;
4476
4477 lock_output_stream(out);
4478 if (out->card_status != status)
4479 out->card_status = status;
4480 pthread_mutex_unlock(&out->lock);
4481
4482 ALOGI("out_snd_mon_cb for card %d usecase %s, status %s", card,
4483 use_case_table[out->usecase],
4484 status == CARD_STATUS_OFFLINE ? "offline" : "online");
4485
Aditya Bavanari59ebed42019-02-05 17:44:57 +05304486 if (status == CARD_STATUS_OFFLINE) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304487 out_on_error(stream);
Aditya Bavanari59ebed42019-02-05 17:44:57 +05304488 if (voice_is_call_state_active(adev) &&
4489 out == adev->primary_output) {
4490 ALOGD("%s: SSR/PDR occurred, end all calls\n", __func__);
4491 pthread_mutex_lock(&adev->lock);
4492 voice_stop_call(adev);
4493 adev->mode = AUDIO_MODE_NORMAL;
4494 pthread_mutex_unlock(&adev->lock);
4495 }
4496 }
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304497 return;
4498}
4499
Kevin Rocardfce19002017-08-07 19:21:36 -07004500static int get_alive_usb_card(struct str_parms* parms) {
4501 int card;
4502 if ((str_parms_get_int(parms, "card", &card) >= 0) &&
4503 !audio_extn_usb_alive(card)) {
4504 return card;
4505 }
4506 return -ENODEV;
4507}
4508
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004509static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
4510{
4511 struct stream_out *out = (struct stream_out *)stream;
4512 struct audio_device *adev = out->dev;
4513 struct str_parms *parms;
4514 char value[32];
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08004515 int ret = 0, val = 0, err;
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07004516 int ext_controller = -1;
4517 int ext_stream = -1;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304518 bool bypass_a2dp = false;
Garmond Leung5fd0b552018-04-17 11:56:12 -07004519 bool reconfig = false;
4520 unsigned long service_interval = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004521
sangwoobc677242013-08-08 16:53:43 +09004522 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07004523 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004524 parms = str_parms_create_str(kvpairs);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05304525 if (!parms)
4526 goto error;
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07004527
4528 err = platform_get_controller_stream_from_params(parms, &ext_controller,
4529 &ext_stream);
4530 if (err >= 0) {
4531 out->extconn.cs.controller = ext_controller;
4532 out->extconn.cs.stream = ext_stream;
4533 ALOGD("%s: usecase(%s) new controller/stream (%d/%d)", __func__,
4534 use_case_table[out->usecase], out->extconn.cs.controller,
4535 out->extconn.cs.stream);
4536 }
4537
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08004538 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
4539 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004540 val = atoi(value);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07004541 lock_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08004542 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004543
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07004544 /*
Weiyin Jiang4256eeb2016-05-19 13:28:30 +08004545 * When HDMI cable is unplugged the music playback is paused and
4546 * the policy manager sends routing=0. But the audioflinger continues
4547 * to write data until standby time (3sec). As the HDMI core is
4548 * turned off, the write gets blocked.
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07004549 * Avoid this by routing audio to speaker until standby.
4550 */
Weiyin Jiang4256eeb2016-05-19 13:28:30 +08004551 if ((out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL) &&
4552 (val == AUDIO_DEVICE_NONE) &&
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05304553 !audio_extn_passthru_is_passthrough_stream(out) &&
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07004554 (platform_get_edid_info_v2(adev->platform,
4555 out->extconn.cs.controller,
4556 out->extconn.cs.stream) != 0)) {
4557 out->extconn.cs.controller = out->extconn.cs.stream = -1;
Weiyin Jiang4256eeb2016-05-19 13:28:30 +08004558 val = AUDIO_DEVICE_OUT_SPEAKER;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07004559 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05304560 /*
4561 * When A2DP is disconnected the
4562 * music playback is paused and the policy manager sends routing=0
Florian Pfister1a84f312018-07-19 14:38:18 +02004563 * But the audioflinger continues to write data until standby time
Naresh Tanniru9d027a62015-03-13 01:32:10 +05304564 * (3sec). As BT is turned off, the write gets blocked.
4565 * Avoid this by routing audio to speaker until standby.
4566 */
Naresh Tanniruf7e9e632016-11-04 14:54:20 -07004567 if ((out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) &&
Aniket Kumar Lata1fb11cb2017-09-08 13:48:55 -07004568 (val == AUDIO_DEVICE_NONE) &&
Aniket Kumar Lata2cf6a922019-05-07 17:01:43 -07004569 !audio_extn_a2dp_source_is_ready() &&
4570 !adev->bt_sco_on) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05304571 val = AUDIO_DEVICE_OUT_SPEAKER;
4572 }
Sharad Sangled0a50b22018-04-05 23:28:32 +05304573 /*
4574 * When USB headset is disconnected the music platback paused
4575 * and the policy manager send routing=0. But if the USB is connected
4576 * back before the standby time, AFE is not closed and opened
4577 * when USB is connected back. So routing to speker will guarantee
4578 * AFE reconfiguration and AFE will be opend once USB is connected again
4579 */
4580 if ((out->devices & AUDIO_DEVICE_OUT_ALL_USB) &&
4581 (val == AUDIO_DEVICE_NONE) &&
4582 !audio_extn_usb_connected(parms)) {
4583 val = AUDIO_DEVICE_OUT_SPEAKER;
4584 }
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05304585 /* To avoid a2dp to sco overlapping / BT device improper state
4586 * check with BT lib about a2dp streaming support before routing
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05304587 */
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05304588 if (val & AUDIO_DEVICE_OUT_ALL_A2DP) {
Florian Pfister1a84f312018-07-19 14:38:18 +02004589 if (!audio_extn_a2dp_source_is_ready()) {
Aalique Grahame22e49102018-12-18 14:23:57 -08004590 if (val &
4591 (AUDIO_DEVICE_OUT_SPEAKER | AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05304592 //combo usecase just by pass a2dp
4593 ALOGW("%s: A2DP profile is not ready,routing to speaker only", __func__);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304594 bypass_a2dp = true;
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05304595 } else {
4596 ALOGE("%s: A2DP profile is not ready,ignoring routing request", __func__);
4597 /* update device to a2dp and don't route as BT returned error
4598 * However it is still possible a2dp routing called because
4599 * of current active device disconnection (like wired headset)
4600 */
4601 out->devices = val;
4602 pthread_mutex_unlock(&out->lock);
4603 pthread_mutex_unlock(&adev->lock);
4604 goto error;
4605 }
4606 }
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05304607 }
Kevin Rocardfce19002017-08-07 19:21:36 -07004608
4609 audio_devices_t new_dev = val;
4610
4611 // Workaround: If routing to an non existing usb device, fail gracefully
4612 // The routing request will otherwise block during 10 second
4613 int card;
4614 if (audio_is_usb_out_device(new_dev) &&
4615 (card = get_alive_usb_card(parms)) >= 0) {
4616
4617 ALOGW("out_set_parameters() ignoring rerouting to non existing USB card %d", card);
4618 pthread_mutex_unlock(&adev->lock);
4619 pthread_mutex_unlock(&out->lock);
4620 ret = -ENOSYS;
4621 goto routing_fail;
4622 }
4623
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07004624 /*
4625 * select_devices() call below switches all the usecases on the same
4626 * backend to the new device. Refer to check_usecases_codec_backend() in
4627 * the select_devices(). But how do we undo this?
4628 *
4629 * For example, music playback is active on headset (deep-buffer usecase)
4630 * and if we go to ringtones and select a ringtone, low-latency usecase
4631 * will be started on headset+speaker. As we can't enable headset+speaker
4632 * and headset devices at the same time, select_devices() switches the music
4633 * playback to headset+speaker while starting low-lateny usecase for ringtone.
4634 * So when the ringtone playback is completed, how do we undo the same?
4635 *
4636 * We are relying on the out_set_parameters() call on deep-buffer output,
4637 * once the ringtone playback is ended.
4638 * NOTE: We should not check if the current devices are same as new devices.
4639 * Because select_devices() must be called to switch back the music
4640 * playback to headset.
4641 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08004642 if (val != 0) {
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004643 audio_devices_t new_dev = val;
4644 bool same_dev = out->devices == new_dev;
4645 out->devices = new_dev;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07004646
Ravi Kumar Alamandabe149392014-10-20 17:07:43 -07004647 if (output_drives_call(adev, out)) {
kunleiz61b9c2e2017-11-01 13:47:23 +08004648 if (!voice_is_call_state_active(adev)) {
Ravi Kumar Alamandabe149392014-10-20 17:07:43 -07004649 if (adev->mode == AUDIO_MODE_IN_CALL) {
4650 adev->current_call_output = out;
Garmond Leung5fd0b552018-04-17 11:56:12 -07004651 if (audio_is_usb_out_device(out->devices & AUDIO_DEVICE_OUT_ALL_USB)) {
4652 service_interval = audio_extn_usb_find_service_interval(true, true /*playback*/);
4653 audio_extn_usb_set_service_interval(true /*playback*/,
4654 service_interval,
4655 &reconfig);
4656 ALOGD("%s, svc_int(%ld),reconfig(%d)",__func__,service_interval, reconfig);
4657 }
4658 ret = voice_start_call(adev);
Ravi Kumar Alamandabe149392014-10-20 17:07:43 -07004659 }
4660 } else {
4661 adev->current_call_output = out;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07004662 voice_update_devices_for_all_voice_usecases(adev);
Ravi Kumar Alamandabe149392014-10-20 17:07:43 -07004663 }
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08004664 }
Shiv Maliyappanahalli6b32c4c2015-11-04 18:10:20 -08004665
4666 if (!out->standby) {
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004667 if (!same_dev) {
4668 ALOGV("update routing change");
Sudheer Papothi80266982016-08-16 02:36:18 +05304669 audio_extn_perf_lock_acquire(&adev->perf_lock_handle, 0,
4670 adev->perf_lock_opts,
4671 adev->perf_lock_opts_size);
Haynes Mathew George822b5492016-07-01 16:57:24 -07004672 if (adev->adm_on_routing_change)
4673 adev->adm_on_routing_change(adev->adm_data,
4674 out->handle);
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004675 }
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304676 if (!bypass_a2dp) {
4677 select_devices(adev, out->usecase);
4678 } else {
Aalique Grahame22e49102018-12-18 14:23:57 -08004679 if (new_dev & AUDIO_DEVICE_OUT_SPEAKER_SAFE)
4680 out->devices = AUDIO_DEVICE_OUT_SPEAKER_SAFE;
4681 else
4682 out->devices = AUDIO_DEVICE_OUT_SPEAKER;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304683 select_devices(adev, out->usecase);
4684 out->devices = new_dev;
4685 }
Vignesh Kulothungan3b5fae52017-09-25 12:16:30 -07004686
4687 if (!same_dev) {
4688 // on device switch force swap, lower functions will make sure
4689 // to check if swap is allowed or not.
4690 platform_set_swap_channels(adev, true);
Sudheer Papothi80266982016-08-16 02:36:18 +05304691 audio_extn_perf_lock_release(&adev->perf_lock_handle);
Vignesh Kulothungan3b5fae52017-09-25 12:16:30 -07004692 }
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304693 if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
4694 out->a2dp_compress_mute &&
Florian Pfister1a84f312018-07-19 14:38:18 +02004695 (!(out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) || audio_extn_a2dp_source_is_ready())) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304696 pthread_mutex_lock(&out->compr_mute_lock);
4697 out->a2dp_compress_mute = false;
4698 out_set_compr_volume(&out->stream, out->volume_l, out->volume_r);
4699 pthread_mutex_unlock(&out->compr_mute_lock);
kunleizba786432018-01-26 15:31:59 +08004700 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP) {
4701 out_set_voip_volume(&out->stream, out->volume_l, out->volume_r);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304702 }
Shiv Maliyappanahalli6b32c4c2015-11-04 18:10:20 -08004703 }
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08004704 }
4705
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004706 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08004707 pthread_mutex_unlock(&out->lock);
Aalique Grahame22e49102018-12-18 14:23:57 -08004708
4709 /*handles device and call state changes*/
4710 audio_extn_extspk_update(adev->extspk);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004711 }
Kevin Rocardfce19002017-08-07 19:21:36 -07004712 routing_fail:
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07004713
4714 if (out == adev->primary_output) {
4715 pthread_mutex_lock(&adev->lock);
4716 audio_extn_set_parameters(adev, parms);
4717 pthread_mutex_unlock(&adev->lock);
4718 }
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07004719 if (is_offload_usecase(out->usecase)) {
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07004720 lock_output_stream(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004721 parse_compress_metadata(out, parms);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08004722
4723 audio_extn_dts_create_state_notifier_node(out->usecase);
4724 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
4725 popcount(out->channel_mask),
4726 out->playback_started);
4727
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08004728 pthread_mutex_unlock(&out->lock);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004729 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07004730
Surendar Karkaf51b5842018-04-26 11:28:38 +05304731 err = str_parms_get_str(parms, AUDIO_PARAMETER_DUAL_MONO, value,
4732 sizeof(value));
4733 if (err >= 0) {
4734 if (!strncmp("true", value, sizeof("true")) || atoi(value))
4735 audio_extn_send_dual_mono_mixing_coefficients(out);
4736 }
4737
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +05304738 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_PROFILE, value, sizeof(value));
4739 if (err >= 0) {
4740 strlcpy(out->profile, value, sizeof(out->profile));
4741 ALOGV("updating stream profile with value '%s'", out->profile);
4742 lock_output_stream(out);
4743 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
4744 &adev->streams_output_cfg_list,
Aalique Grahame65780b52017-09-27 14:59:56 -07004745 out->devices, out->flags, out->hal_op_format,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +05304746 out->sample_rate, out->bit_width,
4747 out->channel_mask, out->profile,
4748 &out->app_type_cfg);
4749 pthread_mutex_unlock(&out->lock);
4750 }
4751
Alexy Joseph98988832017-01-13 14:56:59 -08004752 //suspend, resume handling block
Aniket Kumar Lata932f4872017-11-06 18:29:44 -08004753 //remove QOS only if vendor.audio.hal.dynamic.qos.config.supported is set to true
4754 // and vendor.audio.hal.output.suspend.supported is set to true
4755 if (out->hal_output_suspend_supported && out->dynamic_pm_qos_config_supported) {
Alexy Joseph98988832017-01-13 14:56:59 -08004756 //check suspend parameter only for low latency and if the property
4757 //is enabled
4758 if (str_parms_get_str(parms, "suspend_playback", value, sizeof(value)) >= 0) {
4759 ALOGI("%s: got suspend_playback %s", __func__, value);
4760 lock_output_stream(out);
4761 if (!strncmp(value, "false", 5)) {
4762 //suspend_playback=false is supposed to set QOS value back to 75%
4763 //the mixer control sent with value Enable will achieve that
4764 ret = audio_route_apply_and_update_path(adev->audio_route, out->pm_qos_mixer_path);
4765 } else if (!strncmp (value, "true", 4)) {
4766 //suspend_playback=true is supposed to remove QOS value
4767 //resetting the mixer control will set the default value
4768 //for the mixer control which is Disable and this removes the QOS vote
4769 ret = audio_route_reset_and_update_path(adev->audio_route, out->pm_qos_mixer_path);
4770 } else {
4771 ALOGE("%s: Wrong value sent for suspend_playback, expected true/false,"
4772 " got %s", __func__, value);
4773 ret = -1;
4774 }
4775
4776 if (ret != 0) {
4777 ALOGE("%s: %s mixer ctl failed with %d, ignore suspend/resume setparams",
4778 __func__, out->pm_qos_mixer_path, ret);
4779 }
4780
4781 pthread_mutex_unlock(&out->lock);
4782 }
4783 }
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07004784
Alexy Joseph98988832017-01-13 14:56:59 -08004785 //end suspend, resume handling block
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004786 str_parms_destroy(parms);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05304787error:
Eric Laurent994a6932013-07-17 11:51:42 -07004788 ALOGV("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004789 return ret;
4790}
4791
Paul McLeana50b7332018-12-17 08:24:21 -07004792static int in_set_microphone_direction(const struct audio_stream_in *stream,
4793 audio_microphone_direction_t dir) {
justinweng20fb6d82019-02-21 18:49:00 -07004794 struct stream_in *in = (struct stream_in *)stream;
4795
4796 ALOGVV("%s: standby %d source %d dir %d", __func__, in->standby, in->source, dir);
4797
4798 in->direction = dir;
4799
4800 if (in->standby)
4801 return 0;
4802
4803 return audio_extn_audiozoom_set_microphone_direction(in, dir);
Paul McLeana50b7332018-12-17 08:24:21 -07004804}
4805
4806static int in_set_microphone_field_dimension(const struct audio_stream_in *stream, float zoom) {
justinweng20fb6d82019-02-21 18:49:00 -07004807 struct stream_in *in = (struct stream_in *)stream;
4808
4809 ALOGVV("%s: standby %d source %d zoom %f", __func__, in->standby, in->source, zoom);
4810
4811 if (zoom > 1.0 || zoom < -1.0)
4812 return -EINVAL;
4813
4814 in->zoom = zoom;
4815
4816 if (in->standby)
4817 return 0;
4818
4819 return audio_extn_audiozoom_set_microphone_field_dimension(in, zoom);
Paul McLeana50b7332018-12-17 08:24:21 -07004820}
4821
4822
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004823static bool stream_get_parameter_channels(struct str_parms *query,
4824 struct str_parms *reply,
4825 audio_channel_mask_t *supported_channel_masks) {
4826 int ret = -1;
4827 char value[512];
4828 bool first = true;
4829 size_t i, j;
4830
4831 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) {
4832 ret = 0;
4833 value[0] = '\0';
4834 i = 0;
4835 while (supported_channel_masks[i] != 0) {
4836 for (j = 0; j < ARRAY_SIZE(channels_name_to_enum_table); j++) {
4837 if (channels_name_to_enum_table[j].value == supported_channel_masks[i]) {
4838 if (!first)
Satya Krishna Pindiprolib22ac722017-10-09 15:44:16 +05304839 strlcat(value, "|", sizeof(value));
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004840
Satya Krishna Pindiprolib22ac722017-10-09 15:44:16 +05304841 strlcat(value, channels_name_to_enum_table[j].name, sizeof(value));
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004842 first = false;
4843 break;
4844 }
4845 }
4846 i++;
4847 }
4848 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
4849 }
4850 return ret == 0;
4851}
4852
4853static bool stream_get_parameter_formats(struct str_parms *query,
4854 struct str_parms *reply,
4855 audio_format_t *supported_formats) {
4856 int ret = -1;
4857 char value[256];
4858 size_t i, j;
4859 bool first = true;
4860
4861 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) {
4862 ret = 0;
4863 value[0] = '\0';
4864 i = 0;
4865 while (supported_formats[i] != 0) {
4866 for (j = 0; j < ARRAY_SIZE(formats_name_to_enum_table); j++) {
4867 if (formats_name_to_enum_table[j].value == supported_formats[i]) {
4868 if (!first) {
Satya Krishna Pindiprolib22ac722017-10-09 15:44:16 +05304869 strlcat(value, "|", sizeof(value));
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004870 }
Satya Krishna Pindiprolib22ac722017-10-09 15:44:16 +05304871 strlcat(value, formats_name_to_enum_table[j].name, sizeof(value));
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004872 first = false;
4873 break;
4874 }
4875 }
4876 i++;
4877 }
4878 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_FORMATS, value);
4879 }
4880 return ret == 0;
4881}
4882
4883static bool stream_get_parameter_rates(struct str_parms *query,
4884 struct str_parms *reply,
4885 uint32_t *supported_sample_rates) {
4886
4887 int i;
4888 char value[256];
4889 int ret = -1;
4890 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) {
4891 ret = 0;
4892 value[0] = '\0';
4893 i=0;
4894 int cursor = 0;
4895 while (supported_sample_rates[i]) {
4896 int avail = sizeof(value) - cursor;
4897 ret = snprintf(value + cursor, avail, "%s%d",
4898 cursor > 0 ? "|" : "",
4899 supported_sample_rates[i]);
4900 if (ret < 0 || ret >= avail) {
4901 // if cursor is at the last element of the array
4902 // overwrite with \0 is duplicate work as
4903 // snprintf already put a \0 in place.
4904 // else
4905 // we had space to write the '|' at value[cursor]
4906 // (which will be overwritten) or no space to fill
4907 // the first element (=> cursor == 0)
4908 value[cursor] = '\0';
4909 break;
4910 }
4911 cursor += ret;
4912 ++i;
4913 }
4914 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES,
4915 value);
4916 }
4917 return ret >= 0;
4918}
4919
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004920static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
4921{
4922 struct stream_out *out = (struct stream_out *)stream;
4923 struct str_parms *query = str_parms_create_str(keys);
Alexy Josephaee4fdd2016-01-29 13:02:07 -08004924 char *str = (char*) NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004925 char value[256];
4926 struct str_parms *reply = str_parms_create();
4927 size_t i, j;
4928 int ret;
4929 bool first = true;
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07004930
4931 if (!query || !reply) {
Alexy Josephaee4fdd2016-01-29 13:02:07 -08004932 if (reply) {
4933 str_parms_destroy(reply);
4934 }
4935 if (query) {
4936 str_parms_destroy(query);
4937 }
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07004938 ALOGE("out_get_parameters: failed to allocate mem for query or reply");
4939 return NULL;
4940 }
4941
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004942 ALOGV("%s: %s enter: keys - %s", __func__, use_case_table[out->usecase], keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004943 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
4944 if (ret >= 0) {
4945 value[0] = '\0';
4946 i = 0;
4947 while (out->supported_channel_masks[i] != 0) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004948 for (j = 0; j < ARRAY_SIZE(channels_name_to_enum_table); j++) {
4949 if (channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004950 if (!first) {
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -08004951 strlcat(value, "|", sizeof(value));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004952 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004953 strlcat(value, channels_name_to_enum_table[j].name, sizeof(value));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004954 first = false;
4955 break;
4956 }
4957 }
4958 i++;
4959 }
4960 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
4961 str = str_parms_to_str(reply);
4962 } else {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08004963 voice_extn_out_get_parameters(out, query, reply);
4964 str = str_parms_to_str(reply);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004965 }
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004966
Alexy Joseph62142aa2015-11-16 15:10:34 -08004967
4968 ret = str_parms_get_str(query, "is_direct_pcm_track", value, sizeof(value));
4969 if (ret >= 0) {
4970 value[0] = '\0';
Dhananjay Kumarac341582017-02-23 23:42:25 +05304971 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
4972 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Alexy Joseph62142aa2015-11-16 15:10:34 -08004973 ALOGV("in direct_pcm");
Satya Krishna Pindiprolib6655542017-07-03 19:38:19 +05304974 strlcat(value, "true", sizeof(value));
Alexy Joseph62142aa2015-11-16 15:10:34 -08004975 } else {
4976 ALOGV("not in direct_pcm");
Sharad Sangle3dd5a4a2015-12-10 18:39:17 +05304977 strlcat(value, "false", sizeof(value));
Alexy Joseph62142aa2015-11-16 15:10:34 -08004978 }
4979 str_parms_add_str(reply, "is_direct_pcm_track", value);
Alexy Josephaee4fdd2016-01-29 13:02:07 -08004980 if (str)
4981 free(str);
Alexy Joseph62142aa2015-11-16 15:10:34 -08004982 str = str_parms_to_str(reply);
4983 }
4984
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004985 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS, value, sizeof(value));
4986 if (ret >= 0) {
4987 value[0] = '\0';
4988 i = 0;
4989 first = true;
4990 while (out->supported_formats[i] != 0) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004991 for (j = 0; j < ARRAY_SIZE(formats_name_to_enum_table); j++) {
4992 if (formats_name_to_enum_table[j].value == out->supported_formats[i]) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004993 if (!first) {
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -08004994 strlcat(value, "|", sizeof(value));
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004995 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004996 strlcat(value, formats_name_to_enum_table[j].name, sizeof(value));
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004997 first = false;
4998 break;
4999 }
5000 }
5001 i++;
5002 }
5003 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_FORMATS, value);
Alexy Josephaee4fdd2016-01-29 13:02:07 -08005004 if (str)
5005 free(str);
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07005006 str = str_parms_to_str(reply);
5007 }
Mingming Yin3a941d42016-02-17 18:08:05 -08005008
5009 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES, value, sizeof(value));
5010 if (ret >= 0) {
5011 value[0] = '\0';
5012 i = 0;
5013 first = true;
5014 while (out->supported_sample_rates[i] != 0) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005015 for (j = 0; j < ARRAY_SIZE(out_sample_rates_name_to_enum_table); j++) {
5016 if (out_sample_rates_name_to_enum_table[j].value == out->supported_sample_rates[i]) {
Mingming Yin3a941d42016-02-17 18:08:05 -08005017 if (!first) {
5018 strlcat(value, "|", sizeof(value));
5019 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005020 strlcat(value, out_sample_rates_name_to_enum_table[j].name, sizeof(value));
Mingming Yin3a941d42016-02-17 18:08:05 -08005021 first = false;
5022 break;
5023 }
5024 }
5025 i++;
5026 }
5027 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES, value);
5028 if (str)
5029 free(str);
5030 str = str_parms_to_str(reply);
5031 }
5032
Alexy Joseph98988832017-01-13 14:56:59 -08005033 if (str_parms_get_str(query, "supports_hw_suspend", value, sizeof(value)) >= 0) {
5034 //only low latency track supports suspend_resume
5035 str_parms_add_int(reply, "supports_hw_suspend",
Aniket Kumar Lata932f4872017-11-06 18:29:44 -08005036 (out->hal_output_suspend_supported));
Alexy Joseph98988832017-01-13 14:56:59 -08005037 if (str)
5038 free(str);
5039 str = str_parms_to_str(reply);
5040 }
5041
5042
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005043 str_parms_destroy(query);
5044 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07005045 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005046 return str;
5047}
5048
5049static uint32_t out_get_latency(const struct audio_stream_out *stream)
5050{
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005051 uint32_t period_ms;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005052 struct stream_out *out = (struct stream_out *)stream;
Alexy Josephaa54c872014-12-03 02:46:47 -08005053 uint32_t latency = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005054
Alexy Josephaa54c872014-12-03 02:46:47 -08005055 if (is_offload_usecase(out->usecase)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05305056 lock_output_stream(out);
5057 latency = audio_extn_utils_compress_get_dsp_latency(out);
5058 pthread_mutex_unlock(&out->lock);
Haynes Mathew George16081042017-05-31 17:16:49 -07005059 } else if ((out->realtime) ||
5060 (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP)) {
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005061 // since the buffer won't be filled up faster than realtime,
5062 // return a smaller number
5063 if (out->config.rate)
5064 period_ms = (out->af_period_multiplier * out->config.period_size *
5065 1000) / (out->config.rate);
5066 else
5067 period_ms = 0;
5068 latency = period_ms + platform_render_latency(out->usecase)/1000;
Alexy Josephaa54c872014-12-03 02:46:47 -08005069 } else {
5070 latency = (out->config.period_count * out->config.period_size * 1000) /
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005071 (out->config.rate);
Alexy Josephaa54c872014-12-03 02:46:47 -08005072 }
5073
yidongh0515e042017-07-06 15:00:34 +08005074 if (AUDIO_DEVICE_OUT_ALL_A2DP & out->devices)
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08005075 latency += audio_extn_a2dp_get_encoder_latency();
5076
Anish Kumar50ebcbf2014-12-09 04:01:39 +05305077 ALOGV("%s: Latency %d", __func__, latency);
Alexy Josephaa54c872014-12-03 02:46:47 -08005078 return latency;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005079}
5080
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05305081static float AmpToDb(float amplification)
5082{
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05305083 float db = DSD_VOLUME_MIN_DB;
5084 if (amplification > 0) {
5085 db = 20 * log10(amplification);
5086 if(db < DSD_VOLUME_MIN_DB)
5087 return DSD_VOLUME_MIN_DB;
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05305088 }
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05305089 return db;
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05305090}
5091
Arun Mirpuri5d170872019-03-26 13:21:31 -07005092static int out_set_mmap_volume(struct audio_stream_out *stream, float left,
5093 float right)
5094{
5095 struct stream_out *out = (struct stream_out *)stream;
5096 long volume = 0;
5097 char mixer_ctl_name[128] = "";
5098 struct audio_device *adev = out->dev;
5099 struct mixer_ctl *ctl = NULL;
5100 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
5101 PCM_PLAYBACK);
5102
5103 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
5104 "Playback %d Volume", pcm_device_id);
5105 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
5106 if (!ctl) {
5107 ALOGE("%s: Could not get ctl for mixer cmd - %s",
5108 __func__, mixer_ctl_name);
5109 return -EINVAL;
5110 }
5111 if (left != right)
5112 ALOGW("%s: Left and right channel volume mismatch:%f,%f",
5113 __func__, left, right);
5114 volume = (long)(left * (MMAP_PLAYBACK_VOLUME_MAX*1.0));
5115 if (mixer_ctl_set_value(ctl, 0, volume) < 0){
5116 ALOGE("%s:ctl for mixer cmd - %s, volume %ld returned error",
5117 __func__, mixer_ctl_name, volume);
5118 return -EINVAL;
5119 }
5120 return 0;
5121}
5122
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05305123static int out_set_compr_volume(struct audio_stream_out *stream, float left,
5124 float right)
5125{
5126 struct stream_out *out = (struct stream_out *)stream;
Manish Dewangan338c50a2017-09-12 15:22:03 +05305127 long volume[2];
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05305128 char mixer_ctl_name[128];
5129 struct audio_device *adev = out->dev;
5130 struct mixer_ctl *ctl;
5131 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
5132 PCM_PLAYBACK);
5133
5134 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
5135 "Compress Playback %d Volume", pcm_device_id);
5136 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
5137 if (!ctl) {
5138 ALOGE("%s: Could not get ctl for mixer cmd - %s",
5139 __func__, mixer_ctl_name);
5140 return -EINVAL;
5141 }
5142 ALOGE("%s:ctl for mixer cmd - %s, left %f, right %f",
5143 __func__, mixer_ctl_name, left, right);
5144 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
5145 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
5146 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
5147
5148 return 0;
5149}
5150
Zhou Song2b8f28f2017-09-11 10:51:38 +08005151static int out_set_voip_volume(struct audio_stream_out *stream, float left,
5152 float right)
5153{
5154 struct stream_out *out = (struct stream_out *)stream;
5155 char mixer_ctl_name[] = "App Type Gain";
5156 struct audio_device *adev = out->dev;
5157 struct mixer_ctl *ctl;
Manish Dewangan338c50a2017-09-12 15:22:03 +05305158 long set_values[4];
Zhou Song2b8f28f2017-09-11 10:51:38 +08005159
Aniket Kumar Lata8426d1f2019-06-10 15:25:53 -07005160 if (!is_valid_volume(left, right)) {
5161 ALOGE("%s: Invalid stream volume for left=%f, right=%f",
5162 __func__, left, right);
5163 return -EINVAL;
5164 }
5165
Zhou Song2b8f28f2017-09-11 10:51:38 +08005166 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
5167 if (!ctl) {
5168 ALOGE("%s: Could not get ctl for mixer cmd - %s",
5169 __func__, mixer_ctl_name);
5170 return -EINVAL;
5171 }
5172
5173 set_values[0] = 0; //0: Rx Session 1:Tx Session
5174 set_values[1] = out->app_type_cfg.app_type;
Manish Dewangan338c50a2017-09-12 15:22:03 +05305175 set_values[2] = (long)(left * VOIP_PLAYBACK_VOLUME_MAX);
5176 set_values[3] = (long)(right * VOIP_PLAYBACK_VOLUME_MAX);
Zhou Song2b8f28f2017-09-11 10:51:38 +08005177
5178 mixer_ctl_set_array(ctl, set_values, ARRAY_SIZE(set_values));
5179 return 0;
5180}
5181
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05305182static int out_set_pcm_volume(struct audio_stream_out *stream, float left,
5183 float right)
5184{
5185 struct stream_out *out = (struct stream_out *)stream;
5186 /* Volume control for pcm playback */
5187 if (left != right) {
5188 return -EINVAL;
5189 } else {
5190 char mixer_ctl_name[128];
5191 struct audio_device *adev = out->dev;
5192 struct mixer_ctl *ctl;
5193 int pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
5194 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name), "Playback %d Volume", pcm_device_id);
5195 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
5196 if (!ctl) {
5197 ALOGE("%s : Could not get ctl for mixer cmd - %s", __func__, mixer_ctl_name);
5198 return -EINVAL;
5199 }
5200
5201 int volume = (int) (left * PCM_PLAYBACK_VOLUME_MAX);
5202 int ret = mixer_ctl_set_value(ctl, 0, volume);
5203 if (ret < 0) {
5204 ALOGE("%s: Could not set ctl, error:%d ", __func__, ret);
5205 return -EINVAL;
5206 }
5207
5208 ALOGV("%s : Pcm set volume value %d left %f", __func__, volume, left);
5209
5210 return 0;
5211 }
5212}
5213
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005214static int out_set_volume(struct audio_stream_out *stream, float left,
5215 float right)
5216{
Eric Laurenta9024de2013-04-04 09:19:12 -07005217 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005218 int volume[2];
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05305219 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005220
Arun Mirpuri5d170872019-03-26 13:21:31 -07005221 ALOGD("%s: called with left_vol=%f, right_vol=%f", __func__, left, right);
Eric Laurenta9024de2013-04-04 09:19:12 -07005222 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
5223 /* only take left channel into account: the API is for stereo anyway */
5224 out->muted = (left == 0.0f);
5225 return 0;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07005226 } else if (is_offload_usecase(out->usecase)) {
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05305227 if (audio_extn_passthru_is_passthrough_stream(out)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07005228 /*
5229 * Set mute or umute on HDMI passthrough stream.
5230 * Only take left channel into account.
5231 * Mute is 0 and unmute 1
5232 */
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05305233 audio_extn_passthru_set_volume(out, (left == 0.0f));
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05305234 } else if (out->format == AUDIO_FORMAT_DSD){
5235 char mixer_ctl_name[128] = "DSD Volume";
5236 struct audio_device *adev = out->dev;
5237 struct mixer_ctl *ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
5238
5239 if (!ctl) {
5240 ALOGE("%s: Could not get ctl for mixer cmd - %s",
5241 __func__, mixer_ctl_name);
5242 return -EINVAL;
5243 }
Manish Dewangan338c50a2017-09-12 15:22:03 +05305244 volume[0] = (long)(AmpToDb(left));
5245 volume[1] = (long)(AmpToDb(right));
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05305246 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
5247 return 0;
Philippe Gravel1c9ac352019-05-28 16:08:37 -07005248 } else if ((out->devices & AUDIO_DEVICE_OUT_BUS) &&
Derek Chendf05eea2019-08-01 13:57:49 -07005249 (out->car_audio_stream == CAR_AUDIO_STREAM_MEDIA)) {
Philippe Gravel1c9ac352019-05-28 16:08:37 -07005250 ALOGD("%s: Overriding offload set volume for media bus stream", __func__);
5251 struct listnode *node = NULL;
5252 list_for_each(node, &adev->active_outputs_list) {
5253 streams_output_ctxt_t *out_ctxt = node_to_item(node,
5254 streams_output_ctxt_t,
5255 list);
5256 if (out_ctxt->output->usecase == USECASE_AUDIO_PLAYBACK_MEDIA) {
5257 out->volume_l = out_ctxt->output->volume_l;
5258 out->volume_r = out_ctxt->output->volume_r;
5259 }
5260 }
5261 if (!out->a2dp_compress_mute) {
5262 ret = out_set_compr_volume(&out->stream, out->volume_l, out->volume_r);
5263 }
5264 return ret;
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07005265 } else {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05305266 pthread_mutex_lock(&out->compr_mute_lock);
Arun Mirpuri5d170872019-03-26 13:21:31 -07005267 ALOGV("%s: compress mute %d", __func__, out->a2dp_compress_mute);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05305268 if (!out->a2dp_compress_mute)
5269 ret = out_set_compr_volume(stream, left, right);
5270 out->volume_l = left;
5271 out->volume_r = right;
5272 pthread_mutex_unlock(&out->compr_mute_lock);
5273 return ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005274 }
Vikram Panduranga93f080e2017-06-07 18:16:14 -07005275 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP) {
Aalique Grahame22e49102018-12-18 14:23:57 -08005276 out->app_type_cfg.gain[0] = (int)(left * VOIP_PLAYBACK_VOLUME_MAX);
5277 out->app_type_cfg.gain[1] = (int)(right * VOIP_PLAYBACK_VOLUME_MAX);
5278 if (!out->standby) {
5279 audio_extn_utils_send_app_type_gain(out->dev,
5280 out->app_type_cfg.app_type,
5281 &out->app_type_cfg.gain[0]);
Zhou Song2b8f28f2017-09-11 10:51:38 +08005282 ret = out_set_voip_volume(stream, left, right);
Aalique Grahame22e49102018-12-18 14:23:57 -08005283 }
Zhou Song2b8f28f2017-09-11 10:51:38 +08005284 out->volume_l = left;
5285 out->volume_r = right;
5286 return ret;
Arun Mirpuri5d170872019-03-26 13:21:31 -07005287 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
5288 ALOGV("%s: MMAP set volume called", __func__);
5289 if (!out->standby)
5290 ret = out_set_mmap_volume(stream, left, right);
5291 out->volume_l = left;
5292 out->volume_r = right;
5293 return ret;
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05305294 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_LOW_LATENCY ||
Ramu Gottipati36547092018-12-28 11:32:09 +05305295 out->usecase == USECASE_AUDIO_PLAYBACK_DEEP_BUFFER ||
5296 out->usecase == USECASE_AUDIO_PLAYBACK_ULL) {
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05305297 /* Volume control for pcm playback */
5298 if (!out->standby)
5299 ret = out_set_pcm_volume(stream, left, right);
5300 else
5301 out->apply_volume = true;
5302
5303 out->volume_l = left;
5304 out->volume_r = right;
5305 return ret;
Derek Chenf13dd492018-11-13 14:53:51 -08005306 } else if (audio_extn_auto_hal_is_bus_device_usecase(out->usecase)) {
5307 ALOGV("%s: bus device set volume called", __func__);
5308 if (!out->standby)
5309 ret = out_set_pcm_volume(stream, left, right);
5310 out->volume_l = left;
5311 out->volume_r = right;
5312 return ret;
Eric Laurenta9024de2013-04-04 09:19:12 -07005313 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005314
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005315 return -ENOSYS;
5316}
5317
Zhou Songc9672822017-08-16 16:01:39 +08005318static void update_frames_written(struct stream_out *out, size_t bytes)
5319{
5320 size_t bpf = 0;
5321
5322 if (is_offload_usecase(out->usecase) && !out->non_blocking &&
5323 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD))
5324 bpf = 1;
5325 else if (!is_offload_usecase(out->usecase))
5326 bpf = audio_bytes_per_sample(out->format) *
5327 audio_channel_count_from_out_mask(out->channel_mask);
Zhou Song48453a02018-01-10 17:50:59 +08005328
5329 pthread_mutex_lock(&out->position_query_lock);
5330 if (bpf != 0) {
Zhou Songc9672822017-08-16 16:01:39 +08005331 out->written += bytes / bpf;
Zhou Song48453a02018-01-10 17:50:59 +08005332 clock_gettime(CLOCK_MONOTONIC, &out->writeAt);
5333 }
5334 pthread_mutex_unlock(&out->position_query_lock);
Zhou Songc9672822017-08-16 16:01:39 +08005335}
5336
Vignesh Kulothungana6927272019-02-20 15:17:07 -08005337int split_and_write_audio_haptic_data(struct stream_out *out,
5338 const void *buffer, size_t bytes_to_write)
5339{
5340 struct audio_device *adev = out->dev;
5341
5342 int ret = 0;
5343 size_t channel_count = audio_channel_count_from_out_mask(out->channel_mask);
5344 size_t bytes_per_sample = audio_bytes_per_sample(out->format);
5345 size_t frame_size = channel_count * bytes_per_sample;
5346 size_t frame_count = bytes_to_write / frame_size;
5347
5348 bool force_haptic_path =
5349 property_get_bool("vendor.audio.test_haptic", false);
5350
5351 // extract Haptics data from Audio buffer
5352 bool alloc_haptic_buffer = false;
5353 int haptic_channel_count = adev->haptics_config.channels;
5354 size_t haptic_frame_size = bytes_per_sample * haptic_channel_count;
5355 size_t audio_frame_size = frame_size - haptic_frame_size;
5356 size_t total_haptic_buffer_size = frame_count * haptic_frame_size;
5357
5358 if (adev->haptic_buffer == NULL) {
5359 alloc_haptic_buffer = true;
5360 } else if (adev->haptic_buffer_size < total_haptic_buffer_size) {
5361 free(adev->haptic_buffer);
5362 adev->haptic_buffer_size = 0;
5363 alloc_haptic_buffer = true;
5364 }
5365
5366 if (alloc_haptic_buffer) {
5367 adev->haptic_buffer = (uint8_t *)calloc(1, total_haptic_buffer_size);
Mingshu Pang1513f972019-05-24 12:43:51 +08005368 if(adev->haptic_buffer == NULL) {
5369 ALOGE("%s: failed to allocate mem for dev->haptic_buffer", __func__);
5370 return -ENOMEM;
5371 }
Vignesh Kulothungana6927272019-02-20 15:17:07 -08005372 adev->haptic_buffer_size = total_haptic_buffer_size;
5373 }
5374
5375 size_t src_index = 0, aud_index = 0, hap_index = 0;
5376 uint8_t *audio_buffer = (uint8_t *)buffer;
5377 uint8_t *haptic_buffer = adev->haptic_buffer;
5378
5379 // This is required for testing only. This works for stereo data only.
5380 // One channel is fed to audio stream and other to haptic stream for testing.
5381 if (force_haptic_path)
5382 audio_frame_size = haptic_frame_size = bytes_per_sample;
5383
5384 for (size_t i = 0; i < frame_count; i++) {
5385 memcpy(audio_buffer + aud_index, audio_buffer + src_index,
5386 audio_frame_size);
5387 aud_index += audio_frame_size;
5388 src_index += audio_frame_size;
5389
5390 if (adev->haptic_pcm)
5391 memcpy(haptic_buffer + hap_index, audio_buffer + src_index,
5392 haptic_frame_size);
5393 hap_index += haptic_frame_size;
5394 src_index += haptic_frame_size;
5395
5396 // This is required for testing only.
5397 // Discard haptic channel data.
5398 if (force_haptic_path)
5399 src_index += haptic_frame_size;
5400 }
5401
5402 // write to audio pipeline
5403 ret = pcm_write(out->pcm, (void *)audio_buffer,
5404 frame_count * audio_frame_size);
5405
5406 // write to haptics pipeline
5407 if (adev->haptic_pcm)
5408 ret = pcm_write(adev->haptic_pcm, (void *)adev->haptic_buffer,
5409 frame_count * haptic_frame_size);
5410
5411 return ret;
5412}
5413
Aalique Grahame22e49102018-12-18 14:23:57 -08005414#ifdef NO_AUDIO_OUT
5415static ssize_t out_write_for_no_output(struct audio_stream_out *stream,
5416 const void *buffer __unused, size_t bytes)
5417{
5418 struct stream_out *out = (struct stream_out *)stream;
5419
5420 /* No Output device supported other than BT for playback.
5421 * Sleep for the amount of buffer duration
5422 */
5423 lock_output_stream(out);
5424 usleep(bytes * 1000000 / audio_stream_out_frame_size(
5425 (const struct audio_stream_out *)&out->stream) /
5426 out_get_sample_rate(&out->stream.common));
5427 pthread_mutex_unlock(&out->lock);
5428 return bytes;
5429}
5430#endif
5431
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005432static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
5433 size_t bytes)
5434{
5435 struct stream_out *out = (struct stream_out *)stream;
5436 struct audio_device *adev = out->dev;
Eric Laurent6e895242013-09-05 16:10:57 -07005437 ssize_t ret = 0;
Satish Babu Patakokila715b1422017-08-22 14:33:21 +05305438 int channels = 0;
Arun Mirpuri7da752a2018-09-11 18:01:15 -07005439 const size_t frame_size = audio_stream_out_frame_size(stream);
5440 const size_t frames = (frame_size != 0) ? bytes / frame_size : bytes;
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +05305441 struct audio_usecase *usecase = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005442
Haynes Mathew George380745d2017-10-04 15:27:45 -07005443 ATRACE_BEGIN("out_write");
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005444 lock_output_stream(out);
Naresh Tanniru4c630392014-05-12 01:05:52 +05305445
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305446 if (CARD_STATUS_OFFLINE == out->card_status) {
Zhou Song0b2e5dc2015-03-16 14:41:38 +08005447
Dhananjay Kumarac341582017-02-23 23:42:25 +05305448 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Ashish Jainbbce4322016-02-16 13:25:27 +05305449 /*during SSR for compress usecase we should return error to flinger*/
Naresh Tanniru80659832014-06-04 18:17:56 +05305450 ALOGD(" copl %s: sound card is not active/SSR state", __func__);
5451 pthread_mutex_unlock(&out->lock);
Haynes Mathew George380745d2017-10-04 15:27:45 -07005452 ATRACE_END();
Naresh Tanniru80659832014-06-04 18:17:56 +05305453 return -ENETRESET;
Ashish Jainbbce4322016-02-16 13:25:27 +05305454 } else {
Ashish Jainbbce4322016-02-16 13:25:27 +05305455 ALOGD(" %s: sound card is not active/SSR state", __func__);
5456 ret= -EIO;
5457 goto exit;
Naresh Tanniru4c630392014-05-12 01:05:52 +05305458 }
5459 }
5460
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05305461 if (audio_extn_passthru_should_drop_data(out)) {
Ashish Jaind84fd6a2016-07-27 12:33:25 +05305462 ALOGV(" %s : Drop data as compress passthrough session is going on", __func__);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05305463 ret = -EIO;
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05305464 goto exit;
5465 }
5466
Haynes Mathew George16081042017-05-31 17:16:49 -07005467 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
5468 ret = -EINVAL;
5469 goto exit;
5470 }
5471
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05305472 if ((out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) &&
5473 !out->is_iec61937_info_available) {
5474
5475 if (!audio_extn_passthru_is_passthrough_stream(out)) {
5476 out->is_iec61937_info_available = true;
5477 } else if (audio_extn_passthru_is_enabled()) {
5478 audio_extn_passthru_update_stream_configuration(adev, out, buffer, bytes);
Manish Dewangan37864bc2017-06-09 12:28:37 +05305479 out->is_iec61937_info_available = true;
Manish Dewangan671a4202017-08-18 17:30:46 +05305480
5481 if((out->format == AUDIO_FORMAT_DTS) ||
5482 (out->format == AUDIO_FORMAT_DTS_HD)) {
5483 ret = audio_extn_passthru_update_dts_stream_configuration(out,
5484 buffer, bytes);
5485 if (ret) {
5486 if (ret != -ENOSYS) {
5487 out->is_iec61937_info_available = false;
5488 ALOGD("iec61937 transmission info not yet updated retry");
5489 }
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05305490 } else if (!out->standby) {
Manish Dewangan671a4202017-08-18 17:30:46 +05305491 /* if stream has started and after that there is
5492 * stream config change (iec transmission config)
5493 * then trigger select_device to update backend configuration.
5494 */
5495 out->stream_config_changed = true;
5496 pthread_mutex_lock(&adev->lock);
5497 select_devices(adev, out->usecase);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05305498 if (!audio_extn_passthru_is_supported_backend_edid_cfg(adev, out)) {
Weiyin Jiang29c08a42019-04-30 17:11:10 +08005499 pthread_mutex_unlock(&adev->lock);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05305500 ret = -EINVAL;
5501 goto exit;
5502 }
Manish Dewangan671a4202017-08-18 17:30:46 +05305503 pthread_mutex_unlock(&adev->lock);
5504 out->stream_config_changed = false;
5505 out->is_iec61937_info_available = true;
5506 }
5507 }
Satish Babu Patakokila715b1422017-08-22 14:33:21 +05305508
Garmond Leung317cbf12017-09-13 16:20:50 -07005509 if ((channels < (int)audio_channel_count_from_out_mask(out->channel_mask)) &&
Satish Babu Patakokila715b1422017-08-22 14:33:21 +05305510 (out->compr_config.codec->compr_passthr == PASSTHROUGH) &&
5511 (out->is_iec61937_info_available == true)) {
5512 ALOGE("%s: ERROR: Unsupported channel config in passthrough mode", __func__);
5513 ret = -EINVAL;
5514 goto exit;
5515 }
Manish Dewangan37864bc2017-06-09 12:28:37 +05305516 }
5517 }
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05305518
5519 if ((out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) &&
Florian Pfister1a84f312018-07-19 14:38:18 +02005520 (audio_extn_a2dp_source_is_suspended())) {
Aalique Grahame22e49102018-12-18 14:23:57 -08005521 if (!(out->devices &
5522 (AUDIO_DEVICE_OUT_SPEAKER | AUDIO_DEVICE_OUT_SPEAKER_SAFE))) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05305523 if (!(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05305524 ret = -EIO;
5525 goto exit;
5526 }
5527 }
5528 }
5529
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005530 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07005531 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08005532 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08005533 if (out->usecase == USECASE_COMPRESS_VOIP_CALL)
5534 ret = voice_extn_compress_voip_start_output_stream(out);
5535 else
5536 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08005537 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005538 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005539 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07005540 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005541 goto exit;
5542 }
Ashish Jain1b9b30c2017-05-18 20:57:40 +05305543 out->started = 1;
vivek mehtab72d08d2016-04-29 03:16:47 -07005544 if (last_known_cal_step != -1) {
5545 ALOGD("%s: retry previous failed cal level set", __func__);
5546 audio_hw_send_gain_dep_calibration(last_known_cal_step);
Preetam Singh Ranawatf4ae0222017-05-31 17:07:28 +05305547 last_known_cal_step = -1;
vivek mehtab72d08d2016-04-29 03:16:47 -07005548 }
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05305549
5550 if ((out->is_iec61937_info_available == true) &&
5551 (audio_extn_passthru_is_passthrough_stream(out))&&
5552 (!audio_extn_passthru_is_supported_backend_edid_cfg(adev, out))) {
5553 ret = -EINVAL;
5554 goto exit;
5555 }
Surendar Karkaf51b5842018-04-26 11:28:38 +05305556 if (out->set_dual_mono)
5557 audio_extn_send_dual_mono_mixing_coefficients(out);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005558 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005559
Ashish Jain81eb2a82015-05-13 10:52:34 +05305560 if (adev->is_channel_status_set == false && (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)){
Alexy Josephb1379942016-01-29 15:49:38 -08005561 audio_utils_set_hdmi_channel_status(out, (void *)buffer, bytes);
Ashish Jain81eb2a82015-05-13 10:52:34 +05305562 adev->is_channel_status_set = true;
5563 }
5564
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +05305565 if ((adev->use_old_pspd_mix_ctrl == true) &&
5566 (out->pspd_coeff_sent == false)) {
5567 /*
5568 * Need to resend pspd coefficients after stream started for
5569 * older kernel version as it does not save the coefficients
5570 * and also stream has to be started for coeff to apply.
5571 */
5572 usecase = get_usecase_from_list(adev, out->usecase);
5573 if (usecase != NULL) {
Chaithanya Krishna Bacharajuc9f99712019-04-16 15:32:52 +05305574 audio_extn_set_custom_mtmx_params_v2(adev, usecase, true);
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +05305575 out->pspd_coeff_sent = true;
5576 }
5577 }
5578
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07005579 if (is_offload_usecase(out->usecase)) {
Alexy Joseph01e54e62015-03-03 19:01:03 -08005580 ALOGVV("copl(%p): writing buffer (%zu bytes) to compress device", out, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005581 if (out->send_new_metadata) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07005582 ALOGD("copl(%p):send new gapless metadata", out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005583 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
5584 out->send_new_metadata = 0;
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05305585 if (out->send_next_track_params && out->is_compr_metadata_avail) {
5586 ALOGD("copl(%p):send next track params in gapless", out);
Weiyin Jiangd9b5a4e2019-07-18 17:24:21 +08005587 compress_set_next_track_param(out->compr, &(out->compr_config.codec->options));
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05305588 out->send_next_track_params = false;
5589 out->is_compr_metadata_avail = false;
5590 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005591 }
Dhananjay Kumarac341582017-02-23 23:42:25 +05305592 if (!(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
Ashish Jain83a6cc22016-06-28 14:34:17 +05305593 (out->convert_buffer) != NULL) {
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005594
Ashish Jain83a6cc22016-06-28 14:34:17 +05305595 if ((bytes > out->hal_fragment_size)) {
Ashish Jainf1eaa582016-05-23 20:54:24 +05305596 ALOGW("Error written bytes %zu > %d (fragment_size)",
Ashish Jain83a6cc22016-06-28 14:34:17 +05305597 bytes, out->hal_fragment_size);
Ashish Jainf1eaa582016-05-23 20:54:24 +05305598 pthread_mutex_unlock(&out->lock);
Haynes Mathew George380745d2017-10-04 15:27:45 -07005599 ATRACE_END();
Ashish Jainf1eaa582016-05-23 20:54:24 +05305600 return -EINVAL;
5601 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05305602 audio_format_t dst_format = out->hal_op_format;
5603 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05305604
Dieter Luecking5d57def2018-09-07 14:23:37 +02005605 /* prevent division-by-zero */
5606 uint32_t bitwidth_src = format_to_bitwidth_table[src_format];
5607 uint32_t bitwidth_dst = format_to_bitwidth_table[dst_format];
5608 if ((bitwidth_src == 0) || (bitwidth_dst == 0)) {
5609 ALOGE("%s: Error bitwidth == 0", __func__);
Ramu Gottipati02809682018-12-19 16:46:12 +05305610 pthread_mutex_unlock(&out->lock);
Dieter Luecking5d57def2018-09-07 14:23:37 +02005611 ATRACE_END();
5612 return -EINVAL;
5613 }
5614
Ashish Jainf1eaa582016-05-23 20:54:24 +05305615 uint32_t frames = bytes / format_to_bitwidth_table[src_format];
5616 uint32_t bytes_to_write = frames * format_to_bitwidth_table[dst_format];
5617
Ashish Jain83a6cc22016-06-28 14:34:17 +05305618 memcpy_by_audio_format(out->convert_buffer,
Ashish Jainf1eaa582016-05-23 20:54:24 +05305619 dst_format,
5620 buffer,
5621 src_format,
5622 frames);
5623
Ashish Jain83a6cc22016-06-28 14:34:17 +05305624 ret = compress_write(out->compr, out->convert_buffer,
Ashish Jainf1eaa582016-05-23 20:54:24 +05305625 bytes_to_write);
5626
5627 /*Convert written bytes in audio flinger format*/
5628 if (ret > 0)
5629 ret = ((ret * format_to_bitwidth_table[out->format]) /
5630 format_to_bitwidth_table[dst_format]);
5631 }
5632 } else
5633 ret = compress_write(out->compr, buffer, bytes);
5634
Zhou Songc9672822017-08-16 16:01:39 +08005635 if ((ret < 0 || ret == (ssize_t)bytes) && !out->non_blocking)
5636 update_frames_written(out, bytes);
5637
Dhanalakshmi Siddani37ca1d62014-08-20 12:28:34 +05305638 if (ret < 0)
5639 ret = -errno;
Weiyin Jiangcc60dbb2018-08-21 13:12:03 +08005640 ALOGVV("%s: writing buffer (%zu bytes) to compress device returned %d", __func__, bytes, (int)ret);
Ashish Jainb26edfb2016-08-25 00:10:11 +05305641 /*msg to cb thread only if non blocking write is enabled*/
5642 if (ret >= 0 && ret < (ssize_t)bytes && out->non_blocking) {
Sidipotu Ashok55820562014-02-10 16:16:38 +05305643 ALOGD("No space available in compress driver, post msg to cb thread");
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005644 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
Naresh Tanniru80659832014-06-04 18:17:56 +05305645 } else if (-ENETRESET == ret) {
5646 ALOGE("copl %s: received sound card offline state on compress write", __func__);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305647 out->card_status = CARD_STATUS_OFFLINE;
Naresh Tanniru80659832014-06-04 18:17:56 +05305648 pthread_mutex_unlock(&out->lock);
Dhananjay Kumar1248dd82017-07-28 21:22:16 +05305649 out_on_error(&out->stream.common);
Haynes Mathew George380745d2017-10-04 15:27:45 -07005650 ATRACE_END();
Naresh Tanniru80659832014-06-04 18:17:56 +05305651 return ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005652 }
Ashish Jain5106d362016-05-11 19:23:33 +05305653
Dhanalakshmi Siddania6b76c72016-09-09 18:10:31 +05305654 /* Call compr start only when non-zero bytes of data is there to be rendered */
5655 if (!out->playback_started && ret > 0) {
5656 int status = compress_start(out->compr);
5657 if (status < 0) {
5658 ret = status;
5659 ALOGE("%s: compr start failed with err %d", __func__, errno);
5660 goto exit;
5661 }
Alexy Joseph7de344d2015-03-30 10:40:03 -07005662 audio_extn_dts_eagle_fade(adev, true, out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005663 out->playback_started = 1;
5664 out->offload_state = OFFLOAD_STATE_PLAYING;
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08005665
5666 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
5667 popcount(out->channel_mask),
5668 out->playback_started);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005669 }
5670 pthread_mutex_unlock(&out->lock);
Haynes Mathew George380745d2017-10-04 15:27:45 -07005671 ATRACE_END();
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005672 return ret;
5673 } else {
5674 if (out->pcm) {
Arun Mirpuri7da752a2018-09-11 18:01:15 -07005675 size_t bytes_to_write = bytes;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005676 if (out->muted)
5677 memset((void *)buffer, 0, bytes);
Arun Mirpuri7da752a2018-09-11 18:01:15 -07005678 ALOGV("%s: frames=%zu, frame_size=%zu, bytes_to_write=%zu",
5679 __func__, frames, frame_size, bytes_to_write);
5680
Aalique Grahame22e49102018-12-18 14:23:57 -08005681 if (out->usecase == USECASE_INCALL_MUSIC_UPLINK ||
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07005682 out->usecase == USECASE_INCALL_MUSIC_UPLINK2 ||
5683 (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP &&
5684 !audio_extn_utils_is_vendor_enhanced_fwk())) {
Arun Mirpuri7da752a2018-09-11 18:01:15 -07005685 size_t channel_count = audio_channel_count_from_out_mask(out->channel_mask);
5686 int16_t *src = (int16_t *)buffer;
5687 int16_t *dst = (int16_t *)buffer;
5688
Yunfei Zhanga6cd66d2019-07-22 16:15:36 +08005689 LOG_ALWAYS_FATAL_IF(channel_count > 2 ||
Arun Mirpuri7da752a2018-09-11 18:01:15 -07005690 out->format != AUDIO_FORMAT_PCM_16_BIT,
Aalique Grahame22e49102018-12-18 14:23:57 -08005691 "out_write called for %s use case with wrong properties",
5692 use_case_table[out->usecase]);
Arun Mirpuri7da752a2018-09-11 18:01:15 -07005693
5694 /*
5695 * FIXME: this can be removed once audio flinger mixer supports
5696 * mono output
5697 */
5698
5699 /*
5700 * Code below goes over each frame in the buffer and adds both
5701 * L and R samples and then divides by 2 to convert to mono
5702 */
Yunfei Zhanga6cd66d2019-07-22 16:15:36 +08005703 if (channel_count == 2) {
5704 for (size_t i = 0; i < frames ; i++, dst++, src += 2) {
5705 *dst = (int16_t)(((int32_t)src[0] + (int32_t)src[1]) >> 1);
5706 }
5707 bytes_to_write /= 2;
Arun Mirpuri7da752a2018-09-11 18:01:15 -07005708 }
Arun Mirpuri7da752a2018-09-11 18:01:15 -07005709 }
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05305710 ALOGVV("%s: writing buffer (%zu bytes) to pcm device", __func__, bytes);
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07005711
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005712 long ns = 0;
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07005713
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005714 if (out->config.rate)
5715 ns = pcm_bytes_to_frames(out->pcm, bytes)*1000000000LL/
5716 out->config.rate;
5717
Arun Mirpuri7da752a2018-09-11 18:01:15 -07005718 request_out_focus(out, ns);
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005719 bool use_mmap = is_mmap_usecase(out->usecase) || out->realtime;
5720
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005721 if (use_mmap)
Arun Mirpuri7da752a2018-09-11 18:01:15 -07005722 ret = pcm_mmap_write(out->pcm, (void *)buffer, bytes_to_write);
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005723 else if (out->hal_op_format != out->hal_ip_format &&
Ashish Jain83a6cc22016-06-28 14:34:17 +05305724 out->convert_buffer != NULL) {
5725
5726 memcpy_by_audio_format(out->convert_buffer,
5727 out->hal_op_format,
5728 buffer,
5729 out->hal_ip_format,
5730 out->config.period_size * out->config.channels);
5731
5732 ret = pcm_write(out->pcm, out->convert_buffer,
5733 (out->config.period_size *
5734 out->config.channels *
5735 format_to_bitwidth_table[out->hal_op_format]));
5736 } else {
Aditya Bavanarid4db8ee2017-05-29 21:08:03 +05305737 /*
5738 * To avoid underrun in DSP when the application is not pumping
5739 * data at required rate, check for the no. of bytes and ignore
5740 * pcm_write if it is less than actual buffer size.
5741 * It is a work around to a change in compress VOIP driver.
5742 */
5743 if ((out->flags & AUDIO_OUTPUT_FLAG_VOIP_RX) &&
5744 bytes < (out->config.period_size * out->config.channels *
5745 audio_bytes_per_sample(out->format))) {
5746 size_t voip_buf_size =
5747 out->config.period_size * out->config.channels *
5748 audio_bytes_per_sample(out->format);
5749 ALOGE("%s:VOIP underrun: bytes received %zu, required:%zu\n",
5750 __func__, bytes, voip_buf_size);
5751 usleep(((uint64_t)voip_buf_size - bytes) *
5752 1000000 / audio_stream_out_frame_size(stream) /
5753 out_get_sample_rate(&out->stream.common));
5754 ret = 0;
Vignesh Kulothungana6927272019-02-20 15:17:07 -08005755 } else {
5756 if (out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS)
5757 ret = split_and_write_audio_haptic_data(out, buffer, bytes);
5758 else
5759 ret = pcm_write(out->pcm, (void *)buffer, bytes_to_write);
5760 }
Ashish Jain83a6cc22016-06-28 14:34:17 +05305761 }
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07005762
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005763 release_out_focus(out);
5764
Dhanalakshmi Siddani37ca1d62014-08-20 12:28:34 +05305765 if (ret < 0)
5766 ret = -errno;
Zhou Songc9672822017-08-16 16:01:39 +08005767 else if (ret > 0)
Ashish Jain83a6cc22016-06-28 14:34:17 +05305768 ret = -EINVAL;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005769 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005770 }
5771
5772exit:
Zhou Songc9672822017-08-16 16:01:39 +08005773 update_frames_written(out, bytes);
Naresh Tanniru4c630392014-05-12 01:05:52 +05305774 if (-ENETRESET == ret) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305775 out->card_status = CARD_STATUS_OFFLINE;
Naresh Tanniru4c630392014-05-12 01:05:52 +05305776 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005777 pthread_mutex_unlock(&out->lock);
5778
5779 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07005780 if (out->pcm)
Alexy Josephb1379942016-01-29 15:49:38 -08005781 ALOGE("%s: error %d, %s", __func__, (int)ret, pcm_get_error(out->pcm));
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05305782 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05305783 pthread_mutex_lock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05305784 voice_extn_compress_voip_close_output_stream(&out->stream.common);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05305785 out->started = 0;
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05305786 pthread_mutex_unlock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05305787 out->standby = true;
5788 }
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305789 out_on_error(&out->stream.common);
Dieter Luecking5d57def2018-09-07 14:23:37 +02005790 if (!(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
5791 /* prevent division-by-zero */
5792 uint32_t stream_size = audio_stream_out_frame_size(stream);
5793 uint32_t srate = out_get_sample_rate(&out->stream.common);
Vidyakumar Athotaa9d3a5f2017-08-09 12:13:05 -07005794
Dieter Luecking5d57def2018-09-07 14:23:37 +02005795 if ((stream_size == 0) || (srate == 0)) {
5796 ALOGE("%s: stream_size= %d, srate = %d", __func__, stream_size, srate);
5797 ATRACE_END();
5798 return -EINVAL;
5799 }
5800 usleep((uint64_t)bytes * 1000000 / stream_size / srate);
5801 }
Vidyakumar Athotaa9d3a5f2017-08-09 12:13:05 -07005802 if (audio_extn_passthru_is_passthrough_stream(out)) {
Rajshekar Eashwarappa88834522018-04-02 17:20:15 +05305803 //ALOGE("%s: write error, ret = %zd", __func__, ret);
Haynes Mathew George380745d2017-10-04 15:27:45 -07005804 ATRACE_END();
Vidyakumar Athotaa9d3a5f2017-08-09 12:13:05 -07005805 return ret;
5806 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005807 }
Haynes Mathew George380745d2017-10-04 15:27:45 -07005808 ATRACE_END();
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005809 return bytes;
5810}
5811
5812static int out_get_render_position(const struct audio_stream_out *stream,
5813 uint32_t *dsp_frames)
5814{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005815 struct stream_out *out = (struct stream_out *)stream;
Zhou Song32a556e2015-05-05 10:46:56 +08005816
5817 if (dsp_frames == NULL)
5818 return -EINVAL;
5819
5820 *dsp_frames = 0;
5821 if (is_offload_usecase(out->usecase)) {
Mingming Yin9e348b52014-11-19 16:18:55 -08005822 ssize_t ret = 0;
Ashish Jain5106d362016-05-11 19:23:33 +05305823
5824 /* Below piece of code is not guarded against any lock beacuse audioFliner serializes
5825 * this operation and adev_close_output_stream(where out gets reset).
5826 */
Dhananjay Kumarac341582017-02-23 23:42:25 +05305827 if (!out->non_blocking && !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Zhou Song48453a02018-01-10 17:50:59 +08005828 *dsp_frames = get_actual_pcm_frames_rendered(out, NULL);
Ashish Jain5106d362016-05-11 19:23:33 +05305829 ALOGVV("dsp_frames %d sampleRate %d",(int)*dsp_frames,out->sample_rate);
Haynes Mathew Georgeb0f5dc32017-10-06 18:35:12 -07005830 adjust_frames_for_device_delay(out, dsp_frames);
Ashish Jain5106d362016-05-11 19:23:33 +05305831 return 0;
5832 }
5833
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005834 lock_output_stream(out);
Ashish Jain5106d362016-05-11 19:23:33 +05305835 if (out->compr != NULL && out->non_blocking) {
Naresh Tanniru80659832014-06-04 18:17:56 +05305836 ret = compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005837 &out->sample_rate);
Dhanalakshmi Siddani37ca1d62014-08-20 12:28:34 +05305838 if (ret < 0)
5839 ret = -errno;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005840 ALOGVV("%s rendered frames %d sample_rate %d",
Ashish Jain5106d362016-05-11 19:23:33 +05305841 __func__, *dsp_frames, out->sample_rate);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005842 }
Naresh Tanniru80659832014-06-04 18:17:56 +05305843 if (-ENETRESET == ret) {
5844 ALOGE(" ERROR: sound card not active Unable to get time stamp from compress driver");
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305845 out->card_status = CARD_STATUS_OFFLINE;
5846 ret = -EINVAL;
Naresh Tanniru80659832014-06-04 18:17:56 +05305847 } else if(ret < 0) {
5848 ALOGE(" ERROR: Unable to get time stamp from compress driver");
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305849 ret = -EINVAL;
5850 } else if (out->card_status == CARD_STATUS_OFFLINE) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05305851 /*
5852 * Handle corner case where compress session is closed during SSR
5853 * and timestamp is queried
5854 */
5855 ALOGE(" ERROR: sound card not active, return error");
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305856 ret = -EINVAL;
Manisha Agarwal7b3e3772019-02-20 14:33:45 +05305857 } else if (out->prev_card_status_offline) {
5858 ALOGE("ERROR: previously sound card was offline,return error");
5859 ret = -EINVAL;
Naresh Tanniru80659832014-06-04 18:17:56 +05305860 } else {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305861 ret = 0;
Haynes Mathew Georgeb0f5dc32017-10-06 18:35:12 -07005862 adjust_frames_for_device_delay(out, dsp_frames);
Naresh Tanniru80659832014-06-04 18:17:56 +05305863 }
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305864 pthread_mutex_unlock(&out->lock);
5865 return ret;
Zhou Song32a556e2015-05-05 10:46:56 +08005866 } else if (audio_is_linear_pcm(out->format)) {
5867 *dsp_frames = out->written;
Haynes Mathew Georgeb0f5dc32017-10-06 18:35:12 -07005868 adjust_frames_for_device_delay(out, dsp_frames);
Zhou Song32a556e2015-05-05 10:46:56 +08005869 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005870 } else
5871 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005872}
5873
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07005874static int out_add_audio_effect(const struct audio_stream *stream __unused,
5875 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005876{
5877 return 0;
5878}
5879
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07005880static int out_remove_audio_effect(const struct audio_stream *stream __unused,
5881 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005882{
5883 return 0;
5884}
5885
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07005886static int out_get_next_write_timestamp(const struct audio_stream_out *stream __unused,
5887 int64_t *timestamp __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005888{
Satya Krishna Pindiprolib6655542017-07-03 19:38:19 +05305889 return -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005890}
5891
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07005892static int out_get_presentation_position(const struct audio_stream_out *stream,
5893 uint64_t *frames, struct timespec *timestamp)
5894{
5895 struct stream_out *out = (struct stream_out *)stream;
pavance65c2fe2017-10-18 17:52:01 +05305896 int ret = -ENODATA;
Eric Laurent949a0892013-09-20 09:20:13 -07005897 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07005898
Ashish Jain5106d362016-05-11 19:23:33 +05305899 /* below piece of code is not guarded against any lock because audioFliner serializes
5900 * this operation and adev_close_output_stream( where out gets reset).
5901 */
5902 if (is_offload_usecase(out->usecase) && !out->non_blocking &&
Dhananjay Kumarac341582017-02-23 23:42:25 +05305903 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Zhou Song48453a02018-01-10 17:50:59 +08005904 *frames = get_actual_pcm_frames_rendered(out, timestamp);
Ashish Jain5106d362016-05-11 19:23:33 +05305905 ALOGVV("frames %lld playedat %lld",(long long int)*frames,
5906 timestamp->tv_sec * 1000000LL + timestamp->tv_nsec / 1000);
5907 return 0;
5908 }
5909
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005910 lock_output_stream(out);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07005911
Ashish Jain5106d362016-05-11 19:23:33 +05305912 if (is_offload_usecase(out->usecase) && out->compr != NULL && out->non_blocking) {
5913 ret = compress_get_tstamp(out->compr, &dsp_frames,
5914 &out->sample_rate);
yidongh0515e042017-07-06 15:00:34 +08005915 // Adjustment accounts for A2dp encoder latency with offload usecases
5916 // Note: Encoder latency is returned in ms.
5917 if (AUDIO_DEVICE_OUT_ALL_A2DP & out->devices) {
5918 unsigned long offset =
5919 (audio_extn_a2dp_get_encoder_latency() * out->sample_rate / 1000);
5920 dsp_frames = (dsp_frames > offset) ? (dsp_frames - offset) : 0;
5921 }
Ashish Jain5106d362016-05-11 19:23:33 +05305922 ALOGVV("%s rendered frames %ld sample_rate %d",
5923 __func__, dsp_frames, out->sample_rate);
5924 *frames = dsp_frames;
5925 if (ret < 0)
5926 ret = -errno;
5927 if (-ENETRESET == ret) {
5928 ALOGE(" ERROR: sound card not active Unable to get time stamp from compress driver");
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305929 out->card_status = CARD_STATUS_OFFLINE;
Ashish Jain5106d362016-05-11 19:23:33 +05305930 ret = -EINVAL;
5931 } else
5932 ret = 0;
5933 /* this is the best we can do */
5934 clock_gettime(CLOCK_MONOTONIC, timestamp);
Eric Laurent949a0892013-09-20 09:20:13 -07005935 } else {
5936 if (out->pcm) {
Weiyin Jiangd4633762018-03-16 12:05:03 +08005937 unsigned int avail;
5938 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
5939 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
George Gao62ebc722019-07-29 16:29:44 -07005940
5941 uint64_t signed_frames = 0;
Weiyin Jiang0d25bcf2019-08-30 15:27:23 +08005942 uint64_t frames_temp = 0;
George Gao62ebc722019-07-29 16:29:44 -07005943
Weiyin Jiang0d25bcf2019-08-30 15:27:23 +08005944 frames_temp = (kernel_buffer_size > avail) ? (kernel_buffer_size - avail) : 0;
5945 if (out->written >= frames_temp)
5946 signed_frames = out->written - frames_temp;
George Gao62ebc722019-07-29 16:29:44 -07005947
Weiyin Jiangd4633762018-03-16 12:05:03 +08005948 // This adjustment accounts for buffering after app processor.
5949 // It is based on estimated DSP latency per use case, rather than exact.
Weiyin Jiang0d25bcf2019-08-30 15:27:23 +08005950 frames_temp = platform_render_latency(out->usecase) * out->sample_rate / 1000000LL;
5951 if (signed_frames >= frames_temp)
5952 signed_frames -= frames_temp;
Aniket Kumar Lataff613152017-07-18 18:19:21 -07005953
Weiyin Jiangd4633762018-03-16 12:05:03 +08005954 // Adjustment accounts for A2dp encoder latency with non offload usecases
5955 // Note: Encoder latency is returned in ms, while platform_render_latency in us.
5956 if (AUDIO_DEVICE_OUT_ALL_A2DP & out->devices) {
Weiyin Jiang0d25bcf2019-08-30 15:27:23 +08005957 frames_temp = audio_extn_a2dp_get_encoder_latency() * out->sample_rate / 1000;
5958 if (signed_frames >= frames_temp)
5959 signed_frames -= frames_temp;
Weiyin Jiangd4633762018-03-16 12:05:03 +08005960 }
5961
5962 // It would be unusual for this value to be negative, but check just in case ...
George Gao62ebc722019-07-29 16:29:44 -07005963 *frames = signed_frames;
5964 ret = 0;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07005965 }
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305966 } else if (out->card_status == CARD_STATUS_OFFLINE) {
Ashish Jainbbce4322016-02-16 13:25:27 +05305967 *frames = out->written;
5968 clock_gettime(CLOCK_MONOTONIC, timestamp);
Manisha Agarwal7b3e3772019-02-20 14:33:45 +05305969 if (is_offload_usecase(out->usecase))
5970 ret = -EINVAL;
5971 else
5972 ret = 0;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07005973 }
5974 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07005975 pthread_mutex_unlock(&out->lock);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07005976 return ret;
5977}
5978
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005979static int out_set_callback(struct audio_stream_out *stream,
5980 stream_callback_t callback, void *cookie)
5981{
5982 struct stream_out *out = (struct stream_out *)stream;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08005983 int ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005984
5985 ALOGV("%s", __func__);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005986 lock_output_stream(out);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08005987 out->client_callback = callback;
5988 out->client_cookie = cookie;
5989 if (out->adsp_hdlr_stream_handle) {
5990 ret = audio_extn_adsp_hdlr_stream_set_callback(
5991 out->adsp_hdlr_stream_handle,
5992 callback,
5993 cookie);
5994 if (ret)
5995 ALOGW("%s:adsp hdlr callback registration failed %d",
5996 __func__, ret);
5997 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005998 pthread_mutex_unlock(&out->lock);
5999 return 0;
6000}
6001
6002static int out_pause(struct audio_stream_out* stream)
6003{
6004 struct stream_out *out = (struct stream_out *)stream;
6005 int status = -ENOSYS;
6006 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07006007 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07006008 ALOGD("copl(%p):pause compress driver", out);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006009 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006010 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306011 if (out->card_status != CARD_STATUS_OFFLINE)
Naresh Tanniru80659832014-06-04 18:17:56 +05306012 status = compress_pause(out->compr);
6013
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006014 out->offload_state = OFFLOAD_STATE_PAUSED;
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08006015
Mingming Yin21854652016-04-13 11:54:02 -07006016 if (audio_extn_passthru_is_active()) {
6017 ALOGV("offload use case, pause passthru");
6018 audio_extn_passthru_on_pause(out);
6019 }
6020
Dhanalakshmi Siddani79415e72015-03-23 11:54:47 +05306021 audio_extn_dts_eagle_fade(adev, false, out);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08006022 audio_extn_dts_notify_playback_state(out->usecase, 0,
6023 out->sample_rate, popcount(out->channel_mask),
6024 0);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006025 }
6026 pthread_mutex_unlock(&out->lock);
6027 }
6028 return status;
6029}
6030
6031static int out_resume(struct audio_stream_out* stream)
6032{
6033 struct stream_out *out = (struct stream_out *)stream;
6034 int status = -ENOSYS;
6035 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07006036 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07006037 ALOGD("copl(%p):resume compress driver", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006038 status = 0;
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006039 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006040 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306041 if (out->card_status != CARD_STATUS_OFFLINE) {
Naresh Tanniru80659832014-06-04 18:17:56 +05306042 status = compress_resume(out->compr);
Mingming Yin21854652016-04-13 11:54:02 -07006043 }
6044 if (!status) {
6045 out->offload_state = OFFLOAD_STATE_PLAYING;
6046 }
Dhanalakshmi Siddani79415e72015-03-23 11:54:47 +05306047 audio_extn_dts_eagle_fade(adev, true, out);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08006048 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
6049 popcount(out->channel_mask), 1);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006050 }
6051 pthread_mutex_unlock(&out->lock);
6052 }
6053 return status;
6054}
6055
6056static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
6057{
6058 struct stream_out *out = (struct stream_out *)stream;
6059 int status = -ENOSYS;
6060 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07006061 if (is_offload_usecase(out->usecase)) {
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006062 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006063 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
6064 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
6065 else
6066 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
6067 pthread_mutex_unlock(&out->lock);
6068 }
6069 return status;
6070}
6071
6072static int out_flush(struct audio_stream_out* stream)
6073{
6074 struct stream_out *out = (struct stream_out *)stream;
6075 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07006076 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07006077 ALOGD("copl(%p):calling compress flush", out);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006078 lock_output_stream(out);
Haynes Mathew Georgeafe54d82016-09-21 14:39:19 -07006079 if (out->offload_state == OFFLOAD_STATE_PAUSED) {
6080 stop_compressed_output_l(out);
Haynes Mathew Georgeafe54d82016-09-21 14:39:19 -07006081 } else {
6082 ALOGW("%s called in invalid state %d", __func__, out->offload_state);
6083 }
Weiyin Jiang547e4152017-09-14 17:24:18 +08006084 out->written = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006085 pthread_mutex_unlock(&out->lock);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07006086 ALOGD("copl(%p):out of compress flush", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006087 return 0;
6088 }
6089 return -ENOSYS;
6090}
6091
Haynes Mathew George16081042017-05-31 17:16:49 -07006092static int out_stop(const struct audio_stream_out* stream)
6093{
6094 struct stream_out *out = (struct stream_out *)stream;
6095 struct audio_device *adev = out->dev;
6096 int ret = -ENOSYS;
6097
6098 ALOGV("%s", __func__);
6099 pthread_mutex_lock(&adev->lock);
6100 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP && !out->standby &&
6101 out->playback_started && out->pcm != NULL) {
6102 pcm_stop(out->pcm);
6103 ret = stop_output_stream(out);
6104 out->playback_started = false;
6105 }
6106 pthread_mutex_unlock(&adev->lock);
6107 return ret;
6108}
6109
6110static int out_start(const struct audio_stream_out* stream)
6111{
6112 struct stream_out *out = (struct stream_out *)stream;
6113 struct audio_device *adev = out->dev;
6114 int ret = -ENOSYS;
6115
6116 ALOGV("%s", __func__);
6117 pthread_mutex_lock(&adev->lock);
6118 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP && !out->standby &&
6119 !out->playback_started && out->pcm != NULL) {
6120 ret = start_output_stream(out);
6121 if (ret == 0) {
6122 out->playback_started = true;
6123 }
6124 }
6125 pthread_mutex_unlock(&adev->lock);
6126 return ret;
6127}
6128
6129/*
6130 * Modify config->period_count based on min_size_frames
6131 */
6132static void adjust_mmap_period_count(struct pcm_config *config, int32_t min_size_frames)
6133{
6134 int periodCountRequested = (min_size_frames + config->period_size - 1)
6135 / config->period_size;
6136 int periodCount = MMAP_PERIOD_COUNT_MIN;
6137
6138 ALOGV("%s original config.period_size = %d config.period_count = %d",
6139 __func__, config->period_size, config->period_count);
6140
6141 while (periodCount < periodCountRequested && (periodCount * 2) < MMAP_PERIOD_COUNT_MAX) {
6142 periodCount *= 2;
6143 }
6144 config->period_count = periodCount;
6145
6146 ALOGV("%s requested config.period_count = %d", __func__, config->period_count);
6147}
6148
Phil Burkfe17efd2019-03-25 10:23:35 -07006149// Read offset for the positional timestamp from a persistent vendor property.
6150// This is to workaround apparent inaccuracies in the timing information that
6151// is used by the AAudio timing model. The inaccuracies can cause glitches.
6152static int64_t get_mmap_out_time_offset() {
6153 const int32_t kDefaultOffsetMicros = 0;
6154 int32_t mmap_time_offset_micros = property_get_int32(
Weiyin Jiangbd68b4d2019-05-17 16:29:50 +08006155 "persist.vendor.audio.out_mmap_delay_micros", kDefaultOffsetMicros);
Phil Burkfe17efd2019-03-25 10:23:35 -07006156 ALOGI("mmap_time_offset_micros = %d for output", mmap_time_offset_micros);
6157 return mmap_time_offset_micros * (int64_t)1000;
6158}
6159
Haynes Mathew George16081042017-05-31 17:16:49 -07006160static int out_create_mmap_buffer(const struct audio_stream_out *stream,
6161 int32_t min_size_frames,
6162 struct audio_mmap_buffer_info *info)
6163{
6164 struct stream_out *out = (struct stream_out *)stream;
6165 struct audio_device *adev = out->dev;
6166 int ret = 0;
Aalique Grahame1f123102017-10-12 10:38:32 -07006167 unsigned int offset1 = 0;
6168 unsigned int frames1 = 0;
Haynes Mathew George16081042017-05-31 17:16:49 -07006169 const char *step = "";
Haynes Mathew Georgeef514882017-05-01 17:46:23 -07006170 uint32_t mmap_size;
Arun Mirpuri5d170872019-03-26 13:21:31 -07006171 uint32_t buffer_size;
Haynes Mathew George16081042017-05-31 17:16:49 -07006172
Arun Mirpuri5d170872019-03-26 13:21:31 -07006173 ALOGD("%s", __func__);
Sharad Sanglec6f32552018-05-04 16:15:38 +05306174 lock_output_stream(out);
Haynes Mathew George16081042017-05-31 17:16:49 -07006175 pthread_mutex_lock(&adev->lock);
6176
Sharad Sanglec6f32552018-05-04 16:15:38 +05306177 if (CARD_STATUS_OFFLINE == out->card_status ||
6178 CARD_STATUS_OFFLINE == adev->card_status) {
6179 ALOGW("out->card_status or adev->card_status offline, try again");
6180 ret = -EIO;
6181 goto exit;
6182 }
Haynes Mathew George16081042017-05-31 17:16:49 -07006183 if (info == NULL || min_size_frames == 0) {
6184 ALOGE("%s: info = %p, min_size_frames = %d", __func__, info, min_size_frames);
6185 ret = -EINVAL;
6186 goto exit;
6187 }
6188 if (out->usecase != USECASE_AUDIO_PLAYBACK_MMAP || !out->standby) {
6189 ALOGE("%s: usecase = %d, standby = %d", __func__, out->usecase, out->standby);
6190 ret = -ENOSYS;
6191 goto exit;
6192 }
6193 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
6194 if (out->pcm_device_id < 0) {
6195 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
6196 __func__, out->pcm_device_id, out->usecase);
6197 ret = -EINVAL;
6198 goto exit;
6199 }
6200
6201 adjust_mmap_period_count(&out->config, min_size_frames);
6202
Arun Mirpuri5d170872019-03-26 13:21:31 -07006203 ALOGD("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
Haynes Mathew George16081042017-05-31 17:16:49 -07006204 __func__, adev->snd_card, out->pcm_device_id, out->config.channels);
6205 out->pcm = pcm_open(adev->snd_card, out->pcm_device_id,
6206 (PCM_OUT | PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC), &out->config);
Satish Babu Patakokila54ce83d2018-07-06 18:00:37 +05306207 if (errno == ENETRESET && !pcm_is_ready(out->pcm)) {
Sharad Sanglec6f32552018-05-04 16:15:38 +05306208 ALOGE("%s: pcm_open failed errno:%d\n", __func__, errno);
6209 out->card_status = CARD_STATUS_OFFLINE;
6210 adev->card_status = CARD_STATUS_OFFLINE;
6211 ret = -EIO;
6212 goto exit;
6213 }
6214
Haynes Mathew George16081042017-05-31 17:16:49 -07006215 if (out->pcm == NULL || !pcm_is_ready(out->pcm)) {
6216 step = "open";
6217 ret = -ENODEV;
6218 goto exit;
6219 }
6220 ret = pcm_mmap_begin(out->pcm, &info->shared_memory_address, &offset1, &frames1);
6221 if (ret < 0) {
6222 step = "begin";
6223 goto exit;
6224 }
6225 info->buffer_size_frames = pcm_get_buffer_size(out->pcm);
Arun Mirpuri5d170872019-03-26 13:21:31 -07006226 buffer_size = pcm_frames_to_bytes(out->pcm, info->buffer_size_frames);
Haynes Mathew George16081042017-05-31 17:16:49 -07006227 info->burst_size_frames = out->config.period_size;
Haynes Mathew Georgeef514882017-05-01 17:46:23 -07006228 ret = platform_get_mmap_data_fd(adev->platform,
6229 out->pcm_device_id, 0 /*playback*/,
6230 &info->shared_memory_fd,
6231 &mmap_size);
6232 if (ret < 0) {
Arun Mirpuri5d170872019-03-26 13:21:31 -07006233 // Fall back to non exclusive mode
6234 info->shared_memory_fd = pcm_get_poll_fd(out->pcm);
6235 } else {
Phil Burkd898ba62019-06-20 12:49:01 -07006236 out->mmap_shared_memory_fd = info->shared_memory_fd; // for closing later
6237 ALOGV("%s: opened mmap_shared_memory_fd = %d", __func__, out->mmap_shared_memory_fd);
6238
Arun Mirpuri5d170872019-03-26 13:21:31 -07006239 if (mmap_size < buffer_size) {
6240 step = "mmap";
6241 goto exit;
6242 }
6243 // FIXME: indicate exclusive mode support by returning a negative buffer size
6244 info->buffer_size_frames *= -1;
Haynes Mathew Georgeef514882017-05-01 17:46:23 -07006245 }
Haynes Mathew George16081042017-05-31 17:16:49 -07006246 memset(info->shared_memory_address, 0, pcm_frames_to_bytes(out->pcm,
Haynes Mathew Georgeef514882017-05-01 17:46:23 -07006247 info->buffer_size_frames));
Haynes Mathew George16081042017-05-31 17:16:49 -07006248
6249 ret = pcm_mmap_commit(out->pcm, 0, MMAP_PERIOD_SIZE);
6250 if (ret < 0) {
6251 step = "commit";
6252 goto exit;
6253 }
6254
Phil Burkfe17efd2019-03-25 10:23:35 -07006255 out->mmap_time_offset_nanos = get_mmap_out_time_offset();
6256
Haynes Mathew George16081042017-05-31 17:16:49 -07006257 out->standby = false;
6258 ret = 0;
6259
Arun Mirpuri5d170872019-03-26 13:21:31 -07006260 ALOGD("%s: got mmap buffer address %p info->buffer_size_frames %d",
Haynes Mathew George16081042017-05-31 17:16:49 -07006261 __func__, info->shared_memory_address, info->buffer_size_frames);
6262
6263exit:
6264 if (ret != 0) {
6265 if (out->pcm == NULL) {
6266 ALOGE("%s: %s - %d", __func__, step, ret);
6267 } else {
6268 ALOGE("%s: %s %s", __func__, step, pcm_get_error(out->pcm));
6269 pcm_close(out->pcm);
6270 out->pcm = NULL;
6271 }
6272 }
6273 pthread_mutex_unlock(&adev->lock);
Sharad Sanglec6f32552018-05-04 16:15:38 +05306274 pthread_mutex_unlock(&out->lock);
Haynes Mathew George16081042017-05-31 17:16:49 -07006275 return ret;
6276}
6277
6278static int out_get_mmap_position(const struct audio_stream_out *stream,
6279 struct audio_mmap_position *position)
6280{
6281 struct stream_out *out = (struct stream_out *)stream;
6282 ALOGVV("%s", __func__);
6283 if (position == NULL) {
6284 return -EINVAL;
6285 }
6286 if (out->usecase != USECASE_AUDIO_PLAYBACK_MMAP) {
Haynes Mathew George4ab3ba92017-12-11 14:49:43 -08006287 ALOGE("%s: called on %s", __func__, use_case_table[out->usecase]);
Haynes Mathew George16081042017-05-31 17:16:49 -07006288 return -ENOSYS;
6289 }
6290 if (out->pcm == NULL) {
6291 return -ENOSYS;
6292 }
6293
6294 struct timespec ts = { 0, 0 };
6295 int ret = pcm_mmap_get_hw_ptr(out->pcm, (unsigned int *)&position->position_frames, &ts);
6296 if (ret < 0) {
6297 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
6298 return ret;
6299 }
Phil Burkfe17efd2019-03-25 10:23:35 -07006300 position->time_nanoseconds = ts.tv_sec*1000000000LL + ts.tv_nsec
6301 + out->mmap_time_offset_nanos;
Haynes Mathew George16081042017-05-31 17:16:49 -07006302 return 0;
6303}
6304
6305
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006306/** audio_stream_in implementation **/
6307static uint32_t in_get_sample_rate(const struct audio_stream *stream)
6308{
6309 struct stream_in *in = (struct stream_in *)stream;
6310
6311 return in->config.rate;
6312}
6313
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07006314static int in_set_sample_rate(struct audio_stream *stream __unused,
6315 uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006316{
6317 return -ENOSYS;
6318}
6319
6320static size_t in_get_buffer_size(const struct audio_stream *stream)
6321{
6322 struct stream_in *in = (struct stream_in *)stream;
6323
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08006324 if(in->usecase == USECASE_COMPRESS_VOIP_CALL)
6325 return voice_extn_compress_voip_in_get_buffer_size(in);
Mingming Yine62d7842013-10-25 16:26:03 -07006326 else if(audio_extn_compr_cap_usecase_supported(in->usecase))
6327 return audio_extn_compr_cap_get_buffer_size(in->config.format);
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05306328 else if(audio_extn_cin_attached_usecase(in->usecase))
6329 return audio_extn_cin_get_buffer_size(in);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08006330
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006331 return in->config.period_size * in->af_period_multiplier *
6332 audio_stream_in_frame_size((const struct audio_stream_in *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006333}
6334
6335static uint32_t in_get_channels(const struct audio_stream *stream)
6336{
6337 struct stream_in *in = (struct stream_in *)stream;
6338
6339 return in->channel_mask;
6340}
6341
6342static audio_format_t in_get_format(const struct audio_stream *stream)
6343{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08006344 struct stream_in *in = (struct stream_in *)stream;
6345
6346 return in->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006347}
6348
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07006349static int in_set_format(struct audio_stream *stream __unused,
6350 audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006351{
6352 return -ENOSYS;
6353}
6354
6355static int in_standby(struct audio_stream *stream)
6356{
6357 struct stream_in *in = (struct stream_in *)stream;
6358 struct audio_device *adev = in->dev;
6359 int status = 0;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05306360 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
6361 stream, in->usecase, use_case_table[in->usecase]);
Haynes Mathew George16081042017-05-31 17:16:49 -07006362 bool do_stop = true;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05306363
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006364 lock_input_stream(in);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07006365 if (!in->standby && in->is_st_session) {
6366 ALOGD("%s: sound trigger pcm stop lab", __func__);
6367 audio_extn_sound_trigger_stop_lab(in);
Quinn Malef6050362019-01-30 15:55:40 -08006368 adev->num_va_sessions--;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07006369 in->standby = 1;
6370 }
6371
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006372 if (!in->standby) {
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07006373 if (adev->adm_deregister_stream)
6374 adev->adm_deregister_stream(adev->adm_data, in->capture_handle);
6375
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08006376 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006377 in->standby = true;
Zhou Songa8895042016-07-05 17:54:22 +08006378 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
kunleizbecba2d2017-09-07 13:37:16 +08006379 do_stop = false;
Zhou Songa8895042016-07-05 17:54:22 +08006380 voice_extn_compress_voip_close_input_stream(stream);
6381 ALOGD("VOIP input entered standby");
Haynes Mathew George16081042017-05-31 17:16:49 -07006382 } else if (in->usecase == USECASE_AUDIO_RECORD_MMAP) {
6383 do_stop = in->capture_started;
6384 in->capture_started = false;
Phil Burkd898ba62019-06-20 12:49:01 -07006385 if (in->mmap_shared_memory_fd >= 0) {
6386 ALOGV("%s: closing mmap_shared_memory_fd = %d",
6387 __func__, in->mmap_shared_memory_fd);
6388 close(in->mmap_shared_memory_fd);
6389 in->mmap_shared_memory_fd = -1;
6390 }
Zhou Songa8895042016-07-05 17:54:22 +08006391 } else {
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05306392 if (audio_extn_cin_attached_usecase(in->usecase))
Manish Dewangan46e07982018-12-13 18:18:59 +05306393 audio_extn_cin_close_input_stream(in);
kunleizbecba2d2017-09-07 13:37:16 +08006394 }
6395
Arun Mirpuri5d170872019-03-26 13:21:31 -07006396 if (in->pcm) {
6397 ATRACE_BEGIN("pcm_in_close");
6398 pcm_close(in->pcm);
6399 ATRACE_END();
6400 in->pcm = NULL;
6401 }
6402
Carter Hsu2e429db2019-05-14 18:50:52 +08006403 if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION)
Aalique Grahame22e49102018-12-18 14:23:57 -08006404 adev->enable_voicerx = false;
Carter Hsu2e429db2019-05-14 18:50:52 +08006405
6406 if (do_stop)
Zhou Songa8895042016-07-05 17:54:22 +08006407 status = stop_input_stream(in);
Quinn Malef6050362019-01-30 15:55:40 -08006408
6409 if (in->source == AUDIO_SOURCE_VOICE_RECOGNITION)
6410 adev->num_va_sessions--;
6411
Eric Laurent150dbfe2013-02-27 14:31:02 -08006412 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006413 }
6414 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07006415 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006416 return status;
6417}
6418
Aalique Grahame22e49102018-12-18 14:23:57 -08006419static int in_dump(const struct audio_stream *stream,
6420 int fd)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006421{
Aalique Grahame22e49102018-12-18 14:23:57 -08006422 struct stream_in *in = (struct stream_in *)stream;
6423
6424 // We try to get the lock for consistency,
6425 // but it isn't necessary for these variables.
6426 // If we're not in standby, we may be blocked on a read.
6427 const bool locked = (pthread_mutex_trylock(&in->lock) == 0);
6428 dprintf(fd, " Standby: %s\n", in->standby ? "yes" : "no");
6429 dprintf(fd, " Frames read: %lld\n", (long long)in->frames_read);
6430 dprintf(fd, " Frames muted: %lld\n", (long long)in->frames_muted);
6431
6432 if (locked) {
6433 pthread_mutex_unlock(&in->lock);
6434 }
6435
6436 // dump error info
6437 (void)error_log_dump(
6438 in->error_log, fd, " " /* prefix */, 0 /* lines */, 0 /* limit_ns */);
6439
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006440 return 0;
6441}
6442
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306443static void in_snd_mon_cb(void * stream, struct str_parms * parms)
6444{
6445 if (!stream || !parms)
6446 return;
6447
6448 struct stream_in *in = (struct stream_in *)stream;
6449 struct audio_device *adev = in->dev;
6450
6451 card_status_t status;
6452 int card;
6453 if (parse_snd_card_status(parms, &card, &status) < 0)
6454 return;
6455
6456 pthread_mutex_lock(&adev->lock);
6457 bool valid_cb = (card == adev->snd_card);
6458 pthread_mutex_unlock(&adev->lock);
6459
6460 if (!valid_cb)
6461 return;
6462
6463 lock_input_stream(in);
6464 if (in->card_status != status)
6465 in->card_status = status;
6466 pthread_mutex_unlock(&in->lock);
6467
6468 ALOGW("in_snd_mon_cb for card %d usecase %s, status %s", card,
6469 use_case_table[in->usecase],
6470 status == CARD_STATUS_OFFLINE ? "offline" : "online");
6471
6472 // a better solution would be to report error back to AF and let
6473 // it put the stream to standby
6474 if (status == CARD_STATUS_OFFLINE)
6475 in_standby(&in->stream.common);
6476
6477 return;
6478}
6479
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006480static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
6481{
6482 struct stream_in *in = (struct stream_in *)stream;
6483 struct audio_device *adev = in->dev;
6484 struct str_parms *parms;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006485 char value[32];
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08006486 int ret = 0, val = 0, err;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006487
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05306488 ALOGD("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006489 parms = str_parms_create_str(kvpairs);
6490
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05306491 if (!parms)
6492 goto error;
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006493 lock_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08006494 pthread_mutex_lock(&adev->lock);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08006495
6496 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
6497 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006498 val = atoi(value);
6499 /* no audio source uses val == 0 */
6500 if ((in->source != val) && (val != 0)) {
6501 in->source = val;
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08006502 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
6503 (in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
6504 (voice_extn_compress_voip_is_format_supported(in->format)) &&
Shiv Maliyappanahallie66aba22016-01-27 16:08:57 -08006505 (in->config.rate == 8000 || in->config.rate == 16000 ||
6506 in->config.rate == 32000 || in->config.rate == 48000 ) &&
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07006507 (audio_channel_count_from_in_mask(in->channel_mask) == 1)) {
Narsinga Rao Chella7d5a3e82014-02-04 16:23:52 -08006508 err = voice_extn_compress_voip_open_input_stream(in);
6509 if (err != 0) {
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08006510 ALOGE("%s: Compress voip input cannot be opened, error:%d",
Narsinga Rao Chella7d5a3e82014-02-04 16:23:52 -08006511 __func__, err);
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08006512 }
6513 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006514 }
6515 }
6516
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08006517 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
6518 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006519 val = atoi(value);
Kevin Rocardfce19002017-08-07 19:21:36 -07006520 if (((int)in->device != val) && (val != 0) && audio_is_input_device(val) ) {
6521
6522 // Workaround: If routing to an non existing usb device, fail gracefully
6523 // The routing request will otherwise block during 10 second
6524 int card;
6525 if (audio_is_usb_in_device(val) &&
6526 (card = get_alive_usb_card(parms)) >= 0) {
6527
6528 ALOGW("in_set_parameters() ignoring rerouting to non existing USB card %d", card);
6529 ret = -ENOSYS;
6530 } else {
6531
6532 in->device = val;
6533 /* If recording is in progress, change the tx device to new device */
6534 if (!in->standby && !in->is_st_session) {
6535 ALOGV("update input routing change");
6536 // inform adm before actual routing to prevent glitches.
6537 if (adev->adm_on_routing_change) {
Haynes Mathew George822b5492016-07-01 16:57:24 -07006538 adev->adm_on_routing_change(adev->adm_data,
6539 in->capture_handle);
Kevin Rocardfce19002017-08-07 19:21:36 -07006540 ret = select_devices(adev, in->usecase);
Aniket Kumar Lata60586a92019-05-22 22:18:55 -07006541 if (in->usecase == USECASE_AUDIO_RECORD_LOW_LATENCY)
6542 adev->adm_routing_changed = true;
Kevin Rocardfce19002017-08-07 19:21:36 -07006543 }
6544 }
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006545 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006546 }
6547 }
6548
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +05306549 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_PROFILE, value, sizeof(value));
6550 if (err >= 0) {
6551 strlcpy(in->profile, value, sizeof(in->profile));
6552 ALOGV("updating stream profile with value '%s'", in->profile);
6553 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
6554 &adev->streams_input_cfg_list,
6555 in->device, in->flags, in->format,
6556 in->sample_rate, in->bit_width,
6557 in->profile, &in->app_type_cfg);
6558 }
6559
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006560 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08006561 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006562
6563 str_parms_destroy(parms);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05306564error:
Eric Laurent994a6932013-07-17 11:51:42 -07006565 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006566 return ret;
6567}
6568
6569static char* in_get_parameters(const struct audio_stream *stream,
6570 const char *keys)
6571{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08006572 struct stream_in *in = (struct stream_in *)stream;
6573 struct str_parms *query = str_parms_create_str(keys);
6574 char *str;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08006575 struct str_parms *reply = str_parms_create();
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07006576
6577 if (!query || !reply) {
Alexy Josephaee4fdd2016-01-29 13:02:07 -08006578 if (reply) {
6579 str_parms_destroy(reply);
6580 }
6581 if (query) {
6582 str_parms_destroy(query);
6583 }
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07006584 ALOGE("in_get_parameters: failed to create query or reply");
6585 return NULL;
6586 }
6587
Haynes Mathew George484e8d22017-07-31 18:55:17 -07006588 ALOGV("%s: enter: keys - %s %s ", __func__, use_case_table[in->usecase], keys);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08006589
6590 voice_extn_in_get_parameters(in, query, reply);
6591
Haynes Mathew George484e8d22017-07-31 18:55:17 -07006592 stream_get_parameter_channels(query, reply,
6593 &in->supported_channel_masks[0]);
6594 stream_get_parameter_formats(query, reply,
6595 &in->supported_formats[0]);
6596 stream_get_parameter_rates(query, reply,
6597 &in->supported_sample_rates[0]);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08006598 str = str_parms_to_str(reply);
6599 str_parms_destroy(query);
6600 str_parms_destroy(reply);
6601
6602 ALOGV("%s: exit: returns - %s", __func__, str);
6603 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006604}
6605
Aalique Grahame22e49102018-12-18 14:23:57 -08006606static int in_set_gain(struct audio_stream_in *stream,
6607 float gain)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006608{
Aalique Grahame22e49102018-12-18 14:23:57 -08006609 struct stream_in *in = (struct stream_in *)stream;
6610 char mixer_ctl_name[128];
6611 struct mixer_ctl *ctl;
6612 int ctl_value;
6613
6614 ALOGV("%s: gain %f", __func__, gain);
6615
6616 if (stream == NULL)
6617 return -EINVAL;
6618
6619 /* in_set_gain() only used to silence MMAP capture for now */
6620 if (in->usecase != USECASE_AUDIO_RECORD_MMAP)
6621 return -ENOSYS;
6622
6623 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name), "Capture %d Volume", in->pcm_device_id);
6624
6625 ctl = mixer_get_ctl_by_name(in->dev->mixer, mixer_ctl_name);
6626 if (!ctl) {
6627 ALOGW("%s: Could not get ctl for mixer cmd - %s",
6628 __func__, mixer_ctl_name);
6629 return -ENOSYS;
6630 }
6631
6632 if (gain < RECORD_GAIN_MIN)
6633 gain = RECORD_GAIN_MIN;
6634 else if (gain > RECORD_GAIN_MAX)
6635 gain = RECORD_GAIN_MAX;
6636 ctl_value = (int)(RECORD_VOLUME_CTL_MAX * gain);
6637
6638 mixer_ctl_set_value(ctl, 0, ctl_value);
6639
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006640 return 0;
6641}
6642
6643static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
6644 size_t bytes)
6645{
6646 struct stream_in *in = (struct stream_in *)stream;
Pallavid7c7a272018-01-16 11:22:55 +05306647
6648 if (in == NULL) {
6649 ALOGE("%s: stream_in ptr is NULL", __func__);
6650 return -EINVAL;
6651 }
6652
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006653 struct audio_device *adev = in->dev;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05306654 int ret = -1;
Dhananjay Kumar97b81e32019-05-15 21:00:21 +05306655 size_t bytes_read = 0, frame_size = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006656
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006657 lock_input_stream(in);
Naresh Tanniru4c630392014-05-12 01:05:52 +05306658
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07006659 if (in->is_st_session) {
6660 ALOGVV(" %s: reading on st session bytes=%zu", __func__, bytes);
6661 /* Read from sound trigger HAL */
6662 audio_extn_sound_trigger_read(in, buffer, bytes);
Quinn Malef6050362019-01-30 15:55:40 -08006663 if (in->standby) {
6664 adev->num_va_sessions++;
6665 in->standby = 0;
6666 }
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07006667 pthread_mutex_unlock(&in->lock);
6668 return bytes;
6669 }
6670
Haynes Mathew George16081042017-05-31 17:16:49 -07006671 if (in->usecase == USECASE_AUDIO_RECORD_MMAP) {
6672 ret = -ENOSYS;
6673 goto exit;
6674 }
6675
Aniket Kumar Lata60586a92019-05-22 22:18:55 -07006676 if (in->usecase == USECASE_AUDIO_RECORD_LOW_LATENCY &&
6677 !in->standby && adev->adm_routing_changed) {
6678 ret = -ENOSYS;
6679 goto exit;
6680 }
6681
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006682 if (in->standby) {
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07006683 pthread_mutex_lock(&adev->lock);
6684 if (in->usecase == USECASE_COMPRESS_VOIP_CALL)
6685 ret = voice_extn_compress_voip_start_input_stream(in);
6686 else
6687 ret = start_input_stream(in);
Quinn Malef6050362019-01-30 15:55:40 -08006688 if (!ret && in->source == AUDIO_SOURCE_VOICE_RECOGNITION)
6689 adev->num_va_sessions++;
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07006690 pthread_mutex_unlock(&adev->lock);
6691 if (ret != 0) {
6692 goto exit;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006693 }
6694 in->standby = 0;
6695 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006696
Deeraj Soman30cc7ae2019-03-18 16:26:55 +05306697 /* Avoid read if capture_stopped is set */
6698 if (android_atomic_acquire_load(&(in->capture_stopped)) > 0) {
6699 ALOGD("%s: force stopped catpure session, ignoring read request", __func__);
6700 ret = -EINVAL;
6701 goto exit;
6702 }
6703
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006704 // what's the duration requested by the client?
6705 long ns = 0;
6706
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05306707 if (in->pcm && in->config.rate)
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006708 ns = pcm_bytes_to_frames(in->pcm, bytes)*1000000000LL/
6709 in->config.rate;
6710
Aniket Kumar Lata60586a92019-05-22 22:18:55 -07006711 ret = request_in_focus(in, ns);
6712 if (ret != 0)
6713 goto exit;
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006714 bool use_mmap = is_mmap_usecase(in->usecase) || in->realtime;
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07006715
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05306716 if (audio_extn_cin_attached_usecase(in->usecase)) {
6717 ret = audio_extn_cin_read(in, buffer, bytes, &bytes_read);
6718 } else if (in->pcm) {
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05306719 if (audio_extn_ssr_get_stream() == in) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07006720 ret = audio_extn_ssr_read(stream, buffer, bytes);
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05306721 } else if (audio_extn_compr_cap_usecase_supported(in->usecase)) {
Mingming Yine62d7842013-10-25 16:26:03 -07006722 ret = audio_extn_compr_cap_read(in, buffer, bytes);
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006723 } else if (use_mmap) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07006724 ret = pcm_mmap_read(in->pcm, buffer, bytes);
Garmond Leunge2433c32017-09-28 21:51:22 -07006725 } else if (audio_extn_ffv_get_stream() == in) {
6726 ret = audio_extn_ffv_read(stream, buffer, bytes);
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05306727 } else {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07006728 ret = pcm_read(in->pcm, buffer, bytes);
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05306729 /* data from DSP comes in 24_8 format, convert it to 8_24 */
6730 if (!ret && bytes > 0 && (in->format == AUDIO_FORMAT_PCM_8_24_BIT)) {
6731 if (audio_extn_utils_convert_format_24_8_to_8_24(buffer, bytes)
6732 != bytes) {
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05306733 ret = -EINVAL;
6734 goto exit;
6735 }
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05306736 } else if (ret < 0) {
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05306737 ret = -errno;
6738 }
6739 }
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05306740 /* bytes read is always set to bytes for non compress usecases */
6741 bytes_read = bytes;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006742 }
6743
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006744 release_in_focus(in);
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07006745
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006746 /*
Quinn Malef6050362019-01-30 15:55:40 -08006747 * Instead of writing zeroes here, we could trust the hardware to always
6748 * provide zeroes when muted. This is also muted with voice recognition
6749 * usecases so that other clients do not have access to voice recognition
6750 * data.
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006751 */
Quinn Malef6050362019-01-30 15:55:40 -08006752 if ((ret == 0 && voice_get_mic_mute(adev) &&
6753 !voice_is_in_call_rec_stream(in) &&
6754 in->usecase != USECASE_AUDIO_RECORD_AFE_PROXY) ||
6755 (adev->num_va_sessions &&
6756 in->source != AUDIO_SOURCE_VOICE_RECOGNITION &&
6757 property_get_bool("persist.vendor.audio.va_concurrency_mute_enabled",
6758 false)))
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006759 memset(buffer, 0, bytes);
6760
6761exit:
Dhananjay Kumar97b81e32019-05-15 21:00:21 +05306762 frame_size = audio_stream_in_frame_size(stream);
6763 if (frame_size > 0)
6764 in->frames_read += bytes_read/frame_size;
6765
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07006766 if (-ENETRESET == ret)
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306767 in->card_status = CARD_STATUS_OFFLINE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006768 pthread_mutex_unlock(&in->lock);
6769
6770 if (ret != 0) {
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05306771 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05306772 pthread_mutex_lock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05306773 voice_extn_compress_voip_close_input_stream(&in->stream.common);
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05306774 pthread_mutex_unlock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05306775 in->standby = true;
6776 }
Sharad Sangled17c9122017-03-20 15:58:52 +05306777 if (!audio_extn_cin_attached_usecase(in->usecase)) {
6778 bytes_read = bytes;
6779 memset(buffer, 0, bytes);
6780 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006781 in_standby(&in->stream.common);
Aniket Kumar Lata60586a92019-05-22 22:18:55 -07006782 if (in->usecase == USECASE_AUDIO_RECORD_LOW_LATENCY)
6783 adev->adm_routing_changed = false;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07006784 ALOGV("%s: read failed status %d- sleeping for buffer duration", __func__, ret);
Ashish Jainbbce4322016-02-16 13:25:27 +05306785 usleep((uint64_t)bytes * 1000000 / audio_stream_in_frame_size(stream) /
Naresh Tanniru4c630392014-05-12 01:05:52 +05306786 in_get_sample_rate(&in->stream.common));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006787 }
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05306788 return bytes_read;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006789}
6790
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07006791static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006792{
6793 return 0;
6794}
6795
Aalique Grahame22e49102018-12-18 14:23:57 -08006796static int in_get_capture_position(const struct audio_stream_in *stream,
6797 int64_t *frames, int64_t *time)
6798{
6799 if (stream == NULL || frames == NULL || time == NULL) {
6800 return -EINVAL;
6801 }
6802 struct stream_in *in = (struct stream_in *)stream;
6803 int ret = -ENOSYS;
6804
6805 lock_input_stream(in);
6806 // note: ST sessions do not close the alsa pcm driver synchronously
6807 // on standby. Therefore, we may return an error even though the
6808 // pcm stream is still opened.
6809 if (in->standby) {
6810 ALOGE_IF(in->pcm != NULL && !in->is_st_session,
6811 "%s stream in standby but pcm not NULL for non ST session", __func__);
6812 goto exit;
6813 }
6814 if (in->pcm) {
6815 struct timespec timestamp;
6816 unsigned int avail;
6817 if (pcm_get_htimestamp(in->pcm, &avail, &timestamp) == 0) {
6818 *frames = in->frames_read + avail;
6819 *time = timestamp.tv_sec * 1000000000LL + timestamp.tv_nsec;
6820 ret = 0;
6821 }
6822 }
6823exit:
6824 pthread_mutex_unlock(&in->lock);
6825 return ret;
6826}
6827
Carter Hsu2e429db2019-05-14 18:50:52 +08006828static int in_update_effect_list(bool add, effect_handle_t effect,
6829 struct listnode *head)
6830{
6831 struct listnode *node;
6832 struct in_effect_list *elist = NULL;
6833 struct in_effect_list *target = NULL;
6834 int ret = 0;
6835
6836 if (!head)
6837 return ret;
6838
6839 list_for_each(node, head) {
6840 elist = node_to_item(node, struct in_effect_list, list);
6841 if (elist->handle == effect) {
6842 target = elist;
6843 break;
6844 }
6845 }
6846
6847 if (add) {
6848 if (target) {
6849 ALOGD("effect %p already exist", effect);
6850 return ret;
6851 }
6852
6853 target = (struct in_effect_list *)
6854 calloc(1, sizeof(struct in_effect_list));
6855
6856 if (!target) {
6857 ALOGE("%s:fail to allocate memory", __func__);
6858 return -ENOMEM;
6859 }
6860
6861 target->handle = effect;
6862 list_add_tail(head, &target->list);
6863 } else {
6864 if (target) {
6865 list_remove(&target->list);
6866 free(target);
6867 }
6868 }
6869
6870 return ret;
6871}
6872
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07006873static int add_remove_audio_effect(const struct audio_stream *stream,
6874 effect_handle_t effect,
6875 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006876{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07006877 struct stream_in *in = (struct stream_in *)stream;
6878 int status = 0;
6879 effect_descriptor_t desc;
6880
6881 status = (*effect)->get_descriptor(effect, &desc);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07006882 ALOGV("%s: status %d in->standby %d enable:%d", __func__, status, in->standby, enable);
6883
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07006884 if (status != 0)
6885 return status;
6886
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006887 lock_input_stream(in);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07006888 pthread_mutex_lock(&in->dev->lock);
kunleizd96526c2018-04-09 11:12:32 +08006889 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
Carter Hsu2e429db2019-05-14 18:50:52 +08006890 in->source == AUDIO_SOURCE_VOICE_RECOGNITION ||
6891 adev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07006892 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
Carter Hsu2e429db2019-05-14 18:50:52 +08006893
6894 in_update_effect_list(enable, effect, &in->aec_list);
6895 enable = !list_empty(&in->aec_list);
6896 if (enable == in->enable_aec)
6897 goto exit;
6898
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07006899 in->enable_aec = enable;
Carter Hsu2e429db2019-05-14 18:50:52 +08006900 ALOGD("AEC enable %d", enable);
6901
Aalique Grahame22e49102018-12-18 14:23:57 -08006902 if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
6903 in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) {
6904 in->dev->enable_voicerx = enable;
6905 struct audio_usecase *usecase;
6906 struct listnode *node;
6907 list_for_each(node, &in->dev->usecase_list) {
6908 usecase = node_to_item(node, struct audio_usecase, list);
6909 if (usecase->type == PCM_PLAYBACK)
6910 select_devices(in->dev, usecase->id);
6911 }
6912 }
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07006913 if (!in->standby) {
6914 if (enable_disable_effect(in->dev, EFFECT_AEC, enable) == ENOSYS)
6915 select_devices(in->dev, in->usecase);
6916 }
6917
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07006918 }
Carter Hsu2e429db2019-05-14 18:50:52 +08006919 if (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0) {
6920
6921 in_update_effect_list(enable, effect, &in->ns_list);
6922 enable = !list_empty(&in->ns_list);
6923 if (enable == in->enable_ns)
6924 goto exit;
6925
Ravi Kumar Alamanda198185e2013-11-07 15:42:19 -08006926 in->enable_ns = enable;
Carter Hsu2e429db2019-05-14 18:50:52 +08006927 ALOGD("NS enable %d", enable);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07006928 if (!in->standby) {
kunleizd96526c2018-04-09 11:12:32 +08006929 if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
6930 in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) {
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07006931 if (enable_disable_effect(in->dev, EFFECT_NS, enable) == ENOSYS)
6932 select_devices(in->dev, in->usecase);
6933 } else
6934 select_devices(in->dev, in->usecase);
6935 }
Ravi Kumar Alamanda198185e2013-11-07 15:42:19 -08006936 }
Carter Hsu2e429db2019-05-14 18:50:52 +08006937exit:
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07006938 pthread_mutex_unlock(&in->dev->lock);
6939 pthread_mutex_unlock(&in->lock);
6940
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006941 return 0;
6942}
6943
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07006944static int in_add_audio_effect(const struct audio_stream *stream,
6945 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006946{
Eric Laurent994a6932013-07-17 11:51:42 -07006947 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07006948 return add_remove_audio_effect(stream, effect, true);
6949}
6950
6951static int in_remove_audio_effect(const struct audio_stream *stream,
6952 effect_handle_t effect)
6953{
Eric Laurent994a6932013-07-17 11:51:42 -07006954 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07006955 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006956}
6957
Derek Chenf939fb72018-11-13 13:34:41 -08006958streams_input_ctxt_t *in_get_stream(struct audio_device *dev,
6959 audio_io_handle_t input)
6960{
6961 struct listnode *node;
6962
6963 list_for_each(node, &dev->active_inputs_list) {
6964 streams_input_ctxt_t *in_ctxt = node_to_item(node,
6965 streams_input_ctxt_t,
6966 list);
6967 if (in_ctxt->input->capture_handle == input) {
6968 return in_ctxt;
6969 }
6970 }
6971 return NULL;
6972}
6973
6974streams_output_ctxt_t *out_get_stream(struct audio_device *dev,
6975 audio_io_handle_t output)
6976{
6977 struct listnode *node;
6978
6979 list_for_each(node, &dev->active_outputs_list) {
6980 streams_output_ctxt_t *out_ctxt = node_to_item(node,
6981 streams_output_ctxt_t,
6982 list);
6983 if (out_ctxt->output->handle == output) {
6984 return out_ctxt;
6985 }
6986 }
6987 return NULL;
6988}
6989
Haynes Mathew George16081042017-05-31 17:16:49 -07006990static int in_stop(const struct audio_stream_in* stream)
6991{
6992 struct stream_in *in = (struct stream_in *)stream;
6993 struct audio_device *adev = in->dev;
6994
6995 int ret = -ENOSYS;
6996 ALOGV("%s", __func__);
6997 pthread_mutex_lock(&adev->lock);
6998 if (in->usecase == USECASE_AUDIO_RECORD_MMAP && !in->standby &&
6999 in->capture_started && in->pcm != NULL) {
7000 pcm_stop(in->pcm);
7001 ret = stop_input_stream(in);
7002 in->capture_started = false;
7003 }
7004 pthread_mutex_unlock(&adev->lock);
7005 return ret;
7006}
7007
7008static int in_start(const struct audio_stream_in* stream)
7009{
7010 struct stream_in *in = (struct stream_in *)stream;
7011 struct audio_device *adev = in->dev;
7012 int ret = -ENOSYS;
7013
7014 ALOGV("%s in %p", __func__, in);
7015 pthread_mutex_lock(&adev->lock);
7016 if (in->usecase == USECASE_AUDIO_RECORD_MMAP && !in->standby &&
7017 !in->capture_started && in->pcm != NULL) {
7018 if (!in->capture_started) {
7019 ret = start_input_stream(in);
7020 if (ret == 0) {
7021 in->capture_started = true;
7022 }
7023 }
7024 }
7025 pthread_mutex_unlock(&adev->lock);
7026 return ret;
7027}
7028
Phil Burke0a86d12019-02-16 22:28:11 -08007029// Read offset for the positional timestamp from a persistent vendor property.
7030// This is to workaround apparent inaccuracies in the timing information that
7031// is used by the AAudio timing model. The inaccuracies can cause glitches.
7032static int64_t in_get_mmap_time_offset() {
7033 const int32_t kDefaultOffsetMicros = 0;
7034 int32_t mmap_time_offset_micros = property_get_int32(
Weiyin Jiangbd68b4d2019-05-17 16:29:50 +08007035 "persist.vendor.audio.in_mmap_delay_micros", kDefaultOffsetMicros);
Phil Burke0a86d12019-02-16 22:28:11 -08007036 ALOGI("mmap_time_offset_micros = %d for input", mmap_time_offset_micros);
7037 return mmap_time_offset_micros * (int64_t)1000;
7038}
7039
Haynes Mathew George16081042017-05-31 17:16:49 -07007040static int in_create_mmap_buffer(const struct audio_stream_in *stream,
7041 int32_t min_size_frames,
7042 struct audio_mmap_buffer_info *info)
7043{
7044 struct stream_in *in = (struct stream_in *)stream;
7045 struct audio_device *adev = in->dev;
7046 int ret = 0;
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07007047 unsigned int offset1 = 0;
7048 unsigned int frames1 = 0;
Haynes Mathew George16081042017-05-31 17:16:49 -07007049 const char *step = "";
Arun Mirpuri5d170872019-03-26 13:21:31 -07007050 uint32_t mmap_size = 0;
7051 uint32_t buffer_size = 0;
Haynes Mathew George16081042017-05-31 17:16:49 -07007052
7053 pthread_mutex_lock(&adev->lock);
7054 ALOGV("%s in %p", __func__, in);
7055
Sharad Sanglec6f32552018-05-04 16:15:38 +05307056 if (CARD_STATUS_OFFLINE == in->card_status||
7057 CARD_STATUS_OFFLINE == adev->card_status) {
7058 ALOGW("in->card_status or adev->card_status offline, try again");
7059 ret = -EIO;
7060 goto exit;
7061 }
7062
Haynes Mathew George16081042017-05-31 17:16:49 -07007063 if (info == NULL || min_size_frames == 0) {
7064 ALOGE("%s invalid argument info %p min_size_frames %d", __func__, info, min_size_frames);
7065 ret = -EINVAL;
7066 goto exit;
7067 }
7068 if (in->usecase != USECASE_AUDIO_RECORD_MMAP || !in->standby) {
7069 ALOGE("%s: usecase = %d, standby = %d", __func__, in->usecase, in->standby);
7070 ALOGV("%s in %p", __func__, in);
7071 ret = -ENOSYS;
7072 goto exit;
7073 }
7074 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
7075 if (in->pcm_device_id < 0) {
7076 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
7077 __func__, in->pcm_device_id, in->usecase);
7078 ret = -EINVAL;
7079 goto exit;
7080 }
7081
7082 adjust_mmap_period_count(&in->config, min_size_frames);
7083
7084 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
7085 __func__, adev->snd_card, in->pcm_device_id, in->config.channels);
7086 in->pcm = pcm_open(adev->snd_card, in->pcm_device_id,
7087 (PCM_IN | PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC), &in->config);
Satish Babu Patakokila54ce83d2018-07-06 18:00:37 +05307088 if (errno == ENETRESET && !pcm_is_ready(in->pcm)) {
Sharad Sanglec6f32552018-05-04 16:15:38 +05307089 ALOGE("%s: pcm_open failed errno:%d\n", __func__, errno);
7090 in->card_status = CARD_STATUS_OFFLINE;
7091 adev->card_status = CARD_STATUS_OFFLINE;
7092 ret = -EIO;
7093 goto exit;
7094 }
7095
Haynes Mathew George16081042017-05-31 17:16:49 -07007096 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) {
7097 step = "open";
7098 ret = -ENODEV;
7099 goto exit;
7100 }
7101
7102 ret = pcm_mmap_begin(in->pcm, &info->shared_memory_address, &offset1, &frames1);
7103 if (ret < 0) {
7104 step = "begin";
7105 goto exit;
7106 }
Haynes Mathew George16081042017-05-31 17:16:49 -07007107
Arun Mirpuri5d170872019-03-26 13:21:31 -07007108 info->buffer_size_frames = pcm_get_buffer_size(in->pcm);
7109 buffer_size = pcm_frames_to_bytes(in->pcm, info->buffer_size_frames);
7110 info->burst_size_frames = in->config.period_size;
7111 ret = platform_get_mmap_data_fd(adev->platform,
7112 in->pcm_device_id, 1 /*capture*/,
7113 &info->shared_memory_fd,
7114 &mmap_size);
7115 if (ret < 0) {
7116 // Fall back to non exclusive mode
7117 info->shared_memory_fd = pcm_get_poll_fd(in->pcm);
7118 } else {
Phil Burkd898ba62019-06-20 12:49:01 -07007119 in->mmap_shared_memory_fd = info->shared_memory_fd; // for closing later
7120 ALOGV("%s: opened mmap_shared_memory_fd = %d", __func__, in->mmap_shared_memory_fd);
7121
Arun Mirpuri5d170872019-03-26 13:21:31 -07007122 if (mmap_size < buffer_size) {
7123 step = "mmap";
7124 goto exit;
7125 }
7126 // FIXME: indicate exclusive mode support by returning a negative buffer size
7127 info->buffer_size_frames *= -1;
7128 }
7129
7130 memset(info->shared_memory_address, 0, buffer_size);
Haynes Mathew George16081042017-05-31 17:16:49 -07007131
7132 ret = pcm_mmap_commit(in->pcm, 0, MMAP_PERIOD_SIZE);
7133 if (ret < 0) {
7134 step = "commit";
7135 goto exit;
7136 }
7137
Phil Burke0a86d12019-02-16 22:28:11 -08007138 in->mmap_time_offset_nanos = in_get_mmap_time_offset();
7139
Haynes Mathew George16081042017-05-31 17:16:49 -07007140 in->standby = false;
7141 ret = 0;
7142
7143 ALOGV("%s: got mmap buffer address %p info->buffer_size_frames %d",
7144 __func__, info->shared_memory_address, info->buffer_size_frames);
7145
7146exit:
7147 if (ret != 0) {
7148 if (in->pcm == NULL) {
7149 ALOGE("%s: %s - %d", __func__, step, ret);
7150 } else {
7151 ALOGE("%s: %s %s", __func__, step, pcm_get_error(in->pcm));
7152 pcm_close(in->pcm);
7153 in->pcm = NULL;
7154 }
7155 }
7156 pthread_mutex_unlock(&adev->lock);
7157 return ret;
7158}
7159
7160static int in_get_mmap_position(const struct audio_stream_in *stream,
7161 struct audio_mmap_position *position)
7162{
7163 struct stream_in *in = (struct stream_in *)stream;
7164 ALOGVV("%s", __func__);
7165 if (position == NULL) {
7166 return -EINVAL;
7167 }
7168 if (in->usecase != USECASE_AUDIO_RECORD_MMAP) {
7169 return -ENOSYS;
7170 }
7171 if (in->pcm == NULL) {
7172 return -ENOSYS;
7173 }
7174 struct timespec ts = { 0, 0 };
7175 int ret = pcm_mmap_get_hw_ptr(in->pcm, (unsigned int *)&position->position_frames, &ts);
7176 if (ret < 0) {
7177 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
7178 return ret;
7179 }
Phil Burke0a86d12019-02-16 22:28:11 -08007180 position->time_nanoseconds = ts.tv_sec*1000000000LL + ts.tv_nsec
7181 + in->mmap_time_offset_nanos;
Haynes Mathew George16081042017-05-31 17:16:49 -07007182 return 0;
7183}
7184
Naresh Tannirudcb47c52018-06-25 16:23:32 +05307185static int in_get_active_microphones(const struct audio_stream_in *stream,
7186 struct audio_microphone_characteristic_t *mic_array,
7187 size_t *mic_count) {
7188 struct stream_in *in = (struct stream_in *)stream;
7189 struct audio_device *adev = in->dev;
7190 ALOGVV("%s", __func__);
7191
7192 lock_input_stream(in);
7193 pthread_mutex_lock(&adev->lock);
7194 int ret = platform_get_active_microphones(adev->platform,
7195 audio_channel_count_from_in_mask(in->channel_mask),
7196 in->usecase, mic_array, mic_count);
7197 pthread_mutex_unlock(&adev->lock);
7198 pthread_mutex_unlock(&in->lock);
7199
7200 return ret;
7201}
7202
7203static int adev_get_microphones(const struct audio_hw_device *dev,
7204 struct audio_microphone_characteristic_t *mic_array,
7205 size_t *mic_count) {
7206 struct audio_device *adev = (struct audio_device *)dev;
7207 ALOGVV("%s", __func__);
7208
7209 pthread_mutex_lock(&adev->lock);
7210 int ret = platform_get_microphones(adev->platform, mic_array, mic_count);
7211 pthread_mutex_unlock(&adev->lock);
7212
7213 return ret;
7214}
juyuchendb308c22019-01-21 11:57:17 -07007215
7216static void in_update_sink_metadata(struct audio_stream_in *stream,
7217 const struct sink_metadata *sink_metadata) {
7218
7219 if (stream == NULL
7220 || sink_metadata == NULL
7221 || sink_metadata->tracks == NULL) {
7222 return;
7223 }
7224
7225 int error = 0;
7226 struct stream_in *in = (struct stream_in *)stream;
7227 struct audio_device *adev = in->dev;
7228 audio_devices_t device = AUDIO_DEVICE_NONE;
7229
7230 if (sink_metadata->track_count != 0)
7231 device = sink_metadata->tracks->dest_device;
7232
7233 lock_input_stream(in);
7234 pthread_mutex_lock(&adev->lock);
7235 ALOGV("%s: in->usecase: %d, device: %x", __func__, in->usecase, device);
7236
7237 if (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY
7238 && device != AUDIO_DEVICE_NONE
7239 && adev->voice_tx_output != NULL) {
7240 /* Use the rx device from afe-proxy record to route voice call because
7241 there is no routing if tx device is on primary hal and rx device
7242 is on other hal during voice call. */
7243 adev->voice_tx_output->devices = device;
7244
7245 if (!voice_is_call_state_active(adev)) {
7246 if (adev->mode == AUDIO_MODE_IN_CALL) {
7247 adev->current_call_output = adev->voice_tx_output;
7248 error = voice_start_call(adev);
7249 if (error != 0)
7250 ALOGE("%s: start voice call failed %d", __func__, error);
7251 }
7252 } else {
7253 adev->current_call_output = adev->voice_tx_output;
7254 voice_update_devices_for_all_voice_usecases(adev);
7255 }
7256 }
7257
7258 pthread_mutex_unlock(&adev->lock);
7259 pthread_mutex_unlock(&in->lock);
7260}
7261
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05307262int adev_open_output_stream(struct audio_hw_device *dev,
Haynes Mathew George16081042017-05-31 17:16:49 -07007263 audio_io_handle_t handle,
7264 audio_devices_t devices,
7265 audio_output_flags_t flags,
7266 struct audio_config *config,
7267 struct audio_stream_out **stream_out,
Derek Chenf6318be2017-06-12 17:16:24 -04007268 const char *address)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007269{
7270 struct audio_device *adev = (struct audio_device *)dev;
7271 struct stream_out *out;
Gangadhar Sb0210342019-02-22 17:39:41 +05307272 int ret = 0, ip_hdlr_stream = 0, ip_hdlr_dev = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07007273 audio_format_t format;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08007274 struct adsp_hdlr_stream_cfg hdlr_stream_cfg;
Manish Dewangan21a850a2017-08-14 12:03:55 +05307275 bool is_direct_passthough = false;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007276 bool is_hdmi = devices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
7277 bool is_usb_dev = audio_is_usb_out_device(devices) &&
7278 (devices != AUDIO_DEVICE_OUT_USB_ACCESSORY);
7279 bool direct_dev = is_hdmi || is_usb_dev;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08007280 bool use_db_as_primary =
vivek mehtaae1018c2019-05-09 12:19:57 -07007281 property_get_bool("vendor.audio.feature.deepbuffer_as_primary.enable",
7282 false);
Vignesh Kulothungana6927272019-02-20 15:17:07 -08007283 bool force_haptic_path =
7284 property_get_bool("vendor.audio.test_haptic", false);
Aniket Kumar Lata23d8cbf2019-04-10 19:54:46 -07007285 bool is_voip_rx = flags & AUDIO_OUTPUT_FLAG_VOIP_RX;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007286
kunleizdff872d2018-08-20 14:40:33 +08007287 if (is_usb_dev && (!audio_extn_usb_connected(NULL))) {
kunleizd6a9e0c2018-07-30 15:38:52 +08007288 is_usb_dev = false;
7289 devices = AUDIO_DEVICE_OUT_SPEAKER;
7290 ALOGW("%s: ignore set device to non existing USB card, use output device(%#x)",
7291 __func__, devices);
7292 }
7293
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007294 *stream_out = NULL;
Naresh Tanniru80659832014-06-04 18:17:56 +05307295
Rahul Sharma99770982019-03-06 17:05:26 +05307296 pthread_mutex_lock(&adev->lock);
7297 if (out_get_stream(adev, handle) != NULL) {
7298 ALOGW("%s, output stream already opened", __func__);
7299 ret = -EEXIST;
7300 }
7301 pthread_mutex_unlock(&adev->lock);
7302 if (ret)
7303 return ret;
7304
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007305 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
7306
Mingming Yin3a941d42016-02-17 18:08:05 -08007307 ALOGD("%s: enter: format(%#x) sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)\
Derek Chenf6318be2017-06-12 17:16:24 -04007308 stream_handle(%p) address(%s)", __func__, config->format, config->sample_rate, config->channel_mask,
7309 devices, flags, &out->stream, address);
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05307310
7311
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08007312 if (!out) {
7313 return -ENOMEM;
7314 }
7315
Haynes Mathew George204045b2015-02-25 20:32:03 -08007316 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07007317 pthread_mutex_init(&out->pre_lock, (const pthread_mutexattr_t *) NULL);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05307318 pthread_mutex_init(&out->compr_mute_lock, (const pthread_mutexattr_t *) NULL);
Zhou Song48453a02018-01-10 17:50:59 +08007319 pthread_mutex_init(&out->position_query_lock, (const pthread_mutexattr_t *) NULL);
Haynes Mathew George204045b2015-02-25 20:32:03 -08007320 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
7321
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007322 if (devices == AUDIO_DEVICE_NONE)
7323 devices = AUDIO_DEVICE_OUT_SPEAKER;
7324
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007325 out->flags = flags;
7326 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07007327 out->dev = adev;
Aalique Grahame65780b52017-09-27 14:59:56 -07007328 out->hal_op_format = out->hal_ip_format = format = out->format = config->format;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07007329 out->sample_rate = config->sample_rate;
Sachin Mohan Gadag3d09acd2017-06-19 12:43:44 +05307330 out->channel_mask = config->channel_mask;
Ramjee Singh5857aeb2017-08-03 19:18:50 +05307331 if (out->channel_mask == AUDIO_CHANNEL_NONE)
7332 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
7333 else
7334 out->supported_channel_masks[0] = out->channel_mask;
Eric Laurentc4aef752013-09-12 17:45:53 -07007335 out->handle = handle;
Mingming Yin3ee55c62014-08-04 14:23:35 -07007336 out->bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Alexy Josephaa54c872014-12-03 02:46:47 -08007337 out->non_blocking = 0;
Ashish Jain83a6cc22016-06-28 14:34:17 +05307338 out->convert_buffer = NULL;
Ashish Jain1b9b30c2017-05-18 20:57:40 +05307339 out->started = 0;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05307340 out->a2dp_compress_mute = false;
Aniket Kumar Lata932f4872017-11-06 18:29:44 -08007341 out->hal_output_suspend_supported = 0;
7342 out->dynamic_pm_qos_config_supported = 0;
Surendar Karkaf51b5842018-04-26 11:28:38 +05307343 out->set_dual_mono = false;
Manisha Agarwal7b3e3772019-02-20 14:33:45 +05307344 out->prev_card_status_offline = false;
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +05307345 out->pspd_coeff_sent = false;
Phil Burkd898ba62019-06-20 12:49:01 -07007346 out->mmap_shared_memory_fd = -1; // not open
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007347
Nikhil Laturkar26b690b2017-07-25 11:06:14 +05307348 if ((flags & AUDIO_OUTPUT_FLAG_BD) &&
Satish Babu Patakokila37e7c482018-02-02 11:50:06 +05307349 (property_get_bool("vendor.audio.matrix.limiter.enable", false)))
Ben Romberger6c4d3812017-06-13 17:46:45 -07007350 platform_set_device_params(out, DEVICE_PARAM_LIMITER_ID, 1);
7351
Aalique Grahame22e49102018-12-18 14:23:57 -08007352 if (direct_dev &&
7353 (audio_is_linear_pcm(out->format) ||
7354 config->format == AUDIO_FORMAT_DEFAULT) &&
7355 out->flags == AUDIO_OUTPUT_FLAG_NONE) {
7356 audio_format_t req_format = config->format;
7357 audio_channel_mask_t req_channel_mask = config->channel_mask;
7358 uint32_t req_sample_rate = config->sample_rate;
7359
7360 pthread_mutex_lock(&adev->lock);
7361 if (is_hdmi) {
7362 ALOGV("AUDIO_DEVICE_OUT_AUX_DIGITAL and DIRECT|OFFLOAD, check hdmi caps");
7363 ret = read_hdmi_sink_caps(out);
7364 if (config->sample_rate == 0)
7365 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
7366 if (config->channel_mask == AUDIO_CHANNEL_NONE)
7367 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
7368 if (config->format == AUDIO_FORMAT_DEFAULT)
7369 config->format = AUDIO_FORMAT_PCM_16_BIT;
7370 } else if (is_usb_dev) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007371 ret = read_usb_sup_params_and_compare(true /*is_playback*/,
7372 &config->format,
7373 &out->supported_formats[0],
7374 MAX_SUPPORTED_FORMATS,
7375 &config->channel_mask,
7376 &out->supported_channel_masks[0],
7377 MAX_SUPPORTED_CHANNEL_MASKS,
7378 &config->sample_rate,
7379 &out->supported_sample_rates[0],
7380 MAX_SUPPORTED_SAMPLE_RATES);
7381 ALOGV("plugged dev USB ret %d", ret);
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007382 }
Aalique Grahame22e49102018-12-18 14:23:57 -08007383
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007384 pthread_mutex_unlock(&adev->lock);
7385 if (ret != 0) {
Mingming Yin3a941d42016-02-17 18:08:05 -08007386 if (ret == -ENOSYS) {
7387 /* ignore and go with default */
7388 ret = 0;
Aalique Grahame22e49102018-12-18 14:23:57 -08007389 }
7390 // For MMAP NO IRQ, allow conversions in ADSP
7391 else if (is_hdmi || (flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) == 0)
7392 goto error_open;
7393 else {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007394 ALOGE("error reading direct dev sink caps");
Mingming Yin3a941d42016-02-17 18:08:05 -08007395 goto error_open;
7396 }
Aalique Grahame22e49102018-12-18 14:23:57 -08007397
7398 if (req_sample_rate != 0 && config->sample_rate != req_sample_rate)
7399 config->sample_rate = req_sample_rate;
7400 if (req_channel_mask != AUDIO_CHANNEL_NONE && config->channel_mask != req_channel_mask)
7401 config->channel_mask = req_channel_mask;
7402 if (req_format != AUDIO_FORMAT_DEFAULT && config->format != req_format)
7403 config->format = req_format;
Mingming Yin3a941d42016-02-17 18:08:05 -08007404 }
Aalique Grahame22e49102018-12-18 14:23:57 -08007405
7406 out->sample_rate = config->sample_rate;
7407 out->channel_mask = config->channel_mask;
7408 out->format = config->format;
7409 if (is_hdmi) {
7410 out->usecase = USECASE_AUDIO_PLAYBACK_HIFI;
7411 out->config = pcm_config_hdmi_multi;
7412 } else if (flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) {
7413 out->usecase = USECASE_AUDIO_PLAYBACK_MMAP;
7414 out->config = pcm_config_mmap_playback;
7415 out->stream.start = out_start;
7416 out->stream.stop = out_stop;
7417 out->stream.create_mmap_buffer = out_create_mmap_buffer;
7418 out->stream.get_mmap_position = out_get_mmap_position;
7419 } else {
7420 out->usecase = USECASE_AUDIO_PLAYBACK_HIFI;
7421 out->config = pcm_config_hifi;
7422 }
7423
7424 out->config.rate = out->sample_rate;
7425 out->config.channels = audio_channel_count_from_out_mask(out->channel_mask);
7426 if (is_hdmi) {
7427 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels *
7428 audio_bytes_per_sample(out->format));
7429 }
7430 out->config.format = pcm_format_from_audio_format(out->format);
Mingming Yin3a941d42016-02-17 18:08:05 -08007431 }
7432
Derek Chenf6318be2017-06-12 17:16:24 -04007433 /* validate bus device address */
7434 if (out->devices & AUDIO_DEVICE_OUT_BUS) {
7435 /* extract car audio stream index */
7436 out->car_audio_stream =
7437 audio_extn_auto_hal_get_car_audio_stream_from_address(address);
7438 if (out->car_audio_stream < 0) {
7439 ALOGE("%s: invalid car audio stream %x",
7440 __func__, out->car_audio_stream);
7441 ret = -EINVAL;
7442 goto error_open;
7443 }
7444 /* save car audio stream and address for bus device */
7445 strlcpy(out->address, address, AUDIO_DEVICE_MAX_ADDRESS_LEN);
7446 ALOGV("%s: address %s, car_audio_stream %x",
7447 __func__, out->address, out->car_audio_stream);
7448 }
7449
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08007450 /* Check for VOIP usecase */
Aniket Kumar Lata23d8cbf2019-04-10 19:54:46 -07007451 if (is_voip_rx) {
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08007452 if (!voice_extn_is_compress_voip_supported()) {
7453 if (out->sample_rate == 8000 || out->sample_rate == 16000 ||
7454 out->sample_rate == 32000 || out->sample_rate == 48000) {
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07007455 out->channel_mask = audio_extn_utils_is_vendor_enhanced_fwk() ?
7456 AUDIO_CHANNEL_OUT_MONO : AUDIO_CHANNEL_OUT_STEREO;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08007457 out->usecase = USECASE_AUDIO_PLAYBACK_VOIP;
7458 out->format = AUDIO_FORMAT_PCM_16_BIT;
Aniket Kumar Lata8426d1f2019-06-10 15:25:53 -07007459 out->volume_l = INVALID_OUT_VOLUME;
7460 out->volume_r = INVALID_OUT_VOLUME;
Vikram Panduranga93f080e2017-06-07 18:16:14 -07007461
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08007462 out->config = default_pcm_config_voip_copp;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08007463 out->config.rate = out->sample_rate;
Aniket Kumar Lata1e344f22019-06-19 17:09:01 -07007464 uint32_t channel_count =
7465 audio_channel_count_from_out_mask(out->channel_mask);
7466 uint32_t buffer_size = get_stream_buffer_size(DEFAULT_VOIP_BUF_DURATION_MS,
7467 out->sample_rate, out->format,
7468 channel_count, false);
7469 uint32_t frame_size = audio_bytes_per_sample(out->format) * channel_count;
7470 if (frame_size != 0)
7471 out->config.period_size = buffer_size / frame_size;
7472 else
7473 ALOGW("%s: frame size is 0 for format %#x", __func__, out->format);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08007474 }
7475 } else {
7476 if ((out->dev->mode == AUDIO_MODE_IN_COMMUNICATION ||
7477 voice_extn_compress_voip_is_active(out->dev)) &&
7478 (voice_extn_compress_voip_is_config_supported(config))) {
7479 ret = voice_extn_compress_voip_open_output_stream(out);
7480 if (ret != 0) {
7481 ALOGE("%s: Compress voip output cannot be opened, error:%d",
7482 __func__, ret);
7483 goto error_open;
7484 }
Sujin Panicker19027262019-09-16 18:28:06 +05307485 } else {
7486 out->usecase = GET_USECASE_AUDIO_PLAYBACK_PRIMARY(use_db_as_primary);
7487 out->config = GET_PCM_CONFIG_AUDIO_PLAYBACK_PRIMARY(use_db_as_primary);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08007488 }
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007489 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007490 } else if (audio_is_linear_pcm(out->format) &&
7491 out->flags == AUDIO_OUTPUT_FLAG_NONE && is_usb_dev) {
7492 out->channel_mask = config->channel_mask;
7493 out->sample_rate = config->sample_rate;
7494 out->format = config->format;
7495 out->usecase = USECASE_AUDIO_PLAYBACK_HIFI;
7496 // does this change?
7497 out->config = is_hdmi ? pcm_config_hdmi_multi : pcm_config_hifi;
7498 out->config.rate = config->sample_rate;
7499 out->config.channels = audio_channel_count_from_out_mask(out->channel_mask);
7500 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels *
7501 audio_bytes_per_sample(config->format));
7502 out->config.format = pcm_format_from_audio_format(out->format);
vivek mehta0ea887a2015-08-26 14:01:20 -07007503 } else if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
Dhananjay Kumarac341582017-02-23 23:42:25 +05307504 (out->flags == AUDIO_OUTPUT_FLAG_DIRECT)) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05307505 pthread_mutex_lock(&adev->lock);
7506 bool offline = (adev->card_status == CARD_STATUS_OFFLINE);
7507 pthread_mutex_unlock(&adev->lock);
7508
7509 // reject offload during card offline to allow
7510 // fallback to s/w paths
7511 if (offline) {
7512 ret = -ENODEV;
7513 goto error_open;
7514 }
vivek mehta0ea887a2015-08-26 14:01:20 -07007515
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07007516 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
7517 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
7518 ALOGE("%s: Unsupported Offload information", __func__);
7519 ret = -EINVAL;
7520 goto error_open;
7521 }
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07007522
Atul Khare3fa6e542017-08-09 00:56:17 +05307523 if (config->offload_info.format == 0)
7524 config->offload_info.format = config->format;
7525 if (config->offload_info.sample_rate == 0)
7526 config->offload_info.sample_rate = config->sample_rate;
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07007527
Mingming Yin90310102013-11-13 16:57:00 -08007528 if (!is_supported_format(config->offload_info.format) &&
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05307529 !audio_extn_passthru_is_supported_format(config->offload_info.format)) {
vivek mehta0ea887a2015-08-26 14:01:20 -07007530 ALOGE("%s: Unsupported audio format %x " , __func__, config->offload_info.format);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07007531 ret = -EINVAL;
7532 goto error_open;
7533 }
7534
Ben Romberger0f8c87b2017-05-24 17:41:11 -07007535 /* TrueHD only supported for 48k multiples (48k, 96k, 192k) */
7536 if ((config->offload_info.format == AUDIO_FORMAT_DOLBY_TRUEHD) &&
7537 (audio_extn_passthru_is_passthrough_stream(out)) &&
7538 !((config->sample_rate == 48000) ||
7539 (config->sample_rate == 96000) ||
7540 (config->sample_rate == 192000))) {
7541 ALOGE("%s: Unsupported sample rate %d for audio format %x",
7542 __func__, config->sample_rate, config->offload_info.format);
7543 ret = -EINVAL;
7544 goto error_open;
7545 }
7546
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07007547 out->compr_config.codec = (struct snd_codec *)
7548 calloc(1, sizeof(struct snd_codec));
7549
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07007550 if (!out->compr_config.codec) {
7551 ret = -ENOMEM;
7552 goto error_open;
7553 }
7554
Dhananjay Kumarac341582017-02-23 23:42:25 +05307555 out->stream.pause = out_pause;
7556 out->stream.resume = out_resume;
7557 out->stream.flush = out_flush;
Ashish Jain4847e9d2017-08-17 19:16:57 +05307558 out->stream.set_callback = out_set_callback;
Dhananjay Kumarac341582017-02-23 23:42:25 +05307559 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Mingming Yin21d60472015-09-30 13:56:25 -07007560 out->stream.drain = out_drain;
Dhananjay Kumarac341582017-02-23 23:42:25 +05307561 out->usecase = get_offload_usecase(adev, true /* is_compress */);
vivek mehta446c3962015-09-14 10:57:35 -07007562 ALOGV("Compress Offload usecase .. usecase selected %d", out->usecase);
Dhananjay Kumarac341582017-02-23 23:42:25 +05307563 } else {
7564 out->usecase = get_offload_usecase(adev, false /* is_compress */);
7565 ALOGV("non-offload DIRECT_usecase ... usecase selected %d ", out->usecase);
vivek mehta0ea887a2015-08-26 14:01:20 -07007566 }
vivek mehta446c3962015-09-14 10:57:35 -07007567
Deeraj Somanfa377bf2019-02-06 12:57:59 +05307568 if (out->flags & AUDIO_OUTPUT_FLAG_FAST) {
7569 ALOGD("%s: Setting latency mode to true", __func__);
7570 out->compr_config.codec->flags |= audio_extn_utils_get_perf_mode_flag();
7571 }
7572
vivek mehta446c3962015-09-14 10:57:35 -07007573 if (out->usecase == USECASE_INVALID) {
Mingming Yin3a941d42016-02-17 18:08:05 -08007574 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL &&
7575 config->format == 0 && config->sample_rate == 0 &&
7576 config->channel_mask == 0) {
Mingming Yin21854652016-04-13 11:54:02 -07007577 ALOGI("%s dummy open to query sink capability",__func__);
Mingming Yin3a941d42016-02-17 18:08:05 -08007578 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
7579 } else {
7580 ALOGE("%s, Max allowed OFFLOAD usecase reached ... ", __func__);
7581 ret = -EEXIST;
7582 goto error_open;
7583 }
vivek mehta446c3962015-09-14 10:57:35 -07007584 }
7585
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07007586 if (config->offload_info.channel_mask)
7587 out->channel_mask = config->offload_info.channel_mask;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08007588 else if (config->channel_mask) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07007589 out->channel_mask = config->channel_mask;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08007590 config->offload_info.channel_mask = config->channel_mask;
Haynes Mathew Georgea99f7532016-08-24 16:01:21 -07007591 } else {
Dhananjay Kumarac341582017-02-23 23:42:25 +05307592 ALOGE("out->channel_mask not set for OFFLOAD/DIRECT usecase");
Haynes Mathew Georgea99f7532016-08-24 16:01:21 -07007593 ret = -EINVAL;
7594 goto error_open;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08007595 }
Haynes Mathew Georgea99f7532016-08-24 16:01:21 -07007596
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07007597 format = out->format = config->offload_info.format;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07007598 out->sample_rate = config->offload_info.sample_rate;
7599
Mingming Yin3ee55c62014-08-04 14:23:35 -07007600 out->bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07007601
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05307602 out->compr_config.codec->id = get_snd_codec_id(config->offload_info.format);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05307603 if (audio_extn_utils_is_dolby_format(config->offload_info.format)) {
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05307604 audio_extn_dolby_send_ddp_endp_params(adev);
7605 audio_extn_dolby_set_dmid(adev);
7606 }
vivek mehta0ea887a2015-08-26 14:01:20 -07007607
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07007608 out->compr_config.codec->sample_rate =
Ravi Kumar Alamandab91bff32014-11-14 12:05:54 -08007609 config->offload_info.sample_rate;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07007610 out->compr_config.codec->bit_rate =
7611 config->offload_info.bit_rate;
7612 out->compr_config.codec->ch_in =
Dhanalakshmi Siddania15c6792016-08-10 15:33:53 +05307613 audio_channel_count_from_out_mask(out->channel_mask);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07007614 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
Satish Babu Patakokilaa395a9e2016-11-01 12:18:49 +05307615 /* Update bit width only for non passthrough usecases.
7616 * For passthrough usecases, the output will always be opened @16 bit
7617 */
7618 if (!audio_extn_passthru_is_passthrough_stream(out))
7619 out->bit_width = AUDIO_OUTPUT_BIT_WIDTH;
Naresh Tanniruee3499a2017-01-05 14:05:35 +05307620
7621 if (out->flags & AUDIO_OUTPUT_FLAG_TIMESTAMP)
7622 out->compr_config.codec->flags |= COMPRESSED_TIMESTAMP_FLAG;
7623 ALOGVV("%s : out->compr_config.codec->flags -> (%#x) ", __func__, out->compr_config.codec->flags);
7624
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07007625 /*TODO: Do we need to change it for passthrough */
7626 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_RAW;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07007627
Manish Dewangana6fc5442015-08-24 20:30:31 +05307628 if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC)
7629 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_RAW;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05307630 else if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS)
Manish Dewangana6fc5442015-08-24 20:30:31 +05307631 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_MP4ADTS;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05307632 else if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_LATM)
7633 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_MP4LATM;
Ashish Jainf1eaa582016-05-23 20:54:24 +05307634
7635 if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) ==
7636 AUDIO_FORMAT_PCM) {
7637
7638 /*Based on platform support, configure appropriate alsa format for corresponding
7639 *hal input format.
7640 */
7641 out->compr_config.codec->format = hal_format_to_alsa(
7642 config->offload_info.format);
7643
Ashish Jain83a6cc22016-06-28 14:34:17 +05307644 out->hal_op_format = alsa_format_to_hal(
Ashish Jainf1eaa582016-05-23 20:54:24 +05307645 out->compr_config.codec->format);
Ashish Jain83a6cc22016-06-28 14:34:17 +05307646 out->hal_ip_format = out->format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05307647
Dhananjay Kumarac341582017-02-23 23:42:25 +05307648 /*for direct non-compress playback populate bit_width based on selected alsa format as
Ashish Jainf1eaa582016-05-23 20:54:24 +05307649 *hal input format and alsa format might differ based on platform support.
7650 */
7651 out->bit_width = audio_bytes_per_sample(
Ashish Jain83a6cc22016-06-28 14:34:17 +05307652 out->hal_op_format) << 3;
Ashish Jainf1eaa582016-05-23 20:54:24 +05307653
7654 out->compr_config.fragments = DIRECT_PCM_NUM_FRAGMENTS;
7655
Deeraj Soman93155a62019-09-30 19:00:37 +05307656 if (property_get_bool("vendor.audio.offload.buffer.duration.enabled", false)) {
7657 if ((config->offload_info.duration_us >= MIN_OFFLOAD_BUFFER_DURATION_MS * 1000) &&
7658 (config->offload_info.duration_us <= MAX_OFFLOAD_BUFFER_DURATION_MS * 1000))
7659 out->info.duration_us = (int64_t)config->offload_info.duration_us;
7660 }
Deeraj Soman65358ab2019-02-07 15:40:49 +05307661
Ashish Jainf1eaa582016-05-23 20:54:24 +05307662 /* Check if alsa session is configured with the same format as HAL input format,
7663 * if not then derive correct fragment size needed to accomodate the
7664 * conversion of HAL input format to alsa format.
7665 */
7666 audio_extn_utils_update_direct_pcm_fragment_size(out);
7667
7668 /*if hal input and output fragment size is different this indicates HAL input format is
7669 *not same as the alsa format
7670 */
Ashish Jain83a6cc22016-06-28 14:34:17 +05307671 if (out->hal_fragment_size != out->compr_config.fragment_size) {
Ashish Jainf1eaa582016-05-23 20:54:24 +05307672 /*Allocate a buffer to convert input data to the alsa configured format.
7673 *size of convert buffer is equal to the size required to hold one fragment size
7674 *worth of pcm data, this is because flinger does not write more than fragment_size
7675 */
Ashish Jain83a6cc22016-06-28 14:34:17 +05307676 out->convert_buffer = calloc(1,out->compr_config.fragment_size);
7677 if (out->convert_buffer == NULL){
Ashish Jainf1eaa582016-05-23 20:54:24 +05307678 ALOGE("Allocation failed for convert buffer for size %d", out->compr_config.fragment_size);
7679 ret = -ENOMEM;
7680 goto error_open;
7681 }
7682 }
7683 } else if (audio_extn_passthru_is_passthrough_stream(out)) {
7684 out->compr_config.fragment_size =
7685 audio_extn_passthru_get_buffer_size(&config->offload_info);
7686 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
7687 } else {
7688 out->compr_config.fragment_size =
7689 platform_get_compress_offload_buffer_size(&config->offload_info);
7690 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
7691 }
Mingming Yin3ee55c62014-08-04 14:23:35 -07007692
Naresh Tanniruee3499a2017-01-05 14:05:35 +05307693 if (out->flags & AUDIO_OUTPUT_FLAG_TIMESTAMP) {
7694 out->compr_config.fragment_size += sizeof(struct snd_codec_metadata);
7695 }
Amit Shekhar6f461b12014-08-01 14:52:58 -07007696 if (config->offload_info.format == AUDIO_FORMAT_FLAC)
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +05307697 out->compr_config.codec->options.flac_dec.sample_size = AUDIO_OUTPUT_BIT_WIDTH;
Mingming Yin3ee55c62014-08-04 14:23:35 -07007698
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05307699 if (config->offload_info.format == AUDIO_FORMAT_APTX) {
7700 audio_extn_send_aptx_dec_bt_addr_to_dsp(out);
7701 }
7702
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07007703 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
7704 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07007705
Manish Dewangan69426c82017-01-30 17:35:36 +05307706 if ((flags & AUDIO_OUTPUT_FLAG_TIMESTAMP) &&
7707 (flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC)) {
7708 out->render_mode = RENDER_MODE_AUDIO_STC_MASTER;
7709 } else if(flags & AUDIO_OUTPUT_FLAG_TIMESTAMP) {
7710 out->render_mode = RENDER_MODE_AUDIO_MASTER;
7711 } else {
7712 out->render_mode = RENDER_MODE_AUDIO_NO_TIMESTAMP;
7713 }
Alexy Josephaa54c872014-12-03 02:46:47 -08007714
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05307715 memset(&out->channel_map_param, 0,
7716 sizeof(struct audio_out_channel_map_param));
7717
Haynes Mathew George352f27b2013-07-26 00:00:15 -07007718 out->send_new_metadata = 1;
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05307719 out->send_next_track_params = false;
7720 out->is_compr_metadata_avail = false;
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08007721 out->offload_state = OFFLOAD_STATE_IDLE;
7722 out->playback_started = 0;
Zhou Song48453a02018-01-10 17:50:59 +08007723 out->writeAt.tv_sec = 0;
7724 out->writeAt.tv_nsec = 0;
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08007725
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08007726 audio_extn_dts_create_state_notifier_node(out->usecase);
7727
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07007728 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
7729 __func__, config->offload_info.version,
7730 config->offload_info.bit_rate);
Ashish Jain5106d362016-05-11 19:23:33 +05307731
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05307732 /* Check if DSD audio format is supported in codec
7733 * and there is no active native DSD use case
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05307734 */
7735
7736 if ((config->format == AUDIO_FORMAT_DSD) &&
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05307737 (!platform_check_codec_dsd_support(adev->platform) ||
7738 audio_is_dsd_native_stream_active(adev))) {
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05307739 ret = -EINVAL;
7740 goto error_open;
7741 }
7742
Ashish Jain5106d362016-05-11 19:23:33 +05307743 /* Disable gapless if any of the following is true
7744 * passthrough playback
7745 * AV playback
Dhananjay Kumarac341582017-02-23 23:42:25 +05307746 * non compressed Direct playback
Ashish Jain5106d362016-05-11 19:23:33 +05307747 */
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05307748 if (audio_extn_passthru_is_passthrough_stream(out) ||
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05307749 (config->format == AUDIO_FORMAT_DSD) ||
Naresh Tanniru928f0862017-04-07 16:44:23 -07007750 (config->format == AUDIO_FORMAT_IEC61937) ||
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05307751 config->offload_info.has_video ||
Dhananjay Kumarac341582017-02-23 23:42:25 +05307752 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Ashish Jain5106d362016-05-11 19:23:33 +05307753 check_and_set_gapless_mode(adev, false);
7754 } else
7755 check_and_set_gapless_mode(adev, true);
Mingming Yin21854652016-04-13 11:54:02 -07007756
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05307757 if (audio_extn_passthru_is_passthrough_stream(out)) {
Mingming Yin21854652016-04-13 11:54:02 -07007758 out->flags |= AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH;
7759 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05307760 if (config->format == AUDIO_FORMAT_DSD) {
7761 out->flags |= AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH;
7762 out->compr_config.codec->compr_passthr = PASSTHROUGH_DSD;
7763 }
Aalique Grahame0359a1f2016-09-08 16:54:22 -07007764
7765 create_offload_callback_thread(out);
7766
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07007767 } else if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
Arun Mirpuri7da752a2018-09-11 18:01:15 -07007768 switch (config->sample_rate) {
7769 case 0:
7770 out->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
7771 break;
7772 case 8000:
7773 case 16000:
7774 case 48000:
7775 out->sample_rate = config->sample_rate;
7776 break;
7777 default:
7778 ALOGE("%s: Unsupported sampling rate %d for Incall Music", __func__,
7779 config->sample_rate);
7780 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
7781 ret = -EINVAL;
7782 goto error_open;
7783 }
7784 //FIXME: add support for MONO stream configuration when audioflinger mixer supports it
7785 switch (config->channel_mask) {
7786 case AUDIO_CHANNEL_NONE:
7787 case AUDIO_CHANNEL_OUT_STEREO:
7788 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
7789 break;
7790 default:
7791 ALOGE("%s: Unsupported channel mask %#x for Incall Music", __func__,
7792 config->channel_mask);
7793 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
7794 ret = -EINVAL;
7795 goto error_open;
7796 }
7797 switch (config->format) {
7798 case AUDIO_FORMAT_DEFAULT:
7799 case AUDIO_FORMAT_PCM_16_BIT:
7800 out->format = AUDIO_FORMAT_PCM_16_BIT;
7801 break;
7802 default:
7803 ALOGE("%s: Unsupported format %#x for Incall Music", __func__,
7804 config->format);
7805 config->format = AUDIO_FORMAT_PCM_16_BIT;
7806 ret = -EINVAL;
7807 goto error_open;
7808 }
7809
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05307810 ret = voice_extn_check_and_set_incall_music_usecase(adev, out);
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07007811 if (ret != 0) {
7812 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
Arun Mirpuri7da752a2018-09-11 18:01:15 -07007813 __func__, ret);
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07007814 goto error_open;
7815 }
Arun Mirpuri7da752a2018-09-11 18:01:15 -07007816 } else if (out->devices == AUDIO_DEVICE_OUT_TELEPHONY_TX) {
Aalique Grahame22e49102018-12-18 14:23:57 -08007817 switch (config->sample_rate) {
7818 case 0:
7819 out->sample_rate = AFE_PROXY_SAMPLING_RATE;
7820 break;
7821 case 8000:
7822 case 16000:
7823 case 48000:
7824 out->sample_rate = config->sample_rate;
7825 break;
7826 default:
7827 ALOGE("%s: Unsupported sampling rate %d for Telephony TX", __func__,
7828 config->sample_rate);
7829 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
7830 ret = -EINVAL;
7831 break;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07007832 }
Aalique Grahame22e49102018-12-18 14:23:57 -08007833 //FIXME: add support for MONO stream configuration when audioflinger mixer supports it
7834 switch (config->channel_mask) {
7835 case AUDIO_CHANNEL_NONE:
7836 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
7837 break;
7838 case AUDIO_CHANNEL_OUT_STEREO:
7839 out->channel_mask = config->channel_mask;
7840 break;
7841 default:
7842 ALOGE("%s: Unsupported channel mask %#x for Telephony TX", __func__,
7843 config->channel_mask);
7844 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
7845 ret = -EINVAL;
7846 break;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07007847 }
Aalique Grahame22e49102018-12-18 14:23:57 -08007848 switch (config->format) {
7849 case AUDIO_FORMAT_DEFAULT:
7850 out->format = AUDIO_FORMAT_PCM_16_BIT;
7851 break;
7852 case AUDIO_FORMAT_PCM_16_BIT:
7853 out->format = config->format;
7854 break;
7855 default:
7856 ALOGE("%s: Unsupported format %#x for Telephony TX", __func__,
7857 config->format);
7858 config->format = AUDIO_FORMAT_PCM_16_BIT;
7859 ret = -EINVAL;
7860 break;
7861 }
7862 if (ret != 0)
7863 goto error_open;
7864
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07007865 out->usecase = USECASE_AUDIO_PLAYBACK_AFE_PROXY;
7866 out->config = pcm_config_afe_proxy_playback;
Aalique Grahame22e49102018-12-18 14:23:57 -08007867 out->config.rate = out->sample_rate;
7868 out->config.channels =
7869 audio_channel_count_from_out_mask(out->channel_mask);
7870 out->config.format = pcm_format_from_audio_format(out->format);
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07007871 adev->voice_tx_output = out;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07007872 } else {
Ashish Jain058165c2016-09-28 23:18:48 +05307873 unsigned int channels = 0;
7874 /*Update config params to default if not set by the caller*/
7875 if (config->sample_rate == 0)
7876 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
7877 if (config->channel_mask == AUDIO_CHANNEL_NONE)
7878 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
7879 if (config->format == AUDIO_FORMAT_DEFAULT)
7880 config->format = AUDIO_FORMAT_PCM_16_BIT;
7881
7882 channels = audio_channel_count_from_out_mask(out->channel_mask);
7883
Varun Balaraje49253e2017-07-06 19:48:56 +05307884 if (out->flags & AUDIO_OUTPUT_FLAG_INTERACTIVE) {
7885 out->usecase = get_interactive_usecase(adev);
7886 out->config = pcm_config_low_latency;
7887 } else if (out->flags & AUDIO_OUTPUT_FLAG_RAW) {
Ashish Jain83a6cc22016-06-28 14:34:17 +05307888 out->usecase = USECASE_AUDIO_PLAYBACK_ULL;
Haynes Mathew George5beddd42016-06-27 18:33:40 -07007889 out->realtime = may_use_noirq_mode(adev, USECASE_AUDIO_PLAYBACK_ULL,
7890 out->flags);
7891 out->config = out->realtime ? pcm_config_rt : pcm_config_low_latency;
Haynes Mathew George16081042017-05-31 17:16:49 -07007892 } else if (out->flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) {
7893 out->usecase = USECASE_AUDIO_PLAYBACK_MMAP;
7894 out->config = pcm_config_mmap_playback;
7895 out->stream.start = out_start;
7896 out->stream.stop = out_stop;
7897 out->stream.create_mmap_buffer = out_create_mmap_buffer;
7898 out->stream.get_mmap_position = out_get_mmap_position;
Ashish Jain83a6cc22016-06-28 14:34:17 +05307899 } else if (out->flags & AUDIO_OUTPUT_FLAG_FAST) {
7900 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
Aniket Kumar Lata932f4872017-11-06 18:29:44 -08007901 out->hal_output_suspend_supported =
7902 property_get_bool("vendor.audio.hal.output.suspend.supported", false);
7903 out->dynamic_pm_qos_config_supported =
7904 property_get_bool("vendor.audio.hal.dynamic.qos.config.supported", false);
7905 if (!out->dynamic_pm_qos_config_supported) {
Alexy Joseph98988832017-01-13 14:56:59 -08007906 ALOGI("%s: dynamic qos voting not enabled for platform", __func__);
7907 } else {
7908 ALOGI("%s: dynamic qos voting enabled for platform", __func__);
7909 //the mixer path will be a string similar to "low-latency-playback resume"
7910 strlcpy(out->pm_qos_mixer_path, use_case_table[out->usecase], MAX_MIXER_PATH_LEN);
7911 strlcat(out->pm_qos_mixer_path,
7912 " resume", MAX_MIXER_PATH_LEN);
7913 ALOGI("%s: created %s pm_qos_mixer_path" , __func__,
7914 out->pm_qos_mixer_path);
7915 }
Ashish Jain83a6cc22016-06-28 14:34:17 +05307916 out->config = pcm_config_low_latency;
7917 } else if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
7918 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
7919 out->config = pcm_config_deep_buffer;
Ashish Jain058165c2016-09-28 23:18:48 +05307920 out->config.period_size = get_output_period_size(config->sample_rate, out->format,
7921 channels, DEEP_BUFFER_OUTPUT_PERIOD_DURATION);
7922 if (out->config.period_size <= 0) {
7923 ALOGE("Invalid configuration period size is not valid");
7924 ret = -EINVAL;
7925 goto error_open;
7926 }
Aalique Grahame22e49102018-12-18 14:23:57 -08007927 } else if (flags & AUDIO_OUTPUT_FLAG_TTS) {
7928 out->usecase = USECASE_AUDIO_PLAYBACK_TTS;
7929 out->config = pcm_config_deep_buffer;
Vignesh Kulothungana6927272019-02-20 15:17:07 -08007930 } else if (config->channel_mask & AUDIO_CHANNEL_HAPTIC_ALL) {
7931 out->usecase = USECASE_AUDIO_PLAYBACK_WITH_HAPTICS;
7932 out->config = pcm_config_haptics_audio;
7933 if (force_haptic_path)
7934 adev->haptics_config = pcm_config_haptics_audio;
7935 else
7936 adev->haptics_config = pcm_config_haptics;
7937
7938 out->config.channels =
7939 audio_channel_count_from_out_mask(out->channel_mask & ~AUDIO_CHANNEL_HAPTIC_ALL);
7940
7941 if (force_haptic_path) {
7942 out->config.channels = 1;
7943 adev->haptics_config.channels = 1;
7944 } else
7945 adev->haptics_config.channels = audio_channel_count_from_out_mask(out->channel_mask & AUDIO_CHANNEL_HAPTIC_ALL);
Derek Chenf6318be2017-06-12 17:16:24 -04007946 } else if (out->devices & AUDIO_DEVICE_OUT_BUS) {
7947 ret = audio_extn_auto_hal_open_output_stream(out);
7948 if (ret) {
7949 ALOGE("%s: Failed to open output stream for bus device", __func__);
7950 ret = -EINVAL;
7951 goto error_open;
7952 }
Ashish Jain83a6cc22016-06-28 14:34:17 +05307953 } else {
7954 /* primary path is the default path selected if no other outputs are available/suitable */
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08007955 out->usecase = GET_USECASE_AUDIO_PLAYBACK_PRIMARY(use_db_as_primary);
7956 out->config = GET_PCM_CONFIG_AUDIO_PLAYBACK_PRIMARY(use_db_as_primary);
Ashish Jain83a6cc22016-06-28 14:34:17 +05307957 }
7958 out->hal_ip_format = format = out->format;
7959 out->config.format = hal_format_to_pcm(out->hal_ip_format);
7960 out->hal_op_format = pcm_format_to_hal(out->config.format);
7961 out->bit_width = format_to_bitwidth_table[out->hal_op_format] << 3;
7962 out->config.rate = config->sample_rate;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07007963 out->sample_rate = out->config.rate;
Ashish Jain058165c2016-09-28 23:18:48 +05307964 out->config.channels = channels;
Ashish Jain83a6cc22016-06-28 14:34:17 +05307965 if (out->hal_ip_format != out->hal_op_format) {
7966 uint32_t buffer_size = out->config.period_size *
7967 format_to_bitwidth_table[out->hal_op_format] *
7968 out->config.channels;
7969 out->convert_buffer = calloc(1, buffer_size);
7970 if (out->convert_buffer == NULL){
7971 ALOGE("Allocation failed for convert buffer for size %d",
7972 out->compr_config.fragment_size);
7973 ret = -ENOMEM;
7974 goto error_open;
7975 }
7976 ALOGD("Convert buffer allocated of size %d", buffer_size);
7977 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007978 }
7979
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08007980 ALOGV("%s devices:%d, format:%x, out->sample_rate:%d,out->bit_width:%d out->format:%d out->flags:%x, flags: %x usecase %d",
7981 __func__, devices, format, out->sample_rate, out->bit_width, out->format, out->flags, flags, out->usecase);
Ashish Jain83a6cc22016-06-28 14:34:17 +05307982
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07007983 /* TODO remove this hardcoding and check why width is zero*/
7984 if (out->bit_width == 0)
7985 out->bit_width = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05307986 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07007987 &adev->streams_output_cfg_list,
Aalique Grahame65780b52017-09-27 14:59:56 -07007988 devices, out->flags, out->hal_op_format, out->sample_rate,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +05307989 out->bit_width, out->channel_mask, out->profile,
Manish Dewangan837dc462015-05-27 10:17:41 +05307990 &out->app_type_cfg);
Aalique Grahame6e763712019-01-31 16:18:17 -08007991 if ((out->usecase == (audio_usecase_t)(GET_USECASE_AUDIO_PLAYBACK_PRIMARY(use_db_as_primary))) ||
Haynes Mathew Georgebf143712013-12-03 13:02:53 -08007992 (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
7993 /* Ensure the default output is not selected twice */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08007994 if(adev->primary_output == NULL)
7995 adev->primary_output = out;
7996 else {
7997 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07007998 ret = -EEXIST;
7999 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08008000 }
8001 }
8002
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008003 /* Check if this usecase is already existing */
8004 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella7ce05352014-04-17 20:00:41 -07008005 if ((get_usecase_from_list(adev, out->usecase) != NULL) &&
8006 (out->usecase != USECASE_COMPRESS_VOIP_CALL)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008007 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008008 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07008009 ret = -EEXIST;
8010 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008011 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08008012
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008013 pthread_mutex_unlock(&adev->lock);
8014
8015 out->stream.common.get_sample_rate = out_get_sample_rate;
8016 out->stream.common.set_sample_rate = out_set_sample_rate;
8017 out->stream.common.get_buffer_size = out_get_buffer_size;
8018 out->stream.common.get_channels = out_get_channels;
8019 out->stream.common.get_format = out_get_format;
8020 out->stream.common.set_format = out_set_format;
8021 out->stream.common.standby = out_standby;
8022 out->stream.common.dump = out_dump;
8023 out->stream.common.set_parameters = out_set_parameters;
8024 out->stream.common.get_parameters = out_get_parameters;
8025 out->stream.common.add_audio_effect = out_add_audio_effect;
8026 out->stream.common.remove_audio_effect = out_remove_audio_effect;
8027 out->stream.get_latency = out_get_latency;
8028 out->stream.set_volume = out_set_volume;
Aalique Grahame22e49102018-12-18 14:23:57 -08008029#ifdef NO_AUDIO_OUT
8030 out->stream.write = out_write_for_no_output;
8031#else
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008032 out->stream.write = out_write;
Aalique Grahame22e49102018-12-18 14:23:57 -08008033#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008034 out->stream.get_render_position = out_get_render_position;
8035 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07008036 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008037
Haynes Mathew George16081042017-05-31 17:16:49 -07008038 if (out->realtime)
8039 out->af_period_multiplier = af_period_multiplier;
8040 else
8041 out->af_period_multiplier = 1;
8042
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008043 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07008044 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07008045 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008046
8047 config->format = out->stream.common.get_format(&out->stream.common);
8048 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
8049 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
Naresh Tanniru04f71882018-06-26 17:46:22 +05308050 register_format(out->format, out->supported_formats);
8051 register_channel_mask(out->channel_mask, out->supported_channel_masks);
8052 register_sample_rate(out->sample_rate, out->supported_sample_rates);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008053
Aalique Grahame22e49102018-12-18 14:23:57 -08008054 out->error_log = error_log_create(
8055 ERROR_LOG_ENTRIES,
8056 1000000000 /* aggregate consecutive identical errors within one second in ns */);
8057
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05308058 /*
8059 By locking output stream before registering, we allow the callback
8060 to update stream's state only after stream's initial state is set to
8061 adev state.
8062 */
8063 lock_output_stream(out);
8064 audio_extn_snd_mon_register_listener(out, out_snd_mon_cb);
8065 pthread_mutex_lock(&adev->lock);
8066 out->card_status = adev->card_status;
8067 pthread_mutex_unlock(&adev->lock);
8068 pthread_mutex_unlock(&out->lock);
8069
Aalique Grahame22e49102018-12-18 14:23:57 -08008070 stream_app_type_cfg_init(&out->app_type_cfg);
8071
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008072 *stream_out = &out->stream;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05308073 ALOGD("%s: Stream (%p) picks up usecase (%s)", __func__, &out->stream,
vivek mehta0ea887a2015-08-26 14:01:20 -07008074 use_case_table[out->usecase]);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08008075
8076 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
8077 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
8078 popcount(out->channel_mask), out->playback_started);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08008079 /* setup a channel for client <--> adsp communication for stream events */
Manish Dewangan21a850a2017-08-14 12:03:55 +05308080 is_direct_passthough = audio_extn_passthru_is_direct_passthrough(out);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08008081 if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
Naresh Tanniru85819452017-05-04 18:55:45 -07008082 (out->flags & AUDIO_OUTPUT_FLAG_DIRECT_PCM) ||
Gangadhar Sb0210342019-02-22 17:39:41 +05308083 audio_extn_ip_hdlr_intf_supported_for_copp(adev->platform) ||
8084 (audio_extn_ip_hdlr_intf_supported(config->format, is_direct_passthough, false))) {
Ben Rombergerd771a7c2017-02-22 18:05:17 -08008085 hdlr_stream_cfg.pcm_device_id = platform_get_pcm_device_id(
8086 out->usecase, PCM_PLAYBACK);
8087 hdlr_stream_cfg.flags = out->flags;
8088 hdlr_stream_cfg.type = PCM_PLAYBACK;
8089 ret = audio_extn_adsp_hdlr_stream_open(&out->adsp_hdlr_stream_handle,
8090 &hdlr_stream_cfg);
8091 if (ret) {
8092 ALOGE("%s: adsp_hdlr_stream_open failed %d",__func__, ret);
8093 out->adsp_hdlr_stream_handle = NULL;
8094 }
8095 }
Gangadhar Sb0210342019-02-22 17:39:41 +05308096 ip_hdlr_stream = audio_extn_ip_hdlr_intf_supported(config->format,
8097 is_direct_passthough, false);
8098 ip_hdlr_dev = audio_extn_ip_hdlr_intf_supported_for_copp(adev->platform);
8099 if (ip_hdlr_stream || ip_hdlr_dev ) {
Vidyakumar Athota2062f912017-06-27 14:46:15 -07008100 ret = audio_extn_ip_hdlr_intf_init(&out->ip_hdlr_handle, NULL, NULL, adev, out->usecase);
Naresh Tanniru85819452017-05-04 18:55:45 -07008101 if (ret < 0) {
8102 ALOGE("%s: audio_extn_ip_hdlr_intf_init failed %d",__func__, ret);
8103 out->ip_hdlr_handle = NULL;
8104 }
8105 }
Derek Chenf939fb72018-11-13 13:34:41 -08008106
8107 streams_output_ctxt_t *out_ctxt = (streams_output_ctxt_t *)
8108 calloc(1, sizeof(streams_output_ctxt_t));
8109 if (out_ctxt == NULL) {
8110 ALOGE("%s fail to allocate output ctxt", __func__);
8111 ret = -ENOMEM;
8112 goto error_open;
8113 }
8114 out_ctxt->output = out;
8115
8116 pthread_mutex_lock(&adev->lock);
8117 list_add_tail(&adev->active_outputs_list, &out_ctxt->list);
8118 pthread_mutex_unlock(&adev->lock);
8119
Eric Laurent994a6932013-07-17 11:51:42 -07008120 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008121 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07008122
8123error_open:
Ashish Jain83a6cc22016-06-28 14:34:17 +05308124 if (out->convert_buffer)
8125 free(out->convert_buffer);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07008126 free(out);
8127 *stream_out = NULL;
8128 ALOGD("%s: exit: ret %d", __func__, ret);
8129 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008130}
8131
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05308132void adev_close_output_stream(struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008133 struct audio_stream_out *stream)
8134{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008135 struct stream_out *out = (struct stream_out *)stream;
8136 struct audio_device *adev = out->dev;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08008137 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008138
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008139 ALOGD("%s: enter:stream_handle(%s)",__func__, use_case_table[out->usecase]);
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05308140
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05308141 // must deregister from sndmonitor first to prevent races
8142 // between the callback and close_stream
8143 audio_extn_snd_mon_unregister_listener(out);
8144
Ben Rombergerd771a7c2017-02-22 18:05:17 -08008145 /* close adsp hdrl session before standby */
8146 if (out->adsp_hdlr_stream_handle) {
8147 ret = audio_extn_adsp_hdlr_stream_close(out->adsp_hdlr_stream_handle);
8148 if (ret)
8149 ALOGE("%s: adsp_hdlr_stream_close failed %d",__func__, ret);
8150 out->adsp_hdlr_stream_handle = NULL;
8151 }
8152
Manish Dewangan21a850a2017-08-14 12:03:55 +05308153 if (out->ip_hdlr_handle) {
Naresh Tanniru85819452017-05-04 18:55:45 -07008154 audio_extn_ip_hdlr_intf_deinit(out->ip_hdlr_handle);
8155 out->ip_hdlr_handle = NULL;
8156 }
8157
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08008158 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05308159 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08008160 ret = voice_extn_compress_voip_close_output_stream(&stream->common);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05308161 out->started = 0;
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05308162 pthread_mutex_unlock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08008163 if(ret != 0)
8164 ALOGE("%s: Compress voip output cannot be closed, error:%d",
8165 __func__, ret);
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07008166 } else
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08008167 out_standby(&stream->common);
8168
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07008169 if (is_offload_usecase(out->usecase)) {
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08008170 audio_extn_dts_remove_state_notifier_node(out->usecase);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008171 destroy_offload_callback_thread(out);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07008172 free_offload_usecase(adev, out->usecase);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008173 if (out->compr_config.codec != NULL)
8174 free(out->compr_config.codec);
8175 }
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07008176
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05308177 out->a2dp_compress_mute = false;
8178
Varun Balaraje49253e2017-07-06 19:48:56 +05308179 if (is_interactive_usecase(out->usecase))
8180 free_interactive_usecase(adev, out->usecase);
8181
Ashish Jain83a6cc22016-06-28 14:34:17 +05308182 if (out->convert_buffer != NULL) {
8183 free(out->convert_buffer);
8184 out->convert_buffer = NULL;
8185 }
8186
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07008187 if (adev->voice_tx_output == out)
8188 adev->voice_tx_output = NULL;
8189
Aalique Grahame22e49102018-12-18 14:23:57 -08008190 error_log_destroy(out->error_log);
8191 out->error_log = NULL;
8192
Dhanalakshmi Siddani6c3d0992017-01-16 16:52:33 +05308193 if (adev->primary_output == out)
8194 adev->primary_output = NULL;
8195
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07008196 pthread_cond_destroy(&out->cond);
8197 pthread_mutex_destroy(&out->lock);
Derek Chenf939fb72018-11-13 13:34:41 -08008198
8199 pthread_mutex_lock(&adev->lock);
8200 streams_output_ctxt_t *out_ctxt = out_get_stream(adev, out->handle);
8201 if (out_ctxt != NULL) {
8202 list_remove(&out_ctxt->list);
8203 free(out_ctxt);
8204 } else {
8205 ALOGW("%s, output stream already closed", __func__);
8206 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008207 free(stream);
Derek Chenf939fb72018-11-13 13:34:41 -08008208 pthread_mutex_unlock(&adev->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07008209 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008210}
8211
8212static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
8213{
8214 struct audio_device *adev = (struct audio_device *)dev;
8215 struct str_parms *parms;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008216 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07008217 int val;
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07008218 int ret;
8219 int status = 0;
Aalique Grahame22e49102018-12-18 14:23:57 -08008220 bool a2dp_reconfig = false;
Zhou Songd6d71752019-05-21 18:08:51 +08008221 struct listnode *node;
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07008222 int controller = -1, stream = -1;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008223
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08008224 ALOGD("%s: enter: %s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008225 parms = str_parms_create_str(kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008226
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05308227 if (!parms)
8228 goto error;
Naresh Tanniru4c630392014-05-12 01:05:52 +05308229
Derek Chen6f293672019-04-01 01:40:24 -07008230 /* notify adev and input/output streams on the snd card status */
8231 adev_snd_mon_cb((void *)adev, parms);
8232
8233 list_for_each(node, &adev->active_outputs_list) {
8234 streams_output_ctxt_t *out_ctxt = node_to_item(node,
8235 streams_output_ctxt_t,
8236 list);
8237 out_snd_mon_cb((void *)out_ctxt->output, parms);
8238 }
8239
8240 list_for_each(node, &adev->active_inputs_list) {
8241 streams_input_ctxt_t *in_ctxt = node_to_item(node,
8242 streams_input_ctxt_t,
8243 list);
8244 in_snd_mon_cb((void *)in_ctxt->input, parms);
8245 }
8246
Zhou Songd6d71752019-05-21 18:08:51 +08008247 pthread_mutex_lock(&adev->lock);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05308248 ret = str_parms_get_str(parms, "BT_SCO", value, sizeof(value));
8249 if (ret >= 0) {
8250 /* When set to false, HAL should disable EC and NS */
Zhou Songd6d71752019-05-21 18:08:51 +08008251 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0){
Ashish Jain1b9b30c2017-05-18 20:57:40 +05308252 adev->bt_sco_on = true;
Zhou Songd6d71752019-05-21 18:08:51 +08008253 } else {
Ashish Jain1b9b30c2017-05-18 20:57:40 +05308254 adev->bt_sco_on = false;
Zhou Songd6d71752019-05-21 18:08:51 +08008255 audio_extn_sco_reset_configuration();
Kunlei Zhangd96af8f2019-08-27 16:33:29 +08008256 }
8257 }
8258
8259 ret = str_parms_get_str(parms, "A2dpSuspended", value, sizeof(value));
8260 if (ret>=0) {
8261 if (!strncmp(value, "false", 5) &&
8262 audio_extn_a2dp_source_is_suspended()) {
8263 struct audio_usecase *usecase;
8264 struct listnode *node;
Zhou Songd6d71752019-05-21 18:08:51 +08008265 list_for_each(node, &adev->usecase_list) {
8266 usecase = node_to_item(node, struct audio_usecase, list);
Kunlei Zhangd96af8f2019-08-27 16:33:29 +08008267 if (usecase->stream.in && (usecase->type == PCM_CAPTURE) &&
8268 ((usecase->stream.in->device & ~AUDIO_DEVICE_BIT_IN) &
8269 AUDIO_DEVICE_IN_ALL_SCO)) {
8270 ALOGD("a2dp resumed, switch bt sco mic to handset mic");
Zhou Songd6d71752019-05-21 18:08:51 +08008271 usecase->stream.in->device = AUDIO_DEVICE_IN_BUILTIN_MIC;
Kunlei Zhangd96af8f2019-08-27 16:33:29 +08008272 select_devices(adev, usecase->id);
8273 }
Zhou Songd6d71752019-05-21 18:08:51 +08008274 }
8275 }
Ashish Jain1b9b30c2017-05-18 20:57:40 +05308276 }
8277
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07008278 status = voice_set_parameters(adev, parms);
8279 if (status != 0)
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08008280 goto done;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008281
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07008282 status = platform_set_parameters(adev->platform, parms);
8283 if (status != 0)
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08008284 goto done;
8285
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07008286 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
8287 if (ret >= 0) {
Vicky Sehrawate240e5d2014-08-12 17:17:04 -07008288 /* When set to false, HAL should disable EC and NS */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008289 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
8290 adev->bluetooth_nrec = true;
8291 else
8292 adev->bluetooth_nrec = false;
8293 }
8294
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07008295 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
8296 if (ret >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008297 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
8298 adev->screen_off = false;
8299 else
8300 adev->screen_off = true;
Quinn Male70f20f32019-06-26 16:50:26 -07008301 audio_extn_sound_trigger_update_screen_status(adev->screen_off);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008302 }
8303
Eric Laurent4b084132018-10-19 17:33:43 -07008304 ret = str_parms_get_int(parms, "rotation", &val);
8305 if (ret >= 0) {
8306 bool reverse_speakers = false;
8307 int camera_rotation = CAMERA_ROTATION_LANDSCAPE;
8308 switch (val) {
8309 // FIXME: note that the code below assumes that the speakers are in the correct placement
8310 // relative to the user when the device is rotated 90deg from its default rotation. This
8311 // assumption is device-specific, not platform-specific like this code.
8312 case 270:
8313 reverse_speakers = true;
8314 camera_rotation = CAMERA_ROTATION_INVERT_LANDSCAPE;
8315 break;
8316 case 0:
8317 case 180:
8318 camera_rotation = CAMERA_ROTATION_PORTRAIT;
8319 break;
8320 case 90:
8321 camera_rotation = CAMERA_ROTATION_LANDSCAPE;
8322 break;
8323 default:
8324 ALOGE("%s: unexpected rotation of %d", __func__, val);
8325 status = -EINVAL;
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07008326 }
Eric Laurent4b084132018-10-19 17:33:43 -07008327 if (status == 0) {
8328 // check and set swap
8329 // - check if orientation changed and speaker active
8330 // - set rotation and cache the rotation value
8331 adev->camera_orientation =
8332 (adev->camera_orientation & ~CAMERA_ROTATION_MASK) | camera_rotation;
8333 if (!audio_extn_is_maxx_audio_enabled())
8334 platform_check_and_set_swap_lr_channels(adev, reverse_speakers);
8335 }
8336 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07008337
Mingming Yin514a8bc2014-07-29 15:22:21 -07008338 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_SCO_WB, value, sizeof(value));
8339 if (ret >= 0) {
8340 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
8341 adev->bt_wb_speech_enabled = true;
8342 else
8343 adev->bt_wb_speech_enabled = false;
8344 }
8345
Zhou Song12c29502019-03-16 10:37:18 +08008346 ret = str_parms_get_str(parms, "bt_swb", value, sizeof(value));
8347 if (ret >= 0) {
8348 val = atoi(value);
8349 adev->swb_speech_mode = val;
8350 }
8351
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -07008352 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value, sizeof(value));
8353 if (ret >= 0) {
8354 val = atoi(value);
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05308355 audio_devices_t device = (audio_devices_t) val;
Zhou Song681350a2017-10-19 16:28:42 +08008356 if (audio_is_output_device(val) &&
8357 (val & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07008358 ALOGV("cache new ext disp type and edid");
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07008359 platform_get_controller_stream_from_params(parms, &controller, &stream);
8360 platform_set_ext_display_device_v2(adev->platform, controller, stream);
8361 ret = platform_get_ext_disp_type_v2(adev->platform, controller, stream);
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07008362 if (ret < 0) {
8363 ALOGE("%s: Failed to query disp type, ret:%d", __func__, ret);
Manisha Agarwal2f5ff882018-08-08 17:09:29 +05308364 } else {
Vignesh Kulothungan39fc6a22019-08-01 00:55:59 -07008365 platform_cache_edid_v2(adev->platform, controller, stream);
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07008366 }
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05308367 } else if (audio_is_usb_out_device(device) || audio_is_usb_in_device(device)) {
vivek mehta344576a2016-04-12 18:56:03 -07008368 /*
8369 * Do not allow AFE proxy port usage by WFD source when USB headset is connected.
8370 * Per AudioPolicyManager, USB device is higher priority than WFD.
8371 * For Voice call over USB headset, voice call audio is routed to AFE proxy ports.
8372 * If WFD use case occupies AFE proxy, it may result unintended behavior while
8373 * starting voice call on USB
8374 */
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08008375 ret = str_parms_get_str(parms, "card", value, sizeof(value));
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05308376 if (ret >= 0)
8377 audio_extn_usb_add_device(device, atoi(value));
8378
Zhou Song6f862822017-11-06 17:27:57 +08008379 if (!audio_extn_usb_is_tunnel_supported()) {
8380 ALOGV("detected USB connect .. disable proxy");
8381 adev->allow_afe_proxy_usage = false;
8382 }
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -07008383 }
8384 }
8385
8386 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value, sizeof(value));
8387 if (ret >= 0) {
8388 val = atoi(value);
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05308389 audio_devices_t device = (audio_devices_t) val;
Garmond Leunge3b6d482016-10-25 16:48:01 -07008390 /*
8391 * The HDMI / Displayport disconnect handling has been moved to
8392 * audio extension to ensure that its parameters are not
8393 * invalidated prior to updating sysfs of the disconnect event
8394 * Invalidate will be handled by audio_extn_ext_disp_set_parameters()
8395 */
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05308396 if (audio_is_usb_out_device(device) || audio_is_usb_in_device(device)) {
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08008397 ret = str_parms_get_str(parms, "card", value, sizeof(value));
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05308398 if (ret >= 0)
8399 audio_extn_usb_remove_device(device, atoi(value));
8400
Zhou Song6f862822017-11-06 17:27:57 +08008401 if (!audio_extn_usb_is_tunnel_supported()) {
8402 ALOGV("detected USB disconnect .. enable proxy");
8403 adev->allow_afe_proxy_usage = true;
8404 }
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -07008405 }
8406 }
8407
Aalique Grahame22e49102018-12-18 14:23:57 -08008408 audio_extn_hfp_set_parameters(adev, parms);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008409 audio_extn_qdsp_set_parameters(adev, parms);
Aalique Grahame22e49102018-12-18 14:23:57 -08008410
8411 status = audio_extn_a2dp_set_parameters(parms, &a2dp_reconfig);
Aniket Kumar Lata23300322019-02-20 22:25:30 -08008412 if (status >= 0 && a2dp_reconfig) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05308413 struct audio_usecase *usecase;
8414 struct listnode *node;
8415 list_for_each(node, &adev->usecase_list) {
8416 usecase = node_to_item(node, struct audio_usecase, list);
Sujin Panicker390724d2019-04-26 10:43:36 +05308417 if (usecase->stream.out && (usecase->type == PCM_PLAYBACK) &&
Naresh Tanniruf7e9e632016-11-04 14:54:20 -07008418 (usecase->devices & AUDIO_DEVICE_OUT_ALL_A2DP)){
Naresh Tanniru9d027a62015-03-13 01:32:10 +05308419 ALOGD("reconfigure a2dp... forcing device switch");
Weiyin Jiang425180d2017-06-05 16:40:23 +08008420 pthread_mutex_unlock(&adev->lock);
Naresh Tannirucd2353e2016-08-19 00:37:25 +05308421 lock_output_stream(usecase->stream.out);
Weiyin Jiang425180d2017-06-05 16:40:23 +08008422 pthread_mutex_lock(&adev->lock);
Naresh Tannirucd2353e2016-08-19 00:37:25 +05308423 audio_extn_a2dp_set_handoff_mode(true);
Manisha Agarwalf1c3b6b2019-06-25 13:00:33 +05308424 ALOGD("Switching to speaker and muting the stream before select_devices");
8425 check_a2dp_restore_l(adev, usecase->stream.out, false);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05308426 //force device switch to re configure encoder
8427 select_devices(adev, usecase->id);
Manisha Agarwalf1c3b6b2019-06-25 13:00:33 +05308428 ALOGD("Unmuting the stream after select_devices");
8429 usecase->stream.out->a2dp_compress_mute = false;
8430 out_set_compr_volume(&usecase->stream.out->stream, usecase->stream.out->volume_l, usecase->stream.out->volume_r);
Naresh Tannirucd2353e2016-08-19 00:37:25 +05308431 audio_extn_a2dp_set_handoff_mode(false);
8432 pthread_mutex_unlock(&usecase->stream.out->lock);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05308433 break;
8434 }
8435 }
8436 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08008437
8438 //handle vr audio setparam
8439 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_VR_AUDIO_MODE,
8440 value, sizeof(value));
8441 if (ret >= 0) {
8442 ALOGI("Setting vr mode to be %s", value);
8443 if (!strncmp(value, "true", 4)) {
8444 adev->vr_audio_mode_enabled = true;
8445 ALOGI("Setting vr mode to true");
8446 } else if (!strncmp(value, "false", 5)) {
8447 adev->vr_audio_mode_enabled = false;
8448 ALOGI("Setting vr mode to false");
8449 } else {
8450 ALOGI("wrong vr mode set");
8451 }
8452 }
8453
Eric Laurent4b084132018-10-19 17:33:43 -07008454 //FIXME: to be replaced by proper video capture properties API
8455 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_CAMERA_FACING, value, sizeof(value));
8456 if (ret >= 0) {
8457 int camera_facing = CAMERA_FACING_BACK;
8458 if (strcmp(value, AUDIO_PARAMETER_VALUE_FRONT) == 0)
8459 camera_facing = CAMERA_FACING_FRONT;
8460 else if (strcmp(value, AUDIO_PARAMETER_VALUE_BACK) == 0)
8461 camera_facing = CAMERA_FACING_BACK;
8462 else {
8463 ALOGW("%s: invalid camera facing value: %s", __func__, value);
8464 goto done;
8465 }
8466 adev->camera_orientation =
8467 (adev->camera_orientation & ~CAMERA_FACING_MASK) | camera_facing;
8468 struct audio_usecase *usecase;
8469 struct listnode *node;
8470 list_for_each(node, &adev->usecase_list) {
8471 usecase = node_to_item(node, struct audio_usecase, list);
8472 struct stream_in *in = usecase->stream.in;
8473 if (usecase->type == PCM_CAPTURE && in != NULL &&
8474 in->source == AUDIO_SOURCE_CAMCORDER && !in->standby) {
8475 select_devices(adev, in->usecase);
8476 }
8477 }
8478 }
8479
Naresh Tannirucd2353e2016-08-19 00:37:25 +05308480 audio_extn_set_parameters(adev, parms);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08008481done:
8482 str_parms_destroy(parms);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08008483 pthread_mutex_unlock(&adev->lock);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05308484error:
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07008485 ALOGV("%s: exit with code(%d)", __func__, status);
8486 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008487}
8488
8489static char* adev_get_parameters(const struct audio_hw_device *dev,
8490 const char *keys)
8491{
Sidipotu Ashokaa4fa6a2018-12-21 09:19:26 +05308492 ALOGD("%s:%s", __func__, keys);
8493
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07008494 struct audio_device *adev = (struct audio_device *)dev;
8495 struct str_parms *reply = str_parms_create();
8496 struct str_parms *query = str_parms_create_str(keys);
8497 char *str;
Naresh Tannirud7205b62014-06-20 02:54:48 +05308498 char value[256] = {0};
8499 int ret = 0;
8500
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07008501 if (!query || !reply) {
Alexy Josephaee4fdd2016-01-29 13:02:07 -08008502 if (reply) {
8503 str_parms_destroy(reply);
8504 }
8505 if (query) {
8506 str_parms_destroy(query);
8507 }
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07008508 ALOGE("adev_get_parameters: failed to create query or reply");
8509 return NULL;
8510 }
8511
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08008512 //handle vr audio getparam
8513
8514 ret = str_parms_get_str(query,
8515 AUDIO_PARAMETER_KEY_VR_AUDIO_MODE,
8516 value, sizeof(value));
8517
8518 if (ret >= 0) {
8519 bool vr_audio_enabled = false;
8520 pthread_mutex_lock(&adev->lock);
8521 vr_audio_enabled = adev->vr_audio_mode_enabled;
8522 pthread_mutex_unlock(&adev->lock);
8523
8524 ALOGI("getting vr mode to %d", vr_audio_enabled);
8525
8526 if (vr_audio_enabled) {
8527 str_parms_add_str(reply, AUDIO_PARAMETER_KEY_VR_AUDIO_MODE,
8528 "true");
8529 goto exit;
8530 } else {
8531 str_parms_add_str(reply, AUDIO_PARAMETER_KEY_VR_AUDIO_MODE,
8532 "false");
8533 goto exit;
8534 }
8535 }
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07008536
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08008537 pthread_mutex_lock(&adev->lock);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07008538 audio_extn_get_parameters(adev, query, reply);
Shiv Maliyappanahallif9308492013-12-12 12:18:09 -08008539 voice_get_parameters(adev, query, reply);
Aalique Grahame22e49102018-12-18 14:23:57 -08008540 audio_extn_a2dp_get_parameters(query, reply);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07008541 platform_get_parameters(adev->platform, query, reply);
Naresh Tanniru80659832014-06-04 18:17:56 +05308542 pthread_mutex_unlock(&adev->lock);
8543
Naresh Tannirud7205b62014-06-20 02:54:48 +05308544exit:
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07008545 str = str_parms_to_str(reply);
8546 str_parms_destroy(query);
8547 str_parms_destroy(reply);
8548
Sidipotu Ashokaa4fa6a2018-12-21 09:19:26 +05308549 ALOGD("%s: exit: returns - %s", __func__, str);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07008550 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008551}
8552
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07008553static int adev_init_check(const struct audio_hw_device *dev __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008554{
8555 return 0;
8556}
8557
8558static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
8559{
Haynes Mathew George5191a852013-09-11 14:19:36 -07008560 int ret;
8561 struct audio_device *adev = (struct audio_device *)dev;
Aalique Grahame22e49102018-12-18 14:23:57 -08008562
8563 audio_extn_extspk_set_voice_vol(adev->extspk, volume);
8564
Haynes Mathew George5191a852013-09-11 14:19:36 -07008565 pthread_mutex_lock(&adev->lock);
8566 /* cache volume */
Shruthi Krishnaace10852013-10-25 14:32:12 -07008567 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07008568 pthread_mutex_unlock(&adev->lock);
8569 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008570}
8571
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07008572static int adev_set_master_volume(struct audio_hw_device *dev __unused,
8573 float volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008574{
8575 return -ENOSYS;
8576}
8577
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07008578static int adev_get_master_volume(struct audio_hw_device *dev __unused,
8579 float *volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008580{
8581 return -ENOSYS;
8582}
8583
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07008584static int adev_set_master_mute(struct audio_hw_device *dev __unused,
8585 bool muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008586{
8587 return -ENOSYS;
8588}
8589
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07008590static int adev_get_master_mute(struct audio_hw_device *dev __unused,
8591 bool *muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008592{
8593 return -ENOSYS;
8594}
8595
8596static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
8597{
8598 struct audio_device *adev = (struct audio_device *)dev;
Garmond Leung5fd0b552018-04-17 11:56:12 -07008599 struct listnode *node;
8600 struct audio_usecase *usecase = NULL;
8601 int ret = 0;
kunleizdc4af9d2017-05-04 12:15:35 +08008602
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008603 pthread_mutex_lock(&adev->lock);
8604 if (adev->mode != mode) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07008605 ALOGD("%s: mode %d\n", __func__, mode);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008606 adev->mode = mode;
Kunlei Zhang1d5c7f22019-05-21 14:25:57 +08008607 if (voice_is_in_call(adev) &&
8608 (mode == AUDIO_MODE_NORMAL ||
8609 (mode == AUDIO_MODE_IN_COMMUNICATION && !voice_is_call_state_active(adev)))) {
Garmond Leung5fd0b552018-04-17 11:56:12 -07008610 list_for_each(node, &adev->usecase_list) {
8611 usecase = node_to_item(node, struct audio_usecase, list);
8612 if (usecase->type == VOICE_CALL)
8613 break;
8614 }
8615 if (usecase &&
8616 audio_is_usb_out_device(usecase->out_snd_device & AUDIO_DEVICE_OUT_ALL_USB)) {
8617 ret = audio_extn_usb_check_and_set_svc_int(usecase,
8618 true);
8619 if (ret != 0) {
8620 /* default service interval was successfully updated,
8621 reopen USB backend with new service interval */
8622 check_usecases_codec_backend(adev,
8623 usecase,
8624 usecase->out_snd_device);
8625 }
8626 }
8627
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07008628 voice_stop_call(adev);
Banajit Goswami20cdd212015-09-11 01:11:30 -07008629 platform_set_gsm_mode(adev->platform, false);
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07008630 adev->current_call_output = NULL;
kunleizdc4af9d2017-05-04 12:15:35 +08008631 // restore device for other active usecases after stop call
8632 list_for_each(node, &adev->usecase_list) {
8633 usecase = node_to_item(node, struct audio_usecase, list);
8634 select_devices(adev, usecase->id);
8635 }
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07008636 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008637 }
8638 pthread_mutex_unlock(&adev->lock);
8639 return 0;
8640}
8641
8642static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
8643{
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08008644 int ret;
Aalique Grahame22e49102018-12-18 14:23:57 -08008645 struct audio_device *adev = (struct audio_device *)dev;
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08008646
8647 pthread_mutex_lock(&adev->lock);
Vidyakumar Athota2850d532013-11-19 16:02:12 -08008648 ALOGD("%s state %d\n", __func__, state);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08008649 ret = voice_set_mic_mute((struct audio_device *)dev, state);
Aalique Grahame22e49102018-12-18 14:23:57 -08008650
Derek Chend2530072014-11-24 12:39:14 -08008651 if (adev->ext_hw_plugin)
8652 ret = audio_extn_ext_hw_plugin_set_mic_mute(adev->ext_hw_plugin, state);
Aalique Grahame22e49102018-12-18 14:23:57 -08008653
8654 adev->mic_muted = state;
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08008655 pthread_mutex_unlock(&adev->lock);
8656
8657 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008658}
8659
8660static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
8661{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07008662 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008663 return 0;
8664}
8665
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07008666static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008667 const struct audio_config *config)
8668{
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07008669 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008670
Aalique Grahame22e49102018-12-18 14:23:57 -08008671 /* Don't know if USB HIFI in this context so use true to be conservative */
8672 if (check_input_parameters(config->sample_rate, config->format, channel_count,
8673 true /*is_usb_hifi */) != 0)
8674 return 0;
8675
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07008676 return get_input_buffer_size(config->sample_rate, config->format, channel_count,
8677 false /* is_low_latency: since we don't know, be conservative */);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008678}
8679
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008680static bool adev_input_allow_hifi_record(struct audio_device *adev,
8681 audio_devices_t devices,
8682 audio_input_flags_t flags,
8683 audio_source_t source) {
8684 const bool allowed = true;
8685
8686 if (!audio_is_usb_in_device(devices))
8687 return !allowed;
8688
8689 switch (flags) {
8690 case AUDIO_INPUT_FLAG_NONE:
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008691 break;
Haynes Mathew George59862182017-10-24 16:23:57 -07008692 case AUDIO_INPUT_FLAG_FAST: // disallow hifi record for FAST as
8693 // it affects RTD numbers over USB
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008694 default:
8695 return !allowed;
8696 }
8697
8698 switch (source) {
8699 case AUDIO_SOURCE_DEFAULT:
8700 case AUDIO_SOURCE_MIC:
8701 case AUDIO_SOURCE_UNPROCESSED:
8702 break;
8703 default:
8704 return !allowed;
8705 }
8706
8707 switch (adev->mode) {
8708 case 0:
8709 break;
8710 default:
8711 return !allowed;
8712 }
8713
8714 return allowed;
8715}
8716
Haynes Mathew George4ffef292017-11-21 15:08:02 -08008717static int adev_update_voice_comm_input_stream(struct stream_in *in,
8718 struct audio_config *config)
8719{
8720 bool valid_rate = (config->sample_rate == 8000 ||
8721 config->sample_rate == 16000 ||
8722 config->sample_rate == 32000 ||
8723 config->sample_rate == 48000);
8724 bool valid_ch = audio_channel_count_from_in_mask(in->channel_mask) == 1;
8725
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008726 if(!voice_extn_is_compress_voip_supported()) {
kunleiz28c73e72019-03-27 17:24:04 +08008727 if (valid_rate && valid_ch) {
Haynes Mathew George4ffef292017-11-21 15:08:02 -08008728 in->usecase = USECASE_AUDIO_RECORD_VOIP;
8729 in->config = default_pcm_config_voip_copp;
8730 in->config.period_size = VOIP_IO_BUF_SIZE(in->sample_rate,
8731 DEFAULT_VOIP_BUF_DURATION_MS,
8732 DEFAULT_VOIP_BIT_DEPTH_BYTE)/2;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008733 } else {
8734 ALOGW("%s No valid input in voip, use defaults"
8735 "sample rate %u, channel mask 0x%X",
8736 __func__, config->sample_rate, in->channel_mask);
8737 }
Haynes Mathew George4ffef292017-11-21 15:08:02 -08008738 in->config.rate = config->sample_rate;
8739 in->sample_rate = config->sample_rate;
8740 } else {
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008741 //XXX needed for voice_extn_compress_voip_open_input_stream
8742 in->config.rate = config->sample_rate;
8743 if ((in->dev->mode == AUDIO_MODE_IN_COMMUNICATION ||
Shalini Manjunathaa763cc42019-08-23 15:13:46 +05308744 in->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008745 voice_extn_compress_voip_is_active(in->dev)) &&
8746 (voice_extn_compress_voip_is_format_supported(in->format)) &&
8747 valid_rate && valid_ch) {
8748 voice_extn_compress_voip_open_input_stream(in);
8749 // update rate entries to match config from AF
8750 in->config.rate = config->sample_rate;
8751 in->sample_rate = config->sample_rate;
8752 } else {
8753 ALOGW("%s compress voip not active, use defaults", __func__);
8754 }
Haynes Mathew George4ffef292017-11-21 15:08:02 -08008755 }
Haynes Mathew George4ffef292017-11-21 15:08:02 -08008756 return 0;
8757}
8758
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008759static int adev_open_input_stream(struct audio_hw_device *dev,
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07008760 audio_io_handle_t handle,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008761 audio_devices_t devices,
8762 struct audio_config *config,
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07008763 struct audio_stream_in **stream_in,
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05308764 audio_input_flags_t flags,
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07008765 const char *address __unused,
Vidyakumar Athota5c398212015-03-31 21:53:21 -07008766 audio_source_t source)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008767{
8768 struct audio_device *adev = (struct audio_device *)dev;
8769 struct stream_in *in;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08008770 int ret = 0, buffer_size, frame_size;
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07008771 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07008772 bool is_low_latency = false;
Divya Narayanan Poojary45f19192016-09-30 18:52:13 +05308773 bool channel_mask_updated = false;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008774 bool is_usb_dev = audio_is_usb_in_device(devices);
8775 bool may_use_hifi_record = adev_input_allow_hifi_record(adev,
8776 devices,
8777 flags,
8778 source);
Andy Hung94320602018-10-29 18:31:12 -07008779 ALOGV("%s: enter: flags %#x, is_usb_dev %d, may_use_hifi_record %d,"
8780 " sample_rate %u, channel_mask %#x, format %#x",
8781 __func__, flags, is_usb_dev, may_use_hifi_record,
8782 config->sample_rate, config->channel_mask, config->format);
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05308783
kunleizdff872d2018-08-20 14:40:33 +08008784 if (is_usb_dev && (!audio_extn_usb_connected(NULL))) {
kunleizd6a9e0c2018-07-30 15:38:52 +08008785 is_usb_dev = false;
8786 devices = AUDIO_DEVICE_IN_BUILTIN_MIC;
8787 ALOGW("%s: ignore set device to non existing USB card, use input device(%#x)",
8788 __func__, devices);
8789 }
8790
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008791 *stream_in = NULL;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008792
8793 if (!(is_usb_dev && may_use_hifi_record)) {
8794 if (config->sample_rate == 0)
8795 config->sample_rate = 48000;
8796 if (config->channel_mask == AUDIO_CHANNEL_NONE)
8797 config->channel_mask = AUDIO_CHANNEL_IN_MONO;
8798 if (config->format == AUDIO_FORMAT_DEFAULT)
8799 config->format = AUDIO_FORMAT_PCM_16_BIT;
8800
8801 channel_count = audio_channel_count_from_in_mask(config->channel_mask);
8802
Aalique Grahame22e49102018-12-18 14:23:57 -08008803 if (check_input_parameters(config->sample_rate, config->format, channel_count,
8804 false) != 0)
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008805 return -EINVAL;
Chaithanya Krishna Bacharaju9955b162016-05-25 16:25:53 +05308806 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008807
Rahul Sharma99770982019-03-06 17:05:26 +05308808 pthread_mutex_lock(&adev->lock);
8809 if (in_get_stream(adev, handle) != NULL) {
8810 ALOGW("%s, input stream already opened", __func__);
8811 ret = -EEXIST;
8812 }
8813 pthread_mutex_unlock(&adev->lock);
8814 if (ret)
8815 return ret;
8816
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008817 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07008818
8819 if (!in) {
8820 ALOGE("failed to allocate input stream");
8821 return -ENOMEM;
8822 }
8823
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05308824 ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x)\
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05308825 stream_handle(%p) io_handle(%d) source(%d) format %x",__func__, config->sample_rate,
8826 config->channel_mask, devices, &in->stream, handle, source, config->format);
Ravi Kumar Alamanda40703102014-04-24 10:34:41 -07008827 pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07008828 pthread_mutex_init(&in->pre_lock, (const pthread_mutexattr_t *) NULL);
Ravi Kumar Alamanda40703102014-04-24 10:34:41 -07008829
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008830 in->stream.common.get_sample_rate = in_get_sample_rate;
8831 in->stream.common.set_sample_rate = in_set_sample_rate;
8832 in->stream.common.get_buffer_size = in_get_buffer_size;
8833 in->stream.common.get_channels = in_get_channels;
8834 in->stream.common.get_format = in_get_format;
8835 in->stream.common.set_format = in_set_format;
8836 in->stream.common.standby = in_standby;
8837 in->stream.common.dump = in_dump;
8838 in->stream.common.set_parameters = in_set_parameters;
8839 in->stream.common.get_parameters = in_get_parameters;
8840 in->stream.common.add_audio_effect = in_add_audio_effect;
8841 in->stream.common.remove_audio_effect = in_remove_audio_effect;
8842 in->stream.set_gain = in_set_gain;
8843 in->stream.read = in_read;
8844 in->stream.get_input_frames_lost = in_get_input_frames_lost;
Aalique Grahame22e49102018-12-18 14:23:57 -08008845 in->stream.get_capture_position = in_get_capture_position;
Naresh Tannirudcb47c52018-06-25 16:23:32 +05308846 in->stream.get_active_microphones = in_get_active_microphones;
Paul McLeana50b7332018-12-17 08:24:21 -07008847 in->stream.set_microphone_direction = in_set_microphone_direction;
8848 in->stream.set_microphone_field_dimension = in_set_microphone_field_dimension;
juyuchendb308c22019-01-21 11:57:17 -07008849 in->stream.update_sink_metadata = in_update_sink_metadata;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008850
8851 in->device = devices;
Vidyakumar Athota5c398212015-03-31 21:53:21 -07008852 in->source = source;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008853 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008854 in->standby = 1;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07008855 in->capture_handle = handle;
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07008856 in->flags = flags;
Haynes Mathew George46740472017-10-27 18:40:12 -07008857 in->bit_width = 16;
8858 in->af_period_multiplier = 1;
justinweng20fb6d82019-02-21 18:49:00 -07008859 in->direction = MIC_DIRECTION_UNSPECIFIED;
8860 in->zoom = 0;
Carter Hsu2e429db2019-05-14 18:50:52 +08008861 list_init(&in->aec_list);
8862 list_init(&in->ns_list);
Phil Burkd898ba62019-06-20 12:49:01 -07008863 in->mmap_shared_memory_fd = -1; // not open
Haynes Mathew George46740472017-10-27 18:40:12 -07008864
Andy Hung94320602018-10-29 18:31:12 -07008865 ALOGV("%s: source %d, config->channel_mask %#x", __func__, source, config->channel_mask);
Aalique Grahame22e49102018-12-18 14:23:57 -08008866 if (source == AUDIO_SOURCE_VOICE_UPLINK ||
8867 source == AUDIO_SOURCE_VOICE_DOWNLINK) {
8868 /* Force channel config requested to mono if incall
8869 record is being requested for only uplink/downlink */
8870 if (config->channel_mask != AUDIO_CHANNEL_IN_MONO) {
8871 config->channel_mask = AUDIO_CHANNEL_IN_MONO;
8872 ret = -EINVAL;
8873 goto err_open;
8874 }
8875 }
8876
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008877 if (is_usb_dev && may_use_hifi_record) {
8878 /* HiFi record selects an appropriate format, channel, rate combo
8879 depending on sink capabilities*/
8880 ret = read_usb_sup_params_and_compare(false /*is_playback*/,
8881 &config->format,
8882 &in->supported_formats[0],
8883 MAX_SUPPORTED_FORMATS,
8884 &config->channel_mask,
8885 &in->supported_channel_masks[0],
8886 MAX_SUPPORTED_CHANNEL_MASKS,
8887 &config->sample_rate,
8888 &in->supported_sample_rates[0],
8889 MAX_SUPPORTED_SAMPLE_RATES);
8890 if (ret != 0) {
8891 ret = -EINVAL;
8892 goto err_open;
8893 }
8894 channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Haynes Mathew George4ffef292017-11-21 15:08:02 -08008895 } else if (config->format == AUDIO_FORMAT_DEFAULT) {
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05308896 config->format = AUDIO_FORMAT_PCM_16_BIT;
Surendar karkaaca3d082017-11-09 15:18:37 +05308897 } else if (property_get_bool("vendor.audio.capture.pcm.32bit.enable", false)
8898 && config->format == AUDIO_FORMAT_PCM_32_BIT) {
8899 in->config.format = PCM_FORMAT_S32_LE;
8900 in->bit_width = 32;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05308901 } else if ((config->format == AUDIO_FORMAT_PCM_FLOAT) ||
8902 (config->format == AUDIO_FORMAT_PCM_32_BIT) ||
8903 (config->format == AUDIO_FORMAT_PCM_24_BIT_PACKED) ||
8904 (config->format == AUDIO_FORMAT_PCM_8_24_BIT)) {
8905 bool ret_error = false;
8906 in->bit_width = 24;
8907 /* 24 bit is restricted to UNPROCESSED source only,also format supported
8908 from HAL is 24_packed and 8_24
8909 *> In case of UNPROCESSED source, for 24 bit, if format requested is other than
8910 24_packed return error indicating supported format is 24_packed
8911 *> In case of any other source requesting 24 bit or float return error
8912 indicating format supported is 16 bit only.
8913
8914 on error flinger will retry with supported format passed
8915 */
8916 if ((source != AUDIO_SOURCE_UNPROCESSED) &&
8917 (source != AUDIO_SOURCE_CAMCORDER)) {
8918 config->format = AUDIO_FORMAT_PCM_16_BIT;
8919 if (config->sample_rate > 48000)
8920 config->sample_rate = 48000;
8921 ret_error = true;
Haynes Mathew George46740472017-10-27 18:40:12 -07008922 } else if (!(config->format == AUDIO_FORMAT_PCM_24_BIT_PACKED ||
8923 config->format == AUDIO_FORMAT_PCM_8_24_BIT)) {
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05308924 config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
8925 ret_error = true;
8926 }
8927
8928 if (ret_error) {
8929 ret = -EINVAL;
8930 goto err_open;
8931 }
8932 }
8933
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008934 in->channel_mask = config->channel_mask;
8935 in->format = config->format;
8936
8937 in->usecase = USECASE_AUDIO_RECORD;
Samyak Jain0aa07ab2019-04-04 14:36:32 +05308938
8939 if (in->source == AUDIO_SOURCE_FM_TUNER) {
8940 if(!get_usecase_from_list(adev, USECASE_AUDIO_RECORD_FM_VIRTUAL))
8941 in->usecase = USECASE_AUDIO_RECORD_FM_VIRTUAL;
8942 else {
8943 ret = -EINVAL;
8944 goto err_open;
8945 }
8946 }
8947
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008948 if (config->sample_rate == LOW_LATENCY_CAPTURE_SAMPLE_RATE &&
Deeraj Somanfa377bf2019-02-06 12:57:59 +05308949 (flags & AUDIO_INPUT_FLAG_TIMESTAMP) == 0 &&
8950 (flags & AUDIO_INPUT_FLAG_COMPRESS) == 0 &&
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008951 (flags & AUDIO_INPUT_FLAG_FAST) != 0) {
8952 is_low_latency = true;
8953#if LOW_LATENCY_CAPTURE_USE_CASE
8954 in->usecase = USECASE_AUDIO_RECORD_LOW_LATENCY;
8955#endif
8956 in->realtime = may_use_noirq_mode(adev, in->usecase, in->flags);
Aalique Grahame22e49102018-12-18 14:23:57 -08008957 if (!in->realtime) {
8958 in->config = pcm_config_audio_capture;
8959 frame_size = audio_stream_in_frame_size(&in->stream);
8960 buffer_size = get_input_buffer_size(config->sample_rate,
8961 config->format,
8962 channel_count,
8963 is_low_latency);
8964 in->config.period_size = buffer_size / frame_size;
8965 in->config.rate = config->sample_rate;
8966 in->af_period_multiplier = 1;
8967 } else {
8968 // period size is left untouched for rt mode playback
8969 in->config = pcm_config_audio_capture_rt;
8970 in->af_period_multiplier = af_period_multiplier;
8971 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008972 }
8973
8974 if ((config->sample_rate == LOW_LATENCY_CAPTURE_SAMPLE_RATE) &&
8975 ((in->flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0)) {
8976 in->realtime = 0;
8977 in->usecase = USECASE_AUDIO_RECORD_MMAP;
8978 in->config = pcm_config_mmap_capture;
Haynes Mathew George46740472017-10-27 18:40:12 -07008979 in->config.format = pcm_format_from_audio_format(config->format);
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008980 in->stream.start = in_start;
8981 in->stream.stop = in_stop;
8982 in->stream.create_mmap_buffer = in_create_mmap_buffer;
8983 in->stream.get_mmap_position = in_get_mmap_position;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008984 ALOGV("%s: USECASE_AUDIO_RECORD_MMAP", __func__);
8985 } else if (in->realtime) {
8986 in->config = pcm_config_audio_capture_rt;
Haynes Mathew George46740472017-10-27 18:40:12 -07008987 in->config.format = pcm_format_from_audio_format(config->format);
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008988 in->af_period_multiplier = af_period_multiplier;
Haynes Mathew George46740472017-10-27 18:40:12 -07008989 } else if (is_usb_dev && may_use_hifi_record) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008990 in->usecase = USECASE_AUDIO_RECORD_HIFI;
8991 in->config = pcm_config_audio_capture;
8992 frame_size = audio_stream_in_frame_size(&in->stream);
8993 buffer_size = get_input_buffer_size(config->sample_rate,
8994 config->format,
8995 channel_count,
8996 false /*is_low_latency*/);
8997 in->config.period_size = buffer_size / frame_size;
8998 in->config.rate = config->sample_rate;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008999 in->config.format = pcm_format_from_audio_format(config->format);
Karthikeyan Mani07faa602018-08-20 11:01:32 -07009000 switch (config->format) {
9001 case AUDIO_FORMAT_PCM_32_BIT:
9002 in->bit_width = 32;
9003 break;
9004 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
9005 case AUDIO_FORMAT_PCM_8_24_BIT:
9006 in->bit_width = 24;
9007 break;
9008 default:
9009 in->bit_width = 16;
9010 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07009011 } else if ((in->device == AUDIO_DEVICE_IN_TELEPHONY_RX) ||
Dhanalakshmi Siddani0b1488e2016-09-06 12:58:42 +05309012 (in->device == AUDIO_DEVICE_IN_PROXY)) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07009013 if (config->sample_rate == 0)
9014 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
9015 if (config->sample_rate != 48000 && config->sample_rate != 16000 &&
9016 config->sample_rate != 8000) {
9017 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
9018 ret = -EINVAL;
9019 goto err_open;
9020 }
9021 if (config->format == AUDIO_FORMAT_DEFAULT)
9022 config->format = AUDIO_FORMAT_PCM_16_BIT;
9023 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
9024 config->format = AUDIO_FORMAT_PCM_16_BIT;
9025 ret = -EINVAL;
9026 goto err_open;
9027 }
9028
9029 in->usecase = USECASE_AUDIO_RECORD_AFE_PROXY;
9030 in->config = pcm_config_afe_proxy_record;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07009031 in->config.rate = config->sample_rate;
Aalique Grahame22e49102018-12-18 14:23:57 -08009032 in->af_period_multiplier = 1;
9033 } else if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
9034 in->flags & AUDIO_INPUT_FLAG_VOIP_TX &&
9035 (config->sample_rate == 8000 ||
9036 config->sample_rate == 16000 ||
9037 config->sample_rate == 32000 ||
9038 config->sample_rate == 48000) &&
9039 channel_count == 1) {
9040 in->usecase = USECASE_AUDIO_RECORD_VOIP;
9041 in->config = pcm_config_audio_capture;
9042 frame_size = audio_stream_in_frame_size(&in->stream);
9043 buffer_size = get_stream_buffer_size(VOIP_CAPTURE_PERIOD_DURATION_MSEC,
9044 config->sample_rate,
9045 config->format,
9046 channel_count, false /*is_low_latency*/);
9047 in->config.period_size = buffer_size / frame_size;
9048 in->config.period_count = VOIP_CAPTURE_PERIOD_COUNT;
9049 in->config.rate = config->sample_rate;
9050 in->af_period_multiplier = 1;
Haynes Mathew George46740472017-10-27 18:40:12 -07009051 } else {
Revathi Uddarajud2634032017-12-07 14:42:34 +05309052 int ret_val;
9053 pthread_mutex_lock(&adev->lock);
9054 ret_val = audio_extn_check_and_set_multichannel_usecase(adev,
9055 in, config, &channel_mask_updated);
9056 pthread_mutex_unlock(&adev->lock);
9057
9058 if (!ret_val) {
9059 if (channel_mask_updated == true) {
9060 ALOGD("%s: return error to retry with updated channel mask (%#x)",
9061 __func__, config->channel_mask);
9062 ret = -EINVAL;
9063 goto err_open;
9064 }
9065 ALOGD("%s: created multi-channel session succesfully",__func__);
9066 } else if (audio_extn_compr_cap_enabled() &&
9067 audio_extn_compr_cap_format_supported(config->format) &&
9068 (in->dev->mode != AUDIO_MODE_IN_COMMUNICATION)) {
9069 audio_extn_compr_cap_init(in);
9070 } else if (audio_extn_cin_applicable_stream(in)) {
Deeraj Soman14230922019-01-30 16:39:30 +05309071 ret = audio_extn_cin_configure_input_stream(in, config);
Revathi Uddarajud2634032017-12-07 14:42:34 +05309072 if (ret)
9073 goto err_open;
9074 } else {
9075 in->config = pcm_config_audio_capture;
9076 in->config.rate = config->sample_rate;
9077 in->config.format = pcm_format_from_audio_format(config->format);
Revathi Uddarajud2634032017-12-07 14:42:34 +05309078 in->format = config->format;
9079 frame_size = audio_stream_in_frame_size(&in->stream);
9080 buffer_size = get_input_buffer_size(config->sample_rate,
Haynes Mathew George46740472017-10-27 18:40:12 -07009081 config->format,
9082 channel_count,
9083 is_low_latency);
Dieter Luecking5d57def2018-09-07 14:23:37 +02009084 /* prevent division-by-zero */
9085 if (frame_size == 0) {
9086 ALOGE("%s: Error frame_size==0", __func__);
9087 ret = -EINVAL;
9088 goto err_open;
9089 }
9090
Revathi Uddarajud2634032017-12-07 14:42:34 +05309091 in->config.period_size = buffer_size / frame_size;
Aalique Grahame22e49102018-12-18 14:23:57 -08009092 in->af_period_multiplier = 1;
Haynes Mathew George4ffef292017-11-21 15:08:02 -08009093
Revathi Uddarajud2634032017-12-07 14:42:34 +05309094 if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
9095 /* optionally use VOIP usecase depending on config(s) */
9096 ret = adev_update_voice_comm_input_stream(in, config);
9097 }
Haynes Mathew George4ffef292017-11-21 15:08:02 -08009098
Revathi Uddarajud2634032017-12-07 14:42:34 +05309099 if (ret) {
9100 ALOGE("%s AUDIO_SOURCE_VOICE_COMMUNICATION invalid args", __func__);
9101 goto err_open;
9102 }
Haynes Mathew George4ffef292017-11-21 15:08:02 -08009103 }
Samyak Jainc37062f2019-04-25 18:41:06 +05309104 if (audio_extn_is_concurrent_capture_enabled()) {
9105 /* Acquire lock to avoid two concurrent use cases initialized to
9106 same pcm record use case */
kunleiz28c73e72019-03-27 17:24:04 +08009107
Samyak Jainc37062f2019-04-25 18:41:06 +05309108 if (in->usecase == USECASE_AUDIO_RECORD) {
9109 pthread_mutex_lock(&adev->lock);
9110 if (!(adev->pcm_record_uc_state)) {
9111 ALOGV("%s: using USECASE_AUDIO_RECORD",__func__);
9112 adev->pcm_record_uc_state = 1;
9113 pthread_mutex_unlock(&adev->lock);
9114 } else {
9115 pthread_mutex_unlock(&adev->lock);
9116 /* Assign compress record use case for second record */
9117 in->usecase = USECASE_AUDIO_RECORD_COMPRESS2;
9118 in->flags |= AUDIO_INPUT_FLAG_COMPRESS;
9119 ALOGV("%s: overriding usecase with USECASE_AUDIO_RECORD_COMPRESS2 and appending compress flag", __func__);
9120 if (audio_extn_cin_applicable_stream(in)) {
9121 in->sample_rate = config->sample_rate;
Deeraj Soman14230922019-01-30 16:39:30 +05309122 ret = audio_extn_cin_configure_input_stream(in, config);
Samyak Jainc37062f2019-04-25 18:41:06 +05309123 if (ret)
9124 goto err_open;
9125 }
9126 }
9127 }
kunleiz28c73e72019-03-27 17:24:04 +08009128 }
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07009129 }
Ramjee Singh82fd0c12019-08-21 16:31:33 +05309130 if (audio_extn_ssr_get_stream() != in)
9131 in->config.channels = channel_count;
Aniket Kumar Lata23d8cbf2019-04-10 19:54:46 -07009132
Aniket Kumar Lata23d8cbf2019-04-10 19:54:46 -07009133 in->sample_rate = in->config.rate;
9134
Dhananjay Kumard6d32152016-10-13 16:11:03 +05309135 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
9136 &adev->streams_input_cfg_list,
Haynes Mathew George4ffef292017-11-21 15:08:02 -08009137 devices, flags, in->format,
9138 in->sample_rate, in->bit_width,
9139 in->profile, &in->app_type_cfg);
Naresh Tanniru04f71882018-06-26 17:46:22 +05309140 register_format(in->format, in->supported_formats);
9141 register_channel_mask(in->channel_mask, in->supported_channel_masks);
9142 register_sample_rate(in->sample_rate, in->supported_sample_rates);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05309143
Aalique Grahame22e49102018-12-18 14:23:57 -08009144 in->error_log = error_log_create(
9145 ERROR_LOG_ENTRIES,
9146 1000000000 /* aggregate consecutive identical errors within one second */);
9147
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07009148 /* This stream could be for sound trigger lab,
9149 get sound trigger pcm if present */
9150 audio_extn_sound_trigger_check_and_get_session(in);
9151
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05309152 lock_input_stream(in);
9153 audio_extn_snd_mon_register_listener(in, in_snd_mon_cb);
9154 pthread_mutex_lock(&adev->lock);
9155 in->card_status = adev->card_status;
9156 pthread_mutex_unlock(&adev->lock);
9157 pthread_mutex_unlock(&in->lock);
9158
Aalique Grahame22e49102018-12-18 14:23:57 -08009159 stream_app_type_cfg_init(&in->app_type_cfg);
9160
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009161 *stream_in = &in->stream;
Derek Chenf939fb72018-11-13 13:34:41 -08009162
9163 streams_input_ctxt_t *in_ctxt = (streams_input_ctxt_t *)
9164 calloc(1, sizeof(streams_input_ctxt_t));
9165 if (in_ctxt == NULL) {
9166 ALOGE("%s fail to allocate input ctxt", __func__);
9167 ret = -ENOMEM;
9168 goto err_open;
9169 }
9170 in_ctxt->input = in;
9171
9172 pthread_mutex_lock(&adev->lock);
9173 list_add_tail(&adev->active_inputs_list, &in_ctxt->list);
9174 pthread_mutex_unlock(&adev->lock);
9175
Eric Laurent994a6932013-07-17 11:51:42 -07009176 ALOGV("%s: exit", __func__);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08009177 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009178
9179err_open:
Samyak Jainc37062f2019-04-25 18:41:06 +05309180 if (in->usecase == USECASE_AUDIO_RECORD) {
9181 pthread_mutex_lock(&adev->lock);
9182 adev->pcm_record_uc_state = 0;
9183 pthread_mutex_unlock(&adev->lock);
9184 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009185 free(in);
9186 *stream_in = NULL;
9187 return ret;
9188}
9189
9190static void adev_close_input_stream(struct audio_hw_device *dev,
9191 struct audio_stream_in *stream)
9192{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08009193 int ret;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07009194 struct stream_in *in = (struct stream_in *)stream;
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07009195 struct audio_device *adev = (struct audio_device *)dev;
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05309196
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05309197 ALOGD("%s: enter:stream_handle(%p)",__func__, in);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08009198
kunleiz70e57612018-12-28 17:50:23 +08009199 /* must deregister from sndmonitor first to prevent races
9200 * between the callback and close_stream
9201 */
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05309202 audio_extn_snd_mon_unregister_listener(stream);
9203
kunleiz70e57612018-12-28 17:50:23 +08009204 /* Disable echo reference if there are no active input, hfp call
9205 * and sound trigger while closing input stream
9206 */
Eric Laurent637e2d42018-11-15 12:24:31 -08009207 if (adev_get_active_input(adev) == NULL &&
kunleiz70e57612018-12-28 17:50:23 +08009208 !audio_extn_hfp_is_active(adev) &&
9209 !audio_extn_sound_trigger_check_ec_ref_enable())
kunleiz8c93ce62018-11-29 15:04:56 +08009210 platform_set_echo_reference(adev, false, AUDIO_DEVICE_NONE);
kunleiz70e57612018-12-28 17:50:23 +08009211 else
9212 audio_extn_sound_trigger_update_ec_ref_status(false);
Venkata Narendra Kumar Gutta1bbbf542014-09-04 19:11:25 +05309213
Pallavid7c7a272018-01-16 11:22:55 +05309214 if (in == NULL) {
9215 ALOGE("%s: audio_stream_in ptr is NULL", __func__);
9216 return;
9217 }
Weiyin Jiang2995f662019-04-17 14:25:12 +08009218 error_log_destroy(in->error_log);
9219 in->error_log = NULL;
9220
Pallavid7c7a272018-01-16 11:22:55 +05309221
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08009222 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05309223 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08009224 ret = voice_extn_compress_voip_close_input_stream(&stream->common);
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05309225 pthread_mutex_unlock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08009226 if (ret != 0)
9227 ALOGE("%s: Compress voip input cannot be closed, error:%d",
9228 __func__, ret);
9229 } else
9230 in_standby(&stream->common);
9231
Revathi Uddarajud2634032017-12-07 14:42:34 +05309232 pthread_mutex_lock(&adev->lock);
Samyak Jain15fda662018-12-18 16:40:52 +05309233 if (in->usecase == USECASE_AUDIO_RECORD) {
9234 adev->pcm_record_uc_state = 0;
9235 }
9236
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -07009237 if (audio_extn_ssr_get_stream() == in) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07009238 audio_extn_ssr_deinit();
9239 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009240
Garmond Leunge2433c32017-09-28 21:51:22 -07009241 if (audio_extn_ffv_get_stream() == in) {
9242 audio_extn_ffv_stream_deinit();
9243 }
9244
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05309245 if (audio_extn_compr_cap_enabled() &&
Mingming Yine62d7842013-10-25 16:26:03 -07009246 audio_extn_compr_cap_format_supported(in->config.format))
9247 audio_extn_compr_cap_deinit();
Dhanalakshmi Siddani74cf00b2016-12-02 13:55:57 +05309248
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05309249 if (audio_extn_cin_attached_usecase(in->usecase))
Manish Dewangan46e07982018-12-13 18:18:59 +05309250 audio_extn_cin_free_input_stream_resources(in);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07009251
Mingming Yinfd7607b2016-01-22 12:48:44 -08009252 if (in->is_st_session) {
9253 ALOGV("%s: sound trigger pcm stop lab", __func__);
9254 audio_extn_sound_trigger_stop_lab(in);
9255 }
Derek Chenf939fb72018-11-13 13:34:41 -08009256 streams_input_ctxt_t *in_ctxt = in_get_stream(adev, in->capture_handle);
9257 if (in_ctxt != NULL) {
9258 list_remove(&in_ctxt->list);
9259 free(in_ctxt);
9260 } else {
9261 ALOGW("%s, input stream already closed", __func__);
9262 }
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07009263 free(stream);
Revathi Uddarajud2634032017-12-07 14:42:34 +05309264 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009265 return;
9266}
9267
Aalique Grahame22e49102018-12-18 14:23:57 -08009268/* verifies input and output devices and their capabilities.
9269 *
9270 * This verification is required when enabling extended bit-depth or
9271 * sampling rates, as not all qcom products support it.
9272 *
9273 * Suitable for calling only on initialization such as adev_open().
9274 * It fills the audio_device use_case_table[] array.
9275 *
9276 * Has a side-effect that it needs to configure audio routing / devices
9277 * in order to power up the devices and read the device parameters.
9278 * It does not acquire any hw device lock. Should restore the devices
9279 * back to "normal state" upon completion.
9280 */
9281static int adev_verify_devices(struct audio_device *adev)
9282{
9283 /* enumeration is a bit difficult because one really wants to pull
9284 * the use_case, device id, etc from the hidden pcm_device_table[].
9285 * In this case there are the following use cases and device ids.
9286 *
9287 * [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = {0, 0},
9288 * [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {15, 15},
9289 * [USECASE_AUDIO_PLAYBACK_HIFI] = {1, 1},
9290 * [USECASE_AUDIO_PLAYBACK_OFFLOAD] = {9, 9},
9291 * [USECASE_AUDIO_RECORD] = {0, 0},
9292 * [USECASE_AUDIO_RECORD_LOW_LATENCY] = {15, 15},
9293 * [USECASE_VOICE_CALL] = {2, 2},
9294 *
9295 * USECASE_AUDIO_PLAYBACK_OFFLOAD, USECASE_AUDIO_PLAYBACK_HIFI omitted.
9296 * USECASE_VOICE_CALL omitted, but possible for either input or output.
9297 */
9298
9299 /* should be the usecases enabled in adev_open_input_stream() */
9300 static const int test_in_usecases[] = {
9301 USECASE_AUDIO_RECORD,
9302 USECASE_AUDIO_RECORD_LOW_LATENCY, /* does not appear to be used */
9303 };
9304 /* should be the usecases enabled in adev_open_output_stream()*/
9305 static const int test_out_usecases[] = {
9306 USECASE_AUDIO_PLAYBACK_DEEP_BUFFER,
9307 USECASE_AUDIO_PLAYBACK_LOW_LATENCY,
9308 };
9309 static const usecase_type_t usecase_type_by_dir[] = {
9310 PCM_PLAYBACK,
9311 PCM_CAPTURE,
9312 };
9313 static const unsigned flags_by_dir[] = {
9314 PCM_OUT,
9315 PCM_IN,
9316 };
9317
9318 size_t i;
9319 unsigned dir;
9320 const unsigned card_id = adev->snd_card;
9321
9322 for (dir = 0; dir < 2; ++dir) {
9323 const usecase_type_t usecase_type = usecase_type_by_dir[dir];
9324 const unsigned flags_dir = flags_by_dir[dir];
9325 const size_t testsize =
9326 dir ? ARRAY_SIZE(test_in_usecases) : ARRAY_SIZE(test_out_usecases);
9327 const int *testcases =
9328 dir ? test_in_usecases : test_out_usecases;
9329 const audio_devices_t audio_device =
9330 dir ? AUDIO_DEVICE_IN_BUILTIN_MIC : AUDIO_DEVICE_OUT_SPEAKER;
9331
9332 for (i = 0; i < testsize; ++i) {
9333 const audio_usecase_t audio_usecase = testcases[i];
9334 int device_id;
9335 struct pcm_params **pparams;
9336 struct stream_out out;
9337 struct stream_in in;
9338 struct audio_usecase uc_info;
9339 int retval;
9340
9341 pparams = &adev->use_case_table[audio_usecase];
9342 pcm_params_free(*pparams); /* can accept null input */
9343 *pparams = NULL;
9344
9345 /* find the device ID for the use case (signed, for error) */
9346 device_id = platform_get_pcm_device_id(audio_usecase, usecase_type);
9347 if (device_id < 0)
9348 continue;
9349
9350 /* prepare structures for device probing */
9351 memset(&uc_info, 0, sizeof(uc_info));
9352 uc_info.id = audio_usecase;
9353 uc_info.type = usecase_type;
9354 if (dir) {
Aalique Grahame22e49102018-12-18 14:23:57 -08009355 memset(&in, 0, sizeof(in));
9356 in.device = audio_device;
9357 in.source = AUDIO_SOURCE_VOICE_COMMUNICATION;
9358 uc_info.stream.in = &in;
Aalique Grahame22e49102018-12-18 14:23:57 -08009359 }
9360 memset(&out, 0, sizeof(out));
9361 out.devices = audio_device; /* only field needed in select_devices */
9362 uc_info.stream.out = &out;
9363 uc_info.devices = audio_device;
9364 uc_info.in_snd_device = SND_DEVICE_NONE;
9365 uc_info.out_snd_device = SND_DEVICE_NONE;
9366 list_add_tail(&adev->usecase_list, &uc_info.list);
9367
9368 /* select device - similar to start_(in/out)put_stream() */
9369 retval = select_devices(adev, audio_usecase);
9370 if (retval >= 0) {
9371 *pparams = pcm_params_get(card_id, device_id, flags_dir);
9372#if LOG_NDEBUG == 0
Aalique Grahame203bee02019-03-13 17:49:36 -07009373 char info[512]; /* for possible debug info */
Aalique Grahame22e49102018-12-18 14:23:57 -08009374 if (*pparams) {
9375 ALOGV("%s: (%s) card %d device %d", __func__,
9376 dir ? "input" : "output", card_id, device_id);
9377 pcm_params_to_string(*pparams, info, ARRAY_SIZE(info));
9378 } else {
9379 ALOGV("%s: cannot locate card %d device %d", __func__, card_id, device_id);
9380 }
9381#endif
9382 }
9383
9384 /* deselect device - similar to stop_(in/out)put_stream() */
9385 /* 1. Get and set stream specific mixer controls */
9386 retval = disable_audio_route(adev, &uc_info);
9387 /* 2. Disable the rx device */
9388 retval = disable_snd_device(adev,
9389 dir ? uc_info.in_snd_device : uc_info.out_snd_device);
9390 list_remove(&uc_info.list);
9391 }
9392 }
Aalique Grahame22e49102018-12-18 14:23:57 -08009393 return 0;
9394}
9395
Siddartha Shaik31b530e2017-05-19 15:26:33 +05309396int adev_create_audio_patch(struct audio_hw_device *dev,
9397 unsigned int num_sources,
9398 const struct audio_port_config *sources,
9399 unsigned int num_sinks,
9400 const struct audio_port_config *sinks,
9401 audio_patch_handle_t *handle)
9402{
Derek Chenf939fb72018-11-13 13:34:41 -08009403 int ret;
Siddartha Shaik31b530e2017-05-19 15:26:33 +05309404
Derek Chenf939fb72018-11-13 13:34:41 -08009405 ret = audio_extn_hw_loopback_create_audio_patch(dev,
9406 num_sources,
9407 sources,
9408 num_sinks,
9409 sinks,
9410 handle);
9411 ret |= audio_extn_auto_hal_create_audio_patch(dev,
9412 num_sources,
9413 sources,
9414 num_sinks,
9415 sinks,
9416 handle);
9417 return ret;
Siddartha Shaik31b530e2017-05-19 15:26:33 +05309418}
9419
9420int adev_release_audio_patch(struct audio_hw_device *dev,
9421 audio_patch_handle_t handle)
9422{
Derek Chenf939fb72018-11-13 13:34:41 -08009423 int ret;
9424
9425 ret = audio_extn_hw_loopback_release_audio_patch(dev, handle);
9426 ret |= audio_extn_auto_hal_release_audio_patch(dev, handle);
9427 return ret;
Siddartha Shaik31b530e2017-05-19 15:26:33 +05309428}
9429
9430int adev_get_audio_port(struct audio_hw_device *dev, struct audio_port *config)
9431{
Derek Chenf13dd492018-11-13 14:53:51 -08009432 int ret = 0;
9433
9434 ret = audio_extn_hw_loopback_get_audio_port(dev, config);
9435 ret |= audio_extn_auto_hal_get_audio_port(dev, config);
9436 return ret;
Siddartha Shaik31b530e2017-05-19 15:26:33 +05309437}
9438
9439int adev_set_audio_port_config(struct audio_hw_device *dev,
9440 const struct audio_port_config *config)
9441{
Derek Chenf13dd492018-11-13 14:53:51 -08009442 int ret = 0;
9443
9444 ret = audio_extn_hw_loopback_set_audio_port_config(dev, config);
9445 ret |= audio_extn_auto_hal_set_audio_port_config(dev, config);
9446 return ret;
Siddartha Shaik31b530e2017-05-19 15:26:33 +05309447}
9448
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07009449static int adev_dump(const audio_hw_device_t *device __unused,
9450 int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009451{
9452 return 0;
9453}
9454
9455static int adev_close(hw_device_t *device)
9456{
Aalique Grahame22e49102018-12-18 14:23:57 -08009457 size_t i;
Preetam Singh Ranawatacc64542019-06-24 15:11:28 +05309458 struct audio_device *adev_temp = (struct audio_device *)device;
Kiran Kandi910e1862013-10-29 13:29:42 -07009459
Preetam Singh Ranawatacc64542019-06-24 15:11:28 +05309460 if (!adev_temp)
Kiran Kandi910e1862013-10-29 13:29:42 -07009461 return 0;
9462
9463 pthread_mutex_lock(&adev_init_lock);
9464
9465 if ((--audio_device_ref_count) == 0) {
Sujin Panicker390724d2019-04-26 10:43:36 +05309466 if (audio_extn_spkr_prot_is_enabled())
9467 audio_extn_spkr_prot_deinit();
Jaideep Sharmaa2b49672019-09-10 20:37:03 +05309468 audio_extn_battery_properties_listener_deinit();
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05309469 audio_extn_snd_mon_unregister_listener(adev);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07009470 audio_extn_sound_trigger_deinit(adev);
Kiran Kandide144c82013-11-20 15:58:32 -08009471 audio_extn_listen_deinit(adev);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08009472 audio_extn_qdsp_deinit();
Aalique Grahame22e49102018-12-18 14:23:57 -08009473 audio_extn_extspk_deinit(adev->extspk);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05309474 audio_extn_utils_release_streams_cfg_lists(
9475 &adev->streams_output_cfg_list,
9476 &adev->streams_input_cfg_list);
Sidipotu Ashokd2f10ba2019-05-06 15:41:56 +05309477 if (audio_extn_qap_is_enabled())
9478 audio_extn_qap_deinit();
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05309479 if (audio_extn_qaf_is_enabled())
9480 audio_extn_qaf_deinit();
Kiran Kandi910e1862013-10-29 13:29:42 -07009481 audio_route_free(adev->audio_route);
Weiyin Jiangfa65d3e2019-03-05 23:39:45 +08009482 audio_extn_gef_deinit(adev);
Kiran Kandi910e1862013-10-29 13:29:42 -07009483 free(adev->snd_dev_ref_cnt);
9484 platform_deinit(adev->platform);
Aalique Grahame22e49102018-12-18 14:23:57 -08009485 for (i = 0; i < ARRAY_SIZE(adev->use_case_table); ++i) {
9486 pcm_params_free(adev->use_case_table[i]);
9487 }
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07009488 if (adev->adm_deinit)
9489 adev->adm_deinit(adev->adm_data);
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05309490 qahwi_deinit(device);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08009491 audio_extn_adsp_hdlr_deinit();
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05309492 audio_extn_snd_mon_deinit();
Siddartha Shaik44dd7702017-06-14 12:13:25 +05309493 audio_extn_hw_loopback_deinit(adev);
Garmond Leunge2433c32017-09-28 21:51:22 -07009494 audio_extn_ffv_deinit();
Satish Babu Patakokilac3c5d432017-07-04 22:48:59 +05309495 if (adev->device_cfg_params) {
9496 free(adev->device_cfg_params);
9497 adev->device_cfg_params = NULL;
9498 }
Derek Chend2530072014-11-24 12:39:14 -08009499 if(adev->ext_hw_plugin)
9500 audio_extn_ext_hw_plugin_deinit(adev->ext_hw_plugin);
Derek Chenae7b0342019-02-08 15:17:04 -08009501 audio_extn_auto_hal_deinit();
Kiran Kandi910e1862013-10-29 13:29:42 -07009502 free(device);
9503 adev = NULL;
9504 }
9505 pthread_mutex_unlock(&adev_init_lock);
Vatsal Buchac09ae062018-11-14 13:25:08 +05309506 enable_gcov();
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009507 return 0;
9508}
9509
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07009510/* This returns 1 if the input parameter looks at all plausible as a low latency period size,
9511 * or 0 otherwise. A return value of 1 doesn't mean the value is guaranteed to work,
9512 * just that it _might_ work.
9513 */
9514static int period_size_is_plausible_for_low_latency(int period_size)
9515{
9516 switch (period_size) {
9517 case 160:
Ravi Kumar Alamanda474de5a2015-06-25 20:08:01 -07009518 case 192:
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07009519 case 240:
9520 case 320:
9521 case 480:
9522 return 1;
9523 default:
9524 return 0;
9525 }
9526}
9527
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05309528static void adev_snd_mon_cb(void *cookie, struct str_parms *parms)
9529{
9530 bool is_snd_card_status = false;
9531 bool is_ext_device_status = false;
9532 char value[32];
9533 int card = -1;
9534 card_status_t status;
9535
9536 if (cookie != adev || !parms)
9537 return;
9538
9539 if (!parse_snd_card_status(parms, &card, &status)) {
9540 is_snd_card_status = true;
9541 } else if (0 < str_parms_get_str(parms, "ext_audio_device", value, sizeof(value))) {
9542 is_ext_device_status = true;
9543 } else {
9544 // not a valid event
9545 return;
9546 }
9547
9548 pthread_mutex_lock(&adev->lock);
9549 if (card == adev->snd_card || is_ext_device_status) {
9550 if (is_snd_card_status && adev->card_status != status) {
9551 adev->card_status = status;
9552 platform_snd_card_update(adev->platform, status);
9553 audio_extn_fm_set_parameters(adev, parms);
Derek Chend6f371d2019-03-01 13:45:58 -08009554 audio_extn_auto_hal_set_parameters(adev, parms);
Zhou Song4f43e352019-07-02 00:32:23 +08009555 if (status == CARD_STATUS_OFFLINE)
9556 audio_extn_sco_reset_configuration();
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05309557 } else if (is_ext_device_status) {
9558 platform_set_parameters(adev->platform, parms);
9559 }
9560 }
9561 pthread_mutex_unlock(&adev->lock);
9562 return;
9563}
9564
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05309565/* out and adev lock held */
9566static int check_a2dp_restore_l(struct audio_device *adev, struct stream_out *out, bool restore)
9567{
9568 struct audio_usecase *uc_info;
9569 float left_p;
9570 float right_p;
9571 audio_devices_t devices;
9572
9573 uc_info = get_usecase_from_list(adev, out->usecase);
9574 if (uc_info == NULL) {
9575 ALOGE("%s: Could not find the usecase (%d) in the list",
9576 __func__, out->usecase);
9577 return -EINVAL;
9578 }
9579
9580 ALOGD("%s: enter: usecase(%d: %s)", __func__,
9581 out->usecase, use_case_table[out->usecase]);
9582
9583 if (restore) {
9584 // restore A2DP device for active usecases and unmute if required
Zhou Songe5225132019-09-26 15:33:36 +08009585 if (out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05309586 ALOGD("%s: restoring A2dp and unmuting stream", __func__);
Zhou Songe5225132019-09-26 15:33:36 +08009587 if (uc_info->out_snd_device != SND_DEVICE_OUT_BT_A2DP)
9588 select_devices(adev, uc_info->id);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05309589 pthread_mutex_lock(&out->compr_mute_lock);
9590 if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
Manisha Agarwalc2188fb2019-05-27 10:00:29 +05309591 (out->a2dp_compress_mute) && (uc_info->out_snd_device == SND_DEVICE_OUT_BT_A2DP)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05309592 out->a2dp_compress_mute = false;
9593 out_set_compr_volume(&out->stream, out->volume_l, out->volume_r);
9594 }
9595 pthread_mutex_unlock(&out->compr_mute_lock);
9596 }
9597 } else {
Zhou Songc576a452019-09-09 14:17:40 +08009598 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
9599 // mute compress stream if suspended
9600 pthread_mutex_lock(&out->compr_mute_lock);
9601 if (!out->a2dp_compress_mute && !out->standby) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05309602 ALOGD("%s: selecting speaker and muting stream", __func__);
9603 devices = out->devices;
9604 out->devices = AUDIO_DEVICE_OUT_SPEAKER;
9605 left_p = out->volume_l;
9606 right_p = out->volume_r;
9607 if (out->offload_state == OFFLOAD_STATE_PLAYING)
9608 compress_pause(out->compr);
9609 out_set_compr_volume(&out->stream, (float)0, (float)0);
9610 out->a2dp_compress_mute = true;
9611 select_devices(adev, out->usecase);
9612 if (out->offload_state == OFFLOAD_STATE_PLAYING)
9613 compress_resume(out->compr);
9614 out->devices = devices;
9615 out->volume_l = left_p;
9616 out->volume_r = right_p;
9617 }
Zhou Songc576a452019-09-09 14:17:40 +08009618 pthread_mutex_unlock(&out->compr_mute_lock);
9619 } else {
9620 // tear down a2dp path for non offloaded streams
9621 if (audio_extn_a2dp_source_is_suspended())
9622 out_standby_l(&out->stream.common);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05309623 }
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05309624 }
9625 ALOGV("%s: exit", __func__);
9626 return 0;
9627}
9628
9629int check_a2dp_restore(struct audio_device *adev, struct stream_out *out, bool restore)
9630{
9631 int ret = 0;
9632
9633 lock_output_stream(out);
9634 pthread_mutex_lock(&adev->lock);
9635
9636 ret = check_a2dp_restore_l(adev, out, restore);
9637
9638 pthread_mutex_unlock(&adev->lock);
9639 pthread_mutex_unlock(&out->lock);
9640 return ret;
9641}
9642
Haynes Mathew George01156f92018-04-13 15:29:54 -07009643void adev_on_battery_status_changed(bool charging)
9644{
9645 pthread_mutex_lock(&adev->lock);
9646 ALOGI("%s: battery status changed to %scharging", __func__, charging ? "" : "not ");
9647 adev->is_charging = charging;
Zhou Songc1088ea2018-06-12 00:17:29 +08009648 audio_extn_sound_trigger_update_battery_status(charging);
Haynes Mathew George01156f92018-04-13 15:29:54 -07009649 pthread_mutex_unlock(&adev->lock);
9650}
9651
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009652static int adev_open(const hw_module_t *module, const char *name,
9653 hw_device_t **device)
9654{
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05309655 int ret;
Derek Chenf939fb72018-11-13 13:34:41 -08009656 char value[PROPERTY_VALUE_MAX] = {0};
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +05309657 char mixer_ctl_name[128] = {0};
9658 struct mixer_ctl *ctl = NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05309659
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08009660 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009661 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
9662
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07009663 pthread_mutex_lock(&adev_init_lock);
Kiran Kandi910e1862013-10-29 13:29:42 -07009664 if (audio_device_ref_count != 0){
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07009665 *device = &adev->device.common;
Kiran Kandi910e1862013-10-29 13:29:42 -07009666 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07009667 ALOGD("%s: returning existing instance of adev", __func__);
9668 ALOGD("%s: exit", __func__);
9669 pthread_mutex_unlock(&adev_init_lock);
9670 return 0;
9671 }
9672
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009673 adev = calloc(1, sizeof(struct audio_device));
9674
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07009675 if (!adev) {
9676 pthread_mutex_unlock(&adev_init_lock);
9677 return -ENOMEM;
9678 }
9679
Ravi Kumar Alamanda40703102014-04-24 10:34:41 -07009680 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
9681
Weiyin Jiange6ce6312019-01-28 18:28:22 +08009682 // register audio ext hidl at the earliest
9683 audio_extn_hidl_init();
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +05309684#ifdef DYNAMIC_LOG_ENABLED
9685 register_for_dynamic_logging("hal");
9686#endif
9687
Derek Chenf939fb72018-11-13 13:34:41 -08009688 /* default audio HAL major version */
9689 uint32_t maj_version = 2;
9690 if(property_get("vendor.audio.hal.maj.version", value, NULL))
9691 maj_version = atoi(value);
9692
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009693 adev->device.common.tag = HARDWARE_DEVICE_TAG;
Derek Chenf939fb72018-11-13 13:34:41 -08009694 adev->device.common.version = HARDWARE_DEVICE_API_VERSION(maj_version, 0);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009695 adev->device.common.module = (struct hw_module_t *)module;
9696 adev->device.common.close = adev_close;
9697
9698 adev->device.init_check = adev_init_check;
9699 adev->device.set_voice_volume = adev_set_voice_volume;
9700 adev->device.set_master_volume = adev_set_master_volume;
9701 adev->device.get_master_volume = adev_get_master_volume;
9702 adev->device.set_master_mute = adev_set_master_mute;
9703 adev->device.get_master_mute = adev_get_master_mute;
9704 adev->device.set_mode = adev_set_mode;
9705 adev->device.set_mic_mute = adev_set_mic_mute;
9706 adev->device.get_mic_mute = adev_get_mic_mute;
9707 adev->device.set_parameters = adev_set_parameters;
9708 adev->device.get_parameters = adev_get_parameters;
9709 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
9710 adev->device.open_output_stream = adev_open_output_stream;
9711 adev->device.close_output_stream = adev_close_output_stream;
9712 adev->device.open_input_stream = adev_open_input_stream;
9713 adev->device.close_input_stream = adev_close_input_stream;
Siddartha Shaik31b530e2017-05-19 15:26:33 +05309714 adev->device.create_audio_patch = adev_create_audio_patch;
9715 adev->device.release_audio_patch = adev_release_audio_patch;
9716 adev->device.get_audio_port = adev_get_audio_port;
9717 adev->device.set_audio_port_config = adev_set_audio_port_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009718 adev->device.dump = adev_dump;
Naresh Tannirudcb47c52018-06-25 16:23:32 +05309719 adev->device.get_microphones = adev_get_microphones;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009720
9721 /* Set the default route before the PCM stream is opened */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009722 adev->mode = AUDIO_MODE_NORMAL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08009723 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009724 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009725 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08009726 adev->acdb_settings = TTY_MODE_OFF;
vivek mehta344576a2016-04-12 18:56:03 -07009727 adev->allow_afe_proxy_usage = true;
Ashish Jain1b9b30c2017-05-18 20:57:40 +05309728 adev->bt_sco_on = false;
Eric Laurent07eeafd2013-10-06 12:52:49 -07009729 /* adev->cur_hdmi_channels = 0; by calloc() */
Eric Laurentb23d5282013-05-14 15:27:20 -07009730 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
vivek mehtaae1018c2019-05-09 12:19:57 -07009731 /* Init audio and voice feature */
9732 audio_extn_feature_init();
9733 voice_extn_feature_init();
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07009734 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08009735 list_init(&adev->usecase_list);
Derek Chenf939fb72018-11-13 13:34:41 -08009736 list_init(&adev->active_inputs_list);
9737 list_init(&adev->active_outputs_list);
Rahul Sharma99770982019-03-06 17:05:26 +05309738 list_init(&adev->audio_patch_record_list);
9739 adev->audio_patch_index = 0;
Krishnankutty Kolathappilly0b2de1c2014-02-14 14:45:49 -08009740 adev->cur_wfd_channels = 2;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07009741 adev->offload_usecases_state = 0;
Samyak Jain15fda662018-12-18 16:40:52 +05309742 adev->pcm_record_uc_state = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05309743 adev->is_channel_status_set = false;
Sudheer Papothifa9d2282015-09-17 01:53:25 +05309744 adev->perf_lock_opts[0] = 0x101;
9745 adev->perf_lock_opts[1] = 0x20E;
9746 adev->perf_lock_opts_size = 2;
Xiaojun Sang785b5da2017-08-03 15:52:29 +08009747 adev->dsp_bit_width_enforce_mode = 0;
Aalique Grahame552b0832019-03-11 10:16:38 -07009748 adev->enable_hfp = false;
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +05309749 adev->use_old_pspd_mix_ctrl = false;
Aniket Kumar Lata60586a92019-05-22 22:18:55 -07009750 adev->adm_routing_changed = false;
Naresh Tanniru4c630392014-05-12 01:05:52 +05309751
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009752 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07009753 adev->platform = platform_init(adev);
9754 if (!adev->platform) {
Ben Romberger2684ad72018-06-04 12:41:39 -07009755 pthread_mutex_destroy(&adev->lock);
Eric Laurentb23d5282013-05-14 15:27:20 -07009756 free(adev->snd_dev_ref_cnt);
9757 free(adev);
Ben Romberger2684ad72018-06-04 12:41:39 -07009758 adev = NULL;
Eric Laurentb23d5282013-05-14 15:27:20 -07009759 ALOGE("%s: Failed to init platform data, aborting.", __func__);
9760 *device = NULL;
Apoorv Raghuvanshi6e57d7e2013-12-16 16:02:45 -08009761 pthread_mutex_unlock(&adev_init_lock);
Eric Laurentb23d5282013-05-14 15:27:20 -07009762 return -EINVAL;
9763 }
Eric Laurentc4aef752013-09-12 17:45:53 -07009764
Aalique Grahame22e49102018-12-18 14:23:57 -08009765 adev->extspk = audio_extn_extspk_init(adev);
Sidipotu Ashokd2f10ba2019-05-06 15:41:56 +05309766 if (audio_extn_qap_is_enabled()) {
9767 ret = audio_extn_qap_init(adev);
9768 if (ret < 0) {
9769 pthread_mutex_destroy(&adev->lock);
9770 free(adev);
9771 adev = NULL;
9772 ALOGE("%s: Failed to init platform data, aborting.", __func__);
9773 *device = NULL;
9774 pthread_mutex_unlock(&adev_init_lock);
9775 return ret;
9776 }
9777 adev->device.open_output_stream = audio_extn_qap_open_output_stream;
9778 adev->device.close_output_stream = audio_extn_qap_close_output_stream;
9779 }
Aalique Grahame22e49102018-12-18 14:23:57 -08009780
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05309781 if (audio_extn_qaf_is_enabled()) {
9782 ret = audio_extn_qaf_init(adev);
9783 if (ret < 0) {
Ben Romberger2684ad72018-06-04 12:41:39 -07009784 pthread_mutex_destroy(&adev->lock);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05309785 free(adev);
Ben Romberger2684ad72018-06-04 12:41:39 -07009786 adev = NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05309787 ALOGE("%s: Failed to init platform data, aborting.", __func__);
9788 *device = NULL;
9789 pthread_mutex_unlock(&adev_init_lock);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05309790 return ret;
9791 }
9792
9793 adev->device.open_output_stream = audio_extn_qaf_open_output_stream;
9794 adev->device.close_output_stream = audio_extn_qaf_close_output_stream;
9795 }
9796
Derek Chenae7b0342019-02-08 15:17:04 -08009797 audio_extn_auto_hal_init(adev);
Derek Chend2530072014-11-24 12:39:14 -08009798 adev->ext_hw_plugin = audio_extn_ext_hw_plugin_init(adev);
9799
Eric Laurentc4aef752013-09-12 17:45:53 -07009800 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
9801 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
9802 if (adev->visualizer_lib == NULL) {
9803 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
9804 } else {
9805 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
9806 adev->visualizer_start_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08009807 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07009808 "visualizer_hal_start_output");
9809 adev->visualizer_stop_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08009810 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07009811 "visualizer_hal_stop_output");
9812 }
9813 }
Dhanalakshmi Siddani21be3ac2016-12-29 14:31:08 +05309814 audio_extn_init(adev);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08009815 voice_extn_init(adev);
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08009816 audio_extn_listen_init(adev, adev->snd_card);
Weiyin Jiangaa80acd2016-09-21 16:42:11 +08009817 audio_extn_gef_init(adev);
Siddartha Shaik44dd7702017-06-14 12:13:25 +05309818 audio_extn_hw_loopback_init(adev);
Garmond Leunge2433c32017-09-28 21:51:22 -07009819 audio_extn_ffv_init(adev);
Eric Laurentc4aef752013-09-12 17:45:53 -07009820
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08009821 if (access(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, R_OK) == 0) {
9822 adev->offload_effects_lib = dlopen(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, RTLD_NOW);
9823 if (adev->offload_effects_lib == NULL) {
9824 ALOGE("%s: DLOPEN failed for %s", __func__,
9825 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
9826 } else {
9827 ALOGV("%s: DLOPEN successful for %s", __func__,
9828 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
9829 adev->offload_effects_start_output =
Ashish Jain5106d362016-05-11 19:23:33 +05309830 (int (*)(audio_io_handle_t, int, struct mixer *))dlsym(adev->offload_effects_lib,
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08009831 "offload_effects_bundle_hal_start_output");
9832 adev->offload_effects_stop_output =
9833 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
9834 "offload_effects_bundle_hal_stop_output");
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08009835 adev->offload_effects_set_hpx_state =
9836 (int (*)(bool))dlsym(adev->offload_effects_lib,
9837 "offload_effects_bundle_set_hpx_state");
Dhananjay Kumard68883d2015-09-04 13:39:26 +05309838 adev->offload_effects_get_parameters =
9839 (void (*)(struct str_parms *, struct str_parms *))
9840 dlsym(adev->offload_effects_lib,
9841 "offload_effects_bundle_get_parameters");
9842 adev->offload_effects_set_parameters =
9843 (void (*)(struct str_parms *))dlsym(adev->offload_effects_lib,
9844 "offload_effects_bundle_set_parameters");
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08009845 }
9846 }
9847
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07009848 if (access(ADM_LIBRARY_PATH, R_OK) == 0) {
9849 adev->adm_lib = dlopen(ADM_LIBRARY_PATH, RTLD_NOW);
9850 if (adev->adm_lib == NULL) {
9851 ALOGE("%s: DLOPEN failed for %s", __func__, ADM_LIBRARY_PATH);
9852 } else {
9853 ALOGV("%s: DLOPEN successful for %s", __func__, ADM_LIBRARY_PATH);
9854 adev->adm_init = (adm_init_t)
9855 dlsym(adev->adm_lib, "adm_init");
9856 adev->adm_deinit = (adm_deinit_t)
9857 dlsym(adev->adm_lib, "adm_deinit");
9858 adev->adm_register_input_stream = (adm_register_input_stream_t)
9859 dlsym(adev->adm_lib, "adm_register_input_stream");
9860 adev->adm_register_output_stream = (adm_register_output_stream_t)
9861 dlsym(adev->adm_lib, "adm_register_output_stream");
9862 adev->adm_deregister_stream = (adm_deregister_stream_t)
9863 dlsym(adev->adm_lib, "adm_deregister_stream");
9864 adev->adm_request_focus = (adm_request_focus_t)
9865 dlsym(adev->adm_lib, "adm_request_focus");
9866 adev->adm_abandon_focus = (adm_abandon_focus_t)
9867 dlsym(adev->adm_lib, "adm_abandon_focus");
Haynes Mathew George5beddd42016-06-27 18:33:40 -07009868 adev->adm_set_config = (adm_set_config_t)
9869 dlsym(adev->adm_lib, "adm_set_config");
9870 adev->adm_request_focus_v2 = (adm_request_focus_v2_t)
9871 dlsym(adev->adm_lib, "adm_request_focus_v2");
9872 adev->adm_is_noirq_avail = (adm_is_noirq_avail_t)
9873 dlsym(adev->adm_lib, "adm_is_noirq_avail");
9874 adev->adm_on_routing_change = (adm_on_routing_change_t)
9875 dlsym(adev->adm_lib, "adm_on_routing_change");
Aniket Kumar Lata60586a92019-05-22 22:18:55 -07009876 adev->adm_request_focus_v2_1 = (adm_request_focus_v2_1_t)
9877 dlsym(adev->adm_lib, "adm_request_focus_v2_1");
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07009878 }
9879 }
9880
Aalique Grahame22e49102018-12-18 14:23:57 -08009881 adev->enable_voicerx = false;
Mingming Yin514a8bc2014-07-29 15:22:21 -07009882 adev->bt_wb_speech_enabled = false;
Zhou Song12c29502019-03-16 10:37:18 +08009883 adev->swb_speech_mode = SPEECH_MODE_INVALID;
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08009884 //initialize this to false for now,
9885 //this will be set to true through set param
9886 adev->vr_audio_mode_enabled = false;
Mingming Yin514a8bc2014-07-29 15:22:21 -07009887
Pradnya Chaphekar8a9dcd82014-09-09 09:49:10 -07009888 audio_extn_ds2_enable(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009889 *device = &adev->device.common;
Aalique Grahame22e49102018-12-18 14:23:57 -08009890
9891 if (k_enable_extended_precision)
9892 adev_verify_devices(adev);
9893
Xiaojun Sang785b5da2017-08-03 15:52:29 +08009894 adev->dsp_bit_width_enforce_mode =
9895 adev_init_dsp_bit_width_enforce_mode(adev->mixer);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009896
Dhananjay Kumard6d32152016-10-13 16:11:03 +05309897 audio_extn_utils_update_streams_cfg_lists(adev->platform, adev->mixer,
9898 &adev->streams_output_cfg_list,
9899 &adev->streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07009900
Kiran Kandi910e1862013-10-29 13:29:42 -07009901 audio_device_ref_count++;
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07009902
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07009903 int trial;
Aalique Grahame22e49102018-12-18 14:23:57 -08009904 if ((property_get("vendor.audio_hal.period_size", value, NULL) > 0) ||
9905 (property_get("audio_hal.period_size", value, NULL) > 0)) {
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07009906 trial = atoi(value);
9907 if (period_size_is_plausible_for_low_latency(trial)) {
9908 pcm_config_low_latency.period_size = trial;
9909 pcm_config_low_latency.start_threshold = trial / 4;
9910 pcm_config_low_latency.avail_min = trial / 4;
9911 configured_low_latency_capture_period_size = trial;
9912 }
9913 }
Aalique Grahame22e49102018-12-18 14:23:57 -08009914 if ((property_get("vendor.audio_hal.in_period_size", value, NULL) > 0) ||
9915 (property_get("audio_hal.in_period_size", value, NULL) > 0)) {
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07009916 trial = atoi(value);
9917 if (period_size_is_plausible_for_low_latency(trial)) {
9918 configured_low_latency_capture_period_size = trial;
9919 }
9920 }
9921
Vignesh Kulothungan7d374312018-02-21 17:12:00 -08009922 adev->mic_break_enabled = property_get_bool("vendor.audio.mic_break", false);
9923
Eric Laurent4b084132018-10-19 17:33:43 -07009924 adev->camera_orientation = CAMERA_DEFAULT;
9925
Aalique Grahame22e49102018-12-18 14:23:57 -08009926 if ((property_get("vendor.audio_hal.period_multiplier",value,NULL) > 0) ||
9927 (property_get("audio_hal.period_multiplier",value,NULL) > 0)) {
Haynes Mathew George5beddd42016-06-27 18:33:40 -07009928 af_period_multiplier = atoi(value);
9929 if (af_period_multiplier < 0)
9930 af_period_multiplier = 2;
9931 else if (af_period_multiplier > 4)
9932 af_period_multiplier = 4;
9933
9934 ALOGV("new period_multiplier = %d", af_period_multiplier);
9935 }
9936
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08009937 audio_extn_qdsp_init(adev->platform);
Aalique Grahame22e49102018-12-18 14:23:57 -08009938
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07009939 adev->multi_offload_enable = property_get_bool("vendor.audio.offload.multiple.enabled", false);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07009940 pthread_mutex_unlock(&adev_init_lock);
9941
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07009942 if (adev->adm_init)
9943 adev->adm_data = adev->adm_init();
9944
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05309945 qahwi_init(*device);
Sudheer Papothifa9d2282015-09-17 01:53:25 +05309946 audio_extn_perf_lock_init();
Ben Rombergerd771a7c2017-02-22 18:05:17 -08009947 audio_extn_adsp_hdlr_init(adev->mixer);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05309948
9949 audio_extn_snd_mon_init();
9950 pthread_mutex_lock(&adev->lock);
9951 audio_extn_snd_mon_register_listener(adev, adev_snd_mon_cb);
9952 adev->card_status = CARD_STATUS_ONLINE;
Haynes Mathew George01156f92018-04-13 15:29:54 -07009953 audio_extn_battery_properties_listener_init(adev_on_battery_status_changed);
9954 /*
9955 * if the battery state callback happens before charging can be queried,
9956 * it will be guarded with the adev->lock held in the cb function and so
9957 * the callback value will reflect the latest state
9958 */
9959 adev->is_charging = audio_extn_battery_properties_is_charging();
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05309960 audio_extn_sound_trigger_init(adev); /* dependent on snd_mon_init() */
Zhou Songc1088ea2018-06-12 00:17:29 +08009961 audio_extn_sound_trigger_update_battery_status(adev->is_charging);
justinweng20fb6d82019-02-21 18:49:00 -07009962 audio_extn_audiozoom_init();
Zhou Songc1088ea2018-06-12 00:17:29 +08009963 pthread_mutex_unlock(&adev->lock);
Satish Babu Patakokilac3c5d432017-07-04 22:48:59 +05309964 /* Allocate memory for Device config params */
9965 adev->device_cfg_params = (struct audio_device_config_param*)
9966 calloc(platform_get_max_codec_backend(),
9967 sizeof(struct audio_device_config_param));
9968 if (adev->device_cfg_params == NULL)
9969 ALOGE("%s: Memory allocation failed for Device config params", __func__);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05309970
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +05309971 /*
9972 * Check if new PSPD matrix mixer control is supported. If not
9973 * supported, then set flag so that old mixer ctrl is sent while
9974 * sending pspd coefficients on older kernel version. Query mixer
9975 * control for default pcm id and channel value one.
9976 */
9977 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
9978 "AudStr %d ChMixer Weight Ch %d", 0, 1);
9979
9980 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
9981 if (!ctl) {
9982 ALOGE("%s: ERROR. Could not get ctl for mixer cmd - %s",
9983 __func__, mixer_ctl_name);
9984 adev->use_old_pspd_mix_ctrl = true;
9985 }
9986
Eric Laurent994a6932013-07-17 11:51:42 -07009987 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009988 return 0;
9989}
9990
9991static struct hw_module_methods_t hal_module_methods = {
9992 .open = adev_open,
9993};
9994
9995struct audio_module HAL_MODULE_INFO_SYM = {
9996 .common = {
9997 .tag = HARDWARE_MODULE_TAG,
9998 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
9999 .hal_api_version = HARDWARE_HAL_API_VERSION,
10000 .id = AUDIO_HARDWARE_MODULE_ID,
10001 .name = "QCOM Audio HAL",
Duy Truongfae19622013-11-24 02:17:54 -080010002 .author = "The Linux Foundation",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080010003 .methods = &hal_module_methods,
10004 },
10005};