blob: bbea5f5e9da6c65a6b010e08a2413c5c0abf03f7 [file] [log] [blame]
Simon Wilson19957a32012-04-06 16:17:12 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Paul McLean9ab869a2015-01-13 09:37:06 -080017#define LOG_TAG "modules.usbaudio.audio_hal"
Paul McLeancf611912014-04-28 13:03:18 -070018/*#define LOG_NDEBUG 0*/
Simon Wilson19957a32012-04-06 16:17:12 -070019
20#include <errno.h>
Mark Salyzyn88e458a2014-04-28 12:30:44 -070021#include <inttypes.h>
Simon Wilson19957a32012-04-06 16:17:12 -070022#include <pthread.h>
23#include <stdint.h>
Simon Wilson19957a32012-04-06 16:17:12 -070024#include <stdlib.h>
Mark Salyzyn88e458a2014-04-28 12:30:44 -070025#include <sys/time.h>
Simon Wilson19957a32012-04-06 16:17:12 -070026
Mark Salyzyn88e458a2014-04-28 12:30:44 -070027#include <log/log.h>
Simon Wilson19957a32012-04-06 16:17:12 -070028#include <cutils/str_parms.h>
29#include <cutils/properties.h>
30
Simon Wilson19957a32012-04-06 16:17:12 -070031#include <hardware/audio.h>
Paul McLeane32cbc12014-06-25 10:42:07 -070032#include <hardware/audio_alsaops.h>
33#include <hardware/hardware.h>
34
35#include <system/audio.h>
Simon Wilson19957a32012-04-06 16:17:12 -070036
37#include <tinyalsa/asoundlib.h>
38
Paul McLeanc2201152014-07-16 13:46:07 -070039#include <audio_utils/channels.h>
40
Andy Hung03576be2014-07-21 21:16:45 -070041/* FOR TESTING:
42 * Set k_force_channels to force the number of channels to present to AudioFlinger.
43 * 0 disables (this is default: present the device channels to AudioFlinger).
44 * 2 forces to legacy stereo mode.
45 *
46 * Others values can be tried (up to 8).
47 * TODO: AudioFlinger cannot support more than 8 active output channels
48 * at this time, so limiting logic needs to be put here or communicated from above.
49 */
50static const unsigned k_force_channels = 0;
51
Paul McLeanc88e6ae2014-07-16 09:48:34 -070052#include "alsa_device_profile.h"
53#include "alsa_device_proxy.h"
Paul McLean9ab869a2015-01-13 09:37:06 -080054#include "alsa_logging.h"
Paul McLeanf62d75e2014-07-11 15:14:19 -070055
Paul McLeanc88e6ae2014-07-16 09:48:34 -070056#define DEFAULT_INPUT_BUFFER_SIZE_MS 20
Paul McLeanf62d75e2014-07-11 15:14:19 -070057
Andy Hung5e58a302015-06-10 15:17:00 -070058// stereo channel count
59#define FCC_2 2
Andy Hung780f1f82015-04-22 22:14:39 -070060// fixed channel count of 8 limitation (for data processing in AudioFlinger)
61#define FCC_8 8
62
Simon Wilson19957a32012-04-06 16:17:12 -070063struct audio_device {
64 struct audio_hw_device hw_device;
65
66 pthread_mutex_t lock; /* see note below on mutex acquisition order */
Paul McLeaneedc92e2013-12-19 15:46:15 -080067
68 /* output */
Paul McLeanc88e6ae2014-07-16 09:48:34 -070069 alsa_device_profile out_profile;
Paul McLeaneedc92e2013-12-19 15:46:15 -080070
71 /* input */
Paul McLeanc88e6ae2014-07-16 09:48:34 -070072 alsa_device_profile in_profile;
Paul McLeaneedc92e2013-12-19 15:46:15 -080073
Eric Laurent253def92014-09-14 12:18:18 -070074 bool mic_muted;
75
Simon Wilson19957a32012-04-06 16:17:12 -070076 bool standby;
77};
78
79struct stream_out {
80 struct audio_stream_out stream;
81
Paul McLeaneedc92e2013-12-19 15:46:15 -080082 pthread_mutex_t lock; /* see note below on mutex acquisition order */
Eric Laurent70318092015-06-19 17:49:17 -070083 pthread_mutex_t pre_lock; /* acquire before lock to avoid DOS by playback thread */
Paul McLeaneedc92e2013-12-19 15:46:15 -080084 bool standby;
85
Paul McLeanc88e6ae2014-07-16 09:48:34 -070086 struct audio_device *dev; /* hardware information - only using this for the lock */
87
Paul McLean65ec72b2015-02-18 09:13:24 -080088 alsa_device_profile * profile; /* Points to the alsa_device_profile in the audio_device */
Paul McLeanc88e6ae2014-07-16 09:48:34 -070089 alsa_device_proxy proxy; /* state of the stream */
Paul McLeaneedc92e2013-12-19 15:46:15 -080090
Andy Hung03576be2014-07-21 21:16:45 -070091 unsigned hal_channel_count; /* channel count exposed to AudioFlinger.
92 * This may differ from the device channel count when
93 * the device is not compatible with AudioFlinger
94 * capabilities, e.g. exposes too many channels or
95 * too few channels. */
Andy Hung182ddc72015-05-05 23:37:30 -070096 audio_channel_mask_t hal_channel_mask; /* channel mask exposed to AudioFlinger. */
97
Paul McLeaneedc92e2013-12-19 15:46:15 -080098 void * conversion_buffer; /* any conversions are put into here
99 * they could come from here too if
100 * there was a previous conversion */
101 size_t conversion_buffer_size; /* in bytes */
102};
103
Paul McLeaneedc92e2013-12-19 15:46:15 -0800104struct stream_in {
105 struct audio_stream_in stream;
106
Eric Laurent70318092015-06-19 17:49:17 -0700107 pthread_mutex_t lock; /* see note below on mutex acquisition order */
108 pthread_mutex_t pre_lock; /* acquire before lock to avoid DOS by capture thread */
Simon Wilson19957a32012-04-06 16:17:12 -0700109 bool standby;
110
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700111 struct audio_device *dev; /* hardware information - only using this for the lock */
Paul McLeaneedc92e2013-12-19 15:46:15 -0800112
Paul McLean65ec72b2015-02-18 09:13:24 -0800113 alsa_device_profile * profile; /* Points to the alsa_device_profile in the audio_device */
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700114 alsa_device_proxy proxy; /* state of the stream */
Paul McLeanf62d75e2014-07-11 15:14:19 -0700115
Paul McLean2cfd81b2014-09-15 12:32:23 -0700116 unsigned hal_channel_count; /* channel count exposed to AudioFlinger.
117 * This may differ from the device channel count when
118 * the device is not compatible with AudioFlinger
119 * capabilities, e.g. exposes too many channels or
120 * too few channels. */
Andy Hung780f1f82015-04-22 22:14:39 -0700121 audio_channel_mask_t hal_channel_mask; /* channel mask exposed to AudioFlinger. */
122
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700123 /* 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 -0700124 void * conversion_buffer; /* any conversions are put into here
125 * they could come from here too if
126 * there was a previous conversion */
127 size_t conversion_buffer_size; /* in bytes */
Simon Wilson19957a32012-04-06 16:17:12 -0700128};
129
Paul McLeaneedc92e2013-12-19 15:46:15 -0800130/*
Eric Laurent70318092015-06-19 17:49:17 -0700131 * NOTE: when multiple mutexes have to be acquired, always take the
132 * stream_in or stream_out mutex first, followed by the audio_device mutex.
133 * stream pre_lock is always acquired before stream lock to prevent starvation of control thread by
134 * higher priority playback or capture thread.
135 */
136
137/*
Paul McLean0f1753e2014-12-02 12:36:45 -0700138 * Extract the card and device numbers from the supplied key/value pairs.
139 * kvpairs A null-terminated string containing the key/value pairs or card and device.
140 * i.e. "card=1;device=42"
141 * card A pointer to a variable to receive the parsed-out card number.
142 * device A pointer to a variable to receive the parsed-out device number.
143 * NOTE: The variables pointed to by card and device return -1 (undefined) if the
144 * associated key/value pair is not found in the provided string.
Paul McLean65ec72b2015-02-18 09:13:24 -0800145 * Return true if the kvpairs string contain a card/device spec, false otherwise.
Paul McLean0f1753e2014-12-02 12:36:45 -0700146 */
Paul McLean65ec72b2015-02-18 09:13:24 -0800147static bool parse_card_device_params(const char *kvpairs, int *card, int *device)
Paul McLean0f1753e2014-12-02 12:36:45 -0700148{
149 struct str_parms * parms = str_parms_create_str(kvpairs);
150 char value[32];
151 int param_val;
152
153 // initialize to "undefined" state.
154 *card = -1;
155 *device = -1;
156
157 param_val = str_parms_get_str(parms, "card", value, sizeof(value));
158 if (param_val >= 0) {
159 *card = atoi(value);
160 }
161
162 param_val = str_parms_get_str(parms, "device", value, sizeof(value));
163 if (param_val >= 0) {
164 *device = atoi(value);
165 }
166
167 str_parms_destroy(parms);
Paul McLean65ec72b2015-02-18 09:13:24 -0800168
169 return *card >= 0 && *device >= 0;
Paul McLean0f1753e2014-12-02 12:36:45 -0700170}
171
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700172static char * device_get_parameters(alsa_device_profile * profile, const char * keys)
Paul McLeaneedc92e2013-12-19 15:46:15 -0800173{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700174 if (profile->card < 0 || profile->device < 0) {
175 return strdup("");
Paul McLeaneedc92e2013-12-19 15:46:15 -0800176 }
Paul McLeane32cbc12014-06-25 10:42:07 -0700177
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700178 struct str_parms *query = str_parms_create_str(keys);
179 struct str_parms *result = str_parms_create();
Paul McLeane32cbc12014-06-25 10:42:07 -0700180
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700181 /* These keys are from hardware/libhardware/include/audio.h */
182 /* supported sample rates */
183 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) {
184 char* rates_list = profile_get_sample_rate_strs(profile);
185 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES,
186 rates_list);
187 free(rates_list);
Paul McLeane32cbc12014-06-25 10:42:07 -0700188 }
189
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700190 /* supported channel counts */
191 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) {
192 char* channels_list = profile_get_channel_count_strs(profile);
193 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_CHANNELS,
194 channels_list);
195 free(channels_list);
Paul McLeane32cbc12014-06-25 10:42:07 -0700196 }
197
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700198 /* supported sample formats */
199 if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) {
200 char * format_params = profile_get_format_strs(profile);
201 str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_FORMATS,
202 format_params);
203 free(format_params);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700204 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700205 str_parms_destroy(query);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700206
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700207 char* result_str = str_parms_to_str(result);
208 str_parms_destroy(result);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700209
Paul McLean65ec72b2015-02-18 09:13:24 -0800210 ALOGV("device_get_parameters = %s", result_str);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700211
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700212 return result_str;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800213}
214
Eric Laurent70318092015-06-19 17:49:17 -0700215void lock_input_stream(struct stream_in *in)
216{
217 pthread_mutex_lock(&in->pre_lock);
218 pthread_mutex_lock(&in->lock);
219 pthread_mutex_unlock(&in->pre_lock);
220}
221
222void lock_output_stream(struct stream_out *out)
223{
224 pthread_mutex_lock(&out->pre_lock);
225 pthread_mutex_lock(&out->lock);
226 pthread_mutex_unlock(&out->pre_lock);
227}
228
Paul McLeaneedc92e2013-12-19 15:46:15 -0800229/*
230 * HAl Functions
231 */
Simon Wilson19957a32012-04-06 16:17:12 -0700232/**
233 * NOTE: when multiple mutexes have to be acquired, always respect the
234 * following order: hw device > out stream
235 */
236
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700237/*
238 * OUT functions
239 */
Simon Wilson19957a32012-04-06 16:17:12 -0700240static uint32_t out_get_sample_rate(const struct audio_stream *stream)
241{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700242 uint32_t rate = proxy_get_sample_rate(&((struct stream_out*)stream)->proxy);
243 ALOGV("out_get_sample_rate() = %d", rate);
244 return rate;
Simon Wilson19957a32012-04-06 16:17:12 -0700245}
246
247static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
248{
249 return 0;
250}
251
252static size_t out_get_buffer_size(const struct audio_stream *stream)
253{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700254 const struct stream_out* out = (const struct stream_out*)stream;
255 size_t buffer_size =
256 proxy_get_period_size(&out->proxy) * audio_stream_out_frame_size(&(out->stream));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700257 return buffer_size;
Simon Wilson19957a32012-04-06 16:17:12 -0700258}
259
260static uint32_t out_get_channels(const struct audio_stream *stream)
261{
Andy Hung03576be2014-07-21 21:16:45 -0700262 const struct stream_out *out = (const struct stream_out*)stream;
Andy Hung182ddc72015-05-05 23:37:30 -0700263 return out->hal_channel_mask;
Simon Wilson19957a32012-04-06 16:17:12 -0700264}
265
266static audio_format_t out_get_format(const struct audio_stream *stream)
267{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700268 /* Note: The HAL doesn't do any FORMAT conversion at this time. It
269 * Relies on the framework to provide data in the specified format.
270 * This could change in the future.
271 */
272 alsa_device_proxy * proxy = &((struct stream_out*)stream)->proxy;
273 audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700274 return format;
Simon Wilson19957a32012-04-06 16:17:12 -0700275}
276
277static int out_set_format(struct audio_stream *stream, audio_format_t format)
278{
279 return 0;
280}
281
282static int out_standby(struct audio_stream *stream)
283{
284 struct stream_out *out = (struct stream_out *)stream;
285
Eric Laurent70318092015-06-19 17:49:17 -0700286 lock_output_stream(out);
Simon Wilson19957a32012-04-06 16:17:12 -0700287 if (!out->standby) {
Eric Laurent70318092015-06-19 17:49:17 -0700288 pthread_mutex_lock(&out->dev->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700289 proxy_close(&out->proxy);
Eric Laurent70318092015-06-19 17:49:17 -0700290 pthread_mutex_unlock(&out->dev->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700291 out->standby = true;
292 }
Simon Wilson19957a32012-04-06 16:17:12 -0700293 pthread_mutex_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700294
295 return 0;
296}
297
298static int out_dump(const struct audio_stream *stream, int fd)
299{
300 return 0;
301}
302
303static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
304{
Paul McLean65ec72b2015-02-18 09:13:24 -0800305 ALOGV("out_set_parameters() keys:%s", kvpairs);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800306
Simon Wilson19957a32012-04-06 16:17:12 -0700307 struct stream_out *out = (struct stream_out *)stream;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700308
Simon Wilson19957a32012-04-06 16:17:12 -0700309 int routing = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800310 int ret_value = 0;
Eric Laurent05333d42014-08-04 20:29:17 -0700311 int card = -1;
312 int device = -1;
Simon Wilson19957a32012-04-06 16:17:12 -0700313
Paul McLean65ec72b2015-02-18 09:13:24 -0800314 if (!parse_card_device_params(kvpairs, &card, &device)) {
315 // nothing to do
316 return ret_value;
317 }
318
Eric Laurent70318092015-06-19 17:49:17 -0700319 lock_output_stream(out);
Paul McLean65ec72b2015-02-18 09:13:24 -0800320 /* Lock the device because that is where the profile lives */
Paul McLeanf62d75e2014-07-11 15:14:19 -0700321 pthread_mutex_lock(&out->dev->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700322
Paul McLean65ec72b2015-02-18 09:13:24 -0800323 if (!profile_is_cached_for(out->profile, card, device)) {
Eric Laurent05333d42014-08-04 20:29:17 -0700324 /* cannot read pcm device info if playback is active */
325 if (!out->standby)
326 ret_value = -ENOSYS;
327 else {
328 int saved_card = out->profile->card;
329 int saved_device = out->profile->device;
330 out->profile->card = card;
331 out->profile->device = device;
332 ret_value = profile_read_device_info(out->profile) ? 0 : -EINVAL;
333 if (ret_value != 0) {
334 out->profile->card = saved_card;
335 out->profile->device = saved_device;
336 }
337 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800338 }
Paul McLean2c6196f2014-08-20 16:50:25 -0700339
Paul McLeanf62d75e2014-07-11 15:14:19 -0700340 pthread_mutex_unlock(&out->dev->lock);
Eric Laurent70318092015-06-19 17:49:17 -0700341 pthread_mutex_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700342
Paul McLeaneedc92e2013-12-19 15:46:15 -0800343 return ret_value;
Simon Wilson19957a32012-04-06 16:17:12 -0700344}
345
Paul McLeanf62d75e2014-07-11 15:14:19 -0700346static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
347{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700348 struct stream_out *out = (struct stream_out *)stream;
Eric Laurent70318092015-06-19 17:49:17 -0700349 lock_output_stream(out);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700350 pthread_mutex_lock(&out->dev->lock);
Paul McLeanf62d75e2014-07-11 15:14:19 -0700351
352 char * params_str = device_get_parameters(out->profile, keys);
353
354 pthread_mutex_unlock(&out->lock);
355 pthread_mutex_unlock(&out->dev->lock);
356
357 return params_str;
358}
359
Simon Wilson19957a32012-04-06 16:17:12 -0700360static uint32_t out_get_latency(const struct audio_stream_out *stream)
361{
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700362 alsa_device_proxy * proxy = &((struct stream_out*)stream)->proxy;
363 return proxy_get_latency(proxy);
Simon Wilson19957a32012-04-06 16:17:12 -0700364}
365
Paul McLean30f41852014-04-16 15:44:20 -0700366static int out_set_volume(struct audio_stream_out *stream, float left, float right)
Simon Wilson19957a32012-04-06 16:17:12 -0700367{
368 return -ENOSYS;
369}
370
Paul McLeaneedc92e2013-12-19 15:46:15 -0800371/* must be called with hw device and output stream mutexes locked */
372static int start_output_stream(struct stream_out *out)
373{
Paul McLean65ec72b2015-02-18 09:13:24 -0800374 ALOGV("start_output_stream(card:%d device:%d)", out->profile->card, out->profile->device);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800375
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700376 return proxy_open(&out->proxy);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800377}
378
379static ssize_t out_write(struct audio_stream_out *stream, const void* buffer, size_t bytes)
Simon Wilson19957a32012-04-06 16:17:12 -0700380{
381 int ret;
382 struct stream_out *out = (struct stream_out *)stream;
383
Eric Laurent70318092015-06-19 17:49:17 -0700384 lock_output_stream(out);
Simon Wilson19957a32012-04-06 16:17:12 -0700385 if (out->standby) {
Eric Laurent70318092015-06-19 17:49:17 -0700386 pthread_mutex_lock(&out->dev->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700387 ret = start_output_stream(out);
Eric Laurent70318092015-06-19 17:49:17 -0700388 pthread_mutex_unlock(&out->dev->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700389 if (ret != 0) {
390 goto err;
391 }
392 out->standby = false;
393 }
Eric Laurent05333d42014-08-04 20:29:17 -0700394
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700395 alsa_device_proxy* proxy = &out->proxy;
Mark Salyzyn88e458a2014-04-28 12:30:44 -0700396 const void * write_buff = buffer;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800397 int num_write_buff_bytes = bytes;
Andy Hung03576be2014-07-21 21:16:45 -0700398 const int num_device_channels = proxy_get_channel_count(proxy); /* what we told alsa */
399 const int num_req_channels = out->hal_channel_count; /* what we told AudioFlinger */
Paul McLean30f41852014-04-16 15:44:20 -0700400 if (num_device_channels != num_req_channels) {
Andy Hung03576be2014-07-21 21:16:45 -0700401 /* allocate buffer */
402 const size_t required_conversion_buffer_size =
403 bytes * num_device_channels / num_req_channels;
404 if (required_conversion_buffer_size > out->conversion_buffer_size) {
405 out->conversion_buffer_size = required_conversion_buffer_size;
406 out->conversion_buffer = realloc(out->conversion_buffer,
407 out->conversion_buffer_size);
408 }
409 /* convert data */
410 const audio_format_t audio_format = out_get_format(&(out->stream.common));
411 const unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800412 num_write_buff_bytes =
Andy Hung03576be2014-07-21 21:16:45 -0700413 adjust_channels(write_buff, num_req_channels,
414 out->conversion_buffer, num_device_channels,
415 sample_size_in_bytes, num_write_buff_bytes);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800416 write_buff = out->conversion_buffer;
417 }
418
Paul McLeaneedc92e2013-12-19 15:46:15 -0800419 if (write_buff != NULL && num_write_buff_bytes != 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700420 proxy_write(&out->proxy, write_buff, num_write_buff_bytes);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800421 }
Simon Wilson19957a32012-04-06 16:17:12 -0700422
423 pthread_mutex_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700424
425 return bytes;
426
427err:
428 pthread_mutex_unlock(&out->lock);
Simon Wilson19957a32012-04-06 16:17:12 -0700429 if (ret != 0) {
Eric Laurentc5ae6a02014-07-02 13:45:32 -0700430 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
Simon Wilson19957a32012-04-06 16:17:12 -0700431 out_get_sample_rate(&stream->common));
432 }
433
434 return bytes;
435}
436
Paul McLean30f41852014-04-16 15:44:20 -0700437static int out_get_render_position(const struct audio_stream_out *stream, uint32_t *dsp_frames)
Simon Wilson19957a32012-04-06 16:17:12 -0700438{
439 return -EINVAL;
440}
441
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700442static int out_get_presentation_position(const struct audio_stream_out *stream,
443 uint64_t *frames, struct timespec *timestamp)
444{
445 /* FIXME - This needs to be implemented */
446 return -EINVAL;
447}
448
Simon Wilson19957a32012-04-06 16:17:12 -0700449static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
450{
451 return 0;
452}
453
454static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
455{
456 return 0;
457}
458
Paul McLean30f41852014-04-16 15:44:20 -0700459static int out_get_next_write_timestamp(const struct audio_stream_out *stream, int64_t *timestamp)
Simon Wilson19957a32012-04-06 16:17:12 -0700460{
461 return -EINVAL;
462}
463
464static int adev_open_output_stream(struct audio_hw_device *dev,
Mike Lockwood46a98092012-04-24 16:41:18 -0700465 audio_io_handle_t handle,
466 audio_devices_t devices,
467 audio_output_flags_t flags,
468 struct audio_config *config,
Eric Laurentf5e24692014-07-27 16:14:57 -0700469 struct audio_stream_out **stream_out,
Paul McLean0f1753e2014-12-02 12:36:45 -0700470 const char *address /*__unused*/)
Simon Wilson19957a32012-04-06 16:17:12 -0700471{
Paul McLean65ec72b2015-02-18 09:13:24 -0800472 ALOGV("adev_open_output_stream() handle:0x%X, device:0x%X, flags:0x%X, addr:%s",
Paul McLean0f1753e2014-12-02 12:36:45 -0700473 handle, devices, flags, address);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800474
Simon Wilson19957a32012-04-06 16:17:12 -0700475 struct audio_device *adev = (struct audio_device *)dev;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800476
Simon Wilson19957a32012-04-06 16:17:12 -0700477 struct stream_out *out;
Simon Wilson19957a32012-04-06 16:17:12 -0700478
479 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
480 if (!out)
481 return -ENOMEM;
482
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700483 /* setup function pointers */
Simon Wilson19957a32012-04-06 16:17:12 -0700484 out->stream.common.get_sample_rate = out_get_sample_rate;
485 out->stream.common.set_sample_rate = out_set_sample_rate;
486 out->stream.common.get_buffer_size = out_get_buffer_size;
487 out->stream.common.get_channels = out_get_channels;
488 out->stream.common.get_format = out_get_format;
489 out->stream.common.set_format = out_set_format;
490 out->stream.common.standby = out_standby;
491 out->stream.common.dump = out_dump;
492 out->stream.common.set_parameters = out_set_parameters;
493 out->stream.common.get_parameters = out_get_parameters;
494 out->stream.common.add_audio_effect = out_add_audio_effect;
495 out->stream.common.remove_audio_effect = out_remove_audio_effect;
496 out->stream.get_latency = out_get_latency;
497 out->stream.set_volume = out_set_volume;
498 out->stream.write = out_write;
499 out->stream.get_render_position = out_get_render_position;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700500 out->stream.get_presentation_position = out_get_presentation_position;
Simon Wilson19957a32012-04-06 16:17:12 -0700501 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
502
Eric Laurent70318092015-06-19 17:49:17 -0700503 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
504 pthread_mutex_init(&out->pre_lock, (const pthread_mutexattr_t *) NULL);
505
Simon Wilson19957a32012-04-06 16:17:12 -0700506 out->dev = adev;
Paul McLean0f1753e2014-12-02 12:36:45 -0700507 pthread_mutex_lock(&adev->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700508 out->profile = &adev->out_profile;
Paul McLeanf62d75e2014-07-11 15:14:19 -0700509
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700510 // build this to hand to the alsa_device_proxy
511 struct pcm_config proxy_config;
Paul McLean2c6196f2014-08-20 16:50:25 -0700512 memset(&proxy_config, 0, sizeof(proxy_config));
Paul McLeaneedc92e2013-12-19 15:46:15 -0800513
Paul McLean0f1753e2014-12-02 12:36:45 -0700514 /* Pull out the card/device pair */
515 parse_card_device_params(address, &(out->profile->card), &(out->profile->device));
516
517 profile_read_device_info(out->profile);
518
519 pthread_mutex_unlock(&adev->lock);
520
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700521 int ret = 0;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800522
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700523 /* Rate */
524 if (config->sample_rate == 0) {
525 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(out->profile);
526 } else if (profile_is_sample_rate_valid(out->profile, config->sample_rate)) {
527 proxy_config.rate = config->sample_rate;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800528 } else {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700529 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(out->profile);
530 ret = -EINVAL;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800531 }
Paul McLeaneedc92e2013-12-19 15:46:15 -0800532
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700533 /* Format */
534 if (config->format == AUDIO_FORMAT_DEFAULT) {
535 proxy_config.format = profile_get_default_format(out->profile);
536 config->format = audio_format_from_pcm_format(proxy_config.format);
537 } else {
538 enum pcm_format fmt = pcm_format_from_audio_format(config->format);
539 if (profile_is_format_valid(out->profile, fmt)) {
540 proxy_config.format = fmt;
541 } else {
542 proxy_config.format = profile_get_default_format(out->profile);
543 config->format = audio_format_from_pcm_format(proxy_config.format);
544 ret = -EINVAL;
545 }
546 }
547
548 /* Channels */
Andy Hung182ddc72015-05-05 23:37:30 -0700549 unsigned proposed_channel_count = 0;
Andy Hung03576be2014-07-21 21:16:45 -0700550 if (k_force_channels) {
551 proposed_channel_count = k_force_channels;
Andy Hung182ddc72015-05-05 23:37:30 -0700552 } else if (config->channel_mask == AUDIO_CHANNEL_NONE) {
553 proposed_channel_count = profile_get_default_channel_count(out->profile);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700554 }
Andy Hung182ddc72015-05-05 23:37:30 -0700555 if (proposed_channel_count != 0) {
Andy Hung5e58a302015-06-10 15:17:00 -0700556 if (proposed_channel_count <= FCC_2) {
557 // use channel position mask for mono and stereo
558 config->channel_mask = audio_channel_out_mask_from_count(proposed_channel_count);
559 } else {
560 // use channel index mask for multichannel
Andy Hung182ddc72015-05-05 23:37:30 -0700561 config->channel_mask =
562 audio_channel_mask_for_index_assignment_from_count(proposed_channel_count);
Andy Hung5e58a302015-06-10 15:17:00 -0700563 }
Andy Hung182ddc72015-05-05 23:37:30 -0700564 out->hal_channel_count = proposed_channel_count;
565 } else {
566 out->hal_channel_count = audio_channel_count_from_out_mask(config->channel_mask);
567 }
568 /* we can expose any channel mask, and emulate internally based on channel count. */
569 out->hal_channel_mask = config->channel_mask;
570
Andy Hung03576be2014-07-21 21:16:45 -0700571 /* no validity checks are needed as proxy_prepare() forces channel_count to be valid.
572 * and we emulate any channel count discrepancies in out_write(). */
573 proxy_config.channels = proposed_channel_count;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700574
575 proxy_prepare(&out->proxy, out->profile, &proxy_config);
576
577 /* TODO The retry mechanism isn't implemented in AudioPolicyManager/AudioFlinger. */
578 ret = 0;
579
Paul McLeaneedc92e2013-12-19 15:46:15 -0800580 out->conversion_buffer = NULL;
581 out->conversion_buffer_size = 0;
Simon Wilson19957a32012-04-06 16:17:12 -0700582
583 out->standby = true;
584
585 *stream_out = &out->stream;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700586
587 return ret;
Simon Wilson19957a32012-04-06 16:17:12 -0700588
589err_open:
590 free(out);
591 *stream_out = NULL;
Paul McLeaneedc92e2013-12-19 15:46:15 -0800592 return -ENOSYS;
Simon Wilson19957a32012-04-06 16:17:12 -0700593}
594
595static void adev_close_output_stream(struct audio_hw_device *dev,
596 struct audio_stream_out *stream)
597{
598 struct stream_out *out = (struct stream_out *)stream;
Paul McLean65ec72b2015-02-18 09:13:24 -0800599 ALOGV("adev_close_output_stream(c:%d d:%d)", out->profile->card, out->profile->device);
Simon Wilson19957a32012-04-06 16:17:12 -0700600
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700601 /* Close the pcm device */
Simon Wilson19957a32012-04-06 16:17:12 -0700602 out_standby(&stream->common);
Paul McLeaneedc92e2013-12-19 15:46:15 -0800603
604 free(out->conversion_buffer);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700605
Paul McLeaneedc92e2013-12-19 15:46:15 -0800606 out->conversion_buffer = NULL;
607 out->conversion_buffer_size = 0;
608
Simon Wilson19957a32012-04-06 16:17:12 -0700609 free(stream);
610}
611
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700612static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
613 const struct audio_config *config)
614{
615 /* TODO This needs to be calculated based on format/channels/rate */
616 return 320;
617}
618
619/*
620 * IN functions
621 */
622static uint32_t in_get_sample_rate(const struct audio_stream *stream)
623{
624 uint32_t rate = proxy_get_sample_rate(&((const struct stream_in *)stream)->proxy);
625 ALOGV("in_get_sample_rate() = %d", rate);
626 return rate;
627}
628
629static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
630{
631 ALOGV("in_set_sample_rate(%d) - NOPE", rate);
632 return -ENOSYS;
633}
634
635static size_t in_get_buffer_size(const struct audio_stream *stream)
636{
637 const struct stream_in * in = ((const struct stream_in*)stream);
Paul McLean2cfd81b2014-09-15 12:32:23 -0700638 return proxy_get_period_size(&in->proxy) * audio_stream_in_frame_size(&(in->stream));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700639}
640
641static uint32_t in_get_channels(const struct audio_stream *stream)
642{
Paul McLean2cfd81b2014-09-15 12:32:23 -0700643 const struct stream_in *in = (const struct stream_in*)stream;
Andy Hung780f1f82015-04-22 22:14:39 -0700644 return in->hal_channel_mask;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700645}
646
647static audio_format_t in_get_format(const struct audio_stream *stream)
648{
Andy Hung780f1f82015-04-22 22:14:39 -0700649 alsa_device_proxy *proxy = &((struct stream_in*)stream)->proxy;
650 audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy));
651 return format;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700652}
653
654static int in_set_format(struct audio_stream *stream, audio_format_t format)
655{
656 ALOGV("in_set_format(%d) - NOPE", format);
657
658 return -ENOSYS;
659}
660
661static int in_standby(struct audio_stream *stream)
662{
663 struct stream_in *in = (struct stream_in *)stream;
664
Eric Laurent70318092015-06-19 17:49:17 -0700665 lock_input_stream(in);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700666 if (!in->standby) {
Eric Laurent70318092015-06-19 17:49:17 -0700667 pthread_mutex_lock(&in->dev->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700668 proxy_close(&in->proxy);
Eric Laurent70318092015-06-19 17:49:17 -0700669 pthread_mutex_unlock(&in->dev->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700670 in->standby = true;
671 }
672
673 pthread_mutex_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700674
675 return 0;
676}
677
678static int in_dump(const struct audio_stream *stream, int fd)
679{
680 return 0;
681}
682
683static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
684{
Paul McLean65ec72b2015-02-18 09:13:24 -0800685 ALOGV("in_set_parameters() keys:%s", kvpairs);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700686
687 struct stream_in *in = (struct stream_in *)stream;
688
689 char value[32];
690 int param_val;
691 int routing = 0;
692 int ret_value = 0;
Eric Laurent05333d42014-08-04 20:29:17 -0700693 int card = -1;
694 int device = -1;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700695
Paul McLean65ec72b2015-02-18 09:13:24 -0800696 if (!parse_card_device_params(kvpairs, &card, &device)) {
697 // nothing to do
698 return ret_value;
699 }
700
Eric Laurent70318092015-06-19 17:49:17 -0700701 lock_input_stream(in);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700702 pthread_mutex_lock(&in->dev->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700703
Paul McLean2c6196f2014-08-20 16:50:25 -0700704 if (card >= 0 && device >= 0 && !profile_is_cached_for(in->profile, card, device)) {
Eric Laurent05333d42014-08-04 20:29:17 -0700705 /* cannot read pcm device info if playback is active */
706 if (!in->standby)
707 ret_value = -ENOSYS;
708 else {
709 int saved_card = in->profile->card;
710 int saved_device = in->profile->device;
711 in->profile->card = card;
712 in->profile->device = device;
713 ret_value = profile_read_device_info(in->profile) ? 0 : -EINVAL;
714 if (ret_value != 0) {
715 in->profile->card = saved_card;
716 in->profile->device = saved_device;
717 }
718 }
Paul McLean2c6196f2014-08-20 16:50:25 -0700719 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700720
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700721 pthread_mutex_unlock(&in->dev->lock);
Eric Laurent70318092015-06-19 17:49:17 -0700722 pthread_mutex_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700723
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700724 return ret_value;
725}
726
727static char * in_get_parameters(const struct audio_stream *stream, const char *keys)
728{
729 struct stream_in *in = (struct stream_in *)stream;
730
Eric Laurent70318092015-06-19 17:49:17 -0700731 lock_input_stream(in);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700732 pthread_mutex_lock(&in->dev->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700733
734 char * params_str = device_get_parameters(in->profile, keys);
735
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700736 pthread_mutex_unlock(&in->dev->lock);
Eric Laurent70318092015-06-19 17:49:17 -0700737 pthread_mutex_unlock(&in->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700738
739 return params_str;
740}
741
742static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
743{
744 return 0;
745}
746
747static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
748{
749 return 0;
750}
751
752static int in_set_gain(struct audio_stream_in *stream, float gain)
753{
754 return 0;
755}
756
757/* must be called with hw device and output stream mutexes locked */
758static int start_input_stream(struct stream_in *in)
759{
Paul McLean65ec72b2015-02-18 09:13:24 -0800760 ALOGV("ustart_input_stream(card:%d device:%d)", in->profile->card, in->profile->device);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700761
762 return proxy_open(&in->proxy);
763}
764
765/* TODO mutex stuff here (see out_write) */
766static ssize_t in_read(struct audio_stream_in *stream, void* buffer, size_t bytes)
767{
768 size_t num_read_buff_bytes = 0;
769 void * read_buff = buffer;
770 void * out_buff = buffer;
Pavan Chikkala83b47a62015-01-09 12:21:13 -0800771 int ret = 0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700772
773 struct stream_in * in = (struct stream_in *)stream;
774
Eric Laurent70318092015-06-19 17:49:17 -0700775 lock_input_stream(in);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700776 if (in->standby) {
Eric Laurent70318092015-06-19 17:49:17 -0700777 pthread_mutex_lock(&in->dev->lock);
778 ret = start_input_stream(in);
779 pthread_mutex_unlock(&in->dev->lock);
780 if (ret != 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700781 goto err;
782 }
783 in->standby = false;
784 }
785
786 alsa_device_profile * profile = in->profile;
787
788 /*
789 * OK, we need to figure out how much data to read to be able to output the requested
790 * number of bytes in the HAL format (16-bit, stereo).
791 */
792 num_read_buff_bytes = bytes;
Andy Hung780f1f82015-04-22 22:14:39 -0700793 int num_device_channels = proxy_get_channel_count(&in->proxy); /* what we told Alsa */
794 int num_req_channels = in->hal_channel_count; /* what we told AudioFlinger */
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700795
796 if (num_device_channels != num_req_channels) {
797 num_read_buff_bytes = (num_device_channels * num_read_buff_bytes) / num_req_channels;
798 }
799
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700800 /* Setup/Realloc the conversion buffer (if necessary). */
801 if (num_read_buff_bytes != bytes) {
802 if (num_read_buff_bytes > in->conversion_buffer_size) {
803 /*TODO Remove this when AudioPolicyManger/AudioFlinger support arbitrary formats
804 (and do these conversions themselves) */
805 in->conversion_buffer_size = num_read_buff_bytes;
806 in->conversion_buffer = realloc(in->conversion_buffer, in->conversion_buffer_size);
807 }
808 read_buff = in->conversion_buffer;
809 }
810
Pavan Chikkala83b47a62015-01-09 12:21:13 -0800811 ret = proxy_read(&in->proxy, read_buff, num_read_buff_bytes);
812 if (ret == 0) {
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700813 if (num_device_channels != num_req_channels) {
814 // ALOGV("chans dev:%d req:%d", num_device_channels, num_req_channels);
815
816 out_buff = buffer;
817 /* Num Channels conversion */
818 if (num_device_channels != num_req_channels) {
819 audio_format_t audio_format = in_get_format(&(in->stream.common));
820 unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
821
822 num_read_buff_bytes =
823 adjust_channels(read_buff, num_device_channels,
824 out_buff, num_req_channels,
825 sample_size_in_bytes, num_read_buff_bytes);
826 }
827 }
Eric Laurent253def92014-09-14 12:18:18 -0700828
829 /* no need to acquire in->dev->lock to read mic_muted here as we don't change its state */
830 if (num_read_buff_bytes > 0 && in->dev->mic_muted)
831 memset(buffer, 0, num_read_buff_bytes);
Viswanath L8c7e1112014-10-31 13:07:39 +0530832 } else {
Eric Laurente6499422015-01-09 17:03:27 -0800833 num_read_buff_bytes = 0; // reset the value after USB headset is unplugged
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700834 }
835
836err:
837 pthread_mutex_unlock(&in->lock);
838
839 return num_read_buff_bytes;
840}
841
842static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
843{
844 return 0;
845}
846
847static int adev_open_input_stream(struct audio_hw_device *dev,
848 audio_io_handle_t handle,
849 audio_devices_t devices,
850 struct audio_config *config,
851 struct audio_stream_in **stream_in,
Eric Laurentf5e24692014-07-27 16:14:57 -0700852 audio_input_flags_t flags __unused,
Paul McLean0f1753e2014-12-02 12:36:45 -0700853 const char *address /*__unused*/,
Eric Laurentf5e24692014-07-27 16:14:57 -0700854 audio_source_t source __unused)
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700855{
Paul McLean65ec72b2015-02-18 09:13:24 -0800856 ALOGV("in adev_open_input_stream() rate:%" PRIu32 ", chanMask:0x%" PRIX32 ", fmt:%" PRIu8,
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700857 config->sample_rate, config->channel_mask, config->format);
858
859 struct stream_in *in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
860 int ret = 0;
861
862 if (in == NULL)
863 return -ENOMEM;
864
865 /* setup function pointers */
866 in->stream.common.get_sample_rate = in_get_sample_rate;
867 in->stream.common.set_sample_rate = in_set_sample_rate;
868 in->stream.common.get_buffer_size = in_get_buffer_size;
869 in->stream.common.get_channels = in_get_channels;
870 in->stream.common.get_format = in_get_format;
871 in->stream.common.set_format = in_set_format;
872 in->stream.common.standby = in_standby;
873 in->stream.common.dump = in_dump;
874 in->stream.common.set_parameters = in_set_parameters;
875 in->stream.common.get_parameters = in_get_parameters;
876 in->stream.common.add_audio_effect = in_add_audio_effect;
877 in->stream.common.remove_audio_effect = in_remove_audio_effect;
878
879 in->stream.set_gain = in_set_gain;
880 in->stream.read = in_read;
881 in->stream.get_input_frames_lost = in_get_input_frames_lost;
882
Eric Laurent70318092015-06-19 17:49:17 -0700883 pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL);
884 pthread_mutex_init(&in->pre_lock, (const pthread_mutexattr_t *) NULL);
885
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700886 in->dev = (struct audio_device *)dev;
Paul McLean0f1753e2014-12-02 12:36:45 -0700887 pthread_mutex_lock(&in->dev->lock);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700888
889 in->profile = &in->dev->in_profile;
890
891 struct pcm_config proxy_config;
Paul McLean2c6196f2014-08-20 16:50:25 -0700892 memset(&proxy_config, 0, sizeof(proxy_config));
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700893
Paul McLean0f1753e2014-12-02 12:36:45 -0700894 /* Pull out the card/device pair */
895 parse_card_device_params(address, &(in->profile->card), &(in->profile->device));
896
897 profile_read_device_info(in->profile);
898 pthread_mutex_unlock(&in->dev->lock);
899
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700900 /* Rate */
901 if (config->sample_rate == 0) {
902 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(in->profile);
903 } else if (profile_is_sample_rate_valid(in->profile, config->sample_rate)) {
904 proxy_config.rate = config->sample_rate;
905 } else {
906 proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(in->profile);
907 ret = -EINVAL;
908 }
909
910 /* Format */
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700911 if (config->format == AUDIO_FORMAT_DEFAULT) {
Andy Hung780f1f82015-04-22 22:14:39 -0700912 proxy_config.format = profile_get_default_format(in->profile);
913 config->format = audio_format_from_pcm_format(proxy_config.format);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700914 } else {
Andy Hung780f1f82015-04-22 22:14:39 -0700915 enum pcm_format fmt = pcm_format_from_audio_format(config->format);
916 if (profile_is_format_valid(in->profile, fmt)) {
917 proxy_config.format = fmt;
918 } else {
919 proxy_config.format = profile_get_default_format(in->profile);
920 config->format = audio_format_from_pcm_format(proxy_config.format);
921 ret = -EINVAL;
922 }
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700923 }
924
Paul McLean2cfd81b2014-09-15 12:32:23 -0700925 /* Channels */
Andy Hung780f1f82015-04-22 22:14:39 -0700926 unsigned proposed_channel_count = 0;
Paul McLean2cfd81b2014-09-15 12:32:23 -0700927 if (k_force_channels) {
928 proposed_channel_count = k_force_channels;
Andy Hung780f1f82015-04-22 22:14:39 -0700929 } else if (config->channel_mask == AUDIO_CHANNEL_NONE) {
930 proposed_channel_count = profile_get_default_channel_count(in->profile);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700931 }
Andy Hung780f1f82015-04-22 22:14:39 -0700932 if (proposed_channel_count != 0) {
933 config->channel_mask = audio_channel_in_mask_from_count(proposed_channel_count);
934 if (config->channel_mask == AUDIO_CHANNEL_INVALID)
935 config->channel_mask =
936 audio_channel_mask_for_index_assignment_from_count(proposed_channel_count);
937 in->hal_channel_count = proposed_channel_count;
938 } else {
939 in->hal_channel_count = audio_channel_count_from_in_mask(config->channel_mask);
940 }
941 /* we can expose any channel mask, and emulate internally based on channel count. */
942 in->hal_channel_mask = config->channel_mask;
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700943
Paul McLean2cfd81b2014-09-15 12:32:23 -0700944 proxy_config.channels = profile_get_default_channel_count(in->profile);
Paul McLeanc88e6ae2014-07-16 09:48:34 -0700945 proxy_prepare(&in->proxy, in->profile, &proxy_config);
946
947 in->standby = true;
948
949 in->conversion_buffer = NULL;
950 in->conversion_buffer_size = 0;
951
952 *stream_in = &in->stream;
953
954 return ret;
955}
956
957static void adev_close_input_stream(struct audio_hw_device *dev, struct audio_stream_in *stream)
958{
959 struct stream_in *in = (struct stream_in *)stream;
960
961 /* Close the pcm device */
962 in_standby(&stream->common);
963
964 free(in->conversion_buffer);
965
966 free(stream);
967}
968
969/*
970 * ADEV Functions
971 */
Simon Wilson19957a32012-04-06 16:17:12 -0700972static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
973{
974 return 0;
975}
976
Paul McLean30f41852014-04-16 15:44:20 -0700977static char * adev_get_parameters(const struct audio_hw_device *dev, const char *keys)
Simon Wilson19957a32012-04-06 16:17:12 -0700978{
979 return strdup("");
980}
981
982static int adev_init_check(const struct audio_hw_device *dev)
983{
984 return 0;
985}
986
987static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
988{
989 return -ENOSYS;
990}
991
992static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
993{
994 return -ENOSYS;
995}
996
997static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
998{
999 return 0;
1000}
1001
1002static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
1003{
Eric Laurent253def92014-09-14 12:18:18 -07001004 struct audio_device * adev = (struct audio_device *)dev;
1005 pthread_mutex_lock(&adev->lock);
1006 adev->mic_muted = state;
1007 pthread_mutex_unlock(&adev->lock);
Simon Wilson19957a32012-04-06 16:17:12 -07001008 return -ENOSYS;
1009}
1010
1011static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
1012{
1013 return -ENOSYS;
1014}
1015
Simon Wilson19957a32012-04-06 16:17:12 -07001016static int adev_dump(const audio_hw_device_t *device, int fd)
1017{
1018 return 0;
1019}
1020
1021static int adev_close(hw_device_t *device)
1022{
Paul McLeaneedc92e2013-12-19 15:46:15 -08001023 struct audio_device *adev = (struct audio_device *)device;
Simon Wilson19957a32012-04-06 16:17:12 -07001024 free(device);
Paul McLeaneedc92e2013-12-19 15:46:15 -08001025
Simon Wilson19957a32012-04-06 16:17:12 -07001026 return 0;
1027}
1028
Paul McLean30f41852014-04-16 15:44:20 -07001029static int adev_open(const hw_module_t* module, const char* name, hw_device_t** device)
Simon Wilson19957a32012-04-06 16:17:12 -07001030{
Simon Wilson19957a32012-04-06 16:17:12 -07001031 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
1032 return -EINVAL;
1033
Paul McLeaneedc92e2013-12-19 15:46:15 -08001034 struct audio_device *adev = calloc(1, sizeof(struct audio_device));
Simon Wilson19957a32012-04-06 16:17:12 -07001035 if (!adev)
1036 return -ENOMEM;
1037
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001038 profile_init(&adev->out_profile, PCM_OUT);
1039 profile_init(&adev->in_profile, PCM_IN);
1040
Simon Wilson19957a32012-04-06 16:17:12 -07001041 adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
Eric Laurent85e08e22012-08-28 14:30:35 -07001042 adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
Paul McLeanc88e6ae2014-07-16 09:48:34 -07001043 adev->hw_device.common.module = (struct hw_module_t *)module;
Simon Wilson19957a32012-04-06 16:17:12 -07001044 adev->hw_device.common.close = adev_close;
1045
Simon Wilson19957a32012-04-06 16:17:12 -07001046 adev->hw_device.init_check = adev_init_check;
1047 adev->hw_device.set_voice_volume = adev_set_voice_volume;
1048 adev->hw_device.set_master_volume = adev_set_master_volume;
1049 adev->hw_device.set_mode = adev_set_mode;
1050 adev->hw_device.set_mic_mute = adev_set_mic_mute;
1051 adev->hw_device.get_mic_mute = adev_get_mic_mute;
1052 adev->hw_device.set_parameters = adev_set_parameters;
1053 adev->hw_device.get_parameters = adev_get_parameters;
1054 adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size;
1055 adev->hw_device.open_output_stream = adev_open_output_stream;
1056 adev->hw_device.close_output_stream = adev_close_output_stream;
1057 adev->hw_device.open_input_stream = adev_open_input_stream;
1058 adev->hw_device.close_input_stream = adev_close_input_stream;
1059 adev->hw_device.dump = adev_dump;
1060
1061 *device = &adev->hw_device.common;
1062
1063 return 0;
1064}
1065
1066static struct hw_module_methods_t hal_module_methods = {
1067 .open = adev_open,
1068};
1069
1070struct audio_module HAL_MODULE_INFO_SYM = {
1071 .common = {
1072 .tag = HARDWARE_MODULE_TAG,
Mike Lockwood46a98092012-04-24 16:41:18 -07001073 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
1074 .hal_api_version = HARDWARE_HAL_API_VERSION,
Simon Wilson19957a32012-04-06 16:17:12 -07001075 .id = AUDIO_HARDWARE_MODULE_ID,
1076 .name = "USB audio HW HAL",
1077 .author = "The Android Open Source Project",
1078 .methods = &hal_module_methods,
1079 },
1080};