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];
91 AUDIO_A2DP_STATE_NONE,
92 AUDIO_A2DP_STATE_STANDBY,
93 AUDIO_A2DP_STATE_SUSPENDED,
94 AUDIO_A2DP_STATE_STARTED
97 struct a2dp_stream_out {
98 struct audio_stream_out stream;
100 struct audio_endpoint *ep;
101 enum a2dp_state_t audio_state;
104 struct a2dp_audio_dev {
105 struct audio_hw_device dev;
106 struct a2dp_stream_out *out;
109 static const a2dp_sbc_t sbc_presets[] = {
111 .frequency = SBC_SAMPLING_FREQ_44100 | SBC_SAMPLING_FREQ_48000,
112 .channel_mode = SBC_CHANNEL_MODE_MONO |
113 SBC_CHANNEL_MODE_DUAL_CHANNEL |
114 SBC_CHANNEL_MODE_STEREO |
115 SBC_CHANNEL_MODE_JOINT_STEREO,
116 .subbands = SBC_SUBBANDS_4 | SBC_SUBBANDS_8,
117 .allocation_method = SBC_ALLOCATION_SNR |
118 SBC_ALLOCATION_LOUDNESS,
119 .block_length = SBC_BLOCK_LENGTH_4 | SBC_BLOCK_LENGTH_8 |
120 SBC_BLOCK_LENGTH_12 | SBC_BLOCK_LENGTH_16,
121 .min_bitpool = MIN_BITPOOL,
122 .max_bitpool = MAX_BITPOOL
125 .frequency = SBC_SAMPLING_FREQ_44100,
126 .channel_mode = SBC_CHANNEL_MODE_JOINT_STEREO,
127 .subbands = SBC_SUBBANDS_8,
128 .allocation_method = SBC_ALLOCATION_LOUDNESS,
129 .block_length = SBC_BLOCK_LENGTH_16,
130 .min_bitpool = MIN_BITPOOL,
131 .max_bitpool = MAX_BITPOOL
134 .frequency = SBC_SAMPLING_FREQ_48000,
135 .channel_mode = SBC_CHANNEL_MODE_JOINT_STEREO,
136 .subbands = SBC_SUBBANDS_8,
137 .allocation_method = SBC_ALLOCATION_LOUDNESS,
138 .block_length = SBC_BLOCK_LENGTH_16,
139 .min_bitpool = MIN_BITPOOL,
140 .max_bitpool = MAX_BITPOOL
144 static int sbc_get_presets(struct audio_preset *preset, size_t *len)
149 uint8_t *ptr = (uint8_t *) preset;
150 size_t preset_size = sizeof(*preset) + sizeof(a2dp_sbc_t);
154 count = sizeof(sbc_presets) / sizeof(sbc_presets[0]);
156 for (i = 0; i < count; i++) {
157 preset = (struct audio_preset *) ptr;
159 if (new_len + preset_size > *len)
162 preset->len = sizeof(a2dp_sbc_t);
163 memcpy(preset->data, &sbc_presets[i], preset->len);
165 new_len += preset_size;
174 static void audio_ipc_cleanup(void)
177 shutdown(audio_sk, SHUT_RDWR);
182 static int audio_ipc_cmd(uint8_t service_id, uint8_t opcode, uint16_t len,
183 void *param, size_t *rsp_len, void *rsp, int *fd)
189 char cmsgbuf[CMSG_SPACE(sizeof(int))];
191 size_t s_len = sizeof(s);
194 error("audio: Invalid cmd socket passed to audio_ipc_cmd");
198 if (!rsp || !rsp_len) {
199 memset(&s, 0, s_len);
204 memset(&msg, 0, sizeof(msg));
205 memset(&cmd, 0, sizeof(cmd));
207 cmd.service_id = service_id;
211 iv[0].iov_base = &cmd;
212 iv[0].iov_len = sizeof(cmd);
214 iv[1].iov_base = param;
220 pthread_mutex_lock(&sk_mutex);
222 ret = sendmsg(audio_sk, &msg, 0);
224 error("audio: Sending command failed:%s", strerror(errno));
225 pthread_mutex_unlock(&sk_mutex);
229 /* socket was shutdown */
231 error("audio: Command socket closed");
235 memset(&msg, 0, sizeof(msg));
236 memset(&cmd, 0, sizeof(cmd));
238 iv[0].iov_base = &cmd;
239 iv[0].iov_len = sizeof(cmd);
241 iv[1].iov_base = rsp;
242 iv[1].iov_len = *rsp_len;
248 memset(cmsgbuf, 0, sizeof(cmsgbuf));
249 msg.msg_control = cmsgbuf;
250 msg.msg_controllen = sizeof(cmsgbuf);
253 ret = recvmsg(audio_sk, &msg, 0);
255 error("audio: Receiving command response failed:%s",
257 pthread_mutex_unlock(&sk_mutex);
261 pthread_mutex_unlock(&sk_mutex);
263 if (ret < (ssize_t) sizeof(cmd)) {
264 error("audio: Too small response received(%zd bytes)", ret);
268 if (cmd.service_id != service_id) {
269 error("audio: Invalid service id (%u vs %u)", cmd.service_id,
274 if (ret != (ssize_t) (sizeof(cmd) + cmd.len)) {
275 error("audio: Malformed response received(%zd bytes)", ret);
279 if (cmd.opcode != opcode && cmd.opcode != AUDIO_OP_STATUS) {
280 error("audio: Invalid opcode received (%u vs %u)",
285 if (cmd.opcode == AUDIO_OP_STATUS) {
286 struct hal_status *s = rsp;
288 if (sizeof(*s) != cmd.len) {
289 error("audio: Invalid status length");
293 if (s->code == AUDIO_STATUS_SUCCESS) {
294 error("audio: Invalid success status response");
301 /* Receive auxiliary data in msg */
303 struct cmsghdr *cmsg;
307 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
308 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
309 if (cmsg->cmsg_level == SOL_SOCKET
310 && cmsg->cmsg_type == SCM_RIGHTS) {
311 memcpy(fd, CMSG_DATA(cmsg), sizeof(int));
320 return AUDIO_STATUS_SUCCESS;
323 /* Some serious issue happen on IPC - recover */
324 shutdown(audio_sk, SHUT_RDWR);
326 return AUDIO_STATUS_FAILED;
329 static int ipc_open_cmd(const struct audio_codec *codec)
331 uint8_t buf[BLUEZ_AUDIO_MTU];
332 struct audio_cmd_open *cmd = (struct audio_cmd_open *) buf;
333 struct audio_rsp_open rsp;
334 size_t cmd_len = sizeof(buf) - sizeof(*cmd);
335 size_t rsp_len = sizeof(rsp);
340 memcpy(cmd->uuid, a2dp_src_uuid, sizeof(a2dp_src_uuid));
342 cmd->codec = codec->type;
343 cmd->presets = codec->get_presets(cmd->preset, &cmd_len);
345 cmd_len += sizeof(*cmd);
347 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_OPEN, cmd_len, cmd,
348 &rsp_len, &rsp, NULL);
350 if (result != AUDIO_STATUS_SUCCESS)
356 static int ipc_close_cmd(uint8_t endpoint_id)
358 struct audio_cmd_close cmd;
363 cmd.id = endpoint_id;
365 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_CLOSE,
366 sizeof(cmd), &cmd, NULL, NULL, NULL);
371 static int ipc_open_stream_cmd(uint8_t endpoint_id,
372 struct audio_preset **caps)
374 char buf[BLUEZ_AUDIO_MTU];
375 struct audio_cmd_open_stream cmd;
376 struct audio_rsp_open_stream *rsp =
377 (struct audio_rsp_open_stream *) &buf;
378 size_t rsp_len = sizeof(buf);
384 return AUDIO_STATUS_FAILED;
386 cmd.id = endpoint_id;
388 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_OPEN_STREAM,
389 sizeof(cmd), &cmd, &rsp_len, rsp, NULL);
391 if (result == AUDIO_STATUS_SUCCESS) {
392 size_t buf_len = sizeof(struct audio_preset) +
394 *caps = malloc(buf_len);
395 memcpy(*caps, &rsp->preset, buf_len);
403 static int register_endpoints(void)
405 struct audio_endpoint *ep = &audio_endpoints[0];
408 for (i = 0; i < NUM_CODECS; i++, ep++) {
409 const struct audio_codec *codec = &audio_codecs[i];
411 ep->id = ipc_open_cmd(codec);
414 return AUDIO_STATUS_FAILED;
417 ep->codec_data = NULL;
421 return AUDIO_STATUS_SUCCESS;
424 static void unregister_endpoints(void)
428 for (i = 0; i < MAX_AUDIO_ENDPOINTS; i++) {
429 struct audio_endpoint *ep = &audio_endpoints[i];
432 ipc_close_cmd(ep->id);
433 memset(ep, 0, sizeof(*ep));
438 static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
445 static uint32_t out_get_sample_rate(const struct audio_stream *stream)
451 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
457 static size_t out_get_buffer_size(const struct audio_stream *stream)
463 static uint32_t out_get_channels(const struct audio_stream *stream)
469 static audio_format_t out_get_format(const struct audio_stream *stream)
475 static int out_set_format(struct audio_stream *stream, audio_format_t format)
481 static int out_standby(struct audio_stream *stream)
487 static int out_dump(const struct audio_stream *stream, int fd)
493 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
499 static char *out_get_parameters(const struct audio_stream *stream,
506 static uint32_t out_get_latency(const struct audio_stream_out *stream)
512 static int out_set_volume(struct audio_stream_out *stream, float left,
516 /* volume controlled in audioflinger mixer (digital) */
520 static int out_get_render_position(const struct audio_stream_out *stream,
521 uint32_t *dsp_frames)
527 static int out_add_audio_effect(const struct audio_stream *stream,
528 effect_handle_t effect)
534 static int out_remove_audio_effect(const struct audio_stream *stream,
535 effect_handle_t effect)
541 static uint32_t in_get_sample_rate(const struct audio_stream *stream)
547 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
553 static size_t in_get_buffer_size(const struct audio_stream *stream)
559 static uint32_t in_get_channels(const struct audio_stream *stream)
565 static audio_format_t in_get_format(const struct audio_stream *stream)
571 static int in_set_format(struct audio_stream *stream, audio_format_t format)
577 static int in_standby(struct audio_stream *stream)
583 static int in_dump(const struct audio_stream *stream, int fd)
589 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
595 static char *in_get_parameters(const struct audio_stream *stream,
602 static int in_set_gain(struct audio_stream_in *stream, float gain)
608 static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
615 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
621 static int in_add_audio_effect(const struct audio_stream *stream,
622 effect_handle_t effect)
628 static int in_remove_audio_effect(const struct audio_stream *stream,
629 effect_handle_t effect)
635 static int audio_open_output_stream(struct audio_hw_device *dev,
636 audio_io_handle_t handle,
637 audio_devices_t devices,
638 audio_output_flags_t flags,
639 struct audio_config *config,
640 struct audio_stream_out **stream_out)
643 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
644 struct a2dp_stream_out *out;
645 struct audio_preset *preset;
647 out = calloc(1, sizeof(struct a2dp_stream_out));
653 out->stream.common.get_sample_rate = out_get_sample_rate;
654 out->stream.common.set_sample_rate = out_set_sample_rate;
655 out->stream.common.get_buffer_size = out_get_buffer_size;
656 out->stream.common.get_channels = out_get_channels;
657 out->stream.common.get_format = out_get_format;
658 out->stream.common.set_format = out_set_format;
659 out->stream.common.standby = out_standby;
660 out->stream.common.dump = out_dump;
661 out->stream.common.set_parameters = out_set_parameters;
662 out->stream.common.get_parameters = out_get_parameters;
663 out->stream.common.add_audio_effect = out_add_audio_effect;
664 out->stream.common.remove_audio_effect = out_remove_audio_effect;
665 out->stream.get_latency = out_get_latency;
666 out->stream.set_volume = out_set_volume;
667 out->stream.write = out_write;
668 out->stream.get_render_position = out_get_render_position;
670 /* TODO: for now we always use endpoint 0 */
671 out->ep = &audio_endpoints[0];
673 if (ipc_open_stream_cmd(out->ep->id, &preset) != AUDIO_STATUS_SUCCESS)
679 /* TODO: initialize codec using received audio_preset */
683 *stream_out = &out->stream;
686 out->audio_state = AUDIO_A2DP_STATE_STANDBY;
696 static void audio_close_output_stream(struct audio_hw_device *dev,
697 struct audio_stream_out *stream)
699 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
704 a2dp_dev->out = NULL;
707 static int audio_set_parameters(struct audio_hw_device *dev,
714 static char *audio_get_parameters(const struct audio_hw_device *dev,
721 static int audio_init_check(const struct audio_hw_device *dev)
727 static int audio_set_voice_volume(struct audio_hw_device *dev, float volume)
733 static int audio_set_master_volume(struct audio_hw_device *dev, float volume)
739 static int audio_set_mode(struct audio_hw_device *dev, int mode)
745 static int audio_set_mic_mute(struct audio_hw_device *dev, bool state)
751 static int audio_get_mic_mute(const struct audio_hw_device *dev, bool *state)
757 static size_t audio_get_input_buffer_size(const struct audio_hw_device *dev,
758 const struct audio_config *config)
764 static int audio_open_input_stream(struct audio_hw_device *dev,
765 audio_io_handle_t handle,
766 audio_devices_t devices,
767 struct audio_config *config,
768 struct audio_stream_in **stream_in)
770 struct audio_stream_in *in;
774 in = calloc(1, sizeof(struct audio_stream_in));
778 in->common.get_sample_rate = in_get_sample_rate;
779 in->common.set_sample_rate = in_set_sample_rate;
780 in->common.get_buffer_size = in_get_buffer_size;
781 in->common.get_channels = in_get_channels;
782 in->common.get_format = in_get_format;
783 in->common.set_format = in_set_format;
784 in->common.standby = in_standby;
785 in->common.dump = in_dump;
786 in->common.set_parameters = in_set_parameters;
787 in->common.get_parameters = in_get_parameters;
788 in->common.add_audio_effect = in_add_audio_effect;
789 in->common.remove_audio_effect = in_remove_audio_effect;
790 in->set_gain = in_set_gain;
792 in->get_input_frames_lost = in_get_input_frames_lost;
799 static void audio_close_input_stream(struct audio_hw_device *dev,
800 struct audio_stream_in *stream_in)
806 static int audio_dump(const audio_hw_device_t *device, int fd)
812 static int audio_close(hw_device_t *device)
814 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *)device;
818 pthread_mutex_lock(&close_mutex);
821 pthread_mutex_unlock(&close_mutex);
823 pthread_join(ipc_th, NULL);
832 static void *ipc_handler(void *data)
840 DBG("Waiting for connection ...");
841 audio_sk = accept(listen_sk, NULL, NULL);
844 error("audio: Failed to accept socket: %d (%s)", err,
849 DBG("Audio IPC: Connected");
851 if (register_endpoints() != AUDIO_STATUS_SUCCESS) {
852 error("audio: Failed to register endpoints");
854 unregister_endpoints();
856 shutdown(audio_sk, SHUT_RDWR);
860 memset(&pfd, 0, sizeof(pfd));
862 pfd.events = POLLHUP | POLLERR | POLLNVAL;
864 /* Check if socket is still alive. Empty while loop.*/
865 while (poll(&pfd, 1, -1) < 0 && errno == EINTR);
867 if (pfd.revents & (POLLHUP | POLLERR | POLLNVAL)) {
868 info("Audio HAL: Socket closed");
872 /*Check if audio_dev is closed */
873 pthread_mutex_lock(&close_mutex);
875 close_thread = false;
876 pthread_mutex_unlock(&close_mutex);
879 unregister_endpoints();
881 info("Closing Audio IPC thread");
885 static int audio_ipc_init(void)
887 struct sockaddr_un addr;
893 sk = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
896 error("audio: Failed to create socket: %d (%s)", err,
901 memset(&addr, 0, sizeof(addr));
902 addr.sun_family = AF_UNIX;
904 memcpy(addr.sun_path, BLUEZ_AUDIO_SK_PATH,
905 sizeof(BLUEZ_AUDIO_SK_PATH));
907 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
909 error("audio: Failed to bind socket: %d (%s)", err,
914 if (listen(sk, 1) < 0) {
916 error("audio: Failed to listen on the socket: %d (%s)", err,
923 err = pthread_create(&ipc_th, NULL, ipc_handler, NULL);
927 error("audio: Failed to start Audio IPC thread: %d (%s)",
939 static int audio_open(const hw_module_t *module, const char *name,
940 hw_device_t **device)
942 struct a2dp_audio_dev *a2dp_dev;
947 if (strcmp(name, AUDIO_HARDWARE_INTERFACE)) {
948 error("audio: interface %s not matching [%s]", name,
949 AUDIO_HARDWARE_INTERFACE);
953 err = audio_ipc_init();
957 a2dp_dev = calloc(1, sizeof(struct a2dp_audio_dev));
961 a2dp_dev->dev.common.version = AUDIO_DEVICE_API_VERSION_CURRENT;
962 a2dp_dev->dev.common.module = (struct hw_module_t *) module;
963 a2dp_dev->dev.common.close = audio_close;
965 a2dp_dev->dev.init_check = audio_init_check;
966 a2dp_dev->dev.set_voice_volume = audio_set_voice_volume;
967 a2dp_dev->dev.set_master_volume = audio_set_master_volume;
968 a2dp_dev->dev.set_mode = audio_set_mode;
969 a2dp_dev->dev.set_mic_mute = audio_set_mic_mute;
970 a2dp_dev->dev.get_mic_mute = audio_get_mic_mute;
971 a2dp_dev->dev.set_parameters = audio_set_parameters;
972 a2dp_dev->dev.get_parameters = audio_get_parameters;
973 a2dp_dev->dev.get_input_buffer_size = audio_get_input_buffer_size;
974 a2dp_dev->dev.open_output_stream = audio_open_output_stream;
975 a2dp_dev->dev.close_output_stream = audio_close_output_stream;
976 a2dp_dev->dev.open_input_stream = audio_open_input_stream;
977 a2dp_dev->dev.close_input_stream = audio_close_input_stream;
978 a2dp_dev->dev.dump = audio_dump;
980 /* Note that &a2dp_dev->dev.common is the same pointer as a2dp_dev.
981 * This results from the structure of following structs:a2dp_audio_dev,
982 * audio_hw_device. We will rely on this later in the code.*/
983 *device = &a2dp_dev->dev.common;
988 static struct hw_module_methods_t hal_module_methods = {
992 struct audio_module HAL_MODULE_INFO_SYM = {
994 .tag = HARDWARE_MODULE_TAG,
997 .id = AUDIO_HARDWARE_MODULE_ID,
998 .name = "A2DP Bluez HW HAL",
999 .author = "Intel Corporation",
1000 .methods = &hal_module_methods,