blob: caddf97d67919f8464187430cd3c77b39265d9cf [file] [log] [blame]
Simon Wilson19957a32012-04-06 16:17:12 -07001/*
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 McLeancf611912014-04-28 13:03:18 -070018/*#define LOG_NDEBUG 0*/
Simon Wilson19957a32012-04-06 16:17:12 -070019
20#include <errno.h>
Mark Salyzyn88e458a2014-04-28 12:30:44 -070021#include <inttypes.h>
Simon Wilson19957a32012-04-06 16:17:12 -070022#include <pthread.h>
23#include <stdint.h>
Simon Wilson19957a32012-04-06 16:17:12 -070024#include <stdlib.h>
Mark Salyzyn88e458a2014-04-28 12:30:44 -070025#include <sys/time.h>
Simon Wilson19957a32012-04-06 16:17:12 -070026
Mark Salyzyn88e458a2014-04-28 12:30:44 -070027#include <log/log.h>
Simon Wilson19957a32012-04-06 16:17:12 -070028#include <cutils/str_parms.h>
29#include <cutils/properties.h>
30
Simon Wilson19957a32012-04-06 16:17:12 -070031#include <hardware/audio.h>
Paul McLeane32cbc12014-06-25 10:42:07 -070032#include <hardware/audio_alsaops.h>
33#include <hardware/hardware.h>
34
35#include <system/audio.h>
Simon Wilson19957a32012-04-06 16:17:12 -070036
37#include <tinyalsa/asoundlib.h>
38
Paul McLeanc2201152014-07-16 13:46:07 -070039#include <audio_utils/channels.h>
40
Andy Hung03576be2014-07-21 21:16:45 -070041/* 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 */
50static const unsigned k_force_channels = 0;
51
Paul McLeanc88e6ae2014-07-16 09:48:34 -070052#include "alsa_device_profile.h"
53#include "alsa_device_proxy.h"
54#include "logging.h"
Paul McLeanf62d75e2014-07-11 15:14:19 -070055
Paul McLeanc88e6ae2014-07-16 09:48:34 -070056#define DEFAULT_INPUT_BUFFER_SIZE_MS 20
Paul McLeanf62d75e2014-07-11 15:14:19 -070057
Simon Wilson19957a32012-04-06 16:17:12 -070058struct audio_device {
59 struct audio_hw_device hw_device;
60
61 pthread_mutex_t lock; /* see note below on mutex acquisition order */
Paul McLeaneedc92e2013-12-19 15:46:15 -080062
63 /* output */
Paul McLeanc88e6ae2014-07-16 09:48:34 -070064 alsa_device_profile out_profile;
Paul McLeaneedc92e2013-12-19 15:46:15 -080065
66 /* input */
Paul McLeanc88e6ae2014-07-16 09:48:34 -070067 alsa_device_profile in_profile;
Paul McLeaneedc92e2013-12-19 15:46:15 -080068
Simon Wilson19957a32012-04-06 16:17:12 -070069 bool standby;
70};
71
72struct stream_out {
73 struct audio_stream_out stream;
74
Paul McLeaneedc92e2013-12-19 15:46:15 -080075 pthread_mutex_t lock; /* see note below on mutex acquisition order */
Paul McLeaneedc92e2013-12-19 15:46:15 -080076 bool standby;
77
Paul McLeanc88e6ae2014-07-16 09:48:34 -070078 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 McLeaneedc92e2013-12-19 15:46:15 -080082
Andy Hung03576be2014-07-21 21:16:45 -070083 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 McLeaneedc92e2013-12-19 15:46:15 -080088 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 McLeaneedc92e2013-12-19 15:46:15 -080094struct stream_in {
95 struct audio_stream_in stream;
96
Simon Wilson19957a32012-04-06 16:17:12 -070097 pthread_mutex_t lock; /* see note below on mutex acquisition order */
Simon Wilson19957a32012-04-06 16:17:12 -070098 bool standby;
99
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700100 struct audio_device *dev; /* hardware information - only using this for the lock */
Paul McLeaneedc92e2013-12-19 15:46:15 -0800101
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700102 alsa_device_profile * profile;
103 alsa_device_proxy proxy; /* state of the stream */
Paul McLeanf62d75e2014-07-11 15:14:19 -0700104
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700105 // not used?
106 // struct audio_config hal_pcm_config;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800107
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700108 /* We may need to read more data from the device in order to data reduce to 16bit, 4chan */
Paul McLean30f41852014-04-16 15:44:20 -0700109 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 Wilson19957a32012-04-06 16:17:12 -0700113};
114
Paul McLeaneedc92e2013-12-19 15:46:15 -0800115/*
Paul McLeaneedc92e2013-12-19 15:46:15 -0800116 * Data Conversions
117 */
118/*
Paul McLean30f41852014-04-16 15:44:20 -0700119 * Convert a buffer of packed (3-byte) PCM24LE samples to PCM16LE samples.
120 * in_buff points to the buffer of PCM24LE samples
Paul McLeaneedc92e2013-12-19 15:46:15 -0800121 * num_in_samples size of input buffer in SAMPLES
Paul McLean30f41852014-04-16 15:44:20 -0700122 * 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 McLean30f41852014-04-16 15:44:20 -0700128 * This conversion is safe to do in-place (in_buff == out_buff).
Paul McLeane32cbc12014-06-25 10:42:07 -0700129 * TODO Move this to a utilities module.
Paul McLean30f41852014-04-16 15:44:20 -0700130 */
Paul McLeane32cbc12014-06-25 10:42:07 -0700131static size_t convert_24_3_to_16(const unsigned char * in_buff, size_t num_in_samples,
132 short * out_buff)
133{
Paul McLean30f41852014-04-16 15:44:20 -0700134 /*
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 Salyzyn88e458a2014-04-28 12:30:44 -0700140 const unsigned char* src_ptr = in_buff;
Paul McLean30f41852014-04-16 15:44:20 -0700141 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 McLean6b1c0fe2014-07-02 07:27:41 -0700153 * 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 */
165static 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 McLeanc88e6ae2014-07-16 09:48:34 -0700183static char * device_get_parameters(alsa_device_profile * profile, const char * keys)
Paul McLeaneedc92e2013-12-19 15:46:15 -0800184{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700185 ALOGV("usb:audio_hw::device_get_parameters() keys:%s", keys);
Paul McLeane32cbc12014-06-25 10:42:07 -0700186
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700187 if (profile->card < 0 || profile->device < 0) {
188 return strdup("");
Paul McLeaneedc92e2013-12-19 15:46:15 -0800189 }
Paul McLeane32cbc12014-06-25 10:42:07 -0700190
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700191 struct str_parms *query = str_parms_create_str(keys);
192 struct str_parms *result = str_parms_create();
Paul McLeane32cbc12014-06-25 10:42:07 -0700193
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700194 /* 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 McLeane32cbc12014-06-25 10:42:07 -0700201 }
202
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700203 /* 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 McLeane32cbc12014-06-25 10:42:07 -0700209 }
210
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700211 /* 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 McLeanf62d75e2014-07-11 15:14:19 -0700217 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700218 str_parms_destroy(query);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700219
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700220 char* result_str = str_parms_to_str(result);
221 str_parms_destroy(result);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700222
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700223 ALOGV("usb:audio_hw::device_get_parameters = %s", result_str);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700224
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700225 return result_str;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800226}
227
228/*
229 * HAl Functions
230 */
Simon Wilson19957a32012-04-06 16:17:12 -0700231/**
232 * NOTE: when multiple mutexes have to be acquired, always respect the
233 * following order: hw device > out stream
234 */
235
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700236/*
237 * OUT functions
238 */
Simon Wilson19957a32012-04-06 16:17:12 -0700239static uint32_t out_get_sample_rate(const struct audio_stream *stream)
240{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700241 uint32_t rate = proxy_get_sample_rate(&((struct stream_out*)stream)->proxy);
242 ALOGV("out_get_sample_rate() = %d", rate);
243 return rate;
Simon Wilson19957a32012-04-06 16:17:12 -0700244}
245
246static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
247{
248 return 0;
249}
250
251static size_t out_get_buffer_size(const struct audio_stream *stream)
252{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700253 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));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700256 return buffer_size;
Simon Wilson19957a32012-04-06 16:17:12 -0700257}
258
259static uint32_t out_get_channels(const struct audio_stream *stream)
260{
Andy Hung03576be2014-07-21 21:16:45 -0700261 const struct stream_out *out = (const struct stream_out*)stream;
262 return audio_channel_out_mask_from_count(out->hal_channel_count);
Simon Wilson19957a32012-04-06 16:17:12 -0700263}
264
265static audio_format_t out_get_format(const struct audio_stream *stream)
266{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700267 /* Note: The HAL doesn't do any FORMAT conversion at this time. It
268 * Relies on the framework to provide data in the specified format.
269 * This could change in the future.
270 */
271 alsa_device_proxy * proxy = &((struct stream_out*)stream)->proxy;
272 audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700273 return format;
Simon Wilson19957a32012-04-06 16:17:12 -0700274}
275
276static int out_set_format(struct audio_stream *stream, audio_format_t format)
277{
278 return 0;
279}
280
281static int out_standby(struct audio_stream *stream)
282{
283 struct stream_out *out = (struct stream_out *)stream;
284
285 pthread_mutex_lock(&out->dev->lock);
286 pthread_mutex_lock(&out->lock);
287
288 if (!out->standby) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700289 proxy_close(&out->proxy);
Simon Wilson19957a32012-04-06 16:17:12 -0700290 out->standby = true;
291 }
292
293 pthread_mutex_unlock(&out->lock);
294 pthread_mutex_unlock(&out->dev->lock);
295
296 return 0;
297}
298
299static int out_dump(const struct audio_stream *stream, int fd)
300{
301 return 0;
302}
303
304static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
305{
Paul McLeaneedc92e2013-12-19 15:46:15 -0800306 ALOGV("usb:audio_hw::out out_set_parameters() keys:%s", kvpairs);
307
Simon Wilson19957a32012-04-06 16:17:12 -0700308 struct stream_out *out = (struct stream_out *)stream;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700309
Simon Wilson19957a32012-04-06 16:17:12 -0700310 char value[32];
Paul McLeaneedc92e2013-12-19 15:46:15 -0800311 int param_val;
Simon Wilson19957a32012-04-06 16:17:12 -0700312 int routing = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800313 int ret_value = 0;
Eric Laurent05333d42014-08-04 20:29:17 -0700314 int card = -1;
315 int device = -1;
Simon Wilson19957a32012-04-06 16:17:12 -0700316
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700317 struct str_parms * parms = str_parms_create_str(kvpairs);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700318 pthread_mutex_lock(&out->dev->lock);
319 pthread_mutex_lock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700320
Paul McLeaneedc92e2013-12-19 15:46:15 -0800321 param_val = str_parms_get_str(parms, "card", value, sizeof(value));
Eric Laurent05333d42014-08-04 20:29:17 -0700322 if (param_val >= 0)
323 card = atoi(value);
Simon Wilson19957a32012-04-06 16:17:12 -0700324
Paul McLeaneedc92e2013-12-19 15:46:15 -0800325 param_val = str_parms_get_str(parms, "device", value, sizeof(value));
Eric Laurent05333d42014-08-04 20:29:17 -0700326 if (param_val >= 0)
327 device = atoi(value);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800328
Paul McLean2c6196f2014-08-20 16:50:25 -0700329 if (card >= 0 && device >= 0 && !profile_is_cached_for(out->profile, card, device)) {
Eric Laurent05333d42014-08-04 20:29:17 -0700330 /* cannot read pcm device info if playback is active */
331 if (!out->standby)
332 ret_value = -ENOSYS;
333 else {
334 int saved_card = out->profile->card;
335 int saved_device = out->profile->device;
336 out->profile->card = card;
337 out->profile->device = device;
338 ret_value = profile_read_device_info(out->profile) ? 0 : -EINVAL;
339 if (ret_value != 0) {
340 out->profile->card = saved_card;
341 out->profile->device = saved_device;
342 }
343 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800344 }
Paul McLean2c6196f2014-08-20 16:50:25 -0700345
Paul McLeanf62d75e2014-07-11 15:14:19 -0700346 pthread_mutex_unlock(&out->lock);
347 pthread_mutex_unlock(&out->dev->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700348 str_parms_destroy(parms);
349
Paul McLeaneedc92e2013-12-19 15:46:15 -0800350 return ret_value;
Simon Wilson19957a32012-04-06 16:17:12 -0700351}
352
Paul McLeanf62d75e2014-07-11 15:14:19 -0700353static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
354{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700355 struct stream_out *out = (struct stream_out *)stream;
Paul McLeanf62d75e2014-07-11 15:14:19 -0700356 pthread_mutex_lock(&out->dev->lock);
357 pthread_mutex_lock(&out->lock);
358
359 char * params_str = device_get_parameters(out->profile, keys);
360
361 pthread_mutex_unlock(&out->lock);
362 pthread_mutex_unlock(&out->dev->lock);
363
364 return params_str;
365}
366
Simon Wilson19957a32012-04-06 16:17:12 -0700367static uint32_t out_get_latency(const struct audio_stream_out *stream)
368{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700369 alsa_device_proxy * proxy = &((struct stream_out*)stream)->proxy;
370 return proxy_get_latency(proxy);
Simon Wilson19957a32012-04-06 16:17:12 -0700371}
372
Paul McLean30f41852014-04-16 15:44:20 -0700373static int out_set_volume(struct audio_stream_out *stream, float left, float right)
Simon Wilson19957a32012-04-06 16:17:12 -0700374{
375 return -ENOSYS;
376}
377
Paul McLeaneedc92e2013-12-19 15:46:15 -0800378/* must be called with hw device and output stream mutexes locked */
379static int start_output_stream(struct stream_out *out)
380{
Paul McLean30f41852014-04-16 15:44:20 -0700381 ALOGV("usb:audio_hw::out start_output_stream(card:%d device:%d)",
Paul McLeanf62d75e2014-07-11 15:14:19 -0700382 out->profile->card, out->profile->device);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800383
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700384 return proxy_open(&out->proxy);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800385}
386
387static ssize_t out_write(struct audio_stream_out *stream, const void* buffer, size_t bytes)
Simon Wilson19957a32012-04-06 16:17:12 -0700388{
389 int ret;
390 struct stream_out *out = (struct stream_out *)stream;
391
392 pthread_mutex_lock(&out->dev->lock);
393 pthread_mutex_lock(&out->lock);
394 if (out->standby) {
395 ret = start_output_stream(out);
396 if (ret != 0) {
Eric Laurent05333d42014-08-04 20:29:17 -0700397 pthread_mutex_unlock(&out->dev->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700398 goto err;
399 }
400 out->standby = false;
401 }
Eric Laurent05333d42014-08-04 20:29:17 -0700402 pthread_mutex_unlock(&out->dev->lock);
403
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700404 alsa_device_proxy* proxy = &out->proxy;
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700405 const void * write_buff = buffer;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800406 int num_write_buff_bytes = bytes;
Andy Hung03576be2014-07-21 21:16:45 -0700407 const int num_device_channels = proxy_get_channel_count(proxy); /* what we told alsa */
408 const int num_req_channels = out->hal_channel_count; /* what we told AudioFlinger */
Paul McLean30f41852014-04-16 15:44:20 -0700409 if (num_device_channels != num_req_channels) {
Andy Hung03576be2014-07-21 21:16:45 -0700410 /* allocate buffer */
411 const size_t required_conversion_buffer_size =
412 bytes * num_device_channels / num_req_channels;
413 if (required_conversion_buffer_size > out->conversion_buffer_size) {
414 out->conversion_buffer_size = required_conversion_buffer_size;
415 out->conversion_buffer = realloc(out->conversion_buffer,
416 out->conversion_buffer_size);
417 }
418 /* convert data */
419 const audio_format_t audio_format = out_get_format(&(out->stream.common));
420 const unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800421 num_write_buff_bytes =
Andy Hung03576be2014-07-21 21:16:45 -0700422 adjust_channels(write_buff, num_req_channels,
423 out->conversion_buffer, num_device_channels,
424 sample_size_in_bytes, num_write_buff_bytes);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800425 write_buff = out->conversion_buffer;
426 }
427
Paul McLeaneedc92e2013-12-19 15:46:15 -0800428 if (write_buff != NULL && num_write_buff_bytes != 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700429 proxy_write(&out->proxy, write_buff, num_write_buff_bytes);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800430 }
Simon Wilson19957a32012-04-06 16:17:12 -0700431
432 pthread_mutex_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700433
434 return bytes;
435
436err:
437 pthread_mutex_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700438 if (ret != 0) {
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700439 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
Simon Wilson19957a32012-04-06 16:17:12 -0700440 out_get_sample_rate(&stream->common));
441 }
442
443 return bytes;
444}
445
Paul McLean30f41852014-04-16 15:44:20 -0700446static int out_get_render_position(const struct audio_stream_out *stream, uint32_t *dsp_frames)
Simon Wilson19957a32012-04-06 16:17:12 -0700447{
448 return -EINVAL;
449}
450
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700451static int out_get_presentation_position(const struct audio_stream_out *stream,
452 uint64_t *frames, struct timespec *timestamp)
453{
454 /* FIXME - This needs to be implemented */
455 return -EINVAL;
456}
457
Simon Wilson19957a32012-04-06 16:17:12 -0700458static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
459{
460 return 0;
461}
462
463static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
464{
465 return 0;
466}
467
Paul McLean30f41852014-04-16 15:44:20 -0700468static int out_get_next_write_timestamp(const struct audio_stream_out *stream, int64_t *timestamp)
Simon Wilson19957a32012-04-06 16:17:12 -0700469{
470 return -EINVAL;
471}
472
473static int adev_open_output_stream(struct audio_hw_device *dev,
Mike Lockwood46a98092012-04-24 16:41:18 -0700474 audio_io_handle_t handle,
475 audio_devices_t devices,
476 audio_output_flags_t flags,
477 struct audio_config *config,
Eric Laurentf5e24692014-07-27 16:14:57 -0700478 struct audio_stream_out **stream_out,
479 const char *address __unused)
Simon Wilson19957a32012-04-06 16:17:12 -0700480{
Paul McLean30f41852014-04-16 15:44:20 -0700481 ALOGV("usb:audio_hw::out adev_open_output_stream() handle:0x%X, device:0x%X, flags:0x%X",
Paul McLeaneedc92e2013-12-19 15:46:15 -0800482 handle, devices, flags);
483
Simon Wilson19957a32012-04-06 16:17:12 -0700484 struct audio_device *adev = (struct audio_device *)dev;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800485
Simon Wilson19957a32012-04-06 16:17:12 -0700486 struct stream_out *out;
Simon Wilson19957a32012-04-06 16:17:12 -0700487
488 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
489 if (!out)
490 return -ENOMEM;
491
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700492 /* setup function pointers */
Simon Wilson19957a32012-04-06 16:17:12 -0700493 out->stream.common.get_sample_rate = out_get_sample_rate;
494 out->stream.common.set_sample_rate = out_set_sample_rate;
495 out->stream.common.get_buffer_size = out_get_buffer_size;
496 out->stream.common.get_channels = out_get_channels;
497 out->stream.common.get_format = out_get_format;
498 out->stream.common.set_format = out_set_format;
499 out->stream.common.standby = out_standby;
500 out->stream.common.dump = out_dump;
501 out->stream.common.set_parameters = out_set_parameters;
502 out->stream.common.get_parameters = out_get_parameters;
503 out->stream.common.add_audio_effect = out_add_audio_effect;
504 out->stream.common.remove_audio_effect = out_remove_audio_effect;
505 out->stream.get_latency = out_get_latency;
506 out->stream.set_volume = out_set_volume;
507 out->stream.write = out_write;
508 out->stream.get_render_position = out_get_render_position;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700509 out->stream.get_presentation_position = out_get_presentation_position;
Simon Wilson19957a32012-04-06 16:17:12 -0700510 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
511
512 out->dev = adev;
513
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700514 out->profile = &adev->out_profile;
Paul McLeanf62d75e2014-07-11 15:14:19 -0700515
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700516 // build this to hand to the alsa_device_proxy
517 struct pcm_config proxy_config;
Paul McLean2c6196f2014-08-20 16:50:25 -0700518 memset(&proxy_config, 0, sizeof(proxy_config));
Paul McLeaneedc92e2013-12-19 15:46:15 -0800519
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700520 int ret = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800521
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700522 /* Rate */
523 if (config->sample_rate == 0) {
524 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(out->profile);
525 } else if (profile_is_sample_rate_valid(out->profile, config->sample_rate)) {
526 proxy_config.rate = config->sample_rate;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800527 } else {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700528 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(out->profile);
529 ret = -EINVAL;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800530 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800531
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700532 /* Format */
533 if (config->format == AUDIO_FORMAT_DEFAULT) {
534 proxy_config.format = profile_get_default_format(out->profile);
535 config->format = audio_format_from_pcm_format(proxy_config.format);
536 } else {
537 enum pcm_format fmt = pcm_format_from_audio_format(config->format);
538 if (profile_is_format_valid(out->profile, fmt)) {
539 proxy_config.format = fmt;
540 } else {
541 proxy_config.format = profile_get_default_format(out->profile);
542 config->format = audio_format_from_pcm_format(proxy_config.format);
543 ret = -EINVAL;
544 }
545 }
546
547 /* Channels */
Andy Hung03576be2014-07-21 21:16:45 -0700548 unsigned proposed_channel_count = profile_get_default_channel_count(out->profile);
549 if (k_force_channels) {
550 proposed_channel_count = k_force_channels;
551 } else if (config->channel_mask != AUDIO_CHANNEL_NONE) {
552 proposed_channel_count = audio_channel_count_from_out_mask(config->channel_mask);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700553 }
Andy Hung03576be2014-07-21 21:16:45 -0700554 /* we can expose any channel count mask, and emulate internally. */
555 config->channel_mask = audio_channel_out_mask_from_count(proposed_channel_count);
556 out->hal_channel_count = proposed_channel_count;
557 /* no validity checks are needed as proxy_prepare() forces channel_count to be valid.
558 * and we emulate any channel count discrepancies in out_write(). */
559 proxy_config.channels = proposed_channel_count;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700560
561 proxy_prepare(&out->proxy, out->profile, &proxy_config);
562
563 /* TODO The retry mechanism isn't implemented in AudioPolicyManager/AudioFlinger. */
564 ret = 0;
565
Paul McLeaneedc92e2013-12-19 15:46:15 -0800566 out->conversion_buffer = NULL;
567 out->conversion_buffer_size = 0;
Simon Wilson19957a32012-04-06 16:17:12 -0700568
569 out->standby = true;
570
571 *stream_out = &out->stream;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700572
573 return ret;
Simon Wilson19957a32012-04-06 16:17:12 -0700574
575err_open:
576 free(out);
577 *stream_out = NULL;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800578 return -ENOSYS;
Simon Wilson19957a32012-04-06 16:17:12 -0700579}
580
581static void adev_close_output_stream(struct audio_hw_device *dev,
582 struct audio_stream_out *stream)
583{
Paul McLeaneedc92e2013-12-19 15:46:15 -0800584 ALOGV("usb:audio_hw::out adev_close_output_stream()");
Simon Wilson19957a32012-04-06 16:17:12 -0700585 struct stream_out *out = (struct stream_out *)stream;
586
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700587 /* Close the pcm device */
Simon Wilson19957a32012-04-06 16:17:12 -0700588 out_standby(&stream->common);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800589
590 free(out->conversion_buffer);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700591
Paul McLeaneedc92e2013-12-19 15:46:15 -0800592 out->conversion_buffer = NULL;
593 out->conversion_buffer_size = 0;
594
Simon Wilson19957a32012-04-06 16:17:12 -0700595 free(stream);
596}
597
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700598static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
599 const struct audio_config *config)
600{
601 /* TODO This needs to be calculated based on format/channels/rate */
602 return 320;
603}
604
605/*
606 * IN functions
607 */
608static uint32_t in_get_sample_rate(const struct audio_stream *stream)
609{
610 uint32_t rate = proxy_get_sample_rate(&((const struct stream_in *)stream)->proxy);
611 ALOGV("in_get_sample_rate() = %d", rate);
612 return rate;
613}
614
615static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
616{
617 ALOGV("in_set_sample_rate(%d) - NOPE", rate);
618 return -ENOSYS;
619}
620
621static size_t in_get_buffer_size(const struct audio_stream *stream)
622{
623 const struct stream_in * in = ((const struct stream_in*)stream);
624 size_t buffer_size =
625 proxy_get_period_size(&in->proxy) * audio_stream_in_frame_size(&(in->stream));
626 ALOGV("in_get_buffer_size() = %zd", buffer_size);
627
628 return buffer_size;
629}
630
631static uint32_t in_get_channels(const struct audio_stream *stream)
632{
633 /* TODO Here is the code we need when we support arbitrary channel counts
634 * alsa_device_proxy * proxy = ((struct stream_in*)stream)->proxy;
635 * unsigned channel_count = proxy_get_channel_count(proxy);
636 * uint32_t channel_mask = audio_channel_in_mask_from_count(channel_count);
637 * ALOGV("in_get_channels() = 0x%X count:%d", channel_mask, channel_count);
638 * return channel_mask;
639 */
640 /* TODO When AudioPolicyManager & AudioFlinger supports arbitrary channels
641 rewrite this to return the ACTUAL channel format */
642 return AUDIO_CHANNEL_IN_STEREO;
643}
644
645static audio_format_t in_get_format(const struct audio_stream *stream)
646{
647 /* TODO Here is the code we need when we support arbitrary input formats
648 * alsa_device_proxy * proxy = ((struct stream_in*)stream)->proxy;
649 * audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy));
650 * ALOGV("in_get_format() = %d", format);
651 * return format;
652 */
653 /* Input only supports PCM16 */
654 /* TODO When AudioPolicyManager & AudioFlinger supports arbitrary input formats
655 rewrite this to return the ACTUAL channel format (above) */
656 return AUDIO_FORMAT_PCM_16_BIT;
657}
658
659static int in_set_format(struct audio_stream *stream, audio_format_t format)
660{
661 ALOGV("in_set_format(%d) - NOPE", format);
662
663 return -ENOSYS;
664}
665
666static int in_standby(struct audio_stream *stream)
667{
668 struct stream_in *in = (struct stream_in *)stream;
669
670 pthread_mutex_lock(&in->dev->lock);
671 pthread_mutex_lock(&in->lock);
672
673 if (!in->standby) {
674 proxy_close(&in->proxy);
675 in->standby = true;
676 }
677
678 pthread_mutex_unlock(&in->lock);
679 pthread_mutex_unlock(&in->dev->lock);
680
681 return 0;
682}
683
684static int in_dump(const struct audio_stream *stream, int fd)
685{
686 return 0;
687}
688
689static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
690{
691 ALOGV("usb: audio_hw::in in_set_parameters() keys:%s", kvpairs);
692
693 struct stream_in *in = (struct stream_in *)stream;
694
695 char value[32];
696 int param_val;
697 int routing = 0;
698 int ret_value = 0;
Eric Laurent05333d42014-08-04 20:29:17 -0700699 int card = -1;
700 int device = -1;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700701
702 struct str_parms * parms = str_parms_create_str(kvpairs);
703
704 pthread_mutex_lock(&in->dev->lock);
705 pthread_mutex_lock(&in->lock);
706
Paul McLean2c6196f2014-08-20 16:50:25 -0700707 /* Device Connection Message ("card=1,device=0") */
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700708 param_val = str_parms_get_str(parms, "card", value, sizeof(value));
Eric Laurent05333d42014-08-04 20:29:17 -0700709 if (param_val >= 0)
710 card = atoi(value);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700711
712 param_val = str_parms_get_str(parms, "device", value, sizeof(value));
Eric Laurent05333d42014-08-04 20:29:17 -0700713 if (param_val >= 0)
714 device = atoi(value);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700715
Paul McLean2c6196f2014-08-20 16:50:25 -0700716 if (card >= 0 && device >= 0 && !profile_is_cached_for(in->profile, card, device)) {
Eric Laurent05333d42014-08-04 20:29:17 -0700717 /* cannot read pcm device info if playback is active */
718 if (!in->standby)
719 ret_value = -ENOSYS;
720 else {
721 int saved_card = in->profile->card;
722 int saved_device = in->profile->device;
723 in->profile->card = card;
724 in->profile->device = device;
725 ret_value = profile_read_device_info(in->profile) ? 0 : -EINVAL;
726 if (ret_value != 0) {
727 in->profile->card = saved_card;
728 in->profile->device = saved_device;
729 }
730 }
Paul McLean2c6196f2014-08-20 16:50:25 -0700731 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700732
733 pthread_mutex_unlock(&in->lock);
734 pthread_mutex_unlock(&in->dev->lock);
735
736 str_parms_destroy(parms);
737
738 return ret_value;
739}
740
741static char * in_get_parameters(const struct audio_stream *stream, const char *keys)
742{
743 struct stream_in *in = (struct stream_in *)stream;
744
745 pthread_mutex_lock(&in->dev->lock);
746 pthread_mutex_lock(&in->lock);
747
748 char * params_str = device_get_parameters(in->profile, keys);
749
750 pthread_mutex_unlock(&in->lock);
751 pthread_mutex_unlock(&in->dev->lock);
752
753 return params_str;
754}
755
756static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
757{
758 return 0;
759}
760
761static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
762{
763 return 0;
764}
765
766static int in_set_gain(struct audio_stream_in *stream, float gain)
767{
768 return 0;
769}
770
771/* must be called with hw device and output stream mutexes locked */
772static int start_input_stream(struct stream_in *in)
773{
774 ALOGV("usb:audio_hw::start_input_stream(card:%d device:%d)",
775 in->profile->card, in->profile->device);
776
777 return proxy_open(&in->proxy);
778}
779
780/* TODO mutex stuff here (see out_write) */
781static ssize_t in_read(struct audio_stream_in *stream, void* buffer, size_t bytes)
782{
783 size_t num_read_buff_bytes = 0;
784 void * read_buff = buffer;
785 void * out_buff = buffer;
786
787 struct stream_in * in = (struct stream_in *)stream;
788
789 pthread_mutex_lock(&in->dev->lock);
790 pthread_mutex_lock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700791 if (in->standby) {
792 if (start_input_stream(in) != 0) {
Eric Laurent05333d42014-08-04 20:29:17 -0700793 pthread_mutex_unlock(&in->dev->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700794 goto err;
795 }
796 in->standby = false;
797 }
Eric Laurent05333d42014-08-04 20:29:17 -0700798 pthread_mutex_unlock(&in->dev->lock);
799
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700800
801 alsa_device_profile * profile = in->profile;
802
803 /*
804 * OK, we need to figure out how much data to read to be able to output the requested
805 * number of bytes in the HAL format (16-bit, stereo).
806 */
807 num_read_buff_bytes = bytes;
808 int num_device_channels = proxy_get_channel_count(&in->proxy);
809 int num_req_channels = 2; /* always, for now */
810
811 if (num_device_channels != num_req_channels) {
812 num_read_buff_bytes = (num_device_channels * num_read_buff_bytes) / num_req_channels;
813 }
814
815 enum pcm_format format = proxy_get_format(&in->proxy);
816 if (format == PCM_FORMAT_S24_3LE) {
817 /* 24-bit USB device */
818 num_read_buff_bytes = (3 * num_read_buff_bytes) / 2;
819 } else if (format == PCM_FORMAT_S32_LE) {
820 /* 32-bit USB device */
821 num_read_buff_bytes = num_read_buff_bytes * 2;
822 }
823
824 /* Setup/Realloc the conversion buffer (if necessary). */
825 if (num_read_buff_bytes != bytes) {
826 if (num_read_buff_bytes > in->conversion_buffer_size) {
827 /*TODO Remove this when AudioPolicyManger/AudioFlinger support arbitrary formats
828 (and do these conversions themselves) */
829 in->conversion_buffer_size = num_read_buff_bytes;
830 in->conversion_buffer = realloc(in->conversion_buffer, in->conversion_buffer_size);
831 }
832 read_buff = in->conversion_buffer;
833 }
834
835 if (proxy_read(&in->proxy, read_buff, num_read_buff_bytes) == 0) {
836 /*
837 * Do any conversions necessary to send the data in the format specified to/by the HAL
838 * (but different from the ALSA format), such as 24bit ->16bit, or 4chan -> 2chan.
839 */
840 if (format != PCM_FORMAT_S16_LE) {
841 /* we need to convert */
842 if (num_device_channels != num_req_channels) {
843 out_buff = read_buff;
844 }
845
846 if (format == PCM_FORMAT_S24_3LE) {
847 num_read_buff_bytes =
848 convert_24_3_to_16(read_buff, num_read_buff_bytes / 3, out_buff);
849 } else if (format == PCM_FORMAT_S32_LE) {
850 num_read_buff_bytes =
851 convert_32_to_16(read_buff, num_read_buff_bytes / 4, out_buff);
852 } else {
853 goto err;
854 }
855 }
856
857 if (num_device_channels != num_req_channels) {
858 // ALOGV("chans dev:%d req:%d", num_device_channels, num_req_channels);
859
860 out_buff = buffer;
861 /* Num Channels conversion */
862 if (num_device_channels != num_req_channels) {
863 audio_format_t audio_format = in_get_format(&(in->stream.common));
864 unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
865
866 num_read_buff_bytes =
867 adjust_channels(read_buff, num_device_channels,
868 out_buff, num_req_channels,
869 sample_size_in_bytes, num_read_buff_bytes);
870 }
871 }
872 }
873
874err:
875 pthread_mutex_unlock(&in->lock);
876
877 return num_read_buff_bytes;
878}
879
880static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
881{
882 return 0;
883}
884
885static int adev_open_input_stream(struct audio_hw_device *dev,
886 audio_io_handle_t handle,
887 audio_devices_t devices,
888 struct audio_config *config,
889 struct audio_stream_in **stream_in,
Eric Laurentf5e24692014-07-27 16:14:57 -0700890 audio_input_flags_t flags __unused,
891 const char *address __unused,
892 audio_source_t source __unused)
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700893{
894 ALOGV("usb: in adev_open_input_stream() rate:%" PRIu32 ", chanMask:0x%" PRIX32 ", fmt:%" PRIu8,
895 config->sample_rate, config->channel_mask, config->format);
896
897 struct stream_in *in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
898 int ret = 0;
899
900 if (in == NULL)
901 return -ENOMEM;
902
903 /* setup function pointers */
904 in->stream.common.get_sample_rate = in_get_sample_rate;
905 in->stream.common.set_sample_rate = in_set_sample_rate;
906 in->stream.common.get_buffer_size = in_get_buffer_size;
907 in->stream.common.get_channels = in_get_channels;
908 in->stream.common.get_format = in_get_format;
909 in->stream.common.set_format = in_set_format;
910 in->stream.common.standby = in_standby;
911 in->stream.common.dump = in_dump;
912 in->stream.common.set_parameters = in_set_parameters;
913 in->stream.common.get_parameters = in_get_parameters;
914 in->stream.common.add_audio_effect = in_add_audio_effect;
915 in->stream.common.remove_audio_effect = in_remove_audio_effect;
916
917 in->stream.set_gain = in_set_gain;
918 in->stream.read = in_read;
919 in->stream.get_input_frames_lost = in_get_input_frames_lost;
920
921 in->dev = (struct audio_device *)dev;
922
923 in->profile = &in->dev->in_profile;
924
925 struct pcm_config proxy_config;
Paul McLean2c6196f2014-08-20 16:50:25 -0700926 memset(&proxy_config, 0, sizeof(proxy_config));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700927
928 /* Rate */
929 if (config->sample_rate == 0) {
930 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(in->profile);
931 } else if (profile_is_sample_rate_valid(in->profile, config->sample_rate)) {
932 proxy_config.rate = config->sample_rate;
933 } else {
934 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(in->profile);
935 ret = -EINVAL;
936 }
937
938 /* Format */
939 /* until the framework supports format conversion, just take what it asks for
940 * i.e. AUDIO_FORMAT_PCM_16_BIT */
941 if (config->format == AUDIO_FORMAT_DEFAULT) {
942 /* just return AUDIO_FORMAT_PCM_16_BIT until the framework supports other input
943 * formats */
944 config->format = AUDIO_FORMAT_PCM_16_BIT;
945 proxy_config.format = PCM_FORMAT_S16_LE;
946 } else if (config->format == AUDIO_FORMAT_PCM_16_BIT) {
947 /* Always accept AUDIO_FORMAT_PCM_16_BIT until the framework supports other input
948 * formats */
949 proxy_config.format = PCM_FORMAT_S16_LE;
950 } else {
951 /* When the framework support other formats, validate here */
952 config->format = AUDIO_FORMAT_PCM_16_BIT;
953 proxy_config.format = PCM_FORMAT_S16_LE;
954 ret = -EINVAL;
955 }
956
957 if (config->channel_mask == AUDIO_CHANNEL_NONE) {
958 /* just return AUDIO_CHANNEL_IN_STEREO until the framework supports other input
959 * formats */
960 config->channel_mask = AUDIO_CHANNEL_IN_STEREO;
961
962 } else if (config->channel_mask != AUDIO_CHANNEL_IN_STEREO) {
963 /* allow only stereo capture for now */
964 config->channel_mask = AUDIO_CHANNEL_IN_STEREO;
965 ret = -EINVAL;
966 }
967 // proxy_config.channels = 0; /* don't change */
968 proxy_config.channels = profile_get_default_channel_count(in->profile);
969
970 proxy_prepare(&in->proxy, in->profile, &proxy_config);
971
972 in->standby = true;
973
974 in->conversion_buffer = NULL;
975 in->conversion_buffer_size = 0;
976
977 *stream_in = &in->stream;
978
979 return ret;
980}
981
982static void adev_close_input_stream(struct audio_hw_device *dev, struct audio_stream_in *stream)
983{
984 struct stream_in *in = (struct stream_in *)stream;
985
986 /* Close the pcm device */
987 in_standby(&stream->common);
988
989 free(in->conversion_buffer);
990
991 free(stream);
992}
993
994/*
995 * ADEV Functions
996 */
Simon Wilson19957a32012-04-06 16:17:12 -0700997static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
998{
Paul McLean2c6196f2014-08-20 16:50:25 -0700999 ALOGV("audio_hw:usb adev_set_parameters(%s)", kvpairs);
1000
1001 struct audio_device * adev = (struct audio_device *)dev;
1002
1003 char value[32];
1004 int param_val;
1005
1006 struct str_parms * parms = str_parms_create_str(kvpairs);
1007
1008 /* Check for the "disconnect" message */
1009 param_val = str_parms_get_str(parms, "disconnect", value, sizeof(value));
1010 if (param_val >= 0) {
1011 audio_devices_t device = (audio_devices_t)atoi(value);
1012
1013 param_val = str_parms_get_str(parms, "card", value, sizeof(value));
1014 int alsa_card = param_val >= 0 ? atoi(value) : -1;
1015
1016 param_val = str_parms_get_str(parms, "device", value, sizeof(value));
1017 int alsa_device = param_val >= 0 ? atoi(value) : -1;
1018
1019 if (alsa_card >= 0 && alsa_device >= 0) {
1020 /* "decache" the profile */
1021 pthread_mutex_lock(&adev->lock);
1022 if (device == AUDIO_DEVICE_OUT_USB_DEVICE &&
1023 profile_is_cached_for(&adev->out_profile, alsa_card, alsa_device)) {
1024 profile_decache(&adev->out_profile);
1025 }
1026 if (device == AUDIO_DEVICE_IN_USB_DEVICE &&
1027 profile_is_cached_for(&adev->in_profile, alsa_card, alsa_device)) {
1028 profile_decache(&adev->in_profile);
1029 }
1030 pthread_mutex_unlock(&adev->lock);
1031 }
1032 }
1033
Simon Wilson19957a32012-04-06 16:17:12 -07001034 return 0;
1035}
1036
Paul McLean30f41852014-04-16 15:44:20 -07001037static char * adev_get_parameters(const struct audio_hw_device *dev, const char *keys)
Simon Wilson19957a32012-04-06 16:17:12 -07001038{
1039 return strdup("");
1040}
1041
1042static int adev_init_check(const struct audio_hw_device *dev)
1043{
1044 return 0;
1045}
1046
1047static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
1048{
1049 return -ENOSYS;
1050}
1051
1052static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
1053{
1054 return -ENOSYS;
1055}
1056
1057static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
1058{
1059 return 0;
1060}
1061
1062static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
1063{
1064 return -ENOSYS;
1065}
1066
1067static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
1068{
1069 return -ENOSYS;
1070}
1071
Simon Wilson19957a32012-04-06 16:17:12 -07001072static int adev_dump(const audio_hw_device_t *device, int fd)
1073{
1074 return 0;
1075}
1076
1077static int adev_close(hw_device_t *device)
1078{
Paul McLeaneedc92e2013-12-19 15:46:15 -08001079 struct audio_device *adev = (struct audio_device *)device;
Simon Wilson19957a32012-04-06 16:17:12 -07001080 free(device);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001081
Simon Wilson19957a32012-04-06 16:17:12 -07001082 return 0;
1083}
1084
Paul McLean30f41852014-04-16 15:44:20 -07001085static int adev_open(const hw_module_t* module, const char* name, hw_device_t** device)
Simon Wilson19957a32012-04-06 16:17:12 -07001086{
Simon Wilson19957a32012-04-06 16:17:12 -07001087 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
1088 return -EINVAL;
1089
Paul McLeaneedc92e2013-12-19 15:46:15 -08001090 struct audio_device *adev = calloc(1, sizeof(struct audio_device));
Simon Wilson19957a32012-04-06 16:17:12 -07001091 if (!adev)
1092 return -ENOMEM;
1093
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001094 profile_init(&adev->out_profile, PCM_OUT);
1095 profile_init(&adev->in_profile, PCM_IN);
1096
Simon Wilson19957a32012-04-06 16:17:12 -07001097 adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
Eric Laurent85e08e22012-08-28 14:30:35 -07001098 adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001099 adev->hw_device.common.module = (struct hw_module_t *)module;
Simon Wilson19957a32012-04-06 16:17:12 -07001100 adev->hw_device.common.close = adev_close;
1101
Simon Wilson19957a32012-04-06 16:17:12 -07001102 adev->hw_device.init_check = adev_init_check;
1103 adev->hw_device.set_voice_volume = adev_set_voice_volume;
1104 adev->hw_device.set_master_volume = adev_set_master_volume;
1105 adev->hw_device.set_mode = adev_set_mode;
1106 adev->hw_device.set_mic_mute = adev_set_mic_mute;
1107 adev->hw_device.get_mic_mute = adev_get_mic_mute;
1108 adev->hw_device.set_parameters = adev_set_parameters;
1109 adev->hw_device.get_parameters = adev_get_parameters;
1110 adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size;
1111 adev->hw_device.open_output_stream = adev_open_output_stream;
1112 adev->hw_device.close_output_stream = adev_close_output_stream;
1113 adev->hw_device.open_input_stream = adev_open_input_stream;
1114 adev->hw_device.close_input_stream = adev_close_input_stream;
1115 adev->hw_device.dump = adev_dump;
1116
1117 *device = &adev->hw_device.common;
1118
1119 return 0;
1120}
1121
1122static struct hw_module_methods_t hal_module_methods = {
1123 .open = adev_open,
1124};
1125
1126struct audio_module HAL_MODULE_INFO_SYM = {
1127 .common = {
1128 .tag = HARDWARE_MODULE_TAG,
Mike Lockwood46a98092012-04-24 16:41:18 -07001129 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
1130 .hal_api_version = HARDWARE_HAL_API_VERSION,
Simon Wilson19957a32012-04-06 16:17:12 -07001131 .id = AUDIO_HARDWARE_MODULE_ID,
1132 .name = "USB audio HW HAL",
1133 .author = "The Android Open Source Project",
1134 .methods = &hal_module_methods,
1135 },
1136};