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 <malloc.h>
22 #include <pthread.h>
23 #include <stdint.h>
24 #include <sys/time.h>
25
26 #include <cutils/log.h>
27
28 #include <hardware/hardware.h>
29 #include <system/audio.h>
30 #include <hardware/audio.h>
31
32 struct stub_audio_device {
33 struct audio_hw_device device;
34 };
35
36 struct stub_stream_out {
37 struct audio_stream_out stream;
38 int64_t last_write_time_us;
39 };
40
41 struct stub_stream_in {
42 struct audio_stream_in stream;
43 int64_t last_read_time_us;
44 };
45
out_get_sample_rate(const struct audio_stream * stream)46 static uint32_t out_get_sample_rate(const struct audio_stream *stream)
47 {
48 return 44100;
49 }
50
out_set_sample_rate(struct audio_stream * stream,uint32_t rate)51 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
52 {
53 ALOGV("out_set_sample_rate: %d", 0);
54 return -ENOSYS;
55 }
56
out_get_buffer_size(const struct audio_stream * stream)57 static size_t out_get_buffer_size(const struct audio_stream *stream)
58 {
59 ALOGV("out_get_buffer_size: %d", 4096);
60 return 4096;
61 }
62
out_get_channels(const struct audio_stream * stream)63 static audio_channel_mask_t out_get_channels(const struct audio_stream *stream)
64 {
65 ALOGV("out_get_channels");
66 return AUDIO_CHANNEL_OUT_STEREO;
67 }
68
out_get_format(const struct audio_stream * stream)69 static audio_format_t out_get_format(const struct audio_stream *stream)
70 {
71 ALOGV("out_get_format");
72 return AUDIO_FORMAT_PCM_16_BIT;
73 }
74
out_set_format(struct audio_stream * stream,audio_format_t format)75 static int out_set_format(struct audio_stream *stream, audio_format_t format)
76 {
77 ALOGV("out_set_format: %d",format);
78 return -ENOSYS;
79 }
80
out_standby(struct audio_stream * stream)81 static int out_standby(struct audio_stream *stream)
82 {
83 ALOGV("out_standby");
84 // out->last_write_time_us = 0; unnecessary as a stale write time has same effect
85 return 0;
86 }
87
out_dump(const struct audio_stream * stream,int fd)88 static int out_dump(const struct audio_stream *stream, int fd)
89 {
90 ALOGV("out_dump");
91 return 0;
92 }
93
out_set_parameters(struct audio_stream * stream,const char * kvpairs)94 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
95 {
96 ALOGV("out_set_parameters");
97 return 0;
98 }
99
out_get_parameters(const struct audio_stream * stream,const char * keys)100 static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
101 {
102 ALOGV("out_get_parameters");
103 return strdup("");
104 }
105
out_get_latency(const struct audio_stream_out * stream)106 static uint32_t out_get_latency(const struct audio_stream_out *stream)
107 {
108 ALOGV("out_get_latency");
109 return 0;
110 }
111
out_set_volume(struct audio_stream_out * stream,float left,float right)112 static int out_set_volume(struct audio_stream_out *stream, float left,
113 float right)
114 {
115 ALOGV("out_set_volume: Left:%f Right:%f", left, right);
116 return 0;
117 }
118
out_write(struct audio_stream_out * stream,const void * buffer,size_t bytes)119 static ssize_t out_write(struct audio_stream_out *stream, const void* buffer,
120 size_t bytes)
121 {
122 ALOGV("out_write: bytes: %d", bytes);
123
124 /* XXX: fake timing for audio output */
125 struct stub_stream_out *out = (struct stub_stream_out *)stream;
126 struct timespec t = { .tv_sec = 0, .tv_nsec = 0 };
127 clock_gettime(CLOCK_MONOTONIC, &t);
128 const int64_t now = (t.tv_sec * 1000000000LL + t.tv_nsec) / 1000;
129 const int64_t elapsed_time_since_last_write = now - out->last_write_time_us;
130 int64_t sleep_time = bytes * 1000000LL / audio_stream_out_frame_size(stream) /
131 out_get_sample_rate(&stream->common) - elapsed_time_since_last_write;
132 if (sleep_time > 0) {
133 usleep(sleep_time);
134 } else {
135 // we don't sleep when we exit standby (this is typical for a real alsa buffer).
136 sleep_time = 0;
137 }
138 out->last_write_time_us = now + sleep_time;
139 // last_write_time_us is an approximation of when the (simulated) alsa
140 // buffer is believed completely full. The usleep above waits for more space
141 // in the buffer, but by the end of the sleep the buffer is considered
142 // topped-off.
143 //
144 // On the subsequent out_write(), we measure the elapsed time spent in
145 // the mixer. This is subtracted from the sleep estimate based on frames,
146 // thereby accounting for drain in the alsa buffer during mixing.
147 // This is a crude approximation; we don't handle underruns precisely.
148 return bytes;
149 }
150
out_get_render_position(const struct audio_stream_out * stream,uint32_t * dsp_frames)151 static int out_get_render_position(const struct audio_stream_out *stream,
152 uint32_t *dsp_frames)
153 {
154 *dsp_frames = 0;
155 ALOGV("out_get_render_position: dsp_frames: %p", dsp_frames);
156 return -EINVAL;
157 }
158
out_add_audio_effect(const struct audio_stream * stream,effect_handle_t effect)159 static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
160 {
161 ALOGV("out_add_audio_effect: %p", effect);
162 return 0;
163 }
164
out_remove_audio_effect(const struct audio_stream * stream,effect_handle_t effect)165 static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
166 {
167 ALOGV("out_remove_audio_effect: %p", effect);
168 return 0;
169 }
170
out_get_next_write_timestamp(const struct audio_stream_out * stream,int64_t * timestamp)171 static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
172 int64_t *timestamp)
173 {
174 *timestamp = 0;
175 ALOGV("out_get_next_write_timestamp: %ld", (long int)(*timestamp));
176 return -EINVAL;
177 }
178
179 /** audio_stream_in implementation **/
in_get_sample_rate(const struct audio_stream * stream)180 static uint32_t in_get_sample_rate(const struct audio_stream *stream)
181 {
182 ALOGV("in_get_sample_rate");
183 return 8000;
184 }
185
in_set_sample_rate(struct audio_stream * stream,uint32_t rate)186 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
187 {
188 ALOGV("in_set_sample_rate: %d", rate);
189 return -ENOSYS;
190 }
191
in_get_buffer_size(const struct audio_stream * stream)192 static size_t in_get_buffer_size(const struct audio_stream *stream)
193 {
194 ALOGV("in_get_buffer_size: %d", 320);
195 return 320;
196 }
197
in_get_channels(const struct audio_stream * stream)198 static audio_channel_mask_t in_get_channels(const struct audio_stream *stream)
199 {
200 ALOGV("in_get_channels: %d", AUDIO_CHANNEL_IN_MONO);
201 return AUDIO_CHANNEL_IN_MONO;
202 }
203
in_get_format(const struct audio_stream * stream)204 static audio_format_t in_get_format(const struct audio_stream *stream)
205 {
206 return AUDIO_FORMAT_PCM_16_BIT;
207 }
208
in_set_format(struct audio_stream * stream,audio_format_t format)209 static int in_set_format(struct audio_stream *stream, audio_format_t format)
210 {
211 return -ENOSYS;
212 }
213
in_standby(struct audio_stream * stream)214 static int in_standby(struct audio_stream *stream)
215 {
216 struct stub_stream_in *in = (struct stub_stream_in *)stream;
217 in->last_read_time_us = 0;
218 return 0;
219 }
220
in_dump(const struct audio_stream * stream,int fd)221 static int in_dump(const struct audio_stream *stream, int fd)
222 {
223 return 0;
224 }
225
in_set_parameters(struct audio_stream * stream,const char * kvpairs)226 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
227 {
228 return 0;
229 }
230
in_get_parameters(const struct audio_stream * stream,const char * keys)231 static char * in_get_parameters(const struct audio_stream *stream,
232 const char *keys)
233 {
234 return strdup("");
235 }
236
in_set_gain(struct audio_stream_in * stream,float gain)237 static int in_set_gain(struct audio_stream_in *stream, float gain)
238 {
239 return 0;
240 }
241
in_read(struct audio_stream_in * stream,void * buffer,size_t bytes)242 static ssize_t in_read(struct audio_stream_in *stream, void* buffer,
243 size_t bytes)
244 {
245 ALOGV("in_read: bytes %d", bytes);
246
247 /* XXX: fake timing for audio input */
248 struct stub_stream_in *in = (struct stub_stream_in *)stream;
249 struct timespec t = { .tv_sec = 0, .tv_nsec = 0 };
250 clock_gettime(CLOCK_MONOTONIC, &t);
251 const int64_t now = (t.tv_sec * 1000000000LL + t.tv_nsec) / 1000;
252
253 // we do a full sleep when exiting standby.
254 const bool standby = in->last_read_time_us == 0;
255 const int64_t elapsed_time_since_last_read = standby ?
256 0 : now - in->last_read_time_us;
257 int64_t sleep_time = bytes * 1000000LL / audio_stream_in_frame_size(stream) /
258 in_get_sample_rate(&stream->common) - elapsed_time_since_last_read;
259 if (sleep_time > 0) {
260 usleep(sleep_time);
261 } else {
262 sleep_time = 0;
263 }
264 in->last_read_time_us = now + sleep_time;
265 // last_read_time_us is an approximation of when the (simulated) alsa
266 // buffer is drained by the read, and is empty.
267 //
268 // On the subsequent in_read(), we measure the elapsed time spent in
269 // the recording thread. This is subtracted from the sleep estimate based on frames,
270 // thereby accounting for fill in the alsa buffer during the interim.
271 memset(buffer, 0, bytes);
272 return bytes;
273 }
274
in_get_input_frames_lost(struct audio_stream_in * stream)275 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
276 {
277 return 0;
278 }
279
in_add_audio_effect(const struct audio_stream * stream,effect_handle_t effect)280 static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
281 {
282 return 0;
283 }
284
in_remove_audio_effect(const struct audio_stream * stream,effect_handle_t effect)285 static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
286 {
287 return 0;
288 }
289
adev_open_output_stream(struct audio_hw_device * dev,audio_io_handle_t handle,audio_devices_t devices,audio_output_flags_t flags,struct audio_config * config,struct audio_stream_out ** stream_out,const char * address __unused)290 static int adev_open_output_stream(struct audio_hw_device *dev,
291 audio_io_handle_t handle,
292 audio_devices_t devices,
293 audio_output_flags_t flags,
294 struct audio_config *config,
295 struct audio_stream_out **stream_out,
296 const char *address __unused)
297 {
298 ALOGV("adev_open_output_stream...");
299
300 struct stub_audio_device *ladev = (struct stub_audio_device *)dev;
301 struct stub_stream_out *out;
302 int ret;
303
304 out = (struct stub_stream_out *)calloc(1, sizeof(struct stub_stream_out));
305 if (!out)
306 return -ENOMEM;
307
308 out->stream.common.get_sample_rate = out_get_sample_rate;
309 out->stream.common.set_sample_rate = out_set_sample_rate;
310 out->stream.common.get_buffer_size = out_get_buffer_size;
311 out->stream.common.get_channels = out_get_channels;
312 out->stream.common.get_format = out_get_format;
313 out->stream.common.set_format = out_set_format;
314 out->stream.common.standby = out_standby;
315 out->stream.common.dump = out_dump;
316 out->stream.common.set_parameters = out_set_parameters;
317 out->stream.common.get_parameters = out_get_parameters;
318 out->stream.common.add_audio_effect = out_add_audio_effect;
319 out->stream.common.remove_audio_effect = out_remove_audio_effect;
320 out->stream.get_latency = out_get_latency;
321 out->stream.set_volume = out_set_volume;
322 out->stream.write = out_write;
323 out->stream.get_render_position = out_get_render_position;
324 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
325
326 *stream_out = &out->stream;
327 return 0;
328
329 err_open:
330 free(out);
331 *stream_out = NULL;
332 return ret;
333 }
334
adev_close_output_stream(struct audio_hw_device * dev,struct audio_stream_out * stream)335 static void adev_close_output_stream(struct audio_hw_device *dev,
336 struct audio_stream_out *stream)
337 {
338 ALOGV("adev_close_output_stream...");
339 free(stream);
340 }
341
adev_set_parameters(struct audio_hw_device * dev,const char * kvpairs)342 static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
343 {
344 ALOGV("adev_set_parameters");
345 return -ENOSYS;
346 }
347
adev_get_parameters(const struct audio_hw_device * dev,const char * keys)348 static char * adev_get_parameters(const struct audio_hw_device *dev,
349 const char *keys)
350 {
351 ALOGV("adev_get_parameters");
352 return strdup("");
353 }
354
adev_init_check(const struct audio_hw_device * dev)355 static int adev_init_check(const struct audio_hw_device *dev)
356 {
357 ALOGV("adev_init_check");
358 return 0;
359 }
360
adev_set_voice_volume(struct audio_hw_device * dev,float volume)361 static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
362 {
363 ALOGV("adev_set_voice_volume: %f", volume);
364 return -ENOSYS;
365 }
366
adev_set_master_volume(struct audio_hw_device * dev,float volume)367 static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
368 {
369 ALOGV("adev_set_master_volume: %f", volume);
370 return -ENOSYS;
371 }
372
adev_get_master_volume(struct audio_hw_device * dev,float * volume)373 static int adev_get_master_volume(struct audio_hw_device *dev, float *volume)
374 {
375 ALOGV("adev_get_master_volume: %f", *volume);
376 return -ENOSYS;
377 }
378
adev_set_master_mute(struct audio_hw_device * dev,bool muted)379 static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
380 {
381 ALOGV("adev_set_master_mute: %d", muted);
382 return -ENOSYS;
383 }
384
adev_get_master_mute(struct audio_hw_device * dev,bool * muted)385 static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
386 {
387 ALOGV("adev_get_master_mute: %d", *muted);
388 return -ENOSYS;
389 }
390
adev_set_mode(struct audio_hw_device * dev,audio_mode_t mode)391 static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
392 {
393 ALOGV("adev_set_mode: %d", mode);
394 return 0;
395 }
396
adev_set_mic_mute(struct audio_hw_device * dev,bool state)397 static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
398 {
399 ALOGV("adev_set_mic_mute: %d",state);
400 return -ENOSYS;
401 }
402
adev_get_mic_mute(const struct audio_hw_device * dev,bool * state)403 static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
404 {
405 ALOGV("adev_get_mic_mute");
406 return -ENOSYS;
407 }
408
adev_get_input_buffer_size(const struct audio_hw_device * dev,const struct audio_config * config)409 static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
410 const struct audio_config *config)
411 {
412 ALOGV("adev_get_input_buffer_size: %d", 320);
413 return 320;
414 }
415
adev_open_input_stream(struct audio_hw_device * dev,audio_io_handle_t handle,audio_devices_t devices,struct audio_config * config,struct audio_stream_in ** stream_in,audio_input_flags_t flags __unused,const char * address __unused,audio_source_t source __unused)416 static int adev_open_input_stream(struct audio_hw_device *dev,
417 audio_io_handle_t handle,
418 audio_devices_t devices,
419 struct audio_config *config,
420 struct audio_stream_in **stream_in,
421 audio_input_flags_t flags __unused,
422 const char *address __unused,
423 audio_source_t source __unused)
424 {
425 ALOGV("adev_open_input_stream...");
426
427 struct stub_audio_device *ladev = (struct stub_audio_device *)dev;
428 struct stub_stream_in *in;
429 int ret;
430
431 in = (struct stub_stream_in *)calloc(1, sizeof(struct stub_stream_in));
432 if (!in)
433 return -ENOMEM;
434
435 in->stream.common.get_sample_rate = in_get_sample_rate;
436 in->stream.common.set_sample_rate = in_set_sample_rate;
437 in->stream.common.get_buffer_size = in_get_buffer_size;
438 in->stream.common.get_channels = in_get_channels;
439 in->stream.common.get_format = in_get_format;
440 in->stream.common.set_format = in_set_format;
441 in->stream.common.standby = in_standby;
442 in->stream.common.dump = in_dump;
443 in->stream.common.set_parameters = in_set_parameters;
444 in->stream.common.get_parameters = in_get_parameters;
445 in->stream.common.add_audio_effect = in_add_audio_effect;
446 in->stream.common.remove_audio_effect = in_remove_audio_effect;
447 in->stream.set_gain = in_set_gain;
448 in->stream.read = in_read;
449 in->stream.get_input_frames_lost = in_get_input_frames_lost;
450
451 *stream_in = &in->stream;
452 return 0;
453
454 err_open:
455 free(in);
456 *stream_in = NULL;
457 return ret;
458 }
459
adev_close_input_stream(struct audio_hw_device * dev,struct audio_stream_in * in)460 static void adev_close_input_stream(struct audio_hw_device *dev,
461 struct audio_stream_in *in)
462 {
463 ALOGV("adev_close_input_stream...");
464 return;
465 }
466
adev_dump(const audio_hw_device_t * device,int fd)467 static int adev_dump(const audio_hw_device_t *device, int fd)
468 {
469 ALOGV("adev_dump");
470 return 0;
471 }
472
adev_close(hw_device_t * device)473 static int adev_close(hw_device_t *device)
474 {
475 ALOGV("adev_close");
476 free(device);
477 return 0;
478 }
479
adev_open(const hw_module_t * module,const char * name,hw_device_t ** device)480 static int adev_open(const hw_module_t* module, const char* name,
481 hw_device_t** device)
482 {
483 ALOGV("adev_open: %s", name);
484
485 struct stub_audio_device *adev;
486 int ret;
487
488 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
489 return -EINVAL;
490
491 adev = calloc(1, sizeof(struct stub_audio_device));
492 if (!adev)
493 return -ENOMEM;
494
495 adev->device.common.tag = HARDWARE_DEVICE_TAG;
496 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
497 adev->device.common.module = (struct hw_module_t *) module;
498 adev->device.common.close = adev_close;
499
500 adev->device.init_check = adev_init_check;
501 adev->device.set_voice_volume = adev_set_voice_volume;
502 adev->device.set_master_volume = adev_set_master_volume;
503 adev->device.get_master_volume = adev_get_master_volume;
504 adev->device.set_master_mute = adev_set_master_mute;
505 adev->device.get_master_mute = adev_get_master_mute;
506 adev->device.set_mode = adev_set_mode;
507 adev->device.set_mic_mute = adev_set_mic_mute;
508 adev->device.get_mic_mute = adev_get_mic_mute;
509 adev->device.set_parameters = adev_set_parameters;
510 adev->device.get_parameters = adev_get_parameters;
511 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
512 adev->device.open_output_stream = adev_open_output_stream;
513 adev->device.close_output_stream = adev_close_output_stream;
514 adev->device.open_input_stream = adev_open_input_stream;
515 adev->device.close_input_stream = adev_close_input_stream;
516 adev->device.dump = adev_dump;
517
518 *device = &adev->device.common;
519
520 return 0;
521 }
522
523 static struct hw_module_methods_t hal_module_methods = {
524 .open = adev_open,
525 };
526
527 struct audio_module HAL_MODULE_INFO_SYM = {
528 .common = {
529 .tag = HARDWARE_MODULE_TAG,
530 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
531 .hal_api_version = HARDWARE_HAL_API_VERSION,
532 .id = AUDIO_HARDWARE_MODULE_ID,
533 .name = "Default audio HW HAL",
534 .author = "The Android Open Source Project",
535 .methods = &hal_module_methods,
536 },
537 };
538