Dima Zavin | 8cc353a | 2011-04-20 16:38:05 -0700 | [diff] [blame] | 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 <hardware/audio.h> |
| 29 | #include <hardware/audio_hal.h> |
| 30 | |
| 31 | struct stub_audio_device { |
| 32 | struct audio_hw_device device; |
| 33 | }; |
| 34 | |
| 35 | struct stub_stream_out { |
| 36 | struct audio_stream_out stream; |
| 37 | }; |
| 38 | |
| 39 | struct stub_stream_in { |
| 40 | struct audio_stream_in stream; |
| 41 | }; |
| 42 | |
| 43 | static uint32_t out_get_sample_rate(const struct audio_stream *stream) |
| 44 | { |
| 45 | return 44100; |
| 46 | } |
| 47 | |
| 48 | static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate) |
| 49 | { |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | static size_t out_get_buffer_size(const struct audio_stream *stream) |
| 54 | { |
| 55 | return 4096; |
| 56 | } |
| 57 | |
| 58 | static uint32_t out_get_channels(const struct audio_stream *stream) |
| 59 | { |
| 60 | return AUDIO_CHANNEL_OUT_STEREO; |
| 61 | } |
| 62 | |
| 63 | static int out_get_format(const struct audio_stream *stream) |
| 64 | { |
| 65 | return AUDIO_FORMAT_PCM_16_BIT; |
| 66 | } |
| 67 | |
| 68 | static int out_set_format(struct audio_stream *stream, int format) |
| 69 | { |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | static int out_standby(struct audio_stream *stream) |
| 74 | { |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | static int out_dump(const struct audio_stream *stream, int fd) |
| 79 | { |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | static int out_set_parameters(struct audio_stream *stream, const char *kvpairs) |
| 84 | { |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | static char * out_get_parameters(const struct audio_stream *stream, const char *keys) |
| 89 | { |
| 90 | return strdup(""); |
| 91 | } |
| 92 | |
| 93 | static uint32_t out_get_latency(const struct audio_stream_out *stream) |
| 94 | { |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | static int out_set_volume(struct audio_stream_out *stream, float left, |
| 99 | float right) |
| 100 | { |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | static 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 | |
| 113 | static int out_get_render_position(const struct audio_stream_out *stream, |
| 114 | uint32_t *dsp_frames) |
| 115 | { |
| 116 | return -EINVAL; |
| 117 | } |
| 118 | |
| 119 | /** audio_stream_in implementation **/ |
| 120 | static uint32_t in_get_sample_rate(const struct audio_stream *stream) |
| 121 | { |
| 122 | return 8000; |
| 123 | } |
| 124 | |
| 125 | static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate) |
| 126 | { |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | static size_t in_get_buffer_size(const struct audio_stream *stream) |
| 131 | { |
| 132 | return 320; |
| 133 | } |
| 134 | |
| 135 | static uint32_t in_get_channels(const struct audio_stream *stream) |
| 136 | { |
| 137 | return AUDIO_CHANNEL_IN_MONO; |
| 138 | } |
| 139 | |
| 140 | static int in_get_format(const struct audio_stream *stream) |
| 141 | { |
| 142 | return AUDIO_FORMAT_PCM_16_BIT; |
| 143 | } |
| 144 | |
| 145 | static int in_set_format(struct audio_stream *stream, int format) |
| 146 | { |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | static int in_standby(struct audio_stream *stream) |
| 151 | { |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | static int in_dump(const struct audio_stream *stream, int fd) |
| 156 | { |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | static int in_set_parameters(struct audio_stream *stream, const char *kvpairs) |
| 161 | { |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | static char * in_get_parameters(const struct audio_stream *stream, |
| 166 | const char *keys) |
| 167 | { |
| 168 | return strdup(""); |
| 169 | } |
| 170 | |
| 171 | static int in_set_gain(struct audio_stream_in *stream, float gain) |
| 172 | { |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | static ssize_t in_read(struct audio_stream_in *stream, void* buffer, |
| 177 | size_t bytes) |
| 178 | { |
| 179 | /* XXX: fake timing for audio input */ |
| 180 | usleep(bytes * 1000000 / audio_stream_frame_size(&stream->common) / |
| 181 | in_get_sample_rate(&stream->common)); |
| 182 | return bytes; |
| 183 | } |
| 184 | |
| 185 | static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream) |
| 186 | { |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | |
| 191 | static int adev_open_output_stream(struct audio_hw_device *dev, |
| 192 | uint32_t devices, int *format, |
| 193 | uint32_t *channels, uint32_t *sample_rate, |
| 194 | struct audio_stream_out **stream_out) |
| 195 | { |
| 196 | struct stub_audio_device *ladev = (struct stub_audio_device *)dev; |
| 197 | struct stub_stream_out *out; |
| 198 | int ret; |
| 199 | |
| 200 | out = (struct stub_stream_out *)calloc(1, sizeof(struct stub_stream_out)); |
| 201 | if (!out) |
| 202 | return -ENOMEM; |
| 203 | |
| 204 | out->stream.common.get_sample_rate = out_get_sample_rate; |
| 205 | out->stream.common.set_sample_rate = out_set_sample_rate; |
| 206 | out->stream.common.get_buffer_size = out_get_buffer_size; |
| 207 | out->stream.common.get_channels = out_get_channels; |
| 208 | out->stream.common.get_format = out_get_format; |
| 209 | out->stream.common.set_format = out_set_format; |
| 210 | out->stream.common.standby = out_standby; |
| 211 | out->stream.common.dump = out_dump; |
| 212 | out->stream.common.set_parameters = out_set_parameters; |
| 213 | out->stream.common.get_parameters = out_get_parameters; |
| 214 | out->stream.get_latency = out_get_latency; |
| 215 | out->stream.set_volume = out_set_volume; |
| 216 | out->stream.write = out_write; |
| 217 | out->stream.get_render_position = out_get_render_position; |
| 218 | |
| 219 | *stream_out = &out->stream; |
| 220 | return 0; |
| 221 | |
| 222 | err_open: |
| 223 | free(out); |
| 224 | *stream_out = NULL; |
| 225 | return ret; |
| 226 | } |
| 227 | |
| 228 | static void adev_close_output_stream(struct audio_hw_device *dev, |
| 229 | struct audio_stream_out *stream) |
| 230 | { |
| 231 | free(stream); |
| 232 | } |
| 233 | |
| 234 | static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs) |
| 235 | { |
| 236 | return -ENOSYS; |
| 237 | } |
| 238 | |
| 239 | static char * adev_get_parameters(const struct audio_hw_device *dev, |
| 240 | const char *keys) |
| 241 | { |
| 242 | return NULL; |
| 243 | } |
| 244 | |
| 245 | static int adev_init_check(const struct audio_hw_device *dev) |
| 246 | { |
| 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | static int adev_set_voice_volume(struct audio_hw_device *dev, float volume) |
| 251 | { |
| 252 | return -ENOSYS; |
| 253 | } |
| 254 | |
| 255 | static int adev_set_master_volume(struct audio_hw_device *dev, float volume) |
| 256 | { |
| 257 | return -ENOSYS; |
| 258 | } |
| 259 | |
| 260 | static int adev_set_mode(struct audio_hw_device *dev, int mode) |
| 261 | { |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | static int adev_set_mic_mute(struct audio_hw_device *dev, bool state) |
| 266 | { |
| 267 | return -ENOSYS; |
| 268 | } |
| 269 | |
| 270 | static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state) |
| 271 | { |
| 272 | return -ENOSYS; |
| 273 | } |
| 274 | |
| 275 | static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev, |
| 276 | uint32_t sample_rate, int format, |
| 277 | int channel_count) |
| 278 | { |
| 279 | return 320; |
| 280 | } |
| 281 | |
| 282 | static int adev_open_input_stream(struct audio_hw_device *dev, uint32_t devices, |
| 283 | int *format, uint32_t *channels, |
| 284 | uint32_t *sample_rate, |
| 285 | audio_in_acoustics_t acoustics, |
| 286 | struct audio_stream_in **stream_in) |
| 287 | { |
| 288 | struct stub_audio_device *ladev = (struct stub_audio_device *)dev; |
| 289 | struct stub_stream_in *in; |
| 290 | int ret; |
| 291 | |
| 292 | in = (struct stub_stream_in *)calloc(1, sizeof(struct stub_stream_in)); |
| 293 | if (!in) |
| 294 | return -ENOMEM; |
| 295 | |
| 296 | in->stream.common.get_sample_rate = in_get_sample_rate; |
| 297 | in->stream.common.set_sample_rate = in_set_sample_rate; |
| 298 | in->stream.common.get_buffer_size = in_get_buffer_size; |
| 299 | in->stream.common.get_channels = in_get_channels; |
| 300 | in->stream.common.get_format = in_get_format; |
| 301 | in->stream.common.set_format = in_set_format; |
| 302 | in->stream.common.standby = in_standby; |
| 303 | in->stream.common.dump = in_dump; |
| 304 | in->stream.common.set_parameters = in_set_parameters; |
| 305 | in->stream.common.get_parameters = in_get_parameters; |
| 306 | in->stream.set_gain = in_set_gain; |
| 307 | in->stream.read = in_read; |
| 308 | in->stream.get_input_frames_lost = in_get_input_frames_lost; |
| 309 | |
| 310 | *stream_in = &in->stream; |
| 311 | return 0; |
| 312 | |
| 313 | err_open: |
| 314 | free(in); |
| 315 | *stream_in = NULL; |
| 316 | return ret; |
| 317 | } |
| 318 | |
| 319 | static void adev_close_input_stream(struct audio_hw_device *dev, |
| 320 | struct audio_stream_in *in) |
| 321 | { |
| 322 | return; |
| 323 | } |
| 324 | |
| 325 | static int adev_dump(const audio_hw_device_t *device, int fd) |
| 326 | { |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | static int adev_close(hw_device_t *device) |
| 331 | { |
| 332 | free(device); |
| 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | static uint32_t adev_get_supported_devices(const struct audio_hw_device *dev) |
| 337 | { |
| 338 | return (/* OUT */ |
| 339 | AUDIO_DEVICE_OUT_EARPIECE | |
| 340 | AUDIO_DEVICE_OUT_SPEAKER | |
| 341 | AUDIO_DEVICE_OUT_WIRED_HEADSET | |
| 342 | AUDIO_DEVICE_OUT_WIRED_HEADPHONE | |
| 343 | AUDIO_DEVICE_OUT_AUX_DIGITAL | |
| 344 | AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET | |
| 345 | AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET | |
| 346 | AUDIO_DEVICE_OUT_ALL_SCO | |
| 347 | AUDIO_DEVICE_OUT_DEFAULT | |
| 348 | /* IN */ |
| 349 | AUDIO_DEVICE_IN_COMMUNICATION | |
| 350 | AUDIO_DEVICE_IN_AMBIENT | |
| 351 | AUDIO_DEVICE_IN_BUILTIN_MIC | |
| 352 | AUDIO_DEVICE_IN_WIRED_HEADSET | |
| 353 | AUDIO_DEVICE_IN_AUX_DIGITAL | |
| 354 | AUDIO_DEVICE_IN_BACK_MIC | |
| 355 | AUDIO_DEVICE_IN_ALL_SCO | |
| 356 | AUDIO_DEVICE_IN_DEFAULT); |
| 357 | } |
| 358 | |
| 359 | static int adev_open(const hw_module_t* module, const char* name, |
| 360 | hw_device_t** device) |
| 361 | { |
| 362 | struct stub_audio_device *adev; |
| 363 | int ret; |
| 364 | |
| 365 | if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) |
| 366 | return -EINVAL; |
| 367 | |
| 368 | adev = calloc(1, sizeof(struct stub_audio_device)); |
| 369 | if (!adev) |
| 370 | return -ENOMEM; |
| 371 | |
| 372 | adev->device.common.tag = HARDWARE_DEVICE_TAG; |
| 373 | adev->device.common.version = 0; |
| 374 | adev->device.common.module = (struct hw_module_t *) module; |
| 375 | adev->device.common.close = adev_close; |
| 376 | |
| 377 | adev->device.get_supported_devices = adev_get_supported_devices; |
| 378 | adev->device.init_check = adev_init_check; |
| 379 | adev->device.set_voice_volume = adev_set_voice_volume; |
| 380 | adev->device.set_master_volume = adev_set_master_volume; |
| 381 | adev->device.set_mode = adev_set_mode; |
| 382 | adev->device.set_mic_mute = adev_set_mic_mute; |
| 383 | adev->device.get_mic_mute = adev_get_mic_mute; |
| 384 | adev->device.set_parameters = adev_set_parameters; |
| 385 | adev->device.get_parameters = adev_get_parameters; |
| 386 | adev->device.get_input_buffer_size = adev_get_input_buffer_size; |
| 387 | adev->device.open_output_stream = adev_open_output_stream; |
| 388 | adev->device.close_output_stream = adev_close_output_stream; |
| 389 | adev->device.open_input_stream = adev_open_input_stream; |
| 390 | adev->device.close_input_stream = adev_close_input_stream; |
| 391 | adev->device.dump = adev_dump; |
| 392 | |
| 393 | *device = &adev->device.common; |
| 394 | |
| 395 | return 0; |
| 396 | } |
| 397 | |
| 398 | static struct hw_module_methods_t hal_module_methods = { |
| 399 | .open = adev_open, |
| 400 | }; |
| 401 | |
| 402 | struct audio_module HAL_MODULE_INFO_SYM = { |
| 403 | .common = { |
| 404 | .tag = HARDWARE_MODULE_TAG, |
| 405 | .version_major = 1, |
| 406 | .version_minor = 0, |
| 407 | .id = AUDIO_HARDWARE_MODULE_ID, |
| 408 | .name = "Default audio HW HAL", |
| 409 | .author = "The Android Open Source Project", |
| 410 | .methods = &hal_module_methods, |
| 411 | }, |
| 412 | }; |