blob: bc01c8bc9bc23fa2ccb60887578033f017703d2f [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"
Eric Laurentb23d5282013-05-14 15:27:20 -070072#include "platform_api.h"
73#include <platform.h>
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -070074#include "audio_extn.h"
Narsinga Rao Chella05573b72013-11-15 15:21:40 -080075#include "voice_extn.h"
Ashish Jaind5694242017-09-05 20:09:06 +053076#include "ip_hdlr_intf.h"
Arun Mirpurib1bec9c2019-01-29 16:42:45 -080077#include "audio_feature_manager.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)
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070096
Aalique Grahame22e49102018-12-18 14:23:57 -080097#define RECORD_GAIN_MIN 0.0f
98#define RECORD_GAIN_MAX 1.0f
99#define RECORD_VOLUME_CTL_MAX 0x2000
100
101/* treat as unsigned Q1.13 */
102#define APP_TYPE_GAIN_DEFAULT 0x2000
103
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -0700104#define PROXY_OPEN_RETRY_COUNT 100
105#define PROXY_OPEN_WAIT_TIME 20
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -0800106
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800107#define GET_USECASE_AUDIO_PLAYBACK_PRIMARY(db) \
108 (db)? USECASE_AUDIO_PLAYBACK_DEEP_BUFFER : \
109 USECASE_AUDIO_PLAYBACK_LOW_LATENCY
110#define GET_PCM_CONFIG_AUDIO_PLAYBACK_PRIMARY(db) \
111 (db)? pcm_config_deep_buffer : pcm_config_low_latency
Haynes Mathew Georgebf143712013-12-03 13:02:53 -0800112
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700113#define ULL_PERIOD_SIZE (DEFAULT_OUTPUT_SAMPLING_RATE/1000)
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700114#define DEFAULT_VOIP_BUF_DURATION_MS 20
115#define DEFAULT_VOIP_BIT_DEPTH_BYTE sizeof(int16_t)
116#define DEFAULT_VOIP_SAMP_RATE 48000
117
118#define VOIP_IO_BUF_SIZE(SR, DURATION_MS, BIT_DEPTH) (SR)/1000 * DURATION_MS * BIT_DEPTH
119
120struct pcm_config default_pcm_config_voip_copp = {
121 .channels = 1,
122 .rate = DEFAULT_VOIP_SAMP_RATE, /* changed when the stream is opened */
123 .period_size = VOIP_IO_BUF_SIZE(DEFAULT_VOIP_SAMP_RATE, DEFAULT_VOIP_BUF_DURATION_MS, DEFAULT_VOIP_BIT_DEPTH_BYTE)/2,
124 .period_count = 2,
125 .format = PCM_FORMAT_S16_LE,
kunleiz95b597a2017-10-23 17:07:33 +0800126 .avail_min = VOIP_IO_BUF_SIZE(DEFAULT_VOIP_SAMP_RATE, DEFAULT_VOIP_BUF_DURATION_MS, DEFAULT_VOIP_BIT_DEPTH_BYTE)/2,
127 .stop_threshold = INT_MAX,
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700128};
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700129
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700130#define MIN_CHANNEL_COUNT 1
131#define DEFAULT_CHANNEL_COUNT 2
132#define MAX_HIFI_CHANNEL_COUNT 8
133
Aalique Grahame22e49102018-12-18 14:23:57 -0800134#ifndef MAX_TARGET_SPECIFIC_CHANNEL_CNT
135#define MAX_CHANNEL_COUNT 1
136#else
137#define MAX_CHANNEL_COUNT atoi(XSTR(MAX_TARGET_SPECIFIC_CHANNEL_CNT))
138#define XSTR(x) STR(x)
139#define STR(x) #x
140#endif
141
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -0700142static unsigned int configured_low_latency_capture_period_size =
143 LOW_LATENCY_CAPTURE_PERIOD_SIZE;
144
Haynes Mathew George16081042017-05-31 17:16:49 -0700145#define MMAP_PERIOD_SIZE (DEFAULT_OUTPUT_SAMPLING_RATE/1000)
146#define MMAP_PERIOD_COUNT_MIN 32
147#define MMAP_PERIOD_COUNT_MAX 512
148#define MMAP_PERIOD_COUNT_DEFAULT (MMAP_PERIOD_COUNT_MAX)
149
Aalique Grahame22e49102018-12-18 14:23:57 -0800150/* This constant enables extended precision handling.
151 * TODO The flag is off until more testing is done.
152 */
153static const bool k_enable_extended_precision = false;
154
Eric Laurentb23d5282013-05-14 15:27:20 -0700155struct pcm_config pcm_config_deep_buffer = {
156 .channels = 2,
157 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
158 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
159 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
160 .format = PCM_FORMAT_S16_LE,
161 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
162 .stop_threshold = INT_MAX,
163 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
164};
165
166struct pcm_config pcm_config_low_latency = {
167 .channels = 2,
168 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
169 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
170 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
171 .format = PCM_FORMAT_S16_LE,
172 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
173 .stop_threshold = INT_MAX,
174 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
175};
176
Vignesh Kulothungana6927272019-02-20 15:17:07 -0800177struct pcm_config pcm_config_haptics_audio = {
178 .channels = 1,
179 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
180 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
181 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
182 .format = PCM_FORMAT_S16_LE,
183 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
184 .stop_threshold = INT_MAX,
185 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
186};
187
188struct pcm_config pcm_config_haptics = {
189 .channels = 1,
190 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
191 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
192 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
193 .format = PCM_FORMAT_S16_LE,
194 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
195 .stop_threshold = INT_MAX,
196 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
197};
198
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700199static int af_period_multiplier = 4;
200struct pcm_config pcm_config_rt = {
201 .channels = 2,
202 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
203 .period_size = ULL_PERIOD_SIZE, //1 ms
204 .period_count = 512, //=> buffer size is 512ms
205 .format = PCM_FORMAT_S16_LE,
206 .start_threshold = ULL_PERIOD_SIZE*8, //8ms
207 .stop_threshold = INT_MAX,
208 .silence_threshold = 0,
209 .silence_size = 0,
210 .avail_min = ULL_PERIOD_SIZE, //1 ms
211};
212
Eric Laurentb23d5282013-05-14 15:27:20 -0700213struct pcm_config pcm_config_hdmi_multi = {
214 .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
215 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
216 .period_size = HDMI_MULTI_PERIOD_SIZE,
217 .period_count = HDMI_MULTI_PERIOD_COUNT,
218 .format = PCM_FORMAT_S16_LE,
219 .start_threshold = 0,
220 .stop_threshold = INT_MAX,
221 .avail_min = 0,
222};
223
Haynes Mathew George16081042017-05-31 17:16:49 -0700224struct pcm_config pcm_config_mmap_playback = {
225 .channels = 2,
226 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
227 .period_size = MMAP_PERIOD_SIZE,
228 .period_count = MMAP_PERIOD_COUNT_DEFAULT,
229 .format = PCM_FORMAT_S16_LE,
230 .start_threshold = MMAP_PERIOD_SIZE*8,
231 .stop_threshold = INT32_MAX,
232 .silence_threshold = 0,
233 .silence_size = 0,
234 .avail_min = MMAP_PERIOD_SIZE, //1 ms
235};
236
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700237struct pcm_config pcm_config_hifi = {
238 .channels = DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
239 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
240 .period_size = HIFI_BUFFER_OUTPUT_PERIOD_SIZE, /* change #define */
241 .period_count = HIFI_BUFFER_OUTPUT_PERIOD_COUNT,
242 .format = PCM_FORMAT_S24_3LE,
243 .start_threshold = 0,
244 .stop_threshold = INT_MAX,
245 .avail_min = 0,
246};
247
Eric Laurentb23d5282013-05-14 15:27:20 -0700248struct pcm_config pcm_config_audio_capture = {
249 .channels = 2,
Eric Laurentb23d5282013-05-14 15:27:20 -0700250 .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
251 .format = PCM_FORMAT_S16_LE,
252};
253
Haynes Mathew George16081042017-05-31 17:16:49 -0700254struct pcm_config pcm_config_mmap_capture = {
255 .channels = 2,
256 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
257 .period_size = MMAP_PERIOD_SIZE,
258 .period_count = MMAP_PERIOD_COUNT_DEFAULT,
259 .format = PCM_FORMAT_S16_LE,
260 .start_threshold = 0,
261 .stop_threshold = INT_MAX,
262 .silence_threshold = 0,
263 .silence_size = 0,
264 .avail_min = MMAP_PERIOD_SIZE, //1 ms
265};
266
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -0700267#define AFE_PROXY_CHANNEL_COUNT 2
268#define AFE_PROXY_SAMPLING_RATE 48000
269
270#define AFE_PROXY_PLAYBACK_PERIOD_SIZE 768
271#define AFE_PROXY_PLAYBACK_PERIOD_COUNT 4
272
273struct pcm_config pcm_config_afe_proxy_playback = {
274 .channels = AFE_PROXY_CHANNEL_COUNT,
275 .rate = AFE_PROXY_SAMPLING_RATE,
276 .period_size = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
277 .period_count = AFE_PROXY_PLAYBACK_PERIOD_COUNT,
278 .format = PCM_FORMAT_S16_LE,
279 .start_threshold = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
280 .stop_threshold = INT_MAX,
281 .avail_min = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
282};
283
284#define AFE_PROXY_RECORD_PERIOD_SIZE 768
285#define AFE_PROXY_RECORD_PERIOD_COUNT 4
286
Aalique Grahame22e49102018-12-18 14:23:57 -0800287struct pcm_config pcm_config_audio_capture_rt = {
288 .channels = 2,
289 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
290 .period_size = ULL_PERIOD_SIZE,
291 .period_count = 512,
292 .format = PCM_FORMAT_S16_LE,
293 .start_threshold = 0,
294 .stop_threshold = AFE_PROXY_RECORD_PERIOD_SIZE * AFE_PROXY_RECORD_PERIOD_COUNT,
295 .silence_threshold = 0,
296 .silence_size = 0,
297 .avail_min = ULL_PERIOD_SIZE, //1 ms
298};
299
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -0700300struct pcm_config pcm_config_afe_proxy_record = {
301 .channels = AFE_PROXY_CHANNEL_COUNT,
302 .rate = AFE_PROXY_SAMPLING_RATE,
303 .period_size = AFE_PROXY_RECORD_PERIOD_SIZE,
304 .period_count = AFE_PROXY_RECORD_PERIOD_COUNT,
305 .format = PCM_FORMAT_S16_LE,
306 .start_threshold = AFE_PROXY_RECORD_PERIOD_SIZE,
307 .stop_threshold = INT_MAX,
308 .avail_min = AFE_PROXY_RECORD_PERIOD_SIZE,
309};
310
Ashish Jainf1eaa582016-05-23 20:54:24 +0530311#define AUDIO_MAX_PCM_FORMATS 7
312
313const uint32_t format_to_bitwidth_table[AUDIO_MAX_PCM_FORMATS] = {
314 [AUDIO_FORMAT_DEFAULT] = 0,
315 [AUDIO_FORMAT_PCM_16_BIT] = sizeof(uint16_t),
316 [AUDIO_FORMAT_PCM_8_BIT] = sizeof(uint8_t),
317 [AUDIO_FORMAT_PCM_32_BIT] = sizeof(uint32_t),
318 [AUDIO_FORMAT_PCM_8_24_BIT] = sizeof(uint32_t),
319 [AUDIO_FORMAT_PCM_FLOAT] = sizeof(float),
320 [AUDIO_FORMAT_PCM_24_BIT_PACKED] = sizeof(uint8_t) * 3,
321};
322
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800323const char * const use_case_table[AUDIO_USECASE_MAX] = {
Eric Laurentb23d5282013-05-14 15:27:20 -0700324 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
325 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
Vignesh Kulothungana6927272019-02-20 15:17:07 -0800326 [USECASE_AUDIO_PLAYBACK_WITH_HAPTICS] = "audio-with-haptics-playback",
Ravi Kumar Alamanda474de5a2015-06-25 20:08:01 -0700327 [USECASE_AUDIO_PLAYBACK_ULL] = "audio-ull-playback",
328 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback",
Shruthi Krishnaace10852013-10-25 14:32:12 -0700329 [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback",
vivek mehta446c3962015-09-14 10:57:35 -0700330 //Enabled for Direct_PCM
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700331 [USECASE_AUDIO_PLAYBACK_OFFLOAD2] = "compress-offload-playback2",
332 [USECASE_AUDIO_PLAYBACK_OFFLOAD3] = "compress-offload-playback3",
333 [USECASE_AUDIO_PLAYBACK_OFFLOAD4] = "compress-offload-playback4",
334 [USECASE_AUDIO_PLAYBACK_OFFLOAD5] = "compress-offload-playback5",
335 [USECASE_AUDIO_PLAYBACK_OFFLOAD6] = "compress-offload-playback6",
336 [USECASE_AUDIO_PLAYBACK_OFFLOAD7] = "compress-offload-playback7",
337 [USECASE_AUDIO_PLAYBACK_OFFLOAD8] = "compress-offload-playback8",
338 [USECASE_AUDIO_PLAYBACK_OFFLOAD9] = "compress-offload-playback9",
Haynes Mathew George16081042017-05-31 17:16:49 -0700339 [USECASE_AUDIO_PLAYBACK_FM] = "play-fm",
340 [USECASE_AUDIO_PLAYBACK_MMAP] = "mmap-playback",
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700341 [USECASE_AUDIO_PLAYBACK_HIFI] = "hifi-playback",
Aalique Grahame22e49102018-12-18 14:23:57 -0800342 [USECASE_AUDIO_PLAYBACK_TTS] = "audio-tts-playback",
vivek mehta0ea887a2015-08-26 14:01:20 -0700343
Eric Laurentb23d5282013-05-14 15:27:20 -0700344 [USECASE_AUDIO_RECORD] = "audio-record",
Mingming Yine62d7842013-10-25 16:26:03 -0700345 [USECASE_AUDIO_RECORD_COMPRESS] = "audio-record-compress",
Dhananjay Kumaree4d2002016-10-25 18:02:58 +0530346 [USECASE_AUDIO_RECORD_COMPRESS2] = "audio-record-compress2",
347 [USECASE_AUDIO_RECORD_COMPRESS3] = "audio-record-compress3",
348 [USECASE_AUDIO_RECORD_COMPRESS4] = "audio-record-compress4",
Dhananjay Kumar376e38b2017-09-28 22:26:23 +0530349 [USECASE_AUDIO_RECORD_COMPRESS5] = "audio-record-compress5",
350 [USECASE_AUDIO_RECORD_COMPRESS6] = "audio-record-compress6",
Eric Laurentb23d5282013-05-14 15:27:20 -0700351 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
Preetam Singh Ranawatde84f1a2013-11-01 14:58:16 -0700352 [USECASE_AUDIO_RECORD_FM_VIRTUAL] = "fm-virtual-record",
Haynes Mathew George16081042017-05-31 17:16:49 -0700353 [USECASE_AUDIO_RECORD_MMAP] = "mmap-record",
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700354 [USECASE_AUDIO_RECORD_HIFI] = "hifi-record",
Haynes Mathew George16081042017-05-31 17:16:49 -0700355
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -0800356 [USECASE_AUDIO_HFP_SCO] = "hfp-sco",
Vimal Puthanveed47e64852013-12-20 13:23:39 -0800357 [USECASE_AUDIO_HFP_SCO_WB] = "hfp-sco-wb",
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700358 [USECASE_VOICE_CALL] = "voice-call",
Mingming Yin3ee55c62014-08-04 14:23:35 -0700359
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700360 [USECASE_VOICE2_CALL] = "voice2-call",
361 [USECASE_VOLTE_CALL] = "volte-call",
362 [USECASE_QCHAT_CALL] = "qchat-call",
Vicky Sehrawat7e4fc152014-02-12 17:58:59 -0800363 [USECASE_VOWLAN_CALL] = "vowlan-call",
Vidyakumar Athota0e109352015-02-12 17:38:22 -0800364 [USECASE_VOICEMMODE1_CALL] = "voicemmode1-call",
365 [USECASE_VOICEMMODE2_CALL] = "voicemmode2-call",
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800366 [USECASE_COMPRESS_VOIP_CALL] = "compress-voip-call",
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700367 [USECASE_INCALL_REC_UPLINK] = "incall-rec-uplink",
368 [USECASE_INCALL_REC_DOWNLINK] = "incall-rec-downlink",
369 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK] = "incall-rec-uplink-and-downlink",
Helen Zenge56b4852013-12-03 16:54:40 -0800370 [USECASE_INCALL_REC_UPLINK_COMPRESS] = "incall-rec-uplink-compress",
371 [USECASE_INCALL_REC_DOWNLINK_COMPRESS] = "incall-rec-downlink-compress",
372 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK_COMPRESS] = "incall-rec-uplink-and-downlink-compress",
373
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -0700374 [USECASE_INCALL_MUSIC_UPLINK] = "incall_music_uplink",
375 [USECASE_INCALL_MUSIC_UPLINK2] = "incall_music_uplink2",
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700376 [USECASE_AUDIO_SPKR_CALIB_RX] = "spkr-rx-calib",
377 [USECASE_AUDIO_SPKR_CALIB_TX] = "spkr-vi-record",
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -0700378
379 [USECASE_AUDIO_PLAYBACK_AFE_PROXY] = "afe-proxy-playback",
380 [USECASE_AUDIO_RECORD_AFE_PROXY] = "afe-proxy-record",
Md Mansoor Ahmeddb1b4f92018-01-25 18:56:31 +0530381 [USECASE_AUDIO_PLAYBACK_SILENCE] = "silence-playback",
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700382
Siddartha Shaik31b530e2017-05-19 15:26:33 +0530383 /* Transcode loopback cases */
Surendar Karka93cd25a2018-08-28 14:21:37 +0530384 [USECASE_AUDIO_TRANSCODE_LOOPBACK_RX] = "audio-transcode-loopback-rx",
385 [USECASE_AUDIO_TRANSCODE_LOOPBACK_TX] = "audio-transcode-loopback-tx",
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700386
387 [USECASE_AUDIO_PLAYBACK_VOIP] = "audio-playback-voip",
388 [USECASE_AUDIO_RECORD_VOIP] = "audio-record-voip",
Varun Balaraje49253e2017-07-06 19:48:56 +0530389 /* For Interactive Audio Streams */
390 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM1] = "audio-interactive-stream1",
391 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM2] = "audio-interactive-stream2",
392 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM3] = "audio-interactive-stream3",
393 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM4] = "audio-interactive-stream4",
394 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM5] = "audio-interactive-stream5",
395 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM6] = "audio-interactive-stream6",
396 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM7] = "audio-interactive-stream7",
397 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM8] = "audio-interactive-stream8",
Garmond Leunge2433c32017-09-28 21:51:22 -0700398
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800399 [USECASE_AUDIO_EC_REF_LOOPBACK] = "ec-ref-audio-capture",
400
401 [USECASE_AUDIO_A2DP_ABR_FEEDBACK] = "a2dp-abr-feedback"
Eric Laurentb23d5282013-05-14 15:27:20 -0700402};
403
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700404static const audio_usecase_t offload_usecases[] = {
405 USECASE_AUDIO_PLAYBACK_OFFLOAD,
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700406 USECASE_AUDIO_PLAYBACK_OFFLOAD2,
407 USECASE_AUDIO_PLAYBACK_OFFLOAD3,
408 USECASE_AUDIO_PLAYBACK_OFFLOAD4,
409 USECASE_AUDIO_PLAYBACK_OFFLOAD5,
410 USECASE_AUDIO_PLAYBACK_OFFLOAD6,
411 USECASE_AUDIO_PLAYBACK_OFFLOAD7,
412 USECASE_AUDIO_PLAYBACK_OFFLOAD8,
413 USECASE_AUDIO_PLAYBACK_OFFLOAD9,
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700414};
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800415
Varun Balaraje49253e2017-07-06 19:48:56 +0530416static const audio_usecase_t interactive_usecases[] = {
417 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM1,
418 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM2,
419 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM3,
420 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM4,
421 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM5,
422 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM6,
423 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM7,
424 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM8,
425};
426
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800427#define STRING_TO_ENUM(string) { #string, string }
428
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800429struct string_to_enum {
430 const char *name;
431 uint32_t value;
432};
433
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700434static const struct string_to_enum channels_name_to_enum_table[] = {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800435 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
Mingming Yin3a941d42016-02-17 18:08:05 -0800436 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_2POINT1),
437 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_QUAD),
438 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_SURROUND),
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -0700439 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_PENTA),
Mingming Yin3a941d42016-02-17 18:08:05 -0800440 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
441 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_6POINT1),
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800442 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700443 STRING_TO_ENUM(AUDIO_CHANNEL_IN_MONO),
444 STRING_TO_ENUM(AUDIO_CHANNEL_IN_STEREO),
445 STRING_TO_ENUM(AUDIO_CHANNEL_IN_FRONT_BACK),
446 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_1),
447 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_2),
448 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_3),
449 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_4),
450 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_5),
451 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_6),
452 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_7),
453 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_8),
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800454};
455
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700456static const struct string_to_enum formats_name_to_enum_table[] = {
457 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
458 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
459 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Pradnya Chaphekar8a9dcd82014-09-09 09:49:10 -0700460 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
461 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
462 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Ben Romberger1aaaf862017-04-06 17:49:46 -0700463 STRING_TO_ENUM(AUDIO_FORMAT_DOLBY_TRUEHD),
Mingming Yin3a941d42016-02-17 18:08:05 -0800464 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
465 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
Naresh Tanniru928f0862017-04-07 16:44:23 -0700466 STRING_TO_ENUM(AUDIO_FORMAT_IEC61937)
Mingming Yin3a941d42016-02-17 18:08:05 -0800467};
468
469//list of all supported sample rates by HDMI specification.
470static const int out_hdmi_sample_rates[] = {
471 32000, 44100, 48000, 88200, 96000, 176400, 192000,
472};
473
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700474static const struct string_to_enum out_sample_rates_name_to_enum_table[] = {
Mingming Yin3a941d42016-02-17 18:08:05 -0800475 STRING_TO_ENUM(32000),
476 STRING_TO_ENUM(44100),
477 STRING_TO_ENUM(48000),
478 STRING_TO_ENUM(88200),
479 STRING_TO_ENUM(96000),
480 STRING_TO_ENUM(176400),
481 STRING_TO_ENUM(192000),
Pradnya Chaphekar8a9dcd82014-09-09 09:49:10 -0700482};
483
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700484static struct audio_device *adev = NULL;
Ben Romberger02ab1192018-05-24 12:10:08 -0700485static pthread_mutex_t adev_init_lock = PTHREAD_MUTEX_INITIALIZER;
Kiran Kandi910e1862013-10-29 13:29:42 -0700486static unsigned int audio_device_ref_count;
vivek mehtab72d08d2016-04-29 03:16:47 -0700487//cache last MBDRC cal step level
488static int last_known_cal_step = -1 ;
Kiran Kandi910e1862013-10-29 13:29:42 -0700489
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +0530490static int check_a2dp_restore_l(struct audio_device *adev, struct stream_out *out, bool restore);
491static int out_set_compr_volume(struct audio_stream_out *stream, float left, float right);
Arun Mirpuri5d170872019-03-26 13:21:31 -0700492static int out_set_mmap_volume(struct audio_stream_out *stream, float left, float right);
Zhou Song2b8f28f2017-09-11 10:51:38 +0800493static int out_set_voip_volume(struct audio_stream_out *stream, float left, float right);
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +0530494static int out_set_pcm_volume(struct audio_stream_out *stream, float left, float right);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +0530495
Vatsal Buchac09ae062018-11-14 13:25:08 +0530496#ifdef AUDIO_FEATURE_ENABLED_GCOV
497extern void __gcov_flush();
498static void enable_gcov()
499{
500 __gcov_flush();
501}
502#else
503static void enable_gcov()
504{
505}
506#endif
507
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700508static bool may_use_noirq_mode(struct audio_device *adev, audio_usecase_t uc_id,
509 int flags __unused)
510{
511 int dir = 0;
512 switch (uc_id) {
513 case USECASE_AUDIO_RECORD_LOW_LATENCY:
514 dir = 1;
515 case USECASE_AUDIO_PLAYBACK_ULL:
516 break;
517 default:
518 return false;
519 }
520
521 int dev_id = platform_get_pcm_device_id(uc_id, dir == 0 ?
522 PCM_PLAYBACK : PCM_CAPTURE);
523 if (adev->adm_is_noirq_avail)
524 return adev->adm_is_noirq_avail(adev->adm_data,
525 adev->snd_card, dev_id, dir);
526 return false;
527}
528
529static void register_out_stream(struct stream_out *out)
530{
531 struct audio_device *adev = out->dev;
532 if (is_offload_usecase(out->usecase) ||
533 !adev->adm_register_output_stream)
534 return;
535
536 // register stream first for backward compatibility
537 adev->adm_register_output_stream(adev->adm_data,
538 out->handle,
539 out->flags);
540
541 if (!adev->adm_set_config)
542 return;
543
544 if (out->realtime)
545 adev->adm_set_config(adev->adm_data,
546 out->handle,
547 out->pcm, &out->config);
548}
549
550static void register_in_stream(struct stream_in *in)
551{
552 struct audio_device *adev = in->dev;
553 if (!adev->adm_register_input_stream)
554 return;
555
556 adev->adm_register_input_stream(adev->adm_data,
557 in->capture_handle,
558 in->flags);
559
560 if (!adev->adm_set_config)
561 return;
562
563 if (in->realtime)
564 adev->adm_set_config(adev->adm_data,
565 in->capture_handle,
566 in->pcm,
567 &in->config);
568}
569
570static void request_out_focus(struct stream_out *out, long ns)
571{
572 struct audio_device *adev = out->dev;
573
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700574 if (adev->adm_request_focus_v2)
575 adev->adm_request_focus_v2(adev->adm_data, out->handle, ns);
576 else if (adev->adm_request_focus)
577 adev->adm_request_focus(adev->adm_data, out->handle);
578}
579
580static void request_in_focus(struct stream_in *in, long ns)
581{
582 struct audio_device *adev = in->dev;
583
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700584 if (adev->adm_request_focus_v2)
585 adev->adm_request_focus_v2(adev->adm_data, in->capture_handle, ns);
586 else if (adev->adm_request_focus)
587 adev->adm_request_focus(adev->adm_data, in->capture_handle);
588}
589
590static void release_out_focus(struct stream_out *out)
591{
592 struct audio_device *adev = out->dev;
593
594 if (adev->adm_abandon_focus)
595 adev->adm_abandon_focus(adev->adm_data, out->handle);
596}
597
598static void release_in_focus(struct stream_in *in)
599{
600 struct audio_device *adev = in->dev;
601 if (adev->adm_abandon_focus)
602 adev->adm_abandon_focus(adev->adm_data, in->capture_handle);
603}
604
Dhananjay Kumare6293dd2017-05-25 17:25:30 +0530605static int parse_snd_card_status(struct str_parms *parms, int *card,
606 card_status_t *status)
607{
608 char value[32]={0};
609 char state[32]={0};
610
611 int ret = str_parms_get_str(parms, "SND_CARD_STATUS", value, sizeof(value));
612 if (ret < 0)
613 return -1;
614
615 // sscanf should be okay as value is of max length 32.
616 // same as sizeof state.
617 if (sscanf(value, "%d,%s", card, state) < 2)
618 return -1;
619
620 *status = !strcmp(state, "ONLINE") ? CARD_STATUS_ONLINE :
621 CARD_STATUS_OFFLINE;
622 return 0;
623}
624
Haynes Mathew Georgeb0f5dc32017-10-06 18:35:12 -0700625static inline void adjust_frames_for_device_delay(struct stream_out *out,
626 uint32_t *dsp_frames) {
627 // Adjustment accounts for A2dp encoder latency with offload usecases
628 // Note: Encoder latency is returned in ms.
629 if (AUDIO_DEVICE_OUT_ALL_A2DP & out->devices) {
630 unsigned long offset =
631 (audio_extn_a2dp_get_encoder_latency() * out->sample_rate / 1000);
632 *dsp_frames = (*dsp_frames > offset) ? (*dsp_frames - offset) : 0;
633 }
634}
635
vivek mehtaa76401a2015-04-24 14:12:15 -0700636__attribute__ ((visibility ("default")))
637bool audio_hw_send_gain_dep_calibration(int level) {
638 bool ret_val = false;
vivek mehtab72d08d2016-04-29 03:16:47 -0700639 ALOGV("%s: called ...", __func__);
vivek mehtaa76401a2015-04-24 14:12:15 -0700640
641 pthread_mutex_lock(&adev_init_lock);
642
643 if (adev != NULL && adev->platform != NULL) {
644 pthread_mutex_lock(&adev->lock);
645 ret_val = platform_send_gain_dep_cal(adev->platform, level);
vivek mehtab72d08d2016-04-29 03:16:47 -0700646
Preetam Singh Ranawatf4ae0222017-05-31 17:07:28 +0530647 // cache level info for any of the use case which
648 // was not started.
649 last_known_cal_step = level;;
vivek mehtab72d08d2016-04-29 03:16:47 -0700650
vivek mehtaa76401a2015-04-24 14:12:15 -0700651 pthread_mutex_unlock(&adev->lock);
652 } else {
653 ALOGE("%s: %s is NULL", __func__, adev == NULL ? "adev" : "adev->platform");
654 }
655
656 pthread_mutex_unlock(&adev_init_lock);
657
658 return ret_val;
659}
660
Ashish Jain5106d362016-05-11 19:23:33 +0530661static int check_and_set_gapless_mode(struct audio_device *adev, bool enable_gapless)
662{
Krishnankutty Kolathappilly6d8788b2014-01-09 12:45:31 -0800663 bool gapless_enabled = false;
664 const char *mixer_ctl_name = "Compress Gapless Playback";
665 struct mixer_ctl *ctl;
666
667 ALOGV("%s:", __func__);
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700668 gapless_enabled = property_get_bool("vendor.audio.offload.gapless.enabled", false);
Ashish Jain5106d362016-05-11 19:23:33 +0530669
670 /*Disable gapless if its AV playback*/
671 gapless_enabled = gapless_enabled && enable_gapless;
Krishnankutty Kolathappilly6d8788b2014-01-09 12:45:31 -0800672
673 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
674 if (!ctl) {
675 ALOGE("%s: Could not get ctl for mixer cmd - %s",
676 __func__, mixer_ctl_name);
677 return -EINVAL;
678 }
679
680 if (mixer_ctl_set_value(ctl, 0, gapless_enabled) < 0) {
681 ALOGE("%s: Could not set gapless mode %d",
682 __func__, gapless_enabled);
683 return -EINVAL;
684 }
685 return 0;
686}
Haynes Mathew George5191a852013-09-11 14:19:36 -0700687
Aniket Kumar Lataf56b6402016-10-27 12:03:18 -0700688__attribute__ ((visibility ("default")))
689int audio_hw_get_gain_level_mapping(struct amp_db_and_gain_table *mapping_tbl,
690 int table_size) {
691 int ret_val = 0;
692 ALOGV("%s: enter ... ", __func__);
693
694 pthread_mutex_lock(&adev_init_lock);
695 if (adev == NULL) {
696 ALOGW("%s: adev is NULL .... ", __func__);
697 goto done;
698 }
699
700 pthread_mutex_lock(&adev->lock);
701 ret_val = platform_get_gain_level_mapping(mapping_tbl, table_size);
702 pthread_mutex_unlock(&adev->lock);
703done:
704 pthread_mutex_unlock(&adev_init_lock);
705 ALOGV("%s: exit ... ", __func__);
706 return ret_val;
707}
708
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800709bool audio_hw_send_qdsp_parameter(int stream_type, float vol, bool active)
Aalique Grahame22e49102018-12-18 14:23:57 -0800710{
711 bool ret = false;
712 ALOGV("%s: enter ...", __func__);
713
714 pthread_mutex_lock(&adev_init_lock);
715
716 if (adev != NULL && adev->platform != NULL) {
717 pthread_mutex_lock(&adev->lock);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800718 ret = audio_extn_qdsp_set_state(adev, stream_type, vol, active);
Aalique Grahame22e49102018-12-18 14:23:57 -0800719 pthread_mutex_unlock(&adev->lock);
720 }
721
722 pthread_mutex_unlock(&adev_init_lock);
723
724 ALOGV("%s: exit with ret %d", __func__, ret);
725 return ret;
726}
Aalique Grahame22e49102018-12-18 14:23:57 -0800727
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700728static bool is_supported_format(audio_format_t format)
729{
Eric Laurent86e17132013-09-12 17:49:30 -0700730 if (format == AUDIO_FORMAT_MP3 ||
Satish Babu Patakokila0c313922016-12-08 12:07:08 +0530731 format == AUDIO_FORMAT_MP2 ||
Ashish Jainf9b78162014-08-25 20:36:25 +0530732 format == AUDIO_FORMAT_AAC_LC ||
733 format == AUDIO_FORMAT_AAC_HE_V1 ||
734 format == AUDIO_FORMAT_AAC_HE_V2 ||
Manish Dewangana6fc5442015-08-24 20:30:31 +0530735 format == AUDIO_FORMAT_AAC_ADTS_LC ||
736 format == AUDIO_FORMAT_AAC_ADTS_HE_V1 ||
737 format == AUDIO_FORMAT_AAC_ADTS_HE_V2 ||
Arun Kumar Dasari3b174182016-12-27 13:01:14 +0530738 format == AUDIO_FORMAT_AAC_LATM_LC ||
739 format == AUDIO_FORMAT_AAC_LATM_HE_V1 ||
740 format == AUDIO_FORMAT_AAC_LATM_HE_V2 ||
Ashish Jain5106d362016-05-11 19:23:33 +0530741 format == AUDIO_FORMAT_PCM_24_BIT_PACKED ||
742 format == AUDIO_FORMAT_PCM_8_24_BIT ||
Ashish Jainf1eaa582016-05-23 20:54:24 +0530743 format == AUDIO_FORMAT_PCM_FLOAT ||
744 format == AUDIO_FORMAT_PCM_32_BIT ||
vivek mehta0ea887a2015-08-26 14:01:20 -0700745 format == AUDIO_FORMAT_PCM_16_BIT ||
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530746 format == AUDIO_FORMAT_AC3 ||
747 format == AUDIO_FORMAT_E_AC3 ||
Ben Romberger1aaaf862017-04-06 17:49:46 -0700748 format == AUDIO_FORMAT_DOLBY_TRUEHD ||
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530749 format == AUDIO_FORMAT_DTS ||
750 format == AUDIO_FORMAT_DTS_HD ||
Weiyin Jiang18ac4e92015-03-15 15:03:40 +0800751 format == AUDIO_FORMAT_FLAC ||
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530752 format == AUDIO_FORMAT_ALAC ||
753 format == AUDIO_FORMAT_APE ||
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530754 format == AUDIO_FORMAT_DSD ||
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530755 format == AUDIO_FORMAT_VORBIS ||
Weiyin Jiang18ac4e92015-03-15 15:03:40 +0800756 format == AUDIO_FORMAT_WMA ||
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +0530757 format == AUDIO_FORMAT_WMA_PRO ||
Naresh Tanniru928f0862017-04-07 16:44:23 -0700758 format == AUDIO_FORMAT_APTX ||
759 format == AUDIO_FORMAT_IEC61937)
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -0800760 return true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700761
762 return false;
763}
764
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700765static inline bool is_mmap_usecase(audio_usecase_t uc_id)
766{
767 return (uc_id == USECASE_AUDIO_RECORD_AFE_PROXY) ||
768 (uc_id == USECASE_AUDIO_PLAYBACK_AFE_PROXY);
769}
770
Avinash Vaish71a8b972014-07-24 15:36:33 +0530771static int enable_audio_route_for_voice_usecases(struct audio_device *adev,
772 struct audio_usecase *uc_info)
773{
774 struct listnode *node;
775 struct audio_usecase *usecase;
776
777 if (uc_info == NULL)
778 return -EINVAL;
779
780 /* Re-route all voice usecases on the shared backend other than the
781 specified usecase to new snd devices */
782 list_for_each(node, &adev->usecase_list) {
783 usecase = node_to_item(node, struct audio_usecase, list);
Narsinga Rao Chellae7f33c12015-01-28 15:23:34 -0800784 if ((usecase->type == VOICE_CALL) && (usecase != uc_info))
Avinash Vaish71a8b972014-07-24 15:36:33 +0530785 enable_audio_route(adev, usecase);
786 }
787 return 0;
788}
789
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +0530790static void enable_asrc_mode(struct audio_device *adev)
Preetam Singh Ranawatb0c0dd72016-08-18 00:32:06 +0530791{
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +0530792 ALOGV("%s", __func__);
793 audio_route_apply_and_update_path(adev->audio_route,
794 "asrc-mode");
795 adev->asrc_mode_enabled = true;
796}
797
798static void disable_asrc_mode(struct audio_device *adev)
799{
800 ALOGV("%s", __func__);
801 audio_route_reset_and_update_path(adev->audio_route,
802 "asrc-mode");
803 adev->asrc_mode_enabled = false;
804}
805
806/*
807 * - Enable ASRC mode for incoming mix path use case(Headphone backend)if Headphone
808 * 44.1 or Native DSD backends are enabled for any of current use case.
809 * e.g. 48-> + (Naitve DSD or Headphone 44.1)
810 * - Disable current mix path use case(Headphone backend) and re-enable it with
811 * ASRC mode for incoming Headphone 44.1 or Native DSD use case.
812 * e.g. Naitve DSD or Headphone 44.1 -> + 48
813 */
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +0530814static void check_and_set_asrc_mode(struct audio_device *adev,
815 struct audio_usecase *uc_info,
816 snd_device_t snd_device)
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +0530817{
818 ALOGV("%s snd device %d", __func__, snd_device);
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +0530819 int i, num_new_devices = 0;
820 snd_device_t split_new_snd_devices[SND_DEVICE_OUT_END];
821 /*
822 *Split snd device for new combo use case
823 *e.g. Headphopne 44.1-> + Ringtone (Headphone + Speaker)
824 */
825 if (platform_split_snd_device(adev->platform,
826 snd_device,
827 &num_new_devices,
828 split_new_snd_devices) == 0) {
829 for (i = 0; i < num_new_devices; i++)
830 check_and_set_asrc_mode(adev, uc_info, split_new_snd_devices[i]);
831 } else {
832 int new_backend_idx = platform_get_backend_index(snd_device);
833 if (((new_backend_idx == HEADPHONE_BACKEND) ||
834 (new_backend_idx == HEADPHONE_44_1_BACKEND) ||
835 (new_backend_idx == DSD_NATIVE_BACKEND)) &&
836 !adev->asrc_mode_enabled) {
837 struct listnode *node = NULL;
838 struct audio_usecase *uc = NULL;
839 struct stream_out *curr_out = NULL;
840 int usecase_backend_idx = DEFAULT_CODEC_BACKEND;
841 int i, num_devices, ret = 0;
842 snd_device_t split_snd_devices[SND_DEVICE_OUT_END];
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +0530843
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +0530844 list_for_each(node, &adev->usecase_list) {
845 uc = node_to_item(node, struct audio_usecase, list);
846 curr_out = (struct stream_out*) uc->stream.out;
847 if (curr_out && PCM_PLAYBACK == uc->type && uc != uc_info) {
848 /*
849 *Split snd device for existing combo use case
850 *e.g. Ringtone (Headphone + Speaker) + Headphopne 44.1
851 */
852 ret = platform_split_snd_device(adev->platform,
853 uc->out_snd_device,
854 &num_devices,
855 split_snd_devices);
856 if (ret < 0 || num_devices == 0) {
857 ALOGV("%s: Unable to split uc->out_snd_device: %d",__func__, uc->out_snd_device);
858 split_snd_devices[0] = uc->out_snd_device;
859 num_devices = 1;
Garmond Leung50058f62017-02-08 09:49:30 -0800860 }
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +0530861 for (i = 0; i < num_devices; i++) {
862 usecase_backend_idx = platform_get_backend_index(split_snd_devices[i]);
863 ALOGD("%s:snd_dev %d usecase_backend_idx %d",__func__, split_snd_devices[i],usecase_backend_idx);
864 if((new_backend_idx == HEADPHONE_BACKEND) &&
865 ((usecase_backend_idx == HEADPHONE_44_1_BACKEND) ||
866 (usecase_backend_idx == DSD_NATIVE_BACKEND))) {
867 ALOGD("%s:DSD or native stream detected enabling asrcmode in hardware",
868 __func__);
869 enable_asrc_mode(adev);
870 break;
871 } else if(((new_backend_idx == HEADPHONE_44_1_BACKEND) ||
872 (new_backend_idx == DSD_NATIVE_BACKEND)) &&
873 (usecase_backend_idx == HEADPHONE_BACKEND)) {
874 ALOGD("%s:48K stream detected, disabling and enabling it with asrcmode in hardware",
875 __func__);
876 disable_audio_route(adev, uc);
877 disable_snd_device(adev, uc->out_snd_device);
878 // Apply true-high-quality-mode if DSD or > 44.1KHz or >=24-bit
879 if (new_backend_idx == DSD_NATIVE_BACKEND)
880 audio_route_apply_and_update_path(adev->audio_route,
881 "hph-true-highquality-mode");
882 else if ((new_backend_idx == HEADPHONE_44_1_BACKEND) &&
883 (curr_out->bit_width >= 24))
884 audio_route_apply_and_update_path(adev->audio_route,
885 "hph-highquality-mode");
886 enable_asrc_mode(adev);
887 enable_snd_device(adev, uc->out_snd_device);
888 enable_audio_route(adev, uc);
889 break;
890 }
891 }
892 // reset split devices count
893 num_devices = 0;
Garmond Leung50058f62017-02-08 09:49:30 -0800894 }
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +0530895 if (adev->asrc_mode_enabled)
896 break;
Preetam Singh Ranawatb0c0dd72016-08-18 00:32:06 +0530897 }
898 }
899 }
900}
901
Vikram Pandurangadf59cae2017-08-03 18:04:55 -0700902static int send_effect_enable_disable_mixer_ctl(struct audio_device *adev,
903 struct audio_effect_config effect_config,
904 unsigned int param_value)
905{
906 char mixer_ctl_name[] = "Audio Effect";
907 struct mixer_ctl *ctl;
908 long set_values[6];
909 struct stream_in *in = adev->active_input;
910
911 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
912 if (!ctl) {
913 ALOGE("%s: Could not get mixer ctl - %s",
914 __func__, mixer_ctl_name);
915 return -EINVAL;
916 }
917
918 set_values[0] = 1; //0:Rx 1:Tx
919 set_values[1] = in->app_type_cfg.app_type;
920 set_values[2] = (long)effect_config.module_id;
921 set_values[3] = (long)effect_config.instance_id;
922 set_values[4] = (long)effect_config.param_id;
923 set_values[5] = param_value;
924
925 mixer_ctl_set_array(ctl, set_values, ARRAY_SIZE(set_values));
926
927 return 0;
928
929}
930
931static int update_effect_param_ecns(struct audio_device *adev, unsigned int module_id,
932 int effect_type, unsigned int *param_value)
933{
934 int ret = 0;
935 struct audio_effect_config other_effect_config;
936 struct audio_usecase *usecase = NULL;
937 struct stream_in *in = adev->active_input;
938
939 usecase = get_usecase_from_list(adev, in->usecase);
940 if (!usecase)
941 return -EINVAL;
942
943 ret = platform_get_effect_config_data(usecase->in_snd_device, &other_effect_config,
944 effect_type == EFFECT_AEC ? EFFECT_NS : EFFECT_AEC);
945 if (ret < 0) {
946 ALOGE("%s Failed to get effect params %d", __func__, ret);
947 return ret;
948 }
949
950 if (module_id == other_effect_config.module_id) {
951 //Same module id for AEC/NS. Values need to be combined
952 if (((effect_type == EFFECT_AEC) && (in->enable_ns)) ||
953 ((effect_type == EFFECT_NS) && (in->enable_aec))) {
954 *param_value |= other_effect_config.param_value;
955 }
956 }
957
958 return ret;
959}
960
961static int enable_disable_effect(struct audio_device *adev, int effect_type, bool enable)
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800962{
Vikram Pandurangadf59cae2017-08-03 18:04:55 -0700963 struct audio_effect_config effect_config;
964 struct audio_usecase *usecase = NULL;
965 int ret = 0;
966 unsigned int param_value = 0;
967 struct stream_in *in = adev->active_input;
968
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800969 if(!voice_extn_is_dynamic_ecns_enabled())
970 return ENOSYS;
971
Vikram Pandurangadf59cae2017-08-03 18:04:55 -0700972 if (!in) {
973 ALOGE("%s: Invalid input stream", __func__);
974 return -EINVAL;
975 }
976
977 ALOGD("%s: effect_type:%d enable:%d", __func__, effect_type, enable);
978
979 usecase = get_usecase_from_list(adev, in->usecase);
Weiyin Jiang20d3fa62018-08-01 18:06:27 +0800980 if (usecase == NULL) {
981 ALOGE("%s: Could not find the usecase (%d) in the list",
982 __func__, in->usecase);
983 return -EINVAL;
984 }
Vikram Pandurangadf59cae2017-08-03 18:04:55 -0700985
986 ret = platform_get_effect_config_data(usecase->in_snd_device, &effect_config, effect_type);
987 if (ret < 0) {
988 ALOGE("%s Failed to get module id %d", __func__, ret);
989 return ret;
990 }
991 ALOGV("%s: %d %d usecase->id:%d usecase->in_snd_device:%d", __func__, effect_config.module_id,
992 in->app_type_cfg.app_type, usecase->id, usecase->in_snd_device);
993
994 if(enable)
995 param_value = effect_config.param_value;
996
997 /*Special handling for AEC & NS effects Param values need to be
998 updated if module ids are same*/
999
1000 if ((effect_type == EFFECT_AEC) || (effect_type == EFFECT_NS)) {
1001 ret = update_effect_param_ecns(adev, effect_config.module_id, effect_type, &param_value);
1002 if (ret < 0)
1003 return ret;
1004 }
1005
1006 ret = send_effect_enable_disable_mixer_ctl(adev, effect_config, param_value);
1007
1008 return ret;
1009}
1010
1011static void check_and_enable_effect(struct audio_device *adev)
1012{
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001013 if(!voice_extn_is_dynamic_ecns_enabled())
1014 return;
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001015
1016 if (adev->active_input->enable_aec) {
1017 enable_disable_effect(adev, EFFECT_AEC, true);
1018 }
1019
1020 if (adev->active_input->enable_ns &&
1021 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
1022 enable_disable_effect(adev, EFFECT_NS, true);
1023 }
1024}
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07001025
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07001026int pcm_ioctl(struct pcm *pcm, int request, ...)
1027{
1028 va_list ap;
1029 void * arg;
1030 int pcm_fd = *(int*)pcm;
1031
1032 va_start(ap, request);
1033 arg = va_arg(ap, void *);
1034 va_end(ap);
1035
1036 return ioctl(pcm_fd, request, arg);
1037}
1038
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -07001039int enable_audio_route(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001040 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001041{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001042 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001043 char mixer_path[MIXER_PATH_MAX_LENGTH];
Manish Dewangan58229382017-02-02 15:48:41 +05301044 struct stream_out *out = NULL;
Soumya Managoli6993b762018-06-28 16:04:57 +05301045 int ret = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001046
1047 if (usecase == NULL)
1048 return -EINVAL;
1049
1050 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
1051
Surendar Karka93cd25a2018-08-28 14:21:37 +05301052 if (usecase->type == PCM_CAPTURE || usecase->type == TRANSCODE_LOOPBACK_TX)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001053 snd_device = usecase->in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001054 else
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001055 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001056
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08001057#ifdef DS1_DOLBY_DAP_ENABLED
1058 audio_extn_dolby_set_dmid(adev);
1059 audio_extn_dolby_set_endpoint(adev);
1060#endif
Pradnya Chaphekar8a9dcd82014-09-09 09:49:10 -07001061 audio_extn_dolby_ds2_set_endpoint(adev);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001062 audio_extn_sound_trigger_update_stream_status(usecase, ST_EVENT_STREAM_BUSY);
Dhananjay Kumar45b71742014-05-29 21:47:27 +05301063 audio_extn_listen_update_stream_status(usecase, LISTEN_EVENT_STREAM_BUSY);
Ben Romberger1fafdde2015-09-09 19:43:15 -07001064 audio_extn_utils_send_app_type_cfg(adev, usecase);
Dhananjay Kumar14170dd2015-08-28 13:24:16 +05301065 audio_extn_utils_send_audio_calibration(adev, usecase);
Manish Dewangan58229382017-02-02 15:48:41 +05301066 if ((usecase->type == PCM_PLAYBACK) && is_offload_usecase(usecase->id)) {
1067 out = usecase->stream.out;
1068 if (out && out->compr)
1069 audio_extn_utils_compress_set_clk_rec_mode(usecase);
1070 }
1071
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -08001072 strlcpy(mixer_path, use_case_table[usecase->id], MIXER_PATH_MAX_LENGTH);
Banajit Goswami20cdd212015-09-11 01:11:30 -07001073 platform_add_backend_name(mixer_path, snd_device, usecase);
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07001074 ALOGD("%s: apply mixer and update path: %s", __func__, mixer_path);
Soumya Managoli6993b762018-06-28 16:04:57 +05301075 ret = audio_route_apply_and_update_path(adev->audio_route, mixer_path);
1076 if (!ret && usecase->id == USECASE_AUDIO_PLAYBACK_FM) {
1077 struct str_parms *parms = str_parms_create_str("fm_restore_volume=1");
1078 if (parms) {
1079 audio_extn_fm_set_parameters(adev, parms);
1080 str_parms_destroy(parms);
1081 }
1082 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001083 ALOGV("%s: exit", __func__);
1084 return 0;
1085}
1086
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001087int disable_audio_route(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001088 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001089{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001090 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001091 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001092
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05301093 if (usecase == NULL || usecase->id == USECASE_INVALID)
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001094 return -EINVAL;
1095
1096 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Surendar Karka93cd25a2018-08-28 14:21:37 +05301097 if (usecase->type == PCM_CAPTURE || usecase->type == TRANSCODE_LOOPBACK_TX)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001098 snd_device = usecase->in_snd_device;
1099 else
1100 snd_device = usecase->out_snd_device;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -08001101 strlcpy(mixer_path, use_case_table[usecase->id], MIXER_PATH_MAX_LENGTH);
Banajit Goswami20cdd212015-09-11 01:11:30 -07001102 platform_add_backend_name(mixer_path, snd_device, usecase);
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07001103 ALOGD("%s: reset and update mixer path: %s", __func__, mixer_path);
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07001104 audio_route_reset_and_update_path(adev->audio_route, mixer_path);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001105 audio_extn_sound_trigger_update_stream_status(usecase, ST_EVENT_STREAM_FREE);
Dhananjay Kumar45b71742014-05-29 21:47:27 +05301106 audio_extn_listen_update_stream_status(usecase, LISTEN_EVENT_STREAM_FREE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001107 ALOGV("%s: exit", __func__);
1108 return 0;
1109}
1110
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -07001111int enable_snd_device(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001112 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001113{
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301114 int i, num_devices = 0;
1115 snd_device_t new_snd_devices[SND_DEVICE_OUT_END];
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001116 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
1117
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001118 if (snd_device < SND_DEVICE_MIN ||
1119 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001120 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001121 return -EINVAL;
1122 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001123
1124 adev->snd_dev_ref_cnt[snd_device]++;
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001125
1126 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0 ) {
1127 ALOGE("%s: Invalid sound device returned", __func__);
1128 return -EINVAL;
1129 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001130 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
Eric Laurent994a6932013-07-17 11:51:42 -07001131 ALOGV("%s: snd_device(%d: %s) is already active",
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001132 __func__, snd_device, device_name);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001133 return 0;
1134 }
1135
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301136
Gopikrishnaiah Anandane85d0462014-06-30 21:41:20 -07001137 if (audio_extn_spkr_prot_is_enabled())
1138 audio_extn_spkr_prot_calib_cancel(adev);
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -07001139
Aalique Grahame22e49102018-12-18 14:23:57 -08001140 audio_extn_dsm_feedback_enable(adev, snd_device, true);
1141
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001142 if (platform_can_enable_spkr_prot_on_device(snd_device) &&
1143 audio_extn_spkr_prot_is_enabled()) {
1144 if (platform_get_spkr_prot_acdb_id(snd_device) < 0) {
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001145 adev->snd_dev_ref_cnt[snd_device]--;
1146 return -EINVAL;
1147 }
Ravit Dennisaaee49c2015-02-04 21:26:22 +02001148 audio_extn_dev_arbi_acquire(snd_device);
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001149 if (audio_extn_spkr_prot_start_processing(snd_device)) {
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -08001150 ALOGE("%s: spkr_start_processing failed", __func__);
Ravit Dennisaaee49c2015-02-04 21:26:22 +02001151 audio_extn_dev_arbi_release(snd_device);
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -08001152 return -EINVAL;
1153 }
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001154 } else if (platform_split_snd_device(adev->platform,
1155 snd_device,
1156 &num_devices,
1157 new_snd_devices) == 0) {
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301158 for (i = 0; i < num_devices; i++) {
1159 enable_snd_device(adev, new_snd_devices[i]);
1160 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001161 platform_set_speaker_gain_in_combo(adev, snd_device, true);
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -08001162 } else {
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07001163 ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, device_name);
Naresh Tannirucd2353e2016-08-19 00:37:25 +05301164
Preetam Singh Ranawatf1d417c2017-01-10 17:00:32 +05301165
Naresh Tannirucd2353e2016-08-19 00:37:25 +05301166 if ((SND_DEVICE_OUT_BT_A2DP == snd_device) &&
1167 (audio_extn_a2dp_start_playback() < 0)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02001168 ALOGE(" fail to configure A2dp Source control path ");
1169 return -EINVAL;
1170 }
1171
1172 if ((SND_DEVICE_IN_BT_A2DP == snd_device) &&
1173 (audio_extn_a2dp_start_capture() < 0)) {
1174 ALOGE(" fail to configure A2dp Sink control path ");
Naresh Tannirucd2353e2016-08-19 00:37:25 +05301175 return -EINVAL;
1176 }
1177
Bharath Ramachandramurthy0de16782014-03-28 21:34:33 -07001178 /* due to the possibility of calibration overwrite between listen
1179 and audio, notify listen hal before audio calibration is sent */
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001180 audio_extn_sound_trigger_update_device_status(snd_device,
1181 ST_EVENT_SND_DEVICE_BUSY);
Dhananjay Kumar45b71742014-05-29 21:47:27 +05301182 audio_extn_listen_update_device_status(snd_device,
1183 LISTEN_EVENT_SND_DEVICE_BUSY);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001184 if (platform_get_snd_device_acdb_id(snd_device) < 0) {
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -07001185 adev->snd_dev_ref_cnt[snd_device]--;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001186 audio_extn_sound_trigger_update_device_status(snd_device,
1187 ST_EVENT_SND_DEVICE_FREE);
Dhananjay Kumar45b71742014-05-29 21:47:27 +05301188 audio_extn_listen_update_device_status(snd_device,
1189 LISTEN_EVENT_SND_DEVICE_FREE);
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -07001190 return -EINVAL;
1191 }
Lior Barenboim0b61bc72014-05-13 13:01:37 +03001192 audio_extn_dev_arbi_acquire(snd_device);
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07001193 audio_route_apply_and_update_path(adev->audio_route, device_name);
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301194
1195 if (SND_DEVICE_OUT_HEADPHONES == snd_device &&
1196 !adev->native_playback_enabled &&
1197 audio_is_true_native_stream_active(adev)) {
1198 ALOGD("%s: %d: napb: enabling native mode in hardware",
1199 __func__, __LINE__);
1200 audio_route_apply_and_update_path(adev->audio_route,
1201 "true-native-mode");
1202 adev->native_playback_enabled = true;
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +05301203 }
Dhanalakshmi Siddani10621622018-04-30 15:07:27 +05301204 if (((snd_device == SND_DEVICE_IN_HANDSET_6MIC) ||
1205 (snd_device == SND_DEVICE_IN_HANDSET_QMIC)) &&
Garmond Leunge2433c32017-09-28 21:51:22 -07001206 (audio_extn_ffv_get_stream() == adev->active_input)) {
1207 ALOGD("%s: init ec ref loopback", __func__);
1208 audio_extn_ffv_init_ec_ref_loopback(adev, snd_device);
1209 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001210 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001211 return 0;
1212}
1213
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001214int disable_snd_device(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001215 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001216{
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301217 int i, num_devices = 0;
1218 snd_device_t new_snd_devices[SND_DEVICE_OUT_END];
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001219 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
1220
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001221 if (snd_device < SND_DEVICE_MIN ||
1222 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001223 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001224 return -EINVAL;
1225 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001226 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
1227 ALOGE("%s: device ref cnt is already 0", __func__);
1228 return -EINVAL;
1229 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001230
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001231 adev->snd_dev_ref_cnt[snd_device]--;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -07001232
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001233 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0) {
1234 ALOGE("%s: Invalid sound device returned", __func__);
1235 return -EINVAL;
1236 }
1237
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001238 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07001239 ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, device_name);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301240
Aalique Grahame22e49102018-12-18 14:23:57 -08001241 audio_extn_dsm_feedback_enable(adev, snd_device, false);
1242
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001243 if (platform_can_enable_spkr_prot_on_device(snd_device) &&
1244 audio_extn_spkr_prot_is_enabled()) {
Anish Kumar46c7b872014-09-09 01:49:44 -07001245 audio_extn_spkr_prot_stop_processing(snd_device);
Vignesh Kulothungan3b5fae52017-09-25 12:16:30 -07001246
1247 // when speaker device is disabled, reset swap.
1248 // will be renabled on usecase start
1249 platform_set_swap_channels(adev, false);
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001250 } else if (platform_split_snd_device(adev->platform,
1251 snd_device,
1252 &num_devices,
1253 new_snd_devices) == 0) {
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301254 for (i = 0; i < num_devices; i++) {
1255 disable_snd_device(adev, new_snd_devices[i]);
1256 }
Aalique Grahame22e49102018-12-18 14:23:57 -08001257 platform_set_speaker_gain_in_combo(adev, snd_device, false);
Lior Barenboim0b61bc72014-05-13 13:01:37 +03001258 } else {
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07001259 audio_route_reset_and_update_path(adev->audio_route, device_name);
Lior Barenboim0b61bc72014-05-13 13:01:37 +03001260 }
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001261
Naresh Tannirucd2353e2016-08-19 00:37:25 +05301262 if (SND_DEVICE_OUT_BT_A2DP == snd_device)
1263 audio_extn_a2dp_stop_playback();
1264
Florian Pfister1a84f312018-07-19 14:38:18 +02001265 if (SND_DEVICE_IN_BT_A2DP == snd_device)
1266 audio_extn_a2dp_stop_capture();
1267
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07001268 if (snd_device == SND_DEVICE_OUT_HDMI || snd_device == SND_DEVICE_OUT_DISPLAY_PORT)
Ashish Jain81eb2a82015-05-13 10:52:34 +05301269 adev->is_channel_status_set = false;
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301270 else if (SND_DEVICE_OUT_HEADPHONES == snd_device &&
1271 adev->native_playback_enabled) {
1272 ALOGD("%s: %d: napb: disabling native mode in hardware",
1273 __func__, __LINE__);
1274 audio_route_reset_and_update_path(adev->audio_route,
1275 "true-native-mode");
1276 adev->native_playback_enabled = false;
Preetam Singh Ranawatb0c0dd72016-08-18 00:32:06 +05301277 } else if (SND_DEVICE_OUT_HEADPHONES == snd_device &&
1278 adev->asrc_mode_enabled) {
1279 ALOGD("%s: %d: disabling asrc mode in hardware", __func__, __LINE__);
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +05301280 disable_asrc_mode(adev);
1281 audio_route_apply_and_update_path(adev->audio_route, "hph-lowpower-mode");
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301282 }
Dhanalakshmi Siddaniaf4bd622019-02-27 16:28:06 +05301283 if (((snd_device == SND_DEVICE_IN_HANDSET_6MIC) ||
1284 (snd_device == SND_DEVICE_IN_HANDSET_QMIC)) &&
Garmond Leunge2433c32017-09-28 21:51:22 -07001285 (audio_extn_ffv_get_stream() == adev->active_input)) {
1286 ALOGD("%s: deinit ec ref loopback", __func__);
1287 audio_extn_ffv_deinit_ec_ref_loopback(adev, snd_device);
1288 }
Ravit Dennisaaee49c2015-02-04 21:26:22 +02001289 audio_extn_dev_arbi_release(snd_device);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001290 audio_extn_sound_trigger_update_device_status(snd_device,
1291 ST_EVENT_SND_DEVICE_FREE);
Dhananjay Kumar45b71742014-05-29 21:47:27 +05301292 audio_extn_listen_update_device_status(snd_device,
Kiran Kandide144c82013-11-20 15:58:32 -08001293 LISTEN_EVENT_SND_DEVICE_FREE);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001294 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001295
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001296 return 0;
1297}
1298
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001299/*
1300 legend:
1301 uc - existing usecase
1302 new_uc - new usecase
1303 d1, d11, d2 - SND_DEVICE enums
1304 a1, a2 - corresponding ANDROID device enums
1305 B1, B2 - backend strings
1306
1307case 1
1308 uc->dev d1 (a1) B1
1309 new_uc->dev d1 (a1), d2 (a2) B1, B2
1310
1311 resolution: disable and enable uc->dev on d1
1312
1313case 2
1314 uc->dev d1 (a1) B1
1315 new_uc->dev d11 (a1) B1
1316
1317 resolution: need to switch uc since d1 and d11 are related
1318 (e.g. speaker and voice-speaker)
1319 use ANDROID_DEVICE_OUT enums to match devices since SND_DEVICE enums may vary
1320
1321case 3
1322 uc->dev d1 (a1) B1
1323 new_uc->dev d2 (a2) B2
1324
1325 resolution: no need to switch uc
1326
1327case 4
1328 uc->dev d1 (a1) B1
1329 new_uc->dev d2 (a2) B1
1330
1331 resolution: disable enable uc-dev on d2 since backends match
1332 we cannot enable two streams on two different devices if they
1333 share the same backend. e.g. if offload is on speaker device using
1334 QUAD_MI2S backend and a low-latency stream is started on voice-handset
1335 using the same backend, offload must also be switched to voice-handset.
1336
1337case 5
1338 uc->dev d1 (a1) B1
1339 new_uc->dev d1 (a1), d2 (a2) B1
1340
1341 resolution: disable enable uc-dev on d2 since backends match
1342 we cannot enable two streams on two different devices if they
1343 share the same backend.
1344
1345case 6
1346 uc->dev d1 (a1) B1
1347 new_uc->dev d2 (a1) B2
1348
1349 resolution: no need to switch
1350
1351case 7
1352 uc->dev d1 (a1), d2 (a2) B1, B2
1353 new_uc->dev d1 (a1) B1
1354
1355 resolution: no need to switch
1356
Zhou Song4ba65882018-07-09 14:48:07 +08001357case 8
1358 uc->dev d1 (a1) B1
1359 new_uc->dev d11 (a1), d2 (a2) B1, B2
1360 resolution: compared to case 1, for this case, d1 and d11 are related
1361 then need to do the same as case 2 to siwtch to new uc
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001362*/
1363static snd_device_t derive_playback_snd_device(void * platform,
1364 struct audio_usecase *uc,
1365 struct audio_usecase *new_uc,
1366 snd_device_t new_snd_device)
1367{
Siddartha Shaik31b530e2017-05-19 15:26:33 +05301368 audio_devices_t a1, a2;
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001369
1370 snd_device_t d1 = uc->out_snd_device;
1371 snd_device_t d2 = new_snd_device;
1372
Siddartha Shaik31b530e2017-05-19 15:26:33 +05301373 switch (uc->type) {
Surendar Karka93cd25a2018-08-28 14:21:37 +05301374 case TRANSCODE_LOOPBACK_RX :
Siddartha Shaik31b530e2017-05-19 15:26:33 +05301375 a1 = uc->stream.inout->out_config.devices;
1376 a2 = new_uc->stream.inout->out_config.devices;
1377 break;
1378 default :
1379 a1 = uc->stream.out->devices;
1380 a2 = new_uc->stream.out->devices;
1381 break;
1382 }
1383
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001384 // Treat as a special case when a1 and a2 are not disjoint
1385 if ((a1 != a2) && (a1 & a2)) {
1386 snd_device_t d3[2];
1387 int num_devices = 0;
1388 int ret = platform_split_snd_device(platform,
1389 popcount(a1) > 1 ? d1 : d2,
1390 &num_devices,
1391 d3);
1392 if (ret < 0) {
1393 if (ret != -ENOSYS) {
1394 ALOGW("%s failed to split snd_device %d",
1395 __func__,
1396 popcount(a1) > 1 ? d1 : d2);
1397 }
1398 goto end;
1399 }
1400
1401 // NB: case 7 is hypothetical and isn't a practical usecase yet.
1402 // But if it does happen, we need to give priority to d2 if
1403 // the combo devices active on the existing usecase share a backend.
1404 // This is because we cannot have a usecase active on a combo device
1405 // and a new usecase requests one device in this combo pair.
1406 if (platform_check_backends_match(d3[0], d3[1])) {
1407 return d2; // case 5
1408 } else {
Garmond Leungb9eeba42018-09-18 11:10:41 -07001409 // check if d1 is related to any of d3's
1410 if (d1 == d3[0] || d1 == d3[1])
Zhou Song4ba65882018-07-09 14:48:07 +08001411 return d1; // case 1
1412 else
1413 return d3[1]; // case 8
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001414 }
1415 } else {
1416 if (platform_check_backends_match(d1, d2)) {
1417 return d2; // case 2, 4
1418 } else {
1419 return d1; // case 6, 3
1420 }
1421 }
1422
1423end:
1424 return d2; // return whatever was calculated before.
1425}
1426
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001427static void check_usecases_codec_backend(struct audio_device *adev,
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301428 struct audio_usecase *uc_info,
1429 snd_device_t snd_device)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001430{
1431 struct listnode *node;
1432 struct audio_usecase *usecase;
1433 bool switch_device[AUDIO_USECASE_MAX];
Chaithanya Krishna Bacharaju49e7db02017-03-14 11:57:26 +05301434 snd_device_t uc_derive_snd_device;
1435 snd_device_t derive_snd_device[AUDIO_USECASE_MAX];
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001436 int i, num_uc_to_switch = 0;
kunleiz5cd52b82016-11-07 17:22:52 +08001437 int status = 0;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301438 bool force_restart_session = false;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001439 /*
1440 * This function is to make sure that all the usecases that are active on
1441 * the hardware codec backend are always routed to any one device that is
1442 * handled by the hardware codec.
1443 * For example, if low-latency and deep-buffer usecases are currently active
1444 * on speaker and out_set_parameters(headset) is received on low-latency
1445 * output, then we have to make sure deep-buffer is also switched to headset,
1446 * because of the limitation that both the devices cannot be enabled
1447 * at the same time as they share the same backend.
1448 */
Mingming Yin3ee55c62014-08-04 14:23:35 -07001449 /*
1450 * This call is to check if we need to force routing for a particular stream
1451 * If there is a backend configuration change for the device when a
1452 * new stream starts, then ADM needs to be closed and re-opened with the new
1453 * configuraion. This call check if we need to re-route all the streams
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -08001454 * associated with the backend. Touch tone + 24 bit + native playback.
Mingming Yin3ee55c62014-08-04 14:23:35 -07001455 */
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -08001456 bool force_routing = platform_check_and_set_codec_backend_cfg(adev, uc_info,
1457 snd_device);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301458 /* For a2dp device reconfigure all active sessions
1459 * with new AFE encoder format based on a2dp state
1460 */
1461 if ((SND_DEVICE_OUT_BT_A2DP == snd_device ||
1462 SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP == snd_device) &&
1463 audio_extn_a2dp_is_force_device_switch()) {
1464 force_routing = true;
1465 force_restart_session = true;
1466 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301467 ALOGD("%s:becf: force routing %d", __func__, force_routing);
1468
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001469 /* Disable all the usecases on the shared backend other than the
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -08001470 * specified usecase.
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -08001471 */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001472 for (i = 0; i < AUDIO_USECASE_MAX; i++)
1473 switch_device[i] = false;
1474
1475 list_for_each(node, &adev->usecase_list) {
1476 usecase = node_to_item(node, struct audio_usecase, list);
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -08001477
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301478 ALOGD("%s:becf: (%d) check_usecases curr device: %s, usecase device:%s "
1479 "backends match %d",__func__, i,
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301480 platform_get_snd_device_name(snd_device),
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301481 platform_get_snd_device_name(usecase->out_snd_device),
1482 platform_check_backends_match(snd_device, usecase->out_snd_device));
Ashish Jain6a65b352017-03-21 17:24:40 +05301483 if ((usecase->type != PCM_CAPTURE) && (usecase != uc_info)) {
1484 uc_derive_snd_device = derive_playback_snd_device(adev->platform,
1485 usecase, uc_info, snd_device);
1486 if (((uc_derive_snd_device != usecase->out_snd_device) || force_routing) &&
1487 ((usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) ||
1488 (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) ||
1489 (usecase->devices & AUDIO_DEVICE_OUT_USB_DEVICE) ||
Garmond Leung2fd4f122018-06-02 16:04:53 -07001490 (usecase->devices & AUDIO_DEVICE_OUT_USB_HEADSET) ||
Ashish Jain6a65b352017-03-21 17:24:40 +05301491 (usecase->devices & AUDIO_DEVICE_OUT_ALL_A2DP) ||
1492 (usecase->devices & AUDIO_DEVICE_OUT_ALL_SCO)) &&
1493 ((force_restart_session) ||
1494 (platform_check_backends_match(snd_device, usecase->out_snd_device)))) {
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301495 ALOGD("%s:becf: check_usecases (%s) is active on (%s) - disabling ..",
1496 __func__, use_case_table[usecase->id],
1497 platform_get_snd_device_name(usecase->out_snd_device));
1498 disable_audio_route(adev, usecase);
1499 switch_device[usecase->id] = true;
Chaithanya Krishna Bacharaju49e7db02017-03-14 11:57:26 +05301500 /* Enable existing usecase on derived playback device */
1501 derive_snd_device[usecase->id] = uc_derive_snd_device;
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301502 num_uc_to_switch++;
Ashish Jain6a65b352017-03-21 17:24:40 +05301503 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001504 }
1505 }
1506
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301507 ALOGD("%s:becf: check_usecases num.of Usecases to switch %d", __func__,
1508 num_uc_to_switch);
1509
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001510 if (num_uc_to_switch) {
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07001511 /* All streams have been de-routed. Disable the device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001512
Venkata Narendra Kumar Gutta7610e632014-04-14 23:16:38 +05301513 /* Make sure the previous devices to be disabled first and then enable the
1514 selected devices */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001515 list_for_each(node, &adev->usecase_list) {
1516 usecase = node_to_item(node, struct audio_usecase, list);
1517 if (switch_device[usecase->id]) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001518 disable_snd_device(adev, usecase->out_snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001519 }
1520 }
1521
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -07001522 list_for_each(node, &adev->usecase_list) {
1523 usecase = node_to_item(node, struct audio_usecase, list);
1524 if (switch_device[usecase->id]) {
Chaithanya Krishna Bacharaju49e7db02017-03-14 11:57:26 +05301525 enable_snd_device(adev, derive_snd_device[usecase->id]);
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -07001526 }
1527 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001528
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001529 /* Re-route all the usecases on the shared backend other than the
1530 specified usecase to new snd devices */
1531 list_for_each(node, &adev->usecase_list) {
1532 usecase = node_to_item(node, struct audio_usecase, list);
Karthik Reddy Katta3abfee22016-02-23 10:55:27 +05301533 /* Update the out_snd_device only before enabling the audio route */
1534 if (switch_device[usecase->id]) {
Chaithanya Krishna Bacharaju49e7db02017-03-14 11:57:26 +05301535 usecase->out_snd_device = derive_snd_device[usecase->id];
Karthik Reddy Katta3abfee22016-02-23 10:55:27 +05301536 if (usecase->type != VOICE_CALL) {
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301537 ALOGD("%s:becf: enabling usecase (%s) on (%s)", __func__,
Karthik Reddy Katta3abfee22016-02-23 10:55:27 +05301538 use_case_table[usecase->id],
1539 platform_get_snd_device_name(usecase->out_snd_device));
kunleiz5cd52b82016-11-07 17:22:52 +08001540 /* Update voc calibration before enabling VoIP route */
1541 if (usecase->type == VOIP_CALL)
1542 status = platform_switch_voice_call_device_post(adev->platform,
1543 usecase->out_snd_device,
kunleizab514ca2017-01-09 14:59:50 +08001544 platform_get_input_snd_device(adev->platform, uc_info->devices));
Avinash Vaish71a8b972014-07-24 15:36:33 +05301545 enable_audio_route(adev, usecase);
kunleiz46c47dd2018-03-19 16:28:09 +08001546 if (usecase->id == USECASE_AUDIO_PLAYBACK_VOIP) {
1547 out_set_voip_volume(&usecase->stream.out->stream,
1548 usecase->stream.out->volume_l,
1549 usecase->stream.out->volume_r);
1550 }
Karthik Reddy Katta3abfee22016-02-23 10:55:27 +05301551 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001552 }
1553 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001554 }
1555}
1556
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05301557static void check_usecases_capture_codec_backend(struct audio_device *adev,
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001558 struct audio_usecase *uc_info,
1559 snd_device_t snd_device)
1560{
1561 struct listnode *node;
1562 struct audio_usecase *usecase;
1563 bool switch_device[AUDIO_USECASE_MAX];
1564 int i, num_uc_to_switch = 0;
Dhanalakshmi Siddanib678a802016-12-03 11:51:41 +05301565 int backend_check_cond = AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND;
kunleiz5cd52b82016-11-07 17:22:52 +08001566 int status = 0;
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001567
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05301568 bool force_routing = platform_check_and_set_capture_codec_backend_cfg(adev, uc_info,
1569 snd_device);
1570 ALOGD("%s:becf: force routing %d", __func__, force_routing);
Dhanalakshmi Siddanib678a802016-12-03 11:51:41 +05301571
1572 /*
1573 * Make sure out devices is checked against out codec backend device and
1574 * also in devices against in codec backend. Checking out device against in
1575 * codec backend or vice versa causes issues.
1576 */
1577 if (uc_info->type == PCM_CAPTURE)
1578 backend_check_cond = AUDIO_DEVICE_IN_ALL_CODEC_BACKEND;
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001579 /*
1580 * This function is to make sure that all the active capture usecases
1581 * are always routed to the same input sound device.
1582 * For example, if audio-record and voice-call usecases are currently
1583 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
1584 * is received for voice call then we have to make sure that audio-record
1585 * usecase is also switched to earpiece i.e. voice-dmic-ef,
1586 * because of the limitation that two devices cannot be enabled
1587 * at the same time if they share the same backend.
1588 */
1589 for (i = 0; i < AUDIO_USECASE_MAX; i++)
1590 switch_device[i] = false;
1591
1592 list_for_each(node, &adev->usecase_list) {
1593 usecase = node_to_item(node, struct audio_usecase, list);
Dhanalakshmi Siddanib678a802016-12-03 11:51:41 +05301594 /*
1595 * TODO: Enhance below condition to handle BT sco/USB multi recording
1596 */
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -08001597 if (usecase->type != PCM_PLAYBACK &&
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001598 usecase != uc_info &&
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05301599 (usecase->in_snd_device != snd_device || force_routing) &&
Dhanalakshmi Siddanib678a802016-12-03 11:51:41 +05301600 ((uc_info->devices & backend_check_cond) &&
Karthik Reddy Katta0d471762015-08-20 13:36:07 +05301601 (((usecase->devices & ~AUDIO_DEVICE_BIT_IN) & AUDIO_DEVICE_IN_ALL_CODEC_BACKEND) ||
Satya Krishna Pindiproli458b5a72017-03-13 15:14:24 +05301602 (usecase->type == VOIP_CALL))) &&
Anish Kumara020a7c2014-10-17 11:13:22 -07001603 (usecase->id != USECASE_AUDIO_SPKR_CALIB_TX)) {
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001604 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
1605 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -07001606 platform_get_snd_device_name(usecase->in_snd_device));
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001607 disable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001608 switch_device[usecase->id] = true;
1609 num_uc_to_switch++;
1610 }
1611 }
1612
1613 if (num_uc_to_switch) {
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07001614 /* All streams have been de-routed. Disable the device */
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001615
Venkata Narendra Kumar Gutta7610e632014-04-14 23:16:38 +05301616 /* Make sure the previous devices to be disabled first and then enable the
1617 selected devices */
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001618 list_for_each(node, &adev->usecase_list) {
1619 usecase = node_to_item(node, struct audio_usecase, list);
1620 if (switch_device[usecase->id]) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001621 disable_snd_device(adev, usecase->in_snd_device);
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -08001622 }
1623 }
1624
1625 list_for_each(node, &adev->usecase_list) {
1626 usecase = node_to_item(node, struct audio_usecase, list);
1627 if (switch_device[usecase->id]) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001628 enable_snd_device(adev, snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001629 }
1630 }
1631
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001632 /* Re-route all the usecases on the shared backend other than the
1633 specified usecase to new snd devices */
1634 list_for_each(node, &adev->usecase_list) {
1635 usecase = node_to_item(node, struct audio_usecase, list);
1636 /* Update the in_snd_device only before enabling the audio route */
1637 if (switch_device[usecase->id] ) {
1638 usecase->in_snd_device = snd_device;
kunleiz5cd52b82016-11-07 17:22:52 +08001639 if (usecase->type != VOICE_CALL) {
1640 /* Update voc calibration before enabling VoIP route */
1641 if (usecase->type == VOIP_CALL)
1642 status = platform_switch_voice_call_device_post(adev->platform,
Zhou Song557e7282017-05-05 17:18:18 +08001643 platform_get_output_snd_device(adev->platform, uc_info->stream.out),
kunleiz5cd52b82016-11-07 17:22:52 +08001644 usecase->in_snd_device);
Avinash Vaish71a8b972014-07-24 15:36:33 +05301645 enable_audio_route(adev, usecase);
kunleiz5cd52b82016-11-07 17:22:52 +08001646 }
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001647 }
1648 }
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001649 }
1650}
1651
Mingming Yin3a941d42016-02-17 18:08:05 -08001652static void reset_hdmi_sink_caps(struct stream_out *out) {
1653 int i = 0;
1654
1655 for (i = 0; i<= MAX_SUPPORTED_CHANNEL_MASKS; i++) {
1656 out->supported_channel_masks[i] = 0;
1657 }
1658 for (i = 0; i<= MAX_SUPPORTED_FORMATS; i++) {
1659 out->supported_formats[i] = 0;
1660 }
1661 for (i = 0; i<= MAX_SUPPORTED_SAMPLE_RATES; i++) {
1662 out->supported_sample_rates[i] = 0;
1663 }
1664}
1665
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001666/* must be called with hw device mutex locked */
Mingming Yin3a941d42016-02-17 18:08:05 -08001667static int read_hdmi_sink_caps(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001668{
Mingming Yin3a941d42016-02-17 18:08:05 -08001669 int ret = 0, i = 0, j = 0;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07001670 int channels = platform_edid_get_max_channels(out->dev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001671
Mingming Yin3a941d42016-02-17 18:08:05 -08001672 reset_hdmi_sink_caps(out);
1673
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07001674 /* Cache ext disp type */
Garmond Leung37850ab2016-10-06 11:42:18 -07001675 if (platform_get_ext_disp_type(adev->platform) <= 0) {
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07001676 ALOGE("%s: Failed to query disp type, ret:%d", __func__, ret);
Garmond Leung37850ab2016-10-06 11:42:18 -07001677 return -EINVAL;
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07001678 }
1679
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001680 switch (channels) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001681 case 8:
Mingming Yin3a941d42016-02-17 18:08:05 -08001682 ALOGV("%s: HDMI supports 7.1 channels", __func__);
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -07001683 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_7POINT1;
Mingming Yin3a941d42016-02-17 18:08:05 -08001684 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_6POINT1;
1685 case 6:
1686 ALOGV("%s: HDMI supports 5.1 channels", __func__);
1687 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_5POINT1;
1688 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_PENTA;
1689 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_QUAD;
1690 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_SURROUND;
1691 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_2POINT1;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001692 break;
1693 default:
Mingming Yin3a941d42016-02-17 18:08:05 -08001694 ALOGE("invalid/nonstandard channal count[%d]",channels);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001695 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001696 break;
1697 }
Mingming Yin3a941d42016-02-17 18:08:05 -08001698
1699 // check channel format caps
1700 i = 0;
1701 if (platform_is_edid_supported_format(out->dev->platform, AUDIO_FORMAT_AC3)) {
1702 ALOGV(":%s HDMI supports AC3/EAC3 formats", __func__);
1703 out->supported_formats[i++] = AUDIO_FORMAT_AC3;
1704 //Adding EAC3/EAC3_JOC formats if AC3 is supported by the sink.
1705 //EAC3/EAC3_JOC will be converted to AC3 for decoding if needed
1706 out->supported_formats[i++] = AUDIO_FORMAT_E_AC3;
1707 out->supported_formats[i++] = AUDIO_FORMAT_E_AC3_JOC;
1708 }
1709
Ben Romberger1aaaf862017-04-06 17:49:46 -07001710 if (platform_is_edid_supported_format(out->dev->platform, AUDIO_FORMAT_DOLBY_TRUEHD)) {
1711 ALOGV(":%s HDMI supports TRUE HD format", __func__);
1712 out->supported_formats[i++] = AUDIO_FORMAT_DOLBY_TRUEHD;
1713 }
1714
Mingming Yin3a941d42016-02-17 18:08:05 -08001715 if (platform_is_edid_supported_format(out->dev->platform, AUDIO_FORMAT_DTS)) {
1716 ALOGV(":%s HDMI supports DTS format", __func__);
1717 out->supported_formats[i++] = AUDIO_FORMAT_DTS;
1718 }
1719
1720 if (platform_is_edid_supported_format(out->dev->platform, AUDIO_FORMAT_DTS_HD)) {
1721 ALOGV(":%s HDMI supports DTS HD format", __func__);
1722 out->supported_formats[i++] = AUDIO_FORMAT_DTS_HD;
1723 }
1724
Naresh Tanniru928f0862017-04-07 16:44:23 -07001725 if (platform_is_edid_supported_format(out->dev->platform, AUDIO_FORMAT_IEC61937)) {
1726 ALOGV(":%s HDMI supports IEC61937 format", __func__);
1727 out->supported_formats[i++] = AUDIO_FORMAT_IEC61937;
1728 }
1729
Mingming Yin3a941d42016-02-17 18:08:05 -08001730
1731 // check sample rate caps
1732 i = 0;
1733 for (j = 0; j < MAX_SUPPORTED_SAMPLE_RATES; j++) {
1734 if (platform_is_edid_supported_sample_rate(out->dev->platform, out_hdmi_sample_rates[j])) {
1735 ALOGV(":%s HDMI supports sample rate:%d", __func__, out_hdmi_sample_rates[j]);
1736 out->supported_sample_rates[i++] = out_hdmi_sample_rates[j];
1737 }
1738 }
1739
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001740 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001741}
1742
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001743static inline ssize_t read_usb_sup_sample_rates(bool is_playback __unused,
1744 uint32_t *supported_sample_rates __unused,
1745 uint32_t max_rates __unused)
1746{
1747 ssize_t count = audio_extn_usb_get_sup_sample_rates(is_playback,
1748 supported_sample_rates,
1749 max_rates);
Ashish Jain4847e9d2017-08-17 19:16:57 +05301750 ssize_t i = 0;
1751
1752 for (i=0; i<count; i++) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001753 ALOGV("%s %s %d", __func__, is_playback ? "P" : "C",
1754 supported_sample_rates[i]);
1755 }
1756 return count;
1757}
1758
1759static inline int read_usb_sup_channel_masks(bool is_playback,
1760 audio_channel_mask_t *supported_channel_masks,
1761 uint32_t max_masks)
1762{
1763 int channels = audio_extn_usb_get_max_channels(is_playback);
1764 int channel_count;
1765 uint32_t num_masks = 0;
1766 if (channels > MAX_HIFI_CHANNEL_COUNT)
1767 channels = MAX_HIFI_CHANNEL_COUNT;
1768
Lakshman Chaluvarajub79fafd2018-11-21 10:24:37 +05301769 channel_count = DEFAULT_CHANNEL_COUNT;
1770
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001771 if (is_playback) {
1772 // For playback we never report mono because the framework always outputs stereo
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001773 // audio_channel_out_mask_from_count() does return positional masks for channel counts
Lakshman Chaluvarajub79fafd2018-11-21 10:24:37 +05301774 // above 2 but we want indexed masks here.
1775 supported_channel_masks[num_masks++] = audio_channel_out_mask_from_count(channel_count);
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001776 } else {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001777 // audio_channel_in_mask_from_count() does the right conversion to either positional or
1778 // indexed mask
Lakshman Chaluvarajub79fafd2018-11-21 10:24:37 +05301779 supported_channel_masks[num_masks++] = audio_channel_in_mask_from_count(channel_count);
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001780 }
Lakshman Chaluvarajub79fafd2018-11-21 10:24:37 +05301781
1782 for (channel_count = channels; ((channel_count >= DEFAULT_CHANNEL_COUNT) &&
1783 (num_masks < max_masks)); channel_count--) {
1784 supported_channel_masks[num_masks++] =
1785 audio_channel_mask_for_index_assignment_from_count(channel_count);
1786 }
1787
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001788 ALOGV("%s: %s supported ch %d supported_channel_masks[0] %08x num_masks %d", __func__,
1789 is_playback ? "P" : "C", channels, supported_channel_masks[0], num_masks);
1790 return num_masks;
1791}
1792
1793static inline int read_usb_sup_formats(bool is_playback __unused,
1794 audio_format_t *supported_formats,
1795 uint32_t max_formats __unused)
1796{
1797 int bitwidth = audio_extn_usb_get_max_bit_width(is_playback);
1798 switch (bitwidth) {
1799 case 24:
1800 // XXX : usb.c returns 24 for s24 and s24_le?
1801 supported_formats[0] = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1802 break;
1803 case 32:
1804 supported_formats[0] = AUDIO_FORMAT_PCM_32_BIT;
1805 break;
1806 case 16:
1807 default :
1808 supported_formats[0] = AUDIO_FORMAT_PCM_16_BIT;
1809 break;
1810 }
1811 ALOGV("%s: %s supported format %d", __func__,
1812 is_playback ? "P" : "C", bitwidth);
1813 return 1;
1814}
1815
1816static inline int read_usb_sup_params_and_compare(bool is_playback,
1817 audio_format_t *format,
1818 audio_format_t *supported_formats,
1819 uint32_t max_formats,
1820 audio_channel_mask_t *mask,
1821 audio_channel_mask_t *supported_channel_masks,
1822 uint32_t max_masks,
1823 uint32_t *rate,
1824 uint32_t *supported_sample_rates,
1825 uint32_t max_rates) {
1826 int ret = 0;
1827 int num_formats;
1828 int num_masks;
1829 int num_rates;
1830 int i;
1831
1832 num_formats = read_usb_sup_formats(is_playback, supported_formats,
1833 max_formats);
1834 num_masks = read_usb_sup_channel_masks(is_playback, supported_channel_masks,
1835 max_masks);
1836
1837 num_rates = read_usb_sup_sample_rates(is_playback,
1838 supported_sample_rates, max_rates);
1839
1840#define LUT(table, len, what, dflt) \
1841 for (i=0; i<len && (table[i] != what); i++); \
1842 if (i==len) { ret |= (what == dflt ? 0 : -1); what=table[0]; }
1843
1844 LUT(supported_formats, num_formats, *format, AUDIO_FORMAT_DEFAULT);
1845 LUT(supported_channel_masks, num_masks, *mask, AUDIO_CHANNEL_NONE);
1846 LUT(supported_sample_rates, num_rates, *rate, 0);
1847
1848#undef LUT
1849 return ret < 0 ? -EINVAL : 0; // HACK TBD
1850}
1851
Alexy Josephb1379942016-01-29 15:49:38 -08001852audio_usecase_t get_usecase_id_from_usecase_type(const struct audio_device *adev,
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -08001853 usecase_type_t type)
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001854{
1855 struct audio_usecase *usecase;
1856 struct listnode *node;
1857
1858 list_for_each(node, &adev->usecase_list) {
1859 usecase = node_to_item(node, struct audio_usecase, list);
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -08001860 if (usecase->type == type) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001861 ALOGV("%s: usecase id %d", __func__, usecase->id);
1862 return usecase->id;
1863 }
1864 }
1865 return USECASE_INVALID;
1866}
1867
Alexy Josephb1379942016-01-29 15:49:38 -08001868struct audio_usecase *get_usecase_from_list(const struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001869 audio_usecase_t uc_id)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001870{
1871 struct audio_usecase *usecase;
1872 struct listnode *node;
1873
1874 list_for_each(node, &adev->usecase_list) {
1875 usecase = node_to_item(node, struct audio_usecase, list);
1876 if (usecase->id == uc_id)
1877 return usecase;
1878 }
1879 return NULL;
1880}
1881
Dhananjay Kumard4833242016-10-06 22:09:12 +05301882struct stream_in *get_next_active_input(const struct audio_device *adev)
1883{
1884 struct audio_usecase *usecase;
1885 struct listnode *node;
1886
1887 list_for_each_reverse(node, &adev->usecase_list) {
1888 usecase = node_to_item(node, struct audio_usecase, list);
1889 if (usecase->type == PCM_CAPTURE)
1890 return usecase->stream.in;
1891 }
1892 return NULL;
1893}
1894
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301895/*
1896 * is a true native playback active
1897 */
1898bool audio_is_true_native_stream_active(struct audio_device *adev)
1899{
1900 bool active = false;
1901 int i = 0;
1902 struct listnode *node;
1903
1904 if (NATIVE_AUDIO_MODE_TRUE_44_1 != platform_get_native_support()) {
1905 ALOGV("%s:napb: not in true mode or non hdphones device",
1906 __func__);
1907 active = false;
1908 goto exit;
1909 }
1910
1911 list_for_each(node, &adev->usecase_list) {
1912 struct audio_usecase *uc;
1913 uc = node_to_item(node, struct audio_usecase, list);
1914 struct stream_out *curr_out =
1915 (struct stream_out*) uc->stream.out;
1916
1917 if (curr_out && PCM_PLAYBACK == uc->type) {
1918 ALOGD("%s:napb: (%d) (%s)id (%d) sr %d bw "
1919 "(%d) device %s", __func__, i++, use_case_table[uc->id],
1920 uc->id, curr_out->sample_rate,
1921 curr_out->bit_width,
1922 platform_get_snd_device_name(uc->out_snd_device));
1923
1924 if (is_offload_usecase(uc->id) &&
1925 (curr_out->sample_rate == OUTPUT_SAMPLING_RATE_44100)) {
1926 active = true;
1927 ALOGD("%s:napb:native stream detected", __func__);
1928 }
1929 }
1930 }
1931exit:
1932 return active;
1933}
1934
Xiaojun Sang785b5da2017-08-03 15:52:29 +08001935uint32_t adev_get_dsp_bit_width_enforce_mode()
1936{
1937 if (adev == NULL) {
1938 ALOGE("%s: adev is null. Disable DSP bit width enforce mode.\n", __func__);
1939 return 0;
1940 }
1941 return adev->dsp_bit_width_enforce_mode;
1942}
1943
1944static uint32_t adev_init_dsp_bit_width_enforce_mode(struct mixer *mixer)
1945{
1946 char value[PROPERTY_VALUE_MAX];
1947 int trial;
1948 uint32_t dsp_bit_width_enforce_mode = 0;
1949
1950 if (!mixer) {
1951 ALOGE("%s: adev mixer is null. cannot update DSP bitwidth.\n",
1952 __func__);
1953 return 0;
1954 }
1955
1956 if (property_get("persist.vendor.audio_hal.dsp_bit_width_enforce_mode",
1957 value, NULL) > 0) {
1958 trial = atoi(value);
1959 switch (trial) {
1960 case 16:
1961 dsp_bit_width_enforce_mode = 16;
1962 break;
1963 case 24:
1964 dsp_bit_width_enforce_mode = 24;
1965 break;
1966 case 32:
1967 dsp_bit_width_enforce_mode = 32;
1968 break;
1969 default:
1970 dsp_bit_width_enforce_mode = 0;
1971 ALOGD("%s Dynamic DSP bitwidth config is disabled.", __func__);
1972 break;
1973 }
1974 }
1975
1976 return dsp_bit_width_enforce_mode;
1977}
1978
1979static void audio_enable_asm_bit_width_enforce_mode(struct mixer *mixer,
1980 uint32_t enforce_mode,
1981 bool enable)
1982{
1983 struct mixer_ctl *ctl = NULL;
1984 const char *mixer_ctl_name = "ASM Bit Width";
1985 uint32_t asm_bit_width_mode = 0;
1986
1987 if (enforce_mode == 0) {
1988 ALOGD("%s: DSP bitwidth feature is disabled.", __func__);
1989 return;
1990 }
1991
1992 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
1993 if (!ctl) {
1994 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1995 __func__, mixer_ctl_name);
1996 return;
1997 }
1998
1999 if (enable)
2000 asm_bit_width_mode = enforce_mode;
2001 else
2002 asm_bit_width_mode = 0;
2003
2004 ALOGV("%s DSP bit width feature status is %d width=%d",
2005 __func__, enable, asm_bit_width_mode);
2006 if (mixer_ctl_set_value(ctl, 0, asm_bit_width_mode) < 0)
2007 ALOGE("%s: Could not set ASM biwidth %d", __func__,
2008 asm_bit_width_mode);
2009
2010 return;
2011}
2012
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05302013/*
2014 * if native DSD playback active
2015 */
2016bool audio_is_dsd_native_stream_active(struct audio_device *adev)
2017{
2018 bool active = false;
2019 struct listnode *node = NULL;
2020 struct audio_usecase *uc = NULL;
2021 struct stream_out *curr_out = NULL;
2022
2023 list_for_each(node, &adev->usecase_list) {
2024 uc = node_to_item(node, struct audio_usecase, list);
2025 curr_out = (struct stream_out*) uc->stream.out;
2026
2027 if (curr_out && PCM_PLAYBACK == uc->type &&
2028 (DSD_NATIVE_BACKEND == platform_get_backend_index(uc->out_snd_device))) {
2029 active = true;
2030 ALOGV("%s:DSD playback is active", __func__);
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05302031 break;
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05302032 }
2033 }
2034 return active;
2035}
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302036
2037static bool force_device_switch(struct audio_usecase *usecase)
2038{
2039 bool ret = false;
2040 bool is_it_true_mode = false;
2041
Zhou Song30f2c3e2018-02-08 14:02:15 +08002042 if (usecase->type == PCM_CAPTURE ||
Surendar Karka93cd25a2018-08-28 14:21:37 +05302043 usecase->type == TRANSCODE_LOOPBACK_RX ||
2044 usecase->type == TRANSCODE_LOOPBACK_TX) {
Zhou Song30f2c3e2018-02-08 14:02:15 +08002045 return false;
2046 }
2047
Aalique Grahamecbc46a22017-10-05 10:30:23 -07002048 if(usecase->stream.out == NULL) {
2049 ALOGE("%s: stream.out is NULL", __func__);
2050 return false;
2051 }
2052
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302053 if (is_offload_usecase(usecase->id) &&
Xiaojun Sang869f2012016-02-23 16:33:07 +08002054 (usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) &&
2055 (usecase->stream.out->devices == AUDIO_DEVICE_OUT_WIRED_HEADSET ||
2056 usecase->stream.out->devices == AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) {
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302057 is_it_true_mode = (NATIVE_AUDIO_MODE_TRUE_44_1 == platform_get_native_support()? true : false);
2058 if ((is_it_true_mode && !adev->native_playback_enabled) ||
2059 (!is_it_true_mode && adev->native_playback_enabled)){
2060 ret = true;
2061 ALOGD("napb: time to toggle native mode");
2062 }
2063 }
2064
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302065 // Force all a2dp output devices to reconfigure for proper AFE encode format
Ashish Jainc597d102016-12-12 10:31:34 +05302066 //Also handle a case where in earlier a2dp start failed as A2DP stream was
2067 //in suspended state, hence try to trigger a retry when we again get a routing request.
Aalique Grahamecbc46a22017-10-05 10:30:23 -07002068 if((usecase->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) &&
Ashish Jainc597d102016-12-12 10:31:34 +05302069 audio_extn_a2dp_is_force_device_switch()) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302070 ALOGD("Force a2dp device switch to update new encoder config");
2071 ret = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002072 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302073
Florian Pfister1a84f312018-07-19 14:38:18 +02002074 if (usecase->stream.out->stream_config_changed) {
Manish Dewangan671a4202017-08-18 17:30:46 +05302075 ALOGD("Force stream_config_changed to update iec61937 transmission config");
2076 return true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002077 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302078 return ret;
2079}
2080
Aalique Grahame22e49102018-12-18 14:23:57 -08002081static void stream_app_type_cfg_init(struct stream_app_type_cfg *cfg)
2082{
2083 cfg->gain[0] = cfg->gain[1] = APP_TYPE_GAIN_DEFAULT;
2084}
2085
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302086bool is_btsco_device(snd_device_t out_snd_device, snd_device_t in_snd_device)
2087{
2088 bool ret=false;
2089 if ((out_snd_device == SND_DEVICE_OUT_BT_SCO ||
2090 out_snd_device == SND_DEVICE_OUT_BT_SCO_WB) ||
2091 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_WB_NREC ||
2092 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_WB ||
2093 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_NREC ||
2094 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC)
2095 ret = true;
2096
2097 return ret;
2098}
2099
2100bool is_a2dp_device(snd_device_t out_snd_device)
2101{
2102 bool ret=false;
2103 if (out_snd_device == SND_DEVICE_OUT_BT_A2DP)
2104 ret = true;
2105
2106 return ret;
2107}
2108
2109bool is_bt_soc_on(struct audio_device *adev)
2110{
2111 struct mixer_ctl *ctl;
2112 char *mixer_ctl_name = "BT SOC status";
2113 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
2114 bool bt_soc_status = true;
2115 if (!ctl) {
2116 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2117 __func__, mixer_ctl_name);
2118 /*This is to ensure we dont break targets which dont have the kernel change*/
2119 return true;
2120 }
2121 bt_soc_status = mixer_ctl_get_value(ctl, 0);
2122 ALOGD("BT SOC status: %d",bt_soc_status);
2123 return bt_soc_status;
2124}
2125
2126int out_standby_l(struct audio_stream *stream);
2127
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002128int select_devices(struct audio_device *adev, audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002129{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002130 snd_device_t out_snd_device = SND_DEVICE_NONE;
2131 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002132 struct audio_usecase *usecase = NULL;
2133 struct audio_usecase *vc_usecase = NULL;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002134 struct audio_usecase *voip_usecase = NULL;
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -08002135 struct audio_usecase *hfp_usecase = NULL;
Siddartha Shaik44dd7702017-06-14 12:13:25 +05302136 struct stream_out stream_out;
Vimal Puthanveed41fcff22014-01-23 15:56:53 -08002137 audio_usecase_t hfp_ucid;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002138 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002139
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302140 ALOGD("%s for use case (%s)", __func__, use_case_table[uc_id]);
2141
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002142 usecase = get_usecase_from_list(adev, uc_id);
2143 if (usecase == NULL) {
2144 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
2145 return -EINVAL;
2146 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002147
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002148 if ((usecase->type == VOICE_CALL) ||
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -08002149 (usecase->type == VOIP_CALL) ||
2150 (usecase->type == PCM_HFP_CALL)) {
Aditya Bavanaribdda2f22016-10-19 15:02:05 +05302151 if(usecase->stream.out == NULL) {
2152 ALOGE("%s: stream.out is NULL", __func__);
2153 return -EINVAL;
2154 }
Eric Laurentb23d5282013-05-14 15:27:20 -07002155 out_snd_device = platform_get_output_snd_device(adev->platform,
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -08002156 usecase->stream.out);
Eric Laurentb23d5282013-05-14 15:27:20 -07002157 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002158 usecase->devices = usecase->stream.out->devices;
Surendar Karka93cd25a2018-08-28 14:21:37 +05302159 } else if (usecase->type == TRANSCODE_LOOPBACK_RX) {
Siddartha Shaik31b530e2017-05-19 15:26:33 +05302160 if (usecase->stream.inout == NULL) {
2161 ALOGE("%s: stream.inout is NULL", __func__);
2162 return -EINVAL;
2163 }
Siddartha Shaik44dd7702017-06-14 12:13:25 +05302164 stream_out.devices = usecase->stream.inout->out_config.devices;
2165 stream_out.sample_rate = usecase->stream.inout->out_config.sample_rate;
2166 stream_out.format = usecase->stream.inout->out_config.format;
2167 stream_out.channel_mask = usecase->stream.inout->out_config.channel_mask;
2168 out_snd_device = platform_get_output_snd_device(adev->platform,
2169 &stream_out);
Surendar Karka93cd25a2018-08-28 14:21:37 +05302170 usecase->devices = out_snd_device;
2171 } else if (usecase->type == TRANSCODE_LOOPBACK_TX ) {
2172 if (usecase->stream.inout == NULL) {
2173 ALOGE("%s: stream.inout is NULL", __func__);
2174 return -EINVAL;
2175 }
Siddartha Shaik44dd7702017-06-14 12:13:25 +05302176 in_snd_device = platform_get_input_snd_device(adev->platform, AUDIO_DEVICE_NONE);
Surendar Karka93cd25a2018-08-28 14:21:37 +05302177 usecase->devices = in_snd_device;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002178 } else {
2179 /*
2180 * If the voice call is active, use the sound devices of voice call usecase
2181 * so that it would not result any device switch. All the usecases will
2182 * be switched to new device when select_devices() is called for voice call
2183 * usecase. This is to avoid switching devices for voice call when
2184 * check_usecases_codec_backend() is called below.
Alexy Joseph79dfa3c2016-04-20 18:44:56 -07002185 * choose voice call device only if the use case device is
2186 * also using the codec backend
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002187 */
Shiv Maliyappanahallibb4cf0b2016-01-21 11:30:06 -08002188 if (voice_is_in_call(adev) && adev->mode != AUDIO_MODE_NORMAL) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002189 vc_usecase = get_usecase_from_list(adev,
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -08002190 get_usecase_id_from_usecase_type(adev, VOICE_CALL));
Alexy Josephdbcddf22016-05-16 17:54:09 -07002191 if ((vc_usecase) && (((vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) &&
2192 (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)) ||
Preetam Singh Ranawat097cb1f2016-07-19 13:49:04 +05302193 ((vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) &&
2194 (usecase->devices & AUDIO_DEVICE_IN_ALL_CODEC_BACKEND)) ||
Alexy Josephdbcddf22016-05-16 17:54:09 -07002195 (usecase->devices == AUDIO_DEVICE_IN_VOICE_CALL))) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002196 in_snd_device = vc_usecase->in_snd_device;
2197 out_snd_device = vc_usecase->out_snd_device;
2198 }
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002199 } else if (voice_extn_compress_voip_is_active(adev)) {
yidongh02ef86f2017-04-21 15:36:04 +08002200 bool out_snd_device_backend_match = true;
yidongh47785a82017-05-08 19:29:29 +08002201 voip_usecase = get_usecase_from_list(adev, USECASE_COMPRESS_VOIP_CALL);
yidongh6261d8e2017-05-15 17:04:02 +08002202 if ((voip_usecase != NULL) &&
2203 (usecase->type == PCM_PLAYBACK) &&
2204 (usecase->stream.out != NULL)) {
yidongh02ef86f2017-04-21 15:36:04 +08002205 out_snd_device_backend_match = platform_check_backends_match(
2206 voip_usecase->out_snd_device,
2207 platform_get_output_snd_device(
2208 adev->platform,
2209 usecase->stream.out));
2210 }
Mingming Yin2d8aa2e2014-08-14 00:00:51 -07002211 if ((voip_usecase) && ((voip_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) &&
Zhou Song0154bf12016-08-04 10:48:16 +08002212 ((usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) ||
2213 ((usecase->devices & ~AUDIO_DEVICE_BIT_IN) & AUDIO_DEVICE_IN_ALL_CODEC_BACKEND)) &&
yidongh02ef86f2017-04-21 15:36:04 +08002214 out_snd_device_backend_match &&
Mingming Yin2d8aa2e2014-08-14 00:00:51 -07002215 (voip_usecase->stream.out != adev->primary_output))) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002216 in_snd_device = voip_usecase->in_snd_device;
2217 out_snd_device = voip_usecase->out_snd_device;
2218 }
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -08002219 } else if (audio_extn_hfp_is_active(adev)) {
Vimal Puthanveed41fcff22014-01-23 15:56:53 -08002220 hfp_ucid = audio_extn_hfp_get_usecase();
2221 hfp_usecase = get_usecase_from_list(adev, hfp_ucid);
Mingming Yin2d8aa2e2014-08-14 00:00:51 -07002222 if ((hfp_usecase) && (hfp_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)) {
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -08002223 in_snd_device = hfp_usecase->in_snd_device;
2224 out_snd_device = hfp_usecase->out_snd_device;
2225 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002226 }
2227 if (usecase->type == PCM_PLAYBACK) {
Aditya Bavanaribdda2f22016-10-19 15:02:05 +05302228 if (usecase->stream.out == NULL) {
2229 ALOGE("%s: stream.out is NULL", __func__);
2230 return -EINVAL;
2231 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002232 usecase->devices = usecase->stream.out->devices;
2233 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07002234 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurentb23d5282013-05-14 15:27:20 -07002235 out_snd_device = platform_get_output_snd_device(adev->platform,
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -08002236 usecase->stream.out);
kunleizdcf967a2018-08-07 17:09:11 +08002237 voip_usecase = get_usecase_from_list(adev, USECASE_AUDIO_PLAYBACK_VOIP);
kunleizb27e7952018-10-12 15:49:35 +08002238 if (voip_usecase == NULL && adev->primary_output && !adev->primary_output->standby)
kunleizdcf967a2018-08-07 17:09:11 +08002239 voip_usecase = get_usecase_from_list(adev, adev->primary_output->usecase);
2240
2241 if ((usecase->stream.out != NULL &&
2242 voip_usecase != NULL &&
2243 usecase->stream.out->usecase == voip_usecase->id) &&
2244 adev->active_input &&
Aalique Grahame22e49102018-12-18 14:23:57 -08002245 (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
2246 adev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
kunleizdcf967a2018-08-07 17:09:11 +08002247 out_snd_device != usecase->out_snd_device) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07002248 select_devices(adev, adev->active_input->usecase);
2249 }
2250 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002251 } else if (usecase->type == PCM_CAPTURE) {
Aditya Bavanaribdda2f22016-10-19 15:02:05 +05302252 if (usecase->stream.in == NULL) {
2253 ALOGE("%s: stream.in is NULL", __func__);
2254 return -EINVAL;
2255 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002256 usecase->devices = usecase->stream.in->device;
2257 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07002258 if (in_snd_device == SND_DEVICE_NONE) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07002259 audio_devices_t out_device = AUDIO_DEVICE_NONE;
Leena Winterrowd3c8fbae2016-01-12 15:05:53 -08002260 if (adev->active_input &&
2261 (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
Karthik Reddy Kattaf621ec72014-11-21 16:37:38 +05302262 (adev->mode == AUDIO_MODE_IN_COMMUNICATION &&
kunleizdcf967a2018-08-07 17:09:11 +08002263 adev->active_input->source == AUDIO_SOURCE_MIC))) {
2264 voip_usecase = get_usecase_from_list(adev, USECASE_AUDIO_PLAYBACK_VOIP);
2265 if (voip_usecase != NULL && voip_usecase->stream.out != NULL)
2266 out_device = voip_usecase->stream.out->devices;
2267 else if (adev->primary_output && !adev->primary_output->standby)
2268 out_device = adev->primary_output->devices;
Apoorv Raghuvanshi924b3022015-07-06 15:07:14 -07002269 platform_set_echo_reference(adev, false, AUDIO_DEVICE_NONE);
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07002270 } else if (usecase->id == USECASE_AUDIO_RECORD_AFE_PROXY) {
2271 out_device = AUDIO_DEVICE_OUT_TELEPHONY_TX;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07002272 }
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07002273 in_snd_device = platform_get_input_snd_device(adev->platform, out_device);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07002274 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002275 }
2276 }
2277
2278 if (out_snd_device == usecase->out_snd_device &&
2279 in_snd_device == usecase->in_snd_device) {
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302280
2281 if (!force_device_switch(usecase))
2282 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002283 }
2284
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302285 if ((is_btsco_device(out_snd_device,in_snd_device) && !adev->bt_sco_on) ||
Florian Pfister1a84f312018-07-19 14:38:18 +02002286 (is_a2dp_device(out_snd_device) && !audio_extn_a2dp_source_is_ready())) {
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302287 ALOGD("SCO/A2DP is selected but they are not connected/ready hence dont route");
2288 return 0;
2289 }
2290
Aalique Grahame22e49102018-12-18 14:23:57 -08002291 if (out_snd_device != SND_DEVICE_NONE &&
2292 out_snd_device != adev->last_logged_snd_device[uc_id][0]) {
2293 ALOGD("%s: changing use case %s output device from(%d: %s, acdb %d) to (%d: %s, acdb %d)",
2294 __func__,
2295 use_case_table[uc_id],
2296 adev->last_logged_snd_device[uc_id][0],
2297 platform_get_snd_device_name(adev->last_logged_snd_device[uc_id][0]),
2298 adev->last_logged_snd_device[uc_id][0] != SND_DEVICE_NONE ?
2299 platform_get_snd_device_acdb_id(adev->last_logged_snd_device[uc_id][0]) :
2300 -1,
2301 out_snd_device,
2302 platform_get_snd_device_name(out_snd_device),
2303 platform_get_snd_device_acdb_id(out_snd_device));
2304 adev->last_logged_snd_device[uc_id][0] = out_snd_device;
2305 }
2306 if (in_snd_device != SND_DEVICE_NONE &&
2307 in_snd_device != adev->last_logged_snd_device[uc_id][1]) {
2308 ALOGD("%s: changing use case %s input device from(%d: %s, acdb %d) to (%d: %s, acdb %d)",
2309 __func__,
2310 use_case_table[uc_id],
2311 adev->last_logged_snd_device[uc_id][1],
2312 platform_get_snd_device_name(adev->last_logged_snd_device[uc_id][1]),
2313 adev->last_logged_snd_device[uc_id][1] != SND_DEVICE_NONE ?
2314 platform_get_snd_device_acdb_id(adev->last_logged_snd_device[uc_id][1]) :
2315 -1,
2316 in_snd_device,
2317 platform_get_snd_device_name(in_snd_device),
2318 platform_get_snd_device_acdb_id(in_snd_device));
2319 adev->last_logged_snd_device[uc_id][1] = in_snd_device;
2320 }
2321
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002322
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002323 /*
2324 * Limitation: While in call, to do a device switch we need to disable
2325 * and enable both RX and TX devices though one of them is same as current
2326 * device.
2327 */
Vidyakumar Athota21b3bb92014-04-25 11:08:08 -07002328 if ((usecase->type == VOICE_CALL) &&
2329 (usecase->in_snd_device != SND_DEVICE_NONE) &&
2330 (usecase->out_snd_device != SND_DEVICE_NONE)) {
Eric Laurentb23d5282013-05-14 15:27:20 -07002331 status = platform_switch_voice_call_device_pre(adev->platform);
Narsinga Rao Chella116142b2015-08-14 18:00:08 -07002332 }
2333
2334 if (((usecase->type == VOICE_CALL) ||
2335 (usecase->type == VOIP_CALL)) &&
2336 (usecase->out_snd_device != SND_DEVICE_NONE)) {
2337 /* Disable sidetone only if voice/voip call already exists */
2338 if (voice_is_call_state_active(adev) ||
2339 voice_extn_compress_voip_is_started(adev))
Bhalchandra Gajare45fee282015-06-09 22:23:45 -07002340 voice_set_sidetone(adev, usecase->out_snd_device, false);
Vidyakumar Athotaea269c62016-10-31 09:05:59 -07002341
2342 /* Disable aanc only if voice call exists */
2343 if (voice_is_call_state_active(adev))
2344 voice_check_and_update_aanc_path(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002345 }
2346
Aalique Grahame22e49102018-12-18 14:23:57 -08002347 if ((out_snd_device == SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP ||
2348 out_snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP) &&
Florian Pfister1a84f312018-07-19 14:38:18 +02002349 (!audio_extn_a2dp_source_is_ready())) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302350 ALOGW("%s: A2DP profile is not ready, routing to speaker only", __func__);
Aalique Grahame22e49102018-12-18 14:23:57 -08002351 if (out_snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP)
2352 out_snd_device = SND_DEVICE_OUT_SPEAKER_SAFE;
2353 else
2354 out_snd_device = SND_DEVICE_OUT_SPEAKER;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302355 }
2356
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002357 /* Disable current sound devices */
2358 if (usecase->out_snd_device != SND_DEVICE_NONE) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07002359 disable_audio_route(adev, usecase);
2360 disable_snd_device(adev, usecase->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002361 }
2362
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002363 if (usecase->in_snd_device != SND_DEVICE_NONE) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07002364 disable_audio_route(adev, usecase);
2365 disable_snd_device(adev, usecase->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002366 }
2367
Vidyakumar Athota545dbd32013-11-13 17:30:53 -08002368 /* Applicable only on the targets that has external modem.
2369 * New device information should be sent to modem before enabling
2370 * the devices to reduce in-call device switch time.
2371 */
Vidyakumar Athota21b3bb92014-04-25 11:08:08 -07002372 if ((usecase->type == VOICE_CALL) &&
2373 (usecase->in_snd_device != SND_DEVICE_NONE) &&
2374 (usecase->out_snd_device != SND_DEVICE_NONE)) {
Vidyakumar Athota545dbd32013-11-13 17:30:53 -08002375 status = platform_switch_voice_call_enable_device_config(adev->platform,
2376 out_snd_device,
2377 in_snd_device);
Vidyakumar Athota21b3bb92014-04-25 11:08:08 -07002378 }
Vidyakumar Athota545dbd32013-11-13 17:30:53 -08002379
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002380 /* Enable new sound devices */
2381 if (out_snd_device != SND_DEVICE_NONE) {
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08002382 check_usecases_codec_backend(adev, usecase, out_snd_device);
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +05302383 if (platform_check_codec_asrc_support(adev->platform))
2384 check_and_set_asrc_mode(adev, usecase, out_snd_device);
Haynes Mathew George1376ca62014-04-24 11:55:48 -07002385 enable_snd_device(adev, out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002386 }
2387
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002388 if (in_snd_device != SND_DEVICE_NONE) {
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05302389 check_usecases_capture_codec_backend(adev, usecase, in_snd_device);
Haynes Mathew George1376ca62014-04-24 11:55:48 -07002390 enable_snd_device(adev, in_snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002391 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002392
Avinash Vaish71a8b972014-07-24 15:36:33 +05302393 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) {
Eric Laurentb23d5282013-05-14 15:27:20 -07002394 status = platform_switch_voice_call_device_post(adev->platform,
2395 out_snd_device,
2396 in_snd_device);
Avinash Vaish71a8b972014-07-24 15:36:33 +05302397 enable_audio_route_for_voice_usecases(adev, usecase);
2398 }
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002399
sangwoo170731f2013-06-08 15:36:36 +09002400 usecase->in_snd_device = in_snd_device;
2401 usecase->out_snd_device = out_snd_device;
2402
Dhananjay Kumard6d32152016-10-13 16:11:03 +05302403 audio_extn_utils_update_stream_app_type_cfg_for_usecase(adev,
2404 usecase);
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +05302405 if (usecase->type == PCM_PLAYBACK) {
Weiyin Jiang6f4c8062016-11-23 15:30:29 +08002406 if ((24 == usecase->stream.out->bit_width) &&
2407 (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER)) {
2408 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
2409 } else if ((out_snd_device == SND_DEVICE_OUT_HDMI ||
2410 out_snd_device == SND_DEVICE_OUT_USB_HEADSET ||
2411 out_snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
2412 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
2413 /*
2414 * To best utlize DSP, check if the stream sample rate is supported/multiple of
2415 * configured device sample rate, if not update the COPP rate to be equal to the
2416 * device sample rate, else open COPP at stream sample rate
2417 */
2418 platform_check_and_update_copp_sample_rate(adev->platform, out_snd_device,
2419 usecase->stream.out->sample_rate,
2420 &usecase->stream.out->app_type_cfg.sample_rate);
Ashish Jain4826f6c2017-02-06 13:33:20 +05302421 } else if (((out_snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
2422 !audio_is_true_native_stream_active(adev)) &&
Weiyin Jiang6f4c8062016-11-23 15:30:29 +08002423 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
2424 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
2425 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
2426 }
2427
Weiyin Jiangfa65d3e2019-03-05 23:39:45 +08002428 /* Notify device change info to effect clients registered */
2429 audio_extn_gef_notify_device_config(
2430 usecase->stream.out->devices,
2431 usecase->stream.out->channel_mask,
2432 usecase->stream.out->app_type_cfg.sample_rate,
2433 platform_get_snd_device_acdb_id(usecase->out_snd_device));
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +05302434 }
Haynes Mathew George1376ca62014-04-24 11:55:48 -07002435 enable_audio_route(adev, usecase);
sangwoo170731f2013-06-08 15:36:36 +09002436
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002437 audio_extn_qdsp_set_device(usecase);
Aalique Grahame22e49102018-12-18 14:23:57 -08002438
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07002439 /* If input stream is already running then effect needs to be
2440 applied on the new input device that's being enabled here. */
Vidyakumar Athotabbfa5f72018-07-05 18:04:48 -07002441 if ((in_snd_device != SND_DEVICE_NONE) && (adev->active_input != NULL) &&
2442 (!adev->active_input->standby))
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07002443 check_and_enable_effect(adev);
2444
Vidyakumar Athota493f2892016-08-14 11:56:55 -07002445 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) {
Vidyakumar Athotaea269c62016-10-31 09:05:59 -07002446 /* Enable aanc only if voice call exists */
2447 if (voice_is_call_state_active(adev))
2448 voice_check_and_update_aanc_path(adev, out_snd_device, true);
2449
Vidyakumar Athota493f2892016-08-14 11:56:55 -07002450 /* Enable sidetone only if other voice/voip call already exists */
2451 if (voice_is_call_state_active(adev) ||
2452 voice_extn_compress_voip_is_started(adev))
2453 voice_set_sidetone(adev, out_snd_device, true);
2454 }
2455
Vidyakumar Athota1fd21792013-11-15 14:50:57 -08002456 /* Applicable only on the targets that has external modem.
2457 * Enable device command should be sent to modem only after
2458 * enabling voice call mixer controls
2459 */
Vidyakumar Athota339342f2014-07-01 15:30:57 -07002460 if (usecase->type == VOICE_CALL)
Vidyakumar Athota1fd21792013-11-15 14:50:57 -08002461 status = platform_switch_voice_call_usecase_route_post(adev->platform,
2462 out_snd_device,
2463 in_snd_device);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302464
2465 if (is_btsco_device(out_snd_device, in_snd_device) || is_a2dp_device(out_snd_device)) {
2466
2467 if (usecase->type == VOIP_CALL) {
2468 if (adev->active_input != NULL &&
2469 !adev->active_input->standby) {
2470 if (is_bt_soc_on(adev) == false){
2471 ALOGD("BT SCO MIC disconnected while in connection");
2472 if (adev->active_input->pcm != NULL)
2473 pcm_stop(adev->active_input->pcm);
2474 }
2475 }
2476 if ((usecase->stream.out != NULL) && (usecase->stream.out != adev->primary_output)
2477 && usecase->stream.out->started) {
2478 if (is_bt_soc_on(adev) == false) {
2479 ALOGD("BT SCO/A2DP disconnected while in connection");
2480 out_standby_l(&usecase->stream.out->stream.common);
2481 }
2482 }
2483 } else if ((usecase->stream.out != NULL) &&
2484 !(usecase->stream.out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
Surendar Karkae1dc8742018-11-19 16:23:14 +05302485 (usecase->type != TRANSCODE_LOOPBACK_TX) &&
2486 (usecase->type != TRANSCODE_LOOPBACK_RX) &&
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302487 usecase->stream.out->started) {
2488 if (is_bt_soc_on(adev) == false) {
2489 ALOGD("BT SCO/A2dp disconnected while in connection");
2490 out_standby_l(&usecase->stream.out->stream.common);
2491 }
2492 }
2493 }
2494
Aalique Grahame22e49102018-12-18 14:23:57 -08002495 if (usecase == voip_usecase) {
2496 struct stream_out *voip_out = voip_usecase->stream.out;
2497 audio_extn_utils_send_app_type_gain(adev,
2498 voip_out->app_type_cfg.app_type,
2499 &voip_out->app_type_cfg.gain[0]);
2500 }
2501
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302502 ALOGD("%s: done",__func__);
2503
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002504 return status;
2505}
2506
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002507static int stop_input_stream(struct stream_in *in)
2508{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05302509 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002510 struct audio_usecase *uc_info;
Pallavid7c7a272018-01-16 11:22:55 +05302511
2512 if (in == NULL) {
2513 ALOGE("%s: stream_in ptr is NULL", __func__);
2514 return -EINVAL;
2515 }
2516
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002517 struct audio_device *adev = in->dev;
2518
Eric Laurent994a6932013-07-17 11:51:42 -07002519 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002520 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002521 uc_info = get_usecase_from_list(adev, in->usecase);
2522 if (uc_info == NULL) {
2523 ALOGE("%s: Could not find the usecase (%d) in the list",
2524 __func__, in->usecase);
2525 return -EINVAL;
2526 }
2527
Derek Chenea197282019-01-07 17:35:01 -08002528 if (audio_extn_ext_hw_plugin_usecase_stop(adev->ext_hw_plugin, uc_info))
2529 ALOGE("%s: failed to stop ext hw plugin", __func__);
Derek Chend2530072014-11-24 12:39:14 -08002530
Vidyakumar Athota2850d532013-11-19 16:02:12 -08002531 /* Close in-call recording streams */
2532 voice_check_and_stop_incall_rec_usecase(adev, in);
2533
Eric Laurent150dbfe2013-02-27 14:31:02 -08002534 /* 1. Disable stream specific mixer controls */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07002535 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002536
2537 /* 2. Disable the tx device */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07002538 disable_snd_device(adev, uc_info->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002539
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002540 list_remove(&uc_info->list);
2541 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002542
Aalique Grahame2e9b2e42016-12-07 12:43:48 -08002543 adev->active_input = get_next_active_input(adev);
Vatsal Buchac09ae062018-11-14 13:25:08 +05302544 enable_gcov();
Eric Laurent994a6932013-07-17 11:51:42 -07002545 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002546 return ret;
2547}
2548
2549int start_input_stream(struct stream_in *in)
2550{
2551 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -08002552 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002553 struct audio_usecase *uc_info;
Preetam Singh Ranawata87e9742018-02-13 16:52:53 +05302554
2555 if (in == NULL) {
2556 ALOGE("%s: stream_in ptr is NULL", __func__);
2557 return -EINVAL;
2558 }
2559
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002560 struct audio_device *adev = in->dev;
Garmond Leunge2433c32017-09-28 21:51:22 -07002561 struct pcm_config config = in->config;
Garmond Leung438932f2017-10-04 19:35:18 -07002562 int usecase = platform_update_usecase_from_source(in->source,in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002563
Mingming Yin2664a5b2015-09-03 10:53:11 -07002564 if (get_usecase_from_list(adev, usecase) == NULL)
2565 in->usecase = usecase;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302566 ALOGD("%s: enter: stream(%p)usecase(%d: %s)",
2567 __func__, &in->stream, in->usecase, use_case_table[in->usecase]);
Shiv Maliyappanahallida107642013-10-17 11:16:13 -07002568
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05302569 if (CARD_STATUS_OFFLINE == in->card_status||
2570 CARD_STATUS_OFFLINE == adev->card_status) {
2571 ALOGW("in->card_status or adev->card_status offline, try again");
Dhanalakshmi Siddani4d57e992014-07-17 16:37:51 +05302572 ret = -EIO;
Naresh Tanniru4c630392014-05-12 01:05:52 +05302573 goto error_config;
2574 }
Naresh Tanniru4c630392014-05-12 01:05:52 +05302575
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302576 if (audio_is_bluetooth_sco_device(in->device)) {
2577 if (!adev->bt_sco_on) {
2578 ALOGE("%s: SCO profile is not ready, return error", __func__);
2579 ret = -EIO;
2580 goto error_config;
2581 }
2582 }
2583
Shiv Maliyappanahallida107642013-10-17 11:16:13 -07002584 /* Check if source matches incall recording usecase criteria */
2585 ret = voice_check_and_set_incall_rec_usecase(adev, in);
2586 if (ret)
2587 goto error_config;
2588 else
Mingming Yin2664a5b2015-09-03 10:53:11 -07002589 ALOGV("%s: usecase(%d)", __func__, in->usecase);
2590
2591 if (get_usecase_from_list(adev, in->usecase) != NULL) {
2592 ALOGE("%s: use case assigned already in use, stream(%p)usecase(%d: %s)",
2593 __func__, &in->stream, in->usecase, use_case_table[in->usecase]);
Zhou Song4e0704d2016-04-20 13:00:14 +08002594 return -EINVAL;
Mingming Yin2664a5b2015-09-03 10:53:11 -07002595 }
Shiv Maliyappanahallida107642013-10-17 11:16:13 -07002596
Eric Laurentb23d5282013-05-14 15:27:20 -07002597 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002598 if (in->pcm_device_id < 0) {
2599 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
2600 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -08002601 ret = -EINVAL;
2602 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002603 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002604
2605 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002606 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07002607
2608 if (!uc_info) {
2609 ret = -ENOMEM;
2610 goto error_config;
2611 }
2612
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002613 uc_info->id = in->usecase;
2614 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002615 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002616 uc_info->devices = in->device;
2617 uc_info->in_snd_device = SND_DEVICE_NONE;
2618 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002619
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002620 list_add_tail(&adev->usecase_list, &uc_info->list);
Sudheer Papothifa9d2282015-09-17 01:53:25 +05302621 audio_extn_perf_lock_acquire(&adev->perf_lock_handle, 0,
2622 adev->perf_lock_opts,
2623 adev->perf_lock_opts_size);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002624 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002625
Derek Chenea197282019-01-07 17:35:01 -08002626 if (audio_extn_ext_hw_plugin_usecase_start(adev->ext_hw_plugin, uc_info))
2627 ALOGE("%s: failed to start ext hw plugin", __func__);
Derek Chend2530072014-11-24 12:39:14 -08002628
Haynes Mathew George16081042017-05-31 17:16:49 -07002629 if (audio_extn_cin_attached_usecase(in->usecase)) {
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05302630 ret = audio_extn_cin_start_input_stream(in);
2631 if (ret)
2632 goto error_open;
2633 else
2634 goto done_open;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07002635 }
2636
Haynes Mathew George16081042017-05-31 17:16:49 -07002637 if (in->usecase == USECASE_AUDIO_RECORD_MMAP) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07002638 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) {
Haynes Mathew George16081042017-05-31 17:16:49 -07002639 ALOGE("%s: pcm stream not ready", __func__);
2640 goto error_open;
2641 }
2642 ret = pcm_start(in->pcm);
2643 if (ret < 0) {
2644 ALOGE("%s: MMAP pcm_start failed ret %d", __func__, ret);
2645 goto error_open;
2646 }
2647 } else {
2648 unsigned int flags = PCM_IN | PCM_MONOTONIC;
2649 unsigned int pcm_open_retry_count = 0;
2650
2651 if (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY) {
2652 flags |= PCM_MMAP | PCM_NOIRQ;
2653 pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT;
2654 } else if (in->realtime) {
2655 flags |= PCM_MMAP | PCM_NOIRQ;
2656 }
2657
Garmond Leunge2433c32017-09-28 21:51:22 -07002658 if (audio_extn_ffv_get_stream() == in) {
2659 ALOGD("%s: ffv stream, update pcm config", __func__);
2660 audio_extn_ffv_update_pcm_config(&config);
2661 }
Haynes Mathew George16081042017-05-31 17:16:49 -07002662 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
2663 __func__, adev->snd_card, in->pcm_device_id, in->config.channels);
2664
2665 while (1) {
Haynes Mathew George380745d2017-10-04 15:27:45 -07002666 ATRACE_BEGIN("pcm_in_open");
Haynes Mathew George16081042017-05-31 17:16:49 -07002667 in->pcm = pcm_open(adev->snd_card, in->pcm_device_id,
Garmond Leung438932f2017-10-04 19:35:18 -07002668 flags, &config);
Haynes Mathew George380745d2017-10-04 15:27:45 -07002669 ATRACE_END();
Satish Babu Patakokila54ce83d2018-07-06 18:00:37 +05302670 if (errno == ENETRESET && !pcm_is_ready(in->pcm)) {
Sharad Sanglec6f32552018-05-04 16:15:38 +05302671 ALOGE("%s: pcm_open failed errno:%d\n", __func__, errno);
2672 adev->card_status = CARD_STATUS_OFFLINE;
2673 in->card_status = CARD_STATUS_OFFLINE;
2674 ret = -EIO;
2675 goto error_open;
2676 }
2677
Haynes Mathew George16081042017-05-31 17:16:49 -07002678 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) {
2679 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
2680 if (in->pcm != NULL) {
2681 pcm_close(in->pcm);
2682 in->pcm = NULL;
2683 }
2684 if (pcm_open_retry_count-- == 0) {
2685 ret = -EIO;
2686 goto error_open;
2687 }
2688 usleep(PROXY_OPEN_WAIT_TIME * 1000);
2689 continue;
2690 }
2691 break;
2692 }
2693
2694 ALOGV("%s: pcm_prepare", __func__);
Haynes Mathew George380745d2017-10-04 15:27:45 -07002695 ATRACE_BEGIN("pcm_in_prepare");
Haynes Mathew George16081042017-05-31 17:16:49 -07002696 ret = pcm_prepare(in->pcm);
Haynes Mathew George380745d2017-10-04 15:27:45 -07002697 ATRACE_END();
Haynes Mathew George16081042017-05-31 17:16:49 -07002698 if (ret < 0) {
2699 ALOGE("%s: pcm_prepare returned %d", __func__, ret);
2700 pcm_close(in->pcm);
2701 in->pcm = NULL;
2702 goto error_open;
2703 }
2704 register_in_stream(in);
2705 if (in->realtime) {
Haynes Mathew George380745d2017-10-04 15:27:45 -07002706 ATRACE_BEGIN("pcm_in_start");
Haynes Mathew George16081042017-05-31 17:16:49 -07002707 ret = pcm_start(in->pcm);
Haynes Mathew George380745d2017-10-04 15:27:45 -07002708 ATRACE_END();
Haynes Mathew George16081042017-05-31 17:16:49 -07002709 if (ret < 0) {
2710 ALOGE("%s: RT pcm_start failed ret %d", __func__, ret);
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07002711 pcm_close(in->pcm);
2712 in->pcm = NULL;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07002713 goto error_open;
2714 }
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07002715 }
Haynes Mathew George5beddd42016-06-27 18:33:40 -07002716 }
2717
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07002718 check_and_enable_effect(adev);
2719
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05302720done_open:
Sudheer Papothifa9d2282015-09-17 01:53:25 +05302721 audio_extn_perf_lock_release(&adev->perf_lock_handle);
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07002722 ALOGD("%s: exit", __func__);
Vatsal Buchac09ae062018-11-14 13:25:08 +05302723 enable_gcov();
Eric Laurentc8400632013-02-14 19:04:54 -08002724 return ret;
2725
2726error_open:
Sudheer Papothifa9d2282015-09-17 01:53:25 +05302727 audio_extn_perf_lock_release(&adev->perf_lock_handle);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002728 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -08002729error_config:
Dhananjay Kumard4833242016-10-06 22:09:12 +05302730 adev->active_input = get_next_active_input(adev);
Laxminath Kasam2cb4b752015-09-24 03:59:15 +05302731 /*
2732 * sleep 50ms to allow sufficient time for kernel
2733 * drivers to recover incases like SSR.
2734 */
2735 usleep(50000);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002736 ALOGD("%s: exit: status(%d)", __func__, ret);
Vatsal Buchac09ae062018-11-14 13:25:08 +05302737 enable_gcov();
Eric Laurentc8400632013-02-14 19:04:54 -08002738 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002739}
2740
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07002741void lock_input_stream(struct stream_in *in)
2742{
2743 pthread_mutex_lock(&in->pre_lock);
2744 pthread_mutex_lock(&in->lock);
2745 pthread_mutex_unlock(&in->pre_lock);
2746}
2747
2748void lock_output_stream(struct stream_out *out)
2749{
2750 pthread_mutex_lock(&out->pre_lock);
2751 pthread_mutex_lock(&out->lock);
2752 pthread_mutex_unlock(&out->pre_lock);
2753}
2754
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002755/* must be called with out->lock locked */
2756static int send_offload_cmd_l(struct stream_out* out, int command)
2757{
2758 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
2759
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07002760 if (!cmd) {
2761 ALOGE("failed to allocate mem for command 0x%x", command);
2762 return -ENOMEM;
2763 }
2764
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002765 ALOGVV("%s %d", __func__, command);
2766
2767 cmd->cmd = command;
2768 list_add_tail(&out->offload_cmd_list, &cmd->node);
2769 pthread_cond_signal(&out->offload_cond);
2770 return 0;
2771}
2772
2773/* must be called iwth out->lock locked */
2774static void stop_compressed_output_l(struct stream_out *out)
2775{
2776 out->offload_state = OFFLOAD_STATE_IDLE;
2777 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002778 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002779 if (out->compr != NULL) {
2780 compress_stop(out->compr);
2781 while (out->offload_thread_blocked) {
2782 pthread_cond_wait(&out->cond, &out->lock);
2783 }
2784 }
2785}
2786
Varun Balaraje49253e2017-07-06 19:48:56 +05302787bool is_interactive_usecase(audio_usecase_t uc_id)
2788{
2789 unsigned int i;
2790 for (i = 0; i < sizeof(interactive_usecases)/sizeof(interactive_usecases[0]); i++) {
2791 if (uc_id == interactive_usecases[i])
2792 return true;
2793 }
2794 return false;
2795}
2796
2797static audio_usecase_t get_interactive_usecase(struct audio_device *adev)
2798{
2799 audio_usecase_t ret_uc = USECASE_INVALID;
2800 unsigned int intract_uc_index;
2801 unsigned int num_usecase = sizeof(interactive_usecases)/sizeof(interactive_usecases[0]);
2802
2803 ALOGV("%s: num_usecase: %d", __func__, num_usecase);
2804 for (intract_uc_index = 0; intract_uc_index < num_usecase; intract_uc_index++) {
2805 if (!(adev->interactive_usecase_state & (0x1 << intract_uc_index))) {
2806 adev->interactive_usecase_state |= 0x1 << intract_uc_index;
2807 ret_uc = interactive_usecases[intract_uc_index];
2808 break;
2809 }
2810 }
2811
2812 ALOGV("%s: Interactive usecase is %d", __func__, ret_uc);
2813 return ret_uc;
2814}
2815
2816static void free_interactive_usecase(struct audio_device *adev,
2817 audio_usecase_t uc_id)
2818{
2819 unsigned int interact_uc_index;
2820 unsigned int num_usecase = sizeof(interactive_usecases)/sizeof(interactive_usecases[0]);
2821
2822 for (interact_uc_index = 0; interact_uc_index < num_usecase; interact_uc_index++) {
2823 if (interactive_usecases[interact_uc_index] == uc_id) {
2824 adev->interactive_usecase_state &= ~(0x1 << interact_uc_index);
2825 break;
2826 }
2827 }
2828 ALOGV("%s: free Interactive usecase %d", __func__, uc_id);
2829}
2830
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002831bool is_offload_usecase(audio_usecase_t uc_id)
2832{
2833 unsigned int i;
2834 for (i = 0; i < sizeof(offload_usecases)/sizeof(offload_usecases[0]); i++) {
2835 if (uc_id == offload_usecases[i])
2836 return true;
2837 }
2838 return false;
2839}
2840
Dhananjay Kumarac341582017-02-23 23:42:25 +05302841static audio_usecase_t get_offload_usecase(struct audio_device *adev, bool is_compress)
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002842{
vivek mehta446c3962015-09-14 10:57:35 -07002843 audio_usecase_t ret_uc = USECASE_INVALID;
2844 unsigned int offload_uc_index;
Alexy Josephb1379942016-01-29 15:49:38 -08002845 unsigned int num_usecase = sizeof(offload_usecases)/sizeof(offload_usecases[0]);
vivek mehta446c3962015-09-14 10:57:35 -07002846 if (!adev->multi_offload_enable) {
Dhananjay Kumarac341582017-02-23 23:42:25 +05302847 if (!is_compress)
vivek mehta446c3962015-09-14 10:57:35 -07002848 ret_uc = USECASE_AUDIO_PLAYBACK_OFFLOAD2;
2849 else
2850 ret_uc = USECASE_AUDIO_PLAYBACK_OFFLOAD;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002851
vivek mehta446c3962015-09-14 10:57:35 -07002852 pthread_mutex_lock(&adev->lock);
2853 if (get_usecase_from_list(adev, ret_uc) != NULL)
2854 ret_uc = USECASE_INVALID;
2855 pthread_mutex_unlock(&adev->lock);
2856
2857 return ret_uc;
2858 }
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002859
2860 ALOGV("%s: num_usecase: %d", __func__, num_usecase);
vivek mehta446c3962015-09-14 10:57:35 -07002861 for (offload_uc_index = 0; offload_uc_index < num_usecase; offload_uc_index++) {
2862 if (!(adev->offload_usecases_state & (0x1 << offload_uc_index))) {
2863 adev->offload_usecases_state |= 0x1 << offload_uc_index;
2864 ret_uc = offload_usecases[offload_uc_index];
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002865 break;
2866 }
2867 }
vivek mehta446c3962015-09-14 10:57:35 -07002868
2869 ALOGV("%s: offload usecase is %d", __func__, ret_uc);
2870 return ret_uc;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002871}
2872
2873static void free_offload_usecase(struct audio_device *adev,
2874 audio_usecase_t uc_id)
2875{
vivek mehta446c3962015-09-14 10:57:35 -07002876 unsigned int offload_uc_index;
Alexy Josephb1379942016-01-29 15:49:38 -08002877 unsigned int num_usecase = sizeof(offload_usecases)/sizeof(offload_usecases[0]);
vivek mehta446c3962015-09-14 10:57:35 -07002878
2879 if (!adev->multi_offload_enable)
2880 return;
2881
2882 for (offload_uc_index = 0; offload_uc_index < num_usecase; offload_uc_index++) {
2883 if (offload_usecases[offload_uc_index] == uc_id) {
2884 adev->offload_usecases_state &= ~(0x1 << offload_uc_index);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002885 break;
2886 }
2887 }
2888 ALOGV("%s: free offload usecase %d", __func__, uc_id);
2889}
2890
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002891static void *offload_thread_loop(void *context)
2892{
2893 struct stream_out *out = (struct stream_out *) context;
2894 struct listnode *item;
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08002895 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002896
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002897 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002898 //set_sched_policy(0, SP_FOREGROUND);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002899 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
2900
2901 ALOGV("%s", __func__);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07002902 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002903 for (;;) {
2904 struct offload_cmd *cmd = NULL;
2905 stream_callback_event_t event;
2906 bool send_callback = false;
2907
2908 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
2909 __func__, list_empty(&out->offload_cmd_list),
2910 out->offload_state);
2911 if (list_empty(&out->offload_cmd_list)) {
2912 ALOGV("%s SLEEPING", __func__);
2913 pthread_cond_wait(&out->offload_cond, &out->lock);
2914 ALOGV("%s RUNNING", __func__);
2915 continue;
2916 }
2917
2918 item = list_head(&out->offload_cmd_list);
2919 cmd = node_to_item(item, struct offload_cmd, node);
2920 list_remove(item);
2921
2922 ALOGVV("%s STATE %d CMD %d out->compr %p",
2923 __func__, out->offload_state, cmd->cmd, out->compr);
2924
2925 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
2926 free(cmd);
2927 break;
2928 }
2929
Haynes Mathew Georgeee5836f2017-11-21 18:02:10 -08002930 // allow OFFLOAD_CMD_ERROR reporting during standby
2931 // this is needed to handle failures during compress_open
2932 // Note however that on a pause timeout, the stream is closed
2933 // and no offload usecase will be active. Therefore this
2934 // special case is needed for compress_open failures alone
2935 if (cmd->cmd != OFFLOAD_CMD_ERROR &&
2936 out->compr == NULL) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002937 ALOGE("%s: Compress handle is NULL", __func__);
Haynes Mathew Georgea9abb202016-06-02 14:13:20 -07002938 free(cmd);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002939 pthread_cond_signal(&out->cond);
2940 continue;
2941 }
2942 out->offload_thread_blocked = true;
2943 pthread_mutex_unlock(&out->lock);
2944 send_callback = false;
2945 switch(cmd->cmd) {
2946 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07002947 ALOGD("copl(%p):calling compress_wait", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002948 compress_wait(out->compr, -1);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07002949 ALOGD("copl(%p):out of compress_wait", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002950 send_callback = true;
2951 event = STREAM_CBK_EVENT_WRITE_READY;
2952 break;
2953 case OFFLOAD_CMD_PARTIAL_DRAIN:
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08002954 ret = compress_next_track(out->compr);
Sidipotu Ashok55820562014-02-10 16:16:38 +05302955 if(ret == 0) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07002956 ALOGD("copl(%p):calling compress_partial_drain", out);
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05302957 ret = compress_partial_drain(out->compr);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07002958 ALOGD("copl(%p):out of compress_partial_drain", out);
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05302959 if (ret < 0)
2960 ret = -errno;
Sidipotu Ashok55820562014-02-10 16:16:38 +05302961 }
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05302962 else if (ret == -ETIMEDOUT)
Aniket Kumar Lata3570fb12017-11-08 15:53:44 -08002963 ret = compress_drain(out->compr);
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08002964 else
2965 ALOGE("%s: Next track returned error %d",__func__, ret);
Aniket Kumar Lata3570fb12017-11-08 15:53:44 -08002966 if (-ENETRESET != ret && !(-EINTR == ret &&
2967 CARD_STATUS_OFFLINE == out->card_status)) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05302968 send_callback = true;
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05302969 pthread_mutex_lock(&out->lock);
2970 out->send_new_metadata = 1;
2971 out->send_next_track_params = true;
2972 pthread_mutex_unlock(&out->lock);
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05302973 event = STREAM_CBK_EVENT_DRAIN_READY;
2974 ALOGV("copl(%p):send drain callback, ret %d", out, ret);
2975 } else
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05302976 ALOGI("%s: Block drain ready event during SSR", __func__);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002977 break;
2978 case OFFLOAD_CMD_DRAIN:
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07002979 ALOGD("copl(%p):calling compress_drain", out);
Aniket Kumar Lata3570fb12017-11-08 15:53:44 -08002980 ret = compress_drain(out->compr);
2981 ALOGD("copl(%p):out of compress_drain", out);
2982 // EINTR check avoids drain interruption due to SSR
2983 if (-ENETRESET != ret && !(-EINTR == ret &&
2984 CARD_STATUS_OFFLINE == out->card_status)) {
2985 send_callback = true;
2986 event = STREAM_CBK_EVENT_DRAIN_READY;
2987 } else
2988 ALOGI("%s: Block drain ready event during SSR", __func__);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002989 break;
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05302990 case OFFLOAD_CMD_ERROR:
2991 ALOGD("copl(%p): sending error callback to AF", out);
2992 send_callback = true;
2993 event = STREAM_CBK_EVENT_ERROR;
2994 break;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002995 default:
2996 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
2997 break;
2998 }
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07002999 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003000 out->offload_thread_blocked = false;
3001 pthread_cond_signal(&out->cond);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08003002 if (send_callback && out->client_callback) {
3003 ALOGVV("%s: sending client_callback event %d", __func__, event);
3004 out->client_callback(event, NULL, out->client_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -07003005 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003006 free(cmd);
3007 }
3008
3009 pthread_cond_signal(&out->cond);
3010 while (!list_empty(&out->offload_cmd_list)) {
3011 item = list_head(&out->offload_cmd_list);
3012 list_remove(item);
3013 free(node_to_item(item, struct offload_cmd, node));
3014 }
3015 pthread_mutex_unlock(&out->lock);
3016
3017 return NULL;
3018}
3019
3020static int create_offload_callback_thread(struct stream_out *out)
3021{
3022 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
3023 list_init(&out->offload_cmd_list);
3024 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
3025 offload_thread_loop, out);
3026 return 0;
3027}
3028
3029static int destroy_offload_callback_thread(struct stream_out *out)
3030{
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07003031 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003032 stop_compressed_output_l(out);
3033 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
3034
3035 pthread_mutex_unlock(&out->lock);
3036 pthread_join(out->offload_thread, (void **) NULL);
3037 pthread_cond_destroy(&out->offload_cond);
3038
3039 return 0;
3040}
3041
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003042static int stop_output_stream(struct stream_out *out)
3043{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05303044 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003045 struct audio_usecase *uc_info;
3046 struct audio_device *adev = out->dev;
Aalique Grahame22e49102018-12-18 14:23:57 -08003047 bool has_voip_usecase =
3048 get_usecase_from_list(adev, USECASE_AUDIO_PLAYBACK_VOIP) != NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003049
Eric Laurent994a6932013-07-17 11:51:42 -07003050 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003051 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003052 uc_info = get_usecase_from_list(adev, out->usecase);
3053 if (uc_info == NULL) {
3054 ALOGE("%s: Could not find the usecase (%d) in the list",
3055 __func__, out->usecase);
3056 return -EINVAL;
3057 }
3058
Derek Chenea197282019-01-07 17:35:01 -08003059 if (audio_extn_ext_hw_plugin_usecase_stop(adev->ext_hw_plugin, uc_info))
3060 ALOGE("%s: failed to stop ext hw plugin", __func__);
Derek Chend2530072014-11-24 12:39:14 -08003061
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07003062 if (is_offload_usecase(out->usecase) &&
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05303063 !(audio_extn_passthru_is_passthrough_stream(out))) {
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08003064 if (adev->visualizer_stop_output != NULL)
3065 adev->visualizer_stop_output(out->handle, out->pcm_device_id);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08003066
3067 audio_extn_dts_remove_state_notifier_node(out->usecase);
3068
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08003069 if (adev->offload_effects_stop_output != NULL)
3070 adev->offload_effects_stop_output(out->handle, out->pcm_device_id);
3071 }
Eric Laurentc4aef752013-09-12 17:45:53 -07003072
Arun Mirpurief53ce52018-09-11 18:00:09 -07003073 if (out->usecase == USECASE_INCALL_MUSIC_UPLINK)
3074 voice_set_device_mute_flag(adev, false);
3075
Eric Laurent150dbfe2013-02-27 14:31:02 -08003076 /* 1. Get and set stream specific mixer controls */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07003077 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003078
3079 /* 2. Disable the rx device */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07003080 disable_snd_device(adev, uc_info->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003081
Aalique Grahame22e49102018-12-18 14:23:57 -08003082 audio_extn_extspk_update(adev->extspk);
3083
Xiaojun Sang785b5da2017-08-03 15:52:29 +08003084 if (is_offload_usecase(out->usecase)) {
3085 audio_enable_asm_bit_width_enforce_mode(adev->mixer,
3086 adev->dsp_bit_width_enforce_mode,
3087 false);
3088 }
Garmond Leung5fd0b552018-04-17 11:56:12 -07003089 if (audio_is_usb_out_device(out->devices & AUDIO_DEVICE_OUT_ALL_USB)) {
3090 ret = audio_extn_usb_check_and_set_svc_int(uc_info,
3091 false);
3092
3093 if (ret != 0)
3094 check_usecases_codec_backend(adev, uc_info, uc_info->out_snd_device);
3095 /* default service interval was successfully updated,
3096 reopen USB backend with new service interval */
3097 ret = 0;
3098 }
Xiaojun Sang785b5da2017-08-03 15:52:29 +08003099
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08003100 list_remove(&uc_info->list);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303101 out->started = 0;
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07003102 if (is_offload_usecase(out->usecase) &&
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05303103 (audio_extn_passthru_is_passthrough_stream(out))) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07003104 ALOGV("Disable passthrough , reset mixer to pcm");
3105 /* NO_PASSTHROUGH */
3106 out->compr_config.codec->compr_passthr = 0;
Mingming Yin21854652016-04-13 11:54:02 -07003107 audio_extn_passthru_on_stop(out);
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07003108 audio_extn_dolby_set_dap_bypass(adev, DAP_STATE_ON);
3109 }
Eric Laurent07eeafd2013-10-06 12:52:49 -07003110
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05303111 /* Must be called after removing the usecase from list */
3112 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
Md Mansoor Ahmeddb1b4f92018-01-25 18:56:31 +05303113 audio_extn_keep_alive_start(KEEP_ALIVE_OUT_HDMI);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05303114
Manish Dewangan21a850a2017-08-14 12:03:55 +05303115 if (out->ip_hdlr_handle) {
Naresh Tanniru85819452017-05-04 18:55:45 -07003116 ret = audio_extn_ip_hdlr_intf_close(out->ip_hdlr_handle, true, out);
3117 if (ret < 0)
3118 ALOGE("%s: audio_extn_ip_hdlr_intf_close failed %d",__func__, ret);
3119 }
3120
Aalique Grahame22e49102018-12-18 14:23:57 -08003121 if (has_voip_usecase ||
3122 out->devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
3123 struct listnode *node;
3124 struct audio_usecase *usecase;
3125 list_for_each(node, &adev->usecase_list) {
3126 usecase = node_to_item(node, struct audio_usecase, list);
3127 if (usecase->type == PCM_CAPTURE || usecase == uc_info)
3128 continue;
3129
3130 ALOGD("%s: select_devices at usecase(%d: %s) after removing the usecase(%d: %s)",
3131 __func__, usecase->id, use_case_table[usecase->id],
3132 out->usecase, use_case_table[out->usecase]);
3133 select_devices(adev, usecase->id);
3134 }
3135 }
3136
Garmond Leung5fd0b552018-04-17 11:56:12 -07003137 free(uc_info);
Eric Laurent994a6932013-07-17 11:51:42 -07003138 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003139 return ret;
3140}
3141
Vignesh Kulothungana6927272019-02-20 15:17:07 -08003142struct pcm* pcm_open_prepare_helper(unsigned int snd_card, unsigned int pcm_device_id,
3143 unsigned int flags, unsigned int pcm_open_retry_count,
3144 struct pcm_config *config)
3145{
3146 struct pcm* pcm = NULL;
3147
3148 while (1) {
3149 pcm = pcm_open(snd_card, pcm_device_id, flags, config);
3150 if (pcm == NULL || !pcm_is_ready(pcm)) {
3151 ALOGE("%s: %s", __func__, pcm_get_error(pcm));
3152 if (pcm != NULL) {
3153 pcm_close(pcm);
3154 pcm = NULL;
3155 }
3156 if (pcm_open_retry_count-- == 0)
3157 return NULL;
3158
3159 usleep(PROXY_OPEN_WAIT_TIME * 1000);
3160 continue;
3161 }
3162 break;
3163 }
3164
3165 if (pcm_is_ready(pcm)) {
3166 int ret = pcm_prepare(pcm);
3167 if (ret < 0) {
3168 ALOGE("%s: pcm_prepare returned %d", __func__, ret);
3169 pcm_close(pcm);
3170 pcm = NULL;
3171 }
3172 }
3173
3174 return pcm;
3175}
3176
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003177int start_output_stream(struct stream_out *out)
3178{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003179 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003180 struct audio_usecase *uc_info;
3181 struct audio_device *adev = out->dev;
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08003182 char mixer_ctl_name[128];
3183 struct mixer_ctl *ctl = NULL;
3184 char* perf_mode[] = {"ULL", "ULL_PP", "LL"};
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303185 bool a2dp_combo = false;
Vignesh Kulothungana6927272019-02-20 15:17:07 -08003186 bool is_haptic_usecase = (out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) ? true: false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003187
Haynes Mathew George380745d2017-10-04 15:27:45 -07003188 ATRACE_BEGIN("start_output_stream");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07003189 if ((out->usecase < 0) || (out->usecase >= AUDIO_USECASE_MAX)) {
3190 ret = -EINVAL;
3191 goto error_config;
3192 }
3193
Vignesh Kulothungana6927272019-02-20 15:17:07 -08003194 ALOGD("%s: enter: stream(%p)usecase(%d: %s) devices(%#x) is_haptic_usecase(%d)",
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05303195 __func__, &out->stream, out->usecase, use_case_table[out->usecase],
Vignesh Kulothungana6927272019-02-20 15:17:07 -08003196 out->devices, is_haptic_usecase);
Naresh Tanniru4c630392014-05-12 01:05:52 +05303197
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05303198 if (CARD_STATUS_OFFLINE == out->card_status ||
3199 CARD_STATUS_OFFLINE == adev->card_status) {
3200 ALOGW("out->card_status or adev->card_status offline, try again");
Dhanalakshmi Siddani4d57e992014-07-17 16:37:51 +05303201 ret = -EIO;
Naresh Tanniru4c630392014-05-12 01:05:52 +05303202 goto error_config;
3203 }
Naresh Tanniru4c630392014-05-12 01:05:52 +05303204
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05303205 if (out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
Florian Pfister1a84f312018-07-19 14:38:18 +02003206 if (!audio_extn_a2dp_source_is_ready()) {
Aalique Grahame22e49102018-12-18 14:23:57 -08003207 if (out->devices &
3208 (AUDIO_DEVICE_OUT_SPEAKER | AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303209 a2dp_combo = true;
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05303210 } else {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303211 if (!(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
3212 ALOGE("%s: A2DP profile is not ready, return error", __func__);
3213 ret = -EAGAIN;
3214 goto error_config;
3215 }
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05303216 }
3217 }
3218 }
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303219 if (out->devices & AUDIO_DEVICE_OUT_ALL_SCO) {
3220 if (!adev->bt_sco_on) {
3221 if (out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
3222 //combo usecase just by pass a2dp
3223 ALOGW("%s: SCO is not connected, route it to speaker", __func__);
3224 out->devices = AUDIO_DEVICE_OUT_SPEAKER;
3225 } else {
3226 ALOGE("%s: SCO profile is not ready, return error", __func__);
3227 ret = -EAGAIN;
3228 goto error_config;
3229 }
3230 }
3231 }
3232
Eric Laurentb23d5282013-05-14 15:27:20 -07003233 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003234 if (out->pcm_device_id < 0) {
3235 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
3236 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08003237 ret = -EINVAL;
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07003238 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003239 }
3240
Vignesh Kulothungana6927272019-02-20 15:17:07 -08003241 if (is_haptic_usecase) {
3242 adev->haptic_pcm_device_id = platform_get_haptics_pcm_device_id();
3243 if (adev->haptic_pcm_device_id < 0) {
3244 ALOGE("%s: Invalid Haptics pcm device id(%d) for the usecase(%d)",
3245 __func__, adev->haptic_pcm_device_id, out->usecase);
3246 ret = -EINVAL;
3247 goto error_config;
3248 }
3249 }
3250
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003251 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07003252
3253 if (!uc_info) {
3254 ret = -ENOMEM;
3255 goto error_config;
3256 }
3257
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003258 uc_info->id = out->usecase;
3259 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08003260 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003261 uc_info->devices = out->devices;
3262 uc_info->in_snd_device = SND_DEVICE_NONE;
3263 uc_info->out_snd_device = SND_DEVICE_NONE;
Garmond Leung5fd0b552018-04-17 11:56:12 -07003264
3265 /* This must be called before adding this usecase to the list */
3266 if (audio_is_usb_out_device(out->devices & AUDIO_DEVICE_OUT_ALL_USB)) {
3267 audio_extn_usb_check_and_set_svc_int(uc_info, true);
3268 /* USB backend is not reopened immediately.
3269 This is eventually done as part of select_devices */
3270 }
3271
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08003272 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003273
Sudheer Papothifa9d2282015-09-17 01:53:25 +05303274 audio_extn_perf_lock_acquire(&adev->perf_lock_handle, 0,
3275 adev->perf_lock_opts,
3276 adev->perf_lock_opts_size);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05303277
3278 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Md Mansoor Ahmeddb1b4f92018-01-25 18:56:31 +05303279 audio_extn_keep_alive_stop(KEEP_ALIVE_OUT_HDMI);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05303280 if (audio_extn_passthru_is_enabled() &&
3281 audio_extn_passthru_is_passthrough_stream(out)) {
3282 audio_extn_passthru_on_start(out);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05303283 }
3284 }
3285
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303286 if ((out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) &&
Florian Pfister1a84f312018-07-19 14:38:18 +02003287 (!audio_extn_a2dp_source_is_ready())) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303288 if (!a2dp_combo) {
3289 check_a2dp_restore_l(adev, out, false);
3290 } else {
3291 audio_devices_t dev = out->devices;
Aalique Grahame22e49102018-12-18 14:23:57 -08003292 if (dev & AUDIO_DEVICE_OUT_SPEAKER_SAFE)
3293 out->devices = AUDIO_DEVICE_OUT_SPEAKER_SAFE;
3294 else
3295 out->devices = AUDIO_DEVICE_OUT_SPEAKER;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303296 select_devices(adev, out->usecase);
3297 out->devices = dev;
3298 }
3299 } else {
3300 select_devices(adev, out->usecase);
3301 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003302
Arun Mirpurief53ce52018-09-11 18:00:09 -07003303 if (out->usecase == USECASE_INCALL_MUSIC_UPLINK)
3304 voice_set_device_mute_flag(adev, true);
3305
Derek Chenea197282019-01-07 17:35:01 -08003306 if (audio_extn_ext_hw_plugin_usecase_start(adev->ext_hw_plugin, uc_info))
3307 ALOGE("%s: failed to start ext hw plugin", __func__);
Derek Chend2530072014-11-24 12:39:14 -08003308
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003309 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d) format(%#x)",
3310 __func__, adev->snd_card, out->pcm_device_id, out->config.format);
Haynes Mathew George16081042017-05-31 17:16:49 -07003311
3312 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
Arun Mirpuri5d170872019-03-26 13:21:31 -07003313 ALOGD("%s: Starting MMAP stream", __func__);
Haynes Mathew George16081042017-05-31 17:16:49 -07003314 if (out->pcm == NULL || !pcm_is_ready(out->pcm)) {
3315 ALOGE("%s: pcm stream not ready", __func__);
3316 goto error_open;
3317 }
3318 ret = pcm_start(out->pcm);
3319 if (ret < 0) {
3320 ALOGE("%s: MMAP pcm_start failed ret %d", __func__, ret);
3321 goto error_open;
3322 }
Arun Mirpuri5d170872019-03-26 13:21:31 -07003323 out_set_mmap_volume(&out->stream, out->volume_l, out->volume_r);
Haynes Mathew George16081042017-05-31 17:16:49 -07003324 } else if (!is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003325 unsigned int flags = PCM_OUT;
3326 unsigned int pcm_open_retry_count = 0;
3327 if (out->usecase == USECASE_AUDIO_PLAYBACK_AFE_PROXY) {
3328 flags |= PCM_MMAP | PCM_NOIRQ;
3329 pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT;
Haynes Mathew George5beddd42016-06-27 18:33:40 -07003330 } else if (out->realtime) {
Haynes Mathew George4ab3ba92017-12-11 14:49:43 -08003331 flags |= PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003332 } else
3333 flags |= PCM_MONOTONIC;
3334
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08003335 if ((adev->vr_audio_mode_enabled) &&
3336 (out->flags & AUDIO_OUTPUT_FLAG_RAW)) {
3337 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
3338 "PCM_Dev %d Topology", out->pcm_device_id);
3339 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
3340 if (!ctl) {
3341 ALOGI("%s: Could not get ctl for mixer cmd might be ULL - %s",
3342 __func__, mixer_ctl_name);
3343 } else {
3344 //if success use ULLPP
3345 ALOGI("%s: mixer ctrl %s succeeded setting up ULL for %d",
3346 __func__, mixer_ctl_name, out->pcm_device_id);
3347 //There is a still a possibility that some sessions
3348 // that request for FAST|RAW when 3D audio is active
3349 //can go through ULLPP. Ideally we expects apps to
3350 //listen to audio focus and stop concurrent playback
3351 //Also, we will look for mode flag (voice_in_communication)
3352 //before enabling the realtime flag.
3353 mixer_ctl_set_enum_by_string(ctl, perf_mode[1]);
3354 }
3355 }
3356
Surendar Karka91fa3682018-07-02 18:12:12 +05303357 if (out->realtime)
3358 platform_set_stream_channel_map(adev->platform, out->channel_mask,
3359 out->pcm_device_id, &out->channel_map_param.channel_map[0]);
3360
Vignesh Kulothungana6927272019-02-20 15:17:07 -08003361 out->pcm = pcm_open_prepare_helper(adev->snd_card, out->pcm_device_id,
3362 flags, pcm_open_retry_count,
3363 &(out->config));
3364 if (out->pcm == NULL) {
3365 ret = -EIO;
3366 goto error_open;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003367 }
Vignesh Kulothungana6927272019-02-20 15:17:07 -08003368
3369 if (is_haptic_usecase) {
3370 adev->haptic_pcm = pcm_open_prepare_helper(adev->snd_card,
3371 adev->haptic_pcm_device_id,
3372 flags, pcm_open_retry_count,
3373 &(adev->haptics_config));
3374 // failure to open haptics pcm shouldnt stop audio,
3375 // so do not close audio pcm in case of error
3376 }
3377
Surendar Karka91fa3682018-07-02 18:12:12 +05303378 if (!out->realtime)
3379 platform_set_stream_channel_map(adev->platform, out->channel_mask,
Surendar Karkaf51b5842018-04-26 11:28:38 +05303380 out->pcm_device_id, &out->channel_map_param.channel_map[0]);
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07003381
Zhou Song2b8f28f2017-09-11 10:51:38 +08003382 // apply volume for voip playback after path is set up
3383 if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP)
3384 out_set_voip_volume(&out->stream, out->volume_l, out->volume_r);
Ramu Gottipati36547092018-12-28 11:32:09 +05303385 else if ((out->usecase == USECASE_AUDIO_PLAYBACK_LOW_LATENCY || out->usecase == USECASE_AUDIO_PLAYBACK_DEEP_BUFFER ||
3386 out->usecase == USECASE_AUDIO_PLAYBACK_ULL) && (out->apply_volume)) {
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05303387 out_set_pcm_volume(&out->stream, out->volume_l, out->volume_r);
3388 out->apply_volume = false;
3389 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003390 } else {
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -07003391 platform_set_stream_channel_map(adev->platform, out->channel_mask,
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05303392 out->pcm_device_id, &out->channel_map_param.channel_map[0]);
Xiaojun Sang785b5da2017-08-03 15:52:29 +08003393 audio_enable_asm_bit_width_enforce_mode(adev->mixer,
3394 adev->dsp_bit_width_enforce_mode,
3395 true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003396 out->pcm = NULL;
Haynes Mathew George380745d2017-10-04 15:27:45 -07003397 ATRACE_BEGIN("compress_open");
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08003398 out->compr = compress_open(adev->snd_card,
3399 out->pcm_device_id,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003400 COMPRESS_IN, &out->compr_config);
Haynes Mathew George380745d2017-10-04 15:27:45 -07003401 ATRACE_END();
Satish Babu Patakokila54ce83d2018-07-06 18:00:37 +05303402 if (errno == ENETRESET && !is_compress_ready(out->compr)) {
Sharad Sanglec6f32552018-05-04 16:15:38 +05303403 ALOGE("%s: compress_open failed errno:%d\n", __func__, errno);
3404 adev->card_status = CARD_STATUS_OFFLINE;
3405 out->card_status = CARD_STATUS_OFFLINE;
3406 ret = -EIO;
3407 goto error_open;
3408 }
3409
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003410 if (out->compr && !is_compress_ready(out->compr)) {
Haynes Mathew Georgeee5836f2017-11-21 18:02:10 -08003411 ALOGE("%s: failed /w error %s", __func__, compress_get_error(out->compr));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003412 compress_close(out->compr);
3413 out->compr = NULL;
3414 ret = -EIO;
3415 goto error_open;
3416 }
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05303417 /* compress_open sends params of the track, so reset the flag here */
3418 out->is_compr_metadata_avail = false;
3419
Ben Rombergerd771a7c2017-02-22 18:05:17 -08003420 if (out->client_callback)
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003421 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07003422
Fred Oh3f43e742015-03-04 18:42:34 -08003423 /* Since small bufs uses blocking writes, a write will be blocked
3424 for the default max poll time (20s) in the event of an SSR.
3425 Reduce the poll time to observe and deal with SSR faster.
3426 */
Ashish Jain5106d362016-05-11 19:23:33 +05303427 if (!out->non_blocking) {
Fred Oh3f43e742015-03-04 18:42:34 -08003428 compress_set_max_poll_wait(out->compr, 1000);
3429 }
3430
Manish Dewangan69426c82017-01-30 17:35:36 +05303431 audio_extn_utils_compress_set_render_mode(out);
Manish Dewangan58229382017-02-02 15:48:41 +05303432 audio_extn_utils_compress_set_clk_rec_mode(uc_info);
Manish Dewangan69426c82017-01-30 17:35:36 +05303433
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08003434 audio_extn_dts_create_state_notifier_node(out->usecase);
3435 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
3436 popcount(out->channel_mask),
3437 out->playback_started);
3438
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08003439#ifdef DS1_DOLBY_DDP_ENABLED
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05303440 if (audio_extn_utils_is_dolby_format(out->format))
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08003441 audio_extn_dolby_send_ddp_endp_params(adev);
3442#endif
Preetam Singh Ranawatd18d8832017-02-08 17:34:54 +05303443 if (!(audio_extn_passthru_is_passthrough_stream(out)) &&
3444 (out->sample_rate != 176400 && out->sample_rate <= 192000)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07003445 if (adev->visualizer_start_output != NULL)
3446 adev->visualizer_start_output(out->handle, out->pcm_device_id);
3447 if (adev->offload_effects_start_output != NULL)
Ashish Jain5106d362016-05-11 19:23:33 +05303448 adev->offload_effects_start_output(out->handle, out->pcm_device_id, adev->mixer);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08003449 audio_extn_check_and_set_dts_hpx_state(adev);
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07003450 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003451 }
Haynes Mathew George5beddd42016-06-27 18:33:40 -07003452
3453 if (ret == 0) {
3454 register_out_stream(out);
3455 if (out->realtime) {
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07003456 if (out->pcm == NULL || !pcm_is_ready(out->pcm)) {
3457 ALOGE("%s: pcm stream not ready", __func__);
3458 goto error_open;
3459 }
Haynes Mathew George380745d2017-10-04 15:27:45 -07003460 ATRACE_BEGIN("pcm_start");
Haynes Mathew George5beddd42016-06-27 18:33:40 -07003461 ret = pcm_start(out->pcm);
Haynes Mathew George380745d2017-10-04 15:27:45 -07003462 ATRACE_END();
Haynes Mathew George5beddd42016-06-27 18:33:40 -07003463 if (ret < 0)
3464 goto error_open;
3465 }
3466 }
3467
Sudheer Papothifa9d2282015-09-17 01:53:25 +05303468 audio_extn_perf_lock_release(&adev->perf_lock_handle);
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07003469 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07003470
Manish Dewangan21a850a2017-08-14 12:03:55 +05303471 if (out->ip_hdlr_handle) {
Vidyakumar Athota6d655882017-05-22 18:26:24 -07003472 ret = audio_extn_ip_hdlr_intf_open(out->ip_hdlr_handle, true, out, out->usecase);
Naresh Tanniru85819452017-05-04 18:55:45 -07003473 if (ret < 0)
3474 ALOGE("%s: audio_extn_ip_hdlr_intf_open failed %d",__func__, ret);
3475 }
3476
Vignesh Kulothungan3b5fae52017-09-25 12:16:30 -07003477 // consider a scenario where on pause lower layers are tear down.
3478 // so on resume, swap mixer control need to be sent only when
3479 // backend is active, hence rather than sending from enable device
3480 // sending it from start of streamtream
3481
3482 platform_set_swap_channels(adev, true);
3483
Haynes Mathew George380745d2017-10-04 15:27:45 -07003484 ATRACE_END();
Vatsal Buchac09ae062018-11-14 13:25:08 +05303485 enable_gcov();
Haynes Mathew George5beddd42016-06-27 18:33:40 -07003486 return ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003487error_open:
Vignesh Kulothungana6927272019-02-20 15:17:07 -08003488 if (adev->haptic_pcm) {
3489 pcm_close(adev->haptic_pcm);
3490 adev->haptic_pcm = NULL;
3491 }
Sudheer Papothifa9d2282015-09-17 01:53:25 +05303492 audio_extn_perf_lock_release(&adev->perf_lock_handle);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003493 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08003494error_config:
Laxminath Kasam2cb4b752015-09-24 03:59:15 +05303495 /*
3496 * sleep 50ms to allow sufficient time for kernel
3497 * drivers to recover incases like SSR.
3498 */
3499 usleep(50000);
Haynes Mathew George380745d2017-10-04 15:27:45 -07003500 ATRACE_END();
Vatsal Buchac09ae062018-11-14 13:25:08 +05303501 enable_gcov();
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08003502 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003503}
3504
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003505static int check_input_parameters(uint32_t sample_rate,
3506 audio_format_t format,
Aalique Grahame22e49102018-12-18 14:23:57 -08003507 int channel_count,
3508 bool is_usb_hifi)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003509{
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003510 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003511
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05303512 if (((format != AUDIO_FORMAT_PCM_16_BIT) && (format != AUDIO_FORMAT_PCM_8_24_BIT) &&
3513 (format != AUDIO_FORMAT_PCM_24_BIT_PACKED) && (format != AUDIO_FORMAT_PCM_32_BIT) &&
3514 (format != AUDIO_FORMAT_PCM_FLOAT)) &&
Mingming Yine62d7842013-10-25 16:26:03 -07003515 !voice_extn_compress_voip_is_format_supported(format) &&
Ralf Herzaec80262018-07-03 07:08:49 +02003516 !audio_extn_compr_cap_format_supported(format) &&
3517 !audio_extn_cin_format_supported(format))
Haynes Mathew George484e8d22017-07-31 18:55:17 -07003518 ret = -EINVAL;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003519
Aalique Grahame22e49102018-12-18 14:23:57 -08003520 int max_channel_count = is_usb_hifi ? MAX_HIFI_CHANNEL_COUNT : MAX_CHANNEL_COUNT;
3521 if ((channel_count < MIN_CHANNEL_COUNT) || (channel_count > max_channel_count)) {
3522 ALOGE("%s: unsupported channel count (%d) passed Min / Max (%d / %d)", __func__,
3523 channel_count, MIN_CHANNEL_COUNT, max_channel_count);
3524 return -EINVAL;
3525 }
3526
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003527 switch (channel_count) {
3528 case 1:
3529 case 2:
Chaithanya Krishna Bacharaju9955b162016-05-25 16:25:53 +05303530 case 3:
3531 case 4:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003532 case 6:
Karthikeyan Mani07faa602018-08-20 11:01:32 -07003533 case 8:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003534 break;
3535 default:
3536 ret = -EINVAL;
3537 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003538
3539 switch (sample_rate) {
3540 case 8000:
3541 case 11025:
3542 case 12000:
3543 case 16000:
3544 case 22050:
3545 case 24000:
3546 case 32000:
3547 case 44100:
3548 case 48000:
Haynes Mathew Georgec9253d12017-12-13 15:58:28 -08003549 case 88200:
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05303550 case 96000:
Haynes Mathew Georgec9253d12017-12-13 15:58:28 -08003551 case 176400:
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05303552 case 192000:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003553 break;
3554 default:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003555 ret = -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003556 }
3557
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003558 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003559}
3560
Naresh Tanniru04f71882018-06-26 17:46:22 +05303561
3562/** Add a value in a list if not already present.
3563 * @return true if value was successfully inserted or already present,
3564 * false if the list is full and does not contain the value.
3565 */
3566static bool register_uint(uint32_t value, uint32_t* list, size_t list_length) {
3567 for (size_t i = 0; i < list_length; i++) {
3568 if (list[i] == value) return true; // value is already present
3569 if (list[i] == 0) { // no values in this slot
3570 list[i] = value;
3571 return true; // value inserted
3572 }
3573 }
3574 return false; // could not insert value
3575}
3576
3577/** Add channel_mask in supported_channel_masks if not already present.
3578 * @return true if channel_mask was successfully inserted or already present,
3579 * false if supported_channel_masks is full and does not contain channel_mask.
3580 */
3581static void register_channel_mask(audio_channel_mask_t channel_mask,
3582 audio_channel_mask_t supported_channel_masks[static MAX_SUPPORTED_CHANNEL_MASKS]) {
3583 ALOGE_IF(!register_uint(channel_mask, supported_channel_masks, MAX_SUPPORTED_CHANNEL_MASKS),
3584 "%s: stream can not declare supporting its channel_mask %x", __func__, channel_mask);
3585}
3586
3587/** Add format in supported_formats if not already present.
3588 * @return true if format was successfully inserted or already present,
3589 * false if supported_formats is full and does not contain format.
3590 */
3591static void register_format(audio_format_t format,
3592 audio_format_t supported_formats[static MAX_SUPPORTED_FORMATS]) {
3593 ALOGE_IF(!register_uint(format, supported_formats, MAX_SUPPORTED_FORMATS),
3594 "%s: stream can not declare supporting its format %x", __func__, format);
3595}
3596/** Add sample_rate in supported_sample_rates if not already present.
3597 * @return true if sample_rate was successfully inserted or already present,
3598 * false if supported_sample_rates is full and does not contain sample_rate.
3599 */
3600static void register_sample_rate(uint32_t sample_rate,
3601 uint32_t supported_sample_rates[static MAX_SUPPORTED_SAMPLE_RATES]) {
3602 ALOGE_IF(!register_uint(sample_rate, supported_sample_rates, MAX_SUPPORTED_SAMPLE_RATES),
3603 "%s: stream can not declare supporting its sample rate %x", __func__, sample_rate);
3604}
3605
Karthikeyan Manib38769c2018-11-07 15:44:13 -08003606static inline uint32_t lcm(uint32_t num1, uint32_t num2)
3607{
3608 uint32_t high = num1, low = num2, temp = 0;
3609
3610 if (!num1 || !num2)
3611 return 0;
3612
3613 if (num1 < num2) {
3614 high = num2;
3615 low = num1;
3616 }
3617
3618 while (low != 0) {
3619 temp = low;
3620 low = high % low;
3621 high = temp;
3622 }
3623 return (num1 * num2)/high;
3624}
3625
3626static inline uint32_t nearest_multiple(uint32_t num, uint32_t multiplier)
3627{
3628 uint32_t remainder = 0;
3629
3630 if (!multiplier)
3631 return num;
3632
3633 remainder = num % multiplier;
3634 if (remainder)
3635 num += (multiplier - remainder);
3636
3637 return num;
3638}
3639
Aalique Grahame22e49102018-12-18 14:23:57 -08003640static size_t get_stream_buffer_size(size_t duration_ms,
3641 uint32_t sample_rate,
3642 audio_format_t format,
3643 int channel_count,
3644 bool is_low_latency)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003645{
3646 size_t size = 0;
Karthikeyan Manib38769c2018-11-07 15:44:13 -08003647 uint32_t bytes_per_period_sample = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003648
Aalique Grahame22e49102018-12-18 14:23:57 -08003649 size = (sample_rate * duration_ms) / 1000;
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07003650 if (is_low_latency)
3651 size = configured_low_latency_capture_period_size;
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05303652
Karthikeyan Manib38769c2018-11-07 15:44:13 -08003653 bytes_per_period_sample = audio_bytes_per_sample(format) * channel_count;
Aalique Grahame22e49102018-12-18 14:23:57 -08003654 size *= audio_bytes_per_sample(format) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003655
Ralf Herzbd08d632018-09-28 15:50:49 +02003656 /* make sure the size is multiple of 32 bytes and additionally multiple of
3657 * the frame_size (required for 24bit samples and non-power-of-2 channel counts)
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07003658 * At 48 kHz mono 16-bit PCM:
3659 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
3660 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
Karthikeyan Manib38769c2018-11-07 15:44:13 -08003661 * Also, make sure the size is multiple of bytes per period sample
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07003662 */
Karthikeyan Manib38769c2018-11-07 15:44:13 -08003663 size = nearest_multiple(size, lcm(32, bytes_per_period_sample));
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07003664
3665 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003666}
3667
Aalique Grahame22e49102018-12-18 14:23:57 -08003668static size_t get_input_buffer_size(uint32_t sample_rate,
3669 audio_format_t format,
3670 int channel_count,
3671 bool is_low_latency)
3672{
3673 /* Don't know if USB HIFI in this context so use true to be conservative */
3674 if (check_input_parameters(sample_rate, format, channel_count,
3675 true /*is_usb_hifi */) != 0)
3676 return 0;
3677
3678 return get_stream_buffer_size(AUDIO_CAPTURE_PERIOD_DURATION_MSEC,
3679 sample_rate,
3680 format,
3681 channel_count,
3682 is_low_latency);
3683}
3684
Ashish Jain058165c2016-09-28 23:18:48 +05303685static size_t get_output_period_size(uint32_t sample_rate,
3686 audio_format_t format,
3687 int channel_count,
3688 int duration /*in millisecs*/)
3689{
3690 size_t size = 0;
3691 uint32_t bytes_per_sample = audio_bytes_per_sample(format);
3692
3693 if ((duration == 0) || (sample_rate == 0) ||
3694 (bytes_per_sample == 0) || (channel_count == 0)) {
3695 ALOGW("Invalid config duration %d sr %d bps %d ch %d", duration, sample_rate,
3696 bytes_per_sample, channel_count);
3697 return -EINVAL;
3698 }
3699
3700 size = (sample_rate *
3701 duration *
3702 bytes_per_sample *
3703 channel_count) / 1000;
3704 /*
3705 * To have same PCM samples for all channels, the buffer size requires to
3706 * be multiple of (number of channels * bytes per sample)
3707 * For writes to succeed, the buffer must be written at address which is multiple of 32
3708 */
3709 size = ALIGN(size, (bytes_per_sample * channel_count * 32));
3710
3711 return (size/(channel_count * bytes_per_sample));
3712}
3713
Zhou Song48453a02018-01-10 17:50:59 +08003714static uint64_t get_actual_pcm_frames_rendered(struct stream_out *out, struct timespec *timestamp)
Ashish Jain5106d362016-05-11 19:23:33 +05303715{
3716 uint64_t actual_frames_rendered = 0;
3717 size_t kernel_buffer_size = out->compr_config.fragment_size * out->compr_config.fragments;
3718
3719 /* This adjustment accounts for buffering after app processor.
3720 * It is based on estimated DSP latency per use case, rather than exact.
3721 */
3722 int64_t platform_latency = platform_render_latency(out->usecase) *
3723 out->sample_rate / 1000000LL;
3724
Zhou Song48453a02018-01-10 17:50:59 +08003725 pthread_mutex_lock(&out->position_query_lock);
Ashish Jain5106d362016-05-11 19:23:33 +05303726 /* not querying actual state of buffering in kernel as it would involve an ioctl call
3727 * which then needs protection, this causes delay in TS query for pcm_offload usecase
3728 * hence only estimate.
3729 */
3730 int64_t signed_frames = out->written - kernel_buffer_size;
3731
3732 signed_frames = signed_frames / (audio_bytes_per_sample(out->format) * popcount(out->channel_mask)) - platform_latency;
3733
Zhou Song48453a02018-01-10 17:50:59 +08003734 if (signed_frames > 0) {
Ashish Jain5106d362016-05-11 19:23:33 +05303735 actual_frames_rendered = signed_frames;
Zhou Song48453a02018-01-10 17:50:59 +08003736 if (timestamp != NULL )
3737 *timestamp = out->writeAt;
3738 } else if (timestamp != NULL) {
3739 clock_gettime(CLOCK_MONOTONIC, timestamp);
3740 }
3741 pthread_mutex_unlock(&out->position_query_lock);
Ashish Jain5106d362016-05-11 19:23:33 +05303742
3743 ALOGVV("%s signed frames %lld out_written %lld kernel_buffer_size %d"
3744 "bytes/sample %zu channel count %d", __func__,(long long int)signed_frames,
3745 (long long int)out->written, (int)kernel_buffer_size,
3746 audio_bytes_per_sample(out->compr_config.codec->format),
3747 popcount(out->channel_mask));
3748
3749 return actual_frames_rendered;
3750}
3751
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003752static uint32_t out_get_sample_rate(const struct audio_stream *stream)
3753{
3754 struct stream_out *out = (struct stream_out *)stream;
3755
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003756 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003757}
3758
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07003759static int out_set_sample_rate(struct audio_stream *stream __unused,
3760 uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003761{
3762 return -ENOSYS;
3763}
3764
3765static size_t out_get_buffer_size(const struct audio_stream *stream)
3766{
3767 struct stream_out *out = (struct stream_out *)stream;
3768
Varun Balaraje49253e2017-07-06 19:48:56 +05303769 if (is_interactive_usecase(out->usecase)) {
Sri Karri27279e12017-08-07 16:05:20 +05303770 return out->config.period_size * out->config.period_count;
Varun Balaraje49253e2017-07-06 19:48:56 +05303771 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Naresh Tanniruee3499a2017-01-05 14:05:35 +05303772 if (out->flags & AUDIO_OUTPUT_FLAG_TIMESTAMP)
3773 return out->compr_config.fragment_size - sizeof(struct snd_codec_metadata);
3774 else
3775 return out->compr_config.fragment_size;
3776 } else if(out->usecase == USECASE_COMPRESS_VOIP_CALL)
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08003777 return voice_extn_compress_voip_out_get_buffer_size(out);
Vikram Panduranga93f080e2017-06-07 18:16:14 -07003778 else if(out->usecase == USECASE_AUDIO_PLAYBACK_VOIP)
3779 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 +05303780 else if (is_offload_usecase(out->usecase) &&
3781 out->flags == AUDIO_OUTPUT_FLAG_DIRECT)
Ashish Jain83a6cc22016-06-28 14:34:17 +05303782 return out->hal_fragment_size;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003783
Haynes Mathew George5beddd42016-06-27 18:33:40 -07003784 return out->config.period_size * out->af_period_multiplier *
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07003785 audio_stream_out_frame_size((const struct audio_stream_out *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003786}
3787
3788static uint32_t out_get_channels(const struct audio_stream *stream)
3789{
3790 struct stream_out *out = (struct stream_out *)stream;
3791
3792 return out->channel_mask;
3793}
3794
3795static audio_format_t out_get_format(const struct audio_stream *stream)
3796{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003797 struct stream_out *out = (struct stream_out *)stream;
3798
3799 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003800}
3801
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07003802static int out_set_format(struct audio_stream *stream __unused,
3803 audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003804{
3805 return -ENOSYS;
3806}
3807
3808static int out_standby(struct audio_stream *stream)
3809{
3810 struct stream_out *out = (struct stream_out *)stream;
3811 struct audio_device *adev = out->dev;
Haynes Mathew George16081042017-05-31 17:16:49 -07003812 bool do_stop = true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003813
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05303814 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
3815 stream, out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003816
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07003817 lock_output_stream(out);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003818 if (!out->standby) {
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07003819 if (adev->adm_deregister_stream)
3820 adev->adm_deregister_stream(adev->adm_data, out->handle);
3821
Haynes Mathew George7fce0a52016-06-23 18:22:27 -07003822 if (is_offload_usecase(out->usecase))
3823 stop_compressed_output_l(out);
3824
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08003825 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003826 out->standby = true;
Zhou Songa8895042016-07-05 17:54:22 +08003827 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
3828 voice_extn_compress_voip_close_output_stream(stream);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303829 out->started = 0;
Zhou Songa8895042016-07-05 17:54:22 +08003830 pthread_mutex_unlock(&adev->lock);
3831 pthread_mutex_unlock(&out->lock);
3832 ALOGD("VOIP output entered standby");
3833 return 0;
3834 } else if (!is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003835 if (out->pcm) {
3836 pcm_close(out->pcm);
3837 out->pcm = NULL;
3838 }
Haynes Mathew George16081042017-05-31 17:16:49 -07003839 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
3840 do_stop = out->playback_started;
3841 out->playback_started = false;
3842 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003843 } else {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07003844 ALOGD("copl(%p):standby", out);
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05303845 out->send_next_track_params = false;
3846 out->is_compr_metadata_avail = false;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07003847 out->gapless_mdata.encoder_delay = 0;
3848 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003849 if (out->compr != NULL) {
3850 compress_close(out->compr);
3851 out->compr = NULL;
3852 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08003853 }
Haynes Mathew George16081042017-05-31 17:16:49 -07003854 if (do_stop) {
3855 stop_output_stream(out);
3856 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08003857 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003858 }
3859 pthread_mutex_unlock(&out->lock);
Ashish Jainbbce4322016-02-16 13:25:27 +05303860 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003861 return 0;
3862}
3863
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05303864static int out_on_error(struct audio_stream *stream)
3865{
3866 struct stream_out *out = (struct stream_out *)stream;
Ben Rombergerfd02a2f2018-09-17 10:23:23 -07003867 int status = 0;
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05303868
3869 lock_output_stream(out);
Haynes Mathew Georgeee5836f2017-11-21 18:02:10 -08003870 // always send CMD_ERROR for offload streams, this
3871 // is needed e.g. when SSR happens within compress_open
3872 // since the stream is active, offload_callback_thread is also active.
3873 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
3874 stop_compressed_output_l(out);
Ben Rombergerfd02a2f2018-09-17 10:23:23 -07003875 }
3876 pthread_mutex_unlock(&out->lock);
3877
3878 status = out_standby(&out->stream.common);
3879
3880 lock_output_stream(out);
3881 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Haynes Mathew Georgeee5836f2017-11-21 18:02:10 -08003882 send_offload_cmd_l(out, OFFLOAD_CMD_ERROR);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05303883 }
Manisha Agarwal7b3e3772019-02-20 14:33:45 +05303884
3885 if (is_offload_usecase(out->usecase) && out->card_status == CARD_STATUS_OFFLINE) {
3886 ALOGD("Setting previous card status if offline");
3887 out->prev_card_status_offline = true;
3888 }
3889
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05303890 pthread_mutex_unlock(&out->lock);
3891
Ben Rombergerfd02a2f2018-09-17 10:23:23 -07003892 return status;
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05303893}
3894
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303895/*
3896 *standby implementation without locks, assumes that the callee already
3897 *has taken adev and out lock.
3898 */
3899int out_standby_l(struct audio_stream *stream)
3900{
3901 struct stream_out *out = (struct stream_out *)stream;
3902 struct audio_device *adev = out->dev;
3903
3904 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
3905 stream, out->usecase, use_case_table[out->usecase]);
3906
3907 if (!out->standby) {
Haynes Mathew George380745d2017-10-04 15:27:45 -07003908 ATRACE_BEGIN("out_standby_l");
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303909 if (adev->adm_deregister_stream)
3910 adev->adm_deregister_stream(adev->adm_data, out->handle);
3911
3912 if (is_offload_usecase(out->usecase))
3913 stop_compressed_output_l(out);
3914
3915 out->standby = true;
3916 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
3917 voice_extn_compress_voip_close_output_stream(stream);
3918 out->started = 0;
3919 ALOGD("VOIP output entered standby");
Haynes Mathew George380745d2017-10-04 15:27:45 -07003920 ATRACE_END();
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303921 return 0;
3922 } else if (!is_offload_usecase(out->usecase)) {
3923 if (out->pcm) {
3924 pcm_close(out->pcm);
3925 out->pcm = NULL;
3926 }
Vignesh Kulothungana6927272019-02-20 15:17:07 -08003927 if (out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) {
3928 if (adev->haptic_pcm) {
3929 pcm_close(adev->haptic_pcm);
3930 adev->haptic_pcm = NULL;
3931 }
3932
3933 if (adev->haptic_buffer != NULL) {
3934 free(adev->haptic_buffer);
3935 adev->haptic_buffer = NULL;
3936 adev->haptic_buffer_size = 0;
3937 }
3938 adev->haptic_pcm_device_id = 0;
3939 }
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303940 } else {
3941 ALOGD("copl(%p):standby", out);
3942 out->send_next_track_params = false;
3943 out->is_compr_metadata_avail = false;
3944 out->gapless_mdata.encoder_delay = 0;
3945 out->gapless_mdata.encoder_padding = 0;
3946 if (out->compr != NULL) {
3947 compress_close(out->compr);
3948 out->compr = NULL;
3949 }
3950 }
3951 stop_output_stream(out);
Haynes Mathew George380745d2017-10-04 15:27:45 -07003952 ATRACE_END();
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303953 }
3954 ALOGD("%s: exit", __func__);
3955 return 0;
3956}
3957
Aalique Grahame22e49102018-12-18 14:23:57 -08003958static int out_dump(const struct audio_stream *stream, int fd)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003959{
Aalique Grahame22e49102018-12-18 14:23:57 -08003960 struct stream_out *out = (struct stream_out *)stream;
3961
3962 // We try to get the lock for consistency,
3963 // but it isn't necessary for these variables.
3964 // If we're not in standby, we may be blocked on a write.
3965 const bool locked = (pthread_mutex_trylock(&out->lock) == 0);
3966 dprintf(fd, " Standby: %s\n", out->standby ? "yes" : "no");
3967 dprintf(fd, " Frames written: %lld\n", (long long)out->written);
3968
3969 if (locked) {
3970 pthread_mutex_unlock(&out->lock);
3971 }
3972
3973 // dump error info
3974 (void)error_log_dump(
3975 out->error_log, fd, " " /* prefix */, 0 /* lines */, 0 /* limit_ns */);
3976
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003977 return 0;
3978}
3979
Haynes Mathew George352f27b2013-07-26 00:00:15 -07003980static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
3981{
3982 int ret = 0;
3983 char value[32];
ApurupaPattapu2e084df2013-12-18 15:47:59 -08003984
Haynes Mathew George352f27b2013-07-26 00:00:15 -07003985 if (!out || !parms) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08003986 ALOGE("%s: return invalid ",__func__);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07003987 return -EINVAL;
3988 }
3989
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +05303990 ret = audio_extn_parse_compress_metadata(out, parms);
Weiyin Jiang18ac4e92015-03-15 15:03:40 +08003991
Haynes Mathew George352f27b2013-07-26 00:00:15 -07003992 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
3993 if (ret >= 0) {
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +05303994 out->gapless_mdata.encoder_delay = atoi(value); //whats a good limit check?
Haynes Mathew George352f27b2013-07-26 00:00:15 -07003995 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07003996 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
3997 if (ret >= 0) {
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +05303998 out->gapless_mdata.encoder_padding = atoi(value);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07003999 }
4000
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004001 ALOGV("%s new encoder delay %u and padding %u", __func__,
4002 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
4003
4004 return 0;
4005}
4006
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07004007static bool output_drives_call(struct audio_device *adev, struct stream_out *out)
4008{
4009 return out == adev->primary_output || out == adev->voice_tx_output;
4010}
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004011
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304012// note: this call is safe only if the stream_cb is
4013// removed first in close_output_stream (as is done now).
4014static void out_snd_mon_cb(void * stream, struct str_parms * parms)
4015{
4016 if (!stream || !parms)
4017 return;
4018
4019 struct stream_out *out = (struct stream_out *)stream;
4020 struct audio_device *adev = out->dev;
4021
4022 card_status_t status;
4023 int card;
4024 if (parse_snd_card_status(parms, &card, &status) < 0)
4025 return;
4026
4027 pthread_mutex_lock(&adev->lock);
4028 bool valid_cb = (card == adev->snd_card);
4029 pthread_mutex_unlock(&adev->lock);
4030
4031 if (!valid_cb)
4032 return;
4033
4034 lock_output_stream(out);
4035 if (out->card_status != status)
4036 out->card_status = status;
4037 pthread_mutex_unlock(&out->lock);
4038
4039 ALOGI("out_snd_mon_cb for card %d usecase %s, status %s", card,
4040 use_case_table[out->usecase],
4041 status == CARD_STATUS_OFFLINE ? "offline" : "online");
4042
Aditya Bavanari59ebed42019-02-05 17:44:57 +05304043 if (status == CARD_STATUS_OFFLINE) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304044 out_on_error(stream);
Aditya Bavanari59ebed42019-02-05 17:44:57 +05304045 if (voice_is_call_state_active(adev) &&
4046 out == adev->primary_output) {
4047 ALOGD("%s: SSR/PDR occurred, end all calls\n", __func__);
4048 pthread_mutex_lock(&adev->lock);
4049 voice_stop_call(adev);
4050 adev->mode = AUDIO_MODE_NORMAL;
4051 pthread_mutex_unlock(&adev->lock);
4052 }
4053 }
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304054 return;
4055}
4056
Kevin Rocardfce19002017-08-07 19:21:36 -07004057static int get_alive_usb_card(struct str_parms* parms) {
4058 int card;
4059 if ((str_parms_get_int(parms, "card", &card) >= 0) &&
4060 !audio_extn_usb_alive(card)) {
4061 return card;
4062 }
4063 return -ENODEV;
4064}
4065
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004066static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
4067{
4068 struct stream_out *out = (struct stream_out *)stream;
4069 struct audio_device *adev = out->dev;
4070 struct str_parms *parms;
4071 char value[32];
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08004072 int ret = 0, val = 0, err;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304073 bool bypass_a2dp = false;
Garmond Leung5fd0b552018-04-17 11:56:12 -07004074 bool reconfig = false;
4075 unsigned long service_interval = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004076
sangwoobc677242013-08-08 16:53:43 +09004077 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07004078 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004079 parms = str_parms_create_str(kvpairs);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05304080 if (!parms)
4081 goto error;
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08004082 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
4083 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004084 val = atoi(value);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07004085 lock_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08004086 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004087
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07004088 /*
Weiyin Jiang4256eeb2016-05-19 13:28:30 +08004089 * When HDMI cable is unplugged the music playback is paused and
4090 * the policy manager sends routing=0. But the audioflinger continues
4091 * to write data until standby time (3sec). As the HDMI core is
4092 * turned off, the write gets blocked.
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07004093 * Avoid this by routing audio to speaker until standby.
4094 */
Weiyin Jiang4256eeb2016-05-19 13:28:30 +08004095 if ((out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL) &&
4096 (val == AUDIO_DEVICE_NONE) &&
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05304097 !audio_extn_passthru_is_passthrough_stream(out) &&
Weiyin Jiang4256eeb2016-05-19 13:28:30 +08004098 (platform_get_edid_info(adev->platform) != 0) /* HDMI disconnected */) {
4099 val = AUDIO_DEVICE_OUT_SPEAKER;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07004100 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05304101 /*
4102 * When A2DP is disconnected the
4103 * music playback is paused and the policy manager sends routing=0
Florian Pfister1a84f312018-07-19 14:38:18 +02004104 * But the audioflinger continues to write data until standby time
Naresh Tanniru9d027a62015-03-13 01:32:10 +05304105 * (3sec). As BT is turned off, the write gets blocked.
4106 * Avoid this by routing audio to speaker until standby.
4107 */
Naresh Tanniruf7e9e632016-11-04 14:54:20 -07004108 if ((out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) &&
Aniket Kumar Lata1fb11cb2017-09-08 13:48:55 -07004109 (val == AUDIO_DEVICE_NONE) &&
Florian Pfister1a84f312018-07-19 14:38:18 +02004110 !audio_extn_a2dp_source_is_ready()) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05304111 val = AUDIO_DEVICE_OUT_SPEAKER;
4112 }
Sharad Sangled0a50b22018-04-05 23:28:32 +05304113 /*
4114 * When USB headset is disconnected the music platback paused
4115 * and the policy manager send routing=0. But if the USB is connected
4116 * back before the standby time, AFE is not closed and opened
4117 * when USB is connected back. So routing to speker will guarantee
4118 * AFE reconfiguration and AFE will be opend once USB is connected again
4119 */
4120 if ((out->devices & AUDIO_DEVICE_OUT_ALL_USB) &&
4121 (val == AUDIO_DEVICE_NONE) &&
4122 !audio_extn_usb_connected(parms)) {
4123 val = AUDIO_DEVICE_OUT_SPEAKER;
4124 }
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05304125 /* To avoid a2dp to sco overlapping / BT device improper state
4126 * check with BT lib about a2dp streaming support before routing
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05304127 */
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05304128 if (val & AUDIO_DEVICE_OUT_ALL_A2DP) {
Florian Pfister1a84f312018-07-19 14:38:18 +02004129 if (!audio_extn_a2dp_source_is_ready()) {
Aalique Grahame22e49102018-12-18 14:23:57 -08004130 if (val &
4131 (AUDIO_DEVICE_OUT_SPEAKER | AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05304132 //combo usecase just by pass a2dp
4133 ALOGW("%s: A2DP profile is not ready,routing to speaker only", __func__);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304134 bypass_a2dp = true;
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05304135 } else {
4136 ALOGE("%s: A2DP profile is not ready,ignoring routing request", __func__);
4137 /* update device to a2dp and don't route as BT returned error
4138 * However it is still possible a2dp routing called because
4139 * of current active device disconnection (like wired headset)
4140 */
4141 out->devices = val;
4142 pthread_mutex_unlock(&out->lock);
4143 pthread_mutex_unlock(&adev->lock);
4144 goto error;
4145 }
4146 }
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05304147 }
Kevin Rocardfce19002017-08-07 19:21:36 -07004148
4149 audio_devices_t new_dev = val;
4150
4151 // Workaround: If routing to an non existing usb device, fail gracefully
4152 // The routing request will otherwise block during 10 second
4153 int card;
4154 if (audio_is_usb_out_device(new_dev) &&
4155 (card = get_alive_usb_card(parms)) >= 0) {
4156
4157 ALOGW("out_set_parameters() ignoring rerouting to non existing USB card %d", card);
4158 pthread_mutex_unlock(&adev->lock);
4159 pthread_mutex_unlock(&out->lock);
4160 ret = -ENOSYS;
4161 goto routing_fail;
4162 }
4163
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07004164 /*
4165 * select_devices() call below switches all the usecases on the same
4166 * backend to the new device. Refer to check_usecases_codec_backend() in
4167 * the select_devices(). But how do we undo this?
4168 *
4169 * For example, music playback is active on headset (deep-buffer usecase)
4170 * and if we go to ringtones and select a ringtone, low-latency usecase
4171 * will be started on headset+speaker. As we can't enable headset+speaker
4172 * and headset devices at the same time, select_devices() switches the music
4173 * playback to headset+speaker while starting low-lateny usecase for ringtone.
4174 * So when the ringtone playback is completed, how do we undo the same?
4175 *
4176 * We are relying on the out_set_parameters() call on deep-buffer output,
4177 * once the ringtone playback is ended.
4178 * NOTE: We should not check if the current devices are same as new devices.
4179 * Because select_devices() must be called to switch back the music
4180 * playback to headset.
4181 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08004182 if (val != 0) {
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004183 audio_devices_t new_dev = val;
4184 bool same_dev = out->devices == new_dev;
4185 out->devices = new_dev;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07004186
Ravi Kumar Alamandabe149392014-10-20 17:07:43 -07004187 if (output_drives_call(adev, out)) {
kunleiz61b9c2e2017-11-01 13:47:23 +08004188 if (!voice_is_call_state_active(adev)) {
Ravi Kumar Alamandabe149392014-10-20 17:07:43 -07004189 if (adev->mode == AUDIO_MODE_IN_CALL) {
4190 adev->current_call_output = out;
Garmond Leung5fd0b552018-04-17 11:56:12 -07004191 if (audio_is_usb_out_device(out->devices & AUDIO_DEVICE_OUT_ALL_USB)) {
4192 service_interval = audio_extn_usb_find_service_interval(true, true /*playback*/);
4193 audio_extn_usb_set_service_interval(true /*playback*/,
4194 service_interval,
4195 &reconfig);
4196 ALOGD("%s, svc_int(%ld),reconfig(%d)",__func__,service_interval, reconfig);
4197 }
4198 ret = voice_start_call(adev);
Ravi Kumar Alamandabe149392014-10-20 17:07:43 -07004199 }
4200 } else {
4201 adev->current_call_output = out;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07004202 voice_update_devices_for_all_voice_usecases(adev);
Ravi Kumar Alamandabe149392014-10-20 17:07:43 -07004203 }
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08004204 }
Shiv Maliyappanahalli6b32c4c2015-11-04 18:10:20 -08004205
4206 if (!out->standby) {
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004207 if (!same_dev) {
4208 ALOGV("update routing change");
Sudheer Papothi80266982016-08-16 02:36:18 +05304209 audio_extn_perf_lock_acquire(&adev->perf_lock_handle, 0,
4210 adev->perf_lock_opts,
4211 adev->perf_lock_opts_size);
Haynes Mathew George822b5492016-07-01 16:57:24 -07004212 if (adev->adm_on_routing_change)
4213 adev->adm_on_routing_change(adev->adm_data,
4214 out->handle);
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004215 }
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304216 if (!bypass_a2dp) {
4217 select_devices(adev, out->usecase);
4218 } else {
Aalique Grahame22e49102018-12-18 14:23:57 -08004219 if (new_dev & AUDIO_DEVICE_OUT_SPEAKER_SAFE)
4220 out->devices = AUDIO_DEVICE_OUT_SPEAKER_SAFE;
4221 else
4222 out->devices = AUDIO_DEVICE_OUT_SPEAKER;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304223 select_devices(adev, out->usecase);
4224 out->devices = new_dev;
4225 }
Vignesh Kulothungan3b5fae52017-09-25 12:16:30 -07004226
4227 if (!same_dev) {
4228 // on device switch force swap, lower functions will make sure
4229 // to check if swap is allowed or not.
4230 platform_set_swap_channels(adev, true);
Sudheer Papothi80266982016-08-16 02:36:18 +05304231 audio_extn_perf_lock_release(&adev->perf_lock_handle);
Vignesh Kulothungan3b5fae52017-09-25 12:16:30 -07004232 }
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304233 if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
4234 out->a2dp_compress_mute &&
Florian Pfister1a84f312018-07-19 14:38:18 +02004235 (!(out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) || audio_extn_a2dp_source_is_ready())) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304236 pthread_mutex_lock(&out->compr_mute_lock);
4237 out->a2dp_compress_mute = false;
4238 out_set_compr_volume(&out->stream, out->volume_l, out->volume_r);
4239 pthread_mutex_unlock(&out->compr_mute_lock);
kunleizba786432018-01-26 15:31:59 +08004240 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP) {
4241 out_set_voip_volume(&out->stream, out->volume_l, out->volume_r);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304242 }
Shiv Maliyappanahalli6b32c4c2015-11-04 18:10:20 -08004243 }
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08004244 }
4245
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004246 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08004247 pthread_mutex_unlock(&out->lock);
Aalique Grahame22e49102018-12-18 14:23:57 -08004248
4249 /*handles device and call state changes*/
4250 audio_extn_extspk_update(adev->extspk);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004251 }
Kevin Rocardfce19002017-08-07 19:21:36 -07004252 routing_fail:
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07004253
4254 if (out == adev->primary_output) {
4255 pthread_mutex_lock(&adev->lock);
4256 audio_extn_set_parameters(adev, parms);
4257 pthread_mutex_unlock(&adev->lock);
4258 }
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07004259 if (is_offload_usecase(out->usecase)) {
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07004260 lock_output_stream(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004261 parse_compress_metadata(out, parms);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08004262
4263 audio_extn_dts_create_state_notifier_node(out->usecase);
4264 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
4265 popcount(out->channel_mask),
4266 out->playback_started);
4267
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08004268 pthread_mutex_unlock(&out->lock);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004269 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07004270
Surendar Karkaf51b5842018-04-26 11:28:38 +05304271 err = str_parms_get_str(parms, AUDIO_PARAMETER_DUAL_MONO, value,
4272 sizeof(value));
4273 if (err >= 0) {
4274 if (!strncmp("true", value, sizeof("true")) || atoi(value))
4275 audio_extn_send_dual_mono_mixing_coefficients(out);
4276 }
4277
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +05304278 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_PROFILE, value, sizeof(value));
4279 if (err >= 0) {
4280 strlcpy(out->profile, value, sizeof(out->profile));
4281 ALOGV("updating stream profile with value '%s'", out->profile);
4282 lock_output_stream(out);
4283 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
4284 &adev->streams_output_cfg_list,
Aalique Grahame65780b52017-09-27 14:59:56 -07004285 out->devices, out->flags, out->hal_op_format,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +05304286 out->sample_rate, out->bit_width,
4287 out->channel_mask, out->profile,
4288 &out->app_type_cfg);
4289 pthread_mutex_unlock(&out->lock);
4290 }
4291
Alexy Joseph98988832017-01-13 14:56:59 -08004292 //suspend, resume handling block
Aniket Kumar Lata932f4872017-11-06 18:29:44 -08004293 //remove QOS only if vendor.audio.hal.dynamic.qos.config.supported is set to true
4294 // and vendor.audio.hal.output.suspend.supported is set to true
4295 if (out->hal_output_suspend_supported && out->dynamic_pm_qos_config_supported) {
Alexy Joseph98988832017-01-13 14:56:59 -08004296 //check suspend parameter only for low latency and if the property
4297 //is enabled
4298 if (str_parms_get_str(parms, "suspend_playback", value, sizeof(value)) >= 0) {
4299 ALOGI("%s: got suspend_playback %s", __func__, value);
4300 lock_output_stream(out);
4301 if (!strncmp(value, "false", 5)) {
4302 //suspend_playback=false is supposed to set QOS value back to 75%
4303 //the mixer control sent with value Enable will achieve that
4304 ret = audio_route_apply_and_update_path(adev->audio_route, out->pm_qos_mixer_path);
4305 } else if (!strncmp (value, "true", 4)) {
4306 //suspend_playback=true is supposed to remove QOS value
4307 //resetting the mixer control will set the default value
4308 //for the mixer control which is Disable and this removes the QOS vote
4309 ret = audio_route_reset_and_update_path(adev->audio_route, out->pm_qos_mixer_path);
4310 } else {
4311 ALOGE("%s: Wrong value sent for suspend_playback, expected true/false,"
4312 " got %s", __func__, value);
4313 ret = -1;
4314 }
4315
4316 if (ret != 0) {
4317 ALOGE("%s: %s mixer ctl failed with %d, ignore suspend/resume setparams",
4318 __func__, out->pm_qos_mixer_path, ret);
4319 }
4320
4321 pthread_mutex_unlock(&out->lock);
4322 }
4323 }
4324 //end suspend, resume handling block
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004325 str_parms_destroy(parms);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05304326error:
Eric Laurent994a6932013-07-17 11:51:42 -07004327 ALOGV("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004328 return ret;
4329}
4330
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004331static bool stream_get_parameter_channels(struct str_parms *query,
4332 struct str_parms *reply,
4333 audio_channel_mask_t *supported_channel_masks) {
4334 int ret = -1;
4335 char value[512];
4336 bool first = true;
4337 size_t i, j;
4338
4339 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) {
4340 ret = 0;
4341 value[0] = '\0';
4342 i = 0;
4343 while (supported_channel_masks[i] != 0) {
4344 for (j = 0; j < ARRAY_SIZE(channels_name_to_enum_table); j++) {
4345 if (channels_name_to_enum_table[j].value == supported_channel_masks[i]) {
4346 if (!first)
Satya Krishna Pindiprolib22ac722017-10-09 15:44:16 +05304347 strlcat(value, "|", sizeof(value));
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004348
Satya Krishna Pindiprolib22ac722017-10-09 15:44:16 +05304349 strlcat(value, channels_name_to_enum_table[j].name, sizeof(value));
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004350 first = false;
4351 break;
4352 }
4353 }
4354 i++;
4355 }
4356 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
4357 }
4358 return ret == 0;
4359}
4360
4361static bool stream_get_parameter_formats(struct str_parms *query,
4362 struct str_parms *reply,
4363 audio_format_t *supported_formats) {
4364 int ret = -1;
4365 char value[256];
4366 size_t i, j;
4367 bool first = true;
4368
4369 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) {
4370 ret = 0;
4371 value[0] = '\0';
4372 i = 0;
4373 while (supported_formats[i] != 0) {
4374 for (j = 0; j < ARRAY_SIZE(formats_name_to_enum_table); j++) {
4375 if (formats_name_to_enum_table[j].value == supported_formats[i]) {
4376 if (!first) {
Satya Krishna Pindiprolib22ac722017-10-09 15:44:16 +05304377 strlcat(value, "|", sizeof(value));
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004378 }
Satya Krishna Pindiprolib22ac722017-10-09 15:44:16 +05304379 strlcat(value, formats_name_to_enum_table[j].name, sizeof(value));
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004380 first = false;
4381 break;
4382 }
4383 }
4384 i++;
4385 }
4386 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_FORMATS, value);
4387 }
4388 return ret == 0;
4389}
4390
4391static bool stream_get_parameter_rates(struct str_parms *query,
4392 struct str_parms *reply,
4393 uint32_t *supported_sample_rates) {
4394
4395 int i;
4396 char value[256];
4397 int ret = -1;
4398 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) {
4399 ret = 0;
4400 value[0] = '\0';
4401 i=0;
4402 int cursor = 0;
4403 while (supported_sample_rates[i]) {
4404 int avail = sizeof(value) - cursor;
4405 ret = snprintf(value + cursor, avail, "%s%d",
4406 cursor > 0 ? "|" : "",
4407 supported_sample_rates[i]);
4408 if (ret < 0 || ret >= avail) {
4409 // if cursor is at the last element of the array
4410 // overwrite with \0 is duplicate work as
4411 // snprintf already put a \0 in place.
4412 // else
4413 // we had space to write the '|' at value[cursor]
4414 // (which will be overwritten) or no space to fill
4415 // the first element (=> cursor == 0)
4416 value[cursor] = '\0';
4417 break;
4418 }
4419 cursor += ret;
4420 ++i;
4421 }
4422 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES,
4423 value);
4424 }
4425 return ret >= 0;
4426}
4427
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004428static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
4429{
4430 struct stream_out *out = (struct stream_out *)stream;
4431 struct str_parms *query = str_parms_create_str(keys);
Alexy Josephaee4fdd2016-01-29 13:02:07 -08004432 char *str = (char*) NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004433 char value[256];
4434 struct str_parms *reply = str_parms_create();
4435 size_t i, j;
4436 int ret;
4437 bool first = true;
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07004438
4439 if (!query || !reply) {
Alexy Josephaee4fdd2016-01-29 13:02:07 -08004440 if (reply) {
4441 str_parms_destroy(reply);
4442 }
4443 if (query) {
4444 str_parms_destroy(query);
4445 }
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07004446 ALOGE("out_get_parameters: failed to allocate mem for query or reply");
4447 return NULL;
4448 }
4449
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004450 ALOGV("%s: %s enter: keys - %s", __func__, use_case_table[out->usecase], keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004451 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
4452 if (ret >= 0) {
4453 value[0] = '\0';
4454 i = 0;
4455 while (out->supported_channel_masks[i] != 0) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004456 for (j = 0; j < ARRAY_SIZE(channels_name_to_enum_table); j++) {
4457 if (channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004458 if (!first) {
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -08004459 strlcat(value, "|", sizeof(value));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004460 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004461 strlcat(value, channels_name_to_enum_table[j].name, sizeof(value));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004462 first = false;
4463 break;
4464 }
4465 }
4466 i++;
4467 }
4468 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
4469 str = str_parms_to_str(reply);
4470 } else {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08004471 voice_extn_out_get_parameters(out, query, reply);
4472 str = str_parms_to_str(reply);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004473 }
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004474
Alexy Joseph62142aa2015-11-16 15:10:34 -08004475
4476 ret = str_parms_get_str(query, "is_direct_pcm_track", value, sizeof(value));
4477 if (ret >= 0) {
4478 value[0] = '\0';
Dhananjay Kumarac341582017-02-23 23:42:25 +05304479 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
4480 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Alexy Joseph62142aa2015-11-16 15:10:34 -08004481 ALOGV("in direct_pcm");
Satya Krishna Pindiprolib6655542017-07-03 19:38:19 +05304482 strlcat(value, "true", sizeof(value));
Alexy Joseph62142aa2015-11-16 15:10:34 -08004483 } else {
4484 ALOGV("not in direct_pcm");
Sharad Sangle3dd5a4a2015-12-10 18:39:17 +05304485 strlcat(value, "false", sizeof(value));
Alexy Joseph62142aa2015-11-16 15:10:34 -08004486 }
4487 str_parms_add_str(reply, "is_direct_pcm_track", value);
Alexy Josephaee4fdd2016-01-29 13:02:07 -08004488 if (str)
4489 free(str);
Alexy Joseph62142aa2015-11-16 15:10:34 -08004490 str = str_parms_to_str(reply);
4491 }
4492
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004493 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS, value, sizeof(value));
4494 if (ret >= 0) {
4495 value[0] = '\0';
4496 i = 0;
4497 first = true;
4498 while (out->supported_formats[i] != 0) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004499 for (j = 0; j < ARRAY_SIZE(formats_name_to_enum_table); j++) {
4500 if (formats_name_to_enum_table[j].value == out->supported_formats[i]) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004501 if (!first) {
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -08004502 strlcat(value, "|", sizeof(value));
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004503 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004504 strlcat(value, formats_name_to_enum_table[j].name, sizeof(value));
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004505 first = false;
4506 break;
4507 }
4508 }
4509 i++;
4510 }
4511 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_FORMATS, value);
Alexy Josephaee4fdd2016-01-29 13:02:07 -08004512 if (str)
4513 free(str);
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004514 str = str_parms_to_str(reply);
4515 }
Mingming Yin3a941d42016-02-17 18:08:05 -08004516
4517 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES, value, sizeof(value));
4518 if (ret >= 0) {
4519 value[0] = '\0';
4520 i = 0;
4521 first = true;
4522 while (out->supported_sample_rates[i] != 0) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004523 for (j = 0; j < ARRAY_SIZE(out_sample_rates_name_to_enum_table); j++) {
4524 if (out_sample_rates_name_to_enum_table[j].value == out->supported_sample_rates[i]) {
Mingming Yin3a941d42016-02-17 18:08:05 -08004525 if (!first) {
4526 strlcat(value, "|", sizeof(value));
4527 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004528 strlcat(value, out_sample_rates_name_to_enum_table[j].name, sizeof(value));
Mingming Yin3a941d42016-02-17 18:08:05 -08004529 first = false;
4530 break;
4531 }
4532 }
4533 i++;
4534 }
4535 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES, value);
4536 if (str)
4537 free(str);
4538 str = str_parms_to_str(reply);
4539 }
4540
Alexy Joseph98988832017-01-13 14:56:59 -08004541 if (str_parms_get_str(query, "supports_hw_suspend", value, sizeof(value)) >= 0) {
4542 //only low latency track supports suspend_resume
4543 str_parms_add_int(reply, "supports_hw_suspend",
Aniket Kumar Lata932f4872017-11-06 18:29:44 -08004544 (out->hal_output_suspend_supported));
Alexy Joseph98988832017-01-13 14:56:59 -08004545 if (str)
4546 free(str);
4547 str = str_parms_to_str(reply);
4548 }
4549
4550
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004551 str_parms_destroy(query);
4552 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07004553 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004554 return str;
4555}
4556
4557static uint32_t out_get_latency(const struct audio_stream_out *stream)
4558{
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004559 uint32_t period_ms;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004560 struct stream_out *out = (struct stream_out *)stream;
Alexy Josephaa54c872014-12-03 02:46:47 -08004561 uint32_t latency = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004562
Alexy Josephaa54c872014-12-03 02:46:47 -08004563 if (is_offload_usecase(out->usecase)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05304564 lock_output_stream(out);
4565 latency = audio_extn_utils_compress_get_dsp_latency(out);
4566 pthread_mutex_unlock(&out->lock);
Haynes Mathew George16081042017-05-31 17:16:49 -07004567 } else if ((out->realtime) ||
4568 (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP)) {
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004569 // since the buffer won't be filled up faster than realtime,
4570 // return a smaller number
4571 if (out->config.rate)
4572 period_ms = (out->af_period_multiplier * out->config.period_size *
4573 1000) / (out->config.rate);
4574 else
4575 period_ms = 0;
4576 latency = period_ms + platform_render_latency(out->usecase)/1000;
Alexy Josephaa54c872014-12-03 02:46:47 -08004577 } else {
4578 latency = (out->config.period_count * out->config.period_size * 1000) /
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004579 (out->config.rate);
Alexy Josephaa54c872014-12-03 02:46:47 -08004580 }
4581
yidongh0515e042017-07-06 15:00:34 +08004582 if (AUDIO_DEVICE_OUT_ALL_A2DP & out->devices)
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08004583 latency += audio_extn_a2dp_get_encoder_latency();
4584
Anish Kumar50ebcbf2014-12-09 04:01:39 +05304585 ALOGV("%s: Latency %d", __func__, latency);
Alexy Josephaa54c872014-12-03 02:46:47 -08004586 return latency;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004587}
4588
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05304589static float AmpToDb(float amplification)
4590{
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05304591 float db = DSD_VOLUME_MIN_DB;
4592 if (amplification > 0) {
4593 db = 20 * log10(amplification);
4594 if(db < DSD_VOLUME_MIN_DB)
4595 return DSD_VOLUME_MIN_DB;
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05304596 }
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05304597 return db;
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05304598}
4599
Arun Mirpuri5d170872019-03-26 13:21:31 -07004600static int out_set_mmap_volume(struct audio_stream_out *stream, float left,
4601 float right)
4602{
4603 struct stream_out *out = (struct stream_out *)stream;
4604 long volume = 0;
4605 char mixer_ctl_name[128] = "";
4606 struct audio_device *adev = out->dev;
4607 struct mixer_ctl *ctl = NULL;
4608 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
4609 PCM_PLAYBACK);
4610
4611 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
4612 "Playback %d Volume", pcm_device_id);
4613 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
4614 if (!ctl) {
4615 ALOGE("%s: Could not get ctl for mixer cmd - %s",
4616 __func__, mixer_ctl_name);
4617 return -EINVAL;
4618 }
4619 if (left != right)
4620 ALOGW("%s: Left and right channel volume mismatch:%f,%f",
4621 __func__, left, right);
4622 volume = (long)(left * (MMAP_PLAYBACK_VOLUME_MAX*1.0));
4623 if (mixer_ctl_set_value(ctl, 0, volume) < 0){
4624 ALOGE("%s:ctl for mixer cmd - %s, volume %ld returned error",
4625 __func__, mixer_ctl_name, volume);
4626 return -EINVAL;
4627 }
4628 return 0;
4629}
4630
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304631static int out_set_compr_volume(struct audio_stream_out *stream, float left,
4632 float right)
4633{
4634 struct stream_out *out = (struct stream_out *)stream;
Manish Dewangan338c50a2017-09-12 15:22:03 +05304635 long volume[2];
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304636 char mixer_ctl_name[128];
4637 struct audio_device *adev = out->dev;
4638 struct mixer_ctl *ctl;
4639 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
4640 PCM_PLAYBACK);
4641
4642 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
4643 "Compress Playback %d Volume", pcm_device_id);
4644 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
4645 if (!ctl) {
4646 ALOGE("%s: Could not get ctl for mixer cmd - %s",
4647 __func__, mixer_ctl_name);
4648 return -EINVAL;
4649 }
4650 ALOGE("%s:ctl for mixer cmd - %s, left %f, right %f",
4651 __func__, mixer_ctl_name, left, right);
4652 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
4653 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
4654 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
4655
4656 return 0;
4657}
4658
Zhou Song2b8f28f2017-09-11 10:51:38 +08004659static int out_set_voip_volume(struct audio_stream_out *stream, float left,
4660 float right)
4661{
4662 struct stream_out *out = (struct stream_out *)stream;
4663 char mixer_ctl_name[] = "App Type Gain";
4664 struct audio_device *adev = out->dev;
4665 struct mixer_ctl *ctl;
Manish Dewangan338c50a2017-09-12 15:22:03 +05304666 long set_values[4];
Zhou Song2b8f28f2017-09-11 10:51:38 +08004667
4668 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
4669 if (!ctl) {
4670 ALOGE("%s: Could not get ctl for mixer cmd - %s",
4671 __func__, mixer_ctl_name);
4672 return -EINVAL;
4673 }
4674
4675 set_values[0] = 0; //0: Rx Session 1:Tx Session
4676 set_values[1] = out->app_type_cfg.app_type;
Manish Dewangan338c50a2017-09-12 15:22:03 +05304677 set_values[2] = (long)(left * VOIP_PLAYBACK_VOLUME_MAX);
4678 set_values[3] = (long)(right * VOIP_PLAYBACK_VOLUME_MAX);
Zhou Song2b8f28f2017-09-11 10:51:38 +08004679
4680 mixer_ctl_set_array(ctl, set_values, ARRAY_SIZE(set_values));
4681 return 0;
4682}
4683
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05304684static int out_set_pcm_volume(struct audio_stream_out *stream, float left,
4685 float right)
4686{
4687 struct stream_out *out = (struct stream_out *)stream;
4688 /* Volume control for pcm playback */
4689 if (left != right) {
4690 return -EINVAL;
4691 } else {
4692 char mixer_ctl_name[128];
4693 struct audio_device *adev = out->dev;
4694 struct mixer_ctl *ctl;
4695 int pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
4696 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name), "Playback %d Volume", pcm_device_id);
4697 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
4698 if (!ctl) {
4699 ALOGE("%s : Could not get ctl for mixer cmd - %s", __func__, mixer_ctl_name);
4700 return -EINVAL;
4701 }
4702
4703 int volume = (int) (left * PCM_PLAYBACK_VOLUME_MAX);
4704 int ret = mixer_ctl_set_value(ctl, 0, volume);
4705 if (ret < 0) {
4706 ALOGE("%s: Could not set ctl, error:%d ", __func__, ret);
4707 return -EINVAL;
4708 }
4709
4710 ALOGV("%s : Pcm set volume value %d left %f", __func__, volume, left);
4711
4712 return 0;
4713 }
4714}
4715
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004716static int out_set_volume(struct audio_stream_out *stream, float left,
4717 float right)
4718{
Eric Laurenta9024de2013-04-04 09:19:12 -07004719 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004720 int volume[2];
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304721 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004722
Arun Mirpuri5d170872019-03-26 13:21:31 -07004723 ALOGD("%s: called with left_vol=%f, right_vol=%f", __func__, left, right);
Eric Laurenta9024de2013-04-04 09:19:12 -07004724 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
4725 /* only take left channel into account: the API is for stereo anyway */
4726 out->muted = (left == 0.0f);
4727 return 0;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07004728 } else if (is_offload_usecase(out->usecase)) {
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05304729 if (audio_extn_passthru_is_passthrough_stream(out)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004730 /*
4731 * Set mute or umute on HDMI passthrough stream.
4732 * Only take left channel into account.
4733 * Mute is 0 and unmute 1
4734 */
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05304735 audio_extn_passthru_set_volume(out, (left == 0.0f));
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05304736 } else if (out->format == AUDIO_FORMAT_DSD){
4737 char mixer_ctl_name[128] = "DSD Volume";
4738 struct audio_device *adev = out->dev;
4739 struct mixer_ctl *ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
4740
4741 if (!ctl) {
4742 ALOGE("%s: Could not get ctl for mixer cmd - %s",
4743 __func__, mixer_ctl_name);
4744 return -EINVAL;
4745 }
Manish Dewangan338c50a2017-09-12 15:22:03 +05304746 volume[0] = (long)(AmpToDb(left));
4747 volume[1] = (long)(AmpToDb(right));
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05304748 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
4749 return 0;
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004750 } else {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304751 pthread_mutex_lock(&out->compr_mute_lock);
Arun Mirpuri5d170872019-03-26 13:21:31 -07004752 ALOGV("%s: compress mute %d", __func__, out->a2dp_compress_mute);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304753 if (!out->a2dp_compress_mute)
4754 ret = out_set_compr_volume(stream, left, right);
4755 out->volume_l = left;
4756 out->volume_r = right;
4757 pthread_mutex_unlock(&out->compr_mute_lock);
4758 return ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004759 }
Vikram Panduranga93f080e2017-06-07 18:16:14 -07004760 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP) {
Aalique Grahame22e49102018-12-18 14:23:57 -08004761 out->app_type_cfg.gain[0] = (int)(left * VOIP_PLAYBACK_VOLUME_MAX);
4762 out->app_type_cfg.gain[1] = (int)(right * VOIP_PLAYBACK_VOLUME_MAX);
4763 if (!out->standby) {
4764 audio_extn_utils_send_app_type_gain(out->dev,
4765 out->app_type_cfg.app_type,
4766 &out->app_type_cfg.gain[0]);
Zhou Song2b8f28f2017-09-11 10:51:38 +08004767 ret = out_set_voip_volume(stream, left, right);
Aalique Grahame22e49102018-12-18 14:23:57 -08004768 }
Zhou Song2b8f28f2017-09-11 10:51:38 +08004769 out->volume_l = left;
4770 out->volume_r = right;
4771 return ret;
Arun Mirpuri5d170872019-03-26 13:21:31 -07004772 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
4773 ALOGV("%s: MMAP set volume called", __func__);
4774 if (!out->standby)
4775 ret = out_set_mmap_volume(stream, left, right);
4776 out->volume_l = left;
4777 out->volume_r = right;
4778 return ret;
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05304779 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_LOW_LATENCY ||
Ramu Gottipati36547092018-12-28 11:32:09 +05304780 out->usecase == USECASE_AUDIO_PLAYBACK_DEEP_BUFFER ||
4781 out->usecase == USECASE_AUDIO_PLAYBACK_ULL) {
Ramu Gottipati97bdcfb2018-04-13 17:58:24 +05304782 /* Volume control for pcm playback */
4783 if (!out->standby)
4784 ret = out_set_pcm_volume(stream, left, right);
4785 else
4786 out->apply_volume = true;
4787
4788 out->volume_l = left;
4789 out->volume_r = right;
4790 return ret;
Eric Laurenta9024de2013-04-04 09:19:12 -07004791 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004792
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004793 return -ENOSYS;
4794}
4795
Zhou Songc9672822017-08-16 16:01:39 +08004796static void update_frames_written(struct stream_out *out, size_t bytes)
4797{
4798 size_t bpf = 0;
4799
4800 if (is_offload_usecase(out->usecase) && !out->non_blocking &&
4801 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD))
4802 bpf = 1;
4803 else if (!is_offload_usecase(out->usecase))
4804 bpf = audio_bytes_per_sample(out->format) *
4805 audio_channel_count_from_out_mask(out->channel_mask);
Zhou Song48453a02018-01-10 17:50:59 +08004806
4807 pthread_mutex_lock(&out->position_query_lock);
4808 if (bpf != 0) {
Zhou Songc9672822017-08-16 16:01:39 +08004809 out->written += bytes / bpf;
Zhou Song48453a02018-01-10 17:50:59 +08004810 clock_gettime(CLOCK_MONOTONIC, &out->writeAt);
4811 }
4812 pthread_mutex_unlock(&out->position_query_lock);
Zhou Songc9672822017-08-16 16:01:39 +08004813}
4814
Vignesh Kulothungana6927272019-02-20 15:17:07 -08004815int split_and_write_audio_haptic_data(struct stream_out *out,
4816 const void *buffer, size_t bytes_to_write)
4817{
4818 struct audio_device *adev = out->dev;
4819
4820 int ret = 0;
4821 size_t channel_count = audio_channel_count_from_out_mask(out->channel_mask);
4822 size_t bytes_per_sample = audio_bytes_per_sample(out->format);
4823 size_t frame_size = channel_count * bytes_per_sample;
4824 size_t frame_count = bytes_to_write / frame_size;
4825
4826 bool force_haptic_path =
4827 property_get_bool("vendor.audio.test_haptic", false);
4828
4829 // extract Haptics data from Audio buffer
4830 bool alloc_haptic_buffer = false;
4831 int haptic_channel_count = adev->haptics_config.channels;
4832 size_t haptic_frame_size = bytes_per_sample * haptic_channel_count;
4833 size_t audio_frame_size = frame_size - haptic_frame_size;
4834 size_t total_haptic_buffer_size = frame_count * haptic_frame_size;
4835
4836 if (adev->haptic_buffer == NULL) {
4837 alloc_haptic_buffer = true;
4838 } else if (adev->haptic_buffer_size < total_haptic_buffer_size) {
4839 free(adev->haptic_buffer);
4840 adev->haptic_buffer_size = 0;
4841 alloc_haptic_buffer = true;
4842 }
4843
4844 if (alloc_haptic_buffer) {
4845 adev->haptic_buffer = (uint8_t *)calloc(1, total_haptic_buffer_size);
4846 adev->haptic_buffer_size = total_haptic_buffer_size;
4847 }
4848
4849 size_t src_index = 0, aud_index = 0, hap_index = 0;
4850 uint8_t *audio_buffer = (uint8_t *)buffer;
4851 uint8_t *haptic_buffer = adev->haptic_buffer;
4852
4853 // This is required for testing only. This works for stereo data only.
4854 // One channel is fed to audio stream and other to haptic stream for testing.
4855 if (force_haptic_path)
4856 audio_frame_size = haptic_frame_size = bytes_per_sample;
4857
4858 for (size_t i = 0; i < frame_count; i++) {
4859 memcpy(audio_buffer + aud_index, audio_buffer + src_index,
4860 audio_frame_size);
4861 aud_index += audio_frame_size;
4862 src_index += audio_frame_size;
4863
4864 if (adev->haptic_pcm)
4865 memcpy(haptic_buffer + hap_index, audio_buffer + src_index,
4866 haptic_frame_size);
4867 hap_index += haptic_frame_size;
4868 src_index += haptic_frame_size;
4869
4870 // This is required for testing only.
4871 // Discard haptic channel data.
4872 if (force_haptic_path)
4873 src_index += haptic_frame_size;
4874 }
4875
4876 // write to audio pipeline
4877 ret = pcm_write(out->pcm, (void *)audio_buffer,
4878 frame_count * audio_frame_size);
4879
4880 // write to haptics pipeline
4881 if (adev->haptic_pcm)
4882 ret = pcm_write(adev->haptic_pcm, (void *)adev->haptic_buffer,
4883 frame_count * haptic_frame_size);
4884
4885 return ret;
4886}
4887
Aalique Grahame22e49102018-12-18 14:23:57 -08004888#ifdef NO_AUDIO_OUT
4889static ssize_t out_write_for_no_output(struct audio_stream_out *stream,
4890 const void *buffer __unused, size_t bytes)
4891{
4892 struct stream_out *out = (struct stream_out *)stream;
4893
4894 /* No Output device supported other than BT for playback.
4895 * Sleep for the amount of buffer duration
4896 */
4897 lock_output_stream(out);
4898 usleep(bytes * 1000000 / audio_stream_out_frame_size(
4899 (const struct audio_stream_out *)&out->stream) /
4900 out_get_sample_rate(&out->stream.common));
4901 pthread_mutex_unlock(&out->lock);
4902 return bytes;
4903}
4904#endif
4905
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004906static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
4907 size_t bytes)
4908{
4909 struct stream_out *out = (struct stream_out *)stream;
4910 struct audio_device *adev = out->dev;
Eric Laurent6e895242013-09-05 16:10:57 -07004911 ssize_t ret = 0;
Satish Babu Patakokila715b1422017-08-22 14:33:21 +05304912 int channels = 0;
Arun Mirpuri7da752a2018-09-11 18:01:15 -07004913 const size_t frame_size = audio_stream_out_frame_size(stream);
4914 const size_t frames = (frame_size != 0) ? bytes / frame_size : bytes;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004915
Haynes Mathew George380745d2017-10-04 15:27:45 -07004916 ATRACE_BEGIN("out_write");
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07004917 lock_output_stream(out);
Naresh Tanniru4c630392014-05-12 01:05:52 +05304918
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304919 if (CARD_STATUS_OFFLINE == out->card_status) {
Zhou Song0b2e5dc2015-03-16 14:41:38 +08004920
Dhananjay Kumarac341582017-02-23 23:42:25 +05304921 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Ashish Jainbbce4322016-02-16 13:25:27 +05304922 /*during SSR for compress usecase we should return error to flinger*/
Naresh Tanniru80659832014-06-04 18:17:56 +05304923 ALOGD(" copl %s: sound card is not active/SSR state", __func__);
4924 pthread_mutex_unlock(&out->lock);
Haynes Mathew George380745d2017-10-04 15:27:45 -07004925 ATRACE_END();
Naresh Tanniru80659832014-06-04 18:17:56 +05304926 return -ENETRESET;
Ashish Jainbbce4322016-02-16 13:25:27 +05304927 } else {
Ashish Jainbbce4322016-02-16 13:25:27 +05304928 ALOGD(" %s: sound card is not active/SSR state", __func__);
4929 ret= -EIO;
4930 goto exit;
Naresh Tanniru4c630392014-05-12 01:05:52 +05304931 }
4932 }
4933
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05304934 if (audio_extn_passthru_should_drop_data(out)) {
Ashish Jaind84fd6a2016-07-27 12:33:25 +05304935 ALOGV(" %s : Drop data as compress passthrough session is going on", __func__);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05304936 ret = -EIO;
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05304937 goto exit;
4938 }
4939
Haynes Mathew George16081042017-05-31 17:16:49 -07004940 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
4941 ret = -EINVAL;
4942 goto exit;
4943 }
4944
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05304945 if ((out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) &&
4946 !out->is_iec61937_info_available) {
4947
4948 if (!audio_extn_passthru_is_passthrough_stream(out)) {
4949 out->is_iec61937_info_available = true;
4950 } else if (audio_extn_passthru_is_enabled()) {
4951 audio_extn_passthru_update_stream_configuration(adev, out, buffer, bytes);
Manish Dewangan37864bc2017-06-09 12:28:37 +05304952 out->is_iec61937_info_available = true;
Manish Dewangan671a4202017-08-18 17:30:46 +05304953
4954 if((out->format == AUDIO_FORMAT_DTS) ||
4955 (out->format == AUDIO_FORMAT_DTS_HD)) {
4956 ret = audio_extn_passthru_update_dts_stream_configuration(out,
4957 buffer, bytes);
4958 if (ret) {
4959 if (ret != -ENOSYS) {
4960 out->is_iec61937_info_available = false;
4961 ALOGD("iec61937 transmission info not yet updated retry");
4962 }
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05304963 } else if (!out->standby) {
Manish Dewangan671a4202017-08-18 17:30:46 +05304964 /* if stream has started and after that there is
4965 * stream config change (iec transmission config)
4966 * then trigger select_device to update backend configuration.
4967 */
4968 out->stream_config_changed = true;
4969 pthread_mutex_lock(&adev->lock);
4970 select_devices(adev, out->usecase);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05304971 if (!audio_extn_passthru_is_supported_backend_edid_cfg(adev, out)) {
4972 ret = -EINVAL;
4973 goto exit;
4974 }
Manish Dewangan671a4202017-08-18 17:30:46 +05304975 pthread_mutex_unlock(&adev->lock);
4976 out->stream_config_changed = false;
4977 out->is_iec61937_info_available = true;
4978 }
4979 }
Satish Babu Patakokila715b1422017-08-22 14:33:21 +05304980
Garmond Leung317cbf12017-09-13 16:20:50 -07004981 if ((channels < (int)audio_channel_count_from_out_mask(out->channel_mask)) &&
Satish Babu Patakokila715b1422017-08-22 14:33:21 +05304982 (out->compr_config.codec->compr_passthr == PASSTHROUGH) &&
4983 (out->is_iec61937_info_available == true)) {
4984 ALOGE("%s: ERROR: Unsupported channel config in passthrough mode", __func__);
4985 ret = -EINVAL;
4986 goto exit;
4987 }
Manish Dewangan37864bc2017-06-09 12:28:37 +05304988 }
4989 }
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304990
4991 if ((out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) &&
Florian Pfister1a84f312018-07-19 14:38:18 +02004992 (audio_extn_a2dp_source_is_suspended())) {
Aalique Grahame22e49102018-12-18 14:23:57 -08004993 if (!(out->devices &
4994 (AUDIO_DEVICE_OUT_SPEAKER | AUDIO_DEVICE_OUT_SPEAKER_SAFE))) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304995 if (!(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304996 ret = -EIO;
4997 goto exit;
4998 }
4999 }
5000 }
5001
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005002 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07005003 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08005004 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08005005 if (out->usecase == USECASE_COMPRESS_VOIP_CALL)
5006 ret = voice_extn_compress_voip_start_output_stream(out);
5007 else
5008 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08005009 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005010 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005011 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07005012 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005013 goto exit;
5014 }
Ashish Jain1b9b30c2017-05-18 20:57:40 +05305015 out->started = 1;
vivek mehtab72d08d2016-04-29 03:16:47 -07005016 if (last_known_cal_step != -1) {
5017 ALOGD("%s: retry previous failed cal level set", __func__);
5018 audio_hw_send_gain_dep_calibration(last_known_cal_step);
Preetam Singh Ranawatf4ae0222017-05-31 17:07:28 +05305019 last_known_cal_step = -1;
vivek mehtab72d08d2016-04-29 03:16:47 -07005020 }
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05305021
5022 if ((out->is_iec61937_info_available == true) &&
5023 (audio_extn_passthru_is_passthrough_stream(out))&&
5024 (!audio_extn_passthru_is_supported_backend_edid_cfg(adev, out))) {
5025 ret = -EINVAL;
5026 goto exit;
5027 }
Surendar Karkaf51b5842018-04-26 11:28:38 +05305028 if (out->set_dual_mono)
5029 audio_extn_send_dual_mono_mixing_coefficients(out);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005030 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005031
Ashish Jain81eb2a82015-05-13 10:52:34 +05305032 if (adev->is_channel_status_set == false && (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)){
Alexy Josephb1379942016-01-29 15:49:38 -08005033 audio_utils_set_hdmi_channel_status(out, (void *)buffer, bytes);
Ashish Jain81eb2a82015-05-13 10:52:34 +05305034 adev->is_channel_status_set = true;
5035 }
5036
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07005037 if (is_offload_usecase(out->usecase)) {
Alexy Joseph01e54e62015-03-03 19:01:03 -08005038 ALOGVV("copl(%p): writing buffer (%zu bytes) to compress device", out, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005039 if (out->send_new_metadata) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07005040 ALOGD("copl(%p):send new gapless metadata", out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005041 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
5042 out->send_new_metadata = 0;
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05305043 if (out->send_next_track_params && out->is_compr_metadata_avail) {
5044 ALOGD("copl(%p):send next track params in gapless", out);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08005045 // compress_set_next_track_param(out->compr, &(out->compr_config.codec->options));
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05305046 out->send_next_track_params = false;
5047 out->is_compr_metadata_avail = false;
5048 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005049 }
Dhananjay Kumarac341582017-02-23 23:42:25 +05305050 if (!(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
Ashish Jain83a6cc22016-06-28 14:34:17 +05305051 (out->convert_buffer) != NULL) {
Haynes Mathew George352f27b2013-07-26 00:00:15 -07005052
Ashish Jain83a6cc22016-06-28 14:34:17 +05305053 if ((bytes > out->hal_fragment_size)) {
Ashish Jainf1eaa582016-05-23 20:54:24 +05305054 ALOGW("Error written bytes %zu > %d (fragment_size)",
Ashish Jain83a6cc22016-06-28 14:34:17 +05305055 bytes, out->hal_fragment_size);
Ashish Jainf1eaa582016-05-23 20:54:24 +05305056 pthread_mutex_unlock(&out->lock);
Haynes Mathew George380745d2017-10-04 15:27:45 -07005057 ATRACE_END();
Ashish Jainf1eaa582016-05-23 20:54:24 +05305058 return -EINVAL;
5059 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05305060 audio_format_t dst_format = out->hal_op_format;
5061 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05305062
Dieter Luecking5d57def2018-09-07 14:23:37 +02005063 /* prevent division-by-zero */
5064 uint32_t bitwidth_src = format_to_bitwidth_table[src_format];
5065 uint32_t bitwidth_dst = format_to_bitwidth_table[dst_format];
5066 if ((bitwidth_src == 0) || (bitwidth_dst == 0)) {
5067 ALOGE("%s: Error bitwidth == 0", __func__);
Ramu Gottipati02809682018-12-19 16:46:12 +05305068 pthread_mutex_unlock(&out->lock);
Dieter Luecking5d57def2018-09-07 14:23:37 +02005069 ATRACE_END();
5070 return -EINVAL;
5071 }
5072
Ashish Jainf1eaa582016-05-23 20:54:24 +05305073 uint32_t frames = bytes / format_to_bitwidth_table[src_format];
5074 uint32_t bytes_to_write = frames * format_to_bitwidth_table[dst_format];
5075
Ashish Jain83a6cc22016-06-28 14:34:17 +05305076 memcpy_by_audio_format(out->convert_buffer,
Ashish Jainf1eaa582016-05-23 20:54:24 +05305077 dst_format,
5078 buffer,
5079 src_format,
5080 frames);
5081
Ashish Jain83a6cc22016-06-28 14:34:17 +05305082 ret = compress_write(out->compr, out->convert_buffer,
Ashish Jainf1eaa582016-05-23 20:54:24 +05305083 bytes_to_write);
5084
5085 /*Convert written bytes in audio flinger format*/
5086 if (ret > 0)
5087 ret = ((ret * format_to_bitwidth_table[out->format]) /
5088 format_to_bitwidth_table[dst_format]);
5089 }
5090 } else
5091 ret = compress_write(out->compr, buffer, bytes);
5092
Zhou Songc9672822017-08-16 16:01:39 +08005093 if ((ret < 0 || ret == (ssize_t)bytes) && !out->non_blocking)
5094 update_frames_written(out, bytes);
5095
Dhanalakshmi Siddani37ca1d62014-08-20 12:28:34 +05305096 if (ret < 0)
5097 ret = -errno;
Weiyin Jiangcc60dbb2018-08-21 13:12:03 +08005098 ALOGVV("%s: writing buffer (%zu bytes) to compress device returned %d", __func__, bytes, (int)ret);
Ashish Jainb26edfb2016-08-25 00:10:11 +05305099 /*msg to cb thread only if non blocking write is enabled*/
5100 if (ret >= 0 && ret < (ssize_t)bytes && out->non_blocking) {
Sidipotu Ashok55820562014-02-10 16:16:38 +05305101 ALOGD("No space available in compress driver, post msg to cb thread");
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005102 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
Naresh Tanniru80659832014-06-04 18:17:56 +05305103 } else if (-ENETRESET == ret) {
5104 ALOGE("copl %s: received sound card offline state on compress write", __func__);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305105 out->card_status = CARD_STATUS_OFFLINE;
Naresh Tanniru80659832014-06-04 18:17:56 +05305106 pthread_mutex_unlock(&out->lock);
Dhananjay Kumar1248dd82017-07-28 21:22:16 +05305107 out_on_error(&out->stream.common);
Haynes Mathew George380745d2017-10-04 15:27:45 -07005108 ATRACE_END();
Naresh Tanniru80659832014-06-04 18:17:56 +05305109 return ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005110 }
Ashish Jain5106d362016-05-11 19:23:33 +05305111
Dhanalakshmi Siddania6b76c72016-09-09 18:10:31 +05305112 /* Call compr start only when non-zero bytes of data is there to be rendered */
5113 if (!out->playback_started && ret > 0) {
5114 int status = compress_start(out->compr);
5115 if (status < 0) {
5116 ret = status;
5117 ALOGE("%s: compr start failed with err %d", __func__, errno);
5118 goto exit;
5119 }
Alexy Joseph7de344d2015-03-30 10:40:03 -07005120 audio_extn_dts_eagle_fade(adev, true, out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005121 out->playback_started = 1;
5122 out->offload_state = OFFLOAD_STATE_PLAYING;
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08005123
5124 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
5125 popcount(out->channel_mask),
5126 out->playback_started);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005127 }
5128 pthread_mutex_unlock(&out->lock);
Haynes Mathew George380745d2017-10-04 15:27:45 -07005129 ATRACE_END();
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005130 return ret;
5131 } else {
5132 if (out->pcm) {
Arun Mirpuri7da752a2018-09-11 18:01:15 -07005133 size_t bytes_to_write = bytes;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005134 if (out->muted)
5135 memset((void *)buffer, 0, bytes);
Arun Mirpuri7da752a2018-09-11 18:01:15 -07005136 ALOGV("%s: frames=%zu, frame_size=%zu, bytes_to_write=%zu",
5137 __func__, frames, frame_size, bytes_to_write);
5138
Aalique Grahame22e49102018-12-18 14:23:57 -08005139 if (out->usecase == USECASE_INCALL_MUSIC_UPLINK ||
Aalique Grahame22e49102018-12-18 14:23:57 -08005140 out->usecase == USECASE_INCALL_MUSIC_UPLINK2) {
Arun Mirpuri7da752a2018-09-11 18:01:15 -07005141 size_t channel_count = audio_channel_count_from_out_mask(out->channel_mask);
5142 int16_t *src = (int16_t *)buffer;
5143 int16_t *dst = (int16_t *)buffer;
5144
5145 LOG_ALWAYS_FATAL_IF(out->config.channels != 1 || channel_count != 2 ||
5146 out->format != AUDIO_FORMAT_PCM_16_BIT,
Aalique Grahame22e49102018-12-18 14:23:57 -08005147 "out_write called for %s use case with wrong properties",
5148 use_case_table[out->usecase]);
Arun Mirpuri7da752a2018-09-11 18:01:15 -07005149
5150 /*
5151 * FIXME: this can be removed once audio flinger mixer supports
5152 * mono output
5153 */
5154
5155 /*
5156 * Code below goes over each frame in the buffer and adds both
5157 * L and R samples and then divides by 2 to convert to mono
5158 */
5159 for (size_t i = 0; i < frames ; i++, dst++, src += 2) {
5160 *dst = (int16_t)(((int32_t)src[0] + (int32_t)src[1]) >> 1);
5161 }
5162 bytes_to_write /= 2;
5163 }
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07005164
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05305165 ALOGVV("%s: writing buffer (%zu bytes) to pcm device", __func__, bytes);
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07005166
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005167 long ns = 0;
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07005168
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005169 if (out->config.rate)
5170 ns = pcm_bytes_to_frames(out->pcm, bytes)*1000000000LL/
5171 out->config.rate;
5172
Arun Mirpuri7da752a2018-09-11 18:01:15 -07005173 request_out_focus(out, ns);
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005174 bool use_mmap = is_mmap_usecase(out->usecase) || out->realtime;
5175
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005176 if (use_mmap)
Arun Mirpuri7da752a2018-09-11 18:01:15 -07005177 ret = pcm_mmap_write(out->pcm, (void *)buffer, bytes_to_write);
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005178 else if (out->hal_op_format != out->hal_ip_format &&
Ashish Jain83a6cc22016-06-28 14:34:17 +05305179 out->convert_buffer != NULL) {
5180
5181 memcpy_by_audio_format(out->convert_buffer,
5182 out->hal_op_format,
5183 buffer,
5184 out->hal_ip_format,
5185 out->config.period_size * out->config.channels);
5186
5187 ret = pcm_write(out->pcm, out->convert_buffer,
5188 (out->config.period_size *
5189 out->config.channels *
5190 format_to_bitwidth_table[out->hal_op_format]));
5191 } else {
Aditya Bavanarid4db8ee2017-05-29 21:08:03 +05305192 /*
5193 * To avoid underrun in DSP when the application is not pumping
5194 * data at required rate, check for the no. of bytes and ignore
5195 * pcm_write if it is less than actual buffer size.
5196 * It is a work around to a change in compress VOIP driver.
5197 */
5198 if ((out->flags & AUDIO_OUTPUT_FLAG_VOIP_RX) &&
5199 bytes < (out->config.period_size * out->config.channels *
5200 audio_bytes_per_sample(out->format))) {
5201 size_t voip_buf_size =
5202 out->config.period_size * out->config.channels *
5203 audio_bytes_per_sample(out->format);
5204 ALOGE("%s:VOIP underrun: bytes received %zu, required:%zu\n",
5205 __func__, bytes, voip_buf_size);
5206 usleep(((uint64_t)voip_buf_size - bytes) *
5207 1000000 / audio_stream_out_frame_size(stream) /
5208 out_get_sample_rate(&out->stream.common));
5209 ret = 0;
Vignesh Kulothungana6927272019-02-20 15:17:07 -08005210 } else {
5211 if (out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS)
5212 ret = split_and_write_audio_haptic_data(out, buffer, bytes);
5213 else
5214 ret = pcm_write(out->pcm, (void *)buffer, bytes_to_write);
5215 }
Ashish Jain83a6cc22016-06-28 14:34:17 +05305216 }
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07005217
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005218 release_out_focus(out);
5219
Dhanalakshmi Siddani37ca1d62014-08-20 12:28:34 +05305220 if (ret < 0)
5221 ret = -errno;
Zhou Songc9672822017-08-16 16:01:39 +08005222 else if (ret > 0)
Ashish Jain83a6cc22016-06-28 14:34:17 +05305223 ret = -EINVAL;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005224 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005225 }
5226
5227exit:
Zhou Songc9672822017-08-16 16:01:39 +08005228 update_frames_written(out, bytes);
Naresh Tanniru4c630392014-05-12 01:05:52 +05305229 if (-ENETRESET == ret) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305230 out->card_status = CARD_STATUS_OFFLINE;
Naresh Tanniru4c630392014-05-12 01:05:52 +05305231 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005232 pthread_mutex_unlock(&out->lock);
5233
5234 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07005235 if (out->pcm)
Alexy Josephb1379942016-01-29 15:49:38 -08005236 ALOGE("%s: error %d, %s", __func__, (int)ret, pcm_get_error(out->pcm));
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05305237 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05305238 pthread_mutex_lock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05305239 voice_extn_compress_voip_close_output_stream(&out->stream.common);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05305240 out->started = 0;
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05305241 pthread_mutex_unlock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05305242 out->standby = true;
5243 }
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305244 out_on_error(&out->stream.common);
Dieter Luecking5d57def2018-09-07 14:23:37 +02005245 if (!(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
5246 /* prevent division-by-zero */
5247 uint32_t stream_size = audio_stream_out_frame_size(stream);
5248 uint32_t srate = out_get_sample_rate(&out->stream.common);
Vidyakumar Athotaa9d3a5f2017-08-09 12:13:05 -07005249
Dieter Luecking5d57def2018-09-07 14:23:37 +02005250 if ((stream_size == 0) || (srate == 0)) {
5251 ALOGE("%s: stream_size= %d, srate = %d", __func__, stream_size, srate);
5252 ATRACE_END();
5253 return -EINVAL;
5254 }
5255 usleep((uint64_t)bytes * 1000000 / stream_size / srate);
5256 }
Vidyakumar Athotaa9d3a5f2017-08-09 12:13:05 -07005257 if (audio_extn_passthru_is_passthrough_stream(out)) {
Rajshekar Eashwarappa88834522018-04-02 17:20:15 +05305258 //ALOGE("%s: write error, ret = %zd", __func__, ret);
Haynes Mathew George380745d2017-10-04 15:27:45 -07005259 ATRACE_END();
Vidyakumar Athotaa9d3a5f2017-08-09 12:13:05 -07005260 return ret;
5261 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005262 }
Haynes Mathew George380745d2017-10-04 15:27:45 -07005263 ATRACE_END();
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005264 return bytes;
5265}
5266
5267static int out_get_render_position(const struct audio_stream_out *stream,
5268 uint32_t *dsp_frames)
5269{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005270 struct stream_out *out = (struct stream_out *)stream;
Zhou Song32a556e2015-05-05 10:46:56 +08005271
5272 if (dsp_frames == NULL)
5273 return -EINVAL;
5274
5275 *dsp_frames = 0;
5276 if (is_offload_usecase(out->usecase)) {
Mingming Yin9e348b52014-11-19 16:18:55 -08005277 ssize_t ret = 0;
Ashish Jain5106d362016-05-11 19:23:33 +05305278
5279 /* Below piece of code is not guarded against any lock beacuse audioFliner serializes
5280 * this operation and adev_close_output_stream(where out gets reset).
5281 */
Dhananjay Kumarac341582017-02-23 23:42:25 +05305282 if (!out->non_blocking && !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Zhou Song48453a02018-01-10 17:50:59 +08005283 *dsp_frames = get_actual_pcm_frames_rendered(out, NULL);
Ashish Jain5106d362016-05-11 19:23:33 +05305284 ALOGVV("dsp_frames %d sampleRate %d",(int)*dsp_frames,out->sample_rate);
Haynes Mathew Georgeb0f5dc32017-10-06 18:35:12 -07005285 adjust_frames_for_device_delay(out, dsp_frames);
Ashish Jain5106d362016-05-11 19:23:33 +05305286 return 0;
5287 }
5288
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005289 lock_output_stream(out);
Ashish Jain5106d362016-05-11 19:23:33 +05305290 if (out->compr != NULL && out->non_blocking) {
Naresh Tanniru80659832014-06-04 18:17:56 +05305291 ret = compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005292 &out->sample_rate);
Dhanalakshmi Siddani37ca1d62014-08-20 12:28:34 +05305293 if (ret < 0)
5294 ret = -errno;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005295 ALOGVV("%s rendered frames %d sample_rate %d",
Ashish Jain5106d362016-05-11 19:23:33 +05305296 __func__, *dsp_frames, out->sample_rate);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005297 }
Naresh Tanniru80659832014-06-04 18:17:56 +05305298 if (-ENETRESET == ret) {
5299 ALOGE(" ERROR: sound card not active Unable to get time stamp from compress driver");
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305300 out->card_status = CARD_STATUS_OFFLINE;
5301 ret = -EINVAL;
Naresh Tanniru80659832014-06-04 18:17:56 +05305302 } else if(ret < 0) {
5303 ALOGE(" ERROR: Unable to get time stamp from compress driver");
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305304 ret = -EINVAL;
5305 } else if (out->card_status == CARD_STATUS_OFFLINE) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05305306 /*
5307 * Handle corner case where compress session is closed during SSR
5308 * and timestamp is queried
5309 */
5310 ALOGE(" ERROR: sound card not active, return error");
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305311 ret = -EINVAL;
Manisha Agarwal7b3e3772019-02-20 14:33:45 +05305312 } else if (out->prev_card_status_offline) {
5313 ALOGE("ERROR: previously sound card was offline,return error");
5314 ret = -EINVAL;
Naresh Tanniru80659832014-06-04 18:17:56 +05305315 } else {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305316 ret = 0;
Haynes Mathew Georgeb0f5dc32017-10-06 18:35:12 -07005317 adjust_frames_for_device_delay(out, dsp_frames);
Naresh Tanniru80659832014-06-04 18:17:56 +05305318 }
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305319 pthread_mutex_unlock(&out->lock);
5320 return ret;
Zhou Song32a556e2015-05-05 10:46:56 +08005321 } else if (audio_is_linear_pcm(out->format)) {
5322 *dsp_frames = out->written;
Haynes Mathew Georgeb0f5dc32017-10-06 18:35:12 -07005323 adjust_frames_for_device_delay(out, dsp_frames);
Zhou Song32a556e2015-05-05 10:46:56 +08005324 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005325 } else
5326 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005327}
5328
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07005329static int out_add_audio_effect(const struct audio_stream *stream __unused,
5330 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005331{
5332 return 0;
5333}
5334
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07005335static int out_remove_audio_effect(const struct audio_stream *stream __unused,
5336 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005337{
5338 return 0;
5339}
5340
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07005341static int out_get_next_write_timestamp(const struct audio_stream_out *stream __unused,
5342 int64_t *timestamp __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005343{
Satya Krishna Pindiprolib6655542017-07-03 19:38:19 +05305344 return -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005345}
5346
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07005347static int out_get_presentation_position(const struct audio_stream_out *stream,
5348 uint64_t *frames, struct timespec *timestamp)
5349{
5350 struct stream_out *out = (struct stream_out *)stream;
pavance65c2fe2017-10-18 17:52:01 +05305351 int ret = -ENODATA;
Eric Laurent949a0892013-09-20 09:20:13 -07005352 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07005353
Ashish Jain5106d362016-05-11 19:23:33 +05305354 /* below piece of code is not guarded against any lock because audioFliner serializes
5355 * this operation and adev_close_output_stream( where out gets reset).
5356 */
5357 if (is_offload_usecase(out->usecase) && !out->non_blocking &&
Dhananjay Kumarac341582017-02-23 23:42:25 +05305358 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Zhou Song48453a02018-01-10 17:50:59 +08005359 *frames = get_actual_pcm_frames_rendered(out, timestamp);
Ashish Jain5106d362016-05-11 19:23:33 +05305360 ALOGVV("frames %lld playedat %lld",(long long int)*frames,
5361 timestamp->tv_sec * 1000000LL + timestamp->tv_nsec / 1000);
5362 return 0;
5363 }
5364
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005365 lock_output_stream(out);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07005366
Ashish Jain5106d362016-05-11 19:23:33 +05305367 if (is_offload_usecase(out->usecase) && out->compr != NULL && out->non_blocking) {
5368 ret = compress_get_tstamp(out->compr, &dsp_frames,
5369 &out->sample_rate);
yidongh0515e042017-07-06 15:00:34 +08005370 // Adjustment accounts for A2dp encoder latency with offload usecases
5371 // Note: Encoder latency is returned in ms.
5372 if (AUDIO_DEVICE_OUT_ALL_A2DP & out->devices) {
5373 unsigned long offset =
5374 (audio_extn_a2dp_get_encoder_latency() * out->sample_rate / 1000);
5375 dsp_frames = (dsp_frames > offset) ? (dsp_frames - offset) : 0;
5376 }
Ashish Jain5106d362016-05-11 19:23:33 +05305377 ALOGVV("%s rendered frames %ld sample_rate %d",
5378 __func__, dsp_frames, out->sample_rate);
5379 *frames = dsp_frames;
5380 if (ret < 0)
5381 ret = -errno;
5382 if (-ENETRESET == ret) {
5383 ALOGE(" ERROR: sound card not active Unable to get time stamp from compress driver");
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305384 out->card_status = CARD_STATUS_OFFLINE;
Ashish Jain5106d362016-05-11 19:23:33 +05305385 ret = -EINVAL;
5386 } else
5387 ret = 0;
5388 /* this is the best we can do */
5389 clock_gettime(CLOCK_MONOTONIC, timestamp);
Eric Laurent949a0892013-09-20 09:20:13 -07005390 } else {
5391 if (out->pcm) {
Weiyin Jiangd4633762018-03-16 12:05:03 +08005392 unsigned int avail;
5393 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
5394 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
5395 int64_t signed_frames = out->written - kernel_buffer_size + avail;
5396 // This adjustment accounts for buffering after app processor.
5397 // It is based on estimated DSP latency per use case, rather than exact.
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07005398 signed_frames -=
Weiyin Jiangd4633762018-03-16 12:05:03 +08005399 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
Aniket Kumar Lataff613152017-07-18 18:19:21 -07005400
Weiyin Jiangd4633762018-03-16 12:05:03 +08005401 // Adjustment accounts for A2dp encoder latency with non offload usecases
5402 // Note: Encoder latency is returned in ms, while platform_render_latency in us.
5403 if (AUDIO_DEVICE_OUT_ALL_A2DP & out->devices) {
5404 signed_frames -=
5405 (audio_extn_a2dp_get_encoder_latency() * out->sample_rate / 1000);
5406 }
5407
5408 // It would be unusual for this value to be negative, but check just in case ...
5409 if (signed_frames >= 0) {
5410 *frames = signed_frames;
5411 ret = 0;
5412 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07005413 }
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305414 } else if (out->card_status == CARD_STATUS_OFFLINE) {
Ashish Jainbbce4322016-02-16 13:25:27 +05305415 *frames = out->written;
5416 clock_gettime(CLOCK_MONOTONIC, timestamp);
Manisha Agarwal7b3e3772019-02-20 14:33:45 +05305417 if (is_offload_usecase(out->usecase))
5418 ret = -EINVAL;
5419 else
5420 ret = 0;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07005421 }
5422 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07005423 pthread_mutex_unlock(&out->lock);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07005424 return ret;
5425}
5426
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005427static int out_set_callback(struct audio_stream_out *stream,
5428 stream_callback_t callback, void *cookie)
5429{
5430 struct stream_out *out = (struct stream_out *)stream;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08005431 int ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005432
5433 ALOGV("%s", __func__);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005434 lock_output_stream(out);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08005435 out->client_callback = callback;
5436 out->client_cookie = cookie;
5437 if (out->adsp_hdlr_stream_handle) {
5438 ret = audio_extn_adsp_hdlr_stream_set_callback(
5439 out->adsp_hdlr_stream_handle,
5440 callback,
5441 cookie);
5442 if (ret)
5443 ALOGW("%s:adsp hdlr callback registration failed %d",
5444 __func__, ret);
5445 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005446 pthread_mutex_unlock(&out->lock);
5447 return 0;
5448}
5449
5450static int out_pause(struct audio_stream_out* stream)
5451{
5452 struct stream_out *out = (struct stream_out *)stream;
5453 int status = -ENOSYS;
5454 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07005455 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07005456 ALOGD("copl(%p):pause compress driver", out);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005457 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005458 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305459 if (out->card_status != CARD_STATUS_OFFLINE)
Naresh Tanniru80659832014-06-04 18:17:56 +05305460 status = compress_pause(out->compr);
5461
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005462 out->offload_state = OFFLOAD_STATE_PAUSED;
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08005463
Mingming Yin21854652016-04-13 11:54:02 -07005464 if (audio_extn_passthru_is_active()) {
5465 ALOGV("offload use case, pause passthru");
5466 audio_extn_passthru_on_pause(out);
5467 }
5468
Dhanalakshmi Siddani79415e72015-03-23 11:54:47 +05305469 audio_extn_dts_eagle_fade(adev, false, out);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08005470 audio_extn_dts_notify_playback_state(out->usecase, 0,
5471 out->sample_rate, popcount(out->channel_mask),
5472 0);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005473 }
5474 pthread_mutex_unlock(&out->lock);
5475 }
5476 return status;
5477}
5478
5479static int out_resume(struct audio_stream_out* stream)
5480{
5481 struct stream_out *out = (struct stream_out *)stream;
5482 int status = -ENOSYS;
5483 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07005484 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07005485 ALOGD("copl(%p):resume compress driver", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005486 status = 0;
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005487 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005488 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305489 if (out->card_status != CARD_STATUS_OFFLINE) {
Naresh Tanniru80659832014-06-04 18:17:56 +05305490 status = compress_resume(out->compr);
Mingming Yin21854652016-04-13 11:54:02 -07005491 }
5492 if (!status) {
5493 out->offload_state = OFFLOAD_STATE_PLAYING;
5494 }
Dhanalakshmi Siddani79415e72015-03-23 11:54:47 +05305495 audio_extn_dts_eagle_fade(adev, true, out);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08005496 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
5497 popcount(out->channel_mask), 1);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005498 }
5499 pthread_mutex_unlock(&out->lock);
5500 }
5501 return status;
5502}
5503
5504static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
5505{
5506 struct stream_out *out = (struct stream_out *)stream;
5507 int status = -ENOSYS;
5508 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07005509 if (is_offload_usecase(out->usecase)) {
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005510 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005511 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
5512 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
5513 else
5514 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
5515 pthread_mutex_unlock(&out->lock);
5516 }
5517 return status;
5518}
5519
5520static int out_flush(struct audio_stream_out* stream)
5521{
5522 struct stream_out *out = (struct stream_out *)stream;
5523 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07005524 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07005525 ALOGD("copl(%p):calling compress flush", out);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005526 lock_output_stream(out);
Haynes Mathew Georgeafe54d82016-09-21 14:39:19 -07005527 if (out->offload_state == OFFLOAD_STATE_PAUSED) {
5528 stop_compressed_output_l(out);
Haynes Mathew Georgeafe54d82016-09-21 14:39:19 -07005529 } else {
5530 ALOGW("%s called in invalid state %d", __func__, out->offload_state);
5531 }
Weiyin Jiang547e4152017-09-14 17:24:18 +08005532 out->written = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005533 pthread_mutex_unlock(&out->lock);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07005534 ALOGD("copl(%p):out of compress flush", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005535 return 0;
5536 }
5537 return -ENOSYS;
5538}
5539
Haynes Mathew George16081042017-05-31 17:16:49 -07005540static int out_stop(const struct audio_stream_out* stream)
5541{
5542 struct stream_out *out = (struct stream_out *)stream;
5543 struct audio_device *adev = out->dev;
5544 int ret = -ENOSYS;
5545
5546 ALOGV("%s", __func__);
5547 pthread_mutex_lock(&adev->lock);
5548 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP && !out->standby &&
5549 out->playback_started && out->pcm != NULL) {
5550 pcm_stop(out->pcm);
5551 ret = stop_output_stream(out);
5552 out->playback_started = false;
5553 }
5554 pthread_mutex_unlock(&adev->lock);
5555 return ret;
5556}
5557
5558static int out_start(const struct audio_stream_out* stream)
5559{
5560 struct stream_out *out = (struct stream_out *)stream;
5561 struct audio_device *adev = out->dev;
5562 int ret = -ENOSYS;
5563
5564 ALOGV("%s", __func__);
5565 pthread_mutex_lock(&adev->lock);
5566 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP && !out->standby &&
5567 !out->playback_started && out->pcm != NULL) {
5568 ret = start_output_stream(out);
5569 if (ret == 0) {
5570 out->playback_started = true;
5571 }
5572 }
5573 pthread_mutex_unlock(&adev->lock);
5574 return ret;
5575}
5576
5577/*
5578 * Modify config->period_count based on min_size_frames
5579 */
5580static void adjust_mmap_period_count(struct pcm_config *config, int32_t min_size_frames)
5581{
5582 int periodCountRequested = (min_size_frames + config->period_size - 1)
5583 / config->period_size;
5584 int periodCount = MMAP_PERIOD_COUNT_MIN;
5585
5586 ALOGV("%s original config.period_size = %d config.period_count = %d",
5587 __func__, config->period_size, config->period_count);
5588
5589 while (periodCount < periodCountRequested && (periodCount * 2) < MMAP_PERIOD_COUNT_MAX) {
5590 periodCount *= 2;
5591 }
5592 config->period_count = periodCount;
5593
5594 ALOGV("%s requested config.period_count = %d", __func__, config->period_count);
5595}
5596
5597static int out_create_mmap_buffer(const struct audio_stream_out *stream,
5598 int32_t min_size_frames,
5599 struct audio_mmap_buffer_info *info)
5600{
5601 struct stream_out *out = (struct stream_out *)stream;
5602 struct audio_device *adev = out->dev;
5603 int ret = 0;
Aalique Grahame1f123102017-10-12 10:38:32 -07005604 unsigned int offset1 = 0;
5605 unsigned int frames1 = 0;
Haynes Mathew George16081042017-05-31 17:16:49 -07005606 const char *step = "";
Haynes Mathew Georgeef514882017-05-01 17:46:23 -07005607 uint32_t mmap_size;
Arun Mirpuri5d170872019-03-26 13:21:31 -07005608 uint32_t buffer_size;
Haynes Mathew George16081042017-05-31 17:16:49 -07005609
Arun Mirpuri5d170872019-03-26 13:21:31 -07005610 ALOGD("%s", __func__);
Sharad Sanglec6f32552018-05-04 16:15:38 +05305611 lock_output_stream(out);
Haynes Mathew George16081042017-05-31 17:16:49 -07005612 pthread_mutex_lock(&adev->lock);
5613
Sharad Sanglec6f32552018-05-04 16:15:38 +05305614 if (CARD_STATUS_OFFLINE == out->card_status ||
5615 CARD_STATUS_OFFLINE == adev->card_status) {
5616 ALOGW("out->card_status or adev->card_status offline, try again");
5617 ret = -EIO;
5618 goto exit;
5619 }
Haynes Mathew George16081042017-05-31 17:16:49 -07005620 if (info == NULL || min_size_frames == 0) {
5621 ALOGE("%s: info = %p, min_size_frames = %d", __func__, info, min_size_frames);
5622 ret = -EINVAL;
5623 goto exit;
5624 }
5625 if (out->usecase != USECASE_AUDIO_PLAYBACK_MMAP || !out->standby) {
5626 ALOGE("%s: usecase = %d, standby = %d", __func__, out->usecase, out->standby);
5627 ret = -ENOSYS;
5628 goto exit;
5629 }
5630 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
5631 if (out->pcm_device_id < 0) {
5632 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
5633 __func__, out->pcm_device_id, out->usecase);
5634 ret = -EINVAL;
5635 goto exit;
5636 }
5637
5638 adjust_mmap_period_count(&out->config, min_size_frames);
5639
Arun Mirpuri5d170872019-03-26 13:21:31 -07005640 ALOGD("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
Haynes Mathew George16081042017-05-31 17:16:49 -07005641 __func__, adev->snd_card, out->pcm_device_id, out->config.channels);
5642 out->pcm = pcm_open(adev->snd_card, out->pcm_device_id,
5643 (PCM_OUT | PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC), &out->config);
Satish Babu Patakokila54ce83d2018-07-06 18:00:37 +05305644 if (errno == ENETRESET && !pcm_is_ready(out->pcm)) {
Sharad Sanglec6f32552018-05-04 16:15:38 +05305645 ALOGE("%s: pcm_open failed errno:%d\n", __func__, errno);
5646 out->card_status = CARD_STATUS_OFFLINE;
5647 adev->card_status = CARD_STATUS_OFFLINE;
5648 ret = -EIO;
5649 goto exit;
5650 }
5651
Haynes Mathew George16081042017-05-31 17:16:49 -07005652 if (out->pcm == NULL || !pcm_is_ready(out->pcm)) {
5653 step = "open";
5654 ret = -ENODEV;
5655 goto exit;
5656 }
5657 ret = pcm_mmap_begin(out->pcm, &info->shared_memory_address, &offset1, &frames1);
5658 if (ret < 0) {
5659 step = "begin";
5660 goto exit;
5661 }
5662 info->buffer_size_frames = pcm_get_buffer_size(out->pcm);
Arun Mirpuri5d170872019-03-26 13:21:31 -07005663 buffer_size = pcm_frames_to_bytes(out->pcm, info->buffer_size_frames);
Haynes Mathew George16081042017-05-31 17:16:49 -07005664 info->burst_size_frames = out->config.period_size;
Haynes Mathew Georgeef514882017-05-01 17:46:23 -07005665 ret = platform_get_mmap_data_fd(adev->platform,
5666 out->pcm_device_id, 0 /*playback*/,
5667 &info->shared_memory_fd,
5668 &mmap_size);
5669 if (ret < 0) {
Arun Mirpuri5d170872019-03-26 13:21:31 -07005670 // Fall back to non exclusive mode
5671 info->shared_memory_fd = pcm_get_poll_fd(out->pcm);
5672 } else {
5673 if (mmap_size < buffer_size) {
5674 step = "mmap";
5675 goto exit;
5676 }
5677 // FIXME: indicate exclusive mode support by returning a negative buffer size
5678 info->buffer_size_frames *= -1;
Haynes Mathew Georgeef514882017-05-01 17:46:23 -07005679 }
Haynes Mathew George16081042017-05-31 17:16:49 -07005680 memset(info->shared_memory_address, 0, pcm_frames_to_bytes(out->pcm,
Haynes Mathew Georgeef514882017-05-01 17:46:23 -07005681 info->buffer_size_frames));
Haynes Mathew George16081042017-05-31 17:16:49 -07005682
5683 ret = pcm_mmap_commit(out->pcm, 0, MMAP_PERIOD_SIZE);
5684 if (ret < 0) {
5685 step = "commit";
5686 goto exit;
5687 }
5688
5689 out->standby = false;
5690 ret = 0;
5691
Arun Mirpuri5d170872019-03-26 13:21:31 -07005692 ALOGD("%s: got mmap buffer address %p info->buffer_size_frames %d",
Haynes Mathew George16081042017-05-31 17:16:49 -07005693 __func__, info->shared_memory_address, info->buffer_size_frames);
5694
5695exit:
5696 if (ret != 0) {
5697 if (out->pcm == NULL) {
5698 ALOGE("%s: %s - %d", __func__, step, ret);
5699 } else {
5700 ALOGE("%s: %s %s", __func__, step, pcm_get_error(out->pcm));
5701 pcm_close(out->pcm);
5702 out->pcm = NULL;
5703 }
5704 }
5705 pthread_mutex_unlock(&adev->lock);
Sharad Sanglec6f32552018-05-04 16:15:38 +05305706 pthread_mutex_unlock(&out->lock);
Haynes Mathew George16081042017-05-31 17:16:49 -07005707 return ret;
5708}
5709
5710static int out_get_mmap_position(const struct audio_stream_out *stream,
5711 struct audio_mmap_position *position)
5712{
5713 struct stream_out *out = (struct stream_out *)stream;
5714 ALOGVV("%s", __func__);
5715 if (position == NULL) {
5716 return -EINVAL;
5717 }
5718 if (out->usecase != USECASE_AUDIO_PLAYBACK_MMAP) {
Haynes Mathew George4ab3ba92017-12-11 14:49:43 -08005719 ALOGE("%s: called on %s", __func__, use_case_table[out->usecase]);
Haynes Mathew George16081042017-05-31 17:16:49 -07005720 return -ENOSYS;
5721 }
5722 if (out->pcm == NULL) {
5723 return -ENOSYS;
5724 }
5725
5726 struct timespec ts = { 0, 0 };
5727 int ret = pcm_mmap_get_hw_ptr(out->pcm, (unsigned int *)&position->position_frames, &ts);
5728 if (ret < 0) {
5729 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
5730 return ret;
5731 }
Aalique Grahame90717e22018-07-18 15:54:18 -07005732 position->time_nanoseconds = ts.tv_sec*1000000000LL + ts.tv_nsec;
Haynes Mathew George16081042017-05-31 17:16:49 -07005733 return 0;
5734}
5735
5736
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005737/** audio_stream_in implementation **/
5738static uint32_t in_get_sample_rate(const struct audio_stream *stream)
5739{
5740 struct stream_in *in = (struct stream_in *)stream;
5741
5742 return in->config.rate;
5743}
5744
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07005745static int in_set_sample_rate(struct audio_stream *stream __unused,
5746 uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005747{
5748 return -ENOSYS;
5749}
5750
5751static size_t in_get_buffer_size(const struct audio_stream *stream)
5752{
5753 struct stream_in *in = (struct stream_in *)stream;
5754
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08005755 if(in->usecase == USECASE_COMPRESS_VOIP_CALL)
5756 return voice_extn_compress_voip_in_get_buffer_size(in);
Vikram Panduranga93f080e2017-06-07 18:16:14 -07005757 else if(in->usecase == USECASE_AUDIO_RECORD_VOIP)
5758 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 -07005759 else if(audio_extn_compr_cap_usecase_supported(in->usecase))
5760 return audio_extn_compr_cap_get_buffer_size(in->config.format);
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05305761 else if(audio_extn_cin_attached_usecase(in->usecase))
5762 return audio_extn_cin_get_buffer_size(in);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08005763
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005764 return in->config.period_size * in->af_period_multiplier *
5765 audio_stream_in_frame_size((const struct audio_stream_in *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005766}
5767
5768static uint32_t in_get_channels(const struct audio_stream *stream)
5769{
5770 struct stream_in *in = (struct stream_in *)stream;
5771
5772 return in->channel_mask;
5773}
5774
5775static audio_format_t in_get_format(const struct audio_stream *stream)
5776{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08005777 struct stream_in *in = (struct stream_in *)stream;
5778
5779 return in->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005780}
5781
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07005782static int in_set_format(struct audio_stream *stream __unused,
5783 audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005784{
5785 return -ENOSYS;
5786}
5787
5788static int in_standby(struct audio_stream *stream)
5789{
5790 struct stream_in *in = (struct stream_in *)stream;
5791 struct audio_device *adev = in->dev;
5792 int status = 0;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05305793 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
5794 stream, in->usecase, use_case_table[in->usecase]);
Haynes Mathew George16081042017-05-31 17:16:49 -07005795 bool do_stop = true;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05305796
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005797 lock_input_stream(in);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07005798 if (!in->standby && in->is_st_session) {
5799 ALOGD("%s: sound trigger pcm stop lab", __func__);
5800 audio_extn_sound_trigger_stop_lab(in);
Quinn Malef6050362019-01-30 15:55:40 -08005801 adev->num_va_sessions--;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07005802 in->standby = 1;
5803 }
5804
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005805 if (!in->standby) {
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07005806 if (adev->adm_deregister_stream)
5807 adev->adm_deregister_stream(adev->adm_data, in->capture_handle);
5808
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08005809 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005810 in->standby = true;
Zhou Songa8895042016-07-05 17:54:22 +08005811 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
kunleizbecba2d2017-09-07 13:37:16 +08005812 do_stop = false;
Zhou Songa8895042016-07-05 17:54:22 +08005813 voice_extn_compress_voip_close_input_stream(stream);
5814 ALOGD("VOIP input entered standby");
Haynes Mathew George16081042017-05-31 17:16:49 -07005815 } else if (in->usecase == USECASE_AUDIO_RECORD_MMAP) {
5816 do_stop = in->capture_started;
5817 in->capture_started = false;
Zhou Songa8895042016-07-05 17:54:22 +08005818 } else {
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05305819 if (audio_extn_cin_attached_usecase(in->usecase))
5820 audio_extn_cin_stop_input_stream(in);
kunleizbecba2d2017-09-07 13:37:16 +08005821 }
5822
Arun Mirpuri5d170872019-03-26 13:21:31 -07005823 if (in->pcm) {
5824 ATRACE_BEGIN("pcm_in_close");
5825 pcm_close(in->pcm);
5826 ATRACE_END();
5827 in->pcm = NULL;
5828 }
5829
5830 if(do_stop) {
Aalique Grahame22e49102018-12-18 14:23:57 -08005831 adev->enable_voicerx = false;
5832 platform_set_echo_reference(adev, false, AUDIO_DEVICE_NONE);
Zhou Songa8895042016-07-05 17:54:22 +08005833 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08005834 }
Quinn Malef6050362019-01-30 15:55:40 -08005835
5836 if (in->source == AUDIO_SOURCE_VOICE_RECOGNITION)
5837 adev->num_va_sessions--;
5838
Eric Laurent150dbfe2013-02-27 14:31:02 -08005839 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005840 }
5841 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07005842 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005843 return status;
5844}
5845
Aalique Grahame22e49102018-12-18 14:23:57 -08005846static int in_dump(const struct audio_stream *stream,
5847 int fd)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005848{
Aalique Grahame22e49102018-12-18 14:23:57 -08005849 struct stream_in *in = (struct stream_in *)stream;
5850
5851 // We try to get the lock for consistency,
5852 // but it isn't necessary for these variables.
5853 // If we're not in standby, we may be blocked on a read.
5854 const bool locked = (pthread_mutex_trylock(&in->lock) == 0);
5855 dprintf(fd, " Standby: %s\n", in->standby ? "yes" : "no");
5856 dprintf(fd, " Frames read: %lld\n", (long long)in->frames_read);
5857 dprintf(fd, " Frames muted: %lld\n", (long long)in->frames_muted);
5858
5859 if (locked) {
5860 pthread_mutex_unlock(&in->lock);
5861 }
5862
5863 // dump error info
5864 (void)error_log_dump(
5865 in->error_log, fd, " " /* prefix */, 0 /* lines */, 0 /* limit_ns */);
5866
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005867 return 0;
5868}
5869
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305870static void in_snd_mon_cb(void * stream, struct str_parms * parms)
5871{
5872 if (!stream || !parms)
5873 return;
5874
5875 struct stream_in *in = (struct stream_in *)stream;
5876 struct audio_device *adev = in->dev;
5877
5878 card_status_t status;
5879 int card;
5880 if (parse_snd_card_status(parms, &card, &status) < 0)
5881 return;
5882
5883 pthread_mutex_lock(&adev->lock);
5884 bool valid_cb = (card == adev->snd_card);
5885 pthread_mutex_unlock(&adev->lock);
5886
5887 if (!valid_cb)
5888 return;
5889
5890 lock_input_stream(in);
5891 if (in->card_status != status)
5892 in->card_status = status;
5893 pthread_mutex_unlock(&in->lock);
5894
5895 ALOGW("in_snd_mon_cb for card %d usecase %s, status %s", card,
5896 use_case_table[in->usecase],
5897 status == CARD_STATUS_OFFLINE ? "offline" : "online");
5898
5899 // a better solution would be to report error back to AF and let
5900 // it put the stream to standby
5901 if (status == CARD_STATUS_OFFLINE)
5902 in_standby(&in->stream.common);
5903
5904 return;
5905}
5906
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005907static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
5908{
5909 struct stream_in *in = (struct stream_in *)stream;
5910 struct audio_device *adev = in->dev;
5911 struct str_parms *parms;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005912 char value[32];
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08005913 int ret = 0, val = 0, err;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005914
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05305915 ALOGD("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005916 parms = str_parms_create_str(kvpairs);
5917
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05305918 if (!parms)
5919 goto error;
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005920 lock_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08005921 pthread_mutex_lock(&adev->lock);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08005922
5923 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
5924 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005925 val = atoi(value);
5926 /* no audio source uses val == 0 */
5927 if ((in->source != val) && (val != 0)) {
5928 in->source = val;
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08005929 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
5930 (in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
5931 (voice_extn_compress_voip_is_format_supported(in->format)) &&
Shiv Maliyappanahallie66aba22016-01-27 16:08:57 -08005932 (in->config.rate == 8000 || in->config.rate == 16000 ||
5933 in->config.rate == 32000 || in->config.rate == 48000 ) &&
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07005934 (audio_channel_count_from_in_mask(in->channel_mask) == 1)) {
Narsinga Rao Chella7d5a3e82014-02-04 16:23:52 -08005935 err = voice_extn_compress_voip_open_input_stream(in);
5936 if (err != 0) {
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08005937 ALOGE("%s: Compress voip input cannot be opened, error:%d",
Narsinga Rao Chella7d5a3e82014-02-04 16:23:52 -08005938 __func__, err);
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08005939 }
5940 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005941 }
5942 }
5943
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08005944 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
5945 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005946 val = atoi(value);
Kevin Rocardfce19002017-08-07 19:21:36 -07005947 if (((int)in->device != val) && (val != 0) && audio_is_input_device(val) ) {
5948
5949 // Workaround: If routing to an non existing usb device, fail gracefully
5950 // The routing request will otherwise block during 10 second
5951 int card;
5952 if (audio_is_usb_in_device(val) &&
5953 (card = get_alive_usb_card(parms)) >= 0) {
5954
5955 ALOGW("in_set_parameters() ignoring rerouting to non existing USB card %d", card);
5956 ret = -ENOSYS;
5957 } else {
5958
5959 in->device = val;
5960 /* If recording is in progress, change the tx device to new device */
5961 if (!in->standby && !in->is_st_session) {
5962 ALOGV("update input routing change");
5963 // inform adm before actual routing to prevent glitches.
5964 if (adev->adm_on_routing_change) {
Haynes Mathew George822b5492016-07-01 16:57:24 -07005965 adev->adm_on_routing_change(adev->adm_data,
5966 in->capture_handle);
Kevin Rocardfce19002017-08-07 19:21:36 -07005967 ret = select_devices(adev, in->usecase);
5968 }
5969 }
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005970 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005971 }
5972 }
5973
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +05305974 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_PROFILE, value, sizeof(value));
5975 if (err >= 0) {
5976 strlcpy(in->profile, value, sizeof(in->profile));
5977 ALOGV("updating stream profile with value '%s'", in->profile);
5978 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
5979 &adev->streams_input_cfg_list,
5980 in->device, in->flags, in->format,
5981 in->sample_rate, in->bit_width,
5982 in->profile, &in->app_type_cfg);
5983 }
5984
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005985 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08005986 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005987
5988 str_parms_destroy(parms);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05305989error:
Eric Laurent994a6932013-07-17 11:51:42 -07005990 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005991 return ret;
5992}
5993
5994static char* in_get_parameters(const struct audio_stream *stream,
5995 const char *keys)
5996{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08005997 struct stream_in *in = (struct stream_in *)stream;
5998 struct str_parms *query = str_parms_create_str(keys);
5999 char *str;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08006000 struct str_parms *reply = str_parms_create();
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07006001
6002 if (!query || !reply) {
Alexy Josephaee4fdd2016-01-29 13:02:07 -08006003 if (reply) {
6004 str_parms_destroy(reply);
6005 }
6006 if (query) {
6007 str_parms_destroy(query);
6008 }
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07006009 ALOGE("in_get_parameters: failed to create query or reply");
6010 return NULL;
6011 }
6012
Haynes Mathew George484e8d22017-07-31 18:55:17 -07006013 ALOGV("%s: enter: keys - %s %s ", __func__, use_case_table[in->usecase], keys);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08006014
6015 voice_extn_in_get_parameters(in, query, reply);
6016
Haynes Mathew George484e8d22017-07-31 18:55:17 -07006017 stream_get_parameter_channels(query, reply,
6018 &in->supported_channel_masks[0]);
6019 stream_get_parameter_formats(query, reply,
6020 &in->supported_formats[0]);
6021 stream_get_parameter_rates(query, reply,
6022 &in->supported_sample_rates[0]);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08006023 str = str_parms_to_str(reply);
6024 str_parms_destroy(query);
6025 str_parms_destroy(reply);
6026
6027 ALOGV("%s: exit: returns - %s", __func__, str);
6028 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006029}
6030
Aalique Grahame22e49102018-12-18 14:23:57 -08006031static int in_set_gain(struct audio_stream_in *stream,
6032 float gain)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006033{
Aalique Grahame22e49102018-12-18 14:23:57 -08006034 struct stream_in *in = (struct stream_in *)stream;
6035 char mixer_ctl_name[128];
6036 struct mixer_ctl *ctl;
6037 int ctl_value;
6038
6039 ALOGV("%s: gain %f", __func__, gain);
6040
6041 if (stream == NULL)
6042 return -EINVAL;
6043
6044 /* in_set_gain() only used to silence MMAP capture for now */
6045 if (in->usecase != USECASE_AUDIO_RECORD_MMAP)
6046 return -ENOSYS;
6047
6048 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name), "Capture %d Volume", in->pcm_device_id);
6049
6050 ctl = mixer_get_ctl_by_name(in->dev->mixer, mixer_ctl_name);
6051 if (!ctl) {
6052 ALOGW("%s: Could not get ctl for mixer cmd - %s",
6053 __func__, mixer_ctl_name);
6054 return -ENOSYS;
6055 }
6056
6057 if (gain < RECORD_GAIN_MIN)
6058 gain = RECORD_GAIN_MIN;
6059 else if (gain > RECORD_GAIN_MAX)
6060 gain = RECORD_GAIN_MAX;
6061 ctl_value = (int)(RECORD_VOLUME_CTL_MAX * gain);
6062
6063 mixer_ctl_set_value(ctl, 0, ctl_value);
6064
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006065 return 0;
6066}
6067
6068static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
6069 size_t bytes)
6070{
6071 struct stream_in *in = (struct stream_in *)stream;
Pallavid7c7a272018-01-16 11:22:55 +05306072
6073 if (in == NULL) {
6074 ALOGE("%s: stream_in ptr is NULL", __func__);
6075 return -EINVAL;
6076 }
6077
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006078 struct audio_device *adev = in->dev;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05306079 int ret = -1;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05306080 size_t bytes_read = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006081
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006082 lock_input_stream(in);
Naresh Tanniru4c630392014-05-12 01:05:52 +05306083
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07006084 if (in->is_st_session) {
6085 ALOGVV(" %s: reading on st session bytes=%zu", __func__, bytes);
6086 /* Read from sound trigger HAL */
6087 audio_extn_sound_trigger_read(in, buffer, bytes);
Quinn Malef6050362019-01-30 15:55:40 -08006088 if (in->standby) {
6089 adev->num_va_sessions++;
6090 in->standby = 0;
6091 }
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07006092 pthread_mutex_unlock(&in->lock);
6093 return bytes;
6094 }
6095
Haynes Mathew George16081042017-05-31 17:16:49 -07006096 if (in->usecase == USECASE_AUDIO_RECORD_MMAP) {
6097 ret = -ENOSYS;
6098 goto exit;
6099 }
6100
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006101 if (in->standby) {
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07006102 pthread_mutex_lock(&adev->lock);
6103 if (in->usecase == USECASE_COMPRESS_VOIP_CALL)
6104 ret = voice_extn_compress_voip_start_input_stream(in);
6105 else
6106 ret = start_input_stream(in);
Quinn Malef6050362019-01-30 15:55:40 -08006107 if (!ret && in->source == AUDIO_SOURCE_VOICE_RECOGNITION)
6108 adev->num_va_sessions++;
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07006109 pthread_mutex_unlock(&adev->lock);
6110 if (ret != 0) {
6111 goto exit;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006112 }
6113 in->standby = 0;
6114 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006115
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006116 // what's the duration requested by the client?
6117 long ns = 0;
6118
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05306119 if (in->pcm && in->config.rate)
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006120 ns = pcm_bytes_to_frames(in->pcm, bytes)*1000000000LL/
6121 in->config.rate;
6122
6123 request_in_focus(in, ns);
6124 bool use_mmap = is_mmap_usecase(in->usecase) || in->realtime;
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07006125
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05306126 if (audio_extn_cin_attached_usecase(in->usecase)) {
6127 ret = audio_extn_cin_read(in, buffer, bytes, &bytes_read);
6128 } else if (in->pcm) {
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05306129 if (audio_extn_ssr_get_stream() == in) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07006130 ret = audio_extn_ssr_read(stream, buffer, bytes);
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05306131 } else if (audio_extn_compr_cap_usecase_supported(in->usecase)) {
Mingming Yine62d7842013-10-25 16:26:03 -07006132 ret = audio_extn_compr_cap_read(in, buffer, bytes);
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006133 } else if (use_mmap) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07006134 ret = pcm_mmap_read(in->pcm, buffer, bytes);
Garmond Leunge2433c32017-09-28 21:51:22 -07006135 } else if (audio_extn_ffv_get_stream() == in) {
6136 ret = audio_extn_ffv_read(stream, buffer, bytes);
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05306137 } else {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07006138 ret = pcm_read(in->pcm, buffer, bytes);
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05306139 /* data from DSP comes in 24_8 format, convert it to 8_24 */
6140 if (!ret && bytes > 0 && (in->format == AUDIO_FORMAT_PCM_8_24_BIT)) {
6141 if (audio_extn_utils_convert_format_24_8_to_8_24(buffer, bytes)
6142 != bytes) {
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05306143 ret = -EINVAL;
6144 goto exit;
6145 }
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05306146 } else if (ret < 0) {
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05306147 ret = -errno;
6148 }
6149 }
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05306150 /* bytes read is always set to bytes for non compress usecases */
6151 bytes_read = bytes;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006152 }
6153
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006154 release_in_focus(in);
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07006155
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006156 /*
Quinn Malef6050362019-01-30 15:55:40 -08006157 * Instead of writing zeroes here, we could trust the hardware to always
6158 * provide zeroes when muted. This is also muted with voice recognition
6159 * usecases so that other clients do not have access to voice recognition
6160 * data.
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006161 */
Quinn Malef6050362019-01-30 15:55:40 -08006162 if ((ret == 0 && voice_get_mic_mute(adev) &&
6163 !voice_is_in_call_rec_stream(in) &&
6164 in->usecase != USECASE_AUDIO_RECORD_AFE_PROXY) ||
6165 (adev->num_va_sessions &&
6166 in->source != AUDIO_SOURCE_VOICE_RECOGNITION &&
6167 property_get_bool("persist.vendor.audio.va_concurrency_mute_enabled",
6168 false)))
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006169 memset(buffer, 0, bytes);
6170
6171exit:
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07006172 if (-ENETRESET == ret)
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306173 in->card_status = CARD_STATUS_OFFLINE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006174 pthread_mutex_unlock(&in->lock);
6175
6176 if (ret != 0) {
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05306177 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05306178 pthread_mutex_lock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05306179 voice_extn_compress_voip_close_input_stream(&in->stream.common);
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05306180 pthread_mutex_unlock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05306181 in->standby = true;
6182 }
Sharad Sangled17c9122017-03-20 15:58:52 +05306183 if (!audio_extn_cin_attached_usecase(in->usecase)) {
6184 bytes_read = bytes;
6185 memset(buffer, 0, bytes);
6186 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006187 in_standby(&in->stream.common);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07006188 ALOGV("%s: read failed status %d- sleeping for buffer duration", __func__, ret);
Ashish Jainbbce4322016-02-16 13:25:27 +05306189 usleep((uint64_t)bytes * 1000000 / audio_stream_in_frame_size(stream) /
Naresh Tanniru4c630392014-05-12 01:05:52 +05306190 in_get_sample_rate(&in->stream.common));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006191 }
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05306192 return bytes_read;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006193}
6194
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07006195static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006196{
6197 return 0;
6198}
6199
Aalique Grahame22e49102018-12-18 14:23:57 -08006200static int in_get_capture_position(const struct audio_stream_in *stream,
6201 int64_t *frames, int64_t *time)
6202{
6203 if (stream == NULL || frames == NULL || time == NULL) {
6204 return -EINVAL;
6205 }
6206 struct stream_in *in = (struct stream_in *)stream;
6207 int ret = -ENOSYS;
6208
6209 lock_input_stream(in);
6210 // note: ST sessions do not close the alsa pcm driver synchronously
6211 // on standby. Therefore, we may return an error even though the
6212 // pcm stream is still opened.
6213 if (in->standby) {
6214 ALOGE_IF(in->pcm != NULL && !in->is_st_session,
6215 "%s stream in standby but pcm not NULL for non ST session", __func__);
6216 goto exit;
6217 }
6218 if (in->pcm) {
6219 struct timespec timestamp;
6220 unsigned int avail;
6221 if (pcm_get_htimestamp(in->pcm, &avail, &timestamp) == 0) {
6222 *frames = in->frames_read + avail;
6223 *time = timestamp.tv_sec * 1000000000LL + timestamp.tv_nsec;
6224 ret = 0;
6225 }
6226 }
6227exit:
6228 pthread_mutex_unlock(&in->lock);
6229 return ret;
6230}
6231
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07006232static int add_remove_audio_effect(const struct audio_stream *stream,
6233 effect_handle_t effect,
6234 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006235{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07006236 struct stream_in *in = (struct stream_in *)stream;
6237 int status = 0;
6238 effect_descriptor_t desc;
6239
6240 status = (*effect)->get_descriptor(effect, &desc);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07006241 ALOGV("%s: status %d in->standby %d enable:%d", __func__, status, in->standby, enable);
6242
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07006243 if (status != 0)
6244 return status;
6245
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006246 lock_input_stream(in);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07006247 pthread_mutex_lock(&in->dev->lock);
kunleizd96526c2018-04-09 11:12:32 +08006248 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
Aalique Grahame22e49102018-12-18 14:23:57 -08006249 in->source == AUDIO_SOURCE_VOICE_RECOGNITION ||
kunleizd96526c2018-04-09 11:12:32 +08006250 in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07006251 in->enable_aec != enable &&
6252 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
6253 in->enable_aec = enable;
Aalique Grahame22e49102018-12-18 14:23:57 -08006254 if (!enable)
6255 platform_set_echo_reference(in->dev, enable, AUDIO_DEVICE_NONE);
6256 if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
6257 in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) {
6258 in->dev->enable_voicerx = enable;
6259 struct audio_usecase *usecase;
6260 struct listnode *node;
6261 list_for_each(node, &in->dev->usecase_list) {
6262 usecase = node_to_item(node, struct audio_usecase, list);
6263 if (usecase->type == PCM_PLAYBACK)
6264 select_devices(in->dev, usecase->id);
6265 }
6266 }
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07006267 if (!in->standby) {
6268 if (enable_disable_effect(in->dev, EFFECT_AEC, enable) == ENOSYS)
6269 select_devices(in->dev, in->usecase);
6270 }
6271
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07006272 }
Ravi Kumar Alamanda198185e2013-11-07 15:42:19 -08006273 if (in->enable_ns != enable &&
6274 (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
6275 in->enable_ns = enable;
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07006276 if (!in->standby) {
kunleizd96526c2018-04-09 11:12:32 +08006277 if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
6278 in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) {
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07006279 if (enable_disable_effect(in->dev, EFFECT_NS, enable) == ENOSYS)
6280 select_devices(in->dev, in->usecase);
6281 } else
6282 select_devices(in->dev, in->usecase);
6283 }
Ravi Kumar Alamanda198185e2013-11-07 15:42:19 -08006284 }
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07006285 pthread_mutex_unlock(&in->dev->lock);
6286 pthread_mutex_unlock(&in->lock);
6287
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006288 return 0;
6289}
6290
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07006291static int in_add_audio_effect(const struct audio_stream *stream,
6292 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006293{
Eric Laurent994a6932013-07-17 11:51:42 -07006294 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07006295 return add_remove_audio_effect(stream, effect, true);
6296}
6297
6298static int in_remove_audio_effect(const struct audio_stream *stream,
6299 effect_handle_t effect)
6300{
Eric Laurent994a6932013-07-17 11:51:42 -07006301 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07006302 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006303}
6304
Derek Chenf939fb72018-11-13 13:34:41 -08006305streams_input_ctxt_t *in_get_stream(struct audio_device *dev,
6306 audio_io_handle_t input)
6307{
6308 struct listnode *node;
6309
6310 list_for_each(node, &dev->active_inputs_list) {
6311 streams_input_ctxt_t *in_ctxt = node_to_item(node,
6312 streams_input_ctxt_t,
6313 list);
6314 if (in_ctxt->input->capture_handle == input) {
6315 return in_ctxt;
6316 }
6317 }
6318 return NULL;
6319}
6320
6321streams_output_ctxt_t *out_get_stream(struct audio_device *dev,
6322 audio_io_handle_t output)
6323{
6324 struct listnode *node;
6325
6326 list_for_each(node, &dev->active_outputs_list) {
6327 streams_output_ctxt_t *out_ctxt = node_to_item(node,
6328 streams_output_ctxt_t,
6329 list);
6330 if (out_ctxt->output->handle == output) {
6331 return out_ctxt;
6332 }
6333 }
6334 return NULL;
6335}
6336
Haynes Mathew George16081042017-05-31 17:16:49 -07006337static int in_stop(const struct audio_stream_in* stream)
6338{
6339 struct stream_in *in = (struct stream_in *)stream;
6340 struct audio_device *adev = in->dev;
6341
6342 int ret = -ENOSYS;
6343 ALOGV("%s", __func__);
6344 pthread_mutex_lock(&adev->lock);
6345 if (in->usecase == USECASE_AUDIO_RECORD_MMAP && !in->standby &&
6346 in->capture_started && in->pcm != NULL) {
6347 pcm_stop(in->pcm);
6348 ret = stop_input_stream(in);
6349 in->capture_started = false;
6350 }
6351 pthread_mutex_unlock(&adev->lock);
6352 return ret;
6353}
6354
6355static int in_start(const struct audio_stream_in* stream)
6356{
6357 struct stream_in *in = (struct stream_in *)stream;
6358 struct audio_device *adev = in->dev;
6359 int ret = -ENOSYS;
6360
6361 ALOGV("%s in %p", __func__, in);
6362 pthread_mutex_lock(&adev->lock);
6363 if (in->usecase == USECASE_AUDIO_RECORD_MMAP && !in->standby &&
6364 !in->capture_started && in->pcm != NULL) {
6365 if (!in->capture_started) {
6366 ret = start_input_stream(in);
6367 if (ret == 0) {
6368 in->capture_started = true;
6369 }
6370 }
6371 }
6372 pthread_mutex_unlock(&adev->lock);
6373 return ret;
6374}
6375
6376static int in_create_mmap_buffer(const struct audio_stream_in *stream,
6377 int32_t min_size_frames,
6378 struct audio_mmap_buffer_info *info)
6379{
6380 struct stream_in *in = (struct stream_in *)stream;
6381 struct audio_device *adev = in->dev;
6382 int ret = 0;
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07006383 unsigned int offset1 = 0;
6384 unsigned int frames1 = 0;
Haynes Mathew George16081042017-05-31 17:16:49 -07006385 const char *step = "";
Arun Mirpuri5d170872019-03-26 13:21:31 -07006386 uint32_t mmap_size = 0;
6387 uint32_t buffer_size = 0;
Haynes Mathew George16081042017-05-31 17:16:49 -07006388
6389 pthread_mutex_lock(&adev->lock);
6390 ALOGV("%s in %p", __func__, in);
6391
Sharad Sanglec6f32552018-05-04 16:15:38 +05306392 if (CARD_STATUS_OFFLINE == in->card_status||
6393 CARD_STATUS_OFFLINE == adev->card_status) {
6394 ALOGW("in->card_status or adev->card_status offline, try again");
6395 ret = -EIO;
6396 goto exit;
6397 }
6398
Haynes Mathew George16081042017-05-31 17:16:49 -07006399 if (info == NULL || min_size_frames == 0) {
6400 ALOGE("%s invalid argument info %p min_size_frames %d", __func__, info, min_size_frames);
6401 ret = -EINVAL;
6402 goto exit;
6403 }
6404 if (in->usecase != USECASE_AUDIO_RECORD_MMAP || !in->standby) {
6405 ALOGE("%s: usecase = %d, standby = %d", __func__, in->usecase, in->standby);
6406 ALOGV("%s in %p", __func__, in);
6407 ret = -ENOSYS;
6408 goto exit;
6409 }
6410 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
6411 if (in->pcm_device_id < 0) {
6412 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
6413 __func__, in->pcm_device_id, in->usecase);
6414 ret = -EINVAL;
6415 goto exit;
6416 }
6417
6418 adjust_mmap_period_count(&in->config, min_size_frames);
6419
6420 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
6421 __func__, adev->snd_card, in->pcm_device_id, in->config.channels);
6422 in->pcm = pcm_open(adev->snd_card, in->pcm_device_id,
6423 (PCM_IN | PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC), &in->config);
Satish Babu Patakokila54ce83d2018-07-06 18:00:37 +05306424 if (errno == ENETRESET && !pcm_is_ready(in->pcm)) {
Sharad Sanglec6f32552018-05-04 16:15:38 +05306425 ALOGE("%s: pcm_open failed errno:%d\n", __func__, errno);
6426 in->card_status = CARD_STATUS_OFFLINE;
6427 adev->card_status = CARD_STATUS_OFFLINE;
6428 ret = -EIO;
6429 goto exit;
6430 }
6431
Haynes Mathew George16081042017-05-31 17:16:49 -07006432 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) {
6433 step = "open";
6434 ret = -ENODEV;
6435 goto exit;
6436 }
6437
6438 ret = pcm_mmap_begin(in->pcm, &info->shared_memory_address, &offset1, &frames1);
6439 if (ret < 0) {
6440 step = "begin";
6441 goto exit;
6442 }
Haynes Mathew George16081042017-05-31 17:16:49 -07006443
Arun Mirpuri5d170872019-03-26 13:21:31 -07006444 info->buffer_size_frames = pcm_get_buffer_size(in->pcm);
6445 buffer_size = pcm_frames_to_bytes(in->pcm, info->buffer_size_frames);
6446 info->burst_size_frames = in->config.period_size;
6447 ret = platform_get_mmap_data_fd(adev->platform,
6448 in->pcm_device_id, 1 /*capture*/,
6449 &info->shared_memory_fd,
6450 &mmap_size);
6451 if (ret < 0) {
6452 // Fall back to non exclusive mode
6453 info->shared_memory_fd = pcm_get_poll_fd(in->pcm);
6454 } else {
6455 if (mmap_size < buffer_size) {
6456 step = "mmap";
6457 goto exit;
6458 }
6459 // FIXME: indicate exclusive mode support by returning a negative buffer size
6460 info->buffer_size_frames *= -1;
6461 }
6462
6463 memset(info->shared_memory_address, 0, buffer_size);
Haynes Mathew George16081042017-05-31 17:16:49 -07006464
6465 ret = pcm_mmap_commit(in->pcm, 0, MMAP_PERIOD_SIZE);
6466 if (ret < 0) {
6467 step = "commit";
6468 goto exit;
6469 }
6470
6471 in->standby = false;
6472 ret = 0;
6473
6474 ALOGV("%s: got mmap buffer address %p info->buffer_size_frames %d",
6475 __func__, info->shared_memory_address, info->buffer_size_frames);
6476
6477exit:
6478 if (ret != 0) {
6479 if (in->pcm == NULL) {
6480 ALOGE("%s: %s - %d", __func__, step, ret);
6481 } else {
6482 ALOGE("%s: %s %s", __func__, step, pcm_get_error(in->pcm));
6483 pcm_close(in->pcm);
6484 in->pcm = NULL;
6485 }
6486 }
6487 pthread_mutex_unlock(&adev->lock);
6488 return ret;
6489}
6490
6491static int in_get_mmap_position(const struct audio_stream_in *stream,
6492 struct audio_mmap_position *position)
6493{
6494 struct stream_in *in = (struct stream_in *)stream;
6495 ALOGVV("%s", __func__);
6496 if (position == NULL) {
6497 return -EINVAL;
6498 }
6499 if (in->usecase != USECASE_AUDIO_RECORD_MMAP) {
6500 return -ENOSYS;
6501 }
6502 if (in->pcm == NULL) {
6503 return -ENOSYS;
6504 }
6505 struct timespec ts = { 0, 0 };
6506 int ret = pcm_mmap_get_hw_ptr(in->pcm, (unsigned int *)&position->position_frames, &ts);
6507 if (ret < 0) {
6508 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
6509 return ret;
6510 }
Naresh Tanniru119d74a2018-11-02 09:49:44 +05306511 position->time_nanoseconds = ts.tv_sec*1000000000LL + ts.tv_nsec;
Haynes Mathew George16081042017-05-31 17:16:49 -07006512 return 0;
6513}
6514
Naresh Tannirudcb47c52018-06-25 16:23:32 +05306515static int in_get_active_microphones(const struct audio_stream_in *stream,
6516 struct audio_microphone_characteristic_t *mic_array,
6517 size_t *mic_count) {
6518 struct stream_in *in = (struct stream_in *)stream;
6519 struct audio_device *adev = in->dev;
6520 ALOGVV("%s", __func__);
6521
6522 lock_input_stream(in);
6523 pthread_mutex_lock(&adev->lock);
6524 int ret = platform_get_active_microphones(adev->platform,
6525 audio_channel_count_from_in_mask(in->channel_mask),
6526 in->usecase, mic_array, mic_count);
6527 pthread_mutex_unlock(&adev->lock);
6528 pthread_mutex_unlock(&in->lock);
6529
6530 return ret;
6531}
6532
6533static int adev_get_microphones(const struct audio_hw_device *dev,
6534 struct audio_microphone_characteristic_t *mic_array,
6535 size_t *mic_count) {
6536 struct audio_device *adev = (struct audio_device *)dev;
6537 ALOGVV("%s", __func__);
6538
6539 pthread_mutex_lock(&adev->lock);
6540 int ret = platform_get_microphones(adev->platform, mic_array, mic_count);
6541 pthread_mutex_unlock(&adev->lock);
6542
6543 return ret;
6544}
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05306545int adev_open_output_stream(struct audio_hw_device *dev,
Haynes Mathew George16081042017-05-31 17:16:49 -07006546 audio_io_handle_t handle,
6547 audio_devices_t devices,
6548 audio_output_flags_t flags,
6549 struct audio_config *config,
6550 struct audio_stream_out **stream_out,
6551 const char *address __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006552{
6553 struct audio_device *adev = (struct audio_device *)dev;
6554 struct stream_out *out;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05306555 int ret = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07006556 audio_format_t format;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08006557 struct adsp_hdlr_stream_cfg hdlr_stream_cfg;
Manish Dewangan21a850a2017-08-14 12:03:55 +05306558 bool is_direct_passthough = false;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07006559 bool is_hdmi = devices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
6560 bool is_usb_dev = audio_is_usb_out_device(devices) &&
6561 (devices != AUDIO_DEVICE_OUT_USB_ACCESSORY);
6562 bool direct_dev = is_hdmi || is_usb_dev;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08006563 bool use_db_as_primary =
Aalique Grahame6e763712019-01-31 16:18:17 -08006564 audio_feature_manager_is_feature_enabled(USE_DEEP_BUFFER_AS_PRIMARY_OUTPUT);
Vignesh Kulothungana6927272019-02-20 15:17:07 -08006565 bool force_haptic_path =
6566 property_get_bool("vendor.audio.test_haptic", false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006567
kunleizdff872d2018-08-20 14:40:33 +08006568 if (is_usb_dev && (!audio_extn_usb_connected(NULL))) {
kunleizd6a9e0c2018-07-30 15:38:52 +08006569 is_usb_dev = false;
6570 devices = AUDIO_DEVICE_OUT_SPEAKER;
6571 ALOGW("%s: ignore set device to non existing USB card, use output device(%#x)",
6572 __func__, devices);
6573 }
6574
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006575 *stream_out = NULL;
Naresh Tanniru80659832014-06-04 18:17:56 +05306576
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006577 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
6578
Mingming Yin3a941d42016-02-17 18:08:05 -08006579 ALOGD("%s: enter: format(%#x) sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)\
6580 stream_handle(%p)", __func__, config->format, config->sample_rate, config->channel_mask,
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05306581 devices, flags, &out->stream);
6582
6583
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08006584 if (!out) {
6585 return -ENOMEM;
6586 }
6587
Haynes Mathew George204045b2015-02-25 20:32:03 -08006588 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006589 pthread_mutex_init(&out->pre_lock, (const pthread_mutexattr_t *) NULL);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05306590 pthread_mutex_init(&out->compr_mute_lock, (const pthread_mutexattr_t *) NULL);
Zhou Song48453a02018-01-10 17:50:59 +08006591 pthread_mutex_init(&out->position_query_lock, (const pthread_mutexattr_t *) NULL);
Haynes Mathew George204045b2015-02-25 20:32:03 -08006592 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
6593
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006594 if (devices == AUDIO_DEVICE_NONE)
6595 devices = AUDIO_DEVICE_OUT_SPEAKER;
6596
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006597 out->flags = flags;
6598 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07006599 out->dev = adev;
Aalique Grahame65780b52017-09-27 14:59:56 -07006600 out->hal_op_format = out->hal_ip_format = format = out->format = config->format;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006601 out->sample_rate = config->sample_rate;
Sachin Mohan Gadag3d09acd2017-06-19 12:43:44 +05306602 out->channel_mask = config->channel_mask;
Ramjee Singh5857aeb2017-08-03 19:18:50 +05306603 if (out->channel_mask == AUDIO_CHANNEL_NONE)
6604 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
6605 else
6606 out->supported_channel_masks[0] = out->channel_mask;
Eric Laurentc4aef752013-09-12 17:45:53 -07006607 out->handle = handle;
Mingming Yin3ee55c62014-08-04 14:23:35 -07006608 out->bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Alexy Josephaa54c872014-12-03 02:46:47 -08006609 out->non_blocking = 0;
Ashish Jain83a6cc22016-06-28 14:34:17 +05306610 out->convert_buffer = NULL;
Ashish Jain1b9b30c2017-05-18 20:57:40 +05306611 out->started = 0;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05306612 out->a2dp_compress_mute = false;
Aniket Kumar Lata932f4872017-11-06 18:29:44 -08006613 out->hal_output_suspend_supported = 0;
6614 out->dynamic_pm_qos_config_supported = 0;
Surendar Karkaf51b5842018-04-26 11:28:38 +05306615 out->set_dual_mono = false;
Manisha Agarwal7b3e3772019-02-20 14:33:45 +05306616 out->prev_card_status_offline = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006617
Nikhil Laturkar26b690b2017-07-25 11:06:14 +05306618 if ((flags & AUDIO_OUTPUT_FLAG_BD) &&
Satish Babu Patakokila37e7c482018-02-02 11:50:06 +05306619 (property_get_bool("vendor.audio.matrix.limiter.enable", false)))
Ben Romberger6c4d3812017-06-13 17:46:45 -07006620 platform_set_device_params(out, DEVICE_PARAM_LIMITER_ID, 1);
6621
Aalique Grahame22e49102018-12-18 14:23:57 -08006622 if (direct_dev &&
6623 (audio_is_linear_pcm(out->format) ||
6624 config->format == AUDIO_FORMAT_DEFAULT) &&
6625 out->flags == AUDIO_OUTPUT_FLAG_NONE) {
6626 audio_format_t req_format = config->format;
6627 audio_channel_mask_t req_channel_mask = config->channel_mask;
6628 uint32_t req_sample_rate = config->sample_rate;
6629
6630 pthread_mutex_lock(&adev->lock);
6631 if (is_hdmi) {
6632 ALOGV("AUDIO_DEVICE_OUT_AUX_DIGITAL and DIRECT|OFFLOAD, check hdmi caps");
6633 ret = read_hdmi_sink_caps(out);
6634 if (config->sample_rate == 0)
6635 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
6636 if (config->channel_mask == AUDIO_CHANNEL_NONE)
6637 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
6638 if (config->format == AUDIO_FORMAT_DEFAULT)
6639 config->format = AUDIO_FORMAT_PCM_16_BIT;
6640 } else if (is_usb_dev) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07006641 ret = read_usb_sup_params_and_compare(true /*is_playback*/,
6642 &config->format,
6643 &out->supported_formats[0],
6644 MAX_SUPPORTED_FORMATS,
6645 &config->channel_mask,
6646 &out->supported_channel_masks[0],
6647 MAX_SUPPORTED_CHANNEL_MASKS,
6648 &config->sample_rate,
6649 &out->supported_sample_rates[0],
6650 MAX_SUPPORTED_SAMPLE_RATES);
6651 ALOGV("plugged dev USB ret %d", ret);
Haynes Mathew George484e8d22017-07-31 18:55:17 -07006652 }
Aalique Grahame22e49102018-12-18 14:23:57 -08006653
Haynes Mathew George484e8d22017-07-31 18:55:17 -07006654 pthread_mutex_unlock(&adev->lock);
6655 if (ret != 0) {
Mingming Yin3a941d42016-02-17 18:08:05 -08006656 if (ret == -ENOSYS) {
6657 /* ignore and go with default */
6658 ret = 0;
Aalique Grahame22e49102018-12-18 14:23:57 -08006659 }
6660 // For MMAP NO IRQ, allow conversions in ADSP
6661 else if (is_hdmi || (flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) == 0)
6662 goto error_open;
6663 else {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07006664 ALOGE("error reading direct dev sink caps");
Mingming Yin3a941d42016-02-17 18:08:05 -08006665 goto error_open;
6666 }
Aalique Grahame22e49102018-12-18 14:23:57 -08006667
6668 if (req_sample_rate != 0 && config->sample_rate != req_sample_rate)
6669 config->sample_rate = req_sample_rate;
6670 if (req_channel_mask != AUDIO_CHANNEL_NONE && config->channel_mask != req_channel_mask)
6671 config->channel_mask = req_channel_mask;
6672 if (req_format != AUDIO_FORMAT_DEFAULT && config->format != req_format)
6673 config->format = req_format;
Mingming Yin3a941d42016-02-17 18:08:05 -08006674 }
Aalique Grahame22e49102018-12-18 14:23:57 -08006675
6676 out->sample_rate = config->sample_rate;
6677 out->channel_mask = config->channel_mask;
6678 out->format = config->format;
6679 if (is_hdmi) {
6680 out->usecase = USECASE_AUDIO_PLAYBACK_HIFI;
6681 out->config = pcm_config_hdmi_multi;
6682 } else if (flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) {
6683 out->usecase = USECASE_AUDIO_PLAYBACK_MMAP;
6684 out->config = pcm_config_mmap_playback;
6685 out->stream.start = out_start;
6686 out->stream.stop = out_stop;
6687 out->stream.create_mmap_buffer = out_create_mmap_buffer;
6688 out->stream.get_mmap_position = out_get_mmap_position;
6689 } else {
6690 out->usecase = USECASE_AUDIO_PLAYBACK_HIFI;
6691 out->config = pcm_config_hifi;
6692 }
6693
6694 out->config.rate = out->sample_rate;
6695 out->config.channels = audio_channel_count_from_out_mask(out->channel_mask);
6696 if (is_hdmi) {
6697 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels *
6698 audio_bytes_per_sample(out->format));
6699 }
6700 out->config.format = pcm_format_from_audio_format(out->format);
Mingming Yin3a941d42016-02-17 18:08:05 -08006701 }
6702
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08006703 /* Check for VOIP usecase */
6704 if(out->flags == (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_VOIP_RX)) {
6705 if (!voice_extn_is_compress_voip_supported()) {
6706 if (out->sample_rate == 8000 || out->sample_rate == 16000 ||
6707 out->sample_rate == 32000 || out->sample_rate == 48000) {
kunleizf6b5b082019-03-18 17:32:25 +08006708 out->channel_mask = AUDIO_CHANNEL_OUT_MONO;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08006709 out->usecase = USECASE_AUDIO_PLAYBACK_VOIP;
6710 out->format = AUDIO_FORMAT_PCM_16_BIT;
Vikram Panduranga93f080e2017-06-07 18:16:14 -07006711
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08006712 out->config = default_pcm_config_voip_copp;
6713 out->config.period_size = VOIP_IO_BUF_SIZE(out->sample_rate, DEFAULT_VOIP_BUF_DURATION_MS, DEFAULT_VOIP_BIT_DEPTH_BYTE)/2;
6714 out->config.rate = out->sample_rate;
6715 }
6716 } else {
6717 if ((out->dev->mode == AUDIO_MODE_IN_COMMUNICATION ||
6718 voice_extn_compress_voip_is_active(out->dev)) &&
6719 (voice_extn_compress_voip_is_config_supported(config))) {
6720 ret = voice_extn_compress_voip_open_output_stream(out);
6721 if (ret != 0) {
6722 ALOGE("%s: Compress voip output cannot be opened, error:%d",
6723 __func__, ret);
6724 goto error_open;
6725 }
6726 }
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08006727 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07006728 } else if (audio_is_linear_pcm(out->format) &&
6729 out->flags == AUDIO_OUTPUT_FLAG_NONE && is_usb_dev) {
6730 out->channel_mask = config->channel_mask;
6731 out->sample_rate = config->sample_rate;
6732 out->format = config->format;
6733 out->usecase = USECASE_AUDIO_PLAYBACK_HIFI;
6734 // does this change?
6735 out->config = is_hdmi ? pcm_config_hdmi_multi : pcm_config_hifi;
6736 out->config.rate = config->sample_rate;
6737 out->config.channels = audio_channel_count_from_out_mask(out->channel_mask);
6738 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels *
6739 audio_bytes_per_sample(config->format));
6740 out->config.format = pcm_format_from_audio_format(out->format);
vivek mehta0ea887a2015-08-26 14:01:20 -07006741 } else if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
Dhananjay Kumarac341582017-02-23 23:42:25 +05306742 (out->flags == AUDIO_OUTPUT_FLAG_DIRECT)) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306743 pthread_mutex_lock(&adev->lock);
6744 bool offline = (adev->card_status == CARD_STATUS_OFFLINE);
6745 pthread_mutex_unlock(&adev->lock);
6746
6747 // reject offload during card offline to allow
6748 // fallback to s/w paths
6749 if (offline) {
6750 ret = -ENODEV;
6751 goto error_open;
6752 }
vivek mehta0ea887a2015-08-26 14:01:20 -07006753
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006754 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
6755 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
6756 ALOGE("%s: Unsupported Offload information", __func__);
6757 ret = -EINVAL;
6758 goto error_open;
6759 }
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07006760
Atul Khare3fa6e542017-08-09 00:56:17 +05306761 if (config->offload_info.format == 0)
6762 config->offload_info.format = config->format;
6763 if (config->offload_info.sample_rate == 0)
6764 config->offload_info.sample_rate = config->sample_rate;
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07006765
Mingming Yin90310102013-11-13 16:57:00 -08006766 if (!is_supported_format(config->offload_info.format) &&
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05306767 !audio_extn_passthru_is_supported_format(config->offload_info.format)) {
vivek mehta0ea887a2015-08-26 14:01:20 -07006768 ALOGE("%s: Unsupported audio format %x " , __func__, config->offload_info.format);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006769 ret = -EINVAL;
6770 goto error_open;
6771 }
6772
Ben Romberger0f8c87b2017-05-24 17:41:11 -07006773 /* TrueHD only supported for 48k multiples (48k, 96k, 192k) */
6774 if ((config->offload_info.format == AUDIO_FORMAT_DOLBY_TRUEHD) &&
6775 (audio_extn_passthru_is_passthrough_stream(out)) &&
6776 !((config->sample_rate == 48000) ||
6777 (config->sample_rate == 96000) ||
6778 (config->sample_rate == 192000))) {
6779 ALOGE("%s: Unsupported sample rate %d for audio format %x",
6780 __func__, config->sample_rate, config->offload_info.format);
6781 ret = -EINVAL;
6782 goto error_open;
6783 }
6784
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006785 out->compr_config.codec = (struct snd_codec *)
6786 calloc(1, sizeof(struct snd_codec));
6787
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07006788 if (!out->compr_config.codec) {
6789 ret = -ENOMEM;
6790 goto error_open;
6791 }
6792
Dhananjay Kumarac341582017-02-23 23:42:25 +05306793 out->stream.pause = out_pause;
6794 out->stream.resume = out_resume;
6795 out->stream.flush = out_flush;
Ashish Jain4847e9d2017-08-17 19:16:57 +05306796 out->stream.set_callback = out_set_callback;
Dhananjay Kumarac341582017-02-23 23:42:25 +05306797 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Mingming Yin21d60472015-09-30 13:56:25 -07006798 out->stream.drain = out_drain;
Dhananjay Kumarac341582017-02-23 23:42:25 +05306799 out->usecase = get_offload_usecase(adev, true /* is_compress */);
vivek mehta446c3962015-09-14 10:57:35 -07006800 ALOGV("Compress Offload usecase .. usecase selected %d", out->usecase);
Dhananjay Kumarac341582017-02-23 23:42:25 +05306801 } else {
6802 out->usecase = get_offload_usecase(adev, false /* is_compress */);
6803 ALOGV("non-offload DIRECT_usecase ... usecase selected %d ", out->usecase);
vivek mehta0ea887a2015-08-26 14:01:20 -07006804 }
vivek mehta446c3962015-09-14 10:57:35 -07006805
6806 if (out->usecase == USECASE_INVALID) {
Mingming Yin3a941d42016-02-17 18:08:05 -08006807 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL &&
6808 config->format == 0 && config->sample_rate == 0 &&
6809 config->channel_mask == 0) {
Mingming Yin21854652016-04-13 11:54:02 -07006810 ALOGI("%s dummy open to query sink capability",__func__);
Mingming Yin3a941d42016-02-17 18:08:05 -08006811 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
6812 } else {
6813 ALOGE("%s, Max allowed OFFLOAD usecase reached ... ", __func__);
6814 ret = -EEXIST;
6815 goto error_open;
6816 }
vivek mehta446c3962015-09-14 10:57:35 -07006817 }
6818
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006819 if (config->offload_info.channel_mask)
6820 out->channel_mask = config->offload_info.channel_mask;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08006821 else if (config->channel_mask) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006822 out->channel_mask = config->channel_mask;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08006823 config->offload_info.channel_mask = config->channel_mask;
Haynes Mathew Georgea99f7532016-08-24 16:01:21 -07006824 } else {
Dhananjay Kumarac341582017-02-23 23:42:25 +05306825 ALOGE("out->channel_mask not set for OFFLOAD/DIRECT usecase");
Haynes Mathew Georgea99f7532016-08-24 16:01:21 -07006826 ret = -EINVAL;
6827 goto error_open;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08006828 }
Haynes Mathew Georgea99f7532016-08-24 16:01:21 -07006829
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07006830 format = out->format = config->offload_info.format;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006831 out->sample_rate = config->offload_info.sample_rate;
6832
Mingming Yin3ee55c62014-08-04 14:23:35 -07006833 out->bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006834
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05306835 out->compr_config.codec->id = get_snd_codec_id(config->offload_info.format);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05306836 if (audio_extn_utils_is_dolby_format(config->offload_info.format)) {
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05306837 audio_extn_dolby_send_ddp_endp_params(adev);
6838 audio_extn_dolby_set_dmid(adev);
6839 }
vivek mehta0ea887a2015-08-26 14:01:20 -07006840
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006841 out->compr_config.codec->sample_rate =
Ravi Kumar Alamandab91bff32014-11-14 12:05:54 -08006842 config->offload_info.sample_rate;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006843 out->compr_config.codec->bit_rate =
6844 config->offload_info.bit_rate;
6845 out->compr_config.codec->ch_in =
Dhanalakshmi Siddania15c6792016-08-10 15:33:53 +05306846 audio_channel_count_from_out_mask(out->channel_mask);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006847 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
Satish Babu Patakokilaa395a9e2016-11-01 12:18:49 +05306848 /* Update bit width only for non passthrough usecases.
6849 * For passthrough usecases, the output will always be opened @16 bit
6850 */
6851 if (!audio_extn_passthru_is_passthrough_stream(out))
6852 out->bit_width = AUDIO_OUTPUT_BIT_WIDTH;
Naresh Tanniruee3499a2017-01-05 14:05:35 +05306853
6854 if (out->flags & AUDIO_OUTPUT_FLAG_TIMESTAMP)
6855 out->compr_config.codec->flags |= COMPRESSED_TIMESTAMP_FLAG;
6856 ALOGVV("%s : out->compr_config.codec->flags -> (%#x) ", __func__, out->compr_config.codec->flags);
6857
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07006858 /*TODO: Do we need to change it for passthrough */
6859 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_RAW;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006860
Manish Dewangana6fc5442015-08-24 20:30:31 +05306861 if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC)
6862 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_RAW;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05306863 else if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS)
Manish Dewangana6fc5442015-08-24 20:30:31 +05306864 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_MP4ADTS;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05306865 else if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_LATM)
6866 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_MP4LATM;
Ashish Jainf1eaa582016-05-23 20:54:24 +05306867
6868 if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) ==
6869 AUDIO_FORMAT_PCM) {
6870
6871 /*Based on platform support, configure appropriate alsa format for corresponding
6872 *hal input format.
6873 */
6874 out->compr_config.codec->format = hal_format_to_alsa(
6875 config->offload_info.format);
6876
Ashish Jain83a6cc22016-06-28 14:34:17 +05306877 out->hal_op_format = alsa_format_to_hal(
Ashish Jainf1eaa582016-05-23 20:54:24 +05306878 out->compr_config.codec->format);
Ashish Jain83a6cc22016-06-28 14:34:17 +05306879 out->hal_ip_format = out->format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05306880
Dhananjay Kumarac341582017-02-23 23:42:25 +05306881 /*for direct non-compress playback populate bit_width based on selected alsa format as
Ashish Jainf1eaa582016-05-23 20:54:24 +05306882 *hal input format and alsa format might differ based on platform support.
6883 */
6884 out->bit_width = audio_bytes_per_sample(
Ashish Jain83a6cc22016-06-28 14:34:17 +05306885 out->hal_op_format) << 3;
Ashish Jainf1eaa582016-05-23 20:54:24 +05306886
6887 out->compr_config.fragments = DIRECT_PCM_NUM_FRAGMENTS;
6888
6889 /* Check if alsa session is configured with the same format as HAL input format,
6890 * if not then derive correct fragment size needed to accomodate the
6891 * conversion of HAL input format to alsa format.
6892 */
6893 audio_extn_utils_update_direct_pcm_fragment_size(out);
6894
6895 /*if hal input and output fragment size is different this indicates HAL input format is
6896 *not same as the alsa format
6897 */
Ashish Jain83a6cc22016-06-28 14:34:17 +05306898 if (out->hal_fragment_size != out->compr_config.fragment_size) {
Ashish Jainf1eaa582016-05-23 20:54:24 +05306899 /*Allocate a buffer to convert input data to the alsa configured format.
6900 *size of convert buffer is equal to the size required to hold one fragment size
6901 *worth of pcm data, this is because flinger does not write more than fragment_size
6902 */
Ashish Jain83a6cc22016-06-28 14:34:17 +05306903 out->convert_buffer = calloc(1,out->compr_config.fragment_size);
6904 if (out->convert_buffer == NULL){
Ashish Jainf1eaa582016-05-23 20:54:24 +05306905 ALOGE("Allocation failed for convert buffer for size %d", out->compr_config.fragment_size);
6906 ret = -ENOMEM;
6907 goto error_open;
6908 }
6909 }
6910 } else if (audio_extn_passthru_is_passthrough_stream(out)) {
6911 out->compr_config.fragment_size =
6912 audio_extn_passthru_get_buffer_size(&config->offload_info);
6913 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
6914 } else {
6915 out->compr_config.fragment_size =
6916 platform_get_compress_offload_buffer_size(&config->offload_info);
6917 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
6918 }
Mingming Yin3ee55c62014-08-04 14:23:35 -07006919
Naresh Tanniruee3499a2017-01-05 14:05:35 +05306920 if (out->flags & AUDIO_OUTPUT_FLAG_TIMESTAMP) {
6921 out->compr_config.fragment_size += sizeof(struct snd_codec_metadata);
6922 }
Amit Shekhar6f461b12014-08-01 14:52:58 -07006923 if (config->offload_info.format == AUDIO_FORMAT_FLAC)
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +05306924 out->compr_config.codec->options.flac_dec.sample_size = AUDIO_OUTPUT_BIT_WIDTH;
Mingming Yin3ee55c62014-08-04 14:23:35 -07006925
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05306926 if (config->offload_info.format == AUDIO_FORMAT_APTX) {
6927 audio_extn_send_aptx_dec_bt_addr_to_dsp(out);
6928 }
6929
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006930 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
6931 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07006932
Manish Dewangan69426c82017-01-30 17:35:36 +05306933 if ((flags & AUDIO_OUTPUT_FLAG_TIMESTAMP) &&
6934 (flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC)) {
6935 out->render_mode = RENDER_MODE_AUDIO_STC_MASTER;
6936 } else if(flags & AUDIO_OUTPUT_FLAG_TIMESTAMP) {
6937 out->render_mode = RENDER_MODE_AUDIO_MASTER;
6938 } else {
6939 out->render_mode = RENDER_MODE_AUDIO_NO_TIMESTAMP;
6940 }
Alexy Josephaa54c872014-12-03 02:46:47 -08006941
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05306942 memset(&out->channel_map_param, 0,
6943 sizeof(struct audio_out_channel_map_param));
6944
Haynes Mathew George352f27b2013-07-26 00:00:15 -07006945 out->send_new_metadata = 1;
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05306946 out->send_next_track_params = false;
6947 out->is_compr_metadata_avail = false;
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08006948 out->offload_state = OFFLOAD_STATE_IDLE;
6949 out->playback_started = 0;
Zhou Song48453a02018-01-10 17:50:59 +08006950 out->writeAt.tv_sec = 0;
6951 out->writeAt.tv_nsec = 0;
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08006952
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08006953 audio_extn_dts_create_state_notifier_node(out->usecase);
6954
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006955 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
6956 __func__, config->offload_info.version,
6957 config->offload_info.bit_rate);
Ashish Jain5106d362016-05-11 19:23:33 +05306958
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05306959 /* Check if DSD audio format is supported in codec
6960 * and there is no active native DSD use case
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05306961 */
6962
6963 if ((config->format == AUDIO_FORMAT_DSD) &&
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05306964 (!platform_check_codec_dsd_support(adev->platform) ||
6965 audio_is_dsd_native_stream_active(adev))) {
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05306966 ret = -EINVAL;
6967 goto error_open;
6968 }
6969
Ashish Jain5106d362016-05-11 19:23:33 +05306970 /* Disable gapless if any of the following is true
6971 * passthrough playback
6972 * AV playback
Dhananjay Kumarac341582017-02-23 23:42:25 +05306973 * non compressed Direct playback
Ashish Jain5106d362016-05-11 19:23:33 +05306974 */
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05306975 if (audio_extn_passthru_is_passthrough_stream(out) ||
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05306976 (config->format == AUDIO_FORMAT_DSD) ||
Naresh Tanniru928f0862017-04-07 16:44:23 -07006977 (config->format == AUDIO_FORMAT_IEC61937) ||
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05306978 config->offload_info.has_video ||
Dhananjay Kumarac341582017-02-23 23:42:25 +05306979 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Ashish Jain5106d362016-05-11 19:23:33 +05306980 check_and_set_gapless_mode(adev, false);
6981 } else
6982 check_and_set_gapless_mode(adev, true);
Mingming Yin21854652016-04-13 11:54:02 -07006983
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05306984 if (audio_extn_passthru_is_passthrough_stream(out)) {
Mingming Yin21854652016-04-13 11:54:02 -07006985 out->flags |= AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH;
6986 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05306987 if (config->format == AUDIO_FORMAT_DSD) {
6988 out->flags |= AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH;
6989 out->compr_config.codec->compr_passthr = PASSTHROUGH_DSD;
6990 }
Aalique Grahame0359a1f2016-09-08 16:54:22 -07006991
6992 create_offload_callback_thread(out);
6993
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07006994 } else if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006995 switch (config->sample_rate) {
6996 case 0:
6997 out->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
6998 break;
6999 case 8000:
7000 case 16000:
7001 case 48000:
7002 out->sample_rate = config->sample_rate;
7003 break;
7004 default:
7005 ALOGE("%s: Unsupported sampling rate %d for Incall Music", __func__,
7006 config->sample_rate);
7007 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
7008 ret = -EINVAL;
7009 goto error_open;
7010 }
7011 //FIXME: add support for MONO stream configuration when audioflinger mixer supports it
7012 switch (config->channel_mask) {
7013 case AUDIO_CHANNEL_NONE:
7014 case AUDIO_CHANNEL_OUT_STEREO:
7015 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
7016 break;
7017 default:
7018 ALOGE("%s: Unsupported channel mask %#x for Incall Music", __func__,
7019 config->channel_mask);
7020 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
7021 ret = -EINVAL;
7022 goto error_open;
7023 }
7024 switch (config->format) {
7025 case AUDIO_FORMAT_DEFAULT:
7026 case AUDIO_FORMAT_PCM_16_BIT:
7027 out->format = AUDIO_FORMAT_PCM_16_BIT;
7028 break;
7029 default:
7030 ALOGE("%s: Unsupported format %#x for Incall Music", __func__,
7031 config->format);
7032 config->format = AUDIO_FORMAT_PCM_16_BIT;
7033 ret = -EINVAL;
7034 goto error_open;
7035 }
7036
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05307037 ret = voice_extn_check_and_set_incall_music_usecase(adev, out);
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07007038 if (ret != 0) {
7039 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
Arun Mirpuri7da752a2018-09-11 18:01:15 -07007040 __func__, ret);
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07007041 goto error_open;
7042 }
Arun Mirpuri7da752a2018-09-11 18:01:15 -07007043 } else if (out->devices == AUDIO_DEVICE_OUT_TELEPHONY_TX) {
Aalique Grahame22e49102018-12-18 14:23:57 -08007044 switch (config->sample_rate) {
7045 case 0:
7046 out->sample_rate = AFE_PROXY_SAMPLING_RATE;
7047 break;
7048 case 8000:
7049 case 16000:
7050 case 48000:
7051 out->sample_rate = config->sample_rate;
7052 break;
7053 default:
7054 ALOGE("%s: Unsupported sampling rate %d for Telephony TX", __func__,
7055 config->sample_rate);
7056 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
7057 ret = -EINVAL;
7058 break;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07007059 }
Aalique Grahame22e49102018-12-18 14:23:57 -08007060 //FIXME: add support for MONO stream configuration when audioflinger mixer supports it
7061 switch (config->channel_mask) {
7062 case AUDIO_CHANNEL_NONE:
7063 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
7064 break;
7065 case AUDIO_CHANNEL_OUT_STEREO:
7066 out->channel_mask = config->channel_mask;
7067 break;
7068 default:
7069 ALOGE("%s: Unsupported channel mask %#x for Telephony TX", __func__,
7070 config->channel_mask);
7071 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
7072 ret = -EINVAL;
7073 break;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07007074 }
Aalique Grahame22e49102018-12-18 14:23:57 -08007075 switch (config->format) {
7076 case AUDIO_FORMAT_DEFAULT:
7077 out->format = AUDIO_FORMAT_PCM_16_BIT;
7078 break;
7079 case AUDIO_FORMAT_PCM_16_BIT:
7080 out->format = config->format;
7081 break;
7082 default:
7083 ALOGE("%s: Unsupported format %#x for Telephony TX", __func__,
7084 config->format);
7085 config->format = AUDIO_FORMAT_PCM_16_BIT;
7086 ret = -EINVAL;
7087 break;
7088 }
7089 if (ret != 0)
7090 goto error_open;
7091
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07007092 out->usecase = USECASE_AUDIO_PLAYBACK_AFE_PROXY;
7093 out->config = pcm_config_afe_proxy_playback;
Aalique Grahame22e49102018-12-18 14:23:57 -08007094 out->config.rate = out->sample_rate;
7095 out->config.channels =
7096 audio_channel_count_from_out_mask(out->channel_mask);
7097 out->config.format = pcm_format_from_audio_format(out->format);
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07007098 adev->voice_tx_output = out;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07007099 } else {
Ashish Jain058165c2016-09-28 23:18:48 +05307100 unsigned int channels = 0;
7101 /*Update config params to default if not set by the caller*/
7102 if (config->sample_rate == 0)
7103 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
7104 if (config->channel_mask == AUDIO_CHANNEL_NONE)
7105 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
7106 if (config->format == AUDIO_FORMAT_DEFAULT)
7107 config->format = AUDIO_FORMAT_PCM_16_BIT;
7108
7109 channels = audio_channel_count_from_out_mask(out->channel_mask);
7110
Varun Balaraje49253e2017-07-06 19:48:56 +05307111 if (out->flags & AUDIO_OUTPUT_FLAG_INTERACTIVE) {
7112 out->usecase = get_interactive_usecase(adev);
7113 out->config = pcm_config_low_latency;
7114 } else if (out->flags & AUDIO_OUTPUT_FLAG_RAW) {
Ashish Jain83a6cc22016-06-28 14:34:17 +05307115 out->usecase = USECASE_AUDIO_PLAYBACK_ULL;
Haynes Mathew George5beddd42016-06-27 18:33:40 -07007116 out->realtime = may_use_noirq_mode(adev, USECASE_AUDIO_PLAYBACK_ULL,
7117 out->flags);
7118 out->config = out->realtime ? pcm_config_rt : pcm_config_low_latency;
Haynes Mathew George16081042017-05-31 17:16:49 -07007119 } else if (out->flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) {
7120 out->usecase = USECASE_AUDIO_PLAYBACK_MMAP;
7121 out->config = pcm_config_mmap_playback;
7122 out->stream.start = out_start;
7123 out->stream.stop = out_stop;
7124 out->stream.create_mmap_buffer = out_create_mmap_buffer;
7125 out->stream.get_mmap_position = out_get_mmap_position;
Ashish Jain83a6cc22016-06-28 14:34:17 +05307126 } else if (out->flags & AUDIO_OUTPUT_FLAG_FAST) {
7127 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
Aniket Kumar Lata932f4872017-11-06 18:29:44 -08007128 out->hal_output_suspend_supported =
7129 property_get_bool("vendor.audio.hal.output.suspend.supported", false);
7130 out->dynamic_pm_qos_config_supported =
7131 property_get_bool("vendor.audio.hal.dynamic.qos.config.supported", false);
7132 if (!out->dynamic_pm_qos_config_supported) {
Alexy Joseph98988832017-01-13 14:56:59 -08007133 ALOGI("%s: dynamic qos voting not enabled for platform", __func__);
7134 } else {
7135 ALOGI("%s: dynamic qos voting enabled for platform", __func__);
7136 //the mixer path will be a string similar to "low-latency-playback resume"
7137 strlcpy(out->pm_qos_mixer_path, use_case_table[out->usecase], MAX_MIXER_PATH_LEN);
7138 strlcat(out->pm_qos_mixer_path,
7139 " resume", MAX_MIXER_PATH_LEN);
7140 ALOGI("%s: created %s pm_qos_mixer_path" , __func__,
7141 out->pm_qos_mixer_path);
7142 }
Ashish Jain83a6cc22016-06-28 14:34:17 +05307143 out->config = pcm_config_low_latency;
7144 } else if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
7145 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
7146 out->config = pcm_config_deep_buffer;
Ashish Jain058165c2016-09-28 23:18:48 +05307147 out->config.period_size = get_output_period_size(config->sample_rate, out->format,
7148 channels, DEEP_BUFFER_OUTPUT_PERIOD_DURATION);
7149 if (out->config.period_size <= 0) {
7150 ALOGE("Invalid configuration period size is not valid");
7151 ret = -EINVAL;
7152 goto error_open;
7153 }
Aalique Grahame22e49102018-12-18 14:23:57 -08007154 } else if (flags & AUDIO_OUTPUT_FLAG_TTS) {
7155 out->usecase = USECASE_AUDIO_PLAYBACK_TTS;
7156 out->config = pcm_config_deep_buffer;
Vignesh Kulothungana6927272019-02-20 15:17:07 -08007157 } else if (config->channel_mask & AUDIO_CHANNEL_HAPTIC_ALL) {
7158 out->usecase = USECASE_AUDIO_PLAYBACK_WITH_HAPTICS;
7159 out->config = pcm_config_haptics_audio;
7160 if (force_haptic_path)
7161 adev->haptics_config = pcm_config_haptics_audio;
7162 else
7163 adev->haptics_config = pcm_config_haptics;
7164
7165 out->config.channels =
7166 audio_channel_count_from_out_mask(out->channel_mask & ~AUDIO_CHANNEL_HAPTIC_ALL);
7167
7168 if (force_haptic_path) {
7169 out->config.channels = 1;
7170 adev->haptics_config.channels = 1;
7171 } else
7172 adev->haptics_config.channels = audio_channel_count_from_out_mask(out->channel_mask & AUDIO_CHANNEL_HAPTIC_ALL);
Ashish Jain83a6cc22016-06-28 14:34:17 +05307173 } else {
7174 /* primary path is the default path selected if no other outputs are available/suitable */
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08007175 out->usecase = GET_USECASE_AUDIO_PLAYBACK_PRIMARY(use_db_as_primary);
7176 out->config = GET_PCM_CONFIG_AUDIO_PLAYBACK_PRIMARY(use_db_as_primary);
Ashish Jain83a6cc22016-06-28 14:34:17 +05307177 }
7178 out->hal_ip_format = format = out->format;
7179 out->config.format = hal_format_to_pcm(out->hal_ip_format);
7180 out->hal_op_format = pcm_format_to_hal(out->config.format);
7181 out->bit_width = format_to_bitwidth_table[out->hal_op_format] << 3;
7182 out->config.rate = config->sample_rate;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07007183 out->sample_rate = out->config.rate;
Ashish Jain058165c2016-09-28 23:18:48 +05307184 out->config.channels = channels;
Ashish Jain83a6cc22016-06-28 14:34:17 +05307185 if (out->hal_ip_format != out->hal_op_format) {
7186 uint32_t buffer_size = out->config.period_size *
7187 format_to_bitwidth_table[out->hal_op_format] *
7188 out->config.channels;
7189 out->convert_buffer = calloc(1, buffer_size);
7190 if (out->convert_buffer == NULL){
7191 ALOGE("Allocation failed for convert buffer for size %d",
7192 out->compr_config.fragment_size);
7193 ret = -ENOMEM;
7194 goto error_open;
7195 }
7196 ALOGD("Convert buffer allocated of size %d", buffer_size);
7197 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007198 }
7199
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08007200 ALOGV("%s devices:%d, format:%x, out->sample_rate:%d,out->bit_width:%d out->format:%d out->flags:%x, flags: %x usecase %d",
7201 __func__, devices, format, out->sample_rate, out->bit_width, out->format, out->flags, flags, out->usecase);
Ashish Jain83a6cc22016-06-28 14:34:17 +05307202
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07007203 /* TODO remove this hardcoding and check why width is zero*/
7204 if (out->bit_width == 0)
7205 out->bit_width = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05307206 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07007207 &adev->streams_output_cfg_list,
Aalique Grahame65780b52017-09-27 14:59:56 -07007208 devices, out->flags, out->hal_op_format, out->sample_rate,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +05307209 out->bit_width, out->channel_mask, out->profile,
Manish Dewangan837dc462015-05-27 10:17:41 +05307210 &out->app_type_cfg);
Aalique Grahame6e763712019-01-31 16:18:17 -08007211 if ((out->usecase == (audio_usecase_t)(GET_USECASE_AUDIO_PLAYBACK_PRIMARY(use_db_as_primary))) ||
Haynes Mathew Georgebf143712013-12-03 13:02:53 -08007212 (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
7213 /* Ensure the default output is not selected twice */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08007214 if(adev->primary_output == NULL)
7215 adev->primary_output = out;
7216 else {
7217 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07007218 ret = -EEXIST;
7219 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08007220 }
7221 }
7222
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007223 /* Check if this usecase is already existing */
7224 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella7ce05352014-04-17 20:00:41 -07007225 if ((get_usecase_from_list(adev, out->usecase) != NULL) &&
7226 (out->usecase != USECASE_COMPRESS_VOIP_CALL)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007227 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007228 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07007229 ret = -EEXIST;
7230 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007231 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08007232
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007233 pthread_mutex_unlock(&adev->lock);
7234
7235 out->stream.common.get_sample_rate = out_get_sample_rate;
7236 out->stream.common.set_sample_rate = out_set_sample_rate;
7237 out->stream.common.get_buffer_size = out_get_buffer_size;
7238 out->stream.common.get_channels = out_get_channels;
7239 out->stream.common.get_format = out_get_format;
7240 out->stream.common.set_format = out_set_format;
7241 out->stream.common.standby = out_standby;
7242 out->stream.common.dump = out_dump;
7243 out->stream.common.set_parameters = out_set_parameters;
7244 out->stream.common.get_parameters = out_get_parameters;
7245 out->stream.common.add_audio_effect = out_add_audio_effect;
7246 out->stream.common.remove_audio_effect = out_remove_audio_effect;
7247 out->stream.get_latency = out_get_latency;
7248 out->stream.set_volume = out_set_volume;
Aalique Grahame22e49102018-12-18 14:23:57 -08007249#ifdef NO_AUDIO_OUT
7250 out->stream.write = out_write_for_no_output;
7251#else
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007252 out->stream.write = out_write;
Aalique Grahame22e49102018-12-18 14:23:57 -08007253#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007254 out->stream.get_render_position = out_get_render_position;
7255 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07007256 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007257
Haynes Mathew George16081042017-05-31 17:16:49 -07007258 if (out->realtime)
7259 out->af_period_multiplier = af_period_multiplier;
7260 else
7261 out->af_period_multiplier = 1;
7262
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007263 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07007264 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07007265 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007266
7267 config->format = out->stream.common.get_format(&out->stream.common);
7268 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
7269 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
Naresh Tanniru04f71882018-06-26 17:46:22 +05307270 register_format(out->format, out->supported_formats);
7271 register_channel_mask(out->channel_mask, out->supported_channel_masks);
7272 register_sample_rate(out->sample_rate, out->supported_sample_rates);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007273
Aalique Grahame22e49102018-12-18 14:23:57 -08007274 out->error_log = error_log_create(
7275 ERROR_LOG_ENTRIES,
7276 1000000000 /* aggregate consecutive identical errors within one second in ns */);
7277
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05307278 /*
7279 By locking output stream before registering, we allow the callback
7280 to update stream's state only after stream's initial state is set to
7281 adev state.
7282 */
7283 lock_output_stream(out);
7284 audio_extn_snd_mon_register_listener(out, out_snd_mon_cb);
7285 pthread_mutex_lock(&adev->lock);
7286 out->card_status = adev->card_status;
7287 pthread_mutex_unlock(&adev->lock);
7288 pthread_mutex_unlock(&out->lock);
7289
Aalique Grahame22e49102018-12-18 14:23:57 -08007290 stream_app_type_cfg_init(&out->app_type_cfg);
7291
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007292 *stream_out = &out->stream;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05307293 ALOGD("%s: Stream (%p) picks up usecase (%s)", __func__, &out->stream,
vivek mehta0ea887a2015-08-26 14:01:20 -07007294 use_case_table[out->usecase]);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08007295
7296 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
7297 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
7298 popcount(out->channel_mask), out->playback_started);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08007299 /* setup a channel for client <--> adsp communication for stream events */
Manish Dewangan21a850a2017-08-14 12:03:55 +05307300 is_direct_passthough = audio_extn_passthru_is_direct_passthrough(out);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08007301 if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
Naresh Tanniru85819452017-05-04 18:55:45 -07007302 (out->flags & AUDIO_OUTPUT_FLAG_DIRECT_PCM) ||
Manish Dewangan21a850a2017-08-14 12:03:55 +05307303 (audio_extn_ip_hdlr_intf_supported(config->format, is_direct_passthough, false))) {
Ben Rombergerd771a7c2017-02-22 18:05:17 -08007304 hdlr_stream_cfg.pcm_device_id = platform_get_pcm_device_id(
7305 out->usecase, PCM_PLAYBACK);
7306 hdlr_stream_cfg.flags = out->flags;
7307 hdlr_stream_cfg.type = PCM_PLAYBACK;
7308 ret = audio_extn_adsp_hdlr_stream_open(&out->adsp_hdlr_stream_handle,
7309 &hdlr_stream_cfg);
7310 if (ret) {
7311 ALOGE("%s: adsp_hdlr_stream_open failed %d",__func__, ret);
7312 out->adsp_hdlr_stream_handle = NULL;
7313 }
7314 }
Manish Dewangan21a850a2017-08-14 12:03:55 +05307315 if (audio_extn_ip_hdlr_intf_supported(config->format, is_direct_passthough, false)) {
Vidyakumar Athota2062f912017-06-27 14:46:15 -07007316 ret = audio_extn_ip_hdlr_intf_init(&out->ip_hdlr_handle, NULL, NULL, adev, out->usecase);
Naresh Tanniru85819452017-05-04 18:55:45 -07007317 if (ret < 0) {
7318 ALOGE("%s: audio_extn_ip_hdlr_intf_init failed %d",__func__, ret);
7319 out->ip_hdlr_handle = NULL;
7320 }
7321 }
Derek Chenf939fb72018-11-13 13:34:41 -08007322
7323 streams_output_ctxt_t *out_ctxt = (streams_output_ctxt_t *)
7324 calloc(1, sizeof(streams_output_ctxt_t));
7325 if (out_ctxt == NULL) {
7326 ALOGE("%s fail to allocate output ctxt", __func__);
7327 ret = -ENOMEM;
7328 goto error_open;
7329 }
7330 out_ctxt->output = out;
7331
7332 pthread_mutex_lock(&adev->lock);
7333 list_add_tail(&adev->active_outputs_list, &out_ctxt->list);
7334 pthread_mutex_unlock(&adev->lock);
7335
Eric Laurent994a6932013-07-17 11:51:42 -07007336 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007337 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07007338
7339error_open:
Ashish Jain83a6cc22016-06-28 14:34:17 +05307340 if (out->convert_buffer)
7341 free(out->convert_buffer);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07007342 free(out);
7343 *stream_out = NULL;
7344 ALOGD("%s: exit: ret %d", __func__, ret);
7345 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007346}
7347
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05307348void adev_close_output_stream(struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007349 struct audio_stream_out *stream)
7350{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07007351 struct stream_out *out = (struct stream_out *)stream;
7352 struct audio_device *adev = out->dev;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007353 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07007354
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007355 ALOGD("%s: enter:stream_handle(%s)",__func__, use_case_table[out->usecase]);
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05307356
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05307357 // must deregister from sndmonitor first to prevent races
7358 // between the callback and close_stream
7359 audio_extn_snd_mon_unregister_listener(out);
7360
Ben Rombergerd771a7c2017-02-22 18:05:17 -08007361 /* close adsp hdrl session before standby */
7362 if (out->adsp_hdlr_stream_handle) {
7363 ret = audio_extn_adsp_hdlr_stream_close(out->adsp_hdlr_stream_handle);
7364 if (ret)
7365 ALOGE("%s: adsp_hdlr_stream_close failed %d",__func__, ret);
7366 out->adsp_hdlr_stream_handle = NULL;
7367 }
7368
Manish Dewangan21a850a2017-08-14 12:03:55 +05307369 if (out->ip_hdlr_handle) {
Naresh Tanniru85819452017-05-04 18:55:45 -07007370 audio_extn_ip_hdlr_intf_deinit(out->ip_hdlr_handle);
7371 out->ip_hdlr_handle = NULL;
7372 }
7373
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007374 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05307375 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007376 ret = voice_extn_compress_voip_close_output_stream(&stream->common);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05307377 out->started = 0;
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05307378 pthread_mutex_unlock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007379 if(ret != 0)
7380 ALOGE("%s: Compress voip output cannot be closed, error:%d",
7381 __func__, ret);
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07007382 } else
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007383 out_standby(&stream->common);
7384
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07007385 if (is_offload_usecase(out->usecase)) {
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08007386 audio_extn_dts_remove_state_notifier_node(out->usecase);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07007387 destroy_offload_callback_thread(out);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07007388 free_offload_usecase(adev, out->usecase);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07007389 if (out->compr_config.codec != NULL)
7390 free(out->compr_config.codec);
7391 }
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07007392
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05307393 out->a2dp_compress_mute = false;
7394
Varun Balaraje49253e2017-07-06 19:48:56 +05307395 if (is_interactive_usecase(out->usecase))
7396 free_interactive_usecase(adev, out->usecase);
7397
Ashish Jain83a6cc22016-06-28 14:34:17 +05307398 if (out->convert_buffer != NULL) {
7399 free(out->convert_buffer);
7400 out->convert_buffer = NULL;
7401 }
7402
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07007403 if (adev->voice_tx_output == out)
7404 adev->voice_tx_output = NULL;
7405
Aalique Grahame22e49102018-12-18 14:23:57 -08007406 error_log_destroy(out->error_log);
7407 out->error_log = NULL;
7408
Dhanalakshmi Siddani6c3d0992017-01-16 16:52:33 +05307409 if (adev->primary_output == out)
7410 adev->primary_output = NULL;
7411
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07007412 pthread_cond_destroy(&out->cond);
7413 pthread_mutex_destroy(&out->lock);
Derek Chenf939fb72018-11-13 13:34:41 -08007414
7415 pthread_mutex_lock(&adev->lock);
7416 streams_output_ctxt_t *out_ctxt = out_get_stream(adev, out->handle);
7417 if (out_ctxt != NULL) {
7418 list_remove(&out_ctxt->list);
7419 free(out_ctxt);
7420 } else {
7421 ALOGW("%s, output stream already closed", __func__);
7422 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007423 free(stream);
Derek Chenf939fb72018-11-13 13:34:41 -08007424 pthread_mutex_unlock(&adev->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07007425 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007426}
7427
7428static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
7429{
7430 struct audio_device *adev = (struct audio_device *)dev;
7431 struct str_parms *parms;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007432 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07007433 int val;
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07007434 int ret;
7435 int status = 0;
Aalique Grahame22e49102018-12-18 14:23:57 -08007436 bool a2dp_reconfig = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007437
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08007438 ALOGD("%s: enter: %s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007439 parms = str_parms_create_str(kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007440
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05307441 if (!parms)
7442 goto error;
Naresh Tanniru4c630392014-05-12 01:05:52 +05307443
Ashish Jain1b9b30c2017-05-18 20:57:40 +05307444 ret = str_parms_get_str(parms, "BT_SCO", value, sizeof(value));
7445 if (ret >= 0) {
7446 /* When set to false, HAL should disable EC and NS */
7447 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
7448 adev->bt_sco_on = true;
7449 else
7450 adev->bt_sco_on = false;
7451 }
7452
Naresh Tanniru4c630392014-05-12 01:05:52 +05307453 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07007454 status = voice_set_parameters(adev, parms);
7455 if (status != 0)
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08007456 goto done;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007457
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07007458 status = platform_set_parameters(adev->platform, parms);
7459 if (status != 0)
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08007460 goto done;
7461
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07007462 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
7463 if (ret >= 0) {
Vicky Sehrawate240e5d2014-08-12 17:17:04 -07007464 /* When set to false, HAL should disable EC and NS */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007465 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
7466 adev->bluetooth_nrec = true;
7467 else
7468 adev->bluetooth_nrec = false;
7469 }
7470
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07007471 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
7472 if (ret >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007473 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
7474 adev->screen_off = false;
7475 else
7476 adev->screen_off = true;
7477 }
7478
Aalique Grahame22e49102018-12-18 14:23:57 -08007479#ifndef MAXXAUDIO_QDSP_ENABLED
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07007480 ret = str_parms_get_int(parms, "rotation", &val);
7481 if (ret >= 0) {
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07007482 bool reverse_speakers = false;
7483 switch(val) {
7484 // FIXME: note that the code below assumes that the speakers are in the correct placement
7485 // relative to the user when the device is rotated 90deg from its default rotation. This
7486 // assumption is device-specific, not platform-specific like this code.
7487 case 270:
7488 reverse_speakers = true;
7489 break;
7490 case 0:
7491 case 90:
7492 case 180:
7493 break;
7494 default:
7495 ALOGE("%s: unexpected rotation of %d", __func__, val);
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07007496 status = -EINVAL;
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07007497 }
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07007498 if (status == 0) {
Vignesh Kulothungan3b5fae52017-09-25 12:16:30 -07007499 // check and set swap
7500 // - check if orientation changed and speaker active
7501 // - set rotation and cache the rotation value
7502 platform_check_and_set_swap_lr_channels(adev, reverse_speakers);
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07007503 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07007504 }
Aalique Grahame22e49102018-12-18 14:23:57 -08007505#endif
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07007506
Mingming Yin514a8bc2014-07-29 15:22:21 -07007507 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_SCO_WB, value, sizeof(value));
7508 if (ret >= 0) {
7509 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
7510 adev->bt_wb_speech_enabled = true;
7511 else
7512 adev->bt_wb_speech_enabled = false;
7513 }
7514
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -07007515 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value, sizeof(value));
7516 if (ret >= 0) {
7517 val = atoi(value);
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05307518 audio_devices_t device = (audio_devices_t) val;
Zhou Song681350a2017-10-19 16:28:42 +08007519 if (audio_is_output_device(val) &&
7520 (val & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07007521 ALOGV("cache new ext disp type and edid");
7522 ret = platform_get_ext_disp_type(adev->platform);
7523 if (ret < 0) {
7524 ALOGE("%s: Failed to query disp type, ret:%d", __func__, ret);
Manisha Agarwal2f5ff882018-08-08 17:09:29 +05307525 } else {
7526 platform_cache_edid(adev->platform);
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07007527 }
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05307528 } else if (audio_is_usb_out_device(device) || audio_is_usb_in_device(device)) {
vivek mehta344576a2016-04-12 18:56:03 -07007529 /*
7530 * Do not allow AFE proxy port usage by WFD source when USB headset is connected.
7531 * Per AudioPolicyManager, USB device is higher priority than WFD.
7532 * For Voice call over USB headset, voice call audio is routed to AFE proxy ports.
7533 * If WFD use case occupies AFE proxy, it may result unintended behavior while
7534 * starting voice call on USB
7535 */
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08007536 ret = str_parms_get_str(parms, "card", value, sizeof(value));
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05307537 if (ret >= 0)
7538 audio_extn_usb_add_device(device, atoi(value));
7539
Zhou Song6f862822017-11-06 17:27:57 +08007540 if (!audio_extn_usb_is_tunnel_supported()) {
7541 ALOGV("detected USB connect .. disable proxy");
7542 adev->allow_afe_proxy_usage = false;
7543 }
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -07007544 }
7545 }
7546
7547 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value, sizeof(value));
7548 if (ret >= 0) {
7549 val = atoi(value);
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05307550 audio_devices_t device = (audio_devices_t) val;
Garmond Leunge3b6d482016-10-25 16:48:01 -07007551 /*
7552 * The HDMI / Displayport disconnect handling has been moved to
7553 * audio extension to ensure that its parameters are not
7554 * invalidated prior to updating sysfs of the disconnect event
7555 * Invalidate will be handled by audio_extn_ext_disp_set_parameters()
7556 */
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05307557 if (audio_is_usb_out_device(device) || audio_is_usb_in_device(device)) {
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08007558 ret = str_parms_get_str(parms, "card", value, sizeof(value));
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05307559 if (ret >= 0)
7560 audio_extn_usb_remove_device(device, atoi(value));
7561
Zhou Song6f862822017-11-06 17:27:57 +08007562 if (!audio_extn_usb_is_tunnel_supported()) {
7563 ALOGV("detected USB disconnect .. enable proxy");
7564 adev->allow_afe_proxy_usage = true;
7565 }
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -07007566 }
7567 }
7568
Aalique Grahame22e49102018-12-18 14:23:57 -08007569 audio_extn_hfp_set_parameters(adev, parms);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08007570 audio_extn_qdsp_set_parameters(adev, parms);
Aalique Grahame22e49102018-12-18 14:23:57 -08007571
7572 status = audio_extn_a2dp_set_parameters(parms, &a2dp_reconfig);
Aniket Kumar Lata23300322019-02-20 22:25:30 -08007573 if (status >= 0 && a2dp_reconfig) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05307574 struct audio_usecase *usecase;
7575 struct listnode *node;
7576 list_for_each(node, &adev->usecase_list) {
7577 usecase = node_to_item(node, struct audio_usecase, list);
7578 if ((usecase->type == PCM_PLAYBACK) &&
Naresh Tanniruf7e9e632016-11-04 14:54:20 -07007579 (usecase->devices & AUDIO_DEVICE_OUT_ALL_A2DP)){
Naresh Tanniru9d027a62015-03-13 01:32:10 +05307580 ALOGD("reconfigure a2dp... forcing device switch");
Weiyin Jiang425180d2017-06-05 16:40:23 +08007581
7582 pthread_mutex_unlock(&adev->lock);
Naresh Tannirucd2353e2016-08-19 00:37:25 +05307583 lock_output_stream(usecase->stream.out);
Weiyin Jiang425180d2017-06-05 16:40:23 +08007584 pthread_mutex_lock(&adev->lock);
Naresh Tannirucd2353e2016-08-19 00:37:25 +05307585 audio_extn_a2dp_set_handoff_mode(true);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05307586 //force device switch to re configure encoder
7587 select_devices(adev, usecase->id);
Naresh Tannirucd2353e2016-08-19 00:37:25 +05307588 audio_extn_a2dp_set_handoff_mode(false);
7589 pthread_mutex_unlock(&usecase->stream.out->lock);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05307590 break;
7591 }
7592 }
7593 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08007594
7595 //handle vr audio setparam
7596 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_VR_AUDIO_MODE,
7597 value, sizeof(value));
7598 if (ret >= 0) {
7599 ALOGI("Setting vr mode to be %s", value);
7600 if (!strncmp(value, "true", 4)) {
7601 adev->vr_audio_mode_enabled = true;
7602 ALOGI("Setting vr mode to true");
7603 } else if (!strncmp(value, "false", 5)) {
7604 adev->vr_audio_mode_enabled = false;
7605 ALOGI("Setting vr mode to false");
7606 } else {
7607 ALOGI("wrong vr mode set");
7608 }
7609 }
7610
Naresh Tannirucd2353e2016-08-19 00:37:25 +05307611 audio_extn_set_parameters(adev, parms);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08007612done:
7613 str_parms_destroy(parms);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08007614 pthread_mutex_unlock(&adev->lock);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05307615error:
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07007616 ALOGV("%s: exit with code(%d)", __func__, status);
7617 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007618}
7619
7620static char* adev_get_parameters(const struct audio_hw_device *dev,
7621 const char *keys)
7622{
Sidipotu Ashokaa4fa6a2018-12-21 09:19:26 +05307623 ALOGD("%s:%s", __func__, keys);
7624
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07007625 struct audio_device *adev = (struct audio_device *)dev;
7626 struct str_parms *reply = str_parms_create();
7627 struct str_parms *query = str_parms_create_str(keys);
7628 char *str;
Naresh Tannirud7205b62014-06-20 02:54:48 +05307629 char value[256] = {0};
7630 int ret = 0;
7631
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07007632 if (!query || !reply) {
Alexy Josephaee4fdd2016-01-29 13:02:07 -08007633 if (reply) {
7634 str_parms_destroy(reply);
7635 }
7636 if (query) {
7637 str_parms_destroy(query);
7638 }
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07007639 ALOGE("adev_get_parameters: failed to create query or reply");
7640 return NULL;
7641 }
7642
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08007643 //handle vr audio getparam
7644
7645 ret = str_parms_get_str(query,
7646 AUDIO_PARAMETER_KEY_VR_AUDIO_MODE,
7647 value, sizeof(value));
7648
7649 if (ret >= 0) {
7650 bool vr_audio_enabled = false;
7651 pthread_mutex_lock(&adev->lock);
7652 vr_audio_enabled = adev->vr_audio_mode_enabled;
7653 pthread_mutex_unlock(&adev->lock);
7654
7655 ALOGI("getting vr mode to %d", vr_audio_enabled);
7656
7657 if (vr_audio_enabled) {
7658 str_parms_add_str(reply, AUDIO_PARAMETER_KEY_VR_AUDIO_MODE,
7659 "true");
7660 goto exit;
7661 } else {
7662 str_parms_add_str(reply, AUDIO_PARAMETER_KEY_VR_AUDIO_MODE,
7663 "false");
7664 goto exit;
7665 }
7666 }
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07007667
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08007668 pthread_mutex_lock(&adev->lock);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07007669 audio_extn_get_parameters(adev, query, reply);
Shiv Maliyappanahallif9308492013-12-12 12:18:09 -08007670 voice_get_parameters(adev, query, reply);
Aalique Grahame22e49102018-12-18 14:23:57 -08007671 audio_extn_a2dp_get_parameters(query, reply);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07007672 platform_get_parameters(adev->platform, query, reply);
Naresh Tanniru80659832014-06-04 18:17:56 +05307673 pthread_mutex_unlock(&adev->lock);
7674
Naresh Tannirud7205b62014-06-20 02:54:48 +05307675exit:
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07007676 str = str_parms_to_str(reply);
7677 str_parms_destroy(query);
7678 str_parms_destroy(reply);
7679
Sidipotu Ashokaa4fa6a2018-12-21 09:19:26 +05307680 ALOGD("%s: exit: returns - %s", __func__, str);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07007681 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007682}
7683
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07007684static int adev_init_check(const struct audio_hw_device *dev __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007685{
7686 return 0;
7687}
7688
7689static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
7690{
Haynes Mathew George5191a852013-09-11 14:19:36 -07007691 int ret;
7692 struct audio_device *adev = (struct audio_device *)dev;
Aalique Grahame22e49102018-12-18 14:23:57 -08007693
7694 audio_extn_extspk_set_voice_vol(adev->extspk, volume);
7695
Haynes Mathew George5191a852013-09-11 14:19:36 -07007696 pthread_mutex_lock(&adev->lock);
7697 /* cache volume */
Shruthi Krishnaace10852013-10-25 14:32:12 -07007698 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07007699 pthread_mutex_unlock(&adev->lock);
7700 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007701}
7702
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07007703static int adev_set_master_volume(struct audio_hw_device *dev __unused,
7704 float volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007705{
7706 return -ENOSYS;
7707}
7708
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07007709static int adev_get_master_volume(struct audio_hw_device *dev __unused,
7710 float *volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007711{
7712 return -ENOSYS;
7713}
7714
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07007715static int adev_set_master_mute(struct audio_hw_device *dev __unused,
7716 bool muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007717{
7718 return -ENOSYS;
7719}
7720
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07007721static int adev_get_master_mute(struct audio_hw_device *dev __unused,
7722 bool *muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007723{
7724 return -ENOSYS;
7725}
7726
7727static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
7728{
7729 struct audio_device *adev = (struct audio_device *)dev;
Garmond Leung5fd0b552018-04-17 11:56:12 -07007730 struct listnode *node;
7731 struct audio_usecase *usecase = NULL;
7732 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007733 pthread_mutex_lock(&adev->lock);
7734 if (adev->mode != mode) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07007735 ALOGD("%s: mode %d\n", __func__, mode);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007736 adev->mode = mode;
Shiv Maliyappanahallibb4cf0b2016-01-21 11:30:06 -08007737 if ((mode == AUDIO_MODE_NORMAL) && voice_is_in_call(adev)) {
Garmond Leung5fd0b552018-04-17 11:56:12 -07007738 list_for_each(node, &adev->usecase_list) {
7739 usecase = node_to_item(node, struct audio_usecase, list);
7740 if (usecase->type == VOICE_CALL)
7741 break;
7742 }
7743 if (usecase &&
7744 audio_is_usb_out_device(usecase->out_snd_device & AUDIO_DEVICE_OUT_ALL_USB)) {
7745 ret = audio_extn_usb_check_and_set_svc_int(usecase,
7746 true);
7747 if (ret != 0) {
7748 /* default service interval was successfully updated,
7749 reopen USB backend with new service interval */
7750 check_usecases_codec_backend(adev,
7751 usecase,
7752 usecase->out_snd_device);
7753 }
7754 }
7755
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07007756 voice_stop_call(adev);
Banajit Goswami20cdd212015-09-11 01:11:30 -07007757 platform_set_gsm_mode(adev->platform, false);
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07007758 adev->current_call_output = NULL;
7759 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007760 }
7761 pthread_mutex_unlock(&adev->lock);
7762 return 0;
7763}
7764
7765static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
7766{
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08007767 int ret;
Aalique Grahame22e49102018-12-18 14:23:57 -08007768 struct audio_device *adev = (struct audio_device *)dev;
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08007769
7770 pthread_mutex_lock(&adev->lock);
Vidyakumar Athota2850d532013-11-19 16:02:12 -08007771 ALOGD("%s state %d\n", __func__, state);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08007772 ret = voice_set_mic_mute((struct audio_device *)dev, state);
Aalique Grahame22e49102018-12-18 14:23:57 -08007773
Derek Chend2530072014-11-24 12:39:14 -08007774 if (adev->ext_hw_plugin)
7775 ret = audio_extn_ext_hw_plugin_set_mic_mute(adev->ext_hw_plugin, state);
Aalique Grahame22e49102018-12-18 14:23:57 -08007776
7777 adev->mic_muted = state;
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08007778 pthread_mutex_unlock(&adev->lock);
7779
7780 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007781}
7782
7783static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
7784{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07007785 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007786 return 0;
7787}
7788
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07007789static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007790 const struct audio_config *config)
7791{
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07007792 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007793
Aalique Grahame22e49102018-12-18 14:23:57 -08007794 /* Don't know if USB HIFI in this context so use true to be conservative */
7795 if (check_input_parameters(config->sample_rate, config->format, channel_count,
7796 true /*is_usb_hifi */) != 0)
7797 return 0;
7798
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07007799 return get_input_buffer_size(config->sample_rate, config->format, channel_count,
7800 false /* is_low_latency: since we don't know, be conservative */);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007801}
7802
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007803static bool adev_input_allow_hifi_record(struct audio_device *adev,
7804 audio_devices_t devices,
7805 audio_input_flags_t flags,
7806 audio_source_t source) {
7807 const bool allowed = true;
7808
7809 if (!audio_is_usb_in_device(devices))
7810 return !allowed;
7811
7812 switch (flags) {
7813 case AUDIO_INPUT_FLAG_NONE:
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007814 break;
Haynes Mathew George59862182017-10-24 16:23:57 -07007815 case AUDIO_INPUT_FLAG_FAST: // disallow hifi record for FAST as
7816 // it affects RTD numbers over USB
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007817 default:
7818 return !allowed;
7819 }
7820
7821 switch (source) {
7822 case AUDIO_SOURCE_DEFAULT:
7823 case AUDIO_SOURCE_MIC:
7824 case AUDIO_SOURCE_UNPROCESSED:
7825 break;
7826 default:
7827 return !allowed;
7828 }
7829
7830 switch (adev->mode) {
7831 case 0:
7832 break;
7833 default:
7834 return !allowed;
7835 }
7836
7837 return allowed;
7838}
7839
Haynes Mathew George4ffef292017-11-21 15:08:02 -08007840static int adev_update_voice_comm_input_stream(struct stream_in *in,
7841 struct audio_config *config)
7842{
7843 bool valid_rate = (config->sample_rate == 8000 ||
7844 config->sample_rate == 16000 ||
7845 config->sample_rate == 32000 ||
7846 config->sample_rate == 48000);
7847 bool valid_ch = audio_channel_count_from_in_mask(in->channel_mask) == 1;
7848
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08007849 if(!voice_extn_is_compress_voip_supported()) {
kunleiz28c73e72019-03-27 17:24:04 +08007850 if (valid_rate && valid_ch) {
Haynes Mathew George4ffef292017-11-21 15:08:02 -08007851 in->usecase = USECASE_AUDIO_RECORD_VOIP;
7852 in->config = default_pcm_config_voip_copp;
7853 in->config.period_size = VOIP_IO_BUF_SIZE(in->sample_rate,
7854 DEFAULT_VOIP_BUF_DURATION_MS,
7855 DEFAULT_VOIP_BIT_DEPTH_BYTE)/2;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08007856 } else {
7857 ALOGW("%s No valid input in voip, use defaults"
7858 "sample rate %u, channel mask 0x%X",
7859 __func__, config->sample_rate, in->channel_mask);
7860 }
Haynes Mathew George4ffef292017-11-21 15:08:02 -08007861 in->config.rate = config->sample_rate;
7862 in->sample_rate = config->sample_rate;
7863 } else {
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08007864 //XXX needed for voice_extn_compress_voip_open_input_stream
7865 in->config.rate = config->sample_rate;
7866 if ((in->dev->mode == AUDIO_MODE_IN_COMMUNICATION ||
7867 voice_extn_compress_voip_is_active(in->dev)) &&
7868 (voice_extn_compress_voip_is_format_supported(in->format)) &&
7869 valid_rate && valid_ch) {
7870 voice_extn_compress_voip_open_input_stream(in);
7871 // update rate entries to match config from AF
7872 in->config.rate = config->sample_rate;
7873 in->sample_rate = config->sample_rate;
7874 } else {
7875 ALOGW("%s compress voip not active, use defaults", __func__);
7876 }
Haynes Mathew George4ffef292017-11-21 15:08:02 -08007877 }
Haynes Mathew George4ffef292017-11-21 15:08:02 -08007878 return 0;
7879}
7880
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007881static int adev_open_input_stream(struct audio_hw_device *dev,
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07007882 audio_io_handle_t handle,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007883 audio_devices_t devices,
7884 struct audio_config *config,
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07007885 struct audio_stream_in **stream_in,
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307886 audio_input_flags_t flags,
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07007887 const char *address __unused,
Vidyakumar Athota5c398212015-03-31 21:53:21 -07007888 audio_source_t source)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007889{
7890 struct audio_device *adev = (struct audio_device *)dev;
7891 struct stream_in *in;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08007892 int ret = 0, buffer_size, frame_size;
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07007893 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07007894 bool is_low_latency = false;
Divya Narayanan Poojary45f19192016-09-30 18:52:13 +05307895 bool channel_mask_updated = false;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007896 bool is_usb_dev = audio_is_usb_in_device(devices);
7897 bool may_use_hifi_record = adev_input_allow_hifi_record(adev,
7898 devices,
7899 flags,
7900 source);
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05307901
kunleizdff872d2018-08-20 14:40:33 +08007902 if (is_usb_dev && (!audio_extn_usb_connected(NULL))) {
kunleizd6a9e0c2018-07-30 15:38:52 +08007903 is_usb_dev = false;
7904 devices = AUDIO_DEVICE_IN_BUILTIN_MIC;
7905 ALOGW("%s: ignore set device to non existing USB card, use input device(%#x)",
7906 __func__, devices);
7907 }
7908
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007909 *stream_in = NULL;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007910
7911 if (!(is_usb_dev && may_use_hifi_record)) {
7912 if (config->sample_rate == 0)
7913 config->sample_rate = 48000;
7914 if (config->channel_mask == AUDIO_CHANNEL_NONE)
7915 config->channel_mask = AUDIO_CHANNEL_IN_MONO;
7916 if (config->format == AUDIO_FORMAT_DEFAULT)
7917 config->format = AUDIO_FORMAT_PCM_16_BIT;
7918
7919 channel_count = audio_channel_count_from_in_mask(config->channel_mask);
7920
Aalique Grahame22e49102018-12-18 14:23:57 -08007921 if (check_input_parameters(config->sample_rate, config->format, channel_count,
7922 false) != 0)
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007923 return -EINVAL;
Chaithanya Krishna Bacharaju9955b162016-05-25 16:25:53 +05307924 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007925
7926 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07007927
7928 if (!in) {
7929 ALOGE("failed to allocate input stream");
7930 return -ENOMEM;
7931 }
7932
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05307933 ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x)\
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05307934 stream_handle(%p) io_handle(%d) source(%d) format %x",__func__, config->sample_rate,
7935 config->channel_mask, devices, &in->stream, handle, source, config->format);
Ravi Kumar Alamanda40703102014-04-24 10:34:41 -07007936 pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07007937 pthread_mutex_init(&in->pre_lock, (const pthread_mutexattr_t *) NULL);
Ravi Kumar Alamanda40703102014-04-24 10:34:41 -07007938
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007939 in->stream.common.get_sample_rate = in_get_sample_rate;
7940 in->stream.common.set_sample_rate = in_set_sample_rate;
7941 in->stream.common.get_buffer_size = in_get_buffer_size;
7942 in->stream.common.get_channels = in_get_channels;
7943 in->stream.common.get_format = in_get_format;
7944 in->stream.common.set_format = in_set_format;
7945 in->stream.common.standby = in_standby;
7946 in->stream.common.dump = in_dump;
7947 in->stream.common.set_parameters = in_set_parameters;
7948 in->stream.common.get_parameters = in_get_parameters;
7949 in->stream.common.add_audio_effect = in_add_audio_effect;
7950 in->stream.common.remove_audio_effect = in_remove_audio_effect;
7951 in->stream.set_gain = in_set_gain;
7952 in->stream.read = in_read;
7953 in->stream.get_input_frames_lost = in_get_input_frames_lost;
Aalique Grahame22e49102018-12-18 14:23:57 -08007954 in->stream.get_capture_position = in_get_capture_position;
Naresh Tannirudcb47c52018-06-25 16:23:32 +05307955 in->stream.get_active_microphones = in_get_active_microphones;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007956
7957 in->device = devices;
Vidyakumar Athota5c398212015-03-31 21:53:21 -07007958 in->source = source;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007959 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007960 in->standby = 1;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07007961 in->capture_handle = handle;
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07007962 in->flags = flags;
Haynes Mathew George46740472017-10-27 18:40:12 -07007963 in->bit_width = 16;
7964 in->af_period_multiplier = 1;
7965
Aalique Grahame22e49102018-12-18 14:23:57 -08007966 ALOGV("%s: source = %d, config->channel_mask = %d", __func__, source, config->channel_mask);
7967 if (source == AUDIO_SOURCE_VOICE_UPLINK ||
7968 source == AUDIO_SOURCE_VOICE_DOWNLINK) {
7969 /* Force channel config requested to mono if incall
7970 record is being requested for only uplink/downlink */
7971 if (config->channel_mask != AUDIO_CHANNEL_IN_MONO) {
7972 config->channel_mask = AUDIO_CHANNEL_IN_MONO;
7973 ret = -EINVAL;
7974 goto err_open;
7975 }
7976 }
7977
Haynes Mathew George46740472017-10-27 18:40:12 -07007978 /* Update config params with the requested sample rate and channels */
7979 if ((in->device == AUDIO_DEVICE_IN_TELEPHONY_RX) &&
7980 (adev->mode != AUDIO_MODE_IN_CALL)) {
7981 ret = -EINVAL;
7982 goto err_open;
7983 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007984
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007985 if (is_usb_dev && may_use_hifi_record) {
7986 /* HiFi record selects an appropriate format, channel, rate combo
7987 depending on sink capabilities*/
7988 ret = read_usb_sup_params_and_compare(false /*is_playback*/,
7989 &config->format,
7990 &in->supported_formats[0],
7991 MAX_SUPPORTED_FORMATS,
7992 &config->channel_mask,
7993 &in->supported_channel_masks[0],
7994 MAX_SUPPORTED_CHANNEL_MASKS,
7995 &config->sample_rate,
7996 &in->supported_sample_rates[0],
7997 MAX_SUPPORTED_SAMPLE_RATES);
7998 if (ret != 0) {
7999 ret = -EINVAL;
8000 goto err_open;
8001 }
8002 channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Haynes Mathew George4ffef292017-11-21 15:08:02 -08008003 } else if (config->format == AUDIO_FORMAT_DEFAULT) {
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05308004 config->format = AUDIO_FORMAT_PCM_16_BIT;
Surendar karkaaca3d082017-11-09 15:18:37 +05308005 } else if (property_get_bool("vendor.audio.capture.pcm.32bit.enable", false)
8006 && config->format == AUDIO_FORMAT_PCM_32_BIT) {
8007 in->config.format = PCM_FORMAT_S32_LE;
8008 in->bit_width = 32;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05308009 } else if ((config->format == AUDIO_FORMAT_PCM_FLOAT) ||
8010 (config->format == AUDIO_FORMAT_PCM_32_BIT) ||
8011 (config->format == AUDIO_FORMAT_PCM_24_BIT_PACKED) ||
8012 (config->format == AUDIO_FORMAT_PCM_8_24_BIT)) {
8013 bool ret_error = false;
8014 in->bit_width = 24;
8015 /* 24 bit is restricted to UNPROCESSED source only,also format supported
8016 from HAL is 24_packed and 8_24
8017 *> In case of UNPROCESSED source, for 24 bit, if format requested is other than
8018 24_packed return error indicating supported format is 24_packed
8019 *> In case of any other source requesting 24 bit or float return error
8020 indicating format supported is 16 bit only.
8021
8022 on error flinger will retry with supported format passed
8023 */
8024 if ((source != AUDIO_SOURCE_UNPROCESSED) &&
8025 (source != AUDIO_SOURCE_CAMCORDER)) {
8026 config->format = AUDIO_FORMAT_PCM_16_BIT;
8027 if (config->sample_rate > 48000)
8028 config->sample_rate = 48000;
8029 ret_error = true;
Haynes Mathew George46740472017-10-27 18:40:12 -07008030 } else if (!(config->format == AUDIO_FORMAT_PCM_24_BIT_PACKED ||
8031 config->format == AUDIO_FORMAT_PCM_8_24_BIT)) {
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05308032 config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
8033 ret_error = true;
8034 }
8035
8036 if (ret_error) {
8037 ret = -EINVAL;
8038 goto err_open;
8039 }
8040 }
8041
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008042 in->channel_mask = config->channel_mask;
8043 in->format = config->format;
8044
8045 in->usecase = USECASE_AUDIO_RECORD;
Samyak Jain0aa07ab2019-04-04 14:36:32 +05308046
8047 if (in->source == AUDIO_SOURCE_FM_TUNER) {
8048 if(!get_usecase_from_list(adev, USECASE_AUDIO_RECORD_FM_VIRTUAL))
8049 in->usecase = USECASE_AUDIO_RECORD_FM_VIRTUAL;
8050 else {
8051 ret = -EINVAL;
8052 goto err_open;
8053 }
8054 }
8055
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008056 if (config->sample_rate == LOW_LATENCY_CAPTURE_SAMPLE_RATE &&
8057 (flags & AUDIO_INPUT_FLAG_FAST) != 0) {
8058 is_low_latency = true;
8059#if LOW_LATENCY_CAPTURE_USE_CASE
8060 in->usecase = USECASE_AUDIO_RECORD_LOW_LATENCY;
8061#endif
8062 in->realtime = may_use_noirq_mode(adev, in->usecase, in->flags);
Aalique Grahame22e49102018-12-18 14:23:57 -08008063 if (!in->realtime) {
8064 in->config = pcm_config_audio_capture;
8065 frame_size = audio_stream_in_frame_size(&in->stream);
8066 buffer_size = get_input_buffer_size(config->sample_rate,
8067 config->format,
8068 channel_count,
8069 is_low_latency);
8070 in->config.period_size = buffer_size / frame_size;
8071 in->config.rate = config->sample_rate;
8072 in->af_period_multiplier = 1;
8073 } else {
8074 // period size is left untouched for rt mode playback
8075 in->config = pcm_config_audio_capture_rt;
8076 in->af_period_multiplier = af_period_multiplier;
8077 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008078 }
8079
8080 if ((config->sample_rate == LOW_LATENCY_CAPTURE_SAMPLE_RATE) &&
8081 ((in->flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0)) {
8082 in->realtime = 0;
8083 in->usecase = USECASE_AUDIO_RECORD_MMAP;
8084 in->config = pcm_config_mmap_capture;
Haynes Mathew George46740472017-10-27 18:40:12 -07008085 in->config.format = pcm_format_from_audio_format(config->format);
Weiyin Jiang7c30c612018-11-27 18:45:53 +08008086 in->config.channels = channel_count;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008087 in->stream.start = in_start;
8088 in->stream.stop = in_stop;
8089 in->stream.create_mmap_buffer = in_create_mmap_buffer;
8090 in->stream.get_mmap_position = in_get_mmap_position;
Haynes Mathew Georgef29116e2018-01-31 12:48:20 -08008091 in->sample_rate = in->config.rate;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008092 ALOGV("%s: USECASE_AUDIO_RECORD_MMAP", __func__);
8093 } else if (in->realtime) {
8094 in->config = pcm_config_audio_capture_rt;
Haynes Mathew George46740472017-10-27 18:40:12 -07008095 in->config.format = pcm_format_from_audio_format(config->format);
Haynes Mathew George4ffef292017-11-21 15:08:02 -08008096 in->config.channels = channel_count;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008097 in->sample_rate = in->config.rate;
8098 in->af_period_multiplier = af_period_multiplier;
Haynes Mathew George46740472017-10-27 18:40:12 -07008099 } else if (is_usb_dev && may_use_hifi_record) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008100 in->usecase = USECASE_AUDIO_RECORD_HIFI;
8101 in->config = pcm_config_audio_capture;
8102 frame_size = audio_stream_in_frame_size(&in->stream);
8103 buffer_size = get_input_buffer_size(config->sample_rate,
8104 config->format,
8105 channel_count,
8106 false /*is_low_latency*/);
8107 in->config.period_size = buffer_size / frame_size;
8108 in->config.rate = config->sample_rate;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008109 in->config.format = pcm_format_from_audio_format(config->format);
Karthikeyan Mani07faa602018-08-20 11:01:32 -07008110 switch (config->format) {
8111 case AUDIO_FORMAT_PCM_32_BIT:
8112 in->bit_width = 32;
8113 break;
8114 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
8115 case AUDIO_FORMAT_PCM_8_24_BIT:
8116 in->bit_width = 24;
8117 break;
8118 default:
8119 in->bit_width = 16;
8120 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008121 in->config.channels = channel_count;
Haynes Mathew Georgef29116e2018-01-31 12:48:20 -08008122 in->sample_rate = in->config.rate;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008123 } else if ((in->device == AUDIO_DEVICE_IN_TELEPHONY_RX) ||
Dhanalakshmi Siddani0b1488e2016-09-06 12:58:42 +05308124 (in->device == AUDIO_DEVICE_IN_PROXY)) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07008125 if (config->sample_rate == 0)
8126 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
8127 if (config->sample_rate != 48000 && config->sample_rate != 16000 &&
8128 config->sample_rate != 8000) {
8129 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
8130 ret = -EINVAL;
8131 goto err_open;
8132 }
8133 if (config->format == AUDIO_FORMAT_DEFAULT)
8134 config->format = AUDIO_FORMAT_PCM_16_BIT;
8135 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
8136 config->format = AUDIO_FORMAT_PCM_16_BIT;
8137 ret = -EINVAL;
8138 goto err_open;
8139 }
8140
8141 in->usecase = USECASE_AUDIO_RECORD_AFE_PROXY;
8142 in->config = pcm_config_afe_proxy_record;
8143 in->config.channels = channel_count;
8144 in->config.rate = config->sample_rate;
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05308145 in->sample_rate = config->sample_rate;
Aalique Grahame22e49102018-12-18 14:23:57 -08008146 in->af_period_multiplier = 1;
8147 } else if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
8148 in->flags & AUDIO_INPUT_FLAG_VOIP_TX &&
8149 (config->sample_rate == 8000 ||
8150 config->sample_rate == 16000 ||
8151 config->sample_rate == 32000 ||
8152 config->sample_rate == 48000) &&
8153 channel_count == 1) {
8154 in->usecase = USECASE_AUDIO_RECORD_VOIP;
8155 in->config = pcm_config_audio_capture;
8156 frame_size = audio_stream_in_frame_size(&in->stream);
8157 buffer_size = get_stream_buffer_size(VOIP_CAPTURE_PERIOD_DURATION_MSEC,
8158 config->sample_rate,
8159 config->format,
8160 channel_count, false /*is_low_latency*/);
8161 in->config.period_size = buffer_size / frame_size;
8162 in->config.period_count = VOIP_CAPTURE_PERIOD_COUNT;
8163 in->config.rate = config->sample_rate;
8164 in->af_period_multiplier = 1;
Haynes Mathew George46740472017-10-27 18:40:12 -07008165 } else {
Revathi Uddarajud2634032017-12-07 14:42:34 +05308166 int ret_val;
8167 pthread_mutex_lock(&adev->lock);
8168 ret_val = audio_extn_check_and_set_multichannel_usecase(adev,
8169 in, config, &channel_mask_updated);
8170 pthread_mutex_unlock(&adev->lock);
8171
8172 if (!ret_val) {
8173 if (channel_mask_updated == true) {
8174 ALOGD("%s: return error to retry with updated channel mask (%#x)",
8175 __func__, config->channel_mask);
8176 ret = -EINVAL;
8177 goto err_open;
8178 }
8179 ALOGD("%s: created multi-channel session succesfully",__func__);
8180 } else if (audio_extn_compr_cap_enabled() &&
8181 audio_extn_compr_cap_format_supported(config->format) &&
8182 (in->dev->mode != AUDIO_MODE_IN_COMMUNICATION)) {
8183 audio_extn_compr_cap_init(in);
8184 } else if (audio_extn_cin_applicable_stream(in)) {
Sachin Mohan Gadag16273f72017-12-19 18:10:48 +05308185 in->sample_rate = config->sample_rate;
Revathi Uddarajud2634032017-12-07 14:42:34 +05308186 ret = audio_extn_cin_configure_input_stream(in);
8187 if (ret)
8188 goto err_open;
8189 } else {
8190 in->config = pcm_config_audio_capture;
8191 in->config.rate = config->sample_rate;
8192 in->config.format = pcm_format_from_audio_format(config->format);
8193 in->config.channels = channel_count;
8194 in->sample_rate = config->sample_rate;
8195 in->format = config->format;
8196 frame_size = audio_stream_in_frame_size(&in->stream);
8197 buffer_size = get_input_buffer_size(config->sample_rate,
Haynes Mathew George46740472017-10-27 18:40:12 -07008198 config->format,
8199 channel_count,
8200 is_low_latency);
Dieter Luecking5d57def2018-09-07 14:23:37 +02008201 /* prevent division-by-zero */
8202 if (frame_size == 0) {
8203 ALOGE("%s: Error frame_size==0", __func__);
8204 ret = -EINVAL;
8205 goto err_open;
8206 }
8207
Revathi Uddarajud2634032017-12-07 14:42:34 +05308208 in->config.period_size = buffer_size / frame_size;
Aalique Grahame22e49102018-12-18 14:23:57 -08008209 in->af_period_multiplier = 1;
Haynes Mathew George4ffef292017-11-21 15:08:02 -08008210
Revathi Uddarajud2634032017-12-07 14:42:34 +05308211 if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
8212 /* optionally use VOIP usecase depending on config(s) */
8213 ret = adev_update_voice_comm_input_stream(in, config);
8214 }
Haynes Mathew George4ffef292017-11-21 15:08:02 -08008215
Revathi Uddarajud2634032017-12-07 14:42:34 +05308216 if (ret) {
8217 ALOGE("%s AUDIO_SOURCE_VOICE_COMMUNICATION invalid args", __func__);
8218 goto err_open;
8219 }
Haynes Mathew George4ffef292017-11-21 15:08:02 -08008220 }
kunleiz28c73e72019-03-27 17:24:04 +08008221#ifdef CONCURRENT_CAPTURE_ENABLED
8222 /* Acquire lock to avoid two concurrent use cases initialized to
8223 same pcm record use case */
8224
8225 pthread_mutex_lock(&adev->lock);
8226 if (in->usecase == USECASE_AUDIO_RECORD) {
8227 if (!(adev->pcm_record_uc_state)) {
8228 ALOGV("%s: using USECASE_AUDIO_RECORD",__func__);
8229 adev->pcm_record_uc_state = 1;
8230 } else {
8231 /* Assign compress record use case for second record */
8232 in->usecase = USECASE_AUDIO_RECORD_COMPRESS2;
8233 in->flags |= AUDIO_INPUT_FLAG_COMPRESS;
8234 ALOGV("%s: overriding usecase with USECASE_AUDIO_RECORD_COMPRESS2 and appending compress flag", __func__);
8235 }
8236 }
8237 pthread_mutex_unlock(&adev->lock);
8238#endif
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07008239 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +05308240 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
8241 &adev->streams_input_cfg_list,
Haynes Mathew George4ffef292017-11-21 15:08:02 -08008242 devices, flags, in->format,
8243 in->sample_rate, in->bit_width,
8244 in->profile, &in->app_type_cfg);
Naresh Tanniru04f71882018-06-26 17:46:22 +05308245 register_format(in->format, in->supported_formats);
8246 register_channel_mask(in->channel_mask, in->supported_channel_masks);
8247 register_sample_rate(in->sample_rate, in->supported_sample_rates);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05308248
Aalique Grahame22e49102018-12-18 14:23:57 -08008249 in->error_log = error_log_create(
8250 ERROR_LOG_ENTRIES,
8251 1000000000 /* aggregate consecutive identical errors within one second */);
8252
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07008253 /* This stream could be for sound trigger lab,
8254 get sound trigger pcm if present */
8255 audio_extn_sound_trigger_check_and_get_session(in);
8256
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05308257 lock_input_stream(in);
8258 audio_extn_snd_mon_register_listener(in, in_snd_mon_cb);
8259 pthread_mutex_lock(&adev->lock);
8260 in->card_status = adev->card_status;
8261 pthread_mutex_unlock(&adev->lock);
8262 pthread_mutex_unlock(&in->lock);
8263
Aalique Grahame22e49102018-12-18 14:23:57 -08008264 stream_app_type_cfg_init(&in->app_type_cfg);
8265
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008266 *stream_in = &in->stream;
Derek Chenf939fb72018-11-13 13:34:41 -08008267
8268 streams_input_ctxt_t *in_ctxt = (streams_input_ctxt_t *)
8269 calloc(1, sizeof(streams_input_ctxt_t));
8270 if (in_ctxt == NULL) {
8271 ALOGE("%s fail to allocate input ctxt", __func__);
8272 ret = -ENOMEM;
8273 goto err_open;
8274 }
8275 in_ctxt->input = in;
8276
8277 pthread_mutex_lock(&adev->lock);
8278 list_add_tail(&adev->active_inputs_list, &in_ctxt->list);
8279 pthread_mutex_unlock(&adev->lock);
8280
Eric Laurent994a6932013-07-17 11:51:42 -07008281 ALOGV("%s: exit", __func__);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08008282 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008283
8284err_open:
8285 free(in);
8286 *stream_in = NULL;
8287 return ret;
8288}
8289
8290static void adev_close_input_stream(struct audio_hw_device *dev,
8291 struct audio_stream_in *stream)
8292{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08008293 int ret;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07008294 struct stream_in *in = (struct stream_in *)stream;
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07008295 struct audio_device *adev = (struct audio_device *)dev;
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05308296
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05308297 ALOGD("%s: enter:stream_handle(%p)",__func__, in);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08008298
kunleiz70e57612018-12-28 17:50:23 +08008299 /* must deregister from sndmonitor first to prevent races
8300 * between the callback and close_stream
8301 */
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05308302 audio_extn_snd_mon_unregister_listener(stream);
8303
kunleiz70e57612018-12-28 17:50:23 +08008304 /* Disable echo reference if there are no active input, hfp call
8305 * and sound trigger while closing input stream
8306 */
8307 if (!adev->active_input &&
8308 !audio_extn_hfp_is_active(adev) &&
8309 !audio_extn_sound_trigger_check_ec_ref_enable())
kunleiz8c93ce62018-11-29 15:04:56 +08008310 platform_set_echo_reference(adev, false, AUDIO_DEVICE_NONE);
kunleiz70e57612018-12-28 17:50:23 +08008311 else
8312 audio_extn_sound_trigger_update_ec_ref_status(false);
Venkata Narendra Kumar Gutta1bbbf542014-09-04 19:11:25 +05308313
Aalique Grahame22e49102018-12-18 14:23:57 -08008314 error_log_destroy(in->error_log);
8315 in->error_log = NULL;
8316
Pallavid7c7a272018-01-16 11:22:55 +05308317 if (in == NULL) {
8318 ALOGE("%s: audio_stream_in ptr is NULL", __func__);
8319 return;
8320 }
8321
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08008322 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05308323 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08008324 ret = voice_extn_compress_voip_close_input_stream(&stream->common);
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05308325 pthread_mutex_unlock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08008326 if (ret != 0)
8327 ALOGE("%s: Compress voip input cannot be closed, error:%d",
8328 __func__, ret);
8329 } else
8330 in_standby(&stream->common);
8331
Revathi Uddarajud2634032017-12-07 14:42:34 +05308332 pthread_mutex_lock(&adev->lock);
Samyak Jain15fda662018-12-18 16:40:52 +05308333 if (in->usecase == USECASE_AUDIO_RECORD) {
8334 adev->pcm_record_uc_state = 0;
8335 }
8336
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -07008337 if (audio_extn_ssr_get_stream() == in) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07008338 audio_extn_ssr_deinit();
8339 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008340
Garmond Leunge2433c32017-09-28 21:51:22 -07008341 if (audio_extn_ffv_get_stream() == in) {
8342 audio_extn_ffv_stream_deinit();
8343 }
8344
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05308345 if (audio_extn_compr_cap_enabled() &&
Mingming Yine62d7842013-10-25 16:26:03 -07008346 audio_extn_compr_cap_format_supported(in->config.format))
8347 audio_extn_compr_cap_deinit();
Dhanalakshmi Siddani74cf00b2016-12-02 13:55:57 +05308348
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05308349 if (audio_extn_cin_attached_usecase(in->usecase))
8350 audio_extn_cin_close_input_stream(in);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07008351
Mingming Yinfd7607b2016-01-22 12:48:44 -08008352 if (in->is_st_session) {
8353 ALOGV("%s: sound trigger pcm stop lab", __func__);
8354 audio_extn_sound_trigger_stop_lab(in);
8355 }
Derek Chenf939fb72018-11-13 13:34:41 -08008356 streams_input_ctxt_t *in_ctxt = in_get_stream(adev, in->capture_handle);
8357 if (in_ctxt != NULL) {
8358 list_remove(&in_ctxt->list);
8359 free(in_ctxt);
8360 } else {
8361 ALOGW("%s, input stream already closed", __func__);
8362 }
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07008363 free(stream);
Revathi Uddarajud2634032017-12-07 14:42:34 +05308364 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008365 return;
8366}
8367
Aalique Grahame22e49102018-12-18 14:23:57 -08008368/* verifies input and output devices and their capabilities.
8369 *
8370 * This verification is required when enabling extended bit-depth or
8371 * sampling rates, as not all qcom products support it.
8372 *
8373 * Suitable for calling only on initialization such as adev_open().
8374 * It fills the audio_device use_case_table[] array.
8375 *
8376 * Has a side-effect that it needs to configure audio routing / devices
8377 * in order to power up the devices and read the device parameters.
8378 * It does not acquire any hw device lock. Should restore the devices
8379 * back to "normal state" upon completion.
8380 */
8381static int adev_verify_devices(struct audio_device *adev)
8382{
8383 /* enumeration is a bit difficult because one really wants to pull
8384 * the use_case, device id, etc from the hidden pcm_device_table[].
8385 * In this case there are the following use cases and device ids.
8386 *
8387 * [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = {0, 0},
8388 * [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {15, 15},
8389 * [USECASE_AUDIO_PLAYBACK_HIFI] = {1, 1},
8390 * [USECASE_AUDIO_PLAYBACK_OFFLOAD] = {9, 9},
8391 * [USECASE_AUDIO_RECORD] = {0, 0},
8392 * [USECASE_AUDIO_RECORD_LOW_LATENCY] = {15, 15},
8393 * [USECASE_VOICE_CALL] = {2, 2},
8394 *
8395 * USECASE_AUDIO_PLAYBACK_OFFLOAD, USECASE_AUDIO_PLAYBACK_HIFI omitted.
8396 * USECASE_VOICE_CALL omitted, but possible for either input or output.
8397 */
8398
8399 /* should be the usecases enabled in adev_open_input_stream() */
8400 static const int test_in_usecases[] = {
8401 USECASE_AUDIO_RECORD,
8402 USECASE_AUDIO_RECORD_LOW_LATENCY, /* does not appear to be used */
8403 };
8404 /* should be the usecases enabled in adev_open_output_stream()*/
8405 static const int test_out_usecases[] = {
8406 USECASE_AUDIO_PLAYBACK_DEEP_BUFFER,
8407 USECASE_AUDIO_PLAYBACK_LOW_LATENCY,
8408 };
8409 static const usecase_type_t usecase_type_by_dir[] = {
8410 PCM_PLAYBACK,
8411 PCM_CAPTURE,
8412 };
8413 static const unsigned flags_by_dir[] = {
8414 PCM_OUT,
8415 PCM_IN,
8416 };
8417
8418 size_t i;
8419 unsigned dir;
8420 const unsigned card_id = adev->snd_card;
8421
8422 for (dir = 0; dir < 2; ++dir) {
8423 const usecase_type_t usecase_type = usecase_type_by_dir[dir];
8424 const unsigned flags_dir = flags_by_dir[dir];
8425 const size_t testsize =
8426 dir ? ARRAY_SIZE(test_in_usecases) : ARRAY_SIZE(test_out_usecases);
8427 const int *testcases =
8428 dir ? test_in_usecases : test_out_usecases;
8429 const audio_devices_t audio_device =
8430 dir ? AUDIO_DEVICE_IN_BUILTIN_MIC : AUDIO_DEVICE_OUT_SPEAKER;
8431
8432 for (i = 0; i < testsize; ++i) {
8433 const audio_usecase_t audio_usecase = testcases[i];
8434 int device_id;
8435 struct pcm_params **pparams;
8436 struct stream_out out;
8437 struct stream_in in;
8438 struct audio_usecase uc_info;
8439 int retval;
8440
8441 pparams = &adev->use_case_table[audio_usecase];
8442 pcm_params_free(*pparams); /* can accept null input */
8443 *pparams = NULL;
8444
8445 /* find the device ID for the use case (signed, for error) */
8446 device_id = platform_get_pcm_device_id(audio_usecase, usecase_type);
8447 if (device_id < 0)
8448 continue;
8449
8450 /* prepare structures for device probing */
8451 memset(&uc_info, 0, sizeof(uc_info));
8452 uc_info.id = audio_usecase;
8453 uc_info.type = usecase_type;
8454 if (dir) {
8455 adev->active_input = &in;
8456 memset(&in, 0, sizeof(in));
8457 in.device = audio_device;
8458 in.source = AUDIO_SOURCE_VOICE_COMMUNICATION;
8459 uc_info.stream.in = &in;
8460 } else {
8461 adev->active_input = NULL;
8462 }
8463 memset(&out, 0, sizeof(out));
8464 out.devices = audio_device; /* only field needed in select_devices */
8465 uc_info.stream.out = &out;
8466 uc_info.devices = audio_device;
8467 uc_info.in_snd_device = SND_DEVICE_NONE;
8468 uc_info.out_snd_device = SND_DEVICE_NONE;
8469 list_add_tail(&adev->usecase_list, &uc_info.list);
8470
8471 /* select device - similar to start_(in/out)put_stream() */
8472 retval = select_devices(adev, audio_usecase);
8473 if (retval >= 0) {
8474 *pparams = pcm_params_get(card_id, device_id, flags_dir);
8475#if LOG_NDEBUG == 0
Aalique Grahame203bee02019-03-13 17:49:36 -07008476 char info[512]; /* for possible debug info */
Aalique Grahame22e49102018-12-18 14:23:57 -08008477 if (*pparams) {
8478 ALOGV("%s: (%s) card %d device %d", __func__,
8479 dir ? "input" : "output", card_id, device_id);
8480 pcm_params_to_string(*pparams, info, ARRAY_SIZE(info));
8481 } else {
8482 ALOGV("%s: cannot locate card %d device %d", __func__, card_id, device_id);
8483 }
8484#endif
8485 }
8486
8487 /* deselect device - similar to stop_(in/out)put_stream() */
8488 /* 1. Get and set stream specific mixer controls */
8489 retval = disable_audio_route(adev, &uc_info);
8490 /* 2. Disable the rx device */
8491 retval = disable_snd_device(adev,
8492 dir ? uc_info.in_snd_device : uc_info.out_snd_device);
8493 list_remove(&uc_info.list);
8494 }
8495 }
8496 adev->active_input = NULL; /* restore adev state */
8497 return 0;
8498}
8499
Siddartha Shaik31b530e2017-05-19 15:26:33 +05308500int adev_create_audio_patch(struct audio_hw_device *dev,
8501 unsigned int num_sources,
8502 const struct audio_port_config *sources,
8503 unsigned int num_sinks,
8504 const struct audio_port_config *sinks,
8505 audio_patch_handle_t *handle)
8506{
Derek Chenf939fb72018-11-13 13:34:41 -08008507 int ret;
Siddartha Shaik31b530e2017-05-19 15:26:33 +05308508
Derek Chenf939fb72018-11-13 13:34:41 -08008509 ret = audio_extn_hw_loopback_create_audio_patch(dev,
8510 num_sources,
8511 sources,
8512 num_sinks,
8513 sinks,
8514 handle);
8515 ret |= audio_extn_auto_hal_create_audio_patch(dev,
8516 num_sources,
8517 sources,
8518 num_sinks,
8519 sinks,
8520 handle);
8521 return ret;
Siddartha Shaik31b530e2017-05-19 15:26:33 +05308522}
8523
8524int adev_release_audio_patch(struct audio_hw_device *dev,
8525 audio_patch_handle_t handle)
8526{
Derek Chenf939fb72018-11-13 13:34:41 -08008527 int ret;
8528
8529 ret = audio_extn_hw_loopback_release_audio_patch(dev, handle);
8530 ret |= audio_extn_auto_hal_release_audio_patch(dev, handle);
8531 return ret;
Siddartha Shaik31b530e2017-05-19 15:26:33 +05308532}
8533
8534int adev_get_audio_port(struct audio_hw_device *dev, struct audio_port *config)
8535{
8536 return audio_extn_hw_loopback_get_audio_port(dev, config);
8537}
8538
8539int adev_set_audio_port_config(struct audio_hw_device *dev,
8540 const struct audio_port_config *config)
8541{
8542 return audio_extn_hw_loopback_set_audio_port_config(dev, config);
8543}
8544
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07008545static int adev_dump(const audio_hw_device_t *device __unused,
8546 int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008547{
8548 return 0;
8549}
8550
8551static int adev_close(hw_device_t *device)
8552{
Aalique Grahame22e49102018-12-18 14:23:57 -08008553 size_t i;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008554 struct audio_device *adev = (struct audio_device *)device;
Kiran Kandi910e1862013-10-29 13:29:42 -07008555
8556 if (!adev)
8557 return 0;
8558
8559 pthread_mutex_lock(&adev_init_lock);
8560
8561 if ((--audio_device_ref_count) == 0) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05308562 audio_extn_snd_mon_unregister_listener(adev);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07008563 audio_extn_sound_trigger_deinit(adev);
Kiran Kandide144c82013-11-20 15:58:32 -08008564 audio_extn_listen_deinit(adev);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008565 audio_extn_qdsp_deinit();
Aalique Grahame22e49102018-12-18 14:23:57 -08008566 audio_extn_extspk_deinit(adev->extspk);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05308567 audio_extn_utils_release_streams_cfg_lists(
8568 &adev->streams_output_cfg_list,
8569 &adev->streams_input_cfg_list);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05308570 if (audio_extn_qaf_is_enabled())
8571 audio_extn_qaf_deinit();
Kiran Kandi910e1862013-10-29 13:29:42 -07008572 audio_route_free(adev->audio_route);
Weiyin Jiangfa65d3e2019-03-05 23:39:45 +08008573 audio_extn_gef_deinit(adev);
Kiran Kandi910e1862013-10-29 13:29:42 -07008574 free(adev->snd_dev_ref_cnt);
8575 platform_deinit(adev->platform);
Aalique Grahame22e49102018-12-18 14:23:57 -08008576 for (i = 0; i < ARRAY_SIZE(adev->use_case_table); ++i) {
8577 pcm_params_free(adev->use_case_table[i]);
8578 }
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07008579 if (adev->adm_deinit)
8580 adev->adm_deinit(adev->adm_data);
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05308581 qahwi_deinit(device);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08008582 audio_extn_adsp_hdlr_deinit();
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05308583 audio_extn_snd_mon_deinit();
Siddartha Shaik44dd7702017-06-14 12:13:25 +05308584 audio_extn_hw_loopback_deinit(adev);
Garmond Leunge2433c32017-09-28 21:51:22 -07008585 audio_extn_ffv_deinit();
Satish Babu Patakokilac3c5d432017-07-04 22:48:59 +05308586 if (adev->device_cfg_params) {
8587 free(adev->device_cfg_params);
8588 adev->device_cfg_params = NULL;
8589 }
Derek Chend2530072014-11-24 12:39:14 -08008590 if(adev->ext_hw_plugin)
8591 audio_extn_ext_hw_plugin_deinit(adev->ext_hw_plugin);
Derek Chenae7b0342019-02-08 15:17:04 -08008592 audio_extn_auto_hal_deinit();
Kiran Kandi910e1862013-10-29 13:29:42 -07008593 free(device);
8594 adev = NULL;
8595 }
8596 pthread_mutex_unlock(&adev_init_lock);
Vatsal Buchac09ae062018-11-14 13:25:08 +05308597 enable_gcov();
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008598 return 0;
8599}
8600
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07008601/* This returns 1 if the input parameter looks at all plausible as a low latency period size,
8602 * or 0 otherwise. A return value of 1 doesn't mean the value is guaranteed to work,
8603 * just that it _might_ work.
8604 */
8605static int period_size_is_plausible_for_low_latency(int period_size)
8606{
8607 switch (period_size) {
8608 case 160:
Ravi Kumar Alamanda474de5a2015-06-25 20:08:01 -07008609 case 192:
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07008610 case 240:
8611 case 320:
8612 case 480:
8613 return 1;
8614 default:
8615 return 0;
8616 }
8617}
8618
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05308619static void adev_snd_mon_cb(void *cookie, struct str_parms *parms)
8620{
8621 bool is_snd_card_status = false;
8622 bool is_ext_device_status = false;
8623 char value[32];
8624 int card = -1;
8625 card_status_t status;
8626
8627 if (cookie != adev || !parms)
8628 return;
8629
8630 if (!parse_snd_card_status(parms, &card, &status)) {
8631 is_snd_card_status = true;
8632 } else if (0 < str_parms_get_str(parms, "ext_audio_device", value, sizeof(value))) {
8633 is_ext_device_status = true;
8634 } else {
8635 // not a valid event
8636 return;
8637 }
8638
8639 pthread_mutex_lock(&adev->lock);
8640 if (card == adev->snd_card || is_ext_device_status) {
8641 if (is_snd_card_status && adev->card_status != status) {
8642 adev->card_status = status;
8643 platform_snd_card_update(adev->platform, status);
8644 audio_extn_fm_set_parameters(adev, parms);
8645 } else if (is_ext_device_status) {
8646 platform_set_parameters(adev->platform, parms);
8647 }
8648 }
8649 pthread_mutex_unlock(&adev->lock);
8650 return;
8651}
8652
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05308653/* out and adev lock held */
8654static int check_a2dp_restore_l(struct audio_device *adev, struct stream_out *out, bool restore)
8655{
8656 struct audio_usecase *uc_info;
8657 float left_p;
8658 float right_p;
8659 audio_devices_t devices;
8660
8661 uc_info = get_usecase_from_list(adev, out->usecase);
8662 if (uc_info == NULL) {
8663 ALOGE("%s: Could not find the usecase (%d) in the list",
8664 __func__, out->usecase);
8665 return -EINVAL;
8666 }
8667
8668 ALOGD("%s: enter: usecase(%d: %s)", __func__,
8669 out->usecase, use_case_table[out->usecase]);
8670
8671 if (restore) {
8672 // restore A2DP device for active usecases and unmute if required
8673 if ((out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) &&
8674 (uc_info->out_snd_device != SND_DEVICE_OUT_BT_A2DP)) {
8675 ALOGD("%s: restoring A2dp and unmuting stream", __func__);
8676 select_devices(adev, uc_info->id);
8677 pthread_mutex_lock(&out->compr_mute_lock);
8678 if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
8679 (out->a2dp_compress_mute)) {
8680 out->a2dp_compress_mute = false;
8681 out_set_compr_volume(&out->stream, out->volume_l, out->volume_r);
8682 }
8683 pthread_mutex_unlock(&out->compr_mute_lock);
8684 }
8685 } else {
8686 // mute compress stream if suspended
8687 pthread_mutex_lock(&out->compr_mute_lock);
8688 if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
8689 (!out->a2dp_compress_mute)) {
8690 if (!out->standby) {
8691 ALOGD("%s: selecting speaker and muting stream", __func__);
8692 devices = out->devices;
8693 out->devices = AUDIO_DEVICE_OUT_SPEAKER;
8694 left_p = out->volume_l;
8695 right_p = out->volume_r;
8696 if (out->offload_state == OFFLOAD_STATE_PLAYING)
8697 compress_pause(out->compr);
8698 out_set_compr_volume(&out->stream, (float)0, (float)0);
8699 out->a2dp_compress_mute = true;
8700 select_devices(adev, out->usecase);
8701 if (out->offload_state == OFFLOAD_STATE_PLAYING)
8702 compress_resume(out->compr);
8703 out->devices = devices;
8704 out->volume_l = left_p;
8705 out->volume_r = right_p;
8706 }
8707 }
8708 pthread_mutex_unlock(&out->compr_mute_lock);
8709 }
8710 ALOGV("%s: exit", __func__);
8711 return 0;
8712}
8713
8714int check_a2dp_restore(struct audio_device *adev, struct stream_out *out, bool restore)
8715{
8716 int ret = 0;
8717
8718 lock_output_stream(out);
8719 pthread_mutex_lock(&adev->lock);
8720
8721 ret = check_a2dp_restore_l(adev, out, restore);
8722
8723 pthread_mutex_unlock(&adev->lock);
8724 pthread_mutex_unlock(&out->lock);
8725 return ret;
8726}
8727
Haynes Mathew George01156f92018-04-13 15:29:54 -07008728void adev_on_battery_status_changed(bool charging)
8729{
8730 pthread_mutex_lock(&adev->lock);
8731 ALOGI("%s: battery status changed to %scharging", __func__, charging ? "" : "not ");
8732 adev->is_charging = charging;
Zhou Songc1088ea2018-06-12 00:17:29 +08008733 audio_extn_sound_trigger_update_battery_status(charging);
Haynes Mathew George01156f92018-04-13 15:29:54 -07008734 pthread_mutex_unlock(&adev->lock);
8735}
8736
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008737static int adev_open(const hw_module_t *module, const char *name,
8738 hw_device_t **device)
8739{
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05308740 int ret;
Derek Chenf939fb72018-11-13 13:34:41 -08008741 char value[PROPERTY_VALUE_MAX] = {0};
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05308742
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08008743 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008744 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
8745
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07008746 pthread_mutex_lock(&adev_init_lock);
Kiran Kandi910e1862013-10-29 13:29:42 -07008747 if (audio_device_ref_count != 0){
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07008748 *device = &adev->device.common;
Kiran Kandi910e1862013-10-29 13:29:42 -07008749 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07008750 ALOGD("%s: returning existing instance of adev", __func__);
8751 ALOGD("%s: exit", __func__);
8752 pthread_mutex_unlock(&adev_init_lock);
8753 return 0;
8754 }
8755
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008756 adev = calloc(1, sizeof(struct audio_device));
8757
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07008758 if (!adev) {
8759 pthread_mutex_unlock(&adev_init_lock);
8760 return -ENOMEM;
8761 }
8762
Ravi Kumar Alamanda40703102014-04-24 10:34:41 -07008763 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
8764
Weiyin Jiange6ce6312019-01-28 18:28:22 +08008765 // register audio ext hidl at the earliest
8766 audio_extn_hidl_init();
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +05308767#ifdef DYNAMIC_LOG_ENABLED
8768 register_for_dynamic_logging("hal");
8769#endif
8770
Derek Chenf939fb72018-11-13 13:34:41 -08008771 /* default audio HAL major version */
8772 uint32_t maj_version = 2;
8773 if(property_get("vendor.audio.hal.maj.version", value, NULL))
8774 maj_version = atoi(value);
8775
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008776 adev->device.common.tag = HARDWARE_DEVICE_TAG;
Derek Chenf939fb72018-11-13 13:34:41 -08008777 adev->device.common.version = HARDWARE_DEVICE_API_VERSION(maj_version, 0);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008778 adev->device.common.module = (struct hw_module_t *)module;
8779 adev->device.common.close = adev_close;
8780
8781 adev->device.init_check = adev_init_check;
8782 adev->device.set_voice_volume = adev_set_voice_volume;
8783 adev->device.set_master_volume = adev_set_master_volume;
8784 adev->device.get_master_volume = adev_get_master_volume;
8785 adev->device.set_master_mute = adev_set_master_mute;
8786 adev->device.get_master_mute = adev_get_master_mute;
8787 adev->device.set_mode = adev_set_mode;
8788 adev->device.set_mic_mute = adev_set_mic_mute;
8789 adev->device.get_mic_mute = adev_get_mic_mute;
8790 adev->device.set_parameters = adev_set_parameters;
8791 adev->device.get_parameters = adev_get_parameters;
8792 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
8793 adev->device.open_output_stream = adev_open_output_stream;
8794 adev->device.close_output_stream = adev_close_output_stream;
8795 adev->device.open_input_stream = adev_open_input_stream;
8796 adev->device.close_input_stream = adev_close_input_stream;
Siddartha Shaik31b530e2017-05-19 15:26:33 +05308797 adev->device.create_audio_patch = adev_create_audio_patch;
8798 adev->device.release_audio_patch = adev_release_audio_patch;
8799 adev->device.get_audio_port = adev_get_audio_port;
8800 adev->device.set_audio_port_config = adev_set_audio_port_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008801 adev->device.dump = adev_dump;
Naresh Tannirudcb47c52018-06-25 16:23:32 +05308802 adev->device.get_microphones = adev_get_microphones;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008803
8804 /* Set the default route before the PCM stream is opened */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008805 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08008806 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08008807 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008808 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008809 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08008810 adev->acdb_settings = TTY_MODE_OFF;
vivek mehta344576a2016-04-12 18:56:03 -07008811 adev->allow_afe_proxy_usage = true;
Ashish Jain1b9b30c2017-05-18 20:57:40 +05308812 adev->bt_sco_on = false;
Eric Laurent07eeafd2013-10-06 12:52:49 -07008813 /* adev->cur_hdmi_channels = 0; by calloc() */
Eric Laurentb23d5282013-05-14 15:27:20 -07008814 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Aniket Kumar Lata808e8d62019-01-28 22:54:28 -08008815 /* Init audio feature manager */
8816 audio_feature_manager_init();
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07008817 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08008818 list_init(&adev->usecase_list);
Derek Chenf939fb72018-11-13 13:34:41 -08008819 list_init(&adev->active_inputs_list);
8820 list_init(&adev->active_outputs_list);
Krishnankutty Kolathappilly0b2de1c2014-02-14 14:45:49 -08008821 adev->cur_wfd_channels = 2;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07008822 adev->offload_usecases_state = 0;
Samyak Jain15fda662018-12-18 16:40:52 +05308823 adev->pcm_record_uc_state = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05308824 adev->is_channel_status_set = false;
Sudheer Papothifa9d2282015-09-17 01:53:25 +05308825 adev->perf_lock_opts[0] = 0x101;
8826 adev->perf_lock_opts[1] = 0x20E;
8827 adev->perf_lock_opts_size = 2;
Xiaojun Sang785b5da2017-08-03 15:52:29 +08008828 adev->dsp_bit_width_enforce_mode = 0;
Aalique Grahame552b0832019-03-11 10:16:38 -07008829 adev->enable_hfp = false;
Naresh Tanniru4c630392014-05-12 01:05:52 +05308830
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008831 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07008832 adev->platform = platform_init(adev);
8833 if (!adev->platform) {
Ben Romberger2684ad72018-06-04 12:41:39 -07008834 pthread_mutex_destroy(&adev->lock);
Eric Laurentb23d5282013-05-14 15:27:20 -07008835 free(adev->snd_dev_ref_cnt);
8836 free(adev);
Ben Romberger2684ad72018-06-04 12:41:39 -07008837 adev = NULL;
Eric Laurentb23d5282013-05-14 15:27:20 -07008838 ALOGE("%s: Failed to init platform data, aborting.", __func__);
8839 *device = NULL;
Apoorv Raghuvanshi6e57d7e2013-12-16 16:02:45 -08008840 pthread_mutex_unlock(&adev_init_lock);
Eric Laurentb23d5282013-05-14 15:27:20 -07008841 return -EINVAL;
8842 }
Eric Laurentc4aef752013-09-12 17:45:53 -07008843
Aalique Grahame22e49102018-12-18 14:23:57 -08008844 adev->extspk = audio_extn_extspk_init(adev);
8845
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05308846 if (audio_extn_qaf_is_enabled()) {
8847 ret = audio_extn_qaf_init(adev);
8848 if (ret < 0) {
Ben Romberger2684ad72018-06-04 12:41:39 -07008849 pthread_mutex_destroy(&adev->lock);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05308850 free(adev);
Ben Romberger2684ad72018-06-04 12:41:39 -07008851 adev = NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05308852 ALOGE("%s: Failed to init platform data, aborting.", __func__);
8853 *device = NULL;
8854 pthread_mutex_unlock(&adev_init_lock);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05308855 return ret;
8856 }
8857
8858 adev->device.open_output_stream = audio_extn_qaf_open_output_stream;
8859 adev->device.close_output_stream = audio_extn_qaf_close_output_stream;
8860 }
8861
Derek Chenae7b0342019-02-08 15:17:04 -08008862 audio_extn_auto_hal_init(adev);
Derek Chend2530072014-11-24 12:39:14 -08008863 adev->ext_hw_plugin = audio_extn_ext_hw_plugin_init(adev);
8864
Eric Laurentc4aef752013-09-12 17:45:53 -07008865 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
8866 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
8867 if (adev->visualizer_lib == NULL) {
8868 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
8869 } else {
8870 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
8871 adev->visualizer_start_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08008872 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07008873 "visualizer_hal_start_output");
8874 adev->visualizer_stop_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08008875 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07008876 "visualizer_hal_stop_output");
8877 }
8878 }
Dhanalakshmi Siddani21be3ac2016-12-29 14:31:08 +05308879 audio_extn_init(adev);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008880 voice_extn_init(adev);
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08008881 audio_extn_listen_init(adev, adev->snd_card);
Weiyin Jiangaa80acd2016-09-21 16:42:11 +08008882 audio_extn_gef_init(adev);
Siddartha Shaik44dd7702017-06-14 12:13:25 +05308883 audio_extn_hw_loopback_init(adev);
Garmond Leunge2433c32017-09-28 21:51:22 -07008884 audio_extn_ffv_init(adev);
Eric Laurentc4aef752013-09-12 17:45:53 -07008885
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08008886 if (access(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, R_OK) == 0) {
8887 adev->offload_effects_lib = dlopen(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, RTLD_NOW);
8888 if (adev->offload_effects_lib == NULL) {
8889 ALOGE("%s: DLOPEN failed for %s", __func__,
8890 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
8891 } else {
8892 ALOGV("%s: DLOPEN successful for %s", __func__,
8893 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
8894 adev->offload_effects_start_output =
Ashish Jain5106d362016-05-11 19:23:33 +05308895 (int (*)(audio_io_handle_t, int, struct mixer *))dlsym(adev->offload_effects_lib,
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08008896 "offload_effects_bundle_hal_start_output");
8897 adev->offload_effects_stop_output =
8898 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
8899 "offload_effects_bundle_hal_stop_output");
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08008900 adev->offload_effects_set_hpx_state =
8901 (int (*)(bool))dlsym(adev->offload_effects_lib,
8902 "offload_effects_bundle_set_hpx_state");
Dhananjay Kumard68883d2015-09-04 13:39:26 +05308903 adev->offload_effects_get_parameters =
8904 (void (*)(struct str_parms *, struct str_parms *))
8905 dlsym(adev->offload_effects_lib,
8906 "offload_effects_bundle_get_parameters");
8907 adev->offload_effects_set_parameters =
8908 (void (*)(struct str_parms *))dlsym(adev->offload_effects_lib,
8909 "offload_effects_bundle_set_parameters");
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08008910 }
8911 }
8912
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07008913 if (access(ADM_LIBRARY_PATH, R_OK) == 0) {
8914 adev->adm_lib = dlopen(ADM_LIBRARY_PATH, RTLD_NOW);
8915 if (adev->adm_lib == NULL) {
8916 ALOGE("%s: DLOPEN failed for %s", __func__, ADM_LIBRARY_PATH);
8917 } else {
8918 ALOGV("%s: DLOPEN successful for %s", __func__, ADM_LIBRARY_PATH);
8919 adev->adm_init = (adm_init_t)
8920 dlsym(adev->adm_lib, "adm_init");
8921 adev->adm_deinit = (adm_deinit_t)
8922 dlsym(adev->adm_lib, "adm_deinit");
8923 adev->adm_register_input_stream = (adm_register_input_stream_t)
8924 dlsym(adev->adm_lib, "adm_register_input_stream");
8925 adev->adm_register_output_stream = (adm_register_output_stream_t)
8926 dlsym(adev->adm_lib, "adm_register_output_stream");
8927 adev->adm_deregister_stream = (adm_deregister_stream_t)
8928 dlsym(adev->adm_lib, "adm_deregister_stream");
8929 adev->adm_request_focus = (adm_request_focus_t)
8930 dlsym(adev->adm_lib, "adm_request_focus");
8931 adev->adm_abandon_focus = (adm_abandon_focus_t)
8932 dlsym(adev->adm_lib, "adm_abandon_focus");
Haynes Mathew George5beddd42016-06-27 18:33:40 -07008933 adev->adm_set_config = (adm_set_config_t)
8934 dlsym(adev->adm_lib, "adm_set_config");
8935 adev->adm_request_focus_v2 = (adm_request_focus_v2_t)
8936 dlsym(adev->adm_lib, "adm_request_focus_v2");
8937 adev->adm_is_noirq_avail = (adm_is_noirq_avail_t)
8938 dlsym(adev->adm_lib, "adm_is_noirq_avail");
8939 adev->adm_on_routing_change = (adm_on_routing_change_t)
8940 dlsym(adev->adm_lib, "adm_on_routing_change");
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07008941 }
8942 }
8943
Aalique Grahame22e49102018-12-18 14:23:57 -08008944 adev->enable_voicerx = false;
Mingming Yin514a8bc2014-07-29 15:22:21 -07008945 adev->bt_wb_speech_enabled = false;
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08008946 //initialize this to false for now,
8947 //this will be set to true through set param
8948 adev->vr_audio_mode_enabled = false;
Mingming Yin514a8bc2014-07-29 15:22:21 -07008949
Pradnya Chaphekar8a9dcd82014-09-09 09:49:10 -07008950 audio_extn_ds2_enable(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008951 *device = &adev->device.common;
Aalique Grahame22e49102018-12-18 14:23:57 -08008952
8953 if (k_enable_extended_precision)
8954 adev_verify_devices(adev);
8955
Xiaojun Sang785b5da2017-08-03 15:52:29 +08008956 adev->dsp_bit_width_enforce_mode =
8957 adev_init_dsp_bit_width_enforce_mode(adev->mixer);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008958
Dhananjay Kumard6d32152016-10-13 16:11:03 +05308959 audio_extn_utils_update_streams_cfg_lists(adev->platform, adev->mixer,
8960 &adev->streams_output_cfg_list,
8961 &adev->streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07008962
Kiran Kandi910e1862013-10-29 13:29:42 -07008963 audio_device_ref_count++;
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07008964
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07008965 int trial;
Aalique Grahame22e49102018-12-18 14:23:57 -08008966 if ((property_get("vendor.audio_hal.period_size", value, NULL) > 0) ||
8967 (property_get("audio_hal.period_size", value, NULL) > 0)) {
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07008968 trial = atoi(value);
8969 if (period_size_is_plausible_for_low_latency(trial)) {
8970 pcm_config_low_latency.period_size = trial;
8971 pcm_config_low_latency.start_threshold = trial / 4;
8972 pcm_config_low_latency.avail_min = trial / 4;
8973 configured_low_latency_capture_period_size = trial;
8974 }
8975 }
Aalique Grahame22e49102018-12-18 14:23:57 -08008976 if ((property_get("vendor.audio_hal.in_period_size", value, NULL) > 0) ||
8977 (property_get("audio_hal.in_period_size", value, NULL) > 0)) {
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07008978 trial = atoi(value);
8979 if (period_size_is_plausible_for_low_latency(trial)) {
8980 configured_low_latency_capture_period_size = trial;
8981 }
8982 }
8983
Vignesh Kulothungan7d374312018-02-21 17:12:00 -08008984 adev->mic_break_enabled = property_get_bool("vendor.audio.mic_break", false);
8985
Aalique Grahame22e49102018-12-18 14:23:57 -08008986 if ((property_get("vendor.audio_hal.period_multiplier",value,NULL) > 0) ||
8987 (property_get("audio_hal.period_multiplier",value,NULL) > 0)) {
Haynes Mathew George5beddd42016-06-27 18:33:40 -07008988 af_period_multiplier = atoi(value);
8989 if (af_period_multiplier < 0)
8990 af_period_multiplier = 2;
8991 else if (af_period_multiplier > 4)
8992 af_period_multiplier = 4;
8993
8994 ALOGV("new period_multiplier = %d", af_period_multiplier);
8995 }
8996
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008997 audio_extn_qdsp_init(adev->platform);
Aalique Grahame22e49102018-12-18 14:23:57 -08008998
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07008999 adev->multi_offload_enable = property_get_bool("vendor.audio.offload.multiple.enabled", false);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07009000 pthread_mutex_unlock(&adev_init_lock);
9001
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07009002 if (adev->adm_init)
9003 adev->adm_data = adev->adm_init();
9004
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05309005 qahwi_init(*device);
Sudheer Papothifa9d2282015-09-17 01:53:25 +05309006 audio_extn_perf_lock_init();
Ben Rombergerd771a7c2017-02-22 18:05:17 -08009007 audio_extn_adsp_hdlr_init(adev->mixer);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05309008
9009 audio_extn_snd_mon_init();
9010 pthread_mutex_lock(&adev->lock);
9011 audio_extn_snd_mon_register_listener(adev, adev_snd_mon_cb);
9012 adev->card_status = CARD_STATUS_ONLINE;
Haynes Mathew George01156f92018-04-13 15:29:54 -07009013 audio_extn_battery_properties_listener_init(adev_on_battery_status_changed);
9014 /*
9015 * if the battery state callback happens before charging can be queried,
9016 * it will be guarded with the adev->lock held in the cb function and so
9017 * the callback value will reflect the latest state
9018 */
9019 adev->is_charging = audio_extn_battery_properties_is_charging();
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05309020 audio_extn_sound_trigger_init(adev); /* dependent on snd_mon_init() */
Zhou Songc1088ea2018-06-12 00:17:29 +08009021 audio_extn_sound_trigger_update_battery_status(adev->is_charging);
9022 pthread_mutex_unlock(&adev->lock);
Satish Babu Patakokilac3c5d432017-07-04 22:48:59 +05309023 /* Allocate memory for Device config params */
9024 adev->device_cfg_params = (struct audio_device_config_param*)
9025 calloc(platform_get_max_codec_backend(),
9026 sizeof(struct audio_device_config_param));
9027 if (adev->device_cfg_params == NULL)
9028 ALOGE("%s: Memory allocation failed for Device config params", __func__);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05309029
Eric Laurent994a6932013-07-17 11:51:42 -07009030 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009031 return 0;
9032}
9033
9034static struct hw_module_methods_t hal_module_methods = {
9035 .open = adev_open,
9036};
9037
9038struct audio_module HAL_MODULE_INFO_SYM = {
9039 .common = {
9040 .tag = HARDWARE_MODULE_TAG,
9041 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
9042 .hal_api_version = HARDWARE_HAL_API_VERSION,
9043 .id = AUDIO_HARDWARE_MODULE_ID,
9044 .name = "QCOM Audio HAL",
Duy Truongfae19622013-11-24 02:17:54 -08009045 .author = "The Linux Foundation",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009046 .methods = &hal_module_methods,
9047 },
9048};