2 * Copyright (C) 2013 Intel Corporation
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.
24 #include <sys/socket.h>
28 #include <hardware/audio.h>
29 #include <hardware/hardware.h>
31 #include "audio-msg.h"
34 #include "../profiles/audio/a2dp-codecs.h"
36 static const uint8_t a2dp_src_uuid[] = {
37 0x00, 0x00, 0x11, 0x0a, 0x00, 0x00, 0x10, 0x00,
38 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb };
40 static int listen_sk = -1;
41 static int audio_sk = -1;
42 static bool close_thread = false;
44 static pthread_t ipc_th = 0;
45 static pthread_mutex_t close_mutex = PTHREAD_MUTEX_INITIALIZER;
46 static pthread_mutex_t sk_mutex = PTHREAD_MUTEX_INITIALIZER;
48 struct audio_input_config {
51 audio_format_t format;
54 static int sbc_get_presets(struct audio_preset *preset, size_t *len);
59 int (*get_presets) (struct audio_preset *preset, size_t *len);
61 int (*init) (struct audio_preset *preset, void **codec_data);
62 int (*cleanup) (void *codec_data);
63 int (*get_config) (void *codec_data,
64 struct audio_input_config *config);
65 ssize_t (*write_data) (void *codec_data, const void *buffer,
69 static const struct audio_codec audio_codecs[] = {
71 .type = A2DP_CODEC_SBC,
73 .get_presets = sbc_get_presets,
77 #define NUM_CODECS (sizeof(audio_codecs) / sizeof(audio_codecs[0]))
79 #define MAX_AUDIO_ENDPOINTS NUM_CODECS
81 struct audio_endpoint {
83 const struct audio_codec *codec;
88 static struct audio_endpoint audio_endpoints[MAX_AUDIO_ENDPOINTS];
90 struct a2dp_audio_dev {
91 struct audio_hw_device dev;
92 struct audio_stream_out *out;
95 static const a2dp_sbc_t sbc_presets[] = {
97 .frequency = SBC_SAMPLING_FREQ_44100 | SBC_SAMPLING_FREQ_48000,
98 .channel_mode = SBC_CHANNEL_MODE_MONO |
99 SBC_CHANNEL_MODE_DUAL_CHANNEL |
100 SBC_CHANNEL_MODE_STEREO |
101 SBC_CHANNEL_MODE_JOINT_STEREO,
102 .subbands = SBC_SUBBANDS_4 | SBC_SUBBANDS_8,
103 .allocation_method = SBC_ALLOCATION_SNR |
104 SBC_ALLOCATION_LOUDNESS,
105 .block_length = SBC_BLOCK_LENGTH_4 | SBC_BLOCK_LENGTH_8 |
106 SBC_BLOCK_LENGTH_12 | SBC_BLOCK_LENGTH_16,
107 .min_bitpool = MIN_BITPOOL,
108 .max_bitpool = MAX_BITPOOL
111 .frequency = SBC_SAMPLING_FREQ_44100,
112 .channel_mode = SBC_CHANNEL_MODE_JOINT_STEREO,
113 .subbands = SBC_SUBBANDS_8,
114 .allocation_method = SBC_ALLOCATION_LOUDNESS,
115 .block_length = SBC_BLOCK_LENGTH_16,
116 .min_bitpool = MIN_BITPOOL,
117 .max_bitpool = MAX_BITPOOL
120 .frequency = SBC_SAMPLING_FREQ_48000,
121 .channel_mode = SBC_CHANNEL_MODE_JOINT_STEREO,
122 .subbands = SBC_SUBBANDS_8,
123 .allocation_method = SBC_ALLOCATION_LOUDNESS,
124 .block_length = SBC_BLOCK_LENGTH_16,
125 .min_bitpool = MIN_BITPOOL,
126 .max_bitpool = MAX_BITPOOL
130 static int sbc_get_presets(struct audio_preset *preset, size_t *len)
135 uint8_t *ptr = (uint8_t *) preset;
136 size_t preset_size = sizeof(*preset) + sizeof(a2dp_sbc_t);
140 count = sizeof(sbc_presets) / sizeof(sbc_presets[0]);
142 for (i = 0; i < count; i++) {
143 preset = (struct audio_preset *) ptr;
145 if (new_len + preset_size > *len)
148 preset->len = sizeof(a2dp_sbc_t);
149 memcpy(preset->data, &sbc_presets[i], preset->len);
151 new_len += preset_size;
160 static void audio_ipc_cleanup(void)
163 shutdown(audio_sk, SHUT_RDWR);
168 static int audio_ipc_cmd(uint8_t service_id, uint8_t opcode, uint16_t len,
169 void *param, size_t *rsp_len, void *rsp, int *fd)
175 char cmsgbuf[CMSG_SPACE(sizeof(int))];
177 size_t s_len = sizeof(s);
180 error("audio: Invalid cmd socket passed to audio_ipc_cmd");
184 if (!rsp || !rsp_len) {
185 memset(&s, 0, s_len);
190 memset(&msg, 0, sizeof(msg));
191 memset(&cmd, 0, sizeof(cmd));
193 cmd.service_id = service_id;
197 iv[0].iov_base = &cmd;
198 iv[0].iov_len = sizeof(cmd);
200 iv[1].iov_base = param;
206 pthread_mutex_lock(&sk_mutex);
208 ret = sendmsg(audio_sk, &msg, 0);
210 error("audio: Sending command failed:%s", strerror(errno));
211 pthread_mutex_unlock(&sk_mutex);
215 /* socket was shutdown */
217 error("audio: Command socket closed");
221 memset(&msg, 0, sizeof(msg));
222 memset(&cmd, 0, sizeof(cmd));
224 iv[0].iov_base = &cmd;
225 iv[0].iov_len = sizeof(cmd);
227 iv[1].iov_base = rsp;
228 iv[1].iov_len = *rsp_len;
234 memset(cmsgbuf, 0, sizeof(cmsgbuf));
235 msg.msg_control = cmsgbuf;
236 msg.msg_controllen = sizeof(cmsgbuf);
239 ret = recvmsg(audio_sk, &msg, 0);
241 error("audio: Receiving command response failed:%s",
243 pthread_mutex_unlock(&sk_mutex);
247 pthread_mutex_unlock(&sk_mutex);
249 if (ret < (ssize_t) sizeof(cmd)) {
250 error("audio: Too small response received(%zd bytes)", ret);
254 if (cmd.service_id != service_id) {
255 error("audio: Invalid service id (%u vs %u)", cmd.service_id,
260 if (ret != (ssize_t) (sizeof(cmd) + cmd.len)) {
261 error("audio: Malformed response received(%zd bytes)", ret);
265 if (cmd.opcode != opcode && cmd.opcode != AUDIO_OP_STATUS) {
266 error("audio: Invalid opcode received (%u vs %u)",
271 if (cmd.opcode == AUDIO_OP_STATUS) {
272 struct hal_status *s = rsp;
274 if (sizeof(*s) != cmd.len) {
275 error("audio: Invalid status length");
279 if (s->code == AUDIO_STATUS_SUCCESS) {
280 error("audio: Invalid success status response");
287 /* Receive auxiliary data in msg */
289 struct cmsghdr *cmsg;
293 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
294 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
295 if (cmsg->cmsg_level == SOL_SOCKET
296 && cmsg->cmsg_type == SCM_RIGHTS) {
297 memcpy(fd, CMSG_DATA(cmsg), sizeof(int));
306 return AUDIO_STATUS_SUCCESS;
309 /* Some serious issue happen on IPC - recover */
310 shutdown(audio_sk, SHUT_RDWR);
312 return AUDIO_STATUS_FAILED;
315 static int ipc_open_cmd(const struct audio_codec *codec)
317 uint8_t buf[BLUEZ_AUDIO_MTU];
318 struct audio_cmd_open *cmd = (struct audio_cmd_open *) buf;
319 struct audio_rsp_open rsp;
320 size_t cmd_len = sizeof(buf) - sizeof(*cmd);
321 size_t rsp_len = sizeof(rsp);
326 memcpy(cmd->uuid, a2dp_src_uuid, sizeof(a2dp_src_uuid));
328 cmd->codec = codec->type;
329 cmd->presets = codec->get_presets(cmd->preset, &cmd_len);
331 cmd_len += sizeof(*cmd);
333 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_OPEN, cmd_len, cmd,
334 &rsp_len, &rsp, NULL);
336 if (result != AUDIO_STATUS_SUCCESS)
342 static int register_endpoints(void)
344 struct audio_endpoint *ep = &audio_endpoints[0];
347 for (i = 0; i < NUM_CODECS; i++, ep++) {
348 const struct audio_codec *codec = &audio_codecs[i];
350 ep->id = ipc_open_cmd(codec);
353 return AUDIO_STATUS_FAILED;
356 ep->codec_data = NULL;
360 return AUDIO_STATUS_SUCCESS;
363 static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
370 static uint32_t out_get_sample_rate(const struct audio_stream *stream)
376 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
382 static size_t out_get_buffer_size(const struct audio_stream *stream)
388 static uint32_t out_get_channels(const struct audio_stream *stream)
394 static audio_format_t out_get_format(const struct audio_stream *stream)
400 static int out_set_format(struct audio_stream *stream, audio_format_t format)
406 static int out_standby(struct audio_stream *stream)
412 static int out_dump(const struct audio_stream *stream, int fd)
418 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
424 static char *out_get_parameters(const struct audio_stream *stream,
431 static uint32_t out_get_latency(const struct audio_stream_out *stream)
437 static int out_set_volume(struct audio_stream_out *stream, float left,
441 /* volume controlled in audioflinger mixer (digital) */
445 static int out_get_render_position(const struct audio_stream_out *stream,
446 uint32_t *dsp_frames)
452 static int out_add_audio_effect(const struct audio_stream *stream,
453 effect_handle_t effect)
459 static int out_remove_audio_effect(const struct audio_stream *stream,
460 effect_handle_t effect)
466 static uint32_t in_get_sample_rate(const struct audio_stream *stream)
472 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
478 static size_t in_get_buffer_size(const struct audio_stream *stream)
484 static uint32_t in_get_channels(const struct audio_stream *stream)
490 static audio_format_t in_get_format(const struct audio_stream *stream)
496 static int in_set_format(struct audio_stream *stream, audio_format_t format)
502 static int in_standby(struct audio_stream *stream)
508 static int in_dump(const struct audio_stream *stream, int fd)
514 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
520 static char *in_get_parameters(const struct audio_stream *stream,
527 static int in_set_gain(struct audio_stream_in *stream, float gain)
533 static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
540 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
546 static int in_add_audio_effect(const struct audio_stream *stream,
547 effect_handle_t effect)
553 static int in_remove_audio_effect(const struct audio_stream *stream,
554 effect_handle_t effect)
560 static int audio_open_output_stream(struct audio_hw_device *dev,
561 audio_io_handle_t handle,
562 audio_devices_t devices,
563 audio_output_flags_t flags,
564 struct audio_config *config,
565 struct audio_stream_out **stream_out)
568 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
569 struct audio_stream_out *out;
571 out = calloc(1, sizeof(struct audio_stream_out));
577 out->common.get_sample_rate = out_get_sample_rate;
578 out->common.set_sample_rate = out_set_sample_rate;
579 out->common.get_buffer_size = out_get_buffer_size;
580 out->common.get_channels = out_get_channels;
581 out->common.get_format = out_get_format;
582 out->common.set_format = out_set_format;
583 out->common.standby = out_standby;
584 out->common.dump = out_dump;
585 out->common.set_parameters = out_set_parameters;
586 out->common.get_parameters = out_get_parameters;
587 out->common.add_audio_effect = out_add_audio_effect;
588 out->common.remove_audio_effect = out_remove_audio_effect;
589 out->get_latency = out_get_latency;
590 out->set_volume = out_set_volume;
591 out->write = out_write;
592 out->get_render_position = out_get_render_position;
600 static void audio_close_output_stream(struct audio_hw_device *dev,
601 struct audio_stream_out *stream)
603 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
608 a2dp_dev->out = NULL;
611 static int audio_set_parameters(struct audio_hw_device *dev,
618 static char *audio_get_parameters(const struct audio_hw_device *dev,
625 static int audio_init_check(const struct audio_hw_device *dev)
631 static int audio_set_voice_volume(struct audio_hw_device *dev, float volume)
637 static int audio_set_master_volume(struct audio_hw_device *dev, float volume)
643 static int audio_set_mode(struct audio_hw_device *dev, int mode)
649 static int audio_set_mic_mute(struct audio_hw_device *dev, bool state)
655 static int audio_get_mic_mute(const struct audio_hw_device *dev, bool *state)
661 static size_t audio_get_input_buffer_size(const struct audio_hw_device *dev,
662 const struct audio_config *config)
668 static int audio_open_input_stream(struct audio_hw_device *dev,
669 audio_io_handle_t handle,
670 audio_devices_t devices,
671 struct audio_config *config,
672 struct audio_stream_in **stream_in)
674 struct audio_stream_in *in;
678 in = calloc(1, sizeof(struct audio_stream_in));
682 in->common.get_sample_rate = in_get_sample_rate;
683 in->common.set_sample_rate = in_set_sample_rate;
684 in->common.get_buffer_size = in_get_buffer_size;
685 in->common.get_channels = in_get_channels;
686 in->common.get_format = in_get_format;
687 in->common.set_format = in_set_format;
688 in->common.standby = in_standby;
689 in->common.dump = in_dump;
690 in->common.set_parameters = in_set_parameters;
691 in->common.get_parameters = in_get_parameters;
692 in->common.add_audio_effect = in_add_audio_effect;
693 in->common.remove_audio_effect = in_remove_audio_effect;
694 in->set_gain = in_set_gain;
696 in->get_input_frames_lost = in_get_input_frames_lost;
703 static void audio_close_input_stream(struct audio_hw_device *dev,
704 struct audio_stream_in *stream_in)
710 static int audio_dump(const audio_hw_device_t *device, int fd)
716 static int audio_close(hw_device_t *device)
718 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *)device;
722 pthread_mutex_lock(&close_mutex);
725 pthread_mutex_unlock(&close_mutex);
727 pthread_join(ipc_th, NULL);
736 static void *ipc_handler(void *data)
744 DBG("Waiting for connection ...");
745 audio_sk = accept(listen_sk, NULL, NULL);
748 error("audio: Failed to accept socket: %d (%s)", err,
753 DBG("Audio IPC: Connected");
755 if (register_endpoints() != AUDIO_STATUS_SUCCESS) {
756 error("audio: Failed to register endpoints");
758 shutdown(audio_sk, SHUT_RDWR);
762 memset(&pfd, 0, sizeof(pfd));
764 pfd.events = POLLHUP | POLLERR | POLLNVAL;
766 /* Check if socket is still alive. Empty while loop.*/
767 while (poll(&pfd, 1, -1) < 0 && errno == EINTR);
769 if (pfd.revents & (POLLHUP | POLLERR | POLLNVAL)) {
770 info("Audio HAL: Socket closed");
774 /*Check if audio_dev is closed */
775 pthread_mutex_lock(&close_mutex);
777 close_thread = false;
778 pthread_mutex_unlock(&close_mutex);
781 info("Closing Audio IPC thread");
785 static int audio_ipc_init(void)
787 struct sockaddr_un addr;
793 sk = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
796 error("audio: Failed to create socket: %d (%s)", err,
801 memset(&addr, 0, sizeof(addr));
802 addr.sun_family = AF_UNIX;
804 memcpy(addr.sun_path, BLUEZ_AUDIO_SK_PATH,
805 sizeof(BLUEZ_AUDIO_SK_PATH));
807 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
809 error("audio: Failed to bind socket: %d (%s)", err,
814 if (listen(sk, 1) < 0) {
816 error("audio: Failed to listen on the socket: %d (%s)", err,
823 err = pthread_create(&ipc_th, NULL, ipc_handler, NULL);
827 error("audio: Failed to start Audio IPC thread: %d (%s)",
839 static int audio_open(const hw_module_t *module, const char *name,
840 hw_device_t **device)
842 struct a2dp_audio_dev *a2dp_dev;
847 if (strcmp(name, AUDIO_HARDWARE_INTERFACE)) {
848 error("audio: interface %s not matching [%s]", name,
849 AUDIO_HARDWARE_INTERFACE);
853 err = audio_ipc_init();
857 a2dp_dev = calloc(1, sizeof(struct a2dp_audio_dev));
861 a2dp_dev->dev.common.version = AUDIO_DEVICE_API_VERSION_CURRENT;
862 a2dp_dev->dev.common.module = (struct hw_module_t *) module;
863 a2dp_dev->dev.common.close = audio_close;
865 a2dp_dev->dev.init_check = audio_init_check;
866 a2dp_dev->dev.set_voice_volume = audio_set_voice_volume;
867 a2dp_dev->dev.set_master_volume = audio_set_master_volume;
868 a2dp_dev->dev.set_mode = audio_set_mode;
869 a2dp_dev->dev.set_mic_mute = audio_set_mic_mute;
870 a2dp_dev->dev.get_mic_mute = audio_get_mic_mute;
871 a2dp_dev->dev.set_parameters = audio_set_parameters;
872 a2dp_dev->dev.get_parameters = audio_get_parameters;
873 a2dp_dev->dev.get_input_buffer_size = audio_get_input_buffer_size;
874 a2dp_dev->dev.open_output_stream = audio_open_output_stream;
875 a2dp_dev->dev.close_output_stream = audio_close_output_stream;
876 a2dp_dev->dev.open_input_stream = audio_open_input_stream;
877 a2dp_dev->dev.close_input_stream = audio_close_input_stream;
878 a2dp_dev->dev.dump = audio_dump;
880 /* Note that &a2dp_dev->dev.common is the same pointer as a2dp_dev.
881 * This results from the structure of following structs:a2dp_audio_dev,
882 * audio_hw_device. We will rely on this later in the code.*/
883 *device = &a2dp_dev->dev.common;
888 static struct hw_module_methods_t hal_module_methods = {
892 struct audio_module HAL_MODULE_INFO_SYM = {
894 .tag = HARDWARE_MODULE_TAG,
897 .id = AUDIO_HARDWARE_MODULE_ID,
898 .name = "A2DP Bluez HW HAL",
899 .author = "Intel Corporation",
900 .methods = &hal_module_methods,