blob: 894d2542a185e89e98b3d8203563aa2df0fccb4d [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>
Paul McLean6a75e4e2016-05-25 14:09:02 -060028#include <cutils/list.h>
Simon Wilson19957a32012-04-06 16:17:12 -070029#include <cutils/str_parms.h>
30#include <cutils/properties.h>
31
Simon Wilson19957a32012-04-06 16:17:12 -070032#include <hardware/audio.h>
Paul McLeane32cbc12014-06-25 10:42:07 -070033#include <hardware/audio_alsaops.h>
34#include <hardware/hardware.h>
35
36#include <system/audio.h>
Simon Wilson19957a32012-04-06 16:17:12 -070037
38#include <tinyalsa/asoundlib.h>
39
Paul McLeanc2201152014-07-16 13:46:07 -070040#include <audio_utils/channels.h>
41
Andy Hung03576be2014-07-21 21:16:45 -070042/* FOR TESTING:
43 * Set k_force_channels to force the number of channels to present to AudioFlinger.
44 * 0 disables (this is default: present the device channels to AudioFlinger).
45 * 2 forces to legacy stereo mode.
46 *
47 * Others values can be tried (up to 8).
48 * TODO: AudioFlinger cannot support more than 8 active output channels
49 * at this time, so limiting logic needs to be put here or communicated from above.
50 */
51static const unsigned k_force_channels = 0;
52
Paul McLeanc88e6ae2014-07-16 09:48:34 -070053#include "alsa_device_profile.h"
54#include "alsa_device_proxy.h"
Paul McLean9ab869a2015-01-13 09:37:06 -080055#include "alsa_logging.h"
Paul McLeanf62d75e2014-07-11 15:14:19 -070056
Paul McLeanc88e6ae2014-07-16 09:48:34 -070057#define DEFAULT_INPUT_BUFFER_SIZE_MS 20
Paul McLeanf62d75e2014-07-11 15:14:19 -070058
Paul McLean1d585cc2016-05-24 11:28:10 -060059/* Lock play & record samples rates at or above this threshold */
60#define RATELOCK_THRESHOLD 96000
61
Simon Wilson19957a32012-04-06 16:17:12 -070062struct audio_device {
63 struct audio_hw_device hw_device;
64
65 pthread_mutex_t lock; /* see note below on mutex acquisition order */
Paul McLeaneedc92e2013-12-19 15:46:15 -080066
67 /* output */
Paul McLeanc88e6ae2014-07-16 09:48:34 -070068 alsa_device_profile out_profile;
Paul McLean6a75e4e2016-05-25 14:09:02 -060069 struct listnode output_stream_list;
Paul McLeaneedc92e2013-12-19 15:46:15 -080070
71 /* input */
Paul McLeanc88e6ae2014-07-16 09:48:34 -070072 alsa_device_profile in_profile;
Paul McLean6a75e4e2016-05-25 14:09:02 -060073 struct listnode input_stream_list;
Paul McLeaneedc92e2013-12-19 15:46:15 -080074
Paul McLean1d585cc2016-05-24 11:28:10 -060075 /* lock input & output sample rates */
76 /*FIXME - How do we address multiple output streams? */
77 uint32_t device_sample_rate;
78
Eric Laurent253def92014-09-14 12:18:18 -070079 bool mic_muted;
80
Simon Wilson19957a32012-04-06 16:17:12 -070081 bool standby;
82};
83
84struct stream_out {
85 struct audio_stream_out stream;
86
Paul McLeaneedc92e2013-12-19 15:46:15 -080087 pthread_mutex_t lock; /* see note below on mutex acquisition order */
Eric Laurent70318092015-06-19 17:49:17 -070088 pthread_mutex_t pre_lock; /* acquire before lock to avoid DOS by playback thread */
Paul McLeaneedc92e2013-12-19 15:46:15 -080089 bool standby;
90
Paul McLeanc88e6ae2014-07-16 09:48:34 -070091 struct audio_device *dev; /* hardware information - only using this for the lock */
92
Paul McLean65ec72b2015-02-18 09:13:24 -080093 alsa_device_profile * profile; /* Points to the alsa_device_profile in the audio_device */
Paul McLeanc88e6ae2014-07-16 09:48:34 -070094 alsa_device_proxy proxy; /* state of the stream */
Paul McLeaneedc92e2013-12-19 15:46:15 -080095
Andy Hung03576be2014-07-21 21:16:45 -070096 unsigned hal_channel_count; /* channel count exposed to AudioFlinger.
97 * This may differ from the device channel count when
98 * the device is not compatible with AudioFlinger
99 * capabilities, e.g. exposes too many channels or
100 * too few channels. */
Andy Hung182ddc72015-05-05 23:37:30 -0700101 audio_channel_mask_t hal_channel_mask; /* channel mask exposed to AudioFlinger. */
102
Paul McLean6a75e4e2016-05-25 14:09:02 -0600103 struct listnode list_node;
104
Paul McLeaneedc92e2013-12-19 15:46:15 -0800105 void * conversion_buffer; /* any conversions are put into here
106 * they could come from here too if
107 * there was a previous conversion */
108 size_t conversion_buffer_size; /* in bytes */
109};
110
Paul McLeaneedc92e2013-12-19 15:46:15 -0800111struct stream_in {
112 struct audio_stream_in stream;
113
Eric Laurent70318092015-06-19 17:49:17 -0700114 pthread_mutex_t lock; /* see note below on mutex acquisition order */
115 pthread_mutex_t pre_lock; /* acquire before lock to avoid DOS by capture thread */
Simon Wilson19957a32012-04-06 16:17:12 -0700116 bool standby;
117
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700118 struct audio_device *dev; /* hardware information - only using this for the lock */
Paul McLeaneedc92e2013-12-19 15:46:15 -0800119
Paul McLean65ec72b2015-02-18 09:13:24 -0800120 alsa_device_profile * profile; /* Points to the alsa_device_profile in the audio_device */
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700121 alsa_device_proxy proxy; /* state of the stream */
Paul McLeanf62d75e2014-07-11 15:14:19 -0700122
Paul McLean2cfd81b2014-09-15 12:32:23 -0700123 unsigned hal_channel_count; /* channel count exposed to AudioFlinger.
124 * This may differ from the device channel count when
125 * the device is not compatible with AudioFlinger
126 * capabilities, e.g. exposes too many channels or
127 * too few channels. */
Andy Hung780f1f82015-04-22 22:14:39 -0700128 audio_channel_mask_t hal_channel_mask; /* channel mask exposed to AudioFlinger. */
129
Paul McLean6a75e4e2016-05-25 14:09:02 -0600130 struct listnode list_node;
131
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700132 /* 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 -0700133 void * conversion_buffer; /* any conversions are put into here
134 * they could come from here too if
135 * there was a previous conversion */
136 size_t conversion_buffer_size; /* in bytes */
Simon Wilson19957a32012-04-06 16:17:12 -0700137};
138
Paul McLean6a75e4e2016-05-25 14:09:02 -0600139static void adev_add_stream_to_list(
140 struct audio_device* adev, struct listnode* list, struct listnode* stream_node) {
141 pthread_mutex_lock(&adev->lock);
142
143 list_add_tail(list, stream_node);
144
145 pthread_mutex_unlock(&adev->lock);
146}
147
148static void adev_remove_stream_from_list(
149 struct audio_device* adev, struct listnode* stream_node) {
150 pthread_mutex_lock(&adev->lock);
151
152 list_remove(stream_node);
153
154 pthread_mutex_unlock(&adev->lock);
155}
156
Paul McLeaneedc92e2013-12-19 15:46:15 -0800157/*
Eric Laurent70318092015-06-19 17:49:17 -0700158 * NOTE: when multiple mutexes have to be acquired, always take the
159 * stream_in or stream_out mutex first, followed by the audio_device mutex.
160 * stream pre_lock is always acquired before stream lock to prevent starvation of control thread by
161 * higher priority playback or capture thread.
162 */
163
164/*
Paul McLean0f1753e2014-12-02 12:36:45 -0700165 * Extract the card and device numbers from the supplied key/value pairs.
166 * kvpairs A null-terminated string containing the key/value pairs or card and device.
167 * i.e. "card=1;device=42"
168 * card A pointer to a variable to receive the parsed-out card number.
169 * device A pointer to a variable to receive the parsed-out device number.
170 * NOTE: The variables pointed to by card and device return -1 (undefined) if the
171 * associated key/value pair is not found in the provided string.
Paul McLean65ec72b2015-02-18 09:13:24 -0800172 * Return true if the kvpairs string contain a card/device spec, false otherwise.
Paul McLean0f1753e2014-12-02 12:36:45 -0700173 */
Paul McLean65ec72b2015-02-18 09:13:24 -0800174static bool parse_card_device_params(const char *kvpairs, int *card, int *device)
Paul McLean0f1753e2014-12-02 12:36:45 -0700175{
176 struct str_parms * parms = str_parms_create_str(kvpairs);
177 char value[32];
178 int param_val;
179
180 // initialize to "undefined" state.
181 *card = -1;
182 *device = -1;
183
184 param_val = str_parms_get_str(parms, "card", value, sizeof(value));
185 if (param_val >= 0) {
186 *card = atoi(value);
187 }
188
189 param_val = str_parms_get_str(parms, "device", value, sizeof(value));
190 if (param_val >= 0) {
191 *device = atoi(value);
192 }
193
194 str_parms_destroy(parms);
Paul McLean65ec72b2015-02-18 09:13:24 -0800195
196 return *card >= 0 && *device >= 0;
Paul McLean0f1753e2014-12-02 12:36:45 -0700197}
198
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700199static char * device_get_parameters(alsa_device_profile * profile, const char * keys)
Paul McLeaneedc92e2013-12-19 15:46:15 -0800200{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700201 if (profile->card < 0 || profile->device < 0) {
202 return strdup("");
Paul McLeaneedc92e2013-12-19 15:46:15 -0800203 }
Paul McLeane32cbc12014-06-25 10:42:07 -0700204
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700205 struct str_parms *query = str_parms_create_str(keys);
206 struct str_parms *result = str_parms_create();
Paul McLeane32cbc12014-06-25 10:42:07 -0700207
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700208 /* These keys are from hardware/libhardware/include/audio.h */
209 /* supported sample rates */
210 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) {
211 char* rates_list = profile_get_sample_rate_strs(profile);
212 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES,
213 rates_list);
214 free(rates_list);
Paul McLeane32cbc12014-06-25 10:42:07 -0700215 }
216
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700217 /* supported channel counts */
218 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) {
219 char* channels_list = profile_get_channel_count_strs(profile);
220 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_CHANNELS,
221 channels_list);
222 free(channels_list);
Paul McLeane32cbc12014-06-25 10:42:07 -0700223 }
224
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700225 /* supported sample formats */
226 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) {
227 char * format_params = profile_get_format_strs(profile);
228 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_FORMATS,
229 format_params);
230 free(format_params);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700231 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700232 str_parms_destroy(query);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700233
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700234 char* result_str = str_parms_to_str(result);
235 str_parms_destroy(result);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700236
Paul McLean65ec72b2015-02-18 09:13:24 -0800237 ALOGV("device_get_parameters = %s", result_str);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700238
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700239 return result_str;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800240}
241
Eric Laurent70318092015-06-19 17:49:17 -0700242void lock_input_stream(struct stream_in *in)
243{
244 pthread_mutex_lock(&in->pre_lock);
245 pthread_mutex_lock(&in->lock);
246 pthread_mutex_unlock(&in->pre_lock);
247}
248
249void lock_output_stream(struct stream_out *out)
250{
251 pthread_mutex_lock(&out->pre_lock);
252 pthread_mutex_lock(&out->lock);
253 pthread_mutex_unlock(&out->pre_lock);
254}
255
Paul McLeaneedc92e2013-12-19 15:46:15 -0800256/*
257 * HAl Functions
258 */
Simon Wilson19957a32012-04-06 16:17:12 -0700259/**
260 * NOTE: when multiple mutexes have to be acquired, always respect the
261 * following order: hw device > out stream
262 */
263
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700264/*
265 * OUT functions
266 */
Simon Wilson19957a32012-04-06 16:17:12 -0700267static uint32_t out_get_sample_rate(const struct audio_stream *stream)
268{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700269 uint32_t rate = proxy_get_sample_rate(&((struct stream_out*)stream)->proxy);
270 ALOGV("out_get_sample_rate() = %d", rate);
271 return rate;
Simon Wilson19957a32012-04-06 16:17:12 -0700272}
273
274static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
275{
276 return 0;
277}
278
279static size_t out_get_buffer_size(const struct audio_stream *stream)
280{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700281 const struct stream_out* out = (const struct stream_out*)stream;
282 size_t buffer_size =
283 proxy_get_period_size(&out->proxy) * audio_stream_out_frame_size(&(out->stream));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700284 return buffer_size;
Simon Wilson19957a32012-04-06 16:17:12 -0700285}
286
287static uint32_t out_get_channels(const struct audio_stream *stream)
288{
Andy Hung03576be2014-07-21 21:16:45 -0700289 const struct stream_out *out = (const struct stream_out*)stream;
Andy Hung182ddc72015-05-05 23:37:30 -0700290 return out->hal_channel_mask;
Simon Wilson19957a32012-04-06 16:17:12 -0700291}
292
293static audio_format_t out_get_format(const struct audio_stream *stream)
294{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700295 /* Note: The HAL doesn't do any FORMAT conversion at this time. It
296 * Relies on the framework to provide data in the specified format.
297 * This could change in the future.
298 */
299 alsa_device_proxy * proxy = &((struct stream_out*)stream)->proxy;
300 audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700301 return format;
Simon Wilson19957a32012-04-06 16:17:12 -0700302}
303
304static int out_set_format(struct audio_stream *stream, audio_format_t format)
305{
306 return 0;
307}
308
309static int out_standby(struct audio_stream *stream)
310{
311 struct stream_out *out = (struct stream_out *)stream;
312
Eric Laurent70318092015-06-19 17:49:17 -0700313 lock_output_stream(out);
Simon Wilson19957a32012-04-06 16:17:12 -0700314 if (!out->standby) {
Eric Laurent70318092015-06-19 17:49:17 -0700315 pthread_mutex_lock(&out->dev->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700316 proxy_close(&out->proxy);
Eric Laurent70318092015-06-19 17:49:17 -0700317 pthread_mutex_unlock(&out->dev->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700318 out->standby = true;
319 }
Simon Wilson19957a32012-04-06 16:17:12 -0700320 pthread_mutex_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700321
322 return 0;
323}
324
Paul McLean6a75e4e2016-05-25 14:09:02 -0600325static int out_dump(const struct audio_stream *stream, int fd) {
326 const struct stream_out* out_stream = (const struct stream_out*) stream;
327
328 if (out_stream != NULL) {
329 dprintf(fd, "Output Profile:\n");
330 profile_dump(out_stream->profile, fd);
331
332 dprintf(fd, "Output Proxy:\n");
333 proxy_dump(&out_stream->proxy, fd);
334 }
335
Simon Wilson19957a32012-04-06 16:17:12 -0700336 return 0;
337}
338
339static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
340{
Paul McLean65ec72b2015-02-18 09:13:24 -0800341 ALOGV("out_set_parameters() keys:%s", kvpairs);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800342
Simon Wilson19957a32012-04-06 16:17:12 -0700343 struct stream_out *out = (struct stream_out *)stream;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700344
Simon Wilson19957a32012-04-06 16:17:12 -0700345 int routing = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800346 int ret_value = 0;
Eric Laurent05333d42014-08-04 20:29:17 -0700347 int card = -1;
348 int device = -1;
Simon Wilson19957a32012-04-06 16:17:12 -0700349
Paul McLean65ec72b2015-02-18 09:13:24 -0800350 if (!parse_card_device_params(kvpairs, &card, &device)) {
351 // nothing to do
352 return ret_value;
353 }
354
Eric Laurent70318092015-06-19 17:49:17 -0700355 lock_output_stream(out);
Paul McLean65ec72b2015-02-18 09:13:24 -0800356 /* Lock the device because that is where the profile lives */
Paul McLeanf62d75e2014-07-11 15:14:19 -0700357 pthread_mutex_lock(&out->dev->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700358
Paul McLean65ec72b2015-02-18 09:13:24 -0800359 if (!profile_is_cached_for(out->profile, card, device)) {
Eric Laurent05333d42014-08-04 20:29:17 -0700360 /* cannot read pcm device info if playback is active */
361 if (!out->standby)
362 ret_value = -ENOSYS;
363 else {
364 int saved_card = out->profile->card;
365 int saved_device = out->profile->device;
366 out->profile->card = card;
367 out->profile->device = device;
368 ret_value = profile_read_device_info(out->profile) ? 0 : -EINVAL;
369 if (ret_value != 0) {
370 out->profile->card = saved_card;
371 out->profile->device = saved_device;
372 }
373 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800374 }
Paul McLean2c6196f2014-08-20 16:50:25 -0700375
Paul McLeanf62d75e2014-07-11 15:14:19 -0700376 pthread_mutex_unlock(&out->dev->lock);
Eric Laurent70318092015-06-19 17:49:17 -0700377 pthread_mutex_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700378
Paul McLeaneedc92e2013-12-19 15:46:15 -0800379 return ret_value;
Simon Wilson19957a32012-04-06 16:17:12 -0700380}
381
Paul McLeanf62d75e2014-07-11 15:14:19 -0700382static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
383{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700384 struct stream_out *out = (struct stream_out *)stream;
Eric Laurent70318092015-06-19 17:49:17 -0700385 lock_output_stream(out);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700386 pthread_mutex_lock(&out->dev->lock);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700387
388 char * params_str = device_get_parameters(out->profile, keys);
389
390 pthread_mutex_unlock(&out->lock);
391 pthread_mutex_unlock(&out->dev->lock);
392
393 return params_str;
394}
395
Simon Wilson19957a32012-04-06 16:17:12 -0700396static uint32_t out_get_latency(const struct audio_stream_out *stream)
397{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700398 alsa_device_proxy * proxy = &((struct stream_out*)stream)->proxy;
399 return proxy_get_latency(proxy);
Simon Wilson19957a32012-04-06 16:17:12 -0700400}
401
Paul McLean30f41852014-04-16 15:44:20 -0700402static int out_set_volume(struct audio_stream_out *stream, float left, float right)
Simon Wilson19957a32012-04-06 16:17:12 -0700403{
404 return -ENOSYS;
405}
406
Paul McLeaneedc92e2013-12-19 15:46:15 -0800407/* must be called with hw device and output stream mutexes locked */
408static int start_output_stream(struct stream_out *out)
409{
Paul McLean65ec72b2015-02-18 09:13:24 -0800410 ALOGV("start_output_stream(card:%d device:%d)", out->profile->card, out->profile->device);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800411
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700412 return proxy_open(&out->proxy);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800413}
414
415static ssize_t out_write(struct audio_stream_out *stream, const void* buffer, size_t bytes)
Simon Wilson19957a32012-04-06 16:17:12 -0700416{
417 int ret;
418 struct stream_out *out = (struct stream_out *)stream;
419
Eric Laurent70318092015-06-19 17:49:17 -0700420 lock_output_stream(out);
Simon Wilson19957a32012-04-06 16:17:12 -0700421 if (out->standby) {
Eric Laurent70318092015-06-19 17:49:17 -0700422 pthread_mutex_lock(&out->dev->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700423 ret = start_output_stream(out);
Eric Laurent70318092015-06-19 17:49:17 -0700424 pthread_mutex_unlock(&out->dev->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700425 if (ret != 0) {
426 goto err;
427 }
428 out->standby = false;
429 }
Eric Laurent05333d42014-08-04 20:29:17 -0700430
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700431 alsa_device_proxy* proxy = &out->proxy;
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700432 const void * write_buff = buffer;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800433 int num_write_buff_bytes = bytes;
Andy Hung03576be2014-07-21 21:16:45 -0700434 const int num_device_channels = proxy_get_channel_count(proxy); /* what we told alsa */
435 const int num_req_channels = out->hal_channel_count; /* what we told AudioFlinger */
Paul McLean30f41852014-04-16 15:44:20 -0700436 if (num_device_channels != num_req_channels) {
Andy Hung03576be2014-07-21 21:16:45 -0700437 /* allocate buffer */
438 const size_t required_conversion_buffer_size =
439 bytes * num_device_channels / num_req_channels;
440 if (required_conversion_buffer_size > out->conversion_buffer_size) {
441 out->conversion_buffer_size = required_conversion_buffer_size;
442 out->conversion_buffer = realloc(out->conversion_buffer,
443 out->conversion_buffer_size);
444 }
445 /* convert data */
446 const audio_format_t audio_format = out_get_format(&(out->stream.common));
447 const unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800448 num_write_buff_bytes =
Andy Hung03576be2014-07-21 21:16:45 -0700449 adjust_channels(write_buff, num_req_channels,
450 out->conversion_buffer, num_device_channels,
451 sample_size_in_bytes, num_write_buff_bytes);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800452 write_buff = out->conversion_buffer;
453 }
454
Paul McLeaneedc92e2013-12-19 15:46:15 -0800455 if (write_buff != NULL && num_write_buff_bytes != 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700456 proxy_write(&out->proxy, write_buff, num_write_buff_bytes);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800457 }
Simon Wilson19957a32012-04-06 16:17:12 -0700458
459 pthread_mutex_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700460
461 return bytes;
462
463err:
464 pthread_mutex_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700465 if (ret != 0) {
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700466 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
Simon Wilson19957a32012-04-06 16:17:12 -0700467 out_get_sample_rate(&stream->common));
468 }
469
470 return bytes;
471}
472
Paul McLean30f41852014-04-16 15:44:20 -0700473static int out_get_render_position(const struct audio_stream_out *stream, uint32_t *dsp_frames)
Simon Wilson19957a32012-04-06 16:17:12 -0700474{
475 return -EINVAL;
476}
477
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700478static int out_get_presentation_position(const struct audio_stream_out *stream,
479 uint64_t *frames, struct timespec *timestamp)
480{
Andy Hungc9515ce2015-08-04 15:05:19 -0700481 struct stream_out *out = (struct stream_out *)stream; // discard const qualifier
482 lock_output_stream(out);
483
484 const alsa_device_proxy *proxy = &out->proxy;
485 const int ret = proxy_get_presentation_position(proxy, frames, timestamp);
486
487 pthread_mutex_unlock(&out->lock);
Andy Hungc9515ce2015-08-04 15:05:19 -0700488 return ret;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700489}
490
Simon Wilson19957a32012-04-06 16:17:12 -0700491static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
492{
493 return 0;
494}
495
496static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
497{
498 return 0;
499}
500
Paul McLean30f41852014-04-16 15:44:20 -0700501static int out_get_next_write_timestamp(const struct audio_stream_out *stream, int64_t *timestamp)
Simon Wilson19957a32012-04-06 16:17:12 -0700502{
503 return -EINVAL;
504}
505
506static int adev_open_output_stream(struct audio_hw_device *dev,
Mike Lockwood46a98092012-04-24 16:41:18 -0700507 audio_io_handle_t handle,
508 audio_devices_t devices,
509 audio_output_flags_t flags,
510 struct audio_config *config,
Eric Laurentf5e24692014-07-27 16:14:57 -0700511 struct audio_stream_out **stream_out,
Paul McLean0f1753e2014-12-02 12:36:45 -0700512 const char *address /*__unused*/)
Simon Wilson19957a32012-04-06 16:17:12 -0700513{
Paul McLean65ec72b2015-02-18 09:13:24 -0800514 ALOGV("adev_open_output_stream() handle:0x%X, device:0x%X, flags:0x%X, addr:%s",
Paul McLean0f1753e2014-12-02 12:36:45 -0700515 handle, devices, flags, address);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800516
Simon Wilson19957a32012-04-06 16:17:12 -0700517 struct audio_device *adev = (struct audio_device *)dev;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800518
Simon Wilson19957a32012-04-06 16:17:12 -0700519 struct stream_out *out;
Simon Wilson19957a32012-04-06 16:17:12 -0700520
521 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
522 if (!out)
523 return -ENOMEM;
524
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700525 /* setup function pointers */
Simon Wilson19957a32012-04-06 16:17:12 -0700526 out->stream.common.get_sample_rate = out_get_sample_rate;
527 out->stream.common.set_sample_rate = out_set_sample_rate;
528 out->stream.common.get_buffer_size = out_get_buffer_size;
529 out->stream.common.get_channels = out_get_channels;
530 out->stream.common.get_format = out_get_format;
531 out->stream.common.set_format = out_set_format;
532 out->stream.common.standby = out_standby;
533 out->stream.common.dump = out_dump;
534 out->stream.common.set_parameters = out_set_parameters;
535 out->stream.common.get_parameters = out_get_parameters;
536 out->stream.common.add_audio_effect = out_add_audio_effect;
537 out->stream.common.remove_audio_effect = out_remove_audio_effect;
538 out->stream.get_latency = out_get_latency;
539 out->stream.set_volume = out_set_volume;
540 out->stream.write = out_write;
541 out->stream.get_render_position = out_get_render_position;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700542 out->stream.get_presentation_position = out_get_presentation_position;
Simon Wilson19957a32012-04-06 16:17:12 -0700543 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
544
Eric Laurent70318092015-06-19 17:49:17 -0700545 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
546 pthread_mutex_init(&out->pre_lock, (const pthread_mutexattr_t *) NULL);
547
Simon Wilson19957a32012-04-06 16:17:12 -0700548 out->dev = adev;
Paul McLean0f1753e2014-12-02 12:36:45 -0700549 pthread_mutex_lock(&adev->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700550 out->profile = &adev->out_profile;
Paul McLeanf62d75e2014-07-11 15:14:19 -0700551
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700552 // build this to hand to the alsa_device_proxy
553 struct pcm_config proxy_config;
Paul McLean2c6196f2014-08-20 16:50:25 -0700554 memset(&proxy_config, 0, sizeof(proxy_config));
Paul McLeaneedc92e2013-12-19 15:46:15 -0800555
Paul McLean0f1753e2014-12-02 12:36:45 -0700556 /* Pull out the card/device pair */
557 parse_card_device_params(address, &(out->profile->card), &(out->profile->device));
558
559 profile_read_device_info(out->profile);
560
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700561 int ret = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800562
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700563 /* Rate */
564 if (config->sample_rate == 0) {
565 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(out->profile);
566 } else if (profile_is_sample_rate_valid(out->profile, config->sample_rate)) {
567 proxy_config.rate = config->sample_rate;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800568 } else {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700569 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(out->profile);
570 ret = -EINVAL;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800571 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800572
Paul McLean1d585cc2016-05-24 11:28:10 -0600573 out->dev->device_sample_rate = config->sample_rate;
574 pthread_mutex_unlock(&adev->lock);
575
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 }
Paul McLean698dbd72015-11-03 12:24:30 -0800598
Andy Hung182ddc72015-05-05 23:37:30 -0700599 if (proposed_channel_count != 0) {
Andy Hung5e58a302015-06-10 15:17:00 -0700600 if (proposed_channel_count <= FCC_2) {
601 // use channel position mask for mono and stereo
602 config->channel_mask = audio_channel_out_mask_from_count(proposed_channel_count);
603 } else {
604 // use channel index mask for multichannel
Andy Hung182ddc72015-05-05 23:37:30 -0700605 config->channel_mask =
606 audio_channel_mask_for_index_assignment_from_count(proposed_channel_count);
Andy Hung5e58a302015-06-10 15:17:00 -0700607 }
Andy Hung182ddc72015-05-05 23:37:30 -0700608 } else {
Paul McLean698dbd72015-11-03 12:24:30 -0800609 proposed_channel_count = audio_channel_count_from_out_mask(config->channel_mask);
Andy Hung182ddc72015-05-05 23:37:30 -0700610 }
Paul McLean698dbd72015-11-03 12:24:30 -0800611 out->hal_channel_count = proposed_channel_count;
612
Andy Hung182ddc72015-05-05 23:37:30 -0700613 /* we can expose any channel mask, and emulate internally based on channel count. */
614 out->hal_channel_mask = config->channel_mask;
615
Andy Hung03576be2014-07-21 21:16:45 -0700616 /* no validity checks are needed as proxy_prepare() forces channel_count to be valid.
617 * and we emulate any channel count discrepancies in out_write(). */
Paul McLean698dbd72015-11-03 12:24:30 -0800618 proxy_config.channels = out->hal_channel_count;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700619 proxy_prepare(&out->proxy, out->profile, &proxy_config);
620
621 /* TODO The retry mechanism isn't implemented in AudioPolicyManager/AudioFlinger. */
622 ret = 0;
623
Paul McLeaneedc92e2013-12-19 15:46:15 -0800624 out->conversion_buffer = NULL;
625 out->conversion_buffer_size = 0;
Simon Wilson19957a32012-04-06 16:17:12 -0700626
627 out->standby = true;
628
Paul McLean6a75e4e2016-05-25 14:09:02 -0600629 /* Save the stream for adev_dump() */
630 adev_add_stream_to_list(out->dev, &out->dev->output_stream_list, &out->list_node);
631
Simon Wilson19957a32012-04-06 16:17:12 -0700632 *stream_out = &out->stream;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700633
634 return ret;
Simon Wilson19957a32012-04-06 16:17:12 -0700635
636err_open:
637 free(out);
638 *stream_out = NULL;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800639 return -ENOSYS;
Simon Wilson19957a32012-04-06 16:17:12 -0700640}
641
642static void adev_close_output_stream(struct audio_hw_device *dev,
643 struct audio_stream_out *stream)
644{
645 struct stream_out *out = (struct stream_out *)stream;
Paul McLean65ec72b2015-02-18 09:13:24 -0800646 ALOGV("adev_close_output_stream(c:%d d:%d)", out->profile->card, out->profile->device);
Simon Wilson19957a32012-04-06 16:17:12 -0700647
Paul McLean6a75e4e2016-05-25 14:09:02 -0600648 adev_remove_stream_from_list(out->dev, &out->list_node);
649
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700650 /* Close the pcm device */
Simon Wilson19957a32012-04-06 16:17:12 -0700651 out_standby(&stream->common);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800652
653 free(out->conversion_buffer);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700654
Paul McLeaneedc92e2013-12-19 15:46:15 -0800655 out->conversion_buffer = NULL;
656 out->conversion_buffer_size = 0;
657
Paul McLean1d585cc2016-05-24 11:28:10 -0600658 pthread_mutex_lock(&out->dev->lock);
659 out->dev->device_sample_rate = 0;
660 pthread_mutex_unlock(&out->dev->lock);
661
Simon Wilson19957a32012-04-06 16:17:12 -0700662 free(stream);
663}
664
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700665static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
666 const struct audio_config *config)
667{
668 /* TODO This needs to be calculated based on format/channels/rate */
669 return 320;
670}
671
672/*
673 * IN functions
674 */
675static uint32_t in_get_sample_rate(const struct audio_stream *stream)
676{
677 uint32_t rate = proxy_get_sample_rate(&((const struct stream_in *)stream)->proxy);
678 ALOGV("in_get_sample_rate() = %d", rate);
679 return rate;
680}
681
682static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
683{
684 ALOGV("in_set_sample_rate(%d) - NOPE", rate);
685 return -ENOSYS;
686}
687
688static size_t in_get_buffer_size(const struct audio_stream *stream)
689{
690 const struct stream_in * in = ((const struct stream_in*)stream);
Paul McLean2cfd81b2014-09-15 12:32:23 -0700691 return proxy_get_period_size(&in->proxy) * audio_stream_in_frame_size(&(in->stream));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700692}
693
694static uint32_t in_get_channels(const struct audio_stream *stream)
695{
Paul McLean2cfd81b2014-09-15 12:32:23 -0700696 const struct stream_in *in = (const struct stream_in*)stream;
Andy Hung780f1f82015-04-22 22:14:39 -0700697 return in->hal_channel_mask;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700698}
699
700static audio_format_t in_get_format(const struct audio_stream *stream)
701{
Andy Hung780f1f82015-04-22 22:14:39 -0700702 alsa_device_proxy *proxy = &((struct stream_in*)stream)->proxy;
703 audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy));
704 return format;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700705}
706
707static int in_set_format(struct audio_stream *stream, audio_format_t format)
708{
709 ALOGV("in_set_format(%d) - NOPE", format);
710
711 return -ENOSYS;
712}
713
714static int in_standby(struct audio_stream *stream)
715{
716 struct stream_in *in = (struct stream_in *)stream;
717
Eric Laurent70318092015-06-19 17:49:17 -0700718 lock_input_stream(in);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700719 if (!in->standby) {
Eric Laurent70318092015-06-19 17:49:17 -0700720 pthread_mutex_lock(&in->dev->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700721 proxy_close(&in->proxy);
Eric Laurent70318092015-06-19 17:49:17 -0700722 pthread_mutex_unlock(&in->dev->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700723 in->standby = true;
724 }
725
726 pthread_mutex_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700727
728 return 0;
729}
730
731static int in_dump(const struct audio_stream *stream, int fd)
732{
Paul McLean6a75e4e2016-05-25 14:09:02 -0600733 const struct stream_in* in_stream = (const struct stream_in*)stream;
734 if (in_stream != NULL) {
735 dprintf(fd, "Input Profile:\n");
736 profile_dump(in_stream->profile, fd);
737
738 dprintf(fd, "Input Proxy:\n");
739 proxy_dump(&in_stream->proxy, fd);
740 }
741
742 return 0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700743}
744
745static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
746{
Paul McLean65ec72b2015-02-18 09:13:24 -0800747 ALOGV("in_set_parameters() keys:%s", kvpairs);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700748
749 struct stream_in *in = (struct stream_in *)stream;
750
751 char value[32];
752 int param_val;
753 int routing = 0;
754 int ret_value = 0;
Eric Laurent05333d42014-08-04 20:29:17 -0700755 int card = -1;
756 int device = -1;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700757
Paul McLean65ec72b2015-02-18 09:13:24 -0800758 if (!parse_card_device_params(kvpairs, &card, &device)) {
759 // nothing to do
760 return ret_value;
761 }
762
Eric Laurent70318092015-06-19 17:49:17 -0700763 lock_input_stream(in);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700764 pthread_mutex_lock(&in->dev->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700765
Paul McLean2c6196f2014-08-20 16:50:25 -0700766 if (card >= 0 && device >= 0 && !profile_is_cached_for(in->profile, card, device)) {
Eric Laurent05333d42014-08-04 20:29:17 -0700767 /* cannot read pcm device info if playback is active */
768 if (!in->standby)
769 ret_value = -ENOSYS;
770 else {
771 int saved_card = in->profile->card;
772 int saved_device = in->profile->device;
773 in->profile->card = card;
774 in->profile->device = device;
775 ret_value = profile_read_device_info(in->profile) ? 0 : -EINVAL;
776 if (ret_value != 0) {
777 in->profile->card = saved_card;
778 in->profile->device = saved_device;
779 }
780 }
Paul McLean2c6196f2014-08-20 16:50:25 -0700781 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700782
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700783 pthread_mutex_unlock(&in->dev->lock);
Eric Laurent70318092015-06-19 17:49:17 -0700784 pthread_mutex_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700785
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700786 return ret_value;
787}
788
789static char * in_get_parameters(const struct audio_stream *stream, const char *keys)
790{
791 struct stream_in *in = (struct stream_in *)stream;
792
Eric Laurent70318092015-06-19 17:49:17 -0700793 lock_input_stream(in);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700794 pthread_mutex_lock(&in->dev->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700795
796 char * params_str = device_get_parameters(in->profile, keys);
797
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700798 pthread_mutex_unlock(&in->dev->lock);
Eric Laurent70318092015-06-19 17:49:17 -0700799 pthread_mutex_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700800
801 return params_str;
802}
803
804static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
805{
806 return 0;
807}
808
809static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
810{
811 return 0;
812}
813
814static int in_set_gain(struct audio_stream_in *stream, float gain)
815{
816 return 0;
817}
818
819/* must be called with hw device and output stream mutexes locked */
820static int start_input_stream(struct stream_in *in)
821{
Paul McLean1d585cc2016-05-24 11:28:10 -0600822 ALOGV("start_input_stream(card:%d device:%d)", in->profile->card, in->profile->device);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700823
824 return proxy_open(&in->proxy);
825}
826
827/* TODO mutex stuff here (see out_write) */
828static ssize_t in_read(struct audio_stream_in *stream, void* buffer, size_t bytes)
829{
830 size_t num_read_buff_bytes = 0;
831 void * read_buff = buffer;
832 void * out_buff = buffer;
Pavan Chikkala83b47a62015-01-09 12:21:13 -0800833 int ret = 0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700834
835 struct stream_in * in = (struct stream_in *)stream;
836
Eric Laurent70318092015-06-19 17:49:17 -0700837 lock_input_stream(in);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700838 if (in->standby) {
Eric Laurent70318092015-06-19 17:49:17 -0700839 pthread_mutex_lock(&in->dev->lock);
840 ret = start_input_stream(in);
841 pthread_mutex_unlock(&in->dev->lock);
842 if (ret != 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700843 goto err;
844 }
845 in->standby = false;
846 }
847
848 alsa_device_profile * profile = in->profile;
849
850 /*
851 * OK, we need to figure out how much data to read to be able to output the requested
852 * number of bytes in the HAL format (16-bit, stereo).
853 */
854 num_read_buff_bytes = bytes;
Andy Hung780f1f82015-04-22 22:14:39 -0700855 int num_device_channels = proxy_get_channel_count(&in->proxy); /* what we told Alsa */
856 int num_req_channels = in->hal_channel_count; /* what we told AudioFlinger */
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700857
858 if (num_device_channels != num_req_channels) {
859 num_read_buff_bytes = (num_device_channels * num_read_buff_bytes) / num_req_channels;
860 }
861
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700862 /* Setup/Realloc the conversion buffer (if necessary). */
863 if (num_read_buff_bytes != bytes) {
864 if (num_read_buff_bytes > in->conversion_buffer_size) {
865 /*TODO Remove this when AudioPolicyManger/AudioFlinger support arbitrary formats
866 (and do these conversions themselves) */
867 in->conversion_buffer_size = num_read_buff_bytes;
868 in->conversion_buffer = realloc(in->conversion_buffer, in->conversion_buffer_size);
869 }
870 read_buff = in->conversion_buffer;
871 }
872
Pavan Chikkala83b47a62015-01-09 12:21:13 -0800873 ret = proxy_read(&in->proxy, read_buff, num_read_buff_bytes);
874 if (ret == 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700875 if (num_device_channels != num_req_channels) {
876 // ALOGV("chans dev:%d req:%d", num_device_channels, num_req_channels);
877
878 out_buff = buffer;
879 /* Num Channels conversion */
880 if (num_device_channels != num_req_channels) {
881 audio_format_t audio_format = in_get_format(&(in->stream.common));
882 unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
883
884 num_read_buff_bytes =
885 adjust_channels(read_buff, num_device_channels,
886 out_buff, num_req_channels,
887 sample_size_in_bytes, num_read_buff_bytes);
888 }
889 }
Eric Laurent253def92014-09-14 12:18:18 -0700890
891 /* no need to acquire in->dev->lock to read mic_muted here as we don't change its state */
892 if (num_read_buff_bytes > 0 && in->dev->mic_muted)
893 memset(buffer, 0, num_read_buff_bytes);
Viswanath L8c7e1112014-10-31 13:07:39 +0530894 } else {
Eric Laurente6499422015-01-09 17:03:27 -0800895 num_read_buff_bytes = 0; // reset the value after USB headset is unplugged
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700896 }
897
898err:
899 pthread_mutex_unlock(&in->lock);
900
901 return num_read_buff_bytes;
902}
903
904static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
905{
906 return 0;
907}
908
909static int adev_open_input_stream(struct audio_hw_device *dev,
910 audio_io_handle_t handle,
911 audio_devices_t devices,
912 struct audio_config *config,
913 struct audio_stream_in **stream_in,
Eric Laurentf5e24692014-07-27 16:14:57 -0700914 audio_input_flags_t flags __unused,
Paul McLean0f1753e2014-12-02 12:36:45 -0700915 const char *address /*__unused*/,
Eric Laurentf5e24692014-07-27 16:14:57 -0700916 audio_source_t source __unused)
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700917{
Paul McLean1d585cc2016-05-24 11:28:10 -0600918 ALOGV("adev_open_input_stream() rate:%" PRIu32 ", chanMask:0x%" PRIX32 ", fmt:%" PRIu8,
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700919 config->sample_rate, config->channel_mask, config->format);
920
921 struct stream_in *in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
922 int ret = 0;
923
924 if (in == NULL)
925 return -ENOMEM;
926
927 /* setup function pointers */
928 in->stream.common.get_sample_rate = in_get_sample_rate;
929 in->stream.common.set_sample_rate = in_set_sample_rate;
930 in->stream.common.get_buffer_size = in_get_buffer_size;
931 in->stream.common.get_channels = in_get_channels;
932 in->stream.common.get_format = in_get_format;
933 in->stream.common.set_format = in_set_format;
934 in->stream.common.standby = in_standby;
935 in->stream.common.dump = in_dump;
936 in->stream.common.set_parameters = in_set_parameters;
937 in->stream.common.get_parameters = in_get_parameters;
938 in->stream.common.add_audio_effect = in_add_audio_effect;
939 in->stream.common.remove_audio_effect = in_remove_audio_effect;
940
941 in->stream.set_gain = in_set_gain;
942 in->stream.read = in_read;
943 in->stream.get_input_frames_lost = in_get_input_frames_lost;
944
Eric Laurent70318092015-06-19 17:49:17 -0700945 pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL);
946 pthread_mutex_init(&in->pre_lock, (const pthread_mutexattr_t *) NULL);
947
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700948 in->dev = (struct audio_device *)dev;
Paul McLean0f1753e2014-12-02 12:36:45 -0700949 pthread_mutex_lock(&in->dev->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700950
951 in->profile = &in->dev->in_profile;
952
953 struct pcm_config proxy_config;
Paul McLean2c6196f2014-08-20 16:50:25 -0700954 memset(&proxy_config, 0, sizeof(proxy_config));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700955
Paul McLean0f1753e2014-12-02 12:36:45 -0700956 /* Pull out the card/device pair */
957 parse_card_device_params(address, &(in->profile->card), &(in->profile->device));
958
959 profile_read_device_info(in->profile);
Paul McLean0f1753e2014-12-02 12:36:45 -0700960
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700961 /* Rate */
962 if (config->sample_rate == 0) {
Paul McLean9a1c3052016-05-25 13:30:54 -0600963 config->sample_rate = profile_get_default_sample_rate(in->profile);
964 }
965
966 if (in->dev->device_sample_rate != 0 && /* we are playing, so lock the rate */
967 in->dev->device_sample_rate >= RATELOCK_THRESHOLD) {/* but only for high sample rates */
Paul McLean1d585cc2016-05-24 11:28:10 -0600968 ret = config->sample_rate != in->dev->device_sample_rate ? -EINVAL : 0;
969 proxy_config.rate = config->sample_rate = in->dev->device_sample_rate;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700970 } else if (profile_is_sample_rate_valid(in->profile, config->sample_rate)) {
Paul McLean1d585cc2016-05-24 11:28:10 -0600971 in->dev->device_sample_rate = proxy_config.rate = config->sample_rate;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700972 } else {
973 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(in->profile);
974 ret = -EINVAL;
975 }
Paul McLean1d585cc2016-05-24 11:28:10 -0600976 pthread_mutex_unlock(&in->dev->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700977
978 /* Format */
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700979 if (config->format == AUDIO_FORMAT_DEFAULT) {
Andy Hung780f1f82015-04-22 22:14:39 -0700980 proxy_config.format = profile_get_default_format(in->profile);
981 config->format = audio_format_from_pcm_format(proxy_config.format);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700982 } else {
Andy Hung780f1f82015-04-22 22:14:39 -0700983 enum pcm_format fmt = pcm_format_from_audio_format(config->format);
984 if (profile_is_format_valid(in->profile, fmt)) {
985 proxy_config.format = fmt;
986 } else {
987 proxy_config.format = profile_get_default_format(in->profile);
988 config->format = audio_format_from_pcm_format(proxy_config.format);
989 ret = -EINVAL;
990 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700991 }
992
Paul McLean2cfd81b2014-09-15 12:32:23 -0700993 /* Channels */
Andy Hung780f1f82015-04-22 22:14:39 -0700994 unsigned proposed_channel_count = 0;
Paul McLean2cfd81b2014-09-15 12:32:23 -0700995 if (k_force_channels) {
996 proposed_channel_count = k_force_channels;
Andy Hung780f1f82015-04-22 22:14:39 -0700997 } else if (config->channel_mask == AUDIO_CHANNEL_NONE) {
998 proposed_channel_count = profile_get_default_channel_count(in->profile);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700999 }
Andy Hung780f1f82015-04-22 22:14:39 -07001000 if (proposed_channel_count != 0) {
1001 config->channel_mask = audio_channel_in_mask_from_count(proposed_channel_count);
1002 if (config->channel_mask == AUDIO_CHANNEL_INVALID)
1003 config->channel_mask =
1004 audio_channel_mask_for_index_assignment_from_count(proposed_channel_count);
1005 in->hal_channel_count = proposed_channel_count;
1006 } else {
1007 in->hal_channel_count = audio_channel_count_from_in_mask(config->channel_mask);
1008 }
1009 /* we can expose any channel mask, and emulate internally based on channel count. */
1010 in->hal_channel_mask = config->channel_mask;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001011
Paul McLean2cfd81b2014-09-15 12:32:23 -07001012 proxy_config.channels = profile_get_default_channel_count(in->profile);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001013 proxy_prepare(&in->proxy, in->profile, &proxy_config);
1014
1015 in->standby = true;
1016
Paul McLean6a75e4e2016-05-25 14:09:02 -06001017 /* Save this for adev_dump() */
1018 adev_add_stream_to_list(in->dev, &in->dev->input_stream_list, &in->list_node);
1019
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001020 in->conversion_buffer = NULL;
1021 in->conversion_buffer_size = 0;
1022
1023 *stream_in = &in->stream;
1024
1025 return ret;
1026}
1027
1028static void adev_close_input_stream(struct audio_hw_device *dev, struct audio_stream_in *stream)
1029{
1030 struct stream_in *in = (struct stream_in *)stream;
Paul McLean6a75e4e2016-05-25 14:09:02 -06001031 ALOGV("adev_close_input_stream(c:%d d:%d)", in->profile->card, in->profile->device);
1032
1033 adev_remove_stream_from_list(in->dev, &in->list_node);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001034
1035 /* Close the pcm device */
1036 in_standby(&stream->common);
1037
1038 free(in->conversion_buffer);
1039
1040 free(stream);
1041}
1042
1043/*
1044 * ADEV Functions
1045 */
Simon Wilson19957a32012-04-06 16:17:12 -07001046static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
1047{
1048 return 0;
1049}
1050
Paul McLean30f41852014-04-16 15:44:20 -07001051static char * adev_get_parameters(const struct audio_hw_device *dev, const char *keys)
Simon Wilson19957a32012-04-06 16:17:12 -07001052{
1053 return strdup("");
1054}
1055
1056static int adev_init_check(const struct audio_hw_device *dev)
1057{
1058 return 0;
1059}
1060
1061static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
1062{
1063 return -ENOSYS;
1064}
1065
1066static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
1067{
1068 return -ENOSYS;
1069}
1070
1071static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
1072{
1073 return 0;
1074}
1075
1076static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
1077{
Eric Laurent253def92014-09-14 12:18:18 -07001078 struct audio_device * adev = (struct audio_device *)dev;
1079 pthread_mutex_lock(&adev->lock);
1080 adev->mic_muted = state;
1081 pthread_mutex_unlock(&adev->lock);
Simon Wilson19957a32012-04-06 16:17:12 -07001082 return -ENOSYS;
1083}
1084
1085static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
1086{
1087 return -ENOSYS;
1088}
1089
Simon Wilson19957a32012-04-06 16:17:12 -07001090static int adev_dump(const audio_hw_device_t *device, int fd)
1091{
Paul McLean6a75e4e2016-05-25 14:09:02 -06001092 dprintf(fd, "\nUSB audio module:\n");
1093
1094 struct audio_device* adev = (struct audio_device*)device;
1095 const int kNumRetries = 3;
1096 const int kSleepTimeMS = 500;
1097
1098 // use pthread_mutex_trylock() in case we dumpsys during a deadlock
1099 int retry = kNumRetries;
1100 while (retry > 0 && pthread_mutex_trylock(&adev->lock) != 0) {
1101 sleep(kSleepTimeMS);
1102 retry--;
1103 }
1104
1105 if (retry > 0) {
1106 if (list_empty(&adev->output_stream_list)) {
1107 dprintf(fd, " No output streams.\n");
1108 } else {
1109 struct listnode* node;
1110 list_for_each(node, &adev->output_stream_list) {
1111 struct audio_stream* stream =
1112 (struct audio_stream *)node_to_item(node, struct stream_out, list_node);
1113 out_dump(stream, fd);
1114 }
1115 }
1116
1117 if (list_empty(&adev->input_stream_list)) {
1118 dprintf(fd, "\n No input streams.\n");
1119 } else {
1120 struct listnode* node;
1121 list_for_each(node, &adev->input_stream_list) {
1122 struct audio_stream* stream =
1123 (struct audio_stream *)node_to_item(node, struct stream_in, list_node);
1124 in_dump(stream, fd);
1125 }
1126 }
1127
1128 pthread_mutex_unlock(&adev->lock);
1129 } else {
1130 // Couldn't lock
1131 dprintf(fd, " Could not obtain device lock.\n");
1132 }
1133
Simon Wilson19957a32012-04-06 16:17:12 -07001134 return 0;
1135}
1136
1137static int adev_close(hw_device_t *device)
1138{
Paul McLeaneedc92e2013-12-19 15:46:15 -08001139 struct audio_device *adev = (struct audio_device *)device;
Simon Wilson19957a32012-04-06 16:17:12 -07001140 free(device);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001141
Simon Wilson19957a32012-04-06 16:17:12 -07001142 return 0;
1143}
1144
Paul McLean30f41852014-04-16 15:44:20 -07001145static int adev_open(const hw_module_t* module, const char* name, hw_device_t** device)
Simon Wilson19957a32012-04-06 16:17:12 -07001146{
Simon Wilson19957a32012-04-06 16:17:12 -07001147 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
1148 return -EINVAL;
1149
Paul McLeaneedc92e2013-12-19 15:46:15 -08001150 struct audio_device *adev = calloc(1, sizeof(struct audio_device));
Simon Wilson19957a32012-04-06 16:17:12 -07001151 if (!adev)
1152 return -ENOMEM;
1153
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001154 profile_init(&adev->out_profile, PCM_OUT);
1155 profile_init(&adev->in_profile, PCM_IN);
1156
Paul McLean6a75e4e2016-05-25 14:09:02 -06001157 list_init(&adev->output_stream_list);
1158 list_init(&adev->input_stream_list);
1159
Simon Wilson19957a32012-04-06 16:17:12 -07001160 adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
Eric Laurent85e08e22012-08-28 14:30:35 -07001161 adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001162 adev->hw_device.common.module = (struct hw_module_t *)module;
Simon Wilson19957a32012-04-06 16:17:12 -07001163 adev->hw_device.common.close = adev_close;
1164
Simon Wilson19957a32012-04-06 16:17:12 -07001165 adev->hw_device.init_check = adev_init_check;
1166 adev->hw_device.set_voice_volume = adev_set_voice_volume;
1167 adev->hw_device.set_master_volume = adev_set_master_volume;
1168 adev->hw_device.set_mode = adev_set_mode;
1169 adev->hw_device.set_mic_mute = adev_set_mic_mute;
1170 adev->hw_device.get_mic_mute = adev_get_mic_mute;
1171 adev->hw_device.set_parameters = adev_set_parameters;
1172 adev->hw_device.get_parameters = adev_get_parameters;
1173 adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size;
1174 adev->hw_device.open_output_stream = adev_open_output_stream;
1175 adev->hw_device.close_output_stream = adev_close_output_stream;
1176 adev->hw_device.open_input_stream = adev_open_input_stream;
1177 adev->hw_device.close_input_stream = adev_close_input_stream;
1178 adev->hw_device.dump = adev_dump;
1179
1180 *device = &adev->hw_device.common;
1181
1182 return 0;
1183}
1184
1185static struct hw_module_methods_t hal_module_methods = {
1186 .open = adev_open,
1187};
1188
1189struct audio_module HAL_MODULE_INFO_SYM = {
1190 .common = {
1191 .tag = HARDWARE_MODULE_TAG,
Mike Lockwood46a98092012-04-24 16:41:18 -07001192 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
1193 .hal_api_version = HARDWARE_HAL_API_VERSION,
Simon Wilson19957a32012-04-06 16:17:12 -07001194 .id = AUDIO_HARDWARE_MODULE_ID,
1195 .name = "USB audio HW HAL",
1196 .author = "The Android Open Source Project",
1197 .methods = &hal_module_methods,
1198 },
1199};