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 "hal-utils.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);
100 const audio_codec_get_t get_codec;
103 { .get_codec = codec_aptx, .loaded = false },
104 { .get_codec = codec_sbc, .loaded = false },
107 #define NUM_CODECS (sizeof(audio_codecs) / sizeof(audio_codecs[0]))
109 #define MAX_AUDIO_ENDPOINTS NUM_CODECS
111 struct audio_endpoint {
113 const struct audio_codec *codec;
117 struct media_packet *mp;
122 struct timespec start;
127 static struct audio_endpoint audio_endpoints[MAX_AUDIO_ENDPOINTS];
130 AUDIO_A2DP_STATE_NONE,
131 AUDIO_A2DP_STATE_STANDBY,
132 AUDIO_A2DP_STATE_SUSPENDED,
133 AUDIO_A2DP_STATE_STARTED
136 struct a2dp_stream_out {
137 struct audio_stream_out stream;
139 struct audio_endpoint *ep;
140 enum a2dp_state_t audio_state;
141 struct audio_input_config cfg;
143 uint8_t *downmix_buf;
146 struct a2dp_audio_dev {
147 struct audio_hw_device dev;
148 struct a2dp_stream_out *out;
151 static int audio_ipc_cmd(uint8_t service_id, uint8_t opcode, uint16_t len,
152 void *param, size_t *rsp_len, void *rsp, int *fd)
158 char cmsgbuf[CMSG_SPACE(sizeof(int))];
160 size_t s_len = sizeof(s);
162 pthread_mutex_lock(&sk_mutex);
165 error("audio: Invalid cmd socket passed to audio_ipc_cmd");
169 if (!rsp || !rsp_len) {
170 memset(&s, 0, s_len);
175 memset(&msg, 0, sizeof(msg));
176 memset(&cmd, 0, sizeof(cmd));
178 cmd.service_id = service_id;
182 iv[0].iov_base = &cmd;
183 iv[0].iov_len = sizeof(cmd);
185 iv[1].iov_base = param;
191 ret = sendmsg(audio_sk, &msg, 0);
193 error("audio: Sending command failed:%s", strerror(errno));
197 /* socket was shutdown */
199 error("audio: Command socket closed");
203 memset(&msg, 0, sizeof(msg));
204 memset(&cmd, 0, sizeof(cmd));
206 iv[0].iov_base = &cmd;
207 iv[0].iov_len = sizeof(cmd);
209 iv[1].iov_base = rsp;
210 iv[1].iov_len = *rsp_len;
216 memset(cmsgbuf, 0, sizeof(cmsgbuf));
217 msg.msg_control = cmsgbuf;
218 msg.msg_controllen = sizeof(cmsgbuf);
221 ret = recvmsg(audio_sk, &msg, 0);
223 error("audio: Receiving command response failed:%s",
228 if (ret < (ssize_t) sizeof(cmd)) {
229 error("audio: Too small response received(%zd bytes)", ret);
233 if (cmd.service_id != service_id) {
234 error("audio: Invalid service id (%u vs %u)", cmd.service_id,
239 if (ret != (ssize_t) (sizeof(cmd) + cmd.len)) {
240 error("audio: Malformed response received(%zd bytes)", ret);
244 if (cmd.opcode != opcode && cmd.opcode != AUDIO_OP_STATUS) {
245 error("audio: Invalid opcode received (%u vs %u)",
250 if (cmd.opcode == AUDIO_OP_STATUS) {
251 struct ipc_status *s = rsp;
253 if (sizeof(*s) != cmd.len) {
254 error("audio: Invalid status length");
258 if (s->code == AUDIO_STATUS_SUCCESS) {
259 error("audio: Invalid success status response");
263 pthread_mutex_unlock(&sk_mutex);
268 pthread_mutex_unlock(&sk_mutex);
270 /* Receive auxiliary data in msg */
272 struct cmsghdr *cmsg;
276 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
277 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
278 if (cmsg->cmsg_level == SOL_SOCKET
279 && cmsg->cmsg_type == SCM_RIGHTS) {
280 memcpy(fd, CMSG_DATA(cmsg), sizeof(int));
291 return AUDIO_STATUS_SUCCESS;
294 /* Some serious issue happen on IPC - recover */
295 shutdown(audio_sk, SHUT_RDWR);
296 pthread_mutex_unlock(&sk_mutex);
298 return AUDIO_STATUS_FAILED;
301 static int ipc_open_cmd(const struct audio_codec *codec)
303 uint8_t buf[BLUEZ_AUDIO_MTU];
304 struct audio_cmd_open *cmd = (struct audio_cmd_open *) buf;
305 struct audio_rsp_open rsp;
306 size_t cmd_len = sizeof(buf) - sizeof(*cmd);
307 size_t rsp_len = sizeof(rsp);
312 memcpy(cmd->uuid, a2dp_src_uuid, sizeof(a2dp_src_uuid));
314 cmd->codec = codec->type;
315 cmd->presets = codec->get_presets(cmd->preset, &cmd_len);
317 cmd_len += sizeof(*cmd);
319 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_OPEN, cmd_len, cmd,
320 &rsp_len, &rsp, NULL);
322 if (result != AUDIO_STATUS_SUCCESS)
328 static int ipc_close_cmd(uint8_t endpoint_id)
330 struct audio_cmd_close cmd;
335 cmd.id = endpoint_id;
337 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_CLOSE,
338 sizeof(cmd), &cmd, NULL, NULL, NULL);
343 static int ipc_open_stream_cmd(uint8_t *endpoint_id, uint16_t *mtu, int *fd,
344 struct audio_preset **caps)
346 char buf[BLUEZ_AUDIO_MTU];
347 struct audio_cmd_open_stream cmd;
348 struct audio_rsp_open_stream *rsp =
349 (struct audio_rsp_open_stream *) &buf;
350 size_t rsp_len = sizeof(buf);
356 return AUDIO_STATUS_FAILED;
358 cmd.id = *endpoint_id;
360 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_OPEN_STREAM,
361 sizeof(cmd), &cmd, &rsp_len, rsp, fd);
362 if (result == AUDIO_STATUS_SUCCESS) {
363 size_t buf_len = sizeof(struct audio_preset) +
365 *endpoint_id = rsp->id;
367 *caps = malloc(buf_len);
368 memcpy(*caps, &rsp->preset, buf_len);
376 static int ipc_close_stream_cmd(uint8_t endpoint_id)
378 struct audio_cmd_close_stream cmd;
383 cmd.id = endpoint_id;
385 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_CLOSE_STREAM,
386 sizeof(cmd), &cmd, NULL, NULL, NULL);
391 static int ipc_resume_stream_cmd(uint8_t endpoint_id)
393 struct audio_cmd_resume_stream cmd;
398 cmd.id = endpoint_id;
400 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_RESUME_STREAM,
401 sizeof(cmd), &cmd, NULL, NULL, NULL);
406 static int ipc_suspend_stream_cmd(uint8_t endpoint_id)
408 struct audio_cmd_suspend_stream cmd;
413 cmd.id = endpoint_id;
415 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_SUSPEND_STREAM,
416 sizeof(cmd), &cmd, NULL, NULL, NULL);
421 struct register_state {
422 struct audio_endpoint *ep;
426 static void register_endpoint(const struct audio_codec *codec,
427 struct register_state *state)
429 struct audio_endpoint *ep = state->ep;
431 /* don't even try to register more endpoints if one failed */
435 ep->id = ipc_open_cmd(codec);
439 error("Failed to register endpoint");
444 ep->codec_data = NULL;
450 static int register_endpoints(void)
452 struct register_state state;
455 state.ep = &audio_endpoints[0];
458 for (i = 0; i < NUM_CODECS; i++) {
459 const struct audio_codec *codec = audio_codecs[i].get_codec();
461 if (!audio_codecs[i].loaded)
464 register_endpoint(codec, &state);
467 return state.error ? AUDIO_STATUS_FAILED : AUDIO_STATUS_SUCCESS;
470 static void unregister_endpoints(void)
474 for (i = 0; i < MAX_AUDIO_ENDPOINTS; i++) {
475 struct audio_endpoint *ep = &audio_endpoints[i];
478 ipc_close_cmd(ep->id);
479 memset(ep, 0, sizeof(*ep));
484 static bool open_endpoint(struct audio_endpoint **epp,
485 struct audio_input_config *cfg)
487 struct audio_preset *preset;
488 struct audio_endpoint *ep = *epp;
489 const struct audio_codec *codec;
491 uint16_t payload_len;
499 if (ipc_open_stream_cmd(&ep_id, &mtu, &fd, &preset) !=
500 AUDIO_STATUS_SUCCESS)
503 DBG("ep_id=%d mtu=%u", ep_id, mtu);
505 for (i = 0; i < MAX_AUDIO_ENDPOINTS; i++)
506 if (audio_endpoints[i].id == ep_id) {
507 ep = &audio_endpoints[i];
512 error("Cound not find opened endpoint");
519 if (ep->codec->use_rtp)
520 payload_len -= sizeof(struct rtp_header);
525 codec->init(preset, payload_len, &ep->codec_data);
526 codec->get_config(ep->codec_data, cfg);
528 ep->mp = calloc(mtu, 1);
532 if (ep->codec->use_rtp) {
533 struct media_packet_rtp *mp_rtp =
534 (struct media_packet_rtp *) ep->mp;
536 mp_rtp->hdr.pt = 0x60;
537 mp_rtp->hdr.ssrc = htonl(1);
540 ep->mp_data_len = payload_len;
553 static void close_endpoint(struct audio_endpoint *ep)
555 ipc_close_stream_cmd(ep->id);
563 ep->codec->cleanup(ep->codec_data);
564 ep->codec_data = NULL;
567 static bool resume_endpoint(struct audio_endpoint *ep)
569 if (ipc_resume_stream_cmd(ep->id) != AUDIO_STATUS_SUCCESS)
575 ep->codec->update_qos(ep->codec_data, QOS_POLICY_DEFAULT);
580 static void downmix_to_mono(struct a2dp_stream_out *out, const uint8_t *buffer,
583 const int16_t *input = (const void *) buffer;
584 int16_t *output = (void *) out->downmix_buf;
587 /* PCM 16bit stereo */
588 frames = bytes / (2 * sizeof(int16_t));
590 for (i = 0; i < frames; i++) {
591 int16_t l = get_le16(&input[i * 2]);
592 int16_t r = get_le16(&input[i * 2 + 1]);
594 put_le16((l + r) / 2, &output[i]);
598 static bool wait_for_endpoint(struct audio_endpoint *ep, bool *writable)
603 struct pollfd pollfd;
606 pollfd.events = POLLOUT;
609 ret = poll(&pollfd, 1, 500);
612 *writable = !!(pollfd.revents & POLLOUT);
616 if (errno != EINTR) {
618 error("poll failed (%d)", ret);
626 static bool write_to_endpoint(struct audio_endpoint *ep, size_t bytes)
628 struct media_packet *mp = (struct media_packet *) ep->mp;
632 ret = write(ep->fd, mp, bytes);
638 * this should not happen so let's issue warning, but do not
639 * fail, we can try to write next packet
641 if (errno == EAGAIN) {
643 warn("write failed (%d)", ret);
647 if (errno != EINTR) {
649 error("write failed (%d)", ret);
657 static bool write_data(struct a2dp_stream_out *out, const void *buffer,
660 struct audio_endpoint *ep = out->ep;
661 struct media_packet *mp = (struct media_packet *) ep->mp;
662 struct media_packet_rtp *mp_rtp = (struct media_packet_rtp *) ep->mp;
663 size_t free_space = ep->mp_data_len;
666 while (consumed < bytes) {
671 struct timespec current;
672 uint64_t audio_sent, audio_passed;
673 bool do_write = false;
676 * prepare media packet in advance so we don't waste time after
679 if (ep->codec->use_rtp) {
680 mp_rtp->hdr.sequence_number = htons(ep->seq++);
681 mp_rtp->hdr.timestamp = htonl(ep->samples);
683 read = ep->codec->encode_mediapacket(ep->codec_data,
685 bytes - consumed, mp,
686 free_space, &written);
689 * not much we can do here, let's just ignore remaining
695 /* calculate where are we and where we should be */
696 clock_gettime(CLOCK_MONOTONIC, ¤t);
698 memcpy(&ep->start, ¤t, sizeof(ep->start));
699 audio_sent = ep->samples * 1000000ll / out->cfg.rate;
700 audio_passed = timespec_diff_us(¤t, &ep->start);
703 * if we're ahead of stream then wait for next write point,
704 * if we're lagging more than 100ms then stop writing and just
705 * skip data until we're back in sync
707 if (audio_sent > audio_passed) {
708 struct timespec anchor;
712 timespec_add(&ep->start, audio_sent, &anchor);
715 ret = clock_nanosleep(CLOCK_MONOTONIC,
716 TIMER_ABSTIME, &anchor,
723 error("clock_nanosleep failed (%d)",
728 } else if (!ep->resync) {
729 uint64_t diff = audio_passed - audio_sent;
731 if (diff > MAX_DELAY) {
732 warn("lag is %jums, resyncing", diff / 1000);
734 ep->codec->update_qos(ep->codec_data,
735 QOS_POLICY_DECREASE);
740 /* we send data only in case codec encoded some data, i.e. some
741 * codecs do internal buffering and output data only if full
742 * frame can be encoded
743 * in resync mode we'll just drop mediapackets
745 if (written > 0 && !ep->resync) {
746 /* wait some time for socket to be ready for write,
747 * but we'll just skip writing data if timeout occurs
749 if (!wait_for_endpoint(ep, &do_write))
753 if (ep->codec->use_rtp)
754 written += sizeof(struct rtp_header);
756 if (!write_to_endpoint(ep, written))
762 * AudioFlinger provides 16bit PCM, so sample size is 2 bytes
763 * multiplied by number of channels. Number of channels is
764 * simply number of bits set in channels mask.
766 samples = read / (2 * popcount(out->cfg.channels));
767 ep->samples += samples;
774 static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
777 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
778 const void *in_buf = buffer;
779 size_t in_len = bytes;
781 /* just return in case we're closing */
782 if (out->audio_state == AUDIO_A2DP_STATE_NONE)
785 /* We can auto-start only from standby */
786 if (out->audio_state == AUDIO_A2DP_STATE_STANDBY) {
787 DBG("stream in standby, auto-start");
789 if (!resume_endpoint(out->ep))
792 out->audio_state = AUDIO_A2DP_STATE_STARTED;
795 if (out->audio_state != AUDIO_A2DP_STATE_STARTED) {
796 error("audio: stream not started");
800 if (out->ep->fd < 0) {
801 error("audio: no transport socket");
806 * currently Android audioflinger is not able to provide mono stream on
807 * A2DP output so down mixing needs to be done in hal-audio plugin.
810 * AudioFlinger::PlaybackThread::readOutputParameters()
811 * frameworks/av/services/audioflinger/Threads.cpp:1631
813 if (out->cfg.channels == AUDIO_CHANNEL_OUT_MONO) {
814 if (!out->downmix_buf) {
815 error("audio: downmix buffer not initialized");
819 downmix_to_mono(out, buffer, bytes);
821 in_buf = out->downmix_buf;
825 if (!write_data(out, in_buf, in_len))
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)
859 * We should return proper buffer size calculated by codec (so each
860 * input buffer is encoded into single media packed) but this does not
861 * work well with AudioFlinger and causes problems. For this reason we
862 * use magic value here and out_write code takes care of splitting
863 * input buffer into multiple media packets.
865 return FIXED_BUFFER_SIZE;
868 static uint32_t out_get_channels(const struct audio_stream *stream)
873 * AudioFlinger can only provide stereo stream, so we return it here and
874 * later we'll downmix this to mono in case codec requires it
877 return AUDIO_CHANNEL_OUT_STEREO;
880 static audio_format_t out_get_format(const struct audio_stream *stream)
882 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
886 return out->cfg.format;
889 static int out_set_format(struct audio_stream *stream, audio_format_t format)
895 static int out_standby(struct audio_stream *stream)
897 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
901 if (out->audio_state == AUDIO_A2DP_STATE_STARTED) {
902 if (ipc_suspend_stream_cmd(out->ep->id) != AUDIO_STATUS_SUCCESS)
904 out->audio_state = AUDIO_A2DP_STATE_STANDBY;
910 static int out_dump(const struct audio_stream *stream, int fd)
916 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
918 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
922 bool enter_suspend = false;
923 bool exit_suspend = false;
927 str = strdup(kvpairs);
931 kvpair = strtok_r(str, ";", &saveptr);
933 for (; kvpair && *kvpair; kvpair = strtok_r(NULL, ";", &saveptr)) {
936 keyval = strchr(kvpair, '=');
943 if (!strcmp(kvpair, "closing")) {
944 if (!strcmp(keyval, "true"))
945 out->audio_state = AUDIO_A2DP_STATE_NONE;
946 } else if (!strcmp(kvpair, "A2dpSuspended")) {
947 if (!strcmp(keyval, "true"))
948 enter_suspend = true;
956 if (enter_suspend && out->audio_state == AUDIO_A2DP_STATE_STARTED) {
957 if (ipc_suspend_stream_cmd(out->ep->id) != AUDIO_STATUS_SUCCESS)
959 out->audio_state = AUDIO_A2DP_STATE_SUSPENDED;
962 if (exit_suspend && out->audio_state == AUDIO_A2DP_STATE_SUSPENDED)
963 out->audio_state = AUDIO_A2DP_STATE_STANDBY;
968 static char *out_get_parameters(const struct audio_stream *stream,
975 static uint32_t out_get_latency(const struct audio_stream_out *stream)
977 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
978 struct audio_endpoint *ep = out->ep;
983 pkt_duration = ep->codec->get_mediapacket_duration(ep->codec_data);
985 return FIXED_A2DP_PLAYBACK_LATENCY_MS + pkt_duration / 1000;
988 static int out_set_volume(struct audio_stream_out *stream, float left,
992 /* volume controlled in audioflinger mixer (digital) */
996 static int out_get_render_position(const struct audio_stream_out *stream,
997 uint32_t *dsp_frames)
1003 static int out_add_audio_effect(const struct audio_stream *stream,
1004 effect_handle_t effect)
1010 static int out_remove_audio_effect(const struct audio_stream *stream,
1011 effect_handle_t effect)
1017 static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1023 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1029 static size_t in_get_buffer_size(const struct audio_stream *stream)
1035 static uint32_t in_get_channels(const struct audio_stream *stream)
1041 static audio_format_t in_get_format(const struct audio_stream *stream)
1047 static int in_set_format(struct audio_stream *stream, audio_format_t format)
1053 static int in_standby(struct audio_stream *stream)
1059 static int in_dump(const struct audio_stream *stream, int fd)
1065 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1071 static char *in_get_parameters(const struct audio_stream *stream,
1078 static int in_set_gain(struct audio_stream_in *stream, float gain)
1084 static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1091 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1097 static int in_add_audio_effect(const struct audio_stream *stream,
1098 effect_handle_t effect)
1104 static int in_remove_audio_effect(const struct audio_stream *stream,
1105 effect_handle_t effect)
1111 static int audio_open_output_stream_real(struct audio_hw_device *dev,
1112 audio_io_handle_t handle,
1113 audio_devices_t devices,
1114 audio_output_flags_t flags,
1115 struct audio_config *config,
1116 struct audio_stream_out **stream_out,
1117 const char *address)
1119 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
1120 struct a2dp_stream_out *out;
1122 out = calloc(1, sizeof(struct a2dp_stream_out));
1128 out->stream.common.get_sample_rate = out_get_sample_rate;
1129 out->stream.common.set_sample_rate = out_set_sample_rate;
1130 out->stream.common.get_buffer_size = out_get_buffer_size;
1131 out->stream.common.get_channels = out_get_channels;
1132 out->stream.common.get_format = out_get_format;
1133 out->stream.common.set_format = out_set_format;
1134 out->stream.common.standby = out_standby;
1135 out->stream.common.dump = out_dump;
1136 out->stream.common.set_parameters = out_set_parameters;
1137 out->stream.common.get_parameters = out_get_parameters;
1138 out->stream.common.add_audio_effect = out_add_audio_effect;
1139 out->stream.common.remove_audio_effect = out_remove_audio_effect;
1140 out->stream.get_latency = out_get_latency;
1141 out->stream.set_volume = out_set_volume;
1142 out->stream.write = out_write;
1143 out->stream.get_render_position = out_get_render_position;
1145 /* We want to autoselect opened endpoint */
1148 if (!open_endpoint(&out->ep, &out->cfg))
1151 DBG("rate=%d channels=%d format=%d", out->cfg.rate,
1152 out->cfg.channels, out->cfg.format);
1154 if (out->cfg.channels == AUDIO_CHANNEL_OUT_MONO) {
1155 out->downmix_buf = malloc(FIXED_BUFFER_SIZE / 2);
1156 if (!out->downmix_buf)
1160 *stream_out = &out->stream;
1161 a2dp_dev->out = out;
1163 out->audio_state = AUDIO_A2DP_STATE_STANDBY;
1168 error("audio: cannot open output stream");
1174 #if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
1175 static int audio_open_output_stream(struct audio_hw_device *dev,
1176 audio_io_handle_t handle,
1177 audio_devices_t devices,
1178 audio_output_flags_t flags,
1179 struct audio_config *config,
1180 struct audio_stream_out **stream_out,
1181 const char *address)
1183 return audio_open_output_stream_real(dev, handle, devices, flags,
1184 config, stream_out, address);
1187 static int audio_open_output_stream(struct audio_hw_device *dev,
1188 audio_io_handle_t handle,
1189 audio_devices_t devices,
1190 audio_output_flags_t flags,
1191 struct audio_config *config,
1192 struct audio_stream_out **stream_out)
1194 return audio_open_output_stream_real(dev, handle, devices, flags,
1195 config, stream_out, NULL);
1199 static void audio_close_output_stream(struct audio_hw_device *dev,
1200 struct audio_stream_out *stream)
1202 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
1203 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
1207 close_endpoint(a2dp_dev->out->ep);
1209 free(out->downmix_buf);
1212 a2dp_dev->out = NULL;
1215 static int audio_set_parameters(struct audio_hw_device *dev,
1216 const char *kvpairs)
1218 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
1219 struct a2dp_stream_out *out = a2dp_dev->out;
1226 return out->stream.common.set_parameters((struct audio_stream *) out,
1230 static char *audio_get_parameters(const struct audio_hw_device *dev,
1237 static int audio_init_check(const struct audio_hw_device *dev)
1243 static int audio_set_voice_volume(struct audio_hw_device *dev, float volume)
1249 static int audio_set_master_volume(struct audio_hw_device *dev, float volume)
1255 static int audio_set_mode(struct audio_hw_device *dev, int mode)
1261 static int audio_set_mic_mute(struct audio_hw_device *dev, bool state)
1267 static int audio_get_mic_mute(const struct audio_hw_device *dev, bool *state)
1273 static size_t audio_get_input_buffer_size(const struct audio_hw_device *dev,
1274 const struct audio_config *config)
1280 static int audio_open_input_stream_real(struct audio_hw_device *dev,
1281 audio_io_handle_t handle,
1282 audio_devices_t devices,
1283 struct audio_config *config,
1284 struct audio_stream_in **stream_in,
1285 audio_input_flags_t flags,
1286 const char *address,
1287 audio_source_t source)
1289 struct audio_stream_in *in;
1293 in = calloc(1, sizeof(struct audio_stream_in));
1297 in->common.get_sample_rate = in_get_sample_rate;
1298 in->common.set_sample_rate = in_set_sample_rate;
1299 in->common.get_buffer_size = in_get_buffer_size;
1300 in->common.get_channels = in_get_channels;
1301 in->common.get_format = in_get_format;
1302 in->common.set_format = in_set_format;
1303 in->common.standby = in_standby;
1304 in->common.dump = in_dump;
1305 in->common.set_parameters = in_set_parameters;
1306 in->common.get_parameters = in_get_parameters;
1307 in->common.add_audio_effect = in_add_audio_effect;
1308 in->common.remove_audio_effect = in_remove_audio_effect;
1309 in->set_gain = in_set_gain;
1311 in->get_input_frames_lost = in_get_input_frames_lost;
1318 #if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
1319 static int audio_open_input_stream(struct audio_hw_device *dev,
1320 audio_io_handle_t handle,
1321 audio_devices_t devices,
1322 struct audio_config *config,
1323 struct audio_stream_in **stream_in,
1324 audio_input_flags_t flags,
1325 const char *address,
1326 audio_source_t source)
1328 return audio_open_input_stream_real(dev, handle, devices, config,
1329 stream_in, flags, address,
1333 static int audio_open_input_stream(struct audio_hw_device *dev,
1334 audio_io_handle_t handle,
1335 audio_devices_t devices,
1336 struct audio_config *config,
1337 struct audio_stream_in **stream_in)
1339 return audio_open_input_stream_real(dev, handle, devices, config,
1340 stream_in, 0, NULL, 0);
1344 static void audio_close_input_stream(struct audio_hw_device *dev,
1345 struct audio_stream_in *stream_in)
1351 static int audio_dump(const audio_hw_device_t *device, int fd)
1357 static int audio_close(hw_device_t *device)
1359 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *)device;
1364 unregister_endpoints();
1366 for (i = 0; i < NUM_CODECS; i++) {
1367 const struct audio_codec *codec = audio_codecs[i].get_codec();
1369 if (!audio_codecs[i].loaded)
1375 audio_codecs[i].loaded = false;
1378 shutdown(listen_sk, SHUT_RDWR);
1379 shutdown(audio_sk, SHUT_RDWR);
1381 pthread_join(ipc_th, NULL);
1390 static void *ipc_handler(void *data)
1399 DBG("Waiting for connection ...");
1401 sk = accept(listen_sk, NULL, NULL);
1408 if (err != ECONNABORTED && err != EINVAL)
1409 error("audio: Failed to accept socket: %d (%s)",
1410 err, strerror(err));
1415 pthread_mutex_lock(&sk_mutex);
1417 pthread_mutex_unlock(&sk_mutex);
1419 DBG("Audio IPC: Connected");
1421 if (register_endpoints() != AUDIO_STATUS_SUCCESS) {
1422 error("audio: Failed to register endpoints");
1424 unregister_endpoints();
1426 pthread_mutex_lock(&sk_mutex);
1427 shutdown(audio_sk, SHUT_RDWR);
1430 pthread_mutex_unlock(&sk_mutex);
1435 memset(&pfd, 0, sizeof(pfd));
1437 pfd.events = POLLHUP | POLLERR | POLLNVAL;
1439 /* Check if socket is still alive. Empty while loop.*/
1440 while (poll(&pfd, 1, -1) < 0 && errno == EINTR);
1442 info("Audio HAL: Socket closed");
1444 pthread_mutex_lock(&sk_mutex);
1447 pthread_mutex_unlock(&sk_mutex);
1450 /* audio_sk is closed at this point, just cleanup endpoints states */
1451 memset(audio_endpoints, 0, sizeof(audio_endpoints));
1453 info("Closing Audio IPC thread");
1457 static int audio_ipc_init(void)
1459 struct sockaddr_un addr;
1465 sk = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
1468 error("audio: Failed to create socket: %d (%s)", -err,
1473 memset(&addr, 0, sizeof(addr));
1474 addr.sun_family = AF_UNIX;
1476 memcpy(addr.sun_path, BLUEZ_AUDIO_SK_PATH,
1477 sizeof(BLUEZ_AUDIO_SK_PATH));
1479 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1481 error("audio: Failed to bind socket: %d (%s)", -err,
1486 if (listen(sk, 1) < 0) {
1488 error("audio: Failed to listen on the socket: %d (%s)", -err,
1495 err = pthread_create(&ipc_th, NULL, ipc_handler, NULL);
1499 error("audio: Failed to start Audio IPC thread: %d (%s)",
1500 -err, strerror(-err));
1511 static int audio_open(const hw_module_t *module, const char *name,
1512 hw_device_t **device)
1514 struct a2dp_audio_dev *a2dp_dev;
1520 if (strcmp(name, AUDIO_HARDWARE_INTERFACE)) {
1521 error("audio: interface %s not matching [%s]", name,
1522 AUDIO_HARDWARE_INTERFACE);
1526 err = audio_ipc_init();
1530 a2dp_dev = calloc(1, sizeof(struct a2dp_audio_dev));
1534 a2dp_dev->dev.common.tag = HARDWARE_DEVICE_TAG;
1535 a2dp_dev->dev.common.version = AUDIO_DEVICE_API_VERSION_CURRENT;
1536 a2dp_dev->dev.common.module = (struct hw_module_t *) module;
1537 a2dp_dev->dev.common.close = audio_close;
1539 a2dp_dev->dev.init_check = audio_init_check;
1540 a2dp_dev->dev.set_voice_volume = audio_set_voice_volume;
1541 a2dp_dev->dev.set_master_volume = audio_set_master_volume;
1542 a2dp_dev->dev.set_mode = audio_set_mode;
1543 a2dp_dev->dev.set_mic_mute = audio_set_mic_mute;
1544 a2dp_dev->dev.get_mic_mute = audio_get_mic_mute;
1545 a2dp_dev->dev.set_parameters = audio_set_parameters;
1546 a2dp_dev->dev.get_parameters = audio_get_parameters;
1547 a2dp_dev->dev.get_input_buffer_size = audio_get_input_buffer_size;
1548 a2dp_dev->dev.open_output_stream = audio_open_output_stream;
1549 a2dp_dev->dev.close_output_stream = audio_close_output_stream;
1550 a2dp_dev->dev.open_input_stream = audio_open_input_stream;
1551 a2dp_dev->dev.close_input_stream = audio_close_input_stream;
1552 a2dp_dev->dev.dump = audio_dump;
1554 for (i = 0; i < NUM_CODECS; i++) {
1555 const struct audio_codec *codec = audio_codecs[i].get_codec();
1557 if (codec->load && !codec->load())
1560 audio_codecs[i].loaded = true;
1564 * Note that &a2dp_dev->dev.common is the same pointer as a2dp_dev.
1565 * This results from the structure of following structs:a2dp_audio_dev,
1566 * audio_hw_device. We will rely on this later in the code.
1568 *device = &a2dp_dev->dev.common;
1573 static struct hw_module_methods_t hal_module_methods = {
1577 struct audio_module HAL_MODULE_INFO_SYM = {
1579 .tag = HARDWARE_MODULE_TAG,
1582 .id = AUDIO_HARDWARE_MODULE_ID,
1583 .name = "A2DP Bluez HW HAL",
1584 .author = "Intel Corporation",
1585 .methods = &hal_module_methods,