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