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