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