blob: 6a3c5dafcfa35c4e9b936ce62992956aa475314d [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
Paul McLeanc88e6ae2014-07-16 09:48:34 -070042#include "alsa_device_profile.h"
43#include "alsa_device_proxy.h"
Paul McLean9ab869a2015-01-13 09:37:06 -080044#include "alsa_logging.h"
Paul McLeanf62d75e2014-07-11 15:14:19 -070045
Paul McLeanc88e6ae2014-07-16 09:48:34 -070046#define DEFAULT_INPUT_BUFFER_SIZE_MS 20
Paul McLeanf62d75e2014-07-11 15:14:19 -070047
Paul McLean1d585cc2016-05-24 11:28:10 -060048/* Lock play & record samples rates at or above this threshold */
49#define RATELOCK_THRESHOLD 96000
50
Simon Wilson19957a32012-04-06 16:17:12 -070051struct audio_device {
52 struct audio_hw_device hw_device;
53
54 pthread_mutex_t lock; /* see note below on mutex acquisition order */
Paul McLeaneedc92e2013-12-19 15:46:15 -080055
56 /* output */
Paul McLeanc88e6ae2014-07-16 09:48:34 -070057 alsa_device_profile out_profile;
Paul McLean6a75e4e2016-05-25 14:09:02 -060058 struct listnode output_stream_list;
Paul McLeaneedc92e2013-12-19 15:46:15 -080059
60 /* input */
Paul McLeanc88e6ae2014-07-16 09:48:34 -070061 alsa_device_profile in_profile;
Paul McLean6a75e4e2016-05-25 14:09:02 -060062 struct listnode input_stream_list;
Paul McLeaneedc92e2013-12-19 15:46:15 -080063
Paul McLean1d585cc2016-05-24 11:28:10 -060064 /* lock input & output sample rates */
65 /*FIXME - How do we address multiple output streams? */
66 uint32_t device_sample_rate;
67
Eric Laurent253def92014-09-14 12:18:18 -070068 bool mic_muted;
69
Simon Wilson19957a32012-04-06 16:17:12 -070070 bool standby;
71};
72
Paul McLean994ac072016-06-02 15:33:24 -060073struct stream_lock {
74 pthread_mutex_t lock; /* see note below on mutex acquisition order */
75 pthread_mutex_t pre_lock; /* acquire before lock to avoid DOS by playback thread */
76};
77
Simon Wilson19957a32012-04-06 16:17:12 -070078struct stream_out {
79 struct audio_stream_out stream;
80
Paul McLean994ac072016-06-02 15:33:24 -060081 struct stream_lock lock;
82
Paul McLeaneedc92e2013-12-19 15:46:15 -080083 bool standby;
84
Paul McLean76dba682016-06-01 12:29:11 -060085 struct audio_device *adev; /* hardware information - only using this for the lock */
Paul McLeanc88e6ae2014-07-16 09:48:34 -070086
Paul McLean65ec72b2015-02-18 09:13:24 -080087 alsa_device_profile * profile; /* Points to the alsa_device_profile in the audio_device */
Paul McLeanc88e6ae2014-07-16 09:48:34 -070088 alsa_device_proxy proxy; /* state of the stream */
Paul McLeaneedc92e2013-12-19 15:46:15 -080089
Andy Hung03576be2014-07-21 21:16:45 -070090 unsigned hal_channel_count; /* channel count exposed to AudioFlinger.
91 * This may differ from the device channel count when
92 * the device is not compatible with AudioFlinger
93 * capabilities, e.g. exposes too many channels or
94 * too few channels. */
Paul McLean64345f82016-06-06 13:26:05 -060095 audio_channel_mask_t hal_channel_mask; /* USB devices deal in channel counts, not masks
96 * so the proxy doesn't have a channel_mask, but
97 * audio HALs need to talk about channel masks
98 * so expose the one calculated by
99 * adev_open_output_stream */
Andy Hung182ddc72015-05-05 23:37:30 -0700100
Paul McLean6a75e4e2016-05-25 14:09:02 -0600101 struct listnode list_node;
102
Paul McLeaneedc92e2013-12-19 15:46:15 -0800103 void * conversion_buffer; /* any conversions are put into here
104 * they could come from here too if
105 * there was a previous conversion */
106 size_t conversion_buffer_size; /* in bytes */
107};
108
Paul McLeaneedc92e2013-12-19 15:46:15 -0800109struct stream_in {
110 struct audio_stream_in stream;
111
Paul McLean994ac072016-06-02 15:33:24 -0600112 struct stream_lock lock;
113
Simon Wilson19957a32012-04-06 16:17:12 -0700114 bool standby;
115
Paul McLean76dba682016-06-01 12:29:11 -0600116 struct audio_device *adev; /* hardware information - only using this for the lock */
Paul McLeaneedc92e2013-12-19 15:46:15 -0800117
Paul McLean65ec72b2015-02-18 09:13:24 -0800118 alsa_device_profile * profile; /* Points to the alsa_device_profile in the audio_device */
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700119 alsa_device_proxy proxy; /* state of the stream */
Paul McLeanf62d75e2014-07-11 15:14:19 -0700120
Paul McLean2cfd81b2014-09-15 12:32:23 -0700121 unsigned hal_channel_count; /* channel count exposed to AudioFlinger.
122 * This may differ from the device channel count when
123 * the device is not compatible with AudioFlinger
124 * capabilities, e.g. exposes too many channels or
125 * too few channels. */
Paul McLean64345f82016-06-06 13:26:05 -0600126 audio_channel_mask_t hal_channel_mask; /* USB devices deal in channel counts, not masks
127 * so the proxy doesn't have a channel_mask, but
128 * audio HALs need to talk about channel masks
129 * so expose the one calculated by
130 * adev_open_input_stream */
Andy Hung780f1f82015-04-22 22:14:39 -0700131
Paul McLean6a75e4e2016-05-25 14:09:02 -0600132 struct listnode list_node;
133
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700134 /* 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 -0700135 void * conversion_buffer; /* any conversions are put into here
136 * they could come from here too if
137 * there was a previous conversion */
138 size_t conversion_buffer_size; /* in bytes */
Simon Wilson19957a32012-04-06 16:17:12 -0700139};
140
Paul McLean994ac072016-06-02 15:33:24 -0600141/*
142 * Locking Helpers
143 */
Paul McLeaneedc92e2013-12-19 15:46:15 -0800144/*
Eric Laurent70318092015-06-19 17:49:17 -0700145 * NOTE: when multiple mutexes have to be acquired, always take the
146 * stream_in or stream_out mutex first, followed by the audio_device mutex.
147 * stream pre_lock is always acquired before stream lock to prevent starvation of control thread by
148 * higher priority playback or capture thread.
149 */
150
Paul McLean994ac072016-06-02 15:33:24 -0600151static void stream_lock_init(struct stream_lock *lock) {
152 pthread_mutex_init(&lock->lock, (const pthread_mutexattr_t *) NULL);
153 pthread_mutex_init(&lock->pre_lock, (const pthread_mutexattr_t *) NULL);
154}
155
156static void stream_lock(struct stream_lock *lock) {
157 pthread_mutex_lock(&lock->pre_lock);
158 pthread_mutex_lock(&lock->lock);
159 pthread_mutex_unlock(&lock->pre_lock);
160}
161
162static void stream_unlock(struct stream_lock *lock) {
163 pthread_mutex_unlock(&lock->lock);
164}
165
166static void device_lock(struct audio_device *adev) {
167 pthread_mutex_lock(&adev->lock);
168}
169
170static int device_try_lock(struct audio_device *adev) {
171 return pthread_mutex_trylock(&adev->lock);
172}
173
174static void device_unlock(struct audio_device *adev) {
175 pthread_mutex_unlock(&adev->lock);
176}
177
178/*
179 * streams list management
180 */
181static void adev_add_stream_to_list(
182 struct audio_device* adev, struct listnode* list, struct listnode* stream_node) {
183 device_lock(adev);
184
185 list_add_tail(list, stream_node);
186
187 device_unlock(adev);
188}
189
190static void adev_remove_stream_from_list(
191 struct audio_device* adev, struct listnode* stream_node) {
192 device_lock(adev);
193
194 list_remove(stream_node);
195
196 device_unlock(adev);
197}
198
Eric Laurent70318092015-06-19 17:49:17 -0700199/*
Paul McLean0f1753e2014-12-02 12:36:45 -0700200 * Extract the card and device numbers from the supplied key/value pairs.
201 * kvpairs A null-terminated string containing the key/value pairs or card and device.
202 * i.e. "card=1;device=42"
203 * card A pointer to a variable to receive the parsed-out card number.
204 * device A pointer to a variable to receive the parsed-out device number.
205 * NOTE: The variables pointed to by card and device return -1 (undefined) if the
206 * associated key/value pair is not found in the provided string.
Paul McLean65ec72b2015-02-18 09:13:24 -0800207 * Return true if the kvpairs string contain a card/device spec, false otherwise.
Paul McLean0f1753e2014-12-02 12:36:45 -0700208 */
Paul McLean65ec72b2015-02-18 09:13:24 -0800209static bool parse_card_device_params(const char *kvpairs, int *card, int *device)
Paul McLean0f1753e2014-12-02 12:36:45 -0700210{
211 struct str_parms * parms = str_parms_create_str(kvpairs);
212 char value[32];
213 int param_val;
214
215 // initialize to "undefined" state.
216 *card = -1;
217 *device = -1;
218
219 param_val = str_parms_get_str(parms, "card", value, sizeof(value));
220 if (param_val >= 0) {
221 *card = atoi(value);
222 }
223
224 param_val = str_parms_get_str(parms, "device", value, sizeof(value));
225 if (param_val >= 0) {
226 *device = atoi(value);
227 }
228
229 str_parms_destroy(parms);
Paul McLean65ec72b2015-02-18 09:13:24 -0800230
231 return *card >= 0 && *device >= 0;
Paul McLean0f1753e2014-12-02 12:36:45 -0700232}
233
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700234static char * device_get_parameters(alsa_device_profile * profile, const char * keys)
Paul McLeaneedc92e2013-12-19 15:46:15 -0800235{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700236 if (profile->card < 0 || profile->device < 0) {
237 return strdup("");
Paul McLeaneedc92e2013-12-19 15:46:15 -0800238 }
Paul McLeane32cbc12014-06-25 10:42:07 -0700239
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700240 struct str_parms *query = str_parms_create_str(keys);
241 struct str_parms *result = str_parms_create();
Paul McLeane32cbc12014-06-25 10:42:07 -0700242
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700243 /* These keys are from hardware/libhardware/include/audio.h */
244 /* supported sample rates */
245 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) {
246 char* rates_list = profile_get_sample_rate_strs(profile);
247 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES,
248 rates_list);
249 free(rates_list);
Paul McLeane32cbc12014-06-25 10:42:07 -0700250 }
251
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700252 /* supported channel counts */
253 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) {
254 char* channels_list = profile_get_channel_count_strs(profile);
255 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_CHANNELS,
256 channels_list);
257 free(channels_list);
Paul McLeane32cbc12014-06-25 10:42:07 -0700258 }
259
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700260 /* supported sample formats */
261 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) {
262 char * format_params = profile_get_format_strs(profile);
263 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_FORMATS,
264 format_params);
265 free(format_params);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700266 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700267 str_parms_destroy(query);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700268
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700269 char* result_str = str_parms_to_str(result);
270 str_parms_destroy(result);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700271
Paul McLean65ec72b2015-02-18 09:13:24 -0800272 ALOGV("device_get_parameters = %s", result_str);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700273
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700274 return result_str;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800275}
276
277/*
278 * HAl Functions
279 */
Simon Wilson19957a32012-04-06 16:17:12 -0700280/**
281 * NOTE: when multiple mutexes have to be acquired, always respect the
282 * following order: hw device > out stream
283 */
284
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700285/*
286 * OUT functions
287 */
Simon Wilson19957a32012-04-06 16:17:12 -0700288static uint32_t out_get_sample_rate(const struct audio_stream *stream)
289{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700290 uint32_t rate = proxy_get_sample_rate(&((struct stream_out*)stream)->proxy);
291 ALOGV("out_get_sample_rate() = %d", rate);
292 return rate;
Simon Wilson19957a32012-04-06 16:17:12 -0700293}
294
295static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
296{
297 return 0;
298}
299
300static size_t out_get_buffer_size(const struct audio_stream *stream)
301{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700302 const struct stream_out* out = (const struct stream_out*)stream;
303 size_t buffer_size =
304 proxy_get_period_size(&out->proxy) * audio_stream_out_frame_size(&(out->stream));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700305 return buffer_size;
Simon Wilson19957a32012-04-06 16:17:12 -0700306}
307
308static uint32_t out_get_channels(const struct audio_stream *stream)
309{
Andy Hung03576be2014-07-21 21:16:45 -0700310 const struct stream_out *out = (const struct stream_out*)stream;
Andy Hung182ddc72015-05-05 23:37:30 -0700311 return out->hal_channel_mask;
Simon Wilson19957a32012-04-06 16:17:12 -0700312}
313
314static audio_format_t out_get_format(const struct audio_stream *stream)
315{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700316 /* Note: The HAL doesn't do any FORMAT conversion at this time. It
317 * Relies on the framework to provide data in the specified format.
318 * This could change in the future.
319 */
320 alsa_device_proxy * proxy = &((struct stream_out*)stream)->proxy;
321 audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700322 return format;
Simon Wilson19957a32012-04-06 16:17:12 -0700323}
324
325static int out_set_format(struct audio_stream *stream, audio_format_t format)
326{
327 return 0;
328}
329
330static int out_standby(struct audio_stream *stream)
331{
332 struct stream_out *out = (struct stream_out *)stream;
333
Paul McLean994ac072016-06-02 15:33:24 -0600334 stream_lock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700335 if (!out->standby) {
Paul McLean994ac072016-06-02 15:33:24 -0600336 device_lock(out->adev);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700337 proxy_close(&out->proxy);
Paul McLean994ac072016-06-02 15:33:24 -0600338 device_unlock(out->adev);
Simon Wilson19957a32012-04-06 16:17:12 -0700339 out->standby = true;
340 }
Paul McLean994ac072016-06-02 15:33:24 -0600341 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700342 return 0;
343}
344
Paul McLean6a75e4e2016-05-25 14:09:02 -0600345static int out_dump(const struct audio_stream *stream, int fd) {
346 const struct stream_out* out_stream = (const struct stream_out*) stream;
347
348 if (out_stream != NULL) {
349 dprintf(fd, "Output Profile:\n");
350 profile_dump(out_stream->profile, fd);
351
352 dprintf(fd, "Output Proxy:\n");
353 proxy_dump(&out_stream->proxy, fd);
354 }
355
Simon Wilson19957a32012-04-06 16:17:12 -0700356 return 0;
357}
358
359static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
360{
Paul McLean65ec72b2015-02-18 09:13:24 -0800361 ALOGV("out_set_parameters() keys:%s", kvpairs);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800362
Simon Wilson19957a32012-04-06 16:17:12 -0700363 struct stream_out *out = (struct stream_out *)stream;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700364
Simon Wilson19957a32012-04-06 16:17:12 -0700365 int routing = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800366 int ret_value = 0;
Eric Laurent05333d42014-08-04 20:29:17 -0700367 int card = -1;
368 int device = -1;
Simon Wilson19957a32012-04-06 16:17:12 -0700369
Paul McLean65ec72b2015-02-18 09:13:24 -0800370 if (!parse_card_device_params(kvpairs, &card, &device)) {
371 // nothing to do
372 return ret_value;
373 }
374
Paul McLean994ac072016-06-02 15:33:24 -0600375 stream_lock(&out->lock);
Paul McLean65ec72b2015-02-18 09:13:24 -0800376 /* Lock the device because that is where the profile lives */
Paul McLean994ac072016-06-02 15:33:24 -0600377 device_lock(out->adev);
Simon Wilson19957a32012-04-06 16:17:12 -0700378
Paul McLean65ec72b2015-02-18 09:13:24 -0800379 if (!profile_is_cached_for(out->profile, card, device)) {
Eric Laurent05333d42014-08-04 20:29:17 -0700380 /* cannot read pcm device info if playback is active */
381 if (!out->standby)
382 ret_value = -ENOSYS;
383 else {
384 int saved_card = out->profile->card;
385 int saved_device = out->profile->device;
386 out->profile->card = card;
387 out->profile->device = device;
388 ret_value = profile_read_device_info(out->profile) ? 0 : -EINVAL;
389 if (ret_value != 0) {
390 out->profile->card = saved_card;
391 out->profile->device = saved_device;
392 }
393 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800394 }
Paul McLean2c6196f2014-08-20 16:50:25 -0700395
Paul McLean994ac072016-06-02 15:33:24 -0600396 device_unlock(out->adev);
397 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700398
Paul McLeaneedc92e2013-12-19 15:46:15 -0800399 return ret_value;
Simon Wilson19957a32012-04-06 16:17:12 -0700400}
401
Paul McLeanf62d75e2014-07-11 15:14:19 -0700402static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
403{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700404 struct stream_out *out = (struct stream_out *)stream;
Paul McLean994ac072016-06-02 15:33:24 -0600405 stream_lock(&out->lock);
406 device_lock(out->adev);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700407
408 char * params_str = device_get_parameters(out->profile, keys);
409
Paul McLean994ac072016-06-02 15:33:24 -0600410 device_unlock(out->adev);
411 stream_unlock(&out->lock);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700412 return params_str;
413}
414
Simon Wilson19957a32012-04-06 16:17:12 -0700415static uint32_t out_get_latency(const struct audio_stream_out *stream)
416{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700417 alsa_device_proxy * proxy = &((struct stream_out*)stream)->proxy;
418 return proxy_get_latency(proxy);
Simon Wilson19957a32012-04-06 16:17:12 -0700419}
420
Paul McLean30f41852014-04-16 15:44:20 -0700421static int out_set_volume(struct audio_stream_out *stream, float left, float right)
Simon Wilson19957a32012-04-06 16:17:12 -0700422{
423 return -ENOSYS;
424}
425
Paul McLeaneedc92e2013-12-19 15:46:15 -0800426/* must be called with hw device and output stream mutexes locked */
427static int start_output_stream(struct stream_out *out)
428{
Paul McLean65ec72b2015-02-18 09:13:24 -0800429 ALOGV("start_output_stream(card:%d device:%d)", out->profile->card, out->profile->device);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800430
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700431 return proxy_open(&out->proxy);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800432}
433
434static ssize_t out_write(struct audio_stream_out *stream, const void* buffer, size_t bytes)
Simon Wilson19957a32012-04-06 16:17:12 -0700435{
436 int ret;
437 struct stream_out *out = (struct stream_out *)stream;
438
Paul McLean994ac072016-06-02 15:33:24 -0600439 stream_lock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700440 if (out->standby) {
Paul McLean994ac072016-06-02 15:33:24 -0600441 device_lock(out->adev);
Simon Wilson19957a32012-04-06 16:17:12 -0700442 ret = start_output_stream(out);
Paul McLean994ac072016-06-02 15:33:24 -0600443 device_unlock(out->adev);
Simon Wilson19957a32012-04-06 16:17:12 -0700444 if (ret != 0) {
445 goto err;
446 }
447 out->standby = false;
448 }
Eric Laurent05333d42014-08-04 20:29:17 -0700449
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700450 alsa_device_proxy* proxy = &out->proxy;
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700451 const void * write_buff = buffer;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800452 int num_write_buff_bytes = bytes;
Andy Hung03576be2014-07-21 21:16:45 -0700453 const int num_device_channels = proxy_get_channel_count(proxy); /* what we told alsa */
454 const int num_req_channels = out->hal_channel_count; /* what we told AudioFlinger */
Paul McLean30f41852014-04-16 15:44:20 -0700455 if (num_device_channels != num_req_channels) {
Andy Hung03576be2014-07-21 21:16:45 -0700456 /* allocate buffer */
457 const size_t required_conversion_buffer_size =
458 bytes * num_device_channels / num_req_channels;
459 if (required_conversion_buffer_size > out->conversion_buffer_size) {
460 out->conversion_buffer_size = required_conversion_buffer_size;
461 out->conversion_buffer = realloc(out->conversion_buffer,
462 out->conversion_buffer_size);
463 }
464 /* convert data */
465 const audio_format_t audio_format = out_get_format(&(out->stream.common));
466 const unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800467 num_write_buff_bytes =
Andy Hung03576be2014-07-21 21:16:45 -0700468 adjust_channels(write_buff, num_req_channels,
469 out->conversion_buffer, num_device_channels,
470 sample_size_in_bytes, num_write_buff_bytes);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800471 write_buff = out->conversion_buffer;
472 }
473
Paul McLeaneedc92e2013-12-19 15:46:15 -0800474 if (write_buff != NULL && num_write_buff_bytes != 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700475 proxy_write(&out->proxy, write_buff, num_write_buff_bytes);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800476 }
Simon Wilson19957a32012-04-06 16:17:12 -0700477
Paul McLean994ac072016-06-02 15:33:24 -0600478 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700479
480 return bytes;
481
482err:
Paul McLean994ac072016-06-02 15:33:24 -0600483 stream_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700484 if (ret != 0) {
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700485 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
Simon Wilson19957a32012-04-06 16:17:12 -0700486 out_get_sample_rate(&stream->common));
487 }
488
489 return bytes;
490}
491
Paul McLean30f41852014-04-16 15:44:20 -0700492static int out_get_render_position(const struct audio_stream_out *stream, uint32_t *dsp_frames)
Simon Wilson19957a32012-04-06 16:17:12 -0700493{
494 return -EINVAL;
495}
496
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700497static int out_get_presentation_position(const struct audio_stream_out *stream,
498 uint64_t *frames, struct timespec *timestamp)
499{
Andy Hungc9515ce2015-08-04 15:05:19 -0700500 struct stream_out *out = (struct stream_out *)stream; // discard const qualifier
Paul McLean994ac072016-06-02 15:33:24 -0600501 stream_lock(&out->lock);
Andy Hungc9515ce2015-08-04 15:05:19 -0700502
503 const alsa_device_proxy *proxy = &out->proxy;
504 const int ret = proxy_get_presentation_position(proxy, frames, timestamp);
505
Paul McLean994ac072016-06-02 15:33:24 -0600506 stream_unlock(&out->lock);
Andy Hungc9515ce2015-08-04 15:05:19 -0700507 return ret;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700508}
509
Simon Wilson19957a32012-04-06 16:17:12 -0700510static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
511{
512 return 0;
513}
514
515static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
516{
517 return 0;
518}
519
Paul McLean30f41852014-04-16 15:44:20 -0700520static int out_get_next_write_timestamp(const struct audio_stream_out *stream, int64_t *timestamp)
Simon Wilson19957a32012-04-06 16:17:12 -0700521{
522 return -EINVAL;
523}
524
Paul McLean76dba682016-06-01 12:29:11 -0600525static int adev_open_output_stream(struct audio_hw_device *hw_dev,
Mike Lockwood46a98092012-04-24 16:41:18 -0700526 audio_io_handle_t handle,
Paul McLean76dba682016-06-01 12:29:11 -0600527 audio_devices_t devicesSpec __unused,
Mike Lockwood46a98092012-04-24 16:41:18 -0700528 audio_output_flags_t flags,
529 struct audio_config *config,
Eric Laurentf5e24692014-07-27 16:14:57 -0700530 struct audio_stream_out **stream_out,
Paul McLean0f1753e2014-12-02 12:36:45 -0700531 const char *address /*__unused*/)
Simon Wilson19957a32012-04-06 16:17:12 -0700532{
Paul McLean76dba682016-06-01 12:29:11 -0600533 ALOGV("adev_open_output_stream() handle:0x%X, devicesSpec:0x%X, flags:0x%X, addr:%s",
534 handle, devicesSpec, flags, address);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800535
Simon Wilson19957a32012-04-06 16:17:12 -0700536 struct stream_out *out;
Simon Wilson19957a32012-04-06 16:17:12 -0700537
538 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
Paul McLean76dba682016-06-01 12:29:11 -0600539 if (out == NULL) {
Simon Wilson19957a32012-04-06 16:17:12 -0700540 return -ENOMEM;
Paul McLean76dba682016-06-01 12:29:11 -0600541 }
Simon Wilson19957a32012-04-06 16:17:12 -0700542
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700543 /* setup function pointers */
Simon Wilson19957a32012-04-06 16:17:12 -0700544 out->stream.common.get_sample_rate = out_get_sample_rate;
545 out->stream.common.set_sample_rate = out_set_sample_rate;
546 out->stream.common.get_buffer_size = out_get_buffer_size;
547 out->stream.common.get_channels = out_get_channels;
548 out->stream.common.get_format = out_get_format;
549 out->stream.common.set_format = out_set_format;
550 out->stream.common.standby = out_standby;
551 out->stream.common.dump = out_dump;
552 out->stream.common.set_parameters = out_set_parameters;
553 out->stream.common.get_parameters = out_get_parameters;
554 out->stream.common.add_audio_effect = out_add_audio_effect;
555 out->stream.common.remove_audio_effect = out_remove_audio_effect;
556 out->stream.get_latency = out_get_latency;
557 out->stream.set_volume = out_set_volume;
558 out->stream.write = out_write;
559 out->stream.get_render_position = out_get_render_position;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700560 out->stream.get_presentation_position = out_get_presentation_position;
Simon Wilson19957a32012-04-06 16:17:12 -0700561 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
562
Paul McLean994ac072016-06-02 15:33:24 -0600563 stream_lock_init(&out->lock);
Eric Laurent70318092015-06-19 17:49:17 -0700564
Paul McLean76dba682016-06-01 12:29:11 -0600565 out->adev = (struct audio_device *)hw_dev;
Paul McLean994ac072016-06-02 15:33:24 -0600566 device_lock(out->adev);
Paul McLean76dba682016-06-01 12:29:11 -0600567 out->profile = &out->adev->out_profile;
Paul McLeanf62d75e2014-07-11 15:14:19 -0700568
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700569 // build this to hand to the alsa_device_proxy
570 struct pcm_config proxy_config;
Paul McLean2c6196f2014-08-20 16:50:25 -0700571 memset(&proxy_config, 0, sizeof(proxy_config));
Paul McLeaneedc92e2013-12-19 15:46:15 -0800572
Paul McLean0f1753e2014-12-02 12:36:45 -0700573 /* Pull out the card/device pair */
574 parse_card_device_params(address, &(out->profile->card), &(out->profile->device));
575
576 profile_read_device_info(out->profile);
577
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700578 int ret = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800579
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700580 /* Rate */
581 if (config->sample_rate == 0) {
582 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(out->profile);
583 } else if (profile_is_sample_rate_valid(out->profile, config->sample_rate)) {
584 proxy_config.rate = config->sample_rate;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800585 } else {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700586 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(out->profile);
587 ret = -EINVAL;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800588 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800589
Paul McLean76dba682016-06-01 12:29:11 -0600590 out->adev->device_sample_rate = config->sample_rate;
Paul McLean994ac072016-06-02 15:33:24 -0600591 device_unlock(out->adev);
Paul McLean1d585cc2016-05-24 11:28:10 -0600592
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700593 /* Format */
594 if (config->format == AUDIO_FORMAT_DEFAULT) {
595 proxy_config.format = profile_get_default_format(out->profile);
596 config->format = audio_format_from_pcm_format(proxy_config.format);
597 } else {
598 enum pcm_format fmt = pcm_format_from_audio_format(config->format);
599 if (profile_is_format_valid(out->profile, fmt)) {
600 proxy_config.format = fmt;
601 } else {
602 proxy_config.format = profile_get_default_format(out->profile);
603 config->format = audio_format_from_pcm_format(proxy_config.format);
604 ret = -EINVAL;
605 }
606 }
607
608 /* Channels */
Paul McLean64345f82016-06-06 13:26:05 -0600609 bool calc_mask = false;
610 if (config->channel_mask == AUDIO_CHANNEL_NONE) {
611 /* query case */
612 out->hal_channel_count = profile_get_default_channel_count(out->profile);
613 calc_mask = true;
Andy Hung182ddc72015-05-05 23:37:30 -0700614 } else {
Paul McLean64345f82016-06-06 13:26:05 -0600615 /* explicit case */
616 out->hal_channel_count = audio_channel_count_from_out_mask(config->channel_mask);
Andy Hung182ddc72015-05-05 23:37:30 -0700617 }
Paul McLean698dbd72015-11-03 12:24:30 -0800618
Paul McLean64345f82016-06-06 13:26:05 -0600619 /* The Framework is currently limited to no more than this number of channels */
620 if (out->hal_channel_count > FCC_8) {
621 out->hal_channel_count = FCC_8;
622 calc_mask = true;
623 }
624
625 if (calc_mask) {
626 /* need to calculate the mask from channel count either because this is the query case
627 * or the specified mask isn't valid for this device, or is more then the FW can handle */
628 config->channel_mask = out->hal_channel_count <= FCC_2
629 /* position mask for mono and stereo*/
630 ? audio_channel_out_mask_from_count(out->hal_channel_count)
631 /* otherwise indexed */
632 : audio_channel_mask_for_index_assignment_from_count(out->hal_channel_count);
633 }
634
Andy Hung182ddc72015-05-05 23:37:30 -0700635 out->hal_channel_mask = config->channel_mask;
636
Paul McLean64345f82016-06-06 13:26:05 -0600637 // Validate the "logical" channel count against support in the "actual" profile.
638 // if they differ, choose the "actual" number of channels *closest* to the "logical".
639 // and store THAT in proxy_config.channels
640 proxy_config.channels = profile_get_closest_channel_count(out->profile, out->hal_channel_count);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700641 proxy_prepare(&out->proxy, out->profile, &proxy_config);
642
643 /* TODO The retry mechanism isn't implemented in AudioPolicyManager/AudioFlinger. */
644 ret = 0;
645
Paul McLeaneedc92e2013-12-19 15:46:15 -0800646 out->conversion_buffer = NULL;
647 out->conversion_buffer_size = 0;
Simon Wilson19957a32012-04-06 16:17:12 -0700648
649 out->standby = true;
650
Paul McLean6a75e4e2016-05-25 14:09:02 -0600651 /* Save the stream for adev_dump() */
Paul McLean76dba682016-06-01 12:29:11 -0600652 adev_add_stream_to_list(out->adev, &out->adev->output_stream_list, &out->list_node);
Paul McLean6a75e4e2016-05-25 14:09:02 -0600653
Simon Wilson19957a32012-04-06 16:17:12 -0700654 *stream_out = &out->stream;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700655
656 return ret;
Simon Wilson19957a32012-04-06 16:17:12 -0700657
658err_open:
659 free(out);
660 *stream_out = NULL;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800661 return -ENOSYS;
Simon Wilson19957a32012-04-06 16:17:12 -0700662}
663
Paul McLean76dba682016-06-01 12:29:11 -0600664static void adev_close_output_stream(struct audio_hw_device *hw_dev,
Simon Wilson19957a32012-04-06 16:17:12 -0700665 struct audio_stream_out *stream)
666{
667 struct stream_out *out = (struct stream_out *)stream;
Paul McLean65ec72b2015-02-18 09:13:24 -0800668 ALOGV("adev_close_output_stream(c:%d d:%d)", out->profile->card, out->profile->device);
Simon Wilson19957a32012-04-06 16:17:12 -0700669
Paul McLean76dba682016-06-01 12:29:11 -0600670 adev_remove_stream_from_list(out->adev, &out->list_node);
Paul McLean6a75e4e2016-05-25 14:09:02 -0600671
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700672 /* Close the pcm device */
Simon Wilson19957a32012-04-06 16:17:12 -0700673 out_standby(&stream->common);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800674
675 free(out->conversion_buffer);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700676
Paul McLeaneedc92e2013-12-19 15:46:15 -0800677 out->conversion_buffer = NULL;
678 out->conversion_buffer_size = 0;
679
Paul McLean994ac072016-06-02 15:33:24 -0600680 device_lock(out->adev);
Paul McLean76dba682016-06-01 12:29:11 -0600681 out->adev->device_sample_rate = 0;
Paul McLean994ac072016-06-02 15:33:24 -0600682 device_unlock(out->adev);
Paul McLean1d585cc2016-05-24 11:28:10 -0600683
Simon Wilson19957a32012-04-06 16:17:12 -0700684 free(stream);
685}
686
Paul McLean76dba682016-06-01 12:29:11 -0600687static size_t adev_get_input_buffer_size(const struct audio_hw_device *hw_dev,
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700688 const struct audio_config *config)
689{
690 /* TODO This needs to be calculated based on format/channels/rate */
691 return 320;
692}
693
694/*
695 * IN functions
696 */
697static uint32_t in_get_sample_rate(const struct audio_stream *stream)
698{
699 uint32_t rate = proxy_get_sample_rate(&((const struct stream_in *)stream)->proxy);
700 ALOGV("in_get_sample_rate() = %d", rate);
701 return rate;
702}
703
704static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
705{
706 ALOGV("in_set_sample_rate(%d) - NOPE", rate);
707 return -ENOSYS;
708}
709
710static size_t in_get_buffer_size(const struct audio_stream *stream)
711{
712 const struct stream_in * in = ((const struct stream_in*)stream);
Paul McLean2cfd81b2014-09-15 12:32:23 -0700713 return proxy_get_period_size(&in->proxy) * audio_stream_in_frame_size(&(in->stream));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700714}
715
716static uint32_t in_get_channels(const struct audio_stream *stream)
717{
Paul McLean2cfd81b2014-09-15 12:32:23 -0700718 const struct stream_in *in = (const struct stream_in*)stream;
Andy Hung780f1f82015-04-22 22:14:39 -0700719 return in->hal_channel_mask;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700720}
721
722static audio_format_t in_get_format(const struct audio_stream *stream)
723{
Andy Hung780f1f82015-04-22 22:14:39 -0700724 alsa_device_proxy *proxy = &((struct stream_in*)stream)->proxy;
725 audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy));
726 return format;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700727}
728
729static int in_set_format(struct audio_stream *stream, audio_format_t format)
730{
731 ALOGV("in_set_format(%d) - NOPE", format);
732
733 return -ENOSYS;
734}
735
736static int in_standby(struct audio_stream *stream)
737{
738 struct stream_in *in = (struct stream_in *)stream;
739
Paul McLean994ac072016-06-02 15:33:24 -0600740 stream_lock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700741 if (!in->standby) {
Paul McLean994ac072016-06-02 15:33:24 -0600742 device_lock(in->adev);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700743 proxy_close(&in->proxy);
Paul McLean994ac072016-06-02 15:33:24 -0600744 device_unlock(in->adev);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700745 in->standby = true;
746 }
747
Paul McLean994ac072016-06-02 15:33:24 -0600748 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700749
750 return 0;
751}
752
753static int in_dump(const struct audio_stream *stream, int fd)
754{
Paul McLean6a75e4e2016-05-25 14:09:02 -0600755 const struct stream_in* in_stream = (const struct stream_in*)stream;
756 if (in_stream != NULL) {
757 dprintf(fd, "Input Profile:\n");
758 profile_dump(in_stream->profile, fd);
759
760 dprintf(fd, "Input Proxy:\n");
761 proxy_dump(&in_stream->proxy, fd);
762 }
763
764 return 0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700765}
766
767static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
768{
Paul McLean65ec72b2015-02-18 09:13:24 -0800769 ALOGV("in_set_parameters() keys:%s", kvpairs);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700770
771 struct stream_in *in = (struct stream_in *)stream;
772
773 char value[32];
774 int param_val;
775 int routing = 0;
776 int ret_value = 0;
Eric Laurent05333d42014-08-04 20:29:17 -0700777 int card = -1;
778 int device = -1;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700779
Paul McLean65ec72b2015-02-18 09:13:24 -0800780 if (!parse_card_device_params(kvpairs, &card, &device)) {
781 // nothing to do
782 return ret_value;
783 }
784
Paul McLean994ac072016-06-02 15:33:24 -0600785 stream_lock(&in->lock);
786 device_lock(in->adev);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700787
Paul McLean2c6196f2014-08-20 16:50:25 -0700788 if (card >= 0 && device >= 0 && !profile_is_cached_for(in->profile, card, device)) {
Eric Laurent05333d42014-08-04 20:29:17 -0700789 /* cannot read pcm device info if playback is active */
790 if (!in->standby)
791 ret_value = -ENOSYS;
792 else {
793 int saved_card = in->profile->card;
794 int saved_device = in->profile->device;
795 in->profile->card = card;
796 in->profile->device = device;
797 ret_value = profile_read_device_info(in->profile) ? 0 : -EINVAL;
798 if (ret_value != 0) {
799 in->profile->card = saved_card;
800 in->profile->device = saved_device;
801 }
802 }
Paul McLean2c6196f2014-08-20 16:50:25 -0700803 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700804
Paul McLean994ac072016-06-02 15:33:24 -0600805 device_unlock(in->adev);
806 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700807
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700808 return ret_value;
809}
810
811static char * in_get_parameters(const struct audio_stream *stream, const char *keys)
812{
813 struct stream_in *in = (struct stream_in *)stream;
814
Paul McLean994ac072016-06-02 15:33:24 -0600815 stream_lock(&in->lock);
816 device_lock(in->adev);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700817
818 char * params_str = device_get_parameters(in->profile, keys);
819
Paul McLean994ac072016-06-02 15:33:24 -0600820 device_unlock(in->adev);
821 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700822
823 return params_str;
824}
825
826static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
827{
828 return 0;
829}
830
831static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
832{
833 return 0;
834}
835
836static int in_set_gain(struct audio_stream_in *stream, float gain)
837{
838 return 0;
839}
840
841/* must be called with hw device and output stream mutexes locked */
842static int start_input_stream(struct stream_in *in)
843{
Paul McLean1d585cc2016-05-24 11:28:10 -0600844 ALOGV("start_input_stream(card:%d device:%d)", in->profile->card, in->profile->device);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700845
846 return proxy_open(&in->proxy);
847}
848
849/* TODO mutex stuff here (see out_write) */
850static ssize_t in_read(struct audio_stream_in *stream, void* buffer, size_t bytes)
851{
852 size_t num_read_buff_bytes = 0;
853 void * read_buff = buffer;
854 void * out_buff = buffer;
Pavan Chikkala83b47a62015-01-09 12:21:13 -0800855 int ret = 0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700856
857 struct stream_in * in = (struct stream_in *)stream;
858
Paul McLean994ac072016-06-02 15:33:24 -0600859 stream_lock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700860 if (in->standby) {
Paul McLean994ac072016-06-02 15:33:24 -0600861 device_lock(in->adev);
Eric Laurent70318092015-06-19 17:49:17 -0700862 ret = start_input_stream(in);
Paul McLean994ac072016-06-02 15:33:24 -0600863 device_unlock(in->adev);
Eric Laurent70318092015-06-19 17:49:17 -0700864 if (ret != 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700865 goto err;
866 }
867 in->standby = false;
868 }
869
870 alsa_device_profile * profile = in->profile;
871
872 /*
873 * OK, we need to figure out how much data to read to be able to output the requested
874 * number of bytes in the HAL format (16-bit, stereo).
875 */
876 num_read_buff_bytes = bytes;
Andy Hung780f1f82015-04-22 22:14:39 -0700877 int num_device_channels = proxy_get_channel_count(&in->proxy); /* what we told Alsa */
878 int num_req_channels = in->hal_channel_count; /* what we told AudioFlinger */
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700879
880 if (num_device_channels != num_req_channels) {
881 num_read_buff_bytes = (num_device_channels * num_read_buff_bytes) / num_req_channels;
882 }
883
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700884 /* Setup/Realloc the conversion buffer (if necessary). */
885 if (num_read_buff_bytes != bytes) {
886 if (num_read_buff_bytes > in->conversion_buffer_size) {
887 /*TODO Remove this when AudioPolicyManger/AudioFlinger support arbitrary formats
888 (and do these conversions themselves) */
889 in->conversion_buffer_size = num_read_buff_bytes;
890 in->conversion_buffer = realloc(in->conversion_buffer, in->conversion_buffer_size);
891 }
892 read_buff = in->conversion_buffer;
893 }
894
Pavan Chikkala83b47a62015-01-09 12:21:13 -0800895 ret = proxy_read(&in->proxy, read_buff, num_read_buff_bytes);
896 if (ret == 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700897 if (num_device_channels != num_req_channels) {
898 // ALOGV("chans dev:%d req:%d", num_device_channels, num_req_channels);
899
900 out_buff = buffer;
901 /* Num Channels conversion */
902 if (num_device_channels != num_req_channels) {
903 audio_format_t audio_format = in_get_format(&(in->stream.common));
904 unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
905
906 num_read_buff_bytes =
907 adjust_channels(read_buff, num_device_channels,
908 out_buff, num_req_channels,
909 sample_size_in_bytes, num_read_buff_bytes);
910 }
911 }
Eric Laurent253def92014-09-14 12:18:18 -0700912
Paul McLean76dba682016-06-01 12:29:11 -0600913 /* no need to acquire in->adev->lock to read mic_muted here as we don't change its state */
914 if (num_read_buff_bytes > 0 && in->adev->mic_muted)
Eric Laurent253def92014-09-14 12:18:18 -0700915 memset(buffer, 0, num_read_buff_bytes);
Viswanath L8c7e1112014-10-31 13:07:39 +0530916 } else {
Eric Laurente6499422015-01-09 17:03:27 -0800917 num_read_buff_bytes = 0; // reset the value after USB headset is unplugged
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700918 }
919
920err:
Paul McLean994ac072016-06-02 15:33:24 -0600921 stream_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700922 return num_read_buff_bytes;
923}
924
925static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
926{
927 return 0;
928}
929
Paul McLean76dba682016-06-01 12:29:11 -0600930static int adev_open_input_stream(struct audio_hw_device *hw_dev,
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700931 audio_io_handle_t handle,
Paul McLean76dba682016-06-01 12:29:11 -0600932 audio_devices_t devicesSpec __unused,
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700933 struct audio_config *config,
934 struct audio_stream_in **stream_in,
Eric Laurentf5e24692014-07-27 16:14:57 -0700935 audio_input_flags_t flags __unused,
Paul McLean0f1753e2014-12-02 12:36:45 -0700936 const char *address /*__unused*/,
Eric Laurentf5e24692014-07-27 16:14:57 -0700937 audio_source_t source __unused)
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700938{
Paul McLean1d585cc2016-05-24 11:28:10 -0600939 ALOGV("adev_open_input_stream() rate:%" PRIu32 ", chanMask:0x%" PRIX32 ", fmt:%" PRIu8,
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700940 config->sample_rate, config->channel_mask, config->format);
941
942 struct stream_in *in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
943 int ret = 0;
944
Paul McLean76dba682016-06-01 12:29:11 -0600945 if (in == NULL) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700946 return -ENOMEM;
Paul McLean76dba682016-06-01 12:29:11 -0600947 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700948
949 /* setup function pointers */
950 in->stream.common.get_sample_rate = in_get_sample_rate;
951 in->stream.common.set_sample_rate = in_set_sample_rate;
952 in->stream.common.get_buffer_size = in_get_buffer_size;
953 in->stream.common.get_channels = in_get_channels;
954 in->stream.common.get_format = in_get_format;
955 in->stream.common.set_format = in_set_format;
956 in->stream.common.standby = in_standby;
957 in->stream.common.dump = in_dump;
958 in->stream.common.set_parameters = in_set_parameters;
959 in->stream.common.get_parameters = in_get_parameters;
960 in->stream.common.add_audio_effect = in_add_audio_effect;
961 in->stream.common.remove_audio_effect = in_remove_audio_effect;
962
963 in->stream.set_gain = in_set_gain;
964 in->stream.read = in_read;
965 in->stream.get_input_frames_lost = in_get_input_frames_lost;
966
Paul McLean994ac072016-06-02 15:33:24 -0600967 stream_lock_init(&in->lock);
Eric Laurent70318092015-06-19 17:49:17 -0700968
Paul McLean76dba682016-06-01 12:29:11 -0600969 in->adev = (struct audio_device *)hw_dev;
Paul McLean994ac072016-06-02 15:33:24 -0600970 device_lock(in->adev);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700971
Paul McLean76dba682016-06-01 12:29:11 -0600972 in->profile = &in->adev->in_profile;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700973
974 struct pcm_config proxy_config;
Paul McLean2c6196f2014-08-20 16:50:25 -0700975 memset(&proxy_config, 0, sizeof(proxy_config));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700976
Paul McLean0f1753e2014-12-02 12:36:45 -0700977 /* Pull out the card/device pair */
978 parse_card_device_params(address, &(in->profile->card), &(in->profile->device));
979
980 profile_read_device_info(in->profile);
Paul McLean0f1753e2014-12-02 12:36:45 -0700981
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700982 /* Rate */
983 if (config->sample_rate == 0) {
Paul McLean9a1c3052016-05-25 13:30:54 -0600984 config->sample_rate = profile_get_default_sample_rate(in->profile);
985 }
986
Paul McLean76dba682016-06-01 12:29:11 -0600987 if (in->adev->device_sample_rate != 0 && /* we are playing, so lock the rate */
988 in->adev->device_sample_rate >= RATELOCK_THRESHOLD) {/* but only for high sample rates */
989 ret = config->sample_rate != in->adev->device_sample_rate ? -EINVAL : 0;
990 proxy_config.rate = config->sample_rate = in->adev->device_sample_rate;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700991 } else if (profile_is_sample_rate_valid(in->profile, config->sample_rate)) {
Paul McLean76dba682016-06-01 12:29:11 -0600992 in->adev->device_sample_rate = proxy_config.rate = config->sample_rate;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700993 } else {
994 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(in->profile);
995 ret = -EINVAL;
996 }
Paul McLean994ac072016-06-02 15:33:24 -0600997 device_unlock(in->adev);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700998
999 /* Format */
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001000 if (config->format == AUDIO_FORMAT_DEFAULT) {
Andy Hung780f1f82015-04-22 22:14:39 -07001001 proxy_config.format = profile_get_default_format(in->profile);
1002 config->format = audio_format_from_pcm_format(proxy_config.format);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001003 } else {
Andy Hung780f1f82015-04-22 22:14:39 -07001004 enum pcm_format fmt = pcm_format_from_audio_format(config->format);
1005 if (profile_is_format_valid(in->profile, fmt)) {
1006 proxy_config.format = fmt;
1007 } else {
1008 proxy_config.format = profile_get_default_format(in->profile);
1009 config->format = audio_format_from_pcm_format(proxy_config.format);
1010 ret = -EINVAL;
1011 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001012 }
1013
Paul McLean2cfd81b2014-09-15 12:32:23 -07001014 /* Channels */
Paul McLean64345f82016-06-06 13:26:05 -06001015 bool calc_mask = false;
1016 if (config->channel_mask == AUDIO_CHANNEL_NONE) {
1017 /* query case */
1018 in->hal_channel_count = profile_get_default_channel_count(in->profile);
1019 calc_mask = true;
Andy Hung780f1f82015-04-22 22:14:39 -07001020 } else {
Paul McLean64345f82016-06-06 13:26:05 -06001021 /* explicit case */
Andy Hung780f1f82015-04-22 22:14:39 -07001022 in->hal_channel_count = audio_channel_count_from_in_mask(config->channel_mask);
1023 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001024
Paul McLean64345f82016-06-06 13:26:05 -06001025 /* The Framework is currently limited to no more than this number of channels */
1026 if (in->hal_channel_count > FCC_8) {
1027 in->hal_channel_count = FCC_8;
1028 calc_mask = true;
1029 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001030
Paul McLean64345f82016-06-06 13:26:05 -06001031 if (calc_mask) {
1032 /* need to calculate the mask from channel count either because this is the query case
1033 * or the specified mask isn't valid for this device, or is more then the FW can handle */
1034 in->hal_channel_mask = in->hal_channel_count <= FCC_2
1035 /* position mask for mono & stereo */
1036 ? audio_channel_in_mask_from_count(in->hal_channel_count)
1037 /* otherwise indexed */
1038 : audio_channel_mask_for_index_assignment_from_count(in->hal_channel_count);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001039
Paul McLean64345f82016-06-06 13:26:05 -06001040 // if we change the mask...
1041 if (in->hal_channel_mask != config->channel_mask &&
1042 config->channel_mask != AUDIO_CHANNEL_NONE) {
1043 config->channel_mask = in->hal_channel_mask;
1044 ret = -EINVAL;
1045 }
1046 } else {
1047 in->hal_channel_mask = config->channel_mask;
1048 }
Paul McLean6a75e4e2016-05-25 14:09:02 -06001049
Paul McLean64345f82016-06-06 13:26:05 -06001050 if (ret == 0) {
1051 // Validate the "logical" channel count against support in the "actual" profile.
1052 // if they differ, choose the "actual" number of channels *closest* to the "logical".
1053 // and store THAT in proxy_config.channels
1054 proxy_config.channels =
1055 profile_get_closest_channel_count(in->profile, in->hal_channel_count);
1056 proxy_prepare(&in->proxy, in->profile, &proxy_config);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001057
Paul McLean64345f82016-06-06 13:26:05 -06001058 in->standby = true;
1059
1060 in->conversion_buffer = NULL;
1061 in->conversion_buffer_size = 0;
1062
1063 *stream_in = &in->stream;
1064
1065 /* Save this for adev_dump() */
1066 adev_add_stream_to_list(in->adev, &in->adev->input_stream_list, &in->list_node);
1067 } else {
1068 // Deallocate this stream on error, because AudioFlinger won't call
1069 // adev_close_input_stream() in this case.
1070 *stream_in = NULL;
1071 free(in);
1072 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001073
1074 return ret;
1075}
1076
Paul McLean76dba682016-06-01 12:29:11 -06001077static void adev_close_input_stream(struct audio_hw_device *hw_dev,
1078 struct audio_stream_in *stream)
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001079{
1080 struct stream_in *in = (struct stream_in *)stream;
Paul McLean6a75e4e2016-05-25 14:09:02 -06001081 ALOGV("adev_close_input_stream(c:%d d:%d)", in->profile->card, in->profile->device);
1082
Paul McLean76dba682016-06-01 12:29:11 -06001083 adev_remove_stream_from_list(in->adev, &in->list_node);
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001084
1085 /* Close the pcm device */
1086 in_standby(&stream->common);
1087
1088 free(in->conversion_buffer);
1089
1090 free(stream);
1091}
1092
1093/*
1094 * ADEV Functions
1095 */
Paul McLean76dba682016-06-01 12:29:11 -06001096static int adev_set_parameters(struct audio_hw_device *hw_dev, const char *kvpairs)
Simon Wilson19957a32012-04-06 16:17:12 -07001097{
1098 return 0;
1099}
1100
Paul McLean76dba682016-06-01 12:29:11 -06001101static char * adev_get_parameters(const struct audio_hw_device *hw_dev, const char *keys)
Simon Wilson19957a32012-04-06 16:17:12 -07001102{
1103 return strdup("");
1104}
1105
Paul McLean76dba682016-06-01 12:29:11 -06001106static int adev_init_check(const struct audio_hw_device *hw_dev)
Simon Wilson19957a32012-04-06 16:17:12 -07001107{
1108 return 0;
1109}
1110
Paul McLean76dba682016-06-01 12:29:11 -06001111static int adev_set_voice_volume(struct audio_hw_device *hw_dev, float volume)
Simon Wilson19957a32012-04-06 16:17:12 -07001112{
1113 return -ENOSYS;
1114}
1115
Paul McLean76dba682016-06-01 12:29:11 -06001116static int adev_set_master_volume(struct audio_hw_device *hw_dev, float volume)
Simon Wilson19957a32012-04-06 16:17:12 -07001117{
1118 return -ENOSYS;
1119}
1120
Paul McLean76dba682016-06-01 12:29:11 -06001121static int adev_set_mode(struct audio_hw_device *hw_dev, audio_mode_t mode)
Simon Wilson19957a32012-04-06 16:17:12 -07001122{
1123 return 0;
1124}
1125
Paul McLean76dba682016-06-01 12:29:11 -06001126static int adev_set_mic_mute(struct audio_hw_device *hw_dev, bool state)
Simon Wilson19957a32012-04-06 16:17:12 -07001127{
Paul McLean76dba682016-06-01 12:29:11 -06001128 struct audio_device * adev = (struct audio_device *)hw_dev;
Paul McLean994ac072016-06-02 15:33:24 -06001129 device_lock(adev);
Eric Laurent253def92014-09-14 12:18:18 -07001130 adev->mic_muted = state;
Paul McLean994ac072016-06-02 15:33:24 -06001131 device_unlock(adev);
Simon Wilson19957a32012-04-06 16:17:12 -07001132 return -ENOSYS;
1133}
1134
Paul McLean76dba682016-06-01 12:29:11 -06001135static int adev_get_mic_mute(const struct audio_hw_device *hw_dev, bool *state)
Simon Wilson19957a32012-04-06 16:17:12 -07001136{
1137 return -ENOSYS;
1138}
1139
Paul McLean76dba682016-06-01 12:29:11 -06001140static int adev_dump(const struct audio_hw_device *device, int fd)
Simon Wilson19957a32012-04-06 16:17:12 -07001141{
Paul McLean6a75e4e2016-05-25 14:09:02 -06001142 dprintf(fd, "\nUSB audio module:\n");
1143
1144 struct audio_device* adev = (struct audio_device*)device;
1145 const int kNumRetries = 3;
1146 const int kSleepTimeMS = 500;
1147
Paul McLean994ac072016-06-02 15:33:24 -06001148 // use device_try_lock() in case we dumpsys during a deadlock
Paul McLean6a75e4e2016-05-25 14:09:02 -06001149 int retry = kNumRetries;
Paul McLean994ac072016-06-02 15:33:24 -06001150 while (retry > 0 && device_try_lock(adev) != 0) {
Paul McLean6a75e4e2016-05-25 14:09:02 -06001151 sleep(kSleepTimeMS);
1152 retry--;
1153 }
1154
1155 if (retry > 0) {
1156 if (list_empty(&adev->output_stream_list)) {
1157 dprintf(fd, " No output streams.\n");
1158 } else {
1159 struct listnode* node;
1160 list_for_each(node, &adev->output_stream_list) {
1161 struct audio_stream* stream =
1162 (struct audio_stream *)node_to_item(node, struct stream_out, list_node);
1163 out_dump(stream, fd);
1164 }
1165 }
1166
1167 if (list_empty(&adev->input_stream_list)) {
1168 dprintf(fd, "\n No input streams.\n");
1169 } else {
1170 struct listnode* node;
1171 list_for_each(node, &adev->input_stream_list) {
1172 struct audio_stream* stream =
1173 (struct audio_stream *)node_to_item(node, struct stream_in, list_node);
1174 in_dump(stream, fd);
1175 }
1176 }
1177
Paul McLean994ac072016-06-02 15:33:24 -06001178 device_unlock(adev);
Paul McLean6a75e4e2016-05-25 14:09:02 -06001179 } else {
1180 // Couldn't lock
1181 dprintf(fd, " Could not obtain device lock.\n");
1182 }
1183
Simon Wilson19957a32012-04-06 16:17:12 -07001184 return 0;
1185}
1186
1187static int adev_close(hw_device_t *device)
1188{
Paul McLeaneedc92e2013-12-19 15:46:15 -08001189 struct audio_device *adev = (struct audio_device *)device;
Simon Wilson19957a32012-04-06 16:17:12 -07001190 free(device);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001191
Simon Wilson19957a32012-04-06 16:17:12 -07001192 return 0;
1193}
1194
Paul McLean30f41852014-04-16 15:44:20 -07001195static int adev_open(const hw_module_t* module, const char* name, hw_device_t** device)
Simon Wilson19957a32012-04-06 16:17:12 -07001196{
Simon Wilson19957a32012-04-06 16:17:12 -07001197 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
1198 return -EINVAL;
1199
Paul McLeaneedc92e2013-12-19 15:46:15 -08001200 struct audio_device *adev = calloc(1, sizeof(struct audio_device));
Simon Wilson19957a32012-04-06 16:17:12 -07001201 if (!adev)
1202 return -ENOMEM;
1203
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001204 profile_init(&adev->out_profile, PCM_OUT);
1205 profile_init(&adev->in_profile, PCM_IN);
1206
Paul McLean6a75e4e2016-05-25 14:09:02 -06001207 list_init(&adev->output_stream_list);
1208 list_init(&adev->input_stream_list);
1209
Simon Wilson19957a32012-04-06 16:17:12 -07001210 adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
Eric Laurent85e08e22012-08-28 14:30:35 -07001211 adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001212 adev->hw_device.common.module = (struct hw_module_t *)module;
Simon Wilson19957a32012-04-06 16:17:12 -07001213 adev->hw_device.common.close = adev_close;
1214
Simon Wilson19957a32012-04-06 16:17:12 -07001215 adev->hw_device.init_check = adev_init_check;
1216 adev->hw_device.set_voice_volume = adev_set_voice_volume;
1217 adev->hw_device.set_master_volume = adev_set_master_volume;
1218 adev->hw_device.set_mode = adev_set_mode;
1219 adev->hw_device.set_mic_mute = adev_set_mic_mute;
1220 adev->hw_device.get_mic_mute = adev_get_mic_mute;
1221 adev->hw_device.set_parameters = adev_set_parameters;
1222 adev->hw_device.get_parameters = adev_get_parameters;
1223 adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size;
1224 adev->hw_device.open_output_stream = adev_open_output_stream;
1225 adev->hw_device.close_output_stream = adev_close_output_stream;
1226 adev->hw_device.open_input_stream = adev_open_input_stream;
1227 adev->hw_device.close_input_stream = adev_close_input_stream;
1228 adev->hw_device.dump = adev_dump;
1229
1230 *device = &adev->hw_device.common;
1231
1232 return 0;
1233}
1234
1235static struct hw_module_methods_t hal_module_methods = {
1236 .open = adev_open,
1237};
1238
1239struct audio_module HAL_MODULE_INFO_SYM = {
1240 .common = {
1241 .tag = HARDWARE_MODULE_TAG,
Mike Lockwood46a98092012-04-24 16:41:18 -07001242 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
1243 .hal_api_version = HARDWARE_HAL_API_VERSION,
Simon Wilson19957a32012-04-06 16:17:12 -07001244 .id = AUDIO_HARDWARE_MODULE_ID,
1245 .name = "USB audio HW HAL",
1246 .author = "The Android Open Source Project",
1247 .methods = &hal_module_methods,
1248 },
1249};