blob: 7b0379e1c39debc1a141be4e571003d1c418e96d [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 ||
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08005140 (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP
5141 && !voice_extn_is_compress_voip_supported()) ||
Aalique Grahame22e49102018-12-18 14:23:57 -08005142 out->usecase == USECASE_INCALL_MUSIC_UPLINK2) {
Arun Mirpuri7da752a2018-09-11 18:01:15 -07005143 size_t channel_count = audio_channel_count_from_out_mask(out->channel_mask);
5144 int16_t *src = (int16_t *)buffer;
5145 int16_t *dst = (int16_t *)buffer;
5146
5147 LOG_ALWAYS_FATAL_IF(out->config.channels != 1 || channel_count != 2 ||
5148 out->format != AUDIO_FORMAT_PCM_16_BIT,
Aalique Grahame22e49102018-12-18 14:23:57 -08005149 "out_write called for %s use case with wrong properties",
5150 use_case_table[out->usecase]);
Arun Mirpuri7da752a2018-09-11 18:01:15 -07005151
5152 /*
5153 * FIXME: this can be removed once audio flinger mixer supports
5154 * mono output
5155 */
5156
5157 /*
5158 * Code below goes over each frame in the buffer and adds both
5159 * L and R samples and then divides by 2 to convert to mono
5160 */
5161 for (size_t i = 0; i < frames ; i++, dst++, src += 2) {
5162 *dst = (int16_t)(((int32_t)src[0] + (int32_t)src[1]) >> 1);
5163 }
5164 bytes_to_write /= 2;
5165 }
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07005166
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05305167 ALOGVV("%s: writing buffer (%zu bytes) to pcm device", __func__, bytes);
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07005168
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005169 long ns = 0;
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07005170
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005171 if (out->config.rate)
5172 ns = pcm_bytes_to_frames(out->pcm, bytes)*1000000000LL/
5173 out->config.rate;
5174
Arun Mirpuri7da752a2018-09-11 18:01:15 -07005175 request_out_focus(out, ns);
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005176 bool use_mmap = is_mmap_usecase(out->usecase) || out->realtime;
5177
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005178 if (use_mmap)
Arun Mirpuri7da752a2018-09-11 18:01:15 -07005179 ret = pcm_mmap_write(out->pcm, (void *)buffer, bytes_to_write);
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005180 else if (out->hal_op_format != out->hal_ip_format &&
Ashish Jain83a6cc22016-06-28 14:34:17 +05305181 out->convert_buffer != NULL) {
5182
5183 memcpy_by_audio_format(out->convert_buffer,
5184 out->hal_op_format,
5185 buffer,
5186 out->hal_ip_format,
5187 out->config.period_size * out->config.channels);
5188
5189 ret = pcm_write(out->pcm, out->convert_buffer,
5190 (out->config.period_size *
5191 out->config.channels *
5192 format_to_bitwidth_table[out->hal_op_format]));
5193 } else {
Aditya Bavanarid4db8ee2017-05-29 21:08:03 +05305194 /*
5195 * To avoid underrun in DSP when the application is not pumping
5196 * data at required rate, check for the no. of bytes and ignore
5197 * pcm_write if it is less than actual buffer size.
5198 * It is a work around to a change in compress VOIP driver.
5199 */
5200 if ((out->flags & AUDIO_OUTPUT_FLAG_VOIP_RX) &&
5201 bytes < (out->config.period_size * out->config.channels *
5202 audio_bytes_per_sample(out->format))) {
5203 size_t voip_buf_size =
5204 out->config.period_size * out->config.channels *
5205 audio_bytes_per_sample(out->format);
5206 ALOGE("%s:VOIP underrun: bytes received %zu, required:%zu\n",
5207 __func__, bytes, voip_buf_size);
5208 usleep(((uint64_t)voip_buf_size - bytes) *
5209 1000000 / audio_stream_out_frame_size(stream) /
5210 out_get_sample_rate(&out->stream.common));
5211 ret = 0;
Vignesh Kulothungana6927272019-02-20 15:17:07 -08005212 } else {
5213 if (out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS)
5214 ret = split_and_write_audio_haptic_data(out, buffer, bytes);
5215 else
5216 ret = pcm_write(out->pcm, (void *)buffer, bytes_to_write);
5217 }
Ashish Jain83a6cc22016-06-28 14:34:17 +05305218 }
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07005219
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005220 release_out_focus(out);
5221
Dhanalakshmi Siddani37ca1d62014-08-20 12:28:34 +05305222 if (ret < 0)
5223 ret = -errno;
Zhou Songc9672822017-08-16 16:01:39 +08005224 else if (ret > 0)
Ashish Jain83a6cc22016-06-28 14:34:17 +05305225 ret = -EINVAL;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005226 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005227 }
5228
5229exit:
Zhou Songc9672822017-08-16 16:01:39 +08005230 update_frames_written(out, bytes);
Naresh Tanniru4c630392014-05-12 01:05:52 +05305231 if (-ENETRESET == ret) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305232 out->card_status = CARD_STATUS_OFFLINE;
Naresh Tanniru4c630392014-05-12 01:05:52 +05305233 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005234 pthread_mutex_unlock(&out->lock);
5235
5236 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07005237 if (out->pcm)
Alexy Josephb1379942016-01-29 15:49:38 -08005238 ALOGE("%s: error %d, %s", __func__, (int)ret, pcm_get_error(out->pcm));
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05305239 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05305240 pthread_mutex_lock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05305241 voice_extn_compress_voip_close_output_stream(&out->stream.common);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05305242 out->started = 0;
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05305243 pthread_mutex_unlock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05305244 out->standby = true;
5245 }
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305246 out_on_error(&out->stream.common);
Dieter Luecking5d57def2018-09-07 14:23:37 +02005247 if (!(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
5248 /* prevent division-by-zero */
5249 uint32_t stream_size = audio_stream_out_frame_size(stream);
5250 uint32_t srate = out_get_sample_rate(&out->stream.common);
Vidyakumar Athotaa9d3a5f2017-08-09 12:13:05 -07005251
Dieter Luecking5d57def2018-09-07 14:23:37 +02005252 if ((stream_size == 0) || (srate == 0)) {
5253 ALOGE("%s: stream_size= %d, srate = %d", __func__, stream_size, srate);
5254 ATRACE_END();
5255 return -EINVAL;
5256 }
5257 usleep((uint64_t)bytes * 1000000 / stream_size / srate);
5258 }
Vidyakumar Athotaa9d3a5f2017-08-09 12:13:05 -07005259 if (audio_extn_passthru_is_passthrough_stream(out)) {
Rajshekar Eashwarappa88834522018-04-02 17:20:15 +05305260 //ALOGE("%s: write error, ret = %zd", __func__, ret);
Haynes Mathew George380745d2017-10-04 15:27:45 -07005261 ATRACE_END();
Vidyakumar Athotaa9d3a5f2017-08-09 12:13:05 -07005262 return ret;
5263 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005264 }
Haynes Mathew George380745d2017-10-04 15:27:45 -07005265 ATRACE_END();
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005266 return bytes;
5267}
5268
5269static int out_get_render_position(const struct audio_stream_out *stream,
5270 uint32_t *dsp_frames)
5271{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005272 struct stream_out *out = (struct stream_out *)stream;
Zhou Song32a556e2015-05-05 10:46:56 +08005273
5274 if (dsp_frames == NULL)
5275 return -EINVAL;
5276
5277 *dsp_frames = 0;
5278 if (is_offload_usecase(out->usecase)) {
Mingming Yin9e348b52014-11-19 16:18:55 -08005279 ssize_t ret = 0;
Ashish Jain5106d362016-05-11 19:23:33 +05305280
5281 /* Below piece of code is not guarded against any lock beacuse audioFliner serializes
5282 * this operation and adev_close_output_stream(where out gets reset).
5283 */
Dhananjay Kumarac341582017-02-23 23:42:25 +05305284 if (!out->non_blocking && !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Zhou Song48453a02018-01-10 17:50:59 +08005285 *dsp_frames = get_actual_pcm_frames_rendered(out, NULL);
Ashish Jain5106d362016-05-11 19:23:33 +05305286 ALOGVV("dsp_frames %d sampleRate %d",(int)*dsp_frames,out->sample_rate);
Haynes Mathew Georgeb0f5dc32017-10-06 18:35:12 -07005287 adjust_frames_for_device_delay(out, dsp_frames);
Ashish Jain5106d362016-05-11 19:23:33 +05305288 return 0;
5289 }
5290
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005291 lock_output_stream(out);
Ashish Jain5106d362016-05-11 19:23:33 +05305292 if (out->compr != NULL && out->non_blocking) {
Naresh Tanniru80659832014-06-04 18:17:56 +05305293 ret = compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005294 &out->sample_rate);
Dhanalakshmi Siddani37ca1d62014-08-20 12:28:34 +05305295 if (ret < 0)
5296 ret = -errno;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005297 ALOGVV("%s rendered frames %d sample_rate %d",
Ashish Jain5106d362016-05-11 19:23:33 +05305298 __func__, *dsp_frames, out->sample_rate);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005299 }
Naresh Tanniru80659832014-06-04 18:17:56 +05305300 if (-ENETRESET == ret) {
5301 ALOGE(" ERROR: sound card not active Unable to get time stamp from compress driver");
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305302 out->card_status = CARD_STATUS_OFFLINE;
5303 ret = -EINVAL;
Naresh Tanniru80659832014-06-04 18:17:56 +05305304 } else if(ret < 0) {
5305 ALOGE(" ERROR: Unable to get time stamp from compress driver");
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305306 ret = -EINVAL;
5307 } else if (out->card_status == CARD_STATUS_OFFLINE) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05305308 /*
5309 * Handle corner case where compress session is closed during SSR
5310 * and timestamp is queried
5311 */
5312 ALOGE(" ERROR: sound card not active, return error");
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305313 ret = -EINVAL;
Manisha Agarwal7b3e3772019-02-20 14:33:45 +05305314 } else if (out->prev_card_status_offline) {
5315 ALOGE("ERROR: previously sound card was offline,return error");
5316 ret = -EINVAL;
Naresh Tanniru80659832014-06-04 18:17:56 +05305317 } else {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305318 ret = 0;
Haynes Mathew Georgeb0f5dc32017-10-06 18:35:12 -07005319 adjust_frames_for_device_delay(out, dsp_frames);
Naresh Tanniru80659832014-06-04 18:17:56 +05305320 }
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305321 pthread_mutex_unlock(&out->lock);
5322 return ret;
Zhou Song32a556e2015-05-05 10:46:56 +08005323 } else if (audio_is_linear_pcm(out->format)) {
5324 *dsp_frames = out->written;
Haynes Mathew Georgeb0f5dc32017-10-06 18:35:12 -07005325 adjust_frames_for_device_delay(out, dsp_frames);
Zhou Song32a556e2015-05-05 10:46:56 +08005326 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005327 } else
5328 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005329}
5330
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07005331static int out_add_audio_effect(const struct audio_stream *stream __unused,
5332 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005333{
5334 return 0;
5335}
5336
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07005337static int out_remove_audio_effect(const struct audio_stream *stream __unused,
5338 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005339{
5340 return 0;
5341}
5342
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07005343static int out_get_next_write_timestamp(const struct audio_stream_out *stream __unused,
5344 int64_t *timestamp __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005345{
Satya Krishna Pindiprolib6655542017-07-03 19:38:19 +05305346 return -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005347}
5348
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07005349static int out_get_presentation_position(const struct audio_stream_out *stream,
5350 uint64_t *frames, struct timespec *timestamp)
5351{
5352 struct stream_out *out = (struct stream_out *)stream;
pavance65c2fe2017-10-18 17:52:01 +05305353 int ret = -ENODATA;
Eric Laurent949a0892013-09-20 09:20:13 -07005354 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07005355
Ashish Jain5106d362016-05-11 19:23:33 +05305356 /* below piece of code is not guarded against any lock because audioFliner serializes
5357 * this operation and adev_close_output_stream( where out gets reset).
5358 */
5359 if (is_offload_usecase(out->usecase) && !out->non_blocking &&
Dhananjay Kumarac341582017-02-23 23:42:25 +05305360 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Zhou Song48453a02018-01-10 17:50:59 +08005361 *frames = get_actual_pcm_frames_rendered(out, timestamp);
Ashish Jain5106d362016-05-11 19:23:33 +05305362 ALOGVV("frames %lld playedat %lld",(long long int)*frames,
5363 timestamp->tv_sec * 1000000LL + timestamp->tv_nsec / 1000);
5364 return 0;
5365 }
5366
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005367 lock_output_stream(out);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07005368
Ashish Jain5106d362016-05-11 19:23:33 +05305369 if (is_offload_usecase(out->usecase) && out->compr != NULL && out->non_blocking) {
5370 ret = compress_get_tstamp(out->compr, &dsp_frames,
5371 &out->sample_rate);
yidongh0515e042017-07-06 15:00:34 +08005372 // Adjustment accounts for A2dp encoder latency with offload usecases
5373 // Note: Encoder latency is returned in ms.
5374 if (AUDIO_DEVICE_OUT_ALL_A2DP & out->devices) {
5375 unsigned long offset =
5376 (audio_extn_a2dp_get_encoder_latency() * out->sample_rate / 1000);
5377 dsp_frames = (dsp_frames > offset) ? (dsp_frames - offset) : 0;
5378 }
Ashish Jain5106d362016-05-11 19:23:33 +05305379 ALOGVV("%s rendered frames %ld sample_rate %d",
5380 __func__, dsp_frames, out->sample_rate);
5381 *frames = dsp_frames;
5382 if (ret < 0)
5383 ret = -errno;
5384 if (-ENETRESET == ret) {
5385 ALOGE(" ERROR: sound card not active Unable to get time stamp from compress driver");
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305386 out->card_status = CARD_STATUS_OFFLINE;
Ashish Jain5106d362016-05-11 19:23:33 +05305387 ret = -EINVAL;
5388 } else
5389 ret = 0;
5390 /* this is the best we can do */
5391 clock_gettime(CLOCK_MONOTONIC, timestamp);
Eric Laurent949a0892013-09-20 09:20:13 -07005392 } else {
5393 if (out->pcm) {
Weiyin Jiangd4633762018-03-16 12:05:03 +08005394 unsigned int avail;
5395 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
5396 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
5397 int64_t signed_frames = out->written - kernel_buffer_size + avail;
5398 // This adjustment accounts for buffering after app processor.
5399 // It is based on estimated DSP latency per use case, rather than exact.
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07005400 signed_frames -=
Weiyin Jiangd4633762018-03-16 12:05:03 +08005401 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
Aniket Kumar Lataff613152017-07-18 18:19:21 -07005402
Weiyin Jiangd4633762018-03-16 12:05:03 +08005403 // Adjustment accounts for A2dp encoder latency with non offload usecases
5404 // Note: Encoder latency is returned in ms, while platform_render_latency in us.
5405 if (AUDIO_DEVICE_OUT_ALL_A2DP & out->devices) {
5406 signed_frames -=
5407 (audio_extn_a2dp_get_encoder_latency() * out->sample_rate / 1000);
5408 }
5409
5410 // It would be unusual for this value to be negative, but check just in case ...
5411 if (signed_frames >= 0) {
5412 *frames = signed_frames;
5413 ret = 0;
5414 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07005415 }
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305416 } else if (out->card_status == CARD_STATUS_OFFLINE) {
Ashish Jainbbce4322016-02-16 13:25:27 +05305417 *frames = out->written;
5418 clock_gettime(CLOCK_MONOTONIC, timestamp);
Manisha Agarwal7b3e3772019-02-20 14:33:45 +05305419 if (is_offload_usecase(out->usecase))
5420 ret = -EINVAL;
5421 else
5422 ret = 0;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07005423 }
5424 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07005425 pthread_mutex_unlock(&out->lock);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07005426 return ret;
5427}
5428
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005429static int out_set_callback(struct audio_stream_out *stream,
5430 stream_callback_t callback, void *cookie)
5431{
5432 struct stream_out *out = (struct stream_out *)stream;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08005433 int ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005434
5435 ALOGV("%s", __func__);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005436 lock_output_stream(out);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08005437 out->client_callback = callback;
5438 out->client_cookie = cookie;
5439 if (out->adsp_hdlr_stream_handle) {
5440 ret = audio_extn_adsp_hdlr_stream_set_callback(
5441 out->adsp_hdlr_stream_handle,
5442 callback,
5443 cookie);
5444 if (ret)
5445 ALOGW("%s:adsp hdlr callback registration failed %d",
5446 __func__, ret);
5447 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005448 pthread_mutex_unlock(&out->lock);
5449 return 0;
5450}
5451
5452static int out_pause(struct audio_stream_out* stream)
5453{
5454 struct stream_out *out = (struct stream_out *)stream;
5455 int status = -ENOSYS;
5456 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07005457 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07005458 ALOGD("copl(%p):pause compress driver", out);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005459 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005460 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305461 if (out->card_status != CARD_STATUS_OFFLINE)
Naresh Tanniru80659832014-06-04 18:17:56 +05305462 status = compress_pause(out->compr);
5463
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005464 out->offload_state = OFFLOAD_STATE_PAUSED;
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08005465
Mingming Yin21854652016-04-13 11:54:02 -07005466 if (audio_extn_passthru_is_active()) {
5467 ALOGV("offload use case, pause passthru");
5468 audio_extn_passthru_on_pause(out);
5469 }
5470
Dhanalakshmi Siddani79415e72015-03-23 11:54:47 +05305471 audio_extn_dts_eagle_fade(adev, false, out);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08005472 audio_extn_dts_notify_playback_state(out->usecase, 0,
5473 out->sample_rate, popcount(out->channel_mask),
5474 0);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005475 }
5476 pthread_mutex_unlock(&out->lock);
5477 }
5478 return status;
5479}
5480
5481static int out_resume(struct audio_stream_out* stream)
5482{
5483 struct stream_out *out = (struct stream_out *)stream;
5484 int status = -ENOSYS;
5485 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07005486 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07005487 ALOGD("copl(%p):resume compress driver", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005488 status = 0;
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005489 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005490 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305491 if (out->card_status != CARD_STATUS_OFFLINE) {
Naresh Tanniru80659832014-06-04 18:17:56 +05305492 status = compress_resume(out->compr);
Mingming Yin21854652016-04-13 11:54:02 -07005493 }
5494 if (!status) {
5495 out->offload_state = OFFLOAD_STATE_PLAYING;
5496 }
Dhanalakshmi Siddani79415e72015-03-23 11:54:47 +05305497 audio_extn_dts_eagle_fade(adev, true, out);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08005498 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
5499 popcount(out->channel_mask), 1);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005500 }
5501 pthread_mutex_unlock(&out->lock);
5502 }
5503 return status;
5504}
5505
5506static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
5507{
5508 struct stream_out *out = (struct stream_out *)stream;
5509 int status = -ENOSYS;
5510 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07005511 if (is_offload_usecase(out->usecase)) {
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005512 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005513 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
5514 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
5515 else
5516 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
5517 pthread_mutex_unlock(&out->lock);
5518 }
5519 return status;
5520}
5521
5522static int out_flush(struct audio_stream_out* stream)
5523{
5524 struct stream_out *out = (struct stream_out *)stream;
5525 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07005526 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07005527 ALOGD("copl(%p):calling compress flush", out);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005528 lock_output_stream(out);
Haynes Mathew Georgeafe54d82016-09-21 14:39:19 -07005529 if (out->offload_state == OFFLOAD_STATE_PAUSED) {
5530 stop_compressed_output_l(out);
Haynes Mathew Georgeafe54d82016-09-21 14:39:19 -07005531 } else {
5532 ALOGW("%s called in invalid state %d", __func__, out->offload_state);
5533 }
Weiyin Jiang547e4152017-09-14 17:24:18 +08005534 out->written = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005535 pthread_mutex_unlock(&out->lock);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07005536 ALOGD("copl(%p):out of compress flush", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005537 return 0;
5538 }
5539 return -ENOSYS;
5540}
5541
Haynes Mathew George16081042017-05-31 17:16:49 -07005542static int out_stop(const struct audio_stream_out* stream)
5543{
5544 struct stream_out *out = (struct stream_out *)stream;
5545 struct audio_device *adev = out->dev;
5546 int ret = -ENOSYS;
5547
5548 ALOGV("%s", __func__);
5549 pthread_mutex_lock(&adev->lock);
5550 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP && !out->standby &&
5551 out->playback_started && out->pcm != NULL) {
5552 pcm_stop(out->pcm);
5553 ret = stop_output_stream(out);
5554 out->playback_started = false;
5555 }
5556 pthread_mutex_unlock(&adev->lock);
5557 return ret;
5558}
5559
5560static int out_start(const struct audio_stream_out* stream)
5561{
5562 struct stream_out *out = (struct stream_out *)stream;
5563 struct audio_device *adev = out->dev;
5564 int ret = -ENOSYS;
5565
5566 ALOGV("%s", __func__);
5567 pthread_mutex_lock(&adev->lock);
5568 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP && !out->standby &&
5569 !out->playback_started && out->pcm != NULL) {
5570 ret = start_output_stream(out);
5571 if (ret == 0) {
5572 out->playback_started = true;
5573 }
5574 }
5575 pthread_mutex_unlock(&adev->lock);
5576 return ret;
5577}
5578
5579/*
5580 * Modify config->period_count based on min_size_frames
5581 */
5582static void adjust_mmap_period_count(struct pcm_config *config, int32_t min_size_frames)
5583{
5584 int periodCountRequested = (min_size_frames + config->period_size - 1)
5585 / config->period_size;
5586 int periodCount = MMAP_PERIOD_COUNT_MIN;
5587
5588 ALOGV("%s original config.period_size = %d config.period_count = %d",
5589 __func__, config->period_size, config->period_count);
5590
5591 while (periodCount < periodCountRequested && (periodCount * 2) < MMAP_PERIOD_COUNT_MAX) {
5592 periodCount *= 2;
5593 }
5594 config->period_count = periodCount;
5595
5596 ALOGV("%s requested config.period_count = %d", __func__, config->period_count);
5597}
5598
5599static int out_create_mmap_buffer(const struct audio_stream_out *stream,
5600 int32_t min_size_frames,
5601 struct audio_mmap_buffer_info *info)
5602{
5603 struct stream_out *out = (struct stream_out *)stream;
5604 struct audio_device *adev = out->dev;
5605 int ret = 0;
Aalique Grahame1f123102017-10-12 10:38:32 -07005606 unsigned int offset1 = 0;
5607 unsigned int frames1 = 0;
Haynes Mathew George16081042017-05-31 17:16:49 -07005608 const char *step = "";
Haynes Mathew Georgeef514882017-05-01 17:46:23 -07005609 uint32_t mmap_size;
Arun Mirpuri5d170872019-03-26 13:21:31 -07005610 uint32_t buffer_size;
Haynes Mathew George16081042017-05-31 17:16:49 -07005611
Arun Mirpuri5d170872019-03-26 13:21:31 -07005612 ALOGD("%s", __func__);
Sharad Sanglec6f32552018-05-04 16:15:38 +05305613 lock_output_stream(out);
Haynes Mathew George16081042017-05-31 17:16:49 -07005614 pthread_mutex_lock(&adev->lock);
5615
Sharad Sanglec6f32552018-05-04 16:15:38 +05305616 if (CARD_STATUS_OFFLINE == out->card_status ||
5617 CARD_STATUS_OFFLINE == adev->card_status) {
5618 ALOGW("out->card_status or adev->card_status offline, try again");
5619 ret = -EIO;
5620 goto exit;
5621 }
Haynes Mathew George16081042017-05-31 17:16:49 -07005622 if (info == NULL || min_size_frames == 0) {
5623 ALOGE("%s: info = %p, min_size_frames = %d", __func__, info, min_size_frames);
5624 ret = -EINVAL;
5625 goto exit;
5626 }
5627 if (out->usecase != USECASE_AUDIO_PLAYBACK_MMAP || !out->standby) {
5628 ALOGE("%s: usecase = %d, standby = %d", __func__, out->usecase, out->standby);
5629 ret = -ENOSYS;
5630 goto exit;
5631 }
5632 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
5633 if (out->pcm_device_id < 0) {
5634 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
5635 __func__, out->pcm_device_id, out->usecase);
5636 ret = -EINVAL;
5637 goto exit;
5638 }
5639
5640 adjust_mmap_period_count(&out->config, min_size_frames);
5641
Arun Mirpuri5d170872019-03-26 13:21:31 -07005642 ALOGD("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
Haynes Mathew George16081042017-05-31 17:16:49 -07005643 __func__, adev->snd_card, out->pcm_device_id, out->config.channels);
5644 out->pcm = pcm_open(adev->snd_card, out->pcm_device_id,
5645 (PCM_OUT | PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC), &out->config);
Satish Babu Patakokila54ce83d2018-07-06 18:00:37 +05305646 if (errno == ENETRESET && !pcm_is_ready(out->pcm)) {
Sharad Sanglec6f32552018-05-04 16:15:38 +05305647 ALOGE("%s: pcm_open failed errno:%d\n", __func__, errno);
5648 out->card_status = CARD_STATUS_OFFLINE;
5649 adev->card_status = CARD_STATUS_OFFLINE;
5650 ret = -EIO;
5651 goto exit;
5652 }
5653
Haynes Mathew George16081042017-05-31 17:16:49 -07005654 if (out->pcm == NULL || !pcm_is_ready(out->pcm)) {
5655 step = "open";
5656 ret = -ENODEV;
5657 goto exit;
5658 }
5659 ret = pcm_mmap_begin(out->pcm, &info->shared_memory_address, &offset1, &frames1);
5660 if (ret < 0) {
5661 step = "begin";
5662 goto exit;
5663 }
5664 info->buffer_size_frames = pcm_get_buffer_size(out->pcm);
Arun Mirpuri5d170872019-03-26 13:21:31 -07005665 buffer_size = pcm_frames_to_bytes(out->pcm, info->buffer_size_frames);
Haynes Mathew George16081042017-05-31 17:16:49 -07005666 info->burst_size_frames = out->config.period_size;
Haynes Mathew Georgeef514882017-05-01 17:46:23 -07005667 ret = platform_get_mmap_data_fd(adev->platform,
5668 out->pcm_device_id, 0 /*playback*/,
5669 &info->shared_memory_fd,
5670 &mmap_size);
5671 if (ret < 0) {
Arun Mirpuri5d170872019-03-26 13:21:31 -07005672 // Fall back to non exclusive mode
5673 info->shared_memory_fd = pcm_get_poll_fd(out->pcm);
5674 } else {
5675 if (mmap_size < buffer_size) {
5676 step = "mmap";
5677 goto exit;
5678 }
5679 // FIXME: indicate exclusive mode support by returning a negative buffer size
5680 info->buffer_size_frames *= -1;
Haynes Mathew Georgeef514882017-05-01 17:46:23 -07005681 }
Haynes Mathew George16081042017-05-31 17:16:49 -07005682 memset(info->shared_memory_address, 0, pcm_frames_to_bytes(out->pcm,
Haynes Mathew Georgeef514882017-05-01 17:46:23 -07005683 info->buffer_size_frames));
Haynes Mathew George16081042017-05-31 17:16:49 -07005684
5685 ret = pcm_mmap_commit(out->pcm, 0, MMAP_PERIOD_SIZE);
5686 if (ret < 0) {
5687 step = "commit";
5688 goto exit;
5689 }
5690
5691 out->standby = false;
5692 ret = 0;
5693
Arun Mirpuri5d170872019-03-26 13:21:31 -07005694 ALOGD("%s: got mmap buffer address %p info->buffer_size_frames %d",
Haynes Mathew George16081042017-05-31 17:16:49 -07005695 __func__, info->shared_memory_address, info->buffer_size_frames);
5696
5697exit:
5698 if (ret != 0) {
5699 if (out->pcm == NULL) {
5700 ALOGE("%s: %s - %d", __func__, step, ret);
5701 } else {
5702 ALOGE("%s: %s %s", __func__, step, pcm_get_error(out->pcm));
5703 pcm_close(out->pcm);
5704 out->pcm = NULL;
5705 }
5706 }
5707 pthread_mutex_unlock(&adev->lock);
Sharad Sanglec6f32552018-05-04 16:15:38 +05305708 pthread_mutex_unlock(&out->lock);
Haynes Mathew George16081042017-05-31 17:16:49 -07005709 return ret;
5710}
5711
5712static int out_get_mmap_position(const struct audio_stream_out *stream,
5713 struct audio_mmap_position *position)
5714{
5715 struct stream_out *out = (struct stream_out *)stream;
5716 ALOGVV("%s", __func__);
5717 if (position == NULL) {
5718 return -EINVAL;
5719 }
5720 if (out->usecase != USECASE_AUDIO_PLAYBACK_MMAP) {
Haynes Mathew George4ab3ba92017-12-11 14:49:43 -08005721 ALOGE("%s: called on %s", __func__, use_case_table[out->usecase]);
Haynes Mathew George16081042017-05-31 17:16:49 -07005722 return -ENOSYS;
5723 }
5724 if (out->pcm == NULL) {
5725 return -ENOSYS;
5726 }
5727
5728 struct timespec ts = { 0, 0 };
5729 int ret = pcm_mmap_get_hw_ptr(out->pcm, (unsigned int *)&position->position_frames, &ts);
5730 if (ret < 0) {
5731 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
5732 return ret;
5733 }
Aalique Grahame90717e22018-07-18 15:54:18 -07005734 position->time_nanoseconds = ts.tv_sec*1000000000LL + ts.tv_nsec;
Haynes Mathew George16081042017-05-31 17:16:49 -07005735 return 0;
5736}
5737
5738
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005739/** audio_stream_in implementation **/
5740static uint32_t in_get_sample_rate(const struct audio_stream *stream)
5741{
5742 struct stream_in *in = (struct stream_in *)stream;
5743
5744 return in->config.rate;
5745}
5746
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07005747static int in_set_sample_rate(struct audio_stream *stream __unused,
5748 uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005749{
5750 return -ENOSYS;
5751}
5752
5753static size_t in_get_buffer_size(const struct audio_stream *stream)
5754{
5755 struct stream_in *in = (struct stream_in *)stream;
5756
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08005757 if(in->usecase == USECASE_COMPRESS_VOIP_CALL)
5758 return voice_extn_compress_voip_in_get_buffer_size(in);
Vikram Panduranga93f080e2017-06-07 18:16:14 -07005759 else if(in->usecase == USECASE_AUDIO_RECORD_VOIP)
5760 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 -07005761 else if(audio_extn_compr_cap_usecase_supported(in->usecase))
5762 return audio_extn_compr_cap_get_buffer_size(in->config.format);
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05305763 else if(audio_extn_cin_attached_usecase(in->usecase))
5764 return audio_extn_cin_get_buffer_size(in);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08005765
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005766 return in->config.period_size * in->af_period_multiplier *
5767 audio_stream_in_frame_size((const struct audio_stream_in *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005768}
5769
5770static uint32_t in_get_channels(const struct audio_stream *stream)
5771{
5772 struct stream_in *in = (struct stream_in *)stream;
5773
5774 return in->channel_mask;
5775}
5776
5777static audio_format_t in_get_format(const struct audio_stream *stream)
5778{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08005779 struct stream_in *in = (struct stream_in *)stream;
5780
5781 return in->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005782}
5783
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07005784static int in_set_format(struct audio_stream *stream __unused,
5785 audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005786{
5787 return -ENOSYS;
5788}
5789
5790static int in_standby(struct audio_stream *stream)
5791{
5792 struct stream_in *in = (struct stream_in *)stream;
5793 struct audio_device *adev = in->dev;
5794 int status = 0;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05305795 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
5796 stream, in->usecase, use_case_table[in->usecase]);
Haynes Mathew George16081042017-05-31 17:16:49 -07005797 bool do_stop = true;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05305798
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005799 lock_input_stream(in);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07005800 if (!in->standby && in->is_st_session) {
5801 ALOGD("%s: sound trigger pcm stop lab", __func__);
5802 audio_extn_sound_trigger_stop_lab(in);
Quinn Malef6050362019-01-30 15:55:40 -08005803 adev->num_va_sessions--;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07005804 in->standby = 1;
5805 }
5806
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005807 if (!in->standby) {
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07005808 if (adev->adm_deregister_stream)
5809 adev->adm_deregister_stream(adev->adm_data, in->capture_handle);
5810
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08005811 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005812 in->standby = true;
Zhou Songa8895042016-07-05 17:54:22 +08005813 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
kunleizbecba2d2017-09-07 13:37:16 +08005814 do_stop = false;
Zhou Songa8895042016-07-05 17:54:22 +08005815 voice_extn_compress_voip_close_input_stream(stream);
5816 ALOGD("VOIP input entered standby");
Haynes Mathew George16081042017-05-31 17:16:49 -07005817 } else if (in->usecase == USECASE_AUDIO_RECORD_MMAP) {
5818 do_stop = in->capture_started;
5819 in->capture_started = false;
Zhou Songa8895042016-07-05 17:54:22 +08005820 } else {
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05305821 if (audio_extn_cin_attached_usecase(in->usecase))
5822 audio_extn_cin_stop_input_stream(in);
kunleizbecba2d2017-09-07 13:37:16 +08005823 }
5824
Arun Mirpuri5d170872019-03-26 13:21:31 -07005825 if (in->pcm) {
5826 ATRACE_BEGIN("pcm_in_close");
5827 pcm_close(in->pcm);
5828 ATRACE_END();
5829 in->pcm = NULL;
5830 }
5831
5832 if(do_stop) {
Aalique Grahame22e49102018-12-18 14:23:57 -08005833 adev->enable_voicerx = false;
5834 platform_set_echo_reference(adev, false, AUDIO_DEVICE_NONE);
Zhou Songa8895042016-07-05 17:54:22 +08005835 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08005836 }
Quinn Malef6050362019-01-30 15:55:40 -08005837
5838 if (in->source == AUDIO_SOURCE_VOICE_RECOGNITION)
5839 adev->num_va_sessions--;
5840
Eric Laurent150dbfe2013-02-27 14:31:02 -08005841 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005842 }
5843 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07005844 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005845 return status;
5846}
5847
Aalique Grahame22e49102018-12-18 14:23:57 -08005848static int in_dump(const struct audio_stream *stream,
5849 int fd)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005850{
Aalique Grahame22e49102018-12-18 14:23:57 -08005851 struct stream_in *in = (struct stream_in *)stream;
5852
5853 // We try to get the lock for consistency,
5854 // but it isn't necessary for these variables.
5855 // If we're not in standby, we may be blocked on a read.
5856 const bool locked = (pthread_mutex_trylock(&in->lock) == 0);
5857 dprintf(fd, " Standby: %s\n", in->standby ? "yes" : "no");
5858 dprintf(fd, " Frames read: %lld\n", (long long)in->frames_read);
5859 dprintf(fd, " Frames muted: %lld\n", (long long)in->frames_muted);
5860
5861 if (locked) {
5862 pthread_mutex_unlock(&in->lock);
5863 }
5864
5865 // dump error info
5866 (void)error_log_dump(
5867 in->error_log, fd, " " /* prefix */, 0 /* lines */, 0 /* limit_ns */);
5868
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005869 return 0;
5870}
5871
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305872static void in_snd_mon_cb(void * stream, struct str_parms * parms)
5873{
5874 if (!stream || !parms)
5875 return;
5876
5877 struct stream_in *in = (struct stream_in *)stream;
5878 struct audio_device *adev = in->dev;
5879
5880 card_status_t status;
5881 int card;
5882 if (parse_snd_card_status(parms, &card, &status) < 0)
5883 return;
5884
5885 pthread_mutex_lock(&adev->lock);
5886 bool valid_cb = (card == adev->snd_card);
5887 pthread_mutex_unlock(&adev->lock);
5888
5889 if (!valid_cb)
5890 return;
5891
5892 lock_input_stream(in);
5893 if (in->card_status != status)
5894 in->card_status = status;
5895 pthread_mutex_unlock(&in->lock);
5896
5897 ALOGW("in_snd_mon_cb for card %d usecase %s, status %s", card,
5898 use_case_table[in->usecase],
5899 status == CARD_STATUS_OFFLINE ? "offline" : "online");
5900
5901 // a better solution would be to report error back to AF and let
5902 // it put the stream to standby
5903 if (status == CARD_STATUS_OFFLINE)
5904 in_standby(&in->stream.common);
5905
5906 return;
5907}
5908
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005909static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
5910{
5911 struct stream_in *in = (struct stream_in *)stream;
5912 struct audio_device *adev = in->dev;
5913 struct str_parms *parms;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005914 char value[32];
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08005915 int ret = 0, val = 0, err;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005916
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05305917 ALOGD("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005918 parms = str_parms_create_str(kvpairs);
5919
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05305920 if (!parms)
5921 goto error;
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005922 lock_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08005923 pthread_mutex_lock(&adev->lock);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08005924
5925 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
5926 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005927 val = atoi(value);
5928 /* no audio source uses val == 0 */
5929 if ((in->source != val) && (val != 0)) {
5930 in->source = val;
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08005931 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
5932 (in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
5933 (voice_extn_compress_voip_is_format_supported(in->format)) &&
Shiv Maliyappanahallie66aba22016-01-27 16:08:57 -08005934 (in->config.rate == 8000 || in->config.rate == 16000 ||
5935 in->config.rate == 32000 || in->config.rate == 48000 ) &&
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07005936 (audio_channel_count_from_in_mask(in->channel_mask) == 1)) {
Narsinga Rao Chella7d5a3e82014-02-04 16:23:52 -08005937 err = voice_extn_compress_voip_open_input_stream(in);
5938 if (err != 0) {
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08005939 ALOGE("%s: Compress voip input cannot be opened, error:%d",
Narsinga Rao Chella7d5a3e82014-02-04 16:23:52 -08005940 __func__, err);
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08005941 }
5942 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005943 }
5944 }
5945
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08005946 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
5947 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005948 val = atoi(value);
Kevin Rocardfce19002017-08-07 19:21:36 -07005949 if (((int)in->device != val) && (val != 0) && audio_is_input_device(val) ) {
5950
5951 // Workaround: If routing to an non existing usb device, fail gracefully
5952 // The routing request will otherwise block during 10 second
5953 int card;
5954 if (audio_is_usb_in_device(val) &&
5955 (card = get_alive_usb_card(parms)) >= 0) {
5956
5957 ALOGW("in_set_parameters() ignoring rerouting to non existing USB card %d", card);
5958 ret = -ENOSYS;
5959 } else {
5960
5961 in->device = val;
5962 /* If recording is in progress, change the tx device to new device */
5963 if (!in->standby && !in->is_st_session) {
5964 ALOGV("update input routing change");
5965 // inform adm before actual routing to prevent glitches.
5966 if (adev->adm_on_routing_change) {
Haynes Mathew George822b5492016-07-01 16:57:24 -07005967 adev->adm_on_routing_change(adev->adm_data,
5968 in->capture_handle);
Kevin Rocardfce19002017-08-07 19:21:36 -07005969 ret = select_devices(adev, in->usecase);
5970 }
5971 }
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005972 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005973 }
5974 }
5975
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +05305976 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_PROFILE, value, sizeof(value));
5977 if (err >= 0) {
5978 strlcpy(in->profile, value, sizeof(in->profile));
5979 ALOGV("updating stream profile with value '%s'", in->profile);
5980 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
5981 &adev->streams_input_cfg_list,
5982 in->device, in->flags, in->format,
5983 in->sample_rate, in->bit_width,
5984 in->profile, &in->app_type_cfg);
5985 }
5986
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005987 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08005988 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005989
5990 str_parms_destroy(parms);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05305991error:
Eric Laurent994a6932013-07-17 11:51:42 -07005992 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005993 return ret;
5994}
5995
5996static char* in_get_parameters(const struct audio_stream *stream,
5997 const char *keys)
5998{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08005999 struct stream_in *in = (struct stream_in *)stream;
6000 struct str_parms *query = str_parms_create_str(keys);
6001 char *str;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08006002 struct str_parms *reply = str_parms_create();
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07006003
6004 if (!query || !reply) {
Alexy Josephaee4fdd2016-01-29 13:02:07 -08006005 if (reply) {
6006 str_parms_destroy(reply);
6007 }
6008 if (query) {
6009 str_parms_destroy(query);
6010 }
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07006011 ALOGE("in_get_parameters: failed to create query or reply");
6012 return NULL;
6013 }
6014
Haynes Mathew George484e8d22017-07-31 18:55:17 -07006015 ALOGV("%s: enter: keys - %s %s ", __func__, use_case_table[in->usecase], keys);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08006016
6017 voice_extn_in_get_parameters(in, query, reply);
6018
Haynes Mathew George484e8d22017-07-31 18:55:17 -07006019 stream_get_parameter_channels(query, reply,
6020 &in->supported_channel_masks[0]);
6021 stream_get_parameter_formats(query, reply,
6022 &in->supported_formats[0]);
6023 stream_get_parameter_rates(query, reply,
6024 &in->supported_sample_rates[0]);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08006025 str = str_parms_to_str(reply);
6026 str_parms_destroy(query);
6027 str_parms_destroy(reply);
6028
6029 ALOGV("%s: exit: returns - %s", __func__, str);
6030 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006031}
6032
Aalique Grahame22e49102018-12-18 14:23:57 -08006033static int in_set_gain(struct audio_stream_in *stream,
6034 float gain)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006035{
Aalique Grahame22e49102018-12-18 14:23:57 -08006036 struct stream_in *in = (struct stream_in *)stream;
6037 char mixer_ctl_name[128];
6038 struct mixer_ctl *ctl;
6039 int ctl_value;
6040
6041 ALOGV("%s: gain %f", __func__, gain);
6042
6043 if (stream == NULL)
6044 return -EINVAL;
6045
6046 /* in_set_gain() only used to silence MMAP capture for now */
6047 if (in->usecase != USECASE_AUDIO_RECORD_MMAP)
6048 return -ENOSYS;
6049
6050 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name), "Capture %d Volume", in->pcm_device_id);
6051
6052 ctl = mixer_get_ctl_by_name(in->dev->mixer, mixer_ctl_name);
6053 if (!ctl) {
6054 ALOGW("%s: Could not get ctl for mixer cmd - %s",
6055 __func__, mixer_ctl_name);
6056 return -ENOSYS;
6057 }
6058
6059 if (gain < RECORD_GAIN_MIN)
6060 gain = RECORD_GAIN_MIN;
6061 else if (gain > RECORD_GAIN_MAX)
6062 gain = RECORD_GAIN_MAX;
6063 ctl_value = (int)(RECORD_VOLUME_CTL_MAX * gain);
6064
6065 mixer_ctl_set_value(ctl, 0, ctl_value);
6066
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006067 return 0;
6068}
6069
6070static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
6071 size_t bytes)
6072{
6073 struct stream_in *in = (struct stream_in *)stream;
Pallavid7c7a272018-01-16 11:22:55 +05306074
6075 if (in == NULL) {
6076 ALOGE("%s: stream_in ptr is NULL", __func__);
6077 return -EINVAL;
6078 }
6079
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006080 struct audio_device *adev = in->dev;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05306081 int ret = -1;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05306082 size_t bytes_read = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006083
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006084 lock_input_stream(in);
Naresh Tanniru4c630392014-05-12 01:05:52 +05306085
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07006086 if (in->is_st_session) {
6087 ALOGVV(" %s: reading on st session bytes=%zu", __func__, bytes);
6088 /* Read from sound trigger HAL */
6089 audio_extn_sound_trigger_read(in, buffer, bytes);
Quinn Malef6050362019-01-30 15:55:40 -08006090 if (in->standby) {
6091 adev->num_va_sessions++;
6092 in->standby = 0;
6093 }
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07006094 pthread_mutex_unlock(&in->lock);
6095 return bytes;
6096 }
6097
Haynes Mathew George16081042017-05-31 17:16:49 -07006098 if (in->usecase == USECASE_AUDIO_RECORD_MMAP) {
6099 ret = -ENOSYS;
6100 goto exit;
6101 }
6102
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006103 if (in->standby) {
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07006104 pthread_mutex_lock(&adev->lock);
6105 if (in->usecase == USECASE_COMPRESS_VOIP_CALL)
6106 ret = voice_extn_compress_voip_start_input_stream(in);
6107 else
6108 ret = start_input_stream(in);
Quinn Malef6050362019-01-30 15:55:40 -08006109 if (!ret && in->source == AUDIO_SOURCE_VOICE_RECOGNITION)
6110 adev->num_va_sessions++;
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07006111 pthread_mutex_unlock(&adev->lock);
6112 if (ret != 0) {
6113 goto exit;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006114 }
6115 in->standby = 0;
6116 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006117
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006118 // what's the duration requested by the client?
6119 long ns = 0;
6120
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05306121 if (in->pcm && in->config.rate)
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006122 ns = pcm_bytes_to_frames(in->pcm, bytes)*1000000000LL/
6123 in->config.rate;
6124
6125 request_in_focus(in, ns);
6126 bool use_mmap = is_mmap_usecase(in->usecase) || in->realtime;
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07006127
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05306128 if (audio_extn_cin_attached_usecase(in->usecase)) {
6129 ret = audio_extn_cin_read(in, buffer, bytes, &bytes_read);
6130 } else if (in->pcm) {
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05306131 if (audio_extn_ssr_get_stream() == in) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07006132 ret = audio_extn_ssr_read(stream, buffer, bytes);
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05306133 } else if (audio_extn_compr_cap_usecase_supported(in->usecase)) {
Mingming Yine62d7842013-10-25 16:26:03 -07006134 ret = audio_extn_compr_cap_read(in, buffer, bytes);
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006135 } else if (use_mmap) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07006136 ret = pcm_mmap_read(in->pcm, buffer, bytes);
Garmond Leunge2433c32017-09-28 21:51:22 -07006137 } else if (audio_extn_ffv_get_stream() == in) {
6138 ret = audio_extn_ffv_read(stream, buffer, bytes);
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05306139 } else {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07006140 ret = pcm_read(in->pcm, buffer, bytes);
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05306141 /* data from DSP comes in 24_8 format, convert it to 8_24 */
6142 if (!ret && bytes > 0 && (in->format == AUDIO_FORMAT_PCM_8_24_BIT)) {
6143 if (audio_extn_utils_convert_format_24_8_to_8_24(buffer, bytes)
6144 != bytes) {
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05306145 ret = -EINVAL;
6146 goto exit;
6147 }
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05306148 } else if (ret < 0) {
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05306149 ret = -errno;
6150 }
6151 }
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05306152 /* bytes read is always set to bytes for non compress usecases */
6153 bytes_read = bytes;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006154 }
6155
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006156 release_in_focus(in);
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07006157
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006158 /*
Quinn Malef6050362019-01-30 15:55:40 -08006159 * Instead of writing zeroes here, we could trust the hardware to always
6160 * provide zeroes when muted. This is also muted with voice recognition
6161 * usecases so that other clients do not have access to voice recognition
6162 * data.
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006163 */
Quinn Malef6050362019-01-30 15:55:40 -08006164 if ((ret == 0 && voice_get_mic_mute(adev) &&
6165 !voice_is_in_call_rec_stream(in) &&
6166 in->usecase != USECASE_AUDIO_RECORD_AFE_PROXY) ||
6167 (adev->num_va_sessions &&
6168 in->source != AUDIO_SOURCE_VOICE_RECOGNITION &&
6169 property_get_bool("persist.vendor.audio.va_concurrency_mute_enabled",
6170 false)))
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006171 memset(buffer, 0, bytes);
6172
6173exit:
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07006174 if (-ENETRESET == ret)
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306175 in->card_status = CARD_STATUS_OFFLINE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006176 pthread_mutex_unlock(&in->lock);
6177
6178 if (ret != 0) {
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05306179 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05306180 pthread_mutex_lock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05306181 voice_extn_compress_voip_close_input_stream(&in->stream.common);
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05306182 pthread_mutex_unlock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05306183 in->standby = true;
6184 }
Sharad Sangled17c9122017-03-20 15:58:52 +05306185 if (!audio_extn_cin_attached_usecase(in->usecase)) {
6186 bytes_read = bytes;
6187 memset(buffer, 0, bytes);
6188 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006189 in_standby(&in->stream.common);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07006190 ALOGV("%s: read failed status %d- sleeping for buffer duration", __func__, ret);
Ashish Jainbbce4322016-02-16 13:25:27 +05306191 usleep((uint64_t)bytes * 1000000 / audio_stream_in_frame_size(stream) /
Naresh Tanniru4c630392014-05-12 01:05:52 +05306192 in_get_sample_rate(&in->stream.common));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006193 }
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05306194 return bytes_read;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006195}
6196
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07006197static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006198{
6199 return 0;
6200}
6201
Aalique Grahame22e49102018-12-18 14:23:57 -08006202static int in_get_capture_position(const struct audio_stream_in *stream,
6203 int64_t *frames, int64_t *time)
6204{
6205 if (stream == NULL || frames == NULL || time == NULL) {
6206 return -EINVAL;
6207 }
6208 struct stream_in *in = (struct stream_in *)stream;
6209 int ret = -ENOSYS;
6210
6211 lock_input_stream(in);
6212 // note: ST sessions do not close the alsa pcm driver synchronously
6213 // on standby. Therefore, we may return an error even though the
6214 // pcm stream is still opened.
6215 if (in->standby) {
6216 ALOGE_IF(in->pcm != NULL && !in->is_st_session,
6217 "%s stream in standby but pcm not NULL for non ST session", __func__);
6218 goto exit;
6219 }
6220 if (in->pcm) {
6221 struct timespec timestamp;
6222 unsigned int avail;
6223 if (pcm_get_htimestamp(in->pcm, &avail, &timestamp) == 0) {
6224 *frames = in->frames_read + avail;
6225 *time = timestamp.tv_sec * 1000000000LL + timestamp.tv_nsec;
6226 ret = 0;
6227 }
6228 }
6229exit:
6230 pthread_mutex_unlock(&in->lock);
6231 return ret;
6232}
6233
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07006234static int add_remove_audio_effect(const struct audio_stream *stream,
6235 effect_handle_t effect,
6236 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006237{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07006238 struct stream_in *in = (struct stream_in *)stream;
6239 int status = 0;
6240 effect_descriptor_t desc;
6241
6242 status = (*effect)->get_descriptor(effect, &desc);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07006243 ALOGV("%s: status %d in->standby %d enable:%d", __func__, status, in->standby, enable);
6244
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07006245 if (status != 0)
6246 return status;
6247
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006248 lock_input_stream(in);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07006249 pthread_mutex_lock(&in->dev->lock);
kunleizd96526c2018-04-09 11:12:32 +08006250 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
Aalique Grahame22e49102018-12-18 14:23:57 -08006251 in->source == AUDIO_SOURCE_VOICE_RECOGNITION ||
kunleizd96526c2018-04-09 11:12:32 +08006252 in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07006253 in->enable_aec != enable &&
6254 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
6255 in->enable_aec = enable;
Aalique Grahame22e49102018-12-18 14:23:57 -08006256 if (!enable)
6257 platform_set_echo_reference(in->dev, enable, AUDIO_DEVICE_NONE);
6258 if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
6259 in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) {
6260 in->dev->enable_voicerx = enable;
6261 struct audio_usecase *usecase;
6262 struct listnode *node;
6263 list_for_each(node, &in->dev->usecase_list) {
6264 usecase = node_to_item(node, struct audio_usecase, list);
6265 if (usecase->type == PCM_PLAYBACK)
6266 select_devices(in->dev, usecase->id);
6267 }
6268 }
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07006269 if (!in->standby) {
6270 if (enable_disable_effect(in->dev, EFFECT_AEC, enable) == ENOSYS)
6271 select_devices(in->dev, in->usecase);
6272 }
6273
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07006274 }
Ravi Kumar Alamanda198185e2013-11-07 15:42:19 -08006275 if (in->enable_ns != enable &&
6276 (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
6277 in->enable_ns = enable;
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07006278 if (!in->standby) {
kunleizd96526c2018-04-09 11:12:32 +08006279 if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
6280 in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) {
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07006281 if (enable_disable_effect(in->dev, EFFECT_NS, enable) == ENOSYS)
6282 select_devices(in->dev, in->usecase);
6283 } else
6284 select_devices(in->dev, in->usecase);
6285 }
Ravi Kumar Alamanda198185e2013-11-07 15:42:19 -08006286 }
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07006287 pthread_mutex_unlock(&in->dev->lock);
6288 pthread_mutex_unlock(&in->lock);
6289
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006290 return 0;
6291}
6292
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07006293static int in_add_audio_effect(const struct audio_stream *stream,
6294 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006295{
Eric Laurent994a6932013-07-17 11:51:42 -07006296 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07006297 return add_remove_audio_effect(stream, effect, true);
6298}
6299
6300static int in_remove_audio_effect(const struct audio_stream *stream,
6301 effect_handle_t effect)
6302{
Eric Laurent994a6932013-07-17 11:51:42 -07006303 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07006304 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006305}
6306
Derek Chenf939fb72018-11-13 13:34:41 -08006307streams_input_ctxt_t *in_get_stream(struct audio_device *dev,
6308 audio_io_handle_t input)
6309{
6310 struct listnode *node;
6311
6312 list_for_each(node, &dev->active_inputs_list) {
6313 streams_input_ctxt_t *in_ctxt = node_to_item(node,
6314 streams_input_ctxt_t,
6315 list);
6316 if (in_ctxt->input->capture_handle == input) {
6317 return in_ctxt;
6318 }
6319 }
6320 return NULL;
6321}
6322
6323streams_output_ctxt_t *out_get_stream(struct audio_device *dev,
6324 audio_io_handle_t output)
6325{
6326 struct listnode *node;
6327
6328 list_for_each(node, &dev->active_outputs_list) {
6329 streams_output_ctxt_t *out_ctxt = node_to_item(node,
6330 streams_output_ctxt_t,
6331 list);
6332 if (out_ctxt->output->handle == output) {
6333 return out_ctxt;
6334 }
6335 }
6336 return NULL;
6337}
6338
Haynes Mathew George16081042017-05-31 17:16:49 -07006339static int in_stop(const struct audio_stream_in* stream)
6340{
6341 struct stream_in *in = (struct stream_in *)stream;
6342 struct audio_device *adev = in->dev;
6343
6344 int ret = -ENOSYS;
6345 ALOGV("%s", __func__);
6346 pthread_mutex_lock(&adev->lock);
6347 if (in->usecase == USECASE_AUDIO_RECORD_MMAP && !in->standby &&
6348 in->capture_started && in->pcm != NULL) {
6349 pcm_stop(in->pcm);
6350 ret = stop_input_stream(in);
6351 in->capture_started = false;
6352 }
6353 pthread_mutex_unlock(&adev->lock);
6354 return ret;
6355}
6356
6357static int in_start(const struct audio_stream_in* stream)
6358{
6359 struct stream_in *in = (struct stream_in *)stream;
6360 struct audio_device *adev = in->dev;
6361 int ret = -ENOSYS;
6362
6363 ALOGV("%s in %p", __func__, in);
6364 pthread_mutex_lock(&adev->lock);
6365 if (in->usecase == USECASE_AUDIO_RECORD_MMAP && !in->standby &&
6366 !in->capture_started && in->pcm != NULL) {
6367 if (!in->capture_started) {
6368 ret = start_input_stream(in);
6369 if (ret == 0) {
6370 in->capture_started = true;
6371 }
6372 }
6373 }
6374 pthread_mutex_unlock(&adev->lock);
6375 return ret;
6376}
6377
6378static int in_create_mmap_buffer(const struct audio_stream_in *stream,
6379 int32_t min_size_frames,
6380 struct audio_mmap_buffer_info *info)
6381{
6382 struct stream_in *in = (struct stream_in *)stream;
6383 struct audio_device *adev = in->dev;
6384 int ret = 0;
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07006385 unsigned int offset1 = 0;
6386 unsigned int frames1 = 0;
Haynes Mathew George16081042017-05-31 17:16:49 -07006387 const char *step = "";
Arun Mirpuri5d170872019-03-26 13:21:31 -07006388 uint32_t mmap_size = 0;
6389 uint32_t buffer_size = 0;
Haynes Mathew George16081042017-05-31 17:16:49 -07006390
6391 pthread_mutex_lock(&adev->lock);
6392 ALOGV("%s in %p", __func__, in);
6393
Sharad Sanglec6f32552018-05-04 16:15:38 +05306394 if (CARD_STATUS_OFFLINE == in->card_status||
6395 CARD_STATUS_OFFLINE == adev->card_status) {
6396 ALOGW("in->card_status or adev->card_status offline, try again");
6397 ret = -EIO;
6398 goto exit;
6399 }
6400
Haynes Mathew George16081042017-05-31 17:16:49 -07006401 if (info == NULL || min_size_frames == 0) {
6402 ALOGE("%s invalid argument info %p min_size_frames %d", __func__, info, min_size_frames);
6403 ret = -EINVAL;
6404 goto exit;
6405 }
6406 if (in->usecase != USECASE_AUDIO_RECORD_MMAP || !in->standby) {
6407 ALOGE("%s: usecase = %d, standby = %d", __func__, in->usecase, in->standby);
6408 ALOGV("%s in %p", __func__, in);
6409 ret = -ENOSYS;
6410 goto exit;
6411 }
6412 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
6413 if (in->pcm_device_id < 0) {
6414 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
6415 __func__, in->pcm_device_id, in->usecase);
6416 ret = -EINVAL;
6417 goto exit;
6418 }
6419
6420 adjust_mmap_period_count(&in->config, min_size_frames);
6421
6422 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
6423 __func__, adev->snd_card, in->pcm_device_id, in->config.channels);
6424 in->pcm = pcm_open(adev->snd_card, in->pcm_device_id,
6425 (PCM_IN | PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC), &in->config);
Satish Babu Patakokila54ce83d2018-07-06 18:00:37 +05306426 if (errno == ENETRESET && !pcm_is_ready(in->pcm)) {
Sharad Sanglec6f32552018-05-04 16:15:38 +05306427 ALOGE("%s: pcm_open failed errno:%d\n", __func__, errno);
6428 in->card_status = CARD_STATUS_OFFLINE;
6429 adev->card_status = CARD_STATUS_OFFLINE;
6430 ret = -EIO;
6431 goto exit;
6432 }
6433
Haynes Mathew George16081042017-05-31 17:16:49 -07006434 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) {
6435 step = "open";
6436 ret = -ENODEV;
6437 goto exit;
6438 }
6439
6440 ret = pcm_mmap_begin(in->pcm, &info->shared_memory_address, &offset1, &frames1);
6441 if (ret < 0) {
6442 step = "begin";
6443 goto exit;
6444 }
Haynes Mathew George16081042017-05-31 17:16:49 -07006445
Arun Mirpuri5d170872019-03-26 13:21:31 -07006446 info->buffer_size_frames = pcm_get_buffer_size(in->pcm);
6447 buffer_size = pcm_frames_to_bytes(in->pcm, info->buffer_size_frames);
6448 info->burst_size_frames = in->config.period_size;
6449 ret = platform_get_mmap_data_fd(adev->platform,
6450 in->pcm_device_id, 1 /*capture*/,
6451 &info->shared_memory_fd,
6452 &mmap_size);
6453 if (ret < 0) {
6454 // Fall back to non exclusive mode
6455 info->shared_memory_fd = pcm_get_poll_fd(in->pcm);
6456 } else {
6457 if (mmap_size < buffer_size) {
6458 step = "mmap";
6459 goto exit;
6460 }
6461 // FIXME: indicate exclusive mode support by returning a negative buffer size
6462 info->buffer_size_frames *= -1;
6463 }
6464
6465 memset(info->shared_memory_address, 0, buffer_size);
Haynes Mathew George16081042017-05-31 17:16:49 -07006466
6467 ret = pcm_mmap_commit(in->pcm, 0, MMAP_PERIOD_SIZE);
6468 if (ret < 0) {
6469 step = "commit";
6470 goto exit;
6471 }
6472
6473 in->standby = false;
6474 ret = 0;
6475
6476 ALOGV("%s: got mmap buffer address %p info->buffer_size_frames %d",
6477 __func__, info->shared_memory_address, info->buffer_size_frames);
6478
6479exit:
6480 if (ret != 0) {
6481 if (in->pcm == NULL) {
6482 ALOGE("%s: %s - %d", __func__, step, ret);
6483 } else {
6484 ALOGE("%s: %s %s", __func__, step, pcm_get_error(in->pcm));
6485 pcm_close(in->pcm);
6486 in->pcm = NULL;
6487 }
6488 }
6489 pthread_mutex_unlock(&adev->lock);
6490 return ret;
6491}
6492
6493static int in_get_mmap_position(const struct audio_stream_in *stream,
6494 struct audio_mmap_position *position)
6495{
6496 struct stream_in *in = (struct stream_in *)stream;
6497 ALOGVV("%s", __func__);
6498 if (position == NULL) {
6499 return -EINVAL;
6500 }
6501 if (in->usecase != USECASE_AUDIO_RECORD_MMAP) {
6502 return -ENOSYS;
6503 }
6504 if (in->pcm == NULL) {
6505 return -ENOSYS;
6506 }
6507 struct timespec ts = { 0, 0 };
6508 int ret = pcm_mmap_get_hw_ptr(in->pcm, (unsigned int *)&position->position_frames, &ts);
6509 if (ret < 0) {
6510 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
6511 return ret;
6512 }
Naresh Tanniru119d74a2018-11-02 09:49:44 +05306513 position->time_nanoseconds = ts.tv_sec*1000000000LL + ts.tv_nsec;
Haynes Mathew George16081042017-05-31 17:16:49 -07006514 return 0;
6515}
6516
Naresh Tannirudcb47c52018-06-25 16:23:32 +05306517static int in_get_active_microphones(const struct audio_stream_in *stream,
6518 struct audio_microphone_characteristic_t *mic_array,
6519 size_t *mic_count) {
6520 struct stream_in *in = (struct stream_in *)stream;
6521 struct audio_device *adev = in->dev;
6522 ALOGVV("%s", __func__);
6523
6524 lock_input_stream(in);
6525 pthread_mutex_lock(&adev->lock);
6526 int ret = platform_get_active_microphones(adev->platform,
6527 audio_channel_count_from_in_mask(in->channel_mask),
6528 in->usecase, mic_array, mic_count);
6529 pthread_mutex_unlock(&adev->lock);
6530 pthread_mutex_unlock(&in->lock);
6531
6532 return ret;
6533}
6534
6535static int adev_get_microphones(const struct audio_hw_device *dev,
6536 struct audio_microphone_characteristic_t *mic_array,
6537 size_t *mic_count) {
6538 struct audio_device *adev = (struct audio_device *)dev;
6539 ALOGVV("%s", __func__);
6540
6541 pthread_mutex_lock(&adev->lock);
6542 int ret = platform_get_microphones(adev->platform, mic_array, mic_count);
6543 pthread_mutex_unlock(&adev->lock);
6544
6545 return ret;
6546}
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05306547int adev_open_output_stream(struct audio_hw_device *dev,
Haynes Mathew George16081042017-05-31 17:16:49 -07006548 audio_io_handle_t handle,
6549 audio_devices_t devices,
6550 audio_output_flags_t flags,
6551 struct audio_config *config,
6552 struct audio_stream_out **stream_out,
6553 const char *address __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006554{
6555 struct audio_device *adev = (struct audio_device *)dev;
6556 struct stream_out *out;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05306557 int ret = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07006558 audio_format_t format;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08006559 struct adsp_hdlr_stream_cfg hdlr_stream_cfg;
Manish Dewangan21a850a2017-08-14 12:03:55 +05306560 bool is_direct_passthough = false;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07006561 bool is_hdmi = devices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
6562 bool is_usb_dev = audio_is_usb_out_device(devices) &&
6563 (devices != AUDIO_DEVICE_OUT_USB_ACCESSORY);
6564 bool direct_dev = is_hdmi || is_usb_dev;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08006565 bool use_db_as_primary =
Aalique Grahame6e763712019-01-31 16:18:17 -08006566 audio_feature_manager_is_feature_enabled(USE_DEEP_BUFFER_AS_PRIMARY_OUTPUT);
Vignesh Kulothungana6927272019-02-20 15:17:07 -08006567 bool force_haptic_path =
6568 property_get_bool("vendor.audio.test_haptic", false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006569
kunleizdff872d2018-08-20 14:40:33 +08006570 if (is_usb_dev && (!audio_extn_usb_connected(NULL))) {
kunleizd6a9e0c2018-07-30 15:38:52 +08006571 is_usb_dev = false;
6572 devices = AUDIO_DEVICE_OUT_SPEAKER;
6573 ALOGW("%s: ignore set device to non existing USB card, use output device(%#x)",
6574 __func__, devices);
6575 }
6576
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006577 *stream_out = NULL;
Naresh Tanniru80659832014-06-04 18:17:56 +05306578
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006579 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
6580
Mingming Yin3a941d42016-02-17 18:08:05 -08006581 ALOGD("%s: enter: format(%#x) sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)\
6582 stream_handle(%p)", __func__, config->format, config->sample_rate, config->channel_mask,
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05306583 devices, flags, &out->stream);
6584
6585
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08006586 if (!out) {
6587 return -ENOMEM;
6588 }
6589
Haynes Mathew George204045b2015-02-25 20:32:03 -08006590 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006591 pthread_mutex_init(&out->pre_lock, (const pthread_mutexattr_t *) NULL);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05306592 pthread_mutex_init(&out->compr_mute_lock, (const pthread_mutexattr_t *) NULL);
Zhou Song48453a02018-01-10 17:50:59 +08006593 pthread_mutex_init(&out->position_query_lock, (const pthread_mutexattr_t *) NULL);
Haynes Mathew George204045b2015-02-25 20:32:03 -08006594 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
6595
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006596 if (devices == AUDIO_DEVICE_NONE)
6597 devices = AUDIO_DEVICE_OUT_SPEAKER;
6598
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006599 out->flags = flags;
6600 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07006601 out->dev = adev;
Aalique Grahame65780b52017-09-27 14:59:56 -07006602 out->hal_op_format = out->hal_ip_format = format = out->format = config->format;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006603 out->sample_rate = config->sample_rate;
Sachin Mohan Gadag3d09acd2017-06-19 12:43:44 +05306604 out->channel_mask = config->channel_mask;
Ramjee Singh5857aeb2017-08-03 19:18:50 +05306605 if (out->channel_mask == AUDIO_CHANNEL_NONE)
6606 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
6607 else
6608 out->supported_channel_masks[0] = out->channel_mask;
Eric Laurentc4aef752013-09-12 17:45:53 -07006609 out->handle = handle;
Mingming Yin3ee55c62014-08-04 14:23:35 -07006610 out->bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Alexy Josephaa54c872014-12-03 02:46:47 -08006611 out->non_blocking = 0;
Ashish Jain83a6cc22016-06-28 14:34:17 +05306612 out->convert_buffer = NULL;
Ashish Jain1b9b30c2017-05-18 20:57:40 +05306613 out->started = 0;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05306614 out->a2dp_compress_mute = false;
Aniket Kumar Lata932f4872017-11-06 18:29:44 -08006615 out->hal_output_suspend_supported = 0;
6616 out->dynamic_pm_qos_config_supported = 0;
Surendar Karkaf51b5842018-04-26 11:28:38 +05306617 out->set_dual_mono = false;
Manisha Agarwal7b3e3772019-02-20 14:33:45 +05306618 out->prev_card_status_offline = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006619
Nikhil Laturkar26b690b2017-07-25 11:06:14 +05306620 if ((flags & AUDIO_OUTPUT_FLAG_BD) &&
Satish Babu Patakokila37e7c482018-02-02 11:50:06 +05306621 (property_get_bool("vendor.audio.matrix.limiter.enable", false)))
Ben Romberger6c4d3812017-06-13 17:46:45 -07006622 platform_set_device_params(out, DEVICE_PARAM_LIMITER_ID, 1);
6623
Aalique Grahame22e49102018-12-18 14:23:57 -08006624 if (direct_dev &&
6625 (audio_is_linear_pcm(out->format) ||
6626 config->format == AUDIO_FORMAT_DEFAULT) &&
6627 out->flags == AUDIO_OUTPUT_FLAG_NONE) {
6628 audio_format_t req_format = config->format;
6629 audio_channel_mask_t req_channel_mask = config->channel_mask;
6630 uint32_t req_sample_rate = config->sample_rate;
6631
6632 pthread_mutex_lock(&adev->lock);
6633 if (is_hdmi) {
6634 ALOGV("AUDIO_DEVICE_OUT_AUX_DIGITAL and DIRECT|OFFLOAD, check hdmi caps");
6635 ret = read_hdmi_sink_caps(out);
6636 if (config->sample_rate == 0)
6637 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
6638 if (config->channel_mask == AUDIO_CHANNEL_NONE)
6639 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
6640 if (config->format == AUDIO_FORMAT_DEFAULT)
6641 config->format = AUDIO_FORMAT_PCM_16_BIT;
6642 } else if (is_usb_dev) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07006643 ret = read_usb_sup_params_and_compare(true /*is_playback*/,
6644 &config->format,
6645 &out->supported_formats[0],
6646 MAX_SUPPORTED_FORMATS,
6647 &config->channel_mask,
6648 &out->supported_channel_masks[0],
6649 MAX_SUPPORTED_CHANNEL_MASKS,
6650 &config->sample_rate,
6651 &out->supported_sample_rates[0],
6652 MAX_SUPPORTED_SAMPLE_RATES);
6653 ALOGV("plugged dev USB ret %d", ret);
Haynes Mathew George484e8d22017-07-31 18:55:17 -07006654 }
Aalique Grahame22e49102018-12-18 14:23:57 -08006655
Haynes Mathew George484e8d22017-07-31 18:55:17 -07006656 pthread_mutex_unlock(&adev->lock);
6657 if (ret != 0) {
Mingming Yin3a941d42016-02-17 18:08:05 -08006658 if (ret == -ENOSYS) {
6659 /* ignore and go with default */
6660 ret = 0;
Aalique Grahame22e49102018-12-18 14:23:57 -08006661 }
6662 // For MMAP NO IRQ, allow conversions in ADSP
6663 else if (is_hdmi || (flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) == 0)
6664 goto error_open;
6665 else {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07006666 ALOGE("error reading direct dev sink caps");
Mingming Yin3a941d42016-02-17 18:08:05 -08006667 goto error_open;
6668 }
Aalique Grahame22e49102018-12-18 14:23:57 -08006669
6670 if (req_sample_rate != 0 && config->sample_rate != req_sample_rate)
6671 config->sample_rate = req_sample_rate;
6672 if (req_channel_mask != AUDIO_CHANNEL_NONE && config->channel_mask != req_channel_mask)
6673 config->channel_mask = req_channel_mask;
6674 if (req_format != AUDIO_FORMAT_DEFAULT && config->format != req_format)
6675 config->format = req_format;
Mingming Yin3a941d42016-02-17 18:08:05 -08006676 }
Aalique Grahame22e49102018-12-18 14:23:57 -08006677
6678 out->sample_rate = config->sample_rate;
6679 out->channel_mask = config->channel_mask;
6680 out->format = config->format;
6681 if (is_hdmi) {
6682 out->usecase = USECASE_AUDIO_PLAYBACK_HIFI;
6683 out->config = pcm_config_hdmi_multi;
6684 } else if (flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) {
6685 out->usecase = USECASE_AUDIO_PLAYBACK_MMAP;
6686 out->config = pcm_config_mmap_playback;
6687 out->stream.start = out_start;
6688 out->stream.stop = out_stop;
6689 out->stream.create_mmap_buffer = out_create_mmap_buffer;
6690 out->stream.get_mmap_position = out_get_mmap_position;
6691 } else {
6692 out->usecase = USECASE_AUDIO_PLAYBACK_HIFI;
6693 out->config = pcm_config_hifi;
6694 }
6695
6696 out->config.rate = out->sample_rate;
6697 out->config.channels = audio_channel_count_from_out_mask(out->channel_mask);
6698 if (is_hdmi) {
6699 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels *
6700 audio_bytes_per_sample(out->format));
6701 }
6702 out->config.format = pcm_format_from_audio_format(out->format);
Mingming Yin3a941d42016-02-17 18:08:05 -08006703 }
6704
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08006705 /* Check for VOIP usecase */
6706 if(out->flags == (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_VOIP_RX)) {
6707 if (!voice_extn_is_compress_voip_supported()) {
6708 if (out->sample_rate == 8000 || out->sample_rate == 16000 ||
6709 out->sample_rate == 32000 || out->sample_rate == 48000) {
kunleizf6b5b082019-03-18 17:32:25 +08006710 out->channel_mask = AUDIO_CHANNEL_OUT_MONO;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08006711 out->usecase = USECASE_AUDIO_PLAYBACK_VOIP;
6712 out->format = AUDIO_FORMAT_PCM_16_BIT;
Vikram Panduranga93f080e2017-06-07 18:16:14 -07006713
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08006714 out->config = default_pcm_config_voip_copp;
6715 out->config.period_size = VOIP_IO_BUF_SIZE(out->sample_rate, DEFAULT_VOIP_BUF_DURATION_MS, DEFAULT_VOIP_BIT_DEPTH_BYTE)/2;
6716 out->config.rate = out->sample_rate;
6717 }
6718 } else {
6719 if ((out->dev->mode == AUDIO_MODE_IN_COMMUNICATION ||
6720 voice_extn_compress_voip_is_active(out->dev)) &&
6721 (voice_extn_compress_voip_is_config_supported(config))) {
6722 ret = voice_extn_compress_voip_open_output_stream(out);
6723 if (ret != 0) {
6724 ALOGE("%s: Compress voip output cannot be opened, error:%d",
6725 __func__, ret);
6726 goto error_open;
6727 }
6728 }
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08006729 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07006730 } else if (audio_is_linear_pcm(out->format) &&
6731 out->flags == AUDIO_OUTPUT_FLAG_NONE && is_usb_dev) {
6732 out->channel_mask = config->channel_mask;
6733 out->sample_rate = config->sample_rate;
6734 out->format = config->format;
6735 out->usecase = USECASE_AUDIO_PLAYBACK_HIFI;
6736 // does this change?
6737 out->config = is_hdmi ? pcm_config_hdmi_multi : pcm_config_hifi;
6738 out->config.rate = config->sample_rate;
6739 out->config.channels = audio_channel_count_from_out_mask(out->channel_mask);
6740 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels *
6741 audio_bytes_per_sample(config->format));
6742 out->config.format = pcm_format_from_audio_format(out->format);
vivek mehta0ea887a2015-08-26 14:01:20 -07006743 } else if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
Dhananjay Kumarac341582017-02-23 23:42:25 +05306744 (out->flags == AUDIO_OUTPUT_FLAG_DIRECT)) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306745 pthread_mutex_lock(&adev->lock);
6746 bool offline = (adev->card_status == CARD_STATUS_OFFLINE);
6747 pthread_mutex_unlock(&adev->lock);
6748
6749 // reject offload during card offline to allow
6750 // fallback to s/w paths
6751 if (offline) {
6752 ret = -ENODEV;
6753 goto error_open;
6754 }
vivek mehta0ea887a2015-08-26 14:01:20 -07006755
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006756 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
6757 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
6758 ALOGE("%s: Unsupported Offload information", __func__);
6759 ret = -EINVAL;
6760 goto error_open;
6761 }
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07006762
Atul Khare3fa6e542017-08-09 00:56:17 +05306763 if (config->offload_info.format == 0)
6764 config->offload_info.format = config->format;
6765 if (config->offload_info.sample_rate == 0)
6766 config->offload_info.sample_rate = config->sample_rate;
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07006767
Mingming Yin90310102013-11-13 16:57:00 -08006768 if (!is_supported_format(config->offload_info.format) &&
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05306769 !audio_extn_passthru_is_supported_format(config->offload_info.format)) {
vivek mehta0ea887a2015-08-26 14:01:20 -07006770 ALOGE("%s: Unsupported audio format %x " , __func__, config->offload_info.format);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006771 ret = -EINVAL;
6772 goto error_open;
6773 }
6774
Ben Romberger0f8c87b2017-05-24 17:41:11 -07006775 /* TrueHD only supported for 48k multiples (48k, 96k, 192k) */
6776 if ((config->offload_info.format == AUDIO_FORMAT_DOLBY_TRUEHD) &&
6777 (audio_extn_passthru_is_passthrough_stream(out)) &&
6778 !((config->sample_rate == 48000) ||
6779 (config->sample_rate == 96000) ||
6780 (config->sample_rate == 192000))) {
6781 ALOGE("%s: Unsupported sample rate %d for audio format %x",
6782 __func__, config->sample_rate, config->offload_info.format);
6783 ret = -EINVAL;
6784 goto error_open;
6785 }
6786
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006787 out->compr_config.codec = (struct snd_codec *)
6788 calloc(1, sizeof(struct snd_codec));
6789
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07006790 if (!out->compr_config.codec) {
6791 ret = -ENOMEM;
6792 goto error_open;
6793 }
6794
Dhananjay Kumarac341582017-02-23 23:42:25 +05306795 out->stream.pause = out_pause;
6796 out->stream.resume = out_resume;
6797 out->stream.flush = out_flush;
Ashish Jain4847e9d2017-08-17 19:16:57 +05306798 out->stream.set_callback = out_set_callback;
Dhananjay Kumarac341582017-02-23 23:42:25 +05306799 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Mingming Yin21d60472015-09-30 13:56:25 -07006800 out->stream.drain = out_drain;
Dhananjay Kumarac341582017-02-23 23:42:25 +05306801 out->usecase = get_offload_usecase(adev, true /* is_compress */);
vivek mehta446c3962015-09-14 10:57:35 -07006802 ALOGV("Compress Offload usecase .. usecase selected %d", out->usecase);
Dhananjay Kumarac341582017-02-23 23:42:25 +05306803 } else {
6804 out->usecase = get_offload_usecase(adev, false /* is_compress */);
6805 ALOGV("non-offload DIRECT_usecase ... usecase selected %d ", out->usecase);
vivek mehta0ea887a2015-08-26 14:01:20 -07006806 }
vivek mehta446c3962015-09-14 10:57:35 -07006807
6808 if (out->usecase == USECASE_INVALID) {
Mingming Yin3a941d42016-02-17 18:08:05 -08006809 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL &&
6810 config->format == 0 && config->sample_rate == 0 &&
6811 config->channel_mask == 0) {
Mingming Yin21854652016-04-13 11:54:02 -07006812 ALOGI("%s dummy open to query sink capability",__func__);
Mingming Yin3a941d42016-02-17 18:08:05 -08006813 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
6814 } else {
6815 ALOGE("%s, Max allowed OFFLOAD usecase reached ... ", __func__);
6816 ret = -EEXIST;
6817 goto error_open;
6818 }
vivek mehta446c3962015-09-14 10:57:35 -07006819 }
6820
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006821 if (config->offload_info.channel_mask)
6822 out->channel_mask = config->offload_info.channel_mask;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08006823 else if (config->channel_mask) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006824 out->channel_mask = config->channel_mask;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08006825 config->offload_info.channel_mask = config->channel_mask;
Haynes Mathew Georgea99f7532016-08-24 16:01:21 -07006826 } else {
Dhananjay Kumarac341582017-02-23 23:42:25 +05306827 ALOGE("out->channel_mask not set for OFFLOAD/DIRECT usecase");
Haynes Mathew Georgea99f7532016-08-24 16:01:21 -07006828 ret = -EINVAL;
6829 goto error_open;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08006830 }
Haynes Mathew Georgea99f7532016-08-24 16:01:21 -07006831
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07006832 format = out->format = config->offload_info.format;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006833 out->sample_rate = config->offload_info.sample_rate;
6834
Mingming Yin3ee55c62014-08-04 14:23:35 -07006835 out->bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006836
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05306837 out->compr_config.codec->id = get_snd_codec_id(config->offload_info.format);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05306838 if (audio_extn_utils_is_dolby_format(config->offload_info.format)) {
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05306839 audio_extn_dolby_send_ddp_endp_params(adev);
6840 audio_extn_dolby_set_dmid(adev);
6841 }
vivek mehta0ea887a2015-08-26 14:01:20 -07006842
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006843 out->compr_config.codec->sample_rate =
Ravi Kumar Alamandab91bff32014-11-14 12:05:54 -08006844 config->offload_info.sample_rate;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006845 out->compr_config.codec->bit_rate =
6846 config->offload_info.bit_rate;
6847 out->compr_config.codec->ch_in =
Dhanalakshmi Siddania15c6792016-08-10 15:33:53 +05306848 audio_channel_count_from_out_mask(out->channel_mask);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006849 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
Satish Babu Patakokilaa395a9e2016-11-01 12:18:49 +05306850 /* Update bit width only for non passthrough usecases.
6851 * For passthrough usecases, the output will always be opened @16 bit
6852 */
6853 if (!audio_extn_passthru_is_passthrough_stream(out))
6854 out->bit_width = AUDIO_OUTPUT_BIT_WIDTH;
Naresh Tanniruee3499a2017-01-05 14:05:35 +05306855
6856 if (out->flags & AUDIO_OUTPUT_FLAG_TIMESTAMP)
6857 out->compr_config.codec->flags |= COMPRESSED_TIMESTAMP_FLAG;
6858 ALOGVV("%s : out->compr_config.codec->flags -> (%#x) ", __func__, out->compr_config.codec->flags);
6859
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07006860 /*TODO: Do we need to change it for passthrough */
6861 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_RAW;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006862
Manish Dewangana6fc5442015-08-24 20:30:31 +05306863 if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC)
6864 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_RAW;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05306865 else if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS)
Manish Dewangana6fc5442015-08-24 20:30:31 +05306866 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_MP4ADTS;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05306867 else if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_LATM)
6868 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_MP4LATM;
Ashish Jainf1eaa582016-05-23 20:54:24 +05306869
6870 if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) ==
6871 AUDIO_FORMAT_PCM) {
6872
6873 /*Based on platform support, configure appropriate alsa format for corresponding
6874 *hal input format.
6875 */
6876 out->compr_config.codec->format = hal_format_to_alsa(
6877 config->offload_info.format);
6878
Ashish Jain83a6cc22016-06-28 14:34:17 +05306879 out->hal_op_format = alsa_format_to_hal(
Ashish Jainf1eaa582016-05-23 20:54:24 +05306880 out->compr_config.codec->format);
Ashish Jain83a6cc22016-06-28 14:34:17 +05306881 out->hal_ip_format = out->format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05306882
Dhananjay Kumarac341582017-02-23 23:42:25 +05306883 /*for direct non-compress playback populate bit_width based on selected alsa format as
Ashish Jainf1eaa582016-05-23 20:54:24 +05306884 *hal input format and alsa format might differ based on platform support.
6885 */
6886 out->bit_width = audio_bytes_per_sample(
Ashish Jain83a6cc22016-06-28 14:34:17 +05306887 out->hal_op_format) << 3;
Ashish Jainf1eaa582016-05-23 20:54:24 +05306888
6889 out->compr_config.fragments = DIRECT_PCM_NUM_FRAGMENTS;
6890
6891 /* Check if alsa session is configured with the same format as HAL input format,
6892 * if not then derive correct fragment size needed to accomodate the
6893 * conversion of HAL input format to alsa format.
6894 */
6895 audio_extn_utils_update_direct_pcm_fragment_size(out);
6896
6897 /*if hal input and output fragment size is different this indicates HAL input format is
6898 *not same as the alsa format
6899 */
Ashish Jain83a6cc22016-06-28 14:34:17 +05306900 if (out->hal_fragment_size != out->compr_config.fragment_size) {
Ashish Jainf1eaa582016-05-23 20:54:24 +05306901 /*Allocate a buffer to convert input data to the alsa configured format.
6902 *size of convert buffer is equal to the size required to hold one fragment size
6903 *worth of pcm data, this is because flinger does not write more than fragment_size
6904 */
Ashish Jain83a6cc22016-06-28 14:34:17 +05306905 out->convert_buffer = calloc(1,out->compr_config.fragment_size);
6906 if (out->convert_buffer == NULL){
Ashish Jainf1eaa582016-05-23 20:54:24 +05306907 ALOGE("Allocation failed for convert buffer for size %d", out->compr_config.fragment_size);
6908 ret = -ENOMEM;
6909 goto error_open;
6910 }
6911 }
6912 } else if (audio_extn_passthru_is_passthrough_stream(out)) {
6913 out->compr_config.fragment_size =
6914 audio_extn_passthru_get_buffer_size(&config->offload_info);
6915 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
6916 } else {
6917 out->compr_config.fragment_size =
6918 platform_get_compress_offload_buffer_size(&config->offload_info);
6919 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
6920 }
Mingming Yin3ee55c62014-08-04 14:23:35 -07006921
Naresh Tanniruee3499a2017-01-05 14:05:35 +05306922 if (out->flags & AUDIO_OUTPUT_FLAG_TIMESTAMP) {
6923 out->compr_config.fragment_size += sizeof(struct snd_codec_metadata);
6924 }
Amit Shekhar6f461b12014-08-01 14:52:58 -07006925 if (config->offload_info.format == AUDIO_FORMAT_FLAC)
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +05306926 out->compr_config.codec->options.flac_dec.sample_size = AUDIO_OUTPUT_BIT_WIDTH;
Mingming Yin3ee55c62014-08-04 14:23:35 -07006927
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05306928 if (config->offload_info.format == AUDIO_FORMAT_APTX) {
6929 audio_extn_send_aptx_dec_bt_addr_to_dsp(out);
6930 }
6931
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006932 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
6933 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07006934
Manish Dewangan69426c82017-01-30 17:35:36 +05306935 if ((flags & AUDIO_OUTPUT_FLAG_TIMESTAMP) &&
6936 (flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC)) {
6937 out->render_mode = RENDER_MODE_AUDIO_STC_MASTER;
6938 } else if(flags & AUDIO_OUTPUT_FLAG_TIMESTAMP) {
6939 out->render_mode = RENDER_MODE_AUDIO_MASTER;
6940 } else {
6941 out->render_mode = RENDER_MODE_AUDIO_NO_TIMESTAMP;
6942 }
Alexy Josephaa54c872014-12-03 02:46:47 -08006943
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05306944 memset(&out->channel_map_param, 0,
6945 sizeof(struct audio_out_channel_map_param));
6946
Haynes Mathew George352f27b2013-07-26 00:00:15 -07006947 out->send_new_metadata = 1;
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05306948 out->send_next_track_params = false;
6949 out->is_compr_metadata_avail = false;
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08006950 out->offload_state = OFFLOAD_STATE_IDLE;
6951 out->playback_started = 0;
Zhou Song48453a02018-01-10 17:50:59 +08006952 out->writeAt.tv_sec = 0;
6953 out->writeAt.tv_nsec = 0;
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08006954
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08006955 audio_extn_dts_create_state_notifier_node(out->usecase);
6956
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006957 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
6958 __func__, config->offload_info.version,
6959 config->offload_info.bit_rate);
Ashish Jain5106d362016-05-11 19:23:33 +05306960
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05306961 /* Check if DSD audio format is supported in codec
6962 * and there is no active native DSD use case
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05306963 */
6964
6965 if ((config->format == AUDIO_FORMAT_DSD) &&
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05306966 (!platform_check_codec_dsd_support(adev->platform) ||
6967 audio_is_dsd_native_stream_active(adev))) {
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05306968 ret = -EINVAL;
6969 goto error_open;
6970 }
6971
Ashish Jain5106d362016-05-11 19:23:33 +05306972 /* Disable gapless if any of the following is true
6973 * passthrough playback
6974 * AV playback
Dhananjay Kumarac341582017-02-23 23:42:25 +05306975 * non compressed Direct playback
Ashish Jain5106d362016-05-11 19:23:33 +05306976 */
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05306977 if (audio_extn_passthru_is_passthrough_stream(out) ||
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05306978 (config->format == AUDIO_FORMAT_DSD) ||
Naresh Tanniru928f0862017-04-07 16:44:23 -07006979 (config->format == AUDIO_FORMAT_IEC61937) ||
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05306980 config->offload_info.has_video ||
Dhananjay Kumarac341582017-02-23 23:42:25 +05306981 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Ashish Jain5106d362016-05-11 19:23:33 +05306982 check_and_set_gapless_mode(adev, false);
6983 } else
6984 check_and_set_gapless_mode(adev, true);
Mingming Yin21854652016-04-13 11:54:02 -07006985
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05306986 if (audio_extn_passthru_is_passthrough_stream(out)) {
Mingming Yin21854652016-04-13 11:54:02 -07006987 out->flags |= AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH;
6988 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05306989 if (config->format == AUDIO_FORMAT_DSD) {
6990 out->flags |= AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH;
6991 out->compr_config.codec->compr_passthr = PASSTHROUGH_DSD;
6992 }
Aalique Grahame0359a1f2016-09-08 16:54:22 -07006993
6994 create_offload_callback_thread(out);
6995
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07006996 } else if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006997 switch (config->sample_rate) {
6998 case 0:
6999 out->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
7000 break;
7001 case 8000:
7002 case 16000:
7003 case 48000:
7004 out->sample_rate = config->sample_rate;
7005 break;
7006 default:
7007 ALOGE("%s: Unsupported sampling rate %d for Incall Music", __func__,
7008 config->sample_rate);
7009 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
7010 ret = -EINVAL;
7011 goto error_open;
7012 }
7013 //FIXME: add support for MONO stream configuration when audioflinger mixer supports it
7014 switch (config->channel_mask) {
7015 case AUDIO_CHANNEL_NONE:
7016 case AUDIO_CHANNEL_OUT_STEREO:
7017 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
7018 break;
7019 default:
7020 ALOGE("%s: Unsupported channel mask %#x for Incall Music", __func__,
7021 config->channel_mask);
7022 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
7023 ret = -EINVAL;
7024 goto error_open;
7025 }
7026 switch (config->format) {
7027 case AUDIO_FORMAT_DEFAULT:
7028 case AUDIO_FORMAT_PCM_16_BIT:
7029 out->format = AUDIO_FORMAT_PCM_16_BIT;
7030 break;
7031 default:
7032 ALOGE("%s: Unsupported format %#x for Incall Music", __func__,
7033 config->format);
7034 config->format = AUDIO_FORMAT_PCM_16_BIT;
7035 ret = -EINVAL;
7036 goto error_open;
7037 }
7038
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05307039 ret = voice_extn_check_and_set_incall_music_usecase(adev, out);
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07007040 if (ret != 0) {
7041 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
Arun Mirpuri7da752a2018-09-11 18:01:15 -07007042 __func__, ret);
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07007043 goto error_open;
7044 }
Arun Mirpuri7da752a2018-09-11 18:01:15 -07007045 } else if (out->devices == AUDIO_DEVICE_OUT_TELEPHONY_TX) {
Aalique Grahame22e49102018-12-18 14:23:57 -08007046 switch (config->sample_rate) {
7047 case 0:
7048 out->sample_rate = AFE_PROXY_SAMPLING_RATE;
7049 break;
7050 case 8000:
7051 case 16000:
7052 case 48000:
7053 out->sample_rate = config->sample_rate;
7054 break;
7055 default:
7056 ALOGE("%s: Unsupported sampling rate %d for Telephony TX", __func__,
7057 config->sample_rate);
7058 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
7059 ret = -EINVAL;
7060 break;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07007061 }
Aalique Grahame22e49102018-12-18 14:23:57 -08007062 //FIXME: add support for MONO stream configuration when audioflinger mixer supports it
7063 switch (config->channel_mask) {
7064 case AUDIO_CHANNEL_NONE:
7065 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
7066 break;
7067 case AUDIO_CHANNEL_OUT_STEREO:
7068 out->channel_mask = config->channel_mask;
7069 break;
7070 default:
7071 ALOGE("%s: Unsupported channel mask %#x for Telephony TX", __func__,
7072 config->channel_mask);
7073 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
7074 ret = -EINVAL;
7075 break;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07007076 }
Aalique Grahame22e49102018-12-18 14:23:57 -08007077 switch (config->format) {
7078 case AUDIO_FORMAT_DEFAULT:
7079 out->format = AUDIO_FORMAT_PCM_16_BIT;
7080 break;
7081 case AUDIO_FORMAT_PCM_16_BIT:
7082 out->format = config->format;
7083 break;
7084 default:
7085 ALOGE("%s: Unsupported format %#x for Telephony TX", __func__,
7086 config->format);
7087 config->format = AUDIO_FORMAT_PCM_16_BIT;
7088 ret = -EINVAL;
7089 break;
7090 }
7091 if (ret != 0)
7092 goto error_open;
7093
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07007094 out->usecase = USECASE_AUDIO_PLAYBACK_AFE_PROXY;
7095 out->config = pcm_config_afe_proxy_playback;
Aalique Grahame22e49102018-12-18 14:23:57 -08007096 out->config.rate = out->sample_rate;
7097 out->config.channels =
7098 audio_channel_count_from_out_mask(out->channel_mask);
7099 out->config.format = pcm_format_from_audio_format(out->format);
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07007100 adev->voice_tx_output = out;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07007101 } else {
Ashish Jain058165c2016-09-28 23:18:48 +05307102 unsigned int channels = 0;
7103 /*Update config params to default if not set by the caller*/
7104 if (config->sample_rate == 0)
7105 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
7106 if (config->channel_mask == AUDIO_CHANNEL_NONE)
7107 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
7108 if (config->format == AUDIO_FORMAT_DEFAULT)
7109 config->format = AUDIO_FORMAT_PCM_16_BIT;
7110
7111 channels = audio_channel_count_from_out_mask(out->channel_mask);
7112
Varun Balaraje49253e2017-07-06 19:48:56 +05307113 if (out->flags & AUDIO_OUTPUT_FLAG_INTERACTIVE) {
7114 out->usecase = get_interactive_usecase(adev);
7115 out->config = pcm_config_low_latency;
7116 } else if (out->flags & AUDIO_OUTPUT_FLAG_RAW) {
Ashish Jain83a6cc22016-06-28 14:34:17 +05307117 out->usecase = USECASE_AUDIO_PLAYBACK_ULL;
Haynes Mathew George5beddd42016-06-27 18:33:40 -07007118 out->realtime = may_use_noirq_mode(adev, USECASE_AUDIO_PLAYBACK_ULL,
7119 out->flags);
7120 out->config = out->realtime ? pcm_config_rt : pcm_config_low_latency;
Haynes Mathew George16081042017-05-31 17:16:49 -07007121 } else if (out->flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) {
7122 out->usecase = USECASE_AUDIO_PLAYBACK_MMAP;
7123 out->config = pcm_config_mmap_playback;
7124 out->stream.start = out_start;
7125 out->stream.stop = out_stop;
7126 out->stream.create_mmap_buffer = out_create_mmap_buffer;
7127 out->stream.get_mmap_position = out_get_mmap_position;
Ashish Jain83a6cc22016-06-28 14:34:17 +05307128 } else if (out->flags & AUDIO_OUTPUT_FLAG_FAST) {
7129 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
Aniket Kumar Lata932f4872017-11-06 18:29:44 -08007130 out->hal_output_suspend_supported =
7131 property_get_bool("vendor.audio.hal.output.suspend.supported", false);
7132 out->dynamic_pm_qos_config_supported =
7133 property_get_bool("vendor.audio.hal.dynamic.qos.config.supported", false);
7134 if (!out->dynamic_pm_qos_config_supported) {
Alexy Joseph98988832017-01-13 14:56:59 -08007135 ALOGI("%s: dynamic qos voting not enabled for platform", __func__);
7136 } else {
7137 ALOGI("%s: dynamic qos voting enabled for platform", __func__);
7138 //the mixer path will be a string similar to "low-latency-playback resume"
7139 strlcpy(out->pm_qos_mixer_path, use_case_table[out->usecase], MAX_MIXER_PATH_LEN);
7140 strlcat(out->pm_qos_mixer_path,
7141 " resume", MAX_MIXER_PATH_LEN);
7142 ALOGI("%s: created %s pm_qos_mixer_path" , __func__,
7143 out->pm_qos_mixer_path);
7144 }
Ashish Jain83a6cc22016-06-28 14:34:17 +05307145 out->config = pcm_config_low_latency;
7146 } else if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
7147 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
7148 out->config = pcm_config_deep_buffer;
Ashish Jain058165c2016-09-28 23:18:48 +05307149 out->config.period_size = get_output_period_size(config->sample_rate, out->format,
7150 channels, DEEP_BUFFER_OUTPUT_PERIOD_DURATION);
7151 if (out->config.period_size <= 0) {
7152 ALOGE("Invalid configuration period size is not valid");
7153 ret = -EINVAL;
7154 goto error_open;
7155 }
Aalique Grahame22e49102018-12-18 14:23:57 -08007156 } else if (flags & AUDIO_OUTPUT_FLAG_TTS) {
7157 out->usecase = USECASE_AUDIO_PLAYBACK_TTS;
7158 out->config = pcm_config_deep_buffer;
Vignesh Kulothungana6927272019-02-20 15:17:07 -08007159 } else if (config->channel_mask & AUDIO_CHANNEL_HAPTIC_ALL) {
7160 out->usecase = USECASE_AUDIO_PLAYBACK_WITH_HAPTICS;
7161 out->config = pcm_config_haptics_audio;
7162 if (force_haptic_path)
7163 adev->haptics_config = pcm_config_haptics_audio;
7164 else
7165 adev->haptics_config = pcm_config_haptics;
7166
7167 out->config.channels =
7168 audio_channel_count_from_out_mask(out->channel_mask & ~AUDIO_CHANNEL_HAPTIC_ALL);
7169
7170 if (force_haptic_path) {
7171 out->config.channels = 1;
7172 adev->haptics_config.channels = 1;
7173 } else
7174 adev->haptics_config.channels = audio_channel_count_from_out_mask(out->channel_mask & AUDIO_CHANNEL_HAPTIC_ALL);
Ashish Jain83a6cc22016-06-28 14:34:17 +05307175 } else {
7176 /* primary path is the default path selected if no other outputs are available/suitable */
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08007177 out->usecase = GET_USECASE_AUDIO_PLAYBACK_PRIMARY(use_db_as_primary);
7178 out->config = GET_PCM_CONFIG_AUDIO_PLAYBACK_PRIMARY(use_db_as_primary);
Ashish Jain83a6cc22016-06-28 14:34:17 +05307179 }
7180 out->hal_ip_format = format = out->format;
7181 out->config.format = hal_format_to_pcm(out->hal_ip_format);
7182 out->hal_op_format = pcm_format_to_hal(out->config.format);
7183 out->bit_width = format_to_bitwidth_table[out->hal_op_format] << 3;
7184 out->config.rate = config->sample_rate;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07007185 out->sample_rate = out->config.rate;
Ashish Jain058165c2016-09-28 23:18:48 +05307186 out->config.channels = channels;
Ashish Jain83a6cc22016-06-28 14:34:17 +05307187 if (out->hal_ip_format != out->hal_op_format) {
7188 uint32_t buffer_size = out->config.period_size *
7189 format_to_bitwidth_table[out->hal_op_format] *
7190 out->config.channels;
7191 out->convert_buffer = calloc(1, buffer_size);
7192 if (out->convert_buffer == NULL){
7193 ALOGE("Allocation failed for convert buffer for size %d",
7194 out->compr_config.fragment_size);
7195 ret = -ENOMEM;
7196 goto error_open;
7197 }
7198 ALOGD("Convert buffer allocated of size %d", buffer_size);
7199 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007200 }
7201
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08007202 ALOGV("%s devices:%d, format:%x, out->sample_rate:%d,out->bit_width:%d out->format:%d out->flags:%x, flags: %x usecase %d",
7203 __func__, devices, format, out->sample_rate, out->bit_width, out->format, out->flags, flags, out->usecase);
Ashish Jain83a6cc22016-06-28 14:34:17 +05307204
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07007205 /* TODO remove this hardcoding and check why width is zero*/
7206 if (out->bit_width == 0)
7207 out->bit_width = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05307208 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07007209 &adev->streams_output_cfg_list,
Aalique Grahame65780b52017-09-27 14:59:56 -07007210 devices, out->flags, out->hal_op_format, out->sample_rate,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +05307211 out->bit_width, out->channel_mask, out->profile,
Manish Dewangan837dc462015-05-27 10:17:41 +05307212 &out->app_type_cfg);
Aalique Grahame6e763712019-01-31 16:18:17 -08007213 if ((out->usecase == (audio_usecase_t)(GET_USECASE_AUDIO_PLAYBACK_PRIMARY(use_db_as_primary))) ||
Haynes Mathew Georgebf143712013-12-03 13:02:53 -08007214 (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
7215 /* Ensure the default output is not selected twice */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08007216 if(adev->primary_output == NULL)
7217 adev->primary_output = out;
7218 else {
7219 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07007220 ret = -EEXIST;
7221 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08007222 }
7223 }
7224
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007225 /* Check if this usecase is already existing */
7226 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella7ce05352014-04-17 20:00:41 -07007227 if ((get_usecase_from_list(adev, out->usecase) != NULL) &&
7228 (out->usecase != USECASE_COMPRESS_VOIP_CALL)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007229 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007230 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07007231 ret = -EEXIST;
7232 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007233 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08007234
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007235 pthread_mutex_unlock(&adev->lock);
7236
7237 out->stream.common.get_sample_rate = out_get_sample_rate;
7238 out->stream.common.set_sample_rate = out_set_sample_rate;
7239 out->stream.common.get_buffer_size = out_get_buffer_size;
7240 out->stream.common.get_channels = out_get_channels;
7241 out->stream.common.get_format = out_get_format;
7242 out->stream.common.set_format = out_set_format;
7243 out->stream.common.standby = out_standby;
7244 out->stream.common.dump = out_dump;
7245 out->stream.common.set_parameters = out_set_parameters;
7246 out->stream.common.get_parameters = out_get_parameters;
7247 out->stream.common.add_audio_effect = out_add_audio_effect;
7248 out->stream.common.remove_audio_effect = out_remove_audio_effect;
7249 out->stream.get_latency = out_get_latency;
7250 out->stream.set_volume = out_set_volume;
Aalique Grahame22e49102018-12-18 14:23:57 -08007251#ifdef NO_AUDIO_OUT
7252 out->stream.write = out_write_for_no_output;
7253#else
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007254 out->stream.write = out_write;
Aalique Grahame22e49102018-12-18 14:23:57 -08007255#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007256 out->stream.get_render_position = out_get_render_position;
7257 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07007258 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007259
Haynes Mathew George16081042017-05-31 17:16:49 -07007260 if (out->realtime)
7261 out->af_period_multiplier = af_period_multiplier;
7262 else
7263 out->af_period_multiplier = 1;
7264
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007265 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07007266 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07007267 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007268
7269 config->format = out->stream.common.get_format(&out->stream.common);
7270 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
7271 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
Naresh Tanniru04f71882018-06-26 17:46:22 +05307272 register_format(out->format, out->supported_formats);
7273 register_channel_mask(out->channel_mask, out->supported_channel_masks);
7274 register_sample_rate(out->sample_rate, out->supported_sample_rates);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007275
Aalique Grahame22e49102018-12-18 14:23:57 -08007276 out->error_log = error_log_create(
7277 ERROR_LOG_ENTRIES,
7278 1000000000 /* aggregate consecutive identical errors within one second in ns */);
7279
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05307280 /*
7281 By locking output stream before registering, we allow the callback
7282 to update stream's state only after stream's initial state is set to
7283 adev state.
7284 */
7285 lock_output_stream(out);
7286 audio_extn_snd_mon_register_listener(out, out_snd_mon_cb);
7287 pthread_mutex_lock(&adev->lock);
7288 out->card_status = adev->card_status;
7289 pthread_mutex_unlock(&adev->lock);
7290 pthread_mutex_unlock(&out->lock);
7291
Aalique Grahame22e49102018-12-18 14:23:57 -08007292 stream_app_type_cfg_init(&out->app_type_cfg);
7293
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007294 *stream_out = &out->stream;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05307295 ALOGD("%s: Stream (%p) picks up usecase (%s)", __func__, &out->stream,
vivek mehta0ea887a2015-08-26 14:01:20 -07007296 use_case_table[out->usecase]);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08007297
7298 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
7299 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
7300 popcount(out->channel_mask), out->playback_started);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08007301 /* setup a channel for client <--> adsp communication for stream events */
Manish Dewangan21a850a2017-08-14 12:03:55 +05307302 is_direct_passthough = audio_extn_passthru_is_direct_passthrough(out);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08007303 if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
Naresh Tanniru85819452017-05-04 18:55:45 -07007304 (out->flags & AUDIO_OUTPUT_FLAG_DIRECT_PCM) ||
Manish Dewangan21a850a2017-08-14 12:03:55 +05307305 (audio_extn_ip_hdlr_intf_supported(config->format, is_direct_passthough, false))) {
Ben Rombergerd771a7c2017-02-22 18:05:17 -08007306 hdlr_stream_cfg.pcm_device_id = platform_get_pcm_device_id(
7307 out->usecase, PCM_PLAYBACK);
7308 hdlr_stream_cfg.flags = out->flags;
7309 hdlr_stream_cfg.type = PCM_PLAYBACK;
7310 ret = audio_extn_adsp_hdlr_stream_open(&out->adsp_hdlr_stream_handle,
7311 &hdlr_stream_cfg);
7312 if (ret) {
7313 ALOGE("%s: adsp_hdlr_stream_open failed %d",__func__, ret);
7314 out->adsp_hdlr_stream_handle = NULL;
7315 }
7316 }
Manish Dewangan21a850a2017-08-14 12:03:55 +05307317 if (audio_extn_ip_hdlr_intf_supported(config->format, is_direct_passthough, false)) {
Vidyakumar Athota2062f912017-06-27 14:46:15 -07007318 ret = audio_extn_ip_hdlr_intf_init(&out->ip_hdlr_handle, NULL, NULL, adev, out->usecase);
Naresh Tanniru85819452017-05-04 18:55:45 -07007319 if (ret < 0) {
7320 ALOGE("%s: audio_extn_ip_hdlr_intf_init failed %d",__func__, ret);
7321 out->ip_hdlr_handle = NULL;
7322 }
7323 }
Derek Chenf939fb72018-11-13 13:34:41 -08007324
7325 streams_output_ctxt_t *out_ctxt = (streams_output_ctxt_t *)
7326 calloc(1, sizeof(streams_output_ctxt_t));
7327 if (out_ctxt == NULL) {
7328 ALOGE("%s fail to allocate output ctxt", __func__);
7329 ret = -ENOMEM;
7330 goto error_open;
7331 }
7332 out_ctxt->output = out;
7333
7334 pthread_mutex_lock(&adev->lock);
7335 list_add_tail(&adev->active_outputs_list, &out_ctxt->list);
7336 pthread_mutex_unlock(&adev->lock);
7337
Eric Laurent994a6932013-07-17 11:51:42 -07007338 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007339 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07007340
7341error_open:
Ashish Jain83a6cc22016-06-28 14:34:17 +05307342 if (out->convert_buffer)
7343 free(out->convert_buffer);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07007344 free(out);
7345 *stream_out = NULL;
7346 ALOGD("%s: exit: ret %d", __func__, ret);
7347 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007348}
7349
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05307350void adev_close_output_stream(struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007351 struct audio_stream_out *stream)
7352{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07007353 struct stream_out *out = (struct stream_out *)stream;
7354 struct audio_device *adev = out->dev;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007355 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07007356
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007357 ALOGD("%s: enter:stream_handle(%s)",__func__, use_case_table[out->usecase]);
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05307358
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05307359 // must deregister from sndmonitor first to prevent races
7360 // between the callback and close_stream
7361 audio_extn_snd_mon_unregister_listener(out);
7362
Ben Rombergerd771a7c2017-02-22 18:05:17 -08007363 /* close adsp hdrl session before standby */
7364 if (out->adsp_hdlr_stream_handle) {
7365 ret = audio_extn_adsp_hdlr_stream_close(out->adsp_hdlr_stream_handle);
7366 if (ret)
7367 ALOGE("%s: adsp_hdlr_stream_close failed %d",__func__, ret);
7368 out->adsp_hdlr_stream_handle = NULL;
7369 }
7370
Manish Dewangan21a850a2017-08-14 12:03:55 +05307371 if (out->ip_hdlr_handle) {
Naresh Tanniru85819452017-05-04 18:55:45 -07007372 audio_extn_ip_hdlr_intf_deinit(out->ip_hdlr_handle);
7373 out->ip_hdlr_handle = NULL;
7374 }
7375
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007376 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05307377 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007378 ret = voice_extn_compress_voip_close_output_stream(&stream->common);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05307379 out->started = 0;
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05307380 pthread_mutex_unlock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007381 if(ret != 0)
7382 ALOGE("%s: Compress voip output cannot be closed, error:%d",
7383 __func__, ret);
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07007384 } else
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007385 out_standby(&stream->common);
7386
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07007387 if (is_offload_usecase(out->usecase)) {
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08007388 audio_extn_dts_remove_state_notifier_node(out->usecase);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07007389 destroy_offload_callback_thread(out);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07007390 free_offload_usecase(adev, out->usecase);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07007391 if (out->compr_config.codec != NULL)
7392 free(out->compr_config.codec);
7393 }
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07007394
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05307395 out->a2dp_compress_mute = false;
7396
Varun Balaraje49253e2017-07-06 19:48:56 +05307397 if (is_interactive_usecase(out->usecase))
7398 free_interactive_usecase(adev, out->usecase);
7399
Ashish Jain83a6cc22016-06-28 14:34:17 +05307400 if (out->convert_buffer != NULL) {
7401 free(out->convert_buffer);
7402 out->convert_buffer = NULL;
7403 }
7404
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07007405 if (adev->voice_tx_output == out)
7406 adev->voice_tx_output = NULL;
7407
Aalique Grahame22e49102018-12-18 14:23:57 -08007408 error_log_destroy(out->error_log);
7409 out->error_log = NULL;
7410
Dhanalakshmi Siddani6c3d0992017-01-16 16:52:33 +05307411 if (adev->primary_output == out)
7412 adev->primary_output = NULL;
7413
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07007414 pthread_cond_destroy(&out->cond);
7415 pthread_mutex_destroy(&out->lock);
Derek Chenf939fb72018-11-13 13:34:41 -08007416
7417 pthread_mutex_lock(&adev->lock);
7418 streams_output_ctxt_t *out_ctxt = out_get_stream(adev, out->handle);
7419 if (out_ctxt != NULL) {
7420 list_remove(&out_ctxt->list);
7421 free(out_ctxt);
7422 } else {
7423 ALOGW("%s, output stream already closed", __func__);
7424 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007425 free(stream);
Derek Chenf939fb72018-11-13 13:34:41 -08007426 pthread_mutex_unlock(&adev->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07007427 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007428}
7429
7430static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
7431{
7432 struct audio_device *adev = (struct audio_device *)dev;
7433 struct str_parms *parms;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007434 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07007435 int val;
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07007436 int ret;
7437 int status = 0;
Aalique Grahame22e49102018-12-18 14:23:57 -08007438 bool a2dp_reconfig = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007439
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08007440 ALOGD("%s: enter: %s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007441 parms = str_parms_create_str(kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007442
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05307443 if (!parms)
7444 goto error;
Naresh Tanniru4c630392014-05-12 01:05:52 +05307445
Ashish Jain1b9b30c2017-05-18 20:57:40 +05307446 ret = str_parms_get_str(parms, "BT_SCO", value, sizeof(value));
7447 if (ret >= 0) {
7448 /* When set to false, HAL should disable EC and NS */
7449 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
7450 adev->bt_sco_on = true;
7451 else
7452 adev->bt_sco_on = false;
7453 }
7454
Naresh Tanniru4c630392014-05-12 01:05:52 +05307455 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07007456 status = voice_set_parameters(adev, parms);
7457 if (status != 0)
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08007458 goto done;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007459
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07007460 status = platform_set_parameters(adev->platform, parms);
7461 if (status != 0)
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08007462 goto done;
7463
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07007464 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
7465 if (ret >= 0) {
Vicky Sehrawate240e5d2014-08-12 17:17:04 -07007466 /* When set to false, HAL should disable EC and NS */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007467 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
7468 adev->bluetooth_nrec = true;
7469 else
7470 adev->bluetooth_nrec = false;
7471 }
7472
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07007473 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
7474 if (ret >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007475 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
7476 adev->screen_off = false;
7477 else
7478 adev->screen_off = true;
7479 }
7480
Aalique Grahame22e49102018-12-18 14:23:57 -08007481#ifndef MAXXAUDIO_QDSP_ENABLED
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07007482 ret = str_parms_get_int(parms, "rotation", &val);
7483 if (ret >= 0) {
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07007484 bool reverse_speakers = false;
7485 switch(val) {
7486 // FIXME: note that the code below assumes that the speakers are in the correct placement
7487 // relative to the user when the device is rotated 90deg from its default rotation. This
7488 // assumption is device-specific, not platform-specific like this code.
7489 case 270:
7490 reverse_speakers = true;
7491 break;
7492 case 0:
7493 case 90:
7494 case 180:
7495 break;
7496 default:
7497 ALOGE("%s: unexpected rotation of %d", __func__, val);
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07007498 status = -EINVAL;
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07007499 }
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07007500 if (status == 0) {
Vignesh Kulothungan3b5fae52017-09-25 12:16:30 -07007501 // check and set swap
7502 // - check if orientation changed and speaker active
7503 // - set rotation and cache the rotation value
7504 platform_check_and_set_swap_lr_channels(adev, reverse_speakers);
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07007505 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07007506 }
Aalique Grahame22e49102018-12-18 14:23:57 -08007507#endif
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07007508
Mingming Yin514a8bc2014-07-29 15:22:21 -07007509 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_SCO_WB, value, sizeof(value));
7510 if (ret >= 0) {
7511 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
7512 adev->bt_wb_speech_enabled = true;
7513 else
7514 adev->bt_wb_speech_enabled = false;
7515 }
7516
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -07007517 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value, sizeof(value));
7518 if (ret >= 0) {
7519 val = atoi(value);
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05307520 audio_devices_t device = (audio_devices_t) val;
Zhou Song681350a2017-10-19 16:28:42 +08007521 if (audio_is_output_device(val) &&
7522 (val & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07007523 ALOGV("cache new ext disp type and edid");
7524 ret = platform_get_ext_disp_type(adev->platform);
7525 if (ret < 0) {
7526 ALOGE("%s: Failed to query disp type, ret:%d", __func__, ret);
Manisha Agarwal2f5ff882018-08-08 17:09:29 +05307527 } else {
7528 platform_cache_edid(adev->platform);
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07007529 }
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05307530 } else if (audio_is_usb_out_device(device) || audio_is_usb_in_device(device)) {
vivek mehta344576a2016-04-12 18:56:03 -07007531 /*
7532 * Do not allow AFE proxy port usage by WFD source when USB headset is connected.
7533 * Per AudioPolicyManager, USB device is higher priority than WFD.
7534 * For Voice call over USB headset, voice call audio is routed to AFE proxy ports.
7535 * If WFD use case occupies AFE proxy, it may result unintended behavior while
7536 * starting voice call on USB
7537 */
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08007538 ret = str_parms_get_str(parms, "card", value, sizeof(value));
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05307539 if (ret >= 0)
7540 audio_extn_usb_add_device(device, atoi(value));
7541
Zhou Song6f862822017-11-06 17:27:57 +08007542 if (!audio_extn_usb_is_tunnel_supported()) {
7543 ALOGV("detected USB connect .. disable proxy");
7544 adev->allow_afe_proxy_usage = false;
7545 }
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -07007546 }
7547 }
7548
7549 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value, sizeof(value));
7550 if (ret >= 0) {
7551 val = atoi(value);
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05307552 audio_devices_t device = (audio_devices_t) val;
Garmond Leunge3b6d482016-10-25 16:48:01 -07007553 /*
7554 * The HDMI / Displayport disconnect handling has been moved to
7555 * audio extension to ensure that its parameters are not
7556 * invalidated prior to updating sysfs of the disconnect event
7557 * Invalidate will be handled by audio_extn_ext_disp_set_parameters()
7558 */
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05307559 if (audio_is_usb_out_device(device) || audio_is_usb_in_device(device)) {
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08007560 ret = str_parms_get_str(parms, "card", value, sizeof(value));
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05307561 if (ret >= 0)
7562 audio_extn_usb_remove_device(device, atoi(value));
7563
Zhou Song6f862822017-11-06 17:27:57 +08007564 if (!audio_extn_usb_is_tunnel_supported()) {
7565 ALOGV("detected USB disconnect .. enable proxy");
7566 adev->allow_afe_proxy_usage = true;
7567 }
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -07007568 }
7569 }
7570
Aalique Grahame22e49102018-12-18 14:23:57 -08007571 audio_extn_hfp_set_parameters(adev, parms);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08007572 audio_extn_qdsp_set_parameters(adev, parms);
Aalique Grahame22e49102018-12-18 14:23:57 -08007573
7574 status = audio_extn_a2dp_set_parameters(parms, &a2dp_reconfig);
Aniket Kumar Lata23300322019-02-20 22:25:30 -08007575 if (status >= 0 && a2dp_reconfig) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05307576 struct audio_usecase *usecase;
7577 struct listnode *node;
7578 list_for_each(node, &adev->usecase_list) {
7579 usecase = node_to_item(node, struct audio_usecase, list);
7580 if ((usecase->type == PCM_PLAYBACK) &&
Naresh Tanniruf7e9e632016-11-04 14:54:20 -07007581 (usecase->devices & AUDIO_DEVICE_OUT_ALL_A2DP)){
Naresh Tanniru9d027a62015-03-13 01:32:10 +05307582 ALOGD("reconfigure a2dp... forcing device switch");
Weiyin Jiang425180d2017-06-05 16:40:23 +08007583
7584 pthread_mutex_unlock(&adev->lock);
Naresh Tannirucd2353e2016-08-19 00:37:25 +05307585 lock_output_stream(usecase->stream.out);
Weiyin Jiang425180d2017-06-05 16:40:23 +08007586 pthread_mutex_lock(&adev->lock);
Naresh Tannirucd2353e2016-08-19 00:37:25 +05307587 audio_extn_a2dp_set_handoff_mode(true);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05307588 //force device switch to re configure encoder
7589 select_devices(adev, usecase->id);
Naresh Tannirucd2353e2016-08-19 00:37:25 +05307590 audio_extn_a2dp_set_handoff_mode(false);
7591 pthread_mutex_unlock(&usecase->stream.out->lock);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05307592 break;
7593 }
7594 }
7595 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08007596
7597 //handle vr audio setparam
7598 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_VR_AUDIO_MODE,
7599 value, sizeof(value));
7600 if (ret >= 0) {
7601 ALOGI("Setting vr mode to be %s", value);
7602 if (!strncmp(value, "true", 4)) {
7603 adev->vr_audio_mode_enabled = true;
7604 ALOGI("Setting vr mode to true");
7605 } else if (!strncmp(value, "false", 5)) {
7606 adev->vr_audio_mode_enabled = false;
7607 ALOGI("Setting vr mode to false");
7608 } else {
7609 ALOGI("wrong vr mode set");
7610 }
7611 }
7612
Naresh Tannirucd2353e2016-08-19 00:37:25 +05307613 audio_extn_set_parameters(adev, parms);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08007614done:
7615 str_parms_destroy(parms);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08007616 pthread_mutex_unlock(&adev->lock);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05307617error:
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07007618 ALOGV("%s: exit with code(%d)", __func__, status);
7619 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007620}
7621
7622static char* adev_get_parameters(const struct audio_hw_device *dev,
7623 const char *keys)
7624{
Sidipotu Ashokaa4fa6a2018-12-21 09:19:26 +05307625 ALOGD("%s:%s", __func__, keys);
7626
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07007627 struct audio_device *adev = (struct audio_device *)dev;
7628 struct str_parms *reply = str_parms_create();
7629 struct str_parms *query = str_parms_create_str(keys);
7630 char *str;
Naresh Tannirud7205b62014-06-20 02:54:48 +05307631 char value[256] = {0};
7632 int ret = 0;
7633
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07007634 if (!query || !reply) {
Alexy Josephaee4fdd2016-01-29 13:02:07 -08007635 if (reply) {
7636 str_parms_destroy(reply);
7637 }
7638 if (query) {
7639 str_parms_destroy(query);
7640 }
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07007641 ALOGE("adev_get_parameters: failed to create query or reply");
7642 return NULL;
7643 }
7644
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08007645 //handle vr audio getparam
7646
7647 ret = str_parms_get_str(query,
7648 AUDIO_PARAMETER_KEY_VR_AUDIO_MODE,
7649 value, sizeof(value));
7650
7651 if (ret >= 0) {
7652 bool vr_audio_enabled = false;
7653 pthread_mutex_lock(&adev->lock);
7654 vr_audio_enabled = adev->vr_audio_mode_enabled;
7655 pthread_mutex_unlock(&adev->lock);
7656
7657 ALOGI("getting vr mode to %d", vr_audio_enabled);
7658
7659 if (vr_audio_enabled) {
7660 str_parms_add_str(reply, AUDIO_PARAMETER_KEY_VR_AUDIO_MODE,
7661 "true");
7662 goto exit;
7663 } else {
7664 str_parms_add_str(reply, AUDIO_PARAMETER_KEY_VR_AUDIO_MODE,
7665 "false");
7666 goto exit;
7667 }
7668 }
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07007669
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08007670 pthread_mutex_lock(&adev->lock);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07007671 audio_extn_get_parameters(adev, query, reply);
Shiv Maliyappanahallif9308492013-12-12 12:18:09 -08007672 voice_get_parameters(adev, query, reply);
Aalique Grahame22e49102018-12-18 14:23:57 -08007673 audio_extn_a2dp_get_parameters(query, reply);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07007674 platform_get_parameters(adev->platform, query, reply);
Naresh Tanniru80659832014-06-04 18:17:56 +05307675 pthread_mutex_unlock(&adev->lock);
7676
Naresh Tannirud7205b62014-06-20 02:54:48 +05307677exit:
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07007678 str = str_parms_to_str(reply);
7679 str_parms_destroy(query);
7680 str_parms_destroy(reply);
7681
Sidipotu Ashokaa4fa6a2018-12-21 09:19:26 +05307682 ALOGD("%s: exit: returns - %s", __func__, str);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07007683 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007684}
7685
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07007686static int adev_init_check(const struct audio_hw_device *dev __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007687{
7688 return 0;
7689}
7690
7691static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
7692{
Haynes Mathew George5191a852013-09-11 14:19:36 -07007693 int ret;
7694 struct audio_device *adev = (struct audio_device *)dev;
Aalique Grahame22e49102018-12-18 14:23:57 -08007695
7696 audio_extn_extspk_set_voice_vol(adev->extspk, volume);
7697
Haynes Mathew George5191a852013-09-11 14:19:36 -07007698 pthread_mutex_lock(&adev->lock);
7699 /* cache volume */
Shruthi Krishnaace10852013-10-25 14:32:12 -07007700 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07007701 pthread_mutex_unlock(&adev->lock);
7702 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007703}
7704
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07007705static int adev_set_master_volume(struct audio_hw_device *dev __unused,
7706 float volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007707{
7708 return -ENOSYS;
7709}
7710
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07007711static int adev_get_master_volume(struct audio_hw_device *dev __unused,
7712 float *volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007713{
7714 return -ENOSYS;
7715}
7716
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07007717static int adev_set_master_mute(struct audio_hw_device *dev __unused,
7718 bool muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007719{
7720 return -ENOSYS;
7721}
7722
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07007723static int adev_get_master_mute(struct audio_hw_device *dev __unused,
7724 bool *muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007725{
7726 return -ENOSYS;
7727}
7728
7729static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
7730{
7731 struct audio_device *adev = (struct audio_device *)dev;
Garmond Leung5fd0b552018-04-17 11:56:12 -07007732 struct listnode *node;
7733 struct audio_usecase *usecase = NULL;
7734 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007735 pthread_mutex_lock(&adev->lock);
7736 if (adev->mode != mode) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07007737 ALOGD("%s: mode %d\n", __func__, mode);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007738 adev->mode = mode;
Shiv Maliyappanahallibb4cf0b2016-01-21 11:30:06 -08007739 if ((mode == AUDIO_MODE_NORMAL) && voice_is_in_call(adev)) {
Garmond Leung5fd0b552018-04-17 11:56:12 -07007740 list_for_each(node, &adev->usecase_list) {
7741 usecase = node_to_item(node, struct audio_usecase, list);
7742 if (usecase->type == VOICE_CALL)
7743 break;
7744 }
7745 if (usecase &&
7746 audio_is_usb_out_device(usecase->out_snd_device & AUDIO_DEVICE_OUT_ALL_USB)) {
7747 ret = audio_extn_usb_check_and_set_svc_int(usecase,
7748 true);
7749 if (ret != 0) {
7750 /* default service interval was successfully updated,
7751 reopen USB backend with new service interval */
7752 check_usecases_codec_backend(adev,
7753 usecase,
7754 usecase->out_snd_device);
7755 }
7756 }
7757
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07007758 voice_stop_call(adev);
Banajit Goswami20cdd212015-09-11 01:11:30 -07007759 platform_set_gsm_mode(adev->platform, false);
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07007760 adev->current_call_output = NULL;
7761 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007762 }
7763 pthread_mutex_unlock(&adev->lock);
7764 return 0;
7765}
7766
7767static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
7768{
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08007769 int ret;
Aalique Grahame22e49102018-12-18 14:23:57 -08007770 struct audio_device *adev = (struct audio_device *)dev;
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08007771
7772 pthread_mutex_lock(&adev->lock);
Vidyakumar Athota2850d532013-11-19 16:02:12 -08007773 ALOGD("%s state %d\n", __func__, state);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08007774 ret = voice_set_mic_mute((struct audio_device *)dev, state);
Aalique Grahame22e49102018-12-18 14:23:57 -08007775
Derek Chend2530072014-11-24 12:39:14 -08007776 if (adev->ext_hw_plugin)
7777 ret = audio_extn_ext_hw_plugin_set_mic_mute(adev->ext_hw_plugin, state);
Aalique Grahame22e49102018-12-18 14:23:57 -08007778
7779 adev->mic_muted = state;
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08007780 pthread_mutex_unlock(&adev->lock);
7781
7782 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007783}
7784
7785static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
7786{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07007787 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007788 return 0;
7789}
7790
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07007791static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007792 const struct audio_config *config)
7793{
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07007794 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007795
Aalique Grahame22e49102018-12-18 14:23:57 -08007796 /* Don't know if USB HIFI in this context so use true to be conservative */
7797 if (check_input_parameters(config->sample_rate, config->format, channel_count,
7798 true /*is_usb_hifi */) != 0)
7799 return 0;
7800
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07007801 return get_input_buffer_size(config->sample_rate, config->format, channel_count,
7802 false /* is_low_latency: since we don't know, be conservative */);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007803}
7804
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007805static bool adev_input_allow_hifi_record(struct audio_device *adev,
7806 audio_devices_t devices,
7807 audio_input_flags_t flags,
7808 audio_source_t source) {
7809 const bool allowed = true;
7810
7811 if (!audio_is_usb_in_device(devices))
7812 return !allowed;
7813
7814 switch (flags) {
7815 case AUDIO_INPUT_FLAG_NONE:
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007816 break;
Haynes Mathew George59862182017-10-24 16:23:57 -07007817 case AUDIO_INPUT_FLAG_FAST: // disallow hifi record for FAST as
7818 // it affects RTD numbers over USB
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007819 default:
7820 return !allowed;
7821 }
7822
7823 switch (source) {
7824 case AUDIO_SOURCE_DEFAULT:
7825 case AUDIO_SOURCE_MIC:
7826 case AUDIO_SOURCE_UNPROCESSED:
7827 break;
7828 default:
7829 return !allowed;
7830 }
7831
7832 switch (adev->mode) {
7833 case 0:
7834 break;
7835 default:
7836 return !allowed;
7837 }
7838
7839 return allowed;
7840}
7841
Haynes Mathew George4ffef292017-11-21 15:08:02 -08007842static int adev_update_voice_comm_input_stream(struct stream_in *in,
7843 struct audio_config *config)
7844{
7845 bool valid_rate = (config->sample_rate == 8000 ||
7846 config->sample_rate == 16000 ||
7847 config->sample_rate == 32000 ||
7848 config->sample_rate == 48000);
7849 bool valid_ch = audio_channel_count_from_in_mask(in->channel_mask) == 1;
7850
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08007851 if(!voice_extn_is_compress_voip_supported()) {
7852 if (valid_rate && valid_ch &&
kunleizd96526c2018-04-09 11:12:32 +08007853 in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) {
Haynes Mathew George4ffef292017-11-21 15:08:02 -08007854 in->usecase = USECASE_AUDIO_RECORD_VOIP;
7855 in->config = default_pcm_config_voip_copp;
7856 in->config.period_size = VOIP_IO_BUF_SIZE(in->sample_rate,
7857 DEFAULT_VOIP_BUF_DURATION_MS,
7858 DEFAULT_VOIP_BIT_DEPTH_BYTE)/2;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08007859 } else {
7860 ALOGW("%s No valid input in voip, use defaults"
7861 "sample rate %u, channel mask 0x%X",
7862 __func__, config->sample_rate, in->channel_mask);
7863 }
Haynes Mathew George4ffef292017-11-21 15:08:02 -08007864 in->config.rate = config->sample_rate;
7865 in->sample_rate = config->sample_rate;
7866 } else {
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08007867 //XXX needed for voice_extn_compress_voip_open_input_stream
7868 in->config.rate = config->sample_rate;
7869 if ((in->dev->mode == AUDIO_MODE_IN_COMMUNICATION ||
7870 voice_extn_compress_voip_is_active(in->dev)) &&
7871 (voice_extn_compress_voip_is_format_supported(in->format)) &&
7872 valid_rate && valid_ch) {
7873 voice_extn_compress_voip_open_input_stream(in);
7874 // update rate entries to match config from AF
7875 in->config.rate = config->sample_rate;
7876 in->sample_rate = config->sample_rate;
7877 } else {
7878 ALOGW("%s compress voip not active, use defaults", __func__);
7879 }
Haynes Mathew George4ffef292017-11-21 15:08:02 -08007880 }
Haynes Mathew George4ffef292017-11-21 15:08:02 -08007881 return 0;
7882}
7883
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007884static int adev_open_input_stream(struct audio_hw_device *dev,
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07007885 audio_io_handle_t handle,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007886 audio_devices_t devices,
7887 struct audio_config *config,
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07007888 struct audio_stream_in **stream_in,
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307889 audio_input_flags_t flags,
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07007890 const char *address __unused,
Vidyakumar Athota5c398212015-03-31 21:53:21 -07007891 audio_source_t source)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007892{
7893 struct audio_device *adev = (struct audio_device *)dev;
7894 struct stream_in *in;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08007895 int ret = 0, buffer_size, frame_size;
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07007896 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07007897 bool is_low_latency = false;
Divya Narayanan Poojary45f19192016-09-30 18:52:13 +05307898 bool channel_mask_updated = false;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007899 bool is_usb_dev = audio_is_usb_in_device(devices);
7900 bool may_use_hifi_record = adev_input_allow_hifi_record(adev,
7901 devices,
7902 flags,
7903 source);
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05307904
kunleizdff872d2018-08-20 14:40:33 +08007905 if (is_usb_dev && (!audio_extn_usb_connected(NULL))) {
kunleizd6a9e0c2018-07-30 15:38:52 +08007906 is_usb_dev = false;
7907 devices = AUDIO_DEVICE_IN_BUILTIN_MIC;
7908 ALOGW("%s: ignore set device to non existing USB card, use input device(%#x)",
7909 __func__, devices);
7910 }
7911
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007912 *stream_in = NULL;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007913
7914 if (!(is_usb_dev && may_use_hifi_record)) {
7915 if (config->sample_rate == 0)
7916 config->sample_rate = 48000;
7917 if (config->channel_mask == AUDIO_CHANNEL_NONE)
7918 config->channel_mask = AUDIO_CHANNEL_IN_MONO;
7919 if (config->format == AUDIO_FORMAT_DEFAULT)
7920 config->format = AUDIO_FORMAT_PCM_16_BIT;
7921
7922 channel_count = audio_channel_count_from_in_mask(config->channel_mask);
7923
Aalique Grahame22e49102018-12-18 14:23:57 -08007924 if (check_input_parameters(config->sample_rate, config->format, channel_count,
7925 false) != 0)
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007926 return -EINVAL;
Chaithanya Krishna Bacharaju9955b162016-05-25 16:25:53 +05307927 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007928
7929 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07007930
7931 if (!in) {
7932 ALOGE("failed to allocate input stream");
7933 return -ENOMEM;
7934 }
7935
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05307936 ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x)\
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05307937 stream_handle(%p) io_handle(%d) source(%d) format %x",__func__, config->sample_rate,
7938 config->channel_mask, devices, &in->stream, handle, source, config->format);
Ravi Kumar Alamanda40703102014-04-24 10:34:41 -07007939 pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07007940 pthread_mutex_init(&in->pre_lock, (const pthread_mutexattr_t *) NULL);
Ravi Kumar Alamanda40703102014-04-24 10:34:41 -07007941
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007942 in->stream.common.get_sample_rate = in_get_sample_rate;
7943 in->stream.common.set_sample_rate = in_set_sample_rate;
7944 in->stream.common.get_buffer_size = in_get_buffer_size;
7945 in->stream.common.get_channels = in_get_channels;
7946 in->stream.common.get_format = in_get_format;
7947 in->stream.common.set_format = in_set_format;
7948 in->stream.common.standby = in_standby;
7949 in->stream.common.dump = in_dump;
7950 in->stream.common.set_parameters = in_set_parameters;
7951 in->stream.common.get_parameters = in_get_parameters;
7952 in->stream.common.add_audio_effect = in_add_audio_effect;
7953 in->stream.common.remove_audio_effect = in_remove_audio_effect;
7954 in->stream.set_gain = in_set_gain;
7955 in->stream.read = in_read;
7956 in->stream.get_input_frames_lost = in_get_input_frames_lost;
Aalique Grahame22e49102018-12-18 14:23:57 -08007957 in->stream.get_capture_position = in_get_capture_position;
Naresh Tannirudcb47c52018-06-25 16:23:32 +05307958 in->stream.get_active_microphones = in_get_active_microphones;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007959
7960 in->device = devices;
Vidyakumar Athota5c398212015-03-31 21:53:21 -07007961 in->source = source;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007962 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007963 in->standby = 1;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07007964 in->capture_handle = handle;
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07007965 in->flags = flags;
Haynes Mathew George46740472017-10-27 18:40:12 -07007966 in->bit_width = 16;
7967 in->af_period_multiplier = 1;
7968
Aalique Grahame22e49102018-12-18 14:23:57 -08007969 ALOGV("%s: source = %d, config->channel_mask = %d", __func__, source, config->channel_mask);
7970 if (source == AUDIO_SOURCE_VOICE_UPLINK ||
7971 source == AUDIO_SOURCE_VOICE_DOWNLINK) {
7972 /* Force channel config requested to mono if incall
7973 record is being requested for only uplink/downlink */
7974 if (config->channel_mask != AUDIO_CHANNEL_IN_MONO) {
7975 config->channel_mask = AUDIO_CHANNEL_IN_MONO;
7976 ret = -EINVAL;
7977 goto err_open;
7978 }
7979 }
7980
Haynes Mathew George46740472017-10-27 18:40:12 -07007981 /* Update config params with the requested sample rate and channels */
7982 if ((in->device == AUDIO_DEVICE_IN_TELEPHONY_RX) &&
7983 (adev->mode != AUDIO_MODE_IN_CALL)) {
7984 ret = -EINVAL;
7985 goto err_open;
7986 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007987
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007988 if (is_usb_dev && may_use_hifi_record) {
7989 /* HiFi record selects an appropriate format, channel, rate combo
7990 depending on sink capabilities*/
7991 ret = read_usb_sup_params_and_compare(false /*is_playback*/,
7992 &config->format,
7993 &in->supported_formats[0],
7994 MAX_SUPPORTED_FORMATS,
7995 &config->channel_mask,
7996 &in->supported_channel_masks[0],
7997 MAX_SUPPORTED_CHANNEL_MASKS,
7998 &config->sample_rate,
7999 &in->supported_sample_rates[0],
8000 MAX_SUPPORTED_SAMPLE_RATES);
8001 if (ret != 0) {
8002 ret = -EINVAL;
8003 goto err_open;
8004 }
8005 channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Haynes Mathew George4ffef292017-11-21 15:08:02 -08008006 } else if (config->format == AUDIO_FORMAT_DEFAULT) {
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05308007 config->format = AUDIO_FORMAT_PCM_16_BIT;
Surendar karkaaca3d082017-11-09 15:18:37 +05308008 } else if (property_get_bool("vendor.audio.capture.pcm.32bit.enable", false)
8009 && config->format == AUDIO_FORMAT_PCM_32_BIT) {
8010 in->config.format = PCM_FORMAT_S32_LE;
8011 in->bit_width = 32;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05308012 } else if ((config->format == AUDIO_FORMAT_PCM_FLOAT) ||
8013 (config->format == AUDIO_FORMAT_PCM_32_BIT) ||
8014 (config->format == AUDIO_FORMAT_PCM_24_BIT_PACKED) ||
8015 (config->format == AUDIO_FORMAT_PCM_8_24_BIT)) {
8016 bool ret_error = false;
8017 in->bit_width = 24;
8018 /* 24 bit is restricted to UNPROCESSED source only,also format supported
8019 from HAL is 24_packed and 8_24
8020 *> In case of UNPROCESSED source, for 24 bit, if format requested is other than
8021 24_packed return error indicating supported format is 24_packed
8022 *> In case of any other source requesting 24 bit or float return error
8023 indicating format supported is 16 bit only.
8024
8025 on error flinger will retry with supported format passed
8026 */
8027 if ((source != AUDIO_SOURCE_UNPROCESSED) &&
8028 (source != AUDIO_SOURCE_CAMCORDER)) {
8029 config->format = AUDIO_FORMAT_PCM_16_BIT;
8030 if (config->sample_rate > 48000)
8031 config->sample_rate = 48000;
8032 ret_error = true;
Haynes Mathew George46740472017-10-27 18:40:12 -07008033 } else if (!(config->format == AUDIO_FORMAT_PCM_24_BIT_PACKED ||
8034 config->format == AUDIO_FORMAT_PCM_8_24_BIT)) {
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05308035 config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
8036 ret_error = true;
8037 }
8038
8039 if (ret_error) {
8040 ret = -EINVAL;
8041 goto err_open;
8042 }
8043 }
8044
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008045 in->channel_mask = config->channel_mask;
8046 in->format = config->format;
8047
8048 in->usecase = USECASE_AUDIO_RECORD;
8049 if (config->sample_rate == LOW_LATENCY_CAPTURE_SAMPLE_RATE &&
8050 (flags & AUDIO_INPUT_FLAG_FAST) != 0) {
8051 is_low_latency = true;
8052#if LOW_LATENCY_CAPTURE_USE_CASE
8053 in->usecase = USECASE_AUDIO_RECORD_LOW_LATENCY;
8054#endif
8055 in->realtime = may_use_noirq_mode(adev, in->usecase, in->flags);
Aalique Grahame22e49102018-12-18 14:23:57 -08008056 if (!in->realtime) {
8057 in->config = pcm_config_audio_capture;
8058 frame_size = audio_stream_in_frame_size(&in->stream);
8059 buffer_size = get_input_buffer_size(config->sample_rate,
8060 config->format,
8061 channel_count,
8062 is_low_latency);
8063 in->config.period_size = buffer_size / frame_size;
8064 in->config.rate = config->sample_rate;
8065 in->af_period_multiplier = 1;
8066 } else {
8067 // period size is left untouched for rt mode playback
8068 in->config = pcm_config_audio_capture_rt;
8069 in->af_period_multiplier = af_period_multiplier;
8070 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008071 }
8072
8073 if ((config->sample_rate == LOW_LATENCY_CAPTURE_SAMPLE_RATE) &&
8074 ((in->flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0)) {
8075 in->realtime = 0;
8076 in->usecase = USECASE_AUDIO_RECORD_MMAP;
8077 in->config = pcm_config_mmap_capture;
Haynes Mathew George46740472017-10-27 18:40:12 -07008078 in->config.format = pcm_format_from_audio_format(config->format);
Weiyin Jiang7c30c612018-11-27 18:45:53 +08008079 in->config.channels = channel_count;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008080 in->stream.start = in_start;
8081 in->stream.stop = in_stop;
8082 in->stream.create_mmap_buffer = in_create_mmap_buffer;
8083 in->stream.get_mmap_position = in_get_mmap_position;
Haynes Mathew Georgef29116e2018-01-31 12:48:20 -08008084 in->sample_rate = in->config.rate;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008085 ALOGV("%s: USECASE_AUDIO_RECORD_MMAP", __func__);
8086 } else if (in->realtime) {
8087 in->config = pcm_config_audio_capture_rt;
Haynes Mathew George46740472017-10-27 18:40:12 -07008088 in->config.format = pcm_format_from_audio_format(config->format);
Haynes Mathew George4ffef292017-11-21 15:08:02 -08008089 in->config.channels = channel_count;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008090 in->sample_rate = in->config.rate;
8091 in->af_period_multiplier = af_period_multiplier;
Haynes Mathew George46740472017-10-27 18:40:12 -07008092 } else if (is_usb_dev && may_use_hifi_record) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008093 in->usecase = USECASE_AUDIO_RECORD_HIFI;
8094 in->config = pcm_config_audio_capture;
8095 frame_size = audio_stream_in_frame_size(&in->stream);
8096 buffer_size = get_input_buffer_size(config->sample_rate,
8097 config->format,
8098 channel_count,
8099 false /*is_low_latency*/);
8100 in->config.period_size = buffer_size / frame_size;
8101 in->config.rate = config->sample_rate;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008102 in->config.format = pcm_format_from_audio_format(config->format);
Karthikeyan Mani07faa602018-08-20 11:01:32 -07008103 switch (config->format) {
8104 case AUDIO_FORMAT_PCM_32_BIT:
8105 in->bit_width = 32;
8106 break;
8107 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
8108 case AUDIO_FORMAT_PCM_8_24_BIT:
8109 in->bit_width = 24;
8110 break;
8111 default:
8112 in->bit_width = 16;
8113 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008114 in->config.channels = channel_count;
Haynes Mathew Georgef29116e2018-01-31 12:48:20 -08008115 in->sample_rate = in->config.rate;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07008116 } else if ((in->device == AUDIO_DEVICE_IN_TELEPHONY_RX) ||
Dhanalakshmi Siddani0b1488e2016-09-06 12:58:42 +05308117 (in->device == AUDIO_DEVICE_IN_PROXY)) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07008118 if (config->sample_rate == 0)
8119 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
8120 if (config->sample_rate != 48000 && config->sample_rate != 16000 &&
8121 config->sample_rate != 8000) {
8122 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
8123 ret = -EINVAL;
8124 goto err_open;
8125 }
8126 if (config->format == AUDIO_FORMAT_DEFAULT)
8127 config->format = AUDIO_FORMAT_PCM_16_BIT;
8128 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
8129 config->format = AUDIO_FORMAT_PCM_16_BIT;
8130 ret = -EINVAL;
8131 goto err_open;
8132 }
8133
8134 in->usecase = USECASE_AUDIO_RECORD_AFE_PROXY;
8135 in->config = pcm_config_afe_proxy_record;
8136 in->config.channels = channel_count;
8137 in->config.rate = config->sample_rate;
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05308138 in->sample_rate = config->sample_rate;
Aalique Grahame22e49102018-12-18 14:23:57 -08008139 in->af_period_multiplier = 1;
8140 } else if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
8141 in->flags & AUDIO_INPUT_FLAG_VOIP_TX &&
8142 (config->sample_rate == 8000 ||
8143 config->sample_rate == 16000 ||
8144 config->sample_rate == 32000 ||
8145 config->sample_rate == 48000) &&
8146 channel_count == 1) {
8147 in->usecase = USECASE_AUDIO_RECORD_VOIP;
8148 in->config = pcm_config_audio_capture;
8149 frame_size = audio_stream_in_frame_size(&in->stream);
8150 buffer_size = get_stream_buffer_size(VOIP_CAPTURE_PERIOD_DURATION_MSEC,
8151 config->sample_rate,
8152 config->format,
8153 channel_count, false /*is_low_latency*/);
8154 in->config.period_size = buffer_size / frame_size;
8155 in->config.period_count = VOIP_CAPTURE_PERIOD_COUNT;
8156 in->config.rate = config->sample_rate;
8157 in->af_period_multiplier = 1;
Haynes Mathew George46740472017-10-27 18:40:12 -07008158 } else {
Revathi Uddarajud2634032017-12-07 14:42:34 +05308159 int ret_val;
8160 pthread_mutex_lock(&adev->lock);
8161 ret_val = audio_extn_check_and_set_multichannel_usecase(adev,
8162 in, config, &channel_mask_updated);
Samyak Jain15fda662018-12-18 16:40:52 +05308163#ifdef CONCURRENT_CAPTURE_ENABLED
8164 /* Acquire lock to avoid two concurrent use cases initialized to
8165 same pcm record use case*/
8166
8167 if(in->usecase == USECASE_AUDIO_RECORD) {
8168 if (!(adev->pcm_record_uc_state)) {
8169 ALOGV("%s: using USECASE_AUDIO_RECORD",__func__);
8170 adev->pcm_record_uc_state = 1;
8171 } else {
8172 /* Assign compress record use case for second record */
8173 in->usecase = USECASE_AUDIO_RECORD_COMPRESS2;
8174 in->flags |= AUDIO_INPUT_FLAG_COMPRESS;
8175 ALOGV("%s: overriding usecase with USECASE_AUDIO_RECORD_COMPRESS2 and appending compress flag", __func__);
8176 }
8177 }
8178#endif
Revathi Uddarajud2634032017-12-07 14:42:34 +05308179 pthread_mutex_unlock(&adev->lock);
8180
8181 if (!ret_val) {
8182 if (channel_mask_updated == true) {
8183 ALOGD("%s: return error to retry with updated channel mask (%#x)",
8184 __func__, config->channel_mask);
8185 ret = -EINVAL;
8186 goto err_open;
8187 }
8188 ALOGD("%s: created multi-channel session succesfully",__func__);
8189 } else if (audio_extn_compr_cap_enabled() &&
8190 audio_extn_compr_cap_format_supported(config->format) &&
8191 (in->dev->mode != AUDIO_MODE_IN_COMMUNICATION)) {
8192 audio_extn_compr_cap_init(in);
8193 } else if (audio_extn_cin_applicable_stream(in)) {
Sachin Mohan Gadag16273f72017-12-19 18:10:48 +05308194 in->sample_rate = config->sample_rate;
Revathi Uddarajud2634032017-12-07 14:42:34 +05308195 ret = audio_extn_cin_configure_input_stream(in);
8196 if (ret)
8197 goto err_open;
8198 } else {
8199 in->config = pcm_config_audio_capture;
8200 in->config.rate = config->sample_rate;
8201 in->config.format = pcm_format_from_audio_format(config->format);
8202 in->config.channels = channel_count;
8203 in->sample_rate = config->sample_rate;
8204 in->format = config->format;
8205 frame_size = audio_stream_in_frame_size(&in->stream);
8206 buffer_size = get_input_buffer_size(config->sample_rate,
Haynes Mathew George46740472017-10-27 18:40:12 -07008207 config->format,
8208 channel_count,
8209 is_low_latency);
Dieter Luecking5d57def2018-09-07 14:23:37 +02008210 /* prevent division-by-zero */
8211 if (frame_size == 0) {
8212 ALOGE("%s: Error frame_size==0", __func__);
8213 ret = -EINVAL;
8214 goto err_open;
8215 }
8216
Revathi Uddarajud2634032017-12-07 14:42:34 +05308217 in->config.period_size = buffer_size / frame_size;
Aalique Grahame22e49102018-12-18 14:23:57 -08008218 in->af_period_multiplier = 1;
Haynes Mathew George4ffef292017-11-21 15:08:02 -08008219
Revathi Uddarajud2634032017-12-07 14:42:34 +05308220 if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
8221 /* optionally use VOIP usecase depending on config(s) */
8222 ret = adev_update_voice_comm_input_stream(in, config);
8223 }
Haynes Mathew George4ffef292017-11-21 15:08:02 -08008224
Revathi Uddarajud2634032017-12-07 14:42:34 +05308225 if (ret) {
8226 ALOGE("%s AUDIO_SOURCE_VOICE_COMMUNICATION invalid args", __func__);
8227 goto err_open;
8228 }
Haynes Mathew George4ffef292017-11-21 15:08:02 -08008229 }
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07008230 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +05308231 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
8232 &adev->streams_input_cfg_list,
Haynes Mathew George4ffef292017-11-21 15:08:02 -08008233 devices, flags, in->format,
8234 in->sample_rate, in->bit_width,
8235 in->profile, &in->app_type_cfg);
Naresh Tanniru04f71882018-06-26 17:46:22 +05308236 register_format(in->format, in->supported_formats);
8237 register_channel_mask(in->channel_mask, in->supported_channel_masks);
8238 register_sample_rate(in->sample_rate, in->supported_sample_rates);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05308239
Aalique Grahame22e49102018-12-18 14:23:57 -08008240 in->error_log = error_log_create(
8241 ERROR_LOG_ENTRIES,
8242 1000000000 /* aggregate consecutive identical errors within one second */);
8243
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07008244 /* This stream could be for sound trigger lab,
8245 get sound trigger pcm if present */
8246 audio_extn_sound_trigger_check_and_get_session(in);
8247
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05308248 lock_input_stream(in);
8249 audio_extn_snd_mon_register_listener(in, in_snd_mon_cb);
8250 pthread_mutex_lock(&adev->lock);
8251 in->card_status = adev->card_status;
8252 pthread_mutex_unlock(&adev->lock);
8253 pthread_mutex_unlock(&in->lock);
8254
Aalique Grahame22e49102018-12-18 14:23:57 -08008255 stream_app_type_cfg_init(&in->app_type_cfg);
8256
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008257 *stream_in = &in->stream;
Derek Chenf939fb72018-11-13 13:34:41 -08008258
8259 streams_input_ctxt_t *in_ctxt = (streams_input_ctxt_t *)
8260 calloc(1, sizeof(streams_input_ctxt_t));
8261 if (in_ctxt == NULL) {
8262 ALOGE("%s fail to allocate input ctxt", __func__);
8263 ret = -ENOMEM;
8264 goto err_open;
8265 }
8266 in_ctxt->input = in;
8267
8268 pthread_mutex_lock(&adev->lock);
8269 list_add_tail(&adev->active_inputs_list, &in_ctxt->list);
8270 pthread_mutex_unlock(&adev->lock);
8271
Eric Laurent994a6932013-07-17 11:51:42 -07008272 ALOGV("%s: exit", __func__);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08008273 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008274
8275err_open:
8276 free(in);
8277 *stream_in = NULL;
8278 return ret;
8279}
8280
8281static void adev_close_input_stream(struct audio_hw_device *dev,
8282 struct audio_stream_in *stream)
8283{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08008284 int ret;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07008285 struct stream_in *in = (struct stream_in *)stream;
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07008286 struct audio_device *adev = (struct audio_device *)dev;
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05308287
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05308288 ALOGD("%s: enter:stream_handle(%p)",__func__, in);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08008289
kunleiz70e57612018-12-28 17:50:23 +08008290 /* must deregister from sndmonitor first to prevent races
8291 * between the callback and close_stream
8292 */
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05308293 audio_extn_snd_mon_unregister_listener(stream);
8294
kunleiz70e57612018-12-28 17:50:23 +08008295 /* Disable echo reference if there are no active input, hfp call
8296 * and sound trigger while closing input stream
8297 */
8298 if (!adev->active_input &&
8299 !audio_extn_hfp_is_active(adev) &&
8300 !audio_extn_sound_trigger_check_ec_ref_enable())
kunleiz8c93ce62018-11-29 15:04:56 +08008301 platform_set_echo_reference(adev, false, AUDIO_DEVICE_NONE);
kunleiz70e57612018-12-28 17:50:23 +08008302 else
8303 audio_extn_sound_trigger_update_ec_ref_status(false);
Venkata Narendra Kumar Gutta1bbbf542014-09-04 19:11:25 +05308304
Aalique Grahame22e49102018-12-18 14:23:57 -08008305 error_log_destroy(in->error_log);
8306 in->error_log = NULL;
8307
Pallavid7c7a272018-01-16 11:22:55 +05308308 if (in == NULL) {
8309 ALOGE("%s: audio_stream_in ptr is NULL", __func__);
8310 return;
8311 }
8312
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08008313 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05308314 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08008315 ret = voice_extn_compress_voip_close_input_stream(&stream->common);
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05308316 pthread_mutex_unlock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08008317 if (ret != 0)
8318 ALOGE("%s: Compress voip input cannot be closed, error:%d",
8319 __func__, ret);
8320 } else
8321 in_standby(&stream->common);
8322
Revathi Uddarajud2634032017-12-07 14:42:34 +05308323 pthread_mutex_lock(&adev->lock);
Samyak Jain15fda662018-12-18 16:40:52 +05308324 if (in->usecase == USECASE_AUDIO_RECORD) {
8325 adev->pcm_record_uc_state = 0;
8326 }
8327
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -07008328 if (audio_extn_ssr_get_stream() == in) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07008329 audio_extn_ssr_deinit();
8330 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008331
Garmond Leunge2433c32017-09-28 21:51:22 -07008332 if (audio_extn_ffv_get_stream() == in) {
8333 audio_extn_ffv_stream_deinit();
8334 }
8335
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05308336 if (audio_extn_compr_cap_enabled() &&
Mingming Yine62d7842013-10-25 16:26:03 -07008337 audio_extn_compr_cap_format_supported(in->config.format))
8338 audio_extn_compr_cap_deinit();
Dhanalakshmi Siddani74cf00b2016-12-02 13:55:57 +05308339
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05308340 if (audio_extn_cin_attached_usecase(in->usecase))
8341 audio_extn_cin_close_input_stream(in);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07008342
Mingming Yinfd7607b2016-01-22 12:48:44 -08008343 if (in->is_st_session) {
8344 ALOGV("%s: sound trigger pcm stop lab", __func__);
8345 audio_extn_sound_trigger_stop_lab(in);
8346 }
Derek Chenf939fb72018-11-13 13:34:41 -08008347 streams_input_ctxt_t *in_ctxt = in_get_stream(adev, in->capture_handle);
8348 if (in_ctxt != NULL) {
8349 list_remove(&in_ctxt->list);
8350 free(in_ctxt);
8351 } else {
8352 ALOGW("%s, input stream already closed", __func__);
8353 }
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07008354 free(stream);
Revathi Uddarajud2634032017-12-07 14:42:34 +05308355 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008356 return;
8357}
8358
Aalique Grahame22e49102018-12-18 14:23:57 -08008359/* verifies input and output devices and their capabilities.
8360 *
8361 * This verification is required when enabling extended bit-depth or
8362 * sampling rates, as not all qcom products support it.
8363 *
8364 * Suitable for calling only on initialization such as adev_open().
8365 * It fills the audio_device use_case_table[] array.
8366 *
8367 * Has a side-effect that it needs to configure audio routing / devices
8368 * in order to power up the devices and read the device parameters.
8369 * It does not acquire any hw device lock. Should restore the devices
8370 * back to "normal state" upon completion.
8371 */
8372static int adev_verify_devices(struct audio_device *adev)
8373{
8374 /* enumeration is a bit difficult because one really wants to pull
8375 * the use_case, device id, etc from the hidden pcm_device_table[].
8376 * In this case there are the following use cases and device ids.
8377 *
8378 * [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = {0, 0},
8379 * [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {15, 15},
8380 * [USECASE_AUDIO_PLAYBACK_HIFI] = {1, 1},
8381 * [USECASE_AUDIO_PLAYBACK_OFFLOAD] = {9, 9},
8382 * [USECASE_AUDIO_RECORD] = {0, 0},
8383 * [USECASE_AUDIO_RECORD_LOW_LATENCY] = {15, 15},
8384 * [USECASE_VOICE_CALL] = {2, 2},
8385 *
8386 * USECASE_AUDIO_PLAYBACK_OFFLOAD, USECASE_AUDIO_PLAYBACK_HIFI omitted.
8387 * USECASE_VOICE_CALL omitted, but possible for either input or output.
8388 */
8389
8390 /* should be the usecases enabled in adev_open_input_stream() */
8391 static const int test_in_usecases[] = {
8392 USECASE_AUDIO_RECORD,
8393 USECASE_AUDIO_RECORD_LOW_LATENCY, /* does not appear to be used */
8394 };
8395 /* should be the usecases enabled in adev_open_output_stream()*/
8396 static const int test_out_usecases[] = {
8397 USECASE_AUDIO_PLAYBACK_DEEP_BUFFER,
8398 USECASE_AUDIO_PLAYBACK_LOW_LATENCY,
8399 };
8400 static const usecase_type_t usecase_type_by_dir[] = {
8401 PCM_PLAYBACK,
8402 PCM_CAPTURE,
8403 };
8404 static const unsigned flags_by_dir[] = {
8405 PCM_OUT,
8406 PCM_IN,
8407 };
8408
8409 size_t i;
8410 unsigned dir;
8411 const unsigned card_id = adev->snd_card;
8412
8413 for (dir = 0; dir < 2; ++dir) {
8414 const usecase_type_t usecase_type = usecase_type_by_dir[dir];
8415 const unsigned flags_dir = flags_by_dir[dir];
8416 const size_t testsize =
8417 dir ? ARRAY_SIZE(test_in_usecases) : ARRAY_SIZE(test_out_usecases);
8418 const int *testcases =
8419 dir ? test_in_usecases : test_out_usecases;
8420 const audio_devices_t audio_device =
8421 dir ? AUDIO_DEVICE_IN_BUILTIN_MIC : AUDIO_DEVICE_OUT_SPEAKER;
8422
8423 for (i = 0; i < testsize; ++i) {
8424 const audio_usecase_t audio_usecase = testcases[i];
8425 int device_id;
8426 struct pcm_params **pparams;
8427 struct stream_out out;
8428 struct stream_in in;
8429 struct audio_usecase uc_info;
8430 int retval;
8431
8432 pparams = &adev->use_case_table[audio_usecase];
8433 pcm_params_free(*pparams); /* can accept null input */
8434 *pparams = NULL;
8435
8436 /* find the device ID for the use case (signed, for error) */
8437 device_id = platform_get_pcm_device_id(audio_usecase, usecase_type);
8438 if (device_id < 0)
8439 continue;
8440
8441 /* prepare structures for device probing */
8442 memset(&uc_info, 0, sizeof(uc_info));
8443 uc_info.id = audio_usecase;
8444 uc_info.type = usecase_type;
8445 if (dir) {
8446 adev->active_input = &in;
8447 memset(&in, 0, sizeof(in));
8448 in.device = audio_device;
8449 in.source = AUDIO_SOURCE_VOICE_COMMUNICATION;
8450 uc_info.stream.in = &in;
8451 } else {
8452 adev->active_input = NULL;
8453 }
8454 memset(&out, 0, sizeof(out));
8455 out.devices = audio_device; /* only field needed in select_devices */
8456 uc_info.stream.out = &out;
8457 uc_info.devices = audio_device;
8458 uc_info.in_snd_device = SND_DEVICE_NONE;
8459 uc_info.out_snd_device = SND_DEVICE_NONE;
8460 list_add_tail(&adev->usecase_list, &uc_info.list);
8461
8462 /* select device - similar to start_(in/out)put_stream() */
8463 retval = select_devices(adev, audio_usecase);
8464 if (retval >= 0) {
8465 *pparams = pcm_params_get(card_id, device_id, flags_dir);
8466#if LOG_NDEBUG == 0
Aalique Grahame203bee02019-03-13 17:49:36 -07008467 char info[512]; /* for possible debug info */
Aalique Grahame22e49102018-12-18 14:23:57 -08008468 if (*pparams) {
8469 ALOGV("%s: (%s) card %d device %d", __func__,
8470 dir ? "input" : "output", card_id, device_id);
8471 pcm_params_to_string(*pparams, info, ARRAY_SIZE(info));
8472 } else {
8473 ALOGV("%s: cannot locate card %d device %d", __func__, card_id, device_id);
8474 }
8475#endif
8476 }
8477
8478 /* deselect device - similar to stop_(in/out)put_stream() */
8479 /* 1. Get and set stream specific mixer controls */
8480 retval = disable_audio_route(adev, &uc_info);
8481 /* 2. Disable the rx device */
8482 retval = disable_snd_device(adev,
8483 dir ? uc_info.in_snd_device : uc_info.out_snd_device);
8484 list_remove(&uc_info.list);
8485 }
8486 }
8487 adev->active_input = NULL; /* restore adev state */
8488 return 0;
8489}
8490
Siddartha Shaik31b530e2017-05-19 15:26:33 +05308491int adev_create_audio_patch(struct audio_hw_device *dev,
8492 unsigned int num_sources,
8493 const struct audio_port_config *sources,
8494 unsigned int num_sinks,
8495 const struct audio_port_config *sinks,
8496 audio_patch_handle_t *handle)
8497{
Derek Chenf939fb72018-11-13 13:34:41 -08008498 int ret;
Siddartha Shaik31b530e2017-05-19 15:26:33 +05308499
Derek Chenf939fb72018-11-13 13:34:41 -08008500 ret = audio_extn_hw_loopback_create_audio_patch(dev,
8501 num_sources,
8502 sources,
8503 num_sinks,
8504 sinks,
8505 handle);
8506 ret |= audio_extn_auto_hal_create_audio_patch(dev,
8507 num_sources,
8508 sources,
8509 num_sinks,
8510 sinks,
8511 handle);
8512 return ret;
Siddartha Shaik31b530e2017-05-19 15:26:33 +05308513}
8514
8515int adev_release_audio_patch(struct audio_hw_device *dev,
8516 audio_patch_handle_t handle)
8517{
Derek Chenf939fb72018-11-13 13:34:41 -08008518 int ret;
8519
8520 ret = audio_extn_hw_loopback_release_audio_patch(dev, handle);
8521 ret |= audio_extn_auto_hal_release_audio_patch(dev, handle);
8522 return ret;
Siddartha Shaik31b530e2017-05-19 15:26:33 +05308523}
8524
8525int adev_get_audio_port(struct audio_hw_device *dev, struct audio_port *config)
8526{
8527 return audio_extn_hw_loopback_get_audio_port(dev, config);
8528}
8529
8530int adev_set_audio_port_config(struct audio_hw_device *dev,
8531 const struct audio_port_config *config)
8532{
8533 return audio_extn_hw_loopback_set_audio_port_config(dev, config);
8534}
8535
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07008536static int adev_dump(const audio_hw_device_t *device __unused,
8537 int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008538{
8539 return 0;
8540}
8541
8542static int adev_close(hw_device_t *device)
8543{
Aalique Grahame22e49102018-12-18 14:23:57 -08008544 size_t i;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008545 struct audio_device *adev = (struct audio_device *)device;
Kiran Kandi910e1862013-10-29 13:29:42 -07008546
8547 if (!adev)
8548 return 0;
8549
8550 pthread_mutex_lock(&adev_init_lock);
8551
8552 if ((--audio_device_ref_count) == 0) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05308553 audio_extn_snd_mon_unregister_listener(adev);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07008554 audio_extn_sound_trigger_deinit(adev);
Kiran Kandide144c82013-11-20 15:58:32 -08008555 audio_extn_listen_deinit(adev);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008556 audio_extn_qdsp_deinit();
Aalique Grahame22e49102018-12-18 14:23:57 -08008557 audio_extn_extspk_deinit(adev->extspk);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05308558 audio_extn_utils_release_streams_cfg_lists(
8559 &adev->streams_output_cfg_list,
8560 &adev->streams_input_cfg_list);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05308561 if (audio_extn_qaf_is_enabled())
8562 audio_extn_qaf_deinit();
Kiran Kandi910e1862013-10-29 13:29:42 -07008563 audio_route_free(adev->audio_route);
Weiyin Jiangfa65d3e2019-03-05 23:39:45 +08008564 audio_extn_gef_deinit(adev);
Kiran Kandi910e1862013-10-29 13:29:42 -07008565 free(adev->snd_dev_ref_cnt);
8566 platform_deinit(adev->platform);
Aalique Grahame22e49102018-12-18 14:23:57 -08008567 for (i = 0; i < ARRAY_SIZE(adev->use_case_table); ++i) {
8568 pcm_params_free(adev->use_case_table[i]);
8569 }
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07008570 if (adev->adm_deinit)
8571 adev->adm_deinit(adev->adm_data);
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05308572 qahwi_deinit(device);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08008573 audio_extn_adsp_hdlr_deinit();
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05308574 audio_extn_snd_mon_deinit();
Siddartha Shaik44dd7702017-06-14 12:13:25 +05308575 audio_extn_hw_loopback_deinit(adev);
Garmond Leunge2433c32017-09-28 21:51:22 -07008576 audio_extn_ffv_deinit();
Satish Babu Patakokilac3c5d432017-07-04 22:48:59 +05308577 if (adev->device_cfg_params) {
8578 free(adev->device_cfg_params);
8579 adev->device_cfg_params = NULL;
8580 }
Derek Chend2530072014-11-24 12:39:14 -08008581 if(adev->ext_hw_plugin)
8582 audio_extn_ext_hw_plugin_deinit(adev->ext_hw_plugin);
Derek Chenae7b0342019-02-08 15:17:04 -08008583 audio_extn_auto_hal_deinit();
Kiran Kandi910e1862013-10-29 13:29:42 -07008584 free(device);
8585 adev = NULL;
8586 }
8587 pthread_mutex_unlock(&adev_init_lock);
Vatsal Buchac09ae062018-11-14 13:25:08 +05308588 enable_gcov();
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008589 return 0;
8590}
8591
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07008592/* This returns 1 if the input parameter looks at all plausible as a low latency period size,
8593 * or 0 otherwise. A return value of 1 doesn't mean the value is guaranteed to work,
8594 * just that it _might_ work.
8595 */
8596static int period_size_is_plausible_for_low_latency(int period_size)
8597{
8598 switch (period_size) {
8599 case 160:
Ravi Kumar Alamanda474de5a2015-06-25 20:08:01 -07008600 case 192:
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07008601 case 240:
8602 case 320:
8603 case 480:
8604 return 1;
8605 default:
8606 return 0;
8607 }
8608}
8609
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05308610static void adev_snd_mon_cb(void *cookie, struct str_parms *parms)
8611{
8612 bool is_snd_card_status = false;
8613 bool is_ext_device_status = false;
8614 char value[32];
8615 int card = -1;
8616 card_status_t status;
8617
8618 if (cookie != adev || !parms)
8619 return;
8620
8621 if (!parse_snd_card_status(parms, &card, &status)) {
8622 is_snd_card_status = true;
8623 } else if (0 < str_parms_get_str(parms, "ext_audio_device", value, sizeof(value))) {
8624 is_ext_device_status = true;
8625 } else {
8626 // not a valid event
8627 return;
8628 }
8629
8630 pthread_mutex_lock(&adev->lock);
8631 if (card == adev->snd_card || is_ext_device_status) {
8632 if (is_snd_card_status && adev->card_status != status) {
8633 adev->card_status = status;
8634 platform_snd_card_update(adev->platform, status);
8635 audio_extn_fm_set_parameters(adev, parms);
8636 } else if (is_ext_device_status) {
8637 platform_set_parameters(adev->platform, parms);
8638 }
8639 }
8640 pthread_mutex_unlock(&adev->lock);
8641 return;
8642}
8643
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05308644/* out and adev lock held */
8645static int check_a2dp_restore_l(struct audio_device *adev, struct stream_out *out, bool restore)
8646{
8647 struct audio_usecase *uc_info;
8648 float left_p;
8649 float right_p;
8650 audio_devices_t devices;
8651
8652 uc_info = get_usecase_from_list(adev, out->usecase);
8653 if (uc_info == NULL) {
8654 ALOGE("%s: Could not find the usecase (%d) in the list",
8655 __func__, out->usecase);
8656 return -EINVAL;
8657 }
8658
8659 ALOGD("%s: enter: usecase(%d: %s)", __func__,
8660 out->usecase, use_case_table[out->usecase]);
8661
8662 if (restore) {
8663 // restore A2DP device for active usecases and unmute if required
8664 if ((out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) &&
8665 (uc_info->out_snd_device != SND_DEVICE_OUT_BT_A2DP)) {
8666 ALOGD("%s: restoring A2dp and unmuting stream", __func__);
8667 select_devices(adev, uc_info->id);
8668 pthread_mutex_lock(&out->compr_mute_lock);
8669 if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
8670 (out->a2dp_compress_mute)) {
8671 out->a2dp_compress_mute = false;
8672 out_set_compr_volume(&out->stream, out->volume_l, out->volume_r);
8673 }
8674 pthread_mutex_unlock(&out->compr_mute_lock);
8675 }
8676 } else {
8677 // mute compress stream if suspended
8678 pthread_mutex_lock(&out->compr_mute_lock);
8679 if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
8680 (!out->a2dp_compress_mute)) {
8681 if (!out->standby) {
8682 ALOGD("%s: selecting speaker and muting stream", __func__);
8683 devices = out->devices;
8684 out->devices = AUDIO_DEVICE_OUT_SPEAKER;
8685 left_p = out->volume_l;
8686 right_p = out->volume_r;
8687 if (out->offload_state == OFFLOAD_STATE_PLAYING)
8688 compress_pause(out->compr);
8689 out_set_compr_volume(&out->stream, (float)0, (float)0);
8690 out->a2dp_compress_mute = true;
8691 select_devices(adev, out->usecase);
8692 if (out->offload_state == OFFLOAD_STATE_PLAYING)
8693 compress_resume(out->compr);
8694 out->devices = devices;
8695 out->volume_l = left_p;
8696 out->volume_r = right_p;
8697 }
8698 }
8699 pthread_mutex_unlock(&out->compr_mute_lock);
8700 }
8701 ALOGV("%s: exit", __func__);
8702 return 0;
8703}
8704
8705int check_a2dp_restore(struct audio_device *adev, struct stream_out *out, bool restore)
8706{
8707 int ret = 0;
8708
8709 lock_output_stream(out);
8710 pthread_mutex_lock(&adev->lock);
8711
8712 ret = check_a2dp_restore_l(adev, out, restore);
8713
8714 pthread_mutex_unlock(&adev->lock);
8715 pthread_mutex_unlock(&out->lock);
8716 return ret;
8717}
8718
Haynes Mathew George01156f92018-04-13 15:29:54 -07008719void adev_on_battery_status_changed(bool charging)
8720{
8721 pthread_mutex_lock(&adev->lock);
8722 ALOGI("%s: battery status changed to %scharging", __func__, charging ? "" : "not ");
8723 adev->is_charging = charging;
Zhou Songc1088ea2018-06-12 00:17:29 +08008724 audio_extn_sound_trigger_update_battery_status(charging);
Haynes Mathew George01156f92018-04-13 15:29:54 -07008725 pthread_mutex_unlock(&adev->lock);
8726}
8727
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008728static int adev_open(const hw_module_t *module, const char *name,
8729 hw_device_t **device)
8730{
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05308731 int ret;
Derek Chenf939fb72018-11-13 13:34:41 -08008732 char value[PROPERTY_VALUE_MAX] = {0};
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05308733
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08008734 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008735 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
8736
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07008737 pthread_mutex_lock(&adev_init_lock);
Kiran Kandi910e1862013-10-29 13:29:42 -07008738 if (audio_device_ref_count != 0){
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07008739 *device = &adev->device.common;
Kiran Kandi910e1862013-10-29 13:29:42 -07008740 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07008741 ALOGD("%s: returning existing instance of adev", __func__);
8742 ALOGD("%s: exit", __func__);
8743 pthread_mutex_unlock(&adev_init_lock);
8744 return 0;
8745 }
8746
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008747 adev = calloc(1, sizeof(struct audio_device));
8748
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07008749 if (!adev) {
8750 pthread_mutex_unlock(&adev_init_lock);
8751 return -ENOMEM;
8752 }
8753
Ravi Kumar Alamanda40703102014-04-24 10:34:41 -07008754 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
8755
Weiyin Jiange6ce6312019-01-28 18:28:22 +08008756 // register audio ext hidl at the earliest
8757 audio_extn_hidl_init();
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +05308758#ifdef DYNAMIC_LOG_ENABLED
8759 register_for_dynamic_logging("hal");
8760#endif
8761
Derek Chenf939fb72018-11-13 13:34:41 -08008762 /* default audio HAL major version */
8763 uint32_t maj_version = 2;
8764 if(property_get("vendor.audio.hal.maj.version", value, NULL))
8765 maj_version = atoi(value);
8766
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008767 adev->device.common.tag = HARDWARE_DEVICE_TAG;
Derek Chenf939fb72018-11-13 13:34:41 -08008768 adev->device.common.version = HARDWARE_DEVICE_API_VERSION(maj_version, 0);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008769 adev->device.common.module = (struct hw_module_t *)module;
8770 adev->device.common.close = adev_close;
8771
8772 adev->device.init_check = adev_init_check;
8773 adev->device.set_voice_volume = adev_set_voice_volume;
8774 adev->device.set_master_volume = adev_set_master_volume;
8775 adev->device.get_master_volume = adev_get_master_volume;
8776 adev->device.set_master_mute = adev_set_master_mute;
8777 adev->device.get_master_mute = adev_get_master_mute;
8778 adev->device.set_mode = adev_set_mode;
8779 adev->device.set_mic_mute = adev_set_mic_mute;
8780 adev->device.get_mic_mute = adev_get_mic_mute;
8781 adev->device.set_parameters = adev_set_parameters;
8782 adev->device.get_parameters = adev_get_parameters;
8783 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
8784 adev->device.open_output_stream = adev_open_output_stream;
8785 adev->device.close_output_stream = adev_close_output_stream;
8786 adev->device.open_input_stream = adev_open_input_stream;
8787 adev->device.close_input_stream = adev_close_input_stream;
Siddartha Shaik31b530e2017-05-19 15:26:33 +05308788 adev->device.create_audio_patch = adev_create_audio_patch;
8789 adev->device.release_audio_patch = adev_release_audio_patch;
8790 adev->device.get_audio_port = adev_get_audio_port;
8791 adev->device.set_audio_port_config = adev_set_audio_port_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008792 adev->device.dump = adev_dump;
Naresh Tannirudcb47c52018-06-25 16:23:32 +05308793 adev->device.get_microphones = adev_get_microphones;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008794
8795 /* Set the default route before the PCM stream is opened */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008796 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08008797 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08008798 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008799 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008800 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08008801 adev->acdb_settings = TTY_MODE_OFF;
vivek mehta344576a2016-04-12 18:56:03 -07008802 adev->allow_afe_proxy_usage = true;
Ashish Jain1b9b30c2017-05-18 20:57:40 +05308803 adev->bt_sco_on = false;
Eric Laurent07eeafd2013-10-06 12:52:49 -07008804 /* adev->cur_hdmi_channels = 0; by calloc() */
Eric Laurentb23d5282013-05-14 15:27:20 -07008805 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Aniket Kumar Lata808e8d62019-01-28 22:54:28 -08008806 /* Init audio feature manager */
8807 audio_feature_manager_init();
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07008808 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08008809 list_init(&adev->usecase_list);
Derek Chenf939fb72018-11-13 13:34:41 -08008810 list_init(&adev->active_inputs_list);
8811 list_init(&adev->active_outputs_list);
Krishnankutty Kolathappilly0b2de1c2014-02-14 14:45:49 -08008812 adev->cur_wfd_channels = 2;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07008813 adev->offload_usecases_state = 0;
Samyak Jain15fda662018-12-18 16:40:52 +05308814 adev->pcm_record_uc_state = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05308815 adev->is_channel_status_set = false;
Sudheer Papothifa9d2282015-09-17 01:53:25 +05308816 adev->perf_lock_opts[0] = 0x101;
8817 adev->perf_lock_opts[1] = 0x20E;
8818 adev->perf_lock_opts_size = 2;
Xiaojun Sang785b5da2017-08-03 15:52:29 +08008819 adev->dsp_bit_width_enforce_mode = 0;
Naresh Tanniru4c630392014-05-12 01:05:52 +05308820
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008821 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07008822 adev->platform = platform_init(adev);
8823 if (!adev->platform) {
Ben Romberger2684ad72018-06-04 12:41:39 -07008824 pthread_mutex_destroy(&adev->lock);
Eric Laurentb23d5282013-05-14 15:27:20 -07008825 free(adev->snd_dev_ref_cnt);
8826 free(adev);
Ben Romberger2684ad72018-06-04 12:41:39 -07008827 adev = NULL;
Eric Laurentb23d5282013-05-14 15:27:20 -07008828 ALOGE("%s: Failed to init platform data, aborting.", __func__);
8829 *device = NULL;
Apoorv Raghuvanshi6e57d7e2013-12-16 16:02:45 -08008830 pthread_mutex_unlock(&adev_init_lock);
Eric Laurentb23d5282013-05-14 15:27:20 -07008831 return -EINVAL;
8832 }
Eric Laurentc4aef752013-09-12 17:45:53 -07008833
Aalique Grahame22e49102018-12-18 14:23:57 -08008834 adev->extspk = audio_extn_extspk_init(adev);
8835
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05308836 if (audio_extn_qaf_is_enabled()) {
8837 ret = audio_extn_qaf_init(adev);
8838 if (ret < 0) {
Ben Romberger2684ad72018-06-04 12:41:39 -07008839 pthread_mutex_destroy(&adev->lock);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05308840 free(adev);
Ben Romberger2684ad72018-06-04 12:41:39 -07008841 adev = NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05308842 ALOGE("%s: Failed to init platform data, aborting.", __func__);
8843 *device = NULL;
8844 pthread_mutex_unlock(&adev_init_lock);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05308845 return ret;
8846 }
8847
8848 adev->device.open_output_stream = audio_extn_qaf_open_output_stream;
8849 adev->device.close_output_stream = audio_extn_qaf_close_output_stream;
8850 }
8851
Derek Chenae7b0342019-02-08 15:17:04 -08008852 audio_extn_auto_hal_init(adev);
Derek Chend2530072014-11-24 12:39:14 -08008853 adev->ext_hw_plugin = audio_extn_ext_hw_plugin_init(adev);
8854
Eric Laurentc4aef752013-09-12 17:45:53 -07008855 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
8856 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
8857 if (adev->visualizer_lib == NULL) {
8858 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
8859 } else {
8860 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
8861 adev->visualizer_start_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08008862 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07008863 "visualizer_hal_start_output");
8864 adev->visualizer_stop_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08008865 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07008866 "visualizer_hal_stop_output");
8867 }
8868 }
Dhanalakshmi Siddani21be3ac2016-12-29 14:31:08 +05308869 audio_extn_init(adev);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008870 voice_extn_init(adev);
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08008871 audio_extn_listen_init(adev, adev->snd_card);
Weiyin Jiangaa80acd2016-09-21 16:42:11 +08008872 audio_extn_gef_init(adev);
Siddartha Shaik44dd7702017-06-14 12:13:25 +05308873 audio_extn_hw_loopback_init(adev);
Garmond Leunge2433c32017-09-28 21:51:22 -07008874 audio_extn_ffv_init(adev);
Eric Laurentc4aef752013-09-12 17:45:53 -07008875
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08008876 if (access(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, R_OK) == 0) {
8877 adev->offload_effects_lib = dlopen(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, RTLD_NOW);
8878 if (adev->offload_effects_lib == NULL) {
8879 ALOGE("%s: DLOPEN failed for %s", __func__,
8880 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
8881 } else {
8882 ALOGV("%s: DLOPEN successful for %s", __func__,
8883 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
8884 adev->offload_effects_start_output =
Ashish Jain5106d362016-05-11 19:23:33 +05308885 (int (*)(audio_io_handle_t, int, struct mixer *))dlsym(adev->offload_effects_lib,
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08008886 "offload_effects_bundle_hal_start_output");
8887 adev->offload_effects_stop_output =
8888 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
8889 "offload_effects_bundle_hal_stop_output");
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08008890 adev->offload_effects_set_hpx_state =
8891 (int (*)(bool))dlsym(adev->offload_effects_lib,
8892 "offload_effects_bundle_set_hpx_state");
Dhananjay Kumard68883d2015-09-04 13:39:26 +05308893 adev->offload_effects_get_parameters =
8894 (void (*)(struct str_parms *, struct str_parms *))
8895 dlsym(adev->offload_effects_lib,
8896 "offload_effects_bundle_get_parameters");
8897 adev->offload_effects_set_parameters =
8898 (void (*)(struct str_parms *))dlsym(adev->offload_effects_lib,
8899 "offload_effects_bundle_set_parameters");
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08008900 }
8901 }
8902
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07008903 if (access(ADM_LIBRARY_PATH, R_OK) == 0) {
8904 adev->adm_lib = dlopen(ADM_LIBRARY_PATH, RTLD_NOW);
8905 if (adev->adm_lib == NULL) {
8906 ALOGE("%s: DLOPEN failed for %s", __func__, ADM_LIBRARY_PATH);
8907 } else {
8908 ALOGV("%s: DLOPEN successful for %s", __func__, ADM_LIBRARY_PATH);
8909 adev->adm_init = (adm_init_t)
8910 dlsym(adev->adm_lib, "adm_init");
8911 adev->adm_deinit = (adm_deinit_t)
8912 dlsym(adev->adm_lib, "adm_deinit");
8913 adev->adm_register_input_stream = (adm_register_input_stream_t)
8914 dlsym(adev->adm_lib, "adm_register_input_stream");
8915 adev->adm_register_output_stream = (adm_register_output_stream_t)
8916 dlsym(adev->adm_lib, "adm_register_output_stream");
8917 adev->adm_deregister_stream = (adm_deregister_stream_t)
8918 dlsym(adev->adm_lib, "adm_deregister_stream");
8919 adev->adm_request_focus = (adm_request_focus_t)
8920 dlsym(adev->adm_lib, "adm_request_focus");
8921 adev->adm_abandon_focus = (adm_abandon_focus_t)
8922 dlsym(adev->adm_lib, "adm_abandon_focus");
Haynes Mathew George5beddd42016-06-27 18:33:40 -07008923 adev->adm_set_config = (adm_set_config_t)
8924 dlsym(adev->adm_lib, "adm_set_config");
8925 adev->adm_request_focus_v2 = (adm_request_focus_v2_t)
8926 dlsym(adev->adm_lib, "adm_request_focus_v2");
8927 adev->adm_is_noirq_avail = (adm_is_noirq_avail_t)
8928 dlsym(adev->adm_lib, "adm_is_noirq_avail");
8929 adev->adm_on_routing_change = (adm_on_routing_change_t)
8930 dlsym(adev->adm_lib, "adm_on_routing_change");
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07008931 }
8932 }
8933
Aalique Grahame22e49102018-12-18 14:23:57 -08008934 adev->enable_voicerx = false;
Mingming Yin514a8bc2014-07-29 15:22:21 -07008935 adev->bt_wb_speech_enabled = false;
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08008936 //initialize this to false for now,
8937 //this will be set to true through set param
8938 adev->vr_audio_mode_enabled = false;
Mingming Yin514a8bc2014-07-29 15:22:21 -07008939
Pradnya Chaphekar8a9dcd82014-09-09 09:49:10 -07008940 audio_extn_ds2_enable(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008941 *device = &adev->device.common;
Aalique Grahame22e49102018-12-18 14:23:57 -08008942
8943 if (k_enable_extended_precision)
8944 adev_verify_devices(adev);
8945
Xiaojun Sang785b5da2017-08-03 15:52:29 +08008946 adev->dsp_bit_width_enforce_mode =
8947 adev_init_dsp_bit_width_enforce_mode(adev->mixer);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008948
Dhananjay Kumard6d32152016-10-13 16:11:03 +05308949 audio_extn_utils_update_streams_cfg_lists(adev->platform, adev->mixer,
8950 &adev->streams_output_cfg_list,
8951 &adev->streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07008952
Kiran Kandi910e1862013-10-29 13:29:42 -07008953 audio_device_ref_count++;
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07008954
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07008955 int trial;
Aalique Grahame22e49102018-12-18 14:23:57 -08008956 if ((property_get("vendor.audio_hal.period_size", value, NULL) > 0) ||
8957 (property_get("audio_hal.period_size", value, NULL) > 0)) {
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07008958 trial = atoi(value);
8959 if (period_size_is_plausible_for_low_latency(trial)) {
8960 pcm_config_low_latency.period_size = trial;
8961 pcm_config_low_latency.start_threshold = trial / 4;
8962 pcm_config_low_latency.avail_min = trial / 4;
8963 configured_low_latency_capture_period_size = trial;
8964 }
8965 }
Aalique Grahame22e49102018-12-18 14:23:57 -08008966 if ((property_get("vendor.audio_hal.in_period_size", value, NULL) > 0) ||
8967 (property_get("audio_hal.in_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 configured_low_latency_capture_period_size = trial;
8971 }
8972 }
8973
Vignesh Kulothungan7d374312018-02-21 17:12:00 -08008974 adev->mic_break_enabled = property_get_bool("vendor.audio.mic_break", false);
8975
Aalique Grahame22e49102018-12-18 14:23:57 -08008976 if ((property_get("vendor.audio_hal.period_multiplier",value,NULL) > 0) ||
8977 (property_get("audio_hal.period_multiplier",value,NULL) > 0)) {
Haynes Mathew George5beddd42016-06-27 18:33:40 -07008978 af_period_multiplier = atoi(value);
8979 if (af_period_multiplier < 0)
8980 af_period_multiplier = 2;
8981 else if (af_period_multiplier > 4)
8982 af_period_multiplier = 4;
8983
8984 ALOGV("new period_multiplier = %d", af_period_multiplier);
8985 }
8986
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08008987 audio_extn_qdsp_init(adev->platform);
Aalique Grahame22e49102018-12-18 14:23:57 -08008988
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07008989 adev->multi_offload_enable = property_get_bool("vendor.audio.offload.multiple.enabled", false);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07008990 pthread_mutex_unlock(&adev_init_lock);
8991
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07008992 if (adev->adm_init)
8993 adev->adm_data = adev->adm_init();
8994
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05308995 qahwi_init(*device);
Sudheer Papothifa9d2282015-09-17 01:53:25 +05308996 audio_extn_perf_lock_init();
Ben Rombergerd771a7c2017-02-22 18:05:17 -08008997 audio_extn_adsp_hdlr_init(adev->mixer);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05308998
8999 audio_extn_snd_mon_init();
9000 pthread_mutex_lock(&adev->lock);
9001 audio_extn_snd_mon_register_listener(adev, adev_snd_mon_cb);
9002 adev->card_status = CARD_STATUS_ONLINE;
Haynes Mathew George01156f92018-04-13 15:29:54 -07009003 audio_extn_battery_properties_listener_init(adev_on_battery_status_changed);
9004 /*
9005 * if the battery state callback happens before charging can be queried,
9006 * it will be guarded with the adev->lock held in the cb function and so
9007 * the callback value will reflect the latest state
9008 */
9009 adev->is_charging = audio_extn_battery_properties_is_charging();
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05309010 audio_extn_sound_trigger_init(adev); /* dependent on snd_mon_init() */
Zhou Songc1088ea2018-06-12 00:17:29 +08009011 audio_extn_sound_trigger_update_battery_status(adev->is_charging);
9012 pthread_mutex_unlock(&adev->lock);
Satish Babu Patakokilac3c5d432017-07-04 22:48:59 +05309013 /* Allocate memory for Device config params */
9014 adev->device_cfg_params = (struct audio_device_config_param*)
9015 calloc(platform_get_max_codec_backend(),
9016 sizeof(struct audio_device_config_param));
9017 if (adev->device_cfg_params == NULL)
9018 ALOGE("%s: Memory allocation failed for Device config params", __func__);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05309019
Eric Laurent994a6932013-07-17 11:51:42 -07009020 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009021 return 0;
9022}
9023
9024static struct hw_module_methods_t hal_module_methods = {
9025 .open = adev_open,
9026};
9027
9028struct audio_module HAL_MODULE_INFO_SYM = {
9029 .common = {
9030 .tag = HARDWARE_MODULE_TAG,
9031 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
9032 .hal_api_version = HARDWARE_HAL_API_VERSION,
9033 .id = AUDIO_HARDWARE_MODULE_ID,
9034 .name = "QCOM Audio HAL",
Duy Truongfae19622013-11-24 02:17:54 -08009035 .author = "The Linux Foundation",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08009036 .methods = &hal_module_methods,
9037 },
9038};