2 * Copyright (C) 2011 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define LOG_TAG "legacy_audio_hw_hal"
18 //#define LOG_NDEBUG 0
22 #include <hardware/hardware.h>
23 #include <system/audio.h>
24 #include <hardware/audio.h>
26 #include <hardware_legacy/AudioHardwareInterface.h>
27 #include <hardware_legacy/AudioSystemLegacy.h>
29 namespace android_audio_legacy {
33 struct legacy_audio_module {
34 struct audio_module module;
37 struct legacy_audio_device {
38 struct audio_hw_device device;
40 struct AudioHardwareInterface *hwif;
43 struct legacy_stream_out {
44 struct audio_stream_out stream;
46 AudioStreamOut *legacy_out;
49 struct legacy_stream_in {
50 struct audio_stream_in stream;
52 AudioStreamIn *legacy_in;
55 /** audio_stream_out implementation **/
56 static uint32_t out_get_sample_rate(const struct audio_stream *stream)
58 const struct legacy_stream_out *out =
59 reinterpret_cast<const struct legacy_stream_out *>(stream);
60 return out->legacy_out->sampleRate();
63 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
65 struct legacy_stream_out *out =
66 reinterpret_cast<struct legacy_stream_out *>(stream);
68 ALOGE("(%s:%d) %s: Implement me!", __FILE__, __LINE__, __func__);
69 /* TODO: implement this */
73 static size_t out_get_buffer_size(const struct audio_stream *stream)
75 const struct legacy_stream_out *out =
76 reinterpret_cast<const struct legacy_stream_out *>(stream);
77 return out->legacy_out->bufferSize();
80 static uint32_t out_get_channels(const struct audio_stream *stream)
82 const struct legacy_stream_out *out =
83 reinterpret_cast<const struct legacy_stream_out *>(stream);
84 return out->legacy_out->channels();
87 static audio_format_t out_get_format(const struct audio_stream *stream)
89 const struct legacy_stream_out *out =
90 reinterpret_cast<const struct legacy_stream_out *>(stream);
91 // legacy API, don't change return type
92 return (audio_format_t) out->legacy_out->format();
95 static int out_set_format(struct audio_stream *stream, audio_format_t format)
97 struct legacy_stream_out *out =
98 reinterpret_cast<struct legacy_stream_out *>(stream);
99 ALOGE("(%s:%d) %s: Implement me!", __FILE__, __LINE__, __func__);
100 /* TODO: implement me */
104 static int out_standby(struct audio_stream *stream)
106 struct legacy_stream_out *out =
107 reinterpret_cast<struct legacy_stream_out *>(stream);
108 return out->legacy_out->standby();
111 static int out_dump(const struct audio_stream *stream, int fd)
113 const struct legacy_stream_out *out =
114 reinterpret_cast<const struct legacy_stream_out *>(stream);
115 Vector<String16> args;
116 return out->legacy_out->dump(fd, args);
119 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
121 struct legacy_stream_out *out =
122 reinterpret_cast<struct legacy_stream_out *>(stream);
123 return out->legacy_out->setParameters(String8(kvpairs));
126 static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
128 const struct legacy_stream_out *out =
129 reinterpret_cast<const struct legacy_stream_out *>(stream);
131 s8 = out->legacy_out->getParameters(String8(keys));
132 return strdup(s8.string());
135 static uint32_t out_get_latency(const struct audio_stream_out *stream)
137 const struct legacy_stream_out *out =
138 reinterpret_cast<const struct legacy_stream_out *>(stream);
139 return out->legacy_out->latency();
142 static int out_set_volume(struct audio_stream_out *stream, float left,
145 struct legacy_stream_out *out =
146 reinterpret_cast<struct legacy_stream_out *>(stream);
147 return out->legacy_out->setVolume(left, right);
150 static ssize_t out_write(struct audio_stream_out *stream, const void* buffer,
153 struct legacy_stream_out *out =
154 reinterpret_cast<struct legacy_stream_out *>(stream);
155 return out->legacy_out->write(buffer, bytes);
158 static int out_get_render_position(const struct audio_stream_out *stream,
159 uint32_t *dsp_frames)
161 const struct legacy_stream_out *out =
162 reinterpret_cast<const struct legacy_stream_out *>(stream);
163 return out->legacy_out->getRenderPosition(dsp_frames);
166 static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
169 const struct legacy_stream_out *out =
170 reinterpret_cast<const struct legacy_stream_out *>(stream);
171 return out->legacy_out->getNextWriteTimestamp(timestamp);
174 static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
179 static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
184 /** audio_stream_in implementation **/
185 static uint32_t in_get_sample_rate(const struct audio_stream *stream)
187 const struct legacy_stream_in *in =
188 reinterpret_cast<const struct legacy_stream_in *>(stream);
189 return in->legacy_in->sampleRate();
192 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
194 struct legacy_stream_in *in =
195 reinterpret_cast<struct legacy_stream_in *>(stream);
197 ALOGE("(%s:%d) %s: Implement me!", __FILE__, __LINE__, __func__);
198 /* TODO: implement this */
202 static size_t in_get_buffer_size(const struct audio_stream *stream)
204 const struct legacy_stream_in *in =
205 reinterpret_cast<const struct legacy_stream_in *>(stream);
206 return in->legacy_in->bufferSize();
209 static uint32_t in_get_channels(const struct audio_stream *stream)
211 const struct legacy_stream_in *in =
212 reinterpret_cast<const struct legacy_stream_in *>(stream);
213 return in->legacy_in->channels();
216 static audio_format_t in_get_format(const struct audio_stream *stream)
218 const struct legacy_stream_in *in =
219 reinterpret_cast<const struct legacy_stream_in *>(stream);
220 // legacy API, don't change return type
221 return (audio_format_t) in->legacy_in->format();
224 static int in_set_format(struct audio_stream *stream, audio_format_t format)
226 struct legacy_stream_in *in =
227 reinterpret_cast<struct legacy_stream_in *>(stream);
228 ALOGE("(%s:%d) %s: Implement me!", __FILE__, __LINE__, __func__);
229 /* TODO: implement me */
233 static int in_standby(struct audio_stream *stream)
235 struct legacy_stream_in *in = reinterpret_cast<struct legacy_stream_in *>(stream);
236 return in->legacy_in->standby();
239 static int in_dump(const struct audio_stream *stream, int fd)
241 const struct legacy_stream_in *in =
242 reinterpret_cast<const struct legacy_stream_in *>(stream);
243 Vector<String16> args;
244 return in->legacy_in->dump(fd, args);
247 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
249 struct legacy_stream_in *in =
250 reinterpret_cast<struct legacy_stream_in *>(stream);
251 return in->legacy_in->setParameters(String8(kvpairs));
254 static char * in_get_parameters(const struct audio_stream *stream,
257 const struct legacy_stream_in *in =
258 reinterpret_cast<const struct legacy_stream_in *>(stream);
260 s8 = in->legacy_in->getParameters(String8(keys));
261 return strdup(s8.string());
264 static int in_set_gain(struct audio_stream_in *stream, float gain)
266 struct legacy_stream_in *in =
267 reinterpret_cast<struct legacy_stream_in *>(stream);
268 return in->legacy_in->setGain(gain);
271 static ssize_t in_read(struct audio_stream_in *stream, void* buffer,
274 struct legacy_stream_in *in =
275 reinterpret_cast<struct legacy_stream_in *>(stream);
276 return in->legacy_in->read(buffer, bytes);
279 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
281 struct legacy_stream_in *in =
282 reinterpret_cast<struct legacy_stream_in *>(stream);
283 return in->legacy_in->getInputFramesLost();
286 static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
288 const struct legacy_stream_in *in =
289 reinterpret_cast<const struct legacy_stream_in *>(stream);
290 return in->legacy_in->addAudioEffect(effect);
293 static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
295 const struct legacy_stream_in *in =
296 reinterpret_cast<const struct legacy_stream_in *>(stream);
297 return in->legacy_in->removeAudioEffect(effect);
300 /** audio_hw_device implementation **/
301 static inline struct legacy_audio_device * to_ladev(struct audio_hw_device *dev)
303 return reinterpret_cast<struct legacy_audio_device *>(dev);
306 static inline const struct legacy_audio_device * to_cladev(const struct audio_hw_device *dev)
308 return reinterpret_cast<const struct legacy_audio_device *>(dev);
311 static uint32_t adev_get_supported_devices(const struct audio_hw_device *dev)
313 /* XXX: The old AudioHardwareInterface interface is not smart enough to
314 * tell us this, so we'll lie and basically tell AF that we support the
315 * below input/output devices and cross our fingers. To do things properly,
316 * audio hardware interfaces that need advanced features (like this) should
317 * convert to the new HAL interface and not use this wrapper. */
320 AUDIO_DEVICE_OUT_EARPIECE |
321 AUDIO_DEVICE_OUT_SPEAKER |
322 AUDIO_DEVICE_OUT_WIRED_HEADSET |
323 AUDIO_DEVICE_OUT_WIRED_HEADPHONE |
324 AUDIO_DEVICE_OUT_AUX_DIGITAL |
325 AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET |
326 AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET |
327 AUDIO_DEVICE_OUT_ALL_SCO |
328 AUDIO_DEVICE_OUT_DEFAULT |
330 AUDIO_DEVICE_IN_COMMUNICATION |
331 AUDIO_DEVICE_IN_AMBIENT |
332 AUDIO_DEVICE_IN_BUILTIN_MIC |
333 AUDIO_DEVICE_IN_WIRED_HEADSET |
334 AUDIO_DEVICE_IN_AUX_DIGITAL |
335 AUDIO_DEVICE_IN_BACK_MIC |
336 AUDIO_DEVICE_IN_ALL_SCO |
337 AUDIO_DEVICE_IN_DEFAULT);
340 static int adev_init_check(const struct audio_hw_device *dev)
342 const struct legacy_audio_device *ladev = to_cladev(dev);
344 return ladev->hwif->initCheck();
347 static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
349 struct legacy_audio_device *ladev = to_ladev(dev);
350 return ladev->hwif->setVoiceVolume(volume);
353 static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
355 struct legacy_audio_device *ladev = to_ladev(dev);
356 return ladev->hwif->setMasterVolume(volume);
359 static int adev_get_master_volume(struct audio_hw_device *dev, float* volume)
361 struct legacy_audio_device *ladev = to_ladev(dev);
362 return ladev->hwif->getMasterVolume(volume);
365 static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
367 struct legacy_audio_device *ladev = to_ladev(dev);
368 // as this is the legacy API, don't change it to use audio_mode_t instead of int
369 return ladev->hwif->setMode((int) mode);
372 static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
374 struct legacy_audio_device *ladev = to_ladev(dev);
375 return ladev->hwif->setMicMute(state);
378 static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
380 const struct legacy_audio_device *ladev = to_cladev(dev);
381 return ladev->hwif->getMicMute(state);
384 static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
386 struct legacy_audio_device *ladev = to_ladev(dev);
387 return ladev->hwif->setParameters(String8(kvpairs));
390 static char * adev_get_parameters(const struct audio_hw_device *dev,
393 const struct legacy_audio_device *ladev = to_cladev(dev);
396 s8 = ladev->hwif->getParameters(String8(keys));
397 return strdup(s8.string());
400 static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
401 uint32_t sample_rate, audio_format_t format,
404 const struct legacy_audio_device *ladev = to_cladev(dev);
405 return ladev->hwif->getInputBufferSize(sample_rate, (int) format, channel_count);
408 static int adev_open_output_stream(struct audio_hw_device *dev,
410 audio_format_t *format,
412 uint32_t *sample_rate,
413 struct audio_stream_out **stream_out)
415 struct legacy_audio_device *ladev = to_ladev(dev);
417 struct legacy_stream_out *out;
420 out = (struct legacy_stream_out *)calloc(1, sizeof(*out));
424 out->legacy_out = ladev->hwif->openOutputStream(devices, (int *) format, channels,
425 sample_rate, &status);
426 if (!out->legacy_out) {
431 out->stream.common.get_sample_rate = out_get_sample_rate;
432 out->stream.common.set_sample_rate = out_set_sample_rate;
433 out->stream.common.get_buffer_size = out_get_buffer_size;
434 out->stream.common.get_channels = out_get_channels;
435 out->stream.common.get_format = out_get_format;
436 out->stream.common.set_format = out_set_format;
437 out->stream.common.standby = out_standby;
438 out->stream.common.dump = out_dump;
439 out->stream.common.set_parameters = out_set_parameters;
440 out->stream.common.get_parameters = out_get_parameters;
441 out->stream.common.add_audio_effect = out_add_audio_effect;
442 out->stream.common.remove_audio_effect = out_remove_audio_effect;
443 out->stream.get_latency = out_get_latency;
444 out->stream.set_volume = out_set_volume;
445 out->stream.write = out_write;
446 out->stream.get_render_position = out_get_render_position;
447 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
449 *stream_out = &out->stream;
458 static void adev_close_output_stream(struct audio_hw_device *dev,
459 struct audio_stream_out* stream)
461 struct legacy_audio_device *ladev = to_ladev(dev);
462 struct legacy_stream_out *out = reinterpret_cast<struct legacy_stream_out *>(stream);
464 ladev->hwif->closeOutputStream(out->legacy_out);
468 /** This method creates and opens the audio hardware input stream */
469 static int adev_open_input_stream(struct audio_hw_device *dev,
470 uint32_t devices, audio_format_t *format,
471 uint32_t *channels, uint32_t *sample_rate,
472 audio_in_acoustics_t acoustics,
473 struct audio_stream_in **stream_in)
475 struct legacy_audio_device *ladev = to_ladev(dev);
477 struct legacy_stream_in *in;
480 in = (struct legacy_stream_in *)calloc(1, sizeof(*in));
484 in->legacy_in = ladev->hwif->openInputStream(devices, (int *) format, channels,
485 sample_rate, &status,
486 (AudioSystem::audio_in_acoustics)acoustics);
487 if (!in->legacy_in) {
492 in->stream.common.get_sample_rate = in_get_sample_rate;
493 in->stream.common.set_sample_rate = in_set_sample_rate;
494 in->stream.common.get_buffer_size = in_get_buffer_size;
495 in->stream.common.get_channels = in_get_channels;
496 in->stream.common.get_format = in_get_format;
497 in->stream.common.set_format = in_set_format;
498 in->stream.common.standby = in_standby;
499 in->stream.common.dump = in_dump;
500 in->stream.common.set_parameters = in_set_parameters;
501 in->stream.common.get_parameters = in_get_parameters;
502 in->stream.common.add_audio_effect = in_add_audio_effect;
503 in->stream.common.remove_audio_effect = in_remove_audio_effect;
504 in->stream.set_gain = in_set_gain;
505 in->stream.read = in_read;
506 in->stream.get_input_frames_lost = in_get_input_frames_lost;
508 *stream_in = &in->stream;
517 static void adev_close_input_stream(struct audio_hw_device *dev,
518 struct audio_stream_in *stream)
520 struct legacy_audio_device *ladev = to_ladev(dev);
521 struct legacy_stream_in *in =
522 reinterpret_cast<struct legacy_stream_in *>(stream);
524 ladev->hwif->closeInputStream(in->legacy_in);
528 static int adev_dump(const struct audio_hw_device *dev, int fd)
530 const struct legacy_audio_device *ladev = to_cladev(dev);
531 Vector<String16> args;
533 return ladev->hwif->dumpState(fd, args);
536 static int legacy_adev_close(hw_device_t* device)
538 struct audio_hw_device *hwdev =
539 reinterpret_cast<struct audio_hw_device *>(device);
540 struct legacy_audio_device *ladev = to_ladev(hwdev);
552 static int legacy_adev_open(const hw_module_t* module, const char* name,
553 hw_device_t** device)
555 struct legacy_audio_device *ladev;
558 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
561 ladev = (struct legacy_audio_device *)calloc(1, sizeof(*ladev));
565 ladev->device.common.tag = HARDWARE_DEVICE_TAG;
566 ladev->device.common.version = 0;
567 ladev->device.common.module = const_cast<hw_module_t*>(module);
568 ladev->device.common.close = legacy_adev_close;
570 ladev->device.get_supported_devices = adev_get_supported_devices;
571 ladev->device.init_check = adev_init_check;
572 ladev->device.set_voice_volume = adev_set_voice_volume;
573 ladev->device.set_master_volume = adev_set_master_volume;
574 ladev->device.get_master_volume = adev_get_master_volume;
575 ladev->device.set_mode = adev_set_mode;
576 ladev->device.set_mic_mute = adev_set_mic_mute;
577 ladev->device.get_mic_mute = adev_get_mic_mute;
578 ladev->device.set_parameters = adev_set_parameters;
579 ladev->device.get_parameters = adev_get_parameters;
580 ladev->device.get_input_buffer_size = adev_get_input_buffer_size;
581 ladev->device.open_output_stream = adev_open_output_stream;
582 ladev->device.close_output_stream = adev_close_output_stream;
583 ladev->device.open_input_stream = adev_open_input_stream;
584 ladev->device.close_input_stream = adev_close_input_stream;
585 ladev->device.dump = adev_dump;
587 ladev->hwif = createAudioHardware();
590 goto err_create_audio_hw;
593 *device = &ladev->device.common;
602 static struct hw_module_methods_t legacy_audio_module_methods = {
603 open: legacy_adev_open
606 struct legacy_audio_module HAL_MODULE_INFO_SYM = {
609 tag: HARDWARE_MODULE_TAG,
612 id: AUDIO_HARDWARE_MODULE_ID,
613 name: "LEGACY Audio HW HAL",
614 author: "The Android Open Source Project",
615 methods: &legacy_audio_module_methods,
624 }; // namespace android_audio_legacy