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