1/*
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>
28#include <system/audio.h>
29#include <hardware/audio.h>
30
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 audio_channel_mask_t out_get_channels(const struct audio_stream *stream)
59{
60    return AUDIO_CHANNEL_OUT_STEREO;
61}
62
63static audio_format_t 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, audio_format_t 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_out_frame_size(stream) /
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
119static 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
129static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
130                                        int64_t *timestamp)
131{
132    return -EINVAL;
133}
134
135/** 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 audio_channel_mask_t in_get_channels(const struct audio_stream *stream)
152{
153    return AUDIO_CHANNEL_IN_MONO;
154}
155
156static audio_format_t in_get_format(const struct audio_stream *stream)
157{
158    return AUDIO_FORMAT_PCM_16_BIT;
159}
160
161static int in_set_format(struct audio_stream *stream, audio_format_t format)
162{
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_in_frame_size(stream) /
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
206static 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}
215
216static int adev_open_output_stream(struct audio_hw_device *dev,
217                                   audio_io_handle_t handle,
218                                   audio_devices_t devices,
219                                   audio_output_flags_t flags,
220                                   struct audio_config *config,
221                                   struct audio_stream_out **stream_out,
222                                   const char *address __unused)
223{
224    struct stub_audio_device *ladev = (struct stub_audio_device *)dev;
225    struct stub_stream_out *out;
226    int ret;
227
228    out = (struct stub_stream_out *)calloc(1, sizeof(struct stub_stream_out));
229    if (!out)
230        return -ENOMEM;
231
232    out->stream.common.get_sample_rate = out_get_sample_rate;
233    out->stream.common.set_sample_rate = out_set_sample_rate;
234    out->stream.common.get_buffer_size = out_get_buffer_size;
235    out->stream.common.get_channels = out_get_channels;
236    out->stream.common.get_format = out_get_format;
237    out->stream.common.set_format = out_set_format;
238    out->stream.common.standby = out_standby;
239    out->stream.common.dump = out_dump;
240    out->stream.common.set_parameters = out_set_parameters;
241    out->stream.common.get_parameters = out_get_parameters;
242    out->stream.common.add_audio_effect = out_add_audio_effect;
243    out->stream.common.remove_audio_effect = out_remove_audio_effect;
244    out->stream.get_latency = out_get_latency;
245    out->stream.set_volume = out_set_volume;
246    out->stream.write = out_write;
247    out->stream.get_render_position = out_get_render_position;
248    out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
249
250    *stream_out = &out->stream;
251    return 0;
252
253err_open:
254    free(out);
255    *stream_out = NULL;
256    return ret;
257}
258
259static void adev_close_output_stream(struct audio_hw_device *dev,
260                                     struct audio_stream_out *stream)
261{
262    free(stream);
263}
264
265static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
266{
267    return -ENOSYS;
268}
269
270static char * adev_get_parameters(const struct audio_hw_device *dev,
271                                  const char *keys)
272{
273    return NULL;
274}
275
276static int adev_init_check(const struct audio_hw_device *dev)
277{
278    return 0;
279}
280
281static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
282{
283    return -ENOSYS;
284}
285
286static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
287{
288    return -ENOSYS;
289}
290
291static int adev_get_master_volume(struct audio_hw_device *dev, float *volume)
292{
293    return -ENOSYS;
294}
295
296static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
297{
298    return -ENOSYS;
299}
300
301static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
302{
303    return -ENOSYS;
304}
305
306static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
307{
308    return 0;
309}
310
311static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
312{
313    return -ENOSYS;
314}
315
316static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
317{
318    return -ENOSYS;
319}
320
321static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
322                                         const struct audio_config *config)
323{
324    return 320;
325}
326
327static int adev_open_input_stream(struct audio_hw_device *dev,
328                                  audio_io_handle_t handle,
329                                  audio_devices_t devices,
330                                  struct audio_config *config,
331                                  struct audio_stream_in **stream_in,
332                                  audio_input_flags_t flags __unused,
333                                  const char *address __unused,
334                                  audio_source_t source __unused)
335{
336    struct stub_audio_device *ladev = (struct stub_audio_device *)dev;
337    struct stub_stream_in *in;
338    int ret;
339
340    in = (struct stub_stream_in *)calloc(1, sizeof(struct stub_stream_in));
341    if (!in)
342        return -ENOMEM;
343
344    in->stream.common.get_sample_rate = in_get_sample_rate;
345    in->stream.common.set_sample_rate = in_set_sample_rate;
346    in->stream.common.get_buffer_size = in_get_buffer_size;
347    in->stream.common.get_channels = in_get_channels;
348    in->stream.common.get_format = in_get_format;
349    in->stream.common.set_format = in_set_format;
350    in->stream.common.standby = in_standby;
351    in->stream.common.dump = in_dump;
352    in->stream.common.set_parameters = in_set_parameters;
353    in->stream.common.get_parameters = in_get_parameters;
354    in->stream.common.add_audio_effect = in_add_audio_effect;
355    in->stream.common.remove_audio_effect = in_remove_audio_effect;
356    in->stream.set_gain = in_set_gain;
357    in->stream.read = in_read;
358    in->stream.get_input_frames_lost = in_get_input_frames_lost;
359
360    *stream_in = &in->stream;
361    return 0;
362
363err_open:
364    free(in);
365    *stream_in = NULL;
366    return ret;
367}
368
369static void adev_close_input_stream(struct audio_hw_device *dev,
370                                   struct audio_stream_in *in)
371{
372    return;
373}
374
375static int adev_dump(const audio_hw_device_t *device, int fd)
376{
377    return 0;
378}
379
380static int adev_close(hw_device_t *device)
381{
382    free(device);
383    return 0;
384}
385
386static int adev_open(const hw_module_t* module, const char* name,
387                     hw_device_t** device)
388{
389    struct stub_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 stub_audio_device));
396    if (!adev)
397        return -ENOMEM;
398
399    adev->device.common.tag = HARDWARE_DEVICE_TAG;
400    adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
401    adev->device.common.module = (struct hw_module_t *) module;
402    adev->device.common.close = adev_close;
403
404    adev->device.init_check = adev_init_check;
405    adev->device.set_voice_volume = adev_set_voice_volume;
406    adev->device.set_master_volume = adev_set_master_volume;
407    adev->device.get_master_volume = adev_get_master_volume;
408    adev->device.set_master_mute = adev_set_master_mute;
409    adev->device.get_master_mute = adev_get_master_mute;
410    adev->device.set_mode = adev_set_mode;
411    adev->device.set_mic_mute = adev_set_mic_mute;
412    adev->device.get_mic_mute = adev_get_mic_mute;
413    adev->device.set_parameters = adev_set_parameters;
414    adev->device.get_parameters = adev_get_parameters;
415    adev->device.get_input_buffer_size = adev_get_input_buffer_size;
416    adev->device.open_output_stream = adev_open_output_stream;
417    adev->device.close_output_stream = adev_close_output_stream;
418    adev->device.open_input_stream = adev_open_input_stream;
419    adev->device.close_input_stream = adev_close_input_stream;
420    adev->device.dump = adev_dump;
421
422    *device = &adev->device.common;
423
424    return 0;
425}
426
427static struct hw_module_methods_t hal_module_methods = {
428    .open = adev_open,
429};
430
431struct audio_module HAL_MODULE_INFO_SYM = {
432    .common = {
433        .tag = HARDWARE_MODULE_TAG,
434        .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
435        .hal_api_version = HARDWARE_HAL_API_VERSION,
436        .id = AUDIO_HARDWARE_MODULE_ID,
437        .name = "Default audio HW HAL",
438        .author = "The Android Open Source Project",
439        .methods = &hal_module_methods,
440    },
441};
442