blob: d35ff0da63db8c63334ebf4f8566fffbd7b9016d [file] [log] [blame]
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001/*
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05302 * Copyright (c) 2013-2018, 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 }
995
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -0800996 strlcpy(mixer_path, use_case_table[usecase->id], MIXER_PATH_MAX_LENGTH);
Banajit Goswami20cdd212015-09-11 01:11:30 -0700997 platform_add_backend_name(mixer_path, snd_device, usecase);
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -0700998 ALOGD("%s: apply mixer and update path: %s", __func__, mixer_path);
Soumya Managolie7651c42018-06-28 16:04:57 +0530999 ret = audio_route_apply_and_update_path(adev->audio_route, mixer_path);
1000 if (!ret && usecase->id == USECASE_AUDIO_PLAYBACK_FM) {
1001 struct str_parms *parms = str_parms_create_str("fm_restore_volume=1");
1002 if (parms) {
1003 audio_extn_fm_set_parameters(adev, parms);
1004 str_parms_destroy(parms);
1005 }
1006 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001007 ALOGV("%s: exit", __func__);
1008 return 0;
1009}
1010
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001011int disable_audio_route(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001012 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001013{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001014 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001015 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001016
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05301017 if (usecase == NULL || usecase->id == USECASE_INVALID)
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001018 return -EINVAL;
1019
1020 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001021 if (usecase->type == PCM_CAPTURE)
1022 snd_device = usecase->in_snd_device;
1023 else
1024 snd_device = usecase->out_snd_device;
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -08001025 strlcpy(mixer_path, use_case_table[usecase->id], MIXER_PATH_MAX_LENGTH);
Banajit Goswami20cdd212015-09-11 01:11:30 -07001026 platform_add_backend_name(mixer_path, snd_device, usecase);
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07001027 ALOGD("%s: reset and update mixer path: %s", __func__, mixer_path);
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07001028 audio_route_reset_and_update_path(adev->audio_route, mixer_path);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001029 audio_extn_sound_trigger_update_stream_status(usecase, ST_EVENT_STREAM_FREE);
Dhananjay Kumar45b71742014-05-29 21:47:27 +05301030 audio_extn_listen_update_stream_status(usecase, LISTEN_EVENT_STREAM_FREE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001031 ALOGV("%s: exit", __func__);
1032 return 0;
1033}
1034
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -07001035int enable_snd_device(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001036 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001037{
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301038 int i, num_devices = 0;
1039 snd_device_t new_snd_devices[SND_DEVICE_OUT_END];
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001040 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
1041
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001042 if (snd_device < SND_DEVICE_MIN ||
1043 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001044 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001045 return -EINVAL;
1046 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001047
1048 adev->snd_dev_ref_cnt[snd_device]++;
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001049
1050 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0 ) {
1051 ALOGE("%s: Invalid sound device returned", __func__);
1052 return -EINVAL;
1053 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001054 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
Eric Laurent994a6932013-07-17 11:51:42 -07001055 ALOGV("%s: snd_device(%d: %s) is already active",
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001056 __func__, snd_device, device_name);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001057 return 0;
1058 }
1059
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301060
Gopikrishnaiah Anandane85d0462014-06-30 21:41:20 -07001061 if (audio_extn_spkr_prot_is_enabled())
1062 audio_extn_spkr_prot_calib_cancel(adev);
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -07001063
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001064 if (platform_can_enable_spkr_prot_on_device(snd_device) &&
1065 audio_extn_spkr_prot_is_enabled()) {
1066 if (platform_get_spkr_prot_acdb_id(snd_device) < 0) {
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001067 adev->snd_dev_ref_cnt[snd_device]--;
1068 return -EINVAL;
1069 }
Ravit Dennisaaee49c2015-02-04 21:26:22 +02001070 audio_extn_dev_arbi_acquire(snd_device);
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08001071 if (audio_extn_spkr_prot_start_processing(snd_device)) {
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -08001072 ALOGE("%s: spkr_start_processing failed", __func__);
Ravit Dennisaaee49c2015-02-04 21:26:22 +02001073 audio_extn_dev_arbi_release(snd_device);
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -08001074 return -EINVAL;
1075 }
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001076 } else if (platform_split_snd_device(adev->platform,
1077 snd_device,
1078 &num_devices,
1079 new_snd_devices) == 0) {
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301080 for (i = 0; i < num_devices; i++) {
1081 enable_snd_device(adev, new_snd_devices[i]);
1082 }
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -08001083 } else {
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07001084 ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, device_name);
Naresh Tannirucd2353e2016-08-19 00:37:25 +05301085
Preetam Singh Ranawatf1d417c2017-01-10 17:00:32 +05301086
Naresh Tannirucd2353e2016-08-19 00:37:25 +05301087 if ((SND_DEVICE_OUT_BT_A2DP == snd_device) &&
1088 (audio_extn_a2dp_start_playback() < 0)) {
1089 ALOGE(" fail to configure A2dp control path ");
1090 return -EINVAL;
1091 }
1092
Bharath Ramachandramurthy0de16782014-03-28 21:34:33 -07001093 /* due to the possibility of calibration overwrite between listen
1094 and audio, notify listen hal before audio calibration is sent */
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001095 audio_extn_sound_trigger_update_device_status(snd_device,
1096 ST_EVENT_SND_DEVICE_BUSY);
Dhananjay Kumar45b71742014-05-29 21:47:27 +05301097 audio_extn_listen_update_device_status(snd_device,
1098 LISTEN_EVENT_SND_DEVICE_BUSY);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -07001099 if (platform_get_snd_device_acdb_id(snd_device) < 0) {
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -07001100 adev->snd_dev_ref_cnt[snd_device]--;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001101 audio_extn_sound_trigger_update_device_status(snd_device,
1102 ST_EVENT_SND_DEVICE_FREE);
Dhananjay Kumar45b71742014-05-29 21:47:27 +05301103 audio_extn_listen_update_device_status(snd_device,
1104 LISTEN_EVENT_SND_DEVICE_FREE);
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -07001105 return -EINVAL;
1106 }
Lior Barenboim0b61bc72014-05-13 13:01:37 +03001107 audio_extn_dev_arbi_acquire(snd_device);
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07001108 audio_route_apply_and_update_path(adev->audio_route, device_name);
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301109
1110 if (SND_DEVICE_OUT_HEADPHONES == snd_device &&
1111 !adev->native_playback_enabled &&
1112 audio_is_true_native_stream_active(adev)) {
1113 ALOGD("%s: %d: napb: enabling native mode in hardware",
1114 __func__, __LINE__);
1115 audio_route_apply_and_update_path(adev->audio_route,
1116 "true-native-mode");
1117 adev->native_playback_enabled = true;
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +05301118 }
Garmond Leunge2433c32017-09-28 21:51:22 -07001119 if ((snd_device == SND_DEVICE_IN_HANDSET_6MIC) &&
1120 (audio_extn_ffv_get_stream() == adev->active_input)) {
1121 ALOGD("%s: init ec ref loopback", __func__);
1122 audio_extn_ffv_init_ec_ref_loopback(adev, snd_device);
1123 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001124 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001125 return 0;
1126}
1127
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001128int disable_snd_device(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001129 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001130{
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301131 int i, num_devices = 0;
1132 snd_device_t new_snd_devices[SND_DEVICE_OUT_END];
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001133 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
1134
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001135 if (snd_device < SND_DEVICE_MIN ||
1136 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001137 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001138 return -EINVAL;
1139 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001140 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
1141 ALOGE("%s: device ref cnt is already 0", __func__);
1142 return -EINVAL;
1143 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001144
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001145 adev->snd_dev_ref_cnt[snd_device]--;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -07001146
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001147 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0) {
1148 ALOGE("%s: Invalid sound device returned", __func__);
1149 return -EINVAL;
1150 }
1151
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001152 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07001153 ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, device_name);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301154
Xiaojun Sang040cc9f2015-08-03 19:38:28 +08001155 if (platform_can_enable_spkr_prot_on_device(snd_device) &&
1156 audio_extn_spkr_prot_is_enabled()) {
Anish Kumar46c7b872014-09-09 01:49:44 -07001157 audio_extn_spkr_prot_stop_processing(snd_device);
Vignesh Kulothungan3b5fae52017-09-25 12:16:30 -07001158
1159 // when speaker device is disabled, reset swap.
1160 // will be renabled on usecase start
1161 platform_set_swap_channels(adev, false);
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001162 } else if (platform_split_snd_device(adev->platform,
1163 snd_device,
1164 &num_devices,
1165 new_snd_devices) == 0) {
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301166 for (i = 0; i < num_devices; i++) {
1167 disable_snd_device(adev, new_snd_devices[i]);
1168 }
Lior Barenboim0b61bc72014-05-13 13:01:37 +03001169 } else {
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07001170 audio_route_reset_and_update_path(adev->audio_route, device_name);
Lior Barenboim0b61bc72014-05-13 13:01:37 +03001171 }
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07001172
Naresh Tannirucd2353e2016-08-19 00:37:25 +05301173 if (SND_DEVICE_OUT_BT_A2DP == snd_device)
1174 audio_extn_a2dp_stop_playback();
1175
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07001176 if (snd_device == SND_DEVICE_OUT_HDMI || snd_device == SND_DEVICE_OUT_DISPLAY_PORT)
Ashish Jain81eb2a82015-05-13 10:52:34 +05301177 adev->is_channel_status_set = false;
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301178 else if (SND_DEVICE_OUT_HEADPHONES == snd_device &&
1179 adev->native_playback_enabled) {
1180 ALOGD("%s: %d: napb: disabling native mode in hardware",
1181 __func__, __LINE__);
1182 audio_route_reset_and_update_path(adev->audio_route,
1183 "true-native-mode");
1184 adev->native_playback_enabled = false;
Preetam Singh Ranawatb0c0dd72016-08-18 00:32:06 +05301185 } else if (SND_DEVICE_OUT_HEADPHONES == snd_device &&
1186 adev->asrc_mode_enabled) {
1187 ALOGD("%s: %d: disabling asrc mode in hardware", __func__, __LINE__);
Preetam Singh Ranawat6a836662016-09-08 17:04:35 +05301188 disable_asrc_mode(adev);
1189 audio_route_apply_and_update_path(adev->audio_route, "hph-lowpower-mode");
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301190 }
Garmond Leunge2433c32017-09-28 21:51:22 -07001191 if ((snd_device == SND_DEVICE_IN_HANDSET_6MIC) &&
1192 (audio_extn_ffv_get_stream() == adev->active_input)) {
1193 ALOGD("%s: deinit ec ref loopback", __func__);
1194 audio_extn_ffv_deinit_ec_ref_loopback(adev, snd_device);
1195 }
Aniket Kumar Lata1be15992018-10-15 12:12:59 -07001196 audio_extn_utils_release_snd_device(snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001197 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -07001198
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001199 return 0;
1200}
1201
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001202/*
1203 legend:
1204 uc - existing usecase
1205 new_uc - new usecase
1206 d1, d11, d2 - SND_DEVICE enums
1207 a1, a2 - corresponding ANDROID device enums
1208 B1, B2 - backend strings
1209
1210case 1
1211 uc->dev d1 (a1) B1
1212 new_uc->dev d1 (a1), d2 (a2) B1, B2
1213
1214 resolution: disable and enable uc->dev on d1
1215
1216case 2
1217 uc->dev d1 (a1) B1
1218 new_uc->dev d11 (a1) B1
1219
1220 resolution: need to switch uc since d1 and d11 are related
1221 (e.g. speaker and voice-speaker)
1222 use ANDROID_DEVICE_OUT enums to match devices since SND_DEVICE enums may vary
1223
1224case 3
1225 uc->dev d1 (a1) B1
1226 new_uc->dev d2 (a2) B2
1227
1228 resolution: no need to switch uc
1229
1230case 4
1231 uc->dev d1 (a1) B1
1232 new_uc->dev d2 (a2) B1
1233
1234 resolution: disable enable uc-dev on d2 since backends match
1235 we cannot enable two streams on two different devices if they
1236 share the same backend. e.g. if offload is on speaker device using
1237 QUAD_MI2S backend and a low-latency stream is started on voice-handset
1238 using the same backend, offload must also be switched to voice-handset.
1239
1240case 5
1241 uc->dev d1 (a1) B1
1242 new_uc->dev d1 (a1), d2 (a2) B1
1243
1244 resolution: disable enable uc-dev on d2 since backends match
1245 we cannot enable two streams on two different devices if they
1246 share the same backend.
1247
1248case 6
1249 uc->dev d1 (a1) B1
1250 new_uc->dev d2 (a1) B2
1251
1252 resolution: no need to switch
1253
1254case 7
1255 uc->dev d1 (a1), d2 (a2) B1, B2
1256 new_uc->dev d1 (a1) B1
1257
1258 resolution: no need to switch
1259
Zhou Song4ba65882018-07-09 14:48:07 +08001260case 8
1261 uc->dev d1 (a1) B1
1262 new_uc->dev d11 (a1), d2 (a2) B1, B2
1263 resolution: compared to case 1, for this case, d1 and d11 are related
1264 then need to do the same as case 2 to siwtch to new uc
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001265*/
1266static snd_device_t derive_playback_snd_device(void * platform,
1267 struct audio_usecase *uc,
1268 struct audio_usecase *new_uc,
1269 snd_device_t new_snd_device)
1270{
Siddartha Shaik31b530e2017-05-19 15:26:33 +05301271 audio_devices_t a1, a2;
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001272
1273 snd_device_t d1 = uc->out_snd_device;
1274 snd_device_t d2 = new_snd_device;
1275
Siddartha Shaik31b530e2017-05-19 15:26:33 +05301276 switch (uc->type) {
1277 case TRANSCODE_LOOPBACK :
1278 a1 = uc->stream.inout->out_config.devices;
1279 a2 = new_uc->stream.inout->out_config.devices;
1280 break;
1281 default :
1282 a1 = uc->stream.out->devices;
1283 a2 = new_uc->stream.out->devices;
1284 break;
1285 }
1286
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001287 // Treat as a special case when a1 and a2 are not disjoint
1288 if ((a1 != a2) && (a1 & a2)) {
1289 snd_device_t d3[2];
1290 int num_devices = 0;
1291 int ret = platform_split_snd_device(platform,
1292 popcount(a1) > 1 ? d1 : d2,
1293 &num_devices,
1294 d3);
1295 if (ret < 0) {
1296 if (ret != -ENOSYS) {
1297 ALOGW("%s failed to split snd_device %d",
1298 __func__,
1299 popcount(a1) > 1 ? d1 : d2);
1300 }
1301 goto end;
1302 }
1303
1304 // NB: case 7 is hypothetical and isn't a practical usecase yet.
1305 // But if it does happen, we need to give priority to d2 if
1306 // the combo devices active on the existing usecase share a backend.
1307 // This is because we cannot have a usecase active on a combo device
1308 // and a new usecase requests one device in this combo pair.
1309 if (platform_check_backends_match(d3[0], d3[1])) {
1310 return d2; // case 5
1311 } else {
Zhou Song671be042018-08-27 15:33:52 +08001312 // check if d1 is related to any of d3's OR
1313 // old uc is combo device but new_uc is one of the combo device
1314 if (d1 == d3[0] || d1 == d3[1] || popcount(a1) > 1)
1315 return d1; // case 1, 7
Zhou Song4ba65882018-07-09 14:48:07 +08001316 else
1317 return d3[1]; // case 8
Haynes Mathew Georgebfe8ff42016-09-22 17:38:16 -07001318 }
1319 } else {
1320 if (platform_check_backends_match(d1, d2)) {
1321 return d2; // case 2, 4
1322 } else {
1323 return d1; // case 6, 3
1324 }
1325 }
1326
1327end:
1328 return d2; // return whatever was calculated before.
1329}
1330
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001331static void check_usecases_codec_backend(struct audio_device *adev,
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301332 struct audio_usecase *uc_info,
1333 snd_device_t snd_device)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001334{
1335 struct listnode *node;
1336 struct audio_usecase *usecase;
1337 bool switch_device[AUDIO_USECASE_MAX];
Chaithanya Krishna Bacharaju49e7db02017-03-14 11:57:26 +05301338 snd_device_t uc_derive_snd_device;
1339 snd_device_t derive_snd_device[AUDIO_USECASE_MAX];
Aniket Kumar Lata25b344d2018-09-25 19:00:35 -07001340 snd_device_t split_snd_devices[SND_DEVICE_OUT_END];
1341 int i, num_uc_to_switch = 0, num_devices = 0;
kunleiz5cd52b82016-11-07 17:22:52 +08001342 int status = 0;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301343 bool force_restart_session = false;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001344 /*
1345 * This function is to make sure that all the usecases that are active on
1346 * the hardware codec backend are always routed to any one device that is
1347 * handled by the hardware codec.
1348 * For example, if low-latency and deep-buffer usecases are currently active
1349 * on speaker and out_set_parameters(headset) is received on low-latency
1350 * output, then we have to make sure deep-buffer is also switched to headset,
1351 * because of the limitation that both the devices cannot be enabled
1352 * at the same time as they share the same backend.
1353 */
Mingming Yin3ee55c62014-08-04 14:23:35 -07001354 /*
1355 * This call is to check if we need to force routing for a particular stream
1356 * If there is a backend configuration change for the device when a
1357 * new stream starts, then ADM needs to be closed and re-opened with the new
1358 * configuraion. This call check if we need to re-route all the streams
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -08001359 * associated with the backend. Touch tone + 24 bit + native playback.
Mingming Yin3ee55c62014-08-04 14:23:35 -07001360 */
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -08001361 bool force_routing = platform_check_and_set_codec_backend_cfg(adev, uc_info,
1362 snd_device);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301363 /* For a2dp device reconfigure all active sessions
1364 * with new AFE encoder format based on a2dp state
1365 */
1366 if ((SND_DEVICE_OUT_BT_A2DP == snd_device ||
1367 SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP == snd_device) &&
1368 audio_extn_a2dp_is_force_device_switch()) {
1369 force_routing = true;
1370 force_restart_session = true;
1371 }
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301372 ALOGD("%s:becf: force routing %d", __func__, force_routing);
1373
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001374 /* Disable all the usecases on the shared backend other than the
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -08001375 * specified usecase.
Apoorv Raghuvanshif59bb222015-02-18 12:23:23 -08001376 */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001377 for (i = 0; i < AUDIO_USECASE_MAX; i++)
1378 switch_device[i] = false;
1379
1380 list_for_each(node, &adev->usecase_list) {
1381 usecase = node_to_item(node, struct audio_usecase, list);
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -08001382
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301383 ALOGD("%s:becf: (%d) check_usecases curr device: %s, usecase device:%s "
1384 "backends match %d",__func__, i,
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301385 platform_get_snd_device_name(snd_device),
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301386 platform_get_snd_device_name(usecase->out_snd_device),
1387 platform_check_backends_match(snd_device, usecase->out_snd_device));
Ashish Jain6a65b352017-03-21 17:24:40 +05301388 if ((usecase->type != PCM_CAPTURE) && (usecase != uc_info)) {
1389 uc_derive_snd_device = derive_playback_snd_device(adev->platform,
1390 usecase, uc_info, snd_device);
1391 if (((uc_derive_snd_device != usecase->out_snd_device) || force_routing) &&
1392 ((usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) ||
1393 (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) ||
1394 (usecase->devices & AUDIO_DEVICE_OUT_USB_DEVICE) ||
Garmond Leung2fd4f122018-06-02 16:04:53 -07001395 (usecase->devices & AUDIO_DEVICE_OUT_USB_HEADSET) ||
Ashish Jain6a65b352017-03-21 17:24:40 +05301396 (usecase->devices & AUDIO_DEVICE_OUT_ALL_A2DP) ||
1397 (usecase->devices & AUDIO_DEVICE_OUT_ALL_SCO)) &&
1398 ((force_restart_session) ||
1399 (platform_check_backends_match(snd_device, usecase->out_snd_device)))) {
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301400 ALOGD("%s:becf: check_usecases (%s) is active on (%s) - disabling ..",
1401 __func__, use_case_table[usecase->id],
1402 platform_get_snd_device_name(usecase->out_snd_device));
1403 disable_audio_route(adev, usecase);
1404 switch_device[usecase->id] = true;
Chaithanya Krishna Bacharaju49e7db02017-03-14 11:57:26 +05301405 /* Enable existing usecase on derived playback device */
1406 derive_snd_device[usecase->id] = uc_derive_snd_device;
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +05301407 num_uc_to_switch++;
Ashish Jain6a65b352017-03-21 17:24:40 +05301408 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001409 }
1410 }
1411
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301412 ALOGD("%s:becf: check_usecases num.of Usecases to switch %d", __func__,
1413 num_uc_to_switch);
1414
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001415 if (num_uc_to_switch) {
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07001416 /* All streams have been de-routed. Disable the device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001417
Venkata Narendra Kumar Gutta7610e632014-04-14 23:16:38 +05301418 /* Make sure the previous devices to be disabled first and then enable the
1419 selected devices */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001420 list_for_each(node, &adev->usecase_list) {
1421 usecase = node_to_item(node, struct audio_usecase, list);
1422 if (switch_device[usecase->id]) {
Aniket Kumar Lata1be15992018-10-15 12:12:59 -07001423 /* Check if output sound device to be switched can be split and if any
Aniket Kumar Lata25b344d2018-09-25 19:00:35 -07001424 of the split devices match with derived sound device */
Aniket Kumar Lata1be15992018-10-15 12:12:59 -07001425 if (platform_split_snd_device(adev->platform, usecase->out_snd_device,
1426 &num_devices, split_snd_devices) == 0) {
1427 adev->snd_dev_ref_cnt[usecase->out_snd_device]--;
1428 if (adev->snd_dev_ref_cnt[usecase->out_snd_device] == 0) {
1429 ALOGD("%s: disabling snd_device(%d)", __func__, usecase->out_snd_device);
1430 for (i = 0; i < num_devices; i++) {
1431 /* Disable devices that do not match with derived sound device */
1432 if (split_snd_devices[i] != derive_snd_device[usecase->id])
1433 disable_snd_device(adev, split_snd_devices[i]);
Aniket Kumar Lata25b344d2018-09-25 19:00:35 -07001434 }
Aniket Kumar Lata1be15992018-10-15 12:12:59 -07001435 audio_extn_utils_release_snd_device(usecase->out_snd_device);
Aniket Kumar Lata25b344d2018-09-25 19:00:35 -07001436 }
1437 } else {
1438 disable_snd_device(adev, usecase->out_snd_device);
1439 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001440 }
1441 }
1442
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -07001443 list_for_each(node, &adev->usecase_list) {
1444 usecase = node_to_item(node, struct audio_usecase, list);
1445 if (switch_device[usecase->id]) {
Aniket Kumar Lata1be15992018-10-15 12:12:59 -07001446 if (platform_split_snd_device(adev->platform, usecase->out_snd_device,
1447 &num_devices, split_snd_devices) == 0) {
1448 /* Enable derived sound device only if it does not match with
1449 one of the split sound devices. This is because the matching
1450 sound device was not disabled */
1451 bool should_enable = true;
1452 for (i = 0; i < num_devices; i++) {
1453 if (derive_snd_device[usecase->id] == split_snd_devices[i]) {
1454 should_enable = false;
1455 break;
1456 }
1457 }
1458 if (should_enable)
1459 enable_snd_device(adev, derive_snd_device[usecase->id]);
1460 } else {
1461 enable_snd_device(adev, derive_snd_device[usecase->id]);
1462 }
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -07001463 }
1464 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001465
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001466 /* Re-route all the usecases on the shared backend other than the
1467 specified usecase to new snd devices */
1468 list_for_each(node, &adev->usecase_list) {
1469 usecase = node_to_item(node, struct audio_usecase, list);
Karthik Reddy Katta3abfee22016-02-23 10:55:27 +05301470 /* Update the out_snd_device only before enabling the audio route */
1471 if (switch_device[usecase->id]) {
Chaithanya Krishna Bacharaju49e7db02017-03-14 11:57:26 +05301472 usecase->out_snd_device = derive_snd_device[usecase->id];
Karthik Reddy Katta3abfee22016-02-23 10:55:27 +05301473 if (usecase->type != VOICE_CALL) {
Sidipotu Ashoke6f78cb2015-11-05 14:42:20 +05301474 ALOGD("%s:becf: enabling usecase (%s) on (%s)", __func__,
Karthik Reddy Katta3abfee22016-02-23 10:55:27 +05301475 use_case_table[usecase->id],
1476 platform_get_snd_device_name(usecase->out_snd_device));
kunleiz5cd52b82016-11-07 17:22:52 +08001477 /* Update voc calibration before enabling VoIP route */
1478 if (usecase->type == VOIP_CALL)
1479 status = platform_switch_voice_call_device_post(adev->platform,
1480 usecase->out_snd_device,
kunleizab514ca2017-01-09 14:59:50 +08001481 platform_get_input_snd_device(adev->platform, uc_info->devices));
Avinash Vaish71a8b972014-07-24 15:36:33 +05301482 enable_audio_route(adev, usecase);
kunleiz46c47dd2018-03-19 16:28:09 +08001483 if (usecase->id == USECASE_AUDIO_PLAYBACK_VOIP) {
1484 out_set_voip_volume(&usecase->stream.out->stream,
1485 usecase->stream.out->volume_l,
1486 usecase->stream.out->volume_r);
1487 }
Karthik Reddy Katta3abfee22016-02-23 10:55:27 +05301488 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001489 }
1490 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001491 }
1492}
1493
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05301494static void check_usecases_capture_codec_backend(struct audio_device *adev,
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001495 struct audio_usecase *uc_info,
1496 snd_device_t snd_device)
1497{
1498 struct listnode *node;
1499 struct audio_usecase *usecase;
1500 bool switch_device[AUDIO_USECASE_MAX];
1501 int i, num_uc_to_switch = 0;
Dhanalakshmi Siddanib678a802016-12-03 11:51:41 +05301502 int backend_check_cond = AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND;
kunleiz5cd52b82016-11-07 17:22:52 +08001503 int status = 0;
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001504
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05301505 bool force_routing = platform_check_and_set_capture_codec_backend_cfg(adev, uc_info,
1506 snd_device);
1507 ALOGD("%s:becf: force routing %d", __func__, force_routing);
Dhanalakshmi Siddanib678a802016-12-03 11:51:41 +05301508
1509 /*
1510 * Make sure out devices is checked against out codec backend device and
1511 * also in devices against in codec backend. Checking out device against in
1512 * codec backend or vice versa causes issues.
1513 */
1514 if (uc_info->type == PCM_CAPTURE)
1515 backend_check_cond = AUDIO_DEVICE_IN_ALL_CODEC_BACKEND;
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001516 /*
1517 * This function is to make sure that all the active capture usecases
1518 * are always routed to the same input sound device.
1519 * For example, if audio-record and voice-call usecases are currently
1520 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
1521 * is received for voice call then we have to make sure that audio-record
1522 * usecase is also switched to earpiece i.e. voice-dmic-ef,
1523 * because of the limitation that two devices cannot be enabled
1524 * at the same time if they share the same backend.
1525 */
1526 for (i = 0; i < AUDIO_USECASE_MAX; i++)
1527 switch_device[i] = false;
1528
1529 list_for_each(node, &adev->usecase_list) {
1530 usecase = node_to_item(node, struct audio_usecase, list);
Dhanalakshmi Siddanib678a802016-12-03 11:51:41 +05301531 /*
1532 * TODO: Enhance below condition to handle BT sco/USB multi recording
1533 */
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -08001534 if (usecase->type != PCM_PLAYBACK &&
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001535 usecase != uc_info &&
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05301536 (usecase->in_snd_device != snd_device || force_routing) &&
Dhanalakshmi Siddanib678a802016-12-03 11:51:41 +05301537 ((uc_info->devices & backend_check_cond) &&
Karthik Reddy Katta0d471762015-08-20 13:36:07 +05301538 (((usecase->devices & ~AUDIO_DEVICE_BIT_IN) & AUDIO_DEVICE_IN_ALL_CODEC_BACKEND) ||
Satya Krishna Pindiproli458b5a72017-03-13 15:14:24 +05301539 (usecase->type == VOIP_CALL))) &&
Anish Kumara020a7c2014-10-17 11:13:22 -07001540 (usecase->id != USECASE_AUDIO_SPKR_CALIB_TX)) {
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001541 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
1542 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -07001543 platform_get_snd_device_name(usecase->in_snd_device));
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001544 disable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001545 switch_device[usecase->id] = true;
1546 num_uc_to_switch++;
1547 }
1548 }
1549
1550 if (num_uc_to_switch) {
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -07001551 /* All streams have been de-routed. Disable the device */
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001552
Venkata Narendra Kumar Gutta7610e632014-04-14 23:16:38 +05301553 /* Make sure the previous devices to be disabled first and then enable the
1554 selected devices */
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001555 list_for_each(node, &adev->usecase_list) {
1556 usecase = node_to_item(node, struct audio_usecase, list);
1557 if (switch_device[usecase->id]) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001558 disable_snd_device(adev, usecase->in_snd_device);
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -08001559 }
1560 }
1561
1562 list_for_each(node, &adev->usecase_list) {
1563 usecase = node_to_item(node, struct audio_usecase, list);
1564 if (switch_device[usecase->id]) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001565 enable_snd_device(adev, snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001566 }
1567 }
1568
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001569 /* Re-route all the usecases on the shared backend other than the
1570 specified usecase to new snd devices */
1571 list_for_each(node, &adev->usecase_list) {
1572 usecase = node_to_item(node, struct audio_usecase, list);
1573 /* Update the in_snd_device only before enabling the audio route */
1574 if (switch_device[usecase->id] ) {
1575 usecase->in_snd_device = snd_device;
kunleiz5cd52b82016-11-07 17:22:52 +08001576 if (usecase->type != VOICE_CALL) {
1577 /* Update voc calibration before enabling VoIP route */
1578 if (usecase->type == VOIP_CALL)
1579 status = platform_switch_voice_call_device_post(adev->platform,
Zhou Song557e7282017-05-05 17:18:18 +08001580 platform_get_output_snd_device(adev->platform, uc_info->stream.out),
kunleiz5cd52b82016-11-07 17:22:52 +08001581 usecase->in_snd_device);
Avinash Vaish71a8b972014-07-24 15:36:33 +05301582 enable_audio_route(adev, usecase);
kunleiz5cd52b82016-11-07 17:22:52 +08001583 }
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001584 }
1585 }
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001586 }
1587}
1588
Mingming Yin3a941d42016-02-17 18:08:05 -08001589static void reset_hdmi_sink_caps(struct stream_out *out) {
1590 int i = 0;
1591
1592 for (i = 0; i<= MAX_SUPPORTED_CHANNEL_MASKS; i++) {
1593 out->supported_channel_masks[i] = 0;
1594 }
1595 for (i = 0; i<= MAX_SUPPORTED_FORMATS; i++) {
1596 out->supported_formats[i] = 0;
1597 }
1598 for (i = 0; i<= MAX_SUPPORTED_SAMPLE_RATES; i++) {
1599 out->supported_sample_rates[i] = 0;
1600 }
1601}
1602
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001603/* must be called with hw device mutex locked */
Mingming Yin3a941d42016-02-17 18:08:05 -08001604static int read_hdmi_sink_caps(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001605{
Mingming Yin3a941d42016-02-17 18:08:05 -08001606 int ret = 0, i = 0, j = 0;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07001607 int channels = platform_edid_get_max_channels(out->dev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001608
Mingming Yin3a941d42016-02-17 18:08:05 -08001609 reset_hdmi_sink_caps(out);
1610
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07001611 /* Cache ext disp type */
Garmond Leung37850ab2016-10-06 11:42:18 -07001612 if (platform_get_ext_disp_type(adev->platform) <= 0) {
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07001613 ALOGE("%s: Failed to query disp type, ret:%d", __func__, ret);
Garmond Leung37850ab2016-10-06 11:42:18 -07001614 return -EINVAL;
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07001615 }
1616
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001617 switch (channels) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001618 case 8:
Mingming Yin3a941d42016-02-17 18:08:05 -08001619 ALOGV("%s: HDMI supports 7.1 channels", __func__);
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -07001620 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_7POINT1;
Mingming Yin3a941d42016-02-17 18:08:05 -08001621 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_6POINT1;
1622 case 6:
1623 ALOGV("%s: HDMI supports 5.1 channels", __func__);
1624 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_5POINT1;
1625 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_PENTA;
1626 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_QUAD;
1627 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_SURROUND;
1628 out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_2POINT1;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001629 break;
1630 default:
Mingming Yin3a941d42016-02-17 18:08:05 -08001631 ALOGE("invalid/nonstandard channal count[%d]",channels);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001632 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001633 break;
1634 }
Mingming Yin3a941d42016-02-17 18:08:05 -08001635
1636 // check channel format caps
1637 i = 0;
1638 if (platform_is_edid_supported_format(out->dev->platform, AUDIO_FORMAT_AC3)) {
1639 ALOGV(":%s HDMI supports AC3/EAC3 formats", __func__);
1640 out->supported_formats[i++] = AUDIO_FORMAT_AC3;
1641 //Adding EAC3/EAC3_JOC formats if AC3 is supported by the sink.
1642 //EAC3/EAC3_JOC will be converted to AC3 for decoding if needed
1643 out->supported_formats[i++] = AUDIO_FORMAT_E_AC3;
1644 out->supported_formats[i++] = AUDIO_FORMAT_E_AC3_JOC;
1645 }
1646
Ben Romberger1aaaf862017-04-06 17:49:46 -07001647 if (platform_is_edid_supported_format(out->dev->platform, AUDIO_FORMAT_DOLBY_TRUEHD)) {
1648 ALOGV(":%s HDMI supports TRUE HD format", __func__);
1649 out->supported_formats[i++] = AUDIO_FORMAT_DOLBY_TRUEHD;
1650 }
1651
Mingming Yin3a941d42016-02-17 18:08:05 -08001652 if (platform_is_edid_supported_format(out->dev->platform, AUDIO_FORMAT_DTS)) {
1653 ALOGV(":%s HDMI supports DTS format", __func__);
1654 out->supported_formats[i++] = AUDIO_FORMAT_DTS;
1655 }
1656
1657 if (platform_is_edid_supported_format(out->dev->platform, AUDIO_FORMAT_DTS_HD)) {
1658 ALOGV(":%s HDMI supports DTS HD format", __func__);
1659 out->supported_formats[i++] = AUDIO_FORMAT_DTS_HD;
1660 }
1661
Naresh Tanniru928f0862017-04-07 16:44:23 -07001662 if (platform_is_edid_supported_format(out->dev->platform, AUDIO_FORMAT_IEC61937)) {
1663 ALOGV(":%s HDMI supports IEC61937 format", __func__);
1664 out->supported_formats[i++] = AUDIO_FORMAT_IEC61937;
1665 }
1666
Mingming Yin3a941d42016-02-17 18:08:05 -08001667
1668 // check sample rate caps
1669 i = 0;
1670 for (j = 0; j < MAX_SUPPORTED_SAMPLE_RATES; j++) {
1671 if (platform_is_edid_supported_sample_rate(out->dev->platform, out_hdmi_sample_rates[j])) {
1672 ALOGV(":%s HDMI supports sample rate:%d", __func__, out_hdmi_sample_rates[j]);
1673 out->supported_sample_rates[i++] = out_hdmi_sample_rates[j];
1674 }
1675 }
1676
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001677 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001678}
1679
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001680static inline ssize_t read_usb_sup_sample_rates(bool is_playback __unused,
1681 uint32_t *supported_sample_rates __unused,
1682 uint32_t max_rates __unused)
1683{
1684 ssize_t count = audio_extn_usb_get_sup_sample_rates(is_playback,
1685 supported_sample_rates,
1686 max_rates);
Ashish Jain4847e9d2017-08-17 19:16:57 +05301687 ssize_t i = 0;
1688
1689 for (i=0; i<count; i++) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07001690 ALOGV("%s %s %d", __func__, is_playback ? "P" : "C",
1691 supported_sample_rates[i]);
1692 }
1693 return count;
1694}
1695
1696static inline int read_usb_sup_channel_masks(bool is_playback,
1697 audio_channel_mask_t *supported_channel_masks,
1698 uint32_t max_masks)
1699{
1700 int channels = audio_extn_usb_get_max_channels(is_playback);
1701 int channel_count;
1702 uint32_t num_masks = 0;
1703 if (channels > MAX_HIFI_CHANNEL_COUNT)
1704 channels = MAX_HIFI_CHANNEL_COUNT;
1705
1706 if (is_playback) {
1707 // For playback we never report mono because the framework always outputs stereo
1708 channel_count = DEFAULT_CHANNEL_COUNT;
1709 // audio_channel_out_mask_from_count() does return positional masks for channel counts
1710 // above 2 but we want indexed masks here. So we
1711 for ( ; channel_count <= channels && num_masks < max_masks; channel_count++) {
1712 supported_channel_masks[num_masks++] = audio_channel_out_mask_from_count(channel_count);
1713 }
1714 for ( ; channel_count <= channels && num_masks < max_masks; channel_count++) {
1715 supported_channel_masks[num_masks++] =
1716 audio_channel_mask_for_index_assignment_from_count(channel_count);
1717 }
1718 } else {
1719 // For capture we report all supported channel masks from 1 channel up.
1720 channel_count = MIN_CHANNEL_COUNT;
1721 // audio_channel_in_mask_from_count() does the right conversion to either positional or
1722 // indexed mask
1723 for ( ; channel_count <= channels && num_masks < max_masks; channel_count++) {
1724 supported_channel_masks[num_masks++] =
1725 audio_channel_in_mask_from_count(channel_count);
1726 }
1727 }
1728 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();
Haynes Mathew George16081042017-05-31 17:16:49 -07002571 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) {
2572 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
2573 if (in->pcm != NULL) {
2574 pcm_close(in->pcm);
2575 in->pcm = NULL;
2576 }
2577 if (pcm_open_retry_count-- == 0) {
2578 ret = -EIO;
2579 goto error_open;
2580 }
2581 usleep(PROXY_OPEN_WAIT_TIME * 1000);
2582 continue;
2583 }
2584 break;
2585 }
2586
2587 ALOGV("%s: pcm_prepare", __func__);
Haynes Mathew George380745d2017-10-04 15:27:45 -07002588 ATRACE_BEGIN("pcm_in_prepare");
Haynes Mathew George16081042017-05-31 17:16:49 -07002589 ret = pcm_prepare(in->pcm);
Haynes Mathew George380745d2017-10-04 15:27:45 -07002590 ATRACE_END();
Haynes Mathew George16081042017-05-31 17:16:49 -07002591 if (ret < 0) {
2592 ALOGE("%s: pcm_prepare returned %d", __func__, ret);
2593 pcm_close(in->pcm);
2594 in->pcm = NULL;
2595 goto error_open;
2596 }
2597 register_in_stream(in);
2598 if (in->realtime) {
Haynes Mathew George380745d2017-10-04 15:27:45 -07002599 ATRACE_BEGIN("pcm_in_start");
Haynes Mathew George16081042017-05-31 17:16:49 -07002600 ret = pcm_start(in->pcm);
Haynes Mathew George380745d2017-10-04 15:27:45 -07002601 ATRACE_END();
Haynes Mathew George16081042017-05-31 17:16:49 -07002602 if (ret < 0) {
2603 ALOGE("%s: RT pcm_start failed ret %d", __func__, ret);
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07002604 pcm_close(in->pcm);
2605 in->pcm = NULL;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07002606 goto error_open;
2607 }
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07002608 }
Haynes Mathew George5beddd42016-06-27 18:33:40 -07002609 }
2610
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07002611 check_and_enable_effect(adev);
2612
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05302613done_open:
Sudheer Papothifa9d2282015-09-17 01:53:25 +05302614 audio_extn_perf_lock_release(&adev->perf_lock_handle);
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07002615 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07002616
Eric Laurentc8400632013-02-14 19:04:54 -08002617 return ret;
2618
2619error_open:
Sudheer Papothifa9d2282015-09-17 01:53:25 +05302620 audio_extn_perf_lock_release(&adev->perf_lock_handle);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002621 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -08002622error_config:
Dhananjay Kumard4833242016-10-06 22:09:12 +05302623 adev->active_input = get_next_active_input(adev);
Laxminath Kasam2cb4b752015-09-24 03:59:15 +05302624 /*
2625 * sleep 50ms to allow sufficient time for kernel
2626 * drivers to recover incases like SSR.
2627 */
2628 usleep(50000);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002629 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -08002630
2631 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002632}
2633
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07002634void lock_input_stream(struct stream_in *in)
2635{
2636 pthread_mutex_lock(&in->pre_lock);
2637 pthread_mutex_lock(&in->lock);
2638 pthread_mutex_unlock(&in->pre_lock);
2639}
2640
2641void lock_output_stream(struct stream_out *out)
2642{
2643 pthread_mutex_lock(&out->pre_lock);
2644 pthread_mutex_lock(&out->lock);
2645 pthread_mutex_unlock(&out->pre_lock);
2646}
2647
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002648/* must be called with out->lock locked */
2649static int send_offload_cmd_l(struct stream_out* out, int command)
2650{
2651 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
2652
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07002653 if (!cmd) {
2654 ALOGE("failed to allocate mem for command 0x%x", command);
2655 return -ENOMEM;
2656 }
2657
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002658 ALOGVV("%s %d", __func__, command);
2659
2660 cmd->cmd = command;
2661 list_add_tail(&out->offload_cmd_list, &cmd->node);
2662 pthread_cond_signal(&out->offload_cond);
2663 return 0;
2664}
2665
2666/* must be called iwth out->lock locked */
2667static void stop_compressed_output_l(struct stream_out *out)
2668{
2669 out->offload_state = OFFLOAD_STATE_IDLE;
2670 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002671 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002672 if (out->compr != NULL) {
2673 compress_stop(out->compr);
2674 while (out->offload_thread_blocked) {
2675 pthread_cond_wait(&out->cond, &out->lock);
2676 }
2677 }
2678}
2679
Varun Balaraje49253e2017-07-06 19:48:56 +05302680bool is_interactive_usecase(audio_usecase_t uc_id)
2681{
2682 unsigned int i;
2683 for (i = 0; i < sizeof(interactive_usecases)/sizeof(interactive_usecases[0]); i++) {
2684 if (uc_id == interactive_usecases[i])
2685 return true;
2686 }
2687 return false;
2688}
2689
2690static audio_usecase_t get_interactive_usecase(struct audio_device *adev)
2691{
2692 audio_usecase_t ret_uc = USECASE_INVALID;
2693 unsigned int intract_uc_index;
2694 unsigned int num_usecase = sizeof(interactive_usecases)/sizeof(interactive_usecases[0]);
2695
2696 ALOGV("%s: num_usecase: %d", __func__, num_usecase);
2697 for (intract_uc_index = 0; intract_uc_index < num_usecase; intract_uc_index++) {
2698 if (!(adev->interactive_usecase_state & (0x1 << intract_uc_index))) {
2699 adev->interactive_usecase_state |= 0x1 << intract_uc_index;
2700 ret_uc = interactive_usecases[intract_uc_index];
2701 break;
2702 }
2703 }
2704
2705 ALOGV("%s: Interactive usecase is %d", __func__, ret_uc);
2706 return ret_uc;
2707}
2708
2709static void free_interactive_usecase(struct audio_device *adev,
2710 audio_usecase_t uc_id)
2711{
2712 unsigned int interact_uc_index;
2713 unsigned int num_usecase = sizeof(interactive_usecases)/sizeof(interactive_usecases[0]);
2714
2715 for (interact_uc_index = 0; interact_uc_index < num_usecase; interact_uc_index++) {
2716 if (interactive_usecases[interact_uc_index] == uc_id) {
2717 adev->interactive_usecase_state &= ~(0x1 << interact_uc_index);
2718 break;
2719 }
2720 }
2721 ALOGV("%s: free Interactive usecase %d", __func__, uc_id);
2722}
2723
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002724bool is_offload_usecase(audio_usecase_t uc_id)
2725{
2726 unsigned int i;
2727 for (i = 0; i < sizeof(offload_usecases)/sizeof(offload_usecases[0]); i++) {
2728 if (uc_id == offload_usecases[i])
2729 return true;
2730 }
2731 return false;
2732}
2733
Dhananjay Kumarac341582017-02-23 23:42:25 +05302734static audio_usecase_t get_offload_usecase(struct audio_device *adev, bool is_compress)
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002735{
vivek mehta446c3962015-09-14 10:57:35 -07002736 audio_usecase_t ret_uc = USECASE_INVALID;
2737 unsigned int offload_uc_index;
Alexy Josephb1379942016-01-29 15:49:38 -08002738 unsigned int num_usecase = sizeof(offload_usecases)/sizeof(offload_usecases[0]);
vivek mehta446c3962015-09-14 10:57:35 -07002739 if (!adev->multi_offload_enable) {
Dhananjay Kumarac341582017-02-23 23:42:25 +05302740 if (!is_compress)
vivek mehta446c3962015-09-14 10:57:35 -07002741 ret_uc = USECASE_AUDIO_PLAYBACK_OFFLOAD2;
2742 else
2743 ret_uc = USECASE_AUDIO_PLAYBACK_OFFLOAD;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002744
vivek mehta446c3962015-09-14 10:57:35 -07002745 pthread_mutex_lock(&adev->lock);
2746 if (get_usecase_from_list(adev, ret_uc) != NULL)
2747 ret_uc = USECASE_INVALID;
2748 pthread_mutex_unlock(&adev->lock);
2749
2750 return ret_uc;
2751 }
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002752
2753 ALOGV("%s: num_usecase: %d", __func__, num_usecase);
vivek mehta446c3962015-09-14 10:57:35 -07002754 for (offload_uc_index = 0; offload_uc_index < num_usecase; offload_uc_index++) {
2755 if (!(adev->offload_usecases_state & (0x1 << offload_uc_index))) {
2756 adev->offload_usecases_state |= 0x1 << offload_uc_index;
2757 ret_uc = offload_usecases[offload_uc_index];
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002758 break;
2759 }
2760 }
vivek mehta446c3962015-09-14 10:57:35 -07002761
2762 ALOGV("%s: offload usecase is %d", __func__, ret_uc);
2763 return ret_uc;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002764}
2765
2766static void free_offload_usecase(struct audio_device *adev,
2767 audio_usecase_t uc_id)
2768{
vivek mehta446c3962015-09-14 10:57:35 -07002769 unsigned int offload_uc_index;
Alexy Josephb1379942016-01-29 15:49:38 -08002770 unsigned int num_usecase = sizeof(offload_usecases)/sizeof(offload_usecases[0]);
vivek mehta446c3962015-09-14 10:57:35 -07002771
2772 if (!adev->multi_offload_enable)
2773 return;
2774
2775 for (offload_uc_index = 0; offload_uc_index < num_usecase; offload_uc_index++) {
2776 if (offload_usecases[offload_uc_index] == uc_id) {
2777 adev->offload_usecases_state &= ~(0x1 << offload_uc_index);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002778 break;
2779 }
2780 }
2781 ALOGV("%s: free offload usecase %d", __func__, uc_id);
2782}
2783
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002784static void *offload_thread_loop(void *context)
2785{
2786 struct stream_out *out = (struct stream_out *) context;
2787 struct listnode *item;
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08002788 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002789
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002790 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
2791 set_sched_policy(0, SP_FOREGROUND);
2792 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
2793
2794 ALOGV("%s", __func__);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07002795 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002796 for (;;) {
2797 struct offload_cmd *cmd = NULL;
2798 stream_callback_event_t event;
2799 bool send_callback = false;
2800
2801 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
2802 __func__, list_empty(&out->offload_cmd_list),
2803 out->offload_state);
2804 if (list_empty(&out->offload_cmd_list)) {
2805 ALOGV("%s SLEEPING", __func__);
2806 pthread_cond_wait(&out->offload_cond, &out->lock);
2807 ALOGV("%s RUNNING", __func__);
2808 continue;
2809 }
2810
2811 item = list_head(&out->offload_cmd_list);
2812 cmd = node_to_item(item, struct offload_cmd, node);
2813 list_remove(item);
2814
2815 ALOGVV("%s STATE %d CMD %d out->compr %p",
2816 __func__, out->offload_state, cmd->cmd, out->compr);
2817
2818 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
2819 free(cmd);
2820 break;
2821 }
2822
Haynes Mathew Georgeee5836f2017-11-21 18:02:10 -08002823 // allow OFFLOAD_CMD_ERROR reporting during standby
2824 // this is needed to handle failures during compress_open
2825 // Note however that on a pause timeout, the stream is closed
2826 // and no offload usecase will be active. Therefore this
2827 // special case is needed for compress_open failures alone
2828 if (cmd->cmd != OFFLOAD_CMD_ERROR &&
2829 out->compr == NULL) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002830 ALOGE("%s: Compress handle is NULL", __func__);
Haynes Mathew Georgea9abb202016-06-02 14:13:20 -07002831 free(cmd);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002832 pthread_cond_signal(&out->cond);
2833 continue;
2834 }
2835 out->offload_thread_blocked = true;
2836 pthread_mutex_unlock(&out->lock);
2837 send_callback = false;
2838 switch(cmd->cmd) {
2839 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07002840 ALOGD("copl(%p):calling compress_wait", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002841 compress_wait(out->compr, -1);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07002842 ALOGD("copl(%p):out of compress_wait", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002843 send_callback = true;
2844 event = STREAM_CBK_EVENT_WRITE_READY;
2845 break;
2846 case OFFLOAD_CMD_PARTIAL_DRAIN:
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08002847 ret = compress_next_track(out->compr);
Sidipotu Ashok55820562014-02-10 16:16:38 +05302848 if(ret == 0) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07002849 ALOGD("copl(%p):calling compress_partial_drain", out);
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05302850 ret = compress_partial_drain(out->compr);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07002851 ALOGD("copl(%p):out of compress_partial_drain", out);
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05302852 if (ret < 0)
2853 ret = -errno;
Sidipotu Ashok55820562014-02-10 16:16:38 +05302854 }
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05302855 else if (ret == -ETIMEDOUT)
Aniket Kumar Lata3570fb12017-11-08 15:53:44 -08002856 ret = compress_drain(out->compr);
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08002857 else
2858 ALOGE("%s: Next track returned error %d",__func__, ret);
Aniket Kumar Lata3570fb12017-11-08 15:53:44 -08002859 if (-ENETRESET != ret && !(-EINTR == ret &&
2860 CARD_STATUS_OFFLINE == out->card_status)) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05302861 send_callback = true;
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05302862 pthread_mutex_lock(&out->lock);
2863 out->send_new_metadata = 1;
2864 out->send_next_track_params = true;
2865 pthread_mutex_unlock(&out->lock);
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05302866 event = STREAM_CBK_EVENT_DRAIN_READY;
2867 ALOGV("copl(%p):send drain callback, ret %d", out, ret);
2868 } else
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05302869 ALOGI("%s: Block drain ready event during SSR", __func__);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002870 break;
2871 case OFFLOAD_CMD_DRAIN:
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07002872 ALOGD("copl(%p):calling compress_drain", out);
Aniket Kumar Lata3570fb12017-11-08 15:53:44 -08002873 ret = compress_drain(out->compr);
2874 ALOGD("copl(%p):out of compress_drain", out);
2875 // EINTR check avoids drain interruption due to SSR
2876 if (-ENETRESET != ret && !(-EINTR == ret &&
2877 CARD_STATUS_OFFLINE == out->card_status)) {
2878 send_callback = true;
2879 event = STREAM_CBK_EVENT_DRAIN_READY;
2880 } else
2881 ALOGI("%s: Block drain ready event during SSR", __func__);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002882 break;
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05302883 case OFFLOAD_CMD_ERROR:
2884 ALOGD("copl(%p): sending error callback to AF", out);
2885 send_callback = true;
2886 event = STREAM_CBK_EVENT_ERROR;
2887 break;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002888 default:
2889 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
2890 break;
2891 }
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07002892 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002893 out->offload_thread_blocked = false;
2894 pthread_cond_signal(&out->cond);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002895 if (send_callback && out->client_callback) {
2896 ALOGVV("%s: sending client_callback event %d", __func__, event);
2897 out->client_callback(event, NULL, out->client_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -07002898 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002899 free(cmd);
2900 }
2901
2902 pthread_cond_signal(&out->cond);
2903 while (!list_empty(&out->offload_cmd_list)) {
2904 item = list_head(&out->offload_cmd_list);
2905 list_remove(item);
2906 free(node_to_item(item, struct offload_cmd, node));
2907 }
2908 pthread_mutex_unlock(&out->lock);
2909
2910 return NULL;
2911}
2912
2913static int create_offload_callback_thread(struct stream_out *out)
2914{
2915 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
2916 list_init(&out->offload_cmd_list);
2917 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
2918 offload_thread_loop, out);
2919 return 0;
2920}
2921
2922static int destroy_offload_callback_thread(struct stream_out *out)
2923{
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07002924 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002925 stop_compressed_output_l(out);
2926 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
2927
2928 pthread_mutex_unlock(&out->lock);
2929 pthread_join(out->offload_thread, (void **) NULL);
2930 pthread_cond_destroy(&out->offload_cond);
2931
2932 return 0;
2933}
2934
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002935static int stop_output_stream(struct stream_out *out)
2936{
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05302937 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002938 struct audio_usecase *uc_info;
2939 struct audio_device *adev = out->dev;
2940
Eric Laurent994a6932013-07-17 11:51:42 -07002941 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002942 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002943 uc_info = get_usecase_from_list(adev, out->usecase);
2944 if (uc_info == NULL) {
2945 ALOGE("%s: Could not find the usecase (%d) in the list",
2946 __func__, out->usecase);
2947 return -EINVAL;
2948 }
2949
Derek Chencdd17c72014-11-24 12:39:14 -08002950 if (uc_info->out_snd_device != SND_DEVICE_NONE) {
2951 if (audio_extn_ext_hw_plugin_usecase_stop(adev->ext_hw_plugin, uc_info))
2952 ALOGE("%s: failed to stop ext hw plugin", __func__);
2953 }
2954
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07002955 if (is_offload_usecase(out->usecase) &&
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05302956 !(audio_extn_passthru_is_passthrough_stream(out))) {
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08002957 if (adev->visualizer_stop_output != NULL)
2958 adev->visualizer_stop_output(out->handle, out->pcm_device_id);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08002959
2960 audio_extn_dts_remove_state_notifier_node(out->usecase);
2961
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08002962 if (adev->offload_effects_stop_output != NULL)
2963 adev->offload_effects_stop_output(out->handle, out->pcm_device_id);
2964 }
Eric Laurentc4aef752013-09-12 17:45:53 -07002965
Arun Mirpuridbef0c72018-09-12 18:36:10 -07002966 if (out->usecase == USECASE_INCALL_MUSIC_UPLINK ||
2967 out->usecase == USECASE_INCALL_MUSIC_UPLINK2)
Arun Mirpurief53ce52018-09-11 18:00:09 -07002968 voice_set_device_mute_flag(adev, false);
2969
Eric Laurent150dbfe2013-02-27 14:31:02 -08002970 /* 1. Get and set stream specific mixer controls */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07002971 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002972
2973 /* 2. Disable the rx device */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07002974 disable_snd_device(adev, uc_info->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002975
Xiaojun Sang785b5da2017-08-03 15:52:29 +08002976 if (is_offload_usecase(out->usecase)) {
2977 audio_enable_asm_bit_width_enforce_mode(adev->mixer,
2978 adev->dsp_bit_width_enforce_mode,
2979 false);
2980 }
Garmond Leung5fd0b552018-04-17 11:56:12 -07002981 if (audio_is_usb_out_device(out->devices & AUDIO_DEVICE_OUT_ALL_USB)) {
2982 ret = audio_extn_usb_check_and_set_svc_int(uc_info,
2983 false);
2984
2985 if (ret != 0)
2986 check_usecases_codec_backend(adev, uc_info, uc_info->out_snd_device);
2987 /* default service interval was successfully updated,
2988 reopen USB backend with new service interval */
2989 ret = 0;
2990 }
Xiaojun Sang785b5da2017-08-03 15:52:29 +08002991
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002992 list_remove(&uc_info->list);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05302993 out->started = 0;
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07002994 if (is_offload_usecase(out->usecase) &&
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05302995 (audio_extn_passthru_is_passthrough_stream(out))) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07002996 ALOGV("Disable passthrough , reset mixer to pcm");
2997 /* NO_PASSTHROUGH */
2998 out->compr_config.codec->compr_passthr = 0;
Mingming Yin21854652016-04-13 11:54:02 -07002999 audio_extn_passthru_on_stop(out);
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07003000 audio_extn_dolby_set_dap_bypass(adev, DAP_STATE_ON);
3001 }
Eric Laurent07eeafd2013-10-06 12:52:49 -07003002
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05303003 /* Must be called after removing the usecase from list */
3004 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
Md Mansoor Ahmeddb1b4f92018-01-25 18:56:31 +05303005 audio_extn_keep_alive_start(KEEP_ALIVE_OUT_HDMI);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05303006
Manish Dewangan21a850a2017-08-14 12:03:55 +05303007 if (out->ip_hdlr_handle) {
Naresh Tanniru85819452017-05-04 18:55:45 -07003008 ret = audio_extn_ip_hdlr_intf_close(out->ip_hdlr_handle, true, out);
3009 if (ret < 0)
3010 ALOGE("%s: audio_extn_ip_hdlr_intf_close failed %d",__func__, ret);
3011 }
3012
Garmond Leung5fd0b552018-04-17 11:56:12 -07003013 free(uc_info);
Eric Laurent994a6932013-07-17 11:51:42 -07003014 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003015 return ret;
3016}
3017
3018int start_output_stream(struct stream_out *out)
3019{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003020 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003021 struct audio_usecase *uc_info;
3022 struct audio_device *adev = out->dev;
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08003023 char mixer_ctl_name[128];
3024 struct mixer_ctl *ctl = NULL;
3025 char* perf_mode[] = {"ULL", "ULL_PP", "LL"};
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303026 bool a2dp_combo = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003027
Haynes Mathew George380745d2017-10-04 15:27:45 -07003028 ATRACE_BEGIN("start_output_stream");
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07003029 if ((out->usecase < 0) || (out->usecase >= AUDIO_USECASE_MAX)) {
3030 ret = -EINVAL;
3031 goto error_config;
3032 }
3033
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05303034 ALOGD("%s: enter: stream(%p)usecase(%d: %s) devices(%#x)",
3035 __func__, &out->stream, out->usecase, use_case_table[out->usecase],
3036 out->devices);
Naresh Tanniru4c630392014-05-12 01:05:52 +05303037
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05303038 if (CARD_STATUS_OFFLINE == out->card_status ||
3039 CARD_STATUS_OFFLINE == adev->card_status) {
3040 ALOGW("out->card_status or adev->card_status offline, try again");
Dhanalakshmi Siddani4d57e992014-07-17 16:37:51 +05303041 ret = -EIO;
Naresh Tanniru4c630392014-05-12 01:05:52 +05303042 goto error_config;
3043 }
Naresh Tanniru4c630392014-05-12 01:05:52 +05303044
Arun Mirpuridbef0c72018-09-12 18:36:10 -07003045 //Update incall music usecase to reflect correct voice session
3046 if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
3047 ret = voice_extn_check_and_set_incall_music_usecase(adev, out);
3048 if (ret != 0) {
3049 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
3050 __func__, ret);
3051 goto error_config;
3052 }
3053 }
3054
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05303055 if (out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
3056 if (!audio_extn_a2dp_is_ready()) {
3057 if (out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303058 a2dp_combo = true;
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05303059 } else {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303060 if (!(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
3061 ALOGE("%s: A2DP profile is not ready, return error", __func__);
3062 ret = -EAGAIN;
3063 goto error_config;
3064 }
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05303065 }
3066 }
3067 }
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303068 if (out->devices & AUDIO_DEVICE_OUT_ALL_SCO) {
3069 if (!adev->bt_sco_on) {
3070 if (out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
3071 //combo usecase just by pass a2dp
3072 ALOGW("%s: SCO is not connected, route it to speaker", __func__);
3073 out->devices = AUDIO_DEVICE_OUT_SPEAKER;
3074 } else {
3075 ALOGE("%s: SCO profile is not ready, return error", __func__);
3076 ret = -EAGAIN;
3077 goto error_config;
3078 }
3079 }
3080 }
3081
Eric Laurentb23d5282013-05-14 15:27:20 -07003082 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003083 if (out->pcm_device_id < 0) {
3084 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
3085 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08003086 ret = -EINVAL;
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07003087 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003088 }
3089
3090 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07003091
3092 if (!uc_info) {
3093 ret = -ENOMEM;
3094 goto error_config;
3095 }
3096
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003097 uc_info->id = out->usecase;
3098 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08003099 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003100 uc_info->devices = out->devices;
3101 uc_info->in_snd_device = SND_DEVICE_NONE;
3102 uc_info->out_snd_device = SND_DEVICE_NONE;
Garmond Leung5fd0b552018-04-17 11:56:12 -07003103
3104 /* This must be called before adding this usecase to the list */
3105 if (audio_is_usb_out_device(out->devices & AUDIO_DEVICE_OUT_ALL_USB)) {
3106 audio_extn_usb_check_and_set_svc_int(uc_info, true);
3107 /* USB backend is not reopened immediately.
3108 This is eventually done as part of select_devices */
3109 }
3110
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08003111 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003112
Sudheer Papothifa9d2282015-09-17 01:53:25 +05303113 audio_extn_perf_lock_acquire(&adev->perf_lock_handle, 0,
3114 adev->perf_lock_opts,
3115 adev->perf_lock_opts_size);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05303116
3117 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Md Mansoor Ahmeddb1b4f92018-01-25 18:56:31 +05303118 audio_extn_keep_alive_stop(KEEP_ALIVE_OUT_HDMI);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05303119 if (audio_extn_passthru_is_enabled() &&
3120 audio_extn_passthru_is_passthrough_stream(out)) {
3121 audio_extn_passthru_on_start(out);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05303122 }
3123 }
3124
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303125 if ((out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) &&
3126 (!audio_extn_a2dp_is_ready())) {
3127 if (!a2dp_combo) {
3128 check_a2dp_restore_l(adev, out, false);
3129 } else {
3130 audio_devices_t dev = out->devices;
3131 out->devices = AUDIO_DEVICE_OUT_SPEAKER;
3132 select_devices(adev, out->usecase);
3133 out->devices = dev;
3134 }
3135 } else {
3136 select_devices(adev, out->usecase);
3137 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003138
Arun Mirpuridbef0c72018-09-12 18:36:10 -07003139 if (out->usecase == USECASE_INCALL_MUSIC_UPLINK ||
3140 out->usecase == USECASE_INCALL_MUSIC_UPLINK2)
Arun Mirpurief53ce52018-09-11 18:00:09 -07003141 voice_set_device_mute_flag(adev, true);
3142
Derek Chencdd17c72014-11-24 12:39:14 -08003143 if (uc_info->out_snd_device != SND_DEVICE_NONE) {
3144 if (audio_extn_ext_hw_plugin_usecase_start(adev->ext_hw_plugin, uc_info))
3145 ALOGE("%s: failed to start ext hw plugin", __func__);
3146 }
3147
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003148 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d) format(%#x)",
3149 __func__, adev->snd_card, out->pcm_device_id, out->config.format);
Haynes Mathew George16081042017-05-31 17:16:49 -07003150
3151 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
Arun Mirpuriebe78a72018-10-04 18:23:46 -07003152 ALOGD("%s: Starting MMAP stream", __func__);
Haynes Mathew George16081042017-05-31 17:16:49 -07003153 if (out->pcm == NULL || !pcm_is_ready(out->pcm)) {
3154 ALOGE("%s: pcm stream not ready", __func__);
3155 goto error_open;
3156 }
3157 ret = pcm_start(out->pcm);
3158 if (ret < 0) {
3159 ALOGE("%s: MMAP pcm_start failed ret %d", __func__, ret);
3160 goto error_open;
3161 }
Arun Mirpuriebe78a72018-10-04 18:23:46 -07003162 out_set_mmap_volume(&out->stream, out->volume_l, out->volume_r);
Haynes Mathew George16081042017-05-31 17:16:49 -07003163 } else if (!is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003164 unsigned int flags = PCM_OUT;
3165 unsigned int pcm_open_retry_count = 0;
3166 if (out->usecase == USECASE_AUDIO_PLAYBACK_AFE_PROXY) {
3167 flags |= PCM_MMAP | PCM_NOIRQ;
3168 pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT;
Haynes Mathew George5beddd42016-06-27 18:33:40 -07003169 } else if (out->realtime) {
Haynes Mathew George4ab3ba92017-12-11 14:49:43 -08003170 flags |= PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003171 } else
3172 flags |= PCM_MONOTONIC;
3173
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08003174 if ((adev->vr_audio_mode_enabled) &&
3175 (out->flags & AUDIO_OUTPUT_FLAG_RAW)) {
3176 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
3177 "PCM_Dev %d Topology", out->pcm_device_id);
3178 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
3179 if (!ctl) {
3180 ALOGI("%s: Could not get ctl for mixer cmd might be ULL - %s",
3181 __func__, mixer_ctl_name);
3182 } else {
3183 //if success use ULLPP
3184 ALOGI("%s: mixer ctrl %s succeeded setting up ULL for %d",
3185 __func__, mixer_ctl_name, out->pcm_device_id);
3186 //There is a still a possibility that some sessions
3187 // that request for FAST|RAW when 3D audio is active
3188 //can go through ULLPP. Ideally we expects apps to
3189 //listen to audio focus and stop concurrent playback
3190 //Also, we will look for mode flag (voice_in_communication)
3191 //before enabling the realtime flag.
3192 mixer_ctl_set_enum_by_string(ctl, perf_mode[1]);
3193 }
3194 }
3195
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003196 while (1) {
Haynes Mathew George380745d2017-10-04 15:27:45 -07003197 ATRACE_BEGIN("pcm_open");
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003198 out->pcm = pcm_open(adev->snd_card, out->pcm_device_id,
3199 flags, &out->config);
Haynes Mathew George380745d2017-10-04 15:27:45 -07003200 ATRACE_END();
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003201 if (out->pcm == NULL || !pcm_is_ready(out->pcm)) {
3202 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
3203 if (out->pcm != NULL) {
3204 pcm_close(out->pcm);
3205 out->pcm = NULL;
3206 }
3207 if (pcm_open_retry_count-- == 0) {
3208 ret = -EIO;
3209 goto error_open;
3210 }
3211 usleep(PROXY_OPEN_WAIT_TIME * 1000);
3212 continue;
3213 }
3214 break;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003215 }
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07003216
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07003217 ALOGV("%s: pcm_prepare", __func__);
3218 if (pcm_is_ready(out->pcm)) {
Haynes Mathew George380745d2017-10-04 15:27:45 -07003219 ATRACE_BEGIN("pcm_prepare");
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07003220 ret = pcm_prepare(out->pcm);
Haynes Mathew George380745d2017-10-04 15:27:45 -07003221 ATRACE_END();
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07003222 if (ret < 0) {
3223 ALOGE("%s: pcm_prepare returned %d", __func__, ret);
3224 pcm_close(out->pcm);
3225 out->pcm = NULL;
3226 goto error_open;
3227 }
3228 }
Divya Narayanan Poojary2c429012017-04-20 16:37:20 +05303229 platform_set_stream_channel_map(adev->platform, out->channel_mask,
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05303230 out->pcm_device_id, &out->channel_map_param.channel_map[0]);
Zhou Song2b8f28f2017-09-11 10:51:38 +08003231 // apply volume for voip playback after path is set up
3232 if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP)
3233 out_set_voip_volume(&out->stream, out->volume_l, out->volume_r);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003234 } else {
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -07003235 platform_set_stream_channel_map(adev->platform, out->channel_mask,
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05303236 out->pcm_device_id, &out->channel_map_param.channel_map[0]);
Xiaojun Sang785b5da2017-08-03 15:52:29 +08003237 audio_enable_asm_bit_width_enforce_mode(adev->mixer,
3238 adev->dsp_bit_width_enforce_mode,
3239 true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003240 out->pcm = NULL;
Haynes Mathew George380745d2017-10-04 15:27:45 -07003241 ATRACE_BEGIN("compress_open");
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08003242 out->compr = compress_open(adev->snd_card,
3243 out->pcm_device_id,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003244 COMPRESS_IN, &out->compr_config);
Haynes Mathew George380745d2017-10-04 15:27:45 -07003245 ATRACE_END();
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003246 if (out->compr && !is_compress_ready(out->compr)) {
Haynes Mathew Georgeee5836f2017-11-21 18:02:10 -08003247 ALOGE("%s: failed /w error %s", __func__, compress_get_error(out->compr));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003248 compress_close(out->compr);
3249 out->compr = NULL;
3250 ret = -EIO;
3251 goto error_open;
3252 }
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05303253 /* compress_open sends params of the track, so reset the flag here */
3254 out->is_compr_metadata_avail = false;
3255
Ben Rombergerd771a7c2017-02-22 18:05:17 -08003256 if (out->client_callback)
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003257 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07003258
Fred Oh3f43e742015-03-04 18:42:34 -08003259 /* Since small bufs uses blocking writes, a write will be blocked
3260 for the default max poll time (20s) in the event of an SSR.
3261 Reduce the poll time to observe and deal with SSR faster.
3262 */
Ashish Jain5106d362016-05-11 19:23:33 +05303263 if (!out->non_blocking) {
Fred Oh3f43e742015-03-04 18:42:34 -08003264 compress_set_max_poll_wait(out->compr, 1000);
3265 }
3266
Manish Dewangan69426c82017-01-30 17:35:36 +05303267 audio_extn_utils_compress_set_render_mode(out);
Manish Dewangan58229382017-02-02 15:48:41 +05303268 audio_extn_utils_compress_set_clk_rec_mode(uc_info);
Manish Dewangan69426c82017-01-30 17:35:36 +05303269
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08003270 audio_extn_dts_create_state_notifier_node(out->usecase);
3271 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
3272 popcount(out->channel_mask),
3273 out->playback_started);
3274
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08003275#ifdef DS1_DOLBY_DDP_ENABLED
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05303276 if (audio_extn_utils_is_dolby_format(out->format))
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08003277 audio_extn_dolby_send_ddp_endp_params(adev);
3278#endif
Preetam Singh Ranawatd18d8832017-02-08 17:34:54 +05303279 if (!(audio_extn_passthru_is_passthrough_stream(out)) &&
3280 (out->sample_rate != 176400 && out->sample_rate <= 192000)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07003281 if (adev->visualizer_start_output != NULL)
3282 adev->visualizer_start_output(out->handle, out->pcm_device_id);
3283 if (adev->offload_effects_start_output != NULL)
Ashish Jain5106d362016-05-11 19:23:33 +05303284 adev->offload_effects_start_output(out->handle, out->pcm_device_id, adev->mixer);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08003285 audio_extn_check_and_set_dts_hpx_state(adev);
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07003286 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003287 }
Haynes Mathew George5beddd42016-06-27 18:33:40 -07003288
3289 if (ret == 0) {
3290 register_out_stream(out);
3291 if (out->realtime) {
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07003292 if (out->pcm == NULL || !pcm_is_ready(out->pcm)) {
3293 ALOGE("%s: pcm stream not ready", __func__);
3294 goto error_open;
3295 }
Haynes Mathew George380745d2017-10-04 15:27:45 -07003296 ATRACE_BEGIN("pcm_start");
Haynes Mathew George5beddd42016-06-27 18:33:40 -07003297 ret = pcm_start(out->pcm);
Haynes Mathew George380745d2017-10-04 15:27:45 -07003298 ATRACE_END();
Haynes Mathew George5beddd42016-06-27 18:33:40 -07003299 if (ret < 0)
3300 goto error_open;
3301 }
3302 }
3303
Sudheer Papothifa9d2282015-09-17 01:53:25 +05303304 audio_extn_perf_lock_release(&adev->perf_lock_handle);
Ravi Kumar Alamandac7d9bef2015-09-30 22:27:26 -07003305 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07003306
Manish Dewangan21a850a2017-08-14 12:03:55 +05303307 if (out->ip_hdlr_handle) {
Vidyakumar Athota6d655882017-05-22 18:26:24 -07003308 ret = audio_extn_ip_hdlr_intf_open(out->ip_hdlr_handle, true, out, out->usecase);
Naresh Tanniru85819452017-05-04 18:55:45 -07003309 if (ret < 0)
3310 ALOGE("%s: audio_extn_ip_hdlr_intf_open failed %d",__func__, ret);
3311 }
3312
Vignesh Kulothungan3b5fae52017-09-25 12:16:30 -07003313 // consider a scenario where on pause lower layers are tear down.
3314 // so on resume, swap mixer control need to be sent only when
3315 // backend is active, hence rather than sending from enable device
3316 // sending it from start of streamtream
3317
3318 platform_set_swap_channels(adev, true);
3319
Haynes Mathew George380745d2017-10-04 15:27:45 -07003320 ATRACE_END();
Haynes Mathew George5beddd42016-06-27 18:33:40 -07003321 return ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003322error_open:
Sudheer Papothifa9d2282015-09-17 01:53:25 +05303323 audio_extn_perf_lock_release(&adev->perf_lock_handle);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003324 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08003325error_config:
Laxminath Kasam2cb4b752015-09-24 03:59:15 +05303326 /*
3327 * sleep 50ms to allow sufficient time for kernel
3328 * drivers to recover incases like SSR.
3329 */
3330 usleep(50000);
Haynes Mathew George380745d2017-10-04 15:27:45 -07003331 ATRACE_END();
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08003332 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003333}
3334
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003335static int check_input_parameters(uint32_t sample_rate,
3336 audio_format_t format,
3337 int channel_count)
3338{
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003339 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003340
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05303341 if (((format != AUDIO_FORMAT_PCM_16_BIT) && (format != AUDIO_FORMAT_PCM_8_24_BIT) &&
3342 (format != AUDIO_FORMAT_PCM_24_BIT_PACKED) && (format != AUDIO_FORMAT_PCM_32_BIT) &&
3343 (format != AUDIO_FORMAT_PCM_FLOAT)) &&
Mingming Yine62d7842013-10-25 16:26:03 -07003344 !voice_extn_compress_voip_is_format_supported(format) &&
Haynes Mathew George484e8d22017-07-31 18:55:17 -07003345 !audio_extn_compr_cap_format_supported(format))
3346 ret = -EINVAL;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003347
3348 switch (channel_count) {
3349 case 1:
3350 case 2:
Chaithanya Krishna Bacharaju9955b162016-05-25 16:25:53 +05303351 case 3:
3352 case 4:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003353 case 6:
Karthikeyan Mani07faa602018-08-20 11:01:32 -07003354 case 8:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003355 break;
3356 default:
3357 ret = -EINVAL;
3358 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003359
3360 switch (sample_rate) {
3361 case 8000:
3362 case 11025:
3363 case 12000:
3364 case 16000:
3365 case 22050:
3366 case 24000:
3367 case 32000:
3368 case 44100:
3369 case 48000:
Haynes Mathew Georgec9253d12017-12-13 15:58:28 -08003370 case 88200:
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05303371 case 96000:
Haynes Mathew Georgec9253d12017-12-13 15:58:28 -08003372 case 176400:
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05303373 case 192000:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003374 break;
3375 default:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003376 ret = -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003377 }
3378
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003379 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003380}
3381
Naresh Tanniru04f71882018-06-26 17:46:22 +05303382
3383/** Add a value in a list if not already present.
3384 * @return true if value was successfully inserted or already present,
3385 * false if the list is full and does not contain the value.
3386 */
3387static bool register_uint(uint32_t value, uint32_t* list, size_t list_length) {
3388 for (size_t i = 0; i < list_length; i++) {
3389 if (list[i] == value) return true; // value is already present
3390 if (list[i] == 0) { // no values in this slot
3391 list[i] = value;
3392 return true; // value inserted
3393 }
3394 }
3395 return false; // could not insert value
3396}
3397
3398/** Add channel_mask in supported_channel_masks if not already present.
3399 * @return true if channel_mask was successfully inserted or already present,
3400 * false if supported_channel_masks is full and does not contain channel_mask.
3401 */
3402static void register_channel_mask(audio_channel_mask_t channel_mask,
3403 audio_channel_mask_t supported_channel_masks[static MAX_SUPPORTED_CHANNEL_MASKS]) {
3404 ALOGE_IF(!register_uint(channel_mask, supported_channel_masks, MAX_SUPPORTED_CHANNEL_MASKS),
3405 "%s: stream can not declare supporting its channel_mask %x", __func__, channel_mask);
3406}
3407
3408/** Add format in supported_formats if not already present.
3409 * @return true if format was successfully inserted or already present,
3410 * false if supported_formats is full and does not contain format.
3411 */
3412static void register_format(audio_format_t format,
3413 audio_format_t supported_formats[static MAX_SUPPORTED_FORMATS]) {
3414 ALOGE_IF(!register_uint(format, supported_formats, MAX_SUPPORTED_FORMATS),
3415 "%s: stream can not declare supporting its format %x", __func__, format);
3416}
3417/** Add sample_rate in supported_sample_rates if not already present.
3418 * @return true if sample_rate was successfully inserted or already present,
3419 * false if supported_sample_rates is full and does not contain sample_rate.
3420 */
3421static void register_sample_rate(uint32_t sample_rate,
3422 uint32_t supported_sample_rates[static MAX_SUPPORTED_SAMPLE_RATES]) {
3423 ALOGE_IF(!register_uint(sample_rate, supported_sample_rates, MAX_SUPPORTED_SAMPLE_RATES),
3424 "%s: stream can not declare supporting its sample rate %x", __func__, sample_rate);
3425}
3426
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003427static size_t get_input_buffer_size(uint32_t sample_rate,
3428 audio_format_t format,
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07003429 int channel_count,
3430 bool is_low_latency)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003431{
3432 size_t size = 0;
3433
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07003434 if (check_input_parameters(sample_rate, format, channel_count) != 0)
3435 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003436
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07003437 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07003438 if (is_low_latency)
3439 size = configured_low_latency_capture_period_size;
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05303440
3441 size *= audio_bytes_per_sample(format) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003442
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07003443 /* make sure the size is multiple of 32 bytes
3444 * At 48 kHz mono 16-bit PCM:
3445 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
3446 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
3447 */
3448 size += 0x1f;
3449 size &= ~0x1f;
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07003450
3451 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003452}
3453
Ashish Jain058165c2016-09-28 23:18:48 +05303454static size_t get_output_period_size(uint32_t sample_rate,
3455 audio_format_t format,
3456 int channel_count,
3457 int duration /*in millisecs*/)
3458{
3459 size_t size = 0;
3460 uint32_t bytes_per_sample = audio_bytes_per_sample(format);
3461
3462 if ((duration == 0) || (sample_rate == 0) ||
3463 (bytes_per_sample == 0) || (channel_count == 0)) {
3464 ALOGW("Invalid config duration %d sr %d bps %d ch %d", duration, sample_rate,
3465 bytes_per_sample, channel_count);
3466 return -EINVAL;
3467 }
3468
3469 size = (sample_rate *
3470 duration *
3471 bytes_per_sample *
3472 channel_count) / 1000;
3473 /*
3474 * To have same PCM samples for all channels, the buffer size requires to
3475 * be multiple of (number of channels * bytes per sample)
3476 * For writes to succeed, the buffer must be written at address which is multiple of 32
3477 */
3478 size = ALIGN(size, (bytes_per_sample * channel_count * 32));
3479
3480 return (size/(channel_count * bytes_per_sample));
3481}
3482
Zhou Song48453a02018-01-10 17:50:59 +08003483static uint64_t get_actual_pcm_frames_rendered(struct stream_out *out, struct timespec *timestamp)
Ashish Jain5106d362016-05-11 19:23:33 +05303484{
3485 uint64_t actual_frames_rendered = 0;
3486 size_t kernel_buffer_size = out->compr_config.fragment_size * out->compr_config.fragments;
3487
3488 /* This adjustment accounts for buffering after app processor.
3489 * It is based on estimated DSP latency per use case, rather than exact.
3490 */
3491 int64_t platform_latency = platform_render_latency(out->usecase) *
3492 out->sample_rate / 1000000LL;
3493
Zhou Song48453a02018-01-10 17:50:59 +08003494 pthread_mutex_lock(&out->position_query_lock);
Ashish Jain5106d362016-05-11 19:23:33 +05303495 /* not querying actual state of buffering in kernel as it would involve an ioctl call
3496 * which then needs protection, this causes delay in TS query for pcm_offload usecase
3497 * hence only estimate.
3498 */
3499 int64_t signed_frames = out->written - kernel_buffer_size;
3500
3501 signed_frames = signed_frames / (audio_bytes_per_sample(out->format) * popcount(out->channel_mask)) - platform_latency;
3502
Zhou Song48453a02018-01-10 17:50:59 +08003503 if (signed_frames > 0) {
Ashish Jain5106d362016-05-11 19:23:33 +05303504 actual_frames_rendered = signed_frames;
Zhou Song48453a02018-01-10 17:50:59 +08003505 if (timestamp != NULL )
3506 *timestamp = out->writeAt;
3507 } else if (timestamp != NULL) {
3508 clock_gettime(CLOCK_MONOTONIC, timestamp);
3509 }
3510 pthread_mutex_unlock(&out->position_query_lock);
Ashish Jain5106d362016-05-11 19:23:33 +05303511
3512 ALOGVV("%s signed frames %lld out_written %lld kernel_buffer_size %d"
3513 "bytes/sample %zu channel count %d", __func__,(long long int)signed_frames,
3514 (long long int)out->written, (int)kernel_buffer_size,
3515 audio_bytes_per_sample(out->compr_config.codec->format),
3516 popcount(out->channel_mask));
3517
3518 return actual_frames_rendered;
3519}
3520
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003521static uint32_t out_get_sample_rate(const struct audio_stream *stream)
3522{
3523 struct stream_out *out = (struct stream_out *)stream;
3524
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003525 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003526}
3527
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07003528static int out_set_sample_rate(struct audio_stream *stream __unused,
3529 uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003530{
3531 return -ENOSYS;
3532}
3533
3534static size_t out_get_buffer_size(const struct audio_stream *stream)
3535{
3536 struct stream_out *out = (struct stream_out *)stream;
3537
Varun Balaraje49253e2017-07-06 19:48:56 +05303538 if (is_interactive_usecase(out->usecase)) {
Sri Karri27279e12017-08-07 16:05:20 +05303539 return out->config.period_size * out->config.period_count;
Varun Balaraje49253e2017-07-06 19:48:56 +05303540 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Naresh Tanniruee3499a2017-01-05 14:05:35 +05303541 if (out->flags & AUDIO_OUTPUT_FLAG_TIMESTAMP)
3542 return out->compr_config.fragment_size - sizeof(struct snd_codec_metadata);
3543 else
3544 return out->compr_config.fragment_size;
3545 } else if(out->usecase == USECASE_COMPRESS_VOIP_CALL)
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08003546 return voice_extn_compress_voip_out_get_buffer_size(out);
Vikram Panduranga93f080e2017-06-07 18:16:14 -07003547 else if(out->usecase == USECASE_AUDIO_PLAYBACK_VOIP)
3548 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 +05303549 else if (is_offload_usecase(out->usecase) &&
3550 out->flags == AUDIO_OUTPUT_FLAG_DIRECT)
Ashish Jain83a6cc22016-06-28 14:34:17 +05303551 return out->hal_fragment_size;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003552
Haynes Mathew George5beddd42016-06-27 18:33:40 -07003553 return out->config.period_size * out->af_period_multiplier *
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07003554 audio_stream_out_frame_size((const struct audio_stream_out *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003555}
3556
3557static uint32_t out_get_channels(const struct audio_stream *stream)
3558{
3559 struct stream_out *out = (struct stream_out *)stream;
3560
3561 return out->channel_mask;
3562}
3563
3564static audio_format_t out_get_format(const struct audio_stream *stream)
3565{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003566 struct stream_out *out = (struct stream_out *)stream;
3567
3568 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003569}
3570
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07003571static int out_set_format(struct audio_stream *stream __unused,
3572 audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003573{
3574 return -ENOSYS;
3575}
3576
3577static int out_standby(struct audio_stream *stream)
3578{
3579 struct stream_out *out = (struct stream_out *)stream;
3580 struct audio_device *adev = out->dev;
Haynes Mathew George16081042017-05-31 17:16:49 -07003581 bool do_stop = true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003582
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05303583 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
3584 stream, out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003585
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07003586 lock_output_stream(out);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003587 if (!out->standby) {
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07003588 if (adev->adm_deregister_stream)
3589 adev->adm_deregister_stream(adev->adm_data, out->handle);
3590
Haynes Mathew George7fce0a52016-06-23 18:22:27 -07003591 if (is_offload_usecase(out->usecase))
3592 stop_compressed_output_l(out);
3593
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08003594 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003595 out->standby = true;
Zhou Songa8895042016-07-05 17:54:22 +08003596 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
3597 voice_extn_compress_voip_close_output_stream(stream);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303598 out->started = 0;
Zhou Songa8895042016-07-05 17:54:22 +08003599 pthread_mutex_unlock(&adev->lock);
3600 pthread_mutex_unlock(&out->lock);
3601 ALOGD("VOIP output entered standby");
3602 return 0;
3603 } else if (!is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003604 if (out->pcm) {
3605 pcm_close(out->pcm);
3606 out->pcm = NULL;
3607 }
Haynes Mathew George16081042017-05-31 17:16:49 -07003608 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
3609 do_stop = out->playback_started;
3610 out->playback_started = false;
3611 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003612 } else {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07003613 ALOGD("copl(%p):standby", out);
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05303614 out->send_next_track_params = false;
3615 out->is_compr_metadata_avail = false;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07003616 out->gapless_mdata.encoder_delay = 0;
3617 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07003618 if (out->compr != NULL) {
3619 compress_close(out->compr);
3620 out->compr = NULL;
3621 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08003622 }
Haynes Mathew George16081042017-05-31 17:16:49 -07003623 if (do_stop) {
3624 stop_output_stream(out);
3625 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08003626 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003627 }
3628 pthread_mutex_unlock(&out->lock);
Ashish Jainbbce4322016-02-16 13:25:27 +05303629 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003630 return 0;
3631}
3632
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05303633static int out_on_error(struct audio_stream *stream)
3634{
3635 struct stream_out *out = (struct stream_out *)stream;
Ben Rombergerfd02a2f2018-09-17 10:23:23 -07003636 int status = 0;
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05303637
3638 lock_output_stream(out);
Haynes Mathew Georgeee5836f2017-11-21 18:02:10 -08003639 // always send CMD_ERROR for offload streams, this
3640 // is needed e.g. when SSR happens within compress_open
3641 // since the stream is active, offload_callback_thread is also active.
3642 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
3643 stop_compressed_output_l(out);
Ben Rombergerfd02a2f2018-09-17 10:23:23 -07003644 }
3645 pthread_mutex_unlock(&out->lock);
3646
3647 status = out_standby(&out->stream.common);
3648
3649 lock_output_stream(out);
3650 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Haynes Mathew Georgeee5836f2017-11-21 18:02:10 -08003651 send_offload_cmd_l(out, OFFLOAD_CMD_ERROR);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05303652 }
3653 pthread_mutex_unlock(&out->lock);
3654
Ben Rombergerfd02a2f2018-09-17 10:23:23 -07003655 return status;
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05303656}
3657
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303658/*
3659 *standby implementation without locks, assumes that the callee already
3660 *has taken adev and out lock.
3661 */
3662int out_standby_l(struct audio_stream *stream)
3663{
3664 struct stream_out *out = (struct stream_out *)stream;
3665 struct audio_device *adev = out->dev;
3666
3667 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
3668 stream, out->usecase, use_case_table[out->usecase]);
3669
3670 if (!out->standby) {
Haynes Mathew George380745d2017-10-04 15:27:45 -07003671 ATRACE_BEGIN("out_standby_l");
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303672 if (adev->adm_deregister_stream)
3673 adev->adm_deregister_stream(adev->adm_data, out->handle);
3674
3675 if (is_offload_usecase(out->usecase))
3676 stop_compressed_output_l(out);
3677
3678 out->standby = true;
3679 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
3680 voice_extn_compress_voip_close_output_stream(stream);
3681 out->started = 0;
3682 ALOGD("VOIP output entered standby");
Haynes Mathew George380745d2017-10-04 15:27:45 -07003683 ATRACE_END();
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303684 return 0;
3685 } else if (!is_offload_usecase(out->usecase)) {
3686 if (out->pcm) {
3687 pcm_close(out->pcm);
3688 out->pcm = NULL;
3689 }
3690 } else {
3691 ALOGD("copl(%p):standby", out);
3692 out->send_next_track_params = false;
3693 out->is_compr_metadata_avail = false;
3694 out->gapless_mdata.encoder_delay = 0;
3695 out->gapless_mdata.encoder_padding = 0;
3696 if (out->compr != NULL) {
3697 compress_close(out->compr);
3698 out->compr = NULL;
3699 }
3700 }
3701 stop_output_stream(out);
Haynes Mathew George380745d2017-10-04 15:27:45 -07003702 ATRACE_END();
Ashish Jain1b9b30c2017-05-18 20:57:40 +05303703 }
3704 ALOGD("%s: exit", __func__);
3705 return 0;
3706}
3707
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07003708static int out_dump(const struct audio_stream *stream __unused,
3709 int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003710{
3711 return 0;
3712}
3713
Haynes Mathew George352f27b2013-07-26 00:00:15 -07003714static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
3715{
3716 int ret = 0;
3717 char value[32];
ApurupaPattapu2e084df2013-12-18 15:47:59 -08003718
Haynes Mathew George352f27b2013-07-26 00:00:15 -07003719 if (!out || !parms) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08003720 ALOGE("%s: return invalid ",__func__);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07003721 return -EINVAL;
3722 }
3723
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +05303724 ret = audio_extn_parse_compress_metadata(out, parms);
Weiyin Jiang18ac4e92015-03-15 15:03:40 +08003725
Haynes Mathew George352f27b2013-07-26 00:00:15 -07003726 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
3727 if (ret >= 0) {
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +05303728 out->gapless_mdata.encoder_delay = atoi(value); //whats a good limit check?
Haynes Mathew George352f27b2013-07-26 00:00:15 -07003729 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07003730 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
3731 if (ret >= 0) {
Satya Krishna Pindiproli70471602015-04-24 19:12:43 +05303732 out->gapless_mdata.encoder_padding = atoi(value);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07003733 }
3734
Haynes Mathew George352f27b2013-07-26 00:00:15 -07003735 ALOGV("%s new encoder delay %u and padding %u", __func__,
3736 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
3737
3738 return 0;
3739}
3740
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003741static bool output_drives_call(struct audio_device *adev, struct stream_out *out)
3742{
3743 return out == adev->primary_output || out == adev->voice_tx_output;
3744}
Haynes Mathew George352f27b2013-07-26 00:00:15 -07003745
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05303746// note: this call is safe only if the stream_cb is
3747// removed first in close_output_stream (as is done now).
3748static void out_snd_mon_cb(void * stream, struct str_parms * parms)
3749{
3750 if (!stream || !parms)
3751 return;
3752
3753 struct stream_out *out = (struct stream_out *)stream;
3754 struct audio_device *adev = out->dev;
3755
3756 card_status_t status;
3757 int card;
3758 if (parse_snd_card_status(parms, &card, &status) < 0)
3759 return;
3760
3761 pthread_mutex_lock(&adev->lock);
3762 bool valid_cb = (card == adev->snd_card);
3763 pthread_mutex_unlock(&adev->lock);
3764
3765 if (!valid_cb)
3766 return;
3767
3768 lock_output_stream(out);
3769 if (out->card_status != status)
3770 out->card_status = status;
3771 pthread_mutex_unlock(&out->lock);
3772
3773 ALOGI("out_snd_mon_cb for card %d usecase %s, status %s", card,
3774 use_case_table[out->usecase],
3775 status == CARD_STATUS_OFFLINE ? "offline" : "online");
3776
3777 if (status == CARD_STATUS_OFFLINE)
3778 out_on_error(stream);
3779
3780 return;
3781}
3782
Kevin Rocardfce19002017-08-07 19:21:36 -07003783static int get_alive_usb_card(struct str_parms* parms) {
3784 int card;
3785 if ((str_parms_get_int(parms, "card", &card) >= 0) &&
3786 !audio_extn_usb_alive(card)) {
3787 return card;
3788 }
3789 return -ENODEV;
3790}
3791
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003792static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
3793{
3794 struct stream_out *out = (struct stream_out *)stream;
3795 struct audio_device *adev = out->dev;
3796 struct str_parms *parms;
3797 char value[32];
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08003798 int ret = 0, val = 0, err;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303799 bool bypass_a2dp = false;
Garmond Leung5fd0b552018-04-17 11:56:12 -07003800 bool reconfig = false;
3801 unsigned long service_interval = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003802
sangwoobc677242013-08-08 16:53:43 +09003803 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003804 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003805 parms = str_parms_create_str(kvpairs);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05303806 if (!parms)
3807 goto error;
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08003808 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
3809 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003810 val = atoi(value);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07003811 lock_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08003812 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003813
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003814 /*
Weiyin Jiang4256eeb2016-05-19 13:28:30 +08003815 * When HDMI cable is unplugged the music playback is paused and
3816 * the policy manager sends routing=0. But the audioflinger continues
3817 * to write data until standby time (3sec). As the HDMI core is
3818 * turned off, the write gets blocked.
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003819 * Avoid this by routing audio to speaker until standby.
3820 */
Weiyin Jiang4256eeb2016-05-19 13:28:30 +08003821 if ((out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL) &&
3822 (val == AUDIO_DEVICE_NONE) &&
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05303823 !audio_extn_passthru_is_passthrough_stream(out) &&
Weiyin Jiang4256eeb2016-05-19 13:28:30 +08003824 (platform_get_edid_info(adev->platform) != 0) /* HDMI disconnected */) {
3825 val = AUDIO_DEVICE_OUT_SPEAKER;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003826 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303827 /*
3828 * When A2DP is disconnected the
3829 * music playback is paused and the policy manager sends routing=0
3830 * But the audioflingercontinues to write data until standby time
3831 * (3sec). As BT is turned off, the write gets blocked.
3832 * Avoid this by routing audio to speaker until standby.
3833 */
Naresh Tanniruf7e9e632016-11-04 14:54:20 -07003834 if ((out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) &&
Aniket Kumar Lata1fb11cb2017-09-08 13:48:55 -07003835 (val == AUDIO_DEVICE_NONE) &&
3836 !audio_extn_a2dp_is_ready()) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303837 val = AUDIO_DEVICE_OUT_SPEAKER;
3838 }
Sharad Sangled0a50b22018-04-05 23:28:32 +05303839 /*
3840 * When USB headset is disconnected the music platback paused
3841 * and the policy manager send routing=0. But if the USB is connected
3842 * back before the standby time, AFE is not closed and opened
3843 * when USB is connected back. So routing to speker will guarantee
3844 * AFE reconfiguration and AFE will be opend once USB is connected again
3845 */
3846 if ((out->devices & AUDIO_DEVICE_OUT_ALL_USB) &&
3847 (val == AUDIO_DEVICE_NONE) &&
3848 !audio_extn_usb_connected(parms)) {
3849 val = AUDIO_DEVICE_OUT_SPEAKER;
3850 }
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05303851 /* To avoid a2dp to sco overlapping / BT device improper state
3852 * check with BT lib about a2dp streaming support before routing
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05303853 */
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05303854 if (val & AUDIO_DEVICE_OUT_ALL_A2DP) {
3855 if (!audio_extn_a2dp_is_ready()) {
3856 if (val & AUDIO_DEVICE_OUT_SPEAKER) {
3857 //combo usecase just by pass a2dp
3858 ALOGW("%s: A2DP profile is not ready,routing to speaker only", __func__);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303859 bypass_a2dp = true;
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05303860 } else {
3861 ALOGE("%s: A2DP profile is not ready,ignoring routing request", __func__);
3862 /* update device to a2dp and don't route as BT returned error
3863 * However it is still possible a2dp routing called because
3864 * of current active device disconnection (like wired headset)
3865 */
3866 out->devices = val;
3867 pthread_mutex_unlock(&out->lock);
3868 pthread_mutex_unlock(&adev->lock);
3869 goto error;
3870 }
3871 }
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05303872 }
Kevin Rocardfce19002017-08-07 19:21:36 -07003873
3874 audio_devices_t new_dev = val;
3875
3876 // Workaround: If routing to an non existing usb device, fail gracefully
3877 // The routing request will otherwise block during 10 second
3878 int card;
3879 if (audio_is_usb_out_device(new_dev) &&
3880 (card = get_alive_usb_card(parms)) >= 0) {
3881
3882 ALOGW("out_set_parameters() ignoring rerouting to non existing USB card %d", card);
3883 pthread_mutex_unlock(&adev->lock);
3884 pthread_mutex_unlock(&out->lock);
3885 ret = -ENOSYS;
3886 goto routing_fail;
3887 }
3888
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003889 /*
3890 * select_devices() call below switches all the usecases on the same
3891 * backend to the new device. Refer to check_usecases_codec_backend() in
3892 * the select_devices(). But how do we undo this?
3893 *
3894 * For example, music playback is active on headset (deep-buffer usecase)
3895 * and if we go to ringtones and select a ringtone, low-latency usecase
3896 * will be started on headset+speaker. As we can't enable headset+speaker
3897 * and headset devices at the same time, select_devices() switches the music
3898 * playback to headset+speaker while starting low-lateny usecase for ringtone.
3899 * So when the ringtone playback is completed, how do we undo the same?
3900 *
3901 * We are relying on the out_set_parameters() call on deep-buffer output,
3902 * once the ringtone playback is ended.
3903 * NOTE: We should not check if the current devices are same as new devices.
3904 * Because select_devices() must be called to switch back the music
3905 * playback to headset.
3906 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08003907 if (val != 0) {
Haynes Mathew George5beddd42016-06-27 18:33:40 -07003908 audio_devices_t new_dev = val;
3909 bool same_dev = out->devices == new_dev;
3910 out->devices = new_dev;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003911
Ravi Kumar Alamandabe149392014-10-20 17:07:43 -07003912 if (output_drives_call(adev, out)) {
kunleiz61b9c2e2017-11-01 13:47:23 +08003913 if (!voice_is_call_state_active(adev)) {
Ravi Kumar Alamandabe149392014-10-20 17:07:43 -07003914 if (adev->mode == AUDIO_MODE_IN_CALL) {
3915 adev->current_call_output = out;
Garmond Leung5fd0b552018-04-17 11:56:12 -07003916 if (audio_is_usb_out_device(out->devices & AUDIO_DEVICE_OUT_ALL_USB)) {
3917 service_interval = audio_extn_usb_find_service_interval(true, true /*playback*/);
3918 audio_extn_usb_set_service_interval(true /*playback*/,
3919 service_interval,
3920 &reconfig);
3921 ALOGD("%s, svc_int(%ld),reconfig(%d)",__func__,service_interval, reconfig);
3922 }
3923 ret = voice_start_call(adev);
Ravi Kumar Alamandabe149392014-10-20 17:07:43 -07003924 }
3925 } else {
3926 adev->current_call_output = out;
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07003927 voice_update_devices_for_all_voice_usecases(adev);
Ravi Kumar Alamandabe149392014-10-20 17:07:43 -07003928 }
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08003929 }
Shiv Maliyappanahalli6b32c4c2015-11-04 18:10:20 -08003930
3931 if (!out->standby) {
Haynes Mathew George5beddd42016-06-27 18:33:40 -07003932 if (!same_dev) {
3933 ALOGV("update routing change");
Sudheer Papothi80266982016-08-16 02:36:18 +05303934 audio_extn_perf_lock_acquire(&adev->perf_lock_handle, 0,
3935 adev->perf_lock_opts,
3936 adev->perf_lock_opts_size);
Haynes Mathew George822b5492016-07-01 16:57:24 -07003937 if (adev->adm_on_routing_change)
3938 adev->adm_on_routing_change(adev->adm_data,
3939 out->handle);
Haynes Mathew George5beddd42016-06-27 18:33:40 -07003940 }
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303941 if (!bypass_a2dp) {
3942 select_devices(adev, out->usecase);
3943 } else {
3944 out->devices = AUDIO_DEVICE_OUT_SPEAKER;
3945 select_devices(adev, out->usecase);
3946 out->devices = new_dev;
3947 }
Vignesh Kulothungan3b5fae52017-09-25 12:16:30 -07003948
3949 if (!same_dev) {
3950 // on device switch force swap, lower functions will make sure
3951 // to check if swap is allowed or not.
3952 platform_set_swap_channels(adev, true);
Sudheer Papothi80266982016-08-16 02:36:18 +05303953 audio_extn_perf_lock_release(&adev->perf_lock_handle);
Vignesh Kulothungan3b5fae52017-09-25 12:16:30 -07003954 }
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303955 if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
3956 out->a2dp_compress_mute &&
Zhou Songc66eb7e2017-08-08 18:29:07 +08003957 (!(out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) || audio_extn_a2dp_is_ready())) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303958 pthread_mutex_lock(&out->compr_mute_lock);
3959 out->a2dp_compress_mute = false;
3960 out_set_compr_volume(&out->stream, out->volume_l, out->volume_r);
3961 pthread_mutex_unlock(&out->compr_mute_lock);
kunleizba786432018-01-26 15:31:59 +08003962 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP) {
3963 out_set_voip_volume(&out->stream, out->volume_l, out->volume_r);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303964 }
Shiv Maliyappanahalli6b32c4c2015-11-04 18:10:20 -08003965 }
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08003966 }
3967
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003968 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08003969 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003970 }
Kevin Rocardfce19002017-08-07 19:21:36 -07003971 routing_fail:
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07003972
3973 if (out == adev->primary_output) {
3974 pthread_mutex_lock(&adev->lock);
3975 audio_extn_set_parameters(adev, parms);
3976 pthread_mutex_unlock(&adev->lock);
3977 }
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003978 if (is_offload_usecase(out->usecase)) {
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07003979 lock_output_stream(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07003980 parse_compress_metadata(out, parms);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08003981
3982 audio_extn_dts_create_state_notifier_node(out->usecase);
3983 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
3984 popcount(out->channel_mask),
3985 out->playback_started);
3986
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08003987 pthread_mutex_unlock(&out->lock);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07003988 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07003989
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +05303990 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_PROFILE, value, sizeof(value));
3991 if (err >= 0) {
3992 strlcpy(out->profile, value, sizeof(out->profile));
3993 ALOGV("updating stream profile with value '%s'", out->profile);
3994 lock_output_stream(out);
3995 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
3996 &adev->streams_output_cfg_list,
Aalique Grahame65780b52017-09-27 14:59:56 -07003997 out->devices, out->flags, out->hal_op_format,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +05303998 out->sample_rate, out->bit_width,
3999 out->channel_mask, out->profile,
4000 &out->app_type_cfg);
4001 pthread_mutex_unlock(&out->lock);
4002 }
4003
Alexy Joseph98988832017-01-13 14:56:59 -08004004 //suspend, resume handling block
Aniket Kumar Lata932f4872017-11-06 18:29:44 -08004005 //remove QOS only if vendor.audio.hal.dynamic.qos.config.supported is set to true
4006 // and vendor.audio.hal.output.suspend.supported is set to true
4007 if (out->hal_output_suspend_supported && out->dynamic_pm_qos_config_supported) {
Alexy Joseph98988832017-01-13 14:56:59 -08004008 //check suspend parameter only for low latency and if the property
4009 //is enabled
4010 if (str_parms_get_str(parms, "suspend_playback", value, sizeof(value)) >= 0) {
4011 ALOGI("%s: got suspend_playback %s", __func__, value);
4012 lock_output_stream(out);
4013 if (!strncmp(value, "false", 5)) {
4014 //suspend_playback=false is supposed to set QOS value back to 75%
4015 //the mixer control sent with value Enable will achieve that
4016 ret = audio_route_apply_and_update_path(adev->audio_route, out->pm_qos_mixer_path);
4017 } else if (!strncmp (value, "true", 4)) {
4018 //suspend_playback=true is supposed to remove QOS value
4019 //resetting the mixer control will set the default value
4020 //for the mixer control which is Disable and this removes the QOS vote
4021 ret = audio_route_reset_and_update_path(adev->audio_route, out->pm_qos_mixer_path);
4022 } else {
4023 ALOGE("%s: Wrong value sent for suspend_playback, expected true/false,"
4024 " got %s", __func__, value);
4025 ret = -1;
4026 }
4027
4028 if (ret != 0) {
4029 ALOGE("%s: %s mixer ctl failed with %d, ignore suspend/resume setparams",
4030 __func__, out->pm_qos_mixer_path, ret);
4031 }
4032
4033 pthread_mutex_unlock(&out->lock);
4034 }
4035 }
4036 //end suspend, resume handling block
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004037 str_parms_destroy(parms);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05304038error:
Eric Laurent994a6932013-07-17 11:51:42 -07004039 ALOGV("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004040 return ret;
4041}
4042
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004043static bool stream_get_parameter_channels(struct str_parms *query,
4044 struct str_parms *reply,
4045 audio_channel_mask_t *supported_channel_masks) {
4046 int ret = -1;
4047 char value[512];
4048 bool first = true;
4049 size_t i, j;
4050
4051 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) {
4052 ret = 0;
4053 value[0] = '\0';
4054 i = 0;
4055 while (supported_channel_masks[i] != 0) {
4056 for (j = 0; j < ARRAY_SIZE(channels_name_to_enum_table); j++) {
4057 if (channels_name_to_enum_table[j].value == supported_channel_masks[i]) {
4058 if (!first)
Satya Krishna Pindiprolib22ac722017-10-09 15:44:16 +05304059 strlcat(value, "|", sizeof(value));
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004060
Satya Krishna Pindiprolib22ac722017-10-09 15:44:16 +05304061 strlcat(value, channels_name_to_enum_table[j].name, sizeof(value));
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004062 first = false;
4063 break;
4064 }
4065 }
4066 i++;
4067 }
4068 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
4069 }
4070 return ret == 0;
4071}
4072
4073static bool stream_get_parameter_formats(struct str_parms *query,
4074 struct str_parms *reply,
4075 audio_format_t *supported_formats) {
4076 int ret = -1;
4077 char value[256];
4078 size_t i, j;
4079 bool first = true;
4080
4081 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) {
4082 ret = 0;
4083 value[0] = '\0';
4084 i = 0;
4085 while (supported_formats[i] != 0) {
4086 for (j = 0; j < ARRAY_SIZE(formats_name_to_enum_table); j++) {
4087 if (formats_name_to_enum_table[j].value == supported_formats[i]) {
4088 if (!first) {
Satya Krishna Pindiprolib22ac722017-10-09 15:44:16 +05304089 strlcat(value, "|", sizeof(value));
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004090 }
Satya Krishna Pindiprolib22ac722017-10-09 15:44:16 +05304091 strlcat(value, formats_name_to_enum_table[j].name, sizeof(value));
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004092 first = false;
4093 break;
4094 }
4095 }
4096 i++;
4097 }
4098 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_FORMATS, value);
4099 }
4100 return ret == 0;
4101}
4102
4103static bool stream_get_parameter_rates(struct str_parms *query,
4104 struct str_parms *reply,
4105 uint32_t *supported_sample_rates) {
4106
4107 int i;
4108 char value[256];
4109 int ret = -1;
4110 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) {
4111 ret = 0;
4112 value[0] = '\0';
4113 i=0;
4114 int cursor = 0;
4115 while (supported_sample_rates[i]) {
4116 int avail = sizeof(value) - cursor;
4117 ret = snprintf(value + cursor, avail, "%s%d",
4118 cursor > 0 ? "|" : "",
4119 supported_sample_rates[i]);
4120 if (ret < 0 || ret >= avail) {
4121 // if cursor is at the last element of the array
4122 // overwrite with \0 is duplicate work as
4123 // snprintf already put a \0 in place.
4124 // else
4125 // we had space to write the '|' at value[cursor]
4126 // (which will be overwritten) or no space to fill
4127 // the first element (=> cursor == 0)
4128 value[cursor] = '\0';
4129 break;
4130 }
4131 cursor += ret;
4132 ++i;
4133 }
4134 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES,
4135 value);
4136 }
4137 return ret >= 0;
4138}
4139
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004140static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
4141{
4142 struct stream_out *out = (struct stream_out *)stream;
4143 struct str_parms *query = str_parms_create_str(keys);
Alexy Josephaee4fdd2016-01-29 13:02:07 -08004144 char *str = (char*) NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004145 char value[256];
4146 struct str_parms *reply = str_parms_create();
4147 size_t i, j;
4148 int ret;
4149 bool first = true;
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07004150
4151 if (!query || !reply) {
Alexy Josephaee4fdd2016-01-29 13:02:07 -08004152 if (reply) {
4153 str_parms_destroy(reply);
4154 }
4155 if (query) {
4156 str_parms_destroy(query);
4157 }
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07004158 ALOGE("out_get_parameters: failed to allocate mem for query or reply");
4159 return NULL;
4160 }
4161
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004162 ALOGV("%s: %s enter: keys - %s", __func__, use_case_table[out->usecase], keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004163 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
4164 if (ret >= 0) {
4165 value[0] = '\0';
4166 i = 0;
4167 while (out->supported_channel_masks[i] != 0) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004168 for (j = 0; j < ARRAY_SIZE(channels_name_to_enum_table); j++) {
4169 if (channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004170 if (!first) {
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -08004171 strlcat(value, "|", sizeof(value));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004172 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004173 strlcat(value, channels_name_to_enum_table[j].name, sizeof(value));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004174 first = false;
4175 break;
4176 }
4177 }
4178 i++;
4179 }
4180 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
4181 str = str_parms_to_str(reply);
4182 } else {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08004183 voice_extn_out_get_parameters(out, query, reply);
4184 str = str_parms_to_str(reply);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004185 }
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004186
Alexy Joseph62142aa2015-11-16 15:10:34 -08004187
4188 ret = str_parms_get_str(query, "is_direct_pcm_track", value, sizeof(value));
4189 if (ret >= 0) {
4190 value[0] = '\0';
Dhananjay Kumarac341582017-02-23 23:42:25 +05304191 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
4192 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Alexy Joseph62142aa2015-11-16 15:10:34 -08004193 ALOGV("in direct_pcm");
Satya Krishna Pindiprolib6655542017-07-03 19:38:19 +05304194 strlcat(value, "true", sizeof(value));
Alexy Joseph62142aa2015-11-16 15:10:34 -08004195 } else {
4196 ALOGV("not in direct_pcm");
Sharad Sangle3dd5a4a2015-12-10 18:39:17 +05304197 strlcat(value, "false", sizeof(value));
Alexy Joseph62142aa2015-11-16 15:10:34 -08004198 }
4199 str_parms_add_str(reply, "is_direct_pcm_track", value);
Alexy Josephaee4fdd2016-01-29 13:02:07 -08004200 if (str)
4201 free(str);
Alexy Joseph62142aa2015-11-16 15:10:34 -08004202 str = str_parms_to_str(reply);
4203 }
4204
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004205 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS, value, sizeof(value));
4206 if (ret >= 0) {
4207 value[0] = '\0';
4208 i = 0;
4209 first = true;
4210 while (out->supported_formats[i] != 0) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004211 for (j = 0; j < ARRAY_SIZE(formats_name_to_enum_table); j++) {
4212 if (formats_name_to_enum_table[j].value == out->supported_formats[i]) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004213 if (!first) {
Apoorv Raghuvanshi8880cac2015-02-06 15:33:49 -08004214 strlcat(value, "|", sizeof(value));
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004215 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004216 strlcat(value, formats_name_to_enum_table[j].name, sizeof(value));
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004217 first = false;
4218 break;
4219 }
4220 }
4221 i++;
4222 }
4223 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_FORMATS, value);
Alexy Josephaee4fdd2016-01-29 13:02:07 -08004224 if (str)
4225 free(str);
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004226 str = str_parms_to_str(reply);
4227 }
Mingming Yin3a941d42016-02-17 18:08:05 -08004228
4229 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES, value, sizeof(value));
4230 if (ret >= 0) {
4231 value[0] = '\0';
4232 i = 0;
4233 first = true;
4234 while (out->supported_sample_rates[i] != 0) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004235 for (j = 0; j < ARRAY_SIZE(out_sample_rates_name_to_enum_table); j++) {
4236 if (out_sample_rates_name_to_enum_table[j].value == out->supported_sample_rates[i]) {
Mingming Yin3a941d42016-02-17 18:08:05 -08004237 if (!first) {
4238 strlcat(value, "|", sizeof(value));
4239 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07004240 strlcat(value, out_sample_rates_name_to_enum_table[j].name, sizeof(value));
Mingming Yin3a941d42016-02-17 18:08:05 -08004241 first = false;
4242 break;
4243 }
4244 }
4245 i++;
4246 }
4247 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES, value);
4248 if (str)
4249 free(str);
4250 str = str_parms_to_str(reply);
4251 }
4252
Alexy Joseph98988832017-01-13 14:56:59 -08004253 if (str_parms_get_str(query, "supports_hw_suspend", value, sizeof(value)) >= 0) {
4254 //only low latency track supports suspend_resume
4255 str_parms_add_int(reply, "supports_hw_suspend",
Aniket Kumar Lata932f4872017-11-06 18:29:44 -08004256 (out->hal_output_suspend_supported));
Alexy Joseph98988832017-01-13 14:56:59 -08004257 if (str)
4258 free(str);
4259 str = str_parms_to_str(reply);
4260 }
4261
4262
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004263 str_parms_destroy(query);
4264 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07004265 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004266 return str;
4267}
4268
4269static uint32_t out_get_latency(const struct audio_stream_out *stream)
4270{
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004271 uint32_t period_ms;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004272 struct stream_out *out = (struct stream_out *)stream;
Alexy Josephaa54c872014-12-03 02:46:47 -08004273 uint32_t latency = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004274
Alexy Josephaa54c872014-12-03 02:46:47 -08004275 if (is_offload_usecase(out->usecase)) {
Manish Dewangan07de2142017-02-27 19:27:20 +05304276 lock_output_stream(out);
4277 latency = audio_extn_utils_compress_get_dsp_latency(out);
4278 pthread_mutex_unlock(&out->lock);
Haynes Mathew George16081042017-05-31 17:16:49 -07004279 } else if ((out->realtime) ||
4280 (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP)) {
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004281 // since the buffer won't be filled up faster than realtime,
4282 // return a smaller number
4283 if (out->config.rate)
4284 period_ms = (out->af_period_multiplier * out->config.period_size *
4285 1000) / (out->config.rate);
4286 else
4287 period_ms = 0;
4288 latency = period_ms + platform_render_latency(out->usecase)/1000;
Alexy Josephaa54c872014-12-03 02:46:47 -08004289 } else {
4290 latency = (out->config.period_count * out->config.period_size * 1000) /
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004291 (out->config.rate);
Alexy Josephaa54c872014-12-03 02:46:47 -08004292 }
4293
yidongh0515e042017-07-06 15:00:34 +08004294 if (AUDIO_DEVICE_OUT_ALL_A2DP & out->devices)
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08004295 latency += audio_extn_a2dp_get_encoder_latency();
4296
Anish Kumar50ebcbf2014-12-09 04:01:39 +05304297 ALOGV("%s: Latency %d", __func__, latency);
Alexy Josephaa54c872014-12-03 02:46:47 -08004298 return latency;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004299}
4300
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05304301static float AmpToDb(float amplification)
4302{
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05304303 float db = DSD_VOLUME_MIN_DB;
4304 if (amplification > 0) {
4305 db = 20 * log10(amplification);
4306 if(db < DSD_VOLUME_MIN_DB)
4307 return DSD_VOLUME_MIN_DB;
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05304308 }
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05304309 return db;
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05304310}
4311
Arun Mirpuriebe78a72018-10-04 18:23:46 -07004312static int out_set_mmap_volume(struct audio_stream_out *stream, float left,
4313 float right)
4314{
4315 struct stream_out *out = (struct stream_out *)stream;
4316 long volume = 0;
4317 char mixer_ctl_name[128] = "";
4318 struct audio_device *adev = out->dev;
4319 struct mixer_ctl *ctl = NULL;
4320 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
4321 PCM_PLAYBACK);
4322
4323 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
4324 "Playback %d Volume", pcm_device_id);
4325 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
4326 if (!ctl) {
4327 ALOGE("%s: Could not get ctl for mixer cmd - %s",
4328 __func__, mixer_ctl_name);
4329 return -EINVAL;
4330 }
4331 if (left != right)
4332 ALOGW("%s: Left and right channel volume mismatch:%f,%f",
4333 __func__, left, right);
4334 volume = (long)(left * (MMAP_PLAYBACK_VOLUME_MAX*1.0));
4335 if (mixer_ctl_set_value(ctl, 0, volume) < 0){
4336 ALOGE("%s:ctl for mixer cmd - %s, volume %ld returned error",
4337 __func__, mixer_ctl_name, volume);
4338 return -EINVAL;
4339 }
4340 return 0;
4341}
4342
4343
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304344static int out_set_compr_volume(struct audio_stream_out *stream, float left,
4345 float right)
4346{
4347 struct stream_out *out = (struct stream_out *)stream;
Manish Dewangan338c50a2017-09-12 15:22:03 +05304348 long volume[2];
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304349 char mixer_ctl_name[128];
4350 struct audio_device *adev = out->dev;
4351 struct mixer_ctl *ctl;
4352 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
4353 PCM_PLAYBACK);
4354
4355 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
4356 "Compress Playback %d Volume", pcm_device_id);
4357 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
4358 if (!ctl) {
4359 ALOGE("%s: Could not get ctl for mixer cmd - %s",
4360 __func__, mixer_ctl_name);
4361 return -EINVAL;
4362 }
4363 ALOGE("%s:ctl for mixer cmd - %s, left %f, right %f",
4364 __func__, mixer_ctl_name, left, right);
4365 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
4366 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
4367 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
4368
4369 return 0;
4370}
4371
Zhou Song2b8f28f2017-09-11 10:51:38 +08004372static int out_set_voip_volume(struct audio_stream_out *stream, float left,
4373 float right)
4374{
4375 struct stream_out *out = (struct stream_out *)stream;
4376 char mixer_ctl_name[] = "App Type Gain";
4377 struct audio_device *adev = out->dev;
4378 struct mixer_ctl *ctl;
Manish Dewangan338c50a2017-09-12 15:22:03 +05304379 long set_values[4];
Zhou Song2b8f28f2017-09-11 10:51:38 +08004380
4381 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
4382 if (!ctl) {
4383 ALOGE("%s: Could not get ctl for mixer cmd - %s",
4384 __func__, mixer_ctl_name);
4385 return -EINVAL;
4386 }
4387
4388 set_values[0] = 0; //0: Rx Session 1:Tx Session
4389 set_values[1] = out->app_type_cfg.app_type;
Manish Dewangan338c50a2017-09-12 15:22:03 +05304390 set_values[2] = (long)(left * VOIP_PLAYBACK_VOLUME_MAX);
4391 set_values[3] = (long)(right * VOIP_PLAYBACK_VOLUME_MAX);
Zhou Song2b8f28f2017-09-11 10:51:38 +08004392
4393 mixer_ctl_set_array(ctl, set_values, ARRAY_SIZE(set_values));
4394 return 0;
4395}
4396
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004397static int out_set_volume(struct audio_stream_out *stream, float left,
4398 float right)
4399{
Eric Laurenta9024de2013-04-04 09:19:12 -07004400 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004401 int volume[2];
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304402 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004403
Arun Mirpuriebe78a72018-10-04 18:23:46 -07004404 ALOGD("%s: called with left_vol=%f, right_vol=%f", __func__, left, right);
4405
Eric Laurenta9024de2013-04-04 09:19:12 -07004406 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
4407 /* only take left channel into account: the API is for stereo anyway */
4408 out->muted = (left == 0.0f);
4409 return 0;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07004410 } else if (is_offload_usecase(out->usecase)) {
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05304411 if (audio_extn_passthru_is_passthrough_stream(out)) {
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004412 /*
4413 * Set mute or umute on HDMI passthrough stream.
4414 * Only take left channel into account.
4415 * Mute is 0 and unmute 1
4416 */
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05304417 audio_extn_passthru_set_volume(out, (left == 0.0f));
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05304418 } else if (out->format == AUDIO_FORMAT_DSD){
4419 char mixer_ctl_name[128] = "DSD Volume";
4420 struct audio_device *adev = out->dev;
4421 struct mixer_ctl *ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
4422
4423 if (!ctl) {
4424 ALOGE("%s: Could not get ctl for mixer cmd - %s",
4425 __func__, mixer_ctl_name);
4426 return -EINVAL;
4427 }
Manish Dewangan338c50a2017-09-12 15:22:03 +05304428 volume[0] = (long)(AmpToDb(left));
4429 volume[1] = (long)(AmpToDb(right));
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05304430 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
4431 return 0;
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07004432 } else {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304433 pthread_mutex_lock(&out->compr_mute_lock);
Arun Mirpuriebe78a72018-10-04 18:23:46 -07004434 ALOGV("%s: compress mute %d", __func__, out->a2dp_compress_mute);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304435 if (!out->a2dp_compress_mute)
4436 ret = out_set_compr_volume(stream, left, right);
4437 out->volume_l = left;
4438 out->volume_r = right;
4439 pthread_mutex_unlock(&out->compr_mute_lock);
4440 return ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004441 }
Vikram Panduranga93f080e2017-06-07 18:16:14 -07004442 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP) {
Zhou Song2b8f28f2017-09-11 10:51:38 +08004443 if (!out->standby)
4444 ret = out_set_voip_volume(stream, left, right);
4445 out->volume_l = left;
4446 out->volume_r = right;
4447 return ret;
Arun Mirpuriebe78a72018-10-04 18:23:46 -07004448 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
4449 ALOGV("%s: MMAP set volume called", __func__);
4450 if (!out->standby)
4451 ret = out_set_mmap_volume(stream, left, right);
4452 out->volume_l = left;
4453 out->volume_r = right;
4454 return ret;
Eric Laurenta9024de2013-04-04 09:19:12 -07004455 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004456
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004457 return -ENOSYS;
4458}
4459
Zhou Songc9672822017-08-16 16:01:39 +08004460static void update_frames_written(struct stream_out *out, size_t bytes)
4461{
4462 size_t bpf = 0;
4463
4464 if (is_offload_usecase(out->usecase) && !out->non_blocking &&
4465 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD))
4466 bpf = 1;
4467 else if (!is_offload_usecase(out->usecase))
4468 bpf = audio_bytes_per_sample(out->format) *
4469 audio_channel_count_from_out_mask(out->channel_mask);
Zhou Song48453a02018-01-10 17:50:59 +08004470
4471 pthread_mutex_lock(&out->position_query_lock);
4472 if (bpf != 0) {
Zhou Songc9672822017-08-16 16:01:39 +08004473 out->written += bytes / bpf;
Zhou Song48453a02018-01-10 17:50:59 +08004474 clock_gettime(CLOCK_MONOTONIC, &out->writeAt);
4475 }
4476 pthread_mutex_unlock(&out->position_query_lock);
Zhou Songc9672822017-08-16 16:01:39 +08004477}
4478
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004479static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
4480 size_t bytes)
4481{
4482 struct stream_out *out = (struct stream_out *)stream;
4483 struct audio_device *adev = out->dev;
Eric Laurent6e895242013-09-05 16:10:57 -07004484 ssize_t ret = 0;
Satish Babu Patakokila715b1422017-08-22 14:33:21 +05304485 int channels = 0;
Arun Mirpuri7da752a2018-09-11 18:01:15 -07004486 const size_t frame_size = audio_stream_out_frame_size(stream);
4487 const size_t frames = (frame_size != 0) ? bytes / frame_size : bytes;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004488
Haynes Mathew George380745d2017-10-04 15:27:45 -07004489 ATRACE_BEGIN("out_write");
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07004490 lock_output_stream(out);
Naresh Tanniru4c630392014-05-12 01:05:52 +05304491
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304492 if (CARD_STATUS_OFFLINE == out->card_status) {
Zhou Song0b2e5dc2015-03-16 14:41:38 +08004493
Dhananjay Kumarac341582017-02-23 23:42:25 +05304494 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Ashish Jainbbce4322016-02-16 13:25:27 +05304495 /*during SSR for compress usecase we should return error to flinger*/
Naresh Tanniru80659832014-06-04 18:17:56 +05304496 ALOGD(" copl %s: sound card is not active/SSR state", __func__);
4497 pthread_mutex_unlock(&out->lock);
Haynes Mathew George380745d2017-10-04 15:27:45 -07004498 ATRACE_END();
Naresh Tanniru80659832014-06-04 18:17:56 +05304499 return -ENETRESET;
Ashish Jainbbce4322016-02-16 13:25:27 +05304500 } else {
Ashish Jainbbce4322016-02-16 13:25:27 +05304501 ALOGD(" %s: sound card is not active/SSR state", __func__);
4502 ret= -EIO;
4503 goto exit;
Naresh Tanniru4c630392014-05-12 01:05:52 +05304504 }
4505 }
4506
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05304507 if (audio_extn_passthru_should_drop_data(out)) {
Ashish Jaind84fd6a2016-07-27 12:33:25 +05304508 ALOGV(" %s : Drop data as compress passthrough session is going on", __func__);
Ashish Jaind84fd6a2016-07-27 12:33:25 +05304509 ret = -EIO;
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05304510 goto exit;
4511 }
4512
Haynes Mathew George16081042017-05-31 17:16:49 -07004513 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
4514 ret = -EINVAL;
4515 goto exit;
4516 }
4517
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05304518 if ((out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) &&
4519 !out->is_iec61937_info_available) {
4520
4521 if (!audio_extn_passthru_is_passthrough_stream(out)) {
4522 out->is_iec61937_info_available = true;
4523 } else if (audio_extn_passthru_is_enabled()) {
4524 audio_extn_passthru_update_stream_configuration(adev, out, buffer, bytes);
Manish Dewangan37864bc2017-06-09 12:28:37 +05304525 out->is_iec61937_info_available = true;
Manish Dewangan671a4202017-08-18 17:30:46 +05304526
4527 if((out->format == AUDIO_FORMAT_DTS) ||
4528 (out->format == AUDIO_FORMAT_DTS_HD)) {
4529 ret = audio_extn_passthru_update_dts_stream_configuration(out,
4530 buffer, bytes);
4531 if (ret) {
4532 if (ret != -ENOSYS) {
4533 out->is_iec61937_info_available = false;
4534 ALOGD("iec61937 transmission info not yet updated retry");
4535 }
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05304536 } else if (!out->standby) {
Manish Dewangan671a4202017-08-18 17:30:46 +05304537 /* if stream has started and after that there is
4538 * stream config change (iec transmission config)
4539 * then trigger select_device to update backend configuration.
4540 */
4541 out->stream_config_changed = true;
4542 pthread_mutex_lock(&adev->lock);
4543 select_devices(adev, out->usecase);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05304544 if (!audio_extn_passthru_is_supported_backend_edid_cfg(adev, out)) {
4545 ret = -EINVAL;
4546 goto exit;
4547 }
Manish Dewangan671a4202017-08-18 17:30:46 +05304548 pthread_mutex_unlock(&adev->lock);
4549 out->stream_config_changed = false;
4550 out->is_iec61937_info_available = true;
4551 }
4552 }
Satish Babu Patakokila715b1422017-08-22 14:33:21 +05304553
Garmond Leung317cbf12017-09-13 16:20:50 -07004554 if ((channels < (int)audio_channel_count_from_out_mask(out->channel_mask)) &&
Satish Babu Patakokila715b1422017-08-22 14:33:21 +05304555 (out->compr_config.codec->compr_passthr == PASSTHROUGH) &&
4556 (out->is_iec61937_info_available == true)) {
4557 ALOGE("%s: ERROR: Unsupported channel config in passthrough mode", __func__);
4558 ret = -EINVAL;
4559 goto exit;
4560 }
Manish Dewangan37864bc2017-06-09 12:28:37 +05304561 }
4562 }
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304563
4564 if ((out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) &&
4565 (audio_extn_a2dp_is_suspended())) {
4566 if (!(out->devices & AUDIO_DEVICE_OUT_SPEAKER)) {
4567 if (!(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05304568 ret = -EIO;
4569 goto exit;
4570 }
4571 }
4572 }
4573
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004574 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07004575 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08004576 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08004577 if (out->usecase == USECASE_COMPRESS_VOIP_CALL)
4578 ret = voice_extn_compress_voip_start_output_stream(out);
4579 else
4580 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08004581 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004582 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004583 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07004584 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004585 goto exit;
4586 }
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304587 out->started = 1;
vivek mehtab72d08d2016-04-29 03:16:47 -07004588 if (last_known_cal_step != -1) {
4589 ALOGD("%s: retry previous failed cal level set", __func__);
4590 audio_hw_send_gain_dep_calibration(last_known_cal_step);
Preetam Singh Ranawatf4ae0222017-05-31 17:07:28 +05304591 last_known_cal_step = -1;
vivek mehtab72d08d2016-04-29 03:16:47 -07004592 }
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05304593
4594 if ((out->is_iec61937_info_available == true) &&
4595 (audio_extn_passthru_is_passthrough_stream(out))&&
4596 (!audio_extn_passthru_is_supported_backend_edid_cfg(adev, out))) {
4597 ret = -EINVAL;
4598 goto exit;
4599 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004600 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004601
Ashish Jain81eb2a82015-05-13 10:52:34 +05304602 if (adev->is_channel_status_set == false && (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)){
Alexy Josephb1379942016-01-29 15:49:38 -08004603 audio_utils_set_hdmi_channel_status(out, (void *)buffer, bytes);
Ashish Jain81eb2a82015-05-13 10:52:34 +05304604 adev->is_channel_status_set = true;
4605 }
4606
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07004607 if (is_offload_usecase(out->usecase)) {
Alexy Joseph01e54e62015-03-03 19:01:03 -08004608 ALOGVV("copl(%p): writing buffer (%zu bytes) to compress device", out, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004609 if (out->send_new_metadata) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07004610 ALOGD("copl(%p):send new gapless metadata", out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004611 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
4612 out->send_new_metadata = 0;
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05304613 if (out->send_next_track_params && out->is_compr_metadata_avail) {
4614 ALOGD("copl(%p):send next track params in gapless", out);
4615 compress_set_next_track_param(out->compr, &(out->compr_config.codec->options));
4616 out->send_next_track_params = false;
4617 out->is_compr_metadata_avail = false;
4618 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004619 }
Dhananjay Kumarac341582017-02-23 23:42:25 +05304620 if (!(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
Ashish Jain83a6cc22016-06-28 14:34:17 +05304621 (out->convert_buffer) != NULL) {
Haynes Mathew George352f27b2013-07-26 00:00:15 -07004622
Ashish Jain83a6cc22016-06-28 14:34:17 +05304623 if ((bytes > out->hal_fragment_size)) {
Ashish Jainf1eaa582016-05-23 20:54:24 +05304624 ALOGW("Error written bytes %zu > %d (fragment_size)",
Ashish Jain83a6cc22016-06-28 14:34:17 +05304625 bytes, out->hal_fragment_size);
Ashish Jainf1eaa582016-05-23 20:54:24 +05304626 pthread_mutex_unlock(&out->lock);
Haynes Mathew George380745d2017-10-04 15:27:45 -07004627 ATRACE_END();
Ashish Jainf1eaa582016-05-23 20:54:24 +05304628 return -EINVAL;
4629 } else {
Ashish Jain83a6cc22016-06-28 14:34:17 +05304630 audio_format_t dst_format = out->hal_op_format;
4631 audio_format_t src_format = out->hal_ip_format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05304632
4633 uint32_t frames = bytes / format_to_bitwidth_table[src_format];
4634 uint32_t bytes_to_write = frames * format_to_bitwidth_table[dst_format];
4635
Ashish Jain83a6cc22016-06-28 14:34:17 +05304636 memcpy_by_audio_format(out->convert_buffer,
Ashish Jainf1eaa582016-05-23 20:54:24 +05304637 dst_format,
4638 buffer,
4639 src_format,
4640 frames);
4641
Ashish Jain83a6cc22016-06-28 14:34:17 +05304642 ret = compress_write(out->compr, out->convert_buffer,
Ashish Jainf1eaa582016-05-23 20:54:24 +05304643 bytes_to_write);
4644
4645 /*Convert written bytes in audio flinger format*/
4646 if (ret > 0)
4647 ret = ((ret * format_to_bitwidth_table[out->format]) /
4648 format_to_bitwidth_table[dst_format]);
4649 }
4650 } else
4651 ret = compress_write(out->compr, buffer, bytes);
4652
Zhou Songc9672822017-08-16 16:01:39 +08004653 if ((ret < 0 || ret == (ssize_t)bytes) && !out->non_blocking)
4654 update_frames_written(out, bytes);
4655
Dhanalakshmi Siddani37ca1d62014-08-20 12:28:34 +05304656 if (ret < 0)
4657 ret = -errno;
Weiyin Jiangcc60dbb2018-08-21 13:12:03 +08004658 ALOGVV("%s: writing buffer (%zu bytes) to compress device returned %d", __func__, bytes, (int)ret);
Ashish Jainb26edfb2016-08-25 00:10:11 +05304659 /*msg to cb thread only if non blocking write is enabled*/
4660 if (ret >= 0 && ret < (ssize_t)bytes && out->non_blocking) {
Sidipotu Ashok55820562014-02-10 16:16:38 +05304661 ALOGD("No space available in compress driver, post msg to cb thread");
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004662 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
Naresh Tanniru80659832014-06-04 18:17:56 +05304663 } else if (-ENETRESET == ret) {
4664 ALOGE("copl %s: received sound card offline state on compress write", __func__);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304665 out->card_status = CARD_STATUS_OFFLINE;
Naresh Tanniru80659832014-06-04 18:17:56 +05304666 pthread_mutex_unlock(&out->lock);
Dhananjay Kumar1248dd82017-07-28 21:22:16 +05304667 out_on_error(&out->stream.common);
Haynes Mathew George380745d2017-10-04 15:27:45 -07004668 ATRACE_END();
Naresh Tanniru80659832014-06-04 18:17:56 +05304669 return ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004670 }
Ashish Jain5106d362016-05-11 19:23:33 +05304671
Dhanalakshmi Siddania6b76c72016-09-09 18:10:31 +05304672 /* Call compr start only when non-zero bytes of data is there to be rendered */
4673 if (!out->playback_started && ret > 0) {
4674 int status = compress_start(out->compr);
4675 if (status < 0) {
4676 ret = status;
4677 ALOGE("%s: compr start failed with err %d", __func__, errno);
4678 goto exit;
4679 }
Alexy Joseph7de344d2015-03-30 10:40:03 -07004680 audio_extn_dts_eagle_fade(adev, true, out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004681 out->playback_started = 1;
4682 out->offload_state = OFFLOAD_STATE_PLAYING;
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08004683
4684 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
4685 popcount(out->channel_mask),
4686 out->playback_started);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004687 }
4688 pthread_mutex_unlock(&out->lock);
Haynes Mathew George380745d2017-10-04 15:27:45 -07004689 ATRACE_END();
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004690 return ret;
4691 } else {
4692 if (out->pcm) {
Arun Mirpuri7da752a2018-09-11 18:01:15 -07004693 size_t bytes_to_write = bytes;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004694 if (out->muted)
4695 memset((void *)buffer, 0, bytes);
Arun Mirpuri7da752a2018-09-11 18:01:15 -07004696 ALOGV("%s: frames=%zu, frame_size=%zu, bytes_to_write=%zu",
4697 __func__, frames, frame_size, bytes_to_write);
4698
Arun Mirpuridbef0c72018-09-12 18:36:10 -07004699 if (out->usecase == USECASE_INCALL_MUSIC_UPLINK ||
4700 out->usecase == USECASE_INCALL_MUSIC_UPLINK2) {
Arun Mirpuri7da752a2018-09-11 18:01:15 -07004701 size_t channel_count = audio_channel_count_from_out_mask(out->channel_mask);
4702 int16_t *src = (int16_t *)buffer;
4703 int16_t *dst = (int16_t *)buffer;
4704
4705 LOG_ALWAYS_FATAL_IF(out->config.channels != 1 || channel_count != 2 ||
4706 out->format != AUDIO_FORMAT_PCM_16_BIT,
4707 "out_write called for incall music use case with wrong properties");
4708
4709 /*
4710 * FIXME: this can be removed once audio flinger mixer supports
4711 * mono output
4712 */
4713
4714 /*
4715 * Code below goes over each frame in the buffer and adds both
4716 * L and R samples and then divides by 2 to convert to mono
4717 */
4718 for (size_t i = 0; i < frames ; i++, dst++, src += 2) {
4719 *dst = (int16_t)(((int32_t)src[0] + (int32_t)src[1]) >> 1);
4720 }
4721 bytes_to_write /= 2;
4722 }
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07004723
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05304724 ALOGVV("%s: writing buffer (%zu bytes) to pcm device", __func__, bytes);
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07004725
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004726 long ns = 0;
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07004727
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004728 if (out->config.rate)
4729 ns = pcm_bytes_to_frames(out->pcm, bytes)*1000000000LL/
4730 out->config.rate;
4731
Arun Mirpuri7da752a2018-09-11 18:01:15 -07004732 request_out_focus(out, ns);
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004733 bool use_mmap = is_mmap_usecase(out->usecase) || out->realtime;
4734
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004735 if (use_mmap)
Arun Mirpuri7da752a2018-09-11 18:01:15 -07004736 ret = pcm_mmap_write(out->pcm, (void *)buffer, bytes_to_write);
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004737 else if (out->hal_op_format != out->hal_ip_format &&
Ashish Jain83a6cc22016-06-28 14:34:17 +05304738 out->convert_buffer != NULL) {
4739
4740 memcpy_by_audio_format(out->convert_buffer,
4741 out->hal_op_format,
4742 buffer,
4743 out->hal_ip_format,
4744 out->config.period_size * out->config.channels);
4745
4746 ret = pcm_write(out->pcm, out->convert_buffer,
4747 (out->config.period_size *
4748 out->config.channels *
4749 format_to_bitwidth_table[out->hal_op_format]));
4750 } else {
Aditya Bavanarid4db8ee2017-05-29 21:08:03 +05304751 /*
4752 * To avoid underrun in DSP when the application is not pumping
4753 * data at required rate, check for the no. of bytes and ignore
4754 * pcm_write if it is less than actual buffer size.
4755 * It is a work around to a change in compress VOIP driver.
4756 */
4757 if ((out->flags & AUDIO_OUTPUT_FLAG_VOIP_RX) &&
4758 bytes < (out->config.period_size * out->config.channels *
4759 audio_bytes_per_sample(out->format))) {
4760 size_t voip_buf_size =
4761 out->config.period_size * out->config.channels *
4762 audio_bytes_per_sample(out->format);
4763 ALOGE("%s:VOIP underrun: bytes received %zu, required:%zu\n",
4764 __func__, bytes, voip_buf_size);
4765 usleep(((uint64_t)voip_buf_size - bytes) *
4766 1000000 / audio_stream_out_frame_size(stream) /
4767 out_get_sample_rate(&out->stream.common));
4768 ret = 0;
4769 } else
Arun Mirpuri7da752a2018-09-11 18:01:15 -07004770 ret = pcm_write(out->pcm, (void *)buffer, bytes_to_write);
Ashish Jain83a6cc22016-06-28 14:34:17 +05304771 }
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07004772
Haynes Mathew George5beddd42016-06-27 18:33:40 -07004773 release_out_focus(out);
4774
Dhanalakshmi Siddani37ca1d62014-08-20 12:28:34 +05304775 if (ret < 0)
4776 ret = -errno;
Zhou Songc9672822017-08-16 16:01:39 +08004777 else if (ret > 0)
Ashish Jain83a6cc22016-06-28 14:34:17 +05304778 ret = -EINVAL;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004779 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004780 }
4781
4782exit:
Zhou Songc9672822017-08-16 16:01:39 +08004783 update_frames_written(out, bytes);
Naresh Tanniru4c630392014-05-12 01:05:52 +05304784 if (-ENETRESET == ret) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304785 out->card_status = CARD_STATUS_OFFLINE;
Naresh Tanniru4c630392014-05-12 01:05:52 +05304786 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004787 pthread_mutex_unlock(&out->lock);
4788
4789 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07004790 if (out->pcm)
Alexy Josephb1379942016-01-29 15:49:38 -08004791 ALOGE("%s: error %d, %s", __func__, (int)ret, pcm_get_error(out->pcm));
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05304792 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05304793 pthread_mutex_lock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05304794 voice_extn_compress_voip_close_output_stream(&out->stream.common);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05304795 out->started = 0;
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05304796 pthread_mutex_unlock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05304797 out->standby = true;
4798 }
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304799 out_on_error(&out->stream.common);
Dhanalakshmi Siddania6b76c72016-09-09 18:10:31 +05304800 if (!(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD))
4801 usleep((uint64_t)bytes * 1000000 / audio_stream_out_frame_size(stream) /
4802 out_get_sample_rate(&out->stream.common));
Vidyakumar Athotaa9d3a5f2017-08-09 12:13:05 -07004803
4804 if (audio_extn_passthru_is_passthrough_stream(out)) {
Rajshekar Eashwarappa88834522018-04-02 17:20:15 +05304805 //ALOGE("%s: write error, ret = %zd", __func__, ret);
Haynes Mathew George380745d2017-10-04 15:27:45 -07004806 ATRACE_END();
Vidyakumar Athotaa9d3a5f2017-08-09 12:13:05 -07004807 return ret;
4808 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004809 }
Haynes Mathew George380745d2017-10-04 15:27:45 -07004810 ATRACE_END();
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004811 return bytes;
4812}
4813
4814static int out_get_render_position(const struct audio_stream_out *stream,
4815 uint32_t *dsp_frames)
4816{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004817 struct stream_out *out = (struct stream_out *)stream;
Zhou Song32a556e2015-05-05 10:46:56 +08004818
4819 if (dsp_frames == NULL)
4820 return -EINVAL;
4821
4822 *dsp_frames = 0;
4823 if (is_offload_usecase(out->usecase)) {
Mingming Yin9e348b52014-11-19 16:18:55 -08004824 ssize_t ret = 0;
Ashish Jain5106d362016-05-11 19:23:33 +05304825
4826 /* Below piece of code is not guarded against any lock beacuse audioFliner serializes
4827 * this operation and adev_close_output_stream(where out gets reset).
4828 */
Dhananjay Kumarac341582017-02-23 23:42:25 +05304829 if (!out->non_blocking && !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Zhou Song48453a02018-01-10 17:50:59 +08004830 *dsp_frames = get_actual_pcm_frames_rendered(out, NULL);
Ashish Jain5106d362016-05-11 19:23:33 +05304831 ALOGVV("dsp_frames %d sampleRate %d",(int)*dsp_frames,out->sample_rate);
Haynes Mathew Georgeb0f5dc32017-10-06 18:35:12 -07004832 adjust_frames_for_device_delay(out, dsp_frames);
Ashish Jain5106d362016-05-11 19:23:33 +05304833 return 0;
4834 }
4835
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07004836 lock_output_stream(out);
Ashish Jain5106d362016-05-11 19:23:33 +05304837 if (out->compr != NULL && out->non_blocking) {
Naresh Tanniru80659832014-06-04 18:17:56 +05304838 ret = compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004839 &out->sample_rate);
Dhanalakshmi Siddani37ca1d62014-08-20 12:28:34 +05304840 if (ret < 0)
4841 ret = -errno;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004842 ALOGVV("%s rendered frames %d sample_rate %d",
Ashish Jain5106d362016-05-11 19:23:33 +05304843 __func__, *dsp_frames, out->sample_rate);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004844 }
Naresh Tanniru80659832014-06-04 18:17:56 +05304845 if (-ENETRESET == ret) {
4846 ALOGE(" ERROR: sound card not active Unable to get time stamp from compress driver");
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304847 out->card_status = CARD_STATUS_OFFLINE;
4848 ret = -EINVAL;
Naresh Tanniru80659832014-06-04 18:17:56 +05304849 } else if(ret < 0) {
4850 ALOGE(" ERROR: Unable to get time stamp from compress driver");
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304851 ret = -EINVAL;
4852 } else if (out->card_status == CARD_STATUS_OFFLINE) {
Preetam Singh Ranawat2d0e4632015-02-02 12:40:59 +05304853 /*
4854 * Handle corner case where compress session is closed during SSR
4855 * and timestamp is queried
4856 */
4857 ALOGE(" ERROR: sound card not active, return error");
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304858 ret = -EINVAL;
Naresh Tanniru80659832014-06-04 18:17:56 +05304859 } else {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304860 ret = 0;
Haynes Mathew Georgeb0f5dc32017-10-06 18:35:12 -07004861 adjust_frames_for_device_delay(out, dsp_frames);
Naresh Tanniru80659832014-06-04 18:17:56 +05304862 }
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304863 pthread_mutex_unlock(&out->lock);
4864 return ret;
Zhou Song32a556e2015-05-05 10:46:56 +08004865 } else if (audio_is_linear_pcm(out->format)) {
4866 *dsp_frames = out->written;
Haynes Mathew Georgeb0f5dc32017-10-06 18:35:12 -07004867 adjust_frames_for_device_delay(out, dsp_frames);
Zhou Song32a556e2015-05-05 10:46:56 +08004868 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004869 } else
4870 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004871}
4872
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07004873static int out_add_audio_effect(const struct audio_stream *stream __unused,
4874 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004875{
4876 return 0;
4877}
4878
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07004879static int out_remove_audio_effect(const struct audio_stream *stream __unused,
4880 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004881{
4882 return 0;
4883}
4884
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07004885static int out_get_next_write_timestamp(const struct audio_stream_out *stream __unused,
4886 int64_t *timestamp __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004887{
Satya Krishna Pindiprolib6655542017-07-03 19:38:19 +05304888 return -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08004889}
4890
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07004891static int out_get_presentation_position(const struct audio_stream_out *stream,
4892 uint64_t *frames, struct timespec *timestamp)
4893{
4894 struct stream_out *out = (struct stream_out *)stream;
pavance65c2fe2017-10-18 17:52:01 +05304895 int ret = -ENODATA;
Eric Laurent949a0892013-09-20 09:20:13 -07004896 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07004897
Ashish Jain5106d362016-05-11 19:23:33 +05304898 /* below piece of code is not guarded against any lock because audioFliner serializes
4899 * this operation and adev_close_output_stream( where out gets reset).
4900 */
4901 if (is_offload_usecase(out->usecase) && !out->non_blocking &&
Dhananjay Kumarac341582017-02-23 23:42:25 +05304902 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Zhou Song48453a02018-01-10 17:50:59 +08004903 *frames = get_actual_pcm_frames_rendered(out, timestamp);
Ashish Jain5106d362016-05-11 19:23:33 +05304904 ALOGVV("frames %lld playedat %lld",(long long int)*frames,
4905 timestamp->tv_sec * 1000000LL + timestamp->tv_nsec / 1000);
4906 return 0;
4907 }
4908
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07004909 lock_output_stream(out);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07004910
Ashish Jain5106d362016-05-11 19:23:33 +05304911 if (is_offload_usecase(out->usecase) && out->compr != NULL && out->non_blocking) {
4912 ret = compress_get_tstamp(out->compr, &dsp_frames,
4913 &out->sample_rate);
yidongh0515e042017-07-06 15:00:34 +08004914 // Adjustment accounts for A2dp encoder latency with offload usecases
4915 // Note: Encoder latency is returned in ms.
4916 if (AUDIO_DEVICE_OUT_ALL_A2DP & out->devices) {
4917 unsigned long offset =
4918 (audio_extn_a2dp_get_encoder_latency() * out->sample_rate / 1000);
4919 dsp_frames = (dsp_frames > offset) ? (dsp_frames - offset) : 0;
4920 }
Ashish Jain5106d362016-05-11 19:23:33 +05304921 ALOGVV("%s rendered frames %ld sample_rate %d",
4922 __func__, dsp_frames, out->sample_rate);
4923 *frames = dsp_frames;
4924 if (ret < 0)
4925 ret = -errno;
4926 if (-ENETRESET == ret) {
4927 ALOGE(" ERROR: sound card not active Unable to get time stamp from compress driver");
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304928 out->card_status = CARD_STATUS_OFFLINE;
Ashish Jain5106d362016-05-11 19:23:33 +05304929 ret = -EINVAL;
4930 } else
4931 ret = 0;
4932 /* this is the best we can do */
4933 clock_gettime(CLOCK_MONOTONIC, timestamp);
Eric Laurent949a0892013-09-20 09:20:13 -07004934 } else {
4935 if (out->pcm) {
Weiyin Jiangd4633762018-03-16 12:05:03 +08004936 unsigned int avail;
4937 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
4938 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
4939 int64_t signed_frames = out->written - kernel_buffer_size + avail;
4940 // This adjustment accounts for buffering after app processor.
4941 // It is based on estimated DSP latency per use case, rather than exact.
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07004942 signed_frames -=
Weiyin Jiangd4633762018-03-16 12:05:03 +08004943 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
Aniket Kumar Lataff613152017-07-18 18:19:21 -07004944
Weiyin Jiangd4633762018-03-16 12:05:03 +08004945 // Adjustment accounts for A2dp encoder latency with non offload usecases
4946 // Note: Encoder latency is returned in ms, while platform_render_latency in us.
4947 if (AUDIO_DEVICE_OUT_ALL_A2DP & out->devices) {
4948 signed_frames -=
4949 (audio_extn_a2dp_get_encoder_latency() * out->sample_rate / 1000);
4950 }
4951
4952 // It would be unusual for this value to be negative, but check just in case ...
4953 if (signed_frames >= 0) {
4954 *frames = signed_frames;
4955 ret = 0;
4956 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07004957 }
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05304958 } else if (out->card_status == CARD_STATUS_OFFLINE) {
Ashish Jainbbce4322016-02-16 13:25:27 +05304959 *frames = out->written;
4960 clock_gettime(CLOCK_MONOTONIC, timestamp);
4961 ret = 0;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07004962 }
4963 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07004964 pthread_mutex_unlock(&out->lock);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07004965 return ret;
4966}
4967
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004968static int out_set_callback(struct audio_stream_out *stream,
4969 stream_callback_t callback, void *cookie)
4970{
4971 struct stream_out *out = (struct stream_out *)stream;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08004972 int ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004973
4974 ALOGV("%s", __func__);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07004975 lock_output_stream(out);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08004976 out->client_callback = callback;
4977 out->client_cookie = cookie;
4978 if (out->adsp_hdlr_stream_handle) {
4979 ret = audio_extn_adsp_hdlr_stream_set_callback(
4980 out->adsp_hdlr_stream_handle,
4981 callback,
4982 cookie);
4983 if (ret)
4984 ALOGW("%s:adsp hdlr callback registration failed %d",
4985 __func__, ret);
4986 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004987 pthread_mutex_unlock(&out->lock);
4988 return 0;
4989}
4990
4991static int out_pause(struct audio_stream_out* stream)
4992{
4993 struct stream_out *out = (struct stream_out *)stream;
4994 int status = -ENOSYS;
4995 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07004996 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07004997 ALOGD("copl(%p):pause compress driver", out);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07004998 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07004999 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305000 if (out->card_status != CARD_STATUS_OFFLINE)
Naresh Tanniru80659832014-06-04 18:17:56 +05305001 status = compress_pause(out->compr);
5002
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005003 out->offload_state = OFFLOAD_STATE_PAUSED;
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08005004
Mingming Yin21854652016-04-13 11:54:02 -07005005 if (audio_extn_passthru_is_active()) {
5006 ALOGV("offload use case, pause passthru");
5007 audio_extn_passthru_on_pause(out);
5008 }
5009
Dhanalakshmi Siddani79415e72015-03-23 11:54:47 +05305010 audio_extn_dts_eagle_fade(adev, false, out);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08005011 audio_extn_dts_notify_playback_state(out->usecase, 0,
5012 out->sample_rate, popcount(out->channel_mask),
5013 0);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005014 }
5015 pthread_mutex_unlock(&out->lock);
5016 }
5017 return status;
5018}
5019
5020static int out_resume(struct audio_stream_out* stream)
5021{
5022 struct stream_out *out = (struct stream_out *)stream;
5023 int status = -ENOSYS;
5024 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07005025 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07005026 ALOGD("copl(%p):resume compress driver", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005027 status = 0;
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005028 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005029 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305030 if (out->card_status != CARD_STATUS_OFFLINE) {
Naresh Tanniru80659832014-06-04 18:17:56 +05305031 status = compress_resume(out->compr);
Mingming Yin21854652016-04-13 11:54:02 -07005032 }
5033 if (!status) {
5034 out->offload_state = OFFLOAD_STATE_PLAYING;
5035 }
Dhanalakshmi Siddani79415e72015-03-23 11:54:47 +05305036 audio_extn_dts_eagle_fade(adev, true, out);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08005037 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
5038 popcount(out->channel_mask), 1);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005039 }
5040 pthread_mutex_unlock(&out->lock);
5041 }
5042 return status;
5043}
5044
5045static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
5046{
5047 struct stream_out *out = (struct stream_out *)stream;
5048 int status = -ENOSYS;
5049 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07005050 if (is_offload_usecase(out->usecase)) {
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005051 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005052 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
5053 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
5054 else
5055 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
5056 pthread_mutex_unlock(&out->lock);
5057 }
5058 return status;
5059}
5060
5061static int out_flush(struct audio_stream_out* stream)
5062{
5063 struct stream_out *out = (struct stream_out *)stream;
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):calling compress flush", out);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005067 lock_output_stream(out);
Haynes Mathew Georgeafe54d82016-09-21 14:39:19 -07005068 if (out->offload_state == OFFLOAD_STATE_PAUSED) {
5069 stop_compressed_output_l(out);
Haynes Mathew Georgeafe54d82016-09-21 14:39:19 -07005070 } else {
5071 ALOGW("%s called in invalid state %d", __func__, out->offload_state);
5072 }
Weiyin Jiang547e4152017-09-14 17:24:18 +08005073 out->written = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005074 pthread_mutex_unlock(&out->lock);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07005075 ALOGD("copl(%p):out of compress flush", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005076 return 0;
5077 }
5078 return -ENOSYS;
5079}
5080
Haynes Mathew George16081042017-05-31 17:16:49 -07005081static int out_stop(const struct audio_stream_out* stream)
5082{
5083 struct stream_out *out = (struct stream_out *)stream;
5084 struct audio_device *adev = out->dev;
5085 int ret = -ENOSYS;
5086
5087 ALOGV("%s", __func__);
5088 pthread_mutex_lock(&adev->lock);
5089 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP && !out->standby &&
5090 out->playback_started && out->pcm != NULL) {
5091 pcm_stop(out->pcm);
5092 ret = stop_output_stream(out);
5093 out->playback_started = false;
5094 }
5095 pthread_mutex_unlock(&adev->lock);
5096 return ret;
5097}
5098
5099static int out_start(const struct audio_stream_out* stream)
5100{
5101 struct stream_out *out = (struct stream_out *)stream;
5102 struct audio_device *adev = out->dev;
5103 int ret = -ENOSYS;
5104
5105 ALOGV("%s", __func__);
5106 pthread_mutex_lock(&adev->lock);
5107 if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP && !out->standby &&
5108 !out->playback_started && out->pcm != NULL) {
5109 ret = start_output_stream(out);
5110 if (ret == 0) {
5111 out->playback_started = true;
5112 }
5113 }
5114 pthread_mutex_unlock(&adev->lock);
5115 return ret;
5116}
5117
5118/*
5119 * Modify config->period_count based on min_size_frames
5120 */
5121static void adjust_mmap_period_count(struct pcm_config *config, int32_t min_size_frames)
5122{
5123 int periodCountRequested = (min_size_frames + config->period_size - 1)
5124 / config->period_size;
5125 int periodCount = MMAP_PERIOD_COUNT_MIN;
5126
5127 ALOGV("%s original config.period_size = %d config.period_count = %d",
5128 __func__, config->period_size, config->period_count);
5129
5130 while (periodCount < periodCountRequested && (periodCount * 2) < MMAP_PERIOD_COUNT_MAX) {
5131 periodCount *= 2;
5132 }
5133 config->period_count = periodCount;
5134
5135 ALOGV("%s requested config.period_count = %d", __func__, config->period_count);
5136}
5137
5138static int out_create_mmap_buffer(const struct audio_stream_out *stream,
5139 int32_t min_size_frames,
5140 struct audio_mmap_buffer_info *info)
5141{
5142 struct stream_out *out = (struct stream_out *)stream;
5143 struct audio_device *adev = out->dev;
5144 int ret = 0;
Aalique Grahame1f123102017-10-12 10:38:32 -07005145 unsigned int offset1 = 0;
5146 unsigned int frames1 = 0;
Haynes Mathew George16081042017-05-31 17:16:49 -07005147 const char *step = "";
Haynes Mathew Georgeef514882017-05-01 17:46:23 -07005148 uint32_t mmap_size;
Arun Mirpuriebe78a72018-10-04 18:23:46 -07005149 uint32_t buffer_size;
Haynes Mathew George16081042017-05-31 17:16:49 -07005150
Arun Mirpuriebe78a72018-10-04 18:23:46 -07005151 ALOGD("%s", __func__);
Haynes Mathew George16081042017-05-31 17:16:49 -07005152 pthread_mutex_lock(&adev->lock);
5153
5154 if (info == NULL || min_size_frames == 0) {
5155 ALOGE("%s: info = %p, min_size_frames = %d", __func__, info, min_size_frames);
5156 ret = -EINVAL;
5157 goto exit;
5158 }
5159 if (out->usecase != USECASE_AUDIO_PLAYBACK_MMAP || !out->standby) {
5160 ALOGE("%s: usecase = %d, standby = %d", __func__, out->usecase, out->standby);
5161 ret = -ENOSYS;
5162 goto exit;
5163 }
5164 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
5165 if (out->pcm_device_id < 0) {
5166 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
5167 __func__, out->pcm_device_id, out->usecase);
5168 ret = -EINVAL;
5169 goto exit;
5170 }
5171
5172 adjust_mmap_period_count(&out->config, min_size_frames);
5173
Arun Mirpuriebe78a72018-10-04 18:23:46 -07005174 ALOGD("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
Haynes Mathew George16081042017-05-31 17:16:49 -07005175 __func__, adev->snd_card, out->pcm_device_id, out->config.channels);
5176 out->pcm = pcm_open(adev->snd_card, out->pcm_device_id,
5177 (PCM_OUT | PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC), &out->config);
5178 if (out->pcm == NULL || !pcm_is_ready(out->pcm)) {
5179 step = "open";
5180 ret = -ENODEV;
5181 goto exit;
5182 }
5183 ret = pcm_mmap_begin(out->pcm, &info->shared_memory_address, &offset1, &frames1);
5184 if (ret < 0) {
5185 step = "begin";
5186 goto exit;
5187 }
5188 info->buffer_size_frames = pcm_get_buffer_size(out->pcm);
Arun Mirpuriebe78a72018-10-04 18:23:46 -07005189 buffer_size = pcm_frames_to_bytes(out->pcm, info->buffer_size_frames);
Haynes Mathew George16081042017-05-31 17:16:49 -07005190 info->burst_size_frames = out->config.period_size;
Haynes Mathew Georgeef514882017-05-01 17:46:23 -07005191 ret = platform_get_mmap_data_fd(adev->platform,
5192 out->pcm_device_id, 0 /*playback*/,
5193 &info->shared_memory_fd,
5194 &mmap_size);
5195 if (ret < 0) {
Arun Mirpuriebe78a72018-10-04 18:23:46 -07005196 // Fall back to non exclusive mode
5197 info->shared_memory_fd = pcm_get_poll_fd(out->pcm);
5198 } else {
5199 if (mmap_size < buffer_size) {
5200 step = "mmap";
5201 goto exit;
5202 }
5203 // FIXME: indicate exclusive mode support by returning a negative buffer size
5204 info->buffer_size_frames *= -1;
Haynes Mathew Georgeef514882017-05-01 17:46:23 -07005205 }
Arun Mirpuriebe78a72018-10-04 18:23:46 -07005206 memset(info->shared_memory_address, 0, buffer_size);
Haynes Mathew George16081042017-05-31 17:16:49 -07005207
5208 ret = pcm_mmap_commit(out->pcm, 0, MMAP_PERIOD_SIZE);
5209 if (ret < 0) {
5210 step = "commit";
5211 goto exit;
5212 }
5213
5214 out->standby = false;
5215 ret = 0;
5216
Arun Mirpuriebe78a72018-10-04 18:23:46 -07005217 ALOGD("%s: got mmap buffer address %p info->buffer_size_frames %d",
Haynes Mathew George16081042017-05-31 17:16:49 -07005218 __func__, info->shared_memory_address, info->buffer_size_frames);
5219
5220exit:
5221 if (ret != 0) {
5222 if (out->pcm == NULL) {
5223 ALOGE("%s: %s - %d", __func__, step, ret);
5224 } else {
5225 ALOGE("%s: %s %s", __func__, step, pcm_get_error(out->pcm));
5226 pcm_close(out->pcm);
5227 out->pcm = NULL;
5228 }
5229 }
5230 pthread_mutex_unlock(&adev->lock);
5231 return ret;
5232}
5233
5234static int out_get_mmap_position(const struct audio_stream_out *stream,
5235 struct audio_mmap_position *position)
5236{
5237 struct stream_out *out = (struct stream_out *)stream;
5238 ALOGVV("%s", __func__);
5239 if (position == NULL) {
5240 return -EINVAL;
5241 }
5242 if (out->usecase != USECASE_AUDIO_PLAYBACK_MMAP) {
Haynes Mathew George4ab3ba92017-12-11 14:49:43 -08005243 ALOGE("%s: called on %s", __func__, use_case_table[out->usecase]);
Haynes Mathew George16081042017-05-31 17:16:49 -07005244 return -ENOSYS;
5245 }
5246 if (out->pcm == NULL) {
5247 return -ENOSYS;
5248 }
5249
5250 struct timespec ts = { 0, 0 };
5251 int ret = pcm_mmap_get_hw_ptr(out->pcm, (unsigned int *)&position->position_frames, &ts);
5252 if (ret < 0) {
5253 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
5254 return ret;
5255 }
Aalique Grahame90717e22018-07-18 15:54:18 -07005256 position->time_nanoseconds = ts.tv_sec*1000000000LL + ts.tv_nsec;
Haynes Mathew George16081042017-05-31 17:16:49 -07005257 return 0;
5258}
5259
5260
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005261/** audio_stream_in implementation **/
5262static uint32_t in_get_sample_rate(const struct audio_stream *stream)
5263{
5264 struct stream_in *in = (struct stream_in *)stream;
5265
5266 return in->config.rate;
5267}
5268
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07005269static int in_set_sample_rate(struct audio_stream *stream __unused,
5270 uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005271{
5272 return -ENOSYS;
5273}
5274
5275static size_t in_get_buffer_size(const struct audio_stream *stream)
5276{
5277 struct stream_in *in = (struct stream_in *)stream;
5278
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08005279 if(in->usecase == USECASE_COMPRESS_VOIP_CALL)
5280 return voice_extn_compress_voip_in_get_buffer_size(in);
Vikram Panduranga93f080e2017-06-07 18:16:14 -07005281 else if(in->usecase == USECASE_AUDIO_RECORD_VOIP)
5282 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 -07005283 else if(audio_extn_compr_cap_usecase_supported(in->usecase))
5284 return audio_extn_compr_cap_get_buffer_size(in->config.format);
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05305285 else if(audio_extn_cin_attached_usecase(in->usecase))
5286 return audio_extn_cin_get_buffer_size(in);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08005287
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005288 return in->config.period_size * in->af_period_multiplier *
5289 audio_stream_in_frame_size((const struct audio_stream_in *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005290}
5291
5292static uint32_t in_get_channels(const struct audio_stream *stream)
5293{
5294 struct stream_in *in = (struct stream_in *)stream;
5295
5296 return in->channel_mask;
5297}
5298
5299static audio_format_t in_get_format(const struct audio_stream *stream)
5300{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08005301 struct stream_in *in = (struct stream_in *)stream;
5302
5303 return in->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005304}
5305
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07005306static int in_set_format(struct audio_stream *stream __unused,
5307 audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005308{
5309 return -ENOSYS;
5310}
5311
5312static int in_standby(struct audio_stream *stream)
5313{
5314 struct stream_in *in = (struct stream_in *)stream;
5315 struct audio_device *adev = in->dev;
5316 int status = 0;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05305317 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
5318 stream, in->usecase, use_case_table[in->usecase]);
Haynes Mathew George16081042017-05-31 17:16:49 -07005319 bool do_stop = true;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05305320
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005321 lock_input_stream(in);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07005322 if (!in->standby && in->is_st_session) {
5323 ALOGD("%s: sound trigger pcm stop lab", __func__);
5324 audio_extn_sound_trigger_stop_lab(in);
5325 in->standby = 1;
5326 }
5327
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005328 if (!in->standby) {
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07005329 if (adev->adm_deregister_stream)
5330 adev->adm_deregister_stream(adev->adm_data, in->capture_handle);
5331
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08005332 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005333 in->standby = true;
Zhou Songa8895042016-07-05 17:54:22 +08005334 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
kunleizbecba2d2017-09-07 13:37:16 +08005335 do_stop = false;
Zhou Songa8895042016-07-05 17:54:22 +08005336 voice_extn_compress_voip_close_input_stream(stream);
5337 ALOGD("VOIP input entered standby");
Haynes Mathew George16081042017-05-31 17:16:49 -07005338 } else if (in->usecase == USECASE_AUDIO_RECORD_MMAP) {
5339 do_stop = in->capture_started;
5340 in->capture_started = false;
Zhou Songa8895042016-07-05 17:54:22 +08005341 } else {
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05305342 if (audio_extn_cin_attached_usecase(in->usecase))
5343 audio_extn_cin_stop_input_stream(in);
kunleizbecba2d2017-09-07 13:37:16 +08005344 }
5345
Arun Mirpuriebe78a72018-10-04 18:23:46 -07005346 if (in->pcm) {
Haynes Mathew George380745d2017-10-04 15:27:45 -07005347 ATRACE_BEGIN("pcm_in_close");
Zhou Songa8895042016-07-05 17:54:22 +08005348 pcm_close(in->pcm);
Haynes Mathew George380745d2017-10-04 15:27:45 -07005349 ATRACE_END();
Zhou Songa8895042016-07-05 17:54:22 +08005350 in->pcm = NULL;
Eric Laurent150dbfe2013-02-27 14:31:02 -08005351 }
Arun Mirpuriebe78a72018-10-04 18:23:46 -07005352
5353 if (do_stop)
5354 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08005355 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005356 }
5357 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07005358 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005359 return status;
5360}
5361
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07005362static int in_dump(const struct audio_stream *stream __unused,
5363 int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005364{
5365 return 0;
5366}
5367
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305368static void in_snd_mon_cb(void * stream, struct str_parms * parms)
5369{
5370 if (!stream || !parms)
5371 return;
5372
5373 struct stream_in *in = (struct stream_in *)stream;
5374 struct audio_device *adev = in->dev;
5375
5376 card_status_t status;
5377 int card;
5378 if (parse_snd_card_status(parms, &card, &status) < 0)
5379 return;
5380
5381 pthread_mutex_lock(&adev->lock);
5382 bool valid_cb = (card == adev->snd_card);
5383 pthread_mutex_unlock(&adev->lock);
5384
5385 if (!valid_cb)
5386 return;
5387
5388 lock_input_stream(in);
5389 if (in->card_status != status)
5390 in->card_status = status;
5391 pthread_mutex_unlock(&in->lock);
5392
5393 ALOGW("in_snd_mon_cb for card %d usecase %s, status %s", card,
5394 use_case_table[in->usecase],
5395 status == CARD_STATUS_OFFLINE ? "offline" : "online");
5396
5397 // a better solution would be to report error back to AF and let
5398 // it put the stream to standby
5399 if (status == CARD_STATUS_OFFLINE)
5400 in_standby(&in->stream.common);
5401
5402 return;
5403}
5404
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005405static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
5406{
5407 struct stream_in *in = (struct stream_in *)stream;
5408 struct audio_device *adev = in->dev;
5409 struct str_parms *parms;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005410 char value[32];
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08005411 int ret = 0, val = 0, err;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005412
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05305413 ALOGD("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005414 parms = str_parms_create_str(kvpairs);
5415
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05305416 if (!parms)
5417 goto error;
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005418 lock_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08005419 pthread_mutex_lock(&adev->lock);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08005420
5421 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
5422 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005423 val = atoi(value);
5424 /* no audio source uses val == 0 */
5425 if ((in->source != val) && (val != 0)) {
5426 in->source = val;
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08005427 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
5428 (in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
5429 (voice_extn_compress_voip_is_format_supported(in->format)) &&
Shiv Maliyappanahallie66aba22016-01-27 16:08:57 -08005430 (in->config.rate == 8000 || in->config.rate == 16000 ||
5431 in->config.rate == 32000 || in->config.rate == 48000 ) &&
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07005432 (audio_channel_count_from_in_mask(in->channel_mask) == 1)) {
Narsinga Rao Chella7d5a3e82014-02-04 16:23:52 -08005433 err = voice_extn_compress_voip_open_input_stream(in);
5434 if (err != 0) {
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08005435 ALOGE("%s: Compress voip input cannot be opened, error:%d",
Narsinga Rao Chella7d5a3e82014-02-04 16:23:52 -08005436 __func__, err);
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08005437 }
5438 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005439 }
5440 }
5441
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08005442 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
5443 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005444 val = atoi(value);
Kevin Rocardfce19002017-08-07 19:21:36 -07005445 if (((int)in->device != val) && (val != 0) && audio_is_input_device(val) ) {
5446
5447 // Workaround: If routing to an non existing usb device, fail gracefully
5448 // The routing request will otherwise block during 10 second
5449 int card;
5450 if (audio_is_usb_in_device(val) &&
5451 (card = get_alive_usb_card(parms)) >= 0) {
5452
5453 ALOGW("in_set_parameters() ignoring rerouting to non existing USB card %d", card);
5454 ret = -ENOSYS;
5455 } else {
5456
5457 in->device = val;
5458 /* If recording is in progress, change the tx device to new device */
5459 if (!in->standby && !in->is_st_session) {
5460 ALOGV("update input routing change");
5461 // inform adm before actual routing to prevent glitches.
5462 if (adev->adm_on_routing_change) {
Haynes Mathew George822b5492016-07-01 16:57:24 -07005463 adev->adm_on_routing_change(adev->adm_data,
5464 in->capture_handle);
Kevin Rocardfce19002017-08-07 19:21:36 -07005465 ret = select_devices(adev, in->usecase);
5466 }
5467 }
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005468 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005469 }
5470 }
5471
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +05305472 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_PROFILE, value, sizeof(value));
5473 if (err >= 0) {
5474 strlcpy(in->profile, value, sizeof(in->profile));
5475 ALOGV("updating stream profile with value '%s'", in->profile);
5476 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
5477 &adev->streams_input_cfg_list,
5478 in->device, in->flags, in->format,
5479 in->sample_rate, in->bit_width,
5480 in->profile, &in->app_type_cfg);
5481 }
5482
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005483 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08005484 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005485
5486 str_parms_destroy(parms);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05305487error:
Eric Laurent994a6932013-07-17 11:51:42 -07005488 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005489 return ret;
5490}
5491
5492static char* in_get_parameters(const struct audio_stream *stream,
5493 const char *keys)
5494{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08005495 struct stream_in *in = (struct stream_in *)stream;
5496 struct str_parms *query = str_parms_create_str(keys);
5497 char *str;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08005498 struct str_parms *reply = str_parms_create();
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07005499
5500 if (!query || !reply) {
Alexy Josephaee4fdd2016-01-29 13:02:07 -08005501 if (reply) {
5502 str_parms_destroy(reply);
5503 }
5504 if (query) {
5505 str_parms_destroy(query);
5506 }
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07005507 ALOGE("in_get_parameters: failed to create query or reply");
5508 return NULL;
5509 }
5510
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005511 ALOGV("%s: enter: keys - %s %s ", __func__, use_case_table[in->usecase], keys);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08005512
5513 voice_extn_in_get_parameters(in, query, reply);
5514
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005515 stream_get_parameter_channels(query, reply,
5516 &in->supported_channel_masks[0]);
5517 stream_get_parameter_formats(query, reply,
5518 &in->supported_formats[0]);
5519 stream_get_parameter_rates(query, reply,
5520 &in->supported_sample_rates[0]);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08005521 str = str_parms_to_str(reply);
5522 str_parms_destroy(query);
5523 str_parms_destroy(reply);
5524
5525 ALOGV("%s: exit: returns - %s", __func__, str);
5526 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005527}
5528
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07005529static int in_set_gain(struct audio_stream_in *stream __unused,
5530 float gain __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005531{
5532 return 0;
5533}
5534
5535static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
5536 size_t bytes)
5537{
5538 struct stream_in *in = (struct stream_in *)stream;
Pallavid7c7a272018-01-16 11:22:55 +05305539
5540 if (in == NULL) {
5541 ALOGE("%s: stream_in ptr is NULL", __func__);
5542 return -EINVAL;
5543 }
5544
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005545 struct audio_device *adev = in->dev;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05305546 int ret = -1;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05305547 size_t bytes_read = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005548
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005549 lock_input_stream(in);
Naresh Tanniru4c630392014-05-12 01:05:52 +05305550
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07005551 if (in->is_st_session) {
5552 ALOGVV(" %s: reading on st session bytes=%zu", __func__, bytes);
5553 /* Read from sound trigger HAL */
5554 audio_extn_sound_trigger_read(in, buffer, bytes);
5555 pthread_mutex_unlock(&in->lock);
5556 return bytes;
5557 }
5558
Haynes Mathew George16081042017-05-31 17:16:49 -07005559 if (in->usecase == USECASE_AUDIO_RECORD_MMAP) {
5560 ret = -ENOSYS;
5561 goto exit;
5562 }
5563
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005564 if (in->standby) {
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07005565 pthread_mutex_lock(&adev->lock);
5566 if (in->usecase == USECASE_COMPRESS_VOIP_CALL)
5567 ret = voice_extn_compress_voip_start_input_stream(in);
5568 else
5569 ret = start_input_stream(in);
5570 pthread_mutex_unlock(&adev->lock);
5571 if (ret != 0) {
5572 goto exit;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005573 }
5574 in->standby = 0;
5575 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005576
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005577 // what's the duration requested by the client?
5578 long ns = 0;
5579
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05305580 if (in->pcm && in->config.rate)
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005581 ns = pcm_bytes_to_frames(in->pcm, bytes)*1000000000LL/
5582 in->config.rate;
5583
5584 request_in_focus(in, ns);
5585 bool use_mmap = is_mmap_usecase(in->usecase) || in->realtime;
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07005586
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05305587 if (audio_extn_cin_attached_usecase(in->usecase)) {
5588 ret = audio_extn_cin_read(in, buffer, bytes, &bytes_read);
5589 } else if (in->pcm) {
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05305590 if (audio_extn_ssr_get_stream() == in) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07005591 ret = audio_extn_ssr_read(stream, buffer, bytes);
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05305592 } else if (audio_extn_compr_cap_usecase_supported(in->usecase)) {
Mingming Yine62d7842013-10-25 16:26:03 -07005593 ret = audio_extn_compr_cap_read(in, buffer, bytes);
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005594 } else if (use_mmap) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07005595 ret = pcm_mmap_read(in->pcm, buffer, bytes);
Garmond Leunge2433c32017-09-28 21:51:22 -07005596 } else if (audio_extn_ffv_get_stream() == in) {
5597 ret = audio_extn_ffv_read(stream, buffer, bytes);
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05305598 } else {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07005599 ret = pcm_read(in->pcm, buffer, bytes);
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05305600 /* data from DSP comes in 24_8 format, convert it to 8_24 */
5601 if (!ret && bytes > 0 && (in->format == AUDIO_FORMAT_PCM_8_24_BIT)) {
5602 if (audio_extn_utils_convert_format_24_8_to_8_24(buffer, bytes)
5603 != bytes) {
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05305604 ret = -EINVAL;
5605 goto exit;
5606 }
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05305607 } else if (ret < 0) {
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05305608 ret = -errno;
5609 }
5610 }
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05305611 /* bytes read is always set to bytes for non compress usecases */
5612 bytes_read = bytes;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005613 }
5614
Haynes Mathew George5beddd42016-06-27 18:33:40 -07005615 release_in_focus(in);
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07005616
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005617 /*
5618 * Instead of writing zeroes here, we could trust the hardware
5619 * to always provide zeroes when muted.
5620 */
Pavan Chikkala63964842014-12-04 10:48:28 +05305621 if (ret == 0 && voice_get_mic_mute(adev) && !voice_is_in_call_rec_stream(in) &&
5622 in->usecase != USECASE_AUDIO_RECORD_AFE_PROXY)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005623 memset(buffer, 0, bytes);
5624
5625exit:
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07005626 if (-ENETRESET == ret)
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05305627 in->card_status = CARD_STATUS_OFFLINE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005628 pthread_mutex_unlock(&in->lock);
5629
5630 if (ret != 0) {
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05305631 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05305632 pthread_mutex_lock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05305633 voice_extn_compress_voip_close_input_stream(&in->stream.common);
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05305634 pthread_mutex_unlock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05305635 in->standby = true;
5636 }
Sharad Sangled17c9122017-03-20 15:58:52 +05305637 if (!audio_extn_cin_attached_usecase(in->usecase)) {
5638 bytes_read = bytes;
5639 memset(buffer, 0, bytes);
5640 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005641 in_standby(&in->stream.common);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07005642 ALOGV("%s: read failed status %d- sleeping for buffer duration", __func__, ret);
Ashish Jainbbce4322016-02-16 13:25:27 +05305643 usleep((uint64_t)bytes * 1000000 / audio_stream_in_frame_size(stream) /
Naresh Tanniru4c630392014-05-12 01:05:52 +05305644 in_get_sample_rate(&in->stream.common));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005645 }
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05305646 return bytes_read;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005647}
5648
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07005649static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005650{
5651 return 0;
5652}
5653
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07005654static int add_remove_audio_effect(const struct audio_stream *stream,
5655 effect_handle_t effect,
5656 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005657{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07005658 struct stream_in *in = (struct stream_in *)stream;
5659 int status = 0;
5660 effect_descriptor_t desc;
5661
5662 status = (*effect)->get_descriptor(effect, &desc);
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07005663 ALOGV("%s: status %d in->standby %d enable:%d", __func__, status, in->standby, enable);
5664
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07005665 if (status != 0)
5666 return status;
5667
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005668 lock_input_stream(in);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07005669 pthread_mutex_lock(&in->dev->lock);
kunleizd96526c2018-04-09 11:12:32 +08005670 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
5671 in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07005672 in->enable_aec != enable &&
5673 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
5674 in->enable_aec = enable;
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07005675 if (!in->standby) {
5676 if (enable_disable_effect(in->dev, EFFECT_AEC, enable) == ENOSYS)
5677 select_devices(in->dev, in->usecase);
5678 }
5679
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07005680 }
Ravi Kumar Alamanda198185e2013-11-07 15:42:19 -08005681 if (in->enable_ns != enable &&
5682 (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
5683 in->enable_ns = enable;
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07005684 if (!in->standby) {
kunleizd96526c2018-04-09 11:12:32 +08005685 if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
5686 in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) {
Vikram Pandurangadf59cae2017-08-03 18:04:55 -07005687 if (enable_disable_effect(in->dev, EFFECT_NS, enable) == ENOSYS)
5688 select_devices(in->dev, in->usecase);
5689 } else
5690 select_devices(in->dev, in->usecase);
5691 }
Ravi Kumar Alamanda198185e2013-11-07 15:42:19 -08005692 }
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07005693 pthread_mutex_unlock(&in->dev->lock);
5694 pthread_mutex_unlock(&in->lock);
5695
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005696 return 0;
5697}
5698
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07005699static int in_add_audio_effect(const struct audio_stream *stream,
5700 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005701{
Eric Laurent994a6932013-07-17 11:51:42 -07005702 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07005703 return add_remove_audio_effect(stream, effect, true);
5704}
5705
5706static int in_remove_audio_effect(const struct audio_stream *stream,
5707 effect_handle_t effect)
5708{
Eric Laurent994a6932013-07-17 11:51:42 -07005709 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07005710 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005711}
5712
Haynes Mathew George16081042017-05-31 17:16:49 -07005713static int in_stop(const struct audio_stream_in* stream)
5714{
5715 struct stream_in *in = (struct stream_in *)stream;
5716 struct audio_device *adev = in->dev;
5717
5718 int ret = -ENOSYS;
5719 ALOGV("%s", __func__);
5720 pthread_mutex_lock(&adev->lock);
5721 if (in->usecase == USECASE_AUDIO_RECORD_MMAP && !in->standby &&
5722 in->capture_started && in->pcm != NULL) {
5723 pcm_stop(in->pcm);
5724 ret = stop_input_stream(in);
5725 in->capture_started = false;
5726 }
5727 pthread_mutex_unlock(&adev->lock);
5728 return ret;
5729}
5730
5731static int in_start(const struct audio_stream_in* stream)
5732{
5733 struct stream_in *in = (struct stream_in *)stream;
5734 struct audio_device *adev = in->dev;
5735 int ret = -ENOSYS;
5736
5737 ALOGV("%s in %p", __func__, in);
5738 pthread_mutex_lock(&adev->lock);
5739 if (in->usecase == USECASE_AUDIO_RECORD_MMAP && !in->standby &&
5740 !in->capture_started && in->pcm != NULL) {
5741 if (!in->capture_started) {
5742 ret = start_input_stream(in);
5743 if (ret == 0) {
5744 in->capture_started = true;
5745 }
5746 }
5747 }
5748 pthread_mutex_unlock(&adev->lock);
5749 return ret;
5750}
5751
5752static int in_create_mmap_buffer(const struct audio_stream_in *stream,
5753 int32_t min_size_frames,
5754 struct audio_mmap_buffer_info *info)
5755{
5756 struct stream_in *in = (struct stream_in *)stream;
5757 struct audio_device *adev = in->dev;
5758 int ret = 0;
Aniket Kumar Lataf9f246e2017-09-15 15:20:16 -07005759 unsigned int offset1 = 0;
5760 unsigned int frames1 = 0;
Haynes Mathew George16081042017-05-31 17:16:49 -07005761 const char *step = "";
Arun Mirpuriebe78a72018-10-04 18:23:46 -07005762 uint32_t mmap_size = 0;
5763 uint32_t buffer_size = 0;
Haynes Mathew George16081042017-05-31 17:16:49 -07005764
5765 pthread_mutex_lock(&adev->lock);
5766 ALOGV("%s in %p", __func__, in);
5767
5768 if (info == NULL || min_size_frames == 0) {
5769 ALOGE("%s invalid argument info %p min_size_frames %d", __func__, info, min_size_frames);
5770 ret = -EINVAL;
5771 goto exit;
5772 }
5773 if (in->usecase != USECASE_AUDIO_RECORD_MMAP || !in->standby) {
5774 ALOGE("%s: usecase = %d, standby = %d", __func__, in->usecase, in->standby);
5775 ALOGV("%s in %p", __func__, in);
5776 ret = -ENOSYS;
5777 goto exit;
5778 }
5779 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
5780 if (in->pcm_device_id < 0) {
5781 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
5782 __func__, in->pcm_device_id, in->usecase);
5783 ret = -EINVAL;
5784 goto exit;
5785 }
5786
5787 adjust_mmap_period_count(&in->config, min_size_frames);
5788
5789 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
5790 __func__, adev->snd_card, in->pcm_device_id, in->config.channels);
5791 in->pcm = pcm_open(adev->snd_card, in->pcm_device_id,
5792 (PCM_IN | PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC), &in->config);
5793 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) {
5794 step = "open";
5795 ret = -ENODEV;
5796 goto exit;
5797 }
5798
5799 ret = pcm_mmap_begin(in->pcm, &info->shared_memory_address, &offset1, &frames1);
5800 if (ret < 0) {
5801 step = "begin";
5802 goto exit;
5803 }
Haynes Mathew George16081042017-05-31 17:16:49 -07005804
Arun Mirpuriebe78a72018-10-04 18:23:46 -07005805 info->buffer_size_frames = pcm_get_buffer_size(in->pcm);
5806 buffer_size = pcm_frames_to_bytes(in->pcm, info->buffer_size_frames);
5807 info->burst_size_frames = in->config.period_size;
5808 ret = platform_get_mmap_data_fd(adev->platform,
5809 in->pcm_device_id, 1 /*capture*/,
5810 &info->shared_memory_fd,
5811 &mmap_size);
5812 if (ret < 0) {
5813 // Fall back to non exclusive mode
5814 info->shared_memory_fd = pcm_get_poll_fd(in->pcm);
5815 } else {
5816 if (mmap_size < buffer_size) {
5817 step = "mmap";
5818 goto exit;
5819 }
5820 // FIXME: indicate exclusive mode support by returning a negative buffer size
5821 info->buffer_size_frames *= -1;
5822 }
5823
5824 memset(info->shared_memory_address, 0, buffer_size);
Haynes Mathew George16081042017-05-31 17:16:49 -07005825
5826 ret = pcm_mmap_commit(in->pcm, 0, MMAP_PERIOD_SIZE);
5827 if (ret < 0) {
5828 step = "commit";
5829 goto exit;
5830 }
5831
5832 in->standby = false;
5833 ret = 0;
5834
5835 ALOGV("%s: got mmap buffer address %p info->buffer_size_frames %d",
5836 __func__, info->shared_memory_address, info->buffer_size_frames);
5837
5838exit:
5839 if (ret != 0) {
5840 if (in->pcm == NULL) {
5841 ALOGE("%s: %s - %d", __func__, step, ret);
5842 } else {
5843 ALOGE("%s: %s %s", __func__, step, pcm_get_error(in->pcm));
5844 pcm_close(in->pcm);
5845 in->pcm = NULL;
5846 }
5847 }
5848 pthread_mutex_unlock(&adev->lock);
5849 return ret;
5850}
5851
5852static int in_get_mmap_position(const struct audio_stream_in *stream,
5853 struct audio_mmap_position *position)
5854{
5855 struct stream_in *in = (struct stream_in *)stream;
5856 ALOGVV("%s", __func__);
5857 if (position == NULL) {
5858 return -EINVAL;
5859 }
5860 if (in->usecase != USECASE_AUDIO_RECORD_MMAP) {
5861 return -ENOSYS;
5862 }
5863 if (in->pcm == NULL) {
5864 return -ENOSYS;
5865 }
5866 struct timespec ts = { 0, 0 };
5867 int ret = pcm_mmap_get_hw_ptr(in->pcm, (unsigned int *)&position->position_frames, &ts);
5868 if (ret < 0) {
5869 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
5870 return ret;
5871 }
Naresh Tannirua15d7e92018-11-02 09:49:44 +05305872 position->time_nanoseconds = ts.tv_sec*1000000000LL + ts.tv_nsec;
Haynes Mathew George16081042017-05-31 17:16:49 -07005873 return 0;
5874}
5875
Naresh Tannirudcb47c52018-06-25 16:23:32 +05305876static int in_get_active_microphones(const struct audio_stream_in *stream,
5877 struct audio_microphone_characteristic_t *mic_array,
5878 size_t *mic_count) {
5879 struct stream_in *in = (struct stream_in *)stream;
5880 struct audio_device *adev = in->dev;
5881 ALOGVV("%s", __func__);
5882
5883 lock_input_stream(in);
5884 pthread_mutex_lock(&adev->lock);
5885 int ret = platform_get_active_microphones(adev->platform,
5886 audio_channel_count_from_in_mask(in->channel_mask),
5887 in->usecase, mic_array, mic_count);
5888 pthread_mutex_unlock(&adev->lock);
5889 pthread_mutex_unlock(&in->lock);
5890
5891 return ret;
5892}
5893
5894static int adev_get_microphones(const struct audio_hw_device *dev,
5895 struct audio_microphone_characteristic_t *mic_array,
5896 size_t *mic_count) {
5897 struct audio_device *adev = (struct audio_device *)dev;
5898 ALOGVV("%s", __func__);
5899
5900 pthread_mutex_lock(&adev->lock);
5901 int ret = platform_get_microphones(adev->platform, mic_array, mic_count);
5902 pthread_mutex_unlock(&adev->lock);
5903
5904 return ret;
5905}
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05305906int adev_open_output_stream(struct audio_hw_device *dev,
Haynes Mathew George16081042017-05-31 17:16:49 -07005907 audio_io_handle_t handle,
5908 audio_devices_t devices,
5909 audio_output_flags_t flags,
5910 struct audio_config *config,
5911 struct audio_stream_out **stream_out,
5912 const char *address __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005913{
5914 struct audio_device *adev = (struct audio_device *)dev;
5915 struct stream_out *out;
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05305916 int ret = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07005917 audio_format_t format;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08005918 struct adsp_hdlr_stream_cfg hdlr_stream_cfg;
Manish Dewangan21a850a2017-08-14 12:03:55 +05305919 bool is_direct_passthough = false;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005920 bool is_hdmi = devices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
5921 bool is_usb_dev = audio_is_usb_out_device(devices) &&
5922 (devices != AUDIO_DEVICE_OUT_USB_ACCESSORY);
5923 bool direct_dev = is_hdmi || is_usb_dev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005924
kunleizdff872d2018-08-20 14:40:33 +08005925 if (is_usb_dev && (!audio_extn_usb_connected(NULL))) {
kunleizd6a9e0c2018-07-30 15:38:52 +08005926 is_usb_dev = false;
5927 devices = AUDIO_DEVICE_OUT_SPEAKER;
5928 ALOGW("%s: ignore set device to non existing USB card, use output device(%#x)",
5929 __func__, devices);
5930 }
5931
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005932 *stream_out = NULL;
Naresh Tanniru80659832014-06-04 18:17:56 +05305933
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005934 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
5935
Mingming Yin3a941d42016-02-17 18:08:05 -08005936 ALOGD("%s: enter: format(%#x) sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)\
5937 stream_handle(%p)", __func__, config->format, config->sample_rate, config->channel_mask,
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05305938 devices, flags, &out->stream);
5939
5940
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08005941 if (!out) {
5942 return -ENOMEM;
5943 }
5944
Haynes Mathew George204045b2015-02-25 20:32:03 -08005945 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07005946 pthread_mutex_init(&out->pre_lock, (const pthread_mutexattr_t *) NULL);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05305947 pthread_mutex_init(&out->compr_mute_lock, (const pthread_mutexattr_t *) NULL);
Zhou Song48453a02018-01-10 17:50:59 +08005948 pthread_mutex_init(&out->position_query_lock, (const pthread_mutexattr_t *) NULL);
Haynes Mathew George204045b2015-02-25 20:32:03 -08005949 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
5950
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005951 if (devices == AUDIO_DEVICE_NONE)
5952 devices = AUDIO_DEVICE_OUT_SPEAKER;
5953
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005954 out->flags = flags;
5955 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07005956 out->dev = adev;
Aalique Grahame65780b52017-09-27 14:59:56 -07005957 out->hal_op_format = out->hal_ip_format = format = out->format = config->format;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07005958 out->sample_rate = config->sample_rate;
Sachin Mohan Gadag3d09acd2017-06-19 12:43:44 +05305959 out->channel_mask = config->channel_mask;
Ramjee Singh5857aeb2017-08-03 19:18:50 +05305960 if (out->channel_mask == AUDIO_CHANNEL_NONE)
5961 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
5962 else
5963 out->supported_channel_masks[0] = out->channel_mask;
Eric Laurentc4aef752013-09-12 17:45:53 -07005964 out->handle = handle;
Mingming Yin3ee55c62014-08-04 14:23:35 -07005965 out->bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Alexy Josephaa54c872014-12-03 02:46:47 -08005966 out->non_blocking = 0;
Ashish Jain83a6cc22016-06-28 14:34:17 +05305967 out->convert_buffer = NULL;
Ashish Jain1b9b30c2017-05-18 20:57:40 +05305968 out->started = 0;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05305969 out->a2dp_compress_mute = false;
Aniket Kumar Lata932f4872017-11-06 18:29:44 -08005970 out->hal_output_suspend_supported = 0;
5971 out->dynamic_pm_qos_config_supported = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005972
Nikhil Laturkar26b690b2017-07-25 11:06:14 +05305973 if ((flags & AUDIO_OUTPUT_FLAG_BD) &&
Satish Babu Patakokila37e7c482018-02-02 11:50:06 +05305974 (property_get_bool("vendor.audio.matrix.limiter.enable", false)))
Ben Romberger6c4d3812017-06-13 17:46:45 -07005975 platform_set_device_params(out, DEVICE_PARAM_LIMITER_ID, 1);
5976
Haynes Mathew George484e8d22017-07-31 18:55:17 -07005977 if (audio_is_linear_pcm(out->format) &&
5978 out->flags == AUDIO_OUTPUT_FLAG_NONE && direct_dev) {
5979 pthread_mutex_lock(&adev->lock);
5980 if (is_hdmi) {
5981 ALOGV("AUDIO_DEVICE_OUT_AUX_DIGITAL and DIRECT|OFFLOAD, check hdmi caps");
5982 ret = read_hdmi_sink_caps(out);
5983 } else if (is_usb_dev) {
5984 ret = read_usb_sup_params_and_compare(true /*is_playback*/,
5985 &config->format,
5986 &out->supported_formats[0],
5987 MAX_SUPPORTED_FORMATS,
5988 &config->channel_mask,
5989 &out->supported_channel_masks[0],
5990 MAX_SUPPORTED_CHANNEL_MASKS,
5991 &config->sample_rate,
5992 &out->supported_sample_rates[0],
5993 MAX_SUPPORTED_SAMPLE_RATES);
5994 ALOGV("plugged dev USB ret %d", ret);
5995 } else {
5996 ret = -1;
5997 }
5998 pthread_mutex_unlock(&adev->lock);
5999 if (ret != 0) {
Mingming Yin3a941d42016-02-17 18:08:05 -08006000 if (ret == -ENOSYS) {
6001 /* ignore and go with default */
6002 ret = 0;
6003 } else {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07006004 ALOGE("error reading direct dev sink caps");
Mingming Yin3a941d42016-02-17 18:08:05 -08006005 goto error_open;
6006 }
6007 }
6008 }
6009
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006010 /* Init use case and pcm_config */
Vikram Pandurangafa3128d2017-10-16 16:57:22 -07006011#ifndef COMPRESS_VOIP_ENABLED
Vikram Panduranga93f080e2017-06-07 18:16:14 -07006012 if (out->flags == (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_VOIP_RX) &&
6013 (out->sample_rate == 8000 || out->sample_rate == 16000 ||
6014 out->sample_rate == 32000 || out->sample_rate == 48000)) {
6015 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_MONO;
6016 out->channel_mask = AUDIO_CHANNEL_OUT_MONO;
6017 out->usecase = USECASE_AUDIO_PLAYBACK_VOIP;
6018
6019 out->config = default_pcm_config_voip_copp;
6020 out->config.period_size = VOIP_IO_BUF_SIZE(out->sample_rate, DEFAULT_VOIP_BUF_DURATION_MS, DEFAULT_VOIP_BIT_DEPTH_BYTE)/2;
6021 out->config.rate = out->sample_rate;
6022
6023#else
Preetam Singh Ranawat319b1cd2017-10-12 10:46:30 +05306024 if ((out->dev->mode == AUDIO_MODE_IN_COMMUNICATION || voice_extn_compress_voip_is_active(out->dev)) &&
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08006025 (out->flags == (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_VOIP_RX)) &&
Narsinga Rao Chella1eceff82013-12-02 19:25:28 -08006026 (voice_extn_compress_voip_is_config_supported(config))) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08006027 ret = voice_extn_compress_voip_open_output_stream(out);
6028 if (ret != 0) {
6029 ALOGE("%s: Compress voip output cannot be opened, error:%d",
6030 __func__, ret);
6031 goto error_open;
6032 }
Vikram Panduranga93f080e2017-06-07 18:16:14 -07006033#endif
Haynes Mathew George484e8d22017-07-31 18:55:17 -07006034 } else if (audio_is_linear_pcm(out->format) &&
6035 out->flags == AUDIO_OUTPUT_FLAG_NONE && is_usb_dev) {
6036 out->channel_mask = config->channel_mask;
6037 out->sample_rate = config->sample_rate;
6038 out->format = config->format;
6039 out->usecase = USECASE_AUDIO_PLAYBACK_HIFI;
6040 // does this change?
6041 out->config = is_hdmi ? pcm_config_hdmi_multi : pcm_config_hifi;
6042 out->config.rate = config->sample_rate;
6043 out->config.channels = audio_channel_count_from_out_mask(out->channel_mask);
6044 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels *
6045 audio_bytes_per_sample(config->format));
6046 out->config.format = pcm_format_from_audio_format(out->format);
vivek mehta0ea887a2015-08-26 14:01:20 -07006047 } else if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
Dhananjay Kumarac341582017-02-23 23:42:25 +05306048 (out->flags == AUDIO_OUTPUT_FLAG_DIRECT)) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306049 pthread_mutex_lock(&adev->lock);
6050 bool offline = (adev->card_status == CARD_STATUS_OFFLINE);
6051 pthread_mutex_unlock(&adev->lock);
6052
6053 // reject offload during card offline to allow
6054 // fallback to s/w paths
6055 if (offline) {
6056 ret = -ENODEV;
6057 goto error_open;
6058 }
vivek mehta0ea887a2015-08-26 14:01:20 -07006059
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006060 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
6061 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
6062 ALOGE("%s: Unsupported Offload information", __func__);
6063 ret = -EINVAL;
6064 goto error_open;
6065 }
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07006066
Atul Khare3fa6e542017-08-09 00:56:17 +05306067 if (config->offload_info.format == 0)
6068 config->offload_info.format = config->format;
6069 if (config->offload_info.sample_rate == 0)
6070 config->offload_info.sample_rate = config->sample_rate;
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07006071
Mingming Yin90310102013-11-13 16:57:00 -08006072 if (!is_supported_format(config->offload_info.format) &&
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05306073 !audio_extn_passthru_is_supported_format(config->offload_info.format)) {
vivek mehta0ea887a2015-08-26 14:01:20 -07006074 ALOGE("%s: Unsupported audio format %x " , __func__, config->offload_info.format);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006075 ret = -EINVAL;
6076 goto error_open;
6077 }
6078
Ben Romberger0f8c87b2017-05-24 17:41:11 -07006079 /* TrueHD only supported for 48k multiples (48k, 96k, 192k) */
6080 if ((config->offload_info.format == AUDIO_FORMAT_DOLBY_TRUEHD) &&
6081 (audio_extn_passthru_is_passthrough_stream(out)) &&
6082 !((config->sample_rate == 48000) ||
6083 (config->sample_rate == 96000) ||
6084 (config->sample_rate == 192000))) {
6085 ALOGE("%s: Unsupported sample rate %d for audio format %x",
6086 __func__, config->sample_rate, config->offload_info.format);
6087 ret = -EINVAL;
6088 goto error_open;
6089 }
6090
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006091 out->compr_config.codec = (struct snd_codec *)
6092 calloc(1, sizeof(struct snd_codec));
6093
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07006094 if (!out->compr_config.codec) {
6095 ret = -ENOMEM;
6096 goto error_open;
6097 }
6098
Dhananjay Kumarac341582017-02-23 23:42:25 +05306099 out->stream.pause = out_pause;
6100 out->stream.resume = out_resume;
6101 out->stream.flush = out_flush;
Ashish Jain4847e9d2017-08-17 19:16:57 +05306102 out->stream.set_callback = out_set_callback;
Dhananjay Kumarac341582017-02-23 23:42:25 +05306103 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Mingming Yin21d60472015-09-30 13:56:25 -07006104 out->stream.drain = out_drain;
Dhananjay Kumarac341582017-02-23 23:42:25 +05306105 out->usecase = get_offload_usecase(adev, true /* is_compress */);
vivek mehta446c3962015-09-14 10:57:35 -07006106 ALOGV("Compress Offload usecase .. usecase selected %d", out->usecase);
Dhananjay Kumarac341582017-02-23 23:42:25 +05306107 } else {
6108 out->usecase = get_offload_usecase(adev, false /* is_compress */);
6109 ALOGV("non-offload DIRECT_usecase ... usecase selected %d ", out->usecase);
vivek mehta0ea887a2015-08-26 14:01:20 -07006110 }
vivek mehta446c3962015-09-14 10:57:35 -07006111
6112 if (out->usecase == USECASE_INVALID) {
Mingming Yin3a941d42016-02-17 18:08:05 -08006113 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL &&
6114 config->format == 0 && config->sample_rate == 0 &&
6115 config->channel_mask == 0) {
Mingming Yin21854652016-04-13 11:54:02 -07006116 ALOGI("%s dummy open to query sink capability",__func__);
Mingming Yin3a941d42016-02-17 18:08:05 -08006117 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
6118 } else {
6119 ALOGE("%s, Max allowed OFFLOAD usecase reached ... ", __func__);
6120 ret = -EEXIST;
6121 goto error_open;
6122 }
vivek mehta446c3962015-09-14 10:57:35 -07006123 }
6124
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006125 if (config->offload_info.channel_mask)
6126 out->channel_mask = config->offload_info.channel_mask;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08006127 else if (config->channel_mask) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006128 out->channel_mask = config->channel_mask;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08006129 config->offload_info.channel_mask = config->channel_mask;
Haynes Mathew Georgea99f7532016-08-24 16:01:21 -07006130 } else {
Dhananjay Kumarac341582017-02-23 23:42:25 +05306131 ALOGE("out->channel_mask not set for OFFLOAD/DIRECT usecase");
Haynes Mathew Georgea99f7532016-08-24 16:01:21 -07006132 ret = -EINVAL;
6133 goto error_open;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08006134 }
Haynes Mathew Georgea99f7532016-08-24 16:01:21 -07006135
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07006136 format = out->format = config->offload_info.format;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006137 out->sample_rate = config->offload_info.sample_rate;
6138
Mingming Yin3ee55c62014-08-04 14:23:35 -07006139 out->bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006140
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05306141 out->compr_config.codec->id = get_snd_codec_id(config->offload_info.format);
Satish Babu Patakokila5933e972017-08-24 12:22:08 +05306142 if (audio_extn_utils_is_dolby_format(config->offload_info.format)) {
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05306143 audio_extn_dolby_send_ddp_endp_params(adev);
6144 audio_extn_dolby_set_dmid(adev);
6145 }
vivek mehta0ea887a2015-08-26 14:01:20 -07006146
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006147 out->compr_config.codec->sample_rate =
Ravi Kumar Alamandab91bff32014-11-14 12:05:54 -08006148 config->offload_info.sample_rate;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006149 out->compr_config.codec->bit_rate =
6150 config->offload_info.bit_rate;
6151 out->compr_config.codec->ch_in =
Dhanalakshmi Siddania15c6792016-08-10 15:33:53 +05306152 audio_channel_count_from_out_mask(out->channel_mask);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006153 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
Satish Babu Patakokilaa395a9e2016-11-01 12:18:49 +05306154 /* Update bit width only for non passthrough usecases.
6155 * For passthrough usecases, the output will always be opened @16 bit
6156 */
6157 if (!audio_extn_passthru_is_passthrough_stream(out))
6158 out->bit_width = AUDIO_OUTPUT_BIT_WIDTH;
Naresh Tanniruee3499a2017-01-05 14:05:35 +05306159
6160 if (out->flags & AUDIO_OUTPUT_FLAG_TIMESTAMP)
6161 out->compr_config.codec->flags |= COMPRESSED_TIMESTAMP_FLAG;
6162 ALOGVV("%s : out->compr_config.codec->flags -> (%#x) ", __func__, out->compr_config.codec->flags);
6163
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07006164 /*TODO: Do we need to change it for passthrough */
6165 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_RAW;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006166
Manish Dewangana6fc5442015-08-24 20:30:31 +05306167 if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC)
6168 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_RAW;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05306169 else if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS)
Manish Dewangana6fc5442015-08-24 20:30:31 +05306170 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_MP4ADTS;
Arun Kumar Dasari3b174182016-12-27 13:01:14 +05306171 else if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_LATM)
6172 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_MP4LATM;
Ashish Jainf1eaa582016-05-23 20:54:24 +05306173
6174 if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) ==
6175 AUDIO_FORMAT_PCM) {
6176
6177 /*Based on platform support, configure appropriate alsa format for corresponding
6178 *hal input format.
6179 */
6180 out->compr_config.codec->format = hal_format_to_alsa(
6181 config->offload_info.format);
6182
Ashish Jain83a6cc22016-06-28 14:34:17 +05306183 out->hal_op_format = alsa_format_to_hal(
Ashish Jainf1eaa582016-05-23 20:54:24 +05306184 out->compr_config.codec->format);
Ashish Jain83a6cc22016-06-28 14:34:17 +05306185 out->hal_ip_format = out->format;
Ashish Jainf1eaa582016-05-23 20:54:24 +05306186
Dhananjay Kumarac341582017-02-23 23:42:25 +05306187 /*for direct non-compress playback populate bit_width based on selected alsa format as
Ashish Jainf1eaa582016-05-23 20:54:24 +05306188 *hal input format and alsa format might differ based on platform support.
6189 */
6190 out->bit_width = audio_bytes_per_sample(
Ashish Jain83a6cc22016-06-28 14:34:17 +05306191 out->hal_op_format) << 3;
Ashish Jainf1eaa582016-05-23 20:54:24 +05306192
6193 out->compr_config.fragments = DIRECT_PCM_NUM_FRAGMENTS;
6194
6195 /* Check if alsa session is configured with the same format as HAL input format,
6196 * if not then derive correct fragment size needed to accomodate the
6197 * conversion of HAL input format to alsa format.
6198 */
6199 audio_extn_utils_update_direct_pcm_fragment_size(out);
6200
6201 /*if hal input and output fragment size is different this indicates HAL input format is
6202 *not same as the alsa format
6203 */
Ashish Jain83a6cc22016-06-28 14:34:17 +05306204 if (out->hal_fragment_size != out->compr_config.fragment_size) {
Ashish Jainf1eaa582016-05-23 20:54:24 +05306205 /*Allocate a buffer to convert input data to the alsa configured format.
6206 *size of convert buffer is equal to the size required to hold one fragment size
6207 *worth of pcm data, this is because flinger does not write more than fragment_size
6208 */
Ashish Jain83a6cc22016-06-28 14:34:17 +05306209 out->convert_buffer = calloc(1,out->compr_config.fragment_size);
6210 if (out->convert_buffer == NULL){
Ashish Jainf1eaa582016-05-23 20:54:24 +05306211 ALOGE("Allocation failed for convert buffer for size %d", out->compr_config.fragment_size);
6212 ret = -ENOMEM;
6213 goto error_open;
6214 }
6215 }
6216 } else if (audio_extn_passthru_is_passthrough_stream(out)) {
6217 out->compr_config.fragment_size =
6218 audio_extn_passthru_get_buffer_size(&config->offload_info);
6219 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
6220 } else {
6221 out->compr_config.fragment_size =
6222 platform_get_compress_offload_buffer_size(&config->offload_info);
6223 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
6224 }
Mingming Yin3ee55c62014-08-04 14:23:35 -07006225
Naresh Tanniruee3499a2017-01-05 14:05:35 +05306226 if (out->flags & AUDIO_OUTPUT_FLAG_TIMESTAMP) {
6227 out->compr_config.fragment_size += sizeof(struct snd_codec_metadata);
6228 }
Amit Shekhar6f461b12014-08-01 14:52:58 -07006229 if (config->offload_info.format == AUDIO_FORMAT_FLAC)
Satya Krishna Pindiproli5d82d012015-08-12 18:21:25 +05306230 out->compr_config.codec->options.flac_dec.sample_size = AUDIO_OUTPUT_BIT_WIDTH;
Mingming Yin3ee55c62014-08-04 14:23:35 -07006231
Dhanalakshmi Siddani18737932016-11-29 17:33:17 +05306232 if (config->offload_info.format == AUDIO_FORMAT_APTX) {
6233 audio_extn_send_aptx_dec_bt_addr_to_dsp(out);
6234 }
6235
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006236 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
6237 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07006238
Manish Dewangan69426c82017-01-30 17:35:36 +05306239 if ((flags & AUDIO_OUTPUT_FLAG_TIMESTAMP) &&
6240 (flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC)) {
6241 out->render_mode = RENDER_MODE_AUDIO_STC_MASTER;
6242 } else if(flags & AUDIO_OUTPUT_FLAG_TIMESTAMP) {
6243 out->render_mode = RENDER_MODE_AUDIO_MASTER;
6244 } else {
6245 out->render_mode = RENDER_MODE_AUDIO_NO_TIMESTAMP;
6246 }
Alexy Josephaa54c872014-12-03 02:46:47 -08006247
Naresh Tanniru29bce4e2017-04-27 17:54:30 +05306248 memset(&out->channel_map_param, 0,
6249 sizeof(struct audio_out_channel_map_param));
6250
Haynes Mathew George352f27b2013-07-26 00:00:15 -07006251 out->send_new_metadata = 1;
Chaithanya Krishna Bacharajua70cb6a2015-07-24 14:15:05 +05306252 out->send_next_track_params = false;
6253 out->is_compr_metadata_avail = false;
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08006254 out->offload_state = OFFLOAD_STATE_IDLE;
6255 out->playback_started = 0;
Zhou Song48453a02018-01-10 17:50:59 +08006256 out->writeAt.tv_sec = 0;
6257 out->writeAt.tv_nsec = 0;
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08006258
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08006259 audio_extn_dts_create_state_notifier_node(out->usecase);
6260
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006261 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
6262 __func__, config->offload_info.version,
6263 config->offload_info.bit_rate);
Ashish Jain5106d362016-05-11 19:23:33 +05306264
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05306265 /* Check if DSD audio format is supported in codec
6266 * and there is no active native DSD use case
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05306267 */
6268
6269 if ((config->format == AUDIO_FORMAT_DSD) &&
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05306270 (!platform_check_codec_dsd_support(adev->platform) ||
6271 audio_is_dsd_native_stream_active(adev))) {
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05306272 ret = -EINVAL;
6273 goto error_open;
6274 }
6275
Ashish Jain5106d362016-05-11 19:23:33 +05306276 /* Disable gapless if any of the following is true
6277 * passthrough playback
6278 * AV playback
Dhananjay Kumarac341582017-02-23 23:42:25 +05306279 * non compressed Direct playback
Ashish Jain5106d362016-05-11 19:23:33 +05306280 */
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05306281 if (audio_extn_passthru_is_passthrough_stream(out) ||
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05306282 (config->format == AUDIO_FORMAT_DSD) ||
Naresh Tanniru928f0862017-04-07 16:44:23 -07006283 (config->format == AUDIO_FORMAT_IEC61937) ||
Preetam Singh Ranawatf5fbdd62016-09-29 18:38:31 +05306284 config->offload_info.has_video ||
Dhananjay Kumarac341582017-02-23 23:42:25 +05306285 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Ashish Jain5106d362016-05-11 19:23:33 +05306286 check_and_set_gapless_mode(adev, false);
6287 } else
6288 check_and_set_gapless_mode(adev, true);
Mingming Yin21854652016-04-13 11:54:02 -07006289
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +05306290 if (audio_extn_passthru_is_passthrough_stream(out)) {
Mingming Yin21854652016-04-13 11:54:02 -07006291 out->flags |= AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH;
6292 }
Preetam Singh Ranawatcb6212e2016-07-19 18:33:53 +05306293 if (config->format == AUDIO_FORMAT_DSD) {
6294 out->flags |= AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH;
6295 out->compr_config.codec->compr_passthr = PASSTHROUGH_DSD;
6296 }
Aalique Grahame0359a1f2016-09-08 16:54:22 -07006297
6298 create_offload_callback_thread(out);
6299
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07006300 } else if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006301 switch (config->sample_rate) {
6302 case 0:
6303 out->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
6304 break;
6305 case 8000:
6306 case 16000:
6307 case 48000:
6308 out->sample_rate = config->sample_rate;
6309 break;
6310 default:
6311 ALOGE("%s: Unsupported sampling rate %d for Incall Music", __func__,
6312 config->sample_rate);
6313 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
6314 ret = -EINVAL;
6315 goto error_open;
6316 }
6317 //FIXME: add support for MONO stream configuration when audioflinger mixer supports it
6318 switch (config->channel_mask) {
6319 case AUDIO_CHANNEL_NONE:
6320 case AUDIO_CHANNEL_OUT_STEREO:
6321 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
6322 break;
6323 default:
6324 ALOGE("%s: Unsupported channel mask %#x for Incall Music", __func__,
6325 config->channel_mask);
6326 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
6327 ret = -EINVAL;
6328 goto error_open;
6329 }
6330 switch (config->format) {
6331 case AUDIO_FORMAT_DEFAULT:
6332 case AUDIO_FORMAT_PCM_16_BIT:
6333 out->format = AUDIO_FORMAT_PCM_16_BIT;
6334 break;
6335 default:
6336 ALOGE("%s: Unsupported format %#x for Incall Music", __func__,
6337 config->format);
6338 config->format = AUDIO_FORMAT_PCM_16_BIT;
6339 ret = -EINVAL;
6340 goto error_open;
6341 }
6342
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05306343 ret = voice_extn_check_and_set_incall_music_usecase(adev, out);
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07006344 if (ret != 0) {
6345 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006346 __func__, ret);
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07006347 goto error_open;
6348 }
Arun Mirpuri7da752a2018-09-11 18:01:15 -07006349 } else if (out->devices == AUDIO_DEVICE_OUT_TELEPHONY_TX) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07006350 if (config->sample_rate == 0)
6351 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
6352 if (config->sample_rate != 48000 && config->sample_rate != 16000 &&
6353 config->sample_rate != 8000) {
6354 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
6355 ret = -EINVAL;
6356 goto error_open;
6357 }
6358 out->sample_rate = config->sample_rate;
6359 out->config.rate = config->sample_rate;
6360 if (config->format == AUDIO_FORMAT_DEFAULT)
6361 config->format = AUDIO_FORMAT_PCM_16_BIT;
6362 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
6363 config->format = AUDIO_FORMAT_PCM_16_BIT;
6364 ret = -EINVAL;
6365 goto error_open;
6366 }
6367 out->format = config->format;
6368 out->usecase = USECASE_AUDIO_PLAYBACK_AFE_PROXY;
6369 out->config = pcm_config_afe_proxy_playback;
6370 adev->voice_tx_output = out;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07006371 } else {
Ashish Jain058165c2016-09-28 23:18:48 +05306372 unsigned int channels = 0;
6373 /*Update config params to default if not set by the caller*/
6374 if (config->sample_rate == 0)
6375 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
6376 if (config->channel_mask == AUDIO_CHANNEL_NONE)
6377 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
6378 if (config->format == AUDIO_FORMAT_DEFAULT)
6379 config->format = AUDIO_FORMAT_PCM_16_BIT;
6380
6381 channels = audio_channel_count_from_out_mask(out->channel_mask);
6382
Varun Balaraje49253e2017-07-06 19:48:56 +05306383 if (out->flags & AUDIO_OUTPUT_FLAG_INTERACTIVE) {
6384 out->usecase = get_interactive_usecase(adev);
6385 out->config = pcm_config_low_latency;
6386 } else if (out->flags & AUDIO_OUTPUT_FLAG_RAW) {
Ashish Jain83a6cc22016-06-28 14:34:17 +05306387 out->usecase = USECASE_AUDIO_PLAYBACK_ULL;
Haynes Mathew George5beddd42016-06-27 18:33:40 -07006388 out->realtime = may_use_noirq_mode(adev, USECASE_AUDIO_PLAYBACK_ULL,
6389 out->flags);
6390 out->config = out->realtime ? pcm_config_rt : pcm_config_low_latency;
Haynes Mathew George16081042017-05-31 17:16:49 -07006391 } else if (out->flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) {
6392 out->usecase = USECASE_AUDIO_PLAYBACK_MMAP;
6393 out->config = pcm_config_mmap_playback;
6394 out->stream.start = out_start;
6395 out->stream.stop = out_stop;
6396 out->stream.create_mmap_buffer = out_create_mmap_buffer;
6397 out->stream.get_mmap_position = out_get_mmap_position;
Ashish Jain83a6cc22016-06-28 14:34:17 +05306398 } else if (out->flags & AUDIO_OUTPUT_FLAG_FAST) {
6399 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
Aniket Kumar Lata932f4872017-11-06 18:29:44 -08006400 out->hal_output_suspend_supported =
6401 property_get_bool("vendor.audio.hal.output.suspend.supported", false);
6402 out->dynamic_pm_qos_config_supported =
6403 property_get_bool("vendor.audio.hal.dynamic.qos.config.supported", false);
6404 if (!out->dynamic_pm_qos_config_supported) {
Alexy Joseph98988832017-01-13 14:56:59 -08006405 ALOGI("%s: dynamic qos voting not enabled for platform", __func__);
6406 } else {
6407 ALOGI("%s: dynamic qos voting enabled for platform", __func__);
6408 //the mixer path will be a string similar to "low-latency-playback resume"
6409 strlcpy(out->pm_qos_mixer_path, use_case_table[out->usecase], MAX_MIXER_PATH_LEN);
6410 strlcat(out->pm_qos_mixer_path,
6411 " resume", MAX_MIXER_PATH_LEN);
6412 ALOGI("%s: created %s pm_qos_mixer_path" , __func__,
6413 out->pm_qos_mixer_path);
6414 }
Ashish Jain83a6cc22016-06-28 14:34:17 +05306415 out->config = pcm_config_low_latency;
6416 } else if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
6417 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
6418 out->config = pcm_config_deep_buffer;
Ashish Jain058165c2016-09-28 23:18:48 +05306419 out->config.period_size = get_output_period_size(config->sample_rate, out->format,
6420 channels, DEEP_BUFFER_OUTPUT_PERIOD_DURATION);
6421 if (out->config.period_size <= 0) {
6422 ALOGE("Invalid configuration period size is not valid");
6423 ret = -EINVAL;
6424 goto error_open;
6425 }
Ashish Jain83a6cc22016-06-28 14:34:17 +05306426 } else {
6427 /* primary path is the default path selected if no other outputs are available/suitable */
6428 out->usecase = USECASE_AUDIO_PLAYBACK_PRIMARY;
6429 out->config = PCM_CONFIG_AUDIO_PLAYBACK_PRIMARY;
6430 }
6431 out->hal_ip_format = format = out->format;
6432 out->config.format = hal_format_to_pcm(out->hal_ip_format);
6433 out->hal_op_format = pcm_format_to_hal(out->config.format);
6434 out->bit_width = format_to_bitwidth_table[out->hal_op_format] << 3;
6435 out->config.rate = config->sample_rate;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07006436 out->sample_rate = out->config.rate;
Ashish Jain058165c2016-09-28 23:18:48 +05306437 out->config.channels = channels;
Ashish Jain83a6cc22016-06-28 14:34:17 +05306438 if (out->hal_ip_format != out->hal_op_format) {
6439 uint32_t buffer_size = out->config.period_size *
6440 format_to_bitwidth_table[out->hal_op_format] *
6441 out->config.channels;
6442 out->convert_buffer = calloc(1, buffer_size);
6443 if (out->convert_buffer == NULL){
6444 ALOGE("Allocation failed for convert buffer for size %d",
6445 out->compr_config.fragment_size);
6446 ret = -ENOMEM;
6447 goto error_open;
6448 }
6449 ALOGD("Convert buffer allocated of size %d", buffer_size);
6450 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006451 }
6452
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08006453 ALOGV("%s devices:%d, format:%x, out->sample_rate:%d,out->bit_width:%d out->format:%d out->flags:%x, flags: %x usecase %d",
6454 __func__, devices, format, out->sample_rate, out->bit_width, out->format, out->flags, flags, out->usecase);
Ashish Jain83a6cc22016-06-28 14:34:17 +05306455
Pradnya Chaphekar80a8cfb2014-10-20 16:17:01 -07006456 /* TODO remove this hardcoding and check why width is zero*/
6457 if (out->bit_width == 0)
6458 out->bit_width = 16;
Dhananjay Kumard6d32152016-10-13 16:11:03 +05306459 audio_extn_utils_update_stream_output_app_type_cfg(adev->platform,
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07006460 &adev->streams_output_cfg_list,
Aalique Grahame65780b52017-09-27 14:59:56 -07006461 devices, out->flags, out->hal_op_format, out->sample_rate,
Dhananjay Kumar4d91c1a2016-12-01 23:27:29 +05306462 out->bit_width, out->channel_mask, out->profile,
Manish Dewangan837dc462015-05-27 10:17:41 +05306463 &out->app_type_cfg);
Haynes Mathew Georgebf143712013-12-03 13:02:53 -08006464 if ((out->usecase == USECASE_AUDIO_PLAYBACK_PRIMARY) ||
6465 (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
6466 /* Ensure the default output is not selected twice */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08006467 if(adev->primary_output == NULL)
6468 adev->primary_output = out;
6469 else {
6470 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07006471 ret = -EEXIST;
6472 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08006473 }
6474 }
6475
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006476 /* Check if this usecase is already existing */
6477 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella7ce05352014-04-17 20:00:41 -07006478 if ((get_usecase_from_list(adev, out->usecase) != NULL) &&
6479 (out->usecase != USECASE_COMPRESS_VOIP_CALL)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006480 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006481 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07006482 ret = -EEXIST;
6483 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006484 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08006485
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006486 pthread_mutex_unlock(&adev->lock);
6487
6488 out->stream.common.get_sample_rate = out_get_sample_rate;
6489 out->stream.common.set_sample_rate = out_set_sample_rate;
6490 out->stream.common.get_buffer_size = out_get_buffer_size;
6491 out->stream.common.get_channels = out_get_channels;
6492 out->stream.common.get_format = out_get_format;
6493 out->stream.common.set_format = out_set_format;
6494 out->stream.common.standby = out_standby;
6495 out->stream.common.dump = out_dump;
6496 out->stream.common.set_parameters = out_set_parameters;
6497 out->stream.common.get_parameters = out_get_parameters;
6498 out->stream.common.add_audio_effect = out_add_audio_effect;
6499 out->stream.common.remove_audio_effect = out_remove_audio_effect;
6500 out->stream.get_latency = out_get_latency;
6501 out->stream.set_volume = out_set_volume;
6502 out->stream.write = out_write;
6503 out->stream.get_render_position = out_get_render_position;
6504 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07006505 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006506
Haynes Mathew George16081042017-05-31 17:16:49 -07006507 if (out->realtime)
6508 out->af_period_multiplier = af_period_multiplier;
6509 else
6510 out->af_period_multiplier = 1;
6511
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006512 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07006513 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07006514 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006515
6516 config->format = out->stream.common.get_format(&out->stream.common);
6517 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
6518 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
Naresh Tanniru04f71882018-06-26 17:46:22 +05306519 register_format(out->format, out->supported_formats);
6520 register_channel_mask(out->channel_mask, out->supported_channel_masks);
6521 register_sample_rate(out->sample_rate, out->supported_sample_rates);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006522
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306523 /*
6524 By locking output stream before registering, we allow the callback
6525 to update stream's state only after stream's initial state is set to
6526 adev state.
6527 */
6528 lock_output_stream(out);
6529 audio_extn_snd_mon_register_listener(out, out_snd_mon_cb);
6530 pthread_mutex_lock(&adev->lock);
6531 out->card_status = adev->card_status;
6532 pthread_mutex_unlock(&adev->lock);
6533 pthread_mutex_unlock(&out->lock);
6534
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006535 *stream_out = &out->stream;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05306536 ALOGD("%s: Stream (%p) picks up usecase (%s)", __func__, &out->stream,
vivek mehta0ea887a2015-08-26 14:01:20 -07006537 use_case_table[out->usecase]);
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08006538
6539 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
6540 audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate,
6541 popcount(out->channel_mask), out->playback_started);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08006542 /* setup a channel for client <--> adsp communication for stream events */
Manish Dewangan21a850a2017-08-14 12:03:55 +05306543 is_direct_passthough = audio_extn_passthru_is_direct_passthrough(out);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08006544 if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) ||
Naresh Tanniru85819452017-05-04 18:55:45 -07006545 (out->flags & AUDIO_OUTPUT_FLAG_DIRECT_PCM) ||
Manish Dewangan21a850a2017-08-14 12:03:55 +05306546 (audio_extn_ip_hdlr_intf_supported(config->format, is_direct_passthough, false))) {
Ben Rombergerd771a7c2017-02-22 18:05:17 -08006547 hdlr_stream_cfg.pcm_device_id = platform_get_pcm_device_id(
6548 out->usecase, PCM_PLAYBACK);
6549 hdlr_stream_cfg.flags = out->flags;
6550 hdlr_stream_cfg.type = PCM_PLAYBACK;
6551 ret = audio_extn_adsp_hdlr_stream_open(&out->adsp_hdlr_stream_handle,
6552 &hdlr_stream_cfg);
6553 if (ret) {
6554 ALOGE("%s: adsp_hdlr_stream_open failed %d",__func__, ret);
6555 out->adsp_hdlr_stream_handle = NULL;
6556 }
6557 }
Manish Dewangan21a850a2017-08-14 12:03:55 +05306558 if (audio_extn_ip_hdlr_intf_supported(config->format, is_direct_passthough, false)) {
Vidyakumar Athota2062f912017-06-27 14:46:15 -07006559 ret = audio_extn_ip_hdlr_intf_init(&out->ip_hdlr_handle, NULL, NULL, adev, out->usecase);
Naresh Tanniru85819452017-05-04 18:55:45 -07006560 if (ret < 0) {
6561 ALOGE("%s: audio_extn_ip_hdlr_intf_init failed %d",__func__, ret);
6562 out->ip_hdlr_handle = NULL;
6563 }
6564 }
Eric Laurent994a6932013-07-17 11:51:42 -07006565 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006566 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07006567
6568error_open:
Ashish Jain83a6cc22016-06-28 14:34:17 +05306569 if (out->convert_buffer)
6570 free(out->convert_buffer);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07006571 free(out);
6572 *stream_out = NULL;
6573 ALOGD("%s: exit: ret %d", __func__, ret);
6574 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006575}
6576
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05306577void adev_close_output_stream(struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006578 struct audio_stream_out *stream)
6579{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006580 struct stream_out *out = (struct stream_out *)stream;
6581 struct audio_device *adev = out->dev;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08006582 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006583
Haynes Mathew George484e8d22017-07-31 18:55:17 -07006584 ALOGD("%s: enter:stream_handle(%s)",__func__, use_case_table[out->usecase]);
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05306585
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05306586 // must deregister from sndmonitor first to prevent races
6587 // between the callback and close_stream
6588 audio_extn_snd_mon_unregister_listener(out);
6589
Ben Rombergerd771a7c2017-02-22 18:05:17 -08006590 /* close adsp hdrl session before standby */
6591 if (out->adsp_hdlr_stream_handle) {
6592 ret = audio_extn_adsp_hdlr_stream_close(out->adsp_hdlr_stream_handle);
6593 if (ret)
6594 ALOGE("%s: adsp_hdlr_stream_close failed %d",__func__, ret);
6595 out->adsp_hdlr_stream_handle = NULL;
6596 }
6597
Manish Dewangan21a850a2017-08-14 12:03:55 +05306598 if (out->ip_hdlr_handle) {
Naresh Tanniru85819452017-05-04 18:55:45 -07006599 audio_extn_ip_hdlr_intf_deinit(out->ip_hdlr_handle);
6600 out->ip_hdlr_handle = NULL;
6601 }
6602
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08006603 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05306604 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08006605 ret = voice_extn_compress_voip_close_output_stream(&stream->common);
Ashish Jain1b9b30c2017-05-18 20:57:40 +05306606 out->started = 0;
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05306607 pthread_mutex_unlock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08006608 if(ret != 0)
6609 ALOGE("%s: Compress voip output cannot be closed, error:%d",
6610 __func__, ret);
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07006611 } else
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08006612 out_standby(&stream->common);
6613
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07006614 if (is_offload_usecase(out->usecase)) {
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08006615 audio_extn_dts_remove_state_notifier_node(out->usecase);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006616 destroy_offload_callback_thread(out);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07006617 free_offload_usecase(adev, out->usecase);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006618 if (out->compr_config.codec != NULL)
6619 free(out->compr_config.codec);
6620 }
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07006621
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05306622 out->a2dp_compress_mute = false;
6623
Varun Balaraje49253e2017-07-06 19:48:56 +05306624 if (is_interactive_usecase(out->usecase))
6625 free_interactive_usecase(adev, out->usecase);
6626
Ashish Jain83a6cc22016-06-28 14:34:17 +05306627 if (out->convert_buffer != NULL) {
6628 free(out->convert_buffer);
6629 out->convert_buffer = NULL;
6630 }
6631
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07006632 if (adev->voice_tx_output == out)
6633 adev->voice_tx_output = NULL;
6634
Dhanalakshmi Siddani6c3d0992017-01-16 16:52:33 +05306635 if (adev->primary_output == out)
6636 adev->primary_output = NULL;
6637
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07006638 pthread_cond_destroy(&out->cond);
6639 pthread_mutex_destroy(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006640 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07006641 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006642}
6643
6644static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
6645{
6646 struct audio_device *adev = (struct audio_device *)dev;
6647 struct str_parms *parms;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006648 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07006649 int val;
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07006650 int ret;
6651 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006652
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08006653 ALOGD("%s: enter: %s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006654 parms = str_parms_create_str(kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006655
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05306656 if (!parms)
6657 goto error;
Naresh Tanniru4c630392014-05-12 01:05:52 +05306658
Ashish Jain1b9b30c2017-05-18 20:57:40 +05306659 ret = str_parms_get_str(parms, "BT_SCO", value, sizeof(value));
6660 if (ret >= 0) {
6661 /* When set to false, HAL should disable EC and NS */
6662 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
6663 adev->bt_sco_on = true;
6664 else
6665 adev->bt_sco_on = false;
6666 }
6667
Naresh Tanniru4c630392014-05-12 01:05:52 +05306668 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07006669 status = voice_set_parameters(adev, parms);
6670 if (status != 0)
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08006671 goto done;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006672
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07006673 status = platform_set_parameters(adev->platform, parms);
6674 if (status != 0)
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08006675 goto done;
6676
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07006677 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
6678 if (ret >= 0) {
Vicky Sehrawate240e5d2014-08-12 17:17:04 -07006679 /* When set to false, HAL should disable EC and NS */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006680 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
6681 adev->bluetooth_nrec = true;
6682 else
6683 adev->bluetooth_nrec = false;
6684 }
6685
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07006686 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
6687 if (ret >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006688 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
6689 adev->screen_off = false;
6690 else
6691 adev->screen_off = true;
6692 }
6693
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07006694 ret = str_parms_get_int(parms, "rotation", &val);
6695 if (ret >= 0) {
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07006696 bool reverse_speakers = false;
6697 switch(val) {
6698 // FIXME: note that the code below assumes that the speakers are in the correct placement
6699 // relative to the user when the device is rotated 90deg from its default rotation. This
6700 // assumption is device-specific, not platform-specific like this code.
6701 case 270:
6702 reverse_speakers = true;
6703 break;
6704 case 0:
6705 case 90:
6706 case 180:
6707 break;
6708 default:
6709 ALOGE("%s: unexpected rotation of %d", __func__, val);
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07006710 status = -EINVAL;
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07006711 }
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07006712 if (status == 0) {
Vignesh Kulothungan3b5fae52017-09-25 12:16:30 -07006713 // check and set swap
6714 // - check if orientation changed and speaker active
6715 // - set rotation and cache the rotation value
6716 platform_check_and_set_swap_lr_channels(adev, reverse_speakers);
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07006717 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07006718 }
6719
Mingming Yin514a8bc2014-07-29 15:22:21 -07006720 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_SCO_WB, value, sizeof(value));
6721 if (ret >= 0) {
6722 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
6723 adev->bt_wb_speech_enabled = true;
6724 else
6725 adev->bt_wb_speech_enabled = false;
6726 }
6727
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -07006728 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value, sizeof(value));
6729 if (ret >= 0) {
6730 val = atoi(value);
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05306731 audio_devices_t device = (audio_devices_t) val;
Zhou Song681350a2017-10-19 16:28:42 +08006732 if (audio_is_output_device(val) &&
6733 (val & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07006734 ALOGV("cache new ext disp type and edid");
6735 ret = platform_get_ext_disp_type(adev->platform);
6736 if (ret < 0) {
6737 ALOGE("%s: Failed to query disp type, ret:%d", __func__, ret);
Manisha Agarwal2f5ff882018-08-08 17:09:29 +05306738 } else {
6739 platform_cache_edid(adev->platform);
Shiv Maliyappanahallic0656402016-09-03 14:13:26 -07006740 }
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05306741 } else if (audio_is_usb_out_device(device) || audio_is_usb_in_device(device)) {
vivek mehta344576a2016-04-12 18:56:03 -07006742 /*
6743 * Do not allow AFE proxy port usage by WFD source when USB headset is connected.
6744 * Per AudioPolicyManager, USB device is higher priority than WFD.
6745 * For Voice call over USB headset, voice call audio is routed to AFE proxy ports.
6746 * If WFD use case occupies AFE proxy, it may result unintended behavior while
6747 * starting voice call on USB
6748 */
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08006749 ret = str_parms_get_str(parms, "card", value, sizeof(value));
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05306750 if (ret >= 0)
6751 audio_extn_usb_add_device(device, atoi(value));
6752
Zhou Song6f862822017-11-06 17:27:57 +08006753 if (!audio_extn_usb_is_tunnel_supported()) {
6754 ALOGV("detected USB connect .. disable proxy");
6755 adev->allow_afe_proxy_usage = false;
6756 }
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -07006757 }
6758 }
6759
6760 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value, sizeof(value));
6761 if (ret >= 0) {
6762 val = atoi(value);
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05306763 audio_devices_t device = (audio_devices_t) val;
Garmond Leunge3b6d482016-10-25 16:48:01 -07006764 /*
6765 * The HDMI / Displayport disconnect handling has been moved to
6766 * audio extension to ensure that its parameters are not
6767 * invalidated prior to updating sysfs of the disconnect event
6768 * Invalidate will be handled by audio_extn_ext_disp_set_parameters()
6769 */
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05306770 if (audio_is_usb_out_device(device) || audio_is_usb_in_device(device)) {
Kuirong Wanga9f7cee2016-03-07 11:21:52 -08006771 ret = str_parms_get_str(parms, "card", value, sizeof(value));
Satya Krishna Pindiprolice227962017-12-13 16:07:14 +05306772 if (ret >= 0)
6773 audio_extn_usb_remove_device(device, atoi(value));
6774
Zhou Song6f862822017-11-06 17:27:57 +08006775 if (!audio_extn_usb_is_tunnel_supported()) {
6776 ALOGV("detected USB disconnect .. enable proxy");
6777 adev->allow_afe_proxy_usage = true;
6778 }
Pradnya Chaphekar4403bd72014-09-09 09:50:01 -07006779 }
6780 }
6781
Naresh Tanniru9d027a62015-03-13 01:32:10 +05306782 ret = str_parms_get_str(parms,"reconfigA2dp", value, sizeof(value));
6783 if (ret >= 0) {
6784 struct audio_usecase *usecase;
6785 struct listnode *node;
6786 list_for_each(node, &adev->usecase_list) {
6787 usecase = node_to_item(node, struct audio_usecase, list);
6788 if ((usecase->type == PCM_PLAYBACK) &&
Naresh Tanniruf7e9e632016-11-04 14:54:20 -07006789 (usecase->devices & AUDIO_DEVICE_OUT_ALL_A2DP)){
Naresh Tanniru9d027a62015-03-13 01:32:10 +05306790 ALOGD("reconfigure a2dp... forcing device switch");
Weiyin Jiang425180d2017-06-05 16:40:23 +08006791
6792 pthread_mutex_unlock(&adev->lock);
Naresh Tannirucd2353e2016-08-19 00:37:25 +05306793 lock_output_stream(usecase->stream.out);
Weiyin Jiang425180d2017-06-05 16:40:23 +08006794 pthread_mutex_lock(&adev->lock);
Naresh Tannirucd2353e2016-08-19 00:37:25 +05306795 audio_extn_a2dp_set_handoff_mode(true);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05306796 //force device switch to re configure encoder
6797 select_devices(adev, usecase->id);
Naresh Tannirucd2353e2016-08-19 00:37:25 +05306798 audio_extn_a2dp_set_handoff_mode(false);
6799 pthread_mutex_unlock(&usecase->stream.out->lock);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05306800 break;
6801 }
6802 }
6803 }
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08006804
6805 //handle vr audio setparam
6806 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_VR_AUDIO_MODE,
6807 value, sizeof(value));
6808 if (ret >= 0) {
6809 ALOGI("Setting vr mode to be %s", value);
6810 if (!strncmp(value, "true", 4)) {
6811 adev->vr_audio_mode_enabled = true;
6812 ALOGI("Setting vr mode to true");
6813 } else if (!strncmp(value, "false", 5)) {
6814 adev->vr_audio_mode_enabled = false;
6815 ALOGI("Setting vr mode to false");
6816 } else {
6817 ALOGI("wrong vr mode set");
6818 }
6819 }
6820
Naresh Tannirucd2353e2016-08-19 00:37:25 +05306821 audio_extn_set_parameters(adev, parms);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08006822done:
6823 str_parms_destroy(parms);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08006824 pthread_mutex_unlock(&adev->lock);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05306825error:
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07006826 ALOGV("%s: exit with code(%d)", __func__, status);
6827 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006828}
6829
6830static char* adev_get_parameters(const struct audio_hw_device *dev,
6831 const char *keys)
6832{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07006833 struct audio_device *adev = (struct audio_device *)dev;
6834 struct str_parms *reply = str_parms_create();
6835 struct str_parms *query = str_parms_create_str(keys);
6836 char *str;
Naresh Tannirud7205b62014-06-20 02:54:48 +05306837 char value[256] = {0};
6838 int ret = 0;
6839
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07006840 if (!query || !reply) {
Alexy Josephaee4fdd2016-01-29 13:02:07 -08006841 if (reply) {
6842 str_parms_destroy(reply);
6843 }
6844 if (query) {
6845 str_parms_destroy(query);
6846 }
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07006847 ALOGE("adev_get_parameters: failed to create query or reply");
6848 return NULL;
6849 }
6850
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08006851 //handle vr audio getparam
6852
6853 ret = str_parms_get_str(query,
6854 AUDIO_PARAMETER_KEY_VR_AUDIO_MODE,
6855 value, sizeof(value));
6856
6857 if (ret >= 0) {
6858 bool vr_audio_enabled = false;
6859 pthread_mutex_lock(&adev->lock);
6860 vr_audio_enabled = adev->vr_audio_mode_enabled;
6861 pthread_mutex_unlock(&adev->lock);
6862
6863 ALOGI("getting vr mode to %d", vr_audio_enabled);
6864
6865 if (vr_audio_enabled) {
6866 str_parms_add_str(reply, AUDIO_PARAMETER_KEY_VR_AUDIO_MODE,
6867 "true");
6868 goto exit;
6869 } else {
6870 str_parms_add_str(reply, AUDIO_PARAMETER_KEY_VR_AUDIO_MODE,
6871 "false");
6872 goto exit;
6873 }
6874 }
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07006875
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08006876 pthread_mutex_lock(&adev->lock);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07006877 audio_extn_get_parameters(adev, query, reply);
Shiv Maliyappanahallif9308492013-12-12 12:18:09 -08006878 voice_get_parameters(adev, query, reply);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07006879 platform_get_parameters(adev->platform, query, reply);
Naresh Tanniru80659832014-06-04 18:17:56 +05306880 pthread_mutex_unlock(&adev->lock);
6881
Naresh Tannirud7205b62014-06-20 02:54:48 +05306882exit:
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07006883 str = str_parms_to_str(reply);
6884 str_parms_destroy(query);
6885 str_parms_destroy(reply);
6886
6887 ALOGV("%s: exit: returns - %s", __func__, str);
6888 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006889}
6890
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07006891static int adev_init_check(const struct audio_hw_device *dev __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006892{
6893 return 0;
6894}
6895
6896static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
6897{
Haynes Mathew George5191a852013-09-11 14:19:36 -07006898 int ret;
6899 struct audio_device *adev = (struct audio_device *)dev;
6900 pthread_mutex_lock(&adev->lock);
6901 /* cache volume */
Shruthi Krishnaace10852013-10-25 14:32:12 -07006902 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07006903 pthread_mutex_unlock(&adev->lock);
6904 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006905}
6906
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07006907static int adev_set_master_volume(struct audio_hw_device *dev __unused,
6908 float volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006909{
6910 return -ENOSYS;
6911}
6912
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07006913static int adev_get_master_volume(struct audio_hw_device *dev __unused,
6914 float *volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006915{
6916 return -ENOSYS;
6917}
6918
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07006919static int adev_set_master_mute(struct audio_hw_device *dev __unused,
6920 bool muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006921{
6922 return -ENOSYS;
6923}
6924
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07006925static int adev_get_master_mute(struct audio_hw_device *dev __unused,
6926 bool *muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006927{
6928 return -ENOSYS;
6929}
6930
6931static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
6932{
6933 struct audio_device *adev = (struct audio_device *)dev;
Garmond Leung5fd0b552018-04-17 11:56:12 -07006934 struct listnode *node;
6935 struct audio_usecase *usecase = NULL;
6936 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006937 pthread_mutex_lock(&adev->lock);
6938 if (adev->mode != mode) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07006939 ALOGD("%s: mode %d\n", __func__, mode);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006940 adev->mode = mode;
Shiv Maliyappanahallibb4cf0b2016-01-21 11:30:06 -08006941 if ((mode == AUDIO_MODE_NORMAL) && voice_is_in_call(adev)) {
Garmond Leung5fd0b552018-04-17 11:56:12 -07006942 list_for_each(node, &adev->usecase_list) {
6943 usecase = node_to_item(node, struct audio_usecase, list);
6944 if (usecase->type == VOICE_CALL)
6945 break;
6946 }
6947 if (usecase &&
6948 audio_is_usb_out_device(usecase->out_snd_device & AUDIO_DEVICE_OUT_ALL_USB)) {
6949 ret = audio_extn_usb_check_and_set_svc_int(usecase,
6950 true);
6951 if (ret != 0) {
6952 /* default service interval was successfully updated,
6953 reopen USB backend with new service interval */
6954 check_usecases_codec_backend(adev,
6955 usecase,
6956 usecase->out_snd_device);
6957 }
6958 }
6959
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07006960 voice_stop_call(adev);
Banajit Goswami20cdd212015-09-11 01:11:30 -07006961 platform_set_gsm_mode(adev->platform, false);
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07006962 adev->current_call_output = NULL;
6963 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006964 }
6965 pthread_mutex_unlock(&adev->lock);
6966 return 0;
6967}
6968
6969static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
6970{
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08006971 int ret;
6972
6973 pthread_mutex_lock(&adev->lock);
Vidyakumar Athota2850d532013-11-19 16:02:12 -08006974 ALOGD("%s state %d\n", __func__, state);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08006975 ret = voice_set_mic_mute((struct audio_device *)dev, state);
Derek Chencdd17c72014-11-24 12:39:14 -08006976 if (adev->ext_hw_plugin)
6977 ret = audio_extn_ext_hw_plugin_set_mic_mute(adev->ext_hw_plugin, state);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08006978 pthread_mutex_unlock(&adev->lock);
6979
6980 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006981}
6982
6983static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
6984{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07006985 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006986 return 0;
6987}
6988
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07006989static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006990 const struct audio_config *config)
6991{
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07006992 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006993
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07006994 return get_input_buffer_size(config->sample_rate, config->format, channel_count,
6995 false /* is_low_latency: since we don't know, be conservative */);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006996}
6997
Haynes Mathew George484e8d22017-07-31 18:55:17 -07006998static bool adev_input_allow_hifi_record(struct audio_device *adev,
6999 audio_devices_t devices,
7000 audio_input_flags_t flags,
7001 audio_source_t source) {
7002 const bool allowed = true;
7003
7004 if (!audio_is_usb_in_device(devices))
7005 return !allowed;
7006
7007 switch (flags) {
7008 case AUDIO_INPUT_FLAG_NONE:
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007009 break;
Haynes Mathew George59862182017-10-24 16:23:57 -07007010 case AUDIO_INPUT_FLAG_FAST: // disallow hifi record for FAST as
7011 // it affects RTD numbers over USB
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007012 default:
7013 return !allowed;
7014 }
7015
7016 switch (source) {
7017 case AUDIO_SOURCE_DEFAULT:
7018 case AUDIO_SOURCE_MIC:
7019 case AUDIO_SOURCE_UNPROCESSED:
7020 break;
7021 default:
7022 return !allowed;
7023 }
7024
7025 switch (adev->mode) {
7026 case 0:
7027 break;
7028 default:
7029 return !allowed;
7030 }
7031
7032 return allowed;
7033}
7034
Haynes Mathew George4ffef292017-11-21 15:08:02 -08007035static int adev_update_voice_comm_input_stream(struct stream_in *in,
7036 struct audio_config *config)
7037{
7038 bool valid_rate = (config->sample_rate == 8000 ||
7039 config->sample_rate == 16000 ||
7040 config->sample_rate == 32000 ||
7041 config->sample_rate == 48000);
7042 bool valid_ch = audio_channel_count_from_in_mask(in->channel_mask) == 1;
7043
7044#ifndef COMPRESS_VOIP_ENABLED
kunleizd96526c2018-04-09 11:12:32 +08007045 if (valid_rate && valid_ch &&
7046 in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) {
Haynes Mathew George4ffef292017-11-21 15:08:02 -08007047 in->usecase = USECASE_AUDIO_RECORD_VOIP;
7048 in->config = default_pcm_config_voip_copp;
7049 in->config.period_size = VOIP_IO_BUF_SIZE(in->sample_rate,
7050 DEFAULT_VOIP_BUF_DURATION_MS,
7051 DEFAULT_VOIP_BIT_DEPTH_BYTE)/2;
7052 } else {
kunleizbdd9ad02018-04-24 10:53:17 +08007053 ALOGW("%s No valid input in voip, use defaults"
7054 "sample rate %u, channel mask 0x%X",
7055 __func__, config->sample_rate, in->channel_mask);
Haynes Mathew George4ffef292017-11-21 15:08:02 -08007056 }
7057 in->config.rate = config->sample_rate;
7058 in->sample_rate = config->sample_rate;
7059#else
7060 //XXX needed for voice_extn_compress_voip_open_input_stream
7061 in->config.rate = config->sample_rate;
7062 if ((in->dev->mode == AUDIO_MODE_IN_COMMUNICATION ||
7063 voice_extn_compress_voip_is_active(in->dev)) &&
7064 (voice_extn_compress_voip_is_format_supported(in->format)) &&
7065 valid_rate && valid_ch) {
7066 voice_extn_compress_voip_open_input_stream(in);
7067 // update rate entries to match config from AF
7068 in->config.rate = config->sample_rate;
7069 in->sample_rate = config->sample_rate;
7070 } else {
7071 ALOGW("%s compress voip not active, use defaults", __func__);
7072 }
7073#endif
7074 return 0;
7075}
7076
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007077static int adev_open_input_stream(struct audio_hw_device *dev,
Bharath Ramachandramurthy76d20892015-04-27 15:47:55 -07007078 audio_io_handle_t handle,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007079 audio_devices_t devices,
7080 struct audio_config *config,
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07007081 struct audio_stream_in **stream_in,
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307082 audio_input_flags_t flags,
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07007083 const char *address __unused,
Vidyakumar Athota5c398212015-03-31 21:53:21 -07007084 audio_source_t source)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007085{
7086 struct audio_device *adev = (struct audio_device *)dev;
7087 struct stream_in *in;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08007088 int ret = 0, buffer_size, frame_size;
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07007089 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07007090 bool is_low_latency = false;
Divya Narayanan Poojary45f19192016-09-30 18:52:13 +05307091 bool channel_mask_updated = false;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007092 bool is_usb_dev = audio_is_usb_in_device(devices);
7093 bool may_use_hifi_record = adev_input_allow_hifi_record(adev,
7094 devices,
7095 flags,
7096 source);
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05307097
kunleizdff872d2018-08-20 14:40:33 +08007098 if (is_usb_dev && (!audio_extn_usb_connected(NULL))) {
kunleizd6a9e0c2018-07-30 15:38:52 +08007099 is_usb_dev = false;
7100 devices = AUDIO_DEVICE_IN_BUILTIN_MIC;
7101 ALOGW("%s: ignore set device to non existing USB card, use input device(%#x)",
7102 __func__, devices);
7103 }
7104
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007105 *stream_in = NULL;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007106
7107 if (!(is_usb_dev && may_use_hifi_record)) {
7108 if (config->sample_rate == 0)
7109 config->sample_rate = 48000;
7110 if (config->channel_mask == AUDIO_CHANNEL_NONE)
7111 config->channel_mask = AUDIO_CHANNEL_IN_MONO;
7112 if (config->format == AUDIO_FORMAT_DEFAULT)
7113 config->format = AUDIO_FORMAT_PCM_16_BIT;
7114
7115 channel_count = audio_channel_count_from_in_mask(config->channel_mask);
7116
7117 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
7118 return -EINVAL;
Chaithanya Krishna Bacharaju9955b162016-05-25 16:25:53 +05307119 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007120
7121 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07007122
7123 if (!in) {
7124 ALOGE("failed to allocate input stream");
7125 return -ENOMEM;
7126 }
7127
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05307128 ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x)\
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05307129 stream_handle(%p) io_handle(%d) source(%d) format %x",__func__, config->sample_rate,
7130 config->channel_mask, devices, &in->stream, handle, source, config->format);
Ravi Kumar Alamanda40703102014-04-24 10:34:41 -07007131 pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL);
Shiv Maliyappanahalli736d4ce2015-09-28 15:23:06 -07007132 pthread_mutex_init(&in->pre_lock, (const pthread_mutexattr_t *) NULL);
Ravi Kumar Alamanda40703102014-04-24 10:34:41 -07007133
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007134 in->stream.common.get_sample_rate = in_get_sample_rate;
7135 in->stream.common.set_sample_rate = in_set_sample_rate;
7136 in->stream.common.get_buffer_size = in_get_buffer_size;
7137 in->stream.common.get_channels = in_get_channels;
7138 in->stream.common.get_format = in_get_format;
7139 in->stream.common.set_format = in_set_format;
7140 in->stream.common.standby = in_standby;
7141 in->stream.common.dump = in_dump;
7142 in->stream.common.set_parameters = in_set_parameters;
7143 in->stream.common.get_parameters = in_get_parameters;
7144 in->stream.common.add_audio_effect = in_add_audio_effect;
7145 in->stream.common.remove_audio_effect = in_remove_audio_effect;
7146 in->stream.set_gain = in_set_gain;
7147 in->stream.read = in_read;
7148 in->stream.get_input_frames_lost = in_get_input_frames_lost;
Naresh Tannirudcb47c52018-06-25 16:23:32 +05307149 in->stream.get_active_microphones = in_get_active_microphones;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007150
7151 in->device = devices;
Vidyakumar Athota5c398212015-03-31 21:53:21 -07007152 in->source = source;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007153 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007154 in->standby = 1;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07007155 in->capture_handle = handle;
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07007156 in->flags = flags;
Haynes Mathew George46740472017-10-27 18:40:12 -07007157 in->bit_width = 16;
7158 in->af_period_multiplier = 1;
7159
7160 /* Update config params with the requested sample rate and channels */
7161 if ((in->device == AUDIO_DEVICE_IN_TELEPHONY_RX) &&
7162 (adev->mode != AUDIO_MODE_IN_CALL)) {
7163 ret = -EINVAL;
7164 goto err_open;
7165 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007166
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007167 if (is_usb_dev && may_use_hifi_record) {
7168 /* HiFi record selects an appropriate format, channel, rate combo
7169 depending on sink capabilities*/
7170 ret = read_usb_sup_params_and_compare(false /*is_playback*/,
7171 &config->format,
7172 &in->supported_formats[0],
7173 MAX_SUPPORTED_FORMATS,
7174 &config->channel_mask,
7175 &in->supported_channel_masks[0],
7176 MAX_SUPPORTED_CHANNEL_MASKS,
7177 &config->sample_rate,
7178 &in->supported_sample_rates[0],
7179 MAX_SUPPORTED_SAMPLE_RATES);
7180 if (ret != 0) {
7181 ret = -EINVAL;
7182 goto err_open;
7183 }
7184 channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Haynes Mathew George4ffef292017-11-21 15:08:02 -08007185 } else if (config->format == AUDIO_FORMAT_DEFAULT) {
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307186 config->format = AUDIO_FORMAT_PCM_16_BIT;
Surendar karkaaca3d082017-11-09 15:18:37 +05307187 } else if (property_get_bool("vendor.audio.capture.pcm.32bit.enable", false)
7188 && config->format == AUDIO_FORMAT_PCM_32_BIT) {
7189 in->config.format = PCM_FORMAT_S32_LE;
7190 in->bit_width = 32;
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307191 } else if ((config->format == AUDIO_FORMAT_PCM_FLOAT) ||
7192 (config->format == AUDIO_FORMAT_PCM_32_BIT) ||
7193 (config->format == AUDIO_FORMAT_PCM_24_BIT_PACKED) ||
7194 (config->format == AUDIO_FORMAT_PCM_8_24_BIT)) {
7195 bool ret_error = false;
7196 in->bit_width = 24;
7197 /* 24 bit is restricted to UNPROCESSED source only,also format supported
7198 from HAL is 24_packed and 8_24
7199 *> In case of UNPROCESSED source, for 24 bit, if format requested is other than
7200 24_packed return error indicating supported format is 24_packed
7201 *> In case of any other source requesting 24 bit or float return error
7202 indicating format supported is 16 bit only.
7203
7204 on error flinger will retry with supported format passed
7205 */
7206 if ((source != AUDIO_SOURCE_UNPROCESSED) &&
7207 (source != AUDIO_SOURCE_CAMCORDER)) {
7208 config->format = AUDIO_FORMAT_PCM_16_BIT;
7209 if (config->sample_rate > 48000)
7210 config->sample_rate = 48000;
7211 ret_error = true;
Haynes Mathew George46740472017-10-27 18:40:12 -07007212 } else if (!(config->format == AUDIO_FORMAT_PCM_24_BIT_PACKED ||
7213 config->format == AUDIO_FORMAT_PCM_8_24_BIT)) {
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307214 config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
7215 ret_error = true;
7216 }
7217
7218 if (ret_error) {
7219 ret = -EINVAL;
7220 goto err_open;
7221 }
7222 }
7223
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007224 in->channel_mask = config->channel_mask;
7225 in->format = config->format;
7226
7227 in->usecase = USECASE_AUDIO_RECORD;
7228 if (config->sample_rate == LOW_LATENCY_CAPTURE_SAMPLE_RATE &&
7229 (flags & AUDIO_INPUT_FLAG_FAST) != 0) {
7230 is_low_latency = true;
7231#if LOW_LATENCY_CAPTURE_USE_CASE
7232 in->usecase = USECASE_AUDIO_RECORD_LOW_LATENCY;
7233#endif
7234 in->realtime = may_use_noirq_mode(adev, in->usecase, in->flags);
7235 }
7236
7237 if ((config->sample_rate == LOW_LATENCY_CAPTURE_SAMPLE_RATE) &&
7238 ((in->flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0)) {
7239 in->realtime = 0;
7240 in->usecase = USECASE_AUDIO_RECORD_MMAP;
7241 in->config = pcm_config_mmap_capture;
Haynes Mathew George46740472017-10-27 18:40:12 -07007242 in->config.format = pcm_format_from_audio_format(config->format);
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007243 in->stream.start = in_start;
7244 in->stream.stop = in_stop;
7245 in->stream.create_mmap_buffer = in_create_mmap_buffer;
7246 in->stream.get_mmap_position = in_get_mmap_position;
Haynes Mathew Georgef29116e2018-01-31 12:48:20 -08007247 in->sample_rate = in->config.rate;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007248 ALOGV("%s: USECASE_AUDIO_RECORD_MMAP", __func__);
7249 } else if (in->realtime) {
7250 in->config = pcm_config_audio_capture_rt;
Haynes Mathew George46740472017-10-27 18:40:12 -07007251 in->config.format = pcm_format_from_audio_format(config->format);
Haynes Mathew George4ffef292017-11-21 15:08:02 -08007252 in->config.channels = channel_count;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007253 in->sample_rate = in->config.rate;
7254 in->af_period_multiplier = af_period_multiplier;
Haynes Mathew George46740472017-10-27 18:40:12 -07007255 } else if (is_usb_dev && may_use_hifi_record) {
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007256 in->usecase = USECASE_AUDIO_RECORD_HIFI;
7257 in->config = pcm_config_audio_capture;
7258 frame_size = audio_stream_in_frame_size(&in->stream);
7259 buffer_size = get_input_buffer_size(config->sample_rate,
7260 config->format,
7261 channel_count,
7262 false /*is_low_latency*/);
7263 in->config.period_size = buffer_size / frame_size;
7264 in->config.rate = config->sample_rate;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007265 in->config.format = pcm_format_from_audio_format(config->format);
Karthikeyan Mani07faa602018-08-20 11:01:32 -07007266 switch (config->format) {
7267 case AUDIO_FORMAT_PCM_32_BIT:
7268 in->bit_width = 32;
7269 break;
7270 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
7271 case AUDIO_FORMAT_PCM_8_24_BIT:
7272 in->bit_width = 24;
7273 break;
7274 default:
7275 in->bit_width = 16;
7276 }
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007277 in->config.channels = channel_count;
Haynes Mathew Georgef29116e2018-01-31 12:48:20 -08007278 in->sample_rate = in->config.rate;
Haynes Mathew George484e8d22017-07-31 18:55:17 -07007279 } else if ((in->device == AUDIO_DEVICE_IN_TELEPHONY_RX) ||
Dhanalakshmi Siddani0b1488e2016-09-06 12:58:42 +05307280 (in->device == AUDIO_DEVICE_IN_PROXY)) {
Ravi Kumar Alamanda060bc5a2014-09-05 13:51:35 -07007281 if (config->sample_rate == 0)
7282 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
7283 if (config->sample_rate != 48000 && config->sample_rate != 16000 &&
7284 config->sample_rate != 8000) {
7285 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
7286 ret = -EINVAL;
7287 goto err_open;
7288 }
7289 if (config->format == AUDIO_FORMAT_DEFAULT)
7290 config->format = AUDIO_FORMAT_PCM_16_BIT;
7291 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
7292 config->format = AUDIO_FORMAT_PCM_16_BIT;
7293 ret = -EINVAL;
7294 goto err_open;
7295 }
7296
7297 in->usecase = USECASE_AUDIO_RECORD_AFE_PROXY;
7298 in->config = pcm_config_afe_proxy_record;
7299 in->config.channels = channel_count;
7300 in->config.rate = config->sample_rate;
Manish Dewanganba9fcfa2016-03-24 16:20:06 +05307301 in->sample_rate = config->sample_rate;
Haynes Mathew George46740472017-10-27 18:40:12 -07007302 } else {
Revathi Uddarajud2634032017-12-07 14:42:34 +05307303 int ret_val;
7304 pthread_mutex_lock(&adev->lock);
7305 ret_val = audio_extn_check_and_set_multichannel_usecase(adev,
7306 in, config, &channel_mask_updated);
7307 pthread_mutex_unlock(&adev->lock);
7308
7309 if (!ret_val) {
7310 if (channel_mask_updated == true) {
7311 ALOGD("%s: return error to retry with updated channel mask (%#x)",
7312 __func__, config->channel_mask);
7313 ret = -EINVAL;
7314 goto err_open;
7315 }
7316 ALOGD("%s: created multi-channel session succesfully",__func__);
7317 } else if (audio_extn_compr_cap_enabled() &&
7318 audio_extn_compr_cap_format_supported(config->format) &&
7319 (in->dev->mode != AUDIO_MODE_IN_COMMUNICATION)) {
7320 audio_extn_compr_cap_init(in);
7321 } else if (audio_extn_cin_applicable_stream(in)) {
Sachin Mohan Gadag16273f72017-12-19 18:10:48 +05307322 in->sample_rate = config->sample_rate;
Revathi Uddarajud2634032017-12-07 14:42:34 +05307323 ret = audio_extn_cin_configure_input_stream(in);
7324 if (ret)
7325 goto err_open;
7326 } else {
7327 in->config = pcm_config_audio_capture;
7328 in->config.rate = config->sample_rate;
7329 in->config.format = pcm_format_from_audio_format(config->format);
7330 in->config.channels = channel_count;
7331 in->sample_rate = config->sample_rate;
7332 in->format = config->format;
7333 frame_size = audio_stream_in_frame_size(&in->stream);
7334 buffer_size = get_input_buffer_size(config->sample_rate,
Haynes Mathew George46740472017-10-27 18:40:12 -07007335 config->format,
7336 channel_count,
7337 is_low_latency);
Revathi Uddarajud2634032017-12-07 14:42:34 +05307338 in->config.period_size = buffer_size / frame_size;
Haynes Mathew George4ffef292017-11-21 15:08:02 -08007339
Revathi Uddarajud2634032017-12-07 14:42:34 +05307340 if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
7341 /* optionally use VOIP usecase depending on config(s) */
7342 ret = adev_update_voice_comm_input_stream(in, config);
7343 }
Haynes Mathew George4ffef292017-11-21 15:08:02 -08007344
Revathi Uddarajud2634032017-12-07 14:42:34 +05307345 if (ret) {
7346 ALOGE("%s AUDIO_SOURCE_VOICE_COMMUNICATION invalid args", __func__);
7347 goto err_open;
7348 }
Haynes Mathew George4ffef292017-11-21 15:08:02 -08007349 }
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07007350 }
Dhananjay Kumard6d32152016-10-13 16:11:03 +05307351 audio_extn_utils_update_stream_input_app_type_cfg(adev->platform,
7352 &adev->streams_input_cfg_list,
Haynes Mathew George4ffef292017-11-21 15:08:02 -08007353 devices, flags, in->format,
7354 in->sample_rate, in->bit_width,
7355 in->profile, &in->app_type_cfg);
Naresh Tanniru04f71882018-06-26 17:46:22 +05307356 register_format(in->format, in->supported_formats);
7357 register_channel_mask(in->channel_mask, in->supported_channel_masks);
7358 register_sample_rate(in->sample_rate, in->supported_sample_rates);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05307359
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07007360 /* This stream could be for sound trigger lab,
7361 get sound trigger pcm if present */
7362 audio_extn_sound_trigger_check_and_get_session(in);
7363
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05307364 lock_input_stream(in);
7365 audio_extn_snd_mon_register_listener(in, in_snd_mon_cb);
7366 pthread_mutex_lock(&adev->lock);
7367 in->card_status = adev->card_status;
7368 pthread_mutex_unlock(&adev->lock);
7369 pthread_mutex_unlock(&in->lock);
7370
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007371 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07007372 ALOGV("%s: exit", __func__);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08007373 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007374
7375err_open:
7376 free(in);
7377 *stream_in = NULL;
7378 return ret;
7379}
7380
7381static void adev_close_input_stream(struct audio_hw_device *dev,
7382 struct audio_stream_in *stream)
7383{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007384 int ret;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07007385 struct stream_in *in = (struct stream_in *)stream;
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07007386 struct audio_device *adev = (struct audio_device *)dev;
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05307387
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05307388 ALOGD("%s: enter:stream_handle(%p)",__func__, in);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08007389
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05307390 // must deregister from sndmonitor first to prevent races
7391 // between the callback and close_stream
7392 audio_extn_snd_mon_unregister_listener(stream);
7393
Venkata Narendra Kumar Gutta1bbbf542014-09-04 19:11:25 +05307394 /* Disable echo reference while closing input stream */
Apoorv Raghuvanshi924b3022015-07-06 15:07:14 -07007395 platform_set_echo_reference(adev, false, AUDIO_DEVICE_NONE);
Venkata Narendra Kumar Gutta1bbbf542014-09-04 19:11:25 +05307396
Pallavid7c7a272018-01-16 11:22:55 +05307397 if (in == NULL) {
7398 ALOGE("%s: audio_stream_in ptr is NULL", __func__);
7399 return;
7400 }
7401
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007402 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05307403 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007404 ret = voice_extn_compress_voip_close_input_stream(&stream->common);
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05307405 pthread_mutex_unlock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08007406 if (ret != 0)
7407 ALOGE("%s: Compress voip input cannot be closed, error:%d",
7408 __func__, ret);
7409 } else
7410 in_standby(&stream->common);
7411
Revathi Uddarajud2634032017-12-07 14:42:34 +05307412 pthread_mutex_lock(&adev->lock);
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -07007413 if (audio_extn_ssr_get_stream() == in) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07007414 audio_extn_ssr_deinit();
7415 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007416
Garmond Leunge2433c32017-09-28 21:51:22 -07007417 if (audio_extn_ffv_get_stream() == in) {
7418 audio_extn_ffv_stream_deinit();
7419 }
7420
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307421 if (audio_extn_compr_cap_enabled() &&
Mingming Yine62d7842013-10-25 16:26:03 -07007422 audio_extn_compr_cap_format_supported(in->config.format))
7423 audio_extn_compr_cap_deinit();
Dhanalakshmi Siddani74cf00b2016-12-02 13:55:57 +05307424
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307425 if (audio_extn_cin_attached_usecase(in->usecase))
7426 audio_extn_cin_close_input_stream(in);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07007427
Mingming Yinfd7607b2016-01-22 12:48:44 -08007428 if (in->is_st_session) {
7429 ALOGV("%s: sound trigger pcm stop lab", __func__);
7430 audio_extn_sound_trigger_stop_lab(in);
7431 }
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07007432 free(stream);
Revathi Uddarajud2634032017-12-07 14:42:34 +05307433 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007434 return;
7435}
7436
Siddartha Shaik31b530e2017-05-19 15:26:33 +05307437int adev_create_audio_patch(struct audio_hw_device *dev,
7438 unsigned int num_sources,
7439 const struct audio_port_config *sources,
7440 unsigned int num_sinks,
7441 const struct audio_port_config *sinks,
7442 audio_patch_handle_t *handle)
7443{
7444
7445
7446 return audio_extn_hw_loopback_create_audio_patch(dev,
7447 num_sources,
7448 sources,
7449 num_sinks,
7450 sinks,
7451 handle);
7452
7453}
7454
7455int adev_release_audio_patch(struct audio_hw_device *dev,
7456 audio_patch_handle_t handle)
7457{
7458 return audio_extn_hw_loopback_release_audio_patch(dev, handle);
7459}
7460
7461int adev_get_audio_port(struct audio_hw_device *dev, struct audio_port *config)
7462{
7463 return audio_extn_hw_loopback_get_audio_port(dev, config);
7464}
7465
7466int adev_set_audio_port_config(struct audio_hw_device *dev,
7467 const struct audio_port_config *config)
7468{
7469 return audio_extn_hw_loopback_set_audio_port_config(dev, config);
7470}
7471
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07007472static int adev_dump(const audio_hw_device_t *device __unused,
7473 int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007474{
7475 return 0;
7476}
7477
7478static int adev_close(hw_device_t *device)
7479{
7480 struct audio_device *adev = (struct audio_device *)device;
Kiran Kandi910e1862013-10-29 13:29:42 -07007481
7482 if (!adev)
7483 return 0;
7484
7485 pthread_mutex_lock(&adev_init_lock);
7486
7487 if ((--audio_device_ref_count) == 0) {
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05307488 audio_extn_snd_mon_unregister_listener(adev);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07007489 audio_extn_sound_trigger_deinit(adev);
Kiran Kandide144c82013-11-20 15:58:32 -08007490 audio_extn_listen_deinit(adev);
Dhananjay Kumard6d32152016-10-13 16:11:03 +05307491 audio_extn_utils_release_streams_cfg_lists(
7492 &adev->streams_output_cfg_list,
7493 &adev->streams_input_cfg_list);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05307494 if (audio_extn_qaf_is_enabled())
7495 audio_extn_qaf_deinit();
Kiran Kandi910e1862013-10-29 13:29:42 -07007496 audio_route_free(adev->audio_route);
Weiyin Jiangaa80acd2016-09-21 16:42:11 +08007497 audio_extn_gef_deinit();
Kiran Kandi910e1862013-10-29 13:29:42 -07007498 free(adev->snd_dev_ref_cnt);
7499 platform_deinit(adev->platform);
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07007500 if (adev->adm_deinit)
7501 adev->adm_deinit(adev->adm_data);
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307502 qahwi_deinit(device);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08007503 audio_extn_adsp_hdlr_deinit();
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05307504 audio_extn_snd_mon_deinit();
Siddartha Shaik44dd7702017-06-14 12:13:25 +05307505 audio_extn_hw_loopback_deinit(adev);
Garmond Leunge2433c32017-09-28 21:51:22 -07007506 audio_extn_ffv_deinit();
Satish Babu Patakokilac3c5d432017-07-04 22:48:59 +05307507 if (adev->device_cfg_params) {
7508 free(adev->device_cfg_params);
7509 adev->device_cfg_params = NULL;
7510 }
Derek Chencdd17c72014-11-24 12:39:14 -08007511 if(adev->ext_hw_plugin)
7512 audio_extn_ext_hw_plugin_deinit(adev->ext_hw_plugin);
Kiran Kandi910e1862013-10-29 13:29:42 -07007513 free(device);
7514 adev = NULL;
7515 }
7516 pthread_mutex_unlock(&adev_init_lock);
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07007517
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007518 return 0;
7519}
7520
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07007521/* This returns 1 if the input parameter looks at all plausible as a low latency period size,
7522 * or 0 otherwise. A return value of 1 doesn't mean the value is guaranteed to work,
7523 * just that it _might_ work.
7524 */
7525static int period_size_is_plausible_for_low_latency(int period_size)
7526{
7527 switch (period_size) {
7528 case 160:
Ravi Kumar Alamanda474de5a2015-06-25 20:08:01 -07007529 case 192:
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07007530 case 240:
7531 case 320:
7532 case 480:
7533 return 1;
7534 default:
7535 return 0;
7536 }
7537}
7538
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05307539static void adev_snd_mon_cb(void *cookie, struct str_parms *parms)
7540{
7541 bool is_snd_card_status = false;
7542 bool is_ext_device_status = false;
7543 char value[32];
7544 int card = -1;
7545 card_status_t status;
7546
7547 if (cookie != adev || !parms)
7548 return;
7549
7550 if (!parse_snd_card_status(parms, &card, &status)) {
7551 is_snd_card_status = true;
7552 } else if (0 < str_parms_get_str(parms, "ext_audio_device", value, sizeof(value))) {
7553 is_ext_device_status = true;
7554 } else {
7555 // not a valid event
7556 return;
7557 }
7558
7559 pthread_mutex_lock(&adev->lock);
7560 if (card == adev->snd_card || is_ext_device_status) {
7561 if (is_snd_card_status && adev->card_status != status) {
7562 adev->card_status = status;
7563 platform_snd_card_update(adev->platform, status);
7564 audio_extn_fm_set_parameters(adev, parms);
7565 } else if (is_ext_device_status) {
7566 platform_set_parameters(adev->platform, parms);
7567 }
7568 }
7569 pthread_mutex_unlock(&adev->lock);
7570 return;
7571}
7572
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05307573/* out and adev lock held */
7574static int check_a2dp_restore_l(struct audio_device *adev, struct stream_out *out, bool restore)
7575{
7576 struct audio_usecase *uc_info;
7577 float left_p;
7578 float right_p;
7579 audio_devices_t devices;
7580
7581 uc_info = get_usecase_from_list(adev, out->usecase);
7582 if (uc_info == NULL) {
7583 ALOGE("%s: Could not find the usecase (%d) in the list",
7584 __func__, out->usecase);
7585 return -EINVAL;
7586 }
7587
7588 ALOGD("%s: enter: usecase(%d: %s)", __func__,
7589 out->usecase, use_case_table[out->usecase]);
7590
7591 if (restore) {
7592 // restore A2DP device for active usecases and unmute if required
7593 if ((out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) &&
7594 (uc_info->out_snd_device != SND_DEVICE_OUT_BT_A2DP)) {
7595 ALOGD("%s: restoring A2dp and unmuting stream", __func__);
7596 select_devices(adev, uc_info->id);
7597 pthread_mutex_lock(&out->compr_mute_lock);
7598 if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
7599 (out->a2dp_compress_mute)) {
7600 out->a2dp_compress_mute = false;
7601 out_set_compr_volume(&out->stream, out->volume_l, out->volume_r);
7602 }
7603 pthread_mutex_unlock(&out->compr_mute_lock);
7604 }
7605 } else {
7606 // mute compress stream if suspended
7607 pthread_mutex_lock(&out->compr_mute_lock);
7608 if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
7609 (!out->a2dp_compress_mute)) {
7610 if (!out->standby) {
7611 ALOGD("%s: selecting speaker and muting stream", __func__);
7612 devices = out->devices;
7613 out->devices = AUDIO_DEVICE_OUT_SPEAKER;
7614 left_p = out->volume_l;
7615 right_p = out->volume_r;
7616 if (out->offload_state == OFFLOAD_STATE_PLAYING)
7617 compress_pause(out->compr);
7618 out_set_compr_volume(&out->stream, (float)0, (float)0);
7619 out->a2dp_compress_mute = true;
7620 select_devices(adev, out->usecase);
7621 if (out->offload_state == OFFLOAD_STATE_PLAYING)
7622 compress_resume(out->compr);
7623 out->devices = devices;
7624 out->volume_l = left_p;
7625 out->volume_r = right_p;
7626 }
7627 }
7628 pthread_mutex_unlock(&out->compr_mute_lock);
7629 }
7630 ALOGV("%s: exit", __func__);
7631 return 0;
7632}
7633
7634int check_a2dp_restore(struct audio_device *adev, struct stream_out *out, bool restore)
7635{
7636 int ret = 0;
7637
7638 lock_output_stream(out);
7639 pthread_mutex_lock(&adev->lock);
7640
7641 ret = check_a2dp_restore_l(adev, out, restore);
7642
7643 pthread_mutex_unlock(&adev->lock);
7644 pthread_mutex_unlock(&out->lock);
7645 return ret;
7646}
7647
Haynes Mathew George01156f92018-04-13 15:29:54 -07007648void adev_on_battery_status_changed(bool charging)
7649{
7650 pthread_mutex_lock(&adev->lock);
7651 ALOGI("%s: battery status changed to %scharging", __func__, charging ? "" : "not ");
7652 adev->is_charging = charging;
Zhou Songc1088ea2018-06-12 00:17:29 +08007653 audio_extn_sound_trigger_update_battery_status(charging);
Haynes Mathew George01156f92018-04-13 15:29:54 -07007654 pthread_mutex_unlock(&adev->lock);
7655}
7656
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007657static int adev_open(const hw_module_t *module, const char *name,
7658 hw_device_t **device)
7659{
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05307660 int ret;
7661
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08007662 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007663 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
7664
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07007665 pthread_mutex_lock(&adev_init_lock);
Kiran Kandi910e1862013-10-29 13:29:42 -07007666 if (audio_device_ref_count != 0){
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07007667 *device = &adev->device.common;
Kiran Kandi910e1862013-10-29 13:29:42 -07007668 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07007669 ALOGD("%s: returning existing instance of adev", __func__);
7670 ALOGD("%s: exit", __func__);
7671 pthread_mutex_unlock(&adev_init_lock);
7672 return 0;
7673 }
7674
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007675 adev = calloc(1, sizeof(struct audio_device));
7676
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07007677 if (!adev) {
7678 pthread_mutex_unlock(&adev_init_lock);
7679 return -ENOMEM;
7680 }
7681
Ravi Kumar Alamanda40703102014-04-24 10:34:41 -07007682 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
7683
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +05307684#ifdef DYNAMIC_LOG_ENABLED
7685 register_for_dynamic_logging("hal");
7686#endif
7687
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007688 adev->device.common.tag = HARDWARE_DEVICE_TAG;
7689 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
7690 adev->device.common.module = (struct hw_module_t *)module;
7691 adev->device.common.close = adev_close;
7692
7693 adev->device.init_check = adev_init_check;
7694 adev->device.set_voice_volume = adev_set_voice_volume;
7695 adev->device.set_master_volume = adev_set_master_volume;
7696 adev->device.get_master_volume = adev_get_master_volume;
7697 adev->device.set_master_mute = adev_set_master_mute;
7698 adev->device.get_master_mute = adev_get_master_mute;
7699 adev->device.set_mode = adev_set_mode;
7700 adev->device.set_mic_mute = adev_set_mic_mute;
7701 adev->device.get_mic_mute = adev_get_mic_mute;
7702 adev->device.set_parameters = adev_set_parameters;
7703 adev->device.get_parameters = adev_get_parameters;
7704 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
7705 adev->device.open_output_stream = adev_open_output_stream;
7706 adev->device.close_output_stream = adev_close_output_stream;
7707 adev->device.open_input_stream = adev_open_input_stream;
7708 adev->device.close_input_stream = adev_close_input_stream;
Siddartha Shaik31b530e2017-05-19 15:26:33 +05307709 adev->device.create_audio_patch = adev_create_audio_patch;
7710 adev->device.release_audio_patch = adev_release_audio_patch;
7711 adev->device.get_audio_port = adev_get_audio_port;
7712 adev->device.set_audio_port_config = adev_set_audio_port_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007713 adev->device.dump = adev_dump;
Naresh Tannirudcb47c52018-06-25 16:23:32 +05307714 adev->device.get_microphones = adev_get_microphones;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007715
7716 /* Set the default route before the PCM stream is opened */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007717 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08007718 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08007719 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007720 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007721 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08007722 adev->acdb_settings = TTY_MODE_OFF;
vivek mehta344576a2016-04-12 18:56:03 -07007723 adev->allow_afe_proxy_usage = true;
Ashish Jain1b9b30c2017-05-18 20:57:40 +05307724 adev->bt_sco_on = false;
Eric Laurent07eeafd2013-10-06 12:52:49 -07007725 /* adev->cur_hdmi_channels = 0; by calloc() */
Eric Laurentb23d5282013-05-14 15:27:20 -07007726 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07007727 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08007728 list_init(&adev->usecase_list);
Krishnankutty Kolathappilly0b2de1c2014-02-14 14:45:49 -08007729 adev->cur_wfd_channels = 2;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07007730 adev->offload_usecases_state = 0;
Ashish Jain81eb2a82015-05-13 10:52:34 +05307731 adev->is_channel_status_set = false;
Sudheer Papothifa9d2282015-09-17 01:53:25 +05307732 adev->perf_lock_opts[0] = 0x101;
7733 adev->perf_lock_opts[1] = 0x20E;
7734 adev->perf_lock_opts_size = 2;
Xiaojun Sang785b5da2017-08-03 15:52:29 +08007735 adev->dsp_bit_width_enforce_mode = 0;
Naresh Tanniru4c630392014-05-12 01:05:52 +05307736
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007737 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07007738 adev->platform = platform_init(adev);
7739 if (!adev->platform) {
Ben Romberger2684ad72018-06-04 12:41:39 -07007740 pthread_mutex_destroy(&adev->lock);
Eric Laurentb23d5282013-05-14 15:27:20 -07007741 free(adev->snd_dev_ref_cnt);
7742 free(adev);
Ben Romberger2684ad72018-06-04 12:41:39 -07007743 adev = NULL;
Eric Laurentb23d5282013-05-14 15:27:20 -07007744 ALOGE("%s: Failed to init platform data, aborting.", __func__);
7745 *device = NULL;
Apoorv Raghuvanshi6e57d7e2013-12-16 16:02:45 -08007746 pthread_mutex_unlock(&adev_init_lock);
Eric Laurentb23d5282013-05-14 15:27:20 -07007747 return -EINVAL;
7748 }
Eric Laurentc4aef752013-09-12 17:45:53 -07007749
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05307750 if (audio_extn_qaf_is_enabled()) {
7751 ret = audio_extn_qaf_init(adev);
7752 if (ret < 0) {
Ben Romberger2684ad72018-06-04 12:41:39 -07007753 pthread_mutex_destroy(&adev->lock);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05307754 free(adev);
Ben Romberger2684ad72018-06-04 12:41:39 -07007755 adev = NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05307756 ALOGE("%s: Failed to init platform data, aborting.", __func__);
7757 *device = NULL;
7758 pthread_mutex_unlock(&adev_init_lock);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05307759 return ret;
7760 }
7761
7762 adev->device.open_output_stream = audio_extn_qaf_open_output_stream;
7763 adev->device.close_output_stream = audio_extn_qaf_close_output_stream;
7764 }
7765
Derek Chencdd17c72014-11-24 12:39:14 -08007766 adev->ext_hw_plugin = audio_extn_ext_hw_plugin_init(adev);
7767
Eric Laurentc4aef752013-09-12 17:45:53 -07007768 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
7769 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
7770 if (adev->visualizer_lib == NULL) {
7771 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
7772 } else {
7773 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
7774 adev->visualizer_start_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08007775 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07007776 "visualizer_hal_start_output");
7777 adev->visualizer_stop_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08007778 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07007779 "visualizer_hal_stop_output");
7780 }
7781 }
Dhanalakshmi Siddani21be3ac2016-12-29 14:31:08 +05307782 audio_extn_init(adev);
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08007783 audio_extn_listen_init(adev, adev->snd_card);
Weiyin Jiangaa80acd2016-09-21 16:42:11 +08007784 audio_extn_gef_init(adev);
Siddartha Shaik44dd7702017-06-14 12:13:25 +05307785 audio_extn_hw_loopback_init(adev);
Garmond Leunge2433c32017-09-28 21:51:22 -07007786 audio_extn_ffv_init(adev);
Eric Laurentc4aef752013-09-12 17:45:53 -07007787
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08007788 if (access(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, R_OK) == 0) {
7789 adev->offload_effects_lib = dlopen(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, RTLD_NOW);
7790 if (adev->offload_effects_lib == NULL) {
7791 ALOGE("%s: DLOPEN failed for %s", __func__,
7792 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
7793 } else {
7794 ALOGV("%s: DLOPEN successful for %s", __func__,
7795 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
7796 adev->offload_effects_start_output =
Ashish Jain5106d362016-05-11 19:23:33 +05307797 (int (*)(audio_io_handle_t, int, struct mixer *))dlsym(adev->offload_effects_lib,
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08007798 "offload_effects_bundle_hal_start_output");
7799 adev->offload_effects_stop_output =
7800 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
7801 "offload_effects_bundle_hal_stop_output");
Jitendra Naruka1b6513f2014-11-22 19:34:13 -08007802 adev->offload_effects_set_hpx_state =
7803 (int (*)(bool))dlsym(adev->offload_effects_lib,
7804 "offload_effects_bundle_set_hpx_state");
Dhananjay Kumard68883d2015-09-04 13:39:26 +05307805 adev->offload_effects_get_parameters =
7806 (void (*)(struct str_parms *, struct str_parms *))
7807 dlsym(adev->offload_effects_lib,
7808 "offload_effects_bundle_get_parameters");
7809 adev->offload_effects_set_parameters =
7810 (void (*)(struct str_parms *))dlsym(adev->offload_effects_lib,
7811 "offload_effects_bundle_set_parameters");
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08007812 }
7813 }
7814
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07007815 if (access(ADM_LIBRARY_PATH, R_OK) == 0) {
7816 adev->adm_lib = dlopen(ADM_LIBRARY_PATH, RTLD_NOW);
7817 if (adev->adm_lib == NULL) {
7818 ALOGE("%s: DLOPEN failed for %s", __func__, ADM_LIBRARY_PATH);
7819 } else {
7820 ALOGV("%s: DLOPEN successful for %s", __func__, ADM_LIBRARY_PATH);
7821 adev->adm_init = (adm_init_t)
7822 dlsym(adev->adm_lib, "adm_init");
7823 adev->adm_deinit = (adm_deinit_t)
7824 dlsym(adev->adm_lib, "adm_deinit");
7825 adev->adm_register_input_stream = (adm_register_input_stream_t)
7826 dlsym(adev->adm_lib, "adm_register_input_stream");
7827 adev->adm_register_output_stream = (adm_register_output_stream_t)
7828 dlsym(adev->adm_lib, "adm_register_output_stream");
7829 adev->adm_deregister_stream = (adm_deregister_stream_t)
7830 dlsym(adev->adm_lib, "adm_deregister_stream");
7831 adev->adm_request_focus = (adm_request_focus_t)
7832 dlsym(adev->adm_lib, "adm_request_focus");
7833 adev->adm_abandon_focus = (adm_abandon_focus_t)
7834 dlsym(adev->adm_lib, "adm_abandon_focus");
Haynes Mathew George5beddd42016-06-27 18:33:40 -07007835 adev->adm_set_config = (adm_set_config_t)
7836 dlsym(adev->adm_lib, "adm_set_config");
7837 adev->adm_request_focus_v2 = (adm_request_focus_v2_t)
7838 dlsym(adev->adm_lib, "adm_request_focus_v2");
7839 adev->adm_is_noirq_avail = (adm_is_noirq_avail_t)
7840 dlsym(adev->adm_lib, "adm_is_noirq_avail");
7841 adev->adm_on_routing_change = (adm_on_routing_change_t)
7842 dlsym(adev->adm_lib, "adm_on_routing_change");
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07007843 }
7844 }
7845
Mingming Yin514a8bc2014-07-29 15:22:21 -07007846 adev->bt_wb_speech_enabled = false;
Alexy Joseph5e4ccbc2017-02-21 14:20:12 -08007847 //initialize this to false for now,
7848 //this will be set to true through set param
7849 adev->vr_audio_mode_enabled = false;
Mingming Yin514a8bc2014-07-29 15:22:21 -07007850
Pradnya Chaphekar8a9dcd82014-09-09 09:49:10 -07007851 audio_extn_ds2_enable(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007852 *device = &adev->device.common;
Xiaojun Sang785b5da2017-08-03 15:52:29 +08007853 adev->dsp_bit_width_enforce_mode =
7854 adev_init_dsp_bit_width_enforce_mode(adev->mixer);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007855
Dhananjay Kumard6d32152016-10-13 16:11:03 +05307856 audio_extn_utils_update_streams_cfg_lists(adev->platform, adev->mixer,
7857 &adev->streams_output_cfg_list,
7858 &adev->streams_input_cfg_list);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07007859
Kiran Kandi910e1862013-10-29 13:29:42 -07007860 audio_device_ref_count++;
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07007861
7862 char value[PROPERTY_VALUE_MAX];
7863 int trial;
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07007864 if (property_get("vendor.audio_hal.period_size", value, NULL) > 0) {
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07007865 trial = atoi(value);
7866 if (period_size_is_plausible_for_low_latency(trial)) {
7867 pcm_config_low_latency.period_size = trial;
7868 pcm_config_low_latency.start_threshold = trial / 4;
7869 pcm_config_low_latency.avail_min = trial / 4;
7870 configured_low_latency_capture_period_size = trial;
7871 }
7872 }
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07007873 if (property_get("vendor.audio_hal.in_period_size", value, NULL) > 0) {
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07007874 trial = atoi(value);
7875 if (period_size_is_plausible_for_low_latency(trial)) {
7876 configured_low_latency_capture_period_size = trial;
7877 }
7878 }
7879
Vignesh Kulothungan7d374312018-02-21 17:12:00 -08007880 adev->mic_break_enabled = property_get_bool("vendor.audio.mic_break", false);
7881
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07007882 if (property_get("vendor.audio_hal.period_multiplier", value, NULL) > 0) {
Haynes Mathew George5beddd42016-06-27 18:33:40 -07007883 af_period_multiplier = atoi(value);
7884 if (af_period_multiplier < 0)
7885 af_period_multiplier = 2;
7886 else if (af_period_multiplier > 4)
7887 af_period_multiplier = 4;
7888
7889 ALOGV("new period_multiplier = %d", af_period_multiplier);
7890 }
7891
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07007892 adev->multi_offload_enable = property_get_bool("vendor.audio.offload.multiple.enabled", false);
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07007893 pthread_mutex_unlock(&adev_init_lock);
7894
Ravi Kumar Alamanda8a0f9772015-06-15 10:35:19 -07007895 if (adev->adm_init)
7896 adev->adm_data = adev->adm_init();
7897
Dhananjay Kumaree4d2002016-10-25 18:02:58 +05307898 qahwi_init(*device);
Sudheer Papothifa9d2282015-09-17 01:53:25 +05307899 audio_extn_perf_lock_init();
Ben Rombergerd771a7c2017-02-22 18:05:17 -08007900 audio_extn_adsp_hdlr_init(adev->mixer);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05307901
7902 audio_extn_snd_mon_init();
7903 pthread_mutex_lock(&adev->lock);
7904 audio_extn_snd_mon_register_listener(adev, adev_snd_mon_cb);
7905 adev->card_status = CARD_STATUS_ONLINE;
Haynes Mathew George01156f92018-04-13 15:29:54 -07007906 audio_extn_battery_properties_listener_init(adev_on_battery_status_changed);
7907 /*
7908 * if the battery state callback happens before charging can be queried,
7909 * it will be guarded with the adev->lock held in the cb function and so
7910 * the callback value will reflect the latest state
7911 */
7912 adev->is_charging = audio_extn_battery_properties_is_charging();
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05307913 audio_extn_sound_trigger_init(adev); /* dependent on snd_mon_init() */
Zhou Songc1088ea2018-06-12 00:17:29 +08007914 audio_extn_sound_trigger_update_battery_status(adev->is_charging);
7915 pthread_mutex_unlock(&adev->lock);
Satish Babu Patakokilac3c5d432017-07-04 22:48:59 +05307916 /* Allocate memory for Device config params */
7917 adev->device_cfg_params = (struct audio_device_config_param*)
7918 calloc(platform_get_max_codec_backend(),
7919 sizeof(struct audio_device_config_param));
7920 if (adev->device_cfg_params == NULL)
7921 ALOGE("%s: Memory allocation failed for Device config params", __func__);
Dhananjay Kumare6293dd2017-05-25 17:25:30 +05307922
Eric Laurent994a6932013-07-17 11:51:42 -07007923 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007924 return 0;
7925}
7926
7927static struct hw_module_methods_t hal_module_methods = {
7928 .open = adev_open,
7929};
7930
7931struct audio_module HAL_MODULE_INFO_SYM = {
7932 .common = {
7933 .tag = HARDWARE_MODULE_TAG,
7934 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
7935 .hal_api_version = HARDWARE_HAL_API_VERSION,
7936 .id = AUDIO_HARDWARE_MODULE_ID,
7937 .name = "QCOM Audio HAL",
Duy Truongfae19622013-11-24 02:17:54 -08007938 .author = "The Linux Foundation",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08007939 .methods = &hal_module_methods,
7940 },
7941};