blob: 07705ee5ef066f9126df71abcd7c029e5d289e9f [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 McLean76dba682016-06-01 12:29:11 -060091 struct audio_device *adev; /* hardware information - only using this for the lock */
Paul McLeanc88e6ae2014-07-16 09:48:34 -070092
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 McLean76dba682016-06-01 12:29:11 -0600118 struct audio_device *adev; /* 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) {
Paul McLean76dba682016-06-01 12:29:11 -0600315 pthread_mutex_lock(&out->adev->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700316 proxy_close(&out->proxy);
Paul McLean76dba682016-06-01 12:29:11 -0600317 pthread_mutex_unlock(&out->adev->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 McLean76dba682016-06-01 12:29:11 -0600357 pthread_mutex_lock(&out->adev->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 McLean76dba682016-06-01 12:29:11 -0600376 pthread_mutex_unlock(&out->adev->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 McLean76dba682016-06-01 12:29:11 -0600386 pthread_mutex_lock(&out->adev->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);
Paul McLean76dba682016-06-01 12:29:11 -0600391 pthread_mutex_unlock(&out->adev->lock);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700392
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) {
Paul McLean76dba682016-06-01 12:29:11 -0600422 pthread_mutex_lock(&out->adev->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700423 ret = start_output_stream(out);
Paul McLean76dba682016-06-01 12:29:11 -0600424 pthread_mutex_unlock(&out->adev->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
Paul McLean76dba682016-06-01 12:29:11 -0600506static int adev_open_output_stream(struct audio_hw_device *hw_dev,
Mike Lockwood46a98092012-04-24 16:41:18 -0700507 audio_io_handle_t handle,
Paul McLean76dba682016-06-01 12:29:11 -0600508 audio_devices_t devicesSpec __unused,
Mike Lockwood46a98092012-04-24 16:41:18 -0700509 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 McLean76dba682016-06-01 12:29:11 -0600514 ALOGV("adev_open_output_stream() handle:0x%X, devicesSpec:0x%X, flags:0x%X, addr:%s",
515 handle, devicesSpec, flags, address);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800516
Simon Wilson19957a32012-04-06 16:17:12 -0700517 struct stream_out *out;
Simon Wilson19957a32012-04-06 16:17:12 -0700518
519 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
Paul McLean76dba682016-06-01 12:29:11 -0600520 if (out == NULL) {
Simon Wilson19957a32012-04-06 16:17:12 -0700521 return -ENOMEM;
Paul McLean76dba682016-06-01 12:29:11 -0600522 }
Simon Wilson19957a32012-04-06 16:17:12 -0700523
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700524 /* setup function pointers */
Simon Wilson19957a32012-04-06 16:17:12 -0700525 out->stream.common.get_sample_rate = out_get_sample_rate;
526 out->stream.common.set_sample_rate = out_set_sample_rate;
527 out->stream.common.get_buffer_size = out_get_buffer_size;
528 out->stream.common.get_channels = out_get_channels;
529 out->stream.common.get_format = out_get_format;
530 out->stream.common.set_format = out_set_format;
531 out->stream.common.standby = out_standby;
532 out->stream.common.dump = out_dump;
533 out->stream.common.set_parameters = out_set_parameters;
534 out->stream.common.get_parameters = out_get_parameters;
535 out->stream.common.add_audio_effect = out_add_audio_effect;
536 out->stream.common.remove_audio_effect = out_remove_audio_effect;
537 out->stream.get_latency = out_get_latency;
538 out->stream.set_volume = out_set_volume;
539 out->stream.write = out_write;
540 out->stream.get_render_position = out_get_render_position;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700541 out->stream.get_presentation_position = out_get_presentation_position;
Simon Wilson19957a32012-04-06 16:17:12 -0700542 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
543
Eric Laurent70318092015-06-19 17:49:17 -0700544 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
545 pthread_mutex_init(&out->pre_lock, (const pthread_mutexattr_t *) NULL);
546
Paul McLean76dba682016-06-01 12:29:11 -0600547 out->adev = (struct audio_device *)hw_dev;
548 pthread_mutex_lock(&out->adev->lock);
549 out->profile = &out->adev->out_profile;
Paul McLeanf62d75e2014-07-11 15:14:19 -0700550
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700551 // build this to hand to the alsa_device_proxy
552 struct pcm_config proxy_config;
Paul McLean2c6196f2014-08-20 16:50:25 -0700553 memset(&proxy_config, 0, sizeof(proxy_config));
Paul McLeaneedc92e2013-12-19 15:46:15 -0800554
Paul McLean0f1753e2014-12-02 12:36:45 -0700555 /* Pull out the card/device pair */
556 parse_card_device_params(address, &(out->profile->card), &(out->profile->device));
557
558 profile_read_device_info(out->profile);
559
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700560 int ret = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800561
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700562 /* Rate */
563 if (config->sample_rate == 0) {
564 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(out->profile);
565 } else if (profile_is_sample_rate_valid(out->profile, config->sample_rate)) {
566 proxy_config.rate = config->sample_rate;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800567 } else {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700568 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(out->profile);
569 ret = -EINVAL;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800570 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800571
Paul McLean76dba682016-06-01 12:29:11 -0600572 out->adev->device_sample_rate = config->sample_rate;
573 pthread_mutex_unlock(&out->adev->lock);
Paul McLean1d585cc2016-05-24 11:28:10 -0600574
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700575 /* Format */
576 if (config->format == AUDIO_FORMAT_DEFAULT) {
577 proxy_config.format = profile_get_default_format(out->profile);
578 config->format = audio_format_from_pcm_format(proxy_config.format);
579 } else {
580 enum pcm_format fmt = pcm_format_from_audio_format(config->format);
581 if (profile_is_format_valid(out->profile, fmt)) {
582 proxy_config.format = fmt;
583 } else {
584 proxy_config.format = profile_get_default_format(out->profile);
585 config->format = audio_format_from_pcm_format(proxy_config.format);
586 ret = -EINVAL;
587 }
588 }
589
590 /* Channels */
Andy Hung182ddc72015-05-05 23:37:30 -0700591 unsigned proposed_channel_count = 0;
Andy Hung03576be2014-07-21 21:16:45 -0700592 if (k_force_channels) {
593 proposed_channel_count = k_force_channels;
Andy Hung182ddc72015-05-05 23:37:30 -0700594 } else if (config->channel_mask == AUDIO_CHANNEL_NONE) {
595 proposed_channel_count = profile_get_default_channel_count(out->profile);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700596 }
Paul McLean698dbd72015-11-03 12:24:30 -0800597
Andy Hung182ddc72015-05-05 23:37:30 -0700598 if (proposed_channel_count != 0) {
Andy Hung5e58a302015-06-10 15:17:00 -0700599 if (proposed_channel_count <= FCC_2) {
600 // use channel position mask for mono and stereo
601 config->channel_mask = audio_channel_out_mask_from_count(proposed_channel_count);
602 } else {
603 // use channel index mask for multichannel
Andy Hung182ddc72015-05-05 23:37:30 -0700604 config->channel_mask =
605 audio_channel_mask_for_index_assignment_from_count(proposed_channel_count);
Andy Hung5e58a302015-06-10 15:17:00 -0700606 }
Andy Hung182ddc72015-05-05 23:37:30 -0700607 } else {
Paul McLean698dbd72015-11-03 12:24:30 -0800608 proposed_channel_count = audio_channel_count_from_out_mask(config->channel_mask);
Andy Hung182ddc72015-05-05 23:37:30 -0700609 }
Paul McLean698dbd72015-11-03 12:24:30 -0800610 out->hal_channel_count = proposed_channel_count;
611
Andy Hung182ddc72015-05-05 23:37:30 -0700612 /* we can expose any channel mask, and emulate internally based on channel count. */
613 out->hal_channel_mask = config->channel_mask;
614
Andy Hung03576be2014-07-21 21:16:45 -0700615 /* no validity checks are needed as proxy_prepare() forces channel_count to be valid.
616 * and we emulate any channel count discrepancies in out_write(). */
Paul McLean698dbd72015-11-03 12:24:30 -0800617 proxy_config.channels = out->hal_channel_count;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700618 proxy_prepare(&out->proxy, out->profile, &proxy_config);
619
620 /* TODO The retry mechanism isn't implemented in AudioPolicyManager/AudioFlinger. */
621 ret = 0;
622
Paul McLeaneedc92e2013-12-19 15:46:15 -0800623 out->conversion_buffer = NULL;
624 out->conversion_buffer_size = 0;
Simon Wilson19957a32012-04-06 16:17:12 -0700625
626 out->standby = true;
627
Paul McLean6a75e4e2016-05-25 14:09:02 -0600628 /* Save the stream for adev_dump() */
Paul McLean76dba682016-06-01 12:29:11 -0600629 adev_add_stream_to_list(out->adev, &out->adev->output_stream_list, &out->list_node);
Paul McLean6a75e4e2016-05-25 14:09:02 -0600630
Simon Wilson19957a32012-04-06 16:17:12 -0700631 *stream_out = &out->stream;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700632
633 return ret;
Simon Wilson19957a32012-04-06 16:17:12 -0700634
635err_open:
636 free(out);
637 *stream_out = NULL;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800638 return -ENOSYS;
Simon Wilson19957a32012-04-06 16:17:12 -0700639}
640
Paul McLean76dba682016-06-01 12:29:11 -0600641static void adev_close_output_stream(struct audio_hw_device *hw_dev,
Simon Wilson19957a32012-04-06 16:17:12 -0700642 struct audio_stream_out *stream)
643{
644 struct stream_out *out = (struct stream_out *)stream;
Paul McLean65ec72b2015-02-18 09:13:24 -0800645 ALOGV("adev_close_output_stream(c:%d d:%d)", out->profile->card, out->profile->device);
Simon Wilson19957a32012-04-06 16:17:12 -0700646
Paul McLean76dba682016-06-01 12:29:11 -0600647 adev_remove_stream_from_list(out->adev, &out->list_node);
Paul McLean6a75e4e2016-05-25 14:09:02 -0600648
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700649 /* Close the pcm device */
Simon Wilson19957a32012-04-06 16:17:12 -0700650 out_standby(&stream->common);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800651
652 free(out->conversion_buffer);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700653
Paul McLeaneedc92e2013-12-19 15:46:15 -0800654 out->conversion_buffer = NULL;
655 out->conversion_buffer_size = 0;
656
Paul McLean76dba682016-06-01 12:29:11 -0600657 pthread_mutex_lock(&out->adev->lock);
658 out->adev->device_sample_rate = 0;
659 pthread_mutex_unlock(&out->adev->lock);
Paul McLean1d585cc2016-05-24 11:28:10 -0600660
Simon Wilson19957a32012-04-06 16:17:12 -0700661 free(stream);
662}
663
Paul McLean76dba682016-06-01 12:29:11 -0600664static size_t adev_get_input_buffer_size(const struct audio_hw_device *hw_dev,
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700665 const struct audio_config *config)
666{
667 /* TODO This needs to be calculated based on format/channels/rate */
668 return 320;
669}
670
671/*
672 * IN functions
673 */
674static uint32_t in_get_sample_rate(const struct audio_stream *stream)
675{
676 uint32_t rate = proxy_get_sample_rate(&((const struct stream_in *)stream)->proxy);
677 ALOGV("in_get_sample_rate() = %d", rate);
678 return rate;
679}
680
681static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
682{
683 ALOGV("in_set_sample_rate(%d) - NOPE", rate);
684 return -ENOSYS;
685}
686
687static size_t in_get_buffer_size(const struct audio_stream *stream)
688{
689 const struct stream_in * in = ((const struct stream_in*)stream);
Paul McLean2cfd81b2014-09-15 12:32:23 -0700690 return proxy_get_period_size(&in->proxy) * audio_stream_in_frame_size(&(in->stream));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700691}
692
693static uint32_t in_get_channels(const struct audio_stream *stream)
694{
Paul McLean2cfd81b2014-09-15 12:32:23 -0700695 const struct stream_in *in = (const struct stream_in*)stream;
Andy Hung780f1f82015-04-22 22:14:39 -0700696 return in->hal_channel_mask;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700697}
698
699static audio_format_t in_get_format(const struct audio_stream *stream)
700{
Andy Hung780f1f82015-04-22 22:14:39 -0700701 alsa_device_proxy *proxy = &((struct stream_in*)stream)->proxy;
702 audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy));
703 return format;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700704}
705
706static int in_set_format(struct audio_stream *stream, audio_format_t format)
707{
708 ALOGV("in_set_format(%d) - NOPE", format);
709
710 return -ENOSYS;
711}
712
713static int in_standby(struct audio_stream *stream)
714{
715 struct stream_in *in = (struct stream_in *)stream;
716
Eric Laurent70318092015-06-19 17:49:17 -0700717 lock_input_stream(in);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700718 if (!in->standby) {
Paul McLean76dba682016-06-01 12:29:11 -0600719 pthread_mutex_lock(&in->adev->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700720 proxy_close(&in->proxy);
Paul McLean76dba682016-06-01 12:29:11 -0600721 pthread_mutex_unlock(&in->adev->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700722 in->standby = true;
723 }
724
725 pthread_mutex_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700726
727 return 0;
728}
729
730static int in_dump(const struct audio_stream *stream, int fd)
731{
Paul McLean6a75e4e2016-05-25 14:09:02 -0600732 const struct stream_in* in_stream = (const struct stream_in*)stream;
733 if (in_stream != NULL) {
734 dprintf(fd, "Input Profile:\n");
735 profile_dump(in_stream->profile, fd);
736
737 dprintf(fd, "Input Proxy:\n");
738 proxy_dump(&in_stream->proxy, fd);
739 }
740
741 return 0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700742}
743
744static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
745{
Paul McLean65ec72b2015-02-18 09:13:24 -0800746 ALOGV("in_set_parameters() keys:%s", kvpairs);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700747
748 struct stream_in *in = (struct stream_in *)stream;
749
750 char value[32];
751 int param_val;
752 int routing = 0;
753 int ret_value = 0;
Eric Laurent05333d42014-08-04 20:29:17 -0700754 int card = -1;
755 int device = -1;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700756
Paul McLean65ec72b2015-02-18 09:13:24 -0800757 if (!parse_card_device_params(kvpairs, &card, &device)) {
758 // nothing to do
759 return ret_value;
760 }
761
Eric Laurent70318092015-06-19 17:49:17 -0700762 lock_input_stream(in);
Paul McLean76dba682016-06-01 12:29:11 -0600763 pthread_mutex_lock(&in->adev->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700764
Paul McLean2c6196f2014-08-20 16:50:25 -0700765 if (card >= 0 && device >= 0 && !profile_is_cached_for(in->profile, card, device)) {
Eric Laurent05333d42014-08-04 20:29:17 -0700766 /* cannot read pcm device info if playback is active */
767 if (!in->standby)
768 ret_value = -ENOSYS;
769 else {
770 int saved_card = in->profile->card;
771 int saved_device = in->profile->device;
772 in->profile->card = card;
773 in->profile->device = device;
774 ret_value = profile_read_device_info(in->profile) ? 0 : -EINVAL;
775 if (ret_value != 0) {
776 in->profile->card = saved_card;
777 in->profile->device = saved_device;
778 }
779 }
Paul McLean2c6196f2014-08-20 16:50:25 -0700780 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700781
Paul McLean76dba682016-06-01 12:29:11 -0600782 pthread_mutex_unlock(&in->adev->lock);
Eric Laurent70318092015-06-19 17:49:17 -0700783 pthread_mutex_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700784
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700785 return ret_value;
786}
787
788static char * in_get_parameters(const struct audio_stream *stream, const char *keys)
789{
790 struct stream_in *in = (struct stream_in *)stream;
791
Eric Laurent70318092015-06-19 17:49:17 -0700792 lock_input_stream(in);
Paul McLean76dba682016-06-01 12:29:11 -0600793 pthread_mutex_lock(&in->adev->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700794
795 char * params_str = device_get_parameters(in->profile, keys);
796
Paul McLean76dba682016-06-01 12:29:11 -0600797 pthread_mutex_unlock(&in->adev->lock);
Eric Laurent70318092015-06-19 17:49:17 -0700798 pthread_mutex_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700799
800 return params_str;
801}
802
803static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
804{
805 return 0;
806}
807
808static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
809{
810 return 0;
811}
812
813static int in_set_gain(struct audio_stream_in *stream, float gain)
814{
815 return 0;
816}
817
818/* must be called with hw device and output stream mutexes locked */
819static int start_input_stream(struct stream_in *in)
820{
Paul McLean1d585cc2016-05-24 11:28:10 -0600821 ALOGV("start_input_stream(card:%d device:%d)", in->profile->card, in->profile->device);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700822
823 return proxy_open(&in->proxy);
824}
825
826/* TODO mutex stuff here (see out_write) */
827static ssize_t in_read(struct audio_stream_in *stream, void* buffer, size_t bytes)
828{
829 size_t num_read_buff_bytes = 0;
830 void * read_buff = buffer;
831 void * out_buff = buffer;
Pavan Chikkala83b47a62015-01-09 12:21:13 -0800832 int ret = 0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700833
834 struct stream_in * in = (struct stream_in *)stream;
835
Eric Laurent70318092015-06-19 17:49:17 -0700836 lock_input_stream(in);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700837 if (in->standby) {
Paul McLean76dba682016-06-01 12:29:11 -0600838 pthread_mutex_lock(&in->adev->lock);
Eric Laurent70318092015-06-19 17:49:17 -0700839 ret = start_input_stream(in);
Paul McLean76dba682016-06-01 12:29:11 -0600840 pthread_mutex_unlock(&in->adev->lock);
Eric Laurent70318092015-06-19 17:49:17 -0700841 if (ret != 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700842 goto err;
843 }
844 in->standby = false;
845 }
846
847 alsa_device_profile * profile = in->profile;
848
849 /*
850 * OK, we need to figure out how much data to read to be able to output the requested
851 * number of bytes in the HAL format (16-bit, stereo).
852 */
853 num_read_buff_bytes = bytes;
Andy Hung780f1f82015-04-22 22:14:39 -0700854 int num_device_channels = proxy_get_channel_count(&in->proxy); /* what we told Alsa */
855 int num_req_channels = in->hal_channel_count; /* what we told AudioFlinger */
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700856
857 if (num_device_channels != num_req_channels) {
858 num_read_buff_bytes = (num_device_channels * num_read_buff_bytes) / num_req_channels;
859 }
860
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700861 /* Setup/Realloc the conversion buffer (if necessary). */
862 if (num_read_buff_bytes != bytes) {
863 if (num_read_buff_bytes > in->conversion_buffer_size) {
864 /*TODO Remove this when AudioPolicyManger/AudioFlinger support arbitrary formats
865 (and do these conversions themselves) */
866 in->conversion_buffer_size = num_read_buff_bytes;
867 in->conversion_buffer = realloc(in->conversion_buffer, in->conversion_buffer_size);
868 }
869 read_buff = in->conversion_buffer;
870 }
871
Pavan Chikkala83b47a62015-01-09 12:21:13 -0800872 ret = proxy_read(&in->proxy, read_buff, num_read_buff_bytes);
873 if (ret == 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700874 if (num_device_channels != num_req_channels) {
875 // ALOGV("chans dev:%d req:%d", num_device_channels, num_req_channels);
876
877 out_buff = buffer;
878 /* Num Channels conversion */
879 if (num_device_channels != num_req_channels) {
880 audio_format_t audio_format = in_get_format(&(in->stream.common));
881 unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
882
883 num_read_buff_bytes =
884 adjust_channels(read_buff, num_device_channels,
885 out_buff, num_req_channels,
886 sample_size_in_bytes, num_read_buff_bytes);
887 }
888 }
Eric Laurent253def92014-09-14 12:18:18 -0700889
Paul McLean76dba682016-06-01 12:29:11 -0600890 /* no need to acquire in->adev->lock to read mic_muted here as we don't change its state */
891 if (num_read_buff_bytes > 0 && in->adev->mic_muted)
Eric Laurent253def92014-09-14 12:18:18 -0700892 memset(buffer, 0, num_read_buff_bytes);
Viswanath L8c7e1112014-10-31 13:07:39 +0530893 } else {
Eric Laurente6499422015-01-09 17:03:27 -0800894 num_read_buff_bytes = 0; // reset the value after USB headset is unplugged
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700895 }
896
897err:
898 pthread_mutex_unlock(&in->lock);
899
900 return num_read_buff_bytes;
901}
902
903static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
904{
905 return 0;
906}
907
Paul McLean76dba682016-06-01 12:29:11 -0600908static int adev_open_input_stream(struct audio_hw_device *hw_dev,
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700909 audio_io_handle_t handle,
Paul McLean76dba682016-06-01 12:29:11 -0600910 audio_devices_t devicesSpec __unused,
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700911 struct audio_config *config,
912 struct audio_stream_in **stream_in,
Eric Laurentf5e24692014-07-27 16:14:57 -0700913 audio_input_flags_t flags __unused,
Paul McLean0f1753e2014-12-02 12:36:45 -0700914 const char *address /*__unused*/,
Eric Laurentf5e24692014-07-27 16:14:57 -0700915 audio_source_t source __unused)
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700916{
Paul McLean1d585cc2016-05-24 11:28:10 -0600917 ALOGV("adev_open_input_stream() rate:%" PRIu32 ", chanMask:0x%" PRIX32 ", fmt:%" PRIu8,
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700918 config->sample_rate, config->channel_mask, config->format);
919
920 struct stream_in *in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
921 int ret = 0;
922
Paul McLean76dba682016-06-01 12:29:11 -0600923 if (in == NULL) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700924 return -ENOMEM;
Paul McLean76dba682016-06-01 12:29:11 -0600925 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700926
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 McLean76dba682016-06-01 12:29:11 -0600948 in->adev = (struct audio_device *)hw_dev;
949 pthread_mutex_lock(&in->adev->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700950
Paul McLean76dba682016-06-01 12:29:11 -0600951 in->profile = &in->adev->in_profile;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700952
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
Paul McLean76dba682016-06-01 12:29:11 -0600966 if (in->adev->device_sample_rate != 0 && /* we are playing, so lock the rate */
967 in->adev->device_sample_rate >= RATELOCK_THRESHOLD) {/* but only for high sample rates */
968 ret = config->sample_rate != in->adev->device_sample_rate ? -EINVAL : 0;
969 proxy_config.rate = config->sample_rate = in->adev->device_sample_rate;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700970 } else if (profile_is_sample_rate_valid(in->profile, config->sample_rate)) {
Paul McLean76dba682016-06-01 12:29:11 -0600971 in->adev->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 McLean76dba682016-06-01 12:29:11 -0600976 pthread_mutex_unlock(&in->adev->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() */
Paul McLean76dba682016-06-01 12:29:11 -06001018 adev_add_stream_to_list(in->adev, &in->adev->input_stream_list, &in->list_node);
Paul McLean6a75e4e2016-05-25 14:09:02 -06001019
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
Paul McLean76dba682016-06-01 12:29:11 -06001028static void adev_close_input_stream(struct audio_hw_device *hw_dev,
1029 struct audio_stream_in *stream)
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001030{
1031 struct stream_in *in = (struct stream_in *)stream;
Paul McLean6a75e4e2016-05-25 14:09:02 -06001032 ALOGV("adev_close_input_stream(c:%d d:%d)", in->profile->card, in->profile->device);
1033
Paul McLean76dba682016-06-01 12:29:11 -06001034 adev_remove_stream_from_list(in->adev, &in->list_node);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001035
1036 /* Close the pcm device */
1037 in_standby(&stream->common);
1038
1039 free(in->conversion_buffer);
1040
1041 free(stream);
1042}
1043
1044/*
1045 * ADEV Functions
1046 */
Paul McLean76dba682016-06-01 12:29:11 -06001047static int adev_set_parameters(struct audio_hw_device *hw_dev, const char *kvpairs)
Simon Wilson19957a32012-04-06 16:17:12 -07001048{
1049 return 0;
1050}
1051
Paul McLean76dba682016-06-01 12:29:11 -06001052static char * adev_get_parameters(const struct audio_hw_device *hw_dev, const char *keys)
Simon Wilson19957a32012-04-06 16:17:12 -07001053{
1054 return strdup("");
1055}
1056
Paul McLean76dba682016-06-01 12:29:11 -06001057static int adev_init_check(const struct audio_hw_device *hw_dev)
Simon Wilson19957a32012-04-06 16:17:12 -07001058{
1059 return 0;
1060}
1061
Paul McLean76dba682016-06-01 12:29:11 -06001062static int adev_set_voice_volume(struct audio_hw_device *hw_dev, float volume)
Simon Wilson19957a32012-04-06 16:17:12 -07001063{
1064 return -ENOSYS;
1065}
1066
Paul McLean76dba682016-06-01 12:29:11 -06001067static int adev_set_master_volume(struct audio_hw_device *hw_dev, float volume)
Simon Wilson19957a32012-04-06 16:17:12 -07001068{
1069 return -ENOSYS;
1070}
1071
Paul McLean76dba682016-06-01 12:29:11 -06001072static int adev_set_mode(struct audio_hw_device *hw_dev, audio_mode_t mode)
Simon Wilson19957a32012-04-06 16:17:12 -07001073{
1074 return 0;
1075}
1076
Paul McLean76dba682016-06-01 12:29:11 -06001077static int adev_set_mic_mute(struct audio_hw_device *hw_dev, bool state)
Simon Wilson19957a32012-04-06 16:17:12 -07001078{
Paul McLean76dba682016-06-01 12:29:11 -06001079 struct audio_device * adev = (struct audio_device *)hw_dev;
Eric Laurent253def92014-09-14 12:18:18 -07001080 pthread_mutex_lock(&adev->lock);
1081 adev->mic_muted = state;
1082 pthread_mutex_unlock(&adev->lock);
Simon Wilson19957a32012-04-06 16:17:12 -07001083 return -ENOSYS;
1084}
1085
Paul McLean76dba682016-06-01 12:29:11 -06001086static int adev_get_mic_mute(const struct audio_hw_device *hw_dev, bool *state)
Simon Wilson19957a32012-04-06 16:17:12 -07001087{
1088 return -ENOSYS;
1089}
1090
Paul McLean76dba682016-06-01 12:29:11 -06001091static int adev_dump(const struct audio_hw_device *device, int fd)
Simon Wilson19957a32012-04-06 16:17:12 -07001092{
Paul McLean6a75e4e2016-05-25 14:09:02 -06001093 dprintf(fd, "\nUSB audio module:\n");
1094
1095 struct audio_device* adev = (struct audio_device*)device;
1096 const int kNumRetries = 3;
1097 const int kSleepTimeMS = 500;
1098
1099 // use pthread_mutex_trylock() in case we dumpsys during a deadlock
1100 int retry = kNumRetries;
1101 while (retry > 0 && pthread_mutex_trylock(&adev->lock) != 0) {
1102 sleep(kSleepTimeMS);
1103 retry--;
1104 }
1105
1106 if (retry > 0) {
1107 if (list_empty(&adev->output_stream_list)) {
1108 dprintf(fd, " No output streams.\n");
1109 } else {
1110 struct listnode* node;
1111 list_for_each(node, &adev->output_stream_list) {
1112 struct audio_stream* stream =
1113 (struct audio_stream *)node_to_item(node, struct stream_out, list_node);
1114 out_dump(stream, fd);
1115 }
1116 }
1117
1118 if (list_empty(&adev->input_stream_list)) {
1119 dprintf(fd, "\n No input streams.\n");
1120 } else {
1121 struct listnode* node;
1122 list_for_each(node, &adev->input_stream_list) {
1123 struct audio_stream* stream =
1124 (struct audio_stream *)node_to_item(node, struct stream_in, list_node);
1125 in_dump(stream, fd);
1126 }
1127 }
1128
1129 pthread_mutex_unlock(&adev->lock);
1130 } else {
1131 // Couldn't lock
1132 dprintf(fd, " Could not obtain device lock.\n");
1133 }
1134
Simon Wilson19957a32012-04-06 16:17:12 -07001135 return 0;
1136}
1137
1138static int adev_close(hw_device_t *device)
1139{
Paul McLeaneedc92e2013-12-19 15:46:15 -08001140 struct audio_device *adev = (struct audio_device *)device;
Simon Wilson19957a32012-04-06 16:17:12 -07001141 free(device);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001142
Simon Wilson19957a32012-04-06 16:17:12 -07001143 return 0;
1144}
1145
Paul McLean30f41852014-04-16 15:44:20 -07001146static int adev_open(const hw_module_t* module, const char* name, hw_device_t** device)
Simon Wilson19957a32012-04-06 16:17:12 -07001147{
Simon Wilson19957a32012-04-06 16:17:12 -07001148 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
1149 return -EINVAL;
1150
Paul McLeaneedc92e2013-12-19 15:46:15 -08001151 struct audio_device *adev = calloc(1, sizeof(struct audio_device));
Simon Wilson19957a32012-04-06 16:17:12 -07001152 if (!adev)
1153 return -ENOMEM;
1154
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001155 profile_init(&adev->out_profile, PCM_OUT);
1156 profile_init(&adev->in_profile, PCM_IN);
1157
Paul McLean6a75e4e2016-05-25 14:09:02 -06001158 list_init(&adev->output_stream_list);
1159 list_init(&adev->input_stream_list);
1160
Simon Wilson19957a32012-04-06 16:17:12 -07001161 adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
Eric Laurent85e08e22012-08-28 14:30:35 -07001162 adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001163 adev->hw_device.common.module = (struct hw_module_t *)module;
Simon Wilson19957a32012-04-06 16:17:12 -07001164 adev->hw_device.common.close = adev_close;
1165
Simon Wilson19957a32012-04-06 16:17:12 -07001166 adev->hw_device.init_check = adev_init_check;
1167 adev->hw_device.set_voice_volume = adev_set_voice_volume;
1168 adev->hw_device.set_master_volume = adev_set_master_volume;
1169 adev->hw_device.set_mode = adev_set_mode;
1170 adev->hw_device.set_mic_mute = adev_set_mic_mute;
1171 adev->hw_device.get_mic_mute = adev_get_mic_mute;
1172 adev->hw_device.set_parameters = adev_set_parameters;
1173 adev->hw_device.get_parameters = adev_get_parameters;
1174 adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size;
1175 adev->hw_device.open_output_stream = adev_open_output_stream;
1176 adev->hw_device.close_output_stream = adev_close_output_stream;
1177 adev->hw_device.open_input_stream = adev_open_input_stream;
1178 adev->hw_device.close_input_stream = adev_close_input_stream;
1179 adev->hw_device.dump = adev_dump;
1180
1181 *device = &adev->hw_device.common;
1182
1183 return 0;
1184}
1185
1186static struct hw_module_methods_t hal_module_methods = {
1187 .open = adev_open,
1188};
1189
1190struct audio_module HAL_MODULE_INFO_SYM = {
1191 .common = {
1192 .tag = HARDWARE_MODULE_TAG,
Mike Lockwood46a98092012-04-24 16:41:18 -07001193 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
1194 .hal_api_version = HARDWARE_HAL_API_VERSION,
Simon Wilson19957a32012-04-06 16:17:12 -07001195 .id = AUDIO_HARDWARE_MODULE_ID,
1196 .name = "USB audio HW HAL",
1197 .author = "The Android Open Source Project",
1198 .methods = &hal_module_methods,
1199 },
1200};