Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1 | /* |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 2 | * Copyright (c) 2013-2019, The Linux Foundation. All rights reserved. |
Apoorv Raghuvanshi | 9eaf94e | 2013-10-04 16:13:44 -0700 | [diff] [blame] | 3 | * Not a Contribution. |
| 4 | * |
Shiv Maliyappanahalli | 8911f28 | 2014-01-10 15:56:19 -0800 | [diff] [blame] | 5 | * Copyright (C) 2013 The Android Open Source Project |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6 | * |
| 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 Naruka | 1b6513f | 2014-11-22 19:34:13 -0800 | [diff] [blame] | 18 | * |
| 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 Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 36 | */ |
| 37 | |
| 38 | #define LOG_TAG "audio_hw_primary" |
Haynes Mathew George | 5beddd4 | 2016-06-27 18:33:40 -0700 | [diff] [blame] | 39 | #define ATRACE_TAG (ATRACE_TAG_AUDIO|ATRACE_TAG_HAL) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 40 | /*#define LOG_NDEBUG 0*/ |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 41 | /*#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 Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 47 | |
| 48 | #include <errno.h> |
| 49 | #include <pthread.h> |
| 50 | #include <stdint.h> |
| 51 | #include <sys/time.h> |
| 52 | #include <stdlib.h> |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 53 | #include <math.h> |
Eric Laurent | c4aef75 | 2013-09-12 17:45:53 -0700 | [diff] [blame] | 54 | #include <dlfcn.h> |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 55 | #include <sys/resource.h> |
| 56 | #include <sys/prctl.h> |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 57 | |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 58 | #include <log/log.h> |
Haynes Mathew George | 5beddd4 | 2016-06-27 18:33:40 -0700 | [diff] [blame] | 59 | #include <cutils/trace.h> |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 60 | #include <cutils/str_parms.h> |
| 61 | #include <cutils/properties.h> |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 62 | #include <cutils/atomic.h> |
| 63 | #include <cutils/sched_policy.h> |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 64 | #include <hardware/audio_effect.h> |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 65 | #include <hardware/audio_alsaops.h> |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 66 | #include <system/thread_defs.h> |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 67 | #include <tinyalsa/asoundlib.h> |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 68 | #include <audio_effects/effect_aec.h> |
| 69 | #include <audio_effects/effect_ns.h> |
Ashish Jain | f1eaa58 | 2016-05-23 20:54:24 +0530 | [diff] [blame] | 70 | #include <audio_utils/format.h> |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 71 | #include "audio_hw.h" |
Wei Wang | f7ca6c9 | 2017-11-21 14:51:20 -0800 | [diff] [blame] | 72 | #include "audio_perf.h" |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 73 | #include "platform_api.h" |
| 74 | #include <platform.h> |
Apoorv Raghuvanshi | 9eaf94e | 2013-10-04 16:13:44 -0700 | [diff] [blame] | 75 | #include "audio_extn.h" |
Narsinga Rao Chella | 05573b7 | 2013-11-15 15:21:40 -0800 | [diff] [blame] | 76 | #include "voice_extn.h" |
Ashish Jain | d569424 | 2017-09-05 20:09:06 +0530 | [diff] [blame] | 77 | #include "ip_hdlr_intf.h" |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 78 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 79 | #include "sound/compress_params.h" |
ApurupaPattapu | c6a3a9e | 2014-01-10 14:46:02 -0800 | [diff] [blame] | 80 | #include "sound/asound.h" |
ApurupaPattapu | 2e084df | 2013-12-18 15:47:59 -0800 | [diff] [blame] | 81 | |
Revathi Uddaraju | 1eac8b0 | 2017-05-18 17:13:33 +0530 | [diff] [blame] | 82 | #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 Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 88 | #define COMPRESS_OFFLOAD_NUM_FRAGMENTS 4 |
Ashish Jain | 5106d36 | 2016-05-11 19:23:33 +0530 | [diff] [blame] | 89 | /*DIRECT PCM has same buffer sizes as DEEP Buffer*/ |
| 90 | #define DIRECT_PCM_NUM_FRAGMENTS 2 |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 91 | #define COMPRESS_PLAYBACK_VOLUME_MAX 0x2000 |
Vikram Panduranga | 93f080e | 2017-06-07 18:16:14 -0700 | [diff] [blame] | 92 | #define VOIP_PLAYBACK_VOLUME_MAX 0x2000 |
Arun Mirpuri | 5d17087 | 2019-03-26 13:21:31 -0700 | [diff] [blame] | 93 | #define MMAP_PLAYBACK_VOLUME_MAX 0x2000 |
Ramu Gottipati | 97bdcfb | 2018-04-13 17:58:24 +0530 | [diff] [blame] | 94 | #define PCM_PLAYBACK_VOLUME_MAX 0x2000 |
Preetam Singh Ranawat | cb6212e | 2016-07-19 18:33:53 +0530 | [diff] [blame] | 95 | #define DSD_VOLUME_MIN_DB (-110) |
Aniket Kumar Lata | 8426d1f | 2019-06-10 15:25:53 -0700 | [diff] [blame] | 96 | #define INVALID_OUT_VOLUME -1 |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 97 | |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 98 | #define RECORD_GAIN_MIN 0.0f |
| 99 | #define RECORD_GAIN_MAX 1.0f |
| 100 | #define RECORD_VOLUME_CTL_MAX 0x2000 |
| 101 | |
| 102 | /* treat as unsigned Q1.13 */ |
| 103 | #define APP_TYPE_GAIN_DEFAULT 0x2000 |
| 104 | |
Ravi Kumar Alamanda | 060bc5a | 2014-09-05 13:51:35 -0700 | [diff] [blame] | 105 | #define PROXY_OPEN_RETRY_COUNT 100 |
| 106 | #define PROXY_OPEN_WAIT_TIME 20 |
ApurupaPattapu | c6a3a9e | 2014-01-10 14:46:02 -0800 | [diff] [blame] | 107 | |
Arun Mirpuri | b1bec9c | 2019-01-29 16:42:45 -0800 | [diff] [blame] | 108 | #define GET_USECASE_AUDIO_PLAYBACK_PRIMARY(db) \ |
| 109 | (db)? USECASE_AUDIO_PLAYBACK_DEEP_BUFFER : \ |
| 110 | USECASE_AUDIO_PLAYBACK_LOW_LATENCY |
| 111 | #define GET_PCM_CONFIG_AUDIO_PLAYBACK_PRIMARY(db) \ |
| 112 | (db)? pcm_config_deep_buffer : pcm_config_low_latency |
Haynes Mathew George | bf14371 | 2013-12-03 13:02:53 -0800 | [diff] [blame] | 113 | |
Haynes Mathew George | 5beddd4 | 2016-06-27 18:33:40 -0700 | [diff] [blame] | 114 | #define ULL_PERIOD_SIZE (DEFAULT_OUTPUT_SAMPLING_RATE/1000) |
Vikram Panduranga | 93f080e | 2017-06-07 18:16:14 -0700 | [diff] [blame] | 115 | #define DEFAULT_VOIP_BUF_DURATION_MS 20 |
| 116 | #define DEFAULT_VOIP_BIT_DEPTH_BYTE sizeof(int16_t) |
| 117 | #define DEFAULT_VOIP_SAMP_RATE 48000 |
| 118 | |
| 119 | #define VOIP_IO_BUF_SIZE(SR, DURATION_MS, BIT_DEPTH) (SR)/1000 * DURATION_MS * BIT_DEPTH |
| 120 | |
| 121 | struct pcm_config default_pcm_config_voip_copp = { |
| 122 | .channels = 1, |
| 123 | .rate = DEFAULT_VOIP_SAMP_RATE, /* changed when the stream is opened */ |
| 124 | .period_size = VOIP_IO_BUF_SIZE(DEFAULT_VOIP_SAMP_RATE, DEFAULT_VOIP_BUF_DURATION_MS, DEFAULT_VOIP_BIT_DEPTH_BYTE)/2, |
| 125 | .period_count = 2, |
| 126 | .format = PCM_FORMAT_S16_LE, |
kunleiz | 95b597a | 2017-10-23 17:07:33 +0800 | [diff] [blame] | 127 | .avail_min = VOIP_IO_BUF_SIZE(DEFAULT_VOIP_SAMP_RATE, DEFAULT_VOIP_BUF_DURATION_MS, DEFAULT_VOIP_BIT_DEPTH_BYTE)/2, |
| 128 | .stop_threshold = INT_MAX, |
Vikram Panduranga | 93f080e | 2017-06-07 18:16:14 -0700 | [diff] [blame] | 129 | }; |
Haynes Mathew George | 5beddd4 | 2016-06-27 18:33:40 -0700 | [diff] [blame] | 130 | |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 131 | #define MIN_CHANNEL_COUNT 1 |
| 132 | #define DEFAULT_CHANNEL_COUNT 2 |
| 133 | #define MAX_HIFI_CHANNEL_COUNT 8 |
| 134 | |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 135 | #ifndef MAX_TARGET_SPECIFIC_CHANNEL_CNT |
| 136 | #define MAX_CHANNEL_COUNT 1 |
| 137 | #else |
| 138 | #define MAX_CHANNEL_COUNT atoi(XSTR(MAX_TARGET_SPECIFIC_CHANNEL_CNT)) |
| 139 | #define XSTR(x) STR(x) |
| 140 | #define STR(x) #x |
| 141 | #endif |
| 142 | |
Ravi Kumar Alamanda | dc9bb15 | 2014-09-08 15:59:58 -0700 | [diff] [blame] | 143 | static unsigned int configured_low_latency_capture_period_size = |
| 144 | LOW_LATENCY_CAPTURE_PERIOD_SIZE; |
| 145 | |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 146 | #define MMAP_PERIOD_SIZE (DEFAULT_OUTPUT_SAMPLING_RATE/1000) |
| 147 | #define MMAP_PERIOD_COUNT_MIN 32 |
| 148 | #define MMAP_PERIOD_COUNT_MAX 512 |
| 149 | #define MMAP_PERIOD_COUNT_DEFAULT (MMAP_PERIOD_COUNT_MAX) |
| 150 | |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 151 | /* This constant enables extended precision handling. |
| 152 | * TODO The flag is off until more testing is done. |
| 153 | */ |
| 154 | static const bool k_enable_extended_precision = false; |
Arun Mirpuri | e008ed2 | 2019-03-21 11:21:04 -0700 | [diff] [blame] | 155 | extern int AUDIO_DEVICE_IN_ALL_CODEC_BACKEND; |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 156 | |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 157 | struct pcm_config pcm_config_deep_buffer = { |
| 158 | .channels = 2, |
| 159 | .rate = DEFAULT_OUTPUT_SAMPLING_RATE, |
| 160 | .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE, |
| 161 | .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT, |
| 162 | .format = PCM_FORMAT_S16_LE, |
| 163 | .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4, |
| 164 | .stop_threshold = INT_MAX, |
| 165 | .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4, |
| 166 | }; |
| 167 | |
| 168 | struct pcm_config pcm_config_low_latency = { |
| 169 | .channels = 2, |
| 170 | .rate = DEFAULT_OUTPUT_SAMPLING_RATE, |
| 171 | .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE, |
| 172 | .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT, |
| 173 | .format = PCM_FORMAT_S16_LE, |
| 174 | .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4, |
| 175 | .stop_threshold = INT_MAX, |
| 176 | .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4, |
| 177 | }; |
| 178 | |
Vignesh Kulothungan | a692727 | 2019-02-20 15:17:07 -0800 | [diff] [blame] | 179 | struct pcm_config pcm_config_haptics_audio = { |
| 180 | .channels = 1, |
| 181 | .rate = DEFAULT_OUTPUT_SAMPLING_RATE, |
| 182 | .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE, |
| 183 | .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT, |
| 184 | .format = PCM_FORMAT_S16_LE, |
| 185 | .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4, |
| 186 | .stop_threshold = INT_MAX, |
| 187 | .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4, |
| 188 | }; |
| 189 | |
| 190 | struct pcm_config pcm_config_haptics = { |
| 191 | .channels = 1, |
| 192 | .rate = DEFAULT_OUTPUT_SAMPLING_RATE, |
| 193 | .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE, |
| 194 | .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT, |
| 195 | .format = PCM_FORMAT_S16_LE, |
| 196 | .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE, |
| 197 | .stop_threshold = INT_MAX, |
| 198 | .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4, |
| 199 | }; |
| 200 | |
Haynes Mathew George | 5beddd4 | 2016-06-27 18:33:40 -0700 | [diff] [blame] | 201 | static int af_period_multiplier = 4; |
| 202 | struct pcm_config pcm_config_rt = { |
| 203 | .channels = 2, |
| 204 | .rate = DEFAULT_OUTPUT_SAMPLING_RATE, |
| 205 | .period_size = ULL_PERIOD_SIZE, //1 ms |
| 206 | .period_count = 512, //=> buffer size is 512ms |
| 207 | .format = PCM_FORMAT_S16_LE, |
| 208 | .start_threshold = ULL_PERIOD_SIZE*8, //8ms |
| 209 | .stop_threshold = INT_MAX, |
| 210 | .silence_threshold = 0, |
| 211 | .silence_size = 0, |
| 212 | .avail_min = ULL_PERIOD_SIZE, //1 ms |
| 213 | }; |
| 214 | |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 215 | struct pcm_config pcm_config_hdmi_multi = { |
| 216 | .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */ |
| 217 | .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */ |
| 218 | .period_size = HDMI_MULTI_PERIOD_SIZE, |
| 219 | .period_count = HDMI_MULTI_PERIOD_COUNT, |
| 220 | .format = PCM_FORMAT_S16_LE, |
| 221 | .start_threshold = 0, |
| 222 | .stop_threshold = INT_MAX, |
| 223 | .avail_min = 0, |
| 224 | }; |
| 225 | |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 226 | struct pcm_config pcm_config_mmap_playback = { |
| 227 | .channels = 2, |
| 228 | .rate = DEFAULT_OUTPUT_SAMPLING_RATE, |
| 229 | .period_size = MMAP_PERIOD_SIZE, |
| 230 | .period_count = MMAP_PERIOD_COUNT_DEFAULT, |
| 231 | .format = PCM_FORMAT_S16_LE, |
| 232 | .start_threshold = MMAP_PERIOD_SIZE*8, |
| 233 | .stop_threshold = INT32_MAX, |
| 234 | .silence_threshold = 0, |
| 235 | .silence_size = 0, |
| 236 | .avail_min = MMAP_PERIOD_SIZE, //1 ms |
| 237 | }; |
| 238 | |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 239 | struct pcm_config pcm_config_hifi = { |
| 240 | .channels = DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */ |
| 241 | .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */ |
| 242 | .period_size = HIFI_BUFFER_OUTPUT_PERIOD_SIZE, /* change #define */ |
| 243 | .period_count = HIFI_BUFFER_OUTPUT_PERIOD_COUNT, |
| 244 | .format = PCM_FORMAT_S24_3LE, |
| 245 | .start_threshold = 0, |
| 246 | .stop_threshold = INT_MAX, |
| 247 | .avail_min = 0, |
| 248 | }; |
| 249 | |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 250 | struct pcm_config pcm_config_audio_capture = { |
| 251 | .channels = 2, |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 252 | .period_count = AUDIO_CAPTURE_PERIOD_COUNT, |
| 253 | .format = PCM_FORMAT_S16_LE, |
| 254 | }; |
| 255 | |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 256 | struct pcm_config pcm_config_mmap_capture = { |
| 257 | .channels = 2, |
| 258 | .rate = DEFAULT_OUTPUT_SAMPLING_RATE, |
| 259 | .period_size = MMAP_PERIOD_SIZE, |
| 260 | .period_count = MMAP_PERIOD_COUNT_DEFAULT, |
| 261 | .format = PCM_FORMAT_S16_LE, |
| 262 | .start_threshold = 0, |
| 263 | .stop_threshold = INT_MAX, |
| 264 | .silence_threshold = 0, |
| 265 | .silence_size = 0, |
| 266 | .avail_min = MMAP_PERIOD_SIZE, //1 ms |
| 267 | }; |
| 268 | |
Ravi Kumar Alamanda | 060bc5a | 2014-09-05 13:51:35 -0700 | [diff] [blame] | 269 | #define AFE_PROXY_CHANNEL_COUNT 2 |
| 270 | #define AFE_PROXY_SAMPLING_RATE 48000 |
| 271 | |
| 272 | #define AFE_PROXY_PLAYBACK_PERIOD_SIZE 768 |
| 273 | #define AFE_PROXY_PLAYBACK_PERIOD_COUNT 4 |
| 274 | |
| 275 | struct pcm_config pcm_config_afe_proxy_playback = { |
| 276 | .channels = AFE_PROXY_CHANNEL_COUNT, |
| 277 | .rate = AFE_PROXY_SAMPLING_RATE, |
| 278 | .period_size = AFE_PROXY_PLAYBACK_PERIOD_SIZE, |
| 279 | .period_count = AFE_PROXY_PLAYBACK_PERIOD_COUNT, |
| 280 | .format = PCM_FORMAT_S16_LE, |
| 281 | .start_threshold = AFE_PROXY_PLAYBACK_PERIOD_SIZE, |
| 282 | .stop_threshold = INT_MAX, |
| 283 | .avail_min = AFE_PROXY_PLAYBACK_PERIOD_SIZE, |
| 284 | }; |
| 285 | |
| 286 | #define AFE_PROXY_RECORD_PERIOD_SIZE 768 |
| 287 | #define AFE_PROXY_RECORD_PERIOD_COUNT 4 |
| 288 | |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 289 | struct pcm_config pcm_config_audio_capture_rt = { |
| 290 | .channels = 2, |
| 291 | .rate = DEFAULT_OUTPUT_SAMPLING_RATE, |
| 292 | .period_size = ULL_PERIOD_SIZE, |
| 293 | .period_count = 512, |
| 294 | .format = PCM_FORMAT_S16_LE, |
| 295 | .start_threshold = 0, |
| 296 | .stop_threshold = AFE_PROXY_RECORD_PERIOD_SIZE * AFE_PROXY_RECORD_PERIOD_COUNT, |
| 297 | .silence_threshold = 0, |
| 298 | .silence_size = 0, |
| 299 | .avail_min = ULL_PERIOD_SIZE, //1 ms |
| 300 | }; |
| 301 | |
Ravi Kumar Alamanda | 060bc5a | 2014-09-05 13:51:35 -0700 | [diff] [blame] | 302 | struct pcm_config pcm_config_afe_proxy_record = { |
| 303 | .channels = AFE_PROXY_CHANNEL_COUNT, |
| 304 | .rate = AFE_PROXY_SAMPLING_RATE, |
| 305 | .period_size = AFE_PROXY_RECORD_PERIOD_SIZE, |
| 306 | .period_count = AFE_PROXY_RECORD_PERIOD_COUNT, |
| 307 | .format = PCM_FORMAT_S16_LE, |
| 308 | .start_threshold = AFE_PROXY_RECORD_PERIOD_SIZE, |
| 309 | .stop_threshold = INT_MAX, |
| 310 | .avail_min = AFE_PROXY_RECORD_PERIOD_SIZE, |
| 311 | }; |
| 312 | |
Ashish Jain | f1eaa58 | 2016-05-23 20:54:24 +0530 | [diff] [blame] | 313 | #define AUDIO_MAX_PCM_FORMATS 7 |
| 314 | |
| 315 | const uint32_t format_to_bitwidth_table[AUDIO_MAX_PCM_FORMATS] = { |
| 316 | [AUDIO_FORMAT_DEFAULT] = 0, |
| 317 | [AUDIO_FORMAT_PCM_16_BIT] = sizeof(uint16_t), |
| 318 | [AUDIO_FORMAT_PCM_8_BIT] = sizeof(uint8_t), |
| 319 | [AUDIO_FORMAT_PCM_32_BIT] = sizeof(uint32_t), |
| 320 | [AUDIO_FORMAT_PCM_8_24_BIT] = sizeof(uint32_t), |
| 321 | [AUDIO_FORMAT_PCM_FLOAT] = sizeof(float), |
| 322 | [AUDIO_FORMAT_PCM_24_BIT_PACKED] = sizeof(uint8_t) * 3, |
| 323 | }; |
| 324 | |
Shiv Maliyappanahalli | 3bb7358 | 2013-11-05 12:49:15 -0800 | [diff] [blame] | 325 | const char * const use_case_table[AUDIO_USECASE_MAX] = { |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 326 | [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback", |
| 327 | [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback", |
Vignesh Kulothungan | a692727 | 2019-02-20 15:17:07 -0800 | [diff] [blame] | 328 | [USECASE_AUDIO_PLAYBACK_WITH_HAPTICS] = "audio-with-haptics-playback", |
Ravi Kumar Alamanda | 474de5a | 2015-06-25 20:08:01 -0700 | [diff] [blame] | 329 | [USECASE_AUDIO_PLAYBACK_ULL] = "audio-ull-playback", |
| 330 | [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback", |
Shruthi Krishna | ace1085 | 2013-10-25 14:32:12 -0700 | [diff] [blame] | 331 | [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback", |
vivek mehta | 446c396 | 2015-09-14 10:57:35 -0700 | [diff] [blame] | 332 | //Enabled for Direct_PCM |
Subhash Chandra Bose Naripeddy | 16ff4f8 | 2014-04-01 21:03:10 -0700 | [diff] [blame] | 333 | [USECASE_AUDIO_PLAYBACK_OFFLOAD2] = "compress-offload-playback2", |
| 334 | [USECASE_AUDIO_PLAYBACK_OFFLOAD3] = "compress-offload-playback3", |
| 335 | [USECASE_AUDIO_PLAYBACK_OFFLOAD4] = "compress-offload-playback4", |
| 336 | [USECASE_AUDIO_PLAYBACK_OFFLOAD5] = "compress-offload-playback5", |
| 337 | [USECASE_AUDIO_PLAYBACK_OFFLOAD6] = "compress-offload-playback6", |
| 338 | [USECASE_AUDIO_PLAYBACK_OFFLOAD7] = "compress-offload-playback7", |
| 339 | [USECASE_AUDIO_PLAYBACK_OFFLOAD8] = "compress-offload-playback8", |
| 340 | [USECASE_AUDIO_PLAYBACK_OFFLOAD9] = "compress-offload-playback9", |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 341 | [USECASE_AUDIO_PLAYBACK_FM] = "play-fm", |
| 342 | [USECASE_AUDIO_PLAYBACK_MMAP] = "mmap-playback", |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 343 | [USECASE_AUDIO_PLAYBACK_HIFI] = "hifi-playback", |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 344 | [USECASE_AUDIO_PLAYBACK_TTS] = "audio-tts-playback", |
vivek mehta | 0ea887a | 2015-08-26 14:01:20 -0700 | [diff] [blame] | 345 | |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 346 | [USECASE_AUDIO_RECORD] = "audio-record", |
Mingming Yin | e62d784 | 2013-10-25 16:26:03 -0700 | [diff] [blame] | 347 | [USECASE_AUDIO_RECORD_COMPRESS] = "audio-record-compress", |
Dhananjay Kumar | ee4d200 | 2016-10-25 18:02:58 +0530 | [diff] [blame] | 348 | [USECASE_AUDIO_RECORD_COMPRESS2] = "audio-record-compress2", |
| 349 | [USECASE_AUDIO_RECORD_COMPRESS3] = "audio-record-compress3", |
| 350 | [USECASE_AUDIO_RECORD_COMPRESS4] = "audio-record-compress4", |
Dhananjay Kumar | 376e38b | 2017-09-28 22:26:23 +0530 | [diff] [blame] | 351 | [USECASE_AUDIO_RECORD_COMPRESS5] = "audio-record-compress5", |
| 352 | [USECASE_AUDIO_RECORD_COMPRESS6] = "audio-record-compress6", |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 353 | [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record", |
Preetam Singh Ranawat | de84f1a | 2013-11-01 14:58:16 -0700 | [diff] [blame] | 354 | [USECASE_AUDIO_RECORD_FM_VIRTUAL] = "fm-virtual-record", |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 355 | [USECASE_AUDIO_RECORD_MMAP] = "mmap-record", |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 356 | [USECASE_AUDIO_RECORD_HIFI] = "hifi-record", |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 357 | |
Vimal Puthanveed | 5b4d3f1 | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 358 | [USECASE_AUDIO_HFP_SCO] = "hfp-sco", |
Vimal Puthanveed | 47e6485 | 2013-12-20 13:23:39 -0800 | [diff] [blame] | 359 | [USECASE_AUDIO_HFP_SCO_WB] = "hfp-sco-wb", |
Derek Chen | f709279 | 2017-05-23 12:23:53 -0400 | [diff] [blame] | 360 | [USECASE_AUDIO_HFP_SCO_DOWNLINK] = "hfp-sco-downlink", |
| 361 | [USECASE_AUDIO_HFP_SCO_WB_DOWNLINK] = "hfp-sco-wb-downlink", |
Mingming Yin | 3ee55c6 | 2014-08-04 14:23:35 -0700 | [diff] [blame] | 362 | |
Derek Chen | f709279 | 2017-05-23 12:23:53 -0400 | [diff] [blame] | 363 | [USECASE_VOICE_CALL] = "voice-call", |
Shiv Maliyappanahalli | 34b585f | 2013-10-01 15:49:05 -0700 | [diff] [blame] | 364 | [USECASE_VOICE2_CALL] = "voice2-call", |
| 365 | [USECASE_VOLTE_CALL] = "volte-call", |
| 366 | [USECASE_QCHAT_CALL] = "qchat-call", |
Vicky Sehrawat | 7e4fc15 | 2014-02-12 17:58:59 -0800 | [diff] [blame] | 367 | [USECASE_VOWLAN_CALL] = "vowlan-call", |
Vidyakumar Athota | 0e10935 | 2015-02-12 17:38:22 -0800 | [diff] [blame] | 368 | [USECASE_VOICEMMODE1_CALL] = "voicemmode1-call", |
| 369 | [USECASE_VOICEMMODE2_CALL] = "voicemmode2-call", |
Narsinga Rao Chella | 05573b7 | 2013-11-15 15:21:40 -0800 | [diff] [blame] | 370 | [USECASE_COMPRESS_VOIP_CALL] = "compress-voip-call", |
Shiv Maliyappanahalli | da10764 | 2013-10-17 11:16:13 -0700 | [diff] [blame] | 371 | [USECASE_INCALL_REC_UPLINK] = "incall-rec-uplink", |
| 372 | [USECASE_INCALL_REC_DOWNLINK] = "incall-rec-downlink", |
| 373 | [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK] = "incall-rec-uplink-and-downlink", |
Helen Zeng | e56b485 | 2013-12-03 16:54:40 -0800 | [diff] [blame] | 374 | [USECASE_INCALL_REC_UPLINK_COMPRESS] = "incall-rec-uplink-compress", |
| 375 | [USECASE_INCALL_REC_DOWNLINK_COMPRESS] = "incall-rec-downlink-compress", |
| 376 | [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK_COMPRESS] = "incall-rec-uplink-and-downlink-compress", |
| 377 | |
Shiv Maliyappanahalli | f3b9a42 | 2013-10-22 16:38:08 -0700 | [diff] [blame] | 378 | [USECASE_INCALL_MUSIC_UPLINK] = "incall_music_uplink", |
| 379 | [USECASE_INCALL_MUSIC_UPLINK2] = "incall_music_uplink2", |
Gopikrishnaiah Anandan | f538cef | 2013-10-28 14:06:03 -0700 | [diff] [blame] | 380 | [USECASE_AUDIO_SPKR_CALIB_RX] = "spkr-rx-calib", |
| 381 | [USECASE_AUDIO_SPKR_CALIB_TX] = "spkr-vi-record", |
Ravi Kumar Alamanda | 060bc5a | 2014-09-05 13:51:35 -0700 | [diff] [blame] | 382 | |
| 383 | [USECASE_AUDIO_PLAYBACK_AFE_PROXY] = "afe-proxy-playback", |
| 384 | [USECASE_AUDIO_RECORD_AFE_PROXY] = "afe-proxy-record", |
Md Mansoor Ahmed | db1b4f9 | 2018-01-25 18:56:31 +0530 | [diff] [blame] | 385 | [USECASE_AUDIO_PLAYBACK_SILENCE] = "silence-playback", |
Vikram Panduranga | 93f080e | 2017-06-07 18:16:14 -0700 | [diff] [blame] | 386 | |
Siddartha Shaik | 31b530e | 2017-05-19 15:26:33 +0530 | [diff] [blame] | 387 | /* Transcode loopback cases */ |
Surendar Karka | 93cd25a | 2018-08-28 14:21:37 +0530 | [diff] [blame] | 388 | [USECASE_AUDIO_TRANSCODE_LOOPBACK_RX] = "audio-transcode-loopback-rx", |
| 389 | [USECASE_AUDIO_TRANSCODE_LOOPBACK_TX] = "audio-transcode-loopback-tx", |
Vikram Panduranga | 93f080e | 2017-06-07 18:16:14 -0700 | [diff] [blame] | 390 | |
| 391 | [USECASE_AUDIO_PLAYBACK_VOIP] = "audio-playback-voip", |
| 392 | [USECASE_AUDIO_RECORD_VOIP] = "audio-record-voip", |
Varun Balaraj | e49253e | 2017-07-06 19:48:56 +0530 | [diff] [blame] | 393 | /* For Interactive Audio Streams */ |
| 394 | [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM1] = "audio-interactive-stream1", |
| 395 | [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM2] = "audio-interactive-stream2", |
| 396 | [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM3] = "audio-interactive-stream3", |
| 397 | [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM4] = "audio-interactive-stream4", |
| 398 | [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM5] = "audio-interactive-stream5", |
| 399 | [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM6] = "audio-interactive-stream6", |
| 400 | [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM7] = "audio-interactive-stream7", |
| 401 | [USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM8] = "audio-interactive-stream8", |
Garmond Leung | e2433c3 | 2017-09-28 21:51:22 -0700 | [diff] [blame] | 402 | |
Aniket Kumar Lata | 7fd86e1 | 2018-02-20 19:26:10 -0800 | [diff] [blame] | 403 | [USECASE_AUDIO_EC_REF_LOOPBACK] = "ec-ref-audio-capture", |
| 404 | |
Derek Chen | f6318be | 2017-06-12 17:16:24 -0400 | [diff] [blame] | 405 | [USECASE_AUDIO_A2DP_ABR_FEEDBACK] = "a2dp-abr-feedback", |
| 406 | |
| 407 | [USECASE_AUDIO_PLAYBACK_MEDIA] = "media-playback", |
| 408 | [USECASE_AUDIO_PLAYBACK_SYS_NOTIFICATION] = "sys-notification-playback", |
| 409 | [USECASE_AUDIO_PLAYBACK_NAV_GUIDANCE] = "nav-guidance-playback", |
| 410 | [USECASE_AUDIO_PLAYBACK_PHONE] = "phone-playback", |
Derek Chen | df05eea | 2019-08-01 13:57:49 -0700 | [diff] [blame] | 411 | [USECASE_AUDIO_PLAYBACK_REAR_SEAT] = "rear-seat-playback", |
Rahul Sharma | 9977098 | 2019-03-06 17:05:26 +0530 | [diff] [blame] | 412 | [USECASE_AUDIO_FM_TUNER_EXT] = "fm-tuner-ext", |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 413 | }; |
| 414 | |
Subhash Chandra Bose Naripeddy | 16ff4f8 | 2014-04-01 21:03:10 -0700 | [diff] [blame] | 415 | static const audio_usecase_t offload_usecases[] = { |
| 416 | USECASE_AUDIO_PLAYBACK_OFFLOAD, |
Subhash Chandra Bose Naripeddy | 16ff4f8 | 2014-04-01 21:03:10 -0700 | [diff] [blame] | 417 | USECASE_AUDIO_PLAYBACK_OFFLOAD2, |
| 418 | USECASE_AUDIO_PLAYBACK_OFFLOAD3, |
| 419 | USECASE_AUDIO_PLAYBACK_OFFLOAD4, |
| 420 | USECASE_AUDIO_PLAYBACK_OFFLOAD5, |
| 421 | USECASE_AUDIO_PLAYBACK_OFFLOAD6, |
| 422 | USECASE_AUDIO_PLAYBACK_OFFLOAD7, |
| 423 | USECASE_AUDIO_PLAYBACK_OFFLOAD8, |
| 424 | USECASE_AUDIO_PLAYBACK_OFFLOAD9, |
Subhash Chandra Bose Naripeddy | 16ff4f8 | 2014-04-01 21:03:10 -0700 | [diff] [blame] | 425 | }; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 426 | |
Varun Balaraj | e49253e | 2017-07-06 19:48:56 +0530 | [diff] [blame] | 427 | static const audio_usecase_t interactive_usecases[] = { |
| 428 | USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM1, |
| 429 | USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM2, |
| 430 | USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM3, |
| 431 | USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM4, |
| 432 | USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM5, |
| 433 | USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM6, |
| 434 | USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM7, |
| 435 | USECASE_AUDIO_PLAYBACK_INTERACTIVE_STREAM8, |
| 436 | }; |
| 437 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 438 | #define STRING_TO_ENUM(string) { #string, string } |
| 439 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 440 | struct string_to_enum { |
| 441 | const char *name; |
| 442 | uint32_t value; |
| 443 | }; |
| 444 | |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 445 | static const struct string_to_enum channels_name_to_enum_table[] = { |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 446 | STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO), |
Mingming Yin | 3a941d4 | 2016-02-17 18:08:05 -0800 | [diff] [blame] | 447 | STRING_TO_ENUM(AUDIO_CHANNEL_OUT_2POINT1), |
| 448 | STRING_TO_ENUM(AUDIO_CHANNEL_OUT_QUAD), |
| 449 | STRING_TO_ENUM(AUDIO_CHANNEL_OUT_SURROUND), |
Pradnya Chaphekar | 4403bd7 | 2014-09-09 09:50:01 -0700 | [diff] [blame] | 450 | STRING_TO_ENUM(AUDIO_CHANNEL_OUT_PENTA), |
Mingming Yin | 3a941d4 | 2016-02-17 18:08:05 -0800 | [diff] [blame] | 451 | STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1), |
| 452 | STRING_TO_ENUM(AUDIO_CHANNEL_OUT_6POINT1), |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 453 | STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1), |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 454 | STRING_TO_ENUM(AUDIO_CHANNEL_IN_MONO), |
| 455 | STRING_TO_ENUM(AUDIO_CHANNEL_IN_STEREO), |
| 456 | STRING_TO_ENUM(AUDIO_CHANNEL_IN_FRONT_BACK), |
| 457 | STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_1), |
| 458 | STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_2), |
| 459 | STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_3), |
| 460 | STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_4), |
| 461 | STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_5), |
| 462 | STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_6), |
| 463 | STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_7), |
| 464 | STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_8), |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 465 | }; |
| 466 | |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 467 | static const struct string_to_enum formats_name_to_enum_table[] = { |
| 468 | STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT), |
| 469 | STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED), |
| 470 | STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT), |
Pradnya Chaphekar | 8a9dcd8 | 2014-09-09 09:49:10 -0700 | [diff] [blame] | 471 | STRING_TO_ENUM(AUDIO_FORMAT_AC3), |
| 472 | STRING_TO_ENUM(AUDIO_FORMAT_E_AC3), |
| 473 | STRING_TO_ENUM(AUDIO_FORMAT_E_AC3_JOC), |
Ben Romberger | 1aaaf86 | 2017-04-06 17:49:46 -0700 | [diff] [blame] | 474 | STRING_TO_ENUM(AUDIO_FORMAT_DOLBY_TRUEHD), |
Mingming Yin | 3a941d4 | 2016-02-17 18:08:05 -0800 | [diff] [blame] | 475 | STRING_TO_ENUM(AUDIO_FORMAT_DTS), |
| 476 | STRING_TO_ENUM(AUDIO_FORMAT_DTS_HD), |
Naresh Tanniru | 928f086 | 2017-04-07 16:44:23 -0700 | [diff] [blame] | 477 | STRING_TO_ENUM(AUDIO_FORMAT_IEC61937) |
Mingming Yin | 3a941d4 | 2016-02-17 18:08:05 -0800 | [diff] [blame] | 478 | }; |
| 479 | |
| 480 | //list of all supported sample rates by HDMI specification. |
| 481 | static const int out_hdmi_sample_rates[] = { |
| 482 | 32000, 44100, 48000, 88200, 96000, 176400, 192000, |
| 483 | }; |
| 484 | |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 485 | static const struct string_to_enum out_sample_rates_name_to_enum_table[] = { |
Mingming Yin | 3a941d4 | 2016-02-17 18:08:05 -0800 | [diff] [blame] | 486 | STRING_TO_ENUM(32000), |
| 487 | STRING_TO_ENUM(44100), |
| 488 | STRING_TO_ENUM(48000), |
| 489 | STRING_TO_ENUM(88200), |
| 490 | STRING_TO_ENUM(96000), |
| 491 | STRING_TO_ENUM(176400), |
| 492 | STRING_TO_ENUM(192000), |
Pradnya Chaphekar | 8a9dcd8 | 2014-09-09 09:49:10 -0700 | [diff] [blame] | 493 | }; |
| 494 | |
Carter Hsu | 2e429db | 2019-05-14 18:50:52 +0800 | [diff] [blame] | 495 | struct in_effect_list { |
| 496 | struct listnode list; |
| 497 | effect_handle_t handle; |
| 498 | }; |
| 499 | |
Apoorv Raghuvanshi | 6e26284 | 2013-10-06 14:39:35 -0700 | [diff] [blame] | 500 | static struct audio_device *adev = NULL; |
Ben Romberger | 02ab119 | 2018-05-24 12:10:08 -0700 | [diff] [blame] | 501 | static pthread_mutex_t adev_init_lock = PTHREAD_MUTEX_INITIALIZER; |
Kiran Kandi | 910e186 | 2013-10-29 13:29:42 -0700 | [diff] [blame] | 502 | static unsigned int audio_device_ref_count; |
vivek mehta | b72d08d | 2016-04-29 03:16:47 -0700 | [diff] [blame] | 503 | //cache last MBDRC cal step level |
| 504 | static int last_known_cal_step = -1 ; |
Kiran Kandi | 910e186 | 2013-10-29 13:29:42 -0700 | [diff] [blame] | 505 | |
Chaithanya Krishna Bacharaju | 69d2e4c | 2017-05-26 18:22:46 +0530 | [diff] [blame] | 506 | static int check_a2dp_restore_l(struct audio_device *adev, struct stream_out *out, bool restore); |
| 507 | static int out_set_compr_volume(struct audio_stream_out *stream, float left, float right); |
Arun Mirpuri | 5d17087 | 2019-03-26 13:21:31 -0700 | [diff] [blame] | 508 | static int out_set_mmap_volume(struct audio_stream_out *stream, float left, float right); |
Zhou Song | 2b8f28f | 2017-09-11 10:51:38 +0800 | [diff] [blame] | 509 | static int out_set_voip_volume(struct audio_stream_out *stream, float left, float right); |
Ramu Gottipati | 97bdcfb | 2018-04-13 17:58:24 +0530 | [diff] [blame] | 510 | static int out_set_pcm_volume(struct audio_stream_out *stream, float left, float right); |
Chaithanya Krishna Bacharaju | 69d2e4c | 2017-05-26 18:22:46 +0530 | [diff] [blame] | 511 | |
Derek Chen | 6f29367 | 2019-04-01 01:40:24 -0700 | [diff] [blame] | 512 | static void adev_snd_mon_cb(void *cookie, struct str_parms *parms); |
| 513 | static void in_snd_mon_cb(void * stream, struct str_parms * parms); |
| 514 | static void out_snd_mon_cb(void * stream, struct str_parms * parms); |
| 515 | |
Zhou Song | 331c8e5 | 2019-08-26 14:16:12 +0800 | [diff] [blame] | 516 | static int configure_btsco_sample_rate(snd_device_t snd_device); |
| 517 | |
Vatsal Bucha | c09ae06 | 2018-11-14 13:25:08 +0530 | [diff] [blame] | 518 | #ifdef AUDIO_FEATURE_ENABLED_GCOV |
| 519 | extern void __gcov_flush(); |
| 520 | static void enable_gcov() |
| 521 | { |
| 522 | __gcov_flush(); |
| 523 | } |
| 524 | #else |
| 525 | static void enable_gcov() |
| 526 | { |
| 527 | } |
| 528 | #endif |
| 529 | |
justinweng | 20fb6d8 | 2019-02-21 18:49:00 -0700 | [diff] [blame] | 530 | static int in_set_microphone_direction(const struct audio_stream_in *stream, |
| 531 | audio_microphone_direction_t dir); |
| 532 | static int in_set_microphone_field_dimension(const struct audio_stream_in *stream, float zoom); |
| 533 | |
Haynes Mathew George | 5beddd4 | 2016-06-27 18:33:40 -0700 | [diff] [blame] | 534 | static bool may_use_noirq_mode(struct audio_device *adev, audio_usecase_t uc_id, |
| 535 | int flags __unused) |
| 536 | { |
| 537 | int dir = 0; |
| 538 | switch (uc_id) { |
| 539 | case USECASE_AUDIO_RECORD_LOW_LATENCY: |
| 540 | dir = 1; |
| 541 | case USECASE_AUDIO_PLAYBACK_ULL: |
| 542 | break; |
| 543 | default: |
| 544 | return false; |
| 545 | } |
| 546 | |
| 547 | int dev_id = platform_get_pcm_device_id(uc_id, dir == 0 ? |
| 548 | PCM_PLAYBACK : PCM_CAPTURE); |
| 549 | if (adev->adm_is_noirq_avail) |
| 550 | return adev->adm_is_noirq_avail(adev->adm_data, |
| 551 | adev->snd_card, dev_id, dir); |
| 552 | return false; |
| 553 | } |
| 554 | |
| 555 | static void register_out_stream(struct stream_out *out) |
| 556 | { |
| 557 | struct audio_device *adev = out->dev; |
| 558 | if (is_offload_usecase(out->usecase) || |
| 559 | !adev->adm_register_output_stream) |
| 560 | return; |
| 561 | |
| 562 | // register stream first for backward compatibility |
| 563 | adev->adm_register_output_stream(adev->adm_data, |
| 564 | out->handle, |
| 565 | out->flags); |
| 566 | |
| 567 | if (!adev->adm_set_config) |
| 568 | return; |
| 569 | |
| 570 | if (out->realtime) |
| 571 | adev->adm_set_config(adev->adm_data, |
| 572 | out->handle, |
| 573 | out->pcm, &out->config); |
| 574 | } |
| 575 | |
| 576 | static void register_in_stream(struct stream_in *in) |
| 577 | { |
| 578 | struct audio_device *adev = in->dev; |
| 579 | if (!adev->adm_register_input_stream) |
| 580 | return; |
| 581 | |
| 582 | adev->adm_register_input_stream(adev->adm_data, |
| 583 | in->capture_handle, |
| 584 | in->flags); |
| 585 | |
| 586 | if (!adev->adm_set_config) |
| 587 | return; |
| 588 | |
| 589 | if (in->realtime) |
| 590 | adev->adm_set_config(adev->adm_data, |
| 591 | in->capture_handle, |
| 592 | in->pcm, |
| 593 | &in->config); |
| 594 | } |
| 595 | |
| 596 | static void request_out_focus(struct stream_out *out, long ns) |
| 597 | { |
| 598 | struct audio_device *adev = out->dev; |
| 599 | |
Haynes Mathew George | 5beddd4 | 2016-06-27 18:33:40 -0700 | [diff] [blame] | 600 | if (adev->adm_request_focus_v2) |
| 601 | adev->adm_request_focus_v2(adev->adm_data, out->handle, ns); |
| 602 | else if (adev->adm_request_focus) |
| 603 | adev->adm_request_focus(adev->adm_data, out->handle); |
| 604 | } |
| 605 | |
Aniket Kumar Lata | 60586a9 | 2019-05-22 22:18:55 -0700 | [diff] [blame] | 606 | static int request_in_focus(struct stream_in *in, long ns) |
Haynes Mathew George | 5beddd4 | 2016-06-27 18:33:40 -0700 | [diff] [blame] | 607 | { |
| 608 | struct audio_device *adev = in->dev; |
Aniket Kumar Lata | 60586a9 | 2019-05-22 22:18:55 -0700 | [diff] [blame] | 609 | int ret = 0; |
Haynes Mathew George | 5beddd4 | 2016-06-27 18:33:40 -0700 | [diff] [blame] | 610 | |
Aniket Kumar Lata | 60586a9 | 2019-05-22 22:18:55 -0700 | [diff] [blame] | 611 | if (adev->adm_request_focus_v2_1) |
| 612 | ret = adev->adm_request_focus_v2_1(adev->adm_data, in->capture_handle, ns); |
| 613 | else if (adev->adm_request_focus_v2) |
Haynes Mathew George | 5beddd4 | 2016-06-27 18:33:40 -0700 | [diff] [blame] | 614 | adev->adm_request_focus_v2(adev->adm_data, in->capture_handle, ns); |
| 615 | else if (adev->adm_request_focus) |
| 616 | adev->adm_request_focus(adev->adm_data, in->capture_handle); |
Aniket Kumar Lata | 60586a9 | 2019-05-22 22:18:55 -0700 | [diff] [blame] | 617 | |
| 618 | return ret; |
Haynes Mathew George | 5beddd4 | 2016-06-27 18:33:40 -0700 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | static void release_out_focus(struct stream_out *out) |
| 622 | { |
| 623 | struct audio_device *adev = out->dev; |
| 624 | |
| 625 | if (adev->adm_abandon_focus) |
| 626 | adev->adm_abandon_focus(adev->adm_data, out->handle); |
| 627 | } |
| 628 | |
| 629 | static void release_in_focus(struct stream_in *in) |
| 630 | { |
| 631 | struct audio_device *adev = in->dev; |
| 632 | if (adev->adm_abandon_focus) |
| 633 | adev->adm_abandon_focus(adev->adm_data, in->capture_handle); |
| 634 | } |
| 635 | |
Dhananjay Kumar | e6293dd | 2017-05-25 17:25:30 +0530 | [diff] [blame] | 636 | static int parse_snd_card_status(struct str_parms *parms, int *card, |
| 637 | card_status_t *status) |
| 638 | { |
| 639 | char value[32]={0}; |
| 640 | char state[32]={0}; |
| 641 | |
| 642 | int ret = str_parms_get_str(parms, "SND_CARD_STATUS", value, sizeof(value)); |
| 643 | if (ret < 0) |
| 644 | return -1; |
| 645 | |
| 646 | // sscanf should be okay as value is of max length 32. |
| 647 | // same as sizeof state. |
| 648 | if (sscanf(value, "%d,%s", card, state) < 2) |
| 649 | return -1; |
| 650 | |
| 651 | *status = !strcmp(state, "ONLINE") ? CARD_STATUS_ONLINE : |
| 652 | CARD_STATUS_OFFLINE; |
| 653 | return 0; |
| 654 | } |
| 655 | |
Haynes Mathew George | b0f5dc3 | 2017-10-06 18:35:12 -0700 | [diff] [blame] | 656 | static inline void adjust_frames_for_device_delay(struct stream_out *out, |
| 657 | uint32_t *dsp_frames) { |
| 658 | // Adjustment accounts for A2dp encoder latency with offload usecases |
| 659 | // Note: Encoder latency is returned in ms. |
| 660 | if (AUDIO_DEVICE_OUT_ALL_A2DP & out->devices) { |
| 661 | unsigned long offset = |
| 662 | (audio_extn_a2dp_get_encoder_latency() * out->sample_rate / 1000); |
| 663 | *dsp_frames = (*dsp_frames > offset) ? (*dsp_frames - offset) : 0; |
| 664 | } |
| 665 | } |
| 666 | |
vivek mehta | a76401a | 2015-04-24 14:12:15 -0700 | [diff] [blame] | 667 | __attribute__ ((visibility ("default"))) |
| 668 | bool audio_hw_send_gain_dep_calibration(int level) { |
| 669 | bool ret_val = false; |
vivek mehta | b72d08d | 2016-04-29 03:16:47 -0700 | [diff] [blame] | 670 | ALOGV("%s: called ...", __func__); |
vivek mehta | a76401a | 2015-04-24 14:12:15 -0700 | [diff] [blame] | 671 | |
| 672 | pthread_mutex_lock(&adev_init_lock); |
| 673 | |
| 674 | if (adev != NULL && adev->platform != NULL) { |
| 675 | pthread_mutex_lock(&adev->lock); |
| 676 | ret_val = platform_send_gain_dep_cal(adev->platform, level); |
vivek mehta | b72d08d | 2016-04-29 03:16:47 -0700 | [diff] [blame] | 677 | |
Preetam Singh Ranawat | f4ae022 | 2017-05-31 17:07:28 +0530 | [diff] [blame] | 678 | // cache level info for any of the use case which |
| 679 | // was not started. |
| 680 | last_known_cal_step = level;; |
vivek mehta | b72d08d | 2016-04-29 03:16:47 -0700 | [diff] [blame] | 681 | |
vivek mehta | a76401a | 2015-04-24 14:12:15 -0700 | [diff] [blame] | 682 | pthread_mutex_unlock(&adev->lock); |
| 683 | } else { |
| 684 | ALOGE("%s: %s is NULL", __func__, adev == NULL ? "adev" : "adev->platform"); |
| 685 | } |
| 686 | |
| 687 | pthread_mutex_unlock(&adev_init_lock); |
| 688 | |
| 689 | return ret_val; |
| 690 | } |
| 691 | |
Ashish Jain | 5106d36 | 2016-05-11 19:23:33 +0530 | [diff] [blame] | 692 | static int check_and_set_gapless_mode(struct audio_device *adev, bool enable_gapless) |
| 693 | { |
Krishnankutty Kolathappilly | 6d8788b | 2014-01-09 12:45:31 -0800 | [diff] [blame] | 694 | bool gapless_enabled = false; |
| 695 | const char *mixer_ctl_name = "Compress Gapless Playback"; |
| 696 | struct mixer_ctl *ctl; |
| 697 | |
| 698 | ALOGV("%s:", __func__); |
Aniket Kumar Lata | 8fc67e6 | 2017-05-02 12:33:46 -0700 | [diff] [blame] | 699 | gapless_enabled = property_get_bool("vendor.audio.offload.gapless.enabled", false); |
Ashish Jain | 5106d36 | 2016-05-11 19:23:33 +0530 | [diff] [blame] | 700 | |
| 701 | /*Disable gapless if its AV playback*/ |
| 702 | gapless_enabled = gapless_enabled && enable_gapless; |
Krishnankutty Kolathappilly | 6d8788b | 2014-01-09 12:45:31 -0800 | [diff] [blame] | 703 | |
| 704 | ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name); |
| 705 | if (!ctl) { |
| 706 | ALOGE("%s: Could not get ctl for mixer cmd - %s", |
| 707 | __func__, mixer_ctl_name); |
| 708 | return -EINVAL; |
| 709 | } |
| 710 | |
| 711 | if (mixer_ctl_set_value(ctl, 0, gapless_enabled) < 0) { |
| 712 | ALOGE("%s: Could not set gapless mode %d", |
| 713 | __func__, gapless_enabled); |
| 714 | return -EINVAL; |
| 715 | } |
| 716 | return 0; |
| 717 | } |
Haynes Mathew George | 5191a85 | 2013-09-11 14:19:36 -0700 | [diff] [blame] | 718 | |
Aniket Kumar Lata | f56b640 | 2016-10-27 12:03:18 -0700 | [diff] [blame] | 719 | __attribute__ ((visibility ("default"))) |
| 720 | int audio_hw_get_gain_level_mapping(struct amp_db_and_gain_table *mapping_tbl, |
| 721 | int table_size) { |
| 722 | int ret_val = 0; |
| 723 | ALOGV("%s: enter ... ", __func__); |
| 724 | |
| 725 | pthread_mutex_lock(&adev_init_lock); |
| 726 | if (adev == NULL) { |
| 727 | ALOGW("%s: adev is NULL .... ", __func__); |
| 728 | goto done; |
| 729 | } |
| 730 | |
| 731 | pthread_mutex_lock(&adev->lock); |
| 732 | ret_val = platform_get_gain_level_mapping(mapping_tbl, table_size); |
| 733 | pthread_mutex_unlock(&adev->lock); |
| 734 | done: |
| 735 | pthread_mutex_unlock(&adev_init_lock); |
| 736 | ALOGV("%s: exit ... ", __func__); |
| 737 | return ret_val; |
| 738 | } |
| 739 | |
Arun Mirpuri | b1bec9c | 2019-01-29 16:42:45 -0800 | [diff] [blame] | 740 | bool audio_hw_send_qdsp_parameter(int stream_type, float vol, bool active) |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 741 | { |
| 742 | bool ret = false; |
| 743 | ALOGV("%s: enter ...", __func__); |
| 744 | |
| 745 | pthread_mutex_lock(&adev_init_lock); |
| 746 | |
| 747 | if (adev != NULL && adev->platform != NULL) { |
| 748 | pthread_mutex_lock(&adev->lock); |
Arun Mirpuri | b1bec9c | 2019-01-29 16:42:45 -0800 | [diff] [blame] | 749 | ret = audio_extn_qdsp_set_state(adev, stream_type, vol, active); |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 750 | pthread_mutex_unlock(&adev->lock); |
| 751 | } |
| 752 | |
| 753 | pthread_mutex_unlock(&adev_init_lock); |
| 754 | |
| 755 | ALOGV("%s: exit with ret %d", __func__, ret); |
| 756 | return ret; |
| 757 | } |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 758 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 759 | static bool is_supported_format(audio_format_t format) |
| 760 | { |
Eric Laurent | 86e1713 | 2013-09-12 17:49:30 -0700 | [diff] [blame] | 761 | if (format == AUDIO_FORMAT_MP3 || |
Satish Babu Patakokila | 0c31392 | 2016-12-08 12:07:08 +0530 | [diff] [blame] | 762 | format == AUDIO_FORMAT_MP2 || |
Ashish Jain | f9b7816 | 2014-08-25 20:36:25 +0530 | [diff] [blame] | 763 | format == AUDIO_FORMAT_AAC_LC || |
| 764 | format == AUDIO_FORMAT_AAC_HE_V1 || |
| 765 | format == AUDIO_FORMAT_AAC_HE_V2 || |
Manish Dewangan | a6fc544 | 2015-08-24 20:30:31 +0530 | [diff] [blame] | 766 | format == AUDIO_FORMAT_AAC_ADTS_LC || |
| 767 | format == AUDIO_FORMAT_AAC_ADTS_HE_V1 || |
| 768 | format == AUDIO_FORMAT_AAC_ADTS_HE_V2 || |
Arun Kumar Dasari | 3b17418 | 2016-12-27 13:01:14 +0530 | [diff] [blame] | 769 | format == AUDIO_FORMAT_AAC_LATM_LC || |
| 770 | format == AUDIO_FORMAT_AAC_LATM_HE_V1 || |
| 771 | format == AUDIO_FORMAT_AAC_LATM_HE_V2 || |
Ashish Jain | 5106d36 | 2016-05-11 19:23:33 +0530 | [diff] [blame] | 772 | format == AUDIO_FORMAT_PCM_24_BIT_PACKED || |
| 773 | format == AUDIO_FORMAT_PCM_8_24_BIT || |
Ashish Jain | f1eaa58 | 2016-05-23 20:54:24 +0530 | [diff] [blame] | 774 | format == AUDIO_FORMAT_PCM_FLOAT || |
| 775 | format == AUDIO_FORMAT_PCM_32_BIT || |
vivek mehta | 0ea887a | 2015-08-26 14:01:20 -0700 | [diff] [blame] | 776 | format == AUDIO_FORMAT_PCM_16_BIT || |
Satish Babu Patakokila | 1caa1b7 | 2016-05-24 13:47:08 +0530 | [diff] [blame] | 777 | format == AUDIO_FORMAT_AC3 || |
| 778 | format == AUDIO_FORMAT_E_AC3 || |
Ben Romberger | 1aaaf86 | 2017-04-06 17:49:46 -0700 | [diff] [blame] | 779 | format == AUDIO_FORMAT_DOLBY_TRUEHD || |
Satish Babu Patakokila | 1caa1b7 | 2016-05-24 13:47:08 +0530 | [diff] [blame] | 780 | format == AUDIO_FORMAT_DTS || |
| 781 | format == AUDIO_FORMAT_DTS_HD || |
Weiyin Jiang | 18ac4e9 | 2015-03-15 15:03:40 +0800 | [diff] [blame] | 782 | format == AUDIO_FORMAT_FLAC || |
Satya Krishna Pindiproli | 7047160 | 2015-04-24 19:12:43 +0530 | [diff] [blame] | 783 | format == AUDIO_FORMAT_ALAC || |
| 784 | format == AUDIO_FORMAT_APE || |
Preetam Singh Ranawat | cb6212e | 2016-07-19 18:33:53 +0530 | [diff] [blame] | 785 | format == AUDIO_FORMAT_DSD || |
Satya Krishna Pindiproli | 7047160 | 2015-04-24 19:12:43 +0530 | [diff] [blame] | 786 | format == AUDIO_FORMAT_VORBIS || |
Weiyin Jiang | 18ac4e9 | 2015-03-15 15:03:40 +0800 | [diff] [blame] | 787 | format == AUDIO_FORMAT_WMA || |
Dhanalakshmi Siddani | 1873793 | 2016-11-29 17:33:17 +0530 | [diff] [blame] | 788 | format == AUDIO_FORMAT_WMA_PRO || |
Naresh Tanniru | 928f086 | 2017-04-07 16:44:23 -0700 | [diff] [blame] | 789 | format == AUDIO_FORMAT_APTX || |
| 790 | format == AUDIO_FORMAT_IEC61937) |
ApurupaPattapu | c6a3a9e | 2014-01-10 14:46:02 -0800 | [diff] [blame] | 791 | return true; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 792 | |
| 793 | return false; |
| 794 | } |
| 795 | |
Haynes Mathew George | 5beddd4 | 2016-06-27 18:33:40 -0700 | [diff] [blame] | 796 | static inline bool is_mmap_usecase(audio_usecase_t uc_id) |
| 797 | { |
| 798 | return (uc_id == USECASE_AUDIO_RECORD_AFE_PROXY) || |
| 799 | (uc_id == USECASE_AUDIO_PLAYBACK_AFE_PROXY); |
| 800 | } |
| 801 | |
Aniket Kumar Lata | 8426d1f | 2019-06-10 15:25:53 -0700 | [diff] [blame] | 802 | static inline bool is_valid_volume(float left, float right) |
| 803 | { |
| 804 | return ((left >= 0.0f && right >= 0.0f) ? true : false); |
| 805 | } |
| 806 | |
Avinash Vaish | 71a8b97 | 2014-07-24 15:36:33 +0530 | [diff] [blame] | 807 | static int enable_audio_route_for_voice_usecases(struct audio_device *adev, |
| 808 | struct audio_usecase *uc_info) |
| 809 | { |
| 810 | struct listnode *node; |
| 811 | struct audio_usecase *usecase; |
| 812 | |
| 813 | if (uc_info == NULL) |
| 814 | return -EINVAL; |
| 815 | |
| 816 | /* Re-route all voice usecases on the shared backend other than the |
| 817 | specified usecase to new snd devices */ |
| 818 | list_for_each(node, &adev->usecase_list) { |
| 819 | usecase = node_to_item(node, struct audio_usecase, list); |
Narsinga Rao Chella | e7f33c1 | 2015-01-28 15:23:34 -0800 | [diff] [blame] | 820 | if ((usecase->type == VOICE_CALL) && (usecase != uc_info)) |
Avinash Vaish | 71a8b97 | 2014-07-24 15:36:33 +0530 | [diff] [blame] | 821 | enable_audio_route(adev, usecase); |
| 822 | } |
| 823 | return 0; |
| 824 | } |
| 825 | |
Preetam Singh Ranawat | 6a83666 | 2016-09-08 17:04:35 +0530 | [diff] [blame] | 826 | static void enable_asrc_mode(struct audio_device *adev) |
Preetam Singh Ranawat | b0c0dd7 | 2016-08-18 00:32:06 +0530 | [diff] [blame] | 827 | { |
Preetam Singh Ranawat | 6a83666 | 2016-09-08 17:04:35 +0530 | [diff] [blame] | 828 | ALOGV("%s", __func__); |
| 829 | audio_route_apply_and_update_path(adev->audio_route, |
| 830 | "asrc-mode"); |
| 831 | adev->asrc_mode_enabled = true; |
| 832 | } |
| 833 | |
| 834 | static void disable_asrc_mode(struct audio_device *adev) |
| 835 | { |
| 836 | ALOGV("%s", __func__); |
| 837 | audio_route_reset_and_update_path(adev->audio_route, |
| 838 | "asrc-mode"); |
| 839 | adev->asrc_mode_enabled = false; |
| 840 | } |
| 841 | |
| 842 | /* |
| 843 | * - Enable ASRC mode for incoming mix path use case(Headphone backend)if Headphone |
| 844 | * 44.1 or Native DSD backends are enabled for any of current use case. |
| 845 | * e.g. 48-> + (Naitve DSD or Headphone 44.1) |
| 846 | * - Disable current mix path use case(Headphone backend) and re-enable it with |
| 847 | * ASRC mode for incoming Headphone 44.1 or Native DSD use case. |
| 848 | * e.g. Naitve DSD or Headphone 44.1 -> + 48 |
| 849 | */ |
Preetam Singh Ranawat | 43eac68 | 2017-03-07 18:19:02 +0530 | [diff] [blame] | 850 | static void check_and_set_asrc_mode(struct audio_device *adev, |
| 851 | struct audio_usecase *uc_info, |
| 852 | snd_device_t snd_device) |
Preetam Singh Ranawat | 6a83666 | 2016-09-08 17:04:35 +0530 | [diff] [blame] | 853 | { |
| 854 | ALOGV("%s snd device %d", __func__, snd_device); |
Preetam Singh Ranawat | 43eac68 | 2017-03-07 18:19:02 +0530 | [diff] [blame] | 855 | int i, num_new_devices = 0; |
| 856 | snd_device_t split_new_snd_devices[SND_DEVICE_OUT_END]; |
| 857 | /* |
| 858 | *Split snd device for new combo use case |
| 859 | *e.g. Headphopne 44.1-> + Ringtone (Headphone + Speaker) |
| 860 | */ |
| 861 | if (platform_split_snd_device(adev->platform, |
| 862 | snd_device, |
| 863 | &num_new_devices, |
| 864 | split_new_snd_devices) == 0) { |
| 865 | for (i = 0; i < num_new_devices; i++) |
| 866 | check_and_set_asrc_mode(adev, uc_info, split_new_snd_devices[i]); |
| 867 | } else { |
| 868 | int new_backend_idx = platform_get_backend_index(snd_device); |
| 869 | if (((new_backend_idx == HEADPHONE_BACKEND) || |
| 870 | (new_backend_idx == HEADPHONE_44_1_BACKEND) || |
| 871 | (new_backend_idx == DSD_NATIVE_BACKEND)) && |
| 872 | !adev->asrc_mode_enabled) { |
| 873 | struct listnode *node = NULL; |
| 874 | struct audio_usecase *uc = NULL; |
| 875 | struct stream_out *curr_out = NULL; |
| 876 | int usecase_backend_idx = DEFAULT_CODEC_BACKEND; |
| 877 | int i, num_devices, ret = 0; |
| 878 | snd_device_t split_snd_devices[SND_DEVICE_OUT_END]; |
Preetam Singh Ranawat | 6a83666 | 2016-09-08 17:04:35 +0530 | [diff] [blame] | 879 | |
Preetam Singh Ranawat | 43eac68 | 2017-03-07 18:19:02 +0530 | [diff] [blame] | 880 | list_for_each(node, &adev->usecase_list) { |
| 881 | uc = node_to_item(node, struct audio_usecase, list); |
| 882 | curr_out = (struct stream_out*) uc->stream.out; |
| 883 | if (curr_out && PCM_PLAYBACK == uc->type && uc != uc_info) { |
| 884 | /* |
| 885 | *Split snd device for existing combo use case |
| 886 | *e.g. Ringtone (Headphone + Speaker) + Headphopne 44.1 |
| 887 | */ |
| 888 | ret = platform_split_snd_device(adev->platform, |
| 889 | uc->out_snd_device, |
| 890 | &num_devices, |
| 891 | split_snd_devices); |
| 892 | if (ret < 0 || num_devices == 0) { |
| 893 | ALOGV("%s: Unable to split uc->out_snd_device: %d",__func__, uc->out_snd_device); |
| 894 | split_snd_devices[0] = uc->out_snd_device; |
| 895 | num_devices = 1; |
Garmond Leung | 50058f6 | 2017-02-08 09:49:30 -0800 | [diff] [blame] | 896 | } |
Preetam Singh Ranawat | 43eac68 | 2017-03-07 18:19:02 +0530 | [diff] [blame] | 897 | for (i = 0; i < num_devices; i++) { |
| 898 | usecase_backend_idx = platform_get_backend_index(split_snd_devices[i]); |
| 899 | ALOGD("%s:snd_dev %d usecase_backend_idx %d",__func__, split_snd_devices[i],usecase_backend_idx); |
| 900 | if((new_backend_idx == HEADPHONE_BACKEND) && |
| 901 | ((usecase_backend_idx == HEADPHONE_44_1_BACKEND) || |
| 902 | (usecase_backend_idx == DSD_NATIVE_BACKEND))) { |
| 903 | ALOGD("%s:DSD or native stream detected enabling asrcmode in hardware", |
| 904 | __func__); |
| 905 | enable_asrc_mode(adev); |
| 906 | break; |
| 907 | } else if(((new_backend_idx == HEADPHONE_44_1_BACKEND) || |
| 908 | (new_backend_idx == DSD_NATIVE_BACKEND)) && |
| 909 | (usecase_backend_idx == HEADPHONE_BACKEND)) { |
| 910 | ALOGD("%s:48K stream detected, disabling and enabling it with asrcmode in hardware", |
| 911 | __func__); |
| 912 | disable_audio_route(adev, uc); |
| 913 | disable_snd_device(adev, uc->out_snd_device); |
| 914 | // Apply true-high-quality-mode if DSD or > 44.1KHz or >=24-bit |
| 915 | if (new_backend_idx == DSD_NATIVE_BACKEND) |
| 916 | audio_route_apply_and_update_path(adev->audio_route, |
| 917 | "hph-true-highquality-mode"); |
| 918 | else if ((new_backend_idx == HEADPHONE_44_1_BACKEND) && |
| 919 | (curr_out->bit_width >= 24)) |
| 920 | audio_route_apply_and_update_path(adev->audio_route, |
| 921 | "hph-highquality-mode"); |
| 922 | enable_asrc_mode(adev); |
| 923 | enable_snd_device(adev, uc->out_snd_device); |
| 924 | enable_audio_route(adev, uc); |
| 925 | break; |
| 926 | } |
| 927 | } |
| 928 | // reset split devices count |
| 929 | num_devices = 0; |
Garmond Leung | 50058f6 | 2017-02-08 09:49:30 -0800 | [diff] [blame] | 930 | } |
Preetam Singh Ranawat | 43eac68 | 2017-03-07 18:19:02 +0530 | [diff] [blame] | 931 | if (adev->asrc_mode_enabled) |
| 932 | break; |
Preetam Singh Ranawat | b0c0dd7 | 2016-08-18 00:32:06 +0530 | [diff] [blame] | 933 | } |
| 934 | } |
| 935 | } |
| 936 | } |
| 937 | |
Vikram Panduranga | df59cae | 2017-08-03 18:04:55 -0700 | [diff] [blame] | 938 | static int send_effect_enable_disable_mixer_ctl(struct audio_device *adev, |
| 939 | struct audio_effect_config effect_config, |
| 940 | unsigned int param_value) |
| 941 | { |
| 942 | char mixer_ctl_name[] = "Audio Effect"; |
| 943 | struct mixer_ctl *ctl; |
| 944 | long set_values[6]; |
Eric Laurent | 637e2d4 | 2018-11-15 12:24:31 -0800 | [diff] [blame] | 945 | struct stream_in *in = adev_get_active_input(adev); |
Vikram Panduranga | df59cae | 2017-08-03 18:04:55 -0700 | [diff] [blame] | 946 | |
Aniket Kumar Lata | 7de6352 | 2019-06-13 11:05:18 -0700 | [diff] [blame] | 947 | if (in == NULL) { |
| 948 | ALOGE("%s: active input stream is NULL", __func__); |
| 949 | return -EINVAL; |
| 950 | } |
| 951 | |
Vikram Panduranga | df59cae | 2017-08-03 18:04:55 -0700 | [diff] [blame] | 952 | ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name); |
| 953 | if (!ctl) { |
| 954 | ALOGE("%s: Could not get mixer ctl - %s", |
| 955 | __func__, mixer_ctl_name); |
| 956 | return -EINVAL; |
| 957 | } |
| 958 | |
| 959 | set_values[0] = 1; //0:Rx 1:Tx |
| 960 | set_values[1] = in->app_type_cfg.app_type; |
| 961 | set_values[2] = (long)effect_config.module_id; |
| 962 | set_values[3] = (long)effect_config.instance_id; |
| 963 | set_values[4] = (long)effect_config.param_id; |
| 964 | set_values[5] = param_value; |
| 965 | |
| 966 | mixer_ctl_set_array(ctl, set_values, ARRAY_SIZE(set_values)); |
| 967 | |
| 968 | return 0; |
| 969 | |
| 970 | } |
| 971 | |
| 972 | static int update_effect_param_ecns(struct audio_device *adev, unsigned int module_id, |
| 973 | int effect_type, unsigned int *param_value) |
| 974 | { |
| 975 | int ret = 0; |
| 976 | struct audio_effect_config other_effect_config; |
| 977 | struct audio_usecase *usecase = NULL; |
Eric Laurent | 637e2d4 | 2018-11-15 12:24:31 -0800 | [diff] [blame] | 978 | struct stream_in *in = adev_get_active_input(adev); |
Vikram Panduranga | df59cae | 2017-08-03 18:04:55 -0700 | [diff] [blame] | 979 | |
Aniket Kumar Lata | 7de6352 | 2019-06-13 11:05:18 -0700 | [diff] [blame] | 980 | if (in == NULL) { |
| 981 | ALOGE("%s: active input stream is NULL", __func__); |
| 982 | return -EINVAL; |
| 983 | } |
| 984 | |
Vikram Panduranga | df59cae | 2017-08-03 18:04:55 -0700 | [diff] [blame] | 985 | usecase = get_usecase_from_list(adev, in->usecase); |
| 986 | if (!usecase) |
| 987 | return -EINVAL; |
| 988 | |
| 989 | ret = platform_get_effect_config_data(usecase->in_snd_device, &other_effect_config, |
| 990 | effect_type == EFFECT_AEC ? EFFECT_NS : EFFECT_AEC); |
| 991 | if (ret < 0) { |
| 992 | ALOGE("%s Failed to get effect params %d", __func__, ret); |
| 993 | return ret; |
| 994 | } |
| 995 | |
| 996 | if (module_id == other_effect_config.module_id) { |
| 997 | //Same module id for AEC/NS. Values need to be combined |
| 998 | if (((effect_type == EFFECT_AEC) && (in->enable_ns)) || |
| 999 | ((effect_type == EFFECT_NS) && (in->enable_aec))) { |
| 1000 | *param_value |= other_effect_config.param_value; |
| 1001 | } |
| 1002 | } |
| 1003 | |
| 1004 | return ret; |
| 1005 | } |
| 1006 | |
| 1007 | static int enable_disable_effect(struct audio_device *adev, int effect_type, bool enable) |
Gangadhar S | b021034 | 2019-02-22 17:39:41 +0530 | [diff] [blame] | 1008 | { |
Vikram Panduranga | df59cae | 2017-08-03 18:04:55 -0700 | [diff] [blame] | 1009 | struct audio_effect_config effect_config; |
| 1010 | struct audio_usecase *usecase = NULL; |
| 1011 | int ret = 0; |
| 1012 | unsigned int param_value = 0; |
Eric Laurent | 637e2d4 | 2018-11-15 12:24:31 -0800 | [diff] [blame] | 1013 | struct stream_in *in = adev_get_active_input(adev); |
Vikram Panduranga | df59cae | 2017-08-03 18:04:55 -0700 | [diff] [blame] | 1014 | |
Arun Mirpuri | b1bec9c | 2019-01-29 16:42:45 -0800 | [diff] [blame] | 1015 | if(!voice_extn_is_dynamic_ecns_enabled()) |
| 1016 | return ENOSYS; |
| 1017 | |
Vikram Panduranga | df59cae | 2017-08-03 18:04:55 -0700 | [diff] [blame] | 1018 | if (!in) { |
| 1019 | ALOGE("%s: Invalid input stream", __func__); |
| 1020 | return -EINVAL; |
| 1021 | } |
| 1022 | |
| 1023 | ALOGD("%s: effect_type:%d enable:%d", __func__, effect_type, enable); |
| 1024 | |
| 1025 | usecase = get_usecase_from_list(adev, in->usecase); |
Weiyin Jiang | 20d3fa6 | 2018-08-01 18:06:27 +0800 | [diff] [blame] | 1026 | if (usecase == NULL) { |
| 1027 | ALOGE("%s: Could not find the usecase (%d) in the list", |
| 1028 | __func__, in->usecase); |
| 1029 | return -EINVAL; |
| 1030 | } |
Vikram Panduranga | df59cae | 2017-08-03 18:04:55 -0700 | [diff] [blame] | 1031 | |
| 1032 | ret = platform_get_effect_config_data(usecase->in_snd_device, &effect_config, effect_type); |
| 1033 | if (ret < 0) { |
| 1034 | ALOGE("%s Failed to get module id %d", __func__, ret); |
| 1035 | return ret; |
| 1036 | } |
| 1037 | ALOGV("%s: %d %d usecase->id:%d usecase->in_snd_device:%d", __func__, effect_config.module_id, |
| 1038 | in->app_type_cfg.app_type, usecase->id, usecase->in_snd_device); |
| 1039 | |
| 1040 | if(enable) |
| 1041 | param_value = effect_config.param_value; |
| 1042 | |
| 1043 | /*Special handling for AEC & NS effects Param values need to be |
| 1044 | updated if module ids are same*/ |
| 1045 | |
| 1046 | if ((effect_type == EFFECT_AEC) || (effect_type == EFFECT_NS)) { |
| 1047 | ret = update_effect_param_ecns(adev, effect_config.module_id, effect_type, ¶m_value); |
| 1048 | if (ret < 0) |
| 1049 | return ret; |
| 1050 | } |
| 1051 | |
| 1052 | ret = send_effect_enable_disable_mixer_ctl(adev, effect_config, param_value); |
| 1053 | |
| 1054 | return ret; |
| 1055 | } |
| 1056 | |
| 1057 | static void check_and_enable_effect(struct audio_device *adev) |
| 1058 | { |
Arun Mirpuri | b1bec9c | 2019-01-29 16:42:45 -0800 | [diff] [blame] | 1059 | if(!voice_extn_is_dynamic_ecns_enabled()) |
| 1060 | return; |
Vikram Panduranga | df59cae | 2017-08-03 18:04:55 -0700 | [diff] [blame] | 1061 | |
Eric Laurent | 637e2d4 | 2018-11-15 12:24:31 -0800 | [diff] [blame] | 1062 | struct stream_in *in = adev_get_active_input(adev); |
Vikram Panduranga | df59cae | 2017-08-03 18:04:55 -0700 | [diff] [blame] | 1063 | |
Eric Laurent | 637e2d4 | 2018-11-15 12:24:31 -0800 | [diff] [blame] | 1064 | if (in != NULL && !in->standby) { |
| 1065 | if (in->enable_aec) |
| 1066 | enable_disable_effect(adev, EFFECT_AEC, true); |
| 1067 | |
| 1068 | if (in->enable_ns && |
| 1069 | in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) { |
| 1070 | enable_disable_effect(adev, EFFECT_NS, true); |
| 1071 | } |
Vikram Panduranga | df59cae | 2017-08-03 18:04:55 -0700 | [diff] [blame] | 1072 | } |
| 1073 | } |
Vikram Panduranga | df59cae | 2017-08-03 18:04:55 -0700 | [diff] [blame] | 1074 | |
Ravi Kumar Alamanda | dc9bb15 | 2014-09-08 15:59:58 -0700 | [diff] [blame] | 1075 | int pcm_ioctl(struct pcm *pcm, int request, ...) |
| 1076 | { |
| 1077 | va_list ap; |
| 1078 | void * arg; |
| 1079 | int pcm_fd = *(int*)pcm; |
| 1080 | |
| 1081 | va_start(ap, request); |
| 1082 | arg = va_arg(ap, void *); |
| 1083 | va_end(ap); |
| 1084 | |
| 1085 | return ioctl(pcm_fd, request, arg); |
| 1086 | } |
| 1087 | |
Gopikrishnaiah Anandan | f538cef | 2013-10-28 14:06:03 -0700 | [diff] [blame] | 1088 | int enable_audio_route(struct audio_device *adev, |
Haynes Mathew George | 1376ca6 | 2014-04-24 11:55:48 -0700 | [diff] [blame] | 1089 | struct audio_usecase *usecase) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1090 | { |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1091 | snd_device_t snd_device; |
Apoorv Raghuvanshi | 6e26284 | 2013-10-06 14:39:35 -0700 | [diff] [blame] | 1092 | char mixer_path[MIXER_PATH_MAX_LENGTH]; |
Manish Dewangan | 5822938 | 2017-02-02 15:48:41 +0530 | [diff] [blame] | 1093 | struct stream_out *out = NULL; |
Chaithanya Krishna Bacharaju | c9f9971 | 2019-04-16 15:32:52 +0530 | [diff] [blame] | 1094 | struct stream_in *in = NULL; |
Soumya Managoli | 6993b76 | 2018-06-28 16:04:57 +0530 | [diff] [blame] | 1095 | int ret = 0; |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 1096 | |
| 1097 | if (usecase == NULL) |
| 1098 | return -EINVAL; |
| 1099 | |
| 1100 | ALOGV("%s: enter: usecase(%d)", __func__, usecase->id); |
| 1101 | |
Carter Hsu | 2e429db | 2019-05-14 18:50:52 +0800 | [diff] [blame] | 1102 | if (usecase->type == PCM_CAPTURE) { |
| 1103 | struct stream_in *in = usecase->stream.in; |
| 1104 | struct audio_usecase *uinfo; |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1105 | snd_device = usecase->in_snd_device; |
Carter Hsu | 2e429db | 2019-05-14 18:50:52 +0800 | [diff] [blame] | 1106 | |
| 1107 | if (in) { |
| 1108 | if (in->enable_aec || in->enable_ec_port) { |
| 1109 | audio_devices_t out_device = AUDIO_DEVICE_OUT_SPEAKER; |
| 1110 | struct listnode *node; |
| 1111 | struct audio_usecase *voip_usecase = get_usecase_from_list(adev, |
| 1112 | USECASE_AUDIO_PLAYBACK_VOIP); |
| 1113 | if (voip_usecase) { |
| 1114 | out_device = voip_usecase->stream.out->devices; |
| 1115 | } else if (adev->primary_output && |
| 1116 | !adev->primary_output->standby) { |
| 1117 | out_device = adev->primary_output->devices; |
| 1118 | } else { |
| 1119 | list_for_each(node, &adev->usecase_list) { |
| 1120 | uinfo = node_to_item(node, struct audio_usecase, list); |
| 1121 | if (uinfo->type != PCM_CAPTURE) { |
| 1122 | out_device = uinfo->stream.out->devices; |
| 1123 | break; |
| 1124 | } |
| 1125 | } |
| 1126 | } |
| 1127 | platform_set_echo_reference(adev, true, out_device); |
| 1128 | in->ec_opened = true; |
| 1129 | } |
| 1130 | } |
Guodong Hu | f5e614d | 2019-06-24 18:42:03 +0800 | [diff] [blame^] | 1131 | } else if ((usecase->type == TRANSCODE_LOOPBACK_TX) || ((usecase->type == PCM_HFP_CALL) && |
| 1132 | ((usecase->id == USECASE_AUDIO_HFP_SCO) || (usecase->id == USECASE_AUDIO_HFP_SCO_WB)) && |
| 1133 | (usecase->in_snd_device == SND_DEVICE_IN_VOICE_SPEAKER_MIC_HFP_MMSECNS))) { |
Carter Hsu | 2e429db | 2019-05-14 18:50:52 +0800 | [diff] [blame] | 1134 | snd_device = usecase->in_snd_device; |
| 1135 | } else { |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1136 | snd_device = usecase->out_snd_device; |
Carter Hsu | 2e429db | 2019-05-14 18:50:52 +0800 | [diff] [blame] | 1137 | } |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 1138 | |
Subhash Chandra Bose Naripeddy | 7690c56 | 2013-12-14 00:34:53 -0800 | [diff] [blame] | 1139 | #ifdef DS1_DOLBY_DAP_ENABLED |
| 1140 | audio_extn_dolby_set_dmid(adev); |
| 1141 | audio_extn_dolby_set_endpoint(adev); |
| 1142 | #endif |
Pradnya Chaphekar | 8a9dcd8 | 2014-09-09 09:49:10 -0700 | [diff] [blame] | 1143 | audio_extn_dolby_ds2_set_endpoint(adev); |
Ravi Kumar Alamanda | 8fa6b19 | 2014-09-09 16:06:42 -0700 | [diff] [blame] | 1144 | audio_extn_sound_trigger_update_stream_status(usecase, ST_EVENT_STREAM_BUSY); |
Dhananjay Kumar | 45b7174 | 2014-05-29 21:47:27 +0530 | [diff] [blame] | 1145 | audio_extn_listen_update_stream_status(usecase, LISTEN_EVENT_STREAM_BUSY); |
Ben Romberger | 1fafdde | 2015-09-09 19:43:15 -0700 | [diff] [blame] | 1146 | audio_extn_utils_send_app_type_cfg(adev, usecase); |
Jasmine Cha | 4dcc109 | 2019-03-04 18:12:47 +0800 | [diff] [blame] | 1147 | if (audio_extn_is_maxx_audio_enabled()) |
| 1148 | audio_extn_ma_set_device(usecase); |
Dhananjay Kumar | 14170dd | 2015-08-28 13:24:16 +0530 | [diff] [blame] | 1149 | audio_extn_utils_send_audio_calibration(adev, usecase); |
Manish Dewangan | 5822938 | 2017-02-02 15:48:41 +0530 | [diff] [blame] | 1150 | if ((usecase->type == PCM_PLAYBACK) && is_offload_usecase(usecase->id)) { |
| 1151 | out = usecase->stream.out; |
| 1152 | if (out && out->compr) |
| 1153 | audio_extn_utils_compress_set_clk_rec_mode(usecase); |
| 1154 | } |
Chaithanya Krishna Bacharaju | c9f9971 | 2019-04-16 15:32:52 +0530 | [diff] [blame] | 1155 | |
| 1156 | if (usecase->type == PCM_CAPTURE) { |
| 1157 | in = usecase->stream.in; |
| 1158 | if (in && is_loopback_input_device(in->device)) { |
| 1159 | ALOGD("%s: set custom mtmx params v1", __func__); |
| 1160 | audio_extn_set_custom_mtmx_params_v1(adev, usecase, true); |
| 1161 | } |
| 1162 | } else { |
| 1163 | audio_extn_set_custom_mtmx_params_v2(adev, usecase, true); |
| 1164 | } |
Manish Dewangan | 5822938 | 2017-02-02 15:48:41 +0530 | [diff] [blame] | 1165 | |
Andy Hung | 756ecc1 | 2018-10-19 17:47:12 -0700 | [diff] [blame] | 1166 | // we shouldn't truncate mixer_path |
| 1167 | ALOGW_IF(strlcpy(mixer_path, use_case_table[usecase->id], sizeof(mixer_path)) |
| 1168 | >= sizeof(mixer_path), "%s: truncation on mixer path", __func__); |
| 1169 | // this also appends to mixer_path |
Banajit Goswami | 20cdd21 | 2015-09-11 01:11:30 -0700 | [diff] [blame] | 1170 | platform_add_backend_name(mixer_path, snd_device, usecase); |
Ravi Kumar Alamanda | c7d9bef | 2015-09-30 22:27:26 -0700 | [diff] [blame] | 1171 | ALOGD("%s: apply mixer and update path: %s", __func__, mixer_path); |
Soumya Managoli | 6993b76 | 2018-06-28 16:04:57 +0530 | [diff] [blame] | 1172 | ret = audio_route_apply_and_update_path(adev->audio_route, mixer_path); |
| 1173 | if (!ret && usecase->id == USECASE_AUDIO_PLAYBACK_FM) { |
| 1174 | struct str_parms *parms = str_parms_create_str("fm_restore_volume=1"); |
| 1175 | if (parms) { |
| 1176 | audio_extn_fm_set_parameters(adev, parms); |
| 1177 | str_parms_destroy(parms); |
| 1178 | } |
| 1179 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1180 | ALOGV("%s: exit", __func__); |
| 1181 | return 0; |
| 1182 | } |
| 1183 | |
Apoorv Raghuvanshi | 6e26284 | 2013-10-06 14:39:35 -0700 | [diff] [blame] | 1184 | int disable_audio_route(struct audio_device *adev, |
Haynes Mathew George | 1376ca6 | 2014-04-24 11:55:48 -0700 | [diff] [blame] | 1185 | struct audio_usecase *usecase) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1186 | { |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1187 | snd_device_t snd_device; |
Apoorv Raghuvanshi | 6e26284 | 2013-10-06 14:39:35 -0700 | [diff] [blame] | 1188 | char mixer_path[MIXER_PATH_MAX_LENGTH]; |
Chaithanya Krishna Bacharaju | c9f9971 | 2019-04-16 15:32:52 +0530 | [diff] [blame] | 1189 | struct stream_in *in = NULL; |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 1190 | |
Preetam Singh Ranawat | a5f32b4 | 2014-09-23 12:12:47 +0530 | [diff] [blame] | 1191 | if (usecase == NULL || usecase->id == USECASE_INVALID) |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 1192 | return -EINVAL; |
| 1193 | |
| 1194 | ALOGV("%s: enter: usecase(%d)", __func__, usecase->id); |
Surendar Karka | 93cd25a | 2018-08-28 14:21:37 +0530 | [diff] [blame] | 1195 | if (usecase->type == PCM_CAPTURE || usecase->type == TRANSCODE_LOOPBACK_TX) |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1196 | snd_device = usecase->in_snd_device; |
| 1197 | else |
| 1198 | snd_device = usecase->out_snd_device; |
Andy Hung | 756ecc1 | 2018-10-19 17:47:12 -0700 | [diff] [blame] | 1199 | // we shouldn't truncate mixer_path |
| 1200 | ALOGW_IF(strlcpy(mixer_path, use_case_table[usecase->id], sizeof(mixer_path)) |
| 1201 | >= sizeof(mixer_path), "%s: truncation on mixer path", __func__); |
| 1202 | // this also appends to mixer_path |
Banajit Goswami | 20cdd21 | 2015-09-11 01:11:30 -0700 | [diff] [blame] | 1203 | platform_add_backend_name(mixer_path, snd_device, usecase); |
Ravi Kumar Alamanda | c7d9bef | 2015-09-30 22:27:26 -0700 | [diff] [blame] | 1204 | ALOGD("%s: reset and update mixer path: %s", __func__, mixer_path); |
Haynes Mathew George | ef1e3d3 | 2014-04-24 11:53:44 -0700 | [diff] [blame] | 1205 | audio_route_reset_and_update_path(adev->audio_route, mixer_path); |
Carter Hsu | 2e429db | 2019-05-14 18:50:52 +0800 | [diff] [blame] | 1206 | if (usecase->type == PCM_CAPTURE) { |
| 1207 | struct stream_in *in = usecase->stream.in; |
| 1208 | if (in && in->ec_opened) { |
| 1209 | platform_set_echo_reference(in->dev, false, AUDIO_DEVICE_NONE); |
| 1210 | in->ec_opened = false; |
| 1211 | } |
| 1212 | } |
Ravi Kumar Alamanda | 8fa6b19 | 2014-09-09 16:06:42 -0700 | [diff] [blame] | 1213 | audio_extn_sound_trigger_update_stream_status(usecase, ST_EVENT_STREAM_FREE); |
Dhananjay Kumar | 45b7174 | 2014-05-29 21:47:27 +0530 | [diff] [blame] | 1214 | audio_extn_listen_update_stream_status(usecase, LISTEN_EVENT_STREAM_FREE); |
Chaithanya Krishna Bacharaju | c9f9971 | 2019-04-16 15:32:52 +0530 | [diff] [blame] | 1215 | |
| 1216 | if (usecase->type == PCM_CAPTURE) { |
| 1217 | in = usecase->stream.in; |
| 1218 | if (in && is_loopback_input_device(in->device)) { |
| 1219 | ALOGD("%s: reset custom mtmx params v1", __func__); |
| 1220 | audio_extn_set_custom_mtmx_params_v1(adev, usecase, false); |
| 1221 | } |
| 1222 | } else { |
| 1223 | audio_extn_set_custom_mtmx_params_v2(adev, usecase, false); |
| 1224 | } |
| 1225 | |
Weiyin Jiang | 298ffd9 | 2019-06-03 14:29:30 +0800 | [diff] [blame] | 1226 | if ((usecase->type == PCM_PLAYBACK) && |
| 1227 | (usecase->stream.out != NULL)) |
Dhanalakshmi Siddani | 20628c8 | 2019-01-27 17:31:09 +0530 | [diff] [blame] | 1228 | usecase->stream.out->pspd_coeff_sent = false; |
Chaithanya Krishna Bacharaju | c9f9971 | 2019-04-16 15:32:52 +0530 | [diff] [blame] | 1229 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1230 | ALOGV("%s: exit", __func__); |
| 1231 | return 0; |
| 1232 | } |
| 1233 | |
Gopikrishnaiah Anandan | f538cef | 2013-10-28 14:06:03 -0700 | [diff] [blame] | 1234 | int enable_snd_device(struct audio_device *adev, |
Haynes Mathew George | 1376ca6 | 2014-04-24 11:55:48 -0700 | [diff] [blame] | 1235 | snd_device_t snd_device) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1236 | { |
Sidipotu Ashok | 9f0b16e | 2016-04-28 13:48:28 +0530 | [diff] [blame] | 1237 | int i, num_devices = 0; |
| 1238 | snd_device_t new_snd_devices[SND_DEVICE_OUT_END]; |
Ravi Kumar Alamanda | 48c921d | 2013-10-29 06:07:44 -0700 | [diff] [blame] | 1239 | char device_name[DEVICE_NAME_MAX_SIZE] = {0}; |
| 1240 | |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 1241 | if (snd_device < SND_DEVICE_MIN || |
| 1242 | snd_device >= SND_DEVICE_MAX) { |
Ravi Kumar Alamanda | 3b1816c | 2013-02-27 23:01:21 -0800 | [diff] [blame] | 1243 | ALOGE("%s: Invalid sound device %d", __func__, snd_device); |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 1244 | return -EINVAL; |
| 1245 | } |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1246 | |
Weiyin Jiang | 0d84c8e | 2019-04-09 22:59:54 +0800 | [diff] [blame] | 1247 | if (platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0) { |
Ravi Kumar Alamanda | 48c921d | 2013-10-29 06:07:44 -0700 | [diff] [blame] | 1248 | ALOGE("%s: Invalid sound device returned", __func__); |
| 1249 | return -EINVAL; |
| 1250 | } |
Weiyin Jiang | 0d84c8e | 2019-04-09 22:59:54 +0800 | [diff] [blame] | 1251 | |
| 1252 | adev->snd_dev_ref_cnt[snd_device]++; |
| 1253 | |
| 1254 | if ((adev->snd_dev_ref_cnt[snd_device] > 1) && |
| 1255 | (platform_split_snd_device(adev->platform, |
| 1256 | snd_device, |
| 1257 | &num_devices, |
| 1258 | new_snd_devices) != 0)) { |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 1259 | ALOGV("%s: snd_device(%d: %s) is already active", |
Ravi Kumar Alamanda | 48c921d | 2013-10-29 06:07:44 -0700 | [diff] [blame] | 1260 | __func__, snd_device, device_name); |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1261 | return 0; |
| 1262 | } |
| 1263 | |
Gopikrishnaiah Anandan | e85d046 | 2014-06-30 21:41:20 -0700 | [diff] [blame] | 1264 | if (audio_extn_spkr_prot_is_enabled()) |
| 1265 | audio_extn_spkr_prot_calib_cancel(adev); |
Apoorv Raghuvanshi | 5792d4b | 2013-10-07 18:40:05 -0700 | [diff] [blame] | 1266 | |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 1267 | audio_extn_dsm_feedback_enable(adev, snd_device, true); |
| 1268 | |
Xiaojun Sang | 040cc9f | 2015-08-03 19:38:28 +0800 | [diff] [blame] | 1269 | if (platform_can_enable_spkr_prot_on_device(snd_device) && |
| 1270 | audio_extn_spkr_prot_is_enabled()) { |
Weiyin Jiang | 0d84c8e | 2019-04-09 22:59:54 +0800 | [diff] [blame] | 1271 | if (platform_get_spkr_prot_acdb_id(snd_device) < 0) { |
| 1272 | goto err; |
| 1273 | } |
| 1274 | audio_extn_dev_arbi_acquire(snd_device); |
| 1275 | if (audio_extn_spkr_prot_start_processing(snd_device)) { |
Vidyakumar Athota | 1c6419a | 2014-01-10 14:47:34 -0800 | [diff] [blame] | 1276 | ALOGE("%s: spkr_start_processing failed", __func__); |
Ravit Dennis | aaee49c | 2015-02-04 21:26:22 +0200 | [diff] [blame] | 1277 | audio_extn_dev_arbi_release(snd_device); |
Weiyin Jiang | 0d84c8e | 2019-04-09 22:59:54 +0800 | [diff] [blame] | 1278 | goto err; |
Vidyakumar Athota | 1c6419a | 2014-01-10 14:47:34 -0800 | [diff] [blame] | 1279 | } |
Haynes Mathew George | bfe8ff4 | 2016-09-22 17:38:16 -0700 | [diff] [blame] | 1280 | } else if (platform_split_snd_device(adev->platform, |
| 1281 | snd_device, |
| 1282 | &num_devices, |
| 1283 | new_snd_devices) == 0) { |
Sidipotu Ashok | 9f0b16e | 2016-04-28 13:48:28 +0530 | [diff] [blame] | 1284 | for (i = 0; i < num_devices; i++) { |
| 1285 | enable_snd_device(adev, new_snd_devices[i]); |
| 1286 | } |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 1287 | platform_set_speaker_gain_in_combo(adev, snd_device, true); |
Vidyakumar Athota | 1c6419a | 2014-01-10 14:47:34 -0800 | [diff] [blame] | 1288 | } else { |
Ravi Kumar Alamanda | c7d9bef | 2015-09-30 22:27:26 -0700 | [diff] [blame] | 1289 | ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, device_name); |
Naresh Tanniru | cd2353e | 2016-08-19 00:37:25 +0530 | [diff] [blame] | 1290 | |
Preetam Singh Ranawat | f1d417c | 2017-01-10 17:00:32 +0530 | [diff] [blame] | 1291 | |
Weiyin Jiang | 0d84c8e | 2019-04-09 22:59:54 +0800 | [diff] [blame] | 1292 | if ((SND_DEVICE_OUT_BT_A2DP == snd_device) && |
| 1293 | (audio_extn_a2dp_start_playback() < 0)) { |
| 1294 | ALOGE(" fail to configure A2dp Source control path "); |
| 1295 | goto err; |
| 1296 | } |
Florian Pfister | 1a84f31 | 2018-07-19 14:38:18 +0200 | [diff] [blame] | 1297 | |
Weiyin Jiang | 0d84c8e | 2019-04-09 22:59:54 +0800 | [diff] [blame] | 1298 | if ((SND_DEVICE_IN_BT_A2DP == snd_device) && |
| 1299 | (audio_extn_a2dp_start_capture() < 0)) { |
| 1300 | ALOGE(" fail to configure A2dp Sink control path "); |
| 1301 | goto err; |
| 1302 | } |
Naresh Tanniru | cd2353e | 2016-08-19 00:37:25 +0530 | [diff] [blame] | 1303 | |
Zhou Song | 12c2950 | 2019-03-16 10:37:18 +0800 | [diff] [blame] | 1304 | if (((SND_DEVICE_OUT_BT_SCO_SWB == snd_device) || |
| 1305 | (SND_DEVICE_IN_BT_SCO_MIC_SWB_NREC == snd_device) || |
| 1306 | (SND_DEVICE_IN_BT_SCO_MIC_SWB == snd_device)) && |
| 1307 | (audio_extn_sco_start_configuration() < 0)) { |
| 1308 | ALOGE(" fail to configure sco control path "); |
| 1309 | goto err; |
| 1310 | } |
| 1311 | |
Zhou Song | 331c8e5 | 2019-08-26 14:16:12 +0800 | [diff] [blame] | 1312 | configure_btsco_sample_rate(snd_device); |
Bharath Ramachandramurthy | 0de1678 | 2014-03-28 21:34:33 -0700 | [diff] [blame] | 1313 | /* due to the possibility of calibration overwrite between listen |
| 1314 | and audio, notify listen hal before audio calibration is sent */ |
Ravi Kumar Alamanda | 8fa6b19 | 2014-09-09 16:06:42 -0700 | [diff] [blame] | 1315 | audio_extn_sound_trigger_update_device_status(snd_device, |
| 1316 | ST_EVENT_SND_DEVICE_BUSY); |
Dhananjay Kumar | 45b7174 | 2014-05-29 21:47:27 +0530 | [diff] [blame] | 1317 | audio_extn_listen_update_device_status(snd_device, |
| 1318 | LISTEN_EVENT_SND_DEVICE_BUSY); |
Subhash Chandra Bose Naripeddy | 5427467 | 2014-03-10 14:51:02 -0700 | [diff] [blame] | 1319 | if (platform_get_snd_device_acdb_id(snd_device) < 0) { |
Ravi Kumar Alamanda | 8fa6b19 | 2014-09-09 16:06:42 -0700 | [diff] [blame] | 1320 | audio_extn_sound_trigger_update_device_status(snd_device, |
| 1321 | ST_EVENT_SND_DEVICE_FREE); |
Dhananjay Kumar | 45b7174 | 2014-05-29 21:47:27 +0530 | [diff] [blame] | 1322 | audio_extn_listen_update_device_status(snd_device, |
| 1323 | LISTEN_EVENT_SND_DEVICE_FREE); |
Weiyin Jiang | 0d84c8e | 2019-04-09 22:59:54 +0800 | [diff] [blame] | 1324 | goto err; |
Gopikrishnaiah Anandan | f538cef | 2013-10-28 14:06:03 -0700 | [diff] [blame] | 1325 | } |
Lior Barenboim | 0b61bc7 | 2014-05-13 13:01:37 +0300 | [diff] [blame] | 1326 | audio_extn_dev_arbi_acquire(snd_device); |
Haynes Mathew George | ef1e3d3 | 2014-04-24 11:53:44 -0700 | [diff] [blame] | 1327 | audio_route_apply_and_update_path(adev->audio_route, device_name); |
Sidipotu Ashok | e6f78cb | 2015-11-05 14:42:20 +0530 | [diff] [blame] | 1328 | |
| 1329 | if (SND_DEVICE_OUT_HEADPHONES == snd_device && |
| 1330 | !adev->native_playback_enabled && |
| 1331 | audio_is_true_native_stream_active(adev)) { |
| 1332 | ALOGD("%s: %d: napb: enabling native mode in hardware", |
| 1333 | __func__, __LINE__); |
| 1334 | audio_route_apply_and_update_path(adev->audio_route, |
| 1335 | "true-native-mode"); |
| 1336 | adev->native_playback_enabled = true; |
Preetam Singh Ranawat | 6a83666 | 2016-09-08 17:04:35 +0530 | [diff] [blame] | 1337 | } |
Dhanalakshmi Siddani | 1062162 | 2018-04-30 15:07:27 +0530 | [diff] [blame] | 1338 | if (((snd_device == SND_DEVICE_IN_HANDSET_6MIC) || |
| 1339 | (snd_device == SND_DEVICE_IN_HANDSET_QMIC)) && |
Eric Laurent | 637e2d4 | 2018-11-15 12:24:31 -0800 | [diff] [blame] | 1340 | (audio_extn_ffv_get_stream() == adev_get_active_input(adev))) { |
Garmond Leung | e2433c3 | 2017-09-28 21:51:22 -0700 | [diff] [blame] | 1341 | ALOGD("%s: init ec ref loopback", __func__); |
| 1342 | audio_extn_ffv_init_ec_ref_loopback(adev, snd_device); |
| 1343 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1344 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1345 | return 0; |
Weiyin Jiang | 0d84c8e | 2019-04-09 22:59:54 +0800 | [diff] [blame] | 1346 | err: |
| 1347 | adev->snd_dev_ref_cnt[snd_device]--; |
| 1348 | return -EINVAL;; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1349 | } |
| 1350 | |
Apoorv Raghuvanshi | 6e26284 | 2013-10-06 14:39:35 -0700 | [diff] [blame] | 1351 | int disable_snd_device(struct audio_device *adev, |
Haynes Mathew George | 1376ca6 | 2014-04-24 11:55:48 -0700 | [diff] [blame] | 1352 | snd_device_t snd_device) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1353 | { |
Sidipotu Ashok | 9f0b16e | 2016-04-28 13:48:28 +0530 | [diff] [blame] | 1354 | int i, num_devices = 0; |
| 1355 | snd_device_t new_snd_devices[SND_DEVICE_OUT_END]; |
Ravi Kumar Alamanda | 48c921d | 2013-10-29 06:07:44 -0700 | [diff] [blame] | 1356 | char device_name[DEVICE_NAME_MAX_SIZE] = {0}; |
| 1357 | |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 1358 | if (snd_device < SND_DEVICE_MIN || |
| 1359 | snd_device >= SND_DEVICE_MAX) { |
Ravi Kumar Alamanda | 3b1816c | 2013-02-27 23:01:21 -0800 | [diff] [blame] | 1360 | ALOGE("%s: Invalid sound device %d", __func__, snd_device); |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 1361 | return -EINVAL; |
| 1362 | } |
Weiyin Jiang | 0d84c8e | 2019-04-09 22:59:54 +0800 | [diff] [blame] | 1363 | |
| 1364 | if (platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0) { |
| 1365 | ALOGE("%s: Invalid sound device returned", __func__); |
| 1366 | return -EINVAL; |
| 1367 | } |
| 1368 | |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1369 | if (adev->snd_dev_ref_cnt[snd_device] <= 0) { |
| 1370 | ALOGE("%s: device ref cnt is already 0", __func__); |
| 1371 | return -EINVAL; |
| 1372 | } |
Ravi Kumar Alamanda | 48c921d | 2013-10-29 06:07:44 -0700 | [diff] [blame] | 1373 | |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1374 | adev->snd_dev_ref_cnt[snd_device]--; |
Apoorv Raghuvanshi | 5792d4b | 2013-10-07 18:40:05 -0700 | [diff] [blame] | 1375 | |
Ravi Kumar Alamanda | 48c921d | 2013-10-29 06:07:44 -0700 | [diff] [blame] | 1376 | |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1377 | if (adev->snd_dev_ref_cnt[snd_device] == 0) { |
Ravi Kumar Alamanda | c7d9bef | 2015-09-30 22:27:26 -0700 | [diff] [blame] | 1378 | ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, device_name); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 1379 | |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 1380 | audio_extn_dsm_feedback_enable(adev, snd_device, false); |
| 1381 | |
Xiaojun Sang | 040cc9f | 2015-08-03 19:38:28 +0800 | [diff] [blame] | 1382 | if (platform_can_enable_spkr_prot_on_device(snd_device) && |
| 1383 | audio_extn_spkr_prot_is_enabled()) { |
Anish Kumar | 46c7b87 | 2014-09-09 01:49:44 -0700 | [diff] [blame] | 1384 | audio_extn_spkr_prot_stop_processing(snd_device); |
Vignesh Kulothungan | 3b5fae5 | 2017-09-25 12:16:30 -0700 | [diff] [blame] | 1385 | |
| 1386 | // when speaker device is disabled, reset swap. |
| 1387 | // will be renabled on usecase start |
| 1388 | platform_set_swap_channels(adev, false); |
Haynes Mathew George | bfe8ff4 | 2016-09-22 17:38:16 -0700 | [diff] [blame] | 1389 | } else if (platform_split_snd_device(adev->platform, |
| 1390 | snd_device, |
| 1391 | &num_devices, |
| 1392 | new_snd_devices) == 0) { |
Sidipotu Ashok | 9f0b16e | 2016-04-28 13:48:28 +0530 | [diff] [blame] | 1393 | for (i = 0; i < num_devices; i++) { |
| 1394 | disable_snd_device(adev, new_snd_devices[i]); |
| 1395 | } |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 1396 | platform_set_speaker_gain_in_combo(adev, snd_device, false); |
Lior Barenboim | 0b61bc7 | 2014-05-13 13:01:37 +0300 | [diff] [blame] | 1397 | } else { |
Haynes Mathew George | ef1e3d3 | 2014-04-24 11:53:44 -0700 | [diff] [blame] | 1398 | audio_route_reset_and_update_path(adev->audio_route, device_name); |
Lior Barenboim | 0b61bc7 | 2014-05-13 13:01:37 +0300 | [diff] [blame] | 1399 | } |
Ravi Kumar Alamanda | 8fa6b19 | 2014-09-09 16:06:42 -0700 | [diff] [blame] | 1400 | |
Weiyin Jiang | 0d84c8e | 2019-04-09 22:59:54 +0800 | [diff] [blame] | 1401 | if (snd_device == SND_DEVICE_OUT_BT_A2DP) |
Naresh Tanniru | cd2353e | 2016-08-19 00:37:25 +0530 | [diff] [blame] | 1402 | audio_extn_a2dp_stop_playback(); |
Weiyin Jiang | 0d84c8e | 2019-04-09 22:59:54 +0800 | [diff] [blame] | 1403 | else if (snd_device == SND_DEVICE_IN_BT_A2DP) |
Florian Pfister | 1a84f31 | 2018-07-19 14:38:18 +0200 | [diff] [blame] | 1404 | audio_extn_a2dp_stop_capture(); |
Zhou Song | d6d7175 | 2019-05-21 18:08:51 +0800 | [diff] [blame] | 1405 | else if ((snd_device == SND_DEVICE_OUT_HDMI) || |
Weiyin Jiang | 0d84c8e | 2019-04-09 22:59:54 +0800 | [diff] [blame] | 1406 | (snd_device == SND_DEVICE_OUT_DISPLAY_PORT)) |
Ashish Jain | 81eb2a8 | 2015-05-13 10:52:34 +0530 | [diff] [blame] | 1407 | adev->is_channel_status_set = false; |
Weiyin Jiang | 0d84c8e | 2019-04-09 22:59:54 +0800 | [diff] [blame] | 1408 | else if ((snd_device == SND_DEVICE_OUT_HEADPHONES) && |
Sidipotu Ashok | e6f78cb | 2015-11-05 14:42:20 +0530 | [diff] [blame] | 1409 | adev->native_playback_enabled) { |
| 1410 | ALOGD("%s: %d: napb: disabling native mode in hardware", |
| 1411 | __func__, __LINE__); |
| 1412 | audio_route_reset_and_update_path(adev->audio_route, |
| 1413 | "true-native-mode"); |
| 1414 | adev->native_playback_enabled = false; |
Weiyin Jiang | 0d84c8e | 2019-04-09 22:59:54 +0800 | [diff] [blame] | 1415 | } else if ((snd_device == SND_DEVICE_OUT_HEADPHONES) && |
Preetam Singh Ranawat | b0c0dd7 | 2016-08-18 00:32:06 +0530 | [diff] [blame] | 1416 | adev->asrc_mode_enabled) { |
| 1417 | ALOGD("%s: %d: disabling asrc mode in hardware", __func__, __LINE__); |
Preetam Singh Ranawat | 6a83666 | 2016-09-08 17:04:35 +0530 | [diff] [blame] | 1418 | disable_asrc_mode(adev); |
| 1419 | audio_route_apply_and_update_path(adev->audio_route, "hph-lowpower-mode"); |
Weiyin Jiang | 0d84c8e | 2019-04-09 22:59:54 +0800 | [diff] [blame] | 1420 | } else if (((snd_device == SND_DEVICE_IN_HANDSET_6MIC) || |
Dhanalakshmi Siddani | af4bd62 | 2019-02-27 16:28:06 +0530 | [diff] [blame] | 1421 | (snd_device == SND_DEVICE_IN_HANDSET_QMIC)) && |
Eric Laurent | 637e2d4 | 2018-11-15 12:24:31 -0800 | [diff] [blame] | 1422 | (audio_extn_ffv_get_stream() == adev_get_active_input(adev))) { |
Garmond Leung | e2433c3 | 2017-09-28 21:51:22 -0700 | [diff] [blame] | 1423 | ALOGD("%s: deinit ec ref loopback", __func__); |
| 1424 | audio_extn_ffv_deinit_ec_ref_loopback(adev, snd_device); |
| 1425 | } |
Weiyin Jiang | 0d84c8e | 2019-04-09 22:59:54 +0800 | [diff] [blame] | 1426 | |
| 1427 | audio_extn_utils_release_snd_device(snd_device); |
| 1428 | } else { |
| 1429 | if (platform_split_snd_device(adev->platform, |
| 1430 | snd_device, |
| 1431 | &num_devices, |
| 1432 | new_snd_devices) == 0) { |
| 1433 | for (i = 0; i < num_devices; i++) { |
| 1434 | adev->snd_dev_ref_cnt[new_snd_devices[i]]--; |
| 1435 | } |
| 1436 | } |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1437 | } |
Ravi Kumar Alamanda | 48c921d | 2013-10-29 06:07:44 -0700 | [diff] [blame] | 1438 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1439 | return 0; |
| 1440 | } |
| 1441 | |
Haynes Mathew George | bfe8ff4 | 2016-09-22 17:38:16 -0700 | [diff] [blame] | 1442 | /* |
| 1443 | legend: |
| 1444 | uc - existing usecase |
| 1445 | new_uc - new usecase |
| 1446 | d1, d11, d2 - SND_DEVICE enums |
| 1447 | a1, a2 - corresponding ANDROID device enums |
| 1448 | B1, B2 - backend strings |
| 1449 | |
| 1450 | case 1 |
| 1451 | uc->dev d1 (a1) B1 |
| 1452 | new_uc->dev d1 (a1), d2 (a2) B1, B2 |
| 1453 | |
| 1454 | resolution: disable and enable uc->dev on d1 |
| 1455 | |
| 1456 | case 2 |
| 1457 | uc->dev d1 (a1) B1 |
| 1458 | new_uc->dev d11 (a1) B1 |
| 1459 | |
| 1460 | resolution: need to switch uc since d1 and d11 are related |
| 1461 | (e.g. speaker and voice-speaker) |
| 1462 | use ANDROID_DEVICE_OUT enums to match devices since SND_DEVICE enums may vary |
| 1463 | |
| 1464 | case 3 |
| 1465 | uc->dev d1 (a1) B1 |
| 1466 | new_uc->dev d2 (a2) B2 |
| 1467 | |
| 1468 | resolution: no need to switch uc |
| 1469 | |
| 1470 | case 4 |
| 1471 | uc->dev d1 (a1) B1 |
| 1472 | new_uc->dev d2 (a2) B1 |
| 1473 | |
| 1474 | resolution: disable enable uc-dev on d2 since backends match |
| 1475 | we cannot enable two streams on two different devices if they |
| 1476 | share the same backend. e.g. if offload is on speaker device using |
| 1477 | QUAD_MI2S backend and a low-latency stream is started on voice-handset |
| 1478 | using the same backend, offload must also be switched to voice-handset. |
| 1479 | |
| 1480 | case 5 |
| 1481 | uc->dev d1 (a1) B1 |
| 1482 | new_uc->dev d1 (a1), d2 (a2) B1 |
| 1483 | |
| 1484 | resolution: disable enable uc-dev on d2 since backends match |
| 1485 | we cannot enable two streams on two different devices if they |
| 1486 | share the same backend. |
| 1487 | |
| 1488 | case 6 |
| 1489 | uc->dev d1 (a1) B1 |
| 1490 | new_uc->dev d2 (a1) B2 |
| 1491 | |
| 1492 | resolution: no need to switch |
| 1493 | |
| 1494 | case 7 |
| 1495 | uc->dev d1 (a1), d2 (a2) B1, B2 |
| 1496 | new_uc->dev d1 (a1) B1 |
| 1497 | |
| 1498 | resolution: no need to switch |
| 1499 | |
Zhou Song | 4ba6588 | 2018-07-09 14:48:07 +0800 | [diff] [blame] | 1500 | case 8 |
| 1501 | uc->dev d1 (a1) B1 |
| 1502 | new_uc->dev d11 (a1), d2 (a2) B1, B2 |
| 1503 | resolution: compared to case 1, for this case, d1 and d11 are related |
| 1504 | then need to do the same as case 2 to siwtch to new uc |
Haynes Mathew George | bfe8ff4 | 2016-09-22 17:38:16 -0700 | [diff] [blame] | 1505 | */ |
| 1506 | static snd_device_t derive_playback_snd_device(void * platform, |
| 1507 | struct audio_usecase *uc, |
| 1508 | struct audio_usecase *new_uc, |
| 1509 | snd_device_t new_snd_device) |
| 1510 | { |
Siddartha Shaik | 31b530e | 2017-05-19 15:26:33 +0530 | [diff] [blame] | 1511 | audio_devices_t a1, a2; |
Haynes Mathew George | bfe8ff4 | 2016-09-22 17:38:16 -0700 | [diff] [blame] | 1512 | |
| 1513 | snd_device_t d1 = uc->out_snd_device; |
| 1514 | snd_device_t d2 = new_snd_device; |
| 1515 | |
Siddartha Shaik | 31b530e | 2017-05-19 15:26:33 +0530 | [diff] [blame] | 1516 | switch (uc->type) { |
Surendar Karka | 93cd25a | 2018-08-28 14:21:37 +0530 | [diff] [blame] | 1517 | case TRANSCODE_LOOPBACK_RX : |
Siddartha Shaik | 31b530e | 2017-05-19 15:26:33 +0530 | [diff] [blame] | 1518 | a1 = uc->stream.inout->out_config.devices; |
| 1519 | a2 = new_uc->stream.inout->out_config.devices; |
| 1520 | break; |
| 1521 | default : |
| 1522 | a1 = uc->stream.out->devices; |
| 1523 | a2 = new_uc->stream.out->devices; |
| 1524 | break; |
| 1525 | } |
| 1526 | |
Haynes Mathew George | bfe8ff4 | 2016-09-22 17:38:16 -0700 | [diff] [blame] | 1527 | // Treat as a special case when a1 and a2 are not disjoint |
| 1528 | if ((a1 != a2) && (a1 & a2)) { |
| 1529 | snd_device_t d3[2]; |
| 1530 | int num_devices = 0; |
| 1531 | int ret = platform_split_snd_device(platform, |
| 1532 | popcount(a1) > 1 ? d1 : d2, |
| 1533 | &num_devices, |
| 1534 | d3); |
| 1535 | if (ret < 0) { |
| 1536 | if (ret != -ENOSYS) { |
| 1537 | ALOGW("%s failed to split snd_device %d", |
| 1538 | __func__, |
| 1539 | popcount(a1) > 1 ? d1 : d2); |
| 1540 | } |
| 1541 | goto end; |
| 1542 | } |
| 1543 | |
Haynes Mathew George | bfe8ff4 | 2016-09-22 17:38:16 -0700 | [diff] [blame] | 1544 | if (platform_check_backends_match(d3[0], d3[1])) { |
| 1545 | return d2; // case 5 |
| 1546 | } else { |
Samyak Jain | d826b50 | 2019-07-17 16:16:42 +0530 | [diff] [blame] | 1547 | if (popcount(a1) > 1) |
| 1548 | return d1; //case 7 |
Garmond Leung | b9eeba4 | 2018-09-18 11:10:41 -0700 | [diff] [blame] | 1549 | // check if d1 is related to any of d3's |
| 1550 | if (d1 == d3[0] || d1 == d3[1]) |
Zhou Song | 4ba6588 | 2018-07-09 14:48:07 +0800 | [diff] [blame] | 1551 | return d1; // case 1 |
| 1552 | else |
| 1553 | return d3[1]; // case 8 |
Haynes Mathew George | bfe8ff4 | 2016-09-22 17:38:16 -0700 | [diff] [blame] | 1554 | } |
| 1555 | } else { |
| 1556 | if (platform_check_backends_match(d1, d2)) { |
| 1557 | return d2; // case 2, 4 |
| 1558 | } else { |
| 1559 | return d1; // case 6, 3 |
| 1560 | } |
| 1561 | } |
| 1562 | |
| 1563 | end: |
| 1564 | return d2; // return whatever was calculated before. |
| 1565 | } |
| 1566 | |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1567 | static void check_usecases_codec_backend(struct audio_device *adev, |
Sidipotu Ashok | 9f0b16e | 2016-04-28 13:48:28 +0530 | [diff] [blame] | 1568 | struct audio_usecase *uc_info, |
| 1569 | snd_device_t snd_device) |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1570 | { |
| 1571 | struct listnode *node; |
| 1572 | struct audio_usecase *usecase; |
| 1573 | bool switch_device[AUDIO_USECASE_MAX]; |
Chaithanya Krishna Bacharaju | 49e7db0 | 2017-03-14 11:57:26 +0530 | [diff] [blame] | 1574 | snd_device_t uc_derive_snd_device; |
| 1575 | snd_device_t derive_snd_device[AUDIO_USECASE_MAX]; |
Weiyin Jiang | 0d84c8e | 2019-04-09 22:59:54 +0800 | [diff] [blame] | 1576 | snd_device_t split_snd_devices[SND_DEVICE_OUT_END]; |
| 1577 | int i, num_uc_to_switch = 0, num_devices = 0; |
kunleiz | 5cd52b8 | 2016-11-07 17:22:52 +0800 | [diff] [blame] | 1578 | int status = 0; |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 1579 | bool force_restart_session = false; |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1580 | /* |
| 1581 | * This function is to make sure that all the usecases that are active on |
| 1582 | * the hardware codec backend are always routed to any one device that is |
| 1583 | * handled by the hardware codec. |
| 1584 | * For example, if low-latency and deep-buffer usecases are currently active |
| 1585 | * on speaker and out_set_parameters(headset) is received on low-latency |
| 1586 | * output, then we have to make sure deep-buffer is also switched to headset, |
| 1587 | * because of the limitation that both the devices cannot be enabled |
| 1588 | * at the same time as they share the same backend. |
| 1589 | */ |
Mingming Yin | 3ee55c6 | 2014-08-04 14:23:35 -0700 | [diff] [blame] | 1590 | /* |
| 1591 | * This call is to check if we need to force routing for a particular stream |
| 1592 | * If there is a backend configuration change for the device when a |
| 1593 | * new stream starts, then ADM needs to be closed and re-opened with the new |
| 1594 | * configuraion. This call check if we need to re-route all the streams |
Apoorv Raghuvanshi | 2149216 | 2015-02-19 18:19:36 -0800 | [diff] [blame] | 1595 | * associated with the backend. Touch tone + 24 bit + native playback. |
Mingming Yin | 3ee55c6 | 2014-08-04 14:23:35 -0700 | [diff] [blame] | 1596 | */ |
Apoorv Raghuvanshi | 2149216 | 2015-02-19 18:19:36 -0800 | [diff] [blame] | 1597 | bool force_routing = platform_check_and_set_codec_backend_cfg(adev, uc_info, |
| 1598 | snd_device); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 1599 | /* For a2dp device reconfigure all active sessions |
| 1600 | * with new AFE encoder format based on a2dp state |
| 1601 | */ |
| 1602 | if ((SND_DEVICE_OUT_BT_A2DP == snd_device || |
| 1603 | SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP == snd_device) && |
| 1604 | audio_extn_a2dp_is_force_device_switch()) { |
| 1605 | force_routing = true; |
| 1606 | force_restart_session = true; |
| 1607 | } |
Sidipotu Ashok | e6f78cb | 2015-11-05 14:42:20 +0530 | [diff] [blame] | 1608 | ALOGD("%s:becf: force routing %d", __func__, force_routing); |
| 1609 | |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1610 | /* Disable all the usecases on the shared backend other than the |
Apoorv Raghuvanshi | f59bb22 | 2015-02-18 12:23:23 -0800 | [diff] [blame] | 1611 | * specified usecase. |
Apoorv Raghuvanshi | f59bb22 | 2015-02-18 12:23:23 -0800 | [diff] [blame] | 1612 | */ |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1613 | for (i = 0; i < AUDIO_USECASE_MAX; i++) |
| 1614 | switch_device[i] = false; |
| 1615 | |
| 1616 | list_for_each(node, &adev->usecase_list) { |
| 1617 | usecase = node_to_item(node, struct audio_usecase, list); |
Apoorv Raghuvanshi | 2149216 | 2015-02-19 18:19:36 -0800 | [diff] [blame] | 1618 | |
Sidipotu Ashok | 9f0b16e | 2016-04-28 13:48:28 +0530 | [diff] [blame] | 1619 | ALOGD("%s:becf: (%d) check_usecases curr device: %s, usecase device:%s " |
| 1620 | "backends match %d",__func__, i, |
Sidipotu Ashok | e6f78cb | 2015-11-05 14:42:20 +0530 | [diff] [blame] | 1621 | platform_get_snd_device_name(snd_device), |
Sidipotu Ashok | 9f0b16e | 2016-04-28 13:48:28 +0530 | [diff] [blame] | 1622 | platform_get_snd_device_name(usecase->out_snd_device), |
| 1623 | platform_check_backends_match(snd_device, usecase->out_snd_device)); |
Rahul Sharma | 9977098 | 2019-03-06 17:05:26 +0530 | [diff] [blame] | 1624 | if ((usecase->type != PCM_CAPTURE) && (usecase != uc_info) && |
| 1625 | (usecase->type != PCM_PASSTHROUGH)) { |
Ashish Jain | 6a65b35 | 2017-03-21 17:24:40 +0530 | [diff] [blame] | 1626 | uc_derive_snd_device = derive_playback_snd_device(adev->platform, |
| 1627 | usecase, uc_info, snd_device); |
| 1628 | if (((uc_derive_snd_device != usecase->out_snd_device) || force_routing) && |
| 1629 | ((usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) || |
| 1630 | (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) || |
| 1631 | (usecase->devices & AUDIO_DEVICE_OUT_USB_DEVICE) || |
Garmond Leung | 2fd4f12 | 2018-06-02 16:04:53 -0700 | [diff] [blame] | 1632 | (usecase->devices & AUDIO_DEVICE_OUT_USB_HEADSET) || |
Ashish Jain | 6a65b35 | 2017-03-21 17:24:40 +0530 | [diff] [blame] | 1633 | (usecase->devices & AUDIO_DEVICE_OUT_ALL_A2DP) || |
| 1634 | (usecase->devices & AUDIO_DEVICE_OUT_ALL_SCO)) && |
| 1635 | ((force_restart_session) || |
| 1636 | (platform_check_backends_match(snd_device, usecase->out_snd_device)))) { |
Sidipotu Ashok | 9f0b16e | 2016-04-28 13:48:28 +0530 | [diff] [blame] | 1637 | ALOGD("%s:becf: check_usecases (%s) is active on (%s) - disabling ..", |
| 1638 | __func__, use_case_table[usecase->id], |
| 1639 | platform_get_snd_device_name(usecase->out_snd_device)); |
| 1640 | disable_audio_route(adev, usecase); |
| 1641 | switch_device[usecase->id] = true; |
Chaithanya Krishna Bacharaju | 49e7db0 | 2017-03-14 11:57:26 +0530 | [diff] [blame] | 1642 | /* Enable existing usecase on derived playback device */ |
| 1643 | derive_snd_device[usecase->id] = uc_derive_snd_device; |
Sidipotu Ashok | 9f0b16e | 2016-04-28 13:48:28 +0530 | [diff] [blame] | 1644 | num_uc_to_switch++; |
Ashish Jain | 6a65b35 | 2017-03-21 17:24:40 +0530 | [diff] [blame] | 1645 | } |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1646 | } |
| 1647 | } |
| 1648 | |
Sidipotu Ashok | e6f78cb | 2015-11-05 14:42:20 +0530 | [diff] [blame] | 1649 | ALOGD("%s:becf: check_usecases num.of Usecases to switch %d", __func__, |
| 1650 | num_uc_to_switch); |
| 1651 | |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1652 | if (num_uc_to_switch) { |
Haynes Mathew George | ef1e3d3 | 2014-04-24 11:53:44 -0700 | [diff] [blame] | 1653 | /* All streams have been de-routed. Disable the device */ |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1654 | |
Venkata Narendra Kumar Gutta | 7610e63 | 2014-04-14 23:16:38 +0530 | [diff] [blame] | 1655 | /* Make sure the previous devices to be disabled first and then enable the |
| 1656 | selected devices */ |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1657 | list_for_each(node, &adev->usecase_list) { |
| 1658 | usecase = node_to_item(node, struct audio_usecase, list); |
| 1659 | if (switch_device[usecase->id]) { |
Weiyin Jiang | 0d84c8e | 2019-04-09 22:59:54 +0800 | [diff] [blame] | 1660 | /* Check if output sound device to be switched can be split and if any |
| 1661 | of the split devices match with derived sound device */ |
| 1662 | if (platform_split_snd_device(adev->platform, usecase->out_snd_device, |
| 1663 | &num_devices, split_snd_devices) == 0) { |
| 1664 | adev->snd_dev_ref_cnt[usecase->out_snd_device]--; |
| 1665 | for (i = 0; i < num_devices; i++) { |
| 1666 | /* Disable devices that do not match with derived sound device */ |
| 1667 | if (split_snd_devices[i] != derive_snd_device[usecase->id]) |
| 1668 | disable_snd_device(adev, split_snd_devices[i]); |
| 1669 | } |
| 1670 | } else { |
| 1671 | disable_snd_device(adev, usecase->out_snd_device); |
| 1672 | } |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1673 | } |
| 1674 | } |
| 1675 | |
Krishnankutty Kolathappilly | dc4f757 | 2013-11-01 20:07:13 -0700 | [diff] [blame] | 1676 | list_for_each(node, &adev->usecase_list) { |
| 1677 | usecase = node_to_item(node, struct audio_usecase, list); |
| 1678 | if (switch_device[usecase->id]) { |
Weiyin Jiang | 0d84c8e | 2019-04-09 22:59:54 +0800 | [diff] [blame] | 1679 | if (platform_split_snd_device(adev->platform, usecase->out_snd_device, |
| 1680 | &num_devices, split_snd_devices) == 0) { |
| 1681 | /* Enable derived sound device only if it does not match with |
| 1682 | one of the split sound devices. This is because the matching |
| 1683 | sound device was not disabled */ |
| 1684 | bool should_enable = true; |
| 1685 | for (i = 0; i < num_devices; i++) { |
| 1686 | if (derive_snd_device[usecase->id] == split_snd_devices[i]) { |
| 1687 | should_enable = false; |
| 1688 | break; |
| 1689 | } |
| 1690 | } |
| 1691 | if (should_enable) |
| 1692 | enable_snd_device(adev, derive_snd_device[usecase->id]); |
| 1693 | } else { |
| 1694 | enable_snd_device(adev, derive_snd_device[usecase->id]); |
| 1695 | } |
Krishnankutty Kolathappilly | dc4f757 | 2013-11-01 20:07:13 -0700 | [diff] [blame] | 1696 | } |
| 1697 | } |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1698 | |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1699 | /* Re-route all the usecases on the shared backend other than the |
| 1700 | specified usecase to new snd devices */ |
| 1701 | list_for_each(node, &adev->usecase_list) { |
| 1702 | usecase = node_to_item(node, struct audio_usecase, list); |
Karthik Reddy Katta | 3abfee2 | 2016-02-23 10:55:27 +0530 | [diff] [blame] | 1703 | /* Update the out_snd_device only before enabling the audio route */ |
| 1704 | if (switch_device[usecase->id]) { |
Chaithanya Krishna Bacharaju | 49e7db0 | 2017-03-14 11:57:26 +0530 | [diff] [blame] | 1705 | usecase->out_snd_device = derive_snd_device[usecase->id]; |
Karthik Reddy Katta | 3abfee2 | 2016-02-23 10:55:27 +0530 | [diff] [blame] | 1706 | if (usecase->type != VOICE_CALL) { |
Sidipotu Ashok | e6f78cb | 2015-11-05 14:42:20 +0530 | [diff] [blame] | 1707 | ALOGD("%s:becf: enabling usecase (%s) on (%s)", __func__, |
Karthik Reddy Katta | 3abfee2 | 2016-02-23 10:55:27 +0530 | [diff] [blame] | 1708 | use_case_table[usecase->id], |
| 1709 | platform_get_snd_device_name(usecase->out_snd_device)); |
kunleiz | 5cd52b8 | 2016-11-07 17:22:52 +0800 | [diff] [blame] | 1710 | /* Update voc calibration before enabling VoIP route */ |
| 1711 | if (usecase->type == VOIP_CALL) |
| 1712 | status = platform_switch_voice_call_device_post(adev->platform, |
Eric Laurent | 637e2d4 | 2018-11-15 12:24:31 -0800 | [diff] [blame] | 1713 | usecase->out_snd_device, |
| 1714 | platform_get_input_snd_device( |
| 1715 | adev->platform, NULL, |
| 1716 | uc_info->devices)); |
Avinash Vaish | 71a8b97 | 2014-07-24 15:36:33 +0530 | [diff] [blame] | 1717 | enable_audio_route(adev, usecase); |
Sujin Panicker | 390724d | 2019-04-26 10:43:36 +0530 | [diff] [blame] | 1718 | if (usecase->stream.out && usecase->id == USECASE_AUDIO_PLAYBACK_VOIP) { |
kunleiz | 46c47dd | 2018-03-19 16:28:09 +0800 | [diff] [blame] | 1719 | out_set_voip_volume(&usecase->stream.out->stream, |
| 1720 | usecase->stream.out->volume_l, |
| 1721 | usecase->stream.out->volume_r); |
| 1722 | } |
Karthik Reddy Katta | 3abfee2 | 2016-02-23 10:55:27 +0530 | [diff] [blame] | 1723 | } |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1724 | } |
| 1725 | } |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1726 | } |
| 1727 | } |
| 1728 | |
Manish Dewangan | ba9fcfa | 2016-03-24 16:20:06 +0530 | [diff] [blame] | 1729 | static void check_usecases_capture_codec_backend(struct audio_device *adev, |
Ravi Kumar Alamanda | c4ba743 | 2013-06-05 14:11:39 -0700 | [diff] [blame] | 1730 | struct audio_usecase *uc_info, |
| 1731 | snd_device_t snd_device) |
| 1732 | { |
| 1733 | struct listnode *node; |
| 1734 | struct audio_usecase *usecase; |
| 1735 | bool switch_device[AUDIO_USECASE_MAX]; |
| 1736 | int i, num_uc_to_switch = 0; |
Dhanalakshmi Siddani | b678a80 | 2016-12-03 11:51:41 +0530 | [diff] [blame] | 1737 | int backend_check_cond = AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND; |
kunleiz | 5cd52b8 | 2016-11-07 17:22:52 +0800 | [diff] [blame] | 1738 | int status = 0; |
Ravi Kumar Alamanda | c4ba743 | 2013-06-05 14:11:39 -0700 | [diff] [blame] | 1739 | |
Manish Dewangan | ba9fcfa | 2016-03-24 16:20:06 +0530 | [diff] [blame] | 1740 | bool force_routing = platform_check_and_set_capture_codec_backend_cfg(adev, uc_info, |
| 1741 | snd_device); |
| 1742 | ALOGD("%s:becf: force routing %d", __func__, force_routing); |
Dhanalakshmi Siddani | b678a80 | 2016-12-03 11:51:41 +0530 | [diff] [blame] | 1743 | |
| 1744 | /* |
| 1745 | * Make sure out devices is checked against out codec backend device and |
| 1746 | * also in devices against in codec backend. Checking out device against in |
| 1747 | * codec backend or vice versa causes issues. |
| 1748 | */ |
| 1749 | if (uc_info->type == PCM_CAPTURE) |
| 1750 | backend_check_cond = AUDIO_DEVICE_IN_ALL_CODEC_BACKEND; |
Ravi Kumar Alamanda | c4ba743 | 2013-06-05 14:11:39 -0700 | [diff] [blame] | 1751 | /* |
| 1752 | * This function is to make sure that all the active capture usecases |
| 1753 | * are always routed to the same input sound device. |
| 1754 | * For example, if audio-record and voice-call usecases are currently |
| 1755 | * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece) |
| 1756 | * is received for voice call then we have to make sure that audio-record |
| 1757 | * usecase is also switched to earpiece i.e. voice-dmic-ef, |
| 1758 | * because of the limitation that two devices cannot be enabled |
| 1759 | * at the same time if they share the same backend. |
| 1760 | */ |
| 1761 | for (i = 0; i < AUDIO_USECASE_MAX; i++) |
| 1762 | switch_device[i] = false; |
| 1763 | |
| 1764 | list_for_each(node, &adev->usecase_list) { |
| 1765 | usecase = node_to_item(node, struct audio_usecase, list); |
Dhanalakshmi Siddani | b678a80 | 2016-12-03 11:51:41 +0530 | [diff] [blame] | 1766 | /* |
| 1767 | * TODO: Enhance below condition to handle BT sco/USB multi recording |
| 1768 | */ |
Shiv Maliyappanahalli | 80ac628 | 2013-12-20 18:56:15 -0800 | [diff] [blame] | 1769 | if (usecase->type != PCM_PLAYBACK && |
Ravi Kumar Alamanda | c4ba743 | 2013-06-05 14:11:39 -0700 | [diff] [blame] | 1770 | usecase != uc_info && |
Manish Dewangan | ba9fcfa | 2016-03-24 16:20:06 +0530 | [diff] [blame] | 1771 | (usecase->in_snd_device != snd_device || force_routing) && |
Dhanalakshmi Siddani | b678a80 | 2016-12-03 11:51:41 +0530 | [diff] [blame] | 1772 | ((uc_info->devices & backend_check_cond) && |
Karthik Reddy Katta | 0d47176 | 2015-08-20 13:36:07 +0530 | [diff] [blame] | 1773 | (((usecase->devices & ~AUDIO_DEVICE_BIT_IN) & AUDIO_DEVICE_IN_ALL_CODEC_BACKEND) || |
Satya Krishna Pindiproli | 458b5a7 | 2017-03-13 15:14:24 +0530 | [diff] [blame] | 1774 | (usecase->type == VOIP_CALL))) && |
Carter Hsu | 1d2a053 | 2018-10-04 09:24:36 +0800 | [diff] [blame] | 1775 | ((uc_info->type == VOICE_CALL && |
| 1776 | usecase->devices == AUDIO_DEVICE_IN_VOICE_CALL) || |
| 1777 | platform_check_backends_match(snd_device,\ |
| 1778 | usecase->in_snd_device)) && |
Anish Kumar | a020a7c | 2014-10-17 11:13:22 -0700 | [diff] [blame] | 1779 | (usecase->id != USECASE_AUDIO_SPKR_CALIB_TX)) { |
Ravi Kumar Alamanda | c4ba743 | 2013-06-05 14:11:39 -0700 | [diff] [blame] | 1780 | ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..", |
| 1781 | __func__, use_case_table[usecase->id], |
Devin Kim | 1e5f353 | 2013-08-09 07:48:29 -0700 | [diff] [blame] | 1782 | platform_get_snd_device_name(usecase->in_snd_device)); |
Haynes Mathew George | 1376ca6 | 2014-04-24 11:55:48 -0700 | [diff] [blame] | 1783 | disable_audio_route(adev, usecase); |
Ravi Kumar Alamanda | c4ba743 | 2013-06-05 14:11:39 -0700 | [diff] [blame] | 1784 | switch_device[usecase->id] = true; |
| 1785 | num_uc_to_switch++; |
| 1786 | } |
| 1787 | } |
| 1788 | |
| 1789 | if (num_uc_to_switch) { |
Haynes Mathew George | ef1e3d3 | 2014-04-24 11:53:44 -0700 | [diff] [blame] | 1790 | /* All streams have been de-routed. Disable the device */ |
Ravi Kumar Alamanda | c4ba743 | 2013-06-05 14:11:39 -0700 | [diff] [blame] | 1791 | |
Venkata Narendra Kumar Gutta | 7610e63 | 2014-04-14 23:16:38 +0530 | [diff] [blame] | 1792 | /* Make sure the previous devices to be disabled first and then enable the |
| 1793 | selected devices */ |
Ravi Kumar Alamanda | c4ba743 | 2013-06-05 14:11:39 -0700 | [diff] [blame] | 1794 | list_for_each(node, &adev->usecase_list) { |
| 1795 | usecase = node_to_item(node, struct audio_usecase, list); |
| 1796 | if (switch_device[usecase->id]) { |
Haynes Mathew George | 1376ca6 | 2014-04-24 11:55:48 -0700 | [diff] [blame] | 1797 | disable_snd_device(adev, usecase->in_snd_device); |
Shiv Maliyappanahalli | 80ac628 | 2013-12-20 18:56:15 -0800 | [diff] [blame] | 1798 | } |
| 1799 | } |
| 1800 | |
| 1801 | list_for_each(node, &adev->usecase_list) { |
| 1802 | usecase = node_to_item(node, struct audio_usecase, list); |
| 1803 | if (switch_device[usecase->id]) { |
Haynes Mathew George | 1376ca6 | 2014-04-24 11:55:48 -0700 | [diff] [blame] | 1804 | enable_snd_device(adev, snd_device); |
Ravi Kumar Alamanda | c4ba743 | 2013-06-05 14:11:39 -0700 | [diff] [blame] | 1805 | } |
| 1806 | } |
| 1807 | |
Ravi Kumar Alamanda | c4ba743 | 2013-06-05 14:11:39 -0700 | [diff] [blame] | 1808 | /* Re-route all the usecases on the shared backend other than the |
| 1809 | specified usecase to new snd devices */ |
| 1810 | list_for_each(node, &adev->usecase_list) { |
| 1811 | usecase = node_to_item(node, struct audio_usecase, list); |
| 1812 | /* Update the in_snd_device only before enabling the audio route */ |
| 1813 | if (switch_device[usecase->id] ) { |
| 1814 | usecase->in_snd_device = snd_device; |
kunleiz | 5cd52b8 | 2016-11-07 17:22:52 +0800 | [diff] [blame] | 1815 | if (usecase->type != VOICE_CALL) { |
| 1816 | /* Update voc calibration before enabling VoIP route */ |
| 1817 | if (usecase->type == VOIP_CALL) |
| 1818 | status = platform_switch_voice_call_device_post(adev->platform, |
Zhou Song | 557e728 | 2017-05-05 17:18:18 +0800 | [diff] [blame] | 1819 | platform_get_output_snd_device(adev->platform, uc_info->stream.out), |
kunleiz | 5cd52b8 | 2016-11-07 17:22:52 +0800 | [diff] [blame] | 1820 | usecase->in_snd_device); |
Avinash Vaish | 71a8b97 | 2014-07-24 15:36:33 +0530 | [diff] [blame] | 1821 | enable_audio_route(adev, usecase); |
kunleiz | 5cd52b8 | 2016-11-07 17:22:52 +0800 | [diff] [blame] | 1822 | } |
Ravi Kumar Alamanda | c4ba743 | 2013-06-05 14:11:39 -0700 | [diff] [blame] | 1823 | } |
| 1824 | } |
Ravi Kumar Alamanda | c4ba743 | 2013-06-05 14:11:39 -0700 | [diff] [blame] | 1825 | } |
| 1826 | } |
| 1827 | |
Mingming Yin | 3a941d4 | 2016-02-17 18:08:05 -0800 | [diff] [blame] | 1828 | static void reset_hdmi_sink_caps(struct stream_out *out) { |
| 1829 | int i = 0; |
| 1830 | |
| 1831 | for (i = 0; i<= MAX_SUPPORTED_CHANNEL_MASKS; i++) { |
| 1832 | out->supported_channel_masks[i] = 0; |
| 1833 | } |
| 1834 | for (i = 0; i<= MAX_SUPPORTED_FORMATS; i++) { |
| 1835 | out->supported_formats[i] = 0; |
| 1836 | } |
| 1837 | for (i = 0; i<= MAX_SUPPORTED_SAMPLE_RATES; i++) { |
| 1838 | out->supported_sample_rates[i] = 0; |
| 1839 | } |
| 1840 | } |
| 1841 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1842 | /* must be called with hw device mutex locked */ |
Mingming Yin | 3a941d4 | 2016-02-17 18:08:05 -0800 | [diff] [blame] | 1843 | static int read_hdmi_sink_caps(struct stream_out *out) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1844 | { |
Mingming Yin | 3a941d4 | 2016-02-17 18:08:05 -0800 | [diff] [blame] | 1845 | int ret = 0, i = 0, j = 0; |
Vignesh Kulothungan | 39fc6a2 | 2019-08-01 00:55:59 -0700 | [diff] [blame] | 1846 | int channels = platform_edid_get_max_channels_v2(out->dev->platform, |
| 1847 | out->extconn.cs.controller, |
| 1848 | out->extconn.cs.stream); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1849 | |
Mingming Yin | 3a941d4 | 2016-02-17 18:08:05 -0800 | [diff] [blame] | 1850 | reset_hdmi_sink_caps(out); |
| 1851 | |
Shiv Maliyappanahalli | c065640 | 2016-09-03 14:13:26 -0700 | [diff] [blame] | 1852 | /* Cache ext disp type */ |
Vignesh Kulothungan | 39fc6a2 | 2019-08-01 00:55:59 -0700 | [diff] [blame] | 1853 | if (platform_get_ext_disp_type_v2(adev->platform, |
| 1854 | out->extconn.cs.controller, |
| 1855 | out->extconn.cs.stream <= 0)) { |
Shiv Maliyappanahalli | c065640 | 2016-09-03 14:13:26 -0700 | [diff] [blame] | 1856 | ALOGE("%s: Failed to query disp type, ret:%d", __func__, ret); |
Garmond Leung | 37850ab | 2016-10-06 11:42:18 -0700 | [diff] [blame] | 1857 | return -EINVAL; |
Shiv Maliyappanahalli | c065640 | 2016-09-03 14:13:26 -0700 | [diff] [blame] | 1858 | } |
| 1859 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1860 | switch (channels) { |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1861 | case 8: |
Mingming Yin | 3a941d4 | 2016-02-17 18:08:05 -0800 | [diff] [blame] | 1862 | ALOGV("%s: HDMI supports 7.1 channels", __func__); |
Pradnya Chaphekar | 4403bd7 | 2014-09-09 09:50:01 -0700 | [diff] [blame] | 1863 | out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_7POINT1; |
Mingming Yin | 3a941d4 | 2016-02-17 18:08:05 -0800 | [diff] [blame] | 1864 | out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_6POINT1; |
| 1865 | case 6: |
| 1866 | ALOGV("%s: HDMI supports 5.1 channels", __func__); |
| 1867 | out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_5POINT1; |
| 1868 | out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_PENTA; |
| 1869 | out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_QUAD; |
| 1870 | out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_SURROUND; |
| 1871 | out->supported_channel_masks[i++] = AUDIO_CHANNEL_OUT_2POINT1; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1872 | break; |
| 1873 | default: |
Mingming Yin | 3a941d4 | 2016-02-17 18:08:05 -0800 | [diff] [blame] | 1874 | ALOGE("invalid/nonstandard channal count[%d]",channels); |
Ravi Kumar Alamanda | b199506 | 2013-03-21 23:18:20 -0700 | [diff] [blame] | 1875 | ret = -ENOSYS; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1876 | break; |
| 1877 | } |
Mingming Yin | 3a941d4 | 2016-02-17 18:08:05 -0800 | [diff] [blame] | 1878 | |
| 1879 | // check channel format caps |
| 1880 | i = 0; |
Vignesh Kulothungan | 39fc6a2 | 2019-08-01 00:55:59 -0700 | [diff] [blame] | 1881 | if (platform_is_edid_supported_format_v2(out->dev->platform, AUDIO_FORMAT_AC3, |
| 1882 | out->extconn.cs.controller, |
| 1883 | out->extconn.cs.stream)) { |
Mingming Yin | 3a941d4 | 2016-02-17 18:08:05 -0800 | [diff] [blame] | 1884 | ALOGV(":%s HDMI supports AC3/EAC3 formats", __func__); |
| 1885 | out->supported_formats[i++] = AUDIO_FORMAT_AC3; |
| 1886 | //Adding EAC3/EAC3_JOC formats if AC3 is supported by the sink. |
| 1887 | //EAC3/EAC3_JOC will be converted to AC3 for decoding if needed |
| 1888 | out->supported_formats[i++] = AUDIO_FORMAT_E_AC3; |
| 1889 | out->supported_formats[i++] = AUDIO_FORMAT_E_AC3_JOC; |
| 1890 | } |
| 1891 | |
Vignesh Kulothungan | 39fc6a2 | 2019-08-01 00:55:59 -0700 | [diff] [blame] | 1892 | if (platform_is_edid_supported_format_v2(out->dev->platform, AUDIO_FORMAT_DOLBY_TRUEHD, |
| 1893 | out->extconn.cs.controller, |
| 1894 | out->extconn.cs.stream)) { |
Ben Romberger | 1aaaf86 | 2017-04-06 17:49:46 -0700 | [diff] [blame] | 1895 | ALOGV(":%s HDMI supports TRUE HD format", __func__); |
| 1896 | out->supported_formats[i++] = AUDIO_FORMAT_DOLBY_TRUEHD; |
| 1897 | } |
| 1898 | |
Vignesh Kulothungan | 39fc6a2 | 2019-08-01 00:55:59 -0700 | [diff] [blame] | 1899 | if (platform_is_edid_supported_format_v2(out->dev->platform, AUDIO_FORMAT_DTS, |
| 1900 | out->extconn.cs.controller, |
| 1901 | out->extconn.cs.stream)) { |
Mingming Yin | 3a941d4 | 2016-02-17 18:08:05 -0800 | [diff] [blame] | 1902 | ALOGV(":%s HDMI supports DTS format", __func__); |
| 1903 | out->supported_formats[i++] = AUDIO_FORMAT_DTS; |
| 1904 | } |
| 1905 | |
Vignesh Kulothungan | 39fc6a2 | 2019-08-01 00:55:59 -0700 | [diff] [blame] | 1906 | if (platform_is_edid_supported_format_v2(out->dev->platform, AUDIO_FORMAT_DTS_HD, |
| 1907 | out->extconn.cs.controller, |
| 1908 | out->extconn.cs.stream)) { |
Mingming Yin | 3a941d4 | 2016-02-17 18:08:05 -0800 | [diff] [blame] | 1909 | ALOGV(":%s HDMI supports DTS HD format", __func__); |
| 1910 | out->supported_formats[i++] = AUDIO_FORMAT_DTS_HD; |
| 1911 | } |
| 1912 | |
Vignesh Kulothungan | 39fc6a2 | 2019-08-01 00:55:59 -0700 | [diff] [blame] | 1913 | if (platform_is_edid_supported_format_v2(out->dev->platform, AUDIO_FORMAT_IEC61937, |
| 1914 | out->extconn.cs.controller, |
| 1915 | out->extconn.cs.stream)) { |
Naresh Tanniru | 928f086 | 2017-04-07 16:44:23 -0700 | [diff] [blame] | 1916 | ALOGV(":%s HDMI supports IEC61937 format", __func__); |
| 1917 | out->supported_formats[i++] = AUDIO_FORMAT_IEC61937; |
| 1918 | } |
| 1919 | |
Mingming Yin | 3a941d4 | 2016-02-17 18:08:05 -0800 | [diff] [blame] | 1920 | |
| 1921 | // check sample rate caps |
| 1922 | i = 0; |
| 1923 | for (j = 0; j < MAX_SUPPORTED_SAMPLE_RATES; j++) { |
Vignesh Kulothungan | 39fc6a2 | 2019-08-01 00:55:59 -0700 | [diff] [blame] | 1924 | if (platform_is_edid_supported_sample_rate_v2(out->dev->platform, out_hdmi_sample_rates[j], |
| 1925 | out->extconn.cs.controller, |
| 1926 | out->extconn.cs.stream)) { |
Mingming Yin | 3a941d4 | 2016-02-17 18:08:05 -0800 | [diff] [blame] | 1927 | ALOGV(":%s HDMI supports sample rate:%d", __func__, out_hdmi_sample_rates[j]); |
| 1928 | out->supported_sample_rates[i++] = out_hdmi_sample_rates[j]; |
| 1929 | } |
| 1930 | } |
| 1931 | |
Ravi Kumar Alamanda | b199506 | 2013-03-21 23:18:20 -0700 | [diff] [blame] | 1932 | return ret; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1933 | } |
| 1934 | |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 1935 | static inline ssize_t read_usb_sup_sample_rates(bool is_playback __unused, |
| 1936 | uint32_t *supported_sample_rates __unused, |
| 1937 | uint32_t max_rates __unused) |
| 1938 | { |
| 1939 | ssize_t count = audio_extn_usb_get_sup_sample_rates(is_playback, |
| 1940 | supported_sample_rates, |
| 1941 | max_rates); |
Ashish Jain | 4847e9d | 2017-08-17 19:16:57 +0530 | [diff] [blame] | 1942 | ssize_t i = 0; |
| 1943 | |
| 1944 | for (i=0; i<count; i++) { |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 1945 | ALOGV("%s %s %d", __func__, is_playback ? "P" : "C", |
| 1946 | supported_sample_rates[i]); |
| 1947 | } |
| 1948 | return count; |
| 1949 | } |
| 1950 | |
| 1951 | static inline int read_usb_sup_channel_masks(bool is_playback, |
| 1952 | audio_channel_mask_t *supported_channel_masks, |
| 1953 | uint32_t max_masks) |
| 1954 | { |
| 1955 | int channels = audio_extn_usb_get_max_channels(is_playback); |
| 1956 | int channel_count; |
| 1957 | uint32_t num_masks = 0; |
| 1958 | if (channels > MAX_HIFI_CHANNEL_COUNT) |
| 1959 | channels = MAX_HIFI_CHANNEL_COUNT; |
| 1960 | |
| 1961 | if (is_playback) { |
Eric Laurent | 68a8711 | 2019-05-01 18:07:29 -0700 | [diff] [blame] | 1962 | // start from 2 channels as framework currently doesn't support mono. |
| 1963 | if (channels >= FCC_2) { |
| 1964 | supported_channel_masks[num_masks++] = audio_channel_out_mask_from_count(FCC_2); |
| 1965 | } |
| 1966 | for (channel_count = FCC_2; |
| 1967 | channel_count <= channels && num_masks < max_masks; |
| 1968 | ++channel_count) { |
| 1969 | supported_channel_masks[num_masks++] = |
| 1970 | audio_channel_mask_for_index_assignment_from_count(channel_count); |
| 1971 | } |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 1972 | } else { |
vincenttew | f51c94e | 2019-05-07 10:28:53 +0800 | [diff] [blame] | 1973 | // For capture we report all supported channel masks from 1 channel up. |
| 1974 | channel_count = MIN_CHANNEL_COUNT; |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 1975 | // audio_channel_in_mask_from_count() does the right conversion to either positional or |
| 1976 | // indexed mask |
Eric Laurent | 68a8711 | 2019-05-01 18:07:29 -0700 | [diff] [blame] | 1977 | for ( ; channel_count <= channels && num_masks < max_masks; channel_count++) { |
| 1978 | audio_channel_mask_t mask = AUDIO_CHANNEL_NONE; |
| 1979 | if (channel_count <= FCC_2) { |
| 1980 | mask = audio_channel_in_mask_from_count(channel_count); |
| 1981 | supported_channel_masks[num_masks++] = mask; |
| 1982 | } |
| 1983 | const audio_channel_mask_t index_mask = |
| 1984 | audio_channel_mask_for_index_assignment_from_count(channel_count); |
| 1985 | if (mask != index_mask && num_masks < max_masks) { // ensure index mask added. |
| 1986 | supported_channel_masks[num_masks++] = index_mask; |
| 1987 | } |
| 1988 | } |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 1989 | } |
Lakshman Chaluvaraju | b79fafd | 2018-11-21 10:24:37 +0530 | [diff] [blame] | 1990 | |
vincenttew | f51c94e | 2019-05-07 10:28:53 +0800 | [diff] [blame] | 1991 | for (size_t i = 0; i < num_masks; ++i) { |
| 1992 | ALOGV("%s: %s supported ch %d supported_channel_masks[%zu] %08x num_masks %d", __func__, |
| 1993 | is_playback ? "P" : "C", channels, i, supported_channel_masks[i], num_masks); |
Lakshman Chaluvaraju | b79fafd | 2018-11-21 10:24:37 +0530 | [diff] [blame] | 1994 | } |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 1995 | return num_masks; |
| 1996 | } |
| 1997 | |
| 1998 | static inline int read_usb_sup_formats(bool is_playback __unused, |
| 1999 | audio_format_t *supported_formats, |
| 2000 | uint32_t max_formats __unused) |
| 2001 | { |
| 2002 | int bitwidth = audio_extn_usb_get_max_bit_width(is_playback); |
| 2003 | switch (bitwidth) { |
| 2004 | case 24: |
| 2005 | // XXX : usb.c returns 24 for s24 and s24_le? |
| 2006 | supported_formats[0] = AUDIO_FORMAT_PCM_24_BIT_PACKED; |
| 2007 | break; |
| 2008 | case 32: |
| 2009 | supported_formats[0] = AUDIO_FORMAT_PCM_32_BIT; |
| 2010 | break; |
| 2011 | case 16: |
| 2012 | default : |
| 2013 | supported_formats[0] = AUDIO_FORMAT_PCM_16_BIT; |
| 2014 | break; |
| 2015 | } |
| 2016 | ALOGV("%s: %s supported format %d", __func__, |
| 2017 | is_playback ? "P" : "C", bitwidth); |
| 2018 | return 1; |
| 2019 | } |
| 2020 | |
| 2021 | static inline int read_usb_sup_params_and_compare(bool is_playback, |
| 2022 | audio_format_t *format, |
| 2023 | audio_format_t *supported_formats, |
| 2024 | uint32_t max_formats, |
| 2025 | audio_channel_mask_t *mask, |
| 2026 | audio_channel_mask_t *supported_channel_masks, |
| 2027 | uint32_t max_masks, |
| 2028 | uint32_t *rate, |
| 2029 | uint32_t *supported_sample_rates, |
| 2030 | uint32_t max_rates) { |
| 2031 | int ret = 0; |
| 2032 | int num_formats; |
| 2033 | int num_masks; |
| 2034 | int num_rates; |
| 2035 | int i; |
| 2036 | |
| 2037 | num_formats = read_usb_sup_formats(is_playback, supported_formats, |
| 2038 | max_formats); |
| 2039 | num_masks = read_usb_sup_channel_masks(is_playback, supported_channel_masks, |
| 2040 | max_masks); |
| 2041 | |
| 2042 | num_rates = read_usb_sup_sample_rates(is_playback, |
| 2043 | supported_sample_rates, max_rates); |
| 2044 | |
| 2045 | #define LUT(table, len, what, dflt) \ |
| 2046 | for (i=0; i<len && (table[i] != what); i++); \ |
| 2047 | if (i==len) { ret |= (what == dflt ? 0 : -1); what=table[0]; } |
| 2048 | |
| 2049 | LUT(supported_formats, num_formats, *format, AUDIO_FORMAT_DEFAULT); |
| 2050 | LUT(supported_channel_masks, num_masks, *mask, AUDIO_CHANNEL_NONE); |
| 2051 | LUT(supported_sample_rates, num_rates, *rate, 0); |
| 2052 | |
| 2053 | #undef LUT |
| 2054 | return ret < 0 ? -EINVAL : 0; // HACK TBD |
| 2055 | } |
| 2056 | |
Alexy Joseph | b137994 | 2016-01-29 15:49:38 -0800 | [diff] [blame] | 2057 | audio_usecase_t get_usecase_id_from_usecase_type(const struct audio_device *adev, |
Narsinga Rao Chella | f928a98 | 2015-03-06 14:57:35 -0800 | [diff] [blame] | 2058 | usecase_type_t type) |
Shiv Maliyappanahalli | 34b585f | 2013-10-01 15:49:05 -0700 | [diff] [blame] | 2059 | { |
| 2060 | struct audio_usecase *usecase; |
| 2061 | struct listnode *node; |
| 2062 | |
| 2063 | list_for_each(node, &adev->usecase_list) { |
| 2064 | usecase = node_to_item(node, struct audio_usecase, list); |
Narsinga Rao Chella | f928a98 | 2015-03-06 14:57:35 -0800 | [diff] [blame] | 2065 | if (usecase->type == type) { |
Shiv Maliyappanahalli | 34b585f | 2013-10-01 15:49:05 -0700 | [diff] [blame] | 2066 | ALOGV("%s: usecase id %d", __func__, usecase->id); |
| 2067 | return usecase->id; |
| 2068 | } |
| 2069 | } |
| 2070 | return USECASE_INVALID; |
| 2071 | } |
| 2072 | |
Alexy Joseph | b137994 | 2016-01-29 15:49:38 -0800 | [diff] [blame] | 2073 | struct audio_usecase *get_usecase_from_list(const struct audio_device *adev, |
Shiv Maliyappanahalli | 34b585f | 2013-10-01 15:49:05 -0700 | [diff] [blame] | 2074 | audio_usecase_t uc_id) |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 2075 | { |
| 2076 | struct audio_usecase *usecase; |
| 2077 | struct listnode *node; |
| 2078 | |
| 2079 | list_for_each(node, &adev->usecase_list) { |
| 2080 | usecase = node_to_item(node, struct audio_usecase, list); |
| 2081 | if (usecase->id == uc_id) |
| 2082 | return usecase; |
| 2083 | } |
| 2084 | return NULL; |
| 2085 | } |
| 2086 | |
Sidipotu Ashok | e6f78cb | 2015-11-05 14:42:20 +0530 | [diff] [blame] | 2087 | /* |
| 2088 | * is a true native playback active |
| 2089 | */ |
| 2090 | bool audio_is_true_native_stream_active(struct audio_device *adev) |
| 2091 | { |
| 2092 | bool active = false; |
| 2093 | int i = 0; |
| 2094 | struct listnode *node; |
| 2095 | |
| 2096 | if (NATIVE_AUDIO_MODE_TRUE_44_1 != platform_get_native_support()) { |
| 2097 | ALOGV("%s:napb: not in true mode or non hdphones device", |
| 2098 | __func__); |
| 2099 | active = false; |
| 2100 | goto exit; |
| 2101 | } |
| 2102 | |
| 2103 | list_for_each(node, &adev->usecase_list) { |
| 2104 | struct audio_usecase *uc; |
| 2105 | uc = node_to_item(node, struct audio_usecase, list); |
| 2106 | struct stream_out *curr_out = |
| 2107 | (struct stream_out*) uc->stream.out; |
| 2108 | |
| 2109 | if (curr_out && PCM_PLAYBACK == uc->type) { |
| 2110 | ALOGD("%s:napb: (%d) (%s)id (%d) sr %d bw " |
| 2111 | "(%d) device %s", __func__, i++, use_case_table[uc->id], |
| 2112 | uc->id, curr_out->sample_rate, |
| 2113 | curr_out->bit_width, |
| 2114 | platform_get_snd_device_name(uc->out_snd_device)); |
| 2115 | |
| 2116 | if (is_offload_usecase(uc->id) && |
| 2117 | (curr_out->sample_rate == OUTPUT_SAMPLING_RATE_44100)) { |
| 2118 | active = true; |
| 2119 | ALOGD("%s:napb:native stream detected", __func__); |
| 2120 | } |
| 2121 | } |
| 2122 | } |
| 2123 | exit: |
| 2124 | return active; |
| 2125 | } |
| 2126 | |
Xiaojun Sang | 785b5da | 2017-08-03 15:52:29 +0800 | [diff] [blame] | 2127 | uint32_t adev_get_dsp_bit_width_enforce_mode() |
| 2128 | { |
| 2129 | if (adev == NULL) { |
| 2130 | ALOGE("%s: adev is null. Disable DSP bit width enforce mode.\n", __func__); |
| 2131 | return 0; |
| 2132 | } |
| 2133 | return adev->dsp_bit_width_enforce_mode; |
| 2134 | } |
| 2135 | |
| 2136 | static uint32_t adev_init_dsp_bit_width_enforce_mode(struct mixer *mixer) |
| 2137 | { |
| 2138 | char value[PROPERTY_VALUE_MAX]; |
| 2139 | int trial; |
| 2140 | uint32_t dsp_bit_width_enforce_mode = 0; |
| 2141 | |
| 2142 | if (!mixer) { |
| 2143 | ALOGE("%s: adev mixer is null. cannot update DSP bitwidth.\n", |
| 2144 | __func__); |
| 2145 | return 0; |
| 2146 | } |
| 2147 | |
| 2148 | if (property_get("persist.vendor.audio_hal.dsp_bit_width_enforce_mode", |
| 2149 | value, NULL) > 0) { |
| 2150 | trial = atoi(value); |
| 2151 | switch (trial) { |
| 2152 | case 16: |
| 2153 | dsp_bit_width_enforce_mode = 16; |
| 2154 | break; |
| 2155 | case 24: |
| 2156 | dsp_bit_width_enforce_mode = 24; |
| 2157 | break; |
| 2158 | case 32: |
| 2159 | dsp_bit_width_enforce_mode = 32; |
| 2160 | break; |
| 2161 | default: |
| 2162 | dsp_bit_width_enforce_mode = 0; |
| 2163 | ALOGD("%s Dynamic DSP bitwidth config is disabled.", __func__); |
| 2164 | break; |
| 2165 | } |
| 2166 | } |
| 2167 | |
| 2168 | return dsp_bit_width_enforce_mode; |
| 2169 | } |
| 2170 | |
| 2171 | static void audio_enable_asm_bit_width_enforce_mode(struct mixer *mixer, |
| 2172 | uint32_t enforce_mode, |
| 2173 | bool enable) |
| 2174 | { |
| 2175 | struct mixer_ctl *ctl = NULL; |
| 2176 | const char *mixer_ctl_name = "ASM Bit Width"; |
| 2177 | uint32_t asm_bit_width_mode = 0; |
| 2178 | |
| 2179 | if (enforce_mode == 0) { |
| 2180 | ALOGD("%s: DSP bitwidth feature is disabled.", __func__); |
| 2181 | return; |
| 2182 | } |
| 2183 | |
| 2184 | ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name); |
| 2185 | if (!ctl) { |
| 2186 | ALOGE("%s: Could not get ctl for mixer cmd - %s", |
| 2187 | __func__, mixer_ctl_name); |
| 2188 | return; |
| 2189 | } |
| 2190 | |
| 2191 | if (enable) |
| 2192 | asm_bit_width_mode = enforce_mode; |
| 2193 | else |
| 2194 | asm_bit_width_mode = 0; |
| 2195 | |
| 2196 | ALOGV("%s DSP bit width feature status is %d width=%d", |
| 2197 | __func__, enable, asm_bit_width_mode); |
| 2198 | if (mixer_ctl_set_value(ctl, 0, asm_bit_width_mode) < 0) |
| 2199 | ALOGE("%s: Could not set ASM biwidth %d", __func__, |
| 2200 | asm_bit_width_mode); |
| 2201 | |
| 2202 | return; |
| 2203 | } |
| 2204 | |
Preetam Singh Ranawat | cb6212e | 2016-07-19 18:33:53 +0530 | [diff] [blame] | 2205 | /* |
| 2206 | * if native DSD playback active |
| 2207 | */ |
| 2208 | bool audio_is_dsd_native_stream_active(struct audio_device *adev) |
| 2209 | { |
| 2210 | bool active = false; |
| 2211 | struct listnode *node = NULL; |
| 2212 | struct audio_usecase *uc = NULL; |
| 2213 | struct stream_out *curr_out = NULL; |
| 2214 | |
| 2215 | list_for_each(node, &adev->usecase_list) { |
| 2216 | uc = node_to_item(node, struct audio_usecase, list); |
| 2217 | curr_out = (struct stream_out*) uc->stream.out; |
| 2218 | |
| 2219 | if (curr_out && PCM_PLAYBACK == uc->type && |
| 2220 | (DSD_NATIVE_BACKEND == platform_get_backend_index(uc->out_snd_device))) { |
| 2221 | active = true; |
| 2222 | ALOGV("%s:DSD playback is active", __func__); |
Preetam Singh Ranawat | f5fbdd6 | 2016-09-29 18:38:31 +0530 | [diff] [blame] | 2223 | break; |
Preetam Singh Ranawat | cb6212e | 2016-07-19 18:33:53 +0530 | [diff] [blame] | 2224 | } |
| 2225 | } |
| 2226 | return active; |
| 2227 | } |
Sidipotu Ashok | e6f78cb | 2015-11-05 14:42:20 +0530 | [diff] [blame] | 2228 | |
| 2229 | static bool force_device_switch(struct audio_usecase *usecase) |
| 2230 | { |
| 2231 | bool ret = false; |
| 2232 | bool is_it_true_mode = false; |
| 2233 | |
Zhou Song | 30f2c3e | 2018-02-08 14:02:15 +0800 | [diff] [blame] | 2234 | if (usecase->type == PCM_CAPTURE || |
Surendar Karka | 93cd25a | 2018-08-28 14:21:37 +0530 | [diff] [blame] | 2235 | usecase->type == TRANSCODE_LOOPBACK_RX || |
| 2236 | usecase->type == TRANSCODE_LOOPBACK_TX) { |
Zhou Song | 30f2c3e | 2018-02-08 14:02:15 +0800 | [diff] [blame] | 2237 | return false; |
| 2238 | } |
| 2239 | |
Aalique Grahame | cbc46a2 | 2017-10-05 10:30:23 -0700 | [diff] [blame] | 2240 | if(usecase->stream.out == NULL) { |
| 2241 | ALOGE("%s: stream.out is NULL", __func__); |
| 2242 | return false; |
| 2243 | } |
| 2244 | |
Sidipotu Ashok | e6f78cb | 2015-11-05 14:42:20 +0530 | [diff] [blame] | 2245 | if (is_offload_usecase(usecase->id) && |
Xiaojun Sang | 869f201 | 2016-02-23 16:33:07 +0800 | [diff] [blame] | 2246 | (usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) && |
| 2247 | (usecase->stream.out->devices == AUDIO_DEVICE_OUT_WIRED_HEADSET || |
| 2248 | usecase->stream.out->devices == AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) { |
Sidipotu Ashok | e6f78cb | 2015-11-05 14:42:20 +0530 | [diff] [blame] | 2249 | is_it_true_mode = (NATIVE_AUDIO_MODE_TRUE_44_1 == platform_get_native_support()? true : false); |
| 2250 | if ((is_it_true_mode && !adev->native_playback_enabled) || |
| 2251 | (!is_it_true_mode && adev->native_playback_enabled)){ |
| 2252 | ret = true; |
| 2253 | ALOGD("napb: time to toggle native mode"); |
| 2254 | } |
| 2255 | } |
| 2256 | |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 2257 | // Force all a2dp output devices to reconfigure for proper AFE encode format |
Ashish Jain | c597d10 | 2016-12-12 10:31:34 +0530 | [diff] [blame] | 2258 | //Also handle a case where in earlier a2dp start failed as A2DP stream was |
| 2259 | //in suspended state, hence try to trigger a retry when we again get a routing request. |
Aalique Grahame | cbc46a2 | 2017-10-05 10:30:23 -0700 | [diff] [blame] | 2260 | if((usecase->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) && |
Ashish Jain | c597d10 | 2016-12-12 10:31:34 +0530 | [diff] [blame] | 2261 | audio_extn_a2dp_is_force_device_switch()) { |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 2262 | ALOGD("Force a2dp device switch to update new encoder config"); |
| 2263 | ret = true; |
Florian Pfister | 1a84f31 | 2018-07-19 14:38:18 +0200 | [diff] [blame] | 2264 | } |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 2265 | |
Florian Pfister | 1a84f31 | 2018-07-19 14:38:18 +0200 | [diff] [blame] | 2266 | if (usecase->stream.out->stream_config_changed) { |
Manish Dewangan | 671a420 | 2017-08-18 17:30:46 +0530 | [diff] [blame] | 2267 | ALOGD("Force stream_config_changed to update iec61937 transmission config"); |
| 2268 | return true; |
Florian Pfister | 1a84f31 | 2018-07-19 14:38:18 +0200 | [diff] [blame] | 2269 | } |
Sidipotu Ashok | e6f78cb | 2015-11-05 14:42:20 +0530 | [diff] [blame] | 2270 | return ret; |
| 2271 | } |
| 2272 | |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 2273 | static void stream_app_type_cfg_init(struct stream_app_type_cfg *cfg) |
| 2274 | { |
| 2275 | cfg->gain[0] = cfg->gain[1] = APP_TYPE_GAIN_DEFAULT; |
| 2276 | } |
| 2277 | |
Ashish Jain | 1b9b30c | 2017-05-18 20:57:40 +0530 | [diff] [blame] | 2278 | bool is_btsco_device(snd_device_t out_snd_device, snd_device_t in_snd_device) |
| 2279 | { |
| 2280 | bool ret=false; |
| 2281 | if ((out_snd_device == SND_DEVICE_OUT_BT_SCO || |
Zhou Song | 5657f49 | 2019-08-07 11:30:39 +0800 | [diff] [blame] | 2282 | out_snd_device == SND_DEVICE_OUT_BT_SCO_WB || |
| 2283 | out_snd_device == SND_DEVICE_OUT_BT_SCO_SWB) || |
Ashish Jain | 1b9b30c | 2017-05-18 20:57:40 +0530 | [diff] [blame] | 2284 | in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_WB_NREC || |
| 2285 | in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_WB || |
Zhou Song | 5657f49 | 2019-08-07 11:30:39 +0800 | [diff] [blame] | 2286 | in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_SWB || |
Ashish Jain | 1b9b30c | 2017-05-18 20:57:40 +0530 | [diff] [blame] | 2287 | in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_NREC || |
| 2288 | in_snd_device == SND_DEVICE_IN_BT_SCO_MIC) |
| 2289 | ret = true; |
| 2290 | |
| 2291 | return ret; |
| 2292 | } |
| 2293 | |
| 2294 | bool is_a2dp_device(snd_device_t out_snd_device) |
| 2295 | { |
| 2296 | bool ret=false; |
| 2297 | if (out_snd_device == SND_DEVICE_OUT_BT_A2DP) |
| 2298 | ret = true; |
| 2299 | |
| 2300 | return ret; |
| 2301 | } |
| 2302 | |
| 2303 | bool is_bt_soc_on(struct audio_device *adev) |
| 2304 | { |
| 2305 | struct mixer_ctl *ctl; |
| 2306 | char *mixer_ctl_name = "BT SOC status"; |
| 2307 | ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name); |
| 2308 | bool bt_soc_status = true; |
| 2309 | if (!ctl) { |
| 2310 | ALOGE("%s: Could not get ctl for mixer cmd - %s", |
| 2311 | __func__, mixer_ctl_name); |
| 2312 | /*This is to ensure we dont break targets which dont have the kernel change*/ |
| 2313 | return true; |
| 2314 | } |
| 2315 | bt_soc_status = mixer_ctl_get_value(ctl, 0); |
| 2316 | ALOGD("BT SOC status: %d",bt_soc_status); |
| 2317 | return bt_soc_status; |
| 2318 | } |
| 2319 | |
Zhou Song | 331c8e5 | 2019-08-26 14:16:12 +0800 | [diff] [blame] | 2320 | static int configure_btsco_sample_rate(snd_device_t snd_device) |
| 2321 | { |
| 2322 | struct mixer_ctl *ctl = NULL; |
| 2323 | struct mixer_ctl *ctl_sr_rx = NULL, *ctl_sr_tx = NULL, *ctl_sr = NULL; |
| 2324 | char *rate_str = NULL; |
| 2325 | bool is_rx_dev = true; |
| 2326 | |
| 2327 | if (is_btsco_device(snd_device, snd_device)) { |
| 2328 | ctl_sr_tx = mixer_get_ctl_by_name(adev->mixer, "BT SampleRate TX"); |
| 2329 | ctl_sr_rx = mixer_get_ctl_by_name(adev->mixer, "BT SampleRate RX"); |
| 2330 | if (!ctl_sr_tx || !ctl_sr_rx) { |
| 2331 | ctl_sr = mixer_get_ctl_by_name(adev->mixer, "BT SampleRate"); |
| 2332 | if (!ctl_sr) |
| 2333 | return -ENOSYS; |
| 2334 | } |
| 2335 | |
| 2336 | switch (snd_device) { |
| 2337 | case SND_DEVICE_OUT_BT_SCO: |
| 2338 | rate_str = "KHZ_8"; |
| 2339 | break; |
| 2340 | case SND_DEVICE_IN_BT_SCO_MIC_NREC: |
| 2341 | case SND_DEVICE_IN_BT_SCO_MIC: |
| 2342 | rate_str = "KHZ_8"; |
| 2343 | is_rx_dev = false; |
| 2344 | break; |
| 2345 | case SND_DEVICE_OUT_BT_SCO_WB: |
| 2346 | rate_str = "KHZ_16"; |
| 2347 | break; |
| 2348 | case SND_DEVICE_IN_BT_SCO_MIC_WB_NREC: |
| 2349 | case SND_DEVICE_IN_BT_SCO_MIC_WB: |
| 2350 | rate_str = "KHZ_16"; |
| 2351 | is_rx_dev = false; |
| 2352 | break; |
| 2353 | default: |
| 2354 | return 0; |
| 2355 | } |
| 2356 | |
| 2357 | ctl = (ctl_sr == NULL) ? (is_rx_dev ? ctl_sr_rx : ctl_sr_tx) : ctl_sr; |
| 2358 | if (mixer_ctl_set_enum_by_string(ctl, rate_str) != 0) |
| 2359 | return -ENOSYS; |
| 2360 | } |
| 2361 | return 0; |
| 2362 | } |
| 2363 | |
Ashish Jain | 1b9b30c | 2017-05-18 20:57:40 +0530 | [diff] [blame] | 2364 | int out_standby_l(struct audio_stream *stream); |
| 2365 | |
Eric Laurent | 637e2d4 | 2018-11-15 12:24:31 -0800 | [diff] [blame] | 2366 | struct stream_in *adev_get_active_input(const struct audio_device *adev) |
| 2367 | { |
| 2368 | struct listnode *node; |
| 2369 | struct stream_in *last_active_in = NULL; |
| 2370 | |
| 2371 | /* Get last added active input. |
| 2372 | * TODO: We may use a priority mechanism to pick highest priority active source */ |
| 2373 | list_for_each(node, &adev->usecase_list) |
| 2374 | { |
| 2375 | struct audio_usecase *usecase = node_to_item(node, struct audio_usecase, list); |
| 2376 | if (usecase->type == PCM_CAPTURE && usecase->stream.in != NULL) |
| 2377 | last_active_in = usecase->stream.in; |
| 2378 | } |
| 2379 | |
| 2380 | return last_active_in; |
| 2381 | } |
| 2382 | |
| 2383 | struct stream_in *get_voice_communication_input(const struct audio_device *adev) |
| 2384 | { |
| 2385 | struct listnode *node; |
| 2386 | |
| 2387 | /* First check active inputs with voice communication source and then |
| 2388 | * any input if audio mode is in communication */ |
| 2389 | list_for_each(node, &adev->usecase_list) |
| 2390 | { |
| 2391 | struct audio_usecase *usecase = node_to_item(node, struct audio_usecase, list); |
| 2392 | if (usecase->type == PCM_CAPTURE && usecase->stream.in != NULL && |
| 2393 | usecase->stream.in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) |
| 2394 | return usecase->stream.in; |
| 2395 | } |
| 2396 | if (adev->mode == AUDIO_MODE_IN_COMMUNICATION) |
| 2397 | return adev_get_active_input(adev); |
| 2398 | |
| 2399 | return NULL; |
| 2400 | } |
| 2401 | |
Carter Hsu | 2e429db | 2019-05-14 18:50:52 +0800 | [diff] [blame] | 2402 | /* |
| 2403 | * Aligned with policy.h |
| 2404 | */ |
| 2405 | static inline int source_priority(int inputSource) |
| 2406 | { |
| 2407 | switch (inputSource) { |
| 2408 | case AUDIO_SOURCE_VOICE_COMMUNICATION: |
| 2409 | return 9; |
| 2410 | case AUDIO_SOURCE_CAMCORDER: |
| 2411 | return 8; |
| 2412 | case AUDIO_SOURCE_VOICE_PERFORMANCE: |
| 2413 | return 7; |
| 2414 | case AUDIO_SOURCE_UNPROCESSED: |
| 2415 | return 6; |
| 2416 | case AUDIO_SOURCE_MIC: |
| 2417 | return 5; |
| 2418 | case AUDIO_SOURCE_ECHO_REFERENCE: |
| 2419 | return 4; |
| 2420 | case AUDIO_SOURCE_FM_TUNER: |
| 2421 | return 3; |
| 2422 | case AUDIO_SOURCE_VOICE_RECOGNITION: |
| 2423 | return 2; |
| 2424 | case AUDIO_SOURCE_HOTWORD: |
| 2425 | return 1; |
| 2426 | default: |
| 2427 | break; |
| 2428 | } |
| 2429 | return 0; |
| 2430 | } |
| 2431 | |
| 2432 | static struct stream_in *get_priority_input(struct audio_device *adev) |
| 2433 | { |
| 2434 | struct listnode *node; |
| 2435 | struct audio_usecase *usecase; |
| 2436 | int last_priority = 0, priority; |
| 2437 | struct stream_in *priority_in = NULL; |
| 2438 | struct stream_in *in; |
| 2439 | |
| 2440 | list_for_each(node, &adev->usecase_list) { |
| 2441 | usecase = node_to_item(node, struct audio_usecase, list); |
| 2442 | if (usecase->type == PCM_CAPTURE) { |
| 2443 | in = usecase->stream.in; |
| 2444 | if (!in) |
| 2445 | continue; |
| 2446 | priority = source_priority(in->source); |
| 2447 | |
| 2448 | if (priority > last_priority) { |
| 2449 | last_priority = priority; |
| 2450 | priority_in = in; |
| 2451 | } |
| 2452 | } |
| 2453 | } |
| 2454 | return priority_in; |
| 2455 | } |
| 2456 | |
Shiv Maliyappanahalli | 34b585f | 2013-10-01 15:49:05 -0700 | [diff] [blame] | 2457 | int select_devices(struct audio_device *adev, audio_usecase_t uc_id) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2458 | { |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 2459 | snd_device_t out_snd_device = SND_DEVICE_NONE; |
| 2460 | snd_device_t in_snd_device = SND_DEVICE_NONE; |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 2461 | struct audio_usecase *usecase = NULL; |
| 2462 | struct audio_usecase *vc_usecase = NULL; |
Narsinga Rao Chella | 05573b7 | 2013-11-15 15:21:40 -0800 | [diff] [blame] | 2463 | struct audio_usecase *voip_usecase = NULL; |
Vimal Puthanveed | 37b4a1c | 2014-01-07 16:47:47 -0800 | [diff] [blame] | 2464 | struct audio_usecase *hfp_usecase = NULL; |
Siddartha Shaik | 44dd770 | 2017-06-14 12:13:25 +0530 | [diff] [blame] | 2465 | struct stream_out stream_out; |
Vimal Puthanveed | 41fcff2 | 2014-01-23 15:56:53 -0800 | [diff] [blame] | 2466 | audio_usecase_t hfp_ucid; |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 2467 | int status = 0; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2468 | |
Sidipotu Ashok | e6f78cb | 2015-11-05 14:42:20 +0530 | [diff] [blame] | 2469 | ALOGD("%s for use case (%s)", __func__, use_case_table[uc_id]); |
| 2470 | |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 2471 | usecase = get_usecase_from_list(adev, uc_id); |
| 2472 | if (usecase == NULL) { |
| 2473 | ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id); |
| 2474 | return -EINVAL; |
| 2475 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2476 | |
Narsinga Rao Chella | 05573b7 | 2013-11-15 15:21:40 -0800 | [diff] [blame] | 2477 | if ((usecase->type == VOICE_CALL) || |
Vimal Puthanveed | 5b4d3f1 | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 2478 | (usecase->type == VOIP_CALL) || |
| 2479 | (usecase->type == PCM_HFP_CALL)) { |
Aditya Bavanari | bdda2f2 | 2016-10-19 15:02:05 +0530 | [diff] [blame] | 2480 | if(usecase->stream.out == NULL) { |
| 2481 | ALOGE("%s: stream.out is NULL", __func__); |
| 2482 | return -EINVAL; |
| 2483 | } |
Guodong Hu | 267bdf8 | 2019-08-12 19:22:32 +0800 | [diff] [blame] | 2484 | if (usecase->devices & AUDIO_DEVICE_OUT_BUS) { |
| 2485 | out_snd_device = audio_extn_auto_hal_get_output_snd_device(adev, |
| 2486 | uc_id); |
| 2487 | in_snd_device = audio_extn_auto_hal_get_input_snd_device(adev, |
| 2488 | uc_id); |
| 2489 | } else { |
| 2490 | out_snd_device = platform_get_output_snd_device(adev->platform, |
| 2491 | usecase->stream.out); |
| 2492 | in_snd_device = platform_get_input_snd_device(adev->platform, |
| 2493 | NULL, |
| 2494 | usecase->stream.out->devices); |
| 2495 | } |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 2496 | usecase->devices = usecase->stream.out->devices; |
Surendar Karka | 93cd25a | 2018-08-28 14:21:37 +0530 | [diff] [blame] | 2497 | } else if (usecase->type == TRANSCODE_LOOPBACK_RX) { |
Siddartha Shaik | 31b530e | 2017-05-19 15:26:33 +0530 | [diff] [blame] | 2498 | if (usecase->stream.inout == NULL) { |
| 2499 | ALOGE("%s: stream.inout is NULL", __func__); |
| 2500 | return -EINVAL; |
| 2501 | } |
Siddartha Shaik | 44dd770 | 2017-06-14 12:13:25 +0530 | [diff] [blame] | 2502 | stream_out.devices = usecase->stream.inout->out_config.devices; |
| 2503 | stream_out.sample_rate = usecase->stream.inout->out_config.sample_rate; |
| 2504 | stream_out.format = usecase->stream.inout->out_config.format; |
| 2505 | stream_out.channel_mask = usecase->stream.inout->out_config.channel_mask; |
| 2506 | out_snd_device = platform_get_output_snd_device(adev->platform, |
| 2507 | &stream_out); |
Surendar Karka | 93cd25a | 2018-08-28 14:21:37 +0530 | [diff] [blame] | 2508 | usecase->devices = out_snd_device; |
| 2509 | } else if (usecase->type == TRANSCODE_LOOPBACK_TX ) { |
| 2510 | if (usecase->stream.inout == NULL) { |
| 2511 | ALOGE("%s: stream.inout is NULL", __func__); |
| 2512 | return -EINVAL; |
| 2513 | } |
Eric Laurent | 637e2d4 | 2018-11-15 12:24:31 -0800 | [diff] [blame] | 2514 | in_snd_device = platform_get_input_snd_device(adev->platform, NULL, AUDIO_DEVICE_NONE); |
Surendar Karka | 93cd25a | 2018-08-28 14:21:37 +0530 | [diff] [blame] | 2515 | usecase->devices = in_snd_device; |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 2516 | } else { |
| 2517 | /* |
| 2518 | * If the voice call is active, use the sound devices of voice call usecase |
| 2519 | * so that it would not result any device switch. All the usecases will |
| 2520 | * be switched to new device when select_devices() is called for voice call |
| 2521 | * usecase. This is to avoid switching devices for voice call when |
| 2522 | * check_usecases_codec_backend() is called below. |
Alexy Joseph | 79dfa3c | 2016-04-20 18:44:56 -0700 | [diff] [blame] | 2523 | * choose voice call device only if the use case device is |
| 2524 | * also using the codec backend |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 2525 | */ |
Shiv Maliyappanahalli | bb4cf0b | 2016-01-21 11:30:06 -0800 | [diff] [blame] | 2526 | if (voice_is_in_call(adev) && adev->mode != AUDIO_MODE_NORMAL) { |
Shiv Maliyappanahalli | 34b585f | 2013-10-01 15:49:05 -0700 | [diff] [blame] | 2527 | vc_usecase = get_usecase_from_list(adev, |
Narsinga Rao Chella | f928a98 | 2015-03-06 14:57:35 -0800 | [diff] [blame] | 2528 | get_usecase_id_from_usecase_type(adev, VOICE_CALL)); |
Alexy Joseph | dbcddf2 | 2016-05-16 17:54:09 -0700 | [diff] [blame] | 2529 | if ((vc_usecase) && (((vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) && |
| 2530 | (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)) || |
Preetam Singh Ranawat | 097cb1f | 2016-07-19 13:49:04 +0530 | [diff] [blame] | 2531 | ((vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) && |
| 2532 | (usecase->devices & AUDIO_DEVICE_IN_ALL_CODEC_BACKEND)) || |
juyuchen | 2d41599 | 2018-11-16 14:15:16 +0800 | [diff] [blame] | 2533 | (vc_usecase->devices == AUDIO_DEVICE_OUT_HEARING_AID) || |
| 2534 | (usecase->devices == AUDIO_DEVICE_IN_VOICE_CALL))) { |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 2535 | in_snd_device = vc_usecase->in_snd_device; |
| 2536 | out_snd_device = vc_usecase->out_snd_device; |
| 2537 | } |
Narsinga Rao Chella | 05573b7 | 2013-11-15 15:21:40 -0800 | [diff] [blame] | 2538 | } else if (voice_extn_compress_voip_is_active(adev)) { |
yidongh | 02ef86f | 2017-04-21 15:36:04 +0800 | [diff] [blame] | 2539 | bool out_snd_device_backend_match = true; |
yidongh | 47785a8 | 2017-05-08 19:29:29 +0800 | [diff] [blame] | 2540 | voip_usecase = get_usecase_from_list(adev, USECASE_COMPRESS_VOIP_CALL); |
yidongh | 6261d8e | 2017-05-15 17:04:02 +0800 | [diff] [blame] | 2541 | if ((voip_usecase != NULL) && |
| 2542 | (usecase->type == PCM_PLAYBACK) && |
| 2543 | (usecase->stream.out != NULL)) { |
yidongh | 02ef86f | 2017-04-21 15:36:04 +0800 | [diff] [blame] | 2544 | out_snd_device_backend_match = platform_check_backends_match( |
| 2545 | voip_usecase->out_snd_device, |
| 2546 | platform_get_output_snd_device( |
| 2547 | adev->platform, |
| 2548 | usecase->stream.out)); |
| 2549 | } |
Mingming Yin | 2d8aa2e | 2014-08-14 00:00:51 -0700 | [diff] [blame] | 2550 | if ((voip_usecase) && ((voip_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) && |
Zhou Song | 0154bf1 | 2016-08-04 10:48:16 +0800 | [diff] [blame] | 2551 | ((usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) || |
| 2552 | ((usecase->devices & ~AUDIO_DEVICE_BIT_IN) & AUDIO_DEVICE_IN_ALL_CODEC_BACKEND)) && |
yidongh | 02ef86f | 2017-04-21 15:36:04 +0800 | [diff] [blame] | 2553 | out_snd_device_backend_match && |
Mingming Yin | 2d8aa2e | 2014-08-14 00:00:51 -0700 | [diff] [blame] | 2554 | (voip_usecase->stream.out != adev->primary_output))) { |
Narsinga Rao Chella | 05573b7 | 2013-11-15 15:21:40 -0800 | [diff] [blame] | 2555 | in_snd_device = voip_usecase->in_snd_device; |
| 2556 | out_snd_device = voip_usecase->out_snd_device; |
| 2557 | } |
Vimal Puthanveed | 37b4a1c | 2014-01-07 16:47:47 -0800 | [diff] [blame] | 2558 | } else if (audio_extn_hfp_is_active(adev)) { |
Vimal Puthanveed | 41fcff2 | 2014-01-23 15:56:53 -0800 | [diff] [blame] | 2559 | hfp_ucid = audio_extn_hfp_get_usecase(); |
| 2560 | hfp_usecase = get_usecase_from_list(adev, hfp_ucid); |
Mingming Yin | 2d8aa2e | 2014-08-14 00:00:51 -0700 | [diff] [blame] | 2561 | if ((hfp_usecase) && (hfp_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)) { |
Vimal Puthanveed | 37b4a1c | 2014-01-07 16:47:47 -0800 | [diff] [blame] | 2562 | in_snd_device = hfp_usecase->in_snd_device; |
| 2563 | out_snd_device = hfp_usecase->out_snd_device; |
| 2564 | } |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 2565 | } |
| 2566 | if (usecase->type == PCM_PLAYBACK) { |
Aditya Bavanari | bdda2f2 | 2016-10-19 15:02:05 +0530 | [diff] [blame] | 2567 | if (usecase->stream.out == NULL) { |
| 2568 | ALOGE("%s: stream.out is NULL", __func__); |
| 2569 | return -EINVAL; |
| 2570 | } |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 2571 | usecase->devices = usecase->stream.out->devices; |
| 2572 | in_snd_device = SND_DEVICE_NONE; |
Ravi Kumar Alamanda | 59d296d | 2013-05-02 11:25:27 -0700 | [diff] [blame] | 2573 | if (out_snd_device == SND_DEVICE_NONE) { |
Eric Laurent | 637e2d4 | 2018-11-15 12:24:31 -0800 | [diff] [blame] | 2574 | struct stream_out *voip_out = adev->primary_output; |
| 2575 | struct stream_in *voip_in = get_voice_communication_input(adev); |
Guodong Hu | 267bdf8 | 2019-08-12 19:22:32 +0800 | [diff] [blame] | 2576 | if (usecase->devices & AUDIO_DEVICE_OUT_BUS) |
| 2577 | out_snd_device = audio_extn_auto_hal_get_output_snd_device(adev, uc_id); |
| 2578 | else |
| 2579 | out_snd_device = platform_get_output_snd_device(adev->platform, |
| 2580 | usecase->stream.out); |
kunleiz | dcf967a | 2018-08-07 17:09:11 +0800 | [diff] [blame] | 2581 | voip_usecase = get_usecase_from_list(adev, USECASE_AUDIO_PLAYBACK_VOIP); |
kunleiz | dcf967a | 2018-08-07 17:09:11 +0800 | [diff] [blame] | 2582 | |
Eric Laurent | 637e2d4 | 2018-11-15 12:24:31 -0800 | [diff] [blame] | 2583 | if (voip_usecase) |
| 2584 | voip_out = voip_usecase->stream.out; |
| 2585 | |
| 2586 | if (usecase->stream.out == voip_out && voip_in != NULL) |
| 2587 | select_devices(adev, voip_in->usecase); |
Ravi Kumar Alamanda | 59d296d | 2013-05-02 11:25:27 -0700 | [diff] [blame] | 2588 | } |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 2589 | } else if (usecase->type == PCM_CAPTURE) { |
Aditya Bavanari | bdda2f2 | 2016-10-19 15:02:05 +0530 | [diff] [blame] | 2590 | if (usecase->stream.in == NULL) { |
| 2591 | ALOGE("%s: stream.in is NULL", __func__); |
| 2592 | return -EINVAL; |
| 2593 | } |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 2594 | usecase->devices = usecase->stream.in->device; |
| 2595 | out_snd_device = SND_DEVICE_NONE; |
Ravi Kumar Alamanda | 59d296d | 2013-05-02 11:25:27 -0700 | [diff] [blame] | 2596 | if (in_snd_device == SND_DEVICE_NONE) { |
Ravi Kumar Alamanda | 060bc5a | 2014-09-05 13:51:35 -0700 | [diff] [blame] | 2597 | audio_devices_t out_device = AUDIO_DEVICE_NONE; |
Eric Laurent | 637e2d4 | 2018-11-15 12:24:31 -0800 | [diff] [blame] | 2598 | struct stream_in *voip_in = get_voice_communication_input(adev); |
Carter Hsu | 2e429db | 2019-05-14 18:50:52 +0800 | [diff] [blame] | 2599 | struct stream_in *priority_in = NULL; |
Eric Laurent | 637e2d4 | 2018-11-15 12:24:31 -0800 | [diff] [blame] | 2600 | |
| 2601 | if (voip_in != NULL) { |
Eric Laurent | 637e2d4 | 2018-11-15 12:24:31 -0800 | [diff] [blame] | 2602 | struct audio_usecase *voip_usecase = get_usecase_from_list(adev, |
| 2603 | USECASE_AUDIO_PLAYBACK_VOIP); |
| 2604 | |
Carter Hsu | 2e429db | 2019-05-14 18:50:52 +0800 | [diff] [blame] | 2605 | usecase->stream.in->enable_ec_port = false; |
| 2606 | |
Eric Laurent | 637e2d4 | 2018-11-15 12:24:31 -0800 | [diff] [blame] | 2607 | if (usecase->id == USECASE_AUDIO_RECORD_AFE_PROXY) { |
| 2608 | out_device = AUDIO_DEVICE_OUT_TELEPHONY_TX; |
| 2609 | } else if (voip_usecase) { |
| 2610 | out_device = voip_usecase->stream.out->devices; |
| 2611 | } else if (adev->primary_output && |
| 2612 | !adev->primary_output->standby) { |
| 2613 | out_device = adev->primary_output->devices; |
| 2614 | } else { |
| 2615 | /* forcing speaker o/p device to get matching i/p pair |
| 2616 | in case o/p is not routed from same primary HAL */ |
| 2617 | out_device = AUDIO_DEVICE_OUT_SPEAKER; |
| 2618 | } |
Carter Hsu | 2e429db | 2019-05-14 18:50:52 +0800 | [diff] [blame] | 2619 | priority_in = voip_in; |
| 2620 | } else { |
| 2621 | /* get the input with the highest priority source*/ |
| 2622 | priority_in = get_priority_input(adev); |
| 2623 | |
| 2624 | if (!priority_in) |
| 2625 | priority_in = usecase->stream.in; |
Ravi Kumar Alamanda | 59d296d | 2013-05-02 11:25:27 -0700 | [diff] [blame] | 2626 | } |
Carter Hsu | 2e429db | 2019-05-14 18:50:52 +0800 | [diff] [blame] | 2627 | |
Eric Laurent | 637e2d4 | 2018-11-15 12:24:31 -0800 | [diff] [blame] | 2628 | in_snd_device = platform_get_input_snd_device(adev->platform, |
Carter Hsu | 2e429db | 2019-05-14 18:50:52 +0800 | [diff] [blame] | 2629 | priority_in, |
Eric Laurent | 637e2d4 | 2018-11-15 12:24:31 -0800 | [diff] [blame] | 2630 | out_device); |
Ravi Kumar Alamanda | 59d296d | 2013-05-02 11:25:27 -0700 | [diff] [blame] | 2631 | } |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 2632 | } |
| 2633 | } |
| 2634 | |
| 2635 | if (out_snd_device == usecase->out_snd_device && |
| 2636 | in_snd_device == usecase->in_snd_device) { |
Sidipotu Ashok | e6f78cb | 2015-11-05 14:42:20 +0530 | [diff] [blame] | 2637 | |
| 2638 | if (!force_device_switch(usecase)) |
| 2639 | return 0; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2640 | } |
| 2641 | |
Guodong Hu | 267bdf8 | 2019-08-12 19:22:32 +0800 | [diff] [blame] | 2642 | if (!(usecase->devices & AUDIO_DEVICE_OUT_BUS) && |
| 2643 | ((is_btsco_device(out_snd_device,in_snd_device) && !adev->bt_sco_on) || |
| 2644 | (is_a2dp_device(out_snd_device) && !audio_extn_a2dp_source_is_ready()))) { |
| 2645 | ALOGD("SCO/A2DP is selected but they are not connected/ready hence dont route"); |
| 2646 | return 0; |
Ashish Jain | 1b9b30c | 2017-05-18 20:57:40 +0530 | [diff] [blame] | 2647 | } |
| 2648 | |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 2649 | if (out_snd_device != SND_DEVICE_NONE && |
| 2650 | out_snd_device != adev->last_logged_snd_device[uc_id][0]) { |
| 2651 | ALOGD("%s: changing use case %s output device from(%d: %s, acdb %d) to (%d: %s, acdb %d)", |
| 2652 | __func__, |
| 2653 | use_case_table[uc_id], |
| 2654 | adev->last_logged_snd_device[uc_id][0], |
| 2655 | platform_get_snd_device_name(adev->last_logged_snd_device[uc_id][0]), |
| 2656 | adev->last_logged_snd_device[uc_id][0] != SND_DEVICE_NONE ? |
| 2657 | platform_get_snd_device_acdb_id(adev->last_logged_snd_device[uc_id][0]) : |
| 2658 | -1, |
| 2659 | out_snd_device, |
| 2660 | platform_get_snd_device_name(out_snd_device), |
| 2661 | platform_get_snd_device_acdb_id(out_snd_device)); |
| 2662 | adev->last_logged_snd_device[uc_id][0] = out_snd_device; |
| 2663 | } |
| 2664 | if (in_snd_device != SND_DEVICE_NONE && |
| 2665 | in_snd_device != adev->last_logged_snd_device[uc_id][1]) { |
| 2666 | ALOGD("%s: changing use case %s input device from(%d: %s, acdb %d) to (%d: %s, acdb %d)", |
| 2667 | __func__, |
| 2668 | use_case_table[uc_id], |
| 2669 | adev->last_logged_snd_device[uc_id][1], |
| 2670 | platform_get_snd_device_name(adev->last_logged_snd_device[uc_id][1]), |
| 2671 | adev->last_logged_snd_device[uc_id][1] != SND_DEVICE_NONE ? |
| 2672 | platform_get_snd_device_acdb_id(adev->last_logged_snd_device[uc_id][1]) : |
| 2673 | -1, |
| 2674 | in_snd_device, |
| 2675 | platform_get_snd_device_name(in_snd_device), |
| 2676 | platform_get_snd_device_acdb_id(in_snd_device)); |
| 2677 | adev->last_logged_snd_device[uc_id][1] = in_snd_device; |
| 2678 | } |
| 2679 | |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 2680 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2681 | /* |
| 2682 | * Limitation: While in call, to do a device switch we need to disable |
| 2683 | * and enable both RX and TX devices though one of them is same as current |
| 2684 | * device. |
| 2685 | */ |
Vidyakumar Athota | 21b3bb9 | 2014-04-25 11:08:08 -0700 | [diff] [blame] | 2686 | if ((usecase->type == VOICE_CALL) && |
| 2687 | (usecase->in_snd_device != SND_DEVICE_NONE) && |
| 2688 | (usecase->out_snd_device != SND_DEVICE_NONE)) { |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 2689 | status = platform_switch_voice_call_device_pre(adev->platform); |
Narsinga Rao Chella | 116142b | 2015-08-14 18:00:08 -0700 | [diff] [blame] | 2690 | } |
| 2691 | |
| 2692 | if (((usecase->type == VOICE_CALL) || |
| 2693 | (usecase->type == VOIP_CALL)) && |
| 2694 | (usecase->out_snd_device != SND_DEVICE_NONE)) { |
| 2695 | /* Disable sidetone only if voice/voip call already exists */ |
| 2696 | if (voice_is_call_state_active(adev) || |
| 2697 | voice_extn_compress_voip_is_started(adev)) |
Bhalchandra Gajare | 45fee28 | 2015-06-09 22:23:45 -0700 | [diff] [blame] | 2698 | voice_set_sidetone(adev, usecase->out_snd_device, false); |
Vidyakumar Athota | ea269c6 | 2016-10-31 09:05:59 -0700 | [diff] [blame] | 2699 | |
| 2700 | /* Disable aanc only if voice call exists */ |
| 2701 | if (voice_is_call_state_active(adev)) |
| 2702 | voice_check_and_update_aanc_path(adev, usecase->out_snd_device, false); |
Ravi Kumar Alamanda | 610e8cc | 2013-02-12 01:42:38 -0800 | [diff] [blame] | 2703 | } |
| 2704 | |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 2705 | if ((out_snd_device == SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP || |
| 2706 | out_snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP) && |
Florian Pfister | 1a84f31 | 2018-07-19 14:38:18 +0200 | [diff] [blame] | 2707 | (!audio_extn_a2dp_source_is_ready())) { |
Chaithanya Krishna Bacharaju | 69d2e4c | 2017-05-26 18:22:46 +0530 | [diff] [blame] | 2708 | ALOGW("%s: A2DP profile is not ready, routing to speaker only", __func__); |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 2709 | if (out_snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP) |
| 2710 | out_snd_device = SND_DEVICE_OUT_SPEAKER_SAFE; |
| 2711 | else |
| 2712 | out_snd_device = SND_DEVICE_OUT_SPEAKER; |
Chaithanya Krishna Bacharaju | 69d2e4c | 2017-05-26 18:22:46 +0530 | [diff] [blame] | 2713 | } |
| 2714 | |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 2715 | /* Disable current sound devices */ |
| 2716 | if (usecase->out_snd_device != SND_DEVICE_NONE) { |
Haynes Mathew George | 1376ca6 | 2014-04-24 11:55:48 -0700 | [diff] [blame] | 2717 | disable_audio_route(adev, usecase); |
| 2718 | disable_snd_device(adev, usecase->out_snd_device); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2719 | } |
| 2720 | |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 2721 | if (usecase->in_snd_device != SND_DEVICE_NONE) { |
Haynes Mathew George | 1376ca6 | 2014-04-24 11:55:48 -0700 | [diff] [blame] | 2722 | disable_audio_route(adev, usecase); |
| 2723 | disable_snd_device(adev, usecase->in_snd_device); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2724 | } |
| 2725 | |
Vidyakumar Athota | 545dbd3 | 2013-11-13 17:30:53 -0800 | [diff] [blame] | 2726 | /* Applicable only on the targets that has external modem. |
| 2727 | * New device information should be sent to modem before enabling |
| 2728 | * the devices to reduce in-call device switch time. |
| 2729 | */ |
Vidyakumar Athota | 21b3bb9 | 2014-04-25 11:08:08 -0700 | [diff] [blame] | 2730 | if ((usecase->type == VOICE_CALL) && |
| 2731 | (usecase->in_snd_device != SND_DEVICE_NONE) && |
| 2732 | (usecase->out_snd_device != SND_DEVICE_NONE)) { |
Vidyakumar Athota | 545dbd3 | 2013-11-13 17:30:53 -0800 | [diff] [blame] | 2733 | status = platform_switch_voice_call_enable_device_config(adev->platform, |
| 2734 | out_snd_device, |
| 2735 | in_snd_device); |
Vidyakumar Athota | 21b3bb9 | 2014-04-25 11:08:08 -0700 | [diff] [blame] | 2736 | } |
Vidyakumar Athota | 545dbd3 | 2013-11-13 17:30:53 -0800 | [diff] [blame] | 2737 | |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 2738 | /* Enable new sound devices */ |
| 2739 | if (out_snd_device != SND_DEVICE_NONE) { |
Kuirong Wang | a9f7cee | 2016-03-07 11:21:52 -0800 | [diff] [blame] | 2740 | check_usecases_codec_backend(adev, usecase, out_snd_device); |
Preetam Singh Ranawat | 43eac68 | 2017-03-07 18:19:02 +0530 | [diff] [blame] | 2741 | if (platform_check_codec_asrc_support(adev->platform)) |
| 2742 | check_and_set_asrc_mode(adev, usecase, out_snd_device); |
Haynes Mathew George | 1376ca6 | 2014-04-24 11:55:48 -0700 | [diff] [blame] | 2743 | enable_snd_device(adev, out_snd_device); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2744 | } |
| 2745 | |
Ravi Kumar Alamanda | c4ba743 | 2013-06-05 14:11:39 -0700 | [diff] [blame] | 2746 | if (in_snd_device != SND_DEVICE_NONE) { |
Manish Dewangan | ba9fcfa | 2016-03-24 16:20:06 +0530 | [diff] [blame] | 2747 | check_usecases_capture_codec_backend(adev, usecase, in_snd_device); |
Haynes Mathew George | 1376ca6 | 2014-04-24 11:55:48 -0700 | [diff] [blame] | 2748 | enable_snd_device(adev, in_snd_device); |
Ravi Kumar Alamanda | c4ba743 | 2013-06-05 14:11:39 -0700 | [diff] [blame] | 2749 | } |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 2750 | |
Avinash Vaish | 71a8b97 | 2014-07-24 15:36:33 +0530 | [diff] [blame] | 2751 | if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) { |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 2752 | status = platform_switch_voice_call_device_post(adev->platform, |
| 2753 | out_snd_device, |
| 2754 | in_snd_device); |
Avinash Vaish | 71a8b97 | 2014-07-24 15:36:33 +0530 | [diff] [blame] | 2755 | enable_audio_route_for_voice_usecases(adev, usecase); |
| 2756 | } |
Ravi Kumar Alamanda | 610e8cc | 2013-02-12 01:42:38 -0800 | [diff] [blame] | 2757 | |
sangwoo | 170731f | 2013-06-08 15:36:36 +0900 | [diff] [blame] | 2758 | usecase->in_snd_device = in_snd_device; |
| 2759 | usecase->out_snd_device = out_snd_device; |
| 2760 | |
Dhananjay Kumar | d6d3215 | 2016-10-13 16:11:03 +0530 | [diff] [blame] | 2761 | audio_extn_utils_update_stream_app_type_cfg_for_usecase(adev, |
| 2762 | usecase); |
Preetam Singh Ranawat | a4a37d8 | 2014-09-25 16:56:38 +0530 | [diff] [blame] | 2763 | if (usecase->type == PCM_PLAYBACK) { |
Weiyin Jiang | 6f4c806 | 2016-11-23 15:30:29 +0800 | [diff] [blame] | 2764 | if ((24 == usecase->stream.out->bit_width) && |
| 2765 | (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER)) { |
| 2766 | usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE; |
| 2767 | } else if ((out_snd_device == SND_DEVICE_OUT_HDMI || |
| 2768 | out_snd_device == SND_DEVICE_OUT_USB_HEADSET || |
| 2769 | out_snd_device == SND_DEVICE_OUT_DISPLAY_PORT) && |
| 2770 | (usecase->stream.out->sample_rate >= OUTPUT_SAMPLING_RATE_44100)) { |
| 2771 | /* |
| 2772 | * To best utlize DSP, check if the stream sample rate is supported/multiple of |
| 2773 | * configured device sample rate, if not update the COPP rate to be equal to the |
| 2774 | * device sample rate, else open COPP at stream sample rate |
| 2775 | */ |
| 2776 | platform_check_and_update_copp_sample_rate(adev->platform, out_snd_device, |
| 2777 | usecase->stream.out->sample_rate, |
| 2778 | &usecase->stream.out->app_type_cfg.sample_rate); |
Ashish Jain | 4826f6c | 2017-02-06 13:33:20 +0530 | [diff] [blame] | 2779 | } else if (((out_snd_device != SND_DEVICE_OUT_HEADPHONES_44_1 && |
Preetam Singh Ranawat | 590d043 | 2019-09-30 14:39:47 +0530 | [diff] [blame] | 2780 | out_snd_device != SND_DEVICE_OUT_HEADPHONES && |
| 2781 | out_snd_device != SND_DEVICE_OUT_HEADPHONES_HIFI_FILTER && |
Ashish Jain | 4826f6c | 2017-02-06 13:33:20 +0530 | [diff] [blame] | 2782 | !audio_is_true_native_stream_active(adev)) && |
Weiyin Jiang | 6f4c806 | 2016-11-23 15:30:29 +0800 | [diff] [blame] | 2783 | usecase->stream.out->sample_rate == OUTPUT_SAMPLING_RATE_44100) || |
| 2784 | (usecase->stream.out->sample_rate < OUTPUT_SAMPLING_RATE_44100)) { |
| 2785 | usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE; |
| 2786 | } |
Weiyin Jiang | cdece20 | 2019-07-08 16:13:16 +0800 | [diff] [blame] | 2787 | } |
| 2788 | enable_audio_route(adev, usecase); |
Weiyin Jiang | 6f4c806 | 2016-11-23 15:30:29 +0800 | [diff] [blame] | 2789 | |
Arun Mirpuri | b1bec9c | 2019-01-29 16:42:45 -0800 | [diff] [blame] | 2790 | audio_extn_qdsp_set_device(usecase); |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 2791 | |
Vikram Panduranga | df59cae | 2017-08-03 18:04:55 -0700 | [diff] [blame] | 2792 | /* If input stream is already running then effect needs to be |
| 2793 | applied on the new input device that's being enabled here. */ |
Eric Laurent | 637e2d4 | 2018-11-15 12:24:31 -0800 | [diff] [blame] | 2794 | if (in_snd_device != SND_DEVICE_NONE) |
Vikram Panduranga | df59cae | 2017-08-03 18:04:55 -0700 | [diff] [blame] | 2795 | check_and_enable_effect(adev); |
| 2796 | |
Vidyakumar Athota | 493f289 | 2016-08-14 11:56:55 -0700 | [diff] [blame] | 2797 | if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) { |
Vidyakumar Athota | ea269c6 | 2016-10-31 09:05:59 -0700 | [diff] [blame] | 2798 | /* Enable aanc only if voice call exists */ |
| 2799 | if (voice_is_call_state_active(adev)) |
| 2800 | voice_check_and_update_aanc_path(adev, out_snd_device, true); |
| 2801 | |
Vidyakumar Athota | 493f289 | 2016-08-14 11:56:55 -0700 | [diff] [blame] | 2802 | /* Enable sidetone only if other voice/voip call already exists */ |
| 2803 | if (voice_is_call_state_active(adev) || |
| 2804 | voice_extn_compress_voip_is_started(adev)) |
| 2805 | voice_set_sidetone(adev, out_snd_device, true); |
| 2806 | } |
| 2807 | |
Vidyakumar Athota | 1fd2179 | 2013-11-15 14:50:57 -0800 | [diff] [blame] | 2808 | /* Applicable only on the targets that has external modem. |
| 2809 | * Enable device command should be sent to modem only after |
| 2810 | * enabling voice call mixer controls |
| 2811 | */ |
Vidyakumar Athota | 339342f | 2014-07-01 15:30:57 -0700 | [diff] [blame] | 2812 | if (usecase->type == VOICE_CALL) |
Vidyakumar Athota | 1fd2179 | 2013-11-15 14:50:57 -0800 | [diff] [blame] | 2813 | status = platform_switch_voice_call_usecase_route_post(adev->platform, |
| 2814 | out_snd_device, |
| 2815 | in_snd_device); |
Ashish Jain | 1b9b30c | 2017-05-18 20:57:40 +0530 | [diff] [blame] | 2816 | |
| 2817 | if (is_btsco_device(out_snd_device, in_snd_device) || is_a2dp_device(out_snd_device)) { |
Eric Laurent | 637e2d4 | 2018-11-15 12:24:31 -0800 | [diff] [blame] | 2818 | struct stream_in *in = adev_get_active_input(adev); |
Ashish Jain | 1b9b30c | 2017-05-18 20:57:40 +0530 | [diff] [blame] | 2819 | if (usecase->type == VOIP_CALL) { |
Eric Laurent | 637e2d4 | 2018-11-15 12:24:31 -0800 | [diff] [blame] | 2820 | if (in != NULL && !in->standby) { |
Ashish Jain | 1b9b30c | 2017-05-18 20:57:40 +0530 | [diff] [blame] | 2821 | if (is_bt_soc_on(adev) == false){ |
| 2822 | ALOGD("BT SCO MIC disconnected while in connection"); |
Eric Laurent | 637e2d4 | 2018-11-15 12:24:31 -0800 | [diff] [blame] | 2823 | if (in->pcm != NULL) |
| 2824 | pcm_stop(in->pcm); |
Ashish Jain | 1b9b30c | 2017-05-18 20:57:40 +0530 | [diff] [blame] | 2825 | } |
| 2826 | } |
| 2827 | if ((usecase->stream.out != NULL) && (usecase->stream.out != adev->primary_output) |
| 2828 | && usecase->stream.out->started) { |
| 2829 | if (is_bt_soc_on(adev) == false) { |
| 2830 | ALOGD("BT SCO/A2DP disconnected while in connection"); |
| 2831 | out_standby_l(&usecase->stream.out->stream.common); |
| 2832 | } |
| 2833 | } |
| 2834 | } else if ((usecase->stream.out != NULL) && |
| 2835 | !(usecase->stream.out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && |
Surendar Karka | e1dc874 | 2018-11-19 16:23:14 +0530 | [diff] [blame] | 2836 | (usecase->type != TRANSCODE_LOOPBACK_TX) && |
| 2837 | (usecase->type != TRANSCODE_LOOPBACK_RX) && |
Weiyin Jiang | 0d37324 | 2019-07-25 13:18:17 +0800 | [diff] [blame] | 2838 | (usecase->type != PCM_CAPTURE) && |
Ashish Jain | 1b9b30c | 2017-05-18 20:57:40 +0530 | [diff] [blame] | 2839 | usecase->stream.out->started) { |
| 2840 | if (is_bt_soc_on(adev) == false) { |
| 2841 | ALOGD("BT SCO/A2dp disconnected while in connection"); |
| 2842 | out_standby_l(&usecase->stream.out->stream.common); |
| 2843 | } |
| 2844 | } |
| 2845 | } |
| 2846 | |
Yung Ti Su | 70cb824 | 2018-06-22 17:38:47 +0800 | [diff] [blame] | 2847 | if (usecase->type != PCM_CAPTURE && usecase == voip_usecase) { |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 2848 | struct stream_out *voip_out = voip_usecase->stream.out; |
| 2849 | audio_extn_utils_send_app_type_gain(adev, |
| 2850 | voip_out->app_type_cfg.app_type, |
| 2851 | &voip_out->app_type_cfg.gain[0]); |
| 2852 | } |
| 2853 | |
Sidipotu Ashok | f43018c | 2014-05-02 16:21:50 +0530 | [diff] [blame] | 2854 | ALOGD("%s: done",__func__); |
| 2855 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2856 | return status; |
| 2857 | } |
| 2858 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2859 | static int stop_input_stream(struct stream_in *in) |
| 2860 | { |
Satya Krishna Pindiproli | f1cd92b | 2016-04-14 19:05:23 +0530 | [diff] [blame] | 2861 | int ret = 0; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2862 | struct audio_usecase *uc_info; |
Pallavi | d7c7a27 | 2018-01-16 11:22:55 +0530 | [diff] [blame] | 2863 | |
| 2864 | if (in == NULL) { |
| 2865 | ALOGE("%s: stream_in ptr is NULL", __func__); |
| 2866 | return -EINVAL; |
| 2867 | } |
| 2868 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2869 | struct audio_device *adev = in->dev; |
Carter Hsu | 2e429db | 2019-05-14 18:50:52 +0800 | [diff] [blame] | 2870 | struct stream_in *priority_in = NULL; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2871 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 2872 | ALOGV("%s: enter: usecase(%d: %s)", __func__, |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 2873 | in->usecase, use_case_table[in->usecase]); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2874 | uc_info = get_usecase_from_list(adev, in->usecase); |
| 2875 | if (uc_info == NULL) { |
| 2876 | ALOGE("%s: Could not find the usecase (%d) in the list", |
| 2877 | __func__, in->usecase); |
| 2878 | return -EINVAL; |
| 2879 | } |
| 2880 | |
Carter Hsu | 2e429db | 2019-05-14 18:50:52 +0800 | [diff] [blame] | 2881 | priority_in = get_priority_input(adev); |
| 2882 | |
Derek Chen | ea19728 | 2019-01-07 17:35:01 -0800 | [diff] [blame] | 2883 | if (audio_extn_ext_hw_plugin_usecase_stop(adev->ext_hw_plugin, uc_info)) |
| 2884 | ALOGE("%s: failed to stop ext hw plugin", __func__); |
Derek Chen | d253007 | 2014-11-24 12:39:14 -0800 | [diff] [blame] | 2885 | |
Vidyakumar Athota | 2850d53 | 2013-11-19 16:02:12 -0800 | [diff] [blame] | 2886 | /* Close in-call recording streams */ |
| 2887 | voice_check_and_stop_incall_rec_usecase(adev, in); |
| 2888 | |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 2889 | /* 1. Disable stream specific mixer controls */ |
Haynes Mathew George | 1376ca6 | 2014-04-24 11:55:48 -0700 | [diff] [blame] | 2890 | disable_audio_route(adev, uc_info); |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 2891 | |
| 2892 | /* 2. Disable the tx device */ |
Haynes Mathew George | 1376ca6 | 2014-04-24 11:55:48 -0700 | [diff] [blame] | 2893 | disable_snd_device(adev, uc_info->in_snd_device); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2894 | |
Chaithanya Krishna Bacharaju | c9f9971 | 2019-04-16 15:32:52 +0530 | [diff] [blame] | 2895 | if (is_loopback_input_device(in->device)) |
| 2896 | audio_extn_keep_alive_stop(KEEP_ALIVE_OUT_PRIMARY); |
| 2897 | |
Ravi Kumar Alamanda | 3b1816c | 2013-02-27 23:01:21 -0800 | [diff] [blame] | 2898 | list_remove(&uc_info->list); |
| 2899 | free(uc_info); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2900 | |
Carter Hsu | 2e429db | 2019-05-14 18:50:52 +0800 | [diff] [blame] | 2901 | if (priority_in == in) { |
| 2902 | priority_in = get_priority_input(adev); |
| 2903 | if (priority_in) |
| 2904 | select_devices(adev, priority_in->usecase); |
| 2905 | } |
| 2906 | |
Vatsal Bucha | c09ae06 | 2018-11-14 13:25:08 +0530 | [diff] [blame] | 2907 | enable_gcov(); |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 2908 | ALOGV("%s: exit: status(%d)", __func__, ret); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2909 | return ret; |
| 2910 | } |
| 2911 | |
| 2912 | int start_input_stream(struct stream_in *in) |
| 2913 | { |
| 2914 | /* 1. Enable output device and stream routing controls */ |
Eric Laurent | c840063 | 2013-02-14 19:04:54 -0800 | [diff] [blame] | 2915 | int ret = 0; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2916 | struct audio_usecase *uc_info; |
Preetam Singh Ranawat | a87e974 | 2018-02-13 16:52:53 +0530 | [diff] [blame] | 2917 | |
| 2918 | if (in == NULL) { |
| 2919 | ALOGE("%s: stream_in ptr is NULL", __func__); |
| 2920 | return -EINVAL; |
| 2921 | } |
| 2922 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2923 | struct audio_device *adev = in->dev; |
Garmond Leung | e2433c3 | 2017-09-28 21:51:22 -0700 | [diff] [blame] | 2924 | struct pcm_config config = in->config; |
Garmond Leung | 438932f | 2017-10-04 19:35:18 -0700 | [diff] [blame] | 2925 | int usecase = platform_update_usecase_from_source(in->source,in->usecase); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2926 | |
Mingming Yin | 2664a5b | 2015-09-03 10:53:11 -0700 | [diff] [blame] | 2927 | if (get_usecase_from_list(adev, usecase) == NULL) |
| 2928 | in->usecase = usecase; |
Sidipotu Ashok | f43018c | 2014-05-02 16:21:50 +0530 | [diff] [blame] | 2929 | ALOGD("%s: enter: stream(%p)usecase(%d: %s)", |
| 2930 | __func__, &in->stream, in->usecase, use_case_table[in->usecase]); |
Shiv Maliyappanahalli | da10764 | 2013-10-17 11:16:13 -0700 | [diff] [blame] | 2931 | |
Dhananjay Kumar | e6293dd | 2017-05-25 17:25:30 +0530 | [diff] [blame] | 2932 | if (CARD_STATUS_OFFLINE == in->card_status|| |
| 2933 | CARD_STATUS_OFFLINE == adev->card_status) { |
| 2934 | ALOGW("in->card_status or adev->card_status offline, try again"); |
Dhanalakshmi Siddani | 4d57e99 | 2014-07-17 16:37:51 +0530 | [diff] [blame] | 2935 | ret = -EIO; |
Naresh Tanniru | 4c63039 | 2014-05-12 01:05:52 +0530 | [diff] [blame] | 2936 | goto error_config; |
| 2937 | } |
Naresh Tanniru | 4c63039 | 2014-05-12 01:05:52 +0530 | [diff] [blame] | 2938 | |
Ashish Jain | 1b9b30c | 2017-05-18 20:57:40 +0530 | [diff] [blame] | 2939 | if (audio_is_bluetooth_sco_device(in->device)) { |
| 2940 | if (!adev->bt_sco_on) { |
| 2941 | ALOGE("%s: SCO profile is not ready, return error", __func__); |
| 2942 | ret = -EIO; |
| 2943 | goto error_config; |
| 2944 | } |
| 2945 | } |
| 2946 | |
Shiv Maliyappanahalli | da10764 | 2013-10-17 11:16:13 -0700 | [diff] [blame] | 2947 | /* Check if source matches incall recording usecase criteria */ |
| 2948 | ret = voice_check_and_set_incall_rec_usecase(adev, in); |
| 2949 | if (ret) |
| 2950 | goto error_config; |
| 2951 | else |
Mingming Yin | 2664a5b | 2015-09-03 10:53:11 -0700 | [diff] [blame] | 2952 | ALOGV("%s: usecase(%d)", __func__, in->usecase); |
| 2953 | |
| 2954 | if (get_usecase_from_list(adev, in->usecase) != NULL) { |
| 2955 | ALOGE("%s: use case assigned already in use, stream(%p)usecase(%d: %s)", |
| 2956 | __func__, &in->stream, in->usecase, use_case_table[in->usecase]); |
Zhou Song | 4e0704d | 2016-04-20 13:00:14 +0800 | [diff] [blame] | 2957 | return -EINVAL; |
Mingming Yin | 2664a5b | 2015-09-03 10:53:11 -0700 | [diff] [blame] | 2958 | } |
Shiv Maliyappanahalli | da10764 | 2013-10-17 11:16:13 -0700 | [diff] [blame] | 2959 | |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 2960 | in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2961 | if (in->pcm_device_id < 0) { |
| 2962 | ALOGE("%s: Could not find PCM device id for the usecase(%d)", |
| 2963 | __func__, in->usecase); |
Eric Laurent | c840063 | 2013-02-14 19:04:54 -0800 | [diff] [blame] | 2964 | ret = -EINVAL; |
| 2965 | goto error_config; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2966 | } |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 2967 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2968 | uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase)); |
Haynes Mathew George | b51ceb1 | 2014-06-30 13:56:18 -0700 | [diff] [blame] | 2969 | |
| 2970 | if (!uc_info) { |
| 2971 | ret = -ENOMEM; |
| 2972 | goto error_config; |
| 2973 | } |
| 2974 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2975 | uc_info->id = in->usecase; |
| 2976 | uc_info->type = PCM_CAPTURE; |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 2977 | uc_info->stream.in = in; |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 2978 | uc_info->devices = in->device; |
| 2979 | uc_info->in_snd_device = SND_DEVICE_NONE; |
| 2980 | uc_info->out_snd_device = SND_DEVICE_NONE; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2981 | |
Ravi Kumar Alamanda | 3b1816c | 2013-02-27 23:01:21 -0800 | [diff] [blame] | 2982 | list_add_tail(&adev->usecase_list, &uc_info->list); |
Wei Wang | f7ca6c9 | 2017-11-21 14:51:20 -0800 | [diff] [blame] | 2983 | audio_streaming_hint_start(); |
Sudheer Papothi | fa9d228 | 2015-09-17 01:53:25 +0530 | [diff] [blame] | 2984 | audio_extn_perf_lock_acquire(&adev->perf_lock_handle, 0, |
| 2985 | adev->perf_lock_opts, |
| 2986 | adev->perf_lock_opts_size); |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 2987 | select_devices(adev, in->usecase); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2988 | |
Derek Chen | ea19728 | 2019-01-07 17:35:01 -0800 | [diff] [blame] | 2989 | if (audio_extn_ext_hw_plugin_usecase_start(adev->ext_hw_plugin, uc_info)) |
| 2990 | ALOGE("%s: failed to start ext hw plugin", __func__); |
Derek Chen | d253007 | 2014-11-24 12:39:14 -0800 | [diff] [blame] | 2991 | |
Deeraj Soman | 30cc7ae | 2019-03-18 16:26:55 +0530 | [diff] [blame] | 2992 | android_atomic_acquire_cas(true, false, &(in->capture_stopped)); |
| 2993 | |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 2994 | if (audio_extn_cin_attached_usecase(in->usecase)) { |
Manish Dewangan | 46e0798 | 2018-12-13 18:18:59 +0530 | [diff] [blame] | 2995 | ret = audio_extn_cin_open_input_stream(in); |
Dhananjay Kumar | ee4d200 | 2016-10-25 18:02:58 +0530 | [diff] [blame] | 2996 | if (ret) |
| 2997 | goto error_open; |
| 2998 | else |
| 2999 | goto done_open; |
Ravi Kumar Alamanda | 060bc5a | 2014-09-05 13:51:35 -0700 | [diff] [blame] | 3000 | } |
| 3001 | |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 3002 | if (in->usecase == USECASE_AUDIO_RECORD_MMAP) { |
Ravi Kumar Alamanda | 060bc5a | 2014-09-05 13:51:35 -0700 | [diff] [blame] | 3003 | if (in->pcm == NULL || !pcm_is_ready(in->pcm)) { |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 3004 | ALOGE("%s: pcm stream not ready", __func__); |
| 3005 | goto error_open; |
| 3006 | } |
| 3007 | ret = pcm_start(in->pcm); |
| 3008 | if (ret < 0) { |
| 3009 | ALOGE("%s: MMAP pcm_start failed ret %d", __func__, ret); |
| 3010 | goto error_open; |
| 3011 | } |
| 3012 | } else { |
| 3013 | unsigned int flags = PCM_IN | PCM_MONOTONIC; |
| 3014 | unsigned int pcm_open_retry_count = 0; |
| 3015 | |
| 3016 | if (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY) { |
| 3017 | flags |= PCM_MMAP | PCM_NOIRQ; |
| 3018 | pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT; |
| 3019 | } else if (in->realtime) { |
| 3020 | flags |= PCM_MMAP | PCM_NOIRQ; |
| 3021 | } |
| 3022 | |
Garmond Leung | e2433c3 | 2017-09-28 21:51:22 -0700 | [diff] [blame] | 3023 | if (audio_extn_ffv_get_stream() == in) { |
| 3024 | ALOGD("%s: ffv stream, update pcm config", __func__); |
| 3025 | audio_extn_ffv_update_pcm_config(&config); |
| 3026 | } |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 3027 | ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d", |
| 3028 | __func__, adev->snd_card, in->pcm_device_id, in->config.channels); |
| 3029 | |
| 3030 | while (1) { |
Haynes Mathew George | 380745d | 2017-10-04 15:27:45 -0700 | [diff] [blame] | 3031 | ATRACE_BEGIN("pcm_in_open"); |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 3032 | in->pcm = pcm_open(adev->snd_card, in->pcm_device_id, |
Garmond Leung | 438932f | 2017-10-04 19:35:18 -0700 | [diff] [blame] | 3033 | flags, &config); |
Haynes Mathew George | 380745d | 2017-10-04 15:27:45 -0700 | [diff] [blame] | 3034 | ATRACE_END(); |
Satish Babu Patakokila | 54ce83d | 2018-07-06 18:00:37 +0530 | [diff] [blame] | 3035 | if (errno == ENETRESET && !pcm_is_ready(in->pcm)) { |
Sharad Sangle | c6f3255 | 2018-05-04 16:15:38 +0530 | [diff] [blame] | 3036 | ALOGE("%s: pcm_open failed errno:%d\n", __func__, errno); |
| 3037 | adev->card_status = CARD_STATUS_OFFLINE; |
| 3038 | in->card_status = CARD_STATUS_OFFLINE; |
| 3039 | ret = -EIO; |
| 3040 | goto error_open; |
| 3041 | } |
| 3042 | |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 3043 | if (in->pcm == NULL || !pcm_is_ready(in->pcm)) { |
| 3044 | ALOGE("%s: %s", __func__, pcm_get_error(in->pcm)); |
| 3045 | if (in->pcm != NULL) { |
| 3046 | pcm_close(in->pcm); |
| 3047 | in->pcm = NULL; |
| 3048 | } |
Weiyin Jiang | 7219725 | 2019-10-09 11:49:32 +0800 | [diff] [blame] | 3049 | if (pcm_open_retry_count == 0) { |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 3050 | ret = -EIO; |
| 3051 | goto error_open; |
| 3052 | } |
Weiyin Jiang | 7219725 | 2019-10-09 11:49:32 +0800 | [diff] [blame] | 3053 | pcm_open_retry_count--; |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 3054 | usleep(PROXY_OPEN_WAIT_TIME * 1000); |
| 3055 | continue; |
| 3056 | } |
| 3057 | break; |
| 3058 | } |
| 3059 | |
| 3060 | ALOGV("%s: pcm_prepare", __func__); |
Haynes Mathew George | 380745d | 2017-10-04 15:27:45 -0700 | [diff] [blame] | 3061 | ATRACE_BEGIN("pcm_in_prepare"); |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 3062 | ret = pcm_prepare(in->pcm); |
Haynes Mathew George | 380745d | 2017-10-04 15:27:45 -0700 | [diff] [blame] | 3063 | ATRACE_END(); |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 3064 | if (ret < 0) { |
| 3065 | ALOGE("%s: pcm_prepare returned %d", __func__, ret); |
| 3066 | pcm_close(in->pcm); |
| 3067 | in->pcm = NULL; |
| 3068 | goto error_open; |
| 3069 | } |
| 3070 | register_in_stream(in); |
| 3071 | if (in->realtime) { |
Haynes Mathew George | 380745d | 2017-10-04 15:27:45 -0700 | [diff] [blame] | 3072 | ATRACE_BEGIN("pcm_in_start"); |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 3073 | ret = pcm_start(in->pcm); |
Haynes Mathew George | 380745d | 2017-10-04 15:27:45 -0700 | [diff] [blame] | 3074 | ATRACE_END(); |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 3075 | if (ret < 0) { |
| 3076 | ALOGE("%s: RT pcm_start failed ret %d", __func__, ret); |
Ravi Kumar Alamanda | 060bc5a | 2014-09-05 13:51:35 -0700 | [diff] [blame] | 3077 | pcm_close(in->pcm); |
| 3078 | in->pcm = NULL; |
Ravi Kumar Alamanda | 060bc5a | 2014-09-05 13:51:35 -0700 | [diff] [blame] | 3079 | goto error_open; |
| 3080 | } |
Ravi Kumar Alamanda | 060bc5a | 2014-09-05 13:51:35 -0700 | [diff] [blame] | 3081 | } |
Haynes Mathew George | 5beddd4 | 2016-06-27 18:33:40 -0700 | [diff] [blame] | 3082 | } |
| 3083 | |
Vikram Panduranga | df59cae | 2017-08-03 18:04:55 -0700 | [diff] [blame] | 3084 | check_and_enable_effect(adev); |
justinweng | 20fb6d8 | 2019-02-21 18:49:00 -0700 | [diff] [blame] | 3085 | audio_extn_audiozoom_set_microphone_direction(in, in->zoom); |
| 3086 | audio_extn_audiozoom_set_microphone_field_dimension(in, in->direction); |
Vikram Panduranga | df59cae | 2017-08-03 18:04:55 -0700 | [diff] [blame] | 3087 | |
Chaithanya Krishna Bacharaju | c9f9971 | 2019-04-16 15:32:52 +0530 | [diff] [blame] | 3088 | if (is_loopback_input_device(in->device)) |
| 3089 | audio_extn_keep_alive_start(KEEP_ALIVE_OUT_PRIMARY); |
| 3090 | |
Dhananjay Kumar | ee4d200 | 2016-10-25 18:02:58 +0530 | [diff] [blame] | 3091 | done_open: |
Wei Wang | f7ca6c9 | 2017-11-21 14:51:20 -0800 | [diff] [blame] | 3092 | audio_streaming_hint_end(); |
Sudheer Papothi | fa9d228 | 2015-09-17 01:53:25 +0530 | [diff] [blame] | 3093 | audio_extn_perf_lock_release(&adev->perf_lock_handle); |
Ravi Kumar Alamanda | c7d9bef | 2015-09-30 22:27:26 -0700 | [diff] [blame] | 3094 | ALOGD("%s: exit", __func__); |
Vatsal Bucha | c09ae06 | 2018-11-14 13:25:08 +0530 | [diff] [blame] | 3095 | enable_gcov(); |
Eric Laurent | c840063 | 2013-02-14 19:04:54 -0800 | [diff] [blame] | 3096 | return ret; |
| 3097 | |
| 3098 | error_open: |
Wei Wang | f7ca6c9 | 2017-11-21 14:51:20 -0800 | [diff] [blame] | 3099 | audio_streaming_hint_end(); |
Sudheer Papothi | fa9d228 | 2015-09-17 01:53:25 +0530 | [diff] [blame] | 3100 | audio_extn_perf_lock_release(&adev->perf_lock_handle); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3101 | stop_input_stream(in); |
Wei Wang | f7ca6c9 | 2017-11-21 14:51:20 -0800 | [diff] [blame] | 3102 | |
Eric Laurent | c840063 | 2013-02-14 19:04:54 -0800 | [diff] [blame] | 3103 | error_config: |
Laxminath Kasam | 2cb4b75 | 2015-09-24 03:59:15 +0530 | [diff] [blame] | 3104 | /* |
| 3105 | * sleep 50ms to allow sufficient time for kernel |
| 3106 | * drivers to recover incases like SSR. |
| 3107 | */ |
| 3108 | usleep(50000); |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 3109 | ALOGD("%s: exit: status(%d)", __func__, ret); |
Vatsal Bucha | c09ae06 | 2018-11-14 13:25:08 +0530 | [diff] [blame] | 3110 | enable_gcov(); |
Eric Laurent | c840063 | 2013-02-14 19:04:54 -0800 | [diff] [blame] | 3111 | return ret; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3112 | } |
| 3113 | |
Shiv Maliyappanahalli | 736d4ce | 2015-09-28 15:23:06 -0700 | [diff] [blame] | 3114 | void lock_input_stream(struct stream_in *in) |
| 3115 | { |
| 3116 | pthread_mutex_lock(&in->pre_lock); |
| 3117 | pthread_mutex_lock(&in->lock); |
| 3118 | pthread_mutex_unlock(&in->pre_lock); |
| 3119 | } |
| 3120 | |
| 3121 | void lock_output_stream(struct stream_out *out) |
| 3122 | { |
| 3123 | pthread_mutex_lock(&out->pre_lock); |
| 3124 | pthread_mutex_lock(&out->lock); |
| 3125 | pthread_mutex_unlock(&out->pre_lock); |
| 3126 | } |
| 3127 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3128 | /* must be called with out->lock locked */ |
| 3129 | static int send_offload_cmd_l(struct stream_out* out, int command) |
| 3130 | { |
| 3131 | struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd)); |
| 3132 | |
Haynes Mathew George | b51ceb1 | 2014-06-30 13:56:18 -0700 | [diff] [blame] | 3133 | if (!cmd) { |
| 3134 | ALOGE("failed to allocate mem for command 0x%x", command); |
| 3135 | return -ENOMEM; |
| 3136 | } |
| 3137 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3138 | ALOGVV("%s %d", __func__, command); |
| 3139 | |
| 3140 | cmd->cmd = command; |
| 3141 | list_add_tail(&out->offload_cmd_list, &cmd->node); |
| 3142 | pthread_cond_signal(&out->offload_cond); |
| 3143 | return 0; |
| 3144 | } |
| 3145 | |
| 3146 | /* must be called iwth out->lock locked */ |
| 3147 | static void stop_compressed_output_l(struct stream_out *out) |
| 3148 | { |
| 3149 | out->offload_state = OFFLOAD_STATE_IDLE; |
| 3150 | out->playback_started = 0; |
Haynes Mathew George | 352f27b | 2013-07-26 00:00:15 -0700 | [diff] [blame] | 3151 | out->send_new_metadata = 1; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3152 | if (out->compr != NULL) { |
| 3153 | compress_stop(out->compr); |
| 3154 | while (out->offload_thread_blocked) { |
| 3155 | pthread_cond_wait(&out->cond, &out->lock); |
| 3156 | } |
| 3157 | } |
| 3158 | } |
| 3159 | |
Varun Balaraj | e49253e | 2017-07-06 19:48:56 +0530 | [diff] [blame] | 3160 | bool is_interactive_usecase(audio_usecase_t uc_id) |
| 3161 | { |
| 3162 | unsigned int i; |
| 3163 | for (i = 0; i < sizeof(interactive_usecases)/sizeof(interactive_usecases[0]); i++) { |
| 3164 | if (uc_id == interactive_usecases[i]) |
| 3165 | return true; |
| 3166 | } |
| 3167 | return false; |
| 3168 | } |
| 3169 | |
| 3170 | static audio_usecase_t get_interactive_usecase(struct audio_device *adev) |
| 3171 | { |
| 3172 | audio_usecase_t ret_uc = USECASE_INVALID; |
| 3173 | unsigned int intract_uc_index; |
| 3174 | unsigned int num_usecase = sizeof(interactive_usecases)/sizeof(interactive_usecases[0]); |
| 3175 | |
| 3176 | ALOGV("%s: num_usecase: %d", __func__, num_usecase); |
| 3177 | for (intract_uc_index = 0; intract_uc_index < num_usecase; intract_uc_index++) { |
| 3178 | if (!(adev->interactive_usecase_state & (0x1 << intract_uc_index))) { |
| 3179 | adev->interactive_usecase_state |= 0x1 << intract_uc_index; |
| 3180 | ret_uc = interactive_usecases[intract_uc_index]; |
| 3181 | break; |
| 3182 | } |
| 3183 | } |
| 3184 | |
| 3185 | ALOGV("%s: Interactive usecase is %d", __func__, ret_uc); |
| 3186 | return ret_uc; |
| 3187 | } |
| 3188 | |
| 3189 | static void free_interactive_usecase(struct audio_device *adev, |
| 3190 | audio_usecase_t uc_id) |
| 3191 | { |
| 3192 | unsigned int interact_uc_index; |
| 3193 | unsigned int num_usecase = sizeof(interactive_usecases)/sizeof(interactive_usecases[0]); |
| 3194 | |
| 3195 | for (interact_uc_index = 0; interact_uc_index < num_usecase; interact_uc_index++) { |
| 3196 | if (interactive_usecases[interact_uc_index] == uc_id) { |
| 3197 | adev->interactive_usecase_state &= ~(0x1 << interact_uc_index); |
| 3198 | break; |
| 3199 | } |
| 3200 | } |
| 3201 | ALOGV("%s: free Interactive usecase %d", __func__, uc_id); |
| 3202 | } |
| 3203 | |
Subhash Chandra Bose Naripeddy | 16ff4f8 | 2014-04-01 21:03:10 -0700 | [diff] [blame] | 3204 | bool is_offload_usecase(audio_usecase_t uc_id) |
| 3205 | { |
| 3206 | unsigned int i; |
| 3207 | for (i = 0; i < sizeof(offload_usecases)/sizeof(offload_usecases[0]); i++) { |
| 3208 | if (uc_id == offload_usecases[i]) |
| 3209 | return true; |
| 3210 | } |
| 3211 | return false; |
| 3212 | } |
| 3213 | |
Dhananjay Kumar | ac34158 | 2017-02-23 23:42:25 +0530 | [diff] [blame] | 3214 | static audio_usecase_t get_offload_usecase(struct audio_device *adev, bool is_compress) |
Subhash Chandra Bose Naripeddy | 16ff4f8 | 2014-04-01 21:03:10 -0700 | [diff] [blame] | 3215 | { |
vivek mehta | 446c396 | 2015-09-14 10:57:35 -0700 | [diff] [blame] | 3216 | audio_usecase_t ret_uc = USECASE_INVALID; |
| 3217 | unsigned int offload_uc_index; |
Alexy Joseph | b137994 | 2016-01-29 15:49:38 -0800 | [diff] [blame] | 3218 | unsigned int num_usecase = sizeof(offload_usecases)/sizeof(offload_usecases[0]); |
vivek mehta | 446c396 | 2015-09-14 10:57:35 -0700 | [diff] [blame] | 3219 | if (!adev->multi_offload_enable) { |
Dhananjay Kumar | ac34158 | 2017-02-23 23:42:25 +0530 | [diff] [blame] | 3220 | if (!is_compress) |
vivek mehta | 446c396 | 2015-09-14 10:57:35 -0700 | [diff] [blame] | 3221 | ret_uc = USECASE_AUDIO_PLAYBACK_OFFLOAD2; |
| 3222 | else |
| 3223 | ret_uc = USECASE_AUDIO_PLAYBACK_OFFLOAD; |
Subhash Chandra Bose Naripeddy | 16ff4f8 | 2014-04-01 21:03:10 -0700 | [diff] [blame] | 3224 | |
vivek mehta | 446c396 | 2015-09-14 10:57:35 -0700 | [diff] [blame] | 3225 | pthread_mutex_lock(&adev->lock); |
| 3226 | if (get_usecase_from_list(adev, ret_uc) != NULL) |
| 3227 | ret_uc = USECASE_INVALID; |
| 3228 | pthread_mutex_unlock(&adev->lock); |
| 3229 | |
| 3230 | return ret_uc; |
| 3231 | } |
Subhash Chandra Bose Naripeddy | 16ff4f8 | 2014-04-01 21:03:10 -0700 | [diff] [blame] | 3232 | |
| 3233 | ALOGV("%s: num_usecase: %d", __func__, num_usecase); |
vivek mehta | 446c396 | 2015-09-14 10:57:35 -0700 | [diff] [blame] | 3234 | for (offload_uc_index = 0; offload_uc_index < num_usecase; offload_uc_index++) { |
| 3235 | if (!(adev->offload_usecases_state & (0x1 << offload_uc_index))) { |
| 3236 | adev->offload_usecases_state |= 0x1 << offload_uc_index; |
| 3237 | ret_uc = offload_usecases[offload_uc_index]; |
Subhash Chandra Bose Naripeddy | 16ff4f8 | 2014-04-01 21:03:10 -0700 | [diff] [blame] | 3238 | break; |
| 3239 | } |
| 3240 | } |
vivek mehta | 446c396 | 2015-09-14 10:57:35 -0700 | [diff] [blame] | 3241 | |
| 3242 | ALOGV("%s: offload usecase is %d", __func__, ret_uc); |
| 3243 | return ret_uc; |
Subhash Chandra Bose Naripeddy | 16ff4f8 | 2014-04-01 21:03:10 -0700 | [diff] [blame] | 3244 | } |
| 3245 | |
| 3246 | static void free_offload_usecase(struct audio_device *adev, |
| 3247 | audio_usecase_t uc_id) |
| 3248 | { |
vivek mehta | 446c396 | 2015-09-14 10:57:35 -0700 | [diff] [blame] | 3249 | unsigned int offload_uc_index; |
Alexy Joseph | b137994 | 2016-01-29 15:49:38 -0800 | [diff] [blame] | 3250 | unsigned int num_usecase = sizeof(offload_usecases)/sizeof(offload_usecases[0]); |
vivek mehta | 446c396 | 2015-09-14 10:57:35 -0700 | [diff] [blame] | 3251 | |
| 3252 | if (!adev->multi_offload_enable) |
| 3253 | return; |
| 3254 | |
| 3255 | for (offload_uc_index = 0; offload_uc_index < num_usecase; offload_uc_index++) { |
| 3256 | if (offload_usecases[offload_uc_index] == uc_id) { |
| 3257 | adev->offload_usecases_state &= ~(0x1 << offload_uc_index); |
Subhash Chandra Bose Naripeddy | 16ff4f8 | 2014-04-01 21:03:10 -0700 | [diff] [blame] | 3258 | break; |
| 3259 | } |
| 3260 | } |
| 3261 | ALOGV("%s: free offload usecase %d", __func__, uc_id); |
| 3262 | } |
| 3263 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3264 | static void *offload_thread_loop(void *context) |
| 3265 | { |
| 3266 | struct stream_out *out = (struct stream_out *) context; |
| 3267 | struct listnode *item; |
Krishnankutty Kolathappilly | d4f1d13 | 2014-01-06 18:33:58 -0800 | [diff] [blame] | 3268 | int ret = 0; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3269 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3270 | setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO); |
Weiyin Jiang | d9b5a4e | 2019-07-18 17:24:21 +0800 | [diff] [blame] | 3271 | set_sched_policy(0, SP_FOREGROUND); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3272 | prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0); |
| 3273 | |
| 3274 | ALOGV("%s", __func__); |
Shiv Maliyappanahalli | 736d4ce | 2015-09-28 15:23:06 -0700 | [diff] [blame] | 3275 | lock_output_stream(out); |
juyuchen | 391b5fa | 2018-12-12 17:58:09 +0800 | [diff] [blame] | 3276 | out->offload_state = OFFLOAD_STATE_IDLE; |
| 3277 | out->playback_started = 0; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3278 | for (;;) { |
| 3279 | struct offload_cmd *cmd = NULL; |
| 3280 | stream_callback_event_t event; |
| 3281 | bool send_callback = false; |
| 3282 | |
| 3283 | ALOGVV("%s offload_cmd_list %d out->offload_state %d", |
| 3284 | __func__, list_empty(&out->offload_cmd_list), |
| 3285 | out->offload_state); |
| 3286 | if (list_empty(&out->offload_cmd_list)) { |
| 3287 | ALOGV("%s SLEEPING", __func__); |
| 3288 | pthread_cond_wait(&out->offload_cond, &out->lock); |
| 3289 | ALOGV("%s RUNNING", __func__); |
| 3290 | continue; |
| 3291 | } |
| 3292 | |
| 3293 | item = list_head(&out->offload_cmd_list); |
| 3294 | cmd = node_to_item(item, struct offload_cmd, node); |
| 3295 | list_remove(item); |
| 3296 | |
| 3297 | ALOGVV("%s STATE %d CMD %d out->compr %p", |
| 3298 | __func__, out->offload_state, cmd->cmd, out->compr); |
| 3299 | |
| 3300 | if (cmd->cmd == OFFLOAD_CMD_EXIT) { |
| 3301 | free(cmd); |
| 3302 | break; |
| 3303 | } |
| 3304 | |
Haynes Mathew George | ee5836f | 2017-11-21 18:02:10 -0800 | [diff] [blame] | 3305 | // allow OFFLOAD_CMD_ERROR reporting during standby |
| 3306 | // this is needed to handle failures during compress_open |
| 3307 | // Note however that on a pause timeout, the stream is closed |
| 3308 | // and no offload usecase will be active. Therefore this |
| 3309 | // special case is needed for compress_open failures alone |
| 3310 | if (cmd->cmd != OFFLOAD_CMD_ERROR && |
| 3311 | out->compr == NULL) { |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3312 | ALOGE("%s: Compress handle is NULL", __func__); |
Haynes Mathew George | a9abb20 | 2016-06-02 14:13:20 -0700 | [diff] [blame] | 3313 | free(cmd); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3314 | pthread_cond_signal(&out->cond); |
| 3315 | continue; |
| 3316 | } |
| 3317 | out->offload_thread_blocked = true; |
| 3318 | pthread_mutex_unlock(&out->lock); |
| 3319 | send_callback = false; |
| 3320 | switch(cmd->cmd) { |
| 3321 | case OFFLOAD_CMD_WAIT_FOR_BUFFER: |
Apoorv Raghuvanshi | 44bd917 | 2014-05-28 14:50:07 -0700 | [diff] [blame] | 3322 | ALOGD("copl(%p):calling compress_wait", out); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3323 | compress_wait(out->compr, -1); |
Apoorv Raghuvanshi | 44bd917 | 2014-05-28 14:50:07 -0700 | [diff] [blame] | 3324 | ALOGD("copl(%p):out of compress_wait", out); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3325 | send_callback = true; |
| 3326 | event = STREAM_CBK_EVENT_WRITE_READY; |
| 3327 | break; |
| 3328 | case OFFLOAD_CMD_PARTIAL_DRAIN: |
Krishnankutty Kolathappilly | d4f1d13 | 2014-01-06 18:33:58 -0800 | [diff] [blame] | 3329 | ret = compress_next_track(out->compr); |
Sidipotu Ashok | 5582056 | 2014-02-10 16:16:38 +0530 | [diff] [blame] | 3330 | if(ret == 0) { |
Apoorv Raghuvanshi | 44bd917 | 2014-05-28 14:50:07 -0700 | [diff] [blame] | 3331 | ALOGD("copl(%p):calling compress_partial_drain", out); |
Preetam Singh Ranawat | 2d0e463 | 2015-02-02 12:40:59 +0530 | [diff] [blame] | 3332 | ret = compress_partial_drain(out->compr); |
Apoorv Raghuvanshi | 44bd917 | 2014-05-28 14:50:07 -0700 | [diff] [blame] | 3333 | ALOGD("copl(%p):out of compress_partial_drain", out); |
Preetam Singh Ranawat | 2d0e463 | 2015-02-02 12:40:59 +0530 | [diff] [blame] | 3334 | if (ret < 0) |
| 3335 | ret = -errno; |
Sidipotu Ashok | 5582056 | 2014-02-10 16:16:38 +0530 | [diff] [blame] | 3336 | } |
Preetam Singh Ranawat | 2d0e463 | 2015-02-02 12:40:59 +0530 | [diff] [blame] | 3337 | else if (ret == -ETIMEDOUT) |
Aniket Kumar Lata | 3570fb1 | 2017-11-08 15:53:44 -0800 | [diff] [blame] | 3338 | ret = compress_drain(out->compr); |
Krishnankutty Kolathappilly | d4f1d13 | 2014-01-06 18:33:58 -0800 | [diff] [blame] | 3339 | else |
| 3340 | ALOGE("%s: Next track returned error %d",__func__, ret); |
Aniket Kumar Lata | 3570fb1 | 2017-11-08 15:53:44 -0800 | [diff] [blame] | 3341 | if (-ENETRESET != ret && !(-EINTR == ret && |
| 3342 | CARD_STATUS_OFFLINE == out->card_status)) { |
Preetam Singh Ranawat | 2d0e463 | 2015-02-02 12:40:59 +0530 | [diff] [blame] | 3343 | send_callback = true; |
Chaithanya Krishna Bacharaju | a70cb6a | 2015-07-24 14:15:05 +0530 | [diff] [blame] | 3344 | pthread_mutex_lock(&out->lock); |
| 3345 | out->send_new_metadata = 1; |
| 3346 | out->send_next_track_params = true; |
| 3347 | pthread_mutex_unlock(&out->lock); |
Preetam Singh Ranawat | 2d0e463 | 2015-02-02 12:40:59 +0530 | [diff] [blame] | 3348 | event = STREAM_CBK_EVENT_DRAIN_READY; |
| 3349 | ALOGV("copl(%p):send drain callback, ret %d", out, ret); |
| 3350 | } else |
Dhananjay Kumar | e6293dd | 2017-05-25 17:25:30 +0530 | [diff] [blame] | 3351 | ALOGI("%s: Block drain ready event during SSR", __func__); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3352 | break; |
| 3353 | case OFFLOAD_CMD_DRAIN: |
Apoorv Raghuvanshi | 44bd917 | 2014-05-28 14:50:07 -0700 | [diff] [blame] | 3354 | ALOGD("copl(%p):calling compress_drain", out); |
Aniket Kumar Lata | 3570fb1 | 2017-11-08 15:53:44 -0800 | [diff] [blame] | 3355 | ret = compress_drain(out->compr); |
| 3356 | ALOGD("copl(%p):out of compress_drain", out); |
| 3357 | // EINTR check avoids drain interruption due to SSR |
| 3358 | if (-ENETRESET != ret && !(-EINTR == ret && |
| 3359 | CARD_STATUS_OFFLINE == out->card_status)) { |
| 3360 | send_callback = true; |
| 3361 | event = STREAM_CBK_EVENT_DRAIN_READY; |
| 3362 | } else |
| 3363 | ALOGI("%s: Block drain ready event during SSR", __func__); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3364 | break; |
Dhananjay Kumar | e6293dd | 2017-05-25 17:25:30 +0530 | [diff] [blame] | 3365 | case OFFLOAD_CMD_ERROR: |
| 3366 | ALOGD("copl(%p): sending error callback to AF", out); |
| 3367 | send_callback = true; |
| 3368 | event = STREAM_CBK_EVENT_ERROR; |
| 3369 | break; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3370 | default: |
| 3371 | ALOGE("%s unknown command received: %d", __func__, cmd->cmd); |
| 3372 | break; |
| 3373 | } |
Shiv Maliyappanahalli | 736d4ce | 2015-09-28 15:23:06 -0700 | [diff] [blame] | 3374 | lock_output_stream(out); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3375 | out->offload_thread_blocked = false; |
| 3376 | pthread_cond_signal(&out->cond); |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 3377 | if (send_callback && out->client_callback) { |
| 3378 | ALOGVV("%s: sending client_callback event %d", __func__, event); |
| 3379 | out->client_callback(event, NULL, out->client_cookie); |
Eric Laurent | 6e89524 | 2013-09-05 16:10:57 -0700 | [diff] [blame] | 3380 | } |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3381 | free(cmd); |
| 3382 | } |
| 3383 | |
| 3384 | pthread_cond_signal(&out->cond); |
| 3385 | while (!list_empty(&out->offload_cmd_list)) { |
| 3386 | item = list_head(&out->offload_cmd_list); |
| 3387 | list_remove(item); |
| 3388 | free(node_to_item(item, struct offload_cmd, node)); |
| 3389 | } |
| 3390 | pthread_mutex_unlock(&out->lock); |
| 3391 | |
| 3392 | return NULL; |
| 3393 | } |
| 3394 | |
| 3395 | static int create_offload_callback_thread(struct stream_out *out) |
| 3396 | { |
| 3397 | pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL); |
| 3398 | list_init(&out->offload_cmd_list); |
| 3399 | pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL, |
| 3400 | offload_thread_loop, out); |
| 3401 | return 0; |
| 3402 | } |
| 3403 | |
| 3404 | static int destroy_offload_callback_thread(struct stream_out *out) |
| 3405 | { |
Shiv Maliyappanahalli | 736d4ce | 2015-09-28 15:23:06 -0700 | [diff] [blame] | 3406 | lock_output_stream(out); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3407 | stop_compressed_output_l(out); |
| 3408 | send_offload_cmd_l(out, OFFLOAD_CMD_EXIT); |
| 3409 | |
| 3410 | pthread_mutex_unlock(&out->lock); |
| 3411 | pthread_join(out->offload_thread, (void **) NULL); |
| 3412 | pthread_cond_destroy(&out->offload_cond); |
| 3413 | |
| 3414 | return 0; |
| 3415 | } |
| 3416 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3417 | static int stop_output_stream(struct stream_out *out) |
| 3418 | { |
Satya Krishna Pindiproli | f1cd92b | 2016-04-14 19:05:23 +0530 | [diff] [blame] | 3419 | int ret = 0; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3420 | struct audio_usecase *uc_info; |
| 3421 | struct audio_device *adev = out->dev; |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 3422 | bool has_voip_usecase = |
| 3423 | get_usecase_from_list(adev, USECASE_AUDIO_PLAYBACK_VOIP) != NULL; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3424 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 3425 | ALOGV("%s: enter: usecase(%d: %s)", __func__, |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 3426 | out->usecase, use_case_table[out->usecase]); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3427 | uc_info = get_usecase_from_list(adev, out->usecase); |
| 3428 | if (uc_info == NULL) { |
| 3429 | ALOGE("%s: Could not find the usecase (%d) in the list", |
| 3430 | __func__, out->usecase); |
| 3431 | return -EINVAL; |
| 3432 | } |
| 3433 | |
Derek Chen | ea19728 | 2019-01-07 17:35:01 -0800 | [diff] [blame] | 3434 | if (audio_extn_ext_hw_plugin_usecase_stop(adev->ext_hw_plugin, uc_info)) |
| 3435 | ALOGE("%s: failed to stop ext hw plugin", __func__); |
Derek Chen | d253007 | 2014-11-24 12:39:14 -0800 | [diff] [blame] | 3436 | |
Pradnya Chaphekar | 80a8cfb | 2014-10-20 16:17:01 -0700 | [diff] [blame] | 3437 | if (is_offload_usecase(out->usecase) && |
Satish Babu Patakokila | 1caa1b7 | 2016-05-24 13:47:08 +0530 | [diff] [blame] | 3438 | !(audio_extn_passthru_is_passthrough_stream(out))) { |
Subhash Chandra Bose Naripeddy | 1d08916 | 2013-11-13 13:31:50 -0800 | [diff] [blame] | 3439 | if (adev->visualizer_stop_output != NULL) |
| 3440 | adev->visualizer_stop_output(out->handle, out->pcm_device_id); |
Jitendra Naruka | 1b6513f | 2014-11-22 19:34:13 -0800 | [diff] [blame] | 3441 | |
| 3442 | audio_extn_dts_remove_state_notifier_node(out->usecase); |
| 3443 | |
Subhash Chandra Bose Naripeddy | 1d08916 | 2013-11-13 13:31:50 -0800 | [diff] [blame] | 3444 | if (adev->offload_effects_stop_output != NULL) |
| 3445 | adev->offload_effects_stop_output(out->handle, out->pcm_device_id); |
vivek mehta | d15d2bf | 2019-05-17 13:35:10 -0700 | [diff] [blame] | 3446 | } else if (out->usecase == USECASE_AUDIO_PLAYBACK_ULL || |
| 3447 | out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) { |
| 3448 | audio_low_latency_hint_end(); |
Subhash Chandra Bose Naripeddy | 1d08916 | 2013-11-13 13:31:50 -0800 | [diff] [blame] | 3449 | } |
Eric Laurent | c4aef75 | 2013-09-12 17:45:53 -0700 | [diff] [blame] | 3450 | |
Arun Mirpuri | 2ce92a6 | 2018-09-12 18:36:10 -0700 | [diff] [blame] | 3451 | if (out->usecase == USECASE_INCALL_MUSIC_UPLINK || |
| 3452 | out->usecase == USECASE_INCALL_MUSIC_UPLINK2) { |
Arun Mirpuri | ef53ce5 | 2018-09-11 18:00:09 -0700 | [diff] [blame] | 3453 | voice_set_device_mute_flag(adev, false); |
Arun Mirpuri | 2ce92a6 | 2018-09-12 18:36:10 -0700 | [diff] [blame] | 3454 | } |
Arun Mirpuri | ef53ce5 | 2018-09-11 18:00:09 -0700 | [diff] [blame] | 3455 | |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 3456 | /* 1. Get and set stream specific mixer controls */ |
Haynes Mathew George | 1376ca6 | 2014-04-24 11:55:48 -0700 | [diff] [blame] | 3457 | disable_audio_route(adev, uc_info); |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 3458 | |
| 3459 | /* 2. Disable the rx device */ |
Haynes Mathew George | 1376ca6 | 2014-04-24 11:55:48 -0700 | [diff] [blame] | 3460 | disable_snd_device(adev, uc_info->out_snd_device); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3461 | |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 3462 | audio_extn_extspk_update(adev->extspk); |
| 3463 | |
Xiaojun Sang | 785b5da | 2017-08-03 15:52:29 +0800 | [diff] [blame] | 3464 | if (is_offload_usecase(out->usecase)) { |
| 3465 | audio_enable_asm_bit_width_enforce_mode(adev->mixer, |
| 3466 | adev->dsp_bit_width_enforce_mode, |
| 3467 | false); |
| 3468 | } |
Garmond Leung | 5fd0b55 | 2018-04-17 11:56:12 -0700 | [diff] [blame] | 3469 | if (audio_is_usb_out_device(out->devices & AUDIO_DEVICE_OUT_ALL_USB)) { |
| 3470 | ret = audio_extn_usb_check_and_set_svc_int(uc_info, |
| 3471 | false); |
| 3472 | |
| 3473 | if (ret != 0) |
| 3474 | check_usecases_codec_backend(adev, uc_info, uc_info->out_snd_device); |
| 3475 | /* default service interval was successfully updated, |
| 3476 | reopen USB backend with new service interval */ |
| 3477 | ret = 0; |
| 3478 | } |
Xiaojun Sang | 785b5da | 2017-08-03 15:52:29 +0800 | [diff] [blame] | 3479 | |
Ravi Kumar Alamanda | 3b1816c | 2013-02-27 23:01:21 -0800 | [diff] [blame] | 3480 | list_remove(&uc_info->list); |
Ashish Jain | 1b9b30c | 2017-05-18 20:57:40 +0530 | [diff] [blame] | 3481 | out->started = 0; |
Pradnya Chaphekar | 80a8cfb | 2014-10-20 16:17:01 -0700 | [diff] [blame] | 3482 | if (is_offload_usecase(out->usecase) && |
Satish Babu Patakokila | 1caa1b7 | 2016-05-24 13:47:08 +0530 | [diff] [blame] | 3483 | (audio_extn_passthru_is_passthrough_stream(out))) { |
Pradnya Chaphekar | 80a8cfb | 2014-10-20 16:17:01 -0700 | [diff] [blame] | 3484 | ALOGV("Disable passthrough , reset mixer to pcm"); |
| 3485 | /* NO_PASSTHROUGH */ |
| 3486 | out->compr_config.codec->compr_passthr = 0; |
Mingming Yin | 2185465 | 2016-04-13 11:54:02 -0700 | [diff] [blame] | 3487 | audio_extn_passthru_on_stop(out); |
Pradnya Chaphekar | 80a8cfb | 2014-10-20 16:17:01 -0700 | [diff] [blame] | 3488 | audio_extn_dolby_set_dap_bypass(adev, DAP_STATE_ON); |
| 3489 | } |
Eric Laurent | 07eeafd | 2013-10-06 12:52:49 -0700 | [diff] [blame] | 3490 | |
Satish Babu Patakokila | 1caa1b7 | 2016-05-24 13:47:08 +0530 | [diff] [blame] | 3491 | /* Must be called after removing the usecase from list */ |
| 3492 | if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) |
Md Mansoor Ahmed | db1b4f9 | 2018-01-25 18:56:31 +0530 | [diff] [blame] | 3493 | audio_extn_keep_alive_start(KEEP_ALIVE_OUT_HDMI); |
Ashish Jain | d84fd6a | 2016-07-27 12:33:25 +0530 | [diff] [blame] | 3494 | |
Manish Dewangan | 21a850a | 2017-08-14 12:03:55 +0530 | [diff] [blame] | 3495 | if (out->ip_hdlr_handle) { |
Naresh Tanniru | 8581945 | 2017-05-04 18:55:45 -0700 | [diff] [blame] | 3496 | ret = audio_extn_ip_hdlr_intf_close(out->ip_hdlr_handle, true, out); |
| 3497 | if (ret < 0) |
| 3498 | ALOGE("%s: audio_extn_ip_hdlr_intf_close failed %d",__func__, ret); |
| 3499 | } |
| 3500 | |
juyuchen | 2d41599 | 2018-11-16 14:15:16 +0800 | [diff] [blame] | 3501 | /* 1) media + voip output routing to handset must route media back to |
| 3502 | speaker when voip stops. |
| 3503 | 2) trigger voip input to reroute when voip output changes to |
| 3504 | hearing aid. */ |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 3505 | if (has_voip_usecase || |
| 3506 | out->devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) { |
| 3507 | struct listnode *node; |
| 3508 | struct audio_usecase *usecase; |
| 3509 | list_for_each(node, &adev->usecase_list) { |
| 3510 | usecase = node_to_item(node, struct audio_usecase, list); |
juyuchen | 2d41599 | 2018-11-16 14:15:16 +0800 | [diff] [blame] | 3511 | if ((usecase->type == PCM_CAPTURE && |
| 3512 | usecase->id != USECASE_AUDIO_RECORD_VOIP) |
| 3513 | || usecase == uc_info) |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 3514 | continue; |
| 3515 | |
| 3516 | ALOGD("%s: select_devices at usecase(%d: %s) after removing the usecase(%d: %s)", |
| 3517 | __func__, usecase->id, use_case_table[usecase->id], |
| 3518 | out->usecase, use_case_table[out->usecase]); |
| 3519 | select_devices(adev, usecase->id); |
| 3520 | } |
| 3521 | } |
| 3522 | |
Garmond Leung | 5fd0b55 | 2018-04-17 11:56:12 -0700 | [diff] [blame] | 3523 | free(uc_info); |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 3524 | ALOGV("%s: exit: status(%d)", __func__, ret); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3525 | return ret; |
| 3526 | } |
| 3527 | |
Vignesh Kulothungan | a692727 | 2019-02-20 15:17:07 -0800 | [diff] [blame] | 3528 | struct pcm* pcm_open_prepare_helper(unsigned int snd_card, unsigned int pcm_device_id, |
| 3529 | unsigned int flags, unsigned int pcm_open_retry_count, |
| 3530 | struct pcm_config *config) |
| 3531 | { |
| 3532 | struct pcm* pcm = NULL; |
| 3533 | |
| 3534 | while (1) { |
| 3535 | pcm = pcm_open(snd_card, pcm_device_id, flags, config); |
| 3536 | if (pcm == NULL || !pcm_is_ready(pcm)) { |
| 3537 | ALOGE("%s: %s", __func__, pcm_get_error(pcm)); |
| 3538 | if (pcm != NULL) { |
| 3539 | pcm_close(pcm); |
| 3540 | pcm = NULL; |
| 3541 | } |
Weiyin Jiang | 7219725 | 2019-10-09 11:49:32 +0800 | [diff] [blame] | 3542 | if (pcm_open_retry_count == 0) |
Vignesh Kulothungan | a692727 | 2019-02-20 15:17:07 -0800 | [diff] [blame] | 3543 | return NULL; |
| 3544 | |
Weiyin Jiang | 7219725 | 2019-10-09 11:49:32 +0800 | [diff] [blame] | 3545 | pcm_open_retry_count--; |
Vignesh Kulothungan | a692727 | 2019-02-20 15:17:07 -0800 | [diff] [blame] | 3546 | usleep(PROXY_OPEN_WAIT_TIME * 1000); |
| 3547 | continue; |
| 3548 | } |
| 3549 | break; |
| 3550 | } |
| 3551 | |
| 3552 | if (pcm_is_ready(pcm)) { |
| 3553 | int ret = pcm_prepare(pcm); |
| 3554 | if (ret < 0) { |
| 3555 | ALOGE("%s: pcm_prepare returned %d", __func__, ret); |
| 3556 | pcm_close(pcm); |
| 3557 | pcm = NULL; |
| 3558 | } |
| 3559 | } |
| 3560 | |
| 3561 | return pcm; |
| 3562 | } |
| 3563 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3564 | int start_output_stream(struct stream_out *out) |
| 3565 | { |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3566 | int ret = 0; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3567 | struct audio_usecase *uc_info; |
| 3568 | struct audio_device *adev = out->dev; |
Alexy Joseph | 5e4ccbc | 2017-02-21 14:20:12 -0800 | [diff] [blame] | 3569 | char mixer_ctl_name[128]; |
| 3570 | struct mixer_ctl *ctl = NULL; |
| 3571 | char* perf_mode[] = {"ULL", "ULL_PP", "LL"}; |
Chaithanya Krishna Bacharaju | 69d2e4c | 2017-05-26 18:22:46 +0530 | [diff] [blame] | 3572 | bool a2dp_combo = false; |
Vignesh Kulothungan | a692727 | 2019-02-20 15:17:07 -0800 | [diff] [blame] | 3573 | bool is_haptic_usecase = (out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) ? true: false; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3574 | |
Haynes Mathew George | 380745d | 2017-10-04 15:27:45 -0700 | [diff] [blame] | 3575 | ATRACE_BEGIN("start_output_stream"); |
Haynes Mathew George | b51ceb1 | 2014-06-30 13:56:18 -0700 | [diff] [blame] | 3576 | if ((out->usecase < 0) || (out->usecase >= AUDIO_USECASE_MAX)) { |
| 3577 | ret = -EINVAL; |
| 3578 | goto error_config; |
| 3579 | } |
| 3580 | |
Vignesh Kulothungan | a692727 | 2019-02-20 15:17:07 -0800 | [diff] [blame] | 3581 | ALOGD("%s: enter: stream(%p)usecase(%d: %s) devices(%#x) is_haptic_usecase(%d)", |
Sidipotu Ashok | f43018c | 2014-05-02 16:21:50 +0530 | [diff] [blame] | 3582 | __func__, &out->stream, out->usecase, use_case_table[out->usecase], |
Vignesh Kulothungan | a692727 | 2019-02-20 15:17:07 -0800 | [diff] [blame] | 3583 | out->devices, is_haptic_usecase); |
Naresh Tanniru | 4c63039 | 2014-05-12 01:05:52 +0530 | [diff] [blame] | 3584 | |
Dhananjay Kumar | e6293dd | 2017-05-25 17:25:30 +0530 | [diff] [blame] | 3585 | if (CARD_STATUS_OFFLINE == out->card_status || |
| 3586 | CARD_STATUS_OFFLINE == adev->card_status) { |
| 3587 | ALOGW("out->card_status or adev->card_status offline, try again"); |
Dhanalakshmi Siddani | 4d57e99 | 2014-07-17 16:37:51 +0530 | [diff] [blame] | 3588 | ret = -EIO; |
Naresh Tanniru | 4c63039 | 2014-05-12 01:05:52 +0530 | [diff] [blame] | 3589 | goto error_config; |
| 3590 | } |
Naresh Tanniru | 4c63039 | 2014-05-12 01:05:52 +0530 | [diff] [blame] | 3591 | |
Arun Mirpuri | 2ce92a6 | 2018-09-12 18:36:10 -0700 | [diff] [blame] | 3592 | //Update incall music usecase to reflect correct voice session |
| 3593 | if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) { |
| 3594 | ret = voice_extn_check_and_set_incall_music_usecase(adev, out); |
| 3595 | if (ret != 0) { |
| 3596 | ALOGE("%s: Incall music delivery usecase cannot be set error:%d", |
| 3597 | __func__, ret); |
| 3598 | goto error_config; |
| 3599 | } |
| 3600 | } |
| 3601 | |
Preetam Singh Ranawat | a1849ba | 2017-02-06 14:10:11 +0530 | [diff] [blame] | 3602 | if (out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) { |
Florian Pfister | 1a84f31 | 2018-07-19 14:38:18 +0200 | [diff] [blame] | 3603 | if (!audio_extn_a2dp_source_is_ready()) { |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 3604 | if (out->devices & |
| 3605 | (AUDIO_DEVICE_OUT_SPEAKER | AUDIO_DEVICE_OUT_SPEAKER_SAFE)) { |
Chaithanya Krishna Bacharaju | 69d2e4c | 2017-05-26 18:22:46 +0530 | [diff] [blame] | 3606 | a2dp_combo = true; |
Preetam Singh Ranawat | a1849ba | 2017-02-06 14:10:11 +0530 | [diff] [blame] | 3607 | } else { |
Chaithanya Krishna Bacharaju | 69d2e4c | 2017-05-26 18:22:46 +0530 | [diff] [blame] | 3608 | if (!(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) { |
| 3609 | ALOGE("%s: A2DP profile is not ready, return error", __func__); |
| 3610 | ret = -EAGAIN; |
| 3611 | goto error_config; |
| 3612 | } |
Preetam Singh Ranawat | a1849ba | 2017-02-06 14:10:11 +0530 | [diff] [blame] | 3613 | } |
| 3614 | } |
| 3615 | } |
Ashish Jain | 1b9b30c | 2017-05-18 20:57:40 +0530 | [diff] [blame] | 3616 | if (out->devices & AUDIO_DEVICE_OUT_ALL_SCO) { |
| 3617 | if (!adev->bt_sco_on) { |
| 3618 | if (out->devices & AUDIO_DEVICE_OUT_SPEAKER) { |
| 3619 | //combo usecase just by pass a2dp |
| 3620 | ALOGW("%s: SCO is not connected, route it to speaker", __func__); |
| 3621 | out->devices = AUDIO_DEVICE_OUT_SPEAKER; |
| 3622 | } else { |
| 3623 | ALOGE("%s: SCO profile is not ready, return error", __func__); |
| 3624 | ret = -EAGAIN; |
| 3625 | goto error_config; |
| 3626 | } |
| 3627 | } |
| 3628 | } |
| 3629 | |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 3630 | out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3631 | if (out->pcm_device_id < 0) { |
| 3632 | ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)", |
| 3633 | __func__, out->pcm_device_id, out->usecase); |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 3634 | ret = -EINVAL; |
Pradnya Chaphekar | 80a8cfb | 2014-10-20 16:17:01 -0700 | [diff] [blame] | 3635 | goto error_open; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3636 | } |
| 3637 | |
Vignesh Kulothungan | a692727 | 2019-02-20 15:17:07 -0800 | [diff] [blame] | 3638 | if (is_haptic_usecase) { |
| 3639 | adev->haptic_pcm_device_id = platform_get_haptics_pcm_device_id(); |
| 3640 | if (adev->haptic_pcm_device_id < 0) { |
| 3641 | ALOGE("%s: Invalid Haptics pcm device id(%d) for the usecase(%d)", |
| 3642 | __func__, adev->haptic_pcm_device_id, out->usecase); |
| 3643 | ret = -EINVAL; |
| 3644 | goto error_config; |
| 3645 | } |
| 3646 | } |
| 3647 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3648 | uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase)); |
Haynes Mathew George | b51ceb1 | 2014-06-30 13:56:18 -0700 | [diff] [blame] | 3649 | |
| 3650 | if (!uc_info) { |
| 3651 | ret = -ENOMEM; |
| 3652 | goto error_config; |
| 3653 | } |
| 3654 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3655 | uc_info->id = out->usecase; |
| 3656 | uc_info->type = PCM_PLAYBACK; |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 3657 | uc_info->stream.out = out; |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 3658 | uc_info->devices = out->devices; |
| 3659 | uc_info->in_snd_device = SND_DEVICE_NONE; |
| 3660 | uc_info->out_snd_device = SND_DEVICE_NONE; |
Garmond Leung | 5fd0b55 | 2018-04-17 11:56:12 -0700 | [diff] [blame] | 3661 | |
| 3662 | /* This must be called before adding this usecase to the list */ |
| 3663 | if (audio_is_usb_out_device(out->devices & AUDIO_DEVICE_OUT_ALL_USB)) { |
| 3664 | audio_extn_usb_check_and_set_svc_int(uc_info, true); |
| 3665 | /* USB backend is not reopened immediately. |
| 3666 | This is eventually done as part of select_devices */ |
| 3667 | } |
| 3668 | |
Ravi Kumar Alamanda | 3b1816c | 2013-02-27 23:01:21 -0800 | [diff] [blame] | 3669 | list_add_tail(&adev->usecase_list, &uc_info->list); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3670 | |
Wei Wang | f7ca6c9 | 2017-11-21 14:51:20 -0800 | [diff] [blame] | 3671 | audio_streaming_hint_start(); |
Sudheer Papothi | fa9d228 | 2015-09-17 01:53:25 +0530 | [diff] [blame] | 3672 | audio_extn_perf_lock_acquire(&adev->perf_lock_handle, 0, |
| 3673 | adev->perf_lock_opts, |
| 3674 | adev->perf_lock_opts_size); |
Ashish Jain | d84fd6a | 2016-07-27 12:33:25 +0530 | [diff] [blame] | 3675 | |
| 3676 | if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) { |
Md Mansoor Ahmed | db1b4f9 | 2018-01-25 18:56:31 +0530 | [diff] [blame] | 3677 | audio_extn_keep_alive_stop(KEEP_ALIVE_OUT_HDMI); |
Ashish Jain | d84fd6a | 2016-07-27 12:33:25 +0530 | [diff] [blame] | 3678 | if (audio_extn_passthru_is_enabled() && |
| 3679 | audio_extn_passthru_is_passthrough_stream(out)) { |
| 3680 | audio_extn_passthru_on_start(out); |
Ashish Jain | d84fd6a | 2016-07-27 12:33:25 +0530 | [diff] [blame] | 3681 | } |
| 3682 | } |
| 3683 | |
Chaithanya Krishna Bacharaju | 69d2e4c | 2017-05-26 18:22:46 +0530 | [diff] [blame] | 3684 | if ((out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) && |
Florian Pfister | 1a84f31 | 2018-07-19 14:38:18 +0200 | [diff] [blame] | 3685 | (!audio_extn_a2dp_source_is_ready())) { |
Chaithanya Krishna Bacharaju | 69d2e4c | 2017-05-26 18:22:46 +0530 | [diff] [blame] | 3686 | if (!a2dp_combo) { |
| 3687 | check_a2dp_restore_l(adev, out, false); |
| 3688 | } else { |
| 3689 | audio_devices_t dev = out->devices; |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 3690 | if (dev & AUDIO_DEVICE_OUT_SPEAKER_SAFE) |
| 3691 | out->devices = AUDIO_DEVICE_OUT_SPEAKER_SAFE; |
| 3692 | else |
| 3693 | out->devices = AUDIO_DEVICE_OUT_SPEAKER; |
Chaithanya Krishna Bacharaju | 69d2e4c | 2017-05-26 18:22:46 +0530 | [diff] [blame] | 3694 | select_devices(adev, out->usecase); |
| 3695 | out->devices = dev; |
| 3696 | } |
| 3697 | } else { |
| 3698 | select_devices(adev, out->usecase); |
| 3699 | } |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 3700 | |
Arun Mirpuri | 2ce92a6 | 2018-09-12 18:36:10 -0700 | [diff] [blame] | 3701 | if (out->usecase == USECASE_INCALL_MUSIC_UPLINK || |
| 3702 | out->usecase == USECASE_INCALL_MUSIC_UPLINK2) { |
Arun Mirpuri | ef53ce5 | 2018-09-11 18:00:09 -0700 | [diff] [blame] | 3703 | voice_set_device_mute_flag(adev, true); |
Arun Mirpuri | 2ce92a6 | 2018-09-12 18:36:10 -0700 | [diff] [blame] | 3704 | } |
Arun Mirpuri | ef53ce5 | 2018-09-11 18:00:09 -0700 | [diff] [blame] | 3705 | |
Derek Chen | ea19728 | 2019-01-07 17:35:01 -0800 | [diff] [blame] | 3706 | if (audio_extn_ext_hw_plugin_usecase_start(adev->ext_hw_plugin, uc_info)) |
| 3707 | ALOGE("%s: failed to start ext hw plugin", __func__); |
Derek Chen | d253007 | 2014-11-24 12:39:14 -0800 | [diff] [blame] | 3708 | |
Ravi Kumar Alamanda | 060bc5a | 2014-09-05 13:51:35 -0700 | [diff] [blame] | 3709 | ALOGV("%s: Opening PCM device card_id(%d) device_id(%d) format(%#x)", |
| 3710 | __func__, adev->snd_card, out->pcm_device_id, out->config.format); |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 3711 | |
| 3712 | if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) { |
Arun Mirpuri | 5d17087 | 2019-03-26 13:21:31 -0700 | [diff] [blame] | 3713 | ALOGD("%s: Starting MMAP stream", __func__); |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 3714 | if (out->pcm == NULL || !pcm_is_ready(out->pcm)) { |
| 3715 | ALOGE("%s: pcm stream not ready", __func__); |
| 3716 | goto error_open; |
| 3717 | } |
| 3718 | ret = pcm_start(out->pcm); |
| 3719 | if (ret < 0) { |
| 3720 | ALOGE("%s: MMAP pcm_start failed ret %d", __func__, ret); |
| 3721 | goto error_open; |
| 3722 | } |
Arun Mirpuri | 5d17087 | 2019-03-26 13:21:31 -0700 | [diff] [blame] | 3723 | out_set_mmap_volume(&out->stream, out->volume_l, out->volume_r); |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 3724 | } else if (!is_offload_usecase(out->usecase)) { |
Ravi Kumar Alamanda | 060bc5a | 2014-09-05 13:51:35 -0700 | [diff] [blame] | 3725 | unsigned int flags = PCM_OUT; |
| 3726 | unsigned int pcm_open_retry_count = 0; |
| 3727 | if (out->usecase == USECASE_AUDIO_PLAYBACK_AFE_PROXY) { |
| 3728 | flags |= PCM_MMAP | PCM_NOIRQ; |
| 3729 | pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT; |
Haynes Mathew George | 5beddd4 | 2016-06-27 18:33:40 -0700 | [diff] [blame] | 3730 | } else if (out->realtime) { |
Haynes Mathew George | 4ab3ba9 | 2017-12-11 14:49:43 -0800 | [diff] [blame] | 3731 | flags |= PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC; |
Ravi Kumar Alamanda | 060bc5a | 2014-09-05 13:51:35 -0700 | [diff] [blame] | 3732 | } else |
| 3733 | flags |= PCM_MONOTONIC; |
| 3734 | |
Alexy Joseph | 5e4ccbc | 2017-02-21 14:20:12 -0800 | [diff] [blame] | 3735 | if ((adev->vr_audio_mode_enabled) && |
| 3736 | (out->flags & AUDIO_OUTPUT_FLAG_RAW)) { |
| 3737 | snprintf(mixer_ctl_name, sizeof(mixer_ctl_name), |
| 3738 | "PCM_Dev %d Topology", out->pcm_device_id); |
| 3739 | ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name); |
| 3740 | if (!ctl) { |
| 3741 | ALOGI("%s: Could not get ctl for mixer cmd might be ULL - %s", |
| 3742 | __func__, mixer_ctl_name); |
| 3743 | } else { |
| 3744 | //if success use ULLPP |
| 3745 | ALOGI("%s: mixer ctrl %s succeeded setting up ULL for %d", |
| 3746 | __func__, mixer_ctl_name, out->pcm_device_id); |
| 3747 | //There is a still a possibility that some sessions |
| 3748 | // that request for FAST|RAW when 3D audio is active |
| 3749 | //can go through ULLPP. Ideally we expects apps to |
| 3750 | //listen to audio focus and stop concurrent playback |
| 3751 | //Also, we will look for mode flag (voice_in_communication) |
| 3752 | //before enabling the realtime flag. |
| 3753 | mixer_ctl_set_enum_by_string(ctl, perf_mode[1]); |
| 3754 | } |
| 3755 | } |
| 3756 | |
Surendar Karka | 91fa368 | 2018-07-02 18:12:12 +0530 | [diff] [blame] | 3757 | if (out->realtime) |
| 3758 | platform_set_stream_channel_map(adev->platform, out->channel_mask, |
| 3759 | out->pcm_device_id, &out->channel_map_param.channel_map[0]); |
| 3760 | |
Vignesh Kulothungan | a692727 | 2019-02-20 15:17:07 -0800 | [diff] [blame] | 3761 | out->pcm = pcm_open_prepare_helper(adev->snd_card, out->pcm_device_id, |
| 3762 | flags, pcm_open_retry_count, |
| 3763 | &(out->config)); |
| 3764 | if (out->pcm == NULL) { |
| 3765 | ret = -EIO; |
| 3766 | goto error_open; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3767 | } |
Vignesh Kulothungan | a692727 | 2019-02-20 15:17:07 -0800 | [diff] [blame] | 3768 | |
| 3769 | if (is_haptic_usecase) { |
| 3770 | adev->haptic_pcm = pcm_open_prepare_helper(adev->snd_card, |
| 3771 | adev->haptic_pcm_device_id, |
| 3772 | flags, pcm_open_retry_count, |
| 3773 | &(adev->haptics_config)); |
| 3774 | // failure to open haptics pcm shouldnt stop audio, |
| 3775 | // so do not close audio pcm in case of error |
Vignesh Kulothungan | e4039c1 | 2019-05-07 15:51:39 -0700 | [diff] [blame] | 3776 | |
| 3777 | if (property_get_bool("vendor.audio.enable_haptic_audio_sync", false)) { |
| 3778 | ALOGD("%s: enable haptic audio synchronization", __func__); |
| 3779 | platform_set_qtime(adev->platform, out->pcm_device_id, adev->haptic_pcm_device_id); |
| 3780 | } |
Vignesh Kulothungan | a692727 | 2019-02-20 15:17:07 -0800 | [diff] [blame] | 3781 | } |
| 3782 | |
Surendar Karka | 91fa368 | 2018-07-02 18:12:12 +0530 | [diff] [blame] | 3783 | if (!out->realtime) |
| 3784 | platform_set_stream_channel_map(adev->platform, out->channel_mask, |
Surendar Karka | f51b584 | 2018-04-26 11:28:38 +0530 | [diff] [blame] | 3785 | out->pcm_device_id, &out->channel_map_param.channel_map[0]); |
Ravi Kumar Alamanda | 8a0f977 | 2015-06-15 10:35:19 -0700 | [diff] [blame] | 3786 | |
Zhou Song | 2b8f28f | 2017-09-11 10:51:38 +0800 | [diff] [blame] | 3787 | // apply volume for voip playback after path is set up |
| 3788 | if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP) |
| 3789 | out_set_voip_volume(&out->stream, out->volume_l, out->volume_r); |
Ramu Gottipati | 3654709 | 2018-12-28 11:32:09 +0530 | [diff] [blame] | 3790 | else if ((out->usecase == USECASE_AUDIO_PLAYBACK_LOW_LATENCY || out->usecase == USECASE_AUDIO_PLAYBACK_DEEP_BUFFER || |
| 3791 | out->usecase == USECASE_AUDIO_PLAYBACK_ULL) && (out->apply_volume)) { |
Ramu Gottipati | 97bdcfb | 2018-04-13 17:58:24 +0530 | [diff] [blame] | 3792 | out_set_pcm_volume(&out->stream, out->volume_l, out->volume_r); |
| 3793 | out->apply_volume = false; |
Derek Chen | f13dd49 | 2018-11-13 14:53:51 -0800 | [diff] [blame] | 3794 | } else if (audio_extn_auto_hal_is_bus_device_usecase(out->usecase)) { |
| 3795 | out_set_pcm_volume(&out->stream, out->volume_l, out->volume_r); |
Ramu Gottipati | 97bdcfb | 2018-04-13 17:58:24 +0530 | [diff] [blame] | 3796 | } |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3797 | } else { |
Pradnya Chaphekar | 4403bd7 | 2014-09-09 09:50:01 -0700 | [diff] [blame] | 3798 | platform_set_stream_channel_map(adev->platform, out->channel_mask, |
Naresh Tanniru | 29bce4e | 2017-04-27 17:54:30 +0530 | [diff] [blame] | 3799 | out->pcm_device_id, &out->channel_map_param.channel_map[0]); |
Xiaojun Sang | 785b5da | 2017-08-03 15:52:29 +0800 | [diff] [blame] | 3800 | audio_enable_asm_bit_width_enforce_mode(adev->mixer, |
| 3801 | adev->dsp_bit_width_enforce_mode, |
| 3802 | true); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3803 | out->pcm = NULL; |
Haynes Mathew George | 380745d | 2017-10-04 15:27:45 -0700 | [diff] [blame] | 3804 | ATRACE_BEGIN("compress_open"); |
Apoorv Raghuvanshi | 84fa2fe | 2013-12-04 11:57:47 -0800 | [diff] [blame] | 3805 | out->compr = compress_open(adev->snd_card, |
| 3806 | out->pcm_device_id, |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3807 | COMPRESS_IN, &out->compr_config); |
Haynes Mathew George | 380745d | 2017-10-04 15:27:45 -0700 | [diff] [blame] | 3808 | ATRACE_END(); |
Satish Babu Patakokila | 54ce83d | 2018-07-06 18:00:37 +0530 | [diff] [blame] | 3809 | if (errno == ENETRESET && !is_compress_ready(out->compr)) { |
Sharad Sangle | c6f3255 | 2018-05-04 16:15:38 +0530 | [diff] [blame] | 3810 | ALOGE("%s: compress_open failed errno:%d\n", __func__, errno); |
| 3811 | adev->card_status = CARD_STATUS_OFFLINE; |
| 3812 | out->card_status = CARD_STATUS_OFFLINE; |
| 3813 | ret = -EIO; |
| 3814 | goto error_open; |
| 3815 | } |
| 3816 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3817 | if (out->compr && !is_compress_ready(out->compr)) { |
Haynes Mathew George | ee5836f | 2017-11-21 18:02:10 -0800 | [diff] [blame] | 3818 | ALOGE("%s: failed /w error %s", __func__, compress_get_error(out->compr)); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3819 | compress_close(out->compr); |
| 3820 | out->compr = NULL; |
| 3821 | ret = -EIO; |
| 3822 | goto error_open; |
| 3823 | } |
Chaithanya Krishna Bacharaju | a70cb6a | 2015-07-24 14:15:05 +0530 | [diff] [blame] | 3824 | /* compress_open sends params of the track, so reset the flag here */ |
| 3825 | out->is_compr_metadata_avail = false; |
| 3826 | |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 3827 | if (out->client_callback) |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3828 | compress_nonblock(out->compr, out->non_blocking); |
Eric Laurent | c4aef75 | 2013-09-12 17:45:53 -0700 | [diff] [blame] | 3829 | |
Fred Oh | 3f43e74 | 2015-03-04 18:42:34 -0800 | [diff] [blame] | 3830 | /* Since small bufs uses blocking writes, a write will be blocked |
| 3831 | for the default max poll time (20s) in the event of an SSR. |
| 3832 | Reduce the poll time to observe and deal with SSR faster. |
| 3833 | */ |
Ashish Jain | 5106d36 | 2016-05-11 19:23:33 +0530 | [diff] [blame] | 3834 | if (!out->non_blocking) { |
Fred Oh | 3f43e74 | 2015-03-04 18:42:34 -0800 | [diff] [blame] | 3835 | compress_set_max_poll_wait(out->compr, 1000); |
| 3836 | } |
| 3837 | |
Manish Dewangan | 69426c8 | 2017-01-30 17:35:36 +0530 | [diff] [blame] | 3838 | audio_extn_utils_compress_set_render_mode(out); |
Manish Dewangan | 5822938 | 2017-02-02 15:48:41 +0530 | [diff] [blame] | 3839 | audio_extn_utils_compress_set_clk_rec_mode(uc_info); |
Manish Dewangan | 69426c8 | 2017-01-30 17:35:36 +0530 | [diff] [blame] | 3840 | |
Jitendra Naruka | 1b6513f | 2014-11-22 19:34:13 -0800 | [diff] [blame] | 3841 | audio_extn_dts_create_state_notifier_node(out->usecase); |
| 3842 | audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate, |
| 3843 | popcount(out->channel_mask), |
| 3844 | out->playback_started); |
| 3845 | |
Subhash Chandra Bose Naripeddy | 7690c56 | 2013-12-14 00:34:53 -0800 | [diff] [blame] | 3846 | #ifdef DS1_DOLBY_DDP_ENABLED |
Satish Babu Patakokila | 5933e97 | 2017-08-24 12:22:08 +0530 | [diff] [blame] | 3847 | if (audio_extn_utils_is_dolby_format(out->format)) |
Subhash Chandra Bose Naripeddy | 7690c56 | 2013-12-14 00:34:53 -0800 | [diff] [blame] | 3848 | audio_extn_dolby_send_ddp_endp_params(adev); |
| 3849 | #endif |
Preetam Singh Ranawat | d18d883 | 2017-02-08 17:34:54 +0530 | [diff] [blame] | 3850 | if (!(audio_extn_passthru_is_passthrough_stream(out)) && |
| 3851 | (out->sample_rate != 176400 && out->sample_rate <= 192000)) { |
Pradnya Chaphekar | 80a8cfb | 2014-10-20 16:17:01 -0700 | [diff] [blame] | 3852 | if (adev->visualizer_start_output != NULL) |
| 3853 | adev->visualizer_start_output(out->handle, out->pcm_device_id); |
| 3854 | if (adev->offload_effects_start_output != NULL) |
Ashish Jain | 5106d36 | 2016-05-11 19:23:33 +0530 | [diff] [blame] | 3855 | adev->offload_effects_start_output(out->handle, out->pcm_device_id, adev->mixer); |
Jitendra Naruka | 1b6513f | 2014-11-22 19:34:13 -0800 | [diff] [blame] | 3856 | audio_extn_check_and_set_dts_hpx_state(adev); |
Pradnya Chaphekar | 80a8cfb | 2014-10-20 16:17:01 -0700 | [diff] [blame] | 3857 | } |
Derek Chen | f13dd49 | 2018-11-13 14:53:51 -0800 | [diff] [blame] | 3858 | |
| 3859 | if (out->devices & AUDIO_DEVICE_OUT_BUS) { |
| 3860 | /* Update cached volume from media to offload/direct stream */ |
| 3861 | struct listnode *node = NULL; |
| 3862 | list_for_each(node, &adev->active_outputs_list) { |
| 3863 | streams_output_ctxt_t *out_ctxt = node_to_item(node, |
| 3864 | streams_output_ctxt_t, |
| 3865 | list); |
| 3866 | if (out_ctxt->output->usecase == USECASE_AUDIO_PLAYBACK_MEDIA) { |
| 3867 | out->volume_l = out_ctxt->output->volume_l; |
| 3868 | out->volume_r = out_ctxt->output->volume_r; |
| 3869 | } |
| 3870 | } |
| 3871 | out_set_compr_volume(&out->stream, |
| 3872 | out->volume_l, out->volume_r); |
| 3873 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3874 | } |
Haynes Mathew George | 5beddd4 | 2016-06-27 18:33:40 -0700 | [diff] [blame] | 3875 | |
| 3876 | if (ret == 0) { |
| 3877 | register_out_stream(out); |
| 3878 | if (out->realtime) { |
Aniket Kumar Lata | f9f246e | 2017-09-15 15:20:16 -0700 | [diff] [blame] | 3879 | if (out->pcm == NULL || !pcm_is_ready(out->pcm)) { |
| 3880 | ALOGE("%s: pcm stream not ready", __func__); |
| 3881 | goto error_open; |
| 3882 | } |
Haynes Mathew George | 380745d | 2017-10-04 15:27:45 -0700 | [diff] [blame] | 3883 | ATRACE_BEGIN("pcm_start"); |
Haynes Mathew George | 5beddd4 | 2016-06-27 18:33:40 -0700 | [diff] [blame] | 3884 | ret = pcm_start(out->pcm); |
Haynes Mathew George | 380745d | 2017-10-04 15:27:45 -0700 | [diff] [blame] | 3885 | ATRACE_END(); |
Haynes Mathew George | 5beddd4 | 2016-06-27 18:33:40 -0700 | [diff] [blame] | 3886 | if (ret < 0) |
| 3887 | goto error_open; |
| 3888 | } |
| 3889 | } |
Wei Wang | f7ca6c9 | 2017-11-21 14:51:20 -0800 | [diff] [blame] | 3890 | audio_streaming_hint_end(); |
Sudheer Papothi | fa9d228 | 2015-09-17 01:53:25 +0530 | [diff] [blame] | 3891 | audio_extn_perf_lock_release(&adev->perf_lock_handle); |
Ravi Kumar Alamanda | c7d9bef | 2015-09-30 22:27:26 -0700 | [diff] [blame] | 3892 | ALOGD("%s: exit", __func__); |
Ravi Kumar Alamanda | 8a0f977 | 2015-06-15 10:35:19 -0700 | [diff] [blame] | 3893 | |
vivek mehta | d15d2bf | 2019-05-17 13:35:10 -0700 | [diff] [blame] | 3894 | if (out->usecase == USECASE_AUDIO_PLAYBACK_ULL || |
| 3895 | out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) { |
| 3896 | audio_low_latency_hint_start(); |
| 3897 | } |
| 3898 | |
Manish Dewangan | 21a850a | 2017-08-14 12:03:55 +0530 | [diff] [blame] | 3899 | if (out->ip_hdlr_handle) { |
Vidyakumar Athota | 6d65588 | 2017-05-22 18:26:24 -0700 | [diff] [blame] | 3900 | ret = audio_extn_ip_hdlr_intf_open(out->ip_hdlr_handle, true, out, out->usecase); |
Naresh Tanniru | 8581945 | 2017-05-04 18:55:45 -0700 | [diff] [blame] | 3901 | if (ret < 0) |
| 3902 | ALOGE("%s: audio_extn_ip_hdlr_intf_open failed %d",__func__, ret); |
| 3903 | } |
| 3904 | |
Vignesh Kulothungan | 3b5fae5 | 2017-09-25 12:16:30 -0700 | [diff] [blame] | 3905 | // consider a scenario where on pause lower layers are tear down. |
| 3906 | // so on resume, swap mixer control need to be sent only when |
| 3907 | // backend is active, hence rather than sending from enable device |
| 3908 | // sending it from start of streamtream |
| 3909 | |
| 3910 | platform_set_swap_channels(adev, true); |
| 3911 | |
Haynes Mathew George | 380745d | 2017-10-04 15:27:45 -0700 | [diff] [blame] | 3912 | ATRACE_END(); |
Vatsal Bucha | c09ae06 | 2018-11-14 13:25:08 +0530 | [diff] [blame] | 3913 | enable_gcov(); |
Haynes Mathew George | 5beddd4 | 2016-06-27 18:33:40 -0700 | [diff] [blame] | 3914 | return ret; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3915 | error_open: |
Vignesh Kulothungan | a692727 | 2019-02-20 15:17:07 -0800 | [diff] [blame] | 3916 | if (adev->haptic_pcm) { |
| 3917 | pcm_close(adev->haptic_pcm); |
| 3918 | adev->haptic_pcm = NULL; |
| 3919 | } |
Wei Wang | f7ca6c9 | 2017-11-21 14:51:20 -0800 | [diff] [blame] | 3920 | audio_streaming_hint_end(); |
Sudheer Papothi | fa9d228 | 2015-09-17 01:53:25 +0530 | [diff] [blame] | 3921 | audio_extn_perf_lock_release(&adev->perf_lock_handle); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3922 | stop_output_stream(out); |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 3923 | error_config: |
Laxminath Kasam | 2cb4b75 | 2015-09-24 03:59:15 +0530 | [diff] [blame] | 3924 | /* |
| 3925 | * sleep 50ms to allow sufficient time for kernel |
| 3926 | * drivers to recover incases like SSR. |
| 3927 | */ |
| 3928 | usleep(50000); |
Haynes Mathew George | 380745d | 2017-10-04 15:27:45 -0700 | [diff] [blame] | 3929 | ATRACE_END(); |
Vatsal Bucha | c09ae06 | 2018-11-14 13:25:08 +0530 | [diff] [blame] | 3930 | enable_gcov(); |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 3931 | return ret; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3932 | } |
| 3933 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3934 | static int check_input_parameters(uint32_t sample_rate, |
| 3935 | audio_format_t format, |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 3936 | int channel_count, |
| 3937 | bool is_usb_hifi) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3938 | { |
Ravi Kumar Alamanda | fae4211 | 2013-11-07 23:31:54 -0800 | [diff] [blame] | 3939 | int ret = 0; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3940 | |
Manish Dewangan | ba9fcfa | 2016-03-24 16:20:06 +0530 | [diff] [blame] | 3941 | if (((format != AUDIO_FORMAT_PCM_16_BIT) && (format != AUDIO_FORMAT_PCM_8_24_BIT) && |
| 3942 | (format != AUDIO_FORMAT_PCM_24_BIT_PACKED) && (format != AUDIO_FORMAT_PCM_32_BIT) && |
| 3943 | (format != AUDIO_FORMAT_PCM_FLOAT)) && |
Mingming Yin | e62d784 | 2013-10-25 16:26:03 -0700 | [diff] [blame] | 3944 | !voice_extn_compress_voip_is_format_supported(format) && |
Ralf Herz | aec8026 | 2018-07-03 07:08:49 +0200 | [diff] [blame] | 3945 | !audio_extn_compr_cap_format_supported(format) && |
| 3946 | !audio_extn_cin_format_supported(format)) |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 3947 | ret = -EINVAL; |
Ravi Kumar Alamanda | fae4211 | 2013-11-07 23:31:54 -0800 | [diff] [blame] | 3948 | |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 3949 | int max_channel_count = is_usb_hifi ? MAX_HIFI_CHANNEL_COUNT : MAX_CHANNEL_COUNT; |
| 3950 | if ((channel_count < MIN_CHANNEL_COUNT) || (channel_count > max_channel_count)) { |
| 3951 | ALOGE("%s: unsupported channel count (%d) passed Min / Max (%d / %d)", __func__, |
| 3952 | channel_count, MIN_CHANNEL_COUNT, max_channel_count); |
| 3953 | return -EINVAL; |
| 3954 | } |
| 3955 | |
Ravi Kumar Alamanda | fae4211 | 2013-11-07 23:31:54 -0800 | [diff] [blame] | 3956 | switch (channel_count) { |
| 3957 | case 1: |
| 3958 | case 2: |
Chaithanya Krishna Bacharaju | 9955b16 | 2016-05-25 16:25:53 +0530 | [diff] [blame] | 3959 | case 3: |
| 3960 | case 4: |
Ravi Kumar Alamanda | fae4211 | 2013-11-07 23:31:54 -0800 | [diff] [blame] | 3961 | case 6: |
Karthikeyan Mani | 07faa60 | 2018-08-20 11:01:32 -0700 | [diff] [blame] | 3962 | case 8: |
Chaithanya Krishna Bacharaju | c9f9971 | 2019-04-16 15:32:52 +0530 | [diff] [blame] | 3963 | case 10: |
| 3964 | case 12: |
| 3965 | case 14: |
Ravi Kumar Alamanda | fae4211 | 2013-11-07 23:31:54 -0800 | [diff] [blame] | 3966 | break; |
| 3967 | default: |
| 3968 | ret = -EINVAL; |
| 3969 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3970 | |
| 3971 | switch (sample_rate) { |
| 3972 | case 8000: |
| 3973 | case 11025: |
| 3974 | case 12000: |
| 3975 | case 16000: |
| 3976 | case 22050: |
| 3977 | case 24000: |
| 3978 | case 32000: |
| 3979 | case 44100: |
| 3980 | case 48000: |
Haynes Mathew George | c9253d1 | 2017-12-13 15:58:28 -0800 | [diff] [blame] | 3981 | case 88200: |
Manish Dewangan | ba9fcfa | 2016-03-24 16:20:06 +0530 | [diff] [blame] | 3982 | case 96000: |
Haynes Mathew George | c9253d1 | 2017-12-13 15:58:28 -0800 | [diff] [blame] | 3983 | case 176400: |
Manish Dewangan | ba9fcfa | 2016-03-24 16:20:06 +0530 | [diff] [blame] | 3984 | case 192000: |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3985 | break; |
| 3986 | default: |
Ravi Kumar Alamanda | fae4211 | 2013-11-07 23:31:54 -0800 | [diff] [blame] | 3987 | ret = -EINVAL; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3988 | } |
| 3989 | |
Ravi Kumar Alamanda | fae4211 | 2013-11-07 23:31:54 -0800 | [diff] [blame] | 3990 | return ret; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3991 | } |
| 3992 | |
Naresh Tanniru | 04f7188 | 2018-06-26 17:46:22 +0530 | [diff] [blame] | 3993 | |
| 3994 | /** Add a value in a list if not already present. |
| 3995 | * @return true if value was successfully inserted or already present, |
| 3996 | * false if the list is full and does not contain the value. |
| 3997 | */ |
| 3998 | static bool register_uint(uint32_t value, uint32_t* list, size_t list_length) { |
| 3999 | for (size_t i = 0; i < list_length; i++) { |
| 4000 | if (list[i] == value) return true; // value is already present |
| 4001 | if (list[i] == 0) { // no values in this slot |
| 4002 | list[i] = value; |
| 4003 | return true; // value inserted |
| 4004 | } |
| 4005 | } |
| 4006 | return false; // could not insert value |
| 4007 | } |
| 4008 | |
| 4009 | /** Add channel_mask in supported_channel_masks if not already present. |
| 4010 | * @return true if channel_mask was successfully inserted or already present, |
| 4011 | * false if supported_channel_masks is full and does not contain channel_mask. |
| 4012 | */ |
| 4013 | static void register_channel_mask(audio_channel_mask_t channel_mask, |
| 4014 | audio_channel_mask_t supported_channel_masks[static MAX_SUPPORTED_CHANNEL_MASKS]) { |
| 4015 | ALOGE_IF(!register_uint(channel_mask, supported_channel_masks, MAX_SUPPORTED_CHANNEL_MASKS), |
| 4016 | "%s: stream can not declare supporting its channel_mask %x", __func__, channel_mask); |
| 4017 | } |
| 4018 | |
| 4019 | /** Add format in supported_formats if not already present. |
| 4020 | * @return true if format was successfully inserted or already present, |
| 4021 | * false if supported_formats is full and does not contain format. |
| 4022 | */ |
| 4023 | static void register_format(audio_format_t format, |
| 4024 | audio_format_t supported_formats[static MAX_SUPPORTED_FORMATS]) { |
| 4025 | ALOGE_IF(!register_uint(format, supported_formats, MAX_SUPPORTED_FORMATS), |
| 4026 | "%s: stream can not declare supporting its format %x", __func__, format); |
| 4027 | } |
| 4028 | /** Add sample_rate in supported_sample_rates if not already present. |
| 4029 | * @return true if sample_rate was successfully inserted or already present, |
| 4030 | * false if supported_sample_rates is full and does not contain sample_rate. |
| 4031 | */ |
| 4032 | static void register_sample_rate(uint32_t sample_rate, |
| 4033 | uint32_t supported_sample_rates[static MAX_SUPPORTED_SAMPLE_RATES]) { |
| 4034 | ALOGE_IF(!register_uint(sample_rate, supported_sample_rates, MAX_SUPPORTED_SAMPLE_RATES), |
| 4035 | "%s: stream can not declare supporting its sample rate %x", __func__, sample_rate); |
| 4036 | } |
| 4037 | |
Karthikeyan Mani | b38769c | 2018-11-07 15:44:13 -0800 | [diff] [blame] | 4038 | static inline uint32_t lcm(uint32_t num1, uint32_t num2) |
| 4039 | { |
| 4040 | uint32_t high = num1, low = num2, temp = 0; |
| 4041 | |
| 4042 | if (!num1 || !num2) |
| 4043 | return 0; |
| 4044 | |
| 4045 | if (num1 < num2) { |
| 4046 | high = num2; |
| 4047 | low = num1; |
| 4048 | } |
| 4049 | |
| 4050 | while (low != 0) { |
| 4051 | temp = low; |
| 4052 | low = high % low; |
| 4053 | high = temp; |
| 4054 | } |
| 4055 | return (num1 * num2)/high; |
| 4056 | } |
| 4057 | |
| 4058 | static inline uint32_t nearest_multiple(uint32_t num, uint32_t multiplier) |
| 4059 | { |
| 4060 | uint32_t remainder = 0; |
| 4061 | |
| 4062 | if (!multiplier) |
| 4063 | return num; |
| 4064 | |
| 4065 | remainder = num % multiplier; |
| 4066 | if (remainder) |
| 4067 | num += (multiplier - remainder); |
| 4068 | |
| 4069 | return num; |
| 4070 | } |
| 4071 | |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 4072 | static size_t get_stream_buffer_size(size_t duration_ms, |
| 4073 | uint32_t sample_rate, |
| 4074 | audio_format_t format, |
| 4075 | int channel_count, |
| 4076 | bool is_low_latency) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4077 | { |
| 4078 | size_t size = 0; |
Karthikeyan Mani | b38769c | 2018-11-07 15:44:13 -0800 | [diff] [blame] | 4079 | uint32_t bytes_per_period_sample = 0; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4080 | |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 4081 | size = (sample_rate * duration_ms) / 1000; |
Ravi Kumar Alamanda | dc9bb15 | 2014-09-08 15:59:58 -0700 | [diff] [blame] | 4082 | if (is_low_latency) |
| 4083 | size = configured_low_latency_capture_period_size; |
Manish Dewangan | ba9fcfa | 2016-03-24 16:20:06 +0530 | [diff] [blame] | 4084 | |
Karthikeyan Mani | b38769c | 2018-11-07 15:44:13 -0800 | [diff] [blame] | 4085 | bytes_per_period_sample = audio_bytes_per_sample(format) * channel_count; |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 4086 | size *= audio_bytes_per_sample(format) * channel_count; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4087 | |
Ralf Herz | bd08d63 | 2018-09-28 15:50:49 +0200 | [diff] [blame] | 4088 | /* make sure the size is multiple of 32 bytes and additionally multiple of |
| 4089 | * the frame_size (required for 24bit samples and non-power-of-2 channel counts) |
Ravi Kumar Alamanda | dc9bb15 | 2014-09-08 15:59:58 -0700 | [diff] [blame] | 4090 | * At 48 kHz mono 16-bit PCM: |
| 4091 | * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15) |
| 4092 | * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10) |
Karthikeyan Mani | b38769c | 2018-11-07 15:44:13 -0800 | [diff] [blame] | 4093 | * Also, make sure the size is multiple of bytes per period sample |
Ravi Kumar Alamanda | dc9bb15 | 2014-09-08 15:59:58 -0700 | [diff] [blame] | 4094 | */ |
Karthikeyan Mani | b38769c | 2018-11-07 15:44:13 -0800 | [diff] [blame] | 4095 | size = nearest_multiple(size, lcm(32, bytes_per_period_sample)); |
Ravi Kumar Alamanda | 33d3306 | 2013-06-11 14:40:01 -0700 | [diff] [blame] | 4096 | |
| 4097 | return size; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4098 | } |
| 4099 | |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 4100 | static size_t get_input_buffer_size(uint32_t sample_rate, |
| 4101 | audio_format_t format, |
| 4102 | int channel_count, |
| 4103 | bool is_low_latency) |
| 4104 | { |
| 4105 | /* Don't know if USB HIFI in this context so use true to be conservative */ |
| 4106 | if (check_input_parameters(sample_rate, format, channel_count, |
| 4107 | true /*is_usb_hifi */) != 0) |
| 4108 | return 0; |
| 4109 | |
| 4110 | return get_stream_buffer_size(AUDIO_CAPTURE_PERIOD_DURATION_MSEC, |
| 4111 | sample_rate, |
| 4112 | format, |
| 4113 | channel_count, |
| 4114 | is_low_latency); |
| 4115 | } |
| 4116 | |
Derek Chen | f6318be | 2017-06-12 17:16:24 -0400 | [diff] [blame] | 4117 | size_t get_output_period_size(uint32_t sample_rate, |
| 4118 | audio_format_t format, |
| 4119 | int channel_count, |
| 4120 | int duration /*in millisecs*/) |
Ashish Jain | 058165c | 2016-09-28 23:18:48 +0530 | [diff] [blame] | 4121 | { |
| 4122 | size_t size = 0; |
| 4123 | uint32_t bytes_per_sample = audio_bytes_per_sample(format); |
| 4124 | |
| 4125 | if ((duration == 0) || (sample_rate == 0) || |
| 4126 | (bytes_per_sample == 0) || (channel_count == 0)) { |
| 4127 | ALOGW("Invalid config duration %d sr %d bps %d ch %d", duration, sample_rate, |
| 4128 | bytes_per_sample, channel_count); |
| 4129 | return -EINVAL; |
| 4130 | } |
| 4131 | |
| 4132 | size = (sample_rate * |
| 4133 | duration * |
| 4134 | bytes_per_sample * |
| 4135 | channel_count) / 1000; |
| 4136 | /* |
| 4137 | * To have same PCM samples for all channels, the buffer size requires to |
| 4138 | * be multiple of (number of channels * bytes per sample) |
| 4139 | * For writes to succeed, the buffer must be written at address which is multiple of 32 |
| 4140 | */ |
| 4141 | size = ALIGN(size, (bytes_per_sample * channel_count * 32)); |
| 4142 | |
| 4143 | return (size/(channel_count * bytes_per_sample)); |
| 4144 | } |
| 4145 | |
Zhou Song | 48453a0 | 2018-01-10 17:50:59 +0800 | [diff] [blame] | 4146 | static uint64_t get_actual_pcm_frames_rendered(struct stream_out *out, struct timespec *timestamp) |
Ashish Jain | 5106d36 | 2016-05-11 19:23:33 +0530 | [diff] [blame] | 4147 | { |
| 4148 | uint64_t actual_frames_rendered = 0; |
| 4149 | size_t kernel_buffer_size = out->compr_config.fragment_size * out->compr_config.fragments; |
| 4150 | |
| 4151 | /* This adjustment accounts for buffering after app processor. |
| 4152 | * It is based on estimated DSP latency per use case, rather than exact. |
| 4153 | */ |
| 4154 | int64_t platform_latency = platform_render_latency(out->usecase) * |
| 4155 | out->sample_rate / 1000000LL; |
| 4156 | |
Zhou Song | 48453a0 | 2018-01-10 17:50:59 +0800 | [diff] [blame] | 4157 | pthread_mutex_lock(&out->position_query_lock); |
Ashish Jain | 5106d36 | 2016-05-11 19:23:33 +0530 | [diff] [blame] | 4158 | /* not querying actual state of buffering in kernel as it would involve an ioctl call |
| 4159 | * which then needs protection, this causes delay in TS query for pcm_offload usecase |
| 4160 | * hence only estimate. |
| 4161 | */ |
George Gao | 62ebc72 | 2019-07-29 16:29:44 -0700 | [diff] [blame] | 4162 | uint64_t signed_frames = 0; |
| 4163 | if (out->written >= kernel_buffer_size) |
| 4164 | signed_frames = out->written - kernel_buffer_size; |
Ashish Jain | 5106d36 | 2016-05-11 19:23:33 +0530 | [diff] [blame] | 4165 | |
George Gao | 62ebc72 | 2019-07-29 16:29:44 -0700 | [diff] [blame] | 4166 | signed_frames = signed_frames / (audio_bytes_per_sample(out->format) * popcount(out->channel_mask)); |
| 4167 | if (signed_frames >= platform_latency) |
| 4168 | signed_frames = signed_frames - platform_latency; |
Ashish Jain | 5106d36 | 2016-05-11 19:23:33 +0530 | [diff] [blame] | 4169 | |
Zhou Song | 48453a0 | 2018-01-10 17:50:59 +0800 | [diff] [blame] | 4170 | if (signed_frames > 0) { |
Ashish Jain | 5106d36 | 2016-05-11 19:23:33 +0530 | [diff] [blame] | 4171 | actual_frames_rendered = signed_frames; |
Zhou Song | 48453a0 | 2018-01-10 17:50:59 +0800 | [diff] [blame] | 4172 | if (timestamp != NULL ) |
| 4173 | *timestamp = out->writeAt; |
| 4174 | } else if (timestamp != NULL) { |
| 4175 | clock_gettime(CLOCK_MONOTONIC, timestamp); |
| 4176 | } |
| 4177 | pthread_mutex_unlock(&out->position_query_lock); |
Ashish Jain | 5106d36 | 2016-05-11 19:23:33 +0530 | [diff] [blame] | 4178 | |
| 4179 | ALOGVV("%s signed frames %lld out_written %lld kernel_buffer_size %d" |
George Gao | 62ebc72 | 2019-07-29 16:29:44 -0700 | [diff] [blame] | 4180 | "bytes/sample %zu channel count %d", __func__, signed_frames, |
Ashish Jain | 5106d36 | 2016-05-11 19:23:33 +0530 | [diff] [blame] | 4181 | (long long int)out->written, (int)kernel_buffer_size, |
| 4182 | audio_bytes_per_sample(out->compr_config.codec->format), |
| 4183 | popcount(out->channel_mask)); |
| 4184 | |
| 4185 | return actual_frames_rendered; |
| 4186 | } |
| 4187 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4188 | static uint32_t out_get_sample_rate(const struct audio_stream *stream) |
| 4189 | { |
| 4190 | struct stream_out *out = (struct stream_out *)stream; |
| 4191 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 4192 | return out->sample_rate; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4193 | } |
| 4194 | |
Ravi Kumar Alamanda | bdf1416 | 2014-09-05 16:14:17 -0700 | [diff] [blame] | 4195 | static int out_set_sample_rate(struct audio_stream *stream __unused, |
| 4196 | uint32_t rate __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4197 | { |
| 4198 | return -ENOSYS; |
| 4199 | } |
| 4200 | |
| 4201 | static size_t out_get_buffer_size(const struct audio_stream *stream) |
| 4202 | { |
| 4203 | struct stream_out *out = (struct stream_out *)stream; |
| 4204 | |
Varun Balaraj | e49253e | 2017-07-06 19:48:56 +0530 | [diff] [blame] | 4205 | if (is_interactive_usecase(out->usecase)) { |
Sri Karri | 27279e1 | 2017-08-07 16:05:20 +0530 | [diff] [blame] | 4206 | return out->config.period_size * out->config.period_count; |
Varun Balaraj | e49253e | 2017-07-06 19:48:56 +0530 | [diff] [blame] | 4207 | } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { |
Naresh Tanniru | ee3499a | 2017-01-05 14:05:35 +0530 | [diff] [blame] | 4208 | if (out->flags & AUDIO_OUTPUT_FLAG_TIMESTAMP) |
| 4209 | return out->compr_config.fragment_size - sizeof(struct snd_codec_metadata); |
| 4210 | else |
| 4211 | return out->compr_config.fragment_size; |
| 4212 | } else if(out->usecase == USECASE_COMPRESS_VOIP_CALL) |
Narsinga Rao Chella | 05573b7 | 2013-11-15 15:21:40 -0800 | [diff] [blame] | 4213 | return voice_extn_compress_voip_out_get_buffer_size(out); |
Dhananjay Kumar | ac34158 | 2017-02-23 23:42:25 +0530 | [diff] [blame] | 4214 | else if (is_offload_usecase(out->usecase) && |
| 4215 | out->flags == AUDIO_OUTPUT_FLAG_DIRECT) |
Ashish Jain | 83a6cc2 | 2016-06-28 14:34:17 +0530 | [diff] [blame] | 4216 | return out->hal_fragment_size; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 4217 | |
Haynes Mathew George | 5beddd4 | 2016-06-27 18:33:40 -0700 | [diff] [blame] | 4218 | return out->config.period_size * out->af_period_multiplier * |
Ravi Kumar Alamanda | c3bc081 | 2014-09-08 15:34:32 -0700 | [diff] [blame] | 4219 | audio_stream_out_frame_size((const struct audio_stream_out *)stream); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4220 | } |
| 4221 | |
| 4222 | static uint32_t out_get_channels(const struct audio_stream *stream) |
| 4223 | { |
| 4224 | struct stream_out *out = (struct stream_out *)stream; |
| 4225 | |
| 4226 | return out->channel_mask; |
| 4227 | } |
| 4228 | |
| 4229 | static audio_format_t out_get_format(const struct audio_stream *stream) |
| 4230 | { |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 4231 | struct stream_out *out = (struct stream_out *)stream; |
| 4232 | |
| 4233 | return out->format; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4234 | } |
| 4235 | |
Ravi Kumar Alamanda | bdf1416 | 2014-09-05 16:14:17 -0700 | [diff] [blame] | 4236 | static int out_set_format(struct audio_stream *stream __unused, |
| 4237 | audio_format_t format __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4238 | { |
| 4239 | return -ENOSYS; |
| 4240 | } |
| 4241 | |
| 4242 | static int out_standby(struct audio_stream *stream) |
| 4243 | { |
| 4244 | struct stream_out *out = (struct stream_out *)stream; |
| 4245 | struct audio_device *adev = out->dev; |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 4246 | bool do_stop = true; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 4247 | |
Sidipotu Ashok | f43018c | 2014-05-02 16:21:50 +0530 | [diff] [blame] | 4248 | ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__, |
| 4249 | stream, out->usecase, use_case_table[out->usecase]); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4250 | |
Shiv Maliyappanahalli | 736d4ce | 2015-09-28 15:23:06 -0700 | [diff] [blame] | 4251 | lock_output_stream(out); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4252 | if (!out->standby) { |
Ravi Kumar Alamanda | 8a0f977 | 2015-06-15 10:35:19 -0700 | [diff] [blame] | 4253 | if (adev->adm_deregister_stream) |
| 4254 | adev->adm_deregister_stream(adev->adm_data, out->handle); |
| 4255 | |
Haynes Mathew George | 7fce0a5 | 2016-06-23 18:22:27 -0700 | [diff] [blame] | 4256 | if (is_offload_usecase(out->usecase)) |
| 4257 | stop_compressed_output_l(out); |
| 4258 | |
Ravi Kumar Alamanda | 8bba9e9 | 2013-11-11 21:09:07 -0800 | [diff] [blame] | 4259 | pthread_mutex_lock(&adev->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4260 | out->standby = true; |
Zhou Song | a889504 | 2016-07-05 17:54:22 +0800 | [diff] [blame] | 4261 | if (out->usecase == USECASE_COMPRESS_VOIP_CALL) { |
| 4262 | voice_extn_compress_voip_close_output_stream(stream); |
Ashish Jain | 1b9b30c | 2017-05-18 20:57:40 +0530 | [diff] [blame] | 4263 | out->started = 0; |
Zhou Song | a889504 | 2016-07-05 17:54:22 +0800 | [diff] [blame] | 4264 | pthread_mutex_unlock(&adev->lock); |
| 4265 | pthread_mutex_unlock(&out->lock); |
| 4266 | ALOGD("VOIP output entered standby"); |
| 4267 | return 0; |
| 4268 | } else if (!is_offload_usecase(out->usecase)) { |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 4269 | if (out->pcm) { |
| 4270 | pcm_close(out->pcm); |
| 4271 | out->pcm = NULL; |
| 4272 | } |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 4273 | if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) { |
| 4274 | do_stop = out->playback_started; |
| 4275 | out->playback_started = false; |
Phil Burk | d898ba6 | 2019-06-20 12:49:01 -0700 | [diff] [blame] | 4276 | |
| 4277 | if (out->mmap_shared_memory_fd >= 0) { |
| 4278 | ALOGV("%s: closing mmap_shared_memory_fd = %d", |
| 4279 | __func__, out->mmap_shared_memory_fd); |
| 4280 | close(out->mmap_shared_memory_fd); |
| 4281 | out->mmap_shared_memory_fd = -1; |
| 4282 | } |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 4283 | } |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 4284 | } else { |
Apoorv Raghuvanshi | 44bd917 | 2014-05-28 14:50:07 -0700 | [diff] [blame] | 4285 | ALOGD("copl(%p):standby", out); |
Chaithanya Krishna Bacharaju | a70cb6a | 2015-07-24 14:15:05 +0530 | [diff] [blame] | 4286 | out->send_next_track_params = false; |
| 4287 | out->is_compr_metadata_avail = false; |
Haynes Mathew George | 352f27b | 2013-07-26 00:00:15 -0700 | [diff] [blame] | 4288 | out->gapless_mdata.encoder_delay = 0; |
| 4289 | out->gapless_mdata.encoder_padding = 0; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 4290 | if (out->compr != NULL) { |
| 4291 | compress_close(out->compr); |
| 4292 | out->compr = NULL; |
| 4293 | } |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 4294 | } |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 4295 | if (do_stop) { |
| 4296 | stop_output_stream(out); |
| 4297 | } |
Lakshman Chaluvaraju | 06677b4 | 2019-06-24 10:04:52 +0530 | [diff] [blame] | 4298 | // if fm is active route on selected device in UI |
| 4299 | audio_extn_fm_route_on_selected_device(adev, out->devices); |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 4300 | pthread_mutex_unlock(&adev->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4301 | } |
| 4302 | pthread_mutex_unlock(&out->lock); |
Ashish Jain | bbce432 | 2016-02-16 13:25:27 +0530 | [diff] [blame] | 4303 | ALOGD("%s: exit", __func__); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4304 | return 0; |
| 4305 | } |
| 4306 | |
Dhananjay Kumar | e6293dd | 2017-05-25 17:25:30 +0530 | [diff] [blame] | 4307 | static int out_on_error(struct audio_stream *stream) |
| 4308 | { |
| 4309 | struct stream_out *out = (struct stream_out *)stream; |
Ben Romberger | fd02a2f | 2018-09-17 10:23:23 -0700 | [diff] [blame] | 4310 | int status = 0; |
Dhananjay Kumar | e6293dd | 2017-05-25 17:25:30 +0530 | [diff] [blame] | 4311 | |
| 4312 | lock_output_stream(out); |
Haynes Mathew George | ee5836f | 2017-11-21 18:02:10 -0800 | [diff] [blame] | 4313 | // always send CMD_ERROR for offload streams, this |
| 4314 | // is needed e.g. when SSR happens within compress_open |
| 4315 | // since the stream is active, offload_callback_thread is also active. |
| 4316 | if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { |
| 4317 | stop_compressed_output_l(out); |
Ben Romberger | fd02a2f | 2018-09-17 10:23:23 -0700 | [diff] [blame] | 4318 | } |
| 4319 | pthread_mutex_unlock(&out->lock); |
| 4320 | |
| 4321 | status = out_standby(&out->stream.common); |
| 4322 | |
| 4323 | lock_output_stream(out); |
| 4324 | if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { |
Haynes Mathew George | ee5836f | 2017-11-21 18:02:10 -0800 | [diff] [blame] | 4325 | send_offload_cmd_l(out, OFFLOAD_CMD_ERROR); |
Dhananjay Kumar | e6293dd | 2017-05-25 17:25:30 +0530 | [diff] [blame] | 4326 | } |
Manisha Agarwal | 7b3e377 | 2019-02-20 14:33:45 +0530 | [diff] [blame] | 4327 | |
| 4328 | if (is_offload_usecase(out->usecase) && out->card_status == CARD_STATUS_OFFLINE) { |
| 4329 | ALOGD("Setting previous card status if offline"); |
| 4330 | out->prev_card_status_offline = true; |
| 4331 | } |
| 4332 | |
Dhananjay Kumar | e6293dd | 2017-05-25 17:25:30 +0530 | [diff] [blame] | 4333 | pthread_mutex_unlock(&out->lock); |
| 4334 | |
Ben Romberger | fd02a2f | 2018-09-17 10:23:23 -0700 | [diff] [blame] | 4335 | return status; |
Dhananjay Kumar | e6293dd | 2017-05-25 17:25:30 +0530 | [diff] [blame] | 4336 | } |
| 4337 | |
Ashish Jain | 1b9b30c | 2017-05-18 20:57:40 +0530 | [diff] [blame] | 4338 | /* |
| 4339 | *standby implementation without locks, assumes that the callee already |
| 4340 | *has taken adev and out lock. |
| 4341 | */ |
| 4342 | int out_standby_l(struct audio_stream *stream) |
| 4343 | { |
| 4344 | struct stream_out *out = (struct stream_out *)stream; |
| 4345 | struct audio_device *adev = out->dev; |
| 4346 | |
| 4347 | ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__, |
| 4348 | stream, out->usecase, use_case_table[out->usecase]); |
| 4349 | |
| 4350 | if (!out->standby) { |
Haynes Mathew George | 380745d | 2017-10-04 15:27:45 -0700 | [diff] [blame] | 4351 | ATRACE_BEGIN("out_standby_l"); |
Ashish Jain | 1b9b30c | 2017-05-18 20:57:40 +0530 | [diff] [blame] | 4352 | if (adev->adm_deregister_stream) |
| 4353 | adev->adm_deregister_stream(adev->adm_data, out->handle); |
| 4354 | |
| 4355 | if (is_offload_usecase(out->usecase)) |
| 4356 | stop_compressed_output_l(out); |
| 4357 | |
| 4358 | out->standby = true; |
| 4359 | if (out->usecase == USECASE_COMPRESS_VOIP_CALL) { |
| 4360 | voice_extn_compress_voip_close_output_stream(stream); |
| 4361 | out->started = 0; |
| 4362 | ALOGD("VOIP output entered standby"); |
Haynes Mathew George | 380745d | 2017-10-04 15:27:45 -0700 | [diff] [blame] | 4363 | ATRACE_END(); |
Ashish Jain | 1b9b30c | 2017-05-18 20:57:40 +0530 | [diff] [blame] | 4364 | return 0; |
| 4365 | } else if (!is_offload_usecase(out->usecase)) { |
| 4366 | if (out->pcm) { |
| 4367 | pcm_close(out->pcm); |
| 4368 | out->pcm = NULL; |
| 4369 | } |
Vignesh Kulothungan | a692727 | 2019-02-20 15:17:07 -0800 | [diff] [blame] | 4370 | if (out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) { |
| 4371 | if (adev->haptic_pcm) { |
| 4372 | pcm_close(adev->haptic_pcm); |
| 4373 | adev->haptic_pcm = NULL; |
| 4374 | } |
| 4375 | |
| 4376 | if (adev->haptic_buffer != NULL) { |
| 4377 | free(adev->haptic_buffer); |
| 4378 | adev->haptic_buffer = NULL; |
| 4379 | adev->haptic_buffer_size = 0; |
| 4380 | } |
| 4381 | adev->haptic_pcm_device_id = 0; |
| 4382 | } |
Ashish Jain | 1b9b30c | 2017-05-18 20:57:40 +0530 | [diff] [blame] | 4383 | } else { |
| 4384 | ALOGD("copl(%p):standby", out); |
| 4385 | out->send_next_track_params = false; |
| 4386 | out->is_compr_metadata_avail = false; |
| 4387 | out->gapless_mdata.encoder_delay = 0; |
| 4388 | out->gapless_mdata.encoder_padding = 0; |
| 4389 | if (out->compr != NULL) { |
| 4390 | compress_close(out->compr); |
| 4391 | out->compr = NULL; |
| 4392 | } |
| 4393 | } |
| 4394 | stop_output_stream(out); |
Haynes Mathew George | 380745d | 2017-10-04 15:27:45 -0700 | [diff] [blame] | 4395 | ATRACE_END(); |
Ashish Jain | 1b9b30c | 2017-05-18 20:57:40 +0530 | [diff] [blame] | 4396 | } |
| 4397 | ALOGD("%s: exit", __func__); |
| 4398 | return 0; |
| 4399 | } |
| 4400 | |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 4401 | static int out_dump(const struct audio_stream *stream, int fd) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4402 | { |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 4403 | struct stream_out *out = (struct stream_out *)stream; |
| 4404 | |
| 4405 | // We try to get the lock for consistency, |
| 4406 | // but it isn't necessary for these variables. |
| 4407 | // If we're not in standby, we may be blocked on a write. |
| 4408 | const bool locked = (pthread_mutex_trylock(&out->lock) == 0); |
| 4409 | dprintf(fd, " Standby: %s\n", out->standby ? "yes" : "no"); |
| 4410 | dprintf(fd, " Frames written: %lld\n", (long long)out->written); |
| 4411 | |
| 4412 | if (locked) { |
| 4413 | pthread_mutex_unlock(&out->lock); |
| 4414 | } |
| 4415 | |
| 4416 | // dump error info |
| 4417 | (void)error_log_dump( |
| 4418 | out->error_log, fd, " " /* prefix */, 0 /* lines */, 0 /* limit_ns */); |
| 4419 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4420 | return 0; |
| 4421 | } |
| 4422 | |
Haynes Mathew George | 352f27b | 2013-07-26 00:00:15 -0700 | [diff] [blame] | 4423 | static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms) |
| 4424 | { |
| 4425 | int ret = 0; |
| 4426 | char value[32]; |
ApurupaPattapu | 2e084df | 2013-12-18 15:47:59 -0800 | [diff] [blame] | 4427 | |
Haynes Mathew George | 352f27b | 2013-07-26 00:00:15 -0700 | [diff] [blame] | 4428 | if (!out || !parms) { |
Krishnankutty Kolathappilly | eb78be7 | 2013-12-15 12:03:07 -0800 | [diff] [blame] | 4429 | ALOGE("%s: return invalid ",__func__); |
Haynes Mathew George | 352f27b | 2013-07-26 00:00:15 -0700 | [diff] [blame] | 4430 | return -EINVAL; |
| 4431 | } |
| 4432 | |
Satya Krishna Pindiproli | 7047160 | 2015-04-24 19:12:43 +0530 | [diff] [blame] | 4433 | ret = audio_extn_parse_compress_metadata(out, parms); |
Weiyin Jiang | 18ac4e9 | 2015-03-15 15:03:40 +0800 | [diff] [blame] | 4434 | |
Haynes Mathew George | 352f27b | 2013-07-26 00:00:15 -0700 | [diff] [blame] | 4435 | ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value)); |
| 4436 | if (ret >= 0) { |
Satya Krishna Pindiproli | 7047160 | 2015-04-24 19:12:43 +0530 | [diff] [blame] | 4437 | out->gapless_mdata.encoder_delay = atoi(value); //whats a good limit check? |
Haynes Mathew George | 352f27b | 2013-07-26 00:00:15 -0700 | [diff] [blame] | 4438 | } |
Haynes Mathew George | 352f27b | 2013-07-26 00:00:15 -0700 | [diff] [blame] | 4439 | ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value)); |
| 4440 | if (ret >= 0) { |
Satya Krishna Pindiproli | 7047160 | 2015-04-24 19:12:43 +0530 | [diff] [blame] | 4441 | out->gapless_mdata.encoder_padding = atoi(value); |
Haynes Mathew George | 352f27b | 2013-07-26 00:00:15 -0700 | [diff] [blame] | 4442 | } |
| 4443 | |
Haynes Mathew George | 352f27b | 2013-07-26 00:00:15 -0700 | [diff] [blame] | 4444 | ALOGV("%s new encoder delay %u and padding %u", __func__, |
| 4445 | out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding); |
| 4446 | |
| 4447 | return 0; |
| 4448 | } |
| 4449 | |
Ravi Kumar Alamanda | 060bc5a | 2014-09-05 13:51:35 -0700 | [diff] [blame] | 4450 | static bool output_drives_call(struct audio_device *adev, struct stream_out *out) |
| 4451 | { |
| 4452 | return out == adev->primary_output || out == adev->voice_tx_output; |
| 4453 | } |
Haynes Mathew George | 352f27b | 2013-07-26 00:00:15 -0700 | [diff] [blame] | 4454 | |
Dhananjay Kumar | e6293dd | 2017-05-25 17:25:30 +0530 | [diff] [blame] | 4455 | // note: this call is safe only if the stream_cb is |
| 4456 | // removed first in close_output_stream (as is done now). |
| 4457 | static void out_snd_mon_cb(void * stream, struct str_parms * parms) |
| 4458 | { |
| 4459 | if (!stream || !parms) |
| 4460 | return; |
| 4461 | |
| 4462 | struct stream_out *out = (struct stream_out *)stream; |
| 4463 | struct audio_device *adev = out->dev; |
| 4464 | |
| 4465 | card_status_t status; |
| 4466 | int card; |
| 4467 | if (parse_snd_card_status(parms, &card, &status) < 0) |
| 4468 | return; |
| 4469 | |
| 4470 | pthread_mutex_lock(&adev->lock); |
| 4471 | bool valid_cb = (card == adev->snd_card); |
| 4472 | pthread_mutex_unlock(&adev->lock); |
| 4473 | |
| 4474 | if (!valid_cb) |
| 4475 | return; |
| 4476 | |
| 4477 | lock_output_stream(out); |
| 4478 | if (out->card_status != status) |
| 4479 | out->card_status = status; |
| 4480 | pthread_mutex_unlock(&out->lock); |
| 4481 | |
| 4482 | ALOGI("out_snd_mon_cb for card %d usecase %s, status %s", card, |
| 4483 | use_case_table[out->usecase], |
| 4484 | status == CARD_STATUS_OFFLINE ? "offline" : "online"); |
| 4485 | |
Aditya Bavanari | 59ebed4 | 2019-02-05 17:44:57 +0530 | [diff] [blame] | 4486 | if (status == CARD_STATUS_OFFLINE) { |
Dhananjay Kumar | e6293dd | 2017-05-25 17:25:30 +0530 | [diff] [blame] | 4487 | out_on_error(stream); |
Aditya Bavanari | 59ebed4 | 2019-02-05 17:44:57 +0530 | [diff] [blame] | 4488 | if (voice_is_call_state_active(adev) && |
| 4489 | out == adev->primary_output) { |
| 4490 | ALOGD("%s: SSR/PDR occurred, end all calls\n", __func__); |
| 4491 | pthread_mutex_lock(&adev->lock); |
| 4492 | voice_stop_call(adev); |
| 4493 | adev->mode = AUDIO_MODE_NORMAL; |
| 4494 | pthread_mutex_unlock(&adev->lock); |
| 4495 | } |
| 4496 | } |
Dhananjay Kumar | e6293dd | 2017-05-25 17:25:30 +0530 | [diff] [blame] | 4497 | return; |
| 4498 | } |
| 4499 | |
Kevin Rocard | fce1900 | 2017-08-07 19:21:36 -0700 | [diff] [blame] | 4500 | static int get_alive_usb_card(struct str_parms* parms) { |
| 4501 | int card; |
| 4502 | if ((str_parms_get_int(parms, "card", &card) >= 0) && |
| 4503 | !audio_extn_usb_alive(card)) { |
| 4504 | return card; |
| 4505 | } |
| 4506 | return -ENODEV; |
| 4507 | } |
| 4508 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4509 | static int out_set_parameters(struct audio_stream *stream, const char *kvpairs) |
| 4510 | { |
| 4511 | struct stream_out *out = (struct stream_out *)stream; |
| 4512 | struct audio_device *adev = out->dev; |
| 4513 | struct str_parms *parms; |
| 4514 | char value[32]; |
Shiv Maliyappanahalli | 3e064fd | 2013-12-16 15:54:40 -0800 | [diff] [blame] | 4515 | int ret = 0, val = 0, err; |
Vignesh Kulothungan | 39fc6a2 | 2019-08-01 00:55:59 -0700 | [diff] [blame] | 4516 | int ext_controller = -1; |
| 4517 | int ext_stream = -1; |
Chaithanya Krishna Bacharaju | 69d2e4c | 2017-05-26 18:22:46 +0530 | [diff] [blame] | 4518 | bool bypass_a2dp = false; |
Garmond Leung | 5fd0b55 | 2018-04-17 11:56:12 -0700 | [diff] [blame] | 4519 | bool reconfig = false; |
| 4520 | unsigned long service_interval = 0; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4521 | |
sangwoo | bc67724 | 2013-08-08 16:53:43 +0900 | [diff] [blame] | 4522 | ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s", |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 4523 | __func__, out->usecase, use_case_table[out->usecase], kvpairs); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4524 | parms = str_parms_create_str(kvpairs); |
Preetam Singh Ranawat | a5f32b4 | 2014-09-23 12:12:47 +0530 | [diff] [blame] | 4525 | if (!parms) |
| 4526 | goto error; |
Vignesh Kulothungan | 39fc6a2 | 2019-08-01 00:55:59 -0700 | [diff] [blame] | 4527 | |
| 4528 | err = platform_get_controller_stream_from_params(parms, &ext_controller, |
| 4529 | &ext_stream); |
| 4530 | if (err >= 0) { |
| 4531 | out->extconn.cs.controller = ext_controller; |
| 4532 | out->extconn.cs.stream = ext_stream; |
| 4533 | ALOGD("%s: usecase(%s) new controller/stream (%d/%d)", __func__, |
| 4534 | use_case_table[out->usecase], out->extconn.cs.controller, |
| 4535 | out->extconn.cs.stream); |
| 4536 | } |
| 4537 | |
Shiv Maliyappanahalli | 3e064fd | 2013-12-16 15:54:40 -0800 | [diff] [blame] | 4538 | err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value)); |
| 4539 | if (err >= 0) { |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4540 | val = atoi(value); |
Shiv Maliyappanahalli | 736d4ce | 2015-09-28 15:23:06 -0700 | [diff] [blame] | 4541 | lock_output_stream(out); |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 4542 | pthread_mutex_lock(&adev->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4543 | |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 4544 | /* |
Weiyin Jiang | 4256eeb | 2016-05-19 13:28:30 +0800 | [diff] [blame] | 4545 | * When HDMI cable is unplugged the music playback is paused and |
| 4546 | * the policy manager sends routing=0. But the audioflinger continues |
| 4547 | * to write data until standby time (3sec). As the HDMI core is |
| 4548 | * turned off, the write gets blocked. |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 4549 | * Avoid this by routing audio to speaker until standby. |
| 4550 | */ |
Weiyin Jiang | 4256eeb | 2016-05-19 13:28:30 +0800 | [diff] [blame] | 4551 | if ((out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL) && |
| 4552 | (val == AUDIO_DEVICE_NONE) && |
Satish Babu Patakokila | 1caa1b7 | 2016-05-24 13:47:08 +0530 | [diff] [blame] | 4553 | !audio_extn_passthru_is_passthrough_stream(out) && |
Vignesh Kulothungan | 39fc6a2 | 2019-08-01 00:55:59 -0700 | [diff] [blame] | 4554 | (platform_get_edid_info_v2(adev->platform, |
| 4555 | out->extconn.cs.controller, |
| 4556 | out->extconn.cs.stream) != 0)) { |
| 4557 | out->extconn.cs.controller = out->extconn.cs.stream = -1; |
Weiyin Jiang | 4256eeb | 2016-05-19 13:28:30 +0800 | [diff] [blame] | 4558 | val = AUDIO_DEVICE_OUT_SPEAKER; |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 4559 | } |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 4560 | /* |
| 4561 | * When A2DP is disconnected the |
| 4562 | * music playback is paused and the policy manager sends routing=0 |
Florian Pfister | 1a84f31 | 2018-07-19 14:38:18 +0200 | [diff] [blame] | 4563 | * But the audioflinger continues to write data until standby time |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 4564 | * (3sec). As BT is turned off, the write gets blocked. |
| 4565 | * Avoid this by routing audio to speaker until standby. |
| 4566 | */ |
Naresh Tanniru | f7e9e63 | 2016-11-04 14:54:20 -0700 | [diff] [blame] | 4567 | if ((out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) && |
Aniket Kumar Lata | 1fb11cb | 2017-09-08 13:48:55 -0700 | [diff] [blame] | 4568 | (val == AUDIO_DEVICE_NONE) && |
Aniket Kumar Lata | 2cf6a92 | 2019-05-07 17:01:43 -0700 | [diff] [blame] | 4569 | !audio_extn_a2dp_source_is_ready() && |
| 4570 | !adev->bt_sco_on) { |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 4571 | val = AUDIO_DEVICE_OUT_SPEAKER; |
| 4572 | } |
Sharad Sangle | d0a50b2 | 2018-04-05 23:28:32 +0530 | [diff] [blame] | 4573 | /* |
| 4574 | * When USB headset is disconnected the music platback paused |
| 4575 | * and the policy manager send routing=0. But if the USB is connected |
| 4576 | * back before the standby time, AFE is not closed and opened |
| 4577 | * when USB is connected back. So routing to speker will guarantee |
| 4578 | * AFE reconfiguration and AFE will be opend once USB is connected again |
| 4579 | */ |
| 4580 | if ((out->devices & AUDIO_DEVICE_OUT_ALL_USB) && |
| 4581 | (val == AUDIO_DEVICE_NONE) && |
| 4582 | !audio_extn_usb_connected(parms)) { |
| 4583 | val = AUDIO_DEVICE_OUT_SPEAKER; |
| 4584 | } |
Preetam Singh Ranawat | a1849ba | 2017-02-06 14:10:11 +0530 | [diff] [blame] | 4585 | /* To avoid a2dp to sco overlapping / BT device improper state |
| 4586 | * check with BT lib about a2dp streaming support before routing |
Naresh Tanniru | 03f9dd5 | 2016-10-19 18:46:22 +0530 | [diff] [blame] | 4587 | */ |
Preetam Singh Ranawat | a1849ba | 2017-02-06 14:10:11 +0530 | [diff] [blame] | 4588 | if (val & AUDIO_DEVICE_OUT_ALL_A2DP) { |
Florian Pfister | 1a84f31 | 2018-07-19 14:38:18 +0200 | [diff] [blame] | 4589 | if (!audio_extn_a2dp_source_is_ready()) { |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 4590 | if (val & |
| 4591 | (AUDIO_DEVICE_OUT_SPEAKER | AUDIO_DEVICE_OUT_SPEAKER_SAFE)) { |
Preetam Singh Ranawat | a1849ba | 2017-02-06 14:10:11 +0530 | [diff] [blame] | 4592 | //combo usecase just by pass a2dp |
| 4593 | ALOGW("%s: A2DP profile is not ready,routing to speaker only", __func__); |
Chaithanya Krishna Bacharaju | 69d2e4c | 2017-05-26 18:22:46 +0530 | [diff] [blame] | 4594 | bypass_a2dp = true; |
Preetam Singh Ranawat | a1849ba | 2017-02-06 14:10:11 +0530 | [diff] [blame] | 4595 | } else { |
| 4596 | ALOGE("%s: A2DP profile is not ready,ignoring routing request", __func__); |
| 4597 | /* update device to a2dp and don't route as BT returned error |
| 4598 | * However it is still possible a2dp routing called because |
| 4599 | * of current active device disconnection (like wired headset) |
| 4600 | */ |
| 4601 | out->devices = val; |
| 4602 | pthread_mutex_unlock(&out->lock); |
| 4603 | pthread_mutex_unlock(&adev->lock); |
| 4604 | goto error; |
| 4605 | } |
| 4606 | } |
Naresh Tanniru | 03f9dd5 | 2016-10-19 18:46:22 +0530 | [diff] [blame] | 4607 | } |
Kevin Rocard | fce1900 | 2017-08-07 19:21:36 -0700 | [diff] [blame] | 4608 | |
| 4609 | audio_devices_t new_dev = val; |
| 4610 | |
| 4611 | // Workaround: If routing to an non existing usb device, fail gracefully |
| 4612 | // The routing request will otherwise block during 10 second |
| 4613 | int card; |
| 4614 | if (audio_is_usb_out_device(new_dev) && |
| 4615 | (card = get_alive_usb_card(parms)) >= 0) { |
| 4616 | |
| 4617 | ALOGW("out_set_parameters() ignoring rerouting to non existing USB card %d", card); |
| 4618 | pthread_mutex_unlock(&adev->lock); |
| 4619 | pthread_mutex_unlock(&out->lock); |
| 4620 | ret = -ENOSYS; |
| 4621 | goto routing_fail; |
| 4622 | } |
| 4623 | |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 4624 | /* |
| 4625 | * select_devices() call below switches all the usecases on the same |
| 4626 | * backend to the new device. Refer to check_usecases_codec_backend() in |
| 4627 | * the select_devices(). But how do we undo this? |
| 4628 | * |
| 4629 | * For example, music playback is active on headset (deep-buffer usecase) |
| 4630 | * and if we go to ringtones and select a ringtone, low-latency usecase |
| 4631 | * will be started on headset+speaker. As we can't enable headset+speaker |
| 4632 | * and headset devices at the same time, select_devices() switches the music |
| 4633 | * playback to headset+speaker while starting low-lateny usecase for ringtone. |
| 4634 | * So when the ringtone playback is completed, how do we undo the same? |
| 4635 | * |
| 4636 | * We are relying on the out_set_parameters() call on deep-buffer output, |
| 4637 | * once the ringtone playback is ended. |
| 4638 | * NOTE: We should not check if the current devices are same as new devices. |
| 4639 | * Because select_devices() must be called to switch back the music |
| 4640 | * playback to headset. |
| 4641 | */ |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 4642 | if (val != 0) { |
Haynes Mathew George | 5beddd4 | 2016-06-27 18:33:40 -0700 | [diff] [blame] | 4643 | audio_devices_t new_dev = val; |
| 4644 | bool same_dev = out->devices == new_dev; |
| 4645 | out->devices = new_dev; |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 4646 | |
Ravi Kumar Alamanda | be14939 | 2014-10-20 17:07:43 -0700 | [diff] [blame] | 4647 | if (output_drives_call(adev, out)) { |
kunleiz | 61b9c2e | 2017-11-01 13:47:23 +0800 | [diff] [blame] | 4648 | if (!voice_is_call_state_active(adev)) { |
Ravi Kumar Alamanda | be14939 | 2014-10-20 17:07:43 -0700 | [diff] [blame] | 4649 | if (adev->mode == AUDIO_MODE_IN_CALL) { |
| 4650 | adev->current_call_output = out; |
Garmond Leung | 5fd0b55 | 2018-04-17 11:56:12 -0700 | [diff] [blame] | 4651 | if (audio_is_usb_out_device(out->devices & AUDIO_DEVICE_OUT_ALL_USB)) { |
| 4652 | service_interval = audio_extn_usb_find_service_interval(true, true /*playback*/); |
| 4653 | audio_extn_usb_set_service_interval(true /*playback*/, |
| 4654 | service_interval, |
| 4655 | &reconfig); |
| 4656 | ALOGD("%s, svc_int(%ld),reconfig(%d)",__func__,service_interval, reconfig); |
| 4657 | } |
| 4658 | ret = voice_start_call(adev); |
Ravi Kumar Alamanda | be14939 | 2014-10-20 17:07:43 -0700 | [diff] [blame] | 4659 | } |
| 4660 | } else { |
| 4661 | adev->current_call_output = out; |
Ravi Kumar Alamanda | 060bc5a | 2014-09-05 13:51:35 -0700 | [diff] [blame] | 4662 | voice_update_devices_for_all_voice_usecases(adev); |
Ravi Kumar Alamanda | be14939 | 2014-10-20 17:07:43 -0700 | [diff] [blame] | 4663 | } |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 4664 | } |
Shiv Maliyappanahalli | 6b32c4c | 2015-11-04 18:10:20 -0800 | [diff] [blame] | 4665 | |
| 4666 | if (!out->standby) { |
Haynes Mathew George | 5beddd4 | 2016-06-27 18:33:40 -0700 | [diff] [blame] | 4667 | if (!same_dev) { |
| 4668 | ALOGV("update routing change"); |
Sudheer Papothi | 8026698 | 2016-08-16 02:36:18 +0530 | [diff] [blame] | 4669 | audio_extn_perf_lock_acquire(&adev->perf_lock_handle, 0, |
| 4670 | adev->perf_lock_opts, |
| 4671 | adev->perf_lock_opts_size); |
Haynes Mathew George | 822b549 | 2016-07-01 16:57:24 -0700 | [diff] [blame] | 4672 | if (adev->adm_on_routing_change) |
| 4673 | adev->adm_on_routing_change(adev->adm_data, |
| 4674 | out->handle); |
Haynes Mathew George | 5beddd4 | 2016-06-27 18:33:40 -0700 | [diff] [blame] | 4675 | } |
Chaithanya Krishna Bacharaju | 69d2e4c | 2017-05-26 18:22:46 +0530 | [diff] [blame] | 4676 | if (!bypass_a2dp) { |
| 4677 | select_devices(adev, out->usecase); |
| 4678 | } else { |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 4679 | if (new_dev & AUDIO_DEVICE_OUT_SPEAKER_SAFE) |
| 4680 | out->devices = AUDIO_DEVICE_OUT_SPEAKER_SAFE; |
| 4681 | else |
| 4682 | out->devices = AUDIO_DEVICE_OUT_SPEAKER; |
Chaithanya Krishna Bacharaju | 69d2e4c | 2017-05-26 18:22:46 +0530 | [diff] [blame] | 4683 | select_devices(adev, out->usecase); |
| 4684 | out->devices = new_dev; |
| 4685 | } |
Vignesh Kulothungan | 3b5fae5 | 2017-09-25 12:16:30 -0700 | [diff] [blame] | 4686 | |
| 4687 | if (!same_dev) { |
| 4688 | // on device switch force swap, lower functions will make sure |
| 4689 | // to check if swap is allowed or not. |
| 4690 | platform_set_swap_channels(adev, true); |
Sudheer Papothi | 8026698 | 2016-08-16 02:36:18 +0530 | [diff] [blame] | 4691 | audio_extn_perf_lock_release(&adev->perf_lock_handle); |
Vignesh Kulothungan | 3b5fae5 | 2017-09-25 12:16:30 -0700 | [diff] [blame] | 4692 | } |
Chaithanya Krishna Bacharaju | 69d2e4c | 2017-05-26 18:22:46 +0530 | [diff] [blame] | 4693 | if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && |
| 4694 | out->a2dp_compress_mute && |
Florian Pfister | 1a84f31 | 2018-07-19 14:38:18 +0200 | [diff] [blame] | 4695 | (!(out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) || audio_extn_a2dp_source_is_ready())) { |
Chaithanya Krishna Bacharaju | 69d2e4c | 2017-05-26 18:22:46 +0530 | [diff] [blame] | 4696 | pthread_mutex_lock(&out->compr_mute_lock); |
| 4697 | out->a2dp_compress_mute = false; |
| 4698 | out_set_compr_volume(&out->stream, out->volume_l, out->volume_r); |
| 4699 | pthread_mutex_unlock(&out->compr_mute_lock); |
kunleiz | ba78643 | 2018-01-26 15:31:59 +0800 | [diff] [blame] | 4700 | } else if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP) { |
| 4701 | out_set_voip_volume(&out->stream, out->volume_l, out->volume_r); |
Chaithanya Krishna Bacharaju | 69d2e4c | 2017-05-26 18:22:46 +0530 | [diff] [blame] | 4702 | } |
Shiv Maliyappanahalli | 6b32c4c | 2015-11-04 18:10:20 -0800 | [diff] [blame] | 4703 | } |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 4704 | } |
| 4705 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4706 | pthread_mutex_unlock(&adev->lock); |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 4707 | pthread_mutex_unlock(&out->lock); |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 4708 | |
| 4709 | /*handles device and call state changes*/ |
| 4710 | audio_extn_extspk_update(adev->extspk); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4711 | } |
Kevin Rocard | fce1900 | 2017-08-07 19:21:36 -0700 | [diff] [blame] | 4712 | routing_fail: |
Apoorv Raghuvanshi | 6e26284 | 2013-10-06 14:39:35 -0700 | [diff] [blame] | 4713 | |
| 4714 | if (out == adev->primary_output) { |
| 4715 | pthread_mutex_lock(&adev->lock); |
| 4716 | audio_extn_set_parameters(adev, parms); |
| 4717 | pthread_mutex_unlock(&adev->lock); |
| 4718 | } |
Subhash Chandra Bose Naripeddy | 16ff4f8 | 2014-04-01 21:03:10 -0700 | [diff] [blame] | 4719 | if (is_offload_usecase(out->usecase)) { |
Shiv Maliyappanahalli | 736d4ce | 2015-09-28 15:23:06 -0700 | [diff] [blame] | 4720 | lock_output_stream(out); |
Haynes Mathew George | 352f27b | 2013-07-26 00:00:15 -0700 | [diff] [blame] | 4721 | parse_compress_metadata(out, parms); |
Jitendra Naruka | 1b6513f | 2014-11-22 19:34:13 -0800 | [diff] [blame] | 4722 | |
| 4723 | audio_extn_dts_create_state_notifier_node(out->usecase); |
| 4724 | audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate, |
| 4725 | popcount(out->channel_mask), |
| 4726 | out->playback_started); |
| 4727 | |
Krishnankutty Kolathappilly | eb78be7 | 2013-12-15 12:03:07 -0800 | [diff] [blame] | 4728 | pthread_mutex_unlock(&out->lock); |
Haynes Mathew George | 352f27b | 2013-07-26 00:00:15 -0700 | [diff] [blame] | 4729 | } |
Apoorv Raghuvanshi | 6e26284 | 2013-10-06 14:39:35 -0700 | [diff] [blame] | 4730 | |
Surendar Karka | f51b584 | 2018-04-26 11:28:38 +0530 | [diff] [blame] | 4731 | err = str_parms_get_str(parms, AUDIO_PARAMETER_DUAL_MONO, value, |
| 4732 | sizeof(value)); |
| 4733 | if (err >= 0) { |
| 4734 | if (!strncmp("true", value, sizeof("true")) || atoi(value)) |
| 4735 | audio_extn_send_dual_mono_mixing_coefficients(out); |
| 4736 | } |
| 4737 | |
Dhananjay Kumar | 4d91c1a | 2016-12-01 23:27:29 +0530 | [diff] [blame] | 4738 | err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_PROFILE, value, sizeof(value)); |
| 4739 | if (err >= 0) { |
| 4740 | strlcpy(out->profile, value, sizeof(out->profile)); |
| 4741 | ALOGV("updating stream profile with value '%s'", out->profile); |
| 4742 | lock_output_stream(out); |
| 4743 | audio_extn_utils_update_stream_output_app_type_cfg(adev->platform, |
| 4744 | &adev->streams_output_cfg_list, |
Aalique Grahame | 65780b5 | 2017-09-27 14:59:56 -0700 | [diff] [blame] | 4745 | out->devices, out->flags, out->hal_op_format, |
Dhananjay Kumar | 4d91c1a | 2016-12-01 23:27:29 +0530 | [diff] [blame] | 4746 | out->sample_rate, out->bit_width, |
| 4747 | out->channel_mask, out->profile, |
| 4748 | &out->app_type_cfg); |
| 4749 | pthread_mutex_unlock(&out->lock); |
| 4750 | } |
| 4751 | |
Alexy Joseph | 9898883 | 2017-01-13 14:56:59 -0800 | [diff] [blame] | 4752 | //suspend, resume handling block |
Aniket Kumar Lata | 932f487 | 2017-11-06 18:29:44 -0800 | [diff] [blame] | 4753 | //remove QOS only if vendor.audio.hal.dynamic.qos.config.supported is set to true |
| 4754 | // and vendor.audio.hal.output.suspend.supported is set to true |
| 4755 | if (out->hal_output_suspend_supported && out->dynamic_pm_qos_config_supported) { |
Alexy Joseph | 9898883 | 2017-01-13 14:56:59 -0800 | [diff] [blame] | 4756 | //check suspend parameter only for low latency and if the property |
| 4757 | //is enabled |
| 4758 | if (str_parms_get_str(parms, "suspend_playback", value, sizeof(value)) >= 0) { |
| 4759 | ALOGI("%s: got suspend_playback %s", __func__, value); |
| 4760 | lock_output_stream(out); |
| 4761 | if (!strncmp(value, "false", 5)) { |
| 4762 | //suspend_playback=false is supposed to set QOS value back to 75% |
| 4763 | //the mixer control sent with value Enable will achieve that |
| 4764 | ret = audio_route_apply_and_update_path(adev->audio_route, out->pm_qos_mixer_path); |
| 4765 | } else if (!strncmp (value, "true", 4)) { |
| 4766 | //suspend_playback=true is supposed to remove QOS value |
| 4767 | //resetting the mixer control will set the default value |
| 4768 | //for the mixer control which is Disable and this removes the QOS vote |
| 4769 | ret = audio_route_reset_and_update_path(adev->audio_route, out->pm_qos_mixer_path); |
| 4770 | } else { |
| 4771 | ALOGE("%s: Wrong value sent for suspend_playback, expected true/false," |
| 4772 | " got %s", __func__, value); |
| 4773 | ret = -1; |
| 4774 | } |
| 4775 | |
| 4776 | if (ret != 0) { |
| 4777 | ALOGE("%s: %s mixer ctl failed with %d, ignore suspend/resume setparams", |
| 4778 | __func__, out->pm_qos_mixer_path, ret); |
| 4779 | } |
| 4780 | |
| 4781 | pthread_mutex_unlock(&out->lock); |
| 4782 | } |
| 4783 | } |
Vignesh Kulothungan | 39fc6a2 | 2019-08-01 00:55:59 -0700 | [diff] [blame] | 4784 | |
Alexy Joseph | 9898883 | 2017-01-13 14:56:59 -0800 | [diff] [blame] | 4785 | //end suspend, resume handling block |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4786 | str_parms_destroy(parms); |
Preetam Singh Ranawat | a5f32b4 | 2014-09-23 12:12:47 +0530 | [diff] [blame] | 4787 | error: |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 4788 | ALOGV("%s: exit: code(%d)", __func__, ret); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4789 | return ret; |
| 4790 | } |
| 4791 | |
Paul McLean | a50b733 | 2018-12-17 08:24:21 -0700 | [diff] [blame] | 4792 | static int in_set_microphone_direction(const struct audio_stream_in *stream, |
| 4793 | audio_microphone_direction_t dir) { |
justinweng | 20fb6d8 | 2019-02-21 18:49:00 -0700 | [diff] [blame] | 4794 | struct stream_in *in = (struct stream_in *)stream; |
| 4795 | |
| 4796 | ALOGVV("%s: standby %d source %d dir %d", __func__, in->standby, in->source, dir); |
| 4797 | |
| 4798 | in->direction = dir; |
| 4799 | |
| 4800 | if (in->standby) |
| 4801 | return 0; |
| 4802 | |
| 4803 | return audio_extn_audiozoom_set_microphone_direction(in, dir); |
Paul McLean | a50b733 | 2018-12-17 08:24:21 -0700 | [diff] [blame] | 4804 | } |
| 4805 | |
| 4806 | static int in_set_microphone_field_dimension(const struct audio_stream_in *stream, float zoom) { |
justinweng | 20fb6d8 | 2019-02-21 18:49:00 -0700 | [diff] [blame] | 4807 | struct stream_in *in = (struct stream_in *)stream; |
| 4808 | |
| 4809 | ALOGVV("%s: standby %d source %d zoom %f", __func__, in->standby, in->source, zoom); |
| 4810 | |
| 4811 | if (zoom > 1.0 || zoom < -1.0) |
| 4812 | return -EINVAL; |
| 4813 | |
| 4814 | in->zoom = zoom; |
| 4815 | |
| 4816 | if (in->standby) |
| 4817 | return 0; |
| 4818 | |
| 4819 | return audio_extn_audiozoom_set_microphone_field_dimension(in, zoom); |
Paul McLean | a50b733 | 2018-12-17 08:24:21 -0700 | [diff] [blame] | 4820 | } |
| 4821 | |
| 4822 | |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 4823 | static bool stream_get_parameter_channels(struct str_parms *query, |
| 4824 | struct str_parms *reply, |
| 4825 | audio_channel_mask_t *supported_channel_masks) { |
| 4826 | int ret = -1; |
| 4827 | char value[512]; |
| 4828 | bool first = true; |
| 4829 | size_t i, j; |
| 4830 | |
| 4831 | if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) { |
| 4832 | ret = 0; |
| 4833 | value[0] = '\0'; |
| 4834 | i = 0; |
| 4835 | while (supported_channel_masks[i] != 0) { |
| 4836 | for (j = 0; j < ARRAY_SIZE(channels_name_to_enum_table); j++) { |
| 4837 | if (channels_name_to_enum_table[j].value == supported_channel_masks[i]) { |
| 4838 | if (!first) |
Satya Krishna Pindiproli | b22ac72 | 2017-10-09 15:44:16 +0530 | [diff] [blame] | 4839 | strlcat(value, "|", sizeof(value)); |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 4840 | |
Satya Krishna Pindiproli | b22ac72 | 2017-10-09 15:44:16 +0530 | [diff] [blame] | 4841 | strlcat(value, channels_name_to_enum_table[j].name, sizeof(value)); |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 4842 | first = false; |
| 4843 | break; |
| 4844 | } |
| 4845 | } |
| 4846 | i++; |
| 4847 | } |
| 4848 | str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value); |
| 4849 | } |
| 4850 | return ret == 0; |
| 4851 | } |
| 4852 | |
| 4853 | static bool stream_get_parameter_formats(struct str_parms *query, |
| 4854 | struct str_parms *reply, |
| 4855 | audio_format_t *supported_formats) { |
| 4856 | int ret = -1; |
| 4857 | char value[256]; |
| 4858 | size_t i, j; |
| 4859 | bool first = true; |
| 4860 | |
| 4861 | if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) { |
| 4862 | ret = 0; |
| 4863 | value[0] = '\0'; |
| 4864 | i = 0; |
| 4865 | while (supported_formats[i] != 0) { |
| 4866 | for (j = 0; j < ARRAY_SIZE(formats_name_to_enum_table); j++) { |
| 4867 | if (formats_name_to_enum_table[j].value == supported_formats[i]) { |
| 4868 | if (!first) { |
Satya Krishna Pindiproli | b22ac72 | 2017-10-09 15:44:16 +0530 | [diff] [blame] | 4869 | strlcat(value, "|", sizeof(value)); |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 4870 | } |
Satya Krishna Pindiproli | b22ac72 | 2017-10-09 15:44:16 +0530 | [diff] [blame] | 4871 | strlcat(value, formats_name_to_enum_table[j].name, sizeof(value)); |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 4872 | first = false; |
| 4873 | break; |
| 4874 | } |
| 4875 | } |
| 4876 | i++; |
| 4877 | } |
| 4878 | str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_FORMATS, value); |
| 4879 | } |
| 4880 | return ret == 0; |
| 4881 | } |
| 4882 | |
| 4883 | static bool stream_get_parameter_rates(struct str_parms *query, |
| 4884 | struct str_parms *reply, |
| 4885 | uint32_t *supported_sample_rates) { |
| 4886 | |
| 4887 | int i; |
| 4888 | char value[256]; |
| 4889 | int ret = -1; |
| 4890 | if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) { |
| 4891 | ret = 0; |
| 4892 | value[0] = '\0'; |
| 4893 | i=0; |
| 4894 | int cursor = 0; |
| 4895 | while (supported_sample_rates[i]) { |
| 4896 | int avail = sizeof(value) - cursor; |
| 4897 | ret = snprintf(value + cursor, avail, "%s%d", |
| 4898 | cursor > 0 ? "|" : "", |
| 4899 | supported_sample_rates[i]); |
| 4900 | if (ret < 0 || ret >= avail) { |
| 4901 | // if cursor is at the last element of the array |
| 4902 | // overwrite with \0 is duplicate work as |
| 4903 | // snprintf already put a \0 in place. |
| 4904 | // else |
| 4905 | // we had space to write the '|' at value[cursor] |
| 4906 | // (which will be overwritten) or no space to fill |
| 4907 | // the first element (=> cursor == 0) |
| 4908 | value[cursor] = '\0'; |
| 4909 | break; |
| 4910 | } |
| 4911 | cursor += ret; |
| 4912 | ++i; |
| 4913 | } |
| 4914 | str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES, |
| 4915 | value); |
| 4916 | } |
| 4917 | return ret >= 0; |
| 4918 | } |
| 4919 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4920 | static char* out_get_parameters(const struct audio_stream *stream, const char *keys) |
| 4921 | { |
| 4922 | struct stream_out *out = (struct stream_out *)stream; |
| 4923 | struct str_parms *query = str_parms_create_str(keys); |
Alexy Joseph | aee4fdd | 2016-01-29 13:02:07 -0800 | [diff] [blame] | 4924 | char *str = (char*) NULL; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4925 | char value[256]; |
| 4926 | struct str_parms *reply = str_parms_create(); |
| 4927 | size_t i, j; |
| 4928 | int ret; |
| 4929 | bool first = true; |
Haynes Mathew George | b51ceb1 | 2014-06-30 13:56:18 -0700 | [diff] [blame] | 4930 | |
| 4931 | if (!query || !reply) { |
Alexy Joseph | aee4fdd | 2016-01-29 13:02:07 -0800 | [diff] [blame] | 4932 | if (reply) { |
| 4933 | str_parms_destroy(reply); |
| 4934 | } |
| 4935 | if (query) { |
| 4936 | str_parms_destroy(query); |
| 4937 | } |
Haynes Mathew George | b51ceb1 | 2014-06-30 13:56:18 -0700 | [diff] [blame] | 4938 | ALOGE("out_get_parameters: failed to allocate mem for query or reply"); |
| 4939 | return NULL; |
| 4940 | } |
| 4941 | |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 4942 | ALOGV("%s: %s enter: keys - %s", __func__, use_case_table[out->usecase], keys); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4943 | ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value)); |
| 4944 | if (ret >= 0) { |
| 4945 | value[0] = '\0'; |
| 4946 | i = 0; |
| 4947 | while (out->supported_channel_masks[i] != 0) { |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 4948 | for (j = 0; j < ARRAY_SIZE(channels_name_to_enum_table); j++) { |
| 4949 | if (channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) { |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4950 | if (!first) { |
Apoorv Raghuvanshi | 8880cac | 2015-02-06 15:33:49 -0800 | [diff] [blame] | 4951 | strlcat(value, "|", sizeof(value)); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4952 | } |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 4953 | strlcat(value, channels_name_to_enum_table[j].name, sizeof(value)); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4954 | first = false; |
| 4955 | break; |
| 4956 | } |
| 4957 | } |
| 4958 | i++; |
| 4959 | } |
| 4960 | str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value); |
| 4961 | str = str_parms_to_str(reply); |
| 4962 | } else { |
Narsinga Rao Chella | 05573b7 | 2013-11-15 15:21:40 -0800 | [diff] [blame] | 4963 | voice_extn_out_get_parameters(out, query, reply); |
| 4964 | str = str_parms_to_str(reply); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4965 | } |
Pradnya Chaphekar | 80a8cfb | 2014-10-20 16:17:01 -0700 | [diff] [blame] | 4966 | |
Alexy Joseph | 62142aa | 2015-11-16 15:10:34 -0800 | [diff] [blame] | 4967 | |
| 4968 | ret = str_parms_get_str(query, "is_direct_pcm_track", value, sizeof(value)); |
| 4969 | if (ret >= 0) { |
| 4970 | value[0] = '\0'; |
Dhananjay Kumar | ac34158 | 2017-02-23 23:42:25 +0530 | [diff] [blame] | 4971 | if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT && |
| 4972 | !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) { |
Alexy Joseph | 62142aa | 2015-11-16 15:10:34 -0800 | [diff] [blame] | 4973 | ALOGV("in direct_pcm"); |
Satya Krishna Pindiproli | b665554 | 2017-07-03 19:38:19 +0530 | [diff] [blame] | 4974 | strlcat(value, "true", sizeof(value)); |
Alexy Joseph | 62142aa | 2015-11-16 15:10:34 -0800 | [diff] [blame] | 4975 | } else { |
| 4976 | ALOGV("not in direct_pcm"); |
Sharad Sangle | 3dd5a4a | 2015-12-10 18:39:17 +0530 | [diff] [blame] | 4977 | strlcat(value, "false", sizeof(value)); |
Alexy Joseph | 62142aa | 2015-11-16 15:10:34 -0800 | [diff] [blame] | 4978 | } |
| 4979 | str_parms_add_str(reply, "is_direct_pcm_track", value); |
Alexy Joseph | aee4fdd | 2016-01-29 13:02:07 -0800 | [diff] [blame] | 4980 | if (str) |
| 4981 | free(str); |
Alexy Joseph | 62142aa | 2015-11-16 15:10:34 -0800 | [diff] [blame] | 4982 | str = str_parms_to_str(reply); |
| 4983 | } |
| 4984 | |
Pradnya Chaphekar | 80a8cfb | 2014-10-20 16:17:01 -0700 | [diff] [blame] | 4985 | ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS, value, sizeof(value)); |
| 4986 | if (ret >= 0) { |
| 4987 | value[0] = '\0'; |
| 4988 | i = 0; |
| 4989 | first = true; |
| 4990 | while (out->supported_formats[i] != 0) { |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 4991 | for (j = 0; j < ARRAY_SIZE(formats_name_to_enum_table); j++) { |
| 4992 | if (formats_name_to_enum_table[j].value == out->supported_formats[i]) { |
Pradnya Chaphekar | 80a8cfb | 2014-10-20 16:17:01 -0700 | [diff] [blame] | 4993 | if (!first) { |
Apoorv Raghuvanshi | 8880cac | 2015-02-06 15:33:49 -0800 | [diff] [blame] | 4994 | strlcat(value, "|", sizeof(value)); |
Pradnya Chaphekar | 80a8cfb | 2014-10-20 16:17:01 -0700 | [diff] [blame] | 4995 | } |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 4996 | strlcat(value, formats_name_to_enum_table[j].name, sizeof(value)); |
Pradnya Chaphekar | 80a8cfb | 2014-10-20 16:17:01 -0700 | [diff] [blame] | 4997 | first = false; |
| 4998 | break; |
| 4999 | } |
| 5000 | } |
| 5001 | i++; |
| 5002 | } |
| 5003 | str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_FORMATS, value); |
Alexy Joseph | aee4fdd | 2016-01-29 13:02:07 -0800 | [diff] [blame] | 5004 | if (str) |
| 5005 | free(str); |
Pradnya Chaphekar | 80a8cfb | 2014-10-20 16:17:01 -0700 | [diff] [blame] | 5006 | str = str_parms_to_str(reply); |
| 5007 | } |
Mingming Yin | 3a941d4 | 2016-02-17 18:08:05 -0800 | [diff] [blame] | 5008 | |
| 5009 | ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES, value, sizeof(value)); |
| 5010 | if (ret >= 0) { |
| 5011 | value[0] = '\0'; |
| 5012 | i = 0; |
| 5013 | first = true; |
| 5014 | while (out->supported_sample_rates[i] != 0) { |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 5015 | for (j = 0; j < ARRAY_SIZE(out_sample_rates_name_to_enum_table); j++) { |
| 5016 | if (out_sample_rates_name_to_enum_table[j].value == out->supported_sample_rates[i]) { |
Mingming Yin | 3a941d4 | 2016-02-17 18:08:05 -0800 | [diff] [blame] | 5017 | if (!first) { |
| 5018 | strlcat(value, "|", sizeof(value)); |
| 5019 | } |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 5020 | strlcat(value, out_sample_rates_name_to_enum_table[j].name, sizeof(value)); |
Mingming Yin | 3a941d4 | 2016-02-17 18:08:05 -0800 | [diff] [blame] | 5021 | first = false; |
| 5022 | break; |
| 5023 | } |
| 5024 | } |
| 5025 | i++; |
| 5026 | } |
| 5027 | str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES, value); |
| 5028 | if (str) |
| 5029 | free(str); |
| 5030 | str = str_parms_to_str(reply); |
| 5031 | } |
| 5032 | |
Alexy Joseph | 9898883 | 2017-01-13 14:56:59 -0800 | [diff] [blame] | 5033 | if (str_parms_get_str(query, "supports_hw_suspend", value, sizeof(value)) >= 0) { |
| 5034 | //only low latency track supports suspend_resume |
| 5035 | str_parms_add_int(reply, "supports_hw_suspend", |
Aniket Kumar Lata | 932f487 | 2017-11-06 18:29:44 -0800 | [diff] [blame] | 5036 | (out->hal_output_suspend_supported)); |
Alexy Joseph | 9898883 | 2017-01-13 14:56:59 -0800 | [diff] [blame] | 5037 | if (str) |
| 5038 | free(str); |
| 5039 | str = str_parms_to_str(reply); |
| 5040 | } |
| 5041 | |
| 5042 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5043 | str_parms_destroy(query); |
| 5044 | str_parms_destroy(reply); |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 5045 | ALOGV("%s: exit: returns - %s", __func__, str); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5046 | return str; |
| 5047 | } |
| 5048 | |
| 5049 | static uint32_t out_get_latency(const struct audio_stream_out *stream) |
| 5050 | { |
Haynes Mathew George | 5beddd4 | 2016-06-27 18:33:40 -0700 | [diff] [blame] | 5051 | uint32_t period_ms; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5052 | struct stream_out *out = (struct stream_out *)stream; |
Alexy Joseph | aa54c87 | 2014-12-03 02:46:47 -0800 | [diff] [blame] | 5053 | uint32_t latency = 0; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5054 | |
Alexy Joseph | aa54c87 | 2014-12-03 02:46:47 -0800 | [diff] [blame] | 5055 | if (is_offload_usecase(out->usecase)) { |
Manish Dewangan | 07de214 | 2017-02-27 19:27:20 +0530 | [diff] [blame] | 5056 | lock_output_stream(out); |
| 5057 | latency = audio_extn_utils_compress_get_dsp_latency(out); |
| 5058 | pthread_mutex_unlock(&out->lock); |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 5059 | } else if ((out->realtime) || |
| 5060 | (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP)) { |
Haynes Mathew George | 5beddd4 | 2016-06-27 18:33:40 -0700 | [diff] [blame] | 5061 | // since the buffer won't be filled up faster than realtime, |
| 5062 | // return a smaller number |
| 5063 | if (out->config.rate) |
| 5064 | period_ms = (out->af_period_multiplier * out->config.period_size * |
| 5065 | 1000) / (out->config.rate); |
| 5066 | else |
| 5067 | period_ms = 0; |
| 5068 | latency = period_ms + platform_render_latency(out->usecase)/1000; |
Alexy Joseph | aa54c87 | 2014-12-03 02:46:47 -0800 | [diff] [blame] | 5069 | } else { |
| 5070 | latency = (out->config.period_count * out->config.period_size * 1000) / |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 5071 | (out->config.rate); |
Alexy Joseph | aa54c87 | 2014-12-03 02:46:47 -0800 | [diff] [blame] | 5072 | } |
| 5073 | |
yidongh | 0515e04 | 2017-07-06 15:00:34 +0800 | [diff] [blame] | 5074 | if (AUDIO_DEVICE_OUT_ALL_A2DP & out->devices) |
Aniket Kumar Lata | d5972fa | 2017-02-08 13:53:48 -0800 | [diff] [blame] | 5075 | latency += audio_extn_a2dp_get_encoder_latency(); |
| 5076 | |
Anish Kumar | 50ebcbf | 2014-12-09 04:01:39 +0530 | [diff] [blame] | 5077 | ALOGV("%s: Latency %d", __func__, latency); |
Alexy Joseph | aa54c87 | 2014-12-03 02:46:47 -0800 | [diff] [blame] | 5078 | return latency; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5079 | } |
| 5080 | |
Preetam Singh Ranawat | cb6212e | 2016-07-19 18:33:53 +0530 | [diff] [blame] | 5081 | static float AmpToDb(float amplification) |
| 5082 | { |
Preetam Singh Ranawat | f5fbdd6 | 2016-09-29 18:38:31 +0530 | [diff] [blame] | 5083 | float db = DSD_VOLUME_MIN_DB; |
| 5084 | if (amplification > 0) { |
| 5085 | db = 20 * log10(amplification); |
| 5086 | if(db < DSD_VOLUME_MIN_DB) |
| 5087 | return DSD_VOLUME_MIN_DB; |
Preetam Singh Ranawat | cb6212e | 2016-07-19 18:33:53 +0530 | [diff] [blame] | 5088 | } |
Preetam Singh Ranawat | f5fbdd6 | 2016-09-29 18:38:31 +0530 | [diff] [blame] | 5089 | return db; |
Preetam Singh Ranawat | cb6212e | 2016-07-19 18:33:53 +0530 | [diff] [blame] | 5090 | } |
| 5091 | |
Arun Mirpuri | 5d17087 | 2019-03-26 13:21:31 -0700 | [diff] [blame] | 5092 | static int out_set_mmap_volume(struct audio_stream_out *stream, float left, |
| 5093 | float right) |
| 5094 | { |
| 5095 | struct stream_out *out = (struct stream_out *)stream; |
| 5096 | long volume = 0; |
| 5097 | char mixer_ctl_name[128] = ""; |
| 5098 | struct audio_device *adev = out->dev; |
| 5099 | struct mixer_ctl *ctl = NULL; |
| 5100 | int pcm_device_id = platform_get_pcm_device_id(out->usecase, |
| 5101 | PCM_PLAYBACK); |
| 5102 | |
| 5103 | snprintf(mixer_ctl_name, sizeof(mixer_ctl_name), |
| 5104 | "Playback %d Volume", pcm_device_id); |
| 5105 | ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name); |
| 5106 | if (!ctl) { |
| 5107 | ALOGE("%s: Could not get ctl for mixer cmd - %s", |
| 5108 | __func__, mixer_ctl_name); |
| 5109 | return -EINVAL; |
| 5110 | } |
| 5111 | if (left != right) |
| 5112 | ALOGW("%s: Left and right channel volume mismatch:%f,%f", |
| 5113 | __func__, left, right); |
| 5114 | volume = (long)(left * (MMAP_PLAYBACK_VOLUME_MAX*1.0)); |
| 5115 | if (mixer_ctl_set_value(ctl, 0, volume) < 0){ |
| 5116 | ALOGE("%s:ctl for mixer cmd - %s, volume %ld returned error", |
| 5117 | __func__, mixer_ctl_name, volume); |
| 5118 | return -EINVAL; |
| 5119 | } |
| 5120 | return 0; |
| 5121 | } |
| 5122 | |
Chaithanya Krishna Bacharaju | 69d2e4c | 2017-05-26 18:22:46 +0530 | [diff] [blame] | 5123 | static int out_set_compr_volume(struct audio_stream_out *stream, float left, |
| 5124 | float right) |
| 5125 | { |
| 5126 | struct stream_out *out = (struct stream_out *)stream; |
Manish Dewangan | 338c50a | 2017-09-12 15:22:03 +0530 | [diff] [blame] | 5127 | long volume[2]; |
Chaithanya Krishna Bacharaju | 69d2e4c | 2017-05-26 18:22:46 +0530 | [diff] [blame] | 5128 | char mixer_ctl_name[128]; |
| 5129 | struct audio_device *adev = out->dev; |
| 5130 | struct mixer_ctl *ctl; |
| 5131 | int pcm_device_id = platform_get_pcm_device_id(out->usecase, |
| 5132 | PCM_PLAYBACK); |
| 5133 | |
| 5134 | snprintf(mixer_ctl_name, sizeof(mixer_ctl_name), |
| 5135 | "Compress Playback %d Volume", pcm_device_id); |
| 5136 | ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name); |
| 5137 | if (!ctl) { |
| 5138 | ALOGE("%s: Could not get ctl for mixer cmd - %s", |
| 5139 | __func__, mixer_ctl_name); |
| 5140 | return -EINVAL; |
| 5141 | } |
| 5142 | ALOGE("%s:ctl for mixer cmd - %s, left %f, right %f", |
| 5143 | __func__, mixer_ctl_name, left, right); |
| 5144 | volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX); |
| 5145 | volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX); |
| 5146 | mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0])); |
| 5147 | |
| 5148 | return 0; |
| 5149 | } |
| 5150 | |
Zhou Song | 2b8f28f | 2017-09-11 10:51:38 +0800 | [diff] [blame] | 5151 | static int out_set_voip_volume(struct audio_stream_out *stream, float left, |
| 5152 | float right) |
| 5153 | { |
| 5154 | struct stream_out *out = (struct stream_out *)stream; |
| 5155 | char mixer_ctl_name[] = "App Type Gain"; |
| 5156 | struct audio_device *adev = out->dev; |
| 5157 | struct mixer_ctl *ctl; |
Manish Dewangan | 338c50a | 2017-09-12 15:22:03 +0530 | [diff] [blame] | 5158 | long set_values[4]; |
Zhou Song | 2b8f28f | 2017-09-11 10:51:38 +0800 | [diff] [blame] | 5159 | |
Aniket Kumar Lata | 8426d1f | 2019-06-10 15:25:53 -0700 | [diff] [blame] | 5160 | if (!is_valid_volume(left, right)) { |
| 5161 | ALOGE("%s: Invalid stream volume for left=%f, right=%f", |
| 5162 | __func__, left, right); |
| 5163 | return -EINVAL; |
| 5164 | } |
| 5165 | |
Zhou Song | 2b8f28f | 2017-09-11 10:51:38 +0800 | [diff] [blame] | 5166 | ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name); |
| 5167 | if (!ctl) { |
| 5168 | ALOGE("%s: Could not get ctl for mixer cmd - %s", |
| 5169 | __func__, mixer_ctl_name); |
| 5170 | return -EINVAL; |
| 5171 | } |
| 5172 | |
| 5173 | set_values[0] = 0; //0: Rx Session 1:Tx Session |
| 5174 | set_values[1] = out->app_type_cfg.app_type; |
Manish Dewangan | 338c50a | 2017-09-12 15:22:03 +0530 | [diff] [blame] | 5175 | set_values[2] = (long)(left * VOIP_PLAYBACK_VOLUME_MAX); |
| 5176 | set_values[3] = (long)(right * VOIP_PLAYBACK_VOLUME_MAX); |
Zhou Song | 2b8f28f | 2017-09-11 10:51:38 +0800 | [diff] [blame] | 5177 | |
| 5178 | mixer_ctl_set_array(ctl, set_values, ARRAY_SIZE(set_values)); |
| 5179 | return 0; |
| 5180 | } |
| 5181 | |
Ramu Gottipati | 97bdcfb | 2018-04-13 17:58:24 +0530 | [diff] [blame] | 5182 | static int out_set_pcm_volume(struct audio_stream_out *stream, float left, |
| 5183 | float right) |
| 5184 | { |
| 5185 | struct stream_out *out = (struct stream_out *)stream; |
| 5186 | /* Volume control for pcm playback */ |
| 5187 | if (left != right) { |
| 5188 | return -EINVAL; |
| 5189 | } else { |
| 5190 | char mixer_ctl_name[128]; |
| 5191 | struct audio_device *adev = out->dev; |
| 5192 | struct mixer_ctl *ctl; |
| 5193 | int pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK); |
| 5194 | snprintf(mixer_ctl_name, sizeof(mixer_ctl_name), "Playback %d Volume", pcm_device_id); |
| 5195 | ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name); |
| 5196 | if (!ctl) { |
| 5197 | ALOGE("%s : Could not get ctl for mixer cmd - %s", __func__, mixer_ctl_name); |
| 5198 | return -EINVAL; |
| 5199 | } |
| 5200 | |
| 5201 | int volume = (int) (left * PCM_PLAYBACK_VOLUME_MAX); |
| 5202 | int ret = mixer_ctl_set_value(ctl, 0, volume); |
| 5203 | if (ret < 0) { |
| 5204 | ALOGE("%s: Could not set ctl, error:%d ", __func__, ret); |
| 5205 | return -EINVAL; |
| 5206 | } |
| 5207 | |
| 5208 | ALOGV("%s : Pcm set volume value %d left %f", __func__, volume, left); |
| 5209 | |
| 5210 | return 0; |
| 5211 | } |
| 5212 | } |
| 5213 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5214 | static int out_set_volume(struct audio_stream_out *stream, float left, |
| 5215 | float right) |
| 5216 | { |
Eric Laurent | a9024de | 2013-04-04 09:19:12 -0700 | [diff] [blame] | 5217 | struct stream_out *out = (struct stream_out *)stream; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 5218 | int volume[2]; |
Chaithanya Krishna Bacharaju | 69d2e4c | 2017-05-26 18:22:46 +0530 | [diff] [blame] | 5219 | int ret = 0; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 5220 | |
Arun Mirpuri | 5d17087 | 2019-03-26 13:21:31 -0700 | [diff] [blame] | 5221 | ALOGD("%s: called with left_vol=%f, right_vol=%f", __func__, left, right); |
Eric Laurent | a9024de | 2013-04-04 09:19:12 -0700 | [diff] [blame] | 5222 | if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) { |
| 5223 | /* only take left channel into account: the API is for stereo anyway */ |
| 5224 | out->muted = (left == 0.0f); |
| 5225 | return 0; |
Subhash Chandra Bose Naripeddy | 16ff4f8 | 2014-04-01 21:03:10 -0700 | [diff] [blame] | 5226 | } else if (is_offload_usecase(out->usecase)) { |
Satish Babu Patakokila | 1caa1b7 | 2016-05-24 13:47:08 +0530 | [diff] [blame] | 5227 | if (audio_extn_passthru_is_passthrough_stream(out)) { |
Pradnya Chaphekar | 80a8cfb | 2014-10-20 16:17:01 -0700 | [diff] [blame] | 5228 | /* |
| 5229 | * Set mute or umute on HDMI passthrough stream. |
| 5230 | * Only take left channel into account. |
| 5231 | * Mute is 0 and unmute 1 |
| 5232 | */ |
Satish Babu Patakokila | 1caa1b7 | 2016-05-24 13:47:08 +0530 | [diff] [blame] | 5233 | audio_extn_passthru_set_volume(out, (left == 0.0f)); |
Preetam Singh Ranawat | cb6212e | 2016-07-19 18:33:53 +0530 | [diff] [blame] | 5234 | } else if (out->format == AUDIO_FORMAT_DSD){ |
| 5235 | char mixer_ctl_name[128] = "DSD Volume"; |
| 5236 | struct audio_device *adev = out->dev; |
| 5237 | struct mixer_ctl *ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name); |
| 5238 | |
| 5239 | if (!ctl) { |
| 5240 | ALOGE("%s: Could not get ctl for mixer cmd - %s", |
| 5241 | __func__, mixer_ctl_name); |
| 5242 | return -EINVAL; |
| 5243 | } |
Manish Dewangan | 338c50a | 2017-09-12 15:22:03 +0530 | [diff] [blame] | 5244 | volume[0] = (long)(AmpToDb(left)); |
| 5245 | volume[1] = (long)(AmpToDb(right)); |
Preetam Singh Ranawat | cb6212e | 2016-07-19 18:33:53 +0530 | [diff] [blame] | 5246 | mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0])); |
| 5247 | return 0; |
Philippe Gravel | 1c9ac35 | 2019-05-28 16:08:37 -0700 | [diff] [blame] | 5248 | } else if ((out->devices & AUDIO_DEVICE_OUT_BUS) && |
Derek Chen | df05eea | 2019-08-01 13:57:49 -0700 | [diff] [blame] | 5249 | (out->car_audio_stream == CAR_AUDIO_STREAM_MEDIA)) { |
Philippe Gravel | 1c9ac35 | 2019-05-28 16:08:37 -0700 | [diff] [blame] | 5250 | ALOGD("%s: Overriding offload set volume for media bus stream", __func__); |
| 5251 | struct listnode *node = NULL; |
| 5252 | list_for_each(node, &adev->active_outputs_list) { |
| 5253 | streams_output_ctxt_t *out_ctxt = node_to_item(node, |
| 5254 | streams_output_ctxt_t, |
| 5255 | list); |
| 5256 | if (out_ctxt->output->usecase == USECASE_AUDIO_PLAYBACK_MEDIA) { |
| 5257 | out->volume_l = out_ctxt->output->volume_l; |
| 5258 | out->volume_r = out_ctxt->output->volume_r; |
| 5259 | } |
| 5260 | } |
| 5261 | if (!out->a2dp_compress_mute) { |
| 5262 | ret = out_set_compr_volume(&out->stream, out->volume_l, out->volume_r); |
| 5263 | } |
| 5264 | return ret; |
Pradnya Chaphekar | 80a8cfb | 2014-10-20 16:17:01 -0700 | [diff] [blame] | 5265 | } else { |
Chaithanya Krishna Bacharaju | 69d2e4c | 2017-05-26 18:22:46 +0530 | [diff] [blame] | 5266 | pthread_mutex_lock(&out->compr_mute_lock); |
Arun Mirpuri | 5d17087 | 2019-03-26 13:21:31 -0700 | [diff] [blame] | 5267 | ALOGV("%s: compress mute %d", __func__, out->a2dp_compress_mute); |
Chaithanya Krishna Bacharaju | 69d2e4c | 2017-05-26 18:22:46 +0530 | [diff] [blame] | 5268 | if (!out->a2dp_compress_mute) |
| 5269 | ret = out_set_compr_volume(stream, left, right); |
| 5270 | out->volume_l = left; |
| 5271 | out->volume_r = right; |
| 5272 | pthread_mutex_unlock(&out->compr_mute_lock); |
| 5273 | return ret; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 5274 | } |
Vikram Panduranga | 93f080e | 2017-06-07 18:16:14 -0700 | [diff] [blame] | 5275 | } else if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP) { |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 5276 | out->app_type_cfg.gain[0] = (int)(left * VOIP_PLAYBACK_VOLUME_MAX); |
| 5277 | out->app_type_cfg.gain[1] = (int)(right * VOIP_PLAYBACK_VOLUME_MAX); |
| 5278 | if (!out->standby) { |
| 5279 | audio_extn_utils_send_app_type_gain(out->dev, |
| 5280 | out->app_type_cfg.app_type, |
| 5281 | &out->app_type_cfg.gain[0]); |
Zhou Song | 2b8f28f | 2017-09-11 10:51:38 +0800 | [diff] [blame] | 5282 | ret = out_set_voip_volume(stream, left, right); |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 5283 | } |
Zhou Song | 2b8f28f | 2017-09-11 10:51:38 +0800 | [diff] [blame] | 5284 | out->volume_l = left; |
| 5285 | out->volume_r = right; |
| 5286 | return ret; |
Arun Mirpuri | 5d17087 | 2019-03-26 13:21:31 -0700 | [diff] [blame] | 5287 | } else if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) { |
| 5288 | ALOGV("%s: MMAP set volume called", __func__); |
| 5289 | if (!out->standby) |
| 5290 | ret = out_set_mmap_volume(stream, left, right); |
| 5291 | out->volume_l = left; |
| 5292 | out->volume_r = right; |
| 5293 | return ret; |
Ramu Gottipati | 97bdcfb | 2018-04-13 17:58:24 +0530 | [diff] [blame] | 5294 | } else if (out->usecase == USECASE_AUDIO_PLAYBACK_LOW_LATENCY || |
Ramu Gottipati | 3654709 | 2018-12-28 11:32:09 +0530 | [diff] [blame] | 5295 | out->usecase == USECASE_AUDIO_PLAYBACK_DEEP_BUFFER || |
| 5296 | out->usecase == USECASE_AUDIO_PLAYBACK_ULL) { |
Ramu Gottipati | 97bdcfb | 2018-04-13 17:58:24 +0530 | [diff] [blame] | 5297 | /* Volume control for pcm playback */ |
| 5298 | if (!out->standby) |
| 5299 | ret = out_set_pcm_volume(stream, left, right); |
| 5300 | else |
| 5301 | out->apply_volume = true; |
| 5302 | |
| 5303 | out->volume_l = left; |
| 5304 | out->volume_r = right; |
| 5305 | return ret; |
Derek Chen | f13dd49 | 2018-11-13 14:53:51 -0800 | [diff] [blame] | 5306 | } else if (audio_extn_auto_hal_is_bus_device_usecase(out->usecase)) { |
| 5307 | ALOGV("%s: bus device set volume called", __func__); |
| 5308 | if (!out->standby) |
| 5309 | ret = out_set_pcm_volume(stream, left, right); |
| 5310 | out->volume_l = left; |
| 5311 | out->volume_r = right; |
| 5312 | return ret; |
Eric Laurent | a9024de | 2013-04-04 09:19:12 -0700 | [diff] [blame] | 5313 | } |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 5314 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5315 | return -ENOSYS; |
| 5316 | } |
| 5317 | |
Zhou Song | c967282 | 2017-08-16 16:01:39 +0800 | [diff] [blame] | 5318 | static void update_frames_written(struct stream_out *out, size_t bytes) |
| 5319 | { |
| 5320 | size_t bpf = 0; |
| 5321 | |
| 5322 | if (is_offload_usecase(out->usecase) && !out->non_blocking && |
| 5323 | !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) |
| 5324 | bpf = 1; |
| 5325 | else if (!is_offload_usecase(out->usecase)) |
| 5326 | bpf = audio_bytes_per_sample(out->format) * |
| 5327 | audio_channel_count_from_out_mask(out->channel_mask); |
Zhou Song | 48453a0 | 2018-01-10 17:50:59 +0800 | [diff] [blame] | 5328 | |
| 5329 | pthread_mutex_lock(&out->position_query_lock); |
| 5330 | if (bpf != 0) { |
Zhou Song | c967282 | 2017-08-16 16:01:39 +0800 | [diff] [blame] | 5331 | out->written += bytes / bpf; |
Zhou Song | 48453a0 | 2018-01-10 17:50:59 +0800 | [diff] [blame] | 5332 | clock_gettime(CLOCK_MONOTONIC, &out->writeAt); |
| 5333 | } |
| 5334 | pthread_mutex_unlock(&out->position_query_lock); |
Zhou Song | c967282 | 2017-08-16 16:01:39 +0800 | [diff] [blame] | 5335 | } |
| 5336 | |
Vignesh Kulothungan | a692727 | 2019-02-20 15:17:07 -0800 | [diff] [blame] | 5337 | int split_and_write_audio_haptic_data(struct stream_out *out, |
| 5338 | const void *buffer, size_t bytes_to_write) |
| 5339 | { |
| 5340 | struct audio_device *adev = out->dev; |
| 5341 | |
| 5342 | int ret = 0; |
| 5343 | size_t channel_count = audio_channel_count_from_out_mask(out->channel_mask); |
| 5344 | size_t bytes_per_sample = audio_bytes_per_sample(out->format); |
| 5345 | size_t frame_size = channel_count * bytes_per_sample; |
| 5346 | size_t frame_count = bytes_to_write / frame_size; |
| 5347 | |
| 5348 | bool force_haptic_path = |
| 5349 | property_get_bool("vendor.audio.test_haptic", false); |
| 5350 | |
| 5351 | // extract Haptics data from Audio buffer |
| 5352 | bool alloc_haptic_buffer = false; |
| 5353 | int haptic_channel_count = adev->haptics_config.channels; |
| 5354 | size_t haptic_frame_size = bytes_per_sample * haptic_channel_count; |
| 5355 | size_t audio_frame_size = frame_size - haptic_frame_size; |
| 5356 | size_t total_haptic_buffer_size = frame_count * haptic_frame_size; |
| 5357 | |
| 5358 | if (adev->haptic_buffer == NULL) { |
| 5359 | alloc_haptic_buffer = true; |
| 5360 | } else if (adev->haptic_buffer_size < total_haptic_buffer_size) { |
| 5361 | free(adev->haptic_buffer); |
| 5362 | adev->haptic_buffer_size = 0; |
| 5363 | alloc_haptic_buffer = true; |
| 5364 | } |
| 5365 | |
| 5366 | if (alloc_haptic_buffer) { |
| 5367 | adev->haptic_buffer = (uint8_t *)calloc(1, total_haptic_buffer_size); |
Mingshu Pang | 1513f97 | 2019-05-24 12:43:51 +0800 | [diff] [blame] | 5368 | if(adev->haptic_buffer == NULL) { |
| 5369 | ALOGE("%s: failed to allocate mem for dev->haptic_buffer", __func__); |
| 5370 | return -ENOMEM; |
| 5371 | } |
Vignesh Kulothungan | a692727 | 2019-02-20 15:17:07 -0800 | [diff] [blame] | 5372 | adev->haptic_buffer_size = total_haptic_buffer_size; |
| 5373 | } |
| 5374 | |
| 5375 | size_t src_index = 0, aud_index = 0, hap_index = 0; |
| 5376 | uint8_t *audio_buffer = (uint8_t *)buffer; |
| 5377 | uint8_t *haptic_buffer = adev->haptic_buffer; |
| 5378 | |
| 5379 | // This is required for testing only. This works for stereo data only. |
| 5380 | // One channel is fed to audio stream and other to haptic stream for testing. |
| 5381 | if (force_haptic_path) |
| 5382 | audio_frame_size = haptic_frame_size = bytes_per_sample; |
| 5383 | |
| 5384 | for (size_t i = 0; i < frame_count; i++) { |
| 5385 | memcpy(audio_buffer + aud_index, audio_buffer + src_index, |
| 5386 | audio_frame_size); |
| 5387 | aud_index += audio_frame_size; |
| 5388 | src_index += audio_frame_size; |
| 5389 | |
| 5390 | if (adev->haptic_pcm) |
| 5391 | memcpy(haptic_buffer + hap_index, audio_buffer + src_index, |
| 5392 | haptic_frame_size); |
| 5393 | hap_index += haptic_frame_size; |
| 5394 | src_index += haptic_frame_size; |
| 5395 | |
| 5396 | // This is required for testing only. |
| 5397 | // Discard haptic channel data. |
| 5398 | if (force_haptic_path) |
| 5399 | src_index += haptic_frame_size; |
| 5400 | } |
| 5401 | |
| 5402 | // write to audio pipeline |
| 5403 | ret = pcm_write(out->pcm, (void *)audio_buffer, |
| 5404 | frame_count * audio_frame_size); |
| 5405 | |
| 5406 | // write to haptics pipeline |
| 5407 | if (adev->haptic_pcm) |
| 5408 | ret = pcm_write(adev->haptic_pcm, (void *)adev->haptic_buffer, |
| 5409 | frame_count * haptic_frame_size); |
| 5410 | |
| 5411 | return ret; |
| 5412 | } |
| 5413 | |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 5414 | #ifdef NO_AUDIO_OUT |
| 5415 | static ssize_t out_write_for_no_output(struct audio_stream_out *stream, |
| 5416 | const void *buffer __unused, size_t bytes) |
| 5417 | { |
| 5418 | struct stream_out *out = (struct stream_out *)stream; |
| 5419 | |
| 5420 | /* No Output device supported other than BT for playback. |
| 5421 | * Sleep for the amount of buffer duration |
| 5422 | */ |
| 5423 | lock_output_stream(out); |
| 5424 | usleep(bytes * 1000000 / audio_stream_out_frame_size( |
| 5425 | (const struct audio_stream_out *)&out->stream) / |
| 5426 | out_get_sample_rate(&out->stream.common)); |
| 5427 | pthread_mutex_unlock(&out->lock); |
| 5428 | return bytes; |
| 5429 | } |
| 5430 | #endif |
| 5431 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5432 | static ssize_t out_write(struct audio_stream_out *stream, const void *buffer, |
| 5433 | size_t bytes) |
| 5434 | { |
| 5435 | struct stream_out *out = (struct stream_out *)stream; |
| 5436 | struct audio_device *adev = out->dev; |
Eric Laurent | 6e89524 | 2013-09-05 16:10:57 -0700 | [diff] [blame] | 5437 | ssize_t ret = 0; |
Satish Babu Patakokila | 715b142 | 2017-08-22 14:33:21 +0530 | [diff] [blame] | 5438 | int channels = 0; |
Arun Mirpuri | 7da752a | 2018-09-11 18:01:15 -0700 | [diff] [blame] | 5439 | const size_t frame_size = audio_stream_out_frame_size(stream); |
| 5440 | const size_t frames = (frame_size != 0) ? bytes / frame_size : bytes; |
Dhanalakshmi Siddani | 20628c8 | 2019-01-27 17:31:09 +0530 | [diff] [blame] | 5441 | struct audio_usecase *usecase = NULL; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5442 | |
Haynes Mathew George | 380745d | 2017-10-04 15:27:45 -0700 | [diff] [blame] | 5443 | ATRACE_BEGIN("out_write"); |
Shiv Maliyappanahalli | 736d4ce | 2015-09-28 15:23:06 -0700 | [diff] [blame] | 5444 | lock_output_stream(out); |
Naresh Tanniru | 4c63039 | 2014-05-12 01:05:52 +0530 | [diff] [blame] | 5445 | |
Dhananjay Kumar | e6293dd | 2017-05-25 17:25:30 +0530 | [diff] [blame] | 5446 | if (CARD_STATUS_OFFLINE == out->card_status) { |
Zhou Song | 0b2e5dc | 2015-03-16 14:41:38 +0800 | [diff] [blame] | 5447 | |
Dhananjay Kumar | ac34158 | 2017-02-23 23:42:25 +0530 | [diff] [blame] | 5448 | if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { |
Ashish Jain | bbce432 | 2016-02-16 13:25:27 +0530 | [diff] [blame] | 5449 | /*during SSR for compress usecase we should return error to flinger*/ |
Naresh Tanniru | 8065983 | 2014-06-04 18:17:56 +0530 | [diff] [blame] | 5450 | ALOGD(" copl %s: sound card is not active/SSR state", __func__); |
| 5451 | pthread_mutex_unlock(&out->lock); |
Haynes Mathew George | 380745d | 2017-10-04 15:27:45 -0700 | [diff] [blame] | 5452 | ATRACE_END(); |
Naresh Tanniru | 8065983 | 2014-06-04 18:17:56 +0530 | [diff] [blame] | 5453 | return -ENETRESET; |
Ashish Jain | bbce432 | 2016-02-16 13:25:27 +0530 | [diff] [blame] | 5454 | } else { |
Ashish Jain | bbce432 | 2016-02-16 13:25:27 +0530 | [diff] [blame] | 5455 | ALOGD(" %s: sound card is not active/SSR state", __func__); |
| 5456 | ret= -EIO; |
| 5457 | goto exit; |
Naresh Tanniru | 4c63039 | 2014-05-12 01:05:52 +0530 | [diff] [blame] | 5458 | } |
| 5459 | } |
| 5460 | |
Satish Babu Patakokila | 1caa1b7 | 2016-05-24 13:47:08 +0530 | [diff] [blame] | 5461 | if (audio_extn_passthru_should_drop_data(out)) { |
Ashish Jain | d84fd6a | 2016-07-27 12:33:25 +0530 | [diff] [blame] | 5462 | ALOGV(" %s : Drop data as compress passthrough session is going on", __func__); |
Ashish Jain | d84fd6a | 2016-07-27 12:33:25 +0530 | [diff] [blame] | 5463 | ret = -EIO; |
Satish Babu Patakokila | 1caa1b7 | 2016-05-24 13:47:08 +0530 | [diff] [blame] | 5464 | goto exit; |
| 5465 | } |
| 5466 | |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 5467 | if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) { |
| 5468 | ret = -EINVAL; |
| 5469 | goto exit; |
| 5470 | } |
| 5471 | |
Satish Babu Patakokila | 5933e97 | 2017-08-24 12:22:08 +0530 | [diff] [blame] | 5472 | if ((out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) && |
| 5473 | !out->is_iec61937_info_available) { |
| 5474 | |
| 5475 | if (!audio_extn_passthru_is_passthrough_stream(out)) { |
| 5476 | out->is_iec61937_info_available = true; |
| 5477 | } else if (audio_extn_passthru_is_enabled()) { |
| 5478 | audio_extn_passthru_update_stream_configuration(adev, out, buffer, bytes); |
Manish Dewangan | 37864bc | 2017-06-09 12:28:37 +0530 | [diff] [blame] | 5479 | out->is_iec61937_info_available = true; |
Manish Dewangan | 671a420 | 2017-08-18 17:30:46 +0530 | [diff] [blame] | 5480 | |
| 5481 | if((out->format == AUDIO_FORMAT_DTS) || |
| 5482 | (out->format == AUDIO_FORMAT_DTS_HD)) { |
| 5483 | ret = audio_extn_passthru_update_dts_stream_configuration(out, |
| 5484 | buffer, bytes); |
| 5485 | if (ret) { |
| 5486 | if (ret != -ENOSYS) { |
| 5487 | out->is_iec61937_info_available = false; |
| 5488 | ALOGD("iec61937 transmission info not yet updated retry"); |
| 5489 | } |
Satish Babu Patakokila | 5933e97 | 2017-08-24 12:22:08 +0530 | [diff] [blame] | 5490 | } else if (!out->standby) { |
Manish Dewangan | 671a420 | 2017-08-18 17:30:46 +0530 | [diff] [blame] | 5491 | /* if stream has started and after that there is |
| 5492 | * stream config change (iec transmission config) |
| 5493 | * then trigger select_device to update backend configuration. |
| 5494 | */ |
| 5495 | out->stream_config_changed = true; |
| 5496 | pthread_mutex_lock(&adev->lock); |
| 5497 | select_devices(adev, out->usecase); |
Satish Babu Patakokila | 5933e97 | 2017-08-24 12:22:08 +0530 | [diff] [blame] | 5498 | if (!audio_extn_passthru_is_supported_backend_edid_cfg(adev, out)) { |
Weiyin Jiang | 29c08a4 | 2019-04-30 17:11:10 +0800 | [diff] [blame] | 5499 | pthread_mutex_unlock(&adev->lock); |
Satish Babu Patakokila | 5933e97 | 2017-08-24 12:22:08 +0530 | [diff] [blame] | 5500 | ret = -EINVAL; |
| 5501 | goto exit; |
| 5502 | } |
Manish Dewangan | 671a420 | 2017-08-18 17:30:46 +0530 | [diff] [blame] | 5503 | pthread_mutex_unlock(&adev->lock); |
| 5504 | out->stream_config_changed = false; |
| 5505 | out->is_iec61937_info_available = true; |
| 5506 | } |
| 5507 | } |
Satish Babu Patakokila | 715b142 | 2017-08-22 14:33:21 +0530 | [diff] [blame] | 5508 | |
Garmond Leung | 317cbf1 | 2017-09-13 16:20:50 -0700 | [diff] [blame] | 5509 | if ((channels < (int)audio_channel_count_from_out_mask(out->channel_mask)) && |
Satish Babu Patakokila | 715b142 | 2017-08-22 14:33:21 +0530 | [diff] [blame] | 5510 | (out->compr_config.codec->compr_passthr == PASSTHROUGH) && |
| 5511 | (out->is_iec61937_info_available == true)) { |
| 5512 | ALOGE("%s: ERROR: Unsupported channel config in passthrough mode", __func__); |
| 5513 | ret = -EINVAL; |
| 5514 | goto exit; |
| 5515 | } |
Manish Dewangan | 37864bc | 2017-06-09 12:28:37 +0530 | [diff] [blame] | 5516 | } |
| 5517 | } |
Chaithanya Krishna Bacharaju | 69d2e4c | 2017-05-26 18:22:46 +0530 | [diff] [blame] | 5518 | |
| 5519 | if ((out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) && |
Florian Pfister | 1a84f31 | 2018-07-19 14:38:18 +0200 | [diff] [blame] | 5520 | (audio_extn_a2dp_source_is_suspended())) { |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 5521 | if (!(out->devices & |
| 5522 | (AUDIO_DEVICE_OUT_SPEAKER | AUDIO_DEVICE_OUT_SPEAKER_SAFE))) { |
Chaithanya Krishna Bacharaju | 69d2e4c | 2017-05-26 18:22:46 +0530 | [diff] [blame] | 5523 | if (!(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) { |
Chaithanya Krishna Bacharaju | 69d2e4c | 2017-05-26 18:22:46 +0530 | [diff] [blame] | 5524 | ret = -EIO; |
| 5525 | goto exit; |
| 5526 | } |
| 5527 | } |
| 5528 | } |
| 5529 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5530 | if (out->standby) { |
Ravi Kumar Alamanda | 59d296d | 2013-05-02 11:25:27 -0700 | [diff] [blame] | 5531 | out->standby = false; |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 5532 | pthread_mutex_lock(&adev->lock); |
Narsinga Rao Chella | 05573b7 | 2013-11-15 15:21:40 -0800 | [diff] [blame] | 5533 | if (out->usecase == USECASE_COMPRESS_VOIP_CALL) |
| 5534 | ret = voice_extn_compress_voip_start_output_stream(out); |
| 5535 | else |
| 5536 | ret = start_output_stream(out); |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 5537 | pthread_mutex_unlock(&adev->lock); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 5538 | /* ToDo: If use case is compress offload should return 0 */ |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5539 | if (ret != 0) { |
Ravi Kumar Alamanda | 59d296d | 2013-05-02 11:25:27 -0700 | [diff] [blame] | 5540 | out->standby = true; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5541 | goto exit; |
| 5542 | } |
Ashish Jain | 1b9b30c | 2017-05-18 20:57:40 +0530 | [diff] [blame] | 5543 | out->started = 1; |
vivek mehta | b72d08d | 2016-04-29 03:16:47 -0700 | [diff] [blame] | 5544 | if (last_known_cal_step != -1) { |
| 5545 | ALOGD("%s: retry previous failed cal level set", __func__); |
| 5546 | audio_hw_send_gain_dep_calibration(last_known_cal_step); |
Preetam Singh Ranawat | f4ae022 | 2017-05-31 17:07:28 +0530 | [diff] [blame] | 5547 | last_known_cal_step = -1; |
vivek mehta | b72d08d | 2016-04-29 03:16:47 -0700 | [diff] [blame] | 5548 | } |
Satish Babu Patakokila | 5933e97 | 2017-08-24 12:22:08 +0530 | [diff] [blame] | 5549 | |
| 5550 | if ((out->is_iec61937_info_available == true) && |
| 5551 | (audio_extn_passthru_is_passthrough_stream(out))&& |
| 5552 | (!audio_extn_passthru_is_supported_backend_edid_cfg(adev, out))) { |
| 5553 | ret = -EINVAL; |
| 5554 | goto exit; |
| 5555 | } |
Surendar Karka | f51b584 | 2018-04-26 11:28:38 +0530 | [diff] [blame] | 5556 | if (out->set_dual_mono) |
| 5557 | audio_extn_send_dual_mono_mixing_coefficients(out); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5558 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5559 | |
Ashish Jain | 81eb2a8 | 2015-05-13 10:52:34 +0530 | [diff] [blame] | 5560 | if (adev->is_channel_status_set == false && (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)){ |
Alexy Joseph | b137994 | 2016-01-29 15:49:38 -0800 | [diff] [blame] | 5561 | audio_utils_set_hdmi_channel_status(out, (void *)buffer, bytes); |
Ashish Jain | 81eb2a8 | 2015-05-13 10:52:34 +0530 | [diff] [blame] | 5562 | adev->is_channel_status_set = true; |
| 5563 | } |
| 5564 | |
Dhanalakshmi Siddani | 20628c8 | 2019-01-27 17:31:09 +0530 | [diff] [blame] | 5565 | if ((adev->use_old_pspd_mix_ctrl == true) && |
| 5566 | (out->pspd_coeff_sent == false)) { |
| 5567 | /* |
| 5568 | * Need to resend pspd coefficients after stream started for |
| 5569 | * older kernel version as it does not save the coefficients |
| 5570 | * and also stream has to be started for coeff to apply. |
| 5571 | */ |
| 5572 | usecase = get_usecase_from_list(adev, out->usecase); |
| 5573 | if (usecase != NULL) { |
Chaithanya Krishna Bacharaju | c9f9971 | 2019-04-16 15:32:52 +0530 | [diff] [blame] | 5574 | audio_extn_set_custom_mtmx_params_v2(adev, usecase, true); |
Dhanalakshmi Siddani | 20628c8 | 2019-01-27 17:31:09 +0530 | [diff] [blame] | 5575 | out->pspd_coeff_sent = true; |
| 5576 | } |
| 5577 | } |
| 5578 | |
Subhash Chandra Bose Naripeddy | 16ff4f8 | 2014-04-01 21:03:10 -0700 | [diff] [blame] | 5579 | if (is_offload_usecase(out->usecase)) { |
Alexy Joseph | 01e54e6 | 2015-03-03 19:01:03 -0800 | [diff] [blame] | 5580 | ALOGVV("copl(%p): writing buffer (%zu bytes) to compress device", out, bytes); |
Haynes Mathew George | 352f27b | 2013-07-26 00:00:15 -0700 | [diff] [blame] | 5581 | if (out->send_new_metadata) { |
Apoorv Raghuvanshi | 44bd917 | 2014-05-28 14:50:07 -0700 | [diff] [blame] | 5582 | ALOGD("copl(%p):send new gapless metadata", out); |
Haynes Mathew George | 352f27b | 2013-07-26 00:00:15 -0700 | [diff] [blame] | 5583 | compress_set_gapless_metadata(out->compr, &out->gapless_mdata); |
| 5584 | out->send_new_metadata = 0; |
Chaithanya Krishna Bacharaju | a70cb6a | 2015-07-24 14:15:05 +0530 | [diff] [blame] | 5585 | if (out->send_next_track_params && out->is_compr_metadata_avail) { |
| 5586 | ALOGD("copl(%p):send next track params in gapless", out); |
Weiyin Jiang | d9b5a4e | 2019-07-18 17:24:21 +0800 | [diff] [blame] | 5587 | compress_set_next_track_param(out->compr, &(out->compr_config.codec->options)); |
Chaithanya Krishna Bacharaju | a70cb6a | 2015-07-24 14:15:05 +0530 | [diff] [blame] | 5588 | out->send_next_track_params = false; |
| 5589 | out->is_compr_metadata_avail = false; |
| 5590 | } |
Haynes Mathew George | 352f27b | 2013-07-26 00:00:15 -0700 | [diff] [blame] | 5591 | } |
Dhananjay Kumar | ac34158 | 2017-02-23 23:42:25 +0530 | [diff] [blame] | 5592 | if (!(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && |
Ashish Jain | 83a6cc2 | 2016-06-28 14:34:17 +0530 | [diff] [blame] | 5593 | (out->convert_buffer) != NULL) { |
Haynes Mathew George | 352f27b | 2013-07-26 00:00:15 -0700 | [diff] [blame] | 5594 | |
Ashish Jain | 83a6cc2 | 2016-06-28 14:34:17 +0530 | [diff] [blame] | 5595 | if ((bytes > out->hal_fragment_size)) { |
Ashish Jain | f1eaa58 | 2016-05-23 20:54:24 +0530 | [diff] [blame] | 5596 | ALOGW("Error written bytes %zu > %d (fragment_size)", |
Ashish Jain | 83a6cc2 | 2016-06-28 14:34:17 +0530 | [diff] [blame] | 5597 | bytes, out->hal_fragment_size); |
Ashish Jain | f1eaa58 | 2016-05-23 20:54:24 +0530 | [diff] [blame] | 5598 | pthread_mutex_unlock(&out->lock); |
Haynes Mathew George | 380745d | 2017-10-04 15:27:45 -0700 | [diff] [blame] | 5599 | ATRACE_END(); |
Ashish Jain | f1eaa58 | 2016-05-23 20:54:24 +0530 | [diff] [blame] | 5600 | return -EINVAL; |
| 5601 | } else { |
Ashish Jain | 83a6cc2 | 2016-06-28 14:34:17 +0530 | [diff] [blame] | 5602 | audio_format_t dst_format = out->hal_op_format; |
| 5603 | audio_format_t src_format = out->hal_ip_format; |
Ashish Jain | f1eaa58 | 2016-05-23 20:54:24 +0530 | [diff] [blame] | 5604 | |
Dieter Luecking | 5d57def | 2018-09-07 14:23:37 +0200 | [diff] [blame] | 5605 | /* prevent division-by-zero */ |
| 5606 | uint32_t bitwidth_src = format_to_bitwidth_table[src_format]; |
| 5607 | uint32_t bitwidth_dst = format_to_bitwidth_table[dst_format]; |
| 5608 | if ((bitwidth_src == 0) || (bitwidth_dst == 0)) { |
| 5609 | ALOGE("%s: Error bitwidth == 0", __func__); |
Ramu Gottipati | 0280968 | 2018-12-19 16:46:12 +0530 | [diff] [blame] | 5610 | pthread_mutex_unlock(&out->lock); |
Dieter Luecking | 5d57def | 2018-09-07 14:23:37 +0200 | [diff] [blame] | 5611 | ATRACE_END(); |
| 5612 | return -EINVAL; |
| 5613 | } |
| 5614 | |
Ashish Jain | f1eaa58 | 2016-05-23 20:54:24 +0530 | [diff] [blame] | 5615 | uint32_t frames = bytes / format_to_bitwidth_table[src_format]; |
| 5616 | uint32_t bytes_to_write = frames * format_to_bitwidth_table[dst_format]; |
| 5617 | |
Ashish Jain | 83a6cc2 | 2016-06-28 14:34:17 +0530 | [diff] [blame] | 5618 | memcpy_by_audio_format(out->convert_buffer, |
Ashish Jain | f1eaa58 | 2016-05-23 20:54:24 +0530 | [diff] [blame] | 5619 | dst_format, |
| 5620 | buffer, |
| 5621 | src_format, |
| 5622 | frames); |
| 5623 | |
Ashish Jain | 83a6cc2 | 2016-06-28 14:34:17 +0530 | [diff] [blame] | 5624 | ret = compress_write(out->compr, out->convert_buffer, |
Ashish Jain | f1eaa58 | 2016-05-23 20:54:24 +0530 | [diff] [blame] | 5625 | bytes_to_write); |
| 5626 | |
| 5627 | /*Convert written bytes in audio flinger format*/ |
| 5628 | if (ret > 0) |
| 5629 | ret = ((ret * format_to_bitwidth_table[out->format]) / |
| 5630 | format_to_bitwidth_table[dst_format]); |
| 5631 | } |
| 5632 | } else |
| 5633 | ret = compress_write(out->compr, buffer, bytes); |
| 5634 | |
Zhou Song | c967282 | 2017-08-16 16:01:39 +0800 | [diff] [blame] | 5635 | if ((ret < 0 || ret == (ssize_t)bytes) && !out->non_blocking) |
| 5636 | update_frames_written(out, bytes); |
| 5637 | |
Dhanalakshmi Siddani | 37ca1d6 | 2014-08-20 12:28:34 +0530 | [diff] [blame] | 5638 | if (ret < 0) |
| 5639 | ret = -errno; |
Weiyin Jiang | cc60dbb | 2018-08-21 13:12:03 +0800 | [diff] [blame] | 5640 | ALOGVV("%s: writing buffer (%zu bytes) to compress device returned %d", __func__, bytes, (int)ret); |
Ashish Jain | b26edfb | 2016-08-25 00:10:11 +0530 | [diff] [blame] | 5641 | /*msg to cb thread only if non blocking write is enabled*/ |
| 5642 | if (ret >= 0 && ret < (ssize_t)bytes && out->non_blocking) { |
Sidipotu Ashok | 5582056 | 2014-02-10 16:16:38 +0530 | [diff] [blame] | 5643 | ALOGD("No space available in compress driver, post msg to cb thread"); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 5644 | send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER); |
Naresh Tanniru | 8065983 | 2014-06-04 18:17:56 +0530 | [diff] [blame] | 5645 | } else if (-ENETRESET == ret) { |
| 5646 | ALOGE("copl %s: received sound card offline state on compress write", __func__); |
Dhananjay Kumar | e6293dd | 2017-05-25 17:25:30 +0530 | [diff] [blame] | 5647 | out->card_status = CARD_STATUS_OFFLINE; |
Naresh Tanniru | 8065983 | 2014-06-04 18:17:56 +0530 | [diff] [blame] | 5648 | pthread_mutex_unlock(&out->lock); |
Dhananjay Kumar | 1248dd8 | 2017-07-28 21:22:16 +0530 | [diff] [blame] | 5649 | out_on_error(&out->stream.common); |
Haynes Mathew George | 380745d | 2017-10-04 15:27:45 -0700 | [diff] [blame] | 5650 | ATRACE_END(); |
Naresh Tanniru | 8065983 | 2014-06-04 18:17:56 +0530 | [diff] [blame] | 5651 | return ret; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 5652 | } |
Ashish Jain | 5106d36 | 2016-05-11 19:23:33 +0530 | [diff] [blame] | 5653 | |
Dhanalakshmi Siddani | a6b76c7 | 2016-09-09 18:10:31 +0530 | [diff] [blame] | 5654 | /* Call compr start only when non-zero bytes of data is there to be rendered */ |
| 5655 | if (!out->playback_started && ret > 0) { |
| 5656 | int status = compress_start(out->compr); |
| 5657 | if (status < 0) { |
| 5658 | ret = status; |
| 5659 | ALOGE("%s: compr start failed with err %d", __func__, errno); |
| 5660 | goto exit; |
| 5661 | } |
Alexy Joseph | 7de344d | 2015-03-30 10:40:03 -0700 | [diff] [blame] | 5662 | audio_extn_dts_eagle_fade(adev, true, out); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 5663 | out->playback_started = 1; |
| 5664 | out->offload_state = OFFLOAD_STATE_PLAYING; |
Jitendra Naruka | 1b6513f | 2014-11-22 19:34:13 -0800 | [diff] [blame] | 5665 | |
| 5666 | audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate, |
| 5667 | popcount(out->channel_mask), |
| 5668 | out->playback_started); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 5669 | } |
| 5670 | pthread_mutex_unlock(&out->lock); |
Haynes Mathew George | 380745d | 2017-10-04 15:27:45 -0700 | [diff] [blame] | 5671 | ATRACE_END(); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 5672 | return ret; |
| 5673 | } else { |
| 5674 | if (out->pcm) { |
Arun Mirpuri | 7da752a | 2018-09-11 18:01:15 -0700 | [diff] [blame] | 5675 | size_t bytes_to_write = bytes; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 5676 | if (out->muted) |
| 5677 | memset((void *)buffer, 0, bytes); |
Arun Mirpuri | 7da752a | 2018-09-11 18:01:15 -0700 | [diff] [blame] | 5678 | ALOGV("%s: frames=%zu, frame_size=%zu, bytes_to_write=%zu", |
| 5679 | __func__, frames, frame_size, bytes_to_write); |
| 5680 | |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 5681 | if (out->usecase == USECASE_INCALL_MUSIC_UPLINK || |
Aniket Kumar Lata | 4f9a2a5 | 2019-05-09 18:44:09 -0700 | [diff] [blame] | 5682 | out->usecase == USECASE_INCALL_MUSIC_UPLINK2 || |
| 5683 | (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP && |
| 5684 | !audio_extn_utils_is_vendor_enhanced_fwk())) { |
Arun Mirpuri | 7da752a | 2018-09-11 18:01:15 -0700 | [diff] [blame] | 5685 | size_t channel_count = audio_channel_count_from_out_mask(out->channel_mask); |
| 5686 | int16_t *src = (int16_t *)buffer; |
| 5687 | int16_t *dst = (int16_t *)buffer; |
| 5688 | |
Yunfei Zhang | a6cd66d | 2019-07-22 16:15:36 +0800 | [diff] [blame] | 5689 | LOG_ALWAYS_FATAL_IF(channel_count > 2 || |
Arun Mirpuri | 7da752a | 2018-09-11 18:01:15 -0700 | [diff] [blame] | 5690 | out->format != AUDIO_FORMAT_PCM_16_BIT, |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 5691 | "out_write called for %s use case with wrong properties", |
| 5692 | use_case_table[out->usecase]); |
Arun Mirpuri | 7da752a | 2018-09-11 18:01:15 -0700 | [diff] [blame] | 5693 | |
| 5694 | /* |
| 5695 | * FIXME: this can be removed once audio flinger mixer supports |
| 5696 | * mono output |
| 5697 | */ |
| 5698 | |
| 5699 | /* |
| 5700 | * Code below goes over each frame in the buffer and adds both |
| 5701 | * L and R samples and then divides by 2 to convert to mono |
| 5702 | */ |
Yunfei Zhang | a6cd66d | 2019-07-22 16:15:36 +0800 | [diff] [blame] | 5703 | if (channel_count == 2) { |
| 5704 | for (size_t i = 0; i < frames ; i++, dst++, src += 2) { |
| 5705 | *dst = (int16_t)(((int32_t)src[0] + (int32_t)src[1]) >> 1); |
| 5706 | } |
| 5707 | bytes_to_write /= 2; |
Arun Mirpuri | 7da752a | 2018-09-11 18:01:15 -0700 | [diff] [blame] | 5708 | } |
Arun Mirpuri | 7da752a | 2018-09-11 18:01:15 -0700 | [diff] [blame] | 5709 | } |
Satya Krishna Pindiproli | f1cd92b | 2016-04-14 19:05:23 +0530 | [diff] [blame] | 5710 | ALOGVV("%s: writing buffer (%zu bytes) to pcm device", __func__, bytes); |
Ravi Kumar Alamanda | 8a0f977 | 2015-06-15 10:35:19 -0700 | [diff] [blame] | 5711 | |
Haynes Mathew George | 5beddd4 | 2016-06-27 18:33:40 -0700 | [diff] [blame] | 5712 | long ns = 0; |
Ravi Kumar Alamanda | 8a0f977 | 2015-06-15 10:35:19 -0700 | [diff] [blame] | 5713 | |
Haynes Mathew George | 5beddd4 | 2016-06-27 18:33:40 -0700 | [diff] [blame] | 5714 | if (out->config.rate) |
| 5715 | ns = pcm_bytes_to_frames(out->pcm, bytes)*1000000000LL/ |
| 5716 | out->config.rate; |
| 5717 | |
Arun Mirpuri | 7da752a | 2018-09-11 18:01:15 -0700 | [diff] [blame] | 5718 | request_out_focus(out, ns); |
Haynes Mathew George | 5beddd4 | 2016-06-27 18:33:40 -0700 | [diff] [blame] | 5719 | bool use_mmap = is_mmap_usecase(out->usecase) || out->realtime; |
| 5720 | |
Haynes Mathew George | 5beddd4 | 2016-06-27 18:33:40 -0700 | [diff] [blame] | 5721 | if (use_mmap) |
Arun Mirpuri | 7da752a | 2018-09-11 18:01:15 -0700 | [diff] [blame] | 5722 | ret = pcm_mmap_write(out->pcm, (void *)buffer, bytes_to_write); |
Haynes Mathew George | 5beddd4 | 2016-06-27 18:33:40 -0700 | [diff] [blame] | 5723 | else if (out->hal_op_format != out->hal_ip_format && |
Ashish Jain | 83a6cc2 | 2016-06-28 14:34:17 +0530 | [diff] [blame] | 5724 | out->convert_buffer != NULL) { |
| 5725 | |
| 5726 | memcpy_by_audio_format(out->convert_buffer, |
| 5727 | out->hal_op_format, |
| 5728 | buffer, |
| 5729 | out->hal_ip_format, |
| 5730 | out->config.period_size * out->config.channels); |
| 5731 | |
| 5732 | ret = pcm_write(out->pcm, out->convert_buffer, |
| 5733 | (out->config.period_size * |
| 5734 | out->config.channels * |
| 5735 | format_to_bitwidth_table[out->hal_op_format])); |
| 5736 | } else { |
Aditya Bavanari | d4db8ee | 2017-05-29 21:08:03 +0530 | [diff] [blame] | 5737 | /* |
| 5738 | * To avoid underrun in DSP when the application is not pumping |
| 5739 | * data at required rate, check for the no. of bytes and ignore |
| 5740 | * pcm_write if it is less than actual buffer size. |
| 5741 | * It is a work around to a change in compress VOIP driver. |
| 5742 | */ |
| 5743 | if ((out->flags & AUDIO_OUTPUT_FLAG_VOIP_RX) && |
| 5744 | bytes < (out->config.period_size * out->config.channels * |
| 5745 | audio_bytes_per_sample(out->format))) { |
| 5746 | size_t voip_buf_size = |
| 5747 | out->config.period_size * out->config.channels * |
| 5748 | audio_bytes_per_sample(out->format); |
| 5749 | ALOGE("%s:VOIP underrun: bytes received %zu, required:%zu\n", |
| 5750 | __func__, bytes, voip_buf_size); |
| 5751 | usleep(((uint64_t)voip_buf_size - bytes) * |
| 5752 | 1000000 / audio_stream_out_frame_size(stream) / |
| 5753 | out_get_sample_rate(&out->stream.common)); |
| 5754 | ret = 0; |
Vignesh Kulothungan | a692727 | 2019-02-20 15:17:07 -0800 | [diff] [blame] | 5755 | } else { |
| 5756 | if (out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) |
| 5757 | ret = split_and_write_audio_haptic_data(out, buffer, bytes); |
| 5758 | else |
| 5759 | ret = pcm_write(out->pcm, (void *)buffer, bytes_to_write); |
| 5760 | } |
Ashish Jain | 83a6cc2 | 2016-06-28 14:34:17 +0530 | [diff] [blame] | 5761 | } |
Ravi Kumar Alamanda | 8a0f977 | 2015-06-15 10:35:19 -0700 | [diff] [blame] | 5762 | |
Haynes Mathew George | 5beddd4 | 2016-06-27 18:33:40 -0700 | [diff] [blame] | 5763 | release_out_focus(out); |
| 5764 | |
Dhanalakshmi Siddani | 37ca1d6 | 2014-08-20 12:28:34 +0530 | [diff] [blame] | 5765 | if (ret < 0) |
| 5766 | ret = -errno; |
Zhou Song | c967282 | 2017-08-16 16:01:39 +0800 | [diff] [blame] | 5767 | else if (ret > 0) |
Ashish Jain | 83a6cc2 | 2016-06-28 14:34:17 +0530 | [diff] [blame] | 5768 | ret = -EINVAL; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 5769 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5770 | } |
| 5771 | |
| 5772 | exit: |
Zhou Song | c967282 | 2017-08-16 16:01:39 +0800 | [diff] [blame] | 5773 | update_frames_written(out, bytes); |
Naresh Tanniru | 4c63039 | 2014-05-12 01:05:52 +0530 | [diff] [blame] | 5774 | if (-ENETRESET == ret) { |
Dhananjay Kumar | e6293dd | 2017-05-25 17:25:30 +0530 | [diff] [blame] | 5775 | out->card_status = CARD_STATUS_OFFLINE; |
Naresh Tanniru | 4c63039 | 2014-05-12 01:05:52 +0530 | [diff] [blame] | 5776 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5777 | pthread_mutex_unlock(&out->lock); |
| 5778 | |
| 5779 | if (ret != 0) { |
Ravi Kumar Alamanda | b199506 | 2013-03-21 23:18:20 -0700 | [diff] [blame] | 5780 | if (out->pcm) |
Alexy Joseph | b137994 | 2016-01-29 15:49:38 -0800 | [diff] [blame] | 5781 | ALOGE("%s: error %d, %s", __func__, (int)ret, pcm_get_error(out->pcm)); |
Venkata Narendra Kumar Gutta | bc9c9ca | 2014-06-25 20:38:03 +0530 | [diff] [blame] | 5782 | if (out->usecase == USECASE_COMPRESS_VOIP_CALL) { |
Venkata Narendra Kumar Gutta | 9181214 | 2014-08-11 18:20:49 +0530 | [diff] [blame] | 5783 | pthread_mutex_lock(&adev->lock); |
Venkata Narendra Kumar Gutta | bc9c9ca | 2014-06-25 20:38:03 +0530 | [diff] [blame] | 5784 | voice_extn_compress_voip_close_output_stream(&out->stream.common); |
Ashish Jain | 1b9b30c | 2017-05-18 20:57:40 +0530 | [diff] [blame] | 5785 | out->started = 0; |
Venkata Narendra Kumar Gutta | 9181214 | 2014-08-11 18:20:49 +0530 | [diff] [blame] | 5786 | pthread_mutex_unlock(&adev->lock); |
Venkata Narendra Kumar Gutta | bc9c9ca | 2014-06-25 20:38:03 +0530 | [diff] [blame] | 5787 | out->standby = true; |
| 5788 | } |
Dhananjay Kumar | e6293dd | 2017-05-25 17:25:30 +0530 | [diff] [blame] | 5789 | out_on_error(&out->stream.common); |
Dieter Luecking | 5d57def | 2018-09-07 14:23:37 +0200 | [diff] [blame] | 5790 | if (!(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) { |
| 5791 | /* prevent division-by-zero */ |
| 5792 | uint32_t stream_size = audio_stream_out_frame_size(stream); |
| 5793 | uint32_t srate = out_get_sample_rate(&out->stream.common); |
Vidyakumar Athota | a9d3a5f | 2017-08-09 12:13:05 -0700 | [diff] [blame] | 5794 | |
Dieter Luecking | 5d57def | 2018-09-07 14:23:37 +0200 | [diff] [blame] | 5795 | if ((stream_size == 0) || (srate == 0)) { |
| 5796 | ALOGE("%s: stream_size= %d, srate = %d", __func__, stream_size, srate); |
| 5797 | ATRACE_END(); |
| 5798 | return -EINVAL; |
| 5799 | } |
| 5800 | usleep((uint64_t)bytes * 1000000 / stream_size / srate); |
| 5801 | } |
Vidyakumar Athota | a9d3a5f | 2017-08-09 12:13:05 -0700 | [diff] [blame] | 5802 | if (audio_extn_passthru_is_passthrough_stream(out)) { |
Rajshekar Eashwarappa | 8883452 | 2018-04-02 17:20:15 +0530 | [diff] [blame] | 5803 | //ALOGE("%s: write error, ret = %zd", __func__, ret); |
Haynes Mathew George | 380745d | 2017-10-04 15:27:45 -0700 | [diff] [blame] | 5804 | ATRACE_END(); |
Vidyakumar Athota | a9d3a5f | 2017-08-09 12:13:05 -0700 | [diff] [blame] | 5805 | return ret; |
| 5806 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5807 | } |
Haynes Mathew George | 380745d | 2017-10-04 15:27:45 -0700 | [diff] [blame] | 5808 | ATRACE_END(); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5809 | return bytes; |
| 5810 | } |
| 5811 | |
| 5812 | static int out_get_render_position(const struct audio_stream_out *stream, |
| 5813 | uint32_t *dsp_frames) |
| 5814 | { |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 5815 | struct stream_out *out = (struct stream_out *)stream; |
Zhou Song | 32a556e | 2015-05-05 10:46:56 +0800 | [diff] [blame] | 5816 | |
| 5817 | if (dsp_frames == NULL) |
| 5818 | return -EINVAL; |
| 5819 | |
| 5820 | *dsp_frames = 0; |
| 5821 | if (is_offload_usecase(out->usecase)) { |
Mingming Yin | 9e348b5 | 2014-11-19 16:18:55 -0800 | [diff] [blame] | 5822 | ssize_t ret = 0; |
Ashish Jain | 5106d36 | 2016-05-11 19:23:33 +0530 | [diff] [blame] | 5823 | |
| 5824 | /* Below piece of code is not guarded against any lock beacuse audioFliner serializes |
| 5825 | * this operation and adev_close_output_stream(where out gets reset). |
| 5826 | */ |
Dhananjay Kumar | ac34158 | 2017-02-23 23:42:25 +0530 | [diff] [blame] | 5827 | if (!out->non_blocking && !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) { |
Zhou Song | 48453a0 | 2018-01-10 17:50:59 +0800 | [diff] [blame] | 5828 | *dsp_frames = get_actual_pcm_frames_rendered(out, NULL); |
Ashish Jain | 5106d36 | 2016-05-11 19:23:33 +0530 | [diff] [blame] | 5829 | ALOGVV("dsp_frames %d sampleRate %d",(int)*dsp_frames,out->sample_rate); |
Haynes Mathew George | b0f5dc3 | 2017-10-06 18:35:12 -0700 | [diff] [blame] | 5830 | adjust_frames_for_device_delay(out, dsp_frames); |
Ashish Jain | 5106d36 | 2016-05-11 19:23:33 +0530 | [diff] [blame] | 5831 | return 0; |
| 5832 | } |
| 5833 | |
Shiv Maliyappanahalli | 736d4ce | 2015-09-28 15:23:06 -0700 | [diff] [blame] | 5834 | lock_output_stream(out); |
Ashish Jain | 5106d36 | 2016-05-11 19:23:33 +0530 | [diff] [blame] | 5835 | if (out->compr != NULL && out->non_blocking) { |
Naresh Tanniru | 8065983 | 2014-06-04 18:17:56 +0530 | [diff] [blame] | 5836 | ret = compress_get_tstamp(out->compr, (unsigned long *)dsp_frames, |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 5837 | &out->sample_rate); |
Dhanalakshmi Siddani | 37ca1d6 | 2014-08-20 12:28:34 +0530 | [diff] [blame] | 5838 | if (ret < 0) |
| 5839 | ret = -errno; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 5840 | ALOGVV("%s rendered frames %d sample_rate %d", |
Ashish Jain | 5106d36 | 2016-05-11 19:23:33 +0530 | [diff] [blame] | 5841 | __func__, *dsp_frames, out->sample_rate); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 5842 | } |
Naresh Tanniru | 8065983 | 2014-06-04 18:17:56 +0530 | [diff] [blame] | 5843 | if (-ENETRESET == ret) { |
| 5844 | ALOGE(" ERROR: sound card not active Unable to get time stamp from compress driver"); |
Dhananjay Kumar | e6293dd | 2017-05-25 17:25:30 +0530 | [diff] [blame] | 5845 | out->card_status = CARD_STATUS_OFFLINE; |
| 5846 | ret = -EINVAL; |
Naresh Tanniru | 8065983 | 2014-06-04 18:17:56 +0530 | [diff] [blame] | 5847 | } else if(ret < 0) { |
| 5848 | ALOGE(" ERROR: Unable to get time stamp from compress driver"); |
Dhananjay Kumar | e6293dd | 2017-05-25 17:25:30 +0530 | [diff] [blame] | 5849 | ret = -EINVAL; |
| 5850 | } else if (out->card_status == CARD_STATUS_OFFLINE) { |
Preetam Singh Ranawat | 2d0e463 | 2015-02-02 12:40:59 +0530 | [diff] [blame] | 5851 | /* |
| 5852 | * Handle corner case where compress session is closed during SSR |
| 5853 | * and timestamp is queried |
| 5854 | */ |
| 5855 | ALOGE(" ERROR: sound card not active, return error"); |
Dhananjay Kumar | e6293dd | 2017-05-25 17:25:30 +0530 | [diff] [blame] | 5856 | ret = -EINVAL; |
Manisha Agarwal | 7b3e377 | 2019-02-20 14:33:45 +0530 | [diff] [blame] | 5857 | } else if (out->prev_card_status_offline) { |
| 5858 | ALOGE("ERROR: previously sound card was offline,return error"); |
| 5859 | ret = -EINVAL; |
Naresh Tanniru | 8065983 | 2014-06-04 18:17:56 +0530 | [diff] [blame] | 5860 | } else { |
Dhananjay Kumar | e6293dd | 2017-05-25 17:25:30 +0530 | [diff] [blame] | 5861 | ret = 0; |
Haynes Mathew George | b0f5dc3 | 2017-10-06 18:35:12 -0700 | [diff] [blame] | 5862 | adjust_frames_for_device_delay(out, dsp_frames); |
Naresh Tanniru | 8065983 | 2014-06-04 18:17:56 +0530 | [diff] [blame] | 5863 | } |
Dhananjay Kumar | e6293dd | 2017-05-25 17:25:30 +0530 | [diff] [blame] | 5864 | pthread_mutex_unlock(&out->lock); |
| 5865 | return ret; |
Zhou Song | 32a556e | 2015-05-05 10:46:56 +0800 | [diff] [blame] | 5866 | } else if (audio_is_linear_pcm(out->format)) { |
| 5867 | *dsp_frames = out->written; |
Haynes Mathew George | b0f5dc3 | 2017-10-06 18:35:12 -0700 | [diff] [blame] | 5868 | adjust_frames_for_device_delay(out, dsp_frames); |
Zhou Song | 32a556e | 2015-05-05 10:46:56 +0800 | [diff] [blame] | 5869 | return 0; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 5870 | } else |
| 5871 | return -EINVAL; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5872 | } |
| 5873 | |
Ravi Kumar Alamanda | bdf1416 | 2014-09-05 16:14:17 -0700 | [diff] [blame] | 5874 | static int out_add_audio_effect(const struct audio_stream *stream __unused, |
| 5875 | effect_handle_t effect __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5876 | { |
| 5877 | return 0; |
| 5878 | } |
| 5879 | |
Ravi Kumar Alamanda | bdf1416 | 2014-09-05 16:14:17 -0700 | [diff] [blame] | 5880 | static int out_remove_audio_effect(const struct audio_stream *stream __unused, |
| 5881 | effect_handle_t effect __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5882 | { |
| 5883 | return 0; |
| 5884 | } |
| 5885 | |
Ravi Kumar Alamanda | bdf1416 | 2014-09-05 16:14:17 -0700 | [diff] [blame] | 5886 | static int out_get_next_write_timestamp(const struct audio_stream_out *stream __unused, |
| 5887 | int64_t *timestamp __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5888 | { |
Satya Krishna Pindiproli | b665554 | 2017-07-03 19:38:19 +0530 | [diff] [blame] | 5889 | return -ENOSYS; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5890 | } |
| 5891 | |
Glenn Kasten | 2ccd7ba | 2013-09-10 09:04:31 -0700 | [diff] [blame] | 5892 | static int out_get_presentation_position(const struct audio_stream_out *stream, |
| 5893 | uint64_t *frames, struct timespec *timestamp) |
| 5894 | { |
| 5895 | struct stream_out *out = (struct stream_out *)stream; |
pavanc | e65c2fe | 2017-10-18 17:52:01 +0530 | [diff] [blame] | 5896 | int ret = -ENODATA; |
Eric Laurent | 949a089 | 2013-09-20 09:20:13 -0700 | [diff] [blame] | 5897 | unsigned long dsp_frames; |
Glenn Kasten | 2ccd7ba | 2013-09-10 09:04:31 -0700 | [diff] [blame] | 5898 | |
Ashish Jain | 5106d36 | 2016-05-11 19:23:33 +0530 | [diff] [blame] | 5899 | /* below piece of code is not guarded against any lock because audioFliner serializes |
| 5900 | * this operation and adev_close_output_stream( where out gets reset). |
| 5901 | */ |
| 5902 | if (is_offload_usecase(out->usecase) && !out->non_blocking && |
Dhananjay Kumar | ac34158 | 2017-02-23 23:42:25 +0530 | [diff] [blame] | 5903 | !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) { |
Zhou Song | 48453a0 | 2018-01-10 17:50:59 +0800 | [diff] [blame] | 5904 | *frames = get_actual_pcm_frames_rendered(out, timestamp); |
Ashish Jain | 5106d36 | 2016-05-11 19:23:33 +0530 | [diff] [blame] | 5905 | ALOGVV("frames %lld playedat %lld",(long long int)*frames, |
| 5906 | timestamp->tv_sec * 1000000LL + timestamp->tv_nsec / 1000); |
| 5907 | return 0; |
| 5908 | } |
| 5909 | |
Shiv Maliyappanahalli | 736d4ce | 2015-09-28 15:23:06 -0700 | [diff] [blame] | 5910 | lock_output_stream(out); |
Glenn Kasten | 2ccd7ba | 2013-09-10 09:04:31 -0700 | [diff] [blame] | 5911 | |
Ashish Jain | 5106d36 | 2016-05-11 19:23:33 +0530 | [diff] [blame] | 5912 | if (is_offload_usecase(out->usecase) && out->compr != NULL && out->non_blocking) { |
| 5913 | ret = compress_get_tstamp(out->compr, &dsp_frames, |
| 5914 | &out->sample_rate); |
yidongh | 0515e04 | 2017-07-06 15:00:34 +0800 | [diff] [blame] | 5915 | // Adjustment accounts for A2dp encoder latency with offload usecases |
| 5916 | // Note: Encoder latency is returned in ms. |
| 5917 | if (AUDIO_DEVICE_OUT_ALL_A2DP & out->devices) { |
| 5918 | unsigned long offset = |
| 5919 | (audio_extn_a2dp_get_encoder_latency() * out->sample_rate / 1000); |
| 5920 | dsp_frames = (dsp_frames > offset) ? (dsp_frames - offset) : 0; |
| 5921 | } |
Ashish Jain | 5106d36 | 2016-05-11 19:23:33 +0530 | [diff] [blame] | 5922 | ALOGVV("%s rendered frames %ld sample_rate %d", |
| 5923 | __func__, dsp_frames, out->sample_rate); |
| 5924 | *frames = dsp_frames; |
| 5925 | if (ret < 0) |
| 5926 | ret = -errno; |
| 5927 | if (-ENETRESET == ret) { |
| 5928 | ALOGE(" ERROR: sound card not active Unable to get time stamp from compress driver"); |
Dhananjay Kumar | e6293dd | 2017-05-25 17:25:30 +0530 | [diff] [blame] | 5929 | out->card_status = CARD_STATUS_OFFLINE; |
Ashish Jain | 5106d36 | 2016-05-11 19:23:33 +0530 | [diff] [blame] | 5930 | ret = -EINVAL; |
| 5931 | } else |
| 5932 | ret = 0; |
| 5933 | /* this is the best we can do */ |
| 5934 | clock_gettime(CLOCK_MONOTONIC, timestamp); |
Eric Laurent | 949a089 | 2013-09-20 09:20:13 -0700 | [diff] [blame] | 5935 | } else { |
| 5936 | if (out->pcm) { |
Weiyin Jiang | d463376 | 2018-03-16 12:05:03 +0800 | [diff] [blame] | 5937 | unsigned int avail; |
| 5938 | if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) { |
| 5939 | size_t kernel_buffer_size = out->config.period_size * out->config.period_count; |
George Gao | 62ebc72 | 2019-07-29 16:29:44 -0700 | [diff] [blame] | 5940 | |
| 5941 | uint64_t signed_frames = 0; |
Weiyin Jiang | 0d25bcf | 2019-08-30 15:27:23 +0800 | [diff] [blame] | 5942 | uint64_t frames_temp = 0; |
George Gao | 62ebc72 | 2019-07-29 16:29:44 -0700 | [diff] [blame] | 5943 | |
Weiyin Jiang | 0d25bcf | 2019-08-30 15:27:23 +0800 | [diff] [blame] | 5944 | frames_temp = (kernel_buffer_size > avail) ? (kernel_buffer_size - avail) : 0; |
| 5945 | if (out->written >= frames_temp) |
| 5946 | signed_frames = out->written - frames_temp; |
George Gao | 62ebc72 | 2019-07-29 16:29:44 -0700 | [diff] [blame] | 5947 | |
Weiyin Jiang | d463376 | 2018-03-16 12:05:03 +0800 | [diff] [blame] | 5948 | // This adjustment accounts for buffering after app processor. |
| 5949 | // It is based on estimated DSP latency per use case, rather than exact. |
Weiyin Jiang | 0d25bcf | 2019-08-30 15:27:23 +0800 | [diff] [blame] | 5950 | frames_temp = platform_render_latency(out->usecase) * out->sample_rate / 1000000LL; |
| 5951 | if (signed_frames >= frames_temp) |
| 5952 | signed_frames -= frames_temp; |
Aniket Kumar Lata | ff61315 | 2017-07-18 18:19:21 -0700 | [diff] [blame] | 5953 | |
Weiyin Jiang | d463376 | 2018-03-16 12:05:03 +0800 | [diff] [blame] | 5954 | // Adjustment accounts for A2dp encoder latency with non offload usecases |
| 5955 | // Note: Encoder latency is returned in ms, while platform_render_latency in us. |
| 5956 | if (AUDIO_DEVICE_OUT_ALL_A2DP & out->devices) { |
Weiyin Jiang | 0d25bcf | 2019-08-30 15:27:23 +0800 | [diff] [blame] | 5957 | frames_temp = audio_extn_a2dp_get_encoder_latency() * out->sample_rate / 1000; |
| 5958 | if (signed_frames >= frames_temp) |
| 5959 | signed_frames -= frames_temp; |
Weiyin Jiang | d463376 | 2018-03-16 12:05:03 +0800 | [diff] [blame] | 5960 | } |
| 5961 | |
| 5962 | // It would be unusual for this value to be negative, but check just in case ... |
George Gao | 62ebc72 | 2019-07-29 16:29:44 -0700 | [diff] [blame] | 5963 | *frames = signed_frames; |
| 5964 | ret = 0; |
Glenn Kasten | 2ccd7ba | 2013-09-10 09:04:31 -0700 | [diff] [blame] | 5965 | } |
Dhananjay Kumar | e6293dd | 2017-05-25 17:25:30 +0530 | [diff] [blame] | 5966 | } else if (out->card_status == CARD_STATUS_OFFLINE) { |
Ashish Jain | bbce432 | 2016-02-16 13:25:27 +0530 | [diff] [blame] | 5967 | *frames = out->written; |
| 5968 | clock_gettime(CLOCK_MONOTONIC, timestamp); |
Manisha Agarwal | 7b3e377 | 2019-02-20 14:33:45 +0530 | [diff] [blame] | 5969 | if (is_offload_usecase(out->usecase)) |
| 5970 | ret = -EINVAL; |
| 5971 | else |
| 5972 | ret = 0; |
Glenn Kasten | 2ccd7ba | 2013-09-10 09:04:31 -0700 | [diff] [blame] | 5973 | } |
| 5974 | } |
Glenn Kasten | 2ccd7ba | 2013-09-10 09:04:31 -0700 | [diff] [blame] | 5975 | pthread_mutex_unlock(&out->lock); |
Glenn Kasten | 2ccd7ba | 2013-09-10 09:04:31 -0700 | [diff] [blame] | 5976 | return ret; |
| 5977 | } |
| 5978 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 5979 | static int out_set_callback(struct audio_stream_out *stream, |
| 5980 | stream_callback_t callback, void *cookie) |
| 5981 | { |
| 5982 | struct stream_out *out = (struct stream_out *)stream; |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 5983 | int ret; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 5984 | |
| 5985 | ALOGV("%s", __func__); |
Shiv Maliyappanahalli | 736d4ce | 2015-09-28 15:23:06 -0700 | [diff] [blame] | 5986 | lock_output_stream(out); |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 5987 | out->client_callback = callback; |
| 5988 | out->client_cookie = cookie; |
| 5989 | if (out->adsp_hdlr_stream_handle) { |
| 5990 | ret = audio_extn_adsp_hdlr_stream_set_callback( |
| 5991 | out->adsp_hdlr_stream_handle, |
| 5992 | callback, |
| 5993 | cookie); |
| 5994 | if (ret) |
| 5995 | ALOGW("%s:adsp hdlr callback registration failed %d", |
| 5996 | __func__, ret); |
| 5997 | } |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 5998 | pthread_mutex_unlock(&out->lock); |
| 5999 | return 0; |
| 6000 | } |
| 6001 | |
| 6002 | static int out_pause(struct audio_stream_out* stream) |
| 6003 | { |
| 6004 | struct stream_out *out = (struct stream_out *)stream; |
| 6005 | int status = -ENOSYS; |
| 6006 | ALOGV("%s", __func__); |
Subhash Chandra Bose Naripeddy | 16ff4f8 | 2014-04-01 21:03:10 -0700 | [diff] [blame] | 6007 | if (is_offload_usecase(out->usecase)) { |
Apoorv Raghuvanshi | 44bd917 | 2014-05-28 14:50:07 -0700 | [diff] [blame] | 6008 | ALOGD("copl(%p):pause compress driver", out); |
Shiv Maliyappanahalli | 736d4ce | 2015-09-28 15:23:06 -0700 | [diff] [blame] | 6009 | lock_output_stream(out); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 6010 | if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) { |
Dhananjay Kumar | e6293dd | 2017-05-25 17:25:30 +0530 | [diff] [blame] | 6011 | if (out->card_status != CARD_STATUS_OFFLINE) |
Naresh Tanniru | 8065983 | 2014-06-04 18:17:56 +0530 | [diff] [blame] | 6012 | status = compress_pause(out->compr); |
| 6013 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 6014 | out->offload_state = OFFLOAD_STATE_PAUSED; |
Jitendra Naruka | 1b6513f | 2014-11-22 19:34:13 -0800 | [diff] [blame] | 6015 | |
Mingming Yin | 2185465 | 2016-04-13 11:54:02 -0700 | [diff] [blame] | 6016 | if (audio_extn_passthru_is_active()) { |
| 6017 | ALOGV("offload use case, pause passthru"); |
| 6018 | audio_extn_passthru_on_pause(out); |
| 6019 | } |
| 6020 | |
Dhanalakshmi Siddani | 79415e7 | 2015-03-23 11:54:47 +0530 | [diff] [blame] | 6021 | audio_extn_dts_eagle_fade(adev, false, out); |
Jitendra Naruka | 1b6513f | 2014-11-22 19:34:13 -0800 | [diff] [blame] | 6022 | audio_extn_dts_notify_playback_state(out->usecase, 0, |
| 6023 | out->sample_rate, popcount(out->channel_mask), |
| 6024 | 0); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 6025 | } |
| 6026 | pthread_mutex_unlock(&out->lock); |
| 6027 | } |
| 6028 | return status; |
| 6029 | } |
| 6030 | |
| 6031 | static int out_resume(struct audio_stream_out* stream) |
| 6032 | { |
| 6033 | struct stream_out *out = (struct stream_out *)stream; |
| 6034 | int status = -ENOSYS; |
| 6035 | ALOGV("%s", __func__); |
Subhash Chandra Bose Naripeddy | 16ff4f8 | 2014-04-01 21:03:10 -0700 | [diff] [blame] | 6036 | if (is_offload_usecase(out->usecase)) { |
Apoorv Raghuvanshi | 44bd917 | 2014-05-28 14:50:07 -0700 | [diff] [blame] | 6037 | ALOGD("copl(%p):resume compress driver", out); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 6038 | status = 0; |
Shiv Maliyappanahalli | 736d4ce | 2015-09-28 15:23:06 -0700 | [diff] [blame] | 6039 | lock_output_stream(out); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 6040 | if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) { |
Dhananjay Kumar | e6293dd | 2017-05-25 17:25:30 +0530 | [diff] [blame] | 6041 | if (out->card_status != CARD_STATUS_OFFLINE) { |
Naresh Tanniru | 8065983 | 2014-06-04 18:17:56 +0530 | [diff] [blame] | 6042 | status = compress_resume(out->compr); |
Mingming Yin | 2185465 | 2016-04-13 11:54:02 -0700 | [diff] [blame] | 6043 | } |
| 6044 | if (!status) { |
| 6045 | out->offload_state = OFFLOAD_STATE_PLAYING; |
| 6046 | } |
Dhanalakshmi Siddani | 79415e7 | 2015-03-23 11:54:47 +0530 | [diff] [blame] | 6047 | audio_extn_dts_eagle_fade(adev, true, out); |
Jitendra Naruka | 1b6513f | 2014-11-22 19:34:13 -0800 | [diff] [blame] | 6048 | audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate, |
| 6049 | popcount(out->channel_mask), 1); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 6050 | } |
| 6051 | pthread_mutex_unlock(&out->lock); |
| 6052 | } |
| 6053 | return status; |
| 6054 | } |
| 6055 | |
| 6056 | static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type ) |
| 6057 | { |
| 6058 | struct stream_out *out = (struct stream_out *)stream; |
| 6059 | int status = -ENOSYS; |
| 6060 | ALOGV("%s", __func__); |
Subhash Chandra Bose Naripeddy | 16ff4f8 | 2014-04-01 21:03:10 -0700 | [diff] [blame] | 6061 | if (is_offload_usecase(out->usecase)) { |
Shiv Maliyappanahalli | 736d4ce | 2015-09-28 15:23:06 -0700 | [diff] [blame] | 6062 | lock_output_stream(out); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 6063 | if (type == AUDIO_DRAIN_EARLY_NOTIFY) |
| 6064 | status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN); |
| 6065 | else |
| 6066 | status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN); |
| 6067 | pthread_mutex_unlock(&out->lock); |
| 6068 | } |
| 6069 | return status; |
| 6070 | } |
| 6071 | |
| 6072 | static int out_flush(struct audio_stream_out* stream) |
| 6073 | { |
| 6074 | struct stream_out *out = (struct stream_out *)stream; |
| 6075 | ALOGV("%s", __func__); |
Subhash Chandra Bose Naripeddy | 16ff4f8 | 2014-04-01 21:03:10 -0700 | [diff] [blame] | 6076 | if (is_offload_usecase(out->usecase)) { |
Apoorv Raghuvanshi | 44bd917 | 2014-05-28 14:50:07 -0700 | [diff] [blame] | 6077 | ALOGD("copl(%p):calling compress flush", out); |
Shiv Maliyappanahalli | 736d4ce | 2015-09-28 15:23:06 -0700 | [diff] [blame] | 6078 | lock_output_stream(out); |
Haynes Mathew George | afe54d8 | 2016-09-21 14:39:19 -0700 | [diff] [blame] | 6079 | if (out->offload_state == OFFLOAD_STATE_PAUSED) { |
| 6080 | stop_compressed_output_l(out); |
Haynes Mathew George | afe54d8 | 2016-09-21 14:39:19 -0700 | [diff] [blame] | 6081 | } else { |
| 6082 | ALOGW("%s called in invalid state %d", __func__, out->offload_state); |
| 6083 | } |
Weiyin Jiang | 547e415 | 2017-09-14 17:24:18 +0800 | [diff] [blame] | 6084 | out->written = 0; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 6085 | pthread_mutex_unlock(&out->lock); |
Apoorv Raghuvanshi | 44bd917 | 2014-05-28 14:50:07 -0700 | [diff] [blame] | 6086 | ALOGD("copl(%p):out of compress flush", out); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 6087 | return 0; |
| 6088 | } |
| 6089 | return -ENOSYS; |
| 6090 | } |
| 6091 | |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 6092 | static int out_stop(const struct audio_stream_out* stream) |
| 6093 | { |
| 6094 | struct stream_out *out = (struct stream_out *)stream; |
| 6095 | struct audio_device *adev = out->dev; |
| 6096 | int ret = -ENOSYS; |
| 6097 | |
| 6098 | ALOGV("%s", __func__); |
| 6099 | pthread_mutex_lock(&adev->lock); |
| 6100 | if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP && !out->standby && |
| 6101 | out->playback_started && out->pcm != NULL) { |
| 6102 | pcm_stop(out->pcm); |
| 6103 | ret = stop_output_stream(out); |
| 6104 | out->playback_started = false; |
| 6105 | } |
| 6106 | pthread_mutex_unlock(&adev->lock); |
| 6107 | return ret; |
| 6108 | } |
| 6109 | |
| 6110 | static int out_start(const struct audio_stream_out* stream) |
| 6111 | { |
| 6112 | struct stream_out *out = (struct stream_out *)stream; |
| 6113 | struct audio_device *adev = out->dev; |
| 6114 | int ret = -ENOSYS; |
| 6115 | |
| 6116 | ALOGV("%s", __func__); |
| 6117 | pthread_mutex_lock(&adev->lock); |
| 6118 | if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP && !out->standby && |
| 6119 | !out->playback_started && out->pcm != NULL) { |
| 6120 | ret = start_output_stream(out); |
| 6121 | if (ret == 0) { |
| 6122 | out->playback_started = true; |
| 6123 | } |
| 6124 | } |
| 6125 | pthread_mutex_unlock(&adev->lock); |
| 6126 | return ret; |
| 6127 | } |
| 6128 | |
| 6129 | /* |
| 6130 | * Modify config->period_count based on min_size_frames |
| 6131 | */ |
| 6132 | static void adjust_mmap_period_count(struct pcm_config *config, int32_t min_size_frames) |
| 6133 | { |
| 6134 | int periodCountRequested = (min_size_frames + config->period_size - 1) |
| 6135 | / config->period_size; |
| 6136 | int periodCount = MMAP_PERIOD_COUNT_MIN; |
| 6137 | |
| 6138 | ALOGV("%s original config.period_size = %d config.period_count = %d", |
| 6139 | __func__, config->period_size, config->period_count); |
| 6140 | |
| 6141 | while (periodCount < periodCountRequested && (periodCount * 2) < MMAP_PERIOD_COUNT_MAX) { |
| 6142 | periodCount *= 2; |
| 6143 | } |
| 6144 | config->period_count = periodCount; |
| 6145 | |
| 6146 | ALOGV("%s requested config.period_count = %d", __func__, config->period_count); |
| 6147 | } |
| 6148 | |
Phil Burk | fe17efd | 2019-03-25 10:23:35 -0700 | [diff] [blame] | 6149 | // Read offset for the positional timestamp from a persistent vendor property. |
| 6150 | // This is to workaround apparent inaccuracies in the timing information that |
| 6151 | // is used by the AAudio timing model. The inaccuracies can cause glitches. |
| 6152 | static int64_t get_mmap_out_time_offset() { |
| 6153 | const int32_t kDefaultOffsetMicros = 0; |
| 6154 | int32_t mmap_time_offset_micros = property_get_int32( |
Weiyin Jiang | bd68b4d | 2019-05-17 16:29:50 +0800 | [diff] [blame] | 6155 | "persist.vendor.audio.out_mmap_delay_micros", kDefaultOffsetMicros); |
Phil Burk | fe17efd | 2019-03-25 10:23:35 -0700 | [diff] [blame] | 6156 | ALOGI("mmap_time_offset_micros = %d for output", mmap_time_offset_micros); |
| 6157 | return mmap_time_offset_micros * (int64_t)1000; |
| 6158 | } |
| 6159 | |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 6160 | static int out_create_mmap_buffer(const struct audio_stream_out *stream, |
| 6161 | int32_t min_size_frames, |
| 6162 | struct audio_mmap_buffer_info *info) |
| 6163 | { |
| 6164 | struct stream_out *out = (struct stream_out *)stream; |
| 6165 | struct audio_device *adev = out->dev; |
| 6166 | int ret = 0; |
Aalique Grahame | 1f12310 | 2017-10-12 10:38:32 -0700 | [diff] [blame] | 6167 | unsigned int offset1 = 0; |
| 6168 | unsigned int frames1 = 0; |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 6169 | const char *step = ""; |
Haynes Mathew George | ef51488 | 2017-05-01 17:46:23 -0700 | [diff] [blame] | 6170 | uint32_t mmap_size; |
Arun Mirpuri | 5d17087 | 2019-03-26 13:21:31 -0700 | [diff] [blame] | 6171 | uint32_t buffer_size; |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 6172 | |
Arun Mirpuri | 5d17087 | 2019-03-26 13:21:31 -0700 | [diff] [blame] | 6173 | ALOGD("%s", __func__); |
Sharad Sangle | c6f3255 | 2018-05-04 16:15:38 +0530 | [diff] [blame] | 6174 | lock_output_stream(out); |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 6175 | pthread_mutex_lock(&adev->lock); |
| 6176 | |
Sharad Sangle | c6f3255 | 2018-05-04 16:15:38 +0530 | [diff] [blame] | 6177 | if (CARD_STATUS_OFFLINE == out->card_status || |
| 6178 | CARD_STATUS_OFFLINE == adev->card_status) { |
| 6179 | ALOGW("out->card_status or adev->card_status offline, try again"); |
| 6180 | ret = -EIO; |
| 6181 | goto exit; |
| 6182 | } |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 6183 | if (info == NULL || min_size_frames == 0) { |
| 6184 | ALOGE("%s: info = %p, min_size_frames = %d", __func__, info, min_size_frames); |
| 6185 | ret = -EINVAL; |
| 6186 | goto exit; |
| 6187 | } |
| 6188 | if (out->usecase != USECASE_AUDIO_PLAYBACK_MMAP || !out->standby) { |
| 6189 | ALOGE("%s: usecase = %d, standby = %d", __func__, out->usecase, out->standby); |
| 6190 | ret = -ENOSYS; |
| 6191 | goto exit; |
| 6192 | } |
| 6193 | out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK); |
| 6194 | if (out->pcm_device_id < 0) { |
| 6195 | ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)", |
| 6196 | __func__, out->pcm_device_id, out->usecase); |
| 6197 | ret = -EINVAL; |
| 6198 | goto exit; |
| 6199 | } |
| 6200 | |
| 6201 | adjust_mmap_period_count(&out->config, min_size_frames); |
| 6202 | |
Arun Mirpuri | 5d17087 | 2019-03-26 13:21:31 -0700 | [diff] [blame] | 6203 | ALOGD("%s: Opening PCM device card_id(%d) device_id(%d), channels %d", |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 6204 | __func__, adev->snd_card, out->pcm_device_id, out->config.channels); |
| 6205 | out->pcm = pcm_open(adev->snd_card, out->pcm_device_id, |
| 6206 | (PCM_OUT | PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC), &out->config); |
Satish Babu Patakokila | 54ce83d | 2018-07-06 18:00:37 +0530 | [diff] [blame] | 6207 | if (errno == ENETRESET && !pcm_is_ready(out->pcm)) { |
Sharad Sangle | c6f3255 | 2018-05-04 16:15:38 +0530 | [diff] [blame] | 6208 | ALOGE("%s: pcm_open failed errno:%d\n", __func__, errno); |
| 6209 | out->card_status = CARD_STATUS_OFFLINE; |
| 6210 | adev->card_status = CARD_STATUS_OFFLINE; |
| 6211 | ret = -EIO; |
| 6212 | goto exit; |
| 6213 | } |
| 6214 | |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 6215 | if (out->pcm == NULL || !pcm_is_ready(out->pcm)) { |
| 6216 | step = "open"; |
| 6217 | ret = -ENODEV; |
| 6218 | goto exit; |
| 6219 | } |
| 6220 | ret = pcm_mmap_begin(out->pcm, &info->shared_memory_address, &offset1, &frames1); |
| 6221 | if (ret < 0) { |
| 6222 | step = "begin"; |
| 6223 | goto exit; |
| 6224 | } |
| 6225 | info->buffer_size_frames = pcm_get_buffer_size(out->pcm); |
Arun Mirpuri | 5d17087 | 2019-03-26 13:21:31 -0700 | [diff] [blame] | 6226 | buffer_size = pcm_frames_to_bytes(out->pcm, info->buffer_size_frames); |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 6227 | info->burst_size_frames = out->config.period_size; |
Haynes Mathew George | ef51488 | 2017-05-01 17:46:23 -0700 | [diff] [blame] | 6228 | ret = platform_get_mmap_data_fd(adev->platform, |
| 6229 | out->pcm_device_id, 0 /*playback*/, |
| 6230 | &info->shared_memory_fd, |
| 6231 | &mmap_size); |
| 6232 | if (ret < 0) { |
Arun Mirpuri | 5d17087 | 2019-03-26 13:21:31 -0700 | [diff] [blame] | 6233 | // Fall back to non exclusive mode |
| 6234 | info->shared_memory_fd = pcm_get_poll_fd(out->pcm); |
| 6235 | } else { |
Phil Burk | d898ba6 | 2019-06-20 12:49:01 -0700 | [diff] [blame] | 6236 | out->mmap_shared_memory_fd = info->shared_memory_fd; // for closing later |
| 6237 | ALOGV("%s: opened mmap_shared_memory_fd = %d", __func__, out->mmap_shared_memory_fd); |
| 6238 | |
Arun Mirpuri | 5d17087 | 2019-03-26 13:21:31 -0700 | [diff] [blame] | 6239 | if (mmap_size < buffer_size) { |
| 6240 | step = "mmap"; |
| 6241 | goto exit; |
| 6242 | } |
| 6243 | // FIXME: indicate exclusive mode support by returning a negative buffer size |
| 6244 | info->buffer_size_frames *= -1; |
Haynes Mathew George | ef51488 | 2017-05-01 17:46:23 -0700 | [diff] [blame] | 6245 | } |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 6246 | memset(info->shared_memory_address, 0, pcm_frames_to_bytes(out->pcm, |
Haynes Mathew George | ef51488 | 2017-05-01 17:46:23 -0700 | [diff] [blame] | 6247 | info->buffer_size_frames)); |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 6248 | |
| 6249 | ret = pcm_mmap_commit(out->pcm, 0, MMAP_PERIOD_SIZE); |
| 6250 | if (ret < 0) { |
| 6251 | step = "commit"; |
| 6252 | goto exit; |
| 6253 | } |
| 6254 | |
Phil Burk | fe17efd | 2019-03-25 10:23:35 -0700 | [diff] [blame] | 6255 | out->mmap_time_offset_nanos = get_mmap_out_time_offset(); |
| 6256 | |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 6257 | out->standby = false; |
| 6258 | ret = 0; |
| 6259 | |
Arun Mirpuri | 5d17087 | 2019-03-26 13:21:31 -0700 | [diff] [blame] | 6260 | ALOGD("%s: got mmap buffer address %p info->buffer_size_frames %d", |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 6261 | __func__, info->shared_memory_address, info->buffer_size_frames); |
| 6262 | |
| 6263 | exit: |
| 6264 | if (ret != 0) { |
| 6265 | if (out->pcm == NULL) { |
| 6266 | ALOGE("%s: %s - %d", __func__, step, ret); |
| 6267 | } else { |
| 6268 | ALOGE("%s: %s %s", __func__, step, pcm_get_error(out->pcm)); |
| 6269 | pcm_close(out->pcm); |
| 6270 | out->pcm = NULL; |
| 6271 | } |
| 6272 | } |
| 6273 | pthread_mutex_unlock(&adev->lock); |
Sharad Sangle | c6f3255 | 2018-05-04 16:15:38 +0530 | [diff] [blame] | 6274 | pthread_mutex_unlock(&out->lock); |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 6275 | return ret; |
| 6276 | } |
| 6277 | |
| 6278 | static int out_get_mmap_position(const struct audio_stream_out *stream, |
| 6279 | struct audio_mmap_position *position) |
| 6280 | { |
| 6281 | struct stream_out *out = (struct stream_out *)stream; |
| 6282 | ALOGVV("%s", __func__); |
| 6283 | if (position == NULL) { |
| 6284 | return -EINVAL; |
| 6285 | } |
| 6286 | if (out->usecase != USECASE_AUDIO_PLAYBACK_MMAP) { |
Haynes Mathew George | 4ab3ba9 | 2017-12-11 14:49:43 -0800 | [diff] [blame] | 6287 | ALOGE("%s: called on %s", __func__, use_case_table[out->usecase]); |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 6288 | return -ENOSYS; |
| 6289 | } |
| 6290 | if (out->pcm == NULL) { |
| 6291 | return -ENOSYS; |
| 6292 | } |
| 6293 | |
| 6294 | struct timespec ts = { 0, 0 }; |
| 6295 | int ret = pcm_mmap_get_hw_ptr(out->pcm, (unsigned int *)&position->position_frames, &ts); |
| 6296 | if (ret < 0) { |
| 6297 | ALOGE("%s: %s", __func__, pcm_get_error(out->pcm)); |
| 6298 | return ret; |
| 6299 | } |
Phil Burk | fe17efd | 2019-03-25 10:23:35 -0700 | [diff] [blame] | 6300 | position->time_nanoseconds = ts.tv_sec*1000000000LL + ts.tv_nsec |
| 6301 | + out->mmap_time_offset_nanos; |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 6302 | return 0; |
| 6303 | } |
| 6304 | |
| 6305 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6306 | /** audio_stream_in implementation **/ |
| 6307 | static uint32_t in_get_sample_rate(const struct audio_stream *stream) |
| 6308 | { |
| 6309 | struct stream_in *in = (struct stream_in *)stream; |
| 6310 | |
| 6311 | return in->config.rate; |
| 6312 | } |
| 6313 | |
Ravi Kumar Alamanda | bdf1416 | 2014-09-05 16:14:17 -0700 | [diff] [blame] | 6314 | static int in_set_sample_rate(struct audio_stream *stream __unused, |
| 6315 | uint32_t rate __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6316 | { |
| 6317 | return -ENOSYS; |
| 6318 | } |
| 6319 | |
| 6320 | static size_t in_get_buffer_size(const struct audio_stream *stream) |
| 6321 | { |
| 6322 | struct stream_in *in = (struct stream_in *)stream; |
| 6323 | |
Narsinga Rao Chella | 05573b7 | 2013-11-15 15:21:40 -0800 | [diff] [blame] | 6324 | if(in->usecase == USECASE_COMPRESS_VOIP_CALL) |
| 6325 | return voice_extn_compress_voip_in_get_buffer_size(in); |
Mingming Yin | e62d784 | 2013-10-25 16:26:03 -0700 | [diff] [blame] | 6326 | else if(audio_extn_compr_cap_usecase_supported(in->usecase)) |
| 6327 | return audio_extn_compr_cap_get_buffer_size(in->config.format); |
Dhananjay Kumar | ee4d200 | 2016-10-25 18:02:58 +0530 | [diff] [blame] | 6328 | else if(audio_extn_cin_attached_usecase(in->usecase)) |
| 6329 | return audio_extn_cin_get_buffer_size(in); |
Narsinga Rao Chella | 05573b7 | 2013-11-15 15:21:40 -0800 | [diff] [blame] | 6330 | |
Haynes Mathew George | 5beddd4 | 2016-06-27 18:33:40 -0700 | [diff] [blame] | 6331 | return in->config.period_size * in->af_period_multiplier * |
| 6332 | audio_stream_in_frame_size((const struct audio_stream_in *)stream); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6333 | } |
| 6334 | |
| 6335 | static uint32_t in_get_channels(const struct audio_stream *stream) |
| 6336 | { |
| 6337 | struct stream_in *in = (struct stream_in *)stream; |
| 6338 | |
| 6339 | return in->channel_mask; |
| 6340 | } |
| 6341 | |
| 6342 | static audio_format_t in_get_format(const struct audio_stream *stream) |
| 6343 | { |
Narsinga Rao Chella | 05573b7 | 2013-11-15 15:21:40 -0800 | [diff] [blame] | 6344 | struct stream_in *in = (struct stream_in *)stream; |
| 6345 | |
| 6346 | return in->format; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6347 | } |
| 6348 | |
Ravi Kumar Alamanda | bdf1416 | 2014-09-05 16:14:17 -0700 | [diff] [blame] | 6349 | static int in_set_format(struct audio_stream *stream __unused, |
| 6350 | audio_format_t format __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6351 | { |
| 6352 | return -ENOSYS; |
| 6353 | } |
| 6354 | |
| 6355 | static int in_standby(struct audio_stream *stream) |
| 6356 | { |
| 6357 | struct stream_in *in = (struct stream_in *)stream; |
| 6358 | struct audio_device *adev = in->dev; |
| 6359 | int status = 0; |
Sidipotu Ashok | f43018c | 2014-05-02 16:21:50 +0530 | [diff] [blame] | 6360 | ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__, |
| 6361 | stream, in->usecase, use_case_table[in->usecase]); |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 6362 | bool do_stop = true; |
Sidipotu Ashok | f43018c | 2014-05-02 16:21:50 +0530 | [diff] [blame] | 6363 | |
Shiv Maliyappanahalli | 736d4ce | 2015-09-28 15:23:06 -0700 | [diff] [blame] | 6364 | lock_input_stream(in); |
Ravi Kumar Alamanda | 8fa6b19 | 2014-09-09 16:06:42 -0700 | [diff] [blame] | 6365 | if (!in->standby && in->is_st_session) { |
| 6366 | ALOGD("%s: sound trigger pcm stop lab", __func__); |
| 6367 | audio_extn_sound_trigger_stop_lab(in); |
Quinn Male | f605036 | 2019-01-30 15:55:40 -0800 | [diff] [blame] | 6368 | adev->num_va_sessions--; |
Ravi Kumar Alamanda | 8fa6b19 | 2014-09-09 16:06:42 -0700 | [diff] [blame] | 6369 | in->standby = 1; |
| 6370 | } |
| 6371 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6372 | if (!in->standby) { |
Ravi Kumar Alamanda | 8a0f977 | 2015-06-15 10:35:19 -0700 | [diff] [blame] | 6373 | if (adev->adm_deregister_stream) |
| 6374 | adev->adm_deregister_stream(adev->adm_data, in->capture_handle); |
| 6375 | |
Ravi Kumar Alamanda | 8bba9e9 | 2013-11-11 21:09:07 -0800 | [diff] [blame] | 6376 | pthread_mutex_lock(&adev->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6377 | in->standby = true; |
Zhou Song | a889504 | 2016-07-05 17:54:22 +0800 | [diff] [blame] | 6378 | if (in->usecase == USECASE_COMPRESS_VOIP_CALL) { |
kunleiz | becba2d | 2017-09-07 13:37:16 +0800 | [diff] [blame] | 6379 | do_stop = false; |
Zhou Song | a889504 | 2016-07-05 17:54:22 +0800 | [diff] [blame] | 6380 | voice_extn_compress_voip_close_input_stream(stream); |
| 6381 | ALOGD("VOIP input entered standby"); |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 6382 | } else if (in->usecase == USECASE_AUDIO_RECORD_MMAP) { |
| 6383 | do_stop = in->capture_started; |
| 6384 | in->capture_started = false; |
Phil Burk | d898ba6 | 2019-06-20 12:49:01 -0700 | [diff] [blame] | 6385 | if (in->mmap_shared_memory_fd >= 0) { |
| 6386 | ALOGV("%s: closing mmap_shared_memory_fd = %d", |
| 6387 | __func__, in->mmap_shared_memory_fd); |
| 6388 | close(in->mmap_shared_memory_fd); |
| 6389 | in->mmap_shared_memory_fd = -1; |
| 6390 | } |
Zhou Song | a889504 | 2016-07-05 17:54:22 +0800 | [diff] [blame] | 6391 | } else { |
Dhananjay Kumar | ee4d200 | 2016-10-25 18:02:58 +0530 | [diff] [blame] | 6392 | if (audio_extn_cin_attached_usecase(in->usecase)) |
Manish Dewangan | 46e0798 | 2018-12-13 18:18:59 +0530 | [diff] [blame] | 6393 | audio_extn_cin_close_input_stream(in); |
kunleiz | becba2d | 2017-09-07 13:37:16 +0800 | [diff] [blame] | 6394 | } |
| 6395 | |
Arun Mirpuri | 5d17087 | 2019-03-26 13:21:31 -0700 | [diff] [blame] | 6396 | if (in->pcm) { |
| 6397 | ATRACE_BEGIN("pcm_in_close"); |
| 6398 | pcm_close(in->pcm); |
| 6399 | ATRACE_END(); |
| 6400 | in->pcm = NULL; |
| 6401 | } |
| 6402 | |
Carter Hsu | 2e429db | 2019-05-14 18:50:52 +0800 | [diff] [blame] | 6403 | if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 6404 | adev->enable_voicerx = false; |
Carter Hsu | 2e429db | 2019-05-14 18:50:52 +0800 | [diff] [blame] | 6405 | |
| 6406 | if (do_stop) |
Zhou Song | a889504 | 2016-07-05 17:54:22 +0800 | [diff] [blame] | 6407 | status = stop_input_stream(in); |
Quinn Male | f605036 | 2019-01-30 15:55:40 -0800 | [diff] [blame] | 6408 | |
| 6409 | if (in->source == AUDIO_SOURCE_VOICE_RECOGNITION) |
| 6410 | adev->num_va_sessions--; |
| 6411 | |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 6412 | pthread_mutex_unlock(&adev->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6413 | } |
| 6414 | pthread_mutex_unlock(&in->lock); |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 6415 | ALOGV("%s: exit: status(%d)", __func__, status); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6416 | return status; |
| 6417 | } |
| 6418 | |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 6419 | static int in_dump(const struct audio_stream *stream, |
| 6420 | int fd) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6421 | { |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 6422 | struct stream_in *in = (struct stream_in *)stream; |
| 6423 | |
| 6424 | // We try to get the lock for consistency, |
| 6425 | // but it isn't necessary for these variables. |
| 6426 | // If we're not in standby, we may be blocked on a read. |
| 6427 | const bool locked = (pthread_mutex_trylock(&in->lock) == 0); |
| 6428 | dprintf(fd, " Standby: %s\n", in->standby ? "yes" : "no"); |
| 6429 | dprintf(fd, " Frames read: %lld\n", (long long)in->frames_read); |
| 6430 | dprintf(fd, " Frames muted: %lld\n", (long long)in->frames_muted); |
| 6431 | |
| 6432 | if (locked) { |
| 6433 | pthread_mutex_unlock(&in->lock); |
| 6434 | } |
| 6435 | |
| 6436 | // dump error info |
| 6437 | (void)error_log_dump( |
| 6438 | in->error_log, fd, " " /* prefix */, 0 /* lines */, 0 /* limit_ns */); |
| 6439 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6440 | return 0; |
| 6441 | } |
| 6442 | |
Dhananjay Kumar | e6293dd | 2017-05-25 17:25:30 +0530 | [diff] [blame] | 6443 | static void in_snd_mon_cb(void * stream, struct str_parms * parms) |
| 6444 | { |
| 6445 | if (!stream || !parms) |
| 6446 | return; |
| 6447 | |
| 6448 | struct stream_in *in = (struct stream_in *)stream; |
| 6449 | struct audio_device *adev = in->dev; |
| 6450 | |
| 6451 | card_status_t status; |
| 6452 | int card; |
| 6453 | if (parse_snd_card_status(parms, &card, &status) < 0) |
| 6454 | return; |
| 6455 | |
| 6456 | pthread_mutex_lock(&adev->lock); |
| 6457 | bool valid_cb = (card == adev->snd_card); |
| 6458 | pthread_mutex_unlock(&adev->lock); |
| 6459 | |
| 6460 | if (!valid_cb) |
| 6461 | return; |
| 6462 | |
| 6463 | lock_input_stream(in); |
| 6464 | if (in->card_status != status) |
| 6465 | in->card_status = status; |
| 6466 | pthread_mutex_unlock(&in->lock); |
| 6467 | |
| 6468 | ALOGW("in_snd_mon_cb for card %d usecase %s, status %s", card, |
| 6469 | use_case_table[in->usecase], |
| 6470 | status == CARD_STATUS_OFFLINE ? "offline" : "online"); |
| 6471 | |
| 6472 | // a better solution would be to report error back to AF and let |
| 6473 | // it put the stream to standby |
| 6474 | if (status == CARD_STATUS_OFFLINE) |
| 6475 | in_standby(&in->stream.common); |
| 6476 | |
| 6477 | return; |
| 6478 | } |
| 6479 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6480 | static int in_set_parameters(struct audio_stream *stream, const char *kvpairs) |
| 6481 | { |
| 6482 | struct stream_in *in = (struct stream_in *)stream; |
| 6483 | struct audio_device *adev = in->dev; |
| 6484 | struct str_parms *parms; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6485 | char value[32]; |
Shiv Maliyappanahalli | 3e064fd | 2013-12-16 15:54:40 -0800 | [diff] [blame] | 6486 | int ret = 0, val = 0, err; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6487 | |
Sidipotu Ashok | f43018c | 2014-05-02 16:21:50 +0530 | [diff] [blame] | 6488 | ALOGD("%s: enter: kvpairs=%s", __func__, kvpairs); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6489 | parms = str_parms_create_str(kvpairs); |
| 6490 | |
Preetam Singh Ranawat | a5f32b4 | 2014-09-23 12:12:47 +0530 | [diff] [blame] | 6491 | if (!parms) |
| 6492 | goto error; |
Shiv Maliyappanahalli | 736d4ce | 2015-09-28 15:23:06 -0700 | [diff] [blame] | 6493 | lock_input_stream(in); |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 6494 | pthread_mutex_lock(&adev->lock); |
Shiv Maliyappanahalli | 3e064fd | 2013-12-16 15:54:40 -0800 | [diff] [blame] | 6495 | |
| 6496 | err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value)); |
| 6497 | if (err >= 0) { |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6498 | val = atoi(value); |
| 6499 | /* no audio source uses val == 0 */ |
| 6500 | if ((in->source != val) && (val != 0)) { |
| 6501 | in->source = val; |
Narsinga Rao Chella | 2a99dea | 2014-01-24 15:33:23 -0800 | [diff] [blame] | 6502 | if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) && |
| 6503 | (in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) && |
| 6504 | (voice_extn_compress_voip_is_format_supported(in->format)) && |
Shiv Maliyappanahalli | e66aba2 | 2016-01-27 16:08:57 -0800 | [diff] [blame] | 6505 | (in->config.rate == 8000 || in->config.rate == 16000 || |
| 6506 | in->config.rate == 32000 || in->config.rate == 48000 ) && |
Ravi Kumar Alamanda | c3bc081 | 2014-09-08 15:34:32 -0700 | [diff] [blame] | 6507 | (audio_channel_count_from_in_mask(in->channel_mask) == 1)) { |
Narsinga Rao Chella | 7d5a3e8 | 2014-02-04 16:23:52 -0800 | [diff] [blame] | 6508 | err = voice_extn_compress_voip_open_input_stream(in); |
| 6509 | if (err != 0) { |
Narsinga Rao Chella | 2a99dea | 2014-01-24 15:33:23 -0800 | [diff] [blame] | 6510 | ALOGE("%s: Compress voip input cannot be opened, error:%d", |
Narsinga Rao Chella | 7d5a3e8 | 2014-02-04 16:23:52 -0800 | [diff] [blame] | 6511 | __func__, err); |
Narsinga Rao Chella | 2a99dea | 2014-01-24 15:33:23 -0800 | [diff] [blame] | 6512 | } |
| 6513 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6514 | } |
| 6515 | } |
| 6516 | |
Shiv Maliyappanahalli | 3e064fd | 2013-12-16 15:54:40 -0800 | [diff] [blame] | 6517 | err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value)); |
| 6518 | if (err >= 0) { |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6519 | val = atoi(value); |
Kevin Rocard | fce1900 | 2017-08-07 19:21:36 -0700 | [diff] [blame] | 6520 | if (((int)in->device != val) && (val != 0) && audio_is_input_device(val) ) { |
| 6521 | |
| 6522 | // Workaround: If routing to an non existing usb device, fail gracefully |
| 6523 | // The routing request will otherwise block during 10 second |
| 6524 | int card; |
| 6525 | if (audio_is_usb_in_device(val) && |
| 6526 | (card = get_alive_usb_card(parms)) >= 0) { |
| 6527 | |
| 6528 | ALOGW("in_set_parameters() ignoring rerouting to non existing USB card %d", card); |
| 6529 | ret = -ENOSYS; |
| 6530 | } else { |
| 6531 | |
| 6532 | in->device = val; |
| 6533 | /* If recording is in progress, change the tx device to new device */ |
| 6534 | if (!in->standby && !in->is_st_session) { |
| 6535 | ALOGV("update input routing change"); |
| 6536 | // inform adm before actual routing to prevent glitches. |
| 6537 | if (adev->adm_on_routing_change) { |
Haynes Mathew George | 822b549 | 2016-07-01 16:57:24 -0700 | [diff] [blame] | 6538 | adev->adm_on_routing_change(adev->adm_data, |
| 6539 | in->capture_handle); |
Kevin Rocard | fce1900 | 2017-08-07 19:21:36 -0700 | [diff] [blame] | 6540 | ret = select_devices(adev, in->usecase); |
Aniket Kumar Lata | 60586a9 | 2019-05-22 22:18:55 -0700 | [diff] [blame] | 6541 | if (in->usecase == USECASE_AUDIO_RECORD_LOW_LATENCY) |
| 6542 | adev->adm_routing_changed = true; |
Kevin Rocard | fce1900 | 2017-08-07 19:21:36 -0700 | [diff] [blame] | 6543 | } |
| 6544 | } |
Haynes Mathew George | 5beddd4 | 2016-06-27 18:33:40 -0700 | [diff] [blame] | 6545 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6546 | } |
| 6547 | } |
| 6548 | |
Dhananjay Kumar | 4d91c1a | 2016-12-01 23:27:29 +0530 | [diff] [blame] | 6549 | err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_PROFILE, value, sizeof(value)); |
| 6550 | if (err >= 0) { |
| 6551 | strlcpy(in->profile, value, sizeof(in->profile)); |
| 6552 | ALOGV("updating stream profile with value '%s'", in->profile); |
| 6553 | audio_extn_utils_update_stream_input_app_type_cfg(adev->platform, |
| 6554 | &adev->streams_input_cfg_list, |
| 6555 | in->device, in->flags, in->format, |
| 6556 | in->sample_rate, in->bit_width, |
| 6557 | in->profile, &in->app_type_cfg); |
| 6558 | } |
| 6559 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6560 | pthread_mutex_unlock(&adev->lock); |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 6561 | pthread_mutex_unlock(&in->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6562 | |
| 6563 | str_parms_destroy(parms); |
Preetam Singh Ranawat | a5f32b4 | 2014-09-23 12:12:47 +0530 | [diff] [blame] | 6564 | error: |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 6565 | ALOGV("%s: exit: status(%d)", __func__, ret); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6566 | return ret; |
| 6567 | } |
| 6568 | |
| 6569 | static char* in_get_parameters(const struct audio_stream *stream, |
| 6570 | const char *keys) |
| 6571 | { |
Narsinga Rao Chella | 05573b7 | 2013-11-15 15:21:40 -0800 | [diff] [blame] | 6572 | struct stream_in *in = (struct stream_in *)stream; |
| 6573 | struct str_parms *query = str_parms_create_str(keys); |
| 6574 | char *str; |
Narsinga Rao Chella | 05573b7 | 2013-11-15 15:21:40 -0800 | [diff] [blame] | 6575 | struct str_parms *reply = str_parms_create(); |
Haynes Mathew George | b51ceb1 | 2014-06-30 13:56:18 -0700 | [diff] [blame] | 6576 | |
| 6577 | if (!query || !reply) { |
Alexy Joseph | aee4fdd | 2016-01-29 13:02:07 -0800 | [diff] [blame] | 6578 | if (reply) { |
| 6579 | str_parms_destroy(reply); |
| 6580 | } |
| 6581 | if (query) { |
| 6582 | str_parms_destroy(query); |
| 6583 | } |
Haynes Mathew George | b51ceb1 | 2014-06-30 13:56:18 -0700 | [diff] [blame] | 6584 | ALOGE("in_get_parameters: failed to create query or reply"); |
| 6585 | return NULL; |
| 6586 | } |
| 6587 | |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 6588 | ALOGV("%s: enter: keys - %s %s ", __func__, use_case_table[in->usecase], keys); |
Narsinga Rao Chella | 05573b7 | 2013-11-15 15:21:40 -0800 | [diff] [blame] | 6589 | |
| 6590 | voice_extn_in_get_parameters(in, query, reply); |
| 6591 | |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 6592 | stream_get_parameter_channels(query, reply, |
| 6593 | &in->supported_channel_masks[0]); |
| 6594 | stream_get_parameter_formats(query, reply, |
| 6595 | &in->supported_formats[0]); |
| 6596 | stream_get_parameter_rates(query, reply, |
| 6597 | &in->supported_sample_rates[0]); |
Narsinga Rao Chella | 05573b7 | 2013-11-15 15:21:40 -0800 | [diff] [blame] | 6598 | str = str_parms_to_str(reply); |
| 6599 | str_parms_destroy(query); |
| 6600 | str_parms_destroy(reply); |
| 6601 | |
| 6602 | ALOGV("%s: exit: returns - %s", __func__, str); |
| 6603 | return str; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6604 | } |
| 6605 | |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 6606 | static int in_set_gain(struct audio_stream_in *stream, |
| 6607 | float gain) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6608 | { |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 6609 | struct stream_in *in = (struct stream_in *)stream; |
| 6610 | char mixer_ctl_name[128]; |
| 6611 | struct mixer_ctl *ctl; |
| 6612 | int ctl_value; |
| 6613 | |
| 6614 | ALOGV("%s: gain %f", __func__, gain); |
| 6615 | |
| 6616 | if (stream == NULL) |
| 6617 | return -EINVAL; |
| 6618 | |
| 6619 | /* in_set_gain() only used to silence MMAP capture for now */ |
| 6620 | if (in->usecase != USECASE_AUDIO_RECORD_MMAP) |
| 6621 | return -ENOSYS; |
| 6622 | |
| 6623 | snprintf(mixer_ctl_name, sizeof(mixer_ctl_name), "Capture %d Volume", in->pcm_device_id); |
| 6624 | |
| 6625 | ctl = mixer_get_ctl_by_name(in->dev->mixer, mixer_ctl_name); |
| 6626 | if (!ctl) { |
| 6627 | ALOGW("%s: Could not get ctl for mixer cmd - %s", |
| 6628 | __func__, mixer_ctl_name); |
| 6629 | return -ENOSYS; |
| 6630 | } |
| 6631 | |
| 6632 | if (gain < RECORD_GAIN_MIN) |
| 6633 | gain = RECORD_GAIN_MIN; |
| 6634 | else if (gain > RECORD_GAIN_MAX) |
| 6635 | gain = RECORD_GAIN_MAX; |
| 6636 | ctl_value = (int)(RECORD_VOLUME_CTL_MAX * gain); |
| 6637 | |
| 6638 | mixer_ctl_set_value(ctl, 0, ctl_value); |
| 6639 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6640 | return 0; |
| 6641 | } |
| 6642 | |
| 6643 | static ssize_t in_read(struct audio_stream_in *stream, void *buffer, |
| 6644 | size_t bytes) |
| 6645 | { |
| 6646 | struct stream_in *in = (struct stream_in *)stream; |
Pallavi | d7c7a27 | 2018-01-16 11:22:55 +0530 | [diff] [blame] | 6647 | |
| 6648 | if (in == NULL) { |
| 6649 | ALOGE("%s: stream_in ptr is NULL", __func__); |
| 6650 | return -EINVAL; |
| 6651 | } |
| 6652 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6653 | struct audio_device *adev = in->dev; |
Satya Krishna Pindiproli | f1cd92b | 2016-04-14 19:05:23 +0530 | [diff] [blame] | 6654 | int ret = -1; |
Dhananjay Kumar | 97b81e3 | 2019-05-15 21:00:21 +0530 | [diff] [blame] | 6655 | size_t bytes_read = 0, frame_size = 0; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6656 | |
Shiv Maliyappanahalli | 736d4ce | 2015-09-28 15:23:06 -0700 | [diff] [blame] | 6657 | lock_input_stream(in); |
Naresh Tanniru | 4c63039 | 2014-05-12 01:05:52 +0530 | [diff] [blame] | 6658 | |
Bharath Ramachandramurthy | 76d2089 | 2015-04-27 15:47:55 -0700 | [diff] [blame] | 6659 | if (in->is_st_session) { |
| 6660 | ALOGVV(" %s: reading on st session bytes=%zu", __func__, bytes); |
| 6661 | /* Read from sound trigger HAL */ |
| 6662 | audio_extn_sound_trigger_read(in, buffer, bytes); |
Quinn Male | f605036 | 2019-01-30 15:55:40 -0800 | [diff] [blame] | 6663 | if (in->standby) { |
| 6664 | adev->num_va_sessions++; |
| 6665 | in->standby = 0; |
| 6666 | } |
Bharath Ramachandramurthy | 76d2089 | 2015-04-27 15:47:55 -0700 | [diff] [blame] | 6667 | pthread_mutex_unlock(&in->lock); |
| 6668 | return bytes; |
| 6669 | } |
| 6670 | |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 6671 | if (in->usecase == USECASE_AUDIO_RECORD_MMAP) { |
| 6672 | ret = -ENOSYS; |
| 6673 | goto exit; |
| 6674 | } |
| 6675 | |
Aniket Kumar Lata | 60586a9 | 2019-05-22 22:18:55 -0700 | [diff] [blame] | 6676 | if (in->usecase == USECASE_AUDIO_RECORD_LOW_LATENCY && |
| 6677 | !in->standby && adev->adm_routing_changed) { |
| 6678 | ret = -ENOSYS; |
| 6679 | goto exit; |
| 6680 | } |
| 6681 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6682 | if (in->standby) { |
Bharath Ramachandramurthy | 76d2089 | 2015-04-27 15:47:55 -0700 | [diff] [blame] | 6683 | pthread_mutex_lock(&adev->lock); |
| 6684 | if (in->usecase == USECASE_COMPRESS_VOIP_CALL) |
| 6685 | ret = voice_extn_compress_voip_start_input_stream(in); |
| 6686 | else |
| 6687 | ret = start_input_stream(in); |
Quinn Male | f605036 | 2019-01-30 15:55:40 -0800 | [diff] [blame] | 6688 | if (!ret && in->source == AUDIO_SOURCE_VOICE_RECOGNITION) |
| 6689 | adev->num_va_sessions++; |
Bharath Ramachandramurthy | 76d2089 | 2015-04-27 15:47:55 -0700 | [diff] [blame] | 6690 | pthread_mutex_unlock(&adev->lock); |
| 6691 | if (ret != 0) { |
| 6692 | goto exit; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6693 | } |
| 6694 | in->standby = 0; |
| 6695 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6696 | |
Deeraj Soman | 30cc7ae | 2019-03-18 16:26:55 +0530 | [diff] [blame] | 6697 | /* Avoid read if capture_stopped is set */ |
| 6698 | if (android_atomic_acquire_load(&(in->capture_stopped)) > 0) { |
| 6699 | ALOGD("%s: force stopped catpure session, ignoring read request", __func__); |
| 6700 | ret = -EINVAL; |
| 6701 | goto exit; |
| 6702 | } |
| 6703 | |
Haynes Mathew George | 5beddd4 | 2016-06-27 18:33:40 -0700 | [diff] [blame] | 6704 | // what's the duration requested by the client? |
| 6705 | long ns = 0; |
| 6706 | |
Dhananjay Kumar | ee4d200 | 2016-10-25 18:02:58 +0530 | [diff] [blame] | 6707 | if (in->pcm && in->config.rate) |
Haynes Mathew George | 5beddd4 | 2016-06-27 18:33:40 -0700 | [diff] [blame] | 6708 | ns = pcm_bytes_to_frames(in->pcm, bytes)*1000000000LL/ |
| 6709 | in->config.rate; |
| 6710 | |
Aniket Kumar Lata | 60586a9 | 2019-05-22 22:18:55 -0700 | [diff] [blame] | 6711 | ret = request_in_focus(in, ns); |
| 6712 | if (ret != 0) |
| 6713 | goto exit; |
Haynes Mathew George | 5beddd4 | 2016-06-27 18:33:40 -0700 | [diff] [blame] | 6714 | bool use_mmap = is_mmap_usecase(in->usecase) || in->realtime; |
Ravi Kumar Alamanda | 8a0f977 | 2015-06-15 10:35:19 -0700 | [diff] [blame] | 6715 | |
Dhananjay Kumar | ee4d200 | 2016-10-25 18:02:58 +0530 | [diff] [blame] | 6716 | if (audio_extn_cin_attached_usecase(in->usecase)) { |
| 6717 | ret = audio_extn_cin_read(in, buffer, bytes, &bytes_read); |
| 6718 | } else if (in->pcm) { |
Manish Dewangan | ba9fcfa | 2016-03-24 16:20:06 +0530 | [diff] [blame] | 6719 | if (audio_extn_ssr_get_stream() == in) { |
Apoorv Raghuvanshi | 6178a3f | 2013-10-19 12:38:54 -0700 | [diff] [blame] | 6720 | ret = audio_extn_ssr_read(stream, buffer, bytes); |
Manish Dewangan | ba9fcfa | 2016-03-24 16:20:06 +0530 | [diff] [blame] | 6721 | } else if (audio_extn_compr_cap_usecase_supported(in->usecase)) { |
Mingming Yin | e62d784 | 2013-10-25 16:26:03 -0700 | [diff] [blame] | 6722 | ret = audio_extn_compr_cap_read(in, buffer, bytes); |
Haynes Mathew George | 5beddd4 | 2016-06-27 18:33:40 -0700 | [diff] [blame] | 6723 | } else if (use_mmap) { |
Ravi Kumar Alamanda | 060bc5a | 2014-09-05 13:51:35 -0700 | [diff] [blame] | 6724 | ret = pcm_mmap_read(in->pcm, buffer, bytes); |
Garmond Leung | e2433c3 | 2017-09-28 21:51:22 -0700 | [diff] [blame] | 6725 | } else if (audio_extn_ffv_get_stream() == in) { |
| 6726 | ret = audio_extn_ffv_read(stream, buffer, bytes); |
Manish Dewangan | ba9fcfa | 2016-03-24 16:20:06 +0530 | [diff] [blame] | 6727 | } else { |
Apoorv Raghuvanshi | 6178a3f | 2013-10-19 12:38:54 -0700 | [diff] [blame] | 6728 | ret = pcm_read(in->pcm, buffer, bytes); |
Dhananjay Kumar | ee4d200 | 2016-10-25 18:02:58 +0530 | [diff] [blame] | 6729 | /* data from DSP comes in 24_8 format, convert it to 8_24 */ |
| 6730 | if (!ret && bytes > 0 && (in->format == AUDIO_FORMAT_PCM_8_24_BIT)) { |
| 6731 | if (audio_extn_utils_convert_format_24_8_to_8_24(buffer, bytes) |
| 6732 | != bytes) { |
Manish Dewangan | ba9fcfa | 2016-03-24 16:20:06 +0530 | [diff] [blame] | 6733 | ret = -EINVAL; |
| 6734 | goto exit; |
| 6735 | } |
Dhananjay Kumar | ee4d200 | 2016-10-25 18:02:58 +0530 | [diff] [blame] | 6736 | } else if (ret < 0) { |
Manish Dewangan | ba9fcfa | 2016-03-24 16:20:06 +0530 | [diff] [blame] | 6737 | ret = -errno; |
| 6738 | } |
| 6739 | } |
Dhananjay Kumar | ee4d200 | 2016-10-25 18:02:58 +0530 | [diff] [blame] | 6740 | /* bytes read is always set to bytes for non compress usecases */ |
| 6741 | bytes_read = bytes; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6742 | } |
| 6743 | |
Haynes Mathew George | 5beddd4 | 2016-06-27 18:33:40 -0700 | [diff] [blame] | 6744 | release_in_focus(in); |
Ravi Kumar Alamanda | 8a0f977 | 2015-06-15 10:35:19 -0700 | [diff] [blame] | 6745 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6746 | /* |
Quinn Male | f605036 | 2019-01-30 15:55:40 -0800 | [diff] [blame] | 6747 | * Instead of writing zeroes here, we could trust the hardware to always |
| 6748 | * provide zeroes when muted. This is also muted with voice recognition |
| 6749 | * usecases so that other clients do not have access to voice recognition |
| 6750 | * data. |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6751 | */ |
Quinn Male | f605036 | 2019-01-30 15:55:40 -0800 | [diff] [blame] | 6752 | if ((ret == 0 && voice_get_mic_mute(adev) && |
| 6753 | !voice_is_in_call_rec_stream(in) && |
| 6754 | in->usecase != USECASE_AUDIO_RECORD_AFE_PROXY) || |
| 6755 | (adev->num_va_sessions && |
| 6756 | in->source != AUDIO_SOURCE_VOICE_RECOGNITION && |
| 6757 | property_get_bool("persist.vendor.audio.va_concurrency_mute_enabled", |
| 6758 | false))) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6759 | memset(buffer, 0, bytes); |
| 6760 | |
| 6761 | exit: |
Dhananjay Kumar | 97b81e3 | 2019-05-15 21:00:21 +0530 | [diff] [blame] | 6762 | frame_size = audio_stream_in_frame_size(stream); |
| 6763 | if (frame_size > 0) |
| 6764 | in->frames_read += bytes_read/frame_size; |
| 6765 | |
Bharath Ramachandramurthy | 76d2089 | 2015-04-27 15:47:55 -0700 | [diff] [blame] | 6766 | if (-ENETRESET == ret) |
Dhananjay Kumar | e6293dd | 2017-05-25 17:25:30 +0530 | [diff] [blame] | 6767 | in->card_status = CARD_STATUS_OFFLINE; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6768 | pthread_mutex_unlock(&in->lock); |
| 6769 | |
| 6770 | if (ret != 0) { |
Venkata Narendra Kumar Gutta | bc9c9ca | 2014-06-25 20:38:03 +0530 | [diff] [blame] | 6771 | if (in->usecase == USECASE_COMPRESS_VOIP_CALL) { |
Venkata Narendra Kumar Gutta | 9181214 | 2014-08-11 18:20:49 +0530 | [diff] [blame] | 6772 | pthread_mutex_lock(&adev->lock); |
Venkata Narendra Kumar Gutta | bc9c9ca | 2014-06-25 20:38:03 +0530 | [diff] [blame] | 6773 | voice_extn_compress_voip_close_input_stream(&in->stream.common); |
Venkata Narendra Kumar Gutta | 9181214 | 2014-08-11 18:20:49 +0530 | [diff] [blame] | 6774 | pthread_mutex_unlock(&adev->lock); |
Venkata Narendra Kumar Gutta | bc9c9ca | 2014-06-25 20:38:03 +0530 | [diff] [blame] | 6775 | in->standby = true; |
| 6776 | } |
Sharad Sangle | d17c912 | 2017-03-20 15:58:52 +0530 | [diff] [blame] | 6777 | if (!audio_extn_cin_attached_usecase(in->usecase)) { |
| 6778 | bytes_read = bytes; |
| 6779 | memset(buffer, 0, bytes); |
| 6780 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6781 | in_standby(&in->stream.common); |
Aniket Kumar Lata | 60586a9 | 2019-05-22 22:18:55 -0700 | [diff] [blame] | 6782 | if (in->usecase == USECASE_AUDIO_RECORD_LOW_LATENCY) |
| 6783 | adev->adm_routing_changed = false; |
Ravi Kumar Alamanda | 8fa6b19 | 2014-09-09 16:06:42 -0700 | [diff] [blame] | 6784 | ALOGV("%s: read failed status %d- sleeping for buffer duration", __func__, ret); |
Ashish Jain | bbce432 | 2016-02-16 13:25:27 +0530 | [diff] [blame] | 6785 | usleep((uint64_t)bytes * 1000000 / audio_stream_in_frame_size(stream) / |
Naresh Tanniru | 4c63039 | 2014-05-12 01:05:52 +0530 | [diff] [blame] | 6786 | in_get_sample_rate(&in->stream.common)); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6787 | } |
Dhananjay Kumar | ee4d200 | 2016-10-25 18:02:58 +0530 | [diff] [blame] | 6788 | return bytes_read; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6789 | } |
| 6790 | |
Ravi Kumar Alamanda | bdf1416 | 2014-09-05 16:14:17 -0700 | [diff] [blame] | 6791 | static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6792 | { |
| 6793 | return 0; |
| 6794 | } |
| 6795 | |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 6796 | static int in_get_capture_position(const struct audio_stream_in *stream, |
| 6797 | int64_t *frames, int64_t *time) |
| 6798 | { |
| 6799 | if (stream == NULL || frames == NULL || time == NULL) { |
| 6800 | return -EINVAL; |
| 6801 | } |
| 6802 | struct stream_in *in = (struct stream_in *)stream; |
| 6803 | int ret = -ENOSYS; |
| 6804 | |
| 6805 | lock_input_stream(in); |
| 6806 | // note: ST sessions do not close the alsa pcm driver synchronously |
| 6807 | // on standby. Therefore, we may return an error even though the |
| 6808 | // pcm stream is still opened. |
| 6809 | if (in->standby) { |
| 6810 | ALOGE_IF(in->pcm != NULL && !in->is_st_session, |
| 6811 | "%s stream in standby but pcm not NULL for non ST session", __func__); |
| 6812 | goto exit; |
| 6813 | } |
| 6814 | if (in->pcm) { |
| 6815 | struct timespec timestamp; |
| 6816 | unsigned int avail; |
| 6817 | if (pcm_get_htimestamp(in->pcm, &avail, ×tamp) == 0) { |
| 6818 | *frames = in->frames_read + avail; |
| 6819 | *time = timestamp.tv_sec * 1000000000LL + timestamp.tv_nsec; |
| 6820 | ret = 0; |
| 6821 | } |
| 6822 | } |
| 6823 | exit: |
| 6824 | pthread_mutex_unlock(&in->lock); |
| 6825 | return ret; |
| 6826 | } |
| 6827 | |
Carter Hsu | 2e429db | 2019-05-14 18:50:52 +0800 | [diff] [blame] | 6828 | static int in_update_effect_list(bool add, effect_handle_t effect, |
| 6829 | struct listnode *head) |
| 6830 | { |
| 6831 | struct listnode *node; |
| 6832 | struct in_effect_list *elist = NULL; |
| 6833 | struct in_effect_list *target = NULL; |
| 6834 | int ret = 0; |
| 6835 | |
| 6836 | if (!head) |
| 6837 | return ret; |
| 6838 | |
| 6839 | list_for_each(node, head) { |
| 6840 | elist = node_to_item(node, struct in_effect_list, list); |
| 6841 | if (elist->handle == effect) { |
| 6842 | target = elist; |
| 6843 | break; |
| 6844 | } |
| 6845 | } |
| 6846 | |
| 6847 | if (add) { |
| 6848 | if (target) { |
| 6849 | ALOGD("effect %p already exist", effect); |
| 6850 | return ret; |
| 6851 | } |
| 6852 | |
| 6853 | target = (struct in_effect_list *) |
| 6854 | calloc(1, sizeof(struct in_effect_list)); |
| 6855 | |
| 6856 | if (!target) { |
| 6857 | ALOGE("%s:fail to allocate memory", __func__); |
| 6858 | return -ENOMEM; |
| 6859 | } |
| 6860 | |
| 6861 | target->handle = effect; |
| 6862 | list_add_tail(head, &target->list); |
| 6863 | } else { |
| 6864 | if (target) { |
| 6865 | list_remove(&target->list); |
| 6866 | free(target); |
| 6867 | } |
| 6868 | } |
| 6869 | |
| 6870 | return ret; |
| 6871 | } |
| 6872 | |
Ravi Kumar Alamanda | f70ffb4 | 2013-04-16 15:55:53 -0700 | [diff] [blame] | 6873 | static int add_remove_audio_effect(const struct audio_stream *stream, |
| 6874 | effect_handle_t effect, |
| 6875 | bool enable) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6876 | { |
Ravi Kumar Alamanda | f70ffb4 | 2013-04-16 15:55:53 -0700 | [diff] [blame] | 6877 | struct stream_in *in = (struct stream_in *)stream; |
| 6878 | int status = 0; |
| 6879 | effect_descriptor_t desc; |
| 6880 | |
| 6881 | status = (*effect)->get_descriptor(effect, &desc); |
Vikram Panduranga | df59cae | 2017-08-03 18:04:55 -0700 | [diff] [blame] | 6882 | ALOGV("%s: status %d in->standby %d enable:%d", __func__, status, in->standby, enable); |
| 6883 | |
Ravi Kumar Alamanda | f70ffb4 | 2013-04-16 15:55:53 -0700 | [diff] [blame] | 6884 | if (status != 0) |
| 6885 | return status; |
| 6886 | |
Shiv Maliyappanahalli | 736d4ce | 2015-09-28 15:23:06 -0700 | [diff] [blame] | 6887 | lock_input_stream(in); |
Ravi Kumar Alamanda | f70ffb4 | 2013-04-16 15:55:53 -0700 | [diff] [blame] | 6888 | pthread_mutex_lock(&in->dev->lock); |
kunleiz | d96526c | 2018-04-09 11:12:32 +0800 | [diff] [blame] | 6889 | if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION || |
Carter Hsu | 2e429db | 2019-05-14 18:50:52 +0800 | [diff] [blame] | 6890 | in->source == AUDIO_SOURCE_VOICE_RECOGNITION || |
| 6891 | adev->mode == AUDIO_MODE_IN_COMMUNICATION) && |
Ravi Kumar Alamanda | f70ffb4 | 2013-04-16 15:55:53 -0700 | [diff] [blame] | 6892 | (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) { |
Carter Hsu | 2e429db | 2019-05-14 18:50:52 +0800 | [diff] [blame] | 6893 | |
| 6894 | in_update_effect_list(enable, effect, &in->aec_list); |
| 6895 | enable = !list_empty(&in->aec_list); |
| 6896 | if (enable == in->enable_aec) |
| 6897 | goto exit; |
| 6898 | |
Ravi Kumar Alamanda | f70ffb4 | 2013-04-16 15:55:53 -0700 | [diff] [blame] | 6899 | in->enable_aec = enable; |
Carter Hsu | 2e429db | 2019-05-14 18:50:52 +0800 | [diff] [blame] | 6900 | ALOGD("AEC enable %d", enable); |
| 6901 | |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 6902 | if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION || |
| 6903 | in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) { |
| 6904 | in->dev->enable_voicerx = enable; |
| 6905 | struct audio_usecase *usecase; |
| 6906 | struct listnode *node; |
| 6907 | list_for_each(node, &in->dev->usecase_list) { |
| 6908 | usecase = node_to_item(node, struct audio_usecase, list); |
| 6909 | if (usecase->type == PCM_PLAYBACK) |
| 6910 | select_devices(in->dev, usecase->id); |
| 6911 | } |
| 6912 | } |
Vikram Panduranga | df59cae | 2017-08-03 18:04:55 -0700 | [diff] [blame] | 6913 | if (!in->standby) { |
| 6914 | if (enable_disable_effect(in->dev, EFFECT_AEC, enable) == ENOSYS) |
| 6915 | select_devices(in->dev, in->usecase); |
| 6916 | } |
| 6917 | |
Ravi Kumar Alamanda | f70ffb4 | 2013-04-16 15:55:53 -0700 | [diff] [blame] | 6918 | } |
Carter Hsu | 2e429db | 2019-05-14 18:50:52 +0800 | [diff] [blame] | 6919 | if (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0) { |
| 6920 | |
| 6921 | in_update_effect_list(enable, effect, &in->ns_list); |
| 6922 | enable = !list_empty(&in->ns_list); |
| 6923 | if (enable == in->enable_ns) |
| 6924 | goto exit; |
| 6925 | |
Ravi Kumar Alamanda | 198185e | 2013-11-07 15:42:19 -0800 | [diff] [blame] | 6926 | in->enable_ns = enable; |
Carter Hsu | 2e429db | 2019-05-14 18:50:52 +0800 | [diff] [blame] | 6927 | ALOGD("NS enable %d", enable); |
Vikram Panduranga | df59cae | 2017-08-03 18:04:55 -0700 | [diff] [blame] | 6928 | if (!in->standby) { |
kunleiz | d96526c | 2018-04-09 11:12:32 +0800 | [diff] [blame] | 6929 | if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION || |
| 6930 | in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) { |
Vikram Panduranga | df59cae | 2017-08-03 18:04:55 -0700 | [diff] [blame] | 6931 | if (enable_disable_effect(in->dev, EFFECT_NS, enable) == ENOSYS) |
| 6932 | select_devices(in->dev, in->usecase); |
| 6933 | } else |
| 6934 | select_devices(in->dev, in->usecase); |
| 6935 | } |
Ravi Kumar Alamanda | 198185e | 2013-11-07 15:42:19 -0800 | [diff] [blame] | 6936 | } |
Carter Hsu | 2e429db | 2019-05-14 18:50:52 +0800 | [diff] [blame] | 6937 | exit: |
Ravi Kumar Alamanda | f70ffb4 | 2013-04-16 15:55:53 -0700 | [diff] [blame] | 6938 | pthread_mutex_unlock(&in->dev->lock); |
| 6939 | pthread_mutex_unlock(&in->lock); |
| 6940 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6941 | return 0; |
| 6942 | } |
| 6943 | |
Ravi Kumar Alamanda | f70ffb4 | 2013-04-16 15:55:53 -0700 | [diff] [blame] | 6944 | static int in_add_audio_effect(const struct audio_stream *stream, |
| 6945 | effect_handle_t effect) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6946 | { |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 6947 | ALOGV("%s: effect %p", __func__, effect); |
Ravi Kumar Alamanda | f70ffb4 | 2013-04-16 15:55:53 -0700 | [diff] [blame] | 6948 | return add_remove_audio_effect(stream, effect, true); |
| 6949 | } |
| 6950 | |
| 6951 | static int in_remove_audio_effect(const struct audio_stream *stream, |
| 6952 | effect_handle_t effect) |
| 6953 | { |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 6954 | ALOGV("%s: effect %p", __func__, effect); |
Ravi Kumar Alamanda | f70ffb4 | 2013-04-16 15:55:53 -0700 | [diff] [blame] | 6955 | return add_remove_audio_effect(stream, effect, false); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6956 | } |
| 6957 | |
Derek Chen | f939fb7 | 2018-11-13 13:34:41 -0800 | [diff] [blame] | 6958 | streams_input_ctxt_t *in_get_stream(struct audio_device *dev, |
| 6959 | audio_io_handle_t input) |
| 6960 | { |
| 6961 | struct listnode *node; |
| 6962 | |
| 6963 | list_for_each(node, &dev->active_inputs_list) { |
| 6964 | streams_input_ctxt_t *in_ctxt = node_to_item(node, |
| 6965 | streams_input_ctxt_t, |
| 6966 | list); |
| 6967 | if (in_ctxt->input->capture_handle == input) { |
| 6968 | return in_ctxt; |
| 6969 | } |
| 6970 | } |
| 6971 | return NULL; |
| 6972 | } |
| 6973 | |
| 6974 | streams_output_ctxt_t *out_get_stream(struct audio_device *dev, |
| 6975 | audio_io_handle_t output) |
| 6976 | { |
| 6977 | struct listnode *node; |
| 6978 | |
| 6979 | list_for_each(node, &dev->active_outputs_list) { |
| 6980 | streams_output_ctxt_t *out_ctxt = node_to_item(node, |
| 6981 | streams_output_ctxt_t, |
| 6982 | list); |
| 6983 | if (out_ctxt->output->handle == output) { |
| 6984 | return out_ctxt; |
| 6985 | } |
| 6986 | } |
| 6987 | return NULL; |
| 6988 | } |
| 6989 | |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 6990 | static int in_stop(const struct audio_stream_in* stream) |
| 6991 | { |
| 6992 | struct stream_in *in = (struct stream_in *)stream; |
| 6993 | struct audio_device *adev = in->dev; |
| 6994 | |
| 6995 | int ret = -ENOSYS; |
| 6996 | ALOGV("%s", __func__); |
| 6997 | pthread_mutex_lock(&adev->lock); |
| 6998 | if (in->usecase == USECASE_AUDIO_RECORD_MMAP && !in->standby && |
| 6999 | in->capture_started && in->pcm != NULL) { |
| 7000 | pcm_stop(in->pcm); |
| 7001 | ret = stop_input_stream(in); |
| 7002 | in->capture_started = false; |
| 7003 | } |
| 7004 | pthread_mutex_unlock(&adev->lock); |
| 7005 | return ret; |
| 7006 | } |
| 7007 | |
| 7008 | static int in_start(const struct audio_stream_in* stream) |
| 7009 | { |
| 7010 | struct stream_in *in = (struct stream_in *)stream; |
| 7011 | struct audio_device *adev = in->dev; |
| 7012 | int ret = -ENOSYS; |
| 7013 | |
| 7014 | ALOGV("%s in %p", __func__, in); |
| 7015 | pthread_mutex_lock(&adev->lock); |
| 7016 | if (in->usecase == USECASE_AUDIO_RECORD_MMAP && !in->standby && |
| 7017 | !in->capture_started && in->pcm != NULL) { |
| 7018 | if (!in->capture_started) { |
| 7019 | ret = start_input_stream(in); |
| 7020 | if (ret == 0) { |
| 7021 | in->capture_started = true; |
| 7022 | } |
| 7023 | } |
| 7024 | } |
| 7025 | pthread_mutex_unlock(&adev->lock); |
| 7026 | return ret; |
| 7027 | } |
| 7028 | |
Phil Burk | e0a86d1 | 2019-02-16 22:28:11 -0800 | [diff] [blame] | 7029 | // Read offset for the positional timestamp from a persistent vendor property. |
| 7030 | // This is to workaround apparent inaccuracies in the timing information that |
| 7031 | // is used by the AAudio timing model. The inaccuracies can cause glitches. |
| 7032 | static int64_t in_get_mmap_time_offset() { |
| 7033 | const int32_t kDefaultOffsetMicros = 0; |
| 7034 | int32_t mmap_time_offset_micros = property_get_int32( |
Weiyin Jiang | bd68b4d | 2019-05-17 16:29:50 +0800 | [diff] [blame] | 7035 | "persist.vendor.audio.in_mmap_delay_micros", kDefaultOffsetMicros); |
Phil Burk | e0a86d1 | 2019-02-16 22:28:11 -0800 | [diff] [blame] | 7036 | ALOGI("mmap_time_offset_micros = %d for input", mmap_time_offset_micros); |
| 7037 | return mmap_time_offset_micros * (int64_t)1000; |
| 7038 | } |
| 7039 | |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 7040 | static int in_create_mmap_buffer(const struct audio_stream_in *stream, |
| 7041 | int32_t min_size_frames, |
| 7042 | struct audio_mmap_buffer_info *info) |
| 7043 | { |
| 7044 | struct stream_in *in = (struct stream_in *)stream; |
| 7045 | struct audio_device *adev = in->dev; |
| 7046 | int ret = 0; |
Aniket Kumar Lata | f9f246e | 2017-09-15 15:20:16 -0700 | [diff] [blame] | 7047 | unsigned int offset1 = 0; |
| 7048 | unsigned int frames1 = 0; |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 7049 | const char *step = ""; |
Arun Mirpuri | 5d17087 | 2019-03-26 13:21:31 -0700 | [diff] [blame] | 7050 | uint32_t mmap_size = 0; |
| 7051 | uint32_t buffer_size = 0; |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 7052 | |
| 7053 | pthread_mutex_lock(&adev->lock); |
| 7054 | ALOGV("%s in %p", __func__, in); |
| 7055 | |
Sharad Sangle | c6f3255 | 2018-05-04 16:15:38 +0530 | [diff] [blame] | 7056 | if (CARD_STATUS_OFFLINE == in->card_status|| |
| 7057 | CARD_STATUS_OFFLINE == adev->card_status) { |
| 7058 | ALOGW("in->card_status or adev->card_status offline, try again"); |
| 7059 | ret = -EIO; |
| 7060 | goto exit; |
| 7061 | } |
| 7062 | |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 7063 | if (info == NULL || min_size_frames == 0) { |
| 7064 | ALOGE("%s invalid argument info %p min_size_frames %d", __func__, info, min_size_frames); |
| 7065 | ret = -EINVAL; |
| 7066 | goto exit; |
| 7067 | } |
| 7068 | if (in->usecase != USECASE_AUDIO_RECORD_MMAP || !in->standby) { |
| 7069 | ALOGE("%s: usecase = %d, standby = %d", __func__, in->usecase, in->standby); |
| 7070 | ALOGV("%s in %p", __func__, in); |
| 7071 | ret = -ENOSYS; |
| 7072 | goto exit; |
| 7073 | } |
| 7074 | in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE); |
| 7075 | if (in->pcm_device_id < 0) { |
| 7076 | ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)", |
| 7077 | __func__, in->pcm_device_id, in->usecase); |
| 7078 | ret = -EINVAL; |
| 7079 | goto exit; |
| 7080 | } |
| 7081 | |
| 7082 | adjust_mmap_period_count(&in->config, min_size_frames); |
| 7083 | |
| 7084 | ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d", |
| 7085 | __func__, adev->snd_card, in->pcm_device_id, in->config.channels); |
| 7086 | in->pcm = pcm_open(adev->snd_card, in->pcm_device_id, |
| 7087 | (PCM_IN | PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC), &in->config); |
Satish Babu Patakokila | 54ce83d | 2018-07-06 18:00:37 +0530 | [diff] [blame] | 7088 | if (errno == ENETRESET && !pcm_is_ready(in->pcm)) { |
Sharad Sangle | c6f3255 | 2018-05-04 16:15:38 +0530 | [diff] [blame] | 7089 | ALOGE("%s: pcm_open failed errno:%d\n", __func__, errno); |
| 7090 | in->card_status = CARD_STATUS_OFFLINE; |
| 7091 | adev->card_status = CARD_STATUS_OFFLINE; |
| 7092 | ret = -EIO; |
| 7093 | goto exit; |
| 7094 | } |
| 7095 | |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 7096 | if (in->pcm == NULL || !pcm_is_ready(in->pcm)) { |
| 7097 | step = "open"; |
| 7098 | ret = -ENODEV; |
| 7099 | goto exit; |
| 7100 | } |
| 7101 | |
| 7102 | ret = pcm_mmap_begin(in->pcm, &info->shared_memory_address, &offset1, &frames1); |
| 7103 | if (ret < 0) { |
| 7104 | step = "begin"; |
| 7105 | goto exit; |
| 7106 | } |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 7107 | |
Arun Mirpuri | 5d17087 | 2019-03-26 13:21:31 -0700 | [diff] [blame] | 7108 | info->buffer_size_frames = pcm_get_buffer_size(in->pcm); |
| 7109 | buffer_size = pcm_frames_to_bytes(in->pcm, info->buffer_size_frames); |
| 7110 | info->burst_size_frames = in->config.period_size; |
| 7111 | ret = platform_get_mmap_data_fd(adev->platform, |
| 7112 | in->pcm_device_id, 1 /*capture*/, |
| 7113 | &info->shared_memory_fd, |
| 7114 | &mmap_size); |
| 7115 | if (ret < 0) { |
| 7116 | // Fall back to non exclusive mode |
| 7117 | info->shared_memory_fd = pcm_get_poll_fd(in->pcm); |
| 7118 | } else { |
Phil Burk | d898ba6 | 2019-06-20 12:49:01 -0700 | [diff] [blame] | 7119 | in->mmap_shared_memory_fd = info->shared_memory_fd; // for closing later |
| 7120 | ALOGV("%s: opened mmap_shared_memory_fd = %d", __func__, in->mmap_shared_memory_fd); |
| 7121 | |
Arun Mirpuri | 5d17087 | 2019-03-26 13:21:31 -0700 | [diff] [blame] | 7122 | if (mmap_size < buffer_size) { |
| 7123 | step = "mmap"; |
| 7124 | goto exit; |
| 7125 | } |
| 7126 | // FIXME: indicate exclusive mode support by returning a negative buffer size |
| 7127 | info->buffer_size_frames *= -1; |
| 7128 | } |
| 7129 | |
| 7130 | memset(info->shared_memory_address, 0, buffer_size); |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 7131 | |
| 7132 | ret = pcm_mmap_commit(in->pcm, 0, MMAP_PERIOD_SIZE); |
| 7133 | if (ret < 0) { |
| 7134 | step = "commit"; |
| 7135 | goto exit; |
| 7136 | } |
| 7137 | |
Phil Burk | e0a86d1 | 2019-02-16 22:28:11 -0800 | [diff] [blame] | 7138 | in->mmap_time_offset_nanos = in_get_mmap_time_offset(); |
| 7139 | |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 7140 | in->standby = false; |
| 7141 | ret = 0; |
| 7142 | |
| 7143 | ALOGV("%s: got mmap buffer address %p info->buffer_size_frames %d", |
| 7144 | __func__, info->shared_memory_address, info->buffer_size_frames); |
| 7145 | |
| 7146 | exit: |
| 7147 | if (ret != 0) { |
| 7148 | if (in->pcm == NULL) { |
| 7149 | ALOGE("%s: %s - %d", __func__, step, ret); |
| 7150 | } else { |
| 7151 | ALOGE("%s: %s %s", __func__, step, pcm_get_error(in->pcm)); |
| 7152 | pcm_close(in->pcm); |
| 7153 | in->pcm = NULL; |
| 7154 | } |
| 7155 | } |
| 7156 | pthread_mutex_unlock(&adev->lock); |
| 7157 | return ret; |
| 7158 | } |
| 7159 | |
| 7160 | static int in_get_mmap_position(const struct audio_stream_in *stream, |
| 7161 | struct audio_mmap_position *position) |
| 7162 | { |
| 7163 | struct stream_in *in = (struct stream_in *)stream; |
| 7164 | ALOGVV("%s", __func__); |
| 7165 | if (position == NULL) { |
| 7166 | return -EINVAL; |
| 7167 | } |
| 7168 | if (in->usecase != USECASE_AUDIO_RECORD_MMAP) { |
| 7169 | return -ENOSYS; |
| 7170 | } |
| 7171 | if (in->pcm == NULL) { |
| 7172 | return -ENOSYS; |
| 7173 | } |
| 7174 | struct timespec ts = { 0, 0 }; |
| 7175 | int ret = pcm_mmap_get_hw_ptr(in->pcm, (unsigned int *)&position->position_frames, &ts); |
| 7176 | if (ret < 0) { |
| 7177 | ALOGE("%s: %s", __func__, pcm_get_error(in->pcm)); |
| 7178 | return ret; |
| 7179 | } |
Phil Burk | e0a86d1 | 2019-02-16 22:28:11 -0800 | [diff] [blame] | 7180 | position->time_nanoseconds = ts.tv_sec*1000000000LL + ts.tv_nsec |
| 7181 | + in->mmap_time_offset_nanos; |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 7182 | return 0; |
| 7183 | } |
| 7184 | |
Naresh Tanniru | dcb47c5 | 2018-06-25 16:23:32 +0530 | [diff] [blame] | 7185 | static int in_get_active_microphones(const struct audio_stream_in *stream, |
| 7186 | struct audio_microphone_characteristic_t *mic_array, |
| 7187 | size_t *mic_count) { |
| 7188 | struct stream_in *in = (struct stream_in *)stream; |
| 7189 | struct audio_device *adev = in->dev; |
| 7190 | ALOGVV("%s", __func__); |
| 7191 | |
| 7192 | lock_input_stream(in); |
| 7193 | pthread_mutex_lock(&adev->lock); |
| 7194 | int ret = platform_get_active_microphones(adev->platform, |
| 7195 | audio_channel_count_from_in_mask(in->channel_mask), |
| 7196 | in->usecase, mic_array, mic_count); |
| 7197 | pthread_mutex_unlock(&adev->lock); |
| 7198 | pthread_mutex_unlock(&in->lock); |
| 7199 | |
| 7200 | return ret; |
| 7201 | } |
| 7202 | |
| 7203 | static int adev_get_microphones(const struct audio_hw_device *dev, |
| 7204 | struct audio_microphone_characteristic_t *mic_array, |
| 7205 | size_t *mic_count) { |
| 7206 | struct audio_device *adev = (struct audio_device *)dev; |
| 7207 | ALOGVV("%s", __func__); |
| 7208 | |
| 7209 | pthread_mutex_lock(&adev->lock); |
| 7210 | int ret = platform_get_microphones(adev->platform, mic_array, mic_count); |
| 7211 | pthread_mutex_unlock(&adev->lock); |
| 7212 | |
| 7213 | return ret; |
| 7214 | } |
juyuchen | db308c2 | 2019-01-21 11:57:17 -0700 | [diff] [blame] | 7215 | |
| 7216 | static void in_update_sink_metadata(struct audio_stream_in *stream, |
| 7217 | const struct sink_metadata *sink_metadata) { |
| 7218 | |
| 7219 | if (stream == NULL |
| 7220 | || sink_metadata == NULL |
| 7221 | || sink_metadata->tracks == NULL) { |
| 7222 | return; |
| 7223 | } |
| 7224 | |
| 7225 | int error = 0; |
| 7226 | struct stream_in *in = (struct stream_in *)stream; |
| 7227 | struct audio_device *adev = in->dev; |
| 7228 | audio_devices_t device = AUDIO_DEVICE_NONE; |
| 7229 | |
| 7230 | if (sink_metadata->track_count != 0) |
| 7231 | device = sink_metadata->tracks->dest_device; |
| 7232 | |
| 7233 | lock_input_stream(in); |
| 7234 | pthread_mutex_lock(&adev->lock); |
| 7235 | ALOGV("%s: in->usecase: %d, device: %x", __func__, in->usecase, device); |
| 7236 | |
| 7237 | if (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY |
| 7238 | && device != AUDIO_DEVICE_NONE |
| 7239 | && adev->voice_tx_output != NULL) { |
| 7240 | /* Use the rx device from afe-proxy record to route voice call because |
| 7241 | there is no routing if tx device is on primary hal and rx device |
| 7242 | is on other hal during voice call. */ |
| 7243 | adev->voice_tx_output->devices = device; |
| 7244 | |
| 7245 | if (!voice_is_call_state_active(adev)) { |
| 7246 | if (adev->mode == AUDIO_MODE_IN_CALL) { |
| 7247 | adev->current_call_output = adev->voice_tx_output; |
| 7248 | error = voice_start_call(adev); |
| 7249 | if (error != 0) |
| 7250 | ALOGE("%s: start voice call failed %d", __func__, error); |
| 7251 | } |
| 7252 | } else { |
| 7253 | adev->current_call_output = adev->voice_tx_output; |
| 7254 | voice_update_devices_for_all_voice_usecases(adev); |
| 7255 | } |
| 7256 | } |
| 7257 | |
| 7258 | pthread_mutex_unlock(&adev->lock); |
| 7259 | pthread_mutex_unlock(&in->lock); |
| 7260 | } |
| 7261 | |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 7262 | int adev_open_output_stream(struct audio_hw_device *dev, |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 7263 | audio_io_handle_t handle, |
| 7264 | audio_devices_t devices, |
| 7265 | audio_output_flags_t flags, |
| 7266 | struct audio_config *config, |
| 7267 | struct audio_stream_out **stream_out, |
Derek Chen | f6318be | 2017-06-12 17:16:24 -0400 | [diff] [blame] | 7268 | const char *address) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 7269 | { |
| 7270 | struct audio_device *adev = (struct audio_device *)dev; |
| 7271 | struct stream_out *out; |
Gangadhar S | b021034 | 2019-02-22 17:39:41 +0530 | [diff] [blame] | 7272 | int ret = 0, ip_hdlr_stream = 0, ip_hdlr_dev = 0; |
Subhash Chandra Bose Naripeddy | 19dc03b | 2014-03-10 14:43:05 -0700 | [diff] [blame] | 7273 | audio_format_t format; |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 7274 | struct adsp_hdlr_stream_cfg hdlr_stream_cfg; |
Manish Dewangan | 21a850a | 2017-08-14 12:03:55 +0530 | [diff] [blame] | 7275 | bool is_direct_passthough = false; |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 7276 | bool is_hdmi = devices & AUDIO_DEVICE_OUT_AUX_DIGITAL; |
| 7277 | bool is_usb_dev = audio_is_usb_out_device(devices) && |
| 7278 | (devices != AUDIO_DEVICE_OUT_USB_ACCESSORY); |
| 7279 | bool direct_dev = is_hdmi || is_usb_dev; |
Arun Mirpuri | b1bec9c | 2019-01-29 16:42:45 -0800 | [diff] [blame] | 7280 | bool use_db_as_primary = |
vivek mehta | ae1018c | 2019-05-09 12:19:57 -0700 | [diff] [blame] | 7281 | property_get_bool("vendor.audio.feature.deepbuffer_as_primary.enable", |
| 7282 | false); |
Vignesh Kulothungan | a692727 | 2019-02-20 15:17:07 -0800 | [diff] [blame] | 7283 | bool force_haptic_path = |
| 7284 | property_get_bool("vendor.audio.test_haptic", false); |
Aniket Kumar Lata | 23d8cbf | 2019-04-10 19:54:46 -0700 | [diff] [blame] | 7285 | bool is_voip_rx = flags & AUDIO_OUTPUT_FLAG_VOIP_RX; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 7286 | |
kunleiz | dff872d | 2018-08-20 14:40:33 +0800 | [diff] [blame] | 7287 | if (is_usb_dev && (!audio_extn_usb_connected(NULL))) { |
kunleiz | d6a9e0c | 2018-07-30 15:38:52 +0800 | [diff] [blame] | 7288 | is_usb_dev = false; |
| 7289 | devices = AUDIO_DEVICE_OUT_SPEAKER; |
| 7290 | ALOGW("%s: ignore set device to non existing USB card, use output device(%#x)", |
| 7291 | __func__, devices); |
| 7292 | } |
| 7293 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 7294 | *stream_out = NULL; |
Naresh Tanniru | 8065983 | 2014-06-04 18:17:56 +0530 | [diff] [blame] | 7295 | |
Rahul Sharma | 9977098 | 2019-03-06 17:05:26 +0530 | [diff] [blame] | 7296 | pthread_mutex_lock(&adev->lock); |
| 7297 | if (out_get_stream(adev, handle) != NULL) { |
| 7298 | ALOGW("%s, output stream already opened", __func__); |
| 7299 | ret = -EEXIST; |
| 7300 | } |
| 7301 | pthread_mutex_unlock(&adev->lock); |
| 7302 | if (ret) |
| 7303 | return ret; |
| 7304 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 7305 | out = (struct stream_out *)calloc(1, sizeof(struct stream_out)); |
| 7306 | |
Mingming Yin | 3a941d4 | 2016-02-17 18:08:05 -0800 | [diff] [blame] | 7307 | ALOGD("%s: enter: format(%#x) sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)\ |
Derek Chen | f6318be | 2017-06-12 17:16:24 -0400 | [diff] [blame] | 7308 | stream_handle(%p) address(%s)", __func__, config->format, config->sample_rate, config->channel_mask, |
| 7309 | devices, flags, &out->stream, address); |
Sidipotu Ashok | f43018c | 2014-05-02 16:21:50 +0530 | [diff] [blame] | 7310 | |
| 7311 | |
Haynes Mathew George | b9012ab | 2013-12-10 13:44:56 -0800 | [diff] [blame] | 7312 | if (!out) { |
| 7313 | return -ENOMEM; |
| 7314 | } |
| 7315 | |
Haynes Mathew George | 204045b | 2015-02-25 20:32:03 -0800 | [diff] [blame] | 7316 | pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL); |
Shiv Maliyappanahalli | 736d4ce | 2015-09-28 15:23:06 -0700 | [diff] [blame] | 7317 | pthread_mutex_init(&out->pre_lock, (const pthread_mutexattr_t *) NULL); |
Chaithanya Krishna Bacharaju | 69d2e4c | 2017-05-26 18:22:46 +0530 | [diff] [blame] | 7318 | pthread_mutex_init(&out->compr_mute_lock, (const pthread_mutexattr_t *) NULL); |
Zhou Song | 48453a0 | 2018-01-10 17:50:59 +0800 | [diff] [blame] | 7319 | pthread_mutex_init(&out->position_query_lock, (const pthread_mutexattr_t *) NULL); |
Haynes Mathew George | 204045b | 2015-02-25 20:32:03 -0800 | [diff] [blame] | 7320 | pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL); |
| 7321 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 7322 | if (devices == AUDIO_DEVICE_NONE) |
| 7323 | devices = AUDIO_DEVICE_OUT_SPEAKER; |
| 7324 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 7325 | out->flags = flags; |
| 7326 | out->devices = devices; |
Haynes Mathew George | 47cd4cb | 2013-07-19 11:58:50 -0700 | [diff] [blame] | 7327 | out->dev = adev; |
Aalique Grahame | 65780b5 | 2017-09-27 14:59:56 -0700 | [diff] [blame] | 7328 | out->hal_op_format = out->hal_ip_format = format = out->format = config->format; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 7329 | out->sample_rate = config->sample_rate; |
Sachin Mohan Gadag | 3d09acd | 2017-06-19 12:43:44 +0530 | [diff] [blame] | 7330 | out->channel_mask = config->channel_mask; |
Ramjee Singh | 5857aeb | 2017-08-03 19:18:50 +0530 | [diff] [blame] | 7331 | if (out->channel_mask == AUDIO_CHANNEL_NONE) |
| 7332 | out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO; |
| 7333 | else |
| 7334 | out->supported_channel_masks[0] = out->channel_mask; |
Eric Laurent | c4aef75 | 2013-09-12 17:45:53 -0700 | [diff] [blame] | 7335 | out->handle = handle; |
Mingming Yin | 3ee55c6 | 2014-08-04 14:23:35 -0700 | [diff] [blame] | 7336 | out->bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH; |
Alexy Joseph | aa54c87 | 2014-12-03 02:46:47 -0800 | [diff] [blame] | 7337 | out->non_blocking = 0; |
Ashish Jain | 83a6cc2 | 2016-06-28 14:34:17 +0530 | [diff] [blame] | 7338 | out->convert_buffer = NULL; |
Ashish Jain | 1b9b30c | 2017-05-18 20:57:40 +0530 | [diff] [blame] | 7339 | out->started = 0; |
Chaithanya Krishna Bacharaju | 69d2e4c | 2017-05-26 18:22:46 +0530 | [diff] [blame] | 7340 | out->a2dp_compress_mute = false; |
Aniket Kumar Lata | 932f487 | 2017-11-06 18:29:44 -0800 | [diff] [blame] | 7341 | out->hal_output_suspend_supported = 0; |
| 7342 | out->dynamic_pm_qos_config_supported = 0; |
Surendar Karka | f51b584 | 2018-04-26 11:28:38 +0530 | [diff] [blame] | 7343 | out->set_dual_mono = false; |
Manisha Agarwal | 7b3e377 | 2019-02-20 14:33:45 +0530 | [diff] [blame] | 7344 | out->prev_card_status_offline = false; |
Dhanalakshmi Siddani | 20628c8 | 2019-01-27 17:31:09 +0530 | [diff] [blame] | 7345 | out->pspd_coeff_sent = false; |
Phil Burk | d898ba6 | 2019-06-20 12:49:01 -0700 | [diff] [blame] | 7346 | out->mmap_shared_memory_fd = -1; // not open |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 7347 | |
Nikhil Laturkar | 26b690b | 2017-07-25 11:06:14 +0530 | [diff] [blame] | 7348 | if ((flags & AUDIO_OUTPUT_FLAG_BD) && |
Satish Babu Patakokila | 37e7c48 | 2018-02-02 11:50:06 +0530 | [diff] [blame] | 7349 | (property_get_bool("vendor.audio.matrix.limiter.enable", false))) |
Ben Romberger | 6c4d381 | 2017-06-13 17:46:45 -0700 | [diff] [blame] | 7350 | platform_set_device_params(out, DEVICE_PARAM_LIMITER_ID, 1); |
| 7351 | |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 7352 | if (direct_dev && |
| 7353 | (audio_is_linear_pcm(out->format) || |
| 7354 | config->format == AUDIO_FORMAT_DEFAULT) && |
| 7355 | out->flags == AUDIO_OUTPUT_FLAG_NONE) { |
| 7356 | audio_format_t req_format = config->format; |
| 7357 | audio_channel_mask_t req_channel_mask = config->channel_mask; |
| 7358 | uint32_t req_sample_rate = config->sample_rate; |
| 7359 | |
| 7360 | pthread_mutex_lock(&adev->lock); |
| 7361 | if (is_hdmi) { |
| 7362 | ALOGV("AUDIO_DEVICE_OUT_AUX_DIGITAL and DIRECT|OFFLOAD, check hdmi caps"); |
| 7363 | ret = read_hdmi_sink_caps(out); |
| 7364 | if (config->sample_rate == 0) |
| 7365 | config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE; |
| 7366 | if (config->channel_mask == AUDIO_CHANNEL_NONE) |
| 7367 | config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1; |
| 7368 | if (config->format == AUDIO_FORMAT_DEFAULT) |
| 7369 | config->format = AUDIO_FORMAT_PCM_16_BIT; |
| 7370 | } else if (is_usb_dev) { |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 7371 | ret = read_usb_sup_params_and_compare(true /*is_playback*/, |
| 7372 | &config->format, |
| 7373 | &out->supported_formats[0], |
| 7374 | MAX_SUPPORTED_FORMATS, |
| 7375 | &config->channel_mask, |
| 7376 | &out->supported_channel_masks[0], |
| 7377 | MAX_SUPPORTED_CHANNEL_MASKS, |
| 7378 | &config->sample_rate, |
| 7379 | &out->supported_sample_rates[0], |
| 7380 | MAX_SUPPORTED_SAMPLE_RATES); |
| 7381 | ALOGV("plugged dev USB ret %d", ret); |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 7382 | } |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 7383 | |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 7384 | pthread_mutex_unlock(&adev->lock); |
| 7385 | if (ret != 0) { |
Mingming Yin | 3a941d4 | 2016-02-17 18:08:05 -0800 | [diff] [blame] | 7386 | if (ret == -ENOSYS) { |
| 7387 | /* ignore and go with default */ |
| 7388 | ret = 0; |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 7389 | } |
| 7390 | // For MMAP NO IRQ, allow conversions in ADSP |
| 7391 | else if (is_hdmi || (flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) == 0) |
| 7392 | goto error_open; |
| 7393 | else { |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 7394 | ALOGE("error reading direct dev sink caps"); |
Mingming Yin | 3a941d4 | 2016-02-17 18:08:05 -0800 | [diff] [blame] | 7395 | goto error_open; |
| 7396 | } |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 7397 | |
| 7398 | if (req_sample_rate != 0 && config->sample_rate != req_sample_rate) |
| 7399 | config->sample_rate = req_sample_rate; |
| 7400 | if (req_channel_mask != AUDIO_CHANNEL_NONE && config->channel_mask != req_channel_mask) |
| 7401 | config->channel_mask = req_channel_mask; |
| 7402 | if (req_format != AUDIO_FORMAT_DEFAULT && config->format != req_format) |
| 7403 | config->format = req_format; |
Mingming Yin | 3a941d4 | 2016-02-17 18:08:05 -0800 | [diff] [blame] | 7404 | } |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 7405 | |
| 7406 | out->sample_rate = config->sample_rate; |
| 7407 | out->channel_mask = config->channel_mask; |
| 7408 | out->format = config->format; |
| 7409 | if (is_hdmi) { |
| 7410 | out->usecase = USECASE_AUDIO_PLAYBACK_HIFI; |
| 7411 | out->config = pcm_config_hdmi_multi; |
| 7412 | } else if (flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) { |
| 7413 | out->usecase = USECASE_AUDIO_PLAYBACK_MMAP; |
| 7414 | out->config = pcm_config_mmap_playback; |
| 7415 | out->stream.start = out_start; |
| 7416 | out->stream.stop = out_stop; |
| 7417 | out->stream.create_mmap_buffer = out_create_mmap_buffer; |
| 7418 | out->stream.get_mmap_position = out_get_mmap_position; |
| 7419 | } else { |
| 7420 | out->usecase = USECASE_AUDIO_PLAYBACK_HIFI; |
| 7421 | out->config = pcm_config_hifi; |
| 7422 | } |
| 7423 | |
| 7424 | out->config.rate = out->sample_rate; |
| 7425 | out->config.channels = audio_channel_count_from_out_mask(out->channel_mask); |
| 7426 | if (is_hdmi) { |
| 7427 | out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * |
| 7428 | audio_bytes_per_sample(out->format)); |
| 7429 | } |
| 7430 | out->config.format = pcm_format_from_audio_format(out->format); |
Mingming Yin | 3a941d4 | 2016-02-17 18:08:05 -0800 | [diff] [blame] | 7431 | } |
| 7432 | |
Derek Chen | f6318be | 2017-06-12 17:16:24 -0400 | [diff] [blame] | 7433 | /* validate bus device address */ |
| 7434 | if (out->devices & AUDIO_DEVICE_OUT_BUS) { |
| 7435 | /* extract car audio stream index */ |
| 7436 | out->car_audio_stream = |
| 7437 | audio_extn_auto_hal_get_car_audio_stream_from_address(address); |
| 7438 | if (out->car_audio_stream < 0) { |
| 7439 | ALOGE("%s: invalid car audio stream %x", |
| 7440 | __func__, out->car_audio_stream); |
| 7441 | ret = -EINVAL; |
| 7442 | goto error_open; |
| 7443 | } |
| 7444 | /* save car audio stream and address for bus device */ |
| 7445 | strlcpy(out->address, address, AUDIO_DEVICE_MAX_ADDRESS_LEN); |
| 7446 | ALOGV("%s: address %s, car_audio_stream %x", |
| 7447 | __func__, out->address, out->car_audio_stream); |
| 7448 | } |
| 7449 | |
Arun Mirpuri | b1bec9c | 2019-01-29 16:42:45 -0800 | [diff] [blame] | 7450 | /* Check for VOIP usecase */ |
Aniket Kumar Lata | 23d8cbf | 2019-04-10 19:54:46 -0700 | [diff] [blame] | 7451 | if (is_voip_rx) { |
Arun Mirpuri | b1bec9c | 2019-01-29 16:42:45 -0800 | [diff] [blame] | 7452 | if (!voice_extn_is_compress_voip_supported()) { |
| 7453 | if (out->sample_rate == 8000 || out->sample_rate == 16000 || |
| 7454 | out->sample_rate == 32000 || out->sample_rate == 48000) { |
Aniket Kumar Lata | 4f9a2a5 | 2019-05-09 18:44:09 -0700 | [diff] [blame] | 7455 | out->channel_mask = audio_extn_utils_is_vendor_enhanced_fwk() ? |
| 7456 | AUDIO_CHANNEL_OUT_MONO : AUDIO_CHANNEL_OUT_STEREO; |
Arun Mirpuri | b1bec9c | 2019-01-29 16:42:45 -0800 | [diff] [blame] | 7457 | out->usecase = USECASE_AUDIO_PLAYBACK_VOIP; |
| 7458 | out->format = AUDIO_FORMAT_PCM_16_BIT; |
Aniket Kumar Lata | 8426d1f | 2019-06-10 15:25:53 -0700 | [diff] [blame] | 7459 | out->volume_l = INVALID_OUT_VOLUME; |
| 7460 | out->volume_r = INVALID_OUT_VOLUME; |
Vikram Panduranga | 93f080e | 2017-06-07 18:16:14 -0700 | [diff] [blame] | 7461 | |
Arun Mirpuri | b1bec9c | 2019-01-29 16:42:45 -0800 | [diff] [blame] | 7462 | out->config = default_pcm_config_voip_copp; |
Arun Mirpuri | b1bec9c | 2019-01-29 16:42:45 -0800 | [diff] [blame] | 7463 | out->config.rate = out->sample_rate; |
Aniket Kumar Lata | 1e344f2 | 2019-06-19 17:09:01 -0700 | [diff] [blame] | 7464 | uint32_t channel_count = |
| 7465 | audio_channel_count_from_out_mask(out->channel_mask); |
| 7466 | uint32_t buffer_size = get_stream_buffer_size(DEFAULT_VOIP_BUF_DURATION_MS, |
| 7467 | out->sample_rate, out->format, |
| 7468 | channel_count, false); |
| 7469 | uint32_t frame_size = audio_bytes_per_sample(out->format) * channel_count; |
| 7470 | if (frame_size != 0) |
| 7471 | out->config.period_size = buffer_size / frame_size; |
| 7472 | else |
| 7473 | ALOGW("%s: frame size is 0 for format %#x", __func__, out->format); |
Arun Mirpuri | b1bec9c | 2019-01-29 16:42:45 -0800 | [diff] [blame] | 7474 | } |
| 7475 | } else { |
| 7476 | if ((out->dev->mode == AUDIO_MODE_IN_COMMUNICATION || |
| 7477 | voice_extn_compress_voip_is_active(out->dev)) && |
| 7478 | (voice_extn_compress_voip_is_config_supported(config))) { |
| 7479 | ret = voice_extn_compress_voip_open_output_stream(out); |
| 7480 | if (ret != 0) { |
| 7481 | ALOGE("%s: Compress voip output cannot be opened, error:%d", |
| 7482 | __func__, ret); |
| 7483 | goto error_open; |
| 7484 | } |
Sujin Panicker | 1902726 | 2019-09-16 18:28:06 +0530 | [diff] [blame] | 7485 | } else { |
| 7486 | out->usecase = GET_USECASE_AUDIO_PLAYBACK_PRIMARY(use_db_as_primary); |
| 7487 | out->config = GET_PCM_CONFIG_AUDIO_PLAYBACK_PRIMARY(use_db_as_primary); |
Arun Mirpuri | b1bec9c | 2019-01-29 16:42:45 -0800 | [diff] [blame] | 7488 | } |
Narsinga Rao Chella | 05573b7 | 2013-11-15 15:21:40 -0800 | [diff] [blame] | 7489 | } |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 7490 | } else if (audio_is_linear_pcm(out->format) && |
| 7491 | out->flags == AUDIO_OUTPUT_FLAG_NONE && is_usb_dev) { |
| 7492 | out->channel_mask = config->channel_mask; |
| 7493 | out->sample_rate = config->sample_rate; |
| 7494 | out->format = config->format; |
| 7495 | out->usecase = USECASE_AUDIO_PLAYBACK_HIFI; |
| 7496 | // does this change? |
| 7497 | out->config = is_hdmi ? pcm_config_hdmi_multi : pcm_config_hifi; |
| 7498 | out->config.rate = config->sample_rate; |
| 7499 | out->config.channels = audio_channel_count_from_out_mask(out->channel_mask); |
| 7500 | out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * |
| 7501 | audio_bytes_per_sample(config->format)); |
| 7502 | out->config.format = pcm_format_from_audio_format(out->format); |
vivek mehta | 0ea887a | 2015-08-26 14:01:20 -0700 | [diff] [blame] | 7503 | } else if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) || |
Dhananjay Kumar | ac34158 | 2017-02-23 23:42:25 +0530 | [diff] [blame] | 7504 | (out->flags == AUDIO_OUTPUT_FLAG_DIRECT)) { |
Dhananjay Kumar | e6293dd | 2017-05-25 17:25:30 +0530 | [diff] [blame] | 7505 | pthread_mutex_lock(&adev->lock); |
| 7506 | bool offline = (adev->card_status == CARD_STATUS_OFFLINE); |
| 7507 | pthread_mutex_unlock(&adev->lock); |
| 7508 | |
| 7509 | // reject offload during card offline to allow |
| 7510 | // fallback to s/w paths |
| 7511 | if (offline) { |
| 7512 | ret = -ENODEV; |
| 7513 | goto error_open; |
| 7514 | } |
vivek mehta | 0ea887a | 2015-08-26 14:01:20 -0700 | [diff] [blame] | 7515 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 7516 | if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version || |
| 7517 | config->offload_info.size != AUDIO_INFO_INITIALIZER.size) { |
| 7518 | ALOGE("%s: Unsupported Offload information", __func__); |
| 7519 | ret = -EINVAL; |
| 7520 | goto error_open; |
| 7521 | } |
Pradnya Chaphekar | 80a8cfb | 2014-10-20 16:17:01 -0700 | [diff] [blame] | 7522 | |
Atul Khare | 3fa6e54 | 2017-08-09 00:56:17 +0530 | [diff] [blame] | 7523 | if (config->offload_info.format == 0) |
| 7524 | config->offload_info.format = config->format; |
| 7525 | if (config->offload_info.sample_rate == 0) |
| 7526 | config->offload_info.sample_rate = config->sample_rate; |
Pradnya Chaphekar | 80a8cfb | 2014-10-20 16:17:01 -0700 | [diff] [blame] | 7527 | |
Mingming Yin | 9031010 | 2013-11-13 16:57:00 -0800 | [diff] [blame] | 7528 | if (!is_supported_format(config->offload_info.format) && |
Satish Babu Patakokila | 1caa1b7 | 2016-05-24 13:47:08 +0530 | [diff] [blame] | 7529 | !audio_extn_passthru_is_supported_format(config->offload_info.format)) { |
vivek mehta | 0ea887a | 2015-08-26 14:01:20 -0700 | [diff] [blame] | 7530 | ALOGE("%s: Unsupported audio format %x " , __func__, config->offload_info.format); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 7531 | ret = -EINVAL; |
| 7532 | goto error_open; |
| 7533 | } |
| 7534 | |
Ben Romberger | 0f8c87b | 2017-05-24 17:41:11 -0700 | [diff] [blame] | 7535 | /* TrueHD only supported for 48k multiples (48k, 96k, 192k) */ |
| 7536 | if ((config->offload_info.format == AUDIO_FORMAT_DOLBY_TRUEHD) && |
| 7537 | (audio_extn_passthru_is_passthrough_stream(out)) && |
| 7538 | !((config->sample_rate == 48000) || |
| 7539 | (config->sample_rate == 96000) || |
| 7540 | (config->sample_rate == 192000))) { |
| 7541 | ALOGE("%s: Unsupported sample rate %d for audio format %x", |
| 7542 | __func__, config->sample_rate, config->offload_info.format); |
| 7543 | ret = -EINVAL; |
| 7544 | goto error_open; |
| 7545 | } |
| 7546 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 7547 | out->compr_config.codec = (struct snd_codec *) |
| 7548 | calloc(1, sizeof(struct snd_codec)); |
| 7549 | |
Haynes Mathew George | b51ceb1 | 2014-06-30 13:56:18 -0700 | [diff] [blame] | 7550 | if (!out->compr_config.codec) { |
| 7551 | ret = -ENOMEM; |
| 7552 | goto error_open; |
| 7553 | } |
| 7554 | |
Dhananjay Kumar | ac34158 | 2017-02-23 23:42:25 +0530 | [diff] [blame] | 7555 | out->stream.pause = out_pause; |
| 7556 | out->stream.resume = out_resume; |
| 7557 | out->stream.flush = out_flush; |
Ashish Jain | 4847e9d | 2017-08-17 19:16:57 +0530 | [diff] [blame] | 7558 | out->stream.set_callback = out_set_callback; |
Dhananjay Kumar | ac34158 | 2017-02-23 23:42:25 +0530 | [diff] [blame] | 7559 | if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { |
Mingming Yin | 21d6047 | 2015-09-30 13:56:25 -0700 | [diff] [blame] | 7560 | out->stream.drain = out_drain; |
Dhananjay Kumar | ac34158 | 2017-02-23 23:42:25 +0530 | [diff] [blame] | 7561 | out->usecase = get_offload_usecase(adev, true /* is_compress */); |
vivek mehta | 446c396 | 2015-09-14 10:57:35 -0700 | [diff] [blame] | 7562 | ALOGV("Compress Offload usecase .. usecase selected %d", out->usecase); |
Dhananjay Kumar | ac34158 | 2017-02-23 23:42:25 +0530 | [diff] [blame] | 7563 | } else { |
| 7564 | out->usecase = get_offload_usecase(adev, false /* is_compress */); |
| 7565 | ALOGV("non-offload DIRECT_usecase ... usecase selected %d ", out->usecase); |
vivek mehta | 0ea887a | 2015-08-26 14:01:20 -0700 | [diff] [blame] | 7566 | } |
vivek mehta | 446c396 | 2015-09-14 10:57:35 -0700 | [diff] [blame] | 7567 | |
Deeraj Soman | fa377bf | 2019-02-06 12:57:59 +0530 | [diff] [blame] | 7568 | if (out->flags & AUDIO_OUTPUT_FLAG_FAST) { |
| 7569 | ALOGD("%s: Setting latency mode to true", __func__); |
| 7570 | out->compr_config.codec->flags |= audio_extn_utils_get_perf_mode_flag(); |
| 7571 | } |
| 7572 | |
vivek mehta | 446c396 | 2015-09-14 10:57:35 -0700 | [diff] [blame] | 7573 | if (out->usecase == USECASE_INVALID) { |
Mingming Yin | 3a941d4 | 2016-02-17 18:08:05 -0800 | [diff] [blame] | 7574 | if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL && |
| 7575 | config->format == 0 && config->sample_rate == 0 && |
| 7576 | config->channel_mask == 0) { |
Mingming Yin | 2185465 | 2016-04-13 11:54:02 -0700 | [diff] [blame] | 7577 | ALOGI("%s dummy open to query sink capability",__func__); |
Mingming Yin | 3a941d4 | 2016-02-17 18:08:05 -0800 | [diff] [blame] | 7578 | out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD; |
| 7579 | } else { |
| 7580 | ALOGE("%s, Max allowed OFFLOAD usecase reached ... ", __func__); |
| 7581 | ret = -EEXIST; |
| 7582 | goto error_open; |
| 7583 | } |
vivek mehta | 446c396 | 2015-09-14 10:57:35 -0700 | [diff] [blame] | 7584 | } |
| 7585 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 7586 | if (config->offload_info.channel_mask) |
| 7587 | out->channel_mask = config->offload_info.channel_mask; |
ApurupaPattapu | c6a3a9e | 2014-01-10 14:46:02 -0800 | [diff] [blame] | 7588 | else if (config->channel_mask) { |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 7589 | out->channel_mask = config->channel_mask; |
ApurupaPattapu | c6a3a9e | 2014-01-10 14:46:02 -0800 | [diff] [blame] | 7590 | config->offload_info.channel_mask = config->channel_mask; |
Haynes Mathew George | a99f753 | 2016-08-24 16:01:21 -0700 | [diff] [blame] | 7591 | } else { |
Dhananjay Kumar | ac34158 | 2017-02-23 23:42:25 +0530 | [diff] [blame] | 7592 | ALOGE("out->channel_mask not set for OFFLOAD/DIRECT usecase"); |
Haynes Mathew George | a99f753 | 2016-08-24 16:01:21 -0700 | [diff] [blame] | 7593 | ret = -EINVAL; |
| 7594 | goto error_open; |
ApurupaPattapu | c6a3a9e | 2014-01-10 14:46:02 -0800 | [diff] [blame] | 7595 | } |
Haynes Mathew George | a99f753 | 2016-08-24 16:01:21 -0700 | [diff] [blame] | 7596 | |
Subhash Chandra Bose Naripeddy | 19dc03b | 2014-03-10 14:43:05 -0700 | [diff] [blame] | 7597 | format = out->format = config->offload_info.format; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 7598 | out->sample_rate = config->offload_info.sample_rate; |
| 7599 | |
Mingming Yin | 3ee55c6 | 2014-08-04 14:23:35 -0700 | [diff] [blame] | 7600 | out->bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 7601 | |
Satish Babu Patakokila | 1caa1b7 | 2016-05-24 13:47:08 +0530 | [diff] [blame] | 7602 | out->compr_config.codec->id = get_snd_codec_id(config->offload_info.format); |
Satish Babu Patakokila | 5933e97 | 2017-08-24 12:22:08 +0530 | [diff] [blame] | 7603 | if (audio_extn_utils_is_dolby_format(config->offload_info.format)) { |
Satish Babu Patakokila | 1caa1b7 | 2016-05-24 13:47:08 +0530 | [diff] [blame] | 7604 | audio_extn_dolby_send_ddp_endp_params(adev); |
| 7605 | audio_extn_dolby_set_dmid(adev); |
| 7606 | } |
vivek mehta | 0ea887a | 2015-08-26 14:01:20 -0700 | [diff] [blame] | 7607 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 7608 | out->compr_config.codec->sample_rate = |
Ravi Kumar Alamanda | b91bff3 | 2014-11-14 12:05:54 -0800 | [diff] [blame] | 7609 | config->offload_info.sample_rate; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 7610 | out->compr_config.codec->bit_rate = |
| 7611 | config->offload_info.bit_rate; |
| 7612 | out->compr_config.codec->ch_in = |
Dhanalakshmi Siddani | a15c679 | 2016-08-10 15:33:53 +0530 | [diff] [blame] | 7613 | audio_channel_count_from_out_mask(out->channel_mask); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 7614 | out->compr_config.codec->ch_out = out->compr_config.codec->ch_in; |
Satish Babu Patakokila | a395a9e | 2016-11-01 12:18:49 +0530 | [diff] [blame] | 7615 | /* Update bit width only for non passthrough usecases. |
| 7616 | * For passthrough usecases, the output will always be opened @16 bit |
| 7617 | */ |
| 7618 | if (!audio_extn_passthru_is_passthrough_stream(out)) |
| 7619 | out->bit_width = AUDIO_OUTPUT_BIT_WIDTH; |
Naresh Tanniru | ee3499a | 2017-01-05 14:05:35 +0530 | [diff] [blame] | 7620 | |
| 7621 | if (out->flags & AUDIO_OUTPUT_FLAG_TIMESTAMP) |
| 7622 | out->compr_config.codec->flags |= COMPRESSED_TIMESTAMP_FLAG; |
| 7623 | ALOGVV("%s : out->compr_config.codec->flags -> (%#x) ", __func__, out->compr_config.codec->flags); |
| 7624 | |
Pradnya Chaphekar | 80a8cfb | 2014-10-20 16:17:01 -0700 | [diff] [blame] | 7625 | /*TODO: Do we need to change it for passthrough */ |
| 7626 | out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_RAW; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 7627 | |
Manish Dewangan | a6fc544 | 2015-08-24 20:30:31 +0530 | [diff] [blame] | 7628 | if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC) |
| 7629 | out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_RAW; |
Arun Kumar Dasari | 3b17418 | 2016-12-27 13:01:14 +0530 | [diff] [blame] | 7630 | else if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_ADTS) |
Manish Dewangan | a6fc544 | 2015-08-24 20:30:31 +0530 | [diff] [blame] | 7631 | out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_MP4ADTS; |
Arun Kumar Dasari | 3b17418 | 2016-12-27 13:01:14 +0530 | [diff] [blame] | 7632 | else if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC_LATM) |
| 7633 | out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_MP4LATM; |
Ashish Jain | f1eaa58 | 2016-05-23 20:54:24 +0530 | [diff] [blame] | 7634 | |
| 7635 | if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) == |
| 7636 | AUDIO_FORMAT_PCM) { |
| 7637 | |
| 7638 | /*Based on platform support, configure appropriate alsa format for corresponding |
| 7639 | *hal input format. |
| 7640 | */ |
| 7641 | out->compr_config.codec->format = hal_format_to_alsa( |
| 7642 | config->offload_info.format); |
| 7643 | |
Ashish Jain | 83a6cc2 | 2016-06-28 14:34:17 +0530 | [diff] [blame] | 7644 | out->hal_op_format = alsa_format_to_hal( |
Ashish Jain | f1eaa58 | 2016-05-23 20:54:24 +0530 | [diff] [blame] | 7645 | out->compr_config.codec->format); |
Ashish Jain | 83a6cc2 | 2016-06-28 14:34:17 +0530 | [diff] [blame] | 7646 | out->hal_ip_format = out->format; |
Ashish Jain | f1eaa58 | 2016-05-23 20:54:24 +0530 | [diff] [blame] | 7647 | |
Dhananjay Kumar | ac34158 | 2017-02-23 23:42:25 +0530 | [diff] [blame] | 7648 | /*for direct non-compress playback populate bit_width based on selected alsa format as |
Ashish Jain | f1eaa58 | 2016-05-23 20:54:24 +0530 | [diff] [blame] | 7649 | *hal input format and alsa format might differ based on platform support. |
| 7650 | */ |
| 7651 | out->bit_width = audio_bytes_per_sample( |
Ashish Jain | 83a6cc2 | 2016-06-28 14:34:17 +0530 | [diff] [blame] | 7652 | out->hal_op_format) << 3; |
Ashish Jain | f1eaa58 | 2016-05-23 20:54:24 +0530 | [diff] [blame] | 7653 | |
| 7654 | out->compr_config.fragments = DIRECT_PCM_NUM_FRAGMENTS; |
| 7655 | |
Deeraj Soman | 65358ab | 2019-02-07 15:40:49 +0530 | [diff] [blame] | 7656 | if ((config->offload_info.duration_us >= MIN_OFFLOAD_BUFFER_DURATION_MS * 1000) && |
| 7657 | (config->offload_info.duration_us <= MAX_OFFLOAD_BUFFER_DURATION_MS * 1000)) |
| 7658 | out->info.duration_us = (int64_t)config->offload_info.duration_us; |
| 7659 | |
Ashish Jain | f1eaa58 | 2016-05-23 20:54:24 +0530 | [diff] [blame] | 7660 | /* Check if alsa session is configured with the same format as HAL input format, |
| 7661 | * if not then derive correct fragment size needed to accomodate the |
| 7662 | * conversion of HAL input format to alsa format. |
| 7663 | */ |
| 7664 | audio_extn_utils_update_direct_pcm_fragment_size(out); |
| 7665 | |
| 7666 | /*if hal input and output fragment size is different this indicates HAL input format is |
| 7667 | *not same as the alsa format |
| 7668 | */ |
Ashish Jain | 83a6cc2 | 2016-06-28 14:34:17 +0530 | [diff] [blame] | 7669 | if (out->hal_fragment_size != out->compr_config.fragment_size) { |
Ashish Jain | f1eaa58 | 2016-05-23 20:54:24 +0530 | [diff] [blame] | 7670 | /*Allocate a buffer to convert input data to the alsa configured format. |
| 7671 | *size of convert buffer is equal to the size required to hold one fragment size |
| 7672 | *worth of pcm data, this is because flinger does not write more than fragment_size |
| 7673 | */ |
Ashish Jain | 83a6cc2 | 2016-06-28 14:34:17 +0530 | [diff] [blame] | 7674 | out->convert_buffer = calloc(1,out->compr_config.fragment_size); |
| 7675 | if (out->convert_buffer == NULL){ |
Ashish Jain | f1eaa58 | 2016-05-23 20:54:24 +0530 | [diff] [blame] | 7676 | ALOGE("Allocation failed for convert buffer for size %d", out->compr_config.fragment_size); |
| 7677 | ret = -ENOMEM; |
| 7678 | goto error_open; |
| 7679 | } |
| 7680 | } |
| 7681 | } else if (audio_extn_passthru_is_passthrough_stream(out)) { |
| 7682 | out->compr_config.fragment_size = |
| 7683 | audio_extn_passthru_get_buffer_size(&config->offload_info); |
| 7684 | out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS; |
| 7685 | } else { |
| 7686 | out->compr_config.fragment_size = |
| 7687 | platform_get_compress_offload_buffer_size(&config->offload_info); |
| 7688 | out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS; |
| 7689 | } |
Mingming Yin | 3ee55c6 | 2014-08-04 14:23:35 -0700 | [diff] [blame] | 7690 | |
Naresh Tanniru | ee3499a | 2017-01-05 14:05:35 +0530 | [diff] [blame] | 7691 | if (out->flags & AUDIO_OUTPUT_FLAG_TIMESTAMP) { |
| 7692 | out->compr_config.fragment_size += sizeof(struct snd_codec_metadata); |
| 7693 | } |
Amit Shekhar | 6f461b1 | 2014-08-01 14:52:58 -0700 | [diff] [blame] | 7694 | if (config->offload_info.format == AUDIO_FORMAT_FLAC) |
Satya Krishna Pindiproli | 5d82d01 | 2015-08-12 18:21:25 +0530 | [diff] [blame] | 7695 | out->compr_config.codec->options.flac_dec.sample_size = AUDIO_OUTPUT_BIT_WIDTH; |
Mingming Yin | 3ee55c6 | 2014-08-04 14:23:35 -0700 | [diff] [blame] | 7696 | |
Dhanalakshmi Siddani | 1873793 | 2016-11-29 17:33:17 +0530 | [diff] [blame] | 7697 | if (config->offload_info.format == AUDIO_FORMAT_APTX) { |
| 7698 | audio_extn_send_aptx_dec_bt_addr_to_dsp(out); |
| 7699 | } |
| 7700 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 7701 | if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING) |
| 7702 | out->non_blocking = 1; |
Haynes Mathew George | 352f27b | 2013-07-26 00:00:15 -0700 | [diff] [blame] | 7703 | |
Manish Dewangan | 69426c8 | 2017-01-30 17:35:36 +0530 | [diff] [blame] | 7704 | if ((flags & AUDIO_OUTPUT_FLAG_TIMESTAMP) && |
| 7705 | (flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC)) { |
| 7706 | out->render_mode = RENDER_MODE_AUDIO_STC_MASTER; |
| 7707 | } else if(flags & AUDIO_OUTPUT_FLAG_TIMESTAMP) { |
| 7708 | out->render_mode = RENDER_MODE_AUDIO_MASTER; |
| 7709 | } else { |
| 7710 | out->render_mode = RENDER_MODE_AUDIO_NO_TIMESTAMP; |
| 7711 | } |
Alexy Joseph | aa54c87 | 2014-12-03 02:46:47 -0800 | [diff] [blame] | 7712 | |
Naresh Tanniru | 29bce4e | 2017-04-27 17:54:30 +0530 | [diff] [blame] | 7713 | memset(&out->channel_map_param, 0, |
| 7714 | sizeof(struct audio_out_channel_map_param)); |
| 7715 | |
Haynes Mathew George | 352f27b | 2013-07-26 00:00:15 -0700 | [diff] [blame] | 7716 | out->send_new_metadata = 1; |
Chaithanya Krishna Bacharaju | a70cb6a | 2015-07-24 14:15:05 +0530 | [diff] [blame] | 7717 | out->send_next_track_params = false; |
| 7718 | out->is_compr_metadata_avail = false; |
Haynes Mathew George | b9012ab | 2013-12-10 13:44:56 -0800 | [diff] [blame] | 7719 | out->offload_state = OFFLOAD_STATE_IDLE; |
| 7720 | out->playback_started = 0; |
Zhou Song | 48453a0 | 2018-01-10 17:50:59 +0800 | [diff] [blame] | 7721 | out->writeAt.tv_sec = 0; |
| 7722 | out->writeAt.tv_nsec = 0; |
Haynes Mathew George | b9012ab | 2013-12-10 13:44:56 -0800 | [diff] [blame] | 7723 | |
Jitendra Naruka | 1b6513f | 2014-11-22 19:34:13 -0800 | [diff] [blame] | 7724 | audio_extn_dts_create_state_notifier_node(out->usecase); |
| 7725 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 7726 | ALOGV("%s: offloaded output offload_info version %04x bit rate %d", |
| 7727 | __func__, config->offload_info.version, |
| 7728 | config->offload_info.bit_rate); |
Ashish Jain | 5106d36 | 2016-05-11 19:23:33 +0530 | [diff] [blame] | 7729 | |
Preetam Singh Ranawat | f5fbdd6 | 2016-09-29 18:38:31 +0530 | [diff] [blame] | 7730 | /* Check if DSD audio format is supported in codec |
| 7731 | * and there is no active native DSD use case |
Preetam Singh Ranawat | cb6212e | 2016-07-19 18:33:53 +0530 | [diff] [blame] | 7732 | */ |
| 7733 | |
| 7734 | if ((config->format == AUDIO_FORMAT_DSD) && |
Preetam Singh Ranawat | f5fbdd6 | 2016-09-29 18:38:31 +0530 | [diff] [blame] | 7735 | (!platform_check_codec_dsd_support(adev->platform) || |
| 7736 | audio_is_dsd_native_stream_active(adev))) { |
Preetam Singh Ranawat | cb6212e | 2016-07-19 18:33:53 +0530 | [diff] [blame] | 7737 | ret = -EINVAL; |
| 7738 | goto error_open; |
| 7739 | } |
| 7740 | |
Ashish Jain | 5106d36 | 2016-05-11 19:23:33 +0530 | [diff] [blame] | 7741 | /* Disable gapless if any of the following is true |
| 7742 | * passthrough playback |
| 7743 | * AV playback |
Dhananjay Kumar | ac34158 | 2017-02-23 23:42:25 +0530 | [diff] [blame] | 7744 | * non compressed Direct playback |
Ashish Jain | 5106d36 | 2016-05-11 19:23:33 +0530 | [diff] [blame] | 7745 | */ |
Satish Babu Patakokila | 1caa1b7 | 2016-05-24 13:47:08 +0530 | [diff] [blame] | 7746 | if (audio_extn_passthru_is_passthrough_stream(out) || |
Preetam Singh Ranawat | f5fbdd6 | 2016-09-29 18:38:31 +0530 | [diff] [blame] | 7747 | (config->format == AUDIO_FORMAT_DSD) || |
Naresh Tanniru | 928f086 | 2017-04-07 16:44:23 -0700 | [diff] [blame] | 7748 | (config->format == AUDIO_FORMAT_IEC61937) || |
Preetam Singh Ranawat | f5fbdd6 | 2016-09-29 18:38:31 +0530 | [diff] [blame] | 7749 | config->offload_info.has_video || |
Dhananjay Kumar | ac34158 | 2017-02-23 23:42:25 +0530 | [diff] [blame] | 7750 | !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) { |
Ashish Jain | 5106d36 | 2016-05-11 19:23:33 +0530 | [diff] [blame] | 7751 | check_and_set_gapless_mode(adev, false); |
| 7752 | } else |
| 7753 | check_and_set_gapless_mode(adev, true); |
Mingming Yin | 2185465 | 2016-04-13 11:54:02 -0700 | [diff] [blame] | 7754 | |
Satish Babu Patakokila | 1caa1b7 | 2016-05-24 13:47:08 +0530 | [diff] [blame] | 7755 | if (audio_extn_passthru_is_passthrough_stream(out)) { |
Mingming Yin | 2185465 | 2016-04-13 11:54:02 -0700 | [diff] [blame] | 7756 | out->flags |= AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH; |
| 7757 | } |
Preetam Singh Ranawat | cb6212e | 2016-07-19 18:33:53 +0530 | [diff] [blame] | 7758 | if (config->format == AUDIO_FORMAT_DSD) { |
| 7759 | out->flags |= AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH; |
| 7760 | out->compr_config.codec->compr_passthr = PASSTHROUGH_DSD; |
| 7761 | } |
Aalique Grahame | 0359a1f | 2016-09-08 16:54:22 -0700 | [diff] [blame] | 7762 | |
| 7763 | create_offload_callback_thread(out); |
| 7764 | |
Shiv Maliyappanahalli | f3b9a42 | 2013-10-22 16:38:08 -0700 | [diff] [blame] | 7765 | } else if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) { |
Arun Mirpuri | 7da752a | 2018-09-11 18:01:15 -0700 | [diff] [blame] | 7766 | switch (config->sample_rate) { |
| 7767 | case 0: |
| 7768 | out->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE; |
| 7769 | break; |
| 7770 | case 8000: |
| 7771 | case 16000: |
| 7772 | case 48000: |
| 7773 | out->sample_rate = config->sample_rate; |
| 7774 | break; |
| 7775 | default: |
| 7776 | ALOGE("%s: Unsupported sampling rate %d for Incall Music", __func__, |
| 7777 | config->sample_rate); |
| 7778 | config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE; |
| 7779 | ret = -EINVAL; |
| 7780 | goto error_open; |
| 7781 | } |
| 7782 | //FIXME: add support for MONO stream configuration when audioflinger mixer supports it |
| 7783 | switch (config->channel_mask) { |
| 7784 | case AUDIO_CHANNEL_NONE: |
| 7785 | case AUDIO_CHANNEL_OUT_STEREO: |
| 7786 | out->channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 7787 | break; |
| 7788 | default: |
| 7789 | ALOGE("%s: Unsupported channel mask %#x for Incall Music", __func__, |
| 7790 | config->channel_mask); |
| 7791 | config->channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 7792 | ret = -EINVAL; |
| 7793 | goto error_open; |
| 7794 | } |
| 7795 | switch (config->format) { |
| 7796 | case AUDIO_FORMAT_DEFAULT: |
| 7797 | case AUDIO_FORMAT_PCM_16_BIT: |
| 7798 | out->format = AUDIO_FORMAT_PCM_16_BIT; |
| 7799 | break; |
| 7800 | default: |
| 7801 | ALOGE("%s: Unsupported format %#x for Incall Music", __func__, |
| 7802 | config->format); |
| 7803 | config->format = AUDIO_FORMAT_PCM_16_BIT; |
| 7804 | ret = -EINVAL; |
| 7805 | goto error_open; |
| 7806 | } |
| 7807 | |
Satya Krishna Pindiproli | f1cd92b | 2016-04-14 19:05:23 +0530 | [diff] [blame] | 7808 | ret = voice_extn_check_and_set_incall_music_usecase(adev, out); |
Shiv Maliyappanahalli | f3b9a42 | 2013-10-22 16:38:08 -0700 | [diff] [blame] | 7809 | if (ret != 0) { |
| 7810 | ALOGE("%s: Incall music delivery usecase cannot be set error:%d", |
Arun Mirpuri | 7da752a | 2018-09-11 18:01:15 -0700 | [diff] [blame] | 7811 | __func__, ret); |
Shiv Maliyappanahalli | f3b9a42 | 2013-10-22 16:38:08 -0700 | [diff] [blame] | 7812 | goto error_open; |
| 7813 | } |
Arun Mirpuri | 7da752a | 2018-09-11 18:01:15 -0700 | [diff] [blame] | 7814 | } else if (out->devices == AUDIO_DEVICE_OUT_TELEPHONY_TX) { |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 7815 | switch (config->sample_rate) { |
| 7816 | case 0: |
| 7817 | out->sample_rate = AFE_PROXY_SAMPLING_RATE; |
| 7818 | break; |
| 7819 | case 8000: |
| 7820 | case 16000: |
| 7821 | case 48000: |
| 7822 | out->sample_rate = config->sample_rate; |
| 7823 | break; |
| 7824 | default: |
| 7825 | ALOGE("%s: Unsupported sampling rate %d for Telephony TX", __func__, |
| 7826 | config->sample_rate); |
| 7827 | config->sample_rate = AFE_PROXY_SAMPLING_RATE; |
| 7828 | ret = -EINVAL; |
| 7829 | break; |
Ravi Kumar Alamanda | 060bc5a | 2014-09-05 13:51:35 -0700 | [diff] [blame] | 7830 | } |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 7831 | //FIXME: add support for MONO stream configuration when audioflinger mixer supports it |
| 7832 | switch (config->channel_mask) { |
| 7833 | case AUDIO_CHANNEL_NONE: |
| 7834 | out->channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 7835 | break; |
| 7836 | case AUDIO_CHANNEL_OUT_STEREO: |
| 7837 | out->channel_mask = config->channel_mask; |
| 7838 | break; |
| 7839 | default: |
| 7840 | ALOGE("%s: Unsupported channel mask %#x for Telephony TX", __func__, |
| 7841 | config->channel_mask); |
| 7842 | config->channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 7843 | ret = -EINVAL; |
| 7844 | break; |
Ravi Kumar Alamanda | 060bc5a | 2014-09-05 13:51:35 -0700 | [diff] [blame] | 7845 | } |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 7846 | switch (config->format) { |
| 7847 | case AUDIO_FORMAT_DEFAULT: |
| 7848 | out->format = AUDIO_FORMAT_PCM_16_BIT; |
| 7849 | break; |
| 7850 | case AUDIO_FORMAT_PCM_16_BIT: |
| 7851 | out->format = config->format; |
| 7852 | break; |
| 7853 | default: |
| 7854 | ALOGE("%s: Unsupported format %#x for Telephony TX", __func__, |
| 7855 | config->format); |
| 7856 | config->format = AUDIO_FORMAT_PCM_16_BIT; |
| 7857 | ret = -EINVAL; |
| 7858 | break; |
| 7859 | } |
| 7860 | if (ret != 0) |
| 7861 | goto error_open; |
| 7862 | |
Ravi Kumar Alamanda | 060bc5a | 2014-09-05 13:51:35 -0700 | [diff] [blame] | 7863 | out->usecase = USECASE_AUDIO_PLAYBACK_AFE_PROXY; |
| 7864 | out->config = pcm_config_afe_proxy_playback; |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 7865 | out->config.rate = out->sample_rate; |
| 7866 | out->config.channels = |
| 7867 | audio_channel_count_from_out_mask(out->channel_mask); |
| 7868 | out->config.format = pcm_format_from_audio_format(out->format); |
Ravi Kumar Alamanda | 060bc5a | 2014-09-05 13:51:35 -0700 | [diff] [blame] | 7869 | adev->voice_tx_output = out; |
Ravi Kumar Alamanda | 8f715d9 | 2013-11-01 20:37:38 -0700 | [diff] [blame] | 7870 | } else { |
Ashish Jain | 058165c | 2016-09-28 23:18:48 +0530 | [diff] [blame] | 7871 | unsigned int channels = 0; |
| 7872 | /*Update config params to default if not set by the caller*/ |
| 7873 | if (config->sample_rate == 0) |
| 7874 | config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE; |
| 7875 | if (config->channel_mask == AUDIO_CHANNEL_NONE) |
| 7876 | config->channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 7877 | if (config->format == AUDIO_FORMAT_DEFAULT) |
| 7878 | config->format = AUDIO_FORMAT_PCM_16_BIT; |
| 7879 | |
| 7880 | channels = audio_channel_count_from_out_mask(out->channel_mask); |
| 7881 | |
Varun Balaraj | e49253e | 2017-07-06 19:48:56 +0530 | [diff] [blame] | 7882 | if (out->flags & AUDIO_OUTPUT_FLAG_INTERACTIVE) { |
| 7883 | out->usecase = get_interactive_usecase(adev); |
| 7884 | out->config = pcm_config_low_latency; |
| 7885 | } else if (out->flags & AUDIO_OUTPUT_FLAG_RAW) { |
Ashish Jain | 83a6cc2 | 2016-06-28 14:34:17 +0530 | [diff] [blame] | 7886 | out->usecase = USECASE_AUDIO_PLAYBACK_ULL; |
Haynes Mathew George | 5beddd4 | 2016-06-27 18:33:40 -0700 | [diff] [blame] | 7887 | out->realtime = may_use_noirq_mode(adev, USECASE_AUDIO_PLAYBACK_ULL, |
| 7888 | out->flags); |
| 7889 | out->config = out->realtime ? pcm_config_rt : pcm_config_low_latency; |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 7890 | } else if (out->flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) { |
| 7891 | out->usecase = USECASE_AUDIO_PLAYBACK_MMAP; |
| 7892 | out->config = pcm_config_mmap_playback; |
| 7893 | out->stream.start = out_start; |
| 7894 | out->stream.stop = out_stop; |
| 7895 | out->stream.create_mmap_buffer = out_create_mmap_buffer; |
| 7896 | out->stream.get_mmap_position = out_get_mmap_position; |
Ashish Jain | 83a6cc2 | 2016-06-28 14:34:17 +0530 | [diff] [blame] | 7897 | } else if (out->flags & AUDIO_OUTPUT_FLAG_FAST) { |
| 7898 | out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY; |
Aniket Kumar Lata | 932f487 | 2017-11-06 18:29:44 -0800 | [diff] [blame] | 7899 | out->hal_output_suspend_supported = |
| 7900 | property_get_bool("vendor.audio.hal.output.suspend.supported", false); |
| 7901 | out->dynamic_pm_qos_config_supported = |
| 7902 | property_get_bool("vendor.audio.hal.dynamic.qos.config.supported", false); |
| 7903 | if (!out->dynamic_pm_qos_config_supported) { |
Alexy Joseph | 9898883 | 2017-01-13 14:56:59 -0800 | [diff] [blame] | 7904 | ALOGI("%s: dynamic qos voting not enabled for platform", __func__); |
| 7905 | } else { |
| 7906 | ALOGI("%s: dynamic qos voting enabled for platform", __func__); |
| 7907 | //the mixer path will be a string similar to "low-latency-playback resume" |
| 7908 | strlcpy(out->pm_qos_mixer_path, use_case_table[out->usecase], MAX_MIXER_PATH_LEN); |
| 7909 | strlcat(out->pm_qos_mixer_path, |
| 7910 | " resume", MAX_MIXER_PATH_LEN); |
| 7911 | ALOGI("%s: created %s pm_qos_mixer_path" , __func__, |
| 7912 | out->pm_qos_mixer_path); |
| 7913 | } |
Ashish Jain | 83a6cc2 | 2016-06-28 14:34:17 +0530 | [diff] [blame] | 7914 | out->config = pcm_config_low_latency; |
| 7915 | } else if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) { |
| 7916 | out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER; |
| 7917 | out->config = pcm_config_deep_buffer; |
Ashish Jain | 058165c | 2016-09-28 23:18:48 +0530 | [diff] [blame] | 7918 | out->config.period_size = get_output_period_size(config->sample_rate, out->format, |
| 7919 | channels, DEEP_BUFFER_OUTPUT_PERIOD_DURATION); |
| 7920 | if (out->config.period_size <= 0) { |
| 7921 | ALOGE("Invalid configuration period size is not valid"); |
| 7922 | ret = -EINVAL; |
| 7923 | goto error_open; |
| 7924 | } |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 7925 | } else if (flags & AUDIO_OUTPUT_FLAG_TTS) { |
| 7926 | out->usecase = USECASE_AUDIO_PLAYBACK_TTS; |
| 7927 | out->config = pcm_config_deep_buffer; |
Vignesh Kulothungan | a692727 | 2019-02-20 15:17:07 -0800 | [diff] [blame] | 7928 | } else if (config->channel_mask & AUDIO_CHANNEL_HAPTIC_ALL) { |
| 7929 | out->usecase = USECASE_AUDIO_PLAYBACK_WITH_HAPTICS; |
| 7930 | out->config = pcm_config_haptics_audio; |
| 7931 | if (force_haptic_path) |
| 7932 | adev->haptics_config = pcm_config_haptics_audio; |
| 7933 | else |
| 7934 | adev->haptics_config = pcm_config_haptics; |
| 7935 | |
| 7936 | out->config.channels = |
| 7937 | audio_channel_count_from_out_mask(out->channel_mask & ~AUDIO_CHANNEL_HAPTIC_ALL); |
| 7938 | |
| 7939 | if (force_haptic_path) { |
| 7940 | out->config.channels = 1; |
| 7941 | adev->haptics_config.channels = 1; |
| 7942 | } else |
| 7943 | adev->haptics_config.channels = audio_channel_count_from_out_mask(out->channel_mask & AUDIO_CHANNEL_HAPTIC_ALL); |
Derek Chen | f6318be | 2017-06-12 17:16:24 -0400 | [diff] [blame] | 7944 | } else if (out->devices & AUDIO_DEVICE_OUT_BUS) { |
| 7945 | ret = audio_extn_auto_hal_open_output_stream(out); |
| 7946 | if (ret) { |
| 7947 | ALOGE("%s: Failed to open output stream for bus device", __func__); |
| 7948 | ret = -EINVAL; |
| 7949 | goto error_open; |
| 7950 | } |
Ashish Jain | 83a6cc2 | 2016-06-28 14:34:17 +0530 | [diff] [blame] | 7951 | } else { |
| 7952 | /* primary path is the default path selected if no other outputs are available/suitable */ |
Arun Mirpuri | b1bec9c | 2019-01-29 16:42:45 -0800 | [diff] [blame] | 7953 | out->usecase = GET_USECASE_AUDIO_PLAYBACK_PRIMARY(use_db_as_primary); |
| 7954 | out->config = GET_PCM_CONFIG_AUDIO_PLAYBACK_PRIMARY(use_db_as_primary); |
Ashish Jain | 83a6cc2 | 2016-06-28 14:34:17 +0530 | [diff] [blame] | 7955 | } |
| 7956 | out->hal_ip_format = format = out->format; |
| 7957 | out->config.format = hal_format_to_pcm(out->hal_ip_format); |
| 7958 | out->hal_op_format = pcm_format_to_hal(out->config.format); |
| 7959 | out->bit_width = format_to_bitwidth_table[out->hal_op_format] << 3; |
| 7960 | out->config.rate = config->sample_rate; |
Ravi Kumar Alamanda | 8f715d9 | 2013-11-01 20:37:38 -0700 | [diff] [blame] | 7961 | out->sample_rate = out->config.rate; |
Ashish Jain | 058165c | 2016-09-28 23:18:48 +0530 | [diff] [blame] | 7962 | out->config.channels = channels; |
Ashish Jain | 83a6cc2 | 2016-06-28 14:34:17 +0530 | [diff] [blame] | 7963 | if (out->hal_ip_format != out->hal_op_format) { |
| 7964 | uint32_t buffer_size = out->config.period_size * |
| 7965 | format_to_bitwidth_table[out->hal_op_format] * |
| 7966 | out->config.channels; |
| 7967 | out->convert_buffer = calloc(1, buffer_size); |
| 7968 | if (out->convert_buffer == NULL){ |
| 7969 | ALOGE("Allocation failed for convert buffer for size %d", |
| 7970 | out->compr_config.fragment_size); |
| 7971 | ret = -ENOMEM; |
| 7972 | goto error_open; |
| 7973 | } |
| 7974 | ALOGD("Convert buffer allocated of size %d", buffer_size); |
| 7975 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 7976 | } |
| 7977 | |
Alexy Joseph | 5e4ccbc | 2017-02-21 14:20:12 -0800 | [diff] [blame] | 7978 | ALOGV("%s devices:%d, format:%x, out->sample_rate:%d,out->bit_width:%d out->format:%d out->flags:%x, flags: %x usecase %d", |
| 7979 | __func__, devices, format, out->sample_rate, out->bit_width, out->format, out->flags, flags, out->usecase); |
Ashish Jain | 83a6cc2 | 2016-06-28 14:34:17 +0530 | [diff] [blame] | 7980 | |
Pradnya Chaphekar | 80a8cfb | 2014-10-20 16:17:01 -0700 | [diff] [blame] | 7981 | /* TODO remove this hardcoding and check why width is zero*/ |
| 7982 | if (out->bit_width == 0) |
| 7983 | out->bit_width = 16; |
Dhananjay Kumar | d6d3215 | 2016-10-13 16:11:03 +0530 | [diff] [blame] | 7984 | audio_extn_utils_update_stream_output_app_type_cfg(adev->platform, |
Subhash Chandra Bose Naripeddy | 19dc03b | 2014-03-10 14:43:05 -0700 | [diff] [blame] | 7985 | &adev->streams_output_cfg_list, |
Aalique Grahame | 65780b5 | 2017-09-27 14:59:56 -0700 | [diff] [blame] | 7986 | devices, out->flags, out->hal_op_format, out->sample_rate, |
Dhananjay Kumar | 4d91c1a | 2016-12-01 23:27:29 +0530 | [diff] [blame] | 7987 | out->bit_width, out->channel_mask, out->profile, |
Manish Dewangan | 837dc46 | 2015-05-27 10:17:41 +0530 | [diff] [blame] | 7988 | &out->app_type_cfg); |
Aalique Grahame | 6e76371 | 2019-01-31 16:18:17 -0800 | [diff] [blame] | 7989 | if ((out->usecase == (audio_usecase_t)(GET_USECASE_AUDIO_PLAYBACK_PRIMARY(use_db_as_primary))) || |
Haynes Mathew George | bf14371 | 2013-12-03 13:02:53 -0800 | [diff] [blame] | 7990 | (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) { |
| 7991 | /* Ensure the default output is not selected twice */ |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 7992 | if(adev->primary_output == NULL) |
| 7993 | adev->primary_output = out; |
| 7994 | else { |
| 7995 | ALOGE("%s: Primary output is already opened", __func__); |
Ravi Kumar Alamanda | b199506 | 2013-03-21 23:18:20 -0700 | [diff] [blame] | 7996 | ret = -EEXIST; |
| 7997 | goto error_open; |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 7998 | } |
| 7999 | } |
| 8000 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 8001 | /* Check if this usecase is already existing */ |
| 8002 | pthread_mutex_lock(&adev->lock); |
Narsinga Rao Chella | 7ce0535 | 2014-04-17 20:00:41 -0700 | [diff] [blame] | 8003 | if ((get_usecase_from_list(adev, out->usecase) != NULL) && |
| 8004 | (out->usecase != USECASE_COMPRESS_VOIP_CALL)) { |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 8005 | ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 8006 | pthread_mutex_unlock(&adev->lock); |
Ravi Kumar Alamanda | b199506 | 2013-03-21 23:18:20 -0700 | [diff] [blame] | 8007 | ret = -EEXIST; |
| 8008 | goto error_open; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 8009 | } |
Alexy Joseph | 5e4ccbc | 2017-02-21 14:20:12 -0800 | [diff] [blame] | 8010 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 8011 | pthread_mutex_unlock(&adev->lock); |
| 8012 | |
| 8013 | out->stream.common.get_sample_rate = out_get_sample_rate; |
| 8014 | out->stream.common.set_sample_rate = out_set_sample_rate; |
| 8015 | out->stream.common.get_buffer_size = out_get_buffer_size; |
| 8016 | out->stream.common.get_channels = out_get_channels; |
| 8017 | out->stream.common.get_format = out_get_format; |
| 8018 | out->stream.common.set_format = out_set_format; |
| 8019 | out->stream.common.standby = out_standby; |
| 8020 | out->stream.common.dump = out_dump; |
| 8021 | out->stream.common.set_parameters = out_set_parameters; |
| 8022 | out->stream.common.get_parameters = out_get_parameters; |
| 8023 | out->stream.common.add_audio_effect = out_add_audio_effect; |
| 8024 | out->stream.common.remove_audio_effect = out_remove_audio_effect; |
| 8025 | out->stream.get_latency = out_get_latency; |
| 8026 | out->stream.set_volume = out_set_volume; |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 8027 | #ifdef NO_AUDIO_OUT |
| 8028 | out->stream.write = out_write_for_no_output; |
| 8029 | #else |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 8030 | out->stream.write = out_write; |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 8031 | #endif |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 8032 | out->stream.get_render_position = out_get_render_position; |
| 8033 | out->stream.get_next_write_timestamp = out_get_next_write_timestamp; |
Glenn Kasten | 2ccd7ba | 2013-09-10 09:04:31 -0700 | [diff] [blame] | 8034 | out->stream.get_presentation_position = out_get_presentation_position; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 8035 | |
Haynes Mathew George | 1608104 | 2017-05-31 17:16:49 -0700 | [diff] [blame] | 8036 | if (out->realtime) |
| 8037 | out->af_period_multiplier = af_period_multiplier; |
| 8038 | else |
| 8039 | out->af_period_multiplier = 1; |
| 8040 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 8041 | out->standby = 1; |
Eric Laurent | a9024de | 2013-04-04 09:19:12 -0700 | [diff] [blame] | 8042 | /* out->muted = false; by calloc() */ |
Glenn Kasten | 2ccd7ba | 2013-09-10 09:04:31 -0700 | [diff] [blame] | 8043 | /* out->written = 0; by calloc() */ |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 8044 | |
| 8045 | config->format = out->stream.common.get_format(&out->stream.common); |
| 8046 | config->channel_mask = out->stream.common.get_channels(&out->stream.common); |
| 8047 | config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common); |
Naresh Tanniru | 04f7188 | 2018-06-26 17:46:22 +0530 | [diff] [blame] | 8048 | register_format(out->format, out->supported_formats); |
| 8049 | register_channel_mask(out->channel_mask, out->supported_channel_masks); |
| 8050 | register_sample_rate(out->sample_rate, out->supported_sample_rates); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 8051 | |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 8052 | out->error_log = error_log_create( |
| 8053 | ERROR_LOG_ENTRIES, |
| 8054 | 1000000000 /* aggregate consecutive identical errors within one second in ns */); |
| 8055 | |
Dhananjay Kumar | e6293dd | 2017-05-25 17:25:30 +0530 | [diff] [blame] | 8056 | /* |
| 8057 | By locking output stream before registering, we allow the callback |
| 8058 | to update stream's state only after stream's initial state is set to |
| 8059 | adev state. |
| 8060 | */ |
| 8061 | lock_output_stream(out); |
| 8062 | audio_extn_snd_mon_register_listener(out, out_snd_mon_cb); |
| 8063 | pthread_mutex_lock(&adev->lock); |
| 8064 | out->card_status = adev->card_status; |
| 8065 | pthread_mutex_unlock(&adev->lock); |
| 8066 | pthread_mutex_unlock(&out->lock); |
| 8067 | |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 8068 | stream_app_type_cfg_init(&out->app_type_cfg); |
| 8069 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 8070 | *stream_out = &out->stream; |
Sidipotu Ashok | f43018c | 2014-05-02 16:21:50 +0530 | [diff] [blame] | 8071 | ALOGD("%s: Stream (%p) picks up usecase (%s)", __func__, &out->stream, |
vivek mehta | 0ea887a | 2015-08-26 14:01:20 -0700 | [diff] [blame] | 8072 | use_case_table[out->usecase]); |
Jitendra Naruka | 1b6513f | 2014-11-22 19:34:13 -0800 | [diff] [blame] | 8073 | |
| 8074 | if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) |
| 8075 | audio_extn_dts_notify_playback_state(out->usecase, 0, out->sample_rate, |
| 8076 | popcount(out->channel_mask), out->playback_started); |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 8077 | /* setup a channel for client <--> adsp communication for stream events */ |
Manish Dewangan | 21a850a | 2017-08-14 12:03:55 +0530 | [diff] [blame] | 8078 | is_direct_passthough = audio_extn_passthru_is_direct_passthrough(out); |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 8079 | if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) || |
Naresh Tanniru | 8581945 | 2017-05-04 18:55:45 -0700 | [diff] [blame] | 8080 | (out->flags & AUDIO_OUTPUT_FLAG_DIRECT_PCM) || |
Gangadhar S | b021034 | 2019-02-22 17:39:41 +0530 | [diff] [blame] | 8081 | audio_extn_ip_hdlr_intf_supported_for_copp(adev->platform) || |
| 8082 | (audio_extn_ip_hdlr_intf_supported(config->format, is_direct_passthough, false))) { |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 8083 | hdlr_stream_cfg.pcm_device_id = platform_get_pcm_device_id( |
| 8084 | out->usecase, PCM_PLAYBACK); |
| 8085 | hdlr_stream_cfg.flags = out->flags; |
| 8086 | hdlr_stream_cfg.type = PCM_PLAYBACK; |
| 8087 | ret = audio_extn_adsp_hdlr_stream_open(&out->adsp_hdlr_stream_handle, |
| 8088 | &hdlr_stream_cfg); |
| 8089 | if (ret) { |
| 8090 | ALOGE("%s: adsp_hdlr_stream_open failed %d",__func__, ret); |
| 8091 | out->adsp_hdlr_stream_handle = NULL; |
| 8092 | } |
| 8093 | } |
Gangadhar S | b021034 | 2019-02-22 17:39:41 +0530 | [diff] [blame] | 8094 | ip_hdlr_stream = audio_extn_ip_hdlr_intf_supported(config->format, |
| 8095 | is_direct_passthough, false); |
| 8096 | ip_hdlr_dev = audio_extn_ip_hdlr_intf_supported_for_copp(adev->platform); |
| 8097 | if (ip_hdlr_stream || ip_hdlr_dev ) { |
Vidyakumar Athota | 2062f91 | 2017-06-27 14:46:15 -0700 | [diff] [blame] | 8098 | ret = audio_extn_ip_hdlr_intf_init(&out->ip_hdlr_handle, NULL, NULL, adev, out->usecase); |
Naresh Tanniru | 8581945 | 2017-05-04 18:55:45 -0700 | [diff] [blame] | 8099 | if (ret < 0) { |
| 8100 | ALOGE("%s: audio_extn_ip_hdlr_intf_init failed %d",__func__, ret); |
| 8101 | out->ip_hdlr_handle = NULL; |
| 8102 | } |
| 8103 | } |
Derek Chen | f939fb7 | 2018-11-13 13:34:41 -0800 | [diff] [blame] | 8104 | |
| 8105 | streams_output_ctxt_t *out_ctxt = (streams_output_ctxt_t *) |
| 8106 | calloc(1, sizeof(streams_output_ctxt_t)); |
| 8107 | if (out_ctxt == NULL) { |
| 8108 | ALOGE("%s fail to allocate output ctxt", __func__); |
| 8109 | ret = -ENOMEM; |
| 8110 | goto error_open; |
| 8111 | } |
| 8112 | out_ctxt->output = out; |
| 8113 | |
| 8114 | pthread_mutex_lock(&adev->lock); |
| 8115 | list_add_tail(&adev->active_outputs_list, &out_ctxt->list); |
| 8116 | pthread_mutex_unlock(&adev->lock); |
| 8117 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 8118 | ALOGV("%s: exit", __func__); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 8119 | return 0; |
Ravi Kumar Alamanda | b199506 | 2013-03-21 23:18:20 -0700 | [diff] [blame] | 8120 | |
| 8121 | error_open: |
Ashish Jain | 83a6cc2 | 2016-06-28 14:34:17 +0530 | [diff] [blame] | 8122 | if (out->convert_buffer) |
| 8123 | free(out->convert_buffer); |
Ravi Kumar Alamanda | b199506 | 2013-03-21 23:18:20 -0700 | [diff] [blame] | 8124 | free(out); |
| 8125 | *stream_out = NULL; |
| 8126 | ALOGD("%s: exit: ret %d", __func__, ret); |
| 8127 | return ret; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 8128 | } |
| 8129 | |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 8130 | void adev_close_output_stream(struct audio_hw_device *dev __unused, |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 8131 | struct audio_stream_out *stream) |
| 8132 | { |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 8133 | struct stream_out *out = (struct stream_out *)stream; |
| 8134 | struct audio_device *adev = out->dev; |
Narsinga Rao Chella | 05573b7 | 2013-11-15 15:21:40 -0800 | [diff] [blame] | 8135 | int ret = 0; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 8136 | |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 8137 | ALOGD("%s: enter:stream_handle(%s)",__func__, use_case_table[out->usecase]); |
Sidipotu Ashok | f43018c | 2014-05-02 16:21:50 +0530 | [diff] [blame] | 8138 | |
Dhananjay Kumar | e6293dd | 2017-05-25 17:25:30 +0530 | [diff] [blame] | 8139 | // must deregister from sndmonitor first to prevent races |
| 8140 | // between the callback and close_stream |
| 8141 | audio_extn_snd_mon_unregister_listener(out); |
| 8142 | |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 8143 | /* close adsp hdrl session before standby */ |
| 8144 | if (out->adsp_hdlr_stream_handle) { |
| 8145 | ret = audio_extn_adsp_hdlr_stream_close(out->adsp_hdlr_stream_handle); |
| 8146 | if (ret) |
| 8147 | ALOGE("%s: adsp_hdlr_stream_close failed %d",__func__, ret); |
| 8148 | out->adsp_hdlr_stream_handle = NULL; |
| 8149 | } |
| 8150 | |
Manish Dewangan | 21a850a | 2017-08-14 12:03:55 +0530 | [diff] [blame] | 8151 | if (out->ip_hdlr_handle) { |
Naresh Tanniru | 8581945 | 2017-05-04 18:55:45 -0700 | [diff] [blame] | 8152 | audio_extn_ip_hdlr_intf_deinit(out->ip_hdlr_handle); |
| 8153 | out->ip_hdlr_handle = NULL; |
| 8154 | } |
| 8155 | |
Narsinga Rao Chella | 05573b7 | 2013-11-15 15:21:40 -0800 | [diff] [blame] | 8156 | if (out->usecase == USECASE_COMPRESS_VOIP_CALL) { |
Venkata Narendra Kumar Gutta | 9181214 | 2014-08-11 18:20:49 +0530 | [diff] [blame] | 8157 | pthread_mutex_lock(&adev->lock); |
Narsinga Rao Chella | 05573b7 | 2013-11-15 15:21:40 -0800 | [diff] [blame] | 8158 | ret = voice_extn_compress_voip_close_output_stream(&stream->common); |
Ashish Jain | 1b9b30c | 2017-05-18 20:57:40 +0530 | [diff] [blame] | 8159 | out->started = 0; |
Venkata Narendra Kumar Gutta | 9181214 | 2014-08-11 18:20:49 +0530 | [diff] [blame] | 8160 | pthread_mutex_unlock(&adev->lock); |
Narsinga Rao Chella | 05573b7 | 2013-11-15 15:21:40 -0800 | [diff] [blame] | 8161 | if(ret != 0) |
| 8162 | ALOGE("%s: Compress voip output cannot be closed, error:%d", |
| 8163 | __func__, ret); |
Ravi Kumar Alamanda | c3bc081 | 2014-09-08 15:34:32 -0700 | [diff] [blame] | 8164 | } else |
Narsinga Rao Chella | 05573b7 | 2013-11-15 15:21:40 -0800 | [diff] [blame] | 8165 | out_standby(&stream->common); |
| 8166 | |
Subhash Chandra Bose Naripeddy | 16ff4f8 | 2014-04-01 21:03:10 -0700 | [diff] [blame] | 8167 | if (is_offload_usecase(out->usecase)) { |
Jitendra Naruka | 1b6513f | 2014-11-22 19:34:13 -0800 | [diff] [blame] | 8168 | audio_extn_dts_remove_state_notifier_node(out->usecase); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 8169 | destroy_offload_callback_thread(out); |
Subhash Chandra Bose Naripeddy | 16ff4f8 | 2014-04-01 21:03:10 -0700 | [diff] [blame] | 8170 | free_offload_usecase(adev, out->usecase); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 8171 | if (out->compr_config.codec != NULL) |
| 8172 | free(out->compr_config.codec); |
| 8173 | } |
Ravi Kumar Alamanda | 060bc5a | 2014-09-05 13:51:35 -0700 | [diff] [blame] | 8174 | |
Chaithanya Krishna Bacharaju | 69d2e4c | 2017-05-26 18:22:46 +0530 | [diff] [blame] | 8175 | out->a2dp_compress_mute = false; |
| 8176 | |
Varun Balaraj | e49253e | 2017-07-06 19:48:56 +0530 | [diff] [blame] | 8177 | if (is_interactive_usecase(out->usecase)) |
| 8178 | free_interactive_usecase(adev, out->usecase); |
| 8179 | |
Ashish Jain | 83a6cc2 | 2016-06-28 14:34:17 +0530 | [diff] [blame] | 8180 | if (out->convert_buffer != NULL) { |
| 8181 | free(out->convert_buffer); |
| 8182 | out->convert_buffer = NULL; |
| 8183 | } |
| 8184 | |
Ravi Kumar Alamanda | 060bc5a | 2014-09-05 13:51:35 -0700 | [diff] [blame] | 8185 | if (adev->voice_tx_output == out) |
| 8186 | adev->voice_tx_output = NULL; |
| 8187 | |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 8188 | error_log_destroy(out->error_log); |
| 8189 | out->error_log = NULL; |
| 8190 | |
Dhanalakshmi Siddani | 6c3d099 | 2017-01-16 16:52:33 +0530 | [diff] [blame] | 8191 | if (adev->primary_output == out) |
| 8192 | adev->primary_output = NULL; |
| 8193 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 8194 | pthread_cond_destroy(&out->cond); |
| 8195 | pthread_mutex_destroy(&out->lock); |
Derek Chen | f939fb7 | 2018-11-13 13:34:41 -0800 | [diff] [blame] | 8196 | |
| 8197 | pthread_mutex_lock(&adev->lock); |
| 8198 | streams_output_ctxt_t *out_ctxt = out_get_stream(adev, out->handle); |
| 8199 | if (out_ctxt != NULL) { |
| 8200 | list_remove(&out_ctxt->list); |
| 8201 | free(out_ctxt); |
| 8202 | } else { |
| 8203 | ALOGW("%s, output stream already closed", __func__); |
| 8204 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 8205 | free(stream); |
Derek Chen | f939fb7 | 2018-11-13 13:34:41 -0800 | [diff] [blame] | 8206 | pthread_mutex_unlock(&adev->lock); |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 8207 | ALOGV("%s: exit", __func__); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 8208 | } |
| 8209 | |
| 8210 | static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs) |
| 8211 | { |
| 8212 | struct audio_device *adev = (struct audio_device *)dev; |
| 8213 | struct str_parms *parms; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 8214 | char value[32]; |
Jean-Michel Trivi | c56336b | 2013-05-24 16:55:17 -0700 | [diff] [blame] | 8215 | int val; |
Ravi Kumar Alamanda | c3bc081 | 2014-09-08 15:34:32 -0700 | [diff] [blame] | 8216 | int ret; |
| 8217 | int status = 0; |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 8218 | bool a2dp_reconfig = false; |
Zhou Song | d6d7175 | 2019-05-21 18:08:51 +0800 | [diff] [blame] | 8219 | struct listnode *node; |
Vignesh Kulothungan | 39fc6a2 | 2019-08-01 00:55:59 -0700 | [diff] [blame] | 8220 | int controller = -1, stream = -1; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 8221 | |
Shiv Maliyappanahalli | 3bb7358 | 2013-11-05 12:49:15 -0800 | [diff] [blame] | 8222 | ALOGD("%s: enter: %s", __func__, kvpairs); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 8223 | parms = str_parms_create_str(kvpairs); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 8224 | |
Preetam Singh Ranawat | a5f32b4 | 2014-09-23 12:12:47 +0530 | [diff] [blame] | 8225 | if (!parms) |
| 8226 | goto error; |
Naresh Tanniru | 4c63039 | 2014-05-12 01:05:52 +0530 | [diff] [blame] | 8227 | |
Derek Chen | 6f29367 | 2019-04-01 01:40:24 -0700 | [diff] [blame] | 8228 | /* notify adev and input/output streams on the snd card status */ |
| 8229 | adev_snd_mon_cb((void *)adev, parms); |
| 8230 | |
| 8231 | list_for_each(node, &adev->active_outputs_list) { |
| 8232 | streams_output_ctxt_t *out_ctxt = node_to_item(node, |
| 8233 | streams_output_ctxt_t, |
| 8234 | list); |
| 8235 | out_snd_mon_cb((void *)out_ctxt->output, parms); |
| 8236 | } |
| 8237 | |
| 8238 | list_for_each(node, &adev->active_inputs_list) { |
| 8239 | streams_input_ctxt_t *in_ctxt = node_to_item(node, |
| 8240 | streams_input_ctxt_t, |
| 8241 | list); |
| 8242 | in_snd_mon_cb((void *)in_ctxt->input, parms); |
| 8243 | } |
| 8244 | |
Zhou Song | d6d7175 | 2019-05-21 18:08:51 +0800 | [diff] [blame] | 8245 | pthread_mutex_lock(&adev->lock); |
Ashish Jain | 1b9b30c | 2017-05-18 20:57:40 +0530 | [diff] [blame] | 8246 | ret = str_parms_get_str(parms, "BT_SCO", value, sizeof(value)); |
| 8247 | if (ret >= 0) { |
| 8248 | /* When set to false, HAL should disable EC and NS */ |
Zhou Song | d6d7175 | 2019-05-21 18:08:51 +0800 | [diff] [blame] | 8249 | if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0){ |
Ashish Jain | 1b9b30c | 2017-05-18 20:57:40 +0530 | [diff] [blame] | 8250 | adev->bt_sco_on = true; |
Zhou Song | d6d7175 | 2019-05-21 18:08:51 +0800 | [diff] [blame] | 8251 | } else { |
Ashish Jain | 1b9b30c | 2017-05-18 20:57:40 +0530 | [diff] [blame] | 8252 | adev->bt_sco_on = false; |
Zhou Song | d6d7175 | 2019-05-21 18:08:51 +0800 | [diff] [blame] | 8253 | audio_extn_sco_reset_configuration(); |
Kunlei Zhang | d96af8f | 2019-08-27 16:33:29 +0800 | [diff] [blame] | 8254 | } |
| 8255 | } |
| 8256 | |
| 8257 | ret = str_parms_get_str(parms, "A2dpSuspended", value, sizeof(value)); |
| 8258 | if (ret>=0) { |
| 8259 | if (!strncmp(value, "false", 5) && |
| 8260 | audio_extn_a2dp_source_is_suspended()) { |
| 8261 | struct audio_usecase *usecase; |
| 8262 | struct listnode *node; |
Zhou Song | d6d7175 | 2019-05-21 18:08:51 +0800 | [diff] [blame] | 8263 | list_for_each(node, &adev->usecase_list) { |
| 8264 | usecase = node_to_item(node, struct audio_usecase, list); |
Kunlei Zhang | d96af8f | 2019-08-27 16:33:29 +0800 | [diff] [blame] | 8265 | if (usecase->stream.in && (usecase->type == PCM_CAPTURE) && |
| 8266 | ((usecase->stream.in->device & ~AUDIO_DEVICE_BIT_IN) & |
| 8267 | AUDIO_DEVICE_IN_ALL_SCO)) { |
| 8268 | ALOGD("a2dp resumed, switch bt sco mic to handset mic"); |
Zhou Song | d6d7175 | 2019-05-21 18:08:51 +0800 | [diff] [blame] | 8269 | usecase->stream.in->device = AUDIO_DEVICE_IN_BUILTIN_MIC; |
Kunlei Zhang | d96af8f | 2019-08-27 16:33:29 +0800 | [diff] [blame] | 8270 | select_devices(adev, usecase->id); |
| 8271 | } |
Zhou Song | d6d7175 | 2019-05-21 18:08:51 +0800 | [diff] [blame] | 8272 | } |
| 8273 | } |
Ashish Jain | 1b9b30c | 2017-05-18 20:57:40 +0530 | [diff] [blame] | 8274 | } |
| 8275 | |
Ravi Kumar Alamanda | c3bc081 | 2014-09-08 15:34:32 -0700 | [diff] [blame] | 8276 | status = voice_set_parameters(adev, parms); |
| 8277 | if (status != 0) |
Shiv Maliyappanahalli | 3e064fd | 2013-12-16 15:54:40 -0800 | [diff] [blame] | 8278 | goto done; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 8279 | |
Ravi Kumar Alamanda | c3bc081 | 2014-09-08 15:34:32 -0700 | [diff] [blame] | 8280 | status = platform_set_parameters(adev->platform, parms); |
| 8281 | if (status != 0) |
Shiv Maliyappanahalli | 3e064fd | 2013-12-16 15:54:40 -0800 | [diff] [blame] | 8282 | goto done; |
| 8283 | |
Ravi Kumar Alamanda | c3bc081 | 2014-09-08 15:34:32 -0700 | [diff] [blame] | 8284 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value)); |
| 8285 | if (ret >= 0) { |
Vicky Sehrawat | e240e5d | 2014-08-12 17:17:04 -0700 | [diff] [blame] | 8286 | /* When set to false, HAL should disable EC and NS */ |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 8287 | if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0) |
| 8288 | adev->bluetooth_nrec = true; |
| 8289 | else |
| 8290 | adev->bluetooth_nrec = false; |
| 8291 | } |
| 8292 | |
Ravi Kumar Alamanda | c3bc081 | 2014-09-08 15:34:32 -0700 | [diff] [blame] | 8293 | ret = str_parms_get_str(parms, "screen_state", value, sizeof(value)); |
| 8294 | if (ret >= 0) { |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 8295 | if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0) |
| 8296 | adev->screen_off = false; |
| 8297 | else |
| 8298 | adev->screen_off = true; |
Quinn Male | 70f20f3 | 2019-06-26 16:50:26 -0700 | [diff] [blame] | 8299 | audio_extn_sound_trigger_update_screen_status(adev->screen_off); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 8300 | } |
| 8301 | |
Eric Laurent | 4b08413 | 2018-10-19 17:33:43 -0700 | [diff] [blame] | 8302 | ret = str_parms_get_int(parms, "rotation", &val); |
| 8303 | if (ret >= 0) { |
| 8304 | bool reverse_speakers = false; |
| 8305 | int camera_rotation = CAMERA_ROTATION_LANDSCAPE; |
| 8306 | switch (val) { |
| 8307 | // FIXME: note that the code below assumes that the speakers are in the correct placement |
| 8308 | // relative to the user when the device is rotated 90deg from its default rotation. This |
| 8309 | // assumption is device-specific, not platform-specific like this code. |
| 8310 | case 270: |
| 8311 | reverse_speakers = true; |
| 8312 | camera_rotation = CAMERA_ROTATION_INVERT_LANDSCAPE; |
| 8313 | break; |
| 8314 | case 0: |
| 8315 | case 180: |
| 8316 | camera_rotation = CAMERA_ROTATION_PORTRAIT; |
| 8317 | break; |
| 8318 | case 90: |
| 8319 | camera_rotation = CAMERA_ROTATION_LANDSCAPE; |
| 8320 | break; |
| 8321 | default: |
| 8322 | ALOGE("%s: unexpected rotation of %d", __func__, val); |
| 8323 | status = -EINVAL; |
Jean-Michel Trivi | c56336b | 2013-05-24 16:55:17 -0700 | [diff] [blame] | 8324 | } |
Eric Laurent | 4b08413 | 2018-10-19 17:33:43 -0700 | [diff] [blame] | 8325 | if (status == 0) { |
| 8326 | // check and set swap |
| 8327 | // - check if orientation changed and speaker active |
| 8328 | // - set rotation and cache the rotation value |
| 8329 | adev->camera_orientation = |
| 8330 | (adev->camera_orientation & ~CAMERA_ROTATION_MASK) | camera_rotation; |
| 8331 | if (!audio_extn_is_maxx_audio_enabled()) |
| 8332 | platform_check_and_set_swap_lr_channels(adev, reverse_speakers); |
| 8333 | } |
| 8334 | } |
Jean-Michel Trivi | c56336b | 2013-05-24 16:55:17 -0700 | [diff] [blame] | 8335 | |
Mingming Yin | 514a8bc | 2014-07-29 15:22:21 -0700 | [diff] [blame] | 8336 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_SCO_WB, value, sizeof(value)); |
| 8337 | if (ret >= 0) { |
| 8338 | if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0) |
| 8339 | adev->bt_wb_speech_enabled = true; |
| 8340 | else |
| 8341 | adev->bt_wb_speech_enabled = false; |
| 8342 | } |
| 8343 | |
Zhou Song | 12c2950 | 2019-03-16 10:37:18 +0800 | [diff] [blame] | 8344 | ret = str_parms_get_str(parms, "bt_swb", value, sizeof(value)); |
| 8345 | if (ret >= 0) { |
| 8346 | val = atoi(value); |
| 8347 | adev->swb_speech_mode = val; |
| 8348 | } |
| 8349 | |
Pradnya Chaphekar | 4403bd7 | 2014-09-09 09:50:01 -0700 | [diff] [blame] | 8350 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value, sizeof(value)); |
| 8351 | if (ret >= 0) { |
| 8352 | val = atoi(value); |
Satya Krishna Pindiproli | ce22796 | 2017-12-13 16:07:14 +0530 | [diff] [blame] | 8353 | audio_devices_t device = (audio_devices_t) val; |
Zhou Song | 681350a | 2017-10-19 16:28:42 +0800 | [diff] [blame] | 8354 | if (audio_is_output_device(val) && |
| 8355 | (val & AUDIO_DEVICE_OUT_AUX_DIGITAL)) { |
Shiv Maliyappanahalli | c065640 | 2016-09-03 14:13:26 -0700 | [diff] [blame] | 8356 | ALOGV("cache new ext disp type and edid"); |
Vignesh Kulothungan | 39fc6a2 | 2019-08-01 00:55:59 -0700 | [diff] [blame] | 8357 | platform_get_controller_stream_from_params(parms, &controller, &stream); |
| 8358 | platform_set_ext_display_device_v2(adev->platform, controller, stream); |
| 8359 | ret = platform_get_ext_disp_type_v2(adev->platform, controller, stream); |
Shiv Maliyappanahalli | c065640 | 2016-09-03 14:13:26 -0700 | [diff] [blame] | 8360 | if (ret < 0) { |
| 8361 | ALOGE("%s: Failed to query disp type, ret:%d", __func__, ret); |
Manisha Agarwal | 2f5ff88 | 2018-08-08 17:09:29 +0530 | [diff] [blame] | 8362 | } else { |
Vignesh Kulothungan | 39fc6a2 | 2019-08-01 00:55:59 -0700 | [diff] [blame] | 8363 | platform_cache_edid_v2(adev->platform, controller, stream); |
Shiv Maliyappanahalli | c065640 | 2016-09-03 14:13:26 -0700 | [diff] [blame] | 8364 | } |
Satya Krishna Pindiproli | ce22796 | 2017-12-13 16:07:14 +0530 | [diff] [blame] | 8365 | } else if (audio_is_usb_out_device(device) || audio_is_usb_in_device(device)) { |
vivek mehta | 344576a | 2016-04-12 18:56:03 -0700 | [diff] [blame] | 8366 | /* |
| 8367 | * Do not allow AFE proxy port usage by WFD source when USB headset is connected. |
| 8368 | * Per AudioPolicyManager, USB device is higher priority than WFD. |
| 8369 | * For Voice call over USB headset, voice call audio is routed to AFE proxy ports. |
| 8370 | * If WFD use case occupies AFE proxy, it may result unintended behavior while |
| 8371 | * starting voice call on USB |
| 8372 | */ |
Kuirong Wang | a9f7cee | 2016-03-07 11:21:52 -0800 | [diff] [blame] | 8373 | ret = str_parms_get_str(parms, "card", value, sizeof(value)); |
Satya Krishna Pindiproli | ce22796 | 2017-12-13 16:07:14 +0530 | [diff] [blame] | 8374 | if (ret >= 0) |
| 8375 | audio_extn_usb_add_device(device, atoi(value)); |
| 8376 | |
Zhou Song | 6f86282 | 2017-11-06 17:27:57 +0800 | [diff] [blame] | 8377 | if (!audio_extn_usb_is_tunnel_supported()) { |
| 8378 | ALOGV("detected USB connect .. disable proxy"); |
| 8379 | adev->allow_afe_proxy_usage = false; |
| 8380 | } |
Pradnya Chaphekar | 4403bd7 | 2014-09-09 09:50:01 -0700 | [diff] [blame] | 8381 | } |
| 8382 | } |
| 8383 | |
| 8384 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value, sizeof(value)); |
| 8385 | if (ret >= 0) { |
| 8386 | val = atoi(value); |
Satya Krishna Pindiproli | ce22796 | 2017-12-13 16:07:14 +0530 | [diff] [blame] | 8387 | audio_devices_t device = (audio_devices_t) val; |
Garmond Leung | e3b6d48 | 2016-10-25 16:48:01 -0700 | [diff] [blame] | 8388 | /* |
| 8389 | * The HDMI / Displayport disconnect handling has been moved to |
| 8390 | * audio extension to ensure that its parameters are not |
| 8391 | * invalidated prior to updating sysfs of the disconnect event |
| 8392 | * Invalidate will be handled by audio_extn_ext_disp_set_parameters() |
| 8393 | */ |
Satya Krishna Pindiproli | ce22796 | 2017-12-13 16:07:14 +0530 | [diff] [blame] | 8394 | if (audio_is_usb_out_device(device) || audio_is_usb_in_device(device)) { |
Kuirong Wang | a9f7cee | 2016-03-07 11:21:52 -0800 | [diff] [blame] | 8395 | ret = str_parms_get_str(parms, "card", value, sizeof(value)); |
Satya Krishna Pindiproli | ce22796 | 2017-12-13 16:07:14 +0530 | [diff] [blame] | 8396 | if (ret >= 0) |
| 8397 | audio_extn_usb_remove_device(device, atoi(value)); |
| 8398 | |
Zhou Song | 6f86282 | 2017-11-06 17:27:57 +0800 | [diff] [blame] | 8399 | if (!audio_extn_usb_is_tunnel_supported()) { |
| 8400 | ALOGV("detected USB disconnect .. enable proxy"); |
| 8401 | adev->allow_afe_proxy_usage = true; |
| 8402 | } |
Pradnya Chaphekar | 4403bd7 | 2014-09-09 09:50:01 -0700 | [diff] [blame] | 8403 | } |
Manisha Agarwal | c2188fb | 2019-05-27 10:00:29 +0530 | [diff] [blame] | 8404 | if (audio_is_a2dp_out_device(device)) { |
| 8405 | struct audio_usecase *usecase; |
| 8406 | struct listnode *node; |
| 8407 | list_for_each(node, &adev->usecase_list) { |
| 8408 | usecase = node_to_item(node, struct audio_usecase, list); |
| 8409 | if (PCM_PLAYBACK == usecase->type && usecase->stream.out && |
| 8410 | (usecase->stream.out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && |
| 8411 | usecase->stream.out->a2dp_compress_mute) { |
| 8412 | struct stream_out *out = usecase->stream.out; |
| 8413 | ALOGD("Unmuting the stream when Bt-A2dp disconnected and stream is mute"); |
| 8414 | out->a2dp_compress_mute = false; |
| 8415 | out_set_compr_volume(&out->stream, out->volume_l, out->volume_r); |
| 8416 | } |
| 8417 | } |
| 8418 | } |
Pradnya Chaphekar | 4403bd7 | 2014-09-09 09:50:01 -0700 | [diff] [blame] | 8419 | } |
| 8420 | |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 8421 | audio_extn_hfp_set_parameters(adev, parms); |
Arun Mirpuri | b1bec9c | 2019-01-29 16:42:45 -0800 | [diff] [blame] | 8422 | audio_extn_qdsp_set_parameters(adev, parms); |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 8423 | |
| 8424 | status = audio_extn_a2dp_set_parameters(parms, &a2dp_reconfig); |
Aniket Kumar Lata | 2330032 | 2019-02-20 22:25:30 -0800 | [diff] [blame] | 8425 | if (status >= 0 && a2dp_reconfig) { |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 8426 | struct audio_usecase *usecase; |
| 8427 | struct listnode *node; |
| 8428 | list_for_each(node, &adev->usecase_list) { |
| 8429 | usecase = node_to_item(node, struct audio_usecase, list); |
Sujin Panicker | 390724d | 2019-04-26 10:43:36 +0530 | [diff] [blame] | 8430 | if (usecase->stream.out && (usecase->type == PCM_PLAYBACK) && |
Naresh Tanniru | f7e9e63 | 2016-11-04 14:54:20 -0700 | [diff] [blame] | 8431 | (usecase->devices & AUDIO_DEVICE_OUT_ALL_A2DP)){ |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 8432 | ALOGD("reconfigure a2dp... forcing device switch"); |
Weiyin Jiang | 425180d | 2017-06-05 16:40:23 +0800 | [diff] [blame] | 8433 | pthread_mutex_unlock(&adev->lock); |
Naresh Tanniru | cd2353e | 2016-08-19 00:37:25 +0530 | [diff] [blame] | 8434 | lock_output_stream(usecase->stream.out); |
Weiyin Jiang | 425180d | 2017-06-05 16:40:23 +0800 | [diff] [blame] | 8435 | pthread_mutex_lock(&adev->lock); |
Naresh Tanniru | cd2353e | 2016-08-19 00:37:25 +0530 | [diff] [blame] | 8436 | audio_extn_a2dp_set_handoff_mode(true); |
Manisha Agarwal | f1c3b6b | 2019-06-25 13:00:33 +0530 | [diff] [blame] | 8437 | ALOGD("Switching to speaker and muting the stream before select_devices"); |
| 8438 | check_a2dp_restore_l(adev, usecase->stream.out, false); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 8439 | //force device switch to re configure encoder |
| 8440 | select_devices(adev, usecase->id); |
Manisha Agarwal | f1c3b6b | 2019-06-25 13:00:33 +0530 | [diff] [blame] | 8441 | ALOGD("Unmuting the stream after select_devices"); |
| 8442 | usecase->stream.out->a2dp_compress_mute = false; |
| 8443 | out_set_compr_volume(&usecase->stream.out->stream, usecase->stream.out->volume_l, usecase->stream.out->volume_r); |
Naresh Tanniru | cd2353e | 2016-08-19 00:37:25 +0530 | [diff] [blame] | 8444 | audio_extn_a2dp_set_handoff_mode(false); |
| 8445 | pthread_mutex_unlock(&usecase->stream.out->lock); |
Naresh Tanniru | 9d027a6 | 2015-03-13 01:32:10 +0530 | [diff] [blame] | 8446 | break; |
| 8447 | } |
| 8448 | } |
| 8449 | } |
Alexy Joseph | 5e4ccbc | 2017-02-21 14:20:12 -0800 | [diff] [blame] | 8450 | |
| 8451 | //handle vr audio setparam |
| 8452 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_VR_AUDIO_MODE, |
| 8453 | value, sizeof(value)); |
| 8454 | if (ret >= 0) { |
| 8455 | ALOGI("Setting vr mode to be %s", value); |
| 8456 | if (!strncmp(value, "true", 4)) { |
| 8457 | adev->vr_audio_mode_enabled = true; |
| 8458 | ALOGI("Setting vr mode to true"); |
| 8459 | } else if (!strncmp(value, "false", 5)) { |
| 8460 | adev->vr_audio_mode_enabled = false; |
| 8461 | ALOGI("Setting vr mode to false"); |
| 8462 | } else { |
| 8463 | ALOGI("wrong vr mode set"); |
| 8464 | } |
| 8465 | } |
| 8466 | |
Eric Laurent | 4b08413 | 2018-10-19 17:33:43 -0700 | [diff] [blame] | 8467 | //FIXME: to be replaced by proper video capture properties API |
| 8468 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_CAMERA_FACING, value, sizeof(value)); |
| 8469 | if (ret >= 0) { |
| 8470 | int camera_facing = CAMERA_FACING_BACK; |
| 8471 | if (strcmp(value, AUDIO_PARAMETER_VALUE_FRONT) == 0) |
| 8472 | camera_facing = CAMERA_FACING_FRONT; |
| 8473 | else if (strcmp(value, AUDIO_PARAMETER_VALUE_BACK) == 0) |
| 8474 | camera_facing = CAMERA_FACING_BACK; |
| 8475 | else { |
| 8476 | ALOGW("%s: invalid camera facing value: %s", __func__, value); |
| 8477 | goto done; |
| 8478 | } |
| 8479 | adev->camera_orientation = |
| 8480 | (adev->camera_orientation & ~CAMERA_FACING_MASK) | camera_facing; |
| 8481 | struct audio_usecase *usecase; |
| 8482 | struct listnode *node; |
| 8483 | list_for_each(node, &adev->usecase_list) { |
| 8484 | usecase = node_to_item(node, struct audio_usecase, list); |
| 8485 | struct stream_in *in = usecase->stream.in; |
| 8486 | if (usecase->type == PCM_CAPTURE && in != NULL && |
| 8487 | in->source == AUDIO_SOURCE_CAMCORDER && !in->standby) { |
| 8488 | select_devices(adev, in->usecase); |
| 8489 | } |
| 8490 | } |
| 8491 | } |
| 8492 | |
Naresh Tanniru | cd2353e | 2016-08-19 00:37:25 +0530 | [diff] [blame] | 8493 | audio_extn_set_parameters(adev, parms); |
Shiv Maliyappanahalli | 3e064fd | 2013-12-16 15:54:40 -0800 | [diff] [blame] | 8494 | done: |
| 8495 | str_parms_destroy(parms); |
Shiv Maliyappanahalli | 3bb7358 | 2013-11-05 12:49:15 -0800 | [diff] [blame] | 8496 | pthread_mutex_unlock(&adev->lock); |
Preetam Singh Ranawat | a5f32b4 | 2014-09-23 12:12:47 +0530 | [diff] [blame] | 8497 | error: |
Ravi Kumar Alamanda | c3bc081 | 2014-09-08 15:34:32 -0700 | [diff] [blame] | 8498 | ALOGV("%s: exit with code(%d)", __func__, status); |
| 8499 | return status; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 8500 | } |
| 8501 | |
| 8502 | static char* adev_get_parameters(const struct audio_hw_device *dev, |
| 8503 | const char *keys) |
| 8504 | { |
Sidipotu Ashok | aa4fa6a | 2018-12-21 09:19:26 +0530 | [diff] [blame] | 8505 | ALOGD("%s:%s", __func__, keys); |
| 8506 | |
Shiv Maliyappanahalli | 34b585f | 2013-10-01 15:49:05 -0700 | [diff] [blame] | 8507 | struct audio_device *adev = (struct audio_device *)dev; |
| 8508 | struct str_parms *reply = str_parms_create(); |
| 8509 | struct str_parms *query = str_parms_create_str(keys); |
| 8510 | char *str; |
Naresh Tanniru | d7205b6 | 2014-06-20 02:54:48 +0530 | [diff] [blame] | 8511 | char value[256] = {0}; |
| 8512 | int ret = 0; |
| 8513 | |
Haynes Mathew George | b51ceb1 | 2014-06-30 13:56:18 -0700 | [diff] [blame] | 8514 | if (!query || !reply) { |
Alexy Joseph | aee4fdd | 2016-01-29 13:02:07 -0800 | [diff] [blame] | 8515 | if (reply) { |
| 8516 | str_parms_destroy(reply); |
| 8517 | } |
| 8518 | if (query) { |
| 8519 | str_parms_destroy(query); |
| 8520 | } |
Haynes Mathew George | b51ceb1 | 2014-06-30 13:56:18 -0700 | [diff] [blame] | 8521 | ALOGE("adev_get_parameters: failed to create query or reply"); |
| 8522 | return NULL; |
| 8523 | } |
| 8524 | |
Alexy Joseph | 5e4ccbc | 2017-02-21 14:20:12 -0800 | [diff] [blame] | 8525 | //handle vr audio getparam |
| 8526 | |
| 8527 | ret = str_parms_get_str(query, |
| 8528 | AUDIO_PARAMETER_KEY_VR_AUDIO_MODE, |
| 8529 | value, sizeof(value)); |
| 8530 | |
| 8531 | if (ret >= 0) { |
| 8532 | bool vr_audio_enabled = false; |
| 8533 | pthread_mutex_lock(&adev->lock); |
| 8534 | vr_audio_enabled = adev->vr_audio_mode_enabled; |
| 8535 | pthread_mutex_unlock(&adev->lock); |
| 8536 | |
| 8537 | ALOGI("getting vr mode to %d", vr_audio_enabled); |
| 8538 | |
| 8539 | if (vr_audio_enabled) { |
| 8540 | str_parms_add_str(reply, AUDIO_PARAMETER_KEY_VR_AUDIO_MODE, |
| 8541 | "true"); |
| 8542 | goto exit; |
| 8543 | } else { |
| 8544 | str_parms_add_str(reply, AUDIO_PARAMETER_KEY_VR_AUDIO_MODE, |
| 8545 | "false"); |
| 8546 | goto exit; |
| 8547 | } |
| 8548 | } |
Shiv Maliyappanahalli | 34b585f | 2013-10-01 15:49:05 -0700 | [diff] [blame] | 8549 | |
Shiv Maliyappanahalli | 3bb7358 | 2013-11-05 12:49:15 -0800 | [diff] [blame] | 8550 | pthread_mutex_lock(&adev->lock); |
Shiv Maliyappanahalli | 34b585f | 2013-10-01 15:49:05 -0700 | [diff] [blame] | 8551 | audio_extn_get_parameters(adev, query, reply); |
Shiv Maliyappanahalli | f930849 | 2013-12-12 12:18:09 -0800 | [diff] [blame] | 8552 | voice_get_parameters(adev, query, reply); |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 8553 | audio_extn_a2dp_get_parameters(query, reply); |
Shiv Maliyappanahalli | 34b585f | 2013-10-01 15:49:05 -0700 | [diff] [blame] | 8554 | platform_get_parameters(adev->platform, query, reply); |
Naresh Tanniru | 8065983 | 2014-06-04 18:17:56 +0530 | [diff] [blame] | 8555 | pthread_mutex_unlock(&adev->lock); |
| 8556 | |
Naresh Tanniru | d7205b6 | 2014-06-20 02:54:48 +0530 | [diff] [blame] | 8557 | exit: |
Shiv Maliyappanahalli | 34b585f | 2013-10-01 15:49:05 -0700 | [diff] [blame] | 8558 | str = str_parms_to_str(reply); |
| 8559 | str_parms_destroy(query); |
| 8560 | str_parms_destroy(reply); |
| 8561 | |
Sidipotu Ashok | aa4fa6a | 2018-12-21 09:19:26 +0530 | [diff] [blame] | 8562 | ALOGD("%s: exit: returns - %s", __func__, str); |
Shiv Maliyappanahalli | 34b585f | 2013-10-01 15:49:05 -0700 | [diff] [blame] | 8563 | return str; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 8564 | } |
| 8565 | |
Ravi Kumar Alamanda | bdf1416 | 2014-09-05 16:14:17 -0700 | [diff] [blame] | 8566 | static int adev_init_check(const struct audio_hw_device *dev __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 8567 | { |
| 8568 | return 0; |
| 8569 | } |
| 8570 | |
| 8571 | static int adev_set_voice_volume(struct audio_hw_device *dev, float volume) |
| 8572 | { |
Haynes Mathew George | 5191a85 | 2013-09-11 14:19:36 -0700 | [diff] [blame] | 8573 | int ret; |
| 8574 | struct audio_device *adev = (struct audio_device *)dev; |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 8575 | |
| 8576 | audio_extn_extspk_set_voice_vol(adev->extspk, volume); |
| 8577 | |
Haynes Mathew George | 5191a85 | 2013-09-11 14:19:36 -0700 | [diff] [blame] | 8578 | pthread_mutex_lock(&adev->lock); |
| 8579 | /* cache volume */ |
Shruthi Krishna | ace1085 | 2013-10-25 14:32:12 -0700 | [diff] [blame] | 8580 | ret = voice_set_volume(adev, volume); |
Haynes Mathew George | 5191a85 | 2013-09-11 14:19:36 -0700 | [diff] [blame] | 8581 | pthread_mutex_unlock(&adev->lock); |
| 8582 | return ret; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 8583 | } |
| 8584 | |
Ravi Kumar Alamanda | bdf1416 | 2014-09-05 16:14:17 -0700 | [diff] [blame] | 8585 | static int adev_set_master_volume(struct audio_hw_device *dev __unused, |
| 8586 | float volume __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 8587 | { |
| 8588 | return -ENOSYS; |
| 8589 | } |
| 8590 | |
Ravi Kumar Alamanda | bdf1416 | 2014-09-05 16:14:17 -0700 | [diff] [blame] | 8591 | static int adev_get_master_volume(struct audio_hw_device *dev __unused, |
| 8592 | float *volume __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 8593 | { |
| 8594 | return -ENOSYS; |
| 8595 | } |
| 8596 | |
Ravi Kumar Alamanda | bdf1416 | 2014-09-05 16:14:17 -0700 | [diff] [blame] | 8597 | static int adev_set_master_mute(struct audio_hw_device *dev __unused, |
| 8598 | bool muted __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 8599 | { |
| 8600 | return -ENOSYS; |
| 8601 | } |
| 8602 | |
Ravi Kumar Alamanda | bdf1416 | 2014-09-05 16:14:17 -0700 | [diff] [blame] | 8603 | static int adev_get_master_mute(struct audio_hw_device *dev __unused, |
| 8604 | bool *muted __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 8605 | { |
| 8606 | return -ENOSYS; |
| 8607 | } |
| 8608 | |
| 8609 | static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode) |
| 8610 | { |
| 8611 | struct audio_device *adev = (struct audio_device *)dev; |
Garmond Leung | 5fd0b55 | 2018-04-17 11:56:12 -0700 | [diff] [blame] | 8612 | struct listnode *node; |
| 8613 | struct audio_usecase *usecase = NULL; |
| 8614 | int ret = 0; |
kunleiz | dc4af9d | 2017-05-04 12:15:35 +0800 | [diff] [blame] | 8615 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 8616 | pthread_mutex_lock(&adev->lock); |
| 8617 | if (adev->mode != mode) { |
Ravi Kumar Alamanda | 060bc5a | 2014-09-05 13:51:35 -0700 | [diff] [blame] | 8618 | ALOGD("%s: mode %d\n", __func__, mode); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 8619 | adev->mode = mode; |
Kunlei Zhang | 1d5c7f2 | 2019-05-21 14:25:57 +0800 | [diff] [blame] | 8620 | if (voice_is_in_call(adev) && |
| 8621 | (mode == AUDIO_MODE_NORMAL || |
| 8622 | (mode == AUDIO_MODE_IN_COMMUNICATION && !voice_is_call_state_active(adev)))) { |
Garmond Leung | 5fd0b55 | 2018-04-17 11:56:12 -0700 | [diff] [blame] | 8623 | list_for_each(node, &adev->usecase_list) { |
| 8624 | usecase = node_to_item(node, struct audio_usecase, list); |
| 8625 | if (usecase->type == VOICE_CALL) |
| 8626 | break; |
| 8627 | } |
| 8628 | if (usecase && |
| 8629 | audio_is_usb_out_device(usecase->out_snd_device & AUDIO_DEVICE_OUT_ALL_USB)) { |
| 8630 | ret = audio_extn_usb_check_and_set_svc_int(usecase, |
| 8631 | true); |
| 8632 | if (ret != 0) { |
| 8633 | /* default service interval was successfully updated, |
| 8634 | reopen USB backend with new service interval */ |
| 8635 | check_usecases_codec_backend(adev, |
| 8636 | usecase, |
| 8637 | usecase->out_snd_device); |
| 8638 | } |
| 8639 | } |
| 8640 | |
Ravi Kumar Alamanda | 060bc5a | 2014-09-05 13:51:35 -0700 | [diff] [blame] | 8641 | voice_stop_call(adev); |
Banajit Goswami | 20cdd21 | 2015-09-11 01:11:30 -0700 | [diff] [blame] | 8642 | platform_set_gsm_mode(adev->platform, false); |
Ravi Kumar Alamanda | 060bc5a | 2014-09-05 13:51:35 -0700 | [diff] [blame] | 8643 | adev->current_call_output = NULL; |
kunleiz | dc4af9d | 2017-05-04 12:15:35 +0800 | [diff] [blame] | 8644 | // restore device for other active usecases after stop call |
| 8645 | list_for_each(node, &adev->usecase_list) { |
| 8646 | usecase = node_to_item(node, struct audio_usecase, list); |
| 8647 | select_devices(adev, usecase->id); |
| 8648 | } |
Ravi Kumar Alamanda | 060bc5a | 2014-09-05 13:51:35 -0700 | [diff] [blame] | 8649 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 8650 | } |
| 8651 | pthread_mutex_unlock(&adev->lock); |
| 8652 | return 0; |
| 8653 | } |
| 8654 | |
| 8655 | static int adev_set_mic_mute(struct audio_hw_device *dev, bool state) |
| 8656 | { |
Shiv Maliyappanahalli | 3bb7358 | 2013-11-05 12:49:15 -0800 | [diff] [blame] | 8657 | int ret; |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 8658 | struct audio_device *adev = (struct audio_device *)dev; |
Shiv Maliyappanahalli | 3bb7358 | 2013-11-05 12:49:15 -0800 | [diff] [blame] | 8659 | |
| 8660 | pthread_mutex_lock(&adev->lock); |
Vidyakumar Athota | 2850d53 | 2013-11-19 16:02:12 -0800 | [diff] [blame] | 8661 | ALOGD("%s state %d\n", __func__, state); |
Shiv Maliyappanahalli | 3bb7358 | 2013-11-05 12:49:15 -0800 | [diff] [blame] | 8662 | ret = voice_set_mic_mute((struct audio_device *)dev, state); |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 8663 | |
Derek Chen | d253007 | 2014-11-24 12:39:14 -0800 | [diff] [blame] | 8664 | if (adev->ext_hw_plugin) |
| 8665 | ret = audio_extn_ext_hw_plugin_set_mic_mute(adev->ext_hw_plugin, state); |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 8666 | |
| 8667 | adev->mic_muted = state; |
Shiv Maliyappanahalli | 3bb7358 | 2013-11-05 12:49:15 -0800 | [diff] [blame] | 8668 | pthread_mutex_unlock(&adev->lock); |
| 8669 | |
| 8670 | return ret; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 8671 | } |
| 8672 | |
| 8673 | static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state) |
| 8674 | { |
Shiv Maliyappanahalli | 34b585f | 2013-10-01 15:49:05 -0700 | [diff] [blame] | 8675 | *state = voice_get_mic_mute((struct audio_device *)dev); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 8676 | return 0; |
| 8677 | } |
| 8678 | |
Ravi Kumar Alamanda | bdf1416 | 2014-09-05 16:14:17 -0700 | [diff] [blame] | 8679 | static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev __unused, |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 8680 | const struct audio_config *config) |
| 8681 | { |
Ravi Kumar Alamanda | c3bc081 | 2014-09-08 15:34:32 -0700 | [diff] [blame] | 8682 | int channel_count = audio_channel_count_from_in_mask(config->channel_mask); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 8683 | |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 8684 | /* Don't know if USB HIFI in this context so use true to be conservative */ |
| 8685 | if (check_input_parameters(config->sample_rate, config->format, channel_count, |
| 8686 | true /*is_usb_hifi */) != 0) |
| 8687 | return 0; |
| 8688 | |
Ravi Kumar Alamanda | dc9bb15 | 2014-09-08 15:59:58 -0700 | [diff] [blame] | 8689 | return get_input_buffer_size(config->sample_rate, config->format, channel_count, |
| 8690 | false /* is_low_latency: since we don't know, be conservative */); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 8691 | } |
| 8692 | |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 8693 | static bool adev_input_allow_hifi_record(struct audio_device *adev, |
| 8694 | audio_devices_t devices, |
| 8695 | audio_input_flags_t flags, |
| 8696 | audio_source_t source) { |
| 8697 | const bool allowed = true; |
| 8698 | |
| 8699 | if (!audio_is_usb_in_device(devices)) |
| 8700 | return !allowed; |
| 8701 | |
| 8702 | switch (flags) { |
| 8703 | case AUDIO_INPUT_FLAG_NONE: |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 8704 | break; |
Haynes Mathew George | 5986218 | 2017-10-24 16:23:57 -0700 | [diff] [blame] | 8705 | case AUDIO_INPUT_FLAG_FAST: // disallow hifi record for FAST as |
| 8706 | // it affects RTD numbers over USB |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 8707 | default: |
| 8708 | return !allowed; |
| 8709 | } |
| 8710 | |
| 8711 | switch (source) { |
| 8712 | case AUDIO_SOURCE_DEFAULT: |
| 8713 | case AUDIO_SOURCE_MIC: |
| 8714 | case AUDIO_SOURCE_UNPROCESSED: |
| 8715 | break; |
| 8716 | default: |
| 8717 | return !allowed; |
| 8718 | } |
| 8719 | |
| 8720 | switch (adev->mode) { |
| 8721 | case 0: |
| 8722 | break; |
| 8723 | default: |
| 8724 | return !allowed; |
| 8725 | } |
| 8726 | |
| 8727 | return allowed; |
| 8728 | } |
| 8729 | |
Haynes Mathew George | 4ffef29 | 2017-11-21 15:08:02 -0800 | [diff] [blame] | 8730 | static int adev_update_voice_comm_input_stream(struct stream_in *in, |
| 8731 | struct audio_config *config) |
| 8732 | { |
| 8733 | bool valid_rate = (config->sample_rate == 8000 || |
| 8734 | config->sample_rate == 16000 || |
| 8735 | config->sample_rate == 32000 || |
| 8736 | config->sample_rate == 48000); |
| 8737 | bool valid_ch = audio_channel_count_from_in_mask(in->channel_mask) == 1; |
| 8738 | |
Arun Mirpuri | b1bec9c | 2019-01-29 16:42:45 -0800 | [diff] [blame] | 8739 | if(!voice_extn_is_compress_voip_supported()) { |
kunleiz | 28c73e7 | 2019-03-27 17:24:04 +0800 | [diff] [blame] | 8740 | if (valid_rate && valid_ch) { |
Haynes Mathew George | 4ffef29 | 2017-11-21 15:08:02 -0800 | [diff] [blame] | 8741 | in->usecase = USECASE_AUDIO_RECORD_VOIP; |
| 8742 | in->config = default_pcm_config_voip_copp; |
| 8743 | in->config.period_size = VOIP_IO_BUF_SIZE(in->sample_rate, |
| 8744 | DEFAULT_VOIP_BUF_DURATION_MS, |
| 8745 | DEFAULT_VOIP_BIT_DEPTH_BYTE)/2; |
Arun Mirpuri | b1bec9c | 2019-01-29 16:42:45 -0800 | [diff] [blame] | 8746 | } else { |
| 8747 | ALOGW("%s No valid input in voip, use defaults" |
| 8748 | "sample rate %u, channel mask 0x%X", |
| 8749 | __func__, config->sample_rate, in->channel_mask); |
| 8750 | } |
Haynes Mathew George | 4ffef29 | 2017-11-21 15:08:02 -0800 | [diff] [blame] | 8751 | in->config.rate = config->sample_rate; |
| 8752 | in->sample_rate = config->sample_rate; |
| 8753 | } else { |
Arun Mirpuri | b1bec9c | 2019-01-29 16:42:45 -0800 | [diff] [blame] | 8754 | //XXX needed for voice_extn_compress_voip_open_input_stream |
| 8755 | in->config.rate = config->sample_rate; |
| 8756 | if ((in->dev->mode == AUDIO_MODE_IN_COMMUNICATION || |
Shalini Manjunatha | a763cc4 | 2019-08-23 15:13:46 +0530 | [diff] [blame] | 8757 | in->source == AUDIO_SOURCE_VOICE_COMMUNICATION || |
Arun Mirpuri | b1bec9c | 2019-01-29 16:42:45 -0800 | [diff] [blame] | 8758 | voice_extn_compress_voip_is_active(in->dev)) && |
| 8759 | (voice_extn_compress_voip_is_format_supported(in->format)) && |
| 8760 | valid_rate && valid_ch) { |
| 8761 | voice_extn_compress_voip_open_input_stream(in); |
| 8762 | // update rate entries to match config from AF |
| 8763 | in->config.rate = config->sample_rate; |
| 8764 | in->sample_rate = config->sample_rate; |
| 8765 | } else { |
| 8766 | ALOGW("%s compress voip not active, use defaults", __func__); |
| 8767 | } |
Haynes Mathew George | 4ffef29 | 2017-11-21 15:08:02 -0800 | [diff] [blame] | 8768 | } |
Haynes Mathew George | 4ffef29 | 2017-11-21 15:08:02 -0800 | [diff] [blame] | 8769 | return 0; |
| 8770 | } |
| 8771 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 8772 | static int adev_open_input_stream(struct audio_hw_device *dev, |
Bharath Ramachandramurthy | 76d2089 | 2015-04-27 15:47:55 -0700 | [diff] [blame] | 8773 | audio_io_handle_t handle, |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 8774 | audio_devices_t devices, |
| 8775 | struct audio_config *config, |
Ravi Kumar Alamanda | c3bc081 | 2014-09-08 15:34:32 -0700 | [diff] [blame] | 8776 | struct audio_stream_in **stream_in, |
Dhananjay Kumar | ee4d200 | 2016-10-25 18:02:58 +0530 | [diff] [blame] | 8777 | audio_input_flags_t flags, |
Ravi Kumar Alamanda | c3bc081 | 2014-09-08 15:34:32 -0700 | [diff] [blame] | 8778 | const char *address __unused, |
Vidyakumar Athota | 5c39821 | 2015-03-31 21:53:21 -0700 | [diff] [blame] | 8779 | audio_source_t source) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 8780 | { |
| 8781 | struct audio_device *adev = (struct audio_device *)dev; |
| 8782 | struct stream_in *in; |
Ravi Kumar Alamanda | fae4211 | 2013-11-07 23:31:54 -0800 | [diff] [blame] | 8783 | int ret = 0, buffer_size, frame_size; |
Ravi Kumar Alamanda | c3bc081 | 2014-09-08 15:34:32 -0700 | [diff] [blame] | 8784 | int channel_count = audio_channel_count_from_in_mask(config->channel_mask); |
Ravi Kumar Alamanda | dc9bb15 | 2014-09-08 15:59:58 -0700 | [diff] [blame] | 8785 | bool is_low_latency = false; |
Divya Narayanan Poojary | 45f1919 | 2016-09-30 18:52:13 +0530 | [diff] [blame] | 8786 | bool channel_mask_updated = false; |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 8787 | bool is_usb_dev = audio_is_usb_in_device(devices); |
| 8788 | bool may_use_hifi_record = adev_input_allow_hifi_record(adev, |
| 8789 | devices, |
| 8790 | flags, |
| 8791 | source); |
Andy Hung | 9432060 | 2018-10-29 18:31:12 -0700 | [diff] [blame] | 8792 | ALOGV("%s: enter: flags %#x, is_usb_dev %d, may_use_hifi_record %d," |
| 8793 | " sample_rate %u, channel_mask %#x, format %#x", |
| 8794 | __func__, flags, is_usb_dev, may_use_hifi_record, |
| 8795 | config->sample_rate, config->channel_mask, config->format); |
Sidipotu Ashok | f43018c | 2014-05-02 16:21:50 +0530 | [diff] [blame] | 8796 | |
kunleiz | dff872d | 2018-08-20 14:40:33 +0800 | [diff] [blame] | 8797 | if (is_usb_dev && (!audio_extn_usb_connected(NULL))) { |
kunleiz | d6a9e0c | 2018-07-30 15:38:52 +0800 | [diff] [blame] | 8798 | is_usb_dev = false; |
| 8799 | devices = AUDIO_DEVICE_IN_BUILTIN_MIC; |
| 8800 | ALOGW("%s: ignore set device to non existing USB card, use input device(%#x)", |
| 8801 | __func__, devices); |
| 8802 | } |
| 8803 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 8804 | *stream_in = NULL; |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 8805 | |
| 8806 | if (!(is_usb_dev && may_use_hifi_record)) { |
| 8807 | if (config->sample_rate == 0) |
| 8808 | config->sample_rate = 48000; |
| 8809 | if (config->channel_mask == AUDIO_CHANNEL_NONE) |
| 8810 | config->channel_mask = AUDIO_CHANNEL_IN_MONO; |
| 8811 | if (config->format == AUDIO_FORMAT_DEFAULT) |
| 8812 | config->format = AUDIO_FORMAT_PCM_16_BIT; |
| 8813 | |
| 8814 | channel_count = audio_channel_count_from_in_mask(config->channel_mask); |
| 8815 | |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 8816 | if (check_input_parameters(config->sample_rate, config->format, channel_count, |
| 8817 | false) != 0) |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 8818 | return -EINVAL; |
Chaithanya Krishna Bacharaju | 9955b16 | 2016-05-25 16:25:53 +0530 | [diff] [blame] | 8819 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 8820 | |
Rahul Sharma | 9977098 | 2019-03-06 17:05:26 +0530 | [diff] [blame] | 8821 | pthread_mutex_lock(&adev->lock); |
| 8822 | if (in_get_stream(adev, handle) != NULL) { |
| 8823 | ALOGW("%s, input stream already opened", __func__); |
| 8824 | ret = -EEXIST; |
| 8825 | } |
| 8826 | pthread_mutex_unlock(&adev->lock); |
| 8827 | if (ret) |
| 8828 | return ret; |
| 8829 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 8830 | in = (struct stream_in *)calloc(1, sizeof(struct stream_in)); |
Haynes Mathew George | b51ceb1 | 2014-06-30 13:56:18 -0700 | [diff] [blame] | 8831 | |
| 8832 | if (!in) { |
| 8833 | ALOGE("failed to allocate input stream"); |
| 8834 | return -ENOMEM; |
| 8835 | } |
| 8836 | |
Sidipotu Ashok | f43018c | 2014-05-02 16:21:50 +0530 | [diff] [blame] | 8837 | ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x)\ |
Manish Dewangan | ba9fcfa | 2016-03-24 16:20:06 +0530 | [diff] [blame] | 8838 | stream_handle(%p) io_handle(%d) source(%d) format %x",__func__, config->sample_rate, |
| 8839 | config->channel_mask, devices, &in->stream, handle, source, config->format); |
Ravi Kumar Alamanda | 4070310 | 2014-04-24 10:34:41 -0700 | [diff] [blame] | 8840 | pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL); |
Shiv Maliyappanahalli | 736d4ce | 2015-09-28 15:23:06 -0700 | [diff] [blame] | 8841 | pthread_mutex_init(&in->pre_lock, (const pthread_mutexattr_t *) NULL); |
Ravi Kumar Alamanda | 4070310 | 2014-04-24 10:34:41 -0700 | [diff] [blame] | 8842 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 8843 | in->stream.common.get_sample_rate = in_get_sample_rate; |
| 8844 | in->stream.common.set_sample_rate = in_set_sample_rate; |
| 8845 | in->stream.common.get_buffer_size = in_get_buffer_size; |
| 8846 | in->stream.common.get_channels = in_get_channels; |
| 8847 | in->stream.common.get_format = in_get_format; |
| 8848 | in->stream.common.set_format = in_set_format; |
| 8849 | in->stream.common.standby = in_standby; |
| 8850 | in->stream.common.dump = in_dump; |
| 8851 | in->stream.common.set_parameters = in_set_parameters; |
| 8852 | in->stream.common.get_parameters = in_get_parameters; |
| 8853 | in->stream.common.add_audio_effect = in_add_audio_effect; |
| 8854 | in->stream.common.remove_audio_effect = in_remove_audio_effect; |
| 8855 | in->stream.set_gain = in_set_gain; |
| 8856 | in->stream.read = in_read; |
| 8857 | in->stream.get_input_frames_lost = in_get_input_frames_lost; |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 8858 | in->stream.get_capture_position = in_get_capture_position; |
Naresh Tanniru | dcb47c5 | 2018-06-25 16:23:32 +0530 | [diff] [blame] | 8859 | in->stream.get_active_microphones = in_get_active_microphones; |
Paul McLean | a50b733 | 2018-12-17 08:24:21 -0700 | [diff] [blame] | 8860 | in->stream.set_microphone_direction = in_set_microphone_direction; |
| 8861 | in->stream.set_microphone_field_dimension = in_set_microphone_field_dimension; |
juyuchen | db308c2 | 2019-01-21 11:57:17 -0700 | [diff] [blame] | 8862 | in->stream.update_sink_metadata = in_update_sink_metadata; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 8863 | |
| 8864 | in->device = devices; |
Vidyakumar Athota | 5c39821 | 2015-03-31 21:53:21 -0700 | [diff] [blame] | 8865 | in->source = source; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 8866 | in->dev = adev; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 8867 | in->standby = 1; |
Ravi Kumar Alamanda | 8fa6b19 | 2014-09-09 16:06:42 -0700 | [diff] [blame] | 8868 | in->capture_handle = handle; |
Ravi Kumar Alamanda | 8a0f977 | 2015-06-15 10:35:19 -0700 | [diff] [blame] | 8869 | in->flags = flags; |
Haynes Mathew George | 4674047 | 2017-10-27 18:40:12 -0700 | [diff] [blame] | 8870 | in->bit_width = 16; |
| 8871 | in->af_period_multiplier = 1; |
justinweng | 20fb6d8 | 2019-02-21 18:49:00 -0700 | [diff] [blame] | 8872 | in->direction = MIC_DIRECTION_UNSPECIFIED; |
| 8873 | in->zoom = 0; |
Carter Hsu | 2e429db | 2019-05-14 18:50:52 +0800 | [diff] [blame] | 8874 | list_init(&in->aec_list); |
| 8875 | list_init(&in->ns_list); |
Phil Burk | d898ba6 | 2019-06-20 12:49:01 -0700 | [diff] [blame] | 8876 | in->mmap_shared_memory_fd = -1; // not open |
Haynes Mathew George | 4674047 | 2017-10-27 18:40:12 -0700 | [diff] [blame] | 8877 | |
Andy Hung | 9432060 | 2018-10-29 18:31:12 -0700 | [diff] [blame] | 8878 | ALOGV("%s: source %d, config->channel_mask %#x", __func__, source, config->channel_mask); |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 8879 | if (source == AUDIO_SOURCE_VOICE_UPLINK || |
| 8880 | source == AUDIO_SOURCE_VOICE_DOWNLINK) { |
| 8881 | /* Force channel config requested to mono if incall |
| 8882 | record is being requested for only uplink/downlink */ |
| 8883 | if (config->channel_mask != AUDIO_CHANNEL_IN_MONO) { |
| 8884 | config->channel_mask = AUDIO_CHANNEL_IN_MONO; |
| 8885 | ret = -EINVAL; |
| 8886 | goto err_open; |
| 8887 | } |
| 8888 | } |
| 8889 | |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 8890 | if (is_usb_dev && may_use_hifi_record) { |
| 8891 | /* HiFi record selects an appropriate format, channel, rate combo |
| 8892 | depending on sink capabilities*/ |
| 8893 | ret = read_usb_sup_params_and_compare(false /*is_playback*/, |
| 8894 | &config->format, |
| 8895 | &in->supported_formats[0], |
| 8896 | MAX_SUPPORTED_FORMATS, |
| 8897 | &config->channel_mask, |
| 8898 | &in->supported_channel_masks[0], |
| 8899 | MAX_SUPPORTED_CHANNEL_MASKS, |
| 8900 | &config->sample_rate, |
| 8901 | &in->supported_sample_rates[0], |
| 8902 | MAX_SUPPORTED_SAMPLE_RATES); |
| 8903 | if (ret != 0) { |
| 8904 | ret = -EINVAL; |
| 8905 | goto err_open; |
| 8906 | } |
| 8907 | channel_count = audio_channel_count_from_in_mask(config->channel_mask); |
Haynes Mathew George | 4ffef29 | 2017-11-21 15:08:02 -0800 | [diff] [blame] | 8908 | } else if (config->format == AUDIO_FORMAT_DEFAULT) { |
Dhananjay Kumar | ee4d200 | 2016-10-25 18:02:58 +0530 | [diff] [blame] | 8909 | config->format = AUDIO_FORMAT_PCM_16_BIT; |
Surendar karka | aca3d08 | 2017-11-09 15:18:37 +0530 | [diff] [blame] | 8910 | } else if (property_get_bool("vendor.audio.capture.pcm.32bit.enable", false) |
| 8911 | && config->format == AUDIO_FORMAT_PCM_32_BIT) { |
| 8912 | in->config.format = PCM_FORMAT_S32_LE; |
| 8913 | in->bit_width = 32; |
Dhananjay Kumar | ee4d200 | 2016-10-25 18:02:58 +0530 | [diff] [blame] | 8914 | } else if ((config->format == AUDIO_FORMAT_PCM_FLOAT) || |
| 8915 | (config->format == AUDIO_FORMAT_PCM_32_BIT) || |
| 8916 | (config->format == AUDIO_FORMAT_PCM_24_BIT_PACKED) || |
| 8917 | (config->format == AUDIO_FORMAT_PCM_8_24_BIT)) { |
| 8918 | bool ret_error = false; |
| 8919 | in->bit_width = 24; |
| 8920 | /* 24 bit is restricted to UNPROCESSED source only,also format supported |
| 8921 | from HAL is 24_packed and 8_24 |
| 8922 | *> In case of UNPROCESSED source, for 24 bit, if format requested is other than |
| 8923 | 24_packed return error indicating supported format is 24_packed |
| 8924 | *> In case of any other source requesting 24 bit or float return error |
| 8925 | indicating format supported is 16 bit only. |
| 8926 | |
| 8927 | on error flinger will retry with supported format passed |
| 8928 | */ |
| 8929 | if ((source != AUDIO_SOURCE_UNPROCESSED) && |
| 8930 | (source != AUDIO_SOURCE_CAMCORDER)) { |
| 8931 | config->format = AUDIO_FORMAT_PCM_16_BIT; |
| 8932 | if (config->sample_rate > 48000) |
| 8933 | config->sample_rate = 48000; |
| 8934 | ret_error = true; |
Haynes Mathew George | 4674047 | 2017-10-27 18:40:12 -0700 | [diff] [blame] | 8935 | } else if (!(config->format == AUDIO_FORMAT_PCM_24_BIT_PACKED || |
| 8936 | config->format == AUDIO_FORMAT_PCM_8_24_BIT)) { |
Dhananjay Kumar | ee4d200 | 2016-10-25 18:02:58 +0530 | [diff] [blame] | 8937 | config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED; |
| 8938 | ret_error = true; |
| 8939 | } |
| 8940 | |
| 8941 | if (ret_error) { |
| 8942 | ret = -EINVAL; |
| 8943 | goto err_open; |
| 8944 | } |
| 8945 | } |
| 8946 | |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 8947 | in->channel_mask = config->channel_mask; |
| 8948 | in->format = config->format; |
| 8949 | |
| 8950 | in->usecase = USECASE_AUDIO_RECORD; |
Samyak Jain | 0aa07ab | 2019-04-04 14:36:32 +0530 | [diff] [blame] | 8951 | |
| 8952 | if (in->source == AUDIO_SOURCE_FM_TUNER) { |
| 8953 | if(!get_usecase_from_list(adev, USECASE_AUDIO_RECORD_FM_VIRTUAL)) |
| 8954 | in->usecase = USECASE_AUDIO_RECORD_FM_VIRTUAL; |
| 8955 | else { |
| 8956 | ret = -EINVAL; |
| 8957 | goto err_open; |
| 8958 | } |
| 8959 | } |
| 8960 | |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 8961 | if (config->sample_rate == LOW_LATENCY_CAPTURE_SAMPLE_RATE && |
Deeraj Soman | fa377bf | 2019-02-06 12:57:59 +0530 | [diff] [blame] | 8962 | (flags & AUDIO_INPUT_FLAG_TIMESTAMP) == 0 && |
| 8963 | (flags & AUDIO_INPUT_FLAG_COMPRESS) == 0 && |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 8964 | (flags & AUDIO_INPUT_FLAG_FAST) != 0) { |
| 8965 | is_low_latency = true; |
| 8966 | #if LOW_LATENCY_CAPTURE_USE_CASE |
| 8967 | in->usecase = USECASE_AUDIO_RECORD_LOW_LATENCY; |
| 8968 | #endif |
| 8969 | in->realtime = may_use_noirq_mode(adev, in->usecase, in->flags); |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 8970 | if (!in->realtime) { |
| 8971 | in->config = pcm_config_audio_capture; |
| 8972 | frame_size = audio_stream_in_frame_size(&in->stream); |
| 8973 | buffer_size = get_input_buffer_size(config->sample_rate, |
| 8974 | config->format, |
| 8975 | channel_count, |
| 8976 | is_low_latency); |
| 8977 | in->config.period_size = buffer_size / frame_size; |
| 8978 | in->config.rate = config->sample_rate; |
| 8979 | in->af_period_multiplier = 1; |
| 8980 | } else { |
| 8981 | // period size is left untouched for rt mode playback |
| 8982 | in->config = pcm_config_audio_capture_rt; |
| 8983 | in->af_period_multiplier = af_period_multiplier; |
| 8984 | } |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 8985 | } |
| 8986 | |
| 8987 | if ((config->sample_rate == LOW_LATENCY_CAPTURE_SAMPLE_RATE) && |
| 8988 | ((in->flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0)) { |
| 8989 | in->realtime = 0; |
| 8990 | in->usecase = USECASE_AUDIO_RECORD_MMAP; |
| 8991 | in->config = pcm_config_mmap_capture; |
Haynes Mathew George | 4674047 | 2017-10-27 18:40:12 -0700 | [diff] [blame] | 8992 | in->config.format = pcm_format_from_audio_format(config->format); |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 8993 | in->stream.start = in_start; |
| 8994 | in->stream.stop = in_stop; |
| 8995 | in->stream.create_mmap_buffer = in_create_mmap_buffer; |
| 8996 | in->stream.get_mmap_position = in_get_mmap_position; |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 8997 | ALOGV("%s: USECASE_AUDIO_RECORD_MMAP", __func__); |
| 8998 | } else if (in->realtime) { |
| 8999 | in->config = pcm_config_audio_capture_rt; |
Haynes Mathew George | 4674047 | 2017-10-27 18:40:12 -0700 | [diff] [blame] | 9000 | in->config.format = pcm_format_from_audio_format(config->format); |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 9001 | in->af_period_multiplier = af_period_multiplier; |
Haynes Mathew George | 4674047 | 2017-10-27 18:40:12 -0700 | [diff] [blame] | 9002 | } else if (is_usb_dev && may_use_hifi_record) { |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 9003 | in->usecase = USECASE_AUDIO_RECORD_HIFI; |
| 9004 | in->config = pcm_config_audio_capture; |
| 9005 | frame_size = audio_stream_in_frame_size(&in->stream); |
| 9006 | buffer_size = get_input_buffer_size(config->sample_rate, |
| 9007 | config->format, |
| 9008 | channel_count, |
| 9009 | false /*is_low_latency*/); |
| 9010 | in->config.period_size = buffer_size / frame_size; |
| 9011 | in->config.rate = config->sample_rate; |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 9012 | in->config.format = pcm_format_from_audio_format(config->format); |
Karthikeyan Mani | 07faa60 | 2018-08-20 11:01:32 -0700 | [diff] [blame] | 9013 | switch (config->format) { |
| 9014 | case AUDIO_FORMAT_PCM_32_BIT: |
| 9015 | in->bit_width = 32; |
| 9016 | break; |
| 9017 | case AUDIO_FORMAT_PCM_24_BIT_PACKED: |
| 9018 | case AUDIO_FORMAT_PCM_8_24_BIT: |
| 9019 | in->bit_width = 24; |
| 9020 | break; |
| 9021 | default: |
| 9022 | in->bit_width = 16; |
| 9023 | } |
Haynes Mathew George | 484e8d2 | 2017-07-31 18:55:17 -0700 | [diff] [blame] | 9024 | } else if ((in->device == AUDIO_DEVICE_IN_TELEPHONY_RX) || |
Dhanalakshmi Siddani | 0b1488e | 2016-09-06 12:58:42 +0530 | [diff] [blame] | 9025 | (in->device == AUDIO_DEVICE_IN_PROXY)) { |
Ravi Kumar Alamanda | 060bc5a | 2014-09-05 13:51:35 -0700 | [diff] [blame] | 9026 | if (config->sample_rate == 0) |
| 9027 | config->sample_rate = AFE_PROXY_SAMPLING_RATE; |
| 9028 | if (config->sample_rate != 48000 && config->sample_rate != 16000 && |
| 9029 | config->sample_rate != 8000) { |
| 9030 | config->sample_rate = AFE_PROXY_SAMPLING_RATE; |
| 9031 | ret = -EINVAL; |
| 9032 | goto err_open; |
| 9033 | } |
| 9034 | if (config->format == AUDIO_FORMAT_DEFAULT) |
| 9035 | config->format = AUDIO_FORMAT_PCM_16_BIT; |
| 9036 | if (config->format != AUDIO_FORMAT_PCM_16_BIT) { |
| 9037 | config->format = AUDIO_FORMAT_PCM_16_BIT; |
| 9038 | ret = -EINVAL; |
| 9039 | goto err_open; |
| 9040 | } |
| 9041 | |
| 9042 | in->usecase = USECASE_AUDIO_RECORD_AFE_PROXY; |
| 9043 | in->config = pcm_config_afe_proxy_record; |
Ravi Kumar Alamanda | 060bc5a | 2014-09-05 13:51:35 -0700 | [diff] [blame] | 9044 | in->config.rate = config->sample_rate; |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 9045 | in->af_period_multiplier = 1; |
| 9046 | } else if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION && |
| 9047 | in->flags & AUDIO_INPUT_FLAG_VOIP_TX && |
| 9048 | (config->sample_rate == 8000 || |
| 9049 | config->sample_rate == 16000 || |
| 9050 | config->sample_rate == 32000 || |
| 9051 | config->sample_rate == 48000) && |
| 9052 | channel_count == 1) { |
| 9053 | in->usecase = USECASE_AUDIO_RECORD_VOIP; |
| 9054 | in->config = pcm_config_audio_capture; |
| 9055 | frame_size = audio_stream_in_frame_size(&in->stream); |
| 9056 | buffer_size = get_stream_buffer_size(VOIP_CAPTURE_PERIOD_DURATION_MSEC, |
| 9057 | config->sample_rate, |
| 9058 | config->format, |
| 9059 | channel_count, false /*is_low_latency*/); |
| 9060 | in->config.period_size = buffer_size / frame_size; |
| 9061 | in->config.period_count = VOIP_CAPTURE_PERIOD_COUNT; |
| 9062 | in->config.rate = config->sample_rate; |
| 9063 | in->af_period_multiplier = 1; |
Haynes Mathew George | 4674047 | 2017-10-27 18:40:12 -0700 | [diff] [blame] | 9064 | } else { |
Revathi Uddaraju | d263403 | 2017-12-07 14:42:34 +0530 | [diff] [blame] | 9065 | int ret_val; |
| 9066 | pthread_mutex_lock(&adev->lock); |
| 9067 | ret_val = audio_extn_check_and_set_multichannel_usecase(adev, |
| 9068 | in, config, &channel_mask_updated); |
| 9069 | pthread_mutex_unlock(&adev->lock); |
| 9070 | |
| 9071 | if (!ret_val) { |
| 9072 | if (channel_mask_updated == true) { |
| 9073 | ALOGD("%s: return error to retry with updated channel mask (%#x)", |
| 9074 | __func__, config->channel_mask); |
| 9075 | ret = -EINVAL; |
| 9076 | goto err_open; |
| 9077 | } |
| 9078 | ALOGD("%s: created multi-channel session succesfully",__func__); |
| 9079 | } else if (audio_extn_compr_cap_enabled() && |
| 9080 | audio_extn_compr_cap_format_supported(config->format) && |
| 9081 | (in->dev->mode != AUDIO_MODE_IN_COMMUNICATION)) { |
| 9082 | audio_extn_compr_cap_init(in); |
| 9083 | } else if (audio_extn_cin_applicable_stream(in)) { |
Deeraj Soman | 1423092 | 2019-01-30 16:39:30 +0530 | [diff] [blame] | 9084 | ret = audio_extn_cin_configure_input_stream(in, config); |
Revathi Uddaraju | d263403 | 2017-12-07 14:42:34 +0530 | [diff] [blame] | 9085 | if (ret) |
| 9086 | goto err_open; |
| 9087 | } else { |
| 9088 | in->config = pcm_config_audio_capture; |
| 9089 | in->config.rate = config->sample_rate; |
| 9090 | in->config.format = pcm_format_from_audio_format(config->format); |
Revathi Uddaraju | d263403 | 2017-12-07 14:42:34 +0530 | [diff] [blame] | 9091 | in->format = config->format; |
| 9092 | frame_size = audio_stream_in_frame_size(&in->stream); |
| 9093 | buffer_size = get_input_buffer_size(config->sample_rate, |
Haynes Mathew George | 4674047 | 2017-10-27 18:40:12 -0700 | [diff] [blame] | 9094 | config->format, |
| 9095 | channel_count, |
| 9096 | is_low_latency); |
Dieter Luecking | 5d57def | 2018-09-07 14:23:37 +0200 | [diff] [blame] | 9097 | /* prevent division-by-zero */ |
| 9098 | if (frame_size == 0) { |
| 9099 | ALOGE("%s: Error frame_size==0", __func__); |
| 9100 | ret = -EINVAL; |
| 9101 | goto err_open; |
| 9102 | } |
| 9103 | |
Revathi Uddaraju | d263403 | 2017-12-07 14:42:34 +0530 | [diff] [blame] | 9104 | in->config.period_size = buffer_size / frame_size; |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 9105 | in->af_period_multiplier = 1; |
Haynes Mathew George | 4ffef29 | 2017-11-21 15:08:02 -0800 | [diff] [blame] | 9106 | |
Revathi Uddaraju | d263403 | 2017-12-07 14:42:34 +0530 | [diff] [blame] | 9107 | if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) { |
| 9108 | /* optionally use VOIP usecase depending on config(s) */ |
| 9109 | ret = adev_update_voice_comm_input_stream(in, config); |
| 9110 | } |
Haynes Mathew George | 4ffef29 | 2017-11-21 15:08:02 -0800 | [diff] [blame] | 9111 | |
Revathi Uddaraju | d263403 | 2017-12-07 14:42:34 +0530 | [diff] [blame] | 9112 | if (ret) { |
| 9113 | ALOGE("%s AUDIO_SOURCE_VOICE_COMMUNICATION invalid args", __func__); |
| 9114 | goto err_open; |
| 9115 | } |
Haynes Mathew George | 4ffef29 | 2017-11-21 15:08:02 -0800 | [diff] [blame] | 9116 | } |
Samyak Jain | c37062f | 2019-04-25 18:41:06 +0530 | [diff] [blame] | 9117 | if (audio_extn_is_concurrent_capture_enabled()) { |
| 9118 | /* Acquire lock to avoid two concurrent use cases initialized to |
| 9119 | same pcm record use case */ |
kunleiz | 28c73e7 | 2019-03-27 17:24:04 +0800 | [diff] [blame] | 9120 | |
Samyak Jain | c37062f | 2019-04-25 18:41:06 +0530 | [diff] [blame] | 9121 | if (in->usecase == USECASE_AUDIO_RECORD) { |
| 9122 | pthread_mutex_lock(&adev->lock); |
| 9123 | if (!(adev->pcm_record_uc_state)) { |
| 9124 | ALOGV("%s: using USECASE_AUDIO_RECORD",__func__); |
| 9125 | adev->pcm_record_uc_state = 1; |
| 9126 | pthread_mutex_unlock(&adev->lock); |
| 9127 | } else { |
| 9128 | pthread_mutex_unlock(&adev->lock); |
| 9129 | /* Assign compress record use case for second record */ |
| 9130 | in->usecase = USECASE_AUDIO_RECORD_COMPRESS2; |
| 9131 | in->flags |= AUDIO_INPUT_FLAG_COMPRESS; |
| 9132 | ALOGV("%s: overriding usecase with USECASE_AUDIO_RECORD_COMPRESS2 and appending compress flag", __func__); |
| 9133 | if (audio_extn_cin_applicable_stream(in)) { |
| 9134 | in->sample_rate = config->sample_rate; |
Deeraj Soman | 1423092 | 2019-01-30 16:39:30 +0530 | [diff] [blame] | 9135 | ret = audio_extn_cin_configure_input_stream(in, config); |
Samyak Jain | c37062f | 2019-04-25 18:41:06 +0530 | [diff] [blame] | 9136 | if (ret) |
| 9137 | goto err_open; |
| 9138 | } |
| 9139 | } |
| 9140 | } |
kunleiz | 28c73e7 | 2019-03-27 17:24:04 +0800 | [diff] [blame] | 9141 | } |
Apoorv Raghuvanshi | 6178a3f | 2013-10-19 12:38:54 -0700 | [diff] [blame] | 9142 | } |
Ramjee Singh | 82fd0c1 | 2019-08-21 16:31:33 +0530 | [diff] [blame] | 9143 | if (audio_extn_ssr_get_stream() != in) |
| 9144 | in->config.channels = channel_count; |
Aniket Kumar Lata | 23d8cbf | 2019-04-10 19:54:46 -0700 | [diff] [blame] | 9145 | |
Aniket Kumar Lata | 23d8cbf | 2019-04-10 19:54:46 -0700 | [diff] [blame] | 9146 | in->sample_rate = in->config.rate; |
| 9147 | |
Dhananjay Kumar | d6d3215 | 2016-10-13 16:11:03 +0530 | [diff] [blame] | 9148 | audio_extn_utils_update_stream_input_app_type_cfg(adev->platform, |
| 9149 | &adev->streams_input_cfg_list, |
Haynes Mathew George | 4ffef29 | 2017-11-21 15:08:02 -0800 | [diff] [blame] | 9150 | devices, flags, in->format, |
| 9151 | in->sample_rate, in->bit_width, |
| 9152 | in->profile, &in->app_type_cfg); |
Naresh Tanniru | 04f7188 | 2018-06-26 17:46:22 +0530 | [diff] [blame] | 9153 | register_format(in->format, in->supported_formats); |
| 9154 | register_channel_mask(in->channel_mask, in->supported_channel_masks); |
| 9155 | register_sample_rate(in->sample_rate, in->supported_sample_rates); |
Dhananjay Kumar | d6d3215 | 2016-10-13 16:11:03 +0530 | [diff] [blame] | 9156 | |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 9157 | in->error_log = error_log_create( |
| 9158 | ERROR_LOG_ENTRIES, |
| 9159 | 1000000000 /* aggregate consecutive identical errors within one second */); |
| 9160 | |
Ravi Kumar Alamanda | 8fa6b19 | 2014-09-09 16:06:42 -0700 | [diff] [blame] | 9161 | /* This stream could be for sound trigger lab, |
| 9162 | get sound trigger pcm if present */ |
| 9163 | audio_extn_sound_trigger_check_and_get_session(in); |
| 9164 | |
Dhananjay Kumar | e6293dd | 2017-05-25 17:25:30 +0530 | [diff] [blame] | 9165 | lock_input_stream(in); |
| 9166 | audio_extn_snd_mon_register_listener(in, in_snd_mon_cb); |
| 9167 | pthread_mutex_lock(&adev->lock); |
| 9168 | in->card_status = adev->card_status; |
| 9169 | pthread_mutex_unlock(&adev->lock); |
| 9170 | pthread_mutex_unlock(&in->lock); |
| 9171 | |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 9172 | stream_app_type_cfg_init(&in->app_type_cfg); |
| 9173 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 9174 | *stream_in = &in->stream; |
Derek Chen | f939fb7 | 2018-11-13 13:34:41 -0800 | [diff] [blame] | 9175 | |
| 9176 | streams_input_ctxt_t *in_ctxt = (streams_input_ctxt_t *) |
| 9177 | calloc(1, sizeof(streams_input_ctxt_t)); |
| 9178 | if (in_ctxt == NULL) { |
| 9179 | ALOGE("%s fail to allocate input ctxt", __func__); |
| 9180 | ret = -ENOMEM; |
| 9181 | goto err_open; |
| 9182 | } |
| 9183 | in_ctxt->input = in; |
| 9184 | |
| 9185 | pthread_mutex_lock(&adev->lock); |
| 9186 | list_add_tail(&adev->active_inputs_list, &in_ctxt->list); |
| 9187 | pthread_mutex_unlock(&adev->lock); |
| 9188 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 9189 | ALOGV("%s: exit", __func__); |
Ravi Kumar Alamanda | fae4211 | 2013-11-07 23:31:54 -0800 | [diff] [blame] | 9190 | return ret; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 9191 | |
| 9192 | err_open: |
Samyak Jain | c37062f | 2019-04-25 18:41:06 +0530 | [diff] [blame] | 9193 | if (in->usecase == USECASE_AUDIO_RECORD) { |
| 9194 | pthread_mutex_lock(&adev->lock); |
| 9195 | adev->pcm_record_uc_state = 0; |
| 9196 | pthread_mutex_unlock(&adev->lock); |
| 9197 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 9198 | free(in); |
| 9199 | *stream_in = NULL; |
| 9200 | return ret; |
| 9201 | } |
| 9202 | |
| 9203 | static void adev_close_input_stream(struct audio_hw_device *dev, |
| 9204 | struct audio_stream_in *stream) |
| 9205 | { |
Narsinga Rao Chella | 05573b7 | 2013-11-15 15:21:40 -0800 | [diff] [blame] | 9206 | int ret; |
Apoorv Raghuvanshi | 6178a3f | 2013-10-19 12:38:54 -0700 | [diff] [blame] | 9207 | struct stream_in *in = (struct stream_in *)stream; |
Ravi Kumar Alamanda | bdf1416 | 2014-09-05 16:14:17 -0700 | [diff] [blame] | 9208 | struct audio_device *adev = (struct audio_device *)dev; |
Venkata Narendra Kumar Gutta | 9181214 | 2014-08-11 18:20:49 +0530 | [diff] [blame] | 9209 | |
Sidipotu Ashok | f43018c | 2014-05-02 16:21:50 +0530 | [diff] [blame] | 9210 | ALOGD("%s: enter:stream_handle(%p)",__func__, in); |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 9211 | |
kunleiz | 70e5761 | 2018-12-28 17:50:23 +0800 | [diff] [blame] | 9212 | /* must deregister from sndmonitor first to prevent races |
| 9213 | * between the callback and close_stream |
| 9214 | */ |
Dhananjay Kumar | e6293dd | 2017-05-25 17:25:30 +0530 | [diff] [blame] | 9215 | audio_extn_snd_mon_unregister_listener(stream); |
| 9216 | |
kunleiz | 70e5761 | 2018-12-28 17:50:23 +0800 | [diff] [blame] | 9217 | /* Disable echo reference if there are no active input, hfp call |
| 9218 | * and sound trigger while closing input stream |
| 9219 | */ |
Eric Laurent | 637e2d4 | 2018-11-15 12:24:31 -0800 | [diff] [blame] | 9220 | if (adev_get_active_input(adev) == NULL && |
kunleiz | 70e5761 | 2018-12-28 17:50:23 +0800 | [diff] [blame] | 9221 | !audio_extn_hfp_is_active(adev) && |
| 9222 | !audio_extn_sound_trigger_check_ec_ref_enable()) |
kunleiz | 8c93ce6 | 2018-11-29 15:04:56 +0800 | [diff] [blame] | 9223 | platform_set_echo_reference(adev, false, AUDIO_DEVICE_NONE); |
kunleiz | 70e5761 | 2018-12-28 17:50:23 +0800 | [diff] [blame] | 9224 | else |
| 9225 | audio_extn_sound_trigger_update_ec_ref_status(false); |
Venkata Narendra Kumar Gutta | 1bbbf54 | 2014-09-04 19:11:25 +0530 | [diff] [blame] | 9226 | |
Pallavi | d7c7a27 | 2018-01-16 11:22:55 +0530 | [diff] [blame] | 9227 | if (in == NULL) { |
| 9228 | ALOGE("%s: audio_stream_in ptr is NULL", __func__); |
| 9229 | return; |
| 9230 | } |
Weiyin Jiang | 2995f66 | 2019-04-17 14:25:12 +0800 | [diff] [blame] | 9231 | error_log_destroy(in->error_log); |
| 9232 | in->error_log = NULL; |
| 9233 | |
Pallavi | d7c7a27 | 2018-01-16 11:22:55 +0530 | [diff] [blame] | 9234 | |
Narsinga Rao Chella | 05573b7 | 2013-11-15 15:21:40 -0800 | [diff] [blame] | 9235 | if (in->usecase == USECASE_COMPRESS_VOIP_CALL) { |
Venkata Narendra Kumar Gutta | 9181214 | 2014-08-11 18:20:49 +0530 | [diff] [blame] | 9236 | pthread_mutex_lock(&adev->lock); |
Narsinga Rao Chella | 05573b7 | 2013-11-15 15:21:40 -0800 | [diff] [blame] | 9237 | ret = voice_extn_compress_voip_close_input_stream(&stream->common); |
Venkata Narendra Kumar Gutta | 9181214 | 2014-08-11 18:20:49 +0530 | [diff] [blame] | 9238 | pthread_mutex_unlock(&adev->lock); |
Narsinga Rao Chella | 05573b7 | 2013-11-15 15:21:40 -0800 | [diff] [blame] | 9239 | if (ret != 0) |
| 9240 | ALOGE("%s: Compress voip input cannot be closed, error:%d", |
| 9241 | __func__, ret); |
| 9242 | } else |
| 9243 | in_standby(&stream->common); |
| 9244 | |
Revathi Uddaraju | d263403 | 2017-12-07 14:42:34 +0530 | [diff] [blame] | 9245 | pthread_mutex_lock(&adev->lock); |
Samyak Jain | 15fda66 | 2018-12-18 16:40:52 +0530 | [diff] [blame] | 9246 | if (in->usecase == USECASE_AUDIO_RECORD) { |
| 9247 | adev->pcm_record_uc_state = 0; |
| 9248 | } |
| 9249 | |
Shiv Maliyappanahalli | 5a10aea | 2015-07-02 10:36:23 -0700 | [diff] [blame] | 9250 | if (audio_extn_ssr_get_stream() == in) { |
Apoorv Raghuvanshi | 6178a3f | 2013-10-19 12:38:54 -0700 | [diff] [blame] | 9251 | audio_extn_ssr_deinit(); |
| 9252 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 9253 | |
Garmond Leung | e2433c3 | 2017-09-28 21:51:22 -0700 | [diff] [blame] | 9254 | if (audio_extn_ffv_get_stream() == in) { |
| 9255 | audio_extn_ffv_stream_deinit(); |
| 9256 | } |
| 9257 | |
Dhananjay Kumar | ee4d200 | 2016-10-25 18:02:58 +0530 | [diff] [blame] | 9258 | if (audio_extn_compr_cap_enabled() && |
Mingming Yin | e62d784 | 2013-10-25 16:26:03 -0700 | [diff] [blame] | 9259 | audio_extn_compr_cap_format_supported(in->config.format)) |
| 9260 | audio_extn_compr_cap_deinit(); |
Dhanalakshmi Siddani | 74cf00b | 2016-12-02 13:55:57 +0530 | [diff] [blame] | 9261 | |
Dhananjay Kumar | ee4d200 | 2016-10-25 18:02:58 +0530 | [diff] [blame] | 9262 | if (audio_extn_cin_attached_usecase(in->usecase)) |
Manish Dewangan | 46e0798 | 2018-12-13 18:18:59 +0530 | [diff] [blame] | 9263 | audio_extn_cin_free_input_stream_resources(in); |
Ravi Kumar Alamanda | 8fa6b19 | 2014-09-09 16:06:42 -0700 | [diff] [blame] | 9264 | |
Mingming Yin | fd7607b | 2016-01-22 12:48:44 -0800 | [diff] [blame] | 9265 | if (in->is_st_session) { |
| 9266 | ALOGV("%s: sound trigger pcm stop lab", __func__); |
| 9267 | audio_extn_sound_trigger_stop_lab(in); |
| 9268 | } |
Derek Chen | f939fb7 | 2018-11-13 13:34:41 -0800 | [diff] [blame] | 9269 | streams_input_ctxt_t *in_ctxt = in_get_stream(adev, in->capture_handle); |
| 9270 | if (in_ctxt != NULL) { |
| 9271 | list_remove(&in_ctxt->list); |
| 9272 | free(in_ctxt); |
| 9273 | } else { |
| 9274 | ALOGW("%s, input stream already closed", __func__); |
| 9275 | } |
Ravi Kumar Alamanda | 8fa6b19 | 2014-09-09 16:06:42 -0700 | [diff] [blame] | 9276 | free(stream); |
Revathi Uddaraju | d263403 | 2017-12-07 14:42:34 +0530 | [diff] [blame] | 9277 | pthread_mutex_unlock(&adev->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 9278 | return; |
| 9279 | } |
| 9280 | |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 9281 | /* verifies input and output devices and their capabilities. |
| 9282 | * |
| 9283 | * This verification is required when enabling extended bit-depth or |
| 9284 | * sampling rates, as not all qcom products support it. |
| 9285 | * |
| 9286 | * Suitable for calling only on initialization such as adev_open(). |
| 9287 | * It fills the audio_device use_case_table[] array. |
| 9288 | * |
| 9289 | * Has a side-effect that it needs to configure audio routing / devices |
| 9290 | * in order to power up the devices and read the device parameters. |
| 9291 | * It does not acquire any hw device lock. Should restore the devices |
| 9292 | * back to "normal state" upon completion. |
| 9293 | */ |
| 9294 | static int adev_verify_devices(struct audio_device *adev) |
| 9295 | { |
| 9296 | /* enumeration is a bit difficult because one really wants to pull |
| 9297 | * the use_case, device id, etc from the hidden pcm_device_table[]. |
| 9298 | * In this case there are the following use cases and device ids. |
| 9299 | * |
| 9300 | * [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = {0, 0}, |
| 9301 | * [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {15, 15}, |
| 9302 | * [USECASE_AUDIO_PLAYBACK_HIFI] = {1, 1}, |
| 9303 | * [USECASE_AUDIO_PLAYBACK_OFFLOAD] = {9, 9}, |
| 9304 | * [USECASE_AUDIO_RECORD] = {0, 0}, |
| 9305 | * [USECASE_AUDIO_RECORD_LOW_LATENCY] = {15, 15}, |
| 9306 | * [USECASE_VOICE_CALL] = {2, 2}, |
| 9307 | * |
| 9308 | * USECASE_AUDIO_PLAYBACK_OFFLOAD, USECASE_AUDIO_PLAYBACK_HIFI omitted. |
| 9309 | * USECASE_VOICE_CALL omitted, but possible for either input or output. |
| 9310 | */ |
| 9311 | |
| 9312 | /* should be the usecases enabled in adev_open_input_stream() */ |
| 9313 | static const int test_in_usecases[] = { |
| 9314 | USECASE_AUDIO_RECORD, |
| 9315 | USECASE_AUDIO_RECORD_LOW_LATENCY, /* does not appear to be used */ |
| 9316 | }; |
| 9317 | /* should be the usecases enabled in adev_open_output_stream()*/ |
| 9318 | static const int test_out_usecases[] = { |
| 9319 | USECASE_AUDIO_PLAYBACK_DEEP_BUFFER, |
| 9320 | USECASE_AUDIO_PLAYBACK_LOW_LATENCY, |
| 9321 | }; |
| 9322 | static const usecase_type_t usecase_type_by_dir[] = { |
| 9323 | PCM_PLAYBACK, |
| 9324 | PCM_CAPTURE, |
| 9325 | }; |
| 9326 | static const unsigned flags_by_dir[] = { |
| 9327 | PCM_OUT, |
| 9328 | PCM_IN, |
| 9329 | }; |
| 9330 | |
| 9331 | size_t i; |
| 9332 | unsigned dir; |
| 9333 | const unsigned card_id = adev->snd_card; |
| 9334 | |
| 9335 | for (dir = 0; dir < 2; ++dir) { |
| 9336 | const usecase_type_t usecase_type = usecase_type_by_dir[dir]; |
| 9337 | const unsigned flags_dir = flags_by_dir[dir]; |
| 9338 | const size_t testsize = |
| 9339 | dir ? ARRAY_SIZE(test_in_usecases) : ARRAY_SIZE(test_out_usecases); |
| 9340 | const int *testcases = |
| 9341 | dir ? test_in_usecases : test_out_usecases; |
| 9342 | const audio_devices_t audio_device = |
| 9343 | dir ? AUDIO_DEVICE_IN_BUILTIN_MIC : AUDIO_DEVICE_OUT_SPEAKER; |
| 9344 | |
| 9345 | for (i = 0; i < testsize; ++i) { |
| 9346 | const audio_usecase_t audio_usecase = testcases[i]; |
| 9347 | int device_id; |
| 9348 | struct pcm_params **pparams; |
| 9349 | struct stream_out out; |
| 9350 | struct stream_in in; |
| 9351 | struct audio_usecase uc_info; |
| 9352 | int retval; |
| 9353 | |
| 9354 | pparams = &adev->use_case_table[audio_usecase]; |
| 9355 | pcm_params_free(*pparams); /* can accept null input */ |
| 9356 | *pparams = NULL; |
| 9357 | |
| 9358 | /* find the device ID for the use case (signed, for error) */ |
| 9359 | device_id = platform_get_pcm_device_id(audio_usecase, usecase_type); |
| 9360 | if (device_id < 0) |
| 9361 | continue; |
| 9362 | |
| 9363 | /* prepare structures for device probing */ |
| 9364 | memset(&uc_info, 0, sizeof(uc_info)); |
| 9365 | uc_info.id = audio_usecase; |
| 9366 | uc_info.type = usecase_type; |
| 9367 | if (dir) { |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 9368 | memset(&in, 0, sizeof(in)); |
| 9369 | in.device = audio_device; |
| 9370 | in.source = AUDIO_SOURCE_VOICE_COMMUNICATION; |
| 9371 | uc_info.stream.in = ∈ |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 9372 | } |
| 9373 | memset(&out, 0, sizeof(out)); |
| 9374 | out.devices = audio_device; /* only field needed in select_devices */ |
| 9375 | uc_info.stream.out = &out; |
| 9376 | uc_info.devices = audio_device; |
| 9377 | uc_info.in_snd_device = SND_DEVICE_NONE; |
| 9378 | uc_info.out_snd_device = SND_DEVICE_NONE; |
| 9379 | list_add_tail(&adev->usecase_list, &uc_info.list); |
| 9380 | |
| 9381 | /* select device - similar to start_(in/out)put_stream() */ |
| 9382 | retval = select_devices(adev, audio_usecase); |
| 9383 | if (retval >= 0) { |
| 9384 | *pparams = pcm_params_get(card_id, device_id, flags_dir); |
| 9385 | #if LOG_NDEBUG == 0 |
Aalique Grahame | 203bee0 | 2019-03-13 17:49:36 -0700 | [diff] [blame] | 9386 | char info[512]; /* for possible debug info */ |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 9387 | if (*pparams) { |
| 9388 | ALOGV("%s: (%s) card %d device %d", __func__, |
| 9389 | dir ? "input" : "output", card_id, device_id); |
| 9390 | pcm_params_to_string(*pparams, info, ARRAY_SIZE(info)); |
| 9391 | } else { |
| 9392 | ALOGV("%s: cannot locate card %d device %d", __func__, card_id, device_id); |
| 9393 | } |
| 9394 | #endif |
| 9395 | } |
| 9396 | |
| 9397 | /* deselect device - similar to stop_(in/out)put_stream() */ |
| 9398 | /* 1. Get and set stream specific mixer controls */ |
| 9399 | retval = disable_audio_route(adev, &uc_info); |
| 9400 | /* 2. Disable the rx device */ |
| 9401 | retval = disable_snd_device(adev, |
| 9402 | dir ? uc_info.in_snd_device : uc_info.out_snd_device); |
| 9403 | list_remove(&uc_info.list); |
| 9404 | } |
| 9405 | } |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 9406 | return 0; |
| 9407 | } |
| 9408 | |
Siddartha Shaik | 31b530e | 2017-05-19 15:26:33 +0530 | [diff] [blame] | 9409 | int adev_create_audio_patch(struct audio_hw_device *dev, |
| 9410 | unsigned int num_sources, |
| 9411 | const struct audio_port_config *sources, |
| 9412 | unsigned int num_sinks, |
| 9413 | const struct audio_port_config *sinks, |
| 9414 | audio_patch_handle_t *handle) |
| 9415 | { |
Derek Chen | f939fb7 | 2018-11-13 13:34:41 -0800 | [diff] [blame] | 9416 | int ret; |
Siddartha Shaik | 31b530e | 2017-05-19 15:26:33 +0530 | [diff] [blame] | 9417 | |
Derek Chen | f939fb7 | 2018-11-13 13:34:41 -0800 | [diff] [blame] | 9418 | ret = audio_extn_hw_loopback_create_audio_patch(dev, |
| 9419 | num_sources, |
| 9420 | sources, |
| 9421 | num_sinks, |
| 9422 | sinks, |
| 9423 | handle); |
| 9424 | ret |= audio_extn_auto_hal_create_audio_patch(dev, |
| 9425 | num_sources, |
| 9426 | sources, |
| 9427 | num_sinks, |
| 9428 | sinks, |
| 9429 | handle); |
| 9430 | return ret; |
Siddartha Shaik | 31b530e | 2017-05-19 15:26:33 +0530 | [diff] [blame] | 9431 | } |
| 9432 | |
| 9433 | int adev_release_audio_patch(struct audio_hw_device *dev, |
| 9434 | audio_patch_handle_t handle) |
| 9435 | { |
Derek Chen | f939fb7 | 2018-11-13 13:34:41 -0800 | [diff] [blame] | 9436 | int ret; |
| 9437 | |
| 9438 | ret = audio_extn_hw_loopback_release_audio_patch(dev, handle); |
| 9439 | ret |= audio_extn_auto_hal_release_audio_patch(dev, handle); |
| 9440 | return ret; |
Siddartha Shaik | 31b530e | 2017-05-19 15:26:33 +0530 | [diff] [blame] | 9441 | } |
| 9442 | |
| 9443 | int adev_get_audio_port(struct audio_hw_device *dev, struct audio_port *config) |
| 9444 | { |
Derek Chen | f13dd49 | 2018-11-13 14:53:51 -0800 | [diff] [blame] | 9445 | int ret = 0; |
| 9446 | |
| 9447 | ret = audio_extn_hw_loopback_get_audio_port(dev, config); |
| 9448 | ret |= audio_extn_auto_hal_get_audio_port(dev, config); |
| 9449 | return ret; |
Siddartha Shaik | 31b530e | 2017-05-19 15:26:33 +0530 | [diff] [blame] | 9450 | } |
| 9451 | |
| 9452 | int adev_set_audio_port_config(struct audio_hw_device *dev, |
| 9453 | const struct audio_port_config *config) |
| 9454 | { |
Derek Chen | f13dd49 | 2018-11-13 14:53:51 -0800 | [diff] [blame] | 9455 | int ret = 0; |
| 9456 | |
| 9457 | ret = audio_extn_hw_loopback_set_audio_port_config(dev, config); |
| 9458 | ret |= audio_extn_auto_hal_set_audio_port_config(dev, config); |
| 9459 | return ret; |
Siddartha Shaik | 31b530e | 2017-05-19 15:26:33 +0530 | [diff] [blame] | 9460 | } |
| 9461 | |
Ravi Kumar Alamanda | bdf1416 | 2014-09-05 16:14:17 -0700 | [diff] [blame] | 9462 | static int adev_dump(const audio_hw_device_t *device __unused, |
| 9463 | int fd __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 9464 | { |
| 9465 | return 0; |
| 9466 | } |
| 9467 | |
| 9468 | static int adev_close(hw_device_t *device) |
| 9469 | { |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 9470 | size_t i; |
Preetam Singh Ranawat | acc6454 | 2019-06-24 15:11:28 +0530 | [diff] [blame] | 9471 | struct audio_device *adev_temp = (struct audio_device *)device; |
Kiran Kandi | 910e186 | 2013-10-29 13:29:42 -0700 | [diff] [blame] | 9472 | |
Preetam Singh Ranawat | acc6454 | 2019-06-24 15:11:28 +0530 | [diff] [blame] | 9473 | if (!adev_temp) |
Kiran Kandi | 910e186 | 2013-10-29 13:29:42 -0700 | [diff] [blame] | 9474 | return 0; |
| 9475 | |
| 9476 | pthread_mutex_lock(&adev_init_lock); |
| 9477 | |
| 9478 | if ((--audio_device_ref_count) == 0) { |
Sujin Panicker | 390724d | 2019-04-26 10:43:36 +0530 | [diff] [blame] | 9479 | if (audio_extn_spkr_prot_is_enabled()) |
| 9480 | audio_extn_spkr_prot_deinit(); |
Jaideep Sharma | a2b4967 | 2019-09-10 20:37:03 +0530 | [diff] [blame] | 9481 | audio_extn_battery_properties_listener_deinit(); |
Dhananjay Kumar | e6293dd | 2017-05-25 17:25:30 +0530 | [diff] [blame] | 9482 | audio_extn_snd_mon_unregister_listener(adev); |
Ravi Kumar Alamanda | 8fa6b19 | 2014-09-09 16:06:42 -0700 | [diff] [blame] | 9483 | audio_extn_sound_trigger_deinit(adev); |
Kiran Kandi | de144c8 | 2013-11-20 15:58:32 -0800 | [diff] [blame] | 9484 | audio_extn_listen_deinit(adev); |
Arun Mirpuri | b1bec9c | 2019-01-29 16:42:45 -0800 | [diff] [blame] | 9485 | audio_extn_qdsp_deinit(); |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 9486 | audio_extn_extspk_deinit(adev->extspk); |
Dhananjay Kumar | d6d3215 | 2016-10-13 16:11:03 +0530 | [diff] [blame] | 9487 | audio_extn_utils_release_streams_cfg_lists( |
| 9488 | &adev->streams_output_cfg_list, |
| 9489 | &adev->streams_input_cfg_list); |
Sidipotu Ashok | d2f10ba | 2019-05-06 15:41:56 +0530 | [diff] [blame] | 9490 | if (audio_extn_qap_is_enabled()) |
| 9491 | audio_extn_qap_deinit(); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 9492 | if (audio_extn_qaf_is_enabled()) |
| 9493 | audio_extn_qaf_deinit(); |
Kiran Kandi | 910e186 | 2013-10-29 13:29:42 -0700 | [diff] [blame] | 9494 | audio_route_free(adev->audio_route); |
Weiyin Jiang | fa65d3e | 2019-03-05 23:39:45 +0800 | [diff] [blame] | 9495 | audio_extn_gef_deinit(adev); |
Kiran Kandi | 910e186 | 2013-10-29 13:29:42 -0700 | [diff] [blame] | 9496 | free(adev->snd_dev_ref_cnt); |
| 9497 | platform_deinit(adev->platform); |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 9498 | for (i = 0; i < ARRAY_SIZE(adev->use_case_table); ++i) { |
| 9499 | pcm_params_free(adev->use_case_table[i]); |
| 9500 | } |
Ravi Kumar Alamanda | 8a0f977 | 2015-06-15 10:35:19 -0700 | [diff] [blame] | 9501 | if (adev->adm_deinit) |
| 9502 | adev->adm_deinit(adev->adm_data); |
Dhananjay Kumar | ee4d200 | 2016-10-25 18:02:58 +0530 | [diff] [blame] | 9503 | qahwi_deinit(device); |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 9504 | audio_extn_adsp_hdlr_deinit(); |
Dhananjay Kumar | e6293dd | 2017-05-25 17:25:30 +0530 | [diff] [blame] | 9505 | audio_extn_snd_mon_deinit(); |
Siddartha Shaik | 44dd770 | 2017-06-14 12:13:25 +0530 | [diff] [blame] | 9506 | audio_extn_hw_loopback_deinit(adev); |
Garmond Leung | e2433c3 | 2017-09-28 21:51:22 -0700 | [diff] [blame] | 9507 | audio_extn_ffv_deinit(); |
Satish Babu Patakokila | c3c5d43 | 2017-07-04 22:48:59 +0530 | [diff] [blame] | 9508 | if (adev->device_cfg_params) { |
| 9509 | free(adev->device_cfg_params); |
| 9510 | adev->device_cfg_params = NULL; |
| 9511 | } |
Derek Chen | d253007 | 2014-11-24 12:39:14 -0800 | [diff] [blame] | 9512 | if(adev->ext_hw_plugin) |
| 9513 | audio_extn_ext_hw_plugin_deinit(adev->ext_hw_plugin); |
Derek Chen | ae7b034 | 2019-02-08 15:17:04 -0800 | [diff] [blame] | 9514 | audio_extn_auto_hal_deinit(); |
Kiran Kandi | 910e186 | 2013-10-29 13:29:42 -0700 | [diff] [blame] | 9515 | free(device); |
| 9516 | adev = NULL; |
| 9517 | } |
| 9518 | pthread_mutex_unlock(&adev_init_lock); |
Vatsal Bucha | c09ae06 | 2018-11-14 13:25:08 +0530 | [diff] [blame] | 9519 | enable_gcov(); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 9520 | return 0; |
| 9521 | } |
| 9522 | |
Ravi Kumar Alamanda | dc9bb15 | 2014-09-08 15:59:58 -0700 | [diff] [blame] | 9523 | /* This returns 1 if the input parameter looks at all plausible as a low latency period size, |
| 9524 | * or 0 otherwise. A return value of 1 doesn't mean the value is guaranteed to work, |
| 9525 | * just that it _might_ work. |
| 9526 | */ |
| 9527 | static int period_size_is_plausible_for_low_latency(int period_size) |
| 9528 | { |
| 9529 | switch (period_size) { |
| 9530 | case 160: |
Ravi Kumar Alamanda | 474de5a | 2015-06-25 20:08:01 -0700 | [diff] [blame] | 9531 | case 192: |
Ravi Kumar Alamanda | dc9bb15 | 2014-09-08 15:59:58 -0700 | [diff] [blame] | 9532 | case 240: |
| 9533 | case 320: |
| 9534 | case 480: |
| 9535 | return 1; |
| 9536 | default: |
| 9537 | return 0; |
| 9538 | } |
| 9539 | } |
| 9540 | |
Dhananjay Kumar | e6293dd | 2017-05-25 17:25:30 +0530 | [diff] [blame] | 9541 | static void adev_snd_mon_cb(void *cookie, struct str_parms *parms) |
| 9542 | { |
| 9543 | bool is_snd_card_status = false; |
| 9544 | bool is_ext_device_status = false; |
| 9545 | char value[32]; |
| 9546 | int card = -1; |
| 9547 | card_status_t status; |
| 9548 | |
| 9549 | if (cookie != adev || !parms) |
| 9550 | return; |
| 9551 | |
| 9552 | if (!parse_snd_card_status(parms, &card, &status)) { |
| 9553 | is_snd_card_status = true; |
| 9554 | } else if (0 < str_parms_get_str(parms, "ext_audio_device", value, sizeof(value))) { |
| 9555 | is_ext_device_status = true; |
| 9556 | } else { |
| 9557 | // not a valid event |
| 9558 | return; |
| 9559 | } |
| 9560 | |
| 9561 | pthread_mutex_lock(&adev->lock); |
| 9562 | if (card == adev->snd_card || is_ext_device_status) { |
| 9563 | if (is_snd_card_status && adev->card_status != status) { |
| 9564 | adev->card_status = status; |
| 9565 | platform_snd_card_update(adev->platform, status); |
| 9566 | audio_extn_fm_set_parameters(adev, parms); |
Derek Chen | d6f371d | 2019-03-01 13:45:58 -0800 | [diff] [blame] | 9567 | audio_extn_auto_hal_set_parameters(adev, parms); |
Zhou Song | 4f43e35 | 2019-07-02 00:32:23 +0800 | [diff] [blame] | 9568 | if (status == CARD_STATUS_OFFLINE) |
| 9569 | audio_extn_sco_reset_configuration(); |
Dhananjay Kumar | e6293dd | 2017-05-25 17:25:30 +0530 | [diff] [blame] | 9570 | } else if (is_ext_device_status) { |
| 9571 | platform_set_parameters(adev->platform, parms); |
| 9572 | } |
| 9573 | } |
| 9574 | pthread_mutex_unlock(&adev->lock); |
| 9575 | return; |
| 9576 | } |
| 9577 | |
Chaithanya Krishna Bacharaju | 69d2e4c | 2017-05-26 18:22:46 +0530 | [diff] [blame] | 9578 | /* out and adev lock held */ |
| 9579 | static int check_a2dp_restore_l(struct audio_device *adev, struct stream_out *out, bool restore) |
| 9580 | { |
| 9581 | struct audio_usecase *uc_info; |
| 9582 | float left_p; |
| 9583 | float right_p; |
| 9584 | audio_devices_t devices; |
| 9585 | |
| 9586 | uc_info = get_usecase_from_list(adev, out->usecase); |
| 9587 | if (uc_info == NULL) { |
| 9588 | ALOGE("%s: Could not find the usecase (%d) in the list", |
| 9589 | __func__, out->usecase); |
| 9590 | return -EINVAL; |
| 9591 | } |
| 9592 | |
| 9593 | ALOGD("%s: enter: usecase(%d: %s)", __func__, |
| 9594 | out->usecase, use_case_table[out->usecase]); |
| 9595 | |
| 9596 | if (restore) { |
| 9597 | // restore A2DP device for active usecases and unmute if required |
Zhou Song | e522513 | 2019-09-26 15:33:36 +0800 | [diff] [blame] | 9598 | if (out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) { |
Chaithanya Krishna Bacharaju | 69d2e4c | 2017-05-26 18:22:46 +0530 | [diff] [blame] | 9599 | ALOGD("%s: restoring A2dp and unmuting stream", __func__); |
Zhou Song | e522513 | 2019-09-26 15:33:36 +0800 | [diff] [blame] | 9600 | if (uc_info->out_snd_device != SND_DEVICE_OUT_BT_A2DP) |
| 9601 | select_devices(adev, uc_info->id); |
Chaithanya Krishna Bacharaju | 69d2e4c | 2017-05-26 18:22:46 +0530 | [diff] [blame] | 9602 | pthread_mutex_lock(&out->compr_mute_lock); |
| 9603 | if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && |
Manisha Agarwal | c2188fb | 2019-05-27 10:00:29 +0530 | [diff] [blame] | 9604 | (out->a2dp_compress_mute) && (uc_info->out_snd_device == SND_DEVICE_OUT_BT_A2DP)) { |
Chaithanya Krishna Bacharaju | 69d2e4c | 2017-05-26 18:22:46 +0530 | [diff] [blame] | 9605 | out->a2dp_compress_mute = false; |
| 9606 | out_set_compr_volume(&out->stream, out->volume_l, out->volume_r); |
| 9607 | } |
| 9608 | pthread_mutex_unlock(&out->compr_mute_lock); |
| 9609 | } |
| 9610 | } else { |
Zhou Song | c576a45 | 2019-09-09 14:17:40 +0800 | [diff] [blame] | 9611 | if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { |
| 9612 | // mute compress stream if suspended |
| 9613 | pthread_mutex_lock(&out->compr_mute_lock); |
| 9614 | if (!out->a2dp_compress_mute && !out->standby) { |
Chaithanya Krishna Bacharaju | 69d2e4c | 2017-05-26 18:22:46 +0530 | [diff] [blame] | 9615 | ALOGD("%s: selecting speaker and muting stream", __func__); |
| 9616 | devices = out->devices; |
| 9617 | out->devices = AUDIO_DEVICE_OUT_SPEAKER; |
| 9618 | left_p = out->volume_l; |
| 9619 | right_p = out->volume_r; |
| 9620 | if (out->offload_state == OFFLOAD_STATE_PLAYING) |
| 9621 | compress_pause(out->compr); |
| 9622 | out_set_compr_volume(&out->stream, (float)0, (float)0); |
| 9623 | out->a2dp_compress_mute = true; |
| 9624 | select_devices(adev, out->usecase); |
| 9625 | if (out->offload_state == OFFLOAD_STATE_PLAYING) |
| 9626 | compress_resume(out->compr); |
| 9627 | out->devices = devices; |
| 9628 | out->volume_l = left_p; |
| 9629 | out->volume_r = right_p; |
| 9630 | } |
Zhou Song | c576a45 | 2019-09-09 14:17:40 +0800 | [diff] [blame] | 9631 | pthread_mutex_unlock(&out->compr_mute_lock); |
| 9632 | } else { |
| 9633 | // tear down a2dp path for non offloaded streams |
| 9634 | if (audio_extn_a2dp_source_is_suspended()) |
| 9635 | out_standby_l(&out->stream.common); |
Chaithanya Krishna Bacharaju | 69d2e4c | 2017-05-26 18:22:46 +0530 | [diff] [blame] | 9636 | } |
Chaithanya Krishna Bacharaju | 69d2e4c | 2017-05-26 18:22:46 +0530 | [diff] [blame] | 9637 | } |
| 9638 | ALOGV("%s: exit", __func__); |
| 9639 | return 0; |
| 9640 | } |
| 9641 | |
| 9642 | int check_a2dp_restore(struct audio_device *adev, struct stream_out *out, bool restore) |
| 9643 | { |
| 9644 | int ret = 0; |
| 9645 | |
| 9646 | lock_output_stream(out); |
| 9647 | pthread_mutex_lock(&adev->lock); |
| 9648 | |
| 9649 | ret = check_a2dp_restore_l(adev, out, restore); |
| 9650 | |
| 9651 | pthread_mutex_unlock(&adev->lock); |
| 9652 | pthread_mutex_unlock(&out->lock); |
| 9653 | return ret; |
| 9654 | } |
| 9655 | |
Haynes Mathew George | 01156f9 | 2018-04-13 15:29:54 -0700 | [diff] [blame] | 9656 | void adev_on_battery_status_changed(bool charging) |
| 9657 | { |
| 9658 | pthread_mutex_lock(&adev->lock); |
| 9659 | ALOGI("%s: battery status changed to %scharging", __func__, charging ? "" : "not "); |
| 9660 | adev->is_charging = charging; |
Zhou Song | c1088ea | 2018-06-12 00:17:29 +0800 | [diff] [blame] | 9661 | audio_extn_sound_trigger_update_battery_status(charging); |
Haynes Mathew George | 01156f9 | 2018-04-13 15:29:54 -0700 | [diff] [blame] | 9662 | pthread_mutex_unlock(&adev->lock); |
| 9663 | } |
| 9664 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 9665 | static int adev_open(const hw_module_t *module, const char *name, |
| 9666 | hw_device_t **device) |
| 9667 | { |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 9668 | int ret; |
Derek Chen | f939fb7 | 2018-11-13 13:34:41 -0800 | [diff] [blame] | 9669 | char value[PROPERTY_VALUE_MAX] = {0}; |
Dhanalakshmi Siddani | 20628c8 | 2019-01-27 17:31:09 +0530 | [diff] [blame] | 9670 | char mixer_ctl_name[128] = {0}; |
| 9671 | struct mixer_ctl *ctl = NULL; |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 9672 | |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 9673 | ALOGD("%s: enter", __func__); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 9674 | if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL; |
| 9675 | |
Apoorv Raghuvanshi | 6e26284 | 2013-10-06 14:39:35 -0700 | [diff] [blame] | 9676 | pthread_mutex_lock(&adev_init_lock); |
Kiran Kandi | 910e186 | 2013-10-29 13:29:42 -0700 | [diff] [blame] | 9677 | if (audio_device_ref_count != 0){ |
Apoorv Raghuvanshi | 6e26284 | 2013-10-06 14:39:35 -0700 | [diff] [blame] | 9678 | *device = &adev->device.common; |
Kiran Kandi | 910e186 | 2013-10-29 13:29:42 -0700 | [diff] [blame] | 9679 | audio_device_ref_count++; |
Apoorv Raghuvanshi | 6e26284 | 2013-10-06 14:39:35 -0700 | [diff] [blame] | 9680 | ALOGD("%s: returning existing instance of adev", __func__); |
| 9681 | ALOGD("%s: exit", __func__); |
| 9682 | pthread_mutex_unlock(&adev_init_lock); |
| 9683 | return 0; |
| 9684 | } |
| 9685 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 9686 | adev = calloc(1, sizeof(struct audio_device)); |
| 9687 | |
Haynes Mathew George | b51ceb1 | 2014-06-30 13:56:18 -0700 | [diff] [blame] | 9688 | if (!adev) { |
| 9689 | pthread_mutex_unlock(&adev_init_lock); |
| 9690 | return -ENOMEM; |
| 9691 | } |
| 9692 | |
Ravi Kumar Alamanda | 4070310 | 2014-04-24 10:34:41 -0700 | [diff] [blame] | 9693 | pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL); |
| 9694 | |
Weiyin Jiang | e6ce631 | 2019-01-28 18:28:22 +0800 | [diff] [blame] | 9695 | // register audio ext hidl at the earliest |
| 9696 | audio_extn_hidl_init(); |
Revathi Uddaraju | 1eac8b0 | 2017-05-18 17:13:33 +0530 | [diff] [blame] | 9697 | #ifdef DYNAMIC_LOG_ENABLED |
| 9698 | register_for_dynamic_logging("hal"); |
| 9699 | #endif |
| 9700 | |
Derek Chen | f939fb7 | 2018-11-13 13:34:41 -0800 | [diff] [blame] | 9701 | /* default audio HAL major version */ |
| 9702 | uint32_t maj_version = 2; |
| 9703 | if(property_get("vendor.audio.hal.maj.version", value, NULL)) |
| 9704 | maj_version = atoi(value); |
| 9705 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 9706 | adev->device.common.tag = HARDWARE_DEVICE_TAG; |
Derek Chen | f939fb7 | 2018-11-13 13:34:41 -0800 | [diff] [blame] | 9707 | adev->device.common.version = HARDWARE_DEVICE_API_VERSION(maj_version, 0); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 9708 | adev->device.common.module = (struct hw_module_t *)module; |
| 9709 | adev->device.common.close = adev_close; |
| 9710 | |
| 9711 | adev->device.init_check = adev_init_check; |
| 9712 | adev->device.set_voice_volume = adev_set_voice_volume; |
| 9713 | adev->device.set_master_volume = adev_set_master_volume; |
| 9714 | adev->device.get_master_volume = adev_get_master_volume; |
| 9715 | adev->device.set_master_mute = adev_set_master_mute; |
| 9716 | adev->device.get_master_mute = adev_get_master_mute; |
| 9717 | adev->device.set_mode = adev_set_mode; |
| 9718 | adev->device.set_mic_mute = adev_set_mic_mute; |
| 9719 | adev->device.get_mic_mute = adev_get_mic_mute; |
| 9720 | adev->device.set_parameters = adev_set_parameters; |
| 9721 | adev->device.get_parameters = adev_get_parameters; |
| 9722 | adev->device.get_input_buffer_size = adev_get_input_buffer_size; |
| 9723 | adev->device.open_output_stream = adev_open_output_stream; |
| 9724 | adev->device.close_output_stream = adev_close_output_stream; |
| 9725 | adev->device.open_input_stream = adev_open_input_stream; |
| 9726 | adev->device.close_input_stream = adev_close_input_stream; |
Siddartha Shaik | 31b530e | 2017-05-19 15:26:33 +0530 | [diff] [blame] | 9727 | adev->device.create_audio_patch = adev_create_audio_patch; |
| 9728 | adev->device.release_audio_patch = adev_release_audio_patch; |
| 9729 | adev->device.get_audio_port = adev_get_audio_port; |
| 9730 | adev->device.set_audio_port_config = adev_set_audio_port_config; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 9731 | adev->device.dump = adev_dump; |
Naresh Tanniru | dcb47c5 | 2018-06-25 16:23:32 +0530 | [diff] [blame] | 9732 | adev->device.get_microphones = adev_get_microphones; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 9733 | |
| 9734 | /* Set the default route before the PCM stream is opened */ |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 9735 | adev->mode = AUDIO_MODE_NORMAL; |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 9736 | adev->primary_output = NULL; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 9737 | adev->out_device = AUDIO_DEVICE_NONE; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 9738 | adev->bluetooth_nrec = true; |
Ravi Kumar Alamanda | f996704 | 2013-02-14 19:35:14 -0800 | [diff] [blame] | 9739 | adev->acdb_settings = TTY_MODE_OFF; |
vivek mehta | 344576a | 2016-04-12 18:56:03 -0700 | [diff] [blame] | 9740 | adev->allow_afe_proxy_usage = true; |
Ashish Jain | 1b9b30c | 2017-05-18 20:57:40 +0530 | [diff] [blame] | 9741 | adev->bt_sco_on = false; |
Eric Laurent | 07eeafd | 2013-10-06 12:52:49 -0700 | [diff] [blame] | 9742 | /* adev->cur_hdmi_channels = 0; by calloc() */ |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 9743 | adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int)); |
vivek mehta | ae1018c | 2019-05-09 12:19:57 -0700 | [diff] [blame] | 9744 | /* Init audio and voice feature */ |
| 9745 | audio_extn_feature_init(); |
| 9746 | voice_extn_feature_init(); |
Shiv Maliyappanahalli | 34b585f | 2013-10-01 15:49:05 -0700 | [diff] [blame] | 9747 | voice_init(adev); |
Ravi Kumar Alamanda | 3b1816c | 2013-02-27 23:01:21 -0800 | [diff] [blame] | 9748 | list_init(&adev->usecase_list); |
Derek Chen | f939fb7 | 2018-11-13 13:34:41 -0800 | [diff] [blame] | 9749 | list_init(&adev->active_inputs_list); |
| 9750 | list_init(&adev->active_outputs_list); |
Rahul Sharma | 9977098 | 2019-03-06 17:05:26 +0530 | [diff] [blame] | 9751 | list_init(&adev->audio_patch_record_list); |
| 9752 | adev->audio_patch_index = 0; |
Krishnankutty Kolathappilly | 0b2de1c | 2014-02-14 14:45:49 -0800 | [diff] [blame] | 9753 | adev->cur_wfd_channels = 2; |
Subhash Chandra Bose Naripeddy | 16ff4f8 | 2014-04-01 21:03:10 -0700 | [diff] [blame] | 9754 | adev->offload_usecases_state = 0; |
Samyak Jain | 15fda66 | 2018-12-18 16:40:52 +0530 | [diff] [blame] | 9755 | adev->pcm_record_uc_state = 0; |
Ashish Jain | 81eb2a8 | 2015-05-13 10:52:34 +0530 | [diff] [blame] | 9756 | adev->is_channel_status_set = false; |
Sudheer Papothi | fa9d228 | 2015-09-17 01:53:25 +0530 | [diff] [blame] | 9757 | adev->perf_lock_opts[0] = 0x101; |
| 9758 | adev->perf_lock_opts[1] = 0x20E; |
| 9759 | adev->perf_lock_opts_size = 2; |
Xiaojun Sang | 785b5da | 2017-08-03 15:52:29 +0800 | [diff] [blame] | 9760 | adev->dsp_bit_width_enforce_mode = 0; |
Aalique Grahame | 552b083 | 2019-03-11 10:16:38 -0700 | [diff] [blame] | 9761 | adev->enable_hfp = false; |
Dhanalakshmi Siddani | 20628c8 | 2019-01-27 17:31:09 +0530 | [diff] [blame] | 9762 | adev->use_old_pspd_mix_ctrl = false; |
Aniket Kumar Lata | 60586a9 | 2019-05-22 22:18:55 -0700 | [diff] [blame] | 9763 | adev->adm_routing_changed = false; |
Naresh Tanniru | 4c63039 | 2014-05-12 01:05:52 +0530 | [diff] [blame] | 9764 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 9765 | /* Loads platform specific libraries dynamically */ |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 9766 | adev->platform = platform_init(adev); |
| 9767 | if (!adev->platform) { |
Ben Romberger | 2684ad7 | 2018-06-04 12:41:39 -0700 | [diff] [blame] | 9768 | pthread_mutex_destroy(&adev->lock); |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 9769 | free(adev->snd_dev_ref_cnt); |
| 9770 | free(adev); |
Ben Romberger | 2684ad7 | 2018-06-04 12:41:39 -0700 | [diff] [blame] | 9771 | adev = NULL; |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 9772 | ALOGE("%s: Failed to init platform data, aborting.", __func__); |
| 9773 | *device = NULL; |
Apoorv Raghuvanshi | 6e57d7e | 2013-12-16 16:02:45 -0800 | [diff] [blame] | 9774 | pthread_mutex_unlock(&adev_init_lock); |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 9775 | return -EINVAL; |
| 9776 | } |
Eric Laurent | c4aef75 | 2013-09-12 17:45:53 -0700 | [diff] [blame] | 9777 | |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 9778 | adev->extspk = audio_extn_extspk_init(adev); |
Sidipotu Ashok | d2f10ba | 2019-05-06 15:41:56 +0530 | [diff] [blame] | 9779 | if (audio_extn_qap_is_enabled()) { |
| 9780 | ret = audio_extn_qap_init(adev); |
| 9781 | if (ret < 0) { |
| 9782 | pthread_mutex_destroy(&adev->lock); |
| 9783 | free(adev); |
| 9784 | adev = NULL; |
| 9785 | ALOGE("%s: Failed to init platform data, aborting.", __func__); |
| 9786 | *device = NULL; |
| 9787 | pthread_mutex_unlock(&adev_init_lock); |
| 9788 | return ret; |
| 9789 | } |
| 9790 | adev->device.open_output_stream = audio_extn_qap_open_output_stream; |
| 9791 | adev->device.close_output_stream = audio_extn_qap_close_output_stream; |
| 9792 | } |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 9793 | |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 9794 | if (audio_extn_qaf_is_enabled()) { |
| 9795 | ret = audio_extn_qaf_init(adev); |
| 9796 | if (ret < 0) { |
Ben Romberger | 2684ad7 | 2018-06-04 12:41:39 -0700 | [diff] [blame] | 9797 | pthread_mutex_destroy(&adev->lock); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 9798 | free(adev); |
Ben Romberger | 2684ad7 | 2018-06-04 12:41:39 -0700 | [diff] [blame] | 9799 | adev = NULL; |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 9800 | ALOGE("%s: Failed to init platform data, aborting.", __func__); |
| 9801 | *device = NULL; |
| 9802 | pthread_mutex_unlock(&adev_init_lock); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 9803 | return ret; |
| 9804 | } |
| 9805 | |
| 9806 | adev->device.open_output_stream = audio_extn_qaf_open_output_stream; |
| 9807 | adev->device.close_output_stream = audio_extn_qaf_close_output_stream; |
| 9808 | } |
| 9809 | |
Derek Chen | ae7b034 | 2019-02-08 15:17:04 -0800 | [diff] [blame] | 9810 | audio_extn_auto_hal_init(adev); |
Derek Chen | d253007 | 2014-11-24 12:39:14 -0800 | [diff] [blame] | 9811 | adev->ext_hw_plugin = audio_extn_ext_hw_plugin_init(adev); |
| 9812 | |
Eric Laurent | c4aef75 | 2013-09-12 17:45:53 -0700 | [diff] [blame] | 9813 | if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) { |
| 9814 | adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW); |
| 9815 | if (adev->visualizer_lib == NULL) { |
| 9816 | ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH); |
| 9817 | } else { |
| 9818 | ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH); |
| 9819 | adev->visualizer_start_output = |
Subhash Chandra Bose Naripeddy | 1d08916 | 2013-11-13 13:31:50 -0800 | [diff] [blame] | 9820 | (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib, |
Eric Laurent | c4aef75 | 2013-09-12 17:45:53 -0700 | [diff] [blame] | 9821 | "visualizer_hal_start_output"); |
| 9822 | adev->visualizer_stop_output = |
Subhash Chandra Bose Naripeddy | 1d08916 | 2013-11-13 13:31:50 -0800 | [diff] [blame] | 9823 | (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib, |
Eric Laurent | c4aef75 | 2013-09-12 17:45:53 -0700 | [diff] [blame] | 9824 | "visualizer_hal_stop_output"); |
| 9825 | } |
| 9826 | } |
Dhanalakshmi Siddani | 21be3ac | 2016-12-29 14:31:08 +0530 | [diff] [blame] | 9827 | audio_extn_init(adev); |
Arun Mirpuri | b1bec9c | 2019-01-29 16:42:45 -0800 | [diff] [blame] | 9828 | voice_extn_init(adev); |
Apoorv Raghuvanshi | 84fa2fe | 2013-12-04 11:57:47 -0800 | [diff] [blame] | 9829 | audio_extn_listen_init(adev, adev->snd_card); |
Weiyin Jiang | aa80acd | 2016-09-21 16:42:11 +0800 | [diff] [blame] | 9830 | audio_extn_gef_init(adev); |
Siddartha Shaik | 44dd770 | 2017-06-14 12:13:25 +0530 | [diff] [blame] | 9831 | audio_extn_hw_loopback_init(adev); |
Garmond Leung | e2433c3 | 2017-09-28 21:51:22 -0700 | [diff] [blame] | 9832 | audio_extn_ffv_init(adev); |
Eric Laurent | c4aef75 | 2013-09-12 17:45:53 -0700 | [diff] [blame] | 9833 | |
Subhash Chandra Bose Naripeddy | 1d08916 | 2013-11-13 13:31:50 -0800 | [diff] [blame] | 9834 | if (access(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, R_OK) == 0) { |
| 9835 | adev->offload_effects_lib = dlopen(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, RTLD_NOW); |
| 9836 | if (adev->offload_effects_lib == NULL) { |
| 9837 | ALOGE("%s: DLOPEN failed for %s", __func__, |
| 9838 | OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH); |
| 9839 | } else { |
| 9840 | ALOGV("%s: DLOPEN successful for %s", __func__, |
| 9841 | OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH); |
| 9842 | adev->offload_effects_start_output = |
Ashish Jain | 5106d36 | 2016-05-11 19:23:33 +0530 | [diff] [blame] | 9843 | (int (*)(audio_io_handle_t, int, struct mixer *))dlsym(adev->offload_effects_lib, |
Subhash Chandra Bose Naripeddy | 1d08916 | 2013-11-13 13:31:50 -0800 | [diff] [blame] | 9844 | "offload_effects_bundle_hal_start_output"); |
| 9845 | adev->offload_effects_stop_output = |
| 9846 | (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib, |
| 9847 | "offload_effects_bundle_hal_stop_output"); |
Jitendra Naruka | 1b6513f | 2014-11-22 19:34:13 -0800 | [diff] [blame] | 9848 | adev->offload_effects_set_hpx_state = |
| 9849 | (int (*)(bool))dlsym(adev->offload_effects_lib, |
| 9850 | "offload_effects_bundle_set_hpx_state"); |
Dhananjay Kumar | d68883d | 2015-09-04 13:39:26 +0530 | [diff] [blame] | 9851 | adev->offload_effects_get_parameters = |
| 9852 | (void (*)(struct str_parms *, struct str_parms *)) |
| 9853 | dlsym(adev->offload_effects_lib, |
| 9854 | "offload_effects_bundle_get_parameters"); |
| 9855 | adev->offload_effects_set_parameters = |
| 9856 | (void (*)(struct str_parms *))dlsym(adev->offload_effects_lib, |
| 9857 | "offload_effects_bundle_set_parameters"); |
Subhash Chandra Bose Naripeddy | 1d08916 | 2013-11-13 13:31:50 -0800 | [diff] [blame] | 9858 | } |
| 9859 | } |
| 9860 | |
Ravi Kumar Alamanda | 8a0f977 | 2015-06-15 10:35:19 -0700 | [diff] [blame] | 9861 | if (access(ADM_LIBRARY_PATH, R_OK) == 0) { |
| 9862 | adev->adm_lib = dlopen(ADM_LIBRARY_PATH, RTLD_NOW); |
| 9863 | if (adev->adm_lib == NULL) { |
| 9864 | ALOGE("%s: DLOPEN failed for %s", __func__, ADM_LIBRARY_PATH); |
| 9865 | } else { |
| 9866 | ALOGV("%s: DLOPEN successful for %s", __func__, ADM_LIBRARY_PATH); |
| 9867 | adev->adm_init = (adm_init_t) |
| 9868 | dlsym(adev->adm_lib, "adm_init"); |
| 9869 | adev->adm_deinit = (adm_deinit_t) |
| 9870 | dlsym(adev->adm_lib, "adm_deinit"); |
| 9871 | adev->adm_register_input_stream = (adm_register_input_stream_t) |
| 9872 | dlsym(adev->adm_lib, "adm_register_input_stream"); |
| 9873 | adev->adm_register_output_stream = (adm_register_output_stream_t) |
| 9874 | dlsym(adev->adm_lib, "adm_register_output_stream"); |
| 9875 | adev->adm_deregister_stream = (adm_deregister_stream_t) |
| 9876 | dlsym(adev->adm_lib, "adm_deregister_stream"); |
| 9877 | adev->adm_request_focus = (adm_request_focus_t) |
| 9878 | dlsym(adev->adm_lib, "adm_request_focus"); |
| 9879 | adev->adm_abandon_focus = (adm_abandon_focus_t) |
| 9880 | dlsym(adev->adm_lib, "adm_abandon_focus"); |
Haynes Mathew George | 5beddd4 | 2016-06-27 18:33:40 -0700 | [diff] [blame] | 9881 | adev->adm_set_config = (adm_set_config_t) |
| 9882 | dlsym(adev->adm_lib, "adm_set_config"); |
| 9883 | adev->adm_request_focus_v2 = (adm_request_focus_v2_t) |
| 9884 | dlsym(adev->adm_lib, "adm_request_focus_v2"); |
| 9885 | adev->adm_is_noirq_avail = (adm_is_noirq_avail_t) |
| 9886 | dlsym(adev->adm_lib, "adm_is_noirq_avail"); |
| 9887 | adev->adm_on_routing_change = (adm_on_routing_change_t) |
| 9888 | dlsym(adev->adm_lib, "adm_on_routing_change"); |
Aniket Kumar Lata | 60586a9 | 2019-05-22 22:18:55 -0700 | [diff] [blame] | 9889 | adev->adm_request_focus_v2_1 = (adm_request_focus_v2_1_t) |
| 9890 | dlsym(adev->adm_lib, "adm_request_focus_v2_1"); |
Ravi Kumar Alamanda | 8a0f977 | 2015-06-15 10:35:19 -0700 | [diff] [blame] | 9891 | } |
| 9892 | } |
| 9893 | |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 9894 | adev->enable_voicerx = false; |
Mingming Yin | 514a8bc | 2014-07-29 15:22:21 -0700 | [diff] [blame] | 9895 | adev->bt_wb_speech_enabled = false; |
Zhou Song | 12c2950 | 2019-03-16 10:37:18 +0800 | [diff] [blame] | 9896 | adev->swb_speech_mode = SPEECH_MODE_INVALID; |
Alexy Joseph | 5e4ccbc | 2017-02-21 14:20:12 -0800 | [diff] [blame] | 9897 | //initialize this to false for now, |
| 9898 | //this will be set to true through set param |
| 9899 | adev->vr_audio_mode_enabled = false; |
Mingming Yin | 514a8bc | 2014-07-29 15:22:21 -0700 | [diff] [blame] | 9900 | |
Pradnya Chaphekar | 8a9dcd8 | 2014-09-09 09:49:10 -0700 | [diff] [blame] | 9901 | audio_extn_ds2_enable(adev); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 9902 | *device = &adev->device.common; |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 9903 | |
| 9904 | if (k_enable_extended_precision) |
| 9905 | adev_verify_devices(adev); |
| 9906 | |
Xiaojun Sang | 785b5da | 2017-08-03 15:52:29 +0800 | [diff] [blame] | 9907 | adev->dsp_bit_width_enforce_mode = |
| 9908 | adev_init_dsp_bit_width_enforce_mode(adev->mixer); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 9909 | |
Dhananjay Kumar | d6d3215 | 2016-10-13 16:11:03 +0530 | [diff] [blame] | 9910 | audio_extn_utils_update_streams_cfg_lists(adev->platform, adev->mixer, |
| 9911 | &adev->streams_output_cfg_list, |
| 9912 | &adev->streams_input_cfg_list); |
Subhash Chandra Bose Naripeddy | 19dc03b | 2014-03-10 14:43:05 -0700 | [diff] [blame] | 9913 | |
Kiran Kandi | 910e186 | 2013-10-29 13:29:42 -0700 | [diff] [blame] | 9914 | audio_device_ref_count++; |
Ravi Kumar Alamanda | dc9bb15 | 2014-09-08 15:59:58 -0700 | [diff] [blame] | 9915 | |
Ravi Kumar Alamanda | dc9bb15 | 2014-09-08 15:59:58 -0700 | [diff] [blame] | 9916 | int trial; |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 9917 | if ((property_get("vendor.audio_hal.period_size", value, NULL) > 0) || |
| 9918 | (property_get("audio_hal.period_size", value, NULL) > 0)) { |
Ravi Kumar Alamanda | dc9bb15 | 2014-09-08 15:59:58 -0700 | [diff] [blame] | 9919 | trial = atoi(value); |
| 9920 | if (period_size_is_plausible_for_low_latency(trial)) { |
| 9921 | pcm_config_low_latency.period_size = trial; |
| 9922 | pcm_config_low_latency.start_threshold = trial / 4; |
| 9923 | pcm_config_low_latency.avail_min = trial / 4; |
| 9924 | configured_low_latency_capture_period_size = trial; |
| 9925 | } |
| 9926 | } |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 9927 | if ((property_get("vendor.audio_hal.in_period_size", value, NULL) > 0) || |
| 9928 | (property_get("audio_hal.in_period_size", value, NULL) > 0)) { |
Ravi Kumar Alamanda | dc9bb15 | 2014-09-08 15:59:58 -0700 | [diff] [blame] | 9929 | trial = atoi(value); |
| 9930 | if (period_size_is_plausible_for_low_latency(trial)) { |
| 9931 | configured_low_latency_capture_period_size = trial; |
| 9932 | } |
| 9933 | } |
| 9934 | |
Vignesh Kulothungan | 7d37431 | 2018-02-21 17:12:00 -0800 | [diff] [blame] | 9935 | adev->mic_break_enabled = property_get_bool("vendor.audio.mic_break", false); |
| 9936 | |
Eric Laurent | 4b08413 | 2018-10-19 17:33:43 -0700 | [diff] [blame] | 9937 | adev->camera_orientation = CAMERA_DEFAULT; |
| 9938 | |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 9939 | if ((property_get("vendor.audio_hal.period_multiplier",value,NULL) > 0) || |
| 9940 | (property_get("audio_hal.period_multiplier",value,NULL) > 0)) { |
Haynes Mathew George | 5beddd4 | 2016-06-27 18:33:40 -0700 | [diff] [blame] | 9941 | af_period_multiplier = atoi(value); |
| 9942 | if (af_period_multiplier < 0) |
| 9943 | af_period_multiplier = 2; |
| 9944 | else if (af_period_multiplier > 4) |
| 9945 | af_period_multiplier = 4; |
| 9946 | |
| 9947 | ALOGV("new period_multiplier = %d", af_period_multiplier); |
| 9948 | } |
| 9949 | |
Arun Mirpuri | b1bec9c | 2019-01-29 16:42:45 -0800 | [diff] [blame] | 9950 | audio_extn_qdsp_init(adev->platform); |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 9951 | |
Aniket Kumar Lata | 8fc67e6 | 2017-05-02 12:33:46 -0700 | [diff] [blame] | 9952 | adev->multi_offload_enable = property_get_bool("vendor.audio.offload.multiple.enabled", false); |
Apoorv Raghuvanshi | 6e26284 | 2013-10-06 14:39:35 -0700 | [diff] [blame] | 9953 | pthread_mutex_unlock(&adev_init_lock); |
| 9954 | |
Ravi Kumar Alamanda | 8a0f977 | 2015-06-15 10:35:19 -0700 | [diff] [blame] | 9955 | if (adev->adm_init) |
| 9956 | adev->adm_data = adev->adm_init(); |
| 9957 | |
Dhananjay Kumar | ee4d200 | 2016-10-25 18:02:58 +0530 | [diff] [blame] | 9958 | qahwi_init(*device); |
Sudheer Papothi | fa9d228 | 2015-09-17 01:53:25 +0530 | [diff] [blame] | 9959 | audio_extn_perf_lock_init(); |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 9960 | audio_extn_adsp_hdlr_init(adev->mixer); |
Dhananjay Kumar | e6293dd | 2017-05-25 17:25:30 +0530 | [diff] [blame] | 9961 | |
| 9962 | audio_extn_snd_mon_init(); |
| 9963 | pthread_mutex_lock(&adev->lock); |
| 9964 | audio_extn_snd_mon_register_listener(adev, adev_snd_mon_cb); |
| 9965 | adev->card_status = CARD_STATUS_ONLINE; |
Haynes Mathew George | 01156f9 | 2018-04-13 15:29:54 -0700 | [diff] [blame] | 9966 | audio_extn_battery_properties_listener_init(adev_on_battery_status_changed); |
| 9967 | /* |
| 9968 | * if the battery state callback happens before charging can be queried, |
| 9969 | * it will be guarded with the adev->lock held in the cb function and so |
| 9970 | * the callback value will reflect the latest state |
| 9971 | */ |
| 9972 | adev->is_charging = audio_extn_battery_properties_is_charging(); |
Dhananjay Kumar | e6293dd | 2017-05-25 17:25:30 +0530 | [diff] [blame] | 9973 | audio_extn_sound_trigger_init(adev); /* dependent on snd_mon_init() */ |
Zhou Song | c1088ea | 2018-06-12 00:17:29 +0800 | [diff] [blame] | 9974 | audio_extn_sound_trigger_update_battery_status(adev->is_charging); |
justinweng | 20fb6d8 | 2019-02-21 18:49:00 -0700 | [diff] [blame] | 9975 | audio_extn_audiozoom_init(); |
Zhou Song | c1088ea | 2018-06-12 00:17:29 +0800 | [diff] [blame] | 9976 | pthread_mutex_unlock(&adev->lock); |
Satish Babu Patakokila | c3c5d43 | 2017-07-04 22:48:59 +0530 | [diff] [blame] | 9977 | /* Allocate memory for Device config params */ |
| 9978 | adev->device_cfg_params = (struct audio_device_config_param*) |
| 9979 | calloc(platform_get_max_codec_backend(), |
| 9980 | sizeof(struct audio_device_config_param)); |
| 9981 | if (adev->device_cfg_params == NULL) |
| 9982 | ALOGE("%s: Memory allocation failed for Device config params", __func__); |
Dhananjay Kumar | e6293dd | 2017-05-25 17:25:30 +0530 | [diff] [blame] | 9983 | |
Dhanalakshmi Siddani | 20628c8 | 2019-01-27 17:31:09 +0530 | [diff] [blame] | 9984 | /* |
| 9985 | * Check if new PSPD matrix mixer control is supported. If not |
| 9986 | * supported, then set flag so that old mixer ctrl is sent while |
| 9987 | * sending pspd coefficients on older kernel version. Query mixer |
| 9988 | * control for default pcm id and channel value one. |
| 9989 | */ |
| 9990 | snprintf(mixer_ctl_name, sizeof(mixer_ctl_name), |
| 9991 | "AudStr %d ChMixer Weight Ch %d", 0, 1); |
| 9992 | |
| 9993 | ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name); |
| 9994 | if (!ctl) { |
| 9995 | ALOGE("%s: ERROR. Could not get ctl for mixer cmd - %s", |
| 9996 | __func__, mixer_ctl_name); |
| 9997 | adev->use_old_pspd_mix_ctrl = true; |
| 9998 | } |
| 9999 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 10000 | ALOGV("%s: exit", __func__); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 10001 | return 0; |
| 10002 | } |
| 10003 | |
| 10004 | static struct hw_module_methods_t hal_module_methods = { |
| 10005 | .open = adev_open, |
| 10006 | }; |
| 10007 | |
| 10008 | struct audio_module HAL_MODULE_INFO_SYM = { |
| 10009 | .common = { |
| 10010 | .tag = HARDWARE_MODULE_TAG, |
| 10011 | .module_api_version = AUDIO_MODULE_API_VERSION_0_1, |
| 10012 | .hal_api_version = HARDWARE_HAL_API_VERSION, |
| 10013 | .id = AUDIO_HARDWARE_MODULE_ID, |
| 10014 | .name = "QCOM Audio HAL", |
Duy Truong | fae1962 | 2013-11-24 02:17:54 -0800 | [diff] [blame] | 10015 | .author = "The Linux Foundation", |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 10016 | .methods = &hal_module_methods, |
| 10017 | }, |
| 10018 | }; |