blob: 4069acea23fd9e74559ee5d82624c1cc7f7c2d96 [file] [log] [blame]
Dima Zavin8cc353a2011-04-20 16:38:05 -07001/*
2 * Copyright (C) 2011 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 "audio_hw_default"
18//#define LOG_NDEBUG 0
19
20#include <errno.h>
21#include <pthread.h>
22#include <stdint.h>
23#include <sys/time.h>
24
25#include <cutils/log.h>
26
27#include <hardware/hardware.h>
Dima Zavinaa211722011-05-11 14:15:53 -070028#include <system/audio.h>
Dima Zavin3bc15862011-06-13 17:59:54 -070029#include <hardware/audio.h>
Dima Zavin8cc353a2011-04-20 16:38:05 -070030
31struct stub_audio_device {
32 struct audio_hw_device device;
33};
34
35struct stub_stream_out {
36 struct audio_stream_out stream;
37};
38
39struct stub_stream_in {
40 struct audio_stream_in stream;
41};
42
43static uint32_t out_get_sample_rate(const struct audio_stream *stream)
44{
45 return 44100;
46}
47
48static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
49{
50 return 0;
51}
52
53static size_t out_get_buffer_size(const struct audio_stream *stream)
54{
55 return 4096;
56}
57
58static uint32_t out_get_channels(const struct audio_stream *stream)
59{
60 return AUDIO_CHANNEL_OUT_STEREO;
61}
62
Glenn Kastenfe79eb32012-01-12 14:55:57 -080063static audio_format_t out_get_format(const struct audio_stream *stream)
Dima Zavin8cc353a2011-04-20 16:38:05 -070064{
65 return AUDIO_FORMAT_PCM_16_BIT;
66}
67
Glenn Kastenfe79eb32012-01-12 14:55:57 -080068static int out_set_format(struct audio_stream *stream, audio_format_t format)
Dima Zavin8cc353a2011-04-20 16:38:05 -070069{
70 return 0;
71}
72
73static int out_standby(struct audio_stream *stream)
74{
75 return 0;
76}
77
78static int out_dump(const struct audio_stream *stream, int fd)
79{
80 return 0;
81}
82
83static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
84{
85 return 0;
86}
87
88static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
89{
90 return strdup("");
91}
92
93static uint32_t out_get_latency(const struct audio_stream_out *stream)
94{
95 return 0;
96}
97
98static int out_set_volume(struct audio_stream_out *stream, float left,
99 float right)
100{
101 return 0;
102}
103
104static ssize_t out_write(struct audio_stream_out *stream, const void* buffer,
105 size_t bytes)
106{
107 /* XXX: fake timing for audio output */
108 usleep(bytes * 1000000 / audio_stream_frame_size(&stream->common) /
109 out_get_sample_rate(&stream->common));
110 return bytes;
111}
112
113static int out_get_render_position(const struct audio_stream_out *stream,
114 uint32_t *dsp_frames)
115{
116 return -EINVAL;
117}
118
Eric Laurentf3008aa2011-06-17 16:53:12 -0700119static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
120{
121 return 0;
122}
123
124static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
125{
126 return 0;
127}
128
Mike J. Chen5ad38a92011-08-15 12:05:00 -0700129static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
130 int64_t *timestamp)
131{
132 return -EINVAL;
133}
134
Dima Zavin8cc353a2011-04-20 16:38:05 -0700135/** audio_stream_in implementation **/
136static uint32_t in_get_sample_rate(const struct audio_stream *stream)
137{
138 return 8000;
139}
140
141static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
142{
143 return 0;
144}
145
146static size_t in_get_buffer_size(const struct audio_stream *stream)
147{
148 return 320;
149}
150
151static uint32_t in_get_channels(const struct audio_stream *stream)
152{
153 return AUDIO_CHANNEL_IN_MONO;
154}
155
Glenn Kastenfe79eb32012-01-12 14:55:57 -0800156static audio_format_t in_get_format(const struct audio_stream *stream)
Dima Zavin8cc353a2011-04-20 16:38:05 -0700157{
158 return AUDIO_FORMAT_PCM_16_BIT;
159}
160
Glenn Kastenfe79eb32012-01-12 14:55:57 -0800161static int in_set_format(struct audio_stream *stream, audio_format_t format)
Dima Zavin8cc353a2011-04-20 16:38:05 -0700162{
163 return 0;
164}
165
166static int in_standby(struct audio_stream *stream)
167{
168 return 0;
169}
170
171static int in_dump(const struct audio_stream *stream, int fd)
172{
173 return 0;
174}
175
176static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
177{
178 return 0;
179}
180
181static char * in_get_parameters(const struct audio_stream *stream,
182 const char *keys)
183{
184 return strdup("");
185}
186
187static int in_set_gain(struct audio_stream_in *stream, float gain)
188{
189 return 0;
190}
191
192static ssize_t in_read(struct audio_stream_in *stream, void* buffer,
193 size_t bytes)
194{
195 /* XXX: fake timing for audio input */
196 usleep(bytes * 1000000 / audio_stream_frame_size(&stream->common) /
197 in_get_sample_rate(&stream->common));
198 return bytes;
199}
200
201static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
202{
203 return 0;
204}
205
Eric Laurentf3008aa2011-06-17 16:53:12 -0700206static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
207{
208 return 0;
209}
210
211static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
212{
213 return 0;
214}
Dima Zavin8cc353a2011-04-20 16:38:05 -0700215
216static int adev_open_output_stream(struct audio_hw_device *dev,
Glenn Kastenfe79eb32012-01-12 14:55:57 -0800217 uint32_t devices, audio_format_t *format,
Dima Zavin8cc353a2011-04-20 16:38:05 -0700218 uint32_t *channels, uint32_t *sample_rate,
219 struct audio_stream_out **stream_out)
220{
221 struct stub_audio_device *ladev = (struct stub_audio_device *)dev;
222 struct stub_stream_out *out;
223 int ret;
224
225 out = (struct stub_stream_out *)calloc(1, sizeof(struct stub_stream_out));
226 if (!out)
227 return -ENOMEM;
228
229 out->stream.common.get_sample_rate = out_get_sample_rate;
230 out->stream.common.set_sample_rate = out_set_sample_rate;
231 out->stream.common.get_buffer_size = out_get_buffer_size;
232 out->stream.common.get_channels = out_get_channels;
233 out->stream.common.get_format = out_get_format;
234 out->stream.common.set_format = out_set_format;
235 out->stream.common.standby = out_standby;
236 out->stream.common.dump = out_dump;
237 out->stream.common.set_parameters = out_set_parameters;
238 out->stream.common.get_parameters = out_get_parameters;
Eric Laurentf3008aa2011-06-17 16:53:12 -0700239 out->stream.common.add_audio_effect = out_add_audio_effect;
240 out->stream.common.remove_audio_effect = out_remove_audio_effect;
Dima Zavin8cc353a2011-04-20 16:38:05 -0700241 out->stream.get_latency = out_get_latency;
242 out->stream.set_volume = out_set_volume;
243 out->stream.write = out_write;
244 out->stream.get_render_position = out_get_render_position;
Mike J. Chen5ad38a92011-08-15 12:05:00 -0700245 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Dima Zavin8cc353a2011-04-20 16:38:05 -0700246
247 *stream_out = &out->stream;
248 return 0;
249
250err_open:
251 free(out);
252 *stream_out = NULL;
253 return ret;
254}
255
256static void adev_close_output_stream(struct audio_hw_device *dev,
257 struct audio_stream_out *stream)
258{
259 free(stream);
260}
261
262static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
263{
264 return -ENOSYS;
265}
266
267static char * adev_get_parameters(const struct audio_hw_device *dev,
268 const char *keys)
269{
270 return NULL;
271}
272
273static int adev_init_check(const struct audio_hw_device *dev)
274{
275 return 0;
276}
277
278static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
279{
280 return -ENOSYS;
281}
282
283static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
284{
285 return -ENOSYS;
286}
287
Mike J. Chen5ad38a92011-08-15 12:05:00 -0700288static int adev_get_master_volume(struct audio_hw_device *dev,
289 float *volume)
290{
291 return -ENOSYS;
292}
293
Glenn Kasten6df641e2012-01-09 10:41:30 -0800294static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
Dima Zavin8cc353a2011-04-20 16:38:05 -0700295{
296 return 0;
297}
298
299static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
300{
301 return -ENOSYS;
302}
303
304static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
305{
306 return -ENOSYS;
307}
308
309static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
Glenn Kastenfe79eb32012-01-12 14:55:57 -0800310 uint32_t sample_rate, audio_format_t format,
Dima Zavin8cc353a2011-04-20 16:38:05 -0700311 int channel_count)
312{
313 return 320;
314}
315
316static int adev_open_input_stream(struct audio_hw_device *dev, uint32_t devices,
Glenn Kastenfe79eb32012-01-12 14:55:57 -0800317 audio_format_t *format, uint32_t *channels,
Dima Zavin8cc353a2011-04-20 16:38:05 -0700318 uint32_t *sample_rate,
319 audio_in_acoustics_t acoustics,
320 struct audio_stream_in **stream_in)
321{
322 struct stub_audio_device *ladev = (struct stub_audio_device *)dev;
323 struct stub_stream_in *in;
324 int ret;
325
326 in = (struct stub_stream_in *)calloc(1, sizeof(struct stub_stream_in));
327 if (!in)
328 return -ENOMEM;
329
330 in->stream.common.get_sample_rate = in_get_sample_rate;
331 in->stream.common.set_sample_rate = in_set_sample_rate;
332 in->stream.common.get_buffer_size = in_get_buffer_size;
333 in->stream.common.get_channels = in_get_channels;
334 in->stream.common.get_format = in_get_format;
335 in->stream.common.set_format = in_set_format;
336 in->stream.common.standby = in_standby;
337 in->stream.common.dump = in_dump;
338 in->stream.common.set_parameters = in_set_parameters;
339 in->stream.common.get_parameters = in_get_parameters;
Eric Laurentf3008aa2011-06-17 16:53:12 -0700340 in->stream.common.add_audio_effect = in_add_audio_effect;
341 in->stream.common.remove_audio_effect = in_remove_audio_effect;
Dima Zavin8cc353a2011-04-20 16:38:05 -0700342 in->stream.set_gain = in_set_gain;
343 in->stream.read = in_read;
344 in->stream.get_input_frames_lost = in_get_input_frames_lost;
345
346 *stream_in = &in->stream;
347 return 0;
348
349err_open:
350 free(in);
351 *stream_in = NULL;
352 return ret;
353}
354
355static void adev_close_input_stream(struct audio_hw_device *dev,
356 struct audio_stream_in *in)
357{
358 return;
359}
360
361static int adev_dump(const audio_hw_device_t *device, int fd)
362{
363 return 0;
364}
365
366static int adev_close(hw_device_t *device)
367{
368 free(device);
369 return 0;
370}
371
372static uint32_t adev_get_supported_devices(const struct audio_hw_device *dev)
373{
374 return (/* OUT */
375 AUDIO_DEVICE_OUT_EARPIECE |
376 AUDIO_DEVICE_OUT_SPEAKER |
377 AUDIO_DEVICE_OUT_WIRED_HEADSET |
378 AUDIO_DEVICE_OUT_WIRED_HEADPHONE |
379 AUDIO_DEVICE_OUT_AUX_DIGITAL |
380 AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET |
381 AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET |
382 AUDIO_DEVICE_OUT_ALL_SCO |
383 AUDIO_DEVICE_OUT_DEFAULT |
384 /* IN */
385 AUDIO_DEVICE_IN_COMMUNICATION |
386 AUDIO_DEVICE_IN_AMBIENT |
387 AUDIO_DEVICE_IN_BUILTIN_MIC |
388 AUDIO_DEVICE_IN_WIRED_HEADSET |
389 AUDIO_DEVICE_IN_AUX_DIGITAL |
390 AUDIO_DEVICE_IN_BACK_MIC |
391 AUDIO_DEVICE_IN_ALL_SCO |
392 AUDIO_DEVICE_IN_DEFAULT);
393}
394
395static int adev_open(const hw_module_t* module, const char* name,
396 hw_device_t** device)
397{
398 struct stub_audio_device *adev;
399 int ret;
400
401 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
402 return -EINVAL;
403
404 adev = calloc(1, sizeof(struct stub_audio_device));
405 if (!adev)
406 return -ENOMEM;
407
408 adev->device.common.tag = HARDWARE_DEVICE_TAG;
409 adev->device.common.version = 0;
410 adev->device.common.module = (struct hw_module_t *) module;
411 adev->device.common.close = adev_close;
412
413 adev->device.get_supported_devices = adev_get_supported_devices;
414 adev->device.init_check = adev_init_check;
415 adev->device.set_voice_volume = adev_set_voice_volume;
416 adev->device.set_master_volume = adev_set_master_volume;
Mike J. Chen5ad38a92011-08-15 12:05:00 -0700417 adev->device.get_master_volume = adev_get_master_volume;
Dima Zavin8cc353a2011-04-20 16:38:05 -0700418 adev->device.set_mode = adev_set_mode;
419 adev->device.set_mic_mute = adev_set_mic_mute;
420 adev->device.get_mic_mute = adev_get_mic_mute;
421 adev->device.set_parameters = adev_set_parameters;
422 adev->device.get_parameters = adev_get_parameters;
423 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
424 adev->device.open_output_stream = adev_open_output_stream;
425 adev->device.close_output_stream = adev_close_output_stream;
426 adev->device.open_input_stream = adev_open_input_stream;
427 adev->device.close_input_stream = adev_close_input_stream;
428 adev->device.dump = adev_dump;
429
430 *device = &adev->device.common;
431
432 return 0;
433}
434
435static struct hw_module_methods_t hal_module_methods = {
436 .open = adev_open,
437};
438
439struct audio_module HAL_MODULE_INFO_SYM = {
440 .common = {
441 .tag = HARDWARE_MODULE_TAG,
442 .version_major = 1,
443 .version_minor = 0,
444 .id = AUDIO_HARDWARE_MODULE_ID,
445 .name = "Default audio HW HAL",
446 .author = "The Android Open Source Project",
447 .methods = &hal_module_methods,
448 },
449};