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