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 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 378 | int routing = 0; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 379 | int ret_value = 0; |
Eric Laurent | 05333d4 | 2014-08-04 20:29:17 -0700 | [diff] [blame] | 380 | int card = -1; |
| 381 | int device = -1; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 382 | |
Paul McLean | 65ec72b | 2015-02-18 09:13:24 -0800 | [diff] [blame] | 383 | if (!parse_card_device_params(kvpairs, &card, &device)) { |
| 384 | // nothing to do |
| 385 | return ret_value; |
| 386 | } |
| 387 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 388 | stream_lock(&out->lock); |
Paul McLean | 65ec72b | 2015-02-18 09:13:24 -0800 | [diff] [blame] | 389 | /* Lock the device because that is where the profile lives */ |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 390 | device_lock(out->adev); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 391 | |
Paul McLean | 65ec72b | 2015-02-18 09:13:24 -0800 | [diff] [blame] | 392 | if (!profile_is_cached_for(out->profile, card, device)) { |
Eric Laurent | 05333d4 | 2014-08-04 20:29:17 -0700 | [diff] [blame] | 393 | /* cannot read pcm device info if playback is active */ |
| 394 | if (!out->standby) |
| 395 | ret_value = -ENOSYS; |
| 396 | else { |
| 397 | int saved_card = out->profile->card; |
| 398 | int saved_device = out->profile->device; |
Andy Hung | 4e6a1c0 | 2017-10-27 20:31:46 -0700 | [diff] [blame] | 399 | out->adev->out_profile.card = card; |
| 400 | out->adev->out_profile.device = device; |
| 401 | ret_value = profile_read_device_info(&out->adev->out_profile) ? 0 : -EINVAL; |
Eric Laurent | 05333d4 | 2014-08-04 20:29:17 -0700 | [diff] [blame] | 402 | if (ret_value != 0) { |
Andy Hung | 4e6a1c0 | 2017-10-27 20:31:46 -0700 | [diff] [blame] | 403 | out->adev->out_profile.card = saved_card; |
| 404 | out->adev->out_profile.device = saved_device; |
Eric Laurent | 05333d4 | 2014-08-04 20:29:17 -0700 | [diff] [blame] | 405 | } |
| 406 | } |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 407 | } |
Paul McLean | 2c6196f | 2014-08-20 16:50:25 -0700 | [diff] [blame] | 408 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 409 | device_unlock(out->adev); |
| 410 | stream_unlock(&out->lock); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 411 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 412 | return ret_value; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 415 | static char * out_get_parameters(const struct audio_stream *stream, const char *keys) |
| 416 | { |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 417 | struct stream_out *out = (struct stream_out *)stream; |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 418 | stream_lock(&out->lock); |
| 419 | device_lock(out->adev); |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 420 | |
| 421 | char * params_str = device_get_parameters(out->profile, keys); |
| 422 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 423 | device_unlock(out->adev); |
| 424 | stream_unlock(&out->lock); |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 425 | return params_str; |
| 426 | } |
| 427 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 428 | static uint32_t out_get_latency(const struct audio_stream_out *stream) |
| 429 | { |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 430 | alsa_device_proxy * proxy = &((struct stream_out*)stream)->proxy; |
| 431 | return proxy_get_latency(proxy); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 432 | } |
| 433 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 434 | 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] | 435 | { |
| 436 | return -ENOSYS; |
| 437 | } |
| 438 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 439 | /* must be called with hw device and output stream mutexes locked */ |
| 440 | static int start_output_stream(struct stream_out *out) |
| 441 | { |
Paul McLean | 65ec72b | 2015-02-18 09:13:24 -0800 | [diff] [blame] | 442 | 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] | 443 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 444 | return proxy_open(&out->proxy); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | 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] | 448 | { |
| 449 | int ret; |
| 450 | struct stream_out *out = (struct stream_out *)stream; |
| 451 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 452 | stream_lock(&out->lock); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 453 | if (out->standby) { |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 454 | device_lock(out->adev); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 455 | ret = start_output_stream(out); |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 456 | device_unlock(out->adev); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 457 | if (ret != 0) { |
| 458 | goto err; |
| 459 | } |
| 460 | out->standby = false; |
| 461 | } |
Eric Laurent | 05333d4 | 2014-08-04 20:29:17 -0700 | [diff] [blame] | 462 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 463 | alsa_device_proxy* proxy = &out->proxy; |
Mark Salyzyn | 88e458a | 2014-04-28 12:30:44 -0700 | [diff] [blame] | 464 | const void * write_buff = buffer; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 465 | int num_write_buff_bytes = bytes; |
Andy Hung | 03576be | 2014-07-21 21:16:45 -0700 | [diff] [blame] | 466 | const int num_device_channels = proxy_get_channel_count(proxy); /* what we told alsa */ |
| 467 | 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] | 468 | if (num_device_channels != num_req_channels) { |
Andy Hung | 03576be | 2014-07-21 21:16:45 -0700 | [diff] [blame] | 469 | /* allocate buffer */ |
| 470 | const size_t required_conversion_buffer_size = |
| 471 | bytes * num_device_channels / num_req_channels; |
| 472 | if (required_conversion_buffer_size > out->conversion_buffer_size) { |
| 473 | out->conversion_buffer_size = required_conversion_buffer_size; |
| 474 | out->conversion_buffer = realloc(out->conversion_buffer, |
| 475 | out->conversion_buffer_size); |
| 476 | } |
| 477 | /* convert data */ |
| 478 | const audio_format_t audio_format = out_get_format(&(out->stream.common)); |
| 479 | const unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 480 | num_write_buff_bytes = |
Andy Hung | 03576be | 2014-07-21 21:16:45 -0700 | [diff] [blame] | 481 | adjust_channels(write_buff, num_req_channels, |
| 482 | out->conversion_buffer, num_device_channels, |
| 483 | sample_size_in_bytes, num_write_buff_bytes); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 484 | write_buff = out->conversion_buffer; |
| 485 | } |
| 486 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 487 | if (write_buff != NULL && num_write_buff_bytes != 0) { |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 488 | proxy_write(&out->proxy, write_buff, num_write_buff_bytes); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 489 | } |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 490 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 491 | stream_unlock(&out->lock); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 492 | |
| 493 | return bytes; |
| 494 | |
| 495 | err: |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 496 | stream_unlock(&out->lock); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 497 | if (ret != 0) { |
Eric Laurent | c5ae6a0 | 2014-07-02 13:45:32 -0700 | [diff] [blame] | 498 | usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) / |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 499 | out_get_sample_rate(&stream->common)); |
| 500 | } |
| 501 | |
| 502 | return bytes; |
| 503 | } |
| 504 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 505 | 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] | 506 | { |
| 507 | return -EINVAL; |
| 508 | } |
| 509 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 510 | static int out_get_presentation_position(const struct audio_stream_out *stream, |
| 511 | uint64_t *frames, struct timespec *timestamp) |
| 512 | { |
Andy Hung | c9515ce | 2015-08-04 15:05:19 -0700 | [diff] [blame] | 513 | struct stream_out *out = (struct stream_out *)stream; // discard const qualifier |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 514 | stream_lock(&out->lock); |
Andy Hung | c9515ce | 2015-08-04 15:05:19 -0700 | [diff] [blame] | 515 | |
| 516 | const alsa_device_proxy *proxy = &out->proxy; |
| 517 | const int ret = proxy_get_presentation_position(proxy, frames, timestamp); |
| 518 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 519 | stream_unlock(&out->lock); |
Andy Hung | c9515ce | 2015-08-04 15:05:19 -0700 | [diff] [blame] | 520 | return ret; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 523 | static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 524 | { |
| 525 | return 0; |
| 526 | } |
| 527 | |
| 528 | static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 529 | { |
| 530 | return 0; |
| 531 | } |
| 532 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 533 | 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] | 534 | { |
| 535 | return -EINVAL; |
| 536 | } |
| 537 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 538 | static int adev_open_output_stream(struct audio_hw_device *hw_dev, |
Mike Lockwood | 46a9809 | 2012-04-24 16:41:18 -0700 | [diff] [blame] | 539 | audio_io_handle_t handle, |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 540 | audio_devices_t devicesSpec __unused, |
Mike Lockwood | 46a9809 | 2012-04-24 16:41:18 -0700 | [diff] [blame] | 541 | audio_output_flags_t flags, |
| 542 | struct audio_config *config, |
Eric Laurent | f5e2469 | 2014-07-27 16:14:57 -0700 | [diff] [blame] | 543 | struct audio_stream_out **stream_out, |
Paul McLean | 0f1753e | 2014-12-02 12:36:45 -0700 | [diff] [blame] | 544 | const char *address /*__unused*/) |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 545 | { |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 546 | ALOGV("adev_open_output_stream() handle:0x%X, devicesSpec:0x%X, flags:0x%X, addr:%s", |
| 547 | handle, devicesSpec, flags, address); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 548 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 549 | struct stream_out *out; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 550 | |
| 551 | out = (struct stream_out *)calloc(1, sizeof(struct stream_out)); |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 552 | if (out == NULL) { |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 553 | return -ENOMEM; |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 554 | } |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 555 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 556 | /* setup function pointers */ |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 557 | out->stream.common.get_sample_rate = out_get_sample_rate; |
| 558 | out->stream.common.set_sample_rate = out_set_sample_rate; |
| 559 | out->stream.common.get_buffer_size = out_get_buffer_size; |
| 560 | out->stream.common.get_channels = out_get_channels; |
| 561 | out->stream.common.get_format = out_get_format; |
| 562 | out->stream.common.set_format = out_set_format; |
| 563 | out->stream.common.standby = out_standby; |
| 564 | out->stream.common.dump = out_dump; |
| 565 | out->stream.common.set_parameters = out_set_parameters; |
| 566 | out->stream.common.get_parameters = out_get_parameters; |
| 567 | out->stream.common.add_audio_effect = out_add_audio_effect; |
| 568 | out->stream.common.remove_audio_effect = out_remove_audio_effect; |
| 569 | out->stream.get_latency = out_get_latency; |
| 570 | out->stream.set_volume = out_set_volume; |
| 571 | out->stream.write = out_write; |
| 572 | out->stream.get_render_position = out_get_render_position; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 573 | out->stream.get_presentation_position = out_get_presentation_position; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 574 | out->stream.get_next_write_timestamp = out_get_next_write_timestamp; |
| 575 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 576 | stream_lock_init(&out->lock); |
Eric Laurent | 7031809 | 2015-06-19 17:49:17 -0700 | [diff] [blame] | 577 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 578 | out->adev = (struct audio_device *)hw_dev; |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 579 | device_lock(out->adev); |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 580 | out->profile = &out->adev->out_profile; |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 581 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 582 | // build this to hand to the alsa_device_proxy |
| 583 | struct pcm_config proxy_config; |
Paul McLean | 2c6196f | 2014-08-20 16:50:25 -0700 | [diff] [blame] | 584 | memset(&proxy_config, 0, sizeof(proxy_config)); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 585 | |
Paul McLean | 0f1753e | 2014-12-02 12:36:45 -0700 | [diff] [blame] | 586 | /* Pull out the card/device pair */ |
Andy Hung | 4e6a1c0 | 2017-10-27 20:31:46 -0700 | [diff] [blame] | 587 | 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] | 588 | |
Andy Hung | 4e6a1c0 | 2017-10-27 20:31:46 -0700 | [diff] [blame] | 589 | profile_read_device_info(&out->adev->out_profile); |
Paul McLean | 0f1753e | 2014-12-02 12:36:45 -0700 | [diff] [blame] | 590 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 591 | int ret = 0; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 592 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 593 | /* Rate */ |
| 594 | if (config->sample_rate == 0) { |
| 595 | proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(out->profile); |
| 596 | } else if (profile_is_sample_rate_valid(out->profile, config->sample_rate)) { |
| 597 | proxy_config.rate = config->sample_rate; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 598 | } else { |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 599 | proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(out->profile); |
| 600 | ret = -EINVAL; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 601 | } |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 602 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 603 | out->adev->device_sample_rate = config->sample_rate; |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 604 | device_unlock(out->adev); |
Paul McLean | 1d585cc | 2016-05-24 11:28:10 -0600 | [diff] [blame] | 605 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 606 | /* Format */ |
| 607 | if (config->format == AUDIO_FORMAT_DEFAULT) { |
| 608 | proxy_config.format = profile_get_default_format(out->profile); |
| 609 | config->format = audio_format_from_pcm_format(proxy_config.format); |
| 610 | } else { |
| 611 | enum pcm_format fmt = pcm_format_from_audio_format(config->format); |
| 612 | if (profile_is_format_valid(out->profile, fmt)) { |
| 613 | proxy_config.format = fmt; |
| 614 | } else { |
| 615 | proxy_config.format = profile_get_default_format(out->profile); |
| 616 | config->format = audio_format_from_pcm_format(proxy_config.format); |
| 617 | ret = -EINVAL; |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | /* Channels */ |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 622 | bool calc_mask = false; |
| 623 | if (config->channel_mask == AUDIO_CHANNEL_NONE) { |
| 624 | /* query case */ |
| 625 | out->hal_channel_count = profile_get_default_channel_count(out->profile); |
| 626 | calc_mask = true; |
Andy Hung | 182ddc7 | 2015-05-05 23:37:30 -0700 | [diff] [blame] | 627 | } else { |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 628 | /* explicit case */ |
| 629 | 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] | 630 | } |
Paul McLean | 698dbd7 | 2015-11-03 12:24:30 -0800 | [diff] [blame] | 631 | |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 632 | /* The Framework is currently limited to no more than this number of channels */ |
| 633 | if (out->hal_channel_count > FCC_8) { |
| 634 | out->hal_channel_count = FCC_8; |
| 635 | calc_mask = true; |
| 636 | } |
| 637 | |
| 638 | if (calc_mask) { |
| 639 | /* need to calculate the mask from channel count either because this is the query case |
| 640 | * or the specified mask isn't valid for this device, or is more then the FW can handle */ |
| 641 | config->channel_mask = out->hal_channel_count <= FCC_2 |
| 642 | /* position mask for mono and stereo*/ |
| 643 | ? audio_channel_out_mask_from_count(out->hal_channel_count) |
| 644 | /* otherwise indexed */ |
| 645 | : audio_channel_mask_for_index_assignment_from_count(out->hal_channel_count); |
| 646 | } |
| 647 | |
Andy Hung | 182ddc7 | 2015-05-05 23:37:30 -0700 | [diff] [blame] | 648 | out->hal_channel_mask = config->channel_mask; |
| 649 | |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 650 | // Validate the "logical" channel count against support in the "actual" profile. |
| 651 | // if they differ, choose the "actual" number of channels *closest* to the "logical". |
| 652 | // and store THAT in proxy_config.channels |
| 653 | 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] | 654 | proxy_prepare(&out->proxy, out->profile, &proxy_config); |
| 655 | |
| 656 | /* TODO The retry mechanism isn't implemented in AudioPolicyManager/AudioFlinger. */ |
| 657 | ret = 0; |
| 658 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 659 | out->conversion_buffer = NULL; |
| 660 | out->conversion_buffer_size = 0; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 661 | |
| 662 | out->standby = true; |
| 663 | |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 664 | /* Save the stream for adev_dump() */ |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 665 | 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] | 666 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 667 | *stream_out = &out->stream; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 668 | |
| 669 | return ret; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 670 | |
| 671 | err_open: |
| 672 | free(out); |
| 673 | *stream_out = NULL; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 674 | return -ENOSYS; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 675 | } |
| 676 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 677 | static void adev_close_output_stream(struct audio_hw_device *hw_dev, |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 678 | struct audio_stream_out *stream) |
| 679 | { |
| 680 | struct stream_out *out = (struct stream_out *)stream; |
Paul McLean | 65ec72b | 2015-02-18 09:13:24 -0800 | [diff] [blame] | 681 | 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] | 682 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 683 | adev_remove_stream_from_list(out->adev, &out->list_node); |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 684 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 685 | /* Close the pcm device */ |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 686 | out_standby(&stream->common); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 687 | |
| 688 | free(out->conversion_buffer); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 689 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 690 | out->conversion_buffer = NULL; |
| 691 | out->conversion_buffer_size = 0; |
| 692 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 693 | device_lock(out->adev); |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 694 | out->adev->device_sample_rate = 0; |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 695 | device_unlock(out->adev); |
Paul McLean | 1d585cc | 2016-05-24 11:28:10 -0600 | [diff] [blame] | 696 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 697 | free(stream); |
| 698 | } |
| 699 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 700 | 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] | 701 | const struct audio_config *config) |
| 702 | { |
| 703 | /* TODO This needs to be calculated based on format/channels/rate */ |
| 704 | return 320; |
| 705 | } |
| 706 | |
| 707 | /* |
| 708 | * IN functions |
| 709 | */ |
| 710 | static uint32_t in_get_sample_rate(const struct audio_stream *stream) |
| 711 | { |
| 712 | uint32_t rate = proxy_get_sample_rate(&((const struct stream_in *)stream)->proxy); |
| 713 | ALOGV("in_get_sample_rate() = %d", rate); |
| 714 | return rate; |
| 715 | } |
| 716 | |
| 717 | static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate) |
| 718 | { |
| 719 | ALOGV("in_set_sample_rate(%d) - NOPE", rate); |
| 720 | return -ENOSYS; |
| 721 | } |
| 722 | |
| 723 | static size_t in_get_buffer_size(const struct audio_stream *stream) |
| 724 | { |
| 725 | const struct stream_in * in = ((const struct stream_in*)stream); |
Paul McLean | 2cfd81b | 2014-09-15 12:32:23 -0700 | [diff] [blame] | 726 | 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] | 727 | } |
| 728 | |
| 729 | static uint32_t in_get_channels(const struct audio_stream *stream) |
| 730 | { |
Paul McLean | 2cfd81b | 2014-09-15 12:32:23 -0700 | [diff] [blame] | 731 | const struct stream_in *in = (const struct stream_in*)stream; |
Andy Hung | 780f1f8 | 2015-04-22 22:14:39 -0700 | [diff] [blame] | 732 | return in->hal_channel_mask; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | static audio_format_t in_get_format(const struct audio_stream *stream) |
| 736 | { |
Andy Hung | 780f1f8 | 2015-04-22 22:14:39 -0700 | [diff] [blame] | 737 | alsa_device_proxy *proxy = &((struct stream_in*)stream)->proxy; |
| 738 | audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy)); |
| 739 | return format; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | static int in_set_format(struct audio_stream *stream, audio_format_t format) |
| 743 | { |
| 744 | ALOGV("in_set_format(%d) - NOPE", format); |
| 745 | |
| 746 | return -ENOSYS; |
| 747 | } |
| 748 | |
| 749 | static int in_standby(struct audio_stream *stream) |
| 750 | { |
| 751 | struct stream_in *in = (struct stream_in *)stream; |
| 752 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 753 | stream_lock(&in->lock); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 754 | if (!in->standby) { |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 755 | device_lock(in->adev); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 756 | proxy_close(&in->proxy); |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 757 | device_unlock(in->adev); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 758 | in->standby = true; |
| 759 | } |
| 760 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 761 | stream_unlock(&in->lock); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 762 | |
| 763 | return 0; |
| 764 | } |
| 765 | |
| 766 | static int in_dump(const struct audio_stream *stream, int fd) |
| 767 | { |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 768 | const struct stream_in* in_stream = (const struct stream_in*)stream; |
| 769 | if (in_stream != NULL) { |
| 770 | dprintf(fd, "Input Profile:\n"); |
| 771 | profile_dump(in_stream->profile, fd); |
| 772 | |
| 773 | dprintf(fd, "Input Proxy:\n"); |
| 774 | proxy_dump(&in_stream->proxy, fd); |
| 775 | } |
| 776 | |
| 777 | return 0; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | static int in_set_parameters(struct audio_stream *stream, const char *kvpairs) |
| 781 | { |
Paul McLean | 65ec72b | 2015-02-18 09:13:24 -0800 | [diff] [blame] | 782 | ALOGV("in_set_parameters() keys:%s", kvpairs); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 783 | |
| 784 | struct stream_in *in = (struct stream_in *)stream; |
| 785 | |
| 786 | char value[32]; |
| 787 | int param_val; |
| 788 | int routing = 0; |
| 789 | int ret_value = 0; |
Eric Laurent | 05333d4 | 2014-08-04 20:29:17 -0700 | [diff] [blame] | 790 | int card = -1; |
| 791 | int device = -1; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 792 | |
Paul McLean | 65ec72b | 2015-02-18 09:13:24 -0800 | [diff] [blame] | 793 | if (!parse_card_device_params(kvpairs, &card, &device)) { |
| 794 | // nothing to do |
| 795 | return ret_value; |
| 796 | } |
| 797 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 798 | stream_lock(&in->lock); |
| 799 | device_lock(in->adev); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 800 | |
Paul McLean | 2c6196f | 2014-08-20 16:50:25 -0700 | [diff] [blame] | 801 | 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] | 802 | /* cannot read pcm device info if playback is active, or more than one open stream */ |
| 803 | if (!in->standby || in->adev->inputs_open > 1) |
Eric Laurent | 05333d4 | 2014-08-04 20:29:17 -0700 | [diff] [blame] | 804 | ret_value = -ENOSYS; |
| 805 | else { |
| 806 | int saved_card = in->profile->card; |
| 807 | int saved_device = in->profile->device; |
Andy Hung | 4e6a1c0 | 2017-10-27 20:31:46 -0700 | [diff] [blame] | 808 | in->adev->in_profile.card = card; |
| 809 | in->adev->in_profile.device = device; |
| 810 | ret_value = profile_read_device_info(&in->adev->in_profile) ? 0 : -EINVAL; |
Eric Laurent | 05333d4 | 2014-08-04 20:29:17 -0700 | [diff] [blame] | 811 | if (ret_value != 0) { |
Andy Hung | 4e6a1c0 | 2017-10-27 20:31:46 -0700 | [diff] [blame] | 812 | in->adev->in_profile.card = saved_card; |
| 813 | in->adev->in_profile.device = saved_device; |
Eric Laurent | 05333d4 | 2014-08-04 20:29:17 -0700 | [diff] [blame] | 814 | } |
| 815 | } |
Paul McLean | 2c6196f | 2014-08-20 16:50:25 -0700 | [diff] [blame] | 816 | } |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 817 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 818 | device_unlock(in->adev); |
| 819 | stream_unlock(&in->lock); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 820 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 821 | return ret_value; |
| 822 | } |
| 823 | |
| 824 | static char * in_get_parameters(const struct audio_stream *stream, const char *keys) |
| 825 | { |
| 826 | struct stream_in *in = (struct stream_in *)stream; |
| 827 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 828 | stream_lock(&in->lock); |
| 829 | device_lock(in->adev); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 830 | |
| 831 | char * params_str = device_get_parameters(in->profile, keys); |
| 832 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 833 | device_unlock(in->adev); |
| 834 | stream_unlock(&in->lock); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 835 | |
| 836 | return params_str; |
| 837 | } |
| 838 | |
| 839 | static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 840 | { |
| 841 | return 0; |
| 842 | } |
| 843 | |
| 844 | static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 845 | { |
| 846 | return 0; |
| 847 | } |
| 848 | |
| 849 | static int in_set_gain(struct audio_stream_in *stream, float gain) |
| 850 | { |
| 851 | return 0; |
| 852 | } |
| 853 | |
| 854 | /* must be called with hw device and output stream mutexes locked */ |
| 855 | static int start_input_stream(struct stream_in *in) |
| 856 | { |
Paul McLean | 1d585cc | 2016-05-24 11:28:10 -0600 | [diff] [blame] | 857 | 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] | 858 | |
| 859 | return proxy_open(&in->proxy); |
| 860 | } |
| 861 | |
| 862 | /* TODO mutex stuff here (see out_write) */ |
| 863 | static ssize_t in_read(struct audio_stream_in *stream, void* buffer, size_t bytes) |
| 864 | { |
| 865 | size_t num_read_buff_bytes = 0; |
| 866 | void * read_buff = buffer; |
| 867 | void * out_buff = buffer; |
Pavan Chikkala | 83b47a6 | 2015-01-09 12:21:13 -0800 | [diff] [blame] | 868 | int ret = 0; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 869 | |
| 870 | struct stream_in * in = (struct stream_in *)stream; |
| 871 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 872 | stream_lock(&in->lock); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 873 | if (in->standby) { |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 874 | device_lock(in->adev); |
Eric Laurent | 7031809 | 2015-06-19 17:49:17 -0700 | [diff] [blame] | 875 | ret = start_input_stream(in); |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 876 | device_unlock(in->adev); |
Eric Laurent | 7031809 | 2015-06-19 17:49:17 -0700 | [diff] [blame] | 877 | if (ret != 0) { |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 878 | goto err; |
| 879 | } |
| 880 | in->standby = false; |
| 881 | } |
| 882 | |
Andy Hung | 4e6a1c0 | 2017-10-27 20:31:46 -0700 | [diff] [blame] | 883 | const alsa_device_profile *profile = in->profile; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 884 | |
| 885 | /* |
| 886 | * OK, we need to figure out how much data to read to be able to output the requested |
| 887 | * number of bytes in the HAL format (16-bit, stereo). |
| 888 | */ |
| 889 | num_read_buff_bytes = bytes; |
Andy Hung | 780f1f8 | 2015-04-22 22:14:39 -0700 | [diff] [blame] | 890 | int num_device_channels = proxy_get_channel_count(&in->proxy); /* what we told Alsa */ |
| 891 | int num_req_channels = in->hal_channel_count; /* what we told AudioFlinger */ |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 892 | |
| 893 | if (num_device_channels != num_req_channels) { |
| 894 | num_read_buff_bytes = (num_device_channels * num_read_buff_bytes) / num_req_channels; |
| 895 | } |
| 896 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 897 | /* Setup/Realloc the conversion buffer (if necessary). */ |
| 898 | if (num_read_buff_bytes != bytes) { |
| 899 | if (num_read_buff_bytes > in->conversion_buffer_size) { |
| 900 | /*TODO Remove this when AudioPolicyManger/AudioFlinger support arbitrary formats |
| 901 | (and do these conversions themselves) */ |
| 902 | in->conversion_buffer_size = num_read_buff_bytes; |
| 903 | in->conversion_buffer = realloc(in->conversion_buffer, in->conversion_buffer_size); |
| 904 | } |
| 905 | read_buff = in->conversion_buffer; |
| 906 | } |
| 907 | |
Pavan Chikkala | 83b47a6 | 2015-01-09 12:21:13 -0800 | [diff] [blame] | 908 | ret = proxy_read(&in->proxy, read_buff, num_read_buff_bytes); |
| 909 | if (ret == 0) { |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 910 | if (num_device_channels != num_req_channels) { |
| 911 | // ALOGV("chans dev:%d req:%d", num_device_channels, num_req_channels); |
| 912 | |
| 913 | out_buff = buffer; |
| 914 | /* Num Channels conversion */ |
| 915 | if (num_device_channels != num_req_channels) { |
| 916 | audio_format_t audio_format = in_get_format(&(in->stream.common)); |
| 917 | unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format); |
| 918 | |
| 919 | num_read_buff_bytes = |
| 920 | adjust_channels(read_buff, num_device_channels, |
| 921 | out_buff, num_req_channels, |
| 922 | sample_size_in_bytes, num_read_buff_bytes); |
| 923 | } |
| 924 | } |
Eric Laurent | 253def9 | 2014-09-14 12:18:18 -0700 | [diff] [blame] | 925 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 926 | /* no need to acquire in->adev->lock to read mic_muted here as we don't change its state */ |
| 927 | if (num_read_buff_bytes > 0 && in->adev->mic_muted) |
Eric Laurent | 253def9 | 2014-09-14 12:18:18 -0700 | [diff] [blame] | 928 | memset(buffer, 0, num_read_buff_bytes); |
Viswanath L | 8c7e111 | 2014-10-31 13:07:39 +0530 | [diff] [blame] | 929 | } else { |
Eric Laurent | e649942 | 2015-01-09 17:03:27 -0800 | [diff] [blame] | 930 | 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] | 931 | } |
| 932 | |
| 933 | err: |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 934 | stream_unlock(&in->lock); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 935 | return num_read_buff_bytes; |
| 936 | } |
| 937 | |
| 938 | static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream) |
| 939 | { |
| 940 | return 0; |
| 941 | } |
| 942 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 943 | static int adev_open_input_stream(struct audio_hw_device *hw_dev, |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 944 | audio_io_handle_t handle, |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 945 | audio_devices_t devicesSpec __unused, |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 946 | struct audio_config *config, |
| 947 | struct audio_stream_in **stream_in, |
Eric Laurent | f5e2469 | 2014-07-27 16:14:57 -0700 | [diff] [blame] | 948 | audio_input_flags_t flags __unused, |
Eric Laurent | 981f774 | 2017-05-03 12:44:26 -0700 | [diff] [blame] | 949 | const char *address, |
Eric Laurent | f5e2469 | 2014-07-27 16:14:57 -0700 | [diff] [blame] | 950 | audio_source_t source __unused) |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 951 | { |
Paul McLean | 1d585cc | 2016-05-24 11:28:10 -0600 | [diff] [blame] | 952 | ALOGV("adev_open_input_stream() rate:%" PRIu32 ", chanMask:0x%" PRIX32 ", fmt:%" PRIu8, |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 953 | config->sample_rate, config->channel_mask, config->format); |
| 954 | |
Andy Hung | b10ce6d | 2017-10-27 19:37:58 -0700 | [diff] [blame] | 955 | /* Pull out the card/device pair */ |
| 956 | int32_t card, device; |
| 957 | if (!parse_card_device_params(address, &card, &device)) { |
| 958 | ALOGW("%s fail - invalid address %s", __func__, address); |
| 959 | *stream_in = NULL; |
| 960 | return -EINVAL; |
| 961 | } |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 962 | |
Andy Hung | b10ce6d | 2017-10-27 19:37:58 -0700 | [diff] [blame] | 963 | 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] | 964 | if (in == NULL) { |
Andy Hung | b10ce6d | 2017-10-27 19:37:58 -0700 | [diff] [blame] | 965 | *stream_in = NULL; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 966 | return -ENOMEM; |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 967 | } |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 968 | |
| 969 | /* setup function pointers */ |
| 970 | in->stream.common.get_sample_rate = in_get_sample_rate; |
| 971 | in->stream.common.set_sample_rate = in_set_sample_rate; |
| 972 | in->stream.common.get_buffer_size = in_get_buffer_size; |
| 973 | in->stream.common.get_channels = in_get_channels; |
| 974 | in->stream.common.get_format = in_get_format; |
| 975 | in->stream.common.set_format = in_set_format; |
| 976 | in->stream.common.standby = in_standby; |
| 977 | in->stream.common.dump = in_dump; |
| 978 | in->stream.common.set_parameters = in_set_parameters; |
| 979 | in->stream.common.get_parameters = in_get_parameters; |
| 980 | in->stream.common.add_audio_effect = in_add_audio_effect; |
| 981 | in->stream.common.remove_audio_effect = in_remove_audio_effect; |
| 982 | |
| 983 | in->stream.set_gain = in_set_gain; |
| 984 | in->stream.read = in_read; |
| 985 | in->stream.get_input_frames_lost = in_get_input_frames_lost; |
| 986 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 987 | stream_lock_init(&in->lock); |
Eric Laurent | 7031809 | 2015-06-19 17:49:17 -0700 | [diff] [blame] | 988 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 989 | in->adev = (struct audio_device *)hw_dev; |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 990 | device_lock(in->adev); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 991 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 992 | in->profile = &in->adev->in_profile; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 993 | |
| 994 | struct pcm_config proxy_config; |
Paul McLean | 2c6196f | 2014-08-20 16:50:25 -0700 | [diff] [blame] | 995 | memset(&proxy_config, 0, sizeof(proxy_config)); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 996 | |
Andy Hung | b10ce6d | 2017-10-27 19:37:58 -0700 | [diff] [blame] | 997 | int ret = 0; |
| 998 | /* Check if an input stream is already open */ |
| 999 | if (in->adev->inputs_open > 0) { |
| 1000 | if (!profile_is_cached_for(in->profile, card, device)) { |
| 1001 | ALOGW("%s fail - address card:%d device:%d doesn't match existing profile", |
| 1002 | __func__, card, device); |
| 1003 | ret = -EINVAL; |
| 1004 | } |
| 1005 | } else { |
| 1006 | /* Read input profile only if necessary */ |
Andy Hung | 4e6a1c0 | 2017-10-27 20:31:46 -0700 | [diff] [blame] | 1007 | in->adev->in_profile.card = card; |
| 1008 | in->adev->in_profile.device = device; |
| 1009 | if (!profile_read_device_info(&in->adev->in_profile)) { |
Andy Hung | b10ce6d | 2017-10-27 19:37:58 -0700 | [diff] [blame] | 1010 | ALOGW("%s fail - cannot read profile", __func__); |
| 1011 | ret = -EINVAL; |
| 1012 | } |
| 1013 | } |
| 1014 | if (ret != 0) { |
| 1015 | device_unlock(in->adev); |
| 1016 | free(in); |
| 1017 | *stream_in = NULL; |
| 1018 | return ret; |
| 1019 | } |
Paul McLean | 0f1753e | 2014-12-02 12:36:45 -0700 | [diff] [blame] | 1020 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1021 | /* Rate */ |
| 1022 | if (config->sample_rate == 0) { |
Paul McLean | 9a1c305 | 2016-05-25 13:30:54 -0600 | [diff] [blame] | 1023 | config->sample_rate = profile_get_default_sample_rate(in->profile); |
| 1024 | } |
| 1025 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 1026 | if (in->adev->device_sample_rate != 0 && /* we are playing, so lock the rate */ |
| 1027 | in->adev->device_sample_rate >= RATELOCK_THRESHOLD) {/* but only for high sample rates */ |
| 1028 | ret = config->sample_rate != in->adev->device_sample_rate ? -EINVAL : 0; |
| 1029 | proxy_config.rate = config->sample_rate = in->adev->device_sample_rate; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1030 | } else if (profile_is_sample_rate_valid(in->profile, config->sample_rate)) { |
Eric Laurent | 981f774 | 2017-05-03 12:44:26 -0700 | [diff] [blame] | 1031 | proxy_config.rate = config->sample_rate; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1032 | } else { |
| 1033 | proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(in->profile); |
| 1034 | ret = -EINVAL; |
| 1035 | } |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 1036 | device_unlock(in->adev); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1037 | |
| 1038 | /* Format */ |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1039 | if (config->format == AUDIO_FORMAT_DEFAULT) { |
Andy Hung | 780f1f8 | 2015-04-22 22:14:39 -0700 | [diff] [blame] | 1040 | proxy_config.format = profile_get_default_format(in->profile); |
| 1041 | config->format = audio_format_from_pcm_format(proxy_config.format); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1042 | } else { |
Andy Hung | 780f1f8 | 2015-04-22 22:14:39 -0700 | [diff] [blame] | 1043 | enum pcm_format fmt = pcm_format_from_audio_format(config->format); |
| 1044 | if (profile_is_format_valid(in->profile, fmt)) { |
| 1045 | proxy_config.format = fmt; |
| 1046 | } else { |
| 1047 | proxy_config.format = profile_get_default_format(in->profile); |
| 1048 | config->format = audio_format_from_pcm_format(proxy_config.format); |
| 1049 | ret = -EINVAL; |
| 1050 | } |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1051 | } |
| 1052 | |
Paul McLean | 2cfd81b | 2014-09-15 12:32:23 -0700 | [diff] [blame] | 1053 | /* Channels */ |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 1054 | bool calc_mask = false; |
| 1055 | if (config->channel_mask == AUDIO_CHANNEL_NONE) { |
| 1056 | /* query case */ |
| 1057 | in->hal_channel_count = profile_get_default_channel_count(in->profile); |
| 1058 | calc_mask = true; |
Andy Hung | 780f1f8 | 2015-04-22 22:14:39 -0700 | [diff] [blame] | 1059 | } else { |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 1060 | /* explicit case */ |
Andy Hung | 780f1f8 | 2015-04-22 22:14:39 -0700 | [diff] [blame] | 1061 | in->hal_channel_count = audio_channel_count_from_in_mask(config->channel_mask); |
| 1062 | } |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1063 | |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 1064 | /* The Framework is currently limited to no more than this number of channels */ |
| 1065 | if (in->hal_channel_count > FCC_8) { |
| 1066 | in->hal_channel_count = FCC_8; |
| 1067 | calc_mask = true; |
| 1068 | } |
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 (calc_mask) { |
| 1071 | /* need to calculate the mask from channel count either because this is the query case |
| 1072 | * or the specified mask isn't valid for this device, or is more then the FW can handle */ |
| 1073 | in->hal_channel_mask = in->hal_channel_count <= FCC_2 |
| 1074 | /* position mask for mono & stereo */ |
| 1075 | ? audio_channel_in_mask_from_count(in->hal_channel_count) |
| 1076 | /* otherwise indexed */ |
| 1077 | : audio_channel_mask_for_index_assignment_from_count(in->hal_channel_count); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1078 | |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 1079 | // if we change the mask... |
| 1080 | if (in->hal_channel_mask != config->channel_mask && |
| 1081 | config->channel_mask != AUDIO_CHANNEL_NONE) { |
| 1082 | config->channel_mask = in->hal_channel_mask; |
| 1083 | ret = -EINVAL; |
| 1084 | } |
| 1085 | } else { |
| 1086 | in->hal_channel_mask = config->channel_mask; |
| 1087 | } |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 1088 | |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 1089 | if (ret == 0) { |
| 1090 | // Validate the "logical" channel count against support in the "actual" profile. |
| 1091 | // if they differ, choose the "actual" number of channels *closest* to the "logical". |
| 1092 | // and store THAT in proxy_config.channels |
| 1093 | proxy_config.channels = |
| 1094 | profile_get_closest_channel_count(in->profile, in->hal_channel_count); |
Eric Laurent | 981f774 | 2017-05-03 12:44:26 -0700 | [diff] [blame] | 1095 | ret = proxy_prepare(&in->proxy, in->profile, &proxy_config); |
| 1096 | if (ret == 0) { |
| 1097 | in->standby = true; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1098 | |
Eric Laurent | 981f774 | 2017-05-03 12:44:26 -0700 | [diff] [blame] | 1099 | in->conversion_buffer = NULL; |
| 1100 | in->conversion_buffer_size = 0; |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 1101 | |
Eric Laurent | 981f774 | 2017-05-03 12:44:26 -0700 | [diff] [blame] | 1102 | *stream_in = &in->stream; |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 1103 | |
Eric Laurent | 981f774 | 2017-05-03 12:44:26 -0700 | [diff] [blame] | 1104 | /* Save this for adev_dump() */ |
| 1105 | adev_add_stream_to_list(in->adev, &in->adev->input_stream_list, &in->list_node); |
| 1106 | } else { |
| 1107 | ALOGW("proxy_prepare error %d", ret); |
| 1108 | unsigned channel_count = proxy_get_channel_count(&in->proxy); |
| 1109 | config->channel_mask = channel_count <= FCC_2 |
| 1110 | ? audio_channel_in_mask_from_count(channel_count) |
| 1111 | : audio_channel_mask_for_index_assignment_from_count(channel_count); |
| 1112 | config->format = audio_format_from_pcm_format(proxy_get_format(&in->proxy)); |
| 1113 | config->sample_rate = proxy_get_sample_rate(&in->proxy); |
| 1114 | } |
| 1115 | } |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 1116 | |
Eric Laurent | 981f774 | 2017-05-03 12:44:26 -0700 | [diff] [blame] | 1117 | if (ret != 0) { |
Paul McLean | 64345f8 | 2016-06-06 13:26:05 -0600 | [diff] [blame] | 1118 | // Deallocate this stream on error, because AudioFlinger won't call |
| 1119 | // adev_close_input_stream() in this case. |
| 1120 | *stream_in = NULL; |
| 1121 | free(in); |
| 1122 | } |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1123 | |
Andy Hung | b10ce6d | 2017-10-27 19:37:58 -0700 | [diff] [blame] | 1124 | device_lock(in->adev); |
| 1125 | ++in->adev->inputs_open; |
| 1126 | device_unlock(in->adev); |
| 1127 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1128 | return ret; |
| 1129 | } |
| 1130 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 1131 | static void adev_close_input_stream(struct audio_hw_device *hw_dev, |
| 1132 | struct audio_stream_in *stream) |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1133 | { |
| 1134 | struct stream_in *in = (struct stream_in *)stream; |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 1135 | ALOGV("adev_close_input_stream(c:%d d:%d)", in->profile->card, in->profile->device); |
| 1136 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 1137 | adev_remove_stream_from_list(in->adev, &in->list_node); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1138 | |
Andy Hung | b10ce6d | 2017-10-27 19:37:58 -0700 | [diff] [blame] | 1139 | device_lock(in->adev); |
| 1140 | --in->adev->inputs_open; |
| 1141 | LOG_ALWAYS_FATAL_IF(in->adev->inputs_open < 0, |
| 1142 | "invalid inputs_open: %d", in->adev->inputs_open); |
| 1143 | device_unlock(in->adev); |
| 1144 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1145 | /* Close the pcm device */ |
| 1146 | in_standby(&stream->common); |
| 1147 | |
| 1148 | free(in->conversion_buffer); |
| 1149 | |
| 1150 | free(stream); |
| 1151 | } |
| 1152 | |
| 1153 | /* |
| 1154 | * ADEV Functions |
| 1155 | */ |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 1156 | 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] | 1157 | { |
| 1158 | return 0; |
| 1159 | } |
| 1160 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 1161 | 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] | 1162 | { |
| 1163 | return strdup(""); |
| 1164 | } |
| 1165 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 1166 | static int adev_init_check(const struct audio_hw_device *hw_dev) |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1167 | { |
| 1168 | return 0; |
| 1169 | } |
| 1170 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 1171 | 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] | 1172 | { |
| 1173 | return -ENOSYS; |
| 1174 | } |
| 1175 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 1176 | 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] | 1177 | { |
| 1178 | return -ENOSYS; |
| 1179 | } |
| 1180 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 1181 | 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] | 1182 | { |
| 1183 | return 0; |
| 1184 | } |
| 1185 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 1186 | 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] | 1187 | { |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 1188 | struct audio_device * adev = (struct audio_device *)hw_dev; |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 1189 | device_lock(adev); |
Eric Laurent | 253def9 | 2014-09-14 12:18:18 -0700 | [diff] [blame] | 1190 | adev->mic_muted = state; |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 1191 | device_unlock(adev); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1192 | return -ENOSYS; |
| 1193 | } |
| 1194 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 1195 | 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] | 1196 | { |
| 1197 | return -ENOSYS; |
| 1198 | } |
| 1199 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame] | 1200 | static int adev_dump(const struct audio_hw_device *device, int fd) |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1201 | { |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 1202 | dprintf(fd, "\nUSB audio module:\n"); |
| 1203 | |
| 1204 | struct audio_device* adev = (struct audio_device*)device; |
| 1205 | const int kNumRetries = 3; |
| 1206 | const int kSleepTimeMS = 500; |
| 1207 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 1208 | // use device_try_lock() in case we dumpsys during a deadlock |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 1209 | int retry = kNumRetries; |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 1210 | while (retry > 0 && device_try_lock(adev) != 0) { |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 1211 | sleep(kSleepTimeMS); |
| 1212 | retry--; |
| 1213 | } |
| 1214 | |
| 1215 | if (retry > 0) { |
| 1216 | if (list_empty(&adev->output_stream_list)) { |
| 1217 | dprintf(fd, " No output streams.\n"); |
| 1218 | } else { |
| 1219 | struct listnode* node; |
| 1220 | list_for_each(node, &adev->output_stream_list) { |
| 1221 | struct audio_stream* stream = |
| 1222 | (struct audio_stream *)node_to_item(node, struct stream_out, list_node); |
| 1223 | out_dump(stream, fd); |
| 1224 | } |
| 1225 | } |
| 1226 | |
| 1227 | if (list_empty(&adev->input_stream_list)) { |
| 1228 | dprintf(fd, "\n No input streams.\n"); |
| 1229 | } else { |
| 1230 | struct listnode* node; |
| 1231 | list_for_each(node, &adev->input_stream_list) { |
| 1232 | struct audio_stream* stream = |
| 1233 | (struct audio_stream *)node_to_item(node, struct stream_in, list_node); |
| 1234 | in_dump(stream, fd); |
| 1235 | } |
| 1236 | } |
| 1237 | |
Paul McLean | 994ac07 | 2016-06-02 15:33:24 -0600 | [diff] [blame] | 1238 | device_unlock(adev); |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 1239 | } else { |
| 1240 | // Couldn't lock |
| 1241 | dprintf(fd, " Could not obtain device lock.\n"); |
| 1242 | } |
| 1243 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1244 | return 0; |
| 1245 | } |
| 1246 | |
| 1247 | static int adev_close(hw_device_t *device) |
| 1248 | { |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1249 | struct audio_device *adev = (struct audio_device *)device; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1250 | free(device); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1251 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1252 | return 0; |
| 1253 | } |
| 1254 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1255 | 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] | 1256 | { |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1257 | if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) |
| 1258 | return -EINVAL; |
| 1259 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1260 | struct audio_device *adev = calloc(1, sizeof(struct audio_device)); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1261 | if (!adev) |
| 1262 | return -ENOMEM; |
| 1263 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1264 | profile_init(&adev->out_profile, PCM_OUT); |
| 1265 | profile_init(&adev->in_profile, PCM_IN); |
| 1266 | |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 1267 | list_init(&adev->output_stream_list); |
| 1268 | list_init(&adev->input_stream_list); |
| 1269 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1270 | adev->hw_device.common.tag = HARDWARE_DEVICE_TAG; |
Eric Laurent | 85e08e2 | 2012-08-28 14:30:35 -0700 | [diff] [blame] | 1271 | adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1272 | adev->hw_device.common.module = (struct hw_module_t *)module; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1273 | adev->hw_device.common.close = adev_close; |
| 1274 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1275 | adev->hw_device.init_check = adev_init_check; |
| 1276 | adev->hw_device.set_voice_volume = adev_set_voice_volume; |
| 1277 | adev->hw_device.set_master_volume = adev_set_master_volume; |
| 1278 | adev->hw_device.set_mode = adev_set_mode; |
| 1279 | adev->hw_device.set_mic_mute = adev_set_mic_mute; |
| 1280 | adev->hw_device.get_mic_mute = adev_get_mic_mute; |
| 1281 | adev->hw_device.set_parameters = adev_set_parameters; |
| 1282 | adev->hw_device.get_parameters = adev_get_parameters; |
| 1283 | adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size; |
| 1284 | adev->hw_device.open_output_stream = adev_open_output_stream; |
| 1285 | adev->hw_device.close_output_stream = adev_close_output_stream; |
| 1286 | adev->hw_device.open_input_stream = adev_open_input_stream; |
| 1287 | adev->hw_device.close_input_stream = adev_close_input_stream; |
| 1288 | adev->hw_device.dump = adev_dump; |
| 1289 | |
| 1290 | *device = &adev->hw_device.common; |
| 1291 | |
| 1292 | return 0; |
| 1293 | } |
| 1294 | |
| 1295 | static struct hw_module_methods_t hal_module_methods = { |
| 1296 | .open = adev_open, |
| 1297 | }; |
| 1298 | |
| 1299 | struct audio_module HAL_MODULE_INFO_SYM = { |
| 1300 | .common = { |
| 1301 | .tag = HARDWARE_MODULE_TAG, |
Mike Lockwood | 46a9809 | 2012-04-24 16:41:18 -0700 | [diff] [blame] | 1302 | .module_api_version = AUDIO_MODULE_API_VERSION_0_1, |
| 1303 | .hal_api_version = HARDWARE_HAL_API_VERSION, |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1304 | .id = AUDIO_HARDWARE_MODULE_ID, |
| 1305 | .name = "USB audio HW HAL", |
| 1306 | .author = "The Android Open Source Project", |
| 1307 | .methods = &hal_module_methods, |
| 1308 | }, |
| 1309 | }; |