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