Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 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 | |
Paul McLean | 9ab869a | 2015-01-13 09:37:06 -0800 | [diff] [blame] | 17 | #define LOG_TAG "modules.usbaudio.audio_hal" |
Paul McLean | cf5310a | 2018-08-22 14:33:12 -0700 | [diff] [blame] | 18 | /* #define LOG_NDEBUG 0 */ |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 19 | |
| 20 | #include <errno.h> |
Mark Salyzyn | 88e458a | 2014-04-28 12:30:44 -0700 | [diff] [blame] | 21 | #include <inttypes.h> |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 22 | #include <pthread.h> |
| 23 | #include <stdint.h> |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 24 | #include <stdlib.h> |
jiabin | 400bbe0 | 2020-10-28 13:56:59 -0700 | [diff] [blame] | 25 | #include <string.h> |
Mark Salyzyn | 88e458a | 2014-04-28 12:30:44 -0700 | [diff] [blame] | 26 | #include <sys/time.h> |
Tri Vo | 00a8673 | 2017-06-27 11:33:36 -0700 | [diff] [blame] | 27 | #include <unistd.h> |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 28 | |
Mark Salyzyn | 88e458a | 2014-04-28 12:30:44 -0700 | [diff] [blame] | 29 | #include <log/log.h> |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 30 | #include <cutils/list.h> |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 31 | #include <cutils/str_parms.h> |
| 32 | #include <cutils/properties.h> |
| 33 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 34 | #include <hardware/audio.h> |
Paul McLean | e32cbc1 | 2014-06-25 10:42:07 -0700 | [diff] [blame] | 35 | #include <hardware/audio_alsaops.h> |
| 36 | #include <hardware/hardware.h> |
| 37 | |
| 38 | #include <system/audio.h> |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 39 | |
| 40 | #include <tinyalsa/asoundlib.h> |
| 41 | |
Paul McLean | c220115 | 2014-07-16 13:46:07 -0700 | [diff] [blame] | 42 | #include <audio_utils/channels.h> |
| 43 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 44 | #include "alsa_device_profile.h" |
| 45 | #include "alsa_device_proxy.h" |
Paul McLean | 9ab869a | 2015-01-13 09:37:06 -0800 | [diff] [blame] | 46 | #include "alsa_logging.h" |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 47 | |
Paul McLean | 1d585cc | 2016-05-24 11:28:10 -0600 | [diff] [blame] | 48 | /* Lock play & record samples rates at or above this threshold */ |
| 49 | #define RATELOCK_THRESHOLD 96000 |
| 50 | |
Paul McLean | cfdbd6b | 2018-07-27 11:16:02 -0600 | [diff] [blame] | 51 | #define max(a, b) ((a) > (b) ? (a) : (b)) |
| 52 | #define min(a, b) ((a) < (b) ? (a) : (b)) |
| 53 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 54 | struct audio_device { |
| 55 | struct audio_hw_device hw_device; |
| 56 | |
| 57 | pthread_mutex_t lock; /* see note below on mutex acquisition order */ |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 58 | |
| 59 | /* output */ |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 60 | struct listnode output_stream_list; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 61 | |
| 62 | /* input */ |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 63 | struct listnode input_stream_list; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 64 | |
Paul McLean | 1d585cc | 2016-05-24 11:28:10 -0600 | [diff] [blame] | 65 | /* lock input & output sample rates */ |
| 66 | /*FIXME - How do we address multiple output streams? */ |
Paul McLean | cfdbd6b | 2018-07-27 11:16:02 -0600 | [diff] [blame] | 67 | uint32_t device_sample_rate; // this should be a rate that is common to both input & output |
Paul McLean | 1d585cc | 2016-05-24 11:28:10 -0600 | [diff] [blame] | 68 | |
Eric Laurent | 253def9 | 2014-09-14 12:18:18 -0700 | [diff] [blame] | 69 | bool mic_muted; |
| 70 | |
Andy Hung | b10ce6d | 2017-10-27 19:37:58 -0700 | [diff] [blame] | 71 | int32_t inputs_open; /* number of input streams currently open. */ |
jiabin | 400bbe0 | 2020-10-28 13:56:59 -0700 | [diff] [blame] | 72 | |
| 73 | audio_patch_handle_t next_patch_handle; // Increase 1 when create audio patch |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 74 | }; |
| 75 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 76 | struct stream_lock { |
| 77 | pthread_mutex_t lock; /* see note below on mutex acquisition order */ |
| 78 | pthread_mutex_t pre_lock; /* acquire before lock to avoid DOS by playback thread */ |
| 79 | }; |
| 80 | |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 81 | struct alsa_device_info { |
| 82 | alsa_device_profile profile; /* The profile of the ALSA device */ |
| 83 | alsa_device_proxy proxy; /* The state */ |
| 84 | struct listnode list_node; |
| 85 | }; |
| 86 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 87 | struct stream_out { |
| 88 | struct audio_stream_out stream; |
| 89 | |
Paul McLean | cf5310a | 2018-08-22 14:33:12 -0700 | [diff] [blame] | 90 | struct stream_lock lock; |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 91 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 92 | bool standby; |
| 93 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 94 | struct audio_device *adev; /* hardware information - only using this for the lock */ |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 95 | |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 96 | struct listnode alsa_devices; /* The ALSA devices connected to the stream. */ |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 97 | |
Andy Hung | 03576be | 2014-07-21 21:16:45 -0700 | [diff] [blame] | 98 | unsigned hal_channel_count; /* channel count exposed to AudioFlinger. |
| 99 | * This may differ from the device channel count when |
| 100 | * the device is not compatible with AudioFlinger |
| 101 | * capabilities, e.g. exposes too many channels or |
| 102 | * too few channels. */ |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 103 | audio_channel_mask_t hal_channel_mask; /* USB devices deal in channel counts, not masks |
| 104 | * so the proxy doesn't have a channel_mask, but |
| 105 | * audio HALs need to talk about channel masks |
| 106 | * so expose the one calculated by |
| 107 | * adev_open_output_stream */ |
Andy Hung | 182ddc7 | 2015-05-05 23:37:30 -0700 | [diff] [blame] | 108 | |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 109 | struct listnode list_node; |
| 110 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 111 | void * conversion_buffer; /* any conversions are put into here |
| 112 | * they could come from here too if |
| 113 | * there was a previous conversion */ |
| 114 | size_t conversion_buffer_size; /* in bytes */ |
jiabin | 400bbe0 | 2020-10-28 13:56:59 -0700 | [diff] [blame] | 115 | |
| 116 | struct pcm_config config; |
| 117 | |
| 118 | audio_io_handle_t handle; // Unique constant for a stream |
| 119 | |
| 120 | audio_patch_handle_t patch_handle; // Patch handle for this stream |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 121 | }; |
| 122 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 123 | struct stream_in { |
| 124 | struct audio_stream_in stream; |
| 125 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 126 | struct stream_lock lock; |
| 127 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 128 | bool standby; |
| 129 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 130 | struct audio_device *adev; /* hardware information - only using this for the lock */ |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 131 | |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 132 | struct listnode alsa_devices; /* The ALSA devices connected to the stream. */ |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 133 | |
Paul McLean | 2cfd81b | 2014-09-15 12:32:23 -0700 | [diff] [blame] | 134 | unsigned hal_channel_count; /* channel count exposed to AudioFlinger. |
| 135 | * This may differ from the device channel count when |
| 136 | * the device is not compatible with AudioFlinger |
| 137 | * capabilities, e.g. exposes too many channels or |
| 138 | * too few channels. */ |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 139 | audio_channel_mask_t hal_channel_mask; /* USB devices deal in channel counts, not masks |
| 140 | * so the proxy doesn't have a channel_mask, but |
| 141 | * audio HALs need to talk about channel masks |
| 142 | * so expose the one calculated by |
| 143 | * adev_open_input_stream */ |
Andy Hung | 780f1f8 | 2015-04-22 22:14:39 -0700 | [diff] [blame] | 144 | |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 145 | struct listnode list_node; |
| 146 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 147 | /* We may need to read more data from the device in order to data reduce to 16bit, 4chan */ |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 148 | void * conversion_buffer; /* any conversions are put into here |
| 149 | * they could come from here too if |
| 150 | * there was a previous conversion */ |
| 151 | size_t conversion_buffer_size; /* in bytes */ |
jiabin | 400bbe0 | 2020-10-28 13:56:59 -0700 | [diff] [blame] | 152 | |
| 153 | struct pcm_config config; |
| 154 | |
| 155 | audio_io_handle_t handle; // Unique identifier for a stream |
| 156 | |
| 157 | audio_patch_handle_t patch_handle; // Patch handle for this stream |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 158 | }; |
| 159 | |
jiabin | 400bbe0 | 2020-10-28 13:56:59 -0700 | [diff] [blame] | 160 | // Map channel count to output channel mask |
| 161 | static const audio_channel_mask_t OUT_CHANNEL_MASKS_MAP[] = { |
| 162 | AUDIO_CHANNEL_NONE, /* 0 */ |
| 163 | AUDIO_CHANNEL_OUT_MONO, /* 1 */ |
| 164 | AUDIO_CHANNEL_OUT_STEREO, /* 2 */ |
| 165 | AUDIO_CHANNEL_OUT_2POINT1, /* 3 */ |
| 166 | AUDIO_CHANNEL_OUT_QUAD, /* 4 */ |
| 167 | AUDIO_CHANNEL_OUT_PENTA, /* 5 */ |
| 168 | AUDIO_CHANNEL_OUT_5POINT1, /* 6 */ |
| 169 | AUDIO_CHANNEL_OUT_6POINT1, /* 7 */ |
| 170 | AUDIO_CHANNEL_OUT_7POINT1 /* 8 */ |
| 171 | /* channel counts greater than this are not considered */ |
| 172 | }; |
| 173 | static const int OUT_CHANNEL_MASKS_SIZE = AUDIO_ARRAY_SIZE(OUT_CHANNEL_MASKS_MAP); |
| 174 | |
| 175 | // Map channel count to input channel mask |
| 176 | static const audio_channel_mask_t IN_CHANNEL_MASKS_MAP[] = { |
| 177 | AUDIO_CHANNEL_NONE, /* 0 */ |
| 178 | AUDIO_CHANNEL_IN_MONO, /* 1 */ |
| 179 | AUDIO_CHANNEL_IN_STEREO, /* 2 */ |
| 180 | /* channel counts greater than this are not considered */ |
| 181 | }; |
| 182 | static const int IN_CHANNEL_MASKS_SIZE = AUDIO_ARRAY_SIZE(IN_CHANNEL_MASKS_MAP); |
| 183 | |
| 184 | // Map channel count to index mask |
| 185 | static const audio_channel_mask_t CHANNEL_INDEX_MASKS_MAP[] = { |
| 186 | AUDIO_CHANNEL_NONE, /* 0 */ |
| 187 | AUDIO_CHANNEL_INDEX_MASK_1, /* 1 */ |
| 188 | AUDIO_CHANNEL_INDEX_MASK_2, /* 2 */ |
| 189 | AUDIO_CHANNEL_INDEX_MASK_3, /* 3 */ |
| 190 | AUDIO_CHANNEL_INDEX_MASK_4, /* 4 */ |
| 191 | AUDIO_CHANNEL_INDEX_MASK_5, /* 5 */ |
| 192 | AUDIO_CHANNEL_INDEX_MASK_6, /* 6 */ |
| 193 | AUDIO_CHANNEL_INDEX_MASK_7, /* 7 */ |
| 194 | AUDIO_CHANNEL_INDEX_MASK_8 /* 8 */ |
| 195 | /* channel counts greater than this are not considered */ |
| 196 | }; |
| 197 | static const int CHANNEL_INDEX_MASKS_SIZE = AUDIO_ARRAY_SIZE(CHANNEL_INDEX_MASKS_MAP); |
| 198 | |
| 199 | |
| 200 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 201 | /* |
| 202 | * Locking Helpers |
| 203 | */ |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 204 | /* |
Eric Laurent | 7031809 | 2015-06-19 17:49:17 -0700 | [diff] [blame] | 205 | * NOTE: when multiple mutexes have to be acquired, always take the |
| 206 | * stream_in or stream_out mutex first, followed by the audio_device mutex. |
| 207 | * stream pre_lock is always acquired before stream lock to prevent starvation of control thread by |
| 208 | * higher priority playback or capture thread. |
| 209 | */ |
| 210 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 211 | static void stream_lock_init(struct stream_lock *lock) { |
| 212 | pthread_mutex_init(&lock->lock, (const pthread_mutexattr_t *) NULL); |
| 213 | pthread_mutex_init(&lock->pre_lock, (const pthread_mutexattr_t *) NULL); |
| 214 | } |
| 215 | |
| 216 | static void stream_lock(struct stream_lock *lock) { |
jiabin | 400bbe0 | 2020-10-28 13:56:59 -0700 | [diff] [blame] | 217 | if (lock == NULL) { |
| 218 | return; |
| 219 | } |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 220 | pthread_mutex_lock(&lock->pre_lock); |
| 221 | pthread_mutex_lock(&lock->lock); |
| 222 | pthread_mutex_unlock(&lock->pre_lock); |
| 223 | } |
| 224 | |
| 225 | static void stream_unlock(struct stream_lock *lock) { |
| 226 | pthread_mutex_unlock(&lock->lock); |
| 227 | } |
| 228 | |
| 229 | static void device_lock(struct audio_device *adev) { |
| 230 | pthread_mutex_lock(&adev->lock); |
| 231 | } |
| 232 | |
| 233 | static int device_try_lock(struct audio_device *adev) { |
| 234 | return pthread_mutex_trylock(&adev->lock); |
| 235 | } |
| 236 | |
| 237 | static void device_unlock(struct audio_device *adev) { |
| 238 | pthread_mutex_unlock(&adev->lock); |
| 239 | } |
| 240 | |
| 241 | /* |
| 242 | * streams list management |
| 243 | */ |
| 244 | static void adev_add_stream_to_list( |
| 245 | struct audio_device* adev, struct listnode* list, struct listnode* stream_node) { |
| 246 | device_lock(adev); |
| 247 | |
| 248 | list_add_tail(list, stream_node); |
| 249 | |
| 250 | device_unlock(adev); |
| 251 | } |
| 252 | |
jiabin | 400bbe0 | 2020-10-28 13:56:59 -0700 | [diff] [blame] | 253 | static struct stream_out* adev_get_stream_out_by_io_handle_l( |
| 254 | struct audio_device* adev, audio_io_handle_t handle) { |
| 255 | struct listnode *node; |
| 256 | list_for_each (node, &adev->output_stream_list) { |
| 257 | struct stream_out *out = node_to_item(node, struct stream_out, list_node); |
| 258 | if (out->handle == handle) { |
| 259 | return out; |
| 260 | } |
| 261 | } |
| 262 | return NULL; |
| 263 | } |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 264 | |
jiabin | 400bbe0 | 2020-10-28 13:56:59 -0700 | [diff] [blame] | 265 | static struct stream_in* adev_get_stream_in_by_io_handle_l( |
| 266 | struct audio_device* adev, audio_io_handle_t handle) { |
| 267 | struct listnode *node; |
| 268 | list_for_each (node, &adev->input_stream_list) { |
| 269 | struct stream_in *in = node_to_item(node, struct stream_in, list_node); |
| 270 | if (in->handle == handle) { |
| 271 | return in; |
| 272 | } |
| 273 | } |
| 274 | return NULL; |
| 275 | } |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 276 | |
jiabin | 400bbe0 | 2020-10-28 13:56:59 -0700 | [diff] [blame] | 277 | static struct stream_out* adev_get_stream_out_by_patch_handle_l( |
| 278 | struct audio_device* adev, audio_patch_handle_t patch_handle) { |
| 279 | struct listnode *node; |
| 280 | list_for_each (node, &adev->output_stream_list) { |
| 281 | struct stream_out *out = node_to_item(node, struct stream_out, list_node); |
| 282 | if (out->patch_handle == patch_handle) { |
| 283 | return out; |
| 284 | } |
| 285 | } |
| 286 | return NULL; |
| 287 | } |
| 288 | |
| 289 | static struct stream_in* adev_get_stream_in_by_patch_handle_l( |
| 290 | struct audio_device* adev, audio_patch_handle_t patch_handle) { |
| 291 | struct listnode *node; |
| 292 | list_for_each (node, &adev->input_stream_list) { |
| 293 | struct stream_in *in = node_to_item(node, struct stream_in, list_node); |
| 294 | if (in->patch_handle == patch_handle) { |
| 295 | return in; |
| 296 | } |
| 297 | } |
| 298 | return NULL; |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 299 | } |
| 300 | |
Eric Laurent | 7031809 | 2015-06-19 17:49:17 -0700 | [diff] [blame] | 301 | /* |
Paul McLean | 0f1753e | 2014-12-02 12:36:45 -0700 | [diff] [blame] | 302 | * Extract the card and device numbers from the supplied key/value pairs. |
| 303 | * kvpairs A null-terminated string containing the key/value pairs or card and device. |
| 304 | * i.e. "card=1;device=42" |
| 305 | * card A pointer to a variable to receive the parsed-out card number. |
| 306 | * device A pointer to a variable to receive the parsed-out device number. |
| 307 | * NOTE: The variables pointed to by card and device return -1 (undefined) if the |
| 308 | * associated key/value pair is not found in the provided string. |
Paul McLean | 65ec72b | 2015-02-18 09:13:24 -0800 | [diff] [blame] | 309 | * Return true if the kvpairs string contain a card/device spec, false otherwise. |
Paul McLean | 0f1753e | 2014-12-02 12:36:45 -0700 | [diff] [blame] | 310 | */ |
Paul McLean | 65ec72b | 2015-02-18 09:13:24 -0800 | [diff] [blame] | 311 | static bool parse_card_device_params(const char *kvpairs, int *card, int *device) |
Paul McLean | 0f1753e | 2014-12-02 12:36:45 -0700 | [diff] [blame] | 312 | { |
| 313 | struct str_parms * parms = str_parms_create_str(kvpairs); |
| 314 | char value[32]; |
| 315 | int param_val; |
| 316 | |
| 317 | // initialize to "undefined" state. |
| 318 | *card = -1; |
| 319 | *device = -1; |
| 320 | |
| 321 | param_val = str_parms_get_str(parms, "card", value, sizeof(value)); |
| 322 | if (param_val >= 0) { |
| 323 | *card = atoi(value); |
| 324 | } |
| 325 | |
| 326 | param_val = str_parms_get_str(parms, "device", value, sizeof(value)); |
| 327 | if (param_val >= 0) { |
| 328 | *device = atoi(value); |
| 329 | } |
| 330 | |
| 331 | str_parms_destroy(parms); |
Paul McLean | 65ec72b | 2015-02-18 09:13:24 -0800 | [diff] [blame] | 332 | |
| 333 | return *card >= 0 && *device >= 0; |
Paul McLean | 0f1753e | 2014-12-02 12:36:45 -0700 | [diff] [blame] | 334 | } |
| 335 | |
Andy Hung | 4e6a1c0 | 2017-10-27 20:31:46 -0700 | [diff] [blame] | 336 | static char *device_get_parameters(const alsa_device_profile *profile, const char * keys) |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 337 | { |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 338 | if (profile->card < 0 || profile->device < 0) { |
| 339 | return strdup(""); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 340 | } |
Paul McLean | e32cbc1 | 2014-06-25 10:42:07 -0700 | [diff] [blame] | 341 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 342 | struct str_parms *query = str_parms_create_str(keys); |
| 343 | struct str_parms *result = str_parms_create(); |
Paul McLean | e32cbc1 | 2014-06-25 10:42:07 -0700 | [diff] [blame] | 344 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 345 | /* These keys are from hardware/libhardware/include/audio.h */ |
| 346 | /* supported sample rates */ |
| 347 | if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) { |
| 348 | char* rates_list = profile_get_sample_rate_strs(profile); |
| 349 | str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES, |
| 350 | rates_list); |
| 351 | free(rates_list); |
Paul McLean | e32cbc1 | 2014-06-25 10:42:07 -0700 | [diff] [blame] | 352 | } |
| 353 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 354 | /* supported channel counts */ |
| 355 | if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) { |
| 356 | char* channels_list = profile_get_channel_count_strs(profile); |
| 357 | str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, |
| 358 | channels_list); |
| 359 | free(channels_list); |
Paul McLean | e32cbc1 | 2014-06-25 10:42:07 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 362 | /* supported sample formats */ |
| 363 | if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) { |
| 364 | char * format_params = profile_get_format_strs(profile); |
| 365 | str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_FORMATS, |
| 366 | format_params); |
| 367 | free(format_params); |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 368 | } |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 369 | str_parms_destroy(query); |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 370 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 371 | char* result_str = str_parms_to_str(result); |
| 372 | str_parms_destroy(result); |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 373 | |
Paul McLean | 65ec72b | 2015-02-18 09:13:24 -0800 | [diff] [blame] | 374 | ALOGV("device_get_parameters = %s", result_str); |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 375 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 376 | return result_str; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 377 | } |
| 378 | |
jiabin | 400bbe0 | 2020-10-28 13:56:59 -0700 | [diff] [blame] | 379 | static audio_format_t audio_format_from(enum pcm_format format) |
| 380 | { |
| 381 | switch (format) { |
| 382 | case PCM_FORMAT_S16_LE: |
| 383 | return AUDIO_FORMAT_PCM_16_BIT; |
| 384 | case PCM_FORMAT_S32_LE: |
| 385 | return AUDIO_FORMAT_PCM_32_BIT; |
| 386 | case PCM_FORMAT_S8: |
| 387 | return AUDIO_FORMAT_PCM_8_BIT; |
| 388 | case PCM_FORMAT_S24_LE: |
| 389 | return AUDIO_FORMAT_PCM_8_24_BIT; |
| 390 | case PCM_FORMAT_S24_3LE: |
| 391 | return AUDIO_FORMAT_PCM_24_BIT_PACKED; |
| 392 | default: |
| 393 | return AUDIO_FORMAT_INVALID; |
| 394 | } |
| 395 | } |
| 396 | |
jiabin | a635fcf | 2021-01-11 11:34:50 -0800 | [diff] [blame^] | 397 | static unsigned int populate_channel_mask_from_profile(const alsa_device_profile* profile, |
| 398 | bool is_output, |
| 399 | audio_channel_mask_t channel_masks[]) |
| 400 | { |
| 401 | unsigned int num_channel_masks = 0; |
| 402 | const audio_channel_mask_t* channel_masks_map = |
| 403 | is_output ? OUT_CHANNEL_MASKS_MAP : IN_CHANNEL_MASKS_MAP; |
| 404 | const int channel_masks_size = is_output ? OUT_CHANNEL_MASKS_SIZE : IN_CHANNEL_MASKS_SIZE; |
| 405 | unsigned int channel_count = 0; |
| 406 | for (size_t i = 0; i < min(channel_masks_size, AUDIO_PORT_MAX_CHANNEL_MASKS) && |
| 407 | (channel_count = profile->channel_counts[i]) != 0 && |
| 408 | num_channel_masks < AUDIO_PORT_MAX_CHANNEL_MASKS; ++i) { |
| 409 | if (channel_count < channel_masks_size && |
| 410 | channel_masks_map[channel_count] != AUDIO_CHANNEL_NONE) { |
| 411 | channel_masks[num_channel_masks++] = channel_masks_map[channel_count]; |
| 412 | if (num_channel_masks >= AUDIO_PORT_MAX_CHANNEL_MASKS) { |
| 413 | break; |
| 414 | } |
| 415 | } |
| 416 | if (channel_count < CHANNEL_INDEX_MASKS_SIZE && |
| 417 | CHANNEL_INDEX_MASKS_MAP[channel_count] != AUDIO_CHANNEL_NONE) { |
| 418 | channel_masks[num_channel_masks++] = CHANNEL_INDEX_MASKS_MAP[channel_count]; |
| 419 | } |
| 420 | } |
| 421 | return num_channel_masks; |
| 422 | } |
| 423 | |
| 424 | static unsigned int populate_sample_rates_from_profile(const alsa_device_profile* profile, |
| 425 | unsigned int sample_rates[]) |
| 426 | { |
| 427 | unsigned int num_sample_rates = 0; |
| 428 | for (;num_sample_rates < min(MAX_PROFILE_SAMPLE_RATES, AUDIO_PORT_MAX_SAMPLING_RATES) && |
| 429 | profile->sample_rates[num_sample_rates] != 0; num_sample_rates++) { |
| 430 | sample_rates[num_sample_rates] = profile->sample_rates[num_sample_rates]; |
| 431 | } |
| 432 | return num_sample_rates; |
| 433 | } |
| 434 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 435 | /* |
| 436 | * HAl Functions |
| 437 | */ |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 438 | /** |
| 439 | * NOTE: when multiple mutexes have to be acquired, always respect the |
| 440 | * following order: hw device > out stream |
| 441 | */ |
| 442 | |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 443 | static struct alsa_device_info* stream_get_first_alsa_device(const struct listnode *alsa_devices) { |
| 444 | if (list_empty(alsa_devices)) { |
| 445 | return NULL; |
| 446 | } |
| 447 | return node_to_item(list_head(alsa_devices), struct alsa_device_info, list_node); |
| 448 | } |
| 449 | |
jiabin | 400bbe0 | 2020-10-28 13:56:59 -0700 | [diff] [blame] | 450 | /** |
| 451 | * Must be called with holding the stream's lock. |
| 452 | */ |
| 453 | static void stream_standby_l(struct listnode *alsa_devices, bool *standby) |
| 454 | { |
| 455 | if (!*standby) { |
| 456 | struct listnode *node; |
| 457 | list_for_each (node, alsa_devices) { |
| 458 | struct alsa_device_info *device_info = |
| 459 | node_to_item(node, struct alsa_device_info, list_node); |
| 460 | proxy_close(&device_info->proxy); |
| 461 | } |
| 462 | *standby = true; |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | static void stream_clear_devices(struct listnode *alsa_devices) |
| 467 | { |
| 468 | struct listnode *node, *temp; |
| 469 | struct alsa_device_info *device_info = NULL; |
| 470 | list_for_each_safe (node, temp, alsa_devices) { |
| 471 | device_info = node_to_item(node, struct alsa_device_info, list_node); |
| 472 | if (device_info != NULL) { |
| 473 | list_remove(&device_info->list_node); |
| 474 | free(device_info); |
| 475 | } |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | static int stream_set_new_devices(struct pcm_config *config, |
| 480 | struct listnode *alsa_devices, |
| 481 | unsigned int num_devices, |
| 482 | const int cards[], |
| 483 | const int devices[], |
| 484 | int direction) |
| 485 | { |
| 486 | int status = 0; |
| 487 | stream_clear_devices(alsa_devices); |
| 488 | |
| 489 | for (unsigned int i = 0; i < num_devices; ++i) { |
| 490 | struct alsa_device_info *device_info = |
| 491 | (struct alsa_device_info *) calloc(1, sizeof(struct alsa_device_info)); |
| 492 | profile_init(&device_info->profile, direction); |
| 493 | device_info->profile.card = cards[i]; |
| 494 | device_info->profile.device = devices[i]; |
| 495 | status = profile_read_device_info(&device_info->profile) ? 0 : -EINVAL; |
| 496 | if (status != 0) { |
| 497 | ALOGE("%s failed to read device info card=%d;device=%d", |
| 498 | __func__, cards[i], devices[i]); |
| 499 | goto exit; |
| 500 | } |
| 501 | status = proxy_prepare(&device_info->proxy, &device_info->profile, config); |
| 502 | if (status != 0) { |
| 503 | ALOGE("%s failed to prepare device card=%d;device=%d", |
| 504 | __func__, cards[i], devices[i]); |
| 505 | goto exit; |
| 506 | } |
| 507 | list_add_tail(alsa_devices, &device_info->list_node); |
| 508 | } |
| 509 | |
| 510 | exit: |
| 511 | if (status != 0) { |
| 512 | stream_clear_devices(alsa_devices); |
| 513 | } |
| 514 | return status; |
| 515 | } |
| 516 | |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 517 | static void stream_dump_alsa_devices(const struct listnode *alsa_devices, int fd) { |
| 518 | struct listnode *node; |
| 519 | size_t i = 0; |
| 520 | list_for_each(node, alsa_devices) { |
| 521 | struct alsa_device_info *device_info = |
| 522 | node_to_item(node, struct alsa_device_info, list_node); |
| 523 | dprintf(fd, "Output Profile %zu:\n", i); |
| 524 | profile_dump(&device_info->profile, fd); |
| 525 | |
| 526 | dprintf(fd, "Output Proxy %zu:\n", i); |
| 527 | proxy_dump(&device_info->proxy, fd); |
| 528 | } |
| 529 | } |
| 530 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 531 | /* |
| 532 | * OUT functions |
| 533 | */ |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 534 | static uint32_t out_get_sample_rate(const struct audio_stream *stream) |
| 535 | { |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 536 | struct alsa_device_info *device_info = stream_get_first_alsa_device( |
| 537 | &((struct stream_out*)stream)->alsa_devices); |
| 538 | if (device_info == NULL) { |
| 539 | ALOGW("%s device info is null", __func__); |
| 540 | return 0; |
| 541 | } |
| 542 | uint32_t rate = proxy_get_sample_rate(&device_info->proxy); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 543 | ALOGV("out_get_sample_rate() = %d", rate); |
| 544 | return rate; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate) |
| 548 | { |
| 549 | return 0; |
| 550 | } |
| 551 | |
| 552 | static size_t out_get_buffer_size(const struct audio_stream *stream) |
| 553 | { |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 554 | const struct stream_out* out = (const struct stream_out*)stream; |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 555 | const struct alsa_device_info* device_info = stream_get_first_alsa_device(&out->alsa_devices); |
| 556 | if (device_info == NULL) { |
| 557 | ALOGW("%s device info is null", __func__); |
| 558 | return 0; |
| 559 | } |
| 560 | return proxy_get_period_size(&device_info->proxy) * audio_stream_out_frame_size(&(out->stream)); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | static uint32_t out_get_channels(const struct audio_stream *stream) |
| 564 | { |
Andy Hung | 03576be | 2014-07-21 21:16:45 -0700 | [diff] [blame] | 565 | const struct stream_out *out = (const struct stream_out*)stream; |
Andy Hung | 182ddc7 | 2015-05-05 23:37:30 -0700 | [diff] [blame] | 566 | return out->hal_channel_mask; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | static audio_format_t out_get_format(const struct audio_stream *stream) |
| 570 | { |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 571 | /* Note: The HAL doesn't do any FORMAT conversion at this time. It |
| 572 | * Relies on the framework to provide data in the specified format. |
| 573 | * This could change in the future. |
| 574 | */ |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 575 | struct alsa_device_info *device_info = stream_get_first_alsa_device( |
| 576 | &((struct stream_out*)stream)->alsa_devices); |
| 577 | if (device_info == NULL) { |
| 578 | ALOGW("%s device info is null", __func__); |
| 579 | return AUDIO_FORMAT_DEFAULT; |
| 580 | } |
| 581 | audio_format_t format = audio_format_from_pcm_format(proxy_get_format(&device_info->proxy)); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 582 | return format; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | static int out_set_format(struct audio_stream *stream, audio_format_t format) |
| 586 | { |
| 587 | return 0; |
| 588 | } |
| 589 | |
| 590 | static int out_standby(struct audio_stream *stream) |
| 591 | { |
| 592 | struct stream_out *out = (struct stream_out *)stream; |
| 593 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 594 | stream_lock(&out->lock); |
jiabin | 400bbe0 | 2020-10-28 13:56:59 -0700 | [diff] [blame] | 595 | device_lock(out->adev); |
| 596 | stream_standby_l(&out->alsa_devices, &out->standby); |
| 597 | device_unlock(out->adev); |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 598 | stream_unlock(&out->lock); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 599 | return 0; |
| 600 | } |
| 601 | |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 602 | static int out_dump(const struct audio_stream *stream, int fd) { |
| 603 | const struct stream_out* out_stream = (const struct stream_out*) stream; |
| 604 | |
| 605 | if (out_stream != NULL) { |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 606 | stream_dump_alsa_devices(&out_stream->alsa_devices, fd); |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 607 | } |
| 608 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 609 | return 0; |
| 610 | } |
| 611 | |
jiabin | 400bbe0 | 2020-10-28 13:56:59 -0700 | [diff] [blame] | 612 | static int out_set_parameters(struct audio_stream *stream __unused, const char *kvpairs) |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 613 | { |
Paul McLean | 65ec72b | 2015-02-18 09:13:24 -0800 | [diff] [blame] | 614 | ALOGV("out_set_parameters() keys:%s", kvpairs); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 615 | |
jiabin | 400bbe0 | 2020-10-28 13:56:59 -0700 | [diff] [blame] | 616 | // The set parameters here only matters when the routing devices are changed. |
| 617 | // When the device version is not less than 3.0, the framework will use create |
| 618 | // audio patch API instead of set parameters to chanage audio routing. |
| 619 | return 0; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 620 | } |
| 621 | |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 622 | static char * out_get_parameters(const struct audio_stream *stream, const char *keys) |
| 623 | { |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 624 | struct stream_out *out = (struct stream_out *)stream; |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 625 | stream_lock(&out->lock); |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 626 | struct alsa_device_info *device_info = stream_get_first_alsa_device(&out->alsa_devices); |
| 627 | char *params_str = NULL; |
| 628 | if (device_info != NULL) { |
| 629 | params_str = device_get_parameters(&device_info->profile, keys); |
| 630 | } |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 631 | stream_unlock(&out->lock); |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 632 | return params_str; |
| 633 | } |
| 634 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 635 | static uint32_t out_get_latency(const struct audio_stream_out *stream) |
| 636 | { |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 637 | struct alsa_device_info *device_info = stream_get_first_alsa_device( |
| 638 | &((struct stream_out*)stream)->alsa_devices); |
| 639 | if (device_info == NULL) { |
| 640 | ALOGW("%s device info is null", __func__); |
| 641 | return 0; |
| 642 | } |
| 643 | return proxy_get_latency(&device_info->proxy); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 644 | } |
| 645 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 646 | static int out_set_volume(struct audio_stream_out *stream, float left, float right) |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 647 | { |
| 648 | return -ENOSYS; |
| 649 | } |
| 650 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 651 | /* must be called with hw device and output stream mutexes locked */ |
| 652 | static int start_output_stream(struct stream_out *out) |
| 653 | { |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 654 | int status = 0; |
| 655 | struct listnode *node; |
| 656 | list_for_each(node, &out->alsa_devices) { |
| 657 | struct alsa_device_info *device_info = |
| 658 | node_to_item(node, struct alsa_device_info, list_node); |
| 659 | ALOGV("start_output_stream(card:%d device:%d)", |
| 660 | device_info->profile.card, device_info->profile.device); |
| 661 | status = proxy_open(&device_info->proxy); |
| 662 | if (status != 0) { |
| 663 | ALOGE("%s failed to open device(card: %d device: %d)", |
| 664 | __func__, device_info->profile.card, device_info->profile.device); |
| 665 | goto exit; |
| 666 | } |
| 667 | } |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 668 | |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 669 | exit: |
| 670 | if (status != 0) { |
| 671 | list_for_each(node, &out->alsa_devices) { |
| 672 | struct alsa_device_info *device_info = |
| 673 | node_to_item(node, struct alsa_device_info, list_node); |
| 674 | proxy_close(&device_info->proxy); |
| 675 | } |
| 676 | |
| 677 | } |
| 678 | return status; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | static ssize_t out_write(struct audio_stream_out *stream, const void* buffer, size_t bytes) |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 682 | { |
| 683 | int ret; |
| 684 | struct stream_out *out = (struct stream_out *)stream; |
| 685 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 686 | stream_lock(&out->lock); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 687 | if (out->standby) { |
| 688 | ret = start_output_stream(out); |
| 689 | if (ret != 0) { |
| 690 | goto err; |
| 691 | } |
| 692 | out->standby = false; |
| 693 | } |
Eric Laurent | 05333d4 | 2014-08-04 20:29:17 -0700 | [diff] [blame] | 694 | |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 695 | struct listnode* node; |
| 696 | list_for_each(node, &out->alsa_devices) { |
| 697 | struct alsa_device_info* device_info = |
| 698 | node_to_item(node, struct alsa_device_info, list_node); |
| 699 | alsa_device_proxy* proxy = &device_info->proxy; |
| 700 | const void * write_buff = buffer; |
| 701 | int num_write_buff_bytes = bytes; |
| 702 | const int num_device_channels = proxy_get_channel_count(proxy); /* what we told alsa */ |
| 703 | const int num_req_channels = out->hal_channel_count; /* what we told AudioFlinger */ |
| 704 | if (num_device_channels != num_req_channels) { |
| 705 | /* allocate buffer */ |
| 706 | const size_t required_conversion_buffer_size = |
| 707 | bytes * num_device_channels / num_req_channels; |
| 708 | if (required_conversion_buffer_size > out->conversion_buffer_size) { |
| 709 | out->conversion_buffer_size = required_conversion_buffer_size; |
| 710 | out->conversion_buffer = realloc(out->conversion_buffer, |
| 711 | out->conversion_buffer_size); |
| 712 | } |
| 713 | /* convert data */ |
| 714 | const audio_format_t audio_format = out_get_format(&(out->stream.common)); |
| 715 | const unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format); |
| 716 | num_write_buff_bytes = |
| 717 | adjust_channels(write_buff, num_req_channels, |
| 718 | out->conversion_buffer, num_device_channels, |
| 719 | sample_size_in_bytes, num_write_buff_bytes); |
| 720 | write_buff = out->conversion_buffer; |
Andy Hung | 03576be | 2014-07-21 21:16:45 -0700 | [diff] [blame] | 721 | } |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 722 | |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 723 | if (write_buff != NULL && num_write_buff_bytes != 0) { |
| 724 | proxy_write(proxy, write_buff, num_write_buff_bytes); |
| 725 | } |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 726 | } |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 727 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 728 | stream_unlock(&out->lock); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 729 | |
| 730 | return bytes; |
| 731 | |
| 732 | err: |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 733 | stream_unlock(&out->lock); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 734 | if (ret != 0) { |
Eric Laurent | c5ae6a0 | 2014-07-02 13:45:32 -0700 | [diff] [blame] | 735 | usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) / |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 736 | out_get_sample_rate(&stream->common)); |
| 737 | } |
| 738 | |
| 739 | return bytes; |
| 740 | } |
| 741 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 742 | static int out_get_render_position(const struct audio_stream_out *stream, uint32_t *dsp_frames) |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 743 | { |
| 744 | return -EINVAL; |
| 745 | } |
| 746 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 747 | static int out_get_presentation_position(const struct audio_stream_out *stream, |
| 748 | uint64_t *frames, struct timespec *timestamp) |
| 749 | { |
Andy Hung | c9515ce | 2015-08-04 15:05:19 -0700 | [diff] [blame] | 750 | struct stream_out *out = (struct stream_out *)stream; // discard const qualifier |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 751 | stream_lock(&out->lock); |
Andy Hung | c9515ce | 2015-08-04 15:05:19 -0700 | [diff] [blame] | 752 | |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 753 | const struct alsa_device_info* device_info = stream_get_first_alsa_device(&out->alsa_devices); |
| 754 | const int ret = device_info == NULL ? -ENODEV : |
| 755 | proxy_get_presentation_position(&device_info->proxy, frames, timestamp); |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 756 | stream_unlock(&out->lock); |
Andy Hung | c9515ce | 2015-08-04 15:05:19 -0700 | [diff] [blame] | 757 | return ret; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 758 | } |
| 759 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 760 | static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 761 | { |
| 762 | return 0; |
| 763 | } |
| 764 | |
| 765 | static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 766 | { |
| 767 | return 0; |
| 768 | } |
| 769 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 770 | static int out_get_next_write_timestamp(const struct audio_stream_out *stream, int64_t *timestamp) |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 771 | { |
| 772 | return -EINVAL; |
| 773 | } |
| 774 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 775 | static int adev_open_output_stream(struct audio_hw_device *hw_dev, |
Mike Lockwood | 46a9809 | 2012-04-24 16:41:18 -0700 | [diff] [blame] | 776 | audio_io_handle_t handle, |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 777 | audio_devices_t devicesSpec __unused, |
Mike Lockwood | 46a9809 | 2012-04-24 16:41:18 -0700 | [diff] [blame] | 778 | audio_output_flags_t flags, |
| 779 | struct audio_config *config, |
Eric Laurent | f5e2469 | 2014-07-27 16:14:57 -0700 | [diff] [blame] | 780 | struct audio_stream_out **stream_out, |
Paul McLean | 0f1753e | 2014-12-02 12:36:45 -0700 | [diff] [blame] | 781 | const char *address /*__unused*/) |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 782 | { |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 783 | ALOGV("adev_open_output_stream() handle:0x%X, devicesSpec:0x%X, flags:0x%X, addr:%s", |
| 784 | handle, devicesSpec, flags, address); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 785 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 786 | struct stream_out *out; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 787 | |
| 788 | out = (struct stream_out *)calloc(1, sizeof(struct stream_out)); |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 789 | if (out == NULL) { |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 790 | return -ENOMEM; |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 791 | } |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 792 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 793 | /* setup function pointers */ |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 794 | out->stream.common.get_sample_rate = out_get_sample_rate; |
| 795 | out->stream.common.set_sample_rate = out_set_sample_rate; |
| 796 | out->stream.common.get_buffer_size = out_get_buffer_size; |
| 797 | out->stream.common.get_channels = out_get_channels; |
| 798 | out->stream.common.get_format = out_get_format; |
| 799 | out->stream.common.set_format = out_set_format; |
| 800 | out->stream.common.standby = out_standby; |
| 801 | out->stream.common.dump = out_dump; |
| 802 | out->stream.common.set_parameters = out_set_parameters; |
| 803 | out->stream.common.get_parameters = out_get_parameters; |
| 804 | out->stream.common.add_audio_effect = out_add_audio_effect; |
| 805 | out->stream.common.remove_audio_effect = out_remove_audio_effect; |
| 806 | out->stream.get_latency = out_get_latency; |
| 807 | out->stream.set_volume = out_set_volume; |
| 808 | out->stream.write = out_write; |
| 809 | out->stream.get_render_position = out_get_render_position; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 810 | out->stream.get_presentation_position = out_get_presentation_position; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 811 | out->stream.get_next_write_timestamp = out_get_next_write_timestamp; |
| 812 | |
jiabin | 400bbe0 | 2020-10-28 13:56:59 -0700 | [diff] [blame] | 813 | out->handle = handle; |
| 814 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 815 | stream_lock_init(&out->lock); |
Eric Laurent | 7031809 | 2015-06-19 17:49:17 -0700 | [diff] [blame] | 816 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 817 | out->adev = (struct audio_device *)hw_dev; |
Paul McLean | cf5310a | 2018-08-22 14:33:12 -0700 | [diff] [blame] | 818 | |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 819 | list_init(&out->alsa_devices); |
| 820 | struct alsa_device_info *device_info = |
| 821 | (struct alsa_device_info *)calloc(1, sizeof(struct alsa_device_info)); |
| 822 | profile_init(&device_info->profile, PCM_OUT); |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 823 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 824 | // build this to hand to the alsa_device_proxy |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 825 | struct pcm_config proxy_config = {}; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 826 | |
Paul McLean | 0f1753e | 2014-12-02 12:36:45 -0700 | [diff] [blame] | 827 | /* Pull out the card/device pair */ |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 828 | parse_card_device_params(address, &device_info->profile.card, &device_info->profile.device); |
Paul McLean | 0f1753e | 2014-12-02 12:36:45 -0700 | [diff] [blame] | 829 | |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 830 | profile_read_device_info(&device_info->profile); |
Paul McLean | 0f1753e | 2014-12-02 12:36:45 -0700 | [diff] [blame] | 831 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 832 | int ret = 0; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 833 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 834 | /* Rate */ |
| 835 | if (config->sample_rate == 0) { |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 836 | proxy_config.rate = profile_get_default_sample_rate(&device_info->profile); |
| 837 | } else if (profile_is_sample_rate_valid(&device_info->profile, config->sample_rate)) { |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 838 | proxy_config.rate = config->sample_rate; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 839 | } else { |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 840 | proxy_config.rate = config->sample_rate = |
| 841 | profile_get_default_sample_rate(&device_info->profile); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 842 | ret = -EINVAL; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 843 | } |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 844 | |
Paul McLean | cfdbd6b | 2018-07-27 11:16:02 -0600 | [diff] [blame] | 845 | /* TODO: This is a problem if the input does not support this rate */ |
Paul McLean | cf5310a | 2018-08-22 14:33:12 -0700 | [diff] [blame] | 846 | device_lock(out->adev); |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 847 | out->adev->device_sample_rate = config->sample_rate; |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 848 | device_unlock(out->adev); |
Paul McLean | 1d585cc | 2016-05-24 11:28:10 -0600 | [diff] [blame] | 849 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 850 | /* Format */ |
| 851 | if (config->format == AUDIO_FORMAT_DEFAULT) { |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 852 | proxy_config.format = profile_get_default_format(&device_info->profile); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 853 | config->format = audio_format_from_pcm_format(proxy_config.format); |
| 854 | } else { |
| 855 | enum pcm_format fmt = pcm_format_from_audio_format(config->format); |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 856 | if (profile_is_format_valid(&device_info->profile, fmt)) { |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 857 | proxy_config.format = fmt; |
| 858 | } else { |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 859 | proxy_config.format = profile_get_default_format(&device_info->profile); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 860 | config->format = audio_format_from_pcm_format(proxy_config.format); |
| 861 | ret = -EINVAL; |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | /* Channels */ |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 866 | bool calc_mask = false; |
| 867 | if (config->channel_mask == AUDIO_CHANNEL_NONE) { |
| 868 | /* query case */ |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 869 | out->hal_channel_count = profile_get_default_channel_count(&device_info->profile); |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 870 | calc_mask = true; |
Andy Hung | 182ddc7 | 2015-05-05 23:37:30 -0700 | [diff] [blame] | 871 | } else { |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 872 | /* explicit case */ |
| 873 | out->hal_channel_count = audio_channel_count_from_out_mask(config->channel_mask); |
Andy Hung | 182ddc7 | 2015-05-05 23:37:30 -0700 | [diff] [blame] | 874 | } |
Paul McLean | 698dbd7 | 2015-11-03 12:24:30 -0800 | [diff] [blame] | 875 | |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 876 | /* The Framework is currently limited to no more than this number of channels */ |
| 877 | if (out->hal_channel_count > FCC_8) { |
| 878 | out->hal_channel_count = FCC_8; |
| 879 | calc_mask = true; |
| 880 | } |
| 881 | |
| 882 | if (calc_mask) { |
| 883 | /* need to calculate the mask from channel count either because this is the query case |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 884 | * or the specified mask isn't valid for this device, or is more than the FW can handle */ |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 885 | config->channel_mask = out->hal_channel_count <= FCC_2 |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 886 | /* position mask for mono and stereo*/ |
| 887 | ? audio_channel_out_mask_from_count(out->hal_channel_count) |
| 888 | /* otherwise indexed */ |
| 889 | : audio_channel_mask_for_index_assignment_from_count(out->hal_channel_count); |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 890 | } |
| 891 | |
Andy Hung | 182ddc7 | 2015-05-05 23:37:30 -0700 | [diff] [blame] | 892 | out->hal_channel_mask = config->channel_mask; |
| 893 | |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 894 | // Validate the "logical" channel count against support in the "actual" profile. |
| 895 | // if they differ, choose the "actual" number of channels *closest* to the "logical". |
| 896 | // and store THAT in proxy_config.channels |
Paul McLean | cf5310a | 2018-08-22 14:33:12 -0700 | [diff] [blame] | 897 | proxy_config.channels = |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 898 | profile_get_closest_channel_count(&device_info->profile, out->hal_channel_count); |
| 899 | proxy_prepare(&device_info->proxy, &device_info->profile, &proxy_config); |
jiabin | 400bbe0 | 2020-10-28 13:56:59 -0700 | [diff] [blame] | 900 | out->config = proxy_config; |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 901 | |
| 902 | list_add_tail(&out->alsa_devices, &device_info->list_node); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 903 | |
Paul McLean | 96c4d30 | 2017-12-05 09:50:26 -0800 | [diff] [blame] | 904 | /* TODO The retry mechanism isn't implemented in AudioPolicyManager/AudioFlinger |
| 905 | * So clear any errors that may have occurred above. |
| 906 | */ |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 907 | ret = 0; |
| 908 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 909 | out->conversion_buffer = NULL; |
| 910 | out->conversion_buffer_size = 0; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 911 | |
| 912 | out->standby = true; |
| 913 | |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 914 | /* Save the stream for adev_dump() */ |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 915 | adev_add_stream_to_list(out->adev, &out->adev->output_stream_list, &out->list_node); |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 916 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 917 | *stream_out = &out->stream; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 918 | |
| 919 | return ret; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 920 | } |
| 921 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 922 | static void adev_close_output_stream(struct audio_hw_device *hw_dev, |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 923 | struct audio_stream_out *stream) |
| 924 | { |
| 925 | struct stream_out *out = (struct stream_out *)stream; |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 926 | |
jiabin | 400bbe0 | 2020-10-28 13:56:59 -0700 | [diff] [blame] | 927 | stream_lock(&out->lock); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 928 | /* Close the pcm device */ |
jiabin | 400bbe0 | 2020-10-28 13:56:59 -0700 | [diff] [blame] | 929 | stream_standby_l(&out->alsa_devices, &out->standby); |
| 930 | stream_clear_devices(&out->alsa_devices); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 931 | |
| 932 | free(out->conversion_buffer); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 933 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 934 | out->conversion_buffer = NULL; |
| 935 | out->conversion_buffer_size = 0; |
| 936 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 937 | device_lock(out->adev); |
jiabin | 400bbe0 | 2020-10-28 13:56:59 -0700 | [diff] [blame] | 938 | list_remove(&out->list_node); |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 939 | out->adev->device_sample_rate = 0; |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 940 | device_unlock(out->adev); |
jiabin | 400bbe0 | 2020-10-28 13:56:59 -0700 | [diff] [blame] | 941 | stream_unlock(&out->lock); |
Paul McLean | 1d585cc | 2016-05-24 11:28:10 -0600 | [diff] [blame] | 942 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 943 | free(stream); |
| 944 | } |
| 945 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 946 | static size_t adev_get_input_buffer_size(const struct audio_hw_device *hw_dev, |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 947 | const struct audio_config *config) |
| 948 | { |
| 949 | /* TODO This needs to be calculated based on format/channels/rate */ |
| 950 | return 320; |
| 951 | } |
| 952 | |
| 953 | /* |
| 954 | * IN functions |
| 955 | */ |
| 956 | static uint32_t in_get_sample_rate(const struct audio_stream *stream) |
| 957 | { |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 958 | struct alsa_device_info *device_info = stream_get_first_alsa_device( |
| 959 | &((const struct stream_in *)stream)->alsa_devices); |
| 960 | if (device_info == NULL) { |
| 961 | ALOGW("%s device info is null", __func__); |
| 962 | return 0; |
| 963 | } |
| 964 | uint32_t rate = proxy_get_sample_rate(&device_info->proxy); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 965 | ALOGV("in_get_sample_rate() = %d", rate); |
| 966 | return rate; |
| 967 | } |
| 968 | |
| 969 | static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate) |
| 970 | { |
| 971 | ALOGV("in_set_sample_rate(%d) - NOPE", rate); |
| 972 | return -ENOSYS; |
| 973 | } |
| 974 | |
| 975 | static size_t in_get_buffer_size(const struct audio_stream *stream) |
| 976 | { |
| 977 | const struct stream_in * in = ((const struct stream_in*)stream); |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 978 | struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices); |
| 979 | if (device_info == NULL) { |
| 980 | ALOGW("%s device info is null", __func__); |
| 981 | return 0; |
| 982 | } |
| 983 | return proxy_get_period_size(&device_info->proxy) * audio_stream_in_frame_size(&(in->stream)); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | static uint32_t in_get_channels(const struct audio_stream *stream) |
| 987 | { |
Paul McLean | 2cfd81b | 2014-09-15 12:32:23 -0700 | [diff] [blame] | 988 | const struct stream_in *in = (const struct stream_in*)stream; |
Andy Hung | 780f1f8 | 2015-04-22 22:14:39 -0700 | [diff] [blame] | 989 | return in->hal_channel_mask; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | static audio_format_t in_get_format(const struct audio_stream *stream) |
| 993 | { |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 994 | struct alsa_device_info *device_info = stream_get_first_alsa_device( |
| 995 | &((const struct stream_in *)stream)->alsa_devices); |
| 996 | if (device_info == NULL) { |
| 997 | ALOGW("%s device info is null", __func__); |
| 998 | return AUDIO_FORMAT_DEFAULT; |
| 999 | } |
| 1000 | alsa_device_proxy *proxy = &device_info->proxy; |
Andy Hung | 780f1f8 | 2015-04-22 22:14:39 -0700 | [diff] [blame] | 1001 | audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy)); |
| 1002 | return format; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | static int in_set_format(struct audio_stream *stream, audio_format_t format) |
| 1006 | { |
| 1007 | ALOGV("in_set_format(%d) - NOPE", format); |
| 1008 | |
| 1009 | return -ENOSYS; |
| 1010 | } |
| 1011 | |
| 1012 | static int in_standby(struct audio_stream *stream) |
| 1013 | { |
| 1014 | struct stream_in *in = (struct stream_in *)stream; |
| 1015 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 1016 | stream_lock(&in->lock); |
jiabin | 400bbe0 | 2020-10-28 13:56:59 -0700 | [diff] [blame] | 1017 | device_lock(in->adev); |
| 1018 | stream_standby_l(&in->alsa_devices, &in->standby); |
| 1019 | device_unlock(in->adev); |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 1020 | stream_unlock(&in->lock); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1021 | return 0; |
| 1022 | } |
| 1023 | |
| 1024 | static int in_dump(const struct audio_stream *stream, int fd) |
| 1025 | { |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 1026 | const struct stream_in* in_stream = (const struct stream_in*)stream; |
| 1027 | if (in_stream != NULL) { |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 1028 | stream_dump_alsa_devices(&in_stream->alsa_devices, fd); |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | return 0; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | static int in_set_parameters(struct audio_stream *stream, const char *kvpairs) |
| 1035 | { |
Paul McLean | 65ec72b | 2015-02-18 09:13:24 -0800 | [diff] [blame] | 1036 | ALOGV("in_set_parameters() keys:%s", kvpairs); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1037 | |
jiabin | 400bbe0 | 2020-10-28 13:56:59 -0700 | [diff] [blame] | 1038 | // The set parameters here only matters when the routing devices are changed. |
| 1039 | // When the device version higher than 3.0, the framework will use create_audio_patch |
| 1040 | // API instead of set_parameters to change audio routing. |
| 1041 | return 0; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | static char * in_get_parameters(const struct audio_stream *stream, const char *keys) |
| 1045 | { |
| 1046 | struct stream_in *in = (struct stream_in *)stream; |
| 1047 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 1048 | stream_lock(&in->lock); |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 1049 | struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices); |
| 1050 | char *params_str = NULL; |
| 1051 | if (device_info != NULL) { |
| 1052 | params_str = device_get_parameters(&device_info->profile, keys); |
| 1053 | } |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 1054 | stream_unlock(&in->lock); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1055 | |
| 1056 | return params_str; |
| 1057 | } |
| 1058 | |
| 1059 | static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 1060 | { |
| 1061 | return 0; |
| 1062 | } |
| 1063 | |
| 1064 | static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 1065 | { |
| 1066 | return 0; |
| 1067 | } |
| 1068 | |
| 1069 | static int in_set_gain(struct audio_stream_in *stream, float gain) |
| 1070 | { |
| 1071 | return 0; |
| 1072 | } |
| 1073 | |
| 1074 | /* must be called with hw device and output stream mutexes locked */ |
| 1075 | static int start_input_stream(struct stream_in *in) |
| 1076 | { |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 1077 | // Only care about the first device as only one input device is allowed. |
| 1078 | struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices); |
| 1079 | if (device_info == NULL) { |
| 1080 | return -ENODEV; |
| 1081 | } |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1082 | |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 1083 | ALOGV("start_input_stream(card:%d device:%d)", |
| 1084 | device_info->profile.card, device_info->profile.device); |
| 1085 | return proxy_open(&device_info->proxy); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1086 | } |
| 1087 | |
| 1088 | /* TODO mutex stuff here (see out_write) */ |
| 1089 | static ssize_t in_read(struct audio_stream_in *stream, void* buffer, size_t bytes) |
| 1090 | { |
| 1091 | size_t num_read_buff_bytes = 0; |
| 1092 | void * read_buff = buffer; |
| 1093 | void * out_buff = buffer; |
Pavan Chikkala | 83b47a6 | 2015-01-09 12:21:13 -0800 | [diff] [blame] | 1094 | int ret = 0; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1095 | |
| 1096 | struct stream_in * in = (struct stream_in *)stream; |
| 1097 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 1098 | stream_lock(&in->lock); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1099 | if (in->standby) { |
Eric Laurent | 7031809 | 2015-06-19 17:49:17 -0700 | [diff] [blame] | 1100 | ret = start_input_stream(in); |
Eric Laurent | 7031809 | 2015-06-19 17:49:17 -0700 | [diff] [blame] | 1101 | if (ret != 0) { |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1102 | goto err; |
| 1103 | } |
| 1104 | in->standby = false; |
| 1105 | } |
| 1106 | |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 1107 | // Only care about the first device as only one input device is allowed. |
| 1108 | struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices); |
| 1109 | if (device_info == NULL) { |
| 1110 | return 0; |
| 1111 | } |
| 1112 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1113 | /* |
| 1114 | * OK, we need to figure out how much data to read to be able to output the requested |
| 1115 | * number of bytes in the HAL format (16-bit, stereo). |
| 1116 | */ |
| 1117 | num_read_buff_bytes = bytes; |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 1118 | int num_device_channels = proxy_get_channel_count(&device_info->proxy); /* what we told Alsa */ |
Andy Hung | 780f1f8 | 2015-04-22 22:14:39 -0700 | [diff] [blame] | 1119 | int num_req_channels = in->hal_channel_count; /* what we told AudioFlinger */ |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1120 | |
| 1121 | if (num_device_channels != num_req_channels) { |
| 1122 | num_read_buff_bytes = (num_device_channels * num_read_buff_bytes) / num_req_channels; |
| 1123 | } |
| 1124 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1125 | /* Setup/Realloc the conversion buffer (if necessary). */ |
| 1126 | if (num_read_buff_bytes != bytes) { |
| 1127 | if (num_read_buff_bytes > in->conversion_buffer_size) { |
| 1128 | /*TODO Remove this when AudioPolicyManger/AudioFlinger support arbitrary formats |
| 1129 | (and do these conversions themselves) */ |
| 1130 | in->conversion_buffer_size = num_read_buff_bytes; |
| 1131 | in->conversion_buffer = realloc(in->conversion_buffer, in->conversion_buffer_size); |
| 1132 | } |
| 1133 | read_buff = in->conversion_buffer; |
| 1134 | } |
| 1135 | |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 1136 | ret = proxy_read(&device_info->proxy, read_buff, num_read_buff_bytes); |
Pavan Chikkala | 83b47a6 | 2015-01-09 12:21:13 -0800 | [diff] [blame] | 1137 | if (ret == 0) { |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1138 | if (num_device_channels != num_req_channels) { |
| 1139 | // ALOGV("chans dev:%d req:%d", num_device_channels, num_req_channels); |
| 1140 | |
| 1141 | out_buff = buffer; |
| 1142 | /* Num Channels conversion */ |
| 1143 | if (num_device_channels != num_req_channels) { |
| 1144 | audio_format_t audio_format = in_get_format(&(in->stream.common)); |
| 1145 | unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format); |
| 1146 | |
| 1147 | num_read_buff_bytes = |
| 1148 | adjust_channels(read_buff, num_device_channels, |
| 1149 | out_buff, num_req_channels, |
| 1150 | sample_size_in_bytes, num_read_buff_bytes); |
| 1151 | } |
| 1152 | } |
Eric Laurent | 253def9 | 2014-09-14 12:18:18 -0700 | [diff] [blame] | 1153 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 1154 | /* no need to acquire in->adev->lock to read mic_muted here as we don't change its state */ |
| 1155 | if (num_read_buff_bytes > 0 && in->adev->mic_muted) |
Eric Laurent | 253def9 | 2014-09-14 12:18:18 -0700 | [diff] [blame] | 1156 | memset(buffer, 0, num_read_buff_bytes); |
Viswanath L | 8c7e111 | 2014-10-31 13:07:39 +0530 | [diff] [blame] | 1157 | } else { |
Eric Laurent | e649942 | 2015-01-09 17:03:27 -0800 | [diff] [blame] | 1158 | num_read_buff_bytes = 0; // reset the value after USB headset is unplugged |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | err: |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 1162 | stream_unlock(&in->lock); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1163 | return num_read_buff_bytes; |
| 1164 | } |
| 1165 | |
| 1166 | static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream) |
| 1167 | { |
| 1168 | return 0; |
| 1169 | } |
| 1170 | |
Andy Hung | fa6b4a6 | 2018-06-04 19:14:22 -0700 | [diff] [blame] | 1171 | static int in_get_capture_position(const struct audio_stream_in *stream, |
| 1172 | int64_t *frames, int64_t *time) |
| 1173 | { |
| 1174 | struct stream_in *in = (struct stream_in *)stream; // discard const qualifier |
| 1175 | stream_lock(&in->lock); |
| 1176 | |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 1177 | struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices); |
| 1178 | |
| 1179 | const int ret = device_info == NULL ? -ENODEV |
| 1180 | : proxy_get_capture_position(&device_info->proxy, frames, time); |
Andy Hung | fa6b4a6 | 2018-06-04 19:14:22 -0700 | [diff] [blame] | 1181 | |
| 1182 | stream_unlock(&in->lock); |
| 1183 | return ret; |
| 1184 | } |
| 1185 | |
Paul McLean | fa3ae3e | 2018-12-12 09:57:02 -0800 | [diff] [blame] | 1186 | static int in_get_active_microphones(const struct audio_stream_in *stream, |
| 1187 | struct audio_microphone_characteristic_t *mic_array, |
| 1188 | size_t *mic_count) { |
| 1189 | (void)stream; |
| 1190 | (void)mic_array; |
| 1191 | (void)mic_count; |
| 1192 | |
| 1193 | return -ENOSYS; |
| 1194 | } |
| 1195 | |
| 1196 | static int in_set_microphone_direction(const struct audio_stream_in *stream, |
| 1197 | audio_microphone_direction_t dir) { |
| 1198 | (void)stream; |
| 1199 | (void)dir; |
| 1200 | ALOGV("---- in_set_microphone_direction()"); |
| 1201 | return -ENOSYS; |
| 1202 | } |
| 1203 | |
| 1204 | static int in_set_microphone_field_dimension(const struct audio_stream_in *stream, float zoom) { |
| 1205 | (void)zoom; |
| 1206 | ALOGV("---- in_set_microphone_field_dimension()"); |
| 1207 | return -ENOSYS; |
| 1208 | } |
| 1209 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 1210 | static int adev_open_input_stream(struct audio_hw_device *hw_dev, |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1211 | audio_io_handle_t handle, |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 1212 | audio_devices_t devicesSpec __unused, |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1213 | struct audio_config *config, |
| 1214 | struct audio_stream_in **stream_in, |
Eric Laurent | f5e2469 | 2014-07-27 16:14:57 -0700 | [diff] [blame] | 1215 | audio_input_flags_t flags __unused, |
Eric Laurent | 981f774 | 2017-05-03 12:44:26 -0700 | [diff] [blame] | 1216 | const char *address, |
Eric Laurent | f5e2469 | 2014-07-27 16:14:57 -0700 | [diff] [blame] | 1217 | audio_source_t source __unused) |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1218 | { |
Paul McLean | 1d585cc | 2016-05-24 11:28:10 -0600 | [diff] [blame] | 1219 | ALOGV("adev_open_input_stream() rate:%" PRIu32 ", chanMask:0x%" PRIX32 ", fmt:%" PRIu8, |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1220 | config->sample_rate, config->channel_mask, config->format); |
| 1221 | |
Andy Hung | b10ce6d | 2017-10-27 19:37:58 -0700 | [diff] [blame] | 1222 | /* Pull out the card/device pair */ |
| 1223 | int32_t card, device; |
| 1224 | if (!parse_card_device_params(address, &card, &device)) { |
| 1225 | ALOGW("%s fail - invalid address %s", __func__, address); |
| 1226 | *stream_in = NULL; |
| 1227 | return -EINVAL; |
| 1228 | } |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1229 | |
Andy Hung | b10ce6d | 2017-10-27 19:37:58 -0700 | [diff] [blame] | 1230 | struct stream_in * const in = (struct stream_in *)calloc(1, sizeof(struct stream_in)); |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 1231 | if (in == NULL) { |
Andy Hung | b10ce6d | 2017-10-27 19:37:58 -0700 | [diff] [blame] | 1232 | *stream_in = NULL; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1233 | return -ENOMEM; |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 1234 | } |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1235 | |
| 1236 | /* setup function pointers */ |
| 1237 | in->stream.common.get_sample_rate = in_get_sample_rate; |
| 1238 | in->stream.common.set_sample_rate = in_set_sample_rate; |
| 1239 | in->stream.common.get_buffer_size = in_get_buffer_size; |
| 1240 | in->stream.common.get_channels = in_get_channels; |
| 1241 | in->stream.common.get_format = in_get_format; |
| 1242 | in->stream.common.set_format = in_set_format; |
| 1243 | in->stream.common.standby = in_standby; |
| 1244 | in->stream.common.dump = in_dump; |
| 1245 | in->stream.common.set_parameters = in_set_parameters; |
| 1246 | in->stream.common.get_parameters = in_get_parameters; |
| 1247 | in->stream.common.add_audio_effect = in_add_audio_effect; |
| 1248 | in->stream.common.remove_audio_effect = in_remove_audio_effect; |
| 1249 | |
| 1250 | in->stream.set_gain = in_set_gain; |
| 1251 | in->stream.read = in_read; |
| 1252 | in->stream.get_input_frames_lost = in_get_input_frames_lost; |
Andy Hung | fa6b4a6 | 2018-06-04 19:14:22 -0700 | [diff] [blame] | 1253 | in->stream.get_capture_position = in_get_capture_position; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1254 | |
Paul McLean | fa3ae3e | 2018-12-12 09:57:02 -0800 | [diff] [blame] | 1255 | in->stream.get_active_microphones = in_get_active_microphones; |
| 1256 | in->stream.set_microphone_direction = in_set_microphone_direction; |
| 1257 | in->stream.set_microphone_field_dimension = in_set_microphone_field_dimension; |
| 1258 | |
jiabin | 400bbe0 | 2020-10-28 13:56:59 -0700 | [diff] [blame] | 1259 | in->handle = handle; |
| 1260 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 1261 | stream_lock_init(&in->lock); |
Eric Laurent | 7031809 | 2015-06-19 17:49:17 -0700 | [diff] [blame] | 1262 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 1263 | in->adev = (struct audio_device *)hw_dev; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1264 | |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 1265 | list_init(&in->alsa_devices); |
| 1266 | struct alsa_device_info *device_info = |
| 1267 | (struct alsa_device_info *)calloc(1, sizeof(struct alsa_device_info)); |
| 1268 | profile_init(&device_info->profile, PCM_IN); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1269 | |
jiabin | 400bbe0 | 2020-10-28 13:56:59 -0700 | [diff] [blame] | 1270 | memset(&in->config, 0, sizeof(in->config)); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1271 | |
Andy Hung | b10ce6d | 2017-10-27 19:37:58 -0700 | [diff] [blame] | 1272 | int ret = 0; |
Paul McLean | cf5310a | 2018-08-22 14:33:12 -0700 | [diff] [blame] | 1273 | device_lock(in->adev); |
| 1274 | int num_open_inputs = in->adev->inputs_open; |
| 1275 | device_unlock(in->adev); |
| 1276 | |
Andy Hung | b10ce6d | 2017-10-27 19:37:58 -0700 | [diff] [blame] | 1277 | /* Check if an input stream is already open */ |
Paul McLean | cf5310a | 2018-08-22 14:33:12 -0700 | [diff] [blame] | 1278 | if (num_open_inputs > 0) { |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 1279 | if (!profile_is_cached_for(&device_info->profile, card, device)) { |
Andy Hung | b10ce6d | 2017-10-27 19:37:58 -0700 | [diff] [blame] | 1280 | ALOGW("%s fail - address card:%d device:%d doesn't match existing profile", |
| 1281 | __func__, card, device); |
| 1282 | ret = -EINVAL; |
| 1283 | } |
| 1284 | } else { |
| 1285 | /* Read input profile only if necessary */ |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 1286 | device_info->profile.card = card; |
| 1287 | device_info->profile.device = device; |
| 1288 | if (!profile_read_device_info(&device_info->profile)) { |
Andy Hung | b10ce6d | 2017-10-27 19:37:58 -0700 | [diff] [blame] | 1289 | ALOGW("%s fail - cannot read profile", __func__); |
| 1290 | ret = -EINVAL; |
| 1291 | } |
| 1292 | } |
| 1293 | if (ret != 0) { |
Andy Hung | b10ce6d | 2017-10-27 19:37:58 -0700 | [diff] [blame] | 1294 | free(in); |
| 1295 | *stream_in = NULL; |
| 1296 | return ret; |
| 1297 | } |
Paul McLean | 0f1753e | 2014-12-02 12:36:45 -0700 | [diff] [blame] | 1298 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1299 | /* Rate */ |
Paul McLean | cfdbd6b | 2018-07-27 11:16:02 -0600 | [diff] [blame] | 1300 | int request_config_rate = config->sample_rate; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1301 | if (config->sample_rate == 0) { |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 1302 | config->sample_rate = profile_get_default_sample_rate(&device_info->profile); |
Paul McLean | 9a1c305 | 2016-05-25 13:30:54 -0600 | [diff] [blame] | 1303 | } |
| 1304 | |
Paul McLean | cfdbd6b | 2018-07-27 11:16:02 -0600 | [diff] [blame] | 1305 | if (in->adev->device_sample_rate != 0 && /* we are playing, so lock the rate if possible */ |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 1306 | in->adev->device_sample_rate >= RATELOCK_THRESHOLD) {/* but only for high sample rates */ |
Paul McLean | cfdbd6b | 2018-07-27 11:16:02 -0600 | [diff] [blame] | 1307 | if (config->sample_rate != in->adev->device_sample_rate) { |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 1308 | unsigned highest_rate = profile_get_highest_sample_rate(&device_info->profile); |
Paul McLean | cfdbd6b | 2018-07-27 11:16:02 -0600 | [diff] [blame] | 1309 | if (highest_rate == 0) { |
| 1310 | ret = -EINVAL; /* error with device */ |
| 1311 | } else { |
jiabin | 400bbe0 | 2020-10-28 13:56:59 -0700 | [diff] [blame] | 1312 | in->config.rate = config->sample_rate = |
Paul McLean | cfdbd6b | 2018-07-27 11:16:02 -0600 | [diff] [blame] | 1313 | min(highest_rate, in->adev->device_sample_rate); |
jiabin | 400bbe0 | 2020-10-28 13:56:59 -0700 | [diff] [blame] | 1314 | if (request_config_rate != 0 && in->config.rate != config->sample_rate) { |
Paul McLean | cfdbd6b | 2018-07-27 11:16:02 -0600 | [diff] [blame] | 1315 | /* Changing the requested rate */ |
| 1316 | ret = -EINVAL; |
| 1317 | } else { |
| 1318 | /* Everything AOK! */ |
| 1319 | ret = 0; |
| 1320 | } |
| 1321 | } |
| 1322 | } |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 1323 | } else if (profile_is_sample_rate_valid(&device_info->profile, config->sample_rate)) { |
jiabin | 400bbe0 | 2020-10-28 13:56:59 -0700 | [diff] [blame] | 1324 | in->config.rate = config->sample_rate; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1325 | } else { |
jiabin | 400bbe0 | 2020-10-28 13:56:59 -0700 | [diff] [blame] | 1326 | in->config.rate = config->sample_rate = |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 1327 | profile_get_default_sample_rate(&device_info->profile); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1328 | ret = -EINVAL; |
| 1329 | } |
Paul McLean | cfdbd6b | 2018-07-27 11:16:02 -0600 | [diff] [blame] | 1330 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1331 | /* Format */ |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1332 | if (config->format == AUDIO_FORMAT_DEFAULT) { |
jiabin | 400bbe0 | 2020-10-28 13:56:59 -0700 | [diff] [blame] | 1333 | in->config.format = profile_get_default_format(&device_info->profile); |
| 1334 | config->format = audio_format_from_pcm_format(in->config.format); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1335 | } else { |
Andy Hung | 780f1f8 | 2015-04-22 22:14:39 -0700 | [diff] [blame] | 1336 | enum pcm_format fmt = pcm_format_from_audio_format(config->format); |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 1337 | if (profile_is_format_valid(&device_info->profile, fmt)) { |
jiabin | 400bbe0 | 2020-10-28 13:56:59 -0700 | [diff] [blame] | 1338 | in->config.format = fmt; |
Andy Hung | 780f1f8 | 2015-04-22 22:14:39 -0700 | [diff] [blame] | 1339 | } else { |
jiabin | 400bbe0 | 2020-10-28 13:56:59 -0700 | [diff] [blame] | 1340 | in->config.format = profile_get_default_format(&device_info->profile); |
| 1341 | config->format = audio_format_from_pcm_format(in->config.format); |
Andy Hung | 780f1f8 | 2015-04-22 22:14:39 -0700 | [diff] [blame] | 1342 | ret = -EINVAL; |
| 1343 | } |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1344 | } |
| 1345 | |
Paul McLean | 2cfd81b | 2014-09-15 12:32:23 -0700 | [diff] [blame] | 1346 | /* Channels */ |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 1347 | bool calc_mask = false; |
| 1348 | if (config->channel_mask == AUDIO_CHANNEL_NONE) { |
| 1349 | /* query case */ |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 1350 | in->hal_channel_count = profile_get_default_channel_count(&device_info->profile); |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 1351 | calc_mask = true; |
Andy Hung | 780f1f8 | 2015-04-22 22:14:39 -0700 | [diff] [blame] | 1352 | } else { |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 1353 | /* explicit case */ |
Andy Hung | 780f1f8 | 2015-04-22 22:14:39 -0700 | [diff] [blame] | 1354 | in->hal_channel_count = audio_channel_count_from_in_mask(config->channel_mask); |
| 1355 | } |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1356 | |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 1357 | /* The Framework is currently limited to no more than this number of channels */ |
| 1358 | if (in->hal_channel_count > FCC_8) { |
| 1359 | in->hal_channel_count = FCC_8; |
| 1360 | calc_mask = true; |
| 1361 | } |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1362 | |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 1363 | if (calc_mask) { |
| 1364 | /* need to calculate the mask from channel count either because this is the query case |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 1365 | * or the specified mask isn't valid for this device, or is more than the FW can handle */ |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 1366 | in->hal_channel_mask = in->hal_channel_count <= FCC_2 |
| 1367 | /* position mask for mono & stereo */ |
| 1368 | ? audio_channel_in_mask_from_count(in->hal_channel_count) |
| 1369 | /* otherwise indexed */ |
| 1370 | : audio_channel_mask_for_index_assignment_from_count(in->hal_channel_count); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1371 | |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 1372 | // if we change the mask... |
| 1373 | if (in->hal_channel_mask != config->channel_mask && |
| 1374 | config->channel_mask != AUDIO_CHANNEL_NONE) { |
| 1375 | config->channel_mask = in->hal_channel_mask; |
| 1376 | ret = -EINVAL; |
| 1377 | } |
| 1378 | } else { |
| 1379 | in->hal_channel_mask = config->channel_mask; |
| 1380 | } |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 1381 | |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 1382 | if (ret == 0) { |
| 1383 | // Validate the "logical" channel count against support in the "actual" profile. |
| 1384 | // if they differ, choose the "actual" number of channels *closest* to the "logical". |
| 1385 | // and store THAT in proxy_config.channels |
jiabin | 400bbe0 | 2020-10-28 13:56:59 -0700 | [diff] [blame] | 1386 | in->config.channels = |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 1387 | profile_get_closest_channel_count(&device_info->profile, in->hal_channel_count); |
jiabin | 400bbe0 | 2020-10-28 13:56:59 -0700 | [diff] [blame] | 1388 | ret = proxy_prepare(&device_info->proxy, &device_info->profile, &in->config); |
Eric Laurent | 981f774 | 2017-05-03 12:44:26 -0700 | [diff] [blame] | 1389 | if (ret == 0) { |
| 1390 | in->standby = true; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1391 | |
Eric Laurent | 981f774 | 2017-05-03 12:44:26 -0700 | [diff] [blame] | 1392 | in->conversion_buffer = NULL; |
| 1393 | in->conversion_buffer_size = 0; |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 1394 | |
Eric Laurent | 981f774 | 2017-05-03 12:44:26 -0700 | [diff] [blame] | 1395 | *stream_in = &in->stream; |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 1396 | |
Eric Laurent | 981f774 | 2017-05-03 12:44:26 -0700 | [diff] [blame] | 1397 | /* Save this for adev_dump() */ |
| 1398 | adev_add_stream_to_list(in->adev, &in->adev->input_stream_list, &in->list_node); |
| 1399 | } else { |
| 1400 | ALOGW("proxy_prepare error %d", ret); |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 1401 | unsigned channel_count = proxy_get_channel_count(&device_info->proxy); |
Eric Laurent | 981f774 | 2017-05-03 12:44:26 -0700 | [diff] [blame] | 1402 | config->channel_mask = channel_count <= FCC_2 |
| 1403 | ? audio_channel_in_mask_from_count(channel_count) |
| 1404 | : audio_channel_mask_for_index_assignment_from_count(channel_count); |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 1405 | config->format = audio_format_from_pcm_format(proxy_get_format(&device_info->proxy)); |
| 1406 | config->sample_rate = proxy_get_sample_rate(&device_info->proxy); |
Eric Laurent | 981f774 | 2017-05-03 12:44:26 -0700 | [diff] [blame] | 1407 | } |
| 1408 | } |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 1409 | |
Eric Laurent | 981f774 | 2017-05-03 12:44:26 -0700 | [diff] [blame] | 1410 | if (ret != 0) { |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 1411 | // Deallocate this stream on error, because AudioFlinger won't call |
| 1412 | // adev_close_input_stream() in this case. |
| 1413 | *stream_in = NULL; |
| 1414 | free(in); |
Mikhail Naganov | 1d08a56 | 2020-03-26 13:18:12 -0700 | [diff] [blame] | 1415 | return ret; |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 1416 | } |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1417 | |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 1418 | list_add_tail(&in->alsa_devices, &device_info->list_node); |
| 1419 | |
Andy Hung | b10ce6d | 2017-10-27 19:37:58 -0700 | [diff] [blame] | 1420 | device_lock(in->adev); |
| 1421 | ++in->adev->inputs_open; |
| 1422 | device_unlock(in->adev); |
| 1423 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1424 | return ret; |
| 1425 | } |
| 1426 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 1427 | static void adev_close_input_stream(struct audio_hw_device *hw_dev, |
| 1428 | struct audio_stream_in *stream) |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1429 | { |
| 1430 | struct stream_in *in = (struct stream_in *)stream; |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 1431 | |
jiabin | 400bbe0 | 2020-10-28 13:56:59 -0700 | [diff] [blame] | 1432 | stream_lock(&in->lock); |
Andy Hung | b10ce6d | 2017-10-27 19:37:58 -0700 | [diff] [blame] | 1433 | device_lock(in->adev); |
jiabin | 400bbe0 | 2020-10-28 13:56:59 -0700 | [diff] [blame] | 1434 | list_remove(&in->list_node); |
Andy Hung | b10ce6d | 2017-10-27 19:37:58 -0700 | [diff] [blame] | 1435 | --in->adev->inputs_open; |
jiabin | 8b265c9 | 2020-10-21 15:16:40 -0700 | [diff] [blame] | 1436 | struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices); |
| 1437 | if (device_info != NULL) { |
| 1438 | ALOGV("adev_close_input_stream(c:%d d:%d)", |
| 1439 | device_info->profile.card, device_info->profile.device); |
| 1440 | } |
Andy Hung | b10ce6d | 2017-10-27 19:37:58 -0700 | [diff] [blame] | 1441 | LOG_ALWAYS_FATAL_IF(in->adev->inputs_open < 0, |
| 1442 | "invalid inputs_open: %d", in->adev->inputs_open); |
jiabin | 400bbe0 | 2020-10-28 13:56:59 -0700 | [diff] [blame] | 1443 | |
| 1444 | stream_standby_l(&in->alsa_devices, &in->standby); |
| 1445 | |
Andy Hung | b10ce6d | 2017-10-27 19:37:58 -0700 | [diff] [blame] | 1446 | device_unlock(in->adev); |
| 1447 | |
jiabin | 400bbe0 | 2020-10-28 13:56:59 -0700 | [diff] [blame] | 1448 | stream_clear_devices(&in->alsa_devices); |
| 1449 | stream_unlock(&in->lock); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1450 | |
| 1451 | free(in->conversion_buffer); |
| 1452 | |
| 1453 | free(stream); |
| 1454 | } |
| 1455 | |
| 1456 | /* |
| 1457 | * ADEV Functions |
| 1458 | */ |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 1459 | static int adev_set_parameters(struct audio_hw_device *hw_dev, const char *kvpairs) |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1460 | { |
| 1461 | return 0; |
| 1462 | } |
| 1463 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 1464 | static char * adev_get_parameters(const struct audio_hw_device *hw_dev, const char *keys) |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1465 | { |
| 1466 | return strdup(""); |
| 1467 | } |
| 1468 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 1469 | static int adev_init_check(const struct audio_hw_device *hw_dev) |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1470 | { |
| 1471 | return 0; |
| 1472 | } |
| 1473 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 1474 | static int adev_set_voice_volume(struct audio_hw_device *hw_dev, float volume) |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1475 | { |
| 1476 | return -ENOSYS; |
| 1477 | } |
| 1478 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 1479 | static int adev_set_master_volume(struct audio_hw_device *hw_dev, float volume) |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1480 | { |
| 1481 | return -ENOSYS; |
| 1482 | } |
| 1483 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 1484 | static int adev_set_mode(struct audio_hw_device *hw_dev, audio_mode_t mode) |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1485 | { |
| 1486 | return 0; |
| 1487 | } |
| 1488 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 1489 | static int adev_set_mic_mute(struct audio_hw_device *hw_dev, bool state) |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1490 | { |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 1491 | struct audio_device * adev = (struct audio_device *)hw_dev; |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 1492 | device_lock(adev); |
Eric Laurent | 253def9 | 2014-09-14 12:18:18 -0700 | [diff] [blame] | 1493 | adev->mic_muted = state; |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 1494 | device_unlock(adev); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1495 | return -ENOSYS; |
| 1496 | } |
| 1497 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 1498 | static int adev_get_mic_mute(const struct audio_hw_device *hw_dev, bool *state) |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1499 | { |
| 1500 | return -ENOSYS; |
| 1501 | } |
| 1502 | |
jiabin | 400bbe0 | 2020-10-28 13:56:59 -0700 | [diff] [blame] | 1503 | static int adev_create_audio_patch(struct audio_hw_device *dev, |
| 1504 | unsigned int num_sources, |
| 1505 | const struct audio_port_config *sources, |
| 1506 | unsigned int num_sinks, |
| 1507 | const struct audio_port_config *sinks, |
| 1508 | audio_patch_handle_t *handle) { |
| 1509 | if (num_sources != 1 || num_sinks == 0 || num_sinks > AUDIO_PATCH_PORTS_MAX) { |
| 1510 | // Only accept mix->device and device->mix cases. In that case, the number of sources |
| 1511 | // must be 1. The number of sinks must be in the range of (0, AUDIO_PATCH_PORTS_MAX]. |
| 1512 | return -EINVAL; |
| 1513 | } |
| 1514 | |
| 1515 | if (sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 1516 | // If source is a device, the number of sinks should be 1. |
| 1517 | if (num_sinks != 1 || sinks[0].type != AUDIO_PORT_TYPE_MIX) { |
| 1518 | return -EINVAL; |
| 1519 | } |
| 1520 | } else if (sources[0].type == AUDIO_PORT_TYPE_MIX) { |
| 1521 | // If source is a mix, all sinks should be device. |
| 1522 | for (unsigned int i = 0; i < num_sinks; i++) { |
| 1523 | if (sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 1524 | ALOGE("%s() invalid sink type %#x for mix source", __func__, sinks[i].type); |
| 1525 | return -EINVAL; |
| 1526 | } |
| 1527 | } |
| 1528 | } else { |
| 1529 | // All other cases are invalid. |
| 1530 | return -EINVAL; |
| 1531 | } |
| 1532 | |
| 1533 | struct audio_device* adev = (struct audio_device*) dev; |
| 1534 | bool generatedPatchHandle = false; |
| 1535 | device_lock(adev); |
| 1536 | if (*handle == AUDIO_PATCH_HANDLE_NONE) { |
| 1537 | *handle = ++adev->next_patch_handle; |
| 1538 | generatedPatchHandle = true; |
| 1539 | } |
| 1540 | |
| 1541 | int cards[AUDIO_PATCH_PORTS_MAX]; |
| 1542 | int devices[AUDIO_PATCH_PORTS_MAX]; |
| 1543 | const struct audio_port_config *port_configs = |
| 1544 | sources[0].type == AUDIO_PORT_TYPE_DEVICE ? sources : sinks; |
| 1545 | int num_configs = 0; |
| 1546 | audio_io_handle_t io_handle = 0; |
| 1547 | bool wasStandby = true; |
| 1548 | int direction = PCM_OUT; |
| 1549 | audio_patch_handle_t *patch_handle = NULL; |
| 1550 | struct listnode *alsa_devices = NULL; |
| 1551 | struct stream_lock *lock = NULL; |
| 1552 | struct pcm_config *config = NULL; |
| 1553 | struct stream_in *in = NULL; |
| 1554 | struct stream_out *out = NULL; |
| 1555 | |
| 1556 | unsigned int num_saved_devices = 0; |
| 1557 | int saved_cards[AUDIO_PATCH_PORTS_MAX]; |
| 1558 | int saved_devices[AUDIO_PATCH_PORTS_MAX]; |
| 1559 | |
| 1560 | struct listnode *node; |
| 1561 | |
| 1562 | // Only handle patches for mix->devices and device->mix case. |
| 1563 | if (sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 1564 | in = adev_get_stream_in_by_io_handle_l(adev, sinks[0].ext.mix.handle); |
| 1565 | if (in == NULL) { |
| 1566 | ALOGE("%s()can not find stream with handle(%d)", __func__, sinks[0].ext.mix.handle); |
| 1567 | device_unlock(adev); |
| 1568 | return -EINVAL; |
| 1569 | } |
| 1570 | |
| 1571 | direction = PCM_IN; |
| 1572 | wasStandby = in->standby; |
| 1573 | io_handle = in->handle; |
| 1574 | num_configs = num_sources; |
| 1575 | patch_handle = &in->patch_handle; |
| 1576 | alsa_devices = &in->alsa_devices; |
| 1577 | lock = &in->lock; |
| 1578 | config = &in->config; |
| 1579 | } else { |
| 1580 | out = adev_get_stream_out_by_io_handle_l(adev, sources[0].ext.mix.handle); |
| 1581 | if (out == NULL) { |
| 1582 | ALOGE("%s()can not find stream with handle(%d)", __func__, sources[0].ext.mix.handle); |
| 1583 | device_unlock(adev); |
| 1584 | return -EINVAL; |
| 1585 | } |
| 1586 | |
| 1587 | direction = PCM_OUT; |
| 1588 | wasStandby = out->standby; |
| 1589 | io_handle = out->handle; |
| 1590 | num_configs = num_sinks; |
| 1591 | patch_handle = &out->patch_handle; |
| 1592 | alsa_devices = &out->alsa_devices; |
| 1593 | lock = &out->lock; |
| 1594 | config = &out->config; |
| 1595 | } |
| 1596 | |
| 1597 | // Check if the patch handle match the recorded one if a valid patch handle is passed. |
| 1598 | if (!generatedPatchHandle && *patch_handle != *handle) { |
| 1599 | ALOGE("%s() the patch handle(%d) does not match recorded one(%d) for stream " |
| 1600 | "with handle(%d) when creating audio patch", |
| 1601 | __func__, *handle, *patch_handle, io_handle); |
| 1602 | device_unlock(adev); |
| 1603 | return -EINVAL; |
| 1604 | } |
| 1605 | device_unlock(adev); |
| 1606 | |
| 1607 | for (unsigned int i = 0; i < num_configs; ++i) { |
| 1608 | if (!parse_card_device_params(port_configs[i].ext.device.address, &cards[i], &devices[i])) { |
| 1609 | ALOGE("%s, failed to parse card and device %s", |
| 1610 | __func__, port_configs[i].ext.device.address); |
| 1611 | return -EINVAL; |
| 1612 | } |
| 1613 | } |
| 1614 | |
| 1615 | stream_lock(lock); |
| 1616 | list_for_each (node, alsa_devices) { |
| 1617 | struct alsa_device_info *device_info = |
| 1618 | node_to_item(node, struct alsa_device_info, list_node); |
| 1619 | saved_cards[num_saved_devices] = device_info->profile.card; |
| 1620 | saved_devices[num_saved_devices++] = device_info->profile.device; |
| 1621 | } |
| 1622 | |
| 1623 | device_lock(adev); |
| 1624 | stream_standby_l(alsa_devices, out == NULL ? &in->standby : &out->standby); |
| 1625 | device_unlock(adev); |
| 1626 | |
| 1627 | int ret = stream_set_new_devices(config, alsa_devices, num_configs, cards, devices, direction); |
| 1628 | |
| 1629 | if (ret != 0) { |
| 1630 | *handle = generatedPatchHandle ? AUDIO_PATCH_HANDLE_NONE : *handle; |
| 1631 | stream_set_new_devices( |
| 1632 | config, alsa_devices, num_saved_devices, saved_cards, saved_devices, direction); |
| 1633 | } else { |
| 1634 | *patch_handle = *handle; |
| 1635 | } |
| 1636 | if (!wasStandby) { |
| 1637 | device_lock(adev); |
| 1638 | if (in != NULL) { |
| 1639 | start_input_stream(in); |
| 1640 | } |
| 1641 | if (out != NULL) { |
| 1642 | start_output_stream(out); |
| 1643 | } |
| 1644 | device_unlock(adev); |
| 1645 | } |
| 1646 | stream_unlock(lock); |
| 1647 | return ret; |
| 1648 | } |
| 1649 | |
| 1650 | static int adev_release_audio_patch(struct audio_hw_device *dev, |
| 1651 | audio_patch_handle_t patch_handle) |
| 1652 | { |
| 1653 | struct audio_device* adev = (struct audio_device*) dev; |
| 1654 | |
| 1655 | device_lock(adev); |
| 1656 | struct stream_out *out = adev_get_stream_out_by_patch_handle_l(adev, patch_handle); |
| 1657 | device_unlock(adev); |
| 1658 | if (out != NULL) { |
| 1659 | stream_lock(&out->lock); |
| 1660 | device_lock(adev); |
| 1661 | stream_standby_l(&out->alsa_devices, &out->standby); |
| 1662 | device_unlock(adev); |
| 1663 | out->patch_handle = AUDIO_PATCH_HANDLE_NONE; |
| 1664 | stream_unlock(&out->lock); |
| 1665 | return 0; |
| 1666 | } |
| 1667 | |
| 1668 | device_lock(adev); |
| 1669 | struct stream_in *in = adev_get_stream_in_by_patch_handle_l(adev, patch_handle); |
| 1670 | device_unlock(adev); |
| 1671 | if (in != NULL) { |
| 1672 | stream_lock(&in->lock); |
| 1673 | device_lock(adev); |
| 1674 | stream_standby_l(&in->alsa_devices, &in->standby); |
| 1675 | device_unlock(adev); |
| 1676 | in->patch_handle = AUDIO_PATCH_HANDLE_NONE; |
| 1677 | stream_unlock(&in->lock); |
| 1678 | return 0; |
| 1679 | } |
| 1680 | |
| 1681 | ALOGE("%s cannot find stream with patch handle as %d", __func__, patch_handle); |
| 1682 | return -EINVAL; |
| 1683 | } |
| 1684 | |
| 1685 | static int adev_get_audio_port(struct audio_hw_device *dev, struct audio_port *port) |
| 1686 | { |
| 1687 | if (port->type != AUDIO_PORT_TYPE_DEVICE) { |
| 1688 | return -EINVAL; |
| 1689 | } |
| 1690 | |
| 1691 | alsa_device_profile profile; |
| 1692 | const bool is_output = audio_is_output_device(port->ext.device.type); |
| 1693 | profile_init(&profile, is_output ? PCM_OUT : PCM_IN); |
| 1694 | if (!parse_card_device_params(port->ext.device.address, &profile.card, &profile.device)) { |
| 1695 | return -EINVAL; |
| 1696 | } |
| 1697 | |
| 1698 | if (!profile_read_device_info(&profile)) { |
| 1699 | return -ENOENT; |
| 1700 | } |
| 1701 | |
| 1702 | port->num_formats = 0;; |
| 1703 | for (size_t i = 0; i < min(MAX_PROFILE_FORMATS, AUDIO_PORT_MAX_FORMATS) && |
| 1704 | profile.formats[i] != 0; ++i) { |
| 1705 | audio_format_t format = audio_format_from(profile.formats[i]); |
| 1706 | if (format != AUDIO_FORMAT_INVALID) { |
| 1707 | port->formats[port->num_formats++] = format; |
| 1708 | } |
| 1709 | } |
| 1710 | |
jiabin | a635fcf | 2021-01-11 11:34:50 -0800 | [diff] [blame^] | 1711 | port->num_sample_rates = populate_sample_rates_from_profile(&profile, port->sample_rates); |
| 1712 | port->num_channel_masks = populate_channel_mask_from_profile( |
| 1713 | &profile, is_output, port->channel_masks); |
jiabin | 400bbe0 | 2020-10-28 13:56:59 -0700 | [diff] [blame] | 1714 | |
jiabin | a635fcf | 2021-01-11 11:34:50 -0800 | [diff] [blame^] | 1715 | return 0; |
| 1716 | } |
| 1717 | |
| 1718 | static int adev_get_audio_port_v7(struct audio_hw_device *dev, struct audio_port_v7 *port) |
| 1719 | { |
| 1720 | if (port->type != AUDIO_PORT_TYPE_DEVICE) { |
| 1721 | return -EINVAL; |
jiabin | 400bbe0 | 2020-10-28 13:56:59 -0700 | [diff] [blame] | 1722 | } |
jiabin | a635fcf | 2021-01-11 11:34:50 -0800 | [diff] [blame^] | 1723 | |
| 1724 | alsa_device_profile profile; |
| 1725 | const bool is_output = audio_is_output_device(port->ext.device.type); |
| 1726 | profile_init(&profile, is_output ? PCM_OUT : PCM_IN); |
| 1727 | if (!parse_card_device_params(port->ext.device.address, &profile.card, &profile.device)) { |
| 1728 | return -EINVAL; |
| 1729 | } |
| 1730 | |
| 1731 | if (!profile_read_device_info(&profile)) { |
| 1732 | return -ENOENT; |
| 1733 | } |
| 1734 | |
| 1735 | audio_channel_mask_t channel_masks[AUDIO_PORT_MAX_CHANNEL_MASKS]; |
| 1736 | unsigned int num_channel_masks = populate_channel_mask_from_profile( |
| 1737 | &profile, is_output, channel_masks); |
| 1738 | unsigned int sample_rates[AUDIO_PORT_MAX_SAMPLING_RATES]; |
| 1739 | const unsigned int num_sample_rates = |
| 1740 | populate_sample_rates_from_profile(&profile, sample_rates); |
| 1741 | port->num_audio_profiles = 0;; |
| 1742 | for (size_t i = 0; i < min(MAX_PROFILE_FORMATS, AUDIO_PORT_MAX_AUDIO_PROFILES) && |
| 1743 | profile.formats[i] != 0; ++i) { |
| 1744 | audio_format_t format = audio_format_from(profile.formats[i]); |
| 1745 | if (format == AUDIO_FORMAT_INVALID) { |
| 1746 | continue; |
| 1747 | } |
| 1748 | const unsigned int j = port->num_audio_profiles++; |
| 1749 | port->audio_profiles[j].format = format; |
| 1750 | port->audio_profiles[j].num_sample_rates = num_sample_rates; |
| 1751 | memcpy(port->audio_profiles[j].sample_rates, |
| 1752 | sample_rates, |
| 1753 | num_sample_rates * sizeof(unsigned int)); |
| 1754 | port->audio_profiles[j].num_channel_masks = num_channel_masks; |
| 1755 | memcpy(port->audio_profiles[j].channel_masks, |
| 1756 | channel_masks, |
| 1757 | num_channel_masks* sizeof(audio_channel_mask_t)); |
| 1758 | } |
| 1759 | |
jiabin | 400bbe0 | 2020-10-28 13:56:59 -0700 | [diff] [blame] | 1760 | return 0; |
| 1761 | } |
| 1762 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 1763 | static int adev_dump(const struct audio_hw_device *device, int fd) |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1764 | { |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 1765 | dprintf(fd, "\nUSB audio module:\n"); |
| 1766 | |
| 1767 | struct audio_device* adev = (struct audio_device*)device; |
| 1768 | const int kNumRetries = 3; |
| 1769 | const int kSleepTimeMS = 500; |
| 1770 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 1771 | // use device_try_lock() in case we dumpsys during a deadlock |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 1772 | int retry = kNumRetries; |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 1773 | while (retry > 0 && device_try_lock(adev) != 0) { |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 1774 | sleep(kSleepTimeMS); |
| 1775 | retry--; |
| 1776 | } |
| 1777 | |
| 1778 | if (retry > 0) { |
| 1779 | if (list_empty(&adev->output_stream_list)) { |
| 1780 | dprintf(fd, " No output streams.\n"); |
| 1781 | } else { |
| 1782 | struct listnode* node; |
| 1783 | list_for_each(node, &adev->output_stream_list) { |
| 1784 | struct audio_stream* stream = |
| 1785 | (struct audio_stream *)node_to_item(node, struct stream_out, list_node); |
| 1786 | out_dump(stream, fd); |
| 1787 | } |
| 1788 | } |
| 1789 | |
| 1790 | if (list_empty(&adev->input_stream_list)) { |
| 1791 | dprintf(fd, "\n No input streams.\n"); |
| 1792 | } else { |
| 1793 | struct listnode* node; |
| 1794 | list_for_each(node, &adev->input_stream_list) { |
| 1795 | struct audio_stream* stream = |
| 1796 | (struct audio_stream *)node_to_item(node, struct stream_in, list_node); |
| 1797 | in_dump(stream, fd); |
| 1798 | } |
| 1799 | } |
| 1800 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 1801 | device_unlock(adev); |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 1802 | } else { |
| 1803 | // Couldn't lock |
| 1804 | dprintf(fd, " Could not obtain device lock.\n"); |
| 1805 | } |
| 1806 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1807 | return 0; |
| 1808 | } |
| 1809 | |
| 1810 | static int adev_close(hw_device_t *device) |
| 1811 | { |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1812 | free(device); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1813 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1814 | return 0; |
| 1815 | } |
| 1816 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1817 | static int adev_open(const hw_module_t* module, const char* name, hw_device_t** device) |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1818 | { |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1819 | if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) |
| 1820 | return -EINVAL; |
| 1821 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1822 | struct audio_device *adev = calloc(1, sizeof(struct audio_device)); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1823 | if (!adev) |
| 1824 | return -ENOMEM; |
| 1825 | |
Paul McLean | cf5310a | 2018-08-22 14:33:12 -0700 | [diff] [blame] | 1826 | pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1827 | |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 1828 | list_init(&adev->output_stream_list); |
| 1829 | list_init(&adev->input_stream_list); |
| 1830 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1831 | adev->hw_device.common.tag = HARDWARE_DEVICE_TAG; |
jiabin | a635fcf | 2021-01-11 11:34:50 -0800 | [diff] [blame^] | 1832 | adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_3_2; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1833 | adev->hw_device.common.module = (struct hw_module_t *)module; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1834 | adev->hw_device.common.close = adev_close; |
| 1835 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1836 | adev->hw_device.init_check = adev_init_check; |
| 1837 | adev->hw_device.set_voice_volume = adev_set_voice_volume; |
| 1838 | adev->hw_device.set_master_volume = adev_set_master_volume; |
| 1839 | adev->hw_device.set_mode = adev_set_mode; |
| 1840 | adev->hw_device.set_mic_mute = adev_set_mic_mute; |
| 1841 | adev->hw_device.get_mic_mute = adev_get_mic_mute; |
| 1842 | adev->hw_device.set_parameters = adev_set_parameters; |
| 1843 | adev->hw_device.get_parameters = adev_get_parameters; |
| 1844 | adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size; |
| 1845 | adev->hw_device.open_output_stream = adev_open_output_stream; |
| 1846 | adev->hw_device.close_output_stream = adev_close_output_stream; |
| 1847 | adev->hw_device.open_input_stream = adev_open_input_stream; |
| 1848 | adev->hw_device.close_input_stream = adev_close_input_stream; |
jiabin | 400bbe0 | 2020-10-28 13:56:59 -0700 | [diff] [blame] | 1849 | adev->hw_device.create_audio_patch = adev_create_audio_patch; |
| 1850 | adev->hw_device.release_audio_patch = adev_release_audio_patch; |
| 1851 | adev->hw_device.get_audio_port = adev_get_audio_port; |
jiabin | a635fcf | 2021-01-11 11:34:50 -0800 | [diff] [blame^] | 1852 | adev->hw_device.get_audio_port_v7 = adev_get_audio_port_v7; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1853 | adev->hw_device.dump = adev_dump; |
| 1854 | |
| 1855 | *device = &adev->hw_device.common; |
| 1856 | |
| 1857 | return 0; |
| 1858 | } |
| 1859 | |
| 1860 | static struct hw_module_methods_t hal_module_methods = { |
| 1861 | .open = adev_open, |
| 1862 | }; |
| 1863 | |
| 1864 | struct audio_module HAL_MODULE_INFO_SYM = { |
| 1865 | .common = { |
| 1866 | .tag = HARDWARE_MODULE_TAG, |
Mike Lockwood | 46a9809 | 2012-04-24 16:41:18 -0700 | [diff] [blame] | 1867 | .module_api_version = AUDIO_MODULE_API_VERSION_0_1, |
| 1868 | .hal_api_version = HARDWARE_HAL_API_VERSION, |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1869 | .id = AUDIO_HARDWARE_MODULE_ID, |
| 1870 | .name = "USB audio HW HAL", |
| 1871 | .author = "The Android Open Source Project", |
| 1872 | .methods = &hal_module_methods, |
| 1873 | }, |
| 1874 | }; |