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> |
Mark Salyzyn | 88e458a | 2014-04-28 12:30:44 -0700 | [diff] [blame] | 25 | #include <sys/time.h> |
Tri Vo | 00a8673 | 2017-06-27 11:33:36 -0700 | [diff] [blame] | 26 | #include <unistd.h> |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 27 | |
Mark Salyzyn | 88e458a | 2014-04-28 12:30:44 -0700 | [diff] [blame] | 28 | #include <log/log.h> |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 29 | #include <cutils/list.h> |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 30 | #include <cutils/str_parms.h> |
| 31 | #include <cutils/properties.h> |
| 32 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 33 | #include <hardware/audio.h> |
Paul McLean | e32cbc1 | 2014-06-25 10:42:07 -0700 | [diff] [blame] | 34 | #include <hardware/audio_alsaops.h> |
| 35 | #include <hardware/hardware.h> |
| 36 | |
| 37 | #include <system/audio.h> |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 38 | |
| 39 | #include <tinyalsa/asoundlib.h> |
| 40 | |
Paul McLean | c220115 | 2014-07-16 13:46:07 -0700 | [diff] [blame] | 41 | #include <audio_utils/channels.h> |
| 42 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 43 | #include "alsa_device_profile.h" |
| 44 | #include "alsa_device_proxy.h" |
Paul McLean | 9ab869a | 2015-01-13 09:37:06 -0800 | [diff] [blame] | 45 | #include "alsa_logging.h" |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 46 | |
Paul McLean | 1d585cc | 2016-05-24 11:28:10 -0600 | [diff] [blame] | 47 | /* Lock play & record samples rates at or above this threshold */ |
| 48 | #define RATELOCK_THRESHOLD 96000 |
| 49 | |
Paul McLean | cfdbd6b | 2018-07-27 11:16:02 -0600 | [diff] [blame] | 50 | #define max(a, b) ((a) > (b) ? (a) : (b)) |
| 51 | #define min(a, b) ((a) < (b) ? (a) : (b)) |
| 52 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 53 | struct audio_device { |
| 54 | struct audio_hw_device hw_device; |
| 55 | |
| 56 | pthread_mutex_t lock; /* see note below on mutex acquisition order */ |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 57 | |
| 58 | /* output */ |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 59 | struct listnode output_stream_list; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 60 | |
| 61 | /* input */ |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 62 | struct listnode input_stream_list; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 63 | |
Paul McLean | 1d585cc | 2016-05-24 11:28:10 -0600 | [diff] [blame] | 64 | /* lock input & output sample rates */ |
| 65 | /*FIXME - How do we address multiple output streams? */ |
Paul McLean | cfdbd6b | 2018-07-27 11:16:02 -0600 | [diff] [blame] | 66 | 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] | 67 | |
Eric Laurent | 253def9 | 2014-09-14 12:18:18 -0700 | [diff] [blame] | 68 | bool mic_muted; |
| 69 | |
Andy Hung | b10ce6d | 2017-10-27 19:37:58 -0700 | [diff] [blame] | 70 | int32_t inputs_open; /* number of input streams currently open. */ |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 71 | }; |
| 72 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 73 | struct stream_lock { |
| 74 | pthread_mutex_t lock; /* see note below on mutex acquisition order */ |
| 75 | pthread_mutex_t pre_lock; /* acquire before lock to avoid DOS by playback thread */ |
| 76 | }; |
| 77 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 78 | struct stream_out { |
| 79 | struct audio_stream_out stream; |
| 80 | |
Paul McLean | cf5310a | 2018-08-22 14:33:12 -0700 | [diff] [blame] | 81 | struct stream_lock lock; |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 82 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 83 | bool standby; |
| 84 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 85 | struct audio_device *adev; /* hardware information - only using this for the lock */ |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 86 | |
Paul McLean | cf5310a | 2018-08-22 14:33:12 -0700 | [diff] [blame] | 87 | alsa_device_profile profile; /* The profile of the ALSA device connected to the stream. |
Andy Hung | 4e6a1c0 | 2017-10-27 20:31:46 -0700 | [diff] [blame] | 88 | */ |
| 89 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 90 | alsa_device_proxy proxy; /* state of the stream */ |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 91 | |
Andy Hung | 03576be | 2014-07-21 21:16:45 -0700 | [diff] [blame] | 92 | unsigned hal_channel_count; /* channel count exposed to AudioFlinger. |
| 93 | * This may differ from the device channel count when |
| 94 | * the device is not compatible with AudioFlinger |
| 95 | * capabilities, e.g. exposes too many channels or |
| 96 | * too few channels. */ |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 97 | audio_channel_mask_t hal_channel_mask; /* USB devices deal in channel counts, not masks |
| 98 | * so the proxy doesn't have a channel_mask, but |
| 99 | * audio HALs need to talk about channel masks |
| 100 | * so expose the one calculated by |
| 101 | * adev_open_output_stream */ |
Andy Hung | 182ddc7 | 2015-05-05 23:37:30 -0700 | [diff] [blame] | 102 | |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 103 | struct listnode list_node; |
| 104 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 105 | void * conversion_buffer; /* any conversions are put into here |
| 106 | * they could come from here too if |
| 107 | * there was a previous conversion */ |
| 108 | size_t conversion_buffer_size; /* in bytes */ |
| 109 | }; |
| 110 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 111 | struct stream_in { |
| 112 | struct audio_stream_in stream; |
| 113 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 114 | struct stream_lock lock; |
| 115 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 116 | bool standby; |
| 117 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 118 | struct audio_device *adev; /* hardware information - only using this for the lock */ |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 119 | |
Paul McLean | cf5310a | 2018-08-22 14:33:12 -0700 | [diff] [blame] | 120 | alsa_device_profile profile; /* The profile of the ALSA device connected to the stream. |
Andy Hung | 4e6a1c0 | 2017-10-27 20:31:46 -0700 | [diff] [blame] | 121 | */ |
| 122 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 123 | alsa_device_proxy proxy; /* state of the stream */ |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 124 | |
Paul McLean | 2cfd81b | 2014-09-15 12:32:23 -0700 | [diff] [blame] | 125 | unsigned hal_channel_count; /* channel count exposed to AudioFlinger. |
| 126 | * This may differ from the device channel count when |
| 127 | * the device is not compatible with AudioFlinger |
| 128 | * capabilities, e.g. exposes too many channels or |
| 129 | * too few channels. */ |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 130 | audio_channel_mask_t hal_channel_mask; /* USB devices deal in channel counts, not masks |
| 131 | * so the proxy doesn't have a channel_mask, but |
| 132 | * audio HALs need to talk about channel masks |
| 133 | * so expose the one calculated by |
| 134 | * adev_open_input_stream */ |
Andy Hung | 780f1f8 | 2015-04-22 22:14:39 -0700 | [diff] [blame] | 135 | |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 136 | struct listnode list_node; |
| 137 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 138 | /* 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] | 139 | void * conversion_buffer; /* any conversions are put into here |
| 140 | * they could come from here too if |
| 141 | * there was a previous conversion */ |
| 142 | size_t conversion_buffer_size; /* in bytes */ |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 143 | }; |
| 144 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 145 | /* |
| 146 | * Locking Helpers |
| 147 | */ |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 148 | /* |
Eric Laurent | 7031809 | 2015-06-19 17:49:17 -0700 | [diff] [blame] | 149 | * NOTE: when multiple mutexes have to be acquired, always take the |
| 150 | * stream_in or stream_out mutex first, followed by the audio_device mutex. |
| 151 | * stream pre_lock is always acquired before stream lock to prevent starvation of control thread by |
| 152 | * higher priority playback or capture thread. |
| 153 | */ |
| 154 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 155 | static void stream_lock_init(struct stream_lock *lock) { |
| 156 | pthread_mutex_init(&lock->lock, (const pthread_mutexattr_t *) NULL); |
| 157 | pthread_mutex_init(&lock->pre_lock, (const pthread_mutexattr_t *) NULL); |
| 158 | } |
| 159 | |
| 160 | static void stream_lock(struct stream_lock *lock) { |
| 161 | pthread_mutex_lock(&lock->pre_lock); |
| 162 | pthread_mutex_lock(&lock->lock); |
| 163 | pthread_mutex_unlock(&lock->pre_lock); |
| 164 | } |
| 165 | |
| 166 | static void stream_unlock(struct stream_lock *lock) { |
| 167 | pthread_mutex_unlock(&lock->lock); |
| 168 | } |
| 169 | |
| 170 | static void device_lock(struct audio_device *adev) { |
| 171 | pthread_mutex_lock(&adev->lock); |
| 172 | } |
| 173 | |
| 174 | static int device_try_lock(struct audio_device *adev) { |
| 175 | return pthread_mutex_trylock(&adev->lock); |
| 176 | } |
| 177 | |
| 178 | static void device_unlock(struct audio_device *adev) { |
| 179 | pthread_mutex_unlock(&adev->lock); |
| 180 | } |
| 181 | |
| 182 | /* |
| 183 | * streams list management |
| 184 | */ |
| 185 | static void adev_add_stream_to_list( |
| 186 | struct audio_device* adev, struct listnode* list, struct listnode* stream_node) { |
| 187 | device_lock(adev); |
| 188 | |
| 189 | list_add_tail(list, stream_node); |
| 190 | |
| 191 | device_unlock(adev); |
| 192 | } |
| 193 | |
| 194 | static void adev_remove_stream_from_list( |
| 195 | struct audio_device* adev, struct listnode* stream_node) { |
| 196 | device_lock(adev); |
| 197 | |
| 198 | list_remove(stream_node); |
| 199 | |
| 200 | device_unlock(adev); |
| 201 | } |
| 202 | |
Eric Laurent | 7031809 | 2015-06-19 17:49:17 -0700 | [diff] [blame] | 203 | /* |
Paul McLean | 0f1753e | 2014-12-02 12:36:45 -0700 | [diff] [blame] | 204 | * Extract the card and device numbers from the supplied key/value pairs. |
| 205 | * kvpairs A null-terminated string containing the key/value pairs or card and device. |
| 206 | * i.e. "card=1;device=42" |
| 207 | * card A pointer to a variable to receive the parsed-out card number. |
| 208 | * device A pointer to a variable to receive the parsed-out device number. |
| 209 | * NOTE: The variables pointed to by card and device return -1 (undefined) if the |
| 210 | * associated key/value pair is not found in the provided string. |
Paul McLean | 65ec72b | 2015-02-18 09:13:24 -0800 | [diff] [blame] | 211 | * 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] | 212 | */ |
Paul McLean | 65ec72b | 2015-02-18 09:13:24 -0800 | [diff] [blame] | 213 | 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] | 214 | { |
| 215 | struct str_parms * parms = str_parms_create_str(kvpairs); |
| 216 | char value[32]; |
| 217 | int param_val; |
| 218 | |
| 219 | // initialize to "undefined" state. |
| 220 | *card = -1; |
| 221 | *device = -1; |
| 222 | |
| 223 | param_val = str_parms_get_str(parms, "card", value, sizeof(value)); |
| 224 | if (param_val >= 0) { |
| 225 | *card = atoi(value); |
| 226 | } |
| 227 | |
| 228 | param_val = str_parms_get_str(parms, "device", value, sizeof(value)); |
| 229 | if (param_val >= 0) { |
| 230 | *device = atoi(value); |
| 231 | } |
| 232 | |
| 233 | str_parms_destroy(parms); |
Paul McLean | 65ec72b | 2015-02-18 09:13:24 -0800 | [diff] [blame] | 234 | |
| 235 | return *card >= 0 && *device >= 0; |
Paul McLean | 0f1753e | 2014-12-02 12:36:45 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Andy Hung | 4e6a1c0 | 2017-10-27 20:31:46 -0700 | [diff] [blame] | 238 | 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] | 239 | { |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 240 | if (profile->card < 0 || profile->device < 0) { |
| 241 | return strdup(""); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 242 | } |
Paul McLean | e32cbc1 | 2014-06-25 10:42:07 -0700 | [diff] [blame] | 243 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 244 | struct str_parms *query = str_parms_create_str(keys); |
| 245 | struct str_parms *result = str_parms_create(); |
Paul McLean | e32cbc1 | 2014-06-25 10:42:07 -0700 | [diff] [blame] | 246 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 247 | /* These keys are from hardware/libhardware/include/audio.h */ |
| 248 | /* supported sample rates */ |
| 249 | if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) { |
| 250 | char* rates_list = profile_get_sample_rate_strs(profile); |
| 251 | str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES, |
| 252 | rates_list); |
| 253 | free(rates_list); |
Paul McLean | e32cbc1 | 2014-06-25 10:42:07 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 256 | /* supported channel counts */ |
| 257 | if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) { |
| 258 | char* channels_list = profile_get_channel_count_strs(profile); |
| 259 | str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, |
| 260 | channels_list); |
| 261 | free(channels_list); |
Paul McLean | e32cbc1 | 2014-06-25 10:42:07 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 264 | /* supported sample formats */ |
| 265 | if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) { |
| 266 | char * format_params = profile_get_format_strs(profile); |
| 267 | str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_FORMATS, |
| 268 | format_params); |
| 269 | free(format_params); |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 270 | } |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 271 | str_parms_destroy(query); |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 272 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 273 | char* result_str = str_parms_to_str(result); |
| 274 | str_parms_destroy(result); |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 275 | |
Paul McLean | 65ec72b | 2015-02-18 09:13:24 -0800 | [diff] [blame] | 276 | ALOGV("device_get_parameters = %s", result_str); |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 277 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 278 | return result_str; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | /* |
| 282 | * HAl Functions |
| 283 | */ |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 284 | /** |
| 285 | * NOTE: when multiple mutexes have to be acquired, always respect the |
| 286 | * following order: hw device > out stream |
| 287 | */ |
| 288 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 289 | /* |
| 290 | * OUT functions |
| 291 | */ |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 292 | static uint32_t out_get_sample_rate(const struct audio_stream *stream) |
| 293 | { |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 294 | uint32_t rate = proxy_get_sample_rate(&((struct stream_out*)stream)->proxy); |
| 295 | ALOGV("out_get_sample_rate() = %d", rate); |
| 296 | return rate; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate) |
| 300 | { |
| 301 | return 0; |
| 302 | } |
| 303 | |
| 304 | static size_t out_get_buffer_size(const struct audio_stream *stream) |
| 305 | { |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 306 | const struct stream_out* out = (const struct stream_out*)stream; |
| 307 | size_t buffer_size = |
| 308 | proxy_get_period_size(&out->proxy) * audio_stream_out_frame_size(&(out->stream)); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 309 | return buffer_size; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | static uint32_t out_get_channels(const struct audio_stream *stream) |
| 313 | { |
Andy Hung | 03576be | 2014-07-21 21:16:45 -0700 | [diff] [blame] | 314 | const struct stream_out *out = (const struct stream_out*)stream; |
Andy Hung | 182ddc7 | 2015-05-05 23:37:30 -0700 | [diff] [blame] | 315 | return out->hal_channel_mask; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | static audio_format_t out_get_format(const struct audio_stream *stream) |
| 319 | { |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 320 | /* Note: The HAL doesn't do any FORMAT conversion at this time. It |
| 321 | * Relies on the framework to provide data in the specified format. |
| 322 | * This could change in the future. |
| 323 | */ |
| 324 | alsa_device_proxy * proxy = &((struct stream_out*)stream)->proxy; |
| 325 | audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy)); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 326 | return format; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | static int out_set_format(struct audio_stream *stream, audio_format_t format) |
| 330 | { |
| 331 | return 0; |
| 332 | } |
| 333 | |
| 334 | static int out_standby(struct audio_stream *stream) |
| 335 | { |
| 336 | struct stream_out *out = (struct stream_out *)stream; |
| 337 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 338 | stream_lock(&out->lock); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 339 | if (!out->standby) { |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 340 | proxy_close(&out->proxy); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 341 | out->standby = true; |
| 342 | } |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 343 | stream_unlock(&out->lock); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 344 | return 0; |
| 345 | } |
| 346 | |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 347 | static int out_dump(const struct audio_stream *stream, int fd) { |
| 348 | const struct stream_out* out_stream = (const struct stream_out*) stream; |
| 349 | |
| 350 | if (out_stream != NULL) { |
| 351 | dprintf(fd, "Output Profile:\n"); |
Paul McLean | cf5310a | 2018-08-22 14:33:12 -0700 | [diff] [blame] | 352 | profile_dump(&out_stream->profile, fd); |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 353 | |
| 354 | dprintf(fd, "Output Proxy:\n"); |
| 355 | proxy_dump(&out_stream->proxy, fd); |
| 356 | } |
| 357 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 358 | return 0; |
| 359 | } |
| 360 | |
| 361 | static int out_set_parameters(struct audio_stream *stream, const char *kvpairs) |
| 362 | { |
Paul McLean | 65ec72b | 2015-02-18 09:13:24 -0800 | [diff] [blame] | 363 | ALOGV("out_set_parameters() keys:%s", kvpairs); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 364 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 365 | struct stream_out *out = (struct stream_out *)stream; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 366 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 367 | int ret_value = 0; |
Eric Laurent | 05333d4 | 2014-08-04 20:29:17 -0700 | [diff] [blame] | 368 | int card = -1; |
| 369 | int device = -1; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 370 | |
Paul McLean | 65ec72b | 2015-02-18 09:13:24 -0800 | [diff] [blame] | 371 | if (!parse_card_device_params(kvpairs, &card, &device)) { |
| 372 | // nothing to do |
| 373 | return ret_value; |
| 374 | } |
| 375 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 376 | stream_lock(&out->lock); |
Paul McLean | cf5310a | 2018-08-22 14:33:12 -0700 | [diff] [blame] | 377 | if (!profile_is_cached_for(&out->profile, card, device)) { |
Eric Laurent | 05333d4 | 2014-08-04 20:29:17 -0700 | [diff] [blame] | 378 | /* cannot read pcm device info if playback is active */ |
| 379 | if (!out->standby) |
| 380 | ret_value = -ENOSYS; |
| 381 | else { |
Paul McLean | cf5310a | 2018-08-22 14:33:12 -0700 | [diff] [blame] | 382 | int saved_card = out->profile.card; |
| 383 | int saved_device = out->profile.device; |
| 384 | out->profile.card = card; |
| 385 | out->profile.device = device; |
| 386 | ret_value = profile_read_device_info(&out->profile) ? 0 : -EINVAL; |
Eric Laurent | 05333d4 | 2014-08-04 20:29:17 -0700 | [diff] [blame] | 387 | if (ret_value != 0) { |
Paul McLean | cf5310a | 2018-08-22 14:33:12 -0700 | [diff] [blame] | 388 | out->profile.card = saved_card; |
| 389 | out->profile.device = saved_device; |
Eric Laurent | 05333d4 | 2014-08-04 20:29:17 -0700 | [diff] [blame] | 390 | } |
| 391 | } |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 392 | } |
Paul McLean | 2c6196f | 2014-08-20 16:50:25 -0700 | [diff] [blame] | 393 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 394 | stream_unlock(&out->lock); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 395 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 396 | return ret_value; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 397 | } |
| 398 | |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 399 | static char * out_get_parameters(const struct audio_stream *stream, const char *keys) |
| 400 | { |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 401 | struct stream_out *out = (struct stream_out *)stream; |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 402 | stream_lock(&out->lock); |
Paul McLean | cf5310a | 2018-08-22 14:33:12 -0700 | [diff] [blame] | 403 | char * params_str = device_get_parameters(&out->profile, keys); |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 404 | stream_unlock(&out->lock); |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 405 | return params_str; |
| 406 | } |
| 407 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 408 | static uint32_t out_get_latency(const struct audio_stream_out *stream) |
| 409 | { |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 410 | alsa_device_proxy * proxy = &((struct stream_out*)stream)->proxy; |
| 411 | return proxy_get_latency(proxy); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 412 | } |
| 413 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 414 | 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] | 415 | { |
| 416 | return -ENOSYS; |
| 417 | } |
| 418 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 419 | /* must be called with hw device and output stream mutexes locked */ |
| 420 | static int start_output_stream(struct stream_out *out) |
| 421 | { |
Paul McLean | cf5310a | 2018-08-22 14:33:12 -0700 | [diff] [blame] | 422 | ALOGV("start_output_stream(card:%d device:%d)", out->profile.card, out->profile.device); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 423 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 424 | return proxy_open(&out->proxy); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | 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] | 428 | { |
| 429 | int ret; |
| 430 | struct stream_out *out = (struct stream_out *)stream; |
| 431 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 432 | stream_lock(&out->lock); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 433 | if (out->standby) { |
| 434 | ret = start_output_stream(out); |
| 435 | if (ret != 0) { |
| 436 | goto err; |
| 437 | } |
| 438 | out->standby = false; |
| 439 | } |
Eric Laurent | 05333d4 | 2014-08-04 20:29:17 -0700 | [diff] [blame] | 440 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 441 | alsa_device_proxy* proxy = &out->proxy; |
Mark Salyzyn | 88e458a | 2014-04-28 12:30:44 -0700 | [diff] [blame] | 442 | const void * write_buff = buffer; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 443 | int num_write_buff_bytes = bytes; |
Andy Hung | 03576be | 2014-07-21 21:16:45 -0700 | [diff] [blame] | 444 | const int num_device_channels = proxy_get_channel_count(proxy); /* what we told alsa */ |
| 445 | const int num_req_channels = out->hal_channel_count; /* what we told AudioFlinger */ |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 446 | if (num_device_channels != num_req_channels) { |
Andy Hung | 03576be | 2014-07-21 21:16:45 -0700 | [diff] [blame] | 447 | /* allocate buffer */ |
| 448 | const size_t required_conversion_buffer_size = |
| 449 | bytes * num_device_channels / num_req_channels; |
| 450 | if (required_conversion_buffer_size > out->conversion_buffer_size) { |
| 451 | out->conversion_buffer_size = required_conversion_buffer_size; |
| 452 | out->conversion_buffer = realloc(out->conversion_buffer, |
| 453 | out->conversion_buffer_size); |
| 454 | } |
| 455 | /* convert data */ |
| 456 | const audio_format_t audio_format = out_get_format(&(out->stream.common)); |
| 457 | const unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 458 | num_write_buff_bytes = |
Andy Hung | 03576be | 2014-07-21 21:16:45 -0700 | [diff] [blame] | 459 | adjust_channels(write_buff, num_req_channels, |
| 460 | out->conversion_buffer, num_device_channels, |
| 461 | sample_size_in_bytes, num_write_buff_bytes); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 462 | write_buff = out->conversion_buffer; |
| 463 | } |
| 464 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 465 | if (write_buff != NULL && num_write_buff_bytes != 0) { |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 466 | proxy_write(&out->proxy, write_buff, num_write_buff_bytes); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 467 | } |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 468 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 469 | stream_unlock(&out->lock); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 470 | |
| 471 | return bytes; |
| 472 | |
| 473 | err: |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 474 | stream_unlock(&out->lock); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 475 | if (ret != 0) { |
Eric Laurent | c5ae6a0 | 2014-07-02 13:45:32 -0700 | [diff] [blame] | 476 | usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) / |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 477 | out_get_sample_rate(&stream->common)); |
| 478 | } |
| 479 | |
| 480 | return bytes; |
| 481 | } |
| 482 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 483 | 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] | 484 | { |
| 485 | return -EINVAL; |
| 486 | } |
| 487 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 488 | static int out_get_presentation_position(const struct audio_stream_out *stream, |
| 489 | uint64_t *frames, struct timespec *timestamp) |
| 490 | { |
Andy Hung | c9515ce | 2015-08-04 15:05:19 -0700 | [diff] [blame] | 491 | struct stream_out *out = (struct stream_out *)stream; // discard const qualifier |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 492 | stream_lock(&out->lock); |
Andy Hung | c9515ce | 2015-08-04 15:05:19 -0700 | [diff] [blame] | 493 | |
| 494 | const alsa_device_proxy *proxy = &out->proxy; |
| 495 | const int ret = proxy_get_presentation_position(proxy, frames, timestamp); |
| 496 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 497 | stream_unlock(&out->lock); |
Andy Hung | c9515ce | 2015-08-04 15:05:19 -0700 | [diff] [blame] | 498 | return ret; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 499 | } |
| 500 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 501 | static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 502 | { |
| 503 | return 0; |
| 504 | } |
| 505 | |
| 506 | static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 507 | { |
| 508 | return 0; |
| 509 | } |
| 510 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 511 | 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] | 512 | { |
| 513 | return -EINVAL; |
| 514 | } |
| 515 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 516 | static int adev_open_output_stream(struct audio_hw_device *hw_dev, |
Mike Lockwood | 46a9809 | 2012-04-24 16:41:18 -0700 | [diff] [blame] | 517 | audio_io_handle_t handle, |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 518 | audio_devices_t devicesSpec __unused, |
Mike Lockwood | 46a9809 | 2012-04-24 16:41:18 -0700 | [diff] [blame] | 519 | audio_output_flags_t flags, |
| 520 | struct audio_config *config, |
Eric Laurent | f5e2469 | 2014-07-27 16:14:57 -0700 | [diff] [blame] | 521 | struct audio_stream_out **stream_out, |
Paul McLean | 0f1753e | 2014-12-02 12:36:45 -0700 | [diff] [blame] | 522 | const char *address /*__unused*/) |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 523 | { |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 524 | ALOGV("adev_open_output_stream() handle:0x%X, devicesSpec:0x%X, flags:0x%X, addr:%s", |
| 525 | handle, devicesSpec, flags, address); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 526 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 527 | struct stream_out *out; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 528 | |
| 529 | out = (struct stream_out *)calloc(1, sizeof(struct stream_out)); |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 530 | if (out == NULL) { |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 531 | return -ENOMEM; |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 532 | } |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 533 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 534 | /* setup function pointers */ |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 535 | out->stream.common.get_sample_rate = out_get_sample_rate; |
| 536 | out->stream.common.set_sample_rate = out_set_sample_rate; |
| 537 | out->stream.common.get_buffer_size = out_get_buffer_size; |
| 538 | out->stream.common.get_channels = out_get_channels; |
| 539 | out->stream.common.get_format = out_get_format; |
| 540 | out->stream.common.set_format = out_set_format; |
| 541 | out->stream.common.standby = out_standby; |
| 542 | out->stream.common.dump = out_dump; |
| 543 | out->stream.common.set_parameters = out_set_parameters; |
| 544 | out->stream.common.get_parameters = out_get_parameters; |
| 545 | out->stream.common.add_audio_effect = out_add_audio_effect; |
| 546 | out->stream.common.remove_audio_effect = out_remove_audio_effect; |
| 547 | out->stream.get_latency = out_get_latency; |
| 548 | out->stream.set_volume = out_set_volume; |
| 549 | out->stream.write = out_write; |
| 550 | out->stream.get_render_position = out_get_render_position; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 551 | out->stream.get_presentation_position = out_get_presentation_position; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 552 | out->stream.get_next_write_timestamp = out_get_next_write_timestamp; |
| 553 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 554 | stream_lock_init(&out->lock); |
Eric Laurent | 7031809 | 2015-06-19 17:49:17 -0700 | [diff] [blame] | 555 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 556 | out->adev = (struct audio_device *)hw_dev; |
Paul McLean | cf5310a | 2018-08-22 14:33:12 -0700 | [diff] [blame] | 557 | |
| 558 | profile_init(&out->profile, PCM_OUT); |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 559 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 560 | // build this to hand to the alsa_device_proxy |
| 561 | struct pcm_config proxy_config; |
Paul McLean | 2c6196f | 2014-08-20 16:50:25 -0700 | [diff] [blame] | 562 | memset(&proxy_config, 0, sizeof(proxy_config)); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 563 | |
Paul McLean | 0f1753e | 2014-12-02 12:36:45 -0700 | [diff] [blame] | 564 | /* Pull out the card/device pair */ |
Paul McLean | cf5310a | 2018-08-22 14:33:12 -0700 | [diff] [blame] | 565 | parse_card_device_params(address, &out->profile.card, &out->profile.device); |
Paul McLean | 0f1753e | 2014-12-02 12:36:45 -0700 | [diff] [blame] | 566 | |
Paul McLean | cf5310a | 2018-08-22 14:33:12 -0700 | [diff] [blame] | 567 | profile_read_device_info(&out->profile); |
Paul McLean | 0f1753e | 2014-12-02 12:36:45 -0700 | [diff] [blame] | 568 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 569 | int ret = 0; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 570 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 571 | /* Rate */ |
| 572 | if (config->sample_rate == 0) { |
Paul McLean | cf5310a | 2018-08-22 14:33:12 -0700 | [diff] [blame] | 573 | proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(&out->profile); |
| 574 | } else if (profile_is_sample_rate_valid(&out->profile, config->sample_rate)) { |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 575 | proxy_config.rate = config->sample_rate; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 576 | } else { |
Paul McLean | cf5310a | 2018-08-22 14:33:12 -0700 | [diff] [blame] | 577 | proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(&out->profile); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 578 | ret = -EINVAL; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 579 | } |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 580 | |
Paul McLean | cfdbd6b | 2018-07-27 11:16:02 -0600 | [diff] [blame] | 581 | /* 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] | 582 | device_lock(out->adev); |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 583 | out->adev->device_sample_rate = config->sample_rate; |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 584 | device_unlock(out->adev); |
Paul McLean | 1d585cc | 2016-05-24 11:28:10 -0600 | [diff] [blame] | 585 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 586 | /* Format */ |
| 587 | if (config->format == AUDIO_FORMAT_DEFAULT) { |
Paul McLean | cf5310a | 2018-08-22 14:33:12 -0700 | [diff] [blame] | 588 | proxy_config.format = profile_get_default_format(&out->profile); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 589 | config->format = audio_format_from_pcm_format(proxy_config.format); |
| 590 | } else { |
| 591 | enum pcm_format fmt = pcm_format_from_audio_format(config->format); |
Paul McLean | cf5310a | 2018-08-22 14:33:12 -0700 | [diff] [blame] | 592 | if (profile_is_format_valid(&out->profile, fmt)) { |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 593 | proxy_config.format = fmt; |
| 594 | } else { |
Paul McLean | cf5310a | 2018-08-22 14:33:12 -0700 | [diff] [blame] | 595 | proxy_config.format = profile_get_default_format(&out->profile); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 596 | config->format = audio_format_from_pcm_format(proxy_config.format); |
| 597 | ret = -EINVAL; |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | /* Channels */ |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 602 | bool calc_mask = false; |
| 603 | if (config->channel_mask == AUDIO_CHANNEL_NONE) { |
| 604 | /* query case */ |
Paul McLean | cf5310a | 2018-08-22 14:33:12 -0700 | [diff] [blame] | 605 | out->hal_channel_count = profile_get_default_channel_count(&out->profile); |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 606 | calc_mask = true; |
Andy Hung | 182ddc7 | 2015-05-05 23:37:30 -0700 | [diff] [blame] | 607 | } else { |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 608 | /* explicit case */ |
| 609 | 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] | 610 | } |
Paul McLean | 698dbd7 | 2015-11-03 12:24:30 -0800 | [diff] [blame] | 611 | |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 612 | /* The Framework is currently limited to no more than this number of channels */ |
| 613 | if (out->hal_channel_count > FCC_8) { |
| 614 | out->hal_channel_count = FCC_8; |
| 615 | calc_mask = true; |
| 616 | } |
| 617 | |
| 618 | if (calc_mask) { |
| 619 | /* need to calculate the mask from channel count either because this is the query case |
| 620 | * or the specified mask isn't valid for this device, or is more then the FW can handle */ |
| 621 | config->channel_mask = out->hal_channel_count <= FCC_2 |
| 622 | /* position mask for mono and stereo*/ |
| 623 | ? audio_channel_out_mask_from_count(out->hal_channel_count) |
| 624 | /* otherwise indexed */ |
| 625 | : audio_channel_mask_for_index_assignment_from_count(out->hal_channel_count); |
| 626 | } |
| 627 | |
Andy Hung | 182ddc7 | 2015-05-05 23:37:30 -0700 | [diff] [blame] | 628 | out->hal_channel_mask = config->channel_mask; |
| 629 | |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 630 | // Validate the "logical" channel count against support in the "actual" profile. |
| 631 | // if they differ, choose the "actual" number of channels *closest* to the "logical". |
| 632 | // and store THAT in proxy_config.channels |
Paul McLean | cf5310a | 2018-08-22 14:33:12 -0700 | [diff] [blame] | 633 | proxy_config.channels = |
| 634 | profile_get_closest_channel_count(&out->profile, out->hal_channel_count); |
| 635 | proxy_prepare(&out->proxy, &out->profile, &proxy_config); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 636 | |
Paul McLean | 96c4d30 | 2017-12-05 09:50:26 -0800 | [diff] [blame] | 637 | /* TODO The retry mechanism isn't implemented in AudioPolicyManager/AudioFlinger |
| 638 | * So clear any errors that may have occurred above. |
| 639 | */ |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 640 | ret = 0; |
| 641 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 642 | out->conversion_buffer = NULL; |
| 643 | out->conversion_buffer_size = 0; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 644 | |
| 645 | out->standby = true; |
| 646 | |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 647 | /* Save the stream for adev_dump() */ |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 648 | 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] | 649 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 650 | *stream_out = &out->stream; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 651 | |
| 652 | return ret; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 653 | } |
| 654 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 655 | static void adev_close_output_stream(struct audio_hw_device *hw_dev, |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 656 | struct audio_stream_out *stream) |
| 657 | { |
| 658 | struct stream_out *out = (struct stream_out *)stream; |
Paul McLean | cf5310a | 2018-08-22 14:33:12 -0700 | [diff] [blame] | 659 | ALOGV("adev_close_output_stream(c:%d d:%d)", out->profile.card, out->profile.device); |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 660 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 661 | /* Close the pcm device */ |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 662 | out_standby(&stream->common); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 663 | |
| 664 | free(out->conversion_buffer); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 665 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 666 | out->conversion_buffer = NULL; |
| 667 | out->conversion_buffer_size = 0; |
| 668 | |
Paul McLean | cf5310a | 2018-08-22 14:33:12 -0700 | [diff] [blame] | 669 | adev_remove_stream_from_list(out->adev, &out->list_node); |
| 670 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 671 | device_lock(out->adev); |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 672 | out->adev->device_sample_rate = 0; |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 673 | device_unlock(out->adev); |
Paul McLean | 1d585cc | 2016-05-24 11:28:10 -0600 | [diff] [blame] | 674 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 675 | free(stream); |
| 676 | } |
| 677 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 678 | 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] | 679 | const struct audio_config *config) |
| 680 | { |
| 681 | /* TODO This needs to be calculated based on format/channels/rate */ |
| 682 | return 320; |
| 683 | } |
| 684 | |
| 685 | /* |
| 686 | * IN functions |
| 687 | */ |
| 688 | static uint32_t in_get_sample_rate(const struct audio_stream *stream) |
| 689 | { |
| 690 | uint32_t rate = proxy_get_sample_rate(&((const struct stream_in *)stream)->proxy); |
| 691 | ALOGV("in_get_sample_rate() = %d", rate); |
| 692 | return rate; |
| 693 | } |
| 694 | |
| 695 | static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate) |
| 696 | { |
| 697 | ALOGV("in_set_sample_rate(%d) - NOPE", rate); |
| 698 | return -ENOSYS; |
| 699 | } |
| 700 | |
| 701 | static size_t in_get_buffer_size(const struct audio_stream *stream) |
| 702 | { |
| 703 | const struct stream_in * in = ((const struct stream_in*)stream); |
Paul McLean | 2cfd81b | 2014-09-15 12:32:23 -0700 | [diff] [blame] | 704 | return proxy_get_period_size(&in->proxy) * audio_stream_in_frame_size(&(in->stream)); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | static uint32_t in_get_channels(const struct audio_stream *stream) |
| 708 | { |
Paul McLean | 2cfd81b | 2014-09-15 12:32:23 -0700 | [diff] [blame] | 709 | const struct stream_in *in = (const struct stream_in*)stream; |
Andy Hung | 780f1f8 | 2015-04-22 22:14:39 -0700 | [diff] [blame] | 710 | return in->hal_channel_mask; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | static audio_format_t in_get_format(const struct audio_stream *stream) |
| 714 | { |
Andy Hung | 780f1f8 | 2015-04-22 22:14:39 -0700 | [diff] [blame] | 715 | alsa_device_proxy *proxy = &((struct stream_in*)stream)->proxy; |
| 716 | audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy)); |
| 717 | return format; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | static int in_set_format(struct audio_stream *stream, audio_format_t format) |
| 721 | { |
| 722 | ALOGV("in_set_format(%d) - NOPE", format); |
| 723 | |
| 724 | return -ENOSYS; |
| 725 | } |
| 726 | |
| 727 | static int in_standby(struct audio_stream *stream) |
| 728 | { |
| 729 | struct stream_in *in = (struct stream_in *)stream; |
| 730 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 731 | stream_lock(&in->lock); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 732 | if (!in->standby) { |
| 733 | proxy_close(&in->proxy); |
| 734 | in->standby = true; |
| 735 | } |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 736 | stream_unlock(&in->lock); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 737 | |
| 738 | return 0; |
| 739 | } |
| 740 | |
| 741 | static int in_dump(const struct audio_stream *stream, int fd) |
| 742 | { |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 743 | const struct stream_in* in_stream = (const struct stream_in*)stream; |
| 744 | if (in_stream != NULL) { |
| 745 | dprintf(fd, "Input Profile:\n"); |
Paul McLean | cf5310a | 2018-08-22 14:33:12 -0700 | [diff] [blame] | 746 | profile_dump(&in_stream->profile, fd); |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 747 | |
| 748 | dprintf(fd, "Input Proxy:\n"); |
| 749 | proxy_dump(&in_stream->proxy, fd); |
| 750 | } |
| 751 | |
| 752 | return 0; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | static int in_set_parameters(struct audio_stream *stream, const char *kvpairs) |
| 756 | { |
Paul McLean | 65ec72b | 2015-02-18 09:13:24 -0800 | [diff] [blame] | 757 | ALOGV("in_set_parameters() keys:%s", kvpairs); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 758 | |
| 759 | struct stream_in *in = (struct stream_in *)stream; |
| 760 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 761 | int ret_value = 0; |
Eric Laurent | 05333d4 | 2014-08-04 20:29:17 -0700 | [diff] [blame] | 762 | int card = -1; |
| 763 | int device = -1; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 764 | |
Paul McLean | 65ec72b | 2015-02-18 09:13:24 -0800 | [diff] [blame] | 765 | if (!parse_card_device_params(kvpairs, &card, &device)) { |
| 766 | // nothing to do |
| 767 | return ret_value; |
| 768 | } |
| 769 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 770 | stream_lock(&in->lock); |
| 771 | device_lock(in->adev); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 772 | |
Paul McLean | cf5310a | 2018-08-22 14:33:12 -0700 | [diff] [blame] | 773 | if (card >= 0 && device >= 0 && !profile_is_cached_for(&in->profile, card, device)) { |
| 774 | /* cannot read pcm device info if capture is active, or more than one open stream */ |
Andy Hung | b10ce6d | 2017-10-27 19:37:58 -0700 | [diff] [blame] | 775 | if (!in->standby || in->adev->inputs_open > 1) |
Eric Laurent | 05333d4 | 2014-08-04 20:29:17 -0700 | [diff] [blame] | 776 | ret_value = -ENOSYS; |
| 777 | else { |
Paul McLean | cf5310a | 2018-08-22 14:33:12 -0700 | [diff] [blame] | 778 | int saved_card = in->profile.card; |
| 779 | int saved_device = in->profile.device; |
| 780 | in->profile.card = card; |
| 781 | in->profile.device = device; |
| 782 | ret_value = profile_read_device_info(&in->profile) ? 0 : -EINVAL; |
Eric Laurent | 05333d4 | 2014-08-04 20:29:17 -0700 | [diff] [blame] | 783 | if (ret_value != 0) { |
Paul McLean | cf5310a | 2018-08-22 14:33:12 -0700 | [diff] [blame] | 784 | ALOGE("Can't read device profile. card:%d, device:%d", card, device); |
| 785 | in->profile.card = saved_card; |
| 786 | in->profile.device = saved_device; |
Eric Laurent | 05333d4 | 2014-08-04 20:29:17 -0700 | [diff] [blame] | 787 | } |
| 788 | } |
Paul McLean | 2c6196f | 2014-08-20 16:50:25 -0700 | [diff] [blame] | 789 | } |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 790 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 791 | device_unlock(in->adev); |
| 792 | stream_unlock(&in->lock); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 793 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 794 | return ret_value; |
| 795 | } |
| 796 | |
| 797 | static char * in_get_parameters(const struct audio_stream *stream, const char *keys) |
| 798 | { |
| 799 | struct stream_in *in = (struct stream_in *)stream; |
| 800 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 801 | stream_lock(&in->lock); |
Paul McLean | cf5310a | 2018-08-22 14:33:12 -0700 | [diff] [blame] | 802 | char * params_str = device_get_parameters(&in->profile, keys); |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 803 | stream_unlock(&in->lock); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 804 | |
| 805 | return params_str; |
| 806 | } |
| 807 | |
| 808 | static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 809 | { |
| 810 | return 0; |
| 811 | } |
| 812 | |
| 813 | static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 814 | { |
| 815 | return 0; |
| 816 | } |
| 817 | |
| 818 | static int in_set_gain(struct audio_stream_in *stream, float gain) |
| 819 | { |
| 820 | return 0; |
| 821 | } |
| 822 | |
| 823 | /* must be called with hw device and output stream mutexes locked */ |
| 824 | static int start_input_stream(struct stream_in *in) |
| 825 | { |
Paul McLean | cf5310a | 2018-08-22 14:33:12 -0700 | [diff] [blame] | 826 | ALOGV("start_input_stream(card:%d device:%d)", in->profile.card, in->profile.device); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 827 | |
| 828 | return proxy_open(&in->proxy); |
| 829 | } |
| 830 | |
| 831 | /* TODO mutex stuff here (see out_write) */ |
| 832 | static ssize_t in_read(struct audio_stream_in *stream, void* buffer, size_t bytes) |
| 833 | { |
| 834 | size_t num_read_buff_bytes = 0; |
| 835 | void * read_buff = buffer; |
| 836 | void * out_buff = buffer; |
Pavan Chikkala | 83b47a6 | 2015-01-09 12:21:13 -0800 | [diff] [blame] | 837 | int ret = 0; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 838 | |
| 839 | struct stream_in * in = (struct stream_in *)stream; |
| 840 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 841 | stream_lock(&in->lock); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 842 | if (in->standby) { |
Eric Laurent | 7031809 | 2015-06-19 17:49:17 -0700 | [diff] [blame] | 843 | ret = start_input_stream(in); |
Eric Laurent | 7031809 | 2015-06-19 17:49:17 -0700 | [diff] [blame] | 844 | if (ret != 0) { |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 845 | goto err; |
| 846 | } |
| 847 | in->standby = false; |
| 848 | } |
| 849 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 850 | /* |
| 851 | * OK, we need to figure out how much data to read to be able to output the requested |
| 852 | * number of bytes in the HAL format (16-bit, stereo). |
| 853 | */ |
| 854 | num_read_buff_bytes = bytes; |
Andy Hung | 780f1f8 | 2015-04-22 22:14:39 -0700 | [diff] [blame] | 855 | int num_device_channels = proxy_get_channel_count(&in->proxy); /* what we told Alsa */ |
| 856 | int num_req_channels = in->hal_channel_count; /* what we told AudioFlinger */ |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 857 | |
| 858 | if (num_device_channels != num_req_channels) { |
| 859 | num_read_buff_bytes = (num_device_channels * num_read_buff_bytes) / num_req_channels; |
| 860 | } |
| 861 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 862 | /* Setup/Realloc the conversion buffer (if necessary). */ |
| 863 | if (num_read_buff_bytes != bytes) { |
| 864 | if (num_read_buff_bytes > in->conversion_buffer_size) { |
| 865 | /*TODO Remove this when AudioPolicyManger/AudioFlinger support arbitrary formats |
| 866 | (and do these conversions themselves) */ |
| 867 | in->conversion_buffer_size = num_read_buff_bytes; |
| 868 | in->conversion_buffer = realloc(in->conversion_buffer, in->conversion_buffer_size); |
| 869 | } |
| 870 | read_buff = in->conversion_buffer; |
| 871 | } |
| 872 | |
Pavan Chikkala | 83b47a6 | 2015-01-09 12:21:13 -0800 | [diff] [blame] | 873 | ret = proxy_read(&in->proxy, read_buff, num_read_buff_bytes); |
| 874 | if (ret == 0) { |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 875 | if (num_device_channels != num_req_channels) { |
| 876 | // ALOGV("chans dev:%d req:%d", num_device_channels, num_req_channels); |
| 877 | |
| 878 | out_buff = buffer; |
| 879 | /* Num Channels conversion */ |
| 880 | if (num_device_channels != num_req_channels) { |
| 881 | audio_format_t audio_format = in_get_format(&(in->stream.common)); |
| 882 | unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format); |
| 883 | |
| 884 | num_read_buff_bytes = |
| 885 | adjust_channels(read_buff, num_device_channels, |
| 886 | out_buff, num_req_channels, |
| 887 | sample_size_in_bytes, num_read_buff_bytes); |
| 888 | } |
| 889 | } |
Eric Laurent | 253def9 | 2014-09-14 12:18:18 -0700 | [diff] [blame] | 890 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 891 | /* no need to acquire in->adev->lock to read mic_muted here as we don't change its state */ |
| 892 | if (num_read_buff_bytes > 0 && in->adev->mic_muted) |
Eric Laurent | 253def9 | 2014-09-14 12:18:18 -0700 | [diff] [blame] | 893 | memset(buffer, 0, num_read_buff_bytes); |
Viswanath L | 8c7e111 | 2014-10-31 13:07:39 +0530 | [diff] [blame] | 894 | } else { |
Eric Laurent | e649942 | 2015-01-09 17:03:27 -0800 | [diff] [blame] | 895 | 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] | 896 | } |
| 897 | |
| 898 | err: |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 899 | stream_unlock(&in->lock); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 900 | return num_read_buff_bytes; |
| 901 | } |
| 902 | |
| 903 | static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream) |
| 904 | { |
| 905 | return 0; |
| 906 | } |
| 907 | |
Andy Hung | fa6b4a6 | 2018-06-04 19:14:22 -0700 | [diff] [blame] | 908 | static int in_get_capture_position(const struct audio_stream_in *stream, |
| 909 | int64_t *frames, int64_t *time) |
| 910 | { |
| 911 | struct stream_in *in = (struct stream_in *)stream; // discard const qualifier |
| 912 | stream_lock(&in->lock); |
| 913 | |
| 914 | const alsa_device_proxy *proxy = &in->proxy; |
| 915 | const int ret = proxy_get_capture_position(proxy, frames, time); |
| 916 | |
| 917 | stream_unlock(&in->lock); |
| 918 | return ret; |
| 919 | } |
| 920 | |
Paul McLean | fa3ae3e | 2018-12-12 09:57:02 -0800 | [diff] [blame] | 921 | static int in_get_active_microphones(const struct audio_stream_in *stream, |
| 922 | struct audio_microphone_characteristic_t *mic_array, |
| 923 | size_t *mic_count) { |
| 924 | (void)stream; |
| 925 | (void)mic_array; |
| 926 | (void)mic_count; |
| 927 | |
| 928 | return -ENOSYS; |
| 929 | } |
| 930 | |
| 931 | static int in_set_microphone_direction(const struct audio_stream_in *stream, |
| 932 | audio_microphone_direction_t dir) { |
| 933 | (void)stream; |
| 934 | (void)dir; |
| 935 | ALOGV("---- in_set_microphone_direction()"); |
| 936 | return -ENOSYS; |
| 937 | } |
| 938 | |
| 939 | static int in_set_microphone_field_dimension(const struct audio_stream_in *stream, float zoom) { |
| 940 | (void)zoom; |
| 941 | ALOGV("---- in_set_microphone_field_dimension()"); |
| 942 | return -ENOSYS; |
| 943 | } |
| 944 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 945 | static int adev_open_input_stream(struct audio_hw_device *hw_dev, |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 946 | audio_io_handle_t handle, |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 947 | audio_devices_t devicesSpec __unused, |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 948 | struct audio_config *config, |
| 949 | struct audio_stream_in **stream_in, |
Eric Laurent | f5e2469 | 2014-07-27 16:14:57 -0700 | [diff] [blame] | 950 | audio_input_flags_t flags __unused, |
Eric Laurent | 981f774 | 2017-05-03 12:44:26 -0700 | [diff] [blame] | 951 | const char *address, |
Eric Laurent | f5e2469 | 2014-07-27 16:14:57 -0700 | [diff] [blame] | 952 | audio_source_t source __unused) |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 953 | { |
Paul McLean | 1d585cc | 2016-05-24 11:28:10 -0600 | [diff] [blame] | 954 | ALOGV("adev_open_input_stream() rate:%" PRIu32 ", chanMask:0x%" PRIX32 ", fmt:%" PRIu8, |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 955 | config->sample_rate, config->channel_mask, config->format); |
| 956 | |
Andy Hung | b10ce6d | 2017-10-27 19:37:58 -0700 | [diff] [blame] | 957 | /* Pull out the card/device pair */ |
| 958 | int32_t card, device; |
| 959 | if (!parse_card_device_params(address, &card, &device)) { |
| 960 | ALOGW("%s fail - invalid address %s", __func__, address); |
| 961 | *stream_in = NULL; |
| 962 | return -EINVAL; |
| 963 | } |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 964 | |
Andy Hung | b10ce6d | 2017-10-27 19:37:58 -0700 | [diff] [blame] | 965 | 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] | 966 | if (in == NULL) { |
Andy Hung | b10ce6d | 2017-10-27 19:37:58 -0700 | [diff] [blame] | 967 | *stream_in = NULL; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 968 | return -ENOMEM; |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 969 | } |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 970 | |
| 971 | /* setup function pointers */ |
| 972 | in->stream.common.get_sample_rate = in_get_sample_rate; |
| 973 | in->stream.common.set_sample_rate = in_set_sample_rate; |
| 974 | in->stream.common.get_buffer_size = in_get_buffer_size; |
| 975 | in->stream.common.get_channels = in_get_channels; |
| 976 | in->stream.common.get_format = in_get_format; |
| 977 | in->stream.common.set_format = in_set_format; |
| 978 | in->stream.common.standby = in_standby; |
| 979 | in->stream.common.dump = in_dump; |
| 980 | in->stream.common.set_parameters = in_set_parameters; |
| 981 | in->stream.common.get_parameters = in_get_parameters; |
| 982 | in->stream.common.add_audio_effect = in_add_audio_effect; |
| 983 | in->stream.common.remove_audio_effect = in_remove_audio_effect; |
| 984 | |
| 985 | in->stream.set_gain = in_set_gain; |
| 986 | in->stream.read = in_read; |
| 987 | in->stream.get_input_frames_lost = in_get_input_frames_lost; |
Andy Hung | fa6b4a6 | 2018-06-04 19:14:22 -0700 | [diff] [blame] | 988 | in->stream.get_capture_position = in_get_capture_position; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 989 | |
Paul McLean | fa3ae3e | 2018-12-12 09:57:02 -0800 | [diff] [blame] | 990 | in->stream.get_active_microphones = in_get_active_microphones; |
| 991 | in->stream.set_microphone_direction = in_set_microphone_direction; |
| 992 | in->stream.set_microphone_field_dimension = in_set_microphone_field_dimension; |
| 993 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 994 | stream_lock_init(&in->lock); |
Eric Laurent | 7031809 | 2015-06-19 17:49:17 -0700 | [diff] [blame] | 995 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 996 | in->adev = (struct audio_device *)hw_dev; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 997 | |
Paul McLean | cf5310a | 2018-08-22 14:33:12 -0700 | [diff] [blame] | 998 | profile_init(&in->profile, PCM_IN); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 999 | |
| 1000 | struct pcm_config proxy_config; |
Paul McLean | 2c6196f | 2014-08-20 16:50:25 -0700 | [diff] [blame] | 1001 | memset(&proxy_config, 0, sizeof(proxy_config)); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1002 | |
Andy Hung | b10ce6d | 2017-10-27 19:37:58 -0700 | [diff] [blame] | 1003 | int ret = 0; |
Paul McLean | cf5310a | 2018-08-22 14:33:12 -0700 | [diff] [blame] | 1004 | device_lock(in->adev); |
| 1005 | int num_open_inputs = in->adev->inputs_open; |
| 1006 | device_unlock(in->adev); |
| 1007 | |
Andy Hung | b10ce6d | 2017-10-27 19:37:58 -0700 | [diff] [blame] | 1008 | /* Check if an input stream is already open */ |
Paul McLean | cf5310a | 2018-08-22 14:33:12 -0700 | [diff] [blame] | 1009 | if (num_open_inputs > 0) { |
| 1010 | if (!profile_is_cached_for(&in->profile, card, device)) { |
Andy Hung | b10ce6d | 2017-10-27 19:37:58 -0700 | [diff] [blame] | 1011 | ALOGW("%s fail - address card:%d device:%d doesn't match existing profile", |
| 1012 | __func__, card, device); |
| 1013 | ret = -EINVAL; |
| 1014 | } |
| 1015 | } else { |
| 1016 | /* Read input profile only if necessary */ |
Paul McLean | cf5310a | 2018-08-22 14:33:12 -0700 | [diff] [blame] | 1017 | in->profile.card = card; |
| 1018 | in->profile.device = device; |
| 1019 | if (!profile_read_device_info(&in->profile)) { |
Andy Hung | b10ce6d | 2017-10-27 19:37:58 -0700 | [diff] [blame] | 1020 | ALOGW("%s fail - cannot read profile", __func__); |
| 1021 | ret = -EINVAL; |
| 1022 | } |
| 1023 | } |
| 1024 | if (ret != 0) { |
Andy Hung | b10ce6d | 2017-10-27 19:37:58 -0700 | [diff] [blame] | 1025 | free(in); |
| 1026 | *stream_in = NULL; |
| 1027 | return ret; |
| 1028 | } |
Paul McLean | 0f1753e | 2014-12-02 12:36:45 -0700 | [diff] [blame] | 1029 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1030 | /* Rate */ |
Paul McLean | cfdbd6b | 2018-07-27 11:16:02 -0600 | [diff] [blame] | 1031 | int request_config_rate = config->sample_rate; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1032 | if (config->sample_rate == 0) { |
Paul McLean | cf5310a | 2018-08-22 14:33:12 -0700 | [diff] [blame] | 1033 | config->sample_rate = profile_get_default_sample_rate(&in->profile); |
Paul McLean | 9a1c305 | 2016-05-25 13:30:54 -0600 | [diff] [blame] | 1034 | } |
| 1035 | |
Paul McLean | cfdbd6b | 2018-07-27 11:16:02 -0600 | [diff] [blame] | 1036 | 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] | 1037 | 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] | 1038 | if (config->sample_rate != in->adev->device_sample_rate) { |
Paul McLean | cf5310a | 2018-08-22 14:33:12 -0700 | [diff] [blame] | 1039 | unsigned highest_rate = profile_get_highest_sample_rate(&in->profile); |
Paul McLean | cfdbd6b | 2018-07-27 11:16:02 -0600 | [diff] [blame] | 1040 | if (highest_rate == 0) { |
| 1041 | ret = -EINVAL; /* error with device */ |
| 1042 | } else { |
| 1043 | proxy_config.rate = config->sample_rate = |
| 1044 | min(highest_rate, in->adev->device_sample_rate); |
| 1045 | if (request_config_rate != 0 && proxy_config.rate != config->sample_rate) { |
| 1046 | /* Changing the requested rate */ |
| 1047 | ret = -EINVAL; |
| 1048 | } else { |
| 1049 | /* Everything AOK! */ |
| 1050 | ret = 0; |
| 1051 | } |
| 1052 | } |
| 1053 | } |
Paul McLean | cf5310a | 2018-08-22 14:33:12 -0700 | [diff] [blame] | 1054 | } else if (profile_is_sample_rate_valid(&in->profile, config->sample_rate)) { |
Eric Laurent | 981f774 | 2017-05-03 12:44:26 -0700 | [diff] [blame] | 1055 | proxy_config.rate = config->sample_rate; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1056 | } else { |
Paul McLean | cf5310a | 2018-08-22 14:33:12 -0700 | [diff] [blame] | 1057 | proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(&in->profile); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1058 | ret = -EINVAL; |
| 1059 | } |
Paul McLean | cfdbd6b | 2018-07-27 11:16:02 -0600 | [diff] [blame] | 1060 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1061 | /* Format */ |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1062 | if (config->format == AUDIO_FORMAT_DEFAULT) { |
Paul McLean | cf5310a | 2018-08-22 14:33:12 -0700 | [diff] [blame] | 1063 | proxy_config.format = profile_get_default_format(&in->profile); |
Andy Hung | 780f1f8 | 2015-04-22 22:14:39 -0700 | [diff] [blame] | 1064 | config->format = audio_format_from_pcm_format(proxy_config.format); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1065 | } else { |
Andy Hung | 780f1f8 | 2015-04-22 22:14:39 -0700 | [diff] [blame] | 1066 | enum pcm_format fmt = pcm_format_from_audio_format(config->format); |
Paul McLean | cf5310a | 2018-08-22 14:33:12 -0700 | [diff] [blame] | 1067 | if (profile_is_format_valid(&in->profile, fmt)) { |
Andy Hung | 780f1f8 | 2015-04-22 22:14:39 -0700 | [diff] [blame] | 1068 | proxy_config.format = fmt; |
| 1069 | } else { |
Paul McLean | cf5310a | 2018-08-22 14:33:12 -0700 | [diff] [blame] | 1070 | proxy_config.format = profile_get_default_format(&in->profile); |
Andy Hung | 780f1f8 | 2015-04-22 22:14:39 -0700 | [diff] [blame] | 1071 | config->format = audio_format_from_pcm_format(proxy_config.format); |
| 1072 | ret = -EINVAL; |
| 1073 | } |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1074 | } |
| 1075 | |
Paul McLean | 2cfd81b | 2014-09-15 12:32:23 -0700 | [diff] [blame] | 1076 | /* Channels */ |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 1077 | bool calc_mask = false; |
| 1078 | if (config->channel_mask == AUDIO_CHANNEL_NONE) { |
| 1079 | /* query case */ |
Paul McLean | cf5310a | 2018-08-22 14:33:12 -0700 | [diff] [blame] | 1080 | in->hal_channel_count = profile_get_default_channel_count(&in->profile); |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 1081 | calc_mask = true; |
Andy Hung | 780f1f8 | 2015-04-22 22:14:39 -0700 | [diff] [blame] | 1082 | } else { |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 1083 | /* explicit case */ |
Andy Hung | 780f1f8 | 2015-04-22 22:14:39 -0700 | [diff] [blame] | 1084 | in->hal_channel_count = audio_channel_count_from_in_mask(config->channel_mask); |
| 1085 | } |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1086 | |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 1087 | /* The Framework is currently limited to no more than this number of channels */ |
| 1088 | if (in->hal_channel_count > FCC_8) { |
| 1089 | in->hal_channel_count = FCC_8; |
| 1090 | calc_mask = true; |
| 1091 | } |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1092 | |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 1093 | if (calc_mask) { |
| 1094 | /* need to calculate the mask from channel count either because this is the query case |
| 1095 | * or the specified mask isn't valid for this device, or is more then the FW can handle */ |
| 1096 | in->hal_channel_mask = in->hal_channel_count <= FCC_2 |
| 1097 | /* position mask for mono & stereo */ |
| 1098 | ? audio_channel_in_mask_from_count(in->hal_channel_count) |
| 1099 | /* otherwise indexed */ |
| 1100 | : audio_channel_mask_for_index_assignment_from_count(in->hal_channel_count); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1101 | |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 1102 | // if we change the mask... |
| 1103 | if (in->hal_channel_mask != config->channel_mask && |
| 1104 | config->channel_mask != AUDIO_CHANNEL_NONE) { |
| 1105 | config->channel_mask = in->hal_channel_mask; |
| 1106 | ret = -EINVAL; |
| 1107 | } |
| 1108 | } else { |
| 1109 | in->hal_channel_mask = config->channel_mask; |
| 1110 | } |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 1111 | |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 1112 | if (ret == 0) { |
| 1113 | // Validate the "logical" channel count against support in the "actual" profile. |
| 1114 | // if they differ, choose the "actual" number of channels *closest* to the "logical". |
| 1115 | // and store THAT in proxy_config.channels |
| 1116 | proxy_config.channels = |
Paul McLean | cf5310a | 2018-08-22 14:33:12 -0700 | [diff] [blame] | 1117 | profile_get_closest_channel_count(&in->profile, in->hal_channel_count); |
| 1118 | ret = proxy_prepare(&in->proxy, &in->profile, &proxy_config); |
Eric Laurent | 981f774 | 2017-05-03 12:44:26 -0700 | [diff] [blame] | 1119 | if (ret == 0) { |
| 1120 | in->standby = true; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1121 | |
Eric Laurent | 981f774 | 2017-05-03 12:44:26 -0700 | [diff] [blame] | 1122 | in->conversion_buffer = NULL; |
| 1123 | in->conversion_buffer_size = 0; |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 1124 | |
Eric Laurent | 981f774 | 2017-05-03 12:44:26 -0700 | [diff] [blame] | 1125 | *stream_in = &in->stream; |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 1126 | |
Eric Laurent | 981f774 | 2017-05-03 12:44:26 -0700 | [diff] [blame] | 1127 | /* Save this for adev_dump() */ |
| 1128 | adev_add_stream_to_list(in->adev, &in->adev->input_stream_list, &in->list_node); |
| 1129 | } else { |
| 1130 | ALOGW("proxy_prepare error %d", ret); |
| 1131 | unsigned channel_count = proxy_get_channel_count(&in->proxy); |
| 1132 | config->channel_mask = channel_count <= FCC_2 |
| 1133 | ? audio_channel_in_mask_from_count(channel_count) |
| 1134 | : audio_channel_mask_for_index_assignment_from_count(channel_count); |
| 1135 | config->format = audio_format_from_pcm_format(proxy_get_format(&in->proxy)); |
| 1136 | config->sample_rate = proxy_get_sample_rate(&in->proxy); |
| 1137 | } |
| 1138 | } |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 1139 | |
Eric Laurent | 981f774 | 2017-05-03 12:44:26 -0700 | [diff] [blame] | 1140 | if (ret != 0) { |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 1141 | // Deallocate this stream on error, because AudioFlinger won't call |
| 1142 | // adev_close_input_stream() in this case. |
| 1143 | *stream_in = NULL; |
| 1144 | free(in); |
| 1145 | } |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1146 | |
Andy Hung | b10ce6d | 2017-10-27 19:37:58 -0700 | [diff] [blame] | 1147 | device_lock(in->adev); |
| 1148 | ++in->adev->inputs_open; |
| 1149 | device_unlock(in->adev); |
| 1150 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1151 | return ret; |
| 1152 | } |
| 1153 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 1154 | static void adev_close_input_stream(struct audio_hw_device *hw_dev, |
| 1155 | struct audio_stream_in *stream) |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1156 | { |
| 1157 | struct stream_in *in = (struct stream_in *)stream; |
Paul McLean | cf5310a | 2018-08-22 14:33:12 -0700 | [diff] [blame] | 1158 | ALOGV("adev_close_input_stream(c:%d d:%d)", in->profile.card, in->profile.device); |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 1159 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 1160 | adev_remove_stream_from_list(in->adev, &in->list_node); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1161 | |
Andy Hung | b10ce6d | 2017-10-27 19:37:58 -0700 | [diff] [blame] | 1162 | device_lock(in->adev); |
| 1163 | --in->adev->inputs_open; |
| 1164 | LOG_ALWAYS_FATAL_IF(in->adev->inputs_open < 0, |
| 1165 | "invalid inputs_open: %d", in->adev->inputs_open); |
| 1166 | device_unlock(in->adev); |
| 1167 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1168 | /* Close the pcm device */ |
| 1169 | in_standby(&stream->common); |
| 1170 | |
| 1171 | free(in->conversion_buffer); |
| 1172 | |
| 1173 | free(stream); |
| 1174 | } |
| 1175 | |
| 1176 | /* |
| 1177 | * ADEV Functions |
| 1178 | */ |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 1179 | 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] | 1180 | { |
| 1181 | return 0; |
| 1182 | } |
| 1183 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 1184 | 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] | 1185 | { |
| 1186 | return strdup(""); |
| 1187 | } |
| 1188 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 1189 | static int adev_init_check(const struct audio_hw_device *hw_dev) |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1190 | { |
| 1191 | return 0; |
| 1192 | } |
| 1193 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 1194 | 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] | 1195 | { |
| 1196 | return -ENOSYS; |
| 1197 | } |
| 1198 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 1199 | 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] | 1200 | { |
| 1201 | return -ENOSYS; |
| 1202 | } |
| 1203 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 1204 | 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] | 1205 | { |
| 1206 | return 0; |
| 1207 | } |
| 1208 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 1209 | 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] | 1210 | { |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 1211 | struct audio_device * adev = (struct audio_device *)hw_dev; |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 1212 | device_lock(adev); |
Eric Laurent | 253def9 | 2014-09-14 12:18:18 -0700 | [diff] [blame] | 1213 | adev->mic_muted = state; |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 1214 | device_unlock(adev); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1215 | return -ENOSYS; |
| 1216 | } |
| 1217 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 1218 | 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] | 1219 | { |
| 1220 | return -ENOSYS; |
| 1221 | } |
| 1222 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 1223 | static int adev_dump(const struct audio_hw_device *device, int fd) |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1224 | { |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 1225 | dprintf(fd, "\nUSB audio module:\n"); |
| 1226 | |
| 1227 | struct audio_device* adev = (struct audio_device*)device; |
| 1228 | const int kNumRetries = 3; |
| 1229 | const int kSleepTimeMS = 500; |
| 1230 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 1231 | // use device_try_lock() in case we dumpsys during a deadlock |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 1232 | int retry = kNumRetries; |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 1233 | while (retry > 0 && device_try_lock(adev) != 0) { |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 1234 | sleep(kSleepTimeMS); |
| 1235 | retry--; |
| 1236 | } |
| 1237 | |
| 1238 | if (retry > 0) { |
| 1239 | if (list_empty(&adev->output_stream_list)) { |
| 1240 | dprintf(fd, " No output streams.\n"); |
| 1241 | } else { |
| 1242 | struct listnode* node; |
| 1243 | list_for_each(node, &adev->output_stream_list) { |
| 1244 | struct audio_stream* stream = |
| 1245 | (struct audio_stream *)node_to_item(node, struct stream_out, list_node); |
| 1246 | out_dump(stream, fd); |
| 1247 | } |
| 1248 | } |
| 1249 | |
| 1250 | if (list_empty(&adev->input_stream_list)) { |
| 1251 | dprintf(fd, "\n No input streams.\n"); |
| 1252 | } else { |
| 1253 | struct listnode* node; |
| 1254 | list_for_each(node, &adev->input_stream_list) { |
| 1255 | struct audio_stream* stream = |
| 1256 | (struct audio_stream *)node_to_item(node, struct stream_in, list_node); |
| 1257 | in_dump(stream, fd); |
| 1258 | } |
| 1259 | } |
| 1260 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 1261 | device_unlock(adev); |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 1262 | } else { |
| 1263 | // Couldn't lock |
| 1264 | dprintf(fd, " Could not obtain device lock.\n"); |
| 1265 | } |
| 1266 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1267 | return 0; |
| 1268 | } |
| 1269 | |
| 1270 | static int adev_close(hw_device_t *device) |
| 1271 | { |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1272 | free(device); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1273 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1274 | return 0; |
| 1275 | } |
| 1276 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1277 | 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] | 1278 | { |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1279 | if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) |
| 1280 | return -EINVAL; |
| 1281 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1282 | struct audio_device *adev = calloc(1, sizeof(struct audio_device)); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1283 | if (!adev) |
| 1284 | return -ENOMEM; |
| 1285 | |
Paul McLean | cf5310a | 2018-08-22 14:33:12 -0700 | [diff] [blame] | 1286 | pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1287 | |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 1288 | list_init(&adev->output_stream_list); |
| 1289 | list_init(&adev->input_stream_list); |
| 1290 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1291 | adev->hw_device.common.tag = HARDWARE_DEVICE_TAG; |
Eric Laurent | 85e08e2 | 2012-08-28 14:30:35 -0700 | [diff] [blame] | 1292 | adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1293 | adev->hw_device.common.module = (struct hw_module_t *)module; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1294 | adev->hw_device.common.close = adev_close; |
| 1295 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1296 | adev->hw_device.init_check = adev_init_check; |
| 1297 | adev->hw_device.set_voice_volume = adev_set_voice_volume; |
| 1298 | adev->hw_device.set_master_volume = adev_set_master_volume; |
| 1299 | adev->hw_device.set_mode = adev_set_mode; |
| 1300 | adev->hw_device.set_mic_mute = adev_set_mic_mute; |
| 1301 | adev->hw_device.get_mic_mute = adev_get_mic_mute; |
| 1302 | adev->hw_device.set_parameters = adev_set_parameters; |
| 1303 | adev->hw_device.get_parameters = adev_get_parameters; |
| 1304 | adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size; |
| 1305 | adev->hw_device.open_output_stream = adev_open_output_stream; |
| 1306 | adev->hw_device.close_output_stream = adev_close_output_stream; |
| 1307 | adev->hw_device.open_input_stream = adev_open_input_stream; |
| 1308 | adev->hw_device.close_input_stream = adev_close_input_stream; |
| 1309 | adev->hw_device.dump = adev_dump; |
| 1310 | |
| 1311 | *device = &adev->hw_device.common; |
| 1312 | |
| 1313 | return 0; |
| 1314 | } |
| 1315 | |
| 1316 | static struct hw_module_methods_t hal_module_methods = { |
| 1317 | .open = adev_open, |
| 1318 | }; |
| 1319 | |
| 1320 | struct audio_module HAL_MODULE_INFO_SYM = { |
| 1321 | .common = { |
| 1322 | .tag = HARDWARE_MODULE_TAG, |
Mike Lockwood | 46a9809 | 2012-04-24 16:41:18 -0700 | [diff] [blame] | 1323 | .module_api_version = AUDIO_MODULE_API_VERSION_0_1, |
| 1324 | .hal_api_version = HARDWARE_HAL_API_VERSION, |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1325 | .id = AUDIO_HARDWARE_MODULE_ID, |
| 1326 | .name = "USB audio HW HAL", |
| 1327 | .author = "The Android Open Source Project", |
| 1328 | .methods = &hal_module_methods, |
| 1329 | }, |
| 1330 | }; |