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