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, struct audio_input_config *config);
156 static size_t sbc_get_buffer_size(void *codec_data);
157 static size_t sbc_get_mediapacket_duration(void *codec_data);
158 static void sbc_resume(void *codec_data);
159 static ssize_t sbc_write_data(void *codec_data, const void *buffer,
160 size_t bytes, int fd);
165 int (*get_presets) (struct audio_preset *preset, size_t *len);
167 int (*init) (struct audio_preset *preset, uint16_t mtu,
169 int (*cleanup) (void *codec_data);
170 int (*get_config) (void *codec_data,
171 struct audio_input_config *config);
172 size_t (*get_buffer_size) (void *codec_data);
173 size_t (*get_mediapacket_duration) (void *codec_data);
174 void (*resume) (void *codec_data);
175 ssize_t (*write_data) (void *codec_data, const void *buffer,
176 size_t bytes, int fd);
179 static const struct audio_codec audio_codecs[] = {
181 .type = A2DP_CODEC_SBC,
183 .get_presets = sbc_get_presets,
185 .init = sbc_codec_init,
186 .cleanup = sbc_cleanup,
187 .get_config = sbc_get_config,
188 .get_buffer_size = sbc_get_buffer_size,
189 .get_mediapacket_duration = sbc_get_mediapacket_duration,
190 .resume = sbc_resume,
191 .write_data = sbc_write_data,
195 #define NUM_CODECS (sizeof(audio_codecs) / sizeof(audio_codecs[0]))
197 #define MAX_AUDIO_ENDPOINTS NUM_CODECS
199 struct audio_endpoint {
201 const struct audio_codec *codec;
206 static struct audio_endpoint audio_endpoints[MAX_AUDIO_ENDPOINTS];
209 AUDIO_A2DP_STATE_NONE,
210 AUDIO_A2DP_STATE_STANDBY,
211 AUDIO_A2DP_STATE_SUSPENDED,
212 AUDIO_A2DP_STATE_STARTED
215 struct a2dp_stream_out {
216 struct audio_stream_out stream;
218 struct audio_endpoint *ep;
219 enum a2dp_state_t audio_state;
220 struct audio_input_config cfg;
223 struct a2dp_audio_dev {
224 struct audio_hw_device dev;
225 struct a2dp_stream_out *out;
228 static const a2dp_sbc_t sbc_presets[] = {
230 .frequency = SBC_SAMPLING_FREQ_44100 | SBC_SAMPLING_FREQ_48000,
231 .channel_mode = SBC_CHANNEL_MODE_MONO |
232 SBC_CHANNEL_MODE_DUAL_CHANNEL |
233 SBC_CHANNEL_MODE_STEREO |
234 SBC_CHANNEL_MODE_JOINT_STEREO,
235 .subbands = SBC_SUBBANDS_4 | SBC_SUBBANDS_8,
236 .allocation_method = SBC_ALLOCATION_SNR |
237 SBC_ALLOCATION_LOUDNESS,
238 .block_length = SBC_BLOCK_LENGTH_4 | SBC_BLOCK_LENGTH_8 |
239 SBC_BLOCK_LENGTH_12 | SBC_BLOCK_LENGTH_16,
240 .min_bitpool = MIN_BITPOOL,
241 .max_bitpool = MAX_BITPOOL
244 .frequency = SBC_SAMPLING_FREQ_44100,
245 .channel_mode = SBC_CHANNEL_MODE_JOINT_STEREO,
246 .subbands = SBC_SUBBANDS_8,
247 .allocation_method = SBC_ALLOCATION_LOUDNESS,
248 .block_length = SBC_BLOCK_LENGTH_16,
249 .min_bitpool = MIN_BITPOOL,
250 .max_bitpool = MAX_BITPOOL
253 .frequency = SBC_SAMPLING_FREQ_48000,
254 .channel_mode = SBC_CHANNEL_MODE_JOINT_STEREO,
255 .subbands = SBC_SUBBANDS_8,
256 .allocation_method = SBC_ALLOCATION_LOUDNESS,
257 .block_length = SBC_BLOCK_LENGTH_16,
258 .min_bitpool = MIN_BITPOOL,
259 .max_bitpool = MAX_BITPOOL
263 static int sbc_get_presets(struct audio_preset *preset, size_t *len)
268 uint8_t *ptr = (uint8_t *) preset;
269 size_t preset_size = sizeof(*preset) + sizeof(a2dp_sbc_t);
273 count = sizeof(sbc_presets) / sizeof(sbc_presets[0]);
275 for (i = 0; i < count; i++) {
276 preset = (struct audio_preset *) ptr;
278 if (new_len + preset_size > *len)
281 preset->len = sizeof(a2dp_sbc_t);
282 memcpy(preset->data, &sbc_presets[i], preset->len);
284 new_len += preset_size;
293 static void sbc_init_encoder(struct sbc_data *sbc_data)
295 a2dp_sbc_t *in = &sbc_data->sbc;
296 sbc_t *out = &sbc_data->enc;
300 sbc_init_a2dp(out, 0L, in, sizeof(*in));
302 out->endian = SBC_LE;
303 out->bitpool = in->max_bitpool;
306 static int sbc_codec_init(struct audio_preset *preset, uint16_t mtu,
309 struct sbc_data *sbc_data;
310 size_t hdr_len = sizeof(struct media_packet);
312 size_t out_frame_len;
317 if (preset->len != sizeof(a2dp_sbc_t)) {
318 DBG("preset size mismatch");
319 return AUDIO_STATUS_FAILED;
322 sbc_data = calloc(sizeof(struct sbc_data), 1);
324 memcpy(&sbc_data->sbc, preset->data, preset->len);
326 sbc_init_encoder(sbc_data);
328 in_frame_len = sbc_get_codesize(&sbc_data->enc);
329 out_frame_len = sbc_get_frame_length(&sbc_data->enc);
330 num_frames = (mtu - hdr_len) / out_frame_len;
332 sbc_data->in_frame_len = in_frame_len;
333 sbc_data->in_buf_size = num_frames * in_frame_len;
335 sbc_data->out_buf_size = hdr_len + num_frames * out_frame_len;
336 sbc_data->out_buf = calloc(1, sbc_data->out_buf_size);
338 sbc_data->frame_duration = sbc_get_frame_duration(&sbc_data->enc);
339 sbc_data->frames_per_packet = num_frames;
341 *codec_data = sbc_data;
343 return AUDIO_STATUS_SUCCESS;
346 static int sbc_cleanup(void *codec_data)
348 struct sbc_data *sbc_data = (struct sbc_data *) codec_data;
352 sbc_finish(&sbc_data->enc);
353 free(sbc_data->out_buf);
356 return AUDIO_STATUS_SUCCESS;
359 static int sbc_get_config(void *codec_data, struct audio_input_config *config)
361 struct sbc_data *sbc_data = (struct sbc_data *) codec_data;
363 switch (sbc_data->sbc.frequency) {
364 case SBC_SAMPLING_FREQ_16000:
365 config->rate = 16000;
367 case SBC_SAMPLING_FREQ_32000:
368 config->rate = 32000;
370 case SBC_SAMPLING_FREQ_44100:
371 config->rate = 44100;
373 case SBC_SAMPLING_FREQ_48000:
374 config->rate = 48000;
377 return AUDIO_STATUS_FAILED;
379 config->channels = sbc_data->sbc.channel_mode == SBC_CHANNEL_MODE_MONO ?
380 AUDIO_CHANNEL_OUT_MONO :
381 AUDIO_CHANNEL_OUT_STEREO;
382 config->format = AUDIO_FORMAT_PCM_16_BIT;
384 return AUDIO_STATUS_SUCCESS;
387 static size_t sbc_get_buffer_size(void *codec_data)
389 struct sbc_data *sbc_data = (struct sbc_data *) codec_data;
393 return sbc_data->in_buf_size;
396 static size_t sbc_get_mediapacket_duration(void *codec_data)
398 struct sbc_data *sbc_data = (struct sbc_data *) codec_data;
402 return sbc_data->frame_duration * sbc_data->frames_per_packet;
405 static void sbc_resume(void *codec_data)
407 struct sbc_data *sbc_data = (struct sbc_data *) codec_data;
411 clock_gettime(CLOCK_MONOTONIC, &sbc_data->start);
413 sbc_data->frames_sent = 0;
416 static ssize_t sbc_write_data(void *codec_data, const void *buffer,
417 size_t bytes, int fd)
419 struct sbc_data *sbc_data = (struct sbc_data *) codec_data;
422 struct media_packet *mp = (struct media_packet *) sbc_data->out_buf;
423 size_t free_space = sbc_data->out_buf_size - sizeof(*mp);
425 struct timespec diff;
426 unsigned expected_frames;
431 mp->hdr.sequence_number = htons(sbc_data->seq++);
432 mp->hdr.ssrc = htonl(1);
433 mp->payload.frame_count = 0;
435 while (bytes - consumed >= sbc_data->in_frame_len) {
438 ret = sbc_encode(&sbc_data->enc, buffer + consumed,
439 sbc_data->in_frame_len,
440 mp->data + encoded, free_space,
444 DBG("failed to encode block");
448 mp->payload.frame_count++;
452 free_space -= written;
455 ret = write(fd, mp, sizeof(*mp) + encoded);
458 DBG("error writing data: %d (%s)", err, strerror(err));
461 if (consumed != bytes || free_space != 0) {
462 /* we should encode all input data and fill output buffer
463 * if we did not, something went wrong but we can't really
464 * handle this so this is just sanity check
466 DBG("some data were not encoded");
469 sbc_data->frames_sent += mp->payload.frame_count;
471 clock_gettime(CLOCK_MONOTONIC, &cur);
472 timespec_diff(&cur, &sbc_data->start, &diff);
473 expected_frames = (diff.tv_sec * 1000000 + diff.tv_nsec / 1000) /
474 sbc_data->frame_duration;
476 /* AudioFlinger does not seem to provide any *working* API to provide
477 * data in some interval and will just send another buffer as soon as
478 * we process current one. To prevent overflowing L2CAP socket, we need
479 * to introduce some artificial delay here base on how many audio frames
480 * were sent so far, i.e. if we're not lagging behind audio stream, we
481 * can sleep for duration of single media packet.
483 if (sbc_data->frames_sent >= expected_frames)
484 usleep(sbc_data->frame_duration * mp->payload.frame_count);
486 /* we always assume that all data was processed and sent */
490 static void audio_ipc_cleanup(void)
493 shutdown(audio_sk, SHUT_RDWR);
498 static int audio_ipc_cmd(uint8_t service_id, uint8_t opcode, uint16_t len,
499 void *param, size_t *rsp_len, void *rsp, int *fd)
505 char cmsgbuf[CMSG_SPACE(sizeof(int))];
507 size_t s_len = sizeof(s);
510 error("audio: Invalid cmd socket passed to audio_ipc_cmd");
514 if (!rsp || !rsp_len) {
515 memset(&s, 0, s_len);
520 memset(&msg, 0, sizeof(msg));
521 memset(&cmd, 0, sizeof(cmd));
523 cmd.service_id = service_id;
527 iv[0].iov_base = &cmd;
528 iv[0].iov_len = sizeof(cmd);
530 iv[1].iov_base = param;
536 pthread_mutex_lock(&sk_mutex);
538 ret = sendmsg(audio_sk, &msg, 0);
540 error("audio: Sending command failed:%s", strerror(errno));
541 pthread_mutex_unlock(&sk_mutex);
545 /* socket was shutdown */
547 error("audio: Command socket closed");
551 memset(&msg, 0, sizeof(msg));
552 memset(&cmd, 0, sizeof(cmd));
554 iv[0].iov_base = &cmd;
555 iv[0].iov_len = sizeof(cmd);
557 iv[1].iov_base = rsp;
558 iv[1].iov_len = *rsp_len;
564 memset(cmsgbuf, 0, sizeof(cmsgbuf));
565 msg.msg_control = cmsgbuf;
566 msg.msg_controllen = sizeof(cmsgbuf);
569 ret = recvmsg(audio_sk, &msg, 0);
571 error("audio: Receiving command response failed:%s",
573 pthread_mutex_unlock(&sk_mutex);
577 pthread_mutex_unlock(&sk_mutex);
579 if (ret < (ssize_t) sizeof(cmd)) {
580 error("audio: Too small response received(%zd bytes)", ret);
584 if (cmd.service_id != service_id) {
585 error("audio: Invalid service id (%u vs %u)", cmd.service_id,
590 if (ret != (ssize_t) (sizeof(cmd) + cmd.len)) {
591 error("audio: Malformed response received(%zd bytes)", ret);
595 if (cmd.opcode != opcode && cmd.opcode != AUDIO_OP_STATUS) {
596 error("audio: Invalid opcode received (%u vs %u)",
601 if (cmd.opcode == AUDIO_OP_STATUS) {
602 struct hal_status *s = rsp;
604 if (sizeof(*s) != cmd.len) {
605 error("audio: Invalid status length");
609 if (s->code == AUDIO_STATUS_SUCCESS) {
610 error("audio: Invalid success status response");
617 /* Receive auxiliary data in msg */
619 struct cmsghdr *cmsg;
623 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
624 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
625 if (cmsg->cmsg_level == SOL_SOCKET
626 && cmsg->cmsg_type == SCM_RIGHTS) {
627 memcpy(fd, CMSG_DATA(cmsg), sizeof(int));
636 return AUDIO_STATUS_SUCCESS;
639 /* Some serious issue happen on IPC - recover */
640 shutdown(audio_sk, SHUT_RDWR);
642 return AUDIO_STATUS_FAILED;
645 static int ipc_open_cmd(const struct audio_codec *codec)
647 uint8_t buf[BLUEZ_AUDIO_MTU];
648 struct audio_cmd_open *cmd = (struct audio_cmd_open *) buf;
649 struct audio_rsp_open rsp;
650 size_t cmd_len = sizeof(buf) - sizeof(*cmd);
651 size_t rsp_len = sizeof(rsp);
656 memcpy(cmd->uuid, a2dp_src_uuid, sizeof(a2dp_src_uuid));
658 cmd->codec = codec->type;
659 cmd->presets = codec->get_presets(cmd->preset, &cmd_len);
661 cmd_len += sizeof(*cmd);
663 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_OPEN, cmd_len, cmd,
664 &rsp_len, &rsp, NULL);
666 if (result != AUDIO_STATUS_SUCCESS)
672 static int ipc_close_cmd(uint8_t endpoint_id)
674 struct audio_cmd_close cmd;
679 cmd.id = endpoint_id;
681 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_CLOSE,
682 sizeof(cmd), &cmd, NULL, NULL, NULL);
687 static int ipc_open_stream_cmd(uint8_t endpoint_id, uint16_t *mtu, int *fd,
688 struct audio_preset **caps)
690 char buf[BLUEZ_AUDIO_MTU];
691 struct audio_cmd_open_stream cmd;
692 struct audio_rsp_open_stream *rsp =
693 (struct audio_rsp_open_stream *) &buf;
694 size_t rsp_len = sizeof(buf);
700 return AUDIO_STATUS_FAILED;
702 cmd.id = endpoint_id;
704 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_OPEN_STREAM,
705 sizeof(cmd), &cmd, &rsp_len, rsp, fd);
707 if (result == AUDIO_STATUS_SUCCESS) {
708 size_t buf_len = sizeof(struct audio_preset) +
711 *caps = malloc(buf_len);
712 memcpy(*caps, &rsp->preset, buf_len);
720 static int ipc_close_stream_cmd(uint8_t endpoint_id)
722 struct audio_cmd_close_stream cmd;
727 cmd.id = endpoint_id;
729 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_CLOSE_STREAM,
730 sizeof(cmd), &cmd, NULL, NULL, NULL);
735 static int ipc_resume_stream_cmd(uint8_t endpoint_id)
737 struct audio_cmd_resume_stream cmd;
742 cmd.id = endpoint_id;
744 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_RESUME_STREAM,
745 sizeof(cmd), &cmd, NULL, NULL, NULL);
750 static int ipc_suspend_stream_cmd(uint8_t endpoint_id)
752 struct audio_cmd_suspend_stream cmd;
757 cmd.id = endpoint_id;
759 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_SUSPEND_STREAM,
760 sizeof(cmd), &cmd, NULL, NULL, NULL);
765 static int register_endpoints(void)
767 struct audio_endpoint *ep = &audio_endpoints[0];
770 for (i = 0; i < NUM_CODECS; i++, ep++) {
771 const struct audio_codec *codec = &audio_codecs[i];
773 ep->id = ipc_open_cmd(codec);
776 return AUDIO_STATUS_FAILED;
779 ep->codec_data = NULL;
783 return AUDIO_STATUS_SUCCESS;
786 static void unregister_endpoints(void)
790 for (i = 0; i < MAX_AUDIO_ENDPOINTS; i++) {
791 struct audio_endpoint *ep = &audio_endpoints[i];
794 ipc_close_cmd(ep->id);
795 memset(ep, 0, sizeof(*ep));
800 static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
803 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
805 /* We can auto-start only from standby */
806 if (out->audio_state == AUDIO_A2DP_STATE_STANDBY) {
807 DBG("stream in standby, auto-start");
809 if (ipc_resume_stream_cmd(out->ep->id) != AUDIO_STATUS_SUCCESS)
812 out->ep->codec->resume(out->ep->codec_data);
814 out->audio_state = AUDIO_A2DP_STATE_STARTED;
817 if (out->audio_state != AUDIO_A2DP_STATE_STARTED) {
818 DBG("stream not started");
822 if (out->ep->fd < 0) {
827 return out->ep->codec->write_data(out->ep->codec_data, buffer,
831 static uint32_t out_get_sample_rate(const struct audio_stream *stream)
833 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
837 return out->cfg.rate;
840 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
842 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
846 if (rate != out->cfg.rate) {
847 DBG("cannot set sample rate to %d", rate);
854 static size_t out_get_buffer_size(const struct audio_stream *stream)
856 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
860 return out->ep->codec->get_buffer_size(out->ep->codec_data);
863 static uint32_t out_get_channels(const struct audio_stream *stream)
865 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
869 return out->cfg.channels;
872 static audio_format_t out_get_format(const struct audio_stream *stream)
874 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
878 return out->cfg.format;
881 static int out_set_format(struct audio_stream *stream, audio_format_t format)
887 static int out_standby(struct audio_stream *stream)
889 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
893 if (out->audio_state == AUDIO_A2DP_STATE_STARTED) {
894 if (ipc_suspend_stream_cmd(out->ep->id) != AUDIO_STATUS_SUCCESS)
896 out->audio_state = AUDIO_A2DP_STATE_STANDBY;
902 static int out_dump(const struct audio_stream *stream, int fd)
908 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
910 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
914 bool enter_suspend = false;
915 bool exit_suspend = false;
919 str = strdup(kvpairs);
920 kvpair = strtok_r(str, ";", &saveptr);
922 for (; kvpair && *kvpair; kvpair = strtok_r(NULL, ";", &saveptr)) {
925 keyval = strchr(kvpair, '=');
932 if (!strcmp(kvpair, "closing")) {
933 if (!strcmp(keyval, "true"))
934 out->audio_state = AUDIO_A2DP_STATE_NONE;
935 } else if (!strcmp(kvpair, "A2dpSuspended")) {
936 if (!strcmp(keyval, "true"))
937 enter_suspend = true;
945 if (enter_suspend && out->audio_state == AUDIO_A2DP_STATE_STARTED) {
946 if (ipc_suspend_stream_cmd(out->ep->id) != AUDIO_STATUS_SUCCESS)
948 out->audio_state = AUDIO_A2DP_STATE_SUSPENDED;
951 if (exit_suspend && out->audio_state == AUDIO_A2DP_STATE_SUSPENDED)
952 out->audio_state = AUDIO_A2DP_STATE_STANDBY;
957 static char *out_get_parameters(const struct audio_stream *stream,
964 static uint32_t out_get_latency(const struct audio_stream_out *stream)
966 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
967 struct audio_endpoint *ep = out->ep;
972 pkt_duration = ep->codec->get_mediapacket_duration(ep->codec_data);
974 return FIXED_A2DP_PLAYBACK_LATENCY_MS + pkt_duration / 1000;
977 static int out_set_volume(struct audio_stream_out *stream, float left,
981 /* volume controlled in audioflinger mixer (digital) */
985 static int out_get_render_position(const struct audio_stream_out *stream,
986 uint32_t *dsp_frames)
992 static int out_add_audio_effect(const struct audio_stream *stream,
993 effect_handle_t effect)
999 static int out_remove_audio_effect(const struct audio_stream *stream,
1000 effect_handle_t effect)
1006 static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1012 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1018 static size_t in_get_buffer_size(const struct audio_stream *stream)
1024 static uint32_t in_get_channels(const struct audio_stream *stream)
1030 static audio_format_t in_get_format(const struct audio_stream *stream)
1036 static int in_set_format(struct audio_stream *stream, audio_format_t format)
1042 static int in_standby(struct audio_stream *stream)
1048 static int in_dump(const struct audio_stream *stream, int fd)
1054 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1060 static char *in_get_parameters(const struct audio_stream *stream,
1067 static int in_set_gain(struct audio_stream_in *stream, float gain)
1073 static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1080 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1086 static int in_add_audio_effect(const struct audio_stream *stream,
1087 effect_handle_t effect)
1093 static int in_remove_audio_effect(const struct audio_stream *stream,
1094 effect_handle_t effect)
1100 static int audio_open_output_stream(struct audio_hw_device *dev,
1101 audio_io_handle_t handle,
1102 audio_devices_t devices,
1103 audio_output_flags_t flags,
1104 struct audio_config *config,
1105 struct audio_stream_out **stream_out)
1108 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
1109 struct a2dp_stream_out *out;
1110 struct audio_preset *preset;
1111 const struct audio_codec *codec;
1115 out = calloc(1, sizeof(struct a2dp_stream_out));
1121 out->stream.common.get_sample_rate = out_get_sample_rate;
1122 out->stream.common.set_sample_rate = out_set_sample_rate;
1123 out->stream.common.get_buffer_size = out_get_buffer_size;
1124 out->stream.common.get_channels = out_get_channels;
1125 out->stream.common.get_format = out_get_format;
1126 out->stream.common.set_format = out_set_format;
1127 out->stream.common.standby = out_standby;
1128 out->stream.common.dump = out_dump;
1129 out->stream.common.set_parameters = out_set_parameters;
1130 out->stream.common.get_parameters = out_get_parameters;
1131 out->stream.common.add_audio_effect = out_add_audio_effect;
1132 out->stream.common.remove_audio_effect = out_remove_audio_effect;
1133 out->stream.get_latency = out_get_latency;
1134 out->stream.set_volume = out_set_volume;
1135 out->stream.write = out_write;
1136 out->stream.get_render_position = out_get_render_position;
1138 /* TODO: for now we always use endpoint 0 */
1139 out->ep = &audio_endpoints[0];
1141 if (ipc_open_stream_cmd(out->ep->id, &mtu, &fd, &preset) !=
1142 AUDIO_STATUS_SUCCESS)
1145 if (!preset || fd < 0)
1150 codec = out->ep->codec;
1152 codec->init(preset, mtu, &out->ep->codec_data);
1153 codec->get_config(out->ep->codec_data, &out->cfg);
1155 DBG("rate=%d channels=%d format=%d", out->cfg.rate,
1156 out->cfg.channels, out->cfg.format);
1160 *stream_out = &out->stream;
1161 a2dp_dev->out = out;
1163 out->audio_state = AUDIO_A2DP_STATE_STANDBY;
1173 static void audio_close_output_stream(struct audio_hw_device *dev,
1174 struct audio_stream_out *stream)
1176 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
1177 struct audio_endpoint *ep = a2dp_dev->out->ep;
1181 ipc_close_stream_cmd(ep->id);
1188 ep->codec->cleanup(ep->codec_data);
1189 ep->codec_data = NULL;
1192 a2dp_dev->out = NULL;
1195 static int audio_set_parameters(struct audio_hw_device *dev,
1196 const char *kvpairs)
1198 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
1199 struct a2dp_stream_out *out = a2dp_dev->out;
1206 return out->stream.common.set_parameters((struct audio_stream *) out,
1210 static char *audio_get_parameters(const struct audio_hw_device *dev,
1217 static int audio_init_check(const struct audio_hw_device *dev)
1223 static int audio_set_voice_volume(struct audio_hw_device *dev, float volume)
1229 static int audio_set_master_volume(struct audio_hw_device *dev, float volume)
1235 static int audio_set_mode(struct audio_hw_device *dev, int mode)
1241 static int audio_set_mic_mute(struct audio_hw_device *dev, bool state)
1247 static int audio_get_mic_mute(const struct audio_hw_device *dev, bool *state)
1253 static size_t audio_get_input_buffer_size(const struct audio_hw_device *dev,
1254 const struct audio_config *config)
1260 static int audio_open_input_stream(struct audio_hw_device *dev,
1261 audio_io_handle_t handle,
1262 audio_devices_t devices,
1263 struct audio_config *config,
1264 struct audio_stream_in **stream_in)
1266 struct audio_stream_in *in;
1270 in = calloc(1, sizeof(struct audio_stream_in));
1274 in->common.get_sample_rate = in_get_sample_rate;
1275 in->common.set_sample_rate = in_set_sample_rate;
1276 in->common.get_buffer_size = in_get_buffer_size;
1277 in->common.get_channels = in_get_channels;
1278 in->common.get_format = in_get_format;
1279 in->common.set_format = in_set_format;
1280 in->common.standby = in_standby;
1281 in->common.dump = in_dump;
1282 in->common.set_parameters = in_set_parameters;
1283 in->common.get_parameters = in_get_parameters;
1284 in->common.add_audio_effect = in_add_audio_effect;
1285 in->common.remove_audio_effect = in_remove_audio_effect;
1286 in->set_gain = in_set_gain;
1288 in->get_input_frames_lost = in_get_input_frames_lost;
1295 static void audio_close_input_stream(struct audio_hw_device *dev,
1296 struct audio_stream_in *stream_in)
1302 static int audio_dump(const audio_hw_device_t *device, int fd)
1308 static int audio_close(hw_device_t *device)
1310 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *)device;
1314 pthread_mutex_lock(&close_mutex);
1315 audio_ipc_cleanup();
1316 close_thread = true;
1317 pthread_mutex_unlock(&close_mutex);
1319 pthread_join(ipc_th, NULL);
1328 static void *ipc_handler(void *data)
1336 DBG("Waiting for connection ...");
1337 audio_sk = accept(listen_sk, NULL, NULL);
1340 error("audio: Failed to accept socket: %d (%s)", err,
1345 DBG("Audio IPC: Connected");
1347 if (register_endpoints() != AUDIO_STATUS_SUCCESS) {
1348 error("audio: Failed to register endpoints");
1350 unregister_endpoints();
1352 shutdown(audio_sk, SHUT_RDWR);
1356 memset(&pfd, 0, sizeof(pfd));
1358 pfd.events = POLLHUP | POLLERR | POLLNVAL;
1360 /* Check if socket is still alive. Empty while loop.*/
1361 while (poll(&pfd, 1, -1) < 0 && errno == EINTR);
1363 if (pfd.revents & (POLLHUP | POLLERR | POLLNVAL)) {
1364 info("Audio HAL: Socket closed");
1368 /*Check if audio_dev is closed */
1369 pthread_mutex_lock(&close_mutex);
1370 done = close_thread;
1371 close_thread = false;
1372 pthread_mutex_unlock(&close_mutex);
1375 unregister_endpoints();
1377 info("Closing Audio IPC thread");
1381 static int audio_ipc_init(void)
1383 struct sockaddr_un addr;
1389 sk = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
1392 error("audio: Failed to create socket: %d (%s)", err,
1397 memset(&addr, 0, sizeof(addr));
1398 addr.sun_family = AF_UNIX;
1400 memcpy(addr.sun_path, BLUEZ_AUDIO_SK_PATH,
1401 sizeof(BLUEZ_AUDIO_SK_PATH));
1403 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1405 error("audio: Failed to bind socket: %d (%s)", err,
1410 if (listen(sk, 1) < 0) {
1412 error("audio: Failed to listen on the socket: %d (%s)", err,
1419 err = pthread_create(&ipc_th, NULL, ipc_handler, NULL);
1423 error("audio: Failed to start Audio IPC thread: %d (%s)",
1424 err, strerror(err));
1435 static int audio_open(const hw_module_t *module, const char *name,
1436 hw_device_t **device)
1438 struct a2dp_audio_dev *a2dp_dev;
1443 if (strcmp(name, AUDIO_HARDWARE_INTERFACE)) {
1444 error("audio: interface %s not matching [%s]", name,
1445 AUDIO_HARDWARE_INTERFACE);
1449 err = audio_ipc_init();
1453 a2dp_dev = calloc(1, sizeof(struct a2dp_audio_dev));
1457 a2dp_dev->dev.common.tag = HARDWARE_DEVICE_TAG;
1458 a2dp_dev->dev.common.version = AUDIO_DEVICE_API_VERSION_CURRENT;
1459 a2dp_dev->dev.common.module = (struct hw_module_t *) module;
1460 a2dp_dev->dev.common.close = audio_close;
1462 a2dp_dev->dev.init_check = audio_init_check;
1463 a2dp_dev->dev.set_voice_volume = audio_set_voice_volume;
1464 a2dp_dev->dev.set_master_volume = audio_set_master_volume;
1465 a2dp_dev->dev.set_mode = audio_set_mode;
1466 a2dp_dev->dev.set_mic_mute = audio_set_mic_mute;
1467 a2dp_dev->dev.get_mic_mute = audio_get_mic_mute;
1468 a2dp_dev->dev.set_parameters = audio_set_parameters;
1469 a2dp_dev->dev.get_parameters = audio_get_parameters;
1470 a2dp_dev->dev.get_input_buffer_size = audio_get_input_buffer_size;
1471 a2dp_dev->dev.open_output_stream = audio_open_output_stream;
1472 a2dp_dev->dev.close_output_stream = audio_close_output_stream;
1473 a2dp_dev->dev.open_input_stream = audio_open_input_stream;
1474 a2dp_dev->dev.close_input_stream = audio_close_input_stream;
1475 a2dp_dev->dev.dump = audio_dump;
1477 /* Note that &a2dp_dev->dev.common is the same pointer as a2dp_dev.
1478 * This results from the structure of following structs:a2dp_audio_dev,
1479 * audio_hw_device. We will rely on this later in the code.*/
1480 *device = &a2dp_dev->dev.common;
1485 static struct hw_module_methods_t hal_module_methods = {
1489 struct audio_module HAL_MODULE_INFO_SYM = {
1491 .tag = HARDWARE_MODULE_TAG,
1494 .id = AUDIO_HARDWARE_MODULE_ID,
1495 .name = "A2DP Bluez HW HAL",
1496 .author = "Intel Corporation",
1497 .methods = &hal_module_methods,