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>
33 #include "audio-msg.h"
34 #include "ipc-common.h"
37 #include "hal-audio.h"
38 #include "../src/shared/util.h"
40 #define FIXED_A2DP_PLAYBACK_LATENCY_MS 25
42 #define FIXED_BUFFER_SIZE (20 * 512)
44 #define MAX_DELAY 100000 /* 100ms */
46 static const uint8_t a2dp_src_uuid[] = {
47 0x00, 0x00, 0x11, 0x0a, 0x00, 0x00, 0x10, 0x00,
48 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb };
50 static int listen_sk = -1;
51 static int audio_sk = -1;
53 static pthread_t ipc_th = 0;
54 static pthread_mutex_t sk_mutex = PTHREAD_MUTEX_INITIALIZER;
56 static void timespec_add(struct timespec *base, uint64_t time_us,
59 res->tv_sec = base->tv_sec + time_us / 1000000;
60 res->tv_nsec = base->tv_nsec + (time_us % 1000000) * 1000;
62 if (res->tv_nsec >= 1000000000) {
64 res->tv_nsec -= 1000000000;
68 static void timespec_diff(struct timespec *a, struct timespec *b,
71 res->tv_sec = a->tv_sec - b->tv_sec;
72 res->tv_nsec = a->tv_nsec - b->tv_nsec;
74 if (res->tv_nsec < 0) {
76 res->tv_nsec += 1000000000; /* 1sec */
80 static uint64_t timespec_diff_us(struct timespec *a, struct timespec *b)
84 timespec_diff(a, b, &res);
86 return res.tv_sec * 1000000ll + res.tv_nsec / 1000ll;
91 * Bionic does not have clock_nanosleep() prototype in time.h even though
92 * it provides its implementation.
94 extern int clock_nanosleep(clockid_t clock_id, int flags,
95 const struct timespec *request,
96 struct timespec *remain);
99 static const audio_codec_get_t audio_codecs[] = {
103 #define NUM_CODECS (sizeof(audio_codecs) / sizeof(audio_codecs[0]))
105 #define MAX_AUDIO_ENDPOINTS NUM_CODECS
107 struct audio_endpoint {
109 const struct audio_codec *codec;
113 struct media_packet *mp;
118 struct timespec start;
123 static struct audio_endpoint audio_endpoints[MAX_AUDIO_ENDPOINTS];
126 AUDIO_A2DP_STATE_NONE,
127 AUDIO_A2DP_STATE_STANDBY,
128 AUDIO_A2DP_STATE_SUSPENDED,
129 AUDIO_A2DP_STATE_STARTED
132 struct a2dp_stream_out {
133 struct audio_stream_out stream;
135 struct audio_endpoint *ep;
136 enum a2dp_state_t audio_state;
137 struct audio_input_config cfg;
139 uint8_t *downmix_buf;
142 struct a2dp_audio_dev {
143 struct audio_hw_device dev;
144 struct a2dp_stream_out *out;
147 static int audio_ipc_cmd(uint8_t service_id, uint8_t opcode, uint16_t len,
148 void *param, size_t *rsp_len, void *rsp, int *fd)
154 char cmsgbuf[CMSG_SPACE(sizeof(int))];
156 size_t s_len = sizeof(s);
158 pthread_mutex_lock(&sk_mutex);
161 error("audio: Invalid cmd socket passed to audio_ipc_cmd");
165 if (!rsp || !rsp_len) {
166 memset(&s, 0, s_len);
171 memset(&msg, 0, sizeof(msg));
172 memset(&cmd, 0, sizeof(cmd));
174 cmd.service_id = service_id;
178 iv[0].iov_base = &cmd;
179 iv[0].iov_len = sizeof(cmd);
181 iv[1].iov_base = param;
187 ret = sendmsg(audio_sk, &msg, 0);
189 error("audio: Sending command failed:%s", strerror(errno));
193 /* socket was shutdown */
195 error("audio: Command socket closed");
199 memset(&msg, 0, sizeof(msg));
200 memset(&cmd, 0, sizeof(cmd));
202 iv[0].iov_base = &cmd;
203 iv[0].iov_len = sizeof(cmd);
205 iv[1].iov_base = rsp;
206 iv[1].iov_len = *rsp_len;
212 memset(cmsgbuf, 0, sizeof(cmsgbuf));
213 msg.msg_control = cmsgbuf;
214 msg.msg_controllen = sizeof(cmsgbuf);
217 ret = recvmsg(audio_sk, &msg, 0);
219 error("audio: Receiving command response failed:%s",
224 if (ret < (ssize_t) sizeof(cmd)) {
225 error("audio: Too small response received(%zd bytes)", ret);
229 if (cmd.service_id != service_id) {
230 error("audio: Invalid service id (%u vs %u)", cmd.service_id,
235 if (ret != (ssize_t) (sizeof(cmd) + cmd.len)) {
236 error("audio: Malformed response received(%zd bytes)", ret);
240 if (cmd.opcode != opcode && cmd.opcode != AUDIO_OP_STATUS) {
241 error("audio: Invalid opcode received (%u vs %u)",
246 if (cmd.opcode == AUDIO_OP_STATUS) {
247 struct ipc_status *s = rsp;
249 if (sizeof(*s) != cmd.len) {
250 error("audio: Invalid status length");
254 if (s->code == AUDIO_STATUS_SUCCESS) {
255 error("audio: Invalid success status response");
259 pthread_mutex_unlock(&sk_mutex);
264 pthread_mutex_unlock(&sk_mutex);
266 /* Receive auxiliary data in msg */
268 struct cmsghdr *cmsg;
272 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
273 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
274 if (cmsg->cmsg_level == SOL_SOCKET
275 && cmsg->cmsg_type == SCM_RIGHTS) {
276 memcpy(fd, CMSG_DATA(cmsg), sizeof(int));
288 return AUDIO_STATUS_SUCCESS;
291 /* Some serious issue happen on IPC - recover */
292 shutdown(audio_sk, SHUT_RDWR);
293 pthread_mutex_unlock(&sk_mutex);
295 return AUDIO_STATUS_FAILED;
298 static int ipc_open_cmd(const struct audio_codec *codec)
300 uint8_t buf[BLUEZ_AUDIO_MTU];
301 struct audio_cmd_open *cmd = (struct audio_cmd_open *) buf;
302 struct audio_rsp_open rsp;
303 size_t cmd_len = sizeof(buf) - sizeof(*cmd);
304 size_t rsp_len = sizeof(rsp);
309 memcpy(cmd->uuid, a2dp_src_uuid, sizeof(a2dp_src_uuid));
311 cmd->codec = codec->type;
312 cmd->presets = codec->get_presets(cmd->preset, &cmd_len);
314 cmd_len += sizeof(*cmd);
316 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_OPEN, cmd_len, cmd,
317 &rsp_len, &rsp, NULL);
319 if (result != AUDIO_STATUS_SUCCESS)
325 static int ipc_close_cmd(uint8_t endpoint_id)
327 struct audio_cmd_close cmd;
332 cmd.id = endpoint_id;
334 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_CLOSE,
335 sizeof(cmd), &cmd, NULL, NULL, NULL);
340 static int ipc_open_stream_cmd(uint8_t endpoint_id, uint16_t *mtu, int *fd,
341 struct audio_preset **caps)
343 char buf[BLUEZ_AUDIO_MTU];
344 struct audio_cmd_open_stream cmd;
345 struct audio_rsp_open_stream *rsp =
346 (struct audio_rsp_open_stream *) &buf;
347 size_t rsp_len = sizeof(buf);
353 return AUDIO_STATUS_FAILED;
355 cmd.id = endpoint_id;
357 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_OPEN_STREAM,
358 sizeof(cmd), &cmd, &rsp_len, rsp, fd);
359 if (result == AUDIO_STATUS_SUCCESS) {
360 size_t buf_len = sizeof(struct audio_preset) +
363 *caps = malloc(buf_len);
364 memcpy(*caps, &rsp->preset, buf_len);
372 static int ipc_close_stream_cmd(uint8_t endpoint_id)
374 struct audio_cmd_close_stream cmd;
379 cmd.id = endpoint_id;
381 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_CLOSE_STREAM,
382 sizeof(cmd), &cmd, NULL, NULL, NULL);
387 static int ipc_resume_stream_cmd(uint8_t endpoint_id)
389 struct audio_cmd_resume_stream cmd;
394 cmd.id = endpoint_id;
396 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_RESUME_STREAM,
397 sizeof(cmd), &cmd, NULL, NULL, NULL);
402 static int ipc_suspend_stream_cmd(uint8_t endpoint_id)
404 struct audio_cmd_suspend_stream cmd;
409 cmd.id = endpoint_id;
411 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_SUSPEND_STREAM,
412 sizeof(cmd), &cmd, NULL, NULL, NULL);
417 static int register_endpoints(void)
419 struct audio_endpoint *ep = &audio_endpoints[0];
422 for (i = 0; i < NUM_CODECS; i++, ep++) {
423 const struct audio_codec *codec = audio_codecs[i]();
426 return AUDIO_STATUS_FAILED;
428 ep->id = ipc_open_cmd(codec);
431 return AUDIO_STATUS_FAILED;
434 ep->codec_data = NULL;
438 return AUDIO_STATUS_SUCCESS;
441 static void unregister_endpoints(void)
445 for (i = 0; i < MAX_AUDIO_ENDPOINTS; i++) {
446 struct audio_endpoint *ep = &audio_endpoints[i];
449 ipc_close_cmd(ep->id);
450 memset(ep, 0, sizeof(*ep));
455 static bool open_endpoint(struct audio_endpoint *ep,
456 struct audio_input_config *cfg)
458 struct audio_preset *preset;
459 const struct audio_codec *codec;
461 uint16_t payload_len;
464 if (ipc_open_stream_cmd(ep->id, &mtu, &fd, &preset) !=
465 AUDIO_STATUS_SUCCESS)
471 if (ep->codec->use_rtp)
472 payload_len -= sizeof(struct rtp_header);
477 codec->init(preset, payload_len, &ep->codec_data);
478 codec->get_config(ep->codec_data, cfg);
480 ep->mp = calloc(mtu, 1);
484 if (ep->codec->use_rtp) {
485 struct media_packet_rtp *mp_rtp =
486 (struct media_packet_rtp *) ep->mp;
488 mp_rtp->hdr.pt = 0x60;
489 mp_rtp->hdr.ssrc = htonl(1);
492 ep->mp_data_len = payload_len;
505 static void close_endpoint(struct audio_endpoint *ep)
507 ipc_close_stream_cmd(ep->id);
515 ep->codec->cleanup(ep->codec_data);
516 ep->codec_data = NULL;
519 static bool resume_endpoint(struct audio_endpoint *ep)
521 if (ipc_resume_stream_cmd(ep->id) != AUDIO_STATUS_SUCCESS)
527 if (ep->codec->update_qos)
528 ep->codec->update_qos(ep->codec_data, QOS_POLICY_DEFAULT);
533 static void downmix_to_mono(struct a2dp_stream_out *out, const uint8_t *buffer,
536 const int16_t *input = (const void *) buffer;
537 int16_t *output = (void *) out->downmix_buf;
540 /* PCM 16bit stereo */
541 frames = bytes / (2 * sizeof(int16_t));
543 for (i = 0; i < frames; i++) {
544 int16_t l = le16_to_cpu(get_unaligned(&input[i * 2]));
545 int16_t r = le16_to_cpu(get_unaligned(&input[i * 2 + 1]));
547 put_unaligned(cpu_to_le16((l + r) / 2), &output[i]);
551 static bool wait_for_endpoint(struct audio_endpoint *ep, bool *writable)
556 struct pollfd pollfd;
559 pollfd.events = POLLOUT;
562 ret = poll(&pollfd, 1, 500);
565 *writable = !!(pollfd.revents & POLLOUT);
569 if (errno != EINTR) {
571 error("poll failed (%d)", ret);
579 static bool write_to_endpoint(struct audio_endpoint *ep, size_t bytes)
581 struct media_packet *mp = (struct media_packet *) ep->mp;
585 ret = write(ep->fd, mp, bytes);
591 * this should not happen so let's issue warning, but do not
592 * fail, we can try to write next packet
594 if (errno == EAGAIN) {
596 warn("write failed (%d)", ret);
600 if (errno != EINTR) {
602 error("write failed (%d)", ret);
610 static bool write_data(struct a2dp_stream_out *out, const void *buffer,
613 struct audio_endpoint *ep = out->ep;
614 struct media_packet *mp = (struct media_packet *) ep->mp;
615 struct media_packet_rtp *mp_rtp = (struct media_packet_rtp *) ep->mp;
616 size_t free_space = ep->mp_data_len;
619 while (consumed < bytes) {
624 struct timespec current;
625 uint64_t audio_sent, audio_passed;
626 bool do_write = false;
629 * prepare media packet in advance so we don't waste time after
632 if (ep->codec->use_rtp) {
633 mp_rtp->hdr.sequence_number = htons(ep->seq++);
634 mp_rtp->hdr.timestamp = htonl(ep->samples);
636 read = ep->codec->encode_mediapacket(ep->codec_data,
638 bytes - consumed, mp,
639 free_space, &written);
642 * not much we can do here, let's just ignore remaining
648 /* calculate where are we and where we should be */
649 clock_gettime(CLOCK_MONOTONIC, ¤t);
651 memcpy(&ep->start, ¤t, sizeof(ep->start));
652 audio_sent = ep->samples * 1000000ll / out->cfg.rate;
653 audio_passed = timespec_diff_us(¤t, &ep->start);
656 * if we're ahead of stream then wait for next write point,
657 * if we're lagging more than 100ms then stop writing and just
658 * skip data until we're back in sync
660 if (audio_sent > audio_passed) {
661 struct timespec anchor;
665 timespec_add(&ep->start, audio_sent, &anchor);
668 ret = clock_nanosleep(CLOCK_MONOTONIC,
669 TIMER_ABSTIME, &anchor,
676 error("clock_nanosleep failed (%d)",
681 } else if (!ep->resync) {
682 uint64_t diff = audio_passed - audio_sent;
684 if (diff > MAX_DELAY) {
685 warn("lag is %jums, resyncing", diff / 1000);
687 if (ep->codec->update_qos)
688 ep->codec->update_qos(ep->codec_data,
689 QOS_POLICY_DECREASE);
694 /* we send data only in case codec encoded some data, i.e. some
695 * codecs do internal buffering and output data only if full
696 * frame can be encoded
697 * in resync mode we'll just drop mediapackets
699 if (written > 0 && !ep->resync) {
700 /* wait some time for socket to be ready for write,
701 * but we'll just skip writing data if timeout occurs
703 if (!wait_for_endpoint(ep, &do_write))
707 if (ep->codec->use_rtp)
708 written += sizeof(struct rtp_header);
710 if (!write_to_endpoint(ep, written))
716 * AudioFlinger provides 16bit PCM, so sample size is 2 bytes
717 * multiplied by number of channels. Number of channels is
718 * simply number of bits set in channels mask.
720 samples = read / (2 * popcount(out->cfg.channels));
721 ep->samples += samples;
728 static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
731 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
732 const void *in_buf = buffer;
733 size_t in_len = bytes;
735 /* just return in case we're closing */
736 if (out->audio_state == AUDIO_A2DP_STATE_NONE)
739 /* We can auto-start only from standby */
740 if (out->audio_state == AUDIO_A2DP_STATE_STANDBY) {
741 DBG("stream in standby, auto-start");
743 if (!resume_endpoint(out->ep))
746 out->audio_state = AUDIO_A2DP_STATE_STARTED;
749 if (out->audio_state != AUDIO_A2DP_STATE_STARTED) {
750 error("audio: stream not started");
754 if (out->ep->fd < 0) {
755 error("audio: no transport socket");
760 * currently Android audioflinger is not able to provide mono stream on
761 * A2DP output so down mixing needs to be done in hal-audio plugin.
764 * AudioFlinger::PlaybackThread::readOutputParameters()
765 * frameworks/av/services/audioflinger/Threads.cpp:1631
767 if (out->cfg.channels == AUDIO_CHANNEL_OUT_MONO) {
768 if (!out->downmix_buf) {
769 error("audio: downmix buffer not initialized");
773 downmix_to_mono(out, buffer, bytes);
775 in_buf = out->downmix_buf;
779 if (!write_data(out, in_buf, in_len))
785 static uint32_t out_get_sample_rate(const struct audio_stream *stream)
787 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
791 return out->cfg.rate;
794 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
796 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
800 if (rate != out->cfg.rate) {
801 warn("audio: cannot set sample rate to %d", rate);
808 static size_t out_get_buffer_size(const struct audio_stream *stream)
813 * We should return proper buffer size calculated by codec (so each
814 * input buffer is encoded into single media packed) but this does not
815 * work well with AudioFlinger and causes problems. For this reason we
816 * use magic value here and out_write code takes care of splitting
817 * input buffer into multiple media packets.
819 return FIXED_BUFFER_SIZE;
822 static uint32_t out_get_channels(const struct audio_stream *stream)
827 * AudioFlinger can only provide stereo stream, so we return it here and
828 * later we'll downmix this to mono in case codec requires it
831 return AUDIO_CHANNEL_OUT_STEREO;
834 static audio_format_t out_get_format(const struct audio_stream *stream)
836 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
840 return out->cfg.format;
843 static int out_set_format(struct audio_stream *stream, audio_format_t format)
849 static int out_standby(struct audio_stream *stream)
851 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
855 if (out->audio_state == AUDIO_A2DP_STATE_STARTED) {
856 if (ipc_suspend_stream_cmd(out->ep->id) != AUDIO_STATUS_SUCCESS)
858 out->audio_state = AUDIO_A2DP_STATE_STANDBY;
864 static int out_dump(const struct audio_stream *stream, int fd)
870 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
872 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
876 bool enter_suspend = false;
877 bool exit_suspend = false;
881 str = strdup(kvpairs);
885 kvpair = strtok_r(str, ";", &saveptr);
887 for (; kvpair && *kvpair; kvpair = strtok_r(NULL, ";", &saveptr)) {
890 keyval = strchr(kvpair, '=');
897 if (!strcmp(kvpair, "closing")) {
898 if (!strcmp(keyval, "true"))
899 out->audio_state = AUDIO_A2DP_STATE_NONE;
900 } else if (!strcmp(kvpair, "A2dpSuspended")) {
901 if (!strcmp(keyval, "true"))
902 enter_suspend = true;
910 if (enter_suspend && 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_SUSPENDED;
916 if (exit_suspend && out->audio_state == AUDIO_A2DP_STATE_SUSPENDED)
917 out->audio_state = AUDIO_A2DP_STATE_STANDBY;
922 static char *out_get_parameters(const struct audio_stream *stream,
929 static uint32_t out_get_latency(const struct audio_stream_out *stream)
931 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
932 struct audio_endpoint *ep = out->ep;
937 pkt_duration = ep->codec->get_mediapacket_duration(ep->codec_data);
939 return FIXED_A2DP_PLAYBACK_LATENCY_MS + pkt_duration / 1000;
942 static int out_set_volume(struct audio_stream_out *stream, float left,
946 /* volume controlled in audioflinger mixer (digital) */
950 static int out_get_render_position(const struct audio_stream_out *stream,
951 uint32_t *dsp_frames)
957 static int out_add_audio_effect(const struct audio_stream *stream,
958 effect_handle_t effect)
964 static int out_remove_audio_effect(const struct audio_stream *stream,
965 effect_handle_t effect)
971 static uint32_t in_get_sample_rate(const struct audio_stream *stream)
977 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
983 static size_t in_get_buffer_size(const struct audio_stream *stream)
989 static uint32_t in_get_channels(const struct audio_stream *stream)
995 static audio_format_t in_get_format(const struct audio_stream *stream)
1001 static int in_set_format(struct audio_stream *stream, audio_format_t format)
1007 static int in_standby(struct audio_stream *stream)
1013 static int in_dump(const struct audio_stream *stream, int fd)
1019 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1025 static char *in_get_parameters(const struct audio_stream *stream,
1032 static int in_set_gain(struct audio_stream_in *stream, float gain)
1038 static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1045 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1051 static int in_add_audio_effect(const struct audio_stream *stream,
1052 effect_handle_t effect)
1058 static int in_remove_audio_effect(const struct audio_stream *stream,
1059 effect_handle_t effect)
1065 static int audio_open_output_stream(struct audio_hw_device *dev,
1066 audio_io_handle_t handle,
1067 audio_devices_t devices,
1068 audio_output_flags_t flags,
1069 struct audio_config *config,
1070 struct audio_stream_out **stream_out)
1073 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
1074 struct a2dp_stream_out *out;
1076 out = calloc(1, sizeof(struct a2dp_stream_out));
1082 out->stream.common.get_sample_rate = out_get_sample_rate;
1083 out->stream.common.set_sample_rate = out_set_sample_rate;
1084 out->stream.common.get_buffer_size = out_get_buffer_size;
1085 out->stream.common.get_channels = out_get_channels;
1086 out->stream.common.get_format = out_get_format;
1087 out->stream.common.set_format = out_set_format;
1088 out->stream.common.standby = out_standby;
1089 out->stream.common.dump = out_dump;
1090 out->stream.common.set_parameters = out_set_parameters;
1091 out->stream.common.get_parameters = out_get_parameters;
1092 out->stream.common.add_audio_effect = out_add_audio_effect;
1093 out->stream.common.remove_audio_effect = out_remove_audio_effect;
1094 out->stream.get_latency = out_get_latency;
1095 out->stream.set_volume = out_set_volume;
1096 out->stream.write = out_write;
1097 out->stream.get_render_position = out_get_render_position;
1099 /* TODO: for now we always use endpoint 0 */
1100 out->ep = &audio_endpoints[0];
1102 if (!open_endpoint(out->ep, &out->cfg))
1105 DBG("rate=%d channels=%d format=%d", out->cfg.rate,
1106 out->cfg.channels, out->cfg.format);
1108 if (out->cfg.channels == AUDIO_CHANNEL_OUT_MONO) {
1109 out->downmix_buf = malloc(FIXED_BUFFER_SIZE / 2);
1110 if (!out->downmix_buf)
1114 *stream_out = &out->stream;
1115 a2dp_dev->out = out;
1117 out->audio_state = AUDIO_A2DP_STATE_STANDBY;
1122 error("audio: cannot open output stream");
1128 static void audio_close_output_stream(struct audio_hw_device *dev,
1129 struct audio_stream_out *stream)
1131 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
1132 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
1136 close_endpoint(a2dp_dev->out->ep);
1138 free(out->downmix_buf);
1141 a2dp_dev->out = NULL;
1144 static int audio_set_parameters(struct audio_hw_device *dev,
1145 const char *kvpairs)
1147 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
1148 struct a2dp_stream_out *out = a2dp_dev->out;
1155 return out->stream.common.set_parameters((struct audio_stream *) out,
1159 static char *audio_get_parameters(const struct audio_hw_device *dev,
1166 static int audio_init_check(const struct audio_hw_device *dev)
1172 static int audio_set_voice_volume(struct audio_hw_device *dev, float volume)
1178 static int audio_set_master_volume(struct audio_hw_device *dev, float volume)
1184 static int audio_set_mode(struct audio_hw_device *dev, int mode)
1190 static int audio_set_mic_mute(struct audio_hw_device *dev, bool state)
1196 static int audio_get_mic_mute(const struct audio_hw_device *dev, bool *state)
1202 static size_t audio_get_input_buffer_size(const struct audio_hw_device *dev,
1203 const struct audio_config *config)
1209 static int audio_open_input_stream(struct audio_hw_device *dev,
1210 audio_io_handle_t handle,
1211 audio_devices_t devices,
1212 struct audio_config *config,
1213 struct audio_stream_in **stream_in)
1215 struct audio_stream_in *in;
1219 in = calloc(1, sizeof(struct audio_stream_in));
1223 in->common.get_sample_rate = in_get_sample_rate;
1224 in->common.set_sample_rate = in_set_sample_rate;
1225 in->common.get_buffer_size = in_get_buffer_size;
1226 in->common.get_channels = in_get_channels;
1227 in->common.get_format = in_get_format;
1228 in->common.set_format = in_set_format;
1229 in->common.standby = in_standby;
1230 in->common.dump = in_dump;
1231 in->common.set_parameters = in_set_parameters;
1232 in->common.get_parameters = in_get_parameters;
1233 in->common.add_audio_effect = in_add_audio_effect;
1234 in->common.remove_audio_effect = in_remove_audio_effect;
1235 in->set_gain = in_set_gain;
1237 in->get_input_frames_lost = in_get_input_frames_lost;
1244 static void audio_close_input_stream(struct audio_hw_device *dev,
1245 struct audio_stream_in *stream_in)
1251 static int audio_dump(const audio_hw_device_t *device, int fd)
1257 static int audio_close(hw_device_t *device)
1259 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *)device;
1263 unregister_endpoints();
1265 shutdown(listen_sk, SHUT_RDWR);
1266 shutdown(audio_sk, SHUT_RDWR);
1268 pthread_join(ipc_th, NULL);
1277 static void *ipc_handler(void *data)
1286 DBG("Waiting for connection ...");
1288 sk = accept(listen_sk, NULL, NULL);
1295 if (err != ECONNABORTED && err != EINVAL)
1296 error("audio: Failed to accept socket: %d (%s)",
1297 err, strerror(err));
1302 pthread_mutex_lock(&sk_mutex);
1304 pthread_mutex_unlock(&sk_mutex);
1306 DBG("Audio IPC: Connected");
1308 if (register_endpoints() != AUDIO_STATUS_SUCCESS) {
1309 error("audio: Failed to register endpoints");
1311 unregister_endpoints();
1313 pthread_mutex_lock(&sk_mutex);
1314 shutdown(audio_sk, SHUT_RDWR);
1317 pthread_mutex_unlock(&sk_mutex);
1322 memset(&pfd, 0, sizeof(pfd));
1324 pfd.events = POLLHUP | POLLERR | POLLNVAL;
1326 /* Check if socket is still alive. Empty while loop.*/
1327 while (poll(&pfd, 1, -1) < 0 && errno == EINTR);
1329 if (pfd.revents & (POLLHUP | POLLERR | POLLNVAL)) {
1330 info("Audio HAL: Socket closed");
1332 pthread_mutex_lock(&sk_mutex);
1335 pthread_mutex_unlock(&sk_mutex);
1339 /* audio_sk is closed at this point, just cleanup endpoints states */
1340 memset(audio_endpoints, 0, sizeof(audio_endpoints));
1342 info("Closing Audio IPC thread");
1346 static int audio_ipc_init(void)
1348 struct sockaddr_un addr;
1354 sk = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
1357 error("audio: Failed to create socket: %d (%s)", -err,
1362 memset(&addr, 0, sizeof(addr));
1363 addr.sun_family = AF_UNIX;
1365 memcpy(addr.sun_path, BLUEZ_AUDIO_SK_PATH,
1366 sizeof(BLUEZ_AUDIO_SK_PATH));
1368 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1370 error("audio: Failed to bind socket: %d (%s)", -err,
1375 if (listen(sk, 1) < 0) {
1377 error("audio: Failed to listen on the socket: %d (%s)", -err,
1384 err = pthread_create(&ipc_th, NULL, ipc_handler, NULL);
1388 error("audio: Failed to start Audio IPC thread: %d (%s)",
1389 -err, strerror(-err));
1400 static int audio_open(const hw_module_t *module, const char *name,
1401 hw_device_t **device)
1403 struct a2dp_audio_dev *a2dp_dev;
1408 if (strcmp(name, AUDIO_HARDWARE_INTERFACE)) {
1409 error("audio: interface %s not matching [%s]", name,
1410 AUDIO_HARDWARE_INTERFACE);
1414 err = audio_ipc_init();
1418 a2dp_dev = calloc(1, sizeof(struct a2dp_audio_dev));
1422 a2dp_dev->dev.common.tag = HARDWARE_DEVICE_TAG;
1423 a2dp_dev->dev.common.version = AUDIO_DEVICE_API_VERSION_CURRENT;
1424 a2dp_dev->dev.common.module = (struct hw_module_t *) module;
1425 a2dp_dev->dev.common.close = audio_close;
1427 a2dp_dev->dev.init_check = audio_init_check;
1428 a2dp_dev->dev.set_voice_volume = audio_set_voice_volume;
1429 a2dp_dev->dev.set_master_volume = audio_set_master_volume;
1430 a2dp_dev->dev.set_mode = audio_set_mode;
1431 a2dp_dev->dev.set_mic_mute = audio_set_mic_mute;
1432 a2dp_dev->dev.get_mic_mute = audio_get_mic_mute;
1433 a2dp_dev->dev.set_parameters = audio_set_parameters;
1434 a2dp_dev->dev.get_parameters = audio_get_parameters;
1435 a2dp_dev->dev.get_input_buffer_size = audio_get_input_buffer_size;
1436 a2dp_dev->dev.open_output_stream = audio_open_output_stream;
1437 a2dp_dev->dev.close_output_stream = audio_close_output_stream;
1438 a2dp_dev->dev.open_input_stream = audio_open_input_stream;
1439 a2dp_dev->dev.close_input_stream = audio_close_input_stream;
1440 a2dp_dev->dev.dump = audio_dump;
1443 * Note that &a2dp_dev->dev.common is the same pointer as a2dp_dev.
1444 * This results from the structure of following structs:a2dp_audio_dev,
1445 * audio_hw_device. We will rely on this later in the code.
1447 *device = &a2dp_dev->dev.common;
1452 static struct hw_module_methods_t hal_module_methods = {
1456 struct audio_module HAL_MODULE_INFO_SYM = {
1458 .tag = HARDWARE_MODULE_TAG,
1461 .id = AUDIO_HARDWARE_MODULE_ID,
1462 .name = "A2DP Bluez HW HAL",
1463 .author = "Intel Corporation",
1464 .methods = &hal_module_methods,