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"
34 static int listen_sk = -1;
35 static int audio_sk = -1;
36 static bool close_thread = false;
38 static pthread_t ipc_th = 0;
39 static pthread_mutex_t close_mutex = PTHREAD_MUTEX_INITIALIZER;
41 struct a2dp_audio_dev {
42 struct audio_hw_device dev;
43 struct audio_stream_out *out;
46 static void audio_ipc_cleanup(void)
49 shutdown(audio_sk, SHUT_RDWR);
54 static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
61 static uint32_t out_get_sample_rate(const struct audio_stream *stream)
67 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
73 static size_t out_get_buffer_size(const struct audio_stream *stream)
79 static uint32_t out_get_channels(const struct audio_stream *stream)
85 static audio_format_t out_get_format(const struct audio_stream *stream)
91 static int out_set_format(struct audio_stream *stream, audio_format_t format)
97 static int out_standby(struct audio_stream *stream)
103 static int out_dump(const struct audio_stream *stream, int fd)
109 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
115 static char *out_get_parameters(const struct audio_stream *stream,
122 static uint32_t out_get_latency(const struct audio_stream_out *stream)
128 static int out_set_volume(struct audio_stream_out *stream, float left,
132 /* volume controlled in audioflinger mixer (digital) */
136 static int out_get_render_position(const struct audio_stream_out *stream,
137 uint32_t *dsp_frames)
143 static int out_add_audio_effect(const struct audio_stream *stream,
144 effect_handle_t effect)
150 static int out_remove_audio_effect(const struct audio_stream *stream,
151 effect_handle_t effect)
157 static uint32_t in_get_sample_rate(const struct audio_stream *stream)
163 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
169 static size_t in_get_buffer_size(const struct audio_stream *stream)
175 static uint32_t in_get_channels(const struct audio_stream *stream)
181 static audio_format_t in_get_format(const struct audio_stream *stream)
187 static int in_set_format(struct audio_stream *stream, audio_format_t format)
193 static int in_standby(struct audio_stream *stream)
199 static int in_dump(const struct audio_stream *stream, int fd)
205 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
211 static char *in_get_parameters(const struct audio_stream *stream,
218 static int in_set_gain(struct audio_stream_in *stream, float gain)
224 static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
231 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
237 static int in_add_audio_effect(const struct audio_stream *stream,
238 effect_handle_t effect)
244 static int in_remove_audio_effect(const struct audio_stream *stream,
245 effect_handle_t effect)
251 static int audio_open_output_stream(struct audio_hw_device *dev,
252 audio_io_handle_t handle,
253 audio_devices_t devices,
254 audio_output_flags_t flags,
255 struct audio_config *config,
256 struct audio_stream_out **stream_out)
259 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
260 struct audio_stream_out *out;
262 out = calloc(1, sizeof(struct audio_stream_out));
268 out->common.get_sample_rate = out_get_sample_rate;
269 out->common.set_sample_rate = out_set_sample_rate;
270 out->common.get_buffer_size = out_get_buffer_size;
271 out->common.get_channels = out_get_channels;
272 out->common.get_format = out_get_format;
273 out->common.set_format = out_set_format;
274 out->common.standby = out_standby;
275 out->common.dump = out_dump;
276 out->common.set_parameters = out_set_parameters;
277 out->common.get_parameters = out_get_parameters;
278 out->common.add_audio_effect = out_add_audio_effect;
279 out->common.remove_audio_effect = out_remove_audio_effect;
280 out->get_latency = out_get_latency;
281 out->set_volume = out_set_volume;
282 out->write = out_write;
283 out->get_render_position = out_get_render_position;
291 static void audio_close_output_stream(struct audio_hw_device *dev,
292 struct audio_stream_out *stream)
294 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
299 a2dp_dev->out = NULL;
302 static int audio_set_parameters(struct audio_hw_device *dev,
309 static char *audio_get_parameters(const struct audio_hw_device *dev,
316 static int audio_init_check(const struct audio_hw_device *dev)
322 static int audio_set_voice_volume(struct audio_hw_device *dev, float volume)
328 static int audio_set_master_volume(struct audio_hw_device *dev, float volume)
334 static int audio_set_mode(struct audio_hw_device *dev, int mode)
340 static int audio_set_mic_mute(struct audio_hw_device *dev, bool state)
346 static int audio_get_mic_mute(const struct audio_hw_device *dev, bool *state)
352 static size_t audio_get_input_buffer_size(const struct audio_hw_device *dev,
353 const struct audio_config *config)
359 static int audio_open_input_stream(struct audio_hw_device *dev,
360 audio_io_handle_t handle,
361 audio_devices_t devices,
362 struct audio_config *config,
363 struct audio_stream_in **stream_in)
365 struct audio_stream_in *in;
369 in = calloc(1, sizeof(struct audio_stream_in));
373 in->common.get_sample_rate = in_get_sample_rate;
374 in->common.set_sample_rate = in_set_sample_rate;
375 in->common.get_buffer_size = in_get_buffer_size;
376 in->common.get_channels = in_get_channels;
377 in->common.get_format = in_get_format;
378 in->common.set_format = in_set_format;
379 in->common.standby = in_standby;
380 in->common.dump = in_dump;
381 in->common.set_parameters = in_set_parameters;
382 in->common.get_parameters = in_get_parameters;
383 in->common.add_audio_effect = in_add_audio_effect;
384 in->common.remove_audio_effect = in_remove_audio_effect;
385 in->set_gain = in_set_gain;
387 in->get_input_frames_lost = in_get_input_frames_lost;
394 static void audio_close_input_stream(struct audio_hw_device *dev,
395 struct audio_stream_in *stream_in)
401 static int audio_dump(const audio_hw_device_t *device, int fd)
407 static int audio_close(hw_device_t *device)
409 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *)device;
413 pthread_mutex_lock(&close_mutex);
416 pthread_mutex_unlock(&close_mutex);
418 pthread_join(ipc_th, NULL);
427 static void *ipc_handler(void *data)
435 DBG("Waiting for connection ...");
436 audio_sk = accept(listen_sk, NULL, NULL);
439 error("audio: Failed to accept socket: %d (%s)", err,
444 DBG("Audio IPC: Connected");
446 memset(&pfd, 0, sizeof(pfd));
448 pfd.events = POLLHUP | POLLERR | POLLNVAL;
450 /* Check if socket is still alive. Empty while loop.*/
451 while (poll(&pfd, 1, -1) < 0 && errno == EINTR);
453 if (pfd.revents & (POLLHUP | POLLERR | POLLNVAL)) {
454 info("Audio HAL: Socket closed");
458 /*Check if audio_dev is closed */
459 pthread_mutex_lock(&close_mutex);
461 close_thread = false;
462 pthread_mutex_unlock(&close_mutex);
465 info("Closing Audio IPC thread");
469 static int audio_ipc_init(void)
471 struct sockaddr_un addr;
477 sk = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
480 error("audio: Failed to create socket: %d (%s)", err,
485 memset(&addr, 0, sizeof(addr));
486 addr.sun_family = AF_UNIX;
488 memcpy(addr.sun_path, BLUEZ_AUDIO_SK_PATH,
489 sizeof(BLUEZ_AUDIO_SK_PATH));
491 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
493 error("audio: Failed to bind socket: %d (%s)", err,
498 if (listen(sk, 1) < 0) {
500 error("audio: Failed to listen on the socket: %d (%s)", err,
507 err = pthread_create(&ipc_th, NULL, ipc_handler, NULL);
511 error("audio: Failed to start Audio IPC thread: %d (%s)",
523 static int audio_open(const hw_module_t *module, const char *name,
524 hw_device_t **device)
526 struct a2dp_audio_dev *a2dp_dev;
531 if (strcmp(name, AUDIO_HARDWARE_INTERFACE)) {
532 error("audio: interface %s not matching [%s]", name,
533 AUDIO_HARDWARE_INTERFACE);
537 err = audio_ipc_init();
541 a2dp_dev = calloc(1, sizeof(struct a2dp_audio_dev));
545 a2dp_dev->dev.common.version = AUDIO_DEVICE_API_VERSION_CURRENT;
546 a2dp_dev->dev.common.module = (struct hw_module_t *) module;
547 a2dp_dev->dev.common.close = audio_close;
549 a2dp_dev->dev.init_check = audio_init_check;
550 a2dp_dev->dev.set_voice_volume = audio_set_voice_volume;
551 a2dp_dev->dev.set_master_volume = audio_set_master_volume;
552 a2dp_dev->dev.set_mode = audio_set_mode;
553 a2dp_dev->dev.set_mic_mute = audio_set_mic_mute;
554 a2dp_dev->dev.get_mic_mute = audio_get_mic_mute;
555 a2dp_dev->dev.set_parameters = audio_set_parameters;
556 a2dp_dev->dev.get_parameters = audio_get_parameters;
557 a2dp_dev->dev.get_input_buffer_size = audio_get_input_buffer_size;
558 a2dp_dev->dev.open_output_stream = audio_open_output_stream;
559 a2dp_dev->dev.close_output_stream = audio_close_output_stream;
560 a2dp_dev->dev.open_input_stream = audio_open_input_stream;
561 a2dp_dev->dev.close_input_stream = audio_close_input_stream;
562 a2dp_dev->dev.dump = audio_dump;
564 /* Note that &a2dp_dev->dev.common is the same pointer as a2dp_dev.
565 * This results from the structure of following structs:a2dp_audio_dev,
566 * audio_hw_device. We will rely on this later in the code.*/
567 *device = &a2dp_dev->dev.common;
572 static struct hw_module_methods_t hal_module_methods = {
576 struct audio_module HAL_MODULE_INFO_SYM = {
578 .tag = HARDWARE_MODULE_TAG,
581 .id = AUDIO_HARDWARE_MODULE_ID,
582 .name = "A2DP Bluez HW HAL",
583 .author = "Intel Corporation",
584 .methods = &hal_module_methods,