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;
48 static pthread_t ipc_th = 0;
49 static pthread_mutex_t sk_mutex = PTHREAD_MUTEX_INITIALIZER;
51 #if __BYTE_ORDER == __LITTLE_ENDIAN
62 uint16_t sequence_number;
66 } __attribute__ ((packed));
69 unsigned frame_count:4;
71 unsigned is_last_fragment:1;
72 unsigned is_first_fragment:1;
73 unsigned is_fragmented:1;
74 } __attribute__ ((packed));
76 #elif __BYTE_ORDER == __BIG_ENDIAN
87 uint16_t sequence_number;
91 } __attribute__ ((packed));
94 unsigned is_fragmented:1;
95 unsigned is_first_fragment:1;
96 unsigned is_last_fragment:1;
98 unsigned frame_count:4;
99 } __attribute__ ((packed));
102 #error "Unknown byte order"
105 struct media_packet {
106 struct rtp_header hdr;
107 struct rtp_payload payload;
111 struct audio_input_config {
114 audio_format_t format;
128 unsigned frame_duration;
129 unsigned frames_per_packet;
131 struct timespec start;
132 unsigned frames_sent;
137 static inline void timespec_diff(struct timespec *a, struct timespec *b,
138 struct timespec *res)
140 res->tv_sec = a->tv_sec - b->tv_sec;
141 res->tv_nsec = a->tv_nsec - b->tv_nsec;
143 if (res->tv_nsec < 0) {
145 res->tv_nsec += 1000000000; /* 1sec */
149 static int sbc_get_presets(struct audio_preset *preset, size_t *len);
150 static int sbc_codec_init(struct audio_preset *preset, uint16_t mtu,
152 static int sbc_cleanup(void *codec_data);
153 static int sbc_get_config(void *codec_data, struct audio_input_config *config);
154 static size_t sbc_get_buffer_size(void *codec_data);
155 static size_t sbc_get_mediapacket_duration(void *codec_data);
156 static void sbc_resume(void *codec_data);
157 static ssize_t sbc_write_data(void *codec_data, const void *buffer,
158 size_t bytes, int fd);
163 int (*get_presets) (struct audio_preset *preset, size_t *len);
165 int (*init) (struct audio_preset *preset, uint16_t mtu,
167 int (*cleanup) (void *codec_data);
168 int (*get_config) (void *codec_data,
169 struct audio_input_config *config);
170 size_t (*get_buffer_size) (void *codec_data);
171 size_t (*get_mediapacket_duration) (void *codec_data);
172 void (*resume) (void *codec_data);
173 ssize_t (*write_data) (void *codec_data, const void *buffer,
174 size_t bytes, int fd);
177 static const struct audio_codec audio_codecs[] = {
179 .type = A2DP_CODEC_SBC,
181 .get_presets = sbc_get_presets,
183 .init = sbc_codec_init,
184 .cleanup = sbc_cleanup,
185 .get_config = sbc_get_config,
186 .get_buffer_size = sbc_get_buffer_size,
187 .get_mediapacket_duration = sbc_get_mediapacket_duration,
188 .resume = sbc_resume,
189 .write_data = sbc_write_data,
193 #define NUM_CODECS (sizeof(audio_codecs) / sizeof(audio_codecs[0]))
195 #define MAX_AUDIO_ENDPOINTS NUM_CODECS
197 struct audio_endpoint {
199 const struct audio_codec *codec;
204 static struct audio_endpoint audio_endpoints[MAX_AUDIO_ENDPOINTS];
207 AUDIO_A2DP_STATE_NONE,
208 AUDIO_A2DP_STATE_STANDBY,
209 AUDIO_A2DP_STATE_SUSPENDED,
210 AUDIO_A2DP_STATE_STARTED
213 struct a2dp_stream_out {
214 struct audio_stream_out stream;
216 struct audio_endpoint *ep;
217 enum a2dp_state_t audio_state;
218 struct audio_input_config cfg;
221 struct a2dp_audio_dev {
222 struct audio_hw_device dev;
223 struct a2dp_stream_out *out;
226 static const a2dp_sbc_t sbc_presets[] = {
228 .frequency = SBC_SAMPLING_FREQ_44100 | SBC_SAMPLING_FREQ_48000,
229 .channel_mode = SBC_CHANNEL_MODE_MONO |
230 SBC_CHANNEL_MODE_DUAL_CHANNEL |
231 SBC_CHANNEL_MODE_STEREO |
232 SBC_CHANNEL_MODE_JOINT_STEREO,
233 .subbands = SBC_SUBBANDS_4 | SBC_SUBBANDS_8,
234 .allocation_method = SBC_ALLOCATION_SNR |
235 SBC_ALLOCATION_LOUDNESS,
236 .block_length = SBC_BLOCK_LENGTH_4 | SBC_BLOCK_LENGTH_8 |
237 SBC_BLOCK_LENGTH_12 | SBC_BLOCK_LENGTH_16,
238 .min_bitpool = MIN_BITPOOL,
239 .max_bitpool = MAX_BITPOOL
242 .frequency = SBC_SAMPLING_FREQ_44100,
243 .channel_mode = SBC_CHANNEL_MODE_JOINT_STEREO,
244 .subbands = SBC_SUBBANDS_8,
245 .allocation_method = SBC_ALLOCATION_LOUDNESS,
246 .block_length = SBC_BLOCK_LENGTH_16,
247 .min_bitpool = MIN_BITPOOL,
248 .max_bitpool = MAX_BITPOOL
251 .frequency = SBC_SAMPLING_FREQ_48000,
252 .channel_mode = SBC_CHANNEL_MODE_JOINT_STEREO,
253 .subbands = SBC_SUBBANDS_8,
254 .allocation_method = SBC_ALLOCATION_LOUDNESS,
255 .block_length = SBC_BLOCK_LENGTH_16,
256 .min_bitpool = MIN_BITPOOL,
257 .max_bitpool = MAX_BITPOOL
261 static int sbc_get_presets(struct audio_preset *preset, size_t *len)
266 uint8_t *ptr = (uint8_t *) preset;
267 size_t preset_size = sizeof(*preset) + sizeof(a2dp_sbc_t);
269 count = sizeof(sbc_presets) / sizeof(sbc_presets[0]);
271 for (i = 0; i < count; i++) {
272 preset = (struct audio_preset *) ptr;
274 if (new_len + preset_size > *len)
277 preset->len = sizeof(a2dp_sbc_t);
278 memcpy(preset->data, &sbc_presets[i], preset->len);
280 new_len += preset_size;
289 static void sbc_init_encoder(struct sbc_data *sbc_data)
291 a2dp_sbc_t *in = &sbc_data->sbc;
292 sbc_t *out = &sbc_data->enc;
294 sbc_init_a2dp(out, 0L, in, sizeof(*in));
296 out->endian = SBC_LE;
297 out->bitpool = in->max_bitpool;
300 static int sbc_codec_init(struct audio_preset *preset, uint16_t mtu,
303 struct sbc_data *sbc_data;
304 size_t hdr_len = sizeof(struct media_packet);
306 size_t out_frame_len;
309 if (preset->len != sizeof(a2dp_sbc_t)) {
310 error("SBC: preset size mismatch");
311 return AUDIO_STATUS_FAILED;
314 sbc_data = calloc(sizeof(struct sbc_data), 1);
316 memcpy(&sbc_data->sbc, preset->data, preset->len);
318 sbc_init_encoder(sbc_data);
320 in_frame_len = sbc_get_codesize(&sbc_data->enc);
321 out_frame_len = sbc_get_frame_length(&sbc_data->enc);
322 num_frames = (mtu - hdr_len) / out_frame_len;
324 sbc_data->in_frame_len = in_frame_len;
325 sbc_data->in_buf_size = num_frames * in_frame_len;
327 sbc_data->out_buf_size = hdr_len + num_frames * out_frame_len;
328 sbc_data->out_buf = calloc(1, sbc_data->out_buf_size);
330 sbc_data->frame_duration = sbc_get_frame_duration(&sbc_data->enc);
331 sbc_data->frames_per_packet = num_frames;
333 *codec_data = sbc_data;
335 return AUDIO_STATUS_SUCCESS;
338 static int sbc_cleanup(void *codec_data)
340 struct sbc_data *sbc_data = (struct sbc_data *) codec_data;
342 sbc_finish(&sbc_data->enc);
343 free(sbc_data->out_buf);
346 return AUDIO_STATUS_SUCCESS;
349 static int sbc_get_config(void *codec_data, struct audio_input_config *config)
351 struct sbc_data *sbc_data = (struct sbc_data *) codec_data;
353 switch (sbc_data->sbc.frequency) {
354 case SBC_SAMPLING_FREQ_16000:
355 config->rate = 16000;
357 case SBC_SAMPLING_FREQ_32000:
358 config->rate = 32000;
360 case SBC_SAMPLING_FREQ_44100:
361 config->rate = 44100;
363 case SBC_SAMPLING_FREQ_48000:
364 config->rate = 48000;
367 return AUDIO_STATUS_FAILED;
369 config->channels = sbc_data->sbc.channel_mode == SBC_CHANNEL_MODE_MONO ?
370 AUDIO_CHANNEL_OUT_MONO :
371 AUDIO_CHANNEL_OUT_STEREO;
372 config->format = AUDIO_FORMAT_PCM_16_BIT;
374 return AUDIO_STATUS_SUCCESS;
377 static size_t sbc_get_buffer_size(void *codec_data)
379 struct sbc_data *sbc_data = (struct sbc_data *) codec_data;
381 return sbc_data->in_buf_size;
384 static size_t sbc_get_mediapacket_duration(void *codec_data)
386 struct sbc_data *sbc_data = (struct sbc_data *) codec_data;
388 return sbc_data->frame_duration * sbc_data->frames_per_packet;
391 static void sbc_resume(void *codec_data)
393 struct sbc_data *sbc_data = (struct sbc_data *) codec_data;
397 clock_gettime(CLOCK_MONOTONIC, &sbc_data->start);
399 sbc_data->frames_sent = 0;
402 static void write_media_packet(int fd, struct sbc_data *sbc_data,
403 struct media_packet *mp, size_t data_len)
406 struct timespec diff;
407 unsigned expected_frames;
410 ret = write(fd, mp, sizeof(*mp) + data_len);
413 error("SBC: failed to write data: %d (%s)", err, strerror(err));
416 sbc_data->frames_sent += mp->payload.frame_count;
418 clock_gettime(CLOCK_MONOTONIC, &cur);
419 timespec_diff(&cur, &sbc_data->start, &diff);
420 expected_frames = (diff.tv_sec * 1000000 + diff.tv_nsec / 1000) /
421 sbc_data->frame_duration;
423 /* AudioFlinger does not seem to provide any *working*
424 * API to provide data in some interval and will just
425 * send another buffer as soon as we process current
426 * one. To prevent overflowing L2CAP socket, we need to
427 * introduce some artificial delay here base on how many
428 * audio frames were sent so far, i.e. if we're not
429 * lagging behind audio stream, we can sleep for
430 * duration of single media packet.
432 if (sbc_data->frames_sent >= expected_frames)
433 usleep(sbc_data->frame_duration *
434 mp->payload.frame_count);
437 static ssize_t sbc_write_data(void *codec_data, const void *buffer,
438 size_t bytes, int fd)
440 struct sbc_data *sbc_data = (struct sbc_data *) codec_data;
443 struct media_packet *mp = (struct media_packet *) sbc_data->out_buf;
444 size_t free_space = sbc_data->out_buf_size - sizeof(*mp);
449 mp->hdr.sequence_number = htons(sbc_data->seq++);
450 mp->hdr.ssrc = htonl(1);
451 mp->payload.frame_count = 0;
453 while (bytes - consumed >= sbc_data->in_frame_len) {
456 ret = sbc_encode(&sbc_data->enc, buffer + consumed,
457 sbc_data->in_frame_len,
458 mp->data + encoded, free_space,
462 error("SBC: failed to encode block");
466 mp->payload.frame_count++;
470 free_space -= written;
472 /* write data if we either filled media packed or encoded all
475 if (mp->payload.frame_count == sbc_data->frames_per_packet ||
477 write_media_packet(fd, sbc_data, mp, encoded);
480 free_space = sbc_data->out_buf_size - sizeof(*mp);
481 mp->payload.frame_count = 0;
485 if (consumed != bytes) {
486 /* we should encode all input data
487 * if we did not, something went wrong but we can't really
488 * handle this so this is just sanity check
490 error("SBC: failed to encode complete input buffer");
493 /* we always assume that all data was processed and sent */
497 static int audio_ipc_cmd(uint8_t service_id, uint8_t opcode, uint16_t len,
498 void *param, size_t *rsp_len, void *rsp, int *fd)
504 char cmsgbuf[CMSG_SPACE(sizeof(int))];
506 size_t s_len = sizeof(s);
508 pthread_mutex_lock(&sk_mutex);
511 error("audio: Invalid cmd socket passed to audio_ipc_cmd");
515 if (!rsp || !rsp_len) {
516 memset(&s, 0, s_len);
521 memset(&msg, 0, sizeof(msg));
522 memset(&cmd, 0, sizeof(cmd));
524 cmd.service_id = service_id;
528 iv[0].iov_base = &cmd;
529 iv[0].iov_len = sizeof(cmd);
531 iv[1].iov_base = param;
537 ret = sendmsg(audio_sk, &msg, 0);
539 error("audio: Sending command failed:%s", strerror(errno));
543 /* socket was shutdown */
545 error("audio: Command socket closed");
549 memset(&msg, 0, sizeof(msg));
550 memset(&cmd, 0, sizeof(cmd));
552 iv[0].iov_base = &cmd;
553 iv[0].iov_len = sizeof(cmd);
555 iv[1].iov_base = rsp;
556 iv[1].iov_len = *rsp_len;
562 memset(cmsgbuf, 0, sizeof(cmsgbuf));
563 msg.msg_control = cmsgbuf;
564 msg.msg_controllen = sizeof(cmsgbuf);
567 ret = recvmsg(audio_sk, &msg, 0);
569 error("audio: Receiving command response failed:%s",
574 if (ret < (ssize_t) sizeof(cmd)) {
575 error("audio: Too small response received(%zd bytes)", ret);
579 if (cmd.service_id != service_id) {
580 error("audio: Invalid service id (%u vs %u)", cmd.service_id,
585 if (ret != (ssize_t) (sizeof(cmd) + cmd.len)) {
586 error("audio: Malformed response received(%zd bytes)", ret);
590 if (cmd.opcode != opcode && cmd.opcode != AUDIO_OP_STATUS) {
591 error("audio: Invalid opcode received (%u vs %u)",
596 if (cmd.opcode == AUDIO_OP_STATUS) {
597 struct hal_status *s = rsp;
599 if (sizeof(*s) != cmd.len) {
600 error("audio: Invalid status length");
604 if (s->code == AUDIO_STATUS_SUCCESS) {
605 error("audio: Invalid success status response");
609 pthread_mutex_unlock(&sk_mutex);
614 pthread_mutex_unlock(&sk_mutex);
616 /* Receive auxiliary data in msg */
618 struct cmsghdr *cmsg;
622 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
623 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
624 if (cmsg->cmsg_level == SOL_SOCKET
625 && cmsg->cmsg_type == SCM_RIGHTS) {
626 memcpy(fd, CMSG_DATA(cmsg), sizeof(int));
635 return AUDIO_STATUS_SUCCESS;
638 /* Some serious issue happen on IPC - recover */
639 shutdown(audio_sk, SHUT_RDWR);
640 pthread_mutex_unlock(&sk_mutex);
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 error("audio: stream not started");
822 if (out->ep->fd < 0) {
823 error("audio: no transport socket");
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 warn("audio: cannot set sample rate to %d", rate);
854 static size_t out_get_buffer_size(const struct audio_stream *stream)
858 /* We should return proper buffer size calculated by codec (so each
859 * input buffer is encoded into single media packed) but this does not
860 * work well with AudioFlinger and causes problems. For this reason we
861 * use magic value here and out_write code takes care of splitting
862 * input buffer into multiple media packets.
867 static uint32_t out_get_channels(const struct audio_stream *stream)
869 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
873 return out->cfg.channels;
876 static audio_format_t out_get_format(const struct audio_stream *stream)
878 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
882 return out->cfg.format;
885 static int out_set_format(struct audio_stream *stream, audio_format_t format)
891 static int out_standby(struct audio_stream *stream)
893 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
897 if (out->audio_state == AUDIO_A2DP_STATE_STARTED) {
898 if (ipc_suspend_stream_cmd(out->ep->id) != AUDIO_STATUS_SUCCESS)
900 out->audio_state = AUDIO_A2DP_STATE_STANDBY;
906 static int out_dump(const struct audio_stream *stream, int fd)
912 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
914 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
918 bool enter_suspend = false;
919 bool exit_suspend = false;
923 str = strdup(kvpairs);
924 kvpair = strtok_r(str, ";", &saveptr);
926 for (; kvpair && *kvpair; kvpair = strtok_r(NULL, ";", &saveptr)) {
929 keyval = strchr(kvpair, '=');
936 if (!strcmp(kvpair, "closing")) {
937 if (!strcmp(keyval, "true"))
938 out->audio_state = AUDIO_A2DP_STATE_NONE;
939 } else if (!strcmp(kvpair, "A2dpSuspended")) {
940 if (!strcmp(keyval, "true"))
941 enter_suspend = true;
949 if (enter_suspend && out->audio_state == AUDIO_A2DP_STATE_STARTED) {
950 if (ipc_suspend_stream_cmd(out->ep->id) != AUDIO_STATUS_SUCCESS)
952 out->audio_state = AUDIO_A2DP_STATE_SUSPENDED;
955 if (exit_suspend && out->audio_state == AUDIO_A2DP_STATE_SUSPENDED)
956 out->audio_state = AUDIO_A2DP_STATE_STANDBY;
961 static char *out_get_parameters(const struct audio_stream *stream,
968 static uint32_t out_get_latency(const struct audio_stream_out *stream)
970 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
971 struct audio_endpoint *ep = out->ep;
976 pkt_duration = ep->codec->get_mediapacket_duration(ep->codec_data);
978 return FIXED_A2DP_PLAYBACK_LATENCY_MS + pkt_duration / 1000;
981 static int out_set_volume(struct audio_stream_out *stream, float left,
985 /* volume controlled in audioflinger mixer (digital) */
989 static int out_get_render_position(const struct audio_stream_out *stream,
990 uint32_t *dsp_frames)
996 static int out_add_audio_effect(const struct audio_stream *stream,
997 effect_handle_t effect)
1003 static int out_remove_audio_effect(const struct audio_stream *stream,
1004 effect_handle_t effect)
1010 static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1016 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1022 static size_t in_get_buffer_size(const struct audio_stream *stream)
1028 static uint32_t in_get_channels(const struct audio_stream *stream)
1034 static audio_format_t in_get_format(const struct audio_stream *stream)
1040 static int in_set_format(struct audio_stream *stream, audio_format_t format)
1046 static int in_standby(struct audio_stream *stream)
1052 static int in_dump(const struct audio_stream *stream, int fd)
1058 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1064 static char *in_get_parameters(const struct audio_stream *stream,
1071 static int in_set_gain(struct audio_stream_in *stream, float gain)
1077 static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1084 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1090 static int in_add_audio_effect(const struct audio_stream *stream,
1091 effect_handle_t effect)
1097 static int in_remove_audio_effect(const struct audio_stream *stream,
1098 effect_handle_t effect)
1104 static int audio_open_output_stream(struct audio_hw_device *dev,
1105 audio_io_handle_t handle,
1106 audio_devices_t devices,
1107 audio_output_flags_t flags,
1108 struct audio_config *config,
1109 struct audio_stream_out **stream_out)
1112 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
1113 struct a2dp_stream_out *out;
1114 struct audio_preset *preset;
1115 const struct audio_codec *codec;
1119 out = calloc(1, sizeof(struct a2dp_stream_out));
1125 out->stream.common.get_sample_rate = out_get_sample_rate;
1126 out->stream.common.set_sample_rate = out_set_sample_rate;
1127 out->stream.common.get_buffer_size = out_get_buffer_size;
1128 out->stream.common.get_channels = out_get_channels;
1129 out->stream.common.get_format = out_get_format;
1130 out->stream.common.set_format = out_set_format;
1131 out->stream.common.standby = out_standby;
1132 out->stream.common.dump = out_dump;
1133 out->stream.common.set_parameters = out_set_parameters;
1134 out->stream.common.get_parameters = out_get_parameters;
1135 out->stream.common.add_audio_effect = out_add_audio_effect;
1136 out->stream.common.remove_audio_effect = out_remove_audio_effect;
1137 out->stream.get_latency = out_get_latency;
1138 out->stream.set_volume = out_set_volume;
1139 out->stream.write = out_write;
1140 out->stream.get_render_position = out_get_render_position;
1142 /* TODO: for now we always use endpoint 0 */
1143 out->ep = &audio_endpoints[0];
1145 if (ipc_open_stream_cmd(out->ep->id, &mtu, &fd, &preset) !=
1146 AUDIO_STATUS_SUCCESS)
1149 if (!preset || fd < 0)
1154 codec = out->ep->codec;
1156 codec->init(preset, mtu, &out->ep->codec_data);
1157 codec->get_config(out->ep->codec_data, &out->cfg);
1159 DBG("rate=%d channels=%d format=%d", out->cfg.rate,
1160 out->cfg.channels, out->cfg.format);
1164 *stream_out = &out->stream;
1165 a2dp_dev->out = out;
1167 out->audio_state = AUDIO_A2DP_STATE_STANDBY;
1172 error("audio: cannot open output stream");
1178 static void audio_close_output_stream(struct audio_hw_device *dev,
1179 struct audio_stream_out *stream)
1181 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
1182 struct audio_endpoint *ep = a2dp_dev->out->ep;
1186 ipc_close_stream_cmd(ep->id);
1193 ep->codec->cleanup(ep->codec_data);
1194 ep->codec_data = NULL;
1197 a2dp_dev->out = NULL;
1200 static int audio_set_parameters(struct audio_hw_device *dev,
1201 const char *kvpairs)
1203 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
1204 struct a2dp_stream_out *out = a2dp_dev->out;
1211 return out->stream.common.set_parameters((struct audio_stream *) out,
1215 static char *audio_get_parameters(const struct audio_hw_device *dev,
1222 static int audio_init_check(const struct audio_hw_device *dev)
1228 static int audio_set_voice_volume(struct audio_hw_device *dev, float volume)
1234 static int audio_set_master_volume(struct audio_hw_device *dev, float volume)
1240 static int audio_set_mode(struct audio_hw_device *dev, int mode)
1246 static int audio_set_mic_mute(struct audio_hw_device *dev, bool state)
1252 static int audio_get_mic_mute(const struct audio_hw_device *dev, bool *state)
1258 static size_t audio_get_input_buffer_size(const struct audio_hw_device *dev,
1259 const struct audio_config *config)
1265 static int audio_open_input_stream(struct audio_hw_device *dev,
1266 audio_io_handle_t handle,
1267 audio_devices_t devices,
1268 struct audio_config *config,
1269 struct audio_stream_in **stream_in)
1271 struct audio_stream_in *in;
1275 in = calloc(1, sizeof(struct audio_stream_in));
1279 in->common.get_sample_rate = in_get_sample_rate;
1280 in->common.set_sample_rate = in_set_sample_rate;
1281 in->common.get_buffer_size = in_get_buffer_size;
1282 in->common.get_channels = in_get_channels;
1283 in->common.get_format = in_get_format;
1284 in->common.set_format = in_set_format;
1285 in->common.standby = in_standby;
1286 in->common.dump = in_dump;
1287 in->common.set_parameters = in_set_parameters;
1288 in->common.get_parameters = in_get_parameters;
1289 in->common.add_audio_effect = in_add_audio_effect;
1290 in->common.remove_audio_effect = in_remove_audio_effect;
1291 in->set_gain = in_set_gain;
1293 in->get_input_frames_lost = in_get_input_frames_lost;
1300 static void audio_close_input_stream(struct audio_hw_device *dev,
1301 struct audio_stream_in *stream_in)
1307 static int audio_dump(const audio_hw_device_t *device, int fd)
1313 static int audio_close(hw_device_t *device)
1315 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *)device;
1319 unregister_endpoints();
1321 shutdown(listen_sk, SHUT_RDWR);
1322 shutdown(audio_sk, SHUT_RDWR);
1324 pthread_join(ipc_th, NULL);
1333 static void *ipc_handler(void *data)
1342 DBG("Waiting for connection ...");
1344 sk = accept(listen_sk, NULL, NULL);
1351 if (err != ECONNABORTED && err != EINVAL)
1352 error("audio: Failed to accept socket: %d (%s)",
1353 err, strerror(err));
1358 pthread_mutex_lock(&sk_mutex);
1360 pthread_mutex_unlock(&sk_mutex);
1362 DBG("Audio IPC: Connected");
1364 if (register_endpoints() != AUDIO_STATUS_SUCCESS) {
1365 error("audio: Failed to register endpoints");
1367 unregister_endpoints();
1369 pthread_mutex_lock(&sk_mutex);
1370 shutdown(audio_sk, SHUT_RDWR);
1373 pthread_mutex_unlock(&sk_mutex);
1378 memset(&pfd, 0, sizeof(pfd));
1380 pfd.events = POLLHUP | POLLERR | POLLNVAL;
1382 /* Check if socket is still alive. Empty while loop.*/
1383 while (poll(&pfd, 1, -1) < 0 && errno == EINTR);
1385 if (pfd.revents & (POLLHUP | POLLERR | POLLNVAL)) {
1386 info("Audio HAL: Socket closed");
1388 pthread_mutex_lock(&sk_mutex);
1391 pthread_mutex_unlock(&sk_mutex);
1395 /* audio_sk is closed at this point, just cleanup endpoints states */
1396 memset(audio_endpoints, 0, sizeof(audio_endpoints));
1398 info("Closing Audio IPC thread");
1402 static int audio_ipc_init(void)
1404 struct sockaddr_un addr;
1410 sk = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
1413 error("audio: Failed to create socket: %d (%s)", err,
1418 memset(&addr, 0, sizeof(addr));
1419 addr.sun_family = AF_UNIX;
1421 memcpy(addr.sun_path, BLUEZ_AUDIO_SK_PATH,
1422 sizeof(BLUEZ_AUDIO_SK_PATH));
1424 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1426 error("audio: Failed to bind socket: %d (%s)", err,
1431 if (listen(sk, 1) < 0) {
1433 error("audio: Failed to listen on the socket: %d (%s)", err,
1440 err = pthread_create(&ipc_th, NULL, ipc_handler, NULL);
1444 error("audio: Failed to start Audio IPC thread: %d (%s)",
1445 err, strerror(err));
1456 static int audio_open(const hw_module_t *module, const char *name,
1457 hw_device_t **device)
1459 struct a2dp_audio_dev *a2dp_dev;
1464 if (strcmp(name, AUDIO_HARDWARE_INTERFACE)) {
1465 error("audio: interface %s not matching [%s]", name,
1466 AUDIO_HARDWARE_INTERFACE);
1470 err = audio_ipc_init();
1474 a2dp_dev = calloc(1, sizeof(struct a2dp_audio_dev));
1478 a2dp_dev->dev.common.tag = HARDWARE_DEVICE_TAG;
1479 a2dp_dev->dev.common.version = AUDIO_DEVICE_API_VERSION_CURRENT;
1480 a2dp_dev->dev.common.module = (struct hw_module_t *) module;
1481 a2dp_dev->dev.common.close = audio_close;
1483 a2dp_dev->dev.init_check = audio_init_check;
1484 a2dp_dev->dev.set_voice_volume = audio_set_voice_volume;
1485 a2dp_dev->dev.set_master_volume = audio_set_master_volume;
1486 a2dp_dev->dev.set_mode = audio_set_mode;
1487 a2dp_dev->dev.set_mic_mute = audio_set_mic_mute;
1488 a2dp_dev->dev.get_mic_mute = audio_get_mic_mute;
1489 a2dp_dev->dev.set_parameters = audio_set_parameters;
1490 a2dp_dev->dev.get_parameters = audio_get_parameters;
1491 a2dp_dev->dev.get_input_buffer_size = audio_get_input_buffer_size;
1492 a2dp_dev->dev.open_output_stream = audio_open_output_stream;
1493 a2dp_dev->dev.close_output_stream = audio_close_output_stream;
1494 a2dp_dev->dev.open_input_stream = audio_open_input_stream;
1495 a2dp_dev->dev.close_input_stream = audio_close_input_stream;
1496 a2dp_dev->dev.dump = audio_dump;
1498 /* Note that &a2dp_dev->dev.common is the same pointer as a2dp_dev.
1499 * This results from the structure of following structs:a2dp_audio_dev,
1500 * audio_hw_device. We will rely on this later in the code.*/
1501 *device = &a2dp_dev->dev.common;
1506 static struct hw_module_methods_t hal_module_methods = {
1510 struct audio_module HAL_MODULE_INFO_SYM = {
1512 .tag = HARDWARE_MODULE_TAG,
1515 .id = AUDIO_HARDWARE_MODULE_ID,
1516 .name = "A2DP Bluez HW HAL",
1517 .author = "Intel Corporation",
1518 .methods = &hal_module_methods,