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