blob: a5df9517d265d83ab22b748dbe6a815ff4044936 [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",
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700360 [USECASE_VOICE_CALL] = "voice-call",
Mingming Yin3ee55c62014-08-04 14:23:35 -0700361
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700362 [USECASE_VOICE2_CALL] = "voice2-call",
363 [USECASE_VOLTE_CALL] = "volte-call",
364 [USECASE_QCHAT_CALL] = "qchat-call",
Vicky Sehrawat7e4fc152014-02-12 17:58:59 -0800365 [USECASE_VOWLAN_CALL] = "vowlan-call",
Vidyakumar Athota0e109352015-02-12 17:38:22 -0800366 [USECASE_VOICEMMODE1_CALL] = "voicemmode1-call",
367 [USECASE_VOICEMMODE2_CALL] = "voicemmode2-call",
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800368 [USECASE_COMPRESS_VOIP_CALL] = "compress-voip-call",
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700369 [USECASE_INCALL_REC_UPLINK] = "incall-rec-uplink",
370 [USECASE_INCALL_REC_DOWNLINK] = "incall-rec-downlink",
371 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK] = "incall-rec-uplink-and-downlink",
Helen Zenge56b4852013-12-03 16:54:40 -0800372 [USECASE_INCALL_REC_UPLINK_COMPRESS] = "incall-rec-uplink-compress",
373 [USECASE_INCALL_REC_DOWNLINK_COMPRESS] = "incall-rec-downlink-compress",
374 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK_COMPRESS] = "incall-rec-uplink-and-downlink-compress",
375
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -0700376 [USECASE_INCALL_MUSIC_UPLINK] = "incall_music_uplink",
377 [USECASE_INCALL_MUSIC_UPLINK2] = "incall_music_uplink2",
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700378 [USECASE_AUDIO_SPKR_CALIB_RX] = "spkr-rx-calib",
379 [USECASE_AUDIO_SPKR_CALIB_TX] = "spkr-vi-record",
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -0700380
381 [USECASE_AUDIO_PLAYBACK_AFE_PROXY] = "afe-proxy-playback",
382 [USECASE_AUDIO_RECORD_AFE_PROXY] = "afe-proxy-record",
Md Mansoor Ahmeddb1b4f92018-01-25 18:56:31 +0530383 [USECASE_AUDIO_PLAYBACK_SILENCE] = "silence-playback",
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700384
Siddartha Shaik31b530e2017-05-19 15:26:33 +0530385 /* Transcode loopback cases */
Surendar Karka93cd25a2018-08-28 14:21:37 +0530386 [USECASE_AUDIO_TRANSCODE_LOOPBACK_RX] = "audio-transcode-loopback-rx",
387 [USECASE_AUDIO_TRANSCODE_LOOPBACK_TX] = "audio-transcode-loopback-tx",
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700388
389 [USECASE_AUDIO_PLAYBACK_VOIP] = "audio-playback-voip",
390 [USECASE_AUDIO_RECORD_VOIP] = "audio-record-voip",
Varun Balaraje49253e2017-07-06 19:48:56 +0530391 /* For Interactive Audio Streams */
392 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM1] = "audio-interactive-stream1",
393 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM2] = "audio-interactive-stream2",
394 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM3] = "audio-interactive-stream3",
395 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM4] = "audio-interactive-stream4",
396 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM5] = "audio-interactive-stream5",
397 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM6] = "audio-interactive-stream6",
398 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM7] = "audio-interactive-stream7",
399 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM8] = "audio-interactive-stream8",
Garmond Leunge2433c32017-09-28 21:51:22 -0700400
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800401 [USECASE_AUDIO_EC_REF_LOOPBACK] = "ec-ref-audio-capture",
402
403 [USECASE_AUDIO_A2DP_ABR_FEEDBACK] = "a2dp-abr-feedback"
Eric Laurentb23d5282013-05-14 15:27:20 -0700404};
405
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700406static const audio_usecase_t offload_usecases[] = {
407 USECASE_AUDIO_PLAYBACK_OFFLOAD,
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700408 USECASE_AUDIO_PLAYBACK_OFFLOAD2,
409 USECASE_AUDIO_PLAYBACK_OFFLOAD3,
410 USECASE_AUDIO_PLAYBACK_OFFLOAD4,
411 USECASE_AUDIO_PLAYBACK_OFFLOAD5,
412 USECASE_AUDIO_PLAYBACK_OFFLOAD6,
413 USECASE_AUDIO_PLAYBACK_OFFLOAD7,
414 USECASE_AUDIO_PLAYBACK_OFFLOAD8,
415 USECASE_AUDIO_PLAYBACK_OFFLOAD9,
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700416};
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800417
Varun Balaraje49253e2017-07-06 19:48:56 +0530418static const audio_usecase_t interactive_usecases[] = {
419 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM1,
420 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM2,
421 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM3,
422 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM4,
423 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM5,
424 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM6,
425 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM7,
426 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM8,
427};
428
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800429#define STRING_TO_ENUM(string) { #string, string }
430
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800431struct string_to_enum {
432 const char *name;
433 uint32_t value;
434};
435
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700436static const struct string_to_enum channels_name_to_enum_table[] = {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800437 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
Mingming Yin3a941d42016-02-17 18:08:05 -0800438 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_2POINT1),
439 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_QUAD),
440 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_SURROUND),
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -0700441 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_PENTA),
Mingming Yin3a941d42016-02-17 18:08:05 -0800442 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
443 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_6POINT1),
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800444 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700445 STRING_TO_ENUM(AUDIO_CHANNEL_IN_MONO),
446 STRING_TO_ENUM(AUDIO_CHANNEL_IN_STEREO),
447 STRING_TO_ENUM(AUDIO_CHANNEL_IN_FRONT_BACK),
448 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_1),
449 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_2),
450 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_3),
451 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_4),
452 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_5),
453 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_6),
454 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_7),
455 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_8),
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800456};
457
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700458static const struct string_to_enum formats_name_to_enum_table[] = {
459 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
460 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
461 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Pradnya Chaphekar8a9dcd82014-09-09 09:49:10 -0700462 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
463 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
464 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Ben Romberger1aaaf862017-04-06 17:49:46 -0700465 STRING_TO_ENUM(AUDIO_FORMAT_DOLBY_TRUEHD),
Mingming Yin3a941d42016-02-17 18:08:05 -0800466 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
467 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
Naresh Tanniru928f0862017-04-07 16:44:23 -0700468 STRING_TO_ENUM(AUDIO_FORMAT_IEC61937)
Mingming Yin3a941d42016-02-17 18:08:05 -0800469};
470
471//list of all supported sample rates by HDMI specification.
472static const int out_hdmi_sample_rates[] = {
473 32000, 44100, 48000, 88200, 96000, 176400, 192000,
474};
475
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700476static const struct string_to_enum out_sample_rates_name_to_enum_table[] = {
Mingming Yin3a941d42016-02-17 18:08:05 -0800477 STRING_TO_ENUM(32000),
478 STRING_TO_ENUM(44100),
479 STRING_TO_ENUM(48000),
480 STRING_TO_ENUM(88200),
481 STRING_TO_ENUM(96000),
482 STRING_TO_ENUM(176400),
483 STRING_TO_ENUM(192000),
Pradnya Chaphekar8a9dcd82014-09-09 09:49:10 -0700484};
485
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700486static struct audio_device *adev = NULL;
Ben Romberger02ab1192018-05-24 12:10:08 -0700487static pthread_mutex_t adev_init_lock = PTHREAD_MUTEX_INITIALIZER;
Kiran Kandi910e1862013-10-29 13:29:42 -0700488static unsigned int audio_device_ref_count;
vivek mehtab72d08d2016-04-29 03:16:47 -0700489//cache last MBDRC cal step level
490static int last_known_cal_step = -1 ;
Kiran Kandi910e1862013-10-29 13:29:42 -0700491
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +0530492static int check_a2dp_restore_l(struct audio_device *adev, struct stream_out *out, bool restore);
493static int out_set_compr_volume(struct audio_stream_out *stream, float left, float right);
Arun Mirpuri5d170872019-03-26 13:21:31 -0700494static int out_set_mmap_volume(struct audio_stream_out *stream, float left, float right);
Zhou Song2b8f28f2017-09-11 10:51:38 +0800495static int out_set_voip_volume(struct audio_stream_out *stream, float left, float right);
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +0530496static int out_set_pcm_volume(struct audio_stream_out *stream, float left, float right);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +0530497
Vatsal Buchac09ae062018-11-14 13:25:08 +0530498#ifdef AUDIO_FEATURE_ENABLED_GCOV
499extern void __gcov_flush();
500static void enable_gcov()
501{
502 __gcov_flush();
503}
504#else
505static void enable_gcov()
506{
507}
508#endif
509
justinweng20fb6d82019-02-21 18:49:00 -0700510static int in_set_microphone_direction(const struct audio_stream_in *stream,
511 audio_microphone_direction_t dir);
512static int in_set_microphone_field_dimension(const struct audio_stream_in *stream, float zoom);
513
514
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700515static bool may_use_noirq_mode(struct audio_device *adev, audio_usecase_t uc_id,
516 int flags __unused)
517{
518 int dir = 0;
519 switch (uc_id) {
520 case USECASE_AUDIO_RECORD_LOW_LATENCY:
521 dir = 1;
522 case USECASE_AUDIO_PLAYBACK_ULL:
523 break;
524 default:
525 return false;
526 }
527
528 int dev_id = platform_get_pcm_device_id(uc_id, dir == 0 ?
529 PCM_PLAYBACK : PCM_CAPTURE);
530 if (adev->adm_is_noirq_avail)
531 return adev->adm_is_noirq_avail(adev->adm_data,
532 adev->snd_card, dev_id, dir);
533 return false;
534}
535
536static void register_out_stream(struct stream_out *out)
537{
538 struct audio_device *adev = out->dev;
539 if (is_offload_usecase(out->usecase) ||
540 !adev->adm_register_output_stream)
541 return;
542
543 // register stream first for backward compatibility
544 adev->adm_register_output_stream(adev->adm_data,
545 out->handle,
546 out->flags);
547
548 if (!adev->adm_set_config)
549 return;
550
551 if (out->realtime)
552 adev->adm_set_config(adev->adm_data,
553 out->handle,
554 out->pcm, &out->config);
555}
556
557static void register_in_stream(struct stream_in *in)
558{
559 struct audio_device *adev = in->dev;
560 if (!adev->adm_register_input_stream)
561 return;
562
563 adev->adm_register_input_stream(adev->adm_data,
564 in->capture_handle,
565 in->flags);
566
567 if (!adev->adm_set_config)
568 return;
569
570 if (in->realtime)
571 adev->adm_set_config(adev->adm_data,
572 in->capture_handle,
573 in->pcm,
574 &in->config);
575}
576
577static void request_out_focus(struct stream_out *out, long ns)
578{
579 struct audio_device *adev = out->dev;
580
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700581 if (adev->adm_request_focus_v2)
582 adev->adm_request_focus_v2(adev->adm_data, out->handle, ns);
583 else if (adev->adm_request_focus)
584 adev->adm_request_focus(adev->adm_data, out->handle);
585}
586
Aniket Kumar Lata60586a92019-05-22 22:18:55 -0700587static int request_in_focus(struct stream_in *in, long ns)
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700588{
589 struct audio_device *adev = in->dev;
Aniket Kumar Lata60586a92019-05-22 22:18:55 -0700590 int ret = 0;
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700591
Aniket Kumar Lata60586a92019-05-22 22:18:55 -0700592 if (adev->adm_request_focus_v2_1)
593 ret = adev->adm_request_focus_v2_1(adev->adm_data, in->capture_handle, ns);
594 else if (adev->adm_request_focus_v2)
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700595 adev->adm_request_focus_v2(adev->adm_data, in->capture_handle, ns);
596 else if (adev->adm_request_focus)
597 adev->adm_request_focus(adev->adm_data, in->capture_handle);
Aniket Kumar Lata60586a92019-05-22 22:18:55 -0700598
599 return ret;
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700600}
601
602static void release_out_focus(struct stream_out *out)
603{
604 struct audio_device *adev = out->dev;
605
606 if (adev->adm_abandon_focus)
607 adev->adm_abandon_focus(adev->adm_data, out->handle);
608}
609
610static void release_in_focus(struct stream_in *in)
611{
612 struct audio_device *adev = in->dev;
613 if (adev->adm_abandon_focus)
614 adev->adm_abandon_focus(adev->adm_data, in->capture_handle);
615}
616
Dhananjay Kumare6293dd2017-05-25 17:25:30 +0530617static int parse_snd_card_status(struct str_parms *parms, int *card,
618 card_status_t *status)
619{
620 char value[32]={0};
621 char state[32]={0};
622
623 int ret = str_parms_get_str(parms, "SND_CARD_STATUS", value, sizeof(value));
624 if (ret < 0)
625 return -1;
626
627 // sscanf should be okay as value is of max length 32.
628 // same as sizeof state.
629 if (sscanf(value, "%d,%s", card, state) < 2)
630 return -1;
631
632 *status = !strcmp(state, "ONLINE") ? CARD_STATUS_ONLINE :
633 CARD_STATUS_OFFLINE;
634 return 0;
635}
636
Haynes Mathew Georgeb0f5dc32017-10-06 18:35:12 -0700637static inline void adjust_frames_for_device_delay(struct stream_out *out,
638 uint32_t *dsp_frames) {
639 // Adjustment accounts for A2dp encoder latency with offload usecases
640 // Note: Encoder latency is returned in ms.
641 if (AUDIO_DEVICE_OUT_ALL_A2DP & out->devices) {
642 unsigned long offset =
643 (audio_extn_a2dp_get_encoder_latency() * out->sample_rate / 1000);
644 *dsp_frames = (*dsp_frames > offset) ? (*dsp_frames - offset) : 0;
645 }
646}
647
vivek mehtaa76401a2015-04-24 14:12:15 -0700648__attribute__ ((visibility ("default")))
649bool audio_hw_send_gain_dep_calibration(int level) {
650 bool ret_val = false;
vivek mehtab72d08d2016-04-29 03:16:47 -0700651 ALOGV("%s: called ...", __func__);
vivek mehtaa76401a2015-04-24 14:12:15 -0700652
653 pthread_mutex_lock(&adev_init_lock);
654
655 if (adev != NULL && adev->platform != NULL) {
656 pthread_mutex_lock(&adev->lock);
657 ret_val = platform_send_gain_dep_cal(adev->platform, level);
vivek mehtab72d08d2016-04-29 03:16:47 -0700658
Preetam Singh Ranawatf4ae0222017-05-31 17:07:28 +0530659 // cache level info for any of the use case which
660 // was not started.
661 last_known_cal_step = level;;
vivek mehtab72d08d2016-04-29 03:16:47 -0700662
vivek mehtaa76401a2015-04-24 14:12:15 -0700663 pthread_mutex_unlock(&adev->lock);
664 } else {
665 ALOGE("%s: %s is NULL", __func__, adev == NULL ? "adev" : "adev->platform");
666 }
667
668 pthread_mutex_unlock(&adev_init_lock);
669
670 return ret_val;
671}
672
Ashish Jain5106d362016-05-11 19:23:33 +0530673static int check_and_set_gapless_mode(struct audio_device *adev, bool enable_gapless)
674{
Krishnankutty Kolathappilly6d8788b2014-01-09 12:45:31 -0800675 bool gapless_enabled = false;
676 const char *mixer_ctl_name = "Compress Gapless Playback";
677 struct mixer_ctl *ctl;
678
679 ALOGV("%s:", __func__);
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700680 gapless_enabled = property_get_bool("vendor.audio.offload.gapless.enabled", false);
Ashish Jain5106d362016-05-11 19:23:33 +0530681
682 /*Disable gapless if its AV playback*/
683 gapless_enabled = gapless_enabled && enable_gapless;
Krishnankutty Kolathappilly6d8788b2014-01-09 12:45:31 -0800684
685 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
686 if (!ctl) {
687 ALOGE("%s: Could not get ctl for mixer cmd - %s",
688 __func__, mixer_ctl_name);
689 return -EINVAL;
690 }
691
692 if (mixer_ctl_set_value(ctl, 0, gapless_enabled) < 0) {
693 ALOGE("%s: Could not set gapless mode %d",
694 __func__, gapless_enabled);
695 return -EINVAL;
696 }
697 return 0;
698}
Haynes Mathew George5191a852013-09-11 14:19:36 -0700699
Aniket Kumar Lataf56b6402016-10-27 12:03:18 -0700700__attribute__ ((visibility ("default")))
701int audio_hw_get_gain_level_mapping(struct amp_db_and_gain_table *mapping_tbl,
702 int table_size) {
703 int ret_val = 0;
704 ALOGV("%s: enter ... ", __func__);
705
706 pthread_mutex_lock(&adev_init_lock);
707 if (adev == NULL) {
708 ALOGW("%s: adev is NULL .... ", __func__);
709 goto done;
710 }
711
712 pthread_mutex_lock(&adev->lock);
713 ret_val = platform_get_gain_level_mapping(mapping_tbl, table_size);
714 pthread_mutex_unlock(&adev->lock);
715done:
716 pthread_mutex_unlock(&adev_init_lock);
717 ALOGV("%s: exit ... ", __func__);
718 return ret_val;
719}
720
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800721bool audio_hw_send_qdsp_parameter(int stream_type, float vol, bool active)
Aalique Grahame22e49102018-12-18 14:23:57 -0800722{
723 bool ret = false;
724 ALOGV("%s: enter ...", __func__);
725
726 pthread_mutex_lock(&adev_init_lock);
727
728 if (adev != NULL && adev->platform != NULL) {
729 pthread_mutex_lock(&adev->lock);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800730 ret = audio_extn_qdsp_set_state(adev, stream_type, vol, active);
Aalique Grahame22e49102018-12-18 14:23:57 -0800731 pthread_mutex_unlock(&adev->lock);
732 }
733
734 pthread_mutex_unlock(&adev_init_lock);
735
736 ALOGV("%s: exit with ret %d", __func__, ret);
737 return ret;
738}
Aalique Grahame22e49102018-12-18 14:23:57 -0800739
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700740static bool is_supported_format(audio_format_t format)
741{
Eric Laurent86e17132013-09-12 17:49:30 -0700742 if (format == AUDIO_FORMAT_MP3 ||
Satish Babu Patakokila0c313922016-12-08 12:07:08 +0530743 format == AUDIO_FORMAT_MP2 ||
Ashish Jainf9b78162014-08-25 20:36:25 +0530744 format == AUDIO_FORMAT_AAC_LC ||
745 format == AUDIO_FORMAT_AAC_HE_V1 ||
746 format == AUDIO_FORMAT_AAC_HE_V2 ||
Manish Dewangana6fc5442015-08-24 20:30:31 +0530747 format == AUDIO_FORMAT_AAC_ADTS_LC ||
748 format == AUDIO_FORMAT_AAC_ADTS_HE_V1 ||
749 format == AUDIO_FORMAT_AAC_ADTS_HE_V2 ||
Arun Kumar Dasari3b174182016-12-27 13:01:14 +0530750 format == AUDIO_FORMAT_AAC_LATM_LC ||
751 format == AUDIO_FORMAT_AAC_LATM_HE_V1 ||
752 format == AUDIO_FORMAT_AAC_LATM_HE_V2 ||
Ashish Jain5106d362016-05-11 19:23:33 +0530753 format == AUDIO_FORMAT_PCM_24_BIT_PACKED ||
754 format == AUDIO_FORMAT_PCM_8_24_BIT ||
Ashish Jainf1eaa582016-05-23 20:54:24 +0530755 format == AUDIO_FORMAT_PCM_FLOAT ||
756 format == AUDIO_FORMAT_PCM_32_BIT ||
vivek mehta0ea887a2015-08-26 14:01:20 -0700757 format == AUDIO_FORMAT_PCM_16_BIT ||
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530758 format == AUDIO_FORMAT_AC3 ||
759 format == AUDIO_FORMAT_E_AC3 ||
Ben Romberger1aaaf862017-04-06 17:49:46 -0700760 format == AUDIO_FORMAT_DOLBY_TRUEHD ||
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530761 format == AUDIO_FORMAT_DTS ||
762 format == AUDIO_FORMAT_DTS_HD ||
Weiyin Jiang18ac4e92015-03-15 15:03:40 +0800763 format == AUDIO_FORMAT_FLAC ||
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530764 format == AUDIO_FORMAT_ALAC ||
765 format == AUDIO_FORMAT_APE ||
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530766 format == AUDIO_FORMAT_DSD ||
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530767 format == AUDIO_FORMAT_VORBIS ||
Weiyin Jiang18ac4e92015-03-15 15:03:40 +0800768 format == AUDIO_FORMAT_WMA ||
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +0530769 format == AUDIO_FORMAT_WMA_PRO ||
Naresh Tanniru928f0862017-04-07 16:44:23 -0700770 format == AUDIO_FORMAT_APTX ||
771 format == AUDIO_FORMAT_IEC61937)
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -0800772 return true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700773
774 return false;
775}
776
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700777static inline bool is_mmap_usecase(audio_usecase_t uc_id)
778{
779 return (uc_id == USECASE_AUDIO_RECORD_AFE_PROXY) ||
780 (uc_id == USECASE_AUDIO_PLAYBACK_AFE_PROXY);
781}
782
Aniket Kumar Lata8426d1f2019-06-10 15:25:53 -0700783static inline bool is_valid_volume(float left, float right)
784{
785 return ((left >= 0.0f && right >= 0.0f) ? true : false);
786}
787
Avinash Vaish71a8b972014-07-24 15:36:33 +0530788static int enable_audio_route_for_voice_usecases(struct audio_device *adev,
789 struct audio_usecase *uc_info)
790{
791 struct listnode *node;
792 struct audio_usecase *usecase;
793
794 if (uc_info == NULL)
795 return -EINVAL;
796
797 /* Re-route all voice usecases on the shared backend other than the
798 specified usecase to new snd devices */
799 list_for_each(node, &adev->usecase_list) {
800 usecase = node_to_item(node, struct audio_usecase, list);
Narsinga Rao Chellae7f33c12015-01-28 15:23:34 -0800801 if ((usecase->type == VOICE_CALL) && (usecase != uc_info))
Avinash Vaish71a8b972014-07-24 15:36:33 +0530802 enable_audio_route(adev, usecase);
803 }
804 return 0;
805}
806
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +0530807static void enable_asrc_mode(struct audio_device *adev)
Preetam Singh Ranawatb0c0dd72016-08-18 00:32:06 +0530808{
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +0530809 ALOGV("%s", __func__);
810 audio_route_apply_and_update_path(adev->audio_route,
811 "asrc-mode");
812 adev->asrc_mode_enabled = true;
813}
814
815static void disable_asrc_mode(struct audio_device *adev)
816{
817 ALOGV("%s", __func__);
818 audio_route_reset_and_update_path(adev->audio_route,
819 "asrc-mode");
820 adev->asrc_mode_enabled = false;
821}
822
823/*
824 * - Enable ASRC mode for incoming mix path use case(Headphone backend)if Headphone
825 * 44.1 or Native DSD backends are enabled for any of current use case.
826 * e.g. 48-> + (Naitve DSD or Headphone 44.1)
827 * - Disable current mix path use case(Headphone backend) and re-enable it with
828 * ASRC mode for incoming Headphone 44.1 or Native DSD use case.
829 * e.g. Naitve DSD or Headphone 44.1 -> + 48
830 */
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +0530831static void check_and_set_asrc_mode(struct audio_device *adev,
832 struct audio_usecase *uc_info,
833 snd_device_t snd_device)
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +0530834{
835 ALOGV("%s snd device %d", __func__, snd_device);
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +0530836 int i, num_new_devices = 0;
837 snd_device_t split_new_snd_devices[SND_DEVICE_OUT_END];
838 /*
839 *Split snd device for new combo use case
840 *e.g. Headphopne 44.1-> + Ringtone (Headphone + Speaker)
841 */
842 if (platform_split_snd_device(adev->platform,
843 snd_device,
844 &num_new_devices,
845 split_new_snd_devices) == 0) {
846 for (i = 0; i < num_new_devices; i++)
847 check_and_set_asrc_mode(adev, uc_info, split_new_snd_devices[i]);
848 } else {
849 int new_backend_idx = platform_get_backend_index(snd_device);
850 if (((new_backend_idx == HEADPHONE_BACKEND) ||
851 (new_backend_idx == HEADPHONE_44_1_BACKEND) ||
852 (new_backend_idx == DSD_NATIVE_BACKEND)) &&
853 !adev->asrc_mode_enabled) {
854 struct listnode *node = NULL;
855 struct audio_usecase *uc = NULL;
856 struct stream_out *curr_out = NULL;
857 int usecase_backend_idx = DEFAULT_CODEC_BACKEND;
858 int i, num_devices, ret = 0;
859 snd_device_t split_snd_devices[SND_DEVICE_OUT_END];
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +0530860
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +0530861 list_for_each(node, &adev->usecase_list) {
862 uc = node_to_item(node, struct audio_usecase, list);
863 curr_out = (struct stream_out*) uc->stream.out;
864 if (curr_out && PCM_PLAYBACK == uc->type && uc != uc_info) {
865 /*
866 *Split snd device for existing combo use case
867 *e.g. Ringtone (Headphone + Speaker) + Headphopne 44.1
868 */
869 ret = platform_split_snd_device(adev->platform,
870 uc->out_snd_device,
871 &num_devices,
872 split_snd_devices);
873 if (ret < 0 || num_devices == 0) {
874 ALOGV("%s: Unable to split uc->out_snd_device: %d",__func__, uc->out_snd_device);
875 split_snd_devices[0] = uc->out_snd_device;
876 num_devices = 1;
Garmond Leung50058f62017-02-08 09:49:30 -0800877 }
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +0530878 for (i = 0; i < num_devices; i++) {
879 usecase_backend_idx = platform_get_backend_index(split_snd_devices[i]);
880 ALOGD("%s:snd_dev %d usecase_backend_idx %d",__func__, split_snd_devices[i],usecase_backend_idx);
881 if((new_backend_idx == HEADPHONE_BACKEND) &&
882 ((usecase_backend_idx == HEADPHONE_44_1_BACKEND) ||
883 (usecase_backend_idx == DSD_NATIVE_BACKEND))) {
884 ALOGD("%s:DSD or native stream detected enabling asrcmode in hardware",
885 __func__);
886 enable_asrc_mode(adev);
887 break;
888 } else if(((new_backend_idx == HEADPHONE_44_1_BACKEND) ||
889 (new_backend_idx == DSD_NATIVE_BACKEND)) &&
890 (usecase_backend_idx == HEADPHONE_BACKEND)) {
891 ALOGD("%s:48K stream detected, disabling and enabling it with asrcmode in hardware",
892 __func__);
893 disable_audio_route(adev, uc);
894 disable_snd_device(adev, uc->out_snd_device);
895 // Apply true-high-quality-mode if DSD or > 44.1KHz or >=24-bit
896 if (new_backend_idx == DSD_NATIVE_BACKEND)
897 audio_route_apply_and_update_path(adev->audio_route,
898 "hph-true-highquality-mode");
899 else if ((new_backend_idx == HEADPHONE_44_1_BACKEND) &&
900 (curr_out->bit_width >= 24))
901 audio_route_apply_and_update_path(adev->audio_route,
902 "hph-highquality-mode");
903 enable_asrc_mode(adev);
904 enable_snd_device(adev, uc->out_snd_device);
905 enable_audio_route(adev, uc);
906 break;
907 }
908 }
909 // reset split devices count
910 num_devices = 0;
Garmond Leung50058f62017-02-08 09:49:30 -0800911 }
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +0530912 if (adev->asrc_mode_enabled)
913 break;
Preetam Singh Ranawatb0c0dd72016-08-18 00:32:06 +0530914 }
915 }
916 }
917}
918
Vikram Pandurangadf59cae2017-08-03 18:04:55 -0700919static int send_effect_enable_disable_mixer_ctl(struct audio_device *adev,
920 struct audio_effect_config effect_config,
921 unsigned int param_value)
922{
923 char mixer_ctl_name[] = "Audio Effect";
924 struct mixer_ctl *ctl;
925 long set_values[6];
926 struct stream_in *in = adev->active_input;
927
928 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
929 if (!ctl) {
930 ALOGE("%s: Could not get mixer ctl - %s",
931 __func__, mixer_ctl_name);
932 return -EINVAL;
933 }
934
935 set_values[0] = 1; //0:Rx 1:Tx
936 set_values[1] = in->app_type_cfg.app_type;
937 set_values[2] = (long)effect_config.module_id;
938 set_values[3] = (long)effect_config.instance_id;
939 set_values[4] = (long)effect_config.param_id;
940 set_values[5] = param_value;
941
942 mixer_ctl_set_array(ctl, set_values, ARRAY_SIZE(set_values));
943
944 return 0;
945
946}
947
948static int update_effect_param_ecns(struct audio_device *adev, unsigned int module_id,
949 int effect_type, unsigned int *param_value)
950{
951 int ret = 0;
952 struct audio_effect_config other_effect_config;
953 struct audio_usecase *usecase = NULL;
954 struct stream_in *in = adev->active_input;
955
956 usecase = get_usecase_from_list(adev, in->usecase);
957 if (!usecase)
958 return -EINVAL;
959
960 ret = platform_get_effect_config_data(usecase->in_snd_device, &other_effect_config,
961 effect_type == EFFECT_AEC ? EFFECT_NS : EFFECT_AEC);
962 if (ret < 0) {
963 ALOGE("%s Failed to get effect params %d", __func__, ret);
964 return ret;
965 }
966
967 if (module_id == other_effect_config.module_id) {
968 //Same module id for AEC/NS. Values need to be combined
969 if (((effect_type == EFFECT_AEC) && (in->enable_ns)) ||
970 ((effect_type == EFFECT_NS) && (in->enable_aec))) {
971 *param_value |= other_effect_config.param_value;
972 }
973 }
974
975 return ret;
976}
977
978static int enable_disable_effect(struct audio_device *adev, int effect_type, bool enable)
Gangadhar Sb0210342019-02-22 17:39:41 +0530979{
Vikram Pandurangadf59cae2017-08-03 18:04:55 -0700980 struct audio_effect_config effect_config;
981 struct audio_usecase *usecase = NULL;
982 int ret = 0;
983 unsigned int param_value = 0;
984 struct stream_in *in = adev->active_input;
985
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800986 if(!voice_extn_is_dynamic_ecns_enabled())
987 return ENOSYS;
988
Vikram Pandurangadf59cae2017-08-03 18:04:55 -0700989 if (!in) {
990 ALOGE("%s: Invalid input stream", __func__);
991 return -EINVAL;
992 }
993
994 ALOGD("%s: effect_type:%d enable:%d", __func__, effect_type, enable);
995
996 usecase = get_usecase_from_list(adev, in->usecase);
Weiyin Jiang20d3fa62018-08-01 18:06:27 +0800997 if (usecase == NULL) {
998 ALOGE("%s: Could not find the usecase (%d) in the list",
999 __func__, in->usecase);
1000 return -EINVAL;
1001 }
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001002
1003 ret = platform_get_effect_config_data(usecase->in_snd_device, &effect_config, effect_type);
1004 if (ret < 0) {
1005 ALOGE("%s Failed to get module id %d", __func__, ret);
1006 return ret;
1007 }
1008 ALOGV("%s: %d %d usecase->id:%d usecase->in_snd_device:%d", __func__, effect_config.module_id,
1009 in->app_type_cfg.app_type, usecase->id, usecase->in_snd_device);
1010
1011 if(enable)
1012 param_value = effect_config.param_value;
1013
1014 /*Special handling for AEC & NS effects Param values need to be
1015 updated if module ids are same*/
1016
1017 if ((effect_type == EFFECT_AEC) || (effect_type == EFFECT_NS)) {
1018 ret = update_effect_param_ecns(adev, effect_config.module_id, effect_type, &param_value);
1019 if (ret < 0)
1020 return ret;
1021 }
1022
1023 ret = send_effect_enable_disable_mixer_ctl(adev, effect_config, param_value);
1024
1025 return ret;
1026}
1027
1028static void check_and_enable_effect(struct audio_device *adev)
1029{
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001030 if(!voice_extn_is_dynamic_ecns_enabled())
1031 return;
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001032
1033 if (adev->active_input->enable_aec) {
1034 enable_disable_effect(adev, EFFECT_AEC, true);
1035 }
1036
1037 if (adev->active_input->enable_ns &&
1038 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
1039 enable_disable_effect(adev, EFFECT_NS, true);
1040 }
1041}
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001042
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07001043int pcm_ioctl(struct pcm *pcm, int request, ...)
1044{
1045 va_list ap;
1046 void * arg;
1047 int pcm_fd = *(int*)pcm;
1048
1049 va_start(ap, request);
1050 arg = va_arg(ap, void *);
1051 va_end(ap);
1052
1053 return ioctl(pcm_fd, request, arg);
1054}
1055
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -07001056int enable_audio_route(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001057 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001058{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001059 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001060 char mixer_path[MIXER_PATH_MAX_LENGTH];
Manish Dewangan58229382017-02-02 15:48:41 +05301061 struct stream_out *out = NULL;
Soumya Managoli6993b762018-06-28 16:04:57 +05301062 int ret = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001063
1064 if (usecase == NULL)
1065 return -EINVAL;
1066
1067 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
1068
Surendar Karka93cd25a2018-08-28 14:21:37 +05301069 if (usecase->type == PCM_CAPTURE || usecase->type == TRANSCODE_LOOPBACK_TX)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001070 snd_device = usecase->in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001071 else
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001072 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001073
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08001074#ifdef DS1_DOLBY_DAP_ENABLED
1075 audio_extn_dolby_set_dmid(adev);
1076 audio_extn_dolby_set_endpoint(adev);
1077#endif
Pradnya Chaphekar8a9dcd82014-09-09 09:49:10 -07001078 audio_extn_dolby_ds2_set_endpoint(adev);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001079 audio_extn_sound_trigger_update_stream_status(usecase, ST_EVENT_STREAM_BUSY);
Dhananjay Kumar45b71742014-05-29 21:47:27 +05301080 audio_extn_listen_update_stream_status(usecase, LISTEN_EVENT_STREAM_BUSY);
Ben Romberger1fafdde2015-09-09 19:43:15 -07001081 audio_extn_utils_send_app_type_cfg(adev, usecase);
Jasmine Cha4dcc1092019-03-04 18:12:47 +08001082 if (audio_extn_is_maxx_audio_enabled())
1083 audio_extn_ma_set_device(usecase);
Dhananjay Kumar14170dd2015-08-28 13:24:16 +05301084 audio_extn_utils_send_audio_calibration(adev, usecase);
Manish Dewangan58229382017-02-02 15:48:41 +05301085 if ((usecase->type == PCM_PLAYBACK) && is_offload_usecase(usecase->id)) {
1086 out = usecase->stream.out;
1087 if (out && out->compr)
1088 audio_extn_utils_compress_set_clk_rec_mode(usecase);
1089 }
Dhananjay Kumar429eb452018-12-10 22:26:53 +05301090 audio_extn_set_custom_mtmx_params(adev, usecase, true);
Manish Dewangan58229382017-02-02 15:48:41 +05301091
Andy Hung756ecc12018-10-19 17:47:12 -07001092 // we shouldn't truncate mixer_path
1093 ALOGW_IF(strlcpy(mixer_path, use_case_table[usecase->id], sizeof(mixer_path))
1094 >= sizeof(mixer_path), "%s: truncation on mixer path", __func__);
1095 // this also appends to mixer_path
Banajit Goswami20cdd212015-09-11 01:11:30 -07001096 platform_add_backend_name(mixer_path, snd_device, usecase);
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07001097 ALOGD("%s: apply mixer and update path: %s", __func__, mixer_path);
Soumya Managoli6993b762018-06-28 16:04:57 +05301098 ret = audio_route_apply_and_update_path(adev->audio_route, mixer_path);
1099 if (!ret && usecase->id == USECASE_AUDIO_PLAYBACK_FM) {
1100 struct str_parms *parms = str_parms_create_str("fm_restore_volume=1");
1101 if (parms) {
1102 audio_extn_fm_set_parameters(adev, parms);
1103 str_parms_destroy(parms);
1104 }
1105 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001106 ALOGV("%s: exit", __func__);
1107 return 0;
1108}
1109
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001110int disable_audio_route(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001111 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001112{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001113 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001114 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001115
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05301116 if (usecase == NULL || usecase->id == USECASE_INVALID)
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001117 return -EINVAL;
1118
1119 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Surendar Karka93cd25a2018-08-28 14:21:37 +05301120 if (usecase->type == PCM_CAPTURE || usecase->type == TRANSCODE_LOOPBACK_TX)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001121 snd_device = usecase->in_snd_device;
1122 else
1123 snd_device = usecase->out_snd_device;
Andy Hung756ecc12018-10-19 17:47:12 -07001124 // we shouldn't truncate mixer_path
1125 ALOGW_IF(strlcpy(mixer_path, use_case_table[usecase->id], sizeof(mixer_path))
1126 >= sizeof(mixer_path), "%s: truncation on mixer path", __func__);
1127 // this also appends to mixer_path
Banajit Goswami20cdd212015-09-11 01:11:30 -07001128 platform_add_backend_name(mixer_path, snd_device, usecase);
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07001129 ALOGD("%s: reset and update mixer path: %s", __func__, mixer_path);
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07001130 audio_route_reset_and_update_path(adev->audio_route, mixer_path);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001131 audio_extn_sound_trigger_update_stream_status(usecase, ST_EVENT_STREAM_FREE);
Dhananjay Kumar45b71742014-05-29 21:47:27 +05301132 audio_extn_listen_update_stream_status(usecase, LISTEN_EVENT_STREAM_FREE);
Dhananjay Kumar429eb452018-12-10 22:26:53 +05301133 audio_extn_set_custom_mtmx_params(adev, usecase, false);
Weiyin Jiang298ffd92019-06-03 14:29:30 +08001134 if ((usecase->type == PCM_PLAYBACK) &&
1135 (usecase->stream.out != NULL))
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +05301136 usecase->stream.out->pspd_coeff_sent = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001137 ALOGV("%s: exit", __func__);
1138 return 0;
1139}
1140
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -07001141int enable_snd_device(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001142 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001143{
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301144 int i, num_devices = 0;
1145 snd_device_t new_snd_devices[SND_DEVICE_OUT_END];
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001146 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
1147
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001148 if (snd_device < SND_DEVICE_MIN ||
1149 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001150 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001151 return -EINVAL;
1152 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001153
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001154 if (platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0) {
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001155 ALOGE("%s: Invalid sound device returned", __func__);
1156 return -EINVAL;
1157 }
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001158
1159 adev->snd_dev_ref_cnt[snd_device]++;
1160
1161 if ((adev->snd_dev_ref_cnt[snd_device] > 1) &&
1162 (platform_split_snd_device(adev->platform,
1163 snd_device,
1164 &num_devices,
1165 new_snd_devices) != 0)) {
Eric Laurent994a6932013-07-17 11:51:42 -07001166 ALOGV("%s: snd_device(%d: %s) is already active",
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001167 __func__, snd_device, device_name);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001168 return 0;
1169 }
1170
Gopikrishnaiah Anandane85d0462014-06-30 21:41:20 -07001171 if (audio_extn_spkr_prot_is_enabled())
1172 audio_extn_spkr_prot_calib_cancel(adev);
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -07001173
Aalique Grahame22e49102018-12-18 14:23:57 -08001174 audio_extn_dsm_feedback_enable(adev, snd_device, true);
1175
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001176 if (platform_can_enable_spkr_prot_on_device(snd_device) &&
1177 audio_extn_spkr_prot_is_enabled()) {
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001178 if (platform_get_spkr_prot_acdb_id(snd_device) < 0) {
1179 goto err;
1180 }
1181 audio_extn_dev_arbi_acquire(snd_device);
1182 if (audio_extn_spkr_prot_start_processing(snd_device)) {
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -08001183 ALOGE("%s: spkr_start_processing failed", __func__);
Ravit Dennisaaee49c2015-02-04 21:26:22 +02001184 audio_extn_dev_arbi_release(snd_device);
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001185 goto err;
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -08001186 }
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001187 } else if (platform_split_snd_device(adev->platform,
1188 snd_device,
1189 &num_devices,
1190 new_snd_devices) == 0) {
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301191 for (i = 0; i < num_devices; i++) {
1192 enable_snd_device(adev, new_snd_devices[i]);
1193 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001194 platform_set_speaker_gain_in_combo(adev, snd_device, true);
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -08001195 } else {
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07001196 ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, device_name);
Naresh Tannirucd2353e2016-08-19 00:37:25 +05301197
Preetam Singh Ranawatf1d417c2017-01-10 17:00:32 +05301198
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001199 if ((SND_DEVICE_OUT_BT_A2DP == snd_device) &&
1200 (audio_extn_a2dp_start_playback() < 0)) {
1201 ALOGE(" fail to configure A2dp Source control path ");
1202 goto err;
1203 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001204
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001205 if ((SND_DEVICE_IN_BT_A2DP == snd_device) &&
1206 (audio_extn_a2dp_start_capture() < 0)) {
1207 ALOGE(" fail to configure A2dp Sink control path ");
1208 goto err;
1209 }
Naresh Tannirucd2353e2016-08-19 00:37:25 +05301210
Zhou Song12c29502019-03-16 10:37:18 +08001211 if (((SND_DEVICE_OUT_BT_SCO_SWB == snd_device) ||
1212 (SND_DEVICE_IN_BT_SCO_MIC_SWB_NREC == snd_device) ||
1213 (SND_DEVICE_IN_BT_SCO_MIC_SWB == snd_device)) &&
1214 (audio_extn_sco_start_configuration() < 0)) {
1215 ALOGE(" fail to configure sco control path ");
1216 goto err;
1217 }
1218
Bharath Ramachandramurthy0de16782014-03-28 21:34:33 -07001219 /* due to the possibility of calibration overwrite between listen
1220 and audio, notify listen hal before audio calibration is sent */
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001221 audio_extn_sound_trigger_update_device_status(snd_device,
1222 ST_EVENT_SND_DEVICE_BUSY);
Dhananjay Kumar45b71742014-05-29 21:47:27 +05301223 audio_extn_listen_update_device_status(snd_device,
1224 LISTEN_EVENT_SND_DEVICE_BUSY);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001225 if (platform_get_snd_device_acdb_id(snd_device) < 0) {
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001226 audio_extn_sound_trigger_update_device_status(snd_device,
1227 ST_EVENT_SND_DEVICE_FREE);
Dhananjay Kumar45b71742014-05-29 21:47:27 +05301228 audio_extn_listen_update_device_status(snd_device,
1229 LISTEN_EVENT_SND_DEVICE_FREE);
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001230 goto err;
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -07001231 }
Lior Barenboim0b61bc72014-05-13 13:01:37 +03001232 audio_extn_dev_arbi_acquire(snd_device);
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07001233 audio_route_apply_and_update_path(adev->audio_route, device_name);
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301234
1235 if (SND_DEVICE_OUT_HEADPHONES == snd_device &&
1236 !adev->native_playback_enabled &&
1237 audio_is_true_native_stream_active(adev)) {
1238 ALOGD("%s: %d: napb: enabling native mode in hardware",
1239 __func__, __LINE__);
1240 audio_route_apply_and_update_path(adev->audio_route,
1241 "true-native-mode");
1242 adev->native_playback_enabled = true;
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +05301243 }
Dhanalakshmi Siddani10621622018-04-30 15:07:27 +05301244 if (((snd_device == SND_DEVICE_IN_HANDSET_6MIC) ||
1245 (snd_device == SND_DEVICE_IN_HANDSET_QMIC)) &&
Garmond Leunge2433c32017-09-28 21:51:22 -07001246 (audio_extn_ffv_get_stream() == adev->active_input)) {
1247 ALOGD("%s: init ec ref loopback", __func__);
1248 audio_extn_ffv_init_ec_ref_loopback(adev, snd_device);
1249 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001250 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001251 return 0;
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001252err:
1253 adev->snd_dev_ref_cnt[snd_device]--;
1254 return -EINVAL;;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001255}
1256
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001257int disable_snd_device(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001258 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001259{
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301260 int i, num_devices = 0;
1261 snd_device_t new_snd_devices[SND_DEVICE_OUT_END];
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001262 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
1263
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001264 if (snd_device < SND_DEVICE_MIN ||
1265 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001266 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001267 return -EINVAL;
1268 }
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001269
1270 if (platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0) {
1271 ALOGE("%s: Invalid sound device returned", __func__);
1272 return -EINVAL;
1273 }
1274
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001275 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
1276 ALOGE("%s: device ref cnt is already 0", __func__);
1277 return -EINVAL;
1278 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001279
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001280 adev->snd_dev_ref_cnt[snd_device]--;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -07001281
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001282
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001283 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07001284 ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, device_name);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301285
Aalique Grahame22e49102018-12-18 14:23:57 -08001286 audio_extn_dsm_feedback_enable(adev, snd_device, false);
1287
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001288 if (platform_can_enable_spkr_prot_on_device(snd_device) &&
1289 audio_extn_spkr_prot_is_enabled()) {
Anish Kumar46c7b872014-09-09 01:49:44 -07001290 audio_extn_spkr_prot_stop_processing(snd_device);
Vignesh Kulothungan3b5fae52017-09-25 12:16:30 -07001291
1292 // when speaker device is disabled, reset swap.
1293 // will be renabled on usecase start
1294 platform_set_swap_channels(adev, false);
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001295 } else if (platform_split_snd_device(adev->platform,
1296 snd_device,
1297 &num_devices,
1298 new_snd_devices) == 0) {
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301299 for (i = 0; i < num_devices; i++) {
1300 disable_snd_device(adev, new_snd_devices[i]);
1301 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001302 platform_set_speaker_gain_in_combo(adev, snd_device, false);
Lior Barenboim0b61bc72014-05-13 13:01:37 +03001303 } else {
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07001304 audio_route_reset_and_update_path(adev->audio_route, device_name);
Lior Barenboim0b61bc72014-05-13 13:01:37 +03001305 }
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001306
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001307 if (snd_device == SND_DEVICE_OUT_BT_A2DP)
Naresh Tannirucd2353e2016-08-19 00:37:25 +05301308 audio_extn_a2dp_stop_playback();
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001309 else if (snd_device == SND_DEVICE_IN_BT_A2DP)
Florian Pfister1a84f312018-07-19 14:38:18 +02001310 audio_extn_a2dp_stop_capture();
Zhou Songd6d71752019-05-21 18:08:51 +08001311 else if ((snd_device == SND_DEVICE_OUT_HDMI) ||
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001312 (snd_device == SND_DEVICE_OUT_DISPLAY_PORT))
Ashish Jain81eb2a82015-05-13 10:52:34 +05301313 adev->is_channel_status_set = false;
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001314 else if ((snd_device == SND_DEVICE_OUT_HEADPHONES) &&
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301315 adev->native_playback_enabled) {
1316 ALOGD("%s: %d: napb: disabling native mode in hardware",
1317 __func__, __LINE__);
1318 audio_route_reset_and_update_path(adev->audio_route,
1319 "true-native-mode");
1320 adev->native_playback_enabled = false;
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001321 } else if ((snd_device == SND_DEVICE_OUT_HEADPHONES) &&
Preetam Singh Ranawatb0c0dd72016-08-18 00:32:06 +05301322 adev->asrc_mode_enabled) {
1323 ALOGD("%s: %d: disabling asrc mode in hardware", __func__, __LINE__);
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +05301324 disable_asrc_mode(adev);
1325 audio_route_apply_and_update_path(adev->audio_route, "hph-lowpower-mode");
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001326 } else if (((snd_device == SND_DEVICE_IN_HANDSET_6MIC) ||
Dhanalakshmi Siddaniaf4bd622019-02-27 16:28:06 +05301327 (snd_device == SND_DEVICE_IN_HANDSET_QMIC)) &&
Garmond Leunge2433c32017-09-28 21:51:22 -07001328 (audio_extn_ffv_get_stream() == adev->active_input)) {
1329 ALOGD("%s: deinit ec ref loopback", __func__);
1330 audio_extn_ffv_deinit_ec_ref_loopback(adev, snd_device);
1331 }
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001332
1333 audio_extn_utils_release_snd_device(snd_device);
1334 } else {
1335 if (platform_split_snd_device(adev->platform,
1336 snd_device,
1337 &num_devices,
1338 new_snd_devices) == 0) {
1339 for (i = 0; i < num_devices; i++) {
1340 adev->snd_dev_ref_cnt[new_snd_devices[i]]--;
1341 }
1342 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001343 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001344
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001345 return 0;
1346}
1347
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001348/*
1349 legend:
1350 uc - existing usecase
1351 new_uc - new usecase
1352 d1, d11, d2 - SND_DEVICE enums
1353 a1, a2 - corresponding ANDROID device enums
1354 B1, B2 - backend strings
1355
1356case 1
1357 uc->dev d1 (a1) B1
1358 new_uc->dev d1 (a1), d2 (a2) B1, B2
1359
1360 resolution: disable and enable uc->dev on d1
1361
1362case 2
1363 uc->dev d1 (a1) B1
1364 new_uc->dev d11 (a1) B1
1365
1366 resolution: need to switch uc since d1 and d11 are related
1367 (e.g. speaker and voice-speaker)
1368 use ANDROID_DEVICE_OUT enums to match devices since SND_DEVICE enums may vary
1369
1370case 3
1371 uc->dev d1 (a1) B1
1372 new_uc->dev d2 (a2) B2
1373
1374 resolution: no need to switch uc
1375
1376case 4
1377 uc->dev d1 (a1) B1
1378 new_uc->dev d2 (a2) B1
1379
1380 resolution: disable enable uc-dev on d2 since backends match
1381 we cannot enable two streams on two different devices if they
1382 share the same backend. e.g. if offload is on speaker device using
1383 QUAD_MI2S backend and a low-latency stream is started on voice-handset
1384 using the same backend, offload must also be switched to voice-handset.
1385
1386case 5
1387 uc->dev d1 (a1) B1
1388 new_uc->dev d1 (a1), d2 (a2) B1
1389
1390 resolution: disable enable uc-dev on d2 since backends match
1391 we cannot enable two streams on two different devices if they
1392 share the same backend.
1393
1394case 6
1395 uc->dev d1 (a1) B1
1396 new_uc->dev d2 (a1) B2
1397
1398 resolution: no need to switch
1399
1400case 7
1401 uc->dev d1 (a1), d2 (a2) B1, B2
1402 new_uc->dev d1 (a1) B1
1403
1404 resolution: no need to switch
1405
Zhou Song4ba65882018-07-09 14:48:07 +08001406case 8
1407 uc->dev d1 (a1) B1
1408 new_uc->dev d11 (a1), d2 (a2) B1, B2
1409 resolution: compared to case 1, for this case, d1 and d11 are related
1410 then need to do the same as case 2 to siwtch to new uc
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001411*/
1412static snd_device_t derive_playback_snd_device(void * platform,
1413 struct audio_usecase *uc,
1414 struct audio_usecase *new_uc,
1415 snd_device_t new_snd_device)
1416{
Siddartha Shaik31b530e2017-05-19 15:26:33 +05301417 audio_devices_t a1, a2;
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001418
1419 snd_device_t d1 = uc->out_snd_device;
1420 snd_device_t d2 = new_snd_device;
1421
Siddartha Shaik31b530e2017-05-19 15:26:33 +05301422 switch (uc->type) {
Surendar Karka93cd25a2018-08-28 14:21:37 +05301423 case TRANSCODE_LOOPBACK_RX :
Siddartha Shaik31b530e2017-05-19 15:26:33 +05301424 a1 = uc->stream.inout->out_config.devices;
1425 a2 = new_uc->stream.inout->out_config.devices;
1426 break;
1427 default :
1428 a1 = uc->stream.out->devices;
1429 a2 = new_uc->stream.out->devices;
1430 break;
1431 }
1432
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001433 // Treat as a special case when a1 and a2 are not disjoint
1434 if ((a1 != a2) && (a1 & a2)) {
1435 snd_device_t d3[2];
1436 int num_devices = 0;
1437 int ret = platform_split_snd_device(platform,
1438 popcount(a1) > 1 ? d1 : d2,
1439 &num_devices,
1440 d3);
1441 if (ret < 0) {
1442 if (ret != -ENOSYS) {
1443 ALOGW("%s failed to split snd_device %d",
1444 __func__,
1445 popcount(a1) > 1 ? d1 : d2);
1446 }
1447 goto end;
1448 }
1449
1450 // NB: case 7 is hypothetical and isn't a practical usecase yet.
1451 // But if it does happen, we need to give priority to d2 if
1452 // the combo devices active on the existing usecase share a backend.
1453 // This is because we cannot have a usecase active on a combo device
1454 // and a new usecase requests one device in this combo pair.
1455 if (platform_check_backends_match(d3[0], d3[1])) {
1456 return d2; // case 5
1457 } else {
Garmond Leungb9eeba42018-09-18 11:10:41 -07001458 // check if d1 is related to any of d3's
1459 if (d1 == d3[0] || d1 == d3[1])
Zhou Song4ba65882018-07-09 14:48:07 +08001460 return d1; // case 1
1461 else
1462 return d3[1]; // case 8
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001463 }
1464 } else {
1465 if (platform_check_backends_match(d1, d2)) {
1466 return d2; // case 2, 4
1467 } else {
1468 return d1; // case 6, 3
1469 }
1470 }
1471
1472end:
1473 return d2; // return whatever was calculated before.
1474}
1475
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001476static void check_usecases_codec_backend(struct audio_device *adev,
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301477 struct audio_usecase *uc_info,
1478 snd_device_t snd_device)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001479{
1480 struct listnode *node;
1481 struct audio_usecase *usecase;
1482 bool switch_device[AUDIO_USECASE_MAX];
Chaithanya Krishna Bacharaju49e7db02017-03-14 11:57:26 +05301483 snd_device_t uc_derive_snd_device;
1484 snd_device_t derive_snd_device[AUDIO_USECASE_MAX];
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001485 snd_device_t split_snd_devices[SND_DEVICE_OUT_END];
1486 int i, num_uc_to_switch = 0, num_devices = 0;
kunleiz5cd52b82016-11-07 17:22:52 +08001487 int status = 0;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301488 bool force_restart_session = false;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001489 /*
1490 * This function is to make sure that all the usecases that are active on
1491 * the hardware codec backend are always routed to any one device that is
1492 * handled by the hardware codec.
1493 * For example, if low-latency and deep-buffer usecases are currently active
1494 * on speaker and out_set_parameters(headset) is received on low-latency
1495 * output, then we have to make sure deep-buffer is also switched to headset,
1496 * because of the limitation that both the devices cannot be enabled
1497 * at the same time as they share the same backend.
1498 */
Mingming Yin3ee55c62014-08-04 14:23:35 -07001499 /*
1500 * This call is to check if we need to force routing for a particular stream
1501 * If there is a backend configuration change for the device when a
1502 * new stream starts, then ADM needs to be closed and re-opened with the new
1503 * configuraion. This call check if we need to re-route all the streams
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -08001504 * associated with the backend. Touch tone + 24 bit + native playback.
Mingming Yin3ee55c62014-08-04 14:23:35 -07001505 */
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -08001506 bool force_routing = platform_check_and_set_codec_backend_cfg(adev, uc_info,
1507 snd_device);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301508 /* For a2dp device reconfigure all active sessions
1509 * with new AFE encoder format based on a2dp state
1510 */
1511 if ((SND_DEVICE_OUT_BT_A2DP == snd_device ||
1512 SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP == snd_device) &&
1513 audio_extn_a2dp_is_force_device_switch()) {
1514 force_routing = true;
1515 force_restart_session = true;
1516 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301517 ALOGD("%s:becf: force routing %d", __func__, force_routing);
1518
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001519 /* Disable all the usecases on the shared backend other than the
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -08001520 * specified usecase.
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -08001521 */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001522 for (i = 0; i < AUDIO_USECASE_MAX; i++)
1523 switch_device[i] = false;
1524
1525 list_for_each(node, &adev->usecase_list) {
1526 usecase = node_to_item(node, struct audio_usecase, list);
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -08001527
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301528 ALOGD("%s:becf: (%d) check_usecases curr device: %s, usecase device:%s "
1529 "backends match %d",__func__, i,
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301530 platform_get_snd_device_name(snd_device),
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301531 platform_get_snd_device_name(usecase->out_snd_device),
1532 platform_check_backends_match(snd_device, usecase->out_snd_device));
Ashish Jain6a65b352017-03-21 17:24:40 +05301533 if ((usecase->type != PCM_CAPTURE) && (usecase != uc_info)) {
1534 uc_derive_snd_device = derive_playback_snd_device(adev->platform,
1535 usecase, uc_info, snd_device);
1536 if (((uc_derive_snd_device != usecase->out_snd_device) || force_routing) &&
1537 ((usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) ||
1538 (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) ||
1539 (usecase->devices & AUDIO_DEVICE_OUT_USB_DEVICE) ||
Garmond Leung2fd4f122018-06-02 16:04:53 -07001540 (usecase->devices & AUDIO_DEVICE_OUT_USB_HEADSET) ||
Ashish Jain6a65b352017-03-21 17:24:40 +05301541 (usecase->devices & AUDIO_DEVICE_OUT_ALL_A2DP) ||
1542 (usecase->devices & AUDIO_DEVICE_OUT_ALL_SCO)) &&
1543 ((force_restart_session) ||
1544 (platform_check_backends_match(snd_device, usecase->out_snd_device)))) {
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301545 ALOGD("%s:becf: check_usecases (%s) is active on (%s) - disabling ..",
1546 __func__, use_case_table[usecase->id],
1547 platform_get_snd_device_name(usecase->out_snd_device));
1548 disable_audio_route(adev, usecase);
1549 switch_device[usecase->id] = true;
Chaithanya Krishna Bacharaju49e7db02017-03-14 11:57:26 +05301550 /* Enable existing usecase on derived playback device */
1551 derive_snd_device[usecase->id] = uc_derive_snd_device;
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301552 num_uc_to_switch++;
Ashish Jain6a65b352017-03-21 17:24:40 +05301553 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001554 }
1555 }
1556
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301557 ALOGD("%s:becf: check_usecases num.of Usecases to switch %d", __func__,
1558 num_uc_to_switch);
1559
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001560 if (num_uc_to_switch) {
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07001561 /* All streams have been de-routed. Disable the device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001562
Venkata Narendra Kumar Gutta7610e632014-04-14 23:16:38 +05301563 /* Make sure the previous devices to be disabled first and then enable the
1564 selected devices */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001565 list_for_each(node, &adev->usecase_list) {
1566 usecase = node_to_item(node, struct audio_usecase, list);
1567 if (switch_device[usecase->id]) {
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001568 /* Check if output sound device to be switched can be split and if any
1569 of the split devices match with derived sound device */
1570 if (platform_split_snd_device(adev->platform, usecase->out_snd_device,
1571 &num_devices, split_snd_devices) == 0) {
1572 adev->snd_dev_ref_cnt[usecase->out_snd_device]--;
1573 for (i = 0; i < num_devices; i++) {
1574 /* Disable devices that do not match with derived sound device */
1575 if (split_snd_devices[i] != derive_snd_device[usecase->id])
1576 disable_snd_device(adev, split_snd_devices[i]);
1577 }
1578 } else {
1579 disable_snd_device(adev, usecase->out_snd_device);
1580 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001581 }
1582 }
1583
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -07001584 list_for_each(node, &adev->usecase_list) {
1585 usecase = node_to_item(node, struct audio_usecase, list);
1586 if (switch_device[usecase->id]) {
Weiyin Jiang0d84c8e2019-04-09 22:59:54 +08001587 if (platform_split_snd_device(adev->platform, usecase->out_snd_device,
1588 &num_devices, split_snd_devices) == 0) {
1589 /* Enable derived sound device only if it does not match with
1590 one of the split sound devices. This is because the matching
1591 sound device was not disabled */
1592 bool should_enable = true;
1593 for (i = 0; i < num_devices; i++) {
1594 if (derive_snd_device[usecase->id] == split_snd_devices[i]) {
1595 should_enable = false;
1596 break;
1597 }
1598 }
1599 if (should_enable)
1600 enable_snd_device(adev, derive_snd_device[usecase->id]);
1601 } else {
1602 enable_snd_device(adev, derive_snd_device[usecase->id]);
1603 }
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -07001604 }
1605 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001606
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001607 /* Re-route all the usecases on the shared backend other than the
1608 specified usecase to new snd devices */
1609 list_for_each(node, &adev->usecase_list) {
1610 usecase = node_to_item(node, struct audio_usecase, list);
Karthik Reddy Katta3abfee22016-02-23 10:55:27 +05301611 /* Update the out_snd_device only before enabling the audio route */
1612 if (switch_device[usecase->id]) {
Chaithanya Krishna Bacharaju49e7db02017-03-14 11:57:26 +05301613 usecase->out_snd_device = derive_snd_device[usecase->id];
Karthik Reddy Katta3abfee22016-02-23 10:55:27 +05301614 if (usecase->type != VOICE_CALL) {
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301615 ALOGD("%s:becf: enabling usecase (%s) on (%s)", __func__,
Karthik Reddy Katta3abfee22016-02-23 10:55:27 +05301616 use_case_table[usecase->id],
1617 platform_get_snd_device_name(usecase->out_snd_device));
kunleiz5cd52b82016-11-07 17:22:52 +08001618 /* Update voc calibration before enabling VoIP route */
1619 if (usecase->type == VOIP_CALL)
1620 status = platform_switch_voice_call_device_post(adev->platform,
1621 usecase->out_snd_device,
kunleizab514ca2017-01-09 14:59:50 +08001622 platform_get_input_snd_device(adev->platform, uc_info->devices));
Avinash Vaish71a8b972014-07-24 15:36:33 +05301623 enable_audio_route(adev, usecase);
Sujin Panicker390724d2019-04-26 10:43:36 +05301624 if (usecase->stream.out && usecase->id == USECASE_AUDIO_PLAYBACK_VOIP) {
kunleiz46c47dd2018-03-19 16:28:09 +08001625 out_set_voip_volume(&usecase->stream.out->stream,
1626 usecase->stream.out->volume_l,
1627 usecase->stream.out->volume_r);
1628 }
Karthik Reddy Katta3abfee22016-02-23 10:55:27 +05301629 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001630 }
1631 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001632 }
1633}
1634
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05301635static void check_usecases_capture_codec_backend(struct audio_device *adev,
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001636 struct audio_usecase *uc_info,
1637 snd_device_t snd_device)
1638{
1639 struct listnode *node;
1640 struct audio_usecase *usecase;
1641 bool switch_device[AUDIO_USECASE_MAX];
1642 int i, num_uc_to_switch = 0;
Dhanalakshmi Siddanib678a802016-12-03 11:51:41 +05301643 int backend_check_cond = AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND;
kunleiz5cd52b82016-11-07 17:22:52 +08001644 int status = 0;
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001645
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05301646 bool force_routing = platform_check_and_set_capture_codec_backend_cfg(adev, uc_info,
1647 snd_device);
1648 ALOGD("%s:becf: force routing %d", __func__, force_routing);
Dhanalakshmi Siddanib678a802016-12-03 11:51:41 +05301649
1650 /*
1651 * Make sure out devices is checked against out codec backend device and
1652 * also in devices against in codec backend. Checking out device against in
1653 * codec backend or vice versa causes issues.
1654 */
1655 if (uc_info->type == PCM_CAPTURE)
1656 backend_check_cond = AUDIO_DEVICE_IN_ALL_CODEC_BACKEND;
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001657 /*
1658 * This function is to make sure that all the active capture usecases
1659 * are always routed to the same input sound device.
1660 * For example, if audio-record and voice-call usecases are currently
1661 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
1662 * is received for voice call then we have to make sure that audio-record
1663 * usecase is also switched to earpiece i.e. voice-dmic-ef,
1664 * because of the limitation that two devices cannot be enabled
1665 * at the same time if they share the same backend.
1666 */
1667 for (i = 0; i < AUDIO_USECASE_MAX; i++)
1668 switch_device[i] = false;
1669
1670 list_for_each(node, &adev->usecase_list) {
1671 usecase = node_to_item(node, struct audio_usecase, list);
Dhanalakshmi Siddanib678a802016-12-03 11:51:41 +05301672 /*
1673 * TODO: Enhance below condition to handle BT sco/USB multi recording
1674 */
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -08001675 if (usecase->type != PCM_PLAYBACK &&
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001676 usecase != uc_info &&
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05301677 (usecase->in_snd_device != snd_device || force_routing) &&
Dhanalakshmi Siddanib678a802016-12-03 11:51:41 +05301678 ((uc_info->devices & backend_check_cond) &&
Karthik Reddy Katta0d471762015-08-20 13:36:07 +05301679 (((usecase->devices & ~AUDIO_DEVICE_BIT_IN) & AUDIO_DEVICE_IN_ALL_CODEC_BACKEND) ||
Satya Krishna Pindiproli458b5a72017-03-13 15:14:24 +05301680 (usecase->type == VOIP_CALL))) &&
Carter Hsu1d2a0532018-10-04 09:24:36 +08001681 ((uc_info->type == VOICE_CALL &&
1682 usecase->devices == AUDIO_DEVICE_IN_VOICE_CALL) ||
1683 platform_check_backends_match(snd_device,\
1684 usecase->in_snd_device)) &&
Anish Kumara020a7c2014-10-17 11:13:22 -07001685 (usecase->id != USECASE_AUDIO_SPKR_CALIB_TX)) {
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001686 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
1687 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -07001688 platform_get_snd_device_name(usecase->in_snd_device));
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001689 disable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001690 switch_device[usecase->id] = true;
1691 num_uc_to_switch++;
1692 }
1693 }
1694
1695 if (num_uc_to_switch) {
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07001696 /* All streams have been de-routed. Disable the device */
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001697
Venkata Narendra Kumar Gutta7610e632014-04-14 23:16:38 +05301698 /* Make sure the previous devices to be disabled first and then enable the
1699 selected devices */
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001700 list_for_each(node, &adev->usecase_list) {
1701 usecase = node_to_item(node, struct audio_usecase, list);
1702 if (switch_device[usecase->id]) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001703 disable_snd_device(adev, usecase->in_snd_device);
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -08001704 }
1705 }
1706
1707 list_for_each(node, &adev->usecase_list) {
1708 usecase = node_to_item(node, struct audio_usecase, list);
1709 if (switch_device[usecase->id]) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001710 enable_snd_device(adev, snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001711 }
1712 }
1713
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001714 /* Re-route all the usecases on the shared backend other than the
1715 specified usecase to new snd devices */
1716 list_for_each(node, &adev->usecase_list) {
1717 usecase = node_to_item(node, struct audio_usecase, list);
1718 /* Update the in_snd_device only before enabling the audio route */
1719 if (switch_device[usecase->id] ) {
1720 usecase->in_snd_device = snd_device;
kunleiz5cd52b82016-11-07 17:22:52 +08001721 if (usecase->type != VOICE_CALL) {
1722 /* Update voc calibration before enabling VoIP route */
1723 if (usecase->type == VOIP_CALL)
1724 status = platform_switch_voice_call_device_post(adev->platform,
Zhou Song557e7282017-05-05 17:18:18 +08001725 platform_get_output_snd_device(adev->platform, uc_info->stream.out),
kunleiz5cd52b82016-11-07 17:22:52 +08001726 usecase->in_snd_device);
Avinash Vaish71a8b972014-07-24 15:36:33 +05301727 enable_audio_route(adev, usecase);
kunleiz5cd52b82016-11-07 17:22:52 +08001728 }
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001729 }
1730 }
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001731 }
1732}
1733
Mingming Yin3a941d42016-02-17 18:08:05 -08001734static void reset_hdmi_sink_caps(struct stream_out *out) {
1735 int i = 0;
1736
1737 for (i = 0; i<= MAX_SUPPORTED_CHANNEL_MASKS; i++) {
1738 out->supported_channel_masks[i] = 0;
1739 }
1740 for (i = 0; i<= MAX_SUPPORTED_FORMATS; i++) {
1741 out->supported_formats[i] = 0;
1742 }
1743 for (i = 0; i<= MAX_SUPPORTED_SAMPLE_RATES; i++) {
1744 out->supported_sample_rates[i] = 0;
1745 }
1746}
1747
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001748/* must be called with hw device mutex locked */
Mingming Yin3a941d42016-02-17 18:08:05 -08001749static int read_hdmi_sink_caps(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001750{
Mingming Yin3a941d42016-02-17 18:08:05 -08001751 int ret = 0, i = 0, j = 0;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07001752 int channels = platform_edid_get_max_channels(out->dev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001753
Mingming Yin3a941d42016-02-17 18:08:05 -08001754 reset_hdmi_sink_caps(out);
1755
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07001756 /* Cache ext disp type */
Garmond Leung37850ab2016-10-06 11:42:18 -07001757 if (platform_get_ext_disp_type(adev->platform) <= 0) {
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07001758 ALOGE("%s: Failed to query disp type, ret:%d", __func__, ret);
Garmond Leung37850ab2016-10-06 11:42:18 -07001759 return -EINVAL;
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07001760 }
1761
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001762 switch (channels) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001763 case 8:
Mingming Yin3a941d42016-02-17 18:08:05 -08001764 ALOGV("%s: HDMI supports 7.1 channels", __func__);
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -07001765 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_7POINT1;
Mingming Yin3a941d42016-02-17 18:08:05 -08001766 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_6POINT1;
1767 case 6:
1768 ALOGV("%s: HDMI supports 5.1 channels", __func__);
1769 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_5POINT1;
1770 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_PENTA;
1771 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_QUAD;
1772 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_SURROUND;
1773 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_2POINT1;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001774 break;
1775 default:
Mingming Yin3a941d42016-02-17 18:08:05 -08001776 ALOGE("invalid/nonstandard channal count[%d]",channels);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001777 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001778 break;
1779 }
Mingming Yin3a941d42016-02-17 18:08:05 -08001780
1781 // check channel format caps
1782 i = 0;
1783 if (platform_is_edid_supported_format(out->dev->platform, AUDIO_FORMAT_AC3)) {
1784 ALOGV(":%s HDMI supports AC3/EAC3 formats", __func__);
1785 out->supported_formats[i++] = AUDIO_FORMAT_AC3;
1786 //Adding EAC3/EAC3_JOC formats if AC3 is supported by the sink.
1787 //EAC3/EAC3_JOC will be converted to AC3 for decoding if needed
1788 out->supported_formats[i++] = AUDIO_FORMAT_E_AC3;
1789 out->supported_formats[i++] = AUDIO_FORMAT_E_AC3_JOC;
1790 }
1791
Ben Romberger1aaaf862017-04-06 17:49:46 -07001792 if (platform_is_edid_supported_format(out->dev->platform, AUDIO_FORMAT_DOLBY_TRUEHD)) {
1793 ALOGV(":%s HDMI supports TRUE HD format", __func__);
1794 out->supported_formats[i++] = AUDIO_FORMAT_DOLBY_TRUEHD;
1795 }
1796
Mingming Yin3a941d42016-02-17 18:08:05 -08001797 if (platform_is_edid_supported_format(out->dev->platform, AUDIO_FORMAT_DTS)) {
1798 ALOGV(":%s HDMI supports DTS format", __func__);
1799 out->supported_formats[i++] = AUDIO_FORMAT_DTS;
1800 }
1801
1802 if (platform_is_edid_supported_format(out->dev->platform, AUDIO_FORMAT_DTS_HD)) {
1803 ALOGV(":%s HDMI supports DTS HD format", __func__);
1804 out->supported_formats[i++] = AUDIO_FORMAT_DTS_HD;
1805 }
1806
Naresh Tanniru928f0862017-04-07 16:44:23 -07001807 if (platform_is_edid_supported_format(out->dev->platform, AUDIO_FORMAT_IEC61937)) {
1808 ALOGV(":%s HDMI supports IEC61937 format", __func__);
1809 out->supported_formats[i++] = AUDIO_FORMAT_IEC61937;
1810 }
1811
Mingming Yin3a941d42016-02-17 18:08:05 -08001812
1813 // check sample rate caps
1814 i = 0;
1815 for (j = 0; j < MAX_SUPPORTED_SAMPLE_RATES; j++) {
1816 if (platform_is_edid_supported_sample_rate(out->dev->platform, out_hdmi_sample_rates[j])) {
1817 ALOGV(":%s HDMI supports sample rate:%d", __func__, out_hdmi_sample_rates[j]);
1818 out->supported_sample_rates[i++] = out_hdmi_sample_rates[j];
1819 }
1820 }
1821
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001822 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001823}
1824
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001825static inline ssize_t read_usb_sup_sample_rates(bool is_playback __unused,
1826 uint32_t *supported_sample_rates __unused,
1827 uint32_t max_rates __unused)
1828{
1829 ssize_t count = audio_extn_usb_get_sup_sample_rates(is_playback,
1830 supported_sample_rates,
1831 max_rates);
Ashish Jain4847e9d2017-08-17 19:16:57 +05301832 ssize_t i = 0;
1833
1834 for (i=0; i<count; i++) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001835 ALOGV("%s %s %d", __func__, is_playback ? "P" : "C",
1836 supported_sample_rates[i]);
1837 }
1838 return count;
1839}
1840
1841static inline int read_usb_sup_channel_masks(bool is_playback,
1842 audio_channel_mask_t *supported_channel_masks,
1843 uint32_t max_masks)
1844{
1845 int channels = audio_extn_usb_get_max_channels(is_playback);
1846 int channel_count;
1847 uint32_t num_masks = 0;
1848 if (channels > MAX_HIFI_CHANNEL_COUNT)
1849 channels = MAX_HIFI_CHANNEL_COUNT;
1850
1851 if (is_playback) {
Eric Laurent68a87112019-05-01 18:07:29 -07001852 // start from 2 channels as framework currently doesn't support mono.
1853 if (channels >= FCC_2) {
1854 supported_channel_masks[num_masks++] = audio_channel_out_mask_from_count(FCC_2);
1855 }
1856 for (channel_count = FCC_2;
1857 channel_count <= channels && num_masks < max_masks;
1858 ++channel_count) {
1859 supported_channel_masks[num_masks++] =
1860 audio_channel_mask_for_index_assignment_from_count(channel_count);
1861 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001862 } else {
vincenttewf51c94e2019-05-07 10:28:53 +08001863 // For capture we report all supported channel masks from 1 channel up.
1864 channel_count = MIN_CHANNEL_COUNT;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001865 // audio_channel_in_mask_from_count() does the right conversion to either positional or
1866 // indexed mask
Eric Laurent68a87112019-05-01 18:07:29 -07001867 for ( ; channel_count <= channels && num_masks < max_masks; channel_count++) {
1868 audio_channel_mask_t mask = AUDIO_CHANNEL_NONE;
1869 if (channel_count <= FCC_2) {
1870 mask = audio_channel_in_mask_from_count(channel_count);
1871 supported_channel_masks[num_masks++] = mask;
1872 }
1873 const audio_channel_mask_t index_mask =
1874 audio_channel_mask_for_index_assignment_from_count(channel_count);
1875 if (mask != index_mask && num_masks < max_masks) { // ensure index mask added.
1876 supported_channel_masks[num_masks++] = index_mask;
1877 }
1878 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001879 }
Lakshman Chaluvarajub79fafd2018-11-21 10:24:37 +05301880
vincenttewf51c94e2019-05-07 10:28:53 +08001881 for (size_t i = 0; i < num_masks; ++i) {
1882 ALOGV("%s: %s supported ch %d supported_channel_masks[%zu] %08x num_masks %d", __func__,
1883 is_playback ? "P" : "C", channels, i, supported_channel_masks[i], num_masks);
Lakshman Chaluvarajub79fafd2018-11-21 10:24:37 +05301884 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001885 return num_masks;
1886}
1887
1888static inline int read_usb_sup_formats(bool is_playback __unused,
1889 audio_format_t *supported_formats,
1890 uint32_t max_formats __unused)
1891{
1892 int bitwidth = audio_extn_usb_get_max_bit_width(is_playback);
1893 switch (bitwidth) {
1894 case 24:
1895 // XXX : usb.c returns 24 for s24 and s24_le?
1896 supported_formats[0] = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1897 break;
1898 case 32:
1899 supported_formats[0] = AUDIO_FORMAT_PCM_32_BIT;
1900 break;
1901 case 16:
1902 default :
1903 supported_formats[0] = AUDIO_FORMAT_PCM_16_BIT;
1904 break;
1905 }
1906 ALOGV("%s: %s supported format %d", __func__,
1907 is_playback ? "P" : "C", bitwidth);
1908 return 1;
1909}
1910
1911static inline int read_usb_sup_params_and_compare(bool is_playback,
1912 audio_format_t *format,
1913 audio_format_t *supported_formats,
1914 uint32_t max_formats,
1915 audio_channel_mask_t *mask,
1916 audio_channel_mask_t *supported_channel_masks,
1917 uint32_t max_masks,
1918 uint32_t *rate,
1919 uint32_t *supported_sample_rates,
1920 uint32_t max_rates) {
1921 int ret = 0;
1922 int num_formats;
1923 int num_masks;
1924 int num_rates;
1925 int i;
1926
1927 num_formats = read_usb_sup_formats(is_playback, supported_formats,
1928 max_formats);
1929 num_masks = read_usb_sup_channel_masks(is_playback, supported_channel_masks,
1930 max_masks);
1931
1932 num_rates = read_usb_sup_sample_rates(is_playback,
1933 supported_sample_rates, max_rates);
1934
1935#define LUT(table, len, what, dflt) \
1936 for (i=0; i<len && (table[i] != what); i++); \
1937 if (i==len) { ret |= (what == dflt ? 0 : -1); what=table[0]; }
1938
1939 LUT(supported_formats, num_formats, *format, AUDIO_FORMAT_DEFAULT);
1940 LUT(supported_channel_masks, num_masks, *mask, AUDIO_CHANNEL_NONE);
1941 LUT(supported_sample_rates, num_rates, *rate, 0);
1942
1943#undef LUT
1944 return ret < 0 ? -EINVAL : 0; // HACK TBD
1945}
1946
Alexy Josephb1379942016-01-29 15:49:38 -08001947audio_usecase_t get_usecase_id_from_usecase_type(const struct audio_device *adev,
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -08001948 usecase_type_t type)
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001949{
1950 struct audio_usecase *usecase;
1951 struct listnode *node;
1952
1953 list_for_each(node, &adev->usecase_list) {
1954 usecase = node_to_item(node, struct audio_usecase, list);
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -08001955 if (usecase->type == type) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001956 ALOGV("%s: usecase id %d", __func__, usecase->id);
1957 return usecase->id;
1958 }
1959 }
1960 return USECASE_INVALID;
1961}
1962
Alexy Josephb1379942016-01-29 15:49:38 -08001963struct audio_usecase *get_usecase_from_list(const struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001964 audio_usecase_t uc_id)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001965{
1966 struct audio_usecase *usecase;
1967 struct listnode *node;
1968
1969 list_for_each(node, &adev->usecase_list) {
1970 usecase = node_to_item(node, struct audio_usecase, list);
1971 if (usecase->id == uc_id)
1972 return usecase;
1973 }
1974 return NULL;
1975}
1976
Dhananjay Kumard4833242016-10-06 22:09:12 +05301977struct stream_in *get_next_active_input(const struct audio_device *adev)
1978{
1979 struct audio_usecase *usecase;
1980 struct listnode *node;
1981
1982 list_for_each_reverse(node, &adev->usecase_list) {
1983 usecase = node_to_item(node, struct audio_usecase, list);
1984 if (usecase->type == PCM_CAPTURE)
1985 return usecase->stream.in;
1986 }
1987 return NULL;
1988}
1989
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301990/*
1991 * is a true native playback active
1992 */
1993bool audio_is_true_native_stream_active(struct audio_device *adev)
1994{
1995 bool active = false;
1996 int i = 0;
1997 struct listnode *node;
1998
1999 if (NATIVE_AUDIO_MODE_TRUE_44_1 != platform_get_native_support()) {
2000 ALOGV("%s:napb: not in true mode or non hdphones device",
2001 __func__);
2002 active = false;
2003 goto exit;
2004 }
2005
2006 list_for_each(node, &adev->usecase_list) {
2007 struct audio_usecase *uc;
2008 uc = node_to_item(node, struct audio_usecase, list);
2009 struct stream_out *curr_out =
2010 (struct stream_out*) uc->stream.out;
2011
2012 if (curr_out && PCM_PLAYBACK == uc->type) {
2013 ALOGD("%s:napb: (%d) (%s)id (%d) sr %d bw "
2014 "(%d) device %s", __func__, i++, use_case_table[uc->id],
2015 uc->id, curr_out->sample_rate,
2016 curr_out->bit_width,
2017 platform_get_snd_device_name(uc->out_snd_device));
2018
2019 if (is_offload_usecase(uc->id) &&
2020 (curr_out->sample_rate == OUTPUT_SAMPLING_RATE_44100)) {
2021 active = true;
2022 ALOGD("%s:napb:native stream detected", __func__);
2023 }
2024 }
2025 }
2026exit:
2027 return active;
2028}
2029
Xiaojun Sang785b5da2017-08-03 15:52:29 +08002030uint32_t adev_get_dsp_bit_width_enforce_mode()
2031{
2032 if (adev == NULL) {
2033 ALOGE("%s: adev is null. Disable DSP bit width enforce mode.\n", __func__);
2034 return 0;
2035 }
2036 return adev->dsp_bit_width_enforce_mode;
2037}
2038
2039static uint32_t adev_init_dsp_bit_width_enforce_mode(struct mixer *mixer)
2040{
2041 char value[PROPERTY_VALUE_MAX];
2042 int trial;
2043 uint32_t dsp_bit_width_enforce_mode = 0;
2044
2045 if (!mixer) {
2046 ALOGE("%s: adev mixer is null. cannot update DSP bitwidth.\n",
2047 __func__);
2048 return 0;
2049 }
2050
2051 if (property_get("persist.vendor.audio_hal.dsp_bit_width_enforce_mode",
2052 value, NULL) > 0) {
2053 trial = atoi(value);
2054 switch (trial) {
2055 case 16:
2056 dsp_bit_width_enforce_mode = 16;
2057 break;
2058 case 24:
2059 dsp_bit_width_enforce_mode = 24;
2060 break;
2061 case 32:
2062 dsp_bit_width_enforce_mode = 32;
2063 break;
2064 default:
2065 dsp_bit_width_enforce_mode = 0;
2066 ALOGD("%s Dynamic DSP bitwidth config is disabled.", __func__);
2067 break;
2068 }
2069 }
2070
2071 return dsp_bit_width_enforce_mode;
2072}
2073
2074static void audio_enable_asm_bit_width_enforce_mode(struct mixer *mixer,
2075 uint32_t enforce_mode,
2076 bool enable)
2077{
2078 struct mixer_ctl *ctl = NULL;
2079 const char *mixer_ctl_name = "ASM Bit Width";
2080 uint32_t asm_bit_width_mode = 0;
2081
2082 if (enforce_mode == 0) {
2083 ALOGD("%s: DSP bitwidth feature is disabled.", __func__);
2084 return;
2085 }
2086
2087 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
2088 if (!ctl) {
2089 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2090 __func__, mixer_ctl_name);
2091 return;
2092 }
2093
2094 if (enable)
2095 asm_bit_width_mode = enforce_mode;
2096 else
2097 asm_bit_width_mode = 0;
2098
2099 ALOGV("%s DSP bit width feature status is %d width=%d",
2100 __func__, enable, asm_bit_width_mode);
2101 if (mixer_ctl_set_value(ctl, 0, asm_bit_width_mode) < 0)
2102 ALOGE("%s: Could not set ASM biwidth %d", __func__,
2103 asm_bit_width_mode);
2104
2105 return;
2106}
2107
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05302108/*
2109 * if native DSD playback active
2110 */
2111bool audio_is_dsd_native_stream_active(struct audio_device *adev)
2112{
2113 bool active = false;
2114 struct listnode *node = NULL;
2115 struct audio_usecase *uc = NULL;
2116 struct stream_out *curr_out = NULL;
2117
2118 list_for_each(node, &adev->usecase_list) {
2119 uc = node_to_item(node, struct audio_usecase, list);
2120 curr_out = (struct stream_out*) uc->stream.out;
2121
2122 if (curr_out && PCM_PLAYBACK == uc->type &&
2123 (DSD_NATIVE_BACKEND == platform_get_backend_index(uc->out_snd_device))) {
2124 active = true;
2125 ALOGV("%s:DSD playback is active", __func__);
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05302126 break;
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05302127 }
2128 }
2129 return active;
2130}
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302131
2132static bool force_device_switch(struct audio_usecase *usecase)
2133{
2134 bool ret = false;
2135 bool is_it_true_mode = false;
2136
Zhou Song30f2c3e2018-02-08 14:02:15 +08002137 if (usecase->type == PCM_CAPTURE ||
Surendar Karka93cd25a2018-08-28 14:21:37 +05302138 usecase->type == TRANSCODE_LOOPBACK_RX ||
2139 usecase->type == TRANSCODE_LOOPBACK_TX) {
Zhou Song30f2c3e2018-02-08 14:02:15 +08002140 return false;
2141 }
2142
Aalique Grahamecbc46a22017-10-05 10:30:23 -07002143 if(usecase->stream.out == NULL) {
2144 ALOGE("%s: stream.out is NULL", __func__);
2145 return false;
2146 }
2147
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302148 if (is_offload_usecase(usecase->id) &&
Xiaojun Sang869f2012016-02-23 16:33:07 +08002149 (usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) &&
2150 (usecase->stream.out->devices == AUDIO_DEVICE_OUT_WIRED_HEADSET ||
2151 usecase->stream.out->devices == AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) {
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302152 is_it_true_mode = (NATIVE_AUDIO_MODE_TRUE_44_1 == platform_get_native_support()? true : false);
2153 if ((is_it_true_mode && !adev->native_playback_enabled) ||
2154 (!is_it_true_mode && adev->native_playback_enabled)){
2155 ret = true;
2156 ALOGD("napb: time to toggle native mode");
2157 }
2158 }
2159
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302160 // Force all a2dp output devices to reconfigure for proper AFE encode format
Ashish Jainc597d102016-12-12 10:31:34 +05302161 //Also handle a case where in earlier a2dp start failed as A2DP stream was
2162 //in suspended state, hence try to trigger a retry when we again get a routing request.
Aalique Grahamecbc46a22017-10-05 10:30:23 -07002163 if((usecase->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) &&
Ashish Jainc597d102016-12-12 10:31:34 +05302164 audio_extn_a2dp_is_force_device_switch()) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302165 ALOGD("Force a2dp device switch to update new encoder config");
2166 ret = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002167 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302168
Florian Pfister1a84f312018-07-19 14:38:18 +02002169 if (usecase->stream.out->stream_config_changed) {
Manish Dewangan671a4202017-08-18 17:30:46 +05302170 ALOGD("Force stream_config_changed to update iec61937 transmission config");
2171 return true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002172 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302173 return ret;
2174}
2175
Aalique Grahame22e49102018-12-18 14:23:57 -08002176static void stream_app_type_cfg_init(struct stream_app_type_cfg *cfg)
2177{
2178 cfg->gain[0] = cfg->gain[1] = APP_TYPE_GAIN_DEFAULT;
2179}
2180
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302181bool is_btsco_device(snd_device_t out_snd_device, snd_device_t in_snd_device)
2182{
2183 bool ret=false;
2184 if ((out_snd_device == SND_DEVICE_OUT_BT_SCO ||
2185 out_snd_device == SND_DEVICE_OUT_BT_SCO_WB) ||
2186 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_WB_NREC ||
2187 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_WB ||
2188 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_NREC ||
2189 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC)
2190 ret = true;
2191
2192 return ret;
2193}
2194
2195bool is_a2dp_device(snd_device_t out_snd_device)
2196{
2197 bool ret=false;
2198 if (out_snd_device == SND_DEVICE_OUT_BT_A2DP)
2199 ret = true;
2200
2201 return ret;
2202}
2203
2204bool is_bt_soc_on(struct audio_device *adev)
2205{
2206 struct mixer_ctl *ctl;
2207 char *mixer_ctl_name = "BT SOC status";
2208 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
2209 bool bt_soc_status = true;
2210 if (!ctl) {
2211 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2212 __func__, mixer_ctl_name);
2213 /*This is to ensure we dont break targets which dont have the kernel change*/
2214 return true;
2215 }
2216 bt_soc_status = mixer_ctl_get_value(ctl, 0);
2217 ALOGD("BT SOC status: %d",bt_soc_status);
2218 return bt_soc_status;
2219}
2220
2221int out_standby_l(struct audio_stream *stream);
2222
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002223int select_devices(struct audio_device *adev, audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002224{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002225 snd_device_t out_snd_device = SND_DEVICE_NONE;
2226 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002227 struct audio_usecase *usecase = NULL;
2228 struct audio_usecase *vc_usecase = NULL;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002229 struct audio_usecase *voip_usecase = NULL;
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -08002230 struct audio_usecase *hfp_usecase = NULL;
Siddartha Shaik44dd7702017-06-14 12:13:25 +05302231 struct stream_out stream_out;
Vimal Puthanveed41fcff22014-01-23 15:56:53 -08002232 audio_usecase_t hfp_ucid;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002233 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002234
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302235 ALOGD("%s for use case (%s)", __func__, use_case_table[uc_id]);
2236
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002237 usecase = get_usecase_from_list(adev, uc_id);
2238 if (usecase == NULL) {
2239 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
2240 return -EINVAL;
2241 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002242
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002243 if ((usecase->type == VOICE_CALL) ||
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -08002244 (usecase->type == VOIP_CALL) ||
2245 (usecase->type == PCM_HFP_CALL)) {
Aditya Bavanaribdda2f22016-10-19 15:02:05 +05302246 if(usecase->stream.out == NULL) {
2247 ALOGE("%s: stream.out is NULL", __func__);
2248 return -EINVAL;
2249 }
Eric Laurentb23d5282013-05-14 15:27:20 -07002250 out_snd_device = platform_get_output_snd_device(adev->platform,
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -08002251 usecase->stream.out);
Eric Laurentb23d5282013-05-14 15:27:20 -07002252 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002253 usecase->devices = usecase->stream.out->devices;
Surendar Karka93cd25a2018-08-28 14:21:37 +05302254 } else if (usecase->type == TRANSCODE_LOOPBACK_RX) {
Siddartha Shaik31b530e2017-05-19 15:26:33 +05302255 if (usecase->stream.inout == NULL) {
2256 ALOGE("%s: stream.inout is NULL", __func__);
2257 return -EINVAL;
2258 }
Siddartha Shaik44dd7702017-06-14 12:13:25 +05302259 stream_out.devices = usecase->stream.inout->out_config.devices;
2260 stream_out.sample_rate = usecase->stream.inout->out_config.sample_rate;
2261 stream_out.format = usecase->stream.inout->out_config.format;
2262 stream_out.channel_mask = usecase->stream.inout->out_config.channel_mask;
2263 out_snd_device = platform_get_output_snd_device(adev->platform,
2264 &stream_out);
Surendar Karka93cd25a2018-08-28 14:21:37 +05302265 usecase->devices = out_snd_device;
2266 } else if (usecase->type == TRANSCODE_LOOPBACK_TX ) {
2267 if (usecase->stream.inout == NULL) {
2268 ALOGE("%s: stream.inout is NULL", __func__);
2269 return -EINVAL;
2270 }
Siddartha Shaik44dd7702017-06-14 12:13:25 +05302271 in_snd_device = platform_get_input_snd_device(adev->platform, AUDIO_DEVICE_NONE);
Surendar Karka93cd25a2018-08-28 14:21:37 +05302272 usecase->devices = in_snd_device;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002273 } else {
2274 /*
2275 * If the voice call is active, use the sound devices of voice call usecase
2276 * so that it would not result any device switch. All the usecases will
2277 * be switched to new device when select_devices() is called for voice call
2278 * usecase. This is to avoid switching devices for voice call when
2279 * check_usecases_codec_backend() is called below.
Alexy Joseph79dfa3c2016-04-20 18:44:56 -07002280 * choose voice call device only if the use case device is
2281 * also using the codec backend
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002282 */
Shiv Maliyappanahallibb4cf0b2016-01-21 11:30:06 -08002283 if (voice_is_in_call(adev) && adev->mode != AUDIO_MODE_NORMAL) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002284 vc_usecase = get_usecase_from_list(adev,
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -08002285 get_usecase_id_from_usecase_type(adev, VOICE_CALL));
Alexy Josephdbcddf22016-05-16 17:54:09 -07002286 if ((vc_usecase) && (((vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) &&
2287 (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)) ||
Preetam Singh Ranawat097cb1f2016-07-19 13:49:04 +05302288 ((vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) &&
2289 (usecase->devices & AUDIO_DEVICE_IN_ALL_CODEC_BACKEND)) ||
juyuchen2d415992018-11-16 14:15:16 +08002290 (vc_usecase->devices == AUDIO_DEVICE_OUT_HEARING_AID) ||
2291 (usecase->devices == AUDIO_DEVICE_IN_VOICE_CALL))) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002292 in_snd_device = vc_usecase->in_snd_device;
2293 out_snd_device = vc_usecase->out_snd_device;
2294 }
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002295 } else if (voice_extn_compress_voip_is_active(adev)) {
yidongh02ef86f2017-04-21 15:36:04 +08002296 bool out_snd_device_backend_match = true;
yidongh47785a82017-05-08 19:29:29 +08002297 voip_usecase = get_usecase_from_list(adev, USECASE_COMPRESS_VOIP_CALL);
yidongh6261d8e2017-05-15 17:04:02 +08002298 if ((voip_usecase != NULL) &&
2299 (usecase->type == PCM_PLAYBACK) &&
2300 (usecase->stream.out != NULL)) {
yidongh02ef86f2017-04-21 15:36:04 +08002301 out_snd_device_backend_match = platform_check_backends_match(
2302 voip_usecase->out_snd_device,
2303 platform_get_output_snd_device(
2304 adev->platform,
2305 usecase->stream.out));
2306 }
Mingming Yin2d8aa2e2014-08-14 00:00:51 -07002307 if ((voip_usecase) && ((voip_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) &&
Zhou Song0154bf12016-08-04 10:48:16 +08002308 ((usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) ||
2309 ((usecase->devices & ~AUDIO_DEVICE_BIT_IN) & AUDIO_DEVICE_IN_ALL_CODEC_BACKEND)) &&
yidongh02ef86f2017-04-21 15:36:04 +08002310 out_snd_device_backend_match &&
Mingming Yin2d8aa2e2014-08-14 00:00:51 -07002311 (voip_usecase->stream.out != adev->primary_output))) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002312 in_snd_device = voip_usecase->in_snd_device;
2313 out_snd_device = voip_usecase->out_snd_device;
2314 }
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -08002315 } else if (audio_extn_hfp_is_active(adev)) {
Vimal Puthanveed41fcff22014-01-23 15:56:53 -08002316 hfp_ucid = audio_extn_hfp_get_usecase();
2317 hfp_usecase = get_usecase_from_list(adev, hfp_ucid);
Mingming Yin2d8aa2e2014-08-14 00:00:51 -07002318 if ((hfp_usecase) && (hfp_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)) {
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -08002319 in_snd_device = hfp_usecase->in_snd_device;
2320 out_snd_device = hfp_usecase->out_snd_device;
2321 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002322 }
2323 if (usecase->type == PCM_PLAYBACK) {
Aditya Bavanaribdda2f22016-10-19 15:02:05 +05302324 if (usecase->stream.out == NULL) {
2325 ALOGE("%s: stream.out is NULL", __func__);
2326 return -EINVAL;
2327 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002328 usecase->devices = usecase->stream.out->devices;
2329 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07002330 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurentb23d5282013-05-14 15:27:20 -07002331 out_snd_device = platform_get_output_snd_device(adev->platform,
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -08002332 usecase->stream.out);
kunleizdcf967a2018-08-07 17:09:11 +08002333 voip_usecase = get_usecase_from_list(adev, USECASE_AUDIO_PLAYBACK_VOIP);
kunleizb27e7952018-10-12 15:49:35 +08002334 if (voip_usecase == NULL && adev->primary_output && !adev->primary_output->standby)
kunleizdcf967a2018-08-07 17:09:11 +08002335 voip_usecase = get_usecase_from_list(adev, adev->primary_output->usecase);
2336
2337 if ((usecase->stream.out != NULL &&
2338 voip_usecase != NULL &&
2339 usecase->stream.out->usecase == voip_usecase->id) &&
2340 adev->active_input &&
Aalique Grahame22e49102018-12-18 14:23:57 -08002341 (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
2342 adev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
kunleizdcf967a2018-08-07 17:09:11 +08002343 out_snd_device != usecase->out_snd_device) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07002344 select_devices(adev, adev->active_input->usecase);
2345 }
2346 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002347 } else if (usecase->type == PCM_CAPTURE) {
Aditya Bavanaribdda2f22016-10-19 15:02:05 +05302348 if (usecase->stream.in == NULL) {
2349 ALOGE("%s: stream.in is NULL", __func__);
2350 return -EINVAL;
2351 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002352 usecase->devices = usecase->stream.in->device;
2353 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07002354 if (in_snd_device == SND_DEVICE_NONE) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07002355 audio_devices_t out_device = AUDIO_DEVICE_NONE;
Leena Winterrowd3c8fbae2016-01-12 15:05:53 -08002356 if (adev->active_input &&
2357 (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
Karthik Reddy Kattaf621ec72014-11-21 16:37:38 +05302358 (adev->mode == AUDIO_MODE_IN_COMMUNICATION &&
kunleizdcf967a2018-08-07 17:09:11 +08002359 adev->active_input->source == AUDIO_SOURCE_MIC))) {
2360 voip_usecase = get_usecase_from_list(adev, USECASE_AUDIO_PLAYBACK_VOIP);
2361 if (voip_usecase != NULL && voip_usecase->stream.out != NULL)
2362 out_device = voip_usecase->stream.out->devices;
2363 else if (adev->primary_output && !adev->primary_output->standby)
2364 out_device = adev->primary_output->devices;
Apoorv Raghuvanshi924b3022015-07-06 15:07:14 -07002365 platform_set_echo_reference(adev, false, AUDIO_DEVICE_NONE);
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07002366 } else if (usecase->id == USECASE_AUDIO_RECORD_AFE_PROXY) {
2367 out_device = AUDIO_DEVICE_OUT_TELEPHONY_TX;
juyuchen2d415992018-11-16 14:15:16 +08002368 } else {
2369 /* forcing speaker o/p device to get matching i/p pair
2370 in case o/p is not routed from same primary HAL */
2371 out_device = AUDIO_DEVICE_OUT_SPEAKER;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07002372 }
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07002373 in_snd_device = platform_get_input_snd_device(adev->platform, out_device);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07002374 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002375 }
2376 }
2377
2378 if (out_snd_device == usecase->out_snd_device &&
2379 in_snd_device == usecase->in_snd_device) {
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302380
2381 if (!force_device_switch(usecase))
2382 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002383 }
2384
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302385 if ((is_btsco_device(out_snd_device,in_snd_device) && !adev->bt_sco_on) ||
Florian Pfister1a84f312018-07-19 14:38:18 +02002386 (is_a2dp_device(out_snd_device) && !audio_extn_a2dp_source_is_ready())) {
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302387 ALOGD("SCO/A2DP is selected but they are not connected/ready hence dont route");
2388 return 0;
2389 }
2390
Aalique Grahame22e49102018-12-18 14:23:57 -08002391 if (out_snd_device != SND_DEVICE_NONE &&
2392 out_snd_device != adev->last_logged_snd_device[uc_id][0]) {
2393 ALOGD("%s: changing use case %s output device from(%d: %s, acdb %d) to (%d: %s, acdb %d)",
2394 __func__,
2395 use_case_table[uc_id],
2396 adev->last_logged_snd_device[uc_id][0],
2397 platform_get_snd_device_name(adev->last_logged_snd_device[uc_id][0]),
2398 adev->last_logged_snd_device[uc_id][0] != SND_DEVICE_NONE ?
2399 platform_get_snd_device_acdb_id(adev->last_logged_snd_device[uc_id][0]) :
2400 -1,
2401 out_snd_device,
2402 platform_get_snd_device_name(out_snd_device),
2403 platform_get_snd_device_acdb_id(out_snd_device));
2404 adev->last_logged_snd_device[uc_id][0] = out_snd_device;
2405 }
2406 if (in_snd_device != SND_DEVICE_NONE &&
2407 in_snd_device != adev->last_logged_snd_device[uc_id][1]) {
2408 ALOGD("%s: changing use case %s input device from(%d: %s, acdb %d) to (%d: %s, acdb %d)",
2409 __func__,
2410 use_case_table[uc_id],
2411 adev->last_logged_snd_device[uc_id][1],
2412 platform_get_snd_device_name(adev->last_logged_snd_device[uc_id][1]),
2413 adev->last_logged_snd_device[uc_id][1] != SND_DEVICE_NONE ?
2414 platform_get_snd_device_acdb_id(adev->last_logged_snd_device[uc_id][1]) :
2415 -1,
2416 in_snd_device,
2417 platform_get_snd_device_name(in_snd_device),
2418 platform_get_snd_device_acdb_id(in_snd_device));
2419 adev->last_logged_snd_device[uc_id][1] = in_snd_device;
2420 }
2421
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002422
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002423 /*
2424 * Limitation: While in call, to do a device switch we need to disable
2425 * and enable both RX and TX devices though one of them is same as current
2426 * device.
2427 */
Vidyakumar Athota21b3bb92014-04-25 11:08:08 -07002428 if ((usecase->type == VOICE_CALL) &&
2429 (usecase->in_snd_device != SND_DEVICE_NONE) &&
2430 (usecase->out_snd_device != SND_DEVICE_NONE)) {
Eric Laurentb23d5282013-05-14 15:27:20 -07002431 status = platform_switch_voice_call_device_pre(adev->platform);
Narsinga Rao Chella116142b2015-08-14 18:00:08 -07002432 }
2433
2434 if (((usecase->type == VOICE_CALL) ||
2435 (usecase->type == VOIP_CALL)) &&
2436 (usecase->out_snd_device != SND_DEVICE_NONE)) {
2437 /* Disable sidetone only if voice/voip call already exists */
2438 if (voice_is_call_state_active(adev) ||
2439 voice_extn_compress_voip_is_started(adev))
Bhalchandra Gajare45fee282015-06-09 22:23:45 -07002440 voice_set_sidetone(adev, usecase->out_snd_device, false);
Vidyakumar Athotaea269c62016-10-31 09:05:59 -07002441
2442 /* Disable aanc only if voice call exists */
2443 if (voice_is_call_state_active(adev))
2444 voice_check_and_update_aanc_path(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002445 }
2446
Aalique Grahame22e49102018-12-18 14:23:57 -08002447 if ((out_snd_device == SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP ||
2448 out_snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP) &&
Florian Pfister1a84f312018-07-19 14:38:18 +02002449 (!audio_extn_a2dp_source_is_ready())) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302450 ALOGW("%s: A2DP profile is not ready, routing to speaker only", __func__);
Aalique Grahame22e49102018-12-18 14:23:57 -08002451 if (out_snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP)
2452 out_snd_device = SND_DEVICE_OUT_SPEAKER_SAFE;
2453 else
2454 out_snd_device = SND_DEVICE_OUT_SPEAKER;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302455 }
2456
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002457 /* Disable current sound devices */
2458 if (usecase->out_snd_device != SND_DEVICE_NONE) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07002459 disable_audio_route(adev, usecase);
2460 disable_snd_device(adev, usecase->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002461 }
2462
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002463 if (usecase->in_snd_device != SND_DEVICE_NONE) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07002464 disable_audio_route(adev, usecase);
2465 disable_snd_device(adev, usecase->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002466 }
2467
Vidyakumar Athota545dbd32013-11-13 17:30:53 -08002468 /* Applicable only on the targets that has external modem.
2469 * New device information should be sent to modem before enabling
2470 * the devices to reduce in-call device switch time.
2471 */
Vidyakumar Athota21b3bb92014-04-25 11:08:08 -07002472 if ((usecase->type == VOICE_CALL) &&
2473 (usecase->in_snd_device != SND_DEVICE_NONE) &&
2474 (usecase->out_snd_device != SND_DEVICE_NONE)) {
Vidyakumar Athota545dbd32013-11-13 17:30:53 -08002475 status = platform_switch_voice_call_enable_device_config(adev->platform,
2476 out_snd_device,
2477 in_snd_device);
Vidyakumar Athota21b3bb92014-04-25 11:08:08 -07002478 }
Vidyakumar Athota545dbd32013-11-13 17:30:53 -08002479
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002480 /* Enable new sound devices */
2481 if (out_snd_device != SND_DEVICE_NONE) {
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08002482 check_usecases_codec_backend(adev, usecase, out_snd_device);
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +05302483 if (platform_check_codec_asrc_support(adev->platform))
2484 check_and_set_asrc_mode(adev, usecase, out_snd_device);
Haynes Mathew George1376ca62014-04-24 11:55:48 -07002485 enable_snd_device(adev, out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002486 }
2487
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002488 if (in_snd_device != SND_DEVICE_NONE) {
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05302489 check_usecases_capture_codec_backend(adev, usecase, in_snd_device);
Haynes Mathew George1376ca62014-04-24 11:55:48 -07002490 enable_snd_device(adev, in_snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002491 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002492
Avinash Vaish71a8b972014-07-24 15:36:33 +05302493 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) {
Eric Laurentb23d5282013-05-14 15:27:20 -07002494 status = platform_switch_voice_call_device_post(adev->platform,
2495 out_snd_device,
2496 in_snd_device);
Avinash Vaish71a8b972014-07-24 15:36:33 +05302497 enable_audio_route_for_voice_usecases(adev, usecase);
2498 }
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002499
sangwoo170731f2013-06-08 15:36:36 +09002500 usecase->in_snd_device = in_snd_device;
2501 usecase->out_snd_device = out_snd_device;
2502
Dhananjay Kumard6d32152016-10-13 16:11:03 +05302503 audio_extn_utils_update_stream_app_type_cfg_for_usecase(adev,
2504 usecase);
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +05302505 if (usecase->type == PCM_PLAYBACK) {
Weiyin Jiang6f4c8062016-11-23 15:30:29 +08002506 if ((24 == usecase->stream.out->bit_width) &&
2507 (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER)) {
2508 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
2509 } else if ((out_snd_device == SND_DEVICE_OUT_HDMI ||
2510 out_snd_device == SND_DEVICE_OUT_USB_HEADSET ||
2511 out_snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
2512 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
2513 /*
2514 * To best utlize DSP, check if the stream sample rate is supported/multiple of
2515 * configured device sample rate, if not update the COPP rate to be equal to the
2516 * device sample rate, else open COPP at stream sample rate
2517 */
2518 platform_check_and_update_copp_sample_rate(adev->platform, out_snd_device,
2519 usecase->stream.out->sample_rate,
2520 &usecase->stream.out->app_type_cfg.sample_rate);
Ashish Jain4826f6c2017-02-06 13:33:20 +05302521 } else if (((out_snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
2522 !audio_is_true_native_stream_active(adev)) &&
Weiyin Jiang6f4c8062016-11-23 15:30:29 +08002523 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
2524 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
2525 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
2526 }
2527
Weiyin Jiangfa65d3e2019-03-05 23:39:45 +08002528 /* Notify device change info to effect clients registered */
2529 audio_extn_gef_notify_device_config(
2530 usecase->stream.out->devices,
2531 usecase->stream.out->channel_mask,
2532 usecase->stream.out->app_type_cfg.sample_rate,
2533 platform_get_snd_device_acdb_id(usecase->out_snd_device));
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +05302534 }
Haynes Mathew George1376ca62014-04-24 11:55:48 -07002535 enable_audio_route(adev, usecase);
sangwoo170731f2013-06-08 15:36:36 +09002536
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002537 audio_extn_qdsp_set_device(usecase);
Aalique Grahame22e49102018-12-18 14:23:57 -08002538
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07002539 /* If input stream is already running then effect needs to be
2540 applied on the new input device that's being enabled here. */
Vidyakumar Athotabbfa5f72018-07-05 18:04:48 -07002541 if ((in_snd_device != SND_DEVICE_NONE) && (adev->active_input != NULL) &&
2542 (!adev->active_input->standby))
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07002543 check_and_enable_effect(adev);
2544
Vidyakumar Athota493f2892016-08-14 11:56:55 -07002545 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) {
Vidyakumar Athotaea269c62016-10-31 09:05:59 -07002546 /* Enable aanc only if voice call exists */
2547 if (voice_is_call_state_active(adev))
2548 voice_check_and_update_aanc_path(adev, out_snd_device, true);
2549
Vidyakumar Athota493f2892016-08-14 11:56:55 -07002550 /* Enable sidetone only if other voice/voip call already exists */
2551 if (voice_is_call_state_active(adev) ||
2552 voice_extn_compress_voip_is_started(adev))
2553 voice_set_sidetone(adev, out_snd_device, true);
2554 }
2555
Vidyakumar Athota1fd21792013-11-15 14:50:57 -08002556 /* Applicable only on the targets that has external modem.
2557 * Enable device command should be sent to modem only after
2558 * enabling voice call mixer controls
2559 */
Vidyakumar Athota339342f2014-07-01 15:30:57 -07002560 if (usecase->type == VOICE_CALL)
Vidyakumar Athota1fd21792013-11-15 14:50:57 -08002561 status = platform_switch_voice_call_usecase_route_post(adev->platform,
2562 out_snd_device,
2563 in_snd_device);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302564
2565 if (is_btsco_device(out_snd_device, in_snd_device) || is_a2dp_device(out_snd_device)) {
2566
2567 if (usecase->type == VOIP_CALL) {
2568 if (adev->active_input != NULL &&
2569 !adev->active_input->standby) {
2570 if (is_bt_soc_on(adev) == false){
2571 ALOGD("BT SCO MIC disconnected while in connection");
2572 if (adev->active_input->pcm != NULL)
2573 pcm_stop(adev->active_input->pcm);
2574 }
2575 }
2576 if ((usecase->stream.out != NULL) && (usecase->stream.out != adev->primary_output)
2577 && usecase->stream.out->started) {
2578 if (is_bt_soc_on(adev) == false) {
2579 ALOGD("BT SCO/A2DP disconnected while in connection");
2580 out_standby_l(&usecase->stream.out->stream.common);
2581 }
2582 }
2583 } else if ((usecase->stream.out != NULL) &&
2584 !(usecase->stream.out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
Surendar Karkae1dc8742018-11-19 16:23:14 +05302585 (usecase->type != TRANSCODE_LOOPBACK_TX) &&
2586 (usecase->type != TRANSCODE_LOOPBACK_RX) &&
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302587 usecase->stream.out->started) {
2588 if (is_bt_soc_on(adev) == false) {
2589 ALOGD("BT SCO/A2dp disconnected while in connection");
2590 out_standby_l(&usecase->stream.out->stream.common);
2591 }
2592 }
2593 }
2594
Yung Ti Su70cb8242018-06-22 17:38:47 +08002595 if (usecase->type != PCM_CAPTURE && usecase == voip_usecase) {
Aalique Grahame22e49102018-12-18 14:23:57 -08002596 struct stream_out *voip_out = voip_usecase->stream.out;
2597 audio_extn_utils_send_app_type_gain(adev,
2598 voip_out->app_type_cfg.app_type,
2599 &voip_out->app_type_cfg.gain[0]);
2600 }
2601
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302602 ALOGD("%s: done",__func__);
2603
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002604 return status;
2605}
2606
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002607static int stop_input_stream(struct stream_in *in)
2608{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05302609 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002610 struct audio_usecase *uc_info;
Pallavid7c7a272018-01-16 11:22:55 +05302611
2612 if (in == NULL) {
2613 ALOGE("%s: stream_in ptr is NULL", __func__);
2614 return -EINVAL;
2615 }
2616
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002617 struct audio_device *adev = in->dev;
2618
Eric Laurent994a6932013-07-17 11:51:42 -07002619 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002620 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002621 uc_info = get_usecase_from_list(adev, in->usecase);
2622 if (uc_info == NULL) {
2623 ALOGE("%s: Could not find the usecase (%d) in the list",
2624 __func__, in->usecase);
2625 return -EINVAL;
2626 }
2627
Derek Chenea197282019-01-07 17:35:01 -08002628 if (audio_extn_ext_hw_plugin_usecase_stop(adev->ext_hw_plugin, uc_info))
2629 ALOGE("%s: failed to stop ext hw plugin", __func__);
Derek Chend2530072014-11-24 12:39:14 -08002630
Vidyakumar Athota2850d532013-11-19 16:02:12 -08002631 /* Close in-call recording streams */
2632 voice_check_and_stop_incall_rec_usecase(adev, in);
2633
Eric Laurent150dbfe2013-02-27 14:31:02 -08002634 /* 1. Disable stream specific mixer controls */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07002635 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002636
2637 /* 2. Disable the tx device */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07002638 disable_snd_device(adev, uc_info->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002639
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002640 list_remove(&uc_info->list);
2641 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002642
Aalique Grahame2e9b2e42016-12-07 12:43:48 -08002643 adev->active_input = get_next_active_input(adev);
Vatsal Buchac09ae062018-11-14 13:25:08 +05302644 enable_gcov();
Eric Laurent994a6932013-07-17 11:51:42 -07002645 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002646 return ret;
2647}
2648
2649int start_input_stream(struct stream_in *in)
2650{
2651 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -08002652 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002653 struct audio_usecase *uc_info;
Preetam Singh Ranawata87e9742018-02-13 16:52:53 +05302654
2655 if (in == NULL) {
2656 ALOGE("%s: stream_in ptr is NULL", __func__);
2657 return -EINVAL;
2658 }
2659
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002660 struct audio_device *adev = in->dev;
Garmond Leunge2433c32017-09-28 21:51:22 -07002661 struct pcm_config config = in->config;
Garmond Leung438932f2017-10-04 19:35:18 -07002662 int usecase = platform_update_usecase_from_source(in->source,in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002663
Mingming Yin2664a5b2015-09-03 10:53:11 -07002664 if (get_usecase_from_list(adev, usecase) == NULL)
2665 in->usecase = usecase;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302666 ALOGD("%s: enter: stream(%p)usecase(%d: %s)",
2667 __func__, &in->stream, in->usecase, use_case_table[in->usecase]);
Shiv Maliyappanahallida107642013-10-17 11:16:13 -07002668
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05302669 if (CARD_STATUS_OFFLINE == in->card_status||
2670 CARD_STATUS_OFFLINE == adev->card_status) {
2671 ALOGW("in->card_status or adev->card_status offline, try again");
Dhanalakshmi Siddani4d57e992014-07-17 16:37:51 +05302672 ret = -EIO;
Naresh Tanniru4c630392014-05-12 01:05:52 +05302673 goto error_config;
2674 }
Naresh Tanniru4c630392014-05-12 01:05:52 +05302675
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302676 if (audio_is_bluetooth_sco_device(in->device)) {
2677 if (!adev->bt_sco_on) {
2678 ALOGE("%s: SCO profile is not ready, return error", __func__);
2679 ret = -EIO;
2680 goto error_config;
2681 }
2682 }
2683
Shiv Maliyappanahallida107642013-10-17 11:16:13 -07002684 /* Check if source matches incall recording usecase criteria */
2685 ret = voice_check_and_set_incall_rec_usecase(adev, in);
2686 if (ret)
2687 goto error_config;
2688 else
Mingming Yin2664a5b2015-09-03 10:53:11 -07002689 ALOGV("%s: usecase(%d)", __func__, in->usecase);
2690
2691 if (get_usecase_from_list(adev, in->usecase) != NULL) {
2692 ALOGE("%s: use case assigned already in use, stream(%p)usecase(%d: %s)",
2693 __func__, &in->stream, in->usecase, use_case_table[in->usecase]);
Zhou Song4e0704d2016-04-20 13:00:14 +08002694 return -EINVAL;
Mingming Yin2664a5b2015-09-03 10:53:11 -07002695 }
Shiv Maliyappanahallida107642013-10-17 11:16:13 -07002696
Eric Laurentb23d5282013-05-14 15:27:20 -07002697 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002698 if (in->pcm_device_id < 0) {
2699 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
2700 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -08002701 ret = -EINVAL;
2702 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002703 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002704
2705 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002706 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07002707
2708 if (!uc_info) {
2709 ret = -ENOMEM;
2710 goto error_config;
2711 }
2712
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002713 uc_info->id = in->usecase;
2714 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002715 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002716 uc_info->devices = in->device;
2717 uc_info->in_snd_device = SND_DEVICE_NONE;
2718 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002719
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002720 list_add_tail(&adev->usecase_list, &uc_info->list);
Wei Wangf7ca6c92017-11-21 14:51:20 -08002721 audio_streaming_hint_start();
Sudheer Papothifa9d2282015-09-17 01:53:25 +05302722 audio_extn_perf_lock_acquire(&adev->perf_lock_handle, 0,
2723 adev->perf_lock_opts,
2724 adev->perf_lock_opts_size);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002725 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002726
Derek Chenea197282019-01-07 17:35:01 -08002727 if (audio_extn_ext_hw_plugin_usecase_start(adev->ext_hw_plugin, uc_info))
2728 ALOGE("%s: failed to start ext hw plugin", __func__);
Derek Chend2530072014-11-24 12:39:14 -08002729
Haynes Mathew George16081042017-05-31 17:16:49 -07002730 if (audio_extn_cin_attached_usecase(in->usecase)) {
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05302731 ret = audio_extn_cin_start_input_stream(in);
2732 if (ret)
2733 goto error_open;
2734 else
2735 goto done_open;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07002736 }
2737
Haynes Mathew George16081042017-05-31 17:16:49 -07002738 if (in->usecase == USECASE_AUDIO_RECORD_MMAP) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07002739 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) {
Haynes Mathew George16081042017-05-31 17:16:49 -07002740 ALOGE("%s: pcm stream not ready", __func__);
2741 goto error_open;
2742 }
2743 ret = pcm_start(in->pcm);
2744 if (ret < 0) {
2745 ALOGE("%s: MMAP pcm_start failed ret %d", __func__, ret);
2746 goto error_open;
2747 }
2748 } else {
2749 unsigned int flags = PCM_IN | PCM_MONOTONIC;
2750 unsigned int pcm_open_retry_count = 0;
2751
2752 if (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY) {
2753 flags |= PCM_MMAP | PCM_NOIRQ;
2754 pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT;
2755 } else if (in->realtime) {
2756 flags |= PCM_MMAP | PCM_NOIRQ;
2757 }
2758
Garmond Leunge2433c32017-09-28 21:51:22 -07002759 if (audio_extn_ffv_get_stream() == in) {
2760 ALOGD("%s: ffv stream, update pcm config", __func__);
2761 audio_extn_ffv_update_pcm_config(&config);
2762 }
Haynes Mathew George16081042017-05-31 17:16:49 -07002763 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
2764 __func__, adev->snd_card, in->pcm_device_id, in->config.channels);
2765
2766 while (1) {
Haynes Mathew George380745d2017-10-04 15:27:45 -07002767 ATRACE_BEGIN("pcm_in_open");
Haynes Mathew George16081042017-05-31 17:16:49 -07002768 in->pcm = pcm_open(adev->snd_card, in->pcm_device_id,
Garmond Leung438932f2017-10-04 19:35:18 -07002769 flags, &config);
Haynes Mathew George380745d2017-10-04 15:27:45 -07002770 ATRACE_END();
Satish Babu Patakokila54ce83d2018-07-06 18:00:37 +05302771 if (errno == ENETRESET && !pcm_is_ready(in->pcm)) {
Sharad Sanglec6f32552018-05-04 16:15:38 +05302772 ALOGE("%s: pcm_open failed errno:%d\n", __func__, errno);
2773 adev->card_status = CARD_STATUS_OFFLINE;
2774 in->card_status = CARD_STATUS_OFFLINE;
2775 ret = -EIO;
2776 goto error_open;
2777 }
2778
Haynes Mathew George16081042017-05-31 17:16:49 -07002779 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) {
2780 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
2781 if (in->pcm != NULL) {
2782 pcm_close(in->pcm);
2783 in->pcm = NULL;
2784 }
2785 if (pcm_open_retry_count-- == 0) {
2786 ret = -EIO;
2787 goto error_open;
2788 }
2789 usleep(PROXY_OPEN_WAIT_TIME * 1000);
2790 continue;
2791 }
2792 break;
2793 }
2794
2795 ALOGV("%s: pcm_prepare", __func__);
Haynes Mathew George380745d2017-10-04 15:27:45 -07002796 ATRACE_BEGIN("pcm_in_prepare");
Haynes Mathew George16081042017-05-31 17:16:49 -07002797 ret = pcm_prepare(in->pcm);
Haynes Mathew George380745d2017-10-04 15:27:45 -07002798 ATRACE_END();
Haynes Mathew George16081042017-05-31 17:16:49 -07002799 if (ret < 0) {
2800 ALOGE("%s: pcm_prepare returned %d", __func__, ret);
2801 pcm_close(in->pcm);
2802 in->pcm = NULL;
2803 goto error_open;
2804 }
2805 register_in_stream(in);
2806 if (in->realtime) {
Haynes Mathew George380745d2017-10-04 15:27:45 -07002807 ATRACE_BEGIN("pcm_in_start");
Haynes Mathew George16081042017-05-31 17:16:49 -07002808 ret = pcm_start(in->pcm);
Haynes Mathew George380745d2017-10-04 15:27:45 -07002809 ATRACE_END();
Haynes Mathew George16081042017-05-31 17:16:49 -07002810 if (ret < 0) {
2811 ALOGE("%s: RT pcm_start failed ret %d", __func__, ret);
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07002812 pcm_close(in->pcm);
2813 in->pcm = NULL;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07002814 goto error_open;
2815 }
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07002816 }
Haynes Mathew George5beddd42016-06-27 18:33:40 -07002817 }
2818
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07002819 check_and_enable_effect(adev);
justinweng20fb6d82019-02-21 18:49:00 -07002820 audio_extn_audiozoom_set_microphone_direction(in, in->zoom);
2821 audio_extn_audiozoom_set_microphone_field_dimension(in, in->direction);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07002822
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05302823done_open:
Wei Wangf7ca6c92017-11-21 14:51:20 -08002824 audio_streaming_hint_end();
Sudheer Papothifa9d2282015-09-17 01:53:25 +05302825 audio_extn_perf_lock_release(&adev->perf_lock_handle);
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07002826 ALOGD("%s: exit", __func__);
Vatsal Buchac09ae062018-11-14 13:25:08 +05302827 enable_gcov();
Eric Laurentc8400632013-02-14 19:04:54 -08002828 return ret;
2829
2830error_open:
Wei Wangf7ca6c92017-11-21 14:51:20 -08002831 audio_streaming_hint_end();
Sudheer Papothifa9d2282015-09-17 01:53:25 +05302832 audio_extn_perf_lock_release(&adev->perf_lock_handle);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002833 stop_input_stream(in);
Wei Wangf7ca6c92017-11-21 14:51:20 -08002834
Eric Laurentc8400632013-02-14 19:04:54 -08002835error_config:
Dhananjay Kumard4833242016-10-06 22:09:12 +05302836 adev->active_input = get_next_active_input(adev);
Laxminath Kasam2cb4b752015-09-24 03:59:15 +05302837 /*
2838 * sleep 50ms to allow sufficient time for kernel
2839 * drivers to recover incases like SSR.
2840 */
2841 usleep(50000);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002842 ALOGD("%s: exit: status(%d)", __func__, ret);
Vatsal Buchac09ae062018-11-14 13:25:08 +05302843 enable_gcov();
Eric Laurentc8400632013-02-14 19:04:54 -08002844 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002845}
2846
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07002847void lock_input_stream(struct stream_in *in)
2848{
2849 pthread_mutex_lock(&in->pre_lock);
2850 pthread_mutex_lock(&in->lock);
2851 pthread_mutex_unlock(&in->pre_lock);
2852}
2853
2854void lock_output_stream(struct stream_out *out)
2855{
2856 pthread_mutex_lock(&out->pre_lock);
2857 pthread_mutex_lock(&out->lock);
2858 pthread_mutex_unlock(&out->pre_lock);
2859}
2860
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002861/* must be called with out->lock locked */
2862static int send_offload_cmd_l(struct stream_out* out, int command)
2863{
2864 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
2865
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07002866 if (!cmd) {
2867 ALOGE("failed to allocate mem for command 0x%x", command);
2868 return -ENOMEM;
2869 }
2870
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002871 ALOGVV("%s %d", __func__, command);
2872
2873 cmd->cmd = command;
2874 list_add_tail(&out->offload_cmd_list, &cmd->node);
2875 pthread_cond_signal(&out->offload_cond);
2876 return 0;
2877}
2878
2879/* must be called iwth out->lock locked */
2880static void stop_compressed_output_l(struct stream_out *out)
2881{
2882 out->offload_state = OFFLOAD_STATE_IDLE;
2883 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002884 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002885 if (out->compr != NULL) {
2886 compress_stop(out->compr);
2887 while (out->offload_thread_blocked) {
2888 pthread_cond_wait(&out->cond, &out->lock);
2889 }
2890 }
2891}
2892
Varun Balaraje49253e2017-07-06 19:48:56 +05302893bool is_interactive_usecase(audio_usecase_t uc_id)
2894{
2895 unsigned int i;
2896 for (i = 0; i < sizeof(interactive_usecases)/sizeof(interactive_usecases[0]); i++) {
2897 if (uc_id == interactive_usecases[i])
2898 return true;
2899 }
2900 return false;
2901}
2902
2903static audio_usecase_t get_interactive_usecase(struct audio_device *adev)
2904{
2905 audio_usecase_t ret_uc = USECASE_INVALID;
2906 unsigned int intract_uc_index;
2907 unsigned int num_usecase = sizeof(interactive_usecases)/sizeof(interactive_usecases[0]);
2908
2909 ALOGV("%s: num_usecase: %d", __func__, num_usecase);
2910 for (intract_uc_index = 0; intract_uc_index < num_usecase; intract_uc_index++) {
2911 if (!(adev->interactive_usecase_state & (0x1 << intract_uc_index))) {
2912 adev->interactive_usecase_state |= 0x1 << intract_uc_index;
2913 ret_uc = interactive_usecases[intract_uc_index];
2914 break;
2915 }
2916 }
2917
2918 ALOGV("%s: Interactive usecase is %d", __func__, ret_uc);
2919 return ret_uc;
2920}
2921
2922static void free_interactive_usecase(struct audio_device *adev,
2923 audio_usecase_t uc_id)
2924{
2925 unsigned int interact_uc_index;
2926 unsigned int num_usecase = sizeof(interactive_usecases)/sizeof(interactive_usecases[0]);
2927
2928 for (interact_uc_index = 0; interact_uc_index < num_usecase; interact_uc_index++) {
2929 if (interactive_usecases[interact_uc_index] == uc_id) {
2930 adev->interactive_usecase_state &= ~(0x1 << interact_uc_index);
2931 break;
2932 }
2933 }
2934 ALOGV("%s: free Interactive usecase %d", __func__, uc_id);
2935}
2936
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002937bool is_offload_usecase(audio_usecase_t uc_id)
2938{
2939 unsigned int i;
2940 for (i = 0; i < sizeof(offload_usecases)/sizeof(offload_usecases[0]); i++) {
2941 if (uc_id == offload_usecases[i])
2942 return true;
2943 }
2944 return false;
2945}
2946
Dhananjay Kumarac341582017-02-23 23:42:25 +05302947static audio_usecase_t get_offload_usecase(struct audio_device *adev, bool is_compress)
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002948{
vivek mehta446c3962015-09-14 10:57:35 -07002949 audio_usecase_t ret_uc = USECASE_INVALID;
2950 unsigned int offload_uc_index;
Alexy Josephb1379942016-01-29 15:49:38 -08002951 unsigned int num_usecase = sizeof(offload_usecases)/sizeof(offload_usecases[0]);
vivek mehta446c3962015-09-14 10:57:35 -07002952 if (!adev->multi_offload_enable) {
Dhananjay Kumarac341582017-02-23 23:42:25 +05302953 if (!is_compress)
vivek mehta446c3962015-09-14 10:57:35 -07002954 ret_uc = USECASE_AUDIO_PLAYBACK_OFFLOAD2;
2955 else
2956 ret_uc = USECASE_AUDIO_PLAYBACK_OFFLOAD;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002957
vivek mehta446c3962015-09-14 10:57:35 -07002958 pthread_mutex_lock(&adev->lock);
2959 if (get_usecase_from_list(adev, ret_uc) != NULL)
2960 ret_uc = USECASE_INVALID;
2961 pthread_mutex_unlock(&adev->lock);
2962
2963 return ret_uc;
2964 }
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002965
2966 ALOGV("%s: num_usecase: %d", __func__, num_usecase);
vivek mehta446c3962015-09-14 10:57:35 -07002967 for (offload_uc_index = 0; offload_uc_index < num_usecase; offload_uc_index++) {
2968 if (!(adev->offload_usecases_state & (0x1 << offload_uc_index))) {
2969 adev->offload_usecases_state |= 0x1 << offload_uc_index;
2970 ret_uc = offload_usecases[offload_uc_index];
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002971 break;
2972 }
2973 }
vivek mehta446c3962015-09-14 10:57:35 -07002974
2975 ALOGV("%s: offload usecase is %d", __func__, ret_uc);
2976 return ret_uc;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002977}
2978
2979static void free_offload_usecase(struct audio_device *adev,
2980 audio_usecase_t uc_id)
2981{
vivek mehta446c3962015-09-14 10:57:35 -07002982 unsigned int offload_uc_index;
Alexy Josephb1379942016-01-29 15:49:38 -08002983 unsigned int num_usecase = sizeof(offload_usecases)/sizeof(offload_usecases[0]);
vivek mehta446c3962015-09-14 10:57:35 -07002984
2985 if (!adev->multi_offload_enable)
2986 return;
2987
2988 for (offload_uc_index = 0; offload_uc_index < num_usecase; offload_uc_index++) {
2989 if (offload_usecases[offload_uc_index] == uc_id) {
2990 adev->offload_usecases_state &= ~(0x1 << offload_uc_index);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002991 break;
2992 }
2993 }
2994 ALOGV("%s: free offload usecase %d", __func__, uc_id);
2995}
2996
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002997static void *offload_thread_loop(void *context)
2998{
2999 struct stream_out *out = (struct stream_out *) context;
3000 struct listnode *item;
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08003001 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003002
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003003 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003004 //set_sched_policy(0, SP_FOREGROUND);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003005 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
3006
3007 ALOGV("%s", __func__);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07003008 lock_output_stream(out);
juyuchen391b5fa2018-12-12 17:58:09 +08003009 out->offload_state = OFFLOAD_STATE_IDLE;
3010 out->playback_started = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003011 for (;;) {
3012 struct offload_cmd *cmd = NULL;
3013 stream_callback_event_t event;
3014 bool send_callback = false;
3015
3016 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
3017 __func__, list_empty(&out->offload_cmd_list),
3018 out->offload_state);
3019 if (list_empty(&out->offload_cmd_list)) {
3020 ALOGV("%s SLEEPING", __func__);
3021 pthread_cond_wait(&out->offload_cond, &out->lock);
3022 ALOGV("%s RUNNING", __func__);
3023 continue;
3024 }
3025
3026 item = list_head(&out->offload_cmd_list);
3027 cmd = node_to_item(item, struct offload_cmd, node);
3028 list_remove(item);
3029
3030 ALOGVV("%s STATE %d CMD %d out->compr %p",
3031 __func__, out->offload_state, cmd->cmd, out->compr);
3032
3033 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
3034 free(cmd);
3035 break;
3036 }
3037
Haynes Mathew Georgeee5836f2017-11-21 18:02:10 -08003038 // allow OFFLOAD_CMD_ERROR reporting during standby
3039 // this is needed to handle failures during compress_open
3040 // Note however that on a pause timeout, the stream is closed
3041 // and no offload usecase will be active. Therefore this
3042 // special case is needed for compress_open failures alone
3043 if (cmd->cmd != OFFLOAD_CMD_ERROR &&
3044 out->compr == NULL) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003045 ALOGE("%s: Compress handle is NULL", __func__);
Haynes Mathew Georgea9abb202016-06-02 14:13:20 -07003046 free(cmd);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003047 pthread_cond_signal(&out->cond);
3048 continue;
3049 }
3050 out->offload_thread_blocked = true;
3051 pthread_mutex_unlock(&out->lock);
3052 send_callback = false;
3053 switch(cmd->cmd) {
3054 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07003055 ALOGD("copl(%p):calling compress_wait", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003056 compress_wait(out->compr, -1);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07003057 ALOGD("copl(%p):out of compress_wait", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003058 send_callback = true;
3059 event = STREAM_CBK_EVENT_WRITE_READY;
3060 break;
3061 case OFFLOAD_CMD_PARTIAL_DRAIN:
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08003062 ret = compress_next_track(out->compr);
Sidipotu Ashok55820562014-02-10 16:16:38 +05303063 if(ret == 0) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07003064 ALOGD("copl(%p):calling compress_partial_drain", out);
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05303065 ret = compress_partial_drain(out->compr);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07003066 ALOGD("copl(%p):out of compress_partial_drain", out);
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05303067 if (ret < 0)
3068 ret = -errno;
Sidipotu Ashok55820562014-02-10 16:16:38 +05303069 }
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05303070 else if (ret == -ETIMEDOUT)
Aniket Kumar Lata3570fb12017-11-08 15:53:44 -08003071 ret = compress_drain(out->compr);
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08003072 else
3073 ALOGE("%s: Next track returned error %d",__func__, ret);
Aniket Kumar Lata3570fb12017-11-08 15:53:44 -08003074 if (-ENETRESET != ret && !(-EINTR == ret &&
3075 CARD_STATUS_OFFLINE == out->card_status)) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05303076 send_callback = true;
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05303077 pthread_mutex_lock(&out->lock);
3078 out->send_new_metadata = 1;
3079 out->send_next_track_params = true;
3080 pthread_mutex_unlock(&out->lock);
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05303081 event = STREAM_CBK_EVENT_DRAIN_READY;
3082 ALOGV("copl(%p):send drain callback, ret %d", out, ret);
3083 } else
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05303084 ALOGI("%s: Block drain ready event during SSR", __func__);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003085 break;
3086 case OFFLOAD_CMD_DRAIN:
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07003087 ALOGD("copl(%p):calling compress_drain", out);
Aniket Kumar Lata3570fb12017-11-08 15:53:44 -08003088 ret = compress_drain(out->compr);
3089 ALOGD("copl(%p):out of compress_drain", out);
3090 // EINTR check avoids drain interruption due to SSR
3091 if (-ENETRESET != ret && !(-EINTR == ret &&
3092 CARD_STATUS_OFFLINE == out->card_status)) {
3093 send_callback = true;
3094 event = STREAM_CBK_EVENT_DRAIN_READY;
3095 } else
3096 ALOGI("%s: Block drain ready event during SSR", __func__);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003097 break;
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05303098 case OFFLOAD_CMD_ERROR:
3099 ALOGD("copl(%p): sending error callback to AF", out);
3100 send_callback = true;
3101 event = STREAM_CBK_EVENT_ERROR;
3102 break;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003103 default:
3104 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
3105 break;
3106 }
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07003107 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003108 out->offload_thread_blocked = false;
3109 pthread_cond_signal(&out->cond);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08003110 if (send_callback && out->client_callback) {
3111 ALOGVV("%s: sending client_callback event %d", __func__, event);
3112 out->client_callback(event, NULL, out->client_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -07003113 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003114 free(cmd);
3115 }
3116
3117 pthread_cond_signal(&out->cond);
3118 while (!list_empty(&out->offload_cmd_list)) {
3119 item = list_head(&out->offload_cmd_list);
3120 list_remove(item);
3121 free(node_to_item(item, struct offload_cmd, node));
3122 }
3123 pthread_mutex_unlock(&out->lock);
3124
3125 return NULL;
3126}
3127
3128static int create_offload_callback_thread(struct stream_out *out)
3129{
3130 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
3131 list_init(&out->offload_cmd_list);
3132 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
3133 offload_thread_loop, out);
3134 return 0;
3135}
3136
3137static int destroy_offload_callback_thread(struct stream_out *out)
3138{
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07003139 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003140 stop_compressed_output_l(out);
3141 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
3142
3143 pthread_mutex_unlock(&out->lock);
3144 pthread_join(out->offload_thread, (void **) NULL);
3145 pthread_cond_destroy(&out->offload_cond);
3146
3147 return 0;
3148}
3149
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003150static int stop_output_stream(struct stream_out *out)
3151{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05303152 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003153 struct audio_usecase *uc_info;
3154 struct audio_device *adev = out->dev;
Aalique Grahame22e49102018-12-18 14:23:57 -08003155 bool has_voip_usecase =
3156 get_usecase_from_list(adev, USECASE_AUDIO_PLAYBACK_VOIP) != NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003157
Eric Laurent994a6932013-07-17 11:51:42 -07003158 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003159 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003160 uc_info = get_usecase_from_list(adev, out->usecase);
3161 if (uc_info == NULL) {
3162 ALOGE("%s: Could not find the usecase (%d) in the list",
3163 __func__, out->usecase);
3164 return -EINVAL;
3165 }
3166
Derek Chenea197282019-01-07 17:35:01 -08003167 if (audio_extn_ext_hw_plugin_usecase_stop(adev->ext_hw_plugin, uc_info))
3168 ALOGE("%s: failed to stop ext hw plugin", __func__);
Derek Chend2530072014-11-24 12:39:14 -08003169
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07003170 if (is_offload_usecase(out->usecase) &&
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05303171 !(audio_extn_passthru_is_passthrough_stream(out))) {
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08003172 if (adev->visualizer_stop_output != NULL)
3173 adev->visualizer_stop_output(out->handle, out->pcm_device_id);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08003174
3175 audio_extn_dts_remove_state_notifier_node(out->usecase);
3176
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08003177 if (adev->offload_effects_stop_output != NULL)
3178 adev->offload_effects_stop_output(out->handle, out->pcm_device_id);
vivek mehtad15d2bf2019-05-17 13:35:10 -07003179 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_ULL ||
3180 out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
3181 audio_low_latency_hint_end();
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08003182 }
Eric Laurentc4aef752013-09-12 17:45:53 -07003183
Arun Mirpurief53ce52018-09-11 18:00:09 -07003184 if (out->usecase == USECASE_INCALL_MUSIC_UPLINK)
3185 voice_set_device_mute_flag(adev, false);
3186
Eric Laurent150dbfe2013-02-27 14:31:02 -08003187 /* 1. Get and set stream specific mixer controls */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07003188 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003189
3190 /* 2. Disable the rx device */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07003191 disable_snd_device(adev, uc_info->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003192
Aalique Grahame22e49102018-12-18 14:23:57 -08003193 audio_extn_extspk_update(adev->extspk);
3194
Xiaojun Sang785b5da2017-08-03 15:52:29 +08003195 if (is_offload_usecase(out->usecase)) {
3196 audio_enable_asm_bit_width_enforce_mode(adev->mixer,
3197 adev->dsp_bit_width_enforce_mode,
3198 false);
3199 }
Garmond Leung5fd0b552018-04-17 11:56:12 -07003200 if (audio_is_usb_out_device(out->devices & AUDIO_DEVICE_OUT_ALL_USB)) {
3201 ret = audio_extn_usb_check_and_set_svc_int(uc_info,
3202 false);
3203
3204 if (ret != 0)
3205 check_usecases_codec_backend(adev, uc_info, uc_info->out_snd_device);
3206 /* default service interval was successfully updated,
3207 reopen USB backend with new service interval */
3208 ret = 0;
3209 }
Xiaojun Sang785b5da2017-08-03 15:52:29 +08003210
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08003211 list_remove(&uc_info->list);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303212 out->started = 0;
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07003213 if (is_offload_usecase(out->usecase) &&
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05303214 (audio_extn_passthru_is_passthrough_stream(out))) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07003215 ALOGV("Disable passthrough , reset mixer to pcm");
3216 /* NO_PASSTHROUGH */
3217 out->compr_config.codec->compr_passthr = 0;
Mingming Yin21854652016-04-13 11:54:02 -07003218 audio_extn_passthru_on_stop(out);
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07003219 audio_extn_dolby_set_dap_bypass(adev, DAP_STATE_ON);
3220 }
Eric Laurent07eeafd2013-10-06 12:52:49 -07003221
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05303222 /* Must be called after removing the usecase from list */
3223 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
Md Mansoor Ahmeddb1b4f92018-01-25 18:56:31 +05303224 audio_extn_keep_alive_start(KEEP_ALIVE_OUT_HDMI);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05303225
Manish Dewangan21a850a2017-08-14 12:03:55 +05303226 if (out->ip_hdlr_handle) {
Naresh Tanniru85819452017-05-04 18:55:45 -07003227 ret = audio_extn_ip_hdlr_intf_close(out->ip_hdlr_handle, true, out);
3228 if (ret < 0)
3229 ALOGE("%s: audio_extn_ip_hdlr_intf_close failed %d",__func__, ret);
3230 }
3231
juyuchen2d415992018-11-16 14:15:16 +08003232 /* 1) media + voip output routing to handset must route media back to
3233 speaker when voip stops.
3234 2) trigger voip input to reroute when voip output changes to
3235 hearing aid. */
Aalique Grahame22e49102018-12-18 14:23:57 -08003236 if (has_voip_usecase ||
3237 out->devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
3238 struct listnode *node;
3239 struct audio_usecase *usecase;
3240 list_for_each(node, &adev->usecase_list) {
3241 usecase = node_to_item(node, struct audio_usecase, list);
juyuchen2d415992018-11-16 14:15:16 +08003242 if ((usecase->type == PCM_CAPTURE &&
3243 usecase->id != USECASE_AUDIO_RECORD_VOIP)
3244 || usecase == uc_info)
Aalique Grahame22e49102018-12-18 14:23:57 -08003245 continue;
3246
3247 ALOGD("%s: select_devices at usecase(%d: %s) after removing the usecase(%d: %s)",
3248 __func__, usecase->id, use_case_table[usecase->id],
3249 out->usecase, use_case_table[out->usecase]);
3250 select_devices(adev, usecase->id);
3251 }
3252 }
3253
Garmond Leung5fd0b552018-04-17 11:56:12 -07003254 free(uc_info);
Eric Laurent994a6932013-07-17 11:51:42 -07003255 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003256 return ret;
3257}
3258
Vignesh Kulothungana6927272019-02-20 15:17:07 -08003259struct pcm* pcm_open_prepare_helper(unsigned int snd_card, unsigned int pcm_device_id,
3260 unsigned int flags, unsigned int pcm_open_retry_count,
3261 struct pcm_config *config)
3262{
3263 struct pcm* pcm = NULL;
3264
3265 while (1) {
3266 pcm = pcm_open(snd_card, pcm_device_id, flags, config);
3267 if (pcm == NULL || !pcm_is_ready(pcm)) {
3268 ALOGE("%s: %s", __func__, pcm_get_error(pcm));
3269 if (pcm != NULL) {
3270 pcm_close(pcm);
3271 pcm = NULL;
3272 }
3273 if (pcm_open_retry_count-- == 0)
3274 return NULL;
3275
3276 usleep(PROXY_OPEN_WAIT_TIME * 1000);
3277 continue;
3278 }
3279 break;
3280 }
3281
3282 if (pcm_is_ready(pcm)) {
3283 int ret = pcm_prepare(pcm);
3284 if (ret < 0) {
3285 ALOGE("%s: pcm_prepare returned %d", __func__, ret);
3286 pcm_close(pcm);
3287 pcm = NULL;
3288 }
3289 }
3290
3291 return pcm;
3292}
3293
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003294int start_output_stream(struct stream_out *out)
3295{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003296 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003297 struct audio_usecase *uc_info;
3298 struct audio_device *adev = out->dev;
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08003299 char mixer_ctl_name[128];
3300 struct mixer_ctl *ctl = NULL;
3301 char* perf_mode[] = {"ULL", "ULL_PP", "LL"};
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303302 bool a2dp_combo = false;
Vignesh Kulothungana6927272019-02-20 15:17:07 -08003303 bool is_haptic_usecase = (out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) ? true: false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003304
Haynes Mathew George380745d2017-10-04 15:27:45 -07003305 ATRACE_BEGIN("start_output_stream");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07003306 if ((out->usecase < 0) || (out->usecase >= AUDIO_USECASE_MAX)) {
3307 ret = -EINVAL;
3308 goto error_config;
3309 }
3310
Vignesh Kulothungana6927272019-02-20 15:17:07 -08003311 ALOGD("%s: enter: stream(%p)usecase(%d: %s) devices(%#x) is_haptic_usecase(%d)",
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05303312 __func__, &out->stream, out->usecase, use_case_table[out->usecase],
Vignesh Kulothungana6927272019-02-20 15:17:07 -08003313 out->devices, is_haptic_usecase);
Naresh Tanniru4c630392014-05-12 01:05:52 +05303314
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05303315 if (CARD_STATUS_OFFLINE == out->card_status ||
3316 CARD_STATUS_OFFLINE == adev->card_status) {
3317 ALOGW("out->card_status or adev->card_status offline, try again");
Dhanalakshmi Siddani4d57e992014-07-17 16:37:51 +05303318 ret = -EIO;
Naresh Tanniru4c630392014-05-12 01:05:52 +05303319 goto error_config;
3320 }
Naresh Tanniru4c630392014-05-12 01:05:52 +05303321
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05303322 if (out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
Florian Pfister1a84f312018-07-19 14:38:18 +02003323 if (!audio_extn_a2dp_source_is_ready()) {
Aalique Grahame22e49102018-12-18 14:23:57 -08003324 if (out->devices &
3325 (AUDIO_DEVICE_OUT_SPEAKER | AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303326 a2dp_combo = true;
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05303327 } else {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303328 if (!(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
3329 ALOGE("%s: A2DP profile is not ready, return error", __func__);
3330 ret = -EAGAIN;
3331 goto error_config;
3332 }
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05303333 }
3334 }
3335 }
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303336 if (out->devices & AUDIO_DEVICE_OUT_ALL_SCO) {
3337 if (!adev->bt_sco_on) {
3338 if (out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
3339 //combo usecase just by pass a2dp
3340 ALOGW("%s: SCO is not connected, route it to speaker", __func__);
3341 out->devices = AUDIO_DEVICE_OUT_SPEAKER;
3342 } else {
3343 ALOGE("%s: SCO profile is not ready, return error", __func__);
3344 ret = -EAGAIN;
3345 goto error_config;
3346 }
3347 }
3348 }
3349
Eric Laurentb23d5282013-05-14 15:27:20 -07003350 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003351 if (out->pcm_device_id < 0) {
3352 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
3353 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08003354 ret = -EINVAL;
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07003355 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003356 }
3357
Vignesh Kulothungana6927272019-02-20 15:17:07 -08003358 if (is_haptic_usecase) {
3359 adev->haptic_pcm_device_id = platform_get_haptics_pcm_device_id();
3360 if (adev->haptic_pcm_device_id < 0) {
3361 ALOGE("%s: Invalid Haptics pcm device id(%d) for the usecase(%d)",
3362 __func__, adev->haptic_pcm_device_id, out->usecase);
3363 ret = -EINVAL;
3364 goto error_config;
3365 }
3366 }
3367
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003368 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07003369
3370 if (!uc_info) {
3371 ret = -ENOMEM;
3372 goto error_config;
3373 }
3374
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003375 uc_info->id = out->usecase;
3376 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08003377 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003378 uc_info->devices = out->devices;
3379 uc_info->in_snd_device = SND_DEVICE_NONE;
3380 uc_info->out_snd_device = SND_DEVICE_NONE;
Garmond Leung5fd0b552018-04-17 11:56:12 -07003381
3382 /* This must be called before adding this usecase to the list */
3383 if (audio_is_usb_out_device(out->devices & AUDIO_DEVICE_OUT_ALL_USB)) {
3384 audio_extn_usb_check_and_set_svc_int(uc_info, true);
3385 /* USB backend is not reopened immediately.
3386 This is eventually done as part of select_devices */
3387 }
3388
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08003389 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003390
Wei Wangf7ca6c92017-11-21 14:51:20 -08003391 audio_streaming_hint_start();
Sudheer Papothifa9d2282015-09-17 01:53:25 +05303392 audio_extn_perf_lock_acquire(&adev->perf_lock_handle, 0,
3393 adev->perf_lock_opts,
3394 adev->perf_lock_opts_size);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05303395
3396 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Md Mansoor Ahmeddb1b4f92018-01-25 18:56:31 +05303397 audio_extn_keep_alive_stop(KEEP_ALIVE_OUT_HDMI);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05303398 if (audio_extn_passthru_is_enabled() &&
3399 audio_extn_passthru_is_passthrough_stream(out)) {
3400 audio_extn_passthru_on_start(out);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05303401 }
3402 }
3403
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303404 if ((out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) &&
Florian Pfister1a84f312018-07-19 14:38:18 +02003405 (!audio_extn_a2dp_source_is_ready())) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303406 if (!a2dp_combo) {
3407 check_a2dp_restore_l(adev, out, false);
3408 } else {
3409 audio_devices_t dev = out->devices;
Aalique Grahame22e49102018-12-18 14:23:57 -08003410 if (dev & AUDIO_DEVICE_OUT_SPEAKER_SAFE)
3411 out->devices = AUDIO_DEVICE_OUT_SPEAKER_SAFE;
3412 else
3413 out->devices = AUDIO_DEVICE_OUT_SPEAKER;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303414 select_devices(adev, out->usecase);
3415 out->devices = dev;
3416 }
3417 } else {
3418 select_devices(adev, out->usecase);
3419 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003420
Arun Mirpurief53ce52018-09-11 18:00:09 -07003421 if (out->usecase == USECASE_INCALL_MUSIC_UPLINK)
3422 voice_set_device_mute_flag(adev, true);
3423
Derek Chenea197282019-01-07 17:35:01 -08003424 if (audio_extn_ext_hw_plugin_usecase_start(adev->ext_hw_plugin, uc_info))
3425 ALOGE("%s: failed to start ext hw plugin", __func__);
Derek Chend2530072014-11-24 12:39:14 -08003426
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003427 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d) format(%#x)",
3428 __func__, adev->snd_card, out->pcm_device_id, out->config.format);
Haynes Mathew George16081042017-05-31 17:16:49 -07003429
3430 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
Arun Mirpuri5d170872019-03-26 13:21:31 -07003431 ALOGD("%s: Starting MMAP stream", __func__);
Haynes Mathew George16081042017-05-31 17:16:49 -07003432 if (out->pcm == NULL || !pcm_is_ready(out->pcm)) {
3433 ALOGE("%s: pcm stream not ready", __func__);
3434 goto error_open;
3435 }
3436 ret = pcm_start(out->pcm);
3437 if (ret < 0) {
3438 ALOGE("%s: MMAP pcm_start failed ret %d", __func__, ret);
3439 goto error_open;
3440 }
Arun Mirpuri5d170872019-03-26 13:21:31 -07003441 out_set_mmap_volume(&out->stream, out->volume_l, out->volume_r);
Haynes Mathew George16081042017-05-31 17:16:49 -07003442 } else if (!is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003443 unsigned int flags = PCM_OUT;
3444 unsigned int pcm_open_retry_count = 0;
3445 if (out->usecase == USECASE_AUDIO_PLAYBACK_AFE_PROXY) {
3446 flags |= PCM_MMAP | PCM_NOIRQ;
3447 pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT;
Haynes Mathew George5beddd42016-06-27 18:33:40 -07003448 } else if (out->realtime) {
Haynes Mathew George4ab3ba92017-12-11 14:49:43 -08003449 flags |= PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003450 } else
3451 flags |= PCM_MONOTONIC;
3452
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08003453 if ((adev->vr_audio_mode_enabled) &&
3454 (out->flags & AUDIO_OUTPUT_FLAG_RAW)) {
3455 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
3456 "PCM_Dev %d Topology", out->pcm_device_id);
3457 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
3458 if (!ctl) {
3459 ALOGI("%s: Could not get ctl for mixer cmd might be ULL - %s",
3460 __func__, mixer_ctl_name);
3461 } else {
3462 //if success use ULLPP
3463 ALOGI("%s: mixer ctrl %s succeeded setting up ULL for %d",
3464 __func__, mixer_ctl_name, out->pcm_device_id);
3465 //There is a still a possibility that some sessions
3466 // that request for FAST|RAW when 3D audio is active
3467 //can go through ULLPP. Ideally we expects apps to
3468 //listen to audio focus and stop concurrent playback
3469 //Also, we will look for mode flag (voice_in_communication)
3470 //before enabling the realtime flag.
3471 mixer_ctl_set_enum_by_string(ctl, perf_mode[1]);
3472 }
3473 }
3474
Surendar Karka91fa3682018-07-02 18:12:12 +05303475 if (out->realtime)
3476 platform_set_stream_channel_map(adev->platform, out->channel_mask,
3477 out->pcm_device_id, &out->channel_map_param.channel_map[0]);
3478
Vignesh Kulothungana6927272019-02-20 15:17:07 -08003479 out->pcm = pcm_open_prepare_helper(adev->snd_card, out->pcm_device_id,
3480 flags, pcm_open_retry_count,
3481 &(out->config));
3482 if (out->pcm == NULL) {
3483 ret = -EIO;
3484 goto error_open;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003485 }
Vignesh Kulothungana6927272019-02-20 15:17:07 -08003486
3487 if (is_haptic_usecase) {
3488 adev->haptic_pcm = pcm_open_prepare_helper(adev->snd_card,
3489 adev->haptic_pcm_device_id,
3490 flags, pcm_open_retry_count,
3491 &(adev->haptics_config));
3492 // failure to open haptics pcm shouldnt stop audio,
3493 // so do not close audio pcm in case of error
Vignesh Kulothungane4039c12019-05-07 15:51:39 -07003494
3495 if (property_get_bool("vendor.audio.enable_haptic_audio_sync", false)) {
3496 ALOGD("%s: enable haptic audio synchronization", __func__);
3497 platform_set_qtime(adev->platform, out->pcm_device_id, adev->haptic_pcm_device_id);
3498 }
Vignesh Kulothungana6927272019-02-20 15:17:07 -08003499 }
3500
Surendar Karka91fa3682018-07-02 18:12:12 +05303501 if (!out->realtime)
3502 platform_set_stream_channel_map(adev->platform, out->channel_mask,
Surendar Karkaf51b5842018-04-26 11:28:38 +05303503 out->pcm_device_id, &out->channel_map_param.channel_map[0]);
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07003504
Zhou Song2b8f28f2017-09-11 10:51:38 +08003505 // apply volume for voip playback after path is set up
3506 if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP)
3507 out_set_voip_volume(&out->stream, out->volume_l, out->volume_r);
Ramu Gottipati36547092018-12-28 11:32:09 +05303508 else if ((out->usecase == USECASE_AUDIO_PLAYBACK_LOW_LATENCY || out->usecase == USECASE_AUDIO_PLAYBACK_DEEP_BUFFER ||
3509 out->usecase == USECASE_AUDIO_PLAYBACK_ULL) && (out->apply_volume)) {
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05303510 out_set_pcm_volume(&out->stream, out->volume_l, out->volume_r);
3511 out->apply_volume = false;
3512 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003513 } else {
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -07003514 platform_set_stream_channel_map(adev->platform, out->channel_mask,
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05303515 out->pcm_device_id, &out->channel_map_param.channel_map[0]);
Xiaojun Sang785b5da2017-08-03 15:52:29 +08003516 audio_enable_asm_bit_width_enforce_mode(adev->mixer,
3517 adev->dsp_bit_width_enforce_mode,
3518 true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003519 out->pcm = NULL;
Haynes Mathew George380745d2017-10-04 15:27:45 -07003520 ATRACE_BEGIN("compress_open");
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08003521 out->compr = compress_open(adev->snd_card,
3522 out->pcm_device_id,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003523 COMPRESS_IN, &out->compr_config);
Haynes Mathew George380745d2017-10-04 15:27:45 -07003524 ATRACE_END();
Satish Babu Patakokila54ce83d2018-07-06 18:00:37 +05303525 if (errno == ENETRESET && !is_compress_ready(out->compr)) {
Sharad Sanglec6f32552018-05-04 16:15:38 +05303526 ALOGE("%s: compress_open failed errno:%d\n", __func__, errno);
3527 adev->card_status = CARD_STATUS_OFFLINE;
3528 out->card_status = CARD_STATUS_OFFLINE;
3529 ret = -EIO;
3530 goto error_open;
3531 }
3532
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003533 if (out->compr && !is_compress_ready(out->compr)) {
Haynes Mathew Georgeee5836f2017-11-21 18:02:10 -08003534 ALOGE("%s: failed /w error %s", __func__, compress_get_error(out->compr));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003535 compress_close(out->compr);
3536 out->compr = NULL;
3537 ret = -EIO;
3538 goto error_open;
3539 }
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05303540 /* compress_open sends params of the track, so reset the flag here */
3541 out->is_compr_metadata_avail = false;
3542
Ben Rombergerd771a7c2017-02-22 18:05:17 -08003543 if (out->client_callback)
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003544 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07003545
Fred Oh3f43e742015-03-04 18:42:34 -08003546 /* Since small bufs uses blocking writes, a write will be blocked
3547 for the default max poll time (20s) in the event of an SSR.
3548 Reduce the poll time to observe and deal with SSR faster.
3549 */
Ashish Jain5106d362016-05-11 19:23:33 +05303550 if (!out->non_blocking) {
Fred Oh3f43e742015-03-04 18:42:34 -08003551 compress_set_max_poll_wait(out->compr, 1000);
3552 }
3553
Manish Dewangan69426c82017-01-30 17:35:36 +05303554 audio_extn_utils_compress_set_render_mode(out);
Manish Dewangan58229382017-02-02 15:48:41 +05303555 audio_extn_utils_compress_set_clk_rec_mode(uc_info);
Manish Dewangan69426c82017-01-30 17:35:36 +05303556
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08003557 audio_extn_dts_create_state_notifier_node(out->usecase);
3558 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
3559 popcount(out->channel_mask),
3560 out->playback_started);
3561
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08003562#ifdef DS1_DOLBY_DDP_ENABLED
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05303563 if (audio_extn_utils_is_dolby_format(out->format))
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08003564 audio_extn_dolby_send_ddp_endp_params(adev);
3565#endif
Preetam Singh Ranawatd18d8832017-02-08 17:34:54 +05303566 if (!(audio_extn_passthru_is_passthrough_stream(out)) &&
3567 (out->sample_rate != 176400 && out->sample_rate <= 192000)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07003568 if (adev->visualizer_start_output != NULL)
3569 adev->visualizer_start_output(out->handle, out->pcm_device_id);
3570 if (adev->offload_effects_start_output != NULL)
Ashish Jain5106d362016-05-11 19:23:33 +05303571 adev->offload_effects_start_output(out->handle, out->pcm_device_id, adev->mixer);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08003572 audio_extn_check_and_set_dts_hpx_state(adev);
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07003573 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003574 }
Haynes Mathew George5beddd42016-06-27 18:33:40 -07003575
3576 if (ret == 0) {
3577 register_out_stream(out);
3578 if (out->realtime) {
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07003579 if (out->pcm == NULL || !pcm_is_ready(out->pcm)) {
3580 ALOGE("%s: pcm stream not ready", __func__);
3581 goto error_open;
3582 }
Haynes Mathew George380745d2017-10-04 15:27:45 -07003583 ATRACE_BEGIN("pcm_start");
Haynes Mathew George5beddd42016-06-27 18:33:40 -07003584 ret = pcm_start(out->pcm);
Haynes Mathew George380745d2017-10-04 15:27:45 -07003585 ATRACE_END();
Haynes Mathew George5beddd42016-06-27 18:33:40 -07003586 if (ret < 0)
3587 goto error_open;
3588 }
3589 }
Wei Wangf7ca6c92017-11-21 14:51:20 -08003590 audio_streaming_hint_end();
Sudheer Papothifa9d2282015-09-17 01:53:25 +05303591 audio_extn_perf_lock_release(&adev->perf_lock_handle);
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07003592 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07003593
vivek mehtad15d2bf2019-05-17 13:35:10 -07003594 if (out->usecase == USECASE_AUDIO_PLAYBACK_ULL ||
3595 out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
3596 audio_low_latency_hint_start();
3597 }
3598
Manish Dewangan21a850a2017-08-14 12:03:55 +05303599 if (out->ip_hdlr_handle) {
Vidyakumar Athota6d655882017-05-22 18:26:24 -07003600 ret = audio_extn_ip_hdlr_intf_open(out->ip_hdlr_handle, true, out, out->usecase);
Naresh Tanniru85819452017-05-04 18:55:45 -07003601 if (ret < 0)
3602 ALOGE("%s: audio_extn_ip_hdlr_intf_open failed %d",__func__, ret);
3603 }
3604
Vignesh Kulothungan3b5fae52017-09-25 12:16:30 -07003605 // consider a scenario where on pause lower layers are tear down.
3606 // so on resume, swap mixer control need to be sent only when
3607 // backend is active, hence rather than sending from enable device
3608 // sending it from start of streamtream
3609
3610 platform_set_swap_channels(adev, true);
3611
Haynes Mathew George380745d2017-10-04 15:27:45 -07003612 ATRACE_END();
Vatsal Buchac09ae062018-11-14 13:25:08 +05303613 enable_gcov();
Haynes Mathew George5beddd42016-06-27 18:33:40 -07003614 return ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003615error_open:
Vignesh Kulothungana6927272019-02-20 15:17:07 -08003616 if (adev->haptic_pcm) {
3617 pcm_close(adev->haptic_pcm);
3618 adev->haptic_pcm = NULL;
3619 }
Wei Wangf7ca6c92017-11-21 14:51:20 -08003620 audio_streaming_hint_end();
Sudheer Papothifa9d2282015-09-17 01:53:25 +05303621 audio_extn_perf_lock_release(&adev->perf_lock_handle);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003622 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08003623error_config:
Laxminath Kasam2cb4b752015-09-24 03:59:15 +05303624 /*
3625 * sleep 50ms to allow sufficient time for kernel
3626 * drivers to recover incases like SSR.
3627 */
3628 usleep(50000);
Haynes Mathew George380745d2017-10-04 15:27:45 -07003629 ATRACE_END();
Vatsal Buchac09ae062018-11-14 13:25:08 +05303630 enable_gcov();
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08003631 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003632}
3633
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003634static int check_input_parameters(uint32_t sample_rate,
3635 audio_format_t format,
Aalique Grahame22e49102018-12-18 14:23:57 -08003636 int channel_count,
3637 bool is_usb_hifi)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003638{
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003639 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003640
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05303641 if (((format != AUDIO_FORMAT_PCM_16_BIT) && (format != AUDIO_FORMAT_PCM_8_24_BIT) &&
3642 (format != AUDIO_FORMAT_PCM_24_BIT_PACKED) && (format != AUDIO_FORMAT_PCM_32_BIT) &&
3643 (format != AUDIO_FORMAT_PCM_FLOAT)) &&
Mingming Yine62d7842013-10-25 16:26:03 -07003644 !voice_extn_compress_voip_is_format_supported(format) &&
Ralf Herzaec80262018-07-03 07:08:49 +02003645 !audio_extn_compr_cap_format_supported(format) &&
3646 !audio_extn_cin_format_supported(format))
Haynes Mathew George484e8d22017-07-31 18:55:17 -07003647 ret = -EINVAL;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003648
Aalique Grahame22e49102018-12-18 14:23:57 -08003649 int max_channel_count = is_usb_hifi ? MAX_HIFI_CHANNEL_COUNT : MAX_CHANNEL_COUNT;
3650 if ((channel_count < MIN_CHANNEL_COUNT) || (channel_count > max_channel_count)) {
3651 ALOGE("%s: unsupported channel count (%d) passed Min / Max (%d / %d)", __func__,
3652 channel_count, MIN_CHANNEL_COUNT, max_channel_count);
3653 return -EINVAL;
3654 }
3655
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003656 switch (channel_count) {
3657 case 1:
3658 case 2:
Chaithanya Krishna Bacharaju9955b162016-05-25 16:25:53 +05303659 case 3:
3660 case 4:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003661 case 6:
Karthikeyan Mani07faa602018-08-20 11:01:32 -07003662 case 8:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003663 break;
3664 default:
3665 ret = -EINVAL;
3666 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003667
3668 switch (sample_rate) {
3669 case 8000:
3670 case 11025:
3671 case 12000:
3672 case 16000:
3673 case 22050:
3674 case 24000:
3675 case 32000:
3676 case 44100:
3677 case 48000:
Haynes Mathew Georgec9253d12017-12-13 15:58:28 -08003678 case 88200:
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05303679 case 96000:
Haynes Mathew Georgec9253d12017-12-13 15:58:28 -08003680 case 176400:
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05303681 case 192000:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003682 break;
3683 default:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003684 ret = -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003685 }
3686
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003687 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003688}
3689
Naresh Tanniru04f71882018-06-26 17:46:22 +05303690
3691/** Add a value in a list if not already present.
3692 * @return true if value was successfully inserted or already present,
3693 * false if the list is full and does not contain the value.
3694 */
3695static bool register_uint(uint32_t value, uint32_t* list, size_t list_length) {
3696 for (size_t i = 0; i < list_length; i++) {
3697 if (list[i] == value) return true; // value is already present
3698 if (list[i] == 0) { // no values in this slot
3699 list[i] = value;
3700 return true; // value inserted
3701 }
3702 }
3703 return false; // could not insert value
3704}
3705
3706/** Add channel_mask in supported_channel_masks if not already present.
3707 * @return true if channel_mask was successfully inserted or already present,
3708 * false if supported_channel_masks is full and does not contain channel_mask.
3709 */
3710static void register_channel_mask(audio_channel_mask_t channel_mask,
3711 audio_channel_mask_t supported_channel_masks[static MAX_SUPPORTED_CHANNEL_MASKS]) {
3712 ALOGE_IF(!register_uint(channel_mask, supported_channel_masks, MAX_SUPPORTED_CHANNEL_MASKS),
3713 "%s: stream can not declare supporting its channel_mask %x", __func__, channel_mask);
3714}
3715
3716/** Add format in supported_formats if not already present.
3717 * @return true if format was successfully inserted or already present,
3718 * false if supported_formats is full and does not contain format.
3719 */
3720static void register_format(audio_format_t format,
3721 audio_format_t supported_formats[static MAX_SUPPORTED_FORMATS]) {
3722 ALOGE_IF(!register_uint(format, supported_formats, MAX_SUPPORTED_FORMATS),
3723 "%s: stream can not declare supporting its format %x", __func__, format);
3724}
3725/** Add sample_rate in supported_sample_rates if not already present.
3726 * @return true if sample_rate was successfully inserted or already present,
3727 * false if supported_sample_rates is full and does not contain sample_rate.
3728 */
3729static void register_sample_rate(uint32_t sample_rate,
3730 uint32_t supported_sample_rates[static MAX_SUPPORTED_SAMPLE_RATES]) {
3731 ALOGE_IF(!register_uint(sample_rate, supported_sample_rates, MAX_SUPPORTED_SAMPLE_RATES),
3732 "%s: stream can not declare supporting its sample rate %x", __func__, sample_rate);
3733}
3734
Karthikeyan Manib38769c2018-11-07 15:44:13 -08003735static inline uint32_t lcm(uint32_t num1, uint32_t num2)
3736{
3737 uint32_t high = num1, low = num2, temp = 0;
3738
3739 if (!num1 || !num2)
3740 return 0;
3741
3742 if (num1 < num2) {
3743 high = num2;
3744 low = num1;
3745 }
3746
3747 while (low != 0) {
3748 temp = low;
3749 low = high % low;
3750 high = temp;
3751 }
3752 return (num1 * num2)/high;
3753}
3754
3755static inline uint32_t nearest_multiple(uint32_t num, uint32_t multiplier)
3756{
3757 uint32_t remainder = 0;
3758
3759 if (!multiplier)
3760 return num;
3761
3762 remainder = num % multiplier;
3763 if (remainder)
3764 num += (multiplier - remainder);
3765
3766 return num;
3767}
3768
Aalique Grahame22e49102018-12-18 14:23:57 -08003769static size_t get_stream_buffer_size(size_t duration_ms,
3770 uint32_t sample_rate,
3771 audio_format_t format,
3772 int channel_count,
3773 bool is_low_latency)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003774{
3775 size_t size = 0;
Karthikeyan Manib38769c2018-11-07 15:44:13 -08003776 uint32_t bytes_per_period_sample = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003777
Aalique Grahame22e49102018-12-18 14:23:57 -08003778 size = (sample_rate * duration_ms) / 1000;
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07003779 if (is_low_latency)
3780 size = configured_low_latency_capture_period_size;
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05303781
Karthikeyan Manib38769c2018-11-07 15:44:13 -08003782 bytes_per_period_sample = audio_bytes_per_sample(format) * channel_count;
Aalique Grahame22e49102018-12-18 14:23:57 -08003783 size *= audio_bytes_per_sample(format) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003784
Ralf Herzbd08d632018-09-28 15:50:49 +02003785 /* make sure the size is multiple of 32 bytes and additionally multiple of
3786 * the frame_size (required for 24bit samples and non-power-of-2 channel counts)
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07003787 * At 48 kHz mono 16-bit PCM:
3788 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
3789 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
Karthikeyan Manib38769c2018-11-07 15:44:13 -08003790 * Also, make sure the size is multiple of bytes per period sample
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07003791 */
Karthikeyan Manib38769c2018-11-07 15:44:13 -08003792 size = nearest_multiple(size, lcm(32, bytes_per_period_sample));
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07003793
3794 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003795}
3796
Aalique Grahame22e49102018-12-18 14:23:57 -08003797static size_t get_input_buffer_size(uint32_t sample_rate,
3798 audio_format_t format,
3799 int channel_count,
3800 bool is_low_latency)
3801{
3802 /* Don't know if USB HIFI in this context so use true to be conservative */
3803 if (check_input_parameters(sample_rate, format, channel_count,
3804 true /*is_usb_hifi */) != 0)
3805 return 0;
3806
3807 return get_stream_buffer_size(AUDIO_CAPTURE_PERIOD_DURATION_MSEC,
3808 sample_rate,
3809 format,
3810 channel_count,
3811 is_low_latency);
3812}
3813
Ashish Jain058165c2016-09-28 23:18:48 +05303814static size_t get_output_period_size(uint32_t sample_rate,
3815 audio_format_t format,
3816 int channel_count,
3817 int duration /*in millisecs*/)
3818{
3819 size_t size = 0;
3820 uint32_t bytes_per_sample = audio_bytes_per_sample(format);
3821
3822 if ((duration == 0) || (sample_rate == 0) ||
3823 (bytes_per_sample == 0) || (channel_count == 0)) {
3824 ALOGW("Invalid config duration %d sr %d bps %d ch %d", duration, sample_rate,
3825 bytes_per_sample, channel_count);
3826 return -EINVAL;
3827 }
3828
3829 size = (sample_rate *
3830 duration *
3831 bytes_per_sample *
3832 channel_count) / 1000;
3833 /*
3834 * To have same PCM samples for all channels, the buffer size requires to
3835 * be multiple of (number of channels * bytes per sample)
3836 * For writes to succeed, the buffer must be written at address which is multiple of 32
3837 */
3838 size = ALIGN(size, (bytes_per_sample * channel_count * 32));
3839
3840 return (size/(channel_count * bytes_per_sample));
3841}
3842
Zhou Song48453a02018-01-10 17:50:59 +08003843static uint64_t get_actual_pcm_frames_rendered(struct stream_out *out, struct timespec *timestamp)
Ashish Jain5106d362016-05-11 19:23:33 +05303844{
3845 uint64_t actual_frames_rendered = 0;
3846 size_t kernel_buffer_size = out->compr_config.fragment_size * out->compr_config.fragments;
3847
3848 /* This adjustment accounts for buffering after app processor.
3849 * It is based on estimated DSP latency per use case, rather than exact.
3850 */
3851 int64_t platform_latency = platform_render_latency(out->usecase) *
3852 out->sample_rate / 1000000LL;
3853
Zhou Song48453a02018-01-10 17:50:59 +08003854 pthread_mutex_lock(&out->position_query_lock);
Ashish Jain5106d362016-05-11 19:23:33 +05303855 /* not querying actual state of buffering in kernel as it would involve an ioctl call
3856 * which then needs protection, this causes delay in TS query for pcm_offload usecase
3857 * hence only estimate.
3858 */
3859 int64_t signed_frames = out->written - kernel_buffer_size;
3860
3861 signed_frames = signed_frames / (audio_bytes_per_sample(out->format) * popcount(out->channel_mask)) - platform_latency;
3862
Zhou Song48453a02018-01-10 17:50:59 +08003863 if (signed_frames > 0) {
Ashish Jain5106d362016-05-11 19:23:33 +05303864 actual_frames_rendered = signed_frames;
Zhou Song48453a02018-01-10 17:50:59 +08003865 if (timestamp != NULL )
3866 *timestamp = out->writeAt;
3867 } else if (timestamp != NULL) {
3868 clock_gettime(CLOCK_MONOTONIC, timestamp);
3869 }
3870 pthread_mutex_unlock(&out->position_query_lock);
Ashish Jain5106d362016-05-11 19:23:33 +05303871
3872 ALOGVV("%s signed frames %lld out_written %lld kernel_buffer_size %d"
3873 "bytes/sample %zu channel count %d", __func__,(long long int)signed_frames,
3874 (long long int)out->written, (int)kernel_buffer_size,
3875 audio_bytes_per_sample(out->compr_config.codec->format),
3876 popcount(out->channel_mask));
3877
3878 return actual_frames_rendered;
3879}
3880
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003881static uint32_t out_get_sample_rate(const struct audio_stream *stream)
3882{
3883 struct stream_out *out = (struct stream_out *)stream;
3884
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003885 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003886}
3887
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07003888static int out_set_sample_rate(struct audio_stream *stream __unused,
3889 uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003890{
3891 return -ENOSYS;
3892}
3893
3894static size_t out_get_buffer_size(const struct audio_stream *stream)
3895{
3896 struct stream_out *out = (struct stream_out *)stream;
3897
Varun Balaraje49253e2017-07-06 19:48:56 +05303898 if (is_interactive_usecase(out->usecase)) {
Sri Karri27279e12017-08-07 16:05:20 +05303899 return out->config.period_size * out->config.period_count;
Varun Balaraje49253e2017-07-06 19:48:56 +05303900 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Naresh Tanniruee3499a2017-01-05 14:05:35 +05303901 if (out->flags & AUDIO_OUTPUT_FLAG_TIMESTAMP)
3902 return out->compr_config.fragment_size - sizeof(struct snd_codec_metadata);
3903 else
3904 return out->compr_config.fragment_size;
3905 } else if(out->usecase == USECASE_COMPRESS_VOIP_CALL)
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08003906 return voice_extn_compress_voip_out_get_buffer_size(out);
Vikram Panduranga93f080e2017-06-07 18:16:14 -07003907 else if(out->usecase == USECASE_AUDIO_PLAYBACK_VOIP)
3908 return VOIP_IO_BUF_SIZE(out->config.rate, DEFAULT_VOIP_BUF_DURATION_MS, DEFAULT_VOIP_BIT_DEPTH_BYTE);
Dhananjay Kumarac341582017-02-23 23:42:25 +05303909 else if (is_offload_usecase(out->usecase) &&
3910 out->flags == AUDIO_OUTPUT_FLAG_DIRECT)
Ashish Jain83a6cc22016-06-28 14:34:17 +05303911 return out->hal_fragment_size;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003912
Haynes Mathew George5beddd42016-06-27 18:33:40 -07003913 return out->config.period_size * out->af_period_multiplier *
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07003914 audio_stream_out_frame_size((const struct audio_stream_out *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003915}
3916
3917static uint32_t out_get_channels(const struct audio_stream *stream)
3918{
3919 struct stream_out *out = (struct stream_out *)stream;
3920
3921 return out->channel_mask;
3922}
3923
3924static audio_format_t out_get_format(const struct audio_stream *stream)
3925{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003926 struct stream_out *out = (struct stream_out *)stream;
3927
3928 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003929}
3930
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07003931static int out_set_format(struct audio_stream *stream __unused,
3932 audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003933{
3934 return -ENOSYS;
3935}
3936
3937static int out_standby(struct audio_stream *stream)
3938{
3939 struct stream_out *out = (struct stream_out *)stream;
3940 struct audio_device *adev = out->dev;
Haynes Mathew George16081042017-05-31 17:16:49 -07003941 bool do_stop = true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003942
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05303943 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
3944 stream, out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003945
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07003946 lock_output_stream(out);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003947 if (!out->standby) {
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07003948 if (adev->adm_deregister_stream)
3949 adev->adm_deregister_stream(adev->adm_data, out->handle);
3950
Haynes Mathew George7fce0a52016-06-23 18:22:27 -07003951 if (is_offload_usecase(out->usecase))
3952 stop_compressed_output_l(out);
3953
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08003954 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003955 out->standby = true;
Zhou Songa8895042016-07-05 17:54:22 +08003956 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
3957 voice_extn_compress_voip_close_output_stream(stream);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303958 out->started = 0;
Zhou Songa8895042016-07-05 17:54:22 +08003959 pthread_mutex_unlock(&adev->lock);
3960 pthread_mutex_unlock(&out->lock);
3961 ALOGD("VOIP output entered standby");
3962 return 0;
3963 } else if (!is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003964 if (out->pcm) {
3965 pcm_close(out->pcm);
3966 out->pcm = NULL;
3967 }
Haynes Mathew George16081042017-05-31 17:16:49 -07003968 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
3969 do_stop = out->playback_started;
3970 out->playback_started = false;
3971 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003972 } else {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07003973 ALOGD("copl(%p):standby", out);
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05303974 out->send_next_track_params = false;
3975 out->is_compr_metadata_avail = false;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07003976 out->gapless_mdata.encoder_delay = 0;
3977 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003978 if (out->compr != NULL) {
3979 compress_close(out->compr);
3980 out->compr = NULL;
3981 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08003982 }
Haynes Mathew George16081042017-05-31 17:16:49 -07003983 if (do_stop) {
3984 stop_output_stream(out);
3985 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08003986 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003987 }
3988 pthread_mutex_unlock(&out->lock);
Ashish Jainbbce4322016-02-16 13:25:27 +05303989 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003990 return 0;
3991}
3992
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05303993static int out_on_error(struct audio_stream *stream)
3994{
3995 struct stream_out *out = (struct stream_out *)stream;
Ben Rombergerfd02a2f2018-09-17 10:23:23 -07003996 int status = 0;
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05303997
3998 lock_output_stream(out);
Haynes Mathew Georgeee5836f2017-11-21 18:02:10 -08003999 // always send CMD_ERROR for offload streams, this
4000 // is needed e.g. when SSR happens within compress_open
4001 // since the stream is active, offload_callback_thread is also active.
4002 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
4003 stop_compressed_output_l(out);
Ben Rombergerfd02a2f2018-09-17 10:23:23 -07004004 }
4005 pthread_mutex_unlock(&out->lock);
4006
4007 status = out_standby(&out->stream.common);
4008
4009 lock_output_stream(out);
4010 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Haynes Mathew Georgeee5836f2017-11-21 18:02:10 -08004011 send_offload_cmd_l(out, OFFLOAD_CMD_ERROR);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304012 }
Manisha Agarwal7b3e3772019-02-20 14:33:45 +05304013
4014 if (is_offload_usecase(out->usecase) && out->card_status == CARD_STATUS_OFFLINE) {
4015 ALOGD("Setting previous card status if offline");
4016 out->prev_card_status_offline = true;
4017 }
4018
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304019 pthread_mutex_unlock(&out->lock);
4020
Ben Rombergerfd02a2f2018-09-17 10:23:23 -07004021 return status;
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304022}
4023
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304024/*
4025 *standby implementation without locks, assumes that the callee already
4026 *has taken adev and out lock.
4027 */
4028int out_standby_l(struct audio_stream *stream)
4029{
4030 struct stream_out *out = (struct stream_out *)stream;
4031 struct audio_device *adev = out->dev;
4032
4033 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
4034 stream, out->usecase, use_case_table[out->usecase]);
4035
4036 if (!out->standby) {
Haynes Mathew George380745d2017-10-04 15:27:45 -07004037 ATRACE_BEGIN("out_standby_l");
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304038 if (adev->adm_deregister_stream)
4039 adev->adm_deregister_stream(adev->adm_data, out->handle);
4040
4041 if (is_offload_usecase(out->usecase))
4042 stop_compressed_output_l(out);
4043
4044 out->standby = true;
4045 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
4046 voice_extn_compress_voip_close_output_stream(stream);
4047 out->started = 0;
4048 ALOGD("VOIP output entered standby");
Haynes Mathew George380745d2017-10-04 15:27:45 -07004049 ATRACE_END();
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304050 return 0;
4051 } else if (!is_offload_usecase(out->usecase)) {
4052 if (out->pcm) {
4053 pcm_close(out->pcm);
4054 out->pcm = NULL;
4055 }
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004056 if (out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) {
4057 if (adev->haptic_pcm) {
4058 pcm_close(adev->haptic_pcm);
4059 adev->haptic_pcm = NULL;
4060 }
4061
4062 if (adev->haptic_buffer != NULL) {
4063 free(adev->haptic_buffer);
4064 adev->haptic_buffer = NULL;
4065 adev->haptic_buffer_size = 0;
4066 }
4067 adev->haptic_pcm_device_id = 0;
4068 }
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304069 } else {
4070 ALOGD("copl(%p):standby", out);
4071 out->send_next_track_params = false;
4072 out->is_compr_metadata_avail = false;
4073 out->gapless_mdata.encoder_delay = 0;
4074 out->gapless_mdata.encoder_padding = 0;
4075 if (out->compr != NULL) {
4076 compress_close(out->compr);
4077 out->compr = NULL;
4078 }
4079 }
4080 stop_output_stream(out);
Haynes Mathew George380745d2017-10-04 15:27:45 -07004081 ATRACE_END();
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304082 }
4083 ALOGD("%s: exit", __func__);
4084 return 0;
4085}
4086
Aalique Grahame22e49102018-12-18 14:23:57 -08004087static int out_dump(const struct audio_stream *stream, int fd)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004088{
Aalique Grahame22e49102018-12-18 14:23:57 -08004089 struct stream_out *out = (struct stream_out *)stream;
4090
4091 // We try to get the lock for consistency,
4092 // but it isn't necessary for these variables.
4093 // If we're not in standby, we may be blocked on a write.
4094 const bool locked = (pthread_mutex_trylock(&out->lock) == 0);
4095 dprintf(fd, " Standby: %s\n", out->standby ? "yes" : "no");
4096 dprintf(fd, " Frames written: %lld\n", (long long)out->written);
4097
4098 if (locked) {
4099 pthread_mutex_unlock(&out->lock);
4100 }
4101
4102 // dump error info
4103 (void)error_log_dump(
4104 out->error_log, fd, " " /* prefix */, 0 /* lines */, 0 /* limit_ns */);
4105
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004106 return 0;
4107}
4108
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004109static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
4110{
4111 int ret = 0;
4112 char value[32];
ApurupaPattapu2e084df2013-12-18 15:47:59 -08004113
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004114 if (!out || !parms) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08004115 ALOGE("%s: return invalid ",__func__);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004116 return -EINVAL;
4117 }
4118
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +05304119 ret = audio_extn_parse_compress_metadata(out, parms);
Weiyin Jiang18ac4e92015-03-15 15:03:40 +08004120
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004121 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
4122 if (ret >= 0) {
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +05304123 out->gapless_mdata.encoder_delay = atoi(value); //whats a good limit check?
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004124 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004125 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
4126 if (ret >= 0) {
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +05304127 out->gapless_mdata.encoder_padding = atoi(value);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004128 }
4129
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004130 ALOGV("%s new encoder delay %u and padding %u", __func__,
4131 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
4132
4133 return 0;
4134}
4135
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07004136static bool output_drives_call(struct audio_device *adev, struct stream_out *out)
4137{
4138 return out == adev->primary_output || out == adev->voice_tx_output;
4139}
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004140
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304141// note: this call is safe only if the stream_cb is
4142// removed first in close_output_stream (as is done now).
4143static void out_snd_mon_cb(void * stream, struct str_parms * parms)
4144{
4145 if (!stream || !parms)
4146 return;
4147
4148 struct stream_out *out = (struct stream_out *)stream;
4149 struct audio_device *adev = out->dev;
4150
4151 card_status_t status;
4152 int card;
4153 if (parse_snd_card_status(parms, &card, &status) < 0)
4154 return;
4155
4156 pthread_mutex_lock(&adev->lock);
4157 bool valid_cb = (card == adev->snd_card);
4158 pthread_mutex_unlock(&adev->lock);
4159
4160 if (!valid_cb)
4161 return;
4162
4163 lock_output_stream(out);
4164 if (out->card_status != status)
4165 out->card_status = status;
4166 pthread_mutex_unlock(&out->lock);
4167
4168 ALOGI("out_snd_mon_cb for card %d usecase %s, status %s", card,
4169 use_case_table[out->usecase],
4170 status == CARD_STATUS_OFFLINE ? "offline" : "online");
4171
Aditya Bavanari59ebed42019-02-05 17:44:57 +05304172 if (status == CARD_STATUS_OFFLINE) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304173 out_on_error(stream);
Aditya Bavanari59ebed42019-02-05 17:44:57 +05304174 if (voice_is_call_state_active(adev) &&
4175 out == adev->primary_output) {
4176 ALOGD("%s: SSR/PDR occurred, end all calls\n", __func__);
4177 pthread_mutex_lock(&adev->lock);
4178 voice_stop_call(adev);
4179 adev->mode = AUDIO_MODE_NORMAL;
4180 pthread_mutex_unlock(&adev->lock);
4181 }
4182 }
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304183 return;
4184}
4185
Kevin Rocardfce19002017-08-07 19:21:36 -07004186static int get_alive_usb_card(struct str_parms* parms) {
4187 int card;
4188 if ((str_parms_get_int(parms, "card", &card) >= 0) &&
4189 !audio_extn_usb_alive(card)) {
4190 return card;
4191 }
4192 return -ENODEV;
4193}
4194
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004195static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
4196{
4197 struct stream_out *out = (struct stream_out *)stream;
4198 struct audio_device *adev = out->dev;
4199 struct str_parms *parms;
4200 char value[32];
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08004201 int ret = 0, val = 0, err;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304202 bool bypass_a2dp = false;
Garmond Leung5fd0b552018-04-17 11:56:12 -07004203 bool reconfig = false;
4204 unsigned long service_interval = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004205
sangwoobc677242013-08-08 16:53:43 +09004206 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07004207 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004208 parms = str_parms_create_str(kvpairs);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05304209 if (!parms)
4210 goto error;
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08004211 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
4212 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004213 val = atoi(value);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07004214 lock_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08004215 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004216
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07004217 /*
Weiyin Jiang4256eeb2016-05-19 13:28:30 +08004218 * When HDMI cable is unplugged the music playback is paused and
4219 * the policy manager sends routing=0. But the audioflinger continues
4220 * to write data until standby time (3sec). As the HDMI core is
4221 * turned off, the write gets blocked.
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07004222 * Avoid this by routing audio to speaker until standby.
4223 */
Weiyin Jiang4256eeb2016-05-19 13:28:30 +08004224 if ((out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL) &&
4225 (val == AUDIO_DEVICE_NONE) &&
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05304226 !audio_extn_passthru_is_passthrough_stream(out) &&
Weiyin Jiang4256eeb2016-05-19 13:28:30 +08004227 (platform_get_edid_info(adev->platform) != 0) /* HDMI disconnected */) {
4228 val = AUDIO_DEVICE_OUT_SPEAKER;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07004229 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05304230 /*
4231 * When A2DP is disconnected the
4232 * music playback is paused and the policy manager sends routing=0
Florian Pfister1a84f312018-07-19 14:38:18 +02004233 * But the audioflinger continues to write data until standby time
Naresh Tanniru9d027a62015-03-13 01:32:10 +05304234 * (3sec). As BT is turned off, the write gets blocked.
4235 * Avoid this by routing audio to speaker until standby.
4236 */
Naresh Tanniruf7e9e632016-11-04 14:54:20 -07004237 if ((out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) &&
Aniket Kumar Lata1fb11cb2017-09-08 13:48:55 -07004238 (val == AUDIO_DEVICE_NONE) &&
Aniket Kumar Lata2cf6a922019-05-07 17:01:43 -07004239 !audio_extn_a2dp_source_is_ready() &&
4240 !adev->bt_sco_on) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05304241 val = AUDIO_DEVICE_OUT_SPEAKER;
4242 }
Sharad Sangled0a50b22018-04-05 23:28:32 +05304243 /*
4244 * When USB headset is disconnected the music platback paused
4245 * and the policy manager send routing=0. But if the USB is connected
4246 * back before the standby time, AFE is not closed and opened
4247 * when USB is connected back. So routing to speker will guarantee
4248 * AFE reconfiguration and AFE will be opend once USB is connected again
4249 */
4250 if ((out->devices & AUDIO_DEVICE_OUT_ALL_USB) &&
4251 (val == AUDIO_DEVICE_NONE) &&
4252 !audio_extn_usb_connected(parms)) {
4253 val = AUDIO_DEVICE_OUT_SPEAKER;
4254 }
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05304255 /* To avoid a2dp to sco overlapping / BT device improper state
4256 * check with BT lib about a2dp streaming support before routing
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05304257 */
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05304258 if (val & AUDIO_DEVICE_OUT_ALL_A2DP) {
Florian Pfister1a84f312018-07-19 14:38:18 +02004259 if (!audio_extn_a2dp_source_is_ready()) {
Aalique Grahame22e49102018-12-18 14:23:57 -08004260 if (val &
4261 (AUDIO_DEVICE_OUT_SPEAKER | AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05304262 //combo usecase just by pass a2dp
4263 ALOGW("%s: A2DP profile is not ready,routing to speaker only", __func__);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304264 bypass_a2dp = true;
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05304265 } else {
4266 ALOGE("%s: A2DP profile is not ready,ignoring routing request", __func__);
4267 /* update device to a2dp and don't route as BT returned error
4268 * However it is still possible a2dp routing called because
4269 * of current active device disconnection (like wired headset)
4270 */
4271 out->devices = val;
4272 pthread_mutex_unlock(&out->lock);
4273 pthread_mutex_unlock(&adev->lock);
4274 goto error;
4275 }
4276 }
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05304277 }
Kevin Rocardfce19002017-08-07 19:21:36 -07004278
4279 audio_devices_t new_dev = val;
4280
4281 // Workaround: If routing to an non existing usb device, fail gracefully
4282 // The routing request will otherwise block during 10 second
4283 int card;
4284 if (audio_is_usb_out_device(new_dev) &&
4285 (card = get_alive_usb_card(parms)) >= 0) {
4286
4287 ALOGW("out_set_parameters() ignoring rerouting to non existing USB card %d", card);
4288 pthread_mutex_unlock(&adev->lock);
4289 pthread_mutex_unlock(&out->lock);
4290 ret = -ENOSYS;
4291 goto routing_fail;
4292 }
4293
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07004294 /*
4295 * select_devices() call below switches all the usecases on the same
4296 * backend to the new device. Refer to check_usecases_codec_backend() in
4297 * the select_devices(). But how do we undo this?
4298 *
4299 * For example, music playback is active on headset (deep-buffer usecase)
4300 * and if we go to ringtones and select a ringtone, low-latency usecase
4301 * will be started on headset+speaker. As we can't enable headset+speaker
4302 * and headset devices at the same time, select_devices() switches the music
4303 * playback to headset+speaker while starting low-lateny usecase for ringtone.
4304 * So when the ringtone playback is completed, how do we undo the same?
4305 *
4306 * We are relying on the out_set_parameters() call on deep-buffer output,
4307 * once the ringtone playback is ended.
4308 * NOTE: We should not check if the current devices are same as new devices.
4309 * Because select_devices() must be called to switch back the music
4310 * playback to headset.
4311 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08004312 if (val != 0) {
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004313 audio_devices_t new_dev = val;
4314 bool same_dev = out->devices == new_dev;
4315 out->devices = new_dev;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07004316
Ravi Kumar Alamandabe149392014-10-20 17:07:43 -07004317 if (output_drives_call(adev, out)) {
kunleiz61b9c2e2017-11-01 13:47:23 +08004318 if (!voice_is_call_state_active(adev)) {
Ravi Kumar Alamandabe149392014-10-20 17:07:43 -07004319 if (adev->mode == AUDIO_MODE_IN_CALL) {
4320 adev->current_call_output = out;
Garmond Leung5fd0b552018-04-17 11:56:12 -07004321 if (audio_is_usb_out_device(out->devices & AUDIO_DEVICE_OUT_ALL_USB)) {
4322 service_interval = audio_extn_usb_find_service_interval(true, true /*playback*/);
4323 audio_extn_usb_set_service_interval(true /*playback*/,
4324 service_interval,
4325 &reconfig);
4326 ALOGD("%s, svc_int(%ld),reconfig(%d)",__func__,service_interval, reconfig);
4327 }
4328 ret = voice_start_call(adev);
Ravi Kumar Alamandabe149392014-10-20 17:07:43 -07004329 }
4330 } else {
4331 adev->current_call_output = out;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07004332 voice_update_devices_for_all_voice_usecases(adev);
Ravi Kumar Alamandabe149392014-10-20 17:07:43 -07004333 }
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08004334 }
Shiv Maliyappanahalli6b32c4c2015-11-04 18:10:20 -08004335
4336 if (!out->standby) {
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004337 if (!same_dev) {
4338 ALOGV("update routing change");
Sudheer Papothi80266982016-08-16 02:36:18 +05304339 audio_extn_perf_lock_acquire(&adev->perf_lock_handle, 0,
4340 adev->perf_lock_opts,
4341 adev->perf_lock_opts_size);
Haynes Mathew George822b5492016-07-01 16:57:24 -07004342 if (adev->adm_on_routing_change)
4343 adev->adm_on_routing_change(adev->adm_data,
4344 out->handle);
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004345 }
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304346 if (!bypass_a2dp) {
4347 select_devices(adev, out->usecase);
4348 } else {
Aalique Grahame22e49102018-12-18 14:23:57 -08004349 if (new_dev & AUDIO_DEVICE_OUT_SPEAKER_SAFE)
4350 out->devices = AUDIO_DEVICE_OUT_SPEAKER_SAFE;
4351 else
4352 out->devices = AUDIO_DEVICE_OUT_SPEAKER;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304353 select_devices(adev, out->usecase);
4354 out->devices = new_dev;
4355 }
Vignesh Kulothungan3b5fae52017-09-25 12:16:30 -07004356
4357 if (!same_dev) {
4358 // on device switch force swap, lower functions will make sure
4359 // to check if swap is allowed or not.
4360 platform_set_swap_channels(adev, true);
Sudheer Papothi80266982016-08-16 02:36:18 +05304361 audio_extn_perf_lock_release(&adev->perf_lock_handle);
Vignesh Kulothungan3b5fae52017-09-25 12:16:30 -07004362 }
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304363 if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
4364 out->a2dp_compress_mute &&
Florian Pfister1a84f312018-07-19 14:38:18 +02004365 (!(out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) || audio_extn_a2dp_source_is_ready())) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304366 pthread_mutex_lock(&out->compr_mute_lock);
4367 out->a2dp_compress_mute = false;
4368 out_set_compr_volume(&out->stream, out->volume_l, out->volume_r);
4369 pthread_mutex_unlock(&out->compr_mute_lock);
kunleizba786432018-01-26 15:31:59 +08004370 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP) {
4371 out_set_voip_volume(&out->stream, out->volume_l, out->volume_r);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304372 }
Shiv Maliyappanahalli6b32c4c2015-11-04 18:10:20 -08004373 }
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08004374 }
4375
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004376 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08004377 pthread_mutex_unlock(&out->lock);
Aalique Grahame22e49102018-12-18 14:23:57 -08004378
4379 /*handles device and call state changes*/
4380 audio_extn_extspk_update(adev->extspk);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004381 }
Kevin Rocardfce19002017-08-07 19:21:36 -07004382 routing_fail:
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07004383
4384 if (out == adev->primary_output) {
4385 pthread_mutex_lock(&adev->lock);
4386 audio_extn_set_parameters(adev, parms);
4387 pthread_mutex_unlock(&adev->lock);
4388 }
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07004389 if (is_offload_usecase(out->usecase)) {
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07004390 lock_output_stream(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004391 parse_compress_metadata(out, parms);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08004392
4393 audio_extn_dts_create_state_notifier_node(out->usecase);
4394 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
4395 popcount(out->channel_mask),
4396 out->playback_started);
4397
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08004398 pthread_mutex_unlock(&out->lock);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004399 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07004400
Surendar Karkaf51b5842018-04-26 11:28:38 +05304401 err = str_parms_get_str(parms, AUDIO_PARAMETER_DUAL_MONO, value,
4402 sizeof(value));
4403 if (err >= 0) {
4404 if (!strncmp("true", value, sizeof("true")) || atoi(value))
4405 audio_extn_send_dual_mono_mixing_coefficients(out);
4406 }
4407
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +05304408 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_PROFILE, value, sizeof(value));
4409 if (err >= 0) {
4410 strlcpy(out->profile, value, sizeof(out->profile));
4411 ALOGV("updating stream profile with value '%s'", out->profile);
4412 lock_output_stream(out);
4413 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
4414 &adev->streams_output_cfg_list,
Aalique Grahame65780b52017-09-27 14:59:56 -07004415 out->devices, out->flags, out->hal_op_format,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +05304416 out->sample_rate, out->bit_width,
4417 out->channel_mask, out->profile,
4418 &out->app_type_cfg);
4419 pthread_mutex_unlock(&out->lock);
4420 }
4421
Alexy Joseph98988832017-01-13 14:56:59 -08004422 //suspend, resume handling block
Aniket Kumar Lata932f4872017-11-06 18:29:44 -08004423 //remove QOS only if vendor.audio.hal.dynamic.qos.config.supported is set to true
4424 // and vendor.audio.hal.output.suspend.supported is set to true
4425 if (out->hal_output_suspend_supported && out->dynamic_pm_qos_config_supported) {
Alexy Joseph98988832017-01-13 14:56:59 -08004426 //check suspend parameter only for low latency and if the property
4427 //is enabled
4428 if (str_parms_get_str(parms, "suspend_playback", value, sizeof(value)) >= 0) {
4429 ALOGI("%s: got suspend_playback %s", __func__, value);
4430 lock_output_stream(out);
4431 if (!strncmp(value, "false", 5)) {
4432 //suspend_playback=false is supposed to set QOS value back to 75%
4433 //the mixer control sent with value Enable will achieve that
4434 ret = audio_route_apply_and_update_path(adev->audio_route, out->pm_qos_mixer_path);
4435 } else if (!strncmp (value, "true", 4)) {
4436 //suspend_playback=true is supposed to remove QOS value
4437 //resetting the mixer control will set the default value
4438 //for the mixer control which is Disable and this removes the QOS vote
4439 ret = audio_route_reset_and_update_path(adev->audio_route, out->pm_qos_mixer_path);
4440 } else {
4441 ALOGE("%s: Wrong value sent for suspend_playback, expected true/false,"
4442 " got %s", __func__, value);
4443 ret = -1;
4444 }
4445
4446 if (ret != 0) {
4447 ALOGE("%s: %s mixer ctl failed with %d, ignore suspend/resume setparams",
4448 __func__, out->pm_qos_mixer_path, ret);
4449 }
4450
4451 pthread_mutex_unlock(&out->lock);
4452 }
4453 }
4454 //end suspend, resume handling block
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004455 str_parms_destroy(parms);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05304456error:
Eric Laurent994a6932013-07-17 11:51:42 -07004457 ALOGV("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004458 return ret;
4459}
4460
Paul McLeana50b7332018-12-17 08:24:21 -07004461static int in_set_microphone_direction(const struct audio_stream_in *stream,
4462 audio_microphone_direction_t dir) {
justinweng20fb6d82019-02-21 18:49:00 -07004463 struct stream_in *in = (struct stream_in *)stream;
4464
4465 ALOGVV("%s: standby %d source %d dir %d", __func__, in->standby, in->source, dir);
4466
4467 in->direction = dir;
4468
4469 if (in->standby)
4470 return 0;
4471
4472 return audio_extn_audiozoom_set_microphone_direction(in, dir);
Paul McLeana50b7332018-12-17 08:24:21 -07004473}
4474
4475static int in_set_microphone_field_dimension(const struct audio_stream_in *stream, float zoom) {
justinweng20fb6d82019-02-21 18:49:00 -07004476 struct stream_in *in = (struct stream_in *)stream;
4477
4478 ALOGVV("%s: standby %d source %d zoom %f", __func__, in->standby, in->source, zoom);
4479
4480 if (zoom > 1.0 || zoom < -1.0)
4481 return -EINVAL;
4482
4483 in->zoom = zoom;
4484
4485 if (in->standby)
4486 return 0;
4487
4488 return audio_extn_audiozoom_set_microphone_field_dimension(in, zoom);
Paul McLeana50b7332018-12-17 08:24:21 -07004489}
4490
4491
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004492static bool stream_get_parameter_channels(struct str_parms *query,
4493 struct str_parms *reply,
4494 audio_channel_mask_t *supported_channel_masks) {
4495 int ret = -1;
4496 char value[512];
4497 bool first = true;
4498 size_t i, j;
4499
4500 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) {
4501 ret = 0;
4502 value[0] = '\0';
4503 i = 0;
4504 while (supported_channel_masks[i] != 0) {
4505 for (j = 0; j < ARRAY_SIZE(channels_name_to_enum_table); j++) {
4506 if (channels_name_to_enum_table[j].value == supported_channel_masks[i]) {
4507 if (!first)
Satya Krishna Pindiprolib22ac722017-10-09 15:44:16 +05304508 strlcat(value, "|", sizeof(value));
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004509
Satya Krishna Pindiprolib22ac722017-10-09 15:44:16 +05304510 strlcat(value, channels_name_to_enum_table[j].name, sizeof(value));
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004511 first = false;
4512 break;
4513 }
4514 }
4515 i++;
4516 }
4517 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
4518 }
4519 return ret == 0;
4520}
4521
4522static bool stream_get_parameter_formats(struct str_parms *query,
4523 struct str_parms *reply,
4524 audio_format_t *supported_formats) {
4525 int ret = -1;
4526 char value[256];
4527 size_t i, j;
4528 bool first = true;
4529
4530 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) {
4531 ret = 0;
4532 value[0] = '\0';
4533 i = 0;
4534 while (supported_formats[i] != 0) {
4535 for (j = 0; j < ARRAY_SIZE(formats_name_to_enum_table); j++) {
4536 if (formats_name_to_enum_table[j].value == supported_formats[i]) {
4537 if (!first) {
Satya Krishna Pindiprolib22ac722017-10-09 15:44:16 +05304538 strlcat(value, "|", sizeof(value));
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004539 }
Satya Krishna Pindiprolib22ac722017-10-09 15:44:16 +05304540 strlcat(value, formats_name_to_enum_table[j].name, sizeof(value));
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004541 first = false;
4542 break;
4543 }
4544 }
4545 i++;
4546 }
4547 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_FORMATS, value);
4548 }
4549 return ret == 0;
4550}
4551
4552static bool stream_get_parameter_rates(struct str_parms *query,
4553 struct str_parms *reply,
4554 uint32_t *supported_sample_rates) {
4555
4556 int i;
4557 char value[256];
4558 int ret = -1;
4559 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) {
4560 ret = 0;
4561 value[0] = '\0';
4562 i=0;
4563 int cursor = 0;
4564 while (supported_sample_rates[i]) {
4565 int avail = sizeof(value) - cursor;
4566 ret = snprintf(value + cursor, avail, "%s%d",
4567 cursor > 0 ? "|" : "",
4568 supported_sample_rates[i]);
4569 if (ret < 0 || ret >= avail) {
4570 // if cursor is at the last element of the array
4571 // overwrite with \0 is duplicate work as
4572 // snprintf already put a \0 in place.
4573 // else
4574 // we had space to write the '|' at value[cursor]
4575 // (which will be overwritten) or no space to fill
4576 // the first element (=> cursor == 0)
4577 value[cursor] = '\0';
4578 break;
4579 }
4580 cursor += ret;
4581 ++i;
4582 }
4583 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES,
4584 value);
4585 }
4586 return ret >= 0;
4587}
4588
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004589static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
4590{
4591 struct stream_out *out = (struct stream_out *)stream;
4592 struct str_parms *query = str_parms_create_str(keys);
Alexy Josephaee4fdd2016-01-29 13:02:07 -08004593 char *str = (char*) NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004594 char value[256];
4595 struct str_parms *reply = str_parms_create();
4596 size_t i, j;
4597 int ret;
4598 bool first = true;
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07004599
4600 if (!query || !reply) {
Alexy Josephaee4fdd2016-01-29 13:02:07 -08004601 if (reply) {
4602 str_parms_destroy(reply);
4603 }
4604 if (query) {
4605 str_parms_destroy(query);
4606 }
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07004607 ALOGE("out_get_parameters: failed to allocate mem for query or reply");
4608 return NULL;
4609 }
4610
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004611 ALOGV("%s: %s enter: keys - %s", __func__, use_case_table[out->usecase], keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004612 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
4613 if (ret >= 0) {
4614 value[0] = '\0';
4615 i = 0;
4616 while (out->supported_channel_masks[i] != 0) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004617 for (j = 0; j < ARRAY_SIZE(channels_name_to_enum_table); j++) {
4618 if (channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004619 if (!first) {
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -08004620 strlcat(value, "|", sizeof(value));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004621 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004622 strlcat(value, channels_name_to_enum_table[j].name, sizeof(value));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004623 first = false;
4624 break;
4625 }
4626 }
4627 i++;
4628 }
4629 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
4630 str = str_parms_to_str(reply);
4631 } else {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08004632 voice_extn_out_get_parameters(out, query, reply);
4633 str = str_parms_to_str(reply);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004634 }
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004635
Alexy Joseph62142aa2015-11-16 15:10:34 -08004636
4637 ret = str_parms_get_str(query, "is_direct_pcm_track", value, sizeof(value));
4638 if (ret >= 0) {
4639 value[0] = '\0';
Dhananjay Kumarac341582017-02-23 23:42:25 +05304640 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
4641 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Alexy Joseph62142aa2015-11-16 15:10:34 -08004642 ALOGV("in direct_pcm");
Satya Krishna Pindiprolib6655542017-07-03 19:38:19 +05304643 strlcat(value, "true", sizeof(value));
Alexy Joseph62142aa2015-11-16 15:10:34 -08004644 } else {
4645 ALOGV("not in direct_pcm");
Sharad Sangle3dd5a4a2015-12-10 18:39:17 +05304646 strlcat(value, "false", sizeof(value));
Alexy Joseph62142aa2015-11-16 15:10:34 -08004647 }
4648 str_parms_add_str(reply, "is_direct_pcm_track", value);
Alexy Josephaee4fdd2016-01-29 13:02:07 -08004649 if (str)
4650 free(str);
Alexy Joseph62142aa2015-11-16 15:10:34 -08004651 str = str_parms_to_str(reply);
4652 }
4653
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004654 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS, value, sizeof(value));
4655 if (ret >= 0) {
4656 value[0] = '\0';
4657 i = 0;
4658 first = true;
4659 while (out->supported_formats[i] != 0) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004660 for (j = 0; j < ARRAY_SIZE(formats_name_to_enum_table); j++) {
4661 if (formats_name_to_enum_table[j].value == out->supported_formats[i]) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004662 if (!first) {
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -08004663 strlcat(value, "|", sizeof(value));
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004664 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004665 strlcat(value, formats_name_to_enum_table[j].name, sizeof(value));
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004666 first = false;
4667 break;
4668 }
4669 }
4670 i++;
4671 }
4672 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_FORMATS, value);
Alexy Josephaee4fdd2016-01-29 13:02:07 -08004673 if (str)
4674 free(str);
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004675 str = str_parms_to_str(reply);
4676 }
Mingming Yin3a941d42016-02-17 18:08:05 -08004677
4678 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES, value, sizeof(value));
4679 if (ret >= 0) {
4680 value[0] = '\0';
4681 i = 0;
4682 first = true;
4683 while (out->supported_sample_rates[i] != 0) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004684 for (j = 0; j < ARRAY_SIZE(out_sample_rates_name_to_enum_table); j++) {
4685 if (out_sample_rates_name_to_enum_table[j].value == out->supported_sample_rates[i]) {
Mingming Yin3a941d42016-02-17 18:08:05 -08004686 if (!first) {
4687 strlcat(value, "|", sizeof(value));
4688 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004689 strlcat(value, out_sample_rates_name_to_enum_table[j].name, sizeof(value));
Mingming Yin3a941d42016-02-17 18:08:05 -08004690 first = false;
4691 break;
4692 }
4693 }
4694 i++;
4695 }
4696 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES, value);
4697 if (str)
4698 free(str);
4699 str = str_parms_to_str(reply);
4700 }
4701
Alexy Joseph98988832017-01-13 14:56:59 -08004702 if (str_parms_get_str(query, "supports_hw_suspend", value, sizeof(value)) >= 0) {
4703 //only low latency track supports suspend_resume
4704 str_parms_add_int(reply, "supports_hw_suspend",
Aniket Kumar Lata932f4872017-11-06 18:29:44 -08004705 (out->hal_output_suspend_supported));
Alexy Joseph98988832017-01-13 14:56:59 -08004706 if (str)
4707 free(str);
4708 str = str_parms_to_str(reply);
4709 }
4710
4711
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004712 str_parms_destroy(query);
4713 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07004714 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004715 return str;
4716}
4717
4718static uint32_t out_get_latency(const struct audio_stream_out *stream)
4719{
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004720 uint32_t period_ms;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004721 struct stream_out *out = (struct stream_out *)stream;
Alexy Josephaa54c872014-12-03 02:46:47 -08004722 uint32_t latency = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004723
Alexy Josephaa54c872014-12-03 02:46:47 -08004724 if (is_offload_usecase(out->usecase)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05304725 lock_output_stream(out);
4726 latency = audio_extn_utils_compress_get_dsp_latency(out);
4727 pthread_mutex_unlock(&out->lock);
Haynes Mathew George16081042017-05-31 17:16:49 -07004728 } else if ((out->realtime) ||
4729 (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP)) {
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004730 // since the buffer won't be filled up faster than realtime,
4731 // return a smaller number
4732 if (out->config.rate)
4733 period_ms = (out->af_period_multiplier * out->config.period_size *
4734 1000) / (out->config.rate);
4735 else
4736 period_ms = 0;
4737 latency = period_ms + platform_render_latency(out->usecase)/1000;
Alexy Josephaa54c872014-12-03 02:46:47 -08004738 } else {
4739 latency = (out->config.period_count * out->config.period_size * 1000) /
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004740 (out->config.rate);
Alexy Josephaa54c872014-12-03 02:46:47 -08004741 }
4742
yidongh0515e042017-07-06 15:00:34 +08004743 if (AUDIO_DEVICE_OUT_ALL_A2DP & out->devices)
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08004744 latency += audio_extn_a2dp_get_encoder_latency();
4745
Anish Kumar50ebcbf2014-12-09 04:01:39 +05304746 ALOGV("%s: Latency %d", __func__, latency);
Alexy Josephaa54c872014-12-03 02:46:47 -08004747 return latency;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004748}
4749
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05304750static float AmpToDb(float amplification)
4751{
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05304752 float db = DSD_VOLUME_MIN_DB;
4753 if (amplification > 0) {
4754 db = 20 * log10(amplification);
4755 if(db < DSD_VOLUME_MIN_DB)
4756 return DSD_VOLUME_MIN_DB;
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05304757 }
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05304758 return db;
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05304759}
4760
Arun Mirpuri5d170872019-03-26 13:21:31 -07004761static int out_set_mmap_volume(struct audio_stream_out *stream, float left,
4762 float right)
4763{
4764 struct stream_out *out = (struct stream_out *)stream;
4765 long volume = 0;
4766 char mixer_ctl_name[128] = "";
4767 struct audio_device *adev = out->dev;
4768 struct mixer_ctl *ctl = NULL;
4769 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
4770 PCM_PLAYBACK);
4771
4772 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
4773 "Playback %d Volume", pcm_device_id);
4774 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
4775 if (!ctl) {
4776 ALOGE("%s: Could not get ctl for mixer cmd - %s",
4777 __func__, mixer_ctl_name);
4778 return -EINVAL;
4779 }
4780 if (left != right)
4781 ALOGW("%s: Left and right channel volume mismatch:%f,%f",
4782 __func__, left, right);
4783 volume = (long)(left * (MMAP_PLAYBACK_VOLUME_MAX*1.0));
4784 if (mixer_ctl_set_value(ctl, 0, volume) < 0){
4785 ALOGE("%s:ctl for mixer cmd - %s, volume %ld returned error",
4786 __func__, mixer_ctl_name, volume);
4787 return -EINVAL;
4788 }
4789 return 0;
4790}
4791
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304792static int out_set_compr_volume(struct audio_stream_out *stream, float left,
4793 float right)
4794{
4795 struct stream_out *out = (struct stream_out *)stream;
Manish Dewangan338c50a2017-09-12 15:22:03 +05304796 long volume[2];
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304797 char mixer_ctl_name[128];
4798 struct audio_device *adev = out->dev;
4799 struct mixer_ctl *ctl;
4800 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
4801 PCM_PLAYBACK);
4802
4803 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
4804 "Compress Playback %d Volume", pcm_device_id);
4805 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
4806 if (!ctl) {
4807 ALOGE("%s: Could not get ctl for mixer cmd - %s",
4808 __func__, mixer_ctl_name);
4809 return -EINVAL;
4810 }
4811 ALOGE("%s:ctl for mixer cmd - %s, left %f, right %f",
4812 __func__, mixer_ctl_name, left, right);
4813 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
4814 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
4815 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
4816
4817 return 0;
4818}
4819
Zhou Song2b8f28f2017-09-11 10:51:38 +08004820static int out_set_voip_volume(struct audio_stream_out *stream, float left,
4821 float right)
4822{
4823 struct stream_out *out = (struct stream_out *)stream;
4824 char mixer_ctl_name[] = "App Type Gain";
4825 struct audio_device *adev = out->dev;
4826 struct mixer_ctl *ctl;
Manish Dewangan338c50a2017-09-12 15:22:03 +05304827 long set_values[4];
Zhou Song2b8f28f2017-09-11 10:51:38 +08004828
Aniket Kumar Lata8426d1f2019-06-10 15:25:53 -07004829 if (!is_valid_volume(left, right)) {
4830 ALOGE("%s: Invalid stream volume for left=%f, right=%f",
4831 __func__, left, right);
4832 return -EINVAL;
4833 }
4834
Zhou Song2b8f28f2017-09-11 10:51:38 +08004835 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
4836 if (!ctl) {
4837 ALOGE("%s: Could not get ctl for mixer cmd - %s",
4838 __func__, mixer_ctl_name);
4839 return -EINVAL;
4840 }
4841
4842 set_values[0] = 0; //0: Rx Session 1:Tx Session
4843 set_values[1] = out->app_type_cfg.app_type;
Manish Dewangan338c50a2017-09-12 15:22:03 +05304844 set_values[2] = (long)(left * VOIP_PLAYBACK_VOLUME_MAX);
4845 set_values[3] = (long)(right * VOIP_PLAYBACK_VOLUME_MAX);
Zhou Song2b8f28f2017-09-11 10:51:38 +08004846
4847 mixer_ctl_set_array(ctl, set_values, ARRAY_SIZE(set_values));
4848 return 0;
4849}
4850
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05304851static int out_set_pcm_volume(struct audio_stream_out *stream, float left,
4852 float right)
4853{
4854 struct stream_out *out = (struct stream_out *)stream;
4855 /* Volume control for pcm playback */
4856 if (left != right) {
4857 return -EINVAL;
4858 } else {
4859 char mixer_ctl_name[128];
4860 struct audio_device *adev = out->dev;
4861 struct mixer_ctl *ctl;
4862 int pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
4863 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name), "Playback %d Volume", pcm_device_id);
4864 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
4865 if (!ctl) {
4866 ALOGE("%s : Could not get ctl for mixer cmd - %s", __func__, mixer_ctl_name);
4867 return -EINVAL;
4868 }
4869
4870 int volume = (int) (left * PCM_PLAYBACK_VOLUME_MAX);
4871 int ret = mixer_ctl_set_value(ctl, 0, volume);
4872 if (ret < 0) {
4873 ALOGE("%s: Could not set ctl, error:%d ", __func__, ret);
4874 return -EINVAL;
4875 }
4876
4877 ALOGV("%s : Pcm set volume value %d left %f", __func__, volume, left);
4878
4879 return 0;
4880 }
4881}
4882
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004883static int out_set_volume(struct audio_stream_out *stream, float left,
4884 float right)
4885{
Eric Laurenta9024de2013-04-04 09:19:12 -07004886 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004887 int volume[2];
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304888 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004889
Arun Mirpuri5d170872019-03-26 13:21:31 -07004890 ALOGD("%s: called with left_vol=%f, right_vol=%f", __func__, left, right);
Eric Laurenta9024de2013-04-04 09:19:12 -07004891 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
4892 /* only take left channel into account: the API is for stereo anyway */
4893 out->muted = (left == 0.0f);
4894 return 0;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07004895 } else if (is_offload_usecase(out->usecase)) {
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05304896 if (audio_extn_passthru_is_passthrough_stream(out)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004897 /*
4898 * Set mute or umute on HDMI passthrough stream.
4899 * Only take left channel into account.
4900 * Mute is 0 and unmute 1
4901 */
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05304902 audio_extn_passthru_set_volume(out, (left == 0.0f));
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05304903 } else if (out->format == AUDIO_FORMAT_DSD){
4904 char mixer_ctl_name[128] = "DSD Volume";
4905 struct audio_device *adev = out->dev;
4906 struct mixer_ctl *ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
4907
4908 if (!ctl) {
4909 ALOGE("%s: Could not get ctl for mixer cmd - %s",
4910 __func__, mixer_ctl_name);
4911 return -EINVAL;
4912 }
Manish Dewangan338c50a2017-09-12 15:22:03 +05304913 volume[0] = (long)(AmpToDb(left));
4914 volume[1] = (long)(AmpToDb(right));
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05304915 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
4916 return 0;
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004917 } else {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304918 pthread_mutex_lock(&out->compr_mute_lock);
Arun Mirpuri5d170872019-03-26 13:21:31 -07004919 ALOGV("%s: compress mute %d", __func__, out->a2dp_compress_mute);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304920 if (!out->a2dp_compress_mute)
4921 ret = out_set_compr_volume(stream, left, right);
4922 out->volume_l = left;
4923 out->volume_r = right;
4924 pthread_mutex_unlock(&out->compr_mute_lock);
4925 return ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004926 }
Vikram Panduranga93f080e2017-06-07 18:16:14 -07004927 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP) {
Aalique Grahame22e49102018-12-18 14:23:57 -08004928 out->app_type_cfg.gain[0] = (int)(left * VOIP_PLAYBACK_VOLUME_MAX);
4929 out->app_type_cfg.gain[1] = (int)(right * VOIP_PLAYBACK_VOLUME_MAX);
4930 if (!out->standby) {
4931 audio_extn_utils_send_app_type_gain(out->dev,
4932 out->app_type_cfg.app_type,
4933 &out->app_type_cfg.gain[0]);
Zhou Song2b8f28f2017-09-11 10:51:38 +08004934 ret = out_set_voip_volume(stream, left, right);
Aalique Grahame22e49102018-12-18 14:23:57 -08004935 }
Zhou Song2b8f28f2017-09-11 10:51:38 +08004936 out->volume_l = left;
4937 out->volume_r = right;
4938 return ret;
Arun Mirpuri5d170872019-03-26 13:21:31 -07004939 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
4940 ALOGV("%s: MMAP set volume called", __func__);
4941 if (!out->standby)
4942 ret = out_set_mmap_volume(stream, left, right);
4943 out->volume_l = left;
4944 out->volume_r = right;
4945 return ret;
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05304946 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_LOW_LATENCY ||
Ramu Gottipati36547092018-12-28 11:32:09 +05304947 out->usecase == USECASE_AUDIO_PLAYBACK_DEEP_BUFFER ||
4948 out->usecase == USECASE_AUDIO_PLAYBACK_ULL) {
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05304949 /* Volume control for pcm playback */
4950 if (!out->standby)
4951 ret = out_set_pcm_volume(stream, left, right);
4952 else
4953 out->apply_volume = true;
4954
4955 out->volume_l = left;
4956 out->volume_r = right;
4957 return ret;
Eric Laurenta9024de2013-04-04 09:19:12 -07004958 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004959
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004960 return -ENOSYS;
4961}
4962
Zhou Songc9672822017-08-16 16:01:39 +08004963static void update_frames_written(struct stream_out *out, size_t bytes)
4964{
4965 size_t bpf = 0;
4966
4967 if (is_offload_usecase(out->usecase) && !out->non_blocking &&
4968 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD))
4969 bpf = 1;
4970 else if (!is_offload_usecase(out->usecase))
4971 bpf = audio_bytes_per_sample(out->format) *
4972 audio_channel_count_from_out_mask(out->channel_mask);
Zhou Song48453a02018-01-10 17:50:59 +08004973
4974 pthread_mutex_lock(&out->position_query_lock);
4975 if (bpf != 0) {
Zhou Songc9672822017-08-16 16:01:39 +08004976 out->written += bytes / bpf;
Zhou Song48453a02018-01-10 17:50:59 +08004977 clock_gettime(CLOCK_MONOTONIC, &out->writeAt);
4978 }
4979 pthread_mutex_unlock(&out->position_query_lock);
Zhou Songc9672822017-08-16 16:01:39 +08004980}
4981
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004982int split_and_write_audio_haptic_data(struct stream_out *out,
4983 const void *buffer, size_t bytes_to_write)
4984{
4985 struct audio_device *adev = out->dev;
4986
4987 int ret = 0;
4988 size_t channel_count = audio_channel_count_from_out_mask(out->channel_mask);
4989 size_t bytes_per_sample = audio_bytes_per_sample(out->format);
4990 size_t frame_size = channel_count * bytes_per_sample;
4991 size_t frame_count = bytes_to_write / frame_size;
4992
4993 bool force_haptic_path =
4994 property_get_bool("vendor.audio.test_haptic", false);
4995
4996 // extract Haptics data from Audio buffer
4997 bool alloc_haptic_buffer = false;
4998 int haptic_channel_count = adev->haptics_config.channels;
4999 size_t haptic_frame_size = bytes_per_sample * haptic_channel_count;
5000 size_t audio_frame_size = frame_size - haptic_frame_size;
5001 size_t total_haptic_buffer_size = frame_count * haptic_frame_size;
5002
5003 if (adev->haptic_buffer == NULL) {
5004 alloc_haptic_buffer = true;
5005 } else if (adev->haptic_buffer_size < total_haptic_buffer_size) {
5006 free(adev->haptic_buffer);
5007 adev->haptic_buffer_size = 0;
5008 alloc_haptic_buffer = true;
5009 }
5010
5011 if (alloc_haptic_buffer) {
5012 adev->haptic_buffer = (uint8_t *)calloc(1, total_haptic_buffer_size);
Mingshu Pang1513f972019-05-24 12:43:51 +08005013 if(adev->haptic_buffer == NULL) {
5014 ALOGE("%s: failed to allocate mem for dev->haptic_buffer", __func__);
5015 return -ENOMEM;
5016 }
Vignesh Kulothungana6927272019-02-20 15:17:07 -08005017 adev->haptic_buffer_size = total_haptic_buffer_size;
5018 }
5019
5020 size_t src_index = 0, aud_index = 0, hap_index = 0;
5021 uint8_t *audio_buffer = (uint8_t *)buffer;
5022 uint8_t *haptic_buffer = adev->haptic_buffer;
5023
5024 // This is required for testing only. This works for stereo data only.
5025 // One channel is fed to audio stream and other to haptic stream for testing.
5026 if (force_haptic_path)
5027 audio_frame_size = haptic_frame_size = bytes_per_sample;
5028
5029 for (size_t i = 0; i < frame_count; i++) {
5030 memcpy(audio_buffer + aud_index, audio_buffer + src_index,
5031 audio_frame_size);
5032 aud_index += audio_frame_size;
5033 src_index += audio_frame_size;
5034
5035 if (adev->haptic_pcm)
5036 memcpy(haptic_buffer + hap_index, audio_buffer + src_index,
5037 haptic_frame_size);
5038 hap_index += haptic_frame_size;
5039 src_index += haptic_frame_size;
5040
5041 // This is required for testing only.
5042 // Discard haptic channel data.
5043 if (force_haptic_path)
5044 src_index += haptic_frame_size;
5045 }
5046
5047 // write to audio pipeline
5048 ret = pcm_write(out->pcm, (void *)audio_buffer,
5049 frame_count * audio_frame_size);
5050
5051 // write to haptics pipeline
5052 if (adev->haptic_pcm)
5053 ret = pcm_write(adev->haptic_pcm, (void *)adev->haptic_buffer,
5054 frame_count * haptic_frame_size);
5055
5056 return ret;
5057}
5058
Aalique Grahame22e49102018-12-18 14:23:57 -08005059#ifdef NO_AUDIO_OUT
5060static ssize_t out_write_for_no_output(struct audio_stream_out *stream,
5061 const void *buffer __unused, size_t bytes)
5062{
5063 struct stream_out *out = (struct stream_out *)stream;
5064
5065 /* No Output device supported other than BT for playback.
5066 * Sleep for the amount of buffer duration
5067 */
5068 lock_output_stream(out);
5069 usleep(bytes * 1000000 / audio_stream_out_frame_size(
5070 (const struct audio_stream_out *)&out->stream) /
5071 out_get_sample_rate(&out->stream.common));
5072 pthread_mutex_unlock(&out->lock);
5073 return bytes;
5074}
5075#endif
5076
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005077static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
5078 size_t bytes)
5079{
5080 struct stream_out *out = (struct stream_out *)stream;
5081 struct audio_device *adev = out->dev;
Eric Laurent6e895242013-09-05 16:10:57 -07005082 ssize_t ret = 0;
Satish Babu Patakokila715b1422017-08-22 14:33:21 +05305083 int channels = 0;
Arun Mirpuri7da752a2018-09-11 18:01:15 -07005084 const size_t frame_size = audio_stream_out_frame_size(stream);
5085 const size_t frames = (frame_size != 0) ? bytes / frame_size : bytes;
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +05305086 struct audio_usecase *usecase = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005087
Haynes Mathew George380745d2017-10-04 15:27:45 -07005088 ATRACE_BEGIN("out_write");
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005089 lock_output_stream(out);
Naresh Tanniru4c630392014-05-12 01:05:52 +05305090
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305091 if (CARD_STATUS_OFFLINE == out->card_status) {
Zhou Song0b2e5dc2015-03-16 14:41:38 +08005092
Dhananjay Kumarac341582017-02-23 23:42:25 +05305093 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Ashish Jainbbce4322016-02-16 13:25:27 +05305094 /*during SSR for compress usecase we should return error to flinger*/
Naresh Tanniru80659832014-06-04 18:17:56 +05305095 ALOGD(" copl %s: sound card is not active/SSR state", __func__);
5096 pthread_mutex_unlock(&out->lock);
Haynes Mathew George380745d2017-10-04 15:27:45 -07005097 ATRACE_END();
Naresh Tanniru80659832014-06-04 18:17:56 +05305098 return -ENETRESET;
Ashish Jainbbce4322016-02-16 13:25:27 +05305099 } else {
Ashish Jainbbce4322016-02-16 13:25:27 +05305100 ALOGD(" %s: sound card is not active/SSR state", __func__);
5101 ret= -EIO;
5102 goto exit;
Naresh Tanniru4c630392014-05-12 01:05:52 +05305103 }
5104 }
5105
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05305106 if (audio_extn_passthru_should_drop_data(out)) {
Ashish Jaind84fd6a2016-07-27 12:33:25 +05305107 ALOGV(" %s : Drop data as compress passthrough session is going on", __func__);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05305108 ret = -EIO;
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05305109 goto exit;
5110 }
5111
Haynes Mathew George16081042017-05-31 17:16:49 -07005112 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
5113 ret = -EINVAL;
5114 goto exit;
5115 }
5116
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05305117 if ((out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) &&
5118 !out->is_iec61937_info_available) {
5119
5120 if (!audio_extn_passthru_is_passthrough_stream(out)) {
5121 out->is_iec61937_info_available = true;
5122 } else if (audio_extn_passthru_is_enabled()) {
5123 audio_extn_passthru_update_stream_configuration(adev, out, buffer, bytes);
Manish Dewangan37864bc2017-06-09 12:28:37 +05305124 out->is_iec61937_info_available = true;
Manish Dewangan671a4202017-08-18 17:30:46 +05305125
5126 if((out->format == AUDIO_FORMAT_DTS) ||
5127 (out->format == AUDIO_FORMAT_DTS_HD)) {
5128 ret = audio_extn_passthru_update_dts_stream_configuration(out,
5129 buffer, bytes);
5130 if (ret) {
5131 if (ret != -ENOSYS) {
5132 out->is_iec61937_info_available = false;
5133 ALOGD("iec61937 transmission info not yet updated retry");
5134 }
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05305135 } else if (!out->standby) {
Manish Dewangan671a4202017-08-18 17:30:46 +05305136 /* if stream has started and after that there is
5137 * stream config change (iec transmission config)
5138 * then trigger select_device to update backend configuration.
5139 */
5140 out->stream_config_changed = true;
5141 pthread_mutex_lock(&adev->lock);
5142 select_devices(adev, out->usecase);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05305143 if (!audio_extn_passthru_is_supported_backend_edid_cfg(adev, out)) {
Weiyin Jiang29c08a42019-04-30 17:11:10 +08005144 pthread_mutex_unlock(&adev->lock);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05305145 ret = -EINVAL;
5146 goto exit;
5147 }
Manish Dewangan671a4202017-08-18 17:30:46 +05305148 pthread_mutex_unlock(&adev->lock);
5149 out->stream_config_changed = false;
5150 out->is_iec61937_info_available = true;
5151 }
5152 }
Satish Babu Patakokila715b1422017-08-22 14:33:21 +05305153
Garmond Leung317cbf12017-09-13 16:20:50 -07005154 if ((channels < (int)audio_channel_count_from_out_mask(out->channel_mask)) &&
Satish Babu Patakokila715b1422017-08-22 14:33:21 +05305155 (out->compr_config.codec->compr_passthr == PASSTHROUGH) &&
5156 (out->is_iec61937_info_available == true)) {
5157 ALOGE("%s: ERROR: Unsupported channel config in passthrough mode", __func__);
5158 ret = -EINVAL;
5159 goto exit;
5160 }
Manish Dewangan37864bc2017-06-09 12:28:37 +05305161 }
5162 }
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05305163
5164 if ((out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) &&
Florian Pfister1a84f312018-07-19 14:38:18 +02005165 (audio_extn_a2dp_source_is_suspended())) {
Aalique Grahame22e49102018-12-18 14:23:57 -08005166 if (!(out->devices &
5167 (AUDIO_DEVICE_OUT_SPEAKER | AUDIO_DEVICE_OUT_SPEAKER_SAFE))) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05305168 if (!(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05305169 ret = -EIO;
5170 goto exit;
5171 }
5172 }
5173 }
5174
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005175 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07005176 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08005177 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08005178 if (out->usecase == USECASE_COMPRESS_VOIP_CALL)
5179 ret = voice_extn_compress_voip_start_output_stream(out);
5180 else
5181 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08005182 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005183 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005184 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07005185 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005186 goto exit;
5187 }
Ashish Jain1b9b30c2017-05-18 20:57:40 +05305188 out->started = 1;
vivek mehtab72d08d2016-04-29 03:16:47 -07005189 if (last_known_cal_step != -1) {
5190 ALOGD("%s: retry previous failed cal level set", __func__);
5191 audio_hw_send_gain_dep_calibration(last_known_cal_step);
Preetam Singh Ranawatf4ae0222017-05-31 17:07:28 +05305192 last_known_cal_step = -1;
vivek mehtab72d08d2016-04-29 03:16:47 -07005193 }
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05305194
5195 if ((out->is_iec61937_info_available == true) &&
5196 (audio_extn_passthru_is_passthrough_stream(out))&&
5197 (!audio_extn_passthru_is_supported_backend_edid_cfg(adev, out))) {
5198 ret = -EINVAL;
5199 goto exit;
5200 }
Surendar Karkaf51b5842018-04-26 11:28:38 +05305201 if (out->set_dual_mono)
5202 audio_extn_send_dual_mono_mixing_coefficients(out);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005203 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005204
Ashish Jain81eb2a82015-05-13 10:52:34 +05305205 if (adev->is_channel_status_set == false && (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)){
Alexy Josephb1379942016-01-29 15:49:38 -08005206 audio_utils_set_hdmi_channel_status(out, (void *)buffer, bytes);
Ashish Jain81eb2a82015-05-13 10:52:34 +05305207 adev->is_channel_status_set = true;
5208 }
5209
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +05305210 if ((adev->use_old_pspd_mix_ctrl == true) &&
5211 (out->pspd_coeff_sent == false)) {
5212 /*
5213 * Need to resend pspd coefficients after stream started for
5214 * older kernel version as it does not save the coefficients
5215 * and also stream has to be started for coeff to apply.
5216 */
5217 usecase = get_usecase_from_list(adev, out->usecase);
5218 if (usecase != NULL) {
5219 audio_extn_set_custom_mtmx_params(adev, usecase, true);
5220 out->pspd_coeff_sent = true;
5221 }
5222 }
5223
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07005224 if (is_offload_usecase(out->usecase)) {
Alexy Joseph01e54e62015-03-03 19:01:03 -08005225 ALOGVV("copl(%p): writing buffer (%zu bytes) to compress device", out, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005226 if (out->send_new_metadata) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07005227 ALOGD("copl(%p):send new gapless metadata", out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005228 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
5229 out->send_new_metadata = 0;
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05305230 if (out->send_next_track_params && out->is_compr_metadata_avail) {
5231 ALOGD("copl(%p):send next track params in gapless", out);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08005232 // compress_set_next_track_param(out->compr, &(out->compr_config.codec->options));
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05305233 out->send_next_track_params = false;
5234 out->is_compr_metadata_avail = false;
5235 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005236 }
Dhananjay Kumarac341582017-02-23 23:42:25 +05305237 if (!(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
Ashish Jain83a6cc22016-06-28 14:34:17 +05305238 (out->convert_buffer) != NULL) {
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005239
Ashish Jain83a6cc22016-06-28 14:34:17 +05305240 if ((bytes > out->hal_fragment_size)) {
Ashish Jainf1eaa582016-05-23 20:54:24 +05305241 ALOGW("Error written bytes %zu > %d (fragment_size)",
Ashish Jain83a6cc22016-06-28 14:34:17 +05305242 bytes, out->hal_fragment_size);
Ashish Jainf1eaa582016-05-23 20:54:24 +05305243 pthread_mutex_unlock(&out->lock);
Haynes Mathew George380745d2017-10-04 15:27:45 -07005244 ATRACE_END();
Ashish Jainf1eaa582016-05-23 20:54:24 +05305245 return -EINVAL;
5246 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05305247 audio_format_t dst_format = out->hal_op_format;
5248 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05305249
Dieter Luecking5d57def2018-09-07 14:23:37 +02005250 /* prevent division-by-zero */
5251 uint32_t bitwidth_src = format_to_bitwidth_table[src_format];
5252 uint32_t bitwidth_dst = format_to_bitwidth_table[dst_format];
5253 if ((bitwidth_src == 0) || (bitwidth_dst == 0)) {
5254 ALOGE("%s: Error bitwidth == 0", __func__);
Ramu Gottipati02809682018-12-19 16:46:12 +05305255 pthread_mutex_unlock(&out->lock);
Dieter Luecking5d57def2018-09-07 14:23:37 +02005256 ATRACE_END();
5257 return -EINVAL;
5258 }
5259
Ashish Jainf1eaa582016-05-23 20:54:24 +05305260 uint32_t frames = bytes / format_to_bitwidth_table[src_format];
5261 uint32_t bytes_to_write = frames * format_to_bitwidth_table[dst_format];
5262
Ashish Jain83a6cc22016-06-28 14:34:17 +05305263 memcpy_by_audio_format(out->convert_buffer,
Ashish Jainf1eaa582016-05-23 20:54:24 +05305264 dst_format,
5265 buffer,
5266 src_format,
5267 frames);
5268
Ashish Jain83a6cc22016-06-28 14:34:17 +05305269 ret = compress_write(out->compr, out->convert_buffer,
Ashish Jainf1eaa582016-05-23 20:54:24 +05305270 bytes_to_write);
5271
5272 /*Convert written bytes in audio flinger format*/
5273 if (ret > 0)
5274 ret = ((ret * format_to_bitwidth_table[out->format]) /
5275 format_to_bitwidth_table[dst_format]);
5276 }
5277 } else
5278 ret = compress_write(out->compr, buffer, bytes);
5279
Zhou Songc9672822017-08-16 16:01:39 +08005280 if ((ret < 0 || ret == (ssize_t)bytes) && !out->non_blocking)
5281 update_frames_written(out, bytes);
5282
Dhanalakshmi Siddani37ca1d62014-08-20 12:28:34 +05305283 if (ret < 0)
5284 ret = -errno;
Weiyin Jiangcc60dbb2018-08-21 13:12:03 +08005285 ALOGVV("%s: writing buffer (%zu bytes) to compress device returned %d", __func__, bytes, (int)ret);
Ashish Jainb26edfb2016-08-25 00:10:11 +05305286 /*msg to cb thread only if non blocking write is enabled*/
5287 if (ret >= 0 && ret < (ssize_t)bytes && out->non_blocking) {
Sidipotu Ashok55820562014-02-10 16:16:38 +05305288 ALOGD("No space available in compress driver, post msg to cb thread");
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005289 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
Naresh Tanniru80659832014-06-04 18:17:56 +05305290 } else if (-ENETRESET == ret) {
5291 ALOGE("copl %s: received sound card offline state on compress write", __func__);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305292 out->card_status = CARD_STATUS_OFFLINE;
Naresh Tanniru80659832014-06-04 18:17:56 +05305293 pthread_mutex_unlock(&out->lock);
Dhananjay Kumar1248dd82017-07-28 21:22:16 +05305294 out_on_error(&out->stream.common);
Haynes Mathew George380745d2017-10-04 15:27:45 -07005295 ATRACE_END();
Naresh Tanniru80659832014-06-04 18:17:56 +05305296 return ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005297 }
Ashish Jain5106d362016-05-11 19:23:33 +05305298
Dhanalakshmi Siddania6b76c72016-09-09 18:10:31 +05305299 /* Call compr start only when non-zero bytes of data is there to be rendered */
5300 if (!out->playback_started && ret > 0) {
5301 int status = compress_start(out->compr);
5302 if (status < 0) {
5303 ret = status;
5304 ALOGE("%s: compr start failed with err %d", __func__, errno);
5305 goto exit;
5306 }
Alexy Joseph7de344d2015-03-30 10:40:03 -07005307 audio_extn_dts_eagle_fade(adev, true, out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005308 out->playback_started = 1;
5309 out->offload_state = OFFLOAD_STATE_PLAYING;
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08005310
5311 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
5312 popcount(out->channel_mask),
5313 out->playback_started);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005314 }
5315 pthread_mutex_unlock(&out->lock);
Haynes Mathew George380745d2017-10-04 15:27:45 -07005316 ATRACE_END();
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005317 return ret;
5318 } else {
5319 if (out->pcm) {
Arun Mirpuri7da752a2018-09-11 18:01:15 -07005320 size_t bytes_to_write = bytes;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005321 if (out->muted)
5322 memset((void *)buffer, 0, bytes);
Arun Mirpuri7da752a2018-09-11 18:01:15 -07005323 ALOGV("%s: frames=%zu, frame_size=%zu, bytes_to_write=%zu",
5324 __func__, frames, frame_size, bytes_to_write);
5325
Aalique Grahame22e49102018-12-18 14:23:57 -08005326 if (out->usecase == USECASE_INCALL_MUSIC_UPLINK ||
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07005327 out->usecase == USECASE_INCALL_MUSIC_UPLINK2 ||
5328 (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP &&
5329 !audio_extn_utils_is_vendor_enhanced_fwk())) {
Arun Mirpuri7da752a2018-09-11 18:01:15 -07005330 size_t channel_count = audio_channel_count_from_out_mask(out->channel_mask);
5331 int16_t *src = (int16_t *)buffer;
5332 int16_t *dst = (int16_t *)buffer;
5333
5334 LOG_ALWAYS_FATAL_IF(out->config.channels != 1 || channel_count != 2 ||
5335 out->format != AUDIO_FORMAT_PCM_16_BIT,
Aalique Grahame22e49102018-12-18 14:23:57 -08005336 "out_write called for %s use case with wrong properties",
5337 use_case_table[out->usecase]);
Arun Mirpuri7da752a2018-09-11 18:01:15 -07005338
5339 /*
5340 * FIXME: this can be removed once audio flinger mixer supports
5341 * mono output
5342 */
5343
5344 /*
5345 * Code below goes over each frame in the buffer and adds both
5346 * L and R samples and then divides by 2 to convert to mono
5347 */
5348 for (size_t i = 0; i < frames ; i++, dst++, src += 2) {
5349 *dst = (int16_t)(((int32_t)src[0] + (int32_t)src[1]) >> 1);
5350 }
5351 bytes_to_write /= 2;
5352 }
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07005353
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05305354 ALOGVV("%s: writing buffer (%zu bytes) to pcm device", __func__, bytes);
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07005355
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005356 long ns = 0;
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07005357
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005358 if (out->config.rate)
5359 ns = pcm_bytes_to_frames(out->pcm, bytes)*1000000000LL/
5360 out->config.rate;
5361
Arun Mirpuri7da752a2018-09-11 18:01:15 -07005362 request_out_focus(out, ns);
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005363 bool use_mmap = is_mmap_usecase(out->usecase) || out->realtime;
5364
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005365 if (use_mmap)
Arun Mirpuri7da752a2018-09-11 18:01:15 -07005366 ret = pcm_mmap_write(out->pcm, (void *)buffer, bytes_to_write);
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005367 else if (out->hal_op_format != out->hal_ip_format &&
Ashish Jain83a6cc22016-06-28 14:34:17 +05305368 out->convert_buffer != NULL) {
5369
5370 memcpy_by_audio_format(out->convert_buffer,
5371 out->hal_op_format,
5372 buffer,
5373 out->hal_ip_format,
5374 out->config.period_size * out->config.channels);
5375
5376 ret = pcm_write(out->pcm, out->convert_buffer,
5377 (out->config.period_size *
5378 out->config.channels *
5379 format_to_bitwidth_table[out->hal_op_format]));
5380 } else {
Aditya Bavanarid4db8ee2017-05-29 21:08:03 +05305381 /*
5382 * To avoid underrun in DSP when the application is not pumping
5383 * data at required rate, check for the no. of bytes and ignore
5384 * pcm_write if it is less than actual buffer size.
5385 * It is a work around to a change in compress VOIP driver.
5386 */
5387 if ((out->flags & AUDIO_OUTPUT_FLAG_VOIP_RX) &&
5388 bytes < (out->config.period_size * out->config.channels *
5389 audio_bytes_per_sample(out->format))) {
5390 size_t voip_buf_size =
5391 out->config.period_size * out->config.channels *
5392 audio_bytes_per_sample(out->format);
5393 ALOGE("%s:VOIP underrun: bytes received %zu, required:%zu\n",
5394 __func__, bytes, voip_buf_size);
5395 usleep(((uint64_t)voip_buf_size - bytes) *
5396 1000000 / audio_stream_out_frame_size(stream) /
5397 out_get_sample_rate(&out->stream.common));
5398 ret = 0;
Vignesh Kulothungana6927272019-02-20 15:17:07 -08005399 } else {
5400 if (out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS)
5401 ret = split_and_write_audio_haptic_data(out, buffer, bytes);
5402 else
5403 ret = pcm_write(out->pcm, (void *)buffer, bytes_to_write);
5404 }
Ashish Jain83a6cc22016-06-28 14:34:17 +05305405 }
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07005406
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005407 release_out_focus(out);
5408
Dhanalakshmi Siddani37ca1d62014-08-20 12:28:34 +05305409 if (ret < 0)
5410 ret = -errno;
Zhou Songc9672822017-08-16 16:01:39 +08005411 else if (ret > 0)
Ashish Jain83a6cc22016-06-28 14:34:17 +05305412 ret = -EINVAL;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005413 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005414 }
5415
5416exit:
Zhou Songc9672822017-08-16 16:01:39 +08005417 update_frames_written(out, bytes);
Naresh Tanniru4c630392014-05-12 01:05:52 +05305418 if (-ENETRESET == ret) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305419 out->card_status = CARD_STATUS_OFFLINE;
Naresh Tanniru4c630392014-05-12 01:05:52 +05305420 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005421 pthread_mutex_unlock(&out->lock);
5422
5423 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07005424 if (out->pcm)
Alexy Josephb1379942016-01-29 15:49:38 -08005425 ALOGE("%s: error %d, %s", __func__, (int)ret, pcm_get_error(out->pcm));
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05305426 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05305427 pthread_mutex_lock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05305428 voice_extn_compress_voip_close_output_stream(&out->stream.common);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05305429 out->started = 0;
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05305430 pthread_mutex_unlock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05305431 out->standby = true;
5432 }
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305433 out_on_error(&out->stream.common);
Dieter Luecking5d57def2018-09-07 14:23:37 +02005434 if (!(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
5435 /* prevent division-by-zero */
5436 uint32_t stream_size = audio_stream_out_frame_size(stream);
5437 uint32_t srate = out_get_sample_rate(&out->stream.common);
Vidyakumar Athotaa9d3a5f2017-08-09 12:13:05 -07005438
Dieter Luecking5d57def2018-09-07 14:23:37 +02005439 if ((stream_size == 0) || (srate == 0)) {
5440 ALOGE("%s: stream_size= %d, srate = %d", __func__, stream_size, srate);
5441 ATRACE_END();
5442 return -EINVAL;
5443 }
5444 usleep((uint64_t)bytes * 1000000 / stream_size / srate);
5445 }
Vidyakumar Athotaa9d3a5f2017-08-09 12:13:05 -07005446 if (audio_extn_passthru_is_passthrough_stream(out)) {
Rajshekar Eashwarappa88834522018-04-02 17:20:15 +05305447 //ALOGE("%s: write error, ret = %zd", __func__, ret);
Haynes Mathew George380745d2017-10-04 15:27:45 -07005448 ATRACE_END();
Vidyakumar Athotaa9d3a5f2017-08-09 12:13:05 -07005449 return ret;
5450 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005451 }
Haynes Mathew George380745d2017-10-04 15:27:45 -07005452 ATRACE_END();
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005453 return bytes;
5454}
5455
5456static int out_get_render_position(const struct audio_stream_out *stream,
5457 uint32_t *dsp_frames)
5458{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005459 struct stream_out *out = (struct stream_out *)stream;
Zhou Song32a556e2015-05-05 10:46:56 +08005460
5461 if (dsp_frames == NULL)
5462 return -EINVAL;
5463
5464 *dsp_frames = 0;
5465 if (is_offload_usecase(out->usecase)) {
Mingming Yin9e348b52014-11-19 16:18:55 -08005466 ssize_t ret = 0;
Ashish Jain5106d362016-05-11 19:23:33 +05305467
5468 /* Below piece of code is not guarded against any lock beacuse audioFliner serializes
5469 * this operation and adev_close_output_stream(where out gets reset).
5470 */
Dhananjay Kumarac341582017-02-23 23:42:25 +05305471 if (!out->non_blocking && !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Zhou Song48453a02018-01-10 17:50:59 +08005472 *dsp_frames = get_actual_pcm_frames_rendered(out, NULL);
Ashish Jain5106d362016-05-11 19:23:33 +05305473 ALOGVV("dsp_frames %d sampleRate %d",(int)*dsp_frames,out->sample_rate);
Haynes Mathew Georgeb0f5dc32017-10-06 18:35:12 -07005474 adjust_frames_for_device_delay(out, dsp_frames);
Ashish Jain5106d362016-05-11 19:23:33 +05305475 return 0;
5476 }
5477
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005478 lock_output_stream(out);
Ashish Jain5106d362016-05-11 19:23:33 +05305479 if (out->compr != NULL && out->non_blocking) {
Naresh Tanniru80659832014-06-04 18:17:56 +05305480 ret = compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005481 &out->sample_rate);
Dhanalakshmi Siddani37ca1d62014-08-20 12:28:34 +05305482 if (ret < 0)
5483 ret = -errno;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005484 ALOGVV("%s rendered frames %d sample_rate %d",
Ashish Jain5106d362016-05-11 19:23:33 +05305485 __func__, *dsp_frames, out->sample_rate);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005486 }
Naresh Tanniru80659832014-06-04 18:17:56 +05305487 if (-ENETRESET == ret) {
5488 ALOGE(" ERROR: sound card not active Unable to get time stamp from compress driver");
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305489 out->card_status = CARD_STATUS_OFFLINE;
5490 ret = -EINVAL;
Naresh Tanniru80659832014-06-04 18:17:56 +05305491 } else if(ret < 0) {
5492 ALOGE(" ERROR: Unable to get time stamp from compress driver");
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305493 ret = -EINVAL;
5494 } else if (out->card_status == CARD_STATUS_OFFLINE) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05305495 /*
5496 * Handle corner case where compress session is closed during SSR
5497 * and timestamp is queried
5498 */
5499 ALOGE(" ERROR: sound card not active, return error");
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305500 ret = -EINVAL;
Manisha Agarwal7b3e3772019-02-20 14:33:45 +05305501 } else if (out->prev_card_status_offline) {
5502 ALOGE("ERROR: previously sound card was offline,return error");
5503 ret = -EINVAL;
Naresh Tanniru80659832014-06-04 18:17:56 +05305504 } else {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305505 ret = 0;
Haynes Mathew Georgeb0f5dc32017-10-06 18:35:12 -07005506 adjust_frames_for_device_delay(out, dsp_frames);
Naresh Tanniru80659832014-06-04 18:17:56 +05305507 }
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305508 pthread_mutex_unlock(&out->lock);
5509 return ret;
Zhou Song32a556e2015-05-05 10:46:56 +08005510 } else if (audio_is_linear_pcm(out->format)) {
5511 *dsp_frames = out->written;
Haynes Mathew Georgeb0f5dc32017-10-06 18:35:12 -07005512 adjust_frames_for_device_delay(out, dsp_frames);
Zhou Song32a556e2015-05-05 10:46:56 +08005513 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005514 } else
5515 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005516}
5517
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07005518static int out_add_audio_effect(const struct audio_stream *stream __unused,
5519 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005520{
5521 return 0;
5522}
5523
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07005524static int out_remove_audio_effect(const struct audio_stream *stream __unused,
5525 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005526{
5527 return 0;
5528}
5529
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07005530static int out_get_next_write_timestamp(const struct audio_stream_out *stream __unused,
5531 int64_t *timestamp __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005532{
Satya Krishna Pindiprolib6655542017-07-03 19:38:19 +05305533 return -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005534}
5535
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07005536static int out_get_presentation_position(const struct audio_stream_out *stream,
5537 uint64_t *frames, struct timespec *timestamp)
5538{
5539 struct stream_out *out = (struct stream_out *)stream;
pavance65c2fe2017-10-18 17:52:01 +05305540 int ret = -ENODATA;
Eric Laurent949a0892013-09-20 09:20:13 -07005541 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07005542
Ashish Jain5106d362016-05-11 19:23:33 +05305543 /* below piece of code is not guarded against any lock because audioFliner serializes
5544 * this operation and adev_close_output_stream( where out gets reset).
5545 */
5546 if (is_offload_usecase(out->usecase) && !out->non_blocking &&
Dhananjay Kumarac341582017-02-23 23:42:25 +05305547 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Zhou Song48453a02018-01-10 17:50:59 +08005548 *frames = get_actual_pcm_frames_rendered(out, timestamp);
Ashish Jain5106d362016-05-11 19:23:33 +05305549 ALOGVV("frames %lld playedat %lld",(long long int)*frames,
5550 timestamp->tv_sec * 1000000LL + timestamp->tv_nsec / 1000);
5551 return 0;
5552 }
5553
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005554 lock_output_stream(out);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07005555
Ashish Jain5106d362016-05-11 19:23:33 +05305556 if (is_offload_usecase(out->usecase) && out->compr != NULL && out->non_blocking) {
5557 ret = compress_get_tstamp(out->compr, &dsp_frames,
5558 &out->sample_rate);
yidongh0515e042017-07-06 15:00:34 +08005559 // Adjustment accounts for A2dp encoder latency with offload usecases
5560 // Note: Encoder latency is returned in ms.
5561 if (AUDIO_DEVICE_OUT_ALL_A2DP & out->devices) {
5562 unsigned long offset =
5563 (audio_extn_a2dp_get_encoder_latency() * out->sample_rate / 1000);
5564 dsp_frames = (dsp_frames > offset) ? (dsp_frames - offset) : 0;
5565 }
Ashish Jain5106d362016-05-11 19:23:33 +05305566 ALOGVV("%s rendered frames %ld sample_rate %d",
5567 __func__, dsp_frames, out->sample_rate);
5568 *frames = dsp_frames;
5569 if (ret < 0)
5570 ret = -errno;
5571 if (-ENETRESET == ret) {
5572 ALOGE(" ERROR: sound card not active Unable to get time stamp from compress driver");
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305573 out->card_status = CARD_STATUS_OFFLINE;
Ashish Jain5106d362016-05-11 19:23:33 +05305574 ret = -EINVAL;
5575 } else
5576 ret = 0;
5577 /* this is the best we can do */
5578 clock_gettime(CLOCK_MONOTONIC, timestamp);
Eric Laurent949a0892013-09-20 09:20:13 -07005579 } else {
5580 if (out->pcm) {
Weiyin Jiangd4633762018-03-16 12:05:03 +08005581 unsigned int avail;
5582 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
5583 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
5584 int64_t signed_frames = out->written - kernel_buffer_size + avail;
5585 // This adjustment accounts for buffering after app processor.
5586 // It is based on estimated DSP latency per use case, rather than exact.
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07005587 signed_frames -=
Weiyin Jiangd4633762018-03-16 12:05:03 +08005588 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
Aniket Kumar Lataff613152017-07-18 18:19:21 -07005589
Weiyin Jiangd4633762018-03-16 12:05:03 +08005590 // Adjustment accounts for A2dp encoder latency with non offload usecases
5591 // Note: Encoder latency is returned in ms, while platform_render_latency in us.
5592 if (AUDIO_DEVICE_OUT_ALL_A2DP & out->devices) {
5593 signed_frames -=
5594 (audio_extn_a2dp_get_encoder_latency() * out->sample_rate / 1000);
5595 }
5596
5597 // It would be unusual for this value to be negative, but check just in case ...
5598 if (signed_frames >= 0) {
5599 *frames = signed_frames;
5600 ret = 0;
5601 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07005602 }
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305603 } else if (out->card_status == CARD_STATUS_OFFLINE) {
Ashish Jainbbce4322016-02-16 13:25:27 +05305604 *frames = out->written;
5605 clock_gettime(CLOCK_MONOTONIC, timestamp);
Manisha Agarwal7b3e3772019-02-20 14:33:45 +05305606 if (is_offload_usecase(out->usecase))
5607 ret = -EINVAL;
5608 else
5609 ret = 0;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07005610 }
5611 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07005612 pthread_mutex_unlock(&out->lock);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07005613 return ret;
5614}
5615
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005616static int out_set_callback(struct audio_stream_out *stream,
5617 stream_callback_t callback, void *cookie)
5618{
5619 struct stream_out *out = (struct stream_out *)stream;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08005620 int ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005621
5622 ALOGV("%s", __func__);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005623 lock_output_stream(out);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08005624 out->client_callback = callback;
5625 out->client_cookie = cookie;
5626 if (out->adsp_hdlr_stream_handle) {
5627 ret = audio_extn_adsp_hdlr_stream_set_callback(
5628 out->adsp_hdlr_stream_handle,
5629 callback,
5630 cookie);
5631 if (ret)
5632 ALOGW("%s:adsp hdlr callback registration failed %d",
5633 __func__, ret);
5634 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005635 pthread_mutex_unlock(&out->lock);
5636 return 0;
5637}
5638
5639static int out_pause(struct audio_stream_out* stream)
5640{
5641 struct stream_out *out = (struct stream_out *)stream;
5642 int status = -ENOSYS;
5643 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07005644 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07005645 ALOGD("copl(%p):pause compress driver", out);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005646 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005647 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305648 if (out->card_status != CARD_STATUS_OFFLINE)
Naresh Tanniru80659832014-06-04 18:17:56 +05305649 status = compress_pause(out->compr);
5650
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005651 out->offload_state = OFFLOAD_STATE_PAUSED;
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08005652
Mingming Yin21854652016-04-13 11:54:02 -07005653 if (audio_extn_passthru_is_active()) {
5654 ALOGV("offload use case, pause passthru");
5655 audio_extn_passthru_on_pause(out);
5656 }
5657
Dhanalakshmi Siddani79415e72015-03-23 11:54:47 +05305658 audio_extn_dts_eagle_fade(adev, false, out);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08005659 audio_extn_dts_notify_playback_state(out->usecase, 0,
5660 out->sample_rate, popcount(out->channel_mask),
5661 0);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005662 }
5663 pthread_mutex_unlock(&out->lock);
5664 }
5665 return status;
5666}
5667
5668static int out_resume(struct audio_stream_out* stream)
5669{
5670 struct stream_out *out = (struct stream_out *)stream;
5671 int status = -ENOSYS;
5672 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07005673 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07005674 ALOGD("copl(%p):resume compress driver", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005675 status = 0;
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005676 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005677 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305678 if (out->card_status != CARD_STATUS_OFFLINE) {
Naresh Tanniru80659832014-06-04 18:17:56 +05305679 status = compress_resume(out->compr);
Mingming Yin21854652016-04-13 11:54:02 -07005680 }
5681 if (!status) {
5682 out->offload_state = OFFLOAD_STATE_PLAYING;
5683 }
Dhanalakshmi Siddani79415e72015-03-23 11:54:47 +05305684 audio_extn_dts_eagle_fade(adev, true, out);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08005685 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
5686 popcount(out->channel_mask), 1);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005687 }
5688 pthread_mutex_unlock(&out->lock);
5689 }
5690 return status;
5691}
5692
5693static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
5694{
5695 struct stream_out *out = (struct stream_out *)stream;
5696 int status = -ENOSYS;
5697 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07005698 if (is_offload_usecase(out->usecase)) {
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005699 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005700 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
5701 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
5702 else
5703 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
5704 pthread_mutex_unlock(&out->lock);
5705 }
5706 return status;
5707}
5708
5709static int out_flush(struct audio_stream_out* stream)
5710{
5711 struct stream_out *out = (struct stream_out *)stream;
5712 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07005713 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07005714 ALOGD("copl(%p):calling compress flush", out);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005715 lock_output_stream(out);
Haynes Mathew Georgeafe54d82016-09-21 14:39:19 -07005716 if (out->offload_state == OFFLOAD_STATE_PAUSED) {
5717 stop_compressed_output_l(out);
Haynes Mathew Georgeafe54d82016-09-21 14:39:19 -07005718 } else {
5719 ALOGW("%s called in invalid state %d", __func__, out->offload_state);
5720 }
Weiyin Jiang547e4152017-09-14 17:24:18 +08005721 out->written = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005722 pthread_mutex_unlock(&out->lock);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07005723 ALOGD("copl(%p):out of compress flush", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005724 return 0;
5725 }
5726 return -ENOSYS;
5727}
5728
Haynes Mathew George16081042017-05-31 17:16:49 -07005729static int out_stop(const struct audio_stream_out* stream)
5730{
5731 struct stream_out *out = (struct stream_out *)stream;
5732 struct audio_device *adev = out->dev;
5733 int ret = -ENOSYS;
5734
5735 ALOGV("%s", __func__);
5736 pthread_mutex_lock(&adev->lock);
5737 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP && !out->standby &&
5738 out->playback_started && out->pcm != NULL) {
5739 pcm_stop(out->pcm);
5740 ret = stop_output_stream(out);
5741 out->playback_started = false;
5742 }
5743 pthread_mutex_unlock(&adev->lock);
5744 return ret;
5745}
5746
5747static int out_start(const struct audio_stream_out* stream)
5748{
5749 struct stream_out *out = (struct stream_out *)stream;
5750 struct audio_device *adev = out->dev;
5751 int ret = -ENOSYS;
5752
5753 ALOGV("%s", __func__);
5754 pthread_mutex_lock(&adev->lock);
5755 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP && !out->standby &&
5756 !out->playback_started && out->pcm != NULL) {
5757 ret = start_output_stream(out);
5758 if (ret == 0) {
5759 out->playback_started = true;
5760 }
5761 }
5762 pthread_mutex_unlock(&adev->lock);
5763 return ret;
5764}
5765
5766/*
5767 * Modify config->period_count based on min_size_frames
5768 */
5769static void adjust_mmap_period_count(struct pcm_config *config, int32_t min_size_frames)
5770{
5771 int periodCountRequested = (min_size_frames + config->period_size - 1)
5772 / config->period_size;
5773 int periodCount = MMAP_PERIOD_COUNT_MIN;
5774
5775 ALOGV("%s original config.period_size = %d config.period_count = %d",
5776 __func__, config->period_size, config->period_count);
5777
5778 while (periodCount < periodCountRequested && (periodCount * 2) < MMAP_PERIOD_COUNT_MAX) {
5779 periodCount *= 2;
5780 }
5781 config->period_count = periodCount;
5782
5783 ALOGV("%s requested config.period_count = %d", __func__, config->period_count);
5784}
5785
Phil Burkfe17efd2019-03-25 10:23:35 -07005786// Read offset for the positional timestamp from a persistent vendor property.
5787// This is to workaround apparent inaccuracies in the timing information that
5788// is used by the AAudio timing model. The inaccuracies can cause glitches.
5789static int64_t get_mmap_out_time_offset() {
5790 const int32_t kDefaultOffsetMicros = 0;
5791 int32_t mmap_time_offset_micros = property_get_int32(
Weiyin Jiangbd68b4d2019-05-17 16:29:50 +08005792 "persist.vendor.audio.out_mmap_delay_micros", kDefaultOffsetMicros);
Phil Burkfe17efd2019-03-25 10:23:35 -07005793 ALOGI("mmap_time_offset_micros = %d for output", mmap_time_offset_micros);
5794 return mmap_time_offset_micros * (int64_t)1000;
5795}
5796
Haynes Mathew George16081042017-05-31 17:16:49 -07005797static int out_create_mmap_buffer(const struct audio_stream_out *stream,
5798 int32_t min_size_frames,
5799 struct audio_mmap_buffer_info *info)
5800{
5801 struct stream_out *out = (struct stream_out *)stream;
5802 struct audio_device *adev = out->dev;
5803 int ret = 0;
Aalique Grahame1f123102017-10-12 10:38:32 -07005804 unsigned int offset1 = 0;
5805 unsigned int frames1 = 0;
Haynes Mathew George16081042017-05-31 17:16:49 -07005806 const char *step = "";
Haynes Mathew Georgeef514882017-05-01 17:46:23 -07005807 uint32_t mmap_size;
Arun Mirpuri5d170872019-03-26 13:21:31 -07005808 uint32_t buffer_size;
Haynes Mathew George16081042017-05-31 17:16:49 -07005809
Arun Mirpuri5d170872019-03-26 13:21:31 -07005810 ALOGD("%s", __func__);
Sharad Sanglec6f32552018-05-04 16:15:38 +05305811 lock_output_stream(out);
Haynes Mathew George16081042017-05-31 17:16:49 -07005812 pthread_mutex_lock(&adev->lock);
5813
Sharad Sanglec6f32552018-05-04 16:15:38 +05305814 if (CARD_STATUS_OFFLINE == out->card_status ||
5815 CARD_STATUS_OFFLINE == adev->card_status) {
5816 ALOGW("out->card_status or adev->card_status offline, try again");
5817 ret = -EIO;
5818 goto exit;
5819 }
Haynes Mathew George16081042017-05-31 17:16:49 -07005820 if (info == NULL || min_size_frames == 0) {
5821 ALOGE("%s: info = %p, min_size_frames = %d", __func__, info, min_size_frames);
5822 ret = -EINVAL;
5823 goto exit;
5824 }
5825 if (out->usecase != USECASE_AUDIO_PLAYBACK_MMAP || !out->standby) {
5826 ALOGE("%s: usecase = %d, standby = %d", __func__, out->usecase, out->standby);
5827 ret = -ENOSYS;
5828 goto exit;
5829 }
5830 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
5831 if (out->pcm_device_id < 0) {
5832 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
5833 __func__, out->pcm_device_id, out->usecase);
5834 ret = -EINVAL;
5835 goto exit;
5836 }
5837
5838 adjust_mmap_period_count(&out->config, min_size_frames);
5839
Arun Mirpuri5d170872019-03-26 13:21:31 -07005840 ALOGD("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
Haynes Mathew George16081042017-05-31 17:16:49 -07005841 __func__, adev->snd_card, out->pcm_device_id, out->config.channels);
5842 out->pcm = pcm_open(adev->snd_card, out->pcm_device_id,
5843 (PCM_OUT | PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC), &out->config);
Satish Babu Patakokila54ce83d2018-07-06 18:00:37 +05305844 if (errno == ENETRESET && !pcm_is_ready(out->pcm)) {
Sharad Sanglec6f32552018-05-04 16:15:38 +05305845 ALOGE("%s: pcm_open failed errno:%d\n", __func__, errno);
5846 out->card_status = CARD_STATUS_OFFLINE;
5847 adev->card_status = CARD_STATUS_OFFLINE;
5848 ret = -EIO;
5849 goto exit;
5850 }
5851
Haynes Mathew George16081042017-05-31 17:16:49 -07005852 if (out->pcm == NULL || !pcm_is_ready(out->pcm)) {
5853 step = "open";
5854 ret = -ENODEV;
5855 goto exit;
5856 }
5857 ret = pcm_mmap_begin(out->pcm, &info->shared_memory_address, &offset1, &frames1);
5858 if (ret < 0) {
5859 step = "begin";
5860 goto exit;
5861 }
5862 info->buffer_size_frames = pcm_get_buffer_size(out->pcm);
Arun Mirpuri5d170872019-03-26 13:21:31 -07005863 buffer_size = pcm_frames_to_bytes(out->pcm, info->buffer_size_frames);
Haynes Mathew George16081042017-05-31 17:16:49 -07005864 info->burst_size_frames = out->config.period_size;
Haynes Mathew Georgeef514882017-05-01 17:46:23 -07005865 ret = platform_get_mmap_data_fd(adev->platform,
5866 out->pcm_device_id, 0 /*playback*/,
5867 &info->shared_memory_fd,
5868 &mmap_size);
5869 if (ret < 0) {
Arun Mirpuri5d170872019-03-26 13:21:31 -07005870 // Fall back to non exclusive mode
5871 info->shared_memory_fd = pcm_get_poll_fd(out->pcm);
5872 } else {
5873 if (mmap_size < buffer_size) {
5874 step = "mmap";
5875 goto exit;
5876 }
5877 // FIXME: indicate exclusive mode support by returning a negative buffer size
5878 info->buffer_size_frames *= -1;
Haynes Mathew Georgeef514882017-05-01 17:46:23 -07005879 }
Haynes Mathew George16081042017-05-31 17:16:49 -07005880 memset(info->shared_memory_address, 0, pcm_frames_to_bytes(out->pcm,
Haynes Mathew Georgeef514882017-05-01 17:46:23 -07005881 info->buffer_size_frames));
Haynes Mathew George16081042017-05-31 17:16:49 -07005882
5883 ret = pcm_mmap_commit(out->pcm, 0, MMAP_PERIOD_SIZE);
5884 if (ret < 0) {
5885 step = "commit";
5886 goto exit;
5887 }
5888
Phil Burkfe17efd2019-03-25 10:23:35 -07005889 out->mmap_time_offset_nanos = get_mmap_out_time_offset();
5890
Haynes Mathew George16081042017-05-31 17:16:49 -07005891 out->standby = false;
5892 ret = 0;
5893
Arun Mirpuri5d170872019-03-26 13:21:31 -07005894 ALOGD("%s: got mmap buffer address %p info->buffer_size_frames %d",
Haynes Mathew George16081042017-05-31 17:16:49 -07005895 __func__, info->shared_memory_address, info->buffer_size_frames);
5896
5897exit:
5898 if (ret != 0) {
5899 if (out->pcm == NULL) {
5900 ALOGE("%s: %s - %d", __func__, step, ret);
5901 } else {
5902 ALOGE("%s: %s %s", __func__, step, pcm_get_error(out->pcm));
5903 pcm_close(out->pcm);
5904 out->pcm = NULL;
5905 }
5906 }
5907 pthread_mutex_unlock(&adev->lock);
Sharad Sanglec6f32552018-05-04 16:15:38 +05305908 pthread_mutex_unlock(&out->lock);
Haynes Mathew George16081042017-05-31 17:16:49 -07005909 return ret;
5910}
5911
5912static int out_get_mmap_position(const struct audio_stream_out *stream,
5913 struct audio_mmap_position *position)
5914{
5915 struct stream_out *out = (struct stream_out *)stream;
5916 ALOGVV("%s", __func__);
5917 if (position == NULL) {
5918 return -EINVAL;
5919 }
5920 if (out->usecase != USECASE_AUDIO_PLAYBACK_MMAP) {
Haynes Mathew George4ab3ba92017-12-11 14:49:43 -08005921 ALOGE("%s: called on %s", __func__, use_case_table[out->usecase]);
Haynes Mathew George16081042017-05-31 17:16:49 -07005922 return -ENOSYS;
5923 }
5924 if (out->pcm == NULL) {
5925 return -ENOSYS;
5926 }
5927
5928 struct timespec ts = { 0, 0 };
5929 int ret = pcm_mmap_get_hw_ptr(out->pcm, (unsigned int *)&position->position_frames, &ts);
5930 if (ret < 0) {
5931 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
5932 return ret;
5933 }
Phil Burkfe17efd2019-03-25 10:23:35 -07005934 position->time_nanoseconds = ts.tv_sec*1000000000LL + ts.tv_nsec
5935 + out->mmap_time_offset_nanos;
Haynes Mathew George16081042017-05-31 17:16:49 -07005936 return 0;
5937}
5938
5939
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005940/** audio_stream_in implementation **/
5941static uint32_t in_get_sample_rate(const struct audio_stream *stream)
5942{
5943 struct stream_in *in = (struct stream_in *)stream;
5944
5945 return in->config.rate;
5946}
5947
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07005948static int in_set_sample_rate(struct audio_stream *stream __unused,
5949 uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005950{
5951 return -ENOSYS;
5952}
5953
5954static size_t in_get_buffer_size(const struct audio_stream *stream)
5955{
5956 struct stream_in *in = (struct stream_in *)stream;
5957
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08005958 if(in->usecase == USECASE_COMPRESS_VOIP_CALL)
5959 return voice_extn_compress_voip_in_get_buffer_size(in);
Vikram Panduranga93f080e2017-06-07 18:16:14 -07005960 else if(in->usecase == USECASE_AUDIO_RECORD_VOIP)
5961 return VOIP_IO_BUF_SIZE(in->config.rate, DEFAULT_VOIP_BUF_DURATION_MS, DEFAULT_VOIP_BIT_DEPTH_BYTE);
Mingming Yine62d7842013-10-25 16:26:03 -07005962 else if(audio_extn_compr_cap_usecase_supported(in->usecase))
5963 return audio_extn_compr_cap_get_buffer_size(in->config.format);
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05305964 else if(audio_extn_cin_attached_usecase(in->usecase))
5965 return audio_extn_cin_get_buffer_size(in);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08005966
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005967 return in->config.period_size * in->af_period_multiplier *
5968 audio_stream_in_frame_size((const struct audio_stream_in *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005969}
5970
5971static uint32_t in_get_channels(const struct audio_stream *stream)
5972{
5973 struct stream_in *in = (struct stream_in *)stream;
5974
5975 return in->channel_mask;
5976}
5977
5978static audio_format_t in_get_format(const struct audio_stream *stream)
5979{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08005980 struct stream_in *in = (struct stream_in *)stream;
5981
5982 return in->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005983}
5984
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07005985static int in_set_format(struct audio_stream *stream __unused,
5986 audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005987{
5988 return -ENOSYS;
5989}
5990
5991static int in_standby(struct audio_stream *stream)
5992{
5993 struct stream_in *in = (struct stream_in *)stream;
5994 struct audio_device *adev = in->dev;
5995 int status = 0;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05305996 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
5997 stream, in->usecase, use_case_table[in->usecase]);
Haynes Mathew George16081042017-05-31 17:16:49 -07005998 bool do_stop = true;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05305999
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006000 lock_input_stream(in);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07006001 if (!in->standby && in->is_st_session) {
6002 ALOGD("%s: sound trigger pcm stop lab", __func__);
6003 audio_extn_sound_trigger_stop_lab(in);
Quinn Malef6050362019-01-30 15:55:40 -08006004 adev->num_va_sessions--;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07006005 in->standby = 1;
6006 }
6007
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006008 if (!in->standby) {
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07006009 if (adev->adm_deregister_stream)
6010 adev->adm_deregister_stream(adev->adm_data, in->capture_handle);
6011
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08006012 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006013 in->standby = true;
Zhou Songa8895042016-07-05 17:54:22 +08006014 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
kunleizbecba2d2017-09-07 13:37:16 +08006015 do_stop = false;
Zhou Songa8895042016-07-05 17:54:22 +08006016 voice_extn_compress_voip_close_input_stream(stream);
6017 ALOGD("VOIP input entered standby");
Haynes Mathew George16081042017-05-31 17:16:49 -07006018 } else if (in->usecase == USECASE_AUDIO_RECORD_MMAP) {
6019 do_stop = in->capture_started;
6020 in->capture_started = false;
Zhou Songa8895042016-07-05 17:54:22 +08006021 } else {
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05306022 if (audio_extn_cin_attached_usecase(in->usecase))
6023 audio_extn_cin_stop_input_stream(in);
kunleizbecba2d2017-09-07 13:37:16 +08006024 }
6025
Arun Mirpuri5d170872019-03-26 13:21:31 -07006026 if (in->pcm) {
6027 ATRACE_BEGIN("pcm_in_close");
6028 pcm_close(in->pcm);
6029 ATRACE_END();
6030 in->pcm = NULL;
6031 }
6032
6033 if(do_stop) {
Aalique Grahame22e49102018-12-18 14:23:57 -08006034 adev->enable_voicerx = false;
6035 platform_set_echo_reference(adev, false, AUDIO_DEVICE_NONE);
Zhou Songa8895042016-07-05 17:54:22 +08006036 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08006037 }
Quinn Malef6050362019-01-30 15:55:40 -08006038
6039 if (in->source == AUDIO_SOURCE_VOICE_RECOGNITION)
6040 adev->num_va_sessions--;
6041
Eric Laurent150dbfe2013-02-27 14:31:02 -08006042 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006043 }
6044 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07006045 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006046 return status;
6047}
6048
Aalique Grahame22e49102018-12-18 14:23:57 -08006049static int in_dump(const struct audio_stream *stream,
6050 int fd)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006051{
Aalique Grahame22e49102018-12-18 14:23:57 -08006052 struct stream_in *in = (struct stream_in *)stream;
6053
6054 // We try to get the lock for consistency,
6055 // but it isn't necessary for these variables.
6056 // If we're not in standby, we may be blocked on a read.
6057 const bool locked = (pthread_mutex_trylock(&in->lock) == 0);
6058 dprintf(fd, " Standby: %s\n", in->standby ? "yes" : "no");
6059 dprintf(fd, " Frames read: %lld\n", (long long)in->frames_read);
6060 dprintf(fd, " Frames muted: %lld\n", (long long)in->frames_muted);
6061
6062 if (locked) {
6063 pthread_mutex_unlock(&in->lock);
6064 }
6065
6066 // dump error info
6067 (void)error_log_dump(
6068 in->error_log, fd, " " /* prefix */, 0 /* lines */, 0 /* limit_ns */);
6069
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006070 return 0;
6071}
6072
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306073static void in_snd_mon_cb(void * stream, struct str_parms * parms)
6074{
6075 if (!stream || !parms)
6076 return;
6077
6078 struct stream_in *in = (struct stream_in *)stream;
6079 struct audio_device *adev = in->dev;
6080
6081 card_status_t status;
6082 int card;
6083 if (parse_snd_card_status(parms, &card, &status) < 0)
6084 return;
6085
6086 pthread_mutex_lock(&adev->lock);
6087 bool valid_cb = (card == adev->snd_card);
6088 pthread_mutex_unlock(&adev->lock);
6089
6090 if (!valid_cb)
6091 return;
6092
6093 lock_input_stream(in);
6094 if (in->card_status != status)
6095 in->card_status = status;
6096 pthread_mutex_unlock(&in->lock);
6097
6098 ALOGW("in_snd_mon_cb for card %d usecase %s, status %s", card,
6099 use_case_table[in->usecase],
6100 status == CARD_STATUS_OFFLINE ? "offline" : "online");
6101
6102 // a better solution would be to report error back to AF and let
6103 // it put the stream to standby
6104 if (status == CARD_STATUS_OFFLINE)
6105 in_standby(&in->stream.common);
6106
6107 return;
6108}
6109
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006110static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
6111{
6112 struct stream_in *in = (struct stream_in *)stream;
6113 struct audio_device *adev = in->dev;
6114 struct str_parms *parms;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006115 char value[32];
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08006116 int ret = 0, val = 0, err;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006117
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05306118 ALOGD("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006119 parms = str_parms_create_str(kvpairs);
6120
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05306121 if (!parms)
6122 goto error;
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006123 lock_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08006124 pthread_mutex_lock(&adev->lock);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08006125
6126 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
6127 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006128 val = atoi(value);
6129 /* no audio source uses val == 0 */
6130 if ((in->source != val) && (val != 0)) {
6131 in->source = val;
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08006132 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
6133 (in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
6134 (voice_extn_compress_voip_is_format_supported(in->format)) &&
Shiv Maliyappanahallie66aba22016-01-27 16:08:57 -08006135 (in->config.rate == 8000 || in->config.rate == 16000 ||
6136 in->config.rate == 32000 || in->config.rate == 48000 ) &&
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07006137 (audio_channel_count_from_in_mask(in->channel_mask) == 1)) {
Narsinga Rao Chella7d5a3e82014-02-04 16:23:52 -08006138 err = voice_extn_compress_voip_open_input_stream(in);
6139 if (err != 0) {
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08006140 ALOGE("%s: Compress voip input cannot be opened, error:%d",
Narsinga Rao Chella7d5a3e82014-02-04 16:23:52 -08006141 __func__, err);
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08006142 }
6143 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006144 }
6145 }
6146
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08006147 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
6148 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006149 val = atoi(value);
Kevin Rocardfce19002017-08-07 19:21:36 -07006150 if (((int)in->device != val) && (val != 0) && audio_is_input_device(val) ) {
6151
6152 // Workaround: If routing to an non existing usb device, fail gracefully
6153 // The routing request will otherwise block during 10 second
6154 int card;
6155 if (audio_is_usb_in_device(val) &&
6156 (card = get_alive_usb_card(parms)) >= 0) {
6157
6158 ALOGW("in_set_parameters() ignoring rerouting to non existing USB card %d", card);
6159 ret = -ENOSYS;
6160 } else {
6161
6162 in->device = val;
6163 /* If recording is in progress, change the tx device to new device */
6164 if (!in->standby && !in->is_st_session) {
6165 ALOGV("update input routing change");
6166 // inform adm before actual routing to prevent glitches.
6167 if (adev->adm_on_routing_change) {
Haynes Mathew George822b5492016-07-01 16:57:24 -07006168 adev->adm_on_routing_change(adev->adm_data,
6169 in->capture_handle);
Kevin Rocardfce19002017-08-07 19:21:36 -07006170 ret = select_devices(adev, in->usecase);
Aniket Kumar Lata60586a92019-05-22 22:18:55 -07006171 if (in->usecase == USECASE_AUDIO_RECORD_LOW_LATENCY)
6172 adev->adm_routing_changed = true;
Kevin Rocardfce19002017-08-07 19:21:36 -07006173 }
6174 }
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006175 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006176 }
6177 }
6178
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +05306179 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_PROFILE, value, sizeof(value));
6180 if (err >= 0) {
6181 strlcpy(in->profile, value, sizeof(in->profile));
6182 ALOGV("updating stream profile with value '%s'", in->profile);
6183 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
6184 &adev->streams_input_cfg_list,
6185 in->device, in->flags, in->format,
6186 in->sample_rate, in->bit_width,
6187 in->profile, &in->app_type_cfg);
6188 }
6189
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006190 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08006191 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006192
6193 str_parms_destroy(parms);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05306194error:
Eric Laurent994a6932013-07-17 11:51:42 -07006195 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006196 return ret;
6197}
6198
6199static char* in_get_parameters(const struct audio_stream *stream,
6200 const char *keys)
6201{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08006202 struct stream_in *in = (struct stream_in *)stream;
6203 struct str_parms *query = str_parms_create_str(keys);
6204 char *str;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08006205 struct str_parms *reply = str_parms_create();
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07006206
6207 if (!query || !reply) {
Alexy Josephaee4fdd2016-01-29 13:02:07 -08006208 if (reply) {
6209 str_parms_destroy(reply);
6210 }
6211 if (query) {
6212 str_parms_destroy(query);
6213 }
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07006214 ALOGE("in_get_parameters: failed to create query or reply");
6215 return NULL;
6216 }
6217
Haynes Mathew George484e8d22017-07-31 18:55:17 -07006218 ALOGV("%s: enter: keys - %s %s ", __func__, use_case_table[in->usecase], keys);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08006219
6220 voice_extn_in_get_parameters(in, query, reply);
6221
Haynes Mathew George484e8d22017-07-31 18:55:17 -07006222 stream_get_parameter_channels(query, reply,
6223 &in->supported_channel_masks[0]);
6224 stream_get_parameter_formats(query, reply,
6225 &in->supported_formats[0]);
6226 stream_get_parameter_rates(query, reply,
6227 &in->supported_sample_rates[0]);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08006228 str = str_parms_to_str(reply);
6229 str_parms_destroy(query);
6230 str_parms_destroy(reply);
6231
6232 ALOGV("%s: exit: returns - %s", __func__, str);
6233 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006234}
6235
Aalique Grahame22e49102018-12-18 14:23:57 -08006236static int in_set_gain(struct audio_stream_in *stream,
6237 float gain)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006238{
Aalique Grahame22e49102018-12-18 14:23:57 -08006239 struct stream_in *in = (struct stream_in *)stream;
6240 char mixer_ctl_name[128];
6241 struct mixer_ctl *ctl;
6242 int ctl_value;
6243
6244 ALOGV("%s: gain %f", __func__, gain);
6245
6246 if (stream == NULL)
6247 return -EINVAL;
6248
6249 /* in_set_gain() only used to silence MMAP capture for now */
6250 if (in->usecase != USECASE_AUDIO_RECORD_MMAP)
6251 return -ENOSYS;
6252
6253 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name), "Capture %d Volume", in->pcm_device_id);
6254
6255 ctl = mixer_get_ctl_by_name(in->dev->mixer, mixer_ctl_name);
6256 if (!ctl) {
6257 ALOGW("%s: Could not get ctl for mixer cmd - %s",
6258 __func__, mixer_ctl_name);
6259 return -ENOSYS;
6260 }
6261
6262 if (gain < RECORD_GAIN_MIN)
6263 gain = RECORD_GAIN_MIN;
6264 else if (gain > RECORD_GAIN_MAX)
6265 gain = RECORD_GAIN_MAX;
6266 ctl_value = (int)(RECORD_VOLUME_CTL_MAX * gain);
6267
6268 mixer_ctl_set_value(ctl, 0, ctl_value);
6269
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006270 return 0;
6271}
6272
6273static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
6274 size_t bytes)
6275{
6276 struct stream_in *in = (struct stream_in *)stream;
Pallavid7c7a272018-01-16 11:22:55 +05306277
6278 if (in == NULL) {
6279 ALOGE("%s: stream_in ptr is NULL", __func__);
6280 return -EINVAL;
6281 }
6282
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006283 struct audio_device *adev = in->dev;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05306284 int ret = -1;
Dhananjay Kumar97b81e32019-05-15 21:00:21 +05306285 size_t bytes_read = 0, frame_size = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006286
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006287 lock_input_stream(in);
Naresh Tanniru4c630392014-05-12 01:05:52 +05306288
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07006289 if (in->is_st_session) {
6290 ALOGVV(" %s: reading on st session bytes=%zu", __func__, bytes);
6291 /* Read from sound trigger HAL */
6292 audio_extn_sound_trigger_read(in, buffer, bytes);
Quinn Malef6050362019-01-30 15:55:40 -08006293 if (in->standby) {
6294 adev->num_va_sessions++;
6295 in->standby = 0;
6296 }
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07006297 pthread_mutex_unlock(&in->lock);
6298 return bytes;
6299 }
6300
Haynes Mathew George16081042017-05-31 17:16:49 -07006301 if (in->usecase == USECASE_AUDIO_RECORD_MMAP) {
6302 ret = -ENOSYS;
6303 goto exit;
6304 }
6305
Aniket Kumar Lata60586a92019-05-22 22:18:55 -07006306 if (in->usecase == USECASE_AUDIO_RECORD_LOW_LATENCY &&
6307 !in->standby && adev->adm_routing_changed) {
6308 ret = -ENOSYS;
6309 goto exit;
6310 }
6311
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006312 if (in->standby) {
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07006313 pthread_mutex_lock(&adev->lock);
6314 if (in->usecase == USECASE_COMPRESS_VOIP_CALL)
6315 ret = voice_extn_compress_voip_start_input_stream(in);
6316 else
6317 ret = start_input_stream(in);
Quinn Malef6050362019-01-30 15:55:40 -08006318 if (!ret && in->source == AUDIO_SOURCE_VOICE_RECOGNITION)
6319 adev->num_va_sessions++;
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07006320 pthread_mutex_unlock(&adev->lock);
6321 if (ret != 0) {
6322 goto exit;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006323 }
6324 in->standby = 0;
6325 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006326
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006327 // what's the duration requested by the client?
6328 long ns = 0;
6329
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05306330 if (in->pcm && in->config.rate)
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006331 ns = pcm_bytes_to_frames(in->pcm, bytes)*1000000000LL/
6332 in->config.rate;
6333
Aniket Kumar Lata60586a92019-05-22 22:18:55 -07006334 ret = request_in_focus(in, ns);
6335 if (ret != 0)
6336 goto exit;
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006337 bool use_mmap = is_mmap_usecase(in->usecase) || in->realtime;
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07006338
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05306339 if (audio_extn_cin_attached_usecase(in->usecase)) {
6340 ret = audio_extn_cin_read(in, buffer, bytes, &bytes_read);
6341 } else if (in->pcm) {
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05306342 if (audio_extn_ssr_get_stream() == in) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07006343 ret = audio_extn_ssr_read(stream, buffer, bytes);
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05306344 } else if (audio_extn_compr_cap_usecase_supported(in->usecase)) {
Mingming Yine62d7842013-10-25 16:26:03 -07006345 ret = audio_extn_compr_cap_read(in, buffer, bytes);
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006346 } else if (use_mmap) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07006347 ret = pcm_mmap_read(in->pcm, buffer, bytes);
Garmond Leunge2433c32017-09-28 21:51:22 -07006348 } else if (audio_extn_ffv_get_stream() == in) {
6349 ret = audio_extn_ffv_read(stream, buffer, bytes);
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05306350 } else {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07006351 ret = pcm_read(in->pcm, buffer, bytes);
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05306352 /* data from DSP comes in 24_8 format, convert it to 8_24 */
6353 if (!ret && bytes > 0 && (in->format == AUDIO_FORMAT_PCM_8_24_BIT)) {
6354 if (audio_extn_utils_convert_format_24_8_to_8_24(buffer, bytes)
6355 != bytes) {
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05306356 ret = -EINVAL;
6357 goto exit;
6358 }
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05306359 } else if (ret < 0) {
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05306360 ret = -errno;
6361 }
6362 }
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05306363 /* bytes read is always set to bytes for non compress usecases */
6364 bytes_read = bytes;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006365 }
6366
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006367 release_in_focus(in);
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07006368
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006369 /*
Quinn Malef6050362019-01-30 15:55:40 -08006370 * Instead of writing zeroes here, we could trust the hardware to always
6371 * provide zeroes when muted. This is also muted with voice recognition
6372 * usecases so that other clients do not have access to voice recognition
6373 * data.
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006374 */
Quinn Malef6050362019-01-30 15:55:40 -08006375 if ((ret == 0 && voice_get_mic_mute(adev) &&
6376 !voice_is_in_call_rec_stream(in) &&
6377 in->usecase != USECASE_AUDIO_RECORD_AFE_PROXY) ||
6378 (adev->num_va_sessions &&
6379 in->source != AUDIO_SOURCE_VOICE_RECOGNITION &&
6380 property_get_bool("persist.vendor.audio.va_concurrency_mute_enabled",
6381 false)))
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006382 memset(buffer, 0, bytes);
6383
6384exit:
Dhananjay Kumar97b81e32019-05-15 21:00:21 +05306385 frame_size = audio_stream_in_frame_size(stream);
6386 if (frame_size > 0)
6387 in->frames_read += bytes_read/frame_size;
6388
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07006389 if (-ENETRESET == ret)
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306390 in->card_status = CARD_STATUS_OFFLINE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006391 pthread_mutex_unlock(&in->lock);
6392
6393 if (ret != 0) {
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05306394 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05306395 pthread_mutex_lock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05306396 voice_extn_compress_voip_close_input_stream(&in->stream.common);
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05306397 pthread_mutex_unlock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05306398 in->standby = true;
6399 }
Sharad Sangled17c9122017-03-20 15:58:52 +05306400 if (!audio_extn_cin_attached_usecase(in->usecase)) {
6401 bytes_read = bytes;
6402 memset(buffer, 0, bytes);
6403 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006404 in_standby(&in->stream.common);
Aniket Kumar Lata60586a92019-05-22 22:18:55 -07006405 if (in->usecase == USECASE_AUDIO_RECORD_LOW_LATENCY)
6406 adev->adm_routing_changed = false;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07006407 ALOGV("%s: read failed status %d- sleeping for buffer duration", __func__, ret);
Ashish Jainbbce4322016-02-16 13:25:27 +05306408 usleep((uint64_t)bytes * 1000000 / audio_stream_in_frame_size(stream) /
Naresh Tanniru4c630392014-05-12 01:05:52 +05306409 in_get_sample_rate(&in->stream.common));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006410 }
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05306411 return bytes_read;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006412}
6413
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07006414static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006415{
6416 return 0;
6417}
6418
Aalique Grahame22e49102018-12-18 14:23:57 -08006419static int in_get_capture_position(const struct audio_stream_in *stream,
6420 int64_t *frames, int64_t *time)
6421{
6422 if (stream == NULL || frames == NULL || time == NULL) {
6423 return -EINVAL;
6424 }
6425 struct stream_in *in = (struct stream_in *)stream;
6426 int ret = -ENOSYS;
6427
6428 lock_input_stream(in);
6429 // note: ST sessions do not close the alsa pcm driver synchronously
6430 // on standby. Therefore, we may return an error even though the
6431 // pcm stream is still opened.
6432 if (in->standby) {
6433 ALOGE_IF(in->pcm != NULL && !in->is_st_session,
6434 "%s stream in standby but pcm not NULL for non ST session", __func__);
6435 goto exit;
6436 }
6437 if (in->pcm) {
6438 struct timespec timestamp;
6439 unsigned int avail;
6440 if (pcm_get_htimestamp(in->pcm, &avail, &timestamp) == 0) {
6441 *frames = in->frames_read + avail;
6442 *time = timestamp.tv_sec * 1000000000LL + timestamp.tv_nsec;
6443 ret = 0;
6444 }
6445 }
6446exit:
6447 pthread_mutex_unlock(&in->lock);
6448 return ret;
6449}
6450
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07006451static int add_remove_audio_effect(const struct audio_stream *stream,
6452 effect_handle_t effect,
6453 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006454{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07006455 struct stream_in *in = (struct stream_in *)stream;
6456 int status = 0;
6457 effect_descriptor_t desc;
6458
6459 status = (*effect)->get_descriptor(effect, &desc);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07006460 ALOGV("%s: status %d in->standby %d enable:%d", __func__, status, in->standby, enable);
6461
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07006462 if (status != 0)
6463 return status;
6464
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006465 lock_input_stream(in);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07006466 pthread_mutex_lock(&in->dev->lock);
kunleizd96526c2018-04-09 11:12:32 +08006467 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
Aalique Grahame22e49102018-12-18 14:23:57 -08006468 in->source == AUDIO_SOURCE_VOICE_RECOGNITION ||
kunleizd96526c2018-04-09 11:12:32 +08006469 in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07006470 in->enable_aec != enable &&
6471 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
6472 in->enable_aec = enable;
Aalique Grahame22e49102018-12-18 14:23:57 -08006473 if (!enable)
6474 platform_set_echo_reference(in->dev, enable, AUDIO_DEVICE_NONE);
6475 if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
6476 in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) {
6477 in->dev->enable_voicerx = enable;
6478 struct audio_usecase *usecase;
6479 struct listnode *node;
6480 list_for_each(node, &in->dev->usecase_list) {
6481 usecase = node_to_item(node, struct audio_usecase, list);
6482 if (usecase->type == PCM_PLAYBACK)
6483 select_devices(in->dev, usecase->id);
6484 }
6485 }
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07006486 if (!in->standby) {
6487 if (enable_disable_effect(in->dev, EFFECT_AEC, enable) == ENOSYS)
6488 select_devices(in->dev, in->usecase);
6489 }
6490
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07006491 }
Ravi Kumar Alamanda198185e2013-11-07 15:42:19 -08006492 if (in->enable_ns != enable &&
6493 (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
6494 in->enable_ns = enable;
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07006495 if (!in->standby) {
kunleizd96526c2018-04-09 11:12:32 +08006496 if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
6497 in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) {
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07006498 if (enable_disable_effect(in->dev, EFFECT_NS, enable) == ENOSYS)
6499 select_devices(in->dev, in->usecase);
6500 } else
6501 select_devices(in->dev, in->usecase);
6502 }
Ravi Kumar Alamanda198185e2013-11-07 15:42:19 -08006503 }
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07006504 pthread_mutex_unlock(&in->dev->lock);
6505 pthread_mutex_unlock(&in->lock);
6506
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006507 return 0;
6508}
6509
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07006510static int in_add_audio_effect(const struct audio_stream *stream,
6511 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006512{
Eric Laurent994a6932013-07-17 11:51:42 -07006513 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07006514 return add_remove_audio_effect(stream, effect, true);
6515}
6516
6517static int in_remove_audio_effect(const struct audio_stream *stream,
6518 effect_handle_t effect)
6519{
Eric Laurent994a6932013-07-17 11:51:42 -07006520 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07006521 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006522}
6523
Derek Chenf939fb72018-11-13 13:34:41 -08006524streams_input_ctxt_t *in_get_stream(struct audio_device *dev,
6525 audio_io_handle_t input)
6526{
6527 struct listnode *node;
6528
6529 list_for_each(node, &dev->active_inputs_list) {
6530 streams_input_ctxt_t *in_ctxt = node_to_item(node,
6531 streams_input_ctxt_t,
6532 list);
6533 if (in_ctxt->input->capture_handle == input) {
6534 return in_ctxt;
6535 }
6536 }
6537 return NULL;
6538}
6539
6540streams_output_ctxt_t *out_get_stream(struct audio_device *dev,
6541 audio_io_handle_t output)
6542{
6543 struct listnode *node;
6544
6545 list_for_each(node, &dev->active_outputs_list) {
6546 streams_output_ctxt_t *out_ctxt = node_to_item(node,
6547 streams_output_ctxt_t,
6548 list);
6549 if (out_ctxt->output->handle == output) {
6550 return out_ctxt;
6551 }
6552 }
6553 return NULL;
6554}
6555
Haynes Mathew George16081042017-05-31 17:16:49 -07006556static int in_stop(const struct audio_stream_in* stream)
6557{
6558 struct stream_in *in = (struct stream_in *)stream;
6559 struct audio_device *adev = in->dev;
6560
6561 int ret = -ENOSYS;
6562 ALOGV("%s", __func__);
6563 pthread_mutex_lock(&adev->lock);
6564 if (in->usecase == USECASE_AUDIO_RECORD_MMAP && !in->standby &&
6565 in->capture_started && in->pcm != NULL) {
6566 pcm_stop(in->pcm);
6567 ret = stop_input_stream(in);
6568 in->capture_started = false;
6569 }
6570 pthread_mutex_unlock(&adev->lock);
6571 return ret;
6572}
6573
6574static int in_start(const struct audio_stream_in* stream)
6575{
6576 struct stream_in *in = (struct stream_in *)stream;
6577 struct audio_device *adev = in->dev;
6578 int ret = -ENOSYS;
6579
6580 ALOGV("%s in %p", __func__, in);
6581 pthread_mutex_lock(&adev->lock);
6582 if (in->usecase == USECASE_AUDIO_RECORD_MMAP && !in->standby &&
6583 !in->capture_started && in->pcm != NULL) {
6584 if (!in->capture_started) {
6585 ret = start_input_stream(in);
6586 if (ret == 0) {
6587 in->capture_started = true;
6588 }
6589 }
6590 }
6591 pthread_mutex_unlock(&adev->lock);
6592 return ret;
6593}
6594
Phil Burke0a86d12019-02-16 22:28:11 -08006595// Read offset for the positional timestamp from a persistent vendor property.
6596// This is to workaround apparent inaccuracies in the timing information that
6597// is used by the AAudio timing model. The inaccuracies can cause glitches.
6598static int64_t in_get_mmap_time_offset() {
6599 const int32_t kDefaultOffsetMicros = 0;
6600 int32_t mmap_time_offset_micros = property_get_int32(
Weiyin Jiangbd68b4d2019-05-17 16:29:50 +08006601 "persist.vendor.audio.in_mmap_delay_micros", kDefaultOffsetMicros);
Phil Burke0a86d12019-02-16 22:28:11 -08006602 ALOGI("mmap_time_offset_micros = %d for input", mmap_time_offset_micros);
6603 return mmap_time_offset_micros * (int64_t)1000;
6604}
6605
Haynes Mathew George16081042017-05-31 17:16:49 -07006606static int in_create_mmap_buffer(const struct audio_stream_in *stream,
6607 int32_t min_size_frames,
6608 struct audio_mmap_buffer_info *info)
6609{
6610 struct stream_in *in = (struct stream_in *)stream;
6611 struct audio_device *adev = in->dev;
6612 int ret = 0;
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07006613 unsigned int offset1 = 0;
6614 unsigned int frames1 = 0;
Haynes Mathew George16081042017-05-31 17:16:49 -07006615 const char *step = "";
Arun Mirpuri5d170872019-03-26 13:21:31 -07006616 uint32_t mmap_size = 0;
6617 uint32_t buffer_size = 0;
Haynes Mathew George16081042017-05-31 17:16:49 -07006618
6619 pthread_mutex_lock(&adev->lock);
6620 ALOGV("%s in %p", __func__, in);
6621
Sharad Sanglec6f32552018-05-04 16:15:38 +05306622 if (CARD_STATUS_OFFLINE == in->card_status||
6623 CARD_STATUS_OFFLINE == adev->card_status) {
6624 ALOGW("in->card_status or adev->card_status offline, try again");
6625 ret = -EIO;
6626 goto exit;
6627 }
6628
Haynes Mathew George16081042017-05-31 17:16:49 -07006629 if (info == NULL || min_size_frames == 0) {
6630 ALOGE("%s invalid argument info %p min_size_frames %d", __func__, info, min_size_frames);
6631 ret = -EINVAL;
6632 goto exit;
6633 }
6634 if (in->usecase != USECASE_AUDIO_RECORD_MMAP || !in->standby) {
6635 ALOGE("%s: usecase = %d, standby = %d", __func__, in->usecase, in->standby);
6636 ALOGV("%s in %p", __func__, in);
6637 ret = -ENOSYS;
6638 goto exit;
6639 }
6640 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
6641 if (in->pcm_device_id < 0) {
6642 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
6643 __func__, in->pcm_device_id, in->usecase);
6644 ret = -EINVAL;
6645 goto exit;
6646 }
6647
6648 adjust_mmap_period_count(&in->config, min_size_frames);
6649
6650 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
6651 __func__, adev->snd_card, in->pcm_device_id, in->config.channels);
6652 in->pcm = pcm_open(adev->snd_card, in->pcm_device_id,
6653 (PCM_IN | PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC), &in->config);
Satish Babu Patakokila54ce83d2018-07-06 18:00:37 +05306654 if (errno == ENETRESET && !pcm_is_ready(in->pcm)) {
Sharad Sanglec6f32552018-05-04 16:15:38 +05306655 ALOGE("%s: pcm_open failed errno:%d\n", __func__, errno);
6656 in->card_status = CARD_STATUS_OFFLINE;
6657 adev->card_status = CARD_STATUS_OFFLINE;
6658 ret = -EIO;
6659 goto exit;
6660 }
6661
Haynes Mathew George16081042017-05-31 17:16:49 -07006662 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) {
6663 step = "open";
6664 ret = -ENODEV;
6665 goto exit;
6666 }
6667
6668 ret = pcm_mmap_begin(in->pcm, &info->shared_memory_address, &offset1, &frames1);
6669 if (ret < 0) {
6670 step = "begin";
6671 goto exit;
6672 }
Haynes Mathew George16081042017-05-31 17:16:49 -07006673
Arun Mirpuri5d170872019-03-26 13:21:31 -07006674 info->buffer_size_frames = pcm_get_buffer_size(in->pcm);
6675 buffer_size = pcm_frames_to_bytes(in->pcm, info->buffer_size_frames);
6676 info->burst_size_frames = in->config.period_size;
6677 ret = platform_get_mmap_data_fd(adev->platform,
6678 in->pcm_device_id, 1 /*capture*/,
6679 &info->shared_memory_fd,
6680 &mmap_size);
6681 if (ret < 0) {
6682 // Fall back to non exclusive mode
6683 info->shared_memory_fd = pcm_get_poll_fd(in->pcm);
6684 } else {
6685 if (mmap_size < buffer_size) {
6686 step = "mmap";
6687 goto exit;
6688 }
6689 // FIXME: indicate exclusive mode support by returning a negative buffer size
6690 info->buffer_size_frames *= -1;
6691 }
6692
6693 memset(info->shared_memory_address, 0, buffer_size);
Haynes Mathew George16081042017-05-31 17:16:49 -07006694
6695 ret = pcm_mmap_commit(in->pcm, 0, MMAP_PERIOD_SIZE);
6696 if (ret < 0) {
6697 step = "commit";
6698 goto exit;
6699 }
6700
Phil Burke0a86d12019-02-16 22:28:11 -08006701 in->mmap_time_offset_nanos = in_get_mmap_time_offset();
6702
Haynes Mathew George16081042017-05-31 17:16:49 -07006703 in->standby = false;
6704 ret = 0;
6705
6706 ALOGV("%s: got mmap buffer address %p info->buffer_size_frames %d",
6707 __func__, info->shared_memory_address, info->buffer_size_frames);
6708
6709exit:
6710 if (ret != 0) {
6711 if (in->pcm == NULL) {
6712 ALOGE("%s: %s - %d", __func__, step, ret);
6713 } else {
6714 ALOGE("%s: %s %s", __func__, step, pcm_get_error(in->pcm));
6715 pcm_close(in->pcm);
6716 in->pcm = NULL;
6717 }
6718 }
6719 pthread_mutex_unlock(&adev->lock);
6720 return ret;
6721}
6722
6723static int in_get_mmap_position(const struct audio_stream_in *stream,
6724 struct audio_mmap_position *position)
6725{
6726 struct stream_in *in = (struct stream_in *)stream;
6727 ALOGVV("%s", __func__);
6728 if (position == NULL) {
6729 return -EINVAL;
6730 }
6731 if (in->usecase != USECASE_AUDIO_RECORD_MMAP) {
6732 return -ENOSYS;
6733 }
6734 if (in->pcm == NULL) {
6735 return -ENOSYS;
6736 }
6737 struct timespec ts = { 0, 0 };
6738 int ret = pcm_mmap_get_hw_ptr(in->pcm, (unsigned int *)&position->position_frames, &ts);
6739 if (ret < 0) {
6740 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
6741 return ret;
6742 }
Phil Burke0a86d12019-02-16 22:28:11 -08006743 position->time_nanoseconds = ts.tv_sec*1000000000LL + ts.tv_nsec
6744 + in->mmap_time_offset_nanos;
Haynes Mathew George16081042017-05-31 17:16:49 -07006745 return 0;
6746}
6747
Naresh Tannirudcb47c52018-06-25 16:23:32 +05306748static int in_get_active_microphones(const struct audio_stream_in *stream,
6749 struct audio_microphone_characteristic_t *mic_array,
6750 size_t *mic_count) {
6751 struct stream_in *in = (struct stream_in *)stream;
6752 struct audio_device *adev = in->dev;
6753 ALOGVV("%s", __func__);
6754
6755 lock_input_stream(in);
6756 pthread_mutex_lock(&adev->lock);
6757 int ret = platform_get_active_microphones(adev->platform,
6758 audio_channel_count_from_in_mask(in->channel_mask),
6759 in->usecase, mic_array, mic_count);
6760 pthread_mutex_unlock(&adev->lock);
6761 pthread_mutex_unlock(&in->lock);
6762
6763 return ret;
6764}
6765
6766static int adev_get_microphones(const struct audio_hw_device *dev,
6767 struct audio_microphone_characteristic_t *mic_array,
6768 size_t *mic_count) {
6769 struct audio_device *adev = (struct audio_device *)dev;
6770 ALOGVV("%s", __func__);
6771
6772 pthread_mutex_lock(&adev->lock);
6773 int ret = platform_get_microphones(adev->platform, mic_array, mic_count);
6774 pthread_mutex_unlock(&adev->lock);
6775
6776 return ret;
6777}
juyuchendb308c22019-01-21 11:57:17 -07006778
6779static void in_update_sink_metadata(struct audio_stream_in *stream,
6780 const struct sink_metadata *sink_metadata) {
6781
6782 if (stream == NULL
6783 || sink_metadata == NULL
6784 || sink_metadata->tracks == NULL) {
6785 return;
6786 }
6787
6788 int error = 0;
6789 struct stream_in *in = (struct stream_in *)stream;
6790 struct audio_device *adev = in->dev;
6791 audio_devices_t device = AUDIO_DEVICE_NONE;
6792
6793 if (sink_metadata->track_count != 0)
6794 device = sink_metadata->tracks->dest_device;
6795
6796 lock_input_stream(in);
6797 pthread_mutex_lock(&adev->lock);
6798 ALOGV("%s: in->usecase: %d, device: %x", __func__, in->usecase, device);
6799
6800 if (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY
6801 && device != AUDIO_DEVICE_NONE
6802 && adev->voice_tx_output != NULL) {
6803 /* Use the rx device from afe-proxy record to route voice call because
6804 there is no routing if tx device is on primary hal and rx device
6805 is on other hal during voice call. */
6806 adev->voice_tx_output->devices = device;
6807
6808 if (!voice_is_call_state_active(adev)) {
6809 if (adev->mode == AUDIO_MODE_IN_CALL) {
6810 adev->current_call_output = adev->voice_tx_output;
6811 error = voice_start_call(adev);
6812 if (error != 0)
6813 ALOGE("%s: start voice call failed %d", __func__, error);
6814 }
6815 } else {
6816 adev->current_call_output = adev->voice_tx_output;
6817 voice_update_devices_for_all_voice_usecases(adev);
6818 }
6819 }
6820
6821 pthread_mutex_unlock(&adev->lock);
6822 pthread_mutex_unlock(&in->lock);
6823}
6824
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05306825int adev_open_output_stream(struct audio_hw_device *dev,
Haynes Mathew George16081042017-05-31 17:16:49 -07006826 audio_io_handle_t handle,
6827 audio_devices_t devices,
6828 audio_output_flags_t flags,
6829 struct audio_config *config,
6830 struct audio_stream_out **stream_out,
6831 const char *address __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006832{
6833 struct audio_device *adev = (struct audio_device *)dev;
6834 struct stream_out *out;
Gangadhar Sb0210342019-02-22 17:39:41 +05306835 int ret = 0, ip_hdlr_stream = 0, ip_hdlr_dev = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07006836 audio_format_t format;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08006837 struct adsp_hdlr_stream_cfg hdlr_stream_cfg;
Manish Dewangan21a850a2017-08-14 12:03:55 +05306838 bool is_direct_passthough = false;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07006839 bool is_hdmi = devices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
6840 bool is_usb_dev = audio_is_usb_out_device(devices) &&
6841 (devices != AUDIO_DEVICE_OUT_USB_ACCESSORY);
6842 bool direct_dev = is_hdmi || is_usb_dev;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08006843 bool use_db_as_primary =
vivek mehtaae1018c2019-05-09 12:19:57 -07006844 property_get_bool("vendor.audio.feature.deepbuffer_as_primary.enable",
6845 false);
Vignesh Kulothungana6927272019-02-20 15:17:07 -08006846 bool force_haptic_path =
6847 property_get_bool("vendor.audio.test_haptic", false);
Aniket Kumar Lata23d8cbf2019-04-10 19:54:46 -07006848 bool is_voip_rx = flags & AUDIO_OUTPUT_FLAG_VOIP_RX;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006849
kunleizdff872d2018-08-20 14:40:33 +08006850 if (is_usb_dev && (!audio_extn_usb_connected(NULL))) {
kunleizd6a9e0c2018-07-30 15:38:52 +08006851 is_usb_dev = false;
6852 devices = AUDIO_DEVICE_OUT_SPEAKER;
6853 ALOGW("%s: ignore set device to non existing USB card, use output device(%#x)",
6854 __func__, devices);
6855 }
6856
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006857 *stream_out = NULL;
Naresh Tanniru80659832014-06-04 18:17:56 +05306858
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006859 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
6860
Mingming Yin3a941d42016-02-17 18:08:05 -08006861 ALOGD("%s: enter: format(%#x) sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)\
6862 stream_handle(%p)", __func__, config->format, config->sample_rate, config->channel_mask,
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05306863 devices, flags, &out->stream);
6864
6865
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08006866 if (!out) {
6867 return -ENOMEM;
6868 }
6869
Haynes Mathew George204045b2015-02-25 20:32:03 -08006870 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006871 pthread_mutex_init(&out->pre_lock, (const pthread_mutexattr_t *) NULL);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05306872 pthread_mutex_init(&out->compr_mute_lock, (const pthread_mutexattr_t *) NULL);
Zhou Song48453a02018-01-10 17:50:59 +08006873 pthread_mutex_init(&out->position_query_lock, (const pthread_mutexattr_t *) NULL);
Haynes Mathew George204045b2015-02-25 20:32:03 -08006874 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
6875
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006876 if (devices == AUDIO_DEVICE_NONE)
6877 devices = AUDIO_DEVICE_OUT_SPEAKER;
6878
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006879 out->flags = flags;
6880 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07006881 out->dev = adev;
Aalique Grahame65780b52017-09-27 14:59:56 -07006882 out->hal_op_format = out->hal_ip_format = format = out->format = config->format;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006883 out->sample_rate = config->sample_rate;
Sachin Mohan Gadag3d09acd2017-06-19 12:43:44 +05306884 out->channel_mask = config->channel_mask;
Ramjee Singh5857aeb2017-08-03 19:18:50 +05306885 if (out->channel_mask == AUDIO_CHANNEL_NONE)
6886 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
6887 else
6888 out->supported_channel_masks[0] = out->channel_mask;
Eric Laurentc4aef752013-09-12 17:45:53 -07006889 out->handle = handle;
Mingming Yin3ee55c62014-08-04 14:23:35 -07006890 out->bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Alexy Josephaa54c872014-12-03 02:46:47 -08006891 out->non_blocking = 0;
Ashish Jain83a6cc22016-06-28 14:34:17 +05306892 out->convert_buffer = NULL;
Ashish Jain1b9b30c2017-05-18 20:57:40 +05306893 out->started = 0;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05306894 out->a2dp_compress_mute = false;
Aniket Kumar Lata932f4872017-11-06 18:29:44 -08006895 out->hal_output_suspend_supported = 0;
6896 out->dynamic_pm_qos_config_supported = 0;
Surendar Karkaf51b5842018-04-26 11:28:38 +05306897 out->set_dual_mono = false;
Manisha Agarwal7b3e3772019-02-20 14:33:45 +05306898 out->prev_card_status_offline = false;
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +05306899 out->pspd_coeff_sent = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006900
Nikhil Laturkar26b690b2017-07-25 11:06:14 +05306901 if ((flags & AUDIO_OUTPUT_FLAG_BD) &&
Satish Babu Patakokila37e7c482018-02-02 11:50:06 +05306902 (property_get_bool("vendor.audio.matrix.limiter.enable", false)))
Ben Romberger6c4d3812017-06-13 17:46:45 -07006903 platform_set_device_params(out, DEVICE_PARAM_LIMITER_ID, 1);
6904
Aalique Grahame22e49102018-12-18 14:23:57 -08006905 if (direct_dev &&
6906 (audio_is_linear_pcm(out->format) ||
6907 config->format == AUDIO_FORMAT_DEFAULT) &&
6908 out->flags == AUDIO_OUTPUT_FLAG_NONE) {
6909 audio_format_t req_format = config->format;
6910 audio_channel_mask_t req_channel_mask = config->channel_mask;
6911 uint32_t req_sample_rate = config->sample_rate;
6912
6913 pthread_mutex_lock(&adev->lock);
6914 if (is_hdmi) {
6915 ALOGV("AUDIO_DEVICE_OUT_AUX_DIGITAL and DIRECT|OFFLOAD, check hdmi caps");
6916 ret = read_hdmi_sink_caps(out);
6917 if (config->sample_rate == 0)
6918 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
6919 if (config->channel_mask == AUDIO_CHANNEL_NONE)
6920 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
6921 if (config->format == AUDIO_FORMAT_DEFAULT)
6922 config->format = AUDIO_FORMAT_PCM_16_BIT;
6923 } else if (is_usb_dev) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07006924 ret = read_usb_sup_params_and_compare(true /*is_playback*/,
6925 &config->format,
6926 &out->supported_formats[0],
6927 MAX_SUPPORTED_FORMATS,
6928 &config->channel_mask,
6929 &out->supported_channel_masks[0],
6930 MAX_SUPPORTED_CHANNEL_MASKS,
6931 &config->sample_rate,
6932 &out->supported_sample_rates[0],
6933 MAX_SUPPORTED_SAMPLE_RATES);
6934 ALOGV("plugged dev USB ret %d", ret);
Haynes Mathew George484e8d22017-07-31 18:55:17 -07006935 }
Aalique Grahame22e49102018-12-18 14:23:57 -08006936
Haynes Mathew George484e8d22017-07-31 18:55:17 -07006937 pthread_mutex_unlock(&adev->lock);
6938 if (ret != 0) {
Mingming Yin3a941d42016-02-17 18:08:05 -08006939 if (ret == -ENOSYS) {
6940 /* ignore and go with default */
6941 ret = 0;
Aalique Grahame22e49102018-12-18 14:23:57 -08006942 }
6943 // For MMAP NO IRQ, allow conversions in ADSP
6944 else if (is_hdmi || (flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) == 0)
6945 goto error_open;
6946 else {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07006947 ALOGE("error reading direct dev sink caps");
Mingming Yin3a941d42016-02-17 18:08:05 -08006948 goto error_open;
6949 }
Aalique Grahame22e49102018-12-18 14:23:57 -08006950
6951 if (req_sample_rate != 0 && config->sample_rate != req_sample_rate)
6952 config->sample_rate = req_sample_rate;
6953 if (req_channel_mask != AUDIO_CHANNEL_NONE && config->channel_mask != req_channel_mask)
6954 config->channel_mask = req_channel_mask;
6955 if (req_format != AUDIO_FORMAT_DEFAULT && config->format != req_format)
6956 config->format = req_format;
Mingming Yin3a941d42016-02-17 18:08:05 -08006957 }
Aalique Grahame22e49102018-12-18 14:23:57 -08006958
6959 out->sample_rate = config->sample_rate;
6960 out->channel_mask = config->channel_mask;
6961 out->format = config->format;
6962 if (is_hdmi) {
6963 out->usecase = USECASE_AUDIO_PLAYBACK_HIFI;
6964 out->config = pcm_config_hdmi_multi;
6965 } else if (flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) {
6966 out->usecase = USECASE_AUDIO_PLAYBACK_MMAP;
6967 out->config = pcm_config_mmap_playback;
6968 out->stream.start = out_start;
6969 out->stream.stop = out_stop;
6970 out->stream.create_mmap_buffer = out_create_mmap_buffer;
6971 out->stream.get_mmap_position = out_get_mmap_position;
6972 } else {
6973 out->usecase = USECASE_AUDIO_PLAYBACK_HIFI;
6974 out->config = pcm_config_hifi;
6975 }
6976
6977 out->config.rate = out->sample_rate;
6978 out->config.channels = audio_channel_count_from_out_mask(out->channel_mask);
6979 if (is_hdmi) {
6980 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels *
6981 audio_bytes_per_sample(out->format));
6982 }
6983 out->config.format = pcm_format_from_audio_format(out->format);
Mingming Yin3a941d42016-02-17 18:08:05 -08006984 }
6985
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08006986 /* Check for VOIP usecase */
Aniket Kumar Lata23d8cbf2019-04-10 19:54:46 -07006987 if (is_voip_rx) {
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08006988 if (!voice_extn_is_compress_voip_supported()) {
6989 if (out->sample_rate == 8000 || out->sample_rate == 16000 ||
6990 out->sample_rate == 32000 || out->sample_rate == 48000) {
Aniket Kumar Lata4f9a2a52019-05-09 18:44:09 -07006991 out->channel_mask = audio_extn_utils_is_vendor_enhanced_fwk() ?
6992 AUDIO_CHANNEL_OUT_MONO : AUDIO_CHANNEL_OUT_STEREO;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08006993 out->usecase = USECASE_AUDIO_PLAYBACK_VOIP;
6994 out->format = AUDIO_FORMAT_PCM_16_BIT;
Aniket Kumar Lata8426d1f2019-06-10 15:25:53 -07006995 out->volume_l = INVALID_OUT_VOLUME;
6996 out->volume_r = INVALID_OUT_VOLUME;
Vikram Panduranga93f080e2017-06-07 18:16:14 -07006997
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08006998 out->config = default_pcm_config_voip_copp;
6999 out->config.period_size = VOIP_IO_BUF_SIZE(out->sample_rate, DEFAULT_VOIP_BUF_DURATION_MS, DEFAULT_VOIP_BIT_DEPTH_BYTE)/2;
7000 out->config.rate = out->sample_rate;
7001 }
7002 } else {
7003 if ((out->dev->mode == AUDIO_MODE_IN_COMMUNICATION ||
7004 voice_extn_compress_voip_is_active(out->dev)) &&
7005 (voice_extn_compress_voip_is_config_supported(config))) {
7006 ret = voice_extn_compress_voip_open_output_stream(out);
7007 if (ret != 0) {
7008 ALOGE("%s: Compress voip output cannot be opened, error:%d",
7009 __func__, ret);
7010 goto error_open;
7011 }
7012 }
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007013 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007014 } else if (audio_is_linear_pcm(out->format) &&
7015 out->flags == AUDIO_OUTPUT_FLAG_NONE && is_usb_dev) {
7016 out->channel_mask = config->channel_mask;
7017 out->sample_rate = config->sample_rate;
7018 out->format = config->format;
7019 out->usecase = USECASE_AUDIO_PLAYBACK_HIFI;
7020 // does this change?
7021 out->config = is_hdmi ? pcm_config_hdmi_multi : pcm_config_hifi;
7022 out->config.rate = config->sample_rate;
7023 out->config.channels = audio_channel_count_from_out_mask(out->channel_mask);
7024 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels *
7025 audio_bytes_per_sample(config->format));
7026 out->config.format = pcm_format_from_audio_format(out->format);
vivek mehta0ea887a2015-08-26 14:01:20 -07007027 } else if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
Dhananjay Kumarac341582017-02-23 23:42:25 +05307028 (out->flags == AUDIO_OUTPUT_FLAG_DIRECT)) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05307029 pthread_mutex_lock(&adev->lock);
7030 bool offline = (adev->card_status == CARD_STATUS_OFFLINE);
7031 pthread_mutex_unlock(&adev->lock);
7032
7033 // reject offload during card offline to allow
7034 // fallback to s/w paths
7035 if (offline) {
7036 ret = -ENODEV;
7037 goto error_open;
7038 }
vivek mehta0ea887a2015-08-26 14:01:20 -07007039
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07007040 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
7041 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
7042 ALOGE("%s: Unsupported Offload information", __func__);
7043 ret = -EINVAL;
7044 goto error_open;
7045 }
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07007046
Atul Khare3fa6e542017-08-09 00:56:17 +05307047 if (config->offload_info.format == 0)
7048 config->offload_info.format = config->format;
7049 if (config->offload_info.sample_rate == 0)
7050 config->offload_info.sample_rate = config->sample_rate;
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07007051
Mingming Yin90310102013-11-13 16:57:00 -08007052 if (!is_supported_format(config->offload_info.format) &&
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05307053 !audio_extn_passthru_is_supported_format(config->offload_info.format)) {
vivek mehta0ea887a2015-08-26 14:01:20 -07007054 ALOGE("%s: Unsupported audio format %x " , __func__, config->offload_info.format);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07007055 ret = -EINVAL;
7056 goto error_open;
7057 }
7058
Ben Romberger0f8c87b2017-05-24 17:41:11 -07007059 /* TrueHD only supported for 48k multiples (48k, 96k, 192k) */
7060 if ((config->offload_info.format == AUDIO_FORMAT_DOLBY_TRUEHD) &&
7061 (audio_extn_passthru_is_passthrough_stream(out)) &&
7062 !((config->sample_rate == 48000) ||
7063 (config->sample_rate == 96000) ||
7064 (config->sample_rate == 192000))) {
7065 ALOGE("%s: Unsupported sample rate %d for audio format %x",
7066 __func__, config->sample_rate, config->offload_info.format);
7067 ret = -EINVAL;
7068 goto error_open;
7069 }
7070
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07007071 out->compr_config.codec = (struct snd_codec *)
7072 calloc(1, sizeof(struct snd_codec));
7073
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07007074 if (!out->compr_config.codec) {
7075 ret = -ENOMEM;
7076 goto error_open;
7077 }
7078
Dhananjay Kumarac341582017-02-23 23:42:25 +05307079 out->stream.pause = out_pause;
7080 out->stream.resume = out_resume;
7081 out->stream.flush = out_flush;
Ashish Jain4847e9d2017-08-17 19:16:57 +05307082 out->stream.set_callback = out_set_callback;
Dhananjay Kumarac341582017-02-23 23:42:25 +05307083 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Mingming Yin21d60472015-09-30 13:56:25 -07007084 out->stream.drain = out_drain;
Dhananjay Kumarac341582017-02-23 23:42:25 +05307085 out->usecase = get_offload_usecase(adev, true /* is_compress */);
vivek mehta446c3962015-09-14 10:57:35 -07007086 ALOGV("Compress Offload usecase .. usecase selected %d", out->usecase);
Dhananjay Kumarac341582017-02-23 23:42:25 +05307087 } else {
7088 out->usecase = get_offload_usecase(adev, false /* is_compress */);
7089 ALOGV("non-offload DIRECT_usecase ... usecase selected %d ", out->usecase);
vivek mehta0ea887a2015-08-26 14:01:20 -07007090 }
vivek mehta446c3962015-09-14 10:57:35 -07007091
7092 if (out->usecase == USECASE_INVALID) {
Mingming Yin3a941d42016-02-17 18:08:05 -08007093 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL &&
7094 config->format == 0 && config->sample_rate == 0 &&
7095 config->channel_mask == 0) {
Mingming Yin21854652016-04-13 11:54:02 -07007096 ALOGI("%s dummy open to query sink capability",__func__);
Mingming Yin3a941d42016-02-17 18:08:05 -08007097 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
7098 } else {
7099 ALOGE("%s, Max allowed OFFLOAD usecase reached ... ", __func__);
7100 ret = -EEXIST;
7101 goto error_open;
7102 }
vivek mehta446c3962015-09-14 10:57:35 -07007103 }
7104
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07007105 if (config->offload_info.channel_mask)
7106 out->channel_mask = config->offload_info.channel_mask;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08007107 else if (config->channel_mask) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07007108 out->channel_mask = config->channel_mask;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08007109 config->offload_info.channel_mask = config->channel_mask;
Haynes Mathew Georgea99f7532016-08-24 16:01:21 -07007110 } else {
Dhananjay Kumarac341582017-02-23 23:42:25 +05307111 ALOGE("out->channel_mask not set for OFFLOAD/DIRECT usecase");
Haynes Mathew Georgea99f7532016-08-24 16:01:21 -07007112 ret = -EINVAL;
7113 goto error_open;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08007114 }
Haynes Mathew Georgea99f7532016-08-24 16:01:21 -07007115
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07007116 format = out->format = config->offload_info.format;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07007117 out->sample_rate = config->offload_info.sample_rate;
7118
Mingming Yin3ee55c62014-08-04 14:23:35 -07007119 out->bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07007120
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05307121 out->compr_config.codec->id = get_snd_codec_id(config->offload_info.format);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05307122 if (audio_extn_utils_is_dolby_format(config->offload_info.format)) {
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05307123 audio_extn_dolby_send_ddp_endp_params(adev);
7124 audio_extn_dolby_set_dmid(adev);
7125 }
vivek mehta0ea887a2015-08-26 14:01:20 -07007126
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07007127 out->compr_config.codec->sample_rate =
Ravi Kumar Alamandab91bff32014-11-14 12:05:54 -08007128 config->offload_info.sample_rate;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07007129 out->compr_config.codec->bit_rate =
7130 config->offload_info.bit_rate;
7131 out->compr_config.codec->ch_in =
Dhanalakshmi Siddania15c6792016-08-10 15:33:53 +05307132 audio_channel_count_from_out_mask(out->channel_mask);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07007133 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
Satish Babu Patakokilaa395a9e2016-11-01 12:18:49 +05307134 /* Update bit width only for non passthrough usecases.
7135 * For passthrough usecases, the output will always be opened @16 bit
7136 */
7137 if (!audio_extn_passthru_is_passthrough_stream(out))
7138 out->bit_width = AUDIO_OUTPUT_BIT_WIDTH;
Naresh Tanniruee3499a2017-01-05 14:05:35 +05307139
7140 if (out->flags & AUDIO_OUTPUT_FLAG_TIMESTAMP)
7141 out->compr_config.codec->flags |= COMPRESSED_TIMESTAMP_FLAG;
7142 ALOGVV("%s : out->compr_config.codec->flags -> (%#x) ", __func__, out->compr_config.codec->flags);
7143
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07007144 /*TODO: Do we need to change it for passthrough */
7145 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_RAW;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07007146
Manish Dewangana6fc5442015-08-24 20:30:31 +05307147 if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC)
7148 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_RAW;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05307149 else if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS)
Manish Dewangana6fc5442015-08-24 20:30:31 +05307150 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_MP4ADTS;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05307151 else if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_LATM)
7152 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_MP4LATM;
Ashish Jainf1eaa582016-05-23 20:54:24 +05307153
7154 if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) ==
7155 AUDIO_FORMAT_PCM) {
7156
7157 /*Based on platform support, configure appropriate alsa format for corresponding
7158 *hal input format.
7159 */
7160 out->compr_config.codec->format = hal_format_to_alsa(
7161 config->offload_info.format);
7162
Ashish Jain83a6cc22016-06-28 14:34:17 +05307163 out->hal_op_format = alsa_format_to_hal(
Ashish Jainf1eaa582016-05-23 20:54:24 +05307164 out->compr_config.codec->format);
Ashish Jain83a6cc22016-06-28 14:34:17 +05307165 out->hal_ip_format = out->format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05307166
Dhananjay Kumarac341582017-02-23 23:42:25 +05307167 /*for direct non-compress playback populate bit_width based on selected alsa format as
Ashish Jainf1eaa582016-05-23 20:54:24 +05307168 *hal input format and alsa format might differ based on platform support.
7169 */
7170 out->bit_width = audio_bytes_per_sample(
Ashish Jain83a6cc22016-06-28 14:34:17 +05307171 out->hal_op_format) << 3;
Ashish Jainf1eaa582016-05-23 20:54:24 +05307172
7173 out->compr_config.fragments = DIRECT_PCM_NUM_FRAGMENTS;
7174
7175 /* Check if alsa session is configured with the same format as HAL input format,
7176 * if not then derive correct fragment size needed to accomodate the
7177 * conversion of HAL input format to alsa format.
7178 */
7179 audio_extn_utils_update_direct_pcm_fragment_size(out);
7180
7181 /*if hal input and output fragment size is different this indicates HAL input format is
7182 *not same as the alsa format
7183 */
Ashish Jain83a6cc22016-06-28 14:34:17 +05307184 if (out->hal_fragment_size != out->compr_config.fragment_size) {
Ashish Jainf1eaa582016-05-23 20:54:24 +05307185 /*Allocate a buffer to convert input data to the alsa configured format.
7186 *size of convert buffer is equal to the size required to hold one fragment size
7187 *worth of pcm data, this is because flinger does not write more than fragment_size
7188 */
Ashish Jain83a6cc22016-06-28 14:34:17 +05307189 out->convert_buffer = calloc(1,out->compr_config.fragment_size);
7190 if (out->convert_buffer == NULL){
Ashish Jainf1eaa582016-05-23 20:54:24 +05307191 ALOGE("Allocation failed for convert buffer for size %d", out->compr_config.fragment_size);
7192 ret = -ENOMEM;
7193 goto error_open;
7194 }
7195 }
7196 } else if (audio_extn_passthru_is_passthrough_stream(out)) {
7197 out->compr_config.fragment_size =
7198 audio_extn_passthru_get_buffer_size(&config->offload_info);
7199 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
7200 } else {
7201 out->compr_config.fragment_size =
7202 platform_get_compress_offload_buffer_size(&config->offload_info);
7203 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
7204 }
Mingming Yin3ee55c62014-08-04 14:23:35 -07007205
Naresh Tanniruee3499a2017-01-05 14:05:35 +05307206 if (out->flags & AUDIO_OUTPUT_FLAG_TIMESTAMP) {
7207 out->compr_config.fragment_size += sizeof(struct snd_codec_metadata);
7208 }
Amit Shekhar6f461b12014-08-01 14:52:58 -07007209 if (config->offload_info.format == AUDIO_FORMAT_FLAC)
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +05307210 out->compr_config.codec->options.flac_dec.sample_size = AUDIO_OUTPUT_BIT_WIDTH;
Mingming Yin3ee55c62014-08-04 14:23:35 -07007211
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05307212 if (config->offload_info.format == AUDIO_FORMAT_APTX) {
7213 audio_extn_send_aptx_dec_bt_addr_to_dsp(out);
7214 }
7215
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07007216 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
7217 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07007218
Manish Dewangan69426c82017-01-30 17:35:36 +05307219 if ((flags & AUDIO_OUTPUT_FLAG_TIMESTAMP) &&
7220 (flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC)) {
7221 out->render_mode = RENDER_MODE_AUDIO_STC_MASTER;
7222 } else if(flags & AUDIO_OUTPUT_FLAG_TIMESTAMP) {
7223 out->render_mode = RENDER_MODE_AUDIO_MASTER;
7224 } else {
7225 out->render_mode = RENDER_MODE_AUDIO_NO_TIMESTAMP;
7226 }
Alexy Josephaa54c872014-12-03 02:46:47 -08007227
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05307228 memset(&out->channel_map_param, 0,
7229 sizeof(struct audio_out_channel_map_param));
7230
Haynes Mathew George352f27b2013-07-26 00:00:15 -07007231 out->send_new_metadata = 1;
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05307232 out->send_next_track_params = false;
7233 out->is_compr_metadata_avail = false;
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08007234 out->offload_state = OFFLOAD_STATE_IDLE;
7235 out->playback_started = 0;
Zhou Song48453a02018-01-10 17:50:59 +08007236 out->writeAt.tv_sec = 0;
7237 out->writeAt.tv_nsec = 0;
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08007238
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08007239 audio_extn_dts_create_state_notifier_node(out->usecase);
7240
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07007241 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
7242 __func__, config->offload_info.version,
7243 config->offload_info.bit_rate);
Ashish Jain5106d362016-05-11 19:23:33 +05307244
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05307245 /* Check if DSD audio format is supported in codec
7246 * and there is no active native DSD use case
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05307247 */
7248
7249 if ((config->format == AUDIO_FORMAT_DSD) &&
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05307250 (!platform_check_codec_dsd_support(adev->platform) ||
7251 audio_is_dsd_native_stream_active(adev))) {
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05307252 ret = -EINVAL;
7253 goto error_open;
7254 }
7255
Ashish Jain5106d362016-05-11 19:23:33 +05307256 /* Disable gapless if any of the following is true
7257 * passthrough playback
7258 * AV playback
Dhananjay Kumarac341582017-02-23 23:42:25 +05307259 * non compressed Direct playback
Ashish Jain5106d362016-05-11 19:23:33 +05307260 */
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05307261 if (audio_extn_passthru_is_passthrough_stream(out) ||
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05307262 (config->format == AUDIO_FORMAT_DSD) ||
Naresh Tanniru928f0862017-04-07 16:44:23 -07007263 (config->format == AUDIO_FORMAT_IEC61937) ||
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05307264 config->offload_info.has_video ||
Dhananjay Kumarac341582017-02-23 23:42:25 +05307265 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Ashish Jain5106d362016-05-11 19:23:33 +05307266 check_and_set_gapless_mode(adev, false);
7267 } else
7268 check_and_set_gapless_mode(adev, true);
Mingming Yin21854652016-04-13 11:54:02 -07007269
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05307270 if (audio_extn_passthru_is_passthrough_stream(out)) {
Mingming Yin21854652016-04-13 11:54:02 -07007271 out->flags |= AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH;
7272 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05307273 if (config->format == AUDIO_FORMAT_DSD) {
7274 out->flags |= AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH;
7275 out->compr_config.codec->compr_passthr = PASSTHROUGH_DSD;
7276 }
Aalique Grahame0359a1f2016-09-08 16:54:22 -07007277
7278 create_offload_callback_thread(out);
7279
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07007280 } else if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
Arun Mirpuri7da752a2018-09-11 18:01:15 -07007281 switch (config->sample_rate) {
7282 case 0:
7283 out->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
7284 break;
7285 case 8000:
7286 case 16000:
7287 case 48000:
7288 out->sample_rate = config->sample_rate;
7289 break;
7290 default:
7291 ALOGE("%s: Unsupported sampling rate %d for Incall Music", __func__,
7292 config->sample_rate);
7293 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
7294 ret = -EINVAL;
7295 goto error_open;
7296 }
7297 //FIXME: add support for MONO stream configuration when audioflinger mixer supports it
7298 switch (config->channel_mask) {
7299 case AUDIO_CHANNEL_NONE:
7300 case AUDIO_CHANNEL_OUT_STEREO:
7301 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
7302 break;
7303 default:
7304 ALOGE("%s: Unsupported channel mask %#x for Incall Music", __func__,
7305 config->channel_mask);
7306 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
7307 ret = -EINVAL;
7308 goto error_open;
7309 }
7310 switch (config->format) {
7311 case AUDIO_FORMAT_DEFAULT:
7312 case AUDIO_FORMAT_PCM_16_BIT:
7313 out->format = AUDIO_FORMAT_PCM_16_BIT;
7314 break;
7315 default:
7316 ALOGE("%s: Unsupported format %#x for Incall Music", __func__,
7317 config->format);
7318 config->format = AUDIO_FORMAT_PCM_16_BIT;
7319 ret = -EINVAL;
7320 goto error_open;
7321 }
7322
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05307323 ret = voice_extn_check_and_set_incall_music_usecase(adev, out);
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07007324 if (ret != 0) {
7325 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
Arun Mirpuri7da752a2018-09-11 18:01:15 -07007326 __func__, ret);
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07007327 goto error_open;
7328 }
Arun Mirpuri7da752a2018-09-11 18:01:15 -07007329 } else if (out->devices == AUDIO_DEVICE_OUT_TELEPHONY_TX) {
Aalique Grahame22e49102018-12-18 14:23:57 -08007330 switch (config->sample_rate) {
7331 case 0:
7332 out->sample_rate = AFE_PROXY_SAMPLING_RATE;
7333 break;
7334 case 8000:
7335 case 16000:
7336 case 48000:
7337 out->sample_rate = config->sample_rate;
7338 break;
7339 default:
7340 ALOGE("%s: Unsupported sampling rate %d for Telephony TX", __func__,
7341 config->sample_rate);
7342 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
7343 ret = -EINVAL;
7344 break;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07007345 }
Aalique Grahame22e49102018-12-18 14:23:57 -08007346 //FIXME: add support for MONO stream configuration when audioflinger mixer supports it
7347 switch (config->channel_mask) {
7348 case AUDIO_CHANNEL_NONE:
7349 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
7350 break;
7351 case AUDIO_CHANNEL_OUT_STEREO:
7352 out->channel_mask = config->channel_mask;
7353 break;
7354 default:
7355 ALOGE("%s: Unsupported channel mask %#x for Telephony TX", __func__,
7356 config->channel_mask);
7357 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
7358 ret = -EINVAL;
7359 break;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07007360 }
Aalique Grahame22e49102018-12-18 14:23:57 -08007361 switch (config->format) {
7362 case AUDIO_FORMAT_DEFAULT:
7363 out->format = AUDIO_FORMAT_PCM_16_BIT;
7364 break;
7365 case AUDIO_FORMAT_PCM_16_BIT:
7366 out->format = config->format;
7367 break;
7368 default:
7369 ALOGE("%s: Unsupported format %#x for Telephony TX", __func__,
7370 config->format);
7371 config->format = AUDIO_FORMAT_PCM_16_BIT;
7372 ret = -EINVAL;
7373 break;
7374 }
7375 if (ret != 0)
7376 goto error_open;
7377
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07007378 out->usecase = USECASE_AUDIO_PLAYBACK_AFE_PROXY;
7379 out->config = pcm_config_afe_proxy_playback;
Aalique Grahame22e49102018-12-18 14:23:57 -08007380 out->config.rate = out->sample_rate;
7381 out->config.channels =
7382 audio_channel_count_from_out_mask(out->channel_mask);
7383 out->config.format = pcm_format_from_audio_format(out->format);
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07007384 adev->voice_tx_output = out;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07007385 } else {
Ashish Jain058165c2016-09-28 23:18:48 +05307386 unsigned int channels = 0;
7387 /*Update config params to default if not set by the caller*/
7388 if (config->sample_rate == 0)
7389 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
7390 if (config->channel_mask == AUDIO_CHANNEL_NONE)
7391 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
7392 if (config->format == AUDIO_FORMAT_DEFAULT)
7393 config->format = AUDIO_FORMAT_PCM_16_BIT;
7394
7395 channels = audio_channel_count_from_out_mask(out->channel_mask);
7396
Varun Balaraje49253e2017-07-06 19:48:56 +05307397 if (out->flags & AUDIO_OUTPUT_FLAG_INTERACTIVE) {
7398 out->usecase = get_interactive_usecase(adev);
7399 out->config = pcm_config_low_latency;
7400 } else if (out->flags & AUDIO_OUTPUT_FLAG_RAW) {
Ashish Jain83a6cc22016-06-28 14:34:17 +05307401 out->usecase = USECASE_AUDIO_PLAYBACK_ULL;
Haynes Mathew George5beddd42016-06-27 18:33:40 -07007402 out->realtime = may_use_noirq_mode(adev, USECASE_AUDIO_PLAYBACK_ULL,
7403 out->flags);
7404 out->config = out->realtime ? pcm_config_rt : pcm_config_low_latency;
Haynes Mathew George16081042017-05-31 17:16:49 -07007405 } else if (out->flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) {
7406 out->usecase = USECASE_AUDIO_PLAYBACK_MMAP;
7407 out->config = pcm_config_mmap_playback;
7408 out->stream.start = out_start;
7409 out->stream.stop = out_stop;
7410 out->stream.create_mmap_buffer = out_create_mmap_buffer;
7411 out->stream.get_mmap_position = out_get_mmap_position;
Ashish Jain83a6cc22016-06-28 14:34:17 +05307412 } else if (out->flags & AUDIO_OUTPUT_FLAG_FAST) {
7413 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
Aniket Kumar Lata932f4872017-11-06 18:29:44 -08007414 out->hal_output_suspend_supported =
7415 property_get_bool("vendor.audio.hal.output.suspend.supported", false);
7416 out->dynamic_pm_qos_config_supported =
7417 property_get_bool("vendor.audio.hal.dynamic.qos.config.supported", false);
7418 if (!out->dynamic_pm_qos_config_supported) {
Alexy Joseph98988832017-01-13 14:56:59 -08007419 ALOGI("%s: dynamic qos voting not enabled for platform", __func__);
7420 } else {
7421 ALOGI("%s: dynamic qos voting enabled for platform", __func__);
7422 //the mixer path will be a string similar to "low-latency-playback resume"
7423 strlcpy(out->pm_qos_mixer_path, use_case_table[out->usecase], MAX_MIXER_PATH_LEN);
7424 strlcat(out->pm_qos_mixer_path,
7425 " resume", MAX_MIXER_PATH_LEN);
7426 ALOGI("%s: created %s pm_qos_mixer_path" , __func__,
7427 out->pm_qos_mixer_path);
7428 }
Ashish Jain83a6cc22016-06-28 14:34:17 +05307429 out->config = pcm_config_low_latency;
7430 } else if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
7431 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
7432 out->config = pcm_config_deep_buffer;
Ashish Jain058165c2016-09-28 23:18:48 +05307433 out->config.period_size = get_output_period_size(config->sample_rate, out->format,
7434 channels, DEEP_BUFFER_OUTPUT_PERIOD_DURATION);
7435 if (out->config.period_size <= 0) {
7436 ALOGE("Invalid configuration period size is not valid");
7437 ret = -EINVAL;
7438 goto error_open;
7439 }
Aalique Grahame22e49102018-12-18 14:23:57 -08007440 } else if (flags & AUDIO_OUTPUT_FLAG_TTS) {
7441 out->usecase = USECASE_AUDIO_PLAYBACK_TTS;
7442 out->config = pcm_config_deep_buffer;
Vignesh Kulothungana6927272019-02-20 15:17:07 -08007443 } else if (config->channel_mask & AUDIO_CHANNEL_HAPTIC_ALL) {
7444 out->usecase = USECASE_AUDIO_PLAYBACK_WITH_HAPTICS;
7445 out->config = pcm_config_haptics_audio;
7446 if (force_haptic_path)
7447 adev->haptics_config = pcm_config_haptics_audio;
7448 else
7449 adev->haptics_config = pcm_config_haptics;
7450
7451 out->config.channels =
7452 audio_channel_count_from_out_mask(out->channel_mask & ~AUDIO_CHANNEL_HAPTIC_ALL);
7453
7454 if (force_haptic_path) {
7455 out->config.channels = 1;
7456 adev->haptics_config.channels = 1;
7457 } else
7458 adev->haptics_config.channels = audio_channel_count_from_out_mask(out->channel_mask & AUDIO_CHANNEL_HAPTIC_ALL);
Ashish Jain83a6cc22016-06-28 14:34:17 +05307459 } else {
7460 /* primary path is the default path selected if no other outputs are available/suitable */
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08007461 out->usecase = GET_USECASE_AUDIO_PLAYBACK_PRIMARY(use_db_as_primary);
7462 out->config = GET_PCM_CONFIG_AUDIO_PLAYBACK_PRIMARY(use_db_as_primary);
Ashish Jain83a6cc22016-06-28 14:34:17 +05307463 }
7464 out->hal_ip_format = format = out->format;
7465 out->config.format = hal_format_to_pcm(out->hal_ip_format);
7466 out->hal_op_format = pcm_format_to_hal(out->config.format);
7467 out->bit_width = format_to_bitwidth_table[out->hal_op_format] << 3;
7468 out->config.rate = config->sample_rate;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07007469 out->sample_rate = out->config.rate;
Ashish Jain058165c2016-09-28 23:18:48 +05307470 out->config.channels = channels;
Ashish Jain83a6cc22016-06-28 14:34:17 +05307471 if (out->hal_ip_format != out->hal_op_format) {
7472 uint32_t buffer_size = out->config.period_size *
7473 format_to_bitwidth_table[out->hal_op_format] *
7474 out->config.channels;
7475 out->convert_buffer = calloc(1, buffer_size);
7476 if (out->convert_buffer == NULL){
7477 ALOGE("Allocation failed for convert buffer for size %d",
7478 out->compr_config.fragment_size);
7479 ret = -ENOMEM;
7480 goto error_open;
7481 }
7482 ALOGD("Convert buffer allocated of size %d", buffer_size);
7483 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007484 }
7485
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08007486 ALOGV("%s devices:%d, format:%x, out->sample_rate:%d,out->bit_width:%d out->format:%d out->flags:%x, flags: %x usecase %d",
7487 __func__, devices, format, out->sample_rate, out->bit_width, out->format, out->flags, flags, out->usecase);
Ashish Jain83a6cc22016-06-28 14:34:17 +05307488
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07007489 /* TODO remove this hardcoding and check why width is zero*/
7490 if (out->bit_width == 0)
7491 out->bit_width = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05307492 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07007493 &adev->streams_output_cfg_list,
Aalique Grahame65780b52017-09-27 14:59:56 -07007494 devices, out->flags, out->hal_op_format, out->sample_rate,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +05307495 out->bit_width, out->channel_mask, out->profile,
Manish Dewangan837dc462015-05-27 10:17:41 +05307496 &out->app_type_cfg);
Aalique Grahame6e763712019-01-31 16:18:17 -08007497 if ((out->usecase == (audio_usecase_t)(GET_USECASE_AUDIO_PLAYBACK_PRIMARY(use_db_as_primary))) ||
Haynes Mathew Georgebf143712013-12-03 13:02:53 -08007498 (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
7499 /* Ensure the default output is not selected twice */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08007500 if(adev->primary_output == NULL)
7501 adev->primary_output = out;
7502 else {
7503 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07007504 ret = -EEXIST;
7505 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08007506 }
7507 }
7508
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007509 /* Check if this usecase is already existing */
7510 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella7ce05352014-04-17 20:00:41 -07007511 if ((get_usecase_from_list(adev, out->usecase) != NULL) &&
7512 (out->usecase != USECASE_COMPRESS_VOIP_CALL)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007513 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007514 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07007515 ret = -EEXIST;
7516 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007517 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08007518
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007519 pthread_mutex_unlock(&adev->lock);
7520
7521 out->stream.common.get_sample_rate = out_get_sample_rate;
7522 out->stream.common.set_sample_rate = out_set_sample_rate;
7523 out->stream.common.get_buffer_size = out_get_buffer_size;
7524 out->stream.common.get_channels = out_get_channels;
7525 out->stream.common.get_format = out_get_format;
7526 out->stream.common.set_format = out_set_format;
7527 out->stream.common.standby = out_standby;
7528 out->stream.common.dump = out_dump;
7529 out->stream.common.set_parameters = out_set_parameters;
7530 out->stream.common.get_parameters = out_get_parameters;
7531 out->stream.common.add_audio_effect = out_add_audio_effect;
7532 out->stream.common.remove_audio_effect = out_remove_audio_effect;
7533 out->stream.get_latency = out_get_latency;
7534 out->stream.set_volume = out_set_volume;
Aalique Grahame22e49102018-12-18 14:23:57 -08007535#ifdef NO_AUDIO_OUT
7536 out->stream.write = out_write_for_no_output;
7537#else
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007538 out->stream.write = out_write;
Aalique Grahame22e49102018-12-18 14:23:57 -08007539#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007540 out->stream.get_render_position = out_get_render_position;
7541 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07007542 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007543
Haynes Mathew George16081042017-05-31 17:16:49 -07007544 if (out->realtime)
7545 out->af_period_multiplier = af_period_multiplier;
7546 else
7547 out->af_period_multiplier = 1;
7548
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007549 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07007550 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07007551 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007552
7553 config->format = out->stream.common.get_format(&out->stream.common);
7554 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
7555 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
Naresh Tanniru04f71882018-06-26 17:46:22 +05307556 register_format(out->format, out->supported_formats);
7557 register_channel_mask(out->channel_mask, out->supported_channel_masks);
7558 register_sample_rate(out->sample_rate, out->supported_sample_rates);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007559
Aalique Grahame22e49102018-12-18 14:23:57 -08007560 out->error_log = error_log_create(
7561 ERROR_LOG_ENTRIES,
7562 1000000000 /* aggregate consecutive identical errors within one second in ns */);
7563
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05307564 /*
7565 By locking output stream before registering, we allow the callback
7566 to update stream's state only after stream's initial state is set to
7567 adev state.
7568 */
7569 lock_output_stream(out);
7570 audio_extn_snd_mon_register_listener(out, out_snd_mon_cb);
7571 pthread_mutex_lock(&adev->lock);
7572 out->card_status = adev->card_status;
7573 pthread_mutex_unlock(&adev->lock);
7574 pthread_mutex_unlock(&out->lock);
7575
Aalique Grahame22e49102018-12-18 14:23:57 -08007576 stream_app_type_cfg_init(&out->app_type_cfg);
7577
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007578 *stream_out = &out->stream;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05307579 ALOGD("%s: Stream (%p) picks up usecase (%s)", __func__, &out->stream,
vivek mehta0ea887a2015-08-26 14:01:20 -07007580 use_case_table[out->usecase]);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08007581
7582 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
7583 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
7584 popcount(out->channel_mask), out->playback_started);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08007585 /* setup a channel for client <--> adsp communication for stream events */
Manish Dewangan21a850a2017-08-14 12:03:55 +05307586 is_direct_passthough = audio_extn_passthru_is_direct_passthrough(out);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08007587 if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
Naresh Tanniru85819452017-05-04 18:55:45 -07007588 (out->flags & AUDIO_OUTPUT_FLAG_DIRECT_PCM) ||
Gangadhar Sb0210342019-02-22 17:39:41 +05307589 audio_extn_ip_hdlr_intf_supported_for_copp(adev->platform) ||
7590 (audio_extn_ip_hdlr_intf_supported(config->format, is_direct_passthough, false))) {
Ben Rombergerd771a7c2017-02-22 18:05:17 -08007591 hdlr_stream_cfg.pcm_device_id = platform_get_pcm_device_id(
7592 out->usecase, PCM_PLAYBACK);
7593 hdlr_stream_cfg.flags = out->flags;
7594 hdlr_stream_cfg.type = PCM_PLAYBACK;
7595 ret = audio_extn_adsp_hdlr_stream_open(&out->adsp_hdlr_stream_handle,
7596 &hdlr_stream_cfg);
7597 if (ret) {
7598 ALOGE("%s: adsp_hdlr_stream_open failed %d",__func__, ret);
7599 out->adsp_hdlr_stream_handle = NULL;
7600 }
7601 }
Gangadhar Sb0210342019-02-22 17:39:41 +05307602 ip_hdlr_stream = audio_extn_ip_hdlr_intf_supported(config->format,
7603 is_direct_passthough, false);
7604 ip_hdlr_dev = audio_extn_ip_hdlr_intf_supported_for_copp(adev->platform);
7605 if (ip_hdlr_stream || ip_hdlr_dev ) {
Vidyakumar Athota2062f912017-06-27 14:46:15 -07007606 ret = audio_extn_ip_hdlr_intf_init(&out->ip_hdlr_handle, NULL, NULL, adev, out->usecase);
Naresh Tanniru85819452017-05-04 18:55:45 -07007607 if (ret < 0) {
7608 ALOGE("%s: audio_extn_ip_hdlr_intf_init failed %d",__func__, ret);
7609 out->ip_hdlr_handle = NULL;
7610 }
7611 }
Derek Chenf939fb72018-11-13 13:34:41 -08007612
7613 streams_output_ctxt_t *out_ctxt = (streams_output_ctxt_t *)
7614 calloc(1, sizeof(streams_output_ctxt_t));
7615 if (out_ctxt == NULL) {
7616 ALOGE("%s fail to allocate output ctxt", __func__);
7617 ret = -ENOMEM;
7618 goto error_open;
7619 }
7620 out_ctxt->output = out;
7621
7622 pthread_mutex_lock(&adev->lock);
7623 list_add_tail(&adev->active_outputs_list, &out_ctxt->list);
7624 pthread_mutex_unlock(&adev->lock);
7625
Eric Laurent994a6932013-07-17 11:51:42 -07007626 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007627 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07007628
7629error_open:
Ashish Jain83a6cc22016-06-28 14:34:17 +05307630 if (out->convert_buffer)
7631 free(out->convert_buffer);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07007632 free(out);
7633 *stream_out = NULL;
7634 ALOGD("%s: exit: ret %d", __func__, ret);
7635 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007636}
7637
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05307638void adev_close_output_stream(struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007639 struct audio_stream_out *stream)
7640{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07007641 struct stream_out *out = (struct stream_out *)stream;
7642 struct audio_device *adev = out->dev;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007643 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07007644
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007645 ALOGD("%s: enter:stream_handle(%s)",__func__, use_case_table[out->usecase]);
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05307646
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05307647 // must deregister from sndmonitor first to prevent races
7648 // between the callback and close_stream
7649 audio_extn_snd_mon_unregister_listener(out);
7650
Ben Rombergerd771a7c2017-02-22 18:05:17 -08007651 /* close adsp hdrl session before standby */
7652 if (out->adsp_hdlr_stream_handle) {
7653 ret = audio_extn_adsp_hdlr_stream_close(out->adsp_hdlr_stream_handle);
7654 if (ret)
7655 ALOGE("%s: adsp_hdlr_stream_close failed %d",__func__, ret);
7656 out->adsp_hdlr_stream_handle = NULL;
7657 }
7658
Manish Dewangan21a850a2017-08-14 12:03:55 +05307659 if (out->ip_hdlr_handle) {
Naresh Tanniru85819452017-05-04 18:55:45 -07007660 audio_extn_ip_hdlr_intf_deinit(out->ip_hdlr_handle);
7661 out->ip_hdlr_handle = NULL;
7662 }
7663
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007664 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05307665 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007666 ret = voice_extn_compress_voip_close_output_stream(&stream->common);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05307667 out->started = 0;
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05307668 pthread_mutex_unlock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007669 if(ret != 0)
7670 ALOGE("%s: Compress voip output cannot be closed, error:%d",
7671 __func__, ret);
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07007672 } else
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007673 out_standby(&stream->common);
7674
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07007675 if (is_offload_usecase(out->usecase)) {
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08007676 audio_extn_dts_remove_state_notifier_node(out->usecase);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07007677 destroy_offload_callback_thread(out);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07007678 free_offload_usecase(adev, out->usecase);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07007679 if (out->compr_config.codec != NULL)
7680 free(out->compr_config.codec);
7681 }
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07007682
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05307683 out->a2dp_compress_mute = false;
7684
Varun Balaraje49253e2017-07-06 19:48:56 +05307685 if (is_interactive_usecase(out->usecase))
7686 free_interactive_usecase(adev, out->usecase);
7687
Ashish Jain83a6cc22016-06-28 14:34:17 +05307688 if (out->convert_buffer != NULL) {
7689 free(out->convert_buffer);
7690 out->convert_buffer = NULL;
7691 }
7692
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07007693 if (adev->voice_tx_output == out)
7694 adev->voice_tx_output = NULL;
7695
Aalique Grahame22e49102018-12-18 14:23:57 -08007696 error_log_destroy(out->error_log);
7697 out->error_log = NULL;
7698
Dhanalakshmi Siddani6c3d0992017-01-16 16:52:33 +05307699 if (adev->primary_output == out)
7700 adev->primary_output = NULL;
7701
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07007702 pthread_cond_destroy(&out->cond);
7703 pthread_mutex_destroy(&out->lock);
Derek Chenf939fb72018-11-13 13:34:41 -08007704
7705 pthread_mutex_lock(&adev->lock);
7706 streams_output_ctxt_t *out_ctxt = out_get_stream(adev, out->handle);
7707 if (out_ctxt != NULL) {
7708 list_remove(&out_ctxt->list);
7709 free(out_ctxt);
7710 } else {
7711 ALOGW("%s, output stream already closed", __func__);
7712 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007713 free(stream);
Derek Chenf939fb72018-11-13 13:34:41 -08007714 pthread_mutex_unlock(&adev->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07007715 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007716}
7717
7718static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
7719{
7720 struct audio_device *adev = (struct audio_device *)dev;
7721 struct str_parms *parms;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007722 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07007723 int val;
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07007724 int ret;
7725 int status = 0;
Aalique Grahame22e49102018-12-18 14:23:57 -08007726 bool a2dp_reconfig = false;
Zhou Songd6d71752019-05-21 18:08:51 +08007727 struct listnode *node;
7728 struct audio_usecase *usecase = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007729
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08007730 ALOGD("%s: enter: %s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007731 parms = str_parms_create_str(kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007732
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05307733 if (!parms)
7734 goto error;
Naresh Tanniru4c630392014-05-12 01:05:52 +05307735
Zhou Songd6d71752019-05-21 18:08:51 +08007736 pthread_mutex_lock(&adev->lock);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05307737 ret = str_parms_get_str(parms, "BT_SCO", value, sizeof(value));
7738 if (ret >= 0) {
7739 /* When set to false, HAL should disable EC and NS */
Zhou Songd6d71752019-05-21 18:08:51 +08007740 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0){
Ashish Jain1b9b30c2017-05-18 20:57:40 +05307741 adev->bt_sco_on = true;
Zhou Songd6d71752019-05-21 18:08:51 +08007742 } else {
7743 ALOGD("sco is off, reset sco and route device to handset/mic");
Ashish Jain1b9b30c2017-05-18 20:57:40 +05307744 adev->bt_sco_on = false;
Zhou Songd6d71752019-05-21 18:08:51 +08007745 audio_extn_sco_reset_configuration();
7746 list_for_each(node, &adev->usecase_list) {
7747 usecase = node_to_item(node, struct audio_usecase, list);
7748 if ((usecase->type == PCM_PLAYBACK) && usecase->stream.out &&
7749 (usecase->stream.out->devices & AUDIO_DEVICE_OUT_ALL_SCO))
7750 usecase->stream.out->devices = AUDIO_DEVICE_OUT_EARPIECE;
7751 else if ((usecase->type == PCM_CAPTURE) && usecase->stream.in &&
7752 (usecase->stream.in->device & AUDIO_DEVICE_IN_ALL_SCO))
7753 usecase->stream.in->device = AUDIO_DEVICE_IN_BUILTIN_MIC;
7754 else
7755 continue;
7756 select_devices(adev, usecase->id);
7757 }
7758 }
Ashish Jain1b9b30c2017-05-18 20:57:40 +05307759 }
7760
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07007761 status = voice_set_parameters(adev, parms);
7762 if (status != 0)
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08007763 goto done;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007764
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07007765 status = platform_set_parameters(adev->platform, parms);
7766 if (status != 0)
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08007767 goto done;
7768
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07007769 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
7770 if (ret >= 0) {
Vicky Sehrawate240e5d2014-08-12 17:17:04 -07007771 /* When set to false, HAL should disable EC and NS */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007772 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
7773 adev->bluetooth_nrec = true;
7774 else
7775 adev->bluetooth_nrec = false;
7776 }
7777
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07007778 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
7779 if (ret >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007780 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
7781 adev->screen_off = false;
7782 else
7783 adev->screen_off = true;
7784 }
7785
Eric Laurent4b084132018-10-19 17:33:43 -07007786 ret = str_parms_get_int(parms, "rotation", &val);
7787 if (ret >= 0) {
7788 bool reverse_speakers = false;
7789 int camera_rotation = CAMERA_ROTATION_LANDSCAPE;
7790 switch (val) {
7791 // FIXME: note that the code below assumes that the speakers are in the correct placement
7792 // relative to the user when the device is rotated 90deg from its default rotation. This
7793 // assumption is device-specific, not platform-specific like this code.
7794 case 270:
7795 reverse_speakers = true;
7796 camera_rotation = CAMERA_ROTATION_INVERT_LANDSCAPE;
7797 break;
7798 case 0:
7799 case 180:
7800 camera_rotation = CAMERA_ROTATION_PORTRAIT;
7801 break;
7802 case 90:
7803 camera_rotation = CAMERA_ROTATION_LANDSCAPE;
7804 break;
7805 default:
7806 ALOGE("%s: unexpected rotation of %d", __func__, val);
7807 status = -EINVAL;
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07007808 }
Eric Laurent4b084132018-10-19 17:33:43 -07007809 if (status == 0) {
7810 // check and set swap
7811 // - check if orientation changed and speaker active
7812 // - set rotation and cache the rotation value
7813 adev->camera_orientation =
7814 (adev->camera_orientation & ~CAMERA_ROTATION_MASK) | camera_rotation;
7815 if (!audio_extn_is_maxx_audio_enabled())
7816 platform_check_and_set_swap_lr_channels(adev, reverse_speakers);
7817 }
7818 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07007819
Mingming Yin514a8bc2014-07-29 15:22:21 -07007820 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_SCO_WB, value, sizeof(value));
7821 if (ret >= 0) {
7822 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
7823 adev->bt_wb_speech_enabled = true;
7824 else
7825 adev->bt_wb_speech_enabled = false;
7826 }
7827
Zhou Song12c29502019-03-16 10:37:18 +08007828 ret = str_parms_get_str(parms, "bt_swb", value, sizeof(value));
7829 if (ret >= 0) {
7830 val = atoi(value);
7831 adev->swb_speech_mode = val;
7832 }
7833
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -07007834 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value, sizeof(value));
7835 if (ret >= 0) {
7836 val = atoi(value);
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05307837 audio_devices_t device = (audio_devices_t) val;
Zhou Song681350a2017-10-19 16:28:42 +08007838 if (audio_is_output_device(val) &&
7839 (val & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07007840 ALOGV("cache new ext disp type and edid");
7841 ret = platform_get_ext_disp_type(adev->platform);
7842 if (ret < 0) {
7843 ALOGE("%s: Failed to query disp type, ret:%d", __func__, ret);
Manisha Agarwal2f5ff882018-08-08 17:09:29 +05307844 } else {
7845 platform_cache_edid(adev->platform);
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07007846 }
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05307847 } else if (audio_is_usb_out_device(device) || audio_is_usb_in_device(device)) {
vivek mehta344576a2016-04-12 18:56:03 -07007848 /*
7849 * Do not allow AFE proxy port usage by WFD source when USB headset is connected.
7850 * Per AudioPolicyManager, USB device is higher priority than WFD.
7851 * For Voice call over USB headset, voice call audio is routed to AFE proxy ports.
7852 * If WFD use case occupies AFE proxy, it may result unintended behavior while
7853 * starting voice call on USB
7854 */
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08007855 ret = str_parms_get_str(parms, "card", value, sizeof(value));
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05307856 if (ret >= 0)
7857 audio_extn_usb_add_device(device, atoi(value));
7858
Zhou Song6f862822017-11-06 17:27:57 +08007859 if (!audio_extn_usb_is_tunnel_supported()) {
7860 ALOGV("detected USB connect .. disable proxy");
7861 adev->allow_afe_proxy_usage = false;
7862 }
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -07007863 }
7864 }
7865
7866 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value, sizeof(value));
7867 if (ret >= 0) {
7868 val = atoi(value);
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05307869 audio_devices_t device = (audio_devices_t) val;
Garmond Leunge3b6d482016-10-25 16:48:01 -07007870 /*
7871 * The HDMI / Displayport disconnect handling has been moved to
7872 * audio extension to ensure that its parameters are not
7873 * invalidated prior to updating sysfs of the disconnect event
7874 * Invalidate will be handled by audio_extn_ext_disp_set_parameters()
7875 */
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05307876 if (audio_is_usb_out_device(device) || audio_is_usb_in_device(device)) {
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08007877 ret = str_parms_get_str(parms, "card", value, sizeof(value));
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05307878 if (ret >= 0)
7879 audio_extn_usb_remove_device(device, atoi(value));
7880
Zhou Song6f862822017-11-06 17:27:57 +08007881 if (!audio_extn_usb_is_tunnel_supported()) {
7882 ALOGV("detected USB disconnect .. enable proxy");
7883 adev->allow_afe_proxy_usage = true;
7884 }
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -07007885 }
7886 }
7887
Aalique Grahame22e49102018-12-18 14:23:57 -08007888 audio_extn_hfp_set_parameters(adev, parms);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08007889 audio_extn_qdsp_set_parameters(adev, parms);
Aalique Grahame22e49102018-12-18 14:23:57 -08007890
7891 status = audio_extn_a2dp_set_parameters(parms, &a2dp_reconfig);
Aniket Kumar Lata23300322019-02-20 22:25:30 -08007892 if (status >= 0 && a2dp_reconfig) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05307893 struct audio_usecase *usecase;
7894 struct listnode *node;
7895 list_for_each(node, &adev->usecase_list) {
7896 usecase = node_to_item(node, struct audio_usecase, list);
Sujin Panicker390724d2019-04-26 10:43:36 +05307897 if (usecase->stream.out && (usecase->type == PCM_PLAYBACK) &&
Naresh Tanniruf7e9e632016-11-04 14:54:20 -07007898 (usecase->devices & AUDIO_DEVICE_OUT_ALL_A2DP)){
Naresh Tanniru9d027a62015-03-13 01:32:10 +05307899 ALOGD("reconfigure a2dp... forcing device switch");
Weiyin Jiang425180d2017-06-05 16:40:23 +08007900
7901 pthread_mutex_unlock(&adev->lock);
Naresh Tannirucd2353e2016-08-19 00:37:25 +05307902 lock_output_stream(usecase->stream.out);
Weiyin Jiang425180d2017-06-05 16:40:23 +08007903 pthread_mutex_lock(&adev->lock);
Naresh Tannirucd2353e2016-08-19 00:37:25 +05307904 audio_extn_a2dp_set_handoff_mode(true);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05307905 //force device switch to re configure encoder
7906 select_devices(adev, usecase->id);
Naresh Tannirucd2353e2016-08-19 00:37:25 +05307907 audio_extn_a2dp_set_handoff_mode(false);
7908 pthread_mutex_unlock(&usecase->stream.out->lock);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05307909 break;
7910 }
7911 }
7912 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08007913
7914 //handle vr audio setparam
7915 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_VR_AUDIO_MODE,
7916 value, sizeof(value));
7917 if (ret >= 0) {
7918 ALOGI("Setting vr mode to be %s", value);
7919 if (!strncmp(value, "true", 4)) {
7920 adev->vr_audio_mode_enabled = true;
7921 ALOGI("Setting vr mode to true");
7922 } else if (!strncmp(value, "false", 5)) {
7923 adev->vr_audio_mode_enabled = false;
7924 ALOGI("Setting vr mode to false");
7925 } else {
7926 ALOGI("wrong vr mode set");
7927 }
7928 }
7929
Eric Laurent4b084132018-10-19 17:33:43 -07007930 //FIXME: to be replaced by proper video capture properties API
7931 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_CAMERA_FACING, value, sizeof(value));
7932 if (ret >= 0) {
7933 int camera_facing = CAMERA_FACING_BACK;
7934 if (strcmp(value, AUDIO_PARAMETER_VALUE_FRONT) == 0)
7935 camera_facing = CAMERA_FACING_FRONT;
7936 else if (strcmp(value, AUDIO_PARAMETER_VALUE_BACK) == 0)
7937 camera_facing = CAMERA_FACING_BACK;
7938 else {
7939 ALOGW("%s: invalid camera facing value: %s", __func__, value);
7940 goto done;
7941 }
7942 adev->camera_orientation =
7943 (adev->camera_orientation & ~CAMERA_FACING_MASK) | camera_facing;
7944 struct audio_usecase *usecase;
7945 struct listnode *node;
7946 list_for_each(node, &adev->usecase_list) {
7947 usecase = node_to_item(node, struct audio_usecase, list);
7948 struct stream_in *in = usecase->stream.in;
7949 if (usecase->type == PCM_CAPTURE && in != NULL &&
7950 in->source == AUDIO_SOURCE_CAMCORDER && !in->standby) {
7951 select_devices(adev, in->usecase);
7952 }
7953 }
7954 }
7955
Naresh Tannirucd2353e2016-08-19 00:37:25 +05307956 audio_extn_set_parameters(adev, parms);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08007957done:
7958 str_parms_destroy(parms);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08007959 pthread_mutex_unlock(&adev->lock);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05307960error:
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07007961 ALOGV("%s: exit with code(%d)", __func__, status);
7962 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007963}
7964
7965static char* adev_get_parameters(const struct audio_hw_device *dev,
7966 const char *keys)
7967{
Sidipotu Ashokaa4fa6a2018-12-21 09:19:26 +05307968 ALOGD("%s:%s", __func__, keys);
7969
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07007970 struct audio_device *adev = (struct audio_device *)dev;
7971 struct str_parms *reply = str_parms_create();
7972 struct str_parms *query = str_parms_create_str(keys);
7973 char *str;
Naresh Tannirud7205b62014-06-20 02:54:48 +05307974 char value[256] = {0};
7975 int ret = 0;
7976
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07007977 if (!query || !reply) {
Alexy Josephaee4fdd2016-01-29 13:02:07 -08007978 if (reply) {
7979 str_parms_destroy(reply);
7980 }
7981 if (query) {
7982 str_parms_destroy(query);
7983 }
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07007984 ALOGE("adev_get_parameters: failed to create query or reply");
7985 return NULL;
7986 }
7987
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08007988 //handle vr audio getparam
7989
7990 ret = str_parms_get_str(query,
7991 AUDIO_PARAMETER_KEY_VR_AUDIO_MODE,
7992 value, sizeof(value));
7993
7994 if (ret >= 0) {
7995 bool vr_audio_enabled = false;
7996 pthread_mutex_lock(&adev->lock);
7997 vr_audio_enabled = adev->vr_audio_mode_enabled;
7998 pthread_mutex_unlock(&adev->lock);
7999
8000 ALOGI("getting vr mode to %d", vr_audio_enabled);
8001
8002 if (vr_audio_enabled) {
8003 str_parms_add_str(reply, AUDIO_PARAMETER_KEY_VR_AUDIO_MODE,
8004 "true");
8005 goto exit;
8006 } else {
8007 str_parms_add_str(reply, AUDIO_PARAMETER_KEY_VR_AUDIO_MODE,
8008 "false");
8009 goto exit;
8010 }
8011 }
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07008012
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08008013 pthread_mutex_lock(&adev->lock);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07008014 audio_extn_get_parameters(adev, query, reply);
Shiv Maliyappanahallif9308492013-12-12 12:18:09 -08008015 voice_get_parameters(adev, query, reply);
Aalique Grahame22e49102018-12-18 14:23:57 -08008016 audio_extn_a2dp_get_parameters(query, reply);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07008017 platform_get_parameters(adev->platform, query, reply);
Naresh Tanniru80659832014-06-04 18:17:56 +05308018 pthread_mutex_unlock(&adev->lock);
8019
Naresh Tannirud7205b62014-06-20 02:54:48 +05308020exit:
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07008021 str = str_parms_to_str(reply);
8022 str_parms_destroy(query);
8023 str_parms_destroy(reply);
8024
Sidipotu Ashokaa4fa6a2018-12-21 09:19:26 +05308025 ALOGD("%s: exit: returns - %s", __func__, str);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07008026 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008027}
8028
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07008029static int adev_init_check(const struct audio_hw_device *dev __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008030{
8031 return 0;
8032}
8033
8034static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
8035{
Haynes Mathew George5191a852013-09-11 14:19:36 -07008036 int ret;
8037 struct audio_device *adev = (struct audio_device *)dev;
Aalique Grahame22e49102018-12-18 14:23:57 -08008038
8039 audio_extn_extspk_set_voice_vol(adev->extspk, volume);
8040
Haynes Mathew George5191a852013-09-11 14:19:36 -07008041 pthread_mutex_lock(&adev->lock);
8042 /* cache volume */
Shruthi Krishnaace10852013-10-25 14:32:12 -07008043 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07008044 pthread_mutex_unlock(&adev->lock);
8045 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008046}
8047
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07008048static int adev_set_master_volume(struct audio_hw_device *dev __unused,
8049 float volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008050{
8051 return -ENOSYS;
8052}
8053
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07008054static int adev_get_master_volume(struct audio_hw_device *dev __unused,
8055 float *volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008056{
8057 return -ENOSYS;
8058}
8059
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07008060static int adev_set_master_mute(struct audio_hw_device *dev __unused,
8061 bool muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008062{
8063 return -ENOSYS;
8064}
8065
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07008066static int adev_get_master_mute(struct audio_hw_device *dev __unused,
8067 bool *muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008068{
8069 return -ENOSYS;
8070}
8071
8072static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
8073{
8074 struct audio_device *adev = (struct audio_device *)dev;
Garmond Leung5fd0b552018-04-17 11:56:12 -07008075 struct listnode *node;
8076 struct audio_usecase *usecase = NULL;
8077 int ret = 0;
kunleizdc4af9d2017-05-04 12:15:35 +08008078
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008079 pthread_mutex_lock(&adev->lock);
8080 if (adev->mode != mode) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07008081 ALOGD("%s: mode %d\n", __func__, mode);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008082 adev->mode = mode;
Kunlei Zhang1d5c7f22019-05-21 14:25:57 +08008083 if (voice_is_in_call(adev) &&
8084 (mode == AUDIO_MODE_NORMAL ||
8085 (mode == AUDIO_MODE_IN_COMMUNICATION && !voice_is_call_state_active(adev)))) {
Garmond Leung5fd0b552018-04-17 11:56:12 -07008086 list_for_each(node, &adev->usecase_list) {
8087 usecase = node_to_item(node, struct audio_usecase, list);
8088 if (usecase->type == VOICE_CALL)
8089 break;
8090 }
8091 if (usecase &&
8092 audio_is_usb_out_device(usecase->out_snd_device & AUDIO_DEVICE_OUT_ALL_USB)) {
8093 ret = audio_extn_usb_check_and_set_svc_int(usecase,
8094 true);
8095 if (ret != 0) {
8096 /* default service interval was successfully updated,
8097 reopen USB backend with new service interval */
8098 check_usecases_codec_backend(adev,
8099 usecase,
8100 usecase->out_snd_device);
8101 }
8102 }
8103
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07008104 voice_stop_call(adev);
Banajit Goswami20cdd212015-09-11 01:11:30 -07008105 platform_set_gsm_mode(adev->platform, false);
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07008106 adev->current_call_output = NULL;
kunleizdc4af9d2017-05-04 12:15:35 +08008107 // restore device for other active usecases after stop call
8108 list_for_each(node, &adev->usecase_list) {
8109 usecase = node_to_item(node, struct audio_usecase, list);
8110 select_devices(adev, usecase->id);
8111 }
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07008112 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008113 }
8114 pthread_mutex_unlock(&adev->lock);
8115 return 0;
8116}
8117
8118static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
8119{
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08008120 int ret;
Aalique Grahame22e49102018-12-18 14:23:57 -08008121 struct audio_device *adev = (struct audio_device *)dev;
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08008122
8123 pthread_mutex_lock(&adev->lock);
Vidyakumar Athota2850d532013-11-19 16:02:12 -08008124 ALOGD("%s state %d\n", __func__, state);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08008125 ret = voice_set_mic_mute((struct audio_device *)dev, state);
Aalique Grahame22e49102018-12-18 14:23:57 -08008126
Derek Chend2530072014-11-24 12:39:14 -08008127 if (adev->ext_hw_plugin)
8128 ret = audio_extn_ext_hw_plugin_set_mic_mute(adev->ext_hw_plugin, state);
Aalique Grahame22e49102018-12-18 14:23:57 -08008129
8130 adev->mic_muted = state;
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08008131 pthread_mutex_unlock(&adev->lock);
8132
8133 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008134}
8135
8136static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
8137{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07008138 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008139 return 0;
8140}
8141
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07008142static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008143 const struct audio_config *config)
8144{
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07008145 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008146
Aalique Grahame22e49102018-12-18 14:23:57 -08008147 /* Don't know if USB HIFI in this context so use true to be conservative */
8148 if (check_input_parameters(config->sample_rate, config->format, channel_count,
8149 true /*is_usb_hifi */) != 0)
8150 return 0;
8151
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07008152 return get_input_buffer_size(config->sample_rate, config->format, channel_count,
8153 false /* is_low_latency: since we don't know, be conservative */);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008154}
8155
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008156static bool adev_input_allow_hifi_record(struct audio_device *adev,
8157 audio_devices_t devices,
8158 audio_input_flags_t flags,
8159 audio_source_t source) {
8160 const bool allowed = true;
8161
8162 if (!audio_is_usb_in_device(devices))
8163 return !allowed;
8164
8165 switch (flags) {
8166 case AUDIO_INPUT_FLAG_NONE:
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008167 break;
Haynes Mathew George59862182017-10-24 16:23:57 -07008168 case AUDIO_INPUT_FLAG_FAST: // disallow hifi record for FAST as
8169 // it affects RTD numbers over USB
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008170 default:
8171 return !allowed;
8172 }
8173
8174 switch (source) {
8175 case AUDIO_SOURCE_DEFAULT:
8176 case AUDIO_SOURCE_MIC:
8177 case AUDIO_SOURCE_UNPROCESSED:
8178 break;
8179 default:
8180 return !allowed;
8181 }
8182
8183 switch (adev->mode) {
8184 case 0:
8185 break;
8186 default:
8187 return !allowed;
8188 }
8189
8190 return allowed;
8191}
8192
Haynes Mathew George4ffef292017-11-21 15:08:02 -08008193static int adev_update_voice_comm_input_stream(struct stream_in *in,
8194 struct audio_config *config)
8195{
8196 bool valid_rate = (config->sample_rate == 8000 ||
8197 config->sample_rate == 16000 ||
8198 config->sample_rate == 32000 ||
8199 config->sample_rate == 48000);
8200 bool valid_ch = audio_channel_count_from_in_mask(in->channel_mask) == 1;
8201
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008202 if(!voice_extn_is_compress_voip_supported()) {
kunleiz28c73e72019-03-27 17:24:04 +08008203 if (valid_rate && valid_ch) {
Haynes Mathew George4ffef292017-11-21 15:08:02 -08008204 in->usecase = USECASE_AUDIO_RECORD_VOIP;
8205 in->config = default_pcm_config_voip_copp;
8206 in->config.period_size = VOIP_IO_BUF_SIZE(in->sample_rate,
8207 DEFAULT_VOIP_BUF_DURATION_MS,
8208 DEFAULT_VOIP_BIT_DEPTH_BYTE)/2;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008209 } else {
8210 ALOGW("%s No valid input in voip, use defaults"
8211 "sample rate %u, channel mask 0x%X",
8212 __func__, config->sample_rate, in->channel_mask);
8213 }
Haynes Mathew George4ffef292017-11-21 15:08:02 -08008214 in->config.rate = config->sample_rate;
8215 in->sample_rate = config->sample_rate;
8216 } else {
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008217 //XXX needed for voice_extn_compress_voip_open_input_stream
8218 in->config.rate = config->sample_rate;
8219 if ((in->dev->mode == AUDIO_MODE_IN_COMMUNICATION ||
8220 voice_extn_compress_voip_is_active(in->dev)) &&
8221 (voice_extn_compress_voip_is_format_supported(in->format)) &&
8222 valid_rate && valid_ch) {
8223 voice_extn_compress_voip_open_input_stream(in);
8224 // update rate entries to match config from AF
8225 in->config.rate = config->sample_rate;
8226 in->sample_rate = config->sample_rate;
8227 } else {
8228 ALOGW("%s compress voip not active, use defaults", __func__);
8229 }
Haynes Mathew George4ffef292017-11-21 15:08:02 -08008230 }
Haynes Mathew George4ffef292017-11-21 15:08:02 -08008231 return 0;
8232}
8233
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008234static int adev_open_input_stream(struct audio_hw_device *dev,
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07008235 audio_io_handle_t handle,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008236 audio_devices_t devices,
8237 struct audio_config *config,
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07008238 struct audio_stream_in **stream_in,
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05308239 audio_input_flags_t flags,
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07008240 const char *address __unused,
Vidyakumar Athota5c398212015-03-31 21:53:21 -07008241 audio_source_t source)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008242{
8243 struct audio_device *adev = (struct audio_device *)dev;
8244 struct stream_in *in;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08008245 int ret = 0, buffer_size, frame_size;
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07008246 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07008247 bool is_low_latency = false;
Divya Narayanan Poojary45f19192016-09-30 18:52:13 +05308248 bool channel_mask_updated = false;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008249 bool is_usb_dev = audio_is_usb_in_device(devices);
8250 bool may_use_hifi_record = adev_input_allow_hifi_record(adev,
8251 devices,
8252 flags,
8253 source);
Andy Hung94320602018-10-29 18:31:12 -07008254 ALOGV("%s: enter: flags %#x, is_usb_dev %d, may_use_hifi_record %d,"
8255 " sample_rate %u, channel_mask %#x, format %#x",
8256 __func__, flags, is_usb_dev, may_use_hifi_record,
8257 config->sample_rate, config->channel_mask, config->format);
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05308258
kunleizdff872d2018-08-20 14:40:33 +08008259 if (is_usb_dev && (!audio_extn_usb_connected(NULL))) {
kunleizd6a9e0c2018-07-30 15:38:52 +08008260 is_usb_dev = false;
8261 devices = AUDIO_DEVICE_IN_BUILTIN_MIC;
8262 ALOGW("%s: ignore set device to non existing USB card, use input device(%#x)",
8263 __func__, devices);
8264 }
8265
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008266 *stream_in = NULL;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008267
8268 if (!(is_usb_dev && may_use_hifi_record)) {
8269 if (config->sample_rate == 0)
8270 config->sample_rate = 48000;
8271 if (config->channel_mask == AUDIO_CHANNEL_NONE)
8272 config->channel_mask = AUDIO_CHANNEL_IN_MONO;
8273 if (config->format == AUDIO_FORMAT_DEFAULT)
8274 config->format = AUDIO_FORMAT_PCM_16_BIT;
8275
8276 channel_count = audio_channel_count_from_in_mask(config->channel_mask);
8277
Aalique Grahame22e49102018-12-18 14:23:57 -08008278 if (check_input_parameters(config->sample_rate, config->format, channel_count,
8279 false) != 0)
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008280 return -EINVAL;
Chaithanya Krishna Bacharaju9955b162016-05-25 16:25:53 +05308281 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008282
8283 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07008284
8285 if (!in) {
8286 ALOGE("failed to allocate input stream");
8287 return -ENOMEM;
8288 }
8289
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05308290 ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x)\
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05308291 stream_handle(%p) io_handle(%d) source(%d) format %x",__func__, config->sample_rate,
8292 config->channel_mask, devices, &in->stream, handle, source, config->format);
Ravi Kumar Alamanda40703102014-04-24 10:34:41 -07008293 pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07008294 pthread_mutex_init(&in->pre_lock, (const pthread_mutexattr_t *) NULL);
Ravi Kumar Alamanda40703102014-04-24 10:34:41 -07008295
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008296 in->stream.common.get_sample_rate = in_get_sample_rate;
8297 in->stream.common.set_sample_rate = in_set_sample_rate;
8298 in->stream.common.get_buffer_size = in_get_buffer_size;
8299 in->stream.common.get_channels = in_get_channels;
8300 in->stream.common.get_format = in_get_format;
8301 in->stream.common.set_format = in_set_format;
8302 in->stream.common.standby = in_standby;
8303 in->stream.common.dump = in_dump;
8304 in->stream.common.set_parameters = in_set_parameters;
8305 in->stream.common.get_parameters = in_get_parameters;
8306 in->stream.common.add_audio_effect = in_add_audio_effect;
8307 in->stream.common.remove_audio_effect = in_remove_audio_effect;
8308 in->stream.set_gain = in_set_gain;
8309 in->stream.read = in_read;
8310 in->stream.get_input_frames_lost = in_get_input_frames_lost;
Aalique Grahame22e49102018-12-18 14:23:57 -08008311 in->stream.get_capture_position = in_get_capture_position;
Naresh Tannirudcb47c52018-06-25 16:23:32 +05308312 in->stream.get_active_microphones = in_get_active_microphones;
Paul McLeana50b7332018-12-17 08:24:21 -07008313 in->stream.set_microphone_direction = in_set_microphone_direction;
8314 in->stream.set_microphone_field_dimension = in_set_microphone_field_dimension;
juyuchendb308c22019-01-21 11:57:17 -07008315 in->stream.update_sink_metadata = in_update_sink_metadata;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008316
8317 in->device = devices;
Vidyakumar Athota5c398212015-03-31 21:53:21 -07008318 in->source = source;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008319 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008320 in->standby = 1;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07008321 in->capture_handle = handle;
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07008322 in->flags = flags;
Haynes Mathew George46740472017-10-27 18:40:12 -07008323 in->bit_width = 16;
8324 in->af_period_multiplier = 1;
justinweng20fb6d82019-02-21 18:49:00 -07008325 in->direction = MIC_DIRECTION_UNSPECIFIED;
8326 in->zoom = 0;
Haynes Mathew George46740472017-10-27 18:40:12 -07008327
Andy Hung94320602018-10-29 18:31:12 -07008328 ALOGV("%s: source %d, config->channel_mask %#x", __func__, source, config->channel_mask);
Aalique Grahame22e49102018-12-18 14:23:57 -08008329 if (source == AUDIO_SOURCE_VOICE_UPLINK ||
8330 source == AUDIO_SOURCE_VOICE_DOWNLINK) {
8331 /* Force channel config requested to mono if incall
8332 record is being requested for only uplink/downlink */
8333 if (config->channel_mask != AUDIO_CHANNEL_IN_MONO) {
8334 config->channel_mask = AUDIO_CHANNEL_IN_MONO;
8335 ret = -EINVAL;
8336 goto err_open;
8337 }
8338 }
8339
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008340 if (is_usb_dev && may_use_hifi_record) {
8341 /* HiFi record selects an appropriate format, channel, rate combo
8342 depending on sink capabilities*/
8343 ret = read_usb_sup_params_and_compare(false /*is_playback*/,
8344 &config->format,
8345 &in->supported_formats[0],
8346 MAX_SUPPORTED_FORMATS,
8347 &config->channel_mask,
8348 &in->supported_channel_masks[0],
8349 MAX_SUPPORTED_CHANNEL_MASKS,
8350 &config->sample_rate,
8351 &in->supported_sample_rates[0],
8352 MAX_SUPPORTED_SAMPLE_RATES);
8353 if (ret != 0) {
8354 ret = -EINVAL;
8355 goto err_open;
8356 }
8357 channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Haynes Mathew George4ffef292017-11-21 15:08:02 -08008358 } else if (config->format == AUDIO_FORMAT_DEFAULT) {
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05308359 config->format = AUDIO_FORMAT_PCM_16_BIT;
Surendar karkaaca3d082017-11-09 15:18:37 +05308360 } else if (property_get_bool("vendor.audio.capture.pcm.32bit.enable", false)
8361 && config->format == AUDIO_FORMAT_PCM_32_BIT) {
8362 in->config.format = PCM_FORMAT_S32_LE;
8363 in->bit_width = 32;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05308364 } else if ((config->format == AUDIO_FORMAT_PCM_FLOAT) ||
8365 (config->format == AUDIO_FORMAT_PCM_32_BIT) ||
8366 (config->format == AUDIO_FORMAT_PCM_24_BIT_PACKED) ||
8367 (config->format == AUDIO_FORMAT_PCM_8_24_BIT)) {
8368 bool ret_error = false;
8369 in->bit_width = 24;
8370 /* 24 bit is restricted to UNPROCESSED source only,also format supported
8371 from HAL is 24_packed and 8_24
8372 *> In case of UNPROCESSED source, for 24 bit, if format requested is other than
8373 24_packed return error indicating supported format is 24_packed
8374 *> In case of any other source requesting 24 bit or float return error
8375 indicating format supported is 16 bit only.
8376
8377 on error flinger will retry with supported format passed
8378 */
8379 if ((source != AUDIO_SOURCE_UNPROCESSED) &&
8380 (source != AUDIO_SOURCE_CAMCORDER)) {
8381 config->format = AUDIO_FORMAT_PCM_16_BIT;
8382 if (config->sample_rate > 48000)
8383 config->sample_rate = 48000;
8384 ret_error = true;
Haynes Mathew George46740472017-10-27 18:40:12 -07008385 } else if (!(config->format == AUDIO_FORMAT_PCM_24_BIT_PACKED ||
8386 config->format == AUDIO_FORMAT_PCM_8_24_BIT)) {
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05308387 config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
8388 ret_error = true;
8389 }
8390
8391 if (ret_error) {
8392 ret = -EINVAL;
8393 goto err_open;
8394 }
8395 }
8396
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008397 in->channel_mask = config->channel_mask;
8398 in->format = config->format;
8399
8400 in->usecase = USECASE_AUDIO_RECORD;
Samyak Jain0aa07ab2019-04-04 14:36:32 +05308401
8402 if (in->source == AUDIO_SOURCE_FM_TUNER) {
8403 if(!get_usecase_from_list(adev, USECASE_AUDIO_RECORD_FM_VIRTUAL))
8404 in->usecase = USECASE_AUDIO_RECORD_FM_VIRTUAL;
8405 else {
8406 ret = -EINVAL;
8407 goto err_open;
8408 }
8409 }
8410
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008411 if (config->sample_rate == LOW_LATENCY_CAPTURE_SAMPLE_RATE &&
8412 (flags & AUDIO_INPUT_FLAG_FAST) != 0) {
8413 is_low_latency = true;
8414#if LOW_LATENCY_CAPTURE_USE_CASE
8415 in->usecase = USECASE_AUDIO_RECORD_LOW_LATENCY;
8416#endif
8417 in->realtime = may_use_noirq_mode(adev, in->usecase, in->flags);
Aalique Grahame22e49102018-12-18 14:23:57 -08008418 if (!in->realtime) {
8419 in->config = pcm_config_audio_capture;
8420 frame_size = audio_stream_in_frame_size(&in->stream);
8421 buffer_size = get_input_buffer_size(config->sample_rate,
8422 config->format,
8423 channel_count,
8424 is_low_latency);
8425 in->config.period_size = buffer_size / frame_size;
8426 in->config.rate = config->sample_rate;
8427 in->af_period_multiplier = 1;
8428 } else {
8429 // period size is left untouched for rt mode playback
8430 in->config = pcm_config_audio_capture_rt;
8431 in->af_period_multiplier = af_period_multiplier;
8432 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008433 }
8434
8435 if ((config->sample_rate == LOW_LATENCY_CAPTURE_SAMPLE_RATE) &&
8436 ((in->flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0)) {
8437 in->realtime = 0;
8438 in->usecase = USECASE_AUDIO_RECORD_MMAP;
8439 in->config = pcm_config_mmap_capture;
Haynes Mathew George46740472017-10-27 18:40:12 -07008440 in->config.format = pcm_format_from_audio_format(config->format);
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008441 in->stream.start = in_start;
8442 in->stream.stop = in_stop;
8443 in->stream.create_mmap_buffer = in_create_mmap_buffer;
8444 in->stream.get_mmap_position = in_get_mmap_position;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008445 ALOGV("%s: USECASE_AUDIO_RECORD_MMAP", __func__);
8446 } else if (in->realtime) {
8447 in->config = pcm_config_audio_capture_rt;
Haynes Mathew George46740472017-10-27 18:40:12 -07008448 in->config.format = pcm_format_from_audio_format(config->format);
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008449 in->af_period_multiplier = af_period_multiplier;
Haynes Mathew George46740472017-10-27 18:40:12 -07008450 } else if (is_usb_dev && may_use_hifi_record) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008451 in->usecase = USECASE_AUDIO_RECORD_HIFI;
8452 in->config = pcm_config_audio_capture;
8453 frame_size = audio_stream_in_frame_size(&in->stream);
8454 buffer_size = get_input_buffer_size(config->sample_rate,
8455 config->format,
8456 channel_count,
8457 false /*is_low_latency*/);
8458 in->config.period_size = buffer_size / frame_size;
8459 in->config.rate = config->sample_rate;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008460 in->config.format = pcm_format_from_audio_format(config->format);
Karthikeyan Mani07faa602018-08-20 11:01:32 -07008461 switch (config->format) {
8462 case AUDIO_FORMAT_PCM_32_BIT:
8463 in->bit_width = 32;
8464 break;
8465 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
8466 case AUDIO_FORMAT_PCM_8_24_BIT:
8467 in->bit_width = 24;
8468 break;
8469 default:
8470 in->bit_width = 16;
8471 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008472 } else if ((in->device == AUDIO_DEVICE_IN_TELEPHONY_RX) ||
Dhanalakshmi Siddani0b1488e2016-09-06 12:58:42 +05308473 (in->device == AUDIO_DEVICE_IN_PROXY)) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07008474 if (config->sample_rate == 0)
8475 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
8476 if (config->sample_rate != 48000 && config->sample_rate != 16000 &&
8477 config->sample_rate != 8000) {
8478 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
8479 ret = -EINVAL;
8480 goto err_open;
8481 }
8482 if (config->format == AUDIO_FORMAT_DEFAULT)
8483 config->format = AUDIO_FORMAT_PCM_16_BIT;
8484 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
8485 config->format = AUDIO_FORMAT_PCM_16_BIT;
8486 ret = -EINVAL;
8487 goto err_open;
8488 }
8489
8490 in->usecase = USECASE_AUDIO_RECORD_AFE_PROXY;
8491 in->config = pcm_config_afe_proxy_record;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07008492 in->config.rate = config->sample_rate;
Aalique Grahame22e49102018-12-18 14:23:57 -08008493 in->af_period_multiplier = 1;
8494 } else if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
8495 in->flags & AUDIO_INPUT_FLAG_VOIP_TX &&
8496 (config->sample_rate == 8000 ||
8497 config->sample_rate == 16000 ||
8498 config->sample_rate == 32000 ||
8499 config->sample_rate == 48000) &&
8500 channel_count == 1) {
8501 in->usecase = USECASE_AUDIO_RECORD_VOIP;
8502 in->config = pcm_config_audio_capture;
8503 frame_size = audio_stream_in_frame_size(&in->stream);
8504 buffer_size = get_stream_buffer_size(VOIP_CAPTURE_PERIOD_DURATION_MSEC,
8505 config->sample_rate,
8506 config->format,
8507 channel_count, false /*is_low_latency*/);
8508 in->config.period_size = buffer_size / frame_size;
8509 in->config.period_count = VOIP_CAPTURE_PERIOD_COUNT;
8510 in->config.rate = config->sample_rate;
8511 in->af_period_multiplier = 1;
Haynes Mathew George46740472017-10-27 18:40:12 -07008512 } else {
Revathi Uddarajud2634032017-12-07 14:42:34 +05308513 int ret_val;
8514 pthread_mutex_lock(&adev->lock);
8515 ret_val = audio_extn_check_and_set_multichannel_usecase(adev,
8516 in, config, &channel_mask_updated);
8517 pthread_mutex_unlock(&adev->lock);
8518
8519 if (!ret_val) {
8520 if (channel_mask_updated == true) {
8521 ALOGD("%s: return error to retry with updated channel mask (%#x)",
8522 __func__, config->channel_mask);
8523 ret = -EINVAL;
8524 goto err_open;
8525 }
8526 ALOGD("%s: created multi-channel session succesfully",__func__);
8527 } else if (audio_extn_compr_cap_enabled() &&
8528 audio_extn_compr_cap_format_supported(config->format) &&
8529 (in->dev->mode != AUDIO_MODE_IN_COMMUNICATION)) {
8530 audio_extn_compr_cap_init(in);
8531 } else if (audio_extn_cin_applicable_stream(in)) {
8532 ret = audio_extn_cin_configure_input_stream(in);
8533 if (ret)
8534 goto err_open;
8535 } else {
8536 in->config = pcm_config_audio_capture;
8537 in->config.rate = config->sample_rate;
8538 in->config.format = pcm_format_from_audio_format(config->format);
Revathi Uddarajud2634032017-12-07 14:42:34 +05308539 in->format = config->format;
8540 frame_size = audio_stream_in_frame_size(&in->stream);
8541 buffer_size = get_input_buffer_size(config->sample_rate,
Haynes Mathew George46740472017-10-27 18:40:12 -07008542 config->format,
8543 channel_count,
8544 is_low_latency);
Dieter Luecking5d57def2018-09-07 14:23:37 +02008545 /* prevent division-by-zero */
8546 if (frame_size == 0) {
8547 ALOGE("%s: Error frame_size==0", __func__);
8548 ret = -EINVAL;
8549 goto err_open;
8550 }
8551
Revathi Uddarajud2634032017-12-07 14:42:34 +05308552 in->config.period_size = buffer_size / frame_size;
Aalique Grahame22e49102018-12-18 14:23:57 -08008553 in->af_period_multiplier = 1;
Haynes Mathew George4ffef292017-11-21 15:08:02 -08008554
Revathi Uddarajud2634032017-12-07 14:42:34 +05308555 if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
8556 /* optionally use VOIP usecase depending on config(s) */
8557 ret = adev_update_voice_comm_input_stream(in, config);
8558 }
Haynes Mathew George4ffef292017-11-21 15:08:02 -08008559
Revathi Uddarajud2634032017-12-07 14:42:34 +05308560 if (ret) {
8561 ALOGE("%s AUDIO_SOURCE_VOICE_COMMUNICATION invalid args", __func__);
8562 goto err_open;
8563 }
Haynes Mathew George4ffef292017-11-21 15:08:02 -08008564 }
Samyak Jainc37062f2019-04-25 18:41:06 +05308565 if (audio_extn_is_concurrent_capture_enabled()) {
8566 /* Acquire lock to avoid two concurrent use cases initialized to
8567 same pcm record use case */
kunleiz28c73e72019-03-27 17:24:04 +08008568
Samyak Jainc37062f2019-04-25 18:41:06 +05308569 if (in->usecase == USECASE_AUDIO_RECORD) {
8570 pthread_mutex_lock(&adev->lock);
8571 if (!(adev->pcm_record_uc_state)) {
8572 ALOGV("%s: using USECASE_AUDIO_RECORD",__func__);
8573 adev->pcm_record_uc_state = 1;
8574 pthread_mutex_unlock(&adev->lock);
8575 } else {
8576 pthread_mutex_unlock(&adev->lock);
8577 /* Assign compress record use case for second record */
8578 in->usecase = USECASE_AUDIO_RECORD_COMPRESS2;
8579 in->flags |= AUDIO_INPUT_FLAG_COMPRESS;
8580 ALOGV("%s: overriding usecase with USECASE_AUDIO_RECORD_COMPRESS2 and appending compress flag", __func__);
8581 if (audio_extn_cin_applicable_stream(in)) {
8582 in->sample_rate = config->sample_rate;
8583 ret = audio_extn_cin_configure_input_stream(in);
8584 if (ret)
8585 goto err_open;
8586 }
8587 }
8588 }
kunleiz28c73e72019-03-27 17:24:04 +08008589 }
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07008590 }
Aniket Kumar Lata23d8cbf2019-04-10 19:54:46 -07008591
8592 in->config.channels = channel_count;
8593 in->sample_rate = in->config.rate;
8594
Dhananjay Kumard6d32152016-10-13 16:11:03 +05308595 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
8596 &adev->streams_input_cfg_list,
Haynes Mathew George4ffef292017-11-21 15:08:02 -08008597 devices, flags, in->format,
8598 in->sample_rate, in->bit_width,
8599 in->profile, &in->app_type_cfg);
Naresh Tanniru04f71882018-06-26 17:46:22 +05308600 register_format(in->format, in->supported_formats);
8601 register_channel_mask(in->channel_mask, in->supported_channel_masks);
8602 register_sample_rate(in->sample_rate, in->supported_sample_rates);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05308603
Aalique Grahame22e49102018-12-18 14:23:57 -08008604 in->error_log = error_log_create(
8605 ERROR_LOG_ENTRIES,
8606 1000000000 /* aggregate consecutive identical errors within one second */);
8607
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07008608 /* This stream could be for sound trigger lab,
8609 get sound trigger pcm if present */
8610 audio_extn_sound_trigger_check_and_get_session(in);
8611
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05308612 lock_input_stream(in);
8613 audio_extn_snd_mon_register_listener(in, in_snd_mon_cb);
8614 pthread_mutex_lock(&adev->lock);
8615 in->card_status = adev->card_status;
8616 pthread_mutex_unlock(&adev->lock);
8617 pthread_mutex_unlock(&in->lock);
8618
Aalique Grahame22e49102018-12-18 14:23:57 -08008619 stream_app_type_cfg_init(&in->app_type_cfg);
8620
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008621 *stream_in = &in->stream;
Derek Chenf939fb72018-11-13 13:34:41 -08008622
8623 streams_input_ctxt_t *in_ctxt = (streams_input_ctxt_t *)
8624 calloc(1, sizeof(streams_input_ctxt_t));
8625 if (in_ctxt == NULL) {
8626 ALOGE("%s fail to allocate input ctxt", __func__);
8627 ret = -ENOMEM;
8628 goto err_open;
8629 }
8630 in_ctxt->input = in;
8631
8632 pthread_mutex_lock(&adev->lock);
8633 list_add_tail(&adev->active_inputs_list, &in_ctxt->list);
8634 pthread_mutex_unlock(&adev->lock);
8635
Eric Laurent994a6932013-07-17 11:51:42 -07008636 ALOGV("%s: exit", __func__);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08008637 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008638
8639err_open:
Samyak Jainc37062f2019-04-25 18:41:06 +05308640 if (in->usecase == USECASE_AUDIO_RECORD) {
8641 pthread_mutex_lock(&adev->lock);
8642 adev->pcm_record_uc_state = 0;
8643 pthread_mutex_unlock(&adev->lock);
8644 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008645 free(in);
8646 *stream_in = NULL;
8647 return ret;
8648}
8649
8650static void adev_close_input_stream(struct audio_hw_device *dev,
8651 struct audio_stream_in *stream)
8652{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08008653 int ret;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07008654 struct stream_in *in = (struct stream_in *)stream;
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07008655 struct audio_device *adev = (struct audio_device *)dev;
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05308656
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05308657 ALOGD("%s: enter:stream_handle(%p)",__func__, in);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08008658
kunleiz70e57612018-12-28 17:50:23 +08008659 /* must deregister from sndmonitor first to prevent races
8660 * between the callback and close_stream
8661 */
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05308662 audio_extn_snd_mon_unregister_listener(stream);
8663
kunleiz70e57612018-12-28 17:50:23 +08008664 /* Disable echo reference if there are no active input, hfp call
8665 * and sound trigger while closing input stream
8666 */
8667 if (!adev->active_input &&
8668 !audio_extn_hfp_is_active(adev) &&
8669 !audio_extn_sound_trigger_check_ec_ref_enable())
kunleiz8c93ce62018-11-29 15:04:56 +08008670 platform_set_echo_reference(adev, false, AUDIO_DEVICE_NONE);
kunleiz70e57612018-12-28 17:50:23 +08008671 else
8672 audio_extn_sound_trigger_update_ec_ref_status(false);
Venkata Narendra Kumar Gutta1bbbf542014-09-04 19:11:25 +05308673
Pallavid7c7a272018-01-16 11:22:55 +05308674 if (in == NULL) {
8675 ALOGE("%s: audio_stream_in ptr is NULL", __func__);
8676 return;
8677 }
Weiyin Jiang2995f662019-04-17 14:25:12 +08008678 error_log_destroy(in->error_log);
8679 in->error_log = NULL;
8680
Pallavid7c7a272018-01-16 11:22:55 +05308681
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08008682 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05308683 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08008684 ret = voice_extn_compress_voip_close_input_stream(&stream->common);
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05308685 pthread_mutex_unlock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08008686 if (ret != 0)
8687 ALOGE("%s: Compress voip input cannot be closed, error:%d",
8688 __func__, ret);
8689 } else
8690 in_standby(&stream->common);
8691
Revathi Uddarajud2634032017-12-07 14:42:34 +05308692 pthread_mutex_lock(&adev->lock);
Samyak Jain15fda662018-12-18 16:40:52 +05308693 if (in->usecase == USECASE_AUDIO_RECORD) {
8694 adev->pcm_record_uc_state = 0;
8695 }
8696
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -07008697 if (audio_extn_ssr_get_stream() == in) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07008698 audio_extn_ssr_deinit();
8699 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008700
Garmond Leunge2433c32017-09-28 21:51:22 -07008701 if (audio_extn_ffv_get_stream() == in) {
8702 audio_extn_ffv_stream_deinit();
8703 }
8704
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05308705 if (audio_extn_compr_cap_enabled() &&
Mingming Yine62d7842013-10-25 16:26:03 -07008706 audio_extn_compr_cap_format_supported(in->config.format))
8707 audio_extn_compr_cap_deinit();
Dhanalakshmi Siddani74cf00b2016-12-02 13:55:57 +05308708
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05308709 if (audio_extn_cin_attached_usecase(in->usecase))
8710 audio_extn_cin_close_input_stream(in);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07008711
Mingming Yinfd7607b2016-01-22 12:48:44 -08008712 if (in->is_st_session) {
8713 ALOGV("%s: sound trigger pcm stop lab", __func__);
8714 audio_extn_sound_trigger_stop_lab(in);
8715 }
Derek Chenf939fb72018-11-13 13:34:41 -08008716 streams_input_ctxt_t *in_ctxt = in_get_stream(adev, in->capture_handle);
8717 if (in_ctxt != NULL) {
8718 list_remove(&in_ctxt->list);
8719 free(in_ctxt);
8720 } else {
8721 ALOGW("%s, input stream already closed", __func__);
8722 }
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07008723 free(stream);
Revathi Uddarajud2634032017-12-07 14:42:34 +05308724 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008725 return;
8726}
8727
Aalique Grahame22e49102018-12-18 14:23:57 -08008728/* verifies input and output devices and their capabilities.
8729 *
8730 * This verification is required when enabling extended bit-depth or
8731 * sampling rates, as not all qcom products support it.
8732 *
8733 * Suitable for calling only on initialization such as adev_open().
8734 * It fills the audio_device use_case_table[] array.
8735 *
8736 * Has a side-effect that it needs to configure audio routing / devices
8737 * in order to power up the devices and read the device parameters.
8738 * It does not acquire any hw device lock. Should restore the devices
8739 * back to "normal state" upon completion.
8740 */
8741static int adev_verify_devices(struct audio_device *adev)
8742{
8743 /* enumeration is a bit difficult because one really wants to pull
8744 * the use_case, device id, etc from the hidden pcm_device_table[].
8745 * In this case there are the following use cases and device ids.
8746 *
8747 * [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = {0, 0},
8748 * [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {15, 15},
8749 * [USECASE_AUDIO_PLAYBACK_HIFI] = {1, 1},
8750 * [USECASE_AUDIO_PLAYBACK_OFFLOAD] = {9, 9},
8751 * [USECASE_AUDIO_RECORD] = {0, 0},
8752 * [USECASE_AUDIO_RECORD_LOW_LATENCY] = {15, 15},
8753 * [USECASE_VOICE_CALL] = {2, 2},
8754 *
8755 * USECASE_AUDIO_PLAYBACK_OFFLOAD, USECASE_AUDIO_PLAYBACK_HIFI omitted.
8756 * USECASE_VOICE_CALL omitted, but possible for either input or output.
8757 */
8758
8759 /* should be the usecases enabled in adev_open_input_stream() */
8760 static const int test_in_usecases[] = {
8761 USECASE_AUDIO_RECORD,
8762 USECASE_AUDIO_RECORD_LOW_LATENCY, /* does not appear to be used */
8763 };
8764 /* should be the usecases enabled in adev_open_output_stream()*/
8765 static const int test_out_usecases[] = {
8766 USECASE_AUDIO_PLAYBACK_DEEP_BUFFER,
8767 USECASE_AUDIO_PLAYBACK_LOW_LATENCY,
8768 };
8769 static const usecase_type_t usecase_type_by_dir[] = {
8770 PCM_PLAYBACK,
8771 PCM_CAPTURE,
8772 };
8773 static const unsigned flags_by_dir[] = {
8774 PCM_OUT,
8775 PCM_IN,
8776 };
8777
8778 size_t i;
8779 unsigned dir;
8780 const unsigned card_id = adev->snd_card;
8781
8782 for (dir = 0; dir < 2; ++dir) {
8783 const usecase_type_t usecase_type = usecase_type_by_dir[dir];
8784 const unsigned flags_dir = flags_by_dir[dir];
8785 const size_t testsize =
8786 dir ? ARRAY_SIZE(test_in_usecases) : ARRAY_SIZE(test_out_usecases);
8787 const int *testcases =
8788 dir ? test_in_usecases : test_out_usecases;
8789 const audio_devices_t audio_device =
8790 dir ? AUDIO_DEVICE_IN_BUILTIN_MIC : AUDIO_DEVICE_OUT_SPEAKER;
8791
8792 for (i = 0; i < testsize; ++i) {
8793 const audio_usecase_t audio_usecase = testcases[i];
8794 int device_id;
8795 struct pcm_params **pparams;
8796 struct stream_out out;
8797 struct stream_in in;
8798 struct audio_usecase uc_info;
8799 int retval;
8800
8801 pparams = &adev->use_case_table[audio_usecase];
8802 pcm_params_free(*pparams); /* can accept null input */
8803 *pparams = NULL;
8804
8805 /* find the device ID for the use case (signed, for error) */
8806 device_id = platform_get_pcm_device_id(audio_usecase, usecase_type);
8807 if (device_id < 0)
8808 continue;
8809
8810 /* prepare structures for device probing */
8811 memset(&uc_info, 0, sizeof(uc_info));
8812 uc_info.id = audio_usecase;
8813 uc_info.type = usecase_type;
8814 if (dir) {
8815 adev->active_input = &in;
8816 memset(&in, 0, sizeof(in));
8817 in.device = audio_device;
8818 in.source = AUDIO_SOURCE_VOICE_COMMUNICATION;
8819 uc_info.stream.in = &in;
8820 } else {
8821 adev->active_input = NULL;
8822 }
8823 memset(&out, 0, sizeof(out));
8824 out.devices = audio_device; /* only field needed in select_devices */
8825 uc_info.stream.out = &out;
8826 uc_info.devices = audio_device;
8827 uc_info.in_snd_device = SND_DEVICE_NONE;
8828 uc_info.out_snd_device = SND_DEVICE_NONE;
8829 list_add_tail(&adev->usecase_list, &uc_info.list);
8830
8831 /* select device - similar to start_(in/out)put_stream() */
8832 retval = select_devices(adev, audio_usecase);
8833 if (retval >= 0) {
8834 *pparams = pcm_params_get(card_id, device_id, flags_dir);
8835#if LOG_NDEBUG == 0
Aalique Grahame203bee02019-03-13 17:49:36 -07008836 char info[512]; /* for possible debug info */
Aalique Grahame22e49102018-12-18 14:23:57 -08008837 if (*pparams) {
8838 ALOGV("%s: (%s) card %d device %d", __func__,
8839 dir ? "input" : "output", card_id, device_id);
8840 pcm_params_to_string(*pparams, info, ARRAY_SIZE(info));
8841 } else {
8842 ALOGV("%s: cannot locate card %d device %d", __func__, card_id, device_id);
8843 }
8844#endif
8845 }
8846
8847 /* deselect device - similar to stop_(in/out)put_stream() */
8848 /* 1. Get and set stream specific mixer controls */
8849 retval = disable_audio_route(adev, &uc_info);
8850 /* 2. Disable the rx device */
8851 retval = disable_snd_device(adev,
8852 dir ? uc_info.in_snd_device : uc_info.out_snd_device);
8853 list_remove(&uc_info.list);
8854 }
8855 }
8856 adev->active_input = NULL; /* restore adev state */
8857 return 0;
8858}
8859
Siddartha Shaik31b530e2017-05-19 15:26:33 +05308860int adev_create_audio_patch(struct audio_hw_device *dev,
8861 unsigned int num_sources,
8862 const struct audio_port_config *sources,
8863 unsigned int num_sinks,
8864 const struct audio_port_config *sinks,
8865 audio_patch_handle_t *handle)
8866{
Derek Chenf939fb72018-11-13 13:34:41 -08008867 int ret;
Siddartha Shaik31b530e2017-05-19 15:26:33 +05308868
Derek Chenf939fb72018-11-13 13:34:41 -08008869 ret = audio_extn_hw_loopback_create_audio_patch(dev,
8870 num_sources,
8871 sources,
8872 num_sinks,
8873 sinks,
8874 handle);
8875 ret |= audio_extn_auto_hal_create_audio_patch(dev,
8876 num_sources,
8877 sources,
8878 num_sinks,
8879 sinks,
8880 handle);
8881 return ret;
Siddartha Shaik31b530e2017-05-19 15:26:33 +05308882}
8883
8884int adev_release_audio_patch(struct audio_hw_device *dev,
8885 audio_patch_handle_t handle)
8886{
Derek Chenf939fb72018-11-13 13:34:41 -08008887 int ret;
8888
8889 ret = audio_extn_hw_loopback_release_audio_patch(dev, handle);
8890 ret |= audio_extn_auto_hal_release_audio_patch(dev, handle);
8891 return ret;
Siddartha Shaik31b530e2017-05-19 15:26:33 +05308892}
8893
8894int adev_get_audio_port(struct audio_hw_device *dev, struct audio_port *config)
8895{
8896 return audio_extn_hw_loopback_get_audio_port(dev, config);
8897}
8898
8899int adev_set_audio_port_config(struct audio_hw_device *dev,
8900 const struct audio_port_config *config)
8901{
8902 return audio_extn_hw_loopback_set_audio_port_config(dev, config);
8903}
8904
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07008905static int adev_dump(const audio_hw_device_t *device __unused,
8906 int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008907{
8908 return 0;
8909}
8910
8911static int adev_close(hw_device_t *device)
8912{
Aalique Grahame22e49102018-12-18 14:23:57 -08008913 size_t i;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008914 struct audio_device *adev = (struct audio_device *)device;
Kiran Kandi910e1862013-10-29 13:29:42 -07008915
8916 if (!adev)
8917 return 0;
8918
8919 pthread_mutex_lock(&adev_init_lock);
8920
8921 if ((--audio_device_ref_count) == 0) {
Sujin Panicker390724d2019-04-26 10:43:36 +05308922 if (audio_extn_spkr_prot_is_enabled())
8923 audio_extn_spkr_prot_deinit();
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05308924 audio_extn_snd_mon_unregister_listener(adev);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07008925 audio_extn_sound_trigger_deinit(adev);
Kiran Kandide144c82013-11-20 15:58:32 -08008926 audio_extn_listen_deinit(adev);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008927 audio_extn_qdsp_deinit();
Aalique Grahame22e49102018-12-18 14:23:57 -08008928 audio_extn_extspk_deinit(adev->extspk);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05308929 audio_extn_utils_release_streams_cfg_lists(
8930 &adev->streams_output_cfg_list,
8931 &adev->streams_input_cfg_list);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05308932 if (audio_extn_qaf_is_enabled())
8933 audio_extn_qaf_deinit();
Kiran Kandi910e1862013-10-29 13:29:42 -07008934 audio_route_free(adev->audio_route);
Weiyin Jiangfa65d3e2019-03-05 23:39:45 +08008935 audio_extn_gef_deinit(adev);
Kiran Kandi910e1862013-10-29 13:29:42 -07008936 free(adev->snd_dev_ref_cnt);
8937 platform_deinit(adev->platform);
Aalique Grahame22e49102018-12-18 14:23:57 -08008938 for (i = 0; i < ARRAY_SIZE(adev->use_case_table); ++i) {
8939 pcm_params_free(adev->use_case_table[i]);
8940 }
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07008941 if (adev->adm_deinit)
8942 adev->adm_deinit(adev->adm_data);
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05308943 qahwi_deinit(device);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08008944 audio_extn_adsp_hdlr_deinit();
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05308945 audio_extn_snd_mon_deinit();
Siddartha Shaik44dd7702017-06-14 12:13:25 +05308946 audio_extn_hw_loopback_deinit(adev);
Garmond Leunge2433c32017-09-28 21:51:22 -07008947 audio_extn_ffv_deinit();
Satish Babu Patakokilac3c5d432017-07-04 22:48:59 +05308948 if (adev->device_cfg_params) {
8949 free(adev->device_cfg_params);
8950 adev->device_cfg_params = NULL;
8951 }
Derek Chend2530072014-11-24 12:39:14 -08008952 if(adev->ext_hw_plugin)
8953 audio_extn_ext_hw_plugin_deinit(adev->ext_hw_plugin);
Derek Chenae7b0342019-02-08 15:17:04 -08008954 audio_extn_auto_hal_deinit();
Kiran Kandi910e1862013-10-29 13:29:42 -07008955 free(device);
8956 adev = NULL;
8957 }
8958 pthread_mutex_unlock(&adev_init_lock);
Vatsal Buchac09ae062018-11-14 13:25:08 +05308959 enable_gcov();
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008960 return 0;
8961}
8962
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07008963/* This returns 1 if the input parameter looks at all plausible as a low latency period size,
8964 * or 0 otherwise. A return value of 1 doesn't mean the value is guaranteed to work,
8965 * just that it _might_ work.
8966 */
8967static int period_size_is_plausible_for_low_latency(int period_size)
8968{
8969 switch (period_size) {
8970 case 160:
Ravi Kumar Alamanda474de5a2015-06-25 20:08:01 -07008971 case 192:
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07008972 case 240:
8973 case 320:
8974 case 480:
8975 return 1;
8976 default:
8977 return 0;
8978 }
8979}
8980
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05308981static void adev_snd_mon_cb(void *cookie, struct str_parms *parms)
8982{
8983 bool is_snd_card_status = false;
8984 bool is_ext_device_status = false;
8985 char value[32];
8986 int card = -1;
8987 card_status_t status;
8988
8989 if (cookie != adev || !parms)
8990 return;
8991
8992 if (!parse_snd_card_status(parms, &card, &status)) {
8993 is_snd_card_status = true;
8994 } else if (0 < str_parms_get_str(parms, "ext_audio_device", value, sizeof(value))) {
8995 is_ext_device_status = true;
8996 } else {
8997 // not a valid event
8998 return;
8999 }
9000
9001 pthread_mutex_lock(&adev->lock);
9002 if (card == adev->snd_card || is_ext_device_status) {
9003 if (is_snd_card_status && adev->card_status != status) {
9004 adev->card_status = status;
9005 platform_snd_card_update(adev->platform, status);
9006 audio_extn_fm_set_parameters(adev, parms);
9007 } else if (is_ext_device_status) {
9008 platform_set_parameters(adev->platform, parms);
9009 }
9010 }
9011 pthread_mutex_unlock(&adev->lock);
9012 return;
9013}
9014
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05309015/* out and adev lock held */
9016static int check_a2dp_restore_l(struct audio_device *adev, struct stream_out *out, bool restore)
9017{
9018 struct audio_usecase *uc_info;
9019 float left_p;
9020 float right_p;
9021 audio_devices_t devices;
9022
9023 uc_info = get_usecase_from_list(adev, out->usecase);
9024 if (uc_info == NULL) {
9025 ALOGE("%s: Could not find the usecase (%d) in the list",
9026 __func__, out->usecase);
9027 return -EINVAL;
9028 }
9029
9030 ALOGD("%s: enter: usecase(%d: %s)", __func__,
9031 out->usecase, use_case_table[out->usecase]);
9032
9033 if (restore) {
9034 // restore A2DP device for active usecases and unmute if required
9035 if ((out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) &&
9036 (uc_info->out_snd_device != SND_DEVICE_OUT_BT_A2DP)) {
9037 ALOGD("%s: restoring A2dp and unmuting stream", __func__);
9038 select_devices(adev, uc_info->id);
9039 pthread_mutex_lock(&out->compr_mute_lock);
9040 if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
9041 (out->a2dp_compress_mute)) {
9042 out->a2dp_compress_mute = false;
9043 out_set_compr_volume(&out->stream, out->volume_l, out->volume_r);
9044 }
9045 pthread_mutex_unlock(&out->compr_mute_lock);
9046 }
9047 } else {
9048 // mute compress stream if suspended
9049 pthread_mutex_lock(&out->compr_mute_lock);
9050 if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
9051 (!out->a2dp_compress_mute)) {
9052 if (!out->standby) {
9053 ALOGD("%s: selecting speaker and muting stream", __func__);
9054 devices = out->devices;
9055 out->devices = AUDIO_DEVICE_OUT_SPEAKER;
9056 left_p = out->volume_l;
9057 right_p = out->volume_r;
9058 if (out->offload_state == OFFLOAD_STATE_PLAYING)
9059 compress_pause(out->compr);
9060 out_set_compr_volume(&out->stream, (float)0, (float)0);
9061 out->a2dp_compress_mute = true;
9062 select_devices(adev, out->usecase);
9063 if (out->offload_state == OFFLOAD_STATE_PLAYING)
9064 compress_resume(out->compr);
9065 out->devices = devices;
9066 out->volume_l = left_p;
9067 out->volume_r = right_p;
9068 }
9069 }
9070 pthread_mutex_unlock(&out->compr_mute_lock);
9071 }
9072 ALOGV("%s: exit", __func__);
9073 return 0;
9074}
9075
9076int check_a2dp_restore(struct audio_device *adev, struct stream_out *out, bool restore)
9077{
9078 int ret = 0;
9079
9080 lock_output_stream(out);
9081 pthread_mutex_lock(&adev->lock);
9082
9083 ret = check_a2dp_restore_l(adev, out, restore);
9084
9085 pthread_mutex_unlock(&adev->lock);
9086 pthread_mutex_unlock(&out->lock);
9087 return ret;
9088}
9089
Haynes Mathew George01156f92018-04-13 15:29:54 -07009090void adev_on_battery_status_changed(bool charging)
9091{
9092 pthread_mutex_lock(&adev->lock);
9093 ALOGI("%s: battery status changed to %scharging", __func__, charging ? "" : "not ");
9094 adev->is_charging = charging;
Zhou Songc1088ea2018-06-12 00:17:29 +08009095 audio_extn_sound_trigger_update_battery_status(charging);
Haynes Mathew George01156f92018-04-13 15:29:54 -07009096 pthread_mutex_unlock(&adev->lock);
9097}
9098
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009099static int adev_open(const hw_module_t *module, const char *name,
9100 hw_device_t **device)
9101{
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05309102 int ret;
Derek Chenf939fb72018-11-13 13:34:41 -08009103 char value[PROPERTY_VALUE_MAX] = {0};
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +05309104 char mixer_ctl_name[128] = {0};
9105 struct mixer_ctl *ctl = NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05309106
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08009107 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009108 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
9109
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07009110 pthread_mutex_lock(&adev_init_lock);
Kiran Kandi910e1862013-10-29 13:29:42 -07009111 if (audio_device_ref_count != 0){
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07009112 *device = &adev->device.common;
Kiran Kandi910e1862013-10-29 13:29:42 -07009113 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07009114 ALOGD("%s: returning existing instance of adev", __func__);
9115 ALOGD("%s: exit", __func__);
9116 pthread_mutex_unlock(&adev_init_lock);
9117 return 0;
9118 }
9119
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009120 adev = calloc(1, sizeof(struct audio_device));
9121
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07009122 if (!adev) {
9123 pthread_mutex_unlock(&adev_init_lock);
9124 return -ENOMEM;
9125 }
9126
Ravi Kumar Alamanda40703102014-04-24 10:34:41 -07009127 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
9128
Weiyin Jiange6ce6312019-01-28 18:28:22 +08009129 // register audio ext hidl at the earliest
9130 audio_extn_hidl_init();
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +05309131#ifdef DYNAMIC_LOG_ENABLED
9132 register_for_dynamic_logging("hal");
9133#endif
9134
Derek Chenf939fb72018-11-13 13:34:41 -08009135 /* default audio HAL major version */
9136 uint32_t maj_version = 2;
9137 if(property_get("vendor.audio.hal.maj.version", value, NULL))
9138 maj_version = atoi(value);
9139
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009140 adev->device.common.tag = HARDWARE_DEVICE_TAG;
Derek Chenf939fb72018-11-13 13:34:41 -08009141 adev->device.common.version = HARDWARE_DEVICE_API_VERSION(maj_version, 0);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009142 adev->device.common.module = (struct hw_module_t *)module;
9143 adev->device.common.close = adev_close;
9144
9145 adev->device.init_check = adev_init_check;
9146 adev->device.set_voice_volume = adev_set_voice_volume;
9147 adev->device.set_master_volume = adev_set_master_volume;
9148 adev->device.get_master_volume = adev_get_master_volume;
9149 adev->device.set_master_mute = adev_set_master_mute;
9150 adev->device.get_master_mute = adev_get_master_mute;
9151 adev->device.set_mode = adev_set_mode;
9152 adev->device.set_mic_mute = adev_set_mic_mute;
9153 adev->device.get_mic_mute = adev_get_mic_mute;
9154 adev->device.set_parameters = adev_set_parameters;
9155 adev->device.get_parameters = adev_get_parameters;
9156 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
9157 adev->device.open_output_stream = adev_open_output_stream;
9158 adev->device.close_output_stream = adev_close_output_stream;
9159 adev->device.open_input_stream = adev_open_input_stream;
9160 adev->device.close_input_stream = adev_close_input_stream;
Siddartha Shaik31b530e2017-05-19 15:26:33 +05309161 adev->device.create_audio_patch = adev_create_audio_patch;
9162 adev->device.release_audio_patch = adev_release_audio_patch;
9163 adev->device.get_audio_port = adev_get_audio_port;
9164 adev->device.set_audio_port_config = adev_set_audio_port_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009165 adev->device.dump = adev_dump;
Naresh Tannirudcb47c52018-06-25 16:23:32 +05309166 adev->device.get_microphones = adev_get_microphones;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009167
9168 /* Set the default route before the PCM stream is opened */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009169 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08009170 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08009171 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009172 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009173 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08009174 adev->acdb_settings = TTY_MODE_OFF;
vivek mehta344576a2016-04-12 18:56:03 -07009175 adev->allow_afe_proxy_usage = true;
Ashish Jain1b9b30c2017-05-18 20:57:40 +05309176 adev->bt_sco_on = false;
Eric Laurent07eeafd2013-10-06 12:52:49 -07009177 /* adev->cur_hdmi_channels = 0; by calloc() */
Eric Laurentb23d5282013-05-14 15:27:20 -07009178 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
vivek mehtaae1018c2019-05-09 12:19:57 -07009179 /* Init audio and voice feature */
9180 audio_extn_feature_init();
9181 voice_extn_feature_init();
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07009182 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08009183 list_init(&adev->usecase_list);
Derek Chenf939fb72018-11-13 13:34:41 -08009184 list_init(&adev->active_inputs_list);
9185 list_init(&adev->active_outputs_list);
Krishnankutty Kolathappilly0b2de1c2014-02-14 14:45:49 -08009186 adev->cur_wfd_channels = 2;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07009187 adev->offload_usecases_state = 0;
Samyak Jain15fda662018-12-18 16:40:52 +05309188 adev->pcm_record_uc_state = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05309189 adev->is_channel_status_set = false;
Sudheer Papothifa9d2282015-09-17 01:53:25 +05309190 adev->perf_lock_opts[0] = 0x101;
9191 adev->perf_lock_opts[1] = 0x20E;
9192 adev->perf_lock_opts_size = 2;
Xiaojun Sang785b5da2017-08-03 15:52:29 +08009193 adev->dsp_bit_width_enforce_mode = 0;
Aalique Grahame552b0832019-03-11 10:16:38 -07009194 adev->enable_hfp = false;
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +05309195 adev->use_old_pspd_mix_ctrl = false;
Aniket Kumar Lata60586a92019-05-22 22:18:55 -07009196 adev->adm_routing_changed = false;
Naresh Tanniru4c630392014-05-12 01:05:52 +05309197
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009198 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07009199 adev->platform = platform_init(adev);
9200 if (!adev->platform) {
Ben Romberger2684ad72018-06-04 12:41:39 -07009201 pthread_mutex_destroy(&adev->lock);
Eric Laurentb23d5282013-05-14 15:27:20 -07009202 free(adev->snd_dev_ref_cnt);
9203 free(adev);
Ben Romberger2684ad72018-06-04 12:41:39 -07009204 adev = NULL;
Eric Laurentb23d5282013-05-14 15:27:20 -07009205 ALOGE("%s: Failed to init platform data, aborting.", __func__);
9206 *device = NULL;
Apoorv Raghuvanshi6e57d7e2013-12-16 16:02:45 -08009207 pthread_mutex_unlock(&adev_init_lock);
Eric Laurentb23d5282013-05-14 15:27:20 -07009208 return -EINVAL;
9209 }
Eric Laurentc4aef752013-09-12 17:45:53 -07009210
Aalique Grahame22e49102018-12-18 14:23:57 -08009211 adev->extspk = audio_extn_extspk_init(adev);
9212
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05309213 if (audio_extn_qaf_is_enabled()) {
9214 ret = audio_extn_qaf_init(adev);
9215 if (ret < 0) {
Ben Romberger2684ad72018-06-04 12:41:39 -07009216 pthread_mutex_destroy(&adev->lock);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05309217 free(adev);
Ben Romberger2684ad72018-06-04 12:41:39 -07009218 adev = NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05309219 ALOGE("%s: Failed to init platform data, aborting.", __func__);
9220 *device = NULL;
9221 pthread_mutex_unlock(&adev_init_lock);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05309222 return ret;
9223 }
9224
9225 adev->device.open_output_stream = audio_extn_qaf_open_output_stream;
9226 adev->device.close_output_stream = audio_extn_qaf_close_output_stream;
9227 }
9228
Derek Chenae7b0342019-02-08 15:17:04 -08009229 audio_extn_auto_hal_init(adev);
Derek Chend2530072014-11-24 12:39:14 -08009230 adev->ext_hw_plugin = audio_extn_ext_hw_plugin_init(adev);
9231
Eric Laurentc4aef752013-09-12 17:45:53 -07009232 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
9233 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
9234 if (adev->visualizer_lib == NULL) {
9235 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
9236 } else {
9237 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
9238 adev->visualizer_start_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08009239 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07009240 "visualizer_hal_start_output");
9241 adev->visualizer_stop_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08009242 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07009243 "visualizer_hal_stop_output");
9244 }
9245 }
Dhanalakshmi Siddani21be3ac2016-12-29 14:31:08 +05309246 audio_extn_init(adev);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08009247 voice_extn_init(adev);
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08009248 audio_extn_listen_init(adev, adev->snd_card);
Weiyin Jiangaa80acd2016-09-21 16:42:11 +08009249 audio_extn_gef_init(adev);
Siddartha Shaik44dd7702017-06-14 12:13:25 +05309250 audio_extn_hw_loopback_init(adev);
Garmond Leunge2433c32017-09-28 21:51:22 -07009251 audio_extn_ffv_init(adev);
Eric Laurentc4aef752013-09-12 17:45:53 -07009252
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08009253 if (access(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, R_OK) == 0) {
9254 adev->offload_effects_lib = dlopen(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, RTLD_NOW);
9255 if (adev->offload_effects_lib == NULL) {
9256 ALOGE("%s: DLOPEN failed for %s", __func__,
9257 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
9258 } else {
9259 ALOGV("%s: DLOPEN successful for %s", __func__,
9260 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
9261 adev->offload_effects_start_output =
Ashish Jain5106d362016-05-11 19:23:33 +05309262 (int (*)(audio_io_handle_t, int, struct mixer *))dlsym(adev->offload_effects_lib,
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08009263 "offload_effects_bundle_hal_start_output");
9264 adev->offload_effects_stop_output =
9265 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
9266 "offload_effects_bundle_hal_stop_output");
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08009267 adev->offload_effects_set_hpx_state =
9268 (int (*)(bool))dlsym(adev->offload_effects_lib,
9269 "offload_effects_bundle_set_hpx_state");
Dhananjay Kumard68883d2015-09-04 13:39:26 +05309270 adev->offload_effects_get_parameters =
9271 (void (*)(struct str_parms *, struct str_parms *))
9272 dlsym(adev->offload_effects_lib,
9273 "offload_effects_bundle_get_parameters");
9274 adev->offload_effects_set_parameters =
9275 (void (*)(struct str_parms *))dlsym(adev->offload_effects_lib,
9276 "offload_effects_bundle_set_parameters");
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08009277 }
9278 }
9279
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07009280 if (access(ADM_LIBRARY_PATH, R_OK) == 0) {
9281 adev->adm_lib = dlopen(ADM_LIBRARY_PATH, RTLD_NOW);
9282 if (adev->adm_lib == NULL) {
9283 ALOGE("%s: DLOPEN failed for %s", __func__, ADM_LIBRARY_PATH);
9284 } else {
9285 ALOGV("%s: DLOPEN successful for %s", __func__, ADM_LIBRARY_PATH);
9286 adev->adm_init = (adm_init_t)
9287 dlsym(adev->adm_lib, "adm_init");
9288 adev->adm_deinit = (adm_deinit_t)
9289 dlsym(adev->adm_lib, "adm_deinit");
9290 adev->adm_register_input_stream = (adm_register_input_stream_t)
9291 dlsym(adev->adm_lib, "adm_register_input_stream");
9292 adev->adm_register_output_stream = (adm_register_output_stream_t)
9293 dlsym(adev->adm_lib, "adm_register_output_stream");
9294 adev->adm_deregister_stream = (adm_deregister_stream_t)
9295 dlsym(adev->adm_lib, "adm_deregister_stream");
9296 adev->adm_request_focus = (adm_request_focus_t)
9297 dlsym(adev->adm_lib, "adm_request_focus");
9298 adev->adm_abandon_focus = (adm_abandon_focus_t)
9299 dlsym(adev->adm_lib, "adm_abandon_focus");
Haynes Mathew George5beddd42016-06-27 18:33:40 -07009300 adev->adm_set_config = (adm_set_config_t)
9301 dlsym(adev->adm_lib, "adm_set_config");
9302 adev->adm_request_focus_v2 = (adm_request_focus_v2_t)
9303 dlsym(adev->adm_lib, "adm_request_focus_v2");
9304 adev->adm_is_noirq_avail = (adm_is_noirq_avail_t)
9305 dlsym(adev->adm_lib, "adm_is_noirq_avail");
9306 adev->adm_on_routing_change = (adm_on_routing_change_t)
9307 dlsym(adev->adm_lib, "adm_on_routing_change");
Aniket Kumar Lata60586a92019-05-22 22:18:55 -07009308 adev->adm_request_focus_v2_1 = (adm_request_focus_v2_1_t)
9309 dlsym(adev->adm_lib, "adm_request_focus_v2_1");
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07009310 }
9311 }
9312
Aalique Grahame22e49102018-12-18 14:23:57 -08009313 adev->enable_voicerx = false;
Mingming Yin514a8bc2014-07-29 15:22:21 -07009314 adev->bt_wb_speech_enabled = false;
Zhou Song12c29502019-03-16 10:37:18 +08009315 adev->swb_speech_mode = SPEECH_MODE_INVALID;
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08009316 //initialize this to false for now,
9317 //this will be set to true through set param
9318 adev->vr_audio_mode_enabled = false;
Mingming Yin514a8bc2014-07-29 15:22:21 -07009319
Pradnya Chaphekar8a9dcd82014-09-09 09:49:10 -07009320 audio_extn_ds2_enable(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009321 *device = &adev->device.common;
Aalique Grahame22e49102018-12-18 14:23:57 -08009322
9323 if (k_enable_extended_precision)
9324 adev_verify_devices(adev);
9325
Xiaojun Sang785b5da2017-08-03 15:52:29 +08009326 adev->dsp_bit_width_enforce_mode =
9327 adev_init_dsp_bit_width_enforce_mode(adev->mixer);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009328
Dhananjay Kumard6d32152016-10-13 16:11:03 +05309329 audio_extn_utils_update_streams_cfg_lists(adev->platform, adev->mixer,
9330 &adev->streams_output_cfg_list,
9331 &adev->streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07009332
Kiran Kandi910e1862013-10-29 13:29:42 -07009333 audio_device_ref_count++;
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07009334
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07009335 int trial;
Aalique Grahame22e49102018-12-18 14:23:57 -08009336 if ((property_get("vendor.audio_hal.period_size", value, NULL) > 0) ||
9337 (property_get("audio_hal.period_size", value, NULL) > 0)) {
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07009338 trial = atoi(value);
9339 if (period_size_is_plausible_for_low_latency(trial)) {
9340 pcm_config_low_latency.period_size = trial;
9341 pcm_config_low_latency.start_threshold = trial / 4;
9342 pcm_config_low_latency.avail_min = trial / 4;
9343 configured_low_latency_capture_period_size = trial;
9344 }
9345 }
Aalique Grahame22e49102018-12-18 14:23:57 -08009346 if ((property_get("vendor.audio_hal.in_period_size", value, NULL) > 0) ||
9347 (property_get("audio_hal.in_period_size", value, NULL) > 0)) {
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07009348 trial = atoi(value);
9349 if (period_size_is_plausible_for_low_latency(trial)) {
9350 configured_low_latency_capture_period_size = trial;
9351 }
9352 }
9353
Vignesh Kulothungan7d374312018-02-21 17:12:00 -08009354 adev->mic_break_enabled = property_get_bool("vendor.audio.mic_break", false);
9355
Eric Laurent4b084132018-10-19 17:33:43 -07009356 adev->camera_orientation = CAMERA_DEFAULT;
9357
Aalique Grahame22e49102018-12-18 14:23:57 -08009358 if ((property_get("vendor.audio_hal.period_multiplier",value,NULL) > 0) ||
9359 (property_get("audio_hal.period_multiplier",value,NULL) > 0)) {
Haynes Mathew George5beddd42016-06-27 18:33:40 -07009360 af_period_multiplier = atoi(value);
9361 if (af_period_multiplier < 0)
9362 af_period_multiplier = 2;
9363 else if (af_period_multiplier > 4)
9364 af_period_multiplier = 4;
9365
9366 ALOGV("new period_multiplier = %d", af_period_multiplier);
9367 }
9368
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08009369 audio_extn_qdsp_init(adev->platform);
Aalique Grahame22e49102018-12-18 14:23:57 -08009370
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07009371 adev->multi_offload_enable = property_get_bool("vendor.audio.offload.multiple.enabled", false);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07009372 pthread_mutex_unlock(&adev_init_lock);
9373
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07009374 if (adev->adm_init)
9375 adev->adm_data = adev->adm_init();
9376
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05309377 qahwi_init(*device);
Sudheer Papothifa9d2282015-09-17 01:53:25 +05309378 audio_extn_perf_lock_init();
Ben Rombergerd771a7c2017-02-22 18:05:17 -08009379 audio_extn_adsp_hdlr_init(adev->mixer);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05309380
9381 audio_extn_snd_mon_init();
9382 pthread_mutex_lock(&adev->lock);
9383 audio_extn_snd_mon_register_listener(adev, adev_snd_mon_cb);
9384 adev->card_status = CARD_STATUS_ONLINE;
Haynes Mathew George01156f92018-04-13 15:29:54 -07009385 audio_extn_battery_properties_listener_init(adev_on_battery_status_changed);
9386 /*
9387 * if the battery state callback happens before charging can be queried,
9388 * it will be guarded with the adev->lock held in the cb function and so
9389 * the callback value will reflect the latest state
9390 */
9391 adev->is_charging = audio_extn_battery_properties_is_charging();
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05309392 audio_extn_sound_trigger_init(adev); /* dependent on snd_mon_init() */
Zhou Songc1088ea2018-06-12 00:17:29 +08009393 audio_extn_sound_trigger_update_battery_status(adev->is_charging);
justinweng20fb6d82019-02-21 18:49:00 -07009394 audio_extn_audiozoom_init();
Zhou Songc1088ea2018-06-12 00:17:29 +08009395 pthread_mutex_unlock(&adev->lock);
Satish Babu Patakokilac3c5d432017-07-04 22:48:59 +05309396 /* Allocate memory for Device config params */
9397 adev->device_cfg_params = (struct audio_device_config_param*)
9398 calloc(platform_get_max_codec_backend(),
9399 sizeof(struct audio_device_config_param));
9400 if (adev->device_cfg_params == NULL)
9401 ALOGE("%s: Memory allocation failed for Device config params", __func__);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05309402
Dhanalakshmi Siddani20628c82019-01-27 17:31:09 +05309403 /*
9404 * Check if new PSPD matrix mixer control is supported. If not
9405 * supported, then set flag so that old mixer ctrl is sent while
9406 * sending pspd coefficients on older kernel version. Query mixer
9407 * control for default pcm id and channel value one.
9408 */
9409 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
9410 "AudStr %d ChMixer Weight Ch %d", 0, 1);
9411
9412 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
9413 if (!ctl) {
9414 ALOGE("%s: ERROR. Could not get ctl for mixer cmd - %s",
9415 __func__, mixer_ctl_name);
9416 adev->use_old_pspd_mix_ctrl = true;
9417 }
9418
Eric Laurent994a6932013-07-17 11:51:42 -07009419 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009420 return 0;
9421}
9422
9423static struct hw_module_methods_t hal_module_methods = {
9424 .open = adev_open,
9425};
9426
9427struct audio_module HAL_MODULE_INFO_SYM = {
9428 .common = {
9429 .tag = HARDWARE_MODULE_TAG,
9430 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
9431 .hal_api_version = HARDWARE_HAL_API_VERSION,
9432 .id = AUDIO_HARDWARE_MODULE_ID,
9433 .name = "QCOM Audio HAL",
Duy Truongfae19622013-11-24 02:17:54 -08009434 .author = "The Linux Foundation",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009435 .methods = &hal_module_methods,
9436 },
9437};