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>
33 #include "audio-msg.h"
36 #include "../profiles/audio/a2dp-codecs.h"
38 static const uint8_t a2dp_src_uuid[] = {
39 0x00, 0x00, 0x11, 0x0a, 0x00, 0x00, 0x10, 0x00,
40 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb };
42 static int listen_sk = -1;
43 static int audio_sk = -1;
44 static bool close_thread = false;
46 static pthread_t ipc_th = 0;
47 static pthread_mutex_t close_mutex = PTHREAD_MUTEX_INITIALIZER;
48 static pthread_mutex_t sk_mutex = PTHREAD_MUTEX_INITIALIZER;
50 #if __BYTE_ORDER == __LITTLE_ENDIAN
61 uint16_t sequence_number;
65 } __attribute__ ((packed));
68 unsigned frame_count:4;
70 unsigned is_last_fragment:1;
71 unsigned is_first_fragment:1;
72 unsigned is_fragmented:1;
73 } __attribute__ ((packed));
75 #elif __BYTE_ORDER == __BIG_ENDIAN
86 uint16_t sequence_number;
90 } __attribute__ ((packed));
93 unsigned is_fragmented:1;
94 unsigned is_first_fragment:1;
95 unsigned is_last_fragment:1;
97 unsigned frame_count:4;
98 } __attribute__ ((packed));
101 #error "Unknown byte order"
104 struct media_packet {
105 struct rtp_header hdr;
106 struct rtp_payload payload;
110 struct audio_input_config {
113 audio_format_t format;
127 unsigned frame_duration;
129 struct timespec start;
130 unsigned frames_sent;
133 static int sbc_get_presets(struct audio_preset *preset, size_t *len);
134 static int sbc_codec_init(struct audio_preset *preset, uint16_t mtu,
136 static int sbc_cleanup(void *codec_data);
137 static int sbc_get_config(void *codec_data,
138 struct audio_input_config *config);
139 static size_t sbc_get_buffer_size(void *codec_data);
140 static void sbc_resume(void *codec_data);
145 int (*get_presets) (struct audio_preset *preset, size_t *len);
147 int (*init) (struct audio_preset *preset, uint16_t mtu,
149 int (*cleanup) (void *codec_data);
150 int (*get_config) (void *codec_data,
151 struct audio_input_config *config);
152 size_t (*get_buffer_size) (void *codec_data);
153 void (*resume) (void *codec_data);
154 ssize_t (*write_data) (void *codec_data, const void *buffer,
158 static const struct audio_codec audio_codecs[] = {
160 .type = A2DP_CODEC_SBC,
162 .get_presets = sbc_get_presets,
164 .init = sbc_codec_init,
165 .cleanup = sbc_cleanup,
166 .get_config = sbc_get_config,
167 .get_buffer_size = sbc_get_buffer_size,
168 .resume = sbc_resume,
172 #define NUM_CODECS (sizeof(audio_codecs) / sizeof(audio_codecs[0]))
174 #define MAX_AUDIO_ENDPOINTS NUM_CODECS
176 struct audio_endpoint {
178 const struct audio_codec *codec;
183 static struct audio_endpoint audio_endpoints[MAX_AUDIO_ENDPOINTS];
186 AUDIO_A2DP_STATE_NONE,
187 AUDIO_A2DP_STATE_STANDBY,
188 AUDIO_A2DP_STATE_SUSPENDED,
189 AUDIO_A2DP_STATE_STARTED
192 struct a2dp_stream_out {
193 struct audio_stream_out stream;
195 struct audio_endpoint *ep;
196 enum a2dp_state_t audio_state;
197 struct audio_input_config cfg;
200 struct a2dp_audio_dev {
201 struct audio_hw_device dev;
202 struct a2dp_stream_out *out;
205 static const a2dp_sbc_t sbc_presets[] = {
207 .frequency = SBC_SAMPLING_FREQ_44100 | SBC_SAMPLING_FREQ_48000,
208 .channel_mode = SBC_CHANNEL_MODE_MONO |
209 SBC_CHANNEL_MODE_DUAL_CHANNEL |
210 SBC_CHANNEL_MODE_STEREO |
211 SBC_CHANNEL_MODE_JOINT_STEREO,
212 .subbands = SBC_SUBBANDS_4 | SBC_SUBBANDS_8,
213 .allocation_method = SBC_ALLOCATION_SNR |
214 SBC_ALLOCATION_LOUDNESS,
215 .block_length = SBC_BLOCK_LENGTH_4 | SBC_BLOCK_LENGTH_8 |
216 SBC_BLOCK_LENGTH_12 | SBC_BLOCK_LENGTH_16,
217 .min_bitpool = MIN_BITPOOL,
218 .max_bitpool = MAX_BITPOOL
221 .frequency = SBC_SAMPLING_FREQ_44100,
222 .channel_mode = SBC_CHANNEL_MODE_JOINT_STEREO,
223 .subbands = SBC_SUBBANDS_8,
224 .allocation_method = SBC_ALLOCATION_LOUDNESS,
225 .block_length = SBC_BLOCK_LENGTH_16,
226 .min_bitpool = MIN_BITPOOL,
227 .max_bitpool = MAX_BITPOOL
230 .frequency = SBC_SAMPLING_FREQ_48000,
231 .channel_mode = SBC_CHANNEL_MODE_JOINT_STEREO,
232 .subbands = SBC_SUBBANDS_8,
233 .allocation_method = SBC_ALLOCATION_LOUDNESS,
234 .block_length = SBC_BLOCK_LENGTH_16,
235 .min_bitpool = MIN_BITPOOL,
236 .max_bitpool = MAX_BITPOOL
240 static int sbc_get_presets(struct audio_preset *preset, size_t *len)
245 uint8_t *ptr = (uint8_t *) preset;
246 size_t preset_size = sizeof(*preset) + sizeof(a2dp_sbc_t);
250 count = sizeof(sbc_presets) / sizeof(sbc_presets[0]);
252 for (i = 0; i < count; i++) {
253 preset = (struct audio_preset *) ptr;
255 if (new_len + preset_size > *len)
258 preset->len = sizeof(a2dp_sbc_t);
259 memcpy(preset->data, &sbc_presets[i], preset->len);
261 new_len += preset_size;
270 static void sbc_init_encoder(struct sbc_data *sbc_data)
272 a2dp_sbc_t *in = &sbc_data->sbc;
273 sbc_t *out = &sbc_data->enc;
277 sbc_init_a2dp(out, 0L, in, sizeof(*in));
279 out->endian = SBC_LE;
280 out->bitpool = in->max_bitpool;
283 static int sbc_codec_init(struct audio_preset *preset, uint16_t mtu,
286 struct sbc_data *sbc_data;
287 size_t hdr_len = sizeof(struct media_packet);
289 size_t out_frame_len;
294 if (preset->len != sizeof(a2dp_sbc_t)) {
295 DBG("preset size mismatch");
296 return AUDIO_STATUS_FAILED;
299 sbc_data = calloc(sizeof(struct sbc_data), 1);
301 memcpy(&sbc_data->sbc, preset->data, preset->len);
303 sbc_init_encoder(sbc_data);
305 in_frame_len = sbc_get_codesize(&sbc_data->enc);
306 out_frame_len = sbc_get_frame_length(&sbc_data->enc);
307 num_frames = (mtu - hdr_len) / out_frame_len;
309 sbc_data->in_frame_len = in_frame_len;
310 sbc_data->in_buf_size = num_frames * in_frame_len;
312 sbc_data->out_buf_size = hdr_len + num_frames * out_frame_len;
313 sbc_data->out_buf = calloc(1, sbc_data->out_buf_size);
315 sbc_data->frame_duration = sbc_get_frame_duration(&sbc_data->enc);
317 *codec_data = sbc_data;
319 return AUDIO_STATUS_SUCCESS;
322 static int sbc_cleanup(void *codec_data)
324 struct sbc_data *sbc_data = (struct sbc_data *) codec_data;
328 sbc_finish(&sbc_data->enc);
329 free(sbc_data->out_buf);
332 return AUDIO_STATUS_SUCCESS;
335 static int sbc_get_config(void *codec_data,
336 struct audio_input_config *config)
338 struct sbc_data *sbc_data = (struct sbc_data *) codec_data;
340 switch (sbc_data->sbc.frequency) {
341 case SBC_SAMPLING_FREQ_16000:
342 config->rate = 16000;
344 case SBC_SAMPLING_FREQ_32000:
345 config->rate = 32000;
347 case SBC_SAMPLING_FREQ_44100:
348 config->rate = 44100;
350 case SBC_SAMPLING_FREQ_48000:
351 config->rate = 48000;
354 return AUDIO_STATUS_FAILED;
356 config->channels = sbc_data->sbc.channel_mode == SBC_CHANNEL_MODE_MONO ?
357 AUDIO_CHANNEL_OUT_MONO :
358 AUDIO_CHANNEL_OUT_STEREO;
359 config->format = AUDIO_FORMAT_PCM_16_BIT;
361 return AUDIO_STATUS_SUCCESS;
364 static size_t sbc_get_buffer_size(void *codec_data)
366 struct sbc_data *sbc_data = (struct sbc_data *) codec_data;
370 return sbc_data->in_buf_size;
373 static void sbc_resume(void *codec_data)
375 struct sbc_data *sbc_data = (struct sbc_data *) codec_data;
379 clock_gettime(CLOCK_MONOTONIC, &sbc_data->start);
381 sbc_data->frames_sent = 0;
384 static void audio_ipc_cleanup(void)
387 shutdown(audio_sk, SHUT_RDWR);
392 static int audio_ipc_cmd(uint8_t service_id, uint8_t opcode, uint16_t len,
393 void *param, size_t *rsp_len, void *rsp, int *fd)
399 char cmsgbuf[CMSG_SPACE(sizeof(int))];
401 size_t s_len = sizeof(s);
404 error("audio: Invalid cmd socket passed to audio_ipc_cmd");
408 if (!rsp || !rsp_len) {
409 memset(&s, 0, s_len);
414 memset(&msg, 0, sizeof(msg));
415 memset(&cmd, 0, sizeof(cmd));
417 cmd.service_id = service_id;
421 iv[0].iov_base = &cmd;
422 iv[0].iov_len = sizeof(cmd);
424 iv[1].iov_base = param;
430 pthread_mutex_lock(&sk_mutex);
432 ret = sendmsg(audio_sk, &msg, 0);
434 error("audio: Sending command failed:%s", strerror(errno));
435 pthread_mutex_unlock(&sk_mutex);
439 /* socket was shutdown */
441 error("audio: Command socket closed");
445 memset(&msg, 0, sizeof(msg));
446 memset(&cmd, 0, sizeof(cmd));
448 iv[0].iov_base = &cmd;
449 iv[0].iov_len = sizeof(cmd);
451 iv[1].iov_base = rsp;
452 iv[1].iov_len = *rsp_len;
458 memset(cmsgbuf, 0, sizeof(cmsgbuf));
459 msg.msg_control = cmsgbuf;
460 msg.msg_controllen = sizeof(cmsgbuf);
463 ret = recvmsg(audio_sk, &msg, 0);
465 error("audio: Receiving command response failed:%s",
467 pthread_mutex_unlock(&sk_mutex);
471 pthread_mutex_unlock(&sk_mutex);
473 if (ret < (ssize_t) sizeof(cmd)) {
474 error("audio: Too small response received(%zd bytes)", ret);
478 if (cmd.service_id != service_id) {
479 error("audio: Invalid service id (%u vs %u)", cmd.service_id,
484 if (ret != (ssize_t) (sizeof(cmd) + cmd.len)) {
485 error("audio: Malformed response received(%zd bytes)", ret);
489 if (cmd.opcode != opcode && cmd.opcode != AUDIO_OP_STATUS) {
490 error("audio: Invalid opcode received (%u vs %u)",
495 if (cmd.opcode == AUDIO_OP_STATUS) {
496 struct hal_status *s = rsp;
498 if (sizeof(*s) != cmd.len) {
499 error("audio: Invalid status length");
503 if (s->code == AUDIO_STATUS_SUCCESS) {
504 error("audio: Invalid success status response");
511 /* Receive auxiliary data in msg */
513 struct cmsghdr *cmsg;
517 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
518 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
519 if (cmsg->cmsg_level == SOL_SOCKET
520 && cmsg->cmsg_type == SCM_RIGHTS) {
521 memcpy(fd, CMSG_DATA(cmsg), sizeof(int));
530 return AUDIO_STATUS_SUCCESS;
533 /* Some serious issue happen on IPC - recover */
534 shutdown(audio_sk, SHUT_RDWR);
536 return AUDIO_STATUS_FAILED;
539 static int ipc_open_cmd(const struct audio_codec *codec)
541 uint8_t buf[BLUEZ_AUDIO_MTU];
542 struct audio_cmd_open *cmd = (struct audio_cmd_open *) buf;
543 struct audio_rsp_open rsp;
544 size_t cmd_len = sizeof(buf) - sizeof(*cmd);
545 size_t rsp_len = sizeof(rsp);
550 memcpy(cmd->uuid, a2dp_src_uuid, sizeof(a2dp_src_uuid));
552 cmd->codec = codec->type;
553 cmd->presets = codec->get_presets(cmd->preset, &cmd_len);
555 cmd_len += sizeof(*cmd);
557 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_OPEN, cmd_len, cmd,
558 &rsp_len, &rsp, NULL);
560 if (result != AUDIO_STATUS_SUCCESS)
566 static int ipc_close_cmd(uint8_t endpoint_id)
568 struct audio_cmd_close cmd;
573 cmd.id = endpoint_id;
575 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_CLOSE,
576 sizeof(cmd), &cmd, NULL, NULL, NULL);
581 static int ipc_open_stream_cmd(uint8_t endpoint_id, uint16_t *mtu, int *fd,
582 struct audio_preset **caps)
584 char buf[BLUEZ_AUDIO_MTU];
585 struct audio_cmd_open_stream cmd;
586 struct audio_rsp_open_stream *rsp =
587 (struct audio_rsp_open_stream *) &buf;
588 size_t rsp_len = sizeof(buf);
594 return AUDIO_STATUS_FAILED;
596 cmd.id = endpoint_id;
598 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_OPEN_STREAM,
599 sizeof(cmd), &cmd, &rsp_len, rsp, fd);
601 if (result == AUDIO_STATUS_SUCCESS) {
602 size_t buf_len = sizeof(struct audio_preset) +
605 *caps = malloc(buf_len);
606 memcpy(*caps, &rsp->preset, buf_len);
614 static int ipc_close_stream_cmd(uint8_t endpoint_id)
616 struct audio_cmd_close_stream cmd;
621 cmd.id = endpoint_id;
623 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_CLOSE_STREAM,
624 sizeof(cmd), &cmd, NULL, NULL, NULL);
629 static int ipc_resume_stream_cmd(uint8_t endpoint_id)
631 struct audio_cmd_resume_stream cmd;
636 cmd.id = endpoint_id;
638 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_RESUME_STREAM,
639 sizeof(cmd), &cmd, NULL, NULL, NULL);
644 static int ipc_suspend_stream_cmd(uint8_t endpoint_id)
646 struct audio_cmd_suspend_stream cmd;
651 cmd.id = endpoint_id;
653 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_SUSPEND_STREAM,
654 sizeof(cmd), &cmd, NULL, NULL, NULL);
659 static int register_endpoints(void)
661 struct audio_endpoint *ep = &audio_endpoints[0];
664 for (i = 0; i < NUM_CODECS; i++, ep++) {
665 const struct audio_codec *codec = &audio_codecs[i];
667 ep->id = ipc_open_cmd(codec);
670 return AUDIO_STATUS_FAILED;
673 ep->codec_data = NULL;
677 return AUDIO_STATUS_SUCCESS;
680 static void unregister_endpoints(void)
684 for (i = 0; i < MAX_AUDIO_ENDPOINTS; i++) {
685 struct audio_endpoint *ep = &audio_endpoints[i];
688 ipc_close_cmd(ep->id);
689 memset(ep, 0, sizeof(*ep));
694 static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
697 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
699 /* We can auto-start only from standby */
700 if (out->audio_state == AUDIO_A2DP_STATE_STANDBY) {
701 DBG("stream in standby, auto-start");
703 if (ipc_resume_stream_cmd(out->ep->id) != AUDIO_STATUS_SUCCESS)
706 out->ep->codec->resume(out->ep->codec_data);
708 out->audio_state = AUDIO_A2DP_STATE_STARTED;
711 if (out->audio_state != AUDIO_A2DP_STATE_STARTED) {
712 DBG("stream not started");
716 /* TODO: encode data using codec */
721 static uint32_t out_get_sample_rate(const struct audio_stream *stream)
723 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
727 return out->cfg.rate;
730 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
732 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
736 if (rate != out->cfg.rate) {
737 DBG("cannot set sample rate to %d", rate);
744 static size_t out_get_buffer_size(const struct audio_stream *stream)
746 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
750 return out->ep->codec->get_buffer_size(out->ep->codec_data);
753 static uint32_t out_get_channels(const struct audio_stream *stream)
755 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
759 return out->cfg.channels;
762 static audio_format_t out_get_format(const struct audio_stream *stream)
764 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
768 return out->cfg.format;
771 static int out_set_format(struct audio_stream *stream, audio_format_t format)
777 static int out_standby(struct audio_stream *stream)
779 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
783 if (out->audio_state == AUDIO_A2DP_STATE_STARTED) {
784 if (ipc_suspend_stream_cmd(out->ep->id) != AUDIO_STATUS_SUCCESS)
786 out->audio_state = AUDIO_A2DP_STATE_STANDBY;
792 static int out_dump(const struct audio_stream *stream, int fd)
798 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
800 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
804 bool enter_suspend = false;
805 bool exit_suspend = false;
809 str = strdup(kvpairs);
810 kvpair = strtok_r(str, ";", &saveptr);
812 for (; kvpair && *kvpair; kvpair = strtok_r(NULL, ";", &saveptr)) {
815 keyval = strchr(kvpair, '=');
822 if (!strcmp(kvpair, "closing")) {
823 if (!strcmp(keyval, "true"))
824 out->audio_state = AUDIO_A2DP_STATE_NONE;
825 } else if (!strcmp(kvpair, "A2dpSuspended")) {
826 if (!strcmp(keyval, "true"))
827 enter_suspend = true;
835 if (enter_suspend && out->audio_state == AUDIO_A2DP_STATE_STARTED) {
836 if (ipc_suspend_stream_cmd(out->ep->id) != AUDIO_STATUS_SUCCESS)
838 out->audio_state = AUDIO_A2DP_STATE_SUSPENDED;
841 if (exit_suspend && out->audio_state == AUDIO_A2DP_STATE_SUSPENDED)
842 out->audio_state = AUDIO_A2DP_STATE_STANDBY;
847 static char *out_get_parameters(const struct audio_stream *stream,
854 static uint32_t out_get_latency(const struct audio_stream_out *stream)
860 static int out_set_volume(struct audio_stream_out *stream, float left,
864 /* volume controlled in audioflinger mixer (digital) */
868 static int out_get_render_position(const struct audio_stream_out *stream,
869 uint32_t *dsp_frames)
875 static int out_add_audio_effect(const struct audio_stream *stream,
876 effect_handle_t effect)
882 static int out_remove_audio_effect(const struct audio_stream *stream,
883 effect_handle_t effect)
889 static uint32_t in_get_sample_rate(const struct audio_stream *stream)
895 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
901 static size_t in_get_buffer_size(const struct audio_stream *stream)
907 static uint32_t in_get_channels(const struct audio_stream *stream)
913 static audio_format_t in_get_format(const struct audio_stream *stream)
919 static int in_set_format(struct audio_stream *stream, audio_format_t format)
925 static int in_standby(struct audio_stream *stream)
931 static int in_dump(const struct audio_stream *stream, int fd)
937 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
943 static char *in_get_parameters(const struct audio_stream *stream,
950 static int in_set_gain(struct audio_stream_in *stream, float gain)
956 static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
963 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
969 static int in_add_audio_effect(const struct audio_stream *stream,
970 effect_handle_t effect)
976 static int in_remove_audio_effect(const struct audio_stream *stream,
977 effect_handle_t effect)
983 static int audio_open_output_stream(struct audio_hw_device *dev,
984 audio_io_handle_t handle,
985 audio_devices_t devices,
986 audio_output_flags_t flags,
987 struct audio_config *config,
988 struct audio_stream_out **stream_out)
991 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
992 struct a2dp_stream_out *out;
993 struct audio_preset *preset;
994 const struct audio_codec *codec;
998 out = calloc(1, sizeof(struct a2dp_stream_out));
1004 out->stream.common.get_sample_rate = out_get_sample_rate;
1005 out->stream.common.set_sample_rate = out_set_sample_rate;
1006 out->stream.common.get_buffer_size = out_get_buffer_size;
1007 out->stream.common.get_channels = out_get_channels;
1008 out->stream.common.get_format = out_get_format;
1009 out->stream.common.set_format = out_set_format;
1010 out->stream.common.standby = out_standby;
1011 out->stream.common.dump = out_dump;
1012 out->stream.common.set_parameters = out_set_parameters;
1013 out->stream.common.get_parameters = out_get_parameters;
1014 out->stream.common.add_audio_effect = out_add_audio_effect;
1015 out->stream.common.remove_audio_effect = out_remove_audio_effect;
1016 out->stream.get_latency = out_get_latency;
1017 out->stream.set_volume = out_set_volume;
1018 out->stream.write = out_write;
1019 out->stream.get_render_position = out_get_render_position;
1021 /* TODO: for now we always use endpoint 0 */
1022 out->ep = &audio_endpoints[0];
1024 if (ipc_open_stream_cmd(out->ep->id, &mtu, &fd, &preset) !=
1025 AUDIO_STATUS_SUCCESS)
1028 if (!preset || fd < 0)
1033 codec = out->ep->codec;
1035 codec->init(preset, mtu, &out->ep->codec_data);
1036 codec->get_config(out->ep->codec_data, &out->cfg);
1038 DBG("rate=%d channels=%d format=%d", out->cfg.rate,
1039 out->cfg.channels, out->cfg.format);
1043 *stream_out = &out->stream;
1044 a2dp_dev->out = out;
1046 out->audio_state = AUDIO_A2DP_STATE_STANDBY;
1056 static void audio_close_output_stream(struct audio_hw_device *dev,
1057 struct audio_stream_out *stream)
1059 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
1060 struct audio_endpoint *ep = a2dp_dev->out->ep;
1064 ipc_close_stream_cmd(ep->id);
1071 ep->codec->cleanup(ep->codec_data);
1072 ep->codec_data = NULL;
1075 a2dp_dev->out = NULL;
1078 static int audio_set_parameters(struct audio_hw_device *dev,
1079 const char *kvpairs)
1081 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
1082 struct a2dp_stream_out *out = a2dp_dev->out;
1089 return out->stream.common.set_parameters((struct audio_stream *) out,
1093 static char *audio_get_parameters(const struct audio_hw_device *dev,
1100 static int audio_init_check(const struct audio_hw_device *dev)
1106 static int audio_set_voice_volume(struct audio_hw_device *dev, float volume)
1112 static int audio_set_master_volume(struct audio_hw_device *dev, float volume)
1118 static int audio_set_mode(struct audio_hw_device *dev, int mode)
1124 static int audio_set_mic_mute(struct audio_hw_device *dev, bool state)
1130 static int audio_get_mic_mute(const struct audio_hw_device *dev, bool *state)
1136 static size_t audio_get_input_buffer_size(const struct audio_hw_device *dev,
1137 const struct audio_config *config)
1143 static int audio_open_input_stream(struct audio_hw_device *dev,
1144 audio_io_handle_t handle,
1145 audio_devices_t devices,
1146 struct audio_config *config,
1147 struct audio_stream_in **stream_in)
1149 struct audio_stream_in *in;
1153 in = calloc(1, sizeof(struct audio_stream_in));
1157 in->common.get_sample_rate = in_get_sample_rate;
1158 in->common.set_sample_rate = in_set_sample_rate;
1159 in->common.get_buffer_size = in_get_buffer_size;
1160 in->common.get_channels = in_get_channels;
1161 in->common.get_format = in_get_format;
1162 in->common.set_format = in_set_format;
1163 in->common.standby = in_standby;
1164 in->common.dump = in_dump;
1165 in->common.set_parameters = in_set_parameters;
1166 in->common.get_parameters = in_get_parameters;
1167 in->common.add_audio_effect = in_add_audio_effect;
1168 in->common.remove_audio_effect = in_remove_audio_effect;
1169 in->set_gain = in_set_gain;
1171 in->get_input_frames_lost = in_get_input_frames_lost;
1178 static void audio_close_input_stream(struct audio_hw_device *dev,
1179 struct audio_stream_in *stream_in)
1185 static int audio_dump(const audio_hw_device_t *device, int fd)
1191 static int audio_close(hw_device_t *device)
1193 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *)device;
1197 pthread_mutex_lock(&close_mutex);
1198 audio_ipc_cleanup();
1199 close_thread = true;
1200 pthread_mutex_unlock(&close_mutex);
1202 pthread_join(ipc_th, NULL);
1211 static void *ipc_handler(void *data)
1219 DBG("Waiting for connection ...");
1220 audio_sk = accept(listen_sk, NULL, NULL);
1223 error("audio: Failed to accept socket: %d (%s)", err,
1228 DBG("Audio IPC: Connected");
1230 if (register_endpoints() != AUDIO_STATUS_SUCCESS) {
1231 error("audio: Failed to register endpoints");
1233 unregister_endpoints();
1235 shutdown(audio_sk, SHUT_RDWR);
1239 memset(&pfd, 0, sizeof(pfd));
1241 pfd.events = POLLHUP | POLLERR | POLLNVAL;
1243 /* Check if socket is still alive. Empty while loop.*/
1244 while (poll(&pfd, 1, -1) < 0 && errno == EINTR);
1246 if (pfd.revents & (POLLHUP | POLLERR | POLLNVAL)) {
1247 info("Audio HAL: Socket closed");
1251 /*Check if audio_dev is closed */
1252 pthread_mutex_lock(&close_mutex);
1253 done = close_thread;
1254 close_thread = false;
1255 pthread_mutex_unlock(&close_mutex);
1258 unregister_endpoints();
1260 info("Closing Audio IPC thread");
1264 static int audio_ipc_init(void)
1266 struct sockaddr_un addr;
1272 sk = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
1275 error("audio: Failed to create socket: %d (%s)", err,
1280 memset(&addr, 0, sizeof(addr));
1281 addr.sun_family = AF_UNIX;
1283 memcpy(addr.sun_path, BLUEZ_AUDIO_SK_PATH,
1284 sizeof(BLUEZ_AUDIO_SK_PATH));
1286 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1288 error("audio: Failed to bind socket: %d (%s)", err,
1293 if (listen(sk, 1) < 0) {
1295 error("audio: Failed to listen on the socket: %d (%s)", err,
1302 err = pthread_create(&ipc_th, NULL, ipc_handler, NULL);
1306 error("audio: Failed to start Audio IPC thread: %d (%s)",
1307 err, strerror(err));
1318 static int audio_open(const hw_module_t *module, const char *name,
1319 hw_device_t **device)
1321 struct a2dp_audio_dev *a2dp_dev;
1326 if (strcmp(name, AUDIO_HARDWARE_INTERFACE)) {
1327 error("audio: interface %s not matching [%s]", name,
1328 AUDIO_HARDWARE_INTERFACE);
1332 err = audio_ipc_init();
1336 a2dp_dev = calloc(1, sizeof(struct a2dp_audio_dev));
1340 a2dp_dev->dev.common.tag = HARDWARE_DEVICE_TAG;
1341 a2dp_dev->dev.common.version = AUDIO_DEVICE_API_VERSION_CURRENT;
1342 a2dp_dev->dev.common.module = (struct hw_module_t *) module;
1343 a2dp_dev->dev.common.close = audio_close;
1345 a2dp_dev->dev.init_check = audio_init_check;
1346 a2dp_dev->dev.set_voice_volume = audio_set_voice_volume;
1347 a2dp_dev->dev.set_master_volume = audio_set_master_volume;
1348 a2dp_dev->dev.set_mode = audio_set_mode;
1349 a2dp_dev->dev.set_mic_mute = audio_set_mic_mute;
1350 a2dp_dev->dev.get_mic_mute = audio_get_mic_mute;
1351 a2dp_dev->dev.set_parameters = audio_set_parameters;
1352 a2dp_dev->dev.get_parameters = audio_get_parameters;
1353 a2dp_dev->dev.get_input_buffer_size = audio_get_input_buffer_size;
1354 a2dp_dev->dev.open_output_stream = audio_open_output_stream;
1355 a2dp_dev->dev.close_output_stream = audio_close_output_stream;
1356 a2dp_dev->dev.open_input_stream = audio_open_input_stream;
1357 a2dp_dev->dev.close_input_stream = audio_close_input_stream;
1358 a2dp_dev->dev.dump = audio_dump;
1360 /* Note that &a2dp_dev->dev.common is the same pointer as a2dp_dev.
1361 * This results from the structure of following structs:a2dp_audio_dev,
1362 * audio_hw_device. We will rely on this later in the code.*/
1363 *device = &a2dp_dev->dev.common;
1368 static struct hw_module_methods_t hal_module_methods = {
1372 struct audio_module HAL_MODULE_INFO_SYM = {
1374 .tag = HARDWARE_MODULE_TAG,
1377 .id = AUDIO_HARDWARE_MODULE_ID,
1378 .name = "A2DP Bluez HW HAL",
1379 .author = "Intel Corporation",
1380 .methods = &hal_module_methods,