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 ipc_close_stream_cmd(uint8_t endpoint_id)
405 struct audio_cmd_close_stream cmd;
410 cmd.id = endpoint_id;
412 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_CLOSE_STREAM,
413 sizeof(cmd), &cmd, NULL, NULL, NULL);
418 static int ipc_resume_stream_cmd(uint8_t endpoint_id)
420 struct audio_cmd_resume_stream cmd;
425 cmd.id = endpoint_id;
427 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_RESUME_STREAM,
428 sizeof(cmd), &cmd, NULL, NULL, NULL);
433 static int register_endpoints(void)
435 struct audio_endpoint *ep = &audio_endpoints[0];
438 for (i = 0; i < NUM_CODECS; i++, ep++) {
439 const struct audio_codec *codec = &audio_codecs[i];
441 ep->id = ipc_open_cmd(codec);
444 return AUDIO_STATUS_FAILED;
447 ep->codec_data = NULL;
451 return AUDIO_STATUS_SUCCESS;
454 static void unregister_endpoints(void)
458 for (i = 0; i < MAX_AUDIO_ENDPOINTS; i++) {
459 struct audio_endpoint *ep = &audio_endpoints[i];
462 ipc_close_cmd(ep->id);
463 memset(ep, 0, sizeof(*ep));
468 static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
471 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
473 /* We can auto-start only from standby */
474 if (out->audio_state == AUDIO_A2DP_STATE_STANDBY) {
475 DBG("stream in standby, auto-start");
477 if (ipc_resume_stream_cmd(out->ep->id) != AUDIO_STATUS_SUCCESS)
480 out->audio_state = AUDIO_A2DP_STATE_STARTED;
483 if (out->audio_state != AUDIO_A2DP_STATE_STARTED) {
484 DBG("stream not started");
488 /* TODO: encode data using codec */
493 static uint32_t out_get_sample_rate(const struct audio_stream *stream)
499 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
505 static size_t out_get_buffer_size(const struct audio_stream *stream)
511 static uint32_t out_get_channels(const struct audio_stream *stream)
517 static audio_format_t out_get_format(const struct audio_stream *stream)
523 static int out_set_format(struct audio_stream *stream, audio_format_t format)
529 static int out_standby(struct audio_stream *stream)
535 static int out_dump(const struct audio_stream *stream, int fd)
541 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
547 static char *out_get_parameters(const struct audio_stream *stream,
554 static uint32_t out_get_latency(const struct audio_stream_out *stream)
560 static int out_set_volume(struct audio_stream_out *stream, float left,
564 /* volume controlled in audioflinger mixer (digital) */
568 static int out_get_render_position(const struct audio_stream_out *stream,
569 uint32_t *dsp_frames)
575 static int out_add_audio_effect(const struct audio_stream *stream,
576 effect_handle_t effect)
582 static int out_remove_audio_effect(const struct audio_stream *stream,
583 effect_handle_t effect)
589 static uint32_t in_get_sample_rate(const struct audio_stream *stream)
595 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
601 static size_t in_get_buffer_size(const struct audio_stream *stream)
607 static uint32_t in_get_channels(const struct audio_stream *stream)
613 static audio_format_t in_get_format(const struct audio_stream *stream)
619 static int in_set_format(struct audio_stream *stream, audio_format_t format)
625 static int in_standby(struct audio_stream *stream)
631 static int in_dump(const struct audio_stream *stream, int fd)
637 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
643 static char *in_get_parameters(const struct audio_stream *stream,
650 static int in_set_gain(struct audio_stream_in *stream, float gain)
656 static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
663 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
669 static int in_add_audio_effect(const struct audio_stream *stream,
670 effect_handle_t effect)
676 static int in_remove_audio_effect(const struct audio_stream *stream,
677 effect_handle_t effect)
683 static int audio_open_output_stream(struct audio_hw_device *dev,
684 audio_io_handle_t handle,
685 audio_devices_t devices,
686 audio_output_flags_t flags,
687 struct audio_config *config,
688 struct audio_stream_out **stream_out)
691 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
692 struct a2dp_stream_out *out;
693 struct audio_preset *preset;
695 out = calloc(1, sizeof(struct a2dp_stream_out));
701 out->stream.common.get_sample_rate = out_get_sample_rate;
702 out->stream.common.set_sample_rate = out_set_sample_rate;
703 out->stream.common.get_buffer_size = out_get_buffer_size;
704 out->stream.common.get_channels = out_get_channels;
705 out->stream.common.get_format = out_get_format;
706 out->stream.common.set_format = out_set_format;
707 out->stream.common.standby = out_standby;
708 out->stream.common.dump = out_dump;
709 out->stream.common.set_parameters = out_set_parameters;
710 out->stream.common.get_parameters = out_get_parameters;
711 out->stream.common.add_audio_effect = out_add_audio_effect;
712 out->stream.common.remove_audio_effect = out_remove_audio_effect;
713 out->stream.get_latency = out_get_latency;
714 out->stream.set_volume = out_set_volume;
715 out->stream.write = out_write;
716 out->stream.get_render_position = out_get_render_position;
718 /* TODO: for now we always use endpoint 0 */
719 out->ep = &audio_endpoints[0];
721 if (ipc_open_stream_cmd(out->ep->id, &preset) != AUDIO_STATUS_SUCCESS)
727 /* TODO: initialize codec using received audio_preset */
731 *stream_out = &out->stream;
734 out->audio_state = AUDIO_A2DP_STATE_STANDBY;
744 static void audio_close_output_stream(struct audio_hw_device *dev,
745 struct audio_stream_out *stream)
747 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
748 struct audio_endpoint *ep = a2dp_dev->out->ep;
752 ipc_close_stream_cmd(ep->id);
754 /* TODO: cleanup codec */
757 a2dp_dev->out = NULL;
760 static int audio_set_parameters(struct audio_hw_device *dev,
767 static char *audio_get_parameters(const struct audio_hw_device *dev,
774 static int audio_init_check(const struct audio_hw_device *dev)
780 static int audio_set_voice_volume(struct audio_hw_device *dev, float volume)
786 static int audio_set_master_volume(struct audio_hw_device *dev, float volume)
792 static int audio_set_mode(struct audio_hw_device *dev, int mode)
798 static int audio_set_mic_mute(struct audio_hw_device *dev, bool state)
804 static int audio_get_mic_mute(const struct audio_hw_device *dev, bool *state)
810 static size_t audio_get_input_buffer_size(const struct audio_hw_device *dev,
811 const struct audio_config *config)
817 static int audio_open_input_stream(struct audio_hw_device *dev,
818 audio_io_handle_t handle,
819 audio_devices_t devices,
820 struct audio_config *config,
821 struct audio_stream_in **stream_in)
823 struct audio_stream_in *in;
827 in = calloc(1, sizeof(struct audio_stream_in));
831 in->common.get_sample_rate = in_get_sample_rate;
832 in->common.set_sample_rate = in_set_sample_rate;
833 in->common.get_buffer_size = in_get_buffer_size;
834 in->common.get_channels = in_get_channels;
835 in->common.get_format = in_get_format;
836 in->common.set_format = in_set_format;
837 in->common.standby = in_standby;
838 in->common.dump = in_dump;
839 in->common.set_parameters = in_set_parameters;
840 in->common.get_parameters = in_get_parameters;
841 in->common.add_audio_effect = in_add_audio_effect;
842 in->common.remove_audio_effect = in_remove_audio_effect;
843 in->set_gain = in_set_gain;
845 in->get_input_frames_lost = in_get_input_frames_lost;
852 static void audio_close_input_stream(struct audio_hw_device *dev,
853 struct audio_stream_in *stream_in)
859 static int audio_dump(const audio_hw_device_t *device, int fd)
865 static int audio_close(hw_device_t *device)
867 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *)device;
871 pthread_mutex_lock(&close_mutex);
874 pthread_mutex_unlock(&close_mutex);
876 pthread_join(ipc_th, NULL);
885 static void *ipc_handler(void *data)
893 DBG("Waiting for connection ...");
894 audio_sk = accept(listen_sk, NULL, NULL);
897 error("audio: Failed to accept socket: %d (%s)", err,
902 DBG("Audio IPC: Connected");
904 if (register_endpoints() != AUDIO_STATUS_SUCCESS) {
905 error("audio: Failed to register endpoints");
907 unregister_endpoints();
909 shutdown(audio_sk, SHUT_RDWR);
913 memset(&pfd, 0, sizeof(pfd));
915 pfd.events = POLLHUP | POLLERR | POLLNVAL;
917 /* Check if socket is still alive. Empty while loop.*/
918 while (poll(&pfd, 1, -1) < 0 && errno == EINTR);
920 if (pfd.revents & (POLLHUP | POLLERR | POLLNVAL)) {
921 info("Audio HAL: Socket closed");
925 /*Check if audio_dev is closed */
926 pthread_mutex_lock(&close_mutex);
928 close_thread = false;
929 pthread_mutex_unlock(&close_mutex);
932 unregister_endpoints();
934 info("Closing Audio IPC thread");
938 static int audio_ipc_init(void)
940 struct sockaddr_un addr;
946 sk = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
949 error("audio: Failed to create socket: %d (%s)", err,
954 memset(&addr, 0, sizeof(addr));
955 addr.sun_family = AF_UNIX;
957 memcpy(addr.sun_path, BLUEZ_AUDIO_SK_PATH,
958 sizeof(BLUEZ_AUDIO_SK_PATH));
960 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
962 error("audio: Failed to bind socket: %d (%s)", err,
967 if (listen(sk, 1) < 0) {
969 error("audio: Failed to listen on the socket: %d (%s)", err,
976 err = pthread_create(&ipc_th, NULL, ipc_handler, NULL);
980 error("audio: Failed to start Audio IPC thread: %d (%s)",
992 static int audio_open(const hw_module_t *module, const char *name,
993 hw_device_t **device)
995 struct a2dp_audio_dev *a2dp_dev;
1000 if (strcmp(name, AUDIO_HARDWARE_INTERFACE)) {
1001 error("audio: interface %s not matching [%s]", name,
1002 AUDIO_HARDWARE_INTERFACE);
1006 err = audio_ipc_init();
1010 a2dp_dev = calloc(1, sizeof(struct a2dp_audio_dev));
1014 a2dp_dev->dev.common.version = AUDIO_DEVICE_API_VERSION_CURRENT;
1015 a2dp_dev->dev.common.module = (struct hw_module_t *) module;
1016 a2dp_dev->dev.common.close = audio_close;
1018 a2dp_dev->dev.init_check = audio_init_check;
1019 a2dp_dev->dev.set_voice_volume = audio_set_voice_volume;
1020 a2dp_dev->dev.set_master_volume = audio_set_master_volume;
1021 a2dp_dev->dev.set_mode = audio_set_mode;
1022 a2dp_dev->dev.set_mic_mute = audio_set_mic_mute;
1023 a2dp_dev->dev.get_mic_mute = audio_get_mic_mute;
1024 a2dp_dev->dev.set_parameters = audio_set_parameters;
1025 a2dp_dev->dev.get_parameters = audio_get_parameters;
1026 a2dp_dev->dev.get_input_buffer_size = audio_get_input_buffer_size;
1027 a2dp_dev->dev.open_output_stream = audio_open_output_stream;
1028 a2dp_dev->dev.close_output_stream = audio_close_output_stream;
1029 a2dp_dev->dev.open_input_stream = audio_open_input_stream;
1030 a2dp_dev->dev.close_input_stream = audio_close_input_stream;
1031 a2dp_dev->dev.dump = audio_dump;
1033 /* Note that &a2dp_dev->dev.common is the same pointer as a2dp_dev.
1034 * This results from the structure of following structs:a2dp_audio_dev,
1035 * audio_hw_device. We will rely on this later in the code.*/
1036 *device = &a2dp_dev->dev.common;
1041 static struct hw_module_methods_t hal_module_methods = {
1045 struct audio_module HAL_MODULE_INFO_SYM = {
1047 .tag = HARDWARE_MODULE_TAG,
1050 .id = AUDIO_HARDWARE_MODULE_ID,
1051 .name = "A2DP Bluez HW HAL",
1052 .author = "Intel Corporation",
1053 .methods = &hal_module_methods,