2 * Copyright (C) 2012 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 "audio_hw_generic"
25 #include <sys/socket.h>
28 #include <sys/types.h>
32 #include <cutils/log.h>
33 #include <cutils/str_parms.h>
35 #include <hardware/audio.h>
36 #include <hardware/hardware.h>
37 #include <system/audio.h>
39 #include "anbox/audio/client_info.h"
41 #define AUDIO_DEVICE_NAME "/dev/anbox_audio"
42 #define OUT_SAMPLING_RATE 44100
43 #define OUT_BUFFER_SIZE 4096
44 #define OUT_LATENCY_MS 20
45 #define IN_SAMPLING_RATE 8000
46 #define IN_BUFFER_SIZE 320
48 struct generic_audio_device {
49 struct audio_hw_device device;
51 struct audio_stream_out *output;
52 struct audio_stream_in *input;
56 struct generic_stream_out {
57 struct audio_stream_out stream;
58 struct generic_audio_device *dev;
59 audio_devices_t device;
63 struct generic_stream_in {
64 struct audio_stream_in stream;
65 struct generic_audio_device *dev;
66 audio_devices_t device;
70 static uint32_t out_get_sample_rate(const struct audio_stream *stream) {
71 return OUT_SAMPLING_RATE;
74 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate) {
78 static size_t out_get_buffer_size(const struct audio_stream *stream) {
79 return OUT_BUFFER_SIZE;
82 static audio_channel_mask_t out_get_channels(const struct audio_stream *stream) {
83 return AUDIO_CHANNEL_OUT_STEREO;
86 static audio_format_t out_get_format(const struct audio_stream *stream) {
87 return AUDIO_FORMAT_PCM_16_BIT;
90 static int out_set_format(struct audio_stream *stream, audio_format_t format) {
94 static int out_standby(struct audio_stream *stream) {
98 static int out_dump(const struct audio_stream *stream, int fd) {
99 struct generic_stream_out *out = (struct generic_stream_out *)stream;
103 "\t\tsample rate: %u\n"
104 "\t\tbuffer size: %u\n"
105 "\t\tchannel mask: %08x\n"
108 "\t\taudio dev: %p\n\n",
109 out_get_sample_rate(stream),
110 out_get_buffer_size(stream),
111 out_get_channels(stream),
112 out_get_format(stream),
119 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs) {
120 struct generic_stream_out *out = (struct generic_stream_out *)stream;
121 struct str_parms *parms;
127 parms = str_parms_create_str(kvpairs);
129 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING,
130 value, sizeof(value));
133 val = strtol(value, &end, 10);
134 if (errno == 0 && (end != NULL) && (*end == '\0') && ((int)val == val)) {
135 out->device = (int)val;
141 str_parms_destroy(parms);
145 static char *out_get_parameters(const struct audio_stream *stream, const char *keys) {
146 struct generic_stream_out *out = (struct generic_stream_out *)stream;
147 struct str_parms *query = str_parms_create_str(keys);
150 struct str_parms *reply = str_parms_create();
153 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
155 str_parms_add_int(reply, AUDIO_PARAMETER_STREAM_ROUTING, out->device);
156 str = strdup(str_parms_to_str(reply));
161 str_parms_destroy(query);
162 str_parms_destroy(reply);
166 static uint32_t out_get_latency(const struct audio_stream_out *stream) {
167 return OUT_LATENCY_MS;
170 static int out_set_volume(struct audio_stream_out *stream, float left,
175 static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
177 struct generic_stream_out *out = (struct generic_stream_out *)stream;
178 struct generic_audio_device *adev = out->dev;
180 pthread_mutex_lock(&adev->lock);
182 bytes = write(out->fd, buffer, bytes);
183 pthread_mutex_unlock(&adev->lock);
187 static int out_get_render_position(const struct audio_stream_out *stream,
188 uint32_t *dsp_frames) {
192 static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect) {
196 static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect) {
200 static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
201 int64_t *timestamp) {
205 static uint32_t in_get_sample_rate(const struct audio_stream *stream) {
206 return IN_SAMPLING_RATE;
209 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate) {
213 static size_t in_get_buffer_size(const struct audio_stream *stream) {
214 return IN_BUFFER_SIZE;
217 static audio_channel_mask_t in_get_channels(const struct audio_stream *stream) {
218 return AUDIO_CHANNEL_IN_MONO;
221 static audio_format_t in_get_format(const struct audio_stream *stream) {
222 return AUDIO_FORMAT_PCM_16_BIT;
225 static int in_set_format(struct audio_stream *stream, audio_format_t format) {
229 static int in_standby(struct audio_stream *stream) {
233 static int in_dump(const struct audio_stream *stream, int fd) {
234 struct generic_stream_in *in = (struct generic_stream_in *)stream;
238 "\t\tsample rate: %u\n"
239 "\t\tbuffer size: %u\n"
240 "\t\tchannel mask: %08x\n"
243 "\t\taudio dev: %p\n\n",
244 in_get_sample_rate(stream),
245 in_get_buffer_size(stream),
246 in_get_channels(stream),
247 in_get_format(stream),
254 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs) {
255 struct generic_stream_in *in = (struct generic_stream_in *)stream;
256 struct str_parms *parms;
262 parms = str_parms_create_str(kvpairs);
264 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING,
265 value, sizeof(value));
268 val = strtol(value, &end, 10);
269 if ((errno == 0) && (end != NULL) && (*end == '\0') && ((int)val == val)) {
270 in->device = (int)val;
276 str_parms_destroy(parms);
280 static char *in_get_parameters(const struct audio_stream *stream,
282 struct generic_stream_in *in = (struct generic_stream_in *)stream;
283 struct str_parms *query = str_parms_create_str(keys);
286 struct str_parms *reply = str_parms_create();
289 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
291 str_parms_add_int(reply, AUDIO_PARAMETER_STREAM_ROUTING, in->device);
292 str = strdup(str_parms_to_str(reply));
297 str_parms_destroy(query);
298 str_parms_destroy(reply);
302 static int in_set_gain(struct audio_stream_in *stream, float gain) {
306 static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
308 struct generic_stream_in *in = (struct generic_stream_in *)stream;
309 struct generic_audio_device *adev = in->dev;
311 pthread_mutex_lock(&adev->lock);
313 bytes = read(in->fd, buffer, bytes);
314 if (adev->mic_mute && (bytes > 0)) {
315 memset(buffer, 0, bytes);
317 pthread_mutex_unlock(&adev->lock);
322 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream) {
326 static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect) {
330 static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect) {
334 static int connect_audio_server(const anbox::audio::ClientInfo::Type &type) {
335 int fd = socket(AF_LOCAL, SOCK_STREAM, 0);
339 struct sockaddr_un addr;
340 memset(&addr, 0, sizeof(addr));
341 addr.sun_family = AF_UNIX;
342 strncpy(addr.sun_path, AUDIO_DEVICE_NAME, sizeof(addr.sun_path));
344 if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
349 // We will send out client type information to the server and the
350 // server will either deny the request by closing the connection
351 // or by sending us the approved client details back.
352 anbox::audio::ClientInfo client_info{type};
353 if (::write(fd, &client_info, sizeof(client_info)) < 0) {
358 auto bytes_read = ::read(fd, &client_info, sizeof(client_info));
359 if (bytes_read < 0) {
364 // FIXME once we have real client details we need to check if we
365 // got everything we need or if anything is missing.
367 ALOGE("Successfully connected Anbox audio server");
372 static int adev_open_output_stream(struct audio_hw_device *dev,
373 audio_io_handle_t handle,
374 audio_devices_t devices,
375 audio_output_flags_t flags,
376 struct audio_config *config,
377 struct audio_stream_out **stream_out,
378 const char *address __unused) {
379 struct generic_audio_device *adev = (struct generic_audio_device *)dev;
380 struct generic_stream_out *out;
383 pthread_mutex_lock(&adev->lock);
384 if (adev->output != NULL) {
389 fd = connect_audio_server(anbox::audio::ClientInfo::Type::Playback);
392 ALOGE("Failed to connect with Anbox audio servers (err %d)", ret);
396 if ((config->format != AUDIO_FORMAT_PCM_16_BIT) ||
397 (config->channel_mask != AUDIO_CHANNEL_OUT_STEREO) ||
398 (config->sample_rate != OUT_SAMPLING_RATE)) {
399 ALOGE("Error opening output stream format %d, channel_mask %04x, sample_rate %u",
400 config->format, config->channel_mask, config->sample_rate);
401 config->format = AUDIO_FORMAT_PCM_16_BIT;
402 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
403 config->sample_rate = OUT_SAMPLING_RATE;
408 out = (struct generic_stream_out *)calloc(1, sizeof(struct generic_stream_out));
411 out->stream.common.get_sample_rate = out_get_sample_rate;
412 out->stream.common.set_sample_rate = out_set_sample_rate;
413 out->stream.common.get_buffer_size = out_get_buffer_size;
414 out->stream.common.get_channels = out_get_channels;
415 out->stream.common.get_format = out_get_format;
416 out->stream.common.set_format = out_set_format;
417 out->stream.common.standby = out_standby;
418 out->stream.common.dump = out_dump;
419 out->stream.common.set_parameters = out_set_parameters;
420 out->stream.common.get_parameters = out_get_parameters;
421 out->stream.common.add_audio_effect = out_add_audio_effect;
422 out->stream.common.remove_audio_effect = out_remove_audio_effect;
423 out->stream.get_latency = out_get_latency;
424 out->stream.set_volume = out_set_volume;
425 out->stream.write = out_write;
426 out->stream.get_render_position = out_get_render_position;
427 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
430 out->device = devices;
431 adev->output = (struct audio_stream_out *)out;
432 *stream_out = &out->stream;
435 pthread_mutex_unlock(&adev->lock);
440 static void adev_close_output_stream(struct audio_hw_device *dev,
441 struct audio_stream_out *stream) {
442 struct generic_audio_device *adev = (struct generic_audio_device *)dev;
444 pthread_mutex_lock(&adev->lock);
445 if (stream == adev->output) {
449 pthread_mutex_unlock(&adev->lock);
452 static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs) {
456 static char *adev_get_parameters(const struct audio_hw_device *dev,
461 static int adev_init_check(const struct audio_hw_device *dev) {
465 static int adev_set_voice_volume(struct audio_hw_device *dev, float volume) {
469 static int adev_set_master_volume(struct audio_hw_device *dev, float volume) {
473 static int adev_get_master_volume(struct audio_hw_device *dev, float *volume) {
477 static int adev_set_master_mute(struct audio_hw_device *dev, bool muted) {
481 static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted) {
485 static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode) {
489 static int adev_set_mic_mute(struct audio_hw_device *dev, bool state) {
490 struct generic_audio_device *adev = (struct generic_audio_device *)dev;
492 pthread_mutex_lock(&adev->lock);
493 adev->mic_mute = state;
494 pthread_mutex_unlock(&adev->lock);
498 static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state) {
499 struct generic_audio_device *adev = (struct generic_audio_device *)dev;
501 pthread_mutex_lock(&adev->lock);
502 *state = adev->mic_mute;
503 pthread_mutex_unlock(&adev->lock);
508 static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
509 const struct audio_config *config) {
510 return IN_BUFFER_SIZE;
513 static int adev_open_input_stream(struct audio_hw_device *dev,
514 audio_io_handle_t handle,
515 audio_devices_t devices,
516 struct audio_config *config,
517 struct audio_stream_in **stream_in,
518 audio_input_flags_t flags __unused,
519 const char *address __unused,
520 audio_source_t source __unused) {
521 struct generic_audio_device *adev = (struct generic_audio_device *)dev;
522 struct generic_stream_in *in;
525 pthread_mutex_lock(&adev->lock);
526 if (adev->input != NULL) {
531 if ((config->format != AUDIO_FORMAT_PCM_16_BIT) ||
532 (config->channel_mask != AUDIO_CHANNEL_IN_MONO) ||
533 (config->sample_rate != IN_SAMPLING_RATE)) {
534 ALOGE("Error opening input stream format %d, channel_mask %04x, sample_rate %u",
535 config->format, config->channel_mask, config->sample_rate);
536 config->format = AUDIO_FORMAT_PCM_16_BIT;
537 config->channel_mask = AUDIO_CHANNEL_IN_MONO;
538 config->sample_rate = IN_SAMPLING_RATE;
543 fd = connect_audio_server(anbox::audio::ClientInfo::Type::Recording);
546 ALOGE("Failed to connect with Anbox audio servers (err %d)", ret);
550 in = (struct generic_stream_in *)calloc(1, sizeof(struct generic_stream_in));
553 in->stream.common.get_sample_rate = in_get_sample_rate;
554 in->stream.common.set_sample_rate = in_set_sample_rate;
555 in->stream.common.get_buffer_size = in_get_buffer_size;
556 in->stream.common.get_channels = in_get_channels;
557 in->stream.common.get_format = in_get_format;
558 in->stream.common.set_format = in_set_format;
559 in->stream.common.standby = in_standby;
560 in->stream.common.dump = in_dump;
561 in->stream.common.set_parameters = in_set_parameters;
562 in->stream.common.get_parameters = in_get_parameters;
563 in->stream.common.add_audio_effect = in_add_audio_effect;
564 in->stream.common.remove_audio_effect = in_remove_audio_effect;
565 in->stream.set_gain = in_set_gain;
566 in->stream.read = in_read;
567 in->stream.get_input_frames_lost = in_get_input_frames_lost;
570 in->device = devices;
571 adev->input = (struct audio_stream_in *)in;
572 *stream_in = &in->stream;
575 pthread_mutex_unlock(&adev->lock);
580 static void adev_close_input_stream(struct audio_hw_device *dev,
581 struct audio_stream_in *stream) {
582 struct generic_audio_device *adev = (struct generic_audio_device *)dev;
584 pthread_mutex_lock(&adev->lock);
585 if (stream == adev->input) {
589 pthread_mutex_unlock(&adev->lock);
592 static int adev_dump(const audio_hw_device_t *dev, int fd) {
593 struct generic_audio_device *adev = (struct generic_audio_device *)dev;
595 const size_t SIZE = 256;
603 adev->mic_mute ? "true" : "false",
607 if (adev->output != NULL)
608 out_dump((const struct audio_stream *)adev->output, fd);
609 if (adev->input != NULL)
610 in_dump((const struct audio_stream *)adev->input, fd);
615 static int adev_close(hw_device_t *dev) {
616 struct generic_audio_device *adev = (struct generic_audio_device *)dev;
618 adev_close_output_stream((struct audio_hw_device *)dev, adev->output);
619 adev_close_input_stream((struct audio_hw_device *)dev, adev->input);
625 static int adev_open(const hw_module_t *module, const char *name,
626 hw_device_t **device) {
627 struct generic_audio_device *adev;
629 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
632 adev = (struct generic_audio_device*) calloc(1, sizeof(struct generic_audio_device));
634 adev->device.common.tag = HARDWARE_DEVICE_TAG;
635 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
636 adev->device.common.module = (struct hw_module_t *)module;
637 adev->device.common.close = adev_close;
639 adev->device.init_check = adev_init_check;
640 adev->device.set_voice_volume = adev_set_voice_volume;
641 adev->device.set_master_volume = adev_set_master_volume;
642 adev->device.get_master_volume = adev_get_master_volume;
643 adev->device.set_master_mute = adev_set_master_mute;
644 adev->device.get_master_mute = adev_get_master_mute;
645 adev->device.set_mode = adev_set_mode;
646 adev->device.set_mic_mute = adev_set_mic_mute;
647 adev->device.get_mic_mute = adev_get_mic_mute;
648 adev->device.set_parameters = adev_set_parameters;
649 adev->device.get_parameters = adev_get_parameters;
650 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
651 adev->device.open_output_stream = adev_open_output_stream;
652 adev->device.close_output_stream = adev_close_output_stream;
653 adev->device.open_input_stream = adev_open_input_stream;
654 adev->device.close_input_stream = adev_close_input_stream;
655 adev->device.dump = adev_dump;
657 *device = &adev->device.common;
662 static struct hw_module_methods_t hal_module_methods = {
666 struct audio_module HAL_MODULE_INFO_SYM = {
668 .tag = HARDWARE_MODULE_TAG,
669 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
670 .hal_api_version = HARDWARE_HAL_API_VERSION,
671 .id = AUDIO_HARDWARE_MODULE_ID,
672 .name = "Anbox audio HW HAL",
673 .author = "The Android Open Source Project",
674 .methods = &hal_module_methods,