blob: 13dd4a0e2641d19f27599eaedfb82a650df559ca [file] [log] [blame]
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001/*
Dhanalakshmi Siddani0f1dfd52019-01-09 12:38:13 +05302 * 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
58#include <cutils/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>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080064
Eric Laurentb23d5282013-05-14 15:27:20 -070065#include <hardware/audio_effect.h>
Haynes Mathew George484e8d22017-07-31 18:55:17 -070066#include <hardware/audio_alsaops.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070067#include <system/thread_defs.h>
Haynes Mathew George16081042017-05-31 17:16:49 -070068#include <tinyalsa/asoundlib.h>
Eric Laurentb23d5282013-05-14 15:27:20 -070069#include <audio_effects/effect_aec.h>
70#include <audio_effects/effect_ns.h>
Ashish Jainf1eaa582016-05-23 20:54:24 +053071#include <audio_utils/format.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080072#include "audio_hw.h"
Eric Laurentb23d5282013-05-14 15:27:20 -070073#include "platform_api.h"
74#include <platform.h>
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -070075#include "audio_extn.h"
Narsinga Rao Chella05573b72013-11-15 15:21:40 -080076#include "voice_extn.h"
Ashish Jaind5694242017-09-05 20:09:06 +053077#include "ip_hdlr_intf.h"
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080078
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070079#include "sound/compress_params.h"
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -080080#include "sound/asound.h"
ApurupaPattapu2e084df2013-12-18 15:47:59 -080081
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053082#ifdef DYNAMIC_LOG_ENABLED
83#include <log_xml_parser.h>
84#define LOG_MASK HAL_MOD_FILE_AUDIO_HW
85#include <log_utils.h>
86#endif
87
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070088#define COMPRESS_OFFLOAD_NUM_FRAGMENTS 4
Ashish Jain5106d362016-05-11 19:23:33 +053089/*DIRECT PCM has same buffer sizes as DEEP Buffer*/
90#define DIRECT_PCM_NUM_FRAGMENTS 2
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070091#define COMPRESS_PLAYBACK_VOLUME_MAX 0x2000
Arun Mirpuriebe78a72018-10-04 18:23:46 -070092#define MMAP_PLAYBACK_VOLUME_MAX 0x2000
Vikram Panduranga93f080e2017-06-07 18:16:14 -070093#define VOIP_PLAYBACK_VOLUME_MAX 0x2000
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +053094#define DSD_VOLUME_MIN_DB (-110)
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070095
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -070096#define PROXY_OPEN_RETRY_COUNT 100
97#define PROXY_OPEN_WAIT_TIME 20
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -080098
Mingming Yin08c7e312015-03-16 18:10:58 -070099#ifdef USE_LL_AS_PRIMARY_OUTPUT
100#define USECASE_AUDIO_PLAYBACK_PRIMARY USECASE_AUDIO_PLAYBACK_LOW_LATENCY
101#define PCM_CONFIG_AUDIO_PLAYBACK_PRIMARY pcm_config_low_latency
102#else
Haynes Mathew Georgebf143712013-12-03 13:02:53 -0800103#define USECASE_AUDIO_PLAYBACK_PRIMARY USECASE_AUDIO_PLAYBACK_DEEP_BUFFER
Mingming Yin08c7e312015-03-16 18:10:58 -0700104#define PCM_CONFIG_AUDIO_PLAYBACK_PRIMARY pcm_config_deep_buffer
105#endif
Haynes Mathew Georgebf143712013-12-03 13:02:53 -0800106
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700107#define ULL_PERIOD_SIZE (DEFAULT_OUTPUT_SAMPLING_RATE/1000)
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700108#define DEFAULT_VOIP_BUF_DURATION_MS 20
109#define DEFAULT_VOIP_BIT_DEPTH_BYTE sizeof(int16_t)
110#define DEFAULT_VOIP_SAMP_RATE 48000
111
112#define VOIP_IO_BUF_SIZE(SR, DURATION_MS, BIT_DEPTH) (SR)/1000 * DURATION_MS * BIT_DEPTH
113
114struct pcm_config default_pcm_config_voip_copp = {
115 .channels = 1,
116 .rate = DEFAULT_VOIP_SAMP_RATE, /* changed when the stream is opened */
117 .period_size = VOIP_IO_BUF_SIZE(DEFAULT_VOIP_SAMP_RATE, DEFAULT_VOIP_BUF_DURATION_MS, DEFAULT_VOIP_BIT_DEPTH_BYTE)/2,
118 .period_count = 2,
119 .format = PCM_FORMAT_S16_LE,
kunleiz95b597a2017-10-23 17:07:33 +0800120 .avail_min = VOIP_IO_BUF_SIZE(DEFAULT_VOIP_SAMP_RATE, DEFAULT_VOIP_BUF_DURATION_MS, DEFAULT_VOIP_BIT_DEPTH_BYTE)/2,
121 .stop_threshold = INT_MAX,
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700122};
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700123
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700124#define MIN_CHANNEL_COUNT 1
125#define DEFAULT_CHANNEL_COUNT 2
126#define MAX_HIFI_CHANNEL_COUNT 8
127
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -0700128static unsigned int configured_low_latency_capture_period_size =
129 LOW_LATENCY_CAPTURE_PERIOD_SIZE;
130
Haynes Mathew George16081042017-05-31 17:16:49 -0700131#define MMAP_PERIOD_SIZE (DEFAULT_OUTPUT_SAMPLING_RATE/1000)
132#define MMAP_PERIOD_COUNT_MIN 32
133#define MMAP_PERIOD_COUNT_MAX 512
134#define MMAP_PERIOD_COUNT_DEFAULT (MMAP_PERIOD_COUNT_MAX)
135
Eric Laurentb23d5282013-05-14 15:27:20 -0700136struct pcm_config pcm_config_deep_buffer = {
137 .channels = 2,
138 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
139 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
140 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
141 .format = PCM_FORMAT_S16_LE,
142 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
143 .stop_threshold = INT_MAX,
144 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
145};
146
147struct pcm_config pcm_config_low_latency = {
148 .channels = 2,
149 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
150 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
151 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
152 .format = PCM_FORMAT_S16_LE,
153 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
154 .stop_threshold = INT_MAX,
155 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
156};
157
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700158static int af_period_multiplier = 4;
159struct pcm_config pcm_config_rt = {
160 .channels = 2,
161 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
162 .period_size = ULL_PERIOD_SIZE, //1 ms
163 .period_count = 512, //=> buffer size is 512ms
164 .format = PCM_FORMAT_S16_LE,
165 .start_threshold = ULL_PERIOD_SIZE*8, //8ms
166 .stop_threshold = INT_MAX,
167 .silence_threshold = 0,
168 .silence_size = 0,
169 .avail_min = ULL_PERIOD_SIZE, //1 ms
170};
171
Eric Laurentb23d5282013-05-14 15:27:20 -0700172struct pcm_config pcm_config_hdmi_multi = {
173 .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
174 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
175 .period_size = HDMI_MULTI_PERIOD_SIZE,
176 .period_count = HDMI_MULTI_PERIOD_COUNT,
177 .format = PCM_FORMAT_S16_LE,
178 .start_threshold = 0,
179 .stop_threshold = INT_MAX,
180 .avail_min = 0,
181};
182
Haynes Mathew George16081042017-05-31 17:16:49 -0700183struct pcm_config pcm_config_mmap_playback = {
184 .channels = 2,
185 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
186 .period_size = MMAP_PERIOD_SIZE,
187 .period_count = MMAP_PERIOD_COUNT_DEFAULT,
188 .format = PCM_FORMAT_S16_LE,
189 .start_threshold = MMAP_PERIOD_SIZE*8,
190 .stop_threshold = INT32_MAX,
191 .silence_threshold = 0,
192 .silence_size = 0,
193 .avail_min = MMAP_PERIOD_SIZE, //1 ms
194};
195
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700196struct pcm_config pcm_config_hifi = {
197 .channels = DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
198 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
199 .period_size = HIFI_BUFFER_OUTPUT_PERIOD_SIZE, /* change #define */
200 .period_count = HIFI_BUFFER_OUTPUT_PERIOD_COUNT,
201 .format = PCM_FORMAT_S24_3LE,
202 .start_threshold = 0,
203 .stop_threshold = INT_MAX,
204 .avail_min = 0,
205};
206
Eric Laurentb23d5282013-05-14 15:27:20 -0700207struct pcm_config pcm_config_audio_capture = {
208 .channels = 2,
Eric Laurentb23d5282013-05-14 15:27:20 -0700209 .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
210 .format = PCM_FORMAT_S16_LE,
211};
212
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700213struct pcm_config pcm_config_audio_capture_rt = {
214 .channels = 2,
215 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
216 .period_size = ULL_PERIOD_SIZE,
217 .period_count = 512,
218 .format = PCM_FORMAT_S16_LE,
219 .start_threshold = 0,
220 .stop_threshold = INT_MAX,
221 .silence_threshold = 0,
222 .silence_size = 0,
223 .avail_min = ULL_PERIOD_SIZE, //1 ms
224};
225
Haynes Mathew George16081042017-05-31 17:16:49 -0700226struct pcm_config pcm_config_mmap_capture = {
227 .channels = 2,
228 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
229 .period_size = MMAP_PERIOD_SIZE,
230 .period_count = MMAP_PERIOD_COUNT_DEFAULT,
231 .format = PCM_FORMAT_S16_LE,
232 .start_threshold = 0,
233 .stop_threshold = INT_MAX,
234 .silence_threshold = 0,
235 .silence_size = 0,
236 .avail_min = MMAP_PERIOD_SIZE, //1 ms
237};
238
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -0700239#define AFE_PROXY_CHANNEL_COUNT 2
240#define AFE_PROXY_SAMPLING_RATE 48000
241
242#define AFE_PROXY_PLAYBACK_PERIOD_SIZE 768
243#define AFE_PROXY_PLAYBACK_PERIOD_COUNT 4
244
245struct pcm_config pcm_config_afe_proxy_playback = {
246 .channels = AFE_PROXY_CHANNEL_COUNT,
247 .rate = AFE_PROXY_SAMPLING_RATE,
248 .period_size = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
249 .period_count = AFE_PROXY_PLAYBACK_PERIOD_COUNT,
250 .format = PCM_FORMAT_S16_LE,
251 .start_threshold = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
252 .stop_threshold = INT_MAX,
253 .avail_min = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
254};
255
256#define AFE_PROXY_RECORD_PERIOD_SIZE 768
257#define AFE_PROXY_RECORD_PERIOD_COUNT 4
258
259struct pcm_config pcm_config_afe_proxy_record = {
260 .channels = AFE_PROXY_CHANNEL_COUNT,
261 .rate = AFE_PROXY_SAMPLING_RATE,
262 .period_size = AFE_PROXY_RECORD_PERIOD_SIZE,
263 .period_count = AFE_PROXY_RECORD_PERIOD_COUNT,
264 .format = PCM_FORMAT_S16_LE,
265 .start_threshold = AFE_PROXY_RECORD_PERIOD_SIZE,
266 .stop_threshold = INT_MAX,
267 .avail_min = AFE_PROXY_RECORD_PERIOD_SIZE,
268};
269
Ashish Jainf1eaa582016-05-23 20:54:24 +0530270#define AUDIO_MAX_PCM_FORMATS 7
271
272const uint32_t format_to_bitwidth_table[AUDIO_MAX_PCM_FORMATS] = {
273 [AUDIO_FORMAT_DEFAULT] = 0,
274 [AUDIO_FORMAT_PCM_16_BIT] = sizeof(uint16_t),
275 [AUDIO_FORMAT_PCM_8_BIT] = sizeof(uint8_t),
276 [AUDIO_FORMAT_PCM_32_BIT] = sizeof(uint32_t),
277 [AUDIO_FORMAT_PCM_8_24_BIT] = sizeof(uint32_t),
278 [AUDIO_FORMAT_PCM_FLOAT] = sizeof(float),
279 [AUDIO_FORMAT_PCM_24_BIT_PACKED] = sizeof(uint8_t) * 3,
280};
281
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800282const char * const use_case_table[AUDIO_USECASE_MAX] = {
Eric Laurentb23d5282013-05-14 15:27:20 -0700283 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
284 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
Ravi Kumar Alamanda474de5a2015-06-25 20:08:01 -0700285 [USECASE_AUDIO_PLAYBACK_ULL] = "audio-ull-playback",
286 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback",
Shruthi Krishnaace10852013-10-25 14:32:12 -0700287 [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback",
vivek mehta446c3962015-09-14 10:57:35 -0700288 //Enabled for Direct_PCM
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700289 [USECASE_AUDIO_PLAYBACK_OFFLOAD2] = "compress-offload-playback2",
290 [USECASE_AUDIO_PLAYBACK_OFFLOAD3] = "compress-offload-playback3",
291 [USECASE_AUDIO_PLAYBACK_OFFLOAD4] = "compress-offload-playback4",
292 [USECASE_AUDIO_PLAYBACK_OFFLOAD5] = "compress-offload-playback5",
293 [USECASE_AUDIO_PLAYBACK_OFFLOAD6] = "compress-offload-playback6",
294 [USECASE_AUDIO_PLAYBACK_OFFLOAD7] = "compress-offload-playback7",
295 [USECASE_AUDIO_PLAYBACK_OFFLOAD8] = "compress-offload-playback8",
296 [USECASE_AUDIO_PLAYBACK_OFFLOAD9] = "compress-offload-playback9",
Haynes Mathew George16081042017-05-31 17:16:49 -0700297 [USECASE_AUDIO_PLAYBACK_FM] = "play-fm",
298 [USECASE_AUDIO_PLAYBACK_MMAP] = "mmap-playback",
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700299 [USECASE_AUDIO_PLAYBACK_HIFI] = "hifi-playback",
vivek mehta0ea887a2015-08-26 14:01:20 -0700300
Eric Laurentb23d5282013-05-14 15:27:20 -0700301 [USECASE_AUDIO_RECORD] = "audio-record",
Mingming Yine62d7842013-10-25 16:26:03 -0700302 [USECASE_AUDIO_RECORD_COMPRESS] = "audio-record-compress",
Dhananjay Kumaree4d2002016-10-25 18:02:58 +0530303 [USECASE_AUDIO_RECORD_COMPRESS2] = "audio-record-compress2",
304 [USECASE_AUDIO_RECORD_COMPRESS3] = "audio-record-compress3",
305 [USECASE_AUDIO_RECORD_COMPRESS4] = "audio-record-compress4",
Dhananjay Kumar376e38b2017-09-28 22:26:23 +0530306 [USECASE_AUDIO_RECORD_COMPRESS5] = "audio-record-compress5",
307 [USECASE_AUDIO_RECORD_COMPRESS6] = "audio-record-compress6",
Eric Laurentb23d5282013-05-14 15:27:20 -0700308 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
Preetam Singh Ranawatde84f1a2013-11-01 14:58:16 -0700309 [USECASE_AUDIO_RECORD_FM_VIRTUAL] = "fm-virtual-record",
Haynes Mathew George16081042017-05-31 17:16:49 -0700310 [USECASE_AUDIO_RECORD_MMAP] = "mmap-record",
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700311 [USECASE_AUDIO_RECORD_HIFI] = "hifi-record",
Haynes Mathew George16081042017-05-31 17:16:49 -0700312
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -0800313 [USECASE_AUDIO_HFP_SCO] = "hfp-sco",
Vimal Puthanveed47e64852013-12-20 13:23:39 -0800314 [USECASE_AUDIO_HFP_SCO_WB] = "hfp-sco-wb",
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700315 [USECASE_VOICE_CALL] = "voice-call",
Mingming Yin3ee55c62014-08-04 14:23:35 -0700316
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700317 [USECASE_VOICE2_CALL] = "voice2-call",
318 [USECASE_VOLTE_CALL] = "volte-call",
319 [USECASE_QCHAT_CALL] = "qchat-call",
Vicky Sehrawat7e4fc152014-02-12 17:58:59 -0800320 [USECASE_VOWLAN_CALL] = "vowlan-call",
Vidyakumar Athota0e109352015-02-12 17:38:22 -0800321 [USECASE_VOICEMMODE1_CALL] = "voicemmode1-call",
322 [USECASE_VOICEMMODE2_CALL] = "voicemmode2-call",
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800323 [USECASE_COMPRESS_VOIP_CALL] = "compress-voip-call",
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700324 [USECASE_INCALL_REC_UPLINK] = "incall-rec-uplink",
325 [USECASE_INCALL_REC_DOWNLINK] = "incall-rec-downlink",
326 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK] = "incall-rec-uplink-and-downlink",
Helen Zenge56b4852013-12-03 16:54:40 -0800327 [USECASE_INCALL_REC_UPLINK_COMPRESS] = "incall-rec-uplink-compress",
328 [USECASE_INCALL_REC_DOWNLINK_COMPRESS] = "incall-rec-downlink-compress",
329 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK_COMPRESS] = "incall-rec-uplink-and-downlink-compress",
330
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -0700331 [USECASE_INCALL_MUSIC_UPLINK] = "incall_music_uplink",
332 [USECASE_INCALL_MUSIC_UPLINK2] = "incall_music_uplink2",
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700333 [USECASE_AUDIO_SPKR_CALIB_RX] = "spkr-rx-calib",
334 [USECASE_AUDIO_SPKR_CALIB_TX] = "spkr-vi-record",
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -0700335
336 [USECASE_AUDIO_PLAYBACK_AFE_PROXY] = "afe-proxy-playback",
337 [USECASE_AUDIO_RECORD_AFE_PROXY] = "afe-proxy-record",
Md Mansoor Ahmeddb1b4f92018-01-25 18:56:31 +0530338 [USECASE_AUDIO_PLAYBACK_SILENCE] = "silence-playback",
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700339
Siddartha Shaik31b530e2017-05-19 15:26:33 +0530340 /* Transcode loopback cases */
341 [USECASE_AUDIO_TRANSCODE_LOOPBACK] = "audio-transcode-loopback",
Vikram Panduranga93f080e2017-06-07 18:16:14 -0700342
343 [USECASE_AUDIO_PLAYBACK_VOIP] = "audio-playback-voip",
344 [USECASE_AUDIO_RECORD_VOIP] = "audio-record-voip",
Varun Balaraje49253e2017-07-06 19:48:56 +0530345 /* For Interactive Audio Streams */
346 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM1] = "audio-interactive-stream1",
347 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM2] = "audio-interactive-stream2",
348 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM3] = "audio-interactive-stream3",
349 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM4] = "audio-interactive-stream4",
350 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM5] = "audio-interactive-stream5",
351 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM6] = "audio-interactive-stream6",
352 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM7] = "audio-interactive-stream7",
353 [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM8] = "audio-interactive-stream8",
Garmond Leunge2433c32017-09-28 21:51:22 -0700354
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800355 [USECASE_AUDIO_EC_REF_LOOPBACK] = "ec-ref-audio-capture",
356
357 [USECASE_AUDIO_A2DP_ABR_FEEDBACK] = "a2dp-abr-feedback"
Eric Laurentb23d5282013-05-14 15:27:20 -0700358};
359
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700360static const audio_usecase_t offload_usecases[] = {
361 USECASE_AUDIO_PLAYBACK_OFFLOAD,
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700362 USECASE_AUDIO_PLAYBACK_OFFLOAD2,
363 USECASE_AUDIO_PLAYBACK_OFFLOAD3,
364 USECASE_AUDIO_PLAYBACK_OFFLOAD4,
365 USECASE_AUDIO_PLAYBACK_OFFLOAD5,
366 USECASE_AUDIO_PLAYBACK_OFFLOAD6,
367 USECASE_AUDIO_PLAYBACK_OFFLOAD7,
368 USECASE_AUDIO_PLAYBACK_OFFLOAD8,
369 USECASE_AUDIO_PLAYBACK_OFFLOAD9,
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700370};
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800371
Varun Balaraje49253e2017-07-06 19:48:56 +0530372static const audio_usecase_t interactive_usecases[] = {
373 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM1,
374 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM2,
375 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM3,
376 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM4,
377 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM5,
378 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM6,
379 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM7,
380 USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM8,
381};
382
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800383#define STRING_TO_ENUM(string) { #string, string }
384
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800385struct string_to_enum {
386 const char *name;
387 uint32_t value;
388};
389
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700390static const struct string_to_enum channels_name_to_enum_table[] = {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800391 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
Mingming Yin3a941d42016-02-17 18:08:05 -0800392 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_2POINT1),
393 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_QUAD),
394 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_SURROUND),
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -0700395 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_PENTA),
Mingming Yin3a941d42016-02-17 18:08:05 -0800396 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
397 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_6POINT1),
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800398 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700399 STRING_TO_ENUM(AUDIO_CHANNEL_IN_MONO),
400 STRING_TO_ENUM(AUDIO_CHANNEL_IN_STEREO),
401 STRING_TO_ENUM(AUDIO_CHANNEL_IN_FRONT_BACK),
402 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_1),
403 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_2),
404 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_3),
405 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_4),
406 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_5),
407 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_6),
408 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_7),
409 STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_8),
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800410};
411
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700412static const struct string_to_enum formats_name_to_enum_table[] = {
413 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
414 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
415 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
Pradnya Chaphekar8a9dcd82014-09-09 09:49:10 -0700416 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
417 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
418 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC),
Ben Romberger1aaaf862017-04-06 17:49:46 -0700419 STRING_TO_ENUM(AUDIO_FORMAT_DOLBY_TRUEHD),
Mingming Yin3a941d42016-02-17 18:08:05 -0800420 STRING_TO_ENUM(AUDIO_FORMAT_DTS),
421 STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD),
Naresh Tanniru928f0862017-04-07 16:44:23 -0700422 STRING_TO_ENUM(AUDIO_FORMAT_IEC61937)
Mingming Yin3a941d42016-02-17 18:08:05 -0800423};
424
425//list of all supported sample rates by HDMI specification.
426static const int out_hdmi_sample_rates[] = {
427 32000, 44100, 48000, 88200, 96000, 176400, 192000,
428};
429
Haynes Mathew George484e8d22017-07-31 18:55:17 -0700430static const struct string_to_enum out_sample_rates_name_to_enum_table[] = {
Mingming Yin3a941d42016-02-17 18:08:05 -0800431 STRING_TO_ENUM(32000),
432 STRING_TO_ENUM(44100),
433 STRING_TO_ENUM(48000),
434 STRING_TO_ENUM(88200),
435 STRING_TO_ENUM(96000),
436 STRING_TO_ENUM(176400),
437 STRING_TO_ENUM(192000),
Pradnya Chaphekar8a9dcd82014-09-09 09:49:10 -0700438};
439
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700440static struct audio_device *adev = NULL;
Ben Romberger02ab1192018-05-24 12:10:08 -0700441static pthread_mutex_t adev_init_lock = PTHREAD_MUTEX_INITIALIZER;
Kiran Kandi910e1862013-10-29 13:29:42 -0700442static unsigned int audio_device_ref_count;
vivek mehtab72d08d2016-04-29 03:16:47 -0700443//cache last MBDRC cal step level
444static int last_known_cal_step = -1 ;
Kiran Kandi910e1862013-10-29 13:29:42 -0700445
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +0530446static int check_a2dp_restore_l(struct audio_device *adev, struct stream_out *out, bool restore);
447static int out_set_compr_volume(struct audio_stream_out *stream, float left, float right);
Arun Mirpuriebe78a72018-10-04 18:23:46 -0700448static int out_set_mmap_volume(struct audio_stream_out *stream, float left, float right);
Zhou Song2b8f28f2017-09-11 10:51:38 +0800449static int out_set_voip_volume(struct audio_stream_out *stream, float left, float right);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +0530450
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700451static bool may_use_noirq_mode(struct audio_device *adev, audio_usecase_t uc_id,
452 int flags __unused)
453{
454 int dir = 0;
455 switch (uc_id) {
456 case USECASE_AUDIO_RECORD_LOW_LATENCY:
457 dir = 1;
458 case USECASE_AUDIO_PLAYBACK_ULL:
459 break;
460 default:
461 return false;
462 }
463
464 int dev_id = platform_get_pcm_device_id(uc_id, dir == 0 ?
465 PCM_PLAYBACK : PCM_CAPTURE);
466 if (adev->adm_is_noirq_avail)
467 return adev->adm_is_noirq_avail(adev->adm_data,
468 adev->snd_card, dev_id, dir);
469 return false;
470}
471
472static void register_out_stream(struct stream_out *out)
473{
474 struct audio_device *adev = out->dev;
475 if (is_offload_usecase(out->usecase) ||
476 !adev->adm_register_output_stream)
477 return;
478
479 // register stream first for backward compatibility
480 adev->adm_register_output_stream(adev->adm_data,
481 out->handle,
482 out->flags);
483
484 if (!adev->adm_set_config)
485 return;
486
487 if (out->realtime)
488 adev->adm_set_config(adev->adm_data,
489 out->handle,
490 out->pcm, &out->config);
491}
492
493static void register_in_stream(struct stream_in *in)
494{
495 struct audio_device *adev = in->dev;
496 if (!adev->adm_register_input_stream)
497 return;
498
499 adev->adm_register_input_stream(adev->adm_data,
500 in->capture_handle,
501 in->flags);
502
503 if (!adev->adm_set_config)
504 return;
505
506 if (in->realtime)
507 adev->adm_set_config(adev->adm_data,
508 in->capture_handle,
509 in->pcm,
510 &in->config);
511}
512
513static void request_out_focus(struct stream_out *out, long ns)
514{
515 struct audio_device *adev = out->dev;
516
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700517 if (adev->adm_request_focus_v2)
518 adev->adm_request_focus_v2(adev->adm_data, out->handle, ns);
519 else if (adev->adm_request_focus)
520 adev->adm_request_focus(adev->adm_data, out->handle);
521}
522
523static void request_in_focus(struct stream_in *in, long ns)
524{
525 struct audio_device *adev = in->dev;
526
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700527 if (adev->adm_request_focus_v2)
528 adev->adm_request_focus_v2(adev->adm_data, in->capture_handle, ns);
529 else if (adev->adm_request_focus)
530 adev->adm_request_focus(adev->adm_data, in->capture_handle);
531}
532
533static void release_out_focus(struct stream_out *out)
534{
535 struct audio_device *adev = out->dev;
536
537 if (adev->adm_abandon_focus)
538 adev->adm_abandon_focus(adev->adm_data, out->handle);
539}
540
541static void release_in_focus(struct stream_in *in)
542{
543 struct audio_device *adev = in->dev;
544 if (adev->adm_abandon_focus)
545 adev->adm_abandon_focus(adev->adm_data, in->capture_handle);
546}
547
Dhananjay Kumare6293dd2017-05-25 17:25:30 +0530548static int parse_snd_card_status(struct str_parms *parms, int *card,
549 card_status_t *status)
550{
551 char value[32]={0};
552 char state[32]={0};
553
554 int ret = str_parms_get_str(parms, "SND_CARD_STATUS", value, sizeof(value));
555 if (ret < 0)
556 return -1;
557
558 // sscanf should be okay as value is of max length 32.
559 // same as sizeof state.
560 if (sscanf(value, "%d,%s", card, state) < 2)
561 return -1;
562
563 *status = !strcmp(state, "ONLINE") ? CARD_STATUS_ONLINE :
564 CARD_STATUS_OFFLINE;
565 return 0;
566}
567
Haynes Mathew Georgeb0f5dc32017-10-06 18:35:12 -0700568static inline void adjust_frames_for_device_delay(struct stream_out *out,
569 uint32_t *dsp_frames) {
570 // Adjustment accounts for A2dp encoder latency with offload usecases
571 // Note: Encoder latency is returned in ms.
572 if (AUDIO_DEVICE_OUT_ALL_A2DP & out->devices) {
573 unsigned long offset =
574 (audio_extn_a2dp_get_encoder_latency() * out->sample_rate / 1000);
575 *dsp_frames = (*dsp_frames > offset) ? (*dsp_frames - offset) : 0;
576 }
577}
578
vivek mehtaa76401a2015-04-24 14:12:15 -0700579__attribute__ ((visibility ("default")))
580bool audio_hw_send_gain_dep_calibration(int level) {
581 bool ret_val = false;
vivek mehtab72d08d2016-04-29 03:16:47 -0700582 ALOGV("%s: called ...", __func__);
vivek mehtaa76401a2015-04-24 14:12:15 -0700583
584 pthread_mutex_lock(&adev_init_lock);
585
586 if (adev != NULL && adev->platform != NULL) {
587 pthread_mutex_lock(&adev->lock);
588 ret_val = platform_send_gain_dep_cal(adev->platform, level);
vivek mehtab72d08d2016-04-29 03:16:47 -0700589
Preetam Singh Ranawatf4ae0222017-05-31 17:07:28 +0530590 // cache level info for any of the use case which
591 // was not started.
592 last_known_cal_step = level;;
vivek mehtab72d08d2016-04-29 03:16:47 -0700593
vivek mehtaa76401a2015-04-24 14:12:15 -0700594 pthread_mutex_unlock(&adev->lock);
595 } else {
596 ALOGE("%s: %s is NULL", __func__, adev == NULL ? "adev" : "adev->platform");
597 }
598
599 pthread_mutex_unlock(&adev_init_lock);
600
601 return ret_val;
602}
603
Ashish Jain5106d362016-05-11 19:23:33 +0530604static int check_and_set_gapless_mode(struct audio_device *adev, bool enable_gapless)
605{
Krishnankutty Kolathappilly6d8788b2014-01-09 12:45:31 -0800606 bool gapless_enabled = false;
607 const char *mixer_ctl_name = "Compress Gapless Playback";
608 struct mixer_ctl *ctl;
609
610 ALOGV("%s:", __func__);
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700611 gapless_enabled = property_get_bool("vendor.audio.offload.gapless.enabled", false);
Ashish Jain5106d362016-05-11 19:23:33 +0530612
613 /*Disable gapless if its AV playback*/
614 gapless_enabled = gapless_enabled && enable_gapless;
Krishnankutty Kolathappilly6d8788b2014-01-09 12:45:31 -0800615
616 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
617 if (!ctl) {
618 ALOGE("%s: Could not get ctl for mixer cmd - %s",
619 __func__, mixer_ctl_name);
620 return -EINVAL;
621 }
622
623 if (mixer_ctl_set_value(ctl, 0, gapless_enabled) < 0) {
624 ALOGE("%s: Could not set gapless mode %d",
625 __func__, gapless_enabled);
626 return -EINVAL;
627 }
628 return 0;
629}
Haynes Mathew George5191a852013-09-11 14:19:36 -0700630
Aniket Kumar Lataf56b6402016-10-27 12:03:18 -0700631__attribute__ ((visibility ("default")))
632int audio_hw_get_gain_level_mapping(struct amp_db_and_gain_table *mapping_tbl,
633 int table_size) {
634 int ret_val = 0;
635 ALOGV("%s: enter ... ", __func__);
636
637 pthread_mutex_lock(&adev_init_lock);
638 if (adev == NULL) {
639 ALOGW("%s: adev is NULL .... ", __func__);
640 goto done;
641 }
642
643 pthread_mutex_lock(&adev->lock);
644 ret_val = platform_get_gain_level_mapping(mapping_tbl, table_size);
645 pthread_mutex_unlock(&adev->lock);
646done:
647 pthread_mutex_unlock(&adev_init_lock);
648 ALOGV("%s: exit ... ", __func__);
649 return ret_val;
650}
651
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700652static bool is_supported_format(audio_format_t format)
653{
Eric Laurent86e17132013-09-12 17:49:30 -0700654 if (format == AUDIO_FORMAT_MP3 ||
Satish Babu Patakokila0c313922016-12-08 12:07:08 +0530655 format == AUDIO_FORMAT_MP2 ||
Ashish Jainf9b78162014-08-25 20:36:25 +0530656 format == AUDIO_FORMAT_AAC_LC ||
657 format == AUDIO_FORMAT_AAC_HE_V1 ||
658 format == AUDIO_FORMAT_AAC_HE_V2 ||
Manish Dewangana6fc5442015-08-24 20:30:31 +0530659 format == AUDIO_FORMAT_AAC_ADTS_LC ||
660 format == AUDIO_FORMAT_AAC_ADTS_HE_V1 ||
661 format == AUDIO_FORMAT_AAC_ADTS_HE_V2 ||
Arun Kumar Dasari3b174182016-12-27 13:01:14 +0530662 format == AUDIO_FORMAT_AAC_LATM_LC ||
663 format == AUDIO_FORMAT_AAC_LATM_HE_V1 ||
664 format == AUDIO_FORMAT_AAC_LATM_HE_V2 ||
Ashish Jain5106d362016-05-11 19:23:33 +0530665 format == AUDIO_FORMAT_PCM_24_BIT_PACKED ||
666 format == AUDIO_FORMAT_PCM_8_24_BIT ||
Ashish Jainf1eaa582016-05-23 20:54:24 +0530667 format == AUDIO_FORMAT_PCM_FLOAT ||
668 format == AUDIO_FORMAT_PCM_32_BIT ||
vivek mehta0ea887a2015-08-26 14:01:20 -0700669 format == AUDIO_FORMAT_PCM_16_BIT ||
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530670 format == AUDIO_FORMAT_AC3 ||
671 format == AUDIO_FORMAT_E_AC3 ||
Ben Romberger1aaaf862017-04-06 17:49:46 -0700672 format == AUDIO_FORMAT_DOLBY_TRUEHD ||
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530673 format == AUDIO_FORMAT_DTS ||
674 format == AUDIO_FORMAT_DTS_HD ||
Weiyin Jiang18ac4e92015-03-15 15:03:40 +0800675 format == AUDIO_FORMAT_FLAC ||
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530676 format == AUDIO_FORMAT_ALAC ||
677 format == AUDIO_FORMAT_APE ||
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +0530678 format == AUDIO_FORMAT_DSD ||
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +0530679 format == AUDIO_FORMAT_VORBIS ||
Weiyin Jiang18ac4e92015-03-15 15:03:40 +0800680 format == AUDIO_FORMAT_WMA ||
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +0530681 format == AUDIO_FORMAT_WMA_PRO ||
Naresh Tanniru928f0862017-04-07 16:44:23 -0700682 format == AUDIO_FORMAT_APTX ||
683 format == AUDIO_FORMAT_IEC61937)
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -0800684 return true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700685
686 return false;
687}
688
Haynes Mathew George5beddd42016-06-27 18:33:40 -0700689static inline bool is_mmap_usecase(audio_usecase_t uc_id)
690{
691 return (uc_id == USECASE_AUDIO_RECORD_AFE_PROXY) ||
692 (uc_id == USECASE_AUDIO_PLAYBACK_AFE_PROXY);
693}
694
Avinash Vaish71a8b972014-07-24 15:36:33 +0530695static int enable_audio_route_for_voice_usecases(struct audio_device *adev,
696 struct audio_usecase *uc_info)
697{
698 struct listnode *node;
699 struct audio_usecase *usecase;
700
701 if (uc_info == NULL)
702 return -EINVAL;
703
704 /* Re-route all voice usecases on the shared backend other than the
705 specified usecase to new snd devices */
706 list_for_each(node, &adev->usecase_list) {
707 usecase = node_to_item(node, struct audio_usecase, list);
Narsinga Rao Chellae7f33c12015-01-28 15:23:34 -0800708 if ((usecase->type == VOICE_CALL) && (usecase != uc_info))
Avinash Vaish71a8b972014-07-24 15:36:33 +0530709 enable_audio_route(adev, usecase);
710 }
711 return 0;
712}
713
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +0530714static void enable_asrc_mode(struct audio_device *adev)
Preetam Singh Ranawatb0c0dd72016-08-18 00:32:06 +0530715{
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +0530716 ALOGV("%s", __func__);
717 audio_route_apply_and_update_path(adev->audio_route,
718 "asrc-mode");
719 adev->asrc_mode_enabled = true;
720}
721
722static void disable_asrc_mode(struct audio_device *adev)
723{
724 ALOGV("%s", __func__);
725 audio_route_reset_and_update_path(adev->audio_route,
726 "asrc-mode");
727 adev->asrc_mode_enabled = false;
728}
729
730/*
731 * - Enable ASRC mode for incoming mix path use case(Headphone backend)if Headphone
732 * 44.1 or Native DSD backends are enabled for any of current use case.
733 * e.g. 48-> + (Naitve DSD or Headphone 44.1)
734 * - Disable current mix path use case(Headphone backend) and re-enable it with
735 * ASRC mode for incoming Headphone 44.1 or Native DSD use case.
736 * e.g. Naitve DSD or Headphone 44.1 -> + 48
737 */
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +0530738static void check_and_set_asrc_mode(struct audio_device *adev,
739 struct audio_usecase *uc_info,
740 snd_device_t snd_device)
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +0530741{
742 ALOGV("%s snd device %d", __func__, snd_device);
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +0530743 int i, num_new_devices = 0;
744 snd_device_t split_new_snd_devices[SND_DEVICE_OUT_END];
745 /*
746 *Split snd device for new combo use case
747 *e.g. Headphopne 44.1-> + Ringtone (Headphone + Speaker)
748 */
749 if (platform_split_snd_device(adev->platform,
750 snd_device,
751 &num_new_devices,
752 split_new_snd_devices) == 0) {
753 for (i = 0; i < num_new_devices; i++)
754 check_and_set_asrc_mode(adev, uc_info, split_new_snd_devices[i]);
755 } else {
756 int new_backend_idx = platform_get_backend_index(snd_device);
757 if (((new_backend_idx == HEADPHONE_BACKEND) ||
758 (new_backend_idx == HEADPHONE_44_1_BACKEND) ||
759 (new_backend_idx == DSD_NATIVE_BACKEND)) &&
760 !adev->asrc_mode_enabled) {
761 struct listnode *node = NULL;
762 struct audio_usecase *uc = NULL;
763 struct stream_out *curr_out = NULL;
764 int usecase_backend_idx = DEFAULT_CODEC_BACKEND;
765 int i, num_devices, ret = 0;
766 snd_device_t split_snd_devices[SND_DEVICE_OUT_END];
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +0530767
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +0530768 list_for_each(node, &adev->usecase_list) {
769 uc = node_to_item(node, struct audio_usecase, list);
770 curr_out = (struct stream_out*) uc->stream.out;
771 if (curr_out && PCM_PLAYBACK == uc->type && uc != uc_info) {
772 /*
773 *Split snd device for existing combo use case
774 *e.g. Ringtone (Headphone + Speaker) + Headphopne 44.1
775 */
776 ret = platform_split_snd_device(adev->platform,
777 uc->out_snd_device,
778 &num_devices,
779 split_snd_devices);
780 if (ret < 0 || num_devices == 0) {
781 ALOGV("%s: Unable to split uc->out_snd_device: %d",__func__, uc->out_snd_device);
782 split_snd_devices[0] = uc->out_snd_device;
783 num_devices = 1;
Garmond Leung50058f62017-02-08 09:49:30 -0800784 }
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +0530785 for (i = 0; i < num_devices; i++) {
786 usecase_backend_idx = platform_get_backend_index(split_snd_devices[i]);
787 ALOGD("%s:snd_dev %d usecase_backend_idx %d",__func__, split_snd_devices[i],usecase_backend_idx);
788 if((new_backend_idx == HEADPHONE_BACKEND) &&
789 ((usecase_backend_idx == HEADPHONE_44_1_BACKEND) ||
790 (usecase_backend_idx == DSD_NATIVE_BACKEND))) {
791 ALOGD("%s:DSD or native stream detected enabling asrcmode in hardware",
792 __func__);
793 enable_asrc_mode(adev);
794 break;
795 } else if(((new_backend_idx == HEADPHONE_44_1_BACKEND) ||
796 (new_backend_idx == DSD_NATIVE_BACKEND)) &&
797 (usecase_backend_idx == HEADPHONE_BACKEND)) {
798 ALOGD("%s:48K stream detected, disabling and enabling it with asrcmode in hardware",
799 __func__);
800 disable_audio_route(adev, uc);
801 disable_snd_device(adev, uc->out_snd_device);
802 // Apply true-high-quality-mode if DSD or > 44.1KHz or >=24-bit
803 if (new_backend_idx == DSD_NATIVE_BACKEND)
804 audio_route_apply_and_update_path(adev->audio_route,
805 "hph-true-highquality-mode");
806 else if ((new_backend_idx == HEADPHONE_44_1_BACKEND) &&
807 (curr_out->bit_width >= 24))
808 audio_route_apply_and_update_path(adev->audio_route,
809 "hph-highquality-mode");
810 enable_asrc_mode(adev);
811 enable_snd_device(adev, uc->out_snd_device);
812 enable_audio_route(adev, uc);
813 break;
814 }
815 }
816 // reset split devices count
817 num_devices = 0;
Garmond Leung50058f62017-02-08 09:49:30 -0800818 }
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +0530819 if (adev->asrc_mode_enabled)
820 break;
Preetam Singh Ranawatb0c0dd72016-08-18 00:32:06 +0530821 }
822 }
823 }
824}
825
Vikram Pandurangadf59cae2017-08-03 18:04:55 -0700826#ifdef DYNAMIC_ECNS_ENABLED
827static int send_effect_enable_disable_mixer_ctl(struct audio_device *adev,
828 struct audio_effect_config effect_config,
829 unsigned int param_value)
830{
831 char mixer_ctl_name[] = "Audio Effect";
832 struct mixer_ctl *ctl;
833 long set_values[6];
834 struct stream_in *in = adev->active_input;
835
836 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
837 if (!ctl) {
838 ALOGE("%s: Could not get mixer ctl - %s",
839 __func__, mixer_ctl_name);
840 return -EINVAL;
841 }
842
843 set_values[0] = 1; //0:Rx 1:Tx
844 set_values[1] = in->app_type_cfg.app_type;
845 set_values[2] = (long)effect_config.module_id;
846 set_values[3] = (long)effect_config.instance_id;
847 set_values[4] = (long)effect_config.param_id;
848 set_values[5] = param_value;
849
850 mixer_ctl_set_array(ctl, set_values, ARRAY_SIZE(set_values));
851
852 return 0;
853
854}
855
856static int update_effect_param_ecns(struct audio_device *adev, unsigned int module_id,
857 int effect_type, unsigned int *param_value)
858{
859 int ret = 0;
860 struct audio_effect_config other_effect_config;
861 struct audio_usecase *usecase = NULL;
862 struct stream_in *in = adev->active_input;
863
864 usecase = get_usecase_from_list(adev, in->usecase);
865 if (!usecase)
866 return -EINVAL;
867
868 ret = platform_get_effect_config_data(usecase->in_snd_device, &other_effect_config,
869 effect_type == EFFECT_AEC ? EFFECT_NS : EFFECT_AEC);
870 if (ret < 0) {
871 ALOGE("%s Failed to get effect params %d", __func__, ret);
872 return ret;
873 }
874
875 if (module_id == other_effect_config.module_id) {
876 //Same module id for AEC/NS. Values need to be combined
877 if (((effect_type == EFFECT_AEC) && (in->enable_ns)) ||
878 ((effect_type == EFFECT_NS) && (in->enable_aec))) {
879 *param_value |= other_effect_config.param_value;
880 }
881 }
882
883 return ret;
884}
885
886static int enable_disable_effect(struct audio_device *adev, int effect_type, bool enable)
887{
888 struct audio_effect_config effect_config;
889 struct audio_usecase *usecase = NULL;
890 int ret = 0;
891 unsigned int param_value = 0;
892 struct stream_in *in = adev->active_input;
893
894 if (!in) {
895 ALOGE("%s: Invalid input stream", __func__);
896 return -EINVAL;
897 }
898
899 ALOGD("%s: effect_type:%d enable:%d", __func__, effect_type, enable);
900
901 usecase = get_usecase_from_list(adev, in->usecase);
Weiyin Jiang20d3fa62018-08-01 18:06:27 +0800902 if (usecase == NULL) {
903 ALOGE("%s: Could not find the usecase (%d) in the list",
904 __func__, in->usecase);
905 return -EINVAL;
906 }
Vikram Pandurangadf59cae2017-08-03 18:04:55 -0700907
908 ret = platform_get_effect_config_data(usecase->in_snd_device, &effect_config, effect_type);
909 if (ret < 0) {
910 ALOGE("%s Failed to get module id %d", __func__, ret);
911 return ret;
912 }
913 ALOGV("%s: %d %d usecase->id:%d usecase->in_snd_device:%d", __func__, effect_config.module_id,
914 in->app_type_cfg.app_type, usecase->id, usecase->in_snd_device);
915
916 if(enable)
917 param_value = effect_config.param_value;
918
919 /*Special handling for AEC & NS effects Param values need to be
920 updated if module ids are same*/
921
922 if ((effect_type == EFFECT_AEC) || (effect_type == EFFECT_NS)) {
923 ret = update_effect_param_ecns(adev, effect_config.module_id, effect_type, &param_value);
924 if (ret < 0)
925 return ret;
926 }
927
928 ret = send_effect_enable_disable_mixer_ctl(adev, effect_config, param_value);
929
930 return ret;
931}
932
933static void check_and_enable_effect(struct audio_device *adev)
934{
935
936 if (adev->active_input->enable_aec) {
937 enable_disable_effect(adev, EFFECT_AEC, true);
938 }
939
940 if (adev->active_input->enable_ns &&
941 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
942 enable_disable_effect(adev, EFFECT_NS, true);
943 }
944}
945#else
946#define enable_disable_effect(x, y, z) ENOSYS
947#define check_and_enable_effect(x) ENOSYS
948#endif
949
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -0700950int pcm_ioctl(struct pcm *pcm, int request, ...)
951{
952 va_list ap;
953 void * arg;
954 int pcm_fd = *(int*)pcm;
955
956 va_start(ap, request);
957 arg = va_arg(ap, void *);
958 va_end(ap);
959
960 return ioctl(pcm_fd, request, arg);
961}
962
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700963int enable_audio_route(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700964 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800965{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700966 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700967 char mixer_path[MIXER_PATH_MAX_LENGTH];
Manish Dewangan58229382017-02-02 15:48:41 +0530968 struct stream_out *out = NULL;
Soumya Managolie7651c42018-06-28 16:04:57 +0530969 int ret = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800970
971 if (usecase == NULL)
972 return -EINVAL;
973
974 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
975
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800976 if (usecase->type == PCM_CAPTURE)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700977 snd_device = usecase->in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800978 else
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700979 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800980
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -0800981#ifdef DS1_DOLBY_DAP_ENABLED
982 audio_extn_dolby_set_dmid(adev);
983 audio_extn_dolby_set_endpoint(adev);
984#endif
Pradnya Chaphekar8a9dcd82014-09-09 09:49:10 -0700985 audio_extn_dolby_ds2_set_endpoint(adev);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700986 audio_extn_sound_trigger_update_stream_status(usecase, ST_EVENT_STREAM_BUSY);
Dhananjay Kumar45b71742014-05-29 21:47:27 +0530987 audio_extn_listen_update_stream_status(usecase, LISTEN_EVENT_STREAM_BUSY);
Ben Romberger1fafdde2015-09-09 19:43:15 -0700988 audio_extn_utils_send_app_type_cfg(adev, usecase);
Dhananjay Kumar14170dd2015-08-28 13:24:16 +0530989 audio_extn_utils_send_audio_calibration(adev, usecase);
Manish Dewangan58229382017-02-02 15:48:41 +0530990 if ((usecase->type == PCM_PLAYBACK) && is_offload_usecase(usecase->id)) {
991 out = usecase->stream.out;
992 if (out && out->compr)
993 audio_extn_utils_compress_set_clk_rec_mode(usecase);
994 }
Dhananjay Kumar2a7f75b2018-12-10 22:26:53 +0530995 audio_extn_set_custom_mtmx_params(adev, usecase, true);
Manish Dewangan58229382017-02-02 15:48:41 +0530996
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800997 strlcpy(mixer_path, use_case_table[usecase->id], MIXER_PATH_MAX_LENGTH);
Banajit Goswami20cdd212015-09-11 01:11:30 -0700998 platform_add_backend_name(mixer_path, snd_device, usecase);
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -0700999 ALOGD("%s: apply mixer and update path: %s", __func__, mixer_path);
Soumya Managolie7651c42018-06-28 16:04:57 +05301000 ret = audio_route_apply_and_update_path(adev->audio_route, mixer_path);
1001 if (!ret && usecase->id == USECASE_AUDIO_PLAYBACK_FM) {
1002 struct str_parms *parms = str_parms_create_str("fm_restore_volume=1");
1003 if (parms) {
1004 audio_extn_fm_set_parameters(adev, parms);
1005 str_parms_destroy(parms);
1006 }
1007 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001008 ALOGV("%s: exit", __func__);
1009 return 0;
1010}
1011
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001012int disable_audio_route(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001013 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001014{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001015 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001016 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001017
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05301018 if (usecase == NULL || usecase->id == USECASE_INVALID)
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001019 return -EINVAL;
1020
1021 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001022 if (usecase->type == PCM_CAPTURE)
1023 snd_device = usecase->in_snd_device;
1024 else
1025 snd_device = usecase->out_snd_device;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -08001026 strlcpy(mixer_path, use_case_table[usecase->id], MIXER_PATH_MAX_LENGTH);
Banajit Goswami20cdd212015-09-11 01:11:30 -07001027 platform_add_backend_name(mixer_path, snd_device, usecase);
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07001028 ALOGD("%s: reset and update mixer path: %s", __func__, mixer_path);
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07001029 audio_route_reset_and_update_path(adev->audio_route, mixer_path);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001030 audio_extn_sound_trigger_update_stream_status(usecase, ST_EVENT_STREAM_FREE);
Dhananjay Kumar45b71742014-05-29 21:47:27 +05301031 audio_extn_listen_update_stream_status(usecase, LISTEN_EVENT_STREAM_FREE);
Dhananjay Kumar2a7f75b2018-12-10 22:26:53 +05301032 audio_extn_set_custom_mtmx_params(adev, usecase, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001033 ALOGV("%s: exit", __func__);
1034 return 0;
1035}
1036
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -07001037int enable_snd_device(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001038 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001039{
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301040 int i, num_devices = 0;
1041 snd_device_t new_snd_devices[SND_DEVICE_OUT_END];
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001042 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
1043
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001044 if (snd_device < SND_DEVICE_MIN ||
1045 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001046 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001047 return -EINVAL;
1048 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001049
1050 adev->snd_dev_ref_cnt[snd_device]++;
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001051
1052 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0 ) {
1053 ALOGE("%s: Invalid sound device returned", __func__);
1054 return -EINVAL;
1055 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001056 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
Eric Laurent994a6932013-07-17 11:51:42 -07001057 ALOGV("%s: snd_device(%d: %s) is already active",
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001058 __func__, snd_device, device_name);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001059 return 0;
1060 }
1061
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301062
Gopikrishnaiah Anandane85d0462014-06-30 21:41:20 -07001063 if (audio_extn_spkr_prot_is_enabled())
1064 audio_extn_spkr_prot_calib_cancel(adev);
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -07001065
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001066 if (platform_can_enable_spkr_prot_on_device(snd_device) &&
1067 audio_extn_spkr_prot_is_enabled()) {
1068 if (platform_get_spkr_prot_acdb_id(snd_device) < 0) {
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001069 adev->snd_dev_ref_cnt[snd_device]--;
1070 return -EINVAL;
1071 }
Ravit Dennisaaee49c2015-02-04 21:26:22 +02001072 audio_extn_dev_arbi_acquire(snd_device);
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001073 if (audio_extn_spkr_prot_start_processing(snd_device)) {
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -08001074 ALOGE("%s: spkr_start_processing failed", __func__);
Ravit Dennisaaee49c2015-02-04 21:26:22 +02001075 audio_extn_dev_arbi_release(snd_device);
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -08001076 return -EINVAL;
1077 }
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001078 } else if (platform_split_snd_device(adev->platform,
1079 snd_device,
1080 &num_devices,
1081 new_snd_devices) == 0) {
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301082 for (i = 0; i < num_devices; i++) {
1083 enable_snd_device(adev, new_snd_devices[i]);
1084 }
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -08001085 } else {
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07001086 ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, device_name);
Naresh Tannirucd2353e2016-08-19 00:37:25 +05301087
Preetam Singh Ranawatf1d417c2017-01-10 17:00:32 +05301088
Naresh Tannirucd2353e2016-08-19 00:37:25 +05301089 if ((SND_DEVICE_OUT_BT_A2DP == snd_device) &&
1090 (audio_extn_a2dp_start_playback() < 0)) {
1091 ALOGE(" fail to configure A2dp control path ");
1092 return -EINVAL;
1093 }
1094
Bharath Ramachandramurthy0de16782014-03-28 21:34:33 -07001095 /* due to the possibility of calibration overwrite between listen
1096 and audio, notify listen hal before audio calibration is sent */
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001097 audio_extn_sound_trigger_update_device_status(snd_device,
1098 ST_EVENT_SND_DEVICE_BUSY);
Dhananjay Kumar45b71742014-05-29 21:47:27 +05301099 audio_extn_listen_update_device_status(snd_device,
1100 LISTEN_EVENT_SND_DEVICE_BUSY);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001101 if (platform_get_snd_device_acdb_id(snd_device) < 0) {
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -07001102 adev->snd_dev_ref_cnt[snd_device]--;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001103 audio_extn_sound_trigger_update_device_status(snd_device,
1104 ST_EVENT_SND_DEVICE_FREE);
Dhananjay Kumar45b71742014-05-29 21:47:27 +05301105 audio_extn_listen_update_device_status(snd_device,
1106 LISTEN_EVENT_SND_DEVICE_FREE);
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -07001107 return -EINVAL;
1108 }
Lior Barenboim0b61bc72014-05-13 13:01:37 +03001109 audio_extn_dev_arbi_acquire(snd_device);
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07001110 audio_route_apply_and_update_path(adev->audio_route, device_name);
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301111
1112 if (SND_DEVICE_OUT_HEADPHONES == snd_device &&
1113 !adev->native_playback_enabled &&
1114 audio_is_true_native_stream_active(adev)) {
1115 ALOGD("%s: %d: napb: enabling native mode in hardware",
1116 __func__, __LINE__);
1117 audio_route_apply_and_update_path(adev->audio_route,
1118 "true-native-mode");
1119 adev->native_playback_enabled = true;
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +05301120 }
Dhanalakshmi Siddani0f1dfd52019-01-09 12:38:13 +05301121 if (((snd_device == SND_DEVICE_IN_HANDSET_6MIC) ||
1122 (snd_device == SND_DEVICE_IN_HANDSET_QMIC)) &&
Garmond Leunge2433c32017-09-28 21:51:22 -07001123 (audio_extn_ffv_get_stream() == adev->active_input)) {
1124 ALOGD("%s: init ec ref loopback", __func__);
1125 audio_extn_ffv_init_ec_ref_loopback(adev, snd_device);
1126 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001127 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001128 return 0;
1129}
1130
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001131int disable_snd_device(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001132 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001133{
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301134 int i, num_devices = 0;
1135 snd_device_t new_snd_devices[SND_DEVICE_OUT_END];
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001136 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
1137
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001138 if (snd_device < SND_DEVICE_MIN ||
1139 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001140 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001141 return -EINVAL;
1142 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001143 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
1144 ALOGE("%s: device ref cnt is already 0", __func__);
1145 return -EINVAL;
1146 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001147
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001148 adev->snd_dev_ref_cnt[snd_device]--;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -07001149
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001150 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0) {
1151 ALOGE("%s: Invalid sound device returned", __func__);
1152 return -EINVAL;
1153 }
1154
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001155 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07001156 ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, device_name);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301157
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001158 if (platform_can_enable_spkr_prot_on_device(snd_device) &&
1159 audio_extn_spkr_prot_is_enabled()) {
Anish Kumar46c7b872014-09-09 01:49:44 -07001160 audio_extn_spkr_prot_stop_processing(snd_device);
Vignesh Kulothungan3b5fae52017-09-25 12:16:30 -07001161
1162 // when speaker device is disabled, reset swap.
1163 // will be renabled on usecase start
1164 platform_set_swap_channels(adev, false);
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001165 } else if (platform_split_snd_device(adev->platform,
1166 snd_device,
1167 &num_devices,
1168 new_snd_devices) == 0) {
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301169 for (i = 0; i < num_devices; i++) {
1170 disable_snd_device(adev, new_snd_devices[i]);
1171 }
Lior Barenboim0b61bc72014-05-13 13:01:37 +03001172 } else {
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07001173 audio_route_reset_and_update_path(adev->audio_route, device_name);
Lior Barenboim0b61bc72014-05-13 13:01:37 +03001174 }
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001175
Naresh Tannirucd2353e2016-08-19 00:37:25 +05301176 if (SND_DEVICE_OUT_BT_A2DP == snd_device)
1177 audio_extn_a2dp_stop_playback();
1178
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07001179 if (snd_device == SND_DEVICE_OUT_HDMI || snd_device == SND_DEVICE_OUT_DISPLAY_PORT)
Ashish Jain81eb2a82015-05-13 10:52:34 +05301180 adev->is_channel_status_set = false;
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301181 else if (SND_DEVICE_OUT_HEADPHONES == snd_device &&
1182 adev->native_playback_enabled) {
1183 ALOGD("%s: %d: napb: disabling native mode in hardware",
1184 __func__, __LINE__);
1185 audio_route_reset_and_update_path(adev->audio_route,
1186 "true-native-mode");
1187 adev->native_playback_enabled = false;
Preetam Singh Ranawatb0c0dd72016-08-18 00:32:06 +05301188 } else if (SND_DEVICE_OUT_HEADPHONES == snd_device &&
1189 adev->asrc_mode_enabled) {
1190 ALOGD("%s: %d: disabling asrc mode in hardware", __func__, __LINE__);
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +05301191 disable_asrc_mode(adev);
1192 audio_route_apply_and_update_path(adev->audio_route, "hph-lowpower-mode");
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301193 }
Garmond Leunge2433c32017-09-28 21:51:22 -07001194 if ((snd_device == SND_DEVICE_IN_HANDSET_6MIC) &&
1195 (audio_extn_ffv_get_stream() == adev->active_input)) {
1196 ALOGD("%s: deinit ec ref loopback", __func__);
1197 audio_extn_ffv_deinit_ec_ref_loopback(adev, snd_device);
1198 }
Aniket Kumar Lata1be15992018-10-15 12:12:59 -07001199 audio_extn_utils_release_snd_device(snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001200 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001201
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001202 return 0;
1203}
1204
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001205/*
1206 legend:
1207 uc - existing usecase
1208 new_uc - new usecase
1209 d1, d11, d2 - SND_DEVICE enums
1210 a1, a2 - corresponding ANDROID device enums
1211 B1, B2 - backend strings
1212
1213case 1
1214 uc->dev d1 (a1) B1
1215 new_uc->dev d1 (a1), d2 (a2) B1, B2
1216
1217 resolution: disable and enable uc->dev on d1
1218
1219case 2
1220 uc->dev d1 (a1) B1
1221 new_uc->dev d11 (a1) B1
1222
1223 resolution: need to switch uc since d1 and d11 are related
1224 (e.g. speaker and voice-speaker)
1225 use ANDROID_DEVICE_OUT enums to match devices since SND_DEVICE enums may vary
1226
1227case 3
1228 uc->dev d1 (a1) B1
1229 new_uc->dev d2 (a2) B2
1230
1231 resolution: no need to switch uc
1232
1233case 4
1234 uc->dev d1 (a1) B1
1235 new_uc->dev d2 (a2) B1
1236
1237 resolution: disable enable uc-dev on d2 since backends match
1238 we cannot enable two streams on two different devices if they
1239 share the same backend. e.g. if offload is on speaker device using
1240 QUAD_MI2S backend and a low-latency stream is started on voice-handset
1241 using the same backend, offload must also be switched to voice-handset.
1242
1243case 5
1244 uc->dev d1 (a1) B1
1245 new_uc->dev d1 (a1), d2 (a2) B1
1246
1247 resolution: disable enable uc-dev on d2 since backends match
1248 we cannot enable two streams on two different devices if they
1249 share the same backend.
1250
1251case 6
1252 uc->dev d1 (a1) B1
1253 new_uc->dev d2 (a1) B2
1254
1255 resolution: no need to switch
1256
1257case 7
1258 uc->dev d1 (a1), d2 (a2) B1, B2
1259 new_uc->dev d1 (a1) B1
1260
1261 resolution: no need to switch
1262
Zhou Song4ba65882018-07-09 14:48:07 +08001263case 8
1264 uc->dev d1 (a1) B1
1265 new_uc->dev d11 (a1), d2 (a2) B1, B2
1266 resolution: compared to case 1, for this case, d1 and d11 are related
1267 then need to do the same as case 2 to siwtch to new uc
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001268*/
1269static snd_device_t derive_playback_snd_device(void * platform,
1270 struct audio_usecase *uc,
1271 struct audio_usecase *new_uc,
1272 snd_device_t new_snd_device)
1273{
Siddartha Shaik31b530e2017-05-19 15:26:33 +05301274 audio_devices_t a1, a2;
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001275
1276 snd_device_t d1 = uc->out_snd_device;
1277 snd_device_t d2 = new_snd_device;
1278
Siddartha Shaik31b530e2017-05-19 15:26:33 +05301279 switch (uc->type) {
1280 case TRANSCODE_LOOPBACK :
1281 a1 = uc->stream.inout->out_config.devices;
1282 a2 = new_uc->stream.inout->out_config.devices;
1283 break;
1284 default :
1285 a1 = uc->stream.out->devices;
1286 a2 = new_uc->stream.out->devices;
1287 break;
1288 }
1289
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001290 // Treat as a special case when a1 and a2 are not disjoint
1291 if ((a1 != a2) && (a1 & a2)) {
1292 snd_device_t d3[2];
1293 int num_devices = 0;
1294 int ret = platform_split_snd_device(platform,
1295 popcount(a1) > 1 ? d1 : d2,
1296 &num_devices,
1297 d3);
1298 if (ret < 0) {
1299 if (ret != -ENOSYS) {
1300 ALOGW("%s failed to split snd_device %d",
1301 __func__,
1302 popcount(a1) > 1 ? d1 : d2);
1303 }
1304 goto end;
1305 }
1306
1307 // NB: case 7 is hypothetical and isn't a practical usecase yet.
1308 // But if it does happen, we need to give priority to d2 if
1309 // the combo devices active on the existing usecase share a backend.
1310 // This is because we cannot have a usecase active on a combo device
1311 // and a new usecase requests one device in this combo pair.
1312 if (platform_check_backends_match(d3[0], d3[1])) {
1313 return d2; // case 5
1314 } else {
Zhou Song671be042018-08-27 15:33:52 +08001315 // check if d1 is related to any of d3's OR
1316 // old uc is combo device but new_uc is one of the combo device
1317 if (d1 == d3[0] || d1 == d3[1] || popcount(a1) > 1)
1318 return d1; // case 1, 7
Zhou Song4ba65882018-07-09 14:48:07 +08001319 else
1320 return d3[1]; // case 8
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001321 }
1322 } else {
1323 if (platform_check_backends_match(d1, d2)) {
1324 return d2; // case 2, 4
1325 } else {
1326 return d1; // case 6, 3
1327 }
1328 }
1329
1330end:
1331 return d2; // return whatever was calculated before.
1332}
1333
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001334static void check_usecases_codec_backend(struct audio_device *adev,
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301335 struct audio_usecase *uc_info,
1336 snd_device_t snd_device)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001337{
1338 struct listnode *node;
1339 struct audio_usecase *usecase;
1340 bool switch_device[AUDIO_USECASE_MAX];
Chaithanya Krishna Bacharaju49e7db02017-03-14 11:57:26 +05301341 snd_device_t uc_derive_snd_device;
1342 snd_device_t derive_snd_device[AUDIO_USECASE_MAX];
Aniket Kumar Lata25b344d2018-09-25 19:00:35 -07001343 snd_device_t split_snd_devices[SND_DEVICE_OUT_END];
1344 int i, num_uc_to_switch = 0, num_devices = 0;
kunleiz5cd52b82016-11-07 17:22:52 +08001345 int status = 0;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301346 bool force_restart_session = false;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001347 /*
1348 * This function is to make sure that all the usecases that are active on
1349 * the hardware codec backend are always routed to any one device that is
1350 * handled by the hardware codec.
1351 * For example, if low-latency and deep-buffer usecases are currently active
1352 * on speaker and out_set_parameters(headset) is received on low-latency
1353 * output, then we have to make sure deep-buffer is also switched to headset,
1354 * because of the limitation that both the devices cannot be enabled
1355 * at the same time as they share the same backend.
1356 */
Mingming Yin3ee55c62014-08-04 14:23:35 -07001357 /*
1358 * This call is to check if we need to force routing for a particular stream
1359 * If there is a backend configuration change for the device when a
1360 * new stream starts, then ADM needs to be closed and re-opened with the new
1361 * configuraion. This call check if we need to re-route all the streams
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -08001362 * associated with the backend. Touch tone + 24 bit + native playback.
Mingming Yin3ee55c62014-08-04 14:23:35 -07001363 */
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -08001364 bool force_routing = platform_check_and_set_codec_backend_cfg(adev, uc_info,
1365 snd_device);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301366 /* For a2dp device reconfigure all active sessions
1367 * with new AFE encoder format based on a2dp state
1368 */
1369 if ((SND_DEVICE_OUT_BT_A2DP == snd_device ||
1370 SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP == snd_device) &&
1371 audio_extn_a2dp_is_force_device_switch()) {
1372 force_routing = true;
1373 force_restart_session = true;
1374 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301375 ALOGD("%s:becf: force routing %d", __func__, force_routing);
1376
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001377 /* Disable all the usecases on the shared backend other than the
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -08001378 * specified usecase.
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -08001379 */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001380 for (i = 0; i < AUDIO_USECASE_MAX; i++)
1381 switch_device[i] = false;
1382
1383 list_for_each(node, &adev->usecase_list) {
1384 usecase = node_to_item(node, struct audio_usecase, list);
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -08001385
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301386 ALOGD("%s:becf: (%d) check_usecases curr device: %s, usecase device:%s "
1387 "backends match %d",__func__, i,
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301388 platform_get_snd_device_name(snd_device),
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301389 platform_get_snd_device_name(usecase->out_snd_device),
1390 platform_check_backends_match(snd_device, usecase->out_snd_device));
Ashish Jain6a65b352017-03-21 17:24:40 +05301391 if ((usecase->type != PCM_CAPTURE) && (usecase != uc_info)) {
1392 uc_derive_snd_device = derive_playback_snd_device(adev->platform,
1393 usecase, uc_info, snd_device);
1394 if (((uc_derive_snd_device != usecase->out_snd_device) || force_routing) &&
1395 ((usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) ||
1396 (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) ||
1397 (usecase->devices & AUDIO_DEVICE_OUT_USB_DEVICE) ||
Garmond Leung2fd4f122018-06-02 16:04:53 -07001398 (usecase->devices & AUDIO_DEVICE_OUT_USB_HEADSET) ||
Ashish Jain6a65b352017-03-21 17:24:40 +05301399 (usecase->devices & AUDIO_DEVICE_OUT_ALL_A2DP) ||
1400 (usecase->devices & AUDIO_DEVICE_OUT_ALL_SCO)) &&
1401 ((force_restart_session) ||
1402 (platform_check_backends_match(snd_device, usecase->out_snd_device)))) {
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301403 ALOGD("%s:becf: check_usecases (%s) is active on (%s) - disabling ..",
1404 __func__, use_case_table[usecase->id],
1405 platform_get_snd_device_name(usecase->out_snd_device));
1406 disable_audio_route(adev, usecase);
1407 switch_device[usecase->id] = true;
Chaithanya Krishna Bacharaju49e7db02017-03-14 11:57:26 +05301408 /* Enable existing usecase on derived playback device */
1409 derive_snd_device[usecase->id] = uc_derive_snd_device;
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301410 num_uc_to_switch++;
Ashish Jain6a65b352017-03-21 17:24:40 +05301411 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001412 }
1413 }
1414
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301415 ALOGD("%s:becf: check_usecases num.of Usecases to switch %d", __func__,
1416 num_uc_to_switch);
1417
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001418 if (num_uc_to_switch) {
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07001419 /* All streams have been de-routed. Disable the device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001420
Venkata Narendra Kumar Gutta7610e632014-04-14 23:16:38 +05301421 /* Make sure the previous devices to be disabled first and then enable the
1422 selected devices */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001423 list_for_each(node, &adev->usecase_list) {
1424 usecase = node_to_item(node, struct audio_usecase, list);
1425 if (switch_device[usecase->id]) {
Aniket Kumar Lata1be15992018-10-15 12:12:59 -07001426 /* Check if output sound device to be switched can be split and if any
Aniket Kumar Lata25b344d2018-09-25 19:00:35 -07001427 of the split devices match with derived sound device */
Aniket Kumar Lata1be15992018-10-15 12:12:59 -07001428 if (platform_split_snd_device(adev->platform, usecase->out_snd_device,
1429 &num_devices, split_snd_devices) == 0) {
1430 adev->snd_dev_ref_cnt[usecase->out_snd_device]--;
1431 if (adev->snd_dev_ref_cnt[usecase->out_snd_device] == 0) {
1432 ALOGD("%s: disabling snd_device(%d)", __func__, usecase->out_snd_device);
1433 for (i = 0; i < num_devices; i++) {
1434 /* Disable devices that do not match with derived sound device */
1435 if (split_snd_devices[i] != derive_snd_device[usecase->id])
1436 disable_snd_device(adev, split_snd_devices[i]);
Aniket Kumar Lata25b344d2018-09-25 19:00:35 -07001437 }
Aniket Kumar Lata1be15992018-10-15 12:12:59 -07001438 audio_extn_utils_release_snd_device(usecase->out_snd_device);
Aniket Kumar Lata25b344d2018-09-25 19:00:35 -07001439 }
1440 } else {
1441 disable_snd_device(adev, usecase->out_snd_device);
1442 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001443 }
1444 }
1445
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -07001446 list_for_each(node, &adev->usecase_list) {
1447 usecase = node_to_item(node, struct audio_usecase, list);
1448 if (switch_device[usecase->id]) {
Aniket Kumar Lata1be15992018-10-15 12:12:59 -07001449 if (platform_split_snd_device(adev->platform, usecase->out_snd_device,
1450 &num_devices, split_snd_devices) == 0) {
1451 /* Enable derived sound device only if it does not match with
1452 one of the split sound devices. This is because the matching
1453 sound device was not disabled */
1454 bool should_enable = true;
1455 for (i = 0; i < num_devices; i++) {
1456 if (derive_snd_device[usecase->id] == split_snd_devices[i]) {
1457 should_enable = false;
1458 break;
1459 }
1460 }
1461 if (should_enable)
1462 enable_snd_device(adev, derive_snd_device[usecase->id]);
1463 } else {
1464 enable_snd_device(adev, derive_snd_device[usecase->id]);
1465 }
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -07001466 }
1467 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001468
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001469 /* Re-route all the usecases on the shared backend other than the
1470 specified usecase to new snd devices */
1471 list_for_each(node, &adev->usecase_list) {
1472 usecase = node_to_item(node, struct audio_usecase, list);
Karthik Reddy Katta3abfee22016-02-23 10:55:27 +05301473 /* Update the out_snd_device only before enabling the audio route */
1474 if (switch_device[usecase->id]) {
Chaithanya Krishna Bacharaju49e7db02017-03-14 11:57:26 +05301475 usecase->out_snd_device = derive_snd_device[usecase->id];
Karthik Reddy Katta3abfee22016-02-23 10:55:27 +05301476 if (usecase->type != VOICE_CALL) {
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301477 ALOGD("%s:becf: enabling usecase (%s) on (%s)", __func__,
Karthik Reddy Katta3abfee22016-02-23 10:55:27 +05301478 use_case_table[usecase->id],
1479 platform_get_snd_device_name(usecase->out_snd_device));
kunleiz5cd52b82016-11-07 17:22:52 +08001480 /* Update voc calibration before enabling VoIP route */
1481 if (usecase->type == VOIP_CALL)
1482 status = platform_switch_voice_call_device_post(adev->platform,
1483 usecase->out_snd_device,
kunleizab514ca2017-01-09 14:59:50 +08001484 platform_get_input_snd_device(adev->platform, uc_info->devices));
Avinash Vaish71a8b972014-07-24 15:36:33 +05301485 enable_audio_route(adev, usecase);
kunleiz46c47dd2018-03-19 16:28:09 +08001486 if (usecase->id == USECASE_AUDIO_PLAYBACK_VOIP) {
1487 out_set_voip_volume(&usecase->stream.out->stream,
1488 usecase->stream.out->volume_l,
1489 usecase->stream.out->volume_r);
1490 }
Karthik Reddy Katta3abfee22016-02-23 10:55:27 +05301491 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001492 }
1493 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001494 }
1495}
1496
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05301497static void check_usecases_capture_codec_backend(struct audio_device *adev,
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001498 struct audio_usecase *uc_info,
1499 snd_device_t snd_device)
1500{
1501 struct listnode *node;
1502 struct audio_usecase *usecase;
1503 bool switch_device[AUDIO_USECASE_MAX];
1504 int i, num_uc_to_switch = 0;
Dhanalakshmi Siddanib678a802016-12-03 11:51:41 +05301505 int backend_check_cond = AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND;
kunleiz5cd52b82016-11-07 17:22:52 +08001506 int status = 0;
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001507
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05301508 bool force_routing = platform_check_and_set_capture_codec_backend_cfg(adev, uc_info,
1509 snd_device);
1510 ALOGD("%s:becf: force routing %d", __func__, force_routing);
Dhanalakshmi Siddanib678a802016-12-03 11:51:41 +05301511
1512 /*
1513 * Make sure out devices is checked against out codec backend device and
1514 * also in devices against in codec backend. Checking out device against in
1515 * codec backend or vice versa causes issues.
1516 */
1517 if (uc_info->type == PCM_CAPTURE)
1518 backend_check_cond = AUDIO_DEVICE_IN_ALL_CODEC_BACKEND;
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001519 /*
1520 * This function is to make sure that all the active capture usecases
1521 * are always routed to the same input sound device.
1522 * For example, if audio-record and voice-call usecases are currently
1523 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
1524 * is received for voice call then we have to make sure that audio-record
1525 * usecase is also switched to earpiece i.e. voice-dmic-ef,
1526 * because of the limitation that two devices cannot be enabled
1527 * at the same time if they share the same backend.
1528 */
1529 for (i = 0; i < AUDIO_USECASE_MAX; i++)
1530 switch_device[i] = false;
1531
1532 list_for_each(node, &adev->usecase_list) {
1533 usecase = node_to_item(node, struct audio_usecase, list);
Dhanalakshmi Siddanib678a802016-12-03 11:51:41 +05301534 /*
1535 * TODO: Enhance below condition to handle BT sco/USB multi recording
1536 */
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -08001537 if (usecase->type != PCM_PLAYBACK &&
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001538 usecase != uc_info &&
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05301539 (usecase->in_snd_device != snd_device || force_routing) &&
Dhanalakshmi Siddanib678a802016-12-03 11:51:41 +05301540 ((uc_info->devices & backend_check_cond) &&
Karthik Reddy Katta0d471762015-08-20 13:36:07 +05301541 (((usecase->devices & ~AUDIO_DEVICE_BIT_IN) & AUDIO_DEVICE_IN_ALL_CODEC_BACKEND) ||
Satya Krishna Pindiproli458b5a72017-03-13 15:14:24 +05301542 (usecase->type == VOIP_CALL))) &&
Anish Kumara020a7c2014-10-17 11:13:22 -07001543 (usecase->id != USECASE_AUDIO_SPKR_CALIB_TX)) {
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001544 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
1545 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -07001546 platform_get_snd_device_name(usecase->in_snd_device));
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001547 disable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001548 switch_device[usecase->id] = true;
1549 num_uc_to_switch++;
1550 }
1551 }
1552
1553 if (num_uc_to_switch) {
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07001554 /* All streams have been de-routed. Disable the device */
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001555
Venkata Narendra Kumar Gutta7610e632014-04-14 23:16:38 +05301556 /* Make sure the previous devices to be disabled first and then enable the
1557 selected devices */
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001558 list_for_each(node, &adev->usecase_list) {
1559 usecase = node_to_item(node, struct audio_usecase, list);
1560 if (switch_device[usecase->id]) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001561 disable_snd_device(adev, usecase->in_snd_device);
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -08001562 }
1563 }
1564
1565 list_for_each(node, &adev->usecase_list) {
1566 usecase = node_to_item(node, struct audio_usecase, list);
1567 if (switch_device[usecase->id]) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001568 enable_snd_device(adev, snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001569 }
1570 }
1571
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001572 /* Re-route all the usecases on the shared backend other than the
1573 specified usecase to new snd devices */
1574 list_for_each(node, &adev->usecase_list) {
1575 usecase = node_to_item(node, struct audio_usecase, list);
1576 /* Update the in_snd_device only before enabling the audio route */
1577 if (switch_device[usecase->id] ) {
1578 usecase->in_snd_device = snd_device;
kunleiz5cd52b82016-11-07 17:22:52 +08001579 if (usecase->type != VOICE_CALL) {
1580 /* Update voc calibration before enabling VoIP route */
1581 if (usecase->type == VOIP_CALL)
1582 status = platform_switch_voice_call_device_post(adev->platform,
Zhou Song557e7282017-05-05 17:18:18 +08001583 platform_get_output_snd_device(adev->platform, uc_info->stream.out),
kunleiz5cd52b82016-11-07 17:22:52 +08001584 usecase->in_snd_device);
Avinash Vaish71a8b972014-07-24 15:36:33 +05301585 enable_audio_route(adev, usecase);
kunleiz5cd52b82016-11-07 17:22:52 +08001586 }
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001587 }
1588 }
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001589 }
1590}
1591
Mingming Yin3a941d42016-02-17 18:08:05 -08001592static void reset_hdmi_sink_caps(struct stream_out *out) {
1593 int i = 0;
1594
1595 for (i = 0; i<= MAX_SUPPORTED_CHANNEL_MASKS; i++) {
1596 out->supported_channel_masks[i] = 0;
1597 }
1598 for (i = 0; i<= MAX_SUPPORTED_FORMATS; i++) {
1599 out->supported_formats[i] = 0;
1600 }
1601 for (i = 0; i<= MAX_SUPPORTED_SAMPLE_RATES; i++) {
1602 out->supported_sample_rates[i] = 0;
1603 }
1604}
1605
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001606/* must be called with hw device mutex locked */
Mingming Yin3a941d42016-02-17 18:08:05 -08001607static int read_hdmi_sink_caps(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001608{
Mingming Yin3a941d42016-02-17 18:08:05 -08001609 int ret = 0, i = 0, j = 0;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07001610 int channels = platform_edid_get_max_channels(out->dev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001611
Mingming Yin3a941d42016-02-17 18:08:05 -08001612 reset_hdmi_sink_caps(out);
1613
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07001614 /* Cache ext disp type */
Garmond Leung37850ab2016-10-06 11:42:18 -07001615 if (platform_get_ext_disp_type(adev->platform) <= 0) {
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07001616 ALOGE("%s: Failed to query disp type, ret:%d", __func__, ret);
Garmond Leung37850ab2016-10-06 11:42:18 -07001617 return -EINVAL;
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07001618 }
1619
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001620 switch (channels) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001621 case 8:
Mingming Yin3a941d42016-02-17 18:08:05 -08001622 ALOGV("%s: HDMI supports 7.1 channels", __func__);
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -07001623 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_7POINT1;
Mingming Yin3a941d42016-02-17 18:08:05 -08001624 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_6POINT1;
1625 case 6:
1626 ALOGV("%s: HDMI supports 5.1 channels", __func__);
1627 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_5POINT1;
1628 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_PENTA;
1629 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_QUAD;
1630 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_SURROUND;
1631 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_2POINT1;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001632 break;
1633 default:
Mingming Yin3a941d42016-02-17 18:08:05 -08001634 ALOGE("invalid/nonstandard channal count[%d]",channels);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001635 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001636 break;
1637 }
Mingming Yin3a941d42016-02-17 18:08:05 -08001638
1639 // check channel format caps
1640 i = 0;
1641 if (platform_is_edid_supported_format(out->dev->platform, AUDIO_FORMAT_AC3)) {
1642 ALOGV(":%s HDMI supports AC3/EAC3 formats", __func__);
1643 out->supported_formats[i++] = AUDIO_FORMAT_AC3;
1644 //Adding EAC3/EAC3_JOC formats if AC3 is supported by the sink.
1645 //EAC3/EAC3_JOC will be converted to AC3 for decoding if needed
1646 out->supported_formats[i++] = AUDIO_FORMAT_E_AC3;
1647 out->supported_formats[i++] = AUDIO_FORMAT_E_AC3_JOC;
1648 }
1649
Ben Romberger1aaaf862017-04-06 17:49:46 -07001650 if (platform_is_edid_supported_format(out->dev->platform, AUDIO_FORMAT_DOLBY_TRUEHD)) {
1651 ALOGV(":%s HDMI supports TRUE HD format", __func__);
1652 out->supported_formats[i++] = AUDIO_FORMAT_DOLBY_TRUEHD;
1653 }
1654
Mingming Yin3a941d42016-02-17 18:08:05 -08001655 if (platform_is_edid_supported_format(out->dev->platform, AUDIO_FORMAT_DTS)) {
1656 ALOGV(":%s HDMI supports DTS format", __func__);
1657 out->supported_formats[i++] = AUDIO_FORMAT_DTS;
1658 }
1659
1660 if (platform_is_edid_supported_format(out->dev->platform, AUDIO_FORMAT_DTS_HD)) {
1661 ALOGV(":%s HDMI supports DTS HD format", __func__);
1662 out->supported_formats[i++] = AUDIO_FORMAT_DTS_HD;
1663 }
1664
Naresh Tanniru928f0862017-04-07 16:44:23 -07001665 if (platform_is_edid_supported_format(out->dev->platform, AUDIO_FORMAT_IEC61937)) {
1666 ALOGV(":%s HDMI supports IEC61937 format", __func__);
1667 out->supported_formats[i++] = AUDIO_FORMAT_IEC61937;
1668 }
1669
Mingming Yin3a941d42016-02-17 18:08:05 -08001670
1671 // check sample rate caps
1672 i = 0;
1673 for (j = 0; j < MAX_SUPPORTED_SAMPLE_RATES; j++) {
1674 if (platform_is_edid_supported_sample_rate(out->dev->platform, out_hdmi_sample_rates[j])) {
1675 ALOGV(":%s HDMI supports sample rate:%d", __func__, out_hdmi_sample_rates[j]);
1676 out->supported_sample_rates[i++] = out_hdmi_sample_rates[j];
1677 }
1678 }
1679
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001680 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001681}
1682
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001683static inline ssize_t read_usb_sup_sample_rates(bool is_playback __unused,
1684 uint32_t *supported_sample_rates __unused,
1685 uint32_t max_rates __unused)
1686{
1687 ssize_t count = audio_extn_usb_get_sup_sample_rates(is_playback,
1688 supported_sample_rates,
1689 max_rates);
Ashish Jain4847e9d2017-08-17 19:16:57 +05301690 ssize_t i = 0;
1691
1692 for (i=0; i<count; i++) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001693 ALOGV("%s %s %d", __func__, is_playback ? "P" : "C",
1694 supported_sample_rates[i]);
1695 }
1696 return count;
1697}
1698
1699static inline int read_usb_sup_channel_masks(bool is_playback,
1700 audio_channel_mask_t *supported_channel_masks,
1701 uint32_t max_masks)
1702{
1703 int channels = audio_extn_usb_get_max_channels(is_playback);
1704 int channel_count;
1705 uint32_t num_masks = 0;
1706 if (channels > MAX_HIFI_CHANNEL_COUNT)
1707 channels = MAX_HIFI_CHANNEL_COUNT;
1708
Lakshman Chaluvaraju90ddcaa2018-11-21 10:24:37 +05301709 channel_count = DEFAULT_CHANNEL_COUNT;
1710
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001711 if (is_playback) {
1712 // For playback we never report mono because the framework always outputs stereo
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001713 // audio_channel_out_mask_from_count() does return positional masks for channel counts
Lakshman Chaluvaraju90ddcaa2018-11-21 10:24:37 +05301714 // above 2 but we want indexed masks here.
1715 supported_channel_masks[num_masks++] = audio_channel_out_mask_from_count(channel_count);
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001716 } else {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001717 // audio_channel_in_mask_from_count() does the right conversion to either positional or
1718 // indexed mask
Lakshman Chaluvaraju90ddcaa2018-11-21 10:24:37 +05301719 supported_channel_masks[num_masks++] = audio_channel_in_mask_from_count(channel_count);
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001720 }
Lakshman Chaluvaraju90ddcaa2018-11-21 10:24:37 +05301721
1722 for (channel_count = channels; ((channel_count >= DEFAULT_CHANNEL_COUNT) &&
1723 (num_masks < max_masks)); channel_count--) {
1724 supported_channel_masks[num_masks++] =
1725 audio_channel_mask_for_index_assignment_from_count(channel_count);
1726 }
1727
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001728 ALOGV("%s: %s supported ch %d supported_channel_masks[0] %08x num_masks %d", __func__,
1729 is_playback ? "P" : "C", channels, supported_channel_masks[0], num_masks);
1730 return num_masks;
1731}
1732
1733static inline int read_usb_sup_formats(bool is_playback __unused,
1734 audio_format_t *supported_formats,
1735 uint32_t max_formats __unused)
1736{
1737 int bitwidth = audio_extn_usb_get_max_bit_width(is_playback);
1738 switch (bitwidth) {
1739 case 24:
1740 // XXX : usb.c returns 24 for s24 and s24_le?
1741 supported_formats[0] = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1742 break;
1743 case 32:
1744 supported_formats[0] = AUDIO_FORMAT_PCM_32_BIT;
1745 break;
1746 case 16:
1747 default :
1748 supported_formats[0] = AUDIO_FORMAT_PCM_16_BIT;
1749 break;
1750 }
1751 ALOGV("%s: %s supported format %d", __func__,
1752 is_playback ? "P" : "C", bitwidth);
1753 return 1;
1754}
1755
1756static inline int read_usb_sup_params_and_compare(bool is_playback,
1757 audio_format_t *format,
1758 audio_format_t *supported_formats,
1759 uint32_t max_formats,
1760 audio_channel_mask_t *mask,
1761 audio_channel_mask_t *supported_channel_masks,
1762 uint32_t max_masks,
1763 uint32_t *rate,
1764 uint32_t *supported_sample_rates,
1765 uint32_t max_rates) {
1766 int ret = 0;
1767 int num_formats;
1768 int num_masks;
1769 int num_rates;
1770 int i;
1771
1772 num_formats = read_usb_sup_formats(is_playback, supported_formats,
1773 max_formats);
1774 num_masks = read_usb_sup_channel_masks(is_playback, supported_channel_masks,
1775 max_masks);
1776
1777 num_rates = read_usb_sup_sample_rates(is_playback,
1778 supported_sample_rates, max_rates);
1779
1780#define LUT(table, len, what, dflt) \
1781 for (i=0; i<len && (table[i] != what); i++); \
1782 if (i==len) { ret |= (what == dflt ? 0 : -1); what=table[0]; }
1783
1784 LUT(supported_formats, num_formats, *format, AUDIO_FORMAT_DEFAULT);
1785 LUT(supported_channel_masks, num_masks, *mask, AUDIO_CHANNEL_NONE);
1786 LUT(supported_sample_rates, num_rates, *rate, 0);
1787
1788#undef LUT
1789 return ret < 0 ? -EINVAL : 0; // HACK TBD
1790}
1791
Alexy Josephb1379942016-01-29 15:49:38 -08001792audio_usecase_t get_usecase_id_from_usecase_type(const struct audio_device *adev,
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -08001793 usecase_type_t type)
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001794{
1795 struct audio_usecase *usecase;
1796 struct listnode *node;
1797
1798 list_for_each(node, &adev->usecase_list) {
1799 usecase = node_to_item(node, struct audio_usecase, list);
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -08001800 if (usecase->type == type) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001801 ALOGV("%s: usecase id %d", __func__, usecase->id);
1802 return usecase->id;
1803 }
1804 }
1805 return USECASE_INVALID;
1806}
1807
Alexy Josephb1379942016-01-29 15:49:38 -08001808struct audio_usecase *get_usecase_from_list(const struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001809 audio_usecase_t uc_id)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001810{
1811 struct audio_usecase *usecase;
1812 struct listnode *node;
1813
1814 list_for_each(node, &adev->usecase_list) {
1815 usecase = node_to_item(node, struct audio_usecase, list);
1816 if (usecase->id == uc_id)
1817 return usecase;
1818 }
1819 return NULL;
1820}
1821
Dhananjay Kumard4833242016-10-06 22:09:12 +05301822struct stream_in *get_next_active_input(const struct audio_device *adev)
1823{
1824 struct audio_usecase *usecase;
1825 struct listnode *node;
1826
1827 list_for_each_reverse(node, &adev->usecase_list) {
1828 usecase = node_to_item(node, struct audio_usecase, list);
1829 if (usecase->type == PCM_CAPTURE)
1830 return usecase->stream.in;
1831 }
1832 return NULL;
1833}
1834
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301835/*
1836 * is a true native playback active
1837 */
1838bool audio_is_true_native_stream_active(struct audio_device *adev)
1839{
1840 bool active = false;
1841 int i = 0;
1842 struct listnode *node;
1843
1844 if (NATIVE_AUDIO_MODE_TRUE_44_1 != platform_get_native_support()) {
1845 ALOGV("%s:napb: not in true mode or non hdphones device",
1846 __func__);
1847 active = false;
1848 goto exit;
1849 }
1850
1851 list_for_each(node, &adev->usecase_list) {
1852 struct audio_usecase *uc;
1853 uc = node_to_item(node, struct audio_usecase, list);
1854 struct stream_out *curr_out =
1855 (struct stream_out*) uc->stream.out;
1856
1857 if (curr_out && PCM_PLAYBACK == uc->type) {
1858 ALOGD("%s:napb: (%d) (%s)id (%d) sr %d bw "
1859 "(%d) device %s", __func__, i++, use_case_table[uc->id],
1860 uc->id, curr_out->sample_rate,
1861 curr_out->bit_width,
1862 platform_get_snd_device_name(uc->out_snd_device));
1863
1864 if (is_offload_usecase(uc->id) &&
1865 (curr_out->sample_rate == OUTPUT_SAMPLING_RATE_44100)) {
1866 active = true;
1867 ALOGD("%s:napb:native stream detected", __func__);
1868 }
1869 }
1870 }
1871exit:
1872 return active;
1873}
1874
Xiaojun Sang785b5da2017-08-03 15:52:29 +08001875uint32_t adev_get_dsp_bit_width_enforce_mode()
1876{
1877 if (adev == NULL) {
1878 ALOGE("%s: adev is null. Disable DSP bit width enforce mode.\n", __func__);
1879 return 0;
1880 }
1881 return adev->dsp_bit_width_enforce_mode;
1882}
1883
1884static uint32_t adev_init_dsp_bit_width_enforce_mode(struct mixer *mixer)
1885{
1886 char value[PROPERTY_VALUE_MAX];
1887 int trial;
1888 uint32_t dsp_bit_width_enforce_mode = 0;
1889
1890 if (!mixer) {
1891 ALOGE("%s: adev mixer is null. cannot update DSP bitwidth.\n",
1892 __func__);
1893 return 0;
1894 }
1895
1896 if (property_get("persist.vendor.audio_hal.dsp_bit_width_enforce_mode",
1897 value, NULL) > 0) {
1898 trial = atoi(value);
1899 switch (trial) {
1900 case 16:
1901 dsp_bit_width_enforce_mode = 16;
1902 break;
1903 case 24:
1904 dsp_bit_width_enforce_mode = 24;
1905 break;
1906 case 32:
1907 dsp_bit_width_enforce_mode = 32;
1908 break;
1909 default:
1910 dsp_bit_width_enforce_mode = 0;
1911 ALOGD("%s Dynamic DSP bitwidth config is disabled.", __func__);
1912 break;
1913 }
1914 }
1915
1916 return dsp_bit_width_enforce_mode;
1917}
1918
1919static void audio_enable_asm_bit_width_enforce_mode(struct mixer *mixer,
1920 uint32_t enforce_mode,
1921 bool enable)
1922{
1923 struct mixer_ctl *ctl = NULL;
1924 const char *mixer_ctl_name = "ASM Bit Width";
1925 uint32_t asm_bit_width_mode = 0;
1926
1927 if (enforce_mode == 0) {
1928 ALOGD("%s: DSP bitwidth feature is disabled.", __func__);
1929 return;
1930 }
1931
1932 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
1933 if (!ctl) {
1934 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1935 __func__, mixer_ctl_name);
1936 return;
1937 }
1938
1939 if (enable)
1940 asm_bit_width_mode = enforce_mode;
1941 else
1942 asm_bit_width_mode = 0;
1943
1944 ALOGV("%s DSP bit width feature status is %d width=%d",
1945 __func__, enable, asm_bit_width_mode);
1946 if (mixer_ctl_set_value(ctl, 0, asm_bit_width_mode) < 0)
1947 ALOGE("%s: Could not set ASM biwidth %d", __func__,
1948 asm_bit_width_mode);
1949
1950 return;
1951}
1952
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05301953/*
1954 * if native DSD playback active
1955 */
1956bool audio_is_dsd_native_stream_active(struct audio_device *adev)
1957{
1958 bool active = false;
1959 struct listnode *node = NULL;
1960 struct audio_usecase *uc = NULL;
1961 struct stream_out *curr_out = NULL;
1962
1963 list_for_each(node, &adev->usecase_list) {
1964 uc = node_to_item(node, struct audio_usecase, list);
1965 curr_out = (struct stream_out*) uc->stream.out;
1966
1967 if (curr_out && PCM_PLAYBACK == uc->type &&
1968 (DSD_NATIVE_BACKEND == platform_get_backend_index(uc->out_snd_device))) {
1969 active = true;
1970 ALOGV("%s:DSD playback is active", __func__);
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05301971 break;
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05301972 }
1973 }
1974 return active;
1975}
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301976
1977static bool force_device_switch(struct audio_usecase *usecase)
1978{
1979 bool ret = false;
1980 bool is_it_true_mode = false;
1981
Zhou Song30f2c3e2018-02-08 14:02:15 +08001982 if (usecase->type == PCM_CAPTURE ||
1983 usecase->type == TRANSCODE_LOOPBACK) {
1984 return false;
1985 }
1986
Aalique Grahamecbc46a22017-10-05 10:30:23 -07001987 if(usecase->stream.out == NULL) {
1988 ALOGE("%s: stream.out is NULL", __func__);
1989 return false;
1990 }
1991
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301992 if (is_offload_usecase(usecase->id) &&
Xiaojun Sang869f2012016-02-23 16:33:07 +08001993 (usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) &&
1994 (usecase->stream.out->devices == AUDIO_DEVICE_OUT_WIRED_HEADSET ||
1995 usecase->stream.out->devices == AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) {
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301996 is_it_true_mode = (NATIVE_AUDIO_MODE_TRUE_44_1 == platform_get_native_support()? true : false);
1997 if ((is_it_true_mode && !adev->native_playback_enabled) ||
1998 (!is_it_true_mode && adev->native_playback_enabled)){
1999 ret = true;
2000 ALOGD("napb: time to toggle native mode");
2001 }
2002 }
2003
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302004 // Force all a2dp output devices to reconfigure for proper AFE encode format
Ashish Jainc597d102016-12-12 10:31:34 +05302005 //Also handle a case where in earlier a2dp start failed as A2DP stream was
2006 //in suspended state, hence try to trigger a retry when we again get a routing request.
Aalique Grahamecbc46a22017-10-05 10:30:23 -07002007 if((usecase->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) &&
Ashish Jainc597d102016-12-12 10:31:34 +05302008 audio_extn_a2dp_is_force_device_switch()) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302009 ALOGD("Force a2dp device switch to update new encoder config");
2010 ret = true;
2011 }
2012
Manish Dewangan671a4202017-08-18 17:30:46 +05302013 if (usecase->stream.out->stream_config_changed) {
2014 ALOGD("Force stream_config_changed to update iec61937 transmission config");
2015 return true;
2016 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302017 return ret;
2018}
2019
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302020bool is_btsco_device(snd_device_t out_snd_device, snd_device_t in_snd_device)
2021{
2022 bool ret=false;
2023 if ((out_snd_device == SND_DEVICE_OUT_BT_SCO ||
2024 out_snd_device == SND_DEVICE_OUT_BT_SCO_WB) ||
2025 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_WB_NREC ||
2026 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_WB ||
2027 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_NREC ||
2028 in_snd_device == SND_DEVICE_IN_BT_SCO_MIC)
2029 ret = true;
2030
2031 return ret;
2032}
2033
2034bool is_a2dp_device(snd_device_t out_snd_device)
2035{
2036 bool ret=false;
2037 if (out_snd_device == SND_DEVICE_OUT_BT_A2DP)
2038 ret = true;
2039
2040 return ret;
2041}
2042
2043bool is_bt_soc_on(struct audio_device *adev)
2044{
2045 struct mixer_ctl *ctl;
2046 char *mixer_ctl_name = "BT SOC status";
2047 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
2048 bool bt_soc_status = true;
2049 if (!ctl) {
2050 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2051 __func__, mixer_ctl_name);
2052 /*This is to ensure we dont break targets which dont have the kernel change*/
2053 return true;
2054 }
2055 bt_soc_status = mixer_ctl_get_value(ctl, 0);
2056 ALOGD("BT SOC status: %d",bt_soc_status);
2057 return bt_soc_status;
2058}
2059
2060int out_standby_l(struct audio_stream *stream);
2061
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002062int select_devices(struct audio_device *adev, audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002063{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002064 snd_device_t out_snd_device = SND_DEVICE_NONE;
2065 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002066 struct audio_usecase *usecase = NULL;
2067 struct audio_usecase *vc_usecase = NULL;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002068 struct audio_usecase *voip_usecase = NULL;
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -08002069 struct audio_usecase *hfp_usecase = NULL;
Siddartha Shaik44dd7702017-06-14 12:13:25 +05302070 struct stream_out stream_out;
Vimal Puthanveed41fcff22014-01-23 15:56:53 -08002071 audio_usecase_t hfp_ucid;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002072 int status = 0;
Weiyin Jiang5d608082018-02-01 17:24:33 +08002073 audio_devices_t audio_device;
2074 audio_channel_mask_t channel_mask;
2075 int sample_rate;
2076 int acdb_id;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002077
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302078 ALOGD("%s for use case (%s)", __func__, use_case_table[uc_id]);
2079
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002080 usecase = get_usecase_from_list(adev, uc_id);
2081 if (usecase == NULL) {
2082 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
2083 return -EINVAL;
2084 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002085
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002086 if ((usecase->type == VOICE_CALL) ||
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -08002087 (usecase->type == VOIP_CALL) ||
2088 (usecase->type == PCM_HFP_CALL)) {
Aditya Bavanaribdda2f22016-10-19 15:02:05 +05302089 if(usecase->stream.out == NULL) {
2090 ALOGE("%s: stream.out is NULL", __func__);
2091 return -EINVAL;
2092 }
Eric Laurentb23d5282013-05-14 15:27:20 -07002093 out_snd_device = platform_get_output_snd_device(adev->platform,
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -08002094 usecase->stream.out);
Eric Laurentb23d5282013-05-14 15:27:20 -07002095 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002096 usecase->devices = usecase->stream.out->devices;
Siddartha Shaik31b530e2017-05-19 15:26:33 +05302097 } else if (usecase->type == TRANSCODE_LOOPBACK ) {
2098 if (usecase->stream.inout == NULL) {
2099 ALOGE("%s: stream.inout is NULL", __func__);
2100 return -EINVAL;
2101 }
Siddartha Shaik44dd7702017-06-14 12:13:25 +05302102 stream_out.devices = usecase->stream.inout->out_config.devices;
2103 stream_out.sample_rate = usecase->stream.inout->out_config.sample_rate;
2104 stream_out.format = usecase->stream.inout->out_config.format;
2105 stream_out.channel_mask = usecase->stream.inout->out_config.channel_mask;
2106 out_snd_device = platform_get_output_snd_device(adev->platform,
2107 &stream_out);
2108 in_snd_device = platform_get_input_snd_device(adev->platform, AUDIO_DEVICE_NONE);
Siddartha Shaik31b530e2017-05-19 15:26:33 +05302109 usecase->devices = (out_snd_device | in_snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002110 } else {
2111 /*
2112 * If the voice call is active, use the sound devices of voice call usecase
2113 * so that it would not result any device switch. All the usecases will
2114 * be switched to new device when select_devices() is called for voice call
2115 * usecase. This is to avoid switching devices for voice call when
2116 * check_usecases_codec_backend() is called below.
Alexy Joseph79dfa3c2016-04-20 18:44:56 -07002117 * choose voice call device only if the use case device is
2118 * also using the codec backend
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002119 */
Shiv Maliyappanahallibb4cf0b2016-01-21 11:30:06 -08002120 if (voice_is_in_call(adev) && adev->mode != AUDIO_MODE_NORMAL) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002121 vc_usecase = get_usecase_from_list(adev,
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -08002122 get_usecase_id_from_usecase_type(adev, VOICE_CALL));
Alexy Josephdbcddf22016-05-16 17:54:09 -07002123 if ((vc_usecase) && (((vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) &&
2124 (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)) ||
Preetam Singh Ranawat097cb1f2016-07-19 13:49:04 +05302125 ((vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) &&
2126 (usecase->devices & AUDIO_DEVICE_IN_ALL_CODEC_BACKEND)) ||
Alexy Josephdbcddf22016-05-16 17:54:09 -07002127 (usecase->devices == AUDIO_DEVICE_IN_VOICE_CALL))) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002128 in_snd_device = vc_usecase->in_snd_device;
2129 out_snd_device = vc_usecase->out_snd_device;
2130 }
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002131 } else if (voice_extn_compress_voip_is_active(adev)) {
yidongh02ef86f2017-04-21 15:36:04 +08002132 bool out_snd_device_backend_match = true;
yidongh47785a82017-05-08 19:29:29 +08002133 voip_usecase = get_usecase_from_list(adev, USECASE_COMPRESS_VOIP_CALL);
yidongh6261d8e2017-05-15 17:04:02 +08002134 if ((voip_usecase != NULL) &&
2135 (usecase->type == PCM_PLAYBACK) &&
2136 (usecase->stream.out != NULL)) {
yidongh02ef86f2017-04-21 15:36:04 +08002137 out_snd_device_backend_match = platform_check_backends_match(
2138 voip_usecase->out_snd_device,
2139 platform_get_output_snd_device(
2140 adev->platform,
2141 usecase->stream.out));
2142 }
Mingming Yin2d8aa2e2014-08-14 00:00:51 -07002143 if ((voip_usecase) && ((voip_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) &&
Zhou Song0154bf12016-08-04 10:48:16 +08002144 ((usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) ||
2145 ((usecase->devices & ~AUDIO_DEVICE_BIT_IN) & AUDIO_DEVICE_IN_ALL_CODEC_BACKEND)) &&
yidongh02ef86f2017-04-21 15:36:04 +08002146 out_snd_device_backend_match &&
Mingming Yin2d8aa2e2014-08-14 00:00:51 -07002147 (voip_usecase->stream.out != adev->primary_output))) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002148 in_snd_device = voip_usecase->in_snd_device;
2149 out_snd_device = voip_usecase->out_snd_device;
2150 }
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -08002151 } else if (audio_extn_hfp_is_active(adev)) {
Vimal Puthanveed41fcff22014-01-23 15:56:53 -08002152 hfp_ucid = audio_extn_hfp_get_usecase();
2153 hfp_usecase = get_usecase_from_list(adev, hfp_ucid);
Mingming Yin2d8aa2e2014-08-14 00:00:51 -07002154 if ((hfp_usecase) && (hfp_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)) {
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -08002155 in_snd_device = hfp_usecase->in_snd_device;
2156 out_snd_device = hfp_usecase->out_snd_device;
2157 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002158 }
2159 if (usecase->type == PCM_PLAYBACK) {
Aditya Bavanaribdda2f22016-10-19 15:02:05 +05302160 if (usecase->stream.out == NULL) {
2161 ALOGE("%s: stream.out is NULL", __func__);
2162 return -EINVAL;
2163 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002164 usecase->devices = usecase->stream.out->devices;
2165 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07002166 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurentb23d5282013-05-14 15:27:20 -07002167 out_snd_device = platform_get_output_snd_device(adev->platform,
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -08002168 usecase->stream.out);
kunleizdcf967a2018-08-07 17:09:11 +08002169 voip_usecase = get_usecase_from_list(adev, USECASE_AUDIO_PLAYBACK_VOIP);
kunleiz3ca773f2018-10-12 15:49:35 +08002170 if (voip_usecase == NULL && adev->primary_output && !adev->primary_output->standby)
kunleizdcf967a2018-08-07 17:09:11 +08002171 voip_usecase = get_usecase_from_list(adev, adev->primary_output->usecase);
2172
2173 if ((usecase->stream.out != NULL &&
2174 voip_usecase != NULL &&
2175 usecase->stream.out->usecase == voip_usecase->id) &&
2176 adev->active_input &&
kunleiz3ca773f2018-10-12 15:49:35 +08002177 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
kunleizdcf967a2018-08-07 17:09:11 +08002178 out_snd_device != usecase->out_snd_device) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07002179 select_devices(adev, adev->active_input->usecase);
2180 }
2181 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002182 } else if (usecase->type == PCM_CAPTURE) {
Aditya Bavanaribdda2f22016-10-19 15:02:05 +05302183 if (usecase->stream.in == NULL) {
2184 ALOGE("%s: stream.in is NULL", __func__);
2185 return -EINVAL;
2186 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002187 usecase->devices = usecase->stream.in->device;
2188 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07002189 if (in_snd_device == SND_DEVICE_NONE) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07002190 audio_devices_t out_device = AUDIO_DEVICE_NONE;
Leena Winterrowd3c8fbae2016-01-12 15:05:53 -08002191 if (adev->active_input &&
2192 (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
Karthik Reddy Kattaf621ec72014-11-21 16:37:38 +05302193 (adev->mode == AUDIO_MODE_IN_COMMUNICATION &&
kunleizdcf967a2018-08-07 17:09:11 +08002194 adev->active_input->source == AUDIO_SOURCE_MIC))) {
2195 voip_usecase = get_usecase_from_list(adev, USECASE_AUDIO_PLAYBACK_VOIP);
2196 if (voip_usecase != NULL && voip_usecase->stream.out != NULL)
2197 out_device = voip_usecase->stream.out->devices;
2198 else if (adev->primary_output && !adev->primary_output->standby)
2199 out_device = adev->primary_output->devices;
Apoorv Raghuvanshi924b3022015-07-06 15:07:14 -07002200 platform_set_echo_reference(adev, false, AUDIO_DEVICE_NONE);
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07002201 } else if (usecase->id == USECASE_AUDIO_RECORD_AFE_PROXY) {
2202 out_device = AUDIO_DEVICE_OUT_TELEPHONY_TX;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07002203 }
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07002204 in_snd_device = platform_get_input_snd_device(adev->platform, out_device);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07002205 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002206 }
2207 }
2208
2209 if (out_snd_device == usecase->out_snd_device &&
2210 in_snd_device == usecase->in_snd_device) {
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05302211
2212 if (!force_device_switch(usecase))
2213 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002214 }
2215
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302216 if ((is_btsco_device(out_snd_device,in_snd_device) && !adev->bt_sco_on) ||
2217 (is_a2dp_device(out_snd_device) && !audio_extn_a2dp_is_ready())) {
2218 ALOGD("SCO/A2DP is selected but they are not connected/ready hence dont route");
2219 return 0;
2220 }
2221
sangwoobc677242013-08-08 16:53:43 +09002222 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -07002223 out_snd_device, platform_get_snd_device_name(out_snd_device),
2224 in_snd_device, platform_get_snd_device_name(in_snd_device));
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002225
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002226 /*
2227 * Limitation: While in call, to do a device switch we need to disable
2228 * and enable both RX and TX devices though one of them is same as current
2229 * device.
2230 */
Vidyakumar Athota21b3bb92014-04-25 11:08:08 -07002231 if ((usecase->type == VOICE_CALL) &&
2232 (usecase->in_snd_device != SND_DEVICE_NONE) &&
2233 (usecase->out_snd_device != SND_DEVICE_NONE)) {
Eric Laurentb23d5282013-05-14 15:27:20 -07002234 status = platform_switch_voice_call_device_pre(adev->platform);
Narsinga Rao Chella116142b2015-08-14 18:00:08 -07002235 }
2236
2237 if (((usecase->type == VOICE_CALL) ||
2238 (usecase->type == VOIP_CALL)) &&
2239 (usecase->out_snd_device != SND_DEVICE_NONE)) {
2240 /* Disable sidetone only if voice/voip call already exists */
2241 if (voice_is_call_state_active(adev) ||
2242 voice_extn_compress_voip_is_started(adev))
Bhalchandra Gajare45fee282015-06-09 22:23:45 -07002243 voice_set_sidetone(adev, usecase->out_snd_device, false);
Vidyakumar Athotaea269c62016-10-31 09:05:59 -07002244
2245 /* Disable aanc only if voice call exists */
2246 if (voice_is_call_state_active(adev))
2247 voice_check_and_update_aanc_path(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002248 }
2249
Zhou Songc66eb7e2017-08-08 18:29:07 +08002250 if ((out_snd_device == SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP) &&
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302251 (!audio_extn_a2dp_is_ready())) {
2252 ALOGW("%s: A2DP profile is not ready, routing to speaker only", __func__);
Zhou Songc66eb7e2017-08-08 18:29:07 +08002253 out_snd_device = SND_DEVICE_OUT_SPEAKER;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302254 }
2255
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002256 /* Disable current sound devices */
2257 if (usecase->out_snd_device != SND_DEVICE_NONE) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07002258 disable_audio_route(adev, usecase);
2259 disable_snd_device(adev, usecase->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002260 }
2261
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002262 if (usecase->in_snd_device != SND_DEVICE_NONE) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07002263 disable_audio_route(adev, usecase);
2264 disable_snd_device(adev, usecase->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002265 }
2266
Vidyakumar Athota545dbd32013-11-13 17:30:53 -08002267 /* Applicable only on the targets that has external modem.
2268 * New device information should be sent to modem before enabling
2269 * the devices to reduce in-call device switch time.
2270 */
Vidyakumar Athota21b3bb92014-04-25 11:08:08 -07002271 if ((usecase->type == VOICE_CALL) &&
2272 (usecase->in_snd_device != SND_DEVICE_NONE) &&
2273 (usecase->out_snd_device != SND_DEVICE_NONE)) {
Vidyakumar Athota545dbd32013-11-13 17:30:53 -08002274 status = platform_switch_voice_call_enable_device_config(adev->platform,
2275 out_snd_device,
2276 in_snd_device);
Vidyakumar Athota21b3bb92014-04-25 11:08:08 -07002277 }
Vidyakumar Athota545dbd32013-11-13 17:30:53 -08002278
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002279 /* Enable new sound devices */
2280 if (out_snd_device != SND_DEVICE_NONE) {
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08002281 check_usecases_codec_backend(adev, usecase, out_snd_device);
Preetam Singh Ranawat43eac682017-03-07 18:19:02 +05302282 if (platform_check_codec_asrc_support(adev->platform))
2283 check_and_set_asrc_mode(adev, usecase, out_snd_device);
Haynes Mathew George1376ca62014-04-24 11:55:48 -07002284 enable_snd_device(adev, out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002285 }
2286
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002287 if (in_snd_device != SND_DEVICE_NONE) {
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05302288 check_usecases_capture_codec_backend(adev, usecase, in_snd_device);
Haynes Mathew George1376ca62014-04-24 11:55:48 -07002289 enable_snd_device(adev, in_snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07002290 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002291
Avinash Vaish71a8b972014-07-24 15:36:33 +05302292 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) {
Eric Laurentb23d5282013-05-14 15:27:20 -07002293 status = platform_switch_voice_call_device_post(adev->platform,
2294 out_snd_device,
2295 in_snd_device);
Avinash Vaish71a8b972014-07-24 15:36:33 +05302296 enable_audio_route_for_voice_usecases(adev, usecase);
2297 }
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002298
sangwoo170731f2013-06-08 15:36:36 +09002299 usecase->in_snd_device = in_snd_device;
2300 usecase->out_snd_device = out_snd_device;
2301
Dhananjay Kumard6d32152016-10-13 16:11:03 +05302302 audio_extn_utils_update_stream_app_type_cfg_for_usecase(adev,
2303 usecase);
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +05302304 if (usecase->type == PCM_PLAYBACK) {
Weiyin Jiang6f4c8062016-11-23 15:30:29 +08002305 if ((24 == usecase->stream.out->bit_width) &&
2306 (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER)) {
2307 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
2308 } else if ((out_snd_device == SND_DEVICE_OUT_HDMI ||
2309 out_snd_device == SND_DEVICE_OUT_USB_HEADSET ||
2310 out_snd_device == SND_DEVICE_OUT_DISPLAY_PORT) &&
2311 (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) {
2312 /*
2313 * To best utlize DSP, check if the stream sample rate is supported/multiple of
2314 * configured device sample rate, if not update the COPP rate to be equal to the
2315 * device sample rate, else open COPP at stream sample rate
2316 */
2317 platform_check_and_update_copp_sample_rate(adev->platform, out_snd_device,
2318 usecase->stream.out->sample_rate,
2319 &usecase->stream.out->app_type_cfg.sample_rate);
Ashish Jain4826f6c2017-02-06 13:33:20 +05302320 } else if (((out_snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 &&
2321 !audio_is_true_native_stream_active(adev)) &&
Weiyin Jiang6f4c8062016-11-23 15:30:29 +08002322 usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) ||
2323 (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) {
2324 usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
2325 }
2326
Weiyin Jiang5d608082018-02-01 17:24:33 +08002327 /* Cache stream information to be notified to gef clients */
2328 audio_device = usecase->stream.out->devices;
2329 channel_mask = usecase->stream.out->channel_mask;
2330 sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
2331 acdb_id = platform_get_snd_device_acdb_id(usecase->out_snd_device);
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +05302332 }
Haynes Mathew George1376ca62014-04-24 11:55:48 -07002333 enable_audio_route(adev, usecase);
sangwoo170731f2013-06-08 15:36:36 +09002334
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07002335 /* If input stream is already running then effect needs to be
2336 applied on the new input device that's being enabled here. */
Vidyakumar Athotabbfa5f72018-07-05 18:04:48 -07002337 if ((in_snd_device != SND_DEVICE_NONE) && (adev->active_input != NULL) &&
2338 (!adev->active_input->standby))
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07002339 check_and_enable_effect(adev);
2340
Vidyakumar Athota493f2892016-08-14 11:56:55 -07002341 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) {
Vidyakumar Athotaea269c62016-10-31 09:05:59 -07002342 /* Enable aanc only if voice call exists */
2343 if (voice_is_call_state_active(adev))
2344 voice_check_and_update_aanc_path(adev, out_snd_device, true);
2345
Vidyakumar Athota493f2892016-08-14 11:56:55 -07002346 /* Enable sidetone only if other voice/voip call already exists */
2347 if (voice_is_call_state_active(adev) ||
2348 voice_extn_compress_voip_is_started(adev))
2349 voice_set_sidetone(adev, out_snd_device, true);
2350 }
2351
Vidyakumar Athota1fd21792013-11-15 14:50:57 -08002352 /* Applicable only on the targets that has external modem.
2353 * Enable device command should be sent to modem only after
2354 * enabling voice call mixer controls
2355 */
Vidyakumar Athota339342f2014-07-01 15:30:57 -07002356 if (usecase->type == VOICE_CALL)
Vidyakumar Athota1fd21792013-11-15 14:50:57 -08002357 status = platform_switch_voice_call_usecase_route_post(adev->platform,
2358 out_snd_device,
2359 in_snd_device);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302360
2361 if (is_btsco_device(out_snd_device, in_snd_device) || is_a2dp_device(out_snd_device)) {
2362
2363 if (usecase->type == VOIP_CALL) {
2364 if (adev->active_input != NULL &&
2365 !adev->active_input->standby) {
2366 if (is_bt_soc_on(adev) == false){
2367 ALOGD("BT SCO MIC disconnected while in connection");
2368 if (adev->active_input->pcm != NULL)
2369 pcm_stop(adev->active_input->pcm);
2370 }
2371 }
2372 if ((usecase->stream.out != NULL) && (usecase->stream.out != adev->primary_output)
2373 && usecase->stream.out->started) {
2374 if (is_bt_soc_on(adev) == false) {
2375 ALOGD("BT SCO/A2DP disconnected while in connection");
2376 out_standby_l(&usecase->stream.out->stream.common);
2377 }
2378 }
2379 } else if ((usecase->stream.out != NULL) &&
2380 !(usecase->stream.out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
2381 usecase->stream.out->started) {
2382 if (is_bt_soc_on(adev) == false) {
2383 ALOGD("BT SCO/A2dp disconnected while in connection");
2384 out_standby_l(&usecase->stream.out->stream.common);
2385 }
2386 }
2387 }
2388
Weiyin Jiang5d608082018-02-01 17:24:33 +08002389 /* Notify device change info to effect clients registered
2390 * NOTE: device lock has to be unlock temporarily here.
2391 * To the worst case, we notify stale info to clients.
2392 */
2393 if (usecase->type == PCM_PLAYBACK) {
2394 pthread_mutex_unlock(&adev->lock);
2395 audio_extn_gef_notify_device_config(audio_device, channel_mask, sample_rate, acdb_id);
2396 pthread_mutex_lock(&adev->lock);
2397 }
2398
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302399 ALOGD("%s: done",__func__);
2400
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002401 return status;
2402}
2403
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002404static int stop_input_stream(struct stream_in *in)
2405{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05302406 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002407 struct audio_usecase *uc_info;
Pallavid7c7a272018-01-16 11:22:55 +05302408
2409 if (in == NULL) {
2410 ALOGE("%s: stream_in ptr is NULL", __func__);
2411 return -EINVAL;
2412 }
2413
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002414 struct audio_device *adev = in->dev;
2415
Eric Laurent994a6932013-07-17 11:51:42 -07002416 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002417 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002418 uc_info = get_usecase_from_list(adev, in->usecase);
2419 if (uc_info == NULL) {
2420 ALOGE("%s: Could not find the usecase (%d) in the list",
2421 __func__, in->usecase);
2422 return -EINVAL;
2423 }
2424
Derek Chencdd17c72014-11-24 12:39:14 -08002425 if (uc_info->in_snd_device != SND_DEVICE_NONE) {
2426 if (audio_extn_ext_hw_plugin_usecase_stop(adev->ext_hw_plugin, uc_info))
2427 ALOGE("%s: failed to stop ext hw plugin", __func__);
2428 }
2429
Vidyakumar Athota2850d532013-11-19 16:02:12 -08002430 /* Close in-call recording streams */
2431 voice_check_and_stop_incall_rec_usecase(adev, in);
2432
Eric Laurent150dbfe2013-02-27 14:31:02 -08002433 /* 1. Disable stream specific mixer controls */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07002434 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002435
2436 /* 2. Disable the tx device */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07002437 disable_snd_device(adev, uc_info->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002438
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002439 list_remove(&uc_info->list);
2440 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002441
Aalique Grahame2e9b2e42016-12-07 12:43:48 -08002442 adev->active_input = get_next_active_input(adev);
2443
Eric Laurent994a6932013-07-17 11:51:42 -07002444 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002445 return ret;
2446}
2447
2448int start_input_stream(struct stream_in *in)
2449{
2450 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -08002451 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002452 struct audio_usecase *uc_info;
Preetam Singh Ranawata87e9742018-02-13 16:52:53 +05302453
2454 if (in == NULL) {
2455 ALOGE("%s: stream_in ptr is NULL", __func__);
2456 return -EINVAL;
2457 }
2458
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002459 struct audio_device *adev = in->dev;
Garmond Leunge2433c32017-09-28 21:51:22 -07002460 struct pcm_config config = in->config;
Garmond Leung438932f2017-10-04 19:35:18 -07002461 int usecase = platform_update_usecase_from_source(in->source,in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002462
Mingming Yin2664a5b2015-09-03 10:53:11 -07002463 if (get_usecase_from_list(adev, usecase) == NULL)
2464 in->usecase = usecase;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302465 ALOGD("%s: enter: stream(%p)usecase(%d: %s)",
2466 __func__, &in->stream, in->usecase, use_case_table[in->usecase]);
Shiv Maliyappanahallida107642013-10-17 11:16:13 -07002467
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05302468 if (CARD_STATUS_OFFLINE == in->card_status||
2469 CARD_STATUS_OFFLINE == adev->card_status) {
2470 ALOGW("in->card_status or adev->card_status offline, try again");
Dhanalakshmi Siddani4d57e992014-07-17 16:37:51 +05302471 ret = -EIO;
Naresh Tanniru4c630392014-05-12 01:05:52 +05302472 goto error_config;
2473 }
Naresh Tanniru4c630392014-05-12 01:05:52 +05302474
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302475 if (audio_is_bluetooth_sco_device(in->device)) {
2476 if (!adev->bt_sco_on) {
2477 ALOGE("%s: SCO profile is not ready, return error", __func__);
2478 ret = -EIO;
2479 goto error_config;
2480 }
2481 }
2482
Shiv Maliyappanahallida107642013-10-17 11:16:13 -07002483 /* Check if source matches incall recording usecase criteria */
2484 ret = voice_check_and_set_incall_rec_usecase(adev, in);
2485 if (ret)
2486 goto error_config;
2487 else
Mingming Yin2664a5b2015-09-03 10:53:11 -07002488 ALOGV("%s: usecase(%d)", __func__, in->usecase);
2489
2490 if (get_usecase_from_list(adev, in->usecase) != NULL) {
2491 ALOGE("%s: use case assigned already in use, stream(%p)usecase(%d: %s)",
2492 __func__, &in->stream, in->usecase, use_case_table[in->usecase]);
Zhou Song4e0704d2016-04-20 13:00:14 +08002493 return -EINVAL;
Mingming Yin2664a5b2015-09-03 10:53:11 -07002494 }
Shiv Maliyappanahallida107642013-10-17 11:16:13 -07002495
Eric Laurentb23d5282013-05-14 15:27:20 -07002496 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002497 if (in->pcm_device_id < 0) {
2498 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
2499 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -08002500 ret = -EINVAL;
2501 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002502 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002503
2504 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002505 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07002506
2507 if (!uc_info) {
2508 ret = -ENOMEM;
2509 goto error_config;
2510 }
2511
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002512 uc_info->id = in->usecase;
2513 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002514 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002515 uc_info->devices = in->device;
2516 uc_info->in_snd_device = SND_DEVICE_NONE;
2517 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002518
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002519 list_add_tail(&adev->usecase_list, &uc_info->list);
Sudheer Papothifa9d2282015-09-17 01:53:25 +05302520 audio_extn_perf_lock_acquire(&adev->perf_lock_handle, 0,
2521 adev->perf_lock_opts,
2522 adev->perf_lock_opts_size);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002523 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002524
Derek Chencdd17c72014-11-24 12:39:14 -08002525 if (uc_info->in_snd_device != SND_DEVICE_NONE) {
2526 if (audio_extn_ext_hw_plugin_usecase_start(adev->ext_hw_plugin, uc_info))
2527 ALOGE("%s: failed to start ext hw plugin", __func__);
2528 }
2529
Haynes Mathew George16081042017-05-31 17:16:49 -07002530 if (audio_extn_cin_attached_usecase(in->usecase)) {
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05302531 ret = audio_extn_cin_start_input_stream(in);
2532 if (ret)
2533 goto error_open;
2534 else
2535 goto done_open;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07002536 }
2537
Haynes Mathew George16081042017-05-31 17:16:49 -07002538 if (in->usecase == USECASE_AUDIO_RECORD_MMAP) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07002539 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) {
Haynes Mathew George16081042017-05-31 17:16:49 -07002540 ALOGE("%s: pcm stream not ready", __func__);
2541 goto error_open;
2542 }
2543 ret = pcm_start(in->pcm);
2544 if (ret < 0) {
2545 ALOGE("%s: MMAP pcm_start failed ret %d", __func__, ret);
2546 goto error_open;
2547 }
2548 } else {
2549 unsigned int flags = PCM_IN | PCM_MONOTONIC;
2550 unsigned int pcm_open_retry_count = 0;
2551
2552 if (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY) {
2553 flags |= PCM_MMAP | PCM_NOIRQ;
2554 pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT;
2555 } else if (in->realtime) {
2556 flags |= PCM_MMAP | PCM_NOIRQ;
2557 }
2558
Garmond Leunge2433c32017-09-28 21:51:22 -07002559 if (audio_extn_ffv_get_stream() == in) {
2560 ALOGD("%s: ffv stream, update pcm config", __func__);
2561 audio_extn_ffv_update_pcm_config(&config);
2562 }
Haynes Mathew George16081042017-05-31 17:16:49 -07002563 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
2564 __func__, adev->snd_card, in->pcm_device_id, in->config.channels);
2565
2566 while (1) {
Haynes Mathew George380745d2017-10-04 15:27:45 -07002567 ATRACE_BEGIN("pcm_in_open");
Haynes Mathew George16081042017-05-31 17:16:49 -07002568 in->pcm = pcm_open(adev->snd_card, in->pcm_device_id,
Garmond Leung438932f2017-10-04 19:35:18 -07002569 flags, &config);
Haynes Mathew George380745d2017-10-04 15:27:45 -07002570 ATRACE_END();
Sharad Sangle90e613f2018-05-04 16:15:38 +05302571 if (errno == ENETRESET && !pcm_is_ready(in->pcm)) {
2572 ALOGE("%s: pcm_open failed errno:%d\n", __func__, errno);
2573 adev->card_status = CARD_STATUS_OFFLINE;
2574 in->card_status = CARD_STATUS_OFFLINE;
2575 ret = -EIO;
2576 goto error_open;
2577 }
2578
Haynes Mathew George16081042017-05-31 17:16:49 -07002579 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) {
2580 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
2581 if (in->pcm != NULL) {
2582 pcm_close(in->pcm);
2583 in->pcm = NULL;
2584 }
2585 if (pcm_open_retry_count-- == 0) {
2586 ret = -EIO;
2587 goto error_open;
2588 }
2589 usleep(PROXY_OPEN_WAIT_TIME * 1000);
2590 continue;
2591 }
2592 break;
2593 }
2594
2595 ALOGV("%s: pcm_prepare", __func__);
Haynes Mathew George380745d2017-10-04 15:27:45 -07002596 ATRACE_BEGIN("pcm_in_prepare");
Haynes Mathew George16081042017-05-31 17:16:49 -07002597 ret = pcm_prepare(in->pcm);
Haynes Mathew George380745d2017-10-04 15:27:45 -07002598 ATRACE_END();
Haynes Mathew George16081042017-05-31 17:16:49 -07002599 if (ret < 0) {
2600 ALOGE("%s: pcm_prepare returned %d", __func__, ret);
2601 pcm_close(in->pcm);
2602 in->pcm = NULL;
2603 goto error_open;
2604 }
2605 register_in_stream(in);
2606 if (in->realtime) {
Haynes Mathew George380745d2017-10-04 15:27:45 -07002607 ATRACE_BEGIN("pcm_in_start");
Haynes Mathew George16081042017-05-31 17:16:49 -07002608 ret = pcm_start(in->pcm);
Haynes Mathew George380745d2017-10-04 15:27:45 -07002609 ATRACE_END();
Haynes Mathew George16081042017-05-31 17:16:49 -07002610 if (ret < 0) {
2611 ALOGE("%s: RT pcm_start failed ret %d", __func__, ret);
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07002612 pcm_close(in->pcm);
2613 in->pcm = NULL;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07002614 goto error_open;
2615 }
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07002616 }
Haynes Mathew George5beddd42016-06-27 18:33:40 -07002617 }
2618
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07002619 check_and_enable_effect(adev);
2620
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05302621done_open:
Sudheer Papothifa9d2282015-09-17 01:53:25 +05302622 audio_extn_perf_lock_release(&adev->perf_lock_handle);
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07002623 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07002624
Eric Laurentc8400632013-02-14 19:04:54 -08002625 return ret;
2626
2627error_open:
Sudheer Papothifa9d2282015-09-17 01:53:25 +05302628 audio_extn_perf_lock_release(&adev->perf_lock_handle);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002629 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -08002630error_config:
Dhananjay Kumard4833242016-10-06 22:09:12 +05302631 adev->active_input = get_next_active_input(adev);
Laxminath Kasam2cb4b752015-09-24 03:59:15 +05302632 /*
2633 * sleep 50ms to allow sufficient time for kernel
2634 * drivers to recover incases like SSR.
2635 */
2636 usleep(50000);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002637 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -08002638
2639 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002640}
2641
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07002642void lock_input_stream(struct stream_in *in)
2643{
2644 pthread_mutex_lock(&in->pre_lock);
2645 pthread_mutex_lock(&in->lock);
2646 pthread_mutex_unlock(&in->pre_lock);
2647}
2648
2649void lock_output_stream(struct stream_out *out)
2650{
2651 pthread_mutex_lock(&out->pre_lock);
2652 pthread_mutex_lock(&out->lock);
2653 pthread_mutex_unlock(&out->pre_lock);
2654}
2655
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002656/* must be called with out->lock locked */
2657static int send_offload_cmd_l(struct stream_out* out, int command)
2658{
2659 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
2660
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07002661 if (!cmd) {
2662 ALOGE("failed to allocate mem for command 0x%x", command);
2663 return -ENOMEM;
2664 }
2665
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002666 ALOGVV("%s %d", __func__, command);
2667
2668 cmd->cmd = command;
2669 list_add_tail(&out->offload_cmd_list, &cmd->node);
2670 pthread_cond_signal(&out->offload_cond);
2671 return 0;
2672}
2673
2674/* must be called iwth out->lock locked */
2675static void stop_compressed_output_l(struct stream_out *out)
2676{
2677 out->offload_state = OFFLOAD_STATE_IDLE;
2678 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002679 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002680 if (out->compr != NULL) {
2681 compress_stop(out->compr);
2682 while (out->offload_thread_blocked) {
2683 pthread_cond_wait(&out->cond, &out->lock);
2684 }
2685 }
2686}
2687
Varun Balaraje49253e2017-07-06 19:48:56 +05302688bool is_interactive_usecase(audio_usecase_t uc_id)
2689{
2690 unsigned int i;
2691 for (i = 0; i < sizeof(interactive_usecases)/sizeof(interactive_usecases[0]); i++) {
2692 if (uc_id == interactive_usecases[i])
2693 return true;
2694 }
2695 return false;
2696}
2697
2698static audio_usecase_t get_interactive_usecase(struct audio_device *adev)
2699{
2700 audio_usecase_t ret_uc = USECASE_INVALID;
2701 unsigned int intract_uc_index;
2702 unsigned int num_usecase = sizeof(interactive_usecases)/sizeof(interactive_usecases[0]);
2703
2704 ALOGV("%s: num_usecase: %d", __func__, num_usecase);
2705 for (intract_uc_index = 0; intract_uc_index < num_usecase; intract_uc_index++) {
2706 if (!(adev->interactive_usecase_state & (0x1 << intract_uc_index))) {
2707 adev->interactive_usecase_state |= 0x1 << intract_uc_index;
2708 ret_uc = interactive_usecases[intract_uc_index];
2709 break;
2710 }
2711 }
2712
2713 ALOGV("%s: Interactive usecase is %d", __func__, ret_uc);
2714 return ret_uc;
2715}
2716
2717static void free_interactive_usecase(struct audio_device *adev,
2718 audio_usecase_t uc_id)
2719{
2720 unsigned int interact_uc_index;
2721 unsigned int num_usecase = sizeof(interactive_usecases)/sizeof(interactive_usecases[0]);
2722
2723 for (interact_uc_index = 0; interact_uc_index < num_usecase; interact_uc_index++) {
2724 if (interactive_usecases[interact_uc_index] == uc_id) {
2725 adev->interactive_usecase_state &= ~(0x1 << interact_uc_index);
2726 break;
2727 }
2728 }
2729 ALOGV("%s: free Interactive usecase %d", __func__, uc_id);
2730}
2731
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002732bool is_offload_usecase(audio_usecase_t uc_id)
2733{
2734 unsigned int i;
2735 for (i = 0; i < sizeof(offload_usecases)/sizeof(offload_usecases[0]); i++) {
2736 if (uc_id == offload_usecases[i])
2737 return true;
2738 }
2739 return false;
2740}
2741
Dhananjay Kumarac341582017-02-23 23:42:25 +05302742static audio_usecase_t get_offload_usecase(struct audio_device *adev, bool is_compress)
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002743{
vivek mehta446c3962015-09-14 10:57:35 -07002744 audio_usecase_t ret_uc = USECASE_INVALID;
2745 unsigned int offload_uc_index;
Alexy Josephb1379942016-01-29 15:49:38 -08002746 unsigned int num_usecase = sizeof(offload_usecases)/sizeof(offload_usecases[0]);
vivek mehta446c3962015-09-14 10:57:35 -07002747 if (!adev->multi_offload_enable) {
Dhananjay Kumarac341582017-02-23 23:42:25 +05302748 if (!is_compress)
vivek mehta446c3962015-09-14 10:57:35 -07002749 ret_uc = USECASE_AUDIO_PLAYBACK_OFFLOAD2;
2750 else
2751 ret_uc = USECASE_AUDIO_PLAYBACK_OFFLOAD;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002752
vivek mehta446c3962015-09-14 10:57:35 -07002753 pthread_mutex_lock(&adev->lock);
2754 if (get_usecase_from_list(adev, ret_uc) != NULL)
2755 ret_uc = USECASE_INVALID;
2756 pthread_mutex_unlock(&adev->lock);
2757
2758 return ret_uc;
2759 }
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002760
2761 ALOGV("%s: num_usecase: %d", __func__, num_usecase);
vivek mehta446c3962015-09-14 10:57:35 -07002762 for (offload_uc_index = 0; offload_uc_index < num_usecase; offload_uc_index++) {
2763 if (!(adev->offload_usecases_state & (0x1 << offload_uc_index))) {
2764 adev->offload_usecases_state |= 0x1 << offload_uc_index;
2765 ret_uc = offload_usecases[offload_uc_index];
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002766 break;
2767 }
2768 }
vivek mehta446c3962015-09-14 10:57:35 -07002769
2770 ALOGV("%s: offload usecase is %d", __func__, ret_uc);
2771 return ret_uc;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002772}
2773
2774static void free_offload_usecase(struct audio_device *adev,
2775 audio_usecase_t uc_id)
2776{
vivek mehta446c3962015-09-14 10:57:35 -07002777 unsigned int offload_uc_index;
Alexy Josephb1379942016-01-29 15:49:38 -08002778 unsigned int num_usecase = sizeof(offload_usecases)/sizeof(offload_usecases[0]);
vivek mehta446c3962015-09-14 10:57:35 -07002779
2780 if (!adev->multi_offload_enable)
2781 return;
2782
2783 for (offload_uc_index = 0; offload_uc_index < num_usecase; offload_uc_index++) {
2784 if (offload_usecases[offload_uc_index] == uc_id) {
2785 adev->offload_usecases_state &= ~(0x1 << offload_uc_index);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002786 break;
2787 }
2788 }
2789 ALOGV("%s: free offload usecase %d", __func__, uc_id);
2790}
2791
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002792static void *offload_thread_loop(void *context)
2793{
2794 struct stream_out *out = (struct stream_out *) context;
2795 struct listnode *item;
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08002796 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002797
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002798 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
2799 set_sched_policy(0, SP_FOREGROUND);
2800 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
2801
2802 ALOGV("%s", __func__);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07002803 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002804 for (;;) {
2805 struct offload_cmd *cmd = NULL;
2806 stream_callback_event_t event;
2807 bool send_callback = false;
2808
2809 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
2810 __func__, list_empty(&out->offload_cmd_list),
2811 out->offload_state);
2812 if (list_empty(&out->offload_cmd_list)) {
2813 ALOGV("%s SLEEPING", __func__);
2814 pthread_cond_wait(&out->offload_cond, &out->lock);
2815 ALOGV("%s RUNNING", __func__);
2816 continue;
2817 }
2818
2819 item = list_head(&out->offload_cmd_list);
2820 cmd = node_to_item(item, struct offload_cmd, node);
2821 list_remove(item);
2822
2823 ALOGVV("%s STATE %d CMD %d out->compr %p",
2824 __func__, out->offload_state, cmd->cmd, out->compr);
2825
2826 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
2827 free(cmd);
2828 break;
2829 }
2830
Haynes Mathew Georgeee5836f2017-11-21 18:02:10 -08002831 // allow OFFLOAD_CMD_ERROR reporting during standby
2832 // this is needed to handle failures during compress_open
2833 // Note however that on a pause timeout, the stream is closed
2834 // and no offload usecase will be active. Therefore this
2835 // special case is needed for compress_open failures alone
2836 if (cmd->cmd != OFFLOAD_CMD_ERROR &&
2837 out->compr == NULL) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002838 ALOGE("%s: Compress handle is NULL", __func__);
Haynes Mathew Georgea9abb202016-06-02 14:13:20 -07002839 free(cmd);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002840 pthread_cond_signal(&out->cond);
2841 continue;
2842 }
2843 out->offload_thread_blocked = true;
2844 pthread_mutex_unlock(&out->lock);
2845 send_callback = false;
2846 switch(cmd->cmd) {
2847 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07002848 ALOGD("copl(%p):calling compress_wait", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002849 compress_wait(out->compr, -1);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07002850 ALOGD("copl(%p):out of compress_wait", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002851 send_callback = true;
2852 event = STREAM_CBK_EVENT_WRITE_READY;
2853 break;
2854 case OFFLOAD_CMD_PARTIAL_DRAIN:
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08002855 ret = compress_next_track(out->compr);
Sidipotu Ashok55820562014-02-10 16:16:38 +05302856 if(ret == 0) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07002857 ALOGD("copl(%p):calling compress_partial_drain", out);
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05302858 ret = compress_partial_drain(out->compr);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07002859 ALOGD("copl(%p):out of compress_partial_drain", out);
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05302860 if (ret < 0)
2861 ret = -errno;
Sidipotu Ashok55820562014-02-10 16:16:38 +05302862 }
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05302863 else if (ret == -ETIMEDOUT)
Aniket Kumar Lata3570fb12017-11-08 15:53:44 -08002864 ret = compress_drain(out->compr);
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08002865 else
2866 ALOGE("%s: Next track returned error %d",__func__, ret);
Aniket Kumar Lata3570fb12017-11-08 15:53:44 -08002867 if (-ENETRESET != ret && !(-EINTR == ret &&
2868 CARD_STATUS_OFFLINE == out->card_status)) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05302869 send_callback = true;
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05302870 pthread_mutex_lock(&out->lock);
2871 out->send_new_metadata = 1;
2872 out->send_next_track_params = true;
2873 pthread_mutex_unlock(&out->lock);
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05302874 event = STREAM_CBK_EVENT_DRAIN_READY;
2875 ALOGV("copl(%p):send drain callback, ret %d", out, ret);
2876 } else
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05302877 ALOGI("%s: Block drain ready event during SSR", __func__);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002878 break;
2879 case OFFLOAD_CMD_DRAIN:
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07002880 ALOGD("copl(%p):calling compress_drain", out);
Aniket Kumar Lata3570fb12017-11-08 15:53:44 -08002881 ret = compress_drain(out->compr);
2882 ALOGD("copl(%p):out of compress_drain", out);
2883 // EINTR check avoids drain interruption due to SSR
2884 if (-ENETRESET != ret && !(-EINTR == ret &&
2885 CARD_STATUS_OFFLINE == out->card_status)) {
2886 send_callback = true;
2887 event = STREAM_CBK_EVENT_DRAIN_READY;
2888 } else
2889 ALOGI("%s: Block drain ready event during SSR", __func__);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002890 break;
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05302891 case OFFLOAD_CMD_ERROR:
2892 ALOGD("copl(%p): sending error callback to AF", out);
2893 send_callback = true;
2894 event = STREAM_CBK_EVENT_ERROR;
2895 break;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002896 default:
2897 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
2898 break;
2899 }
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07002900 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002901 out->offload_thread_blocked = false;
2902 pthread_cond_signal(&out->cond);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002903 if (send_callback && out->client_callback) {
2904 ALOGVV("%s: sending client_callback event %d", __func__, event);
2905 out->client_callback(event, NULL, out->client_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -07002906 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002907 free(cmd);
2908 }
2909
2910 pthread_cond_signal(&out->cond);
2911 while (!list_empty(&out->offload_cmd_list)) {
2912 item = list_head(&out->offload_cmd_list);
2913 list_remove(item);
2914 free(node_to_item(item, struct offload_cmd, node));
2915 }
2916 pthread_mutex_unlock(&out->lock);
2917
2918 return NULL;
2919}
2920
2921static int create_offload_callback_thread(struct stream_out *out)
2922{
2923 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
2924 list_init(&out->offload_cmd_list);
2925 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
2926 offload_thread_loop, out);
2927 return 0;
2928}
2929
2930static int destroy_offload_callback_thread(struct stream_out *out)
2931{
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07002932 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002933 stop_compressed_output_l(out);
2934 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
2935
2936 pthread_mutex_unlock(&out->lock);
2937 pthread_join(out->offload_thread, (void **) NULL);
2938 pthread_cond_destroy(&out->offload_cond);
2939
2940 return 0;
2941}
2942
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002943static int stop_output_stream(struct stream_out *out)
2944{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05302945 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002946 struct audio_usecase *uc_info;
2947 struct audio_device *adev = out->dev;
2948
Eric Laurent994a6932013-07-17 11:51:42 -07002949 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002950 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002951 uc_info = get_usecase_from_list(adev, out->usecase);
2952 if (uc_info == NULL) {
2953 ALOGE("%s: Could not find the usecase (%d) in the list",
2954 __func__, out->usecase);
2955 return -EINVAL;
2956 }
2957
Derek Chencdd17c72014-11-24 12:39:14 -08002958 if (uc_info->out_snd_device != SND_DEVICE_NONE) {
2959 if (audio_extn_ext_hw_plugin_usecase_stop(adev->ext_hw_plugin, uc_info))
2960 ALOGE("%s: failed to stop ext hw plugin", __func__);
2961 }
2962
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07002963 if (is_offload_usecase(out->usecase) &&
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05302964 !(audio_extn_passthru_is_passthrough_stream(out))) {
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08002965 if (adev->visualizer_stop_output != NULL)
2966 adev->visualizer_stop_output(out->handle, out->pcm_device_id);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08002967
2968 audio_extn_dts_remove_state_notifier_node(out->usecase);
2969
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08002970 if (adev->offload_effects_stop_output != NULL)
2971 adev->offload_effects_stop_output(out->handle, out->pcm_device_id);
2972 }
Eric Laurentc4aef752013-09-12 17:45:53 -07002973
Arun Mirpuridbef0c72018-09-12 18:36:10 -07002974 if (out->usecase == USECASE_INCALL_MUSIC_UPLINK ||
2975 out->usecase == USECASE_INCALL_MUSIC_UPLINK2)
Arun Mirpurief53ce52018-09-11 18:00:09 -07002976 voice_set_device_mute_flag(adev, false);
2977
Eric Laurent150dbfe2013-02-27 14:31:02 -08002978 /* 1. Get and set stream specific mixer controls */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07002979 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002980
2981 /* 2. Disable the rx device */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07002982 disable_snd_device(adev, uc_info->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002983
Xiaojun Sang785b5da2017-08-03 15:52:29 +08002984 if (is_offload_usecase(out->usecase)) {
2985 audio_enable_asm_bit_width_enforce_mode(adev->mixer,
2986 adev->dsp_bit_width_enforce_mode,
2987 false);
2988 }
Garmond Leung5fd0b552018-04-17 11:56:12 -07002989 if (audio_is_usb_out_device(out->devices & AUDIO_DEVICE_OUT_ALL_USB)) {
2990 ret = audio_extn_usb_check_and_set_svc_int(uc_info,
2991 false);
2992
2993 if (ret != 0)
2994 check_usecases_codec_backend(adev, uc_info, uc_info->out_snd_device);
2995 /* default service interval was successfully updated,
2996 reopen USB backend with new service interval */
2997 ret = 0;
2998 }
Xiaojun Sang785b5da2017-08-03 15:52:29 +08002999
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08003000 list_remove(&uc_info->list);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303001 out->started = 0;
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07003002 if (is_offload_usecase(out->usecase) &&
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05303003 (audio_extn_passthru_is_passthrough_stream(out))) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07003004 ALOGV("Disable passthrough , reset mixer to pcm");
3005 /* NO_PASSTHROUGH */
3006 out->compr_config.codec->compr_passthr = 0;
Mingming Yin21854652016-04-13 11:54:02 -07003007 audio_extn_passthru_on_stop(out);
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07003008 audio_extn_dolby_set_dap_bypass(adev, DAP_STATE_ON);
3009 }
Eric Laurent07eeafd2013-10-06 12:52:49 -07003010
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05303011 /* Must be called after removing the usecase from list */
3012 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
Md Mansoor Ahmeddb1b4f92018-01-25 18:56:31 +05303013 audio_extn_keep_alive_start(KEEP_ALIVE_OUT_HDMI);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05303014
Manish Dewangan21a850a2017-08-14 12:03:55 +05303015 if (out->ip_hdlr_handle) {
Naresh Tanniru85819452017-05-04 18:55:45 -07003016 ret = audio_extn_ip_hdlr_intf_close(out->ip_hdlr_handle, true, out);
3017 if (ret < 0)
3018 ALOGE("%s: audio_extn_ip_hdlr_intf_close failed %d",__func__, ret);
3019 }
3020
Garmond Leung5fd0b552018-04-17 11:56:12 -07003021 free(uc_info);
Eric Laurent994a6932013-07-17 11:51:42 -07003022 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003023 return ret;
3024}
3025
3026int start_output_stream(struct stream_out *out)
3027{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003028 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003029 struct audio_usecase *uc_info;
3030 struct audio_device *adev = out->dev;
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08003031 char mixer_ctl_name[128];
3032 struct mixer_ctl *ctl = NULL;
3033 char* perf_mode[] = {"ULL", "ULL_PP", "LL"};
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303034 bool a2dp_combo = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003035
Haynes Mathew George380745d2017-10-04 15:27:45 -07003036 ATRACE_BEGIN("start_output_stream");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07003037 if ((out->usecase < 0) || (out->usecase >= AUDIO_USECASE_MAX)) {
3038 ret = -EINVAL;
3039 goto error_config;
3040 }
3041
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05303042 ALOGD("%s: enter: stream(%p)usecase(%d: %s) devices(%#x)",
3043 __func__, &out->stream, out->usecase, use_case_table[out->usecase],
3044 out->devices);
Naresh Tanniru4c630392014-05-12 01:05:52 +05303045
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05303046 if (CARD_STATUS_OFFLINE == out->card_status ||
3047 CARD_STATUS_OFFLINE == adev->card_status) {
3048 ALOGW("out->card_status or adev->card_status offline, try again");
Dhanalakshmi Siddani4d57e992014-07-17 16:37:51 +05303049 ret = -EIO;
Naresh Tanniru4c630392014-05-12 01:05:52 +05303050 goto error_config;
3051 }
Naresh Tanniru4c630392014-05-12 01:05:52 +05303052
Arun Mirpuridbef0c72018-09-12 18:36:10 -07003053 //Update incall music usecase to reflect correct voice session
3054 if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
3055 ret = voice_extn_check_and_set_incall_music_usecase(adev, out);
3056 if (ret != 0) {
3057 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
3058 __func__, ret);
3059 goto error_config;
3060 }
3061 }
3062
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05303063 if (out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
3064 if (!audio_extn_a2dp_is_ready()) {
3065 if (out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303066 a2dp_combo = true;
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05303067 } else {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303068 if (!(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
3069 ALOGE("%s: A2DP profile is not ready, return error", __func__);
3070 ret = -EAGAIN;
3071 goto error_config;
3072 }
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05303073 }
3074 }
3075 }
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303076 if (out->devices & AUDIO_DEVICE_OUT_ALL_SCO) {
3077 if (!adev->bt_sco_on) {
3078 if (out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
3079 //combo usecase just by pass a2dp
3080 ALOGW("%s: SCO is not connected, route it to speaker", __func__);
3081 out->devices = AUDIO_DEVICE_OUT_SPEAKER;
3082 } else {
3083 ALOGE("%s: SCO profile is not ready, return error", __func__);
3084 ret = -EAGAIN;
3085 goto error_config;
3086 }
3087 }
3088 }
3089
Eric Laurentb23d5282013-05-14 15:27:20 -07003090 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003091 if (out->pcm_device_id < 0) {
3092 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
3093 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08003094 ret = -EINVAL;
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07003095 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003096 }
3097
3098 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07003099
3100 if (!uc_info) {
3101 ret = -ENOMEM;
3102 goto error_config;
3103 }
3104
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003105 uc_info->id = out->usecase;
3106 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08003107 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003108 uc_info->devices = out->devices;
3109 uc_info->in_snd_device = SND_DEVICE_NONE;
3110 uc_info->out_snd_device = SND_DEVICE_NONE;
Garmond Leung5fd0b552018-04-17 11:56:12 -07003111
3112 /* This must be called before adding this usecase to the list */
3113 if (audio_is_usb_out_device(out->devices & AUDIO_DEVICE_OUT_ALL_USB)) {
3114 audio_extn_usb_check_and_set_svc_int(uc_info, true);
3115 /* USB backend is not reopened immediately.
3116 This is eventually done as part of select_devices */
3117 }
3118
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08003119 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003120
Sudheer Papothifa9d2282015-09-17 01:53:25 +05303121 audio_extn_perf_lock_acquire(&adev->perf_lock_handle, 0,
3122 adev->perf_lock_opts,
3123 adev->perf_lock_opts_size);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05303124
3125 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Md Mansoor Ahmeddb1b4f92018-01-25 18:56:31 +05303126 audio_extn_keep_alive_stop(KEEP_ALIVE_OUT_HDMI);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05303127 if (audio_extn_passthru_is_enabled() &&
3128 audio_extn_passthru_is_passthrough_stream(out)) {
3129 audio_extn_passthru_on_start(out);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05303130 }
3131 }
3132
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303133 if ((out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) &&
3134 (!audio_extn_a2dp_is_ready())) {
3135 if (!a2dp_combo) {
3136 check_a2dp_restore_l(adev, out, false);
3137 } else {
3138 audio_devices_t dev = out->devices;
3139 out->devices = AUDIO_DEVICE_OUT_SPEAKER;
3140 select_devices(adev, out->usecase);
3141 out->devices = dev;
3142 }
3143 } else {
3144 select_devices(adev, out->usecase);
3145 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003146
Arun Mirpuridbef0c72018-09-12 18:36:10 -07003147 if (out->usecase == USECASE_INCALL_MUSIC_UPLINK ||
3148 out->usecase == USECASE_INCALL_MUSIC_UPLINK2)
Arun Mirpurief53ce52018-09-11 18:00:09 -07003149 voice_set_device_mute_flag(adev, true);
3150
Derek Chencdd17c72014-11-24 12:39:14 -08003151 if (uc_info->out_snd_device != SND_DEVICE_NONE) {
3152 if (audio_extn_ext_hw_plugin_usecase_start(adev->ext_hw_plugin, uc_info))
3153 ALOGE("%s: failed to start ext hw plugin", __func__);
3154 }
3155
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003156 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d) format(%#x)",
3157 __func__, adev->snd_card, out->pcm_device_id, out->config.format);
Haynes Mathew George16081042017-05-31 17:16:49 -07003158
3159 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
Arun Mirpuriebe78a72018-10-04 18:23:46 -07003160 ALOGD("%s: Starting MMAP stream", __func__);
Haynes Mathew George16081042017-05-31 17:16:49 -07003161 if (out->pcm == NULL || !pcm_is_ready(out->pcm)) {
3162 ALOGE("%s: pcm stream not ready", __func__);
3163 goto error_open;
3164 }
3165 ret = pcm_start(out->pcm);
3166 if (ret < 0) {
3167 ALOGE("%s: MMAP pcm_start failed ret %d", __func__, ret);
3168 goto error_open;
3169 }
Arun Mirpuriebe78a72018-10-04 18:23:46 -07003170 out_set_mmap_volume(&out->stream, out->volume_l, out->volume_r);
Haynes Mathew George16081042017-05-31 17:16:49 -07003171 } else if (!is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003172 unsigned int flags = PCM_OUT;
3173 unsigned int pcm_open_retry_count = 0;
3174 if (out->usecase == USECASE_AUDIO_PLAYBACK_AFE_PROXY) {
3175 flags |= PCM_MMAP | PCM_NOIRQ;
3176 pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT;
Haynes Mathew George5beddd42016-06-27 18:33:40 -07003177 } else if (out->realtime) {
Haynes Mathew George4ab3ba92017-12-11 14:49:43 -08003178 flags |= PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003179 } else
3180 flags |= PCM_MONOTONIC;
3181
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08003182 if ((adev->vr_audio_mode_enabled) &&
3183 (out->flags & AUDIO_OUTPUT_FLAG_RAW)) {
3184 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
3185 "PCM_Dev %d Topology", out->pcm_device_id);
3186 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
3187 if (!ctl) {
3188 ALOGI("%s: Could not get ctl for mixer cmd might be ULL - %s",
3189 __func__, mixer_ctl_name);
3190 } else {
3191 //if success use ULLPP
3192 ALOGI("%s: mixer ctrl %s succeeded setting up ULL for %d",
3193 __func__, mixer_ctl_name, out->pcm_device_id);
3194 //There is a still a possibility that some sessions
3195 // that request for FAST|RAW when 3D audio is active
3196 //can go through ULLPP. Ideally we expects apps to
3197 //listen to audio focus and stop concurrent playback
3198 //Also, we will look for mode flag (voice_in_communication)
3199 //before enabling the realtime flag.
3200 mixer_ctl_set_enum_by_string(ctl, perf_mode[1]);
3201 }
3202 }
3203
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003204 while (1) {
Haynes Mathew George380745d2017-10-04 15:27:45 -07003205 ATRACE_BEGIN("pcm_open");
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003206 out->pcm = pcm_open(adev->snd_card, out->pcm_device_id,
3207 flags, &out->config);
Haynes Mathew George380745d2017-10-04 15:27:45 -07003208 ATRACE_END();
Sharad Sangle90e613f2018-05-04 16:15:38 +05303209 if (errno == ENETRESET && !pcm_is_ready(out->pcm)) {
3210 ALOGE("%s: pcm_open failed errno:%d\n", __func__, errno);
3211 out->card_status = CARD_STATUS_OFFLINE;
3212 adev->card_status = CARD_STATUS_OFFLINE;
3213 ret = -EIO;
3214 goto error_open;
3215 }
3216
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003217 if (out->pcm == NULL || !pcm_is_ready(out->pcm)) {
3218 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
3219 if (out->pcm != NULL) {
3220 pcm_close(out->pcm);
3221 out->pcm = NULL;
3222 }
3223 if (pcm_open_retry_count-- == 0) {
3224 ret = -EIO;
3225 goto error_open;
3226 }
3227 usleep(PROXY_OPEN_WAIT_TIME * 1000);
3228 continue;
3229 }
3230 break;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003231 }
Surendar Karka5a18a7a2018-04-26 11:28:38 +05303232 platform_set_stream_channel_map(adev->platform, out->channel_mask,
3233 out->pcm_device_id, &out->channel_map_param.channel_map[0]);
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07003234
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07003235 ALOGV("%s: pcm_prepare", __func__);
3236 if (pcm_is_ready(out->pcm)) {
Haynes Mathew George380745d2017-10-04 15:27:45 -07003237 ATRACE_BEGIN("pcm_prepare");
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07003238 ret = pcm_prepare(out->pcm);
Haynes Mathew George380745d2017-10-04 15:27:45 -07003239 ATRACE_END();
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07003240 if (ret < 0) {
3241 ALOGE("%s: pcm_prepare returned %d", __func__, ret);
3242 pcm_close(out->pcm);
3243 out->pcm = NULL;
3244 goto error_open;
3245 }
3246 }
Zhou Song2b8f28f2017-09-11 10:51:38 +08003247 // apply volume for voip playback after path is set up
3248 if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP)
3249 out_set_voip_volume(&out->stream, out->volume_l, out->volume_r);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003250 } else {
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -07003251 platform_set_stream_channel_map(adev->platform, out->channel_mask,
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05303252 out->pcm_device_id, &out->channel_map_param.channel_map[0]);
Xiaojun Sang785b5da2017-08-03 15:52:29 +08003253 audio_enable_asm_bit_width_enforce_mode(adev->mixer,
3254 adev->dsp_bit_width_enforce_mode,
3255 true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003256 out->pcm = NULL;
Haynes Mathew George380745d2017-10-04 15:27:45 -07003257 ATRACE_BEGIN("compress_open");
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08003258 out->compr = compress_open(adev->snd_card,
3259 out->pcm_device_id,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003260 COMPRESS_IN, &out->compr_config);
Haynes Mathew George380745d2017-10-04 15:27:45 -07003261 ATRACE_END();
Sharad Sangle90e613f2018-05-04 16:15:38 +05303262 if (errno == ENETRESET && !is_compress_ready(out->compr)) {
3263 ALOGE("%s: compress_open failed errno:%d\n", __func__, errno);
3264 adev->card_status = CARD_STATUS_OFFLINE;
3265 out->card_status = CARD_STATUS_OFFLINE;
3266 ret = -EIO;
3267 goto error_open;
3268 }
3269
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003270 if (out->compr && !is_compress_ready(out->compr)) {
Haynes Mathew Georgeee5836f2017-11-21 18:02:10 -08003271 ALOGE("%s: failed /w error %s", __func__, compress_get_error(out->compr));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003272 compress_close(out->compr);
3273 out->compr = NULL;
3274 ret = -EIO;
3275 goto error_open;
3276 }
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05303277 /* compress_open sends params of the track, so reset the flag here */
3278 out->is_compr_metadata_avail = false;
3279
Ben Rombergerd771a7c2017-02-22 18:05:17 -08003280 if (out->client_callback)
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003281 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07003282
Fred Oh3f43e742015-03-04 18:42:34 -08003283 /* Since small bufs uses blocking writes, a write will be blocked
3284 for the default max poll time (20s) in the event of an SSR.
3285 Reduce the poll time to observe and deal with SSR faster.
3286 */
Ashish Jain5106d362016-05-11 19:23:33 +05303287 if (!out->non_blocking) {
Fred Oh3f43e742015-03-04 18:42:34 -08003288 compress_set_max_poll_wait(out->compr, 1000);
3289 }
3290
Manish Dewangan69426c82017-01-30 17:35:36 +05303291 audio_extn_utils_compress_set_render_mode(out);
Manish Dewangan58229382017-02-02 15:48:41 +05303292 audio_extn_utils_compress_set_clk_rec_mode(uc_info);
Manish Dewangan69426c82017-01-30 17:35:36 +05303293
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08003294 audio_extn_dts_create_state_notifier_node(out->usecase);
3295 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
3296 popcount(out->channel_mask),
3297 out->playback_started);
3298
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08003299#ifdef DS1_DOLBY_DDP_ENABLED
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05303300 if (audio_extn_utils_is_dolby_format(out->format))
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08003301 audio_extn_dolby_send_ddp_endp_params(adev);
3302#endif
Preetam Singh Ranawatd18d8832017-02-08 17:34:54 +05303303 if (!(audio_extn_passthru_is_passthrough_stream(out)) &&
3304 (out->sample_rate != 176400 && out->sample_rate <= 192000)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07003305 if (adev->visualizer_start_output != NULL)
3306 adev->visualizer_start_output(out->handle, out->pcm_device_id);
3307 if (adev->offload_effects_start_output != NULL)
Ashish Jain5106d362016-05-11 19:23:33 +05303308 adev->offload_effects_start_output(out->handle, out->pcm_device_id, adev->mixer);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08003309 audio_extn_check_and_set_dts_hpx_state(adev);
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07003310 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003311 }
Haynes Mathew George5beddd42016-06-27 18:33:40 -07003312
3313 if (ret == 0) {
3314 register_out_stream(out);
3315 if (out->realtime) {
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07003316 if (out->pcm == NULL || !pcm_is_ready(out->pcm)) {
3317 ALOGE("%s: pcm stream not ready", __func__);
3318 goto error_open;
3319 }
Haynes Mathew George380745d2017-10-04 15:27:45 -07003320 ATRACE_BEGIN("pcm_start");
Haynes Mathew George5beddd42016-06-27 18:33:40 -07003321 ret = pcm_start(out->pcm);
Haynes Mathew George380745d2017-10-04 15:27:45 -07003322 ATRACE_END();
Haynes Mathew George5beddd42016-06-27 18:33:40 -07003323 if (ret < 0)
3324 goto error_open;
3325 }
3326 }
3327
Sudheer Papothifa9d2282015-09-17 01:53:25 +05303328 audio_extn_perf_lock_release(&adev->perf_lock_handle);
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07003329 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07003330
Manish Dewangan21a850a2017-08-14 12:03:55 +05303331 if (out->ip_hdlr_handle) {
Vidyakumar Athota6d655882017-05-22 18:26:24 -07003332 ret = audio_extn_ip_hdlr_intf_open(out->ip_hdlr_handle, true, out, out->usecase);
Naresh Tanniru85819452017-05-04 18:55:45 -07003333 if (ret < 0)
3334 ALOGE("%s: audio_extn_ip_hdlr_intf_open failed %d",__func__, ret);
3335 }
3336
Vignesh Kulothungan3b5fae52017-09-25 12:16:30 -07003337 // consider a scenario where on pause lower layers are tear down.
3338 // so on resume, swap mixer control need to be sent only when
3339 // backend is active, hence rather than sending from enable device
3340 // sending it from start of streamtream
3341
3342 platform_set_swap_channels(adev, true);
3343
Haynes Mathew George380745d2017-10-04 15:27:45 -07003344 ATRACE_END();
Haynes Mathew George5beddd42016-06-27 18:33:40 -07003345 return ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003346error_open:
Sudheer Papothifa9d2282015-09-17 01:53:25 +05303347 audio_extn_perf_lock_release(&adev->perf_lock_handle);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003348 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08003349error_config:
Laxminath Kasam2cb4b752015-09-24 03:59:15 +05303350 /*
3351 * sleep 50ms to allow sufficient time for kernel
3352 * drivers to recover incases like SSR.
3353 */
3354 usleep(50000);
Haynes Mathew George380745d2017-10-04 15:27:45 -07003355 ATRACE_END();
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08003356 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003357}
3358
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003359static int check_input_parameters(uint32_t sample_rate,
3360 audio_format_t format,
3361 int channel_count)
3362{
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003363 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003364
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05303365 if (((format != AUDIO_FORMAT_PCM_16_BIT) && (format != AUDIO_FORMAT_PCM_8_24_BIT) &&
3366 (format != AUDIO_FORMAT_PCM_24_BIT_PACKED) && (format != AUDIO_FORMAT_PCM_32_BIT) &&
3367 (format != AUDIO_FORMAT_PCM_FLOAT)) &&
Mingming Yine62d7842013-10-25 16:26:03 -07003368 !voice_extn_compress_voip_is_format_supported(format) &&
Haynes Mathew George484e8d22017-07-31 18:55:17 -07003369 !audio_extn_compr_cap_format_supported(format))
3370 ret = -EINVAL;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003371
3372 switch (channel_count) {
3373 case 1:
3374 case 2:
Chaithanya Krishna Bacharaju9955b162016-05-25 16:25:53 +05303375 case 3:
3376 case 4:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003377 case 6:
Karthikeyan Mani07faa602018-08-20 11:01:32 -07003378 case 8:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003379 break;
3380 default:
3381 ret = -EINVAL;
3382 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003383
3384 switch (sample_rate) {
3385 case 8000:
3386 case 11025:
3387 case 12000:
3388 case 16000:
3389 case 22050:
3390 case 24000:
3391 case 32000:
3392 case 44100:
3393 case 48000:
Haynes Mathew Georgec9253d12017-12-13 15:58:28 -08003394 case 88200:
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05303395 case 96000:
Haynes Mathew Georgec9253d12017-12-13 15:58:28 -08003396 case 176400:
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05303397 case 192000:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003398 break;
3399 default:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003400 ret = -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003401 }
3402
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003403 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003404}
3405
Naresh Tanniru04f71882018-06-26 17:46:22 +05303406
3407/** Add a value in a list if not already present.
3408 * @return true if value was successfully inserted or already present,
3409 * false if the list is full and does not contain the value.
3410 */
3411static bool register_uint(uint32_t value, uint32_t* list, size_t list_length) {
3412 for (size_t i = 0; i < list_length; i++) {
3413 if (list[i] == value) return true; // value is already present
3414 if (list[i] == 0) { // no values in this slot
3415 list[i] = value;
3416 return true; // value inserted
3417 }
3418 }
3419 return false; // could not insert value
3420}
3421
3422/** Add channel_mask in supported_channel_masks if not already present.
3423 * @return true if channel_mask was successfully inserted or already present,
3424 * false if supported_channel_masks is full and does not contain channel_mask.
3425 */
3426static void register_channel_mask(audio_channel_mask_t channel_mask,
3427 audio_channel_mask_t supported_channel_masks[static MAX_SUPPORTED_CHANNEL_MASKS]) {
3428 ALOGE_IF(!register_uint(channel_mask, supported_channel_masks, MAX_SUPPORTED_CHANNEL_MASKS),
3429 "%s: stream can not declare supporting its channel_mask %x", __func__, channel_mask);
3430}
3431
3432/** Add format in supported_formats if not already present.
3433 * @return true if format was successfully inserted or already present,
3434 * false if supported_formats is full and does not contain format.
3435 */
3436static void register_format(audio_format_t format,
3437 audio_format_t supported_formats[static MAX_SUPPORTED_FORMATS]) {
3438 ALOGE_IF(!register_uint(format, supported_formats, MAX_SUPPORTED_FORMATS),
3439 "%s: stream can not declare supporting its format %x", __func__, format);
3440}
3441/** Add sample_rate in supported_sample_rates if not already present.
3442 * @return true if sample_rate was successfully inserted or already present,
3443 * false if supported_sample_rates is full and does not contain sample_rate.
3444 */
3445static void register_sample_rate(uint32_t sample_rate,
3446 uint32_t supported_sample_rates[static MAX_SUPPORTED_SAMPLE_RATES]) {
3447 ALOGE_IF(!register_uint(sample_rate, supported_sample_rates, MAX_SUPPORTED_SAMPLE_RATES),
3448 "%s: stream can not declare supporting its sample rate %x", __func__, sample_rate);
3449}
3450
Karthikeyan Mani35531922018-11-07 15:44:13 -08003451static inline uint32_t lcm(uint32_t num1, uint32_t num2)
3452{
3453 uint32_t high = num1, low = num2, temp = 0;
3454
3455 if (!num1 || !num2)
3456 return 0;
3457
3458 if (num1 < num2) {
3459 high = num2;
3460 low = num1;
3461 }
3462
3463 while (low != 0) {
3464 temp = low;
3465 low = high % low;
3466 high = temp;
3467 }
3468 return (num1 * num2)/high;
3469}
3470
3471static inline uint32_t nearest_multiple(uint32_t num, uint32_t multiplier)
3472{
3473 uint32_t remainder = 0;
3474
3475 if (!multiplier)
3476 return num;
3477
3478 remainder = num % multiplier;
3479 if (remainder)
3480 num += (multiplier - remainder);
3481
3482 return num;
3483}
3484
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003485static size_t get_input_buffer_size(uint32_t sample_rate,
3486 audio_format_t format,
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07003487 int channel_count,
3488 bool is_low_latency)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003489{
3490 size_t size = 0;
Karthikeyan Mani35531922018-11-07 15:44:13 -08003491 uint32_t bytes_per_period_sample = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003492
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07003493 if (check_input_parameters(sample_rate, format, channel_count) != 0)
3494 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003495
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07003496 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07003497 if (is_low_latency)
3498 size = configured_low_latency_capture_period_size;
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05303499
Karthikeyan Mani35531922018-11-07 15:44:13 -08003500 bytes_per_period_sample = audio_bytes_per_sample(format) * channel_count;
3501 size *= bytes_per_period_sample;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003502
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07003503 /* make sure the size is multiple of 32 bytes
3504 * At 48 kHz mono 16-bit PCM:
3505 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
3506 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
Karthikeyan Mani35531922018-11-07 15:44:13 -08003507 * Also, make sure the size is multiple of bytes per period sample
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07003508 */
Karthikeyan Mani35531922018-11-07 15:44:13 -08003509 size = nearest_multiple(size, lcm(32, bytes_per_period_sample));
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07003510
3511 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003512}
3513
Ashish Jain058165c2016-09-28 23:18:48 +05303514static size_t get_output_period_size(uint32_t sample_rate,
3515 audio_format_t format,
3516 int channel_count,
3517 int duration /*in millisecs*/)
3518{
3519 size_t size = 0;
3520 uint32_t bytes_per_sample = audio_bytes_per_sample(format);
3521
3522 if ((duration == 0) || (sample_rate == 0) ||
3523 (bytes_per_sample == 0) || (channel_count == 0)) {
3524 ALOGW("Invalid config duration %d sr %d bps %d ch %d", duration, sample_rate,
3525 bytes_per_sample, channel_count);
3526 return -EINVAL;
3527 }
3528
3529 size = (sample_rate *
3530 duration *
3531 bytes_per_sample *
3532 channel_count) / 1000;
3533 /*
3534 * To have same PCM samples for all channels, the buffer size requires to
3535 * be multiple of (number of channels * bytes per sample)
3536 * For writes to succeed, the buffer must be written at address which is multiple of 32
3537 */
3538 size = ALIGN(size, (bytes_per_sample * channel_count * 32));
3539
3540 return (size/(channel_count * bytes_per_sample));
3541}
3542
Zhou Song48453a02018-01-10 17:50:59 +08003543static uint64_t get_actual_pcm_frames_rendered(struct stream_out *out, struct timespec *timestamp)
Ashish Jain5106d362016-05-11 19:23:33 +05303544{
3545 uint64_t actual_frames_rendered = 0;
3546 size_t kernel_buffer_size = out->compr_config.fragment_size * out->compr_config.fragments;
3547
3548 /* This adjustment accounts for buffering after app processor.
3549 * It is based on estimated DSP latency per use case, rather than exact.
3550 */
3551 int64_t platform_latency = platform_render_latency(out->usecase) *
3552 out->sample_rate / 1000000LL;
3553
Zhou Song48453a02018-01-10 17:50:59 +08003554 pthread_mutex_lock(&out->position_query_lock);
Ashish Jain5106d362016-05-11 19:23:33 +05303555 /* not querying actual state of buffering in kernel as it would involve an ioctl call
3556 * which then needs protection, this causes delay in TS query for pcm_offload usecase
3557 * hence only estimate.
3558 */
3559 int64_t signed_frames = out->written - kernel_buffer_size;
3560
3561 signed_frames = signed_frames / (audio_bytes_per_sample(out->format) * popcount(out->channel_mask)) - platform_latency;
3562
Zhou Song48453a02018-01-10 17:50:59 +08003563 if (signed_frames > 0) {
Ashish Jain5106d362016-05-11 19:23:33 +05303564 actual_frames_rendered = signed_frames;
Zhou Song48453a02018-01-10 17:50:59 +08003565 if (timestamp != NULL )
3566 *timestamp = out->writeAt;
3567 } else if (timestamp != NULL) {
3568 clock_gettime(CLOCK_MONOTONIC, timestamp);
3569 }
3570 pthread_mutex_unlock(&out->position_query_lock);
Ashish Jain5106d362016-05-11 19:23:33 +05303571
3572 ALOGVV("%s signed frames %lld out_written %lld kernel_buffer_size %d"
3573 "bytes/sample %zu channel count %d", __func__,(long long int)signed_frames,
3574 (long long int)out->written, (int)kernel_buffer_size,
3575 audio_bytes_per_sample(out->compr_config.codec->format),
3576 popcount(out->channel_mask));
3577
3578 return actual_frames_rendered;
3579}
3580
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003581static uint32_t out_get_sample_rate(const struct audio_stream *stream)
3582{
3583 struct stream_out *out = (struct stream_out *)stream;
3584
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003585 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003586}
3587
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07003588static int out_set_sample_rate(struct audio_stream *stream __unused,
3589 uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003590{
3591 return -ENOSYS;
3592}
3593
3594static size_t out_get_buffer_size(const struct audio_stream *stream)
3595{
3596 struct stream_out *out = (struct stream_out *)stream;
3597
Varun Balaraje49253e2017-07-06 19:48:56 +05303598 if (is_interactive_usecase(out->usecase)) {
Sri Karri27279e12017-08-07 16:05:20 +05303599 return out->config.period_size * out->config.period_count;
Varun Balaraje49253e2017-07-06 19:48:56 +05303600 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Naresh Tanniruee3499a2017-01-05 14:05:35 +05303601 if (out->flags & AUDIO_OUTPUT_FLAG_TIMESTAMP)
3602 return out->compr_config.fragment_size - sizeof(struct snd_codec_metadata);
3603 else
3604 return out->compr_config.fragment_size;
3605 } else if(out->usecase == USECASE_COMPRESS_VOIP_CALL)
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08003606 return voice_extn_compress_voip_out_get_buffer_size(out);
Vikram Panduranga93f080e2017-06-07 18:16:14 -07003607 else if(out->usecase == USECASE_AUDIO_PLAYBACK_VOIP)
3608 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 +05303609 else if (is_offload_usecase(out->usecase) &&
3610 out->flags == AUDIO_OUTPUT_FLAG_DIRECT)
Ashish Jain83a6cc22016-06-28 14:34:17 +05303611 return out->hal_fragment_size;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003612
Haynes Mathew George5beddd42016-06-27 18:33:40 -07003613 return out->config.period_size * out->af_period_multiplier *
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07003614 audio_stream_out_frame_size((const struct audio_stream_out *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003615}
3616
3617static uint32_t out_get_channels(const struct audio_stream *stream)
3618{
3619 struct stream_out *out = (struct stream_out *)stream;
3620
3621 return out->channel_mask;
3622}
3623
3624static audio_format_t out_get_format(const struct audio_stream *stream)
3625{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003626 struct stream_out *out = (struct stream_out *)stream;
3627
3628 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003629}
3630
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07003631static int out_set_format(struct audio_stream *stream __unused,
3632 audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003633{
3634 return -ENOSYS;
3635}
3636
3637static int out_standby(struct audio_stream *stream)
3638{
3639 struct stream_out *out = (struct stream_out *)stream;
3640 struct audio_device *adev = out->dev;
Haynes Mathew George16081042017-05-31 17:16:49 -07003641 bool do_stop = true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003642
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05303643 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
3644 stream, out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003645
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07003646 lock_output_stream(out);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003647 if (!out->standby) {
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07003648 if (adev->adm_deregister_stream)
3649 adev->adm_deregister_stream(adev->adm_data, out->handle);
3650
Haynes Mathew George7fce0a52016-06-23 18:22:27 -07003651 if (is_offload_usecase(out->usecase))
3652 stop_compressed_output_l(out);
3653
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08003654 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003655 out->standby = true;
Zhou Songa8895042016-07-05 17:54:22 +08003656 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
3657 voice_extn_compress_voip_close_output_stream(stream);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303658 out->started = 0;
Zhou Songa8895042016-07-05 17:54:22 +08003659 pthread_mutex_unlock(&adev->lock);
3660 pthread_mutex_unlock(&out->lock);
3661 ALOGD("VOIP output entered standby");
3662 return 0;
3663 } else if (!is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003664 if (out->pcm) {
3665 pcm_close(out->pcm);
3666 out->pcm = NULL;
3667 }
Haynes Mathew George16081042017-05-31 17:16:49 -07003668 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
3669 do_stop = out->playback_started;
3670 out->playback_started = false;
3671 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003672 } else {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07003673 ALOGD("copl(%p):standby", out);
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05303674 out->send_next_track_params = false;
3675 out->is_compr_metadata_avail = false;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07003676 out->gapless_mdata.encoder_delay = 0;
3677 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003678 if (out->compr != NULL) {
3679 compress_close(out->compr);
3680 out->compr = NULL;
3681 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08003682 }
Haynes Mathew George16081042017-05-31 17:16:49 -07003683 if (do_stop) {
3684 stop_output_stream(out);
3685 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08003686 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003687 }
3688 pthread_mutex_unlock(&out->lock);
Ashish Jainbbce4322016-02-16 13:25:27 +05303689 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003690 return 0;
3691}
3692
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05303693static int out_on_error(struct audio_stream *stream)
3694{
3695 struct stream_out *out = (struct stream_out *)stream;
Ben Rombergerfd02a2f2018-09-17 10:23:23 -07003696 int status = 0;
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05303697
3698 lock_output_stream(out);
Haynes Mathew Georgeee5836f2017-11-21 18:02:10 -08003699 // always send CMD_ERROR for offload streams, this
3700 // is needed e.g. when SSR happens within compress_open
3701 // since the stream is active, offload_callback_thread is also active.
3702 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
3703 stop_compressed_output_l(out);
Ben Rombergerfd02a2f2018-09-17 10:23:23 -07003704 }
3705 pthread_mutex_unlock(&out->lock);
3706
3707 status = out_standby(&out->stream.common);
3708
3709 lock_output_stream(out);
3710 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Haynes Mathew Georgeee5836f2017-11-21 18:02:10 -08003711 send_offload_cmd_l(out, OFFLOAD_CMD_ERROR);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05303712 }
3713 pthread_mutex_unlock(&out->lock);
3714
Ben Rombergerfd02a2f2018-09-17 10:23:23 -07003715 return status;
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05303716}
3717
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303718/*
3719 *standby implementation without locks, assumes that the callee already
3720 *has taken adev and out lock.
3721 */
3722int out_standby_l(struct audio_stream *stream)
3723{
3724 struct stream_out *out = (struct stream_out *)stream;
3725 struct audio_device *adev = out->dev;
3726
3727 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
3728 stream, out->usecase, use_case_table[out->usecase]);
3729
3730 if (!out->standby) {
Haynes Mathew George380745d2017-10-04 15:27:45 -07003731 ATRACE_BEGIN("out_standby_l");
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303732 if (adev->adm_deregister_stream)
3733 adev->adm_deregister_stream(adev->adm_data, out->handle);
3734
3735 if (is_offload_usecase(out->usecase))
3736 stop_compressed_output_l(out);
3737
3738 out->standby = true;
3739 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
3740 voice_extn_compress_voip_close_output_stream(stream);
3741 out->started = 0;
3742 ALOGD("VOIP output entered standby");
Haynes Mathew George380745d2017-10-04 15:27:45 -07003743 ATRACE_END();
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303744 return 0;
3745 } else if (!is_offload_usecase(out->usecase)) {
3746 if (out->pcm) {
3747 pcm_close(out->pcm);
3748 out->pcm = NULL;
3749 }
3750 } else {
3751 ALOGD("copl(%p):standby", out);
3752 out->send_next_track_params = false;
3753 out->is_compr_metadata_avail = false;
3754 out->gapless_mdata.encoder_delay = 0;
3755 out->gapless_mdata.encoder_padding = 0;
3756 if (out->compr != NULL) {
3757 compress_close(out->compr);
3758 out->compr = NULL;
3759 }
3760 }
3761 stop_output_stream(out);
Haynes Mathew George380745d2017-10-04 15:27:45 -07003762 ATRACE_END();
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303763 }
3764 ALOGD("%s: exit", __func__);
3765 return 0;
3766}
3767
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07003768static int out_dump(const struct audio_stream *stream __unused,
3769 int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003770{
3771 return 0;
3772}
3773
Haynes Mathew George352f27b2013-07-26 00:00:15 -07003774static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
3775{
3776 int ret = 0;
3777 char value[32];
ApurupaPattapu2e084df2013-12-18 15:47:59 -08003778
Haynes Mathew George352f27b2013-07-26 00:00:15 -07003779 if (!out || !parms) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08003780 ALOGE("%s: return invalid ",__func__);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07003781 return -EINVAL;
3782 }
3783
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +05303784 ret = audio_extn_parse_compress_metadata(out, parms);
Weiyin Jiang18ac4e92015-03-15 15:03:40 +08003785
Haynes Mathew George352f27b2013-07-26 00:00:15 -07003786 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
3787 if (ret >= 0) {
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +05303788 out->gapless_mdata.encoder_delay = atoi(value); //whats a good limit check?
Haynes Mathew George352f27b2013-07-26 00:00:15 -07003789 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07003790 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
3791 if (ret >= 0) {
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +05303792 out->gapless_mdata.encoder_padding = atoi(value);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07003793 }
3794
Haynes Mathew George352f27b2013-07-26 00:00:15 -07003795 ALOGV("%s new encoder delay %u and padding %u", __func__,
3796 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
3797
3798 return 0;
3799}
3800
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003801static bool output_drives_call(struct audio_device *adev, struct stream_out *out)
3802{
3803 return out == adev->primary_output || out == adev->voice_tx_output;
3804}
Haynes Mathew George352f27b2013-07-26 00:00:15 -07003805
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05303806// note: this call is safe only if the stream_cb is
3807// removed first in close_output_stream (as is done now).
3808static void out_snd_mon_cb(void * stream, struct str_parms * parms)
3809{
3810 if (!stream || !parms)
3811 return;
3812
3813 struct stream_out *out = (struct stream_out *)stream;
3814 struct audio_device *adev = out->dev;
3815
3816 card_status_t status;
3817 int card;
3818 if (parse_snd_card_status(parms, &card, &status) < 0)
3819 return;
3820
3821 pthread_mutex_lock(&adev->lock);
3822 bool valid_cb = (card == adev->snd_card);
3823 pthread_mutex_unlock(&adev->lock);
3824
3825 if (!valid_cb)
3826 return;
3827
3828 lock_output_stream(out);
3829 if (out->card_status != status)
3830 out->card_status = status;
3831 pthread_mutex_unlock(&out->lock);
3832
3833 ALOGI("out_snd_mon_cb for card %d usecase %s, status %s", card,
3834 use_case_table[out->usecase],
3835 status == CARD_STATUS_OFFLINE ? "offline" : "online");
3836
3837 if (status == CARD_STATUS_OFFLINE)
3838 out_on_error(stream);
3839
3840 return;
3841}
3842
Kevin Rocardfce19002017-08-07 19:21:36 -07003843static int get_alive_usb_card(struct str_parms* parms) {
3844 int card;
3845 if ((str_parms_get_int(parms, "card", &card) >= 0) &&
3846 !audio_extn_usb_alive(card)) {
3847 return card;
3848 }
3849 return -ENODEV;
3850}
3851
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003852static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
3853{
3854 struct stream_out *out = (struct stream_out *)stream;
3855 struct audio_device *adev = out->dev;
3856 struct str_parms *parms;
3857 char value[32];
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08003858 int ret = 0, val = 0, err;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303859 bool bypass_a2dp = false;
Garmond Leung5fd0b552018-04-17 11:56:12 -07003860 bool reconfig = false;
3861 unsigned long service_interval = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003862
sangwoobc677242013-08-08 16:53:43 +09003863 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003864 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003865 parms = str_parms_create_str(kvpairs);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05303866 if (!parms)
3867 goto error;
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08003868 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
3869 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003870 val = atoi(value);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07003871 lock_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08003872 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003873
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003874 /*
Weiyin Jiang4256eeb2016-05-19 13:28:30 +08003875 * When HDMI cable is unplugged the music playback is paused and
3876 * the policy manager sends routing=0. But the audioflinger continues
3877 * to write data until standby time (3sec). As the HDMI core is
3878 * turned off, the write gets blocked.
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003879 * Avoid this by routing audio to speaker until standby.
3880 */
Weiyin Jiang4256eeb2016-05-19 13:28:30 +08003881 if ((out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL) &&
3882 (val == AUDIO_DEVICE_NONE) &&
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05303883 !audio_extn_passthru_is_passthrough_stream(out) &&
Weiyin Jiang4256eeb2016-05-19 13:28:30 +08003884 (platform_get_edid_info(adev->platform) != 0) /* HDMI disconnected */) {
3885 val = AUDIO_DEVICE_OUT_SPEAKER;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003886 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303887 /*
3888 * When A2DP is disconnected the
3889 * music playback is paused and the policy manager sends routing=0
3890 * But the audioflingercontinues to write data until standby time
3891 * (3sec). As BT is turned off, the write gets blocked.
3892 * Avoid this by routing audio to speaker until standby.
3893 */
Naresh Tanniruf7e9e632016-11-04 14:54:20 -07003894 if ((out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) &&
Aniket Kumar Lata1fb11cb2017-09-08 13:48:55 -07003895 (val == AUDIO_DEVICE_NONE) &&
3896 !audio_extn_a2dp_is_ready()) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303897 val = AUDIO_DEVICE_OUT_SPEAKER;
3898 }
Sharad Sangled0a50b22018-04-05 23:28:32 +05303899 /*
3900 * When USB headset is disconnected the music platback paused
3901 * and the policy manager send routing=0. But if the USB is connected
3902 * back before the standby time, AFE is not closed and opened
3903 * when USB is connected back. So routing to speker will guarantee
3904 * AFE reconfiguration and AFE will be opend once USB is connected again
3905 */
3906 if ((out->devices & AUDIO_DEVICE_OUT_ALL_USB) &&
3907 (val == AUDIO_DEVICE_NONE) &&
3908 !audio_extn_usb_connected(parms)) {
3909 val = AUDIO_DEVICE_OUT_SPEAKER;
3910 }
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05303911 /* To avoid a2dp to sco overlapping / BT device improper state
3912 * check with BT lib about a2dp streaming support before routing
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05303913 */
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05303914 if (val & AUDIO_DEVICE_OUT_ALL_A2DP) {
3915 if (!audio_extn_a2dp_is_ready()) {
3916 if (val & AUDIO_DEVICE_OUT_SPEAKER) {
3917 //combo usecase just by pass a2dp
3918 ALOGW("%s: A2DP profile is not ready,routing to speaker only", __func__);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303919 bypass_a2dp = true;
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05303920 } else {
3921 ALOGE("%s: A2DP profile is not ready,ignoring routing request", __func__);
3922 /* update device to a2dp and don't route as BT returned error
3923 * However it is still possible a2dp routing called because
3924 * of current active device disconnection (like wired headset)
3925 */
3926 out->devices = val;
3927 pthread_mutex_unlock(&out->lock);
3928 pthread_mutex_unlock(&adev->lock);
3929 goto error;
3930 }
3931 }
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05303932 }
Kevin Rocardfce19002017-08-07 19:21:36 -07003933
3934 audio_devices_t new_dev = val;
3935
3936 // Workaround: If routing to an non existing usb device, fail gracefully
3937 // The routing request will otherwise block during 10 second
3938 int card;
3939 if (audio_is_usb_out_device(new_dev) &&
3940 (card = get_alive_usb_card(parms)) >= 0) {
3941
3942 ALOGW("out_set_parameters() ignoring rerouting to non existing USB card %d", card);
3943 pthread_mutex_unlock(&adev->lock);
3944 pthread_mutex_unlock(&out->lock);
3945 ret = -ENOSYS;
3946 goto routing_fail;
3947 }
3948
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003949 /*
3950 * select_devices() call below switches all the usecases on the same
3951 * backend to the new device. Refer to check_usecases_codec_backend() in
3952 * the select_devices(). But how do we undo this?
3953 *
3954 * For example, music playback is active on headset (deep-buffer usecase)
3955 * and if we go to ringtones and select a ringtone, low-latency usecase
3956 * will be started on headset+speaker. As we can't enable headset+speaker
3957 * and headset devices at the same time, select_devices() switches the music
3958 * playback to headset+speaker while starting low-lateny usecase for ringtone.
3959 * So when the ringtone playback is completed, how do we undo the same?
3960 *
3961 * We are relying on the out_set_parameters() call on deep-buffer output,
3962 * once the ringtone playback is ended.
3963 * NOTE: We should not check if the current devices are same as new devices.
3964 * Because select_devices() must be called to switch back the music
3965 * playback to headset.
3966 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08003967 if (val != 0) {
Haynes Mathew George5beddd42016-06-27 18:33:40 -07003968 audio_devices_t new_dev = val;
3969 bool same_dev = out->devices == new_dev;
3970 out->devices = new_dev;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003971
Ravi Kumar Alamandabe149392014-10-20 17:07:43 -07003972 if (output_drives_call(adev, out)) {
kunleiz61b9c2e2017-11-01 13:47:23 +08003973 if (!voice_is_call_state_active(adev)) {
Ravi Kumar Alamandabe149392014-10-20 17:07:43 -07003974 if (adev->mode == AUDIO_MODE_IN_CALL) {
3975 adev->current_call_output = out;
Garmond Leung5fd0b552018-04-17 11:56:12 -07003976 if (audio_is_usb_out_device(out->devices & AUDIO_DEVICE_OUT_ALL_USB)) {
3977 service_interval = audio_extn_usb_find_service_interval(true, true /*playback*/);
3978 audio_extn_usb_set_service_interval(true /*playback*/,
3979 service_interval,
3980 &reconfig);
3981 ALOGD("%s, svc_int(%ld),reconfig(%d)",__func__,service_interval, reconfig);
3982 }
3983 ret = voice_start_call(adev);
Ravi Kumar Alamandabe149392014-10-20 17:07:43 -07003984 }
3985 } else {
3986 adev->current_call_output = out;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003987 voice_update_devices_for_all_voice_usecases(adev);
Ravi Kumar Alamandabe149392014-10-20 17:07:43 -07003988 }
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08003989 }
Shiv Maliyappanahalli6b32c4c2015-11-04 18:10:20 -08003990
3991 if (!out->standby) {
Haynes Mathew George5beddd42016-06-27 18:33:40 -07003992 if (!same_dev) {
3993 ALOGV("update routing change");
Sudheer Papothi80266982016-08-16 02:36:18 +05303994 audio_extn_perf_lock_acquire(&adev->perf_lock_handle, 0,
3995 adev->perf_lock_opts,
3996 adev->perf_lock_opts_size);
Haynes Mathew George822b5492016-07-01 16:57:24 -07003997 if (adev->adm_on_routing_change)
3998 adev->adm_on_routing_change(adev->adm_data,
3999 out->handle);
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004000 }
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304001 if (!bypass_a2dp) {
4002 select_devices(adev, out->usecase);
4003 } else {
4004 out->devices = AUDIO_DEVICE_OUT_SPEAKER;
4005 select_devices(adev, out->usecase);
4006 out->devices = new_dev;
4007 }
Vignesh Kulothungan3b5fae52017-09-25 12:16:30 -07004008
4009 if (!same_dev) {
4010 // on device switch force swap, lower functions will make sure
4011 // to check if swap is allowed or not.
4012 platform_set_swap_channels(adev, true);
Sudheer Papothi80266982016-08-16 02:36:18 +05304013 audio_extn_perf_lock_release(&adev->perf_lock_handle);
Vignesh Kulothungan3b5fae52017-09-25 12:16:30 -07004014 }
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304015 if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
4016 out->a2dp_compress_mute &&
Zhou Songc66eb7e2017-08-08 18:29:07 +08004017 (!(out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) || audio_extn_a2dp_is_ready())) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304018 pthread_mutex_lock(&out->compr_mute_lock);
4019 out->a2dp_compress_mute = false;
4020 out_set_compr_volume(&out->stream, out->volume_l, out->volume_r);
4021 pthread_mutex_unlock(&out->compr_mute_lock);
kunleizba786432018-01-26 15:31:59 +08004022 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP) {
4023 out_set_voip_volume(&out->stream, out->volume_l, out->volume_r);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304024 }
Shiv Maliyappanahalli6b32c4c2015-11-04 18:10:20 -08004025 }
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08004026 }
4027
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004028 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08004029 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004030 }
Kevin Rocardfce19002017-08-07 19:21:36 -07004031 routing_fail:
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07004032
4033 if (out == adev->primary_output) {
4034 pthread_mutex_lock(&adev->lock);
4035 audio_extn_set_parameters(adev, parms);
4036 pthread_mutex_unlock(&adev->lock);
4037 }
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07004038 if (is_offload_usecase(out->usecase)) {
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07004039 lock_output_stream(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004040 parse_compress_metadata(out, parms);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08004041
4042 audio_extn_dts_create_state_notifier_node(out->usecase);
4043 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
4044 popcount(out->channel_mask),
4045 out->playback_started);
4046
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08004047 pthread_mutex_unlock(&out->lock);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004048 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07004049
Surendar Karka5a18a7a2018-04-26 11:28:38 +05304050 err = str_parms_get_str(parms, AUDIO_PARAMETER_DUAL_MONO, value,
4051 sizeof(value));
4052 if (err >= 0) {
4053 if (!strncmp("true", value, sizeof("true")) || atoi(value))
4054 audio_extn_send_dual_mono_mixing_coefficients(out);
4055 }
4056
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +05304057 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_PROFILE, value, sizeof(value));
4058 if (err >= 0) {
4059 strlcpy(out->profile, value, sizeof(out->profile));
4060 ALOGV("updating stream profile with value '%s'", out->profile);
4061 lock_output_stream(out);
4062 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
4063 &adev->streams_output_cfg_list,
Aalique Grahame65780b52017-09-27 14:59:56 -07004064 out->devices, out->flags, out->hal_op_format,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +05304065 out->sample_rate, out->bit_width,
4066 out->channel_mask, out->profile,
4067 &out->app_type_cfg);
4068 pthread_mutex_unlock(&out->lock);
4069 }
4070
Alexy Joseph98988832017-01-13 14:56:59 -08004071 //suspend, resume handling block
Aniket Kumar Lata932f4872017-11-06 18:29:44 -08004072 //remove QOS only if vendor.audio.hal.dynamic.qos.config.supported is set to true
4073 // and vendor.audio.hal.output.suspend.supported is set to true
4074 if (out->hal_output_suspend_supported && out->dynamic_pm_qos_config_supported) {
Alexy Joseph98988832017-01-13 14:56:59 -08004075 //check suspend parameter only for low latency and if the property
4076 //is enabled
4077 if (str_parms_get_str(parms, "suspend_playback", value, sizeof(value)) >= 0) {
4078 ALOGI("%s: got suspend_playback %s", __func__, value);
4079 lock_output_stream(out);
4080 if (!strncmp(value, "false", 5)) {
4081 //suspend_playback=false is supposed to set QOS value back to 75%
4082 //the mixer control sent with value Enable will achieve that
4083 ret = audio_route_apply_and_update_path(adev->audio_route, out->pm_qos_mixer_path);
4084 } else if (!strncmp (value, "true", 4)) {
4085 //suspend_playback=true is supposed to remove QOS value
4086 //resetting the mixer control will set the default value
4087 //for the mixer control which is Disable and this removes the QOS vote
4088 ret = audio_route_reset_and_update_path(adev->audio_route, out->pm_qos_mixer_path);
4089 } else {
4090 ALOGE("%s: Wrong value sent for suspend_playback, expected true/false,"
4091 " got %s", __func__, value);
4092 ret = -1;
4093 }
4094
4095 if (ret != 0) {
4096 ALOGE("%s: %s mixer ctl failed with %d, ignore suspend/resume setparams",
4097 __func__, out->pm_qos_mixer_path, ret);
4098 }
4099
4100 pthread_mutex_unlock(&out->lock);
4101 }
4102 }
4103 //end suspend, resume handling block
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004104 str_parms_destroy(parms);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05304105error:
Eric Laurent994a6932013-07-17 11:51:42 -07004106 ALOGV("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004107 return ret;
4108}
4109
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004110static bool stream_get_parameter_channels(struct str_parms *query,
4111 struct str_parms *reply,
4112 audio_channel_mask_t *supported_channel_masks) {
4113 int ret = -1;
4114 char value[512];
4115 bool first = true;
4116 size_t i, j;
4117
4118 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) {
4119 ret = 0;
4120 value[0] = '\0';
4121 i = 0;
4122 while (supported_channel_masks[i] != 0) {
4123 for (j = 0; j < ARRAY_SIZE(channels_name_to_enum_table); j++) {
4124 if (channels_name_to_enum_table[j].value == supported_channel_masks[i]) {
4125 if (!first)
Satya Krishna Pindiprolib22ac722017-10-09 15:44:16 +05304126 strlcat(value, "|", sizeof(value));
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004127
Satya Krishna Pindiprolib22ac722017-10-09 15:44:16 +05304128 strlcat(value, channels_name_to_enum_table[j].name, sizeof(value));
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004129 first = false;
4130 break;
4131 }
4132 }
4133 i++;
4134 }
4135 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
4136 }
4137 return ret == 0;
4138}
4139
4140static bool stream_get_parameter_formats(struct str_parms *query,
4141 struct str_parms *reply,
4142 audio_format_t *supported_formats) {
4143 int ret = -1;
4144 char value[256];
4145 size_t i, j;
4146 bool first = true;
4147
4148 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) {
4149 ret = 0;
4150 value[0] = '\0';
4151 i = 0;
4152 while (supported_formats[i] != 0) {
4153 for (j = 0; j < ARRAY_SIZE(formats_name_to_enum_table); j++) {
4154 if (formats_name_to_enum_table[j].value == supported_formats[i]) {
4155 if (!first) {
Satya Krishna Pindiprolib22ac722017-10-09 15:44:16 +05304156 strlcat(value, "|", sizeof(value));
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004157 }
Satya Krishna Pindiprolib22ac722017-10-09 15:44:16 +05304158 strlcat(value, formats_name_to_enum_table[j].name, sizeof(value));
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004159 first = false;
4160 break;
4161 }
4162 }
4163 i++;
4164 }
4165 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_FORMATS, value);
4166 }
4167 return ret == 0;
4168}
4169
4170static bool stream_get_parameter_rates(struct str_parms *query,
4171 struct str_parms *reply,
4172 uint32_t *supported_sample_rates) {
4173
4174 int i;
4175 char value[256];
4176 int ret = -1;
4177 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) {
4178 ret = 0;
4179 value[0] = '\0';
4180 i=0;
4181 int cursor = 0;
4182 while (supported_sample_rates[i]) {
4183 int avail = sizeof(value) - cursor;
4184 ret = snprintf(value + cursor, avail, "%s%d",
4185 cursor > 0 ? "|" : "",
4186 supported_sample_rates[i]);
4187 if (ret < 0 || ret >= avail) {
4188 // if cursor is at the last element of the array
4189 // overwrite with \0 is duplicate work as
4190 // snprintf already put a \0 in place.
4191 // else
4192 // we had space to write the '|' at value[cursor]
4193 // (which will be overwritten) or no space to fill
4194 // the first element (=> cursor == 0)
4195 value[cursor] = '\0';
4196 break;
4197 }
4198 cursor += ret;
4199 ++i;
4200 }
4201 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES,
4202 value);
4203 }
4204 return ret >= 0;
4205}
4206
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004207static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
4208{
4209 struct stream_out *out = (struct stream_out *)stream;
4210 struct str_parms *query = str_parms_create_str(keys);
Alexy Josephaee4fdd2016-01-29 13:02:07 -08004211 char *str = (char*) NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004212 char value[256];
4213 struct str_parms *reply = str_parms_create();
4214 size_t i, j;
4215 int ret;
4216 bool first = true;
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07004217
4218 if (!query || !reply) {
Alexy Josephaee4fdd2016-01-29 13:02:07 -08004219 if (reply) {
4220 str_parms_destroy(reply);
4221 }
4222 if (query) {
4223 str_parms_destroy(query);
4224 }
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07004225 ALOGE("out_get_parameters: failed to allocate mem for query or reply");
4226 return NULL;
4227 }
4228
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004229 ALOGV("%s: %s enter: keys - %s", __func__, use_case_table[out->usecase], keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004230 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
4231 if (ret >= 0) {
4232 value[0] = '\0';
4233 i = 0;
4234 while (out->supported_channel_masks[i] != 0) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004235 for (j = 0; j < ARRAY_SIZE(channels_name_to_enum_table); j++) {
4236 if (channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004237 if (!first) {
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -08004238 strlcat(value, "|", sizeof(value));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004239 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004240 strlcat(value, channels_name_to_enum_table[j].name, sizeof(value));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004241 first = false;
4242 break;
4243 }
4244 }
4245 i++;
4246 }
4247 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
4248 str = str_parms_to_str(reply);
4249 } else {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08004250 voice_extn_out_get_parameters(out, query, reply);
4251 str = str_parms_to_str(reply);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004252 }
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004253
Alexy Joseph62142aa2015-11-16 15:10:34 -08004254
4255 ret = str_parms_get_str(query, "is_direct_pcm_track", value, sizeof(value));
4256 if (ret >= 0) {
4257 value[0] = '\0';
Dhananjay Kumarac341582017-02-23 23:42:25 +05304258 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
4259 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Alexy Joseph62142aa2015-11-16 15:10:34 -08004260 ALOGV("in direct_pcm");
Satya Krishna Pindiprolib6655542017-07-03 19:38:19 +05304261 strlcat(value, "true", sizeof(value));
Alexy Joseph62142aa2015-11-16 15:10:34 -08004262 } else {
4263 ALOGV("not in direct_pcm");
Sharad Sangle3dd5a4a2015-12-10 18:39:17 +05304264 strlcat(value, "false", sizeof(value));
Alexy Joseph62142aa2015-11-16 15:10:34 -08004265 }
4266 str_parms_add_str(reply, "is_direct_pcm_track", value);
Alexy Josephaee4fdd2016-01-29 13:02:07 -08004267 if (str)
4268 free(str);
Alexy Joseph62142aa2015-11-16 15:10:34 -08004269 str = str_parms_to_str(reply);
4270 }
4271
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004272 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS, value, sizeof(value));
4273 if (ret >= 0) {
4274 value[0] = '\0';
4275 i = 0;
4276 first = true;
4277 while (out->supported_formats[i] != 0) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004278 for (j = 0; j < ARRAY_SIZE(formats_name_to_enum_table); j++) {
4279 if (formats_name_to_enum_table[j].value == out->supported_formats[i]) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004280 if (!first) {
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -08004281 strlcat(value, "|", sizeof(value));
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004282 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004283 strlcat(value, formats_name_to_enum_table[j].name, sizeof(value));
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004284 first = false;
4285 break;
4286 }
4287 }
4288 i++;
4289 }
4290 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_FORMATS, value);
Alexy Josephaee4fdd2016-01-29 13:02:07 -08004291 if (str)
4292 free(str);
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004293 str = str_parms_to_str(reply);
4294 }
Mingming Yin3a941d42016-02-17 18:08:05 -08004295
4296 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES, value, sizeof(value));
4297 if (ret >= 0) {
4298 value[0] = '\0';
4299 i = 0;
4300 first = true;
4301 while (out->supported_sample_rates[i] != 0) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004302 for (j = 0; j < ARRAY_SIZE(out_sample_rates_name_to_enum_table); j++) {
4303 if (out_sample_rates_name_to_enum_table[j].value == out->supported_sample_rates[i]) {
Mingming Yin3a941d42016-02-17 18:08:05 -08004304 if (!first) {
4305 strlcat(value, "|", sizeof(value));
4306 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004307 strlcat(value, out_sample_rates_name_to_enum_table[j].name, sizeof(value));
Mingming Yin3a941d42016-02-17 18:08:05 -08004308 first = false;
4309 break;
4310 }
4311 }
4312 i++;
4313 }
4314 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES, value);
4315 if (str)
4316 free(str);
4317 str = str_parms_to_str(reply);
4318 }
4319
Alexy Joseph98988832017-01-13 14:56:59 -08004320 if (str_parms_get_str(query, "supports_hw_suspend", value, sizeof(value)) >= 0) {
4321 //only low latency track supports suspend_resume
4322 str_parms_add_int(reply, "supports_hw_suspend",
Aniket Kumar Lata932f4872017-11-06 18:29:44 -08004323 (out->hal_output_suspend_supported));
Alexy Joseph98988832017-01-13 14:56:59 -08004324 if (str)
4325 free(str);
4326 str = str_parms_to_str(reply);
4327 }
4328
4329
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004330 str_parms_destroy(query);
4331 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07004332 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004333 return str;
4334}
4335
4336static uint32_t out_get_latency(const struct audio_stream_out *stream)
4337{
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004338 uint32_t period_ms;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004339 struct stream_out *out = (struct stream_out *)stream;
Alexy Josephaa54c872014-12-03 02:46:47 -08004340 uint32_t latency = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004341
Alexy Josephaa54c872014-12-03 02:46:47 -08004342 if (is_offload_usecase(out->usecase)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05304343 lock_output_stream(out);
4344 latency = audio_extn_utils_compress_get_dsp_latency(out);
4345 pthread_mutex_unlock(&out->lock);
Haynes Mathew George16081042017-05-31 17:16:49 -07004346 } else if ((out->realtime) ||
4347 (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP)) {
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004348 // since the buffer won't be filled up faster than realtime,
4349 // return a smaller number
4350 if (out->config.rate)
4351 period_ms = (out->af_period_multiplier * out->config.period_size *
4352 1000) / (out->config.rate);
4353 else
4354 period_ms = 0;
4355 latency = period_ms + platform_render_latency(out->usecase)/1000;
Alexy Josephaa54c872014-12-03 02:46:47 -08004356 } else {
4357 latency = (out->config.period_count * out->config.period_size * 1000) /
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004358 (out->config.rate);
Alexy Josephaa54c872014-12-03 02:46:47 -08004359 }
4360
yidongh0515e042017-07-06 15:00:34 +08004361 if (AUDIO_DEVICE_OUT_ALL_A2DP & out->devices)
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08004362 latency += audio_extn_a2dp_get_encoder_latency();
4363
Anish Kumar50ebcbf2014-12-09 04:01:39 +05304364 ALOGV("%s: Latency %d", __func__, latency);
Alexy Josephaa54c872014-12-03 02:46:47 -08004365 return latency;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004366}
4367
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05304368static float AmpToDb(float amplification)
4369{
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05304370 float db = DSD_VOLUME_MIN_DB;
4371 if (amplification > 0) {
4372 db = 20 * log10(amplification);
4373 if(db < DSD_VOLUME_MIN_DB)
4374 return DSD_VOLUME_MIN_DB;
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05304375 }
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05304376 return db;
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05304377}
4378
Arun Mirpuriebe78a72018-10-04 18:23:46 -07004379static int out_set_mmap_volume(struct audio_stream_out *stream, float left,
4380 float right)
4381{
4382 struct stream_out *out = (struct stream_out *)stream;
4383 long volume = 0;
4384 char mixer_ctl_name[128] = "";
4385 struct audio_device *adev = out->dev;
4386 struct mixer_ctl *ctl = NULL;
4387 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
4388 PCM_PLAYBACK);
4389
4390 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
4391 "Playback %d Volume", pcm_device_id);
4392 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
4393 if (!ctl) {
4394 ALOGE("%s: Could not get ctl for mixer cmd - %s",
4395 __func__, mixer_ctl_name);
4396 return -EINVAL;
4397 }
4398 if (left != right)
4399 ALOGW("%s: Left and right channel volume mismatch:%f,%f",
4400 __func__, left, right);
4401 volume = (long)(left * (MMAP_PLAYBACK_VOLUME_MAX*1.0));
4402 if (mixer_ctl_set_value(ctl, 0, volume) < 0){
4403 ALOGE("%s:ctl for mixer cmd - %s, volume %ld returned error",
4404 __func__, mixer_ctl_name, volume);
4405 return -EINVAL;
4406 }
4407 return 0;
4408}
4409
4410
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304411static int out_set_compr_volume(struct audio_stream_out *stream, float left,
4412 float right)
4413{
4414 struct stream_out *out = (struct stream_out *)stream;
Manish Dewangan338c50a2017-09-12 15:22:03 +05304415 long volume[2];
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304416 char mixer_ctl_name[128];
4417 struct audio_device *adev = out->dev;
4418 struct mixer_ctl *ctl;
4419 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
4420 PCM_PLAYBACK);
4421
4422 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
4423 "Compress Playback %d Volume", pcm_device_id);
4424 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
4425 if (!ctl) {
4426 ALOGE("%s: Could not get ctl for mixer cmd - %s",
4427 __func__, mixer_ctl_name);
4428 return -EINVAL;
4429 }
4430 ALOGE("%s:ctl for mixer cmd - %s, left %f, right %f",
4431 __func__, mixer_ctl_name, left, right);
4432 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
4433 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
4434 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
4435
4436 return 0;
4437}
4438
Zhou Song2b8f28f2017-09-11 10:51:38 +08004439static int out_set_voip_volume(struct audio_stream_out *stream, float left,
4440 float right)
4441{
4442 struct stream_out *out = (struct stream_out *)stream;
4443 char mixer_ctl_name[] = "App Type Gain";
4444 struct audio_device *adev = out->dev;
4445 struct mixer_ctl *ctl;
Manish Dewangan338c50a2017-09-12 15:22:03 +05304446 long set_values[4];
Zhou Song2b8f28f2017-09-11 10:51:38 +08004447
4448 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
4449 if (!ctl) {
4450 ALOGE("%s: Could not get ctl for mixer cmd - %s",
4451 __func__, mixer_ctl_name);
4452 return -EINVAL;
4453 }
4454
4455 set_values[0] = 0; //0: Rx Session 1:Tx Session
4456 set_values[1] = out->app_type_cfg.app_type;
Manish Dewangan338c50a2017-09-12 15:22:03 +05304457 set_values[2] = (long)(left * VOIP_PLAYBACK_VOLUME_MAX);
4458 set_values[3] = (long)(right * VOIP_PLAYBACK_VOLUME_MAX);
Zhou Song2b8f28f2017-09-11 10:51:38 +08004459
4460 mixer_ctl_set_array(ctl, set_values, ARRAY_SIZE(set_values));
4461 return 0;
4462}
4463
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004464static int out_set_volume(struct audio_stream_out *stream, float left,
4465 float right)
4466{
Eric Laurenta9024de2013-04-04 09:19:12 -07004467 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004468 int volume[2];
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304469 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004470
Arun Mirpuriebe78a72018-10-04 18:23:46 -07004471 ALOGD("%s: called with left_vol=%f, right_vol=%f", __func__, left, right);
4472
Eric Laurenta9024de2013-04-04 09:19:12 -07004473 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
4474 /* only take left channel into account: the API is for stereo anyway */
4475 out->muted = (left == 0.0f);
4476 return 0;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07004477 } else if (is_offload_usecase(out->usecase)) {
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05304478 if (audio_extn_passthru_is_passthrough_stream(out)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004479 /*
4480 * Set mute or umute on HDMI passthrough stream.
4481 * Only take left channel into account.
4482 * Mute is 0 and unmute 1
4483 */
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05304484 audio_extn_passthru_set_volume(out, (left == 0.0f));
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05304485 } else if (out->format == AUDIO_FORMAT_DSD){
4486 char mixer_ctl_name[128] = "DSD Volume";
4487 struct audio_device *adev = out->dev;
4488 struct mixer_ctl *ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
4489
4490 if (!ctl) {
4491 ALOGE("%s: Could not get ctl for mixer cmd - %s",
4492 __func__, mixer_ctl_name);
4493 return -EINVAL;
4494 }
Manish Dewangan338c50a2017-09-12 15:22:03 +05304495 volume[0] = (long)(AmpToDb(left));
4496 volume[1] = (long)(AmpToDb(right));
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05304497 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
4498 return 0;
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004499 } else {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304500 pthread_mutex_lock(&out->compr_mute_lock);
Arun Mirpuriebe78a72018-10-04 18:23:46 -07004501 ALOGV("%s: compress mute %d", __func__, out->a2dp_compress_mute);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304502 if (!out->a2dp_compress_mute)
4503 ret = out_set_compr_volume(stream, left, right);
4504 out->volume_l = left;
4505 out->volume_r = right;
4506 pthread_mutex_unlock(&out->compr_mute_lock);
4507 return ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004508 }
Vikram Panduranga93f080e2017-06-07 18:16:14 -07004509 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP) {
Zhou Song2b8f28f2017-09-11 10:51:38 +08004510 if (!out->standby)
4511 ret = out_set_voip_volume(stream, left, right);
4512 out->volume_l = left;
4513 out->volume_r = right;
4514 return ret;
Arun Mirpuriebe78a72018-10-04 18:23:46 -07004515 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
4516 ALOGV("%s: MMAP set volume called", __func__);
4517 if (!out->standby)
4518 ret = out_set_mmap_volume(stream, left, right);
4519 out->volume_l = left;
4520 out->volume_r = right;
4521 return ret;
Eric Laurenta9024de2013-04-04 09:19:12 -07004522 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004523
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004524 return -ENOSYS;
4525}
4526
Zhou Songc9672822017-08-16 16:01:39 +08004527static void update_frames_written(struct stream_out *out, size_t bytes)
4528{
4529 size_t bpf = 0;
4530
4531 if (is_offload_usecase(out->usecase) && !out->non_blocking &&
4532 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD))
4533 bpf = 1;
4534 else if (!is_offload_usecase(out->usecase))
4535 bpf = audio_bytes_per_sample(out->format) *
4536 audio_channel_count_from_out_mask(out->channel_mask);
Zhou Song48453a02018-01-10 17:50:59 +08004537
4538 pthread_mutex_lock(&out->position_query_lock);
4539 if (bpf != 0) {
Zhou Songc9672822017-08-16 16:01:39 +08004540 out->written += bytes / bpf;
Zhou Song48453a02018-01-10 17:50:59 +08004541 clock_gettime(CLOCK_MONOTONIC, &out->writeAt);
4542 }
4543 pthread_mutex_unlock(&out->position_query_lock);
Zhou Songc9672822017-08-16 16:01:39 +08004544}
4545
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004546static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
4547 size_t bytes)
4548{
4549 struct stream_out *out = (struct stream_out *)stream;
4550 struct audio_device *adev = out->dev;
Eric Laurent6e895242013-09-05 16:10:57 -07004551 ssize_t ret = 0;
Satish Babu Patakokila715b1422017-08-22 14:33:21 +05304552 int channels = 0;
Arun Mirpuri7da752a2018-09-11 18:01:15 -07004553 const size_t frame_size = audio_stream_out_frame_size(stream);
4554 const size_t frames = (frame_size != 0) ? bytes / frame_size : bytes;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004555
Haynes Mathew George380745d2017-10-04 15:27:45 -07004556 ATRACE_BEGIN("out_write");
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07004557 lock_output_stream(out);
Naresh Tanniru4c630392014-05-12 01:05:52 +05304558
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304559 if (CARD_STATUS_OFFLINE == out->card_status) {
Zhou Song0b2e5dc2015-03-16 14:41:38 +08004560
Dhananjay Kumarac341582017-02-23 23:42:25 +05304561 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Ashish Jainbbce4322016-02-16 13:25:27 +05304562 /*during SSR for compress usecase we should return error to flinger*/
Naresh Tanniru80659832014-06-04 18:17:56 +05304563 ALOGD(" copl %s: sound card is not active/SSR state", __func__);
4564 pthread_mutex_unlock(&out->lock);
Haynes Mathew George380745d2017-10-04 15:27:45 -07004565 ATRACE_END();
Naresh Tanniru80659832014-06-04 18:17:56 +05304566 return -ENETRESET;
Ashish Jainbbce4322016-02-16 13:25:27 +05304567 } else {
Ashish Jainbbce4322016-02-16 13:25:27 +05304568 ALOGD(" %s: sound card is not active/SSR state", __func__);
4569 ret= -EIO;
4570 goto exit;
Naresh Tanniru4c630392014-05-12 01:05:52 +05304571 }
4572 }
4573
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05304574 if (audio_extn_passthru_should_drop_data(out)) {
Ashish Jaind84fd6a2016-07-27 12:33:25 +05304575 ALOGV(" %s : Drop data as compress passthrough session is going on", __func__);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05304576 ret = -EIO;
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05304577 goto exit;
4578 }
4579
Haynes Mathew George16081042017-05-31 17:16:49 -07004580 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
4581 ret = -EINVAL;
4582 goto exit;
4583 }
4584
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05304585 if ((out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) &&
4586 !out->is_iec61937_info_available) {
4587
4588 if (!audio_extn_passthru_is_passthrough_stream(out)) {
4589 out->is_iec61937_info_available = true;
4590 } else if (audio_extn_passthru_is_enabled()) {
4591 audio_extn_passthru_update_stream_configuration(adev, out, buffer, bytes);
Manish Dewangan37864bc2017-06-09 12:28:37 +05304592 out->is_iec61937_info_available = true;
Manish Dewangan671a4202017-08-18 17:30:46 +05304593
4594 if((out->format == AUDIO_FORMAT_DTS) ||
4595 (out->format == AUDIO_FORMAT_DTS_HD)) {
4596 ret = audio_extn_passthru_update_dts_stream_configuration(out,
4597 buffer, bytes);
4598 if (ret) {
4599 if (ret != -ENOSYS) {
4600 out->is_iec61937_info_available = false;
4601 ALOGD("iec61937 transmission info not yet updated retry");
4602 }
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05304603 } else if (!out->standby) {
Manish Dewangan671a4202017-08-18 17:30:46 +05304604 /* if stream has started and after that there is
4605 * stream config change (iec transmission config)
4606 * then trigger select_device to update backend configuration.
4607 */
4608 out->stream_config_changed = true;
4609 pthread_mutex_lock(&adev->lock);
4610 select_devices(adev, out->usecase);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05304611 if (!audio_extn_passthru_is_supported_backend_edid_cfg(adev, out)) {
4612 ret = -EINVAL;
4613 goto exit;
4614 }
Manish Dewangan671a4202017-08-18 17:30:46 +05304615 pthread_mutex_unlock(&adev->lock);
4616 out->stream_config_changed = false;
4617 out->is_iec61937_info_available = true;
4618 }
4619 }
Satish Babu Patakokila715b1422017-08-22 14:33:21 +05304620
Garmond Leung317cbf12017-09-13 16:20:50 -07004621 if ((channels < (int)audio_channel_count_from_out_mask(out->channel_mask)) &&
Satish Babu Patakokila715b1422017-08-22 14:33:21 +05304622 (out->compr_config.codec->compr_passthr == PASSTHROUGH) &&
4623 (out->is_iec61937_info_available == true)) {
4624 ALOGE("%s: ERROR: Unsupported channel config in passthrough mode", __func__);
4625 ret = -EINVAL;
4626 goto exit;
4627 }
Manish Dewangan37864bc2017-06-09 12:28:37 +05304628 }
4629 }
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304630
4631 if ((out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) &&
4632 (audio_extn_a2dp_is_suspended())) {
4633 if (!(out->devices & AUDIO_DEVICE_OUT_SPEAKER)) {
4634 if (!(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304635 ret = -EIO;
4636 goto exit;
4637 }
4638 }
4639 }
4640
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004641 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07004642 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08004643 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08004644 if (out->usecase == USECASE_COMPRESS_VOIP_CALL)
4645 ret = voice_extn_compress_voip_start_output_stream(out);
4646 else
4647 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08004648 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004649 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004650 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07004651 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004652 goto exit;
4653 }
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304654 out->started = 1;
vivek mehtab72d08d2016-04-29 03:16:47 -07004655 if (last_known_cal_step != -1) {
4656 ALOGD("%s: retry previous failed cal level set", __func__);
4657 audio_hw_send_gain_dep_calibration(last_known_cal_step);
Preetam Singh Ranawatf4ae0222017-05-31 17:07:28 +05304658 last_known_cal_step = -1;
vivek mehtab72d08d2016-04-29 03:16:47 -07004659 }
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05304660
4661 if ((out->is_iec61937_info_available == true) &&
4662 (audio_extn_passthru_is_passthrough_stream(out))&&
4663 (!audio_extn_passthru_is_supported_backend_edid_cfg(adev, out))) {
4664 ret = -EINVAL;
4665 goto exit;
4666 }
Surendar Karka5a18a7a2018-04-26 11:28:38 +05304667 if (out->set_dual_mono)
4668 audio_extn_send_dual_mono_mixing_coefficients(out);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004669 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004670
Ashish Jain81eb2a82015-05-13 10:52:34 +05304671 if (adev->is_channel_status_set == false && (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)){
Alexy Josephb1379942016-01-29 15:49:38 -08004672 audio_utils_set_hdmi_channel_status(out, (void *)buffer, bytes);
Ashish Jain81eb2a82015-05-13 10:52:34 +05304673 adev->is_channel_status_set = true;
4674 }
4675
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07004676 if (is_offload_usecase(out->usecase)) {
Alexy Joseph01e54e62015-03-03 19:01:03 -08004677 ALOGVV("copl(%p): writing buffer (%zu bytes) to compress device", out, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004678 if (out->send_new_metadata) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07004679 ALOGD("copl(%p):send new gapless metadata", out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004680 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
4681 out->send_new_metadata = 0;
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05304682 if (out->send_next_track_params && out->is_compr_metadata_avail) {
4683 ALOGD("copl(%p):send next track params in gapless", out);
4684 compress_set_next_track_param(out->compr, &(out->compr_config.codec->options));
4685 out->send_next_track_params = false;
4686 out->is_compr_metadata_avail = false;
4687 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004688 }
Dhananjay Kumarac341582017-02-23 23:42:25 +05304689 if (!(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
Ashish Jain83a6cc22016-06-28 14:34:17 +05304690 (out->convert_buffer) != NULL) {
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004691
Ashish Jain83a6cc22016-06-28 14:34:17 +05304692 if ((bytes > out->hal_fragment_size)) {
Ashish Jainf1eaa582016-05-23 20:54:24 +05304693 ALOGW("Error written bytes %zu > %d (fragment_size)",
Ashish Jain83a6cc22016-06-28 14:34:17 +05304694 bytes, out->hal_fragment_size);
Ashish Jainf1eaa582016-05-23 20:54:24 +05304695 pthread_mutex_unlock(&out->lock);
Haynes Mathew George380745d2017-10-04 15:27:45 -07004696 ATRACE_END();
Ashish Jainf1eaa582016-05-23 20:54:24 +05304697 return -EINVAL;
4698 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05304699 audio_format_t dst_format = out->hal_op_format;
4700 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05304701
4702 uint32_t frames = bytes / format_to_bitwidth_table[src_format];
4703 uint32_t bytes_to_write = frames * format_to_bitwidth_table[dst_format];
4704
Ashish Jain83a6cc22016-06-28 14:34:17 +05304705 memcpy_by_audio_format(out->convert_buffer,
Ashish Jainf1eaa582016-05-23 20:54:24 +05304706 dst_format,
4707 buffer,
4708 src_format,
4709 frames);
4710
Ashish Jain83a6cc22016-06-28 14:34:17 +05304711 ret = compress_write(out->compr, out->convert_buffer,
Ashish Jainf1eaa582016-05-23 20:54:24 +05304712 bytes_to_write);
4713
4714 /*Convert written bytes in audio flinger format*/
4715 if (ret > 0)
4716 ret = ((ret * format_to_bitwidth_table[out->format]) /
4717 format_to_bitwidth_table[dst_format]);
4718 }
4719 } else
4720 ret = compress_write(out->compr, buffer, bytes);
4721
Zhou Songc9672822017-08-16 16:01:39 +08004722 if ((ret < 0 || ret == (ssize_t)bytes) && !out->non_blocking)
4723 update_frames_written(out, bytes);
4724
Dhanalakshmi Siddani37ca1d62014-08-20 12:28:34 +05304725 if (ret < 0)
4726 ret = -errno;
Weiyin Jiangcc60dbb2018-08-21 13:12:03 +08004727 ALOGVV("%s: writing buffer (%zu bytes) to compress device returned %d", __func__, bytes, (int)ret);
Ashish Jainb26edfb2016-08-25 00:10:11 +05304728 /*msg to cb thread only if non blocking write is enabled*/
4729 if (ret >= 0 && ret < (ssize_t)bytes && out->non_blocking) {
Sidipotu Ashok55820562014-02-10 16:16:38 +05304730 ALOGD("No space available in compress driver, post msg to cb thread");
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004731 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
Naresh Tanniru80659832014-06-04 18:17:56 +05304732 } else if (-ENETRESET == ret) {
4733 ALOGE("copl %s: received sound card offline state on compress write", __func__);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304734 out->card_status = CARD_STATUS_OFFLINE;
Naresh Tanniru80659832014-06-04 18:17:56 +05304735 pthread_mutex_unlock(&out->lock);
Dhananjay Kumar1248dd82017-07-28 21:22:16 +05304736 out_on_error(&out->stream.common);
Haynes Mathew George380745d2017-10-04 15:27:45 -07004737 ATRACE_END();
Naresh Tanniru80659832014-06-04 18:17:56 +05304738 return ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004739 }
Ashish Jain5106d362016-05-11 19:23:33 +05304740
Dhanalakshmi Siddania6b76c72016-09-09 18:10:31 +05304741 /* Call compr start only when non-zero bytes of data is there to be rendered */
4742 if (!out->playback_started && ret > 0) {
4743 int status = compress_start(out->compr);
4744 if (status < 0) {
4745 ret = status;
4746 ALOGE("%s: compr start failed with err %d", __func__, errno);
4747 goto exit;
4748 }
Alexy Joseph7de344d2015-03-30 10:40:03 -07004749 audio_extn_dts_eagle_fade(adev, true, out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004750 out->playback_started = 1;
4751 out->offload_state = OFFLOAD_STATE_PLAYING;
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08004752
4753 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
4754 popcount(out->channel_mask),
4755 out->playback_started);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004756 }
4757 pthread_mutex_unlock(&out->lock);
Haynes Mathew George380745d2017-10-04 15:27:45 -07004758 ATRACE_END();
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004759 return ret;
4760 } else {
4761 if (out->pcm) {
Arun Mirpuri7da752a2018-09-11 18:01:15 -07004762 size_t bytes_to_write = bytes;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004763 if (out->muted)
4764 memset((void *)buffer, 0, bytes);
Arun Mirpuri7da752a2018-09-11 18:01:15 -07004765 ALOGV("%s: frames=%zu, frame_size=%zu, bytes_to_write=%zu",
4766 __func__, frames, frame_size, bytes_to_write);
4767
Arun Mirpuridbef0c72018-09-12 18:36:10 -07004768 if (out->usecase == USECASE_INCALL_MUSIC_UPLINK ||
4769 out->usecase == USECASE_INCALL_MUSIC_UPLINK2) {
Arun Mirpuri7da752a2018-09-11 18:01:15 -07004770 size_t channel_count = audio_channel_count_from_out_mask(out->channel_mask);
4771 int16_t *src = (int16_t *)buffer;
4772 int16_t *dst = (int16_t *)buffer;
4773
4774 LOG_ALWAYS_FATAL_IF(out->config.channels != 1 || channel_count != 2 ||
4775 out->format != AUDIO_FORMAT_PCM_16_BIT,
4776 "out_write called for incall music use case with wrong properties");
4777
4778 /*
4779 * FIXME: this can be removed once audio flinger mixer supports
4780 * mono output
4781 */
4782
4783 /*
4784 * Code below goes over each frame in the buffer and adds both
4785 * L and R samples and then divides by 2 to convert to mono
4786 */
4787 for (size_t i = 0; i < frames ; i++, dst++, src += 2) {
4788 *dst = (int16_t)(((int32_t)src[0] + (int32_t)src[1]) >> 1);
4789 }
4790 bytes_to_write /= 2;
4791 }
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07004792
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05304793 ALOGVV("%s: writing buffer (%zu bytes) to pcm device", __func__, bytes);
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07004794
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004795 long ns = 0;
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07004796
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004797 if (out->config.rate)
4798 ns = pcm_bytes_to_frames(out->pcm, bytes)*1000000000LL/
4799 out->config.rate;
4800
Arun Mirpuri7da752a2018-09-11 18:01:15 -07004801 request_out_focus(out, ns);
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004802 bool use_mmap = is_mmap_usecase(out->usecase) || out->realtime;
4803
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004804 if (use_mmap)
Arun Mirpuri7da752a2018-09-11 18:01:15 -07004805 ret = pcm_mmap_write(out->pcm, (void *)buffer, bytes_to_write);
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004806 else if (out->hal_op_format != out->hal_ip_format &&
Ashish Jain83a6cc22016-06-28 14:34:17 +05304807 out->convert_buffer != NULL) {
4808
4809 memcpy_by_audio_format(out->convert_buffer,
4810 out->hal_op_format,
4811 buffer,
4812 out->hal_ip_format,
4813 out->config.period_size * out->config.channels);
4814
4815 ret = pcm_write(out->pcm, out->convert_buffer,
4816 (out->config.period_size *
4817 out->config.channels *
4818 format_to_bitwidth_table[out->hal_op_format]));
4819 } else {
Aditya Bavanarid4db8ee2017-05-29 21:08:03 +05304820 /*
4821 * To avoid underrun in DSP when the application is not pumping
4822 * data at required rate, check for the no. of bytes and ignore
4823 * pcm_write if it is less than actual buffer size.
4824 * It is a work around to a change in compress VOIP driver.
4825 */
4826 if ((out->flags & AUDIO_OUTPUT_FLAG_VOIP_RX) &&
4827 bytes < (out->config.period_size * out->config.channels *
4828 audio_bytes_per_sample(out->format))) {
4829 size_t voip_buf_size =
4830 out->config.period_size * out->config.channels *
4831 audio_bytes_per_sample(out->format);
4832 ALOGE("%s:VOIP underrun: bytes received %zu, required:%zu\n",
4833 __func__, bytes, voip_buf_size);
4834 usleep(((uint64_t)voip_buf_size - bytes) *
4835 1000000 / audio_stream_out_frame_size(stream) /
4836 out_get_sample_rate(&out->stream.common));
4837 ret = 0;
4838 } else
Arun Mirpuri7da752a2018-09-11 18:01:15 -07004839 ret = pcm_write(out->pcm, (void *)buffer, bytes_to_write);
Ashish Jain83a6cc22016-06-28 14:34:17 +05304840 }
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07004841
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004842 release_out_focus(out);
4843
Dhanalakshmi Siddani37ca1d62014-08-20 12:28:34 +05304844 if (ret < 0)
4845 ret = -errno;
Zhou Songc9672822017-08-16 16:01:39 +08004846 else if (ret > 0)
Ashish Jain83a6cc22016-06-28 14:34:17 +05304847 ret = -EINVAL;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004848 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004849 }
4850
4851exit:
Zhou Songc9672822017-08-16 16:01:39 +08004852 update_frames_written(out, bytes);
Naresh Tanniru4c630392014-05-12 01:05:52 +05304853 if (-ENETRESET == ret) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304854 out->card_status = CARD_STATUS_OFFLINE;
Naresh Tanniru4c630392014-05-12 01:05:52 +05304855 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004856 pthread_mutex_unlock(&out->lock);
4857
4858 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07004859 if (out->pcm)
Alexy Josephb1379942016-01-29 15:49:38 -08004860 ALOGE("%s: error %d, %s", __func__, (int)ret, pcm_get_error(out->pcm));
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05304861 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05304862 pthread_mutex_lock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05304863 voice_extn_compress_voip_close_output_stream(&out->stream.common);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304864 out->started = 0;
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05304865 pthread_mutex_unlock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05304866 out->standby = true;
4867 }
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304868 out_on_error(&out->stream.common);
Dhanalakshmi Siddania6b76c72016-09-09 18:10:31 +05304869 if (!(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD))
4870 usleep((uint64_t)bytes * 1000000 / audio_stream_out_frame_size(stream) /
4871 out_get_sample_rate(&out->stream.common));
Vidyakumar Athotaa9d3a5f2017-08-09 12:13:05 -07004872
4873 if (audio_extn_passthru_is_passthrough_stream(out)) {
Rajshekar Eashwarappa88834522018-04-02 17:20:15 +05304874 //ALOGE("%s: write error, ret = %zd", __func__, ret);
Haynes Mathew George380745d2017-10-04 15:27:45 -07004875 ATRACE_END();
Vidyakumar Athotaa9d3a5f2017-08-09 12:13:05 -07004876 return ret;
4877 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004878 }
Haynes Mathew George380745d2017-10-04 15:27:45 -07004879 ATRACE_END();
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004880 return bytes;
4881}
4882
4883static int out_get_render_position(const struct audio_stream_out *stream,
4884 uint32_t *dsp_frames)
4885{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004886 struct stream_out *out = (struct stream_out *)stream;
Zhou Song32a556e2015-05-05 10:46:56 +08004887
4888 if (dsp_frames == NULL)
4889 return -EINVAL;
4890
4891 *dsp_frames = 0;
4892 if (is_offload_usecase(out->usecase)) {
Mingming Yin9e348b52014-11-19 16:18:55 -08004893 ssize_t ret = 0;
Ashish Jain5106d362016-05-11 19:23:33 +05304894
4895 /* Below piece of code is not guarded against any lock beacuse audioFliner serializes
4896 * this operation and adev_close_output_stream(where out gets reset).
4897 */
Dhananjay Kumarac341582017-02-23 23:42:25 +05304898 if (!out->non_blocking && !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Zhou Song48453a02018-01-10 17:50:59 +08004899 *dsp_frames = get_actual_pcm_frames_rendered(out, NULL);
Ashish Jain5106d362016-05-11 19:23:33 +05304900 ALOGVV("dsp_frames %d sampleRate %d",(int)*dsp_frames,out->sample_rate);
Haynes Mathew Georgeb0f5dc32017-10-06 18:35:12 -07004901 adjust_frames_for_device_delay(out, dsp_frames);
Ashish Jain5106d362016-05-11 19:23:33 +05304902 return 0;
4903 }
4904
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07004905 lock_output_stream(out);
Ashish Jain5106d362016-05-11 19:23:33 +05304906 if (out->compr != NULL && out->non_blocking) {
Naresh Tanniru80659832014-06-04 18:17:56 +05304907 ret = compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004908 &out->sample_rate);
Dhanalakshmi Siddani37ca1d62014-08-20 12:28:34 +05304909 if (ret < 0)
4910 ret = -errno;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004911 ALOGVV("%s rendered frames %d sample_rate %d",
Ashish Jain5106d362016-05-11 19:23:33 +05304912 __func__, *dsp_frames, out->sample_rate);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004913 }
Naresh Tanniru80659832014-06-04 18:17:56 +05304914 if (-ENETRESET == ret) {
4915 ALOGE(" ERROR: sound card not active Unable to get time stamp from compress driver");
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304916 out->card_status = CARD_STATUS_OFFLINE;
4917 ret = -EINVAL;
Naresh Tanniru80659832014-06-04 18:17:56 +05304918 } else if(ret < 0) {
4919 ALOGE(" ERROR: Unable to get time stamp from compress driver");
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304920 ret = -EINVAL;
4921 } else if (out->card_status == CARD_STATUS_OFFLINE) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05304922 /*
4923 * Handle corner case where compress session is closed during SSR
4924 * and timestamp is queried
4925 */
4926 ALOGE(" ERROR: sound card not active, return error");
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304927 ret = -EINVAL;
Naresh Tanniru80659832014-06-04 18:17:56 +05304928 } else {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304929 ret = 0;
Haynes Mathew Georgeb0f5dc32017-10-06 18:35:12 -07004930 adjust_frames_for_device_delay(out, dsp_frames);
Naresh Tanniru80659832014-06-04 18:17:56 +05304931 }
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304932 pthread_mutex_unlock(&out->lock);
4933 return ret;
Zhou Song32a556e2015-05-05 10:46:56 +08004934 } else if (audio_is_linear_pcm(out->format)) {
4935 *dsp_frames = out->written;
Haynes Mathew Georgeb0f5dc32017-10-06 18:35:12 -07004936 adjust_frames_for_device_delay(out, dsp_frames);
Zhou Song32a556e2015-05-05 10:46:56 +08004937 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004938 } else
4939 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004940}
4941
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07004942static int out_add_audio_effect(const struct audio_stream *stream __unused,
4943 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004944{
4945 return 0;
4946}
4947
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07004948static int out_remove_audio_effect(const struct audio_stream *stream __unused,
4949 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004950{
4951 return 0;
4952}
4953
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07004954static int out_get_next_write_timestamp(const struct audio_stream_out *stream __unused,
4955 int64_t *timestamp __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004956{
Satya Krishna Pindiprolib6655542017-07-03 19:38:19 +05304957 return -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004958}
4959
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07004960static int out_get_presentation_position(const struct audio_stream_out *stream,
4961 uint64_t *frames, struct timespec *timestamp)
4962{
4963 struct stream_out *out = (struct stream_out *)stream;
pavance65c2fe2017-10-18 17:52:01 +05304964 int ret = -ENODATA;
Eric Laurent949a0892013-09-20 09:20:13 -07004965 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07004966
Ashish Jain5106d362016-05-11 19:23:33 +05304967 /* below piece of code is not guarded against any lock because audioFliner serializes
4968 * this operation and adev_close_output_stream( where out gets reset).
4969 */
4970 if (is_offload_usecase(out->usecase) && !out->non_blocking &&
Dhananjay Kumarac341582017-02-23 23:42:25 +05304971 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Zhou Song48453a02018-01-10 17:50:59 +08004972 *frames = get_actual_pcm_frames_rendered(out, timestamp);
Ashish Jain5106d362016-05-11 19:23:33 +05304973 ALOGVV("frames %lld playedat %lld",(long long int)*frames,
4974 timestamp->tv_sec * 1000000LL + timestamp->tv_nsec / 1000);
4975 return 0;
4976 }
4977
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07004978 lock_output_stream(out);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07004979
Ashish Jain5106d362016-05-11 19:23:33 +05304980 if (is_offload_usecase(out->usecase) && out->compr != NULL && out->non_blocking) {
4981 ret = compress_get_tstamp(out->compr, &dsp_frames,
4982 &out->sample_rate);
yidongh0515e042017-07-06 15:00:34 +08004983 // Adjustment accounts for A2dp encoder latency with offload usecases
4984 // Note: Encoder latency is returned in ms.
4985 if (AUDIO_DEVICE_OUT_ALL_A2DP & out->devices) {
4986 unsigned long offset =
4987 (audio_extn_a2dp_get_encoder_latency() * out->sample_rate / 1000);
4988 dsp_frames = (dsp_frames > offset) ? (dsp_frames - offset) : 0;
4989 }
Ashish Jain5106d362016-05-11 19:23:33 +05304990 ALOGVV("%s rendered frames %ld sample_rate %d",
4991 __func__, dsp_frames, out->sample_rate);
4992 *frames = dsp_frames;
4993 if (ret < 0)
4994 ret = -errno;
4995 if (-ENETRESET == ret) {
4996 ALOGE(" ERROR: sound card not active Unable to get time stamp from compress driver");
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304997 out->card_status = CARD_STATUS_OFFLINE;
Ashish Jain5106d362016-05-11 19:23:33 +05304998 ret = -EINVAL;
4999 } else
5000 ret = 0;
5001 /* this is the best we can do */
5002 clock_gettime(CLOCK_MONOTONIC, timestamp);
Eric Laurent949a0892013-09-20 09:20:13 -07005003 } else {
5004 if (out->pcm) {
Weiyin Jiangd4633762018-03-16 12:05:03 +08005005 unsigned int avail;
5006 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
5007 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
5008 int64_t signed_frames = out->written - kernel_buffer_size + avail;
5009 // This adjustment accounts for buffering after app processor.
5010 // It is based on estimated DSP latency per use case, rather than exact.
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07005011 signed_frames -=
Weiyin Jiangd4633762018-03-16 12:05:03 +08005012 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
Aniket Kumar Lataff613152017-07-18 18:19:21 -07005013
Weiyin Jiangd4633762018-03-16 12:05:03 +08005014 // Adjustment accounts for A2dp encoder latency with non offload usecases
5015 // Note: Encoder latency is returned in ms, while platform_render_latency in us.
5016 if (AUDIO_DEVICE_OUT_ALL_A2DP & out->devices) {
5017 signed_frames -=
5018 (audio_extn_a2dp_get_encoder_latency() * out->sample_rate / 1000);
5019 }
5020
5021 // It would be unusual for this value to be negative, but check just in case ...
5022 if (signed_frames >= 0) {
5023 *frames = signed_frames;
5024 ret = 0;
5025 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07005026 }
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305027 } else if (out->card_status == CARD_STATUS_OFFLINE) {
Ashish Jainbbce4322016-02-16 13:25:27 +05305028 *frames = out->written;
5029 clock_gettime(CLOCK_MONOTONIC, timestamp);
5030 ret = 0;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07005031 }
5032 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07005033 pthread_mutex_unlock(&out->lock);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07005034 return ret;
5035}
5036
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005037static int out_set_callback(struct audio_stream_out *stream,
5038 stream_callback_t callback, void *cookie)
5039{
5040 struct stream_out *out = (struct stream_out *)stream;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08005041 int ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005042
5043 ALOGV("%s", __func__);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005044 lock_output_stream(out);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08005045 out->client_callback = callback;
5046 out->client_cookie = cookie;
5047 if (out->adsp_hdlr_stream_handle) {
5048 ret = audio_extn_adsp_hdlr_stream_set_callback(
5049 out->adsp_hdlr_stream_handle,
5050 callback,
5051 cookie);
5052 if (ret)
5053 ALOGW("%s:adsp hdlr callback registration failed %d",
5054 __func__, ret);
5055 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005056 pthread_mutex_unlock(&out->lock);
5057 return 0;
5058}
5059
5060static int out_pause(struct audio_stream_out* stream)
5061{
5062 struct stream_out *out = (struct stream_out *)stream;
5063 int status = -ENOSYS;
5064 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07005065 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07005066 ALOGD("copl(%p):pause compress driver", out);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005067 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005068 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305069 if (out->card_status != CARD_STATUS_OFFLINE)
Naresh Tanniru80659832014-06-04 18:17:56 +05305070 status = compress_pause(out->compr);
5071
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005072 out->offload_state = OFFLOAD_STATE_PAUSED;
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08005073
Mingming Yin21854652016-04-13 11:54:02 -07005074 if (audio_extn_passthru_is_active()) {
5075 ALOGV("offload use case, pause passthru");
5076 audio_extn_passthru_on_pause(out);
5077 }
5078
Dhanalakshmi Siddani79415e72015-03-23 11:54:47 +05305079 audio_extn_dts_eagle_fade(adev, false, out);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08005080 audio_extn_dts_notify_playback_state(out->usecase, 0,
5081 out->sample_rate, popcount(out->channel_mask),
5082 0);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005083 }
5084 pthread_mutex_unlock(&out->lock);
5085 }
5086 return status;
5087}
5088
5089static int out_resume(struct audio_stream_out* stream)
5090{
5091 struct stream_out *out = (struct stream_out *)stream;
5092 int status = -ENOSYS;
5093 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07005094 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07005095 ALOGD("copl(%p):resume compress driver", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005096 status = 0;
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005097 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005098 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305099 if (out->card_status != CARD_STATUS_OFFLINE) {
Naresh Tanniru80659832014-06-04 18:17:56 +05305100 status = compress_resume(out->compr);
Mingming Yin21854652016-04-13 11:54:02 -07005101 }
5102 if (!status) {
5103 out->offload_state = OFFLOAD_STATE_PLAYING;
5104 }
Dhanalakshmi Siddani79415e72015-03-23 11:54:47 +05305105 audio_extn_dts_eagle_fade(adev, true, out);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08005106 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
5107 popcount(out->channel_mask), 1);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005108 }
5109 pthread_mutex_unlock(&out->lock);
5110 }
5111 return status;
5112}
5113
5114static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
5115{
5116 struct stream_out *out = (struct stream_out *)stream;
5117 int status = -ENOSYS;
5118 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07005119 if (is_offload_usecase(out->usecase)) {
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005120 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005121 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
5122 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
5123 else
5124 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
5125 pthread_mutex_unlock(&out->lock);
5126 }
5127 return status;
5128}
5129
5130static int out_flush(struct audio_stream_out* stream)
5131{
5132 struct stream_out *out = (struct stream_out *)stream;
5133 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07005134 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07005135 ALOGD("copl(%p):calling compress flush", out);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005136 lock_output_stream(out);
Haynes Mathew Georgeafe54d82016-09-21 14:39:19 -07005137 if (out->offload_state == OFFLOAD_STATE_PAUSED) {
5138 stop_compressed_output_l(out);
Haynes Mathew Georgeafe54d82016-09-21 14:39:19 -07005139 } else {
5140 ALOGW("%s called in invalid state %d", __func__, out->offload_state);
5141 }
Weiyin Jiang547e4152017-09-14 17:24:18 +08005142 out->written = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005143 pthread_mutex_unlock(&out->lock);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07005144 ALOGD("copl(%p):out of compress flush", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005145 return 0;
5146 }
5147 return -ENOSYS;
5148}
5149
Haynes Mathew George16081042017-05-31 17:16:49 -07005150static int out_stop(const struct audio_stream_out* stream)
5151{
5152 struct stream_out *out = (struct stream_out *)stream;
5153 struct audio_device *adev = out->dev;
5154 int ret = -ENOSYS;
5155
5156 ALOGV("%s", __func__);
5157 pthread_mutex_lock(&adev->lock);
5158 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP && !out->standby &&
5159 out->playback_started && out->pcm != NULL) {
5160 pcm_stop(out->pcm);
5161 ret = stop_output_stream(out);
5162 out->playback_started = false;
5163 }
5164 pthread_mutex_unlock(&adev->lock);
5165 return ret;
5166}
5167
5168static int out_start(const struct audio_stream_out* stream)
5169{
5170 struct stream_out *out = (struct stream_out *)stream;
5171 struct audio_device *adev = out->dev;
5172 int ret = -ENOSYS;
5173
5174 ALOGV("%s", __func__);
5175 pthread_mutex_lock(&adev->lock);
5176 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP && !out->standby &&
5177 !out->playback_started && out->pcm != NULL) {
5178 ret = start_output_stream(out);
5179 if (ret == 0) {
5180 out->playback_started = true;
5181 }
5182 }
5183 pthread_mutex_unlock(&adev->lock);
5184 return ret;
5185}
5186
5187/*
5188 * Modify config->period_count based on min_size_frames
5189 */
5190static void adjust_mmap_period_count(struct pcm_config *config, int32_t min_size_frames)
5191{
5192 int periodCountRequested = (min_size_frames + config->period_size - 1)
5193 / config->period_size;
5194 int periodCount = MMAP_PERIOD_COUNT_MIN;
5195
5196 ALOGV("%s original config.period_size = %d config.period_count = %d",
5197 __func__, config->period_size, config->period_count);
5198
5199 while (periodCount < periodCountRequested && (periodCount * 2) < MMAP_PERIOD_COUNT_MAX) {
5200 periodCount *= 2;
5201 }
5202 config->period_count = periodCount;
5203
5204 ALOGV("%s requested config.period_count = %d", __func__, config->period_count);
5205}
5206
5207static int out_create_mmap_buffer(const struct audio_stream_out *stream,
5208 int32_t min_size_frames,
5209 struct audio_mmap_buffer_info *info)
5210{
5211 struct stream_out *out = (struct stream_out *)stream;
5212 struct audio_device *adev = out->dev;
5213 int ret = 0;
Aalique Grahame1f123102017-10-12 10:38:32 -07005214 unsigned int offset1 = 0;
5215 unsigned int frames1 = 0;
Haynes Mathew George16081042017-05-31 17:16:49 -07005216 const char *step = "";
Haynes Mathew Georgeef514882017-05-01 17:46:23 -07005217 uint32_t mmap_size;
Arun Mirpuriebe78a72018-10-04 18:23:46 -07005218 uint32_t buffer_size;
Haynes Mathew George16081042017-05-31 17:16:49 -07005219
Arun Mirpuriebe78a72018-10-04 18:23:46 -07005220 ALOGD("%s", __func__);
Sharad Sangle90e613f2018-05-04 16:15:38 +05305221 lock_output_stream(out);
Haynes Mathew George16081042017-05-31 17:16:49 -07005222 pthread_mutex_lock(&adev->lock);
5223
Sharad Sangle90e613f2018-05-04 16:15:38 +05305224 if (CARD_STATUS_OFFLINE == out->card_status ||
5225 CARD_STATUS_OFFLINE == adev->card_status) {
5226 ALOGW("out->card_status or adev->card_status offline, try again");
5227 ret = -EIO;
5228 goto exit;
5229 }
Haynes Mathew George16081042017-05-31 17:16:49 -07005230 if (info == NULL || min_size_frames == 0) {
5231 ALOGE("%s: info = %p, min_size_frames = %d", __func__, info, min_size_frames);
5232 ret = -EINVAL;
5233 goto exit;
5234 }
5235 if (out->usecase != USECASE_AUDIO_PLAYBACK_MMAP || !out->standby) {
5236 ALOGE("%s: usecase = %d, standby = %d", __func__, out->usecase, out->standby);
5237 ret = -ENOSYS;
5238 goto exit;
5239 }
5240 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
5241 if (out->pcm_device_id < 0) {
5242 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
5243 __func__, out->pcm_device_id, out->usecase);
5244 ret = -EINVAL;
5245 goto exit;
5246 }
5247
5248 adjust_mmap_period_count(&out->config, min_size_frames);
5249
Arun Mirpuriebe78a72018-10-04 18:23:46 -07005250 ALOGD("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
Haynes Mathew George16081042017-05-31 17:16:49 -07005251 __func__, adev->snd_card, out->pcm_device_id, out->config.channels);
5252 out->pcm = pcm_open(adev->snd_card, out->pcm_device_id,
5253 (PCM_OUT | PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC), &out->config);
Sharad Sangle90e613f2018-05-04 16:15:38 +05305254 if (errno == ENETRESET && !pcm_is_ready(out->pcm)) {
5255 ALOGE("%s: pcm_open failed errno:%d\n", __func__, errno);
5256 out->card_status = CARD_STATUS_OFFLINE;
5257 adev->card_status = CARD_STATUS_OFFLINE;
5258 ret = -EIO;
5259 goto exit;
5260 }
5261
Haynes Mathew George16081042017-05-31 17:16:49 -07005262 if (out->pcm == NULL || !pcm_is_ready(out->pcm)) {
5263 step = "open";
5264 ret = -ENODEV;
5265 goto exit;
5266 }
5267 ret = pcm_mmap_begin(out->pcm, &info->shared_memory_address, &offset1, &frames1);
5268 if (ret < 0) {
5269 step = "begin";
5270 goto exit;
5271 }
5272 info->buffer_size_frames = pcm_get_buffer_size(out->pcm);
Arun Mirpuriebe78a72018-10-04 18:23:46 -07005273 buffer_size = pcm_frames_to_bytes(out->pcm, info->buffer_size_frames);
Haynes Mathew George16081042017-05-31 17:16:49 -07005274 info->burst_size_frames = out->config.period_size;
Haynes Mathew Georgeef514882017-05-01 17:46:23 -07005275 ret = platform_get_mmap_data_fd(adev->platform,
5276 out->pcm_device_id, 0 /*playback*/,
5277 &info->shared_memory_fd,
5278 &mmap_size);
5279 if (ret < 0) {
Arun Mirpuriebe78a72018-10-04 18:23:46 -07005280 // Fall back to non exclusive mode
5281 info->shared_memory_fd = pcm_get_poll_fd(out->pcm);
5282 } else {
5283 if (mmap_size < buffer_size) {
5284 step = "mmap";
5285 goto exit;
5286 }
5287 // FIXME: indicate exclusive mode support by returning a negative buffer size
5288 info->buffer_size_frames *= -1;
Haynes Mathew Georgeef514882017-05-01 17:46:23 -07005289 }
Arun Mirpuriebe78a72018-10-04 18:23:46 -07005290 memset(info->shared_memory_address, 0, buffer_size);
Haynes Mathew George16081042017-05-31 17:16:49 -07005291
5292 ret = pcm_mmap_commit(out->pcm, 0, MMAP_PERIOD_SIZE);
5293 if (ret < 0) {
5294 step = "commit";
5295 goto exit;
5296 }
5297
5298 out->standby = false;
5299 ret = 0;
5300
Arun Mirpuriebe78a72018-10-04 18:23:46 -07005301 ALOGD("%s: got mmap buffer address %p info->buffer_size_frames %d",
Haynes Mathew George16081042017-05-31 17:16:49 -07005302 __func__, info->shared_memory_address, info->buffer_size_frames);
5303
5304exit:
5305 if (ret != 0) {
5306 if (out->pcm == NULL) {
5307 ALOGE("%s: %s - %d", __func__, step, ret);
5308 } else {
5309 ALOGE("%s: %s %s", __func__, step, pcm_get_error(out->pcm));
5310 pcm_close(out->pcm);
5311 out->pcm = NULL;
5312 }
5313 }
5314 pthread_mutex_unlock(&adev->lock);
Sharad Sangle90e613f2018-05-04 16:15:38 +05305315 pthread_mutex_unlock(&out->lock);
Haynes Mathew George16081042017-05-31 17:16:49 -07005316 return ret;
5317}
5318
5319static int out_get_mmap_position(const struct audio_stream_out *stream,
5320 struct audio_mmap_position *position)
5321{
5322 struct stream_out *out = (struct stream_out *)stream;
5323 ALOGVV("%s", __func__);
5324 if (position == NULL) {
5325 return -EINVAL;
5326 }
5327 if (out->usecase != USECASE_AUDIO_PLAYBACK_MMAP) {
Haynes Mathew George4ab3ba92017-12-11 14:49:43 -08005328 ALOGE("%s: called on %s", __func__, use_case_table[out->usecase]);
Haynes Mathew George16081042017-05-31 17:16:49 -07005329 return -ENOSYS;
5330 }
5331 if (out->pcm == NULL) {
5332 return -ENOSYS;
5333 }
5334
5335 struct timespec ts = { 0, 0 };
5336 int ret = pcm_mmap_get_hw_ptr(out->pcm, (unsigned int *)&position->position_frames, &ts);
5337 if (ret < 0) {
5338 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
5339 return ret;
5340 }
Aalique Grahame90717e22018-07-18 15:54:18 -07005341 position->time_nanoseconds = ts.tv_sec*1000000000LL + ts.tv_nsec;
Haynes Mathew George16081042017-05-31 17:16:49 -07005342 return 0;
5343}
5344
5345
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005346/** audio_stream_in implementation **/
5347static uint32_t in_get_sample_rate(const struct audio_stream *stream)
5348{
5349 struct stream_in *in = (struct stream_in *)stream;
5350
5351 return in->config.rate;
5352}
5353
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07005354static int in_set_sample_rate(struct audio_stream *stream __unused,
5355 uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005356{
5357 return -ENOSYS;
5358}
5359
5360static size_t in_get_buffer_size(const struct audio_stream *stream)
5361{
5362 struct stream_in *in = (struct stream_in *)stream;
5363
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08005364 if(in->usecase == USECASE_COMPRESS_VOIP_CALL)
5365 return voice_extn_compress_voip_in_get_buffer_size(in);
Vikram Panduranga93f080e2017-06-07 18:16:14 -07005366 else if(in->usecase == USECASE_AUDIO_RECORD_VOIP)
5367 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 -07005368 else if(audio_extn_compr_cap_usecase_supported(in->usecase))
5369 return audio_extn_compr_cap_get_buffer_size(in->config.format);
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05305370 else if(audio_extn_cin_attached_usecase(in->usecase))
5371 return audio_extn_cin_get_buffer_size(in);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08005372
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005373 return in->config.period_size * in->af_period_multiplier *
5374 audio_stream_in_frame_size((const struct audio_stream_in *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005375}
5376
5377static uint32_t in_get_channels(const struct audio_stream *stream)
5378{
5379 struct stream_in *in = (struct stream_in *)stream;
5380
5381 return in->channel_mask;
5382}
5383
5384static audio_format_t in_get_format(const struct audio_stream *stream)
5385{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08005386 struct stream_in *in = (struct stream_in *)stream;
5387
5388 return in->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005389}
5390
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07005391static int in_set_format(struct audio_stream *stream __unused,
5392 audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005393{
5394 return -ENOSYS;
5395}
5396
5397static int in_standby(struct audio_stream *stream)
5398{
5399 struct stream_in *in = (struct stream_in *)stream;
5400 struct audio_device *adev = in->dev;
5401 int status = 0;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05305402 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
5403 stream, in->usecase, use_case_table[in->usecase]);
Haynes Mathew George16081042017-05-31 17:16:49 -07005404 bool do_stop = true;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05305405
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005406 lock_input_stream(in);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07005407 if (!in->standby && in->is_st_session) {
5408 ALOGD("%s: sound trigger pcm stop lab", __func__);
5409 audio_extn_sound_trigger_stop_lab(in);
5410 in->standby = 1;
5411 }
5412
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005413 if (!in->standby) {
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07005414 if (adev->adm_deregister_stream)
5415 adev->adm_deregister_stream(adev->adm_data, in->capture_handle);
5416
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08005417 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005418 in->standby = true;
Zhou Songa8895042016-07-05 17:54:22 +08005419 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
kunleizbecba2d2017-09-07 13:37:16 +08005420 do_stop = false;
Zhou Songa8895042016-07-05 17:54:22 +08005421 voice_extn_compress_voip_close_input_stream(stream);
5422 ALOGD("VOIP input entered standby");
Haynes Mathew George16081042017-05-31 17:16:49 -07005423 } else if (in->usecase == USECASE_AUDIO_RECORD_MMAP) {
5424 do_stop = in->capture_started;
5425 in->capture_started = false;
Zhou Songa8895042016-07-05 17:54:22 +08005426 } else {
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05305427 if (audio_extn_cin_attached_usecase(in->usecase))
5428 audio_extn_cin_stop_input_stream(in);
kunleizbecba2d2017-09-07 13:37:16 +08005429 }
5430
Arun Mirpuriebe78a72018-10-04 18:23:46 -07005431 if (in->pcm) {
Haynes Mathew George380745d2017-10-04 15:27:45 -07005432 ATRACE_BEGIN("pcm_in_close");
Zhou Songa8895042016-07-05 17:54:22 +08005433 pcm_close(in->pcm);
Haynes Mathew George380745d2017-10-04 15:27:45 -07005434 ATRACE_END();
Zhou Songa8895042016-07-05 17:54:22 +08005435 in->pcm = NULL;
Eric Laurent150dbfe2013-02-27 14:31:02 -08005436 }
Arun Mirpuriebe78a72018-10-04 18:23:46 -07005437
5438 if (do_stop)
5439 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08005440 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005441 }
5442 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07005443 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005444 return status;
5445}
5446
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07005447static int in_dump(const struct audio_stream *stream __unused,
5448 int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005449{
5450 return 0;
5451}
5452
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305453static void in_snd_mon_cb(void * stream, struct str_parms * parms)
5454{
5455 if (!stream || !parms)
5456 return;
5457
5458 struct stream_in *in = (struct stream_in *)stream;
5459 struct audio_device *adev = in->dev;
5460
5461 card_status_t status;
5462 int card;
5463 if (parse_snd_card_status(parms, &card, &status) < 0)
5464 return;
5465
5466 pthread_mutex_lock(&adev->lock);
5467 bool valid_cb = (card == adev->snd_card);
5468 pthread_mutex_unlock(&adev->lock);
5469
5470 if (!valid_cb)
5471 return;
5472
5473 lock_input_stream(in);
5474 if (in->card_status != status)
5475 in->card_status = status;
5476 pthread_mutex_unlock(&in->lock);
5477
5478 ALOGW("in_snd_mon_cb for card %d usecase %s, status %s", card,
5479 use_case_table[in->usecase],
5480 status == CARD_STATUS_OFFLINE ? "offline" : "online");
5481
5482 // a better solution would be to report error back to AF and let
5483 // it put the stream to standby
5484 if (status == CARD_STATUS_OFFLINE)
5485 in_standby(&in->stream.common);
5486
5487 return;
5488}
5489
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005490static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
5491{
5492 struct stream_in *in = (struct stream_in *)stream;
5493 struct audio_device *adev = in->dev;
5494 struct str_parms *parms;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005495 char value[32];
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08005496 int ret = 0, val = 0, err;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005497
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05305498 ALOGD("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005499 parms = str_parms_create_str(kvpairs);
5500
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05305501 if (!parms)
5502 goto error;
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005503 lock_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08005504 pthread_mutex_lock(&adev->lock);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08005505
5506 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
5507 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005508 val = atoi(value);
5509 /* no audio source uses val == 0 */
5510 if ((in->source != val) && (val != 0)) {
5511 in->source = val;
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08005512 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
5513 (in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
5514 (voice_extn_compress_voip_is_format_supported(in->format)) &&
Shiv Maliyappanahallie66aba22016-01-27 16:08:57 -08005515 (in->config.rate == 8000 || in->config.rate == 16000 ||
5516 in->config.rate == 32000 || in->config.rate == 48000 ) &&
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07005517 (audio_channel_count_from_in_mask(in->channel_mask) == 1)) {
Narsinga Rao Chella7d5a3e82014-02-04 16:23:52 -08005518 err = voice_extn_compress_voip_open_input_stream(in);
5519 if (err != 0) {
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08005520 ALOGE("%s: Compress voip input cannot be opened, error:%d",
Narsinga Rao Chella7d5a3e82014-02-04 16:23:52 -08005521 __func__, err);
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08005522 }
5523 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005524 }
5525 }
5526
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08005527 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
5528 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005529 val = atoi(value);
Kevin Rocardfce19002017-08-07 19:21:36 -07005530 if (((int)in->device != val) && (val != 0) && audio_is_input_device(val) ) {
5531
5532 // Workaround: If routing to an non existing usb device, fail gracefully
5533 // The routing request will otherwise block during 10 second
5534 int card;
5535 if (audio_is_usb_in_device(val) &&
5536 (card = get_alive_usb_card(parms)) >= 0) {
5537
5538 ALOGW("in_set_parameters() ignoring rerouting to non existing USB card %d", card);
5539 ret = -ENOSYS;
5540 } else {
5541
5542 in->device = val;
5543 /* If recording is in progress, change the tx device to new device */
5544 if (!in->standby && !in->is_st_session) {
5545 ALOGV("update input routing change");
5546 // inform adm before actual routing to prevent glitches.
5547 if (adev->adm_on_routing_change) {
Haynes Mathew George822b5492016-07-01 16:57:24 -07005548 adev->adm_on_routing_change(adev->adm_data,
5549 in->capture_handle);
Kevin Rocardfce19002017-08-07 19:21:36 -07005550 ret = select_devices(adev, in->usecase);
5551 }
5552 }
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005553 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005554 }
5555 }
5556
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +05305557 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_PROFILE, value, sizeof(value));
5558 if (err >= 0) {
5559 strlcpy(in->profile, value, sizeof(in->profile));
5560 ALOGV("updating stream profile with value '%s'", in->profile);
5561 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
5562 &adev->streams_input_cfg_list,
5563 in->device, in->flags, in->format,
5564 in->sample_rate, in->bit_width,
5565 in->profile, &in->app_type_cfg);
5566 }
5567
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005568 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08005569 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005570
5571 str_parms_destroy(parms);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05305572error:
Eric Laurent994a6932013-07-17 11:51:42 -07005573 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005574 return ret;
5575}
5576
5577static char* in_get_parameters(const struct audio_stream *stream,
5578 const char *keys)
5579{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08005580 struct stream_in *in = (struct stream_in *)stream;
5581 struct str_parms *query = str_parms_create_str(keys);
5582 char *str;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08005583 struct str_parms *reply = str_parms_create();
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07005584
5585 if (!query || !reply) {
Alexy Josephaee4fdd2016-01-29 13:02:07 -08005586 if (reply) {
5587 str_parms_destroy(reply);
5588 }
5589 if (query) {
5590 str_parms_destroy(query);
5591 }
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07005592 ALOGE("in_get_parameters: failed to create query or reply");
5593 return NULL;
5594 }
5595
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005596 ALOGV("%s: enter: keys - %s %s ", __func__, use_case_table[in->usecase], keys);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08005597
5598 voice_extn_in_get_parameters(in, query, reply);
5599
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005600 stream_get_parameter_channels(query, reply,
5601 &in->supported_channel_masks[0]);
5602 stream_get_parameter_formats(query, reply,
5603 &in->supported_formats[0]);
5604 stream_get_parameter_rates(query, reply,
5605 &in->supported_sample_rates[0]);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08005606 str = str_parms_to_str(reply);
5607 str_parms_destroy(query);
5608 str_parms_destroy(reply);
5609
5610 ALOGV("%s: exit: returns - %s", __func__, str);
5611 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005612}
5613
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07005614static int in_set_gain(struct audio_stream_in *stream __unused,
5615 float gain __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005616{
5617 return 0;
5618}
5619
5620static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
5621 size_t bytes)
5622{
5623 struct stream_in *in = (struct stream_in *)stream;
Pallavid7c7a272018-01-16 11:22:55 +05305624
5625 if (in == NULL) {
5626 ALOGE("%s: stream_in ptr is NULL", __func__);
5627 return -EINVAL;
5628 }
5629
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005630 struct audio_device *adev = in->dev;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05305631 int ret = -1;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05305632 size_t bytes_read = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005633
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005634 lock_input_stream(in);
Naresh Tanniru4c630392014-05-12 01:05:52 +05305635
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07005636 if (in->is_st_session) {
5637 ALOGVV(" %s: reading on st session bytes=%zu", __func__, bytes);
5638 /* Read from sound trigger HAL */
5639 audio_extn_sound_trigger_read(in, buffer, bytes);
5640 pthread_mutex_unlock(&in->lock);
5641 return bytes;
5642 }
5643
Haynes Mathew George16081042017-05-31 17:16:49 -07005644 if (in->usecase == USECASE_AUDIO_RECORD_MMAP) {
5645 ret = -ENOSYS;
5646 goto exit;
5647 }
5648
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005649 if (in->standby) {
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07005650 pthread_mutex_lock(&adev->lock);
5651 if (in->usecase == USECASE_COMPRESS_VOIP_CALL)
5652 ret = voice_extn_compress_voip_start_input_stream(in);
5653 else
5654 ret = start_input_stream(in);
5655 pthread_mutex_unlock(&adev->lock);
5656 if (ret != 0) {
5657 goto exit;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005658 }
5659 in->standby = 0;
5660 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005661
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005662 // what's the duration requested by the client?
5663 long ns = 0;
5664
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05305665 if (in->pcm && in->config.rate)
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005666 ns = pcm_bytes_to_frames(in->pcm, bytes)*1000000000LL/
5667 in->config.rate;
5668
5669 request_in_focus(in, ns);
5670 bool use_mmap = is_mmap_usecase(in->usecase) || in->realtime;
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07005671
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05305672 if (audio_extn_cin_attached_usecase(in->usecase)) {
5673 ret = audio_extn_cin_read(in, buffer, bytes, &bytes_read);
5674 } else if (in->pcm) {
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05305675 if (audio_extn_ssr_get_stream() == in) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07005676 ret = audio_extn_ssr_read(stream, buffer, bytes);
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05305677 } else if (audio_extn_compr_cap_usecase_supported(in->usecase)) {
Mingming Yine62d7842013-10-25 16:26:03 -07005678 ret = audio_extn_compr_cap_read(in, buffer, bytes);
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005679 } else if (use_mmap) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07005680 ret = pcm_mmap_read(in->pcm, buffer, bytes);
Garmond Leunge2433c32017-09-28 21:51:22 -07005681 } else if (audio_extn_ffv_get_stream() == in) {
5682 ret = audio_extn_ffv_read(stream, buffer, bytes);
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05305683 } else {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07005684 ret = pcm_read(in->pcm, buffer, bytes);
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05305685 /* data from DSP comes in 24_8 format, convert it to 8_24 */
5686 if (!ret && bytes > 0 && (in->format == AUDIO_FORMAT_PCM_8_24_BIT)) {
5687 if (audio_extn_utils_convert_format_24_8_to_8_24(buffer, bytes)
5688 != bytes) {
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05305689 ret = -EINVAL;
5690 goto exit;
5691 }
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05305692 } else if (ret < 0) {
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05305693 ret = -errno;
5694 }
5695 }
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05305696 /* bytes read is always set to bytes for non compress usecases */
5697 bytes_read = bytes;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005698 }
5699
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005700 release_in_focus(in);
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07005701
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005702 /*
5703 * Instead of writing zeroes here, we could trust the hardware
5704 * to always provide zeroes when muted.
5705 */
Pavan Chikkala63964842014-12-04 10:48:28 +05305706 if (ret == 0 && voice_get_mic_mute(adev) && !voice_is_in_call_rec_stream(in) &&
5707 in->usecase != USECASE_AUDIO_RECORD_AFE_PROXY)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005708 memset(buffer, 0, bytes);
5709
5710exit:
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07005711 if (-ENETRESET == ret)
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305712 in->card_status = CARD_STATUS_OFFLINE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005713 pthread_mutex_unlock(&in->lock);
5714
5715 if (ret != 0) {
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05305716 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05305717 pthread_mutex_lock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05305718 voice_extn_compress_voip_close_input_stream(&in->stream.common);
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05305719 pthread_mutex_unlock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05305720 in->standby = true;
5721 }
Sharad Sangled17c9122017-03-20 15:58:52 +05305722 if (!audio_extn_cin_attached_usecase(in->usecase)) {
5723 bytes_read = bytes;
5724 memset(buffer, 0, bytes);
5725 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005726 in_standby(&in->stream.common);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07005727 ALOGV("%s: read failed status %d- sleeping for buffer duration", __func__, ret);
Ashish Jainbbce4322016-02-16 13:25:27 +05305728 usleep((uint64_t)bytes * 1000000 / audio_stream_in_frame_size(stream) /
Naresh Tanniru4c630392014-05-12 01:05:52 +05305729 in_get_sample_rate(&in->stream.common));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005730 }
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05305731 return bytes_read;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005732}
5733
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07005734static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005735{
5736 return 0;
5737}
5738
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07005739static int add_remove_audio_effect(const struct audio_stream *stream,
5740 effect_handle_t effect,
5741 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005742{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07005743 struct stream_in *in = (struct stream_in *)stream;
5744 int status = 0;
5745 effect_descriptor_t desc;
5746
5747 status = (*effect)->get_descriptor(effect, &desc);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07005748 ALOGV("%s: status %d in->standby %d enable:%d", __func__, status, in->standby, enable);
5749
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07005750 if (status != 0)
5751 return status;
5752
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005753 lock_input_stream(in);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07005754 pthread_mutex_lock(&in->dev->lock);
kunleizd96526c2018-04-09 11:12:32 +08005755 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
5756 in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07005757 in->enable_aec != enable &&
5758 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
5759 in->enable_aec = enable;
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07005760 if (!in->standby) {
5761 if (enable_disable_effect(in->dev, EFFECT_AEC, enable) == ENOSYS)
5762 select_devices(in->dev, in->usecase);
5763 }
5764
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07005765 }
Ravi Kumar Alamanda198185e2013-11-07 15:42:19 -08005766 if (in->enable_ns != enable &&
5767 (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
5768 in->enable_ns = enable;
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07005769 if (!in->standby) {
kunleizd96526c2018-04-09 11:12:32 +08005770 if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
5771 in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) {
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07005772 if (enable_disable_effect(in->dev, EFFECT_NS, enable) == ENOSYS)
5773 select_devices(in->dev, in->usecase);
5774 } else
5775 select_devices(in->dev, in->usecase);
5776 }
Ravi Kumar Alamanda198185e2013-11-07 15:42:19 -08005777 }
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07005778 pthread_mutex_unlock(&in->dev->lock);
5779 pthread_mutex_unlock(&in->lock);
5780
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005781 return 0;
5782}
5783
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07005784static int in_add_audio_effect(const struct audio_stream *stream,
5785 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005786{
Eric Laurent994a6932013-07-17 11:51:42 -07005787 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07005788 return add_remove_audio_effect(stream, effect, true);
5789}
5790
5791static int in_remove_audio_effect(const struct audio_stream *stream,
5792 effect_handle_t effect)
5793{
Eric Laurent994a6932013-07-17 11:51:42 -07005794 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07005795 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005796}
5797
Haynes Mathew George16081042017-05-31 17:16:49 -07005798static int in_stop(const struct audio_stream_in* stream)
5799{
5800 struct stream_in *in = (struct stream_in *)stream;
5801 struct audio_device *adev = in->dev;
5802
5803 int ret = -ENOSYS;
5804 ALOGV("%s", __func__);
5805 pthread_mutex_lock(&adev->lock);
5806 if (in->usecase == USECASE_AUDIO_RECORD_MMAP && !in->standby &&
5807 in->capture_started && in->pcm != NULL) {
5808 pcm_stop(in->pcm);
5809 ret = stop_input_stream(in);
5810 in->capture_started = false;
5811 }
5812 pthread_mutex_unlock(&adev->lock);
5813 return ret;
5814}
5815
5816static int in_start(const struct audio_stream_in* stream)
5817{
5818 struct stream_in *in = (struct stream_in *)stream;
5819 struct audio_device *adev = in->dev;
5820 int ret = -ENOSYS;
5821
5822 ALOGV("%s in %p", __func__, in);
5823 pthread_mutex_lock(&adev->lock);
5824 if (in->usecase == USECASE_AUDIO_RECORD_MMAP && !in->standby &&
5825 !in->capture_started && in->pcm != NULL) {
5826 if (!in->capture_started) {
5827 ret = start_input_stream(in);
5828 if (ret == 0) {
5829 in->capture_started = true;
5830 }
5831 }
5832 }
5833 pthread_mutex_unlock(&adev->lock);
5834 return ret;
5835}
5836
5837static int in_create_mmap_buffer(const struct audio_stream_in *stream,
5838 int32_t min_size_frames,
5839 struct audio_mmap_buffer_info *info)
5840{
5841 struct stream_in *in = (struct stream_in *)stream;
5842 struct audio_device *adev = in->dev;
5843 int ret = 0;
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07005844 unsigned int offset1 = 0;
5845 unsigned int frames1 = 0;
Haynes Mathew George16081042017-05-31 17:16:49 -07005846 const char *step = "";
Arun Mirpuriebe78a72018-10-04 18:23:46 -07005847 uint32_t mmap_size = 0;
5848 uint32_t buffer_size = 0;
Haynes Mathew George16081042017-05-31 17:16:49 -07005849
5850 pthread_mutex_lock(&adev->lock);
5851 ALOGV("%s in %p", __func__, in);
5852
Sharad Sangle90e613f2018-05-04 16:15:38 +05305853 if (CARD_STATUS_OFFLINE == in->card_status||
5854 CARD_STATUS_OFFLINE == adev->card_status) {
5855 ALOGW("in->card_status or adev->card_status offline, try again");
5856 ret = -EIO;
5857 goto exit;
5858 }
5859
Haynes Mathew George16081042017-05-31 17:16:49 -07005860 if (info == NULL || min_size_frames == 0) {
5861 ALOGE("%s invalid argument info %p min_size_frames %d", __func__, info, min_size_frames);
5862 ret = -EINVAL;
5863 goto exit;
5864 }
5865 if (in->usecase != USECASE_AUDIO_RECORD_MMAP || !in->standby) {
5866 ALOGE("%s: usecase = %d, standby = %d", __func__, in->usecase, in->standby);
5867 ALOGV("%s in %p", __func__, in);
5868 ret = -ENOSYS;
5869 goto exit;
5870 }
5871 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
5872 if (in->pcm_device_id < 0) {
5873 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
5874 __func__, in->pcm_device_id, in->usecase);
5875 ret = -EINVAL;
5876 goto exit;
5877 }
5878
5879 adjust_mmap_period_count(&in->config, min_size_frames);
5880
5881 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
5882 __func__, adev->snd_card, in->pcm_device_id, in->config.channels);
5883 in->pcm = pcm_open(adev->snd_card, in->pcm_device_id,
5884 (PCM_IN | PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC), &in->config);
Sharad Sangle90e613f2018-05-04 16:15:38 +05305885 if (errno == ENETRESET && !pcm_is_ready(in->pcm)) {
5886 ALOGE("%s: pcm_open failed errno:%d\n", __func__, errno);
5887 in->card_status = CARD_STATUS_OFFLINE;
5888 adev->card_status = CARD_STATUS_OFFLINE;
5889 ret = -EIO;
5890 goto exit;
5891 }
5892
Haynes Mathew George16081042017-05-31 17:16:49 -07005893 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) {
5894 step = "open";
5895 ret = -ENODEV;
5896 goto exit;
5897 }
5898
5899 ret = pcm_mmap_begin(in->pcm, &info->shared_memory_address, &offset1, &frames1);
5900 if (ret < 0) {
5901 step = "begin";
5902 goto exit;
5903 }
Haynes Mathew George16081042017-05-31 17:16:49 -07005904
Arun Mirpuriebe78a72018-10-04 18:23:46 -07005905 info->buffer_size_frames = pcm_get_buffer_size(in->pcm);
5906 buffer_size = pcm_frames_to_bytes(in->pcm, info->buffer_size_frames);
5907 info->burst_size_frames = in->config.period_size;
5908 ret = platform_get_mmap_data_fd(adev->platform,
5909 in->pcm_device_id, 1 /*capture*/,
5910 &info->shared_memory_fd,
5911 &mmap_size);
5912 if (ret < 0) {
5913 // Fall back to non exclusive mode
5914 info->shared_memory_fd = pcm_get_poll_fd(in->pcm);
5915 } else {
5916 if (mmap_size < buffer_size) {
5917 step = "mmap";
5918 goto exit;
5919 }
5920 // FIXME: indicate exclusive mode support by returning a negative buffer size
5921 info->buffer_size_frames *= -1;
5922 }
5923
5924 memset(info->shared_memory_address, 0, buffer_size);
Haynes Mathew George16081042017-05-31 17:16:49 -07005925
5926 ret = pcm_mmap_commit(in->pcm, 0, MMAP_PERIOD_SIZE);
5927 if (ret < 0) {
5928 step = "commit";
5929 goto exit;
5930 }
5931
5932 in->standby = false;
5933 ret = 0;
5934
5935 ALOGV("%s: got mmap buffer address %p info->buffer_size_frames %d",
5936 __func__, info->shared_memory_address, info->buffer_size_frames);
5937
5938exit:
5939 if (ret != 0) {
5940 if (in->pcm == NULL) {
5941 ALOGE("%s: %s - %d", __func__, step, ret);
5942 } else {
5943 ALOGE("%s: %s %s", __func__, step, pcm_get_error(in->pcm));
5944 pcm_close(in->pcm);
5945 in->pcm = NULL;
5946 }
5947 }
5948 pthread_mutex_unlock(&adev->lock);
5949 return ret;
5950}
5951
5952static int in_get_mmap_position(const struct audio_stream_in *stream,
5953 struct audio_mmap_position *position)
5954{
5955 struct stream_in *in = (struct stream_in *)stream;
5956 ALOGVV("%s", __func__);
5957 if (position == NULL) {
5958 return -EINVAL;
5959 }
5960 if (in->usecase != USECASE_AUDIO_RECORD_MMAP) {
5961 return -ENOSYS;
5962 }
5963 if (in->pcm == NULL) {
5964 return -ENOSYS;
5965 }
5966 struct timespec ts = { 0, 0 };
5967 int ret = pcm_mmap_get_hw_ptr(in->pcm, (unsigned int *)&position->position_frames, &ts);
5968 if (ret < 0) {
5969 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
5970 return ret;
5971 }
Naresh Tannirua15d7e92018-11-02 09:49:44 +05305972 position->time_nanoseconds = ts.tv_sec*1000000000LL + ts.tv_nsec;
Haynes Mathew George16081042017-05-31 17:16:49 -07005973 return 0;
5974}
5975
Naresh Tannirudcb47c52018-06-25 16:23:32 +05305976static int in_get_active_microphones(const struct audio_stream_in *stream,
5977 struct audio_microphone_characteristic_t *mic_array,
5978 size_t *mic_count) {
5979 struct stream_in *in = (struct stream_in *)stream;
5980 struct audio_device *adev = in->dev;
5981 ALOGVV("%s", __func__);
5982
5983 lock_input_stream(in);
5984 pthread_mutex_lock(&adev->lock);
5985 int ret = platform_get_active_microphones(adev->platform,
5986 audio_channel_count_from_in_mask(in->channel_mask),
5987 in->usecase, mic_array, mic_count);
5988 pthread_mutex_unlock(&adev->lock);
5989 pthread_mutex_unlock(&in->lock);
5990
5991 return ret;
5992}
5993
5994static int adev_get_microphones(const struct audio_hw_device *dev,
5995 struct audio_microphone_characteristic_t *mic_array,
5996 size_t *mic_count) {
5997 struct audio_device *adev = (struct audio_device *)dev;
5998 ALOGVV("%s", __func__);
5999
6000 pthread_mutex_lock(&adev->lock);
6001 int ret = platform_get_microphones(adev->platform, mic_array, mic_count);
6002 pthread_mutex_unlock(&adev->lock);
6003
6004 return ret;
6005}
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05306006int adev_open_output_stream(struct audio_hw_device *dev,
Haynes Mathew George16081042017-05-31 17:16:49 -07006007 audio_io_handle_t handle,
6008 audio_devices_t devices,
6009 audio_output_flags_t flags,
6010 struct audio_config *config,
6011 struct audio_stream_out **stream_out,
6012 const char *address __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006013{
6014 struct audio_device *adev = (struct audio_device *)dev;
6015 struct stream_out *out;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05306016 int ret = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07006017 audio_format_t format;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08006018 struct adsp_hdlr_stream_cfg hdlr_stream_cfg;
Manish Dewangan21a850a2017-08-14 12:03:55 +05306019 bool is_direct_passthough = false;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07006020 bool is_hdmi = devices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
6021 bool is_usb_dev = audio_is_usb_out_device(devices) &&
6022 (devices != AUDIO_DEVICE_OUT_USB_ACCESSORY);
6023 bool direct_dev = is_hdmi || is_usb_dev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006024
kunleizdff872d2018-08-20 14:40:33 +08006025 if (is_usb_dev && (!audio_extn_usb_connected(NULL))) {
kunleizd6a9e0c2018-07-30 15:38:52 +08006026 is_usb_dev = false;
6027 devices = AUDIO_DEVICE_OUT_SPEAKER;
6028 ALOGW("%s: ignore set device to non existing USB card, use output device(%#x)",
6029 __func__, devices);
6030 }
6031
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006032 *stream_out = NULL;
Naresh Tanniru80659832014-06-04 18:17:56 +05306033
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006034 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
6035
Mingming Yin3a941d42016-02-17 18:08:05 -08006036 ALOGD("%s: enter: format(%#x) sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)\
6037 stream_handle(%p)", __func__, config->format, config->sample_rate, config->channel_mask,
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05306038 devices, flags, &out->stream);
6039
6040
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08006041 if (!out) {
6042 return -ENOMEM;
6043 }
6044
Haynes Mathew George204045b2015-02-25 20:32:03 -08006045 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07006046 pthread_mutex_init(&out->pre_lock, (const pthread_mutexattr_t *) NULL);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05306047 pthread_mutex_init(&out->compr_mute_lock, (const pthread_mutexattr_t *) NULL);
Zhou Song48453a02018-01-10 17:50:59 +08006048 pthread_mutex_init(&out->position_query_lock, (const pthread_mutexattr_t *) NULL);
Haynes Mathew George204045b2015-02-25 20:32:03 -08006049 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
6050
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006051 if (devices == AUDIO_DEVICE_NONE)
6052 devices = AUDIO_DEVICE_OUT_SPEAKER;
6053
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006054 out->flags = flags;
6055 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07006056 out->dev = adev;
Aalique Grahame65780b52017-09-27 14:59:56 -07006057 out->hal_op_format = out->hal_ip_format = format = out->format = config->format;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006058 out->sample_rate = config->sample_rate;
Sachin Mohan Gadag3d09acd2017-06-19 12:43:44 +05306059 out->channel_mask = config->channel_mask;
Ramjee Singh5857aeb2017-08-03 19:18:50 +05306060 if (out->channel_mask == AUDIO_CHANNEL_NONE)
6061 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
6062 else
6063 out->supported_channel_masks[0] = out->channel_mask;
Eric Laurentc4aef752013-09-12 17:45:53 -07006064 out->handle = handle;
Mingming Yin3ee55c62014-08-04 14:23:35 -07006065 out->bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Alexy Josephaa54c872014-12-03 02:46:47 -08006066 out->non_blocking = 0;
Ashish Jain83a6cc22016-06-28 14:34:17 +05306067 out->convert_buffer = NULL;
Ashish Jain1b9b30c2017-05-18 20:57:40 +05306068 out->started = 0;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05306069 out->a2dp_compress_mute = false;
Aniket Kumar Lata932f4872017-11-06 18:29:44 -08006070 out->hal_output_suspend_supported = 0;
6071 out->dynamic_pm_qos_config_supported = 0;
Surendar Karka5a18a7a2018-04-26 11:28:38 +05306072 out->set_dual_mono = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006073
Nikhil Laturkar26b690b2017-07-25 11:06:14 +05306074 if ((flags & AUDIO_OUTPUT_FLAG_BD) &&
Satish Babu Patakokila37e7c482018-02-02 11:50:06 +05306075 (property_get_bool("vendor.audio.matrix.limiter.enable", false)))
Ben Romberger6c4d3812017-06-13 17:46:45 -07006076 platform_set_device_params(out, DEVICE_PARAM_LIMITER_ID, 1);
6077
Haynes Mathew George484e8d22017-07-31 18:55:17 -07006078 if (audio_is_linear_pcm(out->format) &&
6079 out->flags == AUDIO_OUTPUT_FLAG_NONE && direct_dev) {
6080 pthread_mutex_lock(&adev->lock);
6081 if (is_hdmi) {
6082 ALOGV("AUDIO_DEVICE_OUT_AUX_DIGITAL and DIRECT|OFFLOAD, check hdmi caps");
6083 ret = read_hdmi_sink_caps(out);
6084 } else if (is_usb_dev) {
6085 ret = read_usb_sup_params_and_compare(true /*is_playback*/,
6086 &config->format,
6087 &out->supported_formats[0],
6088 MAX_SUPPORTED_FORMATS,
6089 &config->channel_mask,
6090 &out->supported_channel_masks[0],
6091 MAX_SUPPORTED_CHANNEL_MASKS,
6092 &config->sample_rate,
6093 &out->supported_sample_rates[0],
6094 MAX_SUPPORTED_SAMPLE_RATES);
6095 ALOGV("plugged dev USB ret %d", ret);
6096 } else {
6097 ret = -1;
6098 }
6099 pthread_mutex_unlock(&adev->lock);
6100 if (ret != 0) {
Mingming Yin3a941d42016-02-17 18:08:05 -08006101 if (ret == -ENOSYS) {
6102 /* ignore and go with default */
6103 ret = 0;
6104 } else {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07006105 ALOGE("error reading direct dev sink caps");
Mingming Yin3a941d42016-02-17 18:08:05 -08006106 goto error_open;
6107 }
6108 }
6109 }
6110
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006111 /* Init use case and pcm_config */
Vikram Pandurangafa3128d2017-10-16 16:57:22 -07006112#ifndef COMPRESS_VOIP_ENABLED
Vikram Panduranga93f080e2017-06-07 18:16:14 -07006113 if (out->flags == (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_VOIP_RX) &&
6114 (out->sample_rate == 8000 || out->sample_rate == 16000 ||
6115 out->sample_rate == 32000 || out->sample_rate == 48000)) {
6116 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_MONO;
6117 out->channel_mask = AUDIO_CHANNEL_OUT_MONO;
6118 out->usecase = USECASE_AUDIO_PLAYBACK_VOIP;
6119
6120 out->config = default_pcm_config_voip_copp;
6121 out->config.period_size = VOIP_IO_BUF_SIZE(out->sample_rate, DEFAULT_VOIP_BUF_DURATION_MS, DEFAULT_VOIP_BIT_DEPTH_BYTE)/2;
6122 out->config.rate = out->sample_rate;
6123
6124#else
Preetam Singh Ranawat319b1cd2017-10-12 10:46:30 +05306125 if ((out->dev->mode == AUDIO_MODE_IN_COMMUNICATION || voice_extn_compress_voip_is_active(out->dev)) &&
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08006126 (out->flags == (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_VOIP_RX)) &&
Narsinga Rao Chella1eceff82013-12-02 19:25:28 -08006127 (voice_extn_compress_voip_is_config_supported(config))) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08006128 ret = voice_extn_compress_voip_open_output_stream(out);
6129 if (ret != 0) {
6130 ALOGE("%s: Compress voip output cannot be opened, error:%d",
6131 __func__, ret);
6132 goto error_open;
6133 }
Vikram Panduranga93f080e2017-06-07 18:16:14 -07006134#endif
Haynes Mathew George484e8d22017-07-31 18:55:17 -07006135 } else if (audio_is_linear_pcm(out->format) &&
6136 out->flags == AUDIO_OUTPUT_FLAG_NONE && is_usb_dev) {
6137 out->channel_mask = config->channel_mask;
6138 out->sample_rate = config->sample_rate;
6139 out->format = config->format;
6140 out->usecase = USECASE_AUDIO_PLAYBACK_HIFI;
6141 // does this change?
6142 out->config = is_hdmi ? pcm_config_hdmi_multi : pcm_config_hifi;
6143 out->config.rate = config->sample_rate;
6144 out->config.channels = audio_channel_count_from_out_mask(out->channel_mask);
6145 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels *
6146 audio_bytes_per_sample(config->format));
6147 out->config.format = pcm_format_from_audio_format(out->format);
vivek mehta0ea887a2015-08-26 14:01:20 -07006148 } else if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
Dhananjay Kumarac341582017-02-23 23:42:25 +05306149 (out->flags == AUDIO_OUTPUT_FLAG_DIRECT)) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306150 pthread_mutex_lock(&adev->lock);
6151 bool offline = (adev->card_status == CARD_STATUS_OFFLINE);
6152 pthread_mutex_unlock(&adev->lock);
6153
6154 // reject offload during card offline to allow
6155 // fallback to s/w paths
6156 if (offline) {
6157 ret = -ENODEV;
6158 goto error_open;
6159 }
vivek mehta0ea887a2015-08-26 14:01:20 -07006160
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006161 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
6162 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
6163 ALOGE("%s: Unsupported Offload information", __func__);
6164 ret = -EINVAL;
6165 goto error_open;
6166 }
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07006167
Atul Khare3fa6e542017-08-09 00:56:17 +05306168 if (config->offload_info.format == 0)
6169 config->offload_info.format = config->format;
6170 if (config->offload_info.sample_rate == 0)
6171 config->offload_info.sample_rate = config->sample_rate;
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07006172
Mingming Yin90310102013-11-13 16:57:00 -08006173 if (!is_supported_format(config->offload_info.format) &&
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05306174 !audio_extn_passthru_is_supported_format(config->offload_info.format)) {
vivek mehta0ea887a2015-08-26 14:01:20 -07006175 ALOGE("%s: Unsupported audio format %x " , __func__, config->offload_info.format);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006176 ret = -EINVAL;
6177 goto error_open;
6178 }
6179
Ben Romberger0f8c87b2017-05-24 17:41:11 -07006180 /* TrueHD only supported for 48k multiples (48k, 96k, 192k) */
6181 if ((config->offload_info.format == AUDIO_FORMAT_DOLBY_TRUEHD) &&
6182 (audio_extn_passthru_is_passthrough_stream(out)) &&
6183 !((config->sample_rate == 48000) ||
6184 (config->sample_rate == 96000) ||
6185 (config->sample_rate == 192000))) {
6186 ALOGE("%s: Unsupported sample rate %d for audio format %x",
6187 __func__, config->sample_rate, config->offload_info.format);
6188 ret = -EINVAL;
6189 goto error_open;
6190 }
6191
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006192 out->compr_config.codec = (struct snd_codec *)
6193 calloc(1, sizeof(struct snd_codec));
6194
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07006195 if (!out->compr_config.codec) {
6196 ret = -ENOMEM;
6197 goto error_open;
6198 }
6199
Dhananjay Kumarac341582017-02-23 23:42:25 +05306200 out->stream.pause = out_pause;
6201 out->stream.resume = out_resume;
6202 out->stream.flush = out_flush;
Ashish Jain4847e9d2017-08-17 19:16:57 +05306203 out->stream.set_callback = out_set_callback;
Dhananjay Kumarac341582017-02-23 23:42:25 +05306204 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Mingming Yin21d60472015-09-30 13:56:25 -07006205 out->stream.drain = out_drain;
Dhananjay Kumarac341582017-02-23 23:42:25 +05306206 out->usecase = get_offload_usecase(adev, true /* is_compress */);
vivek mehta446c3962015-09-14 10:57:35 -07006207 ALOGV("Compress Offload usecase .. usecase selected %d", out->usecase);
Dhananjay Kumarac341582017-02-23 23:42:25 +05306208 } else {
6209 out->usecase = get_offload_usecase(adev, false /* is_compress */);
6210 ALOGV("non-offload DIRECT_usecase ... usecase selected %d ", out->usecase);
vivek mehta0ea887a2015-08-26 14:01:20 -07006211 }
vivek mehta446c3962015-09-14 10:57:35 -07006212
6213 if (out->usecase == USECASE_INVALID) {
Mingming Yin3a941d42016-02-17 18:08:05 -08006214 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL &&
6215 config->format == 0 && config->sample_rate == 0 &&
6216 config->channel_mask == 0) {
Mingming Yin21854652016-04-13 11:54:02 -07006217 ALOGI("%s dummy open to query sink capability",__func__);
Mingming Yin3a941d42016-02-17 18:08:05 -08006218 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
6219 } else {
6220 ALOGE("%s, Max allowed OFFLOAD usecase reached ... ", __func__);
6221 ret = -EEXIST;
6222 goto error_open;
6223 }
vivek mehta446c3962015-09-14 10:57:35 -07006224 }
6225
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006226 if (config->offload_info.channel_mask)
6227 out->channel_mask = config->offload_info.channel_mask;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08006228 else if (config->channel_mask) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006229 out->channel_mask = config->channel_mask;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08006230 config->offload_info.channel_mask = config->channel_mask;
Haynes Mathew Georgea99f7532016-08-24 16:01:21 -07006231 } else {
Dhananjay Kumarac341582017-02-23 23:42:25 +05306232 ALOGE("out->channel_mask not set for OFFLOAD/DIRECT usecase");
Haynes Mathew Georgea99f7532016-08-24 16:01:21 -07006233 ret = -EINVAL;
6234 goto error_open;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08006235 }
Haynes Mathew Georgea99f7532016-08-24 16:01:21 -07006236
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07006237 format = out->format = config->offload_info.format;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006238 out->sample_rate = config->offload_info.sample_rate;
6239
Mingming Yin3ee55c62014-08-04 14:23:35 -07006240 out->bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006241
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05306242 out->compr_config.codec->id = get_snd_codec_id(config->offload_info.format);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05306243 if (audio_extn_utils_is_dolby_format(config->offload_info.format)) {
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05306244 audio_extn_dolby_send_ddp_endp_params(adev);
6245 audio_extn_dolby_set_dmid(adev);
6246 }
vivek mehta0ea887a2015-08-26 14:01:20 -07006247
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006248 out->compr_config.codec->sample_rate =
Ravi Kumar Alamandab91bff32014-11-14 12:05:54 -08006249 config->offload_info.sample_rate;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006250 out->compr_config.codec->bit_rate =
6251 config->offload_info.bit_rate;
6252 out->compr_config.codec->ch_in =
Dhanalakshmi Siddania15c6792016-08-10 15:33:53 +05306253 audio_channel_count_from_out_mask(out->channel_mask);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006254 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
Satish Babu Patakokilaa395a9e2016-11-01 12:18:49 +05306255 /* Update bit width only for non passthrough usecases.
6256 * For passthrough usecases, the output will always be opened @16 bit
6257 */
6258 if (!audio_extn_passthru_is_passthrough_stream(out))
6259 out->bit_width = AUDIO_OUTPUT_BIT_WIDTH;
Naresh Tanniruee3499a2017-01-05 14:05:35 +05306260
6261 if (out->flags & AUDIO_OUTPUT_FLAG_TIMESTAMP)
6262 out->compr_config.codec->flags |= COMPRESSED_TIMESTAMP_FLAG;
6263 ALOGVV("%s : out->compr_config.codec->flags -> (%#x) ", __func__, out->compr_config.codec->flags);
6264
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07006265 /*TODO: Do we need to change it for passthrough */
6266 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_RAW;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006267
Manish Dewangana6fc5442015-08-24 20:30:31 +05306268 if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC)
6269 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_RAW;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05306270 else if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS)
Manish Dewangana6fc5442015-08-24 20:30:31 +05306271 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_MP4ADTS;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05306272 else if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_LATM)
6273 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_MP4LATM;
Ashish Jainf1eaa582016-05-23 20:54:24 +05306274
6275 if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) ==
6276 AUDIO_FORMAT_PCM) {
6277
6278 /*Based on platform support, configure appropriate alsa format for corresponding
6279 *hal input format.
6280 */
6281 out->compr_config.codec->format = hal_format_to_alsa(
6282 config->offload_info.format);
6283
Ashish Jain83a6cc22016-06-28 14:34:17 +05306284 out->hal_op_format = alsa_format_to_hal(
Ashish Jainf1eaa582016-05-23 20:54:24 +05306285 out->compr_config.codec->format);
Ashish Jain83a6cc22016-06-28 14:34:17 +05306286 out->hal_ip_format = out->format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05306287
Dhananjay Kumarac341582017-02-23 23:42:25 +05306288 /*for direct non-compress playback populate bit_width based on selected alsa format as
Ashish Jainf1eaa582016-05-23 20:54:24 +05306289 *hal input format and alsa format might differ based on platform support.
6290 */
6291 out->bit_width = audio_bytes_per_sample(
Ashish Jain83a6cc22016-06-28 14:34:17 +05306292 out->hal_op_format) << 3;
Ashish Jainf1eaa582016-05-23 20:54:24 +05306293
6294 out->compr_config.fragments = DIRECT_PCM_NUM_FRAGMENTS;
6295
6296 /* Check if alsa session is configured with the same format as HAL input format,
6297 * if not then derive correct fragment size needed to accomodate the
6298 * conversion of HAL input format to alsa format.
6299 */
6300 audio_extn_utils_update_direct_pcm_fragment_size(out);
6301
6302 /*if hal input and output fragment size is different this indicates HAL input format is
6303 *not same as the alsa format
6304 */
Ashish Jain83a6cc22016-06-28 14:34:17 +05306305 if (out->hal_fragment_size != out->compr_config.fragment_size) {
Ashish Jainf1eaa582016-05-23 20:54:24 +05306306 /*Allocate a buffer to convert input data to the alsa configured format.
6307 *size of convert buffer is equal to the size required to hold one fragment size
6308 *worth of pcm data, this is because flinger does not write more than fragment_size
6309 */
Ashish Jain83a6cc22016-06-28 14:34:17 +05306310 out->convert_buffer = calloc(1,out->compr_config.fragment_size);
6311 if (out->convert_buffer == NULL){
Ashish Jainf1eaa582016-05-23 20:54:24 +05306312 ALOGE("Allocation failed for convert buffer for size %d", out->compr_config.fragment_size);
6313 ret = -ENOMEM;
6314 goto error_open;
6315 }
6316 }
6317 } else if (audio_extn_passthru_is_passthrough_stream(out)) {
6318 out->compr_config.fragment_size =
6319 audio_extn_passthru_get_buffer_size(&config->offload_info);
6320 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
6321 } else {
6322 out->compr_config.fragment_size =
6323 platform_get_compress_offload_buffer_size(&config->offload_info);
6324 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
6325 }
Mingming Yin3ee55c62014-08-04 14:23:35 -07006326
Naresh Tanniruee3499a2017-01-05 14:05:35 +05306327 if (out->flags & AUDIO_OUTPUT_FLAG_TIMESTAMP) {
6328 out->compr_config.fragment_size += sizeof(struct snd_codec_metadata);
6329 }
Amit Shekhar6f461b12014-08-01 14:52:58 -07006330 if (config->offload_info.format == AUDIO_FORMAT_FLAC)
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +05306331 out->compr_config.codec->options.flac_dec.sample_size = AUDIO_OUTPUT_BIT_WIDTH;
Mingming Yin3ee55c62014-08-04 14:23:35 -07006332
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05306333 if (config->offload_info.format == AUDIO_FORMAT_APTX) {
6334 audio_extn_send_aptx_dec_bt_addr_to_dsp(out);
6335 }
6336
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006337 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
6338 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07006339
Manish Dewangan69426c82017-01-30 17:35:36 +05306340 if ((flags & AUDIO_OUTPUT_FLAG_TIMESTAMP) &&
6341 (flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC)) {
6342 out->render_mode = RENDER_MODE_AUDIO_STC_MASTER;
6343 } else if(flags & AUDIO_OUTPUT_FLAG_TIMESTAMP) {
6344 out->render_mode = RENDER_MODE_AUDIO_MASTER;
6345 } else {
6346 out->render_mode = RENDER_MODE_AUDIO_NO_TIMESTAMP;
6347 }
Alexy Josephaa54c872014-12-03 02:46:47 -08006348
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05306349 memset(&out->channel_map_param, 0,
6350 sizeof(struct audio_out_channel_map_param));
6351
Haynes Mathew George352f27b2013-07-26 00:00:15 -07006352 out->send_new_metadata = 1;
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05306353 out->send_next_track_params = false;
6354 out->is_compr_metadata_avail = false;
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08006355 out->offload_state = OFFLOAD_STATE_IDLE;
6356 out->playback_started = 0;
Zhou Song48453a02018-01-10 17:50:59 +08006357 out->writeAt.tv_sec = 0;
6358 out->writeAt.tv_nsec = 0;
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08006359
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08006360 audio_extn_dts_create_state_notifier_node(out->usecase);
6361
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006362 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
6363 __func__, config->offload_info.version,
6364 config->offload_info.bit_rate);
Ashish Jain5106d362016-05-11 19:23:33 +05306365
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05306366 /* Check if DSD audio format is supported in codec
6367 * and there is no active native DSD use case
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05306368 */
6369
6370 if ((config->format == AUDIO_FORMAT_DSD) &&
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05306371 (!platform_check_codec_dsd_support(adev->platform) ||
6372 audio_is_dsd_native_stream_active(adev))) {
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05306373 ret = -EINVAL;
6374 goto error_open;
6375 }
6376
Ashish Jain5106d362016-05-11 19:23:33 +05306377 /* Disable gapless if any of the following is true
6378 * passthrough playback
6379 * AV playback
Dhananjay Kumarac341582017-02-23 23:42:25 +05306380 * non compressed Direct playback
Ashish Jain5106d362016-05-11 19:23:33 +05306381 */
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05306382 if (audio_extn_passthru_is_passthrough_stream(out) ||
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05306383 (config->format == AUDIO_FORMAT_DSD) ||
Naresh Tanniru928f0862017-04-07 16:44:23 -07006384 (config->format == AUDIO_FORMAT_IEC61937) ||
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05306385 config->offload_info.has_video ||
Dhananjay Kumarac341582017-02-23 23:42:25 +05306386 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Ashish Jain5106d362016-05-11 19:23:33 +05306387 check_and_set_gapless_mode(adev, false);
6388 } else
6389 check_and_set_gapless_mode(adev, true);
Mingming Yin21854652016-04-13 11:54:02 -07006390
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05306391 if (audio_extn_passthru_is_passthrough_stream(out)) {
Mingming Yin21854652016-04-13 11:54:02 -07006392 out->flags |= AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH;
6393 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05306394 if (config->format == AUDIO_FORMAT_DSD) {
6395 out->flags |= AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH;
6396 out->compr_config.codec->compr_passthr = PASSTHROUGH_DSD;
6397 }
Aalique Grahame0359a1f2016-09-08 16:54:22 -07006398
6399 create_offload_callback_thread(out);
6400
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07006401 } else if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006402 switch (config->sample_rate) {
6403 case 0:
6404 out->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
6405 break;
6406 case 8000:
6407 case 16000:
6408 case 48000:
6409 out->sample_rate = config->sample_rate;
6410 break;
6411 default:
6412 ALOGE("%s: Unsupported sampling rate %d for Incall Music", __func__,
6413 config->sample_rate);
6414 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
6415 ret = -EINVAL;
6416 goto error_open;
6417 }
6418 //FIXME: add support for MONO stream configuration when audioflinger mixer supports it
6419 switch (config->channel_mask) {
6420 case AUDIO_CHANNEL_NONE:
6421 case AUDIO_CHANNEL_OUT_STEREO:
6422 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
6423 break;
6424 default:
6425 ALOGE("%s: Unsupported channel mask %#x for Incall Music", __func__,
6426 config->channel_mask);
6427 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
6428 ret = -EINVAL;
6429 goto error_open;
6430 }
6431 switch (config->format) {
6432 case AUDIO_FORMAT_DEFAULT:
6433 case AUDIO_FORMAT_PCM_16_BIT:
6434 out->format = AUDIO_FORMAT_PCM_16_BIT;
6435 break;
6436 default:
6437 ALOGE("%s: Unsupported format %#x for Incall Music", __func__,
6438 config->format);
6439 config->format = AUDIO_FORMAT_PCM_16_BIT;
6440 ret = -EINVAL;
6441 goto error_open;
6442 }
6443
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05306444 ret = voice_extn_check_and_set_incall_music_usecase(adev, out);
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07006445 if (ret != 0) {
6446 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006447 __func__, ret);
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07006448 goto error_open;
6449 }
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006450 } else if (out->devices == AUDIO_DEVICE_OUT_TELEPHONY_TX) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07006451 if (config->sample_rate == 0)
6452 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
6453 if (config->sample_rate != 48000 && config->sample_rate != 16000 &&
6454 config->sample_rate != 8000) {
6455 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
6456 ret = -EINVAL;
6457 goto error_open;
6458 }
6459 out->sample_rate = config->sample_rate;
6460 out->config.rate = config->sample_rate;
6461 if (config->format == AUDIO_FORMAT_DEFAULT)
6462 config->format = AUDIO_FORMAT_PCM_16_BIT;
6463 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
6464 config->format = AUDIO_FORMAT_PCM_16_BIT;
6465 ret = -EINVAL;
6466 goto error_open;
6467 }
6468 out->format = config->format;
6469 out->usecase = USECASE_AUDIO_PLAYBACK_AFE_PROXY;
6470 out->config = pcm_config_afe_proxy_playback;
6471 adev->voice_tx_output = out;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07006472 } else {
Ashish Jain058165c2016-09-28 23:18:48 +05306473 unsigned int channels = 0;
6474 /*Update config params to default if not set by the caller*/
6475 if (config->sample_rate == 0)
6476 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
6477 if (config->channel_mask == AUDIO_CHANNEL_NONE)
6478 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
6479 if (config->format == AUDIO_FORMAT_DEFAULT)
6480 config->format = AUDIO_FORMAT_PCM_16_BIT;
6481
6482 channels = audio_channel_count_from_out_mask(out->channel_mask);
6483
Varun Balaraje49253e2017-07-06 19:48:56 +05306484 if (out->flags & AUDIO_OUTPUT_FLAG_INTERACTIVE) {
6485 out->usecase = get_interactive_usecase(adev);
6486 out->config = pcm_config_low_latency;
6487 } else if (out->flags & AUDIO_OUTPUT_FLAG_RAW) {
Ashish Jain83a6cc22016-06-28 14:34:17 +05306488 out->usecase = USECASE_AUDIO_PLAYBACK_ULL;
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006489 out->realtime = may_use_noirq_mode(adev, USECASE_AUDIO_PLAYBACK_ULL,
6490 out->flags);
6491 out->config = out->realtime ? pcm_config_rt : pcm_config_low_latency;
Haynes Mathew George16081042017-05-31 17:16:49 -07006492 } else if (out->flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) {
6493 out->usecase = USECASE_AUDIO_PLAYBACK_MMAP;
6494 out->config = pcm_config_mmap_playback;
6495 out->stream.start = out_start;
6496 out->stream.stop = out_stop;
6497 out->stream.create_mmap_buffer = out_create_mmap_buffer;
6498 out->stream.get_mmap_position = out_get_mmap_position;
Ashish Jain83a6cc22016-06-28 14:34:17 +05306499 } else if (out->flags & AUDIO_OUTPUT_FLAG_FAST) {
6500 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
Aniket Kumar Lata932f4872017-11-06 18:29:44 -08006501 out->hal_output_suspend_supported =
6502 property_get_bool("vendor.audio.hal.output.suspend.supported", false);
6503 out->dynamic_pm_qos_config_supported =
6504 property_get_bool("vendor.audio.hal.dynamic.qos.config.supported", false);
6505 if (!out->dynamic_pm_qos_config_supported) {
Alexy Joseph98988832017-01-13 14:56:59 -08006506 ALOGI("%s: dynamic qos voting not enabled for platform", __func__);
6507 } else {
6508 ALOGI("%s: dynamic qos voting enabled for platform", __func__);
6509 //the mixer path will be a string similar to "low-latency-playback resume"
6510 strlcpy(out->pm_qos_mixer_path, use_case_table[out->usecase], MAX_MIXER_PATH_LEN);
6511 strlcat(out->pm_qos_mixer_path,
6512 " resume", MAX_MIXER_PATH_LEN);
6513 ALOGI("%s: created %s pm_qos_mixer_path" , __func__,
6514 out->pm_qos_mixer_path);
6515 }
Ashish Jain83a6cc22016-06-28 14:34:17 +05306516 out->config = pcm_config_low_latency;
6517 } else if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
6518 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
6519 out->config = pcm_config_deep_buffer;
Ashish Jain058165c2016-09-28 23:18:48 +05306520 out->config.period_size = get_output_period_size(config->sample_rate, out->format,
6521 channels, DEEP_BUFFER_OUTPUT_PERIOD_DURATION);
6522 if (out->config.period_size <= 0) {
6523 ALOGE("Invalid configuration period size is not valid");
6524 ret = -EINVAL;
6525 goto error_open;
6526 }
Ashish Jain83a6cc22016-06-28 14:34:17 +05306527 } else {
6528 /* primary path is the default path selected if no other outputs are available/suitable */
6529 out->usecase = USECASE_AUDIO_PLAYBACK_PRIMARY;
6530 out->config = PCM_CONFIG_AUDIO_PLAYBACK_PRIMARY;
6531 }
6532 out->hal_ip_format = format = out->format;
6533 out->config.format = hal_format_to_pcm(out->hal_ip_format);
6534 out->hal_op_format = pcm_format_to_hal(out->config.format);
6535 out->bit_width = format_to_bitwidth_table[out->hal_op_format] << 3;
6536 out->config.rate = config->sample_rate;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07006537 out->sample_rate = out->config.rate;
Ashish Jain058165c2016-09-28 23:18:48 +05306538 out->config.channels = channels;
Ashish Jain83a6cc22016-06-28 14:34:17 +05306539 if (out->hal_ip_format != out->hal_op_format) {
6540 uint32_t buffer_size = out->config.period_size *
6541 format_to_bitwidth_table[out->hal_op_format] *
6542 out->config.channels;
6543 out->convert_buffer = calloc(1, buffer_size);
6544 if (out->convert_buffer == NULL){
6545 ALOGE("Allocation failed for convert buffer for size %d",
6546 out->compr_config.fragment_size);
6547 ret = -ENOMEM;
6548 goto error_open;
6549 }
6550 ALOGD("Convert buffer allocated of size %d", buffer_size);
6551 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006552 }
6553
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08006554 ALOGV("%s devices:%d, format:%x, out->sample_rate:%d,out->bit_width:%d out->format:%d out->flags:%x, flags: %x usecase %d",
6555 __func__, devices, format, out->sample_rate, out->bit_width, out->format, out->flags, flags, out->usecase);
Ashish Jain83a6cc22016-06-28 14:34:17 +05306556
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07006557 /* TODO remove this hardcoding and check why width is zero*/
6558 if (out->bit_width == 0)
6559 out->bit_width = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05306560 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07006561 &adev->streams_output_cfg_list,
Aalique Grahame65780b52017-09-27 14:59:56 -07006562 devices, out->flags, out->hal_op_format, out->sample_rate,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +05306563 out->bit_width, out->channel_mask, out->profile,
Manish Dewangan837dc462015-05-27 10:17:41 +05306564 &out->app_type_cfg);
Haynes Mathew Georgebf143712013-12-03 13:02:53 -08006565 if ((out->usecase == USECASE_AUDIO_PLAYBACK_PRIMARY) ||
6566 (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
6567 /* Ensure the default output is not selected twice */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08006568 if(adev->primary_output == NULL)
6569 adev->primary_output = out;
6570 else {
6571 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07006572 ret = -EEXIST;
6573 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08006574 }
6575 }
6576
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006577 /* Check if this usecase is already existing */
6578 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella7ce05352014-04-17 20:00:41 -07006579 if ((get_usecase_from_list(adev, out->usecase) != NULL) &&
6580 (out->usecase != USECASE_COMPRESS_VOIP_CALL)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006581 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006582 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07006583 ret = -EEXIST;
6584 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006585 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08006586
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006587 pthread_mutex_unlock(&adev->lock);
6588
6589 out->stream.common.get_sample_rate = out_get_sample_rate;
6590 out->stream.common.set_sample_rate = out_set_sample_rate;
6591 out->stream.common.get_buffer_size = out_get_buffer_size;
6592 out->stream.common.get_channels = out_get_channels;
6593 out->stream.common.get_format = out_get_format;
6594 out->stream.common.set_format = out_set_format;
6595 out->stream.common.standby = out_standby;
6596 out->stream.common.dump = out_dump;
6597 out->stream.common.set_parameters = out_set_parameters;
6598 out->stream.common.get_parameters = out_get_parameters;
6599 out->stream.common.add_audio_effect = out_add_audio_effect;
6600 out->stream.common.remove_audio_effect = out_remove_audio_effect;
6601 out->stream.get_latency = out_get_latency;
6602 out->stream.set_volume = out_set_volume;
6603 out->stream.write = out_write;
6604 out->stream.get_render_position = out_get_render_position;
6605 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07006606 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006607
Haynes Mathew George16081042017-05-31 17:16:49 -07006608 if (out->realtime)
6609 out->af_period_multiplier = af_period_multiplier;
6610 else
6611 out->af_period_multiplier = 1;
6612
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006613 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07006614 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07006615 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006616
6617 config->format = out->stream.common.get_format(&out->stream.common);
6618 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
6619 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
Naresh Tanniru04f71882018-06-26 17:46:22 +05306620 register_format(out->format, out->supported_formats);
6621 register_channel_mask(out->channel_mask, out->supported_channel_masks);
6622 register_sample_rate(out->sample_rate, out->supported_sample_rates);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006623
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306624 /*
6625 By locking output stream before registering, we allow the callback
6626 to update stream's state only after stream's initial state is set to
6627 adev state.
6628 */
6629 lock_output_stream(out);
6630 audio_extn_snd_mon_register_listener(out, out_snd_mon_cb);
6631 pthread_mutex_lock(&adev->lock);
6632 out->card_status = adev->card_status;
6633 pthread_mutex_unlock(&adev->lock);
6634 pthread_mutex_unlock(&out->lock);
6635
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006636 *stream_out = &out->stream;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05306637 ALOGD("%s: Stream (%p) picks up usecase (%s)", __func__, &out->stream,
vivek mehta0ea887a2015-08-26 14:01:20 -07006638 use_case_table[out->usecase]);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08006639
6640 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
6641 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
6642 popcount(out->channel_mask), out->playback_started);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08006643 /* setup a channel for client <--> adsp communication for stream events */
Manish Dewangan21a850a2017-08-14 12:03:55 +05306644 is_direct_passthough = audio_extn_passthru_is_direct_passthrough(out);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08006645 if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
Naresh Tanniru85819452017-05-04 18:55:45 -07006646 (out->flags & AUDIO_OUTPUT_FLAG_DIRECT_PCM) ||
Manish Dewangan21a850a2017-08-14 12:03:55 +05306647 (audio_extn_ip_hdlr_intf_supported(config->format, is_direct_passthough, false))) {
Ben Rombergerd771a7c2017-02-22 18:05:17 -08006648 hdlr_stream_cfg.pcm_device_id = platform_get_pcm_device_id(
6649 out->usecase, PCM_PLAYBACK);
6650 hdlr_stream_cfg.flags = out->flags;
6651 hdlr_stream_cfg.type = PCM_PLAYBACK;
6652 ret = audio_extn_adsp_hdlr_stream_open(&out->adsp_hdlr_stream_handle,
6653 &hdlr_stream_cfg);
6654 if (ret) {
6655 ALOGE("%s: adsp_hdlr_stream_open failed %d",__func__, ret);
6656 out->adsp_hdlr_stream_handle = NULL;
6657 }
6658 }
Manish Dewangan21a850a2017-08-14 12:03:55 +05306659 if (audio_extn_ip_hdlr_intf_supported(config->format, is_direct_passthough, false)) {
Vidyakumar Athota2062f912017-06-27 14:46:15 -07006660 ret = audio_extn_ip_hdlr_intf_init(&out->ip_hdlr_handle, NULL, NULL, adev, out->usecase);
Naresh Tanniru85819452017-05-04 18:55:45 -07006661 if (ret < 0) {
6662 ALOGE("%s: audio_extn_ip_hdlr_intf_init failed %d",__func__, ret);
6663 out->ip_hdlr_handle = NULL;
6664 }
6665 }
Eric Laurent994a6932013-07-17 11:51:42 -07006666 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006667 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07006668
6669error_open:
Ashish Jain83a6cc22016-06-28 14:34:17 +05306670 if (out->convert_buffer)
6671 free(out->convert_buffer);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07006672 free(out);
6673 *stream_out = NULL;
6674 ALOGD("%s: exit: ret %d", __func__, ret);
6675 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006676}
6677
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05306678void adev_close_output_stream(struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006679 struct audio_stream_out *stream)
6680{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006681 struct stream_out *out = (struct stream_out *)stream;
6682 struct audio_device *adev = out->dev;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08006683 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006684
Haynes Mathew George484e8d22017-07-31 18:55:17 -07006685 ALOGD("%s: enter:stream_handle(%s)",__func__, use_case_table[out->usecase]);
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05306686
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306687 // must deregister from sndmonitor first to prevent races
6688 // between the callback and close_stream
6689 audio_extn_snd_mon_unregister_listener(out);
6690
Ben Rombergerd771a7c2017-02-22 18:05:17 -08006691 /* close adsp hdrl session before standby */
6692 if (out->adsp_hdlr_stream_handle) {
6693 ret = audio_extn_adsp_hdlr_stream_close(out->adsp_hdlr_stream_handle);
6694 if (ret)
6695 ALOGE("%s: adsp_hdlr_stream_close failed %d",__func__, ret);
6696 out->adsp_hdlr_stream_handle = NULL;
6697 }
6698
Manish Dewangan21a850a2017-08-14 12:03:55 +05306699 if (out->ip_hdlr_handle) {
Naresh Tanniru85819452017-05-04 18:55:45 -07006700 audio_extn_ip_hdlr_intf_deinit(out->ip_hdlr_handle);
6701 out->ip_hdlr_handle = NULL;
6702 }
6703
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08006704 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05306705 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08006706 ret = voice_extn_compress_voip_close_output_stream(&stream->common);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05306707 out->started = 0;
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05306708 pthread_mutex_unlock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08006709 if(ret != 0)
6710 ALOGE("%s: Compress voip output cannot be closed, error:%d",
6711 __func__, ret);
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07006712 } else
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08006713 out_standby(&stream->common);
6714
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07006715 if (is_offload_usecase(out->usecase)) {
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08006716 audio_extn_dts_remove_state_notifier_node(out->usecase);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006717 destroy_offload_callback_thread(out);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07006718 free_offload_usecase(adev, out->usecase);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006719 if (out->compr_config.codec != NULL)
6720 free(out->compr_config.codec);
6721 }
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07006722
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05306723 out->a2dp_compress_mute = false;
6724
Varun Balaraje49253e2017-07-06 19:48:56 +05306725 if (is_interactive_usecase(out->usecase))
6726 free_interactive_usecase(adev, out->usecase);
6727
Ashish Jain83a6cc22016-06-28 14:34:17 +05306728 if (out->convert_buffer != NULL) {
6729 free(out->convert_buffer);
6730 out->convert_buffer = NULL;
6731 }
6732
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07006733 if (adev->voice_tx_output == out)
6734 adev->voice_tx_output = NULL;
6735
Dhanalakshmi Siddani6c3d0992017-01-16 16:52:33 +05306736 if (adev->primary_output == out)
6737 adev->primary_output = NULL;
6738
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006739 pthread_cond_destroy(&out->cond);
6740 pthread_mutex_destroy(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006741 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07006742 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006743}
6744
6745static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
6746{
6747 struct audio_device *adev = (struct audio_device *)dev;
6748 struct str_parms *parms;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006749 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07006750 int val;
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07006751 int ret;
6752 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006753
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08006754 ALOGD("%s: enter: %s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006755 parms = str_parms_create_str(kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006756
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05306757 if (!parms)
6758 goto error;
Naresh Tanniru4c630392014-05-12 01:05:52 +05306759
Ashish Jain1b9b30c2017-05-18 20:57:40 +05306760 ret = str_parms_get_str(parms, "BT_SCO", value, sizeof(value));
6761 if (ret >= 0) {
6762 /* When set to false, HAL should disable EC and NS */
6763 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
6764 adev->bt_sco_on = true;
6765 else
6766 adev->bt_sco_on = false;
6767 }
6768
Naresh Tanniru4c630392014-05-12 01:05:52 +05306769 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07006770 status = voice_set_parameters(adev, parms);
6771 if (status != 0)
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08006772 goto done;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006773
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07006774 status = platform_set_parameters(adev->platform, parms);
6775 if (status != 0)
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08006776 goto done;
6777
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07006778 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
6779 if (ret >= 0) {
Vicky Sehrawate240e5d2014-08-12 17:17:04 -07006780 /* When set to false, HAL should disable EC and NS */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006781 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
6782 adev->bluetooth_nrec = true;
6783 else
6784 adev->bluetooth_nrec = false;
6785 }
6786
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07006787 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
6788 if (ret >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006789 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
6790 adev->screen_off = false;
6791 else
6792 adev->screen_off = true;
6793 }
6794
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07006795 ret = str_parms_get_int(parms, "rotation", &val);
6796 if (ret >= 0) {
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07006797 bool reverse_speakers = false;
6798 switch(val) {
6799 // FIXME: note that the code below assumes that the speakers are in the correct placement
6800 // relative to the user when the device is rotated 90deg from its default rotation. This
6801 // assumption is device-specific, not platform-specific like this code.
6802 case 270:
6803 reverse_speakers = true;
6804 break;
6805 case 0:
6806 case 90:
6807 case 180:
6808 break;
6809 default:
6810 ALOGE("%s: unexpected rotation of %d", __func__, val);
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07006811 status = -EINVAL;
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07006812 }
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07006813 if (status == 0) {
Vignesh Kulothungan3b5fae52017-09-25 12:16:30 -07006814 // check and set swap
6815 // - check if orientation changed and speaker active
6816 // - set rotation and cache the rotation value
6817 platform_check_and_set_swap_lr_channels(adev, reverse_speakers);
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07006818 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07006819 }
6820
Mingming Yin514a8bc2014-07-29 15:22:21 -07006821 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_SCO_WB, value, sizeof(value));
6822 if (ret >= 0) {
6823 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
6824 adev->bt_wb_speech_enabled = true;
6825 else
6826 adev->bt_wb_speech_enabled = false;
6827 }
6828
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -07006829 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value, sizeof(value));
6830 if (ret >= 0) {
6831 val = atoi(value);
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05306832 audio_devices_t device = (audio_devices_t) val;
Zhou Song681350a2017-10-19 16:28:42 +08006833 if (audio_is_output_device(val) &&
6834 (val & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07006835 ALOGV("cache new ext disp type and edid");
6836 ret = platform_get_ext_disp_type(adev->platform);
6837 if (ret < 0) {
6838 ALOGE("%s: Failed to query disp type, ret:%d", __func__, ret);
Manisha Agarwal2f5ff882018-08-08 17:09:29 +05306839 } else {
6840 platform_cache_edid(adev->platform);
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07006841 }
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05306842 } else if (audio_is_usb_out_device(device) || audio_is_usb_in_device(device)) {
vivek mehta344576a2016-04-12 18:56:03 -07006843 /*
6844 * Do not allow AFE proxy port usage by WFD source when USB headset is connected.
6845 * Per AudioPolicyManager, USB device is higher priority than WFD.
6846 * For Voice call over USB headset, voice call audio is routed to AFE proxy ports.
6847 * If WFD use case occupies AFE proxy, it may result unintended behavior while
6848 * starting voice call on USB
6849 */
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08006850 ret = str_parms_get_str(parms, "card", value, sizeof(value));
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05306851 if (ret >= 0)
6852 audio_extn_usb_add_device(device, atoi(value));
6853
Zhou Song6f862822017-11-06 17:27:57 +08006854 if (!audio_extn_usb_is_tunnel_supported()) {
6855 ALOGV("detected USB connect .. disable proxy");
6856 adev->allow_afe_proxy_usage = false;
6857 }
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -07006858 }
6859 }
6860
6861 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value, sizeof(value));
6862 if (ret >= 0) {
6863 val = atoi(value);
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05306864 audio_devices_t device = (audio_devices_t) val;
Garmond Leunge3b6d482016-10-25 16:48:01 -07006865 /*
6866 * The HDMI / Displayport disconnect handling has been moved to
6867 * audio extension to ensure that its parameters are not
6868 * invalidated prior to updating sysfs of the disconnect event
6869 * Invalidate will be handled by audio_extn_ext_disp_set_parameters()
6870 */
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05306871 if (audio_is_usb_out_device(device) || audio_is_usb_in_device(device)) {
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08006872 ret = str_parms_get_str(parms, "card", value, sizeof(value));
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05306873 if (ret >= 0)
6874 audio_extn_usb_remove_device(device, atoi(value));
6875
Zhou Song6f862822017-11-06 17:27:57 +08006876 if (!audio_extn_usb_is_tunnel_supported()) {
6877 ALOGV("detected USB disconnect .. enable proxy");
6878 adev->allow_afe_proxy_usage = true;
6879 }
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -07006880 }
6881 }
6882
Naresh Tanniru9d027a62015-03-13 01:32:10 +05306883 ret = str_parms_get_str(parms,"reconfigA2dp", value, sizeof(value));
6884 if (ret >= 0) {
6885 struct audio_usecase *usecase;
6886 struct listnode *node;
6887 list_for_each(node, &adev->usecase_list) {
6888 usecase = node_to_item(node, struct audio_usecase, list);
6889 if ((usecase->type == PCM_PLAYBACK) &&
Naresh Tanniruf7e9e632016-11-04 14:54:20 -07006890 (usecase->devices & AUDIO_DEVICE_OUT_ALL_A2DP)){
Naresh Tanniru9d027a62015-03-13 01:32:10 +05306891 ALOGD("reconfigure a2dp... forcing device switch");
Weiyin Jiang425180d2017-06-05 16:40:23 +08006892
6893 pthread_mutex_unlock(&adev->lock);
Naresh Tannirucd2353e2016-08-19 00:37:25 +05306894 lock_output_stream(usecase->stream.out);
Weiyin Jiang425180d2017-06-05 16:40:23 +08006895 pthread_mutex_lock(&adev->lock);
Naresh Tannirucd2353e2016-08-19 00:37:25 +05306896 audio_extn_a2dp_set_handoff_mode(true);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05306897 //force device switch to re configure encoder
6898 select_devices(adev, usecase->id);
Naresh Tannirucd2353e2016-08-19 00:37:25 +05306899 audio_extn_a2dp_set_handoff_mode(false);
6900 pthread_mutex_unlock(&usecase->stream.out->lock);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05306901 break;
6902 }
6903 }
6904 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08006905
6906 //handle vr audio setparam
6907 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_VR_AUDIO_MODE,
6908 value, sizeof(value));
6909 if (ret >= 0) {
6910 ALOGI("Setting vr mode to be %s", value);
6911 if (!strncmp(value, "true", 4)) {
6912 adev->vr_audio_mode_enabled = true;
6913 ALOGI("Setting vr mode to true");
6914 } else if (!strncmp(value, "false", 5)) {
6915 adev->vr_audio_mode_enabled = false;
6916 ALOGI("Setting vr mode to false");
6917 } else {
6918 ALOGI("wrong vr mode set");
6919 }
6920 }
6921
Naresh Tannirucd2353e2016-08-19 00:37:25 +05306922 audio_extn_set_parameters(adev, parms);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08006923done:
6924 str_parms_destroy(parms);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08006925 pthread_mutex_unlock(&adev->lock);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05306926error:
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07006927 ALOGV("%s: exit with code(%d)", __func__, status);
6928 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006929}
6930
6931static char* adev_get_parameters(const struct audio_hw_device *dev,
6932 const char *keys)
6933{
Sidipotu Ashok090f0bc2018-12-21 09:19:26 +05306934 ALOGD("%s:%s", __func__, keys);
6935
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07006936 struct audio_device *adev = (struct audio_device *)dev;
6937 struct str_parms *reply = str_parms_create();
6938 struct str_parms *query = str_parms_create_str(keys);
Sidipotu Ashok090f0bc2018-12-21 09:19:26 +05306939
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07006940 char *str;
Naresh Tannirud7205b62014-06-20 02:54:48 +05306941 char value[256] = {0};
6942 int ret = 0;
6943
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07006944 if (!query || !reply) {
Alexy Josephaee4fdd2016-01-29 13:02:07 -08006945 if (reply) {
6946 str_parms_destroy(reply);
6947 }
6948 if (query) {
6949 str_parms_destroy(query);
6950 }
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07006951 ALOGE("adev_get_parameters: failed to create query or reply");
6952 return NULL;
6953 }
6954
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08006955 //handle vr audio getparam
6956
6957 ret = str_parms_get_str(query,
6958 AUDIO_PARAMETER_KEY_VR_AUDIO_MODE,
6959 value, sizeof(value));
6960
6961 if (ret >= 0) {
6962 bool vr_audio_enabled = false;
6963 pthread_mutex_lock(&adev->lock);
6964 vr_audio_enabled = adev->vr_audio_mode_enabled;
6965 pthread_mutex_unlock(&adev->lock);
6966
6967 ALOGI("getting vr mode to %d", vr_audio_enabled);
6968
6969 if (vr_audio_enabled) {
6970 str_parms_add_str(reply, AUDIO_PARAMETER_KEY_VR_AUDIO_MODE,
6971 "true");
6972 goto exit;
6973 } else {
6974 str_parms_add_str(reply, AUDIO_PARAMETER_KEY_VR_AUDIO_MODE,
6975 "false");
6976 goto exit;
6977 }
6978 }
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07006979
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08006980 pthread_mutex_lock(&adev->lock);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07006981 audio_extn_get_parameters(adev, query, reply);
Shiv Maliyappanahallif9308492013-12-12 12:18:09 -08006982 voice_get_parameters(adev, query, reply);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07006983 platform_get_parameters(adev->platform, query, reply);
Naresh Tanniru80659832014-06-04 18:17:56 +05306984 pthread_mutex_unlock(&adev->lock);
6985
Naresh Tannirud7205b62014-06-20 02:54:48 +05306986exit:
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07006987 str = str_parms_to_str(reply);
6988 str_parms_destroy(query);
6989 str_parms_destroy(reply);
6990
Sidipotu Ashok090f0bc2018-12-21 09:19:26 +05306991 ALOGD("%s: exit: returns - %s", __func__, str);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07006992 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006993}
6994
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07006995static int adev_init_check(const struct audio_hw_device *dev __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006996{
6997 return 0;
6998}
6999
7000static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
7001{
Haynes Mathew George5191a852013-09-11 14:19:36 -07007002 int ret;
7003 struct audio_device *adev = (struct audio_device *)dev;
7004 pthread_mutex_lock(&adev->lock);
7005 /* cache volume */
Shruthi Krishnaace10852013-10-25 14:32:12 -07007006 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07007007 pthread_mutex_unlock(&adev->lock);
7008 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007009}
7010
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07007011static int adev_set_master_volume(struct audio_hw_device *dev __unused,
7012 float volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007013{
7014 return -ENOSYS;
7015}
7016
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07007017static int adev_get_master_volume(struct audio_hw_device *dev __unused,
7018 float *volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007019{
7020 return -ENOSYS;
7021}
7022
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07007023static int adev_set_master_mute(struct audio_hw_device *dev __unused,
7024 bool muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007025{
7026 return -ENOSYS;
7027}
7028
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07007029static int adev_get_master_mute(struct audio_hw_device *dev __unused,
7030 bool *muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007031{
7032 return -ENOSYS;
7033}
7034
7035static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
7036{
7037 struct audio_device *adev = (struct audio_device *)dev;
Garmond Leung5fd0b552018-04-17 11:56:12 -07007038 struct listnode *node;
7039 struct audio_usecase *usecase = NULL;
7040 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007041 pthread_mutex_lock(&adev->lock);
7042 if (adev->mode != mode) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07007043 ALOGD("%s: mode %d\n", __func__, mode);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007044 adev->mode = mode;
Shiv Maliyappanahallibb4cf0b2016-01-21 11:30:06 -08007045 if ((mode == AUDIO_MODE_NORMAL) && voice_is_in_call(adev)) {
Garmond Leung5fd0b552018-04-17 11:56:12 -07007046 list_for_each(node, &adev->usecase_list) {
7047 usecase = node_to_item(node, struct audio_usecase, list);
7048 if (usecase->type == VOICE_CALL)
7049 break;
7050 }
7051 if (usecase &&
7052 audio_is_usb_out_device(usecase->out_snd_device & AUDIO_DEVICE_OUT_ALL_USB)) {
7053 ret = audio_extn_usb_check_and_set_svc_int(usecase,
7054 true);
7055 if (ret != 0) {
7056 /* default service interval was successfully updated,
7057 reopen USB backend with new service interval */
7058 check_usecases_codec_backend(adev,
7059 usecase,
7060 usecase->out_snd_device);
7061 }
7062 }
7063
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07007064 voice_stop_call(adev);
Banajit Goswami20cdd212015-09-11 01:11:30 -07007065 platform_set_gsm_mode(adev->platform, false);
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07007066 adev->current_call_output = NULL;
7067 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007068 }
7069 pthread_mutex_unlock(&adev->lock);
7070 return 0;
7071}
7072
7073static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
7074{
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08007075 int ret;
7076
7077 pthread_mutex_lock(&adev->lock);
Vidyakumar Athota2850d532013-11-19 16:02:12 -08007078 ALOGD("%s state %d\n", __func__, state);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08007079 ret = voice_set_mic_mute((struct audio_device *)dev, state);
Derek Chencdd17c72014-11-24 12:39:14 -08007080 if (adev->ext_hw_plugin)
7081 ret = audio_extn_ext_hw_plugin_set_mic_mute(adev->ext_hw_plugin, state);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08007082 pthread_mutex_unlock(&adev->lock);
7083
7084 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007085}
7086
7087static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
7088{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07007089 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007090 return 0;
7091}
7092
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07007093static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007094 const struct audio_config *config)
7095{
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07007096 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007097
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07007098 return get_input_buffer_size(config->sample_rate, config->format, channel_count,
7099 false /* is_low_latency: since we don't know, be conservative */);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007100}
7101
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007102static bool adev_input_allow_hifi_record(struct audio_device *adev,
7103 audio_devices_t devices,
7104 audio_input_flags_t flags,
7105 audio_source_t source) {
7106 const bool allowed = true;
7107
7108 if (!audio_is_usb_in_device(devices))
7109 return !allowed;
7110
7111 switch (flags) {
7112 case AUDIO_INPUT_FLAG_NONE:
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007113 break;
Haynes Mathew George59862182017-10-24 16:23:57 -07007114 case AUDIO_INPUT_FLAG_FAST: // disallow hifi record for FAST as
7115 // it affects RTD numbers over USB
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007116 default:
7117 return !allowed;
7118 }
7119
7120 switch (source) {
7121 case AUDIO_SOURCE_DEFAULT:
7122 case AUDIO_SOURCE_MIC:
7123 case AUDIO_SOURCE_UNPROCESSED:
7124 break;
7125 default:
7126 return !allowed;
7127 }
7128
7129 switch (adev->mode) {
7130 case 0:
7131 break;
7132 default:
7133 return !allowed;
7134 }
7135
7136 return allowed;
7137}
7138
Haynes Mathew George4ffef292017-11-21 15:08:02 -08007139static int adev_update_voice_comm_input_stream(struct stream_in *in,
7140 struct audio_config *config)
7141{
7142 bool valid_rate = (config->sample_rate == 8000 ||
7143 config->sample_rate == 16000 ||
7144 config->sample_rate == 32000 ||
7145 config->sample_rate == 48000);
7146 bool valid_ch = audio_channel_count_from_in_mask(in->channel_mask) == 1;
7147
7148#ifndef COMPRESS_VOIP_ENABLED
kunleizd96526c2018-04-09 11:12:32 +08007149 if (valid_rate && valid_ch &&
7150 in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) {
Haynes Mathew George4ffef292017-11-21 15:08:02 -08007151 in->usecase = USECASE_AUDIO_RECORD_VOIP;
7152 in->config = default_pcm_config_voip_copp;
7153 in->config.period_size = VOIP_IO_BUF_SIZE(in->sample_rate,
7154 DEFAULT_VOIP_BUF_DURATION_MS,
7155 DEFAULT_VOIP_BIT_DEPTH_BYTE)/2;
7156 } else {
kunleizbdd9ad02018-04-24 10:53:17 +08007157 ALOGW("%s No valid input in voip, use defaults"
7158 "sample rate %u, channel mask 0x%X",
7159 __func__, config->sample_rate, in->channel_mask);
Haynes Mathew George4ffef292017-11-21 15:08:02 -08007160 }
7161 in->config.rate = config->sample_rate;
7162 in->sample_rate = config->sample_rate;
7163#else
7164 //XXX needed for voice_extn_compress_voip_open_input_stream
7165 in->config.rate = config->sample_rate;
7166 if ((in->dev->mode == AUDIO_MODE_IN_COMMUNICATION ||
7167 voice_extn_compress_voip_is_active(in->dev)) &&
7168 (voice_extn_compress_voip_is_format_supported(in->format)) &&
7169 valid_rate && valid_ch) {
7170 voice_extn_compress_voip_open_input_stream(in);
7171 // update rate entries to match config from AF
7172 in->config.rate = config->sample_rate;
7173 in->sample_rate = config->sample_rate;
7174 } else {
7175 ALOGW("%s compress voip not active, use defaults", __func__);
7176 }
7177#endif
7178 return 0;
7179}
7180
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007181static int adev_open_input_stream(struct audio_hw_device *dev,
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07007182 audio_io_handle_t handle,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007183 audio_devices_t devices,
7184 struct audio_config *config,
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07007185 struct audio_stream_in **stream_in,
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307186 audio_input_flags_t flags,
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07007187 const char *address __unused,
Vidyakumar Athota5c398212015-03-31 21:53:21 -07007188 audio_source_t source)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007189{
7190 struct audio_device *adev = (struct audio_device *)dev;
7191 struct stream_in *in;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08007192 int ret = 0, buffer_size, frame_size;
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07007193 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07007194 bool is_low_latency = false;
Divya Narayanan Poojary45f19192016-09-30 18:52:13 +05307195 bool channel_mask_updated = false;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007196 bool is_usb_dev = audio_is_usb_in_device(devices);
7197 bool may_use_hifi_record = adev_input_allow_hifi_record(adev,
7198 devices,
7199 flags,
7200 source);
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05307201
kunleizdff872d2018-08-20 14:40:33 +08007202 if (is_usb_dev && (!audio_extn_usb_connected(NULL))) {
kunleizd6a9e0c2018-07-30 15:38:52 +08007203 is_usb_dev = false;
7204 devices = AUDIO_DEVICE_IN_BUILTIN_MIC;
7205 ALOGW("%s: ignore set device to non existing USB card, use input device(%#x)",
7206 __func__, devices);
7207 }
7208
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007209 *stream_in = NULL;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007210
7211 if (!(is_usb_dev && may_use_hifi_record)) {
7212 if (config->sample_rate == 0)
7213 config->sample_rate = 48000;
7214 if (config->channel_mask == AUDIO_CHANNEL_NONE)
7215 config->channel_mask = AUDIO_CHANNEL_IN_MONO;
7216 if (config->format == AUDIO_FORMAT_DEFAULT)
7217 config->format = AUDIO_FORMAT_PCM_16_BIT;
7218
7219 channel_count = audio_channel_count_from_in_mask(config->channel_mask);
7220
7221 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
7222 return -EINVAL;
Chaithanya Krishna Bacharaju9955b162016-05-25 16:25:53 +05307223 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007224
7225 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07007226
7227 if (!in) {
7228 ALOGE("failed to allocate input stream");
7229 return -ENOMEM;
7230 }
7231
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05307232 ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x)\
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05307233 stream_handle(%p) io_handle(%d) source(%d) format %x",__func__, config->sample_rate,
7234 config->channel_mask, devices, &in->stream, handle, source, config->format);
Ravi Kumar Alamanda40703102014-04-24 10:34:41 -07007235 pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07007236 pthread_mutex_init(&in->pre_lock, (const pthread_mutexattr_t *) NULL);
Ravi Kumar Alamanda40703102014-04-24 10:34:41 -07007237
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007238 in->stream.common.get_sample_rate = in_get_sample_rate;
7239 in->stream.common.set_sample_rate = in_set_sample_rate;
7240 in->stream.common.get_buffer_size = in_get_buffer_size;
7241 in->stream.common.get_channels = in_get_channels;
7242 in->stream.common.get_format = in_get_format;
7243 in->stream.common.set_format = in_set_format;
7244 in->stream.common.standby = in_standby;
7245 in->stream.common.dump = in_dump;
7246 in->stream.common.set_parameters = in_set_parameters;
7247 in->stream.common.get_parameters = in_get_parameters;
7248 in->stream.common.add_audio_effect = in_add_audio_effect;
7249 in->stream.common.remove_audio_effect = in_remove_audio_effect;
7250 in->stream.set_gain = in_set_gain;
7251 in->stream.read = in_read;
7252 in->stream.get_input_frames_lost = in_get_input_frames_lost;
Naresh Tannirudcb47c52018-06-25 16:23:32 +05307253 in->stream.get_active_microphones = in_get_active_microphones;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007254
7255 in->device = devices;
Vidyakumar Athota5c398212015-03-31 21:53:21 -07007256 in->source = source;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007257 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007258 in->standby = 1;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07007259 in->capture_handle = handle;
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07007260 in->flags = flags;
Haynes Mathew George46740472017-10-27 18:40:12 -07007261 in->bit_width = 16;
7262 in->af_period_multiplier = 1;
7263
7264 /* Update config params with the requested sample rate and channels */
7265 if ((in->device == AUDIO_DEVICE_IN_TELEPHONY_RX) &&
7266 (adev->mode != AUDIO_MODE_IN_CALL)) {
7267 ret = -EINVAL;
7268 goto err_open;
7269 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007270
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007271 if (is_usb_dev && may_use_hifi_record) {
7272 /* HiFi record selects an appropriate format, channel, rate combo
7273 depending on sink capabilities*/
7274 ret = read_usb_sup_params_and_compare(false /*is_playback*/,
7275 &config->format,
7276 &in->supported_formats[0],
7277 MAX_SUPPORTED_FORMATS,
7278 &config->channel_mask,
7279 &in->supported_channel_masks[0],
7280 MAX_SUPPORTED_CHANNEL_MASKS,
7281 &config->sample_rate,
7282 &in->supported_sample_rates[0],
7283 MAX_SUPPORTED_SAMPLE_RATES);
7284 if (ret != 0) {
7285 ret = -EINVAL;
7286 goto err_open;
7287 }
7288 channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Haynes Mathew George4ffef292017-11-21 15:08:02 -08007289 } else if (config->format == AUDIO_FORMAT_DEFAULT) {
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307290 config->format = AUDIO_FORMAT_PCM_16_BIT;
Surendar karkaaca3d082017-11-09 15:18:37 +05307291 } else if (property_get_bool("vendor.audio.capture.pcm.32bit.enable", false)
7292 && config->format == AUDIO_FORMAT_PCM_32_BIT) {
7293 in->config.format = PCM_FORMAT_S32_LE;
7294 in->bit_width = 32;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307295 } else if ((config->format == AUDIO_FORMAT_PCM_FLOAT) ||
7296 (config->format == AUDIO_FORMAT_PCM_32_BIT) ||
7297 (config->format == AUDIO_FORMAT_PCM_24_BIT_PACKED) ||
7298 (config->format == AUDIO_FORMAT_PCM_8_24_BIT)) {
7299 bool ret_error = false;
7300 in->bit_width = 24;
7301 /* 24 bit is restricted to UNPROCESSED source only,also format supported
7302 from HAL is 24_packed and 8_24
7303 *> In case of UNPROCESSED source, for 24 bit, if format requested is other than
7304 24_packed return error indicating supported format is 24_packed
7305 *> In case of any other source requesting 24 bit or float return error
7306 indicating format supported is 16 bit only.
7307
7308 on error flinger will retry with supported format passed
7309 */
7310 if ((source != AUDIO_SOURCE_UNPROCESSED) &&
7311 (source != AUDIO_SOURCE_CAMCORDER)) {
7312 config->format = AUDIO_FORMAT_PCM_16_BIT;
7313 if (config->sample_rate > 48000)
7314 config->sample_rate = 48000;
7315 ret_error = true;
Haynes Mathew George46740472017-10-27 18:40:12 -07007316 } else if (!(config->format == AUDIO_FORMAT_PCM_24_BIT_PACKED ||
7317 config->format == AUDIO_FORMAT_PCM_8_24_BIT)) {
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307318 config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
7319 ret_error = true;
7320 }
7321
7322 if (ret_error) {
7323 ret = -EINVAL;
7324 goto err_open;
7325 }
7326 }
7327
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007328 in->channel_mask = config->channel_mask;
7329 in->format = config->format;
7330
7331 in->usecase = USECASE_AUDIO_RECORD;
7332 if (config->sample_rate == LOW_LATENCY_CAPTURE_SAMPLE_RATE &&
7333 (flags & AUDIO_INPUT_FLAG_FAST) != 0) {
7334 is_low_latency = true;
7335#if LOW_LATENCY_CAPTURE_USE_CASE
7336 in->usecase = USECASE_AUDIO_RECORD_LOW_LATENCY;
7337#endif
7338 in->realtime = may_use_noirq_mode(adev, in->usecase, in->flags);
7339 }
7340
7341 if ((config->sample_rate == LOW_LATENCY_CAPTURE_SAMPLE_RATE) &&
7342 ((in->flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0)) {
7343 in->realtime = 0;
7344 in->usecase = USECASE_AUDIO_RECORD_MMAP;
7345 in->config = pcm_config_mmap_capture;
Haynes Mathew George46740472017-10-27 18:40:12 -07007346 in->config.format = pcm_format_from_audio_format(config->format);
Weiyin Jiangde7925e2018-11-27 18:45:53 +08007347 in->config.channels = channel_count;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007348 in->stream.start = in_start;
7349 in->stream.stop = in_stop;
7350 in->stream.create_mmap_buffer = in_create_mmap_buffer;
7351 in->stream.get_mmap_position = in_get_mmap_position;
Haynes Mathew Georgef29116e2018-01-31 12:48:20 -08007352 in->sample_rate = in->config.rate;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007353 ALOGV("%s: USECASE_AUDIO_RECORD_MMAP", __func__);
7354 } else if (in->realtime) {
7355 in->config = pcm_config_audio_capture_rt;
Haynes Mathew George46740472017-10-27 18:40:12 -07007356 in->config.format = pcm_format_from_audio_format(config->format);
Haynes Mathew George4ffef292017-11-21 15:08:02 -08007357 in->config.channels = channel_count;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007358 in->sample_rate = in->config.rate;
7359 in->af_period_multiplier = af_period_multiplier;
Haynes Mathew George46740472017-10-27 18:40:12 -07007360 } else if (is_usb_dev && may_use_hifi_record) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007361 in->usecase = USECASE_AUDIO_RECORD_HIFI;
7362 in->config = pcm_config_audio_capture;
7363 frame_size = audio_stream_in_frame_size(&in->stream);
7364 buffer_size = get_input_buffer_size(config->sample_rate,
7365 config->format,
7366 channel_count,
7367 false /*is_low_latency*/);
7368 in->config.period_size = buffer_size / frame_size;
7369 in->config.rate = config->sample_rate;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007370 in->config.format = pcm_format_from_audio_format(config->format);
Karthikeyan Mani07faa602018-08-20 11:01:32 -07007371 switch (config->format) {
7372 case AUDIO_FORMAT_PCM_32_BIT:
7373 in->bit_width = 32;
7374 break;
7375 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
7376 case AUDIO_FORMAT_PCM_8_24_BIT:
7377 in->bit_width = 24;
7378 break;
7379 default:
7380 in->bit_width = 16;
7381 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007382 in->config.channels = channel_count;
Haynes Mathew Georgef29116e2018-01-31 12:48:20 -08007383 in->sample_rate = in->config.rate;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007384 } else if ((in->device == AUDIO_DEVICE_IN_TELEPHONY_RX) ||
Dhanalakshmi Siddani0b1488e2016-09-06 12:58:42 +05307385 (in->device == AUDIO_DEVICE_IN_PROXY)) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07007386 if (config->sample_rate == 0)
7387 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
7388 if (config->sample_rate != 48000 && config->sample_rate != 16000 &&
7389 config->sample_rate != 8000) {
7390 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
7391 ret = -EINVAL;
7392 goto err_open;
7393 }
7394 if (config->format == AUDIO_FORMAT_DEFAULT)
7395 config->format = AUDIO_FORMAT_PCM_16_BIT;
7396 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
7397 config->format = AUDIO_FORMAT_PCM_16_BIT;
7398 ret = -EINVAL;
7399 goto err_open;
7400 }
7401
7402 in->usecase = USECASE_AUDIO_RECORD_AFE_PROXY;
7403 in->config = pcm_config_afe_proxy_record;
7404 in->config.channels = channel_count;
7405 in->config.rate = config->sample_rate;
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05307406 in->sample_rate = config->sample_rate;
Haynes Mathew George46740472017-10-27 18:40:12 -07007407 } else {
Revathi Uddarajud2634032017-12-07 14:42:34 +05307408 int ret_val;
7409 pthread_mutex_lock(&adev->lock);
7410 ret_val = audio_extn_check_and_set_multichannel_usecase(adev,
7411 in, config, &channel_mask_updated);
7412 pthread_mutex_unlock(&adev->lock);
7413
7414 if (!ret_val) {
7415 if (channel_mask_updated == true) {
7416 ALOGD("%s: return error to retry with updated channel mask (%#x)",
7417 __func__, config->channel_mask);
7418 ret = -EINVAL;
7419 goto err_open;
7420 }
7421 ALOGD("%s: created multi-channel session succesfully",__func__);
7422 } else if (audio_extn_compr_cap_enabled() &&
7423 audio_extn_compr_cap_format_supported(config->format) &&
7424 (in->dev->mode != AUDIO_MODE_IN_COMMUNICATION)) {
7425 audio_extn_compr_cap_init(in);
7426 } else if (audio_extn_cin_applicable_stream(in)) {
Sachin Mohan Gadag16273f72017-12-19 18:10:48 +05307427 in->sample_rate = config->sample_rate;
Revathi Uddarajud2634032017-12-07 14:42:34 +05307428 ret = audio_extn_cin_configure_input_stream(in);
7429 if (ret)
7430 goto err_open;
7431 } else {
7432 in->config = pcm_config_audio_capture;
7433 in->config.rate = config->sample_rate;
7434 in->config.format = pcm_format_from_audio_format(config->format);
7435 in->config.channels = channel_count;
7436 in->sample_rate = config->sample_rate;
7437 in->format = config->format;
7438 frame_size = audio_stream_in_frame_size(&in->stream);
7439 buffer_size = get_input_buffer_size(config->sample_rate,
Haynes Mathew George46740472017-10-27 18:40:12 -07007440 config->format,
7441 channel_count,
7442 is_low_latency);
Revathi Uddarajud2634032017-12-07 14:42:34 +05307443 in->config.period_size = buffer_size / frame_size;
Haynes Mathew George4ffef292017-11-21 15:08:02 -08007444
Revathi Uddarajud2634032017-12-07 14:42:34 +05307445 if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
7446 /* optionally use VOIP usecase depending on config(s) */
7447 ret = adev_update_voice_comm_input_stream(in, config);
7448 }
Haynes Mathew George4ffef292017-11-21 15:08:02 -08007449
Revathi Uddarajud2634032017-12-07 14:42:34 +05307450 if (ret) {
7451 ALOGE("%s AUDIO_SOURCE_VOICE_COMMUNICATION invalid args", __func__);
7452 goto err_open;
7453 }
Haynes Mathew George4ffef292017-11-21 15:08:02 -08007454 }
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07007455 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +05307456 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
7457 &adev->streams_input_cfg_list,
Haynes Mathew George4ffef292017-11-21 15:08:02 -08007458 devices, flags, in->format,
7459 in->sample_rate, in->bit_width,
7460 in->profile, &in->app_type_cfg);
Naresh Tanniru04f71882018-06-26 17:46:22 +05307461 register_format(in->format, in->supported_formats);
7462 register_channel_mask(in->channel_mask, in->supported_channel_masks);
7463 register_sample_rate(in->sample_rate, in->supported_sample_rates);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05307464
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07007465 /* This stream could be for sound trigger lab,
7466 get sound trigger pcm if present */
7467 audio_extn_sound_trigger_check_and_get_session(in);
7468
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05307469 lock_input_stream(in);
7470 audio_extn_snd_mon_register_listener(in, in_snd_mon_cb);
7471 pthread_mutex_lock(&adev->lock);
7472 in->card_status = adev->card_status;
7473 pthread_mutex_unlock(&adev->lock);
7474 pthread_mutex_unlock(&in->lock);
7475
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007476 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07007477 ALOGV("%s: exit", __func__);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08007478 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007479
7480err_open:
7481 free(in);
7482 *stream_in = NULL;
7483 return ret;
7484}
7485
7486static void adev_close_input_stream(struct audio_hw_device *dev,
7487 struct audio_stream_in *stream)
7488{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007489 int ret;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07007490 struct stream_in *in = (struct stream_in *)stream;
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07007491 struct audio_device *adev = (struct audio_device *)dev;
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05307492
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05307493 ALOGD("%s: enter:stream_handle(%p)",__func__, in);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08007494
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05307495 // must deregister from sndmonitor first to prevent races
7496 // between the callback and close_stream
7497 audio_extn_snd_mon_unregister_listener(stream);
7498
kunleiz8405edd2018-11-29 15:04:56 +08007499 // Disable echo reference if there are no active input and hfp call
7500 // while closing input stream
7501 if (!adev->active_input && !audio_extn_hfp_is_active(adev))
7502 platform_set_echo_reference(adev, false, AUDIO_DEVICE_NONE);
Venkata Narendra Kumar Gutta1bbbf542014-09-04 19:11:25 +05307503
Pallavid7c7a272018-01-16 11:22:55 +05307504 if (in == NULL) {
7505 ALOGE("%s: audio_stream_in ptr is NULL", __func__);
7506 return;
7507 }
7508
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007509 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05307510 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007511 ret = voice_extn_compress_voip_close_input_stream(&stream->common);
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05307512 pthread_mutex_unlock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007513 if (ret != 0)
7514 ALOGE("%s: Compress voip input cannot be closed, error:%d",
7515 __func__, ret);
7516 } else
7517 in_standby(&stream->common);
7518
Revathi Uddarajud2634032017-12-07 14:42:34 +05307519 pthread_mutex_lock(&adev->lock);
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -07007520 if (audio_extn_ssr_get_stream() == in) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07007521 audio_extn_ssr_deinit();
7522 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007523
Garmond Leunge2433c32017-09-28 21:51:22 -07007524 if (audio_extn_ffv_get_stream() == in) {
7525 audio_extn_ffv_stream_deinit();
7526 }
7527
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307528 if (audio_extn_compr_cap_enabled() &&
Mingming Yine62d7842013-10-25 16:26:03 -07007529 audio_extn_compr_cap_format_supported(in->config.format))
7530 audio_extn_compr_cap_deinit();
Dhanalakshmi Siddani74cf00b2016-12-02 13:55:57 +05307531
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307532 if (audio_extn_cin_attached_usecase(in->usecase))
7533 audio_extn_cin_close_input_stream(in);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07007534
Mingming Yinfd7607b2016-01-22 12:48:44 -08007535 if (in->is_st_session) {
7536 ALOGV("%s: sound trigger pcm stop lab", __func__);
7537 audio_extn_sound_trigger_stop_lab(in);
7538 }
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07007539 free(stream);
Revathi Uddarajud2634032017-12-07 14:42:34 +05307540 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007541 return;
7542}
7543
Siddartha Shaik31b530e2017-05-19 15:26:33 +05307544int adev_create_audio_patch(struct audio_hw_device *dev,
7545 unsigned int num_sources,
7546 const struct audio_port_config *sources,
7547 unsigned int num_sinks,
7548 const struct audio_port_config *sinks,
7549 audio_patch_handle_t *handle)
7550{
7551
7552
7553 return audio_extn_hw_loopback_create_audio_patch(dev,
7554 num_sources,
7555 sources,
7556 num_sinks,
7557 sinks,
7558 handle);
7559
7560}
7561
7562int adev_release_audio_patch(struct audio_hw_device *dev,
7563 audio_patch_handle_t handle)
7564{
7565 return audio_extn_hw_loopback_release_audio_patch(dev, handle);
7566}
7567
7568int adev_get_audio_port(struct audio_hw_device *dev, struct audio_port *config)
7569{
7570 return audio_extn_hw_loopback_get_audio_port(dev, config);
7571}
7572
7573int adev_set_audio_port_config(struct audio_hw_device *dev,
7574 const struct audio_port_config *config)
7575{
7576 return audio_extn_hw_loopback_set_audio_port_config(dev, config);
7577}
7578
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07007579static int adev_dump(const audio_hw_device_t *device __unused,
7580 int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007581{
7582 return 0;
7583}
7584
7585static int adev_close(hw_device_t *device)
7586{
7587 struct audio_device *adev = (struct audio_device *)device;
Kiran Kandi910e1862013-10-29 13:29:42 -07007588
7589 if (!adev)
7590 return 0;
7591
7592 pthread_mutex_lock(&adev_init_lock);
7593
7594 if ((--audio_device_ref_count) == 0) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05307595 audio_extn_snd_mon_unregister_listener(adev);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07007596 audio_extn_sound_trigger_deinit(adev);
Kiran Kandide144c82013-11-20 15:58:32 -08007597 audio_extn_listen_deinit(adev);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05307598 audio_extn_utils_release_streams_cfg_lists(
7599 &adev->streams_output_cfg_list,
7600 &adev->streams_input_cfg_list);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05307601 if (audio_extn_qaf_is_enabled())
7602 audio_extn_qaf_deinit();
Kiran Kandi910e1862013-10-29 13:29:42 -07007603 audio_route_free(adev->audio_route);
Weiyin Jiangaa80acd2016-09-21 16:42:11 +08007604 audio_extn_gef_deinit();
Kiran Kandi910e1862013-10-29 13:29:42 -07007605 free(adev->snd_dev_ref_cnt);
7606 platform_deinit(adev->platform);
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07007607 if (adev->adm_deinit)
7608 adev->adm_deinit(adev->adm_data);
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307609 qahwi_deinit(device);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08007610 audio_extn_adsp_hdlr_deinit();
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05307611 audio_extn_snd_mon_deinit();
Siddartha Shaik44dd7702017-06-14 12:13:25 +05307612 audio_extn_hw_loopback_deinit(adev);
Garmond Leunge2433c32017-09-28 21:51:22 -07007613 audio_extn_ffv_deinit();
Satish Babu Patakokilac3c5d432017-07-04 22:48:59 +05307614 if (adev->device_cfg_params) {
7615 free(adev->device_cfg_params);
7616 adev->device_cfg_params = NULL;
7617 }
Derek Chencdd17c72014-11-24 12:39:14 -08007618 if(adev->ext_hw_plugin)
7619 audio_extn_ext_hw_plugin_deinit(adev->ext_hw_plugin);
Kiran Kandi910e1862013-10-29 13:29:42 -07007620 free(device);
7621 adev = NULL;
7622 }
7623 pthread_mutex_unlock(&adev_init_lock);
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07007624
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007625 return 0;
7626}
7627
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07007628/* This returns 1 if the input parameter looks at all plausible as a low latency period size,
7629 * or 0 otherwise. A return value of 1 doesn't mean the value is guaranteed to work,
7630 * just that it _might_ work.
7631 */
7632static int period_size_is_plausible_for_low_latency(int period_size)
7633{
7634 switch (period_size) {
7635 case 160:
Ravi Kumar Alamanda474de5a2015-06-25 20:08:01 -07007636 case 192:
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07007637 case 240:
7638 case 320:
7639 case 480:
7640 return 1;
7641 default:
7642 return 0;
7643 }
7644}
7645
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05307646static void adev_snd_mon_cb(void *cookie, struct str_parms *parms)
7647{
7648 bool is_snd_card_status = false;
7649 bool is_ext_device_status = false;
7650 char value[32];
7651 int card = -1;
7652 card_status_t status;
7653
7654 if (cookie != adev || !parms)
7655 return;
7656
7657 if (!parse_snd_card_status(parms, &card, &status)) {
7658 is_snd_card_status = true;
7659 } else if (0 < str_parms_get_str(parms, "ext_audio_device", value, sizeof(value))) {
7660 is_ext_device_status = true;
7661 } else {
7662 // not a valid event
7663 return;
7664 }
7665
7666 pthread_mutex_lock(&adev->lock);
7667 if (card == adev->snd_card || is_ext_device_status) {
7668 if (is_snd_card_status && adev->card_status != status) {
7669 adev->card_status = status;
7670 platform_snd_card_update(adev->platform, status);
7671 audio_extn_fm_set_parameters(adev, parms);
7672 } else if (is_ext_device_status) {
7673 platform_set_parameters(adev->platform, parms);
7674 }
7675 }
7676 pthread_mutex_unlock(&adev->lock);
7677 return;
7678}
7679
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05307680/* out and adev lock held */
7681static int check_a2dp_restore_l(struct audio_device *adev, struct stream_out *out, bool restore)
7682{
7683 struct audio_usecase *uc_info;
7684 float left_p;
7685 float right_p;
7686 audio_devices_t devices;
7687
7688 uc_info = get_usecase_from_list(adev, out->usecase);
7689 if (uc_info == NULL) {
7690 ALOGE("%s: Could not find the usecase (%d) in the list",
7691 __func__, out->usecase);
7692 return -EINVAL;
7693 }
7694
7695 ALOGD("%s: enter: usecase(%d: %s)", __func__,
7696 out->usecase, use_case_table[out->usecase]);
7697
7698 if (restore) {
7699 // restore A2DP device for active usecases and unmute if required
7700 if ((out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) &&
7701 (uc_info->out_snd_device != SND_DEVICE_OUT_BT_A2DP)) {
7702 ALOGD("%s: restoring A2dp and unmuting stream", __func__);
7703 select_devices(adev, uc_info->id);
7704 pthread_mutex_lock(&out->compr_mute_lock);
7705 if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
7706 (out->a2dp_compress_mute)) {
7707 out->a2dp_compress_mute = false;
7708 out_set_compr_volume(&out->stream, out->volume_l, out->volume_r);
7709 }
7710 pthread_mutex_unlock(&out->compr_mute_lock);
7711 }
7712 } else {
7713 // mute compress stream if suspended
7714 pthread_mutex_lock(&out->compr_mute_lock);
7715 if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
7716 (!out->a2dp_compress_mute)) {
7717 if (!out->standby) {
7718 ALOGD("%s: selecting speaker and muting stream", __func__);
7719 devices = out->devices;
7720 out->devices = AUDIO_DEVICE_OUT_SPEAKER;
7721 left_p = out->volume_l;
7722 right_p = out->volume_r;
7723 if (out->offload_state == OFFLOAD_STATE_PLAYING)
7724 compress_pause(out->compr);
7725 out_set_compr_volume(&out->stream, (float)0, (float)0);
7726 out->a2dp_compress_mute = true;
7727 select_devices(adev, out->usecase);
7728 if (out->offload_state == OFFLOAD_STATE_PLAYING)
7729 compress_resume(out->compr);
7730 out->devices = devices;
7731 out->volume_l = left_p;
7732 out->volume_r = right_p;
7733 }
7734 }
7735 pthread_mutex_unlock(&out->compr_mute_lock);
7736 }
7737 ALOGV("%s: exit", __func__);
7738 return 0;
7739}
7740
7741int check_a2dp_restore(struct audio_device *adev, struct stream_out *out, bool restore)
7742{
7743 int ret = 0;
7744
7745 lock_output_stream(out);
7746 pthread_mutex_lock(&adev->lock);
7747
7748 ret = check_a2dp_restore_l(adev, out, restore);
7749
7750 pthread_mutex_unlock(&adev->lock);
7751 pthread_mutex_unlock(&out->lock);
7752 return ret;
7753}
7754
Haynes Mathew George01156f92018-04-13 15:29:54 -07007755void adev_on_battery_status_changed(bool charging)
7756{
7757 pthread_mutex_lock(&adev->lock);
7758 ALOGI("%s: battery status changed to %scharging", __func__, charging ? "" : "not ");
7759 adev->is_charging = charging;
Zhou Songc1088ea2018-06-12 00:17:29 +08007760 audio_extn_sound_trigger_update_battery_status(charging);
Haynes Mathew George01156f92018-04-13 15:29:54 -07007761 pthread_mutex_unlock(&adev->lock);
7762}
7763
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007764static int adev_open(const hw_module_t *module, const char *name,
7765 hw_device_t **device)
7766{
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05307767 int ret;
7768
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08007769 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007770 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
7771
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07007772 pthread_mutex_lock(&adev_init_lock);
Kiran Kandi910e1862013-10-29 13:29:42 -07007773 if (audio_device_ref_count != 0){
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07007774 *device = &adev->device.common;
Kiran Kandi910e1862013-10-29 13:29:42 -07007775 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07007776 ALOGD("%s: returning existing instance of adev", __func__);
7777 ALOGD("%s: exit", __func__);
7778 pthread_mutex_unlock(&adev_init_lock);
7779 return 0;
7780 }
7781
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007782 adev = calloc(1, sizeof(struct audio_device));
7783
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07007784 if (!adev) {
7785 pthread_mutex_unlock(&adev_init_lock);
7786 return -ENOMEM;
7787 }
7788
Ravi Kumar Alamanda40703102014-04-24 10:34:41 -07007789 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
7790
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +05307791#ifdef DYNAMIC_LOG_ENABLED
7792 register_for_dynamic_logging("hal");
7793#endif
7794
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007795 adev->device.common.tag = HARDWARE_DEVICE_TAG;
7796 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
7797 adev->device.common.module = (struct hw_module_t *)module;
7798 adev->device.common.close = adev_close;
7799
7800 adev->device.init_check = adev_init_check;
7801 adev->device.set_voice_volume = adev_set_voice_volume;
7802 adev->device.set_master_volume = adev_set_master_volume;
7803 adev->device.get_master_volume = adev_get_master_volume;
7804 adev->device.set_master_mute = adev_set_master_mute;
7805 adev->device.get_master_mute = adev_get_master_mute;
7806 adev->device.set_mode = adev_set_mode;
7807 adev->device.set_mic_mute = adev_set_mic_mute;
7808 adev->device.get_mic_mute = adev_get_mic_mute;
7809 adev->device.set_parameters = adev_set_parameters;
7810 adev->device.get_parameters = adev_get_parameters;
7811 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
7812 adev->device.open_output_stream = adev_open_output_stream;
7813 adev->device.close_output_stream = adev_close_output_stream;
7814 adev->device.open_input_stream = adev_open_input_stream;
7815 adev->device.close_input_stream = adev_close_input_stream;
Siddartha Shaik31b530e2017-05-19 15:26:33 +05307816 adev->device.create_audio_patch = adev_create_audio_patch;
7817 adev->device.release_audio_patch = adev_release_audio_patch;
7818 adev->device.get_audio_port = adev_get_audio_port;
7819 adev->device.set_audio_port_config = adev_set_audio_port_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007820 adev->device.dump = adev_dump;
Naresh Tannirudcb47c52018-06-25 16:23:32 +05307821 adev->device.get_microphones = adev_get_microphones;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007822
7823 /* Set the default route before the PCM stream is opened */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007824 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08007825 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08007826 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007827 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007828 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08007829 adev->acdb_settings = TTY_MODE_OFF;
vivek mehta344576a2016-04-12 18:56:03 -07007830 adev->allow_afe_proxy_usage = true;
Ashish Jain1b9b30c2017-05-18 20:57:40 +05307831 adev->bt_sco_on = false;
Eric Laurent07eeafd2013-10-06 12:52:49 -07007832 /* adev->cur_hdmi_channels = 0; by calloc() */
Eric Laurentb23d5282013-05-14 15:27:20 -07007833 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07007834 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08007835 list_init(&adev->usecase_list);
Krishnankutty Kolathappilly0b2de1c2014-02-14 14:45:49 -08007836 adev->cur_wfd_channels = 2;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07007837 adev->offload_usecases_state = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05307838 adev->is_channel_status_set = false;
Sudheer Papothifa9d2282015-09-17 01:53:25 +05307839 adev->perf_lock_opts[0] = 0x101;
7840 adev->perf_lock_opts[1] = 0x20E;
7841 adev->perf_lock_opts_size = 2;
Xiaojun Sang785b5da2017-08-03 15:52:29 +08007842 adev->dsp_bit_width_enforce_mode = 0;
Naresh Tanniru4c630392014-05-12 01:05:52 +05307843
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007844 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07007845 adev->platform = platform_init(adev);
7846 if (!adev->platform) {
Ben Romberger2684ad72018-06-04 12:41:39 -07007847 pthread_mutex_destroy(&adev->lock);
Eric Laurentb23d5282013-05-14 15:27:20 -07007848 free(adev->snd_dev_ref_cnt);
7849 free(adev);
Ben Romberger2684ad72018-06-04 12:41:39 -07007850 adev = NULL;
Eric Laurentb23d5282013-05-14 15:27:20 -07007851 ALOGE("%s: Failed to init platform data, aborting.", __func__);
7852 *device = NULL;
Apoorv Raghuvanshi6e57d7e2013-12-16 16:02:45 -08007853 pthread_mutex_unlock(&adev_init_lock);
Eric Laurentb23d5282013-05-14 15:27:20 -07007854 return -EINVAL;
7855 }
Eric Laurentc4aef752013-09-12 17:45:53 -07007856
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05307857 if (audio_extn_qaf_is_enabled()) {
7858 ret = audio_extn_qaf_init(adev);
7859 if (ret < 0) {
Ben Romberger2684ad72018-06-04 12:41:39 -07007860 pthread_mutex_destroy(&adev->lock);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05307861 free(adev);
Ben Romberger2684ad72018-06-04 12:41:39 -07007862 adev = NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05307863 ALOGE("%s: Failed to init platform data, aborting.", __func__);
7864 *device = NULL;
7865 pthread_mutex_unlock(&adev_init_lock);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05307866 return ret;
7867 }
7868
7869 adev->device.open_output_stream = audio_extn_qaf_open_output_stream;
7870 adev->device.close_output_stream = audio_extn_qaf_close_output_stream;
7871 }
7872
Derek Chencdd17c72014-11-24 12:39:14 -08007873 adev->ext_hw_plugin = audio_extn_ext_hw_plugin_init(adev);
7874
Eric Laurentc4aef752013-09-12 17:45:53 -07007875 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
7876 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
7877 if (adev->visualizer_lib == NULL) {
7878 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
7879 } else {
7880 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
7881 adev->visualizer_start_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08007882 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07007883 "visualizer_hal_start_output");
7884 adev->visualizer_stop_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08007885 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07007886 "visualizer_hal_stop_output");
7887 }
7888 }
Dhanalakshmi Siddani21be3ac2016-12-29 14:31:08 +05307889 audio_extn_init(adev);
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08007890 audio_extn_listen_init(adev, adev->snd_card);
Weiyin Jiangaa80acd2016-09-21 16:42:11 +08007891 audio_extn_gef_init(adev);
Siddartha Shaik44dd7702017-06-14 12:13:25 +05307892 audio_extn_hw_loopback_init(adev);
Garmond Leunge2433c32017-09-28 21:51:22 -07007893 audio_extn_ffv_init(adev);
Eric Laurentc4aef752013-09-12 17:45:53 -07007894
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08007895 if (access(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, R_OK) == 0) {
7896 adev->offload_effects_lib = dlopen(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, RTLD_NOW);
7897 if (adev->offload_effects_lib == NULL) {
7898 ALOGE("%s: DLOPEN failed for %s", __func__,
7899 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
7900 } else {
7901 ALOGV("%s: DLOPEN successful for %s", __func__,
7902 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
7903 adev->offload_effects_start_output =
Ashish Jain5106d362016-05-11 19:23:33 +05307904 (int (*)(audio_io_handle_t, int, struct mixer *))dlsym(adev->offload_effects_lib,
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08007905 "offload_effects_bundle_hal_start_output");
7906 adev->offload_effects_stop_output =
7907 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
7908 "offload_effects_bundle_hal_stop_output");
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08007909 adev->offload_effects_set_hpx_state =
7910 (int (*)(bool))dlsym(adev->offload_effects_lib,
7911 "offload_effects_bundle_set_hpx_state");
Dhananjay Kumard68883d2015-09-04 13:39:26 +05307912 adev->offload_effects_get_parameters =
7913 (void (*)(struct str_parms *, struct str_parms *))
7914 dlsym(adev->offload_effects_lib,
7915 "offload_effects_bundle_get_parameters");
7916 adev->offload_effects_set_parameters =
7917 (void (*)(struct str_parms *))dlsym(adev->offload_effects_lib,
7918 "offload_effects_bundle_set_parameters");
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08007919 }
7920 }
7921
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07007922 if (access(ADM_LIBRARY_PATH, R_OK) == 0) {
7923 adev->adm_lib = dlopen(ADM_LIBRARY_PATH, RTLD_NOW);
7924 if (adev->adm_lib == NULL) {
7925 ALOGE("%s: DLOPEN failed for %s", __func__, ADM_LIBRARY_PATH);
7926 } else {
7927 ALOGV("%s: DLOPEN successful for %s", __func__, ADM_LIBRARY_PATH);
7928 adev->adm_init = (adm_init_t)
7929 dlsym(adev->adm_lib, "adm_init");
7930 adev->adm_deinit = (adm_deinit_t)
7931 dlsym(adev->adm_lib, "adm_deinit");
7932 adev->adm_register_input_stream = (adm_register_input_stream_t)
7933 dlsym(adev->adm_lib, "adm_register_input_stream");
7934 adev->adm_register_output_stream = (adm_register_output_stream_t)
7935 dlsym(adev->adm_lib, "adm_register_output_stream");
7936 adev->adm_deregister_stream = (adm_deregister_stream_t)
7937 dlsym(adev->adm_lib, "adm_deregister_stream");
7938 adev->adm_request_focus = (adm_request_focus_t)
7939 dlsym(adev->adm_lib, "adm_request_focus");
7940 adev->adm_abandon_focus = (adm_abandon_focus_t)
7941 dlsym(adev->adm_lib, "adm_abandon_focus");
Haynes Mathew George5beddd42016-06-27 18:33:40 -07007942 adev->adm_set_config = (adm_set_config_t)
7943 dlsym(adev->adm_lib, "adm_set_config");
7944 adev->adm_request_focus_v2 = (adm_request_focus_v2_t)
7945 dlsym(adev->adm_lib, "adm_request_focus_v2");
7946 adev->adm_is_noirq_avail = (adm_is_noirq_avail_t)
7947 dlsym(adev->adm_lib, "adm_is_noirq_avail");
7948 adev->adm_on_routing_change = (adm_on_routing_change_t)
7949 dlsym(adev->adm_lib, "adm_on_routing_change");
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07007950 }
7951 }
7952
Mingming Yin514a8bc2014-07-29 15:22:21 -07007953 adev->bt_wb_speech_enabled = false;
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08007954 //initialize this to false for now,
7955 //this will be set to true through set param
7956 adev->vr_audio_mode_enabled = false;
Mingming Yin514a8bc2014-07-29 15:22:21 -07007957
Pradnya Chaphekar8a9dcd82014-09-09 09:49:10 -07007958 audio_extn_ds2_enable(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007959 *device = &adev->device.common;
Xiaojun Sang785b5da2017-08-03 15:52:29 +08007960 adev->dsp_bit_width_enforce_mode =
7961 adev_init_dsp_bit_width_enforce_mode(adev->mixer);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007962
Dhananjay Kumard6d32152016-10-13 16:11:03 +05307963 audio_extn_utils_update_streams_cfg_lists(adev->platform, adev->mixer,
7964 &adev->streams_output_cfg_list,
7965 &adev->streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07007966
Kiran Kandi910e1862013-10-29 13:29:42 -07007967 audio_device_ref_count++;
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07007968
7969 char value[PROPERTY_VALUE_MAX];
7970 int trial;
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07007971 if (property_get("vendor.audio_hal.period_size", value, NULL) > 0) {
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07007972 trial = atoi(value);
7973 if (period_size_is_plausible_for_low_latency(trial)) {
7974 pcm_config_low_latency.period_size = trial;
7975 pcm_config_low_latency.start_threshold = trial / 4;
7976 pcm_config_low_latency.avail_min = trial / 4;
7977 configured_low_latency_capture_period_size = trial;
7978 }
7979 }
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07007980 if (property_get("vendor.audio_hal.in_period_size", value, NULL) > 0) {
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07007981 trial = atoi(value);
7982 if (period_size_is_plausible_for_low_latency(trial)) {
7983 configured_low_latency_capture_period_size = trial;
7984 }
7985 }
7986
Vignesh Kulothungan7d374312018-02-21 17:12:00 -08007987 adev->mic_break_enabled = property_get_bool("vendor.audio.mic_break", false);
7988
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07007989 if (property_get("vendor.audio_hal.period_multiplier", value, NULL) > 0) {
Haynes Mathew George5beddd42016-06-27 18:33:40 -07007990 af_period_multiplier = atoi(value);
7991 if (af_period_multiplier < 0)
7992 af_period_multiplier = 2;
7993 else if (af_period_multiplier > 4)
7994 af_period_multiplier = 4;
7995
7996 ALOGV("new period_multiplier = %d", af_period_multiplier);
7997 }
7998
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07007999 adev->multi_offload_enable = property_get_bool("vendor.audio.offload.multiple.enabled", false);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07008000 pthread_mutex_unlock(&adev_init_lock);
8001
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07008002 if (adev->adm_init)
8003 adev->adm_data = adev->adm_init();
8004
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05308005 qahwi_init(*device);
Sudheer Papothifa9d2282015-09-17 01:53:25 +05308006 audio_extn_perf_lock_init();
Ben Rombergerd771a7c2017-02-22 18:05:17 -08008007 audio_extn_adsp_hdlr_init(adev->mixer);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05308008
8009 audio_extn_snd_mon_init();
8010 pthread_mutex_lock(&adev->lock);
8011 audio_extn_snd_mon_register_listener(adev, adev_snd_mon_cb);
8012 adev->card_status = CARD_STATUS_ONLINE;
Haynes Mathew George01156f92018-04-13 15:29:54 -07008013 audio_extn_battery_properties_listener_init(adev_on_battery_status_changed);
8014 /*
8015 * if the battery state callback happens before charging can be queried,
8016 * it will be guarded with the adev->lock held in the cb function and so
8017 * the callback value will reflect the latest state
8018 */
8019 adev->is_charging = audio_extn_battery_properties_is_charging();
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05308020 audio_extn_sound_trigger_init(adev); /* dependent on snd_mon_init() */
Zhou Songc1088ea2018-06-12 00:17:29 +08008021 audio_extn_sound_trigger_update_battery_status(adev->is_charging);
8022 pthread_mutex_unlock(&adev->lock);
Satish Babu Patakokilac3c5d432017-07-04 22:48:59 +05308023 /* Allocate memory for Device config params */
8024 adev->device_cfg_params = (struct audio_device_config_param*)
8025 calloc(platform_get_max_codec_backend(),
8026 sizeof(struct audio_device_config_param));
8027 if (adev->device_cfg_params == NULL)
8028 ALOGE("%s: Memory allocation failed for Device config params", __func__);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05308029
Eric Laurent994a6932013-07-17 11:51:42 -07008030 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008031 return 0;
8032}
8033
8034static struct hw_module_methods_t hal_module_methods = {
8035 .open = adev_open,
8036};
8037
8038struct audio_module HAL_MODULE_INFO_SYM = {
8039 .common = {
8040 .tag = HARDWARE_MODULE_TAG,
8041 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
8042 .hal_api_version = HARDWARE_HAL_API_VERSION,
8043 .id = AUDIO_HARDWARE_MODULE_ID,
8044 .name = "QCOM Audio HAL",
Duy Truongfae19622013-11-24 02:17:54 -08008045 .author = "The Linux Foundation",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08008046 .methods = &hal_module_methods,
8047 },
8048};