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>
27 #include <arpa/inet.h>
29 #include <hardware/audio.h>
30 #include <hardware/hardware.h>
34 #include "audio-msg.h"
37 #include "../profiles/audio/a2dp-codecs.h"
39 #define FIXED_A2DP_PLAYBACK_LATENCY_MS 25
41 static const uint8_t a2dp_src_uuid[] = {
42 0x00, 0x00, 0x11, 0x0a, 0x00, 0x00, 0x10, 0x00,
43 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb };
45 static int listen_sk = -1;
46 static int audio_sk = -1;
47 static bool close_thread = false;
49 static pthread_t ipc_th = 0;
50 static pthread_mutex_t close_mutex = PTHREAD_MUTEX_INITIALIZER;
51 static pthread_mutex_t sk_mutex = PTHREAD_MUTEX_INITIALIZER;
53 #if __BYTE_ORDER == __LITTLE_ENDIAN
64 uint16_t sequence_number;
68 } __attribute__ ((packed));
71 unsigned frame_count:4;
73 unsigned is_last_fragment:1;
74 unsigned is_first_fragment:1;
75 unsigned is_fragmented:1;
76 } __attribute__ ((packed));
78 #elif __BYTE_ORDER == __BIG_ENDIAN
89 uint16_t sequence_number;
93 } __attribute__ ((packed));
96 unsigned is_fragmented:1;
97 unsigned is_first_fragment:1;
98 unsigned is_last_fragment:1;
100 unsigned frame_count:4;
101 } __attribute__ ((packed));
104 #error "Unknown byte order"
107 struct media_packet {
108 struct rtp_header hdr;
109 struct rtp_payload payload;
113 struct audio_input_config {
116 audio_format_t format;
130 unsigned frame_duration;
131 unsigned frames_per_packet;
133 struct timespec start;
134 unsigned frames_sent;
139 static inline void timespec_diff(struct timespec *a, struct timespec *b,
140 struct timespec *res)
142 res->tv_sec = a->tv_sec - b->tv_sec;
143 res->tv_nsec = a->tv_nsec - b->tv_nsec;
145 if (res->tv_nsec < 0) {
147 res->tv_nsec += 1000000000; /* 1sec */
151 static int sbc_get_presets(struct audio_preset *preset, size_t *len);
152 static int sbc_codec_init(struct audio_preset *preset, uint16_t mtu,
154 static int sbc_cleanup(void *codec_data);
155 static int sbc_get_config(void *codec_data,
156 struct audio_input_config *config);
157 static size_t sbc_get_buffer_size(void *codec_data);
158 static size_t sbc_get_mediapacket_duration(void *codec_data);
159 static void sbc_resume(void *codec_data);
160 static ssize_t sbc_write_data(void *codec_data, const void *buffer,
161 size_t bytes, int fd);
166 int (*get_presets) (struct audio_preset *preset, size_t *len);
168 int (*init) (struct audio_preset *preset, uint16_t mtu,
170 int (*cleanup) (void *codec_data);
171 int (*get_config) (void *codec_data,
172 struct audio_input_config *config);
173 size_t (*get_buffer_size) (void *codec_data);
174 size_t (*get_mediapacket_duration) (void *codec_data);
175 void (*resume) (void *codec_data);
176 ssize_t (*write_data) (void *codec_data, const void *buffer,
177 size_t bytes, int fd);
180 static const struct audio_codec audio_codecs[] = {
182 .type = A2DP_CODEC_SBC,
184 .get_presets = sbc_get_presets,
186 .init = sbc_codec_init,
187 .cleanup = sbc_cleanup,
188 .get_config = sbc_get_config,
189 .get_buffer_size = sbc_get_buffer_size,
190 .get_mediapacket_duration = sbc_get_mediapacket_duration,
191 .resume = sbc_resume,
192 .write_data = sbc_write_data,
196 #define NUM_CODECS (sizeof(audio_codecs) / sizeof(audio_codecs[0]))
198 #define MAX_AUDIO_ENDPOINTS NUM_CODECS
200 struct audio_endpoint {
202 const struct audio_codec *codec;
207 static struct audio_endpoint audio_endpoints[MAX_AUDIO_ENDPOINTS];
210 AUDIO_A2DP_STATE_NONE,
211 AUDIO_A2DP_STATE_STANDBY,
212 AUDIO_A2DP_STATE_SUSPENDED,
213 AUDIO_A2DP_STATE_STARTED
216 struct a2dp_stream_out {
217 struct audio_stream_out stream;
219 struct audio_endpoint *ep;
220 enum a2dp_state_t audio_state;
221 struct audio_input_config cfg;
224 struct a2dp_audio_dev {
225 struct audio_hw_device dev;
226 struct a2dp_stream_out *out;
229 static const a2dp_sbc_t sbc_presets[] = {
231 .frequency = SBC_SAMPLING_FREQ_44100 | SBC_SAMPLING_FREQ_48000,
232 .channel_mode = SBC_CHANNEL_MODE_MONO |
233 SBC_CHANNEL_MODE_DUAL_CHANNEL |
234 SBC_CHANNEL_MODE_STEREO |
235 SBC_CHANNEL_MODE_JOINT_STEREO,
236 .subbands = SBC_SUBBANDS_4 | SBC_SUBBANDS_8,
237 .allocation_method = SBC_ALLOCATION_SNR |
238 SBC_ALLOCATION_LOUDNESS,
239 .block_length = SBC_BLOCK_LENGTH_4 | SBC_BLOCK_LENGTH_8 |
240 SBC_BLOCK_LENGTH_12 | SBC_BLOCK_LENGTH_16,
241 .min_bitpool = MIN_BITPOOL,
242 .max_bitpool = MAX_BITPOOL
245 .frequency = SBC_SAMPLING_FREQ_44100,
246 .channel_mode = SBC_CHANNEL_MODE_JOINT_STEREO,
247 .subbands = SBC_SUBBANDS_8,
248 .allocation_method = SBC_ALLOCATION_LOUDNESS,
249 .block_length = SBC_BLOCK_LENGTH_16,
250 .min_bitpool = MIN_BITPOOL,
251 .max_bitpool = MAX_BITPOOL
254 .frequency = SBC_SAMPLING_FREQ_48000,
255 .channel_mode = SBC_CHANNEL_MODE_JOINT_STEREO,
256 .subbands = SBC_SUBBANDS_8,
257 .allocation_method = SBC_ALLOCATION_LOUDNESS,
258 .block_length = SBC_BLOCK_LENGTH_16,
259 .min_bitpool = MIN_BITPOOL,
260 .max_bitpool = MAX_BITPOOL
264 static int sbc_get_presets(struct audio_preset *preset, size_t *len)
269 uint8_t *ptr = (uint8_t *) preset;
270 size_t preset_size = sizeof(*preset) + sizeof(a2dp_sbc_t);
274 count = sizeof(sbc_presets) / sizeof(sbc_presets[0]);
276 for (i = 0; i < count; i++) {
277 preset = (struct audio_preset *) ptr;
279 if (new_len + preset_size > *len)
282 preset->len = sizeof(a2dp_sbc_t);
283 memcpy(preset->data, &sbc_presets[i], preset->len);
285 new_len += preset_size;
294 static void sbc_init_encoder(struct sbc_data *sbc_data)
296 a2dp_sbc_t *in = &sbc_data->sbc;
297 sbc_t *out = &sbc_data->enc;
301 sbc_init_a2dp(out, 0L, in, sizeof(*in));
303 out->endian = SBC_LE;
304 out->bitpool = in->max_bitpool;
307 static int sbc_codec_init(struct audio_preset *preset, uint16_t mtu,
310 struct sbc_data *sbc_data;
311 size_t hdr_len = sizeof(struct media_packet);
313 size_t out_frame_len;
318 if (preset->len != sizeof(a2dp_sbc_t)) {
319 DBG("preset size mismatch");
320 return AUDIO_STATUS_FAILED;
323 sbc_data = calloc(sizeof(struct sbc_data), 1);
325 memcpy(&sbc_data->sbc, preset->data, preset->len);
327 sbc_init_encoder(sbc_data);
329 in_frame_len = sbc_get_codesize(&sbc_data->enc);
330 out_frame_len = sbc_get_frame_length(&sbc_data->enc);
331 num_frames = (mtu - hdr_len) / out_frame_len;
333 sbc_data->in_frame_len = in_frame_len;
334 sbc_data->in_buf_size = num_frames * in_frame_len;
336 sbc_data->out_buf_size = hdr_len + num_frames * out_frame_len;
337 sbc_data->out_buf = calloc(1, sbc_data->out_buf_size);
339 sbc_data->frame_duration = sbc_get_frame_duration(&sbc_data->enc);
340 sbc_data->frames_per_packet = num_frames;
342 *codec_data = sbc_data;
344 return AUDIO_STATUS_SUCCESS;
347 static int sbc_cleanup(void *codec_data)
349 struct sbc_data *sbc_data = (struct sbc_data *) codec_data;
353 sbc_finish(&sbc_data->enc);
354 free(sbc_data->out_buf);
357 return AUDIO_STATUS_SUCCESS;
360 static int sbc_get_config(void *codec_data,
361 struct audio_input_config *config)
363 struct sbc_data *sbc_data = (struct sbc_data *) codec_data;
365 switch (sbc_data->sbc.frequency) {
366 case SBC_SAMPLING_FREQ_16000:
367 config->rate = 16000;
369 case SBC_SAMPLING_FREQ_32000:
370 config->rate = 32000;
372 case SBC_SAMPLING_FREQ_44100:
373 config->rate = 44100;
375 case SBC_SAMPLING_FREQ_48000:
376 config->rate = 48000;
379 return AUDIO_STATUS_FAILED;
381 config->channels = sbc_data->sbc.channel_mode == SBC_CHANNEL_MODE_MONO ?
382 AUDIO_CHANNEL_OUT_MONO :
383 AUDIO_CHANNEL_OUT_STEREO;
384 config->format = AUDIO_FORMAT_PCM_16_BIT;
386 return AUDIO_STATUS_SUCCESS;
389 static size_t sbc_get_buffer_size(void *codec_data)
391 struct sbc_data *sbc_data = (struct sbc_data *) codec_data;
395 return sbc_data->in_buf_size;
398 static size_t sbc_get_mediapacket_duration(void *codec_data)
400 struct sbc_data *sbc_data = (struct sbc_data *) codec_data;
404 return sbc_data->frame_duration * sbc_data->frames_per_packet;
407 static void sbc_resume(void *codec_data)
409 struct sbc_data *sbc_data = (struct sbc_data *) codec_data;
413 clock_gettime(CLOCK_MONOTONIC, &sbc_data->start);
415 sbc_data->frames_sent = 0;
418 static ssize_t sbc_write_data(void *codec_data, const void *buffer,
419 size_t bytes, int fd)
421 struct sbc_data *sbc_data = (struct sbc_data *) codec_data;
424 struct media_packet *mp = (struct media_packet *) sbc_data->out_buf;
425 size_t free_space = sbc_data->out_buf_size - sizeof(*mp);
427 struct timespec diff;
428 unsigned expected_frames;
433 mp->hdr.sequence_number = htons(sbc_data->seq++);
434 mp->hdr.ssrc = htonl(1);
435 mp->payload.frame_count = 0;
437 while (bytes - consumed >= sbc_data->in_frame_len) {
440 ret = sbc_encode(&sbc_data->enc, buffer + consumed,
441 sbc_data->in_frame_len,
442 mp->data + encoded, free_space,
446 DBG("failed to encode block");
450 mp->payload.frame_count++;
454 free_space -= written;
457 ret = write(fd, mp, sizeof(*mp) + encoded);
460 DBG("error writing data: %d (%s)", err, strerror(err));
463 if (consumed != bytes || free_space != 0) {
464 /* we should encode all input data and fill output buffer
465 * if we did not, something went wrong but we can't really
466 * handle this so this is just sanity check
468 DBG("some data were not encoded");
471 sbc_data->frames_sent += mp->payload.frame_count;
473 clock_gettime(CLOCK_MONOTONIC, &cur);
474 timespec_diff(&cur, &sbc_data->start, &diff);
475 expected_frames = (diff.tv_sec * 1000000 + diff.tv_nsec / 1000) /
476 sbc_data->frame_duration;
478 /* AudioFlinger does not seem to provide any *working* API to provide
479 * data in some interval and will just send another buffer as soon as
480 * we process current one. To prevent overflowing L2CAP socket, we need
481 * to introduce some artificial delay here base on how many audio frames
482 * were sent so far, i.e. if we're not lagging behind audio stream, we
483 * can sleep for duration of single media packet.
485 if (sbc_data->frames_sent >= expected_frames)
486 usleep(sbc_data->frame_duration * mp->payload.frame_count);
488 /* we always assume that all data was processed and sent */
492 static void audio_ipc_cleanup(void)
495 shutdown(audio_sk, SHUT_RDWR);
500 static int audio_ipc_cmd(uint8_t service_id, uint8_t opcode, uint16_t len,
501 void *param, size_t *rsp_len, void *rsp, int *fd)
507 char cmsgbuf[CMSG_SPACE(sizeof(int))];
509 size_t s_len = sizeof(s);
512 error("audio: Invalid cmd socket passed to audio_ipc_cmd");
516 if (!rsp || !rsp_len) {
517 memset(&s, 0, s_len);
522 memset(&msg, 0, sizeof(msg));
523 memset(&cmd, 0, sizeof(cmd));
525 cmd.service_id = service_id;
529 iv[0].iov_base = &cmd;
530 iv[0].iov_len = sizeof(cmd);
532 iv[1].iov_base = param;
538 pthread_mutex_lock(&sk_mutex);
540 ret = sendmsg(audio_sk, &msg, 0);
542 error("audio: Sending command failed:%s", strerror(errno));
543 pthread_mutex_unlock(&sk_mutex);
547 /* socket was shutdown */
549 error("audio: Command socket closed");
553 memset(&msg, 0, sizeof(msg));
554 memset(&cmd, 0, sizeof(cmd));
556 iv[0].iov_base = &cmd;
557 iv[0].iov_len = sizeof(cmd);
559 iv[1].iov_base = rsp;
560 iv[1].iov_len = *rsp_len;
566 memset(cmsgbuf, 0, sizeof(cmsgbuf));
567 msg.msg_control = cmsgbuf;
568 msg.msg_controllen = sizeof(cmsgbuf);
571 ret = recvmsg(audio_sk, &msg, 0);
573 error("audio: Receiving command response failed:%s",
575 pthread_mutex_unlock(&sk_mutex);
579 pthread_mutex_unlock(&sk_mutex);
581 if (ret < (ssize_t) sizeof(cmd)) {
582 error("audio: Too small response received(%zd bytes)", ret);
586 if (cmd.service_id != service_id) {
587 error("audio: Invalid service id (%u vs %u)", cmd.service_id,
592 if (ret != (ssize_t) (sizeof(cmd) + cmd.len)) {
593 error("audio: Malformed response received(%zd bytes)", ret);
597 if (cmd.opcode != opcode && cmd.opcode != AUDIO_OP_STATUS) {
598 error("audio: Invalid opcode received (%u vs %u)",
603 if (cmd.opcode == AUDIO_OP_STATUS) {
604 struct hal_status *s = rsp;
606 if (sizeof(*s) != cmd.len) {
607 error("audio: Invalid status length");
611 if (s->code == AUDIO_STATUS_SUCCESS) {
612 error("audio: Invalid success status response");
619 /* Receive auxiliary data in msg */
621 struct cmsghdr *cmsg;
625 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
626 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
627 if (cmsg->cmsg_level == SOL_SOCKET
628 && cmsg->cmsg_type == SCM_RIGHTS) {
629 memcpy(fd, CMSG_DATA(cmsg), sizeof(int));
638 return AUDIO_STATUS_SUCCESS;
641 /* Some serious issue happen on IPC - recover */
642 shutdown(audio_sk, SHUT_RDWR);
644 return AUDIO_STATUS_FAILED;
647 static int ipc_open_cmd(const struct audio_codec *codec)
649 uint8_t buf[BLUEZ_AUDIO_MTU];
650 struct audio_cmd_open *cmd = (struct audio_cmd_open *) buf;
651 struct audio_rsp_open rsp;
652 size_t cmd_len = sizeof(buf) - sizeof(*cmd);
653 size_t rsp_len = sizeof(rsp);
658 memcpy(cmd->uuid, a2dp_src_uuid, sizeof(a2dp_src_uuid));
660 cmd->codec = codec->type;
661 cmd->presets = codec->get_presets(cmd->preset, &cmd_len);
663 cmd_len += sizeof(*cmd);
665 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_OPEN, cmd_len, cmd,
666 &rsp_len, &rsp, NULL);
668 if (result != AUDIO_STATUS_SUCCESS)
674 static int ipc_close_cmd(uint8_t endpoint_id)
676 struct audio_cmd_close cmd;
681 cmd.id = endpoint_id;
683 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_CLOSE,
684 sizeof(cmd), &cmd, NULL, NULL, NULL);
689 static int ipc_open_stream_cmd(uint8_t endpoint_id, uint16_t *mtu, int *fd,
690 struct audio_preset **caps)
692 char buf[BLUEZ_AUDIO_MTU];
693 struct audio_cmd_open_stream cmd;
694 struct audio_rsp_open_stream *rsp =
695 (struct audio_rsp_open_stream *) &buf;
696 size_t rsp_len = sizeof(buf);
702 return AUDIO_STATUS_FAILED;
704 cmd.id = endpoint_id;
706 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_OPEN_STREAM,
707 sizeof(cmd), &cmd, &rsp_len, rsp, fd);
709 if (result == AUDIO_STATUS_SUCCESS) {
710 size_t buf_len = sizeof(struct audio_preset) +
713 *caps = malloc(buf_len);
714 memcpy(*caps, &rsp->preset, buf_len);
722 static int ipc_close_stream_cmd(uint8_t endpoint_id)
724 struct audio_cmd_close_stream cmd;
729 cmd.id = endpoint_id;
731 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_CLOSE_STREAM,
732 sizeof(cmd), &cmd, NULL, NULL, NULL);
737 static int ipc_resume_stream_cmd(uint8_t endpoint_id)
739 struct audio_cmd_resume_stream cmd;
744 cmd.id = endpoint_id;
746 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_RESUME_STREAM,
747 sizeof(cmd), &cmd, NULL, NULL, NULL);
752 static int ipc_suspend_stream_cmd(uint8_t endpoint_id)
754 struct audio_cmd_suspend_stream cmd;
759 cmd.id = endpoint_id;
761 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_SUSPEND_STREAM,
762 sizeof(cmd), &cmd, NULL, NULL, NULL);
767 static int register_endpoints(void)
769 struct audio_endpoint *ep = &audio_endpoints[0];
772 for (i = 0; i < NUM_CODECS; i++, ep++) {
773 const struct audio_codec *codec = &audio_codecs[i];
775 ep->id = ipc_open_cmd(codec);
778 return AUDIO_STATUS_FAILED;
781 ep->codec_data = NULL;
785 return AUDIO_STATUS_SUCCESS;
788 static void unregister_endpoints(void)
792 for (i = 0; i < MAX_AUDIO_ENDPOINTS; i++) {
793 struct audio_endpoint *ep = &audio_endpoints[i];
796 ipc_close_cmd(ep->id);
797 memset(ep, 0, sizeof(*ep));
802 static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
805 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
807 /* We can auto-start only from standby */
808 if (out->audio_state == AUDIO_A2DP_STATE_STANDBY) {
809 DBG("stream in standby, auto-start");
811 if (ipc_resume_stream_cmd(out->ep->id) != AUDIO_STATUS_SUCCESS)
814 out->ep->codec->resume(out->ep->codec_data);
816 out->audio_state = AUDIO_A2DP_STATE_STARTED;
819 if (out->audio_state != AUDIO_A2DP_STATE_STARTED) {
820 DBG("stream not started");
824 if (out->ep->fd < 0) {
829 return out->ep->codec->write_data(out->ep->codec_data, buffer,
833 static uint32_t out_get_sample_rate(const struct audio_stream *stream)
835 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
839 return out->cfg.rate;
842 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
844 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
848 if (rate != out->cfg.rate) {
849 DBG("cannot set sample rate to %d", rate);
856 static size_t out_get_buffer_size(const struct audio_stream *stream)
858 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
862 return out->ep->codec->get_buffer_size(out->ep->codec_data);
865 static uint32_t out_get_channels(const struct audio_stream *stream)
867 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
871 return out->cfg.channels;
874 static audio_format_t out_get_format(const struct audio_stream *stream)
876 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
880 return out->cfg.format;
883 static int out_set_format(struct audio_stream *stream, audio_format_t format)
889 static int out_standby(struct audio_stream *stream)
891 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
895 if (out->audio_state == AUDIO_A2DP_STATE_STARTED) {
896 if (ipc_suspend_stream_cmd(out->ep->id) != AUDIO_STATUS_SUCCESS)
898 out->audio_state = AUDIO_A2DP_STATE_STANDBY;
904 static int out_dump(const struct audio_stream *stream, int fd)
910 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
912 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
916 bool enter_suspend = false;
917 bool exit_suspend = false;
921 str = strdup(kvpairs);
922 kvpair = strtok_r(str, ";", &saveptr);
924 for (; kvpair && *kvpair; kvpair = strtok_r(NULL, ";", &saveptr)) {
927 keyval = strchr(kvpair, '=');
934 if (!strcmp(kvpair, "closing")) {
935 if (!strcmp(keyval, "true"))
936 out->audio_state = AUDIO_A2DP_STATE_NONE;
937 } else if (!strcmp(kvpair, "A2dpSuspended")) {
938 if (!strcmp(keyval, "true"))
939 enter_suspend = true;
947 if (enter_suspend && out->audio_state == AUDIO_A2DP_STATE_STARTED) {
948 if (ipc_suspend_stream_cmd(out->ep->id) != AUDIO_STATUS_SUCCESS)
950 out->audio_state = AUDIO_A2DP_STATE_SUSPENDED;
953 if (exit_suspend && out->audio_state == AUDIO_A2DP_STATE_SUSPENDED)
954 out->audio_state = AUDIO_A2DP_STATE_STANDBY;
959 static char *out_get_parameters(const struct audio_stream *stream,
966 static uint32_t out_get_latency(const struct audio_stream_out *stream)
968 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
969 struct audio_endpoint *ep = out->ep;
974 pkt_duration = ep->codec->get_mediapacket_duration(ep->codec_data);
976 return FIXED_A2DP_PLAYBACK_LATENCY_MS + pkt_duration / 1000;
979 static int out_set_volume(struct audio_stream_out *stream, float left,
983 /* volume controlled in audioflinger mixer (digital) */
987 static int out_get_render_position(const struct audio_stream_out *stream,
988 uint32_t *dsp_frames)
994 static int out_add_audio_effect(const struct audio_stream *stream,
995 effect_handle_t effect)
1001 static int out_remove_audio_effect(const struct audio_stream *stream,
1002 effect_handle_t effect)
1008 static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1014 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1020 static size_t in_get_buffer_size(const struct audio_stream *stream)
1026 static uint32_t in_get_channels(const struct audio_stream *stream)
1032 static audio_format_t in_get_format(const struct audio_stream *stream)
1038 static int in_set_format(struct audio_stream *stream, audio_format_t format)
1044 static int in_standby(struct audio_stream *stream)
1050 static int in_dump(const struct audio_stream *stream, int fd)
1056 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1062 static char *in_get_parameters(const struct audio_stream *stream,
1069 static int in_set_gain(struct audio_stream_in *stream, float gain)
1075 static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1082 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1088 static int in_add_audio_effect(const struct audio_stream *stream,
1089 effect_handle_t effect)
1095 static int in_remove_audio_effect(const struct audio_stream *stream,
1096 effect_handle_t effect)
1102 static int audio_open_output_stream(struct audio_hw_device *dev,
1103 audio_io_handle_t handle,
1104 audio_devices_t devices,
1105 audio_output_flags_t flags,
1106 struct audio_config *config,
1107 struct audio_stream_out **stream_out)
1110 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
1111 struct a2dp_stream_out *out;
1112 struct audio_preset *preset;
1113 const struct audio_codec *codec;
1117 out = calloc(1, sizeof(struct a2dp_stream_out));
1123 out->stream.common.get_sample_rate = out_get_sample_rate;
1124 out->stream.common.set_sample_rate = out_set_sample_rate;
1125 out->stream.common.get_buffer_size = out_get_buffer_size;
1126 out->stream.common.get_channels = out_get_channels;
1127 out->stream.common.get_format = out_get_format;
1128 out->stream.common.set_format = out_set_format;
1129 out->stream.common.standby = out_standby;
1130 out->stream.common.dump = out_dump;
1131 out->stream.common.set_parameters = out_set_parameters;
1132 out->stream.common.get_parameters = out_get_parameters;
1133 out->stream.common.add_audio_effect = out_add_audio_effect;
1134 out->stream.common.remove_audio_effect = out_remove_audio_effect;
1135 out->stream.get_latency = out_get_latency;
1136 out->stream.set_volume = out_set_volume;
1137 out->stream.write = out_write;
1138 out->stream.get_render_position = out_get_render_position;
1140 /* TODO: for now we always use endpoint 0 */
1141 out->ep = &audio_endpoints[0];
1143 if (ipc_open_stream_cmd(out->ep->id, &mtu, &fd, &preset) !=
1144 AUDIO_STATUS_SUCCESS)
1147 if (!preset || fd < 0)
1152 codec = out->ep->codec;
1154 codec->init(preset, mtu, &out->ep->codec_data);
1155 codec->get_config(out->ep->codec_data, &out->cfg);
1157 DBG("rate=%d channels=%d format=%d", out->cfg.rate,
1158 out->cfg.channels, out->cfg.format);
1162 *stream_out = &out->stream;
1163 a2dp_dev->out = out;
1165 out->audio_state = AUDIO_A2DP_STATE_STANDBY;
1175 static void audio_close_output_stream(struct audio_hw_device *dev,
1176 struct audio_stream_out *stream)
1178 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
1179 struct audio_endpoint *ep = a2dp_dev->out->ep;
1183 ipc_close_stream_cmd(ep->id);
1190 ep->codec->cleanup(ep->codec_data);
1191 ep->codec_data = NULL;
1194 a2dp_dev->out = NULL;
1197 static int audio_set_parameters(struct audio_hw_device *dev,
1198 const char *kvpairs)
1200 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
1201 struct a2dp_stream_out *out = a2dp_dev->out;
1208 return out->stream.common.set_parameters((struct audio_stream *) out,
1212 static char *audio_get_parameters(const struct audio_hw_device *dev,
1219 static int audio_init_check(const struct audio_hw_device *dev)
1225 static int audio_set_voice_volume(struct audio_hw_device *dev, float volume)
1231 static int audio_set_master_volume(struct audio_hw_device *dev, float volume)
1237 static int audio_set_mode(struct audio_hw_device *dev, int mode)
1243 static int audio_set_mic_mute(struct audio_hw_device *dev, bool state)
1249 static int audio_get_mic_mute(const struct audio_hw_device *dev, bool *state)
1255 static size_t audio_get_input_buffer_size(const struct audio_hw_device *dev,
1256 const struct audio_config *config)
1262 static int audio_open_input_stream(struct audio_hw_device *dev,
1263 audio_io_handle_t handle,
1264 audio_devices_t devices,
1265 struct audio_config *config,
1266 struct audio_stream_in **stream_in)
1268 struct audio_stream_in *in;
1272 in = calloc(1, sizeof(struct audio_stream_in));
1276 in->common.get_sample_rate = in_get_sample_rate;
1277 in->common.set_sample_rate = in_set_sample_rate;
1278 in->common.get_buffer_size = in_get_buffer_size;
1279 in->common.get_channels = in_get_channels;
1280 in->common.get_format = in_get_format;
1281 in->common.set_format = in_set_format;
1282 in->common.standby = in_standby;
1283 in->common.dump = in_dump;
1284 in->common.set_parameters = in_set_parameters;
1285 in->common.get_parameters = in_get_parameters;
1286 in->common.add_audio_effect = in_add_audio_effect;
1287 in->common.remove_audio_effect = in_remove_audio_effect;
1288 in->set_gain = in_set_gain;
1290 in->get_input_frames_lost = in_get_input_frames_lost;
1297 static void audio_close_input_stream(struct audio_hw_device *dev,
1298 struct audio_stream_in *stream_in)
1304 static int audio_dump(const audio_hw_device_t *device, int fd)
1310 static int audio_close(hw_device_t *device)
1312 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *)device;
1316 pthread_mutex_lock(&close_mutex);
1317 audio_ipc_cleanup();
1318 close_thread = true;
1319 pthread_mutex_unlock(&close_mutex);
1321 pthread_join(ipc_th, NULL);
1330 static void *ipc_handler(void *data)
1338 DBG("Waiting for connection ...");
1339 audio_sk = accept(listen_sk, NULL, NULL);
1342 error("audio: Failed to accept socket: %d (%s)", err,
1347 DBG("Audio IPC: Connected");
1349 if (register_endpoints() != AUDIO_STATUS_SUCCESS) {
1350 error("audio: Failed to register endpoints");
1352 unregister_endpoints();
1354 shutdown(audio_sk, SHUT_RDWR);
1358 memset(&pfd, 0, sizeof(pfd));
1360 pfd.events = POLLHUP | POLLERR | POLLNVAL;
1362 /* Check if socket is still alive. Empty while loop.*/
1363 while (poll(&pfd, 1, -1) < 0 && errno == EINTR);
1365 if (pfd.revents & (POLLHUP | POLLERR | POLLNVAL)) {
1366 info("Audio HAL: Socket closed");
1370 /*Check if audio_dev is closed */
1371 pthread_mutex_lock(&close_mutex);
1372 done = close_thread;
1373 close_thread = false;
1374 pthread_mutex_unlock(&close_mutex);
1377 unregister_endpoints();
1379 info("Closing Audio IPC thread");
1383 static int audio_ipc_init(void)
1385 struct sockaddr_un addr;
1391 sk = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
1394 error("audio: Failed to create socket: %d (%s)", err,
1399 memset(&addr, 0, sizeof(addr));
1400 addr.sun_family = AF_UNIX;
1402 memcpy(addr.sun_path, BLUEZ_AUDIO_SK_PATH,
1403 sizeof(BLUEZ_AUDIO_SK_PATH));
1405 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1407 error("audio: Failed to bind socket: %d (%s)", err,
1412 if (listen(sk, 1) < 0) {
1414 error("audio: Failed to listen on the socket: %d (%s)", err,
1421 err = pthread_create(&ipc_th, NULL, ipc_handler, NULL);
1425 error("audio: Failed to start Audio IPC thread: %d (%s)",
1426 err, strerror(err));
1437 static int audio_open(const hw_module_t *module, const char *name,
1438 hw_device_t **device)
1440 struct a2dp_audio_dev *a2dp_dev;
1445 if (strcmp(name, AUDIO_HARDWARE_INTERFACE)) {
1446 error("audio: interface %s not matching [%s]", name,
1447 AUDIO_HARDWARE_INTERFACE);
1451 err = audio_ipc_init();
1455 a2dp_dev = calloc(1, sizeof(struct a2dp_audio_dev));
1459 a2dp_dev->dev.common.tag = HARDWARE_DEVICE_TAG;
1460 a2dp_dev->dev.common.version = AUDIO_DEVICE_API_VERSION_CURRENT;
1461 a2dp_dev->dev.common.module = (struct hw_module_t *) module;
1462 a2dp_dev->dev.common.close = audio_close;
1464 a2dp_dev->dev.init_check = audio_init_check;
1465 a2dp_dev->dev.set_voice_volume = audio_set_voice_volume;
1466 a2dp_dev->dev.set_master_volume = audio_set_master_volume;
1467 a2dp_dev->dev.set_mode = audio_set_mode;
1468 a2dp_dev->dev.set_mic_mute = audio_set_mic_mute;
1469 a2dp_dev->dev.get_mic_mute = audio_get_mic_mute;
1470 a2dp_dev->dev.set_parameters = audio_set_parameters;
1471 a2dp_dev->dev.get_parameters = audio_get_parameters;
1472 a2dp_dev->dev.get_input_buffer_size = audio_get_input_buffer_size;
1473 a2dp_dev->dev.open_output_stream = audio_open_output_stream;
1474 a2dp_dev->dev.close_output_stream = audio_close_output_stream;
1475 a2dp_dev->dev.open_input_stream = audio_open_input_stream;
1476 a2dp_dev->dev.close_input_stream = audio_close_input_stream;
1477 a2dp_dev->dev.dump = audio_dump;
1479 /* Note that &a2dp_dev->dev.common is the same pointer as a2dp_dev.
1480 * This results from the structure of following structs:a2dp_audio_dev,
1481 * audio_hw_device. We will rely on this later in the code.*/
1482 *device = &a2dp_dev->dev.common;
1487 static struct hw_module_methods_t hal_module_methods = {
1491 struct audio_module HAL_MODULE_INFO_SYM = {
1493 .tag = HARDWARE_MODULE_TAG,
1496 .id = AUDIO_HARDWARE_MODULE_ID,
1497 .name = "A2DP Bluez HW HAL",
1498 .author = "Intel Corporation",
1499 .methods = &hal_module_methods,