blob: a482604306ca101d3f8fd02d7c2c97da99890137 [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
63static int out_get_format(const struct audio_stream *stream)
64{
65 return AUDIO_FORMAT_PCM_16_BIT;
66}
67
68static int out_set_format(struct audio_stream *stream, int format)
69{
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
Dima Zavin8cc353a2011-04-20 16:38:05 -0700129/** audio_stream_in implementation **/
130static uint32_t in_get_sample_rate(const struct audio_stream *stream)
131{
132 return 8000;
133}
134
135static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
136{
137 return 0;
138}
139
140static size_t in_get_buffer_size(const struct audio_stream *stream)
141{
142 return 320;
143}
144
145static uint32_t in_get_channels(const struct audio_stream *stream)
146{
147 return AUDIO_CHANNEL_IN_MONO;
148}
149
150static int in_get_format(const struct audio_stream *stream)
151{
152 return AUDIO_FORMAT_PCM_16_BIT;
153}
154
155static int in_set_format(struct audio_stream *stream, int format)
156{
157 return 0;
158}
159
160static int in_standby(struct audio_stream *stream)
161{
162 return 0;
163}
164
165static int in_dump(const struct audio_stream *stream, int fd)
166{
167 return 0;
168}
169
170static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
171{
172 return 0;
173}
174
175static char * in_get_parameters(const struct audio_stream *stream,
176 const char *keys)
177{
178 return strdup("");
179}
180
181static int in_set_gain(struct audio_stream_in *stream, float gain)
182{
183 return 0;
184}
185
186static ssize_t in_read(struct audio_stream_in *stream, void* buffer,
187 size_t bytes)
188{
189 /* XXX: fake timing for audio input */
190 usleep(bytes * 1000000 / audio_stream_frame_size(&stream->common) /
191 in_get_sample_rate(&stream->common));
192 return bytes;
193}
194
195static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
196{
197 return 0;
198}
199
Eric Laurentf3008aa2011-06-17 16:53:12 -0700200static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
201{
202 return 0;
203}
204
205static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
206{
207 return 0;
208}
Dima Zavin8cc353a2011-04-20 16:38:05 -0700209
210static int adev_open_output_stream(struct audio_hw_device *dev,
211 uint32_t devices, int *format,
212 uint32_t *channels, uint32_t *sample_rate,
213 struct audio_stream_out **stream_out)
214{
215 struct stub_audio_device *ladev = (struct stub_audio_device *)dev;
216 struct stub_stream_out *out;
217 int ret;
218
219 out = (struct stub_stream_out *)calloc(1, sizeof(struct stub_stream_out));
220 if (!out)
221 return -ENOMEM;
222
223 out->stream.common.get_sample_rate = out_get_sample_rate;
224 out->stream.common.set_sample_rate = out_set_sample_rate;
225 out->stream.common.get_buffer_size = out_get_buffer_size;
226 out->stream.common.get_channels = out_get_channels;
227 out->stream.common.get_format = out_get_format;
228 out->stream.common.set_format = out_set_format;
229 out->stream.common.standby = out_standby;
230 out->stream.common.dump = out_dump;
231 out->stream.common.set_parameters = out_set_parameters;
232 out->stream.common.get_parameters = out_get_parameters;
Eric Laurentf3008aa2011-06-17 16:53:12 -0700233 out->stream.common.add_audio_effect = out_add_audio_effect;
234 out->stream.common.remove_audio_effect = out_remove_audio_effect;
Dima Zavin8cc353a2011-04-20 16:38:05 -0700235 out->stream.get_latency = out_get_latency;
236 out->stream.set_volume = out_set_volume;
237 out->stream.write = out_write;
238 out->stream.get_render_position = out_get_render_position;
239
240 *stream_out = &out->stream;
241 return 0;
242
243err_open:
244 free(out);
245 *stream_out = NULL;
246 return ret;
247}
248
249static void adev_close_output_stream(struct audio_hw_device *dev,
250 struct audio_stream_out *stream)
251{
252 free(stream);
253}
254
255static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
256{
257 return -ENOSYS;
258}
259
260static char * adev_get_parameters(const struct audio_hw_device *dev,
261 const char *keys)
262{
263 return NULL;
264}
265
266static int adev_init_check(const struct audio_hw_device *dev)
267{
268 return 0;
269}
270
271static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
272{
273 return -ENOSYS;
274}
275
276static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
277{
278 return -ENOSYS;
279}
280
281static int adev_set_mode(struct audio_hw_device *dev, int mode)
282{
283 return 0;
284}
285
286static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
287{
288 return -ENOSYS;
289}
290
291static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
292{
293 return -ENOSYS;
294}
295
296static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
297 uint32_t sample_rate, int format,
298 int channel_count)
299{
300 return 320;
301}
302
303static int adev_open_input_stream(struct audio_hw_device *dev, uint32_t devices,
304 int *format, uint32_t *channels,
305 uint32_t *sample_rate,
306 audio_in_acoustics_t acoustics,
307 struct audio_stream_in **stream_in)
308{
309 struct stub_audio_device *ladev = (struct stub_audio_device *)dev;
310 struct stub_stream_in *in;
311 int ret;
312
313 in = (struct stub_stream_in *)calloc(1, sizeof(struct stub_stream_in));
314 if (!in)
315 return -ENOMEM;
316
317 in->stream.common.get_sample_rate = in_get_sample_rate;
318 in->stream.common.set_sample_rate = in_set_sample_rate;
319 in->stream.common.get_buffer_size = in_get_buffer_size;
320 in->stream.common.get_channels = in_get_channels;
321 in->stream.common.get_format = in_get_format;
322 in->stream.common.set_format = in_set_format;
323 in->stream.common.standby = in_standby;
324 in->stream.common.dump = in_dump;
325 in->stream.common.set_parameters = in_set_parameters;
326 in->stream.common.get_parameters = in_get_parameters;
Eric Laurentf3008aa2011-06-17 16:53:12 -0700327 in->stream.common.add_audio_effect = in_add_audio_effect;
328 in->stream.common.remove_audio_effect = in_remove_audio_effect;
Dima Zavin8cc353a2011-04-20 16:38:05 -0700329 in->stream.set_gain = in_set_gain;
330 in->stream.read = in_read;
331 in->stream.get_input_frames_lost = in_get_input_frames_lost;
332
333 *stream_in = &in->stream;
334 return 0;
335
336err_open:
337 free(in);
338 *stream_in = NULL;
339 return ret;
340}
341
342static void adev_close_input_stream(struct audio_hw_device *dev,
343 struct audio_stream_in *in)
344{
345 return;
346}
347
348static int adev_dump(const audio_hw_device_t *device, int fd)
349{
350 return 0;
351}
352
353static int adev_close(hw_device_t *device)
354{
355 free(device);
356 return 0;
357}
358
359static uint32_t adev_get_supported_devices(const struct audio_hw_device *dev)
360{
361 return (/* OUT */
362 AUDIO_DEVICE_OUT_EARPIECE |
363 AUDIO_DEVICE_OUT_SPEAKER |
364 AUDIO_DEVICE_OUT_WIRED_HEADSET |
365 AUDIO_DEVICE_OUT_WIRED_HEADPHONE |
366 AUDIO_DEVICE_OUT_AUX_DIGITAL |
367 AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET |
368 AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET |
369 AUDIO_DEVICE_OUT_ALL_SCO |
370 AUDIO_DEVICE_OUT_DEFAULT |
371 /* IN */
372 AUDIO_DEVICE_IN_COMMUNICATION |
373 AUDIO_DEVICE_IN_AMBIENT |
374 AUDIO_DEVICE_IN_BUILTIN_MIC |
375 AUDIO_DEVICE_IN_WIRED_HEADSET |
376 AUDIO_DEVICE_IN_AUX_DIGITAL |
377 AUDIO_DEVICE_IN_BACK_MIC |
378 AUDIO_DEVICE_IN_ALL_SCO |
379 AUDIO_DEVICE_IN_DEFAULT);
380}
381
382static int adev_open(const hw_module_t* module, const char* name,
383 hw_device_t** device)
384{
385 struct stub_audio_device *adev;
386 int ret;
387
388 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
389 return -EINVAL;
390
391 adev = calloc(1, sizeof(struct stub_audio_device));
392 if (!adev)
393 return -ENOMEM;
394
395 adev->device.common.tag = HARDWARE_DEVICE_TAG;
396 adev->device.common.version = 0;
397 adev->device.common.module = (struct hw_module_t *) module;
398 adev->device.common.close = adev_close;
399
400 adev->device.get_supported_devices = adev_get_supported_devices;
401 adev->device.init_check = adev_init_check;
402 adev->device.set_voice_volume = adev_set_voice_volume;
403 adev->device.set_master_volume = adev_set_master_volume;
404 adev->device.set_mode = adev_set_mode;
405 adev->device.set_mic_mute = adev_set_mic_mute;
406 adev->device.get_mic_mute = adev_get_mic_mute;
407 adev->device.set_parameters = adev_set_parameters;
408 adev->device.get_parameters = adev_get_parameters;
409 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
410 adev->device.open_output_stream = adev_open_output_stream;
411 adev->device.close_output_stream = adev_close_output_stream;
412 adev->device.open_input_stream = adev_open_input_stream;
413 adev->device.close_input_stream = adev_close_input_stream;
414 adev->device.dump = adev_dump;
415
416 *device = &adev->device.common;
417
418 return 0;
419}
420
421static struct hw_module_methods_t hal_module_methods = {
422 .open = adev_open,
423};
424
425struct audio_module HAL_MODULE_INFO_SYM = {
426 .common = {
427 .tag = HARDWARE_MODULE_TAG,
428 .version_major = 1,
429 .version_minor = 0,
430 .id = AUDIO_HARDWARE_MODULE_ID,
431 .name = "Default audio HW HAL",
432 .author = "The Android Open Source Project",
433 .methods = &hal_module_methods,
434 },
435};