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 | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 91 | struct audio_device *adev; /* hardware information - only using this for the lock */ |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 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 | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 118 | struct audio_device *adev; /* 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) { |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 315 | pthread_mutex_lock(&out->adev->lock); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 316 | proxy_close(&out->proxy); |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 317 | pthread_mutex_unlock(&out->adev->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 | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 357 | pthread_mutex_lock(&out->adev->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 | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 376 | pthread_mutex_unlock(&out->adev->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 | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 386 | pthread_mutex_lock(&out->adev->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); |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 391 | pthread_mutex_unlock(&out->adev->lock); |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 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) { |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 422 | pthread_mutex_lock(&out->adev->lock); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 423 | ret = start_output_stream(out); |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 424 | pthread_mutex_unlock(&out->adev->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 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 506 | static int adev_open_output_stream(struct audio_hw_device *hw_dev, |
Mike Lockwood | 46a9809 | 2012-04-24 16:41:18 -0700 | [diff] [blame] | 507 | audio_io_handle_t handle, |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 508 | audio_devices_t devicesSpec __unused, |
Mike Lockwood | 46a9809 | 2012-04-24 16:41:18 -0700 | [diff] [blame] | 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 | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 514 | ALOGV("adev_open_output_stream() handle:0x%X, devicesSpec:0x%X, flags:0x%X, addr:%s", |
| 515 | handle, devicesSpec, 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 stream_out *out; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 518 | |
| 519 | out = (struct stream_out *)calloc(1, sizeof(struct stream_out)); |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 520 | if (out == NULL) { |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 521 | return -ENOMEM; |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 522 | } |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 523 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 524 | /* setup function pointers */ |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 525 | out->stream.common.get_sample_rate = out_get_sample_rate; |
| 526 | out->stream.common.set_sample_rate = out_set_sample_rate; |
| 527 | out->stream.common.get_buffer_size = out_get_buffer_size; |
| 528 | out->stream.common.get_channels = out_get_channels; |
| 529 | out->stream.common.get_format = out_get_format; |
| 530 | out->stream.common.set_format = out_set_format; |
| 531 | out->stream.common.standby = out_standby; |
| 532 | out->stream.common.dump = out_dump; |
| 533 | out->stream.common.set_parameters = out_set_parameters; |
| 534 | out->stream.common.get_parameters = out_get_parameters; |
| 535 | out->stream.common.add_audio_effect = out_add_audio_effect; |
| 536 | out->stream.common.remove_audio_effect = out_remove_audio_effect; |
| 537 | out->stream.get_latency = out_get_latency; |
| 538 | out->stream.set_volume = out_set_volume; |
| 539 | out->stream.write = out_write; |
| 540 | out->stream.get_render_position = out_get_render_position; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 541 | out->stream.get_presentation_position = out_get_presentation_position; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 542 | out->stream.get_next_write_timestamp = out_get_next_write_timestamp; |
| 543 | |
Eric Laurent | 7031809 | 2015-06-19 17:49:17 -0700 | [diff] [blame] | 544 | pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL); |
| 545 | pthread_mutex_init(&out->pre_lock, (const pthread_mutexattr_t *) NULL); |
| 546 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 547 | out->adev = (struct audio_device *)hw_dev; |
| 548 | pthread_mutex_lock(&out->adev->lock); |
| 549 | out->profile = &out->adev->out_profile; |
Paul McLean | f62d75e | 2014-07-11 15:14:19 -0700 | [diff] [blame] | 550 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 551 | // build this to hand to the alsa_device_proxy |
| 552 | struct pcm_config proxy_config; |
Paul McLean | 2c6196f | 2014-08-20 16:50:25 -0700 | [diff] [blame] | 553 | memset(&proxy_config, 0, sizeof(proxy_config)); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 554 | |
Paul McLean | 0f1753e | 2014-12-02 12:36:45 -0700 | [diff] [blame] | 555 | /* Pull out the card/device pair */ |
| 556 | parse_card_device_params(address, &(out->profile->card), &(out->profile->device)); |
| 557 | |
| 558 | profile_read_device_info(out->profile); |
| 559 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 560 | int ret = 0; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 561 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 562 | /* Rate */ |
| 563 | if (config->sample_rate == 0) { |
| 564 | proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(out->profile); |
| 565 | } else if (profile_is_sample_rate_valid(out->profile, config->sample_rate)) { |
| 566 | proxy_config.rate = config->sample_rate; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 567 | } else { |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 568 | proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(out->profile); |
| 569 | ret = -EINVAL; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 570 | } |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 571 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 572 | out->adev->device_sample_rate = config->sample_rate; |
| 573 | pthread_mutex_unlock(&out->adev->lock); |
Paul McLean | 1d585cc | 2016-05-24 11:28:10 -0600 | [diff] [blame] | 574 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 575 | /* Format */ |
| 576 | if (config->format == AUDIO_FORMAT_DEFAULT) { |
| 577 | proxy_config.format = profile_get_default_format(out->profile); |
| 578 | config->format = audio_format_from_pcm_format(proxy_config.format); |
| 579 | } else { |
| 580 | enum pcm_format fmt = pcm_format_from_audio_format(config->format); |
| 581 | if (profile_is_format_valid(out->profile, fmt)) { |
| 582 | proxy_config.format = fmt; |
| 583 | } else { |
| 584 | proxy_config.format = profile_get_default_format(out->profile); |
| 585 | config->format = audio_format_from_pcm_format(proxy_config.format); |
| 586 | ret = -EINVAL; |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | /* Channels */ |
Andy Hung | 182ddc7 | 2015-05-05 23:37:30 -0700 | [diff] [blame] | 591 | unsigned proposed_channel_count = 0; |
Andy Hung | 03576be | 2014-07-21 21:16:45 -0700 | [diff] [blame] | 592 | if (k_force_channels) { |
| 593 | proposed_channel_count = k_force_channels; |
Andy Hung | 182ddc7 | 2015-05-05 23:37:30 -0700 | [diff] [blame] | 594 | } else if (config->channel_mask == AUDIO_CHANNEL_NONE) { |
| 595 | proposed_channel_count = profile_get_default_channel_count(out->profile); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 596 | } |
Paul McLean | 698dbd7 | 2015-11-03 12:24:30 -0800 | [diff] [blame] | 597 | |
Andy Hung | 182ddc7 | 2015-05-05 23:37:30 -0700 | [diff] [blame] | 598 | if (proposed_channel_count != 0) { |
Andy Hung | 5e58a30 | 2015-06-10 15:17:00 -0700 | [diff] [blame] | 599 | if (proposed_channel_count <= FCC_2) { |
| 600 | // use channel position mask for mono and stereo |
| 601 | config->channel_mask = audio_channel_out_mask_from_count(proposed_channel_count); |
| 602 | } else { |
| 603 | // use channel index mask for multichannel |
Andy Hung | 182ddc7 | 2015-05-05 23:37:30 -0700 | [diff] [blame] | 604 | config->channel_mask = |
| 605 | audio_channel_mask_for_index_assignment_from_count(proposed_channel_count); |
Andy Hung | 5e58a30 | 2015-06-10 15:17:00 -0700 | [diff] [blame] | 606 | } |
Andy Hung | 182ddc7 | 2015-05-05 23:37:30 -0700 | [diff] [blame] | 607 | } else { |
Paul McLean | 698dbd7 | 2015-11-03 12:24:30 -0800 | [diff] [blame] | 608 | proposed_channel_count = audio_channel_count_from_out_mask(config->channel_mask); |
Andy Hung | 182ddc7 | 2015-05-05 23:37:30 -0700 | [diff] [blame] | 609 | } |
Paul McLean | 698dbd7 | 2015-11-03 12:24:30 -0800 | [diff] [blame] | 610 | out->hal_channel_count = proposed_channel_count; |
| 611 | |
Andy Hung | 182ddc7 | 2015-05-05 23:37:30 -0700 | [diff] [blame] | 612 | /* we can expose any channel mask, and emulate internally based on channel count. */ |
| 613 | out->hal_channel_mask = config->channel_mask; |
| 614 | |
Andy Hung | 03576be | 2014-07-21 21:16:45 -0700 | [diff] [blame] | 615 | /* no validity checks are needed as proxy_prepare() forces channel_count to be valid. |
| 616 | * and we emulate any channel count discrepancies in out_write(). */ |
Paul McLean | 698dbd7 | 2015-11-03 12:24:30 -0800 | [diff] [blame] | 617 | proxy_config.channels = out->hal_channel_count; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 618 | proxy_prepare(&out->proxy, out->profile, &proxy_config); |
| 619 | |
| 620 | /* TODO The retry mechanism isn't implemented in AudioPolicyManager/AudioFlinger. */ |
| 621 | ret = 0; |
| 622 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 623 | out->conversion_buffer = NULL; |
| 624 | out->conversion_buffer_size = 0; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 625 | |
| 626 | out->standby = true; |
| 627 | |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 628 | /* Save the stream for adev_dump() */ |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 629 | 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] | 630 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 631 | *stream_out = &out->stream; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 632 | |
| 633 | return ret; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 634 | |
| 635 | err_open: |
| 636 | free(out); |
| 637 | *stream_out = NULL; |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 638 | return -ENOSYS; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 639 | } |
| 640 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 641 | static void adev_close_output_stream(struct audio_hw_device *hw_dev, |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 642 | struct audio_stream_out *stream) |
| 643 | { |
| 644 | struct stream_out *out = (struct stream_out *)stream; |
Paul McLean | 65ec72b | 2015-02-18 09:13:24 -0800 | [diff] [blame] | 645 | 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] | 646 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 647 | adev_remove_stream_from_list(out->adev, &out->list_node); |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 648 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 649 | /* Close the pcm device */ |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 650 | out_standby(&stream->common); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 651 | |
| 652 | free(out->conversion_buffer); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 653 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 654 | out->conversion_buffer = NULL; |
| 655 | out->conversion_buffer_size = 0; |
| 656 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 657 | pthread_mutex_lock(&out->adev->lock); |
| 658 | out->adev->device_sample_rate = 0; |
| 659 | pthread_mutex_unlock(&out->adev->lock); |
Paul McLean | 1d585cc | 2016-05-24 11:28:10 -0600 | [diff] [blame] | 660 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 661 | free(stream); |
| 662 | } |
| 663 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 664 | 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] | 665 | const struct audio_config *config) |
| 666 | { |
| 667 | /* TODO This needs to be calculated based on format/channels/rate */ |
| 668 | return 320; |
| 669 | } |
| 670 | |
| 671 | /* |
| 672 | * IN functions |
| 673 | */ |
| 674 | static uint32_t in_get_sample_rate(const struct audio_stream *stream) |
| 675 | { |
| 676 | uint32_t rate = proxy_get_sample_rate(&((const struct stream_in *)stream)->proxy); |
| 677 | ALOGV("in_get_sample_rate() = %d", rate); |
| 678 | return rate; |
| 679 | } |
| 680 | |
| 681 | static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate) |
| 682 | { |
| 683 | ALOGV("in_set_sample_rate(%d) - NOPE", rate); |
| 684 | return -ENOSYS; |
| 685 | } |
| 686 | |
| 687 | static size_t in_get_buffer_size(const struct audio_stream *stream) |
| 688 | { |
| 689 | const struct stream_in * in = ((const struct stream_in*)stream); |
Paul McLean | 2cfd81b | 2014-09-15 12:32:23 -0700 | [diff] [blame] | 690 | 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] | 691 | } |
| 692 | |
| 693 | static uint32_t in_get_channels(const struct audio_stream *stream) |
| 694 | { |
Paul McLean | 2cfd81b | 2014-09-15 12:32:23 -0700 | [diff] [blame] | 695 | const struct stream_in *in = (const struct stream_in*)stream; |
Andy Hung | 780f1f8 | 2015-04-22 22:14:39 -0700 | [diff] [blame] | 696 | return in->hal_channel_mask; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | static audio_format_t in_get_format(const struct audio_stream *stream) |
| 700 | { |
Andy Hung | 780f1f8 | 2015-04-22 22:14:39 -0700 | [diff] [blame] | 701 | alsa_device_proxy *proxy = &((struct stream_in*)stream)->proxy; |
| 702 | audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy)); |
| 703 | return format; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | static int in_set_format(struct audio_stream *stream, audio_format_t format) |
| 707 | { |
| 708 | ALOGV("in_set_format(%d) - NOPE", format); |
| 709 | |
| 710 | return -ENOSYS; |
| 711 | } |
| 712 | |
| 713 | static int in_standby(struct audio_stream *stream) |
| 714 | { |
| 715 | struct stream_in *in = (struct stream_in *)stream; |
| 716 | |
Eric Laurent | 7031809 | 2015-06-19 17:49:17 -0700 | [diff] [blame] | 717 | lock_input_stream(in); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 718 | if (!in->standby) { |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 719 | pthread_mutex_lock(&in->adev->lock); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 720 | proxy_close(&in->proxy); |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 721 | pthread_mutex_unlock(&in->adev->lock); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 722 | in->standby = true; |
| 723 | } |
| 724 | |
| 725 | pthread_mutex_unlock(&in->lock); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 726 | |
| 727 | return 0; |
| 728 | } |
| 729 | |
| 730 | static int in_dump(const struct audio_stream *stream, int fd) |
| 731 | { |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 732 | const struct stream_in* in_stream = (const struct stream_in*)stream; |
| 733 | if (in_stream != NULL) { |
| 734 | dprintf(fd, "Input Profile:\n"); |
| 735 | profile_dump(in_stream->profile, fd); |
| 736 | |
| 737 | dprintf(fd, "Input Proxy:\n"); |
| 738 | proxy_dump(&in_stream->proxy, fd); |
| 739 | } |
| 740 | |
| 741 | return 0; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | static int in_set_parameters(struct audio_stream *stream, const char *kvpairs) |
| 745 | { |
Paul McLean | 65ec72b | 2015-02-18 09:13:24 -0800 | [diff] [blame] | 746 | ALOGV("in_set_parameters() keys:%s", kvpairs); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 747 | |
| 748 | struct stream_in *in = (struct stream_in *)stream; |
| 749 | |
| 750 | char value[32]; |
| 751 | int param_val; |
| 752 | int routing = 0; |
| 753 | int ret_value = 0; |
Eric Laurent | 05333d4 | 2014-08-04 20:29:17 -0700 | [diff] [blame] | 754 | int card = -1; |
| 755 | int device = -1; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 756 | |
Paul McLean | 65ec72b | 2015-02-18 09:13:24 -0800 | [diff] [blame] | 757 | if (!parse_card_device_params(kvpairs, &card, &device)) { |
| 758 | // nothing to do |
| 759 | return ret_value; |
| 760 | } |
| 761 | |
Eric Laurent | 7031809 | 2015-06-19 17:49:17 -0700 | [diff] [blame] | 762 | lock_input_stream(in); |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 763 | pthread_mutex_lock(&in->adev->lock); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 764 | |
Paul McLean | 2c6196f | 2014-08-20 16:50:25 -0700 | [diff] [blame] | 765 | 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] | 766 | /* cannot read pcm device info if playback is active */ |
| 767 | if (!in->standby) |
| 768 | ret_value = -ENOSYS; |
| 769 | else { |
| 770 | int saved_card = in->profile->card; |
| 771 | int saved_device = in->profile->device; |
| 772 | in->profile->card = card; |
| 773 | in->profile->device = device; |
| 774 | ret_value = profile_read_device_info(in->profile) ? 0 : -EINVAL; |
| 775 | if (ret_value != 0) { |
| 776 | in->profile->card = saved_card; |
| 777 | in->profile->device = saved_device; |
| 778 | } |
| 779 | } |
Paul McLean | 2c6196f | 2014-08-20 16:50:25 -0700 | [diff] [blame] | 780 | } |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 781 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 782 | pthread_mutex_unlock(&in->adev->lock); |
Eric Laurent | 7031809 | 2015-06-19 17:49:17 -0700 | [diff] [blame] | 783 | pthread_mutex_unlock(&in->lock); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 784 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 785 | return ret_value; |
| 786 | } |
| 787 | |
| 788 | static char * in_get_parameters(const struct audio_stream *stream, const char *keys) |
| 789 | { |
| 790 | struct stream_in *in = (struct stream_in *)stream; |
| 791 | |
Eric Laurent | 7031809 | 2015-06-19 17:49:17 -0700 | [diff] [blame] | 792 | lock_input_stream(in); |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 793 | pthread_mutex_lock(&in->adev->lock); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 794 | |
| 795 | char * params_str = device_get_parameters(in->profile, keys); |
| 796 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 797 | pthread_mutex_unlock(&in->adev->lock); |
Eric Laurent | 7031809 | 2015-06-19 17:49:17 -0700 | [diff] [blame] | 798 | pthread_mutex_unlock(&in->lock); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 799 | |
| 800 | return params_str; |
| 801 | } |
| 802 | |
| 803 | static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 804 | { |
| 805 | return 0; |
| 806 | } |
| 807 | |
| 808 | static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 809 | { |
| 810 | return 0; |
| 811 | } |
| 812 | |
| 813 | static int in_set_gain(struct audio_stream_in *stream, float gain) |
| 814 | { |
| 815 | return 0; |
| 816 | } |
| 817 | |
| 818 | /* must be called with hw device and output stream mutexes locked */ |
| 819 | static int start_input_stream(struct stream_in *in) |
| 820 | { |
Paul McLean | 1d585cc | 2016-05-24 11:28:10 -0600 | [diff] [blame] | 821 | 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] | 822 | |
| 823 | return proxy_open(&in->proxy); |
| 824 | } |
| 825 | |
| 826 | /* TODO mutex stuff here (see out_write) */ |
| 827 | static ssize_t in_read(struct audio_stream_in *stream, void* buffer, size_t bytes) |
| 828 | { |
| 829 | size_t num_read_buff_bytes = 0; |
| 830 | void * read_buff = buffer; |
| 831 | void * out_buff = buffer; |
Pavan Chikkala | 83b47a6 | 2015-01-09 12:21:13 -0800 | [diff] [blame] | 832 | int ret = 0; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 833 | |
| 834 | struct stream_in * in = (struct stream_in *)stream; |
| 835 | |
Eric Laurent | 7031809 | 2015-06-19 17:49:17 -0700 | [diff] [blame] | 836 | lock_input_stream(in); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 837 | if (in->standby) { |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 838 | pthread_mutex_lock(&in->adev->lock); |
Eric Laurent | 7031809 | 2015-06-19 17:49:17 -0700 | [diff] [blame] | 839 | ret = start_input_stream(in); |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 840 | pthread_mutex_unlock(&in->adev->lock); |
Eric Laurent | 7031809 | 2015-06-19 17:49:17 -0700 | [diff] [blame] | 841 | if (ret != 0) { |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 842 | goto err; |
| 843 | } |
| 844 | in->standby = false; |
| 845 | } |
| 846 | |
| 847 | alsa_device_profile * profile = in->profile; |
| 848 | |
| 849 | /* |
| 850 | * OK, we need to figure out how much data to read to be able to output the requested |
| 851 | * number of bytes in the HAL format (16-bit, stereo). |
| 852 | */ |
| 853 | num_read_buff_bytes = bytes; |
Andy Hung | 780f1f8 | 2015-04-22 22:14:39 -0700 | [diff] [blame] | 854 | int num_device_channels = proxy_get_channel_count(&in->proxy); /* what we told Alsa */ |
| 855 | int num_req_channels = in->hal_channel_count; /* what we told AudioFlinger */ |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 856 | |
| 857 | if (num_device_channels != num_req_channels) { |
| 858 | num_read_buff_bytes = (num_device_channels * num_read_buff_bytes) / num_req_channels; |
| 859 | } |
| 860 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 861 | /* Setup/Realloc the conversion buffer (if necessary). */ |
| 862 | if (num_read_buff_bytes != bytes) { |
| 863 | if (num_read_buff_bytes > in->conversion_buffer_size) { |
| 864 | /*TODO Remove this when AudioPolicyManger/AudioFlinger support arbitrary formats |
| 865 | (and do these conversions themselves) */ |
| 866 | in->conversion_buffer_size = num_read_buff_bytes; |
| 867 | in->conversion_buffer = realloc(in->conversion_buffer, in->conversion_buffer_size); |
| 868 | } |
| 869 | read_buff = in->conversion_buffer; |
| 870 | } |
| 871 | |
Pavan Chikkala | 83b47a6 | 2015-01-09 12:21:13 -0800 | [diff] [blame] | 872 | ret = proxy_read(&in->proxy, read_buff, num_read_buff_bytes); |
| 873 | if (ret == 0) { |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 874 | if (num_device_channels != num_req_channels) { |
| 875 | // ALOGV("chans dev:%d req:%d", num_device_channels, num_req_channels); |
| 876 | |
| 877 | out_buff = buffer; |
| 878 | /* Num Channels conversion */ |
| 879 | if (num_device_channels != num_req_channels) { |
| 880 | audio_format_t audio_format = in_get_format(&(in->stream.common)); |
| 881 | unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format); |
| 882 | |
| 883 | num_read_buff_bytes = |
| 884 | adjust_channels(read_buff, num_device_channels, |
| 885 | out_buff, num_req_channels, |
| 886 | sample_size_in_bytes, num_read_buff_bytes); |
| 887 | } |
| 888 | } |
Eric Laurent | 253def9 | 2014-09-14 12:18:18 -0700 | [diff] [blame] | 889 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 890 | /* no need to acquire in->adev->lock to read mic_muted here as we don't change its state */ |
| 891 | if (num_read_buff_bytes > 0 && in->adev->mic_muted) |
Eric Laurent | 253def9 | 2014-09-14 12:18:18 -0700 | [diff] [blame] | 892 | memset(buffer, 0, num_read_buff_bytes); |
Viswanath L | 8c7e111 | 2014-10-31 13:07:39 +0530 | [diff] [blame] | 893 | } else { |
Eric Laurent | e649942 | 2015-01-09 17:03:27 -0800 | [diff] [blame] | 894 | 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] | 895 | } |
| 896 | |
| 897 | err: |
| 898 | pthread_mutex_unlock(&in->lock); |
| 899 | |
| 900 | return num_read_buff_bytes; |
| 901 | } |
| 902 | |
| 903 | static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream) |
| 904 | { |
| 905 | return 0; |
| 906 | } |
| 907 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 908 | static int adev_open_input_stream(struct audio_hw_device *hw_dev, |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 909 | audio_io_handle_t handle, |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 910 | audio_devices_t devicesSpec __unused, |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 911 | struct audio_config *config, |
| 912 | struct audio_stream_in **stream_in, |
Eric Laurent | f5e2469 | 2014-07-27 16:14:57 -0700 | [diff] [blame] | 913 | audio_input_flags_t flags __unused, |
Paul McLean | 0f1753e | 2014-12-02 12:36:45 -0700 | [diff] [blame] | 914 | const char *address /*__unused*/, |
Eric Laurent | f5e2469 | 2014-07-27 16:14:57 -0700 | [diff] [blame] | 915 | audio_source_t source __unused) |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 916 | { |
Paul McLean | 1d585cc | 2016-05-24 11:28:10 -0600 | [diff] [blame] | 917 | ALOGV("adev_open_input_stream() rate:%" PRIu32 ", chanMask:0x%" PRIX32 ", fmt:%" PRIu8, |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 918 | config->sample_rate, config->channel_mask, config->format); |
| 919 | |
| 920 | struct stream_in *in = (struct stream_in *)calloc(1, sizeof(struct stream_in)); |
| 921 | int ret = 0; |
| 922 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 923 | if (in == NULL) { |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 924 | return -ENOMEM; |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 925 | } |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 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 | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 948 | in->adev = (struct audio_device *)hw_dev; |
| 949 | pthread_mutex_lock(&in->adev->lock); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 950 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 951 | in->profile = &in->adev->in_profile; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 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 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 966 | if (in->adev->device_sample_rate != 0 && /* we are playing, so lock the rate */ |
| 967 | in->adev->device_sample_rate >= RATELOCK_THRESHOLD) {/* but only for high sample rates */ |
| 968 | ret = config->sample_rate != in->adev->device_sample_rate ? -EINVAL : 0; |
| 969 | proxy_config.rate = config->sample_rate = in->adev->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 | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 971 | in->adev->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 | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 976 | pthread_mutex_unlock(&in->adev->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() */ |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 1018 | adev_add_stream_to_list(in->adev, &in->adev->input_stream_list, &in->list_node); |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 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 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 1028 | static void adev_close_input_stream(struct audio_hw_device *hw_dev, |
| 1029 | struct audio_stream_in *stream) |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1030 | { |
| 1031 | struct stream_in *in = (struct stream_in *)stream; |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 1032 | ALOGV("adev_close_input_stream(c:%d d:%d)", in->profile->card, in->profile->device); |
| 1033 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 1034 | adev_remove_stream_from_list(in->adev, &in->list_node); |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1035 | |
| 1036 | /* Close the pcm device */ |
| 1037 | in_standby(&stream->common); |
| 1038 | |
| 1039 | free(in->conversion_buffer); |
| 1040 | |
| 1041 | free(stream); |
| 1042 | } |
| 1043 | |
| 1044 | /* |
| 1045 | * ADEV Functions |
| 1046 | */ |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 1047 | 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] | 1048 | { |
| 1049 | return 0; |
| 1050 | } |
| 1051 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 1052 | 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] | 1053 | { |
| 1054 | return strdup(""); |
| 1055 | } |
| 1056 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 1057 | static int adev_init_check(const struct audio_hw_device *hw_dev) |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1058 | { |
| 1059 | return 0; |
| 1060 | } |
| 1061 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 1062 | 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] | 1063 | { |
| 1064 | return -ENOSYS; |
| 1065 | } |
| 1066 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 1067 | 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] | 1068 | { |
| 1069 | return -ENOSYS; |
| 1070 | } |
| 1071 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 1072 | 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] | 1073 | { |
| 1074 | return 0; |
| 1075 | } |
| 1076 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 1077 | 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] | 1078 | { |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 1079 | struct audio_device * adev = (struct audio_device *)hw_dev; |
Eric Laurent | 253def9 | 2014-09-14 12:18:18 -0700 | [diff] [blame] | 1080 | pthread_mutex_lock(&adev->lock); |
| 1081 | adev->mic_muted = state; |
| 1082 | pthread_mutex_unlock(&adev->lock); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1083 | return -ENOSYS; |
| 1084 | } |
| 1085 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 1086 | 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] | 1087 | { |
| 1088 | return -ENOSYS; |
| 1089 | } |
| 1090 | |
Paul McLean | 76dba68 | 2016-06-01 12:29:11 -0600 | [diff] [blame^] | 1091 | static int adev_dump(const struct audio_hw_device *device, int fd) |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1092 | { |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 1093 | dprintf(fd, "\nUSB audio module:\n"); |
| 1094 | |
| 1095 | struct audio_device* adev = (struct audio_device*)device; |
| 1096 | const int kNumRetries = 3; |
| 1097 | const int kSleepTimeMS = 500; |
| 1098 | |
| 1099 | // use pthread_mutex_trylock() in case we dumpsys during a deadlock |
| 1100 | int retry = kNumRetries; |
| 1101 | while (retry > 0 && pthread_mutex_trylock(&adev->lock) != 0) { |
| 1102 | sleep(kSleepTimeMS); |
| 1103 | retry--; |
| 1104 | } |
| 1105 | |
| 1106 | if (retry > 0) { |
| 1107 | if (list_empty(&adev->output_stream_list)) { |
| 1108 | dprintf(fd, " No output streams.\n"); |
| 1109 | } else { |
| 1110 | struct listnode* node; |
| 1111 | list_for_each(node, &adev->output_stream_list) { |
| 1112 | struct audio_stream* stream = |
| 1113 | (struct audio_stream *)node_to_item(node, struct stream_out, list_node); |
| 1114 | out_dump(stream, fd); |
| 1115 | } |
| 1116 | } |
| 1117 | |
| 1118 | if (list_empty(&adev->input_stream_list)) { |
| 1119 | dprintf(fd, "\n No input streams.\n"); |
| 1120 | } else { |
| 1121 | struct listnode* node; |
| 1122 | list_for_each(node, &adev->input_stream_list) { |
| 1123 | struct audio_stream* stream = |
| 1124 | (struct audio_stream *)node_to_item(node, struct stream_in, list_node); |
| 1125 | in_dump(stream, fd); |
| 1126 | } |
| 1127 | } |
| 1128 | |
| 1129 | pthread_mutex_unlock(&adev->lock); |
| 1130 | } else { |
| 1131 | // Couldn't lock |
| 1132 | dprintf(fd, " Could not obtain device lock.\n"); |
| 1133 | } |
| 1134 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1135 | return 0; |
| 1136 | } |
| 1137 | |
| 1138 | static int adev_close(hw_device_t *device) |
| 1139 | { |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1140 | struct audio_device *adev = (struct audio_device *)device; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1141 | free(device); |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1142 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1143 | return 0; |
| 1144 | } |
| 1145 | |
Paul McLean | 30f4185 | 2014-04-16 15:44:20 -0700 | [diff] [blame] | 1146 | 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] | 1147 | { |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1148 | if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) |
| 1149 | return -EINVAL; |
| 1150 | |
Paul McLean | eedc92e | 2013-12-19 15:46:15 -0800 | [diff] [blame] | 1151 | struct audio_device *adev = calloc(1, sizeof(struct audio_device)); |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1152 | if (!adev) |
| 1153 | return -ENOMEM; |
| 1154 | |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1155 | profile_init(&adev->out_profile, PCM_OUT); |
| 1156 | profile_init(&adev->in_profile, PCM_IN); |
| 1157 | |
Paul McLean | 6a75e4e | 2016-05-25 14:09:02 -0600 | [diff] [blame] | 1158 | list_init(&adev->output_stream_list); |
| 1159 | list_init(&adev->input_stream_list); |
| 1160 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1161 | adev->hw_device.common.tag = HARDWARE_DEVICE_TAG; |
Eric Laurent | 85e08e2 | 2012-08-28 14:30:35 -0700 | [diff] [blame] | 1162 | adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0; |
Paul McLean | c88e6ae | 2014-07-16 09:48:34 -0700 | [diff] [blame] | 1163 | adev->hw_device.common.module = (struct hw_module_t *)module; |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1164 | adev->hw_device.common.close = adev_close; |
| 1165 | |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1166 | adev->hw_device.init_check = adev_init_check; |
| 1167 | adev->hw_device.set_voice_volume = adev_set_voice_volume; |
| 1168 | adev->hw_device.set_master_volume = adev_set_master_volume; |
| 1169 | adev->hw_device.set_mode = adev_set_mode; |
| 1170 | adev->hw_device.set_mic_mute = adev_set_mic_mute; |
| 1171 | adev->hw_device.get_mic_mute = adev_get_mic_mute; |
| 1172 | adev->hw_device.set_parameters = adev_set_parameters; |
| 1173 | adev->hw_device.get_parameters = adev_get_parameters; |
| 1174 | adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size; |
| 1175 | adev->hw_device.open_output_stream = adev_open_output_stream; |
| 1176 | adev->hw_device.close_output_stream = adev_close_output_stream; |
| 1177 | adev->hw_device.open_input_stream = adev_open_input_stream; |
| 1178 | adev->hw_device.close_input_stream = adev_close_input_stream; |
| 1179 | adev->hw_device.dump = adev_dump; |
| 1180 | |
| 1181 | *device = &adev->hw_device.common; |
| 1182 | |
| 1183 | return 0; |
| 1184 | } |
| 1185 | |
| 1186 | static struct hw_module_methods_t hal_module_methods = { |
| 1187 | .open = adev_open, |
| 1188 | }; |
| 1189 | |
| 1190 | struct audio_module HAL_MODULE_INFO_SYM = { |
| 1191 | .common = { |
| 1192 | .tag = HARDWARE_MODULE_TAG, |
Mike Lockwood | 46a9809 | 2012-04-24 16:41:18 -0700 | [diff] [blame] | 1193 | .module_api_version = AUDIO_MODULE_API_VERSION_0_1, |
| 1194 | .hal_api_version = HARDWARE_HAL_API_VERSION, |
Simon Wilson | 19957a3 | 2012-04-06 16:17:12 -0700 | [diff] [blame] | 1195 | .id = AUDIO_HARDWARE_MODULE_ID, |
| 1196 | .name = "USB audio HW HAL", |
| 1197 | .author = "The Android Open Source Project", |
| 1198 | .methods = &hal_module_methods, |
| 1199 | }, |
| 1200 | }; |