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> |
| 43 | #include <cutils/sched_policy.h> |
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> |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 47 | #include <system/thread_defs.h> |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 48 | #include <tinyalsa/asoundlib.h> |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 49 | #include <audio_effects/effect_aec.h> |
| 50 | #include <audio_effects/effect_ns.h> |
Andy Hung | fc044e1 | 2017-03-20 09:24:22 -0700 | [diff] [blame] | 51 | #include <audio_utils/clock.h> |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 52 | #include "audio_hw.h" |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 53 | #include "audio_extn.h" |
Wei Wang | f4837d5 | 2017-11-21 14:51:20 -0800 | [diff] [blame] | 54 | #include "audio_perf.h" |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 55 | #include "platform_api.h" |
| 56 | #include <platform.h> |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 57 | #include "voice_extn.h" |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 58 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 59 | #include "sound/compress_params.h" |
Alain Vongsouvanh | 13f26e8 | 2016-11-18 14:39:11 -0800 | [diff] [blame] | 60 | #include "audio_extn/tfa_98xx.h" |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 61 | |
Eric Laurent | 397db57 | 2016-05-11 11:31:47 -0700 | [diff] [blame] | 62 | /* COMPRESS_OFFLOAD_FRAGMENT_SIZE must be more than 8KB and a multiple of 32KB if more than 32KB. |
| 63 | * 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] | 64 | #define COMPRESS_OFFLOAD_FRAGMENT_SIZE (256 * 1024) |
Marco Nelissen | 94c33a0 | 2015-05-12 09:11:34 -0700 | [diff] [blame] | 65 | // 2 buffers causes problems with high bitrate files |
| 66 | #define COMPRESS_OFFLOAD_NUM_FRAGMENTS 3 |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 67 | /* ToDo: Check and update a proper value in msec */ |
| 68 | #define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 96 |
vivek mehta | 4a82477 | 2017-06-08 19:05:49 -0700 | [diff] [blame] | 69 | /* treat as unsigned Q1.13 */ |
| 70 | #define APP_TYPE_GAIN_DEFAULT 0x2000 |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 71 | #define COMPRESS_PLAYBACK_VOLUME_MAX 0x2000 |
vivek mehta | 4a82477 | 2017-06-08 19:05:49 -0700 | [diff] [blame] | 72 | |
| 73 | /* treat as unsigned Q1.13 */ |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 74 | #define VOIP_PLAYBACK_VOLUME_MAX 0x2000 |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 75 | |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 76 | #define PROXY_OPEN_RETRY_COUNT 100 |
| 77 | #define PROXY_OPEN_WAIT_TIME 20 |
| 78 | |
vivek mehta | dae4471 | 2015-07-27 14:13:18 -0700 | [diff] [blame] | 79 | #define MIN_CHANNEL_COUNT 1 |
| 80 | #define DEFAULT_CHANNEL_COUNT 2 |
| 81 | |
Jean-Michel Trivi | c075069 | 2015-10-12 12:12:32 -0700 | [diff] [blame] | 82 | #ifndef MAX_TARGET_SPECIFIC_CHANNEL_CNT |
| 83 | #define MAX_CHANNEL_COUNT 1 |
| 84 | #else |
vivek mehta | dae4471 | 2015-07-27 14:13:18 -0700 | [diff] [blame] | 85 | #define MAX_CHANNEL_COUNT atoi(XSTR(MAX_TARGET_SPECIFIC_CHANNEL_CNT)) |
| 86 | #define XSTR(x) STR(x) |
| 87 | #define STR(x) #x |
Jean-Michel Trivi | c075069 | 2015-10-12 12:12:32 -0700 | [diff] [blame] | 88 | #endif |
Eric Laurent | 74b5576 | 2017-07-09 17:04:53 -0700 | [diff] [blame] | 89 | #define MAX_HIFI_CHANNEL_COUNT 8 |
vivek mehta | dae4471 | 2015-07-27 14:13:18 -0700 | [diff] [blame] | 90 | |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 91 | #define ULL_PERIOD_SIZE (DEFAULT_OUTPUT_SAMPLING_RATE/1000) |
| 92 | |
Glenn Kasten | 4f99339 | 2014-05-14 07:30:48 -0700 | [diff] [blame] | 93 | static unsigned int configured_low_latency_capture_period_size = |
| 94 | LOW_LATENCY_CAPTURE_PERIOD_SIZE; |
| 95 | |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 96 | |
| 97 | #define MMAP_PERIOD_SIZE (DEFAULT_OUTPUT_SAMPLING_RATE/1000) |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 98 | #define MMAP_PERIOD_COUNT_MIN 32 |
| 99 | #define MMAP_PERIOD_COUNT_MAX 512 |
| 100 | #define MMAP_PERIOD_COUNT_DEFAULT (MMAP_PERIOD_COUNT_MAX) |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 101 | |
Andy Hung | 31aca91 | 2014-03-20 17:14:59 -0700 | [diff] [blame] | 102 | /* This constant enables extended precision handling. |
| 103 | * TODO The flag is off until more testing is done. |
| 104 | */ |
| 105 | static const bool k_enable_extended_precision = false; |
| 106 | |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 107 | struct pcm_config pcm_config_deep_buffer = { |
vivek mehta | dae4471 | 2015-07-27 14:13:18 -0700 | [diff] [blame] | 108 | .channels = DEFAULT_CHANNEL_COUNT, |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 109 | .rate = DEFAULT_OUTPUT_SAMPLING_RATE, |
| 110 | .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE, |
| 111 | .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT, |
| 112 | .format = PCM_FORMAT_S16_LE, |
| 113 | .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4, |
| 114 | .stop_threshold = INT_MAX, |
| 115 | .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4, |
| 116 | }; |
| 117 | |
| 118 | struct pcm_config pcm_config_low_latency = { |
vivek mehta | dae4471 | 2015-07-27 14:13:18 -0700 | [diff] [blame] | 119 | .channels = DEFAULT_CHANNEL_COUNT, |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 120 | .rate = DEFAULT_OUTPUT_SAMPLING_RATE, |
| 121 | .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE, |
| 122 | .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT, |
| 123 | .format = PCM_FORMAT_S16_LE, |
| 124 | .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4, |
| 125 | .stop_threshold = INT_MAX, |
| 126 | .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4, |
| 127 | }; |
| 128 | |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 129 | static int af_period_multiplier = 4; |
| 130 | struct pcm_config pcm_config_rt = { |
| 131 | .channels = DEFAULT_CHANNEL_COUNT, |
| 132 | .rate = DEFAULT_OUTPUT_SAMPLING_RATE, |
| 133 | .period_size = ULL_PERIOD_SIZE, //1 ms |
| 134 | .period_count = 512, //=> buffer size is 512ms |
| 135 | .format = PCM_FORMAT_S16_LE, |
| 136 | .start_threshold = ULL_PERIOD_SIZE*8, //8ms |
| 137 | .stop_threshold = INT_MAX, |
| 138 | .silence_threshold = 0, |
| 139 | .silence_size = 0, |
| 140 | .avail_min = ULL_PERIOD_SIZE, //1 ms |
| 141 | }; |
| 142 | |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 143 | struct pcm_config pcm_config_hdmi_multi = { |
| 144 | .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */ |
| 145 | .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */ |
| 146 | .period_size = HDMI_MULTI_PERIOD_SIZE, |
| 147 | .period_count = HDMI_MULTI_PERIOD_COUNT, |
| 148 | .format = PCM_FORMAT_S16_LE, |
| 149 | .start_threshold = 0, |
| 150 | .stop_threshold = INT_MAX, |
| 151 | .avail_min = 0, |
| 152 | }; |
| 153 | |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 154 | struct pcm_config pcm_config_mmap_playback = { |
| 155 | .channels = DEFAULT_CHANNEL_COUNT, |
| 156 | .rate = DEFAULT_OUTPUT_SAMPLING_RATE, |
| 157 | .period_size = MMAP_PERIOD_SIZE, |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 158 | .period_count = MMAP_PERIOD_COUNT_DEFAULT, |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 159 | .format = PCM_FORMAT_S16_LE, |
| 160 | .start_threshold = MMAP_PERIOD_SIZE*8, |
| 161 | .stop_threshold = INT32_MAX, |
| 162 | .silence_threshold = 0, |
| 163 | .silence_size = 0, |
| 164 | .avail_min = MMAP_PERIOD_SIZE, //1 ms |
| 165 | }; |
| 166 | |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 167 | struct pcm_config pcm_config_hifi = { |
| 168 | .channels = DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */ |
| 169 | .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */ |
| 170 | .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE, /* change #define */ |
| 171 | .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT, |
| 172 | .format = PCM_FORMAT_S24_3LE, |
| 173 | .start_threshold = 0, |
| 174 | .stop_threshold = INT_MAX, |
| 175 | .avail_min = 0, |
| 176 | }; |
| 177 | |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 178 | struct pcm_config pcm_config_audio_capture = { |
vivek mehta | dae4471 | 2015-07-27 14:13:18 -0700 | [diff] [blame] | 179 | .channels = DEFAULT_CHANNEL_COUNT, |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 180 | .period_count = AUDIO_CAPTURE_PERIOD_COUNT, |
| 181 | .format = PCM_FORMAT_S16_LE, |
Eric Laurent | e2d2d1d | 2015-07-06 17:54:15 -0700 | [diff] [blame] | 182 | .stop_threshold = INT_MAX, |
| 183 | .avail_min = 0, |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 184 | }; |
| 185 | |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 186 | struct pcm_config pcm_config_audio_capture_rt = { |
| 187 | .channels = DEFAULT_CHANNEL_COUNT, |
| 188 | .rate = DEFAULT_OUTPUT_SAMPLING_RATE, |
| 189 | .period_size = ULL_PERIOD_SIZE, |
| 190 | .period_count = 512, |
| 191 | .format = PCM_FORMAT_S16_LE, |
| 192 | .start_threshold = 0, |
| 193 | .stop_threshold = INT_MAX, |
| 194 | .silence_threshold = 0, |
| 195 | .silence_size = 0, |
| 196 | .avail_min = ULL_PERIOD_SIZE, //1 ms |
| 197 | }; |
| 198 | |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 199 | struct pcm_config pcm_config_mmap_capture = { |
| 200 | .channels = DEFAULT_CHANNEL_COUNT, |
| 201 | .rate = DEFAULT_OUTPUT_SAMPLING_RATE, |
| 202 | .period_size = MMAP_PERIOD_SIZE, |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 203 | .period_count = MMAP_PERIOD_COUNT_DEFAULT, |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 204 | .format = PCM_FORMAT_S16_LE, |
| 205 | .start_threshold = 0, |
| 206 | .stop_threshold = INT_MAX, |
| 207 | .silence_threshold = 0, |
| 208 | .silence_size = 0, |
| 209 | .avail_min = MMAP_PERIOD_SIZE, //1 ms |
| 210 | }; |
| 211 | |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 212 | struct pcm_config pcm_config_voip = { |
| 213 | .channels = 1, |
| 214 | .period_count = 2, |
| 215 | .format = PCM_FORMAT_S16_LE, |
| 216 | .stop_threshold = INT_MAX, |
| 217 | .avail_min = 0, |
| 218 | }; |
| 219 | |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 220 | #define AFE_PROXY_CHANNEL_COUNT 2 |
| 221 | #define AFE_PROXY_SAMPLING_RATE 48000 |
| 222 | |
| 223 | #define AFE_PROXY_PLAYBACK_PERIOD_SIZE 768 |
| 224 | #define AFE_PROXY_PLAYBACK_PERIOD_COUNT 4 |
| 225 | |
| 226 | struct pcm_config pcm_config_afe_proxy_playback = { |
| 227 | .channels = AFE_PROXY_CHANNEL_COUNT, |
| 228 | .rate = AFE_PROXY_SAMPLING_RATE, |
| 229 | .period_size = AFE_PROXY_PLAYBACK_PERIOD_SIZE, |
| 230 | .period_count = AFE_PROXY_PLAYBACK_PERIOD_COUNT, |
| 231 | .format = PCM_FORMAT_S16_LE, |
| 232 | .start_threshold = AFE_PROXY_PLAYBACK_PERIOD_SIZE, |
| 233 | .stop_threshold = INT_MAX, |
| 234 | .avail_min = AFE_PROXY_PLAYBACK_PERIOD_SIZE, |
| 235 | }; |
| 236 | |
| 237 | #define AFE_PROXY_RECORD_PERIOD_SIZE 768 |
| 238 | #define AFE_PROXY_RECORD_PERIOD_COUNT 4 |
| 239 | |
| 240 | struct pcm_config pcm_config_afe_proxy_record = { |
| 241 | .channels = AFE_PROXY_CHANNEL_COUNT, |
| 242 | .rate = AFE_PROXY_SAMPLING_RATE, |
| 243 | .period_size = AFE_PROXY_RECORD_PERIOD_SIZE, |
| 244 | .period_count = AFE_PROXY_RECORD_PERIOD_COUNT, |
| 245 | .format = PCM_FORMAT_S16_LE, |
| 246 | .start_threshold = AFE_PROXY_RECORD_PERIOD_SIZE, |
| 247 | .stop_threshold = INT_MAX, |
| 248 | .avail_min = AFE_PROXY_RECORD_PERIOD_SIZE, |
| 249 | }; |
| 250 | |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 251 | const char * const use_case_table[AUDIO_USECASE_MAX] = { |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 252 | [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback", |
| 253 | [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback", |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 254 | [USECASE_AUDIO_PLAYBACK_HIFI] = "hifi-playback", |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 255 | [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback", |
Ravi Kumar Alamanda | f78a4d9 | 2015-04-24 15:18:23 -0700 | [diff] [blame] | 256 | [USECASE_AUDIO_PLAYBACK_TTS] = "audio-tts-playback", |
Ravi Kumar Alamanda | 2bc7b02 | 2015-06-25 20:08:01 -0700 | [diff] [blame] | 257 | [USECASE_AUDIO_PLAYBACK_ULL] = "audio-ull-playback", |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 258 | [USECASE_AUDIO_PLAYBACK_MMAP] = "mmap-playback", |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 259 | |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 260 | [USECASE_AUDIO_RECORD] = "audio-record", |
| 261 | [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record", |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 262 | [USECASE_AUDIO_RECORD_MMAP] = "mmap-record", |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 263 | [USECASE_AUDIO_RECORD_HIFI] = "hifi-record", |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 264 | |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 265 | [USECASE_AUDIO_HFP_SCO] = "hfp-sco", |
| 266 | [USECASE_AUDIO_HFP_SCO_WB] = "hfp-sco-wb", |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 267 | |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 268 | [USECASE_VOICE_CALL] = "voice-call", |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 269 | [USECASE_VOICE2_CALL] = "voice2-call", |
| 270 | [USECASE_VOLTE_CALL] = "volte-call", |
| 271 | [USECASE_QCHAT_CALL] = "qchat-call", |
| 272 | [USECASE_VOWLAN_CALL] = "vowlan-call", |
vivek mehta | a51fd40 | 2016-02-04 19:49:33 -0800 | [diff] [blame] | 273 | [USECASE_VOICEMMODE1_CALL] = "voicemmode1-call", |
| 274 | [USECASE_VOICEMMODE2_CALL] = "voicemmode2-call", |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 275 | |
Ravi Kumar Alamanda | 6386300 | 2015-04-22 11:15:25 -0700 | [diff] [blame] | 276 | [USECASE_AUDIO_SPKR_CALIB_RX] = "spkr-rx-calib", |
| 277 | [USECASE_AUDIO_SPKR_CALIB_TX] = "spkr-vi-record", |
| 278 | |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 279 | [USECASE_AUDIO_PLAYBACK_AFE_PROXY] = "afe-proxy-playback", |
| 280 | [USECASE_AUDIO_RECORD_AFE_PROXY] = "afe-proxy-record", |
vivek mehta | 781065c | 2017-04-04 12:55:01 -0700 | [diff] [blame] | 281 | |
| 282 | [USECASE_INCALL_REC_UPLINK] = "incall-rec-uplink", |
| 283 | [USECASE_INCALL_REC_DOWNLINK] = "incall-rec-downlink", |
| 284 | [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK] = "incall-rec-uplink-and-downlink", |
| 285 | |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 286 | [USECASE_AUDIO_PLAYBACK_VOIP] = "audio-playback-voip", |
| 287 | [USECASE_AUDIO_RECORD_VOIP] = "audio-record-voip", |
Nadav Bar | 3d72cfc | 2018-01-07 12:19:24 +0200 | [diff] [blame] | 288 | |
| 289 | [USECASE_INCALL_MUSIC_UPLINK] = "incall-music-uplink", |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 290 | }; |
| 291 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 292 | |
| 293 | #define STRING_TO_ENUM(string) { #string, string } |
| 294 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 295 | struct string_to_enum { |
| 296 | const char *name; |
| 297 | uint32_t value; |
| 298 | }; |
| 299 | |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 300 | static const struct string_to_enum channels_name_to_enum_table[] = { |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 301 | STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO), |
| 302 | STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1), |
| 303 | STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1), |
Eric Laurent | 74b5576 | 2017-07-09 17:04:53 -0700 | [diff] [blame] | 304 | STRING_TO_ENUM(AUDIO_CHANNEL_IN_MONO), |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 305 | STRING_TO_ENUM(AUDIO_CHANNEL_IN_STEREO), |
Eric Laurent | 74b5576 | 2017-07-09 17:04:53 -0700 | [diff] [blame] | 306 | STRING_TO_ENUM(AUDIO_CHANNEL_IN_FRONT_BACK), |
| 307 | STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_1), |
| 308 | STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_2), |
| 309 | STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_3), |
| 310 | STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_4), |
| 311 | STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_5), |
| 312 | STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_6), |
| 313 | STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_7), |
| 314 | STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_8), |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 315 | }; |
| 316 | |
Haynes Mathew George | 5191a85 | 2013-09-11 14:19:36 -0700 | [diff] [blame] | 317 | static int set_voice_volume_l(struct audio_device *adev, float volume); |
vivek mehta | 1a9b7c0 | 2015-06-25 11:49:38 -0700 | [diff] [blame] | 318 | static struct audio_device *adev = NULL; |
Andy Hung | 9e737de | 2017-05-22 10:51:22 -0700 | [diff] [blame] | 319 | static pthread_mutex_t adev_init_lock = PTHREAD_MUTEX_INITIALIZER; |
vivek mehta | 1a9b7c0 | 2015-06-25 11:49:38 -0700 | [diff] [blame] | 320 | static unsigned int audio_device_ref_count; |
vivek mehta | fb4d7bd | 2016-04-29 03:16:47 -0700 | [diff] [blame] | 321 | //cache last MBDRC cal step level |
| 322 | static int last_known_cal_step = -1 ; |
vivek mehta | 1a9b7c0 | 2015-06-25 11:49:38 -0700 | [diff] [blame] | 323 | |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 324 | static int check_a2dp_restore_l(struct audio_device *adev, struct stream_out *out, bool restore); |
| 325 | static int set_compr_volume(struct audio_stream_out *stream, float left, float right); |
| 326 | |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 327 | static bool may_use_noirq_mode(struct audio_device *adev, audio_usecase_t uc_id, |
| 328 | int flags __unused) |
| 329 | { |
| 330 | int dir = 0; |
| 331 | switch (uc_id) { |
| 332 | case USECASE_AUDIO_RECORD_LOW_LATENCY: |
| 333 | dir = 1; |
| 334 | case USECASE_AUDIO_PLAYBACK_ULL: |
| 335 | break; |
| 336 | default: |
| 337 | return false; |
| 338 | } |
| 339 | |
| 340 | int dev_id = platform_get_pcm_device_id(uc_id, dir == 0 ? |
| 341 | PCM_PLAYBACK : PCM_CAPTURE); |
| 342 | if (adev->adm_is_noirq_avail) |
| 343 | return adev->adm_is_noirq_avail(adev->adm_data, |
| 344 | adev->snd_card, dev_id, dir); |
| 345 | return false; |
| 346 | } |
| 347 | |
| 348 | static void register_out_stream(struct stream_out *out) |
| 349 | { |
| 350 | struct audio_device *adev = out->dev; |
| 351 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) |
| 352 | return; |
| 353 | |
| 354 | if (!adev->adm_register_output_stream) |
| 355 | return; |
| 356 | |
| 357 | adev->adm_register_output_stream(adev->adm_data, |
| 358 | out->handle, |
| 359 | out->flags); |
| 360 | |
| 361 | if (!adev->adm_set_config) |
| 362 | return; |
| 363 | |
| 364 | if (out->realtime) { |
| 365 | adev->adm_set_config(adev->adm_data, |
| 366 | out->handle, |
| 367 | out->pcm, &out->config); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | static void register_in_stream(struct stream_in *in) |
| 372 | { |
| 373 | struct audio_device *adev = in->dev; |
| 374 | if (!adev->adm_register_input_stream) |
| 375 | return; |
| 376 | |
| 377 | adev->adm_register_input_stream(adev->adm_data, |
| 378 | in->capture_handle, |
| 379 | in->flags); |
| 380 | |
| 381 | if (!adev->adm_set_config) |
| 382 | return; |
| 383 | |
| 384 | if (in->realtime) { |
| 385 | adev->adm_set_config(adev->adm_data, |
| 386 | in->capture_handle, |
| 387 | in->pcm, |
| 388 | &in->config); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | static void request_out_focus(struct stream_out *out, long ns) |
| 393 | { |
| 394 | struct audio_device *adev = out->dev; |
| 395 | |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 396 | if (adev->adm_request_focus_v2) { |
| 397 | adev->adm_request_focus_v2(adev->adm_data, out->handle, ns); |
| 398 | } else if (adev->adm_request_focus) { |
| 399 | adev->adm_request_focus(adev->adm_data, out->handle); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | static void request_in_focus(struct stream_in *in, long ns) |
| 404 | { |
| 405 | struct audio_device *adev = in->dev; |
| 406 | |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 407 | if (adev->adm_request_focus_v2) { |
| 408 | adev->adm_request_focus_v2(adev->adm_data, in->capture_handle, ns); |
| 409 | } else if (adev->adm_request_focus) { |
| 410 | adev->adm_request_focus(adev->adm_data, in->capture_handle); |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | static void release_out_focus(struct stream_out *out, long ns __unused) |
| 415 | { |
| 416 | struct audio_device *adev = out->dev; |
| 417 | |
| 418 | if (adev->adm_abandon_focus) |
| 419 | adev->adm_abandon_focus(adev->adm_data, out->handle); |
| 420 | } |
| 421 | |
| 422 | static void release_in_focus(struct stream_in *in, long ns __unused) |
| 423 | { |
| 424 | struct audio_device *adev = in->dev; |
| 425 | if (adev->adm_abandon_focus) |
| 426 | adev->adm_abandon_focus(adev->adm_data, in->capture_handle); |
| 427 | } |
| 428 | |
Haynes Mathew George | c735fb0 | 2016-06-30 18:00:28 -0700 | [diff] [blame] | 429 | static int parse_snd_card_status(struct str_parms * parms, int * card, |
| 430 | card_status_t * status) |
| 431 | { |
| 432 | char value[32]={0}; |
| 433 | char state[32]={0}; |
| 434 | |
| 435 | int ret = str_parms_get_str(parms, "SND_CARD_STATUS", value, sizeof(value)); |
| 436 | |
| 437 | if (ret < 0) |
| 438 | return -1; |
| 439 | |
| 440 | // sscanf should be okay as value is of max length 32. |
| 441 | // same as sizeof state. |
| 442 | if (sscanf(value, "%d,%s", card, state) < 2) |
| 443 | return -1; |
| 444 | |
| 445 | *status = !strcmp(state, "ONLINE") ? CARD_STATUS_ONLINE : |
| 446 | CARD_STATUS_OFFLINE; |
| 447 | return 0; |
| 448 | } |
| 449 | |
vivek mehta | 4012509 | 2017-08-21 18:48:51 -0700 | [diff] [blame] | 450 | // always call with adev lock held |
| 451 | void send_gain_dep_calibration_l() { |
| 452 | if (last_known_cal_step >= 0) |
| 453 | platform_send_gain_dep_cal(adev->platform, last_known_cal_step); |
| 454 | } |
| 455 | |
vivek mehta | 1a9b7c0 | 2015-06-25 11:49:38 -0700 | [diff] [blame] | 456 | __attribute__ ((visibility ("default"))) |
| 457 | bool audio_hw_send_gain_dep_calibration(int level) { |
| 458 | bool ret_val = false; |
| 459 | ALOGV("%s: enter ... ", __func__); |
| 460 | |
| 461 | pthread_mutex_lock(&adev_init_lock); |
| 462 | |
| 463 | if (adev != NULL && adev->platform != NULL) { |
| 464 | pthread_mutex_lock(&adev->lock); |
vivek mehta | 4012509 | 2017-08-21 18:48:51 -0700 | [diff] [blame] | 465 | last_known_cal_step = level; |
| 466 | send_gain_dep_calibration_l(); |
vivek mehta | 1a9b7c0 | 2015-06-25 11:49:38 -0700 | [diff] [blame] | 467 | pthread_mutex_unlock(&adev->lock); |
| 468 | } else { |
| 469 | ALOGE("%s: %s is NULL", __func__, adev == NULL ? "adev" : "adev->platform"); |
| 470 | } |
| 471 | |
| 472 | pthread_mutex_unlock(&adev_init_lock); |
| 473 | |
| 474 | ALOGV("%s: exit with ret_val %d ", __func__, ret_val); |
| 475 | return ret_val; |
| 476 | } |
Haynes Mathew George | 5191a85 | 2013-09-11 14:19:36 -0700 | [diff] [blame] | 477 | |
vivek mehta | a8d7c92 | 2016-05-25 14:40:44 -0700 | [diff] [blame] | 478 | __attribute__ ((visibility ("default"))) |
| 479 | int audio_hw_get_gain_level_mapping(struct amp_db_and_gain_table *mapping_tbl, |
| 480 | int table_size) { |
| 481 | int ret_val = 0; |
| 482 | ALOGV("%s: enter ... ", __func__); |
| 483 | |
| 484 | pthread_mutex_lock(&adev_init_lock); |
| 485 | if (adev == NULL) { |
| 486 | ALOGW("%s: adev is NULL .... ", __func__); |
| 487 | goto done; |
| 488 | } |
| 489 | |
| 490 | pthread_mutex_lock(&adev->lock); |
| 491 | ret_val = platform_get_gain_level_mapping(mapping_tbl, table_size); |
| 492 | pthread_mutex_unlock(&adev->lock); |
| 493 | done: |
| 494 | pthread_mutex_unlock(&adev_init_lock); |
| 495 | ALOGV("%s: exit ... ", __func__); |
| 496 | return ret_val; |
| 497 | } |
| 498 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 499 | static bool is_supported_format(audio_format_t format) |
| 500 | { |
Eric Laurent | 8251ac8 | 2014-07-23 11:00:25 -0700 | [diff] [blame] | 501 | switch (format) { |
| 502 | case AUDIO_FORMAT_MP3: |
| 503 | case AUDIO_FORMAT_AAC_LC: |
| 504 | case AUDIO_FORMAT_AAC_HE_V1: |
| 505 | case AUDIO_FORMAT_AAC_HE_V2: |
| 506 | return true; |
| 507 | default: |
| 508 | break; |
| 509 | } |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 510 | return false; |
| 511 | } |
| 512 | |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 513 | static inline bool is_mmap_usecase(audio_usecase_t uc_id) |
| 514 | { |
| 515 | return (uc_id == USECASE_AUDIO_RECORD_AFE_PROXY) || |
| 516 | (uc_id == USECASE_AUDIO_PLAYBACK_AFE_PROXY); |
| 517 | } |
| 518 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 519 | static int get_snd_codec_id(audio_format_t format) |
| 520 | { |
| 521 | int id = 0; |
| 522 | |
Eric Laurent | 8251ac8 | 2014-07-23 11:00:25 -0700 | [diff] [blame] | 523 | switch (format & AUDIO_FORMAT_MAIN_MASK) { |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 524 | case AUDIO_FORMAT_MP3: |
| 525 | id = SND_AUDIOCODEC_MP3; |
| 526 | break; |
| 527 | case AUDIO_FORMAT_AAC: |
| 528 | id = SND_AUDIOCODEC_AAC; |
| 529 | break; |
| 530 | default: |
| 531 | ALOGE("%s: Unsupported audio format", __func__); |
| 532 | } |
| 533 | |
| 534 | return id; |
| 535 | } |
Ravi Kumar Alamanda | f996704 | 2013-02-14 19:35:14 -0800 | [diff] [blame] | 536 | |
Alain Vongsouvanh | 13f26e8 | 2016-11-18 14:39:11 -0800 | [diff] [blame] | 537 | static int audio_ssr_status(struct audio_device *adev) |
| 538 | { |
| 539 | int ret = 0; |
| 540 | struct mixer_ctl *ctl; |
| 541 | const char *mixer_ctl_name = "Audio SSR Status"; |
| 542 | |
| 543 | ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name); |
| 544 | ret = mixer_ctl_get_value(ctl, 0); |
| 545 | ALOGD("%s: value: %d", __func__, ret); |
| 546 | return ret; |
| 547 | } |
| 548 | |
vivek mehta | 4a82477 | 2017-06-08 19:05:49 -0700 | [diff] [blame] | 549 | static void stream_app_type_cfg_init(struct stream_app_type_cfg *cfg) |
| 550 | { |
| 551 | cfg->gain[0] = cfg->gain[1] = APP_TYPE_GAIN_DEFAULT; |
| 552 | } |
| 553 | |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 554 | static bool is_btsco_device(snd_device_t out_snd_device, snd_device_t in_snd_device) |
| 555 | { |
| 556 | return out_snd_device == SND_DEVICE_OUT_BT_SCO || |
| 557 | out_snd_device == SND_DEVICE_OUT_BT_SCO_WB || |
| 558 | in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_WB_NREC || |
| 559 | in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_WB || |
| 560 | in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_NREC || |
| 561 | in_snd_device == SND_DEVICE_IN_BT_SCO_MIC; |
| 562 | |
| 563 | } |
| 564 | |
| 565 | static bool is_a2dp_device(snd_device_t out_snd_device) |
| 566 | { |
| 567 | return out_snd_device == SND_DEVICE_OUT_BT_A2DP; |
| 568 | } |
| 569 | |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 570 | int enable_audio_route(struct audio_device *adev, |
| 571 | struct audio_usecase *usecase) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 572 | { |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 573 | snd_device_t snd_device; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 574 | char mixer_path[50]; |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 575 | |
| 576 | if (usecase == NULL) |
| 577 | return -EINVAL; |
| 578 | |
| 579 | ALOGV("%s: enter: usecase(%d)", __func__, usecase->id); |
| 580 | |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 581 | if (usecase->type == PCM_CAPTURE) |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 582 | snd_device = usecase->in_snd_device; |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 583 | else |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 584 | snd_device = usecase->out_snd_device; |
Yamit Mehta | e3b9956 | 2016-09-16 22:44:00 +0530 | [diff] [blame] | 585 | audio_extn_utils_send_app_type_cfg(adev, usecase); |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 586 | audio_extn_utils_send_audio_calibration(adev, usecase); |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 587 | strcpy(mixer_path, use_case_table[usecase->id]); |
Ravi Kumar Alamanda | 299760a | 2013-11-01 17:29:09 -0500 | [diff] [blame] | 588 | platform_add_backend_name(adev->platform, mixer_path, snd_device); |
Mikhail Naganov | 4ee6e3e | 2018-03-21 16:28:35 +0000 | [diff] [blame] | 589 | audio_extn_sound_trigger_update_stream_status(usecase, ST_EVENT_STREAM_BUSY); |
Eric Laurent | 2e140aa | 2016-06-30 17:14:46 -0700 | [diff] [blame] | 590 | 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] | 591 | audio_route_apply_and_update_path(adev->audio_route, mixer_path); |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 592 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 593 | ALOGV("%s: exit", __func__); |
| 594 | return 0; |
| 595 | } |
| 596 | |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 597 | int disable_audio_route(struct audio_device *adev, |
| 598 | struct audio_usecase *usecase) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 599 | { |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 600 | snd_device_t snd_device; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 601 | char mixer_path[50]; |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 602 | |
| 603 | if (usecase == NULL) |
| 604 | return -EINVAL; |
| 605 | |
| 606 | ALOGV("%s: enter: usecase(%d)", __func__, usecase->id); |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 607 | if (usecase->type == PCM_CAPTURE) |
| 608 | snd_device = usecase->in_snd_device; |
| 609 | else |
| 610 | snd_device = usecase->out_snd_device; |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 611 | strcpy(mixer_path, use_case_table[usecase->id]); |
Ravi Kumar Alamanda | 299760a | 2013-11-01 17:29:09 -0500 | [diff] [blame] | 612 | platform_add_backend_name(adev->platform, mixer_path, snd_device); |
Eric Laurent | 2e140aa | 2016-06-30 17:14:46 -0700 | [diff] [blame] | 613 | ALOGD("%s: usecase(%d) reset and update mixer path: %s", __func__, usecase->id, mixer_path); |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 614 | audio_route_reset_and_update_path(adev->audio_route, mixer_path); |
Mikhail Naganov | 4ee6e3e | 2018-03-21 16:28:35 +0000 | [diff] [blame] | 615 | 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] | 616 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 617 | ALOGV("%s: exit", __func__); |
| 618 | return 0; |
| 619 | } |
| 620 | |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 621 | int enable_snd_device(struct audio_device *adev, |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 622 | snd_device_t snd_device) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 623 | { |
Ravi Kumar Alamanda | b7ea4f5 | 2015-06-08 16:44:05 -0700 | [diff] [blame] | 624 | int i, num_devices = 0; |
| 625 | snd_device_t new_snd_devices[2]; |
vivek mehta | de4849c | 2016-03-03 17:23:38 -0800 | [diff] [blame] | 626 | int ret_val = -EINVAL; |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 627 | if (snd_device < SND_DEVICE_MIN || |
| 628 | snd_device >= SND_DEVICE_MAX) { |
Ravi Kumar Alamanda | 3b1816c | 2013-02-27 23:01:21 -0800 | [diff] [blame] | 629 | ALOGE("%s: Invalid sound device %d", __func__, snd_device); |
vivek mehta | de4849c | 2016-03-03 17:23:38 -0800 | [diff] [blame] | 630 | goto on_error; |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 631 | } |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 632 | |
Ravi Kumar Alamanda | 5a95ff6 | 2015-08-31 17:42:44 -0700 | [diff] [blame] | 633 | platform_send_audio_calibration(adev->platform, snd_device); |
| 634 | |
vivek mehta | de4849c | 2016-03-03 17:23:38 -0800 | [diff] [blame] | 635 | if (adev->snd_dev_ref_cnt[snd_device] >= 1) { |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 636 | ALOGV("%s: snd_device(%d: %s) is already active", |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 637 | __func__, snd_device, platform_get_snd_device_name(snd_device)); |
vivek mehta | de4849c | 2016-03-03 17:23:38 -0800 | [diff] [blame] | 638 | goto on_success; |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 639 | } |
| 640 | |
Ravi Kumar Alamanda | a417cc5 | 2015-05-01 16:41:56 -0700 | [diff] [blame] | 641 | /* due to the possibility of calibration overwrite between listen |
| 642 | and audio, notify sound trigger hal before audio calibration is sent */ |
| 643 | audio_extn_sound_trigger_update_device_status(snd_device, |
| 644 | ST_EVENT_SND_DEVICE_BUSY); |
| 645 | |
Ravi Kumar Alamanda | 6386300 | 2015-04-22 11:15:25 -0700 | [diff] [blame] | 646 | if (audio_extn_spkr_prot_is_enabled()) |
| 647 | audio_extn_spkr_prot_calib_cancel(adev); |
| 648 | |
zhaoyang yin | 4211fad | 2015-06-04 21:13:25 +0800 | [diff] [blame] | 649 | audio_extn_dsm_feedback_enable(adev, snd_device, true); |
| 650 | |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 651 | if (is_a2dp_device(snd_device) && |
| 652 | (audio_extn_a2dp_start_playback() < 0)) { |
| 653 | ALOGE("%s: failed to configure A2DP control path", __func__); |
| 654 | goto on_error; |
| 655 | } |
| 656 | |
Ravi Kumar Alamanda | 6386300 | 2015-04-22 11:15:25 -0700 | [diff] [blame] | 657 | if ((snd_device == SND_DEVICE_OUT_SPEAKER || |
kevinshhsu | 7440d8c | 2018-01-14 22:25:38 +0800 | [diff] [blame] | 658 | snd_device == SND_DEVICE_OUT_SPEAKER_SAFE || |
Ravi Kumar Alamanda | 6386300 | 2015-04-22 11:15:25 -0700 | [diff] [blame] | 659 | snd_device == SND_DEVICE_OUT_VOICE_SPEAKER) && |
| 660 | audio_extn_spkr_prot_is_enabled()) { |
| 661 | if (audio_extn_spkr_prot_get_acdb_id(snd_device) < 0) { |
vivek mehta | de4849c | 2016-03-03 17:23:38 -0800 | [diff] [blame] | 662 | goto on_error; |
Ravi Kumar Alamanda | 6386300 | 2015-04-22 11:15:25 -0700 | [diff] [blame] | 663 | } |
| 664 | if (audio_extn_spkr_prot_start_processing(snd_device)) { |
| 665 | ALOGE("%s: spkr_start_processing failed", __func__); |
vivek mehta | de4849c | 2016-03-03 17:23:38 -0800 | [diff] [blame] | 666 | goto on_error; |
Ravi Kumar Alamanda | 6386300 | 2015-04-22 11:15:25 -0700 | [diff] [blame] | 667 | } |
Haynes Mathew George | 2d809e0 | 2016-09-22 17:38:16 -0700 | [diff] [blame] | 668 | } else if (platform_can_split_snd_device(snd_device, |
| 669 | &num_devices, |
| 670 | new_snd_devices) == 0) { |
Ravi Kumar Alamanda | b7ea4f5 | 2015-06-08 16:44:05 -0700 | [diff] [blame] | 671 | for (i = 0; i < num_devices; i++) { |
| 672 | enable_snd_device(adev, new_snd_devices[i]); |
| 673 | } |
vivek mehta | b650641 | 2015-08-07 16:55:17 -0700 | [diff] [blame] | 674 | platform_set_speaker_gain_in_combo(adev, snd_device, true); |
Ravi Kumar Alamanda | 6386300 | 2015-04-22 11:15:25 -0700 | [diff] [blame] | 675 | } else { |
vivek mehta | de4849c | 2016-03-03 17:23:38 -0800 | [diff] [blame] | 676 | char device_name[DEVICE_NAME_MAX_SIZE] = {0}; |
| 677 | if (platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0 ) { |
| 678 | ALOGE(" %s: Invalid sound device returned", __func__); |
| 679 | goto on_error; |
| 680 | } |
Ed Tam | 70b5c14 | 2016-03-21 19:14:29 -0700 | [diff] [blame] | 681 | |
Eric Laurent | 2e140aa | 2016-06-30 17:14:46 -0700 | [diff] [blame] | 682 | ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, device_name); |
vivek mehta | de4849c | 2016-03-03 17:23:38 -0800 | [diff] [blame] | 683 | audio_route_apply_and_update_path(adev->audio_route, device_name); |
| 684 | } |
| 685 | on_success: |
| 686 | adev->snd_dev_ref_cnt[snd_device]++; |
| 687 | ret_val = 0; |
| 688 | on_error: |
| 689 | return ret_val; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 690 | } |
| 691 | |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 692 | int disable_snd_device(struct audio_device *adev, |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 693 | snd_device_t snd_device) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 694 | { |
Ravi Kumar Alamanda | b7ea4f5 | 2015-06-08 16:44:05 -0700 | [diff] [blame] | 695 | int i, num_devices = 0; |
| 696 | snd_device_t new_snd_devices[2]; |
| 697 | |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 698 | if (snd_device < SND_DEVICE_MIN || |
| 699 | snd_device >= SND_DEVICE_MAX) { |
Ravi Kumar Alamanda | 3b1816c | 2013-02-27 23:01:21 -0800 | [diff] [blame] | 700 | ALOGE("%s: Invalid sound device %d", __func__, snd_device); |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 701 | return -EINVAL; |
| 702 | } |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 703 | if (adev->snd_dev_ref_cnt[snd_device] <= 0) { |
| 704 | ALOGE("%s: device ref cnt is already 0", __func__); |
| 705 | return -EINVAL; |
| 706 | } |
Alain Vongsouvanh | 13f26e8 | 2016-11-18 14:39:11 -0800 | [diff] [blame] | 707 | audio_extn_tfa_98xx_disable_speaker(snd_device); |
| 708 | |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 709 | adev->snd_dev_ref_cnt[snd_device]--; |
| 710 | if (adev->snd_dev_ref_cnt[snd_device] == 0) { |
zhaoyang yin | 4211fad | 2015-06-04 21:13:25 +0800 | [diff] [blame] | 711 | audio_extn_dsm_feedback_enable(adev, snd_device, false); |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 712 | |
| 713 | if (is_a2dp_device(snd_device)) |
| 714 | audio_extn_a2dp_stop_playback(); |
| 715 | |
Ravi Kumar Alamanda | 6386300 | 2015-04-22 11:15:25 -0700 | [diff] [blame] | 716 | if ((snd_device == SND_DEVICE_OUT_SPEAKER || |
kevinshhsu | 7440d8c | 2018-01-14 22:25:38 +0800 | [diff] [blame] | 717 | snd_device == SND_DEVICE_OUT_SPEAKER_SAFE || |
vivek mehta | e59cfb2 | 2017-06-16 15:57:11 -0700 | [diff] [blame] | 718 | snd_device == SND_DEVICE_OUT_SPEAKER_REVERSE || |
Ravi Kumar Alamanda | 6386300 | 2015-04-22 11:15:25 -0700 | [diff] [blame] | 719 | snd_device == SND_DEVICE_OUT_VOICE_SPEAKER) && |
| 720 | audio_extn_spkr_prot_is_enabled()) { |
| 721 | audio_extn_spkr_prot_stop_processing(snd_device); |
vivek mehta | e59cfb2 | 2017-06-16 15:57:11 -0700 | [diff] [blame] | 722 | |
Eric Laurent | 3c1e4b7 | 2017-10-06 11:25:33 -0700 | [diff] [blame] | 723 | // FIXME b/65363602: bullhead is the only Nexus with audio_extn_spkr_prot_is_enabled() |
| 724 | // and does not use speaker swap. As this code causes a problem with device enable ref |
| 725 | // counting we remove it for now. |
vivek mehta | e59cfb2 | 2017-06-16 15:57:11 -0700 | [diff] [blame] | 726 | // when speaker device is disabled, reset swap. |
| 727 | // will be renabled on usecase start |
Eric Laurent | 3c1e4b7 | 2017-10-06 11:25:33 -0700 | [diff] [blame] | 728 | // platform_set_swap_channels(adev, false); |
vivek mehta | e59cfb2 | 2017-06-16 15:57:11 -0700 | [diff] [blame] | 729 | |
Haynes Mathew George | 2d809e0 | 2016-09-22 17:38:16 -0700 | [diff] [blame] | 730 | } else if (platform_can_split_snd_device(snd_device, |
| 731 | &num_devices, |
| 732 | new_snd_devices) == 0) { |
Ravi Kumar Alamanda | b7ea4f5 | 2015-06-08 16:44:05 -0700 | [diff] [blame] | 733 | for (i = 0; i < num_devices; i++) { |
| 734 | disable_snd_device(adev, new_snd_devices[i]); |
| 735 | } |
vivek mehta | b650641 | 2015-08-07 16:55:17 -0700 | [diff] [blame] | 736 | platform_set_speaker_gain_in_combo(adev, snd_device, false); |
Ravi Kumar Alamanda | 6386300 | 2015-04-22 11:15:25 -0700 | [diff] [blame] | 737 | } else { |
vivek mehta | de4849c | 2016-03-03 17:23:38 -0800 | [diff] [blame] | 738 | char device_name[DEVICE_NAME_MAX_SIZE] = {0}; |
| 739 | if (platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0 ) { |
| 740 | ALOGE(" %s: Invalid sound device returned", __func__); |
| 741 | return -EINVAL; |
| 742 | } |
| 743 | |
Eric Laurent | 2e140aa | 2016-06-30 17:14:46 -0700 | [diff] [blame] | 744 | ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, device_name); |
vivek mehta | de4849c | 2016-03-03 17:23:38 -0800 | [diff] [blame] | 745 | audio_route_reset_and_update_path(adev->audio_route, device_name); |
Ravi Kumar Alamanda | 6386300 | 2015-04-22 11:15:25 -0700 | [diff] [blame] | 746 | } |
Ravi Kumar Alamanda | a417cc5 | 2015-05-01 16:41:56 -0700 | [diff] [blame] | 747 | audio_extn_sound_trigger_update_device_status(snd_device, |
| 748 | ST_EVENT_SND_DEVICE_FREE); |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 749 | } |
vivek mehta | b650641 | 2015-08-07 16:55:17 -0700 | [diff] [blame] | 750 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 751 | return 0; |
| 752 | } |
| 753 | |
Haynes Mathew George | 2d809e0 | 2016-09-22 17:38:16 -0700 | [diff] [blame] | 754 | /* |
| 755 | legend: |
| 756 | uc - existing usecase |
| 757 | new_uc - new usecase |
| 758 | d1, d11, d2 - SND_DEVICE enums |
| 759 | a1, a2 - corresponding ANDROID device enums |
| 760 | B, B1, B2 - backend strings |
| 761 | |
| 762 | case 1 |
| 763 | uc->dev d1 (a1) B1 |
| 764 | new_uc->dev d1 (a1), d2 (a2) B1, B2 |
| 765 | |
| 766 | resolution: disable and enable uc->dev on d1 |
| 767 | |
| 768 | case 2 |
| 769 | uc->dev d1 (a1) B1 |
| 770 | new_uc->dev d11 (a1) B1 |
| 771 | |
| 772 | resolution: need to switch uc since d1 and d11 are related |
| 773 | (e.g. speaker and voice-speaker) |
| 774 | use ANDROID_DEVICE_OUT enums to match devices since SND_DEVICE enums may vary |
| 775 | |
| 776 | case 3 |
| 777 | uc->dev d1 (a1) B1 |
| 778 | new_uc->dev d2 (a2) B2 |
| 779 | |
| 780 | resolution: no need to switch uc |
| 781 | |
| 782 | case 4 |
| 783 | uc->dev d1 (a1) B |
| 784 | new_uc->dev d2 (a2) B |
| 785 | |
| 786 | resolution: disable enable uc-dev on d2 since backends match |
| 787 | we cannot enable two streams on two different devices if they |
| 788 | share the same backend. e.g. if offload is on speaker device using |
| 789 | QUAD_MI2S backend and a low-latency stream is started on voice-handset |
| 790 | using the same backend, offload must also be switched to voice-handset. |
| 791 | |
| 792 | case 5 |
| 793 | uc->dev d1 (a1) B |
| 794 | new_uc->dev d1 (a1), d2 (a2) B |
| 795 | |
| 796 | resolution: disable enable uc-dev on d2 since backends match |
| 797 | we cannot enable two streams on two different devices if they |
| 798 | share the same backend. |
| 799 | |
| 800 | case 6 |
| 801 | uc->dev d1 a1 B1 |
| 802 | new_uc->dev d2 a1 B2 |
| 803 | |
| 804 | resolution: no need to switch |
| 805 | |
| 806 | case 7 |
| 807 | |
| 808 | uc->dev d1 (a1), d2 (a2) B1, B2 |
| 809 | new_uc->dev d1 B1 |
| 810 | |
| 811 | resolution: no need to switch |
| 812 | |
| 813 | */ |
| 814 | static snd_device_t derive_playback_snd_device(struct audio_usecase *uc, |
| 815 | struct audio_usecase *new_uc, |
| 816 | snd_device_t new_snd_device) |
| 817 | { |
| 818 | audio_devices_t a1 = uc->stream.out->devices; |
| 819 | audio_devices_t a2 = new_uc->stream.out->devices; |
| 820 | |
| 821 | snd_device_t d1 = uc->out_snd_device; |
| 822 | snd_device_t d2 = new_snd_device; |
| 823 | |
| 824 | // Treat as a special case when a1 and a2 are not disjoint |
| 825 | if ((a1 != a2) && (a1 & a2)) { |
| 826 | snd_device_t d3[2]; |
| 827 | int num_devices = 0; |
| 828 | int ret = platform_can_split_snd_device(popcount(a1) > 1 ? d1 : d2, |
| 829 | &num_devices, |
| 830 | d3); |
| 831 | if (ret < 0) { |
| 832 | if (ret != -ENOSYS) { |
| 833 | ALOGW("%s failed to split snd_device %d", |
| 834 | __func__, |
| 835 | popcount(a1) > 1 ? d1 : d2); |
| 836 | } |
| 837 | goto end; |
| 838 | } |
| 839 | |
| 840 | // NB: case 7 is hypothetical and isn't a practical usecase yet. |
| 841 | // But if it does happen, we need to give priority to d2 if |
| 842 | // the combo devices active on the existing usecase share a backend. |
| 843 | // This is because we cannot have a usecase active on a combo device |
| 844 | // and a new usecase requests one device in this combo pair. |
| 845 | if (platform_check_backends_match(d3[0], d3[1])) { |
| 846 | return d2; // case 5 |
| 847 | } else { |
| 848 | return d1; // case 1 |
| 849 | } |
| 850 | } else { |
| 851 | if (platform_check_backends_match(d1, d2)) { |
| 852 | return d2; // case 2, 4 |
| 853 | } else { |
| 854 | return d1; // case 6, 3 |
| 855 | } |
| 856 | } |
| 857 | |
| 858 | end: |
| 859 | return d2; // return whatever was calculated before. |
| 860 | } |
| 861 | |
Ravi Kumar Alamanda | b7ea4f5 | 2015-06-08 16:44:05 -0700 | [diff] [blame] | 862 | static void check_and_route_playback_usecases(struct audio_device *adev, |
| 863 | struct audio_usecase *uc_info, |
| 864 | snd_device_t snd_device) |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 865 | { |
| 866 | struct listnode *node; |
| 867 | struct audio_usecase *usecase; |
| 868 | bool switch_device[AUDIO_USECASE_MAX]; |
| 869 | int i, num_uc_to_switch = 0; |
| 870 | |
Haynes Mathew George | 137a2ee | 2017-05-23 17:03:35 -0700 | [diff] [blame] | 871 | bool force_routing = platform_check_and_set_playback_backend_cfg(adev, |
| 872 | uc_info, |
| 873 | snd_device); |
David Lin | ee3fe40 | 2017-03-13 10:00:42 -0700 | [diff] [blame] | 874 | |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 875 | /* For a2dp device reconfigure all active sessions |
| 876 | * with new AFE encoder format based on a2dp state |
| 877 | */ |
| 878 | if ((SND_DEVICE_OUT_BT_A2DP == snd_device || |
| 879 | SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP == snd_device) && |
| 880 | audio_extn_a2dp_is_force_device_switch()) { |
| 881 | force_routing = true; |
| 882 | } |
| 883 | |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 884 | /* |
| 885 | * This function is to make sure that all the usecases that are active on |
| 886 | * the hardware codec backend are always routed to any one device that is |
| 887 | * handled by the hardware codec. |
| 888 | * For example, if low-latency and deep-buffer usecases are currently active |
| 889 | * on speaker and out_set_parameters(headset) is received on low-latency |
| 890 | * output, then we have to make sure deep-buffer is also switched to headset, |
| 891 | * because of the limitation that both the devices cannot be enabled |
| 892 | * at the same time as they share the same backend. |
| 893 | */ |
| 894 | /* Disable all the usecases on the shared backend other than the |
| 895 | specified usecase */ |
| 896 | for (i = 0; i < AUDIO_USECASE_MAX; i++) |
| 897 | switch_device[i] = false; |
| 898 | |
| 899 | list_for_each(node, &adev->usecase_list) { |
| 900 | usecase = node_to_item(node, struct audio_usecase, list); |
Haynes Mathew George | 137a2ee | 2017-05-23 17:03:35 -0700 | [diff] [blame] | 901 | if (usecase->type == PCM_CAPTURE || usecase == uc_info) |
| 902 | continue; |
| 903 | |
| 904 | if (force_routing || |
| 905 | (usecase->out_snd_device != snd_device && |
Haynes Mathew George | 39c55dc | 2017-07-11 19:31:23 -0700 | [diff] [blame] | 906 | (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND || |
| 907 | 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] | 908 | platform_check_backends_match(snd_device, usecase->out_snd_device))) { |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 909 | ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..", |
| 910 | __func__, use_case_table[usecase->id], |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 911 | platform_get_snd_device_name(usecase->out_snd_device)); |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 912 | disable_audio_route(adev, usecase); |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 913 | switch_device[usecase->id] = true; |
| 914 | num_uc_to_switch++; |
| 915 | } |
| 916 | } |
| 917 | |
| 918 | if (num_uc_to_switch) { |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 919 | list_for_each(node, &adev->usecase_list) { |
| 920 | usecase = node_to_item(node, struct audio_usecase, list); |
| 921 | if (switch_device[usecase->id]) { |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 922 | disable_snd_device(adev, usecase->out_snd_device); |
sangwon.jeon | 866d5ff | 2013-10-17 21:42:50 +0900 | [diff] [blame] | 923 | } |
| 924 | } |
| 925 | |
Haynes Mathew George | 2d809e0 | 2016-09-22 17:38:16 -0700 | [diff] [blame] | 926 | snd_device_t d_device; |
sangwon.jeon | 866d5ff | 2013-10-17 21:42:50 +0900 | [diff] [blame] | 927 | list_for_each(node, &adev->usecase_list) { |
| 928 | usecase = node_to_item(node, struct audio_usecase, list); |
| 929 | if (switch_device[usecase->id]) { |
Haynes Mathew George | 2d809e0 | 2016-09-22 17:38:16 -0700 | [diff] [blame] | 930 | d_device = derive_playback_snd_device(usecase, uc_info, |
| 931 | snd_device); |
| 932 | enable_snd_device(adev, d_device); |
| 933 | /* Update the out_snd_device before enabling the audio route */ |
| 934 | usecase->out_snd_device = d_device; |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 935 | } |
| 936 | } |
| 937 | |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 938 | /* Re-route all the usecases on the shared backend other than the |
| 939 | specified usecase to new snd devices */ |
| 940 | list_for_each(node, &adev->usecase_list) { |
| 941 | usecase = node_to_item(node, struct audio_usecase, list); |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 942 | if (switch_device[usecase->id] ) { |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 943 | enable_audio_route(adev, usecase); |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 944 | } |
| 945 | } |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 946 | } |
| 947 | } |
| 948 | |
Ravi Kumar Alamanda | c4ba743 | 2013-06-05 14:11:39 -0700 | [diff] [blame] | 949 | static void check_and_route_capture_usecases(struct audio_device *adev, |
| 950 | struct audio_usecase *uc_info, |
| 951 | snd_device_t snd_device) |
| 952 | { |
| 953 | struct listnode *node; |
| 954 | struct audio_usecase *usecase; |
| 955 | bool switch_device[AUDIO_USECASE_MAX]; |
| 956 | int i, num_uc_to_switch = 0; |
| 957 | |
vivek mehta | 4ed66e6 | 2016-04-15 23:33:34 -0700 | [diff] [blame] | 958 | platform_check_and_set_capture_backend_cfg(adev, uc_info, snd_device); |
| 959 | |
Ravi Kumar Alamanda | c4ba743 | 2013-06-05 14:11:39 -0700 | [diff] [blame] | 960 | /* |
| 961 | * This function is to make sure that all the active capture usecases |
| 962 | * are always routed to the same input sound device. |
| 963 | * For example, if audio-record and voice-call usecases are currently |
| 964 | * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece) |
| 965 | * is received for voice call then we have to make sure that audio-record |
| 966 | * usecase is also switched to earpiece i.e. voice-dmic-ef, |
| 967 | * because of the limitation that two devices cannot be enabled |
| 968 | * at the same time if they share the same backend. |
| 969 | */ |
| 970 | for (i = 0; i < AUDIO_USECASE_MAX; i++) |
| 971 | switch_device[i] = false; |
| 972 | |
| 973 | list_for_each(node, &adev->usecase_list) { |
| 974 | usecase = node_to_item(node, struct audio_usecase, list); |
| 975 | if (usecase->type != PCM_PLAYBACK && |
| 976 | usecase != uc_info && |
Anish Kumar | ff86471 | 2015-06-03 13:35:11 -0700 | [diff] [blame] | 977 | usecase->in_snd_device != snd_device && |
| 978 | (usecase->id != USECASE_AUDIO_SPKR_CALIB_TX)) { |
Ravi Kumar Alamanda | c4ba743 | 2013-06-05 14:11:39 -0700 | [diff] [blame] | 979 | ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..", |
| 980 | __func__, use_case_table[usecase->id], |
Devin Kim | 1e5f353 | 2013-08-09 07:48:29 -0700 | [diff] [blame] | 981 | platform_get_snd_device_name(usecase->in_snd_device)); |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 982 | disable_audio_route(adev, usecase); |
Ravi Kumar Alamanda | c4ba743 | 2013-06-05 14:11:39 -0700 | [diff] [blame] | 983 | switch_device[usecase->id] = true; |
| 984 | num_uc_to_switch++; |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | if (num_uc_to_switch) { |
Ravi Kumar Alamanda | c4ba743 | 2013-06-05 14:11:39 -0700 | [diff] [blame] | 989 | list_for_each(node, &adev->usecase_list) { |
| 990 | usecase = node_to_item(node, struct audio_usecase, list); |
| 991 | if (switch_device[usecase->id]) { |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 992 | disable_snd_device(adev, usecase->in_snd_device); |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 993 | } |
| 994 | } |
| 995 | |
| 996 | list_for_each(node, &adev->usecase_list) { |
| 997 | usecase = node_to_item(node, struct audio_usecase, list); |
| 998 | if (switch_device[usecase->id]) { |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 999 | enable_snd_device(adev, snd_device); |
Ravi Kumar Alamanda | c4ba743 | 2013-06-05 14:11:39 -0700 | [diff] [blame] | 1000 | } |
| 1001 | } |
| 1002 | |
Ravi Kumar Alamanda | c4ba743 | 2013-06-05 14:11:39 -0700 | [diff] [blame] | 1003 | /* Re-route all the usecases on the shared backend other than the |
| 1004 | specified usecase to new snd devices */ |
| 1005 | list_for_each(node, &adev->usecase_list) { |
| 1006 | usecase = node_to_item(node, struct audio_usecase, list); |
| 1007 | /* Update the in_snd_device only before enabling the audio route */ |
| 1008 | if (switch_device[usecase->id] ) { |
| 1009 | usecase->in_snd_device = snd_device; |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 1010 | enable_audio_route(adev, usecase); |
Ravi Kumar Alamanda | c4ba743 | 2013-06-05 14:11:39 -0700 | [diff] [blame] | 1011 | } |
| 1012 | } |
Ravi Kumar Alamanda | c4ba743 | 2013-06-05 14:11:39 -0700 | [diff] [blame] | 1013 | } |
| 1014 | } |
| 1015 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1016 | /* must be called with hw device mutex locked */ |
Ravi Kumar Alamanda | b199506 | 2013-03-21 23:18:20 -0700 | [diff] [blame] | 1017 | static int read_hdmi_channel_masks(struct stream_out *out) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1018 | { |
Ravi Kumar Alamanda | b199506 | 2013-03-21 23:18:20 -0700 | [diff] [blame] | 1019 | int ret = 0; |
Haynes Mathew George | 47cd4cb | 2013-07-19 11:58:50 -0700 | [diff] [blame] | 1020 | int channels = platform_edid_get_max_channels(out->dev->platform); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1021 | |
| 1022 | switch (channels) { |
| 1023 | /* |
| 1024 | * Do not handle stereo output in Multi-channel cases |
| 1025 | * Stereo case is handled in normal playback path |
| 1026 | */ |
| 1027 | case 6: |
| 1028 | ALOGV("%s: HDMI supports 5.1", __func__); |
| 1029 | out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1; |
| 1030 | break; |
| 1031 | case 8: |
| 1032 | ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__); |
| 1033 | out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1; |
| 1034 | out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1; |
| 1035 | break; |
| 1036 | default: |
Ravi Kumar Alamanda | b199506 | 2013-03-21 23:18:20 -0700 | [diff] [blame] | 1037 | ALOGE("HDMI does not support multi channel playback"); |
| 1038 | ret = -ENOSYS; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1039 | break; |
| 1040 | } |
Ravi Kumar Alamanda | b199506 | 2013-03-21 23:18:20 -0700 | [diff] [blame] | 1041 | return ret; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1042 | } |
| 1043 | |
Andy Hung | 1885941 | 2017-08-09 11:47:21 -0700 | [diff] [blame] | 1044 | static ssize_t read_usb_sup_sample_rates(bool is_playback, |
| 1045 | uint32_t *supported_sample_rates, |
| 1046 | uint32_t max_rates) |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 1047 | { |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 1048 | ssize_t count = audio_extn_usb_sup_sample_rates(is_playback, |
| 1049 | supported_sample_rates, |
| 1050 | max_rates); |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 1051 | #if !LOG_NDEBUG |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 1052 | for (ssize_t i=0; i<count; i++) { |
| 1053 | ALOGV("%s %s %d", __func__, is_playback ? "P" : "C", |
| 1054 | supported_sample_rates[i]); |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 1055 | } |
| 1056 | #endif |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 1057 | return count; |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 1058 | } |
| 1059 | |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 1060 | static int read_usb_sup_channel_masks(bool is_playback, |
| 1061 | audio_channel_mask_t *supported_channel_masks, |
Eric Laurent | 74b5576 | 2017-07-09 17:04:53 -0700 | [diff] [blame] | 1062 | uint32_t max_masks) |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 1063 | { |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 1064 | int channels = audio_extn_usb_get_max_channels(is_playback); |
Eric Laurent | 74b5576 | 2017-07-09 17:04:53 -0700 | [diff] [blame] | 1065 | int channel_count; |
| 1066 | uint32_t num_masks = 0; |
| 1067 | if (channels > MAX_HIFI_CHANNEL_COUNT) { |
| 1068 | channels = MAX_HIFI_CHANNEL_COUNT; |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 1069 | } |
Eric Laurent | 74b5576 | 2017-07-09 17:04:53 -0700 | [diff] [blame] | 1070 | if (is_playback) { |
Andy Hung | 6c1afb1 | 2017-12-22 11:34:41 -0800 | [diff] [blame] | 1071 | // start from 2 channels as framework currently doesn't support mono. |
| 1072 | // TODO: consider only supporting channel index masks beyond stereo here. |
| 1073 | for (channel_count = FCC_2; |
| 1074 | channel_count <= channels && num_masks < max_masks; |
| 1075 | ++channel_count) { |
Eric Laurent | 74b5576 | 2017-07-09 17:04:53 -0700 | [diff] [blame] | 1076 | supported_channel_masks[num_masks++] = audio_channel_out_mask_from_count(channel_count); |
| 1077 | } |
Andy Hung | 6c1afb1 | 2017-12-22 11:34:41 -0800 | [diff] [blame] | 1078 | for (channel_count = FCC_2; |
| 1079 | channel_count <= channels && num_masks < max_masks; |
| 1080 | ++channel_count) { |
Eric Laurent | 74b5576 | 2017-07-09 17:04:53 -0700 | [diff] [blame] | 1081 | supported_channel_masks[num_masks++] = |
| 1082 | audio_channel_mask_for_index_assignment_from_count(channel_count); |
| 1083 | } |
| 1084 | } else { |
| 1085 | // For capture we report all supported channel masks from 1 channel up. |
| 1086 | channel_count = MIN_CHANNEL_COUNT; |
| 1087 | // audio_channel_in_mask_from_count() does the right conversion to either positional or |
| 1088 | // indexed mask |
| 1089 | for ( ; channel_count <= channels && num_masks < max_masks; channel_count++) { |
| 1090 | supported_channel_masks[num_masks++] = |
| 1091 | audio_channel_in_mask_from_count(channel_count); |
| 1092 | } |
| 1093 | } |
Andy Hung | 6c1afb1 | 2017-12-22 11:34:41 -0800 | [diff] [blame] | 1094 | #ifdef NDEBUG |
| 1095 | for (size_t i = 0; i < num_masks; ++i) { |
| 1096 | ALOGV("%s: %s supported ch %d supported_channel_masks[%zu] %08x num_masks %d", __func__, |
| 1097 | is_playback ? "P" : "C", channels, i, supported_channel_masks[i], num_masks); |
| 1098 | } |
| 1099 | #endif |
Eric Laurent | 74b5576 | 2017-07-09 17:04:53 -0700 | [diff] [blame] | 1100 | return num_masks; |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 1101 | } |
| 1102 | |
Haynes Mathew George | e95340e | 2017-05-24 15:42:06 -0700 | [diff] [blame] | 1103 | static int read_usb_sup_formats(bool is_playback __unused, |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 1104 | audio_format_t *supported_formats, |
| 1105 | uint32_t max_formats __unused) |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 1106 | { |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 1107 | int bitwidth = audio_extn_usb_get_max_bit_width(is_playback); |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 1108 | switch (bitwidth) { |
| 1109 | case 24: |
| 1110 | // XXX : usb.c returns 24 for s24 and s24_le? |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 1111 | supported_formats[0] = AUDIO_FORMAT_PCM_24_BIT_PACKED; |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 1112 | break; |
| 1113 | case 32: |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 1114 | supported_formats[0] = AUDIO_FORMAT_PCM_32_BIT; |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 1115 | break; |
| 1116 | case 16: |
| 1117 | default : |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 1118 | supported_formats[0] = AUDIO_FORMAT_PCM_16_BIT; |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 1119 | break; |
| 1120 | } |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 1121 | ALOGV("%s: %s supported format %d", __func__, |
| 1122 | is_playback ? "P" : "C", bitwidth); |
| 1123 | return 1; |
| 1124 | } |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 1125 | |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 1126 | static int read_usb_sup_params_and_compare(bool is_playback, |
| 1127 | audio_format_t *format, |
| 1128 | audio_format_t *supported_formats, |
| 1129 | uint32_t max_formats, |
| 1130 | audio_channel_mask_t *mask, |
| 1131 | audio_channel_mask_t *supported_channel_masks, |
| 1132 | uint32_t max_masks, |
| 1133 | uint32_t *rate, |
| 1134 | uint32_t *supported_sample_rates, |
| 1135 | uint32_t max_rates) { |
| 1136 | int ret = 0; |
| 1137 | int num_formats; |
| 1138 | int num_masks; |
| 1139 | int num_rates; |
| 1140 | int i; |
| 1141 | |
| 1142 | num_formats = read_usb_sup_formats(is_playback, supported_formats, |
| 1143 | max_formats); |
| 1144 | num_masks = read_usb_sup_channel_masks(is_playback, supported_channel_masks, |
| 1145 | max_masks); |
Eric Laurent | 74b5576 | 2017-07-09 17:04:53 -0700 | [diff] [blame] | 1146 | |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 1147 | num_rates = read_usb_sup_sample_rates(is_playback, |
| 1148 | supported_sample_rates, max_rates); |
| 1149 | |
| 1150 | #define LUT(table, len, what, dflt) \ |
| 1151 | for (i=0; i<len && (table[i] != what); i++); \ |
| 1152 | if (i==len) { ret |= (what == dflt ? 0 : -1); what=table[0]; } |
| 1153 | |
| 1154 | LUT(supported_formats, num_formats, *format, AUDIO_FORMAT_DEFAULT); |
| 1155 | LUT(supported_channel_masks, num_masks, *mask, AUDIO_CHANNEL_NONE); |
| 1156 | LUT(supported_sample_rates, num_rates, *rate, 0); |
| 1157 | |
| 1158 | #undef LUT |
| 1159 | return ret < 0 ? -EINVAL : 0; // HACK TBD |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 1160 | } |
| 1161 | |
Andy Hung | d9653bd | 2017-08-01 19:31:39 -0700 | [diff] [blame] | 1162 | static bool is_usb_ready(struct audio_device *adev, bool is_playback) |
| 1163 | { |
| 1164 | // Check if usb is ready. |
| 1165 | // The usb device may have been removed quickly after insertion and hence |
| 1166 | // no longer available. This will show up as empty channel masks, or rates. |
| 1167 | |
| 1168 | pthread_mutex_lock(&adev->lock); |
| 1169 | uint32_t supported_sample_rate; |
| 1170 | |
| 1171 | // we consider usb ready if we can fetch at least one sample rate. |
| 1172 | const bool ready = read_usb_sup_sample_rates( |
| 1173 | is_playback, &supported_sample_rate, 1 /* max_rates */) > 0; |
| 1174 | pthread_mutex_unlock(&adev->lock); |
| 1175 | return ready; |
| 1176 | } |
| 1177 | |
Ravi Kumar Alamanda | a237ecc | 2014-07-24 17:27:05 -0700 | [diff] [blame] | 1178 | static audio_usecase_t get_voice_usecase_id_from_list(struct audio_device *adev) |
| 1179 | { |
| 1180 | struct audio_usecase *usecase; |
| 1181 | struct listnode *node; |
| 1182 | |
| 1183 | list_for_each(node, &adev->usecase_list) { |
| 1184 | usecase = node_to_item(node, struct audio_usecase, list); |
| 1185 | if (usecase->type == VOICE_CALL) { |
| 1186 | ALOGV("%s: usecase id %d", __func__, usecase->id); |
| 1187 | return usecase->id; |
| 1188 | } |
| 1189 | } |
| 1190 | return USECASE_INVALID; |
| 1191 | } |
| 1192 | |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 1193 | struct audio_usecase *get_usecase_from_list(struct audio_device *adev, |
| 1194 | audio_usecase_t uc_id) |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1195 | { |
| 1196 | struct audio_usecase *usecase; |
| 1197 | struct listnode *node; |
| 1198 | |
| 1199 | list_for_each(node, &adev->usecase_list) { |
| 1200 | usecase = node_to_item(node, struct audio_usecase, list); |
| 1201 | if (usecase->id == uc_id) |
| 1202 | return usecase; |
| 1203 | } |
| 1204 | return NULL; |
| 1205 | } |
| 1206 | |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 1207 | static bool force_device_switch(struct audio_usecase *usecase) |
| 1208 | { |
| 1209 | if (usecase->stream.out == NULL) { |
| 1210 | ALOGE("%s: stream.out is NULL", __func__); |
| 1211 | return false; |
| 1212 | } |
| 1213 | |
| 1214 | // Force all A2DP output devices to reconfigure for proper AFE encode format |
| 1215 | // Also handle a case where in earlier A2DP start failed as A2DP stream was |
| 1216 | // in suspended state, hence try to trigger a retry when we again get a routing request. |
| 1217 | if ((usecase->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) && |
| 1218 | audio_extn_a2dp_is_force_device_switch()) { |
| 1219 | ALOGD("%s: Force A2DP device switch to update new encoder config", __func__); |
| 1220 | return true; |
| 1221 | } |
| 1222 | |
| 1223 | return false; |
| 1224 | } |
| 1225 | |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 1226 | int select_devices(struct audio_device *adev, |
| 1227 | audio_usecase_t uc_id) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1228 | { |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 1229 | snd_device_t out_snd_device = SND_DEVICE_NONE; |
| 1230 | snd_device_t in_snd_device = SND_DEVICE_NONE; |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1231 | struct audio_usecase *usecase = NULL; |
| 1232 | struct audio_usecase *vc_usecase = NULL; |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 1233 | struct audio_usecase *hfp_usecase = NULL; |
| 1234 | audio_usecase_t hfp_ucid; |
Ravi Kumar Alamanda | 3b1816c | 2013-02-27 23:01:21 -0800 | [diff] [blame] | 1235 | struct listnode *node; |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1236 | int status = 0; |
Eric Laurent | f4520b0 | 2017-09-20 18:31:58 -0700 | [diff] [blame] | 1237 | struct audio_usecase *voip_usecase = get_usecase_from_list(adev, |
| 1238 | USECASE_AUDIO_PLAYBACK_VOIP); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1239 | |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1240 | usecase = get_usecase_from_list(adev, uc_id); |
| 1241 | if (usecase == NULL) { |
| 1242 | ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id); |
| 1243 | return -EINVAL; |
| 1244 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1245 | |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 1246 | if ((usecase->type == VOICE_CALL) || |
| 1247 | (usecase->type == PCM_HFP_CALL)) { |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 1248 | out_snd_device = platform_get_output_snd_device(adev->platform, |
| 1249 | usecase->stream.out->devices); |
| 1250 | in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices); |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1251 | usecase->devices = usecase->stream.out->devices; |
| 1252 | } else { |
| 1253 | /* |
| 1254 | * If the voice call is active, use the sound devices of voice call usecase |
| 1255 | * so that it would not result any device switch. All the usecases will |
| 1256 | * be switched to new device when select_devices() is called for voice call |
| 1257 | * usecase. This is to avoid switching devices for voice call when |
Ravi Kumar Alamanda | b7ea4f5 | 2015-06-08 16:44:05 -0700 | [diff] [blame] | 1258 | * check_and_route_playback_usecases() is called below. |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1259 | */ |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 1260 | if (voice_is_in_call(adev)) { |
Ravi Kumar Alamanda | a237ecc | 2014-07-24 17:27:05 -0700 | [diff] [blame] | 1261 | vc_usecase = get_usecase_from_list(adev, |
| 1262 | get_voice_usecase_id_from_list(adev)); |
| 1263 | if ((vc_usecase != NULL) && |
| 1264 | ((vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) || |
| 1265 | (usecase->devices == AUDIO_DEVICE_IN_VOICE_CALL))) { |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1266 | in_snd_device = vc_usecase->in_snd_device; |
| 1267 | out_snd_device = vc_usecase->out_snd_device; |
| 1268 | } |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 1269 | } else if (audio_extn_hfp_is_active(adev)) { |
| 1270 | hfp_ucid = audio_extn_hfp_get_usecase(); |
| 1271 | hfp_usecase = get_usecase_from_list(adev, hfp_ucid); |
| 1272 | if (hfp_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) { |
| 1273 | in_snd_device = hfp_usecase->in_snd_device; |
| 1274 | out_snd_device = hfp_usecase->out_snd_device; |
| 1275 | } |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1276 | } |
| 1277 | if (usecase->type == PCM_PLAYBACK) { |
| 1278 | usecase->devices = usecase->stream.out->devices; |
| 1279 | in_snd_device = SND_DEVICE_NONE; |
Ravi Kumar Alamanda | 59d296d | 2013-05-02 11:25:27 -0700 | [diff] [blame] | 1280 | if (out_snd_device == SND_DEVICE_NONE) { |
carter_hsu | 1e1ebbf | 2017-08-15 11:39:55 +0800 | [diff] [blame] | 1281 | struct stream_out *voip_out = adev->primary_output; |
carter_hsu | 1e1ebbf | 2017-08-15 11:39:55 +0800 | [diff] [blame] | 1282 | |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 1283 | out_snd_device = platform_get_output_snd_device(adev->platform, |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1284 | usecase->stream.out->devices); |
carter_hsu | 1e1ebbf | 2017-08-15 11:39:55 +0800 | [diff] [blame] | 1285 | |
| 1286 | if (voip_usecase) |
| 1287 | voip_out = voip_usecase->stream.out; |
| 1288 | |
| 1289 | if (usecase->stream.out == voip_out && |
Ravi Kumar Alamanda | 59d296d | 2013-05-02 11:25:27 -0700 | [diff] [blame] | 1290 | adev->active_input && |
Eric Laurent | 50a38ed | 2015-10-14 18:48:06 -0700 | [diff] [blame] | 1291 | (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION || |
carter_hsu | 1e1ebbf | 2017-08-15 11:39:55 +0800 | [diff] [blame] | 1292 | adev->mode == AUDIO_MODE_IN_COMMUNICATION)) { |
Ravi Kumar Alamanda | 59d296d | 2013-05-02 11:25:27 -0700 | [diff] [blame] | 1293 | select_devices(adev, adev->active_input->usecase); |
| 1294 | } |
| 1295 | } |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1296 | } else if (usecase->type == PCM_CAPTURE) { |
| 1297 | usecase->devices = usecase->stream.in->device; |
| 1298 | out_snd_device = SND_DEVICE_NONE; |
Ravi Kumar Alamanda | 59d296d | 2013-05-02 11:25:27 -0700 | [diff] [blame] | 1299 | if (in_snd_device == SND_DEVICE_NONE) { |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 1300 | audio_devices_t out_device = AUDIO_DEVICE_NONE; |
Eric Laurent | 50a38ed | 2015-10-14 18:48:06 -0700 | [diff] [blame] | 1301 | if (adev->active_input && |
| 1302 | (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION || |
| 1303 | adev->mode == AUDIO_MODE_IN_COMMUNICATION)) { |
carter_hsu | 1e1ebbf | 2017-08-15 11:39:55 +0800 | [diff] [blame] | 1304 | |
| 1305 | struct audio_usecase *voip_usecase = get_usecase_from_list(adev, |
| 1306 | USECASE_AUDIO_PLAYBACK_VOIP); |
| 1307 | |
Ravi Kumar Alamanda | f282901 | 2014-11-12 16:16:10 -0800 | [diff] [blame] | 1308 | platform_set_echo_reference(adev, false, AUDIO_DEVICE_NONE); |
Ravi Kumar Alamanda | 0464359 | 2015-09-24 19:17:26 -0700 | [diff] [blame] | 1309 | if (usecase->id == USECASE_AUDIO_RECORD_AFE_PROXY) { |
| 1310 | out_device = AUDIO_DEVICE_OUT_TELEPHONY_TX; |
carter_hsu | 1e1ebbf | 2017-08-15 11:39:55 +0800 | [diff] [blame] | 1311 | } else if (voip_usecase) { |
| 1312 | out_device = voip_usecase->stream.out->devices; |
Ravi Kumar Alamanda | 0464359 | 2015-09-24 19:17:26 -0700 | [diff] [blame] | 1313 | } else if (adev->primary_output) { |
| 1314 | out_device = adev->primary_output->devices; |
| 1315 | } |
Ravi Kumar Alamanda | 59d296d | 2013-05-02 11:25:27 -0700 | [diff] [blame] | 1316 | } |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 1317 | in_snd_device = platform_get_input_snd_device(adev->platform, out_device); |
Ravi Kumar Alamanda | 59d296d | 2013-05-02 11:25:27 -0700 | [diff] [blame] | 1318 | } |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1319 | } |
| 1320 | } |
| 1321 | |
| 1322 | if (out_snd_device == usecase->out_snd_device && |
| 1323 | in_snd_device == usecase->in_snd_device) { |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 1324 | if (!force_device_switch(usecase)) |
| 1325 | return 0; |
| 1326 | } |
| 1327 | |
| 1328 | if (is_a2dp_device(out_snd_device) && !audio_extn_a2dp_is_ready()) { |
| 1329 | ALOGD("SCO/A2DP is selected but they are not connected/ready hence dont route"); |
| 1330 | return 0; |
| 1331 | } |
| 1332 | |
| 1333 | if ((out_snd_device == SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP) && |
| 1334 | (!audio_extn_a2dp_is_ready())) { |
| 1335 | ALOGW("%s: A2DP profile is not ready, routing to speaker only", __func__); |
| 1336 | out_snd_device = SND_DEVICE_OUT_SPEAKER; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1337 | } |
| 1338 | |
Eric Laurent | 2bafff1 | 2016-03-17 12:17:23 -0700 | [diff] [blame] | 1339 | if (out_snd_device != SND_DEVICE_NONE && |
| 1340 | out_snd_device != adev->last_logged_snd_device[uc_id][0]) { |
| 1341 | ALOGD("%s: changing use case %s output device from(%d: %s, acdb %d) to (%d: %s, acdb %d)", |
| 1342 | __func__, |
| 1343 | use_case_table[uc_id], |
| 1344 | adev->last_logged_snd_device[uc_id][0], |
| 1345 | platform_get_snd_device_name(adev->last_logged_snd_device[uc_id][0]), |
| 1346 | adev->last_logged_snd_device[uc_id][0] != SND_DEVICE_NONE ? |
| 1347 | platform_get_snd_device_acdb_id(adev->last_logged_snd_device[uc_id][0]) : |
| 1348 | -1, |
| 1349 | out_snd_device, |
| 1350 | platform_get_snd_device_name(out_snd_device), |
| 1351 | platform_get_snd_device_acdb_id(out_snd_device)); |
| 1352 | adev->last_logged_snd_device[uc_id][0] = out_snd_device; |
| 1353 | } |
| 1354 | if (in_snd_device != SND_DEVICE_NONE && |
| 1355 | in_snd_device != adev->last_logged_snd_device[uc_id][1]) { |
| 1356 | ALOGD("%s: changing use case %s input device from(%d: %s, acdb %d) to (%d: %s, acdb %d)", |
| 1357 | __func__, |
| 1358 | use_case_table[uc_id], |
| 1359 | adev->last_logged_snd_device[uc_id][1], |
| 1360 | platform_get_snd_device_name(adev->last_logged_snd_device[uc_id][1]), |
| 1361 | adev->last_logged_snd_device[uc_id][1] != SND_DEVICE_NONE ? |
| 1362 | platform_get_snd_device_acdb_id(adev->last_logged_snd_device[uc_id][1]) : |
| 1363 | -1, |
| 1364 | in_snd_device, |
| 1365 | platform_get_snd_device_name(in_snd_device), |
| 1366 | platform_get_snd_device_acdb_id(in_snd_device)); |
| 1367 | adev->last_logged_snd_device[uc_id][1] = in_snd_device; |
| 1368 | } |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 1369 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1370 | /* |
| 1371 | * Limitation: While in call, to do a device switch we need to disable |
| 1372 | * and enable both RX and TX devices though one of them is same as current |
| 1373 | * device. |
| 1374 | */ |
Ravi Kumar Alamanda | 36886fc | 2014-09-29 13:41:51 -0700 | [diff] [blame] | 1375 | if ((usecase->type == VOICE_CALL) && |
| 1376 | (usecase->in_snd_device != SND_DEVICE_NONE) && |
| 1377 | (usecase->out_snd_device != SND_DEVICE_NONE)) { |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 1378 | status = platform_switch_voice_call_device_pre(adev->platform); |
vivek mehta | 765eb64 | 2015-08-07 19:46:06 -0700 | [diff] [blame] | 1379 | /* Disable sidetone only if voice call already exists */ |
| 1380 | if (voice_is_call_state_active(adev)) |
| 1381 | voice_set_sidetone(adev, usecase->out_snd_device, false); |
Ravi Kumar Alamanda | 610e8cc | 2013-02-12 01:42:38 -0800 | [diff] [blame] | 1382 | } |
| 1383 | |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1384 | /* Disable current sound devices */ |
| 1385 | if (usecase->out_snd_device != SND_DEVICE_NONE) { |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 1386 | disable_audio_route(adev, usecase); |
| 1387 | disable_snd_device(adev, usecase->out_snd_device); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1388 | } |
| 1389 | |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1390 | if (usecase->in_snd_device != SND_DEVICE_NONE) { |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 1391 | disable_audio_route(adev, usecase); |
| 1392 | disable_snd_device(adev, usecase->in_snd_device); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1393 | } |
| 1394 | |
Ravi Kumar Alamanda | 83281a9 | 2014-05-19 18:14:57 -0700 | [diff] [blame] | 1395 | /* Applicable only on the targets that has external modem. |
| 1396 | * New device information should be sent to modem before enabling |
| 1397 | * the devices to reduce in-call device switch time. |
| 1398 | */ |
Ravi Kumar Alamanda | 36886fc | 2014-09-29 13:41:51 -0700 | [diff] [blame] | 1399 | if ((usecase->type == VOICE_CALL) && |
| 1400 | (usecase->in_snd_device != SND_DEVICE_NONE) && |
| 1401 | (usecase->out_snd_device != SND_DEVICE_NONE)) { |
Ravi Kumar Alamanda | 83281a9 | 2014-05-19 18:14:57 -0700 | [diff] [blame] | 1402 | status = platform_switch_voice_call_enable_device_config(adev->platform, |
| 1403 | out_snd_device, |
| 1404 | in_snd_device); |
Ravi Kumar Alamanda | 36886fc | 2014-09-29 13:41:51 -0700 | [diff] [blame] | 1405 | } |
Ravi Kumar Alamanda | 83281a9 | 2014-05-19 18:14:57 -0700 | [diff] [blame] | 1406 | |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1407 | /* Enable new sound devices */ |
| 1408 | if (out_snd_device != SND_DEVICE_NONE) { |
David Lin | ee3fe40 | 2017-03-13 10:00:42 -0700 | [diff] [blame] | 1409 | if ((usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) || |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 1410 | (usecase->devices & (AUDIO_DEVICE_OUT_USB_DEVICE|AUDIO_DEVICE_OUT_USB_HEADSET)) || |
| 1411 | (usecase->devices & AUDIO_DEVICE_OUT_ALL_A2DP)) |
Ravi Kumar Alamanda | b7ea4f5 | 2015-06-08 16:44:05 -0700 | [diff] [blame] | 1412 | check_and_route_playback_usecases(adev, usecase, out_snd_device); |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 1413 | enable_snd_device(adev, out_snd_device); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1414 | } |
| 1415 | |
Ravi Kumar Alamanda | c4ba743 | 2013-06-05 14:11:39 -0700 | [diff] [blame] | 1416 | if (in_snd_device != SND_DEVICE_NONE) { |
| 1417 | check_and_route_capture_usecases(adev, usecase, in_snd_device); |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 1418 | enable_snd_device(adev, in_snd_device); |
Ravi Kumar Alamanda | c4ba743 | 2013-06-05 14:11:39 -0700 | [diff] [blame] | 1419 | } |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1420 | |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 1421 | if (usecase->type == VOICE_CALL) |
| 1422 | status = platform_switch_voice_call_device_post(adev->platform, |
| 1423 | out_snd_device, |
| 1424 | in_snd_device); |
Ravi Kumar Alamanda | 610e8cc | 2013-02-12 01:42:38 -0800 | [diff] [blame] | 1425 | |
sangwoo | 170731f | 2013-06-08 15:36:36 +0900 | [diff] [blame] | 1426 | usecase->in_snd_device = in_snd_device; |
| 1427 | usecase->out_snd_device = out_snd_device; |
| 1428 | |
Alain Vongsouvanh | 13f26e8 | 2016-11-18 14:39:11 -0800 | [diff] [blame] | 1429 | audio_extn_tfa_98xx_set_mode(); |
| 1430 | |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 1431 | enable_audio_route(adev, usecase); |
sangwoo | 170731f | 2013-06-08 15:36:36 +0900 | [diff] [blame] | 1432 | |
Ravi Kumar Alamanda | 83281a9 | 2014-05-19 18:14:57 -0700 | [diff] [blame] | 1433 | /* Applicable only on the targets that has external modem. |
| 1434 | * Enable device command should be sent to modem only after |
| 1435 | * enabling voice call mixer controls |
| 1436 | */ |
vivek mehta | 765eb64 | 2015-08-07 19:46:06 -0700 | [diff] [blame] | 1437 | if (usecase->type == VOICE_CALL) { |
Ravi Kumar Alamanda | 83281a9 | 2014-05-19 18:14:57 -0700 | [diff] [blame] | 1438 | status = platform_switch_voice_call_usecase_route_post(adev->platform, |
| 1439 | out_snd_device, |
| 1440 | in_snd_device); |
vivek mehta | 765eb64 | 2015-08-07 19:46:06 -0700 | [diff] [blame] | 1441 | /* Enable sidetone only if voice call already exists */ |
| 1442 | if (voice_is_call_state_active(adev)) |
| 1443 | voice_set_sidetone(adev, out_snd_device, true); |
| 1444 | } |
Ravi Kumar Alamanda | 83281a9 | 2014-05-19 18:14:57 -0700 | [diff] [blame] | 1445 | |
Eric Laurent | f4520b0 | 2017-09-20 18:31:58 -0700 | [diff] [blame] | 1446 | if (usecase == voip_usecase) { |
| 1447 | struct stream_out *voip_out = voip_usecase->stream.out; |
| 1448 | audio_extn_utils_send_app_type_gain(adev, |
| 1449 | voip_out->app_type_cfg.app_type, |
| 1450 | &voip_out->app_type_cfg.gain[0]); |
| 1451 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1452 | return status; |
| 1453 | } |
| 1454 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1455 | static int stop_input_stream(struct stream_in *in) |
| 1456 | { |
| 1457 | int i, ret = 0; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1458 | struct audio_usecase *uc_info; |
| 1459 | struct audio_device *adev = in->dev; |
| 1460 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 1461 | ALOGV("%s: enter: usecase(%d: %s)", __func__, |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1462 | in->usecase, use_case_table[in->usecase]); |
Haynes Mathew George | 8ea0472 | 2017-07-25 17:41:49 -0700 | [diff] [blame] | 1463 | |
| 1464 | if (adev->active_input) { |
| 1465 | if (adev->active_input->usecase == in->usecase) { |
| 1466 | adev->active_input = NULL; |
| 1467 | } else { |
| 1468 | ALOGW("%s adev->active_input->usecase %s, v/s in->usecase %s", |
| 1469 | __func__, |
| 1470 | use_case_table[adev->active_input->usecase], |
| 1471 | use_case_table[in->usecase]); |
| 1472 | } |
| 1473 | } |
| 1474 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1475 | uc_info = get_usecase_from_list(adev, in->usecase); |
| 1476 | if (uc_info == NULL) { |
| 1477 | ALOGE("%s: Could not find the usecase (%d) in the list", |
| 1478 | __func__, in->usecase); |
| 1479 | return -EINVAL; |
| 1480 | } |
| 1481 | |
vivek mehta | 781065c | 2017-04-04 12:55:01 -0700 | [diff] [blame] | 1482 | /* Close in-call recording streams */ |
| 1483 | voice_check_and_stop_incall_rec_usecase(adev, in); |
| 1484 | |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 1485 | /* 1. Disable stream specific mixer controls */ |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 1486 | disable_audio_route(adev, uc_info); |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1487 | |
| 1488 | /* 2. Disable the tx device */ |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 1489 | disable_snd_device(adev, uc_info->in_snd_device); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1490 | |
Ravi Kumar Alamanda | 3b1816c | 2013-02-27 23:01:21 -0800 | [diff] [blame] | 1491 | list_remove(&uc_info->list); |
| 1492 | free(uc_info); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1493 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 1494 | ALOGV("%s: exit: status(%d)", __func__, ret); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1495 | return ret; |
| 1496 | } |
| 1497 | |
| 1498 | int start_input_stream(struct stream_in *in) |
| 1499 | { |
| 1500 | /* 1. Enable output device and stream routing controls */ |
Eric Laurent | c840063 | 2013-02-14 19:04:54 -0800 | [diff] [blame] | 1501 | int ret = 0; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1502 | struct audio_usecase *uc_info; |
| 1503 | struct audio_device *adev = in->dev; |
| 1504 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 1505 | ALOGV("%s: enter: usecase(%d)", __func__, in->usecase); |
Haynes Mathew George | c735fb0 | 2016-06-30 18:00:28 -0700 | [diff] [blame] | 1506 | |
Alain Vongsouvanh | 13f26e8 | 2016-11-18 14:39:11 -0800 | [diff] [blame] | 1507 | if (audio_extn_tfa_98xx_is_supported() && !audio_ssr_status(adev)) |
| 1508 | return -EIO; |
| 1509 | |
Haynes Mathew George | c735fb0 | 2016-06-30 18:00:28 -0700 | [diff] [blame] | 1510 | if (in->card_status == CARD_STATUS_OFFLINE || |
| 1511 | adev->card_status == CARD_STATUS_OFFLINE) { |
| 1512 | ALOGW("in->card_status or adev->card_status offline, try again"); |
| 1513 | ret = -EAGAIN; |
| 1514 | goto error_config; |
| 1515 | } |
| 1516 | |
vivek mehta | 781065c | 2017-04-04 12:55:01 -0700 | [diff] [blame] | 1517 | /* Check if source matches incall recording usecase criteria */ |
| 1518 | ret = voice_check_and_set_incall_rec_usecase(adev, in); |
| 1519 | if (ret) |
| 1520 | goto error_config; |
| 1521 | else |
| 1522 | ALOGV("%s: usecase(%d)", __func__, in->usecase); |
| 1523 | |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 1524 | 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] | 1525 | if (in->pcm_device_id < 0) { |
| 1526 | ALOGE("%s: Could not find PCM device id for the usecase(%d)", |
| 1527 | __func__, in->usecase); |
Eric Laurent | c840063 | 2013-02-14 19:04:54 -0800 | [diff] [blame] | 1528 | ret = -EINVAL; |
| 1529 | goto error_config; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1530 | } |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1531 | |
| 1532 | adev->active_input = in; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1533 | uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase)); |
| 1534 | uc_info->id = in->usecase; |
| 1535 | uc_info->type = PCM_CAPTURE; |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 1536 | uc_info->stream.in = in; |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1537 | uc_info->devices = in->device; |
| 1538 | uc_info->in_snd_device = SND_DEVICE_NONE; |
| 1539 | uc_info->out_snd_device = SND_DEVICE_NONE; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1540 | |
Ravi Kumar Alamanda | 3b1816c | 2013-02-27 23:01:21 -0800 | [diff] [blame] | 1541 | list_add_tail(&adev->usecase_list, &uc_info->list); |
Ravi Kumar Alamanda | 533bb72 | 2015-09-23 13:47:03 -0700 | [diff] [blame] | 1542 | |
Wei Wang | f4837d5 | 2017-11-21 14:51:20 -0800 | [diff] [blame] | 1543 | audio_streaming_hint_start(); |
Ravi Kumar Alamanda | 533bb72 | 2015-09-23 13:47:03 -0700 | [diff] [blame] | 1544 | audio_extn_perf_lock_acquire(); |
| 1545 | |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1546 | select_devices(adev, in->usecase); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1547 | |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 1548 | if (in->usecase == USECASE_AUDIO_RECORD_MMAP) { |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 1549 | if (in->pcm == NULL || !pcm_is_ready(in->pcm)) { |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 1550 | ALOGE("%s: pcm stream not ready", __func__); |
| 1551 | goto error_open; |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 1552 | } |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 1553 | ret = pcm_start(in->pcm); |
Haynes Mathew George | fbe8731 | 2016-08-01 19:29:27 -0700 | [diff] [blame] | 1554 | if (ret < 0) { |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 1555 | ALOGE("%s: MMAP pcm_start failed ret %d", __func__, ret); |
| 1556 | goto error_open; |
| 1557 | } |
| 1558 | } else { |
| 1559 | unsigned int flags = PCM_IN | PCM_MONOTONIC; |
| 1560 | unsigned int pcm_open_retry_count = 0; |
| 1561 | |
| 1562 | if (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY) { |
| 1563 | flags |= PCM_MMAP | PCM_NOIRQ; |
| 1564 | pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT; |
| 1565 | } else if (in->realtime) { |
| 1566 | flags |= PCM_MMAP | PCM_NOIRQ; |
| 1567 | } |
| 1568 | |
| 1569 | ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d", |
| 1570 | __func__, adev->snd_card, in->pcm_device_id, in->config.channels); |
| 1571 | |
| 1572 | while (1) { |
| 1573 | in->pcm = pcm_open(adev->snd_card, in->pcm_device_id, |
| 1574 | flags, &in->config); |
| 1575 | if (in->pcm == NULL || !pcm_is_ready(in->pcm)) { |
| 1576 | ALOGE("%s: %s", __func__, pcm_get_error(in->pcm)); |
| 1577 | if (in->pcm != NULL) { |
| 1578 | pcm_close(in->pcm); |
| 1579 | in->pcm = NULL; |
| 1580 | } |
| 1581 | if (pcm_open_retry_count-- == 0) { |
| 1582 | ret = -EIO; |
| 1583 | goto error_open; |
| 1584 | } |
| 1585 | usleep(PROXY_OPEN_WAIT_TIME * 1000); |
| 1586 | continue; |
| 1587 | } |
| 1588 | break; |
| 1589 | } |
| 1590 | |
| 1591 | ALOGV("%s: pcm_prepare", __func__); |
| 1592 | ret = pcm_prepare(in->pcm); |
| 1593 | if (ret < 0) { |
| 1594 | ALOGE("%s: pcm_prepare returned %d", __func__, ret); |
Haynes Mathew George | fbe8731 | 2016-08-01 19:29:27 -0700 | [diff] [blame] | 1595 | pcm_close(in->pcm); |
| 1596 | in->pcm = NULL; |
| 1597 | goto error_open; |
| 1598 | } |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 1599 | if (in->realtime) { |
| 1600 | ret = pcm_start(in->pcm); |
| 1601 | if (ret < 0) { |
| 1602 | ALOGE("%s: RT pcm_start failed ret %d", __func__, ret); |
| 1603 | pcm_close(in->pcm); |
| 1604 | in->pcm = NULL; |
| 1605 | goto error_open; |
| 1606 | } |
| 1607 | } |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 1608 | } |
Haynes Mathew George | fbe8731 | 2016-08-01 19:29:27 -0700 | [diff] [blame] | 1609 | register_in_stream(in); |
Wei Wang | f4837d5 | 2017-11-21 14:51:20 -0800 | [diff] [blame] | 1610 | audio_streaming_hint_end(); |
Ravi Kumar Alamanda | 533bb72 | 2015-09-23 13:47:03 -0700 | [diff] [blame] | 1611 | audio_extn_perf_lock_release(); |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 1612 | ALOGV("%s: exit", __func__); |
Haynes Mathew George | 88e6fb2 | 2015-08-19 11:51:34 -0700 | [diff] [blame] | 1613 | |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 1614 | return 0; |
Eric Laurent | c840063 | 2013-02-14 19:04:54 -0800 | [diff] [blame] | 1615 | |
| 1616 | error_open: |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1617 | stop_input_stream(in); |
Wei Wang | f4837d5 | 2017-11-21 14:51:20 -0800 | [diff] [blame] | 1618 | audio_streaming_hint_end(); |
Ravi Kumar Alamanda | 533bb72 | 2015-09-23 13:47:03 -0700 | [diff] [blame] | 1619 | audio_extn_perf_lock_release(); |
Eric Laurent | c840063 | 2013-02-14 19:04:54 -0800 | [diff] [blame] | 1620 | |
| 1621 | error_config: |
| 1622 | adev->active_input = NULL; |
Eric Laurent | 2bafff1 | 2016-03-17 12:17:23 -0700 | [diff] [blame] | 1623 | ALOGW("%s: exit: status(%d)", __func__, ret); |
Eric Laurent | c840063 | 2013-02-14 19:04:54 -0800 | [diff] [blame] | 1624 | return ret; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1625 | } |
| 1626 | |
Eric Laurent | a147807 | 2015-09-21 17:21:52 -0700 | [diff] [blame] | 1627 | void lock_input_stream(struct stream_in *in) |
| 1628 | { |
| 1629 | pthread_mutex_lock(&in->pre_lock); |
| 1630 | pthread_mutex_lock(&in->lock); |
| 1631 | pthread_mutex_unlock(&in->pre_lock); |
| 1632 | } |
| 1633 | |
| 1634 | void lock_output_stream(struct stream_out *out) |
| 1635 | { |
| 1636 | pthread_mutex_lock(&out->pre_lock); |
| 1637 | pthread_mutex_lock(&out->lock); |
| 1638 | pthread_mutex_unlock(&out->pre_lock); |
| 1639 | } |
| 1640 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 1641 | /* must be called with out->lock locked */ |
| 1642 | static int send_offload_cmd_l(struct stream_out* out, int command) |
| 1643 | { |
| 1644 | struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd)); |
| 1645 | |
| 1646 | ALOGVV("%s %d", __func__, command); |
| 1647 | |
| 1648 | cmd->cmd = command; |
| 1649 | list_add_tail(&out->offload_cmd_list, &cmd->node); |
| 1650 | pthread_cond_signal(&out->offload_cond); |
| 1651 | return 0; |
| 1652 | } |
| 1653 | |
| 1654 | /* must be called iwth out->lock locked */ |
| 1655 | static void stop_compressed_output_l(struct stream_out *out) |
| 1656 | { |
| 1657 | out->offload_state = OFFLOAD_STATE_IDLE; |
| 1658 | out->playback_started = 0; |
Haynes Mathew George | 352f27b | 2013-07-26 00:00:15 -0700 | [diff] [blame] | 1659 | out->send_new_metadata = 1; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 1660 | if (out->compr != NULL) { |
| 1661 | compress_stop(out->compr); |
| 1662 | while (out->offload_thread_blocked) { |
| 1663 | pthread_cond_wait(&out->cond, &out->lock); |
| 1664 | } |
| 1665 | } |
| 1666 | } |
| 1667 | |
| 1668 | static void *offload_thread_loop(void *context) |
| 1669 | { |
| 1670 | struct stream_out *out = (struct stream_out *) context; |
| 1671 | struct listnode *item; |
| 1672 | |
| 1673 | out->offload_state = OFFLOAD_STATE_IDLE; |
| 1674 | out->playback_started = 0; |
| 1675 | |
| 1676 | setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO); |
| 1677 | set_sched_policy(0, SP_FOREGROUND); |
| 1678 | prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0); |
| 1679 | |
| 1680 | ALOGV("%s", __func__); |
Eric Laurent | a147807 | 2015-09-21 17:21:52 -0700 | [diff] [blame] | 1681 | lock_output_stream(out); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 1682 | for (;;) { |
| 1683 | struct offload_cmd *cmd = NULL; |
| 1684 | stream_callback_event_t event; |
| 1685 | bool send_callback = false; |
| 1686 | |
| 1687 | ALOGVV("%s offload_cmd_list %d out->offload_state %d", |
| 1688 | __func__, list_empty(&out->offload_cmd_list), |
| 1689 | out->offload_state); |
| 1690 | if (list_empty(&out->offload_cmd_list)) { |
| 1691 | ALOGV("%s SLEEPING", __func__); |
| 1692 | pthread_cond_wait(&out->offload_cond, &out->lock); |
| 1693 | ALOGV("%s RUNNING", __func__); |
| 1694 | continue; |
| 1695 | } |
| 1696 | |
| 1697 | item = list_head(&out->offload_cmd_list); |
| 1698 | cmd = node_to_item(item, struct offload_cmd, node); |
| 1699 | list_remove(item); |
| 1700 | |
| 1701 | ALOGVV("%s STATE %d CMD %d out->compr %p", |
| 1702 | __func__, out->offload_state, cmd->cmd, out->compr); |
| 1703 | |
| 1704 | if (cmd->cmd == OFFLOAD_CMD_EXIT) { |
| 1705 | free(cmd); |
| 1706 | break; |
| 1707 | } |
| 1708 | |
| 1709 | if (out->compr == NULL) { |
| 1710 | ALOGE("%s: Compress handle is NULL", __func__); |
Andy Hung | 68f55fd | 2016-04-21 11:51:11 -0700 | [diff] [blame] | 1711 | free(cmd); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 1712 | pthread_cond_signal(&out->cond); |
| 1713 | continue; |
| 1714 | } |
| 1715 | out->offload_thread_blocked = true; |
| 1716 | pthread_mutex_unlock(&out->lock); |
| 1717 | send_callback = false; |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 1718 | switch (cmd->cmd) { |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 1719 | case OFFLOAD_CMD_WAIT_FOR_BUFFER: |
| 1720 | compress_wait(out->compr, -1); |
| 1721 | send_callback = true; |
| 1722 | event = STREAM_CBK_EVENT_WRITE_READY; |
| 1723 | break; |
| 1724 | case OFFLOAD_CMD_PARTIAL_DRAIN: |
Haynes Mathew George | 352f27b | 2013-07-26 00:00:15 -0700 | [diff] [blame] | 1725 | compress_next_track(out->compr); |
| 1726 | compress_partial_drain(out->compr); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 1727 | send_callback = true; |
| 1728 | event = STREAM_CBK_EVENT_DRAIN_READY; |
Ravi Kumar Alamanda | cc4f6bf | 2014-12-02 19:21:51 -0800 | [diff] [blame] | 1729 | /* Resend the metadata for next iteration */ |
| 1730 | out->send_new_metadata = 1; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 1731 | break; |
| 1732 | case OFFLOAD_CMD_DRAIN: |
| 1733 | compress_drain(out->compr); |
| 1734 | send_callback = true; |
| 1735 | event = STREAM_CBK_EVENT_DRAIN_READY; |
| 1736 | break; |
Haynes Mathew George | 3ddd3bd | 2016-07-07 20:01:53 -0700 | [diff] [blame] | 1737 | case OFFLOAD_CMD_ERROR: |
| 1738 | send_callback = true; |
| 1739 | event = STREAM_CBK_EVENT_ERROR; |
| 1740 | break; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 1741 | default: |
| 1742 | ALOGE("%s unknown command received: %d", __func__, cmd->cmd); |
| 1743 | break; |
| 1744 | } |
Eric Laurent | a147807 | 2015-09-21 17:21:52 -0700 | [diff] [blame] | 1745 | lock_output_stream(out); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 1746 | out->offload_thread_blocked = false; |
| 1747 | pthread_cond_signal(&out->cond); |
Eric Laurent | 6e89524 | 2013-09-05 16:10:57 -0700 | [diff] [blame] | 1748 | if (send_callback) { |
Ravi Kumar Alamanda | cc4f6bf | 2014-12-02 19:21:51 -0800 | [diff] [blame] | 1749 | ALOGVV("%s: sending offload_callback event %d", __func__, event); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 1750 | out->offload_callback(event, NULL, out->offload_cookie); |
Eric Laurent | 6e89524 | 2013-09-05 16:10:57 -0700 | [diff] [blame] | 1751 | } |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 1752 | free(cmd); |
| 1753 | } |
| 1754 | |
| 1755 | pthread_cond_signal(&out->cond); |
| 1756 | while (!list_empty(&out->offload_cmd_list)) { |
| 1757 | item = list_head(&out->offload_cmd_list); |
| 1758 | list_remove(item); |
| 1759 | free(node_to_item(item, struct offload_cmd, node)); |
| 1760 | } |
| 1761 | pthread_mutex_unlock(&out->lock); |
| 1762 | |
| 1763 | return NULL; |
| 1764 | } |
| 1765 | |
| 1766 | static int create_offload_callback_thread(struct stream_out *out) |
| 1767 | { |
| 1768 | pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL); |
| 1769 | list_init(&out->offload_cmd_list); |
| 1770 | pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL, |
| 1771 | offload_thread_loop, out); |
| 1772 | return 0; |
| 1773 | } |
| 1774 | |
| 1775 | static int destroy_offload_callback_thread(struct stream_out *out) |
| 1776 | { |
Eric Laurent | a147807 | 2015-09-21 17:21:52 -0700 | [diff] [blame] | 1777 | lock_output_stream(out); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 1778 | stop_compressed_output_l(out); |
| 1779 | send_offload_cmd_l(out, OFFLOAD_CMD_EXIT); |
| 1780 | |
| 1781 | pthread_mutex_unlock(&out->lock); |
| 1782 | pthread_join(out->offload_thread, (void **) NULL); |
| 1783 | pthread_cond_destroy(&out->offload_cond); |
| 1784 | |
| 1785 | return 0; |
| 1786 | } |
| 1787 | |
Eric Laurent | 07eeafd | 2013-10-06 12:52:49 -0700 | [diff] [blame] | 1788 | static bool allow_hdmi_channel_config(struct audio_device *adev) |
| 1789 | { |
| 1790 | struct listnode *node; |
| 1791 | struct audio_usecase *usecase; |
| 1792 | bool ret = true; |
| 1793 | |
| 1794 | list_for_each(node, &adev->usecase_list) { |
| 1795 | usecase = node_to_item(node, struct audio_usecase, list); |
| 1796 | if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) { |
| 1797 | /* |
| 1798 | * If voice call is already existing, do not proceed further to avoid |
| 1799 | * disabling/enabling both RX and TX devices, CSD calls, etc. |
| 1800 | * Once the voice call done, the HDMI channels can be configured to |
| 1801 | * max channels of remaining use cases. |
| 1802 | */ |
| 1803 | if (usecase->id == USECASE_VOICE_CALL) { |
Joe Onorato | 188b622 | 2016-03-01 11:02:27 -0800 | [diff] [blame] | 1804 | ALOGV("%s: voice call is active, no change in HDMI channels", |
Eric Laurent | 07eeafd | 2013-10-06 12:52:49 -0700 | [diff] [blame] | 1805 | __func__); |
| 1806 | ret = false; |
| 1807 | break; |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 1808 | } else if (usecase->id == USECASE_AUDIO_PLAYBACK_HIFI) { |
| 1809 | ALOGV("%s: hifi playback is active, " |
Eric Laurent | 07eeafd | 2013-10-06 12:52:49 -0700 | [diff] [blame] | 1810 | "no change in HDMI channels", __func__); |
| 1811 | ret = false; |
| 1812 | break; |
| 1813 | } |
| 1814 | } |
| 1815 | } |
| 1816 | return ret; |
| 1817 | } |
| 1818 | |
| 1819 | static int check_and_set_hdmi_channels(struct audio_device *adev, |
| 1820 | unsigned int channels) |
| 1821 | { |
| 1822 | struct listnode *node; |
| 1823 | struct audio_usecase *usecase; |
| 1824 | |
| 1825 | /* Check if change in HDMI channel config is allowed */ |
| 1826 | if (!allow_hdmi_channel_config(adev)) |
| 1827 | return 0; |
| 1828 | |
| 1829 | if (channels == adev->cur_hdmi_channels) { |
Joe Onorato | 188b622 | 2016-03-01 11:02:27 -0800 | [diff] [blame] | 1830 | ALOGV("%s: Requested channels are same as current", __func__); |
Eric Laurent | 07eeafd | 2013-10-06 12:52:49 -0700 | [diff] [blame] | 1831 | return 0; |
| 1832 | } |
| 1833 | |
| 1834 | platform_set_hdmi_channels(adev->platform, channels); |
| 1835 | adev->cur_hdmi_channels = channels; |
| 1836 | |
| 1837 | /* |
| 1838 | * Deroute all the playback streams routed to HDMI so that |
| 1839 | * the back end is deactivated. Note that backend will not |
| 1840 | * be deactivated if any one stream is connected to it. |
| 1841 | */ |
| 1842 | list_for_each(node, &adev->usecase_list) { |
| 1843 | usecase = node_to_item(node, struct audio_usecase, list); |
| 1844 | if (usecase->type == PCM_PLAYBACK && |
| 1845 | usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) { |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 1846 | disable_audio_route(adev, usecase); |
Eric Laurent | 07eeafd | 2013-10-06 12:52:49 -0700 | [diff] [blame] | 1847 | } |
| 1848 | } |
| 1849 | |
| 1850 | /* |
| 1851 | * Enable all the streams disabled above. Now the HDMI backend |
| 1852 | * will be activated with new channel configuration |
| 1853 | */ |
| 1854 | list_for_each(node, &adev->usecase_list) { |
| 1855 | usecase = node_to_item(node, struct audio_usecase, list); |
| 1856 | if (usecase->type == PCM_PLAYBACK && |
| 1857 | usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) { |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 1858 | enable_audio_route(adev, usecase); |
Eric Laurent | 07eeafd | 2013-10-06 12:52:49 -0700 | [diff] [blame] | 1859 | } |
| 1860 | } |
| 1861 | |
| 1862 | return 0; |
| 1863 | } |
| 1864 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1865 | static int stop_output_stream(struct stream_out *out) |
| 1866 | { |
| 1867 | int i, ret = 0; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1868 | struct audio_usecase *uc_info; |
| 1869 | struct audio_device *adev = out->dev; |
| 1870 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 1871 | ALOGV("%s: enter: usecase(%d: %s)", __func__, |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1872 | out->usecase, use_case_table[out->usecase]); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1873 | uc_info = get_usecase_from_list(adev, out->usecase); |
| 1874 | if (uc_info == NULL) { |
| 1875 | ALOGE("%s: Could not find the usecase (%d) in the list", |
| 1876 | __func__, out->usecase); |
| 1877 | return -EINVAL; |
| 1878 | } |
| 1879 | |
Haynes Mathew George | 41f8665 | 2014-06-17 14:22:15 -0700 | [diff] [blame] | 1880 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
| 1881 | if (adev->visualizer_stop_output != NULL) |
| 1882 | adev->visualizer_stop_output(out->handle, out->pcm_device_id); |
| 1883 | if (adev->offload_effects_stop_output != NULL) |
| 1884 | adev->offload_effects_stop_output(out->handle, out->pcm_device_id); |
Haynes Mathew George | d9ca796 | 2017-12-01 16:29:56 -0800 | [diff] [blame] | 1885 | } else if (out->usecase == USECASE_AUDIO_PLAYBACK_ULL || |
| 1886 | out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) { |
| 1887 | audio_low_latency_hint_end(); |
Haynes Mathew George | 41f8665 | 2014-06-17 14:22:15 -0700 | [diff] [blame] | 1888 | } |
Eric Laurent | c4aef75 | 2013-09-12 17:45:53 -0700 | [diff] [blame] | 1889 | |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 1890 | /* 1. Get and set stream specific mixer controls */ |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 1891 | disable_audio_route(adev, uc_info); |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1892 | |
| 1893 | /* 2. Disable the rx device */ |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 1894 | disable_snd_device(adev, uc_info->out_snd_device); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1895 | |
Ravi Kumar Alamanda | 3b1816c | 2013-02-27 23:01:21 -0800 | [diff] [blame] | 1896 | list_remove(&uc_info->list); |
| 1897 | free(uc_info); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1898 | |
Eric Laurent | 0499d4f | 2014-08-25 22:39:29 -0500 | [diff] [blame] | 1899 | audio_extn_extspk_update(adev->extspk); |
| 1900 | |
Eric Laurent | 07eeafd | 2013-10-06 12:52:49 -0700 | [diff] [blame] | 1901 | /* Must be called after removing the usecase from list */ |
| 1902 | if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) |
| 1903 | check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS); |
Haynes Mathew George | 0ecfe3d | 2017-08-04 15:36:09 -0700 | [diff] [blame] | 1904 | else if (out->devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) { |
| 1905 | struct listnode *node; |
| 1906 | struct audio_usecase *usecase; |
| 1907 | list_for_each(node, &adev->usecase_list) { |
| 1908 | usecase = node_to_item(node, struct audio_usecase, list); |
| 1909 | if (usecase->devices & AUDIO_DEVICE_OUT_SPEAKER) |
| 1910 | select_devices(adev, usecase->id); |
| 1911 | } |
| 1912 | } |
Eric Laurent | 07eeafd | 2013-10-06 12:52:49 -0700 | [diff] [blame] | 1913 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 1914 | ALOGV("%s: exit: status(%d)", __func__, ret); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1915 | return ret; |
| 1916 | } |
| 1917 | |
| 1918 | int start_output_stream(struct stream_out *out) |
| 1919 | { |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1920 | int ret = 0; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1921 | struct audio_usecase *uc_info; |
| 1922 | struct audio_device *adev = out->dev; |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 1923 | bool a2dp_combo = false; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1924 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 1925 | ALOGV("%s: enter: usecase(%d: %s) devices(%#x)", |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1926 | __func__, out->usecase, use_case_table[out->usecase], out->devices); |
Haynes Mathew George | c735fb0 | 2016-06-30 18:00:28 -0700 | [diff] [blame] | 1927 | |
| 1928 | if (out->card_status == CARD_STATUS_OFFLINE || |
| 1929 | adev->card_status == CARD_STATUS_OFFLINE) { |
| 1930 | ALOGW("out->card_status or adev->card_status offline, try again"); |
| 1931 | ret = -EAGAIN; |
| 1932 | goto error_config; |
| 1933 | } |
| 1934 | |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 1935 | if (out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) { |
| 1936 | if (!audio_extn_a2dp_is_ready()) { |
| 1937 | if (out->devices & AUDIO_DEVICE_OUT_SPEAKER) { |
| 1938 | a2dp_combo = true; |
| 1939 | } else { |
| 1940 | if (!(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) { |
| 1941 | ALOGE("%s: A2DP profile is not ready, return error", __func__); |
| 1942 | ret = -EAGAIN; |
| 1943 | goto error_config; |
| 1944 | } |
| 1945 | } |
| 1946 | } |
| 1947 | } |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 1948 | 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] | 1949 | if (out->pcm_device_id < 0) { |
| 1950 | ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)", |
| 1951 | __func__, out->pcm_device_id, out->usecase); |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 1952 | ret = -EINVAL; |
| 1953 | goto error_config; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1954 | } |
| 1955 | |
| 1956 | uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase)); |
| 1957 | uc_info->id = out->usecase; |
| 1958 | uc_info->type = PCM_PLAYBACK; |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 1959 | uc_info->stream.out = out; |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1960 | uc_info->devices = out->devices; |
| 1961 | uc_info->in_snd_device = SND_DEVICE_NONE; |
| 1962 | uc_info->out_snd_device = SND_DEVICE_NONE; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1963 | |
Eric Laurent | 07eeafd | 2013-10-06 12:52:49 -0700 | [diff] [blame] | 1964 | /* This must be called before adding this usecase to the list */ |
| 1965 | if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) |
| 1966 | check_and_set_hdmi_channels(adev, out->config.channels); |
| 1967 | |
Ravi Kumar Alamanda | 3b1816c | 2013-02-27 23:01:21 -0800 | [diff] [blame] | 1968 | list_add_tail(&adev->usecase_list, &uc_info->list); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1969 | |
Wei Wang | f4837d5 | 2017-11-21 14:51:20 -0800 | [diff] [blame] | 1970 | audio_streaming_hint_start(); |
Ravi Kumar Alamanda | 533bb72 | 2015-09-23 13:47:03 -0700 | [diff] [blame] | 1971 | audio_extn_perf_lock_acquire(); |
| 1972 | |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 1973 | if ((out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) && |
| 1974 | (!audio_extn_a2dp_is_ready())) { |
| 1975 | if (!a2dp_combo) { |
| 1976 | check_a2dp_restore_l(adev, out, false); |
| 1977 | } else { |
| 1978 | audio_devices_t dev = out->devices; |
| 1979 | out->devices = AUDIO_DEVICE_OUT_SPEAKER; |
| 1980 | select_devices(adev, out->usecase); |
| 1981 | out->devices = dev; |
| 1982 | } |
| 1983 | } else { |
| 1984 | select_devices(adev, out->usecase); |
| 1985 | } |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1986 | |
Eric Laurent | 0499d4f | 2014-08-25 22:39:29 -0500 | [diff] [blame] | 1987 | audio_extn_extspk_update(adev->extspk); |
| 1988 | |
Andy Hung | 31aca91 | 2014-03-20 17:14:59 -0700 | [diff] [blame] | 1989 | 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] | 1990 | __func__, adev->snd_card, out->pcm_device_id, out->config.format); |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 1991 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
| 1992 | out->pcm = NULL; |
| 1993 | out->compr = compress_open(adev->snd_card, out->pcm_device_id, |
| 1994 | COMPRESS_IN, &out->compr_config); |
| 1995 | if (out->compr && !is_compress_ready(out->compr)) { |
| 1996 | ALOGE("%s: %s", __func__, compress_get_error(out->compr)); |
| 1997 | compress_close(out->compr); |
| 1998 | out->compr = NULL; |
| 1999 | ret = -EIO; |
| 2000 | goto error_open; |
| 2001 | } |
| 2002 | if (out->offload_callback) |
| 2003 | compress_nonblock(out->compr, out->non_blocking); |
| 2004 | |
| 2005 | if (adev->visualizer_start_output != NULL) |
| 2006 | adev->visualizer_start_output(out->handle, out->pcm_device_id); |
| 2007 | if (adev->offload_effects_start_output != NULL) |
| 2008 | adev->offload_effects_start_output(out->handle, out->pcm_device_id); |
| 2009 | } else if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) { |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 2010 | if (out->pcm == NULL || !pcm_is_ready(out->pcm)) { |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 2011 | ALOGE("%s: pcm stream not ready", __func__); |
| 2012 | goto error_open; |
| 2013 | } |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 2014 | ret = pcm_start(out->pcm); |
| 2015 | if (ret < 0) { |
| 2016 | ALOGE("%s: MMAP pcm_start failed ret %d", __func__, ret); |
| 2017 | goto error_open; |
| 2018 | } |
| 2019 | } else { |
Mikhail Naganov | 635e743 | 2017-06-02 13:51:00 -0700 | [diff] [blame] | 2020 | unsigned int flags = PCM_OUT | PCM_MONOTONIC; |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 2021 | unsigned int pcm_open_retry_count = 0; |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 2022 | |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 2023 | if (out->usecase == USECASE_AUDIO_PLAYBACK_AFE_PROXY) { |
| 2024 | flags |= PCM_MMAP | PCM_NOIRQ; |
| 2025 | pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT; |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 2026 | } else if (out->realtime) { |
| 2027 | flags |= PCM_MMAP | PCM_NOIRQ; |
Mikhail Naganov | 635e743 | 2017-06-02 13:51:00 -0700 | [diff] [blame] | 2028 | } |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 2029 | |
| 2030 | while (1) { |
| 2031 | out->pcm = pcm_open(adev->snd_card, out->pcm_device_id, |
| 2032 | flags, &out->config); |
| 2033 | if (out->pcm == NULL || !pcm_is_ready(out->pcm)) { |
| 2034 | ALOGE("%s: %s", __func__, pcm_get_error(out->pcm)); |
| 2035 | if (out->pcm != NULL) { |
| 2036 | pcm_close(out->pcm); |
| 2037 | out->pcm = NULL; |
| 2038 | } |
| 2039 | if (pcm_open_retry_count-- == 0) { |
| 2040 | ret = -EIO; |
| 2041 | goto error_open; |
| 2042 | } |
| 2043 | usleep(PROXY_OPEN_WAIT_TIME * 1000); |
| 2044 | continue; |
| 2045 | } |
| 2046 | break; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2047 | } |
Ravi Kumar Alamanda | 50919a7 | 2015-10-02 09:37:33 -0700 | [diff] [blame] | 2048 | ALOGV("%s: pcm_prepare", __func__); |
| 2049 | if (pcm_is_ready(out->pcm)) { |
| 2050 | ret = pcm_prepare(out->pcm); |
| 2051 | if (ret < 0) { |
| 2052 | ALOGE("%s: pcm_prepare returned %d", __func__, ret); |
| 2053 | pcm_close(out->pcm); |
| 2054 | out->pcm = NULL; |
| 2055 | goto error_open; |
| 2056 | } |
| 2057 | } |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 2058 | if (out->realtime) { |
| 2059 | ret = pcm_start(out->pcm); |
| 2060 | if (ret < 0) { |
| 2061 | ALOGE("%s: RT pcm_start failed ret %d", __func__, ret); |
| 2062 | pcm_close(out->pcm); |
| 2063 | out->pcm = NULL; |
| 2064 | goto error_open; |
| 2065 | } |
Haynes Mathew George | fbe8731 | 2016-08-01 19:29:27 -0700 | [diff] [blame] | 2066 | } |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 2067 | } |
Haynes Mathew George | fbe8731 | 2016-08-01 19:29:27 -0700 | [diff] [blame] | 2068 | register_out_stream(out); |
Wei Wang | f4837d5 | 2017-11-21 14:51:20 -0800 | [diff] [blame] | 2069 | audio_streaming_hint_end(); |
Ravi Kumar Alamanda | 533bb72 | 2015-09-23 13:47:03 -0700 | [diff] [blame] | 2070 | audio_extn_perf_lock_release(); |
Alain Vongsouvanh | 13f26e8 | 2016-11-18 14:39:11 -0800 | [diff] [blame] | 2071 | audio_extn_tfa_98xx_enable_speaker(); |
vivek mehta | e59cfb2 | 2017-06-16 15:57:11 -0700 | [diff] [blame] | 2072 | |
Haynes Mathew George | d9ca796 | 2017-12-01 16:29:56 -0800 | [diff] [blame] | 2073 | if (out->usecase == USECASE_AUDIO_PLAYBACK_ULL || |
| 2074 | out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) { |
| 2075 | audio_low_latency_hint_start(); |
| 2076 | } |
| 2077 | |
vivek mehta | e59cfb2 | 2017-06-16 15:57:11 -0700 | [diff] [blame] | 2078 | // consider a scenario where on pause lower layers are tear down. |
| 2079 | // so on resume, swap mixer control need to be sent only when |
| 2080 | // backend is active, hence rather than sending from enable device |
| 2081 | // sending it from start of streamtream |
| 2082 | |
| 2083 | platform_set_swap_channels(adev, true); |
| 2084 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 2085 | ALOGV("%s: exit", __func__); |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 2086 | return 0; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2087 | error_open: |
Wei Wang | f4837d5 | 2017-11-21 14:51:20 -0800 | [diff] [blame] | 2088 | audio_streaming_hint_end(); |
Ravi Kumar Alamanda | 533bb72 | 2015-09-23 13:47:03 -0700 | [diff] [blame] | 2089 | audio_extn_perf_lock_release(); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2090 | stop_output_stream(out); |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 2091 | error_config: |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 2092 | return ret; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2093 | } |
| 2094 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2095 | static int check_input_parameters(uint32_t sample_rate, |
| 2096 | audio_format_t format, |
Eric Laurent | 74b5576 | 2017-07-09 17:04:53 -0700 | [diff] [blame] | 2097 | int channel_count, bool is_usb_hifi) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2098 | { |
Haynes Mathew George | e95340e | 2017-05-24 15:42:06 -0700 | [diff] [blame] | 2099 | if ((format != AUDIO_FORMAT_PCM_16_BIT) && |
| 2100 | (format != AUDIO_FORMAT_PCM_8_24_BIT) && |
Eric Laurent | 74b5576 | 2017-07-09 17:04:53 -0700 | [diff] [blame] | 2101 | (format != AUDIO_FORMAT_PCM_24_BIT_PACKED) && |
| 2102 | !(is_usb_hifi && (format == AUDIO_FORMAT_PCM_32_BIT))) { |
vivek mehta | dae4471 | 2015-07-27 14:13:18 -0700 | [diff] [blame] | 2103 | ALOGE("%s: unsupported AUDIO FORMAT (%d) ", __func__, format); |
| 2104 | return -EINVAL; |
| 2105 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2106 | |
Eric Laurent | 74b5576 | 2017-07-09 17:04:53 -0700 | [diff] [blame] | 2107 | int max_channel_count = is_usb_hifi ? MAX_HIFI_CHANNEL_COUNT : MAX_CHANNEL_COUNT; |
| 2108 | if ((channel_count < MIN_CHANNEL_COUNT) || (channel_count > max_channel_count)) { |
Jean-Michel Trivi | c075069 | 2015-10-12 12:12:32 -0700 | [diff] [blame] | 2109 | ALOGE("%s: unsupported channel count (%d) passed Min / Max (%d / %d)", __func__, |
Eric Laurent | 74b5576 | 2017-07-09 17:04:53 -0700 | [diff] [blame] | 2110 | channel_count, MIN_CHANNEL_COUNT, max_channel_count); |
vivek mehta | dae4471 | 2015-07-27 14:13:18 -0700 | [diff] [blame] | 2111 | return -EINVAL; |
| 2112 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2113 | |
| 2114 | switch (sample_rate) { |
| 2115 | case 8000: |
| 2116 | case 11025: |
| 2117 | case 12000: |
| 2118 | case 16000: |
| 2119 | case 22050: |
| 2120 | case 24000: |
| 2121 | case 32000: |
| 2122 | case 44100: |
| 2123 | case 48000: |
Haynes Mathew George | e95340e | 2017-05-24 15:42:06 -0700 | [diff] [blame] | 2124 | case 96000: |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2125 | break; |
| 2126 | default: |
vivek mehta | dae4471 | 2015-07-27 14:13:18 -0700 | [diff] [blame] | 2127 | ALOGE("%s: unsupported (%d) samplerate passed ", __func__, sample_rate); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2128 | return -EINVAL; |
| 2129 | } |
| 2130 | |
| 2131 | return 0; |
| 2132 | } |
| 2133 | |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 2134 | static size_t get_stream_buffer_size(size_t duration_ms, |
| 2135 | uint32_t sample_rate, |
| 2136 | audio_format_t format, |
| 2137 | int channel_count, |
| 2138 | bool is_low_latency) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2139 | { |
| 2140 | size_t size = 0; |
| 2141 | |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 2142 | size = (sample_rate * duration_ms) / 1000; |
Glenn Kasten | 68e79ce | 2014-07-15 10:56:59 -0700 | [diff] [blame] | 2143 | if (is_low_latency) |
Glenn Kasten | 4f99339 | 2014-05-14 07:30:48 -0700 | [diff] [blame] | 2144 | size = configured_low_latency_capture_period_size; |
vivek mehta | 4ed66e6 | 2016-04-15 23:33:34 -0700 | [diff] [blame] | 2145 | |
| 2146 | size *= channel_count * audio_bytes_per_sample(format); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2147 | |
Glenn Kasten | 4f99339 | 2014-05-14 07:30:48 -0700 | [diff] [blame] | 2148 | /* make sure the size is multiple of 32 bytes |
| 2149 | * At 48 kHz mono 16-bit PCM: |
| 2150 | * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15) |
| 2151 | * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10) |
| 2152 | */ |
| 2153 | size += 0x1f; |
| 2154 | size &= ~0x1f; |
Ravi Kumar Alamanda | 33d3306 | 2013-06-11 14:40:01 -0700 | [diff] [blame] | 2155 | |
| 2156 | return size; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2157 | } |
| 2158 | |
| 2159 | static uint32_t out_get_sample_rate(const struct audio_stream *stream) |
| 2160 | { |
| 2161 | struct stream_out *out = (struct stream_out *)stream; |
| 2162 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2163 | return out->sample_rate; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2164 | } |
| 2165 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 2166 | 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] | 2167 | { |
| 2168 | return -ENOSYS; |
| 2169 | } |
| 2170 | |
| 2171 | static size_t out_get_buffer_size(const struct audio_stream *stream) |
| 2172 | { |
| 2173 | struct stream_out *out = (struct stream_out *)stream; |
| 2174 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2175 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
| 2176 | return out->compr_config.fragment_size; |
| 2177 | } |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 2178 | return out->config.period_size * out->af_period_multiplier * |
Eric Laurent | fdf296a | 2014-07-03 16:41:51 -0700 | [diff] [blame] | 2179 | audio_stream_out_frame_size((const struct audio_stream_out *)stream); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2180 | } |
| 2181 | |
| 2182 | static uint32_t out_get_channels(const struct audio_stream *stream) |
| 2183 | { |
| 2184 | struct stream_out *out = (struct stream_out *)stream; |
| 2185 | |
| 2186 | return out->channel_mask; |
| 2187 | } |
| 2188 | |
| 2189 | static audio_format_t out_get_format(const struct audio_stream *stream) |
| 2190 | { |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2191 | struct stream_out *out = (struct stream_out *)stream; |
| 2192 | |
| 2193 | return out->format; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2194 | } |
| 2195 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 2196 | 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] | 2197 | { |
| 2198 | return -ENOSYS; |
| 2199 | } |
| 2200 | |
Kevin Rocard | 2ba1a47 | 2017-08-10 20:26:09 -0700 | [diff] [blame] | 2201 | /* must be called with out->lock locked */ |
| 2202 | static int out_standby_l(struct audio_stream *stream) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2203 | { |
| 2204 | struct stream_out *out = (struct stream_out *)stream; |
| 2205 | struct audio_device *adev = out->dev; |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 2206 | bool do_stop = true; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2207 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2208 | if (!out->standby) { |
Haynes Mathew George | 88e6fb2 | 2015-08-19 11:51:34 -0700 | [diff] [blame] | 2209 | if (adev->adm_deregister_stream) |
| 2210 | adev->adm_deregister_stream(adev->adm_data, out->handle); |
Ravi Kumar Alamanda | 8bba9e9 | 2013-11-11 21:09:07 -0800 | [diff] [blame] | 2211 | pthread_mutex_lock(&adev->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2212 | out->standby = true; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2213 | if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
| 2214 | if (out->pcm) { |
| 2215 | pcm_close(out->pcm); |
| 2216 | out->pcm = NULL; |
| 2217 | } |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 2218 | if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) { |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 2219 | do_stop = out->playback_started; |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 2220 | out->playback_started = false; |
| 2221 | } |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2222 | } else { |
| 2223 | stop_compressed_output_l(out); |
Haynes Mathew George | 352f27b | 2013-07-26 00:00:15 -0700 | [diff] [blame] | 2224 | out->gapless_mdata.encoder_delay = 0; |
| 2225 | out->gapless_mdata.encoder_padding = 0; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2226 | if (out->compr != NULL) { |
| 2227 | compress_close(out->compr); |
| 2228 | out->compr = NULL; |
| 2229 | } |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 2230 | } |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 2231 | if (do_stop) { |
| 2232 | stop_output_stream(out); |
| 2233 | } |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 2234 | pthread_mutex_unlock(&adev->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2235 | } |
Kevin Rocard | 2ba1a47 | 2017-08-10 20:26:09 -0700 | [diff] [blame] | 2236 | return 0; |
| 2237 | } |
| 2238 | |
| 2239 | static int out_standby(struct audio_stream *stream) |
| 2240 | { |
| 2241 | struct stream_out *out = (struct stream_out *)stream; |
| 2242 | |
| 2243 | ALOGV("%s: enter: usecase(%d: %s)", __func__, |
| 2244 | out->usecase, use_case_table[out->usecase]); |
| 2245 | |
| 2246 | lock_output_stream(out); |
| 2247 | out_standby_l(stream); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2248 | pthread_mutex_unlock(&out->lock); |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 2249 | ALOGV("%s: exit", __func__); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2250 | return 0; |
| 2251 | } |
| 2252 | |
Haynes Mathew George | 3ddd3bd | 2016-07-07 20:01:53 -0700 | [diff] [blame] | 2253 | static int out_on_error(struct audio_stream *stream) |
| 2254 | { |
| 2255 | struct stream_out *out = (struct stream_out *)stream; |
| 2256 | struct audio_device *adev = out->dev; |
| 2257 | bool do_standby = false; |
| 2258 | |
| 2259 | lock_output_stream(out); |
| 2260 | if (!out->standby) { |
| 2261 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
| 2262 | stop_compressed_output_l(out); |
| 2263 | send_offload_cmd_l(out, OFFLOAD_CMD_ERROR); |
| 2264 | } else |
| 2265 | do_standby = true; |
| 2266 | } |
| 2267 | pthread_mutex_unlock(&out->lock); |
| 2268 | |
| 2269 | if (do_standby) |
| 2270 | return out_standby(&out->stream.common); |
| 2271 | |
| 2272 | return 0; |
| 2273 | } |
| 2274 | |
Andy Hung | 7401c7c | 2016-09-21 12:41:21 -0700 | [diff] [blame] | 2275 | static int out_dump(const struct audio_stream *stream, int fd) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2276 | { |
Andy Hung | 7401c7c | 2016-09-21 12:41:21 -0700 | [diff] [blame] | 2277 | struct stream_out *out = (struct stream_out *)stream; |
| 2278 | |
| 2279 | // We try to get the lock for consistency, |
| 2280 | // but it isn't necessary for these variables. |
| 2281 | // If we're not in standby, we may be blocked on a write. |
| 2282 | const bool locked = (pthread_mutex_trylock(&out->lock) == 0); |
| 2283 | dprintf(fd, " Standby: %s\n", out->standby ? "yes" : "no"); |
| 2284 | dprintf(fd, " Frames written: %lld\n", (long long)out->written); |
| 2285 | |
| 2286 | if (locked) { |
Andy Hung | 7401c7c | 2016-09-21 12:41:21 -0700 | [diff] [blame] | 2287 | pthread_mutex_unlock(&out->lock); |
Andy Hung | 7401c7c | 2016-09-21 12:41:21 -0700 | [diff] [blame] | 2288 | } |
Andy Hung | a452b0a | 2017-03-15 14:51:15 -0700 | [diff] [blame] | 2289 | |
| 2290 | // dump error info |
| 2291 | (void)error_log_dump( |
| 2292 | out->error_log, fd, " " /* prefix */, 0 /* lines */, 0 /* limit_ns */); |
Andy Hung | 3ba2c92 | 2017-06-15 17:48:35 -0700 | [diff] [blame] | 2293 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2294 | return 0; |
| 2295 | } |
| 2296 | |
Haynes Mathew George | 352f27b | 2013-07-26 00:00:15 -0700 | [diff] [blame] | 2297 | static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms) |
| 2298 | { |
| 2299 | int ret = 0; |
| 2300 | char value[32]; |
| 2301 | struct compr_gapless_mdata tmp_mdata; |
| 2302 | |
| 2303 | if (!out || !parms) { |
| 2304 | return -EINVAL; |
| 2305 | } |
| 2306 | |
| 2307 | ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value)); |
| 2308 | if (ret >= 0) { |
| 2309 | tmp_mdata.encoder_delay = atoi(value); //whats a good limit check? |
| 2310 | } else { |
| 2311 | return -EINVAL; |
| 2312 | } |
| 2313 | |
| 2314 | ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value)); |
| 2315 | if (ret >= 0) { |
| 2316 | tmp_mdata.encoder_padding = atoi(value); |
| 2317 | } else { |
| 2318 | return -EINVAL; |
| 2319 | } |
| 2320 | |
| 2321 | out->gapless_mdata = tmp_mdata; |
| 2322 | out->send_new_metadata = 1; |
| 2323 | ALOGV("%s new encoder delay %u and padding %u", __func__, |
| 2324 | out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding); |
| 2325 | |
| 2326 | return 0; |
| 2327 | } |
| 2328 | |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 2329 | static bool output_drives_call(struct audio_device *adev, struct stream_out *out) |
| 2330 | { |
| 2331 | return out == adev->primary_output || out == adev->voice_tx_output; |
| 2332 | } |
Haynes Mathew George | 352f27b | 2013-07-26 00:00:15 -0700 | [diff] [blame] | 2333 | |
Kevin Rocard | 1e02c88 | 2017-08-09 15:26:07 -0700 | [diff] [blame] | 2334 | static int get_alive_usb_card(struct str_parms* parms) { |
| 2335 | int card; |
| 2336 | if ((str_parms_get_int(parms, "card", &card) >= 0) && |
| 2337 | !audio_extn_usb_alive(card)) { |
| 2338 | return card; |
| 2339 | } |
| 2340 | return -ENODEV; |
| 2341 | } |
| 2342 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2343 | static int out_set_parameters(struct audio_stream *stream, const char *kvpairs) |
| 2344 | { |
| 2345 | struct stream_out *out = (struct stream_out *)stream; |
| 2346 | struct audio_device *adev = out->dev; |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 2347 | struct audio_usecase *usecase; |
| 2348 | struct listnode *node; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2349 | struct str_parms *parms; |
| 2350 | char value[32]; |
| 2351 | int ret, val = 0; |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 2352 | bool select_new_device = false; |
Eric Laurent | 03f0943 | 2014-03-25 18:09:11 -0700 | [diff] [blame] | 2353 | int status = 0; |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 2354 | bool bypass_a2dp = false; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2355 | |
Eric Laurent | 2e140aa | 2016-06-30 17:14:46 -0700 | [diff] [blame] | 2356 | ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s", |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 2357 | __func__, out->usecase, use_case_table[out->usecase], kvpairs); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2358 | parms = str_parms_create_str(kvpairs); |
| 2359 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value)); |
| 2360 | if (ret >= 0) { |
| 2361 | val = atoi(value); |
Kevin Rocard | 2ba1a47 | 2017-08-10 20:26:09 -0700 | [diff] [blame] | 2362 | |
Eric Laurent | a147807 | 2015-09-21 17:21:52 -0700 | [diff] [blame] | 2363 | lock_output_stream(out); |
Kevin Rocard | 2ba1a47 | 2017-08-10 20:26:09 -0700 | [diff] [blame] | 2364 | |
| 2365 | // The usb driver needs to be closed after usb device disconnection |
| 2366 | // otherwise audio is no longer played on the new usb devices. |
| 2367 | // By forcing the stream in standby, the usb stack refcount drops to 0 |
| 2368 | // and the driver is closed. |
| 2369 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD && val == AUDIO_DEVICE_NONE && |
| 2370 | audio_is_usb_out_device(out->devices)) { |
| 2371 | ALOGD("%s() putting the usb device in standby after disconnection", __func__); |
| 2372 | out_standby_l(&out->stream.common); |
| 2373 | } |
| 2374 | |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 2375 | pthread_mutex_lock(&adev->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2376 | |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 2377 | /* |
| 2378 | * When HDMI cable is unplugged the music playback is paused and |
| 2379 | * the policy manager sends routing=0. But the audioflinger |
| 2380 | * continues to write data until standby time (3sec). |
| 2381 | * As the HDMI core is turned off, the write gets blocked. |
| 2382 | * Avoid this by routing audio to speaker until standby. |
| 2383 | */ |
| 2384 | if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL && |
| 2385 | val == AUDIO_DEVICE_NONE) { |
| 2386 | val = AUDIO_DEVICE_OUT_SPEAKER; |
| 2387 | } |
| 2388 | |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 2389 | /* |
| 2390 | * When A2DP is disconnected the |
| 2391 | * music playback is paused and the policy manager sends routing=0 |
| 2392 | * But the audioflingercontinues to write data until standby time |
| 2393 | * (3sec). As BT is turned off, the write gets blocked. |
| 2394 | * Avoid this by routing audio to speaker until standby. |
| 2395 | */ |
| 2396 | if ((out->devices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP) && |
| 2397 | (val == AUDIO_DEVICE_NONE) && |
| 2398 | !audio_extn_a2dp_is_ready()) { |
| 2399 | val = AUDIO_DEVICE_OUT_SPEAKER; |
| 2400 | } |
| 2401 | |
| 2402 | /* To avoid a2dp to sco overlapping / BT device improper state |
| 2403 | * check with BT lib about a2dp streaming support before routing |
| 2404 | */ |
| 2405 | if (val & AUDIO_DEVICE_OUT_ALL_A2DP) { |
| 2406 | if (!audio_extn_a2dp_is_ready()) { |
| 2407 | if (val & AUDIO_DEVICE_OUT_SPEAKER) { |
| 2408 | //combo usecase just by pass a2dp |
| 2409 | ALOGW("%s: A2DP profile is not ready,routing to speaker only", __func__); |
| 2410 | bypass_a2dp = true; |
| 2411 | } else { |
| 2412 | ALOGE("%s: A2DP profile is not ready,ignoring routing request", __func__); |
| 2413 | /* update device to a2dp and don't route as BT returned error |
| 2414 | * However it is still possible a2dp routing called because |
| 2415 | * of current active device disconnection (like wired headset) |
| 2416 | */ |
| 2417 | out->devices = val; |
| 2418 | pthread_mutex_unlock(&out->lock); |
| 2419 | pthread_mutex_unlock(&adev->lock); |
| 2420 | status = -ENOSYS; |
| 2421 | goto routing_fail; |
| 2422 | } |
| 2423 | } |
| 2424 | } |
| 2425 | |
Kevin Rocard | f9f241e | 2017-08-07 19:21:36 -0700 | [diff] [blame] | 2426 | audio_devices_t new_dev = val; |
| 2427 | |
| 2428 | // Workaround: If routing to an non existing usb device, fail gracefully |
| 2429 | // The routing request will otherwise block during 10 second |
Kevin Rocard | 1e02c88 | 2017-08-09 15:26:07 -0700 | [diff] [blame] | 2430 | int card; |
| 2431 | if (audio_is_usb_out_device(new_dev) && |
| 2432 | (card = get_alive_usb_card(parms)) >= 0) { |
| 2433 | |
| 2434 | 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] | 2435 | pthread_mutex_unlock(&adev->lock); |
| 2436 | pthread_mutex_unlock(&out->lock); |
| 2437 | status = -ENOSYS; |
| 2438 | goto routing_fail; |
| 2439 | } |
| 2440 | |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 2441 | /* |
| 2442 | * select_devices() call below switches all the usecases on the same |
Ravi Kumar Alamanda | b7ea4f5 | 2015-06-08 16:44:05 -0700 | [diff] [blame] | 2443 | * 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] | 2444 | * the select_devices(). But how do we undo this? |
| 2445 | * |
| 2446 | * For example, music playback is active on headset (deep-buffer usecase) |
| 2447 | * and if we go to ringtones and select a ringtone, low-latency usecase |
| 2448 | * will be started on headset+speaker. As we can't enable headset+speaker |
| 2449 | * and headset devices at the same time, select_devices() switches the music |
| 2450 | * playback to headset+speaker while starting low-lateny usecase for ringtone. |
| 2451 | * So when the ringtone playback is completed, how do we undo the same? |
| 2452 | * |
| 2453 | * We are relying on the out_set_parameters() call on deep-buffer output, |
| 2454 | * once the ringtone playback is ended. |
| 2455 | * NOTE: We should not check if the current devices are same as new devices. |
| 2456 | * Because select_devices() must be called to switch back the music |
| 2457 | * playback to headset. |
| 2458 | */ |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 2459 | if (new_dev != AUDIO_DEVICE_NONE) { |
| 2460 | bool same_dev = out->devices == new_dev; |
| 2461 | out->devices = new_dev; |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 2462 | |
Eric Laurent | a765719 | 2014-10-09 21:09:33 -0700 | [diff] [blame] | 2463 | if (output_drives_call(adev, out)) { |
| 2464 | if (!voice_is_in_call(adev)) { |
| 2465 | if (adev->mode == AUDIO_MODE_IN_CALL) { |
| 2466 | adev->current_call_output = out; |
| 2467 | ret = voice_start_call(adev); |
| 2468 | } |
| 2469 | } else { |
| 2470 | adev->current_call_output = out; |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 2471 | voice_update_devices_for_all_voice_usecases(adev); |
Eric Laurent | a765719 | 2014-10-09 21:09:33 -0700 | [diff] [blame] | 2472 | } |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 2473 | } |
vivek mehta | 0d3637a | 2016-07-24 09:32:02 -0700 | [diff] [blame] | 2474 | |
| 2475 | if (!out->standby) { |
| 2476 | if (!same_dev) { |
| 2477 | ALOGV("update routing change"); |
Haynes Mathew George | 104fdfd | 2016-07-01 16:57:24 -0700 | [diff] [blame] | 2478 | // inform adm before actual routing to prevent glitches. |
| 2479 | if (adev->adm_on_routing_change) { |
| 2480 | adev->adm_on_routing_change(adev->adm_data, |
| 2481 | out->handle); |
| 2482 | } |
vivek mehta | 0d3637a | 2016-07-24 09:32:02 -0700 | [diff] [blame] | 2483 | } |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 2484 | if (!bypass_a2dp) { |
| 2485 | select_devices(adev, out->usecase); |
| 2486 | } else { |
| 2487 | out->devices = AUDIO_DEVICE_OUT_SPEAKER; |
| 2488 | select_devices(adev, out->usecase); |
| 2489 | out->devices = new_dev; |
| 2490 | } |
Alain Vongsouvanh | 13f26e8 | 2016-11-18 14:39:11 -0800 | [diff] [blame] | 2491 | audio_extn_tfa_98xx_update(); |
vivek mehta | e59cfb2 | 2017-06-16 15:57:11 -0700 | [diff] [blame] | 2492 | |
| 2493 | // on device switch force swap, lower functions will make sure |
| 2494 | // to check if swap is allowed or not. |
| 2495 | |
| 2496 | if (!same_dev) |
| 2497 | platform_set_swap_channels(adev, true); |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 2498 | |
| 2499 | if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && |
| 2500 | out->a2dp_compress_mute && |
| 2501 | (!(out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) || audio_extn_a2dp_is_ready())) { |
| 2502 | pthread_mutex_lock(&out->compr_mute_lock); |
| 2503 | out->a2dp_compress_mute = false; |
| 2504 | set_compr_volume(&out->stream, out->volume_l, out->volume_r); |
| 2505 | pthread_mutex_unlock(&out->compr_mute_lock); |
| 2506 | } |
vivek mehta | 0d3637a | 2016-07-24 09:32:02 -0700 | [diff] [blame] | 2507 | } |
| 2508 | |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 2509 | } |
| 2510 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2511 | pthread_mutex_unlock(&adev->lock); |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 2512 | pthread_mutex_unlock(&out->lock); |
Eric Laurent | 0499d4f | 2014-08-25 22:39:29 -0500 | [diff] [blame] | 2513 | |
| 2514 | /*handles device and call state changes*/ |
| 2515 | audio_extn_extspk_update(adev->extspk); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2516 | } |
Kevin Rocard | f9f241e | 2017-08-07 19:21:36 -0700 | [diff] [blame] | 2517 | routing_fail: |
Haynes Mathew George | 352f27b | 2013-07-26 00:00:15 -0700 | [diff] [blame] | 2518 | |
| 2519 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
| 2520 | parse_compress_metadata(out, parms); |
| 2521 | } |
| 2522 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2523 | str_parms_destroy(parms); |
Eric Laurent | 03f0943 | 2014-03-25 18:09:11 -0700 | [diff] [blame] | 2524 | ALOGV("%s: exit: code(%d)", __func__, status); |
| 2525 | return status; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2526 | } |
| 2527 | |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 2528 | static bool stream_get_parameter_channels(struct str_parms *query, |
| 2529 | struct str_parms *reply, |
| 2530 | audio_channel_mask_t *supported_channel_masks) { |
| 2531 | int ret = -1; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2532 | char value[256]; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2533 | bool first = true; |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 2534 | size_t i, j; |
| 2535 | |
| 2536 | if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) { |
| 2537 | ret = 0; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2538 | value[0] = '\0'; |
| 2539 | i = 0; |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 2540 | while (supported_channel_masks[i] != 0) { |
| 2541 | for (j = 0; j < ARRAY_SIZE(channels_name_to_enum_table); j++) { |
| 2542 | 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] | 2543 | if (!first) { |
| 2544 | strcat(value, "|"); |
| 2545 | } |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 2546 | strcat(value, channels_name_to_enum_table[j].name); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2547 | first = false; |
| 2548 | break; |
| 2549 | } |
| 2550 | } |
| 2551 | i++; |
| 2552 | } |
| 2553 | str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value); |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 2554 | } |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 2555 | return ret >= 0; |
| 2556 | } |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 2557 | |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 2558 | static bool stream_get_parameter_formats(struct str_parms *query, |
| 2559 | struct str_parms *reply, |
| 2560 | audio_format_t *supported_formats) { |
| 2561 | int ret = -1; |
| 2562 | char value[256]; |
| 2563 | int i; |
| 2564 | |
| 2565 | if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) { |
| 2566 | ret = 0; |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 2567 | value[0] = '\0'; |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 2568 | switch (supported_formats[0]) { |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 2569 | case AUDIO_FORMAT_PCM_16_BIT: |
| 2570 | strcat(value, "AUDIO_FORMAT_PCM_16_BIT"); |
| 2571 | break; |
| 2572 | case AUDIO_FORMAT_PCM_24_BIT_PACKED: |
| 2573 | strcat(value, "AUDIO_FORMAT_PCM_24_BIT_PACKED"); |
| 2574 | break; |
| 2575 | case AUDIO_FORMAT_PCM_32_BIT: |
| 2576 | strcat(value, "AUDIO_FORMAT_PCM_32_BIT"); |
| 2577 | break; |
| 2578 | default: |
| 2579 | ALOGE("%s: unsupported format %#x", __func__, |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 2580 | supported_formats[0]); |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 2581 | break; |
| 2582 | } |
| 2583 | str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_FORMATS, value); |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 2584 | } |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 2585 | return ret >= 0; |
| 2586 | } |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 2587 | |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 2588 | static bool stream_get_parameter_rates(struct str_parms *query, |
| 2589 | struct str_parms *reply, |
| 2590 | uint32_t *supported_sample_rates) { |
| 2591 | |
| 2592 | int i; |
| 2593 | char value[256]; |
| 2594 | int ret = -1; |
| 2595 | if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) { |
| 2596 | ret = 0; |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 2597 | value[0] = '\0'; |
| 2598 | i=0; |
| 2599 | int cursor = 0; |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 2600 | while (supported_sample_rates[i]) { |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 2601 | int avail = sizeof(value) - cursor; |
| 2602 | ret = snprintf(value + cursor, avail, "%s%d", |
| 2603 | cursor > 0 ? "|" : "", |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 2604 | supported_sample_rates[i]); |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 2605 | if (ret < 0 || ret >= avail) { |
| 2606 | // if cursor is at the last element of the array |
| 2607 | // overwrite with \0 is duplicate work as |
| 2608 | // snprintf already put a \0 in place. |
| 2609 | // else |
| 2610 | // we had space to write the '|' at value[cursor] |
| 2611 | // (which will be overwritten) or no space to fill |
| 2612 | // the first element (=> cursor == 0) |
| 2613 | value[cursor] = '\0'; |
| 2614 | break; |
| 2615 | } |
| 2616 | cursor += ret; |
| 2617 | ++i; |
| 2618 | } |
| 2619 | str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES, |
| 2620 | value); |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 2621 | } |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 2622 | return ret >= 0; |
| 2623 | } |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 2624 | |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 2625 | static char* out_get_parameters(const struct audio_stream *stream, const char *keys) |
| 2626 | { |
| 2627 | struct stream_out *out = (struct stream_out *)stream; |
| 2628 | struct str_parms *query = str_parms_create_str(keys); |
| 2629 | char *str; |
| 2630 | struct str_parms *reply = str_parms_create(); |
| 2631 | bool replied = false; |
| 2632 | ALOGV("%s: enter: keys - %s", __func__, keys); |
| 2633 | |
| 2634 | replied |= stream_get_parameter_channels(query, reply, |
| 2635 | &out->supported_channel_masks[0]); |
| 2636 | replied |= stream_get_parameter_formats(query, reply, |
| 2637 | &out->supported_formats[0]); |
| 2638 | replied |= stream_get_parameter_rates(query, reply, |
| 2639 | &out->supported_sample_rates[0]); |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 2640 | if (replied) { |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2641 | str = str_parms_to_str(reply); |
| 2642 | } else { |
Kevin Rocard | edf0b4c | 2017-05-05 09:08:11 -0700 | [diff] [blame] | 2643 | str = strdup(""); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2644 | } |
| 2645 | str_parms_destroy(query); |
| 2646 | str_parms_destroy(reply); |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 2647 | ALOGV("%s: exit: returns - %s", __func__, str); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2648 | return str; |
| 2649 | } |
| 2650 | |
| 2651 | static uint32_t out_get_latency(const struct audio_stream_out *stream) |
| 2652 | { |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 2653 | uint32_t hw_delay, period_ms; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2654 | struct stream_out *out = (struct stream_out *)stream; |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 2655 | uint32_t latency; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2656 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2657 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) |
| 2658 | return COMPRESS_OFFLOAD_PLAYBACK_LATENCY; |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 2659 | else if ((out->realtime) || |
| 2660 | (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP)) { |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 2661 | // since the buffer won't be filled up faster than realtime, |
| 2662 | // return a smaller number |
| 2663 | period_ms = (out->af_period_multiplier * out->config.period_size * |
| 2664 | 1000) / (out->config.rate); |
| 2665 | hw_delay = platform_render_latency(out->usecase)/1000; |
| 2666 | return period_ms + hw_delay; |
| 2667 | } |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2668 | |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 2669 | latency = (out->config.period_count * out->config.period_size * 1000) / |
| 2670 | (out->config.rate); |
| 2671 | |
| 2672 | if (AUDIO_DEVICE_OUT_ALL_A2DP & out->devices) |
| 2673 | latency += audio_extn_a2dp_get_encoder_latency(); |
| 2674 | |
| 2675 | return latency; |
| 2676 | } |
| 2677 | |
| 2678 | static int set_compr_volume(struct audio_stream_out *stream, float left, |
| 2679 | float right) |
| 2680 | { |
| 2681 | struct stream_out *out = (struct stream_out *)stream; |
| 2682 | int volume[2]; |
| 2683 | char mixer_ctl_name[128]; |
| 2684 | struct audio_device *adev = out->dev; |
| 2685 | struct mixer_ctl *ctl; |
| 2686 | int pcm_device_id = platform_get_pcm_device_id(out->usecase, |
| 2687 | PCM_PLAYBACK); |
| 2688 | |
| 2689 | snprintf(mixer_ctl_name, sizeof(mixer_ctl_name), |
| 2690 | "Compress Playback %d Volume", pcm_device_id); |
| 2691 | ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name); |
| 2692 | if (!ctl) { |
| 2693 | ALOGE("%s: Could not get ctl for mixer cmd - %s", |
| 2694 | __func__, mixer_ctl_name); |
| 2695 | return -EINVAL; |
| 2696 | } |
| 2697 | ALOGV("%s: ctl for mixer cmd - %s, left %f, right %f", |
| 2698 | __func__, mixer_ctl_name, left, right); |
| 2699 | volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX); |
| 2700 | volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX); |
| 2701 | mixer_ctl_set_array(ctl, volume, sizeof(volume) / sizeof(volume[0])); |
| 2702 | |
| 2703 | return 0; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2704 | } |
| 2705 | |
| 2706 | static int out_set_volume(struct audio_stream_out *stream, float left, |
| 2707 | float right) |
| 2708 | { |
Eric Laurent | a9024de | 2013-04-04 09:19:12 -0700 | [diff] [blame] | 2709 | struct stream_out *out = (struct stream_out *)stream; |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 2710 | int ret = 0; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2711 | |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 2712 | if (out->usecase == USECASE_AUDIO_PLAYBACK_HIFI) { |
Eric Laurent | a9024de | 2013-04-04 09:19:12 -0700 | [diff] [blame] | 2713 | /* only take left channel into account: the API is for stereo anyway */ |
| 2714 | out->muted = (left == 0.0f); |
| 2715 | return 0; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2716 | } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 2717 | pthread_mutex_lock(&out->compr_mute_lock); |
| 2718 | ALOGV("%s: compress mute %d", __func__, out->a2dp_compress_mute); |
| 2719 | if (!out->a2dp_compress_mute) |
| 2720 | ret = set_compr_volume(stream, left, right); |
| 2721 | out->volume_l = left; |
| 2722 | out->volume_r = right; |
| 2723 | pthread_mutex_unlock(&out->compr_mute_lock); |
| 2724 | return ret; |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 2725 | } else if (out->usecase == USECASE_AUDIO_PLAYBACK_VOIP) { |
vivek mehta | 4a82477 | 2017-06-08 19:05:49 -0700 | [diff] [blame] | 2726 | out->app_type_cfg.gain[0] = (int)(left * VOIP_PLAYBACK_VOLUME_MAX); |
| 2727 | out->app_type_cfg.gain[1] = (int)(right * VOIP_PLAYBACK_VOLUME_MAX); |
| 2728 | if (!out->standby) { |
| 2729 | // if in standby, cached volume will be sent after stream is opened |
| 2730 | audio_extn_utils_send_app_type_gain(out->dev, |
| 2731 | out->app_type_cfg.app_type, |
| 2732 | &out->app_type_cfg.gain[0]); |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 2733 | } |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 2734 | return 0; |
Eric Laurent | a9024de | 2013-04-04 09:19:12 -0700 | [diff] [blame] | 2735 | } |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2736 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2737 | return -ENOSYS; |
| 2738 | } |
| 2739 | |
Haynes Mathew George | c735fb0 | 2016-06-30 18:00:28 -0700 | [diff] [blame] | 2740 | // note: this call is safe only if the stream_cb is |
| 2741 | // removed first in close_output_stream (as is done now). |
| 2742 | static void out_snd_mon_cb(void * stream, struct str_parms * parms) |
| 2743 | { |
| 2744 | if (!stream || !parms) |
| 2745 | return; |
| 2746 | |
| 2747 | struct stream_out *out = (struct stream_out *)stream; |
| 2748 | struct audio_device *adev = out->dev; |
| 2749 | |
| 2750 | card_status_t status; |
| 2751 | int card; |
| 2752 | if (parse_snd_card_status(parms, &card, &status) < 0) |
| 2753 | return; |
| 2754 | |
| 2755 | pthread_mutex_lock(&adev->lock); |
| 2756 | bool valid_cb = (card == adev->snd_card); |
| 2757 | pthread_mutex_unlock(&adev->lock); |
| 2758 | |
| 2759 | if (!valid_cb) |
| 2760 | return; |
| 2761 | |
Haynes Mathew George | c735fb0 | 2016-06-30 18:00:28 -0700 | [diff] [blame] | 2762 | lock_output_stream(out); |
| 2763 | if (out->card_status != status) |
| 2764 | out->card_status = status; |
| 2765 | pthread_mutex_unlock(&out->lock); |
| 2766 | |
Haynes Mathew George | 3ddd3bd | 2016-07-07 20:01:53 -0700 | [diff] [blame] | 2767 | ALOGW("out_snd_mon_cb for card %d usecase %s, status %s", card, |
| 2768 | use_case_table[out->usecase], |
| 2769 | status == CARD_STATUS_OFFLINE ? "offline" : "online"); |
| 2770 | |
| 2771 | if (status == CARD_STATUS_OFFLINE) |
| 2772 | out_on_error(stream); |
Haynes Mathew George | c735fb0 | 2016-06-30 18:00:28 -0700 | [diff] [blame] | 2773 | |
| 2774 | return; |
| 2775 | } |
| 2776 | |
Uday Kishore Pasupuleti | cec8ad8 | 2015-04-15 10:34:06 -0700 | [diff] [blame] | 2777 | #ifdef NO_AUDIO_OUT |
| 2778 | 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] | 2779 | const void *buffer __unused, size_t bytes) |
Uday Kishore Pasupuleti | cec8ad8 | 2015-04-15 10:34:06 -0700 | [diff] [blame] | 2780 | { |
| 2781 | struct stream_out *out = (struct stream_out *)stream; |
| 2782 | |
| 2783 | /* No Output device supported other than BT for playback. |
| 2784 | * Sleep for the amount of buffer duration |
| 2785 | */ |
Eric Laurent | a147807 | 2015-09-21 17:21:52 -0700 | [diff] [blame] | 2786 | lock_output_stream(out); |
Ariel Gertzenstein | c2c1174 | 2016-11-14 18:08:28 -0500 | [diff] [blame] | 2787 | usleep(bytes * 1000000 / audio_stream_out_frame_size( |
| 2788 | (const struct audio_stream_out *)&out->stream) / |
Uday Kishore Pasupuleti | cec8ad8 | 2015-04-15 10:34:06 -0700 | [diff] [blame] | 2789 | out_get_sample_rate(&out->stream.common)); |
| 2790 | pthread_mutex_unlock(&out->lock); |
| 2791 | return bytes; |
| 2792 | } |
| 2793 | #endif |
| 2794 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2795 | static ssize_t out_write(struct audio_stream_out *stream, const void *buffer, |
| 2796 | size_t bytes) |
| 2797 | { |
| 2798 | struct stream_out *out = (struct stream_out *)stream; |
| 2799 | struct audio_device *adev = out->dev; |
Eric Laurent | 6e89524 | 2013-09-05 16:10:57 -0700 | [diff] [blame] | 2800 | ssize_t ret = 0; |
Andy Hung | 7401c7c | 2016-09-21 12:41:21 -0700 | [diff] [blame] | 2801 | int error_code = ERROR_CODE_STANDBY; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2802 | |
Eric Laurent | a147807 | 2015-09-21 17:21:52 -0700 | [diff] [blame] | 2803 | lock_output_stream(out); |
vivek mehta | e3afca2 | 2017-04-11 17:13:50 -0700 | [diff] [blame] | 2804 | // this is always nonzero |
Andy Hung | 8e7f03f | 2017-06-12 14:05:22 -0700 | [diff] [blame] | 2805 | const size_t frame_size = audio_stream_out_frame_size(stream); |
| 2806 | const size_t frames = bytes / frame_size; |
vivek mehta | e3afca2 | 2017-04-11 17:13:50 -0700 | [diff] [blame] | 2807 | |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 2808 | if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) { |
| 2809 | error_code = ERROR_CODE_WRITE; |
| 2810 | goto exit; |
| 2811 | } |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 2812 | |
| 2813 | if ((out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) && |
| 2814 | (audio_extn_a2dp_is_suspended())) { |
| 2815 | if (!(out->devices & AUDIO_DEVICE_OUT_SPEAKER)) { |
| 2816 | if (!(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) { |
| 2817 | ret = -EIO; |
| 2818 | goto exit; |
| 2819 | } |
| 2820 | } |
| 2821 | } |
| 2822 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2823 | if (out->standby) { |
Ravi Kumar Alamanda | 59d296d | 2013-05-02 11:25:27 -0700 | [diff] [blame] | 2824 | out->standby = false; |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 2825 | pthread_mutex_lock(&adev->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2826 | ret = start_output_stream(out); |
vivek mehta | 4012509 | 2017-08-21 18:48:51 -0700 | [diff] [blame] | 2827 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2828 | /* ToDo: If use case is compress offload should return 0 */ |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2829 | if (ret != 0) { |
Ravi Kumar Alamanda | 59d296d | 2013-05-02 11:25:27 -0700 | [diff] [blame] | 2830 | out->standby = true; |
vivek mehta | 4012509 | 2017-08-21 18:48:51 -0700 | [diff] [blame] | 2831 | pthread_mutex_unlock(&adev->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2832 | goto exit; |
| 2833 | } |
vivek mehta | fb4d7bd | 2016-04-29 03:16:47 -0700 | [diff] [blame] | 2834 | |
vivek mehta | 4012509 | 2017-08-21 18:48:51 -0700 | [diff] [blame] | 2835 | // after standby always force set last known cal step |
| 2836 | // dont change level anywhere except at the audio_hw_send_gain_dep_calibration |
| 2837 | ALOGD("%s: retry previous failed cal level set", __func__); |
| 2838 | send_gain_dep_calibration_l(); |
| 2839 | pthread_mutex_unlock(&adev->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2840 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2841 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2842 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 2843 | ALOGVV("%s: writing buffer (%zu bytes) to compress device", __func__, bytes); |
Haynes Mathew George | 352f27b | 2013-07-26 00:00:15 -0700 | [diff] [blame] | 2844 | if (out->send_new_metadata) { |
| 2845 | ALOGVV("send new gapless metadata"); |
| 2846 | compress_set_gapless_metadata(out->compr, &out->gapless_mdata); |
| 2847 | out->send_new_metadata = 0; |
| 2848 | } |
Eric Laurent | b49b3f6 | 2016-02-29 17:59:49 -0800 | [diff] [blame] | 2849 | unsigned int avail; |
| 2850 | struct timespec tstamp; |
| 2851 | ret = compress_get_hpointer(out->compr, &avail, &tstamp); |
| 2852 | /* Do not limit write size if the available frames count is unknown */ |
| 2853 | if (ret != 0) { |
| 2854 | avail = bytes; |
| 2855 | } |
| 2856 | if (avail == 0) { |
| 2857 | ret = 0; |
| 2858 | } else { |
| 2859 | if (avail > bytes) { |
| 2860 | avail = bytes; |
| 2861 | } |
| 2862 | ret = compress_write(out->compr, buffer, avail); |
| 2863 | ALOGVV("%s: writing buffer (%d bytes) to compress device returned %zd", |
| 2864 | __func__, avail, ret); |
| 2865 | } |
Haynes Mathew George | 352f27b | 2013-07-26 00:00:15 -0700 | [diff] [blame] | 2866 | |
Eric Laurent | 6e89524 | 2013-09-05 16:10:57 -0700 | [diff] [blame] | 2867 | if (ret >= 0 && ret < (ssize_t)bytes) { |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2868 | send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER); |
| 2869 | } |
Eric Laurent | b49b3f6 | 2016-02-29 17:59:49 -0800 | [diff] [blame] | 2870 | if (ret > 0 && !out->playback_started) { |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2871 | compress_start(out->compr); |
| 2872 | out->playback_started = 1; |
| 2873 | out->offload_state = OFFLOAD_STATE_PLAYING; |
| 2874 | } |
Andy Hung | 7401c7c | 2016-09-21 12:41:21 -0700 | [diff] [blame] | 2875 | if (ret < 0) { |
Andy Hung | a452b0a | 2017-03-15 14:51:15 -0700 | [diff] [blame] | 2876 | 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] | 2877 | } else { |
| 2878 | out->written += ret; // accumulate bytes written for offload. |
Andy Hung | 7401c7c | 2016-09-21 12:41:21 -0700 | [diff] [blame] | 2879 | } |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2880 | pthread_mutex_unlock(&out->lock); |
Andy Hung | fc044e1 | 2017-03-20 09:24:22 -0700 | [diff] [blame] | 2881 | // TODO: consider logging offload pcm |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2882 | return ret; |
| 2883 | } else { |
Andy Hung | 7401c7c | 2016-09-21 12:41:21 -0700 | [diff] [blame] | 2884 | error_code = ERROR_CODE_WRITE; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2885 | if (out->pcm) { |
Eric Laurent | ad2dde9 | 2017-09-20 18:27:31 -0700 | [diff] [blame] | 2886 | size_t bytes_to_write = bytes; |
| 2887 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2888 | if (out->muted) |
| 2889 | memset((void *)buffer, 0, bytes); |
Eric Laurent | ad2dde9 | 2017-09-20 18:27:31 -0700 | [diff] [blame] | 2890 | // FIXME: this can be removed once audio flinger mixer supports mono output |
Nadav Bar | 3d72cfc | 2018-01-07 12:19:24 +0200 | [diff] [blame] | 2891 | 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] | 2892 | size_t channel_count = audio_channel_count_from_out_mask(out->channel_mask); |
| 2893 | int16_t *src = (int16_t *)buffer; |
| 2894 | int16_t *dst = (int16_t *)buffer; |
Haynes Mathew George | 88e6fb2 | 2015-08-19 11:51:34 -0700 | [diff] [blame] | 2895 | |
Eric Laurent | ad2dde9 | 2017-09-20 18:27:31 -0700 | [diff] [blame] | 2896 | LOG_ALWAYS_FATAL_IF(out->config.channels != 1 || channel_count != 2 || |
| 2897 | out->format != AUDIO_FORMAT_PCM_16_BIT, |
| 2898 | "out_write called for VOIP use case with wrong properties"); |
Haynes Mathew George | 88e6fb2 | 2015-08-19 11:51:34 -0700 | [diff] [blame] | 2899 | |
Eric Laurent | ad2dde9 | 2017-09-20 18:27:31 -0700 | [diff] [blame] | 2900 | for (size_t i = 0; i < frames ; i++, dst++, src += 2) { |
| 2901 | *dst = (int16_t)(((int32_t)src[0] + (int32_t)src[1]) >> 1); |
| 2902 | } |
| 2903 | bytes_to_write /= 2; |
| 2904 | } |
| 2905 | ALOGVV("%s: writing buffer (%zu bytes) to pcm device", __func__, bytes_to_write); |
| 2906 | |
Glenn Kasten | 5a59dff | 2018-02-22 08:05:35 -0800 | [diff] [blame] | 2907 | long ns = (frames * (int64_t) NANOS_PER_SECOND) / out->config.rate; |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 2908 | request_out_focus(out, ns); |
| 2909 | |
| 2910 | bool use_mmap = is_mmap_usecase(out->usecase) || out->realtime; |
| 2911 | if (use_mmap) |
Eric Laurent | ad2dde9 | 2017-09-20 18:27:31 -0700 | [diff] [blame] | 2912 | ret = pcm_mmap_write(out->pcm, (void *)buffer, bytes_to_write); |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 2913 | else |
Eric Laurent | ad2dde9 | 2017-09-20 18:27:31 -0700 | [diff] [blame] | 2914 | ret = pcm_write(out->pcm, (void *)buffer, bytes_to_write); |
Haynes Mathew George | 88e6fb2 | 2015-08-19 11:51:34 -0700 | [diff] [blame] | 2915 | |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 2916 | release_out_focus(out, ns); |
Andy Hung | 7401c7c | 2016-09-21 12:41:21 -0700 | [diff] [blame] | 2917 | } else { |
| 2918 | LOG_ALWAYS_FATAL("out->pcm is NULL after starting output stream"); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2919 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2920 | } |
| 2921 | |
| 2922 | exit: |
Andy Hung | da9b56b | 2016-09-16 20:06:35 -0700 | [diff] [blame] | 2923 | // For PCM we always consume the buffer and return #bytes regardless of ret. |
| 2924 | if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
Andy Hung | 8e7f03f | 2017-06-12 14:05:22 -0700 | [diff] [blame] | 2925 | out->written += frames; |
Andy Hung | da9b56b | 2016-09-16 20:06:35 -0700 | [diff] [blame] | 2926 | } |
Andy Hung | 7401c7c | 2016-09-21 12:41:21 -0700 | [diff] [blame] | 2927 | long long sleeptime_us = 0; |
Andy Hung | 9e737de | 2017-05-22 10:51:22 -0700 | [diff] [blame] | 2928 | |
Andy Hung | 7401c7c | 2016-09-21 12:41:21 -0700 | [diff] [blame] | 2929 | if (ret != 0) { |
Andy Hung | 3ba2c92 | 2017-06-15 17:48:35 -0700 | [diff] [blame] | 2930 | 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] | 2931 | if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
| 2932 | ALOGE_IF(out->pcm != NULL, |
| 2933 | "%s: error %zd - %s", __func__, ret, pcm_get_error(out->pcm)); |
Andy Hung | 8e7f03f | 2017-06-12 14:05:22 -0700 | [diff] [blame] | 2934 | sleeptime_us = frames * 1000000LL / out_get_sample_rate(&out->stream.common); |
Andy Hung | 7401c7c | 2016-09-21 12:41:21 -0700 | [diff] [blame] | 2935 | // usleep not guaranteed for values over 1 second but we don't limit here. |
| 2936 | } |
| 2937 | } |
Andy Hung | da9b56b | 2016-09-16 20:06:35 -0700 | [diff] [blame] | 2938 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2939 | pthread_mutex_unlock(&out->lock); |
| 2940 | |
| 2941 | if (ret != 0) { |
Haynes Mathew George | f9c7aae | 2016-09-20 14:12:41 -0700 | [diff] [blame] | 2942 | out_on_error(&out->stream.common); |
Andy Hung | 7401c7c | 2016-09-21 12:41:21 -0700 | [diff] [blame] | 2943 | if (sleeptime_us != 0) |
| 2944 | usleep(sleeptime_us); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2945 | } |
| 2946 | return bytes; |
| 2947 | } |
| 2948 | |
| 2949 | static int out_get_render_position(const struct audio_stream_out *stream, |
| 2950 | uint32_t *dsp_frames) |
| 2951 | { |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2952 | struct stream_out *out = (struct stream_out *)stream; |
| 2953 | *dsp_frames = 0; |
| 2954 | if ((out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) && (dsp_frames != NULL)) { |
Eric Laurent | a147807 | 2015-09-21 17:21:52 -0700 | [diff] [blame] | 2955 | lock_output_stream(out); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2956 | if (out->compr != NULL) { |
Andy Hung | 7171da2 | 2016-03-08 16:58:42 -0800 | [diff] [blame] | 2957 | unsigned long frames = 0; |
| 2958 | // TODO: check return value |
| 2959 | compress_get_tstamp(out->compr, &frames, &out->sample_rate); |
| 2960 | *dsp_frames = (uint32_t)frames; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2961 | ALOGVV("%s rendered frames %d sample_rate %d", |
| 2962 | __func__, *dsp_frames, out->sample_rate); |
| 2963 | } |
| 2964 | pthread_mutex_unlock(&out->lock); |
| 2965 | return 0; |
| 2966 | } else |
Kevin Rocard | 75d41bf | 2017-05-02 16:38:39 -0700 | [diff] [blame] | 2967 | return -ENODATA; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2968 | } |
| 2969 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 2970 | static int out_add_audio_effect(const struct audio_stream *stream __unused, |
| 2971 | effect_handle_t effect __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2972 | { |
| 2973 | return 0; |
| 2974 | } |
| 2975 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 2976 | static int out_remove_audio_effect(const struct audio_stream *stream __unused, |
| 2977 | effect_handle_t effect __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2978 | { |
| 2979 | return 0; |
| 2980 | } |
| 2981 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 2982 | static int out_get_next_write_timestamp(const struct audio_stream_out *stream __unused, |
| 2983 | int64_t *timestamp __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2984 | { |
Kevin Rocard | 75d41bf | 2017-05-02 16:38:39 -0700 | [diff] [blame] | 2985 | return -ENOSYS; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2986 | } |
| 2987 | |
Glenn Kasten | 2ccd7ba | 2013-09-10 09:04:31 -0700 | [diff] [blame] | 2988 | static int out_get_presentation_position(const struct audio_stream_out *stream, |
| 2989 | uint64_t *frames, struct timespec *timestamp) |
| 2990 | { |
| 2991 | struct stream_out *out = (struct stream_out *)stream; |
Kevin Rocard | 75d41bf | 2017-05-02 16:38:39 -0700 | [diff] [blame] | 2992 | int ret = -ENODATA; |
Eric Laurent | 949a089 | 2013-09-20 09:20:13 -0700 | [diff] [blame] | 2993 | unsigned long dsp_frames; |
Glenn Kasten | 2ccd7ba | 2013-09-10 09:04:31 -0700 | [diff] [blame] | 2994 | |
Eric Laurent | a147807 | 2015-09-21 17:21:52 -0700 | [diff] [blame] | 2995 | lock_output_stream(out); |
Glenn Kasten | 2ccd7ba | 2013-09-10 09:04:31 -0700 | [diff] [blame] | 2996 | |
Eric Laurent | 949a089 | 2013-09-20 09:20:13 -0700 | [diff] [blame] | 2997 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
| 2998 | if (out->compr != NULL) { |
Andy Hung | 7171da2 | 2016-03-08 16:58:42 -0800 | [diff] [blame] | 2999 | // TODO: check return value |
Eric Laurent | 949a089 | 2013-09-20 09:20:13 -0700 | [diff] [blame] | 3000 | compress_get_tstamp(out->compr, &dsp_frames, |
| 3001 | &out->sample_rate); |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 3002 | // Adjustment accounts for A2DP encoder latency with offload usecases |
| 3003 | // Note: Encoder latency is returned in ms. |
| 3004 | if (AUDIO_DEVICE_OUT_ALL_A2DP & out->devices) { |
| 3005 | unsigned long offset = |
| 3006 | (audio_extn_a2dp_get_encoder_latency() * out->sample_rate / 1000); |
| 3007 | dsp_frames = (dsp_frames > offset) ? (dsp_frames - offset) : 0; |
| 3008 | } |
Eric Laurent | 949a089 | 2013-09-20 09:20:13 -0700 | [diff] [blame] | 3009 | ALOGVV("%s rendered frames %ld sample_rate %d", |
| 3010 | __func__, dsp_frames, out->sample_rate); |
| 3011 | *frames = dsp_frames; |
| 3012 | ret = 0; |
| 3013 | /* this is the best we can do */ |
| 3014 | clock_gettime(CLOCK_MONOTONIC, timestamp); |
| 3015 | } |
| 3016 | } else { |
| 3017 | if (out->pcm) { |
vivek mehta | 1a9b7c0 | 2015-06-25 11:49:38 -0700 | [diff] [blame] | 3018 | unsigned int avail; |
Eric Laurent | 949a089 | 2013-09-20 09:20:13 -0700 | [diff] [blame] | 3019 | if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) { |
| 3020 | size_t kernel_buffer_size = out->config.period_size * out->config.period_count; |
Eric Laurent | 949a089 | 2013-09-20 09:20:13 -0700 | [diff] [blame] | 3021 | int64_t signed_frames = out->written - kernel_buffer_size + avail; |
Haynes Mathew George | 7ff216f | 2013-09-11 19:51:41 -0700 | [diff] [blame] | 3022 | // This adjustment accounts for buffering after app processor. |
| 3023 | // It is based on estimated DSP latency per use case, rather than exact. |
| 3024 | signed_frames -= |
| 3025 | (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL); |
| 3026 | |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 3027 | // Adjustment accounts for A2DP encoder latency with non-offload usecases |
| 3028 | // Note: Encoder latency is returned in ms, while platform_render_latency in us. |
| 3029 | if (AUDIO_DEVICE_OUT_ALL_A2DP & out->devices) { |
| 3030 | signed_frames -= |
| 3031 | (audio_extn_a2dp_get_encoder_latency() * out->sample_rate / 1000); |
| 3032 | } |
| 3033 | |
Eric Laurent | 949a089 | 2013-09-20 09:20:13 -0700 | [diff] [blame] | 3034 | // It would be unusual for this value to be negative, but check just in case ... |
| 3035 | if (signed_frames >= 0) { |
| 3036 | *frames = signed_frames; |
| 3037 | ret = 0; |
| 3038 | } |
Glenn Kasten | 2ccd7ba | 2013-09-10 09:04:31 -0700 | [diff] [blame] | 3039 | } |
| 3040 | } |
| 3041 | } |
| 3042 | |
| 3043 | pthread_mutex_unlock(&out->lock); |
| 3044 | |
| 3045 | return ret; |
| 3046 | } |
| 3047 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3048 | static int out_set_callback(struct audio_stream_out *stream, |
| 3049 | stream_callback_t callback, void *cookie) |
| 3050 | { |
| 3051 | struct stream_out *out = (struct stream_out *)stream; |
| 3052 | |
| 3053 | ALOGV("%s", __func__); |
Eric Laurent | a147807 | 2015-09-21 17:21:52 -0700 | [diff] [blame] | 3054 | lock_output_stream(out); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3055 | out->offload_callback = callback; |
| 3056 | out->offload_cookie = cookie; |
| 3057 | pthread_mutex_unlock(&out->lock); |
| 3058 | return 0; |
| 3059 | } |
| 3060 | |
| 3061 | static int out_pause(struct audio_stream_out* stream) |
| 3062 | { |
| 3063 | struct stream_out *out = (struct stream_out *)stream; |
| 3064 | int status = -ENOSYS; |
| 3065 | ALOGV("%s", __func__); |
| 3066 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
Eric Laurent | a147807 | 2015-09-21 17:21:52 -0700 | [diff] [blame] | 3067 | lock_output_stream(out); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3068 | if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) { |
| 3069 | status = compress_pause(out->compr); |
| 3070 | out->offload_state = OFFLOAD_STATE_PAUSED; |
| 3071 | } |
| 3072 | pthread_mutex_unlock(&out->lock); |
| 3073 | } |
| 3074 | return status; |
| 3075 | } |
| 3076 | |
| 3077 | static int out_resume(struct audio_stream_out* stream) |
| 3078 | { |
| 3079 | struct stream_out *out = (struct stream_out *)stream; |
| 3080 | int status = -ENOSYS; |
| 3081 | ALOGV("%s", __func__); |
| 3082 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
| 3083 | status = 0; |
Eric Laurent | a147807 | 2015-09-21 17:21:52 -0700 | [diff] [blame] | 3084 | lock_output_stream(out); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3085 | if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) { |
| 3086 | status = compress_resume(out->compr); |
| 3087 | out->offload_state = OFFLOAD_STATE_PLAYING; |
| 3088 | } |
| 3089 | pthread_mutex_unlock(&out->lock); |
| 3090 | } |
| 3091 | return status; |
| 3092 | } |
| 3093 | |
| 3094 | static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type ) |
| 3095 | { |
| 3096 | struct stream_out *out = (struct stream_out *)stream; |
| 3097 | int status = -ENOSYS; |
| 3098 | ALOGV("%s", __func__); |
| 3099 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
Eric Laurent | a147807 | 2015-09-21 17:21:52 -0700 | [diff] [blame] | 3100 | lock_output_stream(out); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3101 | if (type == AUDIO_DRAIN_EARLY_NOTIFY) |
| 3102 | status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN); |
| 3103 | else |
| 3104 | status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN); |
| 3105 | pthread_mutex_unlock(&out->lock); |
| 3106 | } |
| 3107 | return status; |
| 3108 | } |
| 3109 | |
| 3110 | static int out_flush(struct audio_stream_out* stream) |
| 3111 | { |
| 3112 | struct stream_out *out = (struct stream_out *)stream; |
| 3113 | ALOGV("%s", __func__); |
| 3114 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
Eric Laurent | a147807 | 2015-09-21 17:21:52 -0700 | [diff] [blame] | 3115 | lock_output_stream(out); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3116 | stop_compressed_output_l(out); |
| 3117 | pthread_mutex_unlock(&out->lock); |
| 3118 | return 0; |
| 3119 | } |
| 3120 | return -ENOSYS; |
| 3121 | } |
| 3122 | |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3123 | static int out_stop(const struct audio_stream_out* stream) |
| 3124 | { |
| 3125 | struct stream_out *out = (struct stream_out *)stream; |
| 3126 | struct audio_device *adev = out->dev; |
| 3127 | int ret = -ENOSYS; |
| 3128 | |
| 3129 | ALOGV("%s", __func__); |
| 3130 | pthread_mutex_lock(&adev->lock); |
| 3131 | if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP && !out->standby && |
| 3132 | out->playback_started && out->pcm != NULL) { |
| 3133 | pcm_stop(out->pcm); |
| 3134 | ret = stop_output_stream(out); |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 3135 | out->playback_started = false; |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3136 | } |
| 3137 | pthread_mutex_unlock(&adev->lock); |
| 3138 | return ret; |
| 3139 | } |
| 3140 | |
| 3141 | static int out_start(const struct audio_stream_out* stream) |
| 3142 | { |
| 3143 | struct stream_out *out = (struct stream_out *)stream; |
| 3144 | struct audio_device *adev = out->dev; |
| 3145 | int ret = -ENOSYS; |
| 3146 | |
| 3147 | ALOGV("%s", __func__); |
| 3148 | pthread_mutex_lock(&adev->lock); |
| 3149 | if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP && !out->standby && |
| 3150 | !out->playback_started && out->pcm != NULL) { |
| 3151 | ret = start_output_stream(out); |
| 3152 | if (ret == 0) { |
| 3153 | out->playback_started = true; |
| 3154 | } |
| 3155 | } |
| 3156 | pthread_mutex_unlock(&adev->lock); |
| 3157 | return ret; |
| 3158 | } |
| 3159 | |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 3160 | /* |
| 3161 | * Modify config->period_count based on min_size_frames |
| 3162 | */ |
| 3163 | static void adjust_mmap_period_count(struct pcm_config *config, int32_t min_size_frames) |
| 3164 | { |
| 3165 | int periodCountRequested = (min_size_frames + config->period_size - 1) |
| 3166 | / config->period_size; |
| 3167 | int periodCount = MMAP_PERIOD_COUNT_MIN; |
| 3168 | |
| 3169 | ALOGV("%s original config.period_size = %d config.period_count = %d", |
| 3170 | __func__, config->period_size, config->period_count); |
| 3171 | |
| 3172 | while (periodCount < periodCountRequested && (periodCount * 2) < MMAP_PERIOD_COUNT_MAX) { |
| 3173 | periodCount *= 2; |
| 3174 | } |
| 3175 | config->period_count = periodCount; |
| 3176 | |
| 3177 | ALOGV("%s requested config.period_count = %d", __func__, config->period_count); |
| 3178 | } |
| 3179 | |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3180 | static int out_create_mmap_buffer(const struct audio_stream_out *stream, |
| 3181 | int32_t min_size_frames, |
| 3182 | struct audio_mmap_buffer_info *info) |
| 3183 | { |
| 3184 | struct stream_out *out = (struct stream_out *)stream; |
| 3185 | struct audio_device *adev = out->dev; |
| 3186 | int ret = 0; |
| 3187 | unsigned int offset1; |
| 3188 | unsigned int frames1; |
| 3189 | const char *step = ""; |
Haynes Mathew George | 96483a2 | 2017-03-28 14:52:47 -0700 | [diff] [blame] | 3190 | uint32_t mmap_size; |
| 3191 | uint32_t buffer_size; |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3192 | |
| 3193 | ALOGV("%s", __func__); |
Haynes Mathew George | f1aaf36 | 2017-12-18 15:10:10 -0800 | [diff] [blame] | 3194 | lock_output_stream(out); |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3195 | pthread_mutex_lock(&adev->lock); |
| 3196 | |
| 3197 | if (info == NULL || min_size_frames == 0) { |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 3198 | 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] | 3199 | ret = -EINVAL; |
| 3200 | goto exit; |
| 3201 | } |
| 3202 | if (out->usecase != USECASE_AUDIO_PLAYBACK_MMAP || !out->standby) { |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 3203 | ALOGE("%s: usecase = %d, standby = %d", __func__, out->usecase, out->standby); |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3204 | ret = -ENOSYS; |
| 3205 | goto exit; |
| 3206 | } |
| 3207 | out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK); |
| 3208 | if (out->pcm_device_id < 0) { |
| 3209 | ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)", |
| 3210 | __func__, out->pcm_device_id, out->usecase); |
| 3211 | ret = -EINVAL; |
| 3212 | goto exit; |
| 3213 | } |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 3214 | |
| 3215 | adjust_mmap_period_count(&out->config, min_size_frames); |
| 3216 | |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3217 | ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d", |
| 3218 | __func__, adev->snd_card, out->pcm_device_id, out->config.channels); |
| 3219 | out->pcm = pcm_open(adev->snd_card, out->pcm_device_id, |
| 3220 | (PCM_OUT | PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC), &out->config); |
| 3221 | if (out->pcm == NULL || !pcm_is_ready(out->pcm)) { |
| 3222 | step = "open"; |
| 3223 | ret = -ENODEV; |
| 3224 | goto exit; |
| 3225 | } |
| 3226 | ret = pcm_mmap_begin(out->pcm, &info->shared_memory_address, &offset1, &frames1); |
| 3227 | if (ret < 0) { |
| 3228 | step = "begin"; |
| 3229 | goto exit; |
| 3230 | } |
| 3231 | info->buffer_size_frames = pcm_get_buffer_size(out->pcm); |
Haynes Mathew George | 96483a2 | 2017-03-28 14:52:47 -0700 | [diff] [blame] | 3232 | buffer_size = pcm_frames_to_bytes(out->pcm, info->buffer_size_frames); |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3233 | info->burst_size_frames = out->config.period_size; |
Haynes Mathew George | 96483a2 | 2017-03-28 14:52:47 -0700 | [diff] [blame] | 3234 | ret = platform_get_mmap_data_fd(adev->platform, |
| 3235 | out->pcm_device_id, 0 /*playback*/, |
| 3236 | &info->shared_memory_fd, |
| 3237 | &mmap_size); |
| 3238 | if (ret < 0) { |
| 3239 | // Fall back to non exclusive mode |
| 3240 | info->shared_memory_fd = pcm_get_poll_fd(out->pcm); |
| 3241 | } else { |
| 3242 | if (mmap_size < buffer_size) { |
| 3243 | step = "mmap"; |
| 3244 | goto exit; |
| 3245 | } |
| 3246 | // FIXME: indicate exclusive mode support by returning a negative buffer size |
| 3247 | info->buffer_size_frames *= -1; |
| 3248 | } |
| 3249 | memset(info->shared_memory_address, 0, buffer_size); |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3250 | |
| 3251 | ret = pcm_mmap_commit(out->pcm, 0, MMAP_PERIOD_SIZE); |
| 3252 | if (ret < 0) { |
| 3253 | step = "commit"; |
| 3254 | goto exit; |
| 3255 | } |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 3256 | |
| 3257 | out->standby = false; |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3258 | ret = 0; |
| 3259 | |
| 3260 | ALOGV("%s: got mmap buffer address %p info->buffer_size_frames %d", |
| 3261 | __func__, info->shared_memory_address, info->buffer_size_frames); |
| 3262 | |
| 3263 | exit: |
| 3264 | if (ret != 0) { |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 3265 | if (out->pcm == NULL) { |
| 3266 | ALOGE("%s: %s - %d", __func__, step, ret); |
| 3267 | } else { |
| 3268 | ALOGE("%s: %s %s", __func__, step, pcm_get_error(out->pcm)); |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3269 | pcm_close(out->pcm); |
| 3270 | out->pcm = NULL; |
| 3271 | } |
| 3272 | } |
| 3273 | pthread_mutex_unlock(&adev->lock); |
Haynes Mathew George | f1aaf36 | 2017-12-18 15:10:10 -0800 | [diff] [blame] | 3274 | pthread_mutex_unlock(&out->lock); |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3275 | return ret; |
| 3276 | } |
| 3277 | |
| 3278 | static int out_get_mmap_position(const struct audio_stream_out *stream, |
| 3279 | struct audio_mmap_position *position) |
| 3280 | { |
Haynes Mathew George | f1aaf36 | 2017-12-18 15:10:10 -0800 | [diff] [blame] | 3281 | int ret = 0; |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3282 | struct stream_out *out = (struct stream_out *)stream; |
| 3283 | ALOGVV("%s", __func__); |
| 3284 | if (position == NULL) { |
| 3285 | return -EINVAL; |
| 3286 | } |
Haynes Mathew George | f1aaf36 | 2017-12-18 15:10:10 -0800 | [diff] [blame] | 3287 | lock_output_stream(out); |
| 3288 | if (out->usecase != USECASE_AUDIO_PLAYBACK_MMAP || |
| 3289 | out->pcm == NULL) { |
| 3290 | ret = -ENOSYS; |
| 3291 | goto exit; |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3292 | } |
| 3293 | |
| 3294 | struct timespec ts = { 0, 0 }; |
Haynes Mathew George | f1aaf36 | 2017-12-18 15:10:10 -0800 | [diff] [blame] | 3295 | 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] | 3296 | if (ret < 0) { |
| 3297 | ALOGE("%s: %s", __func__, pcm_get_error(out->pcm)); |
Haynes Mathew George | f1aaf36 | 2017-12-18 15:10:10 -0800 | [diff] [blame] | 3298 | goto exit; |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3299 | } |
Andy Hung | fc044e1 | 2017-03-20 09:24:22 -0700 | [diff] [blame] | 3300 | position->time_nanoseconds = audio_utils_ns_from_timespec(&ts); |
Haynes Mathew George | f1aaf36 | 2017-12-18 15:10:10 -0800 | [diff] [blame] | 3301 | exit: |
| 3302 | pthread_mutex_unlock(&out->lock); |
| 3303 | return ret; |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3304 | } |
| 3305 | |
| 3306 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3307 | /** audio_stream_in implementation **/ |
| 3308 | static uint32_t in_get_sample_rate(const struct audio_stream *stream) |
| 3309 | { |
| 3310 | struct stream_in *in = (struct stream_in *)stream; |
| 3311 | |
| 3312 | return in->config.rate; |
| 3313 | } |
| 3314 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 3315 | 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] | 3316 | { |
| 3317 | return -ENOSYS; |
| 3318 | } |
| 3319 | |
| 3320 | static size_t in_get_buffer_size(const struct audio_stream *stream) |
| 3321 | { |
| 3322 | struct stream_in *in = (struct stream_in *)stream; |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 3323 | return in->config.period_size * in->af_period_multiplier * |
| 3324 | audio_stream_in_frame_size((const struct audio_stream_in *)stream); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3325 | } |
| 3326 | |
| 3327 | static uint32_t in_get_channels(const struct audio_stream *stream) |
| 3328 | { |
| 3329 | struct stream_in *in = (struct stream_in *)stream; |
| 3330 | |
| 3331 | return in->channel_mask; |
| 3332 | } |
| 3333 | |
vivek mehta | 4ed66e6 | 2016-04-15 23:33:34 -0700 | [diff] [blame] | 3334 | 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] | 3335 | { |
vivek mehta | 4ed66e6 | 2016-04-15 23:33:34 -0700 | [diff] [blame] | 3336 | struct stream_in *in = (struct stream_in *)stream; |
| 3337 | return in->format; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3338 | } |
| 3339 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 3340 | 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] | 3341 | { |
| 3342 | return -ENOSYS; |
| 3343 | } |
| 3344 | |
| 3345 | static int in_standby(struct audio_stream *stream) |
| 3346 | { |
| 3347 | struct stream_in *in = (struct stream_in *)stream; |
| 3348 | struct audio_device *adev = in->dev; |
| 3349 | int status = 0; |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 3350 | bool do_stop = true; |
| 3351 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 3352 | ALOGV("%s: enter", __func__); |
Eric Laurent | a147807 | 2015-09-21 17:21:52 -0700 | [diff] [blame] | 3353 | |
| 3354 | lock_input_stream(in); |
Ravi Kumar Alamanda | a417cc5 | 2015-05-01 16:41:56 -0700 | [diff] [blame] | 3355 | |
| 3356 | if (!in->standby && in->is_st_session) { |
Joe Onorato | 188b622 | 2016-03-01 11:02:27 -0800 | [diff] [blame] | 3357 | ALOGV("%s: sound trigger pcm stop lab", __func__); |
Ravi Kumar Alamanda | a417cc5 | 2015-05-01 16:41:56 -0700 | [diff] [blame] | 3358 | audio_extn_sound_trigger_stop_lab(in); |
| 3359 | in->standby = true; |
| 3360 | } |
| 3361 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3362 | if (!in->standby) { |
Haynes Mathew George | 88e6fb2 | 2015-08-19 11:51:34 -0700 | [diff] [blame] | 3363 | if (adev->adm_deregister_stream) |
| 3364 | adev->adm_deregister_stream(adev->adm_data, in->capture_handle); |
| 3365 | |
Ravi Kumar Alamanda | 8bba9e9 | 2013-11-11 21:09:07 -0800 | [diff] [blame] | 3366 | pthread_mutex_lock(&adev->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3367 | in->standby = true; |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3368 | if (in->usecase == USECASE_AUDIO_RECORD_MMAP) { |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 3369 | do_stop = in->capture_started; |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3370 | in->capture_started = false; |
| 3371 | } |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 3372 | if (in->pcm) { |
| 3373 | pcm_close(in->pcm); |
| 3374 | in->pcm = NULL; |
| 3375 | } |
Eric Laurent | cefbbac | 2014-09-04 13:54:10 -0500 | [diff] [blame] | 3376 | adev->enable_voicerx = false; |
| 3377 | platform_set_echo_reference(adev, false, AUDIO_DEVICE_NONE ); |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 3378 | if (do_stop) { |
| 3379 | status = stop_input_stream(in); |
| 3380 | } |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 3381 | pthread_mutex_unlock(&adev->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3382 | } |
| 3383 | pthread_mutex_unlock(&in->lock); |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 3384 | ALOGV("%s: exit: status(%d)", __func__, status); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3385 | return status; |
| 3386 | } |
| 3387 | |
Andy Hung | d13f0d3 | 2017-06-12 13:58:37 -0700 | [diff] [blame] | 3388 | static int in_dump(const struct audio_stream *stream, int fd) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3389 | { |
Andy Hung | d13f0d3 | 2017-06-12 13:58:37 -0700 | [diff] [blame] | 3390 | struct stream_in *in = (struct stream_in *)stream; |
| 3391 | |
| 3392 | // We try to get the lock for consistency, |
| 3393 | // but it isn't necessary for these variables. |
| 3394 | // If we're not in standby, we may be blocked on a read. |
| 3395 | const bool locked = (pthread_mutex_trylock(&in->lock) == 0); |
| 3396 | dprintf(fd, " Standby: %s\n", in->standby ? "yes" : "no"); |
| 3397 | dprintf(fd, " Frames read: %lld\n", (long long)in->frames_read); |
| 3398 | dprintf(fd, " Frames muted: %lld\n", (long long)in->frames_muted); |
| 3399 | |
| 3400 | if (locked) { |
| 3401 | pthread_mutex_unlock(&in->lock); |
| 3402 | } |
| 3403 | |
| 3404 | // dump error info |
| 3405 | (void)error_log_dump( |
| 3406 | in->error_log, fd, " " /* prefix */, 0 /* lines */, 0 /* limit_ns */); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3407 | return 0; |
| 3408 | } |
| 3409 | |
| 3410 | static int in_set_parameters(struct audio_stream *stream, const char *kvpairs) |
| 3411 | { |
| 3412 | struct stream_in *in = (struct stream_in *)stream; |
| 3413 | struct audio_device *adev = in->dev; |
| 3414 | struct str_parms *parms; |
| 3415 | char *str; |
| 3416 | char value[32]; |
| 3417 | int ret, val = 0; |
Eric Laurent | 03f0943 | 2014-03-25 18:09:11 -0700 | [diff] [blame] | 3418 | int status = 0; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3419 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 3420 | ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3421 | parms = str_parms_create_str(kvpairs); |
| 3422 | |
| 3423 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value)); |
| 3424 | |
Eric Laurent | a147807 | 2015-09-21 17:21:52 -0700 | [diff] [blame] | 3425 | lock_input_stream(in); |
| 3426 | |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 3427 | pthread_mutex_lock(&adev->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3428 | if (ret >= 0) { |
| 3429 | val = atoi(value); |
| 3430 | /* no audio source uses val == 0 */ |
| 3431 | if ((in->source != val) && (val != 0)) { |
| 3432 | in->source = val; |
| 3433 | } |
| 3434 | } |
| 3435 | |
| 3436 | 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] | 3437 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3438 | if (ret >= 0) { |
| 3439 | val = atoi(value); |
Haynes Mathew George | 8ea0472 | 2017-07-25 17:41:49 -0700 | [diff] [blame] | 3440 | if (((int)in->device != val) && (val != 0) && audio_is_input_device(val) ) { |
Kevin Rocard | f9f241e | 2017-08-07 19:21:36 -0700 | [diff] [blame] | 3441 | |
| 3442 | // Workaround: If routing to an non existing usb device, fail gracefully |
| 3443 | // The routing request will otherwise block during 10 second |
Kevin Rocard | 1e02c88 | 2017-08-09 15:26:07 -0700 | [diff] [blame] | 3444 | int card; |
| 3445 | if (audio_is_usb_in_device(val) && |
| 3446 | (card = get_alive_usb_card(parms)) >= 0) { |
| 3447 | |
| 3448 | 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] | 3449 | status = -ENOSYS; |
| 3450 | } else { |
| 3451 | |
| 3452 | in->device = val; |
| 3453 | /* If recording is in progress, change the tx device to new device */ |
| 3454 | if (!in->standby) { |
| 3455 | ALOGV("update input routing change"); |
| 3456 | // inform adm before actual routing to prevent glitches. |
| 3457 | if (adev->adm_on_routing_change) { |
| 3458 | adev->adm_on_routing_change(adev->adm_data, |
| 3459 | in->capture_handle); |
| 3460 | } |
| 3461 | select_devices(adev, in->usecase); |
Haynes Mathew George | 104fdfd | 2016-07-01 16:57:24 -0700 | [diff] [blame] | 3462 | } |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 3463 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3464 | } |
| 3465 | } |
| 3466 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3467 | pthread_mutex_unlock(&adev->lock); |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 3468 | pthread_mutex_unlock(&in->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3469 | |
| 3470 | str_parms_destroy(parms); |
Eric Laurent | 03f0943 | 2014-03-25 18:09:11 -0700 | [diff] [blame] | 3471 | ALOGV("%s: exit: status(%d)", __func__, status); |
| 3472 | return status; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3473 | } |
| 3474 | |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 3475 | static char* in_get_parameters(const struct audio_stream *stream, |
| 3476 | const char *keys) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3477 | { |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 3478 | struct stream_in *in = (struct stream_in *)stream; |
| 3479 | struct str_parms *query = str_parms_create_str(keys); |
| 3480 | char *str; |
| 3481 | struct str_parms *reply = str_parms_create(); |
| 3482 | bool replied = false; |
| 3483 | |
| 3484 | ALOGV("%s: enter: keys - %s", __func__, keys); |
| 3485 | replied |= stream_get_parameter_channels(query, reply, |
| 3486 | &in->supported_channel_masks[0]); |
| 3487 | replied |= stream_get_parameter_formats(query, reply, |
| 3488 | &in->supported_formats[0]); |
| 3489 | replied |= stream_get_parameter_rates(query, reply, |
| 3490 | &in->supported_sample_rates[0]); |
| 3491 | if (replied) { |
| 3492 | str = str_parms_to_str(reply); |
| 3493 | } else { |
Kevin Rocard | 7e87668 | 2017-07-26 14:17:26 -0700 | [diff] [blame] | 3494 | str = strdup(""); |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 3495 | } |
| 3496 | str_parms_destroy(query); |
| 3497 | str_parms_destroy(reply); |
| 3498 | ALOGV("%s: exit: returns - %s", __func__, str); |
| 3499 | return str; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3500 | } |
| 3501 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 3502 | static int in_set_gain(struct audio_stream_in *stream __unused, float gain __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3503 | { |
Kevin Rocard | 75d41bf | 2017-05-02 16:38:39 -0700 | [diff] [blame] | 3504 | return -ENOSYS; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3505 | } |
| 3506 | |
Haynes Mathew George | c735fb0 | 2016-06-30 18:00:28 -0700 | [diff] [blame] | 3507 | static void in_snd_mon_cb(void * stream, struct str_parms * parms) |
| 3508 | { |
| 3509 | if (!stream || !parms) |
| 3510 | return; |
| 3511 | |
| 3512 | struct stream_in *in = (struct stream_in *)stream; |
| 3513 | struct audio_device *adev = in->dev; |
| 3514 | |
| 3515 | card_status_t status; |
| 3516 | int card; |
| 3517 | if (parse_snd_card_status(parms, &card, &status) < 0) |
| 3518 | return; |
| 3519 | |
| 3520 | pthread_mutex_lock(&adev->lock); |
| 3521 | bool valid_cb = (card == adev->snd_card); |
| 3522 | pthread_mutex_unlock(&adev->lock); |
| 3523 | |
| 3524 | if (!valid_cb) |
| 3525 | return; |
| 3526 | |
| 3527 | lock_input_stream(in); |
| 3528 | if (in->card_status != status) |
| 3529 | in->card_status = status; |
| 3530 | pthread_mutex_unlock(&in->lock); |
| 3531 | |
| 3532 | ALOGW("in_snd_mon_cb for card %d usecase %s, status %s", card, |
| 3533 | use_case_table[in->usecase], |
| 3534 | status == CARD_STATUS_OFFLINE ? "offline" : "online"); |
| 3535 | |
| 3536 | // a better solution would be to report error back to AF and let |
| 3537 | // it put the stream to standby |
| 3538 | if (status == CARD_STATUS_OFFLINE) |
| 3539 | in_standby(&in->stream.common); |
| 3540 | |
| 3541 | return; |
| 3542 | } |
| 3543 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3544 | static ssize_t in_read(struct audio_stream_in *stream, void *buffer, |
| 3545 | size_t bytes) |
| 3546 | { |
| 3547 | struct stream_in *in = (struct stream_in *)stream; |
| 3548 | struct audio_device *adev = in->dev; |
| 3549 | int i, ret = -1; |
vivek mehta | 4ed66e6 | 2016-04-15 23:33:34 -0700 | [diff] [blame] | 3550 | int *int_buf_stream = NULL; |
Andy Hung | d13f0d3 | 2017-06-12 13:58:37 -0700 | [diff] [blame] | 3551 | 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] | 3552 | |
Eric Laurent | a147807 | 2015-09-21 17:21:52 -0700 | [diff] [blame] | 3553 | lock_input_stream(in); |
Andy Hung | 8e7f03f | 2017-06-12 14:05:22 -0700 | [diff] [blame] | 3554 | const size_t frame_size = audio_stream_in_frame_size(stream); |
| 3555 | const size_t frames = bytes / frame_size; |
Eric Laurent | a147807 | 2015-09-21 17:21:52 -0700 | [diff] [blame] | 3556 | |
Ravi Kumar Alamanda | a417cc5 | 2015-05-01 16:41:56 -0700 | [diff] [blame] | 3557 | if (in->is_st_session) { |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3558 | ALOGVV(" %s: reading on st session bytes=%zu", __func__, bytes); |
Ravi Kumar Alamanda | a417cc5 | 2015-05-01 16:41:56 -0700 | [diff] [blame] | 3559 | /* Read from sound trigger HAL */ |
| 3560 | audio_extn_sound_trigger_read(in, buffer, bytes); |
| 3561 | pthread_mutex_unlock(&in->lock); |
| 3562 | return bytes; |
| 3563 | } |
| 3564 | |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3565 | if (in->usecase == USECASE_AUDIO_RECORD_MMAP) { |
| 3566 | ret = -ENOSYS; |
| 3567 | goto exit; |
| 3568 | } |
| 3569 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3570 | if (in->standby) { |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 3571 | pthread_mutex_lock(&adev->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3572 | ret = start_input_stream(in); |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 3573 | pthread_mutex_unlock(&adev->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3574 | if (ret != 0) { |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3575 | goto exit; |
| 3576 | } |
| 3577 | in->standby = 0; |
| 3578 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3579 | |
Andy Hung | d13f0d3 | 2017-06-12 13:58:37 -0700 | [diff] [blame] | 3580 | // errors that occur here are read errors. |
| 3581 | error_code = ERROR_CODE_READ; |
| 3582 | |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 3583 | //what's the duration requested by the client? |
| 3584 | long ns = pcm_bytes_to_frames(in->pcm, bytes)*1000000000LL/ |
| 3585 | in->config.rate; |
| 3586 | request_in_focus(in, ns); |
Haynes Mathew George | 88e6fb2 | 2015-08-19 11:51:34 -0700 | [diff] [blame] | 3587 | |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 3588 | bool use_mmap = is_mmap_usecase(in->usecase) || in->realtime; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3589 | if (in->pcm) { |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 3590 | if (use_mmap) { |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 3591 | ret = pcm_mmap_read(in->pcm, buffer, bytes); |
vivek mehta | 4ed66e6 | 2016-04-15 23:33:34 -0700 | [diff] [blame] | 3592 | } else { |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 3593 | ret = pcm_read(in->pcm, buffer, bytes); |
Eric Laurent | f8b50aa | 2016-05-06 11:03:53 -0700 | [diff] [blame] | 3594 | } |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 3595 | if (ret < 0) { |
| 3596 | ALOGE("Failed to read w/err %s", strerror(errno)); |
| 3597 | ret = -errno; |
| 3598 | } |
Eric Laurent | f8b50aa | 2016-05-06 11:03:53 -0700 | [diff] [blame] | 3599 | if (!ret && bytes > 0 && (in->format == AUDIO_FORMAT_PCM_8_24_BIT)) { |
| 3600 | if (bytes % 4 == 0) { |
| 3601 | /* data from DSP comes in 24_8 format, convert it to 8_24 */ |
| 3602 | int_buf_stream = buffer; |
| 3603 | for (size_t itt=0; itt < bytes/4 ; itt++) { |
| 3604 | int_buf_stream[itt] >>= 8; |
vivek mehta | 4ed66e6 | 2016-04-15 23:33:34 -0700 | [diff] [blame] | 3605 | } |
Eric Laurent | f8b50aa | 2016-05-06 11:03:53 -0700 | [diff] [blame] | 3606 | } else { |
| 3607 | ALOGE("%s: !!! something wrong !!! ... data not 32 bit aligned ", __func__); |
| 3608 | ret = -EINVAL; |
| 3609 | goto exit; |
vivek mehta | 4ed66e6 | 2016-04-15 23:33:34 -0700 | [diff] [blame] | 3610 | } |
| 3611 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3612 | } |
| 3613 | |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 3614 | release_in_focus(in, ns); |
Haynes Mathew George | 88e6fb2 | 2015-08-19 11:51:34 -0700 | [diff] [blame] | 3615 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3616 | /* |
| 3617 | * Instead of writing zeroes here, we could trust the hardware |
| 3618 | * to always provide zeroes when muted. |
Eric Laurent | 7b2b5ab | 2014-09-14 12:29:59 -0700 | [diff] [blame] | 3619 | * 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] | 3620 | */ |
Haynes Mathew George | 988277b | 2017-12-20 14:14:33 -0800 | [diff] [blame] | 3621 | if (ret == 0 && adev->mic_muted && |
| 3622 | !voice_is_in_call_rec_stream(in) && |
| 3623 | in->usecase != USECASE_AUDIO_RECORD_AFE_PROXY) { |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3624 | memset(buffer, 0, bytes); |
Andy Hung | d13f0d3 | 2017-06-12 13:58:37 -0700 | [diff] [blame] | 3625 | in->frames_muted += frames; |
| 3626 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3627 | |
| 3628 | exit: |
| 3629 | pthread_mutex_unlock(&in->lock); |
| 3630 | |
| 3631 | if (ret != 0) { |
Andy Hung | d13f0d3 | 2017-06-12 13:58:37 -0700 | [diff] [blame] | 3632 | 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] | 3633 | in_standby(&in->stream.common); |
| 3634 | ALOGV("%s: read failed - sleeping for buffer duration", __func__); |
Andy Hung | 8e7f03f | 2017-06-12 14:05:22 -0700 | [diff] [blame] | 3635 | usleep(frames * 1000000LL / in_get_sample_rate(&in->stream.common)); |
Andy Hung | c858987 | 2016-03-10 16:37:48 -0800 | [diff] [blame] | 3636 | memset(buffer, 0, bytes); // clear return data |
Andy Hung | d13f0d3 | 2017-06-12 13:58:37 -0700 | [diff] [blame] | 3637 | in->frames_muted += frames; |
Andy Hung | c858987 | 2016-03-10 16:37:48 -0800 | [diff] [blame] | 3638 | } |
| 3639 | if (bytes > 0) { |
Andy Hung | 8e7f03f | 2017-06-12 14:05:22 -0700 | [diff] [blame] | 3640 | in->frames_read += frames; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3641 | } |
| 3642 | return bytes; |
| 3643 | } |
| 3644 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 3645 | 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] | 3646 | { |
| 3647 | return 0; |
| 3648 | } |
| 3649 | |
Andy Hung | 6ebe596 | 2016-01-15 17:46:57 -0800 | [diff] [blame] | 3650 | static int in_get_capture_position(const struct audio_stream_in *stream, |
| 3651 | int64_t *frames, int64_t *time) |
| 3652 | { |
| 3653 | if (stream == NULL || frames == NULL || time == NULL) { |
| 3654 | return -EINVAL; |
| 3655 | } |
| 3656 | struct stream_in *in = (struct stream_in *)stream; |
| 3657 | int ret = -ENOSYS; |
| 3658 | |
| 3659 | lock_input_stream(in); |
Haynes Mathew George | 7aa29e3 | 2017-12-11 18:10:36 -0800 | [diff] [blame] | 3660 | // note: ST sessions do not close the alsa pcm driver synchronously |
| 3661 | // on standby. Therefore, we may return an error even though the |
| 3662 | // pcm stream is still opened. |
| 3663 | if (in->standby) { |
| 3664 | ALOGE_IF(in->pcm != NULL && !in->is_st_session, |
| 3665 | "%s stream in standby but pcm not NULL for non ST session", __func__); |
| 3666 | goto exit; |
| 3667 | } |
Andy Hung | 6ebe596 | 2016-01-15 17:46:57 -0800 | [diff] [blame] | 3668 | if (in->pcm) { |
| 3669 | struct timespec timestamp; |
| 3670 | unsigned int avail; |
| 3671 | if (pcm_get_htimestamp(in->pcm, &avail, ×tamp) == 0) { |
| 3672 | *frames = in->frames_read + avail; |
| 3673 | *time = timestamp.tv_sec * 1000000000LL + timestamp.tv_nsec; |
| 3674 | ret = 0; |
| 3675 | } |
| 3676 | } |
Haynes Mathew George | 7aa29e3 | 2017-12-11 18:10:36 -0800 | [diff] [blame] | 3677 | exit: |
Andy Hung | 6ebe596 | 2016-01-15 17:46:57 -0800 | [diff] [blame] | 3678 | pthread_mutex_unlock(&in->lock); |
| 3679 | return ret; |
| 3680 | } |
| 3681 | |
Ravi Kumar Alamanda | f70ffb4 | 2013-04-16 15:55:53 -0700 | [diff] [blame] | 3682 | static int add_remove_audio_effect(const struct audio_stream *stream, |
| 3683 | effect_handle_t effect, |
| 3684 | bool enable) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3685 | { |
Ravi Kumar Alamanda | f70ffb4 | 2013-04-16 15:55:53 -0700 | [diff] [blame] | 3686 | struct stream_in *in = (struct stream_in *)stream; |
Eric Laurent | cefbbac | 2014-09-04 13:54:10 -0500 | [diff] [blame] | 3687 | struct audio_device *adev = in->dev; |
Ravi Kumar Alamanda | f70ffb4 | 2013-04-16 15:55:53 -0700 | [diff] [blame] | 3688 | int status = 0; |
| 3689 | effect_descriptor_t desc; |
| 3690 | |
| 3691 | status = (*effect)->get_descriptor(effect, &desc); |
| 3692 | if (status != 0) |
| 3693 | return status; |
| 3694 | |
Eric Laurent | a147807 | 2015-09-21 17:21:52 -0700 | [diff] [blame] | 3695 | lock_input_stream(in); |
Ravi Kumar Alamanda | f70ffb4 | 2013-04-16 15:55:53 -0700 | [diff] [blame] | 3696 | pthread_mutex_lock(&in->dev->lock); |
Eric Laurent | 50a38ed | 2015-10-14 18:48:06 -0700 | [diff] [blame] | 3697 | if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION || |
vivek mehta | 733c1df | 2016-04-04 15:09:24 -0700 | [diff] [blame] | 3698 | in->source == AUDIO_SOURCE_VOICE_RECOGNITION || |
Eric Laurent | 50a38ed | 2015-10-14 18:48:06 -0700 | [diff] [blame] | 3699 | adev->mode == AUDIO_MODE_IN_COMMUNICATION) && |
Ravi Kumar Alamanda | f70ffb4 | 2013-04-16 15:55:53 -0700 | [diff] [blame] | 3700 | in->enable_aec != enable && |
| 3701 | (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) { |
| 3702 | in->enable_aec = enable; |
Eric Laurent | cefbbac | 2014-09-04 13:54:10 -0500 | [diff] [blame] | 3703 | if (!enable) |
| 3704 | platform_set_echo_reference(in->dev, enable, AUDIO_DEVICE_NONE); |
vivek mehta | 733c1df | 2016-04-04 15:09:24 -0700 | [diff] [blame] | 3705 | if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION || |
| 3706 | adev->mode == AUDIO_MODE_IN_COMMUNICATION) { |
| 3707 | adev->enable_voicerx = enable; |
| 3708 | struct audio_usecase *usecase; |
| 3709 | struct listnode *node; |
| 3710 | list_for_each(node, &adev->usecase_list) { |
| 3711 | usecase = node_to_item(node, struct audio_usecase, list); |
vivek mehta | c07684b | 2017-09-15 13:31:12 -0700 | [diff] [blame] | 3712 | if (usecase->type == PCM_PLAYBACK) |
vivek mehta | 733c1df | 2016-04-04 15:09:24 -0700 | [diff] [blame] | 3713 | select_devices(adev, usecase->id); |
Eric Laurent | cefbbac | 2014-09-04 13:54:10 -0500 | [diff] [blame] | 3714 | } |
| 3715 | } |
Ravi Kumar Alamanda | f70ffb4 | 2013-04-16 15:55:53 -0700 | [diff] [blame] | 3716 | if (!in->standby) |
| 3717 | select_devices(in->dev, in->usecase); |
| 3718 | } |
Ravi Kumar Alamanda | 3ad4e1b | 2014-06-03 00:08:15 -0700 | [diff] [blame] | 3719 | if (in->enable_ns != enable && |
| 3720 | (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) { |
| 3721 | in->enable_ns = enable; |
| 3722 | if (!in->standby) |
| 3723 | select_devices(in->dev, in->usecase); |
| 3724 | } |
Ravi Kumar Alamanda | f70ffb4 | 2013-04-16 15:55:53 -0700 | [diff] [blame] | 3725 | pthread_mutex_unlock(&in->dev->lock); |
| 3726 | pthread_mutex_unlock(&in->lock); |
| 3727 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3728 | return 0; |
| 3729 | } |
| 3730 | |
Ravi Kumar Alamanda | f70ffb4 | 2013-04-16 15:55:53 -0700 | [diff] [blame] | 3731 | static int in_add_audio_effect(const struct audio_stream *stream, |
| 3732 | effect_handle_t effect) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3733 | { |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 3734 | ALOGV("%s: effect %p", __func__, effect); |
Ravi Kumar Alamanda | f70ffb4 | 2013-04-16 15:55:53 -0700 | [diff] [blame] | 3735 | return add_remove_audio_effect(stream, effect, true); |
| 3736 | } |
| 3737 | |
| 3738 | static int in_remove_audio_effect(const struct audio_stream *stream, |
| 3739 | effect_handle_t effect) |
| 3740 | { |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 3741 | ALOGV("%s: effect %p", __func__, effect); |
Ravi Kumar Alamanda | f70ffb4 | 2013-04-16 15:55:53 -0700 | [diff] [blame] | 3742 | return add_remove_audio_effect(stream, effect, false); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3743 | } |
| 3744 | |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3745 | static int in_stop(const struct audio_stream_in* stream) |
| 3746 | { |
| 3747 | struct stream_in *in = (struct stream_in *)stream; |
| 3748 | struct audio_device *adev = in->dev; |
| 3749 | |
| 3750 | int ret = -ENOSYS; |
| 3751 | ALOGV("%s", __func__); |
| 3752 | pthread_mutex_lock(&adev->lock); |
| 3753 | if (in->usecase == USECASE_AUDIO_RECORD_MMAP && !in->standby && |
| 3754 | in->capture_started && in->pcm != NULL) { |
| 3755 | pcm_stop(in->pcm); |
| 3756 | ret = stop_input_stream(in); |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 3757 | in->capture_started = false; |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3758 | } |
| 3759 | pthread_mutex_unlock(&adev->lock); |
| 3760 | return ret; |
| 3761 | } |
| 3762 | |
| 3763 | static int in_start(const struct audio_stream_in* stream) |
| 3764 | { |
| 3765 | struct stream_in *in = (struct stream_in *)stream; |
| 3766 | struct audio_device *adev = in->dev; |
| 3767 | int ret = -ENOSYS; |
| 3768 | |
| 3769 | ALOGV("%s in %p", __func__, in); |
| 3770 | pthread_mutex_lock(&adev->lock); |
| 3771 | if (in->usecase == USECASE_AUDIO_RECORD_MMAP && !in->standby && |
| 3772 | !in->capture_started && in->pcm != NULL) { |
| 3773 | if (!in->capture_started) { |
| 3774 | ret = start_input_stream(in); |
| 3775 | if (ret == 0) { |
| 3776 | in->capture_started = true; |
| 3777 | } |
| 3778 | } |
| 3779 | } |
| 3780 | pthread_mutex_unlock(&adev->lock); |
| 3781 | return ret; |
| 3782 | } |
| 3783 | |
| 3784 | static int in_create_mmap_buffer(const struct audio_stream_in *stream, |
| 3785 | int32_t min_size_frames, |
| 3786 | struct audio_mmap_buffer_info *info) |
| 3787 | { |
| 3788 | struct stream_in *in = (struct stream_in *)stream; |
| 3789 | struct audio_device *adev = in->dev; |
| 3790 | int ret = 0; |
| 3791 | unsigned int offset1; |
| 3792 | unsigned int frames1; |
| 3793 | const char *step = ""; |
Haynes Mathew George | 96483a2 | 2017-03-28 14:52:47 -0700 | [diff] [blame] | 3794 | uint32_t mmap_size; |
| 3795 | uint32_t buffer_size; |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3796 | |
Haynes Mathew George | f1aaf36 | 2017-12-18 15:10:10 -0800 | [diff] [blame] | 3797 | lock_input_stream(in); |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3798 | pthread_mutex_lock(&adev->lock); |
| 3799 | ALOGV("%s in %p", __func__, in); |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 3800 | |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3801 | if (info == NULL || min_size_frames == 0) { |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 3802 | 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] | 3803 | ret = -EINVAL; |
| 3804 | goto exit; |
| 3805 | } |
| 3806 | if (in->usecase != USECASE_AUDIO_RECORD_MMAP || !in->standby) { |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 3807 | ALOGE("%s: usecase = %d, standby = %d", __func__, in->usecase, in->standby); |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3808 | ALOGV("%s in %p", __func__, in); |
| 3809 | ret = -ENOSYS; |
| 3810 | goto exit; |
| 3811 | } |
| 3812 | in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE); |
| 3813 | if (in->pcm_device_id < 0) { |
| 3814 | ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)", |
| 3815 | __func__, in->pcm_device_id, in->usecase); |
| 3816 | ret = -EINVAL; |
| 3817 | goto exit; |
| 3818 | } |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 3819 | |
| 3820 | adjust_mmap_period_count(&in->config, min_size_frames); |
| 3821 | |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3822 | ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d", |
| 3823 | __func__, adev->snd_card, in->pcm_device_id, in->config.channels); |
| 3824 | in->pcm = pcm_open(adev->snd_card, in->pcm_device_id, |
| 3825 | (PCM_IN | PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC), &in->config); |
| 3826 | if (in->pcm == NULL || !pcm_is_ready(in->pcm)) { |
| 3827 | step = "open"; |
| 3828 | ret = -ENODEV; |
| 3829 | goto exit; |
| 3830 | } |
| 3831 | |
| 3832 | ret = pcm_mmap_begin(in->pcm, &info->shared_memory_address, &offset1, &frames1); |
| 3833 | if (ret < 0) { |
| 3834 | step = "begin"; |
| 3835 | goto exit; |
| 3836 | } |
| 3837 | info->buffer_size_frames = pcm_get_buffer_size(in->pcm); |
Haynes Mathew George | 96483a2 | 2017-03-28 14:52:47 -0700 | [diff] [blame] | 3838 | buffer_size = pcm_frames_to_bytes(in->pcm, info->buffer_size_frames); |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3839 | info->burst_size_frames = in->config.period_size; |
Haynes Mathew George | 96483a2 | 2017-03-28 14:52:47 -0700 | [diff] [blame] | 3840 | ret = platform_get_mmap_data_fd(adev->platform, |
| 3841 | in->pcm_device_id, 1 /*capture*/, |
| 3842 | &info->shared_memory_fd, |
| 3843 | &mmap_size); |
| 3844 | if (ret < 0) { |
| 3845 | // Fall back to non exclusive mode |
| 3846 | info->shared_memory_fd = pcm_get_poll_fd(in->pcm); |
| 3847 | } else { |
| 3848 | if (mmap_size < buffer_size) { |
| 3849 | step = "mmap"; |
| 3850 | goto exit; |
| 3851 | } |
| 3852 | // FIXME: indicate exclusive mode support by returning a negative buffer size |
| 3853 | info->buffer_size_frames *= -1; |
| 3854 | } |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3855 | |
Haynes Mathew George | 96483a2 | 2017-03-28 14:52:47 -0700 | [diff] [blame] | 3856 | memset(info->shared_memory_address, 0, buffer_size); |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3857 | |
| 3858 | ret = pcm_mmap_commit(in->pcm, 0, MMAP_PERIOD_SIZE); |
| 3859 | if (ret < 0) { |
| 3860 | step = "commit"; |
| 3861 | goto exit; |
| 3862 | } |
| 3863 | |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 3864 | in->standby = false; |
| 3865 | ret = 0; |
| 3866 | |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3867 | ALOGV("%s: got mmap buffer address %p info->buffer_size_frames %d", |
| 3868 | __func__, info->shared_memory_address, info->buffer_size_frames); |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3869 | |
| 3870 | exit: |
| 3871 | if (ret != 0) { |
Phil Burk | bc99104 | 2017-02-24 08:06:44 -0800 | [diff] [blame] | 3872 | if (in->pcm == NULL) { |
| 3873 | ALOGE("%s: %s - %d", __func__, step, ret); |
| 3874 | } else { |
| 3875 | ALOGE("%s: %s %s", __func__, step, pcm_get_error(in->pcm)); |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3876 | pcm_close(in->pcm); |
| 3877 | in->pcm = NULL; |
| 3878 | } |
| 3879 | } |
| 3880 | pthread_mutex_unlock(&adev->lock); |
Haynes Mathew George | f1aaf36 | 2017-12-18 15:10:10 -0800 | [diff] [blame] | 3881 | pthread_mutex_unlock(&in->lock); |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3882 | return ret; |
| 3883 | } |
| 3884 | |
| 3885 | static int in_get_mmap_position(const struct audio_stream_in *stream, |
| 3886 | struct audio_mmap_position *position) |
| 3887 | { |
Haynes Mathew George | f1aaf36 | 2017-12-18 15:10:10 -0800 | [diff] [blame] | 3888 | int ret = 0; |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3889 | struct stream_in *in = (struct stream_in *)stream; |
| 3890 | ALOGVV("%s", __func__); |
| 3891 | if (position == NULL) { |
| 3892 | return -EINVAL; |
| 3893 | } |
Haynes Mathew George | f1aaf36 | 2017-12-18 15:10:10 -0800 | [diff] [blame] | 3894 | lock_input_stream(in); |
| 3895 | if (in->usecase != USECASE_AUDIO_RECORD_MMAP || |
| 3896 | in->pcm == NULL) { |
| 3897 | ret = -ENOSYS; |
| 3898 | goto exit; |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3899 | } |
| 3900 | struct timespec ts = { 0, 0 }; |
Haynes Mathew George | f1aaf36 | 2017-12-18 15:10:10 -0800 | [diff] [blame] | 3901 | 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] | 3902 | if (ret < 0) { |
| 3903 | ALOGE("%s: %s", __func__, pcm_get_error(in->pcm)); |
Haynes Mathew George | f1aaf36 | 2017-12-18 15:10:10 -0800 | [diff] [blame] | 3904 | goto exit; |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3905 | } |
Andy Hung | fc044e1 | 2017-03-20 09:24:22 -0700 | [diff] [blame] | 3906 | position->time_nanoseconds = audio_utils_ns_from_timespec(&ts); |
Haynes Mathew George | f1aaf36 | 2017-12-18 15:10:10 -0800 | [diff] [blame] | 3907 | exit: |
| 3908 | pthread_mutex_unlock(&in->lock); |
| 3909 | return ret; |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3910 | } |
| 3911 | |
jiabin | 8962a4d | 2018-03-19 18:21:24 -0700 | [diff] [blame] | 3912 | static int in_get_active_microphones(const struct audio_stream_in *stream, |
| 3913 | struct audio_microphone_characteristic_t *mic_array, |
| 3914 | size_t *mic_count) { |
| 3915 | struct stream_in *in = (struct stream_in *)stream; |
| 3916 | struct audio_device *adev = in->dev; |
| 3917 | ALOGVV("%s", __func__); |
| 3918 | |
| 3919 | lock_input_stream(in); |
| 3920 | pthread_mutex_lock(&adev->lock); |
| 3921 | int ret = platform_get_active_microphones(adev->platform, in->device, |
| 3922 | audio_channel_count_from_in_mask(in->channel_mask), |
| 3923 | in->source, in->usecase, mic_array, mic_count); |
| 3924 | pthread_mutex_unlock(&adev->lock); |
| 3925 | pthread_mutex_unlock(&in->lock); |
| 3926 | |
| 3927 | return ret; |
| 3928 | } |
| 3929 | |
| 3930 | static int adev_get_microphones(const struct audio_hw_device *dev, |
| 3931 | struct audio_microphone_characteristic_t *mic_array, |
| 3932 | size_t *mic_count) { |
| 3933 | struct audio_device *adev = (struct audio_device *)dev; |
| 3934 | ALOGVV("%s", __func__); |
| 3935 | |
| 3936 | pthread_mutex_lock(&adev->lock); |
| 3937 | int ret = platform_get_microphones(adev->platform, mic_array, mic_count); |
| 3938 | pthread_mutex_unlock(&adev->lock); |
| 3939 | |
| 3940 | return ret; |
| 3941 | } |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 3942 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3943 | static int adev_open_output_stream(struct audio_hw_device *dev, |
| 3944 | audio_io_handle_t handle, |
| 3945 | audio_devices_t devices, |
| 3946 | audio_output_flags_t flags, |
| 3947 | struct audio_config *config, |
Eric Laurent | 91975a6 | 2014-07-27 17:15:50 -0700 | [diff] [blame] | 3948 | struct audio_stream_out **stream_out, |
| 3949 | const char *address __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3950 | { |
| 3951 | struct audio_device *adev = (struct audio_device *)dev; |
| 3952 | struct stream_out *out; |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 3953 | int i, ret = 0; |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 3954 | bool is_hdmi = devices & AUDIO_DEVICE_OUT_AUX_DIGITAL; |
| 3955 | bool is_usb_dev = audio_is_usb_out_device(devices) && |
| 3956 | (devices != AUDIO_DEVICE_OUT_USB_ACCESSORY); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3957 | |
Andy Hung | d9653bd | 2017-08-01 19:31:39 -0700 | [diff] [blame] | 3958 | if (is_usb_dev && !is_usb_ready(adev, true /* is_playback */)) { |
| 3959 | return -ENOSYS; |
| 3960 | } |
| 3961 | |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 3962 | ALOGV("%s: enter: format(%#x) sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)", |
| 3963 | __func__, config->format, config->sample_rate, config->channel_mask, devices, flags); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3964 | *stream_out = NULL; |
| 3965 | out = (struct stream_out *)calloc(1, sizeof(struct stream_out)); |
| 3966 | |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 3967 | pthread_mutex_init(&out->compr_mute_lock, (const pthread_mutexattr_t *) NULL); |
| 3968 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3969 | if (devices == AUDIO_DEVICE_NONE) |
| 3970 | devices = AUDIO_DEVICE_OUT_SPEAKER; |
| 3971 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3972 | out->flags = flags; |
| 3973 | out->devices = devices; |
Haynes Mathew George | 47cd4cb | 2013-07-19 11:58:50 -0700 | [diff] [blame] | 3974 | out->dev = adev; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 3975 | out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO; |
Eric Laurent | c4aef75 | 2013-09-12 17:45:53 -0700 | [diff] [blame] | 3976 | out->handle = handle; |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 3977 | out->a2dp_compress_mute = false; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3978 | |
| 3979 | /* Init use case and pcm_config */ |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 3980 | if ((is_hdmi || is_usb_dev) && |
| 3981 | (audio_is_linear_pcm(config->format) || config->format == AUDIO_FORMAT_DEFAULT) && |
| 3982 | (flags == AUDIO_OUTPUT_FLAG_NONE || |
| 3983 | (flags & AUDIO_OUTPUT_FLAG_DIRECT) != 0)) { |
Eric Laurent | ab805ee | 2018-03-30 12:20:38 -0700 | [diff] [blame] | 3984 | audio_format_t req_format = config->format; |
| 3985 | audio_channel_mask_t req_channel_mask = config->channel_mask; |
| 3986 | uint32_t req_sample_rate = config->sample_rate; |
| 3987 | |
Ravi Kumar Alamanda | b199506 | 2013-03-21 23:18:20 -0700 | [diff] [blame] | 3988 | pthread_mutex_lock(&adev->lock); |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 3989 | if (is_hdmi) { |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 3990 | ret = read_hdmi_channel_masks(out); |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 3991 | if (config->sample_rate == 0) |
| 3992 | config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE; |
Eric Laurent | ab805ee | 2018-03-30 12:20:38 -0700 | [diff] [blame] | 3993 | if (config->channel_mask == AUDIO_CHANNEL_NONE) |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 3994 | config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1; |
| 3995 | if (config->format == AUDIO_FORMAT_DEFAULT) |
| 3996 | config->format = AUDIO_FORMAT_PCM_16_BIT; |
| 3997 | } else if (is_usb_dev) { |
| 3998 | ret = read_usb_sup_params_and_compare(true /*is_playback*/, |
| 3999 | &config->format, |
| 4000 | &out->supported_formats[0], |
| 4001 | MAX_SUPPORTED_FORMATS, |
| 4002 | &config->channel_mask, |
| 4003 | &out->supported_channel_masks[0], |
| 4004 | MAX_SUPPORTED_CHANNEL_MASKS, |
| 4005 | &config->sample_rate, |
| 4006 | &out->supported_sample_rates[0], |
| 4007 | MAX_SUPPORTED_SAMPLE_RATES); |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 4008 | ALOGV("plugged dev USB ret %d", ret); |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 4009 | } |
Ravi Kumar Alamanda | b199506 | 2013-03-21 23:18:20 -0700 | [diff] [blame] | 4010 | pthread_mutex_unlock(&adev->lock); |
Eric Laurent | ab805ee | 2018-03-30 12:20:38 -0700 | [diff] [blame] | 4011 | if (ret != 0) { |
| 4012 | // For MMAP NO IRQ, allow conversions in ADSP |
| 4013 | if (is_hdmi || (flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) == 0) |
| 4014 | goto error_open; |
Ravi Kumar Alamanda | b199506 | 2013-03-21 23:18:20 -0700 | [diff] [blame] | 4015 | |
Eric Laurent | ab805ee | 2018-03-30 12:20:38 -0700 | [diff] [blame] | 4016 | if (req_sample_rate != 0 && config->sample_rate != req_sample_rate) |
| 4017 | config->sample_rate = req_sample_rate; |
| 4018 | if (req_channel_mask != AUDIO_CHANNEL_NONE && config->channel_mask != req_channel_mask) |
| 4019 | config->channel_mask = req_channel_mask; |
| 4020 | if (req_format != AUDIO_FORMAT_DEFAULT && config->format != req_format) |
| 4021 | config->format = req_format; |
| 4022 | } |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 4023 | |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 4024 | out->sample_rate = config->sample_rate; |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 4025 | out->channel_mask = config->channel_mask; |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 4026 | out->format = config->format; |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 4027 | if (is_hdmi) { |
| 4028 | out->usecase = USECASE_AUDIO_PLAYBACK_HIFI; |
| 4029 | out->config = pcm_config_hdmi_multi; |
| 4030 | } else if (flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) { |
| 4031 | out->usecase = USECASE_AUDIO_PLAYBACK_MMAP; |
| 4032 | out->config = pcm_config_mmap_playback; |
| 4033 | out->stream.start = out_start; |
| 4034 | out->stream.stop = out_stop; |
| 4035 | out->stream.create_mmap_buffer = out_create_mmap_buffer; |
| 4036 | out->stream.get_mmap_position = out_get_mmap_position; |
| 4037 | } else { |
| 4038 | out->usecase = USECASE_AUDIO_PLAYBACK_HIFI; |
| 4039 | out->config = pcm_config_hifi; |
| 4040 | } |
| 4041 | |
| 4042 | out->config.rate = out->sample_rate; |
Eric Laurent | 0de8d1f | 2014-07-01 20:34:45 -0700 | [diff] [blame] | 4043 | out->config.channels = audio_channel_count_from_out_mask(out->channel_mask); |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 4044 | if (is_hdmi) { |
| 4045 | out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * |
| 4046 | audio_bytes_per_sample(out->format)); |
| 4047 | } |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 4048 | out->config.format = pcm_format_from_audio_format(out->format); |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 4049 | } else if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { |
Haynes Mathew George | 3580769 | 2016-07-07 20:04:54 -0700 | [diff] [blame] | 4050 | pthread_mutex_lock(&adev->lock); |
| 4051 | bool offline = (adev->card_status == CARD_STATUS_OFFLINE); |
| 4052 | pthread_mutex_unlock(&adev->lock); |
| 4053 | |
| 4054 | // reject offload during card offline to allow |
| 4055 | // fallback to s/w paths |
| 4056 | if (offline) { |
| 4057 | ret = -ENODEV; |
| 4058 | goto error_open; |
| 4059 | } |
| 4060 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 4061 | if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version || |
| 4062 | config->offload_info.size != AUDIO_INFO_INITIALIZER.size) { |
| 4063 | ALOGE("%s: Unsupported Offload information", __func__); |
| 4064 | ret = -EINVAL; |
| 4065 | goto error_open; |
| 4066 | } |
| 4067 | if (!is_supported_format(config->offload_info.format)) { |
| 4068 | ALOGE("%s: Unsupported audio format", __func__); |
| 4069 | ret = -EINVAL; |
| 4070 | goto error_open; |
| 4071 | } |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 4072 | out->sample_rate = config->offload_info.sample_rate; |
| 4073 | if (config->offload_info.channel_mask != AUDIO_CHANNEL_NONE) |
| 4074 | out->channel_mask = config->offload_info.channel_mask; |
| 4075 | else if (config->channel_mask != AUDIO_CHANNEL_NONE) |
| 4076 | out->channel_mask = config->channel_mask; |
| 4077 | else |
| 4078 | out->channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 4079 | |
| 4080 | out->format = config->offload_info.format; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 4081 | |
| 4082 | out->compr_config.codec = (struct snd_codec *) |
| 4083 | calloc(1, sizeof(struct snd_codec)); |
| 4084 | |
| 4085 | out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 4086 | |
| 4087 | out->stream.set_callback = out_set_callback; |
| 4088 | out->stream.pause = out_pause; |
| 4089 | out->stream.resume = out_resume; |
| 4090 | out->stream.drain = out_drain; |
| 4091 | out->stream.flush = out_flush; |
| 4092 | |
| 4093 | out->compr_config.codec->id = |
| 4094 | get_snd_codec_id(config->offload_info.format); |
| 4095 | out->compr_config.fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE; |
| 4096 | out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS; |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 4097 | out->compr_config.codec->sample_rate = out->sample_rate; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 4098 | out->compr_config.codec->bit_rate = |
| 4099 | config->offload_info.bit_rate; |
| 4100 | out->compr_config.codec->ch_in = |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 4101 | audio_channel_count_from_out_mask(out->channel_mask); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 4102 | out->compr_config.codec->ch_out = out->compr_config.codec->ch_in; |
| 4103 | |
| 4104 | if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING) |
| 4105 | out->non_blocking = 1; |
Haynes Mathew George | 352f27b | 2013-07-26 00:00:15 -0700 | [diff] [blame] | 4106 | |
| 4107 | out->send_new_metadata = 1; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 4108 | create_offload_callback_thread(out); |
| 4109 | ALOGV("%s: offloaded output offload_info version %04x bit rate %d", |
| 4110 | __func__, config->offload_info.version, |
| 4111 | config->offload_info.bit_rate); |
Nadav Bar | 3d72cfc | 2018-01-07 12:19:24 +0200 | [diff] [blame] | 4112 | } else if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) { |
| 4113 | switch (config->sample_rate) { |
| 4114 | case 0: |
| 4115 | out->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE; |
| 4116 | break; |
| 4117 | case 8000: |
| 4118 | case 16000: |
| 4119 | case 48000: |
| 4120 | out->sample_rate = config->sample_rate; |
| 4121 | break; |
| 4122 | default: |
| 4123 | ALOGE("%s: Unsupported sampling rate %d for Incall Music", __func__, |
| 4124 | config->sample_rate); |
| 4125 | config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE; |
| 4126 | ret = -EINVAL; |
| 4127 | goto error_open; |
| 4128 | } |
| 4129 | //FIXME: add support for MONO stream configuration when audioflinger mixer supports it |
| 4130 | switch (config->channel_mask) { |
| 4131 | case AUDIO_CHANNEL_NONE: |
| 4132 | case AUDIO_CHANNEL_OUT_STEREO: |
| 4133 | out->channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 4134 | break; |
| 4135 | default: |
| 4136 | ALOGE("%s: Unsupported channel mask %#x for Incall Music", __func__, |
| 4137 | config->channel_mask); |
| 4138 | config->channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 4139 | ret = -EINVAL; |
| 4140 | goto error_open; |
| 4141 | } |
| 4142 | switch (config->format) { |
| 4143 | case AUDIO_FORMAT_DEFAULT: |
| 4144 | case AUDIO_FORMAT_PCM_16_BIT: |
| 4145 | out->format = AUDIO_FORMAT_PCM_16_BIT; |
| 4146 | break; |
| 4147 | default: |
| 4148 | ALOGE("%s: Unsupported format %#x for Incall Music", __func__, |
| 4149 | config->format); |
| 4150 | config->format = AUDIO_FORMAT_PCM_16_BIT; |
| 4151 | ret = -EINVAL; |
| 4152 | goto error_open; |
| 4153 | } |
| 4154 | |
| 4155 | voice_extn_check_and_set_incall_music_usecase(adev, out); |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 4156 | } else if (out->devices == AUDIO_DEVICE_OUT_TELEPHONY_TX) { |
Kevin Rocard | 50eff89 | 2017-05-02 16:52:05 -0700 | [diff] [blame] | 4157 | switch (config->sample_rate) { |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 4158 | case 0: |
| 4159 | out->sample_rate = AFE_PROXY_SAMPLING_RATE; |
| 4160 | break; |
Kevin Rocard | 50eff89 | 2017-05-02 16:52:05 -0700 | [diff] [blame] | 4161 | case 8000: |
| 4162 | case 16000: |
| 4163 | case 48000: |
| 4164 | out->sample_rate = config->sample_rate; |
| 4165 | break; |
| 4166 | default: |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 4167 | ALOGE("%s: Unsupported sampling rate %d for Telephony TX", __func__, |
| 4168 | config->sample_rate); |
| 4169 | config->sample_rate = AFE_PROXY_SAMPLING_RATE; |
| 4170 | ret = -EINVAL; |
| 4171 | break; |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 4172 | } |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 4173 | //FIXME: add support for MONO stream configuration when audioflinger mixer supports it |
| 4174 | switch (config->channel_mask) { |
| 4175 | case AUDIO_CHANNEL_NONE: |
| 4176 | out->channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 4177 | break; |
| 4178 | case AUDIO_CHANNEL_OUT_STEREO: |
| 4179 | out->channel_mask = config->channel_mask; |
| 4180 | break; |
| 4181 | default: |
| 4182 | ALOGE("%s: Unsupported channel mask %#x for Telephony TX", __func__, |
| 4183 | config->channel_mask); |
| 4184 | config->channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 4185 | ret = -EINVAL; |
| 4186 | break; |
| 4187 | } |
| 4188 | switch (config->format) { |
| 4189 | case AUDIO_FORMAT_DEFAULT: |
| 4190 | out->format = AUDIO_FORMAT_PCM_16_BIT; |
| 4191 | break; |
| 4192 | case AUDIO_FORMAT_PCM_16_BIT: |
| 4193 | out->format = config->format; |
| 4194 | break; |
| 4195 | default: |
| 4196 | ALOGE("%s: Unsupported format %#x for Telephony TX", __func__, |
| 4197 | config->format); |
| 4198 | config->format = AUDIO_FORMAT_PCM_16_BIT; |
| 4199 | ret = -EINVAL; |
| 4200 | break; |
| 4201 | } |
| 4202 | if (ret != 0) |
| 4203 | goto error_open; |
| 4204 | |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 4205 | out->usecase = USECASE_AUDIO_PLAYBACK_AFE_PROXY; |
| 4206 | out->config = pcm_config_afe_proxy_playback; |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 4207 | out->config.rate = out->sample_rate; |
| 4208 | out->config.channels = |
| 4209 | audio_channel_count_from_out_mask(out->channel_mask); |
| 4210 | out->config.format = pcm_format_from_audio_format(out->format); |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 4211 | adev->voice_tx_output = out; |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 4212 | } else if (flags == AUDIO_OUTPUT_FLAG_VOIP_RX) { |
| 4213 | switch (config->sample_rate) { |
| 4214 | case 0: |
| 4215 | out->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE; |
| 4216 | break; |
| 4217 | case 8000: |
| 4218 | case 16000: |
| 4219 | case 32000: |
| 4220 | case 48000: |
| 4221 | out->sample_rate = config->sample_rate; |
| 4222 | break; |
| 4223 | default: |
| 4224 | ALOGE("%s: Unsupported sampling rate %d for Voip RX", __func__, |
| 4225 | config->sample_rate); |
| 4226 | config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE; |
| 4227 | ret = -EINVAL; |
| 4228 | break; |
| 4229 | } |
Eric Laurent | ad2dde9 | 2017-09-20 18:27:31 -0700 | [diff] [blame] | 4230 | //FIXME: add support for MONO stream configuration when audioflinger mixer supports it |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 4231 | switch (config->channel_mask) { |
| 4232 | case AUDIO_CHANNEL_NONE: |
| 4233 | out->channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 4234 | break; |
| 4235 | case AUDIO_CHANNEL_OUT_STEREO: |
| 4236 | out->channel_mask = config->channel_mask; |
| 4237 | break; |
| 4238 | default: |
| 4239 | ALOGE("%s: Unsupported channel mask %#x for Voip RX", __func__, |
| 4240 | config->channel_mask); |
| 4241 | config->channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 4242 | ret = -EINVAL; |
| 4243 | break; |
| 4244 | } |
| 4245 | switch (config->format) { |
| 4246 | case AUDIO_FORMAT_DEFAULT: |
| 4247 | out->format = AUDIO_FORMAT_PCM_16_BIT; |
| 4248 | break; |
| 4249 | case AUDIO_FORMAT_PCM_16_BIT: |
| 4250 | out->format = config->format; |
| 4251 | break; |
| 4252 | default: |
| 4253 | ALOGE("%s: Unsupported format %#x for Voip RX", __func__, |
| 4254 | config->format); |
| 4255 | config->format = AUDIO_FORMAT_PCM_16_BIT; |
| 4256 | ret = -EINVAL; |
| 4257 | break; |
| 4258 | } |
| 4259 | if (ret != 0) |
| 4260 | goto error_open; |
| 4261 | |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 4262 | uint32_t buffer_size, frame_size; |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 4263 | out->usecase = USECASE_AUDIO_PLAYBACK_VOIP; |
| 4264 | out->config = pcm_config_voip; |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 4265 | out->config.rate = out->sample_rate; |
| 4266 | out->config.format = pcm_format_from_audio_format(out->format); |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 4267 | buffer_size = get_stream_buffer_size(VOIP_PLAYBACK_PERIOD_DURATION_MSEC, |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 4268 | out->sample_rate, |
| 4269 | out->format, |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 4270 | out->config.channels, |
| 4271 | false /*is_low_latency*/); |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 4272 | frame_size = audio_bytes_per_sample(out->format) * out->config.channels; |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 4273 | out->config.period_size = buffer_size / frame_size; |
| 4274 | out->config.period_count = VOIP_PLAYBACK_PERIOD_COUNT; |
| 4275 | out->af_period_multiplier = 1; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4276 | } else { |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 4277 | if (flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) { |
Andy Hung | 6fcba9c | 2014-03-18 11:53:32 -0700 | [diff] [blame] | 4278 | out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER; |
| 4279 | out->config = pcm_config_deep_buffer; |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 4280 | } else if (flags & AUDIO_OUTPUT_FLAG_TTS) { |
Ravi Kumar Alamanda | f78a4d9 | 2015-04-24 15:18:23 -0700 | [diff] [blame] | 4281 | out->usecase = USECASE_AUDIO_PLAYBACK_TTS; |
| 4282 | out->config = pcm_config_deep_buffer; |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 4283 | } else if (flags & AUDIO_OUTPUT_FLAG_RAW) { |
Ravi Kumar Alamanda | 2bc7b02 | 2015-06-25 20:08:01 -0700 | [diff] [blame] | 4284 | out->usecase = USECASE_AUDIO_PLAYBACK_ULL; |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 4285 | 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] | 4286 | out->config = out->realtime ? pcm_config_rt : pcm_config_low_latency; |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 4287 | } else if (flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) { |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 4288 | out->usecase = USECASE_AUDIO_PLAYBACK_MMAP; |
| 4289 | out->config = pcm_config_mmap_playback; |
| 4290 | out->stream.start = out_start; |
| 4291 | out->stream.stop = out_stop; |
| 4292 | out->stream.create_mmap_buffer = out_create_mmap_buffer; |
| 4293 | out->stream.get_mmap_position = out_get_mmap_position; |
Andy Hung | 6fcba9c | 2014-03-18 11:53:32 -0700 | [diff] [blame] | 4294 | } else { |
| 4295 | out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY; |
| 4296 | out->config = pcm_config_low_latency; |
| 4297 | } |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 4298 | |
| 4299 | if (config->sample_rate == 0) { |
| 4300 | out->sample_rate = out->config.rate; |
| 4301 | } else { |
| 4302 | out->sample_rate = config->sample_rate; |
Andy Hung | 6fcba9c | 2014-03-18 11:53:32 -0700 | [diff] [blame] | 4303 | } |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 4304 | if (config->channel_mask == AUDIO_CHANNEL_NONE) { |
| 4305 | out->channel_mask = audio_channel_out_mask_from_count(out->config.channels); |
| 4306 | } else { |
| 4307 | out->channel_mask = config->channel_mask; |
| 4308 | } |
| 4309 | if (config->format == AUDIO_FORMAT_DEFAULT) |
| 4310 | out->format = audio_format_from_pcm_format(out->config.format); |
| 4311 | else if (!audio_is_linear_pcm(config->format)) { |
| 4312 | config->format = AUDIO_FORMAT_PCM_16_BIT; |
| 4313 | ret = -EINVAL; |
| 4314 | goto error_open; |
| 4315 | } else { |
| 4316 | out->format = config->format; |
| 4317 | } |
| 4318 | |
| 4319 | out->config.rate = out->sample_rate; |
| 4320 | out->config.channels = |
| 4321 | audio_channel_count_from_out_mask(out->channel_mask); |
| 4322 | if (out->format != audio_format_from_pcm_format(out->config.format)) { |
| 4323 | out->config.format = pcm_format_from_audio_format(out->format); |
| 4324 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4325 | } |
Kevin Rocard | 50eff89 | 2017-05-02 16:52:05 -0700 | [diff] [blame] | 4326 | |
| 4327 | if ((config->sample_rate != 0 && config->sample_rate != out->sample_rate) || |
| 4328 | (config->format != AUDIO_FORMAT_DEFAULT && config->format != out->format) || |
Eric Laurent | 0dedc2e | 2017-08-27 14:35:46 -0700 | [diff] [blame] | 4329 | (config->channel_mask != AUDIO_CHANNEL_NONE && config->channel_mask != out->channel_mask)) { |
Kevin Rocard | 50eff89 | 2017-05-02 16:52:05 -0700 | [diff] [blame] | 4330 | ALOGI("%s: Unsupported output config. sample_rate:%u format:%#x channel_mask:%#x", |
| 4331 | __func__, config->sample_rate, config->format, config->channel_mask); |
| 4332 | config->sample_rate = out->sample_rate; |
| 4333 | config->format = out->format; |
| 4334 | config->channel_mask = out->channel_mask; |
| 4335 | ret = -EINVAL; |
| 4336 | goto error_open; |
| 4337 | } |
| 4338 | |
Andy Hung | 6fcba9c | 2014-03-18 11:53:32 -0700 | [diff] [blame] | 4339 | ALOGV("%s: Usecase(%s) config->format %#x out->config.format %#x\n", |
| 4340 | __func__, use_case_table[out->usecase], config->format, out->config.format); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4341 | |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 4342 | if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) { |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 4343 | if (adev->primary_output == NULL) |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 4344 | adev->primary_output = out; |
| 4345 | else { |
| 4346 | ALOGE("%s: Primary output is already opened", __func__); |
Ravi Kumar Alamanda | b199506 | 2013-03-21 23:18:20 -0700 | [diff] [blame] | 4347 | ret = -EEXIST; |
| 4348 | goto error_open; |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 4349 | } |
| 4350 | } |
| 4351 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4352 | /* Check if this usecase is already existing */ |
| 4353 | pthread_mutex_lock(&adev->lock); |
| 4354 | if (get_usecase_from_list(adev, out->usecase) != NULL) { |
| 4355 | ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4356 | pthread_mutex_unlock(&adev->lock); |
Ravi Kumar Alamanda | b199506 | 2013-03-21 23:18:20 -0700 | [diff] [blame] | 4357 | ret = -EEXIST; |
| 4358 | goto error_open; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4359 | } |
| 4360 | pthread_mutex_unlock(&adev->lock); |
| 4361 | |
| 4362 | out->stream.common.get_sample_rate = out_get_sample_rate; |
| 4363 | out->stream.common.set_sample_rate = out_set_sample_rate; |
| 4364 | out->stream.common.get_buffer_size = out_get_buffer_size; |
| 4365 | out->stream.common.get_channels = out_get_channels; |
| 4366 | out->stream.common.get_format = out_get_format; |
| 4367 | out->stream.common.set_format = out_set_format; |
| 4368 | out->stream.common.standby = out_standby; |
| 4369 | out->stream.common.dump = out_dump; |
| 4370 | out->stream.common.set_parameters = out_set_parameters; |
| 4371 | out->stream.common.get_parameters = out_get_parameters; |
| 4372 | out->stream.common.add_audio_effect = out_add_audio_effect; |
| 4373 | out->stream.common.remove_audio_effect = out_remove_audio_effect; |
| 4374 | out->stream.get_latency = out_get_latency; |
| 4375 | out->stream.set_volume = out_set_volume; |
Uday Kishore Pasupuleti | cec8ad8 | 2015-04-15 10:34:06 -0700 | [diff] [blame] | 4376 | #ifdef NO_AUDIO_OUT |
| 4377 | out->stream.write = out_write_for_no_output; |
| 4378 | #else |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4379 | out->stream.write = out_write; |
Uday Kishore Pasupuleti | cec8ad8 | 2015-04-15 10:34:06 -0700 | [diff] [blame] | 4380 | #endif |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4381 | out->stream.get_render_position = out_get_render_position; |
| 4382 | out->stream.get_next_write_timestamp = out_get_next_write_timestamp; |
Glenn Kasten | 2ccd7ba | 2013-09-10 09:04:31 -0700 | [diff] [blame] | 4383 | out->stream.get_presentation_position = out_get_presentation_position; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4384 | |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 4385 | if (out->realtime) |
| 4386 | out->af_period_multiplier = af_period_multiplier; |
| 4387 | else |
| 4388 | out->af_period_multiplier = 1; |
| 4389 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4390 | out->standby = 1; |
Eric Laurent | a9024de | 2013-04-04 09:19:12 -0700 | [diff] [blame] | 4391 | /* out->muted = false; by calloc() */ |
Glenn Kasten | 2ccd7ba | 2013-09-10 09:04:31 -0700 | [diff] [blame] | 4392 | /* out->written = 0; by calloc() */ |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4393 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 4394 | pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL); |
Eric Laurent | a147807 | 2015-09-21 17:21:52 -0700 | [diff] [blame] | 4395 | pthread_mutex_init(&out->pre_lock, (const pthread_mutexattr_t *) NULL); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 4396 | pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL); |
| 4397 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4398 | config->format = out->stream.common.get_format(&out->stream.common); |
| 4399 | config->channel_mask = out->stream.common.get_channels(&out->stream.common); |
| 4400 | config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common); |
| 4401 | |
Andy Hung | a452b0a | 2017-03-15 14:51:15 -0700 | [diff] [blame] | 4402 | out->error_log = error_log_create( |
| 4403 | ERROR_LOG_ENTRIES, |
| 4404 | 1000000000 /* aggregate consecutive identical errors within one second in ns */); |
| 4405 | |
Haynes Mathew George | c735fb0 | 2016-06-30 18:00:28 -0700 | [diff] [blame] | 4406 | /* |
| 4407 | By locking output stream before registering, we allow the callback |
| 4408 | to update stream's state only after stream's initial state is set to |
| 4409 | adev state. |
| 4410 | */ |
| 4411 | lock_output_stream(out); |
| 4412 | audio_extn_snd_mon_register_listener(out, out_snd_mon_cb); |
| 4413 | pthread_mutex_lock(&adev->lock); |
| 4414 | out->card_status = adev->card_status; |
| 4415 | pthread_mutex_unlock(&adev->lock); |
| 4416 | pthread_mutex_unlock(&out->lock); |
| 4417 | |
vivek mehta | 4a82477 | 2017-06-08 19:05:49 -0700 | [diff] [blame] | 4418 | stream_app_type_cfg_init(&out->app_type_cfg); |
| 4419 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4420 | *stream_out = &out->stream; |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 4421 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 4422 | ALOGV("%s: exit", __func__); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4423 | return 0; |
Ravi Kumar Alamanda | b199506 | 2013-03-21 23:18:20 -0700 | [diff] [blame] | 4424 | |
| 4425 | error_open: |
| 4426 | free(out); |
| 4427 | *stream_out = NULL; |
Eric Laurent | 2bafff1 | 2016-03-17 12:17:23 -0700 | [diff] [blame] | 4428 | ALOGW("%s: exit: ret %d", __func__, ret); |
Ravi Kumar Alamanda | b199506 | 2013-03-21 23:18:20 -0700 | [diff] [blame] | 4429 | return ret; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4430 | } |
| 4431 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 4432 | 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] | 4433 | struct audio_stream_out *stream) |
| 4434 | { |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 4435 | struct stream_out *out = (struct stream_out *)stream; |
| 4436 | struct audio_device *adev = out->dev; |
| 4437 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 4438 | ALOGV("%s: enter", __func__); |
Haynes Mathew George | c735fb0 | 2016-06-30 18:00:28 -0700 | [diff] [blame] | 4439 | |
| 4440 | // must deregister from sndmonitor first to prevent races |
| 4441 | // between the callback and close_stream |
| 4442 | audio_extn_snd_mon_unregister_listener(out); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4443 | out_standby(&stream->common); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 4444 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
| 4445 | destroy_offload_callback_thread(out); |
| 4446 | |
| 4447 | if (out->compr_config.codec != NULL) |
| 4448 | free(out->compr_config.codec); |
| 4449 | } |
Ravi Kumar Alamanda | a4fc902 | 2014-10-08 18:57:46 -0700 | [diff] [blame] | 4450 | |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 4451 | out->a2dp_compress_mute = false; |
| 4452 | |
Ravi Kumar Alamanda | a4fc902 | 2014-10-08 18:57:46 -0700 | [diff] [blame] | 4453 | if (adev->voice_tx_output == out) |
| 4454 | adev->voice_tx_output = NULL; |
| 4455 | |
Andy Hung | a452b0a | 2017-03-15 14:51:15 -0700 | [diff] [blame] | 4456 | error_log_destroy(out->error_log); |
| 4457 | out->error_log = NULL; |
| 4458 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 4459 | pthread_cond_destroy(&out->cond); |
Andy Hung | 0dbb52b | 2017-08-09 13:51:38 -0700 | [diff] [blame] | 4460 | pthread_mutex_destroy(&out->pre_lock); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 4461 | pthread_mutex_destroy(&out->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4462 | free(stream); |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 4463 | ALOGV("%s: exit", __func__); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4464 | } |
| 4465 | |
| 4466 | static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs) |
| 4467 | { |
| 4468 | struct audio_device *adev = (struct audio_device *)dev; |
| 4469 | struct str_parms *parms; |
| 4470 | char *str; |
| 4471 | char value[32]; |
Jean-Michel Trivi | c56336b | 2013-05-24 16:55:17 -0700 | [diff] [blame] | 4472 | int val; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4473 | int ret; |
Eric Laurent | 03f0943 | 2014-03-25 18:09:11 -0700 | [diff] [blame] | 4474 | int status = 0; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4475 | |
Joe Onorato | 188b622 | 2016-03-01 11:02:27 -0800 | [diff] [blame] | 4476 | ALOGV("%s: enter: %s", __func__, kvpairs); |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 4477 | |
| 4478 | pthread_mutex_lock(&adev->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4479 | |
| 4480 | parms = str_parms_create_str(kvpairs); |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 4481 | status = voice_set_parameters(adev, parms); |
| 4482 | if (status != 0) { |
| 4483 | goto done; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4484 | } |
| 4485 | |
| 4486 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value)); |
| 4487 | if (ret >= 0) { |
Ravi Kumar Alamanda | e258e68 | 2015-06-25 13:32:42 -0700 | [diff] [blame] | 4488 | /* When set to false, HAL should disable EC and NS */ |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4489 | if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0) |
| 4490 | adev->bluetooth_nrec = true; |
| 4491 | else |
| 4492 | adev->bluetooth_nrec = false; |
| 4493 | } |
| 4494 | |
| 4495 | ret = str_parms_get_str(parms, "screen_state", value, sizeof(value)); |
| 4496 | if (ret >= 0) { |
| 4497 | if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0) |
| 4498 | adev->screen_off = false; |
| 4499 | else |
| 4500 | adev->screen_off = true; |
| 4501 | } |
| 4502 | |
Jean-Michel Trivi | c56336b | 2013-05-24 16:55:17 -0700 | [diff] [blame] | 4503 | ret = str_parms_get_int(parms, "rotation", &val); |
| 4504 | if (ret >= 0) { |
| 4505 | bool reverse_speakers = false; |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 4506 | switch (val) { |
Jean-Michel Trivi | c56336b | 2013-05-24 16:55:17 -0700 | [diff] [blame] | 4507 | // FIXME: note that the code below assumes that the speakers are in the correct placement |
| 4508 | // relative to the user when the device is rotated 90deg from its default rotation. This |
| 4509 | // assumption is device-specific, not platform-specific like this code. |
| 4510 | case 270: |
| 4511 | reverse_speakers = true; |
| 4512 | break; |
| 4513 | case 0: |
| 4514 | case 90: |
| 4515 | case 180: |
| 4516 | break; |
| 4517 | default: |
| 4518 | ALOGE("%s: unexpected rotation of %d", __func__, val); |
Eric Laurent | 03f0943 | 2014-03-25 18:09:11 -0700 | [diff] [blame] | 4519 | status = -EINVAL; |
Jean-Michel Trivi | c56336b | 2013-05-24 16:55:17 -0700 | [diff] [blame] | 4520 | } |
Eric Laurent | 03f0943 | 2014-03-25 18:09:11 -0700 | [diff] [blame] | 4521 | if (status == 0) { |
vivek mehta | e59cfb2 | 2017-06-16 15:57:11 -0700 | [diff] [blame] | 4522 | // check and set swap |
| 4523 | // - check if orientation changed and speaker active |
| 4524 | // - set rotation and cache the rotation value |
| 4525 | platform_check_and_set_swap_lr_channels(adev, reverse_speakers); |
Jean-Michel Trivi | c56336b | 2013-05-24 16:55:17 -0700 | [diff] [blame] | 4526 | } |
Jean-Michel Trivi | c56336b | 2013-05-24 16:55:17 -0700 | [diff] [blame] | 4527 | } |
| 4528 | |
Ravi Kumar Alamanda | 9f30654 | 2014-04-02 15:11:49 -0700 | [diff] [blame] | 4529 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_SCO_WB, value, sizeof(value)); |
| 4530 | if (ret >= 0) { |
Ravi Kumar Alamanda | 9f30654 | 2014-04-02 15:11:49 -0700 | [diff] [blame] | 4531 | adev->bt_wb_speech_enabled = !strcmp(value, AUDIO_PARAMETER_VALUE_ON); |
Ravi Kumar Alamanda | 9f30654 | 2014-04-02 15:11:49 -0700 | [diff] [blame] | 4532 | } |
| 4533 | |
David Lin | ee3fe40 | 2017-03-13 10:00:42 -0700 | [diff] [blame] | 4534 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value, sizeof(value)); |
| 4535 | if (ret >= 0) { |
| 4536 | audio_devices_t device = (audio_devices_t)strtoul(value, NULL, 10); |
Eric Laurent | 99dab49 | 2017-06-17 15:19:08 -0700 | [diff] [blame] | 4537 | if (audio_is_usb_out_device(device)) { |
David Lin | ee3fe40 | 2017-03-13 10:00:42 -0700 | [diff] [blame] | 4538 | ret = str_parms_get_str(parms, "card", value, sizeof(value)); |
| 4539 | if (ret >= 0) { |
| 4540 | const int card = atoi(value); |
Eric Laurent | 99dab49 | 2017-06-17 15:19:08 -0700 | [diff] [blame] | 4541 | audio_extn_usb_add_device(device, card); |
David Lin | ee3fe40 | 2017-03-13 10:00:42 -0700 | [diff] [blame] | 4542 | } |
Eric Laurent | 99dab49 | 2017-06-17 15:19:08 -0700 | [diff] [blame] | 4543 | } else if (audio_is_usb_in_device(device)) { |
David Lin | ee3fe40 | 2017-03-13 10:00:42 -0700 | [diff] [blame] | 4544 | ret = str_parms_get_str(parms, "card", value, sizeof(value)); |
| 4545 | if (ret >= 0) { |
| 4546 | const int card = atoi(value); |
Eric Laurent | 99dab49 | 2017-06-17 15:19:08 -0700 | [diff] [blame] | 4547 | audio_extn_usb_add_device(device, card); |
David Lin | ee3fe40 | 2017-03-13 10:00:42 -0700 | [diff] [blame] | 4548 | } |
| 4549 | } |
| 4550 | } |
| 4551 | |
| 4552 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value, sizeof(value)); |
| 4553 | if (ret >= 0) { |
| 4554 | audio_devices_t device = (audio_devices_t)strtoul(value, NULL, 10); |
Eric Laurent | 99dab49 | 2017-06-17 15:19:08 -0700 | [diff] [blame] | 4555 | if (audio_is_usb_out_device(device)) { |
David Lin | ee3fe40 | 2017-03-13 10:00:42 -0700 | [diff] [blame] | 4556 | ret = str_parms_get_str(parms, "card", value, sizeof(value)); |
| 4557 | if (ret >= 0) { |
| 4558 | const int card = atoi(value); |
| 4559 | |
Eric Laurent | 99dab49 | 2017-06-17 15:19:08 -0700 | [diff] [blame] | 4560 | audio_extn_usb_remove_device(device, card); |
David Lin | ee3fe40 | 2017-03-13 10:00:42 -0700 | [diff] [blame] | 4561 | } |
Eric Laurent | 99dab49 | 2017-06-17 15:19:08 -0700 | [diff] [blame] | 4562 | } else if (audio_is_usb_in_device(device)) { |
David Lin | ee3fe40 | 2017-03-13 10:00:42 -0700 | [diff] [blame] | 4563 | ret = str_parms_get_str(parms, "card", value, sizeof(value)); |
| 4564 | if (ret >= 0) { |
| 4565 | const int card = atoi(value); |
Eric Laurent | 99dab49 | 2017-06-17 15:19:08 -0700 | [diff] [blame] | 4566 | audio_extn_usb_remove_device(device, card); |
David Lin | ee3fe40 | 2017-03-13 10:00:42 -0700 | [diff] [blame] | 4567 | } |
| 4568 | } |
| 4569 | } |
| 4570 | |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 4571 | audio_extn_hfp_set_parameters(adev, parms); |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 4572 | audio_extn_a2dp_set_parameters(parms); |
| 4573 | // reconfigure should be done only after updating A2DP state in audio extension |
| 4574 | ret = str_parms_get_str(parms,"reconfigA2dp", value, sizeof(value)); |
| 4575 | if (ret >= 0) { |
| 4576 | struct audio_usecase *usecase; |
| 4577 | struct listnode *node; |
| 4578 | list_for_each(node, &adev->usecase_list) { |
| 4579 | usecase = node_to_item(node, struct audio_usecase, list); |
| 4580 | if ((usecase->type == PCM_PLAYBACK) && |
| 4581 | (usecase->devices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP)) { |
| 4582 | ALOGD("%s: reconfigure A2DP... forcing device switch", __func__); |
| 4583 | |
| 4584 | pthread_mutex_unlock(&adev->lock); |
| 4585 | lock_output_stream(usecase->stream.out); |
| 4586 | pthread_mutex_lock(&adev->lock); |
| 4587 | audio_extn_a2dp_set_handoff_mode(true); |
| 4588 | // force device switch to reconfigure encoder |
| 4589 | select_devices(adev, usecase->id); |
| 4590 | audio_extn_a2dp_set_handoff_mode(false); |
| 4591 | pthread_mutex_unlock(&usecase->stream.out->lock); |
| 4592 | break; |
| 4593 | } |
| 4594 | } |
| 4595 | } |
| 4596 | |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 4597 | done: |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4598 | str_parms_destroy(parms); |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 4599 | pthread_mutex_unlock(&adev->lock); |
Eric Laurent | 03f0943 | 2014-03-25 18:09:11 -0700 | [diff] [blame] | 4600 | ALOGV("%s: exit with code(%d)", __func__, status); |
| 4601 | return status; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4602 | } |
| 4603 | |
| 4604 | static char* adev_get_parameters(const struct audio_hw_device *dev, |
| 4605 | const char *keys) |
| 4606 | { |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 4607 | struct audio_device *adev = (struct audio_device *)dev; |
| 4608 | struct str_parms *reply = str_parms_create(); |
| 4609 | struct str_parms *query = str_parms_create_str(keys); |
| 4610 | char *str; |
| 4611 | |
| 4612 | pthread_mutex_lock(&adev->lock); |
| 4613 | |
| 4614 | voice_get_parameters(adev, query, reply); |
| 4615 | str = str_parms_to_str(reply); |
| 4616 | str_parms_destroy(query); |
| 4617 | str_parms_destroy(reply); |
| 4618 | |
| 4619 | pthread_mutex_unlock(&adev->lock); |
| 4620 | ALOGV("%s: exit: returns - %s", __func__, str); |
| 4621 | return str; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4622 | } |
| 4623 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 4624 | 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] | 4625 | { |
| 4626 | return 0; |
| 4627 | } |
| 4628 | |
Haynes Mathew George | 5191a85 | 2013-09-11 14:19:36 -0700 | [diff] [blame] | 4629 | static int adev_set_voice_volume(struct audio_hw_device *dev, float volume) |
| 4630 | { |
| 4631 | int ret; |
| 4632 | struct audio_device *adev = (struct audio_device *)dev; |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 4633 | |
Eric Laurent | 4cc4ce1 | 2014-09-10 13:21:01 -0500 | [diff] [blame] | 4634 | audio_extn_extspk_set_voice_vol(adev->extspk, volume); |
| 4635 | |
Haynes Mathew George | 5191a85 | 2013-09-11 14:19:36 -0700 | [diff] [blame] | 4636 | pthread_mutex_lock(&adev->lock); |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 4637 | ret = voice_set_volume(adev, volume); |
Haynes Mathew George | 5191a85 | 2013-09-11 14:19:36 -0700 | [diff] [blame] | 4638 | pthread_mutex_unlock(&adev->lock); |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 4639 | |
Haynes Mathew George | 5191a85 | 2013-09-11 14:19:36 -0700 | [diff] [blame] | 4640 | return ret; |
| 4641 | } |
| 4642 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 4643 | 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] | 4644 | { |
| 4645 | return -ENOSYS; |
| 4646 | } |
| 4647 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 4648 | static int adev_get_master_volume(struct audio_hw_device *dev __unused, |
| 4649 | float *volume __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4650 | { |
| 4651 | return -ENOSYS; |
| 4652 | } |
| 4653 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 4654 | 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] | 4655 | { |
| 4656 | return -ENOSYS; |
| 4657 | } |
| 4658 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 4659 | 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] | 4660 | { |
| 4661 | return -ENOSYS; |
| 4662 | } |
| 4663 | |
| 4664 | static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode) |
| 4665 | { |
| 4666 | struct audio_device *adev = (struct audio_device *)dev; |
| 4667 | |
| 4668 | pthread_mutex_lock(&adev->lock); |
| 4669 | if (adev->mode != mode) { |
Eric Laurent | 2bafff1 | 2016-03-17 12:17:23 -0700 | [diff] [blame] | 4670 | ALOGD("%s: mode %d", __func__, (int)mode); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4671 | adev->mode = mode; |
Ravi Kumar Alamanda | 36886fc | 2014-09-29 13:41:51 -0700 | [diff] [blame] | 4672 | if ((mode == AUDIO_MODE_NORMAL || mode == AUDIO_MODE_IN_COMMUNICATION) && |
| 4673 | voice_is_in_call(adev)) { |
| 4674 | voice_stop_call(adev); |
| 4675 | adev->current_call_output = NULL; |
| 4676 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4677 | } |
| 4678 | pthread_mutex_unlock(&adev->lock); |
Eric Laurent | 0499d4f | 2014-08-25 22:39:29 -0500 | [diff] [blame] | 4679 | |
| 4680 | audio_extn_extspk_set_mode(adev->extspk, mode); |
| 4681 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4682 | return 0; |
| 4683 | } |
| 4684 | |
| 4685 | static int adev_set_mic_mute(struct audio_hw_device *dev, bool state) |
| 4686 | { |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 4687 | int ret; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4688 | struct audio_device *adev = (struct audio_device *)dev; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4689 | |
Eric Laurent | 2bafff1 | 2016-03-17 12:17:23 -0700 | [diff] [blame] | 4690 | ALOGD("%s: state %d", __func__, (int)state); |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 4691 | pthread_mutex_lock(&adev->lock); |
Alain Vongsouvanh | 13f26e8 | 2016-11-18 14:39:11 -0800 | [diff] [blame] | 4692 | if (audio_extn_tfa_98xx_is_supported() && adev->enable_hfp) { |
| 4693 | ret = audio_extn_hfp_set_mic_mute(adev, state); |
| 4694 | } else { |
| 4695 | ret = voice_set_mic_mute(adev, state); |
| 4696 | } |
Eric Laurent | 7b2b5ab | 2014-09-14 12:29:59 -0700 | [diff] [blame] | 4697 | adev->mic_muted = state; |
Eric Laurent | a609e8e | 2014-06-18 02:15:17 +0000 | [diff] [blame] | 4698 | pthread_mutex_unlock(&adev->lock); |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 4699 | |
| 4700 | return ret; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4701 | } |
| 4702 | |
| 4703 | static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state) |
| 4704 | { |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 4705 | *state = voice_get_mic_mute((struct audio_device *)dev); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4706 | return 0; |
| 4707 | } |
| 4708 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 4709 | 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] | 4710 | const struct audio_config *config) |
| 4711 | { |
Eric Laurent | 0de8d1f | 2014-07-01 20:34:45 -0700 | [diff] [blame] | 4712 | 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] | 4713 | |
Eric Laurent | 74b5576 | 2017-07-09 17:04:53 -0700 | [diff] [blame] | 4714 | /* Don't know if USB HIFI in this context so use true to be conservative */ |
| 4715 | if (check_input_parameters(config->sample_rate, config->format, channel_count, |
| 4716 | true /*is_usb_hifi */) != 0) |
| 4717 | return 0; |
| 4718 | |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 4719 | return get_stream_buffer_size(AUDIO_CAPTURE_PERIOD_DURATION_MSEC, |
| 4720 | config->sample_rate, config->format, |
| 4721 | channel_count, |
| 4722 | false /* is_low_latency: since we don't know, be conservative */); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4723 | } |
| 4724 | |
Haynes Mathew George | e95340e | 2017-05-24 15:42:06 -0700 | [diff] [blame] | 4725 | static bool adev_input_allow_hifi_record(struct audio_device *adev, |
| 4726 | audio_devices_t devices, |
| 4727 | audio_input_flags_t flags, |
| 4728 | audio_source_t source) { |
| 4729 | const bool allowed = true; |
| 4730 | |
| 4731 | if (!audio_is_usb_in_device(devices)) |
| 4732 | return !allowed; |
| 4733 | |
| 4734 | switch (flags) { |
| 4735 | case AUDIO_INPUT_FLAG_NONE: |
| 4736 | case AUDIO_INPUT_FLAG_FAST: // just fast, not fast|raw || fast|mmap |
| 4737 | break; |
| 4738 | default: |
| 4739 | return !allowed; |
| 4740 | } |
| 4741 | |
| 4742 | switch (source) { |
| 4743 | case AUDIO_SOURCE_DEFAULT: |
| 4744 | case AUDIO_SOURCE_MIC: |
| 4745 | case AUDIO_SOURCE_UNPROCESSED: |
| 4746 | break; |
| 4747 | default: |
| 4748 | return !allowed; |
| 4749 | } |
| 4750 | |
| 4751 | switch (adev->mode) { |
| 4752 | case 0: |
| 4753 | break; |
| 4754 | default: |
| 4755 | return !allowed; |
| 4756 | } |
| 4757 | |
| 4758 | return allowed; |
| 4759 | } |
| 4760 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4761 | static int adev_open_input_stream(struct audio_hw_device *dev, |
Ravi Kumar Alamanda | a417cc5 | 2015-05-01 16:41:56 -0700 | [diff] [blame] | 4762 | audio_io_handle_t handle, |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4763 | audio_devices_t devices, |
| 4764 | struct audio_config *config, |
Glenn Kasten | 68e79ce | 2014-07-15 10:56:59 -0700 | [diff] [blame] | 4765 | struct audio_stream_in **stream_in, |
Eric Laurent | 91975a6 | 2014-07-27 17:15:50 -0700 | [diff] [blame] | 4766 | audio_input_flags_t flags, |
| 4767 | const char *address __unused, |
Eric Laurent | cefbbac | 2014-09-04 13:54:10 -0500 | [diff] [blame] | 4768 | audio_source_t source ) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4769 | { |
| 4770 | struct audio_device *adev = (struct audio_device *)dev; |
| 4771 | struct stream_in *in; |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 4772 | int ret = 0, buffer_size, frame_size; |
Eric Laurent | 74b5576 | 2017-07-09 17:04:53 -0700 | [diff] [blame] | 4773 | int channel_count; |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 4774 | bool is_low_latency = false; |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 4775 | bool is_usb_dev = audio_is_usb_in_device(devices); |
Haynes Mathew George | e95340e | 2017-05-24 15:42:06 -0700 | [diff] [blame] | 4776 | bool may_use_hifi_record = adev_input_allow_hifi_record(adev, |
| 4777 | devices, |
| 4778 | flags, |
| 4779 | source); |
Eric Laurent | 74b5576 | 2017-07-09 17:04:53 -0700 | [diff] [blame] | 4780 | ALOGV("%s: enter", __func__); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4781 | *stream_in = NULL; |
Haynes Mathew George | e95340e | 2017-05-24 15:42:06 -0700 | [diff] [blame] | 4782 | |
Andy Hung | d9653bd | 2017-08-01 19:31:39 -0700 | [diff] [blame] | 4783 | if (is_usb_dev && !is_usb_ready(adev, false /* is_playback */)) { |
| 4784 | return -ENOSYS; |
| 4785 | } |
| 4786 | |
Eric Laurent | 74b5576 | 2017-07-09 17:04:53 -0700 | [diff] [blame] | 4787 | if (!(is_usb_dev && may_use_hifi_record)) { |
| 4788 | if (config->sample_rate == 0) |
| 4789 | config->sample_rate = DEFAULT_INPUT_SAMPLING_RATE; |
| 4790 | if (config->channel_mask == AUDIO_CHANNEL_NONE) |
| 4791 | config->channel_mask = AUDIO_CHANNEL_IN_MONO; |
| 4792 | if (config->format == AUDIO_FORMAT_DEFAULT) |
| 4793 | config->format = AUDIO_FORMAT_PCM_16_BIT; |
Haynes Mathew George | e95340e | 2017-05-24 15:42:06 -0700 | [diff] [blame] | 4794 | |
Eric Laurent | 74b5576 | 2017-07-09 17:04:53 -0700 | [diff] [blame] | 4795 | channel_count = audio_channel_count_from_in_mask(config->channel_mask); |
| 4796 | |
| 4797 | if (check_input_parameters(config->sample_rate, config->format, channel_count, false) != 0) |
| 4798 | return -EINVAL; |
| 4799 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4800 | |
Haynes Mathew George | e95340e | 2017-05-24 15:42:06 -0700 | [diff] [blame] | 4801 | if (audio_extn_tfa_98xx_is_supported() && |
| 4802 | (audio_extn_hfp_is_active(adev) || voice_is_in_call(adev))) |
Alain Vongsouvanh | 13f26e8 | 2016-11-18 14:39:11 -0800 | [diff] [blame] | 4803 | return -EINVAL; |
| 4804 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4805 | in = (struct stream_in *)calloc(1, sizeof(struct stream_in)); |
| 4806 | |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 4807 | pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL); |
Eric Laurent | a147807 | 2015-09-21 17:21:52 -0700 | [diff] [blame] | 4808 | pthread_mutex_init(&in->pre_lock, (const pthread_mutexattr_t *) NULL); |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 4809 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4810 | in->stream.common.get_sample_rate = in_get_sample_rate; |
| 4811 | in->stream.common.set_sample_rate = in_set_sample_rate; |
| 4812 | in->stream.common.get_buffer_size = in_get_buffer_size; |
| 4813 | in->stream.common.get_channels = in_get_channels; |
| 4814 | in->stream.common.get_format = in_get_format; |
| 4815 | in->stream.common.set_format = in_set_format; |
| 4816 | in->stream.common.standby = in_standby; |
| 4817 | in->stream.common.dump = in_dump; |
| 4818 | in->stream.common.set_parameters = in_set_parameters; |
| 4819 | in->stream.common.get_parameters = in_get_parameters; |
| 4820 | in->stream.common.add_audio_effect = in_add_audio_effect; |
| 4821 | in->stream.common.remove_audio_effect = in_remove_audio_effect; |
| 4822 | in->stream.set_gain = in_set_gain; |
| 4823 | in->stream.read = in_read; |
| 4824 | in->stream.get_input_frames_lost = in_get_input_frames_lost; |
Andy Hung | 6ebe596 | 2016-01-15 17:46:57 -0800 | [diff] [blame] | 4825 | in->stream.get_capture_position = in_get_capture_position; |
jiabin | 8962a4d | 2018-03-19 18:21:24 -0700 | [diff] [blame] | 4826 | in->stream.get_active_microphones = in_get_active_microphones; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4827 | |
| 4828 | in->device = devices; |
Eric Laurent | cefbbac | 2014-09-04 13:54:10 -0500 | [diff] [blame] | 4829 | in->source = source; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4830 | in->dev = adev; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4831 | in->standby = 1; |
Ravi Kumar Alamanda | a417cc5 | 2015-05-01 16:41:56 -0700 | [diff] [blame] | 4832 | in->capture_handle = handle; |
Haynes Mathew George | 88e6fb2 | 2015-08-19 11:51:34 -0700 | [diff] [blame] | 4833 | in->flags = flags; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4834 | |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 4835 | if (is_usb_dev && may_use_hifi_record) { |
| 4836 | /* HiFi record selects an appropriate format, channel, rate combo |
| 4837 | depending on sink capabilities*/ |
| 4838 | ret = read_usb_sup_params_and_compare(false /*is_playback*/, |
| 4839 | &config->format, |
| 4840 | &in->supported_formats[0], |
| 4841 | MAX_SUPPORTED_FORMATS, |
| 4842 | &config->channel_mask, |
| 4843 | &in->supported_channel_masks[0], |
| 4844 | MAX_SUPPORTED_CHANNEL_MASKS, |
| 4845 | &config->sample_rate, |
| 4846 | &in->supported_sample_rates[0], |
| 4847 | MAX_SUPPORTED_SAMPLE_RATES); |
| 4848 | if (ret != 0) { |
| 4849 | ret = -EINVAL; |
| 4850 | goto err_open; |
| 4851 | } |
Eric Laurent | 74b5576 | 2017-07-09 17:04:53 -0700 | [diff] [blame] | 4852 | channel_count = audio_channel_count_from_in_mask(config->channel_mask); |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 4853 | } else if (config->format == AUDIO_FORMAT_DEFAULT) { |
vivek mehta | 4ed66e6 | 2016-04-15 23:33:34 -0700 | [diff] [blame] | 4854 | config->format = AUDIO_FORMAT_PCM_16_BIT; |
vivek mehta | 57ff9b5 | 2016-04-28 14:13:08 -0700 | [diff] [blame] | 4855 | } else if (config->format == AUDIO_FORMAT_PCM_FLOAT || |
| 4856 | config->format == AUDIO_FORMAT_PCM_24_BIT_PACKED || |
| 4857 | config->format == AUDIO_FORMAT_PCM_8_24_BIT) { |
| 4858 | bool ret_error = false; |
| 4859 | /* 24 bit is restricted to UNPROCESSED source only,also format supported |
| 4860 | from HAL is 8_24 |
| 4861 | *> In case of UNPROCESSED source, for 24 bit, if format requested is other than |
| 4862 | 8_24 return error indicating supported format is 8_24 |
| 4863 | *> In case of any other source requesting 24 bit or float return error |
| 4864 | indicating format supported is 16 bit only. |
vivek mehta | 4ed66e6 | 2016-04-15 23:33:34 -0700 | [diff] [blame] | 4865 | |
vivek mehta | 57ff9b5 | 2016-04-28 14:13:08 -0700 | [diff] [blame] | 4866 | on error flinger will retry with supported format passed |
| 4867 | */ |
| 4868 | if (source != AUDIO_SOURCE_UNPROCESSED) { |
| 4869 | config->format = AUDIO_FORMAT_PCM_16_BIT; |
| 4870 | ret_error = true; |
| 4871 | } else if (config->format != AUDIO_FORMAT_PCM_8_24_BIT) { |
| 4872 | config->format = AUDIO_FORMAT_PCM_8_24_BIT; |
| 4873 | ret_error = true; |
| 4874 | } |
| 4875 | |
| 4876 | if (ret_error) { |
| 4877 | ret = -EINVAL; |
| 4878 | goto err_open; |
| 4879 | } |
vivek mehta | 4ed66e6 | 2016-04-15 23:33:34 -0700 | [diff] [blame] | 4880 | } |
| 4881 | |
vivek mehta | 57ff9b5 | 2016-04-28 14:13:08 -0700 | [diff] [blame] | 4882 | in->format = config->format; |
Eric Laurent | 74b5576 | 2017-07-09 17:04:53 -0700 | [diff] [blame] | 4883 | in->channel_mask = config->channel_mask; |
vivek mehta | 57ff9b5 | 2016-04-28 14:13:08 -0700 | [diff] [blame] | 4884 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4885 | /* Update config params with the requested sample rate and channels */ |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 4886 | if (in->device == AUDIO_DEVICE_IN_TELEPHONY_RX) { |
| 4887 | if (config->sample_rate == 0) |
| 4888 | config->sample_rate = AFE_PROXY_SAMPLING_RATE; |
| 4889 | if (config->sample_rate != 48000 && config->sample_rate != 16000 && |
| 4890 | config->sample_rate != 8000) { |
| 4891 | config->sample_rate = AFE_PROXY_SAMPLING_RATE; |
| 4892 | ret = -EINVAL; |
| 4893 | goto err_open; |
| 4894 | } |
vivek mehta | 4ed66e6 | 2016-04-15 23:33:34 -0700 | [diff] [blame] | 4895 | |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 4896 | if (config->format != AUDIO_FORMAT_PCM_16_BIT) { |
| 4897 | config->format = AUDIO_FORMAT_PCM_16_BIT; |
| 4898 | ret = -EINVAL; |
| 4899 | goto err_open; |
| 4900 | } |
| 4901 | |
| 4902 | in->usecase = USECASE_AUDIO_RECORD_AFE_PROXY; |
| 4903 | in->config = pcm_config_afe_proxy_record; |
David Lin | e5b12ac | 2017-03-29 13:37:33 -0700 | [diff] [blame] | 4904 | in->af_period_multiplier = 1; |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 4905 | } else if (is_usb_dev && may_use_hifi_record) { |
| 4906 | in->usecase = USECASE_AUDIO_RECORD_HIFI; |
| 4907 | in->config = pcm_config_audio_capture; |
| 4908 | frame_size = audio_stream_in_frame_size(&in->stream); |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 4909 | buffer_size = get_stream_buffer_size(AUDIO_CAPTURE_PERIOD_DURATION_MSEC, |
| 4910 | config->sample_rate, |
| 4911 | config->format, |
| 4912 | channel_count, |
| 4913 | false /*is_low_latency*/); |
Haynes Mathew George | 569b748 | 2017-05-08 14:44:27 -0700 | [diff] [blame] | 4914 | in->config.period_size = buffer_size / frame_size; |
| 4915 | in->config.rate = config->sample_rate; |
| 4916 | in->af_period_multiplier = 1; |
| 4917 | in->config.format = pcm_format_from_audio_format(config->format); |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 4918 | } else { |
| 4919 | in->usecase = USECASE_AUDIO_RECORD; |
| 4920 | if (config->sample_rate == LOW_LATENCY_CAPTURE_SAMPLE_RATE && |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 4921 | (in->flags & AUDIO_INPUT_FLAG_FAST) != 0) { |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 4922 | is_low_latency = true; |
Glenn Kasten | 4f99339 | 2014-05-14 07:30:48 -0700 | [diff] [blame] | 4923 | #if LOW_LATENCY_CAPTURE_USE_CASE |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 4924 | in->usecase = USECASE_AUDIO_RECORD_LOW_LATENCY; |
Glenn Kasten | 4f99339 | 2014-05-14 07:30:48 -0700 | [diff] [blame] | 4925 | #endif |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 4926 | in->realtime = may_use_noirq_mode(adev, in->usecase, in->flags); |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 4927 | if (!in->realtime) { |
Eric Laurent | ff1e5e6 | 2017-02-21 16:08:55 -0800 | [diff] [blame] | 4928 | in->config = pcm_config_audio_capture; |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 4929 | frame_size = audio_stream_in_frame_size(&in->stream); |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 4930 | buffer_size = get_stream_buffer_size(AUDIO_CAPTURE_PERIOD_DURATION_MSEC, |
| 4931 | config->sample_rate, |
| 4932 | config->format, |
| 4933 | channel_count, |
| 4934 | is_low_latency); |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 4935 | in->config.period_size = buffer_size / frame_size; |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 4936 | in->config.rate = config->sample_rate; |
| 4937 | in->af_period_multiplier = 1; |
| 4938 | } else { |
| 4939 | // period size is left untouched for rt mode playback |
| 4940 | in->config = pcm_config_audio_capture_rt; |
| 4941 | in->af_period_multiplier = af_period_multiplier; |
| 4942 | } |
| 4943 | } else if ((config->sample_rate == LOW_LATENCY_CAPTURE_SAMPLE_RATE) && |
| 4944 | ((in->flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0)) { |
Haynes Mathew George | e95340e | 2017-05-24 15:42:06 -0700 | [diff] [blame] | 4945 | // FIXME: Add support for multichannel capture over USB using MMAP |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 4946 | in->usecase = USECASE_AUDIO_RECORD_MMAP; |
| 4947 | in->config = pcm_config_mmap_capture; |
| 4948 | in->stream.start = in_start; |
| 4949 | in->stream.stop = in_stop; |
| 4950 | in->stream.create_mmap_buffer = in_create_mmap_buffer; |
| 4951 | in->stream.get_mmap_position = in_get_mmap_position; |
| 4952 | in->af_period_multiplier = 1; |
| 4953 | ALOGV("%s: USECASE_AUDIO_RECORD_MMAP", __func__); |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 4954 | } else if (in->source == AUDIO_SOURCE_VOICE_COMMUNICATION && |
vivek mehta | 4a82477 | 2017-06-08 19:05:49 -0700 | [diff] [blame] | 4955 | in->flags & AUDIO_INPUT_FLAG_VOIP_TX && |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 4956 | (config->sample_rate == 8000 || |
| 4957 | config->sample_rate == 16000 || |
| 4958 | config->sample_rate == 32000 || |
| 4959 | config->sample_rate == 48000) && |
| 4960 | channel_count == 1) { |
| 4961 | in->usecase = USECASE_AUDIO_RECORD_VOIP; |
| 4962 | in->config = pcm_config_audio_capture; |
| 4963 | frame_size = audio_stream_in_frame_size(&in->stream); |
| 4964 | buffer_size = get_stream_buffer_size(VOIP_CAPTURE_PERIOD_DURATION_MSEC, |
| 4965 | config->sample_rate, |
| 4966 | config->format, |
| 4967 | channel_count, false /*is_low_latency*/); |
| 4968 | in->config.period_size = buffer_size / frame_size; |
| 4969 | in->config.period_count = VOIP_CAPTURE_PERIOD_COUNT; |
| 4970 | in->config.rate = config->sample_rate; |
| 4971 | in->af_period_multiplier = 1; |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 4972 | } else { |
| 4973 | in->config = pcm_config_audio_capture; |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 4974 | frame_size = audio_stream_in_frame_size(&in->stream); |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 4975 | buffer_size = get_stream_buffer_size(AUDIO_CAPTURE_PERIOD_DURATION_MSEC, |
| 4976 | config->sample_rate, |
| 4977 | config->format, |
| 4978 | channel_count, |
| 4979 | is_low_latency); |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 4980 | in->config.period_size = buffer_size / frame_size; |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 4981 | in->config.rate = config->sample_rate; |
| 4982 | in->af_period_multiplier = 1; |
| 4983 | } |
| 4984 | if (config->format == AUDIO_FORMAT_PCM_8_24_BIT) |
| 4985 | in->config.format = PCM_FORMAT_S24_LE; |
Glenn Kasten | 68e79ce | 2014-07-15 10:56:59 -0700 | [diff] [blame] | 4986 | } |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 4987 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4988 | in->config.channels = channel_count; |
vivek mehta | a68fea6 | 2017-06-08 19:04:02 -0700 | [diff] [blame] | 4989 | in->sample_rate = in->config.rate; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4990 | |
Andy Hung | d13f0d3 | 2017-06-12 13:58:37 -0700 | [diff] [blame] | 4991 | in->error_log = error_log_create( |
| 4992 | ERROR_LOG_ENTRIES, |
| 4993 | NANOS_PER_SECOND /* aggregate consecutive identical errors within one second */); |
| 4994 | |
Ravi Kumar Alamanda | a417cc5 | 2015-05-01 16:41:56 -0700 | [diff] [blame] | 4995 | /* This stream could be for sound trigger lab, |
| 4996 | get sound trigger pcm if present */ |
| 4997 | audio_extn_sound_trigger_check_and_get_session(in); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 4998 | |
Haynes Mathew George | c735fb0 | 2016-06-30 18:00:28 -0700 | [diff] [blame] | 4999 | lock_input_stream(in); |
| 5000 | audio_extn_snd_mon_register_listener(in, in_snd_mon_cb); |
| 5001 | pthread_mutex_lock(&adev->lock); |
| 5002 | in->card_status = adev->card_status; |
| 5003 | pthread_mutex_unlock(&adev->lock); |
| 5004 | pthread_mutex_unlock(&in->lock); |
| 5005 | |
vivek mehta | 4a82477 | 2017-06-08 19:05:49 -0700 | [diff] [blame] | 5006 | stream_app_type_cfg_init(&in->app_type_cfg); |
| 5007 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5008 | *stream_in = &in->stream; |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 5009 | ALOGV("%s: exit", __func__); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5010 | return 0; |
| 5011 | |
| 5012 | err_open: |
| 5013 | free(in); |
| 5014 | *stream_in = NULL; |
| 5015 | return ret; |
| 5016 | } |
| 5017 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 5018 | 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] | 5019 | struct audio_stream_in *stream) |
| 5020 | { |
Andy Hung | d13f0d3 | 2017-06-12 13:58:37 -0700 | [diff] [blame] | 5021 | struct stream_in *in = (struct stream_in *)stream; |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 5022 | ALOGV("%s", __func__); |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 5023 | |
Haynes Mathew George | c735fb0 | 2016-06-30 18:00:28 -0700 | [diff] [blame] | 5024 | // must deregister from sndmonitor first to prevent races |
| 5025 | // between the callback and close_stream |
| 5026 | audio_extn_snd_mon_unregister_listener(stream); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5027 | in_standby(&stream->common); |
Andy Hung | d13f0d3 | 2017-06-12 13:58:37 -0700 | [diff] [blame] | 5028 | |
| 5029 | error_log_destroy(in->error_log); |
| 5030 | in->error_log = NULL; |
| 5031 | |
Andy Hung | 0dbb52b | 2017-08-09 13:51:38 -0700 | [diff] [blame] | 5032 | pthread_mutex_destroy(&in->pre_lock); |
| 5033 | pthread_mutex_destroy(&in->lock); |
| 5034 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5035 | free(stream); |
| 5036 | |
| 5037 | return; |
| 5038 | } |
| 5039 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 5040 | 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] | 5041 | { |
| 5042 | return 0; |
| 5043 | } |
| 5044 | |
Andy Hung | 31aca91 | 2014-03-20 17:14:59 -0700 | [diff] [blame] | 5045 | /* verifies input and output devices and their capabilities. |
| 5046 | * |
| 5047 | * This verification is required when enabling extended bit-depth or |
| 5048 | * sampling rates, as not all qcom products support it. |
| 5049 | * |
| 5050 | * Suitable for calling only on initialization such as adev_open(). |
| 5051 | * It fills the audio_device use_case_table[] array. |
| 5052 | * |
| 5053 | * Has a side-effect that it needs to configure audio routing / devices |
| 5054 | * in order to power up the devices and read the device parameters. |
| 5055 | * It does not acquire any hw device lock. Should restore the devices |
| 5056 | * back to "normal state" upon completion. |
| 5057 | */ |
| 5058 | static int adev_verify_devices(struct audio_device *adev) |
| 5059 | { |
| 5060 | /* enumeration is a bit difficult because one really wants to pull |
| 5061 | * the use_case, device id, etc from the hidden pcm_device_table[]. |
| 5062 | * In this case there are the following use cases and device ids. |
| 5063 | * |
| 5064 | * [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = {0, 0}, |
| 5065 | * [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {15, 15}, |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 5066 | * [USECASE_AUDIO_PLAYBACK_HIFI] = {1, 1}, |
Andy Hung | 31aca91 | 2014-03-20 17:14:59 -0700 | [diff] [blame] | 5067 | * [USECASE_AUDIO_PLAYBACK_OFFLOAD] = {9, 9}, |
| 5068 | * [USECASE_AUDIO_RECORD] = {0, 0}, |
| 5069 | * [USECASE_AUDIO_RECORD_LOW_LATENCY] = {15, 15}, |
| 5070 | * [USECASE_VOICE_CALL] = {2, 2}, |
| 5071 | * |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 5072 | * USECASE_AUDIO_PLAYBACK_OFFLOAD, USECASE_AUDIO_PLAYBACK_HIFI omitted. |
Andy Hung | 31aca91 | 2014-03-20 17:14:59 -0700 | [diff] [blame] | 5073 | * USECASE_VOICE_CALL omitted, but possible for either input or output. |
| 5074 | */ |
| 5075 | |
| 5076 | /* should be the usecases enabled in adev_open_input_stream() */ |
| 5077 | static const int test_in_usecases[] = { |
| 5078 | USECASE_AUDIO_RECORD, |
| 5079 | USECASE_AUDIO_RECORD_LOW_LATENCY, /* does not appear to be used */ |
| 5080 | }; |
| 5081 | /* should be the usecases enabled in adev_open_output_stream()*/ |
| 5082 | static const int test_out_usecases[] = { |
| 5083 | USECASE_AUDIO_PLAYBACK_DEEP_BUFFER, |
| 5084 | USECASE_AUDIO_PLAYBACK_LOW_LATENCY, |
| 5085 | }; |
| 5086 | static const usecase_type_t usecase_type_by_dir[] = { |
| 5087 | PCM_PLAYBACK, |
| 5088 | PCM_CAPTURE, |
| 5089 | }; |
| 5090 | static const unsigned flags_by_dir[] = { |
| 5091 | PCM_OUT, |
| 5092 | PCM_IN, |
| 5093 | }; |
| 5094 | |
| 5095 | size_t i; |
| 5096 | unsigned dir; |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 5097 | const unsigned card_id = adev->snd_card; |
Andy Hung | 31aca91 | 2014-03-20 17:14:59 -0700 | [diff] [blame] | 5098 | char info[512]; /* for possible debug info */ |
| 5099 | |
| 5100 | for (dir = 0; dir < 2; ++dir) { |
| 5101 | const usecase_type_t usecase_type = usecase_type_by_dir[dir]; |
| 5102 | const unsigned flags_dir = flags_by_dir[dir]; |
| 5103 | const size_t testsize = |
| 5104 | dir ? ARRAY_SIZE(test_in_usecases) : ARRAY_SIZE(test_out_usecases); |
| 5105 | const int *testcases = |
| 5106 | dir ? test_in_usecases : test_out_usecases; |
| 5107 | const audio_devices_t audio_device = |
| 5108 | dir ? AUDIO_DEVICE_IN_BUILTIN_MIC : AUDIO_DEVICE_OUT_SPEAKER; |
| 5109 | |
| 5110 | for (i = 0; i < testsize; ++i) { |
| 5111 | const audio_usecase_t audio_usecase = testcases[i]; |
| 5112 | int device_id; |
| 5113 | snd_device_t snd_device; |
| 5114 | struct pcm_params **pparams; |
| 5115 | struct stream_out out; |
| 5116 | struct stream_in in; |
| 5117 | struct audio_usecase uc_info; |
| 5118 | int retval; |
| 5119 | |
| 5120 | pparams = &adev->use_case_table[audio_usecase]; |
| 5121 | pcm_params_free(*pparams); /* can accept null input */ |
| 5122 | *pparams = NULL; |
| 5123 | |
| 5124 | /* find the device ID for the use case (signed, for error) */ |
| 5125 | device_id = platform_get_pcm_device_id(audio_usecase, usecase_type); |
| 5126 | if (device_id < 0) |
| 5127 | continue; |
| 5128 | |
| 5129 | /* prepare structures for device probing */ |
| 5130 | memset(&uc_info, 0, sizeof(uc_info)); |
| 5131 | uc_info.id = audio_usecase; |
| 5132 | uc_info.type = usecase_type; |
| 5133 | if (dir) { |
| 5134 | adev->active_input = ∈ |
| 5135 | memset(&in, 0, sizeof(in)); |
| 5136 | in.device = audio_device; |
| 5137 | in.source = AUDIO_SOURCE_VOICE_COMMUNICATION; |
| 5138 | uc_info.stream.in = ∈ |
| 5139 | } else { |
| 5140 | adev->active_input = NULL; |
| 5141 | } |
| 5142 | memset(&out, 0, sizeof(out)); |
| 5143 | out.devices = audio_device; /* only field needed in select_devices */ |
| 5144 | uc_info.stream.out = &out; |
| 5145 | uc_info.devices = audio_device; |
| 5146 | uc_info.in_snd_device = SND_DEVICE_NONE; |
| 5147 | uc_info.out_snd_device = SND_DEVICE_NONE; |
| 5148 | list_add_tail(&adev->usecase_list, &uc_info.list); |
| 5149 | |
| 5150 | /* select device - similar to start_(in/out)put_stream() */ |
| 5151 | retval = select_devices(adev, audio_usecase); |
| 5152 | if (retval >= 0) { |
| 5153 | *pparams = pcm_params_get(card_id, device_id, flags_dir); |
| 5154 | #if LOG_NDEBUG == 0 |
| 5155 | if (*pparams) { |
| 5156 | ALOGV("%s: (%s) card %d device %d", __func__, |
| 5157 | dir ? "input" : "output", card_id, device_id); |
| 5158 | pcm_params_to_string(*pparams, info, ARRAY_SIZE(info)); |
Andy Hung | 31aca91 | 2014-03-20 17:14:59 -0700 | [diff] [blame] | 5159 | } else { |
| 5160 | ALOGV("%s: cannot locate card %d device %d", __func__, card_id, device_id); |
| 5161 | } |
| 5162 | #endif |
| 5163 | } |
| 5164 | |
| 5165 | /* deselect device - similar to stop_(in/out)put_stream() */ |
| 5166 | /* 1. Get and set stream specific mixer controls */ |
Glenn Kasten | e713730 | 2014-04-28 15:12:18 -0700 | [diff] [blame] | 5167 | retval = disable_audio_route(adev, &uc_info); |
Andy Hung | 31aca91 | 2014-03-20 17:14:59 -0700 | [diff] [blame] | 5168 | /* 2. Disable the rx device */ |
| 5169 | retval = disable_snd_device(adev, |
Glenn Kasten | e713730 | 2014-04-28 15:12:18 -0700 | [diff] [blame] | 5170 | dir ? uc_info.in_snd_device : uc_info.out_snd_device); |
Andy Hung | 31aca91 | 2014-03-20 17:14:59 -0700 | [diff] [blame] | 5171 | list_remove(&uc_info.list); |
| 5172 | } |
| 5173 | } |
| 5174 | adev->active_input = NULL; /* restore adev state */ |
| 5175 | return 0; |
| 5176 | } |
| 5177 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5178 | static int adev_close(hw_device_t *device) |
| 5179 | { |
Andy Hung | 31aca91 | 2014-03-20 17:14:59 -0700 | [diff] [blame] | 5180 | size_t i; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5181 | struct audio_device *adev = (struct audio_device *)device; |
vivek mehta | 1a9b7c0 | 2015-06-25 11:49:38 -0700 | [diff] [blame] | 5182 | |
| 5183 | if (!adev) |
| 5184 | return 0; |
| 5185 | |
| 5186 | pthread_mutex_lock(&adev_init_lock); |
| 5187 | |
| 5188 | if ((--audio_device_ref_count) == 0) { |
Haynes Mathew George | ceafc55 | 2017-05-24 15:44:23 -0700 | [diff] [blame] | 5189 | audio_extn_snd_mon_unregister_listener(adev); |
| 5190 | audio_extn_tfa_98xx_deinit(); |
vivek mehta | 1a9b7c0 | 2015-06-25 11:49:38 -0700 | [diff] [blame] | 5191 | audio_route_free(adev->audio_route); |
| 5192 | free(adev->snd_dev_ref_cnt); |
| 5193 | platform_deinit(adev->platform); |
| 5194 | audio_extn_extspk_deinit(adev->extspk); |
| 5195 | audio_extn_sound_trigger_deinit(adev); |
Haynes Mathew George | ceafc55 | 2017-05-24 15:44:23 -0700 | [diff] [blame] | 5196 | audio_extn_snd_mon_deinit(); |
vivek mehta | 1a9b7c0 | 2015-06-25 11:49:38 -0700 | [diff] [blame] | 5197 | for (i = 0; i < ARRAY_SIZE(adev->use_case_table); ++i) { |
| 5198 | pcm_params_free(adev->use_case_table[i]); |
| 5199 | } |
Haynes Mathew George | 88e6fb2 | 2015-08-19 11:51:34 -0700 | [diff] [blame] | 5200 | if (adev->adm_deinit) |
| 5201 | adev->adm_deinit(adev->adm_data); |
Andy Hung | 0dbb52b | 2017-08-09 13:51:38 -0700 | [diff] [blame] | 5202 | pthread_mutex_destroy(&adev->lock); |
vivek mehta | 1a9b7c0 | 2015-06-25 11:49:38 -0700 | [diff] [blame] | 5203 | free(device); |
Andy Hung | 31aca91 | 2014-03-20 17:14:59 -0700 | [diff] [blame] | 5204 | } |
vivek mehta | 1a9b7c0 | 2015-06-25 11:49:38 -0700 | [diff] [blame] | 5205 | |
| 5206 | pthread_mutex_unlock(&adev_init_lock); |
Haynes Mathew George | 88e6fb2 | 2015-08-19 11:51:34 -0700 | [diff] [blame] | 5207 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5208 | return 0; |
| 5209 | } |
| 5210 | |
Glenn Kasten | 4f99339 | 2014-05-14 07:30:48 -0700 | [diff] [blame] | 5211 | /* This returns 1 if the input parameter looks at all plausible as a low latency period size, |
| 5212 | * or 0 otherwise. A return value of 1 doesn't mean the value is guaranteed to work, |
| 5213 | * just that it _might_ work. |
| 5214 | */ |
| 5215 | static int period_size_is_plausible_for_low_latency(int period_size) |
| 5216 | { |
| 5217 | switch (period_size) { |
Glenn Kasten | 5b5d04e | 2015-04-09 09:43:29 -0700 | [diff] [blame] | 5218 | case 48: |
| 5219 | case 96: |
| 5220 | case 144: |
Glenn Kasten | 4f99339 | 2014-05-14 07:30:48 -0700 | [diff] [blame] | 5221 | case 160: |
Glenn Kasten | 5b5d04e | 2015-04-09 09:43:29 -0700 | [diff] [blame] | 5222 | case 192: |
Glenn Kasten | 4f99339 | 2014-05-14 07:30:48 -0700 | [diff] [blame] | 5223 | case 240: |
| 5224 | case 320: |
| 5225 | case 480: |
| 5226 | return 1; |
| 5227 | default: |
| 5228 | return 0; |
| 5229 | } |
| 5230 | } |
| 5231 | |
Haynes Mathew George | c735fb0 | 2016-06-30 18:00:28 -0700 | [diff] [blame] | 5232 | static void adev_snd_mon_cb(void * stream __unused, struct str_parms * parms) |
| 5233 | { |
| 5234 | int card; |
| 5235 | card_status_t status; |
| 5236 | |
| 5237 | if (!parms) |
| 5238 | return; |
| 5239 | |
| 5240 | if (parse_snd_card_status(parms, &card, &status) < 0) |
| 5241 | return; |
| 5242 | |
| 5243 | pthread_mutex_lock(&adev->lock); |
| 5244 | bool valid_cb = (card == adev->snd_card); |
| 5245 | if (valid_cb) { |
| 5246 | if (adev->card_status != status) { |
| 5247 | adev->card_status = status; |
| 5248 | platform_snd_card_update(adev->platform, status); |
| 5249 | } |
| 5250 | } |
| 5251 | pthread_mutex_unlock(&adev->lock); |
| 5252 | return; |
| 5253 | } |
| 5254 | |
Aniket Kumar Lata | 2648301 | 2018-01-31 20:21:42 -0800 | [diff] [blame] | 5255 | /* out and adev lock held */ |
| 5256 | static int check_a2dp_restore_l(struct audio_device *adev, struct stream_out *out, bool restore) |
| 5257 | { |
| 5258 | struct audio_usecase *uc_info; |
| 5259 | float left_p; |
| 5260 | float right_p; |
| 5261 | audio_devices_t devices; |
| 5262 | |
| 5263 | uc_info = get_usecase_from_list(adev, out->usecase); |
| 5264 | if (uc_info == NULL) { |
| 5265 | ALOGE("%s: Could not find the usecase (%d) in the list", |
| 5266 | __func__, out->usecase); |
| 5267 | return -EINVAL; |
| 5268 | } |
| 5269 | |
| 5270 | ALOGD("%s: enter: usecase(%d: %s)", __func__, |
| 5271 | out->usecase, use_case_table[out->usecase]); |
| 5272 | |
| 5273 | if (restore) { |
| 5274 | // restore A2DP device for active usecases and unmute if required |
| 5275 | if ((out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) && |
| 5276 | !is_a2dp_device(uc_info->out_snd_device)) { |
| 5277 | ALOGD("%s: restoring A2DP and unmuting stream", __func__); |
| 5278 | select_devices(adev, uc_info->id); |
| 5279 | pthread_mutex_lock(&out->compr_mute_lock); |
| 5280 | if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && |
| 5281 | (out->a2dp_compress_mute)) { |
| 5282 | out->a2dp_compress_mute = false; |
| 5283 | set_compr_volume(&out->stream, out->volume_l, out->volume_r); |
| 5284 | } |
| 5285 | pthread_mutex_unlock(&out->compr_mute_lock); |
| 5286 | } |
| 5287 | } else { |
| 5288 | // mute compress stream if suspended |
| 5289 | pthread_mutex_lock(&out->compr_mute_lock); |
| 5290 | if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && |
| 5291 | (!out->a2dp_compress_mute)) { |
| 5292 | if (!out->standby) { |
| 5293 | ALOGD("%s: selecting speaker and muting stream", __func__); |
| 5294 | devices = out->devices; |
| 5295 | out->devices = AUDIO_DEVICE_OUT_SPEAKER; |
| 5296 | left_p = out->volume_l; |
| 5297 | right_p = out->volume_r; |
| 5298 | if (out->offload_state == OFFLOAD_STATE_PLAYING) |
| 5299 | compress_pause(out->compr); |
| 5300 | set_compr_volume(&out->stream, 0.0f, 0.0f); |
| 5301 | out->a2dp_compress_mute = true; |
| 5302 | select_devices(adev, out->usecase); |
| 5303 | if (out->offload_state == OFFLOAD_STATE_PLAYING) |
| 5304 | compress_resume(out->compr); |
| 5305 | out->devices = devices; |
| 5306 | out->volume_l = left_p; |
| 5307 | out->volume_r = right_p; |
| 5308 | } |
| 5309 | } |
| 5310 | pthread_mutex_unlock(&out->compr_mute_lock); |
| 5311 | } |
| 5312 | ALOGV("%s: exit", __func__); |
| 5313 | return 0; |
| 5314 | } |
| 5315 | |
| 5316 | int check_a2dp_restore(struct audio_device *adev, struct stream_out *out, bool restore) |
| 5317 | { |
| 5318 | int ret = 0; |
| 5319 | |
| 5320 | lock_output_stream(out); |
| 5321 | pthread_mutex_lock(&adev->lock); |
| 5322 | |
| 5323 | ret = check_a2dp_restore_l(adev, out, restore); |
| 5324 | |
| 5325 | pthread_mutex_unlock(&adev->lock); |
| 5326 | pthread_mutex_unlock(&out->lock); |
| 5327 | return ret; |
| 5328 | } |
| 5329 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5330 | static int adev_open(const hw_module_t *module, const char *name, |
| 5331 | hw_device_t **device) |
| 5332 | { |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 5333 | int i, ret; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5334 | |
Eric Laurent | 2bafff1 | 2016-03-17 12:17:23 -0700 | [diff] [blame] | 5335 | ALOGD("%s: enter", __func__); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5336 | if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL; |
vivek mehta | 1a9b7c0 | 2015-06-25 11:49:38 -0700 | [diff] [blame] | 5337 | pthread_mutex_lock(&adev_init_lock); |
| 5338 | if (audio_device_ref_count != 0) { |
| 5339 | *device = &adev->device.common; |
| 5340 | audio_device_ref_count++; |
| 5341 | ALOGV("%s: returning existing instance of adev", __func__); |
| 5342 | ALOGV("%s: exit", __func__); |
| 5343 | pthread_mutex_unlock(&adev_init_lock); |
| 5344 | return 0; |
| 5345 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5346 | adev = calloc(1, sizeof(struct audio_device)); |
| 5347 | |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 5348 | pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL); |
| 5349 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5350 | adev->device.common.tag = HARDWARE_DEVICE_TAG; |
| 5351 | adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0; |
| 5352 | adev->device.common.module = (struct hw_module_t *)module; |
| 5353 | adev->device.common.close = adev_close; |
| 5354 | |
| 5355 | adev->device.init_check = adev_init_check; |
| 5356 | adev->device.set_voice_volume = adev_set_voice_volume; |
| 5357 | adev->device.set_master_volume = adev_set_master_volume; |
| 5358 | adev->device.get_master_volume = adev_get_master_volume; |
| 5359 | adev->device.set_master_mute = adev_set_master_mute; |
| 5360 | adev->device.get_master_mute = adev_get_master_mute; |
| 5361 | adev->device.set_mode = adev_set_mode; |
| 5362 | adev->device.set_mic_mute = adev_set_mic_mute; |
| 5363 | adev->device.get_mic_mute = adev_get_mic_mute; |
| 5364 | adev->device.set_parameters = adev_set_parameters; |
| 5365 | adev->device.get_parameters = adev_get_parameters; |
| 5366 | adev->device.get_input_buffer_size = adev_get_input_buffer_size; |
| 5367 | adev->device.open_output_stream = adev_open_output_stream; |
| 5368 | adev->device.close_output_stream = adev_close_output_stream; |
| 5369 | adev->device.open_input_stream = adev_open_input_stream; |
Eric Laurent | 0e46adf | 2016-12-16 12:49:24 -0800 | [diff] [blame] | 5370 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5371 | adev->device.close_input_stream = adev_close_input_stream; |
| 5372 | adev->device.dump = adev_dump; |
jiabin | 8962a4d | 2018-03-19 18:21:24 -0700 | [diff] [blame] | 5373 | adev->device.get_microphones = adev_get_microphones; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5374 | |
| 5375 | /* Set the default route before the PCM stream is opened */ |
| 5376 | pthread_mutex_lock(&adev->lock); |
| 5377 | adev->mode = AUDIO_MODE_NORMAL; |
Eric Laurent | c840063 | 2013-02-14 19:04:54 -0800 | [diff] [blame] | 5378 | adev->active_input = NULL; |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 5379 | adev->primary_output = NULL; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5380 | adev->bluetooth_nrec = true; |
Ravi Kumar Alamanda | f996704 | 2013-02-14 19:35:14 -0800 | [diff] [blame] | 5381 | adev->acdb_settings = TTY_MODE_OFF; |
Eric Laurent | 07eeafd | 2013-10-06 12:52:49 -0700 | [diff] [blame] | 5382 | /* adev->cur_hdmi_channels = 0; by calloc() */ |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 5383 | adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int)); |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 5384 | voice_init(adev); |
Ravi Kumar Alamanda | 3b1816c | 2013-02-27 23:01:21 -0800 | [diff] [blame] | 5385 | list_init(&adev->usecase_list); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5386 | pthread_mutex_unlock(&adev->lock); |
| 5387 | |
| 5388 | /* Loads platform specific libraries dynamically */ |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 5389 | adev->platform = platform_init(adev); |
| 5390 | if (!adev->platform) { |
| 5391 | free(adev->snd_dev_ref_cnt); |
| 5392 | free(adev); |
| 5393 | ALOGE("%s: Failed to init platform data, aborting.", __func__); |
| 5394 | *device = NULL; |
vivek mehta | 1a9b7c0 | 2015-06-25 11:49:38 -0700 | [diff] [blame] | 5395 | pthread_mutex_unlock(&adev_init_lock); |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 5396 | return -EINVAL; |
| 5397 | } |
Eric Laurent | 0499d4f | 2014-08-25 22:39:29 -0500 | [diff] [blame] | 5398 | adev->extspk = audio_extn_extspk_init(adev); |
| 5399 | |
Glenn Kasten | 6d53bfd | 2016-04-04 16:58:03 -0700 | [diff] [blame] | 5400 | adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW); |
| 5401 | if (adev->visualizer_lib == NULL) { |
| 5402 | ALOGW("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH); |
| 5403 | } else { |
| 5404 | ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH); |
| 5405 | adev->visualizer_start_output = |
| 5406 | (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib, |
| 5407 | "visualizer_hal_start_output"); |
| 5408 | adev->visualizer_stop_output = |
| 5409 | (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib, |
| 5410 | "visualizer_hal_stop_output"); |
Eric Laurent | c4aef75 | 2013-09-12 17:45:53 -0700 | [diff] [blame] | 5411 | } |
| 5412 | |
Glenn Kasten | 6d53bfd | 2016-04-04 16:58:03 -0700 | [diff] [blame] | 5413 | adev->offload_effects_lib = dlopen(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, RTLD_NOW); |
| 5414 | if (adev->offload_effects_lib == NULL) { |
| 5415 | ALOGW("%s: DLOPEN failed for %s", __func__, |
| 5416 | OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH); |
| 5417 | } else { |
| 5418 | ALOGV("%s: DLOPEN successful for %s", __func__, |
| 5419 | OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH); |
| 5420 | adev->offload_effects_start_output = |
| 5421 | (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib, |
| 5422 | "offload_effects_bundle_hal_start_output"); |
| 5423 | adev->offload_effects_stop_output = |
| 5424 | (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib, |
| 5425 | "offload_effects_bundle_hal_stop_output"); |
Haynes Mathew George | 41f8665 | 2014-06-17 14:22:15 -0700 | [diff] [blame] | 5426 | } |
| 5427 | |
Glenn Kasten | 6d53bfd | 2016-04-04 16:58:03 -0700 | [diff] [blame] | 5428 | adev->adm_lib = dlopen(ADM_LIBRARY_PATH, RTLD_NOW); |
| 5429 | if (adev->adm_lib == NULL) { |
| 5430 | ALOGW("%s: DLOPEN failed for %s", __func__, ADM_LIBRARY_PATH); |
| 5431 | } else { |
| 5432 | ALOGV("%s: DLOPEN successful for %s", __func__, ADM_LIBRARY_PATH); |
| 5433 | adev->adm_init = (adm_init_t) |
| 5434 | dlsym(adev->adm_lib, "adm_init"); |
| 5435 | adev->adm_deinit = (adm_deinit_t) |
| 5436 | dlsym(adev->adm_lib, "adm_deinit"); |
| 5437 | adev->adm_register_input_stream = (adm_register_input_stream_t) |
| 5438 | dlsym(adev->adm_lib, "adm_register_input_stream"); |
| 5439 | adev->adm_register_output_stream = (adm_register_output_stream_t) |
| 5440 | dlsym(adev->adm_lib, "adm_register_output_stream"); |
| 5441 | adev->adm_deregister_stream = (adm_deregister_stream_t) |
| 5442 | dlsym(adev->adm_lib, "adm_deregister_stream"); |
| 5443 | adev->adm_request_focus = (adm_request_focus_t) |
| 5444 | dlsym(adev->adm_lib, "adm_request_focus"); |
| 5445 | adev->adm_abandon_focus = (adm_abandon_focus_t) |
| 5446 | dlsym(adev->adm_lib, "adm_abandon_focus"); |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 5447 | adev->adm_set_config = (adm_set_config_t) |
| 5448 | dlsym(adev->adm_lib, "adm_set_config"); |
| 5449 | adev->adm_request_focus_v2 = (adm_request_focus_v2_t) |
| 5450 | dlsym(adev->adm_lib, "adm_request_focus_v2"); |
| 5451 | adev->adm_is_noirq_avail = (adm_is_noirq_avail_t) |
| 5452 | dlsym(adev->adm_lib, "adm_is_noirq_avail"); |
| 5453 | adev->adm_on_routing_change = (adm_on_routing_change_t) |
| 5454 | dlsym(adev->adm_lib, "adm_on_routing_change"); |
Haynes Mathew George | 88e6fb2 | 2015-08-19 11:51:34 -0700 | [diff] [blame] | 5455 | } |
| 5456 | |
Ravi Kumar Alamanda | 9f30654 | 2014-04-02 15:11:49 -0700 | [diff] [blame] | 5457 | adev->bt_wb_speech_enabled = false; |
Eric Laurent | cefbbac | 2014-09-04 13:54:10 -0500 | [diff] [blame] | 5458 | adev->enable_voicerx = false; |
Ravi Kumar Alamanda | 9f30654 | 2014-04-02 15:11:49 -0700 | [diff] [blame] | 5459 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5460 | *device = &adev->device.common; |
vivek mehta | 1a9b7c0 | 2015-06-25 11:49:38 -0700 | [diff] [blame] | 5461 | |
Andy Hung | 31aca91 | 2014-03-20 17:14:59 -0700 | [diff] [blame] | 5462 | if (k_enable_extended_precision) |
| 5463 | adev_verify_devices(adev); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5464 | |
Glenn Kasten | 4f99339 | 2014-05-14 07:30:48 -0700 | [diff] [blame] | 5465 | char value[PROPERTY_VALUE_MAX]; |
| 5466 | int trial; |
| 5467 | if (property_get("audio_hal.period_size", value, NULL) > 0) { |
| 5468 | trial = atoi(value); |
| 5469 | if (period_size_is_plausible_for_low_latency(trial)) { |
| 5470 | pcm_config_low_latency.period_size = trial; |
| 5471 | pcm_config_low_latency.start_threshold = trial / 4; |
| 5472 | pcm_config_low_latency.avail_min = trial / 4; |
| 5473 | configured_low_latency_capture_period_size = trial; |
| 5474 | } |
| 5475 | } |
| 5476 | if (property_get("audio_hal.in_period_size", value, NULL) > 0) { |
| 5477 | trial = atoi(value); |
| 5478 | if (period_size_is_plausible_for_low_latency(trial)) { |
| 5479 | configured_low_latency_capture_period_size = trial; |
| 5480 | } |
| 5481 | } |
| 5482 | |
Haynes Mathew George | e5ff0fc | 2017-02-16 20:33:38 -0800 | [diff] [blame] | 5483 | // commented as full set of app type cfg is sent from platform |
| 5484 | // audio_extn_utils_send_default_app_type_cfg(adev->platform, adev->mixer); |
vivek mehta | 1a9b7c0 | 2015-06-25 11:49:38 -0700 | [diff] [blame] | 5485 | audio_device_ref_count++; |
Haynes Mathew George | 03c4010 | 2016-01-29 17:57:48 -0800 | [diff] [blame] | 5486 | |
| 5487 | if (property_get("audio_hal.period_multiplier", value, NULL) > 0) { |
| 5488 | af_period_multiplier = atoi(value); |
| 5489 | if (af_period_multiplier < 0) { |
| 5490 | af_period_multiplier = 2; |
| 5491 | } else if (af_period_multiplier > 4) { |
| 5492 | af_period_multiplier = 4; |
| 5493 | } |
| 5494 | ALOGV("new period_multiplier = %d", af_period_multiplier); |
| 5495 | } |
| 5496 | |
Alain Vongsouvanh | 13f26e8 | 2016-11-18 14:39:11 -0800 | [diff] [blame] | 5497 | audio_extn_tfa_98xx_init(adev); |
| 5498 | |
vivek mehta | 1a9b7c0 | 2015-06-25 11:49:38 -0700 | [diff] [blame] | 5499 | pthread_mutex_unlock(&adev_init_lock); |
| 5500 | |
Haynes Mathew George | 88e6fb2 | 2015-08-19 11:51:34 -0700 | [diff] [blame] | 5501 | if (adev->adm_init) |
| 5502 | adev->adm_data = adev->adm_init(); |
| 5503 | |
Ravi Kumar Alamanda | 533bb72 | 2015-09-23 13:47:03 -0700 | [diff] [blame] | 5504 | audio_extn_perf_lock_init(); |
Haynes Mathew George | c735fb0 | 2016-06-30 18:00:28 -0700 | [diff] [blame] | 5505 | audio_extn_snd_mon_init(); |
| 5506 | pthread_mutex_lock(&adev->lock); |
| 5507 | audio_extn_snd_mon_register_listener(NULL, adev_snd_mon_cb); |
| 5508 | adev->card_status = CARD_STATUS_ONLINE; |
| 5509 | pthread_mutex_unlock(&adev->lock); |
Haynes Mathew George | ceafc55 | 2017-05-24 15:44:23 -0700 | [diff] [blame] | 5510 | audio_extn_sound_trigger_init(adev);/* dependent on snd_mon_init() */ |
Ravi Kumar Alamanda | 533bb72 | 2015-09-23 13:47:03 -0700 | [diff] [blame] | 5511 | |
Eric Laurent | 2bafff1 | 2016-03-17 12:17:23 -0700 | [diff] [blame] | 5512 | ALOGD("%s: exit", __func__); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5513 | return 0; |
| 5514 | } |
| 5515 | |
| 5516 | static struct hw_module_methods_t hal_module_methods = { |
| 5517 | .open = adev_open, |
| 5518 | }; |
| 5519 | |
| 5520 | struct audio_module HAL_MODULE_INFO_SYM = { |
| 5521 | .common = { |
| 5522 | .tag = HARDWARE_MODULE_TAG, |
| 5523 | .module_api_version = AUDIO_MODULE_API_VERSION_0_1, |
| 5524 | .hal_api_version = HARDWARE_HAL_API_VERSION, |
| 5525 | .id = AUDIO_HARDWARE_MODULE_ID, |
| 5526 | .name = "QCOM Audio HAL", |
| 5527 | .author = "Code Aurora Forum", |
| 5528 | .methods = &hal_module_methods, |
| 5529 | }, |
| 5530 | }; |