blob: 2b887971dfdca0228b22c7d0dda3a2b25fc2d9aa [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
Paul McLean9ab869a2015-01-13 09:37:06 -080017#define LOG_TAG "modules.usbaudio.audio_hal"
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"
Paul McLean9ab869a2015-01-13 09:37:06 -080054#include "alsa_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
Andy Hung780f1f82015-04-22 22:14:39 -070058// fixed channel count of 8 limitation (for data processing in AudioFlinger)
59#define FCC_8 8
60
Simon Wilson19957a32012-04-06 16:17:12 -070061struct audio_device {
62 struct audio_hw_device hw_device;
63
64 pthread_mutex_t lock; /* see note below on mutex acquisition order */
Paul McLeaneedc92e2013-12-19 15:46:15 -080065
66 /* output */
Paul McLeanc88e6ae2014-07-16 09:48:34 -070067 alsa_device_profile out_profile;
Paul McLeaneedc92e2013-12-19 15:46:15 -080068
69 /* input */
Paul McLeanc88e6ae2014-07-16 09:48:34 -070070 alsa_device_profile in_profile;
Paul McLeaneedc92e2013-12-19 15:46:15 -080071
Eric Laurent253def92014-09-14 12:18:18 -070072 bool mic_muted;
73
Simon Wilson19957a32012-04-06 16:17:12 -070074 bool standby;
75};
76
77struct stream_out {
78 struct audio_stream_out stream;
79
Paul McLeaneedc92e2013-12-19 15:46:15 -080080 pthread_mutex_t lock; /* see note below on mutex acquisition order */
Paul McLeaneedc92e2013-12-19 15:46:15 -080081 bool standby;
82
Paul McLeanc88e6ae2014-07-16 09:48:34 -070083 struct audio_device *dev; /* hardware information - only using this for the lock */
84
Paul McLean65ec72b2015-02-18 09:13:24 -080085 alsa_device_profile * profile; /* Points to the alsa_device_profile in the audio_device */
Paul McLeanc88e6ae2014-07-16 09:48:34 -070086 alsa_device_proxy proxy; /* state of the stream */
Paul McLeaneedc92e2013-12-19 15:46:15 -080087
Andy Hung03576be2014-07-21 21:16:45 -070088 unsigned hal_channel_count; /* channel count exposed to AudioFlinger.
89 * This may differ from the device channel count when
90 * the device is not compatible with AudioFlinger
91 * capabilities, e.g. exposes too many channels or
92 * too few channels. */
Paul McLeaneedc92e2013-12-19 15:46:15 -080093 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 */
97};
98
Paul McLeaneedc92e2013-12-19 15:46:15 -080099struct stream_in {
100 struct audio_stream_in stream;
101
Simon Wilson19957a32012-04-06 16:17:12 -0700102 pthread_mutex_t lock; /* see note below on mutex acquisition order */
Simon Wilson19957a32012-04-06 16:17:12 -0700103 bool standby;
104
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700105 struct audio_device *dev; /* hardware information - only using this for the lock */
Paul McLeaneedc92e2013-12-19 15:46:15 -0800106
Paul McLean65ec72b2015-02-18 09:13:24 -0800107 alsa_device_profile * profile; /* Points to the alsa_device_profile in the audio_device */
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700108 alsa_device_proxy proxy; /* state of the stream */
Paul McLeanf62d75e2014-07-11 15:14:19 -0700109
Paul McLean2cfd81b2014-09-15 12:32:23 -0700110 unsigned hal_channel_count; /* channel count exposed to AudioFlinger.
111 * This may differ from the device channel count when
112 * the device is not compatible with AudioFlinger
113 * capabilities, e.g. exposes too many channels or
114 * too few channels. */
Andy Hung780f1f82015-04-22 22:14:39 -0700115 audio_channel_mask_t hal_channel_mask; /* channel mask exposed to AudioFlinger. */
116
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700117 /* 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 -0700118 void * conversion_buffer; /* any conversions are put into here
119 * they could come from here too if
120 * there was a previous conversion */
121 size_t conversion_buffer_size; /* in bytes */
Simon Wilson19957a32012-04-06 16:17:12 -0700122};
123
Paul McLeaneedc92e2013-12-19 15:46:15 -0800124/*
Paul McLeaneedc92e2013-12-19 15:46:15 -0800125 * Data Conversions
126 */
127/*
Paul McLean30f41852014-04-16 15:44:20 -0700128 * Convert a buffer of packed (3-byte) PCM24LE samples to PCM16LE samples.
129 * in_buff points to the buffer of PCM24LE samples
Paul McLeaneedc92e2013-12-19 15:46:15 -0800130 * num_in_samples size of input buffer in SAMPLES
Paul McLean30f41852014-04-16 15:44:20 -0700131 * out_buff points to the buffer to receive converted PCM16LE LE samples.
132 * returns
133 * the number of BYTES of output data.
134 * We are doing this since we *always* present to The Framework as A PCM16LE device, but need to
135 * support PCM24_3LE (24-bit, packed).
136 * NOTE:
Paul McLean30f41852014-04-16 15:44:20 -0700137 * This conversion is safe to do in-place (in_buff == out_buff).
Paul McLeane32cbc12014-06-25 10:42:07 -0700138 * TODO Move this to a utilities module.
Paul McLean30f41852014-04-16 15:44:20 -0700139 */
Paul McLeane32cbc12014-06-25 10:42:07 -0700140static size_t convert_24_3_to_16(const unsigned char * in_buff, size_t num_in_samples,
141 short * out_buff)
142{
Paul McLean30f41852014-04-16 15:44:20 -0700143 /*
144 * Move from front to back so that the conversion can be done in-place
145 * i.e. in_buff == out_buff
146 */
147 /* we need 2 bytes in the output for every 3 bytes in the input */
148 unsigned char* dst_ptr = (unsigned char*)out_buff;
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700149 const unsigned char* src_ptr = in_buff;
Paul McLean30f41852014-04-16 15:44:20 -0700150 size_t src_smpl_index;
151 for (src_smpl_index = 0; src_smpl_index < num_in_samples; src_smpl_index++) {
152 src_ptr++; /* lowest-(skip)-byte */
153 *dst_ptr++ = *src_ptr++; /* low-byte */
154 *dst_ptr++ = *src_ptr++; /* high-byte */
155 }
156
157 /* return number of *bytes* generated: */
158 return num_in_samples * 2;
159}
160
161/*
Paul McLean6b1c0fe2014-07-02 07:27:41 -0700162 * Convert a buffer of packed (3-byte) PCM32 samples to PCM16LE samples.
163 * in_buff points to the buffer of PCM32 samples
164 * num_in_samples size of input buffer in SAMPLES
165 * out_buff points to the buffer to receive converted PCM16LE LE samples.
166 * returns
167 * the number of BYTES of output data.
168 * We are doing this since we *always* present to The Framework as A PCM16LE device, but need to
169 * support PCM_FORMAT_S32_LE (32-bit).
170 * NOTE:
171 * This conversion is safe to do in-place (in_buff == out_buff).
172 * TODO Move this to a utilities module.
173 */
174static size_t convert_32_to_16(const int32_t * in_buff, size_t num_in_samples, short * out_buff)
175{
176 /*
177 * Move from front to back so that the conversion can be done in-place
178 * i.e. in_buff == out_buff
179 */
180
181 short * dst_ptr = out_buff;
182 const int32_t* src_ptr = in_buff;
183 size_t src_smpl_index;
184 for (src_smpl_index = 0; src_smpl_index < num_in_samples; src_smpl_index++) {
185 *dst_ptr++ = *src_ptr++ >> 16;
186 }
187
188 /* return number of *bytes* generated: */
189 return num_in_samples * 2;
190}
191
Paul McLean0f1753e2014-12-02 12:36:45 -0700192/*
193 * Extract the card and device numbers from the supplied key/value pairs.
194 * kvpairs A null-terminated string containing the key/value pairs or card and device.
195 * i.e. "card=1;device=42"
196 * card A pointer to a variable to receive the parsed-out card number.
197 * device A pointer to a variable to receive the parsed-out device number.
198 * NOTE: The variables pointed to by card and device return -1 (undefined) if the
199 * associated key/value pair is not found in the provided string.
Paul McLean65ec72b2015-02-18 09:13:24 -0800200 * Return true if the kvpairs string contain a card/device spec, false otherwise.
Paul McLean0f1753e2014-12-02 12:36:45 -0700201 */
Paul McLean65ec72b2015-02-18 09:13:24 -0800202static bool parse_card_device_params(const char *kvpairs, int *card, int *device)
Paul McLean0f1753e2014-12-02 12:36:45 -0700203{
204 struct str_parms * parms = str_parms_create_str(kvpairs);
205 char value[32];
206 int param_val;
207
208 // initialize to "undefined" state.
209 *card = -1;
210 *device = -1;
211
212 param_val = str_parms_get_str(parms, "card", value, sizeof(value));
213 if (param_val >= 0) {
214 *card = atoi(value);
215 }
216
217 param_val = str_parms_get_str(parms, "device", value, sizeof(value));
218 if (param_val >= 0) {
219 *device = atoi(value);
220 }
221
222 str_parms_destroy(parms);
Paul McLean65ec72b2015-02-18 09:13:24 -0800223
224 return *card >= 0 && *device >= 0;
Paul McLean0f1753e2014-12-02 12:36:45 -0700225}
226
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700227static char * device_get_parameters(alsa_device_profile * profile, const char * keys)
Paul McLeaneedc92e2013-12-19 15:46:15 -0800228{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700229 if (profile->card < 0 || profile->device < 0) {
230 return strdup("");
Paul McLeaneedc92e2013-12-19 15:46:15 -0800231 }
Paul McLeane32cbc12014-06-25 10:42:07 -0700232
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700233 struct str_parms *query = str_parms_create_str(keys);
234 struct str_parms *result = str_parms_create();
Paul McLeane32cbc12014-06-25 10:42:07 -0700235
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700236 /* These keys are from hardware/libhardware/include/audio.h */
237 /* supported sample rates */
238 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) {
239 char* rates_list = profile_get_sample_rate_strs(profile);
240 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES,
241 rates_list);
242 free(rates_list);
Paul McLeane32cbc12014-06-25 10:42:07 -0700243 }
244
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700245 /* supported channel counts */
246 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) {
247 char* channels_list = profile_get_channel_count_strs(profile);
248 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_CHANNELS,
249 channels_list);
250 free(channels_list);
Paul McLeane32cbc12014-06-25 10:42:07 -0700251 }
252
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700253 /* supported sample formats */
254 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) {
255 char * format_params = profile_get_format_strs(profile);
256 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_FORMATS,
257 format_params);
258 free(format_params);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700259 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700260 str_parms_destroy(query);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700261
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700262 char* result_str = str_parms_to_str(result);
263 str_parms_destroy(result);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700264
Paul McLean65ec72b2015-02-18 09:13:24 -0800265 ALOGV("device_get_parameters = %s", result_str);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700266
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700267 return result_str;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800268}
269
270/*
271 * HAl Functions
272 */
Simon Wilson19957a32012-04-06 16:17:12 -0700273/**
274 * NOTE: when multiple mutexes have to be acquired, always respect the
275 * following order: hw device > out stream
276 */
277
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700278/*
279 * OUT functions
280 */
Simon Wilson19957a32012-04-06 16:17:12 -0700281static uint32_t out_get_sample_rate(const struct audio_stream *stream)
282{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700283 uint32_t rate = proxy_get_sample_rate(&((struct stream_out*)stream)->proxy);
284 ALOGV("out_get_sample_rate() = %d", rate);
285 return rate;
Simon Wilson19957a32012-04-06 16:17:12 -0700286}
287
288static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
289{
290 return 0;
291}
292
293static size_t out_get_buffer_size(const struct audio_stream *stream)
294{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700295 const struct stream_out* out = (const struct stream_out*)stream;
296 size_t buffer_size =
297 proxy_get_period_size(&out->proxy) * audio_stream_out_frame_size(&(out->stream));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700298 return buffer_size;
Simon Wilson19957a32012-04-06 16:17:12 -0700299}
300
301static uint32_t out_get_channels(const struct audio_stream *stream)
302{
Andy Hung03576be2014-07-21 21:16:45 -0700303 const struct stream_out *out = (const struct stream_out*)stream;
304 return audio_channel_out_mask_from_count(out->hal_channel_count);
Simon Wilson19957a32012-04-06 16:17:12 -0700305}
306
307static audio_format_t out_get_format(const struct audio_stream *stream)
308{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700309 /* Note: The HAL doesn't do any FORMAT conversion at this time. It
310 * Relies on the framework to provide data in the specified format.
311 * This could change in the future.
312 */
313 alsa_device_proxy * proxy = &((struct stream_out*)stream)->proxy;
314 audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700315 return format;
Simon Wilson19957a32012-04-06 16:17:12 -0700316}
317
318static int out_set_format(struct audio_stream *stream, audio_format_t format)
319{
320 return 0;
321}
322
323static int out_standby(struct audio_stream *stream)
324{
325 struct stream_out *out = (struct stream_out *)stream;
326
327 pthread_mutex_lock(&out->dev->lock);
328 pthread_mutex_lock(&out->lock);
329
330 if (!out->standby) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700331 proxy_close(&out->proxy);
Simon Wilson19957a32012-04-06 16:17:12 -0700332 out->standby = true;
333 }
334
335 pthread_mutex_unlock(&out->lock);
336 pthread_mutex_unlock(&out->dev->lock);
337
338 return 0;
339}
340
341static int out_dump(const struct audio_stream *stream, int fd)
342{
343 return 0;
344}
345
346static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
347{
Paul McLean65ec72b2015-02-18 09:13:24 -0800348 ALOGV("out_set_parameters() keys:%s", kvpairs);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800349
Simon Wilson19957a32012-04-06 16:17:12 -0700350 struct stream_out *out = (struct stream_out *)stream;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700351
Simon Wilson19957a32012-04-06 16:17:12 -0700352 int routing = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800353 int ret_value = 0;
Eric Laurent05333d42014-08-04 20:29:17 -0700354 int card = -1;
355 int device = -1;
Simon Wilson19957a32012-04-06 16:17:12 -0700356
Paul McLean65ec72b2015-02-18 09:13:24 -0800357 if (!parse_card_device_params(kvpairs, &card, &device)) {
358 // nothing to do
359 return ret_value;
360 }
361
362 /* Lock the device because that is where the profile lives */
Paul McLeanf62d75e2014-07-11 15:14:19 -0700363 pthread_mutex_lock(&out->dev->lock);
364 pthread_mutex_lock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700365
Paul McLean65ec72b2015-02-18 09:13:24 -0800366 if (!profile_is_cached_for(out->profile, card, device)) {
Eric Laurent05333d42014-08-04 20:29:17 -0700367 /* cannot read pcm device info if playback is active */
368 if (!out->standby)
369 ret_value = -ENOSYS;
370 else {
371 int saved_card = out->profile->card;
372 int saved_device = out->profile->device;
373 out->profile->card = card;
374 out->profile->device = device;
375 ret_value = profile_read_device_info(out->profile) ? 0 : -EINVAL;
376 if (ret_value != 0) {
377 out->profile->card = saved_card;
378 out->profile->device = saved_device;
379 }
380 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800381 }
Paul McLean2c6196f2014-08-20 16:50:25 -0700382
Paul McLeanf62d75e2014-07-11 15:14:19 -0700383 pthread_mutex_unlock(&out->lock);
384 pthread_mutex_unlock(&out->dev->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700385
Paul McLeaneedc92e2013-12-19 15:46:15 -0800386 return ret_value;
Simon Wilson19957a32012-04-06 16:17:12 -0700387}
388
Paul McLeanf62d75e2014-07-11 15:14:19 -0700389static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
390{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700391 struct stream_out *out = (struct stream_out *)stream;
Paul McLeanf62d75e2014-07-11 15:14:19 -0700392 pthread_mutex_lock(&out->dev->lock);
393 pthread_mutex_lock(&out->lock);
394
395 char * params_str = device_get_parameters(out->profile, keys);
396
397 pthread_mutex_unlock(&out->lock);
398 pthread_mutex_unlock(&out->dev->lock);
399
400 return params_str;
401}
402
Simon Wilson19957a32012-04-06 16:17:12 -0700403static uint32_t out_get_latency(const struct audio_stream_out *stream)
404{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700405 alsa_device_proxy * proxy = &((struct stream_out*)stream)->proxy;
406 return proxy_get_latency(proxy);
Simon Wilson19957a32012-04-06 16:17:12 -0700407}
408
Paul McLean30f41852014-04-16 15:44:20 -0700409static int out_set_volume(struct audio_stream_out *stream, float left, float right)
Simon Wilson19957a32012-04-06 16:17:12 -0700410{
411 return -ENOSYS;
412}
413
Paul McLeaneedc92e2013-12-19 15:46:15 -0800414/* must be called with hw device and output stream mutexes locked */
415static int start_output_stream(struct stream_out *out)
416{
Paul McLean65ec72b2015-02-18 09:13:24 -0800417 ALOGV("start_output_stream(card:%d device:%d)", out->profile->card, out->profile->device);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800418
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700419 return proxy_open(&out->proxy);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800420}
421
422static ssize_t out_write(struct audio_stream_out *stream, const void* buffer, size_t bytes)
Simon Wilson19957a32012-04-06 16:17:12 -0700423{
424 int ret;
425 struct stream_out *out = (struct stream_out *)stream;
426
427 pthread_mutex_lock(&out->dev->lock);
428 pthread_mutex_lock(&out->lock);
429 if (out->standby) {
430 ret = start_output_stream(out);
431 if (ret != 0) {
Paul McLeanf55fdb22015-02-20 15:39:59 -0800432 pthread_mutex_unlock(&out->dev->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700433 goto err;
434 }
435 out->standby = false;
436 }
Paul McLeanf55fdb22015-02-20 15:39:59 -0800437 pthread_mutex_unlock(&out->dev->lock);
Eric Laurent05333d42014-08-04 20:29:17 -0700438
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700439 alsa_device_proxy* proxy = &out->proxy;
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700440 const void * write_buff = buffer;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800441 int num_write_buff_bytes = bytes;
Andy Hung03576be2014-07-21 21:16:45 -0700442 const int num_device_channels = proxy_get_channel_count(proxy); /* what we told alsa */
443 const int num_req_channels = out->hal_channel_count; /* what we told AudioFlinger */
Paul McLean30f41852014-04-16 15:44:20 -0700444 if (num_device_channels != num_req_channels) {
Andy Hung03576be2014-07-21 21:16:45 -0700445 /* allocate buffer */
446 const size_t required_conversion_buffer_size =
447 bytes * num_device_channels / num_req_channels;
448 if (required_conversion_buffer_size > out->conversion_buffer_size) {
449 out->conversion_buffer_size = required_conversion_buffer_size;
450 out->conversion_buffer = realloc(out->conversion_buffer,
451 out->conversion_buffer_size);
452 }
453 /* convert data */
454 const audio_format_t audio_format = out_get_format(&(out->stream.common));
455 const unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800456 num_write_buff_bytes =
Andy Hung03576be2014-07-21 21:16:45 -0700457 adjust_channels(write_buff, num_req_channels,
458 out->conversion_buffer, num_device_channels,
459 sample_size_in_bytes, num_write_buff_bytes);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800460 write_buff = out->conversion_buffer;
461 }
462
Paul McLeaneedc92e2013-12-19 15:46:15 -0800463 if (write_buff != NULL && num_write_buff_bytes != 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700464 proxy_write(&out->proxy, write_buff, num_write_buff_bytes);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800465 }
Simon Wilson19957a32012-04-06 16:17:12 -0700466
467 pthread_mutex_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700468
469 return bytes;
470
471err:
472 pthread_mutex_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700473 if (ret != 0) {
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700474 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
Simon Wilson19957a32012-04-06 16:17:12 -0700475 out_get_sample_rate(&stream->common));
476 }
477
478 return bytes;
479}
480
Paul McLean30f41852014-04-16 15:44:20 -0700481static int out_get_render_position(const struct audio_stream_out *stream, uint32_t *dsp_frames)
Simon Wilson19957a32012-04-06 16:17:12 -0700482{
483 return -EINVAL;
484}
485
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700486static int out_get_presentation_position(const struct audio_stream_out *stream,
487 uint64_t *frames, struct timespec *timestamp)
488{
489 /* FIXME - This needs to be implemented */
490 return -EINVAL;
491}
492
Simon Wilson19957a32012-04-06 16:17:12 -0700493static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
494{
495 return 0;
496}
497
498static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
499{
500 return 0;
501}
502
Paul McLean30f41852014-04-16 15:44:20 -0700503static int out_get_next_write_timestamp(const struct audio_stream_out *stream, int64_t *timestamp)
Simon Wilson19957a32012-04-06 16:17:12 -0700504{
505 return -EINVAL;
506}
507
508static int adev_open_output_stream(struct audio_hw_device *dev,
Mike Lockwood46a98092012-04-24 16:41:18 -0700509 audio_io_handle_t handle,
510 audio_devices_t devices,
511 audio_output_flags_t flags,
512 struct audio_config *config,
Eric Laurentf5e24692014-07-27 16:14:57 -0700513 struct audio_stream_out **stream_out,
Paul McLean0f1753e2014-12-02 12:36:45 -0700514 const char *address /*__unused*/)
Simon Wilson19957a32012-04-06 16:17:12 -0700515{
Paul McLean65ec72b2015-02-18 09:13:24 -0800516 ALOGV("adev_open_output_stream() handle:0x%X, device:0x%X, flags:0x%X, addr:%s",
Paul McLean0f1753e2014-12-02 12:36:45 -0700517 handle, devices, flags, address);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800518
Simon Wilson19957a32012-04-06 16:17:12 -0700519 struct audio_device *adev = (struct audio_device *)dev;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800520
Simon Wilson19957a32012-04-06 16:17:12 -0700521 struct stream_out *out;
Simon Wilson19957a32012-04-06 16:17:12 -0700522
523 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
524 if (!out)
525 return -ENOMEM;
526
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700527 /* setup function pointers */
Simon Wilson19957a32012-04-06 16:17:12 -0700528 out->stream.common.get_sample_rate = out_get_sample_rate;
529 out->stream.common.set_sample_rate = out_set_sample_rate;
530 out->stream.common.get_buffer_size = out_get_buffer_size;
531 out->stream.common.get_channels = out_get_channels;
532 out->stream.common.get_format = out_get_format;
533 out->stream.common.set_format = out_set_format;
534 out->stream.common.standby = out_standby;
535 out->stream.common.dump = out_dump;
536 out->stream.common.set_parameters = out_set_parameters;
537 out->stream.common.get_parameters = out_get_parameters;
538 out->stream.common.add_audio_effect = out_add_audio_effect;
539 out->stream.common.remove_audio_effect = out_remove_audio_effect;
540 out->stream.get_latency = out_get_latency;
541 out->stream.set_volume = out_set_volume;
542 out->stream.write = out_write;
543 out->stream.get_render_position = out_get_render_position;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700544 out->stream.get_presentation_position = out_get_presentation_position;
Simon Wilson19957a32012-04-06 16:17:12 -0700545 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
546
547 out->dev = adev;
Paul McLean0f1753e2014-12-02 12:36:45 -0700548 pthread_mutex_lock(&adev->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700549 out->profile = &adev->out_profile;
Paul McLeanf62d75e2014-07-11 15:14:19 -0700550
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700551 // build this to hand to the alsa_device_proxy
552 struct pcm_config proxy_config;
Paul McLean2c6196f2014-08-20 16:50:25 -0700553 memset(&proxy_config, 0, sizeof(proxy_config));
Paul McLeaneedc92e2013-12-19 15:46:15 -0800554
Paul McLean0f1753e2014-12-02 12:36:45 -0700555 /* Pull out the card/device pair */
556 parse_card_device_params(address, &(out->profile->card), &(out->profile->device));
557
558 profile_read_device_info(out->profile);
559
560 pthread_mutex_unlock(&adev->lock);
561
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700562 int ret = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800563
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700564 /* Rate */
565 if (config->sample_rate == 0) {
566 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(out->profile);
567 } else if (profile_is_sample_rate_valid(out->profile, config->sample_rate)) {
568 proxy_config.rate = config->sample_rate;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800569 } else {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700570 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(out->profile);
571 ret = -EINVAL;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800572 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800573
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700574 /* Format */
575 if (config->format == AUDIO_FORMAT_DEFAULT) {
576 proxy_config.format = profile_get_default_format(out->profile);
577 config->format = audio_format_from_pcm_format(proxy_config.format);
578 } else {
579 enum pcm_format fmt = pcm_format_from_audio_format(config->format);
580 if (profile_is_format_valid(out->profile, fmt)) {
581 proxy_config.format = fmt;
582 } else {
583 proxy_config.format = profile_get_default_format(out->profile);
584 config->format = audio_format_from_pcm_format(proxy_config.format);
585 ret = -EINVAL;
586 }
587 }
588
589 /* Channels */
Andy Hung03576be2014-07-21 21:16:45 -0700590 unsigned proposed_channel_count = profile_get_default_channel_count(out->profile);
591 if (k_force_channels) {
592 proposed_channel_count = k_force_channels;
593 } else if (config->channel_mask != AUDIO_CHANNEL_NONE) {
594 proposed_channel_count = audio_channel_count_from_out_mask(config->channel_mask);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700595 }
Andy Hung03576be2014-07-21 21:16:45 -0700596 /* we can expose any channel count mask, and emulate internally. */
597 config->channel_mask = audio_channel_out_mask_from_count(proposed_channel_count);
598 out->hal_channel_count = proposed_channel_count;
599 /* no validity checks are needed as proxy_prepare() forces channel_count to be valid.
600 * and we emulate any channel count discrepancies in out_write(). */
601 proxy_config.channels = proposed_channel_count;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700602
603 proxy_prepare(&out->proxy, out->profile, &proxy_config);
604
605 /* TODO The retry mechanism isn't implemented in AudioPolicyManager/AudioFlinger. */
606 ret = 0;
607
Paul McLeaneedc92e2013-12-19 15:46:15 -0800608 out->conversion_buffer = NULL;
609 out->conversion_buffer_size = 0;
Simon Wilson19957a32012-04-06 16:17:12 -0700610
611 out->standby = true;
612
613 *stream_out = &out->stream;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700614
615 return ret;
Simon Wilson19957a32012-04-06 16:17:12 -0700616
617err_open:
618 free(out);
619 *stream_out = NULL;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800620 return -ENOSYS;
Simon Wilson19957a32012-04-06 16:17:12 -0700621}
622
623static void adev_close_output_stream(struct audio_hw_device *dev,
624 struct audio_stream_out *stream)
625{
626 struct stream_out *out = (struct stream_out *)stream;
Paul McLean65ec72b2015-02-18 09:13:24 -0800627 ALOGV("adev_close_output_stream(c:%d d:%d)", out->profile->card, out->profile->device);
Simon Wilson19957a32012-04-06 16:17:12 -0700628
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700629 /* Close the pcm device */
Simon Wilson19957a32012-04-06 16:17:12 -0700630 out_standby(&stream->common);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800631
632 free(out->conversion_buffer);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700633
Paul McLeaneedc92e2013-12-19 15:46:15 -0800634 out->conversion_buffer = NULL;
635 out->conversion_buffer_size = 0;
636
Simon Wilson19957a32012-04-06 16:17:12 -0700637 free(stream);
638}
639
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700640static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
641 const struct audio_config *config)
642{
643 /* TODO This needs to be calculated based on format/channels/rate */
644 return 320;
645}
646
647/*
648 * IN functions
649 */
650static uint32_t in_get_sample_rate(const struct audio_stream *stream)
651{
652 uint32_t rate = proxy_get_sample_rate(&((const struct stream_in *)stream)->proxy);
653 ALOGV("in_get_sample_rate() = %d", rate);
654 return rate;
655}
656
657static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
658{
659 ALOGV("in_set_sample_rate(%d) - NOPE", rate);
660 return -ENOSYS;
661}
662
663static size_t in_get_buffer_size(const struct audio_stream *stream)
664{
665 const struct stream_in * in = ((const struct stream_in*)stream);
Paul McLean2cfd81b2014-09-15 12:32:23 -0700666 return proxy_get_period_size(&in->proxy) * audio_stream_in_frame_size(&(in->stream));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700667}
668
669static uint32_t in_get_channels(const struct audio_stream *stream)
670{
Paul McLean2cfd81b2014-09-15 12:32:23 -0700671 const struct stream_in *in = (const struct stream_in*)stream;
Andy Hung780f1f82015-04-22 22:14:39 -0700672 return in->hal_channel_mask;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700673}
674
675static audio_format_t in_get_format(const struct audio_stream *stream)
676{
Andy Hung780f1f82015-04-22 22:14:39 -0700677 alsa_device_proxy *proxy = &((struct stream_in*)stream)->proxy;
678 audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy));
679 return format;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700680}
681
682static int in_set_format(struct audio_stream *stream, audio_format_t format)
683{
684 ALOGV("in_set_format(%d) - NOPE", format);
685
686 return -ENOSYS;
687}
688
689static int in_standby(struct audio_stream *stream)
690{
691 struct stream_in *in = (struct stream_in *)stream;
692
693 pthread_mutex_lock(&in->dev->lock);
694 pthread_mutex_lock(&in->lock);
695
696 if (!in->standby) {
697 proxy_close(&in->proxy);
698 in->standby = true;
699 }
700
701 pthread_mutex_unlock(&in->lock);
702 pthread_mutex_unlock(&in->dev->lock);
703
704 return 0;
705}
706
707static int in_dump(const struct audio_stream *stream, int fd)
708{
709 return 0;
710}
711
712static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
713{
Paul McLean65ec72b2015-02-18 09:13:24 -0800714 ALOGV("in_set_parameters() keys:%s", kvpairs);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700715
716 struct stream_in *in = (struct stream_in *)stream;
717
718 char value[32];
719 int param_val;
720 int routing = 0;
721 int ret_value = 0;
Eric Laurent05333d42014-08-04 20:29:17 -0700722 int card = -1;
723 int device = -1;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700724
Paul McLean65ec72b2015-02-18 09:13:24 -0800725 if (!parse_card_device_params(kvpairs, &card, &device)) {
726 // nothing to do
727 return ret_value;
728 }
729
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700730 pthread_mutex_lock(&in->dev->lock);
731 pthread_mutex_lock(&in->lock);
732
Paul McLean2c6196f2014-08-20 16:50:25 -0700733 if (card >= 0 && device >= 0 && !profile_is_cached_for(in->profile, card, device)) {
Eric Laurent05333d42014-08-04 20:29:17 -0700734 /* cannot read pcm device info if playback is active */
735 if (!in->standby)
736 ret_value = -ENOSYS;
737 else {
738 int saved_card = in->profile->card;
739 int saved_device = in->profile->device;
740 in->profile->card = card;
741 in->profile->device = device;
742 ret_value = profile_read_device_info(in->profile) ? 0 : -EINVAL;
743 if (ret_value != 0) {
744 in->profile->card = saved_card;
745 in->profile->device = saved_device;
746 }
747 }
Paul McLean2c6196f2014-08-20 16:50:25 -0700748 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700749
750 pthread_mutex_unlock(&in->lock);
751 pthread_mutex_unlock(&in->dev->lock);
752
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700753 return ret_value;
754}
755
756static char * in_get_parameters(const struct audio_stream *stream, const char *keys)
757{
758 struct stream_in *in = (struct stream_in *)stream;
759
760 pthread_mutex_lock(&in->dev->lock);
761 pthread_mutex_lock(&in->lock);
762
763 char * params_str = device_get_parameters(in->profile, keys);
764
765 pthread_mutex_unlock(&in->lock);
766 pthread_mutex_unlock(&in->dev->lock);
767
768 return params_str;
769}
770
771static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
772{
773 return 0;
774}
775
776static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
777{
778 return 0;
779}
780
781static int in_set_gain(struct audio_stream_in *stream, float gain)
782{
783 return 0;
784}
785
786/* must be called with hw device and output stream mutexes locked */
787static int start_input_stream(struct stream_in *in)
788{
Paul McLean65ec72b2015-02-18 09:13:24 -0800789 ALOGV("ustart_input_stream(card:%d device:%d)", in->profile->card, in->profile->device);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700790
791 return proxy_open(&in->proxy);
792}
793
794/* TODO mutex stuff here (see out_write) */
795static ssize_t in_read(struct audio_stream_in *stream, void* buffer, size_t bytes)
796{
797 size_t num_read_buff_bytes = 0;
798 void * read_buff = buffer;
799 void * out_buff = buffer;
Pavan Chikkala83b47a62015-01-09 12:21:13 -0800800 int ret = 0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700801
802 struct stream_in * in = (struct stream_in *)stream;
803
804 pthread_mutex_lock(&in->dev->lock);
805 pthread_mutex_lock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700806 if (in->standby) {
807 if (start_input_stream(in) != 0) {
Paul McLeanf55fdb22015-02-20 15:39:59 -0800808 pthread_mutex_unlock(&in->dev->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700809 goto err;
810 }
811 in->standby = false;
812 }
Paul McLeanf55fdb22015-02-20 15:39:59 -0800813 pthread_mutex_unlock(&in->dev->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700814
815 alsa_device_profile * profile = in->profile;
816
817 /*
818 * OK, we need to figure out how much data to read to be able to output the requested
819 * number of bytes in the HAL format (16-bit, stereo).
820 */
821 num_read_buff_bytes = bytes;
Andy Hung780f1f82015-04-22 22:14:39 -0700822 int num_device_channels = proxy_get_channel_count(&in->proxy); /* what we told Alsa */
823 int num_req_channels = in->hal_channel_count; /* what we told AudioFlinger */
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700824
825 if (num_device_channels != num_req_channels) {
826 num_read_buff_bytes = (num_device_channels * num_read_buff_bytes) / num_req_channels;
827 }
828
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700829 /* Setup/Realloc the conversion buffer (if necessary). */
830 if (num_read_buff_bytes != bytes) {
831 if (num_read_buff_bytes > in->conversion_buffer_size) {
832 /*TODO Remove this when AudioPolicyManger/AudioFlinger support arbitrary formats
833 (and do these conversions themselves) */
834 in->conversion_buffer_size = num_read_buff_bytes;
835 in->conversion_buffer = realloc(in->conversion_buffer, in->conversion_buffer_size);
836 }
837 read_buff = in->conversion_buffer;
838 }
839
Pavan Chikkala83b47a62015-01-09 12:21:13 -0800840 ret = proxy_read(&in->proxy, read_buff, num_read_buff_bytes);
841 if (ret == 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700842 if (num_device_channels != num_req_channels) {
843 // ALOGV("chans dev:%d req:%d", num_device_channels, num_req_channels);
844
845 out_buff = buffer;
846 /* Num Channels conversion */
847 if (num_device_channels != num_req_channels) {
848 audio_format_t audio_format = in_get_format(&(in->stream.common));
849 unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
850
851 num_read_buff_bytes =
852 adjust_channels(read_buff, num_device_channels,
853 out_buff, num_req_channels,
854 sample_size_in_bytes, num_read_buff_bytes);
855 }
856 }
Eric Laurent253def92014-09-14 12:18:18 -0700857
858 /* no need to acquire in->dev->lock to read mic_muted here as we don't change its state */
859 if (num_read_buff_bytes > 0 && in->dev->mic_muted)
860 memset(buffer, 0, num_read_buff_bytes);
Viswanath L8c7e1112014-10-31 13:07:39 +0530861 } else {
Eric Laurente6499422015-01-09 17:03:27 -0800862 num_read_buff_bytes = 0; // reset the value after USB headset is unplugged
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700863 }
864
865err:
866 pthread_mutex_unlock(&in->lock);
867
868 return num_read_buff_bytes;
869}
870
871static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
872{
873 return 0;
874}
875
876static int adev_open_input_stream(struct audio_hw_device *dev,
877 audio_io_handle_t handle,
878 audio_devices_t devices,
879 struct audio_config *config,
880 struct audio_stream_in **stream_in,
Eric Laurentf5e24692014-07-27 16:14:57 -0700881 audio_input_flags_t flags __unused,
Paul McLean0f1753e2014-12-02 12:36:45 -0700882 const char *address /*__unused*/,
Eric Laurentf5e24692014-07-27 16:14:57 -0700883 audio_source_t source __unused)
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700884{
Paul McLean65ec72b2015-02-18 09:13:24 -0800885 ALOGV("in adev_open_input_stream() rate:%" PRIu32 ", chanMask:0x%" PRIX32 ", fmt:%" PRIu8,
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700886 config->sample_rate, config->channel_mask, config->format);
887
888 struct stream_in *in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
889 int ret = 0;
890
891 if (in == NULL)
892 return -ENOMEM;
893
894 /* setup function pointers */
895 in->stream.common.get_sample_rate = in_get_sample_rate;
896 in->stream.common.set_sample_rate = in_set_sample_rate;
897 in->stream.common.get_buffer_size = in_get_buffer_size;
898 in->stream.common.get_channels = in_get_channels;
899 in->stream.common.get_format = in_get_format;
900 in->stream.common.set_format = in_set_format;
901 in->stream.common.standby = in_standby;
902 in->stream.common.dump = in_dump;
903 in->stream.common.set_parameters = in_set_parameters;
904 in->stream.common.get_parameters = in_get_parameters;
905 in->stream.common.add_audio_effect = in_add_audio_effect;
906 in->stream.common.remove_audio_effect = in_remove_audio_effect;
907
908 in->stream.set_gain = in_set_gain;
909 in->stream.read = in_read;
910 in->stream.get_input_frames_lost = in_get_input_frames_lost;
911
912 in->dev = (struct audio_device *)dev;
Paul McLean0f1753e2014-12-02 12:36:45 -0700913 pthread_mutex_lock(&in->dev->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700914
915 in->profile = &in->dev->in_profile;
916
917 struct pcm_config proxy_config;
Paul McLean2c6196f2014-08-20 16:50:25 -0700918 memset(&proxy_config, 0, sizeof(proxy_config));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700919
Paul McLean0f1753e2014-12-02 12:36:45 -0700920 /* Pull out the card/device pair */
921 parse_card_device_params(address, &(in->profile->card), &(in->profile->device));
922
923 profile_read_device_info(in->profile);
924 pthread_mutex_unlock(&in->dev->lock);
925
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700926 /* 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 */
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700937 if (config->format == AUDIO_FORMAT_DEFAULT) {
Andy Hung780f1f82015-04-22 22:14:39 -0700938 proxy_config.format = profile_get_default_format(in->profile);
939 config->format = audio_format_from_pcm_format(proxy_config.format);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700940 } else {
Andy Hung780f1f82015-04-22 22:14:39 -0700941 enum pcm_format fmt = pcm_format_from_audio_format(config->format);
942 if (profile_is_format_valid(in->profile, fmt)) {
943 proxy_config.format = fmt;
944 } else {
945 proxy_config.format = profile_get_default_format(in->profile);
946 config->format = audio_format_from_pcm_format(proxy_config.format);
947 ret = -EINVAL;
948 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700949 }
950
Paul McLean2cfd81b2014-09-15 12:32:23 -0700951 /* Channels */
Andy Hung780f1f82015-04-22 22:14:39 -0700952 unsigned proposed_channel_count = 0;
Paul McLean2cfd81b2014-09-15 12:32:23 -0700953 if (k_force_channels) {
954 proposed_channel_count = k_force_channels;
Andy Hung780f1f82015-04-22 22:14:39 -0700955 } else if (config->channel_mask == AUDIO_CHANNEL_NONE) {
956 proposed_channel_count = profile_get_default_channel_count(in->profile);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700957 }
Andy Hung780f1f82015-04-22 22:14:39 -0700958 if (proposed_channel_count != 0) {
959 config->channel_mask = audio_channel_in_mask_from_count(proposed_channel_count);
960 if (config->channel_mask == AUDIO_CHANNEL_INVALID)
961 config->channel_mask =
962 audio_channel_mask_for_index_assignment_from_count(proposed_channel_count);
963 in->hal_channel_count = proposed_channel_count;
964 } else {
965 in->hal_channel_count = audio_channel_count_from_in_mask(config->channel_mask);
966 }
967 /* we can expose any channel mask, and emulate internally based on channel count. */
968 in->hal_channel_mask = config->channel_mask;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700969
Paul McLean2cfd81b2014-09-15 12:32:23 -0700970 proxy_config.channels = profile_get_default_channel_count(in->profile);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700971 proxy_prepare(&in->proxy, in->profile, &proxy_config);
972
973 in->standby = true;
974
975 in->conversion_buffer = NULL;
976 in->conversion_buffer_size = 0;
977
978 *stream_in = &in->stream;
979
980 return ret;
981}
982
983static void adev_close_input_stream(struct audio_hw_device *dev, struct audio_stream_in *stream)
984{
985 struct stream_in *in = (struct stream_in *)stream;
986
987 /* Close the pcm device */
988 in_standby(&stream->common);
989
990 free(in->conversion_buffer);
991
992 free(stream);
993}
994
995/*
996 * ADEV Functions
997 */
Simon Wilson19957a32012-04-06 16:17:12 -0700998static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
999{
1000 return 0;
1001}
1002
Paul McLean30f41852014-04-16 15:44:20 -07001003static char * adev_get_parameters(const struct audio_hw_device *dev, const char *keys)
Simon Wilson19957a32012-04-06 16:17:12 -07001004{
1005 return strdup("");
1006}
1007
1008static int adev_init_check(const struct audio_hw_device *dev)
1009{
1010 return 0;
1011}
1012
1013static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
1014{
1015 return -ENOSYS;
1016}
1017
1018static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
1019{
1020 return -ENOSYS;
1021}
1022
1023static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
1024{
1025 return 0;
1026}
1027
1028static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
1029{
Eric Laurent253def92014-09-14 12:18:18 -07001030 struct audio_device * adev = (struct audio_device *)dev;
1031 pthread_mutex_lock(&adev->lock);
1032 adev->mic_muted = state;
1033 pthread_mutex_unlock(&adev->lock);
Simon Wilson19957a32012-04-06 16:17:12 -07001034 return -ENOSYS;
1035}
1036
1037static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
1038{
1039 return -ENOSYS;
1040}
1041
Simon Wilson19957a32012-04-06 16:17:12 -07001042static int adev_dump(const audio_hw_device_t *device, int fd)
1043{
1044 return 0;
1045}
1046
1047static int adev_close(hw_device_t *device)
1048{
Paul McLeaneedc92e2013-12-19 15:46:15 -08001049 struct audio_device *adev = (struct audio_device *)device;
Simon Wilson19957a32012-04-06 16:17:12 -07001050 free(device);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001051
Simon Wilson19957a32012-04-06 16:17:12 -07001052 return 0;
1053}
1054
Paul McLean30f41852014-04-16 15:44:20 -07001055static int adev_open(const hw_module_t* module, const char* name, hw_device_t** device)
Simon Wilson19957a32012-04-06 16:17:12 -07001056{
Simon Wilson19957a32012-04-06 16:17:12 -07001057 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
1058 return -EINVAL;
1059
Paul McLeaneedc92e2013-12-19 15:46:15 -08001060 struct audio_device *adev = calloc(1, sizeof(struct audio_device));
Simon Wilson19957a32012-04-06 16:17:12 -07001061 if (!adev)
1062 return -ENOMEM;
1063
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001064 profile_init(&adev->out_profile, PCM_OUT);
1065 profile_init(&adev->in_profile, PCM_IN);
1066
Simon Wilson19957a32012-04-06 16:17:12 -07001067 adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
Eric Laurent85e08e22012-08-28 14:30:35 -07001068 adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001069 adev->hw_device.common.module = (struct hw_module_t *)module;
Simon Wilson19957a32012-04-06 16:17:12 -07001070 adev->hw_device.common.close = adev_close;
1071
Simon Wilson19957a32012-04-06 16:17:12 -07001072 adev->hw_device.init_check = adev_init_check;
1073 adev->hw_device.set_voice_volume = adev_set_voice_volume;
1074 adev->hw_device.set_master_volume = adev_set_master_volume;
1075 adev->hw_device.set_mode = adev_set_mode;
1076 adev->hw_device.set_mic_mute = adev_set_mic_mute;
1077 adev->hw_device.get_mic_mute = adev_get_mic_mute;
1078 adev->hw_device.set_parameters = adev_set_parameters;
1079 adev->hw_device.get_parameters = adev_get_parameters;
1080 adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size;
1081 adev->hw_device.open_output_stream = adev_open_output_stream;
1082 adev->hw_device.close_output_stream = adev_close_output_stream;
1083 adev->hw_device.open_input_stream = adev_open_input_stream;
1084 adev->hw_device.close_input_stream = adev_close_input_stream;
1085 adev->hw_device.dump = adev_dump;
1086
1087 *device = &adev->hw_device.common;
1088
1089 return 0;
1090}
1091
1092static struct hw_module_methods_t hal_module_methods = {
1093 .open = adev_open,
1094};
1095
1096struct audio_module HAL_MODULE_INFO_SYM = {
1097 .common = {
1098 .tag = HARDWARE_MODULE_TAG,
Mike Lockwood46a98092012-04-24 16:41:18 -07001099 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
1100 .hal_api_version = HARDWARE_HAL_API_VERSION,
Simon Wilson19957a32012-04-06 16:17:12 -07001101 .id = AUDIO_HARDWARE_MODULE_ID,
1102 .name = "USB audio HW HAL",
1103 .author = "The Android Open Source Project",
1104 .methods = &hal_module_methods,
1105 },
1106};