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>
28 #include <hardware/audio.h>
29 #include <hardware/hardware.h>
33 #include "audio-msg.h"
36 #include "../profiles/audio/a2dp-codecs.h"
38 static const uint8_t a2dp_src_uuid[] = {
39 0x00, 0x00, 0x11, 0x0a, 0x00, 0x00, 0x10, 0x00,
40 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb };
42 static int listen_sk = -1;
43 static int audio_sk = -1;
44 static bool close_thread = false;
46 static pthread_t ipc_th = 0;
47 static pthread_mutex_t close_mutex = PTHREAD_MUTEX_INITIALIZER;
48 static pthread_mutex_t sk_mutex = PTHREAD_MUTEX_INITIALIZER;
50 #if __BYTE_ORDER == __LITTLE_ENDIAN
61 uint16_t sequence_number;
65 } __attribute__ ((packed));
68 unsigned frame_count:4;
70 unsigned is_last_fragment:1;
71 unsigned is_first_fragment:1;
72 unsigned is_fragmented:1;
73 } __attribute__ ((packed));
75 #elif __BYTE_ORDER == __BIG_ENDIAN
86 uint16_t sequence_number;
90 } __attribute__ ((packed));
93 unsigned is_fragmented:1;
94 unsigned is_first_fragment:1;
95 unsigned is_last_fragment:1;
97 unsigned frame_count:4;
98 } __attribute__ ((packed));
101 #error "Unknown byte order"
104 struct media_packet {
105 struct rtp_header hdr;
106 struct rtp_payload payload;
110 struct audio_input_config {
113 audio_format_t format;
127 unsigned frame_duration;
129 struct timespec start;
130 unsigned frames_sent;
133 static int sbc_get_presets(struct audio_preset *preset, size_t *len);
134 static int sbc_codec_init(struct audio_preset *preset, uint16_t mtu,
136 static int sbc_cleanup(void *codec_data);
137 static int sbc_get_config(void *codec_data,
138 struct audio_input_config *config);
139 static void sbc_resume(void *codec_data);
144 int (*get_presets) (struct audio_preset *preset, size_t *len);
146 int (*init) (struct audio_preset *preset, uint16_t mtu,
148 int (*cleanup) (void *codec_data);
149 int (*get_config) (void *codec_data,
150 struct audio_input_config *config);
151 void (*resume) (void *codec_data);
152 ssize_t (*write_data) (void *codec_data, const void *buffer,
156 static const struct audio_codec audio_codecs[] = {
158 .type = A2DP_CODEC_SBC,
160 .get_presets = sbc_get_presets,
162 .init = sbc_codec_init,
163 .cleanup = sbc_cleanup,
164 .get_config = sbc_get_config,
165 .resume = sbc_resume,
169 #define NUM_CODECS (sizeof(audio_codecs) / sizeof(audio_codecs[0]))
171 #define MAX_AUDIO_ENDPOINTS NUM_CODECS
173 struct audio_endpoint {
175 const struct audio_codec *codec;
180 static struct audio_endpoint audio_endpoints[MAX_AUDIO_ENDPOINTS];
183 AUDIO_A2DP_STATE_NONE,
184 AUDIO_A2DP_STATE_STANDBY,
185 AUDIO_A2DP_STATE_SUSPENDED,
186 AUDIO_A2DP_STATE_STARTED
189 struct a2dp_stream_out {
190 struct audio_stream_out stream;
192 struct audio_endpoint *ep;
193 enum a2dp_state_t audio_state;
194 struct audio_input_config cfg;
197 struct a2dp_audio_dev {
198 struct audio_hw_device dev;
199 struct a2dp_stream_out *out;
202 static const a2dp_sbc_t sbc_presets[] = {
204 .frequency = SBC_SAMPLING_FREQ_44100 | SBC_SAMPLING_FREQ_48000,
205 .channel_mode = SBC_CHANNEL_MODE_MONO |
206 SBC_CHANNEL_MODE_DUAL_CHANNEL |
207 SBC_CHANNEL_MODE_STEREO |
208 SBC_CHANNEL_MODE_JOINT_STEREO,
209 .subbands = SBC_SUBBANDS_4 | SBC_SUBBANDS_8,
210 .allocation_method = SBC_ALLOCATION_SNR |
211 SBC_ALLOCATION_LOUDNESS,
212 .block_length = SBC_BLOCK_LENGTH_4 | SBC_BLOCK_LENGTH_8 |
213 SBC_BLOCK_LENGTH_12 | SBC_BLOCK_LENGTH_16,
214 .min_bitpool = MIN_BITPOOL,
215 .max_bitpool = MAX_BITPOOL
218 .frequency = SBC_SAMPLING_FREQ_44100,
219 .channel_mode = SBC_CHANNEL_MODE_JOINT_STEREO,
220 .subbands = SBC_SUBBANDS_8,
221 .allocation_method = SBC_ALLOCATION_LOUDNESS,
222 .block_length = SBC_BLOCK_LENGTH_16,
223 .min_bitpool = MIN_BITPOOL,
224 .max_bitpool = MAX_BITPOOL
227 .frequency = SBC_SAMPLING_FREQ_48000,
228 .channel_mode = SBC_CHANNEL_MODE_JOINT_STEREO,
229 .subbands = SBC_SUBBANDS_8,
230 .allocation_method = SBC_ALLOCATION_LOUDNESS,
231 .block_length = SBC_BLOCK_LENGTH_16,
232 .min_bitpool = MIN_BITPOOL,
233 .max_bitpool = MAX_BITPOOL
237 static int sbc_get_presets(struct audio_preset *preset, size_t *len)
242 uint8_t *ptr = (uint8_t *) preset;
243 size_t preset_size = sizeof(*preset) + sizeof(a2dp_sbc_t);
247 count = sizeof(sbc_presets) / sizeof(sbc_presets[0]);
249 for (i = 0; i < count; i++) {
250 preset = (struct audio_preset *) ptr;
252 if (new_len + preset_size > *len)
255 preset->len = sizeof(a2dp_sbc_t);
256 memcpy(preset->data, &sbc_presets[i], preset->len);
258 new_len += preset_size;
267 static void sbc_init_encoder(struct sbc_data *sbc_data)
269 a2dp_sbc_t *in = &sbc_data->sbc;
270 sbc_t *out = &sbc_data->enc;
274 sbc_init_a2dp(out, 0L, in, sizeof(*in));
276 out->endian = SBC_LE;
277 out->bitpool = in->max_bitpool;
280 static int sbc_codec_init(struct audio_preset *preset, uint16_t mtu,
283 struct sbc_data *sbc_data;
284 size_t hdr_len = sizeof(struct media_packet);
286 size_t out_frame_len;
291 if (preset->len != sizeof(a2dp_sbc_t)) {
292 DBG("preset size mismatch");
293 return AUDIO_STATUS_FAILED;
296 sbc_data = calloc(sizeof(struct sbc_data), 1);
298 memcpy(&sbc_data->sbc, preset->data, preset->len);
300 sbc_init_encoder(sbc_data);
302 in_frame_len = sbc_get_codesize(&sbc_data->enc);
303 out_frame_len = sbc_get_frame_length(&sbc_data->enc);
304 num_frames = (mtu - hdr_len) / out_frame_len;
306 sbc_data->in_frame_len = in_frame_len;
307 sbc_data->in_buf_size = num_frames * in_frame_len;
309 sbc_data->out_buf_size = hdr_len + num_frames * out_frame_len;
310 sbc_data->out_buf = calloc(1, sbc_data->out_buf_size);
312 sbc_data->frame_duration = sbc_get_frame_duration(&sbc_data->enc);
314 *codec_data = sbc_data;
316 return AUDIO_STATUS_SUCCESS;
319 static int sbc_cleanup(void *codec_data)
321 struct sbc_data *sbc_data = (struct sbc_data *) codec_data;
325 sbc_finish(&sbc_data->enc);
326 free(sbc_data->out_buf);
329 return AUDIO_STATUS_SUCCESS;
332 static int sbc_get_config(void *codec_data,
333 struct audio_input_config *config)
335 struct sbc_data *sbc_data = (struct sbc_data *) codec_data;
337 switch (sbc_data->sbc.frequency) {
338 case SBC_SAMPLING_FREQ_16000:
339 config->rate = 16000;
341 case SBC_SAMPLING_FREQ_32000:
342 config->rate = 32000;
344 case SBC_SAMPLING_FREQ_44100:
345 config->rate = 44100;
347 case SBC_SAMPLING_FREQ_48000:
348 config->rate = 48000;
351 return AUDIO_STATUS_FAILED;
353 config->channels = sbc_data->sbc.channel_mode == SBC_CHANNEL_MODE_MONO ?
354 AUDIO_CHANNEL_OUT_MONO :
355 AUDIO_CHANNEL_OUT_STEREO;
356 config->format = AUDIO_FORMAT_PCM_16_BIT;
358 return AUDIO_STATUS_SUCCESS;
361 static void sbc_resume(void *codec_data)
363 struct sbc_data *sbc_data = (struct sbc_data *) codec_data;
367 clock_gettime(CLOCK_MONOTONIC, &sbc_data->start);
369 sbc_data->frames_sent = 0;
372 static void audio_ipc_cleanup(void)
375 shutdown(audio_sk, SHUT_RDWR);
380 static int audio_ipc_cmd(uint8_t service_id, uint8_t opcode, uint16_t len,
381 void *param, size_t *rsp_len, void *rsp, int *fd)
387 char cmsgbuf[CMSG_SPACE(sizeof(int))];
389 size_t s_len = sizeof(s);
392 error("audio: Invalid cmd socket passed to audio_ipc_cmd");
396 if (!rsp || !rsp_len) {
397 memset(&s, 0, s_len);
402 memset(&msg, 0, sizeof(msg));
403 memset(&cmd, 0, sizeof(cmd));
405 cmd.service_id = service_id;
409 iv[0].iov_base = &cmd;
410 iv[0].iov_len = sizeof(cmd);
412 iv[1].iov_base = param;
418 pthread_mutex_lock(&sk_mutex);
420 ret = sendmsg(audio_sk, &msg, 0);
422 error("audio: Sending command failed:%s", strerror(errno));
423 pthread_mutex_unlock(&sk_mutex);
427 /* socket was shutdown */
429 error("audio: Command socket closed");
433 memset(&msg, 0, sizeof(msg));
434 memset(&cmd, 0, sizeof(cmd));
436 iv[0].iov_base = &cmd;
437 iv[0].iov_len = sizeof(cmd);
439 iv[1].iov_base = rsp;
440 iv[1].iov_len = *rsp_len;
446 memset(cmsgbuf, 0, sizeof(cmsgbuf));
447 msg.msg_control = cmsgbuf;
448 msg.msg_controllen = sizeof(cmsgbuf);
451 ret = recvmsg(audio_sk, &msg, 0);
453 error("audio: Receiving command response failed:%s",
455 pthread_mutex_unlock(&sk_mutex);
459 pthread_mutex_unlock(&sk_mutex);
461 if (ret < (ssize_t) sizeof(cmd)) {
462 error("audio: Too small response received(%zd bytes)", ret);
466 if (cmd.service_id != service_id) {
467 error("audio: Invalid service id (%u vs %u)", cmd.service_id,
472 if (ret != (ssize_t) (sizeof(cmd) + cmd.len)) {
473 error("audio: Malformed response received(%zd bytes)", ret);
477 if (cmd.opcode != opcode && cmd.opcode != AUDIO_OP_STATUS) {
478 error("audio: Invalid opcode received (%u vs %u)",
483 if (cmd.opcode == AUDIO_OP_STATUS) {
484 struct hal_status *s = rsp;
486 if (sizeof(*s) != cmd.len) {
487 error("audio: Invalid status length");
491 if (s->code == AUDIO_STATUS_SUCCESS) {
492 error("audio: Invalid success status response");
499 /* Receive auxiliary data in msg */
501 struct cmsghdr *cmsg;
505 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
506 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
507 if (cmsg->cmsg_level == SOL_SOCKET
508 && cmsg->cmsg_type == SCM_RIGHTS) {
509 memcpy(fd, CMSG_DATA(cmsg), sizeof(int));
518 return AUDIO_STATUS_SUCCESS;
521 /* Some serious issue happen on IPC - recover */
522 shutdown(audio_sk, SHUT_RDWR);
524 return AUDIO_STATUS_FAILED;
527 static int ipc_open_cmd(const struct audio_codec *codec)
529 uint8_t buf[BLUEZ_AUDIO_MTU];
530 struct audio_cmd_open *cmd = (struct audio_cmd_open *) buf;
531 struct audio_rsp_open rsp;
532 size_t cmd_len = sizeof(buf) - sizeof(*cmd);
533 size_t rsp_len = sizeof(rsp);
538 memcpy(cmd->uuid, a2dp_src_uuid, sizeof(a2dp_src_uuid));
540 cmd->codec = codec->type;
541 cmd->presets = codec->get_presets(cmd->preset, &cmd_len);
543 cmd_len += sizeof(*cmd);
545 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_OPEN, cmd_len, cmd,
546 &rsp_len, &rsp, NULL);
548 if (result != AUDIO_STATUS_SUCCESS)
554 static int ipc_close_cmd(uint8_t endpoint_id)
556 struct audio_cmd_close cmd;
561 cmd.id = endpoint_id;
563 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_CLOSE,
564 sizeof(cmd), &cmd, NULL, NULL, NULL);
569 static int ipc_open_stream_cmd(uint8_t endpoint_id, uint16_t *mtu,
570 struct audio_preset **caps)
572 char buf[BLUEZ_AUDIO_MTU];
573 struct audio_cmd_open_stream cmd;
574 struct audio_rsp_open_stream *rsp =
575 (struct audio_rsp_open_stream *) &buf;
576 size_t rsp_len = sizeof(buf);
582 return AUDIO_STATUS_FAILED;
584 cmd.id = endpoint_id;
586 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_OPEN_STREAM,
587 sizeof(cmd), &cmd, &rsp_len, rsp, NULL);
589 if (result == AUDIO_STATUS_SUCCESS) {
590 size_t buf_len = sizeof(struct audio_preset) +
593 *caps = malloc(buf_len);
594 memcpy(*caps, &rsp->preset, buf_len);
602 static int ipc_close_stream_cmd(uint8_t endpoint_id)
604 struct audio_cmd_close_stream cmd;
609 cmd.id = endpoint_id;
611 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_CLOSE_STREAM,
612 sizeof(cmd), &cmd, NULL, NULL, NULL);
617 static int ipc_resume_stream_cmd(uint8_t endpoint_id)
619 struct audio_cmd_resume_stream cmd;
624 cmd.id = endpoint_id;
626 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_RESUME_STREAM,
627 sizeof(cmd), &cmd, NULL, NULL, NULL);
632 static int ipc_suspend_stream_cmd(uint8_t endpoint_id)
634 struct audio_cmd_suspend_stream cmd;
639 cmd.id = endpoint_id;
641 result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_SUSPEND_STREAM,
642 sizeof(cmd), &cmd, NULL, NULL, NULL);
647 static int register_endpoints(void)
649 struct audio_endpoint *ep = &audio_endpoints[0];
652 for (i = 0; i < NUM_CODECS; i++, ep++) {
653 const struct audio_codec *codec = &audio_codecs[i];
655 ep->id = ipc_open_cmd(codec);
658 return AUDIO_STATUS_FAILED;
661 ep->codec_data = NULL;
665 return AUDIO_STATUS_SUCCESS;
668 static void unregister_endpoints(void)
672 for (i = 0; i < MAX_AUDIO_ENDPOINTS; i++) {
673 struct audio_endpoint *ep = &audio_endpoints[i];
676 ipc_close_cmd(ep->id);
677 memset(ep, 0, sizeof(*ep));
682 static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
685 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
687 /* We can auto-start only from standby */
688 if (out->audio_state == AUDIO_A2DP_STATE_STANDBY) {
689 DBG("stream in standby, auto-start");
691 if (ipc_resume_stream_cmd(out->ep->id) != AUDIO_STATUS_SUCCESS)
694 out->ep->codec->resume(out->ep->codec_data);
696 out->audio_state = AUDIO_A2DP_STATE_STARTED;
699 if (out->audio_state != AUDIO_A2DP_STATE_STARTED) {
700 DBG("stream not started");
704 /* TODO: encode data using codec */
709 static uint32_t out_get_sample_rate(const struct audio_stream *stream)
711 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
715 return out->cfg.rate;
718 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
720 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
724 if (rate != out->cfg.rate) {
725 DBG("cannot set sample rate to %d", rate);
732 static size_t out_get_buffer_size(const struct audio_stream *stream)
738 static uint32_t out_get_channels(const struct audio_stream *stream)
740 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
744 return out->cfg.channels;
747 static audio_format_t out_get_format(const struct audio_stream *stream)
749 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
753 return out->cfg.format;
756 static int out_set_format(struct audio_stream *stream, audio_format_t format)
762 static int out_standby(struct audio_stream *stream)
764 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
768 if (out->audio_state == AUDIO_A2DP_STATE_STARTED) {
769 if (ipc_suspend_stream_cmd(out->ep->id) != AUDIO_STATUS_SUCCESS)
771 out->audio_state = AUDIO_A2DP_STATE_STANDBY;
777 static int out_dump(const struct audio_stream *stream, int fd)
783 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
785 struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
789 bool enter_suspend = false;
790 bool exit_suspend = false;
794 str = strdup(kvpairs);
795 kvpair = strtok_r(str, ";", &saveptr);
797 for (; kvpair && *kvpair; kvpair = strtok_r(NULL, ";", &saveptr)) {
800 keyval = strchr(kvpair, '=');
807 if (!strcmp(kvpair, "closing")) {
808 if (!strcmp(keyval, "true"))
809 out->audio_state = AUDIO_A2DP_STATE_NONE;
810 } else if (!strcmp(kvpair, "A2dpSuspended")) {
811 if (!strcmp(keyval, "true"))
812 enter_suspend = true;
820 if (enter_suspend && out->audio_state == AUDIO_A2DP_STATE_STARTED) {
821 if (ipc_suspend_stream_cmd(out->ep->id) != AUDIO_STATUS_SUCCESS)
823 out->audio_state = AUDIO_A2DP_STATE_SUSPENDED;
826 if (exit_suspend && out->audio_state == AUDIO_A2DP_STATE_SUSPENDED)
827 out->audio_state = AUDIO_A2DP_STATE_STANDBY;
832 static char *out_get_parameters(const struct audio_stream *stream,
839 static uint32_t out_get_latency(const struct audio_stream_out *stream)
845 static int out_set_volume(struct audio_stream_out *stream, float left,
849 /* volume controlled in audioflinger mixer (digital) */
853 static int out_get_render_position(const struct audio_stream_out *stream,
854 uint32_t *dsp_frames)
860 static int out_add_audio_effect(const struct audio_stream *stream,
861 effect_handle_t effect)
867 static int out_remove_audio_effect(const struct audio_stream *stream,
868 effect_handle_t effect)
874 static uint32_t in_get_sample_rate(const struct audio_stream *stream)
880 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
886 static size_t in_get_buffer_size(const struct audio_stream *stream)
892 static uint32_t in_get_channels(const struct audio_stream *stream)
898 static audio_format_t in_get_format(const struct audio_stream *stream)
904 static int in_set_format(struct audio_stream *stream, audio_format_t format)
910 static int in_standby(struct audio_stream *stream)
916 static int in_dump(const struct audio_stream *stream, int fd)
922 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
928 static char *in_get_parameters(const struct audio_stream *stream,
935 static int in_set_gain(struct audio_stream_in *stream, float gain)
941 static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
948 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
954 static int in_add_audio_effect(const struct audio_stream *stream,
955 effect_handle_t effect)
961 static int in_remove_audio_effect(const struct audio_stream *stream,
962 effect_handle_t effect)
968 static int audio_open_output_stream(struct audio_hw_device *dev,
969 audio_io_handle_t handle,
970 audio_devices_t devices,
971 audio_output_flags_t flags,
972 struct audio_config *config,
973 struct audio_stream_out **stream_out)
976 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
977 struct a2dp_stream_out *out;
978 struct audio_preset *preset;
979 const struct audio_codec *codec;
982 out = calloc(1, sizeof(struct a2dp_stream_out));
988 out->stream.common.get_sample_rate = out_get_sample_rate;
989 out->stream.common.set_sample_rate = out_set_sample_rate;
990 out->stream.common.get_buffer_size = out_get_buffer_size;
991 out->stream.common.get_channels = out_get_channels;
992 out->stream.common.get_format = out_get_format;
993 out->stream.common.set_format = out_set_format;
994 out->stream.common.standby = out_standby;
995 out->stream.common.dump = out_dump;
996 out->stream.common.set_parameters = out_set_parameters;
997 out->stream.common.get_parameters = out_get_parameters;
998 out->stream.common.add_audio_effect = out_add_audio_effect;
999 out->stream.common.remove_audio_effect = out_remove_audio_effect;
1000 out->stream.get_latency = out_get_latency;
1001 out->stream.set_volume = out_set_volume;
1002 out->stream.write = out_write;
1003 out->stream.get_render_position = out_get_render_position;
1005 /* TODO: for now we always use endpoint 0 */
1006 out->ep = &audio_endpoints[0];
1008 if (ipc_open_stream_cmd(out->ep->id, &mtu, &preset) !=
1009 AUDIO_STATUS_SUCCESS)
1015 codec = out->ep->codec;
1017 codec->init(preset, mtu, &out->ep->codec_data);
1018 codec->get_config(out->ep->codec_data, &out->cfg);
1020 DBG("rate=%d channels=%d format=%d", out->cfg.rate,
1021 out->cfg.channels, out->cfg.format);
1025 *stream_out = &out->stream;
1026 a2dp_dev->out = out;
1028 out->audio_state = AUDIO_A2DP_STATE_STANDBY;
1038 static void audio_close_output_stream(struct audio_hw_device *dev,
1039 struct audio_stream_out *stream)
1041 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
1042 struct audio_endpoint *ep = a2dp_dev->out->ep;
1046 ipc_close_stream_cmd(ep->id);
1048 ep->codec->cleanup(ep->codec_data);
1049 ep->codec_data = NULL;
1052 a2dp_dev->out = NULL;
1055 static int audio_set_parameters(struct audio_hw_device *dev,
1056 const char *kvpairs)
1058 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
1059 struct a2dp_stream_out *out = a2dp_dev->out;
1066 return out->stream.common.set_parameters((struct audio_stream *) out,
1070 static char *audio_get_parameters(const struct audio_hw_device *dev,
1077 static int audio_init_check(const struct audio_hw_device *dev)
1083 static int audio_set_voice_volume(struct audio_hw_device *dev, float volume)
1089 static int audio_set_master_volume(struct audio_hw_device *dev, float volume)
1095 static int audio_set_mode(struct audio_hw_device *dev, int mode)
1101 static int audio_set_mic_mute(struct audio_hw_device *dev, bool state)
1107 static int audio_get_mic_mute(const struct audio_hw_device *dev, bool *state)
1113 static size_t audio_get_input_buffer_size(const struct audio_hw_device *dev,
1114 const struct audio_config *config)
1120 static int audio_open_input_stream(struct audio_hw_device *dev,
1121 audio_io_handle_t handle,
1122 audio_devices_t devices,
1123 struct audio_config *config,
1124 struct audio_stream_in **stream_in)
1126 struct audio_stream_in *in;
1130 in = calloc(1, sizeof(struct audio_stream_in));
1134 in->common.get_sample_rate = in_get_sample_rate;
1135 in->common.set_sample_rate = in_set_sample_rate;
1136 in->common.get_buffer_size = in_get_buffer_size;
1137 in->common.get_channels = in_get_channels;
1138 in->common.get_format = in_get_format;
1139 in->common.set_format = in_set_format;
1140 in->common.standby = in_standby;
1141 in->common.dump = in_dump;
1142 in->common.set_parameters = in_set_parameters;
1143 in->common.get_parameters = in_get_parameters;
1144 in->common.add_audio_effect = in_add_audio_effect;
1145 in->common.remove_audio_effect = in_remove_audio_effect;
1146 in->set_gain = in_set_gain;
1148 in->get_input_frames_lost = in_get_input_frames_lost;
1155 static void audio_close_input_stream(struct audio_hw_device *dev,
1156 struct audio_stream_in *stream_in)
1162 static int audio_dump(const audio_hw_device_t *device, int fd)
1168 static int audio_close(hw_device_t *device)
1170 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *)device;
1174 pthread_mutex_lock(&close_mutex);
1175 audio_ipc_cleanup();
1176 close_thread = true;
1177 pthread_mutex_unlock(&close_mutex);
1179 pthread_join(ipc_th, NULL);
1188 static void *ipc_handler(void *data)
1196 DBG("Waiting for connection ...");
1197 audio_sk = accept(listen_sk, NULL, NULL);
1200 error("audio: Failed to accept socket: %d (%s)", err,
1205 DBG("Audio IPC: Connected");
1207 if (register_endpoints() != AUDIO_STATUS_SUCCESS) {
1208 error("audio: Failed to register endpoints");
1210 unregister_endpoints();
1212 shutdown(audio_sk, SHUT_RDWR);
1216 memset(&pfd, 0, sizeof(pfd));
1218 pfd.events = POLLHUP | POLLERR | POLLNVAL;
1220 /* Check if socket is still alive. Empty while loop.*/
1221 while (poll(&pfd, 1, -1) < 0 && errno == EINTR);
1223 if (pfd.revents & (POLLHUP | POLLERR | POLLNVAL)) {
1224 info("Audio HAL: Socket closed");
1228 /*Check if audio_dev is closed */
1229 pthread_mutex_lock(&close_mutex);
1230 done = close_thread;
1231 close_thread = false;
1232 pthread_mutex_unlock(&close_mutex);
1235 unregister_endpoints();
1237 info("Closing Audio IPC thread");
1241 static int audio_ipc_init(void)
1243 struct sockaddr_un addr;
1249 sk = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
1252 error("audio: Failed to create socket: %d (%s)", err,
1257 memset(&addr, 0, sizeof(addr));
1258 addr.sun_family = AF_UNIX;
1260 memcpy(addr.sun_path, BLUEZ_AUDIO_SK_PATH,
1261 sizeof(BLUEZ_AUDIO_SK_PATH));
1263 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1265 error("audio: Failed to bind socket: %d (%s)", err,
1270 if (listen(sk, 1) < 0) {
1272 error("audio: Failed to listen on the socket: %d (%s)", err,
1279 err = pthread_create(&ipc_th, NULL, ipc_handler, NULL);
1283 error("audio: Failed to start Audio IPC thread: %d (%s)",
1284 err, strerror(err));
1295 static int audio_open(const hw_module_t *module, const char *name,
1296 hw_device_t **device)
1298 struct a2dp_audio_dev *a2dp_dev;
1303 if (strcmp(name, AUDIO_HARDWARE_INTERFACE)) {
1304 error("audio: interface %s not matching [%s]", name,
1305 AUDIO_HARDWARE_INTERFACE);
1309 err = audio_ipc_init();
1313 a2dp_dev = calloc(1, sizeof(struct a2dp_audio_dev));
1317 a2dp_dev->dev.common.tag = HARDWARE_DEVICE_TAG;
1318 a2dp_dev->dev.common.version = AUDIO_DEVICE_API_VERSION_CURRENT;
1319 a2dp_dev->dev.common.module = (struct hw_module_t *) module;
1320 a2dp_dev->dev.common.close = audio_close;
1322 a2dp_dev->dev.init_check = audio_init_check;
1323 a2dp_dev->dev.set_voice_volume = audio_set_voice_volume;
1324 a2dp_dev->dev.set_master_volume = audio_set_master_volume;
1325 a2dp_dev->dev.set_mode = audio_set_mode;
1326 a2dp_dev->dev.set_mic_mute = audio_set_mic_mute;
1327 a2dp_dev->dev.get_mic_mute = audio_get_mic_mute;
1328 a2dp_dev->dev.set_parameters = audio_set_parameters;
1329 a2dp_dev->dev.get_parameters = audio_get_parameters;
1330 a2dp_dev->dev.get_input_buffer_size = audio_get_input_buffer_size;
1331 a2dp_dev->dev.open_output_stream = audio_open_output_stream;
1332 a2dp_dev->dev.close_output_stream = audio_close_output_stream;
1333 a2dp_dev->dev.open_input_stream = audio_open_input_stream;
1334 a2dp_dev->dev.close_input_stream = audio_close_input_stream;
1335 a2dp_dev->dev.dump = audio_dump;
1337 /* Note that &a2dp_dev->dev.common is the same pointer as a2dp_dev.
1338 * This results from the structure of following structs:a2dp_audio_dev,
1339 * audio_hw_device. We will rely on this later in the code.*/
1340 *device = &a2dp_dev->dev.common;
1345 static struct hw_module_methods_t hal_module_methods = {
1349 struct audio_module HAL_MODULE_INFO_SYM = {
1351 .tag = HARDWARE_MODULE_TAG,
1354 .id = AUDIO_HARDWARE_MODULE_ID,
1355 .name = "A2DP Bluez HW HAL",
1356 .author = "Intel Corporation",
1357 .methods = &hal_module_methods,