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