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>
30 #include <hardware/audio.h>
31 #include <hardware/hardware.h>
35 #include "audio-msg.h"
38 #include "../profiles/audio/a2dp-codecs.h"
40 #define FIXED_A2DP_PLAYBACK_LATENCY_MS 25
42 static const uint8_t a2dp_src_uuid[] = {
43 0x00, 0x00, 0x11, 0x0a, 0x00, 0x00, 0x10, 0x00,
44 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb };
46 static int listen_sk = -1;
47 static int audio_sk = -1;
49 static pthread_t ipc_th = 0;
50 static pthread_mutex_t sk_mutex = PTHREAD_MUTEX_INITIALIZER;
52 #if __BYTE_ORDER == __LITTLE_ENDIAN
63 uint16_t sequence_number;
67 } __attribute__ ((packed));
70 unsigned frame_count:4;
72 unsigned is_last_fragment:1;
73 unsigned is_first_fragment:1;
74 unsigned is_fragmented:1;
75 } __attribute__ ((packed));
77 #elif __BYTE_ORDER == __BIG_ENDIAN
88 uint16_t sequence_number;
92 } __attribute__ ((packed));
95 unsigned is_fragmented:1;
96 unsigned is_first_fragment:1;
97 unsigned is_last_fragment:1;
99 unsigned frame_count:4;
100 } __attribute__ ((packed));
103 #error "Unknown byte order"
106 struct media_packet {
107 struct rtp_header hdr;
108 struct rtp_payload payload;
112 struct audio_input_config {
115 audio_format_t format;
129 unsigned frame_duration;
130 unsigned frames_per_packet;
132 struct timespec start;
133 unsigned frames_sent;
138 static inline void timespec_diff(struct timespec *a, struct timespec *b,
139 struct timespec *res)
141 res->tv_sec = a->tv_sec - b->tv_sec;
142 res->tv_nsec = a->tv_nsec - b->tv_nsec;
144 if (res->tv_nsec < 0) {
146 res->tv_nsec += 1000000000; /* 1sec */
150 static int sbc_get_presets(struct audio_preset *preset, size_t *len);
151 static int sbc_codec_init(struct audio_preset *preset, uint16_t mtu,
153 static int sbc_cleanup(void *codec_data);
154 static int sbc_get_config(void *codec_data, struct audio_input_config *config);
155 static size_t sbc_get_buffer_size(void *codec_data);
156 static size_t sbc_get_mediapacket_duration(void *codec_data);
157 static void sbc_resume(void *codec_data);
158 static ssize_t sbc_write_data(void *codec_data, const void *buffer,
159 size_t bytes, int fd);
164 int (*get_presets) (struct audio_preset *preset, size_t *len);
166 int (*init) (struct audio_preset *preset, uint16_t mtu,
168 int (*cleanup) (void *codec_data);
169 int (*get_config) (void *codec_data,
170 struct audio_input_config *config);
171 size_t (*get_buffer_size) (void *codec_data);
172 size_t (*get_mediapacket_duration) (void *codec_data);
173 void (*resume) (void *codec_data);
174 ssize_t (*write_data) (void *codec_data, const void *buffer,
175 size_t bytes, int fd);
178 static const struct audio_codec audio_codecs[] = {
180 .type = A2DP_CODEC_SBC,
182 .get_presets = sbc_get_presets,
184 .init = sbc_codec_init,
185 .cleanup = sbc_cleanup,
186 .get_config = sbc_get_config,
187 .get_buffer_size = sbc_get_buffer_size,
188 .get_mediapacket_duration = sbc_get_mediapacket_duration,
189 .resume = sbc_resume,
190 .write_data = sbc_write_data,
194 #define NUM_CODECS (sizeof(audio_codecs) / sizeof(audio_codecs[0]))
196 #define MAX_AUDIO_ENDPOINTS NUM_CODECS
198 struct audio_endpoint {
200 const struct audio_codec *codec;
205 static struct audio_endpoint audio_endpoints[MAX_AUDIO_ENDPOINTS];
208 AUDIO_A2DP_STATE_NONE,
209 AUDIO_A2DP_STATE_STANDBY,
210 AUDIO_A2DP_STATE_SUSPENDED,
211 AUDIO_A2DP_STATE_STARTED
214 struct a2dp_stream_out {
215 struct audio_stream_out stream;
217 struct audio_endpoint *ep;
218 enum a2dp_state_t audio_state;
219 struct audio_input_config cfg;
222 struct a2dp_audio_dev {
223 struct audio_hw_device dev;
224 struct a2dp_stream_out *out;
227 static const a2dp_sbc_t sbc_presets[] = {
229 .frequency = SBC_SAMPLING_FREQ_44100 | SBC_SAMPLING_FREQ_48000,
230 .channel_mode = SBC_CHANNEL_MODE_MONO |
231 SBC_CHANNEL_MODE_DUAL_CHANNEL |
232 SBC_CHANNEL_MODE_STEREO |
233 SBC_CHANNEL_MODE_JOINT_STEREO,
234 .subbands = SBC_SUBBANDS_4 | SBC_SUBBANDS_8,
235 .allocation_method = SBC_ALLOCATION_SNR |
236 SBC_ALLOCATION_LOUDNESS,
237 .block_length = SBC_BLOCK_LENGTH_4 | SBC_BLOCK_LENGTH_8 |
238 SBC_BLOCK_LENGTH_12 | SBC_BLOCK_LENGTH_16,
239 .min_bitpool = MIN_BITPOOL,
240 .max_bitpool = MAX_BITPOOL
243 .frequency = SBC_SAMPLING_FREQ_44100,
244 .channel_mode = SBC_CHANNEL_MODE_JOINT_STEREO,
245 .subbands = SBC_SUBBANDS_8,
246 .allocation_method = SBC_ALLOCATION_LOUDNESS,
247 .block_length = SBC_BLOCK_LENGTH_16,
248 .min_bitpool = MIN_BITPOOL,
249 .max_bitpool = MAX_BITPOOL
252 .frequency = SBC_SAMPLING_FREQ_48000,
253 .channel_mode = SBC_CHANNEL_MODE_JOINT_STEREO,
254 .subbands = SBC_SUBBANDS_8,
255 .allocation_method = SBC_ALLOCATION_LOUDNESS,
256 .block_length = SBC_BLOCK_LENGTH_16,
257 .min_bitpool = MIN_BITPOOL,
258 .max_bitpool = MAX_BITPOOL
262 static int sbc_get_presets(struct audio_preset *preset, size_t *len)
267 uint8_t *ptr = (uint8_t *) preset;
268 size_t preset_size = sizeof(*preset) + sizeof(a2dp_sbc_t);
270 count = sizeof(sbc_presets) / sizeof(sbc_presets[0]);
272 for (i = 0; i < count; i++) {
273 preset = (struct audio_preset *) ptr;
275 if (new_len + preset_size > *len)
278 preset->len = sizeof(a2dp_sbc_t);
279 memcpy(preset->data, &sbc_presets[i], preset->len);
281 new_len += preset_size;
290 static void sbc_init_encoder(struct sbc_data *sbc_data)
292 a2dp_sbc_t *in = &sbc_data->sbc;
293 sbc_t *out = &sbc_data->enc;
295 sbc_init_a2dp(out, 0L, in, sizeof(*in));
297 out->endian = SBC_LE;
298 out->bitpool = in->max_bitpool;
301 static int sbc_codec_init(struct audio_preset *preset, uint16_t mtu,
304 struct sbc_data *sbc_data;
305 size_t hdr_len = sizeof(struct media_packet);
307 size_t out_frame_len;
310 if (preset->len != sizeof(a2dp_sbc_t)) {
311 error("SBC: preset size mismatch");
312 return AUDIO_STATUS_FAILED;
315 sbc_data = calloc(sizeof(struct sbc_data), 1);
317 memcpy(&sbc_data->sbc, preset->data, preset->len);
319 sbc_init_encoder(sbc_data);
321 in_frame_len = sbc_get_codesize(&sbc_data->enc);
322 out_frame_len = sbc_get_frame_length(&sbc_data->enc);
323 num_frames = (mtu - hdr_len) / out_frame_len;
325 sbc_data->in_frame_len = in_frame_len;
326 sbc_data->in_buf_size = num_frames * in_frame_len;
328 sbc_data->out_buf_size = hdr_len + num_frames * out_frame_len;
329 sbc_data->out_buf = calloc(1, sbc_data->out_buf_size);
331 sbc_data->frame_duration = sbc_get_frame_duration(&sbc_data->enc);
332 sbc_data->frames_per_packet = num_frames;
334 *codec_data = sbc_data;
336 return AUDIO_STATUS_SUCCESS;
339 static int sbc_cleanup(void *codec_data)
341 struct sbc_data *sbc_data = (struct sbc_data *) codec_data;
343 sbc_finish(&sbc_data->enc);
344 free(sbc_data->out_buf);
347 return AUDIO_STATUS_SUCCESS;
350 static int sbc_get_config(void *codec_data, struct audio_input_config *config)
352 struct sbc_data *sbc_data = (struct sbc_data *) codec_data;
354 switch (sbc_data->sbc.frequency) {
355 case SBC_SAMPLING_FREQ_16000:
356 config->rate = 16000;
358 case SBC_SAMPLING_FREQ_32000:
359 config->rate = 32000;
361 case SBC_SAMPLING_FREQ_44100:
362 config->rate = 44100;
364 case SBC_SAMPLING_FREQ_48000:
365 config->rate = 48000;
368 return AUDIO_STATUS_FAILED;
370 config->channels = sbc_data->sbc.channel_mode == SBC_CHANNEL_MODE_MONO ?
371 AUDIO_CHANNEL_OUT_MONO :
372 AUDIO_CHANNEL_OUT_STEREO;
373 config->format = AUDIO_FORMAT_PCM_16_BIT;
375 return AUDIO_STATUS_SUCCESS;
378 static size_t sbc_get_buffer_size(void *codec_data)
380 struct sbc_data *sbc_data = (struct sbc_data *) codec_data;
382 return sbc_data->in_buf_size;
385 static size_t sbc_get_mediapacket_duration(void *codec_data)
387 struct sbc_data *sbc_data = (struct sbc_data *) codec_data;
389 return sbc_data->frame_duration * sbc_data->frames_per_packet;
392 static void sbc_resume(void *codec_data)
394 struct sbc_data *sbc_data = (struct sbc_data *) codec_data;
398 clock_gettime(CLOCK_MONOTONIC, &sbc_data->start);
400 sbc_data->frames_sent = 0;
403 static int write_media_packet(int fd, struct sbc_data *sbc_data,
404 struct media_packet *mp, size_t data_len)
407 struct timespec diff;
408 unsigned expected_frames;
412 ret = write(fd, mp, sizeof(*mp) + data_len);
420 sbc_data->frames_sent += mp->payload.frame_count;
422 clock_gettime(CLOCK_MONOTONIC, &cur);
423 timespec_diff(&cur, &sbc_data->start, &diff);
424 expected_frames = (diff.tv_sec * 1000000 + diff.tv_nsec / 1000) /
425 sbc_data->frame_duration;
427 /* AudioFlinger does not seem to provide any *working*
428 * API to provide data in some interval and will just
429 * send another buffer as soon as we process current
430 * one. To prevent overflowing L2CAP socket, we need to
431 * introduce some artificial delay here base on how many
432 * audio frames were sent so far, i.e. if we're not
433 * lagging behind audio stream, we can sleep for
434 * duration of single media packet.
436 if (sbc_data->frames_sent >= expected_frames)
437 usleep(sbc_data->frame_duration *
438 mp->payload.frame_count);
443 static ssize_t sbc_write_data(void *codec_data, const void *buffer,
444 size_t bytes, int fd)
446 struct sbc_data *sbc_data = (struct sbc_data *) codec_data;
449 struct media_packet *mp = (struct media_packet *) sbc_data->out_buf;
450 size_t free_space = sbc_data->out_buf_size - sizeof(*mp);
455 mp->hdr.sequence_number = htons(sbc_data->seq++);
456 mp->hdr.ssrc = htonl(1);
457 mp->payload.frame_count = 0;
459 while (bytes - consumed >= sbc_data->in_frame_len) {
462 ret = sbc_encode(&sbc_data->enc, buffer + consumed,
463 sbc_data->in_frame_len,
464 mp->data + encoded, free_space,
468 error("SBC: failed to encode block");
472 mp->payload.frame_count++;
476 free_space -= written;
478 /* write data if we either filled media packed or encoded all
481 if (mp->payload.frame_count == sbc_data->frames_per_packet ||
483 ret = write_media_packet(fd, sbc_data, mp, encoded);
488 free_space = sbc_data->out_buf_size - sizeof(*mp);
489 mp->payload.frame_count = 0;
493 if (consumed != bytes) {
494 /* we should encode all input data
495 * if we did not, something went wrong but we can't really
496 * handle this so this is just sanity check
498 error("SBC: failed to encode complete input buffer");
501 /* we always assume that all data was processed and sent */
505 static int audio_ipc_cmd(uint8_t service_id, uint8_t opcode, uint16_t len,
506 void *param, size_t *rsp_len, void *rsp, int *fd)
512 char cmsgbuf[CMSG_SPACE(sizeof(int))];
514 size_t s_len = sizeof(s);
516 pthread_mutex_lock(&sk_mutex);
519 error("audio: Invalid cmd socket passed to audio_ipc_cmd");
523 if (!rsp || !rsp_len) {
524 memset(&s, 0, s_len);
529 memset(&msg, 0, sizeof(msg));
530 memset(&cmd, 0, sizeof(cmd));
532 cmd.service_id = service_id;
536 iv[0].iov_base = &cmd;
537 iv[0].iov_len = sizeof(cmd);
539 iv[1].iov_base = param;
545 ret = sendmsg(audio_sk, &msg, 0);
547 error("audio: Sending command failed:%s", strerror(errno));
551 /* socket was shutdown */
553 error("audio: Command socket closed");
557 memset(&msg, 0, sizeof(msg));
558 memset(&cmd, 0, sizeof(cmd));
560 iv[0].iov_base = &cmd;
561 iv[0].iov_len = sizeof(cmd);
563 iv[1].iov_base = rsp;
564 iv[1].iov_len = *rsp_len;
570 memset(cmsgbuf, 0, sizeof(cmsgbuf));
571 msg.msg_control = cmsgbuf;
572 msg.msg_controllen = sizeof(cmsgbuf);
575 ret = recvmsg(audio_sk, &msg, 0);
577 error("audio: Receiving command response failed:%s",
582 if (ret < (ssize_t) sizeof(cmd)) {
583 error("audio: Too small response received(%zd bytes)", ret);
587 if (cmd.service_id != service_id) {
588 error("audio: Invalid service id (%u vs %u)", cmd.service_id,
593 if (ret != (ssize_t) (sizeof(cmd) + cmd.len)) {
594 error("audio: Malformed response received(%zd bytes)", ret);
598 if (cmd.opcode != opcode && cmd.opcode != AUDIO_OP_STATUS) {
599 error("audio: Invalid opcode received (%u vs %u)",
604 if (cmd.opcode == AUDIO_OP_STATUS) {
605 struct hal_status *s = rsp;
607 if (sizeof(*s) != cmd.len) {
608 error("audio: Invalid status length");
612 if (s->code == AUDIO_STATUS_SUCCESS) {
613 error("audio: Invalid success status response");
617 pthread_mutex_unlock(&sk_mutex);
622 pthread_mutex_unlock(&sk_mutex);
624 /* Receive auxiliary data in msg */
626 struct cmsghdr *cmsg;
630 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
631 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
632 if (cmsg->cmsg_level == SOL_SOCKET
633 && cmsg->cmsg_type == SCM_RIGHTS) {
634 memcpy(fd, CMSG_DATA(cmsg), sizeof(int));
643 return AUDIO_STATUS_SUCCESS;
646 /* Some serious issue happen on IPC - recover */
647 shutdown(audio_sk, SHUT_RDWR);
648 pthread_mutex_unlock(&sk_mutex);
650 return AUDIO_STATUS_FAILED;
653 static int ipc_open_cmd(const struct audio_codec *codec)
655 uint8_t buf[BLUEZ_AUDIO_MTU];
656 struct audio_cmd_open *cmd = (struct audio_cmd_open *) buf;
657 struct audio_rsp_open rsp;
658 size_t cmd_len = sizeof(buf) - sizeof(*cmd);
659 size_t rsp_len = sizeof(rsp);
664 memcpy(cmd->uuid, a2dp_src_uuid, sizeof(a2dp_src_uuid));
666 cmd->codec = codec->type;
667 cmd->presets = codec->get_presets(cmd->preset, &cmd_len);
669 cmd_len += sizeof(*cmd);
671 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_OPEN, cmd_len, cmd,
672 &rsp_len, &rsp, NULL);
674 if (result != AUDIO_STATUS_SUCCESS)
680 static int ipc_close_cmd(uint8_t endpoint_id)
682 struct audio_cmd_close cmd;
687 cmd.id = endpoint_id;
689 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_CLOSE,
690 sizeof(cmd), &cmd, NULL, NULL, NULL);
695 static int ipc_open_stream_cmd(uint8_t endpoint_id, uint16_t *mtu, int *fd,
696 struct audio_preset **caps)
698 char buf[BLUEZ_AUDIO_MTU];
699 struct audio_cmd_open_stream cmd;
700 struct audio_rsp_open_stream *rsp =
701 (struct audio_rsp_open_stream *) &buf;
702 size_t rsp_len = sizeof(buf);
708 return AUDIO_STATUS_FAILED;
710 cmd.id = endpoint_id;
712 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_OPEN_STREAM,
713 sizeof(cmd), &cmd, &rsp_len, rsp, fd);
715 if (result == AUDIO_STATUS_SUCCESS) {
716 size_t buf_len = sizeof(struct audio_preset) +
719 *caps = malloc(buf_len);
720 memcpy(*caps, &rsp->preset, buf_len);
728 static int ipc_close_stream_cmd(uint8_t endpoint_id)
730 struct audio_cmd_close_stream cmd;
735 cmd.id = endpoint_id;
737 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_CLOSE_STREAM,
738 sizeof(cmd), &cmd, NULL, NULL, NULL);
743 static int ipc_resume_stream_cmd(uint8_t endpoint_id)
745 struct audio_cmd_resume_stream cmd;
750 cmd.id = endpoint_id;
752 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_RESUME_STREAM,
753 sizeof(cmd), &cmd, NULL, NULL, NULL);
758 static int ipc_suspend_stream_cmd(uint8_t endpoint_id)
760 struct audio_cmd_suspend_stream cmd;
765 cmd.id = endpoint_id;
767 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_SUSPEND_STREAM,
768 sizeof(cmd), &cmd, NULL, NULL, NULL);
773 static int register_endpoints(void)
775 struct audio_endpoint *ep = &audio_endpoints[0];
778 for (i = 0; i < NUM_CODECS; i++, ep++) {
779 const struct audio_codec *codec = &audio_codecs[i];
781 ep->id = ipc_open_cmd(codec);
784 return AUDIO_STATUS_FAILED;
787 ep->codec_data = NULL;
791 return AUDIO_STATUS_SUCCESS;
794 static void unregister_endpoints(void)
798 for (i = 0; i < MAX_AUDIO_ENDPOINTS; i++) {
799 struct audio_endpoint *ep = &audio_endpoints[i];
802 ipc_close_cmd(ep->id);
803 memset(ep, 0, sizeof(*ep));
808 static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
811 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
813 /* We can auto-start only from standby */
814 if (out->audio_state == AUDIO_A2DP_STATE_STANDBY) {
815 DBG("stream in standby, auto-start");
817 if (ipc_resume_stream_cmd(out->ep->id) != AUDIO_STATUS_SUCCESS)
820 out->ep->codec->resume(out->ep->codec_data);
822 out->audio_state = AUDIO_A2DP_STATE_STARTED;
825 if (out->audio_state != AUDIO_A2DP_STATE_STARTED) {
826 error("audio: stream not started");
830 if (out->ep->fd < 0) {
831 error("audio: no transport socket");
835 return out->ep->codec->write_data(out->ep->codec_data, buffer,
839 static uint32_t out_get_sample_rate(const struct audio_stream *stream)
841 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
845 return out->cfg.rate;
848 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
850 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
854 if (rate != out->cfg.rate) {
855 warn("audio: cannot set sample rate to %d", rate);
862 static size_t out_get_buffer_size(const struct audio_stream *stream)
866 /* We should return proper buffer size calculated by codec (so each
867 * input buffer is encoded into single media packed) but this does not
868 * work well with AudioFlinger and causes problems. For this reason we
869 * use magic value here and out_write code takes care of splitting
870 * input buffer into multiple media packets.
875 static uint32_t out_get_channels(const struct audio_stream *stream)
877 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
881 return out->cfg.channels;
884 static audio_format_t out_get_format(const struct audio_stream *stream)
886 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
890 return out->cfg.format;
893 static int out_set_format(struct audio_stream *stream, audio_format_t format)
899 static int out_standby(struct audio_stream *stream)
901 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
905 if (out->audio_state == AUDIO_A2DP_STATE_STARTED) {
906 if (ipc_suspend_stream_cmd(out->ep->id) != AUDIO_STATUS_SUCCESS)
908 out->audio_state = AUDIO_A2DP_STATE_STANDBY;
914 static int out_dump(const struct audio_stream *stream, int fd)
920 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
922 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
926 bool enter_suspend = false;
927 bool exit_suspend = false;
931 str = strdup(kvpairs);
932 kvpair = strtok_r(str, ";", &saveptr);
934 for (; kvpair && *kvpair; kvpair = strtok_r(NULL, ";", &saveptr)) {
937 keyval = strchr(kvpair, '=');
944 if (!strcmp(kvpair, "closing")) {
945 if (!strcmp(keyval, "true"))
946 out->audio_state = AUDIO_A2DP_STATE_NONE;
947 } else if (!strcmp(kvpair, "A2dpSuspended")) {
948 if (!strcmp(keyval, "true"))
949 enter_suspend = true;
957 if (enter_suspend && out->audio_state == AUDIO_A2DP_STATE_STARTED) {
958 if (ipc_suspend_stream_cmd(out->ep->id) != AUDIO_STATUS_SUCCESS)
960 out->audio_state = AUDIO_A2DP_STATE_SUSPENDED;
963 if (exit_suspend && out->audio_state == AUDIO_A2DP_STATE_SUSPENDED)
964 out->audio_state = AUDIO_A2DP_STATE_STANDBY;
969 static char *out_get_parameters(const struct audio_stream *stream,
976 static uint32_t out_get_latency(const struct audio_stream_out *stream)
978 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
979 struct audio_endpoint *ep = out->ep;
984 pkt_duration = ep->codec->get_mediapacket_duration(ep->codec_data);
986 return FIXED_A2DP_PLAYBACK_LATENCY_MS + pkt_duration / 1000;
989 static int out_set_volume(struct audio_stream_out *stream, float left,
993 /* volume controlled in audioflinger mixer (digital) */
997 static int out_get_render_position(const struct audio_stream_out *stream,
998 uint32_t *dsp_frames)
1004 static int out_add_audio_effect(const struct audio_stream *stream,
1005 effect_handle_t effect)
1011 static int out_remove_audio_effect(const struct audio_stream *stream,
1012 effect_handle_t effect)
1018 static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1024 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1030 static size_t in_get_buffer_size(const struct audio_stream *stream)
1036 static uint32_t in_get_channels(const struct audio_stream *stream)
1042 static audio_format_t in_get_format(const struct audio_stream *stream)
1048 static int in_set_format(struct audio_stream *stream, audio_format_t format)
1054 static int in_standby(struct audio_stream *stream)
1060 static int in_dump(const struct audio_stream *stream, int fd)
1066 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1072 static char *in_get_parameters(const struct audio_stream *stream,
1079 static int in_set_gain(struct audio_stream_in *stream, float gain)
1085 static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1092 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1098 static int in_add_audio_effect(const struct audio_stream *stream,
1099 effect_handle_t effect)
1105 static int in_remove_audio_effect(const struct audio_stream *stream,
1106 effect_handle_t effect)
1112 static int set_blocking(int fd)
1116 flags = fcntl(fd, F_GETFL, 0);
1118 error("fcntl(F_GETFL): %s (%d)", strerror(errno), errno);
1122 if (fcntl(fd, F_SETFL, flags & ~O_NONBLOCK) < 0) {
1123 error("fcntl(F_SETFL): %s (%d)", strerror(errno), errno);
1130 static int audio_open_output_stream(struct audio_hw_device *dev,
1131 audio_io_handle_t handle,
1132 audio_devices_t devices,
1133 audio_output_flags_t flags,
1134 struct audio_config *config,
1135 struct audio_stream_out **stream_out)
1138 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
1139 struct a2dp_stream_out *out;
1140 struct audio_preset *preset;
1141 const struct audio_codec *codec;
1145 out = calloc(1, sizeof(struct a2dp_stream_out));
1151 out->stream.common.get_sample_rate = out_get_sample_rate;
1152 out->stream.common.set_sample_rate = out_set_sample_rate;
1153 out->stream.common.get_buffer_size = out_get_buffer_size;
1154 out->stream.common.get_channels = out_get_channels;
1155 out->stream.common.get_format = out_get_format;
1156 out->stream.common.set_format = out_set_format;
1157 out->stream.common.standby = out_standby;
1158 out->stream.common.dump = out_dump;
1159 out->stream.common.set_parameters = out_set_parameters;
1160 out->stream.common.get_parameters = out_get_parameters;
1161 out->stream.common.add_audio_effect = out_add_audio_effect;
1162 out->stream.common.remove_audio_effect = out_remove_audio_effect;
1163 out->stream.get_latency = out_get_latency;
1164 out->stream.set_volume = out_set_volume;
1165 out->stream.write = out_write;
1166 out->stream.get_render_position = out_get_render_position;
1168 /* TODO: for now we always use endpoint 0 */
1169 out->ep = &audio_endpoints[0];
1171 if (ipc_open_stream_cmd(out->ep->id, &mtu, &fd, &preset) !=
1172 AUDIO_STATUS_SUCCESS)
1175 if (!preset || fd < 0)
1178 if (set_blocking(fd) < 0)
1182 codec = out->ep->codec;
1184 codec->init(preset, mtu, &out->ep->codec_data);
1185 codec->get_config(out->ep->codec_data, &out->cfg);
1187 DBG("rate=%d channels=%d format=%d", out->cfg.rate,
1188 out->cfg.channels, out->cfg.format);
1192 *stream_out = &out->stream;
1193 a2dp_dev->out = out;
1195 out->audio_state = AUDIO_A2DP_STATE_STANDBY;
1200 error("audio: cannot open output stream");
1206 static void audio_close_output_stream(struct audio_hw_device *dev,
1207 struct audio_stream_out *stream)
1209 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
1210 struct audio_endpoint *ep = a2dp_dev->out->ep;
1214 ipc_close_stream_cmd(ep->id);
1221 ep->codec->cleanup(ep->codec_data);
1222 ep->codec_data = NULL;
1225 a2dp_dev->out = NULL;
1228 static int audio_set_parameters(struct audio_hw_device *dev,
1229 const char *kvpairs)
1231 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
1232 struct a2dp_stream_out *out = a2dp_dev->out;
1239 return out->stream.common.set_parameters((struct audio_stream *) out,
1243 static char *audio_get_parameters(const struct audio_hw_device *dev,
1250 static int audio_init_check(const struct audio_hw_device *dev)
1256 static int audio_set_voice_volume(struct audio_hw_device *dev, float volume)
1262 static int audio_set_master_volume(struct audio_hw_device *dev, float volume)
1268 static int audio_set_mode(struct audio_hw_device *dev, int mode)
1274 static int audio_set_mic_mute(struct audio_hw_device *dev, bool state)
1280 static int audio_get_mic_mute(const struct audio_hw_device *dev, bool *state)
1286 static size_t audio_get_input_buffer_size(const struct audio_hw_device *dev,
1287 const struct audio_config *config)
1293 static int audio_open_input_stream(struct audio_hw_device *dev,
1294 audio_io_handle_t handle,
1295 audio_devices_t devices,
1296 struct audio_config *config,
1297 struct audio_stream_in **stream_in)
1299 struct audio_stream_in *in;
1303 in = calloc(1, sizeof(struct audio_stream_in));
1307 in->common.get_sample_rate = in_get_sample_rate;
1308 in->common.set_sample_rate = in_set_sample_rate;
1309 in->common.get_buffer_size = in_get_buffer_size;
1310 in->common.get_channels = in_get_channels;
1311 in->common.get_format = in_get_format;
1312 in->common.set_format = in_set_format;
1313 in->common.standby = in_standby;
1314 in->common.dump = in_dump;
1315 in->common.set_parameters = in_set_parameters;
1316 in->common.get_parameters = in_get_parameters;
1317 in->common.add_audio_effect = in_add_audio_effect;
1318 in->common.remove_audio_effect = in_remove_audio_effect;
1319 in->set_gain = in_set_gain;
1321 in->get_input_frames_lost = in_get_input_frames_lost;
1328 static void audio_close_input_stream(struct audio_hw_device *dev,
1329 struct audio_stream_in *stream_in)
1335 static int audio_dump(const audio_hw_device_t *device, int fd)
1341 static int audio_close(hw_device_t *device)
1343 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *)device;
1347 unregister_endpoints();
1349 shutdown(listen_sk, SHUT_RDWR);
1350 shutdown(audio_sk, SHUT_RDWR);
1352 pthread_join(ipc_th, NULL);
1361 static void *ipc_handler(void *data)
1370 DBG("Waiting for connection ...");
1372 sk = accept(listen_sk, NULL, NULL);
1379 if (err != ECONNABORTED && err != EINVAL)
1380 error("audio: Failed to accept socket: %d (%s)",
1381 err, strerror(err));
1386 pthread_mutex_lock(&sk_mutex);
1388 pthread_mutex_unlock(&sk_mutex);
1390 DBG("Audio IPC: Connected");
1392 if (register_endpoints() != AUDIO_STATUS_SUCCESS) {
1393 error("audio: Failed to register endpoints");
1395 unregister_endpoints();
1397 pthread_mutex_lock(&sk_mutex);
1398 shutdown(audio_sk, SHUT_RDWR);
1401 pthread_mutex_unlock(&sk_mutex);
1406 memset(&pfd, 0, sizeof(pfd));
1408 pfd.events = POLLHUP | POLLERR | POLLNVAL;
1410 /* Check if socket is still alive. Empty while loop.*/
1411 while (poll(&pfd, 1, -1) < 0 && errno == EINTR);
1413 if (pfd.revents & (POLLHUP | POLLERR | POLLNVAL)) {
1414 info("Audio HAL: Socket closed");
1416 pthread_mutex_lock(&sk_mutex);
1419 pthread_mutex_unlock(&sk_mutex);
1423 /* audio_sk is closed at this point, just cleanup endpoints states */
1424 memset(audio_endpoints, 0, sizeof(audio_endpoints));
1426 info("Closing Audio IPC thread");
1430 static int audio_ipc_init(void)
1432 struct sockaddr_un addr;
1438 sk = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
1441 error("audio: Failed to create socket: %d (%s)", err,
1446 memset(&addr, 0, sizeof(addr));
1447 addr.sun_family = AF_UNIX;
1449 memcpy(addr.sun_path, BLUEZ_AUDIO_SK_PATH,
1450 sizeof(BLUEZ_AUDIO_SK_PATH));
1452 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1454 error("audio: Failed to bind socket: %d (%s)", err,
1459 if (listen(sk, 1) < 0) {
1461 error("audio: Failed to listen on the socket: %d (%s)", err,
1468 err = pthread_create(&ipc_th, NULL, ipc_handler, NULL);
1472 error("audio: Failed to start Audio IPC thread: %d (%s)",
1473 err, strerror(err));
1484 static int audio_open(const hw_module_t *module, const char *name,
1485 hw_device_t **device)
1487 struct a2dp_audio_dev *a2dp_dev;
1492 if (strcmp(name, AUDIO_HARDWARE_INTERFACE)) {
1493 error("audio: interface %s not matching [%s]", name,
1494 AUDIO_HARDWARE_INTERFACE);
1498 err = audio_ipc_init();
1502 a2dp_dev = calloc(1, sizeof(struct a2dp_audio_dev));
1506 a2dp_dev->dev.common.tag = HARDWARE_DEVICE_TAG;
1507 a2dp_dev->dev.common.version = AUDIO_DEVICE_API_VERSION_CURRENT;
1508 a2dp_dev->dev.common.module = (struct hw_module_t *) module;
1509 a2dp_dev->dev.common.close = audio_close;
1511 a2dp_dev->dev.init_check = audio_init_check;
1512 a2dp_dev->dev.set_voice_volume = audio_set_voice_volume;
1513 a2dp_dev->dev.set_master_volume = audio_set_master_volume;
1514 a2dp_dev->dev.set_mode = audio_set_mode;
1515 a2dp_dev->dev.set_mic_mute = audio_set_mic_mute;
1516 a2dp_dev->dev.get_mic_mute = audio_get_mic_mute;
1517 a2dp_dev->dev.set_parameters = audio_set_parameters;
1518 a2dp_dev->dev.get_parameters = audio_get_parameters;
1519 a2dp_dev->dev.get_input_buffer_size = audio_get_input_buffer_size;
1520 a2dp_dev->dev.open_output_stream = audio_open_output_stream;
1521 a2dp_dev->dev.close_output_stream = audio_close_output_stream;
1522 a2dp_dev->dev.open_input_stream = audio_open_input_stream;
1523 a2dp_dev->dev.close_input_stream = audio_close_input_stream;
1524 a2dp_dev->dev.dump = audio_dump;
1526 /* Note that &a2dp_dev->dev.common is the same pointer as a2dp_dev.
1527 * This results from the structure of following structs:a2dp_audio_dev,
1528 * audio_hw_device. We will rely on this later in the code.*/
1529 *device = &a2dp_dev->dev.common;
1534 static struct hw_module_methods_t hal_module_methods = {
1538 struct audio_module HAL_MODULE_INFO_SYM = {
1540 .tag = HARDWARE_MODULE_TAG,
1543 .id = AUDIO_HARDWARE_MODULE_ID,
1544 .name = "A2DP Bluez HW HAL",
1545 .author = "Intel Corporation",
1546 .methods = &hal_module_methods,