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