blob: 4022efd94ae6e80e1a20831c816443b2ad0f1162 [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
17#define LOG_TAG "usb_audio_hw"
18/*#define LOG_NDEBUG 0*/
19
20#include <errno.h>
21#include <pthread.h>
22#include <stdint.h>
23#include <sys/time.h>
24#include <stdlib.h>
25
26#include <cutils/log.h>
27#include <cutils/str_parms.h>
28#include <cutils/properties.h>
29
30#include <hardware/hardware.h>
31#include <system/audio.h>
32#include <hardware/audio.h>
33
34#include <tinyalsa/asoundlib.h>
35
36struct pcm_config pcm_config = {
37 .channels = 2,
38 .rate = 44100,
39 .period_size = 1024,
40 .period_count = 4,
41 .format = PCM_FORMAT_S16_LE,
42};
43
44struct audio_device {
45 struct audio_hw_device hw_device;
46
47 pthread_mutex_t lock; /* see note below on mutex acquisition order */
48 int card;
49 int device;
50 bool standby;
51};
52
53struct stream_out {
54 struct audio_stream_out stream;
55
56 pthread_mutex_t lock; /* see note below on mutex acquisition order */
57 struct pcm *pcm;
58 bool standby;
59
60 struct audio_device *dev;
61};
62
63/**
64 * NOTE: when multiple mutexes have to be acquired, always respect the
65 * following order: hw device > out stream
66 */
67
68/* Helper functions */
69
70/* must be called with hw device and output stream mutexes locked */
71static int start_output_stream(struct stream_out *out)
72{
73 struct audio_device *adev = out->dev;
74 int i;
75
76 if ((adev->card < 0) || (adev->device < 0))
77 return -EINVAL;
78
79 out->pcm = pcm_open(adev->card, adev->device, PCM_OUT, &pcm_config);
80
81 if (out->pcm && !pcm_is_ready(out->pcm)) {
82 ALOGE("pcm_open() failed: %s", pcm_get_error(out->pcm));
83 pcm_close(out->pcm);
84 return -ENOMEM;
85 }
86
87 return 0;
88}
89
90/* API functions */
91
92static uint32_t out_get_sample_rate(const struct audio_stream *stream)
93{
94 return pcm_config.rate;
95}
96
97static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
98{
99 return 0;
100}
101
102static size_t out_get_buffer_size(const struct audio_stream *stream)
103{
104 return pcm_config.period_size *
105 audio_stream_frame_size((struct audio_stream *)stream);
106}
107
108static uint32_t out_get_channels(const struct audio_stream *stream)
109{
110 return AUDIO_CHANNEL_OUT_STEREO;
111}
112
113static audio_format_t out_get_format(const struct audio_stream *stream)
114{
115 return AUDIO_FORMAT_PCM_16_BIT;
116}
117
118static int out_set_format(struct audio_stream *stream, audio_format_t format)
119{
120 return 0;
121}
122
123static int out_standby(struct audio_stream *stream)
124{
125 struct stream_out *out = (struct stream_out *)stream;
126
127 pthread_mutex_lock(&out->dev->lock);
128 pthread_mutex_lock(&out->lock);
129
130 if (!out->standby) {
131 pcm_close(out->pcm);
132 out->pcm = NULL;
133 out->standby = true;
134 }
135
136 pthread_mutex_unlock(&out->lock);
137 pthread_mutex_unlock(&out->dev->lock);
138
139 return 0;
140}
141
142static int out_dump(const struct audio_stream *stream, int fd)
143{
144 return 0;
145}
146
147static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
148{
149 struct stream_out *out = (struct stream_out *)stream;
150 struct audio_device *adev = out->dev;
151 struct str_parms *parms;
152 char value[32];
153 int ret;
154 int routing = 0;
155
156 parms = str_parms_create_str(kvpairs);
157 pthread_mutex_lock(&adev->lock);
158
159 adev->card = -1;
160 adev->device = -1;
161
162 ret = str_parms_get_str(parms, "card", value, sizeof(value));
163 if (ret >= 0)
164 adev->card = atoi(value);
165
166 ret = str_parms_get_str(parms, "device", value, sizeof(value));
167 if (ret >= 0)
168 adev->device = atoi(value);
169
170 pthread_mutex_unlock(&adev->lock);
171 str_parms_destroy(parms);
172
173 return 0;
174}
175
176static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
177{
178 return strdup("");
179}
180
181static uint32_t out_get_latency(const struct audio_stream_out *stream)
182{
183 return (pcm_config.period_size * pcm_config.period_count * 1000) /
184 out_get_sample_rate(&stream->common);
185}
186
187static int out_set_volume(struct audio_stream_out *stream, float left,
188 float right)
189{
190 return -ENOSYS;
191}
192
193static ssize_t out_write(struct audio_stream_out *stream, const void* buffer,
194 size_t bytes)
195{
196 int ret;
197 struct stream_out *out = (struct stream_out *)stream;
198
199 pthread_mutex_lock(&out->dev->lock);
200 pthread_mutex_lock(&out->lock);
201 if (out->standby) {
202 ret = start_output_stream(out);
203 if (ret != 0) {
204 goto err;
205 }
206 out->standby = false;
207 }
208
209 pcm_write(out->pcm, (void *)buffer, bytes);
210
211 pthread_mutex_unlock(&out->lock);
212 pthread_mutex_unlock(&out->dev->lock);
213
214 return bytes;
215
216err:
217 pthread_mutex_unlock(&out->lock);
218
219 if (ret != 0) {
220 usleep(bytes * 1000000 / audio_stream_frame_size(&stream->common) /
221 out_get_sample_rate(&stream->common));
222 }
223
224 return bytes;
225}
226
227static int out_get_render_position(const struct audio_stream_out *stream,
228 uint32_t *dsp_frames)
229{
230 return -EINVAL;
231}
232
233static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
234{
235 return 0;
236}
237
238static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
239{
240 return 0;
241}
242
243static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
244 int64_t *timestamp)
245{
246 return -EINVAL;
247}
248
249static int adev_open_output_stream(struct audio_hw_device *dev,
250 uint32_t devices, audio_format_t *format,
251 uint32_t *channels, uint32_t *sample_rate,
252 struct audio_stream_out **stream_out)
253{
254 struct audio_device *adev = (struct audio_device *)dev;
255 struct stream_out *out;
256 int ret;
257
258 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
259 if (!out)
260 return -ENOMEM;
261
262 out->stream.common.get_sample_rate = out_get_sample_rate;
263 out->stream.common.set_sample_rate = out_set_sample_rate;
264 out->stream.common.get_buffer_size = out_get_buffer_size;
265 out->stream.common.get_channels = out_get_channels;
266 out->stream.common.get_format = out_get_format;
267 out->stream.common.set_format = out_set_format;
268 out->stream.common.standby = out_standby;
269 out->stream.common.dump = out_dump;
270 out->stream.common.set_parameters = out_set_parameters;
271 out->stream.common.get_parameters = out_get_parameters;
272 out->stream.common.add_audio_effect = out_add_audio_effect;
273 out->stream.common.remove_audio_effect = out_remove_audio_effect;
274 out->stream.get_latency = out_get_latency;
275 out->stream.set_volume = out_set_volume;
276 out->stream.write = out_write;
277 out->stream.get_render_position = out_get_render_position;
278 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
279
280 out->dev = adev;
281
282 *format = out_get_format(&out->stream.common);
283 *channels = out_get_channels(&out->stream.common);
284 *sample_rate = out_get_sample_rate(&out->stream.common);
285
286 out->standby = true;
287
288 *stream_out = &out->stream;
289 return 0;
290
291err_open:
292 free(out);
293 *stream_out = NULL;
294 return ret;
295}
296
297static void adev_close_output_stream(struct audio_hw_device *dev,
298 struct audio_stream_out *stream)
299{
300 struct stream_out *out = (struct stream_out *)stream;
301
302 out_standby(&stream->common);
303 free(stream);
304}
305
306static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
307{
308 return 0;
309}
310
311static char * adev_get_parameters(const struct audio_hw_device *dev,
312 const char *keys)
313{
314 return strdup("");
315}
316
317static int adev_init_check(const struct audio_hw_device *dev)
318{
319 return 0;
320}
321
322static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
323{
324 return -ENOSYS;
325}
326
327static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
328{
329 return -ENOSYS;
330}
331
332static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
333{
334 return 0;
335}
336
337static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
338{
339 return -ENOSYS;
340}
341
342static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
343{
344 return -ENOSYS;
345}
346
347static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
348 uint32_t sample_rate, audio_format_t format,
349 int channel_count)
350{
351 return 0;
352}
353
354static int adev_open_input_stream(struct audio_hw_device *dev, uint32_t devices,
355 audio_format_t *format, uint32_t *channel_mask,
356 uint32_t *sample_rate,
357 audio_in_acoustics_t acoustics,
358 struct audio_stream_in **stream_in)
359{
360 return -ENOSYS;
361}
362
363static void adev_close_input_stream(struct audio_hw_device *dev,
364 struct audio_stream_in *stream)
365{
366}
367
368static int adev_dump(const audio_hw_device_t *device, int fd)
369{
370 return 0;
371}
372
373static int adev_close(hw_device_t *device)
374{
375 struct audio_device *adev = (struct audio_device *)device;
376
377 free(device);
378 return 0;
379}
380
381static uint32_t adev_get_supported_devices(const struct audio_hw_device *dev)
382{
383 return AUDIO_DEVICE_OUT_ALL_USB;
384}
385
386static int adev_open(const hw_module_t* module, const char* name,
387 hw_device_t** device)
388{
389 struct audio_device *adev;
390 int ret;
391
392 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
393 return -EINVAL;
394
395 adev = calloc(1, sizeof(struct audio_device));
396 if (!adev)
397 return -ENOMEM;
398
399 adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
400 adev->hw_device.common.version = 0;
401 adev->hw_device.common.module = (struct hw_module_t *) module;
402 adev->hw_device.common.close = adev_close;
403
404 adev->hw_device.get_supported_devices = adev_get_supported_devices;
405 adev->hw_device.init_check = adev_init_check;
406 adev->hw_device.set_voice_volume = adev_set_voice_volume;
407 adev->hw_device.set_master_volume = adev_set_master_volume;
408 adev->hw_device.set_mode = adev_set_mode;
409 adev->hw_device.set_mic_mute = adev_set_mic_mute;
410 adev->hw_device.get_mic_mute = adev_get_mic_mute;
411 adev->hw_device.set_parameters = adev_set_parameters;
412 adev->hw_device.get_parameters = adev_get_parameters;
413 adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size;
414 adev->hw_device.open_output_stream = adev_open_output_stream;
415 adev->hw_device.close_output_stream = adev_close_output_stream;
416 adev->hw_device.open_input_stream = adev_open_input_stream;
417 adev->hw_device.close_input_stream = adev_close_input_stream;
418 adev->hw_device.dump = adev_dump;
419
420 *device = &adev->hw_device.common;
421
422 return 0;
423}
424
425static struct hw_module_methods_t hal_module_methods = {
426 .open = adev_open,
427};
428
429struct audio_module HAL_MODULE_INFO_SYM = {
430 .common = {
431 .tag = HARDWARE_MODULE_TAG,
432 .version_major = 1,
433 .version_minor = 0,
434 .id = AUDIO_HARDWARE_MODULE_ID,
435 .name = "USB audio HW HAL",
436 .author = "The Android Open Source Project",
437 .methods = &hal_module_methods,
438 },
439};