blob: 84cc643094f630b852167e5ed3ff4e5ba73f2c37 [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. */
Andy Hung182ddc72015-05-05 23:37:30 -070093 audio_channel_mask_t hal_channel_mask; /* channel mask exposed to AudioFlinger. */
94
Paul McLeaneedc92e2013-12-19 15:46:15 -080095 void * conversion_buffer; /* any conversions are put into here
96 * they could come from here too if
97 * there was a previous conversion */
98 size_t conversion_buffer_size; /* in bytes */
99};
100
Paul McLeaneedc92e2013-12-19 15:46:15 -0800101struct stream_in {
102 struct audio_stream_in stream;
103
Simon Wilson19957a32012-04-06 16:17:12 -0700104 pthread_mutex_t lock; /* see note below on mutex acquisition order */
Simon Wilson19957a32012-04-06 16:17:12 -0700105 bool standby;
106
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700107 struct audio_device *dev; /* hardware information - only using this for the lock */
Paul McLeaneedc92e2013-12-19 15:46:15 -0800108
Paul McLean65ec72b2015-02-18 09:13:24 -0800109 alsa_device_profile * profile; /* Points to the alsa_device_profile in the audio_device */
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700110 alsa_device_proxy proxy; /* state of the stream */
Paul McLeanf62d75e2014-07-11 15:14:19 -0700111
Paul McLean2cfd81b2014-09-15 12:32:23 -0700112 unsigned hal_channel_count; /* channel count exposed to AudioFlinger.
113 * This may differ from the device channel count when
114 * the device is not compatible with AudioFlinger
115 * capabilities, e.g. exposes too many channels or
116 * too few channels. */
Andy Hung780f1f82015-04-22 22:14:39 -0700117 audio_channel_mask_t hal_channel_mask; /* channel mask exposed to AudioFlinger. */
118
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700119 /* 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 -0700120 void * conversion_buffer; /* any conversions are put into here
121 * they could come from here too if
122 * there was a previous conversion */
123 size_t conversion_buffer_size; /* in bytes */
Simon Wilson19957a32012-04-06 16:17:12 -0700124};
125
Paul McLeaneedc92e2013-12-19 15:46:15 -0800126/*
Paul McLeaneedc92e2013-12-19 15:46:15 -0800127 * Data Conversions
128 */
129/*
Paul McLean30f41852014-04-16 15:44:20 -0700130 * Convert a buffer of packed (3-byte) PCM24LE samples to PCM16LE samples.
131 * in_buff points to the buffer of PCM24LE samples
Paul McLeaneedc92e2013-12-19 15:46:15 -0800132 * num_in_samples size of input buffer in SAMPLES
Paul McLean30f41852014-04-16 15:44:20 -0700133 * out_buff points to the buffer to receive converted PCM16LE LE samples.
134 * returns
135 * the number of BYTES of output data.
136 * We are doing this since we *always* present to The Framework as A PCM16LE device, but need to
137 * support PCM24_3LE (24-bit, packed).
138 * NOTE:
Paul McLean30f41852014-04-16 15:44:20 -0700139 * This conversion is safe to do in-place (in_buff == out_buff).
Paul McLeane32cbc12014-06-25 10:42:07 -0700140 * TODO Move this to a utilities module.
Paul McLean30f41852014-04-16 15:44:20 -0700141 */
Paul McLeane32cbc12014-06-25 10:42:07 -0700142static size_t convert_24_3_to_16(const unsigned char * in_buff, size_t num_in_samples,
143 short * out_buff)
144{
Paul McLean30f41852014-04-16 15:44:20 -0700145 /*
146 * Move from front to back so that the conversion can be done in-place
147 * i.e. in_buff == out_buff
148 */
149 /* we need 2 bytes in the output for every 3 bytes in the input */
150 unsigned char* dst_ptr = (unsigned char*)out_buff;
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700151 const unsigned char* src_ptr = in_buff;
Paul McLean30f41852014-04-16 15:44:20 -0700152 size_t src_smpl_index;
153 for (src_smpl_index = 0; src_smpl_index < num_in_samples; src_smpl_index++) {
154 src_ptr++; /* lowest-(skip)-byte */
155 *dst_ptr++ = *src_ptr++; /* low-byte */
156 *dst_ptr++ = *src_ptr++; /* high-byte */
157 }
158
159 /* return number of *bytes* generated: */
160 return num_in_samples * 2;
161}
162
163/*
Paul McLean6b1c0fe2014-07-02 07:27:41 -0700164 * Convert a buffer of packed (3-byte) PCM32 samples to PCM16LE samples.
165 * in_buff points to the buffer of PCM32 samples
166 * num_in_samples size of input buffer in SAMPLES
167 * out_buff points to the buffer to receive converted PCM16LE LE samples.
168 * returns
169 * the number of BYTES of output data.
170 * We are doing this since we *always* present to The Framework as A PCM16LE device, but need to
171 * support PCM_FORMAT_S32_LE (32-bit).
172 * NOTE:
173 * This conversion is safe to do in-place (in_buff == out_buff).
174 * TODO Move this to a utilities module.
175 */
176static size_t convert_32_to_16(const int32_t * in_buff, size_t num_in_samples, short * out_buff)
177{
178 /*
179 * Move from front to back so that the conversion can be done in-place
180 * i.e. in_buff == out_buff
181 */
182
183 short * dst_ptr = out_buff;
184 const int32_t* src_ptr = in_buff;
185 size_t src_smpl_index;
186 for (src_smpl_index = 0; src_smpl_index < num_in_samples; src_smpl_index++) {
187 *dst_ptr++ = *src_ptr++ >> 16;
188 }
189
190 /* return number of *bytes* generated: */
191 return num_in_samples * 2;
192}
193
Paul McLean0f1753e2014-12-02 12:36:45 -0700194/*
195 * Extract the card and device numbers from the supplied key/value pairs.
196 * kvpairs A null-terminated string containing the key/value pairs or card and device.
197 * i.e. "card=1;device=42"
198 * card A pointer to a variable to receive the parsed-out card number.
199 * device A pointer to a variable to receive the parsed-out device number.
200 * NOTE: The variables pointed to by card and device return -1 (undefined) if the
201 * associated key/value pair is not found in the provided string.
Paul McLean65ec72b2015-02-18 09:13:24 -0800202 * Return true if the kvpairs string contain a card/device spec, false otherwise.
Paul McLean0f1753e2014-12-02 12:36:45 -0700203 */
Paul McLean65ec72b2015-02-18 09:13:24 -0800204static bool parse_card_device_params(const char *kvpairs, int *card, int *device)
Paul McLean0f1753e2014-12-02 12:36:45 -0700205{
206 struct str_parms * parms = str_parms_create_str(kvpairs);
207 char value[32];
208 int param_val;
209
210 // initialize to "undefined" state.
211 *card = -1;
212 *device = -1;
213
214 param_val = str_parms_get_str(parms, "card", value, sizeof(value));
215 if (param_val >= 0) {
216 *card = atoi(value);
217 }
218
219 param_val = str_parms_get_str(parms, "device", value, sizeof(value));
220 if (param_val >= 0) {
221 *device = atoi(value);
222 }
223
224 str_parms_destroy(parms);
Paul McLean65ec72b2015-02-18 09:13:24 -0800225
226 return *card >= 0 && *device >= 0;
Paul McLean0f1753e2014-12-02 12:36:45 -0700227}
228
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700229static char * device_get_parameters(alsa_device_profile * profile, const char * keys)
Paul McLeaneedc92e2013-12-19 15:46:15 -0800230{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700231 if (profile->card < 0 || profile->device < 0) {
232 return strdup("");
Paul McLeaneedc92e2013-12-19 15:46:15 -0800233 }
Paul McLeane32cbc12014-06-25 10:42:07 -0700234
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700235 struct str_parms *query = str_parms_create_str(keys);
236 struct str_parms *result = str_parms_create();
Paul McLeane32cbc12014-06-25 10:42:07 -0700237
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700238 /* These keys are from hardware/libhardware/include/audio.h */
239 /* supported sample rates */
240 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) {
241 char* rates_list = profile_get_sample_rate_strs(profile);
242 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES,
243 rates_list);
244 free(rates_list);
Paul McLeane32cbc12014-06-25 10:42:07 -0700245 }
246
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700247 /* supported channel counts */
248 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) {
249 char* channels_list = profile_get_channel_count_strs(profile);
250 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_CHANNELS,
251 channels_list);
252 free(channels_list);
Paul McLeane32cbc12014-06-25 10:42:07 -0700253 }
254
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700255 /* supported sample formats */
256 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) {
257 char * format_params = profile_get_format_strs(profile);
258 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_FORMATS,
259 format_params);
260 free(format_params);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700261 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700262 str_parms_destroy(query);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700263
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700264 char* result_str = str_parms_to_str(result);
265 str_parms_destroy(result);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700266
Paul McLean65ec72b2015-02-18 09:13:24 -0800267 ALOGV("device_get_parameters = %s", result_str);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700268
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700269 return result_str;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800270}
271
272/*
273 * HAl Functions
274 */
Simon Wilson19957a32012-04-06 16:17:12 -0700275/**
276 * NOTE: when multiple mutexes have to be acquired, always respect the
277 * following order: hw device > out stream
278 */
279
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700280/*
281 * OUT functions
282 */
Simon Wilson19957a32012-04-06 16:17:12 -0700283static uint32_t out_get_sample_rate(const struct audio_stream *stream)
284{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700285 uint32_t rate = proxy_get_sample_rate(&((struct stream_out*)stream)->proxy);
286 ALOGV("out_get_sample_rate() = %d", rate);
287 return rate;
Simon Wilson19957a32012-04-06 16:17:12 -0700288}
289
290static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
291{
292 return 0;
293}
294
295static size_t out_get_buffer_size(const struct audio_stream *stream)
296{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700297 const struct stream_out* out = (const struct stream_out*)stream;
298 size_t buffer_size =
299 proxy_get_period_size(&out->proxy) * audio_stream_out_frame_size(&(out->stream));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700300 return buffer_size;
Simon Wilson19957a32012-04-06 16:17:12 -0700301}
302
303static uint32_t out_get_channels(const struct audio_stream *stream)
304{
Andy Hung03576be2014-07-21 21:16:45 -0700305 const struct stream_out *out = (const struct stream_out*)stream;
Andy Hung182ddc72015-05-05 23:37:30 -0700306 return out->hal_channel_mask;
Simon Wilson19957a32012-04-06 16:17:12 -0700307}
308
309static audio_format_t out_get_format(const struct audio_stream *stream)
310{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700311 /* Note: The HAL doesn't do any FORMAT conversion at this time. It
312 * Relies on the framework to provide data in the specified format.
313 * This could change in the future.
314 */
315 alsa_device_proxy * proxy = &((struct stream_out*)stream)->proxy;
316 audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700317 return format;
Simon Wilson19957a32012-04-06 16:17:12 -0700318}
319
320static int out_set_format(struct audio_stream *stream, audio_format_t format)
321{
322 return 0;
323}
324
325static int out_standby(struct audio_stream *stream)
326{
327 struct stream_out *out = (struct stream_out *)stream;
328
329 pthread_mutex_lock(&out->dev->lock);
330 pthread_mutex_lock(&out->lock);
331
332 if (!out->standby) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700333 proxy_close(&out->proxy);
Simon Wilson19957a32012-04-06 16:17:12 -0700334 out->standby = true;
335 }
336
337 pthread_mutex_unlock(&out->lock);
338 pthread_mutex_unlock(&out->dev->lock);
339
340 return 0;
341}
342
343static int out_dump(const struct audio_stream *stream, int fd)
344{
345 return 0;
346}
347
348static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
349{
Paul McLean65ec72b2015-02-18 09:13:24 -0800350 ALOGV("out_set_parameters() keys:%s", kvpairs);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800351
Simon Wilson19957a32012-04-06 16:17:12 -0700352 struct stream_out *out = (struct stream_out *)stream;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700353
Simon Wilson19957a32012-04-06 16:17:12 -0700354 int routing = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800355 int ret_value = 0;
Eric Laurent05333d42014-08-04 20:29:17 -0700356 int card = -1;
357 int device = -1;
Simon Wilson19957a32012-04-06 16:17:12 -0700358
Paul McLean65ec72b2015-02-18 09:13:24 -0800359 if (!parse_card_device_params(kvpairs, &card, &device)) {
360 // nothing to do
361 return ret_value;
362 }
363
364 /* Lock the device because that is where the profile lives */
Paul McLeanf62d75e2014-07-11 15:14:19 -0700365 pthread_mutex_lock(&out->dev->lock);
366 pthread_mutex_lock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700367
Paul McLean65ec72b2015-02-18 09:13:24 -0800368 if (!profile_is_cached_for(out->profile, card, device)) {
Eric Laurent05333d42014-08-04 20:29:17 -0700369 /* cannot read pcm device info if playback is active */
370 if (!out->standby)
371 ret_value = -ENOSYS;
372 else {
373 int saved_card = out->profile->card;
374 int saved_device = out->profile->device;
375 out->profile->card = card;
376 out->profile->device = device;
377 ret_value = profile_read_device_info(out->profile) ? 0 : -EINVAL;
378 if (ret_value != 0) {
379 out->profile->card = saved_card;
380 out->profile->device = saved_device;
381 }
382 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800383 }
Paul McLean2c6196f2014-08-20 16:50:25 -0700384
Paul McLeanf62d75e2014-07-11 15:14:19 -0700385 pthread_mutex_unlock(&out->lock);
386 pthread_mutex_unlock(&out->dev->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700387
Paul McLeaneedc92e2013-12-19 15:46:15 -0800388 return ret_value;
Simon Wilson19957a32012-04-06 16:17:12 -0700389}
390
Paul McLeanf62d75e2014-07-11 15:14:19 -0700391static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
392{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700393 struct stream_out *out = (struct stream_out *)stream;
Paul McLeanf62d75e2014-07-11 15:14:19 -0700394 pthread_mutex_lock(&out->dev->lock);
395 pthread_mutex_lock(&out->lock);
396
397 char * params_str = device_get_parameters(out->profile, keys);
398
399 pthread_mutex_unlock(&out->lock);
400 pthread_mutex_unlock(&out->dev->lock);
401
402 return params_str;
403}
404
Simon Wilson19957a32012-04-06 16:17:12 -0700405static uint32_t out_get_latency(const struct audio_stream_out *stream)
406{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700407 alsa_device_proxy * proxy = &((struct stream_out*)stream)->proxy;
408 return proxy_get_latency(proxy);
Simon Wilson19957a32012-04-06 16:17:12 -0700409}
410
Paul McLean30f41852014-04-16 15:44:20 -0700411static int out_set_volume(struct audio_stream_out *stream, float left, float right)
Simon Wilson19957a32012-04-06 16:17:12 -0700412{
413 return -ENOSYS;
414}
415
Paul McLeaneedc92e2013-12-19 15:46:15 -0800416/* must be called with hw device and output stream mutexes locked */
417static int start_output_stream(struct stream_out *out)
418{
Paul McLean65ec72b2015-02-18 09:13:24 -0800419 ALOGV("start_output_stream(card:%d device:%d)", out->profile->card, out->profile->device);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800420
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700421 return proxy_open(&out->proxy);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800422}
423
424static ssize_t out_write(struct audio_stream_out *stream, const void* buffer, size_t bytes)
Simon Wilson19957a32012-04-06 16:17:12 -0700425{
426 int ret;
427 struct stream_out *out = (struct stream_out *)stream;
428
429 pthread_mutex_lock(&out->dev->lock);
430 pthread_mutex_lock(&out->lock);
431 if (out->standby) {
432 ret = start_output_stream(out);
433 if (ret != 0) {
Paul McLeanf55fdb22015-02-20 15:39:59 -0800434 pthread_mutex_unlock(&out->dev->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700435 goto err;
436 }
437 out->standby = false;
438 }
Paul McLeanf55fdb22015-02-20 15:39:59 -0800439 pthread_mutex_unlock(&out->dev->lock);
Eric Laurent05333d42014-08-04 20:29:17 -0700440
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700441 alsa_device_proxy* proxy = &out->proxy;
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700442 const void * write_buff = buffer;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800443 int num_write_buff_bytes = bytes;
Andy Hung03576be2014-07-21 21:16:45 -0700444 const int num_device_channels = proxy_get_channel_count(proxy); /* what we told alsa */
445 const int num_req_channels = out->hal_channel_count; /* what we told AudioFlinger */
Paul McLean30f41852014-04-16 15:44:20 -0700446 if (num_device_channels != num_req_channels) {
Andy Hung03576be2014-07-21 21:16:45 -0700447 /* allocate buffer */
448 const size_t required_conversion_buffer_size =
449 bytes * num_device_channels / num_req_channels;
450 if (required_conversion_buffer_size > out->conversion_buffer_size) {
451 out->conversion_buffer_size = required_conversion_buffer_size;
452 out->conversion_buffer = realloc(out->conversion_buffer,
453 out->conversion_buffer_size);
454 }
455 /* convert data */
456 const audio_format_t audio_format = out_get_format(&(out->stream.common));
457 const unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800458 num_write_buff_bytes =
Andy Hung03576be2014-07-21 21:16:45 -0700459 adjust_channels(write_buff, num_req_channels,
460 out->conversion_buffer, num_device_channels,
461 sample_size_in_bytes, num_write_buff_bytes);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800462 write_buff = out->conversion_buffer;
463 }
464
Paul McLeaneedc92e2013-12-19 15:46:15 -0800465 if (write_buff != NULL && num_write_buff_bytes != 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700466 proxy_write(&out->proxy, write_buff, num_write_buff_bytes);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800467 }
Simon Wilson19957a32012-04-06 16:17:12 -0700468
469 pthread_mutex_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700470
471 return bytes;
472
473err:
474 pthread_mutex_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700475 if (ret != 0) {
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700476 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
Simon Wilson19957a32012-04-06 16:17:12 -0700477 out_get_sample_rate(&stream->common));
478 }
479
480 return bytes;
481}
482
Paul McLean30f41852014-04-16 15:44:20 -0700483static int out_get_render_position(const struct audio_stream_out *stream, uint32_t *dsp_frames)
Simon Wilson19957a32012-04-06 16:17:12 -0700484{
485 return -EINVAL;
486}
487
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700488static int out_get_presentation_position(const struct audio_stream_out *stream,
489 uint64_t *frames, struct timespec *timestamp)
490{
491 /* FIXME - This needs to be implemented */
492 return -EINVAL;
493}
494
Simon Wilson19957a32012-04-06 16:17:12 -0700495static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
496{
497 return 0;
498}
499
500static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
501{
502 return 0;
503}
504
Paul McLean30f41852014-04-16 15:44:20 -0700505static int out_get_next_write_timestamp(const struct audio_stream_out *stream, int64_t *timestamp)
Simon Wilson19957a32012-04-06 16:17:12 -0700506{
507 return -EINVAL;
508}
509
510static int adev_open_output_stream(struct audio_hw_device *dev,
Mike Lockwood46a98092012-04-24 16:41:18 -0700511 audio_io_handle_t handle,
512 audio_devices_t devices,
513 audio_output_flags_t flags,
514 struct audio_config *config,
Eric Laurentf5e24692014-07-27 16:14:57 -0700515 struct audio_stream_out **stream_out,
Paul McLean0f1753e2014-12-02 12:36:45 -0700516 const char *address /*__unused*/)
Simon Wilson19957a32012-04-06 16:17:12 -0700517{
Paul McLean65ec72b2015-02-18 09:13:24 -0800518 ALOGV("adev_open_output_stream() handle:0x%X, device:0x%X, flags:0x%X, addr:%s",
Paul McLean0f1753e2014-12-02 12:36:45 -0700519 handle, devices, flags, address);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800520
Simon Wilson19957a32012-04-06 16:17:12 -0700521 struct audio_device *adev = (struct audio_device *)dev;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800522
Simon Wilson19957a32012-04-06 16:17:12 -0700523 struct stream_out *out;
Simon Wilson19957a32012-04-06 16:17:12 -0700524
525 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
526 if (!out)
527 return -ENOMEM;
528
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700529 /* setup function pointers */
Simon Wilson19957a32012-04-06 16:17:12 -0700530 out->stream.common.get_sample_rate = out_get_sample_rate;
531 out->stream.common.set_sample_rate = out_set_sample_rate;
532 out->stream.common.get_buffer_size = out_get_buffer_size;
533 out->stream.common.get_channels = out_get_channels;
534 out->stream.common.get_format = out_get_format;
535 out->stream.common.set_format = out_set_format;
536 out->stream.common.standby = out_standby;
537 out->stream.common.dump = out_dump;
538 out->stream.common.set_parameters = out_set_parameters;
539 out->stream.common.get_parameters = out_get_parameters;
540 out->stream.common.add_audio_effect = out_add_audio_effect;
541 out->stream.common.remove_audio_effect = out_remove_audio_effect;
542 out->stream.get_latency = out_get_latency;
543 out->stream.set_volume = out_set_volume;
544 out->stream.write = out_write;
545 out->stream.get_render_position = out_get_render_position;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700546 out->stream.get_presentation_position = out_get_presentation_position;
Simon Wilson19957a32012-04-06 16:17:12 -0700547 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
548
549 out->dev = adev;
Paul McLean0f1753e2014-12-02 12:36:45 -0700550 pthread_mutex_lock(&adev->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700551 out->profile = &adev->out_profile;
Paul McLeanf62d75e2014-07-11 15:14:19 -0700552
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700553 // build this to hand to the alsa_device_proxy
554 struct pcm_config proxy_config;
Paul McLean2c6196f2014-08-20 16:50:25 -0700555 memset(&proxy_config, 0, sizeof(proxy_config));
Paul McLeaneedc92e2013-12-19 15:46:15 -0800556
Paul McLean0f1753e2014-12-02 12:36:45 -0700557 /* Pull out the card/device pair */
558 parse_card_device_params(address, &(out->profile->card), &(out->profile->device));
559
560 profile_read_device_info(out->profile);
561
562 pthread_mutex_unlock(&adev->lock);
563
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700564 int ret = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800565
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700566 /* Rate */
567 if (config->sample_rate == 0) {
568 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(out->profile);
569 } else if (profile_is_sample_rate_valid(out->profile, config->sample_rate)) {
570 proxy_config.rate = config->sample_rate;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800571 } else {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700572 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(out->profile);
573 ret = -EINVAL;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800574 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800575
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700576 /* Format */
577 if (config->format == AUDIO_FORMAT_DEFAULT) {
578 proxy_config.format = profile_get_default_format(out->profile);
579 config->format = audio_format_from_pcm_format(proxy_config.format);
580 } else {
581 enum pcm_format fmt = pcm_format_from_audio_format(config->format);
582 if (profile_is_format_valid(out->profile, fmt)) {
583 proxy_config.format = fmt;
584 } else {
585 proxy_config.format = profile_get_default_format(out->profile);
586 config->format = audio_format_from_pcm_format(proxy_config.format);
587 ret = -EINVAL;
588 }
589 }
590
591 /* Channels */
Andy Hung182ddc72015-05-05 23:37:30 -0700592 unsigned proposed_channel_count = 0;
Andy Hung03576be2014-07-21 21:16:45 -0700593 if (k_force_channels) {
594 proposed_channel_count = k_force_channels;
Andy Hung182ddc72015-05-05 23:37:30 -0700595 } else if (config->channel_mask == AUDIO_CHANNEL_NONE) {
596 proposed_channel_count = profile_get_default_channel_count(out->profile);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700597 }
Andy Hung182ddc72015-05-05 23:37:30 -0700598 if (proposed_channel_count != 0) {
599 config->channel_mask = audio_channel_out_mask_from_count(proposed_channel_count);
600 if (config->channel_mask == AUDIO_CHANNEL_INVALID)
601 config->channel_mask =
602 audio_channel_mask_for_index_assignment_from_count(proposed_channel_count);
603 out->hal_channel_count = proposed_channel_count;
604 } else {
605 out->hal_channel_count = audio_channel_count_from_out_mask(config->channel_mask);
606 }
607 /* we can expose any channel mask, and emulate internally based on channel count. */
608 out->hal_channel_mask = config->channel_mask;
609
Andy Hung03576be2014-07-21 21:16:45 -0700610 /* no validity checks are needed as proxy_prepare() forces channel_count to be valid.
611 * and we emulate any channel count discrepancies in out_write(). */
612 proxy_config.channels = proposed_channel_count;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700613
614 proxy_prepare(&out->proxy, out->profile, &proxy_config);
615
616 /* TODO The retry mechanism isn't implemented in AudioPolicyManager/AudioFlinger. */
617 ret = 0;
618
Paul McLeaneedc92e2013-12-19 15:46:15 -0800619 out->conversion_buffer = NULL;
620 out->conversion_buffer_size = 0;
Simon Wilson19957a32012-04-06 16:17:12 -0700621
622 out->standby = true;
623
624 *stream_out = &out->stream;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700625
626 return ret;
Simon Wilson19957a32012-04-06 16:17:12 -0700627
628err_open:
629 free(out);
630 *stream_out = NULL;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800631 return -ENOSYS;
Simon Wilson19957a32012-04-06 16:17:12 -0700632}
633
634static void adev_close_output_stream(struct audio_hw_device *dev,
635 struct audio_stream_out *stream)
636{
637 struct stream_out *out = (struct stream_out *)stream;
Paul McLean65ec72b2015-02-18 09:13:24 -0800638 ALOGV("adev_close_output_stream(c:%d d:%d)", out->profile->card, out->profile->device);
Simon Wilson19957a32012-04-06 16:17:12 -0700639
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700640 /* Close the pcm device */
Simon Wilson19957a32012-04-06 16:17:12 -0700641 out_standby(&stream->common);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800642
643 free(out->conversion_buffer);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700644
Paul McLeaneedc92e2013-12-19 15:46:15 -0800645 out->conversion_buffer = NULL;
646 out->conversion_buffer_size = 0;
647
Simon Wilson19957a32012-04-06 16:17:12 -0700648 free(stream);
649}
650
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700651static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
652 const struct audio_config *config)
653{
654 /* TODO This needs to be calculated based on format/channels/rate */
655 return 320;
656}
657
658/*
659 * IN functions
660 */
661static uint32_t in_get_sample_rate(const struct audio_stream *stream)
662{
663 uint32_t rate = proxy_get_sample_rate(&((const struct stream_in *)stream)->proxy);
664 ALOGV("in_get_sample_rate() = %d", rate);
665 return rate;
666}
667
668static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
669{
670 ALOGV("in_set_sample_rate(%d) - NOPE", rate);
671 return -ENOSYS;
672}
673
674static size_t in_get_buffer_size(const struct audio_stream *stream)
675{
676 const struct stream_in * in = ((const struct stream_in*)stream);
Paul McLean2cfd81b2014-09-15 12:32:23 -0700677 return proxy_get_period_size(&in->proxy) * audio_stream_in_frame_size(&(in->stream));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700678}
679
680static uint32_t in_get_channels(const struct audio_stream *stream)
681{
Paul McLean2cfd81b2014-09-15 12:32:23 -0700682 const struct stream_in *in = (const struct stream_in*)stream;
Andy Hung780f1f82015-04-22 22:14:39 -0700683 return in->hal_channel_mask;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700684}
685
686static audio_format_t in_get_format(const struct audio_stream *stream)
687{
Andy Hung780f1f82015-04-22 22:14:39 -0700688 alsa_device_proxy *proxy = &((struct stream_in*)stream)->proxy;
689 audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy));
690 return format;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700691}
692
693static int in_set_format(struct audio_stream *stream, audio_format_t format)
694{
695 ALOGV("in_set_format(%d) - NOPE", format);
696
697 return -ENOSYS;
698}
699
700static int in_standby(struct audio_stream *stream)
701{
702 struct stream_in *in = (struct stream_in *)stream;
703
704 pthread_mutex_lock(&in->dev->lock);
705 pthread_mutex_lock(&in->lock);
706
707 if (!in->standby) {
708 proxy_close(&in->proxy);
709 in->standby = true;
710 }
711
712 pthread_mutex_unlock(&in->lock);
713 pthread_mutex_unlock(&in->dev->lock);
714
715 return 0;
716}
717
718static int in_dump(const struct audio_stream *stream, int fd)
719{
720 return 0;
721}
722
723static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
724{
Paul McLean65ec72b2015-02-18 09:13:24 -0800725 ALOGV("in_set_parameters() keys:%s", kvpairs);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700726
727 struct stream_in *in = (struct stream_in *)stream;
728
729 char value[32];
730 int param_val;
731 int routing = 0;
732 int ret_value = 0;
Eric Laurent05333d42014-08-04 20:29:17 -0700733 int card = -1;
734 int device = -1;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700735
Paul McLean65ec72b2015-02-18 09:13:24 -0800736 if (!parse_card_device_params(kvpairs, &card, &device)) {
737 // nothing to do
738 return ret_value;
739 }
740
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700741 pthread_mutex_lock(&in->dev->lock);
742 pthread_mutex_lock(&in->lock);
743
Paul McLean2c6196f2014-08-20 16:50:25 -0700744 if (card >= 0 && device >= 0 && !profile_is_cached_for(in->profile, card, device)) {
Eric Laurent05333d42014-08-04 20:29:17 -0700745 /* cannot read pcm device info if playback is active */
746 if (!in->standby)
747 ret_value = -ENOSYS;
748 else {
749 int saved_card = in->profile->card;
750 int saved_device = in->profile->device;
751 in->profile->card = card;
752 in->profile->device = device;
753 ret_value = profile_read_device_info(in->profile) ? 0 : -EINVAL;
754 if (ret_value != 0) {
755 in->profile->card = saved_card;
756 in->profile->device = saved_device;
757 }
758 }
Paul McLean2c6196f2014-08-20 16:50:25 -0700759 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700760
761 pthread_mutex_unlock(&in->lock);
762 pthread_mutex_unlock(&in->dev->lock);
763
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700764 return ret_value;
765}
766
767static char * in_get_parameters(const struct audio_stream *stream, const char *keys)
768{
769 struct stream_in *in = (struct stream_in *)stream;
770
771 pthread_mutex_lock(&in->dev->lock);
772 pthread_mutex_lock(&in->lock);
773
774 char * params_str = device_get_parameters(in->profile, keys);
775
776 pthread_mutex_unlock(&in->lock);
777 pthread_mutex_unlock(&in->dev->lock);
778
779 return params_str;
780}
781
782static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
783{
784 return 0;
785}
786
787static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
788{
789 return 0;
790}
791
792static int in_set_gain(struct audio_stream_in *stream, float gain)
793{
794 return 0;
795}
796
797/* must be called with hw device and output stream mutexes locked */
798static int start_input_stream(struct stream_in *in)
799{
Paul McLean65ec72b2015-02-18 09:13:24 -0800800 ALOGV("ustart_input_stream(card:%d device:%d)", in->profile->card, in->profile->device);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700801
802 return proxy_open(&in->proxy);
803}
804
805/* TODO mutex stuff here (see out_write) */
806static ssize_t in_read(struct audio_stream_in *stream, void* buffer, size_t bytes)
807{
808 size_t num_read_buff_bytes = 0;
809 void * read_buff = buffer;
810 void * out_buff = buffer;
Pavan Chikkala83b47a62015-01-09 12:21:13 -0800811 int ret = 0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700812
813 struct stream_in * in = (struct stream_in *)stream;
814
815 pthread_mutex_lock(&in->dev->lock);
816 pthread_mutex_lock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700817 if (in->standby) {
818 if (start_input_stream(in) != 0) {
Paul McLeanf55fdb22015-02-20 15:39:59 -0800819 pthread_mutex_unlock(&in->dev->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700820 goto err;
821 }
822 in->standby = false;
823 }
Paul McLeanf55fdb22015-02-20 15:39:59 -0800824 pthread_mutex_unlock(&in->dev->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700825
826 alsa_device_profile * profile = in->profile;
827
828 /*
829 * OK, we need to figure out how much data to read to be able to output the requested
830 * number of bytes in the HAL format (16-bit, stereo).
831 */
832 num_read_buff_bytes = bytes;
Andy Hung780f1f82015-04-22 22:14:39 -0700833 int num_device_channels = proxy_get_channel_count(&in->proxy); /* what we told Alsa */
834 int num_req_channels = in->hal_channel_count; /* what we told AudioFlinger */
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700835
836 if (num_device_channels != num_req_channels) {
837 num_read_buff_bytes = (num_device_channels * num_read_buff_bytes) / num_req_channels;
838 }
839
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700840 /* Setup/Realloc the conversion buffer (if necessary). */
841 if (num_read_buff_bytes != bytes) {
842 if (num_read_buff_bytes > in->conversion_buffer_size) {
843 /*TODO Remove this when AudioPolicyManger/AudioFlinger support arbitrary formats
844 (and do these conversions themselves) */
845 in->conversion_buffer_size = num_read_buff_bytes;
846 in->conversion_buffer = realloc(in->conversion_buffer, in->conversion_buffer_size);
847 }
848 read_buff = in->conversion_buffer;
849 }
850
Pavan Chikkala83b47a62015-01-09 12:21:13 -0800851 ret = proxy_read(&in->proxy, read_buff, num_read_buff_bytes);
852 if (ret == 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700853 if (num_device_channels != num_req_channels) {
854 // ALOGV("chans dev:%d req:%d", num_device_channels, num_req_channels);
855
856 out_buff = buffer;
857 /* Num Channels conversion */
858 if (num_device_channels != num_req_channels) {
859 audio_format_t audio_format = in_get_format(&(in->stream.common));
860 unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
861
862 num_read_buff_bytes =
863 adjust_channels(read_buff, num_device_channels,
864 out_buff, num_req_channels,
865 sample_size_in_bytes, num_read_buff_bytes);
866 }
867 }
Eric Laurent253def92014-09-14 12:18:18 -0700868
869 /* no need to acquire in->dev->lock to read mic_muted here as we don't change its state */
870 if (num_read_buff_bytes > 0 && in->dev->mic_muted)
871 memset(buffer, 0, num_read_buff_bytes);
Viswanath L8c7e1112014-10-31 13:07:39 +0530872 } else {
Eric Laurente6499422015-01-09 17:03:27 -0800873 num_read_buff_bytes = 0; // reset the value after USB headset is unplugged
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700874 }
875
876err:
877 pthread_mutex_unlock(&in->lock);
878
879 return num_read_buff_bytes;
880}
881
882static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
883{
884 return 0;
885}
886
887static int adev_open_input_stream(struct audio_hw_device *dev,
888 audio_io_handle_t handle,
889 audio_devices_t devices,
890 struct audio_config *config,
891 struct audio_stream_in **stream_in,
Eric Laurentf5e24692014-07-27 16:14:57 -0700892 audio_input_flags_t flags __unused,
Paul McLean0f1753e2014-12-02 12:36:45 -0700893 const char *address /*__unused*/,
Eric Laurentf5e24692014-07-27 16:14:57 -0700894 audio_source_t source __unused)
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700895{
Paul McLean65ec72b2015-02-18 09:13:24 -0800896 ALOGV("in adev_open_input_stream() rate:%" PRIu32 ", chanMask:0x%" PRIX32 ", fmt:%" PRIu8,
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700897 config->sample_rate, config->channel_mask, config->format);
898
899 struct stream_in *in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
900 int ret = 0;
901
902 if (in == NULL)
903 return -ENOMEM;
904
905 /* setup function pointers */
906 in->stream.common.get_sample_rate = in_get_sample_rate;
907 in->stream.common.set_sample_rate = in_set_sample_rate;
908 in->stream.common.get_buffer_size = in_get_buffer_size;
909 in->stream.common.get_channels = in_get_channels;
910 in->stream.common.get_format = in_get_format;
911 in->stream.common.set_format = in_set_format;
912 in->stream.common.standby = in_standby;
913 in->stream.common.dump = in_dump;
914 in->stream.common.set_parameters = in_set_parameters;
915 in->stream.common.get_parameters = in_get_parameters;
916 in->stream.common.add_audio_effect = in_add_audio_effect;
917 in->stream.common.remove_audio_effect = in_remove_audio_effect;
918
919 in->stream.set_gain = in_set_gain;
920 in->stream.read = in_read;
921 in->stream.get_input_frames_lost = in_get_input_frames_lost;
922
923 in->dev = (struct audio_device *)dev;
Paul McLean0f1753e2014-12-02 12:36:45 -0700924 pthread_mutex_lock(&in->dev->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700925
926 in->profile = &in->dev->in_profile;
927
928 struct pcm_config proxy_config;
Paul McLean2c6196f2014-08-20 16:50:25 -0700929 memset(&proxy_config, 0, sizeof(proxy_config));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700930
Paul McLean0f1753e2014-12-02 12:36:45 -0700931 /* Pull out the card/device pair */
932 parse_card_device_params(address, &(in->profile->card), &(in->profile->device));
933
934 profile_read_device_info(in->profile);
935 pthread_mutex_unlock(&in->dev->lock);
936
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700937 /* Rate */
938 if (config->sample_rate == 0) {
939 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(in->profile);
940 } else if (profile_is_sample_rate_valid(in->profile, config->sample_rate)) {
941 proxy_config.rate = config->sample_rate;
942 } else {
943 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(in->profile);
944 ret = -EINVAL;
945 }
946
947 /* Format */
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700948 if (config->format == AUDIO_FORMAT_DEFAULT) {
Andy Hung780f1f82015-04-22 22:14:39 -0700949 proxy_config.format = profile_get_default_format(in->profile);
950 config->format = audio_format_from_pcm_format(proxy_config.format);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700951 } else {
Andy Hung780f1f82015-04-22 22:14:39 -0700952 enum pcm_format fmt = pcm_format_from_audio_format(config->format);
953 if (profile_is_format_valid(in->profile, fmt)) {
954 proxy_config.format = fmt;
955 } else {
956 proxy_config.format = profile_get_default_format(in->profile);
957 config->format = audio_format_from_pcm_format(proxy_config.format);
958 ret = -EINVAL;
959 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700960 }
961
Paul McLean2cfd81b2014-09-15 12:32:23 -0700962 /* Channels */
Andy Hung780f1f82015-04-22 22:14:39 -0700963 unsigned proposed_channel_count = 0;
Paul McLean2cfd81b2014-09-15 12:32:23 -0700964 if (k_force_channels) {
965 proposed_channel_count = k_force_channels;
Andy Hung780f1f82015-04-22 22:14:39 -0700966 } else if (config->channel_mask == AUDIO_CHANNEL_NONE) {
967 proposed_channel_count = profile_get_default_channel_count(in->profile);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700968 }
Andy Hung780f1f82015-04-22 22:14:39 -0700969 if (proposed_channel_count != 0) {
970 config->channel_mask = audio_channel_in_mask_from_count(proposed_channel_count);
971 if (config->channel_mask == AUDIO_CHANNEL_INVALID)
972 config->channel_mask =
973 audio_channel_mask_for_index_assignment_from_count(proposed_channel_count);
974 in->hal_channel_count = proposed_channel_count;
975 } else {
976 in->hal_channel_count = audio_channel_count_from_in_mask(config->channel_mask);
977 }
978 /* we can expose any channel mask, and emulate internally based on channel count. */
979 in->hal_channel_mask = config->channel_mask;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700980
Paul McLean2cfd81b2014-09-15 12:32:23 -0700981 proxy_config.channels = profile_get_default_channel_count(in->profile);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700982 proxy_prepare(&in->proxy, in->profile, &proxy_config);
983
984 in->standby = true;
985
986 in->conversion_buffer = NULL;
987 in->conversion_buffer_size = 0;
988
989 *stream_in = &in->stream;
990
991 return ret;
992}
993
994static void adev_close_input_stream(struct audio_hw_device *dev, struct audio_stream_in *stream)
995{
996 struct stream_in *in = (struct stream_in *)stream;
997
998 /* Close the pcm device */
999 in_standby(&stream->common);
1000
1001 free(in->conversion_buffer);
1002
1003 free(stream);
1004}
1005
1006/*
1007 * ADEV Functions
1008 */
Simon Wilson19957a32012-04-06 16:17:12 -07001009static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
1010{
1011 return 0;
1012}
1013
Paul McLean30f41852014-04-16 15:44:20 -07001014static char * adev_get_parameters(const struct audio_hw_device *dev, const char *keys)
Simon Wilson19957a32012-04-06 16:17:12 -07001015{
1016 return strdup("");
1017}
1018
1019static int adev_init_check(const struct audio_hw_device *dev)
1020{
1021 return 0;
1022}
1023
1024static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
1025{
1026 return -ENOSYS;
1027}
1028
1029static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
1030{
1031 return -ENOSYS;
1032}
1033
1034static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
1035{
1036 return 0;
1037}
1038
1039static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
1040{
Eric Laurent253def92014-09-14 12:18:18 -07001041 struct audio_device * adev = (struct audio_device *)dev;
1042 pthread_mutex_lock(&adev->lock);
1043 adev->mic_muted = state;
1044 pthread_mutex_unlock(&adev->lock);
Simon Wilson19957a32012-04-06 16:17:12 -07001045 return -ENOSYS;
1046}
1047
1048static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
1049{
1050 return -ENOSYS;
1051}
1052
Simon Wilson19957a32012-04-06 16:17:12 -07001053static int adev_dump(const audio_hw_device_t *device, int fd)
1054{
1055 return 0;
1056}
1057
1058static int adev_close(hw_device_t *device)
1059{
Paul McLeaneedc92e2013-12-19 15:46:15 -08001060 struct audio_device *adev = (struct audio_device *)device;
Simon Wilson19957a32012-04-06 16:17:12 -07001061 free(device);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001062
Simon Wilson19957a32012-04-06 16:17:12 -07001063 return 0;
1064}
1065
Paul McLean30f41852014-04-16 15:44:20 -07001066static int adev_open(const hw_module_t* module, const char* name, hw_device_t** device)
Simon Wilson19957a32012-04-06 16:17:12 -07001067{
Simon Wilson19957a32012-04-06 16:17:12 -07001068 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
1069 return -EINVAL;
1070
Paul McLeaneedc92e2013-12-19 15:46:15 -08001071 struct audio_device *adev = calloc(1, sizeof(struct audio_device));
Simon Wilson19957a32012-04-06 16:17:12 -07001072 if (!adev)
1073 return -ENOMEM;
1074
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001075 profile_init(&adev->out_profile, PCM_OUT);
1076 profile_init(&adev->in_profile, PCM_IN);
1077
Simon Wilson19957a32012-04-06 16:17:12 -07001078 adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
Eric Laurent85e08e22012-08-28 14:30:35 -07001079 adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001080 adev->hw_device.common.module = (struct hw_module_t *)module;
Simon Wilson19957a32012-04-06 16:17:12 -07001081 adev->hw_device.common.close = adev_close;
1082
Simon Wilson19957a32012-04-06 16:17:12 -07001083 adev->hw_device.init_check = adev_init_check;
1084 adev->hw_device.set_voice_volume = adev_set_voice_volume;
1085 adev->hw_device.set_master_volume = adev_set_master_volume;
1086 adev->hw_device.set_mode = adev_set_mode;
1087 adev->hw_device.set_mic_mute = adev_set_mic_mute;
1088 adev->hw_device.get_mic_mute = adev_get_mic_mute;
1089 adev->hw_device.set_parameters = adev_set_parameters;
1090 adev->hw_device.get_parameters = adev_get_parameters;
1091 adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size;
1092 adev->hw_device.open_output_stream = adev_open_output_stream;
1093 adev->hw_device.close_output_stream = adev_close_output_stream;
1094 adev->hw_device.open_input_stream = adev_open_input_stream;
1095 adev->hw_device.close_input_stream = adev_close_input_stream;
1096 adev->hw_device.dump = adev_dump;
1097
1098 *device = &adev->hw_device.common;
1099
1100 return 0;
1101}
1102
1103static struct hw_module_methods_t hal_module_methods = {
1104 .open = adev_open,
1105};
1106
1107struct audio_module HAL_MODULE_INFO_SYM = {
1108 .common = {
1109 .tag = HARDWARE_MODULE_TAG,
Mike Lockwood46a98092012-04-24 16:41:18 -07001110 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
1111 .hal_api_version = HARDWARE_HAL_API_VERSION,
Simon Wilson19957a32012-04-06 16:17:12 -07001112 .id = AUDIO_HARDWARE_MODULE_ID,
1113 .name = "USB audio HW HAL",
1114 .author = "The Android Open Source Project",
1115 .methods = &hal_module_methods,
1116 },
1117};