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);
271 count = sizeof(sbc_presets) / sizeof(sbc_presets[0]);
273 for (i = 0; i < count; i++) {
274 preset = (struct audio_preset *) ptr;
276 if (new_len + preset_size > *len)
279 preset->len = sizeof(a2dp_sbc_t);
280 memcpy(preset->data, &sbc_presets[i], preset->len);
282 new_len += preset_size;
291 static void sbc_init_encoder(struct sbc_data *sbc_data)
293 a2dp_sbc_t *in = &sbc_data->sbc;
294 sbc_t *out = &sbc_data->enc;
298 sbc_init_a2dp(out, 0L, in, sizeof(*in));
300 out->endian = SBC_LE;
301 out->bitpool = in->max_bitpool;
304 static int sbc_codec_init(struct audio_preset *preset, uint16_t mtu,
307 struct sbc_data *sbc_data;
308 size_t hdr_len = sizeof(struct media_packet);
310 size_t out_frame_len;
315 if (preset->len != sizeof(a2dp_sbc_t)) {
316 DBG("preset size mismatch");
317 return AUDIO_STATUS_FAILED;
320 sbc_data = calloc(sizeof(struct sbc_data), 1);
322 memcpy(&sbc_data->sbc, preset->data, preset->len);
324 sbc_init_encoder(sbc_data);
326 in_frame_len = sbc_get_codesize(&sbc_data->enc);
327 out_frame_len = sbc_get_frame_length(&sbc_data->enc);
328 num_frames = (mtu - hdr_len) / out_frame_len;
330 sbc_data->in_frame_len = in_frame_len;
331 sbc_data->in_buf_size = num_frames * in_frame_len;
333 sbc_data->out_buf_size = hdr_len + num_frames * out_frame_len;
334 sbc_data->out_buf = calloc(1, sbc_data->out_buf_size);
336 sbc_data->frame_duration = sbc_get_frame_duration(&sbc_data->enc);
337 sbc_data->frames_per_packet = num_frames;
339 *codec_data = sbc_data;
341 return AUDIO_STATUS_SUCCESS;
344 static int sbc_cleanup(void *codec_data)
346 struct sbc_data *sbc_data = (struct sbc_data *) codec_data;
350 sbc_finish(&sbc_data->enc);
351 free(sbc_data->out_buf);
354 return AUDIO_STATUS_SUCCESS;
357 static int sbc_get_config(void *codec_data, struct audio_input_config *config)
359 struct sbc_data *sbc_data = (struct sbc_data *) codec_data;
361 switch (sbc_data->sbc.frequency) {
362 case SBC_SAMPLING_FREQ_16000:
363 config->rate = 16000;
365 case SBC_SAMPLING_FREQ_32000:
366 config->rate = 32000;
368 case SBC_SAMPLING_FREQ_44100:
369 config->rate = 44100;
371 case SBC_SAMPLING_FREQ_48000:
372 config->rate = 48000;
375 return AUDIO_STATUS_FAILED;
377 config->channels = sbc_data->sbc.channel_mode == SBC_CHANNEL_MODE_MONO ?
378 AUDIO_CHANNEL_OUT_MONO :
379 AUDIO_CHANNEL_OUT_STEREO;
380 config->format = AUDIO_FORMAT_PCM_16_BIT;
382 return AUDIO_STATUS_SUCCESS;
385 static size_t sbc_get_buffer_size(void *codec_data)
387 struct sbc_data *sbc_data = (struct sbc_data *) codec_data;
391 return sbc_data->in_buf_size;
394 static size_t sbc_get_mediapacket_duration(void *codec_data)
396 struct sbc_data *sbc_data = (struct sbc_data *) codec_data;
400 return sbc_data->frame_duration * sbc_data->frames_per_packet;
403 static void sbc_resume(void *codec_data)
405 struct sbc_data *sbc_data = (struct sbc_data *) codec_data;
409 clock_gettime(CLOCK_MONOTONIC, &sbc_data->start);
411 sbc_data->frames_sent = 0;
414 static void write_media_packet(int fd, struct sbc_data *sbc_data,
415 struct media_packet *mp, size_t data_len)
418 struct timespec diff;
419 unsigned expected_frames;
422 ret = write(fd, mp, sizeof(*mp) + data_len);
425 DBG("error writing data: %d (%s)", err,
429 sbc_data->frames_sent += mp->payload.frame_count;
431 clock_gettime(CLOCK_MONOTONIC, &cur);
432 timespec_diff(&cur, &sbc_data->start, &diff);
433 expected_frames = (diff.tv_sec * 1000000 + diff.tv_nsec / 1000) /
434 sbc_data->frame_duration;
436 /* AudioFlinger does not seem to provide any *working*
437 * API to provide data in some interval and will just
438 * send another buffer as soon as we process current
439 * one. To prevent overflowing L2CAP socket, we need to
440 * introduce some artificial delay here base on how many
441 * audio frames were sent so far, i.e. if we're not
442 * lagging behind audio stream, we can sleep for
443 * duration of single media packet.
445 if (sbc_data->frames_sent >= expected_frames)
446 usleep(sbc_data->frame_duration *
447 mp->payload.frame_count);
450 static ssize_t sbc_write_data(void *codec_data, const void *buffer,
451 size_t bytes, int fd)
453 struct sbc_data *sbc_data = (struct sbc_data *) codec_data;
456 struct media_packet *mp = (struct media_packet *) sbc_data->out_buf;
457 size_t free_space = sbc_data->out_buf_size - sizeof(*mp);
462 mp->hdr.sequence_number = htons(sbc_data->seq++);
463 mp->hdr.ssrc = htonl(1);
464 mp->payload.frame_count = 0;
466 while (bytes - consumed >= sbc_data->in_frame_len) {
469 ret = sbc_encode(&sbc_data->enc, buffer + consumed,
470 sbc_data->in_frame_len,
471 mp->data + encoded, free_space,
475 DBG("failed to encode block");
479 mp->payload.frame_count++;
483 free_space -= written;
485 /* write data if we either filled media packed or encoded all
488 if (mp->payload.frame_count == sbc_data->frames_per_packet ||
490 write_media_packet(fd, sbc_data, mp, encoded);
493 free_space = sbc_data->out_buf_size - sizeof(*mp);
494 mp->payload.frame_count = 0;
498 if (consumed != bytes) {
499 /* we should encode all input data
500 * if we did not, something went wrong but we can't really
501 * handle this so this is just sanity check
503 DBG("some data were not encoded");
506 /* we always assume that all data was processed and sent */
510 static int audio_ipc_cmd(uint8_t service_id, uint8_t opcode, uint16_t len,
511 void *param, size_t *rsp_len, void *rsp, int *fd)
517 char cmsgbuf[CMSG_SPACE(sizeof(int))];
519 size_t s_len = sizeof(s);
521 pthread_mutex_lock(&sk_mutex);
524 error("audio: Invalid cmd socket passed to audio_ipc_cmd");
528 if (!rsp || !rsp_len) {
529 memset(&s, 0, s_len);
534 memset(&msg, 0, sizeof(msg));
535 memset(&cmd, 0, sizeof(cmd));
537 cmd.service_id = service_id;
541 iv[0].iov_base = &cmd;
542 iv[0].iov_len = sizeof(cmd);
544 iv[1].iov_base = param;
550 ret = sendmsg(audio_sk, &msg, 0);
552 error("audio: Sending command failed:%s", strerror(errno));
556 /* socket was shutdown */
558 error("audio: Command socket closed");
562 memset(&msg, 0, sizeof(msg));
563 memset(&cmd, 0, sizeof(cmd));
565 iv[0].iov_base = &cmd;
566 iv[0].iov_len = sizeof(cmd);
568 iv[1].iov_base = rsp;
569 iv[1].iov_len = *rsp_len;
575 memset(cmsgbuf, 0, sizeof(cmsgbuf));
576 msg.msg_control = cmsgbuf;
577 msg.msg_controllen = sizeof(cmsgbuf);
580 ret = recvmsg(audio_sk, &msg, 0);
582 error("audio: Receiving command response failed:%s",
587 if (ret < (ssize_t) sizeof(cmd)) {
588 error("audio: Too small response received(%zd bytes)", ret);
592 if (cmd.service_id != service_id) {
593 error("audio: Invalid service id (%u vs %u)", cmd.service_id,
598 if (ret != (ssize_t) (sizeof(cmd) + cmd.len)) {
599 error("audio: Malformed response received(%zd bytes)", ret);
603 if (cmd.opcode != opcode && cmd.opcode != AUDIO_OP_STATUS) {
604 error("audio: Invalid opcode received (%u vs %u)",
609 if (cmd.opcode == AUDIO_OP_STATUS) {
610 struct hal_status *s = rsp;
612 if (sizeof(*s) != cmd.len) {
613 error("audio: Invalid status length");
617 if (s->code == AUDIO_STATUS_SUCCESS) {
618 error("audio: Invalid success status response");
622 pthread_mutex_unlock(&sk_mutex);
627 pthread_mutex_unlock(&sk_mutex);
629 /* Receive auxiliary data in msg */
631 struct cmsghdr *cmsg;
635 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
636 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
637 if (cmsg->cmsg_level == SOL_SOCKET
638 && cmsg->cmsg_type == SCM_RIGHTS) {
639 memcpy(fd, CMSG_DATA(cmsg), sizeof(int));
648 return AUDIO_STATUS_SUCCESS;
651 /* Some serious issue happen on IPC - recover */
652 shutdown(audio_sk, SHUT_RDWR);
653 pthread_mutex_unlock(&sk_mutex);
655 return AUDIO_STATUS_FAILED;
658 static int ipc_open_cmd(const struct audio_codec *codec)
660 uint8_t buf[BLUEZ_AUDIO_MTU];
661 struct audio_cmd_open *cmd = (struct audio_cmd_open *) buf;
662 struct audio_rsp_open rsp;
663 size_t cmd_len = sizeof(buf) - sizeof(*cmd);
664 size_t rsp_len = sizeof(rsp);
669 memcpy(cmd->uuid, a2dp_src_uuid, sizeof(a2dp_src_uuid));
671 cmd->codec = codec->type;
672 cmd->presets = codec->get_presets(cmd->preset, &cmd_len);
674 cmd_len += sizeof(*cmd);
676 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_OPEN, cmd_len, cmd,
677 &rsp_len, &rsp, NULL);
679 if (result != AUDIO_STATUS_SUCCESS)
685 static int ipc_close_cmd(uint8_t endpoint_id)
687 struct audio_cmd_close cmd;
692 cmd.id = endpoint_id;
694 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_CLOSE,
695 sizeof(cmd), &cmd, NULL, NULL, NULL);
700 static int ipc_open_stream_cmd(uint8_t endpoint_id, uint16_t *mtu, int *fd,
701 struct audio_preset **caps)
703 char buf[BLUEZ_AUDIO_MTU];
704 struct audio_cmd_open_stream cmd;
705 struct audio_rsp_open_stream *rsp =
706 (struct audio_rsp_open_stream *) &buf;
707 size_t rsp_len = sizeof(buf);
713 return AUDIO_STATUS_FAILED;
715 cmd.id = endpoint_id;
717 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_OPEN_STREAM,
718 sizeof(cmd), &cmd, &rsp_len, rsp, fd);
720 if (result == AUDIO_STATUS_SUCCESS) {
721 size_t buf_len = sizeof(struct audio_preset) +
724 *caps = malloc(buf_len);
725 memcpy(*caps, &rsp->preset, buf_len);
733 static int ipc_close_stream_cmd(uint8_t endpoint_id)
735 struct audio_cmd_close_stream cmd;
740 cmd.id = endpoint_id;
742 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_CLOSE_STREAM,
743 sizeof(cmd), &cmd, NULL, NULL, NULL);
748 static int ipc_resume_stream_cmd(uint8_t endpoint_id)
750 struct audio_cmd_resume_stream cmd;
755 cmd.id = endpoint_id;
757 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_RESUME_STREAM,
758 sizeof(cmd), &cmd, NULL, NULL, NULL);
763 static int ipc_suspend_stream_cmd(uint8_t endpoint_id)
765 struct audio_cmd_suspend_stream cmd;
770 cmd.id = endpoint_id;
772 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_SUSPEND_STREAM,
773 sizeof(cmd), &cmd, NULL, NULL, NULL);
778 static int register_endpoints(void)
780 struct audio_endpoint *ep = &audio_endpoints[0];
783 for (i = 0; i < NUM_CODECS; i++, ep++) {
784 const struct audio_codec *codec = &audio_codecs[i];
786 ep->id = ipc_open_cmd(codec);
789 return AUDIO_STATUS_FAILED;
792 ep->codec_data = NULL;
796 return AUDIO_STATUS_SUCCESS;
799 static void unregister_endpoints(void)
803 for (i = 0; i < MAX_AUDIO_ENDPOINTS; i++) {
804 struct audio_endpoint *ep = &audio_endpoints[i];
807 ipc_close_cmd(ep->id);
808 memset(ep, 0, sizeof(*ep));
813 static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
816 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
818 /* We can auto-start only from standby */
819 if (out->audio_state == AUDIO_A2DP_STATE_STANDBY) {
820 DBG("stream in standby, auto-start");
822 if (ipc_resume_stream_cmd(out->ep->id) != AUDIO_STATUS_SUCCESS)
825 out->ep->codec->resume(out->ep->codec_data);
827 out->audio_state = AUDIO_A2DP_STATE_STARTED;
830 if (out->audio_state != AUDIO_A2DP_STATE_STARTED) {
831 DBG("stream not started");
835 if (out->ep->fd < 0) {
840 return out->ep->codec->write_data(out->ep->codec_data, buffer,
844 static uint32_t out_get_sample_rate(const struct audio_stream *stream)
846 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
850 return out->cfg.rate;
853 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
855 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
859 if (rate != out->cfg.rate) {
860 DBG("cannot set sample rate to %d", rate);
867 static size_t out_get_buffer_size(const struct audio_stream *stream)
871 /* We should return proper buffer size calculated by codec (so each
872 * input buffer is encoded into single media packed) but this does not
873 * work well with AudioFlinger and causes problems. For this reason we
874 * use magic value here and out_write code takes care of splitting
875 * input buffer into multiple media packets.
880 static uint32_t out_get_channels(const struct audio_stream *stream)
882 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
886 return out->cfg.channels;
889 static audio_format_t out_get_format(const struct audio_stream *stream)
891 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
895 return out->cfg.format;
898 static int out_set_format(struct audio_stream *stream, audio_format_t format)
904 static int out_standby(struct audio_stream *stream)
906 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
910 if (out->audio_state == AUDIO_A2DP_STATE_STARTED) {
911 if (ipc_suspend_stream_cmd(out->ep->id) != AUDIO_STATUS_SUCCESS)
913 out->audio_state = AUDIO_A2DP_STATE_STANDBY;
919 static int out_dump(const struct audio_stream *stream, int fd)
925 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
927 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
931 bool enter_suspend = false;
932 bool exit_suspend = false;
936 str = strdup(kvpairs);
937 kvpair = strtok_r(str, ";", &saveptr);
939 for (; kvpair && *kvpair; kvpair = strtok_r(NULL, ";", &saveptr)) {
942 keyval = strchr(kvpair, '=');
949 if (!strcmp(kvpair, "closing")) {
950 if (!strcmp(keyval, "true"))
951 out->audio_state = AUDIO_A2DP_STATE_NONE;
952 } else if (!strcmp(kvpair, "A2dpSuspended")) {
953 if (!strcmp(keyval, "true"))
954 enter_suspend = true;
962 if (enter_suspend && out->audio_state == AUDIO_A2DP_STATE_STARTED) {
963 if (ipc_suspend_stream_cmd(out->ep->id) != AUDIO_STATUS_SUCCESS)
965 out->audio_state = AUDIO_A2DP_STATE_SUSPENDED;
968 if (exit_suspend && out->audio_state == AUDIO_A2DP_STATE_SUSPENDED)
969 out->audio_state = AUDIO_A2DP_STATE_STANDBY;
974 static char *out_get_parameters(const struct audio_stream *stream,
981 static uint32_t out_get_latency(const struct audio_stream_out *stream)
983 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
984 struct audio_endpoint *ep = out->ep;
989 pkt_duration = ep->codec->get_mediapacket_duration(ep->codec_data);
991 return FIXED_A2DP_PLAYBACK_LATENCY_MS + pkt_duration / 1000;
994 static int out_set_volume(struct audio_stream_out *stream, float left,
998 /* volume controlled in audioflinger mixer (digital) */
1002 static int out_get_render_position(const struct audio_stream_out *stream,
1003 uint32_t *dsp_frames)
1009 static int out_add_audio_effect(const struct audio_stream *stream,
1010 effect_handle_t effect)
1016 static int out_remove_audio_effect(const struct audio_stream *stream,
1017 effect_handle_t effect)
1023 static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1029 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1035 static size_t in_get_buffer_size(const struct audio_stream *stream)
1041 static uint32_t in_get_channels(const struct audio_stream *stream)
1047 static audio_format_t in_get_format(const struct audio_stream *stream)
1053 static int in_set_format(struct audio_stream *stream, audio_format_t format)
1059 static int in_standby(struct audio_stream *stream)
1065 static int in_dump(const struct audio_stream *stream, int fd)
1071 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1077 static char *in_get_parameters(const struct audio_stream *stream,
1084 static int in_set_gain(struct audio_stream_in *stream, float gain)
1090 static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1097 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1103 static int in_add_audio_effect(const struct audio_stream *stream,
1104 effect_handle_t effect)
1110 static int in_remove_audio_effect(const struct audio_stream *stream,
1111 effect_handle_t effect)
1117 static int audio_open_output_stream(struct audio_hw_device *dev,
1118 audio_io_handle_t handle,
1119 audio_devices_t devices,
1120 audio_output_flags_t flags,
1121 struct audio_config *config,
1122 struct audio_stream_out **stream_out)
1125 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
1126 struct a2dp_stream_out *out;
1127 struct audio_preset *preset;
1128 const struct audio_codec *codec;
1132 out = calloc(1, sizeof(struct a2dp_stream_out));
1138 out->stream.common.get_sample_rate = out_get_sample_rate;
1139 out->stream.common.set_sample_rate = out_set_sample_rate;
1140 out->stream.common.get_buffer_size = out_get_buffer_size;
1141 out->stream.common.get_channels = out_get_channels;
1142 out->stream.common.get_format = out_get_format;
1143 out->stream.common.set_format = out_set_format;
1144 out->stream.common.standby = out_standby;
1145 out->stream.common.dump = out_dump;
1146 out->stream.common.set_parameters = out_set_parameters;
1147 out->stream.common.get_parameters = out_get_parameters;
1148 out->stream.common.add_audio_effect = out_add_audio_effect;
1149 out->stream.common.remove_audio_effect = out_remove_audio_effect;
1150 out->stream.get_latency = out_get_latency;
1151 out->stream.set_volume = out_set_volume;
1152 out->stream.write = out_write;
1153 out->stream.get_render_position = out_get_render_position;
1155 /* TODO: for now we always use endpoint 0 */
1156 out->ep = &audio_endpoints[0];
1158 if (ipc_open_stream_cmd(out->ep->id, &mtu, &fd, &preset) !=
1159 AUDIO_STATUS_SUCCESS)
1162 if (!preset || fd < 0)
1167 codec = out->ep->codec;
1169 codec->init(preset, mtu, &out->ep->codec_data);
1170 codec->get_config(out->ep->codec_data, &out->cfg);
1172 DBG("rate=%d channels=%d format=%d", out->cfg.rate,
1173 out->cfg.channels, out->cfg.format);
1177 *stream_out = &out->stream;
1178 a2dp_dev->out = out;
1180 out->audio_state = AUDIO_A2DP_STATE_STANDBY;
1190 static void audio_close_output_stream(struct audio_hw_device *dev,
1191 struct audio_stream_out *stream)
1193 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
1194 struct audio_endpoint *ep = a2dp_dev->out->ep;
1198 ipc_close_stream_cmd(ep->id);
1205 ep->codec->cleanup(ep->codec_data);
1206 ep->codec_data = NULL;
1209 a2dp_dev->out = NULL;
1212 static int audio_set_parameters(struct audio_hw_device *dev,
1213 const char *kvpairs)
1215 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
1216 struct a2dp_stream_out *out = a2dp_dev->out;
1223 return out->stream.common.set_parameters((struct audio_stream *) out,
1227 static char *audio_get_parameters(const struct audio_hw_device *dev,
1234 static int audio_init_check(const struct audio_hw_device *dev)
1240 static int audio_set_voice_volume(struct audio_hw_device *dev, float volume)
1246 static int audio_set_master_volume(struct audio_hw_device *dev, float volume)
1252 static int audio_set_mode(struct audio_hw_device *dev, int mode)
1258 static int audio_set_mic_mute(struct audio_hw_device *dev, bool state)
1264 static int audio_get_mic_mute(const struct audio_hw_device *dev, bool *state)
1270 static size_t audio_get_input_buffer_size(const struct audio_hw_device *dev,
1271 const struct audio_config *config)
1277 static int audio_open_input_stream(struct audio_hw_device *dev,
1278 audio_io_handle_t handle,
1279 audio_devices_t devices,
1280 struct audio_config *config,
1281 struct audio_stream_in **stream_in)
1283 struct audio_stream_in *in;
1287 in = calloc(1, sizeof(struct audio_stream_in));
1291 in->common.get_sample_rate = in_get_sample_rate;
1292 in->common.set_sample_rate = in_set_sample_rate;
1293 in->common.get_buffer_size = in_get_buffer_size;
1294 in->common.get_channels = in_get_channels;
1295 in->common.get_format = in_get_format;
1296 in->common.set_format = in_set_format;
1297 in->common.standby = in_standby;
1298 in->common.dump = in_dump;
1299 in->common.set_parameters = in_set_parameters;
1300 in->common.get_parameters = in_get_parameters;
1301 in->common.add_audio_effect = in_add_audio_effect;
1302 in->common.remove_audio_effect = in_remove_audio_effect;
1303 in->set_gain = in_set_gain;
1305 in->get_input_frames_lost = in_get_input_frames_lost;
1312 static void audio_close_input_stream(struct audio_hw_device *dev,
1313 struct audio_stream_in *stream_in)
1319 static int audio_dump(const audio_hw_device_t *device, int fd)
1325 static int audio_close(hw_device_t *device)
1327 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *)device;
1331 unregister_endpoints();
1333 shutdown(listen_sk, SHUT_RDWR);
1334 shutdown(audio_sk, SHUT_RDWR);
1336 pthread_join(ipc_th, NULL);
1345 static void *ipc_handler(void *data)
1354 DBG("Waiting for connection ...");
1356 sk = accept(listen_sk, NULL, NULL);
1363 if (err != ECONNABORTED && err != EINVAL)
1364 error("audio: Failed to accept socket: %d (%s)",
1365 err, strerror(err));
1370 pthread_mutex_lock(&sk_mutex);
1372 pthread_mutex_unlock(&sk_mutex);
1374 DBG("Audio IPC: Connected");
1376 if (register_endpoints() != AUDIO_STATUS_SUCCESS) {
1377 error("audio: Failed to register endpoints");
1379 unregister_endpoints();
1381 pthread_mutex_lock(&sk_mutex);
1382 shutdown(audio_sk, SHUT_RDWR);
1385 pthread_mutex_unlock(&sk_mutex);
1390 memset(&pfd, 0, sizeof(pfd));
1392 pfd.events = POLLHUP | POLLERR | POLLNVAL;
1394 /* Check if socket is still alive. Empty while loop.*/
1395 while (poll(&pfd, 1, -1) < 0 && errno == EINTR);
1397 if (pfd.revents & (POLLHUP | POLLERR | POLLNVAL)) {
1398 info("Audio HAL: Socket closed");
1400 pthread_mutex_lock(&sk_mutex);
1403 pthread_mutex_unlock(&sk_mutex);
1407 /* audio_sk is closed at this point, just cleanup endpoints states */
1408 memset(audio_endpoints, 0, sizeof(audio_endpoints));
1410 info("Closing Audio IPC thread");
1414 static int audio_ipc_init(void)
1416 struct sockaddr_un addr;
1422 sk = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
1425 error("audio: Failed to create socket: %d (%s)", err,
1430 memset(&addr, 0, sizeof(addr));
1431 addr.sun_family = AF_UNIX;
1433 memcpy(addr.sun_path, BLUEZ_AUDIO_SK_PATH,
1434 sizeof(BLUEZ_AUDIO_SK_PATH));
1436 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1438 error("audio: Failed to bind socket: %d (%s)", err,
1443 if (listen(sk, 1) < 0) {
1445 error("audio: Failed to listen on the socket: %d (%s)", err,
1452 err = pthread_create(&ipc_th, NULL, ipc_handler, NULL);
1456 error("audio: Failed to start Audio IPC thread: %d (%s)",
1457 err, strerror(err));
1468 static int audio_open(const hw_module_t *module, const char *name,
1469 hw_device_t **device)
1471 struct a2dp_audio_dev *a2dp_dev;
1476 if (strcmp(name, AUDIO_HARDWARE_INTERFACE)) {
1477 error("audio: interface %s not matching [%s]", name,
1478 AUDIO_HARDWARE_INTERFACE);
1482 err = audio_ipc_init();
1486 a2dp_dev = calloc(1, sizeof(struct a2dp_audio_dev));
1490 a2dp_dev->dev.common.tag = HARDWARE_DEVICE_TAG;
1491 a2dp_dev->dev.common.version = AUDIO_DEVICE_API_VERSION_CURRENT;
1492 a2dp_dev->dev.common.module = (struct hw_module_t *) module;
1493 a2dp_dev->dev.common.close = audio_close;
1495 a2dp_dev->dev.init_check = audio_init_check;
1496 a2dp_dev->dev.set_voice_volume = audio_set_voice_volume;
1497 a2dp_dev->dev.set_master_volume = audio_set_master_volume;
1498 a2dp_dev->dev.set_mode = audio_set_mode;
1499 a2dp_dev->dev.set_mic_mute = audio_set_mic_mute;
1500 a2dp_dev->dev.get_mic_mute = audio_get_mic_mute;
1501 a2dp_dev->dev.set_parameters = audio_set_parameters;
1502 a2dp_dev->dev.get_parameters = audio_get_parameters;
1503 a2dp_dev->dev.get_input_buffer_size = audio_get_input_buffer_size;
1504 a2dp_dev->dev.open_output_stream = audio_open_output_stream;
1505 a2dp_dev->dev.close_output_stream = audio_close_output_stream;
1506 a2dp_dev->dev.open_input_stream = audio_open_input_stream;
1507 a2dp_dev->dev.close_input_stream = audio_close_input_stream;
1508 a2dp_dev->dev.dump = audio_dump;
1510 /* Note that &a2dp_dev->dev.common is the same pointer as a2dp_dev.
1511 * This results from the structure of following structs:a2dp_audio_dev,
1512 * audio_hw_device. We will rely on this later in the code.*/
1513 *device = &a2dp_dev->dev.common;
1518 static struct hw_module_methods_t hal_module_methods = {
1522 struct audio_module HAL_MODULE_INFO_SYM = {
1524 .tag = HARDWARE_MODULE_TAG,
1527 .id = AUDIO_HARDWARE_MODULE_ID,
1528 .name = "A2DP Bluez HW HAL",
1529 .author = "Intel Corporation",
1530 .methods = &hal_module_methods,