Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1 | /* |
vivek mehta | a51fd40 | 2016-02-04 19:49:33 -0800 | [diff] [blame] | 2 | * Copyright (C) 2013-2016 The Android Open Source Project |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "audio_hw_primary" |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 18 | #define ATRACE_TAG ATRACE_TAG_AUDIO |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 19 | /*#define LOG_NDEBUG 0*/ |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 20 | /*#define VERY_VERY_VERBOSE_LOGGING*/ |
| 21 | #ifdef VERY_VERY_VERBOSE_LOGGING |
| 22 | #define ALOGVV ALOGV |
| 23 | #else |
| 24 | #define ALOGVV(a...) do { } while(0) |
| 25 | #endif |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 26 | |
| 27 | #include <errno.h> |
| 28 | #include <pthread.h> |
| 29 | #include <stdint.h> |
| 30 | #include <sys/time.h> |
| 31 | #include <stdlib.h> |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 32 | #include <math.h> |
Eric Laurent | c4aef75 | 2013-09-12 17:45:53 -0700 | [diff] [blame] | 33 | #include <dlfcn.h> |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 34 | #include <sys/resource.h> |
| 35 | #include <sys/prctl.h> |
Kevin Rocard | b5c2819 | 2017-05-02 15:30:20 -0700 | [diff] [blame] | 36 | #include <limits.h> |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 37 | |
Haynes Mathew George | e6e2d44 | 2018-02-22 18:51:56 -0800 | [diff] [blame] | 38 | #include <log/log.h> |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 39 | #include <cutils/trace.h> |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 40 | #include <cutils/str_parms.h> |
| 41 | #include <cutils/properties.h> |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 42 | #include <cutils/atomic.h> |
Andy Hung | 572633e | 2019-02-19 11:58:24 -0800 | [diff] [blame] | 43 | #include <utils/Timers.h> // systemTime |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 44 | |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 45 | #include <hardware/audio_effect.h> |
Andy Hung | 31aca91 | 2014-03-20 17:14:59 -0700 | [diff] [blame] | 46 | #include <hardware/audio_alsaops.h> |
Suren Baghdasaryan | 7f924c2 | 2018-12-21 15:07:18 -0800 | [diff] [blame] | 47 | #include <processgroup/sched_policy.h> |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 48 | #include <system/thread_defs.h> |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 49 | #include <tinyalsa/asoundlib.h> |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 50 | #include <audio_effects/effect_aec.h> |
| 51 | #include <audio_effects/effect_ns.h> |
Andy Hung | fc044e1 | 2017-03-20 09:24:22 -0700 | [diff] [blame] | 52 | #include <audio_utils/clock.h> |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 53 | #include "audio_hw.h" |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 54 | #include "audio_extn.h" |
Wei Wang | f4837d5 | 2017-11-21 14:51:20 -0800 | [diff] [blame] | 55 | #include "audio_perf.h" |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 56 | #include "platform_api.h" |
| 57 | #include <platform.h> |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 58 | #include "voice_extn.h" |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 59 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 60 | #include "sound/compress_params.h" |
Alain Vongsouvanh | 13f26e8 | 2016-11-18 14:39:11 -0800 | [diff] [blame] | 61 | #include "audio_extn/tfa_98xx.h" |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 62 | #include "audio_extn/maxxaudio.h" |
justinweng | c6347db | 2019-02-21 18:49:00 +0800 | [diff] [blame] | 63 | #include "audio_extn/audiozoom.h" |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 64 | |
Eric Laurent | 397db57 | 2016-05-11 11:31:47 -0700 | [diff] [blame] | 65 | /* COMPRESS_OFFLOAD_FRAGMENT_SIZE must be more than 8KB and a multiple of 32KB if more than 32KB. |
| 66 | * COMPRESS_OFFLOAD_FRAGMENT_SIZE * COMPRESS_OFFLOAD_NUM_FRAGMENTS must be less than 8MB. */ |
Marco Nelissen | 32093f5 | 2015-04-08 15:14:02 -0700 | [diff] [blame] | 67 | #define COMPRESS_OFFLOAD_FRAGMENT_SIZE (256 * 1024) |
Marco Nelissen | 94c33a0 | 2015-05-12 09:11:34 -0700 | [diff] [blame] | 68 | // 2 buffers causes problems with high bitrate files |
| 69 | #define COMPRESS_OFFLOAD_NUM_FRAGMENTS 3 |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 70 | /* ToDo: Check and update a proper value in msec */ |
| 71 | #define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 96 |
vivek mehta | 4a82477 | 2017-06-08 19:05:49 -0700 | [diff] [blame] | 72 | /* treat as unsigned Q1.13 */ |
| 73 | #define APP_TYPE_GAIN_DEFAULT 0x2000 |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 74 | #define COMPRESS_PLAYBACK_VOLUME_MAX 0x2000 |
vivek mehta | 4a82477 | 2017-06-08 19:05:49 -0700 | [diff] [blame] | 75 | |
| 76 | /* treat as unsigned Q1.13 */ |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 77 | #define VOIP_PLAYBACK_VOLUME_MAX 0x2000 |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 78 | |
Eric Laurent | 51f3c66 | 2018-04-10 18:21:34 -0700 | [diff] [blame] | 79 | #define RECORD_GAIN_MIN 0.0f |
| 80 | #define RECORD_GAIN_MAX 1.0f |
| 81 | #define RECORD_VOLUME_CTL_MAX 0x2000 |
| 82 | |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 83 | #define PROXY_OPEN_RETRY_COUNT 100 |
| 84 | #define PROXY_OPEN_WAIT_TIME 20 |
| 85 | |
vivek mehta | dae4471 | 2015-07-27 14:13:18 -0700 | [diff] [blame] | 86 | #define MIN_CHANNEL_COUNT 1 |
| 87 | #define DEFAULT_CHANNEL_COUNT 2 |
| 88 | |
Jean-Michel Trivi | c075069 | 2015-10-12 12:12:32 -0700 | [diff] [blame] | 89 | #ifndef MAX_TARGET_SPECIFIC_CHANNEL_CNT |
| 90 | #define MAX_CHANNEL_COUNT 1 |
| 91 | #else |
vivek mehta | dae4471 | 2015-07-27 14:13:18 -0700 | [diff] [blame] | 92 | #define MAX_CHANNEL_COUNT atoi(XSTR(MAX_TARGET_SPECIFIC_CHANNEL_CNT)) |
| 93 | #define XSTR(x) STR(x) |
| 94 | #define STR(x) #x |
Jean-Michel Trivi | c075069 | 2015-10-12 12:12:32 -0700 | [diff] [blame] | 95 | #endif |
Eric Laurent | 74b5576 | 2017-07-09 17:04:53 -0700 | [diff] [blame] | 96 | #define MAX_HIFI_CHANNEL_COUNT 8 |
vivek mehta | dae4471 | 2015-07-27 14:13:18 -0700 | [diff] [blame] | 97 | |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 98 | #define ULL_PERIOD_SIZE (DEFAULT_OUTPUT_SAMPLING_RATE/1000) |
| 99 | |
Glenn Kasten | 4f99339 | 2014-05-14 07:30:48 -0700 | [diff] [blame] | 100 | static unsigned int configured_low_latency_capture_period_size = |
| 101 | LOW_LATENCY_CAPTURE_PERIOD_SIZE; |
| 102 | |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 103 | |
| 104 | #define MMAP_PERIOD_SIZE (DEFAULT_OUTPUT_SAMPLING_RATE/1000) |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 105 | #define MMAP_PERIOD_COUNT_MIN 32 |
| 106 | #define MMAP_PERIOD_COUNT_MAX 512 |
| 107 | #define MMAP_PERIOD_COUNT_DEFAULT (MMAP_PERIOD_COUNT_MAX) |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 108 | |
Andy Hung | 31aca91 | 2014-03-20 17:14:59 -0700 | [diff] [blame] | 109 | /* This constant enables extended precision handling. |
| 110 | * TODO The flag is off until more testing is done. |
| 111 | */ |
| 112 | static const bool k_enable_extended_precision = false; |
| 113 | |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 114 | struct pcm_config pcm_config_deep_buffer = { |
vivek mehta | dae4471 | 2015-07-27 14:13:18 -0700 | [diff] [blame] | 115 | .channels = DEFAULT_CHANNEL_COUNT, |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 116 | .rate = DEFAULT_OUTPUT_SAMPLING_RATE, |
| 117 | .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE, |
| 118 | .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT, |
| 119 | .format = PCM_FORMAT_S16_LE, |
| 120 | .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4, |
| 121 | .stop_threshold = INT_MAX, |
| 122 | .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4, |
| 123 | }; |
| 124 | |
| 125 | struct pcm_config pcm_config_low_latency = { |
vivek mehta | dae4471 | 2015-07-27 14:13:18 -0700 | [diff] [blame] | 126 | .channels = DEFAULT_CHANNEL_COUNT, |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 127 | .rate = DEFAULT_OUTPUT_SAMPLING_RATE, |
| 128 | .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE, |
| 129 | .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT, |
| 130 | .format = PCM_FORMAT_S16_LE, |
| 131 | .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4, |
| 132 | .stop_threshold = INT_MAX, |
| 133 | .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4, |
| 134 | }; |
| 135 | |
Vignesh Kulothungan | 019d19b | 2019-01-23 11:09:18 -0800 | [diff] [blame] | 136 | struct pcm_config pcm_config_haptics_audio = { |
| 137 | .channels = 1, |
| 138 | .rate = DEFAULT_OUTPUT_SAMPLING_RATE, |
| 139 | .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE, |
| 140 | .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT, |
| 141 | .format = PCM_FORMAT_S16_LE, |
| 142 | .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4, |
| 143 | .stop_threshold = INT_MAX, |
| 144 | .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4, |
| 145 | }; |
| 146 | |
| 147 | struct pcm_config pcm_config_haptics = { |
| 148 | .channels = 1, |
| 149 | .rate = DEFAULT_OUTPUT_SAMPLING_RATE, |
| 150 | .period_count = 2, |
| 151 | .format = PCM_FORMAT_S16_LE, |
| 152 | .stop_threshold = INT_MAX, |
| 153 | .avail_min = 0, |
| 154 | }; |
| 155 | |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 156 | static int af_period_multiplier = 4; |
| 157 | struct pcm_config pcm_config_rt = { |
| 158 | .channels = DEFAULT_CHANNEL_COUNT, |
| 159 | .rate = DEFAULT_OUTPUT_SAMPLING_RATE, |
| 160 | .period_size = ULL_PERIOD_SIZE, //1 ms |
| 161 | .period_count = 512, //=> buffer size is 512ms |
| 162 | .format = PCM_FORMAT_S16_LE, |
| 163 | .start_threshold = ULL_PERIOD_SIZE*8, //8ms |
| 164 | .stop_threshold = INT_MAX, |
| 165 | .silence_threshold = 0, |
| 166 | .silence_size = 0, |
| 167 | .avail_min = ULL_PERIOD_SIZE, //1 ms |
| 168 | }; |
| 169 | |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 170 | struct pcm_config pcm_config_hdmi_multi = { |
| 171 | .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */ |
| 172 | .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */ |
| 173 | .period_size = HDMI_MULTI_PERIOD_SIZE, |
| 174 | .period_count = HDMI_MULTI_PERIOD_COUNT, |
| 175 | .format = PCM_FORMAT_S16_LE, |
| 176 | .start_threshold = 0, |
| 177 | .stop_threshold = INT_MAX, |
| 178 | .avail_min = 0, |
| 179 | }; |
| 180 | |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 181 | struct pcm_config pcm_config_mmap_playback = { |
| 182 | .channels = DEFAULT_CHANNEL_COUNT, |
| 183 | .rate = DEFAULT_OUTPUT_SAMPLING_RATE, |
| 184 | .period_size = MMAP_PERIOD_SIZE, |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 185 | .period_count = MMAP_PERIOD_COUNT_DEFAULT, |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 186 | .format = PCM_FORMAT_S16_LE, |
| 187 | .start_threshold = MMAP_PERIOD_SIZE*8, |
| 188 | .stop_threshold = INT32_MAX, |
| 189 | .silence_threshold = 0, |
| 190 | .silence_size = 0, |
| 191 | .avail_min = MMAP_PERIOD_SIZE, //1 ms |
| 192 | }; |
| 193 | |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 194 | struct pcm_config pcm_config_hifi = { |
| 195 | .channels = DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */ |
| 196 | .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */ |
| 197 | .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE, /* change #define */ |
| 198 | .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT, |
| 199 | .format = PCM_FORMAT_S24_3LE, |
| 200 | .start_threshold = 0, |
| 201 | .stop_threshold = INT_MAX, |
| 202 | .avail_min = 0, |
| 203 | }; |
| 204 | |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 205 | struct pcm_config pcm_config_audio_capture = { |
vivek mehta | dae4471 | 2015-07-27 14:13:18 -0700 | [diff] [blame] | 206 | .channels = DEFAULT_CHANNEL_COUNT, |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 207 | .period_count = AUDIO_CAPTURE_PERIOD_COUNT, |
| 208 | .format = PCM_FORMAT_S16_LE, |
Eric Laurent | e2d2d1d | 2015-07-06 17:54:15 -0700 | [diff] [blame] | 209 | .stop_threshold = INT_MAX, |
| 210 | .avail_min = 0, |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 211 | }; |
| 212 | |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 213 | struct pcm_config pcm_config_audio_capture_rt = { |
| 214 | .channels = DEFAULT_CHANNEL_COUNT, |
| 215 | .rate = DEFAULT_OUTPUT_SAMPLING_RATE, |
| 216 | .period_size = ULL_PERIOD_SIZE, |
| 217 | .period_count = 512, |
| 218 | .format = PCM_FORMAT_S16_LE, |
| 219 | .start_threshold = 0, |
| 220 | .stop_threshold = INT_MAX, |
| 221 | .silence_threshold = 0, |
| 222 | .silence_size = 0, |
| 223 | .avail_min = ULL_PERIOD_SIZE, //1 ms |
| 224 | }; |
| 225 | |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 226 | struct pcm_config pcm_config_mmap_capture = { |
| 227 | .channels = DEFAULT_CHANNEL_COUNT, |
| 228 | .rate = DEFAULT_OUTPUT_SAMPLING_RATE, |
| 229 | .period_size = MMAP_PERIOD_SIZE, |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 230 | .period_count = MMAP_PERIOD_COUNT_DEFAULT, |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 231 | .format = PCM_FORMAT_S16_LE, |
| 232 | .start_threshold = 0, |
| 233 | .stop_threshold = INT_MAX, |
| 234 | .silence_threshold = 0, |
| 235 | .silence_size = 0, |
| 236 | .avail_min = MMAP_PERIOD_SIZE, //1 ms |
| 237 | }; |
| 238 | |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 239 | struct pcm_config pcm_config_voip = { |
| 240 | .channels = 1, |
| 241 | .period_count = 2, |
| 242 | .format = PCM_FORMAT_S16_LE, |
| 243 | .stop_threshold = INT_MAX, |
| 244 | .avail_min = 0, |
| 245 | }; |
| 246 | |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 247 | #define AFE_PROXY_CHANNEL_COUNT 2 |
| 248 | #define AFE_PROXY_SAMPLING_RATE 48000 |
| 249 | |
jiabin | ad481a7 | 2018-07-23 12:03:17 -0700 | [diff] [blame] | 250 | #define AFE_PROXY_PLAYBACK_PERIOD_SIZE 256 |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 251 | #define AFE_PROXY_PLAYBACK_PERIOD_COUNT 4 |
| 252 | |
| 253 | struct pcm_config pcm_config_afe_proxy_playback = { |
| 254 | .channels = AFE_PROXY_CHANNEL_COUNT, |
| 255 | .rate = AFE_PROXY_SAMPLING_RATE, |
| 256 | .period_size = AFE_PROXY_PLAYBACK_PERIOD_SIZE, |
| 257 | .period_count = AFE_PROXY_PLAYBACK_PERIOD_COUNT, |
| 258 | .format = PCM_FORMAT_S16_LE, |
| 259 | .start_threshold = AFE_PROXY_PLAYBACK_PERIOD_SIZE, |
| 260 | .stop_threshold = INT_MAX, |
| 261 | .avail_min = AFE_PROXY_PLAYBACK_PERIOD_SIZE, |
| 262 | }; |
| 263 | |
jiabin | ad481a7 | 2018-07-23 12:03:17 -0700 | [diff] [blame] | 264 | #define AFE_PROXY_RECORD_PERIOD_SIZE 256 |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 265 | #define AFE_PROXY_RECORD_PERIOD_COUNT 4 |
| 266 | |
| 267 | struct pcm_config pcm_config_afe_proxy_record = { |
| 268 | .channels = AFE_PROXY_CHANNEL_COUNT, |
| 269 | .rate = AFE_PROXY_SAMPLING_RATE, |
| 270 | .period_size = AFE_PROXY_RECORD_PERIOD_SIZE, |
| 271 | .period_count = AFE_PROXY_RECORD_PERIOD_COUNT, |
| 272 | .format = PCM_FORMAT_S16_LE, |
| 273 | .start_threshold = AFE_PROXY_RECORD_PERIOD_SIZE, |
jiabin | 5d0261e | 2018-05-25 09:43:59 -0700 | [diff] [blame] | 274 | .stop_threshold = AFE_PROXY_RECORD_PERIOD_SIZE * AFE_PROXY_RECORD_PERIOD_COUNT, |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 275 | .avail_min = AFE_PROXY_RECORD_PERIOD_SIZE, |
| 276 | }; |
| 277 | |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 278 | const char * const use_case_table[AUDIO_USECASE_MAX] = { |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 279 | [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback", |
| 280 | [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback", |
Vignesh Kulothungan | 019d19b | 2019-01-23 11:09:18 -0800 | [diff] [blame] | 281 | [USECASE_AUDIO_PLAYBACK_WITH_HAPTICS] = "audio-with-haptics-playback", |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 282 | [USECASE_AUDIO_PLAYBACK_HIFI] = "hifi-playback", |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 283 | [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback", |
Ravi Kumar Alamanda | f78a4d9 | 2015-04-24 15:18:23 -0700 | [diff] [blame] | 284 | [USECASE_AUDIO_PLAYBACK_TTS] = "audio-tts-playback", |
Ravi Kumar Alamanda | 2bc7b02 | 2015-06-25 20:08:01 -0700 | [diff] [blame] | 285 | [USECASE_AUDIO_PLAYBACK_ULL] = "audio-ull-playback", |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 286 | [USECASE_AUDIO_PLAYBACK_MMAP] = "mmap-playback", |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 287 | |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 288 | [USECASE_AUDIO_RECORD] = "audio-record", |
| 289 | [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record", |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 290 | [USECASE_AUDIO_RECORD_MMAP] = "mmap-record", |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 291 | [USECASE_AUDIO_RECORD_HIFI] = "hifi-record", |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 292 | |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 293 | [USECASE_AUDIO_HFP_SCO] = "hfp-sco", |
| 294 | [USECASE_AUDIO_HFP_SCO_WB] = "hfp-sco-wb", |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 295 | |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 296 | [USECASE_VOICE_CALL] = "voice-call", |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 297 | [USECASE_VOICE2_CALL] = "voice2-call", |
| 298 | [USECASE_VOLTE_CALL] = "volte-call", |
| 299 | [USECASE_QCHAT_CALL] = "qchat-call", |
| 300 | [USECASE_VOWLAN_CALL] = "vowlan-call", |
vivek mehta | a51fd40 | 2016-02-04 19:49:33 -0800 | [diff] [blame] | 301 | [USECASE_VOICEMMODE1_CALL] = "voicemmode1-call", |
| 302 | [USECASE_VOICEMMODE2_CALL] = "voicemmode2-call", |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 303 | |
Ravi Kumar Alamanda | 6386300 | 2015-04-22 11:15:25 -0700 | [diff] [blame] | 304 | [USECASE_AUDIO_SPKR_CALIB_RX] = "spkr-rx-calib", |
| 305 | [USECASE_AUDIO_SPKR_CALIB_TX] = "spkr-vi-record", |
| 306 | |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 307 | [USECASE_AUDIO_PLAYBACK_AFE_PROXY] = "afe-proxy-playback", |
| 308 | [USECASE_AUDIO_RECORD_AFE_PROXY] = "afe-proxy-record", |
vivek mehta | 781065c | 2017-04-04 12:55:01 -0700 | [diff] [blame] | 309 | |
| 310 | [USECASE_INCALL_REC_UPLINK] = "incall-rec-uplink", |
| 311 | [USECASE_INCALL_REC_DOWNLINK] = "incall-rec-downlink", |
| 312 | [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK] = "incall-rec-uplink-and-downlink", |
| 313 | |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 314 | [USECASE_AUDIO_PLAYBACK_VOIP] = "audio-playback-voip", |
| 315 | [USECASE_AUDIO_RECORD_VOIP] = "audio-record-voip", |
Nadav Bar | 3d72cfc | 2018-01-07 12:19:24 +0200 | [diff] [blame] | 316 | |
| 317 | [USECASE_INCALL_MUSIC_UPLINK] = "incall-music-uplink", |
Aniket Kumar Lata | 9954631 | 2018-03-19 21:38:38 -0700 | [diff] [blame] | 318 | |
| 319 | [USECASE_AUDIO_A2DP_ABR_FEEDBACK] = "a2dp-abr-feedback", |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 320 | }; |
| 321 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 322 | |
| 323 | #define STRING_TO_ENUM(string) { #string, string } |
| 324 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 325 | struct string_to_enum { |
| 326 | const char *name; |
| 327 | uint32_t value; |
| 328 | }; |
| 329 | |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 330 | static const struct string_to_enum channels_name_to_enum_table[] = { |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 331 | STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO), |
| 332 | STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1), |
| 333 | STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1), |
Eric Laurent | 74b5576 | 2017-07-09 17:04:53 -0700 | [diff] [blame] | 334 | STRING_TO_ENUM(AUDIO_CHANNEL_IN_MONO), |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 335 | STRING_TO_ENUM(AUDIO_CHANNEL_IN_STEREO), |
Eric Laurent | 74b5576 | 2017-07-09 17:04:53 -0700 | [diff] [blame] | 336 | STRING_TO_ENUM(AUDIO_CHANNEL_IN_FRONT_BACK), |
| 337 | STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_1), |
| 338 | STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_2), |
| 339 | STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_3), |
| 340 | STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_4), |
| 341 | STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_5), |
| 342 | STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_6), |
| 343 | STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_7), |
| 344 | STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_8), |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 345 | }; |
| 346 | |
Haynes Mathew George | 5191a85 | 2013-09-11 14:19:36 -0700 | [diff] [blame] | 347 | static int set_voice_volume_l(struct audio_device *adev, float volume); |
vivek mehta | 1a9b7c0 | 2015-06-25 11:49:38 -0700 | [diff] [blame] | 348 | static struct audio_device *adev = NULL; |
Andy Hung | 9e737de | 2017-05-22 10:51:22 -0700 | [diff] [blame] | 349 | static pthread_mutex_t adev_init_lock = PTHREAD_MUTEX_INITIALIZER; |
vivek mehta | 1a9b7c0 | 2015-06-25 11:49:38 -0700 | [diff] [blame] | 350 | static unsigned int audio_device_ref_count; |
vivek mehta | fb4d7bd | 2016-04-29 03:16:47 -0700 | [diff] [blame] | 351 | //cache last MBDRC cal step level |
| 352 | static int last_known_cal_step = -1 ; |
vivek mehta | 1a9b7c0 | 2015-06-25 11:49:38 -0700 | [diff] [blame] | 353 | |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 354 | static int check_a2dp_restore_l(struct audio_device *adev, struct stream_out *out, bool restore); |
| 355 | static int set_compr_volume(struct audio_stream_out *stream, float left, float right); |
| 356 | |
justinweng | c6347db | 2019-02-21 18:49:00 +0800 | [diff] [blame] | 357 | static int in_set_microphone_direction(const struct audio_stream_in *stream, |
| 358 | audio_microphone_direction_t dir); |
| 359 | static int in_set_microphone_field_dimension(const struct audio_stream_in *stream, float zoom); |
| 360 | |
| 361 | |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 362 | static bool may_use_noirq_mode(struct audio_device *adev, audio_usecase_t uc_id, |
| 363 | int flags __unused) |
| 364 | { |
| 365 | int dir = 0; |
| 366 | switch (uc_id) { |
| 367 | case USECASE_AUDIO_RECORD_LOW_LATENCY: |
| 368 | dir = 1; |
| 369 | case USECASE_AUDIO_PLAYBACK_ULL: |
| 370 | break; |
| 371 | default: |
| 372 | return false; |
| 373 | } |
| 374 | |
| 375 | int dev_id = platform_get_pcm_device_id(uc_id, dir == 0 ? |
| 376 | PCM_PLAYBACK : PCM_CAPTURE); |
| 377 | if (adev->adm_is_noirq_avail) |
| 378 | return adev->adm_is_noirq_avail(adev->adm_data, |
| 379 | adev->snd_card, dev_id, dir); |
| 380 | return false; |
| 381 | } |
| 382 | |
| 383 | static void register_out_stream(struct stream_out *out) |
| 384 | { |
| 385 | struct audio_device *adev = out->dev; |
| 386 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) |
| 387 | return; |
| 388 | |
| 389 | if (!adev->adm_register_output_stream) |
| 390 | return; |
| 391 | |
| 392 | adev->adm_register_output_stream(adev->adm_data, |
| 393 | out->handle, |
| 394 | out->flags); |
| 395 | |
| 396 | if (!adev->adm_set_config) |
| 397 | return; |
| 398 | |
| 399 | if (out->realtime) { |
| 400 | adev->adm_set_config(adev->adm_data, |
| 401 | out->handle, |
| 402 | out->pcm, &out->config); |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | static void register_in_stream(struct stream_in *in) |
| 407 | { |
| 408 | struct audio_device *adev = in->dev; |
| 409 | if (!adev->adm_register_input_stream) |
| 410 | return; |
| 411 | |
| 412 | adev->adm_register_input_stream(adev->adm_data, |
| 413 | in->capture_handle, |
| 414 | in->flags); |
| 415 | |
| 416 | if (!adev->adm_set_config) |
| 417 | return; |
| 418 | |
| 419 | if (in->realtime) { |
| 420 | adev->adm_set_config(adev->adm_data, |
| 421 | in->capture_handle, |
| 422 | in->pcm, |
| 423 | &in->config); |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | static void request_out_focus(struct stream_out *out, long ns) |
| 428 | { |
| 429 | struct audio_device *adev = out->dev; |
| 430 | |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 431 | if (adev->adm_request_focus_v2) { |
| 432 | adev->adm_request_focus_v2(adev->adm_data, out->handle, ns); |
| 433 | } else if (adev->adm_request_focus) { |
| 434 | adev->adm_request_focus(adev->adm_data, out->handle); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | static void request_in_focus(struct stream_in *in, long ns) |
| 439 | { |
| 440 | struct audio_device *adev = in->dev; |
| 441 | |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 442 | if (adev->adm_request_focus_v2) { |
| 443 | adev->adm_request_focus_v2(adev->adm_data, in->capture_handle, ns); |
| 444 | } else if (adev->adm_request_focus) { |
| 445 | adev->adm_request_focus(adev->adm_data, in->capture_handle); |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | static void release_out_focus(struct stream_out *out, long ns __unused) |
| 450 | { |
| 451 | struct audio_device *adev = out->dev; |
| 452 | |
| 453 | if (adev->adm_abandon_focus) |
| 454 | adev->adm_abandon_focus(adev->adm_data, out->handle); |
| 455 | } |
| 456 | |
| 457 | static void release_in_focus(struct stream_in *in, long ns __unused) |
| 458 | { |
| 459 | struct audio_device *adev = in->dev; |
| 460 | if (adev->adm_abandon_focus) |
| 461 | adev->adm_abandon_focus(adev->adm_data, in->capture_handle); |
| 462 | } |
| 463 | |
Haynes Mathew George | c735fb0 | 2016-06-30 18:00:28 -0700 | [diff] [blame] | 464 | static int parse_snd_card_status(struct str_parms * parms, int * card, |
| 465 | card_status_t * status) |
| 466 | { |
| 467 | char value[32]={0}; |
| 468 | char state[32]={0}; |
| 469 | |
| 470 | int ret = str_parms_get_str(parms, "SND_CARD_STATUS", value, sizeof(value)); |
| 471 | |
| 472 | if (ret < 0) |
| 473 | return -1; |
| 474 | |
| 475 | // sscanf should be okay as value is of max length 32. |
| 476 | // same as sizeof state. |
| 477 | if (sscanf(value, "%d,%s", card, state) < 2) |
| 478 | return -1; |
| 479 | |
| 480 | *status = !strcmp(state, "ONLINE") ? CARD_STATUS_ONLINE : |
| 481 | CARD_STATUS_OFFLINE; |
| 482 | return 0; |
| 483 | } |
| 484 | |
vivek mehta | 4012509 | 2017-08-21 18:48:51 -0700 | [diff] [blame] | 485 | // always call with adev lock held |
| 486 | void send_gain_dep_calibration_l() { |
| 487 | if (last_known_cal_step >= 0) |
| 488 | platform_send_gain_dep_cal(adev->platform, last_known_cal_step); |
| 489 | } |
| 490 | |
vivek mehta | 1a9b7c0 | 2015-06-25 11:49:38 -0700 | [diff] [blame] | 491 | __attribute__ ((visibility ("default"))) |
| 492 | bool audio_hw_send_gain_dep_calibration(int level) { |
| 493 | bool ret_val = false; |
| 494 | ALOGV("%s: enter ... ", __func__); |
| 495 | |
| 496 | pthread_mutex_lock(&adev_init_lock); |
| 497 | |
| 498 | if (adev != NULL && adev->platform != NULL) { |
| 499 | pthread_mutex_lock(&adev->lock); |
vivek mehta | 4012509 | 2017-08-21 18:48:51 -0700 | [diff] [blame] | 500 | last_known_cal_step = level; |
| 501 | send_gain_dep_calibration_l(); |
vivek mehta | 1a9b7c0 | 2015-06-25 11:49:38 -0700 | [diff] [blame] | 502 | pthread_mutex_unlock(&adev->lock); |
| 503 | } else { |
| 504 | ALOGE("%s: %s is NULL", __func__, adev == NULL ? "adev" : "adev->platform"); |
| 505 | } |
| 506 | |
| 507 | pthread_mutex_unlock(&adev_init_lock); |
| 508 | |
| 509 | ALOGV("%s: exit with ret_val %d ", __func__, ret_val); |
| 510 | return ret_val; |
| 511 | } |
Haynes Mathew George | 5191a85 | 2013-09-11 14:19:36 -0700 | [diff] [blame] | 512 | |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 513 | #ifdef MAXXAUDIO_QDSP_ENABLED |
| 514 | bool audio_hw_send_ma_parameter(int stream_type, float vol, bool active) |
| 515 | { |
| 516 | bool ret = false; |
| 517 | ALOGV("%s: enter ...", __func__); |
| 518 | |
| 519 | pthread_mutex_lock(&adev_init_lock); |
| 520 | |
| 521 | if (adev != NULL && adev->platform != NULL) { |
| 522 | pthread_mutex_lock(&adev->lock); |
| 523 | ret = audio_extn_ma_set_state(adev, stream_type, vol, active); |
| 524 | pthread_mutex_unlock(&adev->lock); |
| 525 | } |
| 526 | |
| 527 | pthread_mutex_unlock(&adev_init_lock); |
| 528 | |
| 529 | ALOGV("%s: exit with ret %d", __func__, ret); |
| 530 | return ret; |
| 531 | } |
| 532 | #else |
| 533 | #define audio_hw_send_ma_parameter(stream_type, vol, active) (0) |
| 534 | #endif |
| 535 | |
vivek mehta | a8d7c92 | 2016-05-25 14:40:44 -0700 | [diff] [blame] | 536 | __attribute__ ((visibility ("default"))) |
| 537 | int audio_hw_get_gain_level_mapping(struct amp_db_and_gain_table *mapping_tbl, |
| 538 | int table_size) { |
| 539 | int ret_val = 0; |
| 540 | ALOGV("%s: enter ... ", __func__); |
| 541 | |
| 542 | pthread_mutex_lock(&adev_init_lock); |
| 543 | if (adev == NULL) { |
| 544 | ALOGW("%s: adev is NULL .... ", __func__); |
| 545 | goto done; |
| 546 | } |
| 547 | |
| 548 | pthread_mutex_lock(&adev->lock); |
| 549 | ret_val = platform_get_gain_level_mapping(mapping_tbl, table_size); |
| 550 | pthread_mutex_unlock(&adev->lock); |
| 551 | done: |
| 552 | pthread_mutex_unlock(&adev_init_lock); |
| 553 | ALOGV("%s: exit ... ", __func__); |
| 554 | return ret_val; |
| 555 | } |
| 556 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 557 | static bool is_supported_format(audio_format_t format) |
| 558 | { |
Eric Laurent | 8251ac8 | 2014-07-23 11:00:25 -0700 | [diff] [blame] | 559 | switch (format) { |
| 560 | case AUDIO_FORMAT_MP3: |
| 561 | case AUDIO_FORMAT_AAC_LC: |
| 562 | case AUDIO_FORMAT_AAC_HE_V1: |
| 563 | case AUDIO_FORMAT_AAC_HE_V2: |
| 564 | return true; |
| 565 | default: |
| 566 | break; |
| 567 | } |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 568 | return false; |
| 569 | } |
| 570 | |
juyuchen | be5c67e | 2018-08-29 17:19:20 +0800 | [diff] [blame] | 571 | static bool is_supported_24bits_audiosource(audio_source_t source) |
| 572 | { |
| 573 | switch (source) { |
| 574 | case AUDIO_SOURCE_UNPROCESSED: |
| 575 | #ifdef ENABLED_24BITS_CAMCORDER |
| 576 | case AUDIO_SOURCE_CAMCORDER: |
| 577 | #endif |
| 578 | return true; |
| 579 | default: |
| 580 | break; |
| 581 | } |
| 582 | return false; |
| 583 | } |
| 584 | |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 585 | static inline bool is_mmap_usecase(audio_usecase_t uc_id) |
| 586 | { |
| 587 | return (uc_id == USECASE_AUDIO_RECORD_AFE_PROXY) || |
| 588 | (uc_id == USECASE_AUDIO_PLAYBACK_AFE_PROXY); |
| 589 | } |
| 590 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 591 | static int get_snd_codec_id(audio_format_t format) |
| 592 | { |
| 593 | int id = 0; |
| 594 | |
Eric Laurent | 8251ac8 | 2014-07-23 11:00:25 -0700 | [diff] [blame] | 595 | switch (format & AUDIO_FORMAT_MAIN_MASK) { |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 596 | case AUDIO_FORMAT_MP3: |
| 597 | id = SND_AUDIOCODEC_MP3; |
| 598 | break; |
| 599 | case AUDIO_FORMAT_AAC: |
| 600 | id = SND_AUDIOCODEC_AAC; |
| 601 | break; |
| 602 | default: |
| 603 | ALOGE("%s: Unsupported audio format", __func__); |
| 604 | } |
| 605 | |
| 606 | return id; |
| 607 | } |
Ravi Kumar Alamanda | f996704 | 2013-02-14 19:35:14 -0800 | [diff] [blame] | 608 | |
Alain Vongsouvanh | 13f26e8 | 2016-11-18 14:39:11 -0800 | [diff] [blame] | 609 | static int audio_ssr_status(struct audio_device *adev) |
| 610 | { |
| 611 | int ret = 0; |
| 612 | struct mixer_ctl *ctl; |
| 613 | const char *mixer_ctl_name = "Audio SSR Status"; |
| 614 | |
| 615 | ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name); |
| 616 | ret = mixer_ctl_get_value(ctl, 0); |
| 617 | ALOGD("%s: value: %d", __func__, ret); |
| 618 | return ret; |
| 619 | } |
| 620 | |
vivek mehta | 4a82477 | 2017-06-08 19:05:49 -0700 | [diff] [blame] | 621 | static void stream_app_type_cfg_init(struct stream_app_type_cfg *cfg) |
| 622 | { |
| 623 | cfg->gain[0] = cfg->gain[1] = APP_TYPE_GAIN_DEFAULT; |
| 624 | } |
| 625 | |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 626 | static bool is_btsco_device(snd_device_t out_snd_device, snd_device_t in_snd_device) |
| 627 | { |
| 628 | return out_snd_device == SND_DEVICE_OUT_BT_SCO || |
| 629 | out_snd_device == SND_DEVICE_OUT_BT_SCO_WB || |
| 630 | in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_WB_NREC || |
| 631 | in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_WB || |
| 632 | in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_NREC || |
| 633 | in_snd_device == SND_DEVICE_IN_BT_SCO_MIC; |
| 634 | |
| 635 | } |
| 636 | |
| 637 | static bool is_a2dp_device(snd_device_t out_snd_device) |
| 638 | { |
| 639 | return out_snd_device == SND_DEVICE_OUT_BT_A2DP; |
| 640 | } |
| 641 | |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 642 | int enable_audio_route(struct audio_device *adev, |
| 643 | struct audio_usecase *usecase) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 644 | { |
yixuanjiang | 509f0a7 | 2018-09-06 18:37:23 +0800 | [diff] [blame] | 645 | snd_device_t snd_device; |
Andy Hung | 11f10e2 | 2018-10-19 17:47:12 -0700 | [diff] [blame] | 646 | char mixer_path[MIXER_PATH_MAX_LENGTH]; |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 647 | |
| 648 | if (usecase == NULL) |
| 649 | return -EINVAL; |
| 650 | |
| 651 | ALOGV("%s: enter: usecase(%d)", __func__, usecase->id); |
| 652 | |
yixuanjiang | 509f0a7 | 2018-09-06 18:37:23 +0800 | [diff] [blame] | 653 | if (usecase->type == PCM_CAPTURE) |
| 654 | snd_device = usecase->in_snd_device; |
| 655 | else |
| 656 | snd_device = usecase->out_snd_device; |
Yamit Mehta | e3b9956 | 2016-09-16 22:44:00 +0530 | [diff] [blame] | 657 | audio_extn_utils_send_app_type_cfg(adev, usecase); |
Jasmine Cha | fb2ea2c | 2019-03-04 18:12:47 +0800 | [diff] [blame] | 658 | audio_extn_ma_set_device(usecase); |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 659 | audio_extn_utils_send_audio_calibration(adev, usecase); |
Andy Hung | 11f10e2 | 2018-10-19 17:47:12 -0700 | [diff] [blame] | 660 | |
| 661 | // we shouldn't truncate mixer_path |
| 662 | ALOGW_IF(strlcpy(mixer_path, use_case_table[usecase->id], sizeof(mixer_path)) |
| 663 | >= sizeof(mixer_path), "%s: truncation on mixer path", __func__); |
| 664 | // this also appends to mixer_path |
yixuanjiang | 509f0a7 | 2018-09-06 18:37:23 +0800 | [diff] [blame] | 665 | platform_add_backend_name(adev->platform, mixer_path, snd_device); |
Andy Hung | 11f10e2 | 2018-10-19 17:47:12 -0700 | [diff] [blame] | 666 | |
yixuanjiang | 509f0a7 | 2018-09-06 18:37:23 +0800 | [diff] [blame] | 667 | audio_extn_sound_trigger_update_stream_status(usecase, ST_EVENT_STREAM_BUSY); |
Eric Laurent | 2e140aa | 2016-06-30 17:14:46 -0700 | [diff] [blame] | 668 | ALOGD("%s: usecase(%d) apply and update mixer path: %s", __func__, usecase->id, mixer_path); |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 669 | audio_route_apply_and_update_path(adev->audio_route, mixer_path); |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 670 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 671 | ALOGV("%s: exit", __func__); |
| 672 | return 0; |
| 673 | } |
| 674 | |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 675 | int disable_audio_route(struct audio_device *adev, |
| 676 | struct audio_usecase *usecase) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 677 | { |
yixuanjiang | 509f0a7 | 2018-09-06 18:37:23 +0800 | [diff] [blame] | 678 | snd_device_t snd_device; |
Andy Hung | 11f10e2 | 2018-10-19 17:47:12 -0700 | [diff] [blame] | 679 | char mixer_path[MIXER_PATH_MAX_LENGTH]; |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 680 | |
| 681 | if (usecase == NULL) |
| 682 | return -EINVAL; |
| 683 | |
| 684 | ALOGV("%s: enter: usecase(%d)", __func__, usecase->id); |
yixuanjiang | 509f0a7 | 2018-09-06 18:37:23 +0800 | [diff] [blame] | 685 | if (usecase->type == PCM_CAPTURE) |
| 686 | snd_device = usecase->in_snd_device; |
| 687 | else |
| 688 | snd_device = usecase->out_snd_device; |
Andy Hung | 11f10e2 | 2018-10-19 17:47:12 -0700 | [diff] [blame] | 689 | |
| 690 | // we shouldn't truncate mixer_path |
| 691 | ALOGW_IF(strlcpy(mixer_path, use_case_table[usecase->id], sizeof(mixer_path)) |
| 692 | >= sizeof(mixer_path), "%s: truncation on mixer path", __func__); |
| 693 | // this also appends to mixer_path |
yixuanjiang | 509f0a7 | 2018-09-06 18:37:23 +0800 | [diff] [blame] | 694 | platform_add_backend_name(adev->platform, mixer_path, snd_device); |
Eric Laurent | 2e140aa | 2016-06-30 17:14:46 -0700 | [diff] [blame] | 695 | ALOGD("%s: usecase(%d) reset and update mixer path: %s", __func__, usecase->id, mixer_path); |
Andy Hung | 11f10e2 | 2018-10-19 17:47:12 -0700 | [diff] [blame] | 696 | |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 697 | audio_route_reset_and_update_path(adev->audio_route, mixer_path); |
Mikhail Naganov | 4ee6e3e | 2018-03-21 16:28:35 +0000 | [diff] [blame] | 698 | audio_extn_sound_trigger_update_stream_status(usecase, ST_EVENT_STREAM_FREE); |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 699 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 700 | ALOGV("%s: exit", __func__); |
| 701 | return 0; |
| 702 | } |
| 703 | |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 704 | int enable_snd_device(struct audio_device *adev, |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 705 | snd_device_t snd_device) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 706 | { |
Ravi Kumar Alamanda | b7ea4f5 | 2015-06-08 16:44:05 -0700 | [diff] [blame] | 707 | int i, num_devices = 0; |
| 708 | snd_device_t new_snd_devices[2]; |
vivek mehta | de4849c | 2016-03-03 17:23:38 -0800 | [diff] [blame] | 709 | int ret_val = -EINVAL; |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 710 | if (snd_device < SND_DEVICE_MIN || |
| 711 | snd_device >= SND_DEVICE_MAX) { |
Ravi Kumar Alamanda | 3b1816c | 2013-02-27 23:01:21 -0800 | [diff] [blame] | 712 | ALOGE("%s: Invalid sound device %d", __func__, snd_device); |
vivek mehta | de4849c | 2016-03-03 17:23:38 -0800 | [diff] [blame] | 713 | goto on_error; |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 714 | } |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 715 | |
Ravi Kumar Alamanda | 5a95ff6 | 2015-08-31 17:42:44 -0700 | [diff] [blame] | 716 | platform_send_audio_calibration(adev->platform, snd_device); |
| 717 | |
vivek mehta | de4849c | 2016-03-03 17:23:38 -0800 | [diff] [blame] | 718 | if (adev->snd_dev_ref_cnt[snd_device] >= 1) { |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 719 | ALOGV("%s: snd_device(%d: %s) is already active", |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 720 | __func__, snd_device, platform_get_snd_device_name(snd_device)); |
vivek mehta | de4849c | 2016-03-03 17:23:38 -0800 | [diff] [blame] | 721 | goto on_success; |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 722 | } |
| 723 | |
Ravi Kumar Alamanda | a417cc5 | 2015-05-01 16:41:56 -0700 | [diff] [blame] | 724 | /* due to the possibility of calibration overwrite between listen |
| 725 | and audio, notify sound trigger hal before audio calibration is sent */ |
| 726 | audio_extn_sound_trigger_update_device_status(snd_device, |
| 727 | ST_EVENT_SND_DEVICE_BUSY); |
| 728 | |
Ravi Kumar Alamanda | 6386300 | 2015-04-22 11:15:25 -0700 | [diff] [blame] | 729 | if (audio_extn_spkr_prot_is_enabled()) |
| 730 | audio_extn_spkr_prot_calib_cancel(adev); |
| 731 | |
zhaoyang yin | 4211fad | 2015-06-04 21:13:25 +0800 | [diff] [blame] | 732 | audio_extn_dsm_feedback_enable(adev, snd_device, true); |
| 733 | |
Ravi Kumar Alamanda | 6386300 | 2015-04-22 11:15:25 -0700 | [diff] [blame] | 734 | if ((snd_device == SND_DEVICE_OUT_SPEAKER || |
kevinshhsu | 7440d8c | 2018-01-14 22:25:38 +0800 | [diff] [blame] | 735 | snd_device == SND_DEVICE_OUT_SPEAKER_SAFE || |
juyuchen | 1d05ba3 | 2018-03-31 16:03:51 +0800 | [diff] [blame] | 736 | snd_device == SND_DEVICE_OUT_SPEAKER_REVERSE || |
Ravi Kumar Alamanda | 6386300 | 2015-04-22 11:15:25 -0700 | [diff] [blame] | 737 | snd_device == SND_DEVICE_OUT_VOICE_SPEAKER) && |
| 738 | audio_extn_spkr_prot_is_enabled()) { |
Aniket Kumar Lata | 9d6679a | 2018-04-11 18:13:23 -0700 | [diff] [blame] | 739 | if (platform_get_snd_device_acdb_id(snd_device) < 0) { |
vivek mehta | de4849c | 2016-03-03 17:23:38 -0800 | [diff] [blame] | 740 | goto on_error; |
Ravi Kumar Alamanda | 6386300 | 2015-04-22 11:15:25 -0700 | [diff] [blame] | 741 | } |
| 742 | if (audio_extn_spkr_prot_start_processing(snd_device)) { |
| 743 | ALOGE("%s: spkr_start_processing failed", __func__); |
vivek mehta | de4849c | 2016-03-03 17:23:38 -0800 | [diff] [blame] | 744 | goto on_error; |
Ravi Kumar Alamanda | 6386300 | 2015-04-22 11:15:25 -0700 | [diff] [blame] | 745 | } |
Haynes Mathew George | 2d809e0 | 2016-09-22 17:38:16 -0700 | [diff] [blame] | 746 | } else if (platform_can_split_snd_device(snd_device, |
| 747 | &num_devices, |
| 748 | new_snd_devices) == 0) { |
Ravi Kumar Alamanda | b7ea4f5 | 2015-06-08 16:44:05 -0700 | [diff] [blame] | 749 | for (i = 0; i < num_devices; i++) { |
| 750 | enable_snd_device(adev, new_snd_devices[i]); |
| 751 | } |
vivek mehta | b650641 | 2015-08-07 16:55:17 -0700 | [diff] [blame] | 752 | platform_set_speaker_gain_in_combo(adev, snd_device, true); |
Ravi Kumar Alamanda | 6386300 | 2015-04-22 11:15:25 -0700 | [diff] [blame] | 753 | } else { |
vivek mehta | de4849c | 2016-03-03 17:23:38 -0800 | [diff] [blame] | 754 | char device_name[DEVICE_NAME_MAX_SIZE] = {0}; |
| 755 | if (platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0 ) { |
| 756 | ALOGE(" %s: Invalid sound device returned", __func__); |
| 757 | goto on_error; |
| 758 | } |
Ed Tam | 70b5c14 | 2016-03-21 19:14:29 -0700 | [diff] [blame] | 759 | |
Eric Laurent | 2e140aa | 2016-06-30 17:14:46 -0700 | [diff] [blame] | 760 | ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, device_name); |
Aniket Kumar Lata | a158e86 | 2018-05-11 17:28:40 -0700 | [diff] [blame] | 761 | |
| 762 | if (is_a2dp_device(snd_device) && |
| 763 | (audio_extn_a2dp_start_playback() < 0)) { |
| 764 | ALOGE("%s: failed to configure A2DP control path", __func__); |
| 765 | goto on_error; |
| 766 | } |
| 767 | |
vivek mehta | de4849c | 2016-03-03 17:23:38 -0800 | [diff] [blame] | 768 | audio_route_apply_and_update_path(adev->audio_route, device_name); |
| 769 | } |
| 770 | on_success: |
| 771 | adev->snd_dev_ref_cnt[snd_device]++; |
| 772 | ret_val = 0; |
| 773 | on_error: |
| 774 | return ret_val; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 775 | } |
| 776 | |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 777 | int disable_snd_device(struct audio_device *adev, |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 778 | snd_device_t snd_device) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 779 | { |
Ravi Kumar Alamanda | b7ea4f5 | 2015-06-08 16:44:05 -0700 | [diff] [blame] | 780 | int i, num_devices = 0; |
| 781 | snd_device_t new_snd_devices[2]; |
| 782 | |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 783 | if (snd_device < SND_DEVICE_MIN || |
| 784 | snd_device >= SND_DEVICE_MAX) { |
Ravi Kumar Alamanda | 3b1816c | 2013-02-27 23:01:21 -0800 | [diff] [blame] | 785 | ALOGE("%s: Invalid sound device %d", __func__, snd_device); |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 786 | return -EINVAL; |
| 787 | } |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 788 | if (adev->snd_dev_ref_cnt[snd_device] <= 0) { |
| 789 | ALOGE("%s: device ref cnt is already 0", __func__); |
| 790 | return -EINVAL; |
| 791 | } |
Alain Vongsouvanh | 13f26e8 | 2016-11-18 14:39:11 -0800 | [diff] [blame] | 792 | audio_extn_tfa_98xx_disable_speaker(snd_device); |
| 793 | |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 794 | adev->snd_dev_ref_cnt[snd_device]--; |
| 795 | if (adev->snd_dev_ref_cnt[snd_device] == 0) { |
zhaoyang yin | 4211fad | 2015-06-04 21:13:25 +0800 | [diff] [blame] | 796 | audio_extn_dsm_feedback_enable(adev, snd_device, false); |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 797 | |
| 798 | if (is_a2dp_device(snd_device)) |
| 799 | audio_extn_a2dp_stop_playback(); |
| 800 | |
Ravi Kumar Alamanda | 6386300 | 2015-04-22 11:15:25 -0700 | [diff] [blame] | 801 | if ((snd_device == SND_DEVICE_OUT_SPEAKER || |
kevinshhsu | 7440d8c | 2018-01-14 22:25:38 +0800 | [diff] [blame] | 802 | snd_device == SND_DEVICE_OUT_SPEAKER_SAFE || |
vivek mehta | e59cfb2 | 2017-06-16 15:57:11 -0700 | [diff] [blame] | 803 | snd_device == SND_DEVICE_OUT_SPEAKER_REVERSE || |
Ravi Kumar Alamanda | 6386300 | 2015-04-22 11:15:25 -0700 | [diff] [blame] | 804 | snd_device == SND_DEVICE_OUT_VOICE_SPEAKER) && |
| 805 | audio_extn_spkr_prot_is_enabled()) { |
| 806 | audio_extn_spkr_prot_stop_processing(snd_device); |
vivek mehta | e59cfb2 | 2017-06-16 15:57:11 -0700 | [diff] [blame] | 807 | |
Eric Laurent | 3c1e4b7 | 2017-10-06 11:25:33 -0700 | [diff] [blame] | 808 | // FIXME b/65363602: bullhead is the only Nexus with audio_extn_spkr_prot_is_enabled() |
| 809 | // and does not use speaker swap. As this code causes a problem with device enable ref |
| 810 | // counting we remove it for now. |
vivek mehta | e59cfb2 | 2017-06-16 15:57:11 -0700 | [diff] [blame] | 811 | // when speaker device is disabled, reset swap. |
| 812 | // will be renabled on usecase start |
Eric Laurent | 3c1e4b7 | 2017-10-06 11:25:33 -0700 | [diff] [blame] | 813 | // platform_set_swap_channels(adev, false); |
vivek mehta | e59cfb2 | 2017-06-16 15:57:11 -0700 | [diff] [blame] | 814 | |
Haynes Mathew George | 2d809e0 | 2016-09-22 17:38:16 -0700 | [diff] [blame] | 815 | } else if (platform_can_split_snd_device(snd_device, |
| 816 | &num_devices, |
| 817 | new_snd_devices) == 0) { |
Ravi Kumar Alamanda | b7ea4f5 | 2015-06-08 16:44:05 -0700 | [diff] [blame] | 818 | for (i = 0; i < num_devices; i++) { |
| 819 | disable_snd_device(adev, new_snd_devices[i]); |
| 820 | } |
vivek mehta | b650641 | 2015-08-07 16:55:17 -0700 | [diff] [blame] | 821 | platform_set_speaker_gain_in_combo(adev, snd_device, false); |
Ravi Kumar Alamanda | 6386300 | 2015-04-22 11:15:25 -0700 | [diff] [blame] | 822 | } else { |
vivek mehta | de4849c | 2016-03-03 17:23:38 -0800 | [diff] [blame] | 823 | char device_name[DEVICE_NAME_MAX_SIZE] = {0}; |
| 824 | if (platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0 ) { |
| 825 | ALOGE(" %s: Invalid sound device returned", __func__); |
| 826 | return -EINVAL; |
| 827 | } |
| 828 | |
Eric Laurent | 2e140aa | 2016-06-30 17:14:46 -0700 | [diff] [blame] | 829 | ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, device_name); |
vivek mehta | de4849c | 2016-03-03 17:23:38 -0800 | [diff] [blame] | 830 | audio_route_reset_and_update_path(adev->audio_route, device_name); |
Ravi Kumar Alamanda | 6386300 | 2015-04-22 11:15:25 -0700 | [diff] [blame] | 831 | } |
Ravi Kumar Alamanda | a417cc5 | 2015-05-01 16:41:56 -0700 | [diff] [blame] | 832 | audio_extn_sound_trigger_update_device_status(snd_device, |
| 833 | ST_EVENT_SND_DEVICE_FREE); |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 834 | } |
vivek mehta | b650641 | 2015-08-07 16:55:17 -0700 | [diff] [blame] | 835 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 836 | return 0; |
| 837 | } |
| 838 | |
Vignesh Kulothungan | 6469882 | 2018-01-23 11:25:18 -0800 | [diff] [blame] | 839 | #ifdef DYNAMIC_ECNS_ENABLED |
| 840 | static int send_effect_enable_disable_mixer_ctl(struct audio_device *adev, |
| 841 | struct stream_in *in, |
| 842 | struct audio_effect_config effect_config, |
| 843 | unsigned int param_value) |
| 844 | { |
| 845 | char mixer_ctl_name[] = "Audio Effect"; |
| 846 | long set_values[6]; |
| 847 | |
| 848 | struct mixer_ctl *ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name); |
| 849 | if (!ctl) { |
| 850 | ALOGE("%s: Could not get mixer ctl - %s", |
| 851 | __func__, mixer_ctl_name); |
| 852 | return -EINVAL; |
| 853 | } |
| 854 | |
| 855 | set_values[0] = 1; //0:Rx 1:Tx |
| 856 | set_values[1] = in->app_type_cfg.app_type; |
| 857 | set_values[2] = (long)effect_config.module_id; |
| 858 | set_values[3] = (long)effect_config.instance_id; |
| 859 | set_values[4] = (long)effect_config.param_id; |
| 860 | set_values[5] = param_value; |
| 861 | |
| 862 | mixer_ctl_set_array(ctl, set_values, ARRAY_SIZE(set_values)); |
| 863 | |
| 864 | return 0; |
| 865 | |
| 866 | } |
| 867 | |
| 868 | static int update_effect_param_ecns(struct audio_usecase *usecase, |
| 869 | unsigned int module_id, int effect_type, |
| 870 | unsigned int *param_value) |
| 871 | { |
| 872 | int ret = 0; |
| 873 | struct audio_effect_config other_effect_config; |
| 874 | struct stream_in *in = NULL; |
| 875 | |
| 876 | if (!usecase) |
| 877 | return -EINVAL; |
| 878 | |
| 879 | in = usecase->stream.in; |
| 880 | |
| 881 | /* Get the effect config data of the other effect */ |
| 882 | ret = platform_get_effect_config_data(usecase->in_snd_device, |
| 883 | &other_effect_config, |
| 884 | effect_type == EFFECT_AEC ? EFFECT_NS : EFFECT_AEC); |
| 885 | if (ret < 0) { |
| 886 | ALOGE("%s Failed to get effect params %d", __func__, ret); |
| 887 | return ret; |
| 888 | } |
| 889 | |
| 890 | if (module_id == other_effect_config.module_id) { |
| 891 | //Same module id for AEC/NS. Values need to be combined |
| 892 | if (((effect_type == EFFECT_AEC) && (in->enable_ns)) || |
| 893 | ((effect_type == EFFECT_NS) && (in->enable_aec))) |
| 894 | *param_value |= other_effect_config.param_value; |
| 895 | } |
| 896 | |
| 897 | return ret; |
| 898 | } |
| 899 | |
| 900 | static int enable_disable_effect(struct audio_device *adev, struct stream_in *in, |
| 901 | int effect_type, bool enable) |
| 902 | { |
| 903 | struct audio_effect_config effect_config; |
| 904 | struct audio_usecase *usecase = NULL; |
| 905 | int ret = 0; |
| 906 | unsigned int param_value = 0; |
| 907 | |
| 908 | if (!in) { |
| 909 | ALOGE("%s: Invalid input stream", __func__); |
| 910 | return -EINVAL; |
| 911 | } |
| 912 | |
| 913 | ALOGD("%s: effect_type:%d enable:%d", __func__, effect_type, enable); |
| 914 | |
| 915 | usecase = get_usecase_from_list(adev, in->usecase); |
| 916 | |
| 917 | ret = platform_get_effect_config_data(usecase->in_snd_device, |
| 918 | &effect_config, effect_type); |
| 919 | if (ret < 0) { |
| 920 | ALOGE("%s Failed to get module id %d", __func__, ret); |
| 921 | return ret; |
| 922 | } |
| 923 | ALOGV("%s: module %d app_type %d usecase->id:%d usecase->in_snd_device:%d", |
| 924 | __func__, effect_config.module_id, in->app_type_cfg.app_type, |
| 925 | usecase->id, usecase->in_snd_device); |
| 926 | |
| 927 | if (enable) |
| 928 | param_value = effect_config.param_value; |
| 929 | |
| 930 | /*Special handling for AEC & NS effects Param values need to be |
| 931 | updated if module ids are same*/ |
| 932 | |
| 933 | if ((effect_type == EFFECT_AEC) || (effect_type == EFFECT_NS)) { |
| 934 | ret = update_effect_param_ecns(usecase, effect_config.module_id, |
| 935 | effect_type, ¶m_value); |
| 936 | if (ret < 0) |
| 937 | return ret; |
| 938 | } |
| 939 | |
| 940 | ret = send_effect_enable_disable_mixer_ctl(adev, in, |
| 941 | effect_config, param_value); |
| 942 | |
| 943 | return ret; |
| 944 | } |
| 945 | |
| 946 | static int check_and_enable_effect(struct audio_device *adev) |
| 947 | { |
| 948 | int ret = 0; |
| 949 | |
| 950 | struct listnode *node; |
| 951 | struct stream_in *in = NULL; |
| 952 | |
| 953 | list_for_each(node, &adev->usecase_list) |
| 954 | { |
| 955 | struct audio_usecase *usecase = node_to_item(node, struct audio_usecase, list); |
| 956 | if (usecase->type == PCM_CAPTURE && usecase->stream.in != NULL) { |
| 957 | in = usecase->stream.in; |
| 958 | |
| 959 | if (in->standby) |
| 960 | continue; |
| 961 | |
| 962 | if (in->enable_aec) { |
| 963 | ret = enable_disable_effect(adev, in, EFFECT_AEC, true); |
| 964 | } |
| 965 | |
| 966 | if (in->enable_ns && |
| 967 | in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) { |
| 968 | ret = enable_disable_effect(adev, in, EFFECT_NS, true); |
| 969 | } |
| 970 | } |
| 971 | } |
| 972 | |
| 973 | return ret; |
| 974 | } |
| 975 | #else |
| 976 | #define enable_disable_effect(w, x, y, z) -ENOSYS |
| 977 | #define check_and_enable_effect(x) -ENOSYS |
| 978 | #endif |
| 979 | |
Haynes Mathew George | 2d809e0 | 2016-09-22 17:38:16 -0700 | [diff] [blame] | 980 | /* |
| 981 | legend: |
| 982 | uc - existing usecase |
| 983 | new_uc - new usecase |
| 984 | d1, d11, d2 - SND_DEVICE enums |
| 985 | a1, a2 - corresponding ANDROID device enums |
| 986 | B, B1, B2 - backend strings |
| 987 | |
| 988 | case 1 |
| 989 | uc->dev d1 (a1) B1 |
| 990 | new_uc->dev d1 (a1), d2 (a2) B1, B2 |
| 991 | |
| 992 | resolution: disable and enable uc->dev on d1 |
| 993 | |
| 994 | case 2 |
| 995 | uc->dev d1 (a1) B1 |
| 996 | new_uc->dev d11 (a1) B1 |
| 997 | |
| 998 | resolution: need to switch uc since d1 and d11 are related |
| 999 | (e.g. speaker and voice-speaker) |
| 1000 | use ANDROID_DEVICE_OUT enums to match devices since SND_DEVICE enums may vary |
| 1001 | |
| 1002 | case 3 |
| 1003 | uc->dev d1 (a1) B1 |
| 1004 | new_uc->dev d2 (a2) B2 |
| 1005 | |
| 1006 | resolution: no need to switch uc |
| 1007 | |
| 1008 | case 4 |
| 1009 | uc->dev d1 (a1) B |
| 1010 | new_uc->dev d2 (a2) B |
| 1011 | |
| 1012 | resolution: disable enable uc-dev on d2 since backends match |
| 1013 | we cannot enable two streams on two different devices if they |
| 1014 | share the same backend. e.g. if offload is on speaker device using |
| 1015 | QUAD_MI2S backend and a low-latency stream is started on voice-handset |
| 1016 | using the same backend, offload must also be switched to voice-handset. |
| 1017 | |
| 1018 | case 5 |
| 1019 | uc->dev d1 (a1) B |
| 1020 | new_uc->dev d1 (a1), d2 (a2) B |
| 1021 | |
| 1022 | resolution: disable enable uc-dev on d2 since backends match |
| 1023 | we cannot enable two streams on two different devices if they |
| 1024 | share the same backend. |
| 1025 | |
| 1026 | case 6 |
| 1027 | uc->dev d1 a1 B1 |
| 1028 | new_uc->dev d2 a1 B2 |
| 1029 | |
| 1030 | resolution: no need to switch |
| 1031 | |
| 1032 | case 7 |
| 1033 | |
| 1034 | uc->dev d1 (a1), d2 (a2) B1, B2 |
| 1035 | new_uc->dev d1 B1 |
| 1036 | |
| 1037 | resolution: no need to switch |
| 1038 | |
| 1039 | */ |
| 1040 | static snd_device_t derive_playback_snd_device(struct audio_usecase *uc, |
| 1041 | struct audio_usecase *new_uc, |
| 1042 | snd_device_t new_snd_device) |
| 1043 | { |
| 1044 | audio_devices_t a1 = uc->stream.out->devices; |
| 1045 | audio_devices_t a2 = new_uc->stream.out->devices; |
| 1046 | |
| 1047 | snd_device_t d1 = uc->out_snd_device; |
| 1048 | snd_device_t d2 = new_snd_device; |
| 1049 | |
| 1050 | // Treat as a special case when a1 and a2 are not disjoint |
| 1051 | if ((a1 != a2) && (a1 & a2)) { |
| 1052 | snd_device_t d3[2]; |
| 1053 | int num_devices = 0; |
| 1054 | int ret = platform_can_split_snd_device(popcount(a1) > 1 ? d1 : d2, |
| 1055 | &num_devices, |
| 1056 | d3); |
| 1057 | if (ret < 0) { |
| 1058 | if (ret != -ENOSYS) { |
| 1059 | ALOGW("%s failed to split snd_device %d", |
| 1060 | __func__, |
| 1061 | popcount(a1) > 1 ? d1 : d2); |
| 1062 | } |
| 1063 | goto end; |
| 1064 | } |
| 1065 | |
| 1066 | // NB: case 7 is hypothetical and isn't a practical usecase yet. |
| 1067 | // But if it does happen, we need to give priority to d2 if |
| 1068 | // the combo devices active on the existing usecase share a backend. |
| 1069 | // This is because we cannot have a usecase active on a combo device |
| 1070 | // and a new usecase requests one device in this combo pair. |
| 1071 | if (platform_check_backends_match(d3[0], d3[1])) { |
| 1072 | return d2; // case 5 |
| 1073 | } else { |
| 1074 | return d1; // case 1 |
| 1075 | } |
| 1076 | } else { |
| 1077 | if (platform_check_backends_match(d1, d2)) { |
| 1078 | return d2; // case 2, 4 |
| 1079 | } else { |
| 1080 | return d1; // case 6, 3 |
| 1081 | } |
| 1082 | } |
| 1083 | |
| 1084 | end: |
| 1085 | return d2; // return whatever was calculated before. |
| 1086 | } |
| 1087 | |
Ravi Kumar Alamanda | b7ea4f5 | 2015-06-08 16:44:05 -0700 | [diff] [blame] | 1088 | static void check_and_route_playback_usecases(struct audio_device *adev, |
| 1089 | struct audio_usecase *uc_info, |
| 1090 | snd_device_t snd_device) |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1091 | { |
| 1092 | struct listnode *node; |
| 1093 | struct audio_usecase *usecase; |
| 1094 | bool switch_device[AUDIO_USECASE_MAX]; |
| 1095 | int i, num_uc_to_switch = 0; |
| 1096 | |
Haynes Mathew George | 137a2ee | 2017-05-23 17:03:35 -0700 | [diff] [blame] | 1097 | bool force_routing = platform_check_and_set_playback_backend_cfg(adev, |
| 1098 | uc_info, |
| 1099 | snd_device); |
David Lin | ee3fe40 | 2017-03-13 10:00:42 -0700 | [diff] [blame] | 1100 | |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 1101 | /* For a2dp device reconfigure all active sessions |
| 1102 | * with new AFE encoder format based on a2dp state |
| 1103 | */ |
| 1104 | if ((SND_DEVICE_OUT_BT_A2DP == snd_device || |
Aniket Kumar Lata | 9723a96 | 2018-05-16 17:41:55 -0700 | [diff] [blame] | 1105 | SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP == snd_device || |
| 1106 | SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP == snd_device) && |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 1107 | audio_extn_a2dp_is_force_device_switch()) { |
| 1108 | force_routing = true; |
| 1109 | } |
| 1110 | |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1111 | /* |
| 1112 | * This function is to make sure that all the usecases that are active on |
| 1113 | * the hardware codec backend are always routed to any one device that is |
| 1114 | * handled by the hardware codec. |
| 1115 | * For example, if low-latency and deep-buffer usecases are currently active |
| 1116 | * on speaker and out_set_parameters(headset) is received on low-latency |
| 1117 | * output, then we have to make sure deep-buffer is also switched to headset, |
| 1118 | * because of the limitation that both the devices cannot be enabled |
| 1119 | * at the same time as they share the same backend. |
| 1120 | */ |
| 1121 | /* Disable all the usecases on the shared backend other than the |
| 1122 | specified usecase */ |
| 1123 | for (i = 0; i < AUDIO_USECASE_MAX; i++) |
| 1124 | switch_device[i] = false; |
| 1125 | |
| 1126 | list_for_each(node, &adev->usecase_list) { |
| 1127 | usecase = node_to_item(node, struct audio_usecase, list); |
Haynes Mathew George | 137a2ee | 2017-05-23 17:03:35 -0700 | [diff] [blame] | 1128 | if (usecase->type == PCM_CAPTURE || usecase == uc_info) |
| 1129 | continue; |
| 1130 | |
| 1131 | if (force_routing || |
| 1132 | (usecase->out_snd_device != snd_device && |
Haynes Mathew George | 39c55dc | 2017-07-11 19:31:23 -0700 | [diff] [blame] | 1133 | (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND || |
| 1134 | usecase->devices & (AUDIO_DEVICE_OUT_USB_DEVICE|AUDIO_DEVICE_OUT_USB_HEADSET)) && |
Haynes Mathew George | 137a2ee | 2017-05-23 17:03:35 -0700 | [diff] [blame] | 1135 | platform_check_backends_match(snd_device, usecase->out_snd_device))) { |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1136 | ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..", |
| 1137 | __func__, use_case_table[usecase->id], |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 1138 | platform_get_snd_device_name(usecase->out_snd_device)); |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 1139 | disable_audio_route(adev, usecase); |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1140 | switch_device[usecase->id] = true; |
| 1141 | num_uc_to_switch++; |
| 1142 | } |
| 1143 | } |
| 1144 | |
| 1145 | if (num_uc_to_switch) { |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1146 | list_for_each(node, &adev->usecase_list) { |
| 1147 | usecase = node_to_item(node, struct audio_usecase, list); |
| 1148 | if (switch_device[usecase->id]) { |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 1149 | disable_snd_device(adev, usecase->out_snd_device); |
sangwon.jeon | 866d5ff | 2013-10-17 21:42:50 +0900 | [diff] [blame] | 1150 | } |
| 1151 | } |
| 1152 | |
Haynes Mathew George | 2d809e0 | 2016-09-22 17:38:16 -0700 | [diff] [blame] | 1153 | snd_device_t d_device; |
sangwon.jeon | 866d5ff | 2013-10-17 21:42:50 +0900 | [diff] [blame] | 1154 | list_for_each(node, &adev->usecase_list) { |
| 1155 | usecase = node_to_item(node, struct audio_usecase, list); |
| 1156 | if (switch_device[usecase->id]) { |
Haynes Mathew George | 2d809e0 | 2016-09-22 17:38:16 -0700 | [diff] [blame] | 1157 | d_device = derive_playback_snd_device(usecase, uc_info, |
| 1158 | snd_device); |
| 1159 | enable_snd_device(adev, d_device); |
| 1160 | /* Update the out_snd_device before enabling the audio route */ |
| 1161 | usecase->out_snd_device = d_device; |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1162 | } |
| 1163 | } |
| 1164 | |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1165 | /* Re-route all the usecases on the shared backend other than the |
| 1166 | specified usecase to new snd devices */ |
| 1167 | list_for_each(node, &adev->usecase_list) { |
| 1168 | usecase = node_to_item(node, struct audio_usecase, list); |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1169 | if (switch_device[usecase->id] ) { |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 1170 | enable_audio_route(adev, usecase); |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1171 | } |
| 1172 | } |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1173 | } |
| 1174 | } |
| 1175 | |
Ravi Kumar Alamanda | c4ba743 | 2013-06-05 14:11:39 -0700 | [diff] [blame] | 1176 | static void check_and_route_capture_usecases(struct audio_device *adev, |
| 1177 | struct audio_usecase *uc_info, |
| 1178 | snd_device_t snd_device) |
| 1179 | { |
| 1180 | struct listnode *node; |
| 1181 | struct audio_usecase *usecase; |
| 1182 | bool switch_device[AUDIO_USECASE_MAX]; |
| 1183 | int i, num_uc_to_switch = 0; |
| 1184 | |
vivek mehta | 4ed66e6 | 2016-04-15 23:33:34 -0700 | [diff] [blame] | 1185 | platform_check_and_set_capture_backend_cfg(adev, uc_info, snd_device); |
| 1186 | |
Ravi Kumar Alamanda | c4ba743 | 2013-06-05 14:11:39 -0700 | [diff] [blame] | 1187 | /* |
| 1188 | * This function is to make sure that all the active capture usecases |
| 1189 | * are always routed to the same input sound device. |
| 1190 | * For example, if audio-record and voice-call usecases are currently |
| 1191 | * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece) |
| 1192 | * is received for voice call then we have to make sure that audio-record |
| 1193 | * usecase is also switched to earpiece i.e. voice-dmic-ef, |
| 1194 | * because of the limitation that two devices cannot be enabled |
| 1195 | * at the same time if they share the same backend. |
| 1196 | */ |
| 1197 | for (i = 0; i < AUDIO_USECASE_MAX; i++) |
| 1198 | switch_device[i] = false; |
| 1199 | |
| 1200 | list_for_each(node, &adev->usecase_list) { |
| 1201 | usecase = node_to_item(node, struct audio_usecase, list); |
| 1202 | if (usecase->type != PCM_PLAYBACK && |
| 1203 | usecase != uc_info && |
Anish Kumar | ff86471 | 2015-06-03 13:35:11 -0700 | [diff] [blame] | 1204 | usecase->in_snd_device != snd_device && |
Carter Hsu | 0a1160c | 2018-10-04 09:24:36 +0800 | [diff] [blame] | 1205 | ((uc_info->type == VOICE_CALL && |
| 1206 | usecase->devices == AUDIO_DEVICE_IN_VOICE_CALL) || |
| 1207 | platform_check_backends_match(snd_device,\ |
| 1208 | usecase->in_snd_device)) && |
Anish Kumar | ff86471 | 2015-06-03 13:35:11 -0700 | [diff] [blame] | 1209 | (usecase->id != USECASE_AUDIO_SPKR_CALIB_TX)) { |
Ravi Kumar Alamanda | c4ba743 | 2013-06-05 14:11:39 -0700 | [diff] [blame] | 1210 | ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..", |
| 1211 | __func__, use_case_table[usecase->id], |
Devin Kim | 1e5f353 | 2013-08-09 07:48:29 -0700 | [diff] [blame] | 1212 | platform_get_snd_device_name(usecase->in_snd_device)); |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 1213 | disable_audio_route(adev, usecase); |
Ravi Kumar Alamanda | c4ba743 | 2013-06-05 14:11:39 -0700 | [diff] [blame] | 1214 | switch_device[usecase->id] = true; |
| 1215 | num_uc_to_switch++; |
| 1216 | } |
| 1217 | } |
| 1218 | |
| 1219 | if (num_uc_to_switch) { |
Ravi Kumar Alamanda | c4ba743 | 2013-06-05 14:11:39 -0700 | [diff] [blame] | 1220 | list_for_each(node, &adev->usecase_list) { |
| 1221 | usecase = node_to_item(node, struct audio_usecase, list); |
| 1222 | if (switch_device[usecase->id]) { |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 1223 | disable_snd_device(adev, usecase->in_snd_device); |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 1224 | } |
| 1225 | } |
| 1226 | |
| 1227 | list_for_each(node, &adev->usecase_list) { |
| 1228 | usecase = node_to_item(node, struct audio_usecase, list); |
| 1229 | if (switch_device[usecase->id]) { |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 1230 | enable_snd_device(adev, snd_device); |
Ravi Kumar Alamanda | c4ba743 | 2013-06-05 14:11:39 -0700 | [diff] [blame] | 1231 | } |
| 1232 | } |
| 1233 | |
Ravi Kumar Alamanda | c4ba743 | 2013-06-05 14:11:39 -0700 | [diff] [blame] | 1234 | /* Re-route all the usecases on the shared backend other than the |
| 1235 | specified usecase to new snd devices */ |
| 1236 | list_for_each(node, &adev->usecase_list) { |
| 1237 | usecase = node_to_item(node, struct audio_usecase, list); |
| 1238 | /* Update the in_snd_device only before enabling the audio route */ |
| 1239 | if (switch_device[usecase->id] ) { |
| 1240 | usecase->in_snd_device = snd_device; |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 1241 | enable_audio_route(adev, usecase); |
Ravi Kumar Alamanda | c4ba743 | 2013-06-05 14:11:39 -0700 | [diff] [blame] | 1242 | } |
| 1243 | } |
Ravi Kumar Alamanda | c4ba743 | 2013-06-05 14:11:39 -0700 | [diff] [blame] | 1244 | } |
| 1245 | } |
| 1246 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1247 | /* must be called with hw device mutex locked */ |
Ravi Kumar Alamanda | b199506 | 2013-03-21 23:18:20 -0700 | [diff] [blame] | 1248 | static int read_hdmi_channel_masks(struct stream_out *out) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1249 | { |
Ravi Kumar Alamanda | b199506 | 2013-03-21 23:18:20 -0700 | [diff] [blame] | 1250 | int ret = 0; |
Haynes Mathew George | 47cd4cb | 2013-07-19 11:58:50 -0700 | [diff] [blame] | 1251 | int channels = platform_edid_get_max_channels(out->dev->platform); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1252 | |
| 1253 | switch (channels) { |
| 1254 | /* |
| 1255 | * Do not handle stereo output in Multi-channel cases |
| 1256 | * Stereo case is handled in normal playback path |
| 1257 | */ |
| 1258 | case 6: |
| 1259 | ALOGV("%s: HDMI supports 5.1", __func__); |
| 1260 | out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1; |
| 1261 | break; |
| 1262 | case 8: |
| 1263 | ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__); |
| 1264 | out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1; |
| 1265 | out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1; |
| 1266 | break; |
| 1267 | default: |
Ravi Kumar Alamanda | b199506 | 2013-03-21 23:18:20 -0700 | [diff] [blame] | 1268 | ALOGE("HDMI does not support multi channel playback"); |
| 1269 | ret = -ENOSYS; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1270 | break; |
| 1271 | } |
Ravi Kumar Alamanda | b199506 | 2013-03-21 23:18:20 -0700 | [diff] [blame] | 1272 | return ret; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1273 | } |
| 1274 | |
Andy Hung | 1885941 | 2017-08-09 11:47:21 -0700 | [diff] [blame] | 1275 | static ssize_t read_usb_sup_sample_rates(bool is_playback, |
| 1276 | uint32_t *supported_sample_rates, |
| 1277 | uint32_t max_rates) |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 1278 | { |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 1279 | ssize_t count = audio_extn_usb_sup_sample_rates(is_playback, |
| 1280 | supported_sample_rates, |
| 1281 | max_rates); |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 1282 | #if !LOG_NDEBUG |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 1283 | for (ssize_t i=0; i<count; i++) { |
| 1284 | ALOGV("%s %s %d", __func__, is_playback ? "P" : "C", |
| 1285 | supported_sample_rates[i]); |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 1286 | } |
| 1287 | #endif |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 1288 | return count; |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 1289 | } |
| 1290 | |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 1291 | static int read_usb_sup_channel_masks(bool is_playback, |
| 1292 | audio_channel_mask_t *supported_channel_masks, |
Eric Laurent | 74b5576 | 2017-07-09 17:04:53 -0700 | [diff] [blame] | 1293 | uint32_t max_masks) |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 1294 | { |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 1295 | int channels = audio_extn_usb_get_max_channels(is_playback); |
Eric Laurent | 74b5576 | 2017-07-09 17:04:53 -0700 | [diff] [blame] | 1296 | int channel_count; |
| 1297 | uint32_t num_masks = 0; |
| 1298 | if (channels > MAX_HIFI_CHANNEL_COUNT) { |
| 1299 | channels = MAX_HIFI_CHANNEL_COUNT; |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 1300 | } |
Eric Laurent | 74b5576 | 2017-07-09 17:04:53 -0700 | [diff] [blame] | 1301 | if (is_playback) { |
Andy Hung | 6c1afb1 | 2017-12-22 11:34:41 -0800 | [diff] [blame] | 1302 | // start from 2 channels as framework currently doesn't support mono. |
| 1303 | // TODO: consider only supporting channel index masks beyond stereo here. |
| 1304 | for (channel_count = FCC_2; |
| 1305 | channel_count <= channels && num_masks < max_masks; |
| 1306 | ++channel_count) { |
Eric Laurent | 74b5576 | 2017-07-09 17:04:53 -0700 | [diff] [blame] | 1307 | supported_channel_masks[num_masks++] = audio_channel_out_mask_from_count(channel_count); |
| 1308 | } |
Andy Hung | 6c1afb1 | 2017-12-22 11:34:41 -0800 | [diff] [blame] | 1309 | for (channel_count = FCC_2; |
| 1310 | channel_count <= channels && num_masks < max_masks; |
| 1311 | ++channel_count) { |
Eric Laurent | 74b5576 | 2017-07-09 17:04:53 -0700 | [diff] [blame] | 1312 | supported_channel_masks[num_masks++] = |
| 1313 | audio_channel_mask_for_index_assignment_from_count(channel_count); |
| 1314 | } |
| 1315 | } else { |
| 1316 | // For capture we report all supported channel masks from 1 channel up. |
| 1317 | channel_count = MIN_CHANNEL_COUNT; |
| 1318 | // audio_channel_in_mask_from_count() does the right conversion to either positional or |
| 1319 | // indexed mask |
| 1320 | for ( ; channel_count <= channels && num_masks < max_masks; channel_count++) { |
Andy Hung | 88ce1d9 | 2018-10-29 18:31:12 -0700 | [diff] [blame] | 1321 | const audio_channel_mask_t mask = |
Eric Laurent | 74b5576 | 2017-07-09 17:04:53 -0700 | [diff] [blame] | 1322 | audio_channel_in_mask_from_count(channel_count); |
Andy Hung | 88ce1d9 | 2018-10-29 18:31:12 -0700 | [diff] [blame] | 1323 | supported_channel_masks[num_masks++] = mask; |
| 1324 | const audio_channel_mask_t index_mask = |
| 1325 | audio_channel_mask_for_index_assignment_from_count(channel_count); |
| 1326 | if (mask != index_mask && num_masks < max_masks) { // ensure index mask added. |
| 1327 | supported_channel_masks[num_masks++] = index_mask; |
| 1328 | } |
Eric Laurent | 74b5576 | 2017-07-09 17:04:53 -0700 | [diff] [blame] | 1329 | } |
| 1330 | } |
Andy Hung | 6c1afb1 | 2017-12-22 11:34:41 -0800 | [diff] [blame] | 1331 | #ifdef NDEBUG |
| 1332 | for (size_t i = 0; i < num_masks; ++i) { |
| 1333 | ALOGV("%s: %s supported ch %d supported_channel_masks[%zu] %08x num_masks %d", __func__, |
| 1334 | is_playback ? "P" : "C", channels, i, supported_channel_masks[i], num_masks); |
| 1335 | } |
| 1336 | #endif |
Eric Laurent | 74b5576 | 2017-07-09 17:04:53 -0700 | [diff] [blame] | 1337 | return num_masks; |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 1338 | } |
| 1339 | |
Haynes Mathew George | e95340e | 2017-05-24 15:42:06 -0700 | [diff] [blame] | 1340 | static int read_usb_sup_formats(bool is_playback __unused, |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 1341 | audio_format_t *supported_formats, |
| 1342 | uint32_t max_formats __unused) |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 1343 | { |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 1344 | int bitwidth = audio_extn_usb_get_max_bit_width(is_playback); |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 1345 | switch (bitwidth) { |
| 1346 | case 24: |
| 1347 | // XXX : usb.c returns 24 for s24 and s24_le? |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 1348 | supported_formats[0] = AUDIO_FORMAT_PCM_24_BIT_PACKED; |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 1349 | break; |
| 1350 | case 32: |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 1351 | supported_formats[0] = AUDIO_FORMAT_PCM_32_BIT; |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 1352 | break; |
| 1353 | case 16: |
| 1354 | default : |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 1355 | supported_formats[0] = AUDIO_FORMAT_PCM_16_BIT; |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 1356 | break; |
| 1357 | } |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 1358 | ALOGV("%s: %s supported format %d", __func__, |
| 1359 | is_playback ? "P" : "C", bitwidth); |
| 1360 | return 1; |
| 1361 | } |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 1362 | |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 1363 | static int read_usb_sup_params_and_compare(bool is_playback, |
| 1364 | audio_format_t *format, |
| 1365 | audio_format_t *supported_formats, |
| 1366 | uint32_t max_formats, |
| 1367 | audio_channel_mask_t *mask, |
| 1368 | audio_channel_mask_t *supported_channel_masks, |
| 1369 | uint32_t max_masks, |
| 1370 | uint32_t *rate, |
| 1371 | uint32_t *supported_sample_rates, |
| 1372 | uint32_t max_rates) { |
| 1373 | int ret = 0; |
| 1374 | int num_formats; |
| 1375 | int num_masks; |
| 1376 | int num_rates; |
| 1377 | int i; |
| 1378 | |
| 1379 | num_formats = read_usb_sup_formats(is_playback, supported_formats, |
| 1380 | max_formats); |
| 1381 | num_masks = read_usb_sup_channel_masks(is_playback, supported_channel_masks, |
| 1382 | max_masks); |
Eric Laurent | 74b5576 | 2017-07-09 17:04:53 -0700 | [diff] [blame] | 1383 | |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 1384 | num_rates = read_usb_sup_sample_rates(is_playback, |
| 1385 | supported_sample_rates, max_rates); |
| 1386 | |
| 1387 | #define LUT(table, len, what, dflt) \ |
| 1388 | for (i=0; i<len && (table[i] != what); i++); \ |
| 1389 | if (i==len) { ret |= (what == dflt ? 0 : -1); what=table[0]; } |
| 1390 | |
| 1391 | LUT(supported_formats, num_formats, *format, AUDIO_FORMAT_DEFAULT); |
| 1392 | LUT(supported_channel_masks, num_masks, *mask, AUDIO_CHANNEL_NONE); |
| 1393 | LUT(supported_sample_rates, num_rates, *rate, 0); |
| 1394 | |
| 1395 | #undef LUT |
| 1396 | return ret < 0 ? -EINVAL : 0; // HACK TBD |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 1397 | } |
| 1398 | |
Andy Hung | d9653bd | 2017-08-01 19:31:39 -0700 | [diff] [blame] | 1399 | static bool is_usb_ready(struct audio_device *adev, bool is_playback) |
| 1400 | { |
| 1401 | // Check if usb is ready. |
| 1402 | // The usb device may have been removed quickly after insertion and hence |
| 1403 | // no longer available. This will show up as empty channel masks, or rates. |
| 1404 | |
| 1405 | pthread_mutex_lock(&adev->lock); |
| 1406 | uint32_t supported_sample_rate; |
| 1407 | |
| 1408 | // we consider usb ready if we can fetch at least one sample rate. |
| 1409 | const bool ready = read_usb_sup_sample_rates( |
| 1410 | is_playback, &supported_sample_rate, 1 /* max_rates */) > 0; |
| 1411 | pthread_mutex_unlock(&adev->lock); |
| 1412 | return ready; |
| 1413 | } |
| 1414 | |
Ravi Kumar Alamanda | a237ecc | 2014-07-24 17:27:05 -0700 | [diff] [blame] | 1415 | static audio_usecase_t get_voice_usecase_id_from_list(struct audio_device *adev) |
| 1416 | { |
| 1417 | struct audio_usecase *usecase; |
| 1418 | struct listnode *node; |
| 1419 | |
| 1420 | list_for_each(node, &adev->usecase_list) { |
| 1421 | usecase = node_to_item(node, struct audio_usecase, list); |
| 1422 | if (usecase->type == VOICE_CALL) { |
| 1423 | ALOGV("%s: usecase id %d", __func__, usecase->id); |
| 1424 | return usecase->id; |
| 1425 | } |
| 1426 | } |
| 1427 | return USECASE_INVALID; |
| 1428 | } |
| 1429 | |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 1430 | struct audio_usecase *get_usecase_from_list(struct audio_device *adev, |
| 1431 | audio_usecase_t uc_id) |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1432 | { |
| 1433 | struct audio_usecase *usecase; |
| 1434 | struct listnode *node; |
| 1435 | |
| 1436 | list_for_each(node, &adev->usecase_list) { |
| 1437 | usecase = node_to_item(node, struct audio_usecase, list); |
| 1438 | if (usecase->id == uc_id) |
| 1439 | return usecase; |
| 1440 | } |
| 1441 | return NULL; |
| 1442 | } |
| 1443 | |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 1444 | static bool force_device_switch(struct audio_usecase *usecase) |
| 1445 | { |
| 1446 | if (usecase->stream.out == NULL) { |
| 1447 | ALOGE("%s: stream.out is NULL", __func__); |
| 1448 | return false; |
| 1449 | } |
| 1450 | |
| 1451 | // Force all A2DP output devices to reconfigure for proper AFE encode format |
| 1452 | // Also handle a case where in earlier A2DP start failed as A2DP stream was |
| 1453 | // in suspended state, hence try to trigger a retry when we again get a routing request. |
| 1454 | if ((usecase->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) && |
| 1455 | audio_extn_a2dp_is_force_device_switch()) { |
| 1456 | ALOGD("%s: Force A2DP device switch to update new encoder config", __func__); |
| 1457 | return true; |
| 1458 | } |
| 1459 | |
| 1460 | return false; |
| 1461 | } |
| 1462 | |
Eric Laurent | d1b7a9b | 2018-11-15 12:24:31 -0800 | [diff] [blame] | 1463 | struct stream_in *adev_get_active_input(const struct audio_device *adev) |
| 1464 | { |
| 1465 | struct listnode *node; |
| 1466 | struct stream_in *last_active_in = NULL; |
| 1467 | |
| 1468 | /* Get last added active input. |
| 1469 | * TODO: We may use a priority mechanism to pick highest priority active source */ |
| 1470 | list_for_each(node, &adev->usecase_list) |
| 1471 | { |
| 1472 | struct audio_usecase *usecase = node_to_item(node, struct audio_usecase, list); |
| 1473 | if (usecase->type == PCM_CAPTURE && usecase->stream.in != NULL) { |
| 1474 | last_active_in = usecase->stream.in; |
| 1475 | } |
| 1476 | } |
| 1477 | |
| 1478 | return last_active_in; |
| 1479 | } |
| 1480 | |
| 1481 | struct stream_in *get_voice_communication_input(const struct audio_device *adev) |
| 1482 | { |
| 1483 | struct listnode *node; |
| 1484 | |
| 1485 | /* First check active inputs with voice communication source and then |
| 1486 | * any input if audio mode is in communication */ |
| 1487 | list_for_each(node, &adev->usecase_list) |
| 1488 | { |
| 1489 | struct audio_usecase *usecase = node_to_item(node, struct audio_usecase, list); |
| 1490 | if (usecase->type == PCM_CAPTURE && usecase->stream.in != NULL && |
| 1491 | usecase->stream.in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) { |
| 1492 | return usecase->stream.in; |
| 1493 | } |
| 1494 | } |
| 1495 | if (adev->mode == AUDIO_MODE_IN_COMMUNICATION) { |
| 1496 | return adev_get_active_input(adev); |
| 1497 | } |
| 1498 | return NULL; |
| 1499 | } |
| 1500 | |
HW Lee | f88584d | 2019-03-18 17:27:18 +0800 | [diff] [blame] | 1501 | int select_devices_with_force_switch(struct audio_device *adev, |
| 1502 | audio_usecase_t uc_id, |
| 1503 | bool force_switch) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1504 | { |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 1505 | snd_device_t out_snd_device = SND_DEVICE_NONE; |
| 1506 | snd_device_t in_snd_device = SND_DEVICE_NONE; |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1507 | struct audio_usecase *usecase = NULL; |
| 1508 | struct audio_usecase *vc_usecase = NULL; |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 1509 | struct audio_usecase *hfp_usecase = NULL; |
| 1510 | audio_usecase_t hfp_ucid; |
Ravi Kumar Alamanda | 3b1816c | 2013-02-27 23:01:21 -0800 | [diff] [blame] | 1511 | struct listnode *node; |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1512 | int status = 0; |
Eric Laurent | f4520b0 | 2017-09-20 18:31:58 -0700 | [diff] [blame] | 1513 | struct audio_usecase *voip_usecase = get_usecase_from_list(adev, |
| 1514 | USECASE_AUDIO_PLAYBACK_VOIP); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1515 | |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1516 | usecase = get_usecase_from_list(adev, uc_id); |
| 1517 | if (usecase == NULL) { |
| 1518 | ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id); |
| 1519 | return -EINVAL; |
| 1520 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1521 | |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 1522 | if ((usecase->type == VOICE_CALL) || |
| 1523 | (usecase->type == PCM_HFP_CALL)) { |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 1524 | out_snd_device = platform_get_output_snd_device(adev->platform, |
| 1525 | usecase->stream.out->devices); |
Eric Laurent | d1b7a9b | 2018-11-15 12:24:31 -0800 | [diff] [blame] | 1526 | in_snd_device = platform_get_input_snd_device(adev->platform, |
| 1527 | NULL, |
| 1528 | usecase->stream.out->devices); |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1529 | usecase->devices = usecase->stream.out->devices; |
| 1530 | } else { |
| 1531 | /* |
| 1532 | * If the voice call is active, use the sound devices of voice call usecase |
| 1533 | * so that it would not result any device switch. All the usecases will |
| 1534 | * be switched to new device when select_devices() is called for voice call |
| 1535 | * usecase. This is to avoid switching devices for voice call when |
Ravi Kumar Alamanda | b7ea4f5 | 2015-06-08 16:44:05 -0700 | [diff] [blame] | 1536 | * check_and_route_playback_usecases() is called below. |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1537 | */ |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 1538 | if (voice_is_in_call(adev)) { |
Ravi Kumar Alamanda | a237ecc | 2014-07-24 17:27:05 -0700 | [diff] [blame] | 1539 | vc_usecase = get_usecase_from_list(adev, |
| 1540 | get_voice_usecase_id_from_list(adev)); |
| 1541 | if ((vc_usecase != NULL) && |
| 1542 | ((vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) || |
juyuchen | d194b43 | 2018-11-16 14:15:16 +0800 | [diff] [blame] | 1543 | (vc_usecase->devices == AUDIO_DEVICE_OUT_HEARING_AID) || |
| 1544 | (usecase->devices == AUDIO_DEVICE_IN_VOICE_CALL))) { |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1545 | in_snd_device = vc_usecase->in_snd_device; |
| 1546 | out_snd_device = vc_usecase->out_snd_device; |
| 1547 | } |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 1548 | } else if (audio_extn_hfp_is_active(adev)) { |
| 1549 | hfp_ucid = audio_extn_hfp_get_usecase(); |
| 1550 | hfp_usecase = get_usecase_from_list(adev, hfp_ucid); |
| 1551 | if (hfp_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) { |
| 1552 | in_snd_device = hfp_usecase->in_snd_device; |
| 1553 | out_snd_device = hfp_usecase->out_snd_device; |
| 1554 | } |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1555 | } |
| 1556 | if (usecase->type == PCM_PLAYBACK) { |
| 1557 | usecase->devices = usecase->stream.out->devices; |
| 1558 | in_snd_device = SND_DEVICE_NONE; |
Ravi Kumar Alamanda | 59d296d | 2013-05-02 11:25:27 -0700 | [diff] [blame] | 1559 | if (out_snd_device == SND_DEVICE_NONE) { |
carter_hsu | 1e1ebbf | 2017-08-15 11:39:55 +0800 | [diff] [blame] | 1560 | struct stream_out *voip_out = adev->primary_output; |
Eric Laurent | d1b7a9b | 2018-11-15 12:24:31 -0800 | [diff] [blame] | 1561 | struct stream_in *voip_in = get_voice_communication_input(adev); |
carter_hsu | 1e1ebbf | 2017-08-15 11:39:55 +0800 | [diff] [blame] | 1562 | |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 1563 | out_snd_device = platform_get_output_snd_device(adev->platform, |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1564 | usecase->stream.out->devices); |
carter_hsu | 1e1ebbf | 2017-08-15 11:39:55 +0800 | [diff] [blame] | 1565 | |
| 1566 | if (voip_usecase) |
| 1567 | voip_out = voip_usecase->stream.out; |
| 1568 | |
Eric Laurent | d1b7a9b | 2018-11-15 12:24:31 -0800 | [diff] [blame] | 1569 | if (usecase->stream.out == voip_out && voip_in != NULL) { |
| 1570 | select_devices(adev, voip_in->usecase); |
Ravi Kumar Alamanda | 59d296d | 2013-05-02 11:25:27 -0700 | [diff] [blame] | 1571 | } |
| 1572 | } |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1573 | } else if (usecase->type == PCM_CAPTURE) { |
| 1574 | usecase->devices = usecase->stream.in->device; |
| 1575 | out_snd_device = SND_DEVICE_NONE; |
Ravi Kumar Alamanda | 59d296d | 2013-05-02 11:25:27 -0700 | [diff] [blame] | 1576 | if (in_snd_device == SND_DEVICE_NONE) { |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 1577 | audio_devices_t out_device = AUDIO_DEVICE_NONE; |
Eric Laurent | d1b7a9b | 2018-11-15 12:24:31 -0800 | [diff] [blame] | 1578 | struct stream_in *voip_in = get_voice_communication_input(adev); |
| 1579 | |
| 1580 | if (voip_in != NULL) { |
carter_hsu | 1e1ebbf | 2017-08-15 11:39:55 +0800 | [diff] [blame] | 1581 | |
| 1582 | struct audio_usecase *voip_usecase = get_usecase_from_list(adev, |
| 1583 | USECASE_AUDIO_PLAYBACK_VOIP); |
| 1584 | |
Ravi Kumar Alamanda | f282901 | 2014-11-12 16:16:10 -0800 | [diff] [blame] | 1585 | platform_set_echo_reference(adev, false, AUDIO_DEVICE_NONE); |
Ravi Kumar Alamanda | 0464359 | 2015-09-24 19:17:26 -0700 | [diff] [blame] | 1586 | if (usecase->id == USECASE_AUDIO_RECORD_AFE_PROXY) { |
| 1587 | out_device = AUDIO_DEVICE_OUT_TELEPHONY_TX; |
carter_hsu | 1e1ebbf | 2017-08-15 11:39:55 +0800 | [diff] [blame] | 1588 | } else if (voip_usecase) { |
| 1589 | out_device = voip_usecase->stream.out->devices; |
juyuchen | d194b43 | 2018-11-16 14:15:16 +0800 | [diff] [blame] | 1590 | } else if (adev->primary_output && |
| 1591 | !adev->primary_output->standby) { |
Ravi Kumar Alamanda | 0464359 | 2015-09-24 19:17:26 -0700 | [diff] [blame] | 1592 | out_device = adev->primary_output->devices; |
juyuchen | d194b43 | 2018-11-16 14:15:16 +0800 | [diff] [blame] | 1593 | } else { |
| 1594 | /* forcing speaker o/p device to get matching i/p pair |
| 1595 | in case o/p is not routed from same primary HAL */ |
| 1596 | out_device = AUDIO_DEVICE_OUT_SPEAKER; |
Ravi Kumar Alamanda | 0464359 | 2015-09-24 19:17:26 -0700 | [diff] [blame] | 1597 | } |
Ravi Kumar Alamanda | 59d296d | 2013-05-02 11:25:27 -0700 | [diff] [blame] | 1598 | } |
Eric Laurent | d1b7a9b | 2018-11-15 12:24:31 -0800 | [diff] [blame] | 1599 | in_snd_device = platform_get_input_snd_device(adev->platform, |
| 1600 | usecase->stream.in, |
| 1601 | out_device); |
Ravi Kumar Alamanda | 59d296d | 2013-05-02 11:25:27 -0700 | [diff] [blame] | 1602 | } |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1603 | } |
| 1604 | } |
| 1605 | |
| 1606 | if (out_snd_device == usecase->out_snd_device && |
| 1607 | in_snd_device == usecase->in_snd_device) { |
HW Lee | f88584d | 2019-03-18 17:27:18 +0800 | [diff] [blame] | 1608 | if (!force_device_switch(usecase) && !force_switch) |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 1609 | return 0; |
| 1610 | } |
| 1611 | |
| 1612 | if (is_a2dp_device(out_snd_device) && !audio_extn_a2dp_is_ready()) { |
| 1613 | ALOGD("SCO/A2DP is selected but they are not connected/ready hence dont route"); |
| 1614 | return 0; |
| 1615 | } |
| 1616 | |
Aniket Kumar Lata | 9723a96 | 2018-05-16 17:41:55 -0700 | [diff] [blame] | 1617 | if ((out_snd_device == SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP || |
| 1618 | out_snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP) && |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 1619 | (!audio_extn_a2dp_is_ready())) { |
| 1620 | ALOGW("%s: A2DP profile is not ready, routing to speaker only", __func__); |
juyuchen | 9baad39 | 2018-06-05 19:02:10 +0800 | [diff] [blame] | 1621 | if (out_snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP) |
| 1622 | out_snd_device = SND_DEVICE_OUT_SPEAKER_SAFE; |
| 1623 | else |
| 1624 | out_snd_device = SND_DEVICE_OUT_SPEAKER; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1625 | } |
| 1626 | |
juyuchen | 66c4ecf | 2018-08-06 15:39:34 +0800 | [diff] [blame] | 1627 | if (usecase->id == USECASE_INCALL_MUSIC_UPLINK) { |
| 1628 | out_snd_device = SND_DEVICE_OUT_VOICE_MUSIC_TX; |
| 1629 | } |
| 1630 | |
Eric Laurent | 2bafff1 | 2016-03-17 12:17:23 -0700 | [diff] [blame] | 1631 | if (out_snd_device != SND_DEVICE_NONE && |
| 1632 | out_snd_device != adev->last_logged_snd_device[uc_id][0]) { |
| 1633 | ALOGD("%s: changing use case %s output device from(%d: %s, acdb %d) to (%d: %s, acdb %d)", |
| 1634 | __func__, |
| 1635 | use_case_table[uc_id], |
| 1636 | adev->last_logged_snd_device[uc_id][0], |
| 1637 | platform_get_snd_device_name(adev->last_logged_snd_device[uc_id][0]), |
| 1638 | adev->last_logged_snd_device[uc_id][0] != SND_DEVICE_NONE ? |
| 1639 | platform_get_snd_device_acdb_id(adev->last_logged_snd_device[uc_id][0]) : |
| 1640 | -1, |
| 1641 | out_snd_device, |
| 1642 | platform_get_snd_device_name(out_snd_device), |
| 1643 | platform_get_snd_device_acdb_id(out_snd_device)); |
| 1644 | adev->last_logged_snd_device[uc_id][0] = out_snd_device; |
| 1645 | } |
| 1646 | if (in_snd_device != SND_DEVICE_NONE && |
| 1647 | in_snd_device != adev->last_logged_snd_device[uc_id][1]) { |
| 1648 | ALOGD("%s: changing use case %s input device from(%d: %s, acdb %d) to (%d: %s, acdb %d)", |
| 1649 | __func__, |
| 1650 | use_case_table[uc_id], |
| 1651 | adev->last_logged_snd_device[uc_id][1], |
| 1652 | platform_get_snd_device_name(adev->last_logged_snd_device[uc_id][1]), |
| 1653 | adev->last_logged_snd_device[uc_id][1] != SND_DEVICE_NONE ? |
| 1654 | platform_get_snd_device_acdb_id(adev->last_logged_snd_device[uc_id][1]) : |
| 1655 | -1, |
| 1656 | in_snd_device, |
| 1657 | platform_get_snd_device_name(in_snd_device), |
| 1658 | platform_get_snd_device_acdb_id(in_snd_device)); |
| 1659 | adev->last_logged_snd_device[uc_id][1] = in_snd_device; |
| 1660 | } |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 1661 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1662 | /* |
| 1663 | * Limitation: While in call, to do a device switch we need to disable |
| 1664 | * and enable both RX and TX devices though one of them is same as current |
| 1665 | * device. |
| 1666 | */ |
Ravi Kumar Alamanda | 36886fc | 2014-09-29 13:41:51 -0700 | [diff] [blame] | 1667 | if ((usecase->type == VOICE_CALL) && |
| 1668 | (usecase->in_snd_device != SND_DEVICE_NONE) && |
| 1669 | (usecase->out_snd_device != SND_DEVICE_NONE)) { |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 1670 | status = platform_switch_voice_call_device_pre(adev->platform); |
vivek mehta | 765eb64 | 2015-08-07 19:46:06 -0700 | [diff] [blame] | 1671 | /* Disable sidetone only if voice call already exists */ |
| 1672 | if (voice_is_call_state_active(adev)) |
| 1673 | voice_set_sidetone(adev, usecase->out_snd_device, false); |
Ravi Kumar Alamanda | 610e8cc | 2013-02-12 01:42:38 -0800 | [diff] [blame] | 1674 | } |
| 1675 | |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1676 | /* Disable current sound devices */ |
| 1677 | if (usecase->out_snd_device != SND_DEVICE_NONE) { |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 1678 | disable_audio_route(adev, usecase); |
| 1679 | disable_snd_device(adev, usecase->out_snd_device); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1680 | } |
| 1681 | |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1682 | if (usecase->in_snd_device != SND_DEVICE_NONE) { |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 1683 | disable_audio_route(adev, usecase); |
| 1684 | disable_snd_device(adev, usecase->in_snd_device); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1685 | } |
| 1686 | |
Ravi Kumar Alamanda | 83281a9 | 2014-05-19 18:14:57 -0700 | [diff] [blame] | 1687 | /* Applicable only on the targets that has external modem. |
| 1688 | * New device information should be sent to modem before enabling |
| 1689 | * the devices to reduce in-call device switch time. |
| 1690 | */ |
Ravi Kumar Alamanda | 36886fc | 2014-09-29 13:41:51 -0700 | [diff] [blame] | 1691 | if ((usecase->type == VOICE_CALL) && |
| 1692 | (usecase->in_snd_device != SND_DEVICE_NONE) && |
| 1693 | (usecase->out_snd_device != SND_DEVICE_NONE)) { |
Ravi Kumar Alamanda | 83281a9 | 2014-05-19 18:14:57 -0700 | [diff] [blame] | 1694 | status = platform_switch_voice_call_enable_device_config(adev->platform, |
| 1695 | out_snd_device, |
| 1696 | in_snd_device); |
Ravi Kumar Alamanda | 36886fc | 2014-09-29 13:41:51 -0700 | [diff] [blame] | 1697 | } |
Ravi Kumar Alamanda | 83281a9 | 2014-05-19 18:14:57 -0700 | [diff] [blame] | 1698 | |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1699 | /* Enable new sound devices */ |
| 1700 | if (out_snd_device != SND_DEVICE_NONE) { |
David Lin | ee3fe40 | 2017-03-13 10:00:42 -0700 | [diff] [blame] | 1701 | if ((usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) || |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 1702 | (usecase->devices & (AUDIO_DEVICE_OUT_USB_DEVICE|AUDIO_DEVICE_OUT_USB_HEADSET)) || |
| 1703 | (usecase->devices & AUDIO_DEVICE_OUT_ALL_A2DP)) |
Ravi Kumar Alamanda | b7ea4f5 | 2015-06-08 16:44:05 -0700 | [diff] [blame] | 1704 | check_and_route_playback_usecases(adev, usecase, out_snd_device); |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 1705 | enable_snd_device(adev, out_snd_device); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1706 | } |
| 1707 | |
Ravi Kumar Alamanda | c4ba743 | 2013-06-05 14:11:39 -0700 | [diff] [blame] | 1708 | if (in_snd_device != SND_DEVICE_NONE) { |
| 1709 | check_and_route_capture_usecases(adev, usecase, in_snd_device); |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 1710 | enable_snd_device(adev, in_snd_device); |
Ravi Kumar Alamanda | c4ba743 | 2013-06-05 14:11:39 -0700 | [diff] [blame] | 1711 | } |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1712 | |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 1713 | if (usecase->type == VOICE_CALL) |
| 1714 | status = platform_switch_voice_call_device_post(adev->platform, |
| 1715 | out_snd_device, |
| 1716 | in_snd_device); |
Ravi Kumar Alamanda | 610e8cc | 2013-02-12 01:42:38 -0800 | [diff] [blame] | 1717 | |
sangwoo | 170731f | 2013-06-08 15:36:36 +0900 | [diff] [blame] | 1718 | usecase->in_snd_device = in_snd_device; |
| 1719 | usecase->out_snd_device = out_snd_device; |
| 1720 | |
Alain Vongsouvanh | 13f26e8 | 2016-11-18 14:39:11 -0800 | [diff] [blame] | 1721 | audio_extn_tfa_98xx_set_mode(); |
| 1722 | |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 1723 | enable_audio_route(adev, usecase); |
sangwoo | 170731f | 2013-06-08 15:36:36 +0900 | [diff] [blame] | 1724 | |
Vignesh Kulothungan | 6469882 | 2018-01-23 11:25:18 -0800 | [diff] [blame] | 1725 | /* If input stream is already running the effect needs to be |
| 1726 | applied on the new input device that's being enabled here. */ |
| 1727 | if (in_snd_device != SND_DEVICE_NONE) |
| 1728 | check_and_enable_effect(adev); |
| 1729 | |
Ravi Kumar Alamanda | 83281a9 | 2014-05-19 18:14:57 -0700 | [diff] [blame] | 1730 | /* Applicable only on the targets that has external modem. |
| 1731 | * Enable device command should be sent to modem only after |
| 1732 | * enabling voice call mixer controls |
| 1733 | */ |
vivek mehta | 765eb64 | 2015-08-07 19:46:06 -0700 | [diff] [blame] | 1734 | if (usecase->type == VOICE_CALL) { |
Ravi Kumar Alamanda | 83281a9 | 2014-05-19 18:14:57 -0700 | [diff] [blame] | 1735 | status = platform_switch_voice_call_usecase_route_post(adev->platform, |
| 1736 | out_snd_device, |
| 1737 | in_snd_device); |
vivek mehta | 765eb64 | 2015-08-07 19:46:06 -0700 | [diff] [blame] | 1738 | /* Enable sidetone only if voice call already exists */ |
| 1739 | if (voice_is_call_state_active(adev)) |
| 1740 | voice_set_sidetone(adev, out_snd_device, true); |
| 1741 | } |
Ravi Kumar Alamanda | 83281a9 | 2014-05-19 18:14:57 -0700 | [diff] [blame] | 1742 | |
Yung Ti Su | e9d64f9 | 2018-06-22 17:38:47 +0800 | [diff] [blame] | 1743 | if (usecase->type != PCM_CAPTURE && voip_usecase) { |
Eric Laurent | f4520b0 | 2017-09-20 18:31:58 -0700 | [diff] [blame] | 1744 | struct stream_out *voip_out = voip_usecase->stream.out; |
| 1745 | audio_extn_utils_send_app_type_gain(adev, |
| 1746 | voip_out->app_type_cfg.app_type, |
| 1747 | &voip_out->app_type_cfg.gain[0]); |
| 1748 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1749 | return status; |
| 1750 | } |
| 1751 | |
HW Lee | f88584d | 2019-03-18 17:27:18 +0800 | [diff] [blame] | 1752 | int select_devices(struct audio_device *adev, |
| 1753 | audio_usecase_t uc_id) |
| 1754 | { |
| 1755 | return select_devices_with_force_switch(adev, uc_id, false); |
| 1756 | } |
| 1757 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1758 | static int stop_input_stream(struct stream_in *in) |
| 1759 | { |
| 1760 | int i, ret = 0; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1761 | struct audio_usecase *uc_info; |
| 1762 | struct audio_device *adev = in->dev; |
| 1763 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 1764 | ALOGV("%s: enter: usecase(%d: %s)", __func__, |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1765 | in->usecase, use_case_table[in->usecase]); |
Haynes Mathew George | 8ea0472 | 2017-07-25 17:41:49 -0700 | [diff] [blame] | 1766 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1767 | uc_info = get_usecase_from_list(adev, in->usecase); |
| 1768 | if (uc_info == NULL) { |
| 1769 | ALOGE("%s: Could not find the usecase (%d) in the list", |
| 1770 | __func__, in->usecase); |
| 1771 | return -EINVAL; |
| 1772 | } |
| 1773 | |
vivek mehta | 781065c | 2017-04-04 12:55:01 -0700 | [diff] [blame] | 1774 | /* Close in-call recording streams */ |
| 1775 | voice_check_and_stop_incall_rec_usecase(adev, in); |
| 1776 | |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 1777 | /* 1. Disable stream specific mixer controls */ |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 1778 | disable_audio_route(adev, uc_info); |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1779 | |
| 1780 | /* 2. Disable the tx device */ |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 1781 | disable_snd_device(adev, uc_info->in_snd_device); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1782 | |
Ravi Kumar Alamanda | 3b1816c | 2013-02-27 23:01:21 -0800 | [diff] [blame] | 1783 | list_remove(&uc_info->list); |
| 1784 | free(uc_info); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1785 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 1786 | ALOGV("%s: exit: status(%d)", __func__, ret); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1787 | return ret; |
| 1788 | } |
| 1789 | |
| 1790 | int start_input_stream(struct stream_in *in) |
| 1791 | { |
| 1792 | /* 1. Enable output device and stream routing controls */ |
Eric Laurent | c840063 | 2013-02-14 19:04:54 -0800 | [diff] [blame] | 1793 | int ret = 0; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1794 | struct audio_usecase *uc_info; |
| 1795 | struct audio_device *adev = in->dev; |
| 1796 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 1797 | ALOGV("%s: enter: usecase(%d)", __func__, in->usecase); |
Haynes Mathew George | c735fb0 | 2016-06-30 18:00:28 -0700 | [diff] [blame] | 1798 | |
Alain Vongsouvanh | 13f26e8 | 2016-11-18 14:39:11 -0800 | [diff] [blame] | 1799 | if (audio_extn_tfa_98xx_is_supported() && !audio_ssr_status(adev)) |
| 1800 | return -EIO; |
| 1801 | |
Haynes Mathew George | c735fb0 | 2016-06-30 18:00:28 -0700 | [diff] [blame] | 1802 | if (in->card_status == CARD_STATUS_OFFLINE || |
| 1803 | adev->card_status == CARD_STATUS_OFFLINE) { |
| 1804 | ALOGW("in->card_status or adev->card_status offline, try again"); |
| 1805 | ret = -EAGAIN; |
| 1806 | goto error_config; |
| 1807 | } |
| 1808 | |
vivek mehta | 781065c | 2017-04-04 12:55:01 -0700 | [diff] [blame] | 1809 | /* Check if source matches incall recording usecase criteria */ |
| 1810 | ret = voice_check_and_set_incall_rec_usecase(adev, in); |
| 1811 | if (ret) |
| 1812 | goto error_config; |
| 1813 | else |
| 1814 | ALOGV("%s: usecase(%d)", __func__, in->usecase); |
| 1815 | |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 1816 | 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] | 1817 | if (in->pcm_device_id < 0) { |
| 1818 | ALOGE("%s: Could not find PCM device id for the usecase(%d)", |
| 1819 | __func__, in->usecase); |
Eric Laurent | c840063 | 2013-02-14 19:04:54 -0800 | [diff] [blame] | 1820 | ret = -EINVAL; |
| 1821 | goto error_config; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1822 | } |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1823 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1824 | uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase)); |
| 1825 | uc_info->id = in->usecase; |
| 1826 | uc_info->type = PCM_CAPTURE; |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 1827 | uc_info->stream.in = in; |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1828 | uc_info->devices = in->device; |
| 1829 | uc_info->in_snd_device = SND_DEVICE_NONE; |
| 1830 | uc_info->out_snd_device = SND_DEVICE_NONE; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1831 | |
Ravi Kumar Alamanda | 3b1816c | 2013-02-27 23:01:21 -0800 | [diff] [blame] | 1832 | list_add_tail(&adev->usecase_list, &uc_info->list); |
Ravi Kumar Alamanda | 533bb72 | 2015-09-23 13:47:03 -0700 | [diff] [blame] | 1833 | |
Wei Wang | f4837d5 | 2017-11-21 14:51:20 -0800 | [diff] [blame] | 1834 | audio_streaming_hint_start(); |
Ravi Kumar Alamanda | 533bb72 | 2015-09-23 13:47:03 -0700 | [diff] [blame] | 1835 | audio_extn_perf_lock_acquire(); |
| 1836 | |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1837 | select_devices(adev, in->usecase); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1838 | |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 1839 | if (in->usecase == USECASE_AUDIO_RECORD_MMAP) { |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 1840 | if (in->pcm == NULL || !pcm_is_ready(in->pcm)) { |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 1841 | ALOGE("%s: pcm stream not ready", __func__); |
| 1842 | goto error_open; |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 1843 | } |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 1844 | ret = pcm_start(in->pcm); |
Haynes Mathew George | fbe8731 | 2016-08-01 19:29:27 -0700 | [diff] [blame] | 1845 | if (ret < 0) { |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 1846 | ALOGE("%s: MMAP pcm_start failed ret %d", __func__, ret); |
| 1847 | goto error_open; |
| 1848 | } |
| 1849 | } else { |
| 1850 | unsigned int flags = PCM_IN | PCM_MONOTONIC; |
| 1851 | unsigned int pcm_open_retry_count = 0; |
| 1852 | |
| 1853 | if (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY) { |
| 1854 | flags |= PCM_MMAP | PCM_NOIRQ; |
| 1855 | pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT; |
| 1856 | } else if (in->realtime) { |
| 1857 | flags |= PCM_MMAP | PCM_NOIRQ; |
| 1858 | } |
| 1859 | |
| 1860 | ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d", |
| 1861 | __func__, adev->snd_card, in->pcm_device_id, in->config.channels); |
| 1862 | |
| 1863 | while (1) { |
| 1864 | in->pcm = pcm_open(adev->snd_card, in->pcm_device_id, |
| 1865 | flags, &in->config); |
| 1866 | if (in->pcm == NULL || !pcm_is_ready(in->pcm)) { |
| 1867 | ALOGE("%s: %s", __func__, pcm_get_error(in->pcm)); |
| 1868 | if (in->pcm != NULL) { |
| 1869 | pcm_close(in->pcm); |
| 1870 | in->pcm = NULL; |
| 1871 | } |
| 1872 | if (pcm_open_retry_count-- == 0) { |
| 1873 | ret = -EIO; |
| 1874 | goto error_open; |
| 1875 | } |
| 1876 | usleep(PROXY_OPEN_WAIT_TIME * 1000); |
| 1877 | continue; |
| 1878 | } |
| 1879 | break; |
| 1880 | } |
| 1881 | |
| 1882 | ALOGV("%s: pcm_prepare", __func__); |
| 1883 | ret = pcm_prepare(in->pcm); |
| 1884 | if (ret < 0) { |
| 1885 | ALOGE("%s: pcm_prepare returned %d", __func__, ret); |
Haynes Mathew George | fbe8731 | 2016-08-01 19:29:27 -0700 | [diff] [blame] | 1886 | pcm_close(in->pcm); |
| 1887 | in->pcm = NULL; |
| 1888 | goto error_open; |
| 1889 | } |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 1890 | if (in->realtime) { |
| 1891 | ret = pcm_start(in->pcm); |
| 1892 | if (ret < 0) { |
| 1893 | ALOGE("%s: RT pcm_start failed ret %d", __func__, ret); |
| 1894 | pcm_close(in->pcm); |
| 1895 | in->pcm = NULL; |
| 1896 | goto error_open; |
| 1897 | } |
| 1898 | } |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 1899 | } |
Haynes Mathew George | fbe8731 | 2016-08-01 19:29:27 -0700 | [diff] [blame] | 1900 | register_in_stream(in); |
Vignesh Kulothungan | 6469882 | 2018-01-23 11:25:18 -0800 | [diff] [blame] | 1901 | check_and_enable_effect(adev); |
justinweng | c6347db | 2019-02-21 18:49:00 +0800 | [diff] [blame] | 1902 | audio_extn_audiozoom_set_microphone_direction(in, in->zoom); |
| 1903 | audio_extn_audiozoom_set_microphone_field_dimension(in, in->direction); |
Wei Wang | f4837d5 | 2017-11-21 14:51:20 -0800 | [diff] [blame] | 1904 | audio_streaming_hint_end(); |
Ravi Kumar Alamanda | 533bb72 | 2015-09-23 13:47:03 -0700 | [diff] [blame] | 1905 | audio_extn_perf_lock_release(); |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 1906 | ALOGV("%s: exit", __func__); |
Haynes Mathew George | 88e6fb2 | 2015-08-19 11:51:34 -0700 | [diff] [blame] | 1907 | |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 1908 | return 0; |
Eric Laurent | c840063 | 2013-02-14 19:04:54 -0800 | [diff] [blame] | 1909 | |
| 1910 | error_open: |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1911 | stop_input_stream(in); |
Wei Wang | f4837d5 | 2017-11-21 14:51:20 -0800 | [diff] [blame] | 1912 | audio_streaming_hint_end(); |
Ravi Kumar Alamanda | 533bb72 | 2015-09-23 13:47:03 -0700 | [diff] [blame] | 1913 | audio_extn_perf_lock_release(); |
Eric Laurent | c840063 | 2013-02-14 19:04:54 -0800 | [diff] [blame] | 1914 | |
| 1915 | error_config: |
Eric Laurent | 2bafff1 | 2016-03-17 12:17:23 -0700 | [diff] [blame] | 1916 | ALOGW("%s: exit: status(%d)", __func__, ret); |
Eric Laurent | c840063 | 2013-02-14 19:04:54 -0800 | [diff] [blame] | 1917 | return ret; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1918 | } |
| 1919 | |
Eric Laurent | a147807 | 2015-09-21 17:21:52 -0700 | [diff] [blame] | 1920 | void lock_input_stream(struct stream_in *in) |
| 1921 | { |
| 1922 | pthread_mutex_lock(&in->pre_lock); |
| 1923 | pthread_mutex_lock(&in->lock); |
| 1924 | pthread_mutex_unlock(&in->pre_lock); |
| 1925 | } |
| 1926 | |
| 1927 | void lock_output_stream(struct stream_out *out) |
| 1928 | { |
| 1929 | pthread_mutex_lock(&out->pre_lock); |
| 1930 | pthread_mutex_lock(&out->lock); |
| 1931 | pthread_mutex_unlock(&out->pre_lock); |
| 1932 | } |
| 1933 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 1934 | /* must be called with out->lock locked */ |
| 1935 | static int send_offload_cmd_l(struct stream_out* out, int command) |
| 1936 | { |
| 1937 | struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd)); |
| 1938 | |
| 1939 | ALOGVV("%s %d", __func__, command); |
| 1940 | |
| 1941 | cmd->cmd = command; |
| 1942 | list_add_tail(&out->offload_cmd_list, &cmd->node); |
| 1943 | pthread_cond_signal(&out->offload_cond); |
| 1944 | return 0; |
| 1945 | } |
| 1946 | |
| 1947 | /* must be called iwth out->lock locked */ |
| 1948 | static void stop_compressed_output_l(struct stream_out *out) |
| 1949 | { |
| 1950 | out->offload_state = OFFLOAD_STATE_IDLE; |
| 1951 | out->playback_started = 0; |
Haynes Mathew George | 352f27b | 2013-07-26 00:00:15 -0700 | [diff] [blame] | 1952 | out->send_new_metadata = 1; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 1953 | if (out->compr != NULL) { |
| 1954 | compress_stop(out->compr); |
| 1955 | while (out->offload_thread_blocked) { |
| 1956 | pthread_cond_wait(&out->cond, &out->lock); |
| 1957 | } |
| 1958 | } |
| 1959 | } |
| 1960 | |
| 1961 | static void *offload_thread_loop(void *context) |
| 1962 | { |
| 1963 | struct stream_out *out = (struct stream_out *) context; |
| 1964 | struct listnode *item; |
| 1965 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 1966 | setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO); |
| 1967 | set_sched_policy(0, SP_FOREGROUND); |
| 1968 | prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0); |
| 1969 | |
| 1970 | ALOGV("%s", __func__); |
juyuchen | b0f5bfd | 2018-12-12 17:58:09 +0800 | [diff] [blame] | 1971 | |
Eric Laurent | a147807 | 2015-09-21 17:21:52 -0700 | [diff] [blame] | 1972 | lock_output_stream(out); |
juyuchen | b0f5bfd | 2018-12-12 17:58:09 +0800 | [diff] [blame] | 1973 | out->offload_state = OFFLOAD_STATE_IDLE; |
| 1974 | out->playback_started = 0; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 1975 | for (;;) { |
| 1976 | struct offload_cmd *cmd = NULL; |
| 1977 | stream_callback_event_t event; |
| 1978 | bool send_callback = false; |
| 1979 | |
| 1980 | ALOGVV("%s offload_cmd_list %d out->offload_state %d", |
| 1981 | __func__, list_empty(&out->offload_cmd_list), |
| 1982 | out->offload_state); |
| 1983 | if (list_empty(&out->offload_cmd_list)) { |
| 1984 | ALOGV("%s SLEEPING", __func__); |
| 1985 | pthread_cond_wait(&out->offload_cond, &out->lock); |
| 1986 | ALOGV("%s RUNNING", __func__); |
| 1987 | continue; |
| 1988 | } |
| 1989 | |
| 1990 | item = list_head(&out->offload_cmd_list); |
| 1991 | cmd = node_to_item(item, struct offload_cmd, node); |
| 1992 | list_remove(item); |
| 1993 | |
| 1994 | ALOGVV("%s STATE %d CMD %d out->compr %p", |
| 1995 | __func__, out->offload_state, cmd->cmd, out->compr); |
| 1996 | |
| 1997 | if (cmd->cmd == OFFLOAD_CMD_EXIT) { |
| 1998 | free(cmd); |
| 1999 | break; |
| 2000 | } |
| 2001 | |
| 2002 | if (out->compr == NULL) { |
| 2003 | ALOGE("%s: Compress handle is NULL", __func__); |
Andy Hung | 68f55fd | 2016-04-21 11:51:11 -0700 | [diff] [blame] | 2004 | free(cmd); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2005 | pthread_cond_signal(&out->cond); |
| 2006 | continue; |
| 2007 | } |
| 2008 | out->offload_thread_blocked = true; |
| 2009 | pthread_mutex_unlock(&out->lock); |
| 2010 | send_callback = false; |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 2011 | switch (cmd->cmd) { |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2012 | case OFFLOAD_CMD_WAIT_FOR_BUFFER: |
| 2013 | compress_wait(out->compr, -1); |
| 2014 | send_callback = true; |
| 2015 | event = STREAM_CBK_EVENT_WRITE_READY; |
| 2016 | break; |
| 2017 | case OFFLOAD_CMD_PARTIAL_DRAIN: |
Haynes Mathew George | 352f27b | 2013-07-26 00:00:15 -0700 | [diff] [blame] | 2018 | compress_next_track(out->compr); |
| 2019 | compress_partial_drain(out->compr); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2020 | send_callback = true; |
| 2021 | event = STREAM_CBK_EVENT_DRAIN_READY; |
Ravi Kumar Alamanda | cc4f6bf | 2014-12-02 19:21:51 -0800 | [diff] [blame] | 2022 | /* Resend the metadata for next iteration */ |
| 2023 | out->send_new_metadata = 1; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2024 | break; |
| 2025 | case OFFLOAD_CMD_DRAIN: |
| 2026 | compress_drain(out->compr); |
| 2027 | send_callback = true; |
| 2028 | event = STREAM_CBK_EVENT_DRAIN_READY; |
| 2029 | break; |
Haynes Mathew George | 3ddd3bd | 2016-07-07 20:01:53 -0700 | [diff] [blame] | 2030 | case OFFLOAD_CMD_ERROR: |
| 2031 | send_callback = true; |
| 2032 | event = STREAM_CBK_EVENT_ERROR; |
| 2033 | break; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2034 | default: |
| 2035 | ALOGE("%s unknown command received: %d", __func__, cmd->cmd); |
| 2036 | break; |
| 2037 | } |
Eric Laurent | a147807 | 2015-09-21 17:21:52 -0700 | [diff] [blame] | 2038 | lock_output_stream(out); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2039 | out->offload_thread_blocked = false; |
| 2040 | pthread_cond_signal(&out->cond); |
Eric Laurent | 6e89524 | 2013-09-05 16:10:57 -0700 | [diff] [blame] | 2041 | if (send_callback) { |
Ravi Kumar Alamanda | cc4f6bf | 2014-12-02 19:21:51 -0800 | [diff] [blame] | 2042 | ALOGVV("%s: sending offload_callback event %d", __func__, event); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2043 | out->offload_callback(event, NULL, out->offload_cookie); |
Eric Laurent | 6e89524 | 2013-09-05 16:10:57 -0700 | [diff] [blame] | 2044 | } |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2045 | free(cmd); |
| 2046 | } |
| 2047 | |
| 2048 | pthread_cond_signal(&out->cond); |
| 2049 | while (!list_empty(&out->offload_cmd_list)) { |
| 2050 | item = list_head(&out->offload_cmd_list); |
| 2051 | list_remove(item); |
| 2052 | free(node_to_item(item, struct offload_cmd, node)); |
| 2053 | } |
| 2054 | pthread_mutex_unlock(&out->lock); |
| 2055 | |
| 2056 | return NULL; |
| 2057 | } |
| 2058 | |
| 2059 | static int create_offload_callback_thread(struct stream_out *out) |
| 2060 | { |
| 2061 | pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL); |
| 2062 | list_init(&out->offload_cmd_list); |
| 2063 | pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL, |
| 2064 | offload_thread_loop, out); |
| 2065 | return 0; |
| 2066 | } |
| 2067 | |
| 2068 | static int destroy_offload_callback_thread(struct stream_out *out) |
| 2069 | { |
Eric Laurent | a147807 | 2015-09-21 17:21:52 -0700 | [diff] [blame] | 2070 | lock_output_stream(out); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2071 | stop_compressed_output_l(out); |
| 2072 | send_offload_cmd_l(out, OFFLOAD_CMD_EXIT); |
| 2073 | |
| 2074 | pthread_mutex_unlock(&out->lock); |
| 2075 | pthread_join(out->offload_thread, (void **) NULL); |
| 2076 | pthread_cond_destroy(&out->offload_cond); |
| 2077 | |
| 2078 | return 0; |
| 2079 | } |
| 2080 | |
Eric Laurent | 07eeafd | 2013-10-06 12:52:49 -0700 | [diff] [blame] | 2081 | static bool allow_hdmi_channel_config(struct audio_device *adev) |
| 2082 | { |
| 2083 | struct listnode *node; |
| 2084 | struct audio_usecase *usecase; |
| 2085 | bool ret = true; |
| 2086 | |
| 2087 | list_for_each(node, &adev->usecase_list) { |
| 2088 | usecase = node_to_item(node, struct audio_usecase, list); |
| 2089 | if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) { |
| 2090 | /* |
| 2091 | * If voice call is already existing, do not proceed further to avoid |
| 2092 | * disabling/enabling both RX and TX devices, CSD calls, etc. |
| 2093 | * Once the voice call done, the HDMI channels can be configured to |
| 2094 | * max channels of remaining use cases. |
| 2095 | */ |
| 2096 | if (usecase->id == USECASE_VOICE_CALL) { |
Joe Onorato | 188b622 | 2016-03-01 11:02:27 -0800 | [diff] [blame] | 2097 | ALOGV("%s: voice call is active, no change in HDMI channels", |
Eric Laurent | 07eeafd | 2013-10-06 12:52:49 -0700 | [diff] [blame] | 2098 | __func__); |
| 2099 | ret = false; |
| 2100 | break; |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 2101 | } else if (usecase->id == USECASE_AUDIO_PLAYBACK_HIFI) { |
| 2102 | ALOGV("%s: hifi playback is active, " |
Eric Laurent | 07eeafd | 2013-10-06 12:52:49 -0700 | [diff] [blame] | 2103 | "no change in HDMI channels", __func__); |
| 2104 | ret = false; |
| 2105 | break; |
| 2106 | } |
| 2107 | } |
| 2108 | } |
| 2109 | return ret; |
| 2110 | } |
| 2111 | |
| 2112 | static int check_and_set_hdmi_channels(struct audio_device *adev, |
| 2113 | unsigned int channels) |
| 2114 | { |
| 2115 | struct listnode *node; |
| 2116 | struct audio_usecase *usecase; |
| 2117 | |
| 2118 | /* Check if change in HDMI channel config is allowed */ |
| 2119 | if (!allow_hdmi_channel_config(adev)) |
| 2120 | return 0; |
| 2121 | |
| 2122 | if (channels == adev->cur_hdmi_channels) { |
Joe Onorato | 188b622 | 2016-03-01 11:02:27 -0800 | [diff] [blame] | 2123 | ALOGV("%s: Requested channels are same as current", __func__); |
Eric Laurent | 07eeafd | 2013-10-06 12:52:49 -0700 | [diff] [blame] | 2124 | return 0; |
| 2125 | } |
| 2126 | |
| 2127 | platform_set_hdmi_channels(adev->platform, channels); |
| 2128 | adev->cur_hdmi_channels = channels; |
| 2129 | |
| 2130 | /* |
| 2131 | * Deroute all the playback streams routed to HDMI so that |
| 2132 | * the back end is deactivated. Note that backend will not |
| 2133 | * be deactivated if any one stream is connected to it. |
| 2134 | */ |
| 2135 | list_for_each(node, &adev->usecase_list) { |
| 2136 | usecase = node_to_item(node, struct audio_usecase, list); |
| 2137 | if (usecase->type == PCM_PLAYBACK && |
| 2138 | usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) { |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 2139 | disable_audio_route(adev, usecase); |
Eric Laurent | 07eeafd | 2013-10-06 12:52:49 -0700 | [diff] [blame] | 2140 | } |
| 2141 | } |
| 2142 | |
| 2143 | /* |
| 2144 | * Enable all the streams disabled above. Now the HDMI backend |
| 2145 | * will be activated with new channel configuration |
| 2146 | */ |
| 2147 | list_for_each(node, &adev->usecase_list) { |
| 2148 | usecase = node_to_item(node, struct audio_usecase, list); |
| 2149 | if (usecase->type == PCM_PLAYBACK && |
| 2150 | usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) { |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 2151 | enable_audio_route(adev, usecase); |
Eric Laurent | 07eeafd | 2013-10-06 12:52:49 -0700 | [diff] [blame] | 2152 | } |
| 2153 | } |
| 2154 | |
| 2155 | return 0; |
| 2156 | } |
| 2157 | |
Haynes Mathew George | 65f6b43 | 2018-02-27 17:44:55 -0800 | [diff] [blame] | 2158 | static int check_and_set_usb_service_interval(struct audio_device *adev, |
| 2159 | struct audio_usecase *uc_info, |
| 2160 | bool min) |
| 2161 | { |
| 2162 | struct listnode *node; |
| 2163 | struct audio_usecase *usecase; |
| 2164 | bool switch_usecases = false; |
| 2165 | bool reconfig = false; |
| 2166 | |
| 2167 | if ((uc_info->id != USECASE_AUDIO_PLAYBACK_MMAP) && |
| 2168 | (uc_info->id != USECASE_AUDIO_PLAYBACK_ULL)) |
| 2169 | return -1; |
| 2170 | |
| 2171 | /* set if the valid usecase do not already exist */ |
| 2172 | list_for_each(node, &adev->usecase_list) { |
| 2173 | usecase = node_to_item(node, struct audio_usecase, list); |
| 2174 | if (usecase->type == PCM_PLAYBACK && |
| 2175 | (audio_is_usb_out_device(usecase->devices & AUDIO_DEVICE_OUT_ALL_USB))) { |
| 2176 | switch (usecase->id) { |
| 2177 | case USECASE_AUDIO_PLAYBACK_MMAP: |
| 2178 | case USECASE_AUDIO_PLAYBACK_ULL: |
| 2179 | // cannot reconfig while mmap/ull is present. |
| 2180 | return -1; |
| 2181 | default: |
| 2182 | switch_usecases = true; |
| 2183 | break; |
| 2184 | } |
| 2185 | } |
| 2186 | if (switch_usecases) |
| 2187 | break; |
| 2188 | } |
| 2189 | /* |
| 2190 | * client can try to set service interval in start_output_stream |
| 2191 | * to min or to 0 (i.e reset) in stop_output_stream . |
| 2192 | */ |
| 2193 | unsigned long service_interval = |
| 2194 | audio_extn_usb_find_service_interval(min, true /*playback*/); |
| 2195 | int ret = platform_set_usb_service_interval(adev->platform, |
| 2196 | true /*playback*/, |
| 2197 | service_interval, |
| 2198 | &reconfig); |
| 2199 | /* no change or not supported or no active usecases */ |
| 2200 | if (ret || !reconfig || !switch_usecases) |
| 2201 | return -1; |
| 2202 | return 0; |
| 2203 | #undef VALID_USECASE |
| 2204 | } |
| 2205 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2206 | static int stop_output_stream(struct stream_out *out) |
| 2207 | { |
| 2208 | int i, ret = 0; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2209 | struct audio_usecase *uc_info; |
| 2210 | struct audio_device *adev = out->dev; |
HW Lee | 88512e9 | 2018-06-12 15:26:09 +0800 | [diff] [blame] | 2211 | bool has_voip_usecase = |
| 2212 | get_usecase_from_list(adev, USECASE_AUDIO_PLAYBACK_VOIP) != NULL; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2213 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 2214 | ALOGV("%s: enter: usecase(%d: %s)", __func__, |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 2215 | out->usecase, use_case_table[out->usecase]); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2216 | uc_info = get_usecase_from_list(adev, out->usecase); |
| 2217 | if (uc_info == NULL) { |
| 2218 | ALOGE("%s: Could not find the usecase (%d) in the list", |
| 2219 | __func__, out->usecase); |
| 2220 | return -EINVAL; |
| 2221 | } |
| 2222 | |
Haynes Mathew George | 41f8665 | 2014-06-17 14:22:15 -0700 | [diff] [blame] | 2223 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
| 2224 | if (adev->visualizer_stop_output != NULL) |
| 2225 | adev->visualizer_stop_output(out->handle, out->pcm_device_id); |
| 2226 | if (adev->offload_effects_stop_output != NULL) |
| 2227 | adev->offload_effects_stop_output(out->handle, out->pcm_device_id); |
Haynes Mathew George | d9ca796 | 2017-12-01 16:29:56 -0800 | [diff] [blame] | 2228 | } else if (out->usecase == USECASE_AUDIO_PLAYBACK_ULL || |
| 2229 | out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) { |
| 2230 | audio_low_latency_hint_end(); |
Haynes Mathew George | 41f8665 | 2014-06-17 14:22:15 -0700 | [diff] [blame] | 2231 | } |
Eric Laurent | c4aef75 | 2013-09-12 17:45:53 -0700 | [diff] [blame] | 2232 | |
Arun Mirpuri | 0cc2f31 | 2018-05-29 17:26:07 -0700 | [diff] [blame] | 2233 | if (out->usecase == USECASE_INCALL_MUSIC_UPLINK) |
| 2234 | voice_set_device_mute_flag(adev, false); |
| 2235 | |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 2236 | /* 1. Get and set stream specific mixer controls */ |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 2237 | disable_audio_route(adev, uc_info); |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 2238 | |
| 2239 | /* 2. Disable the rx device */ |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 2240 | disable_snd_device(adev, uc_info->out_snd_device); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2241 | |
Ravi Kumar Alamanda | 3b1816c | 2013-02-27 23:01:21 -0800 | [diff] [blame] | 2242 | list_remove(&uc_info->list); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2243 | |
Eric Laurent | 0499d4f | 2014-08-25 22:39:29 -0500 | [diff] [blame] | 2244 | audio_extn_extspk_update(adev->extspk); |
| 2245 | |
Eric Laurent | 07eeafd | 2013-10-06 12:52:49 -0700 | [diff] [blame] | 2246 | /* Must be called after removing the usecase from list */ |
| 2247 | if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) |
| 2248 | check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS); |
HW Lee | 88512e9 | 2018-06-12 15:26:09 +0800 | [diff] [blame] | 2249 | else if (audio_is_usb_out_device(out->devices & AUDIO_DEVICE_OUT_ALL_USB)) { |
Haynes Mathew George | 65f6b43 | 2018-02-27 17:44:55 -0800 | [diff] [blame] | 2250 | ret = check_and_set_usb_service_interval(adev, uc_info, false /*min*/); |
| 2251 | if (ret == 0) { |
| 2252 | /* default service interval was successfully updated, |
| 2253 | reopen USB backend with new service interval */ |
| 2254 | check_and_route_playback_usecases(adev, uc_info, uc_info->out_snd_device); |
| 2255 | } |
| 2256 | ret = 0; |
Haynes Mathew George | 0ecfe3d | 2017-08-04 15:36:09 -0700 | [diff] [blame] | 2257 | } |
juyuchen | d194b43 | 2018-11-16 14:15:16 +0800 | [diff] [blame] | 2258 | /* 1) media + voip output routing to handset must route media back to |
| 2259 | speaker when voip stops. |
| 2260 | 2) trigger voip input to reroute when voip output changes to |
| 2261 | hearing aid. */ |
HW Lee | 88512e9 | 2018-06-12 15:26:09 +0800 | [diff] [blame] | 2262 | if (has_voip_usecase || |
| 2263 | out->devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) { |
| 2264 | struct listnode *node; |
| 2265 | struct audio_usecase *usecase; |
| 2266 | list_for_each(node, &adev->usecase_list) { |
| 2267 | usecase = node_to_item(node, struct audio_usecase, list); |
juyuchen | d194b43 | 2018-11-16 14:15:16 +0800 | [diff] [blame] | 2268 | if ((usecase->type == PCM_CAPTURE && |
| 2269 | usecase->id != USECASE_AUDIO_RECORD_VOIP) |
| 2270 | || usecase == uc_info) |
HW Lee | 88512e9 | 2018-06-12 15:26:09 +0800 | [diff] [blame] | 2271 | continue; |
| 2272 | |
| 2273 | ALOGD("%s: select_devices at usecase(%d: %s) after removing the usecase(%d: %s)", |
| 2274 | __func__, usecase->id, use_case_table[usecase->id], |
| 2275 | out->usecase, use_case_table[out->usecase]); |
| 2276 | select_devices(adev, usecase->id); |
| 2277 | } |
| 2278 | } |
| 2279 | |
Haynes Mathew George | 65f6b43 | 2018-02-27 17:44:55 -0800 | [diff] [blame] | 2280 | free(uc_info); |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 2281 | ALOGV("%s: exit: status(%d)", __func__, ret); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2282 | return ret; |
| 2283 | } |
| 2284 | |
Vignesh Kulothungan | 019d19b | 2019-01-23 11:09:18 -0800 | [diff] [blame] | 2285 | struct pcm* pcm_open_prepare_helper(unsigned int snd_card, unsigned int pcm_device_id, |
| 2286 | unsigned int flags, unsigned int pcm_open_retry_count, |
| 2287 | struct pcm_config *config) |
| 2288 | { |
| 2289 | struct pcm* pcm = NULL; |
| 2290 | |
| 2291 | while (1) { |
| 2292 | pcm = pcm_open(snd_card, pcm_device_id, flags, config); |
| 2293 | if (pcm == NULL || !pcm_is_ready(pcm)) { |
| 2294 | ALOGE("%s: %s", __func__, pcm_get_error(pcm)); |
| 2295 | if (pcm != NULL) { |
| 2296 | pcm_close(pcm); |
| 2297 | pcm = NULL; |
| 2298 | } |
| 2299 | if (pcm_open_retry_count-- == 0) |
| 2300 | return NULL; |
| 2301 | |
| 2302 | usleep(PROXY_OPEN_WAIT_TIME * 1000); |
| 2303 | continue; |
| 2304 | } |
| 2305 | break; |
| 2306 | } |
| 2307 | |
| 2308 | if (pcm_is_ready(pcm)) { |
| 2309 | int ret = pcm_prepare(pcm); |
| 2310 | if (ret < 0) { |
| 2311 | ALOGE("%s: pcm_prepare returned %d", __func__, ret); |
| 2312 | pcm_close(pcm); |
| 2313 | pcm = NULL; |
| 2314 | } |
| 2315 | } |
| 2316 | |
| 2317 | return pcm; |
| 2318 | } |
| 2319 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2320 | int start_output_stream(struct stream_out *out) |
| 2321 | { |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2322 | int ret = 0; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2323 | struct audio_usecase *uc_info; |
| 2324 | struct audio_device *adev = out->dev; |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 2325 | bool a2dp_combo = false; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2326 | |
Vignesh Kulothungan | 019d19b | 2019-01-23 11:09:18 -0800 | [diff] [blame] | 2327 | ALOGV("%s: enter: usecase(%d: %s) %s devices(%#x)", |
| 2328 | __func__, out->usecase, use_case_table[out->usecase], |
| 2329 | out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS ? "(with haptics)" : "", |
| 2330 | out->devices); |
Haynes Mathew George | c735fb0 | 2016-06-30 18:00:28 -0700 | [diff] [blame] | 2331 | |
| 2332 | if (out->card_status == CARD_STATUS_OFFLINE || |
| 2333 | adev->card_status == CARD_STATUS_OFFLINE) { |
| 2334 | ALOGW("out->card_status or adev->card_status offline, try again"); |
| 2335 | ret = -EAGAIN; |
| 2336 | goto error_config; |
| 2337 | } |
| 2338 | |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 2339 | if (out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) { |
| 2340 | if (!audio_extn_a2dp_is_ready()) { |
juyuchen | 9baad39 | 2018-06-05 19:02:10 +0800 | [diff] [blame] | 2341 | if (out->devices & (AUDIO_DEVICE_OUT_SPEAKER | AUDIO_DEVICE_OUT_SPEAKER_SAFE)) { |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 2342 | a2dp_combo = true; |
| 2343 | } else { |
| 2344 | if (!(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) { |
| 2345 | ALOGE("%s: A2DP profile is not ready, return error", __func__); |
| 2346 | ret = -EAGAIN; |
| 2347 | goto error_config; |
| 2348 | } |
| 2349 | } |
| 2350 | } |
| 2351 | } |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 2352 | 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] | 2353 | if (out->pcm_device_id < 0) { |
| 2354 | ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)", |
| 2355 | __func__, out->pcm_device_id, out->usecase); |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 2356 | ret = -EINVAL; |
| 2357 | goto error_config; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2358 | } |
| 2359 | |
| 2360 | uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase)); |
| 2361 | uc_info->id = out->usecase; |
| 2362 | uc_info->type = PCM_PLAYBACK; |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 2363 | uc_info->stream.out = out; |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 2364 | uc_info->devices = out->devices; |
| 2365 | uc_info->in_snd_device = SND_DEVICE_NONE; |
| 2366 | uc_info->out_snd_device = SND_DEVICE_NONE; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2367 | |
Eric Laurent | 07eeafd | 2013-10-06 12:52:49 -0700 | [diff] [blame] | 2368 | /* This must be called before adding this usecase to the list */ |
| 2369 | if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) |
| 2370 | check_and_set_hdmi_channels(adev, out->config.channels); |
Haynes Mathew George | 65f6b43 | 2018-02-27 17:44:55 -0800 | [diff] [blame] | 2371 | else if (audio_is_usb_out_device(out->devices & AUDIO_DEVICE_OUT_ALL_USB)) { |
| 2372 | check_and_set_usb_service_interval(adev, uc_info, true /*min*/); |
| 2373 | /* USB backend is not reopened immediately. |
| 2374 | This is eventually done as part of select_devices */ |
| 2375 | } |
Eric Laurent | 07eeafd | 2013-10-06 12:52:49 -0700 | [diff] [blame] | 2376 | |
Ravi Kumar Alamanda | 3b1816c | 2013-02-27 23:01:21 -0800 | [diff] [blame] | 2377 | list_add_tail(&adev->usecase_list, &uc_info->list); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2378 | |
Wei Wang | f4837d5 | 2017-11-21 14:51:20 -0800 | [diff] [blame] | 2379 | audio_streaming_hint_start(); |
Ravi Kumar Alamanda | 533bb72 | 2015-09-23 13:47:03 -0700 | [diff] [blame] | 2380 | audio_extn_perf_lock_acquire(); |
| 2381 | |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 2382 | if ((out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) && |
| 2383 | (!audio_extn_a2dp_is_ready())) { |
| 2384 | if (!a2dp_combo) { |
| 2385 | check_a2dp_restore_l(adev, out, false); |
| 2386 | } else { |
| 2387 | audio_devices_t dev = out->devices; |
juyuchen | 9baad39 | 2018-06-05 19:02:10 +0800 | [diff] [blame] | 2388 | if (dev & AUDIO_DEVICE_OUT_SPEAKER_SAFE) |
| 2389 | out->devices = AUDIO_DEVICE_OUT_SPEAKER_SAFE; |
| 2390 | else |
| 2391 | out->devices = AUDIO_DEVICE_OUT_SPEAKER; |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 2392 | select_devices(adev, out->usecase); |
| 2393 | out->devices = dev; |
| 2394 | } |
| 2395 | } else { |
| 2396 | select_devices(adev, out->usecase); |
| 2397 | } |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 2398 | |
Eric Laurent | 0499d4f | 2014-08-25 22:39:29 -0500 | [diff] [blame] | 2399 | audio_extn_extspk_update(adev->extspk); |
| 2400 | |
Arun Mirpuri | 0cc2f31 | 2018-05-29 17:26:07 -0700 | [diff] [blame] | 2401 | if (out->usecase == USECASE_INCALL_MUSIC_UPLINK) |
| 2402 | voice_set_device_mute_flag(adev, true); |
| 2403 | |
Andy Hung | 31aca91 | 2014-03-20 17:14:59 -0700 | [diff] [blame] | 2404 | ALOGV("%s: Opening PCM device card_id(%d) device_id(%d) format(%#x)", |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 2405 | __func__, adev->snd_card, out->pcm_device_id, out->config.format); |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 2406 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
| 2407 | out->pcm = NULL; |
| 2408 | out->compr = compress_open(adev->snd_card, out->pcm_device_id, |
| 2409 | COMPRESS_IN, &out->compr_config); |
| 2410 | if (out->compr && !is_compress_ready(out->compr)) { |
| 2411 | ALOGE("%s: %s", __func__, compress_get_error(out->compr)); |
| 2412 | compress_close(out->compr); |
| 2413 | out->compr = NULL; |
| 2414 | ret = -EIO; |
| 2415 | goto error_open; |
| 2416 | } |
| 2417 | if (out->offload_callback) |
| 2418 | compress_nonblock(out->compr, out->non_blocking); |
| 2419 | |
Aniket Kumar Lata | 90c3646 | 2018-04-06 13:56:37 -0700 | [diff] [blame] | 2420 | if (adev->visualizer_start_output != NULL) { |
| 2421 | int capture_device_id = |
| 2422 | platform_get_pcm_device_id(USECASE_AUDIO_RECORD_AFE_PROXY, |
| 2423 | PCM_CAPTURE); |
| 2424 | adev->visualizer_start_output(out->handle, out->pcm_device_id, |
| 2425 | adev->snd_card, capture_device_id); |
| 2426 | } |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 2427 | if (adev->offload_effects_start_output != NULL) |
| 2428 | adev->offload_effects_start_output(out->handle, out->pcm_device_id); |
| 2429 | } else if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) { |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 2430 | if (out->pcm == NULL || !pcm_is_ready(out->pcm)) { |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 2431 | ALOGE("%s: pcm stream not ready", __func__); |
| 2432 | goto error_open; |
| 2433 | } |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 2434 | ret = pcm_start(out->pcm); |
| 2435 | if (ret < 0) { |
| 2436 | ALOGE("%s: MMAP pcm_start failed ret %d", __func__, ret); |
| 2437 | goto error_open; |
| 2438 | } |
| 2439 | } else { |
Mikhail Naganov | 635e743 | 2017-06-02 13:51:00 -0700 | [diff] [blame] | 2440 | unsigned int flags = PCM_OUT | PCM_MONOTONIC; |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 2441 | unsigned int pcm_open_retry_count = 0; |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 2442 | |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 2443 | if (out->usecase == USECASE_AUDIO_PLAYBACK_AFE_PROXY) { |
| 2444 | flags |= PCM_MMAP | PCM_NOIRQ; |
| 2445 | pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT; |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 2446 | } else if (out->realtime) { |
| 2447 | flags |= PCM_MMAP | PCM_NOIRQ; |
Mikhail Naganov | 635e743 | 2017-06-02 13:51:00 -0700 | [diff] [blame] | 2448 | } |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 2449 | |
Vignesh Kulothungan | 019d19b | 2019-01-23 11:09:18 -0800 | [diff] [blame] | 2450 | out->pcm = pcm_open_prepare_helper(adev->snd_card, out->pcm_device_id, |
| 2451 | flags, pcm_open_retry_count, |
| 2452 | &(out->config)); |
| 2453 | if (out->pcm == NULL) { |
| 2454 | ret = -EIO; |
| 2455 | goto error_open; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2456 | } |
Vignesh Kulothungan | 019d19b | 2019-01-23 11:09:18 -0800 | [diff] [blame] | 2457 | |
| 2458 | if (out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) { |
| 2459 | if (adev->haptic_pcm != NULL) { |
| 2460 | pcm_close(adev->haptic_pcm); |
| 2461 | adev->haptic_pcm = NULL; |
Ravi Kumar Alamanda | 50919a7 | 2015-10-02 09:37:33 -0700 | [diff] [blame] | 2462 | } |
Vignesh Kulothungan | 019d19b | 2019-01-23 11:09:18 -0800 | [diff] [blame] | 2463 | adev->haptic_pcm = pcm_open_prepare_helper(adev->snd_card, |
| 2464 | adev->haptic_pcm_device_id, |
| 2465 | flags, pcm_open_retry_count, |
| 2466 | &(adev->haptics_config)); |
| 2467 | // failure to open haptics pcm shouldnt stop audio, |
| 2468 | // so do not close audio pcm in case of error |
Ravi Kumar Alamanda | 50919a7 | 2015-10-02 09:37:33 -0700 | [diff] [blame] | 2469 | } |
Vignesh Kulothungan | 019d19b | 2019-01-23 11:09:18 -0800 | [diff] [blame] | 2470 | |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 2471 | if (out->realtime) { |
| 2472 | ret = pcm_start(out->pcm); |
| 2473 | if (ret < 0) { |
| 2474 | ALOGE("%s: RT pcm_start failed ret %d", __func__, ret); |
| 2475 | pcm_close(out->pcm); |
| 2476 | out->pcm = NULL; |
| 2477 | goto error_open; |
| 2478 | } |
Haynes Mathew George | fbe8731 | 2016-08-01 19:29:27 -0700 | [diff] [blame] | 2479 | } |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 2480 | } |
Vignesh Kulothungan | 019d19b | 2019-01-23 11:09:18 -0800 | [diff] [blame] | 2481 | |
Haynes Mathew George | fbe8731 | 2016-08-01 19:29:27 -0700 | [diff] [blame] | 2482 | register_out_stream(out); |
Wei Wang | f4837d5 | 2017-11-21 14:51:20 -0800 | [diff] [blame] | 2483 | audio_streaming_hint_end(); |
Ravi Kumar Alamanda | 533bb72 | 2015-09-23 13:47:03 -0700 | [diff] [blame] | 2484 | audio_extn_perf_lock_release(); |
Alain Vongsouvanh | 13f26e8 | 2016-11-18 14:39:11 -0800 | [diff] [blame] | 2485 | audio_extn_tfa_98xx_enable_speaker(); |
vivek mehta | e59cfb2 | 2017-06-16 15:57:11 -0700 | [diff] [blame] | 2486 | |
Haynes Mathew George | d9ca796 | 2017-12-01 16:29:56 -0800 | [diff] [blame] | 2487 | if (out->usecase == USECASE_AUDIO_PLAYBACK_ULL || |
| 2488 | out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) { |
| 2489 | audio_low_latency_hint_start(); |
| 2490 | } |
| 2491 | |
vivek mehta | e59cfb2 | 2017-06-16 15:57:11 -0700 | [diff] [blame] | 2492 | // consider a scenario where on pause lower layers are tear down. |
| 2493 | // so on resume, swap mixer control need to be sent only when |
| 2494 | // backend is active, hence rather than sending from enable device |
Vignesh Kulothungan | 019d19b | 2019-01-23 11:09:18 -0800 | [diff] [blame] | 2495 | // sending it from start of stream |
vivek mehta | e59cfb2 | 2017-06-16 15:57:11 -0700 | [diff] [blame] | 2496 | |
| 2497 | platform_set_swap_channels(adev, true); |
| 2498 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 2499 | ALOGV("%s: exit", __func__); |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 2500 | return 0; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2501 | error_open: |
Vignesh Kulothungan | 019d19b | 2019-01-23 11:09:18 -0800 | [diff] [blame] | 2502 | if (adev->haptic_pcm) { |
| 2503 | pcm_close(adev->haptic_pcm); |
| 2504 | adev->haptic_pcm = NULL; |
| 2505 | } |
Wei Wang | f4837d5 | 2017-11-21 14:51:20 -0800 | [diff] [blame] | 2506 | audio_streaming_hint_end(); |
Ravi Kumar Alamanda | 533bb72 | 2015-09-23 13:47:03 -0700 | [diff] [blame] | 2507 | audio_extn_perf_lock_release(); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2508 | stop_output_stream(out); |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 2509 | error_config: |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 2510 | return ret; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2511 | } |
| 2512 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2513 | static int check_input_parameters(uint32_t sample_rate, |
| 2514 | audio_format_t format, |
Eric Laurent | 74b5576 | 2017-07-09 17:04:53 -0700 | [diff] [blame] | 2515 | int channel_count, bool is_usb_hifi) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2516 | { |
Haynes Mathew George | e95340e | 2017-05-24 15:42:06 -0700 | [diff] [blame] | 2517 | if ((format != AUDIO_FORMAT_PCM_16_BIT) && |
| 2518 | (format != AUDIO_FORMAT_PCM_8_24_BIT) && |
Eric Laurent | 74b5576 | 2017-07-09 17:04:53 -0700 | [diff] [blame] | 2519 | (format != AUDIO_FORMAT_PCM_24_BIT_PACKED) && |
| 2520 | !(is_usb_hifi && (format == AUDIO_FORMAT_PCM_32_BIT))) { |
vivek mehta | dae4471 | 2015-07-27 14:13:18 -0700 | [diff] [blame] | 2521 | ALOGE("%s: unsupported AUDIO FORMAT (%d) ", __func__, format); |
| 2522 | return -EINVAL; |
| 2523 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2524 | |
Eric Laurent | 74b5576 | 2017-07-09 17:04:53 -0700 | [diff] [blame] | 2525 | int max_channel_count = is_usb_hifi ? MAX_HIFI_CHANNEL_COUNT : MAX_CHANNEL_COUNT; |
| 2526 | if ((channel_count < MIN_CHANNEL_COUNT) || (channel_count > max_channel_count)) { |
Jean-Michel Trivi | c075069 | 2015-10-12 12:12:32 -0700 | [diff] [blame] | 2527 | ALOGE("%s: unsupported channel count (%d) passed Min / Max (%d / %d)", __func__, |
Eric Laurent | 74b5576 | 2017-07-09 17:04:53 -0700 | [diff] [blame] | 2528 | channel_count, MIN_CHANNEL_COUNT, max_channel_count); |
vivek mehta | dae4471 | 2015-07-27 14:13:18 -0700 | [diff] [blame] | 2529 | return -EINVAL; |
| 2530 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2531 | |
| 2532 | switch (sample_rate) { |
| 2533 | case 8000: |
| 2534 | case 11025: |
| 2535 | case 12000: |
| 2536 | case 16000: |
| 2537 | case 22050: |
| 2538 | case 24000: |
| 2539 | case 32000: |
| 2540 | case 44100: |
| 2541 | case 48000: |
Haynes Mathew George | e95340e | 2017-05-24 15:42:06 -0700 | [diff] [blame] | 2542 | case 96000: |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2543 | break; |
| 2544 | default: |
vivek mehta | dae4471 | 2015-07-27 14:13:18 -0700 | [diff] [blame] | 2545 | ALOGE("%s: unsupported (%d) samplerate passed ", __func__, sample_rate); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2546 | return -EINVAL; |
| 2547 | } |
| 2548 | |
| 2549 | return 0; |
| 2550 | } |
| 2551 | |
Kevin Rocard | a325aa2 | 2018-04-03 09:15:52 -0700 | [diff] [blame] | 2552 | /** Add a value in a list if not already present. |
| 2553 | * @return true if value was successfully inserted or already present, |
| 2554 | * false if the list is full and does not contain the value. |
| 2555 | */ |
| 2556 | static bool register_uint(uint32_t value, uint32_t* list, size_t list_length) { |
| 2557 | for (size_t i = 0; i < list_length; i++) { |
| 2558 | if (list[i] == value) return true; // value is already present |
| 2559 | if (list[i] == 0) { // no values in this slot |
| 2560 | list[i] = value; |
| 2561 | return true; // value inserted |
| 2562 | } |
| 2563 | } |
| 2564 | return false; // could not insert value |
| 2565 | } |
| 2566 | |
| 2567 | /** Add channel_mask in supported_channel_masks if not already present. |
| 2568 | * @return true if channel_mask was successfully inserted or already present, |
| 2569 | * false if supported_channel_masks is full and does not contain channel_mask. |
| 2570 | */ |
| 2571 | static void register_channel_mask(audio_channel_mask_t channel_mask, |
| 2572 | audio_channel_mask_t supported_channel_masks[static MAX_SUPPORTED_CHANNEL_MASKS]) { |
| 2573 | ALOGE_IF(!register_uint(channel_mask, supported_channel_masks, MAX_SUPPORTED_CHANNEL_MASKS), |
| 2574 | "%s: stream can not declare supporting its channel_mask %x", __func__, channel_mask); |
| 2575 | } |
| 2576 | |
| 2577 | /** Add format in supported_formats if not already present. |
| 2578 | * @return true if format was successfully inserted or already present, |
| 2579 | * false if supported_formats is full and does not contain format. |
| 2580 | */ |
| 2581 | static void register_format(audio_format_t format, |
| 2582 | audio_format_t supported_formats[static MAX_SUPPORTED_FORMATS]) { |
| 2583 | ALOGE_IF(!register_uint(format, supported_formats, MAX_SUPPORTED_FORMATS), |
| 2584 | "%s: stream can not declare supporting its format %x", __func__, format); |
| 2585 | } |
| 2586 | /** Add sample_rate in supported_sample_rates if not already present. |
| 2587 | * @return true if sample_rate was successfully inserted or already present, |
| 2588 | * false if supported_sample_rates is full and does not contain sample_rate. |
| 2589 | */ |
| 2590 | static void register_sample_rate(uint32_t sample_rate, |
| 2591 | uint32_t supported_sample_rates[static MAX_SUPPORTED_SAMPLE_RATES]) { |
| 2592 | ALOGE_IF(!register_uint(sample_rate, supported_sample_rates, MAX_SUPPORTED_SAMPLE_RATES), |
| 2593 | "%s: stream can not declare supporting its sample rate %x", __func__, sample_rate); |
| 2594 | } |
| 2595 | |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 2596 | static size_t get_stream_buffer_size(size_t duration_ms, |
| 2597 | uint32_t sample_rate, |
| 2598 | audio_format_t format, |
| 2599 | int channel_count, |
| 2600 | bool is_low_latency) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2601 | { |
| 2602 | size_t size = 0; |
| 2603 | |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 2604 | size = (sample_rate * duration_ms) / 1000; |
Glenn Kasten | 68e79ce | 2014-07-15 10:56:59 -0700 | [diff] [blame] | 2605 | if (is_low_latency) |
Glenn Kasten | 4f99339 | 2014-05-14 07:30:48 -0700 | [diff] [blame] | 2606 | size = configured_low_latency_capture_period_size; |
vivek mehta | 4ed66e6 | 2016-04-15 23:33:34 -0700 | [diff] [blame] | 2607 | |
| 2608 | size *= channel_count * audio_bytes_per_sample(format); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2609 | |
Glenn Kasten | 4f99339 | 2014-05-14 07:30:48 -0700 | [diff] [blame] | 2610 | /* make sure the size is multiple of 32 bytes |
| 2611 | * At 48 kHz mono 16-bit PCM: |
| 2612 | * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15) |
| 2613 | * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10) |
| 2614 | */ |
| 2615 | size += 0x1f; |
| 2616 | size &= ~0x1f; |
Ravi Kumar Alamanda | 33d3306 | 2013-06-11 14:40:01 -0700 | [diff] [blame] | 2617 | |
| 2618 | return size; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2619 | } |
| 2620 | |
| 2621 | static uint32_t out_get_sample_rate(const struct audio_stream *stream) |
| 2622 | { |
| 2623 | struct stream_out *out = (struct stream_out *)stream; |
| 2624 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2625 | return out->sample_rate; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2626 | } |
| 2627 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 2628 | static int out_set_sample_rate(struct audio_stream *stream __unused, uint32_t rate __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2629 | { |
| 2630 | return -ENOSYS; |
| 2631 | } |
| 2632 | |
| 2633 | static size_t out_get_buffer_size(const struct audio_stream *stream) |
| 2634 | { |
| 2635 | struct stream_out *out = (struct stream_out *)stream; |
| 2636 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2637 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
| 2638 | return out->compr_config.fragment_size; |
| 2639 | } |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 2640 | return out->config.period_size * out->af_period_multiplier * |
Eric Laurent | fdf296a | 2014-07-03 16:41:51 -0700 | [diff] [blame] | 2641 | audio_stream_out_frame_size((const struct audio_stream_out *)stream); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2642 | } |
| 2643 | |
| 2644 | static uint32_t out_get_channels(const struct audio_stream *stream) |
| 2645 | { |
| 2646 | struct stream_out *out = (struct stream_out *)stream; |
| 2647 | |
| 2648 | return out->channel_mask; |
| 2649 | } |
| 2650 | |
| 2651 | static audio_format_t out_get_format(const struct audio_stream *stream) |
| 2652 | { |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2653 | struct stream_out *out = (struct stream_out *)stream; |
| 2654 | |
| 2655 | return out->format; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2656 | } |
| 2657 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 2658 | static int out_set_format(struct audio_stream *stream __unused, audio_format_t format __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2659 | { |
| 2660 | return -ENOSYS; |
| 2661 | } |
| 2662 | |
Kevin Rocard | 2ba1a47 | 2017-08-10 20:26:09 -0700 | [diff] [blame] | 2663 | /* must be called with out->lock locked */ |
| 2664 | static int out_standby_l(struct audio_stream *stream) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2665 | { |
| 2666 | struct stream_out *out = (struct stream_out *)stream; |
| 2667 | struct audio_device *adev = out->dev; |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 2668 | bool do_stop = true; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2669 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2670 | if (!out->standby) { |
Haynes Mathew George | 88e6fb2 | 2015-08-19 11:51:34 -0700 | [diff] [blame] | 2671 | if (adev->adm_deregister_stream) |
| 2672 | adev->adm_deregister_stream(adev->adm_data, out->handle); |
Ravi Kumar Alamanda | 8bba9e9 | 2013-11-11 21:09:07 -0800 | [diff] [blame] | 2673 | pthread_mutex_lock(&adev->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2674 | out->standby = true; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2675 | if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
| 2676 | if (out->pcm) { |
| 2677 | pcm_close(out->pcm); |
| 2678 | out->pcm = NULL; |
Vignesh Kulothungan | 019d19b | 2019-01-23 11:09:18 -0800 | [diff] [blame] | 2679 | |
| 2680 | if (out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) { |
| 2681 | if (adev->haptic_pcm) { |
| 2682 | pcm_close(adev->haptic_pcm); |
| 2683 | adev->haptic_pcm = NULL; |
| 2684 | } |
| 2685 | |
| 2686 | if (adev->haptic_buffer != NULL) { |
| 2687 | free(adev->haptic_buffer); |
| 2688 | adev->haptic_buffer = NULL; |
| 2689 | adev->haptic_buffer_size = 0; |
| 2690 | } |
| 2691 | } |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2692 | } |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 2693 | if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) { |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 2694 | do_stop = out->playback_started; |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 2695 | out->playback_started = false; |
| 2696 | } |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2697 | } else { |
| 2698 | stop_compressed_output_l(out); |
Haynes Mathew George | 352f27b | 2013-07-26 00:00:15 -0700 | [diff] [blame] | 2699 | out->gapless_mdata.encoder_delay = 0; |
| 2700 | out->gapless_mdata.encoder_padding = 0; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2701 | if (out->compr != NULL) { |
| 2702 | compress_close(out->compr); |
| 2703 | out->compr = NULL; |
| 2704 | } |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 2705 | } |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 2706 | if (do_stop) { |
| 2707 | stop_output_stream(out); |
| 2708 | } |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 2709 | pthread_mutex_unlock(&adev->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2710 | } |
Kevin Rocard | 2ba1a47 | 2017-08-10 20:26:09 -0700 | [diff] [blame] | 2711 | return 0; |
| 2712 | } |
| 2713 | |
| 2714 | static int out_standby(struct audio_stream *stream) |
| 2715 | { |
| 2716 | struct stream_out *out = (struct stream_out *)stream; |
| 2717 | |
| 2718 | ALOGV("%s: enter: usecase(%d: %s)", __func__, |
| 2719 | out->usecase, use_case_table[out->usecase]); |
| 2720 | |
| 2721 | lock_output_stream(out); |
| 2722 | out_standby_l(stream); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2723 | pthread_mutex_unlock(&out->lock); |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 2724 | ALOGV("%s: exit", __func__); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2725 | return 0; |
| 2726 | } |
| 2727 | |
Haynes Mathew George | 3ddd3bd | 2016-07-07 20:01:53 -0700 | [diff] [blame] | 2728 | static int out_on_error(struct audio_stream *stream) |
| 2729 | { |
| 2730 | struct stream_out *out = (struct stream_out *)stream; |
| 2731 | struct audio_device *adev = out->dev; |
| 2732 | bool do_standby = false; |
| 2733 | |
| 2734 | lock_output_stream(out); |
| 2735 | if (!out->standby) { |
| 2736 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
| 2737 | stop_compressed_output_l(out); |
| 2738 | send_offload_cmd_l(out, OFFLOAD_CMD_ERROR); |
| 2739 | } else |
| 2740 | do_standby = true; |
| 2741 | } |
| 2742 | pthread_mutex_unlock(&out->lock); |
| 2743 | |
| 2744 | if (do_standby) |
| 2745 | return out_standby(&out->stream.common); |
| 2746 | |
| 2747 | return 0; |
| 2748 | } |
| 2749 | |
Andy Hung | 7401c7c | 2016-09-21 12:41:21 -0700 | [diff] [blame] | 2750 | static int out_dump(const struct audio_stream *stream, int fd) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2751 | { |
Andy Hung | 7401c7c | 2016-09-21 12:41:21 -0700 | [diff] [blame] | 2752 | struct stream_out *out = (struct stream_out *)stream; |
| 2753 | |
| 2754 | // We try to get the lock for consistency, |
| 2755 | // but it isn't necessary for these variables. |
| 2756 | // If we're not in standby, we may be blocked on a write. |
| 2757 | const bool locked = (pthread_mutex_trylock(&out->lock) == 0); |
| 2758 | dprintf(fd, " Standby: %s\n", out->standby ? "yes" : "no"); |
| 2759 | dprintf(fd, " Frames written: %lld\n", (long long)out->written); |
| 2760 | |
Andy Hung | 572633e | 2019-02-19 11:58:24 -0800 | [diff] [blame] | 2761 | char buffer[256]; // for statistics formatting |
| 2762 | simple_stats_to_string(&out->fifo_underruns, buffer, sizeof(buffer)); |
| 2763 | dprintf(fd, " Fifo frame underruns: %s\n", buffer); |
| 2764 | |
Andy Hung | 241e36f | 2019-02-19 12:00:38 -0800 | [diff] [blame] | 2765 | if (out->start_latency_ms.n > 0) { |
| 2766 | simple_stats_to_string(&out->start_latency_ms, buffer, sizeof(buffer)); |
| 2767 | dprintf(fd, " Start latency ms: %s\n", buffer); |
| 2768 | } |
| 2769 | |
Andy Hung | 7401c7c | 2016-09-21 12:41:21 -0700 | [diff] [blame] | 2770 | if (locked) { |
Andy Hung | 7401c7c | 2016-09-21 12:41:21 -0700 | [diff] [blame] | 2771 | pthread_mutex_unlock(&out->lock); |
Andy Hung | 7401c7c | 2016-09-21 12:41:21 -0700 | [diff] [blame] | 2772 | } |
Andy Hung | a452b0a | 2017-03-15 14:51:15 -0700 | [diff] [blame] | 2773 | |
| 2774 | // dump error info |
| 2775 | (void)error_log_dump( |
| 2776 | out->error_log, fd, " " /* prefix */, 0 /* lines */, 0 /* limit_ns */); |
Andy Hung | 3ba2c92 | 2017-06-15 17:48:35 -0700 | [diff] [blame] | 2777 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2778 | return 0; |
| 2779 | } |
| 2780 | |
Haynes Mathew George | 352f27b | 2013-07-26 00:00:15 -0700 | [diff] [blame] | 2781 | static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms) |
| 2782 | { |
| 2783 | int ret = 0; |
| 2784 | char value[32]; |
| 2785 | struct compr_gapless_mdata tmp_mdata; |
| 2786 | |
| 2787 | if (!out || !parms) { |
| 2788 | return -EINVAL; |
| 2789 | } |
| 2790 | |
| 2791 | ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value)); |
| 2792 | if (ret >= 0) { |
| 2793 | tmp_mdata.encoder_delay = atoi(value); //whats a good limit check? |
| 2794 | } else { |
| 2795 | return -EINVAL; |
| 2796 | } |
| 2797 | |
| 2798 | ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value)); |
| 2799 | if (ret >= 0) { |
| 2800 | tmp_mdata.encoder_padding = atoi(value); |
| 2801 | } else { |
| 2802 | return -EINVAL; |
| 2803 | } |
| 2804 | |
| 2805 | out->gapless_mdata = tmp_mdata; |
| 2806 | out->send_new_metadata = 1; |
| 2807 | ALOGV("%s new encoder delay %u and padding %u", __func__, |
| 2808 | out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding); |
| 2809 | |
| 2810 | return 0; |
| 2811 | } |
| 2812 | |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 2813 | static bool output_drives_call(struct audio_device *adev, struct stream_out *out) |
| 2814 | { |
| 2815 | return out == adev->primary_output || out == adev->voice_tx_output; |
| 2816 | } |
Haynes Mathew George | 352f27b | 2013-07-26 00:00:15 -0700 | [diff] [blame] | 2817 | |
Kevin Rocard | 1e02c88 | 2017-08-09 15:26:07 -0700 | [diff] [blame] | 2818 | static int get_alive_usb_card(struct str_parms* parms) { |
| 2819 | int card; |
| 2820 | if ((str_parms_get_int(parms, "card", &card) >= 0) && |
| 2821 | !audio_extn_usb_alive(card)) { |
| 2822 | return card; |
| 2823 | } |
| 2824 | return -ENODEV; |
| 2825 | } |
| 2826 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2827 | static int out_set_parameters(struct audio_stream *stream, const char *kvpairs) |
| 2828 | { |
| 2829 | struct stream_out *out = (struct stream_out *)stream; |
| 2830 | struct audio_device *adev = out->dev; |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 2831 | struct audio_usecase *usecase; |
| 2832 | struct listnode *node; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2833 | struct str_parms *parms; |
| 2834 | char value[32]; |
| 2835 | int ret, val = 0; |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 2836 | bool select_new_device = false; |
Eric Laurent | 03f0943 | 2014-03-25 18:09:11 -0700 | [diff] [blame] | 2837 | int status = 0; |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 2838 | bool bypass_a2dp = false; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2839 | |
Eric Laurent | 2e140aa | 2016-06-30 17:14:46 -0700 | [diff] [blame] | 2840 | ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s", |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 2841 | __func__, out->usecase, use_case_table[out->usecase], kvpairs); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2842 | parms = str_parms_create_str(kvpairs); |
| 2843 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value)); |
| 2844 | if (ret >= 0) { |
| 2845 | val = atoi(value); |
Kevin Rocard | 2ba1a47 | 2017-08-10 20:26:09 -0700 | [diff] [blame] | 2846 | |
Eric Laurent | a147807 | 2015-09-21 17:21:52 -0700 | [diff] [blame] | 2847 | lock_output_stream(out); |
Kevin Rocard | 2ba1a47 | 2017-08-10 20:26:09 -0700 | [diff] [blame] | 2848 | |
| 2849 | // The usb driver needs to be closed after usb device disconnection |
| 2850 | // otherwise audio is no longer played on the new usb devices. |
| 2851 | // By forcing the stream in standby, the usb stack refcount drops to 0 |
| 2852 | // and the driver is closed. |
Aniket Kumar Lata | ad4dfc3 | 2018-07-10 16:04:59 -0700 | [diff] [blame] | 2853 | if (val == AUDIO_DEVICE_NONE && |
Kevin Rocard | 2ba1a47 | 2017-08-10 20:26:09 -0700 | [diff] [blame] | 2854 | audio_is_usb_out_device(out->devices)) { |
Aniket Kumar Lata | ad4dfc3 | 2018-07-10 16:04:59 -0700 | [diff] [blame] | 2855 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
| 2856 | ALOGD("%s() putting the usb device in standby after disconnection", __func__); |
| 2857 | out_standby_l(&out->stream.common); |
| 2858 | } |
| 2859 | val = AUDIO_DEVICE_OUT_SPEAKER; |
Kevin Rocard | 2ba1a47 | 2017-08-10 20:26:09 -0700 | [diff] [blame] | 2860 | } |
| 2861 | |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 2862 | pthread_mutex_lock(&adev->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2863 | |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 2864 | /* |
| 2865 | * When HDMI cable is unplugged the music playback is paused and |
| 2866 | * the policy manager sends routing=0. But the audioflinger |
| 2867 | * continues to write data until standby time (3sec). |
| 2868 | * As the HDMI core is turned off, the write gets blocked. |
| 2869 | * Avoid this by routing audio to speaker until standby. |
| 2870 | */ |
| 2871 | if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL && |
| 2872 | val == AUDIO_DEVICE_NONE) { |
| 2873 | val = AUDIO_DEVICE_OUT_SPEAKER; |
| 2874 | } |
| 2875 | |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 2876 | /* |
| 2877 | * When A2DP is disconnected the |
| 2878 | * music playback is paused and the policy manager sends routing=0 |
| 2879 | * But the audioflingercontinues to write data until standby time |
| 2880 | * (3sec). As BT is turned off, the write gets blocked. |
| 2881 | * Avoid this by routing audio to speaker until standby. |
| 2882 | */ |
| 2883 | if ((out->devices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP) && |
| 2884 | (val == AUDIO_DEVICE_NONE) && |
| 2885 | !audio_extn_a2dp_is_ready()) { |
| 2886 | val = AUDIO_DEVICE_OUT_SPEAKER; |
| 2887 | } |
| 2888 | |
| 2889 | /* To avoid a2dp to sco overlapping / BT device improper state |
| 2890 | * check with BT lib about a2dp streaming support before routing |
| 2891 | */ |
| 2892 | if (val & AUDIO_DEVICE_OUT_ALL_A2DP) { |
| 2893 | if (!audio_extn_a2dp_is_ready()) { |
juyuchen | 9baad39 | 2018-06-05 19:02:10 +0800 | [diff] [blame] | 2894 | if (val & (AUDIO_DEVICE_OUT_SPEAKER | AUDIO_DEVICE_OUT_SPEAKER_SAFE)) { |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 2895 | //combo usecase just by pass a2dp |
| 2896 | ALOGW("%s: A2DP profile is not ready,routing to speaker only", __func__); |
| 2897 | bypass_a2dp = true; |
| 2898 | } else { |
| 2899 | ALOGE("%s: A2DP profile is not ready,ignoring routing request", __func__); |
| 2900 | /* update device to a2dp and don't route as BT returned error |
| 2901 | * However it is still possible a2dp routing called because |
| 2902 | * of current active device disconnection (like wired headset) |
| 2903 | */ |
| 2904 | out->devices = val; |
| 2905 | pthread_mutex_unlock(&out->lock); |
| 2906 | pthread_mutex_unlock(&adev->lock); |
| 2907 | status = -ENOSYS; |
| 2908 | goto routing_fail; |
| 2909 | } |
| 2910 | } |
| 2911 | } |
| 2912 | |
Kevin Rocard | f9f241e | 2017-08-07 19:21:36 -0700 | [diff] [blame] | 2913 | audio_devices_t new_dev = val; |
| 2914 | |
| 2915 | // Workaround: If routing to an non existing usb device, fail gracefully |
| 2916 | // The routing request will otherwise block during 10 second |
Kevin Rocard | 1e02c88 | 2017-08-09 15:26:07 -0700 | [diff] [blame] | 2917 | int card; |
| 2918 | if (audio_is_usb_out_device(new_dev) && |
| 2919 | (card = get_alive_usb_card(parms)) >= 0) { |
| 2920 | |
| 2921 | ALOGW("out_set_parameters() ignoring rerouting to non existing USB card %d", card); |
Kevin Rocard | f9f241e | 2017-08-07 19:21:36 -0700 | [diff] [blame] | 2922 | pthread_mutex_unlock(&adev->lock); |
| 2923 | pthread_mutex_unlock(&out->lock); |
| 2924 | status = -ENOSYS; |
| 2925 | goto routing_fail; |
| 2926 | } |
| 2927 | |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 2928 | /* |
| 2929 | * select_devices() call below switches all the usecases on the same |
Ravi Kumar Alamanda | b7ea4f5 | 2015-06-08 16:44:05 -0700 | [diff] [blame] | 2930 | * backend to the new device. Refer to check_and_route_playback_usecases() in |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 2931 | * the select_devices(). But how do we undo this? |
| 2932 | * |
| 2933 | * For example, music playback is active on headset (deep-buffer usecase) |
| 2934 | * and if we go to ringtones and select a ringtone, low-latency usecase |
| 2935 | * will be started on headset+speaker. As we can't enable headset+speaker |
| 2936 | * and headset devices at the same time, select_devices() switches the music |
| 2937 | * playback to headset+speaker while starting low-lateny usecase for ringtone. |
| 2938 | * So when the ringtone playback is completed, how do we undo the same? |
| 2939 | * |
| 2940 | * We are relying on the out_set_parameters() call on deep-buffer output, |
| 2941 | * once the ringtone playback is ended. |
| 2942 | * NOTE: We should not check if the current devices are same as new devices. |
| 2943 | * Because select_devices() must be called to switch back the music |
| 2944 | * playback to headset. |
| 2945 | */ |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 2946 | if (new_dev != AUDIO_DEVICE_NONE) { |
| 2947 | bool same_dev = out->devices == new_dev; |
| 2948 | out->devices = new_dev; |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 2949 | |
Eric Laurent | a765719 | 2014-10-09 21:09:33 -0700 | [diff] [blame] | 2950 | if (output_drives_call(adev, out)) { |
Vignesh Kulothungan | a5c9da1 | 2017-11-10 11:41:24 -0800 | [diff] [blame] | 2951 | if (!voice_is_call_state_active(adev)) { |
Eric Laurent | a765719 | 2014-10-09 21:09:33 -0700 | [diff] [blame] | 2952 | if (adev->mode == AUDIO_MODE_IN_CALL) { |
| 2953 | adev->current_call_output = out; |
| 2954 | ret = voice_start_call(adev); |
| 2955 | } |
| 2956 | } else { |
| 2957 | adev->current_call_output = out; |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 2958 | voice_update_devices_for_all_voice_usecases(adev); |
Eric Laurent | a765719 | 2014-10-09 21:09:33 -0700 | [diff] [blame] | 2959 | } |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 2960 | } |
vivek mehta | 0d3637a | 2016-07-24 09:32:02 -0700 | [diff] [blame] | 2961 | |
| 2962 | if (!out->standby) { |
| 2963 | if (!same_dev) { |
| 2964 | ALOGV("update routing change"); |
Haynes Mathew George | 104fdfd | 2016-07-01 16:57:24 -0700 | [diff] [blame] | 2965 | // inform adm before actual routing to prevent glitches. |
| 2966 | if (adev->adm_on_routing_change) { |
| 2967 | adev->adm_on_routing_change(adev->adm_data, |
| 2968 | out->handle); |
| 2969 | } |
vivek mehta | 0d3637a | 2016-07-24 09:32:02 -0700 | [diff] [blame] | 2970 | } |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 2971 | if (!bypass_a2dp) { |
| 2972 | select_devices(adev, out->usecase); |
| 2973 | } else { |
juyuchen | 9baad39 | 2018-06-05 19:02:10 +0800 | [diff] [blame] | 2974 | if (new_dev & AUDIO_DEVICE_OUT_SPEAKER_SAFE) |
| 2975 | out->devices = AUDIO_DEVICE_OUT_SPEAKER_SAFE; |
| 2976 | else |
| 2977 | out->devices = AUDIO_DEVICE_OUT_SPEAKER; |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 2978 | select_devices(adev, out->usecase); |
| 2979 | out->devices = new_dev; |
| 2980 | } |
Alain Vongsouvanh | 13f26e8 | 2016-11-18 14:39:11 -0800 | [diff] [blame] | 2981 | audio_extn_tfa_98xx_update(); |
vivek mehta | e59cfb2 | 2017-06-16 15:57:11 -0700 | [diff] [blame] | 2982 | |
| 2983 | // on device switch force swap, lower functions will make sure |
| 2984 | // to check if swap is allowed or not. |
| 2985 | |
| 2986 | if (!same_dev) |
| 2987 | platform_set_swap_channels(adev, true); |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 2988 | |
| 2989 | if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && |
| 2990 | out->a2dp_compress_mute && |
| 2991 | (!(out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) || audio_extn_a2dp_is_ready())) { |
| 2992 | pthread_mutex_lock(&out->compr_mute_lock); |
| 2993 | out->a2dp_compress_mute = false; |
| 2994 | set_compr_volume(&out->stream, out->volume_l, out->volume_r); |
| 2995 | pthread_mutex_unlock(&out->compr_mute_lock); |
| 2996 | } |
vivek mehta | 0d3637a | 2016-07-24 09:32:02 -0700 | [diff] [blame] | 2997 | } |
| 2998 | |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 2999 | } |
| 3000 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3001 | pthread_mutex_unlock(&adev->lock); |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 3002 | pthread_mutex_unlock(&out->lock); |
Eric Laurent | 0499d4f | 2014-08-25 22:39:29 -0500 | [diff] [blame] | 3003 | |
| 3004 | /*handles device and call state changes*/ |
| 3005 | audio_extn_extspk_update(adev->extspk); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3006 | } |
Kevin Rocard | f9f241e | 2017-08-07 19:21:36 -0700 | [diff] [blame] | 3007 | routing_fail: |
Haynes Mathew George | 352f27b | 2013-07-26 00:00:15 -0700 | [diff] [blame] | 3008 | |
| 3009 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
| 3010 | parse_compress_metadata(out, parms); |
| 3011 | } |
| 3012 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3013 | str_parms_destroy(parms); |
Eric Laurent | 03f0943 | 2014-03-25 18:09:11 -0700 | [diff] [blame] | 3014 | ALOGV("%s: exit: code(%d)", __func__, status); |
| 3015 | return status; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3016 | } |
| 3017 | |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 3018 | static bool stream_get_parameter_channels(struct str_parms *query, |
| 3019 | struct str_parms *reply, |
| 3020 | audio_channel_mask_t *supported_channel_masks) { |
| 3021 | int ret = -1; |
Andy Hung | 64fdb45 | 2018-09-07 11:25:33 -0700 | [diff] [blame] | 3022 | char value[ARRAY_SIZE(channels_name_to_enum_table) * 32 /* max channel name size */]; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3023 | bool first = true; |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 3024 | size_t i, j; |
| 3025 | |
| 3026 | if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) { |
| 3027 | ret = 0; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3028 | value[0] = '\0'; |
| 3029 | i = 0; |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 3030 | while (supported_channel_masks[i] != 0) { |
| 3031 | for (j = 0; j < ARRAY_SIZE(channels_name_to_enum_table); j++) { |
| 3032 | if (channels_name_to_enum_table[j].value == supported_channel_masks[i]) { |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3033 | if (!first) { |
| 3034 | strcat(value, "|"); |
| 3035 | } |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 3036 | strcat(value, channels_name_to_enum_table[j].name); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3037 | first = false; |
| 3038 | break; |
| 3039 | } |
| 3040 | } |
| 3041 | i++; |
| 3042 | } |
| 3043 | str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value); |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 3044 | } |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 3045 | return ret >= 0; |
| 3046 | } |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 3047 | |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 3048 | static bool stream_get_parameter_formats(struct str_parms *query, |
| 3049 | struct str_parms *reply, |
| 3050 | audio_format_t *supported_formats) { |
| 3051 | int ret = -1; |
| 3052 | char value[256]; |
| 3053 | int i; |
| 3054 | |
| 3055 | if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) { |
| 3056 | ret = 0; |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 3057 | value[0] = '\0'; |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 3058 | switch (supported_formats[0]) { |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 3059 | case AUDIO_FORMAT_PCM_16_BIT: |
| 3060 | strcat(value, "AUDIO_FORMAT_PCM_16_BIT"); |
| 3061 | break; |
| 3062 | case AUDIO_FORMAT_PCM_24_BIT_PACKED: |
| 3063 | strcat(value, "AUDIO_FORMAT_PCM_24_BIT_PACKED"); |
| 3064 | break; |
| 3065 | case AUDIO_FORMAT_PCM_32_BIT: |
| 3066 | strcat(value, "AUDIO_FORMAT_PCM_32_BIT"); |
| 3067 | break; |
| 3068 | default: |
| 3069 | ALOGE("%s: unsupported format %#x", __func__, |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 3070 | supported_formats[0]); |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 3071 | break; |
| 3072 | } |
| 3073 | str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_FORMATS, value); |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 3074 | } |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 3075 | return ret >= 0; |
| 3076 | } |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 3077 | |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 3078 | static bool stream_get_parameter_rates(struct str_parms *query, |
| 3079 | struct str_parms *reply, |
| 3080 | uint32_t *supported_sample_rates) { |
| 3081 | |
| 3082 | int i; |
| 3083 | char value[256]; |
| 3084 | int ret = -1; |
| 3085 | if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) { |
| 3086 | ret = 0; |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 3087 | value[0] = '\0'; |
| 3088 | i=0; |
| 3089 | int cursor = 0; |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 3090 | while (supported_sample_rates[i]) { |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 3091 | int avail = sizeof(value) - cursor; |
| 3092 | ret = snprintf(value + cursor, avail, "%s%d", |
| 3093 | cursor > 0 ? "|" : "", |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 3094 | supported_sample_rates[i]); |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 3095 | if (ret < 0 || ret >= avail) { |
| 3096 | // if cursor is at the last element of the array |
| 3097 | // overwrite with \0 is duplicate work as |
| 3098 | // snprintf already put a \0 in place. |
| 3099 | // else |
| 3100 | // we had space to write the '|' at value[cursor] |
| 3101 | // (which will be overwritten) or no space to fill |
| 3102 | // the first element (=> cursor == 0) |
| 3103 | value[cursor] = '\0'; |
| 3104 | break; |
| 3105 | } |
| 3106 | cursor += ret; |
| 3107 | ++i; |
| 3108 | } |
| 3109 | str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES, |
| 3110 | value); |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 3111 | } |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 3112 | return ret >= 0; |
| 3113 | } |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 3114 | |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 3115 | static char* out_get_parameters(const struct audio_stream *stream, const char *keys) |
| 3116 | { |
| 3117 | struct stream_out *out = (struct stream_out *)stream; |
| 3118 | struct str_parms *query = str_parms_create_str(keys); |
| 3119 | char *str; |
| 3120 | struct str_parms *reply = str_parms_create(); |
| 3121 | bool replied = false; |
| 3122 | ALOGV("%s: enter: keys - %s", __func__, keys); |
| 3123 | |
| 3124 | replied |= stream_get_parameter_channels(query, reply, |
| 3125 | &out->supported_channel_masks[0]); |
| 3126 | replied |= stream_get_parameter_formats(query, reply, |
| 3127 | &out->supported_formats[0]); |
| 3128 | replied |= stream_get_parameter_rates(query, reply, |
| 3129 | &out->supported_sample_rates[0]); |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 3130 | if (replied) { |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3131 | str = str_parms_to_str(reply); |
| 3132 | } else { |
Kevin Rocard | edf0b4c | 2017-05-05 09:08:11 -0700 | [diff] [blame] | 3133 | str = strdup(""); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3134 | } |
| 3135 | str_parms_destroy(query); |
| 3136 | str_parms_destroy(reply); |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 3137 | ALOGV("%s: exit: returns - %s", __func__, str); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3138 | return str; |
| 3139 | } |
| 3140 | |
| 3141 | static uint32_t out_get_latency(const struct audio_stream_out *stream) |
| 3142 | { |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 3143 | uint32_t hw_delay, period_ms; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3144 | struct stream_out *out = (struct stream_out *)stream; |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 3145 | uint32_t latency; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3146 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3147 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) |
| 3148 | return COMPRESS_OFFLOAD_PLAYBACK_LATENCY; |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3149 | else if ((out->realtime) || |
| 3150 | (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP)) { |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 3151 | // since the buffer won't be filled up faster than realtime, |
| 3152 | // return a smaller number |
| 3153 | period_ms = (out->af_period_multiplier * out->config.period_size * |
| 3154 | 1000) / (out->config.rate); |
| 3155 | hw_delay = platform_render_latency(out->usecase)/1000; |
| 3156 | return period_ms + hw_delay; |
| 3157 | } |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3158 | |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 3159 | latency = (out->config.period_count * out->config.period_size * 1000) / |
| 3160 | (out->config.rate); |
| 3161 | |
| 3162 | if (AUDIO_DEVICE_OUT_ALL_A2DP & out->devices) |
| 3163 | latency += audio_extn_a2dp_get_encoder_latency(); |
| 3164 | |
| 3165 | return latency; |
| 3166 | } |
| 3167 | |
| 3168 | static int set_compr_volume(struct audio_stream_out *stream, float left, |
| 3169 | float right) |
| 3170 | { |
| 3171 | struct stream_out *out = (struct stream_out *)stream; |
| 3172 | int volume[2]; |
| 3173 | char mixer_ctl_name[128]; |
| 3174 | struct audio_device *adev = out->dev; |
| 3175 | struct mixer_ctl *ctl; |
| 3176 | int pcm_device_id = platform_get_pcm_device_id(out->usecase, |
| 3177 | PCM_PLAYBACK); |
| 3178 | |
| 3179 | snprintf(mixer_ctl_name, sizeof(mixer_ctl_name), |
| 3180 | "Compress Playback %d Volume", pcm_device_id); |
| 3181 | ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name); |
| 3182 | if (!ctl) { |
| 3183 | ALOGE("%s: Could not get ctl for mixer cmd - %s", |
| 3184 | __func__, mixer_ctl_name); |
| 3185 | return -EINVAL; |
| 3186 | } |
| 3187 | ALOGV("%s: ctl for mixer cmd - %s, left %f, right %f", |
| 3188 | __func__, mixer_ctl_name, left, right); |
| 3189 | volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX); |
| 3190 | volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX); |
| 3191 | mixer_ctl_set_array(ctl, volume, sizeof(volume) / sizeof(volume[0])); |
| 3192 | |
| 3193 | return 0; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3194 | } |
| 3195 | |
| 3196 | static int out_set_volume(struct audio_stream_out *stream, float left, |
| 3197 | float right) |
| 3198 | { |
Eric Laurent | a9024de | 2013-04-04 09:19:12 -0700 | [diff] [blame] | 3199 | struct stream_out *out = (struct stream_out *)stream; |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 3200 | int ret = 0; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3201 | |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 3202 | if (out->usecase == USECASE_AUDIO_PLAYBACK_HIFI) { |
Eric Laurent | a9024de | 2013-04-04 09:19:12 -0700 | [diff] [blame] | 3203 | /* only take left channel into account: the API is for stereo anyway */ |
| 3204 | out->muted = (left == 0.0f); |
| 3205 | return 0; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3206 | } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 3207 | pthread_mutex_lock(&out->compr_mute_lock); |
| 3208 | ALOGV("%s: compress mute %d", __func__, out->a2dp_compress_mute); |
| 3209 | if (!out->a2dp_compress_mute) |
| 3210 | ret = set_compr_volume(stream, left, right); |
| 3211 | out->volume_l = left; |
| 3212 | out->volume_r = right; |
| 3213 | pthread_mutex_unlock(&out->compr_mute_lock); |
| 3214 | return ret; |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 3215 | } else if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP) { |
vivek mehta | 4a82477 | 2017-06-08 19:05:49 -0700 | [diff] [blame] | 3216 | out->app_type_cfg.gain[0] = (int)(left * VOIP_PLAYBACK_VOLUME_MAX); |
| 3217 | out->app_type_cfg.gain[1] = (int)(right * VOIP_PLAYBACK_VOLUME_MAX); |
| 3218 | if (!out->standby) { |
| 3219 | // if in standby, cached volume will be sent after stream is opened |
| 3220 | audio_extn_utils_send_app_type_gain(out->dev, |
| 3221 | out->app_type_cfg.app_type, |
| 3222 | &out->app_type_cfg.gain[0]); |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 3223 | } |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 3224 | return 0; |
Eric Laurent | a9024de | 2013-04-04 09:19:12 -0700 | [diff] [blame] | 3225 | } |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3226 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3227 | return -ENOSYS; |
| 3228 | } |
| 3229 | |
Haynes Mathew George | c735fb0 | 2016-06-30 18:00:28 -0700 | [diff] [blame] | 3230 | // note: this call is safe only if the stream_cb is |
| 3231 | // removed first in close_output_stream (as is done now). |
| 3232 | static void out_snd_mon_cb(void * stream, struct str_parms * parms) |
| 3233 | { |
| 3234 | if (!stream || !parms) |
| 3235 | return; |
| 3236 | |
| 3237 | struct stream_out *out = (struct stream_out *)stream; |
| 3238 | struct audio_device *adev = out->dev; |
| 3239 | |
| 3240 | card_status_t status; |
| 3241 | int card; |
| 3242 | if (parse_snd_card_status(parms, &card, &status) < 0) |
| 3243 | return; |
| 3244 | |
| 3245 | pthread_mutex_lock(&adev->lock); |
| 3246 | bool valid_cb = (card == adev->snd_card); |
| 3247 | pthread_mutex_unlock(&adev->lock); |
| 3248 | |
| 3249 | if (!valid_cb) |
| 3250 | return; |
| 3251 | |
Haynes Mathew George | c735fb0 | 2016-06-30 18:00:28 -0700 | [diff] [blame] | 3252 | lock_output_stream(out); |
| 3253 | if (out->card_status != status) |
| 3254 | out->card_status = status; |
| 3255 | pthread_mutex_unlock(&out->lock); |
| 3256 | |
Haynes Mathew George | 3ddd3bd | 2016-07-07 20:01:53 -0700 | [diff] [blame] | 3257 | ALOGW("out_snd_mon_cb for card %d usecase %s, status %s", card, |
| 3258 | use_case_table[out->usecase], |
| 3259 | status == CARD_STATUS_OFFLINE ? "offline" : "online"); |
| 3260 | |
| 3261 | if (status == CARD_STATUS_OFFLINE) |
| 3262 | out_on_error(stream); |
Haynes Mathew George | c735fb0 | 2016-06-30 18:00:28 -0700 | [diff] [blame] | 3263 | |
| 3264 | return; |
| 3265 | } |
| 3266 | |
Uday Kishore Pasupuleti | cec8ad8 | 2015-04-15 10:34:06 -0700 | [diff] [blame] | 3267 | #ifdef NO_AUDIO_OUT |
| 3268 | static ssize_t out_write_for_no_output(struct audio_stream_out *stream, |
Ariel Gertzenstein | c2c1174 | 2016-11-14 18:08:28 -0500 | [diff] [blame] | 3269 | const void *buffer __unused, size_t bytes) |
Uday Kishore Pasupuleti | cec8ad8 | 2015-04-15 10:34:06 -0700 | [diff] [blame] | 3270 | { |
| 3271 | struct stream_out *out = (struct stream_out *)stream; |
| 3272 | |
| 3273 | /* No Output device supported other than BT for playback. |
| 3274 | * Sleep for the amount of buffer duration |
| 3275 | */ |
Eric Laurent | a147807 | 2015-09-21 17:21:52 -0700 | [diff] [blame] | 3276 | lock_output_stream(out); |
Ariel Gertzenstein | c2c1174 | 2016-11-14 18:08:28 -0500 | [diff] [blame] | 3277 | usleep(bytes * 1000000 / audio_stream_out_frame_size( |
| 3278 | (const struct audio_stream_out *)&out->stream) / |
Uday Kishore Pasupuleti | cec8ad8 | 2015-04-15 10:34:06 -0700 | [diff] [blame] | 3279 | out_get_sample_rate(&out->stream.common)); |
| 3280 | pthread_mutex_unlock(&out->lock); |
| 3281 | return bytes; |
| 3282 | } |
| 3283 | #endif |
| 3284 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3285 | static ssize_t out_write(struct audio_stream_out *stream, const void *buffer, |
| 3286 | size_t bytes) |
| 3287 | { |
| 3288 | struct stream_out *out = (struct stream_out *)stream; |
| 3289 | struct audio_device *adev = out->dev; |
Eric Laurent | 6e89524 | 2013-09-05 16:10:57 -0700 | [diff] [blame] | 3290 | ssize_t ret = 0; |
Andy Hung | 7401c7c | 2016-09-21 12:41:21 -0700 | [diff] [blame] | 3291 | int error_code = ERROR_CODE_STANDBY; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3292 | |
Eric Laurent | a147807 | 2015-09-21 17:21:52 -0700 | [diff] [blame] | 3293 | lock_output_stream(out); |
vivek mehta | e3afca2 | 2017-04-11 17:13:50 -0700 | [diff] [blame] | 3294 | // this is always nonzero |
Andy Hung | 8e7f03f | 2017-06-12 14:05:22 -0700 | [diff] [blame] | 3295 | const size_t frame_size = audio_stream_out_frame_size(stream); |
| 3296 | const size_t frames = bytes / frame_size; |
vivek mehta | e3afca2 | 2017-04-11 17:13:50 -0700 | [diff] [blame] | 3297 | |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3298 | if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) { |
| 3299 | error_code = ERROR_CODE_WRITE; |
| 3300 | goto exit; |
| 3301 | } |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 3302 | |
| 3303 | if ((out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) && |
| 3304 | (audio_extn_a2dp_is_suspended())) { |
juyuchen | 9baad39 | 2018-06-05 19:02:10 +0800 | [diff] [blame] | 3305 | if (!(out->devices & (AUDIO_DEVICE_OUT_SPEAKER | AUDIO_DEVICE_OUT_SPEAKER_SAFE))) { |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 3306 | if (!(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) { |
| 3307 | ret = -EIO; |
| 3308 | goto exit; |
| 3309 | } |
| 3310 | } |
| 3311 | } |
| 3312 | |
Andy Hung | 572633e | 2019-02-19 11:58:24 -0800 | [diff] [blame] | 3313 | const bool was_in_standby = out->standby; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3314 | if (out->standby) { |
Ravi Kumar Alamanda | 59d296d | 2013-05-02 11:25:27 -0700 | [diff] [blame] | 3315 | out->standby = false; |
Andy Hung | 241e36f | 2019-02-19 12:00:38 -0800 | [diff] [blame] | 3316 | const int64_t startNs = systemTime(SYSTEM_TIME_MONOTONIC); |
| 3317 | |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 3318 | pthread_mutex_lock(&adev->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3319 | ret = start_output_stream(out); |
vivek mehta | 4012509 | 2017-08-21 18:48:51 -0700 | [diff] [blame] | 3320 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3321 | /* ToDo: If use case is compress offload should return 0 */ |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3322 | if (ret != 0) { |
Ravi Kumar Alamanda | 59d296d | 2013-05-02 11:25:27 -0700 | [diff] [blame] | 3323 | out->standby = true; |
vivek mehta | 4012509 | 2017-08-21 18:48:51 -0700 | [diff] [blame] | 3324 | pthread_mutex_unlock(&adev->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3325 | goto exit; |
| 3326 | } |
vivek mehta | fb4d7bd | 2016-04-29 03:16:47 -0700 | [diff] [blame] | 3327 | |
vivek mehta | 4012509 | 2017-08-21 18:48:51 -0700 | [diff] [blame] | 3328 | // after standby always force set last known cal step |
| 3329 | // dont change level anywhere except at the audio_hw_send_gain_dep_calibration |
| 3330 | ALOGD("%s: retry previous failed cal level set", __func__); |
| 3331 | send_gain_dep_calibration_l(); |
| 3332 | pthread_mutex_unlock(&adev->lock); |
Andy Hung | 572633e | 2019-02-19 11:58:24 -0800 | [diff] [blame] | 3333 | |
Andy Hung | 241e36f | 2019-02-19 12:00:38 -0800 | [diff] [blame] | 3334 | // log startup time in ms. |
| 3335 | simple_stats_log( |
| 3336 | &out->start_latency_ms, (systemTime(SYSTEM_TIME_MONOTONIC) - startNs) * 1e-6); |
Andy Hung | 572633e | 2019-02-19 11:58:24 -0800 | [diff] [blame] | 3337 | out->last_fifo_valid = false; // we're coming out of standby, last_fifo isn't valid. |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3338 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3339 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3340 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3341 | ALOGVV("%s: writing buffer (%zu bytes) to compress device", __func__, bytes); |
Haynes Mathew George | 352f27b | 2013-07-26 00:00:15 -0700 | [diff] [blame] | 3342 | if (out->send_new_metadata) { |
| 3343 | ALOGVV("send new gapless metadata"); |
| 3344 | compress_set_gapless_metadata(out->compr, &out->gapless_mdata); |
| 3345 | out->send_new_metadata = 0; |
| 3346 | } |
Eric Laurent | b49b3f6 | 2016-02-29 17:59:49 -0800 | [diff] [blame] | 3347 | unsigned int avail; |
| 3348 | struct timespec tstamp; |
| 3349 | ret = compress_get_hpointer(out->compr, &avail, &tstamp); |
| 3350 | /* Do not limit write size if the available frames count is unknown */ |
| 3351 | if (ret != 0) { |
| 3352 | avail = bytes; |
| 3353 | } |
| 3354 | if (avail == 0) { |
| 3355 | ret = 0; |
| 3356 | } else { |
Andy Hung | 572633e | 2019-02-19 11:58:24 -0800 | [diff] [blame] | 3357 | // check for compressed format underrun, essentially an empty buffer check |
| 3358 | // for a lack of better measurement. |
| 3359 | if (!was_in_standby && avail == out->kernel_buffer_size) { |
| 3360 | ALOGW("%s: compressed buffer empty (underrun)", __func__); |
| 3361 | simple_stats_log(&out->fifo_underruns, 1.); // Note: log one frame for compressed. |
| 3362 | } |
| 3363 | |
Eric Laurent | b49b3f6 | 2016-02-29 17:59:49 -0800 | [diff] [blame] | 3364 | if (avail > bytes) { |
| 3365 | avail = bytes; |
| 3366 | } |
| 3367 | ret = compress_write(out->compr, buffer, avail); |
| 3368 | ALOGVV("%s: writing buffer (%d bytes) to compress device returned %zd", |
| 3369 | __func__, avail, ret); |
| 3370 | } |
Haynes Mathew George | 352f27b | 2013-07-26 00:00:15 -0700 | [diff] [blame] | 3371 | |
Eric Laurent | 6e89524 | 2013-09-05 16:10:57 -0700 | [diff] [blame] | 3372 | if (ret >= 0 && ret < (ssize_t)bytes) { |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3373 | send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER); |
| 3374 | } |
Eric Laurent | b49b3f6 | 2016-02-29 17:59:49 -0800 | [diff] [blame] | 3375 | if (ret > 0 && !out->playback_started) { |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3376 | compress_start(out->compr); |
| 3377 | out->playback_started = 1; |
| 3378 | out->offload_state = OFFLOAD_STATE_PLAYING; |
| 3379 | } |
Andy Hung | 7401c7c | 2016-09-21 12:41:21 -0700 | [diff] [blame] | 3380 | if (ret < 0) { |
Andy Hung | a452b0a | 2017-03-15 14:51:15 -0700 | [diff] [blame] | 3381 | error_log_log(out->error_log, ERROR_CODE_WRITE, audio_utils_get_real_time_ns()); |
Andy Hung | dacb45c | 2017-03-31 15:38:14 -0700 | [diff] [blame] | 3382 | } else { |
| 3383 | out->written += ret; // accumulate bytes written for offload. |
Andy Hung | 7401c7c | 2016-09-21 12:41:21 -0700 | [diff] [blame] | 3384 | } |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3385 | pthread_mutex_unlock(&out->lock); |
Andy Hung | fc044e1 | 2017-03-20 09:24:22 -0700 | [diff] [blame] | 3386 | // TODO: consider logging offload pcm |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3387 | return ret; |
| 3388 | } else { |
Andy Hung | 7401c7c | 2016-09-21 12:41:21 -0700 | [diff] [blame] | 3389 | error_code = ERROR_CODE_WRITE; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3390 | if (out->pcm) { |
Eric Laurent | ad2dde9 | 2017-09-20 18:27:31 -0700 | [diff] [blame] | 3391 | size_t bytes_to_write = bytes; |
| 3392 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3393 | if (out->muted) |
| 3394 | memset((void *)buffer, 0, bytes); |
Eric Laurent | ad2dde9 | 2017-09-20 18:27:31 -0700 | [diff] [blame] | 3395 | // FIXME: this can be removed once audio flinger mixer supports mono output |
Nadav Bar | 3d72cfc | 2018-01-07 12:19:24 +0200 | [diff] [blame] | 3396 | if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP || out->usecase == USECASE_INCALL_MUSIC_UPLINK) { |
Eric Laurent | ad2dde9 | 2017-09-20 18:27:31 -0700 | [diff] [blame] | 3397 | size_t channel_count = audio_channel_count_from_out_mask(out->channel_mask); |
| 3398 | int16_t *src = (int16_t *)buffer; |
| 3399 | int16_t *dst = (int16_t *)buffer; |
Haynes Mathew George | 88e6fb2 | 2015-08-19 11:51:34 -0700 | [diff] [blame] | 3400 | |
Eric Laurent | ad2dde9 | 2017-09-20 18:27:31 -0700 | [diff] [blame] | 3401 | LOG_ALWAYS_FATAL_IF(out->config.channels != 1 || channel_count != 2 || |
| 3402 | out->format != AUDIO_FORMAT_PCM_16_BIT, |
| 3403 | "out_write called for VOIP use case with wrong properties"); |
Haynes Mathew George | 88e6fb2 | 2015-08-19 11:51:34 -0700 | [diff] [blame] | 3404 | |
Eric Laurent | ad2dde9 | 2017-09-20 18:27:31 -0700 | [diff] [blame] | 3405 | for (size_t i = 0; i < frames ; i++, dst++, src += 2) { |
| 3406 | *dst = (int16_t)(((int32_t)src[0] + (int32_t)src[1]) >> 1); |
| 3407 | } |
| 3408 | bytes_to_write /= 2; |
| 3409 | } |
Andy Hung | 572633e | 2019-02-19 11:58:24 -0800 | [diff] [blame] | 3410 | |
| 3411 | // Note: since out_get_presentation_position() is called alternating with out_write() |
| 3412 | // by AudioFlinger, we can check underruns using the prior timestamp read. |
| 3413 | // (Alternately we could check if the buffer is empty using pcm_get_htimestamp(). |
| 3414 | if (out->last_fifo_valid) { |
| 3415 | // compute drain to see if there is an underrun. |
| 3416 | const int64_t current_ns = systemTime(SYSTEM_TIME_MONOTONIC); // sys call |
| 3417 | const int64_t frames_by_time = |
| 3418 | (current_ns - out->last_fifo_time_ns) * out->config.rate / NANOS_PER_SECOND; |
| 3419 | const int64_t underrun = frames_by_time - out->last_fifo_frames_remaining; |
| 3420 | |
| 3421 | if (underrun > 0) { |
| 3422 | simple_stats_log(&out->fifo_underruns, underrun); |
| 3423 | |
| 3424 | ALOGW("%s: underrun(%lld) " |
| 3425 | "frames_by_time(%lld) > out->last_fifo_frames_remaining(%lld)", |
| 3426 | __func__, |
| 3427 | (long long)out->fifo_underruns.n, |
| 3428 | (long long)frames_by_time, |
| 3429 | (long long)out->last_fifo_frames_remaining); |
| 3430 | } |
| 3431 | out->last_fifo_valid = false; // we're writing below, mark fifo info as stale. |
| 3432 | } |
Eric Laurent | ad2dde9 | 2017-09-20 18:27:31 -0700 | [diff] [blame] | 3433 | |
Glenn Kasten | 5a59dff | 2018-02-22 08:05:35 -0800 | [diff] [blame] | 3434 | long ns = (frames * (int64_t) NANOS_PER_SECOND) / out->config.rate; |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 3435 | request_out_focus(out, ns); |
| 3436 | |
| 3437 | bool use_mmap = is_mmap_usecase(out->usecase) || out->realtime; |
Vignesh Kulothungan | 019d19b | 2019-01-23 11:09:18 -0800 | [diff] [blame] | 3438 | if (use_mmap) { |
Eric Laurent | ad2dde9 | 2017-09-20 18:27:31 -0700 | [diff] [blame] | 3439 | ret = pcm_mmap_write(out->pcm, (void *)buffer, bytes_to_write); |
Vignesh Kulothungan | 019d19b | 2019-01-23 11:09:18 -0800 | [diff] [blame] | 3440 | } else { |
| 3441 | if (out->usecase == USECASE_AUDIO_PLAYBACK_WITH_HAPTICS) { |
| 3442 | size_t channel_count = audio_channel_count_from_out_mask(out->channel_mask); |
| 3443 | size_t bytes_per_sample = audio_bytes_per_sample(out->format); |
| 3444 | size_t frame_size = channel_count * bytes_per_sample; |
| 3445 | size_t frame_count = bytes_to_write / frame_size; |
Haynes Mathew George | 88e6fb2 | 2015-08-19 11:51:34 -0700 | [diff] [blame] | 3446 | |
Vignesh Kulothungan | 019d19b | 2019-01-23 11:09:18 -0800 | [diff] [blame] | 3447 | bool force_haptic_path = |
| 3448 | property_get_bool("vendor.audio.test_haptic", false); |
| 3449 | |
| 3450 | // extract Haptics data from Audio buffer |
| 3451 | bool alloc_haptic_buffer = false; |
| 3452 | int haptic_channel_count = adev->haptics_config.channels; |
| 3453 | size_t haptic_frame_size = bytes_per_sample * haptic_channel_count; |
| 3454 | size_t audio_frame_size = frame_size - haptic_frame_size; |
| 3455 | size_t total_haptic_buffer_size = frame_count * haptic_frame_size; |
| 3456 | |
| 3457 | if (adev->haptic_buffer == NULL) { |
| 3458 | alloc_haptic_buffer = true; |
| 3459 | } else if (adev->haptic_buffer_size < total_haptic_buffer_size) { |
| 3460 | free(adev->haptic_buffer); |
| 3461 | adev->haptic_buffer_size = 0; |
| 3462 | alloc_haptic_buffer = true; |
| 3463 | } |
| 3464 | |
| 3465 | if (alloc_haptic_buffer) { |
| 3466 | adev->haptic_buffer = (uint8_t *)calloc(1, total_haptic_buffer_size); |
| 3467 | adev->haptic_buffer_size = total_haptic_buffer_size; |
| 3468 | } |
| 3469 | |
| 3470 | size_t src_index = 0, aud_index = 0, hap_index = 0; |
| 3471 | uint8_t *audio_buffer = (uint8_t *)buffer; |
| 3472 | uint8_t *haptic_buffer = adev->haptic_buffer; |
| 3473 | |
| 3474 | // This is required for testing only. This works for stereo data only. |
| 3475 | // One channel is fed to audio stream and other to haptic stream for testing. |
| 3476 | if (force_haptic_path) { |
| 3477 | audio_frame_size = haptic_frame_size = bytes_per_sample; |
| 3478 | } |
| 3479 | |
| 3480 | for (size_t i = 0; i < frame_count; i++) { |
| 3481 | for (size_t j = 0; j < audio_frame_size; j++) |
| 3482 | audio_buffer[aud_index++] = audio_buffer[src_index++]; |
| 3483 | |
| 3484 | for (size_t j = 0; j < haptic_frame_size; j++) |
| 3485 | haptic_buffer[hap_index++] = audio_buffer[src_index++]; |
| 3486 | } |
| 3487 | |
| 3488 | // This is required for testing only. |
| 3489 | // Discard haptic channel data. |
| 3490 | if (force_haptic_path) { |
| 3491 | src_index += haptic_frame_size; |
| 3492 | } |
| 3493 | |
| 3494 | // write to audio pipeline |
| 3495 | ret = pcm_write(out->pcm, |
| 3496 | (void *)audio_buffer, |
| 3497 | frame_count * audio_frame_size); |
| 3498 | |
| 3499 | // write to haptics pipeline |
| 3500 | if (adev->haptic_pcm) |
| 3501 | ret = pcm_write(adev->haptic_pcm, |
| 3502 | (void *)adev->haptic_buffer, |
| 3503 | frame_count * haptic_frame_size); |
| 3504 | |
| 3505 | } else { |
| 3506 | ret = pcm_write(out->pcm, (void *)buffer, bytes_to_write); |
| 3507 | } |
| 3508 | } |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 3509 | release_out_focus(out, ns); |
Andy Hung | 7401c7c | 2016-09-21 12:41:21 -0700 | [diff] [blame] | 3510 | } else { |
| 3511 | LOG_ALWAYS_FATAL("out->pcm is NULL after starting output stream"); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3512 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3513 | } |
| 3514 | |
| 3515 | exit: |
Andy Hung | da9b56b | 2016-09-16 20:06:35 -0700 | [diff] [blame] | 3516 | // For PCM we always consume the buffer and return #bytes regardless of ret. |
| 3517 | if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
Andy Hung | 8e7f03f | 2017-06-12 14:05:22 -0700 | [diff] [blame] | 3518 | out->written += frames; |
Andy Hung | da9b56b | 2016-09-16 20:06:35 -0700 | [diff] [blame] | 3519 | } |
Andy Hung | 7401c7c | 2016-09-21 12:41:21 -0700 | [diff] [blame] | 3520 | long long sleeptime_us = 0; |
Andy Hung | 9e737de | 2017-05-22 10:51:22 -0700 | [diff] [blame] | 3521 | |
Andy Hung | 7401c7c | 2016-09-21 12:41:21 -0700 | [diff] [blame] | 3522 | if (ret != 0) { |
Andy Hung | 3ba2c92 | 2017-06-15 17:48:35 -0700 | [diff] [blame] | 3523 | error_log_log(out->error_log, error_code, audio_utils_get_real_time_ns()); |
Andy Hung | 7401c7c | 2016-09-21 12:41:21 -0700 | [diff] [blame] | 3524 | if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
| 3525 | ALOGE_IF(out->pcm != NULL, |
| 3526 | "%s: error %zd - %s", __func__, ret, pcm_get_error(out->pcm)); |
Andy Hung | 8e7f03f | 2017-06-12 14:05:22 -0700 | [diff] [blame] | 3527 | sleeptime_us = frames * 1000000LL / out_get_sample_rate(&out->stream.common); |
Andy Hung | 7401c7c | 2016-09-21 12:41:21 -0700 | [diff] [blame] | 3528 | // usleep not guaranteed for values over 1 second but we don't limit here. |
| 3529 | } |
| 3530 | } |
Andy Hung | da9b56b | 2016-09-16 20:06:35 -0700 | [diff] [blame] | 3531 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3532 | pthread_mutex_unlock(&out->lock); |
| 3533 | |
| 3534 | if (ret != 0) { |
Haynes Mathew George | f9c7aae | 2016-09-20 14:12:41 -0700 | [diff] [blame] | 3535 | out_on_error(&out->stream.common); |
Andy Hung | 7401c7c | 2016-09-21 12:41:21 -0700 | [diff] [blame] | 3536 | if (sleeptime_us != 0) |
| 3537 | usleep(sleeptime_us); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3538 | } |
| 3539 | return bytes; |
| 3540 | } |
| 3541 | |
| 3542 | static int out_get_render_position(const struct audio_stream_out *stream, |
| 3543 | uint32_t *dsp_frames) |
| 3544 | { |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3545 | struct stream_out *out = (struct stream_out *)stream; |
| 3546 | *dsp_frames = 0; |
| 3547 | if ((out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) && (dsp_frames != NULL)) { |
Eric Laurent | a147807 | 2015-09-21 17:21:52 -0700 | [diff] [blame] | 3548 | lock_output_stream(out); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3549 | if (out->compr != NULL) { |
Andy Hung | 7171da2 | 2016-03-08 16:58:42 -0800 | [diff] [blame] | 3550 | unsigned long frames = 0; |
| 3551 | // TODO: check return value |
| 3552 | compress_get_tstamp(out->compr, &frames, &out->sample_rate); |
| 3553 | *dsp_frames = (uint32_t)frames; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3554 | ALOGVV("%s rendered frames %d sample_rate %d", |
| 3555 | __func__, *dsp_frames, out->sample_rate); |
| 3556 | } |
| 3557 | pthread_mutex_unlock(&out->lock); |
| 3558 | return 0; |
| 3559 | } else |
Kevin Rocard | 75d41bf | 2017-05-02 16:38:39 -0700 | [diff] [blame] | 3560 | return -ENODATA; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3561 | } |
| 3562 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 3563 | static int out_add_audio_effect(const struct audio_stream *stream __unused, |
| 3564 | effect_handle_t effect __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3565 | { |
| 3566 | return 0; |
| 3567 | } |
| 3568 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 3569 | static int out_remove_audio_effect(const struct audio_stream *stream __unused, |
| 3570 | effect_handle_t effect __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3571 | { |
| 3572 | return 0; |
| 3573 | } |
| 3574 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 3575 | static int out_get_next_write_timestamp(const struct audio_stream_out *stream __unused, |
| 3576 | int64_t *timestamp __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3577 | { |
Kevin Rocard | 75d41bf | 2017-05-02 16:38:39 -0700 | [diff] [blame] | 3578 | return -ENOSYS; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3579 | } |
| 3580 | |
Glenn Kasten | 2ccd7ba | 2013-09-10 09:04:31 -0700 | [diff] [blame] | 3581 | static int out_get_presentation_position(const struct audio_stream_out *stream, |
| 3582 | uint64_t *frames, struct timespec *timestamp) |
| 3583 | { |
| 3584 | struct stream_out *out = (struct stream_out *)stream; |
Kevin Rocard | 75d41bf | 2017-05-02 16:38:39 -0700 | [diff] [blame] | 3585 | int ret = -ENODATA; |
Eric Laurent | 949a089 | 2013-09-20 09:20:13 -0700 | [diff] [blame] | 3586 | unsigned long dsp_frames; |
Glenn Kasten | 2ccd7ba | 2013-09-10 09:04:31 -0700 | [diff] [blame] | 3587 | |
Eric Laurent | a147807 | 2015-09-21 17:21:52 -0700 | [diff] [blame] | 3588 | lock_output_stream(out); |
Glenn Kasten | 2ccd7ba | 2013-09-10 09:04:31 -0700 | [diff] [blame] | 3589 | |
Eric Laurent | 949a089 | 2013-09-20 09:20:13 -0700 | [diff] [blame] | 3590 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
| 3591 | if (out->compr != NULL) { |
Andy Hung | 7171da2 | 2016-03-08 16:58:42 -0800 | [diff] [blame] | 3592 | // TODO: check return value |
Eric Laurent | 949a089 | 2013-09-20 09:20:13 -0700 | [diff] [blame] | 3593 | compress_get_tstamp(out->compr, &dsp_frames, |
| 3594 | &out->sample_rate); |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 3595 | // Adjustment accounts for A2DP encoder latency with offload usecases |
| 3596 | // Note: Encoder latency is returned in ms. |
| 3597 | if (AUDIO_DEVICE_OUT_ALL_A2DP & out->devices) { |
| 3598 | unsigned long offset = |
| 3599 | (audio_extn_a2dp_get_encoder_latency() * out->sample_rate / 1000); |
| 3600 | dsp_frames = (dsp_frames > offset) ? (dsp_frames - offset) : 0; |
| 3601 | } |
Eric Laurent | 949a089 | 2013-09-20 09:20:13 -0700 | [diff] [blame] | 3602 | ALOGVV("%s rendered frames %ld sample_rate %d", |
| 3603 | __func__, dsp_frames, out->sample_rate); |
| 3604 | *frames = dsp_frames; |
| 3605 | ret = 0; |
| 3606 | /* this is the best we can do */ |
| 3607 | clock_gettime(CLOCK_MONOTONIC, timestamp); |
| 3608 | } |
| 3609 | } else { |
| 3610 | if (out->pcm) { |
vivek mehta | 1a9b7c0 | 2015-06-25 11:49:38 -0700 | [diff] [blame] | 3611 | unsigned int avail; |
Eric Laurent | 949a089 | 2013-09-20 09:20:13 -0700 | [diff] [blame] | 3612 | if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) { |
Andy Hung | 572633e | 2019-02-19 11:58:24 -0800 | [diff] [blame] | 3613 | |
| 3614 | // pcm_get_htimestamp() computes the available frames by comparing |
| 3615 | // the alsa driver hw_ptr and the appl_ptr levels. |
| 3616 | // In underrun, the hw_ptr may keep running and report an excessively |
| 3617 | // large number available number. |
| 3618 | if (avail > out->kernel_buffer_size) { |
| 3619 | ALOGW("%s: avail:%u > kernel_buffer_size:%zu clamping!", |
| 3620 | __func__, avail, out->kernel_buffer_size); |
| 3621 | avail = out->kernel_buffer_size; |
| 3622 | out->last_fifo_frames_remaining = 0; |
| 3623 | } else { |
| 3624 | out->last_fifo_frames_remaining = out->kernel_buffer_size - avail; |
| 3625 | } |
| 3626 | out->last_fifo_valid = true; |
| 3627 | out->last_fifo_time_ns = audio_utils_ns_from_timespec(timestamp); |
| 3628 | |
| 3629 | int64_t signed_frames = out->written - out->last_fifo_frames_remaining; |
| 3630 | |
| 3631 | ALOGVV("%s: frames:%lld avail:%u kernel_buffer_size:%zu", |
| 3632 | __func__, (long long)signed_frames, avail, out->kernel_buffer_size); |
| 3633 | |
Haynes Mathew George | 7ff216f | 2013-09-11 19:51:41 -0700 | [diff] [blame] | 3634 | // This adjustment accounts for buffering after app processor. |
| 3635 | // It is based on estimated DSP latency per use case, rather than exact. |
| 3636 | signed_frames -= |
| 3637 | (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL); |
| 3638 | |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 3639 | // Adjustment accounts for A2DP encoder latency with non-offload usecases |
| 3640 | // Note: Encoder latency is returned in ms, while platform_render_latency in us. |
| 3641 | if (AUDIO_DEVICE_OUT_ALL_A2DP & out->devices) { |
| 3642 | signed_frames -= |
| 3643 | (audio_extn_a2dp_get_encoder_latency() * out->sample_rate / 1000); |
| 3644 | } |
| 3645 | |
Eric Laurent | 949a089 | 2013-09-20 09:20:13 -0700 | [diff] [blame] | 3646 | // It would be unusual for this value to be negative, but check just in case ... |
| 3647 | if (signed_frames >= 0) { |
| 3648 | *frames = signed_frames; |
| 3649 | ret = 0; |
| 3650 | } |
Glenn Kasten | 2ccd7ba | 2013-09-10 09:04:31 -0700 | [diff] [blame] | 3651 | } |
| 3652 | } |
| 3653 | } |
| 3654 | |
| 3655 | pthread_mutex_unlock(&out->lock); |
| 3656 | |
| 3657 | return ret; |
| 3658 | } |
| 3659 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3660 | static int out_set_callback(struct audio_stream_out *stream, |
| 3661 | stream_callback_t callback, void *cookie) |
| 3662 | { |
| 3663 | struct stream_out *out = (struct stream_out *)stream; |
| 3664 | |
| 3665 | ALOGV("%s", __func__); |
Eric Laurent | a147807 | 2015-09-21 17:21:52 -0700 | [diff] [blame] | 3666 | lock_output_stream(out); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3667 | out->offload_callback = callback; |
| 3668 | out->offload_cookie = cookie; |
| 3669 | pthread_mutex_unlock(&out->lock); |
| 3670 | return 0; |
| 3671 | } |
| 3672 | |
| 3673 | static int out_pause(struct audio_stream_out* stream) |
| 3674 | { |
| 3675 | struct stream_out *out = (struct stream_out *)stream; |
| 3676 | int status = -ENOSYS; |
| 3677 | ALOGV("%s", __func__); |
| 3678 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
Eric Laurent | a147807 | 2015-09-21 17:21:52 -0700 | [diff] [blame] | 3679 | lock_output_stream(out); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3680 | if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) { |
| 3681 | status = compress_pause(out->compr); |
| 3682 | out->offload_state = OFFLOAD_STATE_PAUSED; |
| 3683 | } |
| 3684 | pthread_mutex_unlock(&out->lock); |
| 3685 | } |
| 3686 | return status; |
| 3687 | } |
| 3688 | |
| 3689 | static int out_resume(struct audio_stream_out* stream) |
| 3690 | { |
| 3691 | struct stream_out *out = (struct stream_out *)stream; |
| 3692 | int status = -ENOSYS; |
| 3693 | ALOGV("%s", __func__); |
| 3694 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
| 3695 | status = 0; |
Eric Laurent | a147807 | 2015-09-21 17:21:52 -0700 | [diff] [blame] | 3696 | lock_output_stream(out); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3697 | if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) { |
| 3698 | status = compress_resume(out->compr); |
| 3699 | out->offload_state = OFFLOAD_STATE_PLAYING; |
| 3700 | } |
| 3701 | pthread_mutex_unlock(&out->lock); |
| 3702 | } |
| 3703 | return status; |
| 3704 | } |
| 3705 | |
| 3706 | static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type ) |
| 3707 | { |
| 3708 | struct stream_out *out = (struct stream_out *)stream; |
| 3709 | int status = -ENOSYS; |
| 3710 | ALOGV("%s", __func__); |
| 3711 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
Eric Laurent | a147807 | 2015-09-21 17:21:52 -0700 | [diff] [blame] | 3712 | lock_output_stream(out); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3713 | if (type == AUDIO_DRAIN_EARLY_NOTIFY) |
| 3714 | status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN); |
| 3715 | else |
| 3716 | status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN); |
| 3717 | pthread_mutex_unlock(&out->lock); |
| 3718 | } |
| 3719 | return status; |
| 3720 | } |
| 3721 | |
| 3722 | static int out_flush(struct audio_stream_out* stream) |
| 3723 | { |
| 3724 | struct stream_out *out = (struct stream_out *)stream; |
| 3725 | ALOGV("%s", __func__); |
| 3726 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
Eric Laurent | a147807 | 2015-09-21 17:21:52 -0700 | [diff] [blame] | 3727 | lock_output_stream(out); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3728 | stop_compressed_output_l(out); |
| 3729 | pthread_mutex_unlock(&out->lock); |
| 3730 | return 0; |
| 3731 | } |
| 3732 | return -ENOSYS; |
| 3733 | } |
| 3734 | |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3735 | static int out_stop(const struct audio_stream_out* stream) |
| 3736 | { |
| 3737 | struct stream_out *out = (struct stream_out *)stream; |
| 3738 | struct audio_device *adev = out->dev; |
| 3739 | int ret = -ENOSYS; |
| 3740 | |
| 3741 | ALOGV("%s", __func__); |
| 3742 | pthread_mutex_lock(&adev->lock); |
| 3743 | if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP && !out->standby && |
| 3744 | out->playback_started && out->pcm != NULL) { |
| 3745 | pcm_stop(out->pcm); |
| 3746 | ret = stop_output_stream(out); |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 3747 | out->playback_started = false; |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3748 | } |
| 3749 | pthread_mutex_unlock(&adev->lock); |
| 3750 | return ret; |
| 3751 | } |
| 3752 | |
| 3753 | static int out_start(const struct audio_stream_out* stream) |
| 3754 | { |
| 3755 | struct stream_out *out = (struct stream_out *)stream; |
| 3756 | struct audio_device *adev = out->dev; |
| 3757 | int ret = -ENOSYS; |
| 3758 | |
| 3759 | ALOGV("%s", __func__); |
| 3760 | pthread_mutex_lock(&adev->lock); |
| 3761 | if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP && !out->standby && |
| 3762 | !out->playback_started && out->pcm != NULL) { |
| 3763 | ret = start_output_stream(out); |
| 3764 | if (ret == 0) { |
| 3765 | out->playback_started = true; |
| 3766 | } |
| 3767 | } |
| 3768 | pthread_mutex_unlock(&adev->lock); |
| 3769 | return ret; |
| 3770 | } |
| 3771 | |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 3772 | /* |
| 3773 | * Modify config->period_count based on min_size_frames |
| 3774 | */ |
| 3775 | static void adjust_mmap_period_count(struct pcm_config *config, int32_t min_size_frames) |
| 3776 | { |
| 3777 | int periodCountRequested = (min_size_frames + config->period_size - 1) |
| 3778 | / config->period_size; |
| 3779 | int periodCount = MMAP_PERIOD_COUNT_MIN; |
| 3780 | |
| 3781 | ALOGV("%s original config.period_size = %d config.period_count = %d", |
| 3782 | __func__, config->period_size, config->period_count); |
| 3783 | |
| 3784 | while (periodCount < periodCountRequested && (periodCount * 2) < MMAP_PERIOD_COUNT_MAX) { |
| 3785 | periodCount *= 2; |
| 3786 | } |
| 3787 | config->period_count = periodCount; |
| 3788 | |
| 3789 | ALOGV("%s requested config.period_count = %d", __func__, config->period_count); |
| 3790 | } |
| 3791 | |
Phil Burk | 51e6bc4 | 2019-03-25 10:23:35 -0700 | [diff] [blame] | 3792 | // Read offset for the positional timestamp from a persistent vendor property. |
| 3793 | // This is to workaround apparent inaccuracies in the timing information that |
| 3794 | // is used by the AAudio timing model. The inaccuracies can cause glitches. |
| 3795 | static int64_t get_mmap_out_time_offset() { |
| 3796 | const int32_t kDefaultOffsetMicros = 0; |
| 3797 | int32_t mmap_time_offset_micros = property_get_int32( |
| 3798 | "persist.audio.out_mmap_delay_micros", kDefaultOffsetMicros); |
| 3799 | ALOGI("mmap_time_offset_micros = %d for output", mmap_time_offset_micros); |
| 3800 | return mmap_time_offset_micros * (int64_t)1000; |
| 3801 | } |
| 3802 | |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3803 | static int out_create_mmap_buffer(const struct audio_stream_out *stream, |
| 3804 | int32_t min_size_frames, |
| 3805 | struct audio_mmap_buffer_info *info) |
| 3806 | { |
| 3807 | struct stream_out *out = (struct stream_out *)stream; |
| 3808 | struct audio_device *adev = out->dev; |
| 3809 | int ret = 0; |
| 3810 | unsigned int offset1; |
| 3811 | unsigned int frames1; |
| 3812 | const char *step = ""; |
Haynes Mathew George | 96483a2 | 2017-03-28 14:52:47 -0700 | [diff] [blame] | 3813 | uint32_t mmap_size; |
| 3814 | uint32_t buffer_size; |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3815 | |
| 3816 | ALOGV("%s", __func__); |
Haynes Mathew George | f1aaf36 | 2017-12-18 15:10:10 -0800 | [diff] [blame] | 3817 | lock_output_stream(out); |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3818 | pthread_mutex_lock(&adev->lock); |
| 3819 | |
| 3820 | if (info == NULL || min_size_frames == 0) { |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 3821 | ALOGE("%s: info = %p, min_size_frames = %d", __func__, info, min_size_frames); |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3822 | ret = -EINVAL; |
| 3823 | goto exit; |
| 3824 | } |
| 3825 | if (out->usecase != USECASE_AUDIO_PLAYBACK_MMAP || !out->standby) { |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 3826 | ALOGE("%s: usecase = %d, standby = %d", __func__, out->usecase, out->standby); |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3827 | ret = -ENOSYS; |
| 3828 | goto exit; |
| 3829 | } |
| 3830 | out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK); |
| 3831 | if (out->pcm_device_id < 0) { |
| 3832 | ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)", |
| 3833 | __func__, out->pcm_device_id, out->usecase); |
| 3834 | ret = -EINVAL; |
| 3835 | goto exit; |
| 3836 | } |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 3837 | |
| 3838 | adjust_mmap_period_count(&out->config, min_size_frames); |
| 3839 | |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3840 | ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d", |
| 3841 | __func__, adev->snd_card, out->pcm_device_id, out->config.channels); |
| 3842 | out->pcm = pcm_open(adev->snd_card, out->pcm_device_id, |
| 3843 | (PCM_OUT | PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC), &out->config); |
| 3844 | if (out->pcm == NULL || !pcm_is_ready(out->pcm)) { |
| 3845 | step = "open"; |
| 3846 | ret = -ENODEV; |
| 3847 | goto exit; |
| 3848 | } |
| 3849 | ret = pcm_mmap_begin(out->pcm, &info->shared_memory_address, &offset1, &frames1); |
| 3850 | if (ret < 0) { |
| 3851 | step = "begin"; |
| 3852 | goto exit; |
| 3853 | } |
| 3854 | info->buffer_size_frames = pcm_get_buffer_size(out->pcm); |
Haynes Mathew George | 96483a2 | 2017-03-28 14:52:47 -0700 | [diff] [blame] | 3855 | buffer_size = pcm_frames_to_bytes(out->pcm, info->buffer_size_frames); |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3856 | info->burst_size_frames = out->config.period_size; |
Haynes Mathew George | 96483a2 | 2017-03-28 14:52:47 -0700 | [diff] [blame] | 3857 | ret = platform_get_mmap_data_fd(adev->platform, |
| 3858 | out->pcm_device_id, 0 /*playback*/, |
| 3859 | &info->shared_memory_fd, |
| 3860 | &mmap_size); |
| 3861 | if (ret < 0) { |
| 3862 | // Fall back to non exclusive mode |
| 3863 | info->shared_memory_fd = pcm_get_poll_fd(out->pcm); |
| 3864 | } else { |
| 3865 | if (mmap_size < buffer_size) { |
| 3866 | step = "mmap"; |
| 3867 | goto exit; |
| 3868 | } |
| 3869 | // FIXME: indicate exclusive mode support by returning a negative buffer size |
| 3870 | info->buffer_size_frames *= -1; |
| 3871 | } |
| 3872 | memset(info->shared_memory_address, 0, buffer_size); |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3873 | |
| 3874 | ret = pcm_mmap_commit(out->pcm, 0, MMAP_PERIOD_SIZE); |
| 3875 | if (ret < 0) { |
| 3876 | step = "commit"; |
| 3877 | goto exit; |
| 3878 | } |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 3879 | |
Phil Burk | 51e6bc4 | 2019-03-25 10:23:35 -0700 | [diff] [blame] | 3880 | out->mmap_time_offset_nanos = get_mmap_out_time_offset(); |
| 3881 | |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 3882 | out->standby = false; |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3883 | ret = 0; |
| 3884 | |
| 3885 | ALOGV("%s: got mmap buffer address %p info->buffer_size_frames %d", |
| 3886 | __func__, info->shared_memory_address, info->buffer_size_frames); |
| 3887 | |
| 3888 | exit: |
| 3889 | if (ret != 0) { |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 3890 | if (out->pcm == NULL) { |
| 3891 | ALOGE("%s: %s - %d", __func__, step, ret); |
| 3892 | } else { |
| 3893 | ALOGE("%s: %s %s", __func__, step, pcm_get_error(out->pcm)); |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3894 | pcm_close(out->pcm); |
| 3895 | out->pcm = NULL; |
| 3896 | } |
| 3897 | } |
| 3898 | pthread_mutex_unlock(&adev->lock); |
Haynes Mathew George | f1aaf36 | 2017-12-18 15:10:10 -0800 | [diff] [blame] | 3899 | pthread_mutex_unlock(&out->lock); |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3900 | return ret; |
| 3901 | } |
| 3902 | |
| 3903 | static int out_get_mmap_position(const struct audio_stream_out *stream, |
| 3904 | struct audio_mmap_position *position) |
| 3905 | { |
Haynes Mathew George | f1aaf36 | 2017-12-18 15:10:10 -0800 | [diff] [blame] | 3906 | int ret = 0; |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3907 | struct stream_out *out = (struct stream_out *)stream; |
| 3908 | ALOGVV("%s", __func__); |
| 3909 | if (position == NULL) { |
| 3910 | return -EINVAL; |
| 3911 | } |
Haynes Mathew George | f1aaf36 | 2017-12-18 15:10:10 -0800 | [diff] [blame] | 3912 | lock_output_stream(out); |
| 3913 | if (out->usecase != USECASE_AUDIO_PLAYBACK_MMAP || |
| 3914 | out->pcm == NULL) { |
| 3915 | ret = -ENOSYS; |
| 3916 | goto exit; |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3917 | } |
| 3918 | |
| 3919 | struct timespec ts = { 0, 0 }; |
Haynes Mathew George | f1aaf36 | 2017-12-18 15:10:10 -0800 | [diff] [blame] | 3920 | ret = pcm_mmap_get_hw_ptr(out->pcm, (unsigned int *)&position->position_frames, &ts); |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3921 | if (ret < 0) { |
| 3922 | ALOGE("%s: %s", __func__, pcm_get_error(out->pcm)); |
Haynes Mathew George | f1aaf36 | 2017-12-18 15:10:10 -0800 | [diff] [blame] | 3923 | goto exit; |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3924 | } |
Phil Burk | 51e6bc4 | 2019-03-25 10:23:35 -0700 | [diff] [blame] | 3925 | position->time_nanoseconds = audio_utils_ns_from_timespec(&ts) |
| 3926 | + out->mmap_time_offset_nanos; |
| 3927 | |
Haynes Mathew George | f1aaf36 | 2017-12-18 15:10:10 -0800 | [diff] [blame] | 3928 | exit: |
| 3929 | pthread_mutex_unlock(&out->lock); |
| 3930 | return ret; |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3931 | } |
| 3932 | |
| 3933 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3934 | /** audio_stream_in implementation **/ |
| 3935 | static uint32_t in_get_sample_rate(const struct audio_stream *stream) |
| 3936 | { |
| 3937 | struct stream_in *in = (struct stream_in *)stream; |
| 3938 | |
| 3939 | return in->config.rate; |
| 3940 | } |
| 3941 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 3942 | static int in_set_sample_rate(struct audio_stream *stream __unused, uint32_t rate __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3943 | { |
| 3944 | return -ENOSYS; |
| 3945 | } |
| 3946 | |
| 3947 | static size_t in_get_buffer_size(const struct audio_stream *stream) |
| 3948 | { |
| 3949 | struct stream_in *in = (struct stream_in *)stream; |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 3950 | return in->config.period_size * in->af_period_multiplier * |
| 3951 | audio_stream_in_frame_size((const struct audio_stream_in *)stream); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3952 | } |
| 3953 | |
| 3954 | static uint32_t in_get_channels(const struct audio_stream *stream) |
| 3955 | { |
| 3956 | struct stream_in *in = (struct stream_in *)stream; |
| 3957 | |
| 3958 | return in->channel_mask; |
| 3959 | } |
| 3960 | |
vivek mehta | 4ed66e6 | 2016-04-15 23:33:34 -0700 | [diff] [blame] | 3961 | static audio_format_t in_get_format(const struct audio_stream *stream) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3962 | { |
vivek mehta | 4ed66e6 | 2016-04-15 23:33:34 -0700 | [diff] [blame] | 3963 | struct stream_in *in = (struct stream_in *)stream; |
| 3964 | return in->format; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3965 | } |
| 3966 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 3967 | static int in_set_format(struct audio_stream *stream __unused, audio_format_t format __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3968 | { |
| 3969 | return -ENOSYS; |
| 3970 | } |
| 3971 | |
| 3972 | static int in_standby(struct audio_stream *stream) |
| 3973 | { |
| 3974 | struct stream_in *in = (struct stream_in *)stream; |
| 3975 | struct audio_device *adev = in->dev; |
| 3976 | int status = 0; |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 3977 | bool do_stop = true; |
| 3978 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 3979 | ALOGV("%s: enter", __func__); |
Eric Laurent | a147807 | 2015-09-21 17:21:52 -0700 | [diff] [blame] | 3980 | |
| 3981 | lock_input_stream(in); |
Ravi Kumar Alamanda | a417cc5 | 2015-05-01 16:41:56 -0700 | [diff] [blame] | 3982 | |
| 3983 | if (!in->standby && in->is_st_session) { |
Joe Onorato | 188b622 | 2016-03-01 11:02:27 -0800 | [diff] [blame] | 3984 | ALOGV("%s: sound trigger pcm stop lab", __func__); |
Ravi Kumar Alamanda | a417cc5 | 2015-05-01 16:41:56 -0700 | [diff] [blame] | 3985 | audio_extn_sound_trigger_stop_lab(in); |
| 3986 | in->standby = true; |
| 3987 | } |
| 3988 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3989 | if (!in->standby) { |
Haynes Mathew George | 88e6fb2 | 2015-08-19 11:51:34 -0700 | [diff] [blame] | 3990 | if (adev->adm_deregister_stream) |
| 3991 | adev->adm_deregister_stream(adev->adm_data, in->capture_handle); |
| 3992 | |
Ravi Kumar Alamanda | 8bba9e9 | 2013-11-11 21:09:07 -0800 | [diff] [blame] | 3993 | pthread_mutex_lock(&adev->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3994 | in->standby = true; |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3995 | if (in->usecase == USECASE_AUDIO_RECORD_MMAP) { |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 3996 | do_stop = in->capture_started; |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3997 | in->capture_started = false; |
| 3998 | } |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 3999 | if (in->pcm) { |
| 4000 | pcm_close(in->pcm); |
| 4001 | in->pcm = NULL; |
| 4002 | } |
Eric Laurent | cefbbac | 2014-09-04 13:54:10 -0500 | [diff] [blame] | 4003 | adev->enable_voicerx = false; |
| 4004 | platform_set_echo_reference(adev, false, AUDIO_DEVICE_NONE ); |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 4005 | if (do_stop) { |
| 4006 | status = stop_input_stream(in); |
| 4007 | } |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 4008 | pthread_mutex_unlock(&adev->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4009 | } |
| 4010 | pthread_mutex_unlock(&in->lock); |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 4011 | ALOGV("%s: exit: status(%d)", __func__, status); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4012 | return status; |
| 4013 | } |
| 4014 | |
Andy Hung | d13f0d3 | 2017-06-12 13:58:37 -0700 | [diff] [blame] | 4015 | static int in_dump(const struct audio_stream *stream, int fd) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4016 | { |
Andy Hung | d13f0d3 | 2017-06-12 13:58:37 -0700 | [diff] [blame] | 4017 | struct stream_in *in = (struct stream_in *)stream; |
| 4018 | |
| 4019 | // We try to get the lock for consistency, |
| 4020 | // but it isn't necessary for these variables. |
| 4021 | // If we're not in standby, we may be blocked on a read. |
| 4022 | const bool locked = (pthread_mutex_trylock(&in->lock) == 0); |
| 4023 | dprintf(fd, " Standby: %s\n", in->standby ? "yes" : "no"); |
| 4024 | dprintf(fd, " Frames read: %lld\n", (long long)in->frames_read); |
| 4025 | dprintf(fd, " Frames muted: %lld\n", (long long)in->frames_muted); |
| 4026 | |
Andy Hung | 241e36f | 2019-02-19 12:00:38 -0800 | [diff] [blame] | 4027 | char buffer[256]; // for statistics formatting |
| 4028 | if (in->start_latency_ms.n > 0) { |
| 4029 | simple_stats_to_string(&in->start_latency_ms, buffer, sizeof(buffer)); |
| 4030 | dprintf(fd, " Start latency ms: %s\n", buffer); |
| 4031 | } |
| 4032 | |
Andy Hung | d13f0d3 | 2017-06-12 13:58:37 -0700 | [diff] [blame] | 4033 | if (locked) { |
| 4034 | pthread_mutex_unlock(&in->lock); |
| 4035 | } |
| 4036 | |
| 4037 | // dump error info |
| 4038 | (void)error_log_dump( |
| 4039 | in->error_log, fd, " " /* prefix */, 0 /* lines */, 0 /* limit_ns */); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4040 | return 0; |
| 4041 | } |
| 4042 | |
| 4043 | static int in_set_parameters(struct audio_stream *stream, const char *kvpairs) |
| 4044 | { |
| 4045 | struct stream_in *in = (struct stream_in *)stream; |
| 4046 | struct audio_device *adev = in->dev; |
| 4047 | struct str_parms *parms; |
| 4048 | char *str; |
| 4049 | char value[32]; |
| 4050 | int ret, val = 0; |
Eric Laurent | 03f0943 | 2014-03-25 18:09:11 -0700 | [diff] [blame] | 4051 | int status = 0; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4052 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 4053 | ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4054 | parms = str_parms_create_str(kvpairs); |
| 4055 | |
| 4056 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value)); |
| 4057 | |
Eric Laurent | a147807 | 2015-09-21 17:21:52 -0700 | [diff] [blame] | 4058 | lock_input_stream(in); |
| 4059 | |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 4060 | pthread_mutex_lock(&adev->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4061 | if (ret >= 0) { |
| 4062 | val = atoi(value); |
| 4063 | /* no audio source uses val == 0 */ |
| 4064 | if ((in->source != val) && (val != 0)) { |
| 4065 | in->source = val; |
| 4066 | } |
| 4067 | } |
| 4068 | |
| 4069 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value)); |
Eric Laurent | 03f0943 | 2014-03-25 18:09:11 -0700 | [diff] [blame] | 4070 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4071 | if (ret >= 0) { |
| 4072 | val = atoi(value); |
Haynes Mathew George | 8ea0472 | 2017-07-25 17:41:49 -0700 | [diff] [blame] | 4073 | if (((int)in->device != val) && (val != 0) && audio_is_input_device(val) ) { |
Kevin Rocard | f9f241e | 2017-08-07 19:21:36 -0700 | [diff] [blame] | 4074 | |
| 4075 | // Workaround: If routing to an non existing usb device, fail gracefully |
| 4076 | // The routing request will otherwise block during 10 second |
Kevin Rocard | 1e02c88 | 2017-08-09 15:26:07 -0700 | [diff] [blame] | 4077 | int card; |
| 4078 | if (audio_is_usb_in_device(val) && |
| 4079 | (card = get_alive_usb_card(parms)) >= 0) { |
| 4080 | |
| 4081 | ALOGW("in_set_parameters() ignoring rerouting to non existing USB card %d", card); |
Kevin Rocard | f9f241e | 2017-08-07 19:21:36 -0700 | [diff] [blame] | 4082 | status = -ENOSYS; |
| 4083 | } else { |
| 4084 | |
| 4085 | in->device = val; |
| 4086 | /* If recording is in progress, change the tx device to new device */ |
| 4087 | if (!in->standby) { |
| 4088 | ALOGV("update input routing change"); |
| 4089 | // inform adm before actual routing to prevent glitches. |
| 4090 | if (adev->adm_on_routing_change) { |
| 4091 | adev->adm_on_routing_change(adev->adm_data, |
| 4092 | in->capture_handle); |
| 4093 | } |
| 4094 | select_devices(adev, in->usecase); |
Haynes Mathew George | 104fdfd | 2016-07-01 16:57:24 -0700 | [diff] [blame] | 4095 | } |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 4096 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4097 | } |
| 4098 | } |
| 4099 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4100 | pthread_mutex_unlock(&adev->lock); |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 4101 | pthread_mutex_unlock(&in->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4102 | |
| 4103 | str_parms_destroy(parms); |
Eric Laurent | 03f0943 | 2014-03-25 18:09:11 -0700 | [diff] [blame] | 4104 | ALOGV("%s: exit: status(%d)", __func__, status); |
| 4105 | return status; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4106 | } |
| 4107 | |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 4108 | static char* in_get_parameters(const struct audio_stream *stream, |
| 4109 | const char *keys) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4110 | { |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 4111 | struct stream_in *in = (struct stream_in *)stream; |
| 4112 | struct str_parms *query = str_parms_create_str(keys); |
| 4113 | char *str; |
| 4114 | struct str_parms *reply = str_parms_create(); |
| 4115 | bool replied = false; |
| 4116 | |
| 4117 | ALOGV("%s: enter: keys - %s", __func__, keys); |
| 4118 | replied |= stream_get_parameter_channels(query, reply, |
| 4119 | &in->supported_channel_masks[0]); |
| 4120 | replied |= stream_get_parameter_formats(query, reply, |
| 4121 | &in->supported_formats[0]); |
| 4122 | replied |= stream_get_parameter_rates(query, reply, |
| 4123 | &in->supported_sample_rates[0]); |
| 4124 | if (replied) { |
| 4125 | str = str_parms_to_str(reply); |
| 4126 | } else { |
Kevin Rocard | 7e87668 | 2017-07-26 14:17:26 -0700 | [diff] [blame] | 4127 | str = strdup(""); |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 4128 | } |
| 4129 | str_parms_destroy(query); |
| 4130 | str_parms_destroy(reply); |
| 4131 | ALOGV("%s: exit: returns - %s", __func__, str); |
| 4132 | return str; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4133 | } |
| 4134 | |
Eric Laurent | 51f3c66 | 2018-04-10 18:21:34 -0700 | [diff] [blame] | 4135 | static int in_set_gain(struct audio_stream_in *stream, float gain) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4136 | { |
Eric Laurent | 51f3c66 | 2018-04-10 18:21:34 -0700 | [diff] [blame] | 4137 | struct stream_in *in = (struct stream_in *)stream; |
| 4138 | char mixer_ctl_name[128]; |
| 4139 | struct mixer_ctl *ctl; |
| 4140 | int ctl_value; |
| 4141 | |
| 4142 | ALOGV("%s: gain %f", __func__, gain); |
| 4143 | |
| 4144 | if (stream == NULL) |
| 4145 | return -EINVAL; |
| 4146 | |
| 4147 | /* in_set_gain() only used to silence MMAP capture for now */ |
| 4148 | if (in->usecase != USECASE_AUDIO_RECORD_MMAP) |
| 4149 | return -ENOSYS; |
| 4150 | |
| 4151 | snprintf(mixer_ctl_name, sizeof(mixer_ctl_name), "Capture %d Volume", in->pcm_device_id); |
| 4152 | |
| 4153 | ctl = mixer_get_ctl_by_name(in->dev->mixer, mixer_ctl_name); |
| 4154 | if (!ctl) { |
| 4155 | ALOGW("%s: Could not get ctl for mixer cmd - %s", |
| 4156 | __func__, mixer_ctl_name); |
| 4157 | return -ENOSYS; |
| 4158 | } |
| 4159 | |
| 4160 | if (gain < RECORD_GAIN_MIN) |
| 4161 | gain = RECORD_GAIN_MIN; |
| 4162 | else if (gain > RECORD_GAIN_MAX) |
| 4163 | gain = RECORD_GAIN_MAX; |
| 4164 | ctl_value = (int)(RECORD_VOLUME_CTL_MAX * gain); |
| 4165 | |
| 4166 | mixer_ctl_set_value(ctl, 0, ctl_value); |
| 4167 | return 0; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4168 | } |
| 4169 | |
Haynes Mathew George | c735fb0 | 2016-06-30 18:00:28 -0700 | [diff] [blame] | 4170 | static void in_snd_mon_cb(void * stream, struct str_parms * parms) |
| 4171 | { |
| 4172 | if (!stream || !parms) |
| 4173 | return; |
| 4174 | |
| 4175 | struct stream_in *in = (struct stream_in *)stream; |
| 4176 | struct audio_device *adev = in->dev; |
| 4177 | |
| 4178 | card_status_t status; |
| 4179 | int card; |
| 4180 | if (parse_snd_card_status(parms, &card, &status) < 0) |
| 4181 | return; |
| 4182 | |
| 4183 | pthread_mutex_lock(&adev->lock); |
| 4184 | bool valid_cb = (card == adev->snd_card); |
| 4185 | pthread_mutex_unlock(&adev->lock); |
| 4186 | |
| 4187 | if (!valid_cb) |
| 4188 | return; |
| 4189 | |
| 4190 | lock_input_stream(in); |
| 4191 | if (in->card_status != status) |
| 4192 | in->card_status = status; |
| 4193 | pthread_mutex_unlock(&in->lock); |
| 4194 | |
| 4195 | ALOGW("in_snd_mon_cb for card %d usecase %s, status %s", card, |
| 4196 | use_case_table[in->usecase], |
| 4197 | status == CARD_STATUS_OFFLINE ? "offline" : "online"); |
| 4198 | |
| 4199 | // a better solution would be to report error back to AF and let |
| 4200 | // it put the stream to standby |
| 4201 | if (status == CARD_STATUS_OFFLINE) |
| 4202 | in_standby(&in->stream.common); |
| 4203 | |
| 4204 | return; |
| 4205 | } |
| 4206 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4207 | static ssize_t in_read(struct audio_stream_in *stream, void *buffer, |
| 4208 | size_t bytes) |
| 4209 | { |
| 4210 | struct stream_in *in = (struct stream_in *)stream; |
| 4211 | struct audio_device *adev = in->dev; |
| 4212 | int i, ret = -1; |
vivek mehta | 4ed66e6 | 2016-04-15 23:33:34 -0700 | [diff] [blame] | 4213 | int *int_buf_stream = NULL; |
Andy Hung | d13f0d3 | 2017-06-12 13:58:37 -0700 | [diff] [blame] | 4214 | int error_code = ERROR_CODE_STANDBY; // initial errors are considered coming out of standby. |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4215 | |
Eric Laurent | a147807 | 2015-09-21 17:21:52 -0700 | [diff] [blame] | 4216 | lock_input_stream(in); |
Andy Hung | 8e7f03f | 2017-06-12 14:05:22 -0700 | [diff] [blame] | 4217 | const size_t frame_size = audio_stream_in_frame_size(stream); |
| 4218 | const size_t frames = bytes / frame_size; |
Eric Laurent | a147807 | 2015-09-21 17:21:52 -0700 | [diff] [blame] | 4219 | |
Ravi Kumar Alamanda | a417cc5 | 2015-05-01 16:41:56 -0700 | [diff] [blame] | 4220 | if (in->is_st_session) { |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 4221 | ALOGVV(" %s: reading on st session bytes=%zu", __func__, bytes); |
Ravi Kumar Alamanda | a417cc5 | 2015-05-01 16:41:56 -0700 | [diff] [blame] | 4222 | /* Read from sound trigger HAL */ |
| 4223 | audio_extn_sound_trigger_read(in, buffer, bytes); |
| 4224 | pthread_mutex_unlock(&in->lock); |
| 4225 | return bytes; |
| 4226 | } |
| 4227 | |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 4228 | if (in->usecase == USECASE_AUDIO_RECORD_MMAP) { |
| 4229 | ret = -ENOSYS; |
| 4230 | goto exit; |
| 4231 | } |
| 4232 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4233 | if (in->standby) { |
Andy Hung | 241e36f | 2019-02-19 12:00:38 -0800 | [diff] [blame] | 4234 | const int64_t startNs = systemTime(SYSTEM_TIME_MONOTONIC); |
| 4235 | |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 4236 | pthread_mutex_lock(&adev->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4237 | ret = start_input_stream(in); |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 4238 | pthread_mutex_unlock(&adev->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4239 | if (ret != 0) { |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4240 | goto exit; |
| 4241 | } |
| 4242 | in->standby = 0; |
Andy Hung | 241e36f | 2019-02-19 12:00:38 -0800 | [diff] [blame] | 4243 | |
| 4244 | // log startup time in ms. |
| 4245 | simple_stats_log( |
| 4246 | &in->start_latency_ms, (systemTime(SYSTEM_TIME_MONOTONIC) - startNs) * 1e-6); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4247 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4248 | |
Andy Hung | d13f0d3 | 2017-06-12 13:58:37 -0700 | [diff] [blame] | 4249 | // errors that occur here are read errors. |
| 4250 | error_code = ERROR_CODE_READ; |
| 4251 | |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 4252 | //what's the duration requested by the client? |
| 4253 | long ns = pcm_bytes_to_frames(in->pcm, bytes)*1000000000LL/ |
| 4254 | in->config.rate; |
| 4255 | request_in_focus(in, ns); |
Haynes Mathew George | 88e6fb2 | 2015-08-19 11:51:34 -0700 | [diff] [blame] | 4256 | |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 4257 | bool use_mmap = is_mmap_usecase(in->usecase) || in->realtime; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4258 | if (in->pcm) { |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 4259 | if (use_mmap) { |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 4260 | ret = pcm_mmap_read(in->pcm, buffer, bytes); |
vivek mehta | 4ed66e6 | 2016-04-15 23:33:34 -0700 | [diff] [blame] | 4261 | } else { |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 4262 | ret = pcm_read(in->pcm, buffer, bytes); |
Eric Laurent | f8b50aa | 2016-05-06 11:03:53 -0700 | [diff] [blame] | 4263 | } |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 4264 | if (ret < 0) { |
| 4265 | ALOGE("Failed to read w/err %s", strerror(errno)); |
| 4266 | ret = -errno; |
| 4267 | } |
Eric Laurent | f8b50aa | 2016-05-06 11:03:53 -0700 | [diff] [blame] | 4268 | if (!ret && bytes > 0 && (in->format == AUDIO_FORMAT_PCM_8_24_BIT)) { |
| 4269 | if (bytes % 4 == 0) { |
| 4270 | /* data from DSP comes in 24_8 format, convert it to 8_24 */ |
| 4271 | int_buf_stream = buffer; |
| 4272 | for (size_t itt=0; itt < bytes/4 ; itt++) { |
| 4273 | int_buf_stream[itt] >>= 8; |
vivek mehta | 4ed66e6 | 2016-04-15 23:33:34 -0700 | [diff] [blame] | 4274 | } |
Eric Laurent | f8b50aa | 2016-05-06 11:03:53 -0700 | [diff] [blame] | 4275 | } else { |
| 4276 | ALOGE("%s: !!! something wrong !!! ... data not 32 bit aligned ", __func__); |
| 4277 | ret = -EINVAL; |
| 4278 | goto exit; |
vivek mehta | 4ed66e6 | 2016-04-15 23:33:34 -0700 | [diff] [blame] | 4279 | } |
| 4280 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4281 | } |
| 4282 | |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 4283 | release_in_focus(in, ns); |
Haynes Mathew George | 88e6fb2 | 2015-08-19 11:51:34 -0700 | [diff] [blame] | 4284 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4285 | /* |
| 4286 | * Instead of writing zeroes here, we could trust the hardware |
| 4287 | * to always provide zeroes when muted. |
Eric Laurent | 7b2b5ab | 2014-09-14 12:29:59 -0700 | [diff] [blame] | 4288 | * No need to acquire adev->lock to read mic_muted here as we don't change its state. |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4289 | */ |
Haynes Mathew George | 988277b | 2017-12-20 14:14:33 -0800 | [diff] [blame] | 4290 | if (ret == 0 && adev->mic_muted && |
| 4291 | !voice_is_in_call_rec_stream(in) && |
| 4292 | in->usecase != USECASE_AUDIO_RECORD_AFE_PROXY) { |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4293 | memset(buffer, 0, bytes); |
Andy Hung | d13f0d3 | 2017-06-12 13:58:37 -0700 | [diff] [blame] | 4294 | in->frames_muted += frames; |
| 4295 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4296 | |
| 4297 | exit: |
| 4298 | pthread_mutex_unlock(&in->lock); |
| 4299 | |
| 4300 | if (ret != 0) { |
Andy Hung | d13f0d3 | 2017-06-12 13:58:37 -0700 | [diff] [blame] | 4301 | error_log_log(in->error_log, error_code, audio_utils_get_real_time_ns()); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4302 | in_standby(&in->stream.common); |
| 4303 | ALOGV("%s: read failed - sleeping for buffer duration", __func__); |
Andy Hung | 8e7f03f | 2017-06-12 14:05:22 -0700 | [diff] [blame] | 4304 | usleep(frames * 1000000LL / in_get_sample_rate(&in->stream.common)); |
Andy Hung | c858987 | 2016-03-10 16:37:48 -0800 | [diff] [blame] | 4305 | memset(buffer, 0, bytes); // clear return data |
Andy Hung | d13f0d3 | 2017-06-12 13:58:37 -0700 | [diff] [blame] | 4306 | in->frames_muted += frames; |
Andy Hung | c858987 | 2016-03-10 16:37:48 -0800 | [diff] [blame] | 4307 | } |
| 4308 | if (bytes > 0) { |
Andy Hung | 8e7f03f | 2017-06-12 14:05:22 -0700 | [diff] [blame] | 4309 | in->frames_read += frames; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4310 | } |
| 4311 | return bytes; |
| 4312 | } |
| 4313 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 4314 | 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] | 4315 | { |
| 4316 | return 0; |
| 4317 | } |
| 4318 | |
Andy Hung | 6ebe596 | 2016-01-15 17:46:57 -0800 | [diff] [blame] | 4319 | static int in_get_capture_position(const struct audio_stream_in *stream, |
| 4320 | int64_t *frames, int64_t *time) |
| 4321 | { |
| 4322 | if (stream == NULL || frames == NULL || time == NULL) { |
| 4323 | return -EINVAL; |
| 4324 | } |
| 4325 | struct stream_in *in = (struct stream_in *)stream; |
| 4326 | int ret = -ENOSYS; |
| 4327 | |
| 4328 | lock_input_stream(in); |
Haynes Mathew George | 7aa29e3 | 2017-12-11 18:10:36 -0800 | [diff] [blame] | 4329 | // note: ST sessions do not close the alsa pcm driver synchronously |
| 4330 | // on standby. Therefore, we may return an error even though the |
| 4331 | // pcm stream is still opened. |
| 4332 | if (in->standby) { |
| 4333 | ALOGE_IF(in->pcm != NULL && !in->is_st_session, |
| 4334 | "%s stream in standby but pcm not NULL for non ST session", __func__); |
| 4335 | goto exit; |
| 4336 | } |
Andy Hung | 6ebe596 | 2016-01-15 17:46:57 -0800 | [diff] [blame] | 4337 | if (in->pcm) { |
| 4338 | struct timespec timestamp; |
| 4339 | unsigned int avail; |
| 4340 | if (pcm_get_htimestamp(in->pcm, &avail, ×tamp) == 0) { |
| 4341 | *frames = in->frames_read + avail; |
| 4342 | *time = timestamp.tv_sec * 1000000000LL + timestamp.tv_nsec; |
| 4343 | ret = 0; |
| 4344 | } |
| 4345 | } |
Haynes Mathew George | 7aa29e3 | 2017-12-11 18:10:36 -0800 | [diff] [blame] | 4346 | exit: |
Andy Hung | 6ebe596 | 2016-01-15 17:46:57 -0800 | [diff] [blame] | 4347 | pthread_mutex_unlock(&in->lock); |
| 4348 | return ret; |
| 4349 | } |
| 4350 | |
Ravi Kumar Alamanda | f70ffb4 | 2013-04-16 15:55:53 -0700 | [diff] [blame] | 4351 | static int add_remove_audio_effect(const struct audio_stream *stream, |
| 4352 | effect_handle_t effect, |
| 4353 | bool enable) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4354 | { |
Ravi Kumar Alamanda | f70ffb4 | 2013-04-16 15:55:53 -0700 | [diff] [blame] | 4355 | struct stream_in *in = (struct stream_in *)stream; |
Eric Laurent | cefbbac | 2014-09-04 13:54:10 -0500 | [diff] [blame] | 4356 | struct audio_device *adev = in->dev; |
Ravi Kumar Alamanda | f70ffb4 | 2013-04-16 15:55:53 -0700 | [diff] [blame] | 4357 | int status = 0; |
| 4358 | effect_descriptor_t desc; |
| 4359 | |
| 4360 | status = (*effect)->get_descriptor(effect, &desc); |
Vignesh Kulothungan | 6469882 | 2018-01-23 11:25:18 -0800 | [diff] [blame] | 4361 | ALOGV("%s: status %d in->standby %d enable:%d", __func__, status, in->standby, enable); |
| 4362 | |
Ravi Kumar Alamanda | f70ffb4 | 2013-04-16 15:55:53 -0700 | [diff] [blame] | 4363 | if (status != 0) |
| 4364 | return status; |
| 4365 | |
Eric Laurent | a147807 | 2015-09-21 17:21:52 -0700 | [diff] [blame] | 4366 | lock_input_stream(in); |
Ravi Kumar Alamanda | f70ffb4 | 2013-04-16 15:55:53 -0700 | [diff] [blame] | 4367 | pthread_mutex_lock(&in->dev->lock); |
Eric Laurent | 50a38ed | 2015-10-14 18:48:06 -0700 | [diff] [blame] | 4368 | if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION || |
vivek mehta | 733c1df | 2016-04-04 15:09:24 -0700 | [diff] [blame] | 4369 | in->source == AUDIO_SOURCE_VOICE_RECOGNITION || |
Eric Laurent | 50a38ed | 2015-10-14 18:48:06 -0700 | [diff] [blame] | 4370 | adev->mode == AUDIO_MODE_IN_COMMUNICATION) && |
Ravi Kumar Alamanda | f70ffb4 | 2013-04-16 15:55:53 -0700 | [diff] [blame] | 4371 | in->enable_aec != enable && |
| 4372 | (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) { |
| 4373 | in->enable_aec = enable; |
Eric Laurent | cefbbac | 2014-09-04 13:54:10 -0500 | [diff] [blame] | 4374 | if (!enable) |
| 4375 | platform_set_echo_reference(in->dev, enable, AUDIO_DEVICE_NONE); |
vivek mehta | 733c1df | 2016-04-04 15:09:24 -0700 | [diff] [blame] | 4376 | if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION || |
| 4377 | adev->mode == AUDIO_MODE_IN_COMMUNICATION) { |
| 4378 | adev->enable_voicerx = enable; |
| 4379 | struct audio_usecase *usecase; |
| 4380 | struct listnode *node; |
| 4381 | list_for_each(node, &adev->usecase_list) { |
| 4382 | usecase = node_to_item(node, struct audio_usecase, list); |
vivek mehta | c07684b | 2017-09-15 13:31:12 -0700 | [diff] [blame] | 4383 | if (usecase->type == PCM_PLAYBACK) |
vivek mehta | 733c1df | 2016-04-04 15:09:24 -0700 | [diff] [blame] | 4384 | select_devices(adev, usecase->id); |
Eric Laurent | cefbbac | 2014-09-04 13:54:10 -0500 | [diff] [blame] | 4385 | } |
| 4386 | } |
Vignesh Kulothungan | 6469882 | 2018-01-23 11:25:18 -0800 | [diff] [blame] | 4387 | if (!in->standby |
| 4388 | && enable_disable_effect(in->dev, in, EFFECT_AEC, enable) == -ENOSYS) |
Ravi Kumar Alamanda | f70ffb4 | 2013-04-16 15:55:53 -0700 | [diff] [blame] | 4389 | select_devices(in->dev, in->usecase); |
| 4390 | } |
Ravi Kumar Alamanda | 3ad4e1b | 2014-06-03 00:08:15 -0700 | [diff] [blame] | 4391 | if (in->enable_ns != enable && |
| 4392 | (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) { |
| 4393 | in->enable_ns = enable; |
Vignesh Kulothungan | 6469882 | 2018-01-23 11:25:18 -0800 | [diff] [blame] | 4394 | if (!in->standby) { |
| 4395 | if (in->source != AUDIO_SOURCE_VOICE_COMMUNICATION |
| 4396 | || enable_disable_effect(in->dev, in, EFFECT_NS, enable) == -ENOSYS) |
| 4397 | select_devices(in->dev, in->usecase); |
| 4398 | } |
Ravi Kumar Alamanda | 3ad4e1b | 2014-06-03 00:08:15 -0700 | [diff] [blame] | 4399 | } |
Ravi Kumar Alamanda | f70ffb4 | 2013-04-16 15:55:53 -0700 | [diff] [blame] | 4400 | pthread_mutex_unlock(&in->dev->lock); |
| 4401 | pthread_mutex_unlock(&in->lock); |
| 4402 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4403 | return 0; |
| 4404 | } |
| 4405 | |
Ravi Kumar Alamanda | f70ffb4 | 2013-04-16 15:55:53 -0700 | [diff] [blame] | 4406 | static int in_add_audio_effect(const struct audio_stream *stream, |
| 4407 | effect_handle_t effect) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4408 | { |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 4409 | ALOGV("%s: effect %p", __func__, effect); |
Ravi Kumar Alamanda | f70ffb4 | 2013-04-16 15:55:53 -0700 | [diff] [blame] | 4410 | return add_remove_audio_effect(stream, effect, true); |
| 4411 | } |
| 4412 | |
| 4413 | static int in_remove_audio_effect(const struct audio_stream *stream, |
| 4414 | effect_handle_t effect) |
| 4415 | { |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 4416 | ALOGV("%s: effect %p", __func__, effect); |
Ravi Kumar Alamanda | f70ffb4 | 2013-04-16 15:55:53 -0700 | [diff] [blame] | 4417 | return add_remove_audio_effect(stream, effect, false); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4418 | } |
| 4419 | |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 4420 | static int in_stop(const struct audio_stream_in* stream) |
| 4421 | { |
| 4422 | struct stream_in *in = (struct stream_in *)stream; |
| 4423 | struct audio_device *adev = in->dev; |
| 4424 | |
| 4425 | int ret = -ENOSYS; |
| 4426 | ALOGV("%s", __func__); |
| 4427 | pthread_mutex_lock(&adev->lock); |
| 4428 | if (in->usecase == USECASE_AUDIO_RECORD_MMAP && !in->standby && |
| 4429 | in->capture_started && in->pcm != NULL) { |
| 4430 | pcm_stop(in->pcm); |
| 4431 | ret = stop_input_stream(in); |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 4432 | in->capture_started = false; |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 4433 | } |
| 4434 | pthread_mutex_unlock(&adev->lock); |
| 4435 | return ret; |
| 4436 | } |
| 4437 | |
| 4438 | static int in_start(const struct audio_stream_in* stream) |
| 4439 | { |
| 4440 | struct stream_in *in = (struct stream_in *)stream; |
| 4441 | struct audio_device *adev = in->dev; |
| 4442 | int ret = -ENOSYS; |
| 4443 | |
| 4444 | ALOGV("%s in %p", __func__, in); |
| 4445 | pthread_mutex_lock(&adev->lock); |
| 4446 | if (in->usecase == USECASE_AUDIO_RECORD_MMAP && !in->standby && |
| 4447 | !in->capture_started && in->pcm != NULL) { |
| 4448 | if (!in->capture_started) { |
| 4449 | ret = start_input_stream(in); |
| 4450 | if (ret == 0) { |
| 4451 | in->capture_started = true; |
| 4452 | } |
| 4453 | } |
| 4454 | } |
| 4455 | pthread_mutex_unlock(&adev->lock); |
| 4456 | return ret; |
| 4457 | } |
| 4458 | |
Phil Burk | 8a6a165 | 2019-03-25 10:15:59 -0700 | [diff] [blame] | 4459 | // Read offset for the positional timestamp from a persistent vendor property. |
| 4460 | // This is to workaround apparent inaccuracies in the timing information that |
| 4461 | // is used by the AAudio timing model. The inaccuracies can cause glitches. |
Phil Burk | c4714fc | 2019-02-16 22:28:11 -0800 | [diff] [blame] | 4462 | static int64_t in_get_mmap_time_offset() { |
Phil Burk | 8a6a165 | 2019-03-25 10:15:59 -0700 | [diff] [blame] | 4463 | const int32_t kDefaultOffsetMicros = 0; |
Phil Burk | c4714fc | 2019-02-16 22:28:11 -0800 | [diff] [blame] | 4464 | int32_t mmap_time_offset_micros = property_get_int32( |
Phil Burk | 8a6a165 | 2019-03-25 10:15:59 -0700 | [diff] [blame] | 4465 | "persist.audio.in_mmap_delay_micros", kDefaultOffsetMicros); |
Phil Burk | c4714fc | 2019-02-16 22:28:11 -0800 | [diff] [blame] | 4466 | ALOGI("in_get_mmap_time_offset set to %d micros", mmap_time_offset_micros); |
| 4467 | return mmap_time_offset_micros * (int64_t)1000; |
| 4468 | } |
| 4469 | |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 4470 | static int in_create_mmap_buffer(const struct audio_stream_in *stream, |
| 4471 | int32_t min_size_frames, |
| 4472 | struct audio_mmap_buffer_info *info) |
| 4473 | { |
| 4474 | struct stream_in *in = (struct stream_in *)stream; |
| 4475 | struct audio_device *adev = in->dev; |
| 4476 | int ret = 0; |
| 4477 | unsigned int offset1; |
| 4478 | unsigned int frames1; |
| 4479 | const char *step = ""; |
Haynes Mathew George | 96483a2 | 2017-03-28 14:52:47 -0700 | [diff] [blame] | 4480 | uint32_t mmap_size; |
| 4481 | uint32_t buffer_size; |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 4482 | |
Haynes Mathew George | f1aaf36 | 2017-12-18 15:10:10 -0800 | [diff] [blame] | 4483 | lock_input_stream(in); |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 4484 | pthread_mutex_lock(&adev->lock); |
| 4485 | ALOGV("%s in %p", __func__, in); |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 4486 | |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 4487 | if (info == NULL || min_size_frames == 0) { |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 4488 | ALOGE("%s invalid argument info %p min_size_frames %d", __func__, info, min_size_frames); |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 4489 | ret = -EINVAL; |
| 4490 | goto exit; |
| 4491 | } |
| 4492 | if (in->usecase != USECASE_AUDIO_RECORD_MMAP || !in->standby) { |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 4493 | ALOGE("%s: usecase = %d, standby = %d", __func__, in->usecase, in->standby); |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 4494 | ALOGV("%s in %p", __func__, in); |
| 4495 | ret = -ENOSYS; |
| 4496 | goto exit; |
| 4497 | } |
| 4498 | in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE); |
| 4499 | if (in->pcm_device_id < 0) { |
| 4500 | ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)", |
| 4501 | __func__, in->pcm_device_id, in->usecase); |
| 4502 | ret = -EINVAL; |
| 4503 | goto exit; |
| 4504 | } |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 4505 | |
| 4506 | adjust_mmap_period_count(&in->config, min_size_frames); |
| 4507 | |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 4508 | ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d", |
| 4509 | __func__, adev->snd_card, in->pcm_device_id, in->config.channels); |
| 4510 | in->pcm = pcm_open(adev->snd_card, in->pcm_device_id, |
| 4511 | (PCM_IN | PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC), &in->config); |
| 4512 | if (in->pcm == NULL || !pcm_is_ready(in->pcm)) { |
| 4513 | step = "open"; |
| 4514 | ret = -ENODEV; |
| 4515 | goto exit; |
| 4516 | } |
| 4517 | |
| 4518 | ret = pcm_mmap_begin(in->pcm, &info->shared_memory_address, &offset1, &frames1); |
| 4519 | if (ret < 0) { |
| 4520 | step = "begin"; |
| 4521 | goto exit; |
| 4522 | } |
| 4523 | info->buffer_size_frames = pcm_get_buffer_size(in->pcm); |
Haynes Mathew George | 96483a2 | 2017-03-28 14:52:47 -0700 | [diff] [blame] | 4524 | buffer_size = pcm_frames_to_bytes(in->pcm, info->buffer_size_frames); |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 4525 | info->burst_size_frames = in->config.period_size; |
Haynes Mathew George | 96483a2 | 2017-03-28 14:52:47 -0700 | [diff] [blame] | 4526 | ret = platform_get_mmap_data_fd(adev->platform, |
| 4527 | in->pcm_device_id, 1 /*capture*/, |
| 4528 | &info->shared_memory_fd, |
| 4529 | &mmap_size); |
| 4530 | if (ret < 0) { |
| 4531 | // Fall back to non exclusive mode |
| 4532 | info->shared_memory_fd = pcm_get_poll_fd(in->pcm); |
| 4533 | } else { |
| 4534 | if (mmap_size < buffer_size) { |
| 4535 | step = "mmap"; |
| 4536 | goto exit; |
| 4537 | } |
| 4538 | // FIXME: indicate exclusive mode support by returning a negative buffer size |
| 4539 | info->buffer_size_frames *= -1; |
| 4540 | } |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 4541 | |
Haynes Mathew George | 96483a2 | 2017-03-28 14:52:47 -0700 | [diff] [blame] | 4542 | memset(info->shared_memory_address, 0, buffer_size); |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 4543 | |
| 4544 | ret = pcm_mmap_commit(in->pcm, 0, MMAP_PERIOD_SIZE); |
| 4545 | if (ret < 0) { |
| 4546 | step = "commit"; |
| 4547 | goto exit; |
| 4548 | } |
| 4549 | |
Phil Burk | c4714fc | 2019-02-16 22:28:11 -0800 | [diff] [blame] | 4550 | in->mmap_time_offset_nanos = in_get_mmap_time_offset(); |
| 4551 | |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 4552 | in->standby = false; |
| 4553 | ret = 0; |
| 4554 | |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 4555 | ALOGV("%s: got mmap buffer address %p info->buffer_size_frames %d", |
| 4556 | __func__, info->shared_memory_address, info->buffer_size_frames); |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 4557 | |
| 4558 | exit: |
| 4559 | if (ret != 0) { |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 4560 | if (in->pcm == NULL) { |
| 4561 | ALOGE("%s: %s - %d", __func__, step, ret); |
| 4562 | } else { |
| 4563 | ALOGE("%s: %s %s", __func__, step, pcm_get_error(in->pcm)); |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 4564 | pcm_close(in->pcm); |
| 4565 | in->pcm = NULL; |
| 4566 | } |
| 4567 | } |
| 4568 | pthread_mutex_unlock(&adev->lock); |
Haynes Mathew George | f1aaf36 | 2017-12-18 15:10:10 -0800 | [diff] [blame] | 4569 | pthread_mutex_unlock(&in->lock); |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 4570 | return ret; |
| 4571 | } |
| 4572 | |
| 4573 | static int in_get_mmap_position(const struct audio_stream_in *stream, |
| 4574 | struct audio_mmap_position *position) |
| 4575 | { |
Haynes Mathew George | f1aaf36 | 2017-12-18 15:10:10 -0800 | [diff] [blame] | 4576 | int ret = 0; |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 4577 | struct stream_in *in = (struct stream_in *)stream; |
| 4578 | ALOGVV("%s", __func__); |
| 4579 | if (position == NULL) { |
| 4580 | return -EINVAL; |
| 4581 | } |
Haynes Mathew George | f1aaf36 | 2017-12-18 15:10:10 -0800 | [diff] [blame] | 4582 | lock_input_stream(in); |
| 4583 | if (in->usecase != USECASE_AUDIO_RECORD_MMAP || |
| 4584 | in->pcm == NULL) { |
| 4585 | ret = -ENOSYS; |
| 4586 | goto exit; |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 4587 | } |
| 4588 | struct timespec ts = { 0, 0 }; |
Haynes Mathew George | f1aaf36 | 2017-12-18 15:10:10 -0800 | [diff] [blame] | 4589 | ret = pcm_mmap_get_hw_ptr(in->pcm, (unsigned int *)&position->position_frames, &ts); |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 4590 | if (ret < 0) { |
| 4591 | ALOGE("%s: %s", __func__, pcm_get_error(in->pcm)); |
Haynes Mathew George | f1aaf36 | 2017-12-18 15:10:10 -0800 | [diff] [blame] | 4592 | goto exit; |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 4593 | } |
Phil Burk | 8a6a165 | 2019-03-25 10:15:59 -0700 | [diff] [blame] | 4594 | position->time_nanoseconds = audio_utils_ns_from_timespec(&ts) |
| 4595 | + in->mmap_time_offset_nanos; |
Phil Burk | c4714fc | 2019-02-16 22:28:11 -0800 | [diff] [blame] | 4596 | |
Haynes Mathew George | f1aaf36 | 2017-12-18 15:10:10 -0800 | [diff] [blame] | 4597 | exit: |
| 4598 | pthread_mutex_unlock(&in->lock); |
| 4599 | return ret; |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 4600 | } |
| 4601 | |
jiabin | 8962a4d | 2018-03-19 18:21:24 -0700 | [diff] [blame] | 4602 | static int in_get_active_microphones(const struct audio_stream_in *stream, |
| 4603 | struct audio_microphone_characteristic_t *mic_array, |
| 4604 | size_t *mic_count) { |
| 4605 | struct stream_in *in = (struct stream_in *)stream; |
| 4606 | struct audio_device *adev = in->dev; |
| 4607 | ALOGVV("%s", __func__); |
| 4608 | |
| 4609 | lock_input_stream(in); |
| 4610 | pthread_mutex_lock(&adev->lock); |
Aniket Kumar Lata | debab2b | 2018-04-30 20:20:25 -0700 | [diff] [blame] | 4611 | int ret = platform_get_active_microphones(adev->platform, |
jiabin | 8962a4d | 2018-03-19 18:21:24 -0700 | [diff] [blame] | 4612 | audio_channel_count_from_in_mask(in->channel_mask), |
Aniket Kumar Lata | debab2b | 2018-04-30 20:20:25 -0700 | [diff] [blame] | 4613 | in->usecase, mic_array, mic_count); |
jiabin | 8962a4d | 2018-03-19 18:21:24 -0700 | [diff] [blame] | 4614 | pthread_mutex_unlock(&adev->lock); |
| 4615 | pthread_mutex_unlock(&in->lock); |
| 4616 | |
| 4617 | return ret; |
| 4618 | } |
| 4619 | |
| 4620 | static int adev_get_microphones(const struct audio_hw_device *dev, |
| 4621 | struct audio_microphone_characteristic_t *mic_array, |
| 4622 | size_t *mic_count) { |
| 4623 | struct audio_device *adev = (struct audio_device *)dev; |
| 4624 | ALOGVV("%s", __func__); |
| 4625 | |
| 4626 | pthread_mutex_lock(&adev->lock); |
| 4627 | int ret = platform_get_microphones(adev->platform, mic_array, mic_count); |
| 4628 | pthread_mutex_unlock(&adev->lock); |
| 4629 | |
| 4630 | return ret; |
| 4631 | } |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 4632 | |
Paul McLean | 57530d5 | 2018-12-17 08:24:21 -0700 | [diff] [blame] | 4633 | static int in_set_microphone_direction(const struct audio_stream_in *stream, |
| 4634 | audio_microphone_direction_t dir) { |
justinweng | c6347db | 2019-02-21 18:49:00 +0800 | [diff] [blame] | 4635 | struct stream_in *in = (struct stream_in *)stream; |
| 4636 | |
| 4637 | ALOGVV("%s: standby %d source %d dir %d", __func__, in->standby, in->source, dir); |
| 4638 | |
| 4639 | in->direction = dir; |
| 4640 | |
| 4641 | if (in->standby) |
| 4642 | return 0; |
| 4643 | |
| 4644 | return audio_extn_audiozoom_set_microphone_direction(in, dir); |
Paul McLean | 57530d5 | 2018-12-17 08:24:21 -0700 | [diff] [blame] | 4645 | } |
| 4646 | |
| 4647 | static int in_set_microphone_field_dimension(const struct audio_stream_in *stream, float zoom) { |
justinweng | c6347db | 2019-02-21 18:49:00 +0800 | [diff] [blame] | 4648 | struct stream_in *in = (struct stream_in *)stream; |
| 4649 | |
| 4650 | ALOGVV("%s: standby %d source %d zoom %f", __func__, in->standby, in->source, zoom); |
| 4651 | |
| 4652 | if (zoom > 1.0 || zoom < -1.0) |
| 4653 | return -EINVAL; |
| 4654 | |
| 4655 | in->zoom = zoom; |
| 4656 | |
| 4657 | if (in->standby) |
| 4658 | return 0; |
| 4659 | |
| 4660 | return audio_extn_audiozoom_set_microphone_field_dimension(in, zoom); |
Paul McLean | 57530d5 | 2018-12-17 08:24:21 -0700 | [diff] [blame] | 4661 | } |
| 4662 | |
juyuchen | ba338cd | 2019-01-21 11:57:17 +0800 | [diff] [blame] | 4663 | static void in_update_sink_metadata(struct audio_stream_in *stream, |
| 4664 | const struct sink_metadata *sink_metadata) { |
| 4665 | |
| 4666 | if (stream == NULL |
| 4667 | || sink_metadata == NULL |
| 4668 | || sink_metadata->tracks == NULL) { |
| 4669 | return; |
| 4670 | } |
| 4671 | |
| 4672 | int error = 0; |
| 4673 | struct stream_in *in = (struct stream_in *)stream; |
| 4674 | struct audio_device *adev = in->dev; |
| 4675 | audio_devices_t device = AUDIO_DEVICE_NONE; |
| 4676 | |
| 4677 | if (sink_metadata->track_count != 0) |
| 4678 | device = sink_metadata->tracks->dest_device; |
| 4679 | |
| 4680 | lock_input_stream(in); |
| 4681 | pthread_mutex_lock(&adev->lock); |
| 4682 | ALOGV("%s: in->usecase: %d, device: %x", __func__, in->usecase, device); |
| 4683 | |
| 4684 | if (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY |
| 4685 | && device != AUDIO_DEVICE_NONE |
| 4686 | && adev->voice_tx_output != NULL) { |
| 4687 | /* Use the rx device from afe-proxy record to route voice call because |
| 4688 | there is no routing if tx device is on primary hal and rx device |
| 4689 | is on other hal during voice call. */ |
| 4690 | adev->voice_tx_output->devices = device; |
| 4691 | |
| 4692 | if (!voice_is_call_state_active(adev)) { |
| 4693 | if (adev->mode == AUDIO_MODE_IN_CALL) { |
| 4694 | adev->current_call_output = adev->voice_tx_output; |
| 4695 | error = voice_start_call(adev); |
| 4696 | if (error != 0) |
| 4697 | ALOGE("%s: start voice call failed %d", __func__, error); |
| 4698 | } |
| 4699 | } else { |
| 4700 | adev->current_call_output = adev->voice_tx_output; |
| 4701 | voice_update_devices_for_all_voice_usecases(adev); |
| 4702 | } |
| 4703 | } |
| 4704 | |
| 4705 | pthread_mutex_unlock(&adev->lock); |
| 4706 | pthread_mutex_unlock(&in->lock); |
| 4707 | } |
| 4708 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4709 | static int adev_open_output_stream(struct audio_hw_device *dev, |
| 4710 | audio_io_handle_t handle, |
| 4711 | audio_devices_t devices, |
| 4712 | audio_output_flags_t flags, |
| 4713 | struct audio_config *config, |
Eric Laurent | 91975a6 | 2014-07-27 17:15:50 -0700 | [diff] [blame] | 4714 | struct audio_stream_out **stream_out, |
| 4715 | const char *address __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4716 | { |
| 4717 | struct audio_device *adev = (struct audio_device *)dev; |
| 4718 | struct stream_out *out; |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 4719 | int i, ret = 0; |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 4720 | bool is_hdmi = devices & AUDIO_DEVICE_OUT_AUX_DIGITAL; |
| 4721 | bool is_usb_dev = audio_is_usb_out_device(devices) && |
| 4722 | (devices != AUDIO_DEVICE_OUT_USB_ACCESSORY); |
Vignesh Kulothungan | 019d19b | 2019-01-23 11:09:18 -0800 | [diff] [blame] | 4723 | bool force_haptic_path = |
| 4724 | property_get_bool("vendor.audio.test_haptic", false); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4725 | |
Andy Hung | d9653bd | 2017-08-01 19:31:39 -0700 | [diff] [blame] | 4726 | if (is_usb_dev && !is_usb_ready(adev, true /* is_playback */)) { |
| 4727 | return -ENOSYS; |
| 4728 | } |
| 4729 | |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 4730 | ALOGV("%s: enter: format(%#x) sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)", |
| 4731 | __func__, config->format, config->sample_rate, config->channel_mask, devices, flags); |
Vignesh Kulothungan | 019d19b | 2019-01-23 11:09:18 -0800 | [diff] [blame] | 4732 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4733 | *stream_out = NULL; |
| 4734 | out = (struct stream_out *)calloc(1, sizeof(struct stream_out)); |
| 4735 | |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 4736 | pthread_mutex_init(&out->compr_mute_lock, (const pthread_mutexattr_t *) NULL); |
| 4737 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4738 | if (devices == AUDIO_DEVICE_NONE) |
| 4739 | devices = AUDIO_DEVICE_OUT_SPEAKER; |
| 4740 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4741 | out->flags = flags; |
| 4742 | out->devices = devices; |
Haynes Mathew George | 47cd4cb | 2013-07-19 11:58:50 -0700 | [diff] [blame] | 4743 | out->dev = adev; |
Eric Laurent | c4aef75 | 2013-09-12 17:45:53 -0700 | [diff] [blame] | 4744 | out->handle = handle; |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 4745 | out->a2dp_compress_mute = false; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4746 | |
| 4747 | /* Init use case and pcm_config */ |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 4748 | if ((is_hdmi || is_usb_dev) && |
| 4749 | (audio_is_linear_pcm(config->format) || config->format == AUDIO_FORMAT_DEFAULT) && |
| 4750 | (flags == AUDIO_OUTPUT_FLAG_NONE || |
| 4751 | (flags & AUDIO_OUTPUT_FLAG_DIRECT) != 0)) { |
Eric Laurent | ab805ee | 2018-03-30 12:20:38 -0700 | [diff] [blame] | 4752 | audio_format_t req_format = config->format; |
| 4753 | audio_channel_mask_t req_channel_mask = config->channel_mask; |
| 4754 | uint32_t req_sample_rate = config->sample_rate; |
| 4755 | |
Ravi Kumar Alamanda | b199506 | 2013-03-21 23:18:20 -0700 | [diff] [blame] | 4756 | pthread_mutex_lock(&adev->lock); |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 4757 | if (is_hdmi) { |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 4758 | ret = read_hdmi_channel_masks(out); |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 4759 | if (config->sample_rate == 0) |
| 4760 | config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE; |
Eric Laurent | ab805ee | 2018-03-30 12:20:38 -0700 | [diff] [blame] | 4761 | if (config->channel_mask == AUDIO_CHANNEL_NONE) |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 4762 | config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1; |
| 4763 | if (config->format == AUDIO_FORMAT_DEFAULT) |
| 4764 | config->format = AUDIO_FORMAT_PCM_16_BIT; |
| 4765 | } else if (is_usb_dev) { |
| 4766 | ret = read_usb_sup_params_and_compare(true /*is_playback*/, |
| 4767 | &config->format, |
| 4768 | &out->supported_formats[0], |
| 4769 | MAX_SUPPORTED_FORMATS, |
| 4770 | &config->channel_mask, |
| 4771 | &out->supported_channel_masks[0], |
| 4772 | MAX_SUPPORTED_CHANNEL_MASKS, |
| 4773 | &config->sample_rate, |
| 4774 | &out->supported_sample_rates[0], |
| 4775 | MAX_SUPPORTED_SAMPLE_RATES); |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 4776 | ALOGV("plugged dev USB ret %d", ret); |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 4777 | } |
Ravi Kumar Alamanda | b199506 | 2013-03-21 23:18:20 -0700 | [diff] [blame] | 4778 | pthread_mutex_unlock(&adev->lock); |
Eric Laurent | ab805ee | 2018-03-30 12:20:38 -0700 | [diff] [blame] | 4779 | if (ret != 0) { |
| 4780 | // For MMAP NO IRQ, allow conversions in ADSP |
| 4781 | if (is_hdmi || (flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) == 0) |
| 4782 | goto error_open; |
Ravi Kumar Alamanda | b199506 | 2013-03-21 23:18:20 -0700 | [diff] [blame] | 4783 | |
Eric Laurent | ab805ee | 2018-03-30 12:20:38 -0700 | [diff] [blame] | 4784 | if (req_sample_rate != 0 && config->sample_rate != req_sample_rate) |
| 4785 | config->sample_rate = req_sample_rate; |
| 4786 | if (req_channel_mask != AUDIO_CHANNEL_NONE && config->channel_mask != req_channel_mask) |
| 4787 | config->channel_mask = req_channel_mask; |
| 4788 | if (req_format != AUDIO_FORMAT_DEFAULT && config->format != req_format) |
| 4789 | config->format = req_format; |
| 4790 | } |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 4791 | |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 4792 | out->sample_rate = config->sample_rate; |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 4793 | out->channel_mask = config->channel_mask; |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 4794 | out->format = config->format; |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 4795 | if (is_hdmi) { |
| 4796 | out->usecase = USECASE_AUDIO_PLAYBACK_HIFI; |
| 4797 | out->config = pcm_config_hdmi_multi; |
| 4798 | } else if (flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) { |
| 4799 | out->usecase = USECASE_AUDIO_PLAYBACK_MMAP; |
| 4800 | out->config = pcm_config_mmap_playback; |
| 4801 | out->stream.start = out_start; |
| 4802 | out->stream.stop = out_stop; |
| 4803 | out->stream.create_mmap_buffer = out_create_mmap_buffer; |
| 4804 | out->stream.get_mmap_position = out_get_mmap_position; |
| 4805 | } else { |
| 4806 | out->usecase = USECASE_AUDIO_PLAYBACK_HIFI; |
| 4807 | out->config = pcm_config_hifi; |
| 4808 | } |
| 4809 | |
| 4810 | out->config.rate = out->sample_rate; |
Eric Laurent | 0de8d1f | 2014-07-01 20:34:45 -0700 | [diff] [blame] | 4811 | out->config.channels = audio_channel_count_from_out_mask(out->channel_mask); |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 4812 | if (is_hdmi) { |
| 4813 | out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * |
| 4814 | audio_bytes_per_sample(out->format)); |
| 4815 | } |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 4816 | out->config.format = pcm_format_from_audio_format(out->format); |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 4817 | } else if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { |
Haynes Mathew George | 3580769 | 2016-07-07 20:04:54 -0700 | [diff] [blame] | 4818 | pthread_mutex_lock(&adev->lock); |
| 4819 | bool offline = (adev->card_status == CARD_STATUS_OFFLINE); |
| 4820 | pthread_mutex_unlock(&adev->lock); |
| 4821 | |
| 4822 | // reject offload during card offline to allow |
| 4823 | // fallback to s/w paths |
| 4824 | if (offline) { |
| 4825 | ret = -ENODEV; |
| 4826 | goto error_open; |
| 4827 | } |
| 4828 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 4829 | if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version || |
| 4830 | config->offload_info.size != AUDIO_INFO_INITIALIZER.size) { |
| 4831 | ALOGE("%s: Unsupported Offload information", __func__); |
| 4832 | ret = -EINVAL; |
| 4833 | goto error_open; |
| 4834 | } |
| 4835 | if (!is_supported_format(config->offload_info.format)) { |
| 4836 | ALOGE("%s: Unsupported audio format", __func__); |
| 4837 | ret = -EINVAL; |
| 4838 | goto error_open; |
| 4839 | } |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 4840 | out->sample_rate = config->offload_info.sample_rate; |
| 4841 | if (config->offload_info.channel_mask != AUDIO_CHANNEL_NONE) |
| 4842 | out->channel_mask = config->offload_info.channel_mask; |
| 4843 | else if (config->channel_mask != AUDIO_CHANNEL_NONE) |
| 4844 | out->channel_mask = config->channel_mask; |
| 4845 | else |
| 4846 | out->channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 4847 | |
| 4848 | out->format = config->offload_info.format; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 4849 | |
| 4850 | out->compr_config.codec = (struct snd_codec *) |
| 4851 | calloc(1, sizeof(struct snd_codec)); |
| 4852 | |
| 4853 | out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 4854 | |
| 4855 | out->stream.set_callback = out_set_callback; |
| 4856 | out->stream.pause = out_pause; |
| 4857 | out->stream.resume = out_resume; |
| 4858 | out->stream.drain = out_drain; |
| 4859 | out->stream.flush = out_flush; |
| 4860 | |
| 4861 | out->compr_config.codec->id = |
| 4862 | get_snd_codec_id(config->offload_info.format); |
| 4863 | out->compr_config.fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE; |
| 4864 | out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS; |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 4865 | out->compr_config.codec->sample_rate = out->sample_rate; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 4866 | out->compr_config.codec->bit_rate = |
| 4867 | config->offload_info.bit_rate; |
| 4868 | out->compr_config.codec->ch_in = |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 4869 | audio_channel_count_from_out_mask(out->channel_mask); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 4870 | out->compr_config.codec->ch_out = out->compr_config.codec->ch_in; |
| 4871 | |
| 4872 | if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING) |
| 4873 | out->non_blocking = 1; |
Haynes Mathew George | 352f27b | 2013-07-26 00:00:15 -0700 | [diff] [blame] | 4874 | |
| 4875 | out->send_new_metadata = 1; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 4876 | create_offload_callback_thread(out); |
| 4877 | ALOGV("%s: offloaded output offload_info version %04x bit rate %d", |
| 4878 | __func__, config->offload_info.version, |
| 4879 | config->offload_info.bit_rate); |
Nadav Bar | 3d72cfc | 2018-01-07 12:19:24 +0200 | [diff] [blame] | 4880 | } else if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) { |
| 4881 | switch (config->sample_rate) { |
| 4882 | case 0: |
| 4883 | out->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE; |
| 4884 | break; |
| 4885 | case 8000: |
| 4886 | case 16000: |
| 4887 | case 48000: |
| 4888 | out->sample_rate = config->sample_rate; |
| 4889 | break; |
| 4890 | default: |
| 4891 | ALOGE("%s: Unsupported sampling rate %d for Incall Music", __func__, |
| 4892 | config->sample_rate); |
| 4893 | config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE; |
| 4894 | ret = -EINVAL; |
| 4895 | goto error_open; |
| 4896 | } |
| 4897 | //FIXME: add support for MONO stream configuration when audioflinger mixer supports it |
| 4898 | switch (config->channel_mask) { |
| 4899 | case AUDIO_CHANNEL_NONE: |
| 4900 | case AUDIO_CHANNEL_OUT_STEREO: |
| 4901 | out->channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 4902 | break; |
| 4903 | default: |
| 4904 | ALOGE("%s: Unsupported channel mask %#x for Incall Music", __func__, |
| 4905 | config->channel_mask); |
| 4906 | config->channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 4907 | ret = -EINVAL; |
| 4908 | goto error_open; |
| 4909 | } |
| 4910 | switch (config->format) { |
| 4911 | case AUDIO_FORMAT_DEFAULT: |
| 4912 | case AUDIO_FORMAT_PCM_16_BIT: |
| 4913 | out->format = AUDIO_FORMAT_PCM_16_BIT; |
| 4914 | break; |
| 4915 | default: |
| 4916 | ALOGE("%s: Unsupported format %#x for Incall Music", __func__, |
| 4917 | config->format); |
| 4918 | config->format = AUDIO_FORMAT_PCM_16_BIT; |
| 4919 | ret = -EINVAL; |
| 4920 | goto error_open; |
| 4921 | } |
| 4922 | |
| 4923 | voice_extn_check_and_set_incall_music_usecase(adev, out); |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 4924 | } else if (out->devices == AUDIO_DEVICE_OUT_TELEPHONY_TX) { |
Kevin Rocard | 50eff89 | 2017-05-02 16:52:05 -0700 | [diff] [blame] | 4925 | switch (config->sample_rate) { |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 4926 | case 0: |
| 4927 | out->sample_rate = AFE_PROXY_SAMPLING_RATE; |
| 4928 | break; |
Kevin Rocard | 50eff89 | 2017-05-02 16:52:05 -0700 | [diff] [blame] | 4929 | case 8000: |
| 4930 | case 16000: |
| 4931 | case 48000: |
| 4932 | out->sample_rate = config->sample_rate; |
| 4933 | break; |
| 4934 | default: |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 4935 | ALOGE("%s: Unsupported sampling rate %d for Telephony TX", __func__, |
| 4936 | config->sample_rate); |
| 4937 | config->sample_rate = AFE_PROXY_SAMPLING_RATE; |
| 4938 | ret = -EINVAL; |
| 4939 | break; |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 4940 | } |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 4941 | //FIXME: add support for MONO stream configuration when audioflinger mixer supports it |
| 4942 | switch (config->channel_mask) { |
| 4943 | case AUDIO_CHANNEL_NONE: |
| 4944 | out->channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 4945 | break; |
| 4946 | case AUDIO_CHANNEL_OUT_STEREO: |
| 4947 | out->channel_mask = config->channel_mask; |
| 4948 | break; |
| 4949 | default: |
| 4950 | ALOGE("%s: Unsupported channel mask %#x for Telephony TX", __func__, |
| 4951 | config->channel_mask); |
| 4952 | config->channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 4953 | ret = -EINVAL; |
| 4954 | break; |
| 4955 | } |
| 4956 | switch (config->format) { |
| 4957 | case AUDIO_FORMAT_DEFAULT: |
| 4958 | out->format = AUDIO_FORMAT_PCM_16_BIT; |
| 4959 | break; |
| 4960 | case AUDIO_FORMAT_PCM_16_BIT: |
| 4961 | out->format = config->format; |
| 4962 | break; |
| 4963 | default: |
| 4964 | ALOGE("%s: Unsupported format %#x for Telephony TX", __func__, |
| 4965 | config->format); |
| 4966 | config->format = AUDIO_FORMAT_PCM_16_BIT; |
| 4967 | ret = -EINVAL; |
| 4968 | break; |
| 4969 | } |
| 4970 | if (ret != 0) |
| 4971 | goto error_open; |
| 4972 | |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 4973 | out->usecase = USECASE_AUDIO_PLAYBACK_AFE_PROXY; |
| 4974 | out->config = pcm_config_afe_proxy_playback; |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 4975 | out->config.rate = out->sample_rate; |
| 4976 | out->config.channels = |
| 4977 | audio_channel_count_from_out_mask(out->channel_mask); |
| 4978 | out->config.format = pcm_format_from_audio_format(out->format); |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 4979 | adev->voice_tx_output = out; |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 4980 | } else if (flags == AUDIO_OUTPUT_FLAG_VOIP_RX) { |
| 4981 | switch (config->sample_rate) { |
| 4982 | case 0: |
| 4983 | out->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE; |
| 4984 | break; |
| 4985 | case 8000: |
| 4986 | case 16000: |
| 4987 | case 32000: |
| 4988 | case 48000: |
| 4989 | out->sample_rate = config->sample_rate; |
| 4990 | break; |
| 4991 | default: |
| 4992 | ALOGE("%s: Unsupported sampling rate %d for Voip RX", __func__, |
| 4993 | config->sample_rate); |
| 4994 | config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE; |
| 4995 | ret = -EINVAL; |
| 4996 | break; |
| 4997 | } |
Eric Laurent | ad2dde9 | 2017-09-20 18:27:31 -0700 | [diff] [blame] | 4998 | //FIXME: add support for MONO stream configuration when audioflinger mixer supports it |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 4999 | switch (config->channel_mask) { |
| 5000 | case AUDIO_CHANNEL_NONE: |
| 5001 | out->channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 5002 | break; |
| 5003 | case AUDIO_CHANNEL_OUT_STEREO: |
| 5004 | out->channel_mask = config->channel_mask; |
| 5005 | break; |
| 5006 | default: |
| 5007 | ALOGE("%s: Unsupported channel mask %#x for Voip RX", __func__, |
| 5008 | config->channel_mask); |
| 5009 | config->channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 5010 | ret = -EINVAL; |
| 5011 | break; |
| 5012 | } |
| 5013 | switch (config->format) { |
| 5014 | case AUDIO_FORMAT_DEFAULT: |
| 5015 | out->format = AUDIO_FORMAT_PCM_16_BIT; |
| 5016 | break; |
| 5017 | case AUDIO_FORMAT_PCM_16_BIT: |
| 5018 | out->format = config->format; |
| 5019 | break; |
| 5020 | default: |
| 5021 | ALOGE("%s: Unsupported format %#x for Voip RX", __func__, |
| 5022 | config->format); |
| 5023 | config->format = AUDIO_FORMAT_PCM_16_BIT; |
| 5024 | ret = -EINVAL; |
| 5025 | break; |
| 5026 | } |
| 5027 | if (ret != 0) |
| 5028 | goto error_open; |
| 5029 | |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 5030 | uint32_t buffer_size, frame_size; |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 5031 | out->usecase = USECASE_AUDIO_PLAYBACK_VOIP; |
| 5032 | out->config = pcm_config_voip; |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 5033 | out->config.rate = out->sample_rate; |
| 5034 | out->config.format = pcm_format_from_audio_format(out->format); |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 5035 | buffer_size = get_stream_buffer_size(VOIP_PLAYBACK_PERIOD_DURATION_MSEC, |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 5036 | out->sample_rate, |
| 5037 | out->format, |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 5038 | out->config.channels, |
| 5039 | false /*is_low_latency*/); |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 5040 | frame_size = audio_bytes_per_sample(out->format) * out->config.channels; |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 5041 | out->config.period_size = buffer_size / frame_size; |
| 5042 | out->config.period_count = VOIP_PLAYBACK_PERIOD_COUNT; |
| 5043 | out->af_period_multiplier = 1; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5044 | } else { |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 5045 | if (flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) { |
Andy Hung | 6fcba9c | 2014-03-18 11:53:32 -0700 | [diff] [blame] | 5046 | out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER; |
| 5047 | out->config = pcm_config_deep_buffer; |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 5048 | } else if (flags & AUDIO_OUTPUT_FLAG_TTS) { |
Ravi Kumar Alamanda | f78a4d9 | 2015-04-24 15:18:23 -0700 | [diff] [blame] | 5049 | out->usecase = USECASE_AUDIO_PLAYBACK_TTS; |
| 5050 | out->config = pcm_config_deep_buffer; |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 5051 | } else if (flags & AUDIO_OUTPUT_FLAG_RAW) { |
Ravi Kumar Alamanda | 2bc7b02 | 2015-06-25 20:08:01 -0700 | [diff] [blame] | 5052 | out->usecase = USECASE_AUDIO_PLAYBACK_ULL; |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 5053 | out->realtime = may_use_noirq_mode(adev, USECASE_AUDIO_PLAYBACK_ULL, out->flags); |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 5054 | out->config = out->realtime ? pcm_config_rt : pcm_config_low_latency; |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 5055 | } else if (flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) { |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 5056 | out->usecase = USECASE_AUDIO_PLAYBACK_MMAP; |
| 5057 | out->config = pcm_config_mmap_playback; |
| 5058 | out->stream.start = out_start; |
| 5059 | out->stream.stop = out_stop; |
| 5060 | out->stream.create_mmap_buffer = out_create_mmap_buffer; |
| 5061 | out->stream.get_mmap_position = out_get_mmap_position; |
Andy Hung | 6fcba9c | 2014-03-18 11:53:32 -0700 | [diff] [blame] | 5062 | } else { |
Vignesh Kulothungan | 019d19b | 2019-01-23 11:09:18 -0800 | [diff] [blame] | 5063 | if (config->channel_mask & AUDIO_CHANNEL_HAPTIC_ALL) { |
| 5064 | out->usecase = USECASE_AUDIO_PLAYBACK_WITH_HAPTICS; |
| 5065 | adev->haptic_pcm_device_id = platform_get_haptics_pcm_device_id(); |
| 5066 | if (adev->haptic_pcm_device_id < 0) { |
| 5067 | ALOGE("%s: Invalid Haptics pcm device id(%d) for the usecase(%d)", |
| 5068 | __func__, adev->haptic_pcm_device_id, out->usecase); |
| 5069 | ret = -ENOSYS; |
| 5070 | goto error_open; |
| 5071 | } |
| 5072 | out->config = pcm_config_haptics_audio; |
| 5073 | if (force_haptic_path) |
| 5074 | adev->haptics_config = pcm_config_haptics_audio; |
| 5075 | else |
| 5076 | adev->haptics_config = pcm_config_haptics; |
| 5077 | } else { |
| 5078 | out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY; |
| 5079 | out->config = pcm_config_low_latency; |
| 5080 | } |
Andy Hung | 6fcba9c | 2014-03-18 11:53:32 -0700 | [diff] [blame] | 5081 | } |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 5082 | |
| 5083 | if (config->sample_rate == 0) { |
| 5084 | out->sample_rate = out->config.rate; |
| 5085 | } else { |
| 5086 | out->sample_rate = config->sample_rate; |
Andy Hung | 6fcba9c | 2014-03-18 11:53:32 -0700 | [diff] [blame] | 5087 | } |
Vignesh Kulothungan | 019d19b | 2019-01-23 11:09:18 -0800 | [diff] [blame] | 5088 | |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 5089 | if (config->channel_mask == AUDIO_CHANNEL_NONE) { |
| 5090 | out->channel_mask = audio_channel_out_mask_from_count(out->config.channels); |
| 5091 | } else { |
| 5092 | out->channel_mask = config->channel_mask; |
| 5093 | } |
Vignesh Kulothungan | 019d19b | 2019-01-23 11:09:18 -0800 | [diff] [blame] | 5094 | |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 5095 | if (config->format == AUDIO_FORMAT_DEFAULT) |
| 5096 | out->format = audio_format_from_pcm_format(out->config.format); |
| 5097 | else if (!audio_is_linear_pcm(config->format)) { |
| 5098 | config->format = AUDIO_FORMAT_PCM_16_BIT; |
| 5099 | ret = -EINVAL; |
| 5100 | goto error_open; |
| 5101 | } else { |
| 5102 | out->format = config->format; |
| 5103 | } |
| 5104 | |
| 5105 | out->config.rate = out->sample_rate; |
Vignesh Kulothungan | 019d19b | 2019-01-23 11:09:18 -0800 | [diff] [blame] | 5106 | |
| 5107 | if (config->channel_mask & AUDIO_CHANNEL_HAPTIC_ALL) { |
| 5108 | out->config.channels = |
| 5109 | audio_channel_count_from_out_mask(out->channel_mask & |
| 5110 | ~AUDIO_CHANNEL_HAPTIC_ALL); |
| 5111 | |
| 5112 | if (force_haptic_path) { |
| 5113 | out->config.channels = 1; |
| 5114 | adev->haptics_config.channels = 1; |
| 5115 | } else { |
| 5116 | adev->haptics_config.channels = |
| 5117 | audio_channel_count_from_out_mask(out->channel_mask & |
| 5118 | AUDIO_CHANNEL_HAPTIC_ALL); |
| 5119 | } |
| 5120 | } else { |
| 5121 | out->config.channels = |
| 5122 | audio_channel_count_from_out_mask(out->channel_mask); |
| 5123 | } |
| 5124 | |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 5125 | if (out->format != audio_format_from_pcm_format(out->config.format)) { |
| 5126 | out->config.format = pcm_format_from_audio_format(out->format); |
| 5127 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5128 | } |
Kevin Rocard | 50eff89 | 2017-05-02 16:52:05 -0700 | [diff] [blame] | 5129 | |
| 5130 | if ((config->sample_rate != 0 && config->sample_rate != out->sample_rate) || |
| 5131 | (config->format != AUDIO_FORMAT_DEFAULT && config->format != out->format) || |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 5132 | (config->channel_mask != AUDIO_CHANNEL_NONE && config->channel_mask != out->channel_mask)) { |
Kevin Rocard | 50eff89 | 2017-05-02 16:52:05 -0700 | [diff] [blame] | 5133 | ALOGI("%s: Unsupported output config. sample_rate:%u format:%#x channel_mask:%#x", |
| 5134 | __func__, config->sample_rate, config->format, config->channel_mask); |
| 5135 | config->sample_rate = out->sample_rate; |
| 5136 | config->format = out->format; |
| 5137 | config->channel_mask = out->channel_mask; |
| 5138 | ret = -EINVAL; |
| 5139 | goto error_open; |
| 5140 | } |
| 5141 | |
Andy Hung | 6fcba9c | 2014-03-18 11:53:32 -0700 | [diff] [blame] | 5142 | ALOGV("%s: Usecase(%s) config->format %#x out->config.format %#x\n", |
| 5143 | __func__, use_case_table[out->usecase], config->format, out->config.format); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5144 | |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 5145 | if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) { |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 5146 | if (adev->primary_output == NULL) |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 5147 | adev->primary_output = out; |
| 5148 | else { |
| 5149 | ALOGE("%s: Primary output is already opened", __func__); |
Ravi Kumar Alamanda | b199506 | 2013-03-21 23:18:20 -0700 | [diff] [blame] | 5150 | ret = -EEXIST; |
| 5151 | goto error_open; |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 5152 | } |
| 5153 | } |
| 5154 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5155 | /* Check if this usecase is already existing */ |
| 5156 | pthread_mutex_lock(&adev->lock); |
| 5157 | if (get_usecase_from_list(adev, out->usecase) != NULL) { |
| 5158 | ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5159 | pthread_mutex_unlock(&adev->lock); |
Ravi Kumar Alamanda | b199506 | 2013-03-21 23:18:20 -0700 | [diff] [blame] | 5160 | ret = -EEXIST; |
| 5161 | goto error_open; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5162 | } |
| 5163 | pthread_mutex_unlock(&adev->lock); |
| 5164 | |
| 5165 | out->stream.common.get_sample_rate = out_get_sample_rate; |
| 5166 | out->stream.common.set_sample_rate = out_set_sample_rate; |
| 5167 | out->stream.common.get_buffer_size = out_get_buffer_size; |
| 5168 | out->stream.common.get_channels = out_get_channels; |
| 5169 | out->stream.common.get_format = out_get_format; |
| 5170 | out->stream.common.set_format = out_set_format; |
| 5171 | out->stream.common.standby = out_standby; |
| 5172 | out->stream.common.dump = out_dump; |
| 5173 | out->stream.common.set_parameters = out_set_parameters; |
| 5174 | out->stream.common.get_parameters = out_get_parameters; |
| 5175 | out->stream.common.add_audio_effect = out_add_audio_effect; |
| 5176 | out->stream.common.remove_audio_effect = out_remove_audio_effect; |
| 5177 | out->stream.get_latency = out_get_latency; |
| 5178 | out->stream.set_volume = out_set_volume; |
Uday Kishore Pasupuleti | cec8ad8 | 2015-04-15 10:34:06 -0700 | [diff] [blame] | 5179 | #ifdef NO_AUDIO_OUT |
| 5180 | out->stream.write = out_write_for_no_output; |
| 5181 | #else |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5182 | out->stream.write = out_write; |
Uday Kishore Pasupuleti | cec8ad8 | 2015-04-15 10:34:06 -0700 | [diff] [blame] | 5183 | #endif |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5184 | out->stream.get_render_position = out_get_render_position; |
| 5185 | out->stream.get_next_write_timestamp = out_get_next_write_timestamp; |
Glenn Kasten | 2ccd7ba | 2013-09-10 09:04:31 -0700 | [diff] [blame] | 5186 | out->stream.get_presentation_position = out_get_presentation_position; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5187 | |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 5188 | if (out->realtime) |
| 5189 | out->af_period_multiplier = af_period_multiplier; |
| 5190 | else |
| 5191 | out->af_period_multiplier = 1; |
| 5192 | |
Andy Hung | 572633e | 2019-02-19 11:58:24 -0800 | [diff] [blame] | 5193 | out->kernel_buffer_size = out->config.period_size * out->config.period_count; |
| 5194 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5195 | out->standby = 1; |
Eric Laurent | a9024de | 2013-04-04 09:19:12 -0700 | [diff] [blame] | 5196 | /* out->muted = false; by calloc() */ |
Glenn Kasten | 2ccd7ba | 2013-09-10 09:04:31 -0700 | [diff] [blame] | 5197 | /* out->written = 0; by calloc() */ |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5198 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 5199 | pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL); |
Eric Laurent | a147807 | 2015-09-21 17:21:52 -0700 | [diff] [blame] | 5200 | pthread_mutex_init(&out->pre_lock, (const pthread_mutexattr_t *) NULL); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 5201 | pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL); |
| 5202 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5203 | config->format = out->stream.common.get_format(&out->stream.common); |
| 5204 | config->channel_mask = out->stream.common.get_channels(&out->stream.common); |
| 5205 | config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common); |
| 5206 | |
Kevin Rocard | a325aa2 | 2018-04-03 09:15:52 -0700 | [diff] [blame] | 5207 | register_format(out->format, out->supported_formats); |
| 5208 | register_channel_mask(out->channel_mask, out->supported_channel_masks); |
| 5209 | register_sample_rate(out->sample_rate, out->supported_sample_rates); |
| 5210 | |
Andy Hung | a452b0a | 2017-03-15 14:51:15 -0700 | [diff] [blame] | 5211 | out->error_log = error_log_create( |
| 5212 | ERROR_LOG_ENTRIES, |
| 5213 | 1000000000 /* aggregate consecutive identical errors within one second in ns */); |
| 5214 | |
Haynes Mathew George | c735fb0 | 2016-06-30 18:00:28 -0700 | [diff] [blame] | 5215 | /* |
| 5216 | By locking output stream before registering, we allow the callback |
| 5217 | to update stream's state only after stream's initial state is set to |
| 5218 | adev state. |
| 5219 | */ |
| 5220 | lock_output_stream(out); |
| 5221 | audio_extn_snd_mon_register_listener(out, out_snd_mon_cb); |
| 5222 | pthread_mutex_lock(&adev->lock); |
| 5223 | out->card_status = adev->card_status; |
| 5224 | pthread_mutex_unlock(&adev->lock); |
| 5225 | pthread_mutex_unlock(&out->lock); |
| 5226 | |
vivek mehta | 4a82477 | 2017-06-08 19:05:49 -0700 | [diff] [blame] | 5227 | stream_app_type_cfg_init(&out->app_type_cfg); |
| 5228 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5229 | *stream_out = &out->stream; |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 5230 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 5231 | ALOGV("%s: exit", __func__); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5232 | return 0; |
Ravi Kumar Alamanda | b199506 | 2013-03-21 23:18:20 -0700 | [diff] [blame] | 5233 | |
| 5234 | error_open: |
| 5235 | free(out); |
| 5236 | *stream_out = NULL; |
Eric Laurent | 2bafff1 | 2016-03-17 12:17:23 -0700 | [diff] [blame] | 5237 | ALOGW("%s: exit: ret %d", __func__, ret); |
Ravi Kumar Alamanda | b199506 | 2013-03-21 23:18:20 -0700 | [diff] [blame] | 5238 | return ret; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5239 | } |
| 5240 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 5241 | static void adev_close_output_stream(struct audio_hw_device *dev __unused, |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5242 | struct audio_stream_out *stream) |
| 5243 | { |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 5244 | struct stream_out *out = (struct stream_out *)stream; |
| 5245 | struct audio_device *adev = out->dev; |
| 5246 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 5247 | ALOGV("%s: enter", __func__); |
Haynes Mathew George | c735fb0 | 2016-06-30 18:00:28 -0700 | [diff] [blame] | 5248 | |
| 5249 | // must deregister from sndmonitor first to prevent races |
| 5250 | // between the callback and close_stream |
| 5251 | audio_extn_snd_mon_unregister_listener(out); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5252 | out_standby(&stream->common); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 5253 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
| 5254 | destroy_offload_callback_thread(out); |
| 5255 | |
| 5256 | if (out->compr_config.codec != NULL) |
| 5257 | free(out->compr_config.codec); |
| 5258 | } |
Ravi Kumar Alamanda | a4fc902 | 2014-10-08 18:57:46 -0700 | [diff] [blame] | 5259 | |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 5260 | out->a2dp_compress_mute = false; |
| 5261 | |
Ravi Kumar Alamanda | a4fc902 | 2014-10-08 18:57:46 -0700 | [diff] [blame] | 5262 | if (adev->voice_tx_output == out) |
| 5263 | adev->voice_tx_output = NULL; |
| 5264 | |
Andy Hung | a452b0a | 2017-03-15 14:51:15 -0700 | [diff] [blame] | 5265 | error_log_destroy(out->error_log); |
| 5266 | out->error_log = NULL; |
| 5267 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 5268 | pthread_cond_destroy(&out->cond); |
Andy Hung | 0dbb52b | 2017-08-09 13:51:38 -0700 | [diff] [blame] | 5269 | pthread_mutex_destroy(&out->pre_lock); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 5270 | pthread_mutex_destroy(&out->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5271 | free(stream); |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 5272 | ALOGV("%s: exit", __func__); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5273 | } |
| 5274 | |
| 5275 | static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs) |
| 5276 | { |
| 5277 | struct audio_device *adev = (struct audio_device *)dev; |
| 5278 | struct str_parms *parms; |
| 5279 | char *str; |
| 5280 | char value[32]; |
Jean-Michel Trivi | c56336b | 2013-05-24 16:55:17 -0700 | [diff] [blame] | 5281 | int val; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5282 | int ret; |
Eric Laurent | 03f0943 | 2014-03-25 18:09:11 -0700 | [diff] [blame] | 5283 | int status = 0; |
Aniket Kumar Lata | d6834ef | 2018-04-06 17:02:48 -0700 | [diff] [blame] | 5284 | bool a2dp_reconfig = false; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5285 | |
Joe Onorato | 188b622 | 2016-03-01 11:02:27 -0800 | [diff] [blame] | 5286 | ALOGV("%s: enter: %s", __func__, kvpairs); |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 5287 | |
| 5288 | pthread_mutex_lock(&adev->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5289 | |
| 5290 | parms = str_parms_create_str(kvpairs); |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 5291 | status = voice_set_parameters(adev, parms); |
| 5292 | if (status != 0) { |
| 5293 | goto done; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5294 | } |
| 5295 | |
| 5296 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value)); |
| 5297 | if (ret >= 0) { |
Ravi Kumar Alamanda | e258e68 | 2015-06-25 13:32:42 -0700 | [diff] [blame] | 5298 | /* When set to false, HAL should disable EC and NS */ |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5299 | if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0) |
| 5300 | adev->bluetooth_nrec = true; |
| 5301 | else |
| 5302 | adev->bluetooth_nrec = false; |
| 5303 | } |
| 5304 | |
| 5305 | ret = str_parms_get_str(parms, "screen_state", value, sizeof(value)); |
| 5306 | if (ret >= 0) { |
| 5307 | if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0) |
| 5308 | adev->screen_off = false; |
| 5309 | else |
| 5310 | adev->screen_off = true; |
| 5311 | } |
| 5312 | |
Jean-Michel Trivi | c56336b | 2013-05-24 16:55:17 -0700 | [diff] [blame] | 5313 | ret = str_parms_get_int(parms, "rotation", &val); |
| 5314 | if (ret >= 0) { |
| 5315 | bool reverse_speakers = false; |
Eric Laurent | 5f4ca95 | 2018-10-19 17:33:43 -0700 | [diff] [blame] | 5316 | int camera_rotation = CAMERA_ROTATION_LANDSCAPE; |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 5317 | switch (val) { |
Jean-Michel Trivi | c56336b | 2013-05-24 16:55:17 -0700 | [diff] [blame] | 5318 | // FIXME: note that the code below assumes that the speakers are in the correct placement |
| 5319 | // relative to the user when the device is rotated 90deg from its default rotation. This |
| 5320 | // assumption is device-specific, not platform-specific like this code. |
| 5321 | case 270: |
| 5322 | reverse_speakers = true; |
Eric Laurent | 5f4ca95 | 2018-10-19 17:33:43 -0700 | [diff] [blame] | 5323 | camera_rotation = CAMERA_ROTATION_INVERT_LANDSCAPE; |
Jean-Michel Trivi | c56336b | 2013-05-24 16:55:17 -0700 | [diff] [blame] | 5324 | break; |
| 5325 | case 0: |
Jean-Michel Trivi | c56336b | 2013-05-24 16:55:17 -0700 | [diff] [blame] | 5326 | case 180: |
Eric Laurent | 5f4ca95 | 2018-10-19 17:33:43 -0700 | [diff] [blame] | 5327 | camera_rotation = CAMERA_ROTATION_PORTRAIT; |
| 5328 | break; |
| 5329 | case 90: |
| 5330 | camera_rotation = CAMERA_ROTATION_LANDSCAPE; |
Jean-Michel Trivi | c56336b | 2013-05-24 16:55:17 -0700 | [diff] [blame] | 5331 | break; |
| 5332 | default: |
| 5333 | ALOGE("%s: unexpected rotation of %d", __func__, val); |
Eric Laurent | 03f0943 | 2014-03-25 18:09:11 -0700 | [diff] [blame] | 5334 | status = -EINVAL; |
Jean-Michel Trivi | c56336b | 2013-05-24 16:55:17 -0700 | [diff] [blame] | 5335 | } |
Eric Laurent | 03f0943 | 2014-03-25 18:09:11 -0700 | [diff] [blame] | 5336 | if (status == 0) { |
vivek mehta | e59cfb2 | 2017-06-16 15:57:11 -0700 | [diff] [blame] | 5337 | // check and set swap |
| 5338 | // - check if orientation changed and speaker active |
| 5339 | // - set rotation and cache the rotation value |
Eric Laurent | 5f4ca95 | 2018-10-19 17:33:43 -0700 | [diff] [blame] | 5340 | adev->camera_orientation = |
| 5341 | (adev->camera_orientation & ~CAMERA_ROTATION_MASK) | camera_rotation; |
| 5342 | #ifndef MAXXAUDIO_QDSP_ENABLED |
vivek mehta | e59cfb2 | 2017-06-16 15:57:11 -0700 | [diff] [blame] | 5343 | platform_check_and_set_swap_lr_channels(adev, reverse_speakers); |
Eric Laurent | 5f4ca95 | 2018-10-19 17:33:43 -0700 | [diff] [blame] | 5344 | #endif |
Jean-Michel Trivi | c56336b | 2013-05-24 16:55:17 -0700 | [diff] [blame] | 5345 | } |
Jean-Michel Trivi | c56336b | 2013-05-24 16:55:17 -0700 | [diff] [blame] | 5346 | } |
| 5347 | |
Ravi Kumar Alamanda | 9f30654 | 2014-04-02 15:11:49 -0700 | [diff] [blame] | 5348 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_SCO_WB, value, sizeof(value)); |
| 5349 | if (ret >= 0) { |
Ravi Kumar Alamanda | 9f30654 | 2014-04-02 15:11:49 -0700 | [diff] [blame] | 5350 | adev->bt_wb_speech_enabled = !strcmp(value, AUDIO_PARAMETER_VALUE_ON); |
Ravi Kumar Alamanda | 9f30654 | 2014-04-02 15:11:49 -0700 | [diff] [blame] | 5351 | } |
| 5352 | |
David Lin | ee3fe40 | 2017-03-13 10:00:42 -0700 | [diff] [blame] | 5353 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value, sizeof(value)); |
| 5354 | if (ret >= 0) { |
| 5355 | audio_devices_t device = (audio_devices_t)strtoul(value, NULL, 10); |
Eric Laurent | 99dab49 | 2017-06-17 15:19:08 -0700 | [diff] [blame] | 5356 | if (audio_is_usb_out_device(device)) { |
David Lin | ee3fe40 | 2017-03-13 10:00:42 -0700 | [diff] [blame] | 5357 | ret = str_parms_get_str(parms, "card", value, sizeof(value)); |
| 5358 | if (ret >= 0) { |
| 5359 | const int card = atoi(value); |
Eric Laurent | 99dab49 | 2017-06-17 15:19:08 -0700 | [diff] [blame] | 5360 | audio_extn_usb_add_device(device, card); |
David Lin | ee3fe40 | 2017-03-13 10:00:42 -0700 | [diff] [blame] | 5361 | } |
Eric Laurent | 99dab49 | 2017-06-17 15:19:08 -0700 | [diff] [blame] | 5362 | } else if (audio_is_usb_in_device(device)) { |
David Lin | ee3fe40 | 2017-03-13 10:00:42 -0700 | [diff] [blame] | 5363 | ret = str_parms_get_str(parms, "card", value, sizeof(value)); |
| 5364 | if (ret >= 0) { |
| 5365 | const int card = atoi(value); |
Eric Laurent | 99dab49 | 2017-06-17 15:19:08 -0700 | [diff] [blame] | 5366 | audio_extn_usb_add_device(device, card); |
David Lin | ee3fe40 | 2017-03-13 10:00:42 -0700 | [diff] [blame] | 5367 | } |
| 5368 | } |
| 5369 | } |
| 5370 | |
| 5371 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value, sizeof(value)); |
| 5372 | if (ret >= 0) { |
| 5373 | audio_devices_t device = (audio_devices_t)strtoul(value, NULL, 10); |
Eric Laurent | 99dab49 | 2017-06-17 15:19:08 -0700 | [diff] [blame] | 5374 | if (audio_is_usb_out_device(device)) { |
David Lin | ee3fe40 | 2017-03-13 10:00:42 -0700 | [diff] [blame] | 5375 | ret = str_parms_get_str(parms, "card", value, sizeof(value)); |
| 5376 | if (ret >= 0) { |
| 5377 | const int card = atoi(value); |
Eric Laurent | 99dab49 | 2017-06-17 15:19:08 -0700 | [diff] [blame] | 5378 | audio_extn_usb_remove_device(device, card); |
David Lin | ee3fe40 | 2017-03-13 10:00:42 -0700 | [diff] [blame] | 5379 | } |
Eric Laurent | 99dab49 | 2017-06-17 15:19:08 -0700 | [diff] [blame] | 5380 | } else if (audio_is_usb_in_device(device)) { |
David Lin | ee3fe40 | 2017-03-13 10:00:42 -0700 | [diff] [blame] | 5381 | ret = str_parms_get_str(parms, "card", value, sizeof(value)); |
| 5382 | if (ret >= 0) { |
| 5383 | const int card = atoi(value); |
Eric Laurent | 99dab49 | 2017-06-17 15:19:08 -0700 | [diff] [blame] | 5384 | audio_extn_usb_remove_device(device, card); |
David Lin | ee3fe40 | 2017-03-13 10:00:42 -0700 | [diff] [blame] | 5385 | } |
| 5386 | } |
| 5387 | } |
| 5388 | |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 5389 | audio_extn_hfp_set_parameters(adev, parms); |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 5390 | audio_extn_ma_set_parameters(adev, parms); |
| 5391 | |
Aniket Kumar Lata | d6834ef | 2018-04-06 17:02:48 -0700 | [diff] [blame] | 5392 | status = audio_extn_a2dp_set_parameters(parms, &a2dp_reconfig); |
| 5393 | if (status >= 0 && a2dp_reconfig) { |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 5394 | struct audio_usecase *usecase; |
| 5395 | struct listnode *node; |
| 5396 | list_for_each(node, &adev->usecase_list) { |
| 5397 | usecase = node_to_item(node, struct audio_usecase, list); |
| 5398 | if ((usecase->type == PCM_PLAYBACK) && |
Aniket Kumar Lata | d6834ef | 2018-04-06 17:02:48 -0700 | [diff] [blame] | 5399 | (usecase->devices & AUDIO_DEVICE_OUT_ALL_A2DP)) { |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 5400 | ALOGD("%s: reconfigure A2DP... forcing device switch", __func__); |
| 5401 | |
| 5402 | pthread_mutex_unlock(&adev->lock); |
| 5403 | lock_output_stream(usecase->stream.out); |
| 5404 | pthread_mutex_lock(&adev->lock); |
| 5405 | audio_extn_a2dp_set_handoff_mode(true); |
| 5406 | // force device switch to reconfigure encoder |
| 5407 | select_devices(adev, usecase->id); |
| 5408 | audio_extn_a2dp_set_handoff_mode(false); |
| 5409 | pthread_mutex_unlock(&usecase->stream.out->lock); |
| 5410 | break; |
| 5411 | } |
| 5412 | } |
| 5413 | } |
| 5414 | |
Eric Laurent | 5f4ca95 | 2018-10-19 17:33:43 -0700 | [diff] [blame] | 5415 | //FIXME: to be replaced by proper video capture properties API |
| 5416 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_CAMERA_FACING, value, sizeof(value)); |
| 5417 | if (ret >= 0) { |
| 5418 | int camera_facing = CAMERA_FACING_BACK; |
| 5419 | if (strcmp(value, AUDIO_PARAMETER_VALUE_FRONT) == 0) |
| 5420 | camera_facing = CAMERA_FACING_FRONT; |
| 5421 | else if (strcmp(value, AUDIO_PARAMETER_VALUE_BACK) == 0) |
| 5422 | camera_facing = CAMERA_FACING_BACK; |
| 5423 | else { |
| 5424 | ALOGW("%s: invalid camera facing value: %s", __func__, value); |
| 5425 | goto done; |
| 5426 | } |
| 5427 | adev->camera_orientation = |
| 5428 | (adev->camera_orientation & ~CAMERA_FACING_MASK) | camera_facing; |
| 5429 | struct audio_usecase *usecase; |
| 5430 | struct listnode *node; |
| 5431 | list_for_each(node, &adev->usecase_list) { |
| 5432 | usecase = node_to_item(node, struct audio_usecase, list); |
| 5433 | struct stream_in *in = usecase->stream.in; |
| 5434 | if (usecase->type == PCM_CAPTURE && in != NULL && |
| 5435 | in->source == AUDIO_SOURCE_CAMCORDER && !in->standby) { |
| 5436 | select_devices(adev, in->usecase); |
| 5437 | } |
| 5438 | } |
| 5439 | } |
| 5440 | |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 5441 | done: |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5442 | str_parms_destroy(parms); |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 5443 | pthread_mutex_unlock(&adev->lock); |
Eric Laurent | 03f0943 | 2014-03-25 18:09:11 -0700 | [diff] [blame] | 5444 | ALOGV("%s: exit with code(%d)", __func__, status); |
| 5445 | return status; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5446 | } |
| 5447 | |
| 5448 | static char* adev_get_parameters(const struct audio_hw_device *dev, |
| 5449 | const char *keys) |
| 5450 | { |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 5451 | struct audio_device *adev = (struct audio_device *)dev; |
| 5452 | struct str_parms *reply = str_parms_create(); |
| 5453 | struct str_parms *query = str_parms_create_str(keys); |
| 5454 | char *str; |
| 5455 | |
| 5456 | pthread_mutex_lock(&adev->lock); |
| 5457 | |
| 5458 | voice_get_parameters(adev, query, reply); |
Aniket Kumar Lata | d6834ef | 2018-04-06 17:02:48 -0700 | [diff] [blame] | 5459 | audio_extn_a2dp_get_parameters(query, reply); |
| 5460 | |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 5461 | str = str_parms_to_str(reply); |
| 5462 | str_parms_destroy(query); |
| 5463 | str_parms_destroy(reply); |
| 5464 | |
| 5465 | pthread_mutex_unlock(&adev->lock); |
| 5466 | ALOGV("%s: exit: returns - %s", __func__, str); |
| 5467 | return str; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5468 | } |
| 5469 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 5470 | 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] | 5471 | { |
| 5472 | return 0; |
| 5473 | } |
| 5474 | |
Haynes Mathew George | 5191a85 | 2013-09-11 14:19:36 -0700 | [diff] [blame] | 5475 | static int adev_set_voice_volume(struct audio_hw_device *dev, float volume) |
| 5476 | { |
| 5477 | int ret; |
| 5478 | struct audio_device *adev = (struct audio_device *)dev; |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 5479 | |
Eric Laurent | 4cc4ce1 | 2014-09-10 13:21:01 -0500 | [diff] [blame] | 5480 | audio_extn_extspk_set_voice_vol(adev->extspk, volume); |
| 5481 | |
Haynes Mathew George | 5191a85 | 2013-09-11 14:19:36 -0700 | [diff] [blame] | 5482 | pthread_mutex_lock(&adev->lock); |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 5483 | ret = voice_set_volume(adev, volume); |
Haynes Mathew George | 5191a85 | 2013-09-11 14:19:36 -0700 | [diff] [blame] | 5484 | pthread_mutex_unlock(&adev->lock); |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 5485 | |
Haynes Mathew George | 5191a85 | 2013-09-11 14:19:36 -0700 | [diff] [blame] | 5486 | return ret; |
| 5487 | } |
| 5488 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 5489 | static int adev_set_master_volume(struct audio_hw_device *dev __unused, float volume __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5490 | { |
| 5491 | return -ENOSYS; |
| 5492 | } |
| 5493 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 5494 | static int adev_get_master_volume(struct audio_hw_device *dev __unused, |
| 5495 | float *volume __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5496 | { |
| 5497 | return -ENOSYS; |
| 5498 | } |
| 5499 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 5500 | static int adev_set_master_mute(struct audio_hw_device *dev __unused, bool muted __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5501 | { |
| 5502 | return -ENOSYS; |
| 5503 | } |
| 5504 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 5505 | static int adev_get_master_mute(struct audio_hw_device *dev __unused, bool *muted __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5506 | { |
| 5507 | return -ENOSYS; |
| 5508 | } |
| 5509 | |
| 5510 | static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode) |
| 5511 | { |
| 5512 | struct audio_device *adev = (struct audio_device *)dev; |
| 5513 | |
| 5514 | pthread_mutex_lock(&adev->lock); |
| 5515 | if (adev->mode != mode) { |
Eric Laurent | 2bafff1 | 2016-03-17 12:17:23 -0700 | [diff] [blame] | 5516 | ALOGD("%s: mode %d", __func__, (int)mode); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5517 | adev->mode = mode; |
Ravi Kumar Alamanda | 36886fc | 2014-09-29 13:41:51 -0700 | [diff] [blame] | 5518 | if ((mode == AUDIO_MODE_NORMAL || mode == AUDIO_MODE_IN_COMMUNICATION) && |
| 5519 | voice_is_in_call(adev)) { |
| 5520 | voice_stop_call(adev); |
| 5521 | adev->current_call_output = NULL; |
HW Lee | f88584d | 2019-03-18 17:27:18 +0800 | [diff] [blame] | 5522 | |
| 5523 | /* |
| 5524 | * After stopping the call, it must check if any active capture |
| 5525 | * activity device needs to be re-selected. |
| 5526 | */ |
| 5527 | struct audio_usecase *usecase; |
| 5528 | struct listnode *node; |
| 5529 | list_for_each(node, &adev->usecase_list) { |
| 5530 | usecase = node_to_item(node, struct audio_usecase, list); |
| 5531 | if (usecase->type == PCM_CAPTURE && usecase->stream.in != NULL) { |
| 5532 | select_devices_with_force_switch(adev, usecase->id, true); |
| 5533 | } |
| 5534 | } |
Ravi Kumar Alamanda | 36886fc | 2014-09-29 13:41:51 -0700 | [diff] [blame] | 5535 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5536 | } |
| 5537 | pthread_mutex_unlock(&adev->lock); |
Eric Laurent | 0499d4f | 2014-08-25 22:39:29 -0500 | [diff] [blame] | 5538 | |
| 5539 | audio_extn_extspk_set_mode(adev->extspk, mode); |
| 5540 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5541 | return 0; |
| 5542 | } |
| 5543 | |
| 5544 | static int adev_set_mic_mute(struct audio_hw_device *dev, bool state) |
| 5545 | { |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 5546 | int ret; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5547 | struct audio_device *adev = (struct audio_device *)dev; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5548 | |
Eric Laurent | 2bafff1 | 2016-03-17 12:17:23 -0700 | [diff] [blame] | 5549 | ALOGD("%s: state %d", __func__, (int)state); |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 5550 | pthread_mutex_lock(&adev->lock); |
Alain Vongsouvanh | 13f26e8 | 2016-11-18 14:39:11 -0800 | [diff] [blame] | 5551 | if (audio_extn_tfa_98xx_is_supported() && adev->enable_hfp) { |
| 5552 | ret = audio_extn_hfp_set_mic_mute(adev, state); |
| 5553 | } else { |
| 5554 | ret = voice_set_mic_mute(adev, state); |
| 5555 | } |
Eric Laurent | 7b2b5ab | 2014-09-14 12:29:59 -0700 | [diff] [blame] | 5556 | adev->mic_muted = state; |
Eric Laurent | a609e8e | 2014-06-18 02:15:17 +0000 | [diff] [blame] | 5557 | pthread_mutex_unlock(&adev->lock); |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 5558 | |
| 5559 | return ret; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5560 | } |
| 5561 | |
| 5562 | static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state) |
| 5563 | { |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 5564 | *state = voice_get_mic_mute((struct audio_device *)dev); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5565 | return 0; |
| 5566 | } |
| 5567 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 5568 | 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] | 5569 | const struct audio_config *config) |
| 5570 | { |
Eric Laurent | 0de8d1f | 2014-07-01 20:34:45 -0700 | [diff] [blame] | 5571 | 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] | 5572 | |
Eric Laurent | 74b5576 | 2017-07-09 17:04:53 -0700 | [diff] [blame] | 5573 | /* Don't know if USB HIFI in this context so use true to be conservative */ |
| 5574 | if (check_input_parameters(config->sample_rate, config->format, channel_count, |
| 5575 | true /*is_usb_hifi */) != 0) |
| 5576 | return 0; |
| 5577 | |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 5578 | return get_stream_buffer_size(AUDIO_CAPTURE_PERIOD_DURATION_MSEC, |
| 5579 | config->sample_rate, config->format, |
| 5580 | channel_count, |
| 5581 | false /* is_low_latency: since we don't know, be conservative */); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5582 | } |
| 5583 | |
Haynes Mathew George | e95340e | 2017-05-24 15:42:06 -0700 | [diff] [blame] | 5584 | static bool adev_input_allow_hifi_record(struct audio_device *adev, |
| 5585 | audio_devices_t devices, |
| 5586 | audio_input_flags_t flags, |
| 5587 | audio_source_t source) { |
| 5588 | const bool allowed = true; |
| 5589 | |
| 5590 | if (!audio_is_usb_in_device(devices)) |
| 5591 | return !allowed; |
| 5592 | |
| 5593 | switch (flags) { |
| 5594 | case AUDIO_INPUT_FLAG_NONE: |
| 5595 | case AUDIO_INPUT_FLAG_FAST: // just fast, not fast|raw || fast|mmap |
| 5596 | break; |
| 5597 | default: |
| 5598 | return !allowed; |
| 5599 | } |
| 5600 | |
| 5601 | switch (source) { |
| 5602 | case AUDIO_SOURCE_DEFAULT: |
| 5603 | case AUDIO_SOURCE_MIC: |
| 5604 | case AUDIO_SOURCE_UNPROCESSED: |
| 5605 | break; |
| 5606 | default: |
| 5607 | return !allowed; |
| 5608 | } |
| 5609 | |
| 5610 | switch (adev->mode) { |
| 5611 | case 0: |
| 5612 | break; |
| 5613 | default: |
| 5614 | return !allowed; |
| 5615 | } |
| 5616 | |
| 5617 | return allowed; |
| 5618 | } |
| 5619 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5620 | static int adev_open_input_stream(struct audio_hw_device *dev, |
Ravi Kumar Alamanda | a417cc5 | 2015-05-01 16:41:56 -0700 | [diff] [blame] | 5621 | audio_io_handle_t handle, |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5622 | audio_devices_t devices, |
| 5623 | struct audio_config *config, |
Glenn Kasten | 68e79ce | 2014-07-15 10:56:59 -0700 | [diff] [blame] | 5624 | struct audio_stream_in **stream_in, |
Eric Laurent | 91975a6 | 2014-07-27 17:15:50 -0700 | [diff] [blame] | 5625 | audio_input_flags_t flags, |
| 5626 | const char *address __unused, |
Eric Laurent | cefbbac | 2014-09-04 13:54:10 -0500 | [diff] [blame] | 5627 | audio_source_t source ) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5628 | { |
| 5629 | struct audio_device *adev = (struct audio_device *)dev; |
| 5630 | struct stream_in *in; |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 5631 | int ret = 0, buffer_size, frame_size; |
Eric Laurent | 74b5576 | 2017-07-09 17:04:53 -0700 | [diff] [blame] | 5632 | int channel_count; |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 5633 | bool is_low_latency = false; |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 5634 | bool is_usb_dev = audio_is_usb_in_device(devices); |
Haynes Mathew George | e95340e | 2017-05-24 15:42:06 -0700 | [diff] [blame] | 5635 | bool may_use_hifi_record = adev_input_allow_hifi_record(adev, |
| 5636 | devices, |
| 5637 | flags, |
| 5638 | source); |
Andy Hung | 88ce1d9 | 2018-10-29 18:31:12 -0700 | [diff] [blame] | 5639 | ALOGV("%s: enter: flags %#x, is_usb_dev %d, may_use_hifi_record %d," |
| 5640 | " sample_rate %u, channel_mask %#x, format %#x", |
| 5641 | __func__, flags, is_usb_dev, may_use_hifi_record, |
| 5642 | config->sample_rate, config->channel_mask, config->format); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5643 | *stream_in = NULL; |
Haynes Mathew George | e95340e | 2017-05-24 15:42:06 -0700 | [diff] [blame] | 5644 | |
Andy Hung | d9653bd | 2017-08-01 19:31:39 -0700 | [diff] [blame] | 5645 | if (is_usb_dev && !is_usb_ready(adev, false /* is_playback */)) { |
| 5646 | return -ENOSYS; |
| 5647 | } |
| 5648 | |
Eric Laurent | 74b5576 | 2017-07-09 17:04:53 -0700 | [diff] [blame] | 5649 | if (!(is_usb_dev && may_use_hifi_record)) { |
| 5650 | if (config->sample_rate == 0) |
| 5651 | config->sample_rate = DEFAULT_INPUT_SAMPLING_RATE; |
| 5652 | if (config->channel_mask == AUDIO_CHANNEL_NONE) |
| 5653 | config->channel_mask = AUDIO_CHANNEL_IN_MONO; |
| 5654 | if (config->format == AUDIO_FORMAT_DEFAULT) |
| 5655 | config->format = AUDIO_FORMAT_PCM_16_BIT; |
Haynes Mathew George | e95340e | 2017-05-24 15:42:06 -0700 | [diff] [blame] | 5656 | |
Eric Laurent | 74b5576 | 2017-07-09 17:04:53 -0700 | [diff] [blame] | 5657 | channel_count = audio_channel_count_from_in_mask(config->channel_mask); |
| 5658 | |
| 5659 | if (check_input_parameters(config->sample_rate, config->format, channel_count, false) != 0) |
| 5660 | return -EINVAL; |
| 5661 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5662 | |
Haynes Mathew George | e95340e | 2017-05-24 15:42:06 -0700 | [diff] [blame] | 5663 | if (audio_extn_tfa_98xx_is_supported() && |
| 5664 | (audio_extn_hfp_is_active(adev) || voice_is_in_call(adev))) |
Alain Vongsouvanh | 13f26e8 | 2016-11-18 14:39:11 -0800 | [diff] [blame] | 5665 | return -EINVAL; |
| 5666 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5667 | in = (struct stream_in *)calloc(1, sizeof(struct stream_in)); |
| 5668 | |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 5669 | pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL); |
Eric Laurent | a147807 | 2015-09-21 17:21:52 -0700 | [diff] [blame] | 5670 | pthread_mutex_init(&in->pre_lock, (const pthread_mutexattr_t *) NULL); |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 5671 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5672 | in->stream.common.get_sample_rate = in_get_sample_rate; |
| 5673 | in->stream.common.set_sample_rate = in_set_sample_rate; |
| 5674 | in->stream.common.get_buffer_size = in_get_buffer_size; |
| 5675 | in->stream.common.get_channels = in_get_channels; |
| 5676 | in->stream.common.get_format = in_get_format; |
| 5677 | in->stream.common.set_format = in_set_format; |
| 5678 | in->stream.common.standby = in_standby; |
| 5679 | in->stream.common.dump = in_dump; |
| 5680 | in->stream.common.set_parameters = in_set_parameters; |
| 5681 | in->stream.common.get_parameters = in_get_parameters; |
| 5682 | in->stream.common.add_audio_effect = in_add_audio_effect; |
| 5683 | in->stream.common.remove_audio_effect = in_remove_audio_effect; |
| 5684 | in->stream.set_gain = in_set_gain; |
| 5685 | in->stream.read = in_read; |
| 5686 | in->stream.get_input_frames_lost = in_get_input_frames_lost; |
Andy Hung | 6ebe596 | 2016-01-15 17:46:57 -0800 | [diff] [blame] | 5687 | in->stream.get_capture_position = in_get_capture_position; |
jiabin | 8962a4d | 2018-03-19 18:21:24 -0700 | [diff] [blame] | 5688 | in->stream.get_active_microphones = in_get_active_microphones; |
Paul McLean | 57530d5 | 2018-12-17 08:24:21 -0700 | [diff] [blame] | 5689 | in->stream.set_microphone_direction = in_set_microphone_direction; |
| 5690 | in->stream.set_microphone_field_dimension = in_set_microphone_field_dimension; |
juyuchen | ba338cd | 2019-01-21 11:57:17 +0800 | [diff] [blame] | 5691 | in->stream.update_sink_metadata = in_update_sink_metadata; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5692 | |
| 5693 | in->device = devices; |
Eric Laurent | cefbbac | 2014-09-04 13:54:10 -0500 | [diff] [blame] | 5694 | in->source = source; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5695 | in->dev = adev; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5696 | in->standby = 1; |
Ravi Kumar Alamanda | a417cc5 | 2015-05-01 16:41:56 -0700 | [diff] [blame] | 5697 | in->capture_handle = handle; |
Haynes Mathew George | 88e6fb2 | 2015-08-19 11:51:34 -0700 | [diff] [blame] | 5698 | in->flags = flags; |
justinweng | c6347db | 2019-02-21 18:49:00 +0800 | [diff] [blame] | 5699 | in->direction = MIC_DIRECTION_UNSPECIFIED; |
| 5700 | in->zoom = 0; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5701 | |
Andy Hung | 88ce1d9 | 2018-10-29 18:31:12 -0700 | [diff] [blame] | 5702 | ALOGV("%s: source %d, config->channel_mask %#x", __func__, source, config->channel_mask); |
Arun Mirpuri | ba2749a | 2018-04-17 14:32:24 -0700 | [diff] [blame] | 5703 | if (source == AUDIO_SOURCE_VOICE_UPLINK || |
| 5704 | source == AUDIO_SOURCE_VOICE_DOWNLINK) { |
| 5705 | /* Force channel config requested to mono if incall |
| 5706 | record is being requested for only uplink/downlink */ |
| 5707 | if (config->channel_mask != AUDIO_CHANNEL_IN_MONO) { |
| 5708 | config->channel_mask = AUDIO_CHANNEL_IN_MONO; |
| 5709 | ret = -EINVAL; |
| 5710 | goto err_open; |
| 5711 | } |
| 5712 | } |
| 5713 | |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 5714 | if (is_usb_dev && may_use_hifi_record) { |
| 5715 | /* HiFi record selects an appropriate format, channel, rate combo |
| 5716 | depending on sink capabilities*/ |
| 5717 | ret = read_usb_sup_params_and_compare(false /*is_playback*/, |
| 5718 | &config->format, |
| 5719 | &in->supported_formats[0], |
| 5720 | MAX_SUPPORTED_FORMATS, |
| 5721 | &config->channel_mask, |
| 5722 | &in->supported_channel_masks[0], |
| 5723 | MAX_SUPPORTED_CHANNEL_MASKS, |
| 5724 | &config->sample_rate, |
| 5725 | &in->supported_sample_rates[0], |
| 5726 | MAX_SUPPORTED_SAMPLE_RATES); |
| 5727 | if (ret != 0) { |
| 5728 | ret = -EINVAL; |
| 5729 | goto err_open; |
| 5730 | } |
Eric Laurent | 74b5576 | 2017-07-09 17:04:53 -0700 | [diff] [blame] | 5731 | channel_count = audio_channel_count_from_in_mask(config->channel_mask); |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 5732 | } else if (config->format == AUDIO_FORMAT_DEFAULT) { |
vivek mehta | 4ed66e6 | 2016-04-15 23:33:34 -0700 | [diff] [blame] | 5733 | config->format = AUDIO_FORMAT_PCM_16_BIT; |
vivek mehta | 57ff9b5 | 2016-04-28 14:13:08 -0700 | [diff] [blame] | 5734 | } else if (config->format == AUDIO_FORMAT_PCM_FLOAT || |
| 5735 | config->format == AUDIO_FORMAT_PCM_24_BIT_PACKED || |
| 5736 | config->format == AUDIO_FORMAT_PCM_8_24_BIT) { |
| 5737 | bool ret_error = false; |
| 5738 | /* 24 bit is restricted to UNPROCESSED source only,also format supported |
| 5739 | from HAL is 8_24 |
| 5740 | *> In case of UNPROCESSED source, for 24 bit, if format requested is other than |
| 5741 | 8_24 return error indicating supported format is 8_24 |
| 5742 | *> In case of any other source requesting 24 bit or float return error |
| 5743 | indicating format supported is 16 bit only. |
vivek mehta | 4ed66e6 | 2016-04-15 23:33:34 -0700 | [diff] [blame] | 5744 | |
vivek mehta | 57ff9b5 | 2016-04-28 14:13:08 -0700 | [diff] [blame] | 5745 | on error flinger will retry with supported format passed |
| 5746 | */ |
juyuchen | be5c67e | 2018-08-29 17:19:20 +0800 | [diff] [blame] | 5747 | if (!is_supported_24bits_audiosource(source)) { |
vivek mehta | 57ff9b5 | 2016-04-28 14:13:08 -0700 | [diff] [blame] | 5748 | config->format = AUDIO_FORMAT_PCM_16_BIT; |
| 5749 | ret_error = true; |
| 5750 | } else if (config->format != AUDIO_FORMAT_PCM_8_24_BIT) { |
| 5751 | config->format = AUDIO_FORMAT_PCM_8_24_BIT; |
| 5752 | ret_error = true; |
| 5753 | } |
| 5754 | |
| 5755 | if (ret_error) { |
| 5756 | ret = -EINVAL; |
| 5757 | goto err_open; |
| 5758 | } |
vivek mehta | 4ed66e6 | 2016-04-15 23:33:34 -0700 | [diff] [blame] | 5759 | } |
| 5760 | |
vivek mehta | 57ff9b5 | 2016-04-28 14:13:08 -0700 | [diff] [blame] | 5761 | in->format = config->format; |
Eric Laurent | 74b5576 | 2017-07-09 17:04:53 -0700 | [diff] [blame] | 5762 | in->channel_mask = config->channel_mask; |
vivek mehta | 57ff9b5 | 2016-04-28 14:13:08 -0700 | [diff] [blame] | 5763 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5764 | /* Update config params with the requested sample rate and channels */ |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 5765 | if (in->device == AUDIO_DEVICE_IN_TELEPHONY_RX) { |
| 5766 | if (config->sample_rate == 0) |
| 5767 | config->sample_rate = AFE_PROXY_SAMPLING_RATE; |
| 5768 | if (config->sample_rate != 48000 && config->sample_rate != 16000 && |
| 5769 | config->sample_rate != 8000) { |
| 5770 | config->sample_rate = AFE_PROXY_SAMPLING_RATE; |
| 5771 | ret = -EINVAL; |
| 5772 | goto err_open; |
| 5773 | } |
vivek mehta | 4ed66e6 | 2016-04-15 23:33:34 -0700 | [diff] [blame] | 5774 | |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 5775 | if (config->format != AUDIO_FORMAT_PCM_16_BIT) { |
| 5776 | config->format = AUDIO_FORMAT_PCM_16_BIT; |
| 5777 | ret = -EINVAL; |
| 5778 | goto err_open; |
| 5779 | } |
| 5780 | |
| 5781 | in->usecase = USECASE_AUDIO_RECORD_AFE_PROXY; |
| 5782 | in->config = pcm_config_afe_proxy_record; |
David Lin | e5b12ac | 2017-03-29 13:37:33 -0700 | [diff] [blame] | 5783 | in->af_period_multiplier = 1; |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 5784 | } else if (is_usb_dev && may_use_hifi_record) { |
| 5785 | in->usecase = USECASE_AUDIO_RECORD_HIFI; |
| 5786 | in->config = pcm_config_audio_capture; |
| 5787 | frame_size = audio_stream_in_frame_size(&in->stream); |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 5788 | buffer_size = get_stream_buffer_size(AUDIO_CAPTURE_PERIOD_DURATION_MSEC, |
| 5789 | config->sample_rate, |
| 5790 | config->format, |
| 5791 | channel_count, |
| 5792 | false /*is_low_latency*/); |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 5793 | in->config.period_size = buffer_size / frame_size; |
| 5794 | in->config.rate = config->sample_rate; |
| 5795 | in->af_period_multiplier = 1; |
| 5796 | in->config.format = pcm_format_from_audio_format(config->format); |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 5797 | } else { |
| 5798 | in->usecase = USECASE_AUDIO_RECORD; |
| 5799 | if (config->sample_rate == LOW_LATENCY_CAPTURE_SAMPLE_RATE && |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 5800 | (in->flags & AUDIO_INPUT_FLAG_FAST) != 0) { |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 5801 | is_low_latency = true; |
Glenn Kasten | 4f99339 | 2014-05-14 07:30:48 -0700 | [diff] [blame] | 5802 | #if LOW_LATENCY_CAPTURE_USE_CASE |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 5803 | in->usecase = USECASE_AUDIO_RECORD_LOW_LATENCY; |
Glenn Kasten | 4f99339 | 2014-05-14 07:30:48 -0700 | [diff] [blame] | 5804 | #endif |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 5805 | in->realtime = may_use_noirq_mode(adev, in->usecase, in->flags); |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 5806 | if (!in->realtime) { |
Eric Laurent | ff1e5e6 | 2017-02-21 16:08:55 -0800 | [diff] [blame] | 5807 | in->config = pcm_config_audio_capture; |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 5808 | frame_size = audio_stream_in_frame_size(&in->stream); |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 5809 | buffer_size = get_stream_buffer_size(AUDIO_CAPTURE_PERIOD_DURATION_MSEC, |
| 5810 | config->sample_rate, |
| 5811 | config->format, |
| 5812 | channel_count, |
| 5813 | is_low_latency); |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 5814 | in->config.period_size = buffer_size / frame_size; |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 5815 | in->config.rate = config->sample_rate; |
| 5816 | in->af_period_multiplier = 1; |
| 5817 | } else { |
| 5818 | // period size is left untouched for rt mode playback |
| 5819 | in->config = pcm_config_audio_capture_rt; |
| 5820 | in->af_period_multiplier = af_period_multiplier; |
| 5821 | } |
| 5822 | } else if ((config->sample_rate == LOW_LATENCY_CAPTURE_SAMPLE_RATE) && |
| 5823 | ((in->flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0)) { |
Haynes Mathew George | e95340e | 2017-05-24 15:42:06 -0700 | [diff] [blame] | 5824 | // FIXME: Add support for multichannel capture over USB using MMAP |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 5825 | in->usecase = USECASE_AUDIO_RECORD_MMAP; |
| 5826 | in->config = pcm_config_mmap_capture; |
| 5827 | in->stream.start = in_start; |
| 5828 | in->stream.stop = in_stop; |
| 5829 | in->stream.create_mmap_buffer = in_create_mmap_buffer; |
| 5830 | in->stream.get_mmap_position = in_get_mmap_position; |
| 5831 | in->af_period_multiplier = 1; |
| 5832 | ALOGV("%s: USECASE_AUDIO_RECORD_MMAP", __func__); |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 5833 | } else if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION && |
vivek mehta | 4a82477 | 2017-06-08 19:05:49 -0700 | [diff] [blame] | 5834 | in->flags & AUDIO_INPUT_FLAG_VOIP_TX && |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 5835 | (config->sample_rate == 8000 || |
| 5836 | config->sample_rate == 16000 || |
| 5837 | config->sample_rate == 32000 || |
| 5838 | config->sample_rate == 48000) && |
| 5839 | channel_count == 1) { |
| 5840 | in->usecase = USECASE_AUDIO_RECORD_VOIP; |
| 5841 | in->config = pcm_config_audio_capture; |
| 5842 | frame_size = audio_stream_in_frame_size(&in->stream); |
| 5843 | buffer_size = get_stream_buffer_size(VOIP_CAPTURE_PERIOD_DURATION_MSEC, |
| 5844 | config->sample_rate, |
| 5845 | config->format, |
| 5846 | channel_count, false /*is_low_latency*/); |
| 5847 | in->config.period_size = buffer_size / frame_size; |
| 5848 | in->config.period_count = VOIP_CAPTURE_PERIOD_COUNT; |
| 5849 | in->config.rate = config->sample_rate; |
| 5850 | in->af_period_multiplier = 1; |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 5851 | } else { |
| 5852 | in->config = pcm_config_audio_capture; |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 5853 | frame_size = audio_stream_in_frame_size(&in->stream); |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 5854 | buffer_size = get_stream_buffer_size(AUDIO_CAPTURE_PERIOD_DURATION_MSEC, |
| 5855 | config->sample_rate, |
| 5856 | config->format, |
| 5857 | channel_count, |
| 5858 | is_low_latency); |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 5859 | in->config.period_size = buffer_size / frame_size; |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 5860 | in->config.rate = config->sample_rate; |
| 5861 | in->af_period_multiplier = 1; |
| 5862 | } |
| 5863 | if (config->format == AUDIO_FORMAT_PCM_8_24_BIT) |
| 5864 | in->config.format = PCM_FORMAT_S24_LE; |
Glenn Kasten | 68e79ce | 2014-07-15 10:56:59 -0700 | [diff] [blame] | 5865 | } |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 5866 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5867 | in->config.channels = channel_count; |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 5868 | in->sample_rate = in->config.rate; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5869 | |
Kevin Rocard | a325aa2 | 2018-04-03 09:15:52 -0700 | [diff] [blame] | 5870 | |
| 5871 | register_format(in->format, in->supported_formats); |
| 5872 | register_channel_mask(in->channel_mask, in->supported_channel_masks); |
| 5873 | register_sample_rate(in->sample_rate, in->supported_sample_rates); |
| 5874 | |
Andy Hung | d13f0d3 | 2017-06-12 13:58:37 -0700 | [diff] [blame] | 5875 | in->error_log = error_log_create( |
| 5876 | ERROR_LOG_ENTRIES, |
| 5877 | NANOS_PER_SECOND /* aggregate consecutive identical errors within one second */); |
| 5878 | |
Ravi Kumar Alamanda | a417cc5 | 2015-05-01 16:41:56 -0700 | [diff] [blame] | 5879 | /* This stream could be for sound trigger lab, |
| 5880 | get sound trigger pcm if present */ |
| 5881 | audio_extn_sound_trigger_check_and_get_session(in); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5882 | |
Haynes Mathew George | c735fb0 | 2016-06-30 18:00:28 -0700 | [diff] [blame] | 5883 | lock_input_stream(in); |
| 5884 | audio_extn_snd_mon_register_listener(in, in_snd_mon_cb); |
| 5885 | pthread_mutex_lock(&adev->lock); |
| 5886 | in->card_status = adev->card_status; |
| 5887 | pthread_mutex_unlock(&adev->lock); |
| 5888 | pthread_mutex_unlock(&in->lock); |
| 5889 | |
vivek mehta | 4a82477 | 2017-06-08 19:05:49 -0700 | [diff] [blame] | 5890 | stream_app_type_cfg_init(&in->app_type_cfg); |
| 5891 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5892 | *stream_in = &in->stream; |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 5893 | ALOGV("%s: exit", __func__); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5894 | return 0; |
| 5895 | |
| 5896 | err_open: |
| 5897 | free(in); |
| 5898 | *stream_in = NULL; |
| 5899 | return ret; |
| 5900 | } |
| 5901 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 5902 | static void adev_close_input_stream(struct audio_hw_device *dev __unused, |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5903 | struct audio_stream_in *stream) |
| 5904 | { |
Andy Hung | d13f0d3 | 2017-06-12 13:58:37 -0700 | [diff] [blame] | 5905 | struct stream_in *in = (struct stream_in *)stream; |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 5906 | ALOGV("%s", __func__); |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 5907 | |
Haynes Mathew George | c735fb0 | 2016-06-30 18:00:28 -0700 | [diff] [blame] | 5908 | // must deregister from sndmonitor first to prevent races |
| 5909 | // between the callback and close_stream |
| 5910 | audio_extn_snd_mon_unregister_listener(stream); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5911 | in_standby(&stream->common); |
Andy Hung | d13f0d3 | 2017-06-12 13:58:37 -0700 | [diff] [blame] | 5912 | |
| 5913 | error_log_destroy(in->error_log); |
| 5914 | in->error_log = NULL; |
| 5915 | |
Andy Hung | 0dbb52b | 2017-08-09 13:51:38 -0700 | [diff] [blame] | 5916 | pthread_mutex_destroy(&in->pre_lock); |
| 5917 | pthread_mutex_destroy(&in->lock); |
| 5918 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5919 | free(stream); |
| 5920 | |
| 5921 | return; |
| 5922 | } |
| 5923 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 5924 | static int adev_dump(const audio_hw_device_t *device __unused, int fd __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5925 | { |
| 5926 | return 0; |
| 5927 | } |
| 5928 | |
Andy Hung | 31aca91 | 2014-03-20 17:14:59 -0700 | [diff] [blame] | 5929 | /* verifies input and output devices and their capabilities. |
| 5930 | * |
| 5931 | * This verification is required when enabling extended bit-depth or |
| 5932 | * sampling rates, as not all qcom products support it. |
| 5933 | * |
| 5934 | * Suitable for calling only on initialization such as adev_open(). |
| 5935 | * It fills the audio_device use_case_table[] array. |
| 5936 | * |
| 5937 | * Has a side-effect that it needs to configure audio routing / devices |
| 5938 | * in order to power up the devices and read the device parameters. |
| 5939 | * It does not acquire any hw device lock. Should restore the devices |
| 5940 | * back to "normal state" upon completion. |
| 5941 | */ |
| 5942 | static int adev_verify_devices(struct audio_device *adev) |
| 5943 | { |
| 5944 | /* enumeration is a bit difficult because one really wants to pull |
| 5945 | * the use_case, device id, etc from the hidden pcm_device_table[]. |
| 5946 | * In this case there are the following use cases and device ids. |
| 5947 | * |
| 5948 | * [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = {0, 0}, |
| 5949 | * [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {15, 15}, |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 5950 | * [USECASE_AUDIO_PLAYBACK_HIFI] = {1, 1}, |
Andy Hung | 31aca91 | 2014-03-20 17:14:59 -0700 | [diff] [blame] | 5951 | * [USECASE_AUDIO_PLAYBACK_OFFLOAD] = {9, 9}, |
| 5952 | * [USECASE_AUDIO_RECORD] = {0, 0}, |
| 5953 | * [USECASE_AUDIO_RECORD_LOW_LATENCY] = {15, 15}, |
| 5954 | * [USECASE_VOICE_CALL] = {2, 2}, |
| 5955 | * |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 5956 | * USECASE_AUDIO_PLAYBACK_OFFLOAD, USECASE_AUDIO_PLAYBACK_HIFI omitted. |
Andy Hung | 31aca91 | 2014-03-20 17:14:59 -0700 | [diff] [blame] | 5957 | * USECASE_VOICE_CALL omitted, but possible for either input or output. |
| 5958 | */ |
| 5959 | |
| 5960 | /* should be the usecases enabled in adev_open_input_stream() */ |
| 5961 | static const int test_in_usecases[] = { |
| 5962 | USECASE_AUDIO_RECORD, |
| 5963 | USECASE_AUDIO_RECORD_LOW_LATENCY, /* does not appear to be used */ |
| 5964 | }; |
| 5965 | /* should be the usecases enabled in adev_open_output_stream()*/ |
| 5966 | static const int test_out_usecases[] = { |
| 5967 | USECASE_AUDIO_PLAYBACK_DEEP_BUFFER, |
| 5968 | USECASE_AUDIO_PLAYBACK_LOW_LATENCY, |
| 5969 | }; |
| 5970 | static const usecase_type_t usecase_type_by_dir[] = { |
| 5971 | PCM_PLAYBACK, |
| 5972 | PCM_CAPTURE, |
| 5973 | }; |
| 5974 | static const unsigned flags_by_dir[] = { |
| 5975 | PCM_OUT, |
| 5976 | PCM_IN, |
| 5977 | }; |
| 5978 | |
| 5979 | size_t i; |
| 5980 | unsigned dir; |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 5981 | const unsigned card_id = adev->snd_card; |
Andy Hung | 31aca91 | 2014-03-20 17:14:59 -0700 | [diff] [blame] | 5982 | char info[512]; /* for possible debug info */ |
| 5983 | |
| 5984 | for (dir = 0; dir < 2; ++dir) { |
| 5985 | const usecase_type_t usecase_type = usecase_type_by_dir[dir]; |
| 5986 | const unsigned flags_dir = flags_by_dir[dir]; |
| 5987 | const size_t testsize = |
| 5988 | dir ? ARRAY_SIZE(test_in_usecases) : ARRAY_SIZE(test_out_usecases); |
| 5989 | const int *testcases = |
| 5990 | dir ? test_in_usecases : test_out_usecases; |
| 5991 | const audio_devices_t audio_device = |
| 5992 | dir ? AUDIO_DEVICE_IN_BUILTIN_MIC : AUDIO_DEVICE_OUT_SPEAKER; |
| 5993 | |
| 5994 | for (i = 0; i < testsize; ++i) { |
| 5995 | const audio_usecase_t audio_usecase = testcases[i]; |
| 5996 | int device_id; |
| 5997 | snd_device_t snd_device; |
| 5998 | struct pcm_params **pparams; |
| 5999 | struct stream_out out; |
| 6000 | struct stream_in in; |
| 6001 | struct audio_usecase uc_info; |
| 6002 | int retval; |
| 6003 | |
| 6004 | pparams = &adev->use_case_table[audio_usecase]; |
| 6005 | pcm_params_free(*pparams); /* can accept null input */ |
| 6006 | *pparams = NULL; |
| 6007 | |
| 6008 | /* find the device ID for the use case (signed, for error) */ |
| 6009 | device_id = platform_get_pcm_device_id(audio_usecase, usecase_type); |
| 6010 | if (device_id < 0) |
| 6011 | continue; |
| 6012 | |
| 6013 | /* prepare structures for device probing */ |
| 6014 | memset(&uc_info, 0, sizeof(uc_info)); |
| 6015 | uc_info.id = audio_usecase; |
| 6016 | uc_info.type = usecase_type; |
| 6017 | if (dir) { |
Andy Hung | 31aca91 | 2014-03-20 17:14:59 -0700 | [diff] [blame] | 6018 | memset(&in, 0, sizeof(in)); |
| 6019 | in.device = audio_device; |
| 6020 | in.source = AUDIO_SOURCE_VOICE_COMMUNICATION; |
| 6021 | uc_info.stream.in = ∈ |
Andy Hung | 31aca91 | 2014-03-20 17:14:59 -0700 | [diff] [blame] | 6022 | } |
| 6023 | memset(&out, 0, sizeof(out)); |
| 6024 | out.devices = audio_device; /* only field needed in select_devices */ |
| 6025 | uc_info.stream.out = &out; |
| 6026 | uc_info.devices = audio_device; |
| 6027 | uc_info.in_snd_device = SND_DEVICE_NONE; |
| 6028 | uc_info.out_snd_device = SND_DEVICE_NONE; |
| 6029 | list_add_tail(&adev->usecase_list, &uc_info.list); |
| 6030 | |
| 6031 | /* select device - similar to start_(in/out)put_stream() */ |
| 6032 | retval = select_devices(adev, audio_usecase); |
| 6033 | if (retval >= 0) { |
| 6034 | *pparams = pcm_params_get(card_id, device_id, flags_dir); |
| 6035 | #if LOG_NDEBUG == 0 |
| 6036 | if (*pparams) { |
| 6037 | ALOGV("%s: (%s) card %d device %d", __func__, |
| 6038 | dir ? "input" : "output", card_id, device_id); |
| 6039 | pcm_params_to_string(*pparams, info, ARRAY_SIZE(info)); |
Andy Hung | 31aca91 | 2014-03-20 17:14:59 -0700 | [diff] [blame] | 6040 | } else { |
| 6041 | ALOGV("%s: cannot locate card %d device %d", __func__, card_id, device_id); |
| 6042 | } |
| 6043 | #endif |
| 6044 | } |
| 6045 | |
| 6046 | /* deselect device - similar to stop_(in/out)put_stream() */ |
| 6047 | /* 1. Get and set stream specific mixer controls */ |
Glenn Kasten | e713730 | 2014-04-28 15:12:18 -0700 | [diff] [blame] | 6048 | retval = disable_audio_route(adev, &uc_info); |
Andy Hung | 31aca91 | 2014-03-20 17:14:59 -0700 | [diff] [blame] | 6049 | /* 2. Disable the rx device */ |
| 6050 | retval = disable_snd_device(adev, |
Glenn Kasten | e713730 | 2014-04-28 15:12:18 -0700 | [diff] [blame] | 6051 | dir ? uc_info.in_snd_device : uc_info.out_snd_device); |
Andy Hung | 31aca91 | 2014-03-20 17:14:59 -0700 | [diff] [blame] | 6052 | list_remove(&uc_info.list); |
| 6053 | } |
| 6054 | } |
Andy Hung | 31aca91 | 2014-03-20 17:14:59 -0700 | [diff] [blame] | 6055 | return 0; |
| 6056 | } |
| 6057 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6058 | static int adev_close(hw_device_t *device) |
| 6059 | { |
Andy Hung | 31aca91 | 2014-03-20 17:14:59 -0700 | [diff] [blame] | 6060 | size_t i; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6061 | struct audio_device *adev = (struct audio_device *)device; |
vivek mehta | 1a9b7c0 | 2015-06-25 11:49:38 -0700 | [diff] [blame] | 6062 | |
| 6063 | if (!adev) |
| 6064 | return 0; |
| 6065 | |
| 6066 | pthread_mutex_lock(&adev_init_lock); |
| 6067 | |
| 6068 | if ((--audio_device_ref_count) == 0) { |
Haynes Mathew George | ceafc55 | 2017-05-24 15:44:23 -0700 | [diff] [blame] | 6069 | audio_extn_snd_mon_unregister_listener(adev); |
| 6070 | audio_extn_tfa_98xx_deinit(); |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 6071 | audio_extn_ma_deinit(); |
vivek mehta | 1a9b7c0 | 2015-06-25 11:49:38 -0700 | [diff] [blame] | 6072 | audio_route_free(adev->audio_route); |
| 6073 | free(adev->snd_dev_ref_cnt); |
| 6074 | platform_deinit(adev->platform); |
| 6075 | audio_extn_extspk_deinit(adev->extspk); |
| 6076 | audio_extn_sound_trigger_deinit(adev); |
Haynes Mathew George | ceafc55 | 2017-05-24 15:44:23 -0700 | [diff] [blame] | 6077 | audio_extn_snd_mon_deinit(); |
vivek mehta | 1a9b7c0 | 2015-06-25 11:49:38 -0700 | [diff] [blame] | 6078 | for (i = 0; i < ARRAY_SIZE(adev->use_case_table); ++i) { |
| 6079 | pcm_params_free(adev->use_case_table[i]); |
| 6080 | } |
Haynes Mathew George | 88e6fb2 | 2015-08-19 11:51:34 -0700 | [diff] [blame] | 6081 | if (adev->adm_deinit) |
| 6082 | adev->adm_deinit(adev->adm_data); |
Andy Hung | 0dbb52b | 2017-08-09 13:51:38 -0700 | [diff] [blame] | 6083 | pthread_mutex_destroy(&adev->lock); |
vivek mehta | 1a9b7c0 | 2015-06-25 11:49:38 -0700 | [diff] [blame] | 6084 | free(device); |
Andy Hung | 31aca91 | 2014-03-20 17:14:59 -0700 | [diff] [blame] | 6085 | } |
vivek mehta | 1a9b7c0 | 2015-06-25 11:49:38 -0700 | [diff] [blame] | 6086 | |
| 6087 | pthread_mutex_unlock(&adev_init_lock); |
Haynes Mathew George | 88e6fb2 | 2015-08-19 11:51:34 -0700 | [diff] [blame] | 6088 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6089 | return 0; |
| 6090 | } |
| 6091 | |
Glenn Kasten | 4f99339 | 2014-05-14 07:30:48 -0700 | [diff] [blame] | 6092 | /* This returns 1 if the input parameter looks at all plausible as a low latency period size, |
| 6093 | * or 0 otherwise. A return value of 1 doesn't mean the value is guaranteed to work, |
| 6094 | * just that it _might_ work. |
| 6095 | */ |
| 6096 | static int period_size_is_plausible_for_low_latency(int period_size) |
| 6097 | { |
| 6098 | switch (period_size) { |
Glenn Kasten | 5b5d04e | 2015-04-09 09:43:29 -0700 | [diff] [blame] | 6099 | case 48: |
| 6100 | case 96: |
| 6101 | case 144: |
Glenn Kasten | 4f99339 | 2014-05-14 07:30:48 -0700 | [diff] [blame] | 6102 | case 160: |
Glenn Kasten | 5b5d04e | 2015-04-09 09:43:29 -0700 | [diff] [blame] | 6103 | case 192: |
Glenn Kasten | 4f99339 | 2014-05-14 07:30:48 -0700 | [diff] [blame] | 6104 | case 240: |
| 6105 | case 320: |
| 6106 | case 480: |
| 6107 | return 1; |
| 6108 | default: |
| 6109 | return 0; |
| 6110 | } |
| 6111 | } |
| 6112 | |
Haynes Mathew George | c735fb0 | 2016-06-30 18:00:28 -0700 | [diff] [blame] | 6113 | static void adev_snd_mon_cb(void * stream __unused, struct str_parms * parms) |
| 6114 | { |
| 6115 | int card; |
| 6116 | card_status_t status; |
| 6117 | |
| 6118 | if (!parms) |
| 6119 | return; |
| 6120 | |
| 6121 | if (parse_snd_card_status(parms, &card, &status) < 0) |
| 6122 | return; |
| 6123 | |
| 6124 | pthread_mutex_lock(&adev->lock); |
| 6125 | bool valid_cb = (card == adev->snd_card); |
| 6126 | if (valid_cb) { |
| 6127 | if (adev->card_status != status) { |
| 6128 | adev->card_status = status; |
| 6129 | platform_snd_card_update(adev->platform, status); |
| 6130 | } |
| 6131 | } |
| 6132 | pthread_mutex_unlock(&adev->lock); |
| 6133 | return; |
| 6134 | } |
| 6135 | |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 6136 | /* out and adev lock held */ |
| 6137 | static int check_a2dp_restore_l(struct audio_device *adev, struct stream_out *out, bool restore) |
| 6138 | { |
| 6139 | struct audio_usecase *uc_info; |
| 6140 | float left_p; |
| 6141 | float right_p; |
| 6142 | audio_devices_t devices; |
| 6143 | |
| 6144 | uc_info = get_usecase_from_list(adev, out->usecase); |
| 6145 | if (uc_info == NULL) { |
| 6146 | ALOGE("%s: Could not find the usecase (%d) in the list", |
| 6147 | __func__, out->usecase); |
| 6148 | return -EINVAL; |
| 6149 | } |
| 6150 | |
| 6151 | ALOGD("%s: enter: usecase(%d: %s)", __func__, |
| 6152 | out->usecase, use_case_table[out->usecase]); |
| 6153 | |
| 6154 | if (restore) { |
| 6155 | // restore A2DP device for active usecases and unmute if required |
| 6156 | if ((out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) && |
| 6157 | !is_a2dp_device(uc_info->out_snd_device)) { |
| 6158 | ALOGD("%s: restoring A2DP and unmuting stream", __func__); |
| 6159 | select_devices(adev, uc_info->id); |
| 6160 | pthread_mutex_lock(&out->compr_mute_lock); |
| 6161 | if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && |
| 6162 | (out->a2dp_compress_mute)) { |
| 6163 | out->a2dp_compress_mute = false; |
| 6164 | set_compr_volume(&out->stream, out->volume_l, out->volume_r); |
| 6165 | } |
| 6166 | pthread_mutex_unlock(&out->compr_mute_lock); |
| 6167 | } |
| 6168 | } else { |
| 6169 | // mute compress stream if suspended |
| 6170 | pthread_mutex_lock(&out->compr_mute_lock); |
| 6171 | if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && |
| 6172 | (!out->a2dp_compress_mute)) { |
| 6173 | if (!out->standby) { |
| 6174 | ALOGD("%s: selecting speaker and muting stream", __func__); |
| 6175 | devices = out->devices; |
| 6176 | out->devices = AUDIO_DEVICE_OUT_SPEAKER; |
| 6177 | left_p = out->volume_l; |
| 6178 | right_p = out->volume_r; |
| 6179 | if (out->offload_state == OFFLOAD_STATE_PLAYING) |
| 6180 | compress_pause(out->compr); |
| 6181 | set_compr_volume(&out->stream, 0.0f, 0.0f); |
| 6182 | out->a2dp_compress_mute = true; |
| 6183 | select_devices(adev, out->usecase); |
| 6184 | if (out->offload_state == OFFLOAD_STATE_PLAYING) |
| 6185 | compress_resume(out->compr); |
| 6186 | out->devices = devices; |
| 6187 | out->volume_l = left_p; |
| 6188 | out->volume_r = right_p; |
| 6189 | } |
| 6190 | } |
| 6191 | pthread_mutex_unlock(&out->compr_mute_lock); |
| 6192 | } |
| 6193 | ALOGV("%s: exit", __func__); |
| 6194 | return 0; |
| 6195 | } |
| 6196 | |
| 6197 | int check_a2dp_restore(struct audio_device *adev, struct stream_out *out, bool restore) |
| 6198 | { |
| 6199 | int ret = 0; |
| 6200 | |
| 6201 | lock_output_stream(out); |
| 6202 | pthread_mutex_lock(&adev->lock); |
| 6203 | |
| 6204 | ret = check_a2dp_restore_l(adev, out, restore); |
| 6205 | |
| 6206 | pthread_mutex_unlock(&adev->lock); |
| 6207 | pthread_mutex_unlock(&out->lock); |
| 6208 | return ret; |
| 6209 | } |
| 6210 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6211 | static int adev_open(const hw_module_t *module, const char *name, |
| 6212 | hw_device_t **device) |
| 6213 | { |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 6214 | int i, ret; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6215 | |
Eric Laurent | 2bafff1 | 2016-03-17 12:17:23 -0700 | [diff] [blame] | 6216 | ALOGD("%s: enter", __func__); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6217 | if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL; |
vivek mehta | 1a9b7c0 | 2015-06-25 11:49:38 -0700 | [diff] [blame] | 6218 | pthread_mutex_lock(&adev_init_lock); |
| 6219 | if (audio_device_ref_count != 0) { |
| 6220 | *device = &adev->device.common; |
| 6221 | audio_device_ref_count++; |
| 6222 | ALOGV("%s: returning existing instance of adev", __func__); |
| 6223 | ALOGV("%s: exit", __func__); |
| 6224 | pthread_mutex_unlock(&adev_init_lock); |
| 6225 | return 0; |
| 6226 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6227 | adev = calloc(1, sizeof(struct audio_device)); |
| 6228 | |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 6229 | pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL); |
| 6230 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6231 | adev->device.common.tag = HARDWARE_DEVICE_TAG; |
| 6232 | adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0; |
| 6233 | adev->device.common.module = (struct hw_module_t *)module; |
| 6234 | adev->device.common.close = adev_close; |
| 6235 | |
| 6236 | adev->device.init_check = adev_init_check; |
| 6237 | adev->device.set_voice_volume = adev_set_voice_volume; |
| 6238 | adev->device.set_master_volume = adev_set_master_volume; |
| 6239 | adev->device.get_master_volume = adev_get_master_volume; |
| 6240 | adev->device.set_master_mute = adev_set_master_mute; |
| 6241 | adev->device.get_master_mute = adev_get_master_mute; |
| 6242 | adev->device.set_mode = adev_set_mode; |
| 6243 | adev->device.set_mic_mute = adev_set_mic_mute; |
| 6244 | adev->device.get_mic_mute = adev_get_mic_mute; |
| 6245 | adev->device.set_parameters = adev_set_parameters; |
| 6246 | adev->device.get_parameters = adev_get_parameters; |
| 6247 | adev->device.get_input_buffer_size = adev_get_input_buffer_size; |
| 6248 | adev->device.open_output_stream = adev_open_output_stream; |
| 6249 | adev->device.close_output_stream = adev_close_output_stream; |
| 6250 | adev->device.open_input_stream = adev_open_input_stream; |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 6251 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6252 | adev->device.close_input_stream = adev_close_input_stream; |
| 6253 | adev->device.dump = adev_dump; |
jiabin | 8962a4d | 2018-03-19 18:21:24 -0700 | [diff] [blame] | 6254 | adev->device.get_microphones = adev_get_microphones; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6255 | |
| 6256 | /* Set the default route before the PCM stream is opened */ |
| 6257 | pthread_mutex_lock(&adev->lock); |
| 6258 | adev->mode = AUDIO_MODE_NORMAL; |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 6259 | adev->primary_output = NULL; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6260 | adev->bluetooth_nrec = true; |
Ravi Kumar Alamanda | f996704 | 2013-02-14 19:35:14 -0800 | [diff] [blame] | 6261 | adev->acdb_settings = TTY_MODE_OFF; |
Eric Laurent | 07eeafd | 2013-10-06 12:52:49 -0700 | [diff] [blame] | 6262 | /* adev->cur_hdmi_channels = 0; by calloc() */ |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 6263 | adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int)); |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 6264 | voice_init(adev); |
Ravi Kumar Alamanda | 3b1816c | 2013-02-27 23:01:21 -0800 | [diff] [blame] | 6265 | list_init(&adev->usecase_list); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6266 | pthread_mutex_unlock(&adev->lock); |
| 6267 | |
| 6268 | /* Loads platform specific libraries dynamically */ |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 6269 | adev->platform = platform_init(adev); |
| 6270 | if (!adev->platform) { |
| 6271 | free(adev->snd_dev_ref_cnt); |
| 6272 | free(adev); |
| 6273 | ALOGE("%s: Failed to init platform data, aborting.", __func__); |
| 6274 | *device = NULL; |
vivek mehta | 1a9b7c0 | 2015-06-25 11:49:38 -0700 | [diff] [blame] | 6275 | pthread_mutex_unlock(&adev_init_lock); |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 6276 | return -EINVAL; |
| 6277 | } |
Eric Laurent | 0499d4f | 2014-08-25 22:39:29 -0500 | [diff] [blame] | 6278 | adev->extspk = audio_extn_extspk_init(adev); |
| 6279 | |
Glenn Kasten | 6d53bfd | 2016-04-04 16:58:03 -0700 | [diff] [blame] | 6280 | adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW); |
| 6281 | if (adev->visualizer_lib == NULL) { |
| 6282 | ALOGW("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH); |
| 6283 | } else { |
| 6284 | ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH); |
| 6285 | adev->visualizer_start_output = |
Aniket Kumar Lata | 90c3646 | 2018-04-06 13:56:37 -0700 | [diff] [blame] | 6286 | (int (*)(audio_io_handle_t, int, int, int))dlsym(adev->visualizer_lib, |
Glenn Kasten | 6d53bfd | 2016-04-04 16:58:03 -0700 | [diff] [blame] | 6287 | "visualizer_hal_start_output"); |
| 6288 | adev->visualizer_stop_output = |
| 6289 | (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib, |
| 6290 | "visualizer_hal_stop_output"); |
Eric Laurent | c4aef75 | 2013-09-12 17:45:53 -0700 | [diff] [blame] | 6291 | } |
| 6292 | |
Glenn Kasten | 6d53bfd | 2016-04-04 16:58:03 -0700 | [diff] [blame] | 6293 | adev->offload_effects_lib = dlopen(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, RTLD_NOW); |
| 6294 | if (adev->offload_effects_lib == NULL) { |
| 6295 | ALOGW("%s: DLOPEN failed for %s", __func__, |
| 6296 | OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH); |
| 6297 | } else { |
| 6298 | ALOGV("%s: DLOPEN successful for %s", __func__, |
| 6299 | OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH); |
| 6300 | adev->offload_effects_start_output = |
| 6301 | (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib, |
| 6302 | "offload_effects_bundle_hal_start_output"); |
| 6303 | adev->offload_effects_stop_output = |
| 6304 | (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib, |
| 6305 | "offload_effects_bundle_hal_stop_output"); |
Haynes Mathew George | 41f8665 | 2014-06-17 14:22:15 -0700 | [diff] [blame] | 6306 | } |
| 6307 | |
Glenn Kasten | 6d53bfd | 2016-04-04 16:58:03 -0700 | [diff] [blame] | 6308 | adev->adm_lib = dlopen(ADM_LIBRARY_PATH, RTLD_NOW); |
| 6309 | if (adev->adm_lib == NULL) { |
| 6310 | ALOGW("%s: DLOPEN failed for %s", __func__, ADM_LIBRARY_PATH); |
| 6311 | } else { |
| 6312 | ALOGV("%s: DLOPEN successful for %s", __func__, ADM_LIBRARY_PATH); |
| 6313 | adev->adm_init = (adm_init_t) |
| 6314 | dlsym(adev->adm_lib, "adm_init"); |
| 6315 | adev->adm_deinit = (adm_deinit_t) |
| 6316 | dlsym(adev->adm_lib, "adm_deinit"); |
| 6317 | adev->adm_register_input_stream = (adm_register_input_stream_t) |
| 6318 | dlsym(adev->adm_lib, "adm_register_input_stream"); |
| 6319 | adev->adm_register_output_stream = (adm_register_output_stream_t) |
| 6320 | dlsym(adev->adm_lib, "adm_register_output_stream"); |
| 6321 | adev->adm_deregister_stream = (adm_deregister_stream_t) |
| 6322 | dlsym(adev->adm_lib, "adm_deregister_stream"); |
| 6323 | adev->adm_request_focus = (adm_request_focus_t) |
| 6324 | dlsym(adev->adm_lib, "adm_request_focus"); |
| 6325 | adev->adm_abandon_focus = (adm_abandon_focus_t) |
| 6326 | dlsym(adev->adm_lib, "adm_abandon_focus"); |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 6327 | adev->adm_set_config = (adm_set_config_t) |
| 6328 | dlsym(adev->adm_lib, "adm_set_config"); |
| 6329 | adev->adm_request_focus_v2 = (adm_request_focus_v2_t) |
| 6330 | dlsym(adev->adm_lib, "adm_request_focus_v2"); |
| 6331 | adev->adm_is_noirq_avail = (adm_is_noirq_avail_t) |
| 6332 | dlsym(adev->adm_lib, "adm_is_noirq_avail"); |
| 6333 | adev->adm_on_routing_change = (adm_on_routing_change_t) |
| 6334 | dlsym(adev->adm_lib, "adm_on_routing_change"); |
Haynes Mathew George | 88e6fb2 | 2015-08-19 11:51:34 -0700 | [diff] [blame] | 6335 | } |
| 6336 | |
Ravi Kumar Alamanda | 9f30654 | 2014-04-02 15:11:49 -0700 | [diff] [blame] | 6337 | adev->bt_wb_speech_enabled = false; |
Eric Laurent | cefbbac | 2014-09-04 13:54:10 -0500 | [diff] [blame] | 6338 | adev->enable_voicerx = false; |
Ravi Kumar Alamanda | 9f30654 | 2014-04-02 15:11:49 -0700 | [diff] [blame] | 6339 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6340 | *device = &adev->device.common; |
vivek mehta | 1a9b7c0 | 2015-06-25 11:49:38 -0700 | [diff] [blame] | 6341 | |
Andy Hung | 31aca91 | 2014-03-20 17:14:59 -0700 | [diff] [blame] | 6342 | if (k_enable_extended_precision) |
| 6343 | adev_verify_devices(adev); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6344 | |
Glenn Kasten | 4f99339 | 2014-05-14 07:30:48 -0700 | [diff] [blame] | 6345 | char value[PROPERTY_VALUE_MAX]; |
| 6346 | int trial; |
| 6347 | if (property_get("audio_hal.period_size", value, NULL) > 0) { |
| 6348 | trial = atoi(value); |
| 6349 | if (period_size_is_plausible_for_low_latency(trial)) { |
| 6350 | pcm_config_low_latency.period_size = trial; |
| 6351 | pcm_config_low_latency.start_threshold = trial / 4; |
| 6352 | pcm_config_low_latency.avail_min = trial / 4; |
| 6353 | configured_low_latency_capture_period_size = trial; |
| 6354 | } |
| 6355 | } |
| 6356 | if (property_get("audio_hal.in_period_size", value, NULL) > 0) { |
| 6357 | trial = atoi(value); |
| 6358 | if (period_size_is_plausible_for_low_latency(trial)) { |
| 6359 | configured_low_latency_capture_period_size = trial; |
| 6360 | } |
| 6361 | } |
| 6362 | |
Vignesh Kulothungan | b6f0a9c | 2018-03-22 13:50:22 -0700 | [diff] [blame] | 6363 | adev->mic_break_enabled = property_get_bool("vendor.audio.mic_break", false); |
| 6364 | |
Eric Laurent | 5f4ca95 | 2018-10-19 17:33:43 -0700 | [diff] [blame] | 6365 | adev->camera_orientation = CAMERA_DEFAULT; |
| 6366 | |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 6367 | // commented as full set of app type cfg is sent from platform |
| 6368 | // audio_extn_utils_send_default_app_type_cfg(adev->platform, adev->mixer); |
vivek mehta | 1a9b7c0 | 2015-06-25 11:49:38 -0700 | [diff] [blame] | 6369 | audio_device_ref_count++; |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 6370 | |
| 6371 | if (property_get("audio_hal.period_multiplier", value, NULL) > 0) { |
| 6372 | af_period_multiplier = atoi(value); |
| 6373 | if (af_period_multiplier < 0) { |
| 6374 | af_period_multiplier = 2; |
| 6375 | } else if (af_period_multiplier > 4) { |
| 6376 | af_period_multiplier = 4; |
| 6377 | } |
| 6378 | ALOGV("new period_multiplier = %d", af_period_multiplier); |
| 6379 | } |
| 6380 | |
Alain Vongsouvanh | 13f26e8 | 2016-11-18 14:39:11 -0800 | [diff] [blame] | 6381 | audio_extn_tfa_98xx_init(adev); |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 6382 | audio_extn_ma_init(adev->platform); |
justinweng | c6347db | 2019-02-21 18:49:00 +0800 | [diff] [blame] | 6383 | audio_extn_audiozoom_init(); |
Alain Vongsouvanh | 13f26e8 | 2016-11-18 14:39:11 -0800 | [diff] [blame] | 6384 | |
vivek mehta | 1a9b7c0 | 2015-06-25 11:49:38 -0700 | [diff] [blame] | 6385 | pthread_mutex_unlock(&adev_init_lock); |
| 6386 | |
Haynes Mathew George | 88e6fb2 | 2015-08-19 11:51:34 -0700 | [diff] [blame] | 6387 | if (adev->adm_init) |
| 6388 | adev->adm_data = adev->adm_init(); |
| 6389 | |
Ravi Kumar Alamanda | 533bb72 | 2015-09-23 13:47:03 -0700 | [diff] [blame] | 6390 | audio_extn_perf_lock_init(); |
Haynes Mathew George | c735fb0 | 2016-06-30 18:00:28 -0700 | [diff] [blame] | 6391 | audio_extn_snd_mon_init(); |
| 6392 | pthread_mutex_lock(&adev->lock); |
| 6393 | audio_extn_snd_mon_register_listener(NULL, adev_snd_mon_cb); |
| 6394 | adev->card_status = CARD_STATUS_ONLINE; |
| 6395 | pthread_mutex_unlock(&adev->lock); |
Haynes Mathew George | ceafc55 | 2017-05-24 15:44:23 -0700 | [diff] [blame] | 6396 | audio_extn_sound_trigger_init(adev);/* dependent on snd_mon_init() */ |
Ravi Kumar Alamanda | 533bb72 | 2015-09-23 13:47:03 -0700 | [diff] [blame] | 6397 | |
Eric Laurent | 2bafff1 | 2016-03-17 12:17:23 -0700 | [diff] [blame] | 6398 | ALOGD("%s: exit", __func__); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 6399 | return 0; |
| 6400 | } |
| 6401 | |
| 6402 | static struct hw_module_methods_t hal_module_methods = { |
| 6403 | .open = adev_open, |
| 6404 | }; |
| 6405 | |
| 6406 | struct audio_module HAL_MODULE_INFO_SYM = { |
| 6407 | .common = { |
| 6408 | .tag = HARDWARE_MODULE_TAG, |
| 6409 | .module_api_version = AUDIO_MODULE_API_VERSION_0_1, |
| 6410 | .hal_api_version = HARDWARE_HAL_API_VERSION, |
| 6411 | .id = AUDIO_HARDWARE_MODULE_ID, |
| 6412 | .name = "QCOM Audio HAL", |
| 6413 | .author = "Code Aurora Forum", |
| 6414 | .methods = &hal_module_methods, |
| 6415 | }, |
| 6416 | }; |