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>
31 #include "audio-msg.h"
35 static int listen_sk = -1;
36 static int audio_sk = -1;
37 static bool close_thread = false;
39 static pthread_t ipc_th = 0;
40 static pthread_mutex_t close_mutex = PTHREAD_MUTEX_INITIALIZER;
41 static pthread_mutex_t sk_mutex = PTHREAD_MUTEX_INITIALIZER;
43 struct a2dp_audio_dev {
44 struct audio_hw_device dev;
45 struct audio_stream_out *out;
48 static void audio_ipc_cleanup(void)
51 shutdown(audio_sk, SHUT_RDWR);
56 static int audio_ipc_cmd(uint8_t service_id, uint8_t opcode, uint16_t len,
57 void *param, size_t *rsp_len, void *rsp, int *fd)
63 char cmsgbuf[CMSG_SPACE(sizeof(int))];
65 size_t s_len = sizeof(s);
68 error("audio: Invalid cmd socket passed to audio_ipc_cmd");
72 if (!rsp || !rsp_len) {
78 memset(&msg, 0, sizeof(msg));
79 memset(&cmd, 0, sizeof(cmd));
81 cmd.service_id = service_id;
85 iv[0].iov_base = &cmd;
86 iv[0].iov_len = sizeof(cmd);
88 iv[1].iov_base = param;
94 pthread_mutex_lock(&sk_mutex);
96 ret = sendmsg(audio_sk, &msg, 0);
98 error("audio: Sending command failed:%s", strerror(errno));
99 pthread_mutex_unlock(&sk_mutex);
103 /* socket was shutdown */
105 error("audio: Command socket closed");
109 memset(&msg, 0, sizeof(msg));
110 memset(&cmd, 0, sizeof(cmd));
112 iv[0].iov_base = &cmd;
113 iv[0].iov_len = sizeof(cmd);
115 iv[1].iov_base = rsp;
116 iv[1].iov_len = *rsp_len;
122 memset(cmsgbuf, 0, sizeof(cmsgbuf));
123 msg.msg_control = cmsgbuf;
124 msg.msg_controllen = sizeof(cmsgbuf);
127 ret = recvmsg(audio_sk, &msg, 0);
129 error("audio: Receiving command response failed:%s",
131 pthread_mutex_unlock(&sk_mutex);
135 pthread_mutex_unlock(&sk_mutex);
137 if (ret < (ssize_t) sizeof(cmd)) {
138 error("audio: Too small response received(%zd bytes)", ret);
142 if (cmd.service_id != service_id) {
143 error("audio: Invalid service id (%u vs %u)", cmd.service_id,
148 if (ret != (ssize_t) (sizeof(cmd) + cmd.len)) {
149 error("audio: Malformed response received(%zd bytes)", ret);
153 if (cmd.opcode != opcode && cmd.opcode != AUDIO_OP_STATUS) {
154 error("audio: Invalid opcode received (%u vs %u)",
159 if (cmd.opcode == AUDIO_OP_STATUS) {
160 struct hal_status *s = rsp;
162 if (sizeof(*s) != cmd.len) {
163 error("audio: Invalid status length");
167 if (s->code == AUDIO_STATUS_SUCCESS) {
168 error("audio: Invalid success status response");
175 /* Receive auxiliary data in msg */
177 struct cmsghdr *cmsg;
181 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
182 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
183 if (cmsg->cmsg_level == SOL_SOCKET
184 && cmsg->cmsg_type == SCM_RIGHTS) {
185 memcpy(fd, CMSG_DATA(cmsg), sizeof(int));
194 return AUDIO_STATUS_SUCCESS;
197 /* Some serious issue happen on IPC - recover */
198 shutdown(audio_sk, SHUT_RDWR);
200 return AUDIO_STATUS_FAILED;
203 static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
210 static uint32_t out_get_sample_rate(const struct audio_stream *stream)
216 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
222 static size_t out_get_buffer_size(const struct audio_stream *stream)
228 static uint32_t out_get_channels(const struct audio_stream *stream)
234 static audio_format_t out_get_format(const struct audio_stream *stream)
240 static int out_set_format(struct audio_stream *stream, audio_format_t format)
246 static int out_standby(struct audio_stream *stream)
252 static int out_dump(const struct audio_stream *stream, int fd)
258 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
264 static char *out_get_parameters(const struct audio_stream *stream,
271 static uint32_t out_get_latency(const struct audio_stream_out *stream)
277 static int out_set_volume(struct audio_stream_out *stream, float left,
281 /* volume controlled in audioflinger mixer (digital) */
285 static int out_get_render_position(const struct audio_stream_out *stream,
286 uint32_t *dsp_frames)
292 static int out_add_audio_effect(const struct audio_stream *stream,
293 effect_handle_t effect)
299 static int out_remove_audio_effect(const struct audio_stream *stream,
300 effect_handle_t effect)
306 static uint32_t in_get_sample_rate(const struct audio_stream *stream)
312 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
318 static size_t in_get_buffer_size(const struct audio_stream *stream)
324 static uint32_t in_get_channels(const struct audio_stream *stream)
330 static audio_format_t in_get_format(const struct audio_stream *stream)
336 static int in_set_format(struct audio_stream *stream, audio_format_t format)
342 static int in_standby(struct audio_stream *stream)
348 static int in_dump(const struct audio_stream *stream, int fd)
354 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
360 static char *in_get_parameters(const struct audio_stream *stream,
367 static int in_set_gain(struct audio_stream_in *stream, float gain)
373 static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
380 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
386 static int in_add_audio_effect(const struct audio_stream *stream,
387 effect_handle_t effect)
393 static int in_remove_audio_effect(const struct audio_stream *stream,
394 effect_handle_t effect)
400 static int audio_open_output_stream(struct audio_hw_device *dev,
401 audio_io_handle_t handle,
402 audio_devices_t devices,
403 audio_output_flags_t flags,
404 struct audio_config *config,
405 struct audio_stream_out **stream_out)
408 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
409 struct audio_stream_out *out;
411 out = calloc(1, sizeof(struct audio_stream_out));
417 out->common.get_sample_rate = out_get_sample_rate;
418 out->common.set_sample_rate = out_set_sample_rate;
419 out->common.get_buffer_size = out_get_buffer_size;
420 out->common.get_channels = out_get_channels;
421 out->common.get_format = out_get_format;
422 out->common.set_format = out_set_format;
423 out->common.standby = out_standby;
424 out->common.dump = out_dump;
425 out->common.set_parameters = out_set_parameters;
426 out->common.get_parameters = out_get_parameters;
427 out->common.add_audio_effect = out_add_audio_effect;
428 out->common.remove_audio_effect = out_remove_audio_effect;
429 out->get_latency = out_get_latency;
430 out->set_volume = out_set_volume;
431 out->write = out_write;
432 out->get_render_position = out_get_render_position;
440 static void audio_close_output_stream(struct audio_hw_device *dev,
441 struct audio_stream_out *stream)
443 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
448 a2dp_dev->out = NULL;
451 static int audio_set_parameters(struct audio_hw_device *dev,
458 static char *audio_get_parameters(const struct audio_hw_device *dev,
465 static int audio_init_check(const struct audio_hw_device *dev)
471 static int audio_set_voice_volume(struct audio_hw_device *dev, float volume)
477 static int audio_set_master_volume(struct audio_hw_device *dev, float volume)
483 static int audio_set_mode(struct audio_hw_device *dev, int mode)
489 static int audio_set_mic_mute(struct audio_hw_device *dev, bool state)
495 static int audio_get_mic_mute(const struct audio_hw_device *dev, bool *state)
501 static size_t audio_get_input_buffer_size(const struct audio_hw_device *dev,
502 const struct audio_config *config)
508 static int audio_open_input_stream(struct audio_hw_device *dev,
509 audio_io_handle_t handle,
510 audio_devices_t devices,
511 struct audio_config *config,
512 struct audio_stream_in **stream_in)
514 struct audio_stream_in *in;
518 in = calloc(1, sizeof(struct audio_stream_in));
522 in->common.get_sample_rate = in_get_sample_rate;
523 in->common.set_sample_rate = in_set_sample_rate;
524 in->common.get_buffer_size = in_get_buffer_size;
525 in->common.get_channels = in_get_channels;
526 in->common.get_format = in_get_format;
527 in->common.set_format = in_set_format;
528 in->common.standby = in_standby;
529 in->common.dump = in_dump;
530 in->common.set_parameters = in_set_parameters;
531 in->common.get_parameters = in_get_parameters;
532 in->common.add_audio_effect = in_add_audio_effect;
533 in->common.remove_audio_effect = in_remove_audio_effect;
534 in->set_gain = in_set_gain;
536 in->get_input_frames_lost = in_get_input_frames_lost;
543 static void audio_close_input_stream(struct audio_hw_device *dev,
544 struct audio_stream_in *stream_in)
550 static int audio_dump(const audio_hw_device_t *device, int fd)
556 static int audio_close(hw_device_t *device)
558 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *)device;
562 pthread_mutex_lock(&close_mutex);
565 pthread_mutex_unlock(&close_mutex);
567 pthread_join(ipc_th, NULL);
576 static void *ipc_handler(void *data)
584 DBG("Waiting for connection ...");
585 audio_sk = accept(listen_sk, NULL, NULL);
588 error("audio: Failed to accept socket: %d (%s)", err,
593 DBG("Audio IPC: Connected");
595 memset(&pfd, 0, sizeof(pfd));
597 pfd.events = POLLHUP | POLLERR | POLLNVAL;
599 /* Check if socket is still alive. Empty while loop.*/
600 while (poll(&pfd, 1, -1) < 0 && errno == EINTR);
602 if (pfd.revents & (POLLHUP | POLLERR | POLLNVAL)) {
603 info("Audio HAL: Socket closed");
607 /*Check if audio_dev is closed */
608 pthread_mutex_lock(&close_mutex);
610 close_thread = false;
611 pthread_mutex_unlock(&close_mutex);
614 info("Closing Audio IPC thread");
618 static int audio_ipc_init(void)
620 struct sockaddr_un addr;
626 sk = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
629 error("audio: Failed to create socket: %d (%s)", err,
634 memset(&addr, 0, sizeof(addr));
635 addr.sun_family = AF_UNIX;
637 memcpy(addr.sun_path, BLUEZ_AUDIO_SK_PATH,
638 sizeof(BLUEZ_AUDIO_SK_PATH));
640 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
642 error("audio: Failed to bind socket: %d (%s)", err,
647 if (listen(sk, 1) < 0) {
649 error("audio: Failed to listen on the socket: %d (%s)", err,
656 err = pthread_create(&ipc_th, NULL, ipc_handler, NULL);
660 error("audio: Failed to start Audio IPC thread: %d (%s)",
672 static int audio_open(const hw_module_t *module, const char *name,
673 hw_device_t **device)
675 struct a2dp_audio_dev *a2dp_dev;
680 if (strcmp(name, AUDIO_HARDWARE_INTERFACE)) {
681 error("audio: interface %s not matching [%s]", name,
682 AUDIO_HARDWARE_INTERFACE);
686 err = audio_ipc_init();
690 a2dp_dev = calloc(1, sizeof(struct a2dp_audio_dev));
694 a2dp_dev->dev.common.version = AUDIO_DEVICE_API_VERSION_CURRENT;
695 a2dp_dev->dev.common.module = (struct hw_module_t *) module;
696 a2dp_dev->dev.common.close = audio_close;
698 a2dp_dev->dev.init_check = audio_init_check;
699 a2dp_dev->dev.set_voice_volume = audio_set_voice_volume;
700 a2dp_dev->dev.set_master_volume = audio_set_master_volume;
701 a2dp_dev->dev.set_mode = audio_set_mode;
702 a2dp_dev->dev.set_mic_mute = audio_set_mic_mute;
703 a2dp_dev->dev.get_mic_mute = audio_get_mic_mute;
704 a2dp_dev->dev.set_parameters = audio_set_parameters;
705 a2dp_dev->dev.get_parameters = audio_get_parameters;
706 a2dp_dev->dev.get_input_buffer_size = audio_get_input_buffer_size;
707 a2dp_dev->dev.open_output_stream = audio_open_output_stream;
708 a2dp_dev->dev.close_output_stream = audio_close_output_stream;
709 a2dp_dev->dev.open_input_stream = audio_open_input_stream;
710 a2dp_dev->dev.close_input_stream = audio_close_input_stream;
711 a2dp_dev->dev.dump = audio_dump;
713 /* Note that &a2dp_dev->dev.common is the same pointer as a2dp_dev.
714 * This results from the structure of following structs:a2dp_audio_dev,
715 * audio_hw_device. We will rely on this later in the code.*/
716 *device = &a2dp_dev->dev.common;
721 static struct hw_module_methods_t hal_module_methods = {
725 struct audio_module HAL_MODULE_INFO_SYM = {
727 .tag = HARDWARE_MODULE_TAG,
730 .id = AUDIO_HARDWARE_MODULE_ID,
731 .name = "A2DP Bluez HW HAL",
732 .author = "Intel Corporation",
733 .methods = &hal_module_methods,